code
stringlengths
81
54k
code_codestyle
int64
0
721
style_context
stringlengths
91
41.9k
style_context_codestyle
int64
0
699
label
int64
0
1
import sys import webbrowser import requests from bsa import BeautifulSoup from fake_useragent import UserAgent if __name__ == "__main__": print("Googling.....") _snake_case = '''https://www.google.com/search?q=''' + ''' '''.join(sys.argv[1:]) _snake_case = 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(10000): out_file.write(data) _snake_case = BeautifulSoup(res.text, "html.parser") _snake_case = 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")}""")
721
from __future__ import annotations def lowerCAmelCase_ ( snake_case_,snake_case_ = None ): _A : Tuple = word_bank or [] # create a table _A : int = len(snake_case_ ) + 1 _A : list[list[list[str]]] = [] for _ in range(snake_case_ ): table.append([] ) # seed value _A : Dict = [[]] # because empty string has empty combination # iterate through the indices for i in range(snake_case_ ): # condition if table[i] != []: for word in word_bank: # slice condition if target[i : i + len(snake_case_ )] == word: _A : list[list[str]] = [ [word, *way] for way in table[i] ] # adds the word to every combination the current position holds # now,push that combination to the table[i+len(word)] table[i + len(snake_case_ )] += new_combinations # combinations are in reverse order so reverse for better output for combination in table[len(snake_case_ )]: combination.reverse() return table[len(snake_case_ )] if __name__ == "__main__": print(all_construct("jwajalapa", ["jwa", "j", "w", "a", "la", "lapa"])) print(all_construct("rajamati", ["s", "raj", "amat", "raja", "ma", "i", "t"])) print( all_construct( "hexagonosaurus", ["h", "ex", "hex", "ag", "ago", "ru", "auru", "rus", "go", "no", "o", "s"], ) )
54
0
import random def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_ ): _A : List[Any] = a[left_index] _A : Any = left_index + 1 for j in range(left_index + 1,snake_case__ ): if a[j] < pivot: _A , _A : Tuple = a[i], a[j] i += 1 _A , _A : List[str] = a[i - 1], a[left_index] return i - 1 def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_ ): if left < right: _A : List[Any] = random.randint(snake_case__,right - 1 ) _A , _A : Optional[int] = ( a[left], a[pivot], ) # switches the pivot with the left most bound _A : Union[str, Any] = partition(snake_case__,snake_case__,snake_case__ ) quick_sort_random( snake_case__,snake_case__,snake_case__ ) # recursive quicksort to the left of the pivot point quick_sort_random( snake_case__,pivot_index + 1,snake_case__ ) # recursive quicksort to the right of the pivot point def lowerCAmelCase_ ( ): _A : Any = input("""Enter numbers separated by a comma:\n""" ).strip() _A : List[Any] = [int(snake_case__ ) for item in user_input.split(""",""" )] quick_sort_random(snake_case__,0,len(snake_case__ ) ) print(snake_case__ ) if __name__ == "__main__": main()
700
import operator def lowerCAmelCase_ ( snake_case_,snake_case_ = False,snake_case_ = None ): _A : str = operator.lt if reverse else operator.gt _A : Optional[Any] = solution or [] if not arr: return solution _A : Dict = [arr.pop(0 )] for i, item in enumerate(snake_case_ ): if _operator(snake_case_,sublist[-1] ): sublist.append(snake_case_ ) arr.pop(snake_case_ ) # merging sublist into solution list if not solution: solution.extend(snake_case_ ) else: while sublist: _A : Union[str, Any] = sublist.pop(0 ) for i, xx in enumerate(snake_case_ ): if not _operator(snake_case_,snake_case_ ): solution.insert(snake_case_,snake_case_ ) break else: solution.append(snake_case_ ) strand_sort(snake_case_,snake_case_,snake_case_ ) return solution if __name__ == "__main__": assert strand_sort([4, 3, 5, 1, 2]) == [1, 2, 3, 4, 5] assert strand_sort([4, 3, 5, 1, 2], reverse=True) == [5, 4, 3, 2, 1]
54
0
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 _snake_case = logging.get_logger(__name__) _snake_case = { "hustvl/yolos-small": "https://huggingface.co/hustvl/yolos-small/resolve/main/config.json", # See all YOLOS models at https://huggingface.co/models?filter=yolos } class lowercase ( lowercase__ ): _a = 'yolos' def __init__( self , _a=768 , _a=12 , _a=12 , _a=3072 , _a="gelu" , _a=0.0 , _a=0.0 , _a=0.02 , _a=1e-12 , _a=[512, 864] , _a=16 , _a=3 , _a=True , _a=100 , _a=True , _a=False , _a=1 , _a=5 , _a=2 , _a=5 , _a=2 , _a=0.1 , **_a , ) -> Any: super().__init__(**_a ) _A : Dict = hidden_size _A : str = num_hidden_layers _A : List[Any] = num_attention_heads _A : Optional[Any] = intermediate_size _A : Optional[Any] = hidden_act _A : Any = hidden_dropout_prob _A : Optional[int] = attention_probs_dropout_prob _A : Dict = initializer_range _A : Tuple = layer_norm_eps _A : Optional[Any] = image_size _A : List[str] = patch_size _A : Optional[Any] = num_channels _A : Tuple = qkv_bias _A : Tuple = num_detection_tokens _A : Any = use_mid_position_embeddings _A : Any = auxiliary_loss # Hungarian matcher _A : Optional[Any] = class_cost _A : Any = bbox_cost _A : Optional[int] = giou_cost # Loss coefficients _A : Tuple = bbox_loss_coefficient _A : Optional[int] = giou_loss_coefficient _A : Union[str, Any] = eos_coefficient class lowercase ( lowercase__ ): _a = version.parse("1.11" ) @property def a__ ( self ) -> Dict: return OrderedDict( [ ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}), ] ) @property def a__ ( self ) -> Optional[Any]: return 1e-4 @property def a__ ( self ) -> List[Any]: return 12
701
import gc import unittest from transformers import MODEL_FOR_MASKED_LM_MAPPING, TF_MODEL_FOR_MASKED_LM_MAPPING, FillMaskPipeline, pipeline from transformers.pipelines import PipelineException from transformers.testing_utils import ( is_pipeline_test, is_torch_available, nested_simplify, require_tf, require_torch, require_torch_gpu, slow, ) from .test_pipelines_common import ANY @is_pipeline_test class lowercase ( unittest.TestCase ): _a = MODEL_FOR_MASKED_LM_MAPPING _a = TF_MODEL_FOR_MASKED_LM_MAPPING def a__ ( self ) -> Tuple: super().tearDown() # clean-up as much as possible GPU memory occupied by PyTorch gc.collect() if is_torch_available(): import torch torch.cuda.empty_cache() @require_tf def a__ ( self ) -> Any: _A : Optional[Any] = pipeline(task="""fill-mask""" , model="""sshleifer/tiny-distilroberta-base""" , top_k=2 , framework="""tf""" ) _A : Optional[int] = unmasker("""My name is <mask>""" ) self.assertEqual( nested_simplify(_a , decimals=6 ) , [ {"""sequence""": """My name is grouped""", """score""": 2.1e-05, """token""": 3_8015, """token_str""": """ grouped"""}, {"""sequence""": """My name is accuser""", """score""": 2.1e-05, """token""": 2_5506, """token_str""": """ accuser"""}, ] , ) _A : Tuple = unmasker("""The largest city in France is <mask>""" ) self.assertEqual( nested_simplify(_a , decimals=6 ) , [ { """sequence""": """The largest city in France is grouped""", """score""": 2.1e-05, """token""": 3_8015, """token_str""": """ grouped""", }, { """sequence""": """The largest city in France is accuser""", """score""": 2.1e-05, """token""": 2_5506, """token_str""": """ accuser""", }, ] , ) _A : List[str] = unmasker("""My name is <mask>""" , targets=[""" Patrick""", """ Clara""", """ Teven"""] , top_k=3 ) self.assertEqual( nested_simplify(_a , decimals=6 ) , [ {"""sequence""": """My name is Clara""", """score""": 2e-05, """token""": 1_3606, """token_str""": """ Clara"""}, {"""sequence""": """My name is Patrick""", """score""": 2e-05, """token""": 3499, """token_str""": """ Patrick"""}, {"""sequence""": """My name is Te""", """score""": 1.9e-05, """token""": 2941, """token_str""": """ Te"""}, ] , ) @require_torch def a__ ( self ) -> str: _A : Any = pipeline(task="""fill-mask""" , model="""sshleifer/tiny-distilroberta-base""" , top_k=2 , framework="""pt""" ) _A : List[Any] = unmasker("""My name is <mask>""" ) self.assertEqual( nested_simplify(_a , decimals=6 ) , [ {"""sequence""": """My name is Maul""", """score""": 2.2e-05, """token""": 3_5676, """token_str""": """ Maul"""}, {"""sequence""": """My name isELS""", """score""": 2.2e-05, """token""": 1_6416, """token_str""": """ELS"""}, ] , ) _A : Optional[Any] = unmasker("""The largest city in France is <mask>""" ) self.assertEqual( nested_simplify(_a , decimals=6 ) , [ { """sequence""": """The largest city in France is Maul""", """score""": 2.2e-05, """token""": 3_5676, """token_str""": """ Maul""", }, {"""sequence""": """The largest city in France isELS""", """score""": 2.2e-05, """token""": 1_6416, """token_str""": """ELS"""}, ] , ) _A : Optional[int] = unmasker("""My name is <mask>""" , targets=[""" Patrick""", """ Clara""", """ Teven"""] , top_k=3 ) self.assertEqual( nested_simplify(_a , decimals=6 ) , [ {"""sequence""": """My name is Patrick""", """score""": 2.1e-05, """token""": 3499, """token_str""": """ Patrick"""}, {"""sequence""": """My name is Te""", """score""": 2e-05, """token""": 2941, """token_str""": """ Te"""}, {"""sequence""": """My name is Clara""", """score""": 2e-05, """token""": 1_3606, """token_str""": """ Clara"""}, ] , ) _A : str = unmasker("""My name is <mask> <mask>""" , top_k=2 ) self.assertEqual( nested_simplify(_a , decimals=6 ) , [ [ { """score""": 2.2e-05, """token""": 3_5676, """token_str""": """ Maul""", """sequence""": """<s>My name is Maul<mask></s>""", }, {"""score""": 2.2e-05, """token""": 1_6416, """token_str""": """ELS""", """sequence""": """<s>My name isELS<mask></s>"""}, ], [ { """score""": 2.2e-05, """token""": 3_5676, """token_str""": """ Maul""", """sequence""": """<s>My name is<mask> Maul</s>""", }, {"""score""": 2.2e-05, """token""": 1_6416, """token_str""": """ELS""", """sequence""": """<s>My name is<mask>ELS</s>"""}, ], ] , ) @require_torch_gpu def a__ ( self ) -> Union[str, Any]: _A : int = pipeline("""fill-mask""" , model="""hf-internal-testing/tiny-random-distilbert""" , device=0 , framework="""pt""" ) # convert model to fp16 pipe.model.half() _A : Optional[Any] = pipe("""Paris is the [MASK] of France.""" ) # We actually don't care about the result, we just want to make sure # it works, meaning the float16 tensor got casted back to float32 # for postprocessing. self.assertIsInstance(_a , _a ) @slow @require_torch def a__ ( self ) -> Optional[int]: _A : Optional[Any] = pipeline(task="""fill-mask""" , model="""distilroberta-base""" , top_k=2 , framework="""pt""" ) self.run_large_test(_a ) @slow @require_tf def a__ ( self ) -> Tuple: _A : str = pipeline(task="""fill-mask""" , model="""distilroberta-base""" , top_k=2 , framework="""tf""" ) self.run_large_test(_a ) def a__ ( self , _a ) -> Tuple: _A : Optional[int] = unmasker("""My name is <mask>""" ) self.assertEqual( nested_simplify(_a ) , [ {"""sequence""": """My name is John""", """score""": 0.008, """token""": 610, """token_str""": """ John"""}, {"""sequence""": """My name is Chris""", """score""": 0.007, """token""": 1573, """token_str""": """ Chris"""}, ] , ) _A : int = unmasker("""The largest city in France is <mask>""" ) self.assertEqual( nested_simplify(_a ) , [ { """sequence""": """The largest city in France is Paris""", """score""": 0.251, """token""": 2201, """token_str""": """ Paris""", }, { """sequence""": """The largest city in France is Lyon""", """score""": 0.214, """token""": 1_2790, """token_str""": """ Lyon""", }, ] , ) _A : Optional[Any] = unmasker("""My name is <mask>""" , targets=[""" Patrick""", """ Clara""", """ Teven"""] , top_k=3 ) self.assertEqual( nested_simplify(_a ) , [ {"""sequence""": """My name is Patrick""", """score""": 0.005, """token""": 3499, """token_str""": """ Patrick"""}, {"""sequence""": """My name is Clara""", """score""": 0.000, """token""": 1_3606, """token_str""": """ Clara"""}, {"""sequence""": """My name is Te""", """score""": 0.000, """token""": 2941, """token_str""": """ Te"""}, ] , ) @require_torch def a__ ( self ) -> Tuple: _A : List[str] = pipeline(task="""fill-mask""" , model="""sshleifer/tiny-distilroberta-base""" , framework="""pt""" ) _A : str = None _A : Union[str, Any] = None self.run_pipeline_test(_a , [] ) @require_tf def a__ ( self ) -> Union[str, Any]: _A : Tuple = pipeline(task="""fill-mask""" , model="""sshleifer/tiny-distilroberta-base""" , framework="""tf""" ) _A : Any = None _A : Dict = None self.run_pipeline_test(_a , [] ) def a__ ( self , _a , _a , _a ) -> Any: if tokenizer is None or tokenizer.mask_token_id is None: self.skipTest("""The provided tokenizer has no mask token, (probably reformer or wav2vec2)""" ) _A : Optional[Any] = FillMaskPipeline(model=_a , tokenizer=_a ) _A : Tuple = [ F'''This is another {tokenizer.mask_token} test''', ] return fill_masker, examples def a__ ( self , _a , _a ) -> Dict: _A : Dict = fill_masker.tokenizer _A : List[str] = fill_masker.model _A : List[str] = fill_masker( F'''This is a {tokenizer.mask_token}''' , ) self.assertEqual( _a , [ {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, ] , ) _A : Optional[Any] = fill_masker([F'''This is a {tokenizer.mask_token}'''] ) self.assertEqual( _a , [ {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, ] , ) _A : List[str] = fill_masker([F'''This is a {tokenizer.mask_token}''', F'''Another {tokenizer.mask_token} great test.'''] ) self.assertEqual( _a , [ [ {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, ], [ {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, ], ] , ) with self.assertRaises(_a ): fill_masker([None] ) # No mask_token is not supported with self.assertRaises(_a ): fill_masker("""This is""" ) self.run_test_top_k(_a , _a ) self.run_test_targets(_a , _a ) self.run_test_top_k_targets(_a , _a ) self.fill_mask_with_duplicate_targets_and_top_k(_a , _a ) self.fill_mask_with_multiple_masks(_a , _a ) def a__ ( self , _a , _a ) -> List[str]: _A : int = tokenizer.get_vocab() _A : str = sorted(vocab.keys() )[:2] # Pipeline argument _A : Tuple = FillMaskPipeline(model=_a , tokenizer=_a , targets=_a ) _A : Optional[int] = fill_masker(F'''This is a {tokenizer.mask_token}''' ) self.assertEqual( _a , [ {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, ] , ) _A : List[str] = {vocab[el] for el in targets} self.assertEqual({el["""token"""] for el in outputs} , _a ) _A : Union[str, Any] = [tokenizer.decode([x] ) for x in target_ids] self.assertEqual({el["""token_str"""] for el in outputs} , set(_a ) ) # Call argument _A : str = FillMaskPipeline(model=_a , tokenizer=_a ) _A : Optional[int] = fill_masker(F'''This is a {tokenizer.mask_token}''' , targets=_a ) self.assertEqual( _a , [ {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, ] , ) _A : int = {vocab[el] for el in targets} self.assertEqual({el["""token"""] for el in outputs} , _a ) _A : Any = [tokenizer.decode([x] ) for x in target_ids] self.assertEqual({el["""token_str"""] for el in outputs} , set(_a ) ) # Score equivalence _A : int = fill_masker(F'''This is a {tokenizer.mask_token}''' , targets=_a ) _A : Optional[int] = [top_mask["""token_str"""] for top_mask in outputs] _A : Union[str, Any] = [top_mask["""score"""] for top_mask in outputs] # For some BPE tokenizers, `</w>` is removed during decoding, so `token_str` won't be the same as in `targets`. if set(_a ) == set(_a ): _A : Tuple = fill_masker(F'''This is a {tokenizer.mask_token}''' , targets=_a ) _A : Union[str, Any] = [top_mask["""score"""] for top_mask in unmasked_targets] self.assertEqual(nested_simplify(_a ) , nested_simplify(_a ) ) # Raises with invalid with self.assertRaises(_a ): _A : str = fill_masker(F'''This is a {tokenizer.mask_token}''' , targets=[] ) # For some tokenizers, `""` is actually in the vocabulary and the expected error won't raised if "" not in tokenizer.get_vocab(): with self.assertRaises(_a ): _A : Optional[Any] = fill_masker(F'''This is a {tokenizer.mask_token}''' , targets=[""""""] ) with self.assertRaises(_a ): _A : int = fill_masker(F'''This is a {tokenizer.mask_token}''' , targets="""""" ) def a__ ( self , _a , _a ) -> Optional[Any]: _A : str = FillMaskPipeline(model=_a , tokenizer=_a , top_k=2 ) _A : str = fill_masker(F'''This is a {tokenizer.mask_token}''' ) self.assertEqual( _a , [ {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, ] , ) _A : Union[str, Any] = FillMaskPipeline(model=_a , tokenizer=_a ) _A : Union[str, Any] = fill_masker(F'''This is a {tokenizer.mask_token}''' , top_k=2 ) self.assertEqual( _a , [ {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, ] , ) self.assertEqual(nested_simplify(_a ) , nested_simplify(_a ) ) def a__ ( self , _a , _a ) -> List[Any]: _A : Union[str, Any] = tokenizer.get_vocab() _A : int = FillMaskPipeline(model=_a , tokenizer=_a ) # top_k=2, ntargets=3 _A : List[str] = sorted(vocab.keys() )[:3] _A : Tuple = fill_masker(F'''This is a {tokenizer.mask_token}''' , top_k=2 , targets=_a ) # If we use the most probably targets, and filter differently, we should still # have the same results _A : Any = [el["""token_str"""] for el in sorted(_a , key=lambda _a : x["score"] , reverse=_a )] # For some BPE tokenizers, `</w>` is removed during decoding, so `token_str` won't be the same as in `targets`. if set(_a ).issubset(_a ): _A : Any = fill_masker(F'''This is a {tokenizer.mask_token}''' , top_k=3 , targets=_a ) # They should yield exactly the same result self.assertEqual(nested_simplify(_a ) , nested_simplify(_a ) ) def a__ ( self , _a , _a ) -> str: _A : Optional[int] = FillMaskPipeline(model=_a , tokenizer=_a ) _A : List[Any] = tokenizer.get_vocab() # String duplicates + id duplicates _A : Optional[Any] = sorted(vocab.keys() )[:3] _A : Optional[Any] = [targets[0], targets[1], targets[0], targets[2], targets[1]] _A : Union[str, Any] = fill_masker(F'''My name is {tokenizer.mask_token}''' , targets=_a , top_k=10 ) # The target list contains duplicates, so we can't output more # than them self.assertEqual(len(_a ) , 3 ) def a__ ( self , _a , _a ) -> Tuple: _A : Any = FillMaskPipeline(model=_a , tokenizer=_a ) _A : Optional[Any] = fill_masker( F'''This is a {tokenizer.mask_token} {tokenizer.mask_token} {tokenizer.mask_token}''' , top_k=2 ) self.assertEqual( _a , [ [ {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, ], [ {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, ], [ {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, ], ] , )
54
0
from operator import delitem, getitem, setitem import pytest from data_structures.hashing.hash_map import HashMap def lowerCAmelCase_ ( snake_case_ ): return getitem, k def lowerCAmelCase_ ( snake_case_,snake_case_ ): return setitem, k, v def lowerCAmelCase_ ( snake_case_ ): return delitem, k def lowerCAmelCase_ ( snake_case_,snake_case_,*snake_case_ ): try: return fun(snake_case_,*snake_case_ ), None except Exception as e: return None, e _snake_case = ( _set("key_a", "val_a"), _set("key_b", "val_b"), ) _snake_case = [ _set("key_a", "val_a"), _set("key_a", "val_b"), ] _snake_case = [ _set("key_a", "val_a"), _set("key_b", "val_b"), _del("key_a"), _del("key_b"), _set("key_a", "val_a"), _del("key_a"), ] _snake_case = [ _get("key_a"), _del("key_a"), _set("key_a", "val_a"), _del("key_a"), _del("key_a"), _get("key_a"), ] _snake_case = [ *[_set(x, x) for x in range(5)], # guaranteed upsize ] _snake_case = [ *[_set(x, x) for x in range(5)], # guaranteed upsize *[_del(x) for x in range(5)], _set("key_a", "val_b"), ] @pytest.mark.parametrize( """operations""",( pytest.param(_add_items,id="""add items""" ), pytest.param(_overwrite_items,id="""overwrite items""" ), pytest.param(_delete_items,id="""delete items""" ), pytest.param(_access_absent_items,id="""access absent items""" ), pytest.param(_add_with_resize_up,id="""add with resize up""" ), pytest.param(_add_with_resize_down,id="""add with resize down""" ), ),) def lowerCAmelCase_ ( snake_case_ ): _A : Optional[Any] = HashMap(initial_block_size=4 ) _A : Optional[Any] = {} for _, (fun, *args) in enumerate(snake_case_ ): _A , _A : str = _run_operation(snake_case_,snake_case_,*snake_case_ ) _A , _A : Optional[Any] = _run_operation(snake_case_,snake_case_,*snake_case_ ) assert my_res == py_res assert str(snake_case_ ) == str(snake_case_ ) assert set(snake_case_ ) == set(snake_case_ ) assert len(snake_case_ ) == len(snake_case_ ) assert set(my.items() ) == set(py.items() ) def lowerCAmelCase_ ( ): def is_public(snake_case_ ) -> bool: return not name.startswith("""_""" ) _A : Union[str, Any] = {name for name in dir({} ) if is_public(snake_case_ )} _A : Union[str, Any] = {name for name in dir(HashMap() ) if is_public(snake_case_ )} assert dict_public_names > hash_public_names
702
import json import os import unittest from transformers import CLIPTokenizer, CLIPTokenizerFast from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES from transformers.testing_utils import require_ftfy, require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class lowercase ( UpperCamelCase__,unittest.TestCase ): _a = CLIPTokenizer _a = CLIPTokenizerFast _a = True _a = {} _a = False def a__ ( self ) -> Optional[Any]: super().setUp() # fmt: off _A : int = ["""l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """lo""", """l</w>""", """w</w>""", """r</w>""", """t</w>""", """low</w>""", """er</w>""", """lowest</w>""", """newer</w>""", """wider""", """<unk>""", """<|startoftext|>""", """<|endoftext|>"""] # fmt: on _A : str = dict(zip(_a , range(len(_a ) ) ) ) _A : Optional[int] = ["""#version: 0.2""", """l o""", """lo w</w>""", """e r</w>"""] _A : str = {"""unk_token""": """<unk>"""} _A : Union[str, Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""vocab_file"""] ) _A : Union[str, Any] = 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(_a ) + """\n""" ) with open(self.merges_file , """w""" , encoding="""utf-8""" ) as fp: fp.write("""\n""".join(_a ) ) def a__ ( self , **_a ) -> List[Any]: kwargs.update(self.special_tokens_map ) return CLIPTokenizer.from_pretrained(self.tmpdirname , **_a ) def a__ ( self , **_a ) -> Union[str, Any]: kwargs.update(self.special_tokens_map ) return CLIPTokenizerFast.from_pretrained(self.tmpdirname , **_a ) def a__ ( self , _a ) -> str: _A : Tuple = """lower newer""" _A : Optional[Any] = """lower newer""" return input_text, output_text def a__ ( self ) -> List[Any]: _A : Optional[int] = CLIPTokenizer(self.vocab_file , self.merges_file , **self.special_tokens_map ) _A : str = """lower newer""" _A : List[str] = ["""lo""", """w""", """er</w>""", """n""", """e""", """w""", """er</w>"""] _A : int = tokenizer.tokenize(_a ) self.assertListEqual(_a , _a ) _A : str = tokens + [tokenizer.unk_token] _A : Dict = [10, 2, 16, 9, 3, 2, 16, 20] self.assertListEqual(tokenizer.convert_tokens_to_ids(_a ) , _a ) @require_ftfy def a__ ( self ) -> Tuple: for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): _A : Dict = self.tokenizer_class.from_pretrained(_a , **_a ) _A : str = self.rust_tokenizer_class.from_pretrained(_a , **_a ) _A : List[str] = """A\n'll 11p223RF☆ho!!to?'d'd''d of a cat to-$''d.""" _A : Dict = tokenizer_s.tokenize(_a ) _A : Dict = tokenizer_r.tokenize(_a ) self.assertListEqual(_a , _a ) # Test that the tokenization is identical on an example containing a character (Latin Small Letter A # with Tilde) encoded in 2 different ways _A : Tuple = """xa\u0303y""" + """ """ + """x\xe3y""" _A : Dict = tokenizer_s.tokenize(_a ) _A : Dict = tokenizer_r.tokenize(_a ) self.assertListEqual(_a , _a ) # Test that the tokenization is identical on unicode of space type _A : Any = [ """\u0009""", # (horizontal tab, '\t') """\u000B""", # (vertical tab) """\u000C""", # (form feed) """\u0020""", # (space, ' ') """\u200E""", # (left-to-right mark):w """\u200F""", # (right-to-left mark) ] for unicode_seq in spaces_unicodes: _A : Optional[int] = tokenizer_s.tokenize(_a ) _A : List[Any] = tokenizer_r.tokenize(_a ) self.assertListEqual(_a , _a ) # Test that the tokenization is identical on unicode of line break type _A : int = [ """\u000A""", # (line feed, '\n') """\r\n""", # (carriage return and line feed, '\r\n') """\u000D""", # (carriage return, '\r') """\r""", # (carriage return, '\r') """\u000D""", # (carriage return, '\r') """\u2028""", # (line separator) """\u2029""", # (paragraph separator) # "\u0085", # (next line) ] # The tokenization is not identical for the character "\u0085" (next line). The slow version using ftfy transforms # it into the Horizontal Ellipsis character "…" ("\u2026") while the fast version transforms it into a # space (and thus into an empty list). for unicode_seq in line_break_unicodes: _A : Optional[Any] = tokenizer_s.tokenize(_a ) _A : Tuple = tokenizer_r.tokenize(_a ) self.assertListEqual(_a , _a ) def a__ ( self ) -> Any: # Test which aims to verify that the offsets are well adapted to the argument `add_prefix_space` for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): _A : List[Any] = """hello""" # `hello` is a token in the vocabulary of `pretrained_name` _A : str = F'''{text_of_1_token} {text_of_1_token}''' _A : str = self.rust_tokenizer_class.from_pretrained( _a , use_fast=_a , ) _A : Dict = tokenizer_r(_a , return_offsets_mapping=_a , add_special_tokens=_a ) self.assertEqual(encoding.offset_mapping[0] , (0, len(_a )) ) self.assertEqual( encoding.offset_mapping[1] , (len(_a ) + 1, len(_a ) + 1 + len(_a )) , ) _A : Any = F''' {text}''' _A : List[Any] = self.rust_tokenizer_class.from_pretrained( _a , use_fast=_a , ) _A : Optional[int] = tokenizer_r(_a , return_offsets_mapping=_a , add_special_tokens=_a ) self.assertEqual(encoding.offset_mapping[0] , (1, 1 + len(_a )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(_a ) + 1, 1 + len(_a ) + 1 + len(_a )) , ) def a__ ( self ) -> List[Any]: # Test related to the breaking change introduced in transformers v4.17.0 # We need to check that an error in raised when the user try to load a previous version of the tokenizer. with self.assertRaises(_a ) as context: self.rust_tokenizer_class.from_pretrained("""robot-test/old-clip-tokenizer""" ) self.assertTrue( context.exception.args[0].startswith( """The `backend_tokenizer` provided does not match the expected format.""" ) ) @require_ftfy def a__ ( self ) -> str: super().test_tokenization_python_rust_equals() def a__ ( self ) -> Union[str, Any]: # CLIP always lower cases letters pass
54
0
from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional, Union from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging if TYPE_CHECKING: from ... import FeatureExtractionMixin, PreTrainedTokenizerBase, TensorType _snake_case = logging.get_logger(__name__) _snake_case = { """microsoft/deberta-v2-xlarge""": """https://huggingface.co/microsoft/deberta-v2-xlarge/resolve/main/config.json""", """microsoft/deberta-v2-xxlarge""": """https://huggingface.co/microsoft/deberta-v2-xxlarge/resolve/main/config.json""", """microsoft/deberta-v2-xlarge-mnli""": ( """https://huggingface.co/microsoft/deberta-v2-xlarge-mnli/resolve/main/config.json""" ), """microsoft/deberta-v2-xxlarge-mnli""": ( """https://huggingface.co/microsoft/deberta-v2-xxlarge-mnli/resolve/main/config.json""" ), } class lowercase ( UpperCamelCase_ ): _a = "deberta-v2" def __init__( self , _a=12_8100 , _a=1536 , _a=24 , _a=24 , _a=6144 , _a="gelu" , _a=0.1 , _a=0.1 , _a=512 , _a=0 , _a=0.02 , _a=1e-7 , _a=False , _a=-1 , _a=0 , _a=True , _a=None , _a=0 , _a="gelu" , **_a , ) -> List[str]: super().__init__(**__A ) _A : Optional[int] = hidden_size _A : Union[str, Any] = num_hidden_layers _A : Optional[int] = num_attention_heads _A : Tuple = intermediate_size _A : Union[str, Any] = hidden_act _A : Any = hidden_dropout_prob _A : Tuple = attention_probs_dropout_prob _A : Tuple = max_position_embeddings _A : List[Any] = type_vocab_size _A : int = initializer_range _A : List[Any] = relative_attention _A : Dict = max_relative_positions _A : Any = pad_token_id _A : Any = position_biased_input # Backwards compatibility if type(__A ) == str: _A : List[Any] = [x.strip() for x in pos_att_type.lower().split("""|""" )] _A : List[str] = pos_att_type _A : Tuple = vocab_size _A : List[str] = layer_norm_eps _A : List[str] = kwargs.get("""pooler_hidden_size""" , __A ) _A : Tuple = pooler_dropout _A : str = pooler_hidden_act class lowercase ( UpperCamelCase_ ): @property def a__ ( self ) -> Optional[Any]: if self.task == "multiple-choice": _A : int = {0: "batch", 1: "choice", 2: "sequence"} else: _A : List[Any] = {0: "batch", 1: "sequence"} if self._config.type_vocab_size > 0: return OrderedDict( [("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ("""token_type_ids""", dynamic_axis)] ) else: return OrderedDict([("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis)] ) @property def a__ ( self ) -> List[Any]: return 12 def a__ ( self , _a , _a = -1 , _a = -1 , _a = -1 , _a = False , _a = None , _a = 3 , _a = 40 , _a = 40 , _a = None , ) -> str: _A : int = super().generate_dummy_inputs(preprocessor=__A , framework=__A ) if self._config.type_vocab_size == 0 and "token_type_ids" in dummy_inputs: del dummy_inputs["token_type_ids"] return dummy_inputs
703
from datetime import datetime as dt import os from github import Github _snake_case = [ "good first issue", "good second issue", "good difficult issue", "feature request", "new model", "wip", ] def lowerCAmelCase_ ( ): _A : int = Github(os.environ["""GITHUB_TOKEN"""] ) _A : Tuple = g.get_repo("""huggingface/transformers""" ) _A : Dict = repo.get_issues(state="""open""" ) for issue in open_issues: _A : Optional[Any] = sorted([comment for comment in issue.get_comments()],key=lambda snake_case_ : i.created_at,reverse=snake_case_ ) _A : Dict = comments[0] if len(snake_case_ ) > 0 else None if ( last_comment is not None and last_comment.user.login == "github-actions[bot]" and (dt.utcnow() - issue.updated_at).days > 7 and (dt.utcnow() - issue.created_at).days >= 30 and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels() ) ): # print(f"Would close issue {issue.number} since it has been 7 days of inactivity since bot mention.") issue.edit(state="""closed""" ) elif ( (dt.utcnow() - issue.updated_at).days > 23 and (dt.utcnow() - issue.created_at).days >= 30 and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels() ) ): # print(f"Would add stale comment to {issue.number}") issue.create_comment( """This issue has been automatically marked as stale because it has not had """ """recent activity. If you think this still needs to be addressed """ """please comment on this thread.\n\nPlease note that issues that do not follow the """ """[contributing guidelines](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) """ """are likely to be ignored.""" ) if __name__ == "__main__": main()
54
0
def lowerCAmelCase_ ( snake_case_ = 4000000 ): _A : Any = [0, 1] _A : List[Any] = 0 while fib[i] <= n: fib.append(fib[i] + fib[i + 1] ) if fib[i + 2] > n: break i += 1 _A : Optional[Any] = 0 for j in range(len(_lowerCamelCase ) - 1 ): if fib[j] % 2 == 0: total += fib[j] return total if __name__ == "__main__": print(f"""{solution() = }""")
704
from __future__ import annotations class lowercase : def __init__( self , _a = 0 ) -> str: _A : Any = key def a__ ( self , _a , _a ) -> list[str]: assert isinstance(_a , _a ) and isinstance(_a , _a ) _A : Any = key or self.__key or 1 # make sure key is an appropriate size key %= 255 return [chr(ord(_a ) ^ key ) for ch in content] def a__ ( self , _a , _a ) -> list[str]: assert isinstance(_a , _a ) and isinstance(_a , _a ) _A : List[Any] = key or self.__key or 1 # make sure key is an appropriate size key %= 255 return [chr(ord(_a ) ^ key ) for ch in content] def a__ ( self , _a , _a = 0 ) -> str: assert isinstance(_a , _a ) and isinstance(_a , _a ) _A : List[Any] = key or self.__key or 1 # make sure key can be any size while key > 255: key -= 255 # This will be returned _A : List[str] = """""" for ch in content: ans += chr(ord(_a ) ^ key ) return ans def a__ ( self , _a , _a = 0 ) -> str: assert isinstance(_a , _a ) and isinstance(_a , _a ) _A : List[str] = key or self.__key or 1 # make sure key can be any size while key > 255: key -= 255 # This will be returned _A : List[str] = """""" for ch in content: ans += chr(ord(_a ) ^ key ) return ans def a__ ( self , _a , _a = 0 ) -> bool: assert isinstance(_a , _a ) and isinstance(_a , _a ) try: with open(_a ) as fin, open("""encrypt.out""" , """w+""" ) as fout: # actual encrypt-process for line in fin: fout.write(self.encrypt_string(_a , _a ) ) except OSError: return False return True def a__ ( self , _a , _a ) -> bool: assert isinstance(_a , _a ) and isinstance(_a , _a ) try: with open(_a ) as fin, open("""decrypt.out""" , """w+""" ) as fout: # actual encrypt-process for line in fin: fout.write(self.decrypt_string(_a , _a ) ) except OSError: return False return True # Tests # crypt = XORCipher() # key = 67 # # test encrypt # print(crypt.encrypt("hallo welt",key)) # # test decrypt # print(crypt.decrypt(crypt.encrypt("hallo welt",key), key)) # # test encrypt_string # print(crypt.encrypt_string("hallo welt",key)) # # test decrypt_string # print(crypt.decrypt_string(crypt.encrypt_string("hallo welt",key),key)) # if (crypt.encrypt_file("test.txt",key)): # print("encrypt successful") # else: # print("encrypt unsuccessful") # if (crypt.decrypt_file("encrypt.out",key)): # print("decrypt successful") # else: # print("decrypt unsuccessful")
54
0
from __future__ import annotations import math def lowerCAmelCase_ ( snake_case_ ): if 1 < number < 4: # 2 and 3 are primes return True elif number < 2 or number % 2 == 0 or number % 3 == 0: # Negatives, 0, 1, all even numbers, all multiples of 3 are not primes return False # All primes number are in format of 6k +/- 1 for i in range(5,int(math.sqrt(snake_case_ ) + 1 ),6 ): if number % i == 0 or number % (i + 2) == 0: return False return True def lowerCAmelCase_ ( snake_case_ ): _A : Optional[int] = str(snake_case_ ) _A : Optional[int] = [n] for i in range(1,len(snake_case_ ) ): list_nums.append(int(str_num[i:] ) ) list_nums.append(int(str_num[:-i] ) ) return list_nums def lowerCAmelCase_ ( snake_case_ ): if len(str(snake_case_ ) ) > 3: if not is_prime(int(str(snake_case_ )[-3:] ) ) or not is_prime(int(str(snake_case_ )[:3] ) ): return False return True def lowerCAmelCase_ ( snake_case_ = 11 ): _A : list[int] = [] _A : Union[str, Any] = 13 while len(snake_case_ ) != count: if validate(snake_case_ ): _A : Dict = list_truncated_nums(snake_case_ ) if all(is_prime(snake_case_ ) for i in list_nums ): list_truncated_primes.append(snake_case_ ) num += 2 return list_truncated_primes def lowerCAmelCase_ ( ): return sum(compute_truncated_primes(11 ) ) if __name__ == "__main__": print(f"""{sum(compute_truncated_primes(11)) = }""")
705
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 = random.Random() def lowerCAmelCase_ ( snake_case_,snake_case_=1.0,snake_case_=None,snake_case_=None ): if rng is None: _A : str = global_rng _A : List[Any] = [] 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 lowercase ( unittest.TestCase ): def __init__( self , _a , _a=7 , _a=400 , _a=2000 , _a=10 , _a=160 , _a=8 , _a=0.0 , _a=4000 , _a=False , _a=True , ) -> Optional[int]: _A : Any = parent _A : List[Any] = batch_size _A : List[Any] = min_seq_length _A : Dict = max_seq_length _A : Optional[Any] = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) _A : Tuple = padding_value _A : Tuple = sampling_rate _A : str = return_attention_mask _A : Any = do_normalize _A : Union[str, Any] = feature_size _A : List[Any] = chunk_length _A : List[Any] = hop_length def a__ ( self ) -> List[str]: 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 a__ ( self , _a=False , _a=False ) -> List[str]: def _flatten(_a ): return list(itertools.chain(*_a ) ) 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 : int = [ 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 : Any = [np.asarray(_a ) for x in speech_inputs] return speech_inputs @require_torch @require_torchaudio class lowercase ( UpperCamelCase__,unittest.TestCase ): _a = WhisperFeatureExtractor if is_speech_available() else None def a__ ( self ) -> Tuple: _A : Optional[int] = WhisperFeatureExtractionTester(self ) def a__ ( self ) -> Optional[Any]: _A : List[Any] = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: _A : List[str] = feat_extract_first.save_pretrained(_a )[0] check_json_file_has_correct_format(_a ) _A : Optional[int] = self.feature_extraction_class.from_pretrained(_a ) _A : Tuple = feat_extract_first.to_dict() _A : List[Any] = feat_extract_second.to_dict() _A : List[Any] = feat_extract_first.mel_filters _A : Union[str, Any] = feat_extract_second.mel_filters self.assertTrue(np.allclose(_a , _a ) ) self.assertEqual(_a , _a ) def a__ ( self ) -> Dict: _A : Any = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: _A : Dict = os.path.join(_a , """feat_extract.json""" ) feat_extract_first.to_json_file(_a ) _A : Optional[int] = self.feature_extraction_class.from_json_file(_a ) _A : str = feat_extract_first.to_dict() _A : Any = feat_extract_second.to_dict() _A : Union[str, Any] = feat_extract_first.mel_filters _A : Union[str, Any] = feat_extract_second.mel_filters self.assertTrue(np.allclose(_a , _a ) ) self.assertEqual(_a , _a ) def a__ ( self ) -> Union[str, Any]: # Tests that all call wrap to encode_plus and batch_encode_plus _A : Optional[int] = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 _A : Optional[Any] = [floats_list((1, x) )[0] for x in range(800 , 1400 , 200 )] _A : Any = [np.asarray(_a ) for speech_input in speech_inputs] # Test feature size _A : Dict = feature_extractor(_a , 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 : List[Any] = feature_extractor(speech_inputs[0] , return_tensors="""np""" ).input_features _A : List[str] = feature_extractor(np_speech_inputs[0] , return_tensors="""np""" ).input_features self.assertTrue(np.allclose(_a , _a , atol=1e-3 ) ) # Test batched _A : Union[str, Any] = feature_extractor(_a , return_tensors="""np""" ).input_features _A : Tuple = feature_extractor(_a , return_tensors="""np""" ).input_features for enc_seq_a, enc_seq_a in zip(_a , _a ): self.assertTrue(np.allclose(_a , _a , atol=1e-3 ) ) # Test 2-D numpy arrays are batched. _A : List[str] = [floats_list((1, x) )[0] for x in (800, 800, 800)] _A : Any = np.asarray(_a ) _A : Union[str, Any] = feature_extractor(_a , return_tensors="""np""" ).input_features _A : int = feature_extractor(_a , return_tensors="""np""" ).input_features for enc_seq_a, enc_seq_a in zip(_a , _a ): self.assertTrue(np.allclose(_a , _a , atol=1e-3 ) ) # Test truncation required _A : List[Any] = [floats_list((1, x) )[0] for x in range(200 , (feature_extractor.n_samples + 500) , 200 )] _A : Union[str, Any] = [np.asarray(_a ) for speech_input in speech_inputs] _A : Tuple = [x[: feature_extractor.n_samples] for x in speech_inputs] _A : Union[str, Any] = [np.asarray(_a ) for speech_input in speech_inputs_truncated] _A : Optional[int] = feature_extractor(_a , return_tensors="""np""" ).input_features _A : List[Any] = feature_extractor(_a , return_tensors="""np""" ).input_features for enc_seq_a, enc_seq_a in zip(_a , _a ): self.assertTrue(np.allclose(_a , _a , atol=1e-3 ) ) def a__ ( self ) -> Dict: import torch _A : Dict = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) _A : Optional[int] = np.random.rand(100 , 32 ).astype(np.floataa ) _A : str = np_speech_inputs.tolist() for inputs in [py_speech_inputs, np_speech_inputs]: _A : Optional[Any] = feature_extractor.pad([{"""input_features""": inputs}] , return_tensors="""np""" ) self.assertTrue(np_processed.input_features.dtype == np.floataa ) _A : Optional[int] = feature_extractor.pad([{"""input_features""": inputs}] , return_tensors="""pt""" ) self.assertTrue(pt_processed.input_features.dtype == torch.floataa ) def a__ ( self , _a ) -> Dict: _A : Optional[Any] = load_dataset("""hf-internal-testing/librispeech_asr_dummy""" , """clean""" , split="""validation""" ) # automatic decoding with librispeech _A : Optional[Any] = ds.sort("""id""" ).select(range(_a ) )[:num_samples]["""audio"""] return [x["array"] for x in speech_samples] def a__ ( self ) -> Tuple: # fmt: off _A : Dict = torch.tensor( [ 0.1193, -0.0946, -0.1098, -0.0196, 0.0225, -0.0690, -0.1736, 0.0951, 0.0971, -0.0817, -0.0702, 0.0162, 0.0260, 0.0017, -0.0192, -0.1678, 0.0709, -0.1867, -0.0655, -0.0274, -0.0234, -0.1884, -0.0516, -0.0554, -0.0274, -0.1425, -0.1423, 0.0837, 0.0377, -0.0854 ] ) # fmt: on _A : Dict = self._load_datasamples(1 ) _A : Optional[Any] = WhisperFeatureExtractor() _A : Optional[Any] = feature_extractor(_a , return_tensors="""pt""" ).input_features self.assertEqual(input_features.shape , (1, 80, 3000) ) self.assertTrue(torch.allclose(input_features[0, 0, :30] , _a , atol=1e-4 ) ) def a__ ( self ) -> str: _A : Dict = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) _A : str = self._load_datasamples(1 )[0] _A : Union[str, Any] = ((audio - audio.min()) / (audio.max() - audio.min())) * 6_5535 # Rescale to [0, 65535] to show issue _A : List[Any] = feat_extract.zero_mean_unit_var_norm([audio] , attention_mask=_a )[0] self.assertTrue(np.all(np.mean(_a ) < 1e-3 ) ) self.assertTrue(np.all(np.abs(np.var(_a ) - 1 ) < 1e-3 ) )
54
0
from typing import List, Optional, Tuple, Union import torch from torch import nn from torch.nn import CrossEntropyLoss from ... import AutoBackbone from ...modeling_outputs import SemanticSegmenterOutput from ...modeling_utils import PreTrainedModel from ...utils import add_start_docstrings, add_start_docstrings_to_model_forward, replace_return_docstrings from ...utils.backbone_utils import BackboneMixin from .configuration_upernet import UperNetConfig _snake_case = [ "openmmlab/upernet-convnext-tiny", # See all UperNet models at https://huggingface.co/models?filter=upernet ] # General docstring _snake_case = "UperNetConfig" class lowercase ( nn.Module ): def __init__( self , _a , _a , _a , _a = 0 , _a = False , _a = 1 , ) -> Union[str, Any]: super().__init__() _A : Union[str, Any] = nn.Convad( in_channels=a_ , out_channels=a_ , kernel_size=a_ , padding=a_ , bias=a_ , dilation=a_ , ) _A : Optional[int] = nn.BatchNormad(a_ ) _A : Tuple = nn.ReLU() def a__ ( self , _a ) -> str: _A : Union[str, Any] = self.conv(a_ ) _A : Optional[int] = self.batch_norm(a_ ) _A : Dict = self.activation(a_ ) return output class lowercase ( nn.Module ): def __init__( self , _a , _a , _a ) -> List[str]: super().__init__() _A : List[str] = [ nn.AdaptiveAvgPoolad(a_ ), UperNetConvModule(a_ , a_ , kernel_size=1 ), ] for i, layer in enumerate(self.layers ): self.add_module(str(a_ ) , a_ ) def a__ ( self , _a ) -> int: _A : List[Any] = input for layer in self.layers: _A : str = layer(a_ ) return hidden_state class lowercase ( nn.Module ): def __init__( self , _a , _a , _a , _a ) -> Union[str, Any]: super().__init__() _A : List[str] = pool_scales _A : Optional[Any] = align_corners _A : List[Any] = in_channels _A : Dict = channels _A : Optional[int] = [] for i, pool_scale in enumerate(a_ ): _A : Optional[int] = UperNetPyramidPoolingBlock(pool_scale=a_ , in_channels=a_ , channels=a_ ) self.blocks.append(a_ ) self.add_module(str(a_ ) , a_ ) def a__ ( self , _a ) -> List[Any]: _A : str = [] for ppm in self.blocks: _A : Any = ppm(a_ ) _A : Dict = nn.functional.interpolate( a_ , size=x.size()[2:] , mode="""bilinear""" , align_corners=self.align_corners ) ppm_outs.append(a_ ) return ppm_outs class lowercase ( nn.Module ): def __init__( self , _a , _a ) -> Union[str, Any]: super().__init__() _A : Tuple = config _A : Tuple = config.pool_scales # e.g. (1, 2, 3, 6) _A : str = in_channels _A : Dict = config.hidden_size _A : Any = False _A : List[Any] = nn.Convad(self.channels , config.num_labels , kernel_size=1 ) # PSP Module _A : List[Any] = UperNetPyramidPoolingModule( self.pool_scales , self.in_channels[-1] , self.channels , align_corners=self.align_corners , ) _A : int = UperNetConvModule( self.in_channels[-1] + len(self.pool_scales ) * self.channels , self.channels , kernel_size=3 , padding=1 , ) # FPN Module _A : str = nn.ModuleList() _A : Tuple = nn.ModuleList() for in_channels in self.in_channels[:-1]: # skip the top layer _A : List[Any] = UperNetConvModule(a_ , self.channels , kernel_size=1 ) _A : Optional[Any] = UperNetConvModule(self.channels , self.channels , kernel_size=3 , padding=1 ) self.lateral_convs.append(a_ ) self.fpn_convs.append(a_ ) _A : str = UperNetConvModule( len(self.in_channels ) * self.channels , self.channels , kernel_size=3 , padding=1 , ) def a__ ( self ) -> Optional[Any]: self.apply(self._init_weights ) def a__ ( self , _a ) -> Tuple: if isinstance(a_ , nn.Convad ): module.weight.data.normal_(mean=0.0 , std=self.config.initializer_range ) if module.bias is not None: module.bias.data.zero_() def a__ ( self , _a ) -> List[Any]: _A : Optional[int] = inputs[-1] _A : Tuple = [x] psp_outs.extend(self.psp_modules(a_ ) ) _A : Tuple = torch.cat(a_ , dim=1 ) _A : Optional[Any] = self.bottleneck(a_ ) return output def a__ ( self , _a ) -> int: _A : int = [lateral_conv(encoder_hidden_states[i] ) for i, lateral_conv in enumerate(self.lateral_convs )] laterals.append(self.psp_forward(a_ ) ) # build top-down path _A : List[str] = len(a_ ) for i in range(used_backbone_levels - 1 , 0 , -1 ): _A : Dict = laterals[i - 1].shape[2:] _A : Tuple = laterals[i - 1] + nn.functional.interpolate( laterals[i] , size=a_ , mode="""bilinear""" , align_corners=self.align_corners ) # build outputs _A : Optional[int] = [self.fpn_convs[i](laterals[i] ) for i in range(used_backbone_levels - 1 )] # append psp feature fpn_outs.append(laterals[-1] ) for i in range(used_backbone_levels - 1 , 0 , -1 ): _A : int = nn.functional.interpolate( fpn_outs[i] , size=fpn_outs[0].shape[2:] , mode="""bilinear""" , align_corners=self.align_corners ) _A : Optional[Any] = torch.cat(a_ , dim=1 ) _A : Any = self.fpn_bottleneck(a_ ) _A : Dict = self.classifier(a_ ) return output class lowercase ( nn.Module ): def __init__( self , _a , _a = 2 , _a = 3 , _a = 1 ) -> Optional[int]: super().__init__() _A : List[Any] = config _A : Dict = config.auxiliary_in_channels _A : Dict = config.auxiliary_channels _A : int = config.auxiliary_num_convs _A : Any = config.auxiliary_concat_input _A : Union[str, Any] = in_index _A : int = (kernel_size // 2) * dilation _A : Tuple = [] convs.append( UperNetConvModule( self.in_channels , self.channels , kernel_size=a_ , padding=a_ , dilation=a_ ) ) for i in range(self.num_convs - 1 ): convs.append( UperNetConvModule( self.channels , self.channels , kernel_size=a_ , padding=a_ , dilation=a_ ) ) if self.num_convs == 0: _A : str = nn.Identity() else: _A : List[Any] = nn.Sequential(*a_ ) if self.concat_input: _A : Dict = UperNetConvModule( self.in_channels + self.channels , self.channels , kernel_size=a_ , padding=kernel_size // 2 ) _A : Optional[Any] = nn.Convad(self.channels , config.num_labels , kernel_size=1 ) def a__ ( self ) -> List[str]: self.apply(self._init_weights ) def a__ ( self , _a ) -> Optional[int]: if isinstance(a_ , nn.Convad ): module.weight.data.normal_(mean=0.0 , std=self.config.initializer_range ) if module.bias is not None: module.bias.data.zero_() def a__ ( self , _a ) -> Optional[int]: _A : Any = encoder_hidden_states[self.in_index] _A : Optional[Any] = self.convs(a_ ) if self.concat_input: _A : Any = self.conv_cat(torch.cat([hidden_states, output] , dim=1 ) ) _A : str = self.classifier(a_ ) return output class lowercase ( __a ): _a = UperNetConfig _a = "pixel_values" _a = True def a__ ( self , _a ) -> int: if isinstance(a_ , a_ ): module.backbone.init_weights() module.decode_head.init_weights() module.auxiliary_head.init_weights() def a__ ( self ) -> Union[str, Any]: self.backbone.init_weights() self.decode_head.init_weights() self.auxiliary_head.init_weights() def a__ ( self , _a , _a=False ) -> str: if isinstance(a_ , a_ ): _A : Union[str, Any] = value _snake_case = r"\n Parameters:\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 config ([`UperNetConfig`]): 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" _snake_case = r"\n Args:\n pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):\n Pixel values. Padding will be ignored by default should you provide it. Pixel values can be obtained using\n [`AutoImageProcessor`]. See [`SegformerImageProcessor.__call__`] for details.\n output_attentions (`bool`, *optional*):\n Whether or not to return the attentions tensors of all attention layers in case the backbone has them. See\n `attentions` under returned tensors for more detail.\n output_hidden_states (`bool`, *optional*):\n Whether or not to return the hidden states of all layers of the backbone. See `hidden_states` under\n returned tensors for more detail.\n return_dict (`bool`, *optional*):\n Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.\n" @add_start_docstrings( "UperNet framework leveraging any vision backbone e.g. for ADE20k, CityScapes.",__a,) class lowercase ( __a ): def __init__( self , _a ) -> Union[str, Any]: super().__init__(a_ ) _A : Tuple = AutoBackbone.from_config(config.backbone_config ) # Semantic segmentation head(s) _A : int = UperNetHead(a_ , in_channels=self.backbone.channels ) _A : Tuple = UperNetFCNHead(a_ ) if config.use_auxiliary_head else None # Initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(UPERNET_INPUTS_DOCSTRING.format("""batch_size, sequence_length""" ) ) @replace_return_docstrings(output_type=a_ , config_class=_CONFIG_FOR_DOC ) def a__ ( self , _a = None , _a = None , _a = None , _a = None , _a = None , ) -> Dict: _A : Dict = return_dict if return_dict is not None else self.config.use_return_dict _A : Optional[int] = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) _A : Tuple = output_attentions if output_attentions is not None else self.config.output_attentions _A : str = self.backbone.forward_with_filtered_kwargs( a_ , output_hidden_states=a_ , output_attentions=a_ ) _A : Dict = outputs.feature_maps _A : Optional[Any] = self.decode_head(a_ ) _A : int = nn.functional.interpolate(a_ , size=pixel_values.shape[2:] , mode="""bilinear""" , align_corners=a_ ) _A : Dict = None if self.auxiliary_head is not None: _A : Optional[Any] = self.auxiliary_head(a_ ) _A : List[str] = nn.functional.interpolate( a_ , size=pixel_values.shape[2:] , mode="""bilinear""" , align_corners=a_ ) _A : Any = None if labels is not None: if self.config.num_labels == 1: raise ValueError("""The number of labels should be greater than one""" ) else: # compute weighted loss _A : str = CrossEntropyLoss(ignore_index=self.config.loss_ignore_index ) _A : Any = loss_fct(a_ , a_ ) _A : List[str] = loss_fct(a_ , a_ ) _A : Optional[Any] = main_loss + self.config.auxiliary_loss_weight * auxiliary_loss if not return_dict: if output_hidden_states: _A : List[str] = (logits,) + outputs[1:] else: _A : str = (logits,) + outputs[2:] return ((loss,) + output) if loss is not None else output return SemanticSegmenterOutput( loss=a_ , logits=a_ , hidden_states=outputs.hidden_states , attentions=outputs.attentions , )
706
def lowerCAmelCase_ ( snake_case_,snake_case_ ): _A : Union[str, Any] = """""" for i in table: res += inp[i - 1] return res def lowerCAmelCase_ ( snake_case_ ): return data[1:] + data[0] def lowerCAmelCase_ ( snake_case_,snake_case_ ): _A : Dict = """""" for i in range(len(snake_case_ ) ): if a[i] == b[i]: res += "0" else: res += "1" return res def lowerCAmelCase_ ( snake_case_,snake_case_ ): _A : int = int("""0b""" + data[0] + data[-1],2 ) _A : Any = int("""0b""" + data[1:3],2 ) return bin(s[row][col] )[2:] def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_,snake_case_,snake_case_ ): _A : List[str] = message[:4] _A : List[Any] = message[4:] _A : Union[str, Any] = apply_table(snake_case_,snake_case_ ) _A : List[Any] = xor(snake_case_,snake_case_ ) _A : Optional[Any] = apply_sbox(snake_case_,temp[:4] ) # noqa: E741 _A : List[Any] = apply_sbox(snake_case_,temp[4:] ) _A : int = """0""" * (2 - len(snake_case_ )) + l # noqa: E741 _A : Union[str, Any] = """0""" * (2 - len(snake_case_ )) + r _A : List[Any] = apply_table(l + r,snake_case_ ) _A : Any = xor(snake_case_,snake_case_ ) return temp + right if __name__ == "__main__": _snake_case = input("Enter 10 bit key: ") _snake_case = input("Enter 8 bit message: ") _snake_case = [6, 3, 7, 4, 8, 5, 10, 9] _snake_case = [3, 5, 2, 7, 4, 10, 1, 9, 8, 6] _snake_case = [2, 4, 3, 1] _snake_case = [2, 6, 3, 1, 4, 8, 5, 7] _snake_case = [4, 1, 3, 5, 7, 2, 8, 6] _snake_case = [4, 1, 2, 3, 2, 3, 4, 1] _snake_case = [[1, 0, 3, 2], [3, 2, 1, 0], [0, 2, 1, 3], [3, 1, 3, 2]] _snake_case = [[0, 1, 2, 3], [2, 0, 1, 3], [3, 0, 1, 0], [2, 1, 0, 3]] # key generation _snake_case = apply_table(key, paa_table) _snake_case = temp[:5] _snake_case = temp[5:] _snake_case = left_shift(left) _snake_case = left_shift(right) _snake_case = apply_table(left + right, pa_table) _snake_case = left_shift(left) _snake_case = left_shift(right) _snake_case = left_shift(left) _snake_case = left_shift(right) _snake_case = apply_table(left + right, pa_table) # encryption _snake_case = apply_table(message, IP) _snake_case = function(expansion, sa, sa, keya, temp) _snake_case = temp[4:] + temp[:4] _snake_case = function(expansion, sa, sa, keya, temp) _snake_case = apply_table(temp, IP_inv) print("Cipher text is:", CT) # decryption _snake_case = apply_table(CT, IP) _snake_case = function(expansion, sa, sa, keya, temp) _snake_case = temp[4:] + temp[:4] _snake_case = function(expansion, sa, sa, keya, temp) _snake_case = apply_table(temp, IP_inv) print("Plain text after decypting is:", PT)
54
0
def lowerCAmelCase_ ( snake_case_ ): stooge(__snake_case,0,len(__snake_case ) - 1 ) return arr def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_ ): if i >= h: return # If first element is smaller than the last then swap them if arr[i] > arr[h]: _A , _A : Optional[int] = arr[h], arr[i] # If there are more than 2 elements in the array if h - i + 1 > 2: _A : int = (int)((h - i + 1) / 3 ) # Recursively sort first 2/3 elements stooge(__snake_case,__snake_case,(h - t) ) # Recursively sort last 2/3 elements stooge(__snake_case,i + t,(__snake_case) ) # Recursively sort first 2/3 elements stooge(__snake_case,__snake_case,(h - t) ) if __name__ == "__main__": _snake_case = input("Enter numbers separated by a comma:\n").strip() _snake_case = [int(item) for item in user_input.split(",")] print(stooge_sort(unsorted))
707
import unittest import numpy as np from transformers.file_utils import is_torch_available, is_vision_available from transformers.testing_utils import require_torch, require_vision 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 DPTImageProcessor class lowercase ( unittest.TestCase ): def __init__( self , _a , _a=7 , _a=3 , _a=18 , _a=30 , _a=400 , _a=True , _a=None , _a=True , _a=[0.5, 0.5, 0.5] , _a=[0.5, 0.5, 0.5] , ) -> Tuple: _A : Any = size if size is not None else {"""height""": 18, """width""": 18} _A : Optional[Any] = parent _A : Union[str, Any] = batch_size _A : List[Any] = num_channels _A : List[str] = image_size _A : Optional[Any] = min_resolution _A : List[Any] = max_resolution _A : Optional[Any] = do_resize _A : str = size _A : List[str] = do_normalize _A : Dict = image_mean _A : int = image_std def a__ ( self ) -> Any: 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 ( UpperCamelCase__,unittest.TestCase ): _a = DPTImageProcessor if is_vision_available() else None def a__ ( self ) -> Optional[int]: _A : Optional[Any] = DPTImageProcessingTester(self ) @property def a__ ( self ) -> Tuple: return self.image_processor_tester.prepare_image_processor_dict() def a__ ( self ) -> Any: _A : List[str] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_a , """image_mean""" ) ) self.assertTrue(hasattr(_a , """image_std""" ) ) self.assertTrue(hasattr(_a , """do_normalize""" ) ) self.assertTrue(hasattr(_a , """do_resize""" ) ) self.assertTrue(hasattr(_a , """size""" ) ) def a__ ( self ) -> Any: _A : List[str] = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"""height""": 18, """width""": 18} ) _A : str = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {"""height""": 42, """width""": 42} ) def a__ ( self ) -> List[Any]: # Initialize image_processing _A : Optional[int] = self.image_processing_class(**self.image_processor_dict ) # create random PIL images _A : Any = prepare_image_inputs(self.image_processor_tester , equal_resolution=_a ) for image in image_inputs: self.assertIsInstance(_a , Image.Image ) # Test not batched input _A : Union[str, 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 : int = image_processing(_a , 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 a__ ( self ) -> Union[str, Any]: # Initialize image_processing _A : List[Any] = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors _A : Optional[Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=_a , numpify=_a ) for image in image_inputs: self.assertIsInstance(_a , np.ndarray ) # Test not batched input _A : Dict = 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(_a , 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 a__ ( self ) -> List[str]: # Initialize image_processing _A : Dict = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors _A : str = prepare_image_inputs(self.image_processor_tester , equal_resolution=_a , torchify=_a ) for image in image_inputs: self.assertIsInstance(_a , torch.Tensor ) # Test not batched input _A : Optional[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 : int = image_processing(_a , 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"""], ) , )
54
0
from diffusers.utils.testing_utils import require_onnxruntime @require_onnxruntime class lowercase : pass
708
from typing import Any, Dict, List, Optional, Tuple, Union import torch from torch import nn from torch.utils.data import DistributedSampler, RandomSampler from transformers import PreTrainedModel, Trainer, logging from transformers.integrations import is_fairscale_available from transformers.models.fsmt.configuration_fsmt import FSMTConfig from transformers.optimization import ( Adafactor, AdamW, get_constant_schedule, get_constant_schedule_with_warmup, get_cosine_schedule_with_warmup, get_cosine_with_hard_restarts_schedule_with_warmup, get_linear_schedule_with_warmup, get_polynomial_decay_schedule_with_warmup, ) from transformers.trainer_pt_utils import get_tpu_sampler from transformers.training_args import ParallelMode from transformers.utils import is_torch_tpu_available if is_fairscale_available(): from fairscale.optim import OSS _snake_case = logging.get_logger(__name__) _snake_case = { "linear": get_linear_schedule_with_warmup, "cosine": get_cosine_schedule_with_warmup, "cosine_w_restarts": get_cosine_with_hard_restarts_schedule_with_warmup, "polynomial": get_polynomial_decay_schedule_with_warmup, "constant": get_constant_schedule, "constant_w_warmup": get_constant_schedule_with_warmup, } class lowercase ( UpperCamelCase__ ): def __init__( self , _a=None , _a=None , *_a , **_a ) -> Optional[int]: super().__init__(*_a , **_a ) if config is None: assert isinstance(self.model , _a ), ( "If no `config` is passed the model to be trained has to be of type `PreTrainedModel`, but is" F''' {self.model.__class__}''' ) _A : Optional[Any] = self.model.config else: _A : int = config _A : Optional[Any] = data_args _A : int = self.config.tgt_vocab_size if isinstance(self.config , _a ) else self.config.vocab_size if self.args.label_smoothing != 0 or (self.data_args is not None and self.data_args.ignore_pad_token_for_loss): assert self.config.pad_token_id is not None, ( "Make sure that `config.pad_token_id` is correcly defined when ignoring `pad_token` for loss" " calculation or doing label smoothing." ) if self.config.pad_token_id is None and self.config.eos_token_id is not None: logger.warning( F'''The `config.pad_token_id` is `None`. Using `config.eos_token_id` = {self.config.eos_token_id} for''' """ padding..""" ) if self.args.label_smoothing == 0: _A : Optional[Any] = torch.nn.CrossEntropyLoss(ignore_index=self.config.pad_token_id ) else: # dynamically import label_smoothed_nll_loss from utils import label_smoothed_nll_loss _A : Union[str, Any] = label_smoothed_nll_loss def a__ ( self , _a ) -> int: if self.optimizer is None: _A : List[str] = ["""bias""", """LayerNorm.weight"""] _A : str = [ { """params""": [p for n, p in self.model.named_parameters() if not any(nd in n for nd in no_decay )], """weight_decay""": self.args.weight_decay, }, { """params""": [p for n, p in self.model.named_parameters() if any(nd in n for nd in no_decay )], """weight_decay""": 0.0, }, ] _A : Optional[Any] = Adafactor if self.args.adafactor else AdamW if self.args.adafactor: _A : Dict = Adafactor _A : int = {"""scale_parameter""": False, """relative_step""": False} else: _A : int = AdamW _A : Any = { """betas""": (self.args.adam_betaa, self.args.adam_betaa), """eps""": self.args.adam_epsilon, } _A : List[str] = self.args.learning_rate if self.sharded_ddp: _A : List[str] = OSS( params=_a , optim=_a , **_a , ) else: _A : Tuple = optimizer_cls(_a , **_a ) if self.lr_scheduler is None: _A : Union[str, Any] = self._get_lr_scheduler(_a ) else: # ignoring --lr_scheduler logger.warning("""scheduler is passed to `Seq2SeqTrainer`, `--lr_scheduler` arg is ignored.""" ) def a__ ( self , _a ) -> Dict: _A : List[Any] = arg_to_scheduler[self.args.lr_scheduler] if self.args.lr_scheduler == "constant": _A : Optional[Any] = schedule_func(self.optimizer ) elif self.args.lr_scheduler == "constant_w_warmup": _A : Optional[int] = schedule_func(self.optimizer , num_warmup_steps=self.args.warmup_steps ) else: _A : List[Any] = schedule_func( self.optimizer , num_warmup_steps=self.args.warmup_steps , num_training_steps=_a ) return scheduler def a__ ( self ) -> Optional[torch.utils.data.Sampler]: if isinstance(self.train_dataset , torch.utils.data.IterableDataset ): return None elif is_torch_tpu_available(): return get_tpu_sampler(self.train_dataset ) else: if self.args.sortish_sampler: self.train_dataset.make_sortish_sampler( self.args.per_device_train_batch_size , distributed=(self.args.parallel_mode == ParallelMode.DISTRIBUTED) , ) return ( RandomSampler(self.train_dataset ) if self.args.local_rank == -1 else DistributedSampler(self.train_dataset ) ) def a__ ( self , _a , _a , _a ) -> List[str]: if self.args.label_smoothing == 0: if self.data_args is not None and self.data_args.ignore_pad_token_for_loss: # force training to ignore pad token _A : List[str] = model(**_a , use_cache=_a )[0] _A : Dict = self.loss_fn(logits.view(-1 , logits.shape[-1] ) , labels.view(-1 ) ) else: # compute usual loss via models _A , _A : str = model(**_a , labels=_a , use_cache=_a )[:2] else: # compute label smoothed loss _A : Any = model(**_a , use_cache=_a )[0] _A : Union[str, Any] = torch.nn.functional.log_softmax(_a , dim=-1 ) _A , _A : List[str] = self.loss_fn(_a , _a , self.args.label_smoothing , ignore_index=self.config.pad_token_id ) return loss, logits def a__ ( self , _a , _a ) -> List[Any]: _A : Optional[int] = inputs.pop("""labels""" ) _A , _A : Dict = self._compute_loss(_a , _a , _a ) return loss def a__ ( self , _a , _a , _a , _a = None , ) -> Tuple[Optional[float], Optional[torch.Tensor], Optional[torch.Tensor]]: _A : int = self._prepare_inputs(_a ) _A : Dict = { """max_length""": self.data_args.val_max_target_length if self.data_args is not None else self.config.max_length, """num_beams""": self.data_args.eval_beams if self.data_args is not None else self.config.num_beams, } if self.args.predict_with_generate and not self.args.prediction_loss_only: _A : List[str] = self.model.generate( inputs["""input_ids"""] , attention_mask=inputs["""attention_mask"""] , **_a , ) # in case the batch is shorter than max length, the output should be padded if generated_tokens.shape[-1] < gen_kwargs["max_length"]: _A : str = self._pad_tensors_to_max_len(_a , gen_kwargs["""max_length"""] ) _A : Any = inputs.pop("""labels""" ) with torch.no_grad(): # compute loss on predict data _A , _A : Tuple = self._compute_loss(_a , _a , _a ) _A : Any = loss.mean().detach() if self.args.prediction_loss_only: return (loss, None, None) _A : List[Any] = generated_tokens if self.args.predict_with_generate else logits if labels.shape[-1] < gen_kwargs["max_length"]: _A : Any = self._pad_tensors_to_max_len(_a , gen_kwargs["""max_length"""] ) return (loss, logits, labels) def a__ ( self , _a , _a ) -> Union[str, Any]: # If PAD token is not defined at least EOS token has to be defined _A : Optional[Any] = self.config.pad_token_id if self.config.pad_token_id is not None else self.config.eos_token_id if pad_token_id is None: raise ValueError( """Make sure that either `config.pad_token_id` or `config.eos_token_id` is defined if tensor has to be""" F''' padded to `max_length`={max_length}''' ) _A : Dict = pad_token_id * torch.ones( (tensor.shape[0], max_length) , dtype=tensor.dtype , device=tensor.device ) _A : Dict = tensor return padded_tensor
54
0
import json import os import unittest from transformers import CLIPTokenizer, CLIPTokenizerFast from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES from transformers.testing_utils import require_ftfy, require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class lowercase ( a__,unittest.TestCase ): _a = CLIPTokenizer _a = CLIPTokenizerFast _a = True _a = {} _a = False def a__ ( self ) -> Any: super().setUp() # fmt: off _A : Dict = ["l", "o", "w", "e", "r", "s", "t", "i", "d", "n", "lo", "l</w>", "w</w>", "r</w>", "t</w>", "low</w>", "er</w>", "lowest</w>", "newer</w>", "wider", "<unk>", "<|startoftext|>", "<|endoftext|>"] # fmt: on _A : Any = dict(zip(lowerCAmelCase__ , range(len(lowerCAmelCase__ ) ) ) ) _A : Optional[int] = ["#version: 0.2", "l o", "lo w</w>", "e r</w>"] _A : List[str] = {"unk_token": "<unk>"} _A : Dict = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""vocab_file"""] ) _A : List[str] = 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(lowerCAmelCase__ ) + """\n""" ) with open(self.merges_file , """w""" , encoding="""utf-8""" ) as fp: fp.write("""\n""".join(lowerCAmelCase__ ) ) def a__ ( self , **_a ) -> List[Any]: kwargs.update(self.special_tokens_map ) return CLIPTokenizer.from_pretrained(self.tmpdirname , **lowerCAmelCase__ ) def a__ ( self , **_a ) -> int: kwargs.update(self.special_tokens_map ) return CLIPTokenizerFast.from_pretrained(self.tmpdirname , **lowerCAmelCase__ ) def a__ ( self , _a ) -> Optional[int]: _A : List[Any] = "lower newer" _A : int = "lower newer" return input_text, output_text def a__ ( self ) -> str: _A : Optional[Any] = CLIPTokenizer(self.vocab_file , self.merges_file , **self.special_tokens_map ) _A : List[Any] = "lower newer" _A : Optional[int] = ["lo", "w", "er</w>", "n", "e", "w", "er</w>"] _A : List[str] = tokenizer.tokenize(lowerCAmelCase__ ) self.assertListEqual(lowerCAmelCase__ , lowerCAmelCase__ ) _A : List[Any] = tokens + [tokenizer.unk_token] _A : List[Any] = [10, 2, 16, 9, 3, 2, 16, 20] self.assertListEqual(tokenizer.convert_tokens_to_ids(lowerCAmelCase__ ) , lowerCAmelCase__ ) @require_ftfy def a__ ( self ) -> Dict: for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): _A : List[str] = self.tokenizer_class.from_pretrained(lowerCAmelCase__ , **lowerCAmelCase__ ) _A : List[Any] = self.rust_tokenizer_class.from_pretrained(lowerCAmelCase__ , **lowerCAmelCase__ ) _A : Any = "A\n'll 11p223RF☆ho!!to?'d'd''d of a cat to-$''d." _A : str = tokenizer_s.tokenize(lowerCAmelCase__ ) _A : Union[str, Any] = tokenizer_r.tokenize(lowerCAmelCase__ ) self.assertListEqual(lowerCAmelCase__ , lowerCAmelCase__ ) # Test that the tokenization is identical on an example containing a character (Latin Small Letter A # with Tilde) encoded in 2 different ways _A : List[Any] = "xa\u0303y" + " " + "x\xe3y" _A : str = tokenizer_s.tokenize(lowerCAmelCase__ ) _A : Optional[int] = tokenizer_r.tokenize(lowerCAmelCase__ ) self.assertListEqual(lowerCAmelCase__ , lowerCAmelCase__ ) # Test that the tokenization is identical on unicode of space type _A : Any = [ "\u0009", # (horizontal tab, '\t') "\u000B", # (vertical tab) "\u000C", # (form feed) "\u0020", # (space, ' ') "\u200E", # (left-to-right mark):w "\u200F", # (right-to-left mark) ] for unicode_seq in spaces_unicodes: _A : Any = tokenizer_s.tokenize(lowerCAmelCase__ ) _A : Dict = tokenizer_r.tokenize(lowerCAmelCase__ ) self.assertListEqual(lowerCAmelCase__ , lowerCAmelCase__ ) # Test that the tokenization is identical on unicode of line break type _A : Any = [ "\u000A", # (line feed, '\n') "\r\n", # (carriage return and line feed, '\r\n') "\u000D", # (carriage return, '\r') "\r", # (carriage return, '\r') "\u000D", # (carriage return, '\r') "\u2028", # (line separator) "\u2029", # (paragraph separator) # "\u0085", # (next line) ] # The tokenization is not identical for the character "\u0085" (next line). The slow version using ftfy transforms # it into the Horizontal Ellipsis character "…" ("\u2026") while the fast version transforms it into a # space (and thus into an empty list). for unicode_seq in line_break_unicodes: _A : List[str] = tokenizer_s.tokenize(lowerCAmelCase__ ) _A : Tuple = tokenizer_r.tokenize(lowerCAmelCase__ ) self.assertListEqual(lowerCAmelCase__ , lowerCAmelCase__ ) def a__ ( self ) -> List[str]: # Test which aims to verify that the offsets are well adapted to the argument `add_prefix_space` for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): _A : Tuple = "hello" # `hello` is a token in the vocabulary of `pretrained_name` _A : Tuple = F'''{text_of_1_token} {text_of_1_token}''' _A : Union[str, Any] = self.rust_tokenizer_class.from_pretrained( lowerCAmelCase__ , use_fast=lowerCAmelCase__ , ) _A : str = tokenizer_r(lowerCAmelCase__ , return_offsets_mapping=lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__ ) self.assertEqual(encoding.offset_mapping[0] , (0, len(lowerCAmelCase__ )) ) self.assertEqual( encoding.offset_mapping[1] , (len(lowerCAmelCase__ ) + 1, len(lowerCAmelCase__ ) + 1 + len(lowerCAmelCase__ )) , ) _A : Optional[Any] = F''' {text}''' _A : Optional[int] = self.rust_tokenizer_class.from_pretrained( lowerCAmelCase__ , use_fast=lowerCAmelCase__ , ) _A : Any = tokenizer_r(lowerCAmelCase__ , return_offsets_mapping=lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__ ) self.assertEqual(encoding.offset_mapping[0] , (1, 1 + len(lowerCAmelCase__ )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(lowerCAmelCase__ ) + 1, 1 + len(lowerCAmelCase__ ) + 1 + len(lowerCAmelCase__ )) , ) def a__ ( self ) -> Union[str, Any]: # Test related to the breaking change introduced in transformers v4.17.0 # We need to check that an error in raised when the user try to load a previous version of the tokenizer. with self.assertRaises(lowerCAmelCase__ ) as context: self.rust_tokenizer_class.from_pretrained("""robot-test/old-clip-tokenizer""" ) self.assertTrue( context.exception.args[0].startswith( """The `backend_tokenizer` provided does not match the expected format.""" ) ) @require_ftfy def a__ ( self ) -> Union[str, Any]: super().test_tokenization_python_rust_equals() def a__ ( self ) -> List[str]: # CLIP always lower cases letters pass
709
from collections.abc import Callable from math import pi, sqrt from random import uniform from statistics import mean def lowerCAmelCase_ ( snake_case_ ): # A local function to see if a dot lands in the circle. def is_in_circle(snake_case_,snake_case_ ) -> bool: _A : List[str] = sqrt((x**2) + (y**2) ) # Our circle has a radius of 1, so a distance # greater than 1 would land outside the circle. return distance_from_centre <= 1 # The proportion of guesses that landed in the circle _A : Optional[int] = mean( int(is_in_circle(uniform(-1.0,1.0 ),uniform(-1.0,1.0 ) ) ) for _ in range(snake_case_ ) ) # The ratio of the area for circle to square is pi/4. _A : List[str] = proportion * 4 print(f'''The estimated value of pi is {pi_estimate}''' ) print(f'''The numpy value of pi is {pi}''' ) print(f'''The total error is {abs(pi - pi_estimate )}''' ) def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_ = 0.0,snake_case_ = 1.0,): return mean( function_to_integrate(uniform(snake_case_,snake_case_ ) ) for _ in range(snake_case_ ) ) * (max_value - min_value) def lowerCAmelCase_ ( snake_case_,snake_case_ = 0.0,snake_case_ = 1.0 ): def identity_function(snake_case_ ) -> float: return x _A : Any = area_under_curve_estimator( snake_case_,snake_case_,snake_case_,snake_case_ ) _A : Tuple = (max_value * max_value - min_value * min_value) / 2 print("""******************""" ) print(f'''Estimating area under y=x where x varies from {min_value} to {max_value}''' ) print(f'''Estimated value is {estimated_value}''' ) print(f'''Expected value is {expected_value}''' ) print(f'''Total error is {abs(estimated_value - expected_value )}''' ) print("""******************""" ) def lowerCAmelCase_ ( snake_case_ ): def function_to_integrate(snake_case_ ) -> float: return sqrt(4.0 - x * x ) _A : Optional[int] = area_under_curve_estimator( snake_case_,snake_case_,0.0,2.0 ) print("""******************""" ) print("""Estimating pi using area_under_curve_estimator""" ) print(f'''Estimated value is {estimated_value}''' ) print(f'''Expected value is {pi}''' ) print(f'''Total error is {abs(estimated_value - pi )}''' ) print("""******************""" ) if __name__ == "__main__": import doctest doctest.testmod()
54
0
import os import random import sys from . import cryptomath_module as cryptoMath # noqa: N812 from . import rabin_miller as rabinMiller # noqa: N812 def lowerCAmelCase_ ( ): print("""Making key files...""" ) make_key_files("""rsa""",1024 ) print("""Key files generation successful.""" ) def lowerCAmelCase_ ( snake_case_ ): print("""Generating prime p...""" ) _A : Dict = rabinMiller.generate_large_prime(UpperCAmelCase__ ) print("""Generating prime q...""" ) _A : int = rabinMiller.generate_large_prime(UpperCAmelCase__ ) _A : List[str] = p * q print("""Generating e that is relatively prime to (p - 1) * (q - 1)...""" ) while True: _A : List[str] = random.randrange(2 ** (key_size - 1),2 ** (key_size) ) if cryptoMath.gcd(UpperCAmelCase__,(p - 1) * (q - 1) ) == 1: break print("""Calculating d that is mod inverse of e...""" ) _A : str = cryptoMath.find_mod_inverse(UpperCAmelCase__,(p - 1) * (q - 1) ) _A : str = (n, e) _A : List[str] = (n, d) return (public_key, private_key) def lowerCAmelCase_ ( snake_case_,snake_case_ ): if os.path.exists(f'''{name}_pubkey.txt''' ) or os.path.exists(f'''{name}_privkey.txt''' ): print("""\nWARNING:""" ) print( f'''\"{name}_pubkey.txt\" or \"{name}_privkey.txt\" already exists. \n''' """Use a different name or delete these files and re-run this program.""" ) sys.exit() _A , _A : int = generate_key(UpperCAmelCase__ ) print(f'''\nWriting public key to file {name}_pubkey.txt...''' ) with open(f'''{name}_pubkey.txt''',"""w""" ) as out_file: out_file.write(f'''{key_size},{public_key[0]},{public_key[1]}''' ) print(f'''Writing private key to file {name}_privkey.txt...''' ) with open(f'''{name}_privkey.txt''',"""w""" ) as out_file: out_file.write(f'''{key_size},{private_key[0]},{private_key[1]}''' ) if __name__ == "__main__": main()
710
import json import os import shutil import tempfile from unittest import TestCase from transformers import BartTokenizer, BartTokenizerFast, DPRQuestionEncoderTokenizer, DPRQuestionEncoderTokenizerFast from transformers.models.bart.configuration_bart import BartConfig from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES as DPR_VOCAB_FILES_NAMES from transformers.models.dpr.configuration_dpr import DPRConfig from transformers.models.roberta.tokenization_roberta import VOCAB_FILES_NAMES as BART_VOCAB_FILES_NAMES from transformers.testing_utils import require_faiss, require_tokenizers, require_torch, slow from transformers.utils import is_datasets_available, is_faiss_available, is_torch_available if is_torch_available() and is_datasets_available() and is_faiss_available(): from transformers.models.rag.configuration_rag import RagConfig from transformers.models.rag.tokenization_rag import RagTokenizer @require_faiss @require_torch class lowercase ( UpperCamelCase__ ): def a__ ( self ) -> int: _A : int = tempfile.mkdtemp() _A : Union[str, Any] = 8 # DPR tok _A : List[str] = [ """[UNK]""", """[CLS]""", """[SEP]""", """[PAD]""", """[MASK]""", """want""", """##want""", """##ed""", """wa""", """un""", """runn""", """##ing""", """,""", """low""", """lowest""", ] _A : List[str] = os.path.join(self.tmpdirname , """dpr_tokenizer""" ) os.makedirs(_a , exist_ok=_a ) _A : str = os.path.join(_a , DPR_VOCAB_FILES_NAMES["""vocab_file"""] ) with open(self.vocab_file , """w""" , encoding="""utf-8""" ) as vocab_writer: vocab_writer.write("""""".join([x + """\n""" for x in vocab_tokens] ) ) # BART tok _A : Dict = [ """l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """\u0120""", """\u0120l""", """\u0120n""", """\u0120lo""", """\u0120low""", """er""", """\u0120lowest""", """\u0120newer""", """\u0120wider""", """<unk>""", ] _A : Optional[Any] = dict(zip(_a , range(len(_a ) ) ) ) _A : Tuple = ["""#version: 0.2""", """\u0120 l""", """\u0120l o""", """\u0120lo w""", """e r""", """"""] _A : Dict = {"""unk_token""": """<unk>"""} _A : Optional[Any] = os.path.join(self.tmpdirname , """bart_tokenizer""" ) os.makedirs(_a , exist_ok=_a ) _A : str = os.path.join(_a , BART_VOCAB_FILES_NAMES["""vocab_file"""] ) _A : List[Any] = os.path.join(_a , BART_VOCAB_FILES_NAMES["""merges_file"""] ) with open(self.vocab_file , """w""" , encoding="""utf-8""" ) as fp: fp.write(json.dumps(_a ) + """\n""" ) with open(self.merges_file , """w""" , encoding="""utf-8""" ) as fp: fp.write("""\n""".join(_a ) ) def a__ ( self ) -> DPRQuestionEncoderTokenizer: return DPRQuestionEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname , """dpr_tokenizer""" ) ) def a__ ( self ) -> BartTokenizer: return BartTokenizer.from_pretrained(os.path.join(self.tmpdirname , """bart_tokenizer""" ) ) def a__ ( self ) -> Optional[Any]: shutil.rmtree(self.tmpdirname ) @require_tokenizers def a__ ( self ) -> str: _A : Optional[Any] = os.path.join(self.tmpdirname , """rag_tokenizer""" ) _A : int = RagConfig(question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() ) _A : Any = RagTokenizer(question_encoder=self.get_dpr_tokenizer() , generator=self.get_bart_tokenizer() ) rag_config.save_pretrained(_a ) rag_tokenizer.save_pretrained(_a ) _A : Optional[Any] = RagTokenizer.from_pretrained(_a , config=_a ) self.assertIsInstance(new_rag_tokenizer.question_encoder , _a ) self.assertEqual(new_rag_tokenizer.question_encoder.get_vocab() , rag_tokenizer.question_encoder.get_vocab() ) self.assertIsInstance(new_rag_tokenizer.generator , _a ) self.assertEqual(new_rag_tokenizer.generator.get_vocab() , rag_tokenizer.generator.get_vocab() ) @slow def a__ ( self ) -> str: _A : Union[str, Any] = RagTokenizer.from_pretrained("""facebook/rag-token-nq""" ) _A : Tuple = [ """who got the first nobel prize in physics""", """when is the next deadpool movie being released""", """which mode is used for short wave broadcast service""", """who is the owner of reading football club""", """when is the next scandal episode coming out""", """when is the last time the philadelphia won the superbowl""", """what is the most current adobe flash player version""", """how many episodes are there in dragon ball z""", """what is the first step in the evolution of the eye""", """where is gall bladder situated in human body""", """what is the main mineral in lithium batteries""", """who is the president of usa right now""", """where do the greasers live in the outsiders""", """panda is a national animal of which country""", """what is the name of manchester united stadium""", ] _A : Tuple = tokenizer(_a ) self.assertIsNotNone(_a ) @slow def a__ ( self ) -> Dict: _A : Dict = RagTokenizer.from_pretrained("""facebook/rag-sequence-nq""" ) _A : str = [ """who got the first nobel prize in physics""", """when is the next deadpool movie being released""", """which mode is used for short wave broadcast service""", """who is the owner of reading football club""", """when is the next scandal episode coming out""", """when is the last time the philadelphia won the superbowl""", """what is the most current adobe flash player version""", """how many episodes are there in dragon ball z""", """what is the first step in the evolution of the eye""", """where is gall bladder situated in human body""", """what is the main mineral in lithium batteries""", """who is the president of usa right now""", """where do the greasers live in the outsiders""", """panda is a national animal of which country""", """what is the name of manchester united stadium""", ] _A : Optional[Any] = tokenizer(_a ) self.assertIsNotNone(_a )
54
0
def lowerCAmelCase_ ( snake_case_ ): _A , _A : int = [], [] while len(_snake_case ) > 1: _A , _A : Optional[int] = min(_snake_case ), max(_snake_case ) start.append(_snake_case ) end.append(_snake_case ) collection.remove(_snake_case ) collection.remove(_snake_case ) end.reverse() return start + collection + end if __name__ == "__main__": _snake_case = input("Enter numbers separated by a comma:\n").strip() _snake_case = [int(item) for item in user_input.split(",")] print(*merge_sort(unsorted), sep=",")
711
import os import re import shutil import sys import tempfile import unittest import black _snake_case = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) sys.path.append(os.path.join(git_repo_path, "utils")) import check_copies # noqa: E402 # This is the reference code that will be used in the tests. # If BertLMPredictionHead is changed in modeling_bert.py, this code needs to be manually updated. _snake_case = " def __init__(self, config):\n super().__init__()\n self.transform = BertPredictionHeadTransform(config)\n\n # The output weights are the same as the input embeddings, but there is\n # an output-only bias for each token.\n self.decoder = nn.Linear(config.hidden_size, config.vocab_size, bias=False)\n\n self.bias = nn.Parameter(torch.zeros(config.vocab_size))\n\n # Need a link between the two variables so that the bias is correctly resized with `resize_token_embeddings`\n self.decoder.bias = self.bias\n\n def forward(self, hidden_states):\n hidden_states = self.transform(hidden_states)\n hidden_states = self.decoder(hidden_states)\n return hidden_states\n" class lowercase ( unittest.TestCase ): def a__ ( self ) -> Union[str, Any]: _A : List[str] = tempfile.mkdtemp() os.makedirs(os.path.join(self.transformer_dir , """models/bert/""" ) ) _A : str = self.transformer_dir shutil.copy( os.path.join(_a , """src/transformers/models/bert/modeling_bert.py""" ) , os.path.join(self.transformer_dir , """models/bert/modeling_bert.py""" ) , ) def a__ ( self ) -> Optional[int]: _A : List[str] = """src/transformers""" shutil.rmtree(self.transformer_dir ) def a__ ( self , _a , _a , _a , _a=None ) -> Optional[Any]: _A : Optional[Any] = comment + F'''\nclass {class_name}(nn.Module):\n''' + class_code if overwrite_result is not None: _A : List[str] = comment + F'''\nclass {class_name}(nn.Module):\n''' + overwrite_result _A : List[str] = black.Mode(target_versions={black.TargetVersion.PYaa} , line_length=119 ) _A : Optional[int] = black.format_str(_a , mode=_a ) _A : Optional[Any] = os.path.join(self.transformer_dir , """new_code.py""" ) with open(_a , """w""" , newline="""\n""" ) as f: f.write(_a ) if overwrite_result is None: self.assertTrue(len(check_copies.is_copy_consistent(_a ) ) == 0 ) else: check_copies.is_copy_consistent(f.name , overwrite=_a ) with open(_a , """r""" ) as f: self.assertTrue(f.read() , _a ) def a__ ( self ) -> str: _A : Union[str, Any] = check_copies.find_code_in_transformers("""models.bert.modeling_bert.BertLMPredictionHead""" ) self.assertEqual(_a , _a ) def a__ ( self ) -> int: # Base copy consistency self.check_copy_consistency( """# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead""" , """BertLMPredictionHead""" , REFERENCE_CODE + """\n""" , ) # With no empty line at the end self.check_copy_consistency( """# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead""" , """BertLMPredictionHead""" , _a , ) # Copy consistency with rename self.check_copy_consistency( """# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->TestModel""" , """TestModelLMPredictionHead""" , re.sub("""Bert""" , """TestModel""" , _a ) , ) # Copy consistency with a really long name _A : List[str] = """TestModelWithAReallyLongNameBecauseSomePeopleLikeThatForSomeReason""" self.check_copy_consistency( F'''# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->{long_class_name}''' , F'''{long_class_name}LMPredictionHead''' , re.sub("""Bert""" , _a , _a ) , ) # Copy consistency with overwrite self.check_copy_consistency( """# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->TestModel""" , """TestModelLMPredictionHead""" , _a , overwrite_result=re.sub("""Bert""" , """TestModel""" , _a ) , ) def a__ ( self ) -> Tuple: _A : Union[str, Any] = check_copies.LOCALIZED_READMES["""README_zh-hans.md"""] _A : str = ( """1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (from Google Research and the""" """ Toyota Technological Institute at Chicago) released with the paper [ALBERT: A Lite BERT for""" """ Self-supervised Learning of Language Representations](https://arxiv.org/abs/1909.11942), by Zhenzhong""" """ Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut.\n1.""" """ **[DistilBERT](https://huggingface.co/transformers/model_doc/distilbert.html)** (from HuggingFace),""" """ released together with the paper [DistilBERT, a distilled version of BERT: smaller, faster, cheaper and""" """ lighter](https://arxiv.org/abs/1910.01108) by Victor Sanh, Lysandre Debut and Thomas Wolf. The same""" """ method has been applied to compress GPT2 into""" """ [DistilGPT2](https://github.com/huggingface/transformers/tree/main/examples/distillation), RoBERTa into""" """ [DistilRoBERTa](https://github.com/huggingface/transformers/tree/main/examples/distillation),""" """ Multilingual BERT into""" """ [DistilmBERT](https://github.com/huggingface/transformers/tree/main/examples/distillation) and a German""" """ version of DistilBERT.\n1. **[ELECTRA](https://huggingface.co/transformers/model_doc/electra.html)**""" """ (from Google Research/Stanford University) released with the paper [ELECTRA: Pre-training text encoders""" """ as discriminators rather than generators](https://arxiv.org/abs/2003.10555) by Kevin Clark, Minh-Thang""" """ Luong, Quoc V. Le, Christopher D. Manning.""" ) _A : str = ( """1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the""" """ Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of""" """ Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian""" """ Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n""" ) _A : Any = ( """1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the""" """ Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of""" """ Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian""" """ Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n1.""" """ **[DistilBERT](https://huggingface.co/transformers/model_doc/distilbert.html)** (来自 HuggingFace) 伴随论文""" """ [DistilBERT, a distilled version of BERT: smaller, faster, cheaper and""" """ lighter](https://arxiv.org/abs/1910.01108) 由 Victor Sanh, Lysandre Debut and Thomas Wolf 发布。 The same""" """ method has been applied to compress GPT2 into""" """ [DistilGPT2](https://github.com/huggingface/transformers/tree/main/examples/distillation), RoBERTa into""" """ [DistilRoBERTa](https://github.com/huggingface/transformers/tree/main/examples/distillation),""" """ Multilingual BERT into""" """ [DistilmBERT](https://github.com/huggingface/transformers/tree/main/examples/distillation) and a German""" """ version of DistilBERT.\n1. **[ELECTRA](https://huggingface.co/transformers/model_doc/electra.html)** (来自""" """ Google Research/Stanford University) 伴随论文 [ELECTRA: Pre-training text encoders as discriminators rather""" """ than generators](https://arxiv.org/abs/2003.10555) 由 Kevin Clark, Minh-Thang Luong, Quoc V. Le,""" """ Christopher D. Manning 发布。\n""" ) _A , _A : Tuple = check_copies.convert_to_localized_md( _a , _a , localized_readme["""format_model_list"""] ) self.assertFalse(_a ) self.assertEqual(_a , _a ) _A , _A : List[str] = check_copies.convert_to_localized_md( _a , _a , localized_readme["""format_model_list"""] ) # Check whether the number of models is equal to README.md after conversion. self.assertTrue(_a ) _A : Tuple = ( """1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (from Google Research and the""" """ Toyota Technological Institute at Chicago) released with the paper [ALBERT: A Lite BERT for""" """ Self-supervised Learning of Language Representations](https://arxiv.org/abs/1909.11942), by Zhenzhong""" """ Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut.""" ) _A : Dict = ( """1. **[ALBERT](https://huggingface.co/transformers/main/model_doc/albert.html)** (来自 Google Research and""" """ the Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of""" """ Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian""" """ Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n""" ) _A : Optional[Any] = ( """1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the""" """ Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of""" """ Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian""" """ Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n""" ) _A , _A : Optional[int] = check_copies.convert_to_localized_md( _a , _a , localized_readme["""format_model_list"""] ) # Check if the model link is synchronized. self.assertEqual(_a , _a )
54
0
import numpy as np def lowerCAmelCase_ ( snake_case_ ): return 1 / (1 + np.exp(-vector )) def lowerCAmelCase_ ( snake_case_ ): return vector * sigmoid(1.7_02 * vector ) if __name__ == "__main__": import doctest doctest.testmod()
712
import itertools import os import random import tempfile import unittest import numpy as np from transformers import TvltFeatureExtractor, is_datasets_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_torch_available(): import torch if is_datasets_available(): from datasets import load_dataset _snake_case = random.Random() def lowerCAmelCase_ ( snake_case_,snake_case_=1.0,snake_case_=None,snake_case_=None ): if rng is None: _A : str = global_rng _A : List[Any] = [] for batch_idx in range(shape[0] ): values.append([] ) for _ in range(shape[1] ): values[-1].append(rng.random() * scale ) return values class lowercase ( unittest.TestCase ): def __init__( self , _a , _a=7 , _a=400 , _a=2000 , _a=2048 , _a=128 , _a=1 , _a=512 , _a=30 , _a=4_4100 , ) -> Tuple: _A : Any = parent _A : str = batch_size _A : Union[str, Any] = min_seq_length _A : int = max_seq_length _A : List[str] = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) _A : Tuple = spectrogram_length _A : int = feature_size _A : str = num_audio_channels _A : Tuple = hop_length _A : List[str] = chunk_length _A : Union[str, Any] = sampling_rate def a__ ( self ) -> Tuple: return { "spectrogram_length": self.spectrogram_length, "feature_size": self.feature_size, "num_audio_channels": self.num_audio_channels, "hop_length": self.hop_length, "chunk_length": self.chunk_length, "sampling_rate": self.sampling_rate, } def a__ ( self , _a=False , _a=False ) -> Optional[int]: def _flatten(_a ): return list(itertools.chain(*_a ) ) if equal_length: _A : List[str] = [floats_list((self.max_seq_length, self.feature_size) ) for _ in range(self.batch_size )] else: # make sure that inputs increase in size _A : Union[str, 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 : List[Any] = [np.asarray(_a ) for x in speech_inputs] return speech_inputs @require_torch @require_torchaudio class lowercase ( UpperCamelCase__,unittest.TestCase ): _a = TvltFeatureExtractor def a__ ( self ) -> Any: _A : int = TvltFeatureExtractionTester(self ) def a__ ( self ) -> List[Any]: _A : Tuple = self.feature_extraction_class(**self.feat_extract_dict ) self.assertTrue(hasattr(_a , """spectrogram_length""" ) ) self.assertTrue(hasattr(_a , """feature_size""" ) ) self.assertTrue(hasattr(_a , """num_audio_channels""" ) ) self.assertTrue(hasattr(_a , """hop_length""" ) ) self.assertTrue(hasattr(_a , """chunk_length""" ) ) self.assertTrue(hasattr(_a , """sampling_rate""" ) ) def a__ ( self ) -> Optional[int]: _A : str = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: _A : Tuple = feat_extract_first.save_pretrained(_a )[0] check_json_file_has_correct_format(_a ) _A : Optional[int] = self.feature_extraction_class.from_pretrained(_a ) _A : Optional[Any] = feat_extract_first.to_dict() _A : int = feat_extract_second.to_dict() _A : int = dict_first.pop("""mel_filters""" ) _A : Optional[int] = dict_second.pop("""mel_filters""" ) self.assertTrue(np.allclose(_a , _a ) ) self.assertEqual(_a , _a ) def a__ ( self ) -> int: _A : str = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: _A : List[str] = os.path.join(_a , """feat_extract.json""" ) feat_extract_first.to_json_file(_a ) _A : Union[str, Any] = self.feature_extraction_class.from_json_file(_a ) _A : Optional[Any] = feat_extract_first.to_dict() _A : Union[str, Any] = feat_extract_second.to_dict() _A : List[Any] = dict_first.pop("""mel_filters""" ) _A : Optional[Any] = dict_second.pop("""mel_filters""" ) self.assertTrue(np.allclose(_a , _a ) ) self.assertEqual(_a , _a ) def a__ ( self ) -> Optional[Any]: # Initialize feature_extractor _A : Tuple = self.feature_extraction_class(**self.feat_extract_dict ) # create three inputs of length 800, 1000, and 1200 _A : Optional[int] = [floats_list((1, x) )[0] for x in range(800 , 1400 , 200 )] _A : List[str] = [np.asarray(_a ) for speech_input in speech_inputs] # Test not batched input _A : Optional[int] = feature_extractor(np_speech_inputs[0] , return_tensors="""np""" , sampling_rate=4_4100 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test batched _A : str = feature_extractor(_a , return_tensors="""np""" , sampling_rate=4_4100 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test audio masking _A : Optional[Any] = feature_extractor( _a , return_tensors="""np""" , sampling_rate=4_4100 , mask_audio=_a ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test 2-D numpy arrays are batched. _A : Dict = [floats_list((1, x) )[0] for x in (800, 800, 800)] _A : Optional[int] = np.asarray(_a ) _A : Optional[Any] = feature_extractor(_a , return_tensors="""np""" , sampling_rate=4_4100 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) def a__ ( self , _a ) -> str: _A : Optional[Any] = load_dataset("""hf-internal-testing/librispeech_asr_dummy""" , """clean""" , split="""validation""" ) # automatic decoding with librispeech _A : Dict = ds.sort("""id""" ).select(range(_a ) )[:num_samples]["""audio"""] return [x["array"] for x in speech_samples] def a__ ( self ) -> Optional[Any]: _A : List[str] = self._load_datasamples(1 ) _A : List[str] = TvltFeatureExtractor() _A : List[Any] = feature_extractor(_a , return_tensors="""pt""" ).audio_values self.assertEquals(audio_values.shape , (1, 1, 192, 128) ) _A : int = torch.tensor([[-0.3032, -0.2708], [-0.4434, -0.4007]] ) self.assertTrue(torch.allclose(audio_values[0, 0, :2, :2] , _a , atol=1e-4 ) )
54
0
_snake_case = 65521 def lowerCAmelCase_ ( snake_case_ ): '''simple docstring''' _A : Optional[int] = 1 _A : str = 0 for plain_chr in plain_text: _A : int = (a + ord(__UpperCamelCase )) % MOD_ADLER _A : Tuple = (b + a) % MOD_ADLER return (b << 16) | a
713
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available _snake_case = { "configuration_time_series_transformer": [ "TIME_SERIES_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", "TimeSeriesTransformerConfig", ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = [ "TIME_SERIES_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "TimeSeriesTransformerForPrediction", "TimeSeriesTransformerModel", "TimeSeriesTransformerPreTrainedModel", ] if TYPE_CHECKING: from .configuration_time_series_transformer import ( TIME_SERIES_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TimeSeriesTransformerConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_time_series_transformer import ( TIME_SERIES_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TimeSeriesTransformerForPrediction, TimeSeriesTransformerModel, TimeSeriesTransformerPreTrainedModel, ) else: import sys _snake_case = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
54
0
def lowerCAmelCase_ ( snake_case_,snake_case_ ): return int((input_a, input_a).count(0 ) == 0 ) def lowerCAmelCase_ ( ): 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))
714
import gc import random import unittest import numpy as np import torch from PIL import Image from diffusers import ( DDIMScheduler, KandinskyVaaImgaImgPipeline, KandinskyVaaPriorPipeline, UNetaDConditionModel, VQModel, ) 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 lowercase ( UpperCamelCase__,unittest.TestCase ): _a = KandinskyVaaImgaImgPipeline _a = ["image_embeds", "negative_image_embeds", "image"] _a = [ "image_embeds", "negative_image_embeds", "image", ] _a = [ "generator", "height", "width", "strength", "guidance_scale", "num_inference_steps", "return_dict", "guidance_scale", "num_images_per_prompt", "output_type", "return_dict", ] _a = False @property def a__ ( self ) -> int: return 32 @property def a__ ( self ) -> Union[str, Any]: return 32 @property def a__ ( self ) -> List[str]: return self.time_input_dim @property def a__ ( self ) -> Union[str, Any]: return self.time_input_dim * 4 @property def a__ ( self ) -> str: return 100 @property def a__ ( self ) -> Tuple: torch.manual_seed(0 ) _A : str = { """in_channels""": 4, # Out channels is double in channels because predicts mean and variance """out_channels""": 8, """addition_embed_type""": """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""": """image_proj""", """cross_attention_dim""": self.cross_attention_dim, """attention_head_dim""": 4, """resnet_time_scale_shift""": """scale_shift""", """class_embed_type""": None, } _A : Union[str, Any] = UNetaDConditionModel(**_a ) return model @property def a__ ( self ) -> int: 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 a__ ( self ) -> Tuple: torch.manual_seed(0 ) _A : Dict = VQModel(**self.dummy_movq_kwargs ) return model def a__ ( self ) -> int: _A : Any = self.dummy_unet _A : List[Any] = self.dummy_movq _A : str = { """num_train_timesteps""": 1000, """beta_schedule""": """linear""", """beta_start""": 0.00085, """beta_end""": 0.012, """clip_sample""": False, """set_alpha_to_one""": False, """steps_offset""": 0, """prediction_type""": """epsilon""", """thresholding""": False, } _A : int = DDIMScheduler(**_a ) _A : Tuple = { """unet""": unet, """scheduler""": scheduler, """movq""": movq, } return components def a__ ( self , _a , _a=0 ) -> str: _A : Optional[int] = floats_tensor((1, self.text_embedder_hidden_size) , rng=random.Random(_a ) ).to(_a ) _A : Dict = floats_tensor((1, self.text_embedder_hidden_size) , rng=random.Random(seed + 1 ) ).to( _a ) # create init_image _A : Optional[Any] = floats_tensor((1, 3, 64, 64) , rng=random.Random(_a ) ).to(_a ) _A : Optional[Any] = image.cpu().permute(0 , 2 , 3 , 1 )[0] _A : Optional[Any] = Image.fromarray(np.uinta(_a ) ).convert("""RGB""" ).resize((256, 256) ) if str(_a ).startswith("""mps""" ): _A : Tuple = torch.manual_seed(_a ) else: _A : str = torch.Generator(device=_a ).manual_seed(_a ) _A : Optional[Any] = { """image""": init_image, """image_embeds""": image_embeds, """negative_image_embeds""": negative_image_embeds, """generator""": generator, """height""": 64, """width""": 64, """num_inference_steps""": 10, """guidance_scale""": 7.0, """strength""": 0.2, """output_type""": """np""", } return inputs def a__ ( self ) -> Union[str, Any]: _A : Dict = """cpu""" _A : int = self.get_dummy_components() _A : Optional[int] = self.pipeline_class(**_a ) _A : Any = pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) _A : List[Any] = pipe(**self.get_dummy_inputs(_a ) ) _A : Dict = output.images _A : List[str] = pipe( **self.get_dummy_inputs(_a ) , return_dict=_a , )[0] _A : Dict = image[0, -3:, -3:, -1] _A : Union[str, Any] = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) _A : Optional[int] = np.array( [0.6199778, 0.63984406, 0.46145785, 0.62944984, 0.5622215, 0.47306132, 0.47441456, 0.4607606, 0.48719263] ) 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()}''' @slow @require_torch_gpu class lowercase ( unittest.TestCase ): def a__ ( self ) -> List[Any]: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def a__ ( self ) -> List[str]: _A : Any = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/kandinskyv22/kandinskyv22_img2img_frog.npy""" ) _A : Union[str, Any] = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/kandinsky/cat.png""" ) _A : Dict = """A red cartoon frog, 4k""" _A : Dict = KandinskyVaaPriorPipeline.from_pretrained( """kandinsky-community/kandinsky-2-2-prior""" , torch_dtype=torch.floataa ) pipe_prior.to(_a ) _A : int = KandinskyVaaImgaImgPipeline.from_pretrained( """kandinsky-community/kandinsky-2-2-decoder""" , torch_dtype=torch.floataa ) _A : Dict = pipeline.to(_a ) pipeline.set_progress_bar_config(disable=_a ) _A : Tuple = torch.Generator(device="""cpu""" ).manual_seed(0 ) _A , _A : List[str] = pipe_prior( _a , generator=_a , num_inference_steps=5 , negative_prompt="""""" , ).to_tuple() _A : int = pipeline( image=_a , image_embeds=_a , negative_image_embeds=_a , generator=_a , num_inference_steps=100 , height=768 , width=768 , strength=0.2 , output_type="""np""" , ) _A : Optional[int] = output.images[0] assert image.shape == (768, 768, 3) assert_mean_pixel_difference(_a , _a )
54
0
import argparse import json import os import pickle import shutil import numpy as np import torch from distiller import Distiller from lm_seqs_dataset import LmSeqsDataset from transformers import ( BertConfig, BertForMaskedLM, BertTokenizer, DistilBertConfig, DistilBertForMaskedLM, DistilBertTokenizer, GPTaConfig, GPTaLMHeadModel, GPTaTokenizer, RobertaConfig, RobertaForMaskedLM, RobertaTokenizer, ) from utils import git_log, init_gpu_params, logger, set_seed _snake_case = { "distilbert": (DistilBertConfig, DistilBertForMaskedLM, DistilBertTokenizer), "roberta": (RobertaConfig, RobertaForMaskedLM, RobertaTokenizer), "bert": (BertConfig, BertForMaskedLM, BertTokenizer), "gpt2": (GPTaConfig, GPTaLMHeadModel, GPTaTokenizer), } def lowerCAmelCase_ ( snake_case_ ): assert (args.mlm and args.alpha_mlm > 0.0) or (not args.mlm and args.alpha_mlm == 0.0) assert (args.alpha_mlm > 0.0 and args.alpha_clm == 0.0) or (args.alpha_mlm == 0.0 and args.alpha_clm > 0.0) if args.mlm: assert os.path.isfile(args.token_counts ) assert (args.student_type in ["roberta", "distilbert"]) and (args.teacher_type in ["roberta", "bert"]) else: assert (args.student_type in ["gpt2"]) and (args.teacher_type in ["gpt2"]) assert args.teacher_type == args.student_type or ( args.student_type == "distilbert" and args.teacher_type == "bert" ) assert os.path.isfile(args.student_config ) if args.student_pretrained_weights is not None: assert os.path.isfile(args.student_pretrained_weights ) if args.freeze_token_type_embds: assert args.student_type in ["roberta"] assert args.alpha_ce >= 0.0 assert args.alpha_mlm >= 0.0 assert args.alpha_clm >= 0.0 assert args.alpha_mse >= 0.0 assert args.alpha_cos >= 0.0 assert args.alpha_ce + args.alpha_mlm + args.alpha_clm + args.alpha_mse + args.alpha_cos > 0.0 def lowerCAmelCase_ ( snake_case_,snake_case_ ): if args.student_type == "roberta": _A : Optional[Any] = False elif args.student_type == "gpt2": _A : Optional[Any] = False def lowerCAmelCase_ ( snake_case_,snake_case_ ): if args.student_type == "roberta": _A : Optional[int] = False def lowerCAmelCase_ ( ): _A : Optional[int] = argparse.ArgumentParser(description="""Training""" ) parser.add_argument("""--force""",action="""store_true""",help="""Overwrite dump_path if it already exists.""" ) parser.add_argument( """--dump_path""",type=SCREAMING_SNAKE_CASE_,required=SCREAMING_SNAKE_CASE_,help="""The output directory (log, checkpoints, parameters, etc.)""" ) parser.add_argument( """--data_file""",type=SCREAMING_SNAKE_CASE_,required=SCREAMING_SNAKE_CASE_,help="""The binarized file (tokenized + tokens_to_ids) and grouped by sequence.""",) parser.add_argument( """--student_type""",type=SCREAMING_SNAKE_CASE_,choices=["""distilbert""", """roberta""", """gpt2"""],required=SCREAMING_SNAKE_CASE_,help="""The student type (DistilBERT, RoBERTa).""",) parser.add_argument("""--student_config""",type=SCREAMING_SNAKE_CASE_,required=SCREAMING_SNAKE_CASE_,help="""Path to the student configuration.""" ) parser.add_argument( """--student_pretrained_weights""",default=SCREAMING_SNAKE_CASE_,type=SCREAMING_SNAKE_CASE_,help="""Load student initialization checkpoint.""" ) parser.add_argument( """--teacher_type""",choices=["""bert""", """roberta""", """gpt2"""],required=SCREAMING_SNAKE_CASE_,help="""Teacher type (BERT, RoBERTa).""" ) parser.add_argument("""--teacher_name""",type=SCREAMING_SNAKE_CASE_,required=SCREAMING_SNAKE_CASE_,help="""The teacher model.""" ) parser.add_argument("""--temperature""",default=2.0,type=SCREAMING_SNAKE_CASE_,help="""Temperature for the softmax temperature.""" ) parser.add_argument( """--alpha_ce""",default=0.5,type=SCREAMING_SNAKE_CASE_,help="""Linear weight for the distillation loss. Must be >=0.""" ) parser.add_argument( """--alpha_mlm""",default=0.0,type=SCREAMING_SNAKE_CASE_,help="""Linear weight for the MLM loss. Must be >=0. Should be used in conjunction with `mlm` flag.""",) parser.add_argument("""--alpha_clm""",default=0.5,type=SCREAMING_SNAKE_CASE_,help="""Linear weight for the CLM loss. Must be >=0.""" ) parser.add_argument("""--alpha_mse""",default=0.0,type=SCREAMING_SNAKE_CASE_,help="""Linear weight of the MSE loss. Must be >=0.""" ) parser.add_argument( """--alpha_cos""",default=0.0,type=SCREAMING_SNAKE_CASE_,help="""Linear weight of the cosine embedding loss. Must be >=0.""" ) parser.add_argument( """--mlm""",action="""store_true""",help="""The LM step: MLM or CLM. If `mlm` is True, the MLM is used over CLM.""" ) parser.add_argument( """--mlm_mask_prop""",default=0.15,type=SCREAMING_SNAKE_CASE_,help="""Proportion of tokens for which we need to make a prediction.""",) parser.add_argument("""--word_mask""",default=0.8,type=SCREAMING_SNAKE_CASE_,help="""Proportion of tokens to mask out.""" ) parser.add_argument("""--word_keep""",default=0.1,type=SCREAMING_SNAKE_CASE_,help="""Proportion of tokens to keep.""" ) parser.add_argument("""--word_rand""",default=0.1,type=SCREAMING_SNAKE_CASE_,help="""Proportion of tokens to randomly replace.""" ) parser.add_argument( """--mlm_smoothing""",default=0.7,type=SCREAMING_SNAKE_CASE_,help="""Smoothing parameter to emphasize more rare tokens (see XLM, similar to word2vec).""",) parser.add_argument("""--token_counts""",type=SCREAMING_SNAKE_CASE_,help="""The token counts in the data_file for MLM.""" ) parser.add_argument( """--restrict_ce_to_mask""",action="""store_true""",help="""If true, compute the distillation loss only the [MLM] prediction distribution.""",) parser.add_argument( """--freeze_pos_embs""",action="""store_true""",help="""Freeze positional embeddings during distillation. For student_type in ['roberta', 'gpt2'] only.""",) parser.add_argument( """--freeze_token_type_embds""",action="""store_true""",help="""Freeze token type embeddings during distillation if existent. For student_type in ['roberta'] only.""",) parser.add_argument("""--n_epoch""",type=SCREAMING_SNAKE_CASE_,default=3,help="""Number of pass on the whole dataset.""" ) parser.add_argument("""--batch_size""",type=SCREAMING_SNAKE_CASE_,default=5,help="""Batch size (for each process).""" ) parser.add_argument( """--group_by_size""",action="""store_false""",help="""If true, group sequences that have similar length into the same batch. Default is true.""",) parser.add_argument( """--gradient_accumulation_steps""",type=SCREAMING_SNAKE_CASE_,default=50,help="""Gradient accumulation for larger training batches.""",) parser.add_argument("""--warmup_prop""",default=0.05,type=SCREAMING_SNAKE_CASE_,help="""Linear warmup proportion.""" ) parser.add_argument("""--weight_decay""",default=0.0,type=SCREAMING_SNAKE_CASE_,help="""Weight decay if we apply some.""" ) parser.add_argument("""--learning_rate""",default=5e-4,type=SCREAMING_SNAKE_CASE_,help="""The initial learning rate for Adam.""" ) parser.add_argument("""--adam_epsilon""",default=1e-6,type=SCREAMING_SNAKE_CASE_,help="""Epsilon for Adam optimizer.""" ) parser.add_argument("""--max_grad_norm""",default=5.0,type=SCREAMING_SNAKE_CASE_,help="""Max gradient norm.""" ) parser.add_argument("""--initializer_range""",default=0.02,type=SCREAMING_SNAKE_CASE_,help="""Random initialization range.""" ) parser.add_argument( """--fp16""",action="""store_true""",help="""Whether to use 16-bit (mixed) precision (through NVIDIA apex) instead of 32-bit""",) parser.add_argument( """--fp16_opt_level""",type=SCREAMING_SNAKE_CASE_,default="""O1""",help=( """For fp16: Apex AMP optimization level selected in ['O0', 'O1', 'O2', and 'O3'].""" """See details at https://nvidia.github.io/apex/amp.html""" ),) parser.add_argument("""--n_gpu""",type=SCREAMING_SNAKE_CASE_,default=1,help="""Number of GPUs in the node.""" ) parser.add_argument("""--local_rank""",type=SCREAMING_SNAKE_CASE_,default=-1,help="""Distributed training - Local rank""" ) parser.add_argument("""--seed""",type=SCREAMING_SNAKE_CASE_,default=56,help="""Random seed""" ) parser.add_argument("""--log_interval""",type=SCREAMING_SNAKE_CASE_,default=500,help="""Tensorboard logging interval.""" ) parser.add_argument("""--checkpoint_interval""",type=SCREAMING_SNAKE_CASE_,default=4000,help="""Checkpoint interval.""" ) _A : Dict = parser.parse_args() sanity_checks(SCREAMING_SNAKE_CASE_ ) # ARGS # init_gpu_params(SCREAMING_SNAKE_CASE_ ) set_seed(SCREAMING_SNAKE_CASE_ ) if args.is_master: if os.path.exists(args.dump_path ): if not args.force: raise ValueError( f'''Serialization dir {args.dump_path} already exists, but you have not precised wheter to overwrite''' """ itUse `--force` if you want to overwrite it""" ) else: shutil.rmtree(args.dump_path ) if not os.path.exists(args.dump_path ): os.makedirs(args.dump_path ) logger.info(f'''Experiment will be dumped and logged in {args.dump_path}''' ) # SAVE PARAMS # logger.info(f'''Param: {args}''' ) with open(os.path.join(args.dump_path,"""parameters.json""" ),"""w""" ) as f: json.dump(vars(SCREAMING_SNAKE_CASE_ ),SCREAMING_SNAKE_CASE_,indent=4 ) git_log(args.dump_path ) _A , _A , _A : Union[str, Any] = MODEL_CLASSES[args.student_type] _A , _A , _A : List[Any] = MODEL_CLASSES[args.teacher_type] # TOKENIZER # _A : Dict = teacher_tokenizer_class.from_pretrained(args.teacher_name ) _A : Dict = {} for tok_name, tok_symbol in tokenizer.special_tokens_map.items(): _A : List[str] = tokenizer.all_special_tokens.index(SCREAMING_SNAKE_CASE_ ) _A : Tuple = tokenizer.all_special_ids[idx] logger.info(f'''Special tokens {special_tok_ids}''' ) _A : int = special_tok_ids _A : str = tokenizer.max_model_input_sizes[args.teacher_name] # DATA LOADER # logger.info(f'''Loading data from {args.data_file}''' ) with open(args.data_file,"""rb""" ) as fp: _A : List[Any] = pickle.load(SCREAMING_SNAKE_CASE_ ) if args.mlm: logger.info(f'''Loading token counts from {args.token_counts} (already pre-computed)''' ) with open(args.token_counts,"""rb""" ) as fp: _A : List[str] = pickle.load(SCREAMING_SNAKE_CASE_ ) _A : str = np.maximum(SCREAMING_SNAKE_CASE_,1 ) ** -args.mlm_smoothing for idx in special_tok_ids.values(): _A : Dict = 0.0 # do not predict special tokens _A : int = torch.from_numpy(SCREAMING_SNAKE_CASE_ ) else: _A : Tuple = None _A : Tuple = LmSeqsDataset(params=SCREAMING_SNAKE_CASE_,data=SCREAMING_SNAKE_CASE_ ) logger.info("""Data loader created.""" ) # STUDENT # logger.info(f'''Loading student config from {args.student_config}''' ) _A : int = student_config_class.from_pretrained(args.student_config ) _A : Optional[int] = True if args.student_pretrained_weights is not None: logger.info(f'''Loading pretrained weights from {args.student_pretrained_weights}''' ) _A : Tuple = student_model_class.from_pretrained(args.student_pretrained_weights,config=SCREAMING_SNAKE_CASE_ ) else: _A : Tuple = student_model_class(SCREAMING_SNAKE_CASE_ ) if args.n_gpu > 0: student.to(f'''cuda:{args.local_rank}''' ) logger.info("""Student loaded.""" ) # TEACHER # _A : List[Any] = teacher_model_class.from_pretrained(args.teacher_name,output_hidden_states=SCREAMING_SNAKE_CASE_ ) if args.n_gpu > 0: teacher.to(f'''cuda:{args.local_rank}''' ) logger.info(f'''Teacher loaded from {args.teacher_name}.''' ) # FREEZING # if args.freeze_pos_embs: freeze_pos_embeddings(SCREAMING_SNAKE_CASE_,SCREAMING_SNAKE_CASE_ ) if args.freeze_token_type_embds: freeze_token_type_embeddings(SCREAMING_SNAKE_CASE_,SCREAMING_SNAKE_CASE_ ) # SANITY CHECKS # assert student.config.vocab_size == teacher.config.vocab_size assert student.config.hidden_size == teacher.config.hidden_size assert student.config.max_position_embeddings == teacher.config.max_position_embeddings if args.mlm: assert token_probs.size(0 ) == stu_architecture_config.vocab_size # DISTILLER # torch.cuda.empty_cache() _A : Optional[Any] = Distiller( params=SCREAMING_SNAKE_CASE_,dataset=SCREAMING_SNAKE_CASE_,token_probs=SCREAMING_SNAKE_CASE_,student=SCREAMING_SNAKE_CASE_,teacher=SCREAMING_SNAKE_CASE_ ) distiller.train() logger.info("""Let's go get some drinks.""" ) if __name__ == "__main__": main()
715
def lowerCAmelCase_ ( snake_case_ = 1000000 ): _A : Any = limit + 1 _A : Tuple = [0] * limit for first_term in range(1,snake_case_ ): for n in range(snake_case_,snake_case_,snake_case_ ): _A : Optional[int] = 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 _A : List[str] = sum(1 for x in frequency[1:limit] if x == 10 ) return count if __name__ == "__main__": print(f"""{solution() = }""")
54
0
import os import sys import unittest _snake_case : Dict = 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, ) _snake_case : Optional[int] = os.path.join("tests", "models", "bert", "test_modeling_bert.py") _snake_case : List[Any] = os.path.join("tests", "models", "blip", "test_modeling_blip.py") class lowercase ( unittest.TestCase ): def a__ ( self ) -> Tuple: _A : Optional[int] = get_test_to_tester_mapping(A_ ) _A : List[str] = get_test_to_tester_mapping(A_ ) _A : int = {"BertModelTest": "BertModelTester"} _A : List[Any] = { "BlipModelTest": "BlipModelTester", "BlipTextImageModelTest": "BlipTextImageModelsModelTester", "BlipTextModelTest": "BlipTextModelTester", "BlipTextRetrievalModelTest": "BlipTextRetrievalModelTester", "BlipVQAModelTest": "BlipVQAModelTester", "BlipVisionModelTest": "BlipVisionModelTester", } self.assertEqual(get_test_info.to_json(A_ ) , A_ ) self.assertEqual(get_test_info.to_json(A_ ) , A_ ) def a__ ( self ) -> int: _A : List[Any] = get_model_to_test_mapping(A_ ) _A : List[str] = get_model_to_test_mapping(A_ ) _A : List[str] = { "BertForMaskedLM": ["BertModelTest"], "BertForMultipleChoice": ["BertModelTest"], "BertForNextSentencePrediction": ["BertModelTest"], "BertForPreTraining": ["BertModelTest"], "BertForQuestionAnswering": ["BertModelTest"], "BertForSequenceClassification": ["BertModelTest"], "BertForTokenClassification": ["BertModelTest"], "BertLMHeadModel": ["BertModelTest"], "BertModel": ["BertModelTest"], } _A : int = { "BlipForConditionalGeneration": ["BlipTextImageModelTest"], "BlipForImageTextRetrieval": ["BlipTextRetrievalModelTest"], "BlipForQuestionAnswering": ["BlipVQAModelTest"], "BlipModel": ["BlipModelTest"], "BlipTextModel": ["BlipTextModelTest"], "BlipVisionModel": ["BlipVisionModelTest"], } self.assertEqual(get_test_info.to_json(A_ ) , A_ ) self.assertEqual(get_test_info.to_json(A_ ) , A_ ) def a__ ( self ) -> Dict: _A : List[str] = get_model_to_tester_mapping(A_ ) _A : Tuple = get_model_to_tester_mapping(A_ ) _A : Union[str, Any] = { "BertForMaskedLM": ["BertModelTester"], "BertForMultipleChoice": ["BertModelTester"], "BertForNextSentencePrediction": ["BertModelTester"], "BertForPreTraining": ["BertModelTester"], "BertForQuestionAnswering": ["BertModelTester"], "BertForSequenceClassification": ["BertModelTester"], "BertForTokenClassification": ["BertModelTester"], "BertLMHeadModel": ["BertModelTester"], "BertModel": ["BertModelTester"], } _A : Tuple = { "BlipForConditionalGeneration": ["BlipTextImageModelsModelTester"], "BlipForImageTextRetrieval": ["BlipTextRetrievalModelTester"], "BlipForQuestionAnswering": ["BlipVQAModelTester"], "BlipModel": ["BlipModelTester"], "BlipTextModel": ["BlipTextModelTester"], "BlipVisionModel": ["BlipVisionModelTester"], } self.assertEqual(get_test_info.to_json(A_ ) , A_ ) self.assertEqual(get_test_info.to_json(A_ ) , A_ )
716
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 lowercase ( tf.keras.layers.Layer ): def __init__( self , _a , _a , _a = None , _a = None ) -> Any: super().__init__() _A : Dict = pad_token_id _A : List[Any] = max_length _A : Optional[int] = vocab _A : Optional[int] = merges _A : Optional[int] = BytePairTokenizer(_a , _a , sequence_length=_a ) @classmethod def a__ ( cls , _a , *_a , **_a ) -> str: _A : Any = [""" """.join(_a ) for m in tokenizer.bpe_ranks.keys()] _A : str = tokenizer.get_vocab() return cls(_a , _a , *_a , **_a ) @classmethod def a__ ( cls , _a , *_a , **_a ) -> List[Any]: _A : Union[str, Any] = GPTaTokenizer.from_pretrained(_a , *_a , **_a ) return cls.from_tokenizer(_a , *_a , **_a ) @classmethod def a__ ( cls , _a ) -> Union[str, Any]: return cls(**_a ) def a__ ( self ) -> Union[str, Any]: return { "vocab": self.vocab, "merges": self.merges, "max_length": self.max_length, "pad_token_id": self.pad_token_id, } def a__ ( self , _a , _a = None ) -> int: _A : Optional[int] = self.tf_tokenizer(_a ) _A : Tuple = tf.ones_like(_a ) if self.pad_token_id is not None: # pad the tokens up to max length _A : Dict = max_length if max_length is not None else self.max_length if max_length is not None: _A , _A : Dict = pad_model_inputs( _a , max_seq_length=_a , pad_value=self.pad_token_id ) return {"attention_mask": attention_mask, "input_ids": input_ids}
54
0
def lowerCAmelCase_ ( snake_case_,snake_case_ ): _A : Dict = (boundary[1] - boundary[0]) / steps _A : List[Any] = boundary[0] _A : Union[str, Any] = boundary[1] _A : Optional[Any] = make_points(snake_case_,snake_case_,snake_case_ ) _A : Optional[int] = 0.0 y += (h / 2.0) * f(snake_case_ ) for i in x_i: # print(i) y += h * f(snake_case_ ) y += (h / 2.0) * f(snake_case_ ) return y def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_ ): _A : Dict = a + h while x < (b - h): yield x _A : str = x + h def lowerCAmelCase_ ( snake_case_ ): # enter your function here _A : Union[str, Any] = (x - 0) * (x - 0) return y def lowerCAmelCase_ ( ): _A : Tuple = 0.0 # Lower bound of integration _A : Dict = 1.0 # Upper bound of integration _A : Optional[Any] = 10.0 # define number of steps or resolution _A : Tuple = [a, b] # define boundary of integration _A : str = method_a(snake_case_,snake_case_ ) print(f'''y = {y}''' ) if __name__ == "__main__": main()
717
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 _snake_case = get_tests_dir("fixtures/test_sentencepiece_no_bos.model") @require_sentencepiece @require_tokenizers class lowercase ( UpperCamelCase__,unittest.TestCase ): _a = PegasusTokenizer _a = PegasusTokenizerFast _a = True _a = True def a__ ( self ) -> int: super().setUp() # We have a SentencePiece fixture for testing _A : List[Any] = PegasusTokenizer(_a ) tokenizer.save_pretrained(self.tmpdirname ) @cached_property def a__ ( self ) -> int: return PegasusTokenizer.from_pretrained("""google/pegasus-large""" ) def a__ ( self , **_a ) -> PegasusTokenizer: return PegasusTokenizer.from_pretrained(self.tmpdirname , **_a ) def a__ ( self , _a ) -> List[Any]: return ("This is a test", "This is a test") def a__ ( self ) -> int: _A : Dict = """</s>""" _A : Dict = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(_a ) , _a ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(_a ) , _a ) def a__ ( self ) -> Dict: _A : List[str] = 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(_a ) , 1103 ) def a__ ( self ) -> Optional[int]: self.assertEqual(self.get_tokenizer().vocab_size , 1103 ) def a__ ( self ) -> Tuple: _A : Any = self.rust_tokenizer_class.from_pretrained(self.tmpdirname ) _A : Optional[int] = self.tokenizer_class.from_pretrained(self.tmpdirname ) _A : int = ( """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 : Optional[int] = rust_tokenizer([raw_input_str] , return_tensors=_a , add_special_tokens=_a ).input_ids[0] _A : List[Any] = py_tokenizer([raw_input_str] , return_tensors=_a , add_special_tokens=_a ).input_ids[0] self.assertListEqual(_a , _a ) def a__ ( self ) -> Any: _A : str = self._large_tokenizer # <mask_1> masks whole sentence while <mask_2> masks single word _A : Optional[int] = """<mask_1> To ensure a <mask_2> flow of bank resolutions.""" _A : Union[str, Any] = [2, 413, 615, 114, 3, 1971, 113, 1679, 1_0710, 107, 1] _A : Union[str, Any] = tokenizer([raw_input_str] , return_tensors=_a ).input_ids[0] self.assertListEqual(_a , _a ) def a__ ( self ) -> List[str]: _A : Optional[int] = self._large_tokenizer # The tracebacks for the following asserts are **better** without messages or self.assertEqual assert tokenizer.vocab_size == 9_6103 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 : Any = """To ensure a smooth flow of bank resolutions.""" _A : Optional[int] = [413, 615, 114, 2291, 1971, 113, 1679, 1_0710, 107, 1] _A : Optional[Any] = tokenizer([raw_input_str] , return_tensors=_a ).input_ids[0] self.assertListEqual(_a , _a ) assert tokenizer.convert_ids_to_tokens([0, 1, 2, 3] ) == ["<pad>", "</s>", "<mask_1>", "<mask_2>"] @require_torch def a__ ( self ) -> List[str]: _A : Union[str, Any] = ["""This is going to be way too long.""" * 150, """short example"""] _A : Optional[Any] = ["""not super long but more than 5 tokens""", """tiny"""] _A : Union[str, Any] = self._large_tokenizer(_a , padding=_a , truncation=_a , return_tensors="""pt""" ) _A : Tuple = self._large_tokenizer( text_target=_a , max_length=5 , padding=_a , truncation=_a , 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(_a ) == 2 # input_ids, attention_mask. @slow def a__ ( self ) -> Optional[Any]: # fmt: off _A : List[Any] = {"""input_ids""": [[3_8979, 143, 1_8485, 606, 130, 2_6669, 8_7686, 121, 5_4189, 1129, 111, 2_6669, 8_7686, 121, 9114, 1_4787, 121, 1_3249, 158, 592, 956, 121, 1_4621, 3_1576, 143, 6_2613, 108, 9688, 930, 4_3430, 1_1562, 6_2613, 304, 108, 1_1443, 897, 108, 9314, 1_7415, 6_3399, 108, 1_1443, 7614, 1_8316, 118, 4284, 7148, 1_2430, 143, 1400, 2_5703, 158, 111, 4284, 7148, 1_1772, 143, 2_1297, 1064, 158, 122, 204, 3506, 1754, 1133, 1_4787, 1581, 115, 3_3224, 4482, 111, 1355, 110, 2_9173, 317, 5_0833, 108, 2_0147, 9_4665, 111, 7_7198, 107, 1], [110, 6_2613, 117, 638, 112, 1133, 121, 2_0098, 1355, 7_9050, 1_3872, 135, 1596, 5_3541, 1352, 141, 1_3039, 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, 1_8289, 1_7780, 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=_a , model_name="""google/bigbird-pegasus-large-arxiv""" , revision="""ba85d0851d708441f91440d509690f1ab6353415""" , ) @require_sentencepiece @require_tokenizers class lowercase ( UpperCamelCase__,unittest.TestCase ): _a = PegasusTokenizer _a = PegasusTokenizerFast _a = True _a = True def a__ ( self ) -> Optional[Any]: super().setUp() # We have a SentencePiece fixture for testing _A : Tuple = PegasusTokenizer(_a , offset=0 , mask_token_sent=_a , mask_token="""[MASK]""" ) tokenizer.save_pretrained(self.tmpdirname ) @cached_property def a__ ( self ) -> Optional[Any]: return PegasusTokenizer.from_pretrained("""google/bigbird-pegasus-large-arxiv""" ) def a__ ( self , **_a ) -> PegasusTokenizer: return PegasusTokenizer.from_pretrained(self.tmpdirname , **_a ) def a__ ( self , _a ) -> List[str]: return ("This is a test", "This is a test") def a__ ( self ) -> List[Any]: _A : List[Any] = self.rust_tokenizer_class.from_pretrained(self.tmpdirname ) _A : Dict = self.tokenizer_class.from_pretrained(self.tmpdirname ) _A : Dict = ( """Let's see which <unk> is the better <unk_token> one [MASK] It seems like this [MASK] was important </s>""" """ <pad> <pad> <pad>""" ) _A : Optional[Any] = rust_tokenizer([raw_input_str] , return_tensors=_a , add_special_tokens=_a ).input_ids[0] _A : int = py_tokenizer([raw_input_str] , return_tensors=_a , add_special_tokens=_a ).input_ids[0] self.assertListEqual(_a , _a ) @require_torch def a__ ( self ) -> Optional[int]: _A : Tuple = ["""This is going to be way too long.""" * 1000, """short example"""] _A : Optional[Any] = ["""not super long but more than 5 tokens""", """tiny"""] _A : Tuple = self._large_tokenizer(_a , padding=_a , truncation=_a , return_tensors="""pt""" ) _A : str = self._large_tokenizer( text_target=_a , max_length=5 , padding=_a , truncation=_a , 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(_a ) == 2 # input_ids, attention_mask. def a__ ( self ) -> Dict: _A : Optional[int] = ( """This is an example string that is used to test the original TF implementation against the HF""" """ implementation""" ) _A : Any = self._large_tokenizer(_a ).input_ids self.assertListEqual( _a , [182, 117, 142, 587, 4211, 120, 117, 263, 112, 804, 109, 856, 2_5016, 3137, 464, 109, 2_6955, 3137, 1] , )
54
0
import gc import inspect import unittest import torch from parameterized import parameterized from diffusers import PriorTransformer from diffusers.utils import floats_tensor, slow, torch_all_close, torch_device from diffusers.utils.testing_utils import enable_full_determinism from .test_modeling_common import ModelTesterMixin enable_full_determinism() class lowercase ( UpperCAmelCase__,unittest.TestCase ): _a = PriorTransformer _a = "hidden_states" @property def a__ ( self ) -> List[Any]: _A : List[str] = 4 _A : List[str] = 8 _A : int = 7 _A : Dict = floats_tensor((batch_size, embedding_dim) ).to(lowerCamelCase__ ) _A : Union[str, Any] = floats_tensor((batch_size, embedding_dim) ).to(lowerCamelCase__ ) _A : str = floats_tensor((batch_size, num_embeddings, embedding_dim) ).to(lowerCamelCase__ ) return { "hidden_states": hidden_states, "timestep": 2, "proj_embedding": proj_embedding, "encoder_hidden_states": encoder_hidden_states, } def a__ ( self , _a=0 ) -> Optional[int]: torch.manual_seed(lowerCamelCase__ ) _A : Union[str, Any] = 4 _A : Any = 8 _A : int = 7 _A : List[Any] = torch.randn((batch_size, embedding_dim) ).to(lowerCamelCase__ ) _A : Union[str, Any] = torch.randn((batch_size, embedding_dim) ).to(lowerCamelCase__ ) _A : str = torch.randn((batch_size, num_embeddings, embedding_dim) ).to(lowerCamelCase__ ) return { "hidden_states": hidden_states, "timestep": 2, "proj_embedding": proj_embedding, "encoder_hidden_states": encoder_hidden_states, } @property def a__ ( self ) -> Union[str, Any]: return (4, 8) @property def a__ ( self ) -> int: return (4, 8) def a__ ( self ) -> List[str]: _A : str = { "num_attention_heads": 2, "attention_head_dim": 4, "num_layers": 2, "embedding_dim": 8, "num_embeddings": 7, "additional_embeddings": 4, } _A : Union[str, Any] = self.dummy_input return init_dict, inputs_dict def a__ ( self ) -> Union[str, Any]: _A : Tuple = PriorTransformer.from_pretrained( """hf-internal-testing/prior-dummy""" , output_loading_info=lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) self.assertEqual(len(loading_info["""missing_keys"""] ) , 0 ) model.to(lowerCamelCase__ ) _A : Dict = model(**self.dummy_input )[0] assert hidden_states is not None, "Make sure output is not None" def a__ ( self ) -> List[Any]: _A : List[Any] = self.prepare_init_args_and_inputs_for_common() _A : Optional[int] = self.model_class(**lowerCamelCase__ ) _A : Tuple = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _A : Optional[int] = [*signature.parameters.keys()] _A : List[str] = ["hidden_states", "timestep"] self.assertListEqual(arg_names[:2] , lowerCamelCase__ ) def a__ ( self ) -> Union[str, Any]: _A : Optional[int] = PriorTransformer.from_pretrained("""hf-internal-testing/prior-dummy""" ) _A : Any = model.to(lowerCamelCase__ ) if hasattr(lowerCamelCase__ , """set_default_attn_processor""" ): model.set_default_attn_processor() _A : Any = self.get_dummy_seed_input() with torch.no_grad(): _A : str = model(**lowerCamelCase__ )[0] _A : int = output[0, :5].flatten().cpu() print(lowerCamelCase__ ) # Since the VAE Gaussian prior's generator is seeded on the appropriate device, # the expected output slices are not the same for CPU and GPU. _A : Dict = torch.tensor([-1.3436, -0.2870, 0.7538, 0.4368, -0.0239] ) self.assertTrue(torch_all_close(lowerCamelCase__ , lowerCamelCase__ , rtol=1e-2 ) ) @slow class lowercase ( unittest.TestCase ): def a__ ( self , _a=1 , _a=768 , _a=77 , _a=0 ) -> Union[str, Any]: torch.manual_seed(lowerCamelCase__ ) _A : Optional[int] = batch_size _A : Any = embedding_dim _A : int = num_embeddings _A : Tuple = torch.randn((batch_size, embedding_dim) ).to(lowerCamelCase__ ) _A : Any = torch.randn((batch_size, embedding_dim) ).to(lowerCamelCase__ ) _A : Dict = torch.randn((batch_size, num_embeddings, embedding_dim) ).to(lowerCamelCase__ ) return { "hidden_states": hidden_states, "timestep": 2, "proj_embedding": proj_embedding, "encoder_hidden_states": encoder_hidden_states, } def a__ ( self ) -> Tuple: super().tearDown() gc.collect() torch.cuda.empty_cache() @parameterized.expand( [ # fmt: off [13, [-0.5861, 0.1283, -0.0931, 0.0882, 0.4476, 0.1329, -0.0498, 0.0640]], [37, [-0.4913, 0.0110, -0.0483, 0.0541, 0.4954, -0.0170, 0.0354, 0.1651]], # fmt: on ] ) def a__ ( self , _a , _a ) -> int: _A : Any = PriorTransformer.from_pretrained("""kandinsky-community/kandinsky-2-1-prior""" , subfolder="""prior""" ) model.to(lowerCamelCase__ ) _A : List[Any] = self.get_dummy_seed_input(seed=lowerCamelCase__ ) with torch.no_grad(): _A : List[Any] = model(**lowerCamelCase__ )[0] assert list(sample.shape ) == [1, 768] _A : Optional[int] = sample[0, :8].flatten().cpu() print(lowerCamelCase__ ) _A : Tuple = torch.tensor(lowerCamelCase__ ) assert torch_all_close(lowerCamelCase__ , lowerCamelCase__ , atol=1e-3 )
718
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available, is_vision_available, ) _snake_case = { "configuration_efficientformer": [ "EFFICIENTFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", "EfficientFormerConfig", ] } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = ["EfficientFormerImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = [ "EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "EfficientFormerForImageClassification", "EfficientFormerForImageClassificationWithTeacher", "EfficientFormerModel", "EfficientFormerPreTrainedModel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = [ "TF_EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "TFEfficientFormerForImageClassification", "TFEfficientFormerForImageClassificationWithTeacher", "TFEfficientFormerModel", "TFEfficientFormerPreTrainedModel", ] if TYPE_CHECKING: from .configuration_efficientformer import EFFICIENTFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, EfficientFormerConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_efficientformer import EfficientFormerImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_efficientformer import ( EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, EfficientFormerForImageClassification, EfficientFormerForImageClassificationWithTeacher, EfficientFormerModel, EfficientFormerPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_efficientformer import ( TF_EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TFEfficientFormerForImageClassification, TFEfficientFormerForImageClassificationWithTeacher, TFEfficientFormerModel, TFEfficientFormerPreTrainedModel, ) else: import sys _snake_case = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
54
0
from __future__ import annotations _snake_case = 1.6021e-19 # units = C def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_,): if (conductivity, electron_conc, mobility).count(0 ) != 1: raise ValueError("""You cannot supply more or less than 2 values""" ) elif conductivity < 0: raise ValueError("""Conductivity cannot be negative""" ) elif electron_conc < 0: raise ValueError("""Electron concentration cannot be negative""" ) elif mobility < 0: raise ValueError("""mobility cannot be negative""" ) elif conductivity == 0: return ( "conductivity", mobility * electron_conc * ELECTRON_CHARGE, ) elif electron_conc == 0: return ( "electron_conc", conductivity / (mobility * ELECTRON_CHARGE), ) else: return ( "mobility", conductivity / (electron_conc * ELECTRON_CHARGE), ) if __name__ == "__main__": import doctest doctest.testmod()
719
from unittest import TestCase from datasets import Sequence, Value from datasets.arrow_dataset import Dataset class lowercase ( UpperCamelCase__ ): def a__ ( self ) -> Optional[int]: return [ {"col_1": 3, "col_2": "a"}, {"col_1": 2, "col_2": "b"}, {"col_1": 1, "col_2": "c"}, {"col_1": 0, "col_2": "d"}, ] def a__ ( self ) -> Optional[int]: _A : Union[str, Any] = {"""col_1""": [3, 2, 1, 0], """col_2""": ["""a""", """b""", """c""", """d"""]} return Dataset.from_dict(_a ) def a__ ( self ) -> Any: _A : str = self._create_example_records() _A : List[Any] = Dataset.from_list(_a ) self.assertListEqual(dset.column_names , ["""col_1""", """col_2"""] ) for i, r in enumerate(_a ): self.assertDictEqual(_a , example_records[i] ) def a__ ( self ) -> List[str]: _A : Dict = self._create_example_records() _A : List[str] = Dataset.from_list(_a ) _A : str = Dataset.from_dict({k: [r[k] for r in example_records] for k in example_records[0]} ) self.assertEqual(dset.info , dset_from_dict.info ) def a__ ( self ) -> str: # checks what happens with missing columns _A : List[str] = [{"""col_1""": 1}, {"""col_2""": """x"""}] _A : List[str] = Dataset.from_list(_a ) self.assertDictEqual(dset[0] , {"""col_1""": 1} ) self.assertDictEqual(dset[1] , {"""col_1""": None} ) # NB: first record is used for columns def a__ ( self ) -> Dict: # checks if the type can be inferred from the second record _A : List[Any] = [{"""col_1""": []}, {"""col_1""": [1, 2]}] _A : str = Dataset.from_list(_a ) self.assertEqual(dset.info.features["""col_1"""] , Sequence(Value("""int64""" ) ) ) def a__ ( self ) -> Dict: _A : List[str] = Dataset.from_list([] ) self.assertEqual(len(_a ) , 0 ) self.assertListEqual(dset.column_names , [] )
54
0
import unittest from transformers import AutoTokenizer, is_flax_available from transformers.testing_utils import require_flax, require_sentencepiece, require_tokenizers, slow if is_flax_available(): import jax.numpy as jnp from transformers import FlaxXLMRobertaModel @require_sentencepiece @require_tokenizers @require_flax class lowercase ( unittest.TestCase ): @slow def a__ ( self ) -> List[str]: _A : int = FlaxXLMRobertaModel.from_pretrained("""xlm-roberta-base""" ) _A : List[str] = AutoTokenizer.from_pretrained("""xlm-roberta-base""" ) _A : Optional[Any] = """The dog is cute and lives in the garden house""" _A : Optional[Any] = jnp.array([tokenizer.encode(__a )] ) _A : Tuple = (1, 12, 768) # batch_size, sequence_length, embedding_vector_dim _A : List[str] = jnp.array( [[-0.0101, 0.1218, -0.0803, 0.0801, 0.1327, 0.0776, -0.1215, 0.2383, 0.3338, 0.3106, 0.0300, 0.0252]] ) _A : List[Any] = model(__a )["""last_hidden_state"""] self.assertEqual(output.shape , __a ) # compare the actual values for a slice of last dim self.assertTrue(jnp.allclose(output[:, :, -1] , __a , atol=1e-3 ) )
720
from __future__ import annotations from collections.abc import Sequence from typing import Literal def lowerCAmelCase_ ( snake_case_,snake_case_ ): _A : List[str] = list(snake_case_ ) _A : List[Any] = list(snake_case_ ) _A : Tuple = 0 for i in range(len(snake_case_ ) ): if lista[i] != lista[i]: count += 1 _A : Optional[Any] = """_""" if count > 1: return False else: return "".join(snake_case_ ) def lowerCAmelCase_ ( snake_case_ ): _A : Optional[Any] = [] while True: _A : int = ["""$"""] * len(snake_case_ ) _A : Any = [] for i in range(len(snake_case_ ) ): for j in range(i + 1,len(snake_case_ ) ): _A : Tuple = compare_string(binary[i],binary[j] ) if k is False: _A : str = """*""" _A : str = """*""" temp.append("""X""" ) for i in range(len(snake_case_ ) ): if checka[i] == "$": pi.append(binary[i] ) if len(snake_case_ ) == 0: return pi _A : Dict = list(set(snake_case_ ) ) def lowerCAmelCase_ ( snake_case_,snake_case_ ): _A : List[str] = [] for minterm in minterms: _A : Tuple = """""" for _ in range(snake_case_ ): _A : Optional[Any] = str(minterm % 2 ) + string minterm //= 2 temp.append(snake_case_ ) return temp def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_ ): _A : Dict = list(snake_case_ ) _A : Tuple = list(snake_case_ ) _A : Dict = 0 for i in range(len(snake_case_ ) ): if lista[i] != lista[i]: count_n += 1 return count_n == count def lowerCAmelCase_ ( snake_case_,snake_case_ ): _A : Optional[int] = [] _A : str = [0] * len(snake_case_ ) for i in range(len(chart[0] ) ): _A : Union[str, Any] = 0 _A : Optional[Any] = -1 for j in range(len(snake_case_ ) ): if chart[j][i] == 1: count += 1 _A : Dict = j if count == 1: _A : int = 1 for i in range(len(snake_case_ ) ): if select[i] == 1: for j in range(len(chart[0] ) ): if chart[i][j] == 1: for k in range(len(snake_case_ ) ): _A : int = 0 temp.append(prime_implicants[i] ) while True: _A : Optional[Any] = 0 _A : Tuple = -1 _A : List[Any] = 0 for i in range(len(snake_case_ ) ): _A : List[str] = chart[i].count(1 ) if count_n > max_n: _A : Optional[int] = count_n _A : Tuple = i if max_n == 0: return temp temp.append(prime_implicants[rem] ) for i in range(len(chart[0] ) ): if chart[rem][i] == 1: for j in range(len(snake_case_ ) ): _A : Optional[int] = 0 def lowerCAmelCase_ ( snake_case_,snake_case_ ): _A : Optional[int] = [[0 for x in range(len(snake_case_ ) )] for x in range(len(snake_case_ ) )] for i in range(len(snake_case_ ) ): _A : List[Any] = prime_implicants[i].count("""_""" ) for j in range(len(snake_case_ ) ): if is_for_table(prime_implicants[i],binary[j],snake_case_ ): _A : Union[str, Any] = 1 return chart def lowerCAmelCase_ ( ): _A : Dict = int(input("""Enter the no. of variables\n""" ) ) _A : Dict = [ float(snake_case_ ) for x in input( """Enter the decimal representation of Minterms 'Spaces Separated'\n""" ).split() ] _A : int = decimal_to_binary(snake_case_,snake_case_ ) _A : Optional[Any] = check(snake_case_ ) print("""Prime Implicants are:""" ) print(snake_case_ ) _A : int = prime_implicant_chart(snake_case_,snake_case_ ) _A : int = selection(snake_case_,snake_case_ ) print("""Essential Prime Implicants are:""" ) print(snake_case_ ) if __name__ == "__main__": import doctest doctest.testmod() main()
54
0
from typing import List, Optional, Union from ...configuration_utils import PretrainedConfig from ...utils import logging _snake_case = logging.get_logger(__name__) _snake_case = { "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 lowercase ( UpperCamelCase__ ): _a = "time_series_transformer" _a = { "hidden_size": "d_model", "num_attention_heads": "encoder_attention_heads", "num_hidden_layers": "encoder_layers", } def __init__( self , _a = None , _a = None , _a = "student_t" , _a = "nll" , _a = 1 , _a = [1, 2, 3, 4, 5, 6, 7] , _a = "mean" , _a = 0 , _a = 0 , _a = 0 , _a = 0 , _a = None , _a = None , _a = 32 , _a = 32 , _a = 2 , _a = 2 , _a = 2 , _a = 2 , _a = True , _a = "gelu" , _a = 64 , _a = 0.1 , _a = 0.1 , _a = 0.1 , _a = 0.1 , _a = 0.1 , _a = 100 , _a = 0.02 , _a=True , **_a , ) -> Optional[int]: # time series specific configuration _A : int = prediction_length _A : Optional[Any] = context_length or prediction_length _A : Optional[int] = distribution_output _A : List[str] = loss _A : Dict = input_size _A : Optional[int] = num_time_features _A : str = lags_sequence _A : Any = scaling _A : int = num_dynamic_real_features _A : Optional[Any] = num_static_real_features _A : List[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`""" ) _A : str = cardinality else: _A : Optional[int] = [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`""" ) _A : Dict = embedding_dimension else: _A : str = [min(50 , (cat + 1) // 2 ) for cat in self.cardinality] _A : int = num_parallel_samples # Transformer architecture configuration _A : List[str] = input_size * len(UpperCAmelCase__ ) + self._number_of_features _A : Any = d_model _A : List[Any] = encoder_attention_heads _A : Union[str, Any] = decoder_attention_heads _A : int = encoder_ffn_dim _A : Optional[int] = decoder_ffn_dim _A : int = encoder_layers _A : str = decoder_layers _A : Dict = dropout _A : Any = attention_dropout _A : Any = activation_dropout _A : List[Any] = encoder_layerdrop _A : Optional[Any] = decoder_layerdrop _A : Dict = activation_function _A : Tuple = init_std _A : Union[str, Any] = use_cache super().__init__(is_encoder_decoder=UpperCAmelCase__ , **UpperCAmelCase__ ) @property def a__ ( self ) -> 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 )
721
from __future__ import annotations def lowerCAmelCase_ ( snake_case_,snake_case_ = None ): _A : Tuple = word_bank or [] # create a table _A : int = len(snake_case_ ) + 1 _A : list[list[list[str]]] = [] for _ in range(snake_case_ ): table.append([] ) # seed value _A : Dict = [[]] # because empty string has empty combination # iterate through the indices for i in range(snake_case_ ): # condition if table[i] != []: for word in word_bank: # slice condition if target[i : i + len(snake_case_ )] == word: _A : list[list[str]] = [ [word, *way] for way in table[i] ] # adds the word to every combination the current position holds # now,push that combination to the table[i+len(word)] table[i + len(snake_case_ )] += new_combinations # combinations are in reverse order so reverse for better output for combination in table[len(snake_case_ )]: combination.reverse() return table[len(snake_case_ )] if __name__ == "__main__": print(all_construct("jwajalapa", ["jwa", "j", "w", "a", "la", "lapa"])) print(all_construct("rajamati", ["s", "raj", "amat", "raja", "ma", "i", "t"])) print( all_construct( "hexagonosaurus", ["h", "ex", "hex", "ag", "ago", "ru", "auru", "rus", "go", "no", "o", "s"], ) )
54
0
from __future__ import annotations def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_,snake_case_,snake_case_,): _A : List[str] = len(snake_case_ ) # If row is equal to the size of the board it means there are a queen in each row in # the current board (possible_board) if row == n: # We convert the variable possible_board that looks like this: [1, 3, 0, 2] to # this: ['. Q . . ', '. . . Q ', 'Q . . . ', '. . Q . '] boards.append([""". """ * i + """Q """ + """. """ * (n - 1 - i) for i in possible_board] ) return # We iterate each column in the row to find all possible results in each row for col in range(snake_case_ ): # We apply that we learned previously. First we check that in the current board # (possible_board) there are not other same value because if there is it means # that there are a collision in vertical. Then we apply the two formulas we # learned before: # # 45º: y - x = b or 45: row - col = b # 135º: y + x = b or row + col = b. # # And we verify if the results of this two formulas not exist in their variables # respectively. (diagonal_right_collisions, diagonal_left_collisions) # # If any or these are True it means there is a collision so we continue to the # next value in the for loop. if ( col in possible_board or row - col in diagonal_right_collisions or row + col in diagonal_left_collisions ): continue # If it is False we call dfs function again and we update the inputs depth_first_search( [*possible_board, col],[*diagonal_right_collisions, row - col],[*diagonal_left_collisions, row + col],snake_case_,snake_case_,) def lowerCAmelCase_ ( snake_case_ ): _A : Optional[int] = [] depth_first_search([],[],[],snake_case_,snake_case_ ) # Print all the boards for board in boards: for column in board: print(snake_case_ ) print("""""" ) print(len(snake_case_ ),"""solutions were found.""" ) if __name__ == "__main__": import doctest doctest.testmod() n_queens_solution(4)
700
import operator def lowerCAmelCase_ ( snake_case_,snake_case_ = False,snake_case_ = None ): _A : str = operator.lt if reverse else operator.gt _A : Optional[Any] = solution or [] if not arr: return solution _A : Dict = [arr.pop(0 )] for i, item in enumerate(snake_case_ ): if _operator(snake_case_,sublist[-1] ): sublist.append(snake_case_ ) arr.pop(snake_case_ ) # merging sublist into solution list if not solution: solution.extend(snake_case_ ) else: while sublist: _A : Union[str, Any] = sublist.pop(0 ) for i, xx in enumerate(snake_case_ ): if not _operator(snake_case_,snake_case_ ): solution.insert(snake_case_,snake_case_ ) break else: solution.append(snake_case_ ) strand_sort(snake_case_,snake_case_,snake_case_ ) return solution if __name__ == "__main__": assert strand_sort([4, 3, 5, 1, 2]) == [1, 2, 3, 4, 5] assert strand_sort([4, 3, 5, 1, 2], reverse=True) == [5, 4, 3, 2, 1]
54
0
import argparse import json import os import numpy as np import PIL import requests import tensorflow.keras.applications.efficientnet as efficientnet import torch from huggingface_hub import hf_hub_download from PIL import Image from tensorflow.keras.preprocessing import image from transformers import ( EfficientNetConfig, EfficientNetForImageClassification, EfficientNetImageProcessor, ) from transformers.utils import logging logging.set_verbosity_info() _snake_case = logging.get_logger(__name__) _snake_case = { "b0": efficientnet.EfficientNetBa, "b1": efficientnet.EfficientNetBa, "b2": efficientnet.EfficientNetBa, "b3": efficientnet.EfficientNetBa, "b4": efficientnet.EfficientNetBa, "b5": efficientnet.EfficientNetBa, "b6": efficientnet.EfficientNetBa, "b7": efficientnet.EfficientNetBa, } _snake_case = { "b0": { "hidden_dim": 1280, "width_coef": 1.0, "depth_coef": 1.0, "image_size": 224, "dropout_rate": 0.2, "dw_padding": [], }, "b1": { "hidden_dim": 1280, "width_coef": 1.0, "depth_coef": 1.1, "image_size": 240, "dropout_rate": 0.2, "dw_padding": [16], }, "b2": { "hidden_dim": 1408, "width_coef": 1.1, "depth_coef": 1.2, "image_size": 260, "dropout_rate": 0.3, "dw_padding": [5, 8, 16], }, "b3": { "hidden_dim": 1536, "width_coef": 1.2, "depth_coef": 1.4, "image_size": 300, "dropout_rate": 0.3, "dw_padding": [5, 18], }, "b4": { "hidden_dim": 1792, "width_coef": 1.4, "depth_coef": 1.8, "image_size": 380, "dropout_rate": 0.4, "dw_padding": [6], }, "b5": { "hidden_dim": 2048, "width_coef": 1.6, "depth_coef": 2.2, "image_size": 456, "dropout_rate": 0.4, "dw_padding": [13, 27], }, "b6": { "hidden_dim": 2304, "width_coef": 1.8, "depth_coef": 2.6, "image_size": 528, "dropout_rate": 0.5, "dw_padding": [31], }, "b7": { "hidden_dim": 2560, "width_coef": 2.0, "depth_coef": 3.1, "image_size": 600, "dropout_rate": 0.5, "dw_padding": [18], }, } def lowerCAmelCase_ ( snake_case_ ): _A : Optional[int] = EfficientNetConfig() _A : Any = CONFIG_MAP[model_name]["""hidden_dim"""] _A : Dict = CONFIG_MAP[model_name]["""width_coef"""] _A : Tuple = CONFIG_MAP[model_name]["""depth_coef"""] _A : int = CONFIG_MAP[model_name]["""image_size"""] _A : str = CONFIG_MAP[model_name]["""dropout_rate"""] _A : Union[str, Any] = CONFIG_MAP[model_name]["""dw_padding"""] _A : Optional[int] = """huggingface/label-files""" _A : Optional[int] = """imagenet-1k-id2label.json""" _A : Union[str, Any] = 1000 _A : Dict = json.load(open(hf_hub_download(_SCREAMING_SNAKE_CASE,_SCREAMING_SNAKE_CASE,repo_type="""dataset""" ),"""r""" ) ) _A : Union[str, Any] = {int(_SCREAMING_SNAKE_CASE ): v for k, v in idalabel.items()} _A : Union[str, Any] = idalabel _A : Optional[Any] = {v: k for k, v in idalabel.items()} return config def lowerCAmelCase_ ( ): _A : Any = """http://images.cocodataset.org/val2017/000000039769.jpg""" _A : str = Image.open(requests.get(_SCREAMING_SNAKE_CASE,stream=_SCREAMING_SNAKE_CASE ).raw ) return im def lowerCAmelCase_ ( snake_case_ ): _A : Optional[int] = CONFIG_MAP[model_name]["""image_size"""] _A : Optional[Any] = EfficientNetImageProcessor( size={"""height""": size, """width""": size},image_mean=[0.4_85, 0.4_56, 0.4_06],image_std=[0.47_85_39_44, 0.4_73_28_64, 0.47_43_41_63],do_center_crop=_SCREAMING_SNAKE_CASE,) return preprocessor def lowerCAmelCase_ ( snake_case_ ): _A : Optional[int] = [v.split("""_""" )[0].split("""block""" )[1] for v in original_param_names if v.startswith("""block""" )] _A : Optional[Any] = sorted(set(_SCREAMING_SNAKE_CASE ) ) _A : Dict = len(_SCREAMING_SNAKE_CASE ) _A : Optional[int] = {b: str(_SCREAMING_SNAKE_CASE ) for b, i in zip(_SCREAMING_SNAKE_CASE,range(_SCREAMING_SNAKE_CASE ) )} _A : Optional[int] = [] rename_keys.append(("""stem_conv/kernel:0""", """embeddings.convolution.weight""") ) rename_keys.append(("""stem_bn/gamma:0""", """embeddings.batchnorm.weight""") ) rename_keys.append(("""stem_bn/beta:0""", """embeddings.batchnorm.bias""") ) rename_keys.append(("""stem_bn/moving_mean:0""", """embeddings.batchnorm.running_mean""") ) rename_keys.append(("""stem_bn/moving_variance:0""", """embeddings.batchnorm.running_var""") ) for b in block_names: _A : List[Any] = block_name_mapping[b] rename_keys.append((f'''block{b}_expand_conv/kernel:0''', f'''encoder.blocks.{hf_b}.expansion.expand_conv.weight''') ) rename_keys.append((f'''block{b}_expand_bn/gamma:0''', f'''encoder.blocks.{hf_b}.expansion.expand_bn.weight''') ) rename_keys.append((f'''block{b}_expand_bn/beta:0''', f'''encoder.blocks.{hf_b}.expansion.expand_bn.bias''') ) rename_keys.append( (f'''block{b}_expand_bn/moving_mean:0''', f'''encoder.blocks.{hf_b}.expansion.expand_bn.running_mean''') ) rename_keys.append( (f'''block{b}_expand_bn/moving_variance:0''', f'''encoder.blocks.{hf_b}.expansion.expand_bn.running_var''') ) rename_keys.append( (f'''block{b}_dwconv/depthwise_kernel:0''', f'''encoder.blocks.{hf_b}.depthwise_conv.depthwise_conv.weight''') ) rename_keys.append((f'''block{b}_bn/gamma:0''', f'''encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.weight''') ) rename_keys.append((f'''block{b}_bn/beta:0''', f'''encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.bias''') ) rename_keys.append( (f'''block{b}_bn/moving_mean:0''', f'''encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.running_mean''') ) rename_keys.append( (f'''block{b}_bn/moving_variance:0''', f'''encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.running_var''') ) rename_keys.append((f'''block{b}_se_reduce/kernel:0''', f'''encoder.blocks.{hf_b}.squeeze_excite.reduce.weight''') ) rename_keys.append((f'''block{b}_se_reduce/bias:0''', f'''encoder.blocks.{hf_b}.squeeze_excite.reduce.bias''') ) rename_keys.append((f'''block{b}_se_expand/kernel:0''', f'''encoder.blocks.{hf_b}.squeeze_excite.expand.weight''') ) rename_keys.append((f'''block{b}_se_expand/bias:0''', f'''encoder.blocks.{hf_b}.squeeze_excite.expand.bias''') ) rename_keys.append( (f'''block{b}_project_conv/kernel:0''', f'''encoder.blocks.{hf_b}.projection.project_conv.weight''') ) rename_keys.append((f'''block{b}_project_bn/gamma:0''', f'''encoder.blocks.{hf_b}.projection.project_bn.weight''') ) rename_keys.append((f'''block{b}_project_bn/beta:0''', f'''encoder.blocks.{hf_b}.projection.project_bn.bias''') ) rename_keys.append( (f'''block{b}_project_bn/moving_mean:0''', f'''encoder.blocks.{hf_b}.projection.project_bn.running_mean''') ) rename_keys.append( (f'''block{b}_project_bn/moving_variance:0''', f'''encoder.blocks.{hf_b}.projection.project_bn.running_var''') ) rename_keys.append(("""top_conv/kernel:0""", """encoder.top_conv.weight""") ) rename_keys.append(("""top_bn/gamma:0""", """encoder.top_bn.weight""") ) rename_keys.append(("""top_bn/beta:0""", """encoder.top_bn.bias""") ) rename_keys.append(("""top_bn/moving_mean:0""", """encoder.top_bn.running_mean""") ) rename_keys.append(("""top_bn/moving_variance:0""", """encoder.top_bn.running_var""") ) _A : int = {} for item in rename_keys: if item[0] in original_param_names: _A : Any = """efficientnet.""" + item[1] _A : List[str] = """classifier.weight""" _A : Tuple = """classifier.bias""" return key_mapping def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_ ): for key, value in tf_params.items(): if "normalization" in key: continue _A : List[str] = key_mapping[key] if "_conv" in key and "kernel" in key: _A : List[str] = torch.from_numpy(_SCREAMING_SNAKE_CASE ).permute(3,2,0,1 ) elif "depthwise_kernel" in key: _A : List[str] = torch.from_numpy(_SCREAMING_SNAKE_CASE ).permute(2,3,0,1 ) elif "kernel" in key: _A : int = torch.from_numpy(np.transpose(_SCREAMING_SNAKE_CASE ) ) else: _A : Optional[Any] = torch.from_numpy(_SCREAMING_SNAKE_CASE ) # Replace HF parameters with original TF model parameters assert hf_params[hf_key].shape == new_hf_value.shape hf_params[hf_key].copy_(_SCREAMING_SNAKE_CASE ) @torch.no_grad() def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_,snake_case_ ): _A : Optional[Any] = model_classes[model_name]( include_top=_SCREAMING_SNAKE_CASE,weights="""imagenet""",input_tensor=_SCREAMING_SNAKE_CASE,input_shape=_SCREAMING_SNAKE_CASE,pooling=_SCREAMING_SNAKE_CASE,classes=1000,classifier_activation="""softmax""",) _A : str = original_model.trainable_variables _A : Dict = original_model.non_trainable_variables _A : Union[str, Any] = {param.name: param.numpy() for param in tf_params} for param in tf_non_train_params: _A : Optional[Any] = param.numpy() _A : Tuple = list(tf_params.keys() ) # Load HuggingFace model _A : int = get_efficientnet_config(_SCREAMING_SNAKE_CASE ) _A : Dict = EfficientNetForImageClassification(_SCREAMING_SNAKE_CASE ).eval() _A : Any = hf_model.state_dict() # Create src-to-dst parameter name mapping dictionary print("""Converting parameters...""" ) _A : Union[str, Any] = rename_keys(_SCREAMING_SNAKE_CASE ) replace_params(_SCREAMING_SNAKE_CASE,_SCREAMING_SNAKE_CASE,_SCREAMING_SNAKE_CASE ) # Initialize preprocessor and preprocess input image _A : List[str] = convert_image_processor(_SCREAMING_SNAKE_CASE ) _A : Optional[Any] = preprocessor(images=prepare_img(),return_tensors="""pt""" ) # HF model inference hf_model.eval() with torch.no_grad(): _A : List[str] = hf_model(**_SCREAMING_SNAKE_CASE ) _A : Dict = outputs.logits.detach().numpy() # Original model inference _A : Tuple = False _A : Tuple = CONFIG_MAP[model_name]["""image_size"""] _A : str = prepare_img().resize((image_size, image_size),resample=PIL.Image.NEAREST ) _A : Any = image.img_to_array(_SCREAMING_SNAKE_CASE ) _A : Dict = np.expand_dims(_SCREAMING_SNAKE_CASE,axis=0 ) _A : Any = original_model.predict(_SCREAMING_SNAKE_CASE ) # Check whether original and HF model outputs match -> np.allclose assert np.allclose(_SCREAMING_SNAKE_CASE,_SCREAMING_SNAKE_CASE,atol=1e-3 ), "The predicted logits are not the same." print("""Model outputs match!""" ) if save_model: # Create folder to save model if not os.path.isdir(_SCREAMING_SNAKE_CASE ): os.mkdir(_SCREAMING_SNAKE_CASE ) # Save converted model and image processor hf_model.save_pretrained(_SCREAMING_SNAKE_CASE ) preprocessor.save_pretrained(_SCREAMING_SNAKE_CASE ) if push_to_hub: # Push model and image processor to hub print(f'''Pushing converted {model_name} to the hub...''' ) _A : Tuple = f'''efficientnet-{model_name}''' preprocessor.push_to_hub(_SCREAMING_SNAKE_CASE ) hf_model.push_to_hub(_SCREAMING_SNAKE_CASE ) if __name__ == "__main__": _snake_case = argparse.ArgumentParser() # Required parameters parser.add_argument( "--model_name", default="b0", type=str, help="Version name of the EfficientNet model you want to convert, select from [b0, b1, b2, b3, b4, b5, b6, b7].", ) parser.add_argument( "--pytorch_dump_folder_path", default="hf_model", type=str, help="Path to the output PyTorch model directory.", ) parser.add_argument("--save_model", action="store_true", help="Save model to local") parser.add_argument("--push_to_hub", action="store_true", help="Push model and image processor to the hub") _snake_case = parser.parse_args() convert_efficientnet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.save_model, args.push_to_hub)
701
import gc import unittest from transformers import MODEL_FOR_MASKED_LM_MAPPING, TF_MODEL_FOR_MASKED_LM_MAPPING, FillMaskPipeline, pipeline from transformers.pipelines import PipelineException from transformers.testing_utils import ( is_pipeline_test, is_torch_available, nested_simplify, require_tf, require_torch, require_torch_gpu, slow, ) from .test_pipelines_common import ANY @is_pipeline_test class lowercase ( unittest.TestCase ): _a = MODEL_FOR_MASKED_LM_MAPPING _a = TF_MODEL_FOR_MASKED_LM_MAPPING def a__ ( self ) -> Tuple: super().tearDown() # clean-up as much as possible GPU memory occupied by PyTorch gc.collect() if is_torch_available(): import torch torch.cuda.empty_cache() @require_tf def a__ ( self ) -> Any: _A : Optional[Any] = pipeline(task="""fill-mask""" , model="""sshleifer/tiny-distilroberta-base""" , top_k=2 , framework="""tf""" ) _A : Optional[int] = unmasker("""My name is <mask>""" ) self.assertEqual( nested_simplify(_a , decimals=6 ) , [ {"""sequence""": """My name is grouped""", """score""": 2.1e-05, """token""": 3_8015, """token_str""": """ grouped"""}, {"""sequence""": """My name is accuser""", """score""": 2.1e-05, """token""": 2_5506, """token_str""": """ accuser"""}, ] , ) _A : Tuple = unmasker("""The largest city in France is <mask>""" ) self.assertEqual( nested_simplify(_a , decimals=6 ) , [ { """sequence""": """The largest city in France is grouped""", """score""": 2.1e-05, """token""": 3_8015, """token_str""": """ grouped""", }, { """sequence""": """The largest city in France is accuser""", """score""": 2.1e-05, """token""": 2_5506, """token_str""": """ accuser""", }, ] , ) _A : List[str] = unmasker("""My name is <mask>""" , targets=[""" Patrick""", """ Clara""", """ Teven"""] , top_k=3 ) self.assertEqual( nested_simplify(_a , decimals=6 ) , [ {"""sequence""": """My name is Clara""", """score""": 2e-05, """token""": 1_3606, """token_str""": """ Clara"""}, {"""sequence""": """My name is Patrick""", """score""": 2e-05, """token""": 3499, """token_str""": """ Patrick"""}, {"""sequence""": """My name is Te""", """score""": 1.9e-05, """token""": 2941, """token_str""": """ Te"""}, ] , ) @require_torch def a__ ( self ) -> str: _A : Any = pipeline(task="""fill-mask""" , model="""sshleifer/tiny-distilroberta-base""" , top_k=2 , framework="""pt""" ) _A : List[Any] = unmasker("""My name is <mask>""" ) self.assertEqual( nested_simplify(_a , decimals=6 ) , [ {"""sequence""": """My name is Maul""", """score""": 2.2e-05, """token""": 3_5676, """token_str""": """ Maul"""}, {"""sequence""": """My name isELS""", """score""": 2.2e-05, """token""": 1_6416, """token_str""": """ELS"""}, ] , ) _A : Optional[Any] = unmasker("""The largest city in France is <mask>""" ) self.assertEqual( nested_simplify(_a , decimals=6 ) , [ { """sequence""": """The largest city in France is Maul""", """score""": 2.2e-05, """token""": 3_5676, """token_str""": """ Maul""", }, {"""sequence""": """The largest city in France isELS""", """score""": 2.2e-05, """token""": 1_6416, """token_str""": """ELS"""}, ] , ) _A : Optional[int] = unmasker("""My name is <mask>""" , targets=[""" Patrick""", """ Clara""", """ Teven"""] , top_k=3 ) self.assertEqual( nested_simplify(_a , decimals=6 ) , [ {"""sequence""": """My name is Patrick""", """score""": 2.1e-05, """token""": 3499, """token_str""": """ Patrick"""}, {"""sequence""": """My name is Te""", """score""": 2e-05, """token""": 2941, """token_str""": """ Te"""}, {"""sequence""": """My name is Clara""", """score""": 2e-05, """token""": 1_3606, """token_str""": """ Clara"""}, ] , ) _A : str = unmasker("""My name is <mask> <mask>""" , top_k=2 ) self.assertEqual( nested_simplify(_a , decimals=6 ) , [ [ { """score""": 2.2e-05, """token""": 3_5676, """token_str""": """ Maul""", """sequence""": """<s>My name is Maul<mask></s>""", }, {"""score""": 2.2e-05, """token""": 1_6416, """token_str""": """ELS""", """sequence""": """<s>My name isELS<mask></s>"""}, ], [ { """score""": 2.2e-05, """token""": 3_5676, """token_str""": """ Maul""", """sequence""": """<s>My name is<mask> Maul</s>""", }, {"""score""": 2.2e-05, """token""": 1_6416, """token_str""": """ELS""", """sequence""": """<s>My name is<mask>ELS</s>"""}, ], ] , ) @require_torch_gpu def a__ ( self ) -> Union[str, Any]: _A : int = pipeline("""fill-mask""" , model="""hf-internal-testing/tiny-random-distilbert""" , device=0 , framework="""pt""" ) # convert model to fp16 pipe.model.half() _A : Optional[Any] = pipe("""Paris is the [MASK] of France.""" ) # We actually don't care about the result, we just want to make sure # it works, meaning the float16 tensor got casted back to float32 # for postprocessing. self.assertIsInstance(_a , _a ) @slow @require_torch def a__ ( self ) -> Optional[int]: _A : Optional[Any] = pipeline(task="""fill-mask""" , model="""distilroberta-base""" , top_k=2 , framework="""pt""" ) self.run_large_test(_a ) @slow @require_tf def a__ ( self ) -> Tuple: _A : str = pipeline(task="""fill-mask""" , model="""distilroberta-base""" , top_k=2 , framework="""tf""" ) self.run_large_test(_a ) def a__ ( self , _a ) -> Tuple: _A : Optional[int] = unmasker("""My name is <mask>""" ) self.assertEqual( nested_simplify(_a ) , [ {"""sequence""": """My name is John""", """score""": 0.008, """token""": 610, """token_str""": """ John"""}, {"""sequence""": """My name is Chris""", """score""": 0.007, """token""": 1573, """token_str""": """ Chris"""}, ] , ) _A : int = unmasker("""The largest city in France is <mask>""" ) self.assertEqual( nested_simplify(_a ) , [ { """sequence""": """The largest city in France is Paris""", """score""": 0.251, """token""": 2201, """token_str""": """ Paris""", }, { """sequence""": """The largest city in France is Lyon""", """score""": 0.214, """token""": 1_2790, """token_str""": """ Lyon""", }, ] , ) _A : Optional[Any] = unmasker("""My name is <mask>""" , targets=[""" Patrick""", """ Clara""", """ Teven"""] , top_k=3 ) self.assertEqual( nested_simplify(_a ) , [ {"""sequence""": """My name is Patrick""", """score""": 0.005, """token""": 3499, """token_str""": """ Patrick"""}, {"""sequence""": """My name is Clara""", """score""": 0.000, """token""": 1_3606, """token_str""": """ Clara"""}, {"""sequence""": """My name is Te""", """score""": 0.000, """token""": 2941, """token_str""": """ Te"""}, ] , ) @require_torch def a__ ( self ) -> Tuple: _A : List[str] = pipeline(task="""fill-mask""" , model="""sshleifer/tiny-distilroberta-base""" , framework="""pt""" ) _A : str = None _A : Union[str, Any] = None self.run_pipeline_test(_a , [] ) @require_tf def a__ ( self ) -> Union[str, Any]: _A : Tuple = pipeline(task="""fill-mask""" , model="""sshleifer/tiny-distilroberta-base""" , framework="""tf""" ) _A : Any = None _A : Dict = None self.run_pipeline_test(_a , [] ) def a__ ( self , _a , _a , _a ) -> Any: if tokenizer is None or tokenizer.mask_token_id is None: self.skipTest("""The provided tokenizer has no mask token, (probably reformer or wav2vec2)""" ) _A : Optional[Any] = FillMaskPipeline(model=_a , tokenizer=_a ) _A : Tuple = [ F'''This is another {tokenizer.mask_token} test''', ] return fill_masker, examples def a__ ( self , _a , _a ) -> Dict: _A : Dict = fill_masker.tokenizer _A : List[str] = fill_masker.model _A : List[str] = fill_masker( F'''This is a {tokenizer.mask_token}''' , ) self.assertEqual( _a , [ {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, ] , ) _A : Optional[Any] = fill_masker([F'''This is a {tokenizer.mask_token}'''] ) self.assertEqual( _a , [ {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, ] , ) _A : List[str] = fill_masker([F'''This is a {tokenizer.mask_token}''', F'''Another {tokenizer.mask_token} great test.'''] ) self.assertEqual( _a , [ [ {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, ], [ {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, ], ] , ) with self.assertRaises(_a ): fill_masker([None] ) # No mask_token is not supported with self.assertRaises(_a ): fill_masker("""This is""" ) self.run_test_top_k(_a , _a ) self.run_test_targets(_a , _a ) self.run_test_top_k_targets(_a , _a ) self.fill_mask_with_duplicate_targets_and_top_k(_a , _a ) self.fill_mask_with_multiple_masks(_a , _a ) def a__ ( self , _a , _a ) -> List[str]: _A : int = tokenizer.get_vocab() _A : str = sorted(vocab.keys() )[:2] # Pipeline argument _A : Tuple = FillMaskPipeline(model=_a , tokenizer=_a , targets=_a ) _A : Optional[int] = fill_masker(F'''This is a {tokenizer.mask_token}''' ) self.assertEqual( _a , [ {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, ] , ) _A : List[str] = {vocab[el] for el in targets} self.assertEqual({el["""token"""] for el in outputs} , _a ) _A : Union[str, Any] = [tokenizer.decode([x] ) for x in target_ids] self.assertEqual({el["""token_str"""] for el in outputs} , set(_a ) ) # Call argument _A : str = FillMaskPipeline(model=_a , tokenizer=_a ) _A : Optional[int] = fill_masker(F'''This is a {tokenizer.mask_token}''' , targets=_a ) self.assertEqual( _a , [ {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, ] , ) _A : int = {vocab[el] for el in targets} self.assertEqual({el["""token"""] for el in outputs} , _a ) _A : Any = [tokenizer.decode([x] ) for x in target_ids] self.assertEqual({el["""token_str"""] for el in outputs} , set(_a ) ) # Score equivalence _A : int = fill_masker(F'''This is a {tokenizer.mask_token}''' , targets=_a ) _A : Optional[int] = [top_mask["""token_str"""] for top_mask in outputs] _A : Union[str, Any] = [top_mask["""score"""] for top_mask in outputs] # For some BPE tokenizers, `</w>` is removed during decoding, so `token_str` won't be the same as in `targets`. if set(_a ) == set(_a ): _A : Tuple = fill_masker(F'''This is a {tokenizer.mask_token}''' , targets=_a ) _A : Union[str, Any] = [top_mask["""score"""] for top_mask in unmasked_targets] self.assertEqual(nested_simplify(_a ) , nested_simplify(_a ) ) # Raises with invalid with self.assertRaises(_a ): _A : str = fill_masker(F'''This is a {tokenizer.mask_token}''' , targets=[] ) # For some tokenizers, `""` is actually in the vocabulary and the expected error won't raised if "" not in tokenizer.get_vocab(): with self.assertRaises(_a ): _A : Optional[Any] = fill_masker(F'''This is a {tokenizer.mask_token}''' , targets=[""""""] ) with self.assertRaises(_a ): _A : int = fill_masker(F'''This is a {tokenizer.mask_token}''' , targets="""""" ) def a__ ( self , _a , _a ) -> Optional[Any]: _A : str = FillMaskPipeline(model=_a , tokenizer=_a , top_k=2 ) _A : str = fill_masker(F'''This is a {tokenizer.mask_token}''' ) self.assertEqual( _a , [ {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, ] , ) _A : Union[str, Any] = FillMaskPipeline(model=_a , tokenizer=_a ) _A : Union[str, Any] = fill_masker(F'''This is a {tokenizer.mask_token}''' , top_k=2 ) self.assertEqual( _a , [ {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, ] , ) self.assertEqual(nested_simplify(_a ) , nested_simplify(_a ) ) def a__ ( self , _a , _a ) -> List[Any]: _A : Union[str, Any] = tokenizer.get_vocab() _A : int = FillMaskPipeline(model=_a , tokenizer=_a ) # top_k=2, ntargets=3 _A : List[str] = sorted(vocab.keys() )[:3] _A : Tuple = fill_masker(F'''This is a {tokenizer.mask_token}''' , top_k=2 , targets=_a ) # If we use the most probably targets, and filter differently, we should still # have the same results _A : Any = [el["""token_str"""] for el in sorted(_a , key=lambda _a : x["score"] , reverse=_a )] # For some BPE tokenizers, `</w>` is removed during decoding, so `token_str` won't be the same as in `targets`. if set(_a ).issubset(_a ): _A : Any = fill_masker(F'''This is a {tokenizer.mask_token}''' , top_k=3 , targets=_a ) # They should yield exactly the same result self.assertEqual(nested_simplify(_a ) , nested_simplify(_a ) ) def a__ ( self , _a , _a ) -> str: _A : Optional[int] = FillMaskPipeline(model=_a , tokenizer=_a ) _A : List[Any] = tokenizer.get_vocab() # String duplicates + id duplicates _A : Optional[Any] = sorted(vocab.keys() )[:3] _A : Optional[Any] = [targets[0], targets[1], targets[0], targets[2], targets[1]] _A : Union[str, Any] = fill_masker(F'''My name is {tokenizer.mask_token}''' , targets=_a , top_k=10 ) # The target list contains duplicates, so we can't output more # than them self.assertEqual(len(_a ) , 3 ) def a__ ( self , _a , _a ) -> Tuple: _A : Any = FillMaskPipeline(model=_a , tokenizer=_a ) _A : Optional[Any] = fill_masker( F'''This is a {tokenizer.mask_token} {tokenizer.mask_token} {tokenizer.mask_token}''' , top_k=2 ) self.assertEqual( _a , [ [ {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, ], [ {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, ], [ {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, ], ] , )
54
0
import math import os import unittest from transformers import MegatronBertConfig, is_torch_available from transformers.models.auto import get_values from transformers.testing_utils import require_sentencepiece, require_tokenizers, 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 ( MODEL_FOR_PRETRAINING_MAPPING, MegatronBertForCausalLM, MegatronBertForMaskedLM, MegatronBertForMultipleChoice, MegatronBertForNextSentencePrediction, MegatronBertForPreTraining, MegatronBertForQuestionAnswering, MegatronBertForSequenceClassification, MegatronBertForTokenClassification, MegatronBertModel, ) class lowercase : def __init__( self , _a , _a=13 , _a=7 , _a=True , _a=True , _a=True , _a=True , _a=99 , _a=64 , _a=32 , _a=5 , _a=4 , _a=37 , _a="gelu" , _a=0.1 , _a=0.1 , _a=512 , _a=16 , _a=2 , _a=0.02 , _a=3 , _a=4 , _a=None , ) -> Optional[int]: _A : List[Any] = parent _A : str = batch_size _A : Optional[int] = seq_length _A : str = is_training _A : Tuple = use_input_mask _A : Dict = use_token_type_ids _A : Union[str, Any] = use_labels _A : List[str] = vocab_size _A : Any = hidden_size _A : int = embedding_size _A : Union[str, Any] = num_hidden_layers _A : Optional[int] = num_attention_heads _A : Any = intermediate_size _A : Dict = hidden_act _A : List[str] = hidden_dropout_prob _A : Tuple = attention_probs_dropout_prob _A : str = max_position_embeddings _A : Optional[Any] = type_vocab_size _A : str = type_sequence_label_size _A : str = initializer_range _A : int = num_labels _A : Optional[int] = num_choices _A : Dict = scope def a__ ( self ) -> int: _A : Optional[int] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _A : Dict = None if self.use_input_mask: _A : Tuple = random_attention_mask([self.batch_size, self.seq_length] ) _A : List[Any] = None if self.use_token_type_ids: _A : Any = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) _A : Dict = None _A : Tuple = None _A : List[str] = None if self.use_labels: _A : List[Any] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _A : List[str] = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) _A : Optional[Any] = ids_tensor([self.batch_size] , self.num_choices ) _A : Optional[Any] = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def a__ ( self ) -> Union[str, Any]: return MegatronBertConfig( 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 , embedding_size=self.embedding_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 , ) def a__ ( self , _a , _a , _a , _a , _a , _a , _a ) -> str: _A : List[str] = MegatronBertModel(config=__UpperCamelCase ) model.to(__UpperCamelCase ) model.eval() _A : int = model(__UpperCamelCase , attention_mask=__UpperCamelCase , token_type_ids=__UpperCamelCase ) _A : Optional[Any] = model(__UpperCamelCase , token_type_ids=__UpperCamelCase ) _A : Tuple = model(__UpperCamelCase ) 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 a__ ( self , _a , _a , _a , _a , _a , _a , _a ) -> Any: _A : int = MegatronBertForMaskedLM(config=__UpperCamelCase ) model.to(__UpperCamelCase ) model.eval() _A : Any = model(__UpperCamelCase , attention_mask=__UpperCamelCase , token_type_ids=__UpperCamelCase , labels=__UpperCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def a__ ( self , _a , _a , _a , _a , _a , _a , _a ) -> Any: _A : Optional[int] = MegatronBertForCausalLM(config=__UpperCamelCase ) model.to(__UpperCamelCase ) model.eval() _A : Optional[Any] = model(__UpperCamelCase , attention_mask=__UpperCamelCase , token_type_ids=__UpperCamelCase , labels=__UpperCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def a__ ( self , _a , _a , _a , _a , _a , _a , _a ) -> int: _A : int = MegatronBertForNextSentencePrediction(config=__UpperCamelCase ) model.to(__UpperCamelCase ) model.eval() _A : Optional[Any] = model( __UpperCamelCase , attention_mask=__UpperCamelCase , token_type_ids=__UpperCamelCase , labels=__UpperCamelCase , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, 2) ) def a__ ( self , _a , _a , _a , _a , _a , _a , _a ) -> List[Any]: _A : Dict = MegatronBertForPreTraining(config=__UpperCamelCase ) model.to(__UpperCamelCase ) model.eval() _A : Dict = model( __UpperCamelCase , attention_mask=__UpperCamelCase , token_type_ids=__UpperCamelCase , labels=__UpperCamelCase , next_sentence_label=__UpperCamelCase , ) self.parent.assertEqual(result.prediction_logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) self.parent.assertEqual(result.seq_relationship_logits.shape , (self.batch_size, 2) ) def a__ ( self , _a , _a , _a , _a , _a , _a , _a ) -> List[str]: _A : List[str] = MegatronBertForQuestionAnswering(config=__UpperCamelCase ) model.to(__UpperCamelCase ) model.eval() _A : List[str] = model( __UpperCamelCase , attention_mask=__UpperCamelCase , token_type_ids=__UpperCamelCase , start_positions=__UpperCamelCase , end_positions=__UpperCamelCase , ) 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 a__ ( self , _a , _a , _a , _a , _a , _a , _a ) -> int: _A : Union[str, Any] = self.num_labels _A : int = MegatronBertForSequenceClassification(__UpperCamelCase ) model.to(__UpperCamelCase ) model.eval() _A : Dict = model(__UpperCamelCase , attention_mask=__UpperCamelCase , token_type_ids=__UpperCamelCase , labels=__UpperCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def a__ ( self , _a , _a , _a , _a , _a , _a , _a ) -> List[str]: _A : Union[str, Any] = self.num_labels _A : Any = MegatronBertForTokenClassification(config=__UpperCamelCase ) model.to(__UpperCamelCase ) model.eval() _A : Any = model(__UpperCamelCase , attention_mask=__UpperCamelCase , token_type_ids=__UpperCamelCase , labels=__UpperCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def a__ ( self , _a , _a , _a , _a , _a , _a , _a ) -> Union[str, Any]: _A : int = self.num_choices _A : Optional[Any] = MegatronBertForMultipleChoice(config=__UpperCamelCase ) model.to(__UpperCamelCase ) model.eval() _A : int = input_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() _A : Union[str, Any] = token_type_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() _A : int = input_mask.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() _A : Union[str, Any] = model( __UpperCamelCase , attention_mask=__UpperCamelCase , token_type_ids=__UpperCamelCase , labels=__UpperCamelCase , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def a__ ( self ) -> Optional[Any]: _A : int = self.prepare_config_and_inputs() ( ( _A ) , ( _A ) , ( _A ) , ( _A ) , ( _A ) , ( _A ) , ( _A ) , ) : Any = config_and_inputs _A : List[Any] = {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask} return config, inputs_dict @require_torch class lowercase ( __snake_case,__snake_case,unittest.TestCase ): _a = ( ( MegatronBertModel, MegatronBertForMaskedLM, MegatronBertForCausalLM, MegatronBertForMultipleChoice, MegatronBertForNextSentencePrediction, MegatronBertForPreTraining, MegatronBertForQuestionAnswering, MegatronBertForSequenceClassification, MegatronBertForTokenClassification, ) if is_torch_available() else () ) _a = ( { "feature-extraction": MegatronBertModel, "fill-mask": MegatronBertForMaskedLM, "question-answering": MegatronBertForQuestionAnswering, "text-classification": MegatronBertForSequenceClassification, "text-generation": MegatronBertForCausalLM, "token-classification": MegatronBertForTokenClassification, "zero-shot": MegatronBertForSequenceClassification, } if is_torch_available() else {} ) _a = True # test_resize_embeddings = False _a = False def a__ ( self , _a , _a , _a=False ) -> str: _A : List[Any] = super()._prepare_for_class(__UpperCamelCase , __UpperCamelCase , return_labels=__UpperCamelCase ) if return_labels: if model_class in get_values(__UpperCamelCase ): _A : Dict = torch.zeros( (self.model_tester.batch_size, self.model_tester.seq_length) , dtype=torch.long , device=__UpperCamelCase ) _A : List[str] = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=__UpperCamelCase ) return inputs_dict def a__ ( self ) -> Tuple: _A : Any = MegatronBertModelTester(self ) _A : Any = ConfigTester(self , config_class=__UpperCamelCase , hidden_size=37 ) def a__ ( self ) -> str: self.config_tester.run_common_tests() def a__ ( self ) -> Tuple: _A : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_megatron_bert_model(*__UpperCamelCase ) def a__ ( self ) -> int: _A : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_megatron_bert_for_masked_lm(*__UpperCamelCase ) def a__ ( self ) -> int: _A : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_megatron_bert_for_multiple_choice(*__UpperCamelCase ) def a__ ( self ) -> str: _A : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_megatron_bert_for_next_sequence_prediction(*__UpperCamelCase ) def a__ ( self ) -> int: _A : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_megatron_bert_for_pretraining(*__UpperCamelCase ) def a__ ( self ) -> int: _A : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_megatron_bert_for_question_answering(*__UpperCamelCase ) def a__ ( self ) -> Tuple: _A : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_megatron_bert_for_sequence_classification(*__UpperCamelCase ) def a__ ( self ) -> int: _A : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_megatron_bert_for_token_classification(*__UpperCamelCase ) def lowerCAmelCase_ ( snake_case_ ): return torch.tensor( lowercase__,dtype=torch.long,device=lowercase__,) _snake_case = 1e-4 @require_torch @require_sentencepiece @require_tokenizers class lowercase ( unittest.TestCase ): @slow @unittest.skip("""Model is not available.""" ) def a__ ( self ) -> int: _A : List[Any] = """nvidia/megatron-bert-uncased-345m""" if "MYDIR" in os.environ: _A : List[Any] = os.path.join(os.environ["""MYDIR"""] , __UpperCamelCase ) _A : Union[str, Any] = MegatronBertModel.from_pretrained(__UpperCamelCase ) model.to(__UpperCamelCase ) model.half() _A : str = _long_tensor([[101, 7110, 1005, 1056, 2023, 1_1333, 1_7413, 1029, 102]] ) with torch.no_grad(): _A : Optional[int] = model(__UpperCamelCase )[0] _A : List[str] = torch.Size((1, 9, 1024) ) self.assertEqual(output.shape , __UpperCamelCase ) _A : Any = [-0.6040, -0.2517, -0.1025, 0.3420, -0.6758, -0.0017, -0.1089, -0.1990, 0.5728] for ii in range(3 ): for jj in range(3 ): _A : int = output[0, ii, jj] _A : Any = expected[3 * ii + jj] _A : List[str] = """ii={} jj={} a={} b={}""".format(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) self.assertTrue(math.isclose(__UpperCamelCase , __UpperCamelCase , rel_tol=__UpperCamelCase , abs_tol=__UpperCamelCase ) , msg=__UpperCamelCase )
702
import json import os import unittest from transformers import CLIPTokenizer, CLIPTokenizerFast from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES from transformers.testing_utils import require_ftfy, require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class lowercase ( UpperCamelCase__,unittest.TestCase ): _a = CLIPTokenizer _a = CLIPTokenizerFast _a = True _a = {} _a = False def a__ ( self ) -> Optional[Any]: super().setUp() # fmt: off _A : int = ["""l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """lo""", """l</w>""", """w</w>""", """r</w>""", """t</w>""", """low</w>""", """er</w>""", """lowest</w>""", """newer</w>""", """wider""", """<unk>""", """<|startoftext|>""", """<|endoftext|>"""] # fmt: on _A : str = dict(zip(_a , range(len(_a ) ) ) ) _A : Optional[int] = ["""#version: 0.2""", """l o""", """lo w</w>""", """e r</w>"""] _A : str = {"""unk_token""": """<unk>"""} _A : Union[str, Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""vocab_file"""] ) _A : Union[str, Any] = 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(_a ) + """\n""" ) with open(self.merges_file , """w""" , encoding="""utf-8""" ) as fp: fp.write("""\n""".join(_a ) ) def a__ ( self , **_a ) -> List[Any]: kwargs.update(self.special_tokens_map ) return CLIPTokenizer.from_pretrained(self.tmpdirname , **_a ) def a__ ( self , **_a ) -> Union[str, Any]: kwargs.update(self.special_tokens_map ) return CLIPTokenizerFast.from_pretrained(self.tmpdirname , **_a ) def a__ ( self , _a ) -> str: _A : Tuple = """lower newer""" _A : Optional[Any] = """lower newer""" return input_text, output_text def a__ ( self ) -> List[Any]: _A : Optional[int] = CLIPTokenizer(self.vocab_file , self.merges_file , **self.special_tokens_map ) _A : str = """lower newer""" _A : List[str] = ["""lo""", """w""", """er</w>""", """n""", """e""", """w""", """er</w>"""] _A : int = tokenizer.tokenize(_a ) self.assertListEqual(_a , _a ) _A : str = tokens + [tokenizer.unk_token] _A : Dict = [10, 2, 16, 9, 3, 2, 16, 20] self.assertListEqual(tokenizer.convert_tokens_to_ids(_a ) , _a ) @require_ftfy def a__ ( self ) -> Tuple: for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): _A : Dict = self.tokenizer_class.from_pretrained(_a , **_a ) _A : str = self.rust_tokenizer_class.from_pretrained(_a , **_a ) _A : List[str] = """A\n'll 11p223RF☆ho!!to?'d'd''d of a cat to-$''d.""" _A : Dict = tokenizer_s.tokenize(_a ) _A : Dict = tokenizer_r.tokenize(_a ) self.assertListEqual(_a , _a ) # Test that the tokenization is identical on an example containing a character (Latin Small Letter A # with Tilde) encoded in 2 different ways _A : Tuple = """xa\u0303y""" + """ """ + """x\xe3y""" _A : Dict = tokenizer_s.tokenize(_a ) _A : Dict = tokenizer_r.tokenize(_a ) self.assertListEqual(_a , _a ) # Test that the tokenization is identical on unicode of space type _A : Any = [ """\u0009""", # (horizontal tab, '\t') """\u000B""", # (vertical tab) """\u000C""", # (form feed) """\u0020""", # (space, ' ') """\u200E""", # (left-to-right mark):w """\u200F""", # (right-to-left mark) ] for unicode_seq in spaces_unicodes: _A : Optional[int] = tokenizer_s.tokenize(_a ) _A : List[Any] = tokenizer_r.tokenize(_a ) self.assertListEqual(_a , _a ) # Test that the tokenization is identical on unicode of line break type _A : int = [ """\u000A""", # (line feed, '\n') """\r\n""", # (carriage return and line feed, '\r\n') """\u000D""", # (carriage return, '\r') """\r""", # (carriage return, '\r') """\u000D""", # (carriage return, '\r') """\u2028""", # (line separator) """\u2029""", # (paragraph separator) # "\u0085", # (next line) ] # The tokenization is not identical for the character "\u0085" (next line). The slow version using ftfy transforms # it into the Horizontal Ellipsis character "…" ("\u2026") while the fast version transforms it into a # space (and thus into an empty list). for unicode_seq in line_break_unicodes: _A : Optional[Any] = tokenizer_s.tokenize(_a ) _A : Tuple = tokenizer_r.tokenize(_a ) self.assertListEqual(_a , _a ) def a__ ( self ) -> Any: # Test which aims to verify that the offsets are well adapted to the argument `add_prefix_space` for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): _A : List[Any] = """hello""" # `hello` is a token in the vocabulary of `pretrained_name` _A : str = F'''{text_of_1_token} {text_of_1_token}''' _A : str = self.rust_tokenizer_class.from_pretrained( _a , use_fast=_a , ) _A : Dict = tokenizer_r(_a , return_offsets_mapping=_a , add_special_tokens=_a ) self.assertEqual(encoding.offset_mapping[0] , (0, len(_a )) ) self.assertEqual( encoding.offset_mapping[1] , (len(_a ) + 1, len(_a ) + 1 + len(_a )) , ) _A : Any = F''' {text}''' _A : List[Any] = self.rust_tokenizer_class.from_pretrained( _a , use_fast=_a , ) _A : Optional[int] = tokenizer_r(_a , return_offsets_mapping=_a , add_special_tokens=_a ) self.assertEqual(encoding.offset_mapping[0] , (1, 1 + len(_a )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(_a ) + 1, 1 + len(_a ) + 1 + len(_a )) , ) def a__ ( self ) -> List[Any]: # Test related to the breaking change introduced in transformers v4.17.0 # We need to check that an error in raised when the user try to load a previous version of the tokenizer. with self.assertRaises(_a ) as context: self.rust_tokenizer_class.from_pretrained("""robot-test/old-clip-tokenizer""" ) self.assertTrue( context.exception.args[0].startswith( """The `backend_tokenizer` provided does not match the expected format.""" ) ) @require_ftfy def a__ ( self ) -> str: super().test_tokenization_python_rust_equals() def a__ ( self ) -> Union[str, Any]: # CLIP always lower cases letters pass
54
0
import json import os from functools import lru_cache from typing import Dict, List, Optional, Tuple, Union import regex as re from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...tokenization_utils_base import BatchEncoding, EncodedInput from ...utils import PaddingStrategy, logging _snake_case = logging.get_logger(__name__) _snake_case = {'vocab_file': 'vocab.json', 'merges_file': 'merges.txt'} # See all LED models at https://huggingface.co/models?filter=LED _snake_case = { 'vocab_file': { 'allenai/led-base-16384': 'https://huggingface.co/allenai/led-base-16384/resolve/main/vocab.json', }, 'merges_file': { 'allenai/led-base-16384': 'https://huggingface.co/allenai/led-base-16384/resolve/main/merges.txt', }, 'tokenizer_file': { 'allenai/led-base-16384': 'https://huggingface.co/allenai/led-base-16384/resolve/main/tokenizer.json', }, } _snake_case = { 'allenai/led-base-16384': 16384, } @lru_cache() # Copied from transformers.models.bart.tokenization_bart.bytes_to_unicode def lowerCAmelCase_ ( ): _A : Dict = ( list(range(ord("""!""" ),ord("""~""" ) + 1 ) ) + list(range(ord("""¡""" ),ord("""¬""" ) + 1 ) ) + list(range(ord("""®""" ),ord("""ÿ""" ) + 1 ) ) ) _A : int = bs[:] _A : Optional[Any] = 0 for b in range(2**8 ): if b not in bs: bs.append(lowerCAmelCase_ ) cs.append(2**8 + n ) n += 1 _A : int = [chr(lowerCAmelCase_ ) for n in cs] return dict(zip(lowerCAmelCase_,lowerCAmelCase_ ) ) def lowerCAmelCase_ ( snake_case_ ): _A : Dict = set() _A : Tuple = word[0] for char in word[1:]: pairs.add((prev_char, char) ) _A : Tuple = char return pairs class lowercase ( a__ ): _a = VOCAB_FILES_NAMES _a = PRETRAINED_VOCAB_FILES_MAP _a = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _a = ["""input_ids""", """attention_mask"""] def __init__( self , _a , _a , _a="replace" , _a="<s>" , _a="</s>" , _a="</s>" , _a="<s>" , _a="<unk>" , _a="<pad>" , _a="<mask>" , _a=False , **_a , ) -> List[str]: _A : Tuple = AddedToken(lowercase__ , lstrip=lowercase__ , rstrip=lowercase__ ) if isinstance(lowercase__ , lowercase__ ) else bos_token _A : Dict = AddedToken(lowercase__ , lstrip=lowercase__ , rstrip=lowercase__ ) if isinstance(lowercase__ , lowercase__ ) else eos_token _A : Union[str, Any] = AddedToken(lowercase__ , lstrip=lowercase__ , rstrip=lowercase__ ) if isinstance(lowercase__ , lowercase__ ) else sep_token _A : Optional[int] = AddedToken(lowercase__ , lstrip=lowercase__ , rstrip=lowercase__ ) if isinstance(lowercase__ , lowercase__ ) else cls_token _A : Tuple = AddedToken(lowercase__ , lstrip=lowercase__ , rstrip=lowercase__ ) if isinstance(lowercase__ , lowercase__ ) else unk_token _A : Any = AddedToken(lowercase__ , lstrip=lowercase__ , rstrip=lowercase__ ) if isinstance(lowercase__ , lowercase__ ) else pad_token # Mask token behave like a normal word, i.e. include the space before it _A : str = AddedToken(lowercase__ , lstrip=lowercase__ , rstrip=lowercase__ ) if isinstance(lowercase__ , lowercase__ ) else mask_token super().__init__( errors=lowercase__ , bos_token=lowercase__ , eos_token=lowercase__ , unk_token=lowercase__ , sep_token=lowercase__ , cls_token=lowercase__ , pad_token=lowercase__ , mask_token=lowercase__ , add_prefix_space=lowercase__ , **lowercase__ , ) with open(lowercase__ , encoding="""utf-8""" ) as vocab_handle: _A : List[str] = json.load(lowercase__ ) _A : Any = {v: k for k, v in self.encoder.items()} _A : Dict = errors # how to handle errors in decoding _A : Optional[int] = bytes_to_unicode() _A : Tuple = {v: k for k, v in self.byte_encoder.items()} with open(lowercase__ , encoding="""utf-8""" ) as merges_handle: _A : Tuple = merges_handle.read().split("""\n""" )[1:-1] _A : Tuple = [tuple(merge.split() ) for merge in bpe_merges] _A : int = dict(zip(lowercase__ , range(len(lowercase__ ) ) ) ) _A : List[Any] = {} _A : Tuple = add_prefix_space # Should have added re.IGNORECASE so BPE merges can happen for capitalized versions of contractions _A : Optional[int] = re.compile(R"""\'s|\'t|\'re|\'ve|\'m|\'ll|\'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+""" ) @property # Copied from transformers.models.bart.tokenization_bart.BartTokenizer.vocab_size def a__ ( self ) -> Optional[int]: return len(self.encoder ) def a__ ( self ) -> Any: return dict(self.encoder , **self.added_tokens_encoder ) def a__ ( self , _a ) -> List[Any]: if token in self.cache: return self.cache[token] _A : Optional[int] = tuple(lowercase__ ) _A : List[str] = get_pairs(lowercase__ ) if not pairs: return token while True: _A : Dict = min(lowercase__ , key=lambda _a : self.bpe_ranks.get(lowercase__ , float("""inf""" ) ) ) if bigram not in self.bpe_ranks: break _A : Any = bigram _A : Union[str, Any] = [] _A : Tuple = 0 while i < len(lowercase__ ): try: _A : Union[str, Any] = word.index(lowercase__ , lowercase__ ) except ValueError: new_word.extend(word[i:] ) break else: new_word.extend(word[i:j] ) _A : Union[str, Any] = j if word[i] == first and i < len(lowercase__ ) - 1 and word[i + 1] == second: new_word.append(first + second ) i += 2 else: new_word.append(word[i] ) i += 1 _A : str = tuple(lowercase__ ) _A : Any = new_word if len(lowercase__ ) == 1: break else: _A : str = get_pairs(lowercase__ ) _A : Union[str, Any] = ''' '''.join(lowercase__ ) _A : Optional[int] = word return word def a__ ( self , _a ) -> Tuple: _A : List[str] = [] for token in re.findall(self.pat , lowercase__ ): _A : Optional[int] = ''''''.join( self.byte_encoder[b] for b in token.encode("""utf-8""" ) ) # Maps all our bytes to unicode strings, avoiding control tokens of the BPE (spaces in our case) bpe_tokens.extend(bpe_token for bpe_token in self.bpe(lowercase__ ).split(""" """ ) ) return bpe_tokens def a__ ( self , _a ) -> List[str]: return self.encoder.get(lowercase__ , self.encoder.get(self.unk_token ) ) def a__ ( self , _a ) -> Tuple: return self.decoder.get(lowercase__ ) def a__ ( self , _a ) -> Optional[Any]: _A : Any = ''''''.join(lowercase__ ) _A : List[Any] = bytearray([self.byte_decoder[c] for c in text] ).decode("""utf-8""" , errors=self.errors ) return text def a__ ( self , _a , _a = None ) -> Tuple[str]: if not os.path.isdir(lowercase__ ): logger.error(F'''Vocabulary path ({save_directory}) should be a directory''' ) return _A : Tuple = os.path.join( lowercase__ , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) _A : List[str] = os.path.join( lowercase__ , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""merges_file"""] ) with open(lowercase__ , """w""" , encoding="""utf-8""" ) as f: f.write(json.dumps(self.encoder , indent=2 , sort_keys=lowercase__ , ensure_ascii=lowercase__ ) + """\n""" ) _A : Dict = 0 with open(lowercase__ , """w""" , encoding="""utf-8""" ) as writer: writer.write("""#version: 0.2\n""" ) for bpe_tokens, token_index in sorted(self.bpe_ranks.items() , key=lambda _a : kv[1] ): if index != token_index: logger.warning( F'''Saving vocabulary to {merge_file}: BPE merge indices are not consecutive.''' """ Please check that the tokenizer is not corrupted!""" ) _A : Optional[Any] = token_index writer.write(""" """.join(lowercase__ ) + """\n""" ) index += 1 return vocab_file, merge_file def a__ ( self , _a , _a = None ) -> List[int]: if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] _A : str = [self.cls_token_id] _A : Tuple = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def a__ ( self , _a , _a = None , _a = False ) -> List[int]: if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=lowercase__ , token_ids_a=lowercase__ , already_has_special_tokens=lowercase__ ) if token_ids_a is None: return [1] + ([0] * len(lowercase__ )) + [1] return [1] + ([0] * len(lowercase__ )) + [1, 1] + ([0] * len(lowercase__ )) + [1] def a__ ( self , _a , _a = None ) -> List[int]: _A : Optional[Any] = [self.sep_token_id] _A : int = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def a__ ( self , _a , _a=False , **_a ) -> List[Any]: _A : Union[str, Any] = kwargs.pop("""add_prefix_space""" , self.add_prefix_space ) if (is_split_into_words or add_prefix_space) and (len(lowercase__ ) > 0 and not text[0].isspace()): _A : Dict = ''' ''' + text return (text, kwargs) def a__ ( self , _a , _a = None , _a = PaddingStrategy.DO_NOT_PAD , _a = None , _a = None , ) -> dict: _A : int = super()._pad( encoded_inputs=lowercase__ , max_length=lowercase__ , padding_strategy=lowercase__ , pad_to_multiple_of=lowercase__ , return_attention_mask=lowercase__ , ) # Load from model defaults if return_attention_mask is None: _A : Optional[Any] = '''attention_mask''' in self.model_input_names if return_attention_mask and "global_attention_mask" in encoded_inputs: _A : Optional[Any] = encoded_inputs[self.model_input_names[0]] # `global_attention_mask` need to have the same length as other (sequential) inputs. _A : Dict = len(encoded_inputs["""global_attention_mask"""] ) != len(lowercase__ ) if needs_to_be_padded: _A : Any = len(lowercase__ ) - len(encoded_inputs["""global_attention_mask"""] ) if self.padding_side == "right": # Use `-1` since `0` in `global_attention_mask` means `local attention` instead of `not to attend` _A : Any = ( encoded_inputs['''global_attention_mask'''] + [-1] * difference ) elif self.padding_side == "left": _A : Optional[Any] = [-1] * difference + encoded_inputs[ '''global_attention_mask''' ] else: raise ValueError("""Invalid padding strategy:""" + str(self.padding_side ) ) return encoded_inputs
703
from datetime import datetime as dt import os from github import Github _snake_case = [ "good first issue", "good second issue", "good difficult issue", "feature request", "new model", "wip", ] def lowerCAmelCase_ ( ): _A : int = Github(os.environ["""GITHUB_TOKEN"""] ) _A : Tuple = g.get_repo("""huggingface/transformers""" ) _A : Dict = repo.get_issues(state="""open""" ) for issue in open_issues: _A : Optional[Any] = sorted([comment for comment in issue.get_comments()],key=lambda snake_case_ : i.created_at,reverse=snake_case_ ) _A : Dict = comments[0] if len(snake_case_ ) > 0 else None if ( last_comment is not None and last_comment.user.login == "github-actions[bot]" and (dt.utcnow() - issue.updated_at).days > 7 and (dt.utcnow() - issue.created_at).days >= 30 and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels() ) ): # print(f"Would close issue {issue.number} since it has been 7 days of inactivity since bot mention.") issue.edit(state="""closed""" ) elif ( (dt.utcnow() - issue.updated_at).days > 23 and (dt.utcnow() - issue.created_at).days >= 30 and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels() ) ): # print(f"Would add stale comment to {issue.number}") issue.create_comment( """This issue has been automatically marked as stale because it has not had """ """recent activity. If you think this still needs to be addressed """ """please comment on this thread.\n\nPlease note that issues that do not follow the """ """[contributing guidelines](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) """ """are likely to be ignored.""" ) if __name__ == "__main__": main()
54
0
import argparse import logging import pickle from collections import Counter logging.basicConfig( format="%(asctime)s - %(levelname)s - %(name)s - %(message)s", datefmt="%m/%d/%Y %H:%M:%S", level=logging.INFO ) _snake_case = logging.getLogger(__name__) if __name__ == "__main__": _snake_case = argparse.ArgumentParser( description="Token Counts for smoothing the masking probabilities in MLM (cf XLM/word2vec)" ) parser.add_argument( "--data_file", type=str, default="data/dump.bert-base-uncased.pickle", help="The binarized dataset." ) parser.add_argument( "--token_counts_dump", type=str, default="data/token_counts.bert-base-uncased.pickle", help="The dump file." ) parser.add_argument("--vocab_size", default=30522, type=int) _snake_case = parser.parse_args() logger.info(f"""Loading data from {args.data_file}""") with open(args.data_file, "rb") as fp: _snake_case = pickle.load(fp) logger.info("Counting occurrences for MLM.") _snake_case = Counter() for tk_ids in data: counter.update(tk_ids) _snake_case = [0] * args.vocab_size for k, v in counter.items(): _snake_case = v logger.info(f"""Dump to {args.token_counts_dump}""") with open(args.token_counts_dump, "wb") as handle: pickle.dump(counts, handle, protocol=pickle.HIGHEST_PROTOCOL)
704
from __future__ import annotations class lowercase : def __init__( self , _a = 0 ) -> str: _A : Any = key def a__ ( self , _a , _a ) -> list[str]: assert isinstance(_a , _a ) and isinstance(_a , _a ) _A : Any = key or self.__key or 1 # make sure key is an appropriate size key %= 255 return [chr(ord(_a ) ^ key ) for ch in content] def a__ ( self , _a , _a ) -> list[str]: assert isinstance(_a , _a ) and isinstance(_a , _a ) _A : List[Any] = key or self.__key or 1 # make sure key is an appropriate size key %= 255 return [chr(ord(_a ) ^ key ) for ch in content] def a__ ( self , _a , _a = 0 ) -> str: assert isinstance(_a , _a ) and isinstance(_a , _a ) _A : List[Any] = key or self.__key or 1 # make sure key can be any size while key > 255: key -= 255 # This will be returned _A : List[str] = """""" for ch in content: ans += chr(ord(_a ) ^ key ) return ans def a__ ( self , _a , _a = 0 ) -> str: assert isinstance(_a , _a ) and isinstance(_a , _a ) _A : List[str] = key or self.__key or 1 # make sure key can be any size while key > 255: key -= 255 # This will be returned _A : List[str] = """""" for ch in content: ans += chr(ord(_a ) ^ key ) return ans def a__ ( self , _a , _a = 0 ) -> bool: assert isinstance(_a , _a ) and isinstance(_a , _a ) try: with open(_a ) as fin, open("""encrypt.out""" , """w+""" ) as fout: # actual encrypt-process for line in fin: fout.write(self.encrypt_string(_a , _a ) ) except OSError: return False return True def a__ ( self , _a , _a ) -> bool: assert isinstance(_a , _a ) and isinstance(_a , _a ) try: with open(_a ) as fin, open("""decrypt.out""" , """w+""" ) as fout: # actual encrypt-process for line in fin: fout.write(self.decrypt_string(_a , _a ) ) except OSError: return False return True # Tests # crypt = XORCipher() # key = 67 # # test encrypt # print(crypt.encrypt("hallo welt",key)) # # test decrypt # print(crypt.decrypt(crypt.encrypt("hallo welt",key), key)) # # test encrypt_string # print(crypt.encrypt_string("hallo welt",key)) # # test decrypt_string # print(crypt.decrypt_string(crypt.encrypt_string("hallo welt",key),key)) # if (crypt.encrypt_file("test.txt",key)): # print("encrypt successful") # else: # print("encrypt unsuccessful") # if (crypt.decrypt_file("encrypt.out",key)): # print("decrypt successful") # else: # print("decrypt unsuccessful")
54
0
def lowerCAmelCase_ ( snake_case_ ): _A : Union[str, Any] = 0 while len(a_ ) > 1: _A : List[Any] = 0 # Consider two files with minimum cost to be merged for _ in range(2 ): _A : int = files.index(min(a_ ) ) temp += files[min_index] files.pop(a_ ) files.append(a_ ) optimal_merge_cost += temp return optimal_merge_cost if __name__ == "__main__": import doctest doctest.testmod()
705
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 = random.Random() def lowerCAmelCase_ ( snake_case_,snake_case_=1.0,snake_case_=None,snake_case_=None ): if rng is None: _A : str = global_rng _A : List[Any] = [] 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 lowercase ( unittest.TestCase ): def __init__( self , _a , _a=7 , _a=400 , _a=2000 , _a=10 , _a=160 , _a=8 , _a=0.0 , _a=4000 , _a=False , _a=True , ) -> Optional[int]: _A : Any = parent _A : List[Any] = batch_size _A : List[Any] = min_seq_length _A : Dict = max_seq_length _A : Optional[Any] = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) _A : Tuple = padding_value _A : Tuple = sampling_rate _A : str = return_attention_mask _A : Any = do_normalize _A : Union[str, Any] = feature_size _A : List[Any] = chunk_length _A : List[Any] = hop_length def a__ ( self ) -> List[str]: 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 a__ ( self , _a=False , _a=False ) -> List[str]: def _flatten(_a ): return list(itertools.chain(*_a ) ) 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 : int = [ 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 : Any = [np.asarray(_a ) for x in speech_inputs] return speech_inputs @require_torch @require_torchaudio class lowercase ( UpperCamelCase__,unittest.TestCase ): _a = WhisperFeatureExtractor if is_speech_available() else None def a__ ( self ) -> Tuple: _A : Optional[int] = WhisperFeatureExtractionTester(self ) def a__ ( self ) -> Optional[Any]: _A : List[Any] = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: _A : List[str] = feat_extract_first.save_pretrained(_a )[0] check_json_file_has_correct_format(_a ) _A : Optional[int] = self.feature_extraction_class.from_pretrained(_a ) _A : Tuple = feat_extract_first.to_dict() _A : List[Any] = feat_extract_second.to_dict() _A : List[Any] = feat_extract_first.mel_filters _A : Union[str, Any] = feat_extract_second.mel_filters self.assertTrue(np.allclose(_a , _a ) ) self.assertEqual(_a , _a ) def a__ ( self ) -> Dict: _A : Any = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: _A : Dict = os.path.join(_a , """feat_extract.json""" ) feat_extract_first.to_json_file(_a ) _A : Optional[int] = self.feature_extraction_class.from_json_file(_a ) _A : str = feat_extract_first.to_dict() _A : Any = feat_extract_second.to_dict() _A : Union[str, Any] = feat_extract_first.mel_filters _A : Union[str, Any] = feat_extract_second.mel_filters self.assertTrue(np.allclose(_a , _a ) ) self.assertEqual(_a , _a ) def a__ ( self ) -> Union[str, Any]: # Tests that all call wrap to encode_plus and batch_encode_plus _A : Optional[int] = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 _A : Optional[Any] = [floats_list((1, x) )[0] for x in range(800 , 1400 , 200 )] _A : Any = [np.asarray(_a ) for speech_input in speech_inputs] # Test feature size _A : Dict = feature_extractor(_a , 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 : List[Any] = feature_extractor(speech_inputs[0] , return_tensors="""np""" ).input_features _A : List[str] = feature_extractor(np_speech_inputs[0] , return_tensors="""np""" ).input_features self.assertTrue(np.allclose(_a , _a , atol=1e-3 ) ) # Test batched _A : Union[str, Any] = feature_extractor(_a , return_tensors="""np""" ).input_features _A : Tuple = feature_extractor(_a , return_tensors="""np""" ).input_features for enc_seq_a, enc_seq_a in zip(_a , _a ): self.assertTrue(np.allclose(_a , _a , atol=1e-3 ) ) # Test 2-D numpy arrays are batched. _A : List[str] = [floats_list((1, x) )[0] for x in (800, 800, 800)] _A : Any = np.asarray(_a ) _A : Union[str, Any] = feature_extractor(_a , return_tensors="""np""" ).input_features _A : int = feature_extractor(_a , return_tensors="""np""" ).input_features for enc_seq_a, enc_seq_a in zip(_a , _a ): self.assertTrue(np.allclose(_a , _a , atol=1e-3 ) ) # Test truncation required _A : List[Any] = [floats_list((1, x) )[0] for x in range(200 , (feature_extractor.n_samples + 500) , 200 )] _A : Union[str, Any] = [np.asarray(_a ) for speech_input in speech_inputs] _A : Tuple = [x[: feature_extractor.n_samples] for x in speech_inputs] _A : Union[str, Any] = [np.asarray(_a ) for speech_input in speech_inputs_truncated] _A : Optional[int] = feature_extractor(_a , return_tensors="""np""" ).input_features _A : List[Any] = feature_extractor(_a , return_tensors="""np""" ).input_features for enc_seq_a, enc_seq_a in zip(_a , _a ): self.assertTrue(np.allclose(_a , _a , atol=1e-3 ) ) def a__ ( self ) -> Dict: import torch _A : Dict = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) _A : Optional[int] = np.random.rand(100 , 32 ).astype(np.floataa ) _A : str = np_speech_inputs.tolist() for inputs in [py_speech_inputs, np_speech_inputs]: _A : Optional[Any] = feature_extractor.pad([{"""input_features""": inputs}] , return_tensors="""np""" ) self.assertTrue(np_processed.input_features.dtype == np.floataa ) _A : Optional[int] = feature_extractor.pad([{"""input_features""": inputs}] , return_tensors="""pt""" ) self.assertTrue(pt_processed.input_features.dtype == torch.floataa ) def a__ ( self , _a ) -> Dict: _A : Optional[Any] = load_dataset("""hf-internal-testing/librispeech_asr_dummy""" , """clean""" , split="""validation""" ) # automatic decoding with librispeech _A : Optional[Any] = ds.sort("""id""" ).select(range(_a ) )[:num_samples]["""audio"""] return [x["array"] for x in speech_samples] def a__ ( self ) -> Tuple: # fmt: off _A : Dict = torch.tensor( [ 0.1193, -0.0946, -0.1098, -0.0196, 0.0225, -0.0690, -0.1736, 0.0951, 0.0971, -0.0817, -0.0702, 0.0162, 0.0260, 0.0017, -0.0192, -0.1678, 0.0709, -0.1867, -0.0655, -0.0274, -0.0234, -0.1884, -0.0516, -0.0554, -0.0274, -0.1425, -0.1423, 0.0837, 0.0377, -0.0854 ] ) # fmt: on _A : Dict = self._load_datasamples(1 ) _A : Optional[Any] = WhisperFeatureExtractor() _A : Optional[Any] = feature_extractor(_a , return_tensors="""pt""" ).input_features self.assertEqual(input_features.shape , (1, 80, 3000) ) self.assertTrue(torch.allclose(input_features[0, 0, :30] , _a , atol=1e-4 ) ) def a__ ( self ) -> str: _A : Dict = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) _A : str = self._load_datasamples(1 )[0] _A : Union[str, Any] = ((audio - audio.min()) / (audio.max() - audio.min())) * 6_5535 # Rescale to [0, 65535] to show issue _A : List[Any] = feat_extract.zero_mean_unit_var_norm([audio] , attention_mask=_a )[0] self.assertTrue(np.all(np.mean(_a ) < 1e-3 ) ) self.assertTrue(np.all(np.abs(np.var(_a ) - 1 ) < 1e-3 ) )
54
0
import os def lowerCAmelCase_ ( snake_case_ = "input.txt" ): with open(os.path.join(os.path.dirname(snake_case_ ),snake_case_ ) ) as input_file: _A : Union[str, Any] = [ [int(snake_case_ ) for element in line.split(""",""" )] for line in input_file.readlines() ] _A : Tuple = len(snake_case_ ) _A : Union[str, Any] = len(matrix[0] ) _A : Union[str, Any] = [[-1 for _ in range(snake_case_ )] for _ in range(snake_case_ )] for i in range(snake_case_ ): _A : Dict = matrix[i][0] for j in range(1,snake_case_ ): for i in range(snake_case_ ): _A : Dict = minimal_path_sums[i][j - 1] + matrix[i][j] for i in range(1,snake_case_ ): _A : Optional[int] = min( minimal_path_sums[i][j],minimal_path_sums[i - 1][j] + matrix[i][j] ) for i in range(rows - 2,-1,-1 ): _A : Union[str, Any] = min( minimal_path_sums[i][j],minimal_path_sums[i + 1][j] + matrix[i][j] ) return min(minimal_path_sums_row[-1] for minimal_path_sums_row in minimal_path_sums ) if __name__ == "__main__": print(f"""{solution() = }""")
706
def lowerCAmelCase_ ( snake_case_,snake_case_ ): _A : Union[str, Any] = """""" for i in table: res += inp[i - 1] return res def lowerCAmelCase_ ( snake_case_ ): return data[1:] + data[0] def lowerCAmelCase_ ( snake_case_,snake_case_ ): _A : Dict = """""" for i in range(len(snake_case_ ) ): if a[i] == b[i]: res += "0" else: res += "1" return res def lowerCAmelCase_ ( snake_case_,snake_case_ ): _A : int = int("""0b""" + data[0] + data[-1],2 ) _A : Any = int("""0b""" + data[1:3],2 ) return bin(s[row][col] )[2:] def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_,snake_case_,snake_case_ ): _A : List[str] = message[:4] _A : List[Any] = message[4:] _A : Union[str, Any] = apply_table(snake_case_,snake_case_ ) _A : List[Any] = xor(snake_case_,snake_case_ ) _A : Optional[Any] = apply_sbox(snake_case_,temp[:4] ) # noqa: E741 _A : List[Any] = apply_sbox(snake_case_,temp[4:] ) _A : int = """0""" * (2 - len(snake_case_ )) + l # noqa: E741 _A : Union[str, Any] = """0""" * (2 - len(snake_case_ )) + r _A : List[Any] = apply_table(l + r,snake_case_ ) _A : Any = xor(snake_case_,snake_case_ ) return temp + right if __name__ == "__main__": _snake_case = input("Enter 10 bit key: ") _snake_case = input("Enter 8 bit message: ") _snake_case = [6, 3, 7, 4, 8, 5, 10, 9] _snake_case = [3, 5, 2, 7, 4, 10, 1, 9, 8, 6] _snake_case = [2, 4, 3, 1] _snake_case = [2, 6, 3, 1, 4, 8, 5, 7] _snake_case = [4, 1, 3, 5, 7, 2, 8, 6] _snake_case = [4, 1, 2, 3, 2, 3, 4, 1] _snake_case = [[1, 0, 3, 2], [3, 2, 1, 0], [0, 2, 1, 3], [3, 1, 3, 2]] _snake_case = [[0, 1, 2, 3], [2, 0, 1, 3], [3, 0, 1, 0], [2, 1, 0, 3]] # key generation _snake_case = apply_table(key, paa_table) _snake_case = temp[:5] _snake_case = temp[5:] _snake_case = left_shift(left) _snake_case = left_shift(right) _snake_case = apply_table(left + right, pa_table) _snake_case = left_shift(left) _snake_case = left_shift(right) _snake_case = left_shift(left) _snake_case = left_shift(right) _snake_case = apply_table(left + right, pa_table) # encryption _snake_case = apply_table(message, IP) _snake_case = function(expansion, sa, sa, keya, temp) _snake_case = temp[4:] + temp[:4] _snake_case = function(expansion, sa, sa, keya, temp) _snake_case = apply_table(temp, IP_inv) print("Cipher text is:", CT) # decryption _snake_case = apply_table(CT, IP) _snake_case = function(expansion, sa, sa, keya, temp) _snake_case = temp[4:] + temp[:4] _snake_case = function(expansion, sa, sa, keya, temp) _snake_case = apply_table(temp, IP_inv) print("Plain text after decypting is:", PT)
54
0
import unittest import torch from diffusers import VQModel from diffusers.utils import floats_tensor, torch_device from diffusers.utils.testing_utils import enable_full_determinism from .test_modeling_common import ModelTesterMixin, UNetTesterMixin enable_full_determinism() class lowercase ( _UpperCamelCase,_UpperCamelCase,unittest.TestCase ): _a = VQModel _a = "sample" @property def a__ ( self , _a=(32, 32) ) -> int: _A : List[Any] = 4 _A : Dict = 3 _A : Any = floats_tensor((batch_size, num_channels) + sizes ).to(__a ) return {"sample": image} @property def a__ ( self ) -> Optional[Any]: return (3, 32, 32) @property def a__ ( self ) -> Any: return (3, 32, 32) def a__ ( self ) -> List[str]: _A : Tuple = { "block_out_channels": [32, 64], "in_channels": 3, "out_channels": 3, "down_block_types": ["DownEncoderBlock2D", "DownEncoderBlock2D"], "up_block_types": ["UpDecoderBlock2D", "UpDecoderBlock2D"], "latent_channels": 3, } _A : List[Any] = self.dummy_input return init_dict, inputs_dict def a__ ( self ) -> Tuple: pass def a__ ( self ) -> Optional[Any]: pass def a__ ( self ) -> Tuple: _A : Any = VQModel.from_pretrained("""fusing/vqgan-dummy""" , output_loading_info=__a ) self.assertIsNotNone(__a ) self.assertEqual(len(loading_info["""missing_keys"""] ) , 0 ) model.to(__a ) _A : List[str] = model(**self.dummy_input ) assert image is not None, "Make sure output is not None" def a__ ( self ) -> Any: _A : Dict = VQModel.from_pretrained("""fusing/vqgan-dummy""" ) model.to(__a ).eval() torch.manual_seed(0 ) if torch.cuda.is_available(): torch.cuda.manual_seed_all(0 ) _A : Dict = torch.randn(1 , model.config.in_channels , model.config.sample_size , model.config.sample_size ) _A : str = image.to(__a ) with torch.no_grad(): _A : List[str] = model(__a ).sample _A : str = output[0, -1, -3:, -3:].flatten().cpu() # fmt: off _A : str = torch.tensor([-0.0153, -0.4044, -0.1880, -0.5161, -0.2418, -0.4072, -0.1612, -0.0633, -0.0143] ) # fmt: on self.assertTrue(torch.allclose(__a , __a , atol=1e-3 ) )
707
import unittest import numpy as np from transformers.file_utils import is_torch_available, is_vision_available from transformers.testing_utils import require_torch, require_vision 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 DPTImageProcessor class lowercase ( unittest.TestCase ): def __init__( self , _a , _a=7 , _a=3 , _a=18 , _a=30 , _a=400 , _a=True , _a=None , _a=True , _a=[0.5, 0.5, 0.5] , _a=[0.5, 0.5, 0.5] , ) -> Tuple: _A : Any = size if size is not None else {"""height""": 18, """width""": 18} _A : Optional[Any] = parent _A : Union[str, Any] = batch_size _A : List[Any] = num_channels _A : List[str] = image_size _A : Optional[Any] = min_resolution _A : List[Any] = max_resolution _A : Optional[Any] = do_resize _A : str = size _A : List[str] = do_normalize _A : Dict = image_mean _A : int = image_std def a__ ( self ) -> Any: 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 ( UpperCamelCase__,unittest.TestCase ): _a = DPTImageProcessor if is_vision_available() else None def a__ ( self ) -> Optional[int]: _A : Optional[Any] = DPTImageProcessingTester(self ) @property def a__ ( self ) -> Tuple: return self.image_processor_tester.prepare_image_processor_dict() def a__ ( self ) -> Any: _A : List[str] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_a , """image_mean""" ) ) self.assertTrue(hasattr(_a , """image_std""" ) ) self.assertTrue(hasattr(_a , """do_normalize""" ) ) self.assertTrue(hasattr(_a , """do_resize""" ) ) self.assertTrue(hasattr(_a , """size""" ) ) def a__ ( self ) -> Any: _A : List[str] = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"""height""": 18, """width""": 18} ) _A : str = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {"""height""": 42, """width""": 42} ) def a__ ( self ) -> List[Any]: # Initialize image_processing _A : Optional[int] = self.image_processing_class(**self.image_processor_dict ) # create random PIL images _A : Any = prepare_image_inputs(self.image_processor_tester , equal_resolution=_a ) for image in image_inputs: self.assertIsInstance(_a , Image.Image ) # Test not batched input _A : Union[str, 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 : int = image_processing(_a , 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 a__ ( self ) -> Union[str, Any]: # Initialize image_processing _A : List[Any] = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors _A : Optional[Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=_a , numpify=_a ) for image in image_inputs: self.assertIsInstance(_a , np.ndarray ) # Test not batched input _A : Dict = 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(_a , 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 a__ ( self ) -> List[str]: # Initialize image_processing _A : Dict = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors _A : str = prepare_image_inputs(self.image_processor_tester , equal_resolution=_a , torchify=_a ) for image in image_inputs: self.assertIsInstance(_a , torch.Tensor ) # Test not batched input _A : Optional[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 : int = image_processing(_a , 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"""], ) , )
54
0
# 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 typing import TYPE_CHECKING from ..models.auto import AutoModelForVisionaSeq from ..utils import requires_backends from .base import PipelineTool if TYPE_CHECKING: from PIL import Image class lowercase ( UpperCAmelCase_ ): _a = "Salesforce/blip-image-captioning-base" _a = ( "This is a tool that generates a description of an image. It takes an input named `image` which should be the " "image to caption, and returns a text that contains the description in English." ) _a = "image_captioner" _a = AutoModelForVisionaSeq _a = ["image"] _a = ["text"] def __init__( self , *_a , **_a ) -> Any: requires_backends(self , ["""vision"""] ) super().__init__(*_snake_case , **_snake_case ) def a__ ( self , _a ) -> Union[str, Any]: return self.pre_processor(images=_snake_case , return_tensors="""pt""" ) def a__ ( self , _a ) -> List[str]: return self.model.generate(**_snake_case ) def a__ ( self , _a ) -> List[Any]: return self.pre_processor.batch_decode(_snake_case , skip_special_tokens=_snake_case )[0].strip()
708
from typing import Any, Dict, List, Optional, Tuple, Union import torch from torch import nn from torch.utils.data import DistributedSampler, RandomSampler from transformers import PreTrainedModel, Trainer, logging from transformers.integrations import is_fairscale_available from transformers.models.fsmt.configuration_fsmt import FSMTConfig from transformers.optimization import ( Adafactor, AdamW, get_constant_schedule, get_constant_schedule_with_warmup, get_cosine_schedule_with_warmup, get_cosine_with_hard_restarts_schedule_with_warmup, get_linear_schedule_with_warmup, get_polynomial_decay_schedule_with_warmup, ) from transformers.trainer_pt_utils import get_tpu_sampler from transformers.training_args import ParallelMode from transformers.utils import is_torch_tpu_available if is_fairscale_available(): from fairscale.optim import OSS _snake_case = logging.get_logger(__name__) _snake_case = { "linear": get_linear_schedule_with_warmup, "cosine": get_cosine_schedule_with_warmup, "cosine_w_restarts": get_cosine_with_hard_restarts_schedule_with_warmup, "polynomial": get_polynomial_decay_schedule_with_warmup, "constant": get_constant_schedule, "constant_w_warmup": get_constant_schedule_with_warmup, } class lowercase ( UpperCamelCase__ ): def __init__( self , _a=None , _a=None , *_a , **_a ) -> Optional[int]: super().__init__(*_a , **_a ) if config is None: assert isinstance(self.model , _a ), ( "If no `config` is passed the model to be trained has to be of type `PreTrainedModel`, but is" F''' {self.model.__class__}''' ) _A : Optional[Any] = self.model.config else: _A : int = config _A : Optional[Any] = data_args _A : int = self.config.tgt_vocab_size if isinstance(self.config , _a ) else self.config.vocab_size if self.args.label_smoothing != 0 or (self.data_args is not None and self.data_args.ignore_pad_token_for_loss): assert self.config.pad_token_id is not None, ( "Make sure that `config.pad_token_id` is correcly defined when ignoring `pad_token` for loss" " calculation or doing label smoothing." ) if self.config.pad_token_id is None and self.config.eos_token_id is not None: logger.warning( F'''The `config.pad_token_id` is `None`. Using `config.eos_token_id` = {self.config.eos_token_id} for''' """ padding..""" ) if self.args.label_smoothing == 0: _A : Optional[Any] = torch.nn.CrossEntropyLoss(ignore_index=self.config.pad_token_id ) else: # dynamically import label_smoothed_nll_loss from utils import label_smoothed_nll_loss _A : Union[str, Any] = label_smoothed_nll_loss def a__ ( self , _a ) -> int: if self.optimizer is None: _A : List[str] = ["""bias""", """LayerNorm.weight"""] _A : str = [ { """params""": [p for n, p in self.model.named_parameters() if not any(nd in n for nd in no_decay )], """weight_decay""": self.args.weight_decay, }, { """params""": [p for n, p in self.model.named_parameters() if any(nd in n for nd in no_decay )], """weight_decay""": 0.0, }, ] _A : Optional[Any] = Adafactor if self.args.adafactor else AdamW if self.args.adafactor: _A : Dict = Adafactor _A : int = {"""scale_parameter""": False, """relative_step""": False} else: _A : int = AdamW _A : Any = { """betas""": (self.args.adam_betaa, self.args.adam_betaa), """eps""": self.args.adam_epsilon, } _A : List[str] = self.args.learning_rate if self.sharded_ddp: _A : List[str] = OSS( params=_a , optim=_a , **_a , ) else: _A : Tuple = optimizer_cls(_a , **_a ) if self.lr_scheduler is None: _A : Union[str, Any] = self._get_lr_scheduler(_a ) else: # ignoring --lr_scheduler logger.warning("""scheduler is passed to `Seq2SeqTrainer`, `--lr_scheduler` arg is ignored.""" ) def a__ ( self , _a ) -> Dict: _A : List[Any] = arg_to_scheduler[self.args.lr_scheduler] if self.args.lr_scheduler == "constant": _A : Optional[Any] = schedule_func(self.optimizer ) elif self.args.lr_scheduler == "constant_w_warmup": _A : Optional[int] = schedule_func(self.optimizer , num_warmup_steps=self.args.warmup_steps ) else: _A : List[Any] = schedule_func( self.optimizer , num_warmup_steps=self.args.warmup_steps , num_training_steps=_a ) return scheduler def a__ ( self ) -> Optional[torch.utils.data.Sampler]: if isinstance(self.train_dataset , torch.utils.data.IterableDataset ): return None elif is_torch_tpu_available(): return get_tpu_sampler(self.train_dataset ) else: if self.args.sortish_sampler: self.train_dataset.make_sortish_sampler( self.args.per_device_train_batch_size , distributed=(self.args.parallel_mode == ParallelMode.DISTRIBUTED) , ) return ( RandomSampler(self.train_dataset ) if self.args.local_rank == -1 else DistributedSampler(self.train_dataset ) ) def a__ ( self , _a , _a , _a ) -> List[str]: if self.args.label_smoothing == 0: if self.data_args is not None and self.data_args.ignore_pad_token_for_loss: # force training to ignore pad token _A : List[str] = model(**_a , use_cache=_a )[0] _A : Dict = self.loss_fn(logits.view(-1 , logits.shape[-1] ) , labels.view(-1 ) ) else: # compute usual loss via models _A , _A : str = model(**_a , labels=_a , use_cache=_a )[:2] else: # compute label smoothed loss _A : Any = model(**_a , use_cache=_a )[0] _A : Union[str, Any] = torch.nn.functional.log_softmax(_a , dim=-1 ) _A , _A : List[str] = self.loss_fn(_a , _a , self.args.label_smoothing , ignore_index=self.config.pad_token_id ) return loss, logits def a__ ( self , _a , _a ) -> List[Any]: _A : Optional[int] = inputs.pop("""labels""" ) _A , _A : Dict = self._compute_loss(_a , _a , _a ) return loss def a__ ( self , _a , _a , _a , _a = None , ) -> Tuple[Optional[float], Optional[torch.Tensor], Optional[torch.Tensor]]: _A : int = self._prepare_inputs(_a ) _A : Dict = { """max_length""": self.data_args.val_max_target_length if self.data_args is not None else self.config.max_length, """num_beams""": self.data_args.eval_beams if self.data_args is not None else self.config.num_beams, } if self.args.predict_with_generate and not self.args.prediction_loss_only: _A : List[str] = self.model.generate( inputs["""input_ids"""] , attention_mask=inputs["""attention_mask"""] , **_a , ) # in case the batch is shorter than max length, the output should be padded if generated_tokens.shape[-1] < gen_kwargs["max_length"]: _A : str = self._pad_tensors_to_max_len(_a , gen_kwargs["""max_length"""] ) _A : Any = inputs.pop("""labels""" ) with torch.no_grad(): # compute loss on predict data _A , _A : Tuple = self._compute_loss(_a , _a , _a ) _A : Any = loss.mean().detach() if self.args.prediction_loss_only: return (loss, None, None) _A : List[Any] = generated_tokens if self.args.predict_with_generate else logits if labels.shape[-1] < gen_kwargs["max_length"]: _A : Any = self._pad_tensors_to_max_len(_a , gen_kwargs["""max_length"""] ) return (loss, logits, labels) def a__ ( self , _a , _a ) -> Union[str, Any]: # If PAD token is not defined at least EOS token has to be defined _A : Optional[Any] = self.config.pad_token_id if self.config.pad_token_id is not None else self.config.eos_token_id if pad_token_id is None: raise ValueError( """Make sure that either `config.pad_token_id` or `config.eos_token_id` is defined if tensor has to be""" F''' padded to `max_length`={max_length}''' ) _A : Dict = pad_token_id * torch.ones( (tensor.shape[0], max_length) , dtype=tensor.dtype , device=tensor.device ) _A : Dict = tensor return padded_tensor
54
0
import json from typing import List, Optional, Tuple from tokenizers import normalizers from ....tokenization_utils_fast import PreTrainedTokenizerFast from ....utils import logging from .tokenization_retribert import RetriBertTokenizer _snake_case = logging.get_logger(__name__) _snake_case = {"""vocab_file""": """vocab.txt""", """tokenizer_file""": """tokenizer.json"""} _snake_case = { """vocab_file""": { """yjernite/retribert-base-uncased""": ( """https://huggingface.co/yjernite/retribert-base-uncased/resolve/main/vocab.txt""" ), }, """tokenizer_file""": { """yjernite/retribert-base-uncased""": ( """https://huggingface.co/yjernite/retribert-base-uncased/resolve/main/tokenizer.json""" ), }, } _snake_case = { """yjernite/retribert-base-uncased""": 512, } _snake_case = { """yjernite/retribert-base-uncased""": {"""do_lower_case""": True}, } class lowercase ( _lowerCAmelCase ): _a = VOCAB_FILES_NAMES _a = PRETRAINED_VOCAB_FILES_MAP _a = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _a = PRETRAINED_INIT_CONFIGURATION _a = RetriBertTokenizer _a = ['input_ids', 'attention_mask'] def __init__( self , _a=None , _a=None , _a=True , _a="[UNK]" , _a="[SEP]" , _a="[PAD]" , _a="[CLS]" , _a="[MASK]" , _a=True , _a=None , **_a , ) -> List[Any]: super().__init__( _lowerCAmelCase , tokenizer_file=_lowerCAmelCase , do_lower_case=_lowerCAmelCase , unk_token=_lowerCAmelCase , sep_token=_lowerCAmelCase , pad_token=_lowerCAmelCase , cls_token=_lowerCAmelCase , mask_token=_lowerCAmelCase , tokenize_chinese_chars=_lowerCAmelCase , strip_accents=_lowerCAmelCase , **_lowerCAmelCase , ) _A : Any = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get("""lowercase""" , _lowerCAmelCase ) != do_lower_case or normalizer_state.get("""strip_accents""" , _lowerCAmelCase ) != strip_accents or normalizer_state.get("""handle_chinese_chars""" , _lowerCAmelCase ) != tokenize_chinese_chars ): _A : Optional[int] = getattr(_lowerCAmelCase , normalizer_state.pop("""type""" ) ) _A : Tuple = do_lower_case _A : Tuple = strip_accents _A : str = tokenize_chinese_chars _A : int = normalizer_class(**_lowerCAmelCase ) _A : Optional[Any] = do_lower_case def a__ ( self , _a , _a=None ) -> Optional[int]: _A : List[Any] = [self.cls_token_id] + token_ids_a + [self.sep_token_id] if token_ids_a: output += token_ids_a + [self.sep_token_id] return output def a__ ( self , _a , _a = None ) -> List[int]: _A : Dict = [self.sep_token_id] _A : Union[str, 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 ) * [0] + len(token_ids_a + sep ) * [1] def a__ ( self , _a , _a = None ) -> Tuple[str]: _A : List[str] = self._tokenizer.model.save(_lowerCAmelCase , name=_lowerCAmelCase ) return tuple(_lowerCAmelCase )
709
from collections.abc import Callable from math import pi, sqrt from random import uniform from statistics import mean def lowerCAmelCase_ ( snake_case_ ): # A local function to see if a dot lands in the circle. def is_in_circle(snake_case_,snake_case_ ) -> bool: _A : List[str] = sqrt((x**2) + (y**2) ) # Our circle has a radius of 1, so a distance # greater than 1 would land outside the circle. return distance_from_centre <= 1 # The proportion of guesses that landed in the circle _A : Optional[int] = mean( int(is_in_circle(uniform(-1.0,1.0 ),uniform(-1.0,1.0 ) ) ) for _ in range(snake_case_ ) ) # The ratio of the area for circle to square is pi/4. _A : List[str] = proportion * 4 print(f'''The estimated value of pi is {pi_estimate}''' ) print(f'''The numpy value of pi is {pi}''' ) print(f'''The total error is {abs(pi - pi_estimate )}''' ) def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_ = 0.0,snake_case_ = 1.0,): return mean( function_to_integrate(uniform(snake_case_,snake_case_ ) ) for _ in range(snake_case_ ) ) * (max_value - min_value) def lowerCAmelCase_ ( snake_case_,snake_case_ = 0.0,snake_case_ = 1.0 ): def identity_function(snake_case_ ) -> float: return x _A : Any = area_under_curve_estimator( snake_case_,snake_case_,snake_case_,snake_case_ ) _A : Tuple = (max_value * max_value - min_value * min_value) / 2 print("""******************""" ) print(f'''Estimating area under y=x where x varies from {min_value} to {max_value}''' ) print(f'''Estimated value is {estimated_value}''' ) print(f'''Expected value is {expected_value}''' ) print(f'''Total error is {abs(estimated_value - expected_value )}''' ) print("""******************""" ) def lowerCAmelCase_ ( snake_case_ ): def function_to_integrate(snake_case_ ) -> float: return sqrt(4.0 - x * x ) _A : Optional[int] = area_under_curve_estimator( snake_case_,snake_case_,0.0,2.0 ) print("""******************""" ) print("""Estimating pi using area_under_curve_estimator""" ) print(f'''Estimated value is {estimated_value}''' ) print(f'''Expected value is {pi}''' ) print(f'''Total error is {abs(estimated_value - pi )}''' ) print("""******************""" ) if __name__ == "__main__": import doctest doctest.testmod()
54
0
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 _snake_case = get_tests_dir("fixtures/test_sentencepiece_no_bos.model") @require_sentencepiece @require_tokenizers class lowercase ( SCREAMING_SNAKE_CASE_,unittest.TestCase ): _a = PegasusTokenizer _a = PegasusTokenizerFast _a = True _a = True def a__ ( self ) -> Dict: super().setUp() # We have a SentencePiece fixture for testing _A : List[str] = PegasusTokenizer(UpperCamelCase__ ) tokenizer.save_pretrained(self.tmpdirname ) @cached_property def a__ ( self ) -> Dict: return PegasusTokenizer.from_pretrained("""google/pegasus-large""" ) def a__ ( self , **_a ) -> Any: return PegasusTokenizer.from_pretrained(self.tmpdirname , **UpperCamelCase__ ) def a__ ( self , _a ) -> Dict: return ("This is a test", "This is a test") def a__ ( self ) -> List[str]: _A : List[Any] = '''</s>''' _A : List[Any] = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(UpperCamelCase__ ) , UpperCamelCase__ ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(UpperCamelCase__ ) , UpperCamelCase__ ) def a__ ( self ) -> Dict: _A : Optional[int] = 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 a__ ( self ) -> Optional[Any]: self.assertEqual(self.get_tokenizer().vocab_size , 1103 ) def a__ ( self ) -> Optional[Any]: _A : Tuple = self.rust_tokenizer_class.from_pretrained(self.tmpdirname ) _A : List[str] = self.tokenizer_class.from_pretrained(self.tmpdirname ) _A : List[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 : str = rust_tokenizer([raw_input_str] , return_tensors=UpperCamelCase__ , add_special_tokens=UpperCamelCase__ ).input_ids[0] _A : List[str] = py_tokenizer([raw_input_str] , return_tensors=UpperCamelCase__ , add_special_tokens=UpperCamelCase__ ).input_ids[0] self.assertListEqual(UpperCamelCase__ , UpperCamelCase__ ) def a__ ( self ) -> Optional[int]: _A : List[str] = self._large_tokenizer # <mask_1> masks whole sentence while <mask_2> masks single word _A : List[str] = '''<mask_1> To ensure a <mask_2> flow of bank resolutions.''' _A : List[Any] = [2, 413, 615, 114, 3, 1971, 113, 1679, 1_0710, 107, 1] _A : Union[str, Any] = tokenizer([raw_input_str] , return_tensors=UpperCamelCase__ ).input_ids[0] self.assertListEqual(UpperCamelCase__ , UpperCamelCase__ ) def a__ ( self ) -> Union[str, Any]: _A : Any = self._large_tokenizer # The tracebacks for the following asserts are **better** without messages or self.assertEqual assert tokenizer.vocab_size == 9_6103 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 : List[Any] = '''To ensure a smooth flow of bank resolutions.''' _A : Union[str, Any] = [413, 615, 114, 2291, 1971, 113, 1679, 1_0710, 107, 1] _A : List[str] = 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 a__ ( self ) -> int: _A : Union[str, Any] = ['''This is going to be way too long.''' * 150, '''short example'''] _A : Tuple = ['''not super long but more than 5 tokens''', '''tiny'''] _A : Tuple = self._large_tokenizer(UpperCamelCase__ , padding=UpperCamelCase__ , truncation=UpperCamelCase__ , return_tensors="""pt""" ) _A : 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 a__ ( self ) -> Union[str, Any]: # fmt: off _A : Optional[Any] = {'''input_ids''': [[3_8979, 143, 1_8485, 606, 130, 2_6669, 8_7686, 121, 5_4189, 1129, 111, 2_6669, 8_7686, 121, 9114, 1_4787, 121, 1_3249, 158, 592, 956, 121, 1_4621, 3_1576, 143, 6_2613, 108, 9688, 930, 4_3430, 1_1562, 6_2613, 304, 108, 1_1443, 897, 108, 9314, 1_7415, 6_3399, 108, 1_1443, 7614, 1_8316, 118, 4284, 7148, 1_2430, 143, 1400, 2_5703, 158, 111, 4284, 7148, 1_1772, 143, 2_1297, 1064, 158, 122, 204, 3506, 1754, 1133, 1_4787, 1581, 115, 3_3224, 4482, 111, 1355, 110, 2_9173, 317, 5_0833, 108, 2_0147, 9_4665, 111, 7_7198, 107, 1], [110, 6_2613, 117, 638, 112, 1133, 121, 2_0098, 1355, 7_9050, 1_3872, 135, 1596, 5_3541, 1352, 141, 1_3039, 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, 1_8289, 1_7780, 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 ( SCREAMING_SNAKE_CASE_,unittest.TestCase ): _a = PegasusTokenizer _a = PegasusTokenizerFast _a = True _a = True def a__ ( self ) -> int: super().setUp() # We have a SentencePiece fixture for testing _A : Optional[int] = PegasusTokenizer(UpperCamelCase__ , offset=0 , mask_token_sent=UpperCamelCase__ , mask_token="""[MASK]""" ) tokenizer.save_pretrained(self.tmpdirname ) @cached_property def a__ ( self ) -> str: return PegasusTokenizer.from_pretrained("""google/bigbird-pegasus-large-arxiv""" ) def a__ ( self , **_a ) -> List[Any]: return PegasusTokenizer.from_pretrained(self.tmpdirname , **UpperCamelCase__ ) def a__ ( self , _a ) -> List[Any]: return ("This is a test", "This is a test") def a__ ( self ) -> List[str]: _A : Any = self.rust_tokenizer_class.from_pretrained(self.tmpdirname ) _A : Tuple = self.tokenizer_class.from_pretrained(self.tmpdirname ) _A : Tuple = ( '''Let\'s see which <unk> is the better <unk_token> one [MASK] It seems like this [MASK] was important </s>''' ''' <pad> <pad> <pad>''' ) _A : List[str] = rust_tokenizer([raw_input_str] , return_tensors=UpperCamelCase__ , add_special_tokens=UpperCamelCase__ ).input_ids[0] _A : str = py_tokenizer([raw_input_str] , return_tensors=UpperCamelCase__ , add_special_tokens=UpperCamelCase__ ).input_ids[0] self.assertListEqual(UpperCamelCase__ , UpperCamelCase__ ) @require_torch def a__ ( self ) -> List[str]: _A : Dict = ['''This is going to be way too long.''' * 1000, '''short example'''] _A : str = ['''not super long but more than 5 tokens''', '''tiny'''] _A : List[Any] = self._large_tokenizer(UpperCamelCase__ , padding=UpperCamelCase__ , truncation=UpperCamelCase__ , return_tensors="""pt""" ) _A : Dict = 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 a__ ( self ) -> List[Any]: _A : Tuple = ( '''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, 2_5016, 3137, 464, 109, 2_6955, 3137, 1] , )
710
import json import os import shutil import tempfile from unittest import TestCase from transformers import BartTokenizer, BartTokenizerFast, DPRQuestionEncoderTokenizer, DPRQuestionEncoderTokenizerFast from transformers.models.bart.configuration_bart import BartConfig from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES as DPR_VOCAB_FILES_NAMES from transformers.models.dpr.configuration_dpr import DPRConfig from transformers.models.roberta.tokenization_roberta import VOCAB_FILES_NAMES as BART_VOCAB_FILES_NAMES from transformers.testing_utils import require_faiss, require_tokenizers, require_torch, slow from transformers.utils import is_datasets_available, is_faiss_available, is_torch_available if is_torch_available() and is_datasets_available() and is_faiss_available(): from transformers.models.rag.configuration_rag import RagConfig from transformers.models.rag.tokenization_rag import RagTokenizer @require_faiss @require_torch class lowercase ( UpperCamelCase__ ): def a__ ( self ) -> int: _A : int = tempfile.mkdtemp() _A : Union[str, Any] = 8 # DPR tok _A : List[str] = [ """[UNK]""", """[CLS]""", """[SEP]""", """[PAD]""", """[MASK]""", """want""", """##want""", """##ed""", """wa""", """un""", """runn""", """##ing""", """,""", """low""", """lowest""", ] _A : List[str] = os.path.join(self.tmpdirname , """dpr_tokenizer""" ) os.makedirs(_a , exist_ok=_a ) _A : str = os.path.join(_a , DPR_VOCAB_FILES_NAMES["""vocab_file"""] ) with open(self.vocab_file , """w""" , encoding="""utf-8""" ) as vocab_writer: vocab_writer.write("""""".join([x + """\n""" for x in vocab_tokens] ) ) # BART tok _A : Dict = [ """l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """\u0120""", """\u0120l""", """\u0120n""", """\u0120lo""", """\u0120low""", """er""", """\u0120lowest""", """\u0120newer""", """\u0120wider""", """<unk>""", ] _A : Optional[Any] = dict(zip(_a , range(len(_a ) ) ) ) _A : Tuple = ["""#version: 0.2""", """\u0120 l""", """\u0120l o""", """\u0120lo w""", """e r""", """"""] _A : Dict = {"""unk_token""": """<unk>"""} _A : Optional[Any] = os.path.join(self.tmpdirname , """bart_tokenizer""" ) os.makedirs(_a , exist_ok=_a ) _A : str = os.path.join(_a , BART_VOCAB_FILES_NAMES["""vocab_file"""] ) _A : List[Any] = os.path.join(_a , BART_VOCAB_FILES_NAMES["""merges_file"""] ) with open(self.vocab_file , """w""" , encoding="""utf-8""" ) as fp: fp.write(json.dumps(_a ) + """\n""" ) with open(self.merges_file , """w""" , encoding="""utf-8""" ) as fp: fp.write("""\n""".join(_a ) ) def a__ ( self ) -> DPRQuestionEncoderTokenizer: return DPRQuestionEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname , """dpr_tokenizer""" ) ) def a__ ( self ) -> BartTokenizer: return BartTokenizer.from_pretrained(os.path.join(self.tmpdirname , """bart_tokenizer""" ) ) def a__ ( self ) -> Optional[Any]: shutil.rmtree(self.tmpdirname ) @require_tokenizers def a__ ( self ) -> str: _A : Optional[Any] = os.path.join(self.tmpdirname , """rag_tokenizer""" ) _A : int = RagConfig(question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() ) _A : Any = RagTokenizer(question_encoder=self.get_dpr_tokenizer() , generator=self.get_bart_tokenizer() ) rag_config.save_pretrained(_a ) rag_tokenizer.save_pretrained(_a ) _A : Optional[Any] = RagTokenizer.from_pretrained(_a , config=_a ) self.assertIsInstance(new_rag_tokenizer.question_encoder , _a ) self.assertEqual(new_rag_tokenizer.question_encoder.get_vocab() , rag_tokenizer.question_encoder.get_vocab() ) self.assertIsInstance(new_rag_tokenizer.generator , _a ) self.assertEqual(new_rag_tokenizer.generator.get_vocab() , rag_tokenizer.generator.get_vocab() ) @slow def a__ ( self ) -> str: _A : Union[str, Any] = RagTokenizer.from_pretrained("""facebook/rag-token-nq""" ) _A : Tuple = [ """who got the first nobel prize in physics""", """when is the next deadpool movie being released""", """which mode is used for short wave broadcast service""", """who is the owner of reading football club""", """when is the next scandal episode coming out""", """when is the last time the philadelphia won the superbowl""", """what is the most current adobe flash player version""", """how many episodes are there in dragon ball z""", """what is the first step in the evolution of the eye""", """where is gall bladder situated in human body""", """what is the main mineral in lithium batteries""", """who is the president of usa right now""", """where do the greasers live in the outsiders""", """panda is a national animal of which country""", """what is the name of manchester united stadium""", ] _A : Tuple = tokenizer(_a ) self.assertIsNotNone(_a ) @slow def a__ ( self ) -> Dict: _A : Dict = RagTokenizer.from_pretrained("""facebook/rag-sequence-nq""" ) _A : str = [ """who got the first nobel prize in physics""", """when is the next deadpool movie being released""", """which mode is used for short wave broadcast service""", """who is the owner of reading football club""", """when is the next scandal episode coming out""", """when is the last time the philadelphia won the superbowl""", """what is the most current adobe flash player version""", """how many episodes are there in dragon ball z""", """what is the first step in the evolution of the eye""", """where is gall bladder situated in human body""", """what is the main mineral in lithium batteries""", """who is the president of usa right now""", """where do the greasers live in the outsiders""", """panda is a national animal of which country""", """what is the name of manchester united stadium""", ] _A : Optional[Any] = tokenizer(_a ) self.assertIsNotNone(_a )
54
0
def lowerCAmelCase_ ( snake_case_,snake_case_ ): return base * power(lowerCamelCase_,(exponent - 1) ) if exponent else 1 if __name__ == "__main__": print("Raise base to the power of exponent using recursion...") _snake_case = int(input("Enter the base: ").strip()) _snake_case = int(input("Enter the exponent: ").strip()) _snake_case = power(base, abs(exponent)) if exponent < 0: # power() does not properly deal w/ negative exponents _snake_case = 1 / result print(f"""{base} to the power of {exponent} is {result}""")
711
import os import re import shutil import sys import tempfile import unittest import black _snake_case = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) sys.path.append(os.path.join(git_repo_path, "utils")) import check_copies # noqa: E402 # This is the reference code that will be used in the tests. # If BertLMPredictionHead is changed in modeling_bert.py, this code needs to be manually updated. _snake_case = " def __init__(self, config):\n super().__init__()\n self.transform = BertPredictionHeadTransform(config)\n\n # The output weights are the same as the input embeddings, but there is\n # an output-only bias for each token.\n self.decoder = nn.Linear(config.hidden_size, config.vocab_size, bias=False)\n\n self.bias = nn.Parameter(torch.zeros(config.vocab_size))\n\n # Need a link between the two variables so that the bias is correctly resized with `resize_token_embeddings`\n self.decoder.bias = self.bias\n\n def forward(self, hidden_states):\n hidden_states = self.transform(hidden_states)\n hidden_states = self.decoder(hidden_states)\n return hidden_states\n" class lowercase ( unittest.TestCase ): def a__ ( self ) -> Union[str, Any]: _A : List[str] = tempfile.mkdtemp() os.makedirs(os.path.join(self.transformer_dir , """models/bert/""" ) ) _A : str = self.transformer_dir shutil.copy( os.path.join(_a , """src/transformers/models/bert/modeling_bert.py""" ) , os.path.join(self.transformer_dir , """models/bert/modeling_bert.py""" ) , ) def a__ ( self ) -> Optional[int]: _A : List[str] = """src/transformers""" shutil.rmtree(self.transformer_dir ) def a__ ( self , _a , _a , _a , _a=None ) -> Optional[Any]: _A : Optional[Any] = comment + F'''\nclass {class_name}(nn.Module):\n''' + class_code if overwrite_result is not None: _A : List[str] = comment + F'''\nclass {class_name}(nn.Module):\n''' + overwrite_result _A : List[str] = black.Mode(target_versions={black.TargetVersion.PYaa} , line_length=119 ) _A : Optional[int] = black.format_str(_a , mode=_a ) _A : Optional[Any] = os.path.join(self.transformer_dir , """new_code.py""" ) with open(_a , """w""" , newline="""\n""" ) as f: f.write(_a ) if overwrite_result is None: self.assertTrue(len(check_copies.is_copy_consistent(_a ) ) == 0 ) else: check_copies.is_copy_consistent(f.name , overwrite=_a ) with open(_a , """r""" ) as f: self.assertTrue(f.read() , _a ) def a__ ( self ) -> str: _A : Union[str, Any] = check_copies.find_code_in_transformers("""models.bert.modeling_bert.BertLMPredictionHead""" ) self.assertEqual(_a , _a ) def a__ ( self ) -> int: # Base copy consistency self.check_copy_consistency( """# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead""" , """BertLMPredictionHead""" , REFERENCE_CODE + """\n""" , ) # With no empty line at the end self.check_copy_consistency( """# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead""" , """BertLMPredictionHead""" , _a , ) # Copy consistency with rename self.check_copy_consistency( """# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->TestModel""" , """TestModelLMPredictionHead""" , re.sub("""Bert""" , """TestModel""" , _a ) , ) # Copy consistency with a really long name _A : List[str] = """TestModelWithAReallyLongNameBecauseSomePeopleLikeThatForSomeReason""" self.check_copy_consistency( F'''# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->{long_class_name}''' , F'''{long_class_name}LMPredictionHead''' , re.sub("""Bert""" , _a , _a ) , ) # Copy consistency with overwrite self.check_copy_consistency( """# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->TestModel""" , """TestModelLMPredictionHead""" , _a , overwrite_result=re.sub("""Bert""" , """TestModel""" , _a ) , ) def a__ ( self ) -> Tuple: _A : Union[str, Any] = check_copies.LOCALIZED_READMES["""README_zh-hans.md"""] _A : str = ( """1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (from Google Research and the""" """ Toyota Technological Institute at Chicago) released with the paper [ALBERT: A Lite BERT for""" """ Self-supervised Learning of Language Representations](https://arxiv.org/abs/1909.11942), by Zhenzhong""" """ Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut.\n1.""" """ **[DistilBERT](https://huggingface.co/transformers/model_doc/distilbert.html)** (from HuggingFace),""" """ released together with the paper [DistilBERT, a distilled version of BERT: smaller, faster, cheaper and""" """ lighter](https://arxiv.org/abs/1910.01108) by Victor Sanh, Lysandre Debut and Thomas Wolf. The same""" """ method has been applied to compress GPT2 into""" """ [DistilGPT2](https://github.com/huggingface/transformers/tree/main/examples/distillation), RoBERTa into""" """ [DistilRoBERTa](https://github.com/huggingface/transformers/tree/main/examples/distillation),""" """ Multilingual BERT into""" """ [DistilmBERT](https://github.com/huggingface/transformers/tree/main/examples/distillation) and a German""" """ version of DistilBERT.\n1. **[ELECTRA](https://huggingface.co/transformers/model_doc/electra.html)**""" """ (from Google Research/Stanford University) released with the paper [ELECTRA: Pre-training text encoders""" """ as discriminators rather than generators](https://arxiv.org/abs/2003.10555) by Kevin Clark, Minh-Thang""" """ Luong, Quoc V. Le, Christopher D. Manning.""" ) _A : str = ( """1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the""" """ Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of""" """ Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian""" """ Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n""" ) _A : Any = ( """1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the""" """ Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of""" """ Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian""" """ Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n1.""" """ **[DistilBERT](https://huggingface.co/transformers/model_doc/distilbert.html)** (来自 HuggingFace) 伴随论文""" """ [DistilBERT, a distilled version of BERT: smaller, faster, cheaper and""" """ lighter](https://arxiv.org/abs/1910.01108) 由 Victor Sanh, Lysandre Debut and Thomas Wolf 发布。 The same""" """ method has been applied to compress GPT2 into""" """ [DistilGPT2](https://github.com/huggingface/transformers/tree/main/examples/distillation), RoBERTa into""" """ [DistilRoBERTa](https://github.com/huggingface/transformers/tree/main/examples/distillation),""" """ Multilingual BERT into""" """ [DistilmBERT](https://github.com/huggingface/transformers/tree/main/examples/distillation) and a German""" """ version of DistilBERT.\n1. **[ELECTRA](https://huggingface.co/transformers/model_doc/electra.html)** (来自""" """ Google Research/Stanford University) 伴随论文 [ELECTRA: Pre-training text encoders as discriminators rather""" """ than generators](https://arxiv.org/abs/2003.10555) 由 Kevin Clark, Minh-Thang Luong, Quoc V. Le,""" """ Christopher D. Manning 发布。\n""" ) _A , _A : Tuple = check_copies.convert_to_localized_md( _a , _a , localized_readme["""format_model_list"""] ) self.assertFalse(_a ) self.assertEqual(_a , _a ) _A , _A : List[str] = check_copies.convert_to_localized_md( _a , _a , localized_readme["""format_model_list"""] ) # Check whether the number of models is equal to README.md after conversion. self.assertTrue(_a ) _A : Tuple = ( """1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (from Google Research and the""" """ Toyota Technological Institute at Chicago) released with the paper [ALBERT: A Lite BERT for""" """ Self-supervised Learning of Language Representations](https://arxiv.org/abs/1909.11942), by Zhenzhong""" """ Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut.""" ) _A : Dict = ( """1. **[ALBERT](https://huggingface.co/transformers/main/model_doc/albert.html)** (来自 Google Research and""" """ the Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of""" """ Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian""" """ Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n""" ) _A : Optional[Any] = ( """1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the""" """ Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of""" """ Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian""" """ Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n""" ) _A , _A : Optional[int] = check_copies.convert_to_localized_md( _a , _a , localized_readme["""format_model_list"""] ) # Check if the model link is synchronized. self.assertEqual(_a , _a )
54
0
from collections import defaultdict def lowerCAmelCase_ ( snake_case_ ): _A : List[Any] = 1 _A : int = True for v in tree[start]: if v not in visited: ret += dfs(__snake_case ) if ret % 2 == 0: cuts.append(__snake_case ) return ret def lowerCAmelCase_ ( ): dfs(1 ) if __name__ == "__main__": _snake_case = 10, 9 _snake_case = defaultdict(list) _snake_case = {} _snake_case = [] _snake_case = 0 _snake_case = [(2, 1), (3, 1), (4, 3), (5, 2), (6, 1), (7, 2), (8, 6), (9, 8), (10, 8)] for u, v in edges: tree[u].append(v) tree[v].append(u) even_tree() print(len(cuts) - 1)
712
import itertools import os import random import tempfile import unittest import numpy as np from transformers import TvltFeatureExtractor, is_datasets_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_torch_available(): import torch if is_datasets_available(): from datasets import load_dataset _snake_case = random.Random() def lowerCAmelCase_ ( snake_case_,snake_case_=1.0,snake_case_=None,snake_case_=None ): if rng is None: _A : str = global_rng _A : List[Any] = [] for batch_idx in range(shape[0] ): values.append([] ) for _ in range(shape[1] ): values[-1].append(rng.random() * scale ) return values class lowercase ( unittest.TestCase ): def __init__( self , _a , _a=7 , _a=400 , _a=2000 , _a=2048 , _a=128 , _a=1 , _a=512 , _a=30 , _a=4_4100 , ) -> Tuple: _A : Any = parent _A : str = batch_size _A : Union[str, Any] = min_seq_length _A : int = max_seq_length _A : List[str] = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) _A : Tuple = spectrogram_length _A : int = feature_size _A : str = num_audio_channels _A : Tuple = hop_length _A : List[str] = chunk_length _A : Union[str, Any] = sampling_rate def a__ ( self ) -> Tuple: return { "spectrogram_length": self.spectrogram_length, "feature_size": self.feature_size, "num_audio_channels": self.num_audio_channels, "hop_length": self.hop_length, "chunk_length": self.chunk_length, "sampling_rate": self.sampling_rate, } def a__ ( self , _a=False , _a=False ) -> Optional[int]: def _flatten(_a ): return list(itertools.chain(*_a ) ) if equal_length: _A : List[str] = [floats_list((self.max_seq_length, self.feature_size) ) for _ in range(self.batch_size )] else: # make sure that inputs increase in size _A : Union[str, 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 : List[Any] = [np.asarray(_a ) for x in speech_inputs] return speech_inputs @require_torch @require_torchaudio class lowercase ( UpperCamelCase__,unittest.TestCase ): _a = TvltFeatureExtractor def a__ ( self ) -> Any: _A : int = TvltFeatureExtractionTester(self ) def a__ ( self ) -> List[Any]: _A : Tuple = self.feature_extraction_class(**self.feat_extract_dict ) self.assertTrue(hasattr(_a , """spectrogram_length""" ) ) self.assertTrue(hasattr(_a , """feature_size""" ) ) self.assertTrue(hasattr(_a , """num_audio_channels""" ) ) self.assertTrue(hasattr(_a , """hop_length""" ) ) self.assertTrue(hasattr(_a , """chunk_length""" ) ) self.assertTrue(hasattr(_a , """sampling_rate""" ) ) def a__ ( self ) -> Optional[int]: _A : str = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: _A : Tuple = feat_extract_first.save_pretrained(_a )[0] check_json_file_has_correct_format(_a ) _A : Optional[int] = self.feature_extraction_class.from_pretrained(_a ) _A : Optional[Any] = feat_extract_first.to_dict() _A : int = feat_extract_second.to_dict() _A : int = dict_first.pop("""mel_filters""" ) _A : Optional[int] = dict_second.pop("""mel_filters""" ) self.assertTrue(np.allclose(_a , _a ) ) self.assertEqual(_a , _a ) def a__ ( self ) -> int: _A : str = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: _A : List[str] = os.path.join(_a , """feat_extract.json""" ) feat_extract_first.to_json_file(_a ) _A : Union[str, Any] = self.feature_extraction_class.from_json_file(_a ) _A : Optional[Any] = feat_extract_first.to_dict() _A : Union[str, Any] = feat_extract_second.to_dict() _A : List[Any] = dict_first.pop("""mel_filters""" ) _A : Optional[Any] = dict_second.pop("""mel_filters""" ) self.assertTrue(np.allclose(_a , _a ) ) self.assertEqual(_a , _a ) def a__ ( self ) -> Optional[Any]: # Initialize feature_extractor _A : Tuple = self.feature_extraction_class(**self.feat_extract_dict ) # create three inputs of length 800, 1000, and 1200 _A : Optional[int] = [floats_list((1, x) )[0] for x in range(800 , 1400 , 200 )] _A : List[str] = [np.asarray(_a ) for speech_input in speech_inputs] # Test not batched input _A : Optional[int] = feature_extractor(np_speech_inputs[0] , return_tensors="""np""" , sampling_rate=4_4100 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test batched _A : str = feature_extractor(_a , return_tensors="""np""" , sampling_rate=4_4100 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test audio masking _A : Optional[Any] = feature_extractor( _a , return_tensors="""np""" , sampling_rate=4_4100 , mask_audio=_a ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test 2-D numpy arrays are batched. _A : Dict = [floats_list((1, x) )[0] for x in (800, 800, 800)] _A : Optional[int] = np.asarray(_a ) _A : Optional[Any] = feature_extractor(_a , return_tensors="""np""" , sampling_rate=4_4100 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) def a__ ( self , _a ) -> str: _A : Optional[Any] = load_dataset("""hf-internal-testing/librispeech_asr_dummy""" , """clean""" , split="""validation""" ) # automatic decoding with librispeech _A : Dict = ds.sort("""id""" ).select(range(_a ) )[:num_samples]["""audio"""] return [x["array"] for x in speech_samples] def a__ ( self ) -> Optional[Any]: _A : List[str] = self._load_datasamples(1 ) _A : List[str] = TvltFeatureExtractor() _A : List[Any] = feature_extractor(_a , return_tensors="""pt""" ).audio_values self.assertEquals(audio_values.shape , (1, 1, 192, 128) ) _A : int = torch.tensor([[-0.3032, -0.2708], [-0.4434, -0.4007]] ) self.assertTrue(torch.allclose(audio_values[0, 0, :2, :2] , _a , atol=1e-4 ) )
54
0
import os from pathlib import Path def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_ ): '''simple docstring''' _A : List[Any] = { """en""": """Machine learning is great, isn\'t it?""", """ru""": """Машинное обучение - это здорово, не так ли?""", """de""": """Maschinelles Lernen ist großartig, oder?""", } # BLUE scores as follows: # "pair": [fairseq, transformers] _A : List[Any] = { """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"""], } _A : Dict = f'''{src_lang}-{tgt_lang}''' _A : Optional[Any] = 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(a__,exist_ok=a__ ) _A : Tuple = os.path.join(a__,"""README.md""" ) print(f'''Generating {path}''' ) with open(a__,"""w""",encoding="""utf-8""" ) as f: f.write(a__ ) # make sure we are under the root of the project _snake_case = Path(__file__).resolve().parent.parent.parent _snake_case = repo_dir / "model_cards" for model_name in ["wmt19-ru-en", "wmt19-en-ru", "wmt19-en-de", "wmt19-de-en"]: _snake_case , _snake_case , _snake_case = model_name.split("-") _snake_case = model_cards_dir / "facebook" / model_name write_model_card(model_card_dir, src_lang=src_lang, tgt_lang=tgt_lang)
713
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available _snake_case = { "configuration_time_series_transformer": [ "TIME_SERIES_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", "TimeSeriesTransformerConfig", ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = [ "TIME_SERIES_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "TimeSeriesTransformerForPrediction", "TimeSeriesTransformerModel", "TimeSeriesTransformerPreTrainedModel", ] if TYPE_CHECKING: from .configuration_time_series_transformer import ( TIME_SERIES_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TimeSeriesTransformerConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_time_series_transformer import ( TIME_SERIES_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TimeSeriesTransformerForPrediction, TimeSeriesTransformerModel, TimeSeriesTransformerPreTrainedModel, ) else: import sys _snake_case = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
54
0
from __future__ import annotations import unittest from transformers import DistilBertConfig, 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.models.distilbert.modeling_tf_distilbert import ( TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST, TFDistilBertForMaskedLM, TFDistilBertForMultipleChoice, TFDistilBertForQuestionAnswering, TFDistilBertForSequenceClassification, TFDistilBertForTokenClassification, TFDistilBertModel, ) class lowercase : def __init__( self , _a , ) -> Union[str, Any]: _A : Optional[int] = parent _A : Dict = 13 _A : List[Any] = 7 _A : Any = True _A : Optional[Any] = True _A : List[str] = False _A : Optional[Any] = True _A : Dict = 99 _A : Optional[Any] = 32 _A : List[str] = 2 _A : int = 4 _A : Optional[int] = 37 _A : int = """gelu""" _A : str = 0.1 _A : List[str] = 0.1 _A : Union[str, Any] = 512 _A : List[Any] = 16 _A : List[Any] = 2 _A : Optional[int] = 0.02 _A : Optional[Any] = 3 _A : List[str] = 4 _A : Tuple = None def a__ ( self ) -> str: _A : Optional[int] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _A : Any = None if self.use_input_mask: _A : Dict = random_attention_mask([self.batch_size, self.seq_length] ) _A : str = None _A : Tuple = None _A : List[Any] = None if self.use_labels: _A : str = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _A : Tuple = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) _A : Union[str, Any] = ids_tensor([self.batch_size] , self.num_choices ) _A : Union[str, Any] = DistilBertConfig( vocab_size=self.vocab_size , dim=self.hidden_size , n_layers=self.num_hidden_layers , n_heads=self.num_attention_heads , hidden_dim=self.intermediate_size , hidden_act=self.hidden_act , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , ) return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels def a__ ( self , _a , _a , _a , _a , _a , _a ) -> List[Any]: _A : Tuple = TFDistilBertModel(config=__A ) _A : Tuple = {"""input_ids""": input_ids, """attention_mask""": input_mask} _A : Union[str, Any] = model(__A ) _A : Dict = [input_ids, input_mask] _A : str = model(__A ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def a__ ( self , _a , _a , _a , _a , _a , _a ) -> str: _A : List[Any] = TFDistilBertForMaskedLM(config=__A ) _A : Any = {"""input_ids""": input_ids, """attention_mask""": input_mask} _A : List[str] = model(__A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def a__ ( self , _a , _a , _a , _a , _a , _a ) -> Optional[int]: _A : int = TFDistilBertForQuestionAnswering(config=__A ) _A : Optional[Any] = { """input_ids""": input_ids, """attention_mask""": input_mask, } _A : List[Any] = model(__A ) 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 a__ ( self , _a , _a , _a , _a , _a , _a ) -> Tuple: _A : Any = self.num_labels _A : Optional[int] = TFDistilBertForSequenceClassification(__A ) _A : Dict = {"""input_ids""": input_ids, """attention_mask""": input_mask} _A : Optional[Any] = model(__A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def a__ ( self , _a , _a , _a , _a , _a , _a ) -> List[Any]: _A : Optional[int] = self.num_choices _A : Any = TFDistilBertForMultipleChoice(__A ) _A : List[Any] = tf.tile(tf.expand_dims(__A , 1 ) , (1, self.num_choices, 1) ) _A : Union[str, Any] = tf.tile(tf.expand_dims(__A , 1 ) , (1, self.num_choices, 1) ) _A : int = { """input_ids""": multiple_choice_inputs_ids, """attention_mask""": multiple_choice_input_mask, } _A : List[Any] = model(__A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def a__ ( self , _a , _a , _a , _a , _a , _a ) -> str: _A : Optional[int] = self.num_labels _A : Dict = TFDistilBertForTokenClassification(__A ) _A : Union[str, Any] = {"""input_ids""": input_ids, """attention_mask""": input_mask} _A : Union[str, Any] = model(__A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def a__ ( self ) -> List[str]: _A : Dict = self.prepare_config_and_inputs() ((_A) , (_A) , (_A) , (_A) , (_A) , (_A)) : str = config_and_inputs _A : Optional[int] = {"""input_ids""": input_ids, """attention_mask""": input_mask} return config, inputs_dict @require_tf class lowercase ( UpperCamelCase__,UpperCamelCase__,unittest.TestCase ): _a = ( ( TFDistilBertModel, TFDistilBertForMaskedLM, TFDistilBertForQuestionAnswering, TFDistilBertForSequenceClassification, TFDistilBertForTokenClassification, TFDistilBertForMultipleChoice, ) if is_tf_available() else None ) _a = ( { "feature-extraction": TFDistilBertModel, "fill-mask": TFDistilBertForMaskedLM, "question-answering": TFDistilBertForQuestionAnswering, "text-classification": TFDistilBertForSequenceClassification, "token-classification": TFDistilBertForTokenClassification, "zero-shot": TFDistilBertForSequenceClassification, } if is_tf_available() else {} ) _a = False _a = False def a__ ( self ) -> Optional[Any]: _A : int = TFDistilBertModelTester(self ) _A : Optional[int] = ConfigTester(self , config_class=__A , dim=37 ) def a__ ( self ) -> Optional[int]: self.config_tester.run_common_tests() def a__ ( self ) -> Any: _A : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_model(*__A ) def a__ ( self ) -> str: _A : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_for_masked_lm(*__A ) def a__ ( self ) -> Dict: _A : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_for_question_answering(*__A ) def a__ ( self ) -> Optional[Any]: _A : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_for_sequence_classification(*__A ) def a__ ( self ) -> str: _A : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_for_multiple_choice(*__A ) def a__ ( self ) -> int: _A : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_for_token_classification(*__A ) @slow def a__ ( self ) -> Dict: for model_name in list(TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1] ): _A : Tuple = TFDistilBertModel.from_pretrained(__A ) self.assertIsNotNone(__A ) @require_tf class lowercase ( unittest.TestCase ): @slow def a__ ( self ) -> Optional[Any]: _A : Optional[Any] = TFDistilBertModel.from_pretrained("""distilbert-base-uncased""" ) _A : Union[str, Any] = tf.constant([[0, 1, 2, 3, 4, 5]] ) _A : str = model(__A )[0] _A : str = [1, 6, 768] self.assertEqual(output.shape , __A ) _A : Union[str, Any] = tf.constant( [ [ [0.19261885, -0.13732955, 0.4119799], [0.22150156, -0.07422661, 0.39037204], [0.22756018, -0.0896414, 0.3701467], ] ] ) tf.debugging.assert_near(output[:, :3, :3] , __A , atol=1e-4 )
714
import gc import random import unittest import numpy as np import torch from PIL import Image from diffusers import ( DDIMScheduler, KandinskyVaaImgaImgPipeline, KandinskyVaaPriorPipeline, UNetaDConditionModel, VQModel, ) 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 lowercase ( UpperCamelCase__,unittest.TestCase ): _a = KandinskyVaaImgaImgPipeline _a = ["image_embeds", "negative_image_embeds", "image"] _a = [ "image_embeds", "negative_image_embeds", "image", ] _a = [ "generator", "height", "width", "strength", "guidance_scale", "num_inference_steps", "return_dict", "guidance_scale", "num_images_per_prompt", "output_type", "return_dict", ] _a = False @property def a__ ( self ) -> int: return 32 @property def a__ ( self ) -> Union[str, Any]: return 32 @property def a__ ( self ) -> List[str]: return self.time_input_dim @property def a__ ( self ) -> Union[str, Any]: return self.time_input_dim * 4 @property def a__ ( self ) -> str: return 100 @property def a__ ( self ) -> Tuple: torch.manual_seed(0 ) _A : str = { """in_channels""": 4, # Out channels is double in channels because predicts mean and variance """out_channels""": 8, """addition_embed_type""": """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""": """image_proj""", """cross_attention_dim""": self.cross_attention_dim, """attention_head_dim""": 4, """resnet_time_scale_shift""": """scale_shift""", """class_embed_type""": None, } _A : Union[str, Any] = UNetaDConditionModel(**_a ) return model @property def a__ ( self ) -> int: 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 a__ ( self ) -> Tuple: torch.manual_seed(0 ) _A : Dict = VQModel(**self.dummy_movq_kwargs ) return model def a__ ( self ) -> int: _A : Any = self.dummy_unet _A : List[Any] = self.dummy_movq _A : str = { """num_train_timesteps""": 1000, """beta_schedule""": """linear""", """beta_start""": 0.00085, """beta_end""": 0.012, """clip_sample""": False, """set_alpha_to_one""": False, """steps_offset""": 0, """prediction_type""": """epsilon""", """thresholding""": False, } _A : int = DDIMScheduler(**_a ) _A : Tuple = { """unet""": unet, """scheduler""": scheduler, """movq""": movq, } return components def a__ ( self , _a , _a=0 ) -> str: _A : Optional[int] = floats_tensor((1, self.text_embedder_hidden_size) , rng=random.Random(_a ) ).to(_a ) _A : Dict = floats_tensor((1, self.text_embedder_hidden_size) , rng=random.Random(seed + 1 ) ).to( _a ) # create init_image _A : Optional[Any] = floats_tensor((1, 3, 64, 64) , rng=random.Random(_a ) ).to(_a ) _A : Optional[Any] = image.cpu().permute(0 , 2 , 3 , 1 )[0] _A : Optional[Any] = Image.fromarray(np.uinta(_a ) ).convert("""RGB""" ).resize((256, 256) ) if str(_a ).startswith("""mps""" ): _A : Tuple = torch.manual_seed(_a ) else: _A : str = torch.Generator(device=_a ).manual_seed(_a ) _A : Optional[Any] = { """image""": init_image, """image_embeds""": image_embeds, """negative_image_embeds""": negative_image_embeds, """generator""": generator, """height""": 64, """width""": 64, """num_inference_steps""": 10, """guidance_scale""": 7.0, """strength""": 0.2, """output_type""": """np""", } return inputs def a__ ( self ) -> Union[str, Any]: _A : Dict = """cpu""" _A : int = self.get_dummy_components() _A : Optional[int] = self.pipeline_class(**_a ) _A : Any = pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) _A : List[Any] = pipe(**self.get_dummy_inputs(_a ) ) _A : Dict = output.images _A : List[str] = pipe( **self.get_dummy_inputs(_a ) , return_dict=_a , )[0] _A : Dict = image[0, -3:, -3:, -1] _A : Union[str, Any] = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) _A : Optional[int] = np.array( [0.6199778, 0.63984406, 0.46145785, 0.62944984, 0.5622215, 0.47306132, 0.47441456, 0.4607606, 0.48719263] ) 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()}''' @slow @require_torch_gpu class lowercase ( unittest.TestCase ): def a__ ( self ) -> List[Any]: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def a__ ( self ) -> List[str]: _A : Any = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/kandinskyv22/kandinskyv22_img2img_frog.npy""" ) _A : Union[str, Any] = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/kandinsky/cat.png""" ) _A : Dict = """A red cartoon frog, 4k""" _A : Dict = KandinskyVaaPriorPipeline.from_pretrained( """kandinsky-community/kandinsky-2-2-prior""" , torch_dtype=torch.floataa ) pipe_prior.to(_a ) _A : int = KandinskyVaaImgaImgPipeline.from_pretrained( """kandinsky-community/kandinsky-2-2-decoder""" , torch_dtype=torch.floataa ) _A : Dict = pipeline.to(_a ) pipeline.set_progress_bar_config(disable=_a ) _A : Tuple = torch.Generator(device="""cpu""" ).manual_seed(0 ) _A , _A : List[str] = pipe_prior( _a , generator=_a , num_inference_steps=5 , negative_prompt="""""" , ).to_tuple() _A : int = pipeline( image=_a , image_embeds=_a , negative_image_embeds=_a , generator=_a , num_inference_steps=100 , height=768 , width=768 , strength=0.2 , output_type="""np""" , ) _A : Optional[int] = output.images[0] assert image.shape == (768, 768, 3) assert_mean_pixel_difference(_a , _a )
54
0
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 = (((515, 22, 13), 555), ((61, 35, 49), 150)) _snake_case = [2, 4, 1, 5] _snake_case = len(train_data) _snake_case = 0.0_0_9 def lowerCAmelCase_ ( snake_case_,snake_case_="train" ): return calculate_hypothesis_value(_UpperCAmelCase,_UpperCAmelCase ) - output( _UpperCAmelCase,_UpperCAmelCase ) def lowerCAmelCase_ ( snake_case_ ): _A : int = 0 for i in range(len(_UpperCAmelCase ) - 1 ): hyp_val += data_input_tuple[i] * parameter_vector[i + 1] hyp_val += parameter_vector[0] return hyp_val def lowerCAmelCase_ ( snake_case_,snake_case_ ): if data_set == "train": return train_data[example_no][1] elif data_set == "test": return test_data[example_no][1] return None def lowerCAmelCase_ ( snake_case_,snake_case_ ): 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_ ( snake_case_,snake_case_=m ): _A : List[str] = 0 for i in range(_UpperCAmelCase ): if index == -1: summation_value += _error(_UpperCAmelCase ) else: summation_value += _error(_UpperCAmelCase ) * train_data[i][0][index] return summation_value def lowerCAmelCase_ ( snake_case_ ): _A : int = summation_of_cost_derivative(_UpperCAmelCase,_UpperCAmelCase ) / m return cost_derivative_value def lowerCAmelCase_ ( ): global parameter_vector # Tune these values to set a tolerance value for predicted output _A : Any = 0.00_00_02 _A : List[Any] = 0 _A : Dict = 0 while True: j += 1 _A : str = [0, 0, 0, 0] for i in range(0,len(_UpperCAmelCase ) ): _A : Dict = get_cost_derivative(i - 1 ) _A : Tuple = ( parameter_vector[i] - LEARNING_RATE * cost_derivative ) if numpy.allclose( _UpperCAmelCase,_UpperCAmelCase,atol=_UpperCAmelCase,rtol=_UpperCAmelCase,): break _A : Dict = temp_parameter_vector print(("""Number of iterations:""", j) ) def lowerCAmelCase_ ( ): for i in range(len(_UpperCAmelCase ) ): print(("""Actual output value:""", output(_UpperCAmelCase,"""test""" )) ) print(("""Hypothesis output:""", calculate_hypothesis_value(_UpperCAmelCase,"""test""" )) ) if __name__ == "__main__": run_gradient_descent() print("\nTesting gradient descent for a linear hypothesis function.\n") test_gradient_descent()
715
def lowerCAmelCase_ ( snake_case_ = 1000000 ): _A : Any = limit + 1 _A : Tuple = [0] * limit for first_term in range(1,snake_case_ ): for n in range(snake_case_,snake_case_,snake_case_ ): _A : Optional[int] = 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 _A : List[str] = sum(1 for x in frequency[1:limit] if x == 10 ) return count if __name__ == "__main__": print(f"""{solution() = }""")
54
0
from __future__ import annotations from collections.abc import Callable def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_,snake_case_ = 100,): _A : str = x_start _A : Union[str, Any] = fnc(snake_case_ ) _A : Union[str, Any] = 0.0 for _ in range(snake_case_ ): # Approximates small segments of curve as linear and solve # for trapezoidal area _A : Any = (x_end - x_start) / steps + xa _A : Tuple = fnc(snake_case_ ) area += abs(fxa + fxa ) * (xa - xa) / 2 # Increment step _A : Tuple = xa _A : Dict = fxa return area if __name__ == "__main__": def lowerCAmelCase_ ( snake_case_ ): return x**3 + x**2 print("f(x) = x^3 + x^2") print("The area between the curve, x = -5, x = 5 and the x axis is:") _snake_case : Any = 10 while i <= 100000: print(f"""with {i} steps: {trapezoidal_area(f, -5, 5, i)}""") i *= 10
716
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 lowercase ( tf.keras.layers.Layer ): def __init__( self , _a , _a , _a = None , _a = None ) -> Any: super().__init__() _A : Dict = pad_token_id _A : List[Any] = max_length _A : Optional[int] = vocab _A : Optional[int] = merges _A : Optional[int] = BytePairTokenizer(_a , _a , sequence_length=_a ) @classmethod def a__ ( cls , _a , *_a , **_a ) -> str: _A : Any = [""" """.join(_a ) for m in tokenizer.bpe_ranks.keys()] _A : str = tokenizer.get_vocab() return cls(_a , _a , *_a , **_a ) @classmethod def a__ ( cls , _a , *_a , **_a ) -> List[Any]: _A : Union[str, Any] = GPTaTokenizer.from_pretrained(_a , *_a , **_a ) return cls.from_tokenizer(_a , *_a , **_a ) @classmethod def a__ ( cls , _a ) -> Union[str, Any]: return cls(**_a ) def a__ ( self ) -> Union[str, Any]: return { "vocab": self.vocab, "merges": self.merges, "max_length": self.max_length, "pad_token_id": self.pad_token_id, } def a__ ( self , _a , _a = None ) -> int: _A : Optional[int] = self.tf_tokenizer(_a ) _A : Tuple = tf.ones_like(_a ) if self.pad_token_id is not None: # pad the tokens up to max length _A : Dict = max_length if max_length is not None else self.max_length if max_length is not None: _A , _A : Dict = pad_model_inputs( _a , max_seq_length=_a , pad_value=self.pad_token_id ) return {"attention_mask": attention_mask, "input_ids": input_ids}
54
0
import os import pickle import unittest from transformers import AutoTokenizer from transformers.models.bert.tokenization_bert import BertTokenizer from transformers.models.bert_japanese.tokenization_bert_japanese import ( VOCAB_FILES_NAMES, BertJapaneseTokenizer, CharacterTokenizer, JumanppTokenizer, MecabTokenizer, SudachiTokenizer, WordpieceTokenizer, ) from transformers.testing_utils import custom_tokenizers, require_jumanpp, require_sudachi from ...test_tokenization_common import TokenizerTesterMixin @custom_tokenizers class lowercase ( UpperCamelCase__,unittest.TestCase ): _a = BertJapaneseTokenizer _a = False _a = True def a__ ( self ) -> int: super().setUp() _A : Tuple = [ """[UNK]""", """[CLS]""", """[SEP]""", """こんにちは""", """こん""", """にちは""", """ばんは""", """##こん""", """##にちは""", """##ばんは""", """世界""", """##世界""", """、""", """##、""", """。""", """##。""", ] _A : List[str] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""vocab_file"""] ) with open(self.vocab_file , """w""" , encoding="""utf-8""" ) as vocab_writer: vocab_writer.write("""""".join([x + """\n""" for x in vocab_tokens] ) ) def a__ ( self , _a ) -> List[str]: _A : List[Any] = """こんにちは、世界。 \nこんばんは、世界。""" _A : Optional[int] = """こんにちは 、 世界 。 こんばんは 、 世界 。""" return input_text, output_text def a__ ( self , _a ) -> Dict: _A , _A : List[str] = self.get_input_output_texts(__A ) _A : Optional[int] = tokenizer.encode(__A , add_special_tokens=__A ) _A : str = tokenizer.decode(__A , clean_up_tokenization_spaces=__A ) return text, ids def a__ ( self ) -> List[str]: pass # TODO add if relevant def a__ ( self ) -> List[str]: pass # TODO add if relevant def a__ ( self ) -> List[Any]: pass # TODO add if relevant def a__ ( self ) -> Optional[Any]: _A : Dict = self.tokenizer_class(self.vocab_file ) _A : str = tokenizer.tokenize("""こんにちは、世界。\nこんばんは、世界。""" ) self.assertListEqual(__A , ["""こんにちは""", """、""", """世界""", """。""", """こん""", """##ばんは""", """、""", """世界""", """。"""] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(__A ) , [3, 12, 10, 14, 4, 9, 12, 10, 14] ) def a__ ( self ) -> Union[str, Any]: _A : Any = self.tokenizer_class(self.vocab_file , word_tokenizer_type="""mecab""" ) self.assertIsNotNone(__A ) _A : Optional[Any] = """こんにちは、世界。\nこんばんは、世界。""" _A : List[str] = tokenizer.tokenize(__A ) self.assertListEqual(__A , ["""こんにちは""", """、""", """世界""", """。""", """こん""", """##ばんは""", """、""", """世界""", """。"""] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(__A ) , [3, 12, 10, 14, 4, 9, 12, 10, 14] ) _A : str = os.path.join(self.tmpdirname , """tokenizer.bin""" ) with open(__A , """wb""" ) as handle: pickle.dump(__A , __A ) with open(__A , """rb""" ) as handle: _A : Any = pickle.load(__A ) _A : List[str] = tokenizer_new.tokenize(__A ) self.assertListEqual(__A , __A ) def a__ ( self ) -> List[str]: _A : Any = MecabTokenizer(mecab_dic="""ipadic""" ) self.assertListEqual( tokenizer.tokenize(""" \tアップルストアでiPhone8 が \n 発売された 。 """ ) , ["""アップルストア""", """で""", """iPhone""", """8""", """が""", """発売""", """さ""", """れ""", """た""", """。"""] , ) def a__ ( self ) -> Union[str, Any]: try: _A : List[Any] = MecabTokenizer(mecab_dic="""unidic_lite""" ) except ModuleNotFoundError: return self.assertListEqual( tokenizer.tokenize(""" \tアップルストアでiPhone8 が \n 発売された 。 """ ) , ["""アップル""", """ストア""", """で""", """iPhone""", """8""", """が""", """発売""", """さ""", """れ""", """た""", """。"""] , ) def a__ ( self ) -> Any: try: _A : str = MecabTokenizer(mecab_dic="""unidic""" ) except ModuleNotFoundError: return self.assertListEqual( tokenizer.tokenize(""" \tアップルストアでiPhone8 が \n 発売された 。 """ ) , ["""アップル""", """ストア""", """で""", """iPhone""", """8""", """が""", """発売""", """さ""", """れ""", """た""", """。"""] , ) def a__ ( self ) -> int: _A : str = MecabTokenizer(do_lower_case=__A , mecab_dic="""ipadic""" ) self.assertListEqual( tokenizer.tokenize(""" \tアップルストアでiPhone8 が \n 発売された 。 """ ) , ["""アップルストア""", """で""", """iphone""", """8""", """が""", """発売""", """さ""", """れ""", """た""", """。"""] , ) def a__ ( self ) -> Any: try: _A : Optional[Any] = MecabTokenizer( do_lower_case=__A , normalize_text=__A , mecab_option="""-d /usr/local/lib/mecab/dic/jumandic""" ) except RuntimeError: # if dict doesn't exist in the system, previous code raises this error. return self.assertListEqual( tokenizer.tokenize(""" \tアップルストアでiPhone8 が \n 発売された 。 """ ) , ["""アップルストア""", """で""", """iPhone""", """8""", """が""", """発売""", """さ""", """れた""", """\u3000""", """。"""] , ) def a__ ( self ) -> List[Any]: _A : int = MecabTokenizer(normalize_text=__A , mecab_dic="""ipadic""" ) self.assertListEqual( tokenizer.tokenize(""" \tアップルストアでiPhone8 が \n 発売された 。 """ ) , ["""アップルストア""", """で""", """iPhone""", """8""", """が""", """発売""", """さ""", """れ""", """た""", """ """, """。"""] , ) @require_sudachi def a__ ( self ) -> Any: _A : int = self.tokenizer_class(self.vocab_file , word_tokenizer_type="""sudachi""" ) self.assertIsNotNone(__A ) _A : Union[str, Any] = """こんにちは、世界。\nこんばんは、世界。""" _A : Any = tokenizer.tokenize(__A ) self.assertListEqual(__A , ["""こんにちは""", """、""", """世界""", """。""", """こん""", """##ばんは""", """、""", """世界""", """。"""] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(__A ) , [3, 12, 10, 14, 4, 9, 12, 10, 14] ) _A : Any = os.path.join(self.tmpdirname , """tokenizer.bin""" ) with open(__A , """wb""" ) as handle: pickle.dump(__A , __A ) with open(__A , """rb""" ) as handle: _A : int = pickle.load(__A ) _A : Tuple = tokenizer_new.tokenize(__A ) self.assertListEqual(__A , __A ) @require_sudachi def a__ ( self ) -> Dict: _A : str = SudachiTokenizer(sudachi_dict_type="""core""" ) self.assertListEqual( tokenizer.tokenize(""" \tアップルストアでiPhone8 が \n 発売された 。 """ ) , [""" """, """\t""", """アップル""", """ストア""", """で""", """iPhone""", """8""", """ """, """が""", """ """, """ """, """\n """, """発売""", """さ""", """れ""", """た""", """ """, """。""", """ """, """ """] , ) @require_sudachi def a__ ( self ) -> Union[str, Any]: _A : List[Any] = SudachiTokenizer(sudachi_dict_type="""core""" , sudachi_split_mode="""A""" ) self.assertListEqual(tokenizer.tokenize("""外国人参政権""" ) , ["""外国""", """人""", """参政""", """権"""] ) @require_sudachi def a__ ( self ) -> Optional[int]: _A : List[str] = SudachiTokenizer(sudachi_dict_type="""core""" , sudachi_split_mode="""B""" ) self.assertListEqual(tokenizer.tokenize("""外国人参政権""" ) , ["""外国人""", """参政権"""] ) @require_sudachi def a__ ( self ) -> Any: _A : Union[str, Any] = SudachiTokenizer(sudachi_dict_type="""core""" , sudachi_split_mode="""C""" ) self.assertListEqual(tokenizer.tokenize("""外国人参政権""" ) , ["""外国人参政権"""] ) @require_sudachi def a__ ( self ) -> int: _A : List[str] = SudachiTokenizer(do_lower_case=__A , sudachi_dict_type="""core""" ) self.assertListEqual( tokenizer.tokenize(""" \tアップルストアでiPhone8 が \n 発売された 。 """ ) , [""" """, """\t""", """アップル""", """ストア""", """で""", """iphone""", """8""", """ """, """が""", """ """, """ """, """\n """, """発売""", """さ""", """れ""", """た""", """ """, """。""", """ """, """ """] , ) @require_sudachi def a__ ( self ) -> List[Any]: _A : List[str] = SudachiTokenizer(normalize_text=__A , sudachi_dict_type="""core""" ) self.assertListEqual( tokenizer.tokenize(""" \tアップルストアでiPhone8 が \n 発売された 。 """ ) , [""" """, """\t""", """アップル""", """ストア""", """で""", """iPhone""", """8""", """ """, """が""", """ """, """ """, """\n """, """発売""", """さ""", """れ""", """た""", """\u3000""", """。""", """ """, """ """] , ) @require_sudachi def a__ ( self ) -> List[Any]: _A : str = SudachiTokenizer(trim_whitespace=__A , sudachi_dict_type="""core""" ) self.assertListEqual( tokenizer.tokenize(""" \tアップルストアでiPhone8 が \n 発売された 。 """ ) , ["""アップル""", """ストア""", """で""", """iPhone""", """8""", """が""", """発売""", """さ""", """れ""", """た""", """。"""] , ) @require_jumanpp def a__ ( self ) -> Optional[int]: _A : Optional[Any] = self.tokenizer_class(self.vocab_file , word_tokenizer_type="""jumanpp""" ) self.assertIsNotNone(__A ) _A : Dict = """こんにちは、世界。\nこんばんは、世界。""" _A : int = tokenizer.tokenize(__A ) self.assertListEqual(__A , ["""こんにちは""", """、""", """世界""", """。""", """こん""", """##ばんは""", """、""", """世界""", """。"""] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(__A ) , [3, 12, 10, 14, 4, 9, 12, 10, 14] ) _A : Optional[int] = os.path.join(self.tmpdirname , """tokenizer.bin""" ) with open(__A , """wb""" ) as handle: pickle.dump(__A , __A ) with open(__A , """rb""" ) as handle: _A : str = pickle.load(__A ) _A : Dict = tokenizer_new.tokenize(__A ) self.assertListEqual(__A , __A ) @require_jumanpp def a__ ( self ) -> Optional[Any]: _A : Dict = JumanppTokenizer() self.assertListEqual( tokenizer.tokenize(""" \tアップルストアでiPhone8 が \n 発売された 。 """ ) , ["""アップル""", """ストア""", """で""", """iPhone""", """8""", """\u3000""", """が""", """\u3000""", """\u3000""", """\u3000""", """発売""", """さ""", """れた""", """\u3000""", """。"""] , ) @require_jumanpp def a__ ( self ) -> Dict: _A : List[Any] = JumanppTokenizer(do_lower_case=__A ) self.assertListEqual( tokenizer.tokenize(""" \tアップルストアでiPhone8 が \n 発売された 。 """ ) , ["""アップル""", """ストア""", """で""", """iphone""", """8""", """\u3000""", """が""", """\u3000""", """\u3000""", """\u3000""", """発売""", """さ""", """れた""", """\u3000""", """。"""] , ) @require_jumanpp def a__ ( self ) -> Optional[Any]: _A : Optional[Any] = JumanppTokenizer(normalize_text=__A ) self.assertListEqual( tokenizer.tokenize(""" \tアップルストアでiPhone8 が \n 発売された 。 """ ) , ["""ア""", """ッ""", """フ""", """゚""", """ル""", """ストア""", """で""", """iPhone""", """8""", """\u3000""", """が""", """\u3000""", """\u3000""", """\u3000""", """発売""", """さ""", """れた""", """\u3000""", """。"""] , ) @require_jumanpp def a__ ( self ) -> Union[str, Any]: _A : Any = JumanppTokenizer(trim_whitespace=__A ) self.assertListEqual( tokenizer.tokenize(""" \tアップルストアでiPhone8 が \n 発売された 。 """ ) , ["""アップル""", """ストア""", """で""", """iPhone""", """8""", """が""", """発売""", """さ""", """れた""", """。"""] , ) @require_jumanpp def a__ ( self ) -> Union[str, Any]: _A : int = JumanppTokenizer() self.assertListEqual( tokenizer.tokenize("""ありがとうございますm(_ _)m見つけるのが大変です。""" ) , ["""ありがとう""", """ございます""", """m(_ _)m""", """見つける""", """の""", """が""", """大変です""", """。"""] , ) def a__ ( self ) -> Union[str, Any]: _A : Optional[int] = ["""[UNK]""", """[CLS]""", """[SEP]""", """こんにちは""", """こん""", """にちは""", """ばんは""", """##こん""", """##にちは""", """##ばんは"""] _A : Union[str, Any] = {} for i, token in enumerate(__A ): _A : Tuple = i _A : List[str] = WordpieceTokenizer(vocab=__A , unk_token="""[UNK]""" ) self.assertListEqual(tokenizer.tokenize("""""" ) , [] ) self.assertListEqual(tokenizer.tokenize("""こんにちは""" ) , ["""こんにちは"""] ) self.assertListEqual(tokenizer.tokenize("""こんばんは""" ) , ["""こん""", """##ばんは"""] ) self.assertListEqual(tokenizer.tokenize("""こんばんは こんばんにちは こんにちは""" ) , ["""こん""", """##ばんは""", """[UNK]""", """こんにちは"""] ) def a__ ( self ) -> str: _A : int = BertJapaneseTokenizer.from_pretrained("""nlp-waseda/roberta-base-japanese-with-auto-jumanpp""" ) _A : List[str] = tokenizer.subword_tokenizer _A : str = subword_tokenizer.tokenize("""国境 の 長い トンネル を 抜ける と 雪国 であった 。""" ) self.assertListEqual(__A , ["""▁国境""", """▁の""", """▁長い""", """▁トンネル""", """▁を""", """▁抜ける""", """▁と""", """▁雪""", """国""", """▁であった""", """▁。"""] ) _A : Union[str, Any] = subword_tokenizer.tokenize("""こんばんは こんばん にち は こんにちは""" ) self.assertListEqual(__A , ["""▁こん""", """ばん""", """は""", """▁こん""", """ばん""", """▁に""", """ち""", """▁は""", """▁こんにちは"""] ) def a__ ( self ) -> Union[str, Any]: _A : int = self.tokenizer_class.from_pretrained("""cl-tohoku/bert-base-japanese""" ) _A : str = tokenizer.encode("""ありがとう。""" , add_special_tokens=__A ) _A : int = tokenizer.encode("""どういたしまして。""" , add_special_tokens=__A ) _A : List[str] = tokenizer.build_inputs_with_special_tokens(__A ) _A : str = tokenizer.build_inputs_with_special_tokens(__A , __A ) # 2 is for "[CLS]", 3 is for "[SEP]" assert encoded_sentence == [2] + text + [3] assert encoded_pair == [2] + text + [3] + text_a + [3] @custom_tokenizers class lowercase ( UpperCamelCase__,unittest.TestCase ): _a = BertJapaneseTokenizer _a = False def a__ ( self ) -> Union[str, Any]: super().setUp() _A : Any = ["""[UNK]""", """[CLS]""", """[SEP]""", """こ""", """ん""", """に""", """ち""", """は""", """ば""", """世""", """界""", """、""", """。"""] _A : Optional[int] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""vocab_file"""] ) with open(self.vocab_file , """w""" , encoding="""utf-8""" ) as vocab_writer: vocab_writer.write("""""".join([x + """\n""" for x in vocab_tokens] ) ) def a__ ( self , **_a ) -> Dict: return BertJapaneseTokenizer.from_pretrained(self.tmpdirname , subword_tokenizer_type="""character""" , **__A ) def a__ ( self , _a ) -> Optional[int]: _A : Dict = """こんにちは、世界。 \nこんばんは、世界。""" _A : Any = """こ ん に ち は 、 世 界 。 こ ん ば ん は 、 世 界 。""" return input_text, output_text def a__ ( self ) -> List[str]: pass # TODO add if relevant def a__ ( self ) -> Optional[int]: pass # TODO add if relevant def a__ ( self ) -> Optional[int]: pass # TODO add if relevant def a__ ( self ) -> List[str]: _A : Optional[int] = self.tokenizer_class(self.vocab_file , subword_tokenizer_type="""character""" ) _A : Union[str, Any] = tokenizer.tokenize("""こんにちは、世界。 \nこんばんは、世界。""" ) self.assertListEqual( __A , ["""こ""", """ん""", """に""", """ち""", """は""", """、""", """世""", """界""", """。""", """こ""", """ん""", """ば""", """ん""", """は""", """、""", """世""", """界""", """。"""] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(__A ) , [3, 4, 5, 6, 7, 11, 9, 10, 12, 3, 4, 8, 4, 7, 11, 9, 10, 12] ) def a__ ( self ) -> Any: _A : Any = ["""[UNK]""", """[CLS]""", """[SEP]""", """こ""", """ん""", """に""", """ち""", """は""", """ば""", """世""", """界""", """、""", """。"""] _A : int = {} for i, token in enumerate(__A ): _A : Tuple = i _A : str = CharacterTokenizer(vocab=__A , unk_token="""[UNK]""" ) self.assertListEqual(tokenizer.tokenize("""""" ) , [] ) self.assertListEqual(tokenizer.tokenize("""こんにちは""" ) , ["""こ""", """ん""", """に""", """ち""", """は"""] ) self.assertListEqual(tokenizer.tokenize("""こんにちほ""" ) , ["""こ""", """ん""", """に""", """ち""", """[UNK]"""] ) def a__ ( self ) -> Dict: _A : Any = self.tokenizer_class.from_pretrained("""cl-tohoku/bert-base-japanese-char""" ) _A : List[Any] = tokenizer.encode("""ありがとう。""" , add_special_tokens=__A ) _A : List[str] = tokenizer.encode("""どういたしまして。""" , add_special_tokens=__A ) _A : str = tokenizer.build_inputs_with_special_tokens(__A ) _A : List[str] = tokenizer.build_inputs_with_special_tokens(__A , __A ) # 2 is for "[CLS]", 3 is for "[SEP]" assert encoded_sentence == [2] + text + [3] assert encoded_pair == [2] + text + [3] + text_a + [3] @custom_tokenizers class lowercase ( unittest.TestCase ): def a__ ( self ) -> List[Any]: _A : Tuple = """cl-tohoku/bert-base-japanese""" _A : str = AutoTokenizer.from_pretrained(__A ) self.assertIsInstance(__A , __A ) class lowercase ( unittest.TestCase ): def a__ ( self ) -> List[Any]: _A : str = """cl-tohoku/bert-base-japanese""" with self.assertLogs("""transformers""" , level="""WARNING""" ) as cm: BertTokenizer.from_pretrained(__A ) self.assertTrue( cm.records[0].message.startswith( """The tokenizer class you load from this checkpoint is not the same type as the class this function""" """ is called from.""" ) ) _A : int = """bert-base-cased""" with self.assertLogs("""transformers""" , level="""WARNING""" ) as cm: BertJapaneseTokenizer.from_pretrained(__A ) self.assertTrue( cm.records[0].message.startswith( """The tokenizer class you load from this checkpoint is not the same type as the class this function""" """ is called from.""" ) )
717
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 _snake_case = get_tests_dir("fixtures/test_sentencepiece_no_bos.model") @require_sentencepiece @require_tokenizers class lowercase ( UpperCamelCase__,unittest.TestCase ): _a = PegasusTokenizer _a = PegasusTokenizerFast _a = True _a = True def a__ ( self ) -> int: super().setUp() # We have a SentencePiece fixture for testing _A : List[Any] = PegasusTokenizer(_a ) tokenizer.save_pretrained(self.tmpdirname ) @cached_property def a__ ( self ) -> int: return PegasusTokenizer.from_pretrained("""google/pegasus-large""" ) def a__ ( self , **_a ) -> PegasusTokenizer: return PegasusTokenizer.from_pretrained(self.tmpdirname , **_a ) def a__ ( self , _a ) -> List[Any]: return ("This is a test", "This is a test") def a__ ( self ) -> int: _A : Dict = """</s>""" _A : Dict = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(_a ) , _a ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(_a ) , _a ) def a__ ( self ) -> Dict: _A : List[str] = 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(_a ) , 1103 ) def a__ ( self ) -> Optional[int]: self.assertEqual(self.get_tokenizer().vocab_size , 1103 ) def a__ ( self ) -> Tuple: _A : Any = self.rust_tokenizer_class.from_pretrained(self.tmpdirname ) _A : Optional[int] = self.tokenizer_class.from_pretrained(self.tmpdirname ) _A : int = ( """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 : Optional[int] = rust_tokenizer([raw_input_str] , return_tensors=_a , add_special_tokens=_a ).input_ids[0] _A : List[Any] = py_tokenizer([raw_input_str] , return_tensors=_a , add_special_tokens=_a ).input_ids[0] self.assertListEqual(_a , _a ) def a__ ( self ) -> Any: _A : str = self._large_tokenizer # <mask_1> masks whole sentence while <mask_2> masks single word _A : Optional[int] = """<mask_1> To ensure a <mask_2> flow of bank resolutions.""" _A : Union[str, Any] = [2, 413, 615, 114, 3, 1971, 113, 1679, 1_0710, 107, 1] _A : Union[str, Any] = tokenizer([raw_input_str] , return_tensors=_a ).input_ids[0] self.assertListEqual(_a , _a ) def a__ ( self ) -> List[str]: _A : Optional[int] = self._large_tokenizer # The tracebacks for the following asserts are **better** without messages or self.assertEqual assert tokenizer.vocab_size == 9_6103 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 : Any = """To ensure a smooth flow of bank resolutions.""" _A : Optional[int] = [413, 615, 114, 2291, 1971, 113, 1679, 1_0710, 107, 1] _A : Optional[Any] = tokenizer([raw_input_str] , return_tensors=_a ).input_ids[0] self.assertListEqual(_a , _a ) assert tokenizer.convert_ids_to_tokens([0, 1, 2, 3] ) == ["<pad>", "</s>", "<mask_1>", "<mask_2>"] @require_torch def a__ ( self ) -> List[str]: _A : Union[str, Any] = ["""This is going to be way too long.""" * 150, """short example"""] _A : Optional[Any] = ["""not super long but more than 5 tokens""", """tiny"""] _A : Union[str, Any] = self._large_tokenizer(_a , padding=_a , truncation=_a , return_tensors="""pt""" ) _A : Tuple = self._large_tokenizer( text_target=_a , max_length=5 , padding=_a , truncation=_a , 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(_a ) == 2 # input_ids, attention_mask. @slow def a__ ( self ) -> Optional[Any]: # fmt: off _A : List[Any] = {"""input_ids""": [[3_8979, 143, 1_8485, 606, 130, 2_6669, 8_7686, 121, 5_4189, 1129, 111, 2_6669, 8_7686, 121, 9114, 1_4787, 121, 1_3249, 158, 592, 956, 121, 1_4621, 3_1576, 143, 6_2613, 108, 9688, 930, 4_3430, 1_1562, 6_2613, 304, 108, 1_1443, 897, 108, 9314, 1_7415, 6_3399, 108, 1_1443, 7614, 1_8316, 118, 4284, 7148, 1_2430, 143, 1400, 2_5703, 158, 111, 4284, 7148, 1_1772, 143, 2_1297, 1064, 158, 122, 204, 3506, 1754, 1133, 1_4787, 1581, 115, 3_3224, 4482, 111, 1355, 110, 2_9173, 317, 5_0833, 108, 2_0147, 9_4665, 111, 7_7198, 107, 1], [110, 6_2613, 117, 638, 112, 1133, 121, 2_0098, 1355, 7_9050, 1_3872, 135, 1596, 5_3541, 1352, 141, 1_3039, 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, 1_8289, 1_7780, 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=_a , model_name="""google/bigbird-pegasus-large-arxiv""" , revision="""ba85d0851d708441f91440d509690f1ab6353415""" , ) @require_sentencepiece @require_tokenizers class lowercase ( UpperCamelCase__,unittest.TestCase ): _a = PegasusTokenizer _a = PegasusTokenizerFast _a = True _a = True def a__ ( self ) -> Optional[Any]: super().setUp() # We have a SentencePiece fixture for testing _A : Tuple = PegasusTokenizer(_a , offset=0 , mask_token_sent=_a , mask_token="""[MASK]""" ) tokenizer.save_pretrained(self.tmpdirname ) @cached_property def a__ ( self ) -> Optional[Any]: return PegasusTokenizer.from_pretrained("""google/bigbird-pegasus-large-arxiv""" ) def a__ ( self , **_a ) -> PegasusTokenizer: return PegasusTokenizer.from_pretrained(self.tmpdirname , **_a ) def a__ ( self , _a ) -> List[str]: return ("This is a test", "This is a test") def a__ ( self ) -> List[Any]: _A : List[Any] = self.rust_tokenizer_class.from_pretrained(self.tmpdirname ) _A : Dict = self.tokenizer_class.from_pretrained(self.tmpdirname ) _A : Dict = ( """Let's see which <unk> is the better <unk_token> one [MASK] It seems like this [MASK] was important </s>""" """ <pad> <pad> <pad>""" ) _A : Optional[Any] = rust_tokenizer([raw_input_str] , return_tensors=_a , add_special_tokens=_a ).input_ids[0] _A : int = py_tokenizer([raw_input_str] , return_tensors=_a , add_special_tokens=_a ).input_ids[0] self.assertListEqual(_a , _a ) @require_torch def a__ ( self ) -> Optional[int]: _A : Tuple = ["""This is going to be way too long.""" * 1000, """short example"""] _A : Optional[Any] = ["""not super long but more than 5 tokens""", """tiny"""] _A : Tuple = self._large_tokenizer(_a , padding=_a , truncation=_a , return_tensors="""pt""" ) _A : str = self._large_tokenizer( text_target=_a , max_length=5 , padding=_a , truncation=_a , 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(_a ) == 2 # input_ids, attention_mask. def a__ ( self ) -> Dict: _A : Optional[int] = ( """This is an example string that is used to test the original TF implementation against the HF""" """ implementation""" ) _A : Any = self._large_tokenizer(_a ).input_ids self.assertListEqual( _a , [182, 117, 142, 587, 4211, 120, 117, 263, 112, 804, 109, 856, 2_5016, 3137, 464, 109, 2_6955, 3137, 1] , )
54
0
import json import os import unittest from transformers import MgpstrTokenizer from transformers.models.mgp_str.tokenization_mgp_str import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class lowercase ( __snake_case,unittest.TestCase ): _a = MgpstrTokenizer _a = False _a = {} _a = False def a__ ( self ) -> Tuple: super().setUp() # fmt: off _A : int = ["""[GO]""", """[s]""", """0""", """1""", """2""", """3""", """4""", """5""", """6""", """7""", """8""", """9""", """a""", """b""", """c""", """d""", """e""", """f""", """g""", """h""", """i""", """j""", """k""", """l""", """m""", """n""", """o""", """p""", """q""", """r""", """s""", """t""", """u""", """v""", """w""", """x""", """y""", """z"""] # fmt: on _A : List[Any] = dict(zip(_lowercase , range(len(_lowercase ) ) ) ) _A : Optional[Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""vocab_file"""] ) with open(self.vocab_file , """w""" , encoding="""utf-8""" ) as fp: fp.write(json.dumps(_lowercase ) + """\n""" ) def a__ ( self , **_a ) -> Optional[int]: return MgpstrTokenizer.from_pretrained(self.tmpdirname , **_lowercase ) def a__ ( self , _a ) -> str: _A : str = """tester""" _A : int = """tester""" return input_text, output_text @unittest.skip("""MGP-STR always lower cases letters.""" ) def a__ ( self ) -> Union[str, Any]: pass def a__ ( self ) -> str: _A : Optional[int] = self.get_tokenizers(do_lower_case=_lowercase ) for tokenizer in tokenizers: with self.subTest(F'''{tokenizer.__class__.__name__}''' ): _A : Any = """[SPECIAL_TOKEN]""" tokenizer.add_special_tokens({"""cls_token""": special_token} ) _A : str = tokenizer.encode([special_token] , add_special_tokens=_lowercase ) self.assertEqual(len(_lowercase ) , 1 ) _A : List[Any] = tokenizer.decode(_lowercase , skip_special_tokens=_lowercase ) self.assertTrue(special_token not in decoded ) def a__ ( self ) -> int: _A : Union[str, Any] = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(F'''{tokenizer.__class__.__name__}''' ): _A : str = self.get_input_output_texts(_lowercase ) _A : str = tokenizer.tokenize(_lowercase ) _A : Dict = tokenizer.convert_tokens_to_ids(_lowercase ) _A : List[Any] = tokenizer.encode(_lowercase , add_special_tokens=_lowercase ) self.assertListEqual(_lowercase , _lowercase ) _A : Optional[int] = tokenizer.convert_ids_to_tokens(_lowercase ) self.assertNotEqual(len(_lowercase ) , 0 ) _A : str = tokenizer.decode(_lowercase ) self.assertIsInstance(_lowercase , _lowercase ) self.assertEqual(text_a.replace(""" """ , """""" ) , _lowercase ) @unittest.skip("""MGP-STR tokenizer only handles one sequence.""" ) def a__ ( self ) -> Dict: pass @unittest.skip("""inputs cannot be pretokenized in MgpstrTokenizer""" ) def a__ ( self ) -> Tuple: pass
718
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available, is_vision_available, ) _snake_case = { "configuration_efficientformer": [ "EFFICIENTFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", "EfficientFormerConfig", ] } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = ["EfficientFormerImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = [ "EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "EfficientFormerForImageClassification", "EfficientFormerForImageClassificationWithTeacher", "EfficientFormerModel", "EfficientFormerPreTrainedModel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = [ "TF_EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "TFEfficientFormerForImageClassification", "TFEfficientFormerForImageClassificationWithTeacher", "TFEfficientFormerModel", "TFEfficientFormerPreTrainedModel", ] if TYPE_CHECKING: from .configuration_efficientformer import EFFICIENTFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, EfficientFormerConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_efficientformer import EfficientFormerImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_efficientformer import ( EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, EfficientFormerForImageClassification, EfficientFormerForImageClassificationWithTeacher, EfficientFormerModel, EfficientFormerPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_efficientformer import ( TF_EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TFEfficientFormerForImageClassification, TFEfficientFormerForImageClassificationWithTeacher, TFEfficientFormerModel, TFEfficientFormerPreTrainedModel, ) else: import sys _snake_case = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
54
0
import json import os import re import shutil import tempfile import unittest from typing import Tuple from transformers import AddedToken, BatchEncoding, PerceiverTokenizer from transformers.utils import cached_property, is_tf_available, is_torch_available from ...test_tokenization_common import TokenizerTesterMixin if is_torch_available(): _snake_case = 'pt' elif is_tf_available(): _snake_case = 'tf' else: _snake_case = 'jax' class lowercase ( UpperCamelCase__,unittest.TestCase ): _a = PerceiverTokenizer _a = False def a__ ( self ) -> int: super().setUp() _A : int = PerceiverTokenizer() tokenizer.save_pretrained(self.tmpdirname ) @cached_property def a__ ( self ) -> Optional[Any]: return PerceiverTokenizer.from_pretrained("""deepmind/language-perceiver""" ) def a__ ( self , **_a ) -> List[str]: return self.tokenizer_class.from_pretrained(self.tmpdirname , **A__ ) def a__ ( self , _a , _a=False , _a=20 , _a=5 ) -> Optional[int]: # XXX The default common tokenizer tests assume that every ID is decodable on its own. # This assumption is invalid for Perceiver because single bytes might not be # valid utf-8 (byte 128 for instance). # Here we're overriding the smallest possible method to provide # a clean sequence without making the same assumption. _A : int = [] for i in range(len(A__ ) ): try: _A : List[Any] = tokenizer.decode([i] , clean_up_tokenization_spaces=A__ ) except UnicodeDecodeError: pass toks.append((i, tok) ) _A : Dict = list(filter(lambda _a : re.match(R"""^[ a-zA-Z]+$""" , t[1] ) , A__ ) ) _A : str = list(filter(lambda _a : [t[0]] == tokenizer.encode(t[1] , add_special_tokens=A__ ) , A__ ) ) if max_length is not None and len(A__ ) > max_length: _A : List[Any] = toks[:max_length] if min_length is not None and len(A__ ) < min_length and len(A__ ) > 0: while len(A__ ) < min_length: _A : List[Any] = toks + toks # toks_str = [t[1] for t in toks] _A : Tuple = [t[0] for t in toks] # Ensure consistency _A : Union[str, Any] = tokenizer.decode(A__ , clean_up_tokenization_spaces=A__ ) if " " not in output_txt and len(A__ ) > 1: _A : List[Any] = ( tokenizer.decode([toks_ids[0]] , clean_up_tokenization_spaces=A__ ) + """ """ + tokenizer.decode(toks_ids[1:] , clean_up_tokenization_spaces=A__ ) ) if with_prefix_space: _A : List[Any] = """ """ + output_txt _A : Optional[Any] = tokenizer.encode(A__ , add_special_tokens=A__ ) return output_txt, output_ids def a__ ( self ) -> int: _A : Union[str, Any] = self.perceiver_tokenizer _A : str = """Unicode €.""" _A : Union[str, Any] = tokenizer(A__ ) _A : Any = [4, 91, 116, 111, 105, 117, 106, 107, 38, 232, 136, 178, 52, 5] self.assertEqual(encoded["""input_ids"""] , A__ ) # decoding _A : List[Any] = tokenizer.decode(A__ ) self.assertEqual(A__ , """[CLS]Unicode €.[SEP]""" ) _A : Any = tokenizer("""e è é ê ë""" ) _A : str = [4, 107, 38, 201, 174, 38, 201, 175, 38, 201, 176, 38, 201, 177, 5] self.assertEqual(encoded["""input_ids"""] , A__ ) # decoding _A : Tuple = tokenizer.decode(A__ ) self.assertEqual(A__ , """[CLS]e è é ê ë[SEP]""" ) # encode/decode, but with `encode` instead of `__call__` self.assertEqual(tokenizer.decode(tokenizer.encode("""e è é ê ë""" ) ) , """[CLS]e è é ê ë[SEP]""" ) def a__ ( self ) -> str: _A : int = self.perceiver_tokenizer _A : Optional[int] = ["""A long paragraph for summarization.""", """Another paragraph for summarization."""] # fmt: off _A : str = [4, 71, 38, 114, 117, 116, 109, 38, 118, 103, 120, 103, 109, 120, 103, 118, 110, 38, 108, 117, 120, 38, 121, 123, 115, 115, 103, 120, 111, 128, 103, 122, 111, 117, 116, 52, 5, 0] # fmt: on _A : Union[str, Any] = tokenizer(A__ , padding=A__ , return_tensors=A__ ) self.assertIsInstance(A__ , A__ ) if FRAMEWORK != "jax": _A : Dict = list(batch.input_ids.numpy()[0] ) else: _A : List[Any] = list(batch.input_ids.tolist()[0] ) self.assertListEqual(A__ , A__ ) self.assertEqual((2, 38) , batch.input_ids.shape ) self.assertEqual((2, 38) , batch.attention_mask.shape ) def a__ ( self ) -> int: _A : Tuple = self.perceiver_tokenizer _A : Tuple = ["""A long paragraph for summarization.""", """Another paragraph for summarization."""] _A : List[str] = tokenizer(A__ , padding=A__ , return_tensors=A__ ) # check if input_ids are returned and no decoder_input_ids self.assertIn("""input_ids""" , A__ ) self.assertIn("""attention_mask""" , A__ ) self.assertNotIn("""decoder_input_ids""" , A__ ) self.assertNotIn("""decoder_attention_mask""" , A__ ) def a__ ( self ) -> Optional[Any]: _A : Optional[int] = self.perceiver_tokenizer _A : Optional[Any] = [ """Summary of the text.""", """Another summary.""", ] _A : Optional[Any] = tokenizer( text_target=A__ , max_length=32 , padding="""max_length""" , truncation=A__ , return_tensors=A__ ) self.assertEqual(32 , targets["""input_ids"""].shape[1] ) def a__ ( self ) -> Optional[Any]: # safety check on max_len default value so we are sure the test works _A : List[str] = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(F'''{tokenizer.__class__.__name__}''' ): self.assertNotEqual(tokenizer.model_max_length , 42 ) # Now let's start the test _A : Optional[int] = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(F'''{tokenizer.__class__.__name__}''' ): # Isolate this from the other tests because we save additional tokens/etc _A : Tuple = tempfile.mkdtemp() _A : Tuple = """ He is very happy, UNwant\u00E9d,running""" _A : Optional[Any] = tokenizer.encode(A__ , add_special_tokens=A__ ) tokenizer.save_pretrained(A__ ) _A : List[str] = tokenizer.__class__.from_pretrained(A__ ) _A : int = after_tokenizer.encode(A__ , add_special_tokens=A__ ) self.assertListEqual(A__ , A__ ) shutil.rmtree(A__ ) _A : List[str] = self.get_tokenizers(model_max_length=42 ) for tokenizer in tokenizers: with self.subTest(F'''{tokenizer.__class__.__name__}''' ): # Isolate this from the other tests because we save additional tokens/etc _A : Optional[Any] = tempfile.mkdtemp() _A : Any = """ He is very happy, UNwant\u00E9d,running""" tokenizer.add_tokens(["""bim""", """bambam"""] ) _A : Optional[Any] = tokenizer.additional_special_tokens additional_special_tokens.append("""new_additional_special_token""" ) tokenizer.add_special_tokens({"""additional_special_tokens""": additional_special_tokens} ) _A : Optional[Any] = tokenizer.encode(A__ , add_special_tokens=A__ ) tokenizer.save_pretrained(A__ ) _A : List[Any] = tokenizer.__class__.from_pretrained(A__ ) _A : Optional[int] = after_tokenizer.encode(A__ , add_special_tokens=A__ ) self.assertListEqual(A__ , A__ ) self.assertIn("""new_additional_special_token""" , after_tokenizer.additional_special_tokens ) self.assertEqual(after_tokenizer.model_max_length , 42 ) _A : Dict = tokenizer.__class__.from_pretrained(A__ , model_max_length=43 ) self.assertEqual(tokenizer.model_max_length , 43 ) shutil.rmtree(A__ ) def a__ ( self ) -> Optional[int]: _A : Dict = [] if self.test_slow_tokenizer: tokenizer_list.append((self.tokenizer_class, self.get_tokenizer()) ) if self.test_rust_tokenizer: tokenizer_list.append((self.rust_tokenizer_class, self.get_rust_tokenizer()) ) for tokenizer_class, tokenizer_utils in tokenizer_list: with tempfile.TemporaryDirectory() as tmp_dir: tokenizer_utils.save_pretrained(A__ ) with open(os.path.join(A__ , """special_tokens_map.json""" ) , encoding="""utf-8""" ) as json_file: _A : int = json.load(A__ ) with open(os.path.join(A__ , """tokenizer_config.json""" ) , encoding="""utf-8""" ) as json_file: _A : Tuple = json.load(A__ ) _A : str = [F'''<extra_id_{i}>''' for i in range(125 )] _A : Optional[Any] = added_tokens_extra_ids + [ """an_additional_special_token""" ] _A : Dict = added_tokens_extra_ids + [ """an_additional_special_token""" ] with open(os.path.join(A__ , """special_tokens_map.json""" ) , """w""" , encoding="""utf-8""" ) as outfile: json.dump(A__ , A__ ) with open(os.path.join(A__ , """tokenizer_config.json""" ) , """w""" , encoding="""utf-8""" ) as outfile: json.dump(A__ , A__ ) # the following checks allow us to verify that our test works as expected, i.e. that the tokenizer takes # into account the new value of additional_special_tokens given in the "tokenizer_config.json" and # "special_tokens_map.json" files _A : List[str] = tokenizer_class.from_pretrained( A__ , ) self.assertIn( """an_additional_special_token""" , tokenizer_without_change_in_init.additional_special_tokens ) self.assertEqual( ["""an_additional_special_token"""] , tokenizer_without_change_in_init.convert_ids_to_tokens( tokenizer_without_change_in_init.convert_tokens_to_ids(["""an_additional_special_token"""] ) ) , ) # Now we test that we can change the value of additional_special_tokens in the from_pretrained _A : Tuple = added_tokens_extra_ids + [AddedToken("""a_new_additional_special_token""" , lstrip=A__ )] _A : Tuple = tokenizer_class.from_pretrained( A__ , additional_special_tokens=A__ , ) self.assertIn("""a_new_additional_special_token""" , tokenizer.additional_special_tokens ) self.assertEqual( ["""a_new_additional_special_token"""] , tokenizer.convert_ids_to_tokens( tokenizer.convert_tokens_to_ids(["""a_new_additional_special_token"""] ) ) , ) def a__ ( self ) -> Optional[int]: _A : Any = self.perceiver_tokenizer self.assertEqual(tokenizer.decode([178] ) , """�""" ) def a__ ( self ) -> List[Any]: pass def a__ ( self ) -> List[str]: pass def a__ ( self ) -> Union[str, Any]: pass def a__ ( self ) -> Dict: pass def a__ ( self ) -> int: # The default common tokenizer tests uses invalid tokens for Perceiver that can only accept one-character # strings and special added tokens as tokens _A : int = self.get_tokenizers(fast=A__ , do_lower_case=A__ ) for tokenizer in tokenizers: with self.subTest(F'''{tokenizer.__class__.__name__}''' ): _A : Tuple = ["""[CLS]""", """t""", """h""", """i""", """s""", """ """, """i""", """s""", """ """, """a""", """ """, """t""", """e""", """s""", """t""", """[SEP]"""] _A : Dict = tokenizer.convert_tokens_to_string(A__ ) self.assertIsInstance(A__ , A__ )
719
from unittest import TestCase from datasets import Sequence, Value from datasets.arrow_dataset import Dataset class lowercase ( UpperCamelCase__ ): def a__ ( self ) -> Optional[int]: return [ {"col_1": 3, "col_2": "a"}, {"col_1": 2, "col_2": "b"}, {"col_1": 1, "col_2": "c"}, {"col_1": 0, "col_2": "d"}, ] def a__ ( self ) -> Optional[int]: _A : Union[str, Any] = {"""col_1""": [3, 2, 1, 0], """col_2""": ["""a""", """b""", """c""", """d"""]} return Dataset.from_dict(_a ) def a__ ( self ) -> Any: _A : str = self._create_example_records() _A : List[Any] = Dataset.from_list(_a ) self.assertListEqual(dset.column_names , ["""col_1""", """col_2"""] ) for i, r in enumerate(_a ): self.assertDictEqual(_a , example_records[i] ) def a__ ( self ) -> List[str]: _A : Dict = self._create_example_records() _A : List[str] = Dataset.from_list(_a ) _A : str = Dataset.from_dict({k: [r[k] for r in example_records] for k in example_records[0]} ) self.assertEqual(dset.info , dset_from_dict.info ) def a__ ( self ) -> str: # checks what happens with missing columns _A : List[str] = [{"""col_1""": 1}, {"""col_2""": """x"""}] _A : List[str] = Dataset.from_list(_a ) self.assertDictEqual(dset[0] , {"""col_1""": 1} ) self.assertDictEqual(dset[1] , {"""col_1""": None} ) # NB: first record is used for columns def a__ ( self ) -> Dict: # checks if the type can be inferred from the second record _A : List[Any] = [{"""col_1""": []}, {"""col_1""": [1, 2]}] _A : str = Dataset.from_list(_a ) self.assertEqual(dset.info.features["""col_1"""] , Sequence(Value("""int64""" ) ) ) def a__ ( self ) -> Dict: _A : List[str] = Dataset.from_list([] ) self.assertEqual(len(_a ) , 0 ) self.assertListEqual(dset.column_names , [] )
54
0
def lowerCAmelCase_ ( snake_case_,snake_case_ ): return numa ^ numa < 0 if __name__ == "__main__": import doctest doctest.testmod()
720
from __future__ import annotations from collections.abc import Sequence from typing import Literal def lowerCAmelCase_ ( snake_case_,snake_case_ ): _A : List[str] = list(snake_case_ ) _A : List[Any] = list(snake_case_ ) _A : Tuple = 0 for i in range(len(snake_case_ ) ): if lista[i] != lista[i]: count += 1 _A : Optional[Any] = """_""" if count > 1: return False else: return "".join(snake_case_ ) def lowerCAmelCase_ ( snake_case_ ): _A : Optional[Any] = [] while True: _A : int = ["""$"""] * len(snake_case_ ) _A : Any = [] for i in range(len(snake_case_ ) ): for j in range(i + 1,len(snake_case_ ) ): _A : Tuple = compare_string(binary[i],binary[j] ) if k is False: _A : str = """*""" _A : str = """*""" temp.append("""X""" ) for i in range(len(snake_case_ ) ): if checka[i] == "$": pi.append(binary[i] ) if len(snake_case_ ) == 0: return pi _A : Dict = list(set(snake_case_ ) ) def lowerCAmelCase_ ( snake_case_,snake_case_ ): _A : List[str] = [] for minterm in minterms: _A : Tuple = """""" for _ in range(snake_case_ ): _A : Optional[Any] = str(minterm % 2 ) + string minterm //= 2 temp.append(snake_case_ ) return temp def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_ ): _A : Dict = list(snake_case_ ) _A : Tuple = list(snake_case_ ) _A : Dict = 0 for i in range(len(snake_case_ ) ): if lista[i] != lista[i]: count_n += 1 return count_n == count def lowerCAmelCase_ ( snake_case_,snake_case_ ): _A : Optional[int] = [] _A : str = [0] * len(snake_case_ ) for i in range(len(chart[0] ) ): _A : Union[str, Any] = 0 _A : Optional[Any] = -1 for j in range(len(snake_case_ ) ): if chart[j][i] == 1: count += 1 _A : Dict = j if count == 1: _A : int = 1 for i in range(len(snake_case_ ) ): if select[i] == 1: for j in range(len(chart[0] ) ): if chart[i][j] == 1: for k in range(len(snake_case_ ) ): _A : int = 0 temp.append(prime_implicants[i] ) while True: _A : Optional[Any] = 0 _A : Tuple = -1 _A : List[Any] = 0 for i in range(len(snake_case_ ) ): _A : List[str] = chart[i].count(1 ) if count_n > max_n: _A : Optional[int] = count_n _A : Tuple = i if max_n == 0: return temp temp.append(prime_implicants[rem] ) for i in range(len(chart[0] ) ): if chart[rem][i] == 1: for j in range(len(snake_case_ ) ): _A : Optional[int] = 0 def lowerCAmelCase_ ( snake_case_,snake_case_ ): _A : Optional[int] = [[0 for x in range(len(snake_case_ ) )] for x in range(len(snake_case_ ) )] for i in range(len(snake_case_ ) ): _A : List[Any] = prime_implicants[i].count("""_""" ) for j in range(len(snake_case_ ) ): if is_for_table(prime_implicants[i],binary[j],snake_case_ ): _A : Union[str, Any] = 1 return chart def lowerCAmelCase_ ( ): _A : Dict = int(input("""Enter the no. of variables\n""" ) ) _A : Dict = [ float(snake_case_ ) for x in input( """Enter the decimal representation of Minterms 'Spaces Separated'\n""" ).split() ] _A : int = decimal_to_binary(snake_case_,snake_case_ ) _A : Optional[Any] = check(snake_case_ ) print("""Prime Implicants are:""" ) print(snake_case_ ) _A : int = prime_implicant_chart(snake_case_,snake_case_ ) _A : int = selection(snake_case_,snake_case_ ) print("""Essential Prime Implicants are:""" ) print(snake_case_ ) if __name__ == "__main__": import doctest doctest.testmod() main()
54
0
from typing import TYPE_CHECKING from ...file_utils import _LazyModule, is_tokenizers_available, is_torch_available from ...utils import OptionalDependencyNotAvailable _snake_case = {"""configuration_gpt_neox""": ["""GPT_NEOX_PRETRAINED_CONFIG_ARCHIVE_MAP""", """GPTNeoXConfig"""]} try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = ["""GPTNeoXTokenizerFast"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = [ """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 _snake_case = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
721
from __future__ import annotations def lowerCAmelCase_ ( snake_case_,snake_case_ = None ): _A : Tuple = word_bank or [] # create a table _A : int = len(snake_case_ ) + 1 _A : list[list[list[str]]] = [] for _ in range(snake_case_ ): table.append([] ) # seed value _A : Dict = [[]] # because empty string has empty combination # iterate through the indices for i in range(snake_case_ ): # condition if table[i] != []: for word in word_bank: # slice condition if target[i : i + len(snake_case_ )] == word: _A : list[list[str]] = [ [word, *way] for way in table[i] ] # adds the word to every combination the current position holds # now,push that combination to the table[i+len(word)] table[i + len(snake_case_ )] += new_combinations # combinations are in reverse order so reverse for better output for combination in table[len(snake_case_ )]: combination.reverse() return table[len(snake_case_ )] if __name__ == "__main__": print(all_construct("jwajalapa", ["jwa", "j", "w", "a", "la", "lapa"])) print(all_construct("rajamati", ["s", "raj", "amat", "raja", "ma", "i", "t"])) print( all_construct( "hexagonosaurus", ["h", "ex", "hex", "ag", "ago", "ru", "auru", "rus", "go", "no", "o", "s"], ) )
54
0
import fire from torch.utils.data import DataLoader from tqdm import tqdm from transformers import AutoTokenizer from utils import SeqaSeqDataset, pickle_save def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_=1024,snake_case_=1024,snake_case_=False,**snake_case_ ): _A : str = AutoTokenizer.from_pretrained(snake_case_ ) _A : Tuple = SeqaSeqDataset(snake_case_,snake_case_,snake_case_,snake_case_,type_path="""train""",**snake_case_ ) _A : Dict = tok.pad_token_id def get_lens(snake_case_ ): _A : List[str] = tqdm( DataLoader(snake_case_,batch_size=512,num_workers=8,shuffle=snake_case_,collate_fn=ds.collate_fn ),desc=str(ds.len_file ),) _A : Optional[int] = [] for batch in dl: _A : int = batch["""input_ids"""].ne(snake_case_ ).sum(1 ).tolist() _A : Union[str, Any] = batch["""labels"""].ne(snake_case_ ).sum(1 ).tolist() if consider_target: for src, tgt in zip(snake_case_,snake_case_ ): max_lens.append(max(snake_case_,snake_case_ ) ) else: max_lens.extend(snake_case_ ) return max_lens _A : str = get_lens(snake_case_ ) _A : Union[str, Any] = SeqaSeqDataset(snake_case_,snake_case_,snake_case_,snake_case_,type_path="""val""",**snake_case_ ) _A : Union[str, Any] = get_lens(snake_case_ ) pickle_save(snake_case_,train_ds.len_file ) pickle_save(snake_case_,val_ds.len_file ) if __name__ == "__main__": fire.Fire(save_len_file)
700
import operator def lowerCAmelCase_ ( snake_case_,snake_case_ = False,snake_case_ = None ): _A : str = operator.lt if reverse else operator.gt _A : Optional[Any] = solution or [] if not arr: return solution _A : Dict = [arr.pop(0 )] for i, item in enumerate(snake_case_ ): if _operator(snake_case_,sublist[-1] ): sublist.append(snake_case_ ) arr.pop(snake_case_ ) # merging sublist into solution list if not solution: solution.extend(snake_case_ ) else: while sublist: _A : Union[str, Any] = sublist.pop(0 ) for i, xx in enumerate(snake_case_ ): if not _operator(snake_case_,snake_case_ ): solution.insert(snake_case_,snake_case_ ) break else: solution.append(snake_case_ ) strand_sort(snake_case_,snake_case_,snake_case_ ) return solution if __name__ == "__main__": assert strand_sort([4, 3, 5, 1, 2]) == [1, 2, 3, 4, 5] assert strand_sort([4, 3, 5, 1, 2], reverse=True) == [5, 4, 3, 2, 1]
54
0
import warnings 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 lowercase ( _UpperCAmelCase ): _a = ["image_processor", "tokenizer"] _a = "ViltImageProcessor" _a = ("BertTokenizer", "BertTokenizerFast") def __init__( self , _a=None , _a=None , **_a ) -> Dict: _A : Optional[Any] = None if "feature_extractor" in kwargs: warnings.warn( """The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`""" """ instead.""" , lowercase__ , ) _A : Any = kwargs.pop("""feature_extractor""" ) _A : int = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError("""You need to specify an `image_processor`.""" ) if tokenizer is None: raise ValueError("""You need to specify a `tokenizer`.""" ) super().__init__(lowercase__ , lowercase__ ) _A : Optional[Any] = self.image_processor def __call__( self , _a , _a = None , _a = True , _a = False , _a = None , _a = None , _a = 0 , _a = None , _a = None , _a = None , _a = False , _a = False , _a = False , _a = False , _a = True , _a = None , **_a , ) -> Optional[int]: _A : int = self.tokenizer( text=lowercase__ , add_special_tokens=lowercase__ , padding=lowercase__ , truncation=lowercase__ , max_length=lowercase__ , stride=lowercase__ , pad_to_multiple_of=lowercase__ , return_token_type_ids=lowercase__ , return_attention_mask=lowercase__ , return_overflowing_tokens=lowercase__ , return_special_tokens_mask=lowercase__ , return_offsets_mapping=lowercase__ , return_length=lowercase__ , verbose=lowercase__ , return_tensors=lowercase__ , **lowercase__ , ) # add pixel_values + pixel_mask _A : str = self.image_processor(lowercase__ , return_tensors=lowercase__ ) encoding.update(lowercase__ ) return encoding def a__ ( self , *_a , **_a ) -> Tuple: return self.tokenizer.batch_decode(*lowercase__ , **lowercase__ ) def a__ ( self , *_a , **_a ) -> Any: return self.tokenizer.decode(*lowercase__ , **lowercase__ ) @property def a__ ( self ) -> Optional[Any]: _A : Optional[Any] = self.tokenizer.model_input_names _A : str = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) ) @property def a__ ( self ) -> Optional[Any]: warnings.warn( """`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.""" , lowercase__ , ) return self.image_processor_class @property def a__ ( self ) -> Optional[Any]: warnings.warn( """`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.""" , lowercase__ , ) return self.image_processor
701
import gc import unittest from transformers import MODEL_FOR_MASKED_LM_MAPPING, TF_MODEL_FOR_MASKED_LM_MAPPING, FillMaskPipeline, pipeline from transformers.pipelines import PipelineException from transformers.testing_utils import ( is_pipeline_test, is_torch_available, nested_simplify, require_tf, require_torch, require_torch_gpu, slow, ) from .test_pipelines_common import ANY @is_pipeline_test class lowercase ( unittest.TestCase ): _a = MODEL_FOR_MASKED_LM_MAPPING _a = TF_MODEL_FOR_MASKED_LM_MAPPING def a__ ( self ) -> Tuple: super().tearDown() # clean-up as much as possible GPU memory occupied by PyTorch gc.collect() if is_torch_available(): import torch torch.cuda.empty_cache() @require_tf def a__ ( self ) -> Any: _A : Optional[Any] = pipeline(task="""fill-mask""" , model="""sshleifer/tiny-distilroberta-base""" , top_k=2 , framework="""tf""" ) _A : Optional[int] = unmasker("""My name is <mask>""" ) self.assertEqual( nested_simplify(_a , decimals=6 ) , [ {"""sequence""": """My name is grouped""", """score""": 2.1e-05, """token""": 3_8015, """token_str""": """ grouped"""}, {"""sequence""": """My name is accuser""", """score""": 2.1e-05, """token""": 2_5506, """token_str""": """ accuser"""}, ] , ) _A : Tuple = unmasker("""The largest city in France is <mask>""" ) self.assertEqual( nested_simplify(_a , decimals=6 ) , [ { """sequence""": """The largest city in France is grouped""", """score""": 2.1e-05, """token""": 3_8015, """token_str""": """ grouped""", }, { """sequence""": """The largest city in France is accuser""", """score""": 2.1e-05, """token""": 2_5506, """token_str""": """ accuser""", }, ] , ) _A : List[str] = unmasker("""My name is <mask>""" , targets=[""" Patrick""", """ Clara""", """ Teven"""] , top_k=3 ) self.assertEqual( nested_simplify(_a , decimals=6 ) , [ {"""sequence""": """My name is Clara""", """score""": 2e-05, """token""": 1_3606, """token_str""": """ Clara"""}, {"""sequence""": """My name is Patrick""", """score""": 2e-05, """token""": 3499, """token_str""": """ Patrick"""}, {"""sequence""": """My name is Te""", """score""": 1.9e-05, """token""": 2941, """token_str""": """ Te"""}, ] , ) @require_torch def a__ ( self ) -> str: _A : Any = pipeline(task="""fill-mask""" , model="""sshleifer/tiny-distilroberta-base""" , top_k=2 , framework="""pt""" ) _A : List[Any] = unmasker("""My name is <mask>""" ) self.assertEqual( nested_simplify(_a , decimals=6 ) , [ {"""sequence""": """My name is Maul""", """score""": 2.2e-05, """token""": 3_5676, """token_str""": """ Maul"""}, {"""sequence""": """My name isELS""", """score""": 2.2e-05, """token""": 1_6416, """token_str""": """ELS"""}, ] , ) _A : Optional[Any] = unmasker("""The largest city in France is <mask>""" ) self.assertEqual( nested_simplify(_a , decimals=6 ) , [ { """sequence""": """The largest city in France is Maul""", """score""": 2.2e-05, """token""": 3_5676, """token_str""": """ Maul""", }, {"""sequence""": """The largest city in France isELS""", """score""": 2.2e-05, """token""": 1_6416, """token_str""": """ELS"""}, ] , ) _A : Optional[int] = unmasker("""My name is <mask>""" , targets=[""" Patrick""", """ Clara""", """ Teven"""] , top_k=3 ) self.assertEqual( nested_simplify(_a , decimals=6 ) , [ {"""sequence""": """My name is Patrick""", """score""": 2.1e-05, """token""": 3499, """token_str""": """ Patrick"""}, {"""sequence""": """My name is Te""", """score""": 2e-05, """token""": 2941, """token_str""": """ Te"""}, {"""sequence""": """My name is Clara""", """score""": 2e-05, """token""": 1_3606, """token_str""": """ Clara"""}, ] , ) _A : str = unmasker("""My name is <mask> <mask>""" , top_k=2 ) self.assertEqual( nested_simplify(_a , decimals=6 ) , [ [ { """score""": 2.2e-05, """token""": 3_5676, """token_str""": """ Maul""", """sequence""": """<s>My name is Maul<mask></s>""", }, {"""score""": 2.2e-05, """token""": 1_6416, """token_str""": """ELS""", """sequence""": """<s>My name isELS<mask></s>"""}, ], [ { """score""": 2.2e-05, """token""": 3_5676, """token_str""": """ Maul""", """sequence""": """<s>My name is<mask> Maul</s>""", }, {"""score""": 2.2e-05, """token""": 1_6416, """token_str""": """ELS""", """sequence""": """<s>My name is<mask>ELS</s>"""}, ], ] , ) @require_torch_gpu def a__ ( self ) -> Union[str, Any]: _A : int = pipeline("""fill-mask""" , model="""hf-internal-testing/tiny-random-distilbert""" , device=0 , framework="""pt""" ) # convert model to fp16 pipe.model.half() _A : Optional[Any] = pipe("""Paris is the [MASK] of France.""" ) # We actually don't care about the result, we just want to make sure # it works, meaning the float16 tensor got casted back to float32 # for postprocessing. self.assertIsInstance(_a , _a ) @slow @require_torch def a__ ( self ) -> Optional[int]: _A : Optional[Any] = pipeline(task="""fill-mask""" , model="""distilroberta-base""" , top_k=2 , framework="""pt""" ) self.run_large_test(_a ) @slow @require_tf def a__ ( self ) -> Tuple: _A : str = pipeline(task="""fill-mask""" , model="""distilroberta-base""" , top_k=2 , framework="""tf""" ) self.run_large_test(_a ) def a__ ( self , _a ) -> Tuple: _A : Optional[int] = unmasker("""My name is <mask>""" ) self.assertEqual( nested_simplify(_a ) , [ {"""sequence""": """My name is John""", """score""": 0.008, """token""": 610, """token_str""": """ John"""}, {"""sequence""": """My name is Chris""", """score""": 0.007, """token""": 1573, """token_str""": """ Chris"""}, ] , ) _A : int = unmasker("""The largest city in France is <mask>""" ) self.assertEqual( nested_simplify(_a ) , [ { """sequence""": """The largest city in France is Paris""", """score""": 0.251, """token""": 2201, """token_str""": """ Paris""", }, { """sequence""": """The largest city in France is Lyon""", """score""": 0.214, """token""": 1_2790, """token_str""": """ Lyon""", }, ] , ) _A : Optional[Any] = unmasker("""My name is <mask>""" , targets=[""" Patrick""", """ Clara""", """ Teven"""] , top_k=3 ) self.assertEqual( nested_simplify(_a ) , [ {"""sequence""": """My name is Patrick""", """score""": 0.005, """token""": 3499, """token_str""": """ Patrick"""}, {"""sequence""": """My name is Clara""", """score""": 0.000, """token""": 1_3606, """token_str""": """ Clara"""}, {"""sequence""": """My name is Te""", """score""": 0.000, """token""": 2941, """token_str""": """ Te"""}, ] , ) @require_torch def a__ ( self ) -> Tuple: _A : List[str] = pipeline(task="""fill-mask""" , model="""sshleifer/tiny-distilroberta-base""" , framework="""pt""" ) _A : str = None _A : Union[str, Any] = None self.run_pipeline_test(_a , [] ) @require_tf def a__ ( self ) -> Union[str, Any]: _A : Tuple = pipeline(task="""fill-mask""" , model="""sshleifer/tiny-distilroberta-base""" , framework="""tf""" ) _A : Any = None _A : Dict = None self.run_pipeline_test(_a , [] ) def a__ ( self , _a , _a , _a ) -> Any: if tokenizer is None or tokenizer.mask_token_id is None: self.skipTest("""The provided tokenizer has no mask token, (probably reformer or wav2vec2)""" ) _A : Optional[Any] = FillMaskPipeline(model=_a , tokenizer=_a ) _A : Tuple = [ F'''This is another {tokenizer.mask_token} test''', ] return fill_masker, examples def a__ ( self , _a , _a ) -> Dict: _A : Dict = fill_masker.tokenizer _A : List[str] = fill_masker.model _A : List[str] = fill_masker( F'''This is a {tokenizer.mask_token}''' , ) self.assertEqual( _a , [ {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, ] , ) _A : Optional[Any] = fill_masker([F'''This is a {tokenizer.mask_token}'''] ) self.assertEqual( _a , [ {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, ] , ) _A : List[str] = fill_masker([F'''This is a {tokenizer.mask_token}''', F'''Another {tokenizer.mask_token} great test.'''] ) self.assertEqual( _a , [ [ {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, ], [ {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, ], ] , ) with self.assertRaises(_a ): fill_masker([None] ) # No mask_token is not supported with self.assertRaises(_a ): fill_masker("""This is""" ) self.run_test_top_k(_a , _a ) self.run_test_targets(_a , _a ) self.run_test_top_k_targets(_a , _a ) self.fill_mask_with_duplicate_targets_and_top_k(_a , _a ) self.fill_mask_with_multiple_masks(_a , _a ) def a__ ( self , _a , _a ) -> List[str]: _A : int = tokenizer.get_vocab() _A : str = sorted(vocab.keys() )[:2] # Pipeline argument _A : Tuple = FillMaskPipeline(model=_a , tokenizer=_a , targets=_a ) _A : Optional[int] = fill_masker(F'''This is a {tokenizer.mask_token}''' ) self.assertEqual( _a , [ {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, ] , ) _A : List[str] = {vocab[el] for el in targets} self.assertEqual({el["""token"""] for el in outputs} , _a ) _A : Union[str, Any] = [tokenizer.decode([x] ) for x in target_ids] self.assertEqual({el["""token_str"""] for el in outputs} , set(_a ) ) # Call argument _A : str = FillMaskPipeline(model=_a , tokenizer=_a ) _A : Optional[int] = fill_masker(F'''This is a {tokenizer.mask_token}''' , targets=_a ) self.assertEqual( _a , [ {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, ] , ) _A : int = {vocab[el] for el in targets} self.assertEqual({el["""token"""] for el in outputs} , _a ) _A : Any = [tokenizer.decode([x] ) for x in target_ids] self.assertEqual({el["""token_str"""] for el in outputs} , set(_a ) ) # Score equivalence _A : int = fill_masker(F'''This is a {tokenizer.mask_token}''' , targets=_a ) _A : Optional[int] = [top_mask["""token_str"""] for top_mask in outputs] _A : Union[str, Any] = [top_mask["""score"""] for top_mask in outputs] # For some BPE tokenizers, `</w>` is removed during decoding, so `token_str` won't be the same as in `targets`. if set(_a ) == set(_a ): _A : Tuple = fill_masker(F'''This is a {tokenizer.mask_token}''' , targets=_a ) _A : Union[str, Any] = [top_mask["""score"""] for top_mask in unmasked_targets] self.assertEqual(nested_simplify(_a ) , nested_simplify(_a ) ) # Raises with invalid with self.assertRaises(_a ): _A : str = fill_masker(F'''This is a {tokenizer.mask_token}''' , targets=[] ) # For some tokenizers, `""` is actually in the vocabulary and the expected error won't raised if "" not in tokenizer.get_vocab(): with self.assertRaises(_a ): _A : Optional[Any] = fill_masker(F'''This is a {tokenizer.mask_token}''' , targets=[""""""] ) with self.assertRaises(_a ): _A : int = fill_masker(F'''This is a {tokenizer.mask_token}''' , targets="""""" ) def a__ ( self , _a , _a ) -> Optional[Any]: _A : str = FillMaskPipeline(model=_a , tokenizer=_a , top_k=2 ) _A : str = fill_masker(F'''This is a {tokenizer.mask_token}''' ) self.assertEqual( _a , [ {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, ] , ) _A : Union[str, Any] = FillMaskPipeline(model=_a , tokenizer=_a ) _A : Union[str, Any] = fill_masker(F'''This is a {tokenizer.mask_token}''' , top_k=2 ) self.assertEqual( _a , [ {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, ] , ) self.assertEqual(nested_simplify(_a ) , nested_simplify(_a ) ) def a__ ( self , _a , _a ) -> List[Any]: _A : Union[str, Any] = tokenizer.get_vocab() _A : int = FillMaskPipeline(model=_a , tokenizer=_a ) # top_k=2, ntargets=3 _A : List[str] = sorted(vocab.keys() )[:3] _A : Tuple = fill_masker(F'''This is a {tokenizer.mask_token}''' , top_k=2 , targets=_a ) # If we use the most probably targets, and filter differently, we should still # have the same results _A : Any = [el["""token_str"""] for el in sorted(_a , key=lambda _a : x["score"] , reverse=_a )] # For some BPE tokenizers, `</w>` is removed during decoding, so `token_str` won't be the same as in `targets`. if set(_a ).issubset(_a ): _A : Any = fill_masker(F'''This is a {tokenizer.mask_token}''' , top_k=3 , targets=_a ) # They should yield exactly the same result self.assertEqual(nested_simplify(_a ) , nested_simplify(_a ) ) def a__ ( self , _a , _a ) -> str: _A : Optional[int] = FillMaskPipeline(model=_a , tokenizer=_a ) _A : List[Any] = tokenizer.get_vocab() # String duplicates + id duplicates _A : Optional[Any] = sorted(vocab.keys() )[:3] _A : Optional[Any] = [targets[0], targets[1], targets[0], targets[2], targets[1]] _A : Union[str, Any] = fill_masker(F'''My name is {tokenizer.mask_token}''' , targets=_a , top_k=10 ) # The target list contains duplicates, so we can't output more # than them self.assertEqual(len(_a ) , 3 ) def a__ ( self , _a , _a ) -> Tuple: _A : Any = FillMaskPipeline(model=_a , tokenizer=_a ) _A : Optional[Any] = fill_masker( F'''This is a {tokenizer.mask_token} {tokenizer.mask_token} {tokenizer.mask_token}''' , top_k=2 ) self.assertEqual( _a , [ [ {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, ], [ {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, ], [ {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, ], ] , )
54
0
from typing import Dict from transformers import EvalPrediction, HfArgumentParser, TrainingArguments, is_torch_available from transformers.testing_utils import ( TestCasePlus, execute_subprocess_async, get_torch_dist_unique_port, require_torch_multi_gpu, require_torch_neuroncore, ) from transformers.training_args import ParallelMode from transformers.utils import logging _snake_case = logging.get_logger(__name__) if is_torch_available(): import torch from torch import nn from torch.utils.data import Dataset from transformers import Trainer class lowercase ( UpperCamelCase__ ): def __init__( self , _a = 101 ) -> str: _A : int = length def __len__( self ) -> List[Any]: return self.length def __getitem__( self , _a ) -> List[str]: return i class lowercase : def __call__( self , _a ) -> int: return {"input_ids": torch.tensor(_a ), "labels": torch.tensor(_a )} class lowercase ( nn.Module ): def __init__( self ) -> Optional[Any]: super().__init__() # Add some (unused) params otherwise DDP will complain. _A : Union[str, Any] = nn.Linear(120 , 80 ) def a__ ( self , _a , _a=None ) -> str: if labels is not None: return torch.tensor(0.0 , device=input_ids.device ), input_ids else: return input_ids class lowercase ( UpperCamelCase__ ): @require_torch_neuroncore def a__ ( self ) -> int: _A : Union[str, Any] = F'''--nproc_per_node=2 --master_port={get_torch_dist_unique_port()} {self.test_file_dir}/test_trainer_distributed.py '''.split() _A : str = self.get_auto_remove_tmp_dir() _A : str = F'''--output_dir {output_dir}'''.split() _A : Optional[Any] = ['torchrun'] + distributed_args + args execute_subprocess_async(_a , env=self.get_env() ) # successful return here == success - any errors would have caused an error in the sub-call class lowercase ( UpperCamelCase__ ): @require_torch_multi_gpu def a__ ( self ) -> Optional[Any]: _A : int = F'''--nproc_per_node={torch.cuda.device_count()} --master_port={get_torch_dist_unique_port()} {self.test_file_dir}/test_trainer_distributed.py '''.split() _A : Union[str, Any] = self.get_auto_remove_tmp_dir() _A : List[Any] = F'''--output_dir {output_dir}'''.split() _A : Optional[int] = ['torchrun'] + distributed_args + args execute_subprocess_async(_a , env=self.get_env() ) # successful return here == success - any errors would have caused an error in the sub-call if __name__ == "__main__": # The script below is meant to be run under torch.distributed, on a machine with multiple GPUs: # # PYTHONPATH="src" python -m torch.distributed.run --nproc_per_node 2 --output_dir output_dir ./tests/test_trainer_distributed.py _snake_case = HfArgumentParser((TrainingArguments,)) _snake_case = parser.parse_args_into_dataclasses()[0] logger.warning( f"""Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}, """ f"""distributed training: {training_args.parallel_mode != ParallelMode.NOT_DISTRIBUTED}""" ) # Essentially, what we want to verify in the distributed case is that we get all samples back, # in the right order. (this is crucial for prediction for instance) for dataset_length in [101, 40, 7]: _snake_case = DummyDataset(dataset_length) def lowerCAmelCase_ ( snake_case_ ): _A : Union[str, Any] = list(range(len(SCREAMING_SNAKE_CASE_ ) ) ) _A : Dict = p.predictions.tolist() == sequential and p.label_ids.tolist() == sequential if not success and training_args.local_rank == 0: logger.warning( """Predictions and/or labels do not match expected results:\n - predictions: """ f'''{p.predictions.tolist()}\n - labels: {p.label_ids.tolist()}\n - expected: {sequential}''' ) return {"success": success} _snake_case = Trainer( model=DummyModel(), args=training_args, data_collator=DummyDataCollator(), eval_dataset=dataset, compute_metrics=compute_metrics, ) _snake_case = trainer.evaluate() logger.info(metrics) if metrics["eval_success"] is not True: logger.error(metrics) exit(1) _snake_case = trainer.predict(dataset) logger.info(p.metrics) if p.metrics["test_success"] is not True: logger.error(p.metrics) exit(1) _snake_case = 2 _snake_case = trainer.evaluate() logger.info(metrics) if metrics["eval_success"] is not True: logger.error(metrics) exit(1) _snake_case = trainer.predict(dataset) logger.info(p.metrics) if p.metrics["test_success"] is not True: logger.error(p.metrics) exit(1) _snake_case = None
702
import json import os import unittest from transformers import CLIPTokenizer, CLIPTokenizerFast from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES from transformers.testing_utils import require_ftfy, require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class lowercase ( UpperCamelCase__,unittest.TestCase ): _a = CLIPTokenizer _a = CLIPTokenizerFast _a = True _a = {} _a = False def a__ ( self ) -> Optional[Any]: super().setUp() # fmt: off _A : int = ["""l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """lo""", """l</w>""", """w</w>""", """r</w>""", """t</w>""", """low</w>""", """er</w>""", """lowest</w>""", """newer</w>""", """wider""", """<unk>""", """<|startoftext|>""", """<|endoftext|>"""] # fmt: on _A : str = dict(zip(_a , range(len(_a ) ) ) ) _A : Optional[int] = ["""#version: 0.2""", """l o""", """lo w</w>""", """e r</w>"""] _A : str = {"""unk_token""": """<unk>"""} _A : Union[str, Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""vocab_file"""] ) _A : Union[str, Any] = 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(_a ) + """\n""" ) with open(self.merges_file , """w""" , encoding="""utf-8""" ) as fp: fp.write("""\n""".join(_a ) ) def a__ ( self , **_a ) -> List[Any]: kwargs.update(self.special_tokens_map ) return CLIPTokenizer.from_pretrained(self.tmpdirname , **_a ) def a__ ( self , **_a ) -> Union[str, Any]: kwargs.update(self.special_tokens_map ) return CLIPTokenizerFast.from_pretrained(self.tmpdirname , **_a ) def a__ ( self , _a ) -> str: _A : Tuple = """lower newer""" _A : Optional[Any] = """lower newer""" return input_text, output_text def a__ ( self ) -> List[Any]: _A : Optional[int] = CLIPTokenizer(self.vocab_file , self.merges_file , **self.special_tokens_map ) _A : str = """lower newer""" _A : List[str] = ["""lo""", """w""", """er</w>""", """n""", """e""", """w""", """er</w>"""] _A : int = tokenizer.tokenize(_a ) self.assertListEqual(_a , _a ) _A : str = tokens + [tokenizer.unk_token] _A : Dict = [10, 2, 16, 9, 3, 2, 16, 20] self.assertListEqual(tokenizer.convert_tokens_to_ids(_a ) , _a ) @require_ftfy def a__ ( self ) -> Tuple: for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): _A : Dict = self.tokenizer_class.from_pretrained(_a , **_a ) _A : str = self.rust_tokenizer_class.from_pretrained(_a , **_a ) _A : List[str] = """A\n'll 11p223RF☆ho!!to?'d'd''d of a cat to-$''d.""" _A : Dict = tokenizer_s.tokenize(_a ) _A : Dict = tokenizer_r.tokenize(_a ) self.assertListEqual(_a , _a ) # Test that the tokenization is identical on an example containing a character (Latin Small Letter A # with Tilde) encoded in 2 different ways _A : Tuple = """xa\u0303y""" + """ """ + """x\xe3y""" _A : Dict = tokenizer_s.tokenize(_a ) _A : Dict = tokenizer_r.tokenize(_a ) self.assertListEqual(_a , _a ) # Test that the tokenization is identical on unicode of space type _A : Any = [ """\u0009""", # (horizontal tab, '\t') """\u000B""", # (vertical tab) """\u000C""", # (form feed) """\u0020""", # (space, ' ') """\u200E""", # (left-to-right mark):w """\u200F""", # (right-to-left mark) ] for unicode_seq in spaces_unicodes: _A : Optional[int] = tokenizer_s.tokenize(_a ) _A : List[Any] = tokenizer_r.tokenize(_a ) self.assertListEqual(_a , _a ) # Test that the tokenization is identical on unicode of line break type _A : int = [ """\u000A""", # (line feed, '\n') """\r\n""", # (carriage return and line feed, '\r\n') """\u000D""", # (carriage return, '\r') """\r""", # (carriage return, '\r') """\u000D""", # (carriage return, '\r') """\u2028""", # (line separator) """\u2029""", # (paragraph separator) # "\u0085", # (next line) ] # The tokenization is not identical for the character "\u0085" (next line). The slow version using ftfy transforms # it into the Horizontal Ellipsis character "…" ("\u2026") while the fast version transforms it into a # space (and thus into an empty list). for unicode_seq in line_break_unicodes: _A : Optional[Any] = tokenizer_s.tokenize(_a ) _A : Tuple = tokenizer_r.tokenize(_a ) self.assertListEqual(_a , _a ) def a__ ( self ) -> Any: # Test which aims to verify that the offsets are well adapted to the argument `add_prefix_space` for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): _A : List[Any] = """hello""" # `hello` is a token in the vocabulary of `pretrained_name` _A : str = F'''{text_of_1_token} {text_of_1_token}''' _A : str = self.rust_tokenizer_class.from_pretrained( _a , use_fast=_a , ) _A : Dict = tokenizer_r(_a , return_offsets_mapping=_a , add_special_tokens=_a ) self.assertEqual(encoding.offset_mapping[0] , (0, len(_a )) ) self.assertEqual( encoding.offset_mapping[1] , (len(_a ) + 1, len(_a ) + 1 + len(_a )) , ) _A : Any = F''' {text}''' _A : List[Any] = self.rust_tokenizer_class.from_pretrained( _a , use_fast=_a , ) _A : Optional[int] = tokenizer_r(_a , return_offsets_mapping=_a , add_special_tokens=_a ) self.assertEqual(encoding.offset_mapping[0] , (1, 1 + len(_a )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(_a ) + 1, 1 + len(_a ) + 1 + len(_a )) , ) def a__ ( self ) -> List[Any]: # Test related to the breaking change introduced in transformers v4.17.0 # We need to check that an error in raised when the user try to load a previous version of the tokenizer. with self.assertRaises(_a ) as context: self.rust_tokenizer_class.from_pretrained("""robot-test/old-clip-tokenizer""" ) self.assertTrue( context.exception.args[0].startswith( """The `backend_tokenizer` provided does not match the expected format.""" ) ) @require_ftfy def a__ ( self ) -> str: super().test_tokenization_python_rust_equals() def a__ ( self ) -> Union[str, Any]: # CLIP always lower cases letters pass
54
0
_snake_case = 8.3_1_4_4_5_9_8 def lowerCAmelCase_ ( snake_case_,snake_case_ ): if temperature < 0: raise Exception("""Temperature cannot be less than 0 K""" ) if molar_mass <= 0: raise Exception("""Molar mass cannot be less than or equal to 0 kg/mol""" ) else: return (3 * UNIVERSAL_GAS_CONSTANT * temperature / molar_mass) ** 0.5 if __name__ == "__main__": import doctest # run doctest doctest.testmod() # example _snake_case = 300 _snake_case = 28 _snake_case = rms_speed_of_molecule(temperature, molar_mass) print(f"""Vrms of Nitrogen gas at 300 K is {vrms} m/s""")
703
from datetime import datetime as dt import os from github import Github _snake_case = [ "good first issue", "good second issue", "good difficult issue", "feature request", "new model", "wip", ] def lowerCAmelCase_ ( ): _A : int = Github(os.environ["""GITHUB_TOKEN"""] ) _A : Tuple = g.get_repo("""huggingface/transformers""" ) _A : Dict = repo.get_issues(state="""open""" ) for issue in open_issues: _A : Optional[Any] = sorted([comment for comment in issue.get_comments()],key=lambda snake_case_ : i.created_at,reverse=snake_case_ ) _A : Dict = comments[0] if len(snake_case_ ) > 0 else None if ( last_comment is not None and last_comment.user.login == "github-actions[bot]" and (dt.utcnow() - issue.updated_at).days > 7 and (dt.utcnow() - issue.created_at).days >= 30 and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels() ) ): # print(f"Would close issue {issue.number} since it has been 7 days of inactivity since bot mention.") issue.edit(state="""closed""" ) elif ( (dt.utcnow() - issue.updated_at).days > 23 and (dt.utcnow() - issue.created_at).days >= 30 and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels() ) ): # print(f"Would add stale comment to {issue.number}") issue.create_comment( """This issue has been automatically marked as stale because it has not had """ """recent activity. If you think this still needs to be addressed """ """please comment on this thread.\n\nPlease note that issues that do not follow the """ """[contributing guidelines](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) """ """are likely to be ignored.""" ) if __name__ == "__main__": main()
54
0
from typing import Optional from torch import nn from .transformer_ad import TransformeraDModel, TransformeraDModelOutput class lowercase ( nn.Module ): def __init__( self , _a = 16 , _a = 88 , _a = None , _a = 1 , _a = 0.0 , _a = 32 , _a = None , _a = False , _a = None , _a = None , _a = "geglu" , _a = None , ) -> List[Any]: super().__init__() _A : List[str] = nn.ModuleList( [ TransformeraDModel( num_attention_heads=_A , attention_head_dim=_A , in_channels=_A , num_layers=_A , dropout=_A , norm_num_groups=_A , cross_attention_dim=_A , attention_bias=_A , sample_size=_A , num_vector_embeds=_A , activation_fn=_A , num_embeds_ada_norm=_A , ) for _ in range(2 ) ] ) # Variables that can be set by a pipeline: # The ratio of transformer1 to transformer2's output states to be combined during inference _A : List[Any] = 0.5 # The shape of `encoder_hidden_states` is expected to be # `(batch_size, condition_lengths[0]+condition_lengths[1], num_features)` _A : Any = [77, 257] # Which transformer to use to encode which condition. # E.g. `(1, 0)` means that we'll use `transformers[1](conditions[0])` and `transformers[0](conditions[1])` _A : Any = [1, 0] def a__ ( self , _a , _a , _a=None , _a=None , _a=None , _a = True , ) -> Any: _A : List[str] = hidden_states _A : Optional[Any] = [] _A : Union[str, Any] = 0 # attention_mask is not used yet for i in range(2 ): # for each of the two transformers, pass the corresponding condition tokens _A : Union[str, Any] = encoder_hidden_states[:, tokens_start : tokens_start + self.condition_lengths[i]] _A : int = self.transformer_index_for_condition[i] _A : List[Any] = self.transformers[transformer_index]( _A , encoder_hidden_states=_A , timestep=_A , cross_attention_kwargs=_A , return_dict=_A , )[0] encoded_states.append(encoded_state - input_states ) tokens_start += self.condition_lengths[i] _A : Optional[int] = encoded_states[0] * self.mix_ratio + encoded_states[1] * (1 - self.mix_ratio) _A : int = output_states + input_states if not return_dict: return (output_states,) return TransformeraDModelOutput(sample=_A )
704
from __future__ import annotations class lowercase : def __init__( self , _a = 0 ) -> str: _A : Any = key def a__ ( self , _a , _a ) -> list[str]: assert isinstance(_a , _a ) and isinstance(_a , _a ) _A : Any = key or self.__key or 1 # make sure key is an appropriate size key %= 255 return [chr(ord(_a ) ^ key ) for ch in content] def a__ ( self , _a , _a ) -> list[str]: assert isinstance(_a , _a ) and isinstance(_a , _a ) _A : List[Any] = key or self.__key or 1 # make sure key is an appropriate size key %= 255 return [chr(ord(_a ) ^ key ) for ch in content] def a__ ( self , _a , _a = 0 ) -> str: assert isinstance(_a , _a ) and isinstance(_a , _a ) _A : List[Any] = key or self.__key or 1 # make sure key can be any size while key > 255: key -= 255 # This will be returned _A : List[str] = """""" for ch in content: ans += chr(ord(_a ) ^ key ) return ans def a__ ( self , _a , _a = 0 ) -> str: assert isinstance(_a , _a ) and isinstance(_a , _a ) _A : List[str] = key or self.__key or 1 # make sure key can be any size while key > 255: key -= 255 # This will be returned _A : List[str] = """""" for ch in content: ans += chr(ord(_a ) ^ key ) return ans def a__ ( self , _a , _a = 0 ) -> bool: assert isinstance(_a , _a ) and isinstance(_a , _a ) try: with open(_a ) as fin, open("""encrypt.out""" , """w+""" ) as fout: # actual encrypt-process for line in fin: fout.write(self.encrypt_string(_a , _a ) ) except OSError: return False return True def a__ ( self , _a , _a ) -> bool: assert isinstance(_a , _a ) and isinstance(_a , _a ) try: with open(_a ) as fin, open("""decrypt.out""" , """w+""" ) as fout: # actual encrypt-process for line in fin: fout.write(self.decrypt_string(_a , _a ) ) except OSError: return False return True # Tests # crypt = XORCipher() # key = 67 # # test encrypt # print(crypt.encrypt("hallo welt",key)) # # test decrypt # print(crypt.decrypt(crypt.encrypt("hallo welt",key), key)) # # test encrypt_string # print(crypt.encrypt_string("hallo welt",key)) # # test decrypt_string # print(crypt.decrypt_string(crypt.encrypt_string("hallo welt",key),key)) # if (crypt.encrypt_file("test.txt",key)): # print("encrypt successful") # else: # print("encrypt unsuccessful") # if (crypt.decrypt_file("encrypt.out",key)): # print("decrypt successful") # else: # print("decrypt unsuccessful")
54
0
import warnings from diffusers import StableDiffusionImgaImgPipeline # noqa F401 warnings.warn( "The `image_to_image.py` script is outdated. Please use directly `from diffusers import" " StableDiffusionImg2ImgPipeline` instead." )
705
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 = random.Random() def lowerCAmelCase_ ( snake_case_,snake_case_=1.0,snake_case_=None,snake_case_=None ): if rng is None: _A : str = global_rng _A : List[Any] = [] 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 lowercase ( unittest.TestCase ): def __init__( self , _a , _a=7 , _a=400 , _a=2000 , _a=10 , _a=160 , _a=8 , _a=0.0 , _a=4000 , _a=False , _a=True , ) -> Optional[int]: _A : Any = parent _A : List[Any] = batch_size _A : List[Any] = min_seq_length _A : Dict = max_seq_length _A : Optional[Any] = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) _A : Tuple = padding_value _A : Tuple = sampling_rate _A : str = return_attention_mask _A : Any = do_normalize _A : Union[str, Any] = feature_size _A : List[Any] = chunk_length _A : List[Any] = hop_length def a__ ( self ) -> List[str]: 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 a__ ( self , _a=False , _a=False ) -> List[str]: def _flatten(_a ): return list(itertools.chain(*_a ) ) 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 : int = [ 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 : Any = [np.asarray(_a ) for x in speech_inputs] return speech_inputs @require_torch @require_torchaudio class lowercase ( UpperCamelCase__,unittest.TestCase ): _a = WhisperFeatureExtractor if is_speech_available() else None def a__ ( self ) -> Tuple: _A : Optional[int] = WhisperFeatureExtractionTester(self ) def a__ ( self ) -> Optional[Any]: _A : List[Any] = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: _A : List[str] = feat_extract_first.save_pretrained(_a )[0] check_json_file_has_correct_format(_a ) _A : Optional[int] = self.feature_extraction_class.from_pretrained(_a ) _A : Tuple = feat_extract_first.to_dict() _A : List[Any] = feat_extract_second.to_dict() _A : List[Any] = feat_extract_first.mel_filters _A : Union[str, Any] = feat_extract_second.mel_filters self.assertTrue(np.allclose(_a , _a ) ) self.assertEqual(_a , _a ) def a__ ( self ) -> Dict: _A : Any = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: _A : Dict = os.path.join(_a , """feat_extract.json""" ) feat_extract_first.to_json_file(_a ) _A : Optional[int] = self.feature_extraction_class.from_json_file(_a ) _A : str = feat_extract_first.to_dict() _A : Any = feat_extract_second.to_dict() _A : Union[str, Any] = feat_extract_first.mel_filters _A : Union[str, Any] = feat_extract_second.mel_filters self.assertTrue(np.allclose(_a , _a ) ) self.assertEqual(_a , _a ) def a__ ( self ) -> Union[str, Any]: # Tests that all call wrap to encode_plus and batch_encode_plus _A : Optional[int] = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 _A : Optional[Any] = [floats_list((1, x) )[0] for x in range(800 , 1400 , 200 )] _A : Any = [np.asarray(_a ) for speech_input in speech_inputs] # Test feature size _A : Dict = feature_extractor(_a , 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 : List[Any] = feature_extractor(speech_inputs[0] , return_tensors="""np""" ).input_features _A : List[str] = feature_extractor(np_speech_inputs[0] , return_tensors="""np""" ).input_features self.assertTrue(np.allclose(_a , _a , atol=1e-3 ) ) # Test batched _A : Union[str, Any] = feature_extractor(_a , return_tensors="""np""" ).input_features _A : Tuple = feature_extractor(_a , return_tensors="""np""" ).input_features for enc_seq_a, enc_seq_a in zip(_a , _a ): self.assertTrue(np.allclose(_a , _a , atol=1e-3 ) ) # Test 2-D numpy arrays are batched. _A : List[str] = [floats_list((1, x) )[0] for x in (800, 800, 800)] _A : Any = np.asarray(_a ) _A : Union[str, Any] = feature_extractor(_a , return_tensors="""np""" ).input_features _A : int = feature_extractor(_a , return_tensors="""np""" ).input_features for enc_seq_a, enc_seq_a in zip(_a , _a ): self.assertTrue(np.allclose(_a , _a , atol=1e-3 ) ) # Test truncation required _A : List[Any] = [floats_list((1, x) )[0] for x in range(200 , (feature_extractor.n_samples + 500) , 200 )] _A : Union[str, Any] = [np.asarray(_a ) for speech_input in speech_inputs] _A : Tuple = [x[: feature_extractor.n_samples] for x in speech_inputs] _A : Union[str, Any] = [np.asarray(_a ) for speech_input in speech_inputs_truncated] _A : Optional[int] = feature_extractor(_a , return_tensors="""np""" ).input_features _A : List[Any] = feature_extractor(_a , return_tensors="""np""" ).input_features for enc_seq_a, enc_seq_a in zip(_a , _a ): self.assertTrue(np.allclose(_a , _a , atol=1e-3 ) ) def a__ ( self ) -> Dict: import torch _A : Dict = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) _A : Optional[int] = np.random.rand(100 , 32 ).astype(np.floataa ) _A : str = np_speech_inputs.tolist() for inputs in [py_speech_inputs, np_speech_inputs]: _A : Optional[Any] = feature_extractor.pad([{"""input_features""": inputs}] , return_tensors="""np""" ) self.assertTrue(np_processed.input_features.dtype == np.floataa ) _A : Optional[int] = feature_extractor.pad([{"""input_features""": inputs}] , return_tensors="""pt""" ) self.assertTrue(pt_processed.input_features.dtype == torch.floataa ) def a__ ( self , _a ) -> Dict: _A : Optional[Any] = load_dataset("""hf-internal-testing/librispeech_asr_dummy""" , """clean""" , split="""validation""" ) # automatic decoding with librispeech _A : Optional[Any] = ds.sort("""id""" ).select(range(_a ) )[:num_samples]["""audio"""] return [x["array"] for x in speech_samples] def a__ ( self ) -> Tuple: # fmt: off _A : Dict = torch.tensor( [ 0.1193, -0.0946, -0.1098, -0.0196, 0.0225, -0.0690, -0.1736, 0.0951, 0.0971, -0.0817, -0.0702, 0.0162, 0.0260, 0.0017, -0.0192, -0.1678, 0.0709, -0.1867, -0.0655, -0.0274, -0.0234, -0.1884, -0.0516, -0.0554, -0.0274, -0.1425, -0.1423, 0.0837, 0.0377, -0.0854 ] ) # fmt: on _A : Dict = self._load_datasamples(1 ) _A : Optional[Any] = WhisperFeatureExtractor() _A : Optional[Any] = feature_extractor(_a , return_tensors="""pt""" ).input_features self.assertEqual(input_features.shape , (1, 80, 3000) ) self.assertTrue(torch.allclose(input_features[0, 0, :30] , _a , atol=1e-4 ) ) def a__ ( self ) -> str: _A : Dict = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) _A : str = self._load_datasamples(1 )[0] _A : Union[str, Any] = ((audio - audio.min()) / (audio.max() - audio.min())) * 6_5535 # Rescale to [0, 65535] to show issue _A : List[Any] = feat_extract.zero_mean_unit_var_norm([audio] , attention_mask=_a )[0] self.assertTrue(np.all(np.mean(_a ) < 1e-3 ) ) self.assertTrue(np.all(np.abs(np.var(_a ) - 1 ) < 1e-3 ) )
54
0
from __future__ import annotations def lowerCAmelCase_ ( snake_case_,snake_case_ ): _A : list[list[int]] = [] _A : list[int] = [] _A : str = 0 _A : Optional[Any] = sum(lowerCAmelCase_ ) create_state_space_tree(lowerCAmelCase_,lowerCAmelCase_,lowerCAmelCase_,lowerCAmelCase_,lowerCAmelCase_,lowerCAmelCase_ ) return result def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_,snake_case_,snake_case_,snake_case_,): if sum(lowerCAmelCase_ ) > max_sum or (remaining_nums_sum + sum(lowerCAmelCase_ )) < max_sum: return if sum(lowerCAmelCase_ ) == max_sum: result.append(lowerCAmelCase_ ) return for index in range(lowerCAmelCase_,len(lowerCAmelCase_ ) ): create_state_space_tree( lowerCAmelCase_,lowerCAmelCase_,index + 1,[*path, nums[index]],lowerCAmelCase_,remaining_nums_sum - nums[index],) _snake_case = [3, 34, 4, 12, 5, 2] _snake_case = 9 _snake_case = generate_sum_of_subsets_soln(nums, max_sum) print(*result)
706
def lowerCAmelCase_ ( snake_case_,snake_case_ ): _A : Union[str, Any] = """""" for i in table: res += inp[i - 1] return res def lowerCAmelCase_ ( snake_case_ ): return data[1:] + data[0] def lowerCAmelCase_ ( snake_case_,snake_case_ ): _A : Dict = """""" for i in range(len(snake_case_ ) ): if a[i] == b[i]: res += "0" else: res += "1" return res def lowerCAmelCase_ ( snake_case_,snake_case_ ): _A : int = int("""0b""" + data[0] + data[-1],2 ) _A : Any = int("""0b""" + data[1:3],2 ) return bin(s[row][col] )[2:] def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_,snake_case_,snake_case_ ): _A : List[str] = message[:4] _A : List[Any] = message[4:] _A : Union[str, Any] = apply_table(snake_case_,snake_case_ ) _A : List[Any] = xor(snake_case_,snake_case_ ) _A : Optional[Any] = apply_sbox(snake_case_,temp[:4] ) # noqa: E741 _A : List[Any] = apply_sbox(snake_case_,temp[4:] ) _A : int = """0""" * (2 - len(snake_case_ )) + l # noqa: E741 _A : Union[str, Any] = """0""" * (2 - len(snake_case_ )) + r _A : List[Any] = apply_table(l + r,snake_case_ ) _A : Any = xor(snake_case_,snake_case_ ) return temp + right if __name__ == "__main__": _snake_case = input("Enter 10 bit key: ") _snake_case = input("Enter 8 bit message: ") _snake_case = [6, 3, 7, 4, 8, 5, 10, 9] _snake_case = [3, 5, 2, 7, 4, 10, 1, 9, 8, 6] _snake_case = [2, 4, 3, 1] _snake_case = [2, 6, 3, 1, 4, 8, 5, 7] _snake_case = [4, 1, 3, 5, 7, 2, 8, 6] _snake_case = [4, 1, 2, 3, 2, 3, 4, 1] _snake_case = [[1, 0, 3, 2], [3, 2, 1, 0], [0, 2, 1, 3], [3, 1, 3, 2]] _snake_case = [[0, 1, 2, 3], [2, 0, 1, 3], [3, 0, 1, 0], [2, 1, 0, 3]] # key generation _snake_case = apply_table(key, paa_table) _snake_case = temp[:5] _snake_case = temp[5:] _snake_case = left_shift(left) _snake_case = left_shift(right) _snake_case = apply_table(left + right, pa_table) _snake_case = left_shift(left) _snake_case = left_shift(right) _snake_case = left_shift(left) _snake_case = left_shift(right) _snake_case = apply_table(left + right, pa_table) # encryption _snake_case = apply_table(message, IP) _snake_case = function(expansion, sa, sa, keya, temp) _snake_case = temp[4:] + temp[:4] _snake_case = function(expansion, sa, sa, keya, temp) _snake_case = apply_table(temp, IP_inv) print("Cipher text is:", CT) # decryption _snake_case = apply_table(CT, IP) _snake_case = function(expansion, sa, sa, keya, temp) _snake_case = temp[4:] + temp[:4] _snake_case = function(expansion, sa, sa, keya, temp) _snake_case = apply_table(temp, IP_inv) print("Plain text after decypting is:", PT)
54
0
import os from collections import deque import torch from torch.utils.data import Dataset class lowercase ( UpperCamelCase__ ): def __init__( self , _a="" , _a="train" ) -> Optional[Any]: assert os.path.isdir(_a ) _A : Any = [] _A : Any = os.listdir(_a ) for story_filename in story_filenames_list: if "summary" in story_filename: continue _A : Dict = os.path.join(_a , _a ) if not os.path.isfile(_a ): continue self.documents.append(_a ) def __len__( self ) -> Optional[int]: return len(self.documents ) def __getitem__( self , _a ) -> Optional[Any]: _A : Tuple = self.documents[idx] _A : Optional[int] = document_path.split("""/""" )[-1] with open(_a , encoding="""utf-8""" ) as source: _A : int = source.read() _A , _A : Dict = process_story(_a ) return document_name, story_lines, summary_lines def lowerCAmelCase_ ( snake_case_ ): _A : List[str] = list(filter(lambda snake_case_ : len(_snake_case ) != 0,[line.strip() for line in raw_story.split("""\n""" )] ) ) # for some unknown reason some lines miss a period, add it _A : Dict = [_add_missing_period(_snake_case ) for line in nonempty_lines] # gather article lines _A : Union[str, Any] = [] _A : str = deque(_snake_case ) while True: try: _A : Tuple = lines.popleft() if element.startswith("""@highlight""" ): break story_lines.append(_snake_case ) except IndexError: # if "@highlight" is absent from the file we pop # all elements until there is None, raising an exception. return story_lines, [] # gather summary lines _A : int = list(filter(lambda snake_case_ : not t.startswith("""@highlight""" ),_snake_case ) ) return story_lines, summary_lines def lowerCAmelCase_ ( snake_case_ ): _A : str = [""".""", """!""", """?""", """...""", """'""", """`""", """\"""", """\u2019""", """\u2019""", """)"""] if line.startswith("""@highlight""" ): return line if line[-1] in END_TOKENS: return line return line + "." def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_ ): if len(_snake_case ) > block_size: return sequence[:block_size] else: sequence.extend([pad_token_id] * (block_size - len(_snake_case )) ) return sequence def lowerCAmelCase_ ( snake_case_,snake_case_ ): _A : List[Any] = torch.ones_like(_snake_case ) _A : Any = sequence == pad_token_id _A : Any = 0 return mask def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_ ): _A : Tuple = [tokenizer.encode(_snake_case ) for line in story_lines] _A : List[str] = [token for sentence in story_lines_token_ids for token in sentence] _A : Any = [tokenizer.encode(_snake_case ) for line in summary_lines] _A : Union[str, Any] = [token for sentence in summary_lines_token_ids for token in sentence] return story_token_ids, summary_token_ids def lowerCAmelCase_ ( snake_case_,snake_case_ ): _A : str = [] for sequence in batch: _A : List[Any] = -1 _A : Optional[int] = [] for s in sequence: if s == separator_token_id: sentence_num += 1 embeddings.append(sentence_num % 2 ) batch_embeddings.append(_snake_case ) return torch.tensor(_snake_case )
707
import unittest import numpy as np from transformers.file_utils import is_torch_available, is_vision_available from transformers.testing_utils import require_torch, require_vision 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 DPTImageProcessor class lowercase ( unittest.TestCase ): def __init__( self , _a , _a=7 , _a=3 , _a=18 , _a=30 , _a=400 , _a=True , _a=None , _a=True , _a=[0.5, 0.5, 0.5] , _a=[0.5, 0.5, 0.5] , ) -> Tuple: _A : Any = size if size is not None else {"""height""": 18, """width""": 18} _A : Optional[Any] = parent _A : Union[str, Any] = batch_size _A : List[Any] = num_channels _A : List[str] = image_size _A : Optional[Any] = min_resolution _A : List[Any] = max_resolution _A : Optional[Any] = do_resize _A : str = size _A : List[str] = do_normalize _A : Dict = image_mean _A : int = image_std def a__ ( self ) -> Any: 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 ( UpperCamelCase__,unittest.TestCase ): _a = DPTImageProcessor if is_vision_available() else None def a__ ( self ) -> Optional[int]: _A : Optional[Any] = DPTImageProcessingTester(self ) @property def a__ ( self ) -> Tuple: return self.image_processor_tester.prepare_image_processor_dict() def a__ ( self ) -> Any: _A : List[str] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_a , """image_mean""" ) ) self.assertTrue(hasattr(_a , """image_std""" ) ) self.assertTrue(hasattr(_a , """do_normalize""" ) ) self.assertTrue(hasattr(_a , """do_resize""" ) ) self.assertTrue(hasattr(_a , """size""" ) ) def a__ ( self ) -> Any: _A : List[str] = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"""height""": 18, """width""": 18} ) _A : str = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {"""height""": 42, """width""": 42} ) def a__ ( self ) -> List[Any]: # Initialize image_processing _A : Optional[int] = self.image_processing_class(**self.image_processor_dict ) # create random PIL images _A : Any = prepare_image_inputs(self.image_processor_tester , equal_resolution=_a ) for image in image_inputs: self.assertIsInstance(_a , Image.Image ) # Test not batched input _A : Union[str, 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 : int = image_processing(_a , 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 a__ ( self ) -> Union[str, Any]: # Initialize image_processing _A : List[Any] = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors _A : Optional[Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=_a , numpify=_a ) for image in image_inputs: self.assertIsInstance(_a , np.ndarray ) # Test not batched input _A : Dict = 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(_a , 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 a__ ( self ) -> List[str]: # Initialize image_processing _A : Dict = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors _A : str = prepare_image_inputs(self.image_processor_tester , equal_resolution=_a , torchify=_a ) for image in image_inputs: self.assertIsInstance(_a , torch.Tensor ) # Test not batched input _A : Optional[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 : int = image_processing(_a , 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"""], ) , )
54
0
from collections import namedtuple import requests from lxml import html # type: ignore _snake_case = namedtuple("covid_data", "cases deaths recovered") def lowerCAmelCase_ ( snake_case_ = "https://www.worldometers.info/coronavirus/" ): _A : Optional[Any] = """//div[@class = \"maincounter-number\"]/span/text()""" return covid_data(*html.fromstring(requests.get(_A ).content ).xpath(_A ) ) _snake_case = '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()))
708
from typing import Any, Dict, List, Optional, Tuple, Union import torch from torch import nn from torch.utils.data import DistributedSampler, RandomSampler from transformers import PreTrainedModel, Trainer, logging from transformers.integrations import is_fairscale_available from transformers.models.fsmt.configuration_fsmt import FSMTConfig from transformers.optimization import ( Adafactor, AdamW, get_constant_schedule, get_constant_schedule_with_warmup, get_cosine_schedule_with_warmup, get_cosine_with_hard_restarts_schedule_with_warmup, get_linear_schedule_with_warmup, get_polynomial_decay_schedule_with_warmup, ) from transformers.trainer_pt_utils import get_tpu_sampler from transformers.training_args import ParallelMode from transformers.utils import is_torch_tpu_available if is_fairscale_available(): from fairscale.optim import OSS _snake_case = logging.get_logger(__name__) _snake_case = { "linear": get_linear_schedule_with_warmup, "cosine": get_cosine_schedule_with_warmup, "cosine_w_restarts": get_cosine_with_hard_restarts_schedule_with_warmup, "polynomial": get_polynomial_decay_schedule_with_warmup, "constant": get_constant_schedule, "constant_w_warmup": get_constant_schedule_with_warmup, } class lowercase ( UpperCamelCase__ ): def __init__( self , _a=None , _a=None , *_a , **_a ) -> Optional[int]: super().__init__(*_a , **_a ) if config is None: assert isinstance(self.model , _a ), ( "If no `config` is passed the model to be trained has to be of type `PreTrainedModel`, but is" F''' {self.model.__class__}''' ) _A : Optional[Any] = self.model.config else: _A : int = config _A : Optional[Any] = data_args _A : int = self.config.tgt_vocab_size if isinstance(self.config , _a ) else self.config.vocab_size if self.args.label_smoothing != 0 or (self.data_args is not None and self.data_args.ignore_pad_token_for_loss): assert self.config.pad_token_id is not None, ( "Make sure that `config.pad_token_id` is correcly defined when ignoring `pad_token` for loss" " calculation or doing label smoothing." ) if self.config.pad_token_id is None and self.config.eos_token_id is not None: logger.warning( F'''The `config.pad_token_id` is `None`. Using `config.eos_token_id` = {self.config.eos_token_id} for''' """ padding..""" ) if self.args.label_smoothing == 0: _A : Optional[Any] = torch.nn.CrossEntropyLoss(ignore_index=self.config.pad_token_id ) else: # dynamically import label_smoothed_nll_loss from utils import label_smoothed_nll_loss _A : Union[str, Any] = label_smoothed_nll_loss def a__ ( self , _a ) -> int: if self.optimizer is None: _A : List[str] = ["""bias""", """LayerNorm.weight"""] _A : str = [ { """params""": [p for n, p in self.model.named_parameters() if not any(nd in n for nd in no_decay )], """weight_decay""": self.args.weight_decay, }, { """params""": [p for n, p in self.model.named_parameters() if any(nd in n for nd in no_decay )], """weight_decay""": 0.0, }, ] _A : Optional[Any] = Adafactor if self.args.adafactor else AdamW if self.args.adafactor: _A : Dict = Adafactor _A : int = {"""scale_parameter""": False, """relative_step""": False} else: _A : int = AdamW _A : Any = { """betas""": (self.args.adam_betaa, self.args.adam_betaa), """eps""": self.args.adam_epsilon, } _A : List[str] = self.args.learning_rate if self.sharded_ddp: _A : List[str] = OSS( params=_a , optim=_a , **_a , ) else: _A : Tuple = optimizer_cls(_a , **_a ) if self.lr_scheduler is None: _A : Union[str, Any] = self._get_lr_scheduler(_a ) else: # ignoring --lr_scheduler logger.warning("""scheduler is passed to `Seq2SeqTrainer`, `--lr_scheduler` arg is ignored.""" ) def a__ ( self , _a ) -> Dict: _A : List[Any] = arg_to_scheduler[self.args.lr_scheduler] if self.args.lr_scheduler == "constant": _A : Optional[Any] = schedule_func(self.optimizer ) elif self.args.lr_scheduler == "constant_w_warmup": _A : Optional[int] = schedule_func(self.optimizer , num_warmup_steps=self.args.warmup_steps ) else: _A : List[Any] = schedule_func( self.optimizer , num_warmup_steps=self.args.warmup_steps , num_training_steps=_a ) return scheduler def a__ ( self ) -> Optional[torch.utils.data.Sampler]: if isinstance(self.train_dataset , torch.utils.data.IterableDataset ): return None elif is_torch_tpu_available(): return get_tpu_sampler(self.train_dataset ) else: if self.args.sortish_sampler: self.train_dataset.make_sortish_sampler( self.args.per_device_train_batch_size , distributed=(self.args.parallel_mode == ParallelMode.DISTRIBUTED) , ) return ( RandomSampler(self.train_dataset ) if self.args.local_rank == -1 else DistributedSampler(self.train_dataset ) ) def a__ ( self , _a , _a , _a ) -> List[str]: if self.args.label_smoothing == 0: if self.data_args is not None and self.data_args.ignore_pad_token_for_loss: # force training to ignore pad token _A : List[str] = model(**_a , use_cache=_a )[0] _A : Dict = self.loss_fn(logits.view(-1 , logits.shape[-1] ) , labels.view(-1 ) ) else: # compute usual loss via models _A , _A : str = model(**_a , labels=_a , use_cache=_a )[:2] else: # compute label smoothed loss _A : Any = model(**_a , use_cache=_a )[0] _A : Union[str, Any] = torch.nn.functional.log_softmax(_a , dim=-1 ) _A , _A : List[str] = self.loss_fn(_a , _a , self.args.label_smoothing , ignore_index=self.config.pad_token_id ) return loss, logits def a__ ( self , _a , _a ) -> List[Any]: _A : Optional[int] = inputs.pop("""labels""" ) _A , _A : Dict = self._compute_loss(_a , _a , _a ) return loss def a__ ( self , _a , _a , _a , _a = None , ) -> Tuple[Optional[float], Optional[torch.Tensor], Optional[torch.Tensor]]: _A : int = self._prepare_inputs(_a ) _A : Dict = { """max_length""": self.data_args.val_max_target_length if self.data_args is not None else self.config.max_length, """num_beams""": self.data_args.eval_beams if self.data_args is not None else self.config.num_beams, } if self.args.predict_with_generate and not self.args.prediction_loss_only: _A : List[str] = self.model.generate( inputs["""input_ids"""] , attention_mask=inputs["""attention_mask"""] , **_a , ) # in case the batch is shorter than max length, the output should be padded if generated_tokens.shape[-1] < gen_kwargs["max_length"]: _A : str = self._pad_tensors_to_max_len(_a , gen_kwargs["""max_length"""] ) _A : Any = inputs.pop("""labels""" ) with torch.no_grad(): # compute loss on predict data _A , _A : Tuple = self._compute_loss(_a , _a , _a ) _A : Any = loss.mean().detach() if self.args.prediction_loss_only: return (loss, None, None) _A : List[Any] = generated_tokens if self.args.predict_with_generate else logits if labels.shape[-1] < gen_kwargs["max_length"]: _A : Any = self._pad_tensors_to_max_len(_a , gen_kwargs["""max_length"""] ) return (loss, logits, labels) def a__ ( self , _a , _a ) -> Union[str, Any]: # If PAD token is not defined at least EOS token has to be defined _A : Optional[Any] = self.config.pad_token_id if self.config.pad_token_id is not None else self.config.eos_token_id if pad_token_id is None: raise ValueError( """Make sure that either `config.pad_token_id` or `config.eos_token_id` is defined if tensor has to be""" F''' padded to `max_length`={max_length}''' ) _A : Dict = pad_token_id * torch.ones( (tensor.shape[0], max_length) , dtype=tensor.dtype , device=tensor.device ) _A : Dict = tensor return padded_tensor
54
0
import copy import os from typing import Union from ...configuration_utils import PretrainedConfig from ...utils import logging _snake_case = logging.get_logger(__name__) _snake_case = { 'Salesforce/blip-vqa-base': 'https://huggingface.co/Salesforce/blip-vqa-base/resolve/main/config.json', 'Salesforce/blip-vqa-capfit-large': ( 'https://huggingface.co/Salesforce/blip-vqa-base-capfit/resolve/main/config.json' ), 'Salesforce/blip-image-captioning-base': ( 'https://huggingface.co/Salesforce/blip-image-captioning-base/resolve/main/config.json' ), 'Salesforce/blip-image-captioning-large': ( 'https://huggingface.co/Salesforce/blip-image-captioning-large/resolve/main/config.json' ), 'Salesforce/blip-itm-base-coco': 'https://huggingface.co/Salesforce/blip-itm-base-coco/resolve/main/config.json', 'Salesforce/blip-itm-large-coco': 'https://huggingface.co/Salesforce/blip-itm-large-coco/resolve/main/config.json', 'Salesforce/blip-itm-base-flikr': 'https://huggingface.co/Salesforce/blip-itm-base-flikr/resolve/main/config.json', 'Salesforce/blip-itm-large-flikr': ( 'https://huggingface.co/Salesforce/blip-itm-large-flikr/resolve/main/config.json' ), } class lowercase ( _A ): _a = "blip_text_model" def __init__( self , _a=3_0524 , _a=768 , _a=768 , _a=3072 , _a=768 , _a=12 , _a=8 , _a=512 , _a="gelu" , _a=1e-12 , _a=0.0 , _a=0.0 , _a=0.02 , _a=3_0522 , _a=2 , _a=0 , _a=102 , _a=True , _a=True , **_a , ) -> Tuple: super().__init__( pad_token_id=UpperCamelCase__ , bos_token_id=UpperCamelCase__ , eos_token_id=UpperCamelCase__ , sep_token_id=UpperCamelCase__ , **UpperCamelCase__ , ) _A : Any = vocab_size _A : Dict = hidden_size _A : Optional[int] = encoder_hidden_size _A : Optional[Any] = intermediate_size _A : Tuple = projection_dim _A : str = hidden_dropout_prob _A : List[Any] = num_hidden_layers _A : List[str] = num_attention_heads _A : Tuple = max_position_embeddings _A : List[str] = layer_norm_eps _A : Tuple = hidden_act _A : Optional[int] = initializer_range _A : Union[str, Any] = attention_probs_dropout_prob _A : Any = is_decoder _A : Dict = use_cache @classmethod def a__ ( cls , _a , **_a ) -> "PretrainedConfig": cls._set_token_in_kwargs(UpperCamelCase__ ) _A , _A : List[Any] = cls.get_config_dict(UpperCamelCase__ , **UpperCamelCase__ ) # get the text config dict if we are loading from BlipConfig if config_dict.get("""model_type""" ) == "blip": _A : Any = config_dict["""text_config"""] if "model_type" in config_dict and hasattr(cls , """model_type""" ) and config_dict["model_type"] != cls.model_type: logger.warning( F'''You are using a model of type {config_dict["model_type"]} to instantiate a model of type ''' F'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.''' ) return cls.from_dict(UpperCamelCase__ , **UpperCamelCase__ ) class lowercase ( _A ): _a = "blip_vision_model" def __init__( self , _a=768 , _a=3072 , _a=512 , _a=12 , _a=12 , _a=384 , _a=16 , _a="gelu" , _a=1e-5 , _a=0.0 , _a=1e-10 , **_a , ) -> List[str]: super().__init__(**UpperCamelCase__ ) _A : int = hidden_size _A : Optional[Any] = intermediate_size _A : Optional[Any] = projection_dim _A : Dict = num_hidden_layers _A : Any = num_attention_heads _A : str = patch_size _A : Union[str, Any] = image_size _A : Any = initializer_range _A : int = attention_dropout _A : str = layer_norm_eps _A : Dict = hidden_act @classmethod def a__ ( cls , _a , **_a ) -> "PretrainedConfig": cls._set_token_in_kwargs(UpperCamelCase__ ) _A , _A : Optional[int] = cls.get_config_dict(UpperCamelCase__ , **UpperCamelCase__ ) # get the vision config dict if we are loading from BlipConfig if config_dict.get("""model_type""" ) == "blip": _A : Tuple = config_dict["""vision_config"""] if "model_type" in config_dict and hasattr(cls , """model_type""" ) and config_dict["model_type"] != cls.model_type: logger.warning( F'''You are using a model of type {config_dict["model_type"]} to instantiate a model of type ''' F'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.''' ) return cls.from_dict(UpperCamelCase__ , **UpperCamelCase__ ) class lowercase ( _A ): _a = "blip" _a = True def __init__( self , _a=None , _a=None , _a=512 , _a=2.6592 , _a=256 , **_a , ) -> str: super().__init__(**UpperCamelCase__ ) if text_config is None: _A : Optional[Any] = {} logger.info("""`text_config` is `None`. Initializing the `BlipTextConfig` with default values.""" ) if vision_config is None: _A : Dict = {} logger.info("""`vision_config` is `None`. Initializing the `BlipVisionConfig` with default values.""" ) _A : str = BlipTextConfig(**UpperCamelCase__ ) _A : Optional[Any] = BlipVisionConfig(**UpperCamelCase__ ) _A : Union[str, Any] = self.vision_config.hidden_size _A : Dict = projection_dim _A : str = logit_scale_init_value _A : Optional[int] = 1.0 _A : List[Any] = 0.02 _A : List[str] = image_text_hidden_size @classmethod def a__ ( cls , _a , _a , **_a ) -> List[Any]: return cls(text_config=text_config.to_dict() , vision_config=vision_config.to_dict() , **UpperCamelCase__ ) def a__ ( self ) -> List[str]: _A : List[Any] = copy.deepcopy(self.__dict__ ) _A : str = self.text_config.to_dict() _A : Union[str, Any] = self.vision_config.to_dict() _A : Dict = self.__class__.model_type return output
709
from collections.abc import Callable from math import pi, sqrt from random import uniform from statistics import mean def lowerCAmelCase_ ( snake_case_ ): # A local function to see if a dot lands in the circle. def is_in_circle(snake_case_,snake_case_ ) -> bool: _A : List[str] = sqrt((x**2) + (y**2) ) # Our circle has a radius of 1, so a distance # greater than 1 would land outside the circle. return distance_from_centre <= 1 # The proportion of guesses that landed in the circle _A : Optional[int] = mean( int(is_in_circle(uniform(-1.0,1.0 ),uniform(-1.0,1.0 ) ) ) for _ in range(snake_case_ ) ) # The ratio of the area for circle to square is pi/4. _A : List[str] = proportion * 4 print(f'''The estimated value of pi is {pi_estimate}''' ) print(f'''The numpy value of pi is {pi}''' ) print(f'''The total error is {abs(pi - pi_estimate )}''' ) def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_ = 0.0,snake_case_ = 1.0,): return mean( function_to_integrate(uniform(snake_case_,snake_case_ ) ) for _ in range(snake_case_ ) ) * (max_value - min_value) def lowerCAmelCase_ ( snake_case_,snake_case_ = 0.0,snake_case_ = 1.0 ): def identity_function(snake_case_ ) -> float: return x _A : Any = area_under_curve_estimator( snake_case_,snake_case_,snake_case_,snake_case_ ) _A : Tuple = (max_value * max_value - min_value * min_value) / 2 print("""******************""" ) print(f'''Estimating area under y=x where x varies from {min_value} to {max_value}''' ) print(f'''Estimated value is {estimated_value}''' ) print(f'''Expected value is {expected_value}''' ) print(f'''Total error is {abs(estimated_value - expected_value )}''' ) print("""******************""" ) def lowerCAmelCase_ ( snake_case_ ): def function_to_integrate(snake_case_ ) -> float: return sqrt(4.0 - x * x ) _A : Optional[int] = area_under_curve_estimator( snake_case_,snake_case_,0.0,2.0 ) print("""******************""" ) print("""Estimating pi using area_under_curve_estimator""" ) print(f'''Estimated value is {estimated_value}''' ) print(f'''Expected value is {pi}''' ) print(f'''Total error is {abs(estimated_value - pi )}''' ) print("""******************""" ) if __name__ == "__main__": import doctest doctest.testmod()
54
0
def lowerCAmelCase_ ( snake_case_ ): _A : Union[str, Any] = len(snake_case_ ) for _ in range(snake_case_ ): for i in range(_ % 2,arr_size - 1,2 ): if arr[i + 1] < arr[i]: _A : Tuple = arr[i + 1], arr[i] return arr if __name__ == "__main__": _snake_case = list(range(10, 0, -1)) print(f"""Original: {arr}. Sorted: {odd_even_transposition(arr)}""")
710
import json import os import shutil import tempfile from unittest import TestCase from transformers import BartTokenizer, BartTokenizerFast, DPRQuestionEncoderTokenizer, DPRQuestionEncoderTokenizerFast from transformers.models.bart.configuration_bart import BartConfig from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES as DPR_VOCAB_FILES_NAMES from transformers.models.dpr.configuration_dpr import DPRConfig from transformers.models.roberta.tokenization_roberta import VOCAB_FILES_NAMES as BART_VOCAB_FILES_NAMES from transformers.testing_utils import require_faiss, require_tokenizers, require_torch, slow from transformers.utils import is_datasets_available, is_faiss_available, is_torch_available if is_torch_available() and is_datasets_available() and is_faiss_available(): from transformers.models.rag.configuration_rag import RagConfig from transformers.models.rag.tokenization_rag import RagTokenizer @require_faiss @require_torch class lowercase ( UpperCamelCase__ ): def a__ ( self ) -> int: _A : int = tempfile.mkdtemp() _A : Union[str, Any] = 8 # DPR tok _A : List[str] = [ """[UNK]""", """[CLS]""", """[SEP]""", """[PAD]""", """[MASK]""", """want""", """##want""", """##ed""", """wa""", """un""", """runn""", """##ing""", """,""", """low""", """lowest""", ] _A : List[str] = os.path.join(self.tmpdirname , """dpr_tokenizer""" ) os.makedirs(_a , exist_ok=_a ) _A : str = os.path.join(_a , DPR_VOCAB_FILES_NAMES["""vocab_file"""] ) with open(self.vocab_file , """w""" , encoding="""utf-8""" ) as vocab_writer: vocab_writer.write("""""".join([x + """\n""" for x in vocab_tokens] ) ) # BART tok _A : Dict = [ """l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """\u0120""", """\u0120l""", """\u0120n""", """\u0120lo""", """\u0120low""", """er""", """\u0120lowest""", """\u0120newer""", """\u0120wider""", """<unk>""", ] _A : Optional[Any] = dict(zip(_a , range(len(_a ) ) ) ) _A : Tuple = ["""#version: 0.2""", """\u0120 l""", """\u0120l o""", """\u0120lo w""", """e r""", """"""] _A : Dict = {"""unk_token""": """<unk>"""} _A : Optional[Any] = os.path.join(self.tmpdirname , """bart_tokenizer""" ) os.makedirs(_a , exist_ok=_a ) _A : str = os.path.join(_a , BART_VOCAB_FILES_NAMES["""vocab_file"""] ) _A : List[Any] = os.path.join(_a , BART_VOCAB_FILES_NAMES["""merges_file"""] ) with open(self.vocab_file , """w""" , encoding="""utf-8""" ) as fp: fp.write(json.dumps(_a ) + """\n""" ) with open(self.merges_file , """w""" , encoding="""utf-8""" ) as fp: fp.write("""\n""".join(_a ) ) def a__ ( self ) -> DPRQuestionEncoderTokenizer: return DPRQuestionEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname , """dpr_tokenizer""" ) ) def a__ ( self ) -> BartTokenizer: return BartTokenizer.from_pretrained(os.path.join(self.tmpdirname , """bart_tokenizer""" ) ) def a__ ( self ) -> Optional[Any]: shutil.rmtree(self.tmpdirname ) @require_tokenizers def a__ ( self ) -> str: _A : Optional[Any] = os.path.join(self.tmpdirname , """rag_tokenizer""" ) _A : int = RagConfig(question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() ) _A : Any = RagTokenizer(question_encoder=self.get_dpr_tokenizer() , generator=self.get_bart_tokenizer() ) rag_config.save_pretrained(_a ) rag_tokenizer.save_pretrained(_a ) _A : Optional[Any] = RagTokenizer.from_pretrained(_a , config=_a ) self.assertIsInstance(new_rag_tokenizer.question_encoder , _a ) self.assertEqual(new_rag_tokenizer.question_encoder.get_vocab() , rag_tokenizer.question_encoder.get_vocab() ) self.assertIsInstance(new_rag_tokenizer.generator , _a ) self.assertEqual(new_rag_tokenizer.generator.get_vocab() , rag_tokenizer.generator.get_vocab() ) @slow def a__ ( self ) -> str: _A : Union[str, Any] = RagTokenizer.from_pretrained("""facebook/rag-token-nq""" ) _A : Tuple = [ """who got the first nobel prize in physics""", """when is the next deadpool movie being released""", """which mode is used for short wave broadcast service""", """who is the owner of reading football club""", """when is the next scandal episode coming out""", """when is the last time the philadelphia won the superbowl""", """what is the most current adobe flash player version""", """how many episodes are there in dragon ball z""", """what is the first step in the evolution of the eye""", """where is gall bladder situated in human body""", """what is the main mineral in lithium batteries""", """who is the president of usa right now""", """where do the greasers live in the outsiders""", """panda is a national animal of which country""", """what is the name of manchester united stadium""", ] _A : Tuple = tokenizer(_a ) self.assertIsNotNone(_a ) @slow def a__ ( self ) -> Dict: _A : Dict = RagTokenizer.from_pretrained("""facebook/rag-sequence-nq""" ) _A : str = [ """who got the first nobel prize in physics""", """when is the next deadpool movie being released""", """which mode is used for short wave broadcast service""", """who is the owner of reading football club""", """when is the next scandal episode coming out""", """when is the last time the philadelphia won the superbowl""", """what is the most current adobe flash player version""", """how many episodes are there in dragon ball z""", """what is the first step in the evolution of the eye""", """where is gall bladder situated in human body""", """what is the main mineral in lithium batteries""", """who is the president of usa right now""", """where do the greasers live in the outsiders""", """panda is a national animal of which country""", """what is the name of manchester united stadium""", ] _A : Optional[Any] = tokenizer(_a ) self.assertIsNotNone(_a )
54
0
import gc import random import unittest import numpy as np import torch from transformers import XLMRobertaTokenizer from diffusers import ( AltDiffusionImgaImgPipeline, AutoencoderKL, PNDMScheduler, UNetaDConditionModel, ) from diffusers.image_processor import VaeImageProcessor from diffusers.pipelines.alt_diffusion.modeling_roberta_series import ( RobertaSeriesConfig, RobertaSeriesModelWithTransformation, ) 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 enable_full_determinism() class lowercase ( unittest.TestCase ): def a__ ( self ) -> List[Any]: super().tearDown() gc.collect() torch.cuda.empty_cache() @property def a__ ( self ) -> Dict: _A : Tuple = 1 _A : List[Any] = 3 _A : int = (32, 32) _A : Any = floats_tensor((batch_size, num_channels) + sizes , rng=random.Random(0 ) ).to(__a ) return image @property def a__ ( self ) -> str: torch.manual_seed(0 ) _A : int = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""") , up_block_types=("""CrossAttnUpBlock2D""", """UpBlock2D""") , cross_attention_dim=32 , ) return model @property def a__ ( self ) -> Optional[Any]: torch.manual_seed(0 ) _A : Optional[Any] = 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 , ) return model @property def a__ ( self ) -> str: torch.manual_seed(0 ) _A : List[Any] = RobertaSeriesConfig( hidden_size=32 , project_dim=32 , intermediate_size=37 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=5006 , ) return RobertaSeriesModelWithTransformation(__a ) @property def a__ ( self ) -> str: def extract(*_a , **_a ): class lowercase : def __init__( self ) -> List[Any]: _A : Union[str, Any] = torch.ones([0] ) def a__ ( self , _a ) -> Optional[Any]: self.pixel_values.to(__a ) return self return Out() return extract def a__ ( self ) -> Optional[int]: _A : Union[str, Any] = """cpu""" # ensure determinism for the device-dependent torch.Generator _A : Optional[Any] = self.dummy_cond_unet _A : List[str] = PNDMScheduler(skip_prk_steps=__a ) _A : List[str] = self.dummy_vae _A : Any = self.dummy_text_encoder _A : Union[str, Any] = XLMRobertaTokenizer.from_pretrained("""hf-internal-testing/tiny-xlm-roberta""" ) _A : List[Any] = 77 _A : Dict = self.dummy_image.to(__a ) _A : List[str] = init_image / 2 + 0.5 # make sure here that pndm scheduler skips prk _A : str = AltDiffusionImgaImgPipeline( unet=__a , scheduler=__a , vae=__a , text_encoder=__a , tokenizer=__a , safety_checker=__a , feature_extractor=self.dummy_extractor , ) _A : int = VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor , do_normalize=__a ) _A : Any = alt_pipe.to(__a ) alt_pipe.set_progress_bar_config(disable=__a ) _A : Dict = """A painting of a squirrel eating a burger""" _A : List[Any] = torch.Generator(device=__a ).manual_seed(0 ) _A : Optional[Any] = alt_pipe( [prompt] , generator=__a , guidance_scale=6.0 , num_inference_steps=2 , output_type="""np""" , image=__a , ) _A : Optional[int] = output.images _A : Optional[Any] = torch.Generator(device=__a ).manual_seed(0 ) _A : Optional[int] = alt_pipe( [prompt] , generator=__a , guidance_scale=6.0 , num_inference_steps=2 , output_type="""np""" , image=__a , return_dict=__a , )[0] _A : Tuple = image[0, -3:, -3:, -1] _A : str = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) _A : Dict = np.array([0.4427, 0.3731, 0.4249, 0.4941, 0.4546, 0.4148, 0.4193, 0.4666, 0.4499] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5e-3 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 5e-3 @unittest.skipIf(torch_device != """cuda""" , """This test requires a GPU""" ) def a__ ( self ) -> str: _A : List[str] = self.dummy_cond_unet _A : List[str] = PNDMScheduler(skip_prk_steps=__a ) _A : Optional[int] = self.dummy_vae _A : Tuple = self.dummy_text_encoder _A : Tuple = XLMRobertaTokenizer.from_pretrained("""hf-internal-testing/tiny-xlm-roberta""" ) _A : Any = 77 _A : List[str] = self.dummy_image.to(__a ) # put models in fp16 _A : Union[str, Any] = unet.half() _A : Any = vae.half() _A : Optional[Any] = bert.half() # make sure here that pndm scheduler skips prk _A : Union[str, Any] = AltDiffusionImgaImgPipeline( unet=__a , scheduler=__a , vae=__a , text_encoder=__a , tokenizer=__a , safety_checker=__a , feature_extractor=self.dummy_extractor , ) _A : Optional[Any] = VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor , do_normalize=__a ) _A : List[Any] = alt_pipe.to(__a ) alt_pipe.set_progress_bar_config(disable=__a ) _A : Optional[int] = """A painting of a squirrel eating a burger""" _A : int = torch.manual_seed(0 ) _A : str = alt_pipe( [prompt] , generator=__a , num_inference_steps=2 , output_type="""np""" , image=__a , ).images assert image.shape == (1, 32, 32, 3) @unittest.skipIf(torch_device != """cuda""" , """This test requires a GPU""" ) def a__ ( self ) -> int: _A : Dict = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/img2img/sketch-mountains-input.jpg""" ) # resize to resolution that is divisible by 8 but not 16 or 32 _A : Tuple = init_image.resize((760, 504) ) _A : Optional[Any] = """BAAI/AltDiffusion""" _A : str = AltDiffusionImgaImgPipeline.from_pretrained( __a , safety_checker=__a , ) pipe.to(__a ) pipe.set_progress_bar_config(disable=__a ) pipe.enable_attention_slicing() _A : Optional[int] = """A fantasy landscape, trending on artstation""" _A : Optional[Any] = torch.manual_seed(0 ) _A : Optional[Any] = pipe( prompt=__a , image=__a , strength=0.75 , guidance_scale=7.5 , generator=__a , output_type="""np""" , ) _A : Tuple = output.images[0] _A : Any = image[255:258, 383:386, -1] assert image.shape == (504, 760, 3) _A : Tuple = np.array([0.9358, 0.9397, 0.9599, 0.9901, 1.0000, 1.0000, 0.9882, 1.0000, 1.0000] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 @slow @require_torch_gpu class lowercase ( unittest.TestCase ): def a__ ( self ) -> Optional[Any]: super().tearDown() gc.collect() torch.cuda.empty_cache() def a__ ( self ) -> List[str]: _A : str = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/img2img/sketch-mountains-input.jpg""" ) _A : List[str] = init_image.resize((768, 512) ) _A : int = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/img2img/fantasy_landscape_alt.npy""" ) _A : Any = """BAAI/AltDiffusion""" _A : List[Any] = AltDiffusionImgaImgPipeline.from_pretrained( __a , safety_checker=__a , ) pipe.to(__a ) pipe.set_progress_bar_config(disable=__a ) pipe.enable_attention_slicing() _A : Any = """A fantasy landscape, trending on artstation""" _A : str = torch.manual_seed(0 ) _A : List[str] = pipe( prompt=__a , image=__a , strength=0.75 , guidance_scale=7.5 , generator=__a , output_type="""np""" , ) _A : List[str] = output.images[0] assert image.shape == (512, 768, 3) # img2img is flaky across GPUs even in fp32, so using MAE here assert np.abs(expected_image - image ).max() < 1e-2
711
import os import re import shutil import sys import tempfile import unittest import black _snake_case = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) sys.path.append(os.path.join(git_repo_path, "utils")) import check_copies # noqa: E402 # This is the reference code that will be used in the tests. # If BertLMPredictionHead is changed in modeling_bert.py, this code needs to be manually updated. _snake_case = " def __init__(self, config):\n super().__init__()\n self.transform = BertPredictionHeadTransform(config)\n\n # The output weights are the same as the input embeddings, but there is\n # an output-only bias for each token.\n self.decoder = nn.Linear(config.hidden_size, config.vocab_size, bias=False)\n\n self.bias = nn.Parameter(torch.zeros(config.vocab_size))\n\n # Need a link between the two variables so that the bias is correctly resized with `resize_token_embeddings`\n self.decoder.bias = self.bias\n\n def forward(self, hidden_states):\n hidden_states = self.transform(hidden_states)\n hidden_states = self.decoder(hidden_states)\n return hidden_states\n" class lowercase ( unittest.TestCase ): def a__ ( self ) -> Union[str, Any]: _A : List[str] = tempfile.mkdtemp() os.makedirs(os.path.join(self.transformer_dir , """models/bert/""" ) ) _A : str = self.transformer_dir shutil.copy( os.path.join(_a , """src/transformers/models/bert/modeling_bert.py""" ) , os.path.join(self.transformer_dir , """models/bert/modeling_bert.py""" ) , ) def a__ ( self ) -> Optional[int]: _A : List[str] = """src/transformers""" shutil.rmtree(self.transformer_dir ) def a__ ( self , _a , _a , _a , _a=None ) -> Optional[Any]: _A : Optional[Any] = comment + F'''\nclass {class_name}(nn.Module):\n''' + class_code if overwrite_result is not None: _A : List[str] = comment + F'''\nclass {class_name}(nn.Module):\n''' + overwrite_result _A : List[str] = black.Mode(target_versions={black.TargetVersion.PYaa} , line_length=119 ) _A : Optional[int] = black.format_str(_a , mode=_a ) _A : Optional[Any] = os.path.join(self.transformer_dir , """new_code.py""" ) with open(_a , """w""" , newline="""\n""" ) as f: f.write(_a ) if overwrite_result is None: self.assertTrue(len(check_copies.is_copy_consistent(_a ) ) == 0 ) else: check_copies.is_copy_consistent(f.name , overwrite=_a ) with open(_a , """r""" ) as f: self.assertTrue(f.read() , _a ) def a__ ( self ) -> str: _A : Union[str, Any] = check_copies.find_code_in_transformers("""models.bert.modeling_bert.BertLMPredictionHead""" ) self.assertEqual(_a , _a ) def a__ ( self ) -> int: # Base copy consistency self.check_copy_consistency( """# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead""" , """BertLMPredictionHead""" , REFERENCE_CODE + """\n""" , ) # With no empty line at the end self.check_copy_consistency( """# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead""" , """BertLMPredictionHead""" , _a , ) # Copy consistency with rename self.check_copy_consistency( """# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->TestModel""" , """TestModelLMPredictionHead""" , re.sub("""Bert""" , """TestModel""" , _a ) , ) # Copy consistency with a really long name _A : List[str] = """TestModelWithAReallyLongNameBecauseSomePeopleLikeThatForSomeReason""" self.check_copy_consistency( F'''# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->{long_class_name}''' , F'''{long_class_name}LMPredictionHead''' , re.sub("""Bert""" , _a , _a ) , ) # Copy consistency with overwrite self.check_copy_consistency( """# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->TestModel""" , """TestModelLMPredictionHead""" , _a , overwrite_result=re.sub("""Bert""" , """TestModel""" , _a ) , ) def a__ ( self ) -> Tuple: _A : Union[str, Any] = check_copies.LOCALIZED_READMES["""README_zh-hans.md"""] _A : str = ( """1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (from Google Research and the""" """ Toyota Technological Institute at Chicago) released with the paper [ALBERT: A Lite BERT for""" """ Self-supervised Learning of Language Representations](https://arxiv.org/abs/1909.11942), by Zhenzhong""" """ Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut.\n1.""" """ **[DistilBERT](https://huggingface.co/transformers/model_doc/distilbert.html)** (from HuggingFace),""" """ released together with the paper [DistilBERT, a distilled version of BERT: smaller, faster, cheaper and""" """ lighter](https://arxiv.org/abs/1910.01108) by Victor Sanh, Lysandre Debut and Thomas Wolf. The same""" """ method has been applied to compress GPT2 into""" """ [DistilGPT2](https://github.com/huggingface/transformers/tree/main/examples/distillation), RoBERTa into""" """ [DistilRoBERTa](https://github.com/huggingface/transformers/tree/main/examples/distillation),""" """ Multilingual BERT into""" """ [DistilmBERT](https://github.com/huggingface/transformers/tree/main/examples/distillation) and a German""" """ version of DistilBERT.\n1. **[ELECTRA](https://huggingface.co/transformers/model_doc/electra.html)**""" """ (from Google Research/Stanford University) released with the paper [ELECTRA: Pre-training text encoders""" """ as discriminators rather than generators](https://arxiv.org/abs/2003.10555) by Kevin Clark, Minh-Thang""" """ Luong, Quoc V. Le, Christopher D. Manning.""" ) _A : str = ( """1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the""" """ Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of""" """ Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian""" """ Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n""" ) _A : Any = ( """1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the""" """ Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of""" """ Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian""" """ Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n1.""" """ **[DistilBERT](https://huggingface.co/transformers/model_doc/distilbert.html)** (来自 HuggingFace) 伴随论文""" """ [DistilBERT, a distilled version of BERT: smaller, faster, cheaper and""" """ lighter](https://arxiv.org/abs/1910.01108) 由 Victor Sanh, Lysandre Debut and Thomas Wolf 发布。 The same""" """ method has been applied to compress GPT2 into""" """ [DistilGPT2](https://github.com/huggingface/transformers/tree/main/examples/distillation), RoBERTa into""" """ [DistilRoBERTa](https://github.com/huggingface/transformers/tree/main/examples/distillation),""" """ Multilingual BERT into""" """ [DistilmBERT](https://github.com/huggingface/transformers/tree/main/examples/distillation) and a German""" """ version of DistilBERT.\n1. **[ELECTRA](https://huggingface.co/transformers/model_doc/electra.html)** (来自""" """ Google Research/Stanford University) 伴随论文 [ELECTRA: Pre-training text encoders as discriminators rather""" """ than generators](https://arxiv.org/abs/2003.10555) 由 Kevin Clark, Minh-Thang Luong, Quoc V. Le,""" """ Christopher D. Manning 发布。\n""" ) _A , _A : Tuple = check_copies.convert_to_localized_md( _a , _a , localized_readme["""format_model_list"""] ) self.assertFalse(_a ) self.assertEqual(_a , _a ) _A , _A : List[str] = check_copies.convert_to_localized_md( _a , _a , localized_readme["""format_model_list"""] ) # Check whether the number of models is equal to README.md after conversion. self.assertTrue(_a ) _A : Tuple = ( """1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (from Google Research and the""" """ Toyota Technological Institute at Chicago) released with the paper [ALBERT: A Lite BERT for""" """ Self-supervised Learning of Language Representations](https://arxiv.org/abs/1909.11942), by Zhenzhong""" """ Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut.""" ) _A : Dict = ( """1. **[ALBERT](https://huggingface.co/transformers/main/model_doc/albert.html)** (来自 Google Research and""" """ the Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of""" """ Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian""" """ Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n""" ) _A : Optional[Any] = ( """1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the""" """ Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of""" """ Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian""" """ Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n""" ) _A , _A : Optional[int] = check_copies.convert_to_localized_md( _a , _a , localized_readme["""format_model_list"""] ) # Check if the model link is synchronized. self.assertEqual(_a , _a )
54
0
from ...utils import logging from ..ta.modeling_tf_ta import TFTaEncoderModel, TFTaForConditionalGeneration, TFTaModel from .configuration_mta import MTaConfig _snake_case = logging.get_logger(__name__) _snake_case = "T5Config" class lowercase ( lowercase_ ): _a = "mt5" _a = MTaConfig class lowercase ( lowercase_ ): _a = "mt5" _a = MTaConfig class lowercase ( lowercase_ ): _a = "mt5" _a = MTaConfig
712
import itertools import os import random import tempfile import unittest import numpy as np from transformers import TvltFeatureExtractor, is_datasets_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_torch_available(): import torch if is_datasets_available(): from datasets import load_dataset _snake_case = random.Random() def lowerCAmelCase_ ( snake_case_,snake_case_=1.0,snake_case_=None,snake_case_=None ): if rng is None: _A : str = global_rng _A : List[Any] = [] for batch_idx in range(shape[0] ): values.append([] ) for _ in range(shape[1] ): values[-1].append(rng.random() * scale ) return values class lowercase ( unittest.TestCase ): def __init__( self , _a , _a=7 , _a=400 , _a=2000 , _a=2048 , _a=128 , _a=1 , _a=512 , _a=30 , _a=4_4100 , ) -> Tuple: _A : Any = parent _A : str = batch_size _A : Union[str, Any] = min_seq_length _A : int = max_seq_length _A : List[str] = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) _A : Tuple = spectrogram_length _A : int = feature_size _A : str = num_audio_channels _A : Tuple = hop_length _A : List[str] = chunk_length _A : Union[str, Any] = sampling_rate def a__ ( self ) -> Tuple: return { "spectrogram_length": self.spectrogram_length, "feature_size": self.feature_size, "num_audio_channels": self.num_audio_channels, "hop_length": self.hop_length, "chunk_length": self.chunk_length, "sampling_rate": self.sampling_rate, } def a__ ( self , _a=False , _a=False ) -> Optional[int]: def _flatten(_a ): return list(itertools.chain(*_a ) ) if equal_length: _A : List[str] = [floats_list((self.max_seq_length, self.feature_size) ) for _ in range(self.batch_size )] else: # make sure that inputs increase in size _A : Union[str, 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 : List[Any] = [np.asarray(_a ) for x in speech_inputs] return speech_inputs @require_torch @require_torchaudio class lowercase ( UpperCamelCase__,unittest.TestCase ): _a = TvltFeatureExtractor def a__ ( self ) -> Any: _A : int = TvltFeatureExtractionTester(self ) def a__ ( self ) -> List[Any]: _A : Tuple = self.feature_extraction_class(**self.feat_extract_dict ) self.assertTrue(hasattr(_a , """spectrogram_length""" ) ) self.assertTrue(hasattr(_a , """feature_size""" ) ) self.assertTrue(hasattr(_a , """num_audio_channels""" ) ) self.assertTrue(hasattr(_a , """hop_length""" ) ) self.assertTrue(hasattr(_a , """chunk_length""" ) ) self.assertTrue(hasattr(_a , """sampling_rate""" ) ) def a__ ( self ) -> Optional[int]: _A : str = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: _A : Tuple = feat_extract_first.save_pretrained(_a )[0] check_json_file_has_correct_format(_a ) _A : Optional[int] = self.feature_extraction_class.from_pretrained(_a ) _A : Optional[Any] = feat_extract_first.to_dict() _A : int = feat_extract_second.to_dict() _A : int = dict_first.pop("""mel_filters""" ) _A : Optional[int] = dict_second.pop("""mel_filters""" ) self.assertTrue(np.allclose(_a , _a ) ) self.assertEqual(_a , _a ) def a__ ( self ) -> int: _A : str = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: _A : List[str] = os.path.join(_a , """feat_extract.json""" ) feat_extract_first.to_json_file(_a ) _A : Union[str, Any] = self.feature_extraction_class.from_json_file(_a ) _A : Optional[Any] = feat_extract_first.to_dict() _A : Union[str, Any] = feat_extract_second.to_dict() _A : List[Any] = dict_first.pop("""mel_filters""" ) _A : Optional[Any] = dict_second.pop("""mel_filters""" ) self.assertTrue(np.allclose(_a , _a ) ) self.assertEqual(_a , _a ) def a__ ( self ) -> Optional[Any]: # Initialize feature_extractor _A : Tuple = self.feature_extraction_class(**self.feat_extract_dict ) # create three inputs of length 800, 1000, and 1200 _A : Optional[int] = [floats_list((1, x) )[0] for x in range(800 , 1400 , 200 )] _A : List[str] = [np.asarray(_a ) for speech_input in speech_inputs] # Test not batched input _A : Optional[int] = feature_extractor(np_speech_inputs[0] , return_tensors="""np""" , sampling_rate=4_4100 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test batched _A : str = feature_extractor(_a , return_tensors="""np""" , sampling_rate=4_4100 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test audio masking _A : Optional[Any] = feature_extractor( _a , return_tensors="""np""" , sampling_rate=4_4100 , mask_audio=_a ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test 2-D numpy arrays are batched. _A : Dict = [floats_list((1, x) )[0] for x in (800, 800, 800)] _A : Optional[int] = np.asarray(_a ) _A : Optional[Any] = feature_extractor(_a , return_tensors="""np""" , sampling_rate=4_4100 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) def a__ ( self , _a ) -> str: _A : Optional[Any] = load_dataset("""hf-internal-testing/librispeech_asr_dummy""" , """clean""" , split="""validation""" ) # automatic decoding with librispeech _A : Dict = ds.sort("""id""" ).select(range(_a ) )[:num_samples]["""audio"""] return [x["array"] for x in speech_samples] def a__ ( self ) -> Optional[Any]: _A : List[str] = self._load_datasamples(1 ) _A : List[str] = TvltFeatureExtractor() _A : List[Any] = feature_extractor(_a , return_tensors="""pt""" ).audio_values self.assertEquals(audio_values.shape , (1, 1, 192, 128) ) _A : int = torch.tensor([[-0.3032, -0.2708], [-0.4434, -0.4007]] ) self.assertTrue(torch.allclose(audio_values[0, 0, :2, :2] , _a , atol=1e-4 ) )
54
0
import argparse import os import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType ######################################################################## # This is a fully working simple example to use Accelerate, # specifically showcasing how to properly calculate the metrics on the # validation dataset when in a distributed system, and builds off the # `nlp_example.py` script. # # This example trains a Bert base model on GLUE MRPC # in any of the following settings (with the same script): # - single CPU or single GPU # - multi GPUS (using PyTorch distributed mode) # - (multi) TPUs # - fp16 (mixed-precision) or fp32 (normal precision) # # To help focus on the differences in the code, building `DataLoaders` # was refactored into its own function. # New additions from the base script can be found quickly by # looking for the # New Code # tags # # To run it in each of these various modes, follow the instructions # in the readme for examples: # https://github.com/huggingface/accelerate/tree/main/examples # ######################################################################## _snake_case = 16 _snake_case = 32 def lowerCAmelCase_ ( snake_case_,snake_case_ = 16 ): '''simple docstring''' _A : List[Any] = AutoTokenizer.from_pretrained("""bert-base-cased""" ) _A : Optional[int] = load_dataset("""glue""","""mrpc""" ) def tokenize_function(snake_case_ ): # max_length=None => use the model max length (it's actually the default) _A : List[Any] = tokenizer(examples["""sentence1"""],examples["""sentence2"""],truncation=A__,max_length=A__ ) return outputs # Apply the method we just defined to all the examples in all the splits of the dataset # starting with the main process first: with accelerator.main_process_first(): _A : int = datasets.map( A__,batched=A__,remove_columns=["""idx""", """sentence1""", """sentence2"""],) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library _A : int = tokenized_datasets.rename_column("""label""","""labels""" ) def collate_fn(snake_case_ ): # On TPU it's best to pad everything to the same length or training will be very slow. _A : str = 128 if accelerator.distributed_type == DistributedType.TPU else None # When using mixed precision we want round multiples of 8/16 if accelerator.mixed_precision == "fp8": _A : Union[str, Any] = 16 elif accelerator.mixed_precision != "no": _A : List[str] = 8 else: _A : Union[str, Any] = None return tokenizer.pad( A__,padding="""longest""",max_length=A__,pad_to_multiple_of=A__,return_tensors="""pt""",) # Instantiate dataloaders. _A : Dict = DataLoader( tokenized_datasets["""train"""],shuffle=A__,collate_fn=A__,batch_size=A__ ) _A : Any = DataLoader( tokenized_datasets["""validation"""],shuffle=A__,collate_fn=A__,batch_size=A__ ) return train_dataloader, eval_dataloader # For testing only if os.environ.get("TESTING_MOCKED_DATALOADERS", None) == "1": from accelerate.test_utils.training import mocked_dataloaders _snake_case = mocked_dataloaders # noqa: F811 def lowerCAmelCase_ ( snake_case_,snake_case_ ): '''simple docstring''' if os.environ.get("""TESTING_MOCKED_DATALOADERS""",A__ ) == "1": _A : Optional[Any] = 2 # Initialize accelerator _A : List[Any] = Accelerator(cpu=args.cpu,mixed_precision=args.mixed_precision ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs _A : List[Any] = config["""lr"""] _A : Optional[Any] = int(config["""num_epochs"""] ) _A : str = int(config["""seed"""] ) _A : str = int(config["""batch_size"""] ) _A : Optional[int] = evaluate.load("""glue""","""mrpc""" ) # If the batch size is too big we use gradient accumulation _A : str = 1 if batch_size > MAX_GPU_BATCH_SIZE and accelerator.distributed_type != DistributedType.TPU: _A : Tuple = batch_size // MAX_GPU_BATCH_SIZE _A : Dict = MAX_GPU_BATCH_SIZE set_seed(A__ ) _A , _A : List[Any] = get_dataloaders(A__,A__ ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) _A : Optional[int] = AutoModelForSequenceClassification.from_pretrained("""bert-base-cased""",return_dict=A__ ) # 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). _A : Tuple = model.to(accelerator.device ) # Instantiate optimizer _A : Tuple = AdamW(params=model.parameters(),lr=A__ ) # Instantiate scheduler _A : Optional[Any] = get_linear_schedule_with_warmup( optimizer=A__,num_warmup_steps=100,num_training_steps=(len(A__ ) * num_epochs) // gradient_accumulation_steps,) # 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. _A , _A , _A , _A , _A : Optional[int] = accelerator.prepare( A__,A__,A__,A__,A__ ) # Now we train the model for epoch in range(A__ ): model.train() for step, batch in enumerate(A__ ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) _A : int = model(**A__ ) _A : List[str] = outputs.loss _A : Tuple = loss / gradient_accumulation_steps accelerator.backward(A__ ) if step % gradient_accumulation_steps == 0: optimizer.step() lr_scheduler.step() optimizer.zero_grad() model.eval() _A : Union[str, Any] = 0 for step, batch in enumerate(A__ ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): _A : Optional[int] = model(**A__ ) _A : Any = outputs.logits.argmax(dim=-1 ) _A , _A : Optional[int] = accelerator.gather((predictions, batch["""labels"""]) ) # New Code # # First we check if it's a distributed system if accelerator.use_distributed: # Then see if we're on the last batch of our eval dataloader if step == len(A__ ) - 1: # Last batch needs to be truncated on distributed systems as it contains additional samples _A : List[Any] = predictions[: len(eval_dataloader.dataset ) - samples_seen] _A : List[str] = references[: len(eval_dataloader.dataset ) - samples_seen] else: # Otherwise we add the number of samples seen samples_seen += references.shape[0] # All of this can be avoided if you use `Accelerator.gather_for_metrics` instead of `Accelerator.gather`: # accelerator.gather_for_metrics((predictions, batch["labels"])) metric.add_batch( predictions=A__,references=A__,) _A : int = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(f'''epoch {epoch}:''',A__ ) def lowerCAmelCase_ ( ): '''simple docstring''' _A : str = argparse.ArgumentParser(description="""Simple example of training script.""" ) parser.add_argument( """--mixed_precision""",type=A__,default=A__,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.""" ) _A : List[Any] = parser.parse_args() _A : Union[str, Any] = {"""lr""": 2e-5, """num_epochs""": 3, """seed""": 42, """batch_size""": 16} training_function(A__,A__ ) if __name__ == "__main__": main()
713
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available _snake_case = { "configuration_time_series_transformer": [ "TIME_SERIES_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", "TimeSeriesTransformerConfig", ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = [ "TIME_SERIES_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "TimeSeriesTransformerForPrediction", "TimeSeriesTransformerModel", "TimeSeriesTransformerPreTrainedModel", ] if TYPE_CHECKING: from .configuration_time_series_transformer import ( TIME_SERIES_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TimeSeriesTransformerConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_time_series_transformer import ( TIME_SERIES_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TimeSeriesTransformerForPrediction, TimeSeriesTransformerModel, TimeSeriesTransformerPreTrainedModel, ) else: import sys _snake_case = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
54
0
import requests _snake_case = "YOUR API KEY" def lowerCAmelCase_ ( snake_case_,snake_case_ = giphy_api_key ): _A : Union[str, Any] = "+".join(query.split() ) _A : List[str] = f'''https://api.giphy.com/v1/gifs/search?q={formatted_query}&api_key={api_key}''' _A : int = requests.get(lowerCamelCase__ ).json()["data"] return [gif["url"] for gif in gifs] if __name__ == "__main__": print("\n".join(get_gifs("space ship")))
714
import gc import random import unittest import numpy as np import torch from PIL import Image from diffusers import ( DDIMScheduler, KandinskyVaaImgaImgPipeline, KandinskyVaaPriorPipeline, UNetaDConditionModel, VQModel, ) 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 lowercase ( UpperCamelCase__,unittest.TestCase ): _a = KandinskyVaaImgaImgPipeline _a = ["image_embeds", "negative_image_embeds", "image"] _a = [ "image_embeds", "negative_image_embeds", "image", ] _a = [ "generator", "height", "width", "strength", "guidance_scale", "num_inference_steps", "return_dict", "guidance_scale", "num_images_per_prompt", "output_type", "return_dict", ] _a = False @property def a__ ( self ) -> int: return 32 @property def a__ ( self ) -> Union[str, Any]: return 32 @property def a__ ( self ) -> List[str]: return self.time_input_dim @property def a__ ( self ) -> Union[str, Any]: return self.time_input_dim * 4 @property def a__ ( self ) -> str: return 100 @property def a__ ( self ) -> Tuple: torch.manual_seed(0 ) _A : str = { """in_channels""": 4, # Out channels is double in channels because predicts mean and variance """out_channels""": 8, """addition_embed_type""": """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""": """image_proj""", """cross_attention_dim""": self.cross_attention_dim, """attention_head_dim""": 4, """resnet_time_scale_shift""": """scale_shift""", """class_embed_type""": None, } _A : Union[str, Any] = UNetaDConditionModel(**_a ) return model @property def a__ ( self ) -> int: 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 a__ ( self ) -> Tuple: torch.manual_seed(0 ) _A : Dict = VQModel(**self.dummy_movq_kwargs ) return model def a__ ( self ) -> int: _A : Any = self.dummy_unet _A : List[Any] = self.dummy_movq _A : str = { """num_train_timesteps""": 1000, """beta_schedule""": """linear""", """beta_start""": 0.00085, """beta_end""": 0.012, """clip_sample""": False, """set_alpha_to_one""": False, """steps_offset""": 0, """prediction_type""": """epsilon""", """thresholding""": False, } _A : int = DDIMScheduler(**_a ) _A : Tuple = { """unet""": unet, """scheduler""": scheduler, """movq""": movq, } return components def a__ ( self , _a , _a=0 ) -> str: _A : Optional[int] = floats_tensor((1, self.text_embedder_hidden_size) , rng=random.Random(_a ) ).to(_a ) _A : Dict = floats_tensor((1, self.text_embedder_hidden_size) , rng=random.Random(seed + 1 ) ).to( _a ) # create init_image _A : Optional[Any] = floats_tensor((1, 3, 64, 64) , rng=random.Random(_a ) ).to(_a ) _A : Optional[Any] = image.cpu().permute(0 , 2 , 3 , 1 )[0] _A : Optional[Any] = Image.fromarray(np.uinta(_a ) ).convert("""RGB""" ).resize((256, 256) ) if str(_a ).startswith("""mps""" ): _A : Tuple = torch.manual_seed(_a ) else: _A : str = torch.Generator(device=_a ).manual_seed(_a ) _A : Optional[Any] = { """image""": init_image, """image_embeds""": image_embeds, """negative_image_embeds""": negative_image_embeds, """generator""": generator, """height""": 64, """width""": 64, """num_inference_steps""": 10, """guidance_scale""": 7.0, """strength""": 0.2, """output_type""": """np""", } return inputs def a__ ( self ) -> Union[str, Any]: _A : Dict = """cpu""" _A : int = self.get_dummy_components() _A : Optional[int] = self.pipeline_class(**_a ) _A : Any = pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) _A : List[Any] = pipe(**self.get_dummy_inputs(_a ) ) _A : Dict = output.images _A : List[str] = pipe( **self.get_dummy_inputs(_a ) , return_dict=_a , )[0] _A : Dict = image[0, -3:, -3:, -1] _A : Union[str, Any] = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) _A : Optional[int] = np.array( [0.6199778, 0.63984406, 0.46145785, 0.62944984, 0.5622215, 0.47306132, 0.47441456, 0.4607606, 0.48719263] ) 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()}''' @slow @require_torch_gpu class lowercase ( unittest.TestCase ): def a__ ( self ) -> List[Any]: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def a__ ( self ) -> List[str]: _A : Any = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/kandinskyv22/kandinskyv22_img2img_frog.npy""" ) _A : Union[str, Any] = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/kandinsky/cat.png""" ) _A : Dict = """A red cartoon frog, 4k""" _A : Dict = KandinskyVaaPriorPipeline.from_pretrained( """kandinsky-community/kandinsky-2-2-prior""" , torch_dtype=torch.floataa ) pipe_prior.to(_a ) _A : int = KandinskyVaaImgaImgPipeline.from_pretrained( """kandinsky-community/kandinsky-2-2-decoder""" , torch_dtype=torch.floataa ) _A : Dict = pipeline.to(_a ) pipeline.set_progress_bar_config(disable=_a ) _A : Tuple = torch.Generator(device="""cpu""" ).manual_seed(0 ) _A , _A : List[str] = pipe_prior( _a , generator=_a , num_inference_steps=5 , negative_prompt="""""" , ).to_tuple() _A : int = pipeline( image=_a , image_embeds=_a , negative_image_embeds=_a , generator=_a , num_inference_steps=100 , height=768 , width=768 , strength=0.2 , output_type="""np""" , ) _A : Optional[int] = output.images[0] assert image.shape == (768, 768, 3) assert_mean_pixel_difference(_a , _a )
54
0
from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices _snake_case = logging.get_logger(__name__) _snake_case = { "shi-labs/nat-mini-in1k-224": "https://huggingface.co/shi-labs/nat-mini-in1k-224/resolve/main/config.json", # See all Nat models at https://huggingface.co/models?filter=nat } class lowercase ( UpperCamelCase__,UpperCamelCase__ ): _a = "nat" _a = { "num_attention_heads": "num_heads", "num_hidden_layers": "num_layers", } def __init__( self , _a=4 , _a=3 , _a=64 , _a=[3, 4, 6, 5] , _a=[2, 4, 8, 16] , _a=7 , _a=3.0 , _a=True , _a=0.0 , _a=0.0 , _a=0.1 , _a="gelu" , _a=0.02 , _a=1e-5 , _a=0.0 , _a=None , _a=None , **_a , ) -> Any: super().__init__(**lowercase_ ) _A : Optional[int] = patch_size _A : Optional[Any] = num_channels _A : Tuple = embed_dim _A : Union[str, Any] = depths _A : Any = len(lowercase_ ) _A : List[Any] = num_heads _A : Optional[int] = kernel_size _A : Any = mlp_ratio _A : Any = qkv_bias _A : str = hidden_dropout_prob _A : Union[str, Any] = attention_probs_dropout_prob _A : Optional[Any] = drop_path_rate _A : List[str] = hidden_act _A : Optional[int] = layer_norm_eps _A : List[Any] = initializer_range # we set the hidden_size attribute in order to make Nat work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model _A : Optional[Any] = int(embed_dim * 2 ** (len(lowercase_ ) - 1) ) _A : Tuple = layer_scale_init_value _A : List[str] = ["""stem"""] + [F'''stage{idx}''' for idx in range(1 , len(lowercase_ ) + 1 )] _A , _A : Tuple = get_aligned_output_features_output_indices( out_features=lowercase_ , out_indices=lowercase_ , stage_names=self.stage_names )
715
def lowerCAmelCase_ ( snake_case_ = 1000000 ): _A : Any = limit + 1 _A : Tuple = [0] * limit for first_term in range(1,snake_case_ ): for n in range(snake_case_,snake_case_,snake_case_ ): _A : Optional[int] = 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 _A : List[str] = sum(1 for x in frequency[1:limit] if x == 10 ) return count if __name__ == "__main__": print(f"""{solution() = }""")
54
0
_snake_case : Union[str, Any] = { 0: "0", 1: "1", 2: "2", 3: "3", 4: "4", 5: "5", 6: "6", 7: "7", 8: "8", 9: "9", 10: "a", 11: "b", 12: "c", 13: "d", 14: "e", 15: "f", } def lowerCAmelCase_ ( snake_case_ ): assert type(snake_case__ ) in (int, float) and decimal == int(snake_case__ ) _A : Any = int(snake_case__ ) _A : Any = """""" _A : Tuple = False if decimal < 0: _A : List[str] = True decimal *= -1 while decimal > 0: _A , _A : int = divmod(snake_case__,16 ) _A : List[str] = values[remainder] + hexadecimal _A : List[str] = """0x""" + hexadecimal if negative: _A : Tuple = """-""" + hexadecimal return hexadecimal if __name__ == "__main__": import doctest doctest.testmod()
716
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 lowercase ( tf.keras.layers.Layer ): def __init__( self , _a , _a , _a = None , _a = None ) -> Any: super().__init__() _A : Dict = pad_token_id _A : List[Any] = max_length _A : Optional[int] = vocab _A : Optional[int] = merges _A : Optional[int] = BytePairTokenizer(_a , _a , sequence_length=_a ) @classmethod def a__ ( cls , _a , *_a , **_a ) -> str: _A : Any = [""" """.join(_a ) for m in tokenizer.bpe_ranks.keys()] _A : str = tokenizer.get_vocab() return cls(_a , _a , *_a , **_a ) @classmethod def a__ ( cls , _a , *_a , **_a ) -> List[Any]: _A : Union[str, Any] = GPTaTokenizer.from_pretrained(_a , *_a , **_a ) return cls.from_tokenizer(_a , *_a , **_a ) @classmethod def a__ ( cls , _a ) -> Union[str, Any]: return cls(**_a ) def a__ ( self ) -> Union[str, Any]: return { "vocab": self.vocab, "merges": self.merges, "max_length": self.max_length, "pad_token_id": self.pad_token_id, } def a__ ( self , _a , _a = None ) -> int: _A : Optional[int] = self.tf_tokenizer(_a ) _A : Tuple = tf.ones_like(_a ) if self.pad_token_id is not None: # pad the tokens up to max length _A : Dict = max_length if max_length is not None else self.max_length if max_length is not None: _A , _A : Dict = pad_model_inputs( _a , max_seq_length=_a , pad_value=self.pad_token_id ) return {"attention_mask": attention_mask, "input_ids": input_ids}
54
0
import json from typing import List, Optional, Tuple from tokenizers import normalizers from ....tokenization_utils_fast import PreTrainedTokenizerFast from ....utils import logging from .tokenization_retribert import RetriBertTokenizer _snake_case = logging.get_logger(__name__) _snake_case = {'vocab_file': 'vocab.txt', 'tokenizer_file': 'tokenizer.json'} _snake_case = { 'vocab_file': { 'yjernite/retribert-base-uncased': ( 'https://huggingface.co/yjernite/retribert-base-uncased/resolve/main/vocab.txt' ), }, 'tokenizer_file': { 'yjernite/retribert-base-uncased': ( 'https://huggingface.co/yjernite/retribert-base-uncased/resolve/main/tokenizer.json' ), }, } _snake_case = { 'yjernite/retribert-base-uncased': 512, } _snake_case = { 'yjernite/retribert-base-uncased': {'do_lower_case': True}, } class lowercase ( UpperCamelCase__ ): _a = VOCAB_FILES_NAMES _a = PRETRAINED_VOCAB_FILES_MAP _a = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _a = PRETRAINED_INIT_CONFIGURATION _a = RetriBertTokenizer _a = ['input_ids', 'attention_mask'] def __init__( self , _a=None , _a=None , _a=True , _a="[UNK]" , _a="[SEP]" , _a="[PAD]" , _a="[CLS]" , _a="[MASK]" , _a=True , _a=None , **_a , ) -> List[Any]: super().__init__( UpperCAmelCase__ , tokenizer_file=UpperCAmelCase__ , do_lower_case=UpperCAmelCase__ , unk_token=UpperCAmelCase__ , sep_token=UpperCAmelCase__ , pad_token=UpperCAmelCase__ , cls_token=UpperCAmelCase__ , mask_token=UpperCAmelCase__ , tokenize_chinese_chars=UpperCAmelCase__ , strip_accents=UpperCAmelCase__ , **UpperCAmelCase__ , ) _A : List[Any] = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get("""lowercase""" , UpperCAmelCase__ ) != do_lower_case or normalizer_state.get("""strip_accents""" , UpperCAmelCase__ ) != strip_accents or normalizer_state.get("""handle_chinese_chars""" , UpperCAmelCase__ ) != tokenize_chinese_chars ): _A : int = getattr(UpperCAmelCase__ , normalizer_state.pop("""type""" ) ) _A : List[Any] = do_lower_case _A : Union[str, Any] = strip_accents _A : int = tokenize_chinese_chars _A : int = normalizer_class(**UpperCAmelCase__ ) _A : Union[str, Any] = do_lower_case def a__ ( self , _a , _a=None ) -> List[str]: _A : str = [self.cls_token_id] + token_ids_a + [self.sep_token_id] if token_ids_a: output += token_ids_a + [self.sep_token_id] return output def a__ ( self , _a , _a = None ) -> Dict: _A : List[Any] = [self.sep_token_id] _A : Tuple = [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 a__ ( self , _a , _a = None ) -> List[str]: _A : Tuple = self._tokenizer.model.save(UpperCAmelCase__ , name=UpperCAmelCase__ ) return tuple(UpperCAmelCase__ )
717
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 _snake_case = get_tests_dir("fixtures/test_sentencepiece_no_bos.model") @require_sentencepiece @require_tokenizers class lowercase ( UpperCamelCase__,unittest.TestCase ): _a = PegasusTokenizer _a = PegasusTokenizerFast _a = True _a = True def a__ ( self ) -> int: super().setUp() # We have a SentencePiece fixture for testing _A : List[Any] = PegasusTokenizer(_a ) tokenizer.save_pretrained(self.tmpdirname ) @cached_property def a__ ( self ) -> int: return PegasusTokenizer.from_pretrained("""google/pegasus-large""" ) def a__ ( self , **_a ) -> PegasusTokenizer: return PegasusTokenizer.from_pretrained(self.tmpdirname , **_a ) def a__ ( self , _a ) -> List[Any]: return ("This is a test", "This is a test") def a__ ( self ) -> int: _A : Dict = """</s>""" _A : Dict = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(_a ) , _a ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(_a ) , _a ) def a__ ( self ) -> Dict: _A : List[str] = 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(_a ) , 1103 ) def a__ ( self ) -> Optional[int]: self.assertEqual(self.get_tokenizer().vocab_size , 1103 ) def a__ ( self ) -> Tuple: _A : Any = self.rust_tokenizer_class.from_pretrained(self.tmpdirname ) _A : Optional[int] = self.tokenizer_class.from_pretrained(self.tmpdirname ) _A : int = ( """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 : Optional[int] = rust_tokenizer([raw_input_str] , return_tensors=_a , add_special_tokens=_a ).input_ids[0] _A : List[Any] = py_tokenizer([raw_input_str] , return_tensors=_a , add_special_tokens=_a ).input_ids[0] self.assertListEqual(_a , _a ) def a__ ( self ) -> Any: _A : str = self._large_tokenizer # <mask_1> masks whole sentence while <mask_2> masks single word _A : Optional[int] = """<mask_1> To ensure a <mask_2> flow of bank resolutions.""" _A : Union[str, Any] = [2, 413, 615, 114, 3, 1971, 113, 1679, 1_0710, 107, 1] _A : Union[str, Any] = tokenizer([raw_input_str] , return_tensors=_a ).input_ids[0] self.assertListEqual(_a , _a ) def a__ ( self ) -> List[str]: _A : Optional[int] = self._large_tokenizer # The tracebacks for the following asserts are **better** without messages or self.assertEqual assert tokenizer.vocab_size == 9_6103 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 : Any = """To ensure a smooth flow of bank resolutions.""" _A : Optional[int] = [413, 615, 114, 2291, 1971, 113, 1679, 1_0710, 107, 1] _A : Optional[Any] = tokenizer([raw_input_str] , return_tensors=_a ).input_ids[0] self.assertListEqual(_a , _a ) assert tokenizer.convert_ids_to_tokens([0, 1, 2, 3] ) == ["<pad>", "</s>", "<mask_1>", "<mask_2>"] @require_torch def a__ ( self ) -> List[str]: _A : Union[str, Any] = ["""This is going to be way too long.""" * 150, """short example"""] _A : Optional[Any] = ["""not super long but more than 5 tokens""", """tiny"""] _A : Union[str, Any] = self._large_tokenizer(_a , padding=_a , truncation=_a , return_tensors="""pt""" ) _A : Tuple = self._large_tokenizer( text_target=_a , max_length=5 , padding=_a , truncation=_a , 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(_a ) == 2 # input_ids, attention_mask. @slow def a__ ( self ) -> Optional[Any]: # fmt: off _A : List[Any] = {"""input_ids""": [[3_8979, 143, 1_8485, 606, 130, 2_6669, 8_7686, 121, 5_4189, 1129, 111, 2_6669, 8_7686, 121, 9114, 1_4787, 121, 1_3249, 158, 592, 956, 121, 1_4621, 3_1576, 143, 6_2613, 108, 9688, 930, 4_3430, 1_1562, 6_2613, 304, 108, 1_1443, 897, 108, 9314, 1_7415, 6_3399, 108, 1_1443, 7614, 1_8316, 118, 4284, 7148, 1_2430, 143, 1400, 2_5703, 158, 111, 4284, 7148, 1_1772, 143, 2_1297, 1064, 158, 122, 204, 3506, 1754, 1133, 1_4787, 1581, 115, 3_3224, 4482, 111, 1355, 110, 2_9173, 317, 5_0833, 108, 2_0147, 9_4665, 111, 7_7198, 107, 1], [110, 6_2613, 117, 638, 112, 1133, 121, 2_0098, 1355, 7_9050, 1_3872, 135, 1596, 5_3541, 1352, 141, 1_3039, 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, 1_8289, 1_7780, 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=_a , model_name="""google/bigbird-pegasus-large-arxiv""" , revision="""ba85d0851d708441f91440d509690f1ab6353415""" , ) @require_sentencepiece @require_tokenizers class lowercase ( UpperCamelCase__,unittest.TestCase ): _a = PegasusTokenizer _a = PegasusTokenizerFast _a = True _a = True def a__ ( self ) -> Optional[Any]: super().setUp() # We have a SentencePiece fixture for testing _A : Tuple = PegasusTokenizer(_a , offset=0 , mask_token_sent=_a , mask_token="""[MASK]""" ) tokenizer.save_pretrained(self.tmpdirname ) @cached_property def a__ ( self ) -> Optional[Any]: return PegasusTokenizer.from_pretrained("""google/bigbird-pegasus-large-arxiv""" ) def a__ ( self , **_a ) -> PegasusTokenizer: return PegasusTokenizer.from_pretrained(self.tmpdirname , **_a ) def a__ ( self , _a ) -> List[str]: return ("This is a test", "This is a test") def a__ ( self ) -> List[Any]: _A : List[Any] = self.rust_tokenizer_class.from_pretrained(self.tmpdirname ) _A : Dict = self.tokenizer_class.from_pretrained(self.tmpdirname ) _A : Dict = ( """Let's see which <unk> is the better <unk_token> one [MASK] It seems like this [MASK] was important </s>""" """ <pad> <pad> <pad>""" ) _A : Optional[Any] = rust_tokenizer([raw_input_str] , return_tensors=_a , add_special_tokens=_a ).input_ids[0] _A : int = py_tokenizer([raw_input_str] , return_tensors=_a , add_special_tokens=_a ).input_ids[0] self.assertListEqual(_a , _a ) @require_torch def a__ ( self ) -> Optional[int]: _A : Tuple = ["""This is going to be way too long.""" * 1000, """short example"""] _A : Optional[Any] = ["""not super long but more than 5 tokens""", """tiny"""] _A : Tuple = self._large_tokenizer(_a , padding=_a , truncation=_a , return_tensors="""pt""" ) _A : str = self._large_tokenizer( text_target=_a , max_length=5 , padding=_a , truncation=_a , 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(_a ) == 2 # input_ids, attention_mask. def a__ ( self ) -> Dict: _A : Optional[int] = ( """This is an example string that is used to test the original TF implementation against the HF""" """ implementation""" ) _A : Any = self._large_tokenizer(_a ).input_ids self.assertListEqual( _a , [182, 117, 142, 587, 4211, 120, 117, 263, 112, 804, 109, 856, 2_5016, 3137, 464, 109, 2_6955, 3137, 1] , )
54
0
from typing import List, Optional, Tuple, Union import torch from ...models import UNetaDModel from ...schedulers import ScoreSdeVeScheduler from ...utils import randn_tensor from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput class lowercase ( SCREAMING_SNAKE_CASE__ ): _a = 4_2 _a = 4_2 def __init__( self , _a , _a ) -> Tuple: super().__init__() self.register_modules(unet=snake_case__ , scheduler=snake_case__ ) @torch.no_grad() def __call__( self , _a = 1 , _a = 2000 , _a = None , _a = "pil" , _a = True , **_a , ) -> Optional[int]: _A : Optional[Any] = self.unet.config.sample_size _A : Dict = (batch_size, 3, img_size, img_size) _A : List[Any] = self.unet _A : Tuple = randn_tensor(snake_case__ , generator=snake_case__ ) * self.scheduler.init_noise_sigma _A : Tuple = sample.to(self.device ) self.scheduler.set_timesteps(snake_case__ ) self.scheduler.set_sigmas(snake_case__ ) for i, t in enumerate(self.progress_bar(self.scheduler.timesteps ) ): _A : Optional[Any] = self.scheduler.sigmas[i] * torch.ones(shape[0] , device=self.device ) # correction step for _ in range(self.scheduler.config.correct_steps ): _A : List[Any] = self.unet(snake_case__ , snake_case__ ).sample _A : List[Any] = self.scheduler.step_correct(snake_case__ , snake_case__ , generator=snake_case__ ).prev_sample # prediction step _A : List[str] = model(snake_case__ , snake_case__ ).sample _A : Tuple = self.scheduler.step_pred(snake_case__ , snake_case__ , snake_case__ , generator=snake_case__ ) _A : Tuple = output.prev_sample, output.prev_sample_mean _A : List[str] = sample_mean.clamp(0 , 1 ) _A : Union[str, Any] = sample.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": _A : str = self.numpy_to_pil(snake_case__ ) if not return_dict: return (sample,) return ImagePipelineOutput(images=snake_case__ )
718
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available, is_vision_available, ) _snake_case = { "configuration_efficientformer": [ "EFFICIENTFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", "EfficientFormerConfig", ] } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = ["EfficientFormerImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = [ "EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "EfficientFormerForImageClassification", "EfficientFormerForImageClassificationWithTeacher", "EfficientFormerModel", "EfficientFormerPreTrainedModel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = [ "TF_EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "TFEfficientFormerForImageClassification", "TFEfficientFormerForImageClassificationWithTeacher", "TFEfficientFormerModel", "TFEfficientFormerPreTrainedModel", ] if TYPE_CHECKING: from .configuration_efficientformer import EFFICIENTFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, EfficientFormerConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_efficientformer import EfficientFormerImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_efficientformer import ( EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, EfficientFormerForImageClassification, EfficientFormerForImageClassificationWithTeacher, EfficientFormerModel, EfficientFormerPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_efficientformer import ( TF_EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TFEfficientFormerForImageClassification, TFEfficientFormerForImageClassificationWithTeacher, TFEfficientFormerModel, TFEfficientFormerPreTrainedModel, ) else: import sys _snake_case = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
54
0
from sklearn.metrics import matthews_corrcoef import datasets _snake_case = "\nCompute the Matthews correlation coefficient (MCC)\n\nThe Matthews correlation coefficient is used in machine learning as a\nmeasure of the quality of binary and multiclass classifications. It takes\ninto account true and false positives and negatives and is generally\nregarded as a balanced measure which can be used even if the classes are of\nvery different sizes. The MCC is in essence a correlation coefficient value\nbetween -1 and +1. A coefficient of +1 represents a perfect prediction, 0\nan average random prediction and -1 an inverse prediction. The statistic\nis also known as the phi coefficient. [source: Wikipedia]\n" _snake_case = "\nArgs:\n predictions (list of int): Predicted labels, as returned by a model.\n references (list of int): Ground truth labels.\n sample_weight (list of int, float, or bool): Sample weights. Defaults to `None`.\nReturns:\n matthews_correlation (dict containing float): Matthews correlation.\nExamples:\n Example 1, a basic example with only predictions and references as inputs:\n >>> matthews_metric = datasets.load_metric(\"matthews_correlation\")\n >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],\n ... predictions=[1, 2, 2, 0, 3, 3])\n >>> print(round(results[\'matthews_correlation\'], 2))\n 0.54\n\n Example 2, the same example as above, but also including sample weights:\n >>> matthews_metric = datasets.load_metric(\"matthews_correlation\")\n >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],\n ... predictions=[1, 2, 2, 0, 3, 3],\n ... sample_weight=[0.5, 3, 1, 1, 1, 2])\n >>> print(round(results[\'matthews_correlation\'], 2))\n 0.1\n\n Example 3, the same example as above, but with sample weights that cause a negative correlation:\n >>> matthews_metric = datasets.load_metric(\"matthews_correlation\")\n >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],\n ... predictions=[1, 2, 2, 0, 3, 3],\n ... sample_weight=[0.5, 1, 0, 0, 0, 1])\n >>> print(round(results[\'matthews_correlation\'], 2))\n -0.25\n" _snake_case = "\\n@article{scikit-learn,\n title={Scikit-learn: Machine Learning in {P}ython},\n author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V.\n and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P.\n and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and\n Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.},\n journal={Journal of Machine Learning Research},\n volume={12},\n pages={2825--2830},\n year={2011}\n}\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION,_KWARGS_DESCRIPTION ) class lowercase ( datasets.Metric ): def a__ ( self ) -> int: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": datasets.Value("""int32""" ), """references""": datasets.Value("""int32""" ), } ) , reference_urls=[ """https://scikit-learn.org/stable/modules/generated/sklearn.metrics.matthews_corrcoef.html""" ] , ) def a__ ( self , _a , _a , _a=None ) -> int: return { "matthews_correlation": float(matthews_corrcoef(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , sample_weight=SCREAMING_SNAKE_CASE_ ) ), }
719
from unittest import TestCase from datasets import Sequence, Value from datasets.arrow_dataset import Dataset class lowercase ( UpperCamelCase__ ): def a__ ( self ) -> Optional[int]: return [ {"col_1": 3, "col_2": "a"}, {"col_1": 2, "col_2": "b"}, {"col_1": 1, "col_2": "c"}, {"col_1": 0, "col_2": "d"}, ] def a__ ( self ) -> Optional[int]: _A : Union[str, Any] = {"""col_1""": [3, 2, 1, 0], """col_2""": ["""a""", """b""", """c""", """d"""]} return Dataset.from_dict(_a ) def a__ ( self ) -> Any: _A : str = self._create_example_records() _A : List[Any] = Dataset.from_list(_a ) self.assertListEqual(dset.column_names , ["""col_1""", """col_2"""] ) for i, r in enumerate(_a ): self.assertDictEqual(_a , example_records[i] ) def a__ ( self ) -> List[str]: _A : Dict = self._create_example_records() _A : List[str] = Dataset.from_list(_a ) _A : str = Dataset.from_dict({k: [r[k] for r in example_records] for k in example_records[0]} ) self.assertEqual(dset.info , dset_from_dict.info ) def a__ ( self ) -> str: # checks what happens with missing columns _A : List[str] = [{"""col_1""": 1}, {"""col_2""": """x"""}] _A : List[str] = Dataset.from_list(_a ) self.assertDictEqual(dset[0] , {"""col_1""": 1} ) self.assertDictEqual(dset[1] , {"""col_1""": None} ) # NB: first record is used for columns def a__ ( self ) -> Dict: # checks if the type can be inferred from the second record _A : List[Any] = [{"""col_1""": []}, {"""col_1""": [1, 2]}] _A : str = Dataset.from_list(_a ) self.assertEqual(dset.info.features["""col_1"""] , Sequence(Value("""int64""" ) ) ) def a__ ( self ) -> Dict: _A : List[str] = Dataset.from_list([] ) self.assertEqual(len(_a ) , 0 ) self.assertListEqual(dset.column_names , [] )
54
0
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 _snake_case = logging.get_logger(__name__) _snake_case = {"""vocab_file""": """sentencepiece.bpe.model"""} _snake_case = { """vocab_file""": { """moussaKam/mbarthez""": """https://huggingface.co/moussaKam/mbarthez/resolve/main/sentencepiece.bpe.model""", """moussaKam/barthez""": """https://huggingface.co/moussaKam/barthez/resolve/main/sentencepiece.bpe.model""", """moussaKam/barthez-orangesum-title""": ( """https://huggingface.co/moussaKam/barthez-orangesum-title/resolve/main/sentencepiece.bpe.model""" ), }, } _snake_case = { """moussaKam/mbarthez""": 1024, """moussaKam/barthez""": 1024, """moussaKam/barthez-orangesum-title""": 1024, } _snake_case = """▁""" class lowercase ( _snake_case ): _a = VOCAB_FILES_NAMES _a = PRETRAINED_VOCAB_FILES_MAP _a = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _a = ["""input_ids""", """attention_mask"""] def __init__( self , _a , _a="<s>" , _a="</s>" , _a="</s>" , _a="<s>" , _a="<unk>" , _a="<pad>" , _a="<mask>" , _a = None , **_a , ) -> int: # Mask token behave like a normal word, i.e. include the space before it _A : List[str] = AddedToken(snake_case_ , lstrip=snake_case_ , rstrip=snake_case_ ) if isinstance(snake_case_ , snake_case_ ) else mask_token _A : Optional[Any] = {} if sp_model_kwargs is None else sp_model_kwargs 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_ , sp_model_kwargs=self.sp_model_kwargs , **snake_case_ , ) _A : Optional[Any] = vocab_file _A : Tuple = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(snake_case_ ) ) _A : Optional[Any] = {"<s>": 0, "<pad>": 1, "</s>": 2, "<unk>": 3} _A : Any = len(self.sp_model ) - 1 _A : int = {v: k for k, v in self.fairseq_tokens_to_ids.items()} def a__ ( self , _a , _a = None ) -> List[Any]: if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] _A : List[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 a__ ( self , _a , _a = None , _a = False ) -> Optional[Any]: 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 , _a , _a = None ) -> Tuple: _A : Dict = [self.sep_token_id] _A : List[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]: return len(self.sp_model ) def a__ ( self ) -> Dict: _A : List[str] = {self.convert_ids_to_tokens(snake_case_ ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def a__ ( self , _a ) -> Optional[int]: return self.sp_model.encode(snake_case_ , out_type=snake_case_ ) def a__ ( self , _a ) -> Union[str, Any]: if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] _A : Optional[int] = self.sp_model.PieceToId(snake_case_ ) return spm_id if spm_id else self.unk_token_id def a__ ( self , _a ) -> List[Any]: if index in self.fairseq_ids_to_tokens: return self.fairseq_ids_to_tokens[index] return self.sp_model.IdToPiece(snake_case_ ) def a__ ( self , _a ) -> Optional[int]: _A : Optional[Any] = [] _A : List[Any] = "" _A : Dict = 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(snake_case_ ) + token _A : Tuple = True _A : int = [] else: current_sub_tokens.append(snake_case_ ) _A : Dict = False out_string += self.sp_model.decode(snake_case_ ) return out_string.strip() def __getstate__( self ) -> str: _A : Dict = self.__dict__.copy() _A : int = None return state def __setstate__( self , _a ) -> List[Any]: _A : str = d # for backward compatibility if not hasattr(self , """sp_model_kwargs""" ): _A : List[str] = {} _A : Any = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def a__ ( self , _a , _a = None ) -> Optional[Any]: if not os.path.isdir(snake_case_ ): logger.error(F'''Vocabulary path ({save_directory}) should be a directory''' ) return _A : Any = os.path.join( snake_case_ , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(snake_case_ ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , snake_case_ ) elif not os.path.isfile(self.vocab_file ): with open(snake_case_ , """wb""" ) as fi: _A : Optional[int] = self.sp_model.serialized_model_proto() fi.write(snake_case_ ) return (out_vocab_file,)
720
from __future__ import annotations from collections.abc import Sequence from typing import Literal def lowerCAmelCase_ ( snake_case_,snake_case_ ): _A : List[str] = list(snake_case_ ) _A : List[Any] = list(snake_case_ ) _A : Tuple = 0 for i in range(len(snake_case_ ) ): if lista[i] != lista[i]: count += 1 _A : Optional[Any] = """_""" if count > 1: return False else: return "".join(snake_case_ ) def lowerCAmelCase_ ( snake_case_ ): _A : Optional[Any] = [] while True: _A : int = ["""$"""] * len(snake_case_ ) _A : Any = [] for i in range(len(snake_case_ ) ): for j in range(i + 1,len(snake_case_ ) ): _A : Tuple = compare_string(binary[i],binary[j] ) if k is False: _A : str = """*""" _A : str = """*""" temp.append("""X""" ) for i in range(len(snake_case_ ) ): if checka[i] == "$": pi.append(binary[i] ) if len(snake_case_ ) == 0: return pi _A : Dict = list(set(snake_case_ ) ) def lowerCAmelCase_ ( snake_case_,snake_case_ ): _A : List[str] = [] for minterm in minterms: _A : Tuple = """""" for _ in range(snake_case_ ): _A : Optional[Any] = str(minterm % 2 ) + string minterm //= 2 temp.append(snake_case_ ) return temp def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_ ): _A : Dict = list(snake_case_ ) _A : Tuple = list(snake_case_ ) _A : Dict = 0 for i in range(len(snake_case_ ) ): if lista[i] != lista[i]: count_n += 1 return count_n == count def lowerCAmelCase_ ( snake_case_,snake_case_ ): _A : Optional[int] = [] _A : str = [0] * len(snake_case_ ) for i in range(len(chart[0] ) ): _A : Union[str, Any] = 0 _A : Optional[Any] = -1 for j in range(len(snake_case_ ) ): if chart[j][i] == 1: count += 1 _A : Dict = j if count == 1: _A : int = 1 for i in range(len(snake_case_ ) ): if select[i] == 1: for j in range(len(chart[0] ) ): if chart[i][j] == 1: for k in range(len(snake_case_ ) ): _A : int = 0 temp.append(prime_implicants[i] ) while True: _A : Optional[Any] = 0 _A : Tuple = -1 _A : List[Any] = 0 for i in range(len(snake_case_ ) ): _A : List[str] = chart[i].count(1 ) if count_n > max_n: _A : Optional[int] = count_n _A : Tuple = i if max_n == 0: return temp temp.append(prime_implicants[rem] ) for i in range(len(chart[0] ) ): if chart[rem][i] == 1: for j in range(len(snake_case_ ) ): _A : Optional[int] = 0 def lowerCAmelCase_ ( snake_case_,snake_case_ ): _A : Optional[int] = [[0 for x in range(len(snake_case_ ) )] for x in range(len(snake_case_ ) )] for i in range(len(snake_case_ ) ): _A : List[Any] = prime_implicants[i].count("""_""" ) for j in range(len(snake_case_ ) ): if is_for_table(prime_implicants[i],binary[j],snake_case_ ): _A : Union[str, Any] = 1 return chart def lowerCAmelCase_ ( ): _A : Dict = int(input("""Enter the no. of variables\n""" ) ) _A : Dict = [ float(snake_case_ ) for x in input( """Enter the decimal representation of Minterms 'Spaces Separated'\n""" ).split() ] _A : int = decimal_to_binary(snake_case_,snake_case_ ) _A : Optional[Any] = check(snake_case_ ) print("""Prime Implicants are:""" ) print(snake_case_ ) _A : int = prime_implicant_chart(snake_case_,snake_case_ ) _A : int = selection(snake_case_,snake_case_ ) print("""Essential Prime Implicants are:""" ) print(snake_case_ ) if __name__ == "__main__": import doctest doctest.testmod() main()
54
0
def lowerCAmelCase_ ( snake_case_ = 200 ): _A : Any = [1, 2, 5, 10, 20, 50, 100, 200] _A : int = [0] * (pence + 1) _A : str = 1 # base case: 1 way to make 0 pence for coin in coins: for i in range(snake_case_,pence + 1,1 ): number_of_ways[i] += number_of_ways[i - coin] return number_of_ways[pence] if __name__ == "__main__": assert solution(200) == 73682
721
from __future__ import annotations def lowerCAmelCase_ ( snake_case_,snake_case_ = None ): _A : Tuple = word_bank or [] # create a table _A : int = len(snake_case_ ) + 1 _A : list[list[list[str]]] = [] for _ in range(snake_case_ ): table.append([] ) # seed value _A : Dict = [[]] # because empty string has empty combination # iterate through the indices for i in range(snake_case_ ): # condition if table[i] != []: for word in word_bank: # slice condition if target[i : i + len(snake_case_ )] == word: _A : list[list[str]] = [ [word, *way] for way in table[i] ] # adds the word to every combination the current position holds # now,push that combination to the table[i+len(word)] table[i + len(snake_case_ )] += new_combinations # combinations are in reverse order so reverse for better output for combination in table[len(snake_case_ )]: combination.reverse() return table[len(snake_case_ )] if __name__ == "__main__": print(all_construct("jwajalapa", ["jwa", "j", "w", "a", "la", "lapa"])) print(all_construct("rajamati", ["s", "raj", "amat", "raja", "ma", "i", "t"])) print( all_construct( "hexagonosaurus", ["h", "ex", "hex", "ag", "ago", "ru", "auru", "rus", "go", "no", "o", "s"], ) )
54
0
def lowerCAmelCase_ ( snake_case_,snake_case_ ): # Check if the input is valid if not len(_lowerCAmelCase ) == len(_lowerCAmelCase ) == 3: raise ValueError("""Please enter a valid equation.""" ) if equationa[0] == equationa[1] == equationa[0] == equationa[1] == 0: raise ValueError("""Both a & b of two equations can\'t be zero.""" ) # Extract the coefficients _A , _A , _A : Any = equationa _A , _A , _A : Dict = equationa # Calculate the determinants of the matrices _A : Optional[Any] = aa * ba - aa * ba _A : Dict = ca * ba - ca * ba _A : Dict = aa * ca - aa * ca # Check if the system of linear equations has a solution (using Cramer's rule) if determinant == 0: if determinant_x == determinant_y == 0: raise ValueError("""Infinite solutions. (Consistent system)""" ) else: raise ValueError("""No solution. (Inconsistent system)""" ) else: if determinant_x == determinant_y == 0: # Trivial solution (Inconsistent system) return (0.0, 0.0) else: _A : Any = determinant_x / determinant _A : Optional[Any] = determinant_y / determinant # Non-Trivial Solution (Consistent system) return (x, y)
700
import operator def lowerCAmelCase_ ( snake_case_,snake_case_ = False,snake_case_ = None ): _A : str = operator.lt if reverse else operator.gt _A : Optional[Any] = solution or [] if not arr: return solution _A : Dict = [arr.pop(0 )] for i, item in enumerate(snake_case_ ): if _operator(snake_case_,sublist[-1] ): sublist.append(snake_case_ ) arr.pop(snake_case_ ) # merging sublist into solution list if not solution: solution.extend(snake_case_ ) else: while sublist: _A : Union[str, Any] = sublist.pop(0 ) for i, xx in enumerate(snake_case_ ): if not _operator(snake_case_,snake_case_ ): solution.insert(snake_case_,snake_case_ ) break else: solution.append(snake_case_ ) strand_sort(snake_case_,snake_case_,snake_case_ ) return solution if __name__ == "__main__": assert strand_sort([4, 3, 5, 1, 2]) == [1, 2, 3, 4, 5] assert strand_sort([4, 3, 5, 1, 2], reverse=True) == [5, 4, 3, 2, 1]
54
0
def lowerCAmelCase_ ( snake_case_,snake_case_ ): return x if y == 0 else greatest_common_divisor(lowerCamelCase_,x % y ) def lowerCAmelCase_ ( snake_case_,snake_case_ ): return (x * y) // greatest_common_divisor(lowerCamelCase_,lowerCamelCase_ ) def lowerCAmelCase_ ( snake_case_ = 20 ): _A : Any = 1 for i in range(1,n + 1 ): _A : List[Any] = lcm(lowerCamelCase_,lowerCamelCase_ ) return g if __name__ == "__main__": print(f"""{solution() = }""")
701
import gc import unittest from transformers import MODEL_FOR_MASKED_LM_MAPPING, TF_MODEL_FOR_MASKED_LM_MAPPING, FillMaskPipeline, pipeline from transformers.pipelines import PipelineException from transformers.testing_utils import ( is_pipeline_test, is_torch_available, nested_simplify, require_tf, require_torch, require_torch_gpu, slow, ) from .test_pipelines_common import ANY @is_pipeline_test class lowercase ( unittest.TestCase ): _a = MODEL_FOR_MASKED_LM_MAPPING _a = TF_MODEL_FOR_MASKED_LM_MAPPING def a__ ( self ) -> Tuple: super().tearDown() # clean-up as much as possible GPU memory occupied by PyTorch gc.collect() if is_torch_available(): import torch torch.cuda.empty_cache() @require_tf def a__ ( self ) -> Any: _A : Optional[Any] = pipeline(task="""fill-mask""" , model="""sshleifer/tiny-distilroberta-base""" , top_k=2 , framework="""tf""" ) _A : Optional[int] = unmasker("""My name is <mask>""" ) self.assertEqual( nested_simplify(_a , decimals=6 ) , [ {"""sequence""": """My name is grouped""", """score""": 2.1e-05, """token""": 3_8015, """token_str""": """ grouped"""}, {"""sequence""": """My name is accuser""", """score""": 2.1e-05, """token""": 2_5506, """token_str""": """ accuser"""}, ] , ) _A : Tuple = unmasker("""The largest city in France is <mask>""" ) self.assertEqual( nested_simplify(_a , decimals=6 ) , [ { """sequence""": """The largest city in France is grouped""", """score""": 2.1e-05, """token""": 3_8015, """token_str""": """ grouped""", }, { """sequence""": """The largest city in France is accuser""", """score""": 2.1e-05, """token""": 2_5506, """token_str""": """ accuser""", }, ] , ) _A : List[str] = unmasker("""My name is <mask>""" , targets=[""" Patrick""", """ Clara""", """ Teven"""] , top_k=3 ) self.assertEqual( nested_simplify(_a , decimals=6 ) , [ {"""sequence""": """My name is Clara""", """score""": 2e-05, """token""": 1_3606, """token_str""": """ Clara"""}, {"""sequence""": """My name is Patrick""", """score""": 2e-05, """token""": 3499, """token_str""": """ Patrick"""}, {"""sequence""": """My name is Te""", """score""": 1.9e-05, """token""": 2941, """token_str""": """ Te"""}, ] , ) @require_torch def a__ ( self ) -> str: _A : Any = pipeline(task="""fill-mask""" , model="""sshleifer/tiny-distilroberta-base""" , top_k=2 , framework="""pt""" ) _A : List[Any] = unmasker("""My name is <mask>""" ) self.assertEqual( nested_simplify(_a , decimals=6 ) , [ {"""sequence""": """My name is Maul""", """score""": 2.2e-05, """token""": 3_5676, """token_str""": """ Maul"""}, {"""sequence""": """My name isELS""", """score""": 2.2e-05, """token""": 1_6416, """token_str""": """ELS"""}, ] , ) _A : Optional[Any] = unmasker("""The largest city in France is <mask>""" ) self.assertEqual( nested_simplify(_a , decimals=6 ) , [ { """sequence""": """The largest city in France is Maul""", """score""": 2.2e-05, """token""": 3_5676, """token_str""": """ Maul""", }, {"""sequence""": """The largest city in France isELS""", """score""": 2.2e-05, """token""": 1_6416, """token_str""": """ELS"""}, ] , ) _A : Optional[int] = unmasker("""My name is <mask>""" , targets=[""" Patrick""", """ Clara""", """ Teven"""] , top_k=3 ) self.assertEqual( nested_simplify(_a , decimals=6 ) , [ {"""sequence""": """My name is Patrick""", """score""": 2.1e-05, """token""": 3499, """token_str""": """ Patrick"""}, {"""sequence""": """My name is Te""", """score""": 2e-05, """token""": 2941, """token_str""": """ Te"""}, {"""sequence""": """My name is Clara""", """score""": 2e-05, """token""": 1_3606, """token_str""": """ Clara"""}, ] , ) _A : str = unmasker("""My name is <mask> <mask>""" , top_k=2 ) self.assertEqual( nested_simplify(_a , decimals=6 ) , [ [ { """score""": 2.2e-05, """token""": 3_5676, """token_str""": """ Maul""", """sequence""": """<s>My name is Maul<mask></s>""", }, {"""score""": 2.2e-05, """token""": 1_6416, """token_str""": """ELS""", """sequence""": """<s>My name isELS<mask></s>"""}, ], [ { """score""": 2.2e-05, """token""": 3_5676, """token_str""": """ Maul""", """sequence""": """<s>My name is<mask> Maul</s>""", }, {"""score""": 2.2e-05, """token""": 1_6416, """token_str""": """ELS""", """sequence""": """<s>My name is<mask>ELS</s>"""}, ], ] , ) @require_torch_gpu def a__ ( self ) -> Union[str, Any]: _A : int = pipeline("""fill-mask""" , model="""hf-internal-testing/tiny-random-distilbert""" , device=0 , framework="""pt""" ) # convert model to fp16 pipe.model.half() _A : Optional[Any] = pipe("""Paris is the [MASK] of France.""" ) # We actually don't care about the result, we just want to make sure # it works, meaning the float16 tensor got casted back to float32 # for postprocessing. self.assertIsInstance(_a , _a ) @slow @require_torch def a__ ( self ) -> Optional[int]: _A : Optional[Any] = pipeline(task="""fill-mask""" , model="""distilroberta-base""" , top_k=2 , framework="""pt""" ) self.run_large_test(_a ) @slow @require_tf def a__ ( self ) -> Tuple: _A : str = pipeline(task="""fill-mask""" , model="""distilroberta-base""" , top_k=2 , framework="""tf""" ) self.run_large_test(_a ) def a__ ( self , _a ) -> Tuple: _A : Optional[int] = unmasker("""My name is <mask>""" ) self.assertEqual( nested_simplify(_a ) , [ {"""sequence""": """My name is John""", """score""": 0.008, """token""": 610, """token_str""": """ John"""}, {"""sequence""": """My name is Chris""", """score""": 0.007, """token""": 1573, """token_str""": """ Chris"""}, ] , ) _A : int = unmasker("""The largest city in France is <mask>""" ) self.assertEqual( nested_simplify(_a ) , [ { """sequence""": """The largest city in France is Paris""", """score""": 0.251, """token""": 2201, """token_str""": """ Paris""", }, { """sequence""": """The largest city in France is Lyon""", """score""": 0.214, """token""": 1_2790, """token_str""": """ Lyon""", }, ] , ) _A : Optional[Any] = unmasker("""My name is <mask>""" , targets=[""" Patrick""", """ Clara""", """ Teven"""] , top_k=3 ) self.assertEqual( nested_simplify(_a ) , [ {"""sequence""": """My name is Patrick""", """score""": 0.005, """token""": 3499, """token_str""": """ Patrick"""}, {"""sequence""": """My name is Clara""", """score""": 0.000, """token""": 1_3606, """token_str""": """ Clara"""}, {"""sequence""": """My name is Te""", """score""": 0.000, """token""": 2941, """token_str""": """ Te"""}, ] , ) @require_torch def a__ ( self ) -> Tuple: _A : List[str] = pipeline(task="""fill-mask""" , model="""sshleifer/tiny-distilroberta-base""" , framework="""pt""" ) _A : str = None _A : Union[str, Any] = None self.run_pipeline_test(_a , [] ) @require_tf def a__ ( self ) -> Union[str, Any]: _A : Tuple = pipeline(task="""fill-mask""" , model="""sshleifer/tiny-distilroberta-base""" , framework="""tf""" ) _A : Any = None _A : Dict = None self.run_pipeline_test(_a , [] ) def a__ ( self , _a , _a , _a ) -> Any: if tokenizer is None or tokenizer.mask_token_id is None: self.skipTest("""The provided tokenizer has no mask token, (probably reformer or wav2vec2)""" ) _A : Optional[Any] = FillMaskPipeline(model=_a , tokenizer=_a ) _A : Tuple = [ F'''This is another {tokenizer.mask_token} test''', ] return fill_masker, examples def a__ ( self , _a , _a ) -> Dict: _A : Dict = fill_masker.tokenizer _A : List[str] = fill_masker.model _A : List[str] = fill_masker( F'''This is a {tokenizer.mask_token}''' , ) self.assertEqual( _a , [ {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, ] , ) _A : Optional[Any] = fill_masker([F'''This is a {tokenizer.mask_token}'''] ) self.assertEqual( _a , [ {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, ] , ) _A : List[str] = fill_masker([F'''This is a {tokenizer.mask_token}''', F'''Another {tokenizer.mask_token} great test.'''] ) self.assertEqual( _a , [ [ {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, ], [ {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, ], ] , ) with self.assertRaises(_a ): fill_masker([None] ) # No mask_token is not supported with self.assertRaises(_a ): fill_masker("""This is""" ) self.run_test_top_k(_a , _a ) self.run_test_targets(_a , _a ) self.run_test_top_k_targets(_a , _a ) self.fill_mask_with_duplicate_targets_and_top_k(_a , _a ) self.fill_mask_with_multiple_masks(_a , _a ) def a__ ( self , _a , _a ) -> List[str]: _A : int = tokenizer.get_vocab() _A : str = sorted(vocab.keys() )[:2] # Pipeline argument _A : Tuple = FillMaskPipeline(model=_a , tokenizer=_a , targets=_a ) _A : Optional[int] = fill_masker(F'''This is a {tokenizer.mask_token}''' ) self.assertEqual( _a , [ {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, ] , ) _A : List[str] = {vocab[el] for el in targets} self.assertEqual({el["""token"""] for el in outputs} , _a ) _A : Union[str, Any] = [tokenizer.decode([x] ) for x in target_ids] self.assertEqual({el["""token_str"""] for el in outputs} , set(_a ) ) # Call argument _A : str = FillMaskPipeline(model=_a , tokenizer=_a ) _A : Optional[int] = fill_masker(F'''This is a {tokenizer.mask_token}''' , targets=_a ) self.assertEqual( _a , [ {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, ] , ) _A : int = {vocab[el] for el in targets} self.assertEqual({el["""token"""] for el in outputs} , _a ) _A : Any = [tokenizer.decode([x] ) for x in target_ids] self.assertEqual({el["""token_str"""] for el in outputs} , set(_a ) ) # Score equivalence _A : int = fill_masker(F'''This is a {tokenizer.mask_token}''' , targets=_a ) _A : Optional[int] = [top_mask["""token_str"""] for top_mask in outputs] _A : Union[str, Any] = [top_mask["""score"""] for top_mask in outputs] # For some BPE tokenizers, `</w>` is removed during decoding, so `token_str` won't be the same as in `targets`. if set(_a ) == set(_a ): _A : Tuple = fill_masker(F'''This is a {tokenizer.mask_token}''' , targets=_a ) _A : Union[str, Any] = [top_mask["""score"""] for top_mask in unmasked_targets] self.assertEqual(nested_simplify(_a ) , nested_simplify(_a ) ) # Raises with invalid with self.assertRaises(_a ): _A : str = fill_masker(F'''This is a {tokenizer.mask_token}''' , targets=[] ) # For some tokenizers, `""` is actually in the vocabulary and the expected error won't raised if "" not in tokenizer.get_vocab(): with self.assertRaises(_a ): _A : Optional[Any] = fill_masker(F'''This is a {tokenizer.mask_token}''' , targets=[""""""] ) with self.assertRaises(_a ): _A : int = fill_masker(F'''This is a {tokenizer.mask_token}''' , targets="""""" ) def a__ ( self , _a , _a ) -> Optional[Any]: _A : str = FillMaskPipeline(model=_a , tokenizer=_a , top_k=2 ) _A : str = fill_masker(F'''This is a {tokenizer.mask_token}''' ) self.assertEqual( _a , [ {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, ] , ) _A : Union[str, Any] = FillMaskPipeline(model=_a , tokenizer=_a ) _A : Union[str, Any] = fill_masker(F'''This is a {tokenizer.mask_token}''' , top_k=2 ) self.assertEqual( _a , [ {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, ] , ) self.assertEqual(nested_simplify(_a ) , nested_simplify(_a ) ) def a__ ( self , _a , _a ) -> List[Any]: _A : Union[str, Any] = tokenizer.get_vocab() _A : int = FillMaskPipeline(model=_a , tokenizer=_a ) # top_k=2, ntargets=3 _A : List[str] = sorted(vocab.keys() )[:3] _A : Tuple = fill_masker(F'''This is a {tokenizer.mask_token}''' , top_k=2 , targets=_a ) # If we use the most probably targets, and filter differently, we should still # have the same results _A : Any = [el["""token_str"""] for el in sorted(_a , key=lambda _a : x["score"] , reverse=_a )] # For some BPE tokenizers, `</w>` is removed during decoding, so `token_str` won't be the same as in `targets`. if set(_a ).issubset(_a ): _A : Any = fill_masker(F'''This is a {tokenizer.mask_token}''' , top_k=3 , targets=_a ) # They should yield exactly the same result self.assertEqual(nested_simplify(_a ) , nested_simplify(_a ) ) def a__ ( self , _a , _a ) -> str: _A : Optional[int] = FillMaskPipeline(model=_a , tokenizer=_a ) _A : List[Any] = tokenizer.get_vocab() # String duplicates + id duplicates _A : Optional[Any] = sorted(vocab.keys() )[:3] _A : Optional[Any] = [targets[0], targets[1], targets[0], targets[2], targets[1]] _A : Union[str, Any] = fill_masker(F'''My name is {tokenizer.mask_token}''' , targets=_a , top_k=10 ) # The target list contains duplicates, so we can't output more # than them self.assertEqual(len(_a ) , 3 ) def a__ ( self , _a , _a ) -> Tuple: _A : Any = FillMaskPipeline(model=_a , tokenizer=_a ) _A : Optional[Any] = fill_masker( F'''This is a {tokenizer.mask_token} {tokenizer.mask_token} {tokenizer.mask_token}''' , top_k=2 ) self.assertEqual( _a , [ [ {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, ], [ {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, ], [ {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, ], ] , )
54
0
import gc import unittest import numpy as np import torch import torch.nn.functional as F from transformers import ( ClapTextConfig, ClapTextModelWithProjection, RobertaTokenizer, SpeechTaHifiGan, SpeechTaHifiGanConfig, ) from diffusers import ( AudioLDMPipeline, AutoencoderKL, DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler, UNetaDConditionModel, ) from diffusers.utils import is_xformers_available, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism from ..pipeline_params import TEXT_TO_AUDIO_BATCH_PARAMS, TEXT_TO_AUDIO_PARAMS from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class lowercase ( UpperCAmelCase_,unittest.TestCase ): _a = AudioLDMPipeline _a = TEXT_TO_AUDIO_PARAMS _a = TEXT_TO_AUDIO_BATCH_PARAMS _a = frozenset( [ "num_inference_steps", "num_waveforms_per_prompt", "generator", "latents", "output_type", "return_dict", "callback", "callback_steps", ] ) def a__ ( self ) -> Any: torch.manual_seed(0 ) _A : Optional[int] = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""") , up_block_types=("""CrossAttnUpBlock2D""", """UpBlock2D""") , cross_attention_dim=(32, 64) , class_embed_type="""simple_projection""" , projection_class_embeddings_input_dim=32 , class_embeddings_concat=_lowercase , ) _A : int = DDIMScheduler( beta_start=0.00085 , beta_end=0.012 , beta_schedule="""scaled_linear""" , clip_sample=_lowercase , set_alpha_to_one=_lowercase , ) torch.manual_seed(0 ) _A : Optional[Any] = AutoencoderKL( block_out_channels=[32, 64] , in_channels=1 , out_channels=1 , down_block_types=["""DownEncoderBlock2D""", """DownEncoderBlock2D"""] , up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D"""] , latent_channels=4 , ) torch.manual_seed(0 ) _A : Tuple = ClapTextConfig( 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 , projection_dim=32 , ) _A : Dict = ClapTextModelWithProjection(_lowercase ) _A : Tuple = RobertaTokenizer.from_pretrained("""hf-internal-testing/tiny-random-roberta""" , model_max_length=77 ) _A : Union[str, Any] = SpeechTaHifiGanConfig( model_in_dim=8 , sampling_rate=1_6000 , upsample_initial_channel=16 , upsample_rates=[2, 2] , upsample_kernel_sizes=[4, 4] , resblock_kernel_sizes=[3, 7] , resblock_dilation_sizes=[[1, 3, 5], [1, 3, 5]] , normalize_before=_lowercase , ) _A : List[str] = SpeechTaHifiGan(_lowercase ) _A : int = { """unet""": unet, """scheduler""": scheduler, """vae""": vae, """text_encoder""": text_encoder, """tokenizer""": tokenizer, """vocoder""": vocoder, } return components def a__ ( self , _a , _a=0 ) -> str: if str(_lowercase ).startswith("""mps""" ): _A : int = torch.manual_seed(_lowercase ) else: _A : Union[str, Any] = torch.Generator(device=_lowercase ).manual_seed(_lowercase ) _A : Any = { """prompt""": """A hammer hitting a wooden surface""", """generator""": generator, """num_inference_steps""": 2, """guidance_scale""": 6.0, } return inputs def a__ ( self ) -> Tuple: _A : Tuple = """cpu""" # ensure determinism for the device-dependent torch.Generator _A : Tuple = self.get_dummy_components() _A : int = AudioLDMPipeline(**_lowercase ) _A : Optional[int] = audioldm_pipe.to(_lowercase ) audioldm_pipe.set_progress_bar_config(disable=_lowercase ) _A : Any = self.get_dummy_inputs(_lowercase ) _A : Any = audioldm_pipe(**_lowercase ) _A : List[str] = output.audios[0] assert audio.ndim == 1 assert len(_lowercase ) == 256 _A : List[str] = audio[:10] _A : List[Any] = np.array( [-0.0050, 0.0050, -0.0060, 0.0033, -0.0026, 0.0033, -0.0027, 0.0033, -0.0028, 0.0033] ) assert np.abs(audio_slice - expected_slice ).max() < 1e-2 def a__ ( self ) -> Any: _A : Tuple = self.get_dummy_components() _A : Tuple = AudioLDMPipeline(**_lowercase ) _A : List[Any] = audioldm_pipe.to(_lowercase ) _A : Optional[Any] = audioldm_pipe.to(_lowercase ) audioldm_pipe.set_progress_bar_config(disable=_lowercase ) _A : Optional[int] = self.get_dummy_inputs(_lowercase ) _A : str = 3 * [inputs["""prompt"""]] # forward _A : List[str] = audioldm_pipe(**_lowercase ) _A : Union[str, Any] = output.audios[0] _A : Any = self.get_dummy_inputs(_lowercase ) _A : Optional[int] = 3 * [inputs.pop("""prompt""" )] _A : int = audioldm_pipe.tokenizer( _lowercase , padding="""max_length""" , max_length=audioldm_pipe.tokenizer.model_max_length , truncation=_lowercase , return_tensors="""pt""" , ) _A : int = text_inputs["""input_ids"""].to(_lowercase ) _A : str = audioldm_pipe.text_encoder( _lowercase , ) _A : Dict = prompt_embeds.text_embeds # additional L_2 normalization over each hidden-state _A : List[str] = F.normalize(_lowercase , dim=-1 ) _A : str = prompt_embeds # forward _A : Tuple = audioldm_pipe(**_lowercase ) _A : str = output.audios[0] assert np.abs(audio_a - audio_a ).max() < 1e-2 def a__ ( self ) -> List[Any]: _A : Any = self.get_dummy_components() _A : int = AudioLDMPipeline(**_lowercase ) _A : List[str] = audioldm_pipe.to(_lowercase ) _A : Optional[int] = audioldm_pipe.to(_lowercase ) audioldm_pipe.set_progress_bar_config(disable=_lowercase ) _A : int = self.get_dummy_inputs(_lowercase ) _A : List[Any] = 3 * ["""this is a negative prompt"""] _A : Union[str, Any] = negative_prompt _A : List[str] = 3 * [inputs["""prompt"""]] # forward _A : Dict = audioldm_pipe(**_lowercase ) _A : str = output.audios[0] _A : List[str] = self.get_dummy_inputs(_lowercase ) _A : int = 3 * [inputs.pop("""prompt""" )] _A : List[str] = [] for p in [prompt, negative_prompt]: _A : List[Any] = audioldm_pipe.tokenizer( _lowercase , padding="""max_length""" , max_length=audioldm_pipe.tokenizer.model_max_length , truncation=_lowercase , return_tensors="""pt""" , ) _A : List[str] = text_inputs["""input_ids"""].to(_lowercase ) _A : Union[str, Any] = audioldm_pipe.text_encoder( _lowercase , ) _A : Optional[Any] = text_embeds.text_embeds # additional L_2 normalization over each hidden-state _A : Any = F.normalize(_lowercase , dim=-1 ) embeds.append(_lowercase ) _A , _A : Union[str, Any] = embeds # forward _A : List[str] = audioldm_pipe(**_lowercase ) _A : Optional[Any] = output.audios[0] assert np.abs(audio_a - audio_a ).max() < 1e-2 def a__ ( self ) -> Dict: _A : Dict = """cpu""" # ensure determinism for the device-dependent torch.Generator _A : List[Any] = self.get_dummy_components() _A : Dict = PNDMScheduler(skip_prk_steps=_lowercase ) _A : Optional[int] = AudioLDMPipeline(**_lowercase ) _A : str = audioldm_pipe.to(_lowercase ) audioldm_pipe.set_progress_bar_config(disable=_lowercase ) _A : List[Any] = self.get_dummy_inputs(_lowercase ) _A : Union[str, Any] = """egg cracking""" _A : List[str] = audioldm_pipe(**_lowercase , negative_prompt=_lowercase ) _A : Tuple = output.audios[0] assert audio.ndim == 1 assert len(_lowercase ) == 256 _A : List[str] = audio[:10] _A : Any = np.array( [-0.0051, 0.0050, -0.0060, 0.0034, -0.0026, 0.0033, -0.0027, 0.0033, -0.0028, 0.0032] ) assert np.abs(audio_slice - expected_slice ).max() < 1e-2 def a__ ( self ) -> Any: _A : Any = """cpu""" # ensure determinism for the device-dependent torch.Generator _A : int = self.get_dummy_components() _A : List[Any] = PNDMScheduler(skip_prk_steps=_lowercase ) _A : Dict = AudioLDMPipeline(**_lowercase ) _A : Dict = audioldm_pipe.to(_lowercase ) audioldm_pipe.set_progress_bar_config(disable=_lowercase ) _A : List[str] = """A hammer hitting a wooden surface""" # test num_waveforms_per_prompt=1 (default) _A : Tuple = audioldm_pipe(_lowercase , num_inference_steps=2 ).audios assert audios.shape == (1, 256) # test num_waveforms_per_prompt=1 (default) for batch of prompts _A : Dict = 2 _A : Dict = audioldm_pipe([prompt] * batch_size , num_inference_steps=2 ).audios assert audios.shape == (batch_size, 256) # test num_waveforms_per_prompt for single prompt _A : int = 2 _A : List[str] = audioldm_pipe(_lowercase , num_inference_steps=2 , num_waveforms_per_prompt=_lowercase ).audios assert audios.shape == (num_waveforms_per_prompt, 256) # test num_waveforms_per_prompt for batch of prompts _A : Any = 2 _A : List[Any] = audioldm_pipe( [prompt] * batch_size , num_inference_steps=2 , num_waveforms_per_prompt=_lowercase ).audios assert audios.shape == (batch_size * num_waveforms_per_prompt, 256) def a__ ( self ) -> str: _A : List[Any] = """cpu""" # ensure determinism for the device-dependent torch.Generator _A : Any = self.get_dummy_components() _A : str = AudioLDMPipeline(**_lowercase ) _A : Dict = audioldm_pipe.to(_lowercase ) audioldm_pipe.set_progress_bar_config(disable=_lowercase ) _A : Union[str, Any] = audioldm_pipe.vocoder.config.sampling_rate _A : Optional[int] = self.get_dummy_inputs(_lowercase ) _A : Union[str, Any] = audioldm_pipe(audio_length_in_s=0.016 , **_lowercase ) _A : List[str] = output.audios[0] assert audio.ndim == 1 assert len(_lowercase ) / vocoder_sampling_rate == 0.016 _A : Optional[Any] = audioldm_pipe(audio_length_in_s=0.032 , **_lowercase ) _A : str = output.audios[0] assert audio.ndim == 1 assert len(_lowercase ) / vocoder_sampling_rate == 0.032 def a__ ( self ) -> str: _A : Tuple = self.get_dummy_components() _A : Union[str, Any] = AudioLDMPipeline(**_lowercase ) _A : Any = audioldm_pipe.to(_lowercase ) audioldm_pipe.set_progress_bar_config(disable=_lowercase ) _A : Optional[Any] = ["""hey"""] _A : Optional[Any] = audioldm_pipe(_lowercase , num_inference_steps=1 ) _A : Union[str, Any] = output.audios.shape assert audio_shape == (1, 256) _A : List[str] = audioldm_pipe.vocoder.config config.model_in_dim *= 2 _A : int = SpeechTaHifiGan(_lowercase ).to(_lowercase ) _A : Any = audioldm_pipe(_lowercase , num_inference_steps=1 ) _A : List[str] = output.audios.shape # waveform shape is unchanged, we just have 2x the number of mel channels in the spectrogram assert audio_shape == (1, 256) def a__ ( self ) -> int: self._test_attention_slicing_forward_pass(test_mean_pixel_difference=_lowercase ) def a__ ( self ) -> int: self._test_inference_batch_single_identical(test_mean_pixel_difference=_lowercase ) @unittest.skipIf( torch_device != """cuda""" or not is_xformers_available() , reason="""XFormers attention is only available with CUDA and `xformers` installed""" , ) def a__ ( self ) -> Any: self._test_xformers_attention_forwardGenerator_pass(test_mean_pixel_difference=_lowercase ) @slow class lowercase ( unittest.TestCase ): def a__ ( self ) -> List[str]: super().tearDown() gc.collect() torch.cuda.empty_cache() def a__ ( self , _a , _a="cpu" , _a=torch.floataa , _a=0 ) -> Optional[Any]: _A : int = torch.Generator(device=_lowercase ).manual_seed(_lowercase ) _A : Optional[Any] = np.random.RandomState(_lowercase ).standard_normal((1, 8, 128, 16) ) _A : Tuple = torch.from_numpy(_lowercase ).to(device=_lowercase , dtype=_lowercase ) _A : int = { """prompt""": """A hammer hitting a wooden surface""", """latents""": latents, """generator""": generator, """num_inference_steps""": 3, """guidance_scale""": 2.5, } return inputs def a__ ( self ) -> int: _A : List[Any] = AudioLDMPipeline.from_pretrained("""cvssp/audioldm""" ) _A : List[str] = audioldm_pipe.to(_lowercase ) audioldm_pipe.set_progress_bar_config(disable=_lowercase ) _A : Union[str, Any] = self.get_inputs(_lowercase ) _A : Tuple = 25 _A : List[str] = audioldm_pipe(**_lowercase ).audios[0] assert audio.ndim == 1 assert len(_lowercase ) == 8_1920 _A : Any = audio[7_7230:7_7240] _A : Dict = np.array( [-0.4884, -0.4607, 0.0023, 0.5007, 0.5896, 0.5151, 0.3813, -0.0208, -0.3687, -0.4315] ) _A : str = np.abs(expected_slice - audio_slice ).max() assert max_diff < 1e-2 def a__ ( self ) -> int: _A : Dict = AudioLDMPipeline.from_pretrained("""cvssp/audioldm""" ) _A : Dict = LMSDiscreteScheduler.from_config(audioldm_pipe.scheduler.config ) _A : List[str] = audioldm_pipe.to(_lowercase ) audioldm_pipe.set_progress_bar_config(disable=_lowercase ) _A : List[Any] = self.get_inputs(_lowercase ) _A : Dict = audioldm_pipe(**_lowercase ).audios[0] assert audio.ndim == 1 assert len(_lowercase ) == 8_1920 _A : List[str] = audio[2_7780:2_7790] _A : List[Any] = np.array([-0.2131, -0.0873, -0.0124, -0.0189, 0.0569, 0.1373, 0.1883, 0.2886, 0.3297, 0.2212] ) _A : Optional[Any] = np.abs(expected_slice - audio_slice ).max() assert max_diff < 3e-2
702
import json import os import unittest from transformers import CLIPTokenizer, CLIPTokenizerFast from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES from transformers.testing_utils import require_ftfy, require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class lowercase ( UpperCamelCase__,unittest.TestCase ): _a = CLIPTokenizer _a = CLIPTokenizerFast _a = True _a = {} _a = False def a__ ( self ) -> Optional[Any]: super().setUp() # fmt: off _A : int = ["""l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """lo""", """l</w>""", """w</w>""", """r</w>""", """t</w>""", """low</w>""", """er</w>""", """lowest</w>""", """newer</w>""", """wider""", """<unk>""", """<|startoftext|>""", """<|endoftext|>"""] # fmt: on _A : str = dict(zip(_a , range(len(_a ) ) ) ) _A : Optional[int] = ["""#version: 0.2""", """l o""", """lo w</w>""", """e r</w>"""] _A : str = {"""unk_token""": """<unk>"""} _A : Union[str, Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""vocab_file"""] ) _A : Union[str, Any] = 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(_a ) + """\n""" ) with open(self.merges_file , """w""" , encoding="""utf-8""" ) as fp: fp.write("""\n""".join(_a ) ) def a__ ( self , **_a ) -> List[Any]: kwargs.update(self.special_tokens_map ) return CLIPTokenizer.from_pretrained(self.tmpdirname , **_a ) def a__ ( self , **_a ) -> Union[str, Any]: kwargs.update(self.special_tokens_map ) return CLIPTokenizerFast.from_pretrained(self.tmpdirname , **_a ) def a__ ( self , _a ) -> str: _A : Tuple = """lower newer""" _A : Optional[Any] = """lower newer""" return input_text, output_text def a__ ( self ) -> List[Any]: _A : Optional[int] = CLIPTokenizer(self.vocab_file , self.merges_file , **self.special_tokens_map ) _A : str = """lower newer""" _A : List[str] = ["""lo""", """w""", """er</w>""", """n""", """e""", """w""", """er</w>"""] _A : int = tokenizer.tokenize(_a ) self.assertListEqual(_a , _a ) _A : str = tokens + [tokenizer.unk_token] _A : Dict = [10, 2, 16, 9, 3, 2, 16, 20] self.assertListEqual(tokenizer.convert_tokens_to_ids(_a ) , _a ) @require_ftfy def a__ ( self ) -> Tuple: for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): _A : Dict = self.tokenizer_class.from_pretrained(_a , **_a ) _A : str = self.rust_tokenizer_class.from_pretrained(_a , **_a ) _A : List[str] = """A\n'll 11p223RF☆ho!!to?'d'd''d of a cat to-$''d.""" _A : Dict = tokenizer_s.tokenize(_a ) _A : Dict = tokenizer_r.tokenize(_a ) self.assertListEqual(_a , _a ) # Test that the tokenization is identical on an example containing a character (Latin Small Letter A # with Tilde) encoded in 2 different ways _A : Tuple = """xa\u0303y""" + """ """ + """x\xe3y""" _A : Dict = tokenizer_s.tokenize(_a ) _A : Dict = tokenizer_r.tokenize(_a ) self.assertListEqual(_a , _a ) # Test that the tokenization is identical on unicode of space type _A : Any = [ """\u0009""", # (horizontal tab, '\t') """\u000B""", # (vertical tab) """\u000C""", # (form feed) """\u0020""", # (space, ' ') """\u200E""", # (left-to-right mark):w """\u200F""", # (right-to-left mark) ] for unicode_seq in spaces_unicodes: _A : Optional[int] = tokenizer_s.tokenize(_a ) _A : List[Any] = tokenizer_r.tokenize(_a ) self.assertListEqual(_a , _a ) # Test that the tokenization is identical on unicode of line break type _A : int = [ """\u000A""", # (line feed, '\n') """\r\n""", # (carriage return and line feed, '\r\n') """\u000D""", # (carriage return, '\r') """\r""", # (carriage return, '\r') """\u000D""", # (carriage return, '\r') """\u2028""", # (line separator) """\u2029""", # (paragraph separator) # "\u0085", # (next line) ] # The tokenization is not identical for the character "\u0085" (next line). The slow version using ftfy transforms # it into the Horizontal Ellipsis character "…" ("\u2026") while the fast version transforms it into a # space (and thus into an empty list). for unicode_seq in line_break_unicodes: _A : Optional[Any] = tokenizer_s.tokenize(_a ) _A : Tuple = tokenizer_r.tokenize(_a ) self.assertListEqual(_a , _a ) def a__ ( self ) -> Any: # Test which aims to verify that the offsets are well adapted to the argument `add_prefix_space` for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): _A : List[Any] = """hello""" # `hello` is a token in the vocabulary of `pretrained_name` _A : str = F'''{text_of_1_token} {text_of_1_token}''' _A : str = self.rust_tokenizer_class.from_pretrained( _a , use_fast=_a , ) _A : Dict = tokenizer_r(_a , return_offsets_mapping=_a , add_special_tokens=_a ) self.assertEqual(encoding.offset_mapping[0] , (0, len(_a )) ) self.assertEqual( encoding.offset_mapping[1] , (len(_a ) + 1, len(_a ) + 1 + len(_a )) , ) _A : Any = F''' {text}''' _A : List[Any] = self.rust_tokenizer_class.from_pretrained( _a , use_fast=_a , ) _A : Optional[int] = tokenizer_r(_a , return_offsets_mapping=_a , add_special_tokens=_a ) self.assertEqual(encoding.offset_mapping[0] , (1, 1 + len(_a )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(_a ) + 1, 1 + len(_a ) + 1 + len(_a )) , ) def a__ ( self ) -> List[Any]: # Test related to the breaking change introduced in transformers v4.17.0 # We need to check that an error in raised when the user try to load a previous version of the tokenizer. with self.assertRaises(_a ) as context: self.rust_tokenizer_class.from_pretrained("""robot-test/old-clip-tokenizer""" ) self.assertTrue( context.exception.args[0].startswith( """The `backend_tokenizer` provided does not match the expected format.""" ) ) @require_ftfy def a__ ( self ) -> str: super().test_tokenization_python_rust_equals() def a__ ( self ) -> Union[str, Any]: # CLIP always lower cases letters pass
54
0
import unittest from transformers import MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING, AutoTokenizer, is_vision_available from transformers.pipelines import pipeline from transformers.pipelines.document_question_answering import apply_tesseract from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_detectrona, require_pytesseract, require_tf, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_vision_available(): from PIL import Image from transformers.image_utils import load_image else: class lowercase : @staticmethod def a__ ( *_a , **_a ) -> Any: pass def lowerCAmelCase_ ( snake_case_ ): return None # This is a pinned image from a specific revision of a document question answering space, hosted by HuggingFace, # so we can expect it to be available. _snake_case = ( "https://huggingface.co/spaces/impira/docquery/resolve/2f6c96314dc84dfda62d40de9da55f2f5165d403/invoice.png" ) @is_pipeline_test @require_torch @require_vision class lowercase ( unittest.TestCase ): _a = MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING @require_pytesseract @require_vision def a__ ( self , _a , _a , _a ) -> Union[str, Any]: _A : str = pipeline( """document-question-answering""" , model=lowercase_ , tokenizer=lowercase_ , image_processor=lowercase_ ) _A : Tuple = INVOICE_URL _A : List[Any] = list(zip(*apply_tesseract(load_image(lowercase_ ) , lowercase_ , """""" ) ) ) _A : Union[str, Any] = "What is the placebo?" _A : int = [ { "image": load_image(lowercase_ ), "question": question, }, { "image": image, "question": question, }, { "image": image, "question": question, "word_boxes": word_boxes, }, ] return dqa_pipeline, examples def a__ ( self , _a , _a ) -> Optional[int]: _A : Any = dqa_pipeline(lowercase_ , top_k=2 ) self.assertEqual( lowercase_ , [ [ {"""score""": ANY(lowercase_ ), """answer""": ANY(lowercase_ ), """start""": ANY(lowercase_ ), """end""": ANY(lowercase_ )}, {"""score""": ANY(lowercase_ ), """answer""": ANY(lowercase_ ), """start""": ANY(lowercase_ ), """end""": ANY(lowercase_ )}, ] ] * 3 , ) @require_torch @require_detectrona @require_pytesseract def a__ ( self ) -> Optional[int]: _A : Tuple = pipeline("""document-question-answering""" , model="""hf-internal-testing/tiny-random-layoutlmv2""" ) _A : Optional[int] = INVOICE_URL _A : Union[str, Any] = "How many cats are there?" _A : Union[str, Any] = [ {"score": 0.0001, "answer": "oy 2312/2019", "start": 38, "end": 39}, {"score": 0.0001, "answer": "oy 2312/2019 DUE", "start": 38, "end": 40}, ] _A : Union[str, Any] = dqa_pipeline(image=lowercase_ , question=lowercase_ , top_k=2 ) self.assertEqual(nested_simplify(lowercase_ , decimals=4 ) , lowercase_ ) _A : Tuple = dqa_pipeline({"""image""": image, """question""": question} , top_k=2 ) self.assertEqual(nested_simplify(lowercase_ , decimals=4 ) , lowercase_ ) # This image does not detect ANY text in it, meaning layoutlmv2 should fail. # Empty answer probably _A : str = "./tests/fixtures/tests_samples/COCO/000000039769.png" _A : List[str] = dqa_pipeline(image=lowercase_ , question=lowercase_ , top_k=2 ) self.assertEqual(lowercase_ , [] ) # We can optionnally pass directly the words and bounding boxes _A : List[str] = "./tests/fixtures/tests_samples/COCO/000000039769.png" _A : Any = [] _A : str = [] _A : int = dqa_pipeline(image=lowercase_ , question=lowercase_ , words=lowercase_ , boxes=lowercase_ , top_k=2 ) self.assertEqual(lowercase_ , [] ) @slow @require_torch @require_detectrona @require_pytesseract def a__ ( self ) -> Optional[Any]: _A : str = pipeline( """document-question-answering""" , model="""tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa""" , revision="""9977165""" , ) _A : str = INVOICE_URL _A : List[str] = "What is the invoice number?" _A : Optional[Any] = dqa_pipeline(image=lowercase_ , question=lowercase_ , top_k=2 ) self.assertEqual( nested_simplify(lowercase_ , decimals=4 ) , [ {"""score""": 0.9944, """answer""": """us-001""", """start""": 16, """end""": 16}, {"""score""": 0.0009, """answer""": """us-001""", """start""": 16, """end""": 16}, ] , ) _A : Optional[int] = dqa_pipeline({"""image""": image, """question""": question} , top_k=2 ) self.assertEqual( nested_simplify(lowercase_ , decimals=4 ) , [ {"""score""": 0.9944, """answer""": """us-001""", """start""": 16, """end""": 16}, {"""score""": 0.0009, """answer""": """us-001""", """start""": 16, """end""": 16}, ] , ) _A : Any = dqa_pipeline( [{"""image""": image, """question""": question}, {"""image""": image, """question""": question}] , top_k=2 ) self.assertEqual( nested_simplify(lowercase_ , decimals=4 ) , [ [ {"""score""": 0.9944, """answer""": """us-001""", """start""": 16, """end""": 16}, {"""score""": 0.0009, """answer""": """us-001""", """start""": 16, """end""": 16}, ], ] * 2 , ) @slow @require_torch @require_detectrona @require_pytesseract def a__ ( self ) -> Dict: _A : Tuple = pipeline( """document-question-answering""" , model="""tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa""" , revision="""9977165""" , max_seq_len=50 , ) _A : Optional[Any] = INVOICE_URL _A : str = "What is the invoice number?" _A : Dict = dqa_pipeline(image=lowercase_ , question=lowercase_ , top_k=2 ) self.assertEqual( nested_simplify(lowercase_ , decimals=4 ) , [ {"""score""": 0.9974, """answer""": """1110212019""", """start""": 23, """end""": 23}, {"""score""": 0.9948, """answer""": """us-001""", """start""": 16, """end""": 16}, ] , ) _A : List[str] = dqa_pipeline({"""image""": image, """question""": question} , top_k=2 ) self.assertEqual( nested_simplify(lowercase_ , decimals=4 ) , [ {"""score""": 0.9974, """answer""": """1110212019""", """start""": 23, """end""": 23}, {"""score""": 0.9948, """answer""": """us-001""", """start""": 16, """end""": 16}, ] , ) _A : Dict = dqa_pipeline( [{"""image""": image, """question""": question}, {"""image""": image, """question""": question}] , top_k=2 ) self.assertEqual( nested_simplify(lowercase_ , decimals=4 ) , [ [ {"""score""": 0.9974, """answer""": """1110212019""", """start""": 23, """end""": 23}, {"""score""": 0.9948, """answer""": """us-001""", """start""": 16, """end""": 16}, ] ] * 2 , ) @slow @require_torch @require_pytesseract @require_vision def a__ ( self ) -> str: _A : Optional[int] = AutoTokenizer.from_pretrained( """impira/layoutlm-document-qa""" , revision="""3dc6de3""" , add_prefix_space=lowercase_ ) _A : Optional[int] = pipeline( """document-question-answering""" , model="""impira/layoutlm-document-qa""" , tokenizer=lowercase_ , revision="""3dc6de3""" , ) _A : Optional[Any] = INVOICE_URL _A : Tuple = "What is the invoice number?" _A : Any = dqa_pipeline(image=lowercase_ , question=lowercase_ , top_k=2 ) self.assertEqual( nested_simplify(lowercase_ , decimals=4 ) , [ {"""score""": 0.4251, """answer""": """us-001""", """start""": 16, """end""": 16}, {"""score""": 0.0819, """answer""": """1110212019""", """start""": 23, """end""": 23}, ] , ) _A : Optional[Any] = dqa_pipeline({"""image""": image, """question""": question} , top_k=2 ) self.assertEqual( nested_simplify(lowercase_ , decimals=4 ) , [ {"""score""": 0.4251, """answer""": """us-001""", """start""": 16, """end""": 16}, {"""score""": 0.0819, """answer""": """1110212019""", """start""": 23, """end""": 23}, ] , ) _A : int = dqa_pipeline( [{"""image""": image, """question""": question}, {"""image""": image, """question""": question}] , top_k=2 ) self.assertEqual( nested_simplify(lowercase_ , decimals=4 ) , [ [ {"""score""": 0.4251, """answer""": """us-001""", """start""": 16, """end""": 16}, {"""score""": 0.0819, """answer""": """1110212019""", """start""": 23, """end""": 23}, ] ] * 2 , ) _A : int = list(zip(*apply_tesseract(load_image(lowercase_ ) , lowercase_ , """""" ) ) ) # This model should also work if `image` is set to None _A : Optional[int] = dqa_pipeline({"""image""": None, """word_boxes""": word_boxes, """question""": question} , top_k=2 ) self.assertEqual( nested_simplify(lowercase_ , decimals=4 ) , [ {"""score""": 0.4251, """answer""": """us-001""", """start""": 16, """end""": 16}, {"""score""": 0.0819, """answer""": """1110212019""", """start""": 23, """end""": 23}, ] , ) @slow @require_torch @require_pytesseract @require_vision def a__ ( self ) -> List[str]: _A : Dict = AutoTokenizer.from_pretrained( """impira/layoutlm-document-qa""" , revision="""3dc6de3""" , add_prefix_space=lowercase_ ) _A : str = pipeline( """document-question-answering""" , model="""impira/layoutlm-document-qa""" , tokenizer=lowercase_ , revision="""3dc6de3""" , max_seq_len=50 , ) _A : Union[str, Any] = INVOICE_URL _A : Optional[int] = "What is the invoice number?" _A : str = dqa_pipeline(image=lowercase_ , question=lowercase_ , top_k=2 ) self.assertEqual( nested_simplify(lowercase_ , decimals=4 ) , [ {"""score""": 0.9999, """answer""": """us-001""", """start""": 16, """end""": 16}, {"""score""": 0.9998, """answer""": """us-001""", """start""": 16, """end""": 16}, ] , ) _A : Any = dqa_pipeline( [{"""image""": image, """question""": question}, {"""image""": image, """question""": question}] , top_k=2 ) self.assertEqual( nested_simplify(lowercase_ , decimals=4 ) , [ [ {"""score""": 0.9999, """answer""": """us-001""", """start""": 16, """end""": 16}, {"""score""": 0.9998, """answer""": """us-001""", """start""": 16, """end""": 16}, ] ] * 2 , ) _A : Tuple = list(zip(*apply_tesseract(load_image(lowercase_ ) , lowercase_ , """""" ) ) ) # This model should also work if `image` is set to None _A : Any = dqa_pipeline({"""image""": None, """word_boxes""": word_boxes, """question""": question} , top_k=2 ) self.assertEqual( nested_simplify(lowercase_ , decimals=4 ) , [ {"""score""": 0.9999, """answer""": """us-001""", """start""": 16, """end""": 16}, {"""score""": 0.9998, """answer""": """us-001""", """start""": 16, """end""": 16}, ] , ) @slow @require_torch def a__ ( self ) -> str: _A : Dict = pipeline( """document-question-answering""" , model="""naver-clova-ix/donut-base-finetuned-docvqa""" , tokenizer=AutoTokenizer.from_pretrained("""naver-clova-ix/donut-base-finetuned-docvqa""" ) , feature_extractor="""naver-clova-ix/donut-base-finetuned-docvqa""" , ) _A : Tuple = INVOICE_URL _A : Any = "What is the invoice number?" _A : int = dqa_pipeline(image=lowercase_ , question=lowercase_ , top_k=2 ) self.assertEqual(nested_simplify(lowercase_ , decimals=4 ) , [{"""answer""": """us-001"""}] ) @require_tf @unittest.skip("""Document question answering not implemented in TF""" ) def a__ ( self ) -> List[Any]: pass
703
from datetime import datetime as dt import os from github import Github _snake_case = [ "good first issue", "good second issue", "good difficult issue", "feature request", "new model", "wip", ] def lowerCAmelCase_ ( ): _A : int = Github(os.environ["""GITHUB_TOKEN"""] ) _A : Tuple = g.get_repo("""huggingface/transformers""" ) _A : Dict = repo.get_issues(state="""open""" ) for issue in open_issues: _A : Optional[Any] = sorted([comment for comment in issue.get_comments()],key=lambda snake_case_ : i.created_at,reverse=snake_case_ ) _A : Dict = comments[0] if len(snake_case_ ) > 0 else None if ( last_comment is not None and last_comment.user.login == "github-actions[bot]" and (dt.utcnow() - issue.updated_at).days > 7 and (dt.utcnow() - issue.created_at).days >= 30 and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels() ) ): # print(f"Would close issue {issue.number} since it has been 7 days of inactivity since bot mention.") issue.edit(state="""closed""" ) elif ( (dt.utcnow() - issue.updated_at).days > 23 and (dt.utcnow() - issue.created_at).days >= 30 and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels() ) ): # print(f"Would add stale comment to {issue.number}") issue.create_comment( """This issue has been automatically marked as stale because it has not had """ """recent activity. If you think this still needs to be addressed """ """please comment on this thread.\n\nPlease note that issues that do not follow the """ """[contributing guidelines](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) """ """are likely to be ignored.""" ) if __name__ == "__main__": main()
54
0
def lowerCAmelCase_ ( snake_case_ ): _A : Tuple = int(snake_case_ ) if decimal in (0, 1): # Exit cases for the recursion return str(snake_case_ ) _A , _A : int = divmod(snake_case_,2 ) return binary_recursive(snake_case_ ) + str(snake_case_ ) def lowerCAmelCase_ ( snake_case_ ): _A : Optional[int] = str(snake_case_ ).strip() if not number: raise ValueError("""No input value was provided""" ) _A : Optional[Any] = """-""" if number.startswith("""-""" ) else """""" _A : str = number.lstrip("""-""" ) if not number.isnumeric(): raise ValueError("""Input value is not an integer""" ) return f'''{negative}0b{binary_recursive(int(snake_case_ ) )}''' if __name__ == "__main__": from doctest import testmod testmod()
704
from __future__ import annotations class lowercase : def __init__( self , _a = 0 ) -> str: _A : Any = key def a__ ( self , _a , _a ) -> list[str]: assert isinstance(_a , _a ) and isinstance(_a , _a ) _A : Any = key or self.__key or 1 # make sure key is an appropriate size key %= 255 return [chr(ord(_a ) ^ key ) for ch in content] def a__ ( self , _a , _a ) -> list[str]: assert isinstance(_a , _a ) and isinstance(_a , _a ) _A : List[Any] = key or self.__key or 1 # make sure key is an appropriate size key %= 255 return [chr(ord(_a ) ^ key ) for ch in content] def a__ ( self , _a , _a = 0 ) -> str: assert isinstance(_a , _a ) and isinstance(_a , _a ) _A : List[Any] = key or self.__key or 1 # make sure key can be any size while key > 255: key -= 255 # This will be returned _A : List[str] = """""" for ch in content: ans += chr(ord(_a ) ^ key ) return ans def a__ ( self , _a , _a = 0 ) -> str: assert isinstance(_a , _a ) and isinstance(_a , _a ) _A : List[str] = key or self.__key or 1 # make sure key can be any size while key > 255: key -= 255 # This will be returned _A : List[str] = """""" for ch in content: ans += chr(ord(_a ) ^ key ) return ans def a__ ( self , _a , _a = 0 ) -> bool: assert isinstance(_a , _a ) and isinstance(_a , _a ) try: with open(_a ) as fin, open("""encrypt.out""" , """w+""" ) as fout: # actual encrypt-process for line in fin: fout.write(self.encrypt_string(_a , _a ) ) except OSError: return False return True def a__ ( self , _a , _a ) -> bool: assert isinstance(_a , _a ) and isinstance(_a , _a ) try: with open(_a ) as fin, open("""decrypt.out""" , """w+""" ) as fout: # actual encrypt-process for line in fin: fout.write(self.decrypt_string(_a , _a ) ) except OSError: return False return True # Tests # crypt = XORCipher() # key = 67 # # test encrypt # print(crypt.encrypt("hallo welt",key)) # # test decrypt # print(crypt.decrypt(crypt.encrypt("hallo welt",key), key)) # # test encrypt_string # print(crypt.encrypt_string("hallo welt",key)) # # test decrypt_string # print(crypt.decrypt_string(crypt.encrypt_string("hallo welt",key),key)) # if (crypt.encrypt_file("test.txt",key)): # print("encrypt successful") # else: # print("encrypt unsuccessful") # if (crypt.decrypt_file("encrypt.out",key)): # print("decrypt successful") # else: # print("decrypt unsuccessful")
54
0
from typing import List, Union from ..utils import ( add_end_docstrings, is_tf_available, is_torch_available, is_vision_available, logging, requires_backends, ) from .base import PIPELINE_INIT_ARGS, Pipeline if is_vision_available(): from PIL import Image from ..image_utils import load_image if is_tf_available(): import tensorflow as tf from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING from ..tf_utils import stable_softmax if is_torch_available(): from ..models.auto.modeling_auto import MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING _snake_case = logging.get_logger(__name__) @add_end_docstrings(__lowercase ) class lowercase ( __lowercase ): def __init__( self , *_a , **_a ) -> int: super().__init__(*_a , **_a ) requires_backends(self , """vision""" ) self.check_model_type( TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING if self.framework == """tf""" else MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING ) def a__ ( self , _a=None ) -> Dict: _A : Dict = {} if top_k is not None: _A : Optional[int] = top_k return {}, {}, postprocess_params def __call__( self , _a , **_a ) -> Dict: return super().__call__(_a , **_a ) def a__ ( self , _a ) -> Union[str, Any]: _A : int = load_image(_a ) _A : str = self.image_processor(images=_a , return_tensors=self.framework ) return model_inputs def a__ ( self , _a ) -> Dict: _A : Any = self.model(**_a ) return model_outputs def a__ ( self , _a , _a=5 ) -> Union[str, Any]: if top_k > self.model.config.num_labels: _A : Dict = self.model.config.num_labels if self.framework == "pt": _A : Tuple = model_outputs.logits.softmax(-1 )[0] _A : int = probs.topk(_a ) elif self.framework == "tf": _A : Optional[int] = stable_softmax(model_outputs.logits , axis=-1 )[0] _A : Optional[Any] = tf.math.top_k(_a , k=_a ) _A : Any = topk.values.numpy(), topk.indices.numpy() else: raise ValueError(F'''Unsupported framework: {self.framework}''' ) _A : int = scores.tolist() _A : Optional[int] = ids.tolist() return [{"score": score, "label": self.model.config.idalabel[_id]} for score, _id in zip(_a , _a )]
705
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 = random.Random() def lowerCAmelCase_ ( snake_case_,snake_case_=1.0,snake_case_=None,snake_case_=None ): if rng is None: _A : str = global_rng _A : List[Any] = [] 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 lowercase ( unittest.TestCase ): def __init__( self , _a , _a=7 , _a=400 , _a=2000 , _a=10 , _a=160 , _a=8 , _a=0.0 , _a=4000 , _a=False , _a=True , ) -> Optional[int]: _A : Any = parent _A : List[Any] = batch_size _A : List[Any] = min_seq_length _A : Dict = max_seq_length _A : Optional[Any] = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) _A : Tuple = padding_value _A : Tuple = sampling_rate _A : str = return_attention_mask _A : Any = do_normalize _A : Union[str, Any] = feature_size _A : List[Any] = chunk_length _A : List[Any] = hop_length def a__ ( self ) -> List[str]: 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 a__ ( self , _a=False , _a=False ) -> List[str]: def _flatten(_a ): return list(itertools.chain(*_a ) ) 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 : int = [ 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 : Any = [np.asarray(_a ) for x in speech_inputs] return speech_inputs @require_torch @require_torchaudio class lowercase ( UpperCamelCase__,unittest.TestCase ): _a = WhisperFeatureExtractor if is_speech_available() else None def a__ ( self ) -> Tuple: _A : Optional[int] = WhisperFeatureExtractionTester(self ) def a__ ( self ) -> Optional[Any]: _A : List[Any] = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: _A : List[str] = feat_extract_first.save_pretrained(_a )[0] check_json_file_has_correct_format(_a ) _A : Optional[int] = self.feature_extraction_class.from_pretrained(_a ) _A : Tuple = feat_extract_first.to_dict() _A : List[Any] = feat_extract_second.to_dict() _A : List[Any] = feat_extract_first.mel_filters _A : Union[str, Any] = feat_extract_second.mel_filters self.assertTrue(np.allclose(_a , _a ) ) self.assertEqual(_a , _a ) def a__ ( self ) -> Dict: _A : Any = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: _A : Dict = os.path.join(_a , """feat_extract.json""" ) feat_extract_first.to_json_file(_a ) _A : Optional[int] = self.feature_extraction_class.from_json_file(_a ) _A : str = feat_extract_first.to_dict() _A : Any = feat_extract_second.to_dict() _A : Union[str, Any] = feat_extract_first.mel_filters _A : Union[str, Any] = feat_extract_second.mel_filters self.assertTrue(np.allclose(_a , _a ) ) self.assertEqual(_a , _a ) def a__ ( self ) -> Union[str, Any]: # Tests that all call wrap to encode_plus and batch_encode_plus _A : Optional[int] = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 _A : Optional[Any] = [floats_list((1, x) )[0] for x in range(800 , 1400 , 200 )] _A : Any = [np.asarray(_a ) for speech_input in speech_inputs] # Test feature size _A : Dict = feature_extractor(_a , 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 : List[Any] = feature_extractor(speech_inputs[0] , return_tensors="""np""" ).input_features _A : List[str] = feature_extractor(np_speech_inputs[0] , return_tensors="""np""" ).input_features self.assertTrue(np.allclose(_a , _a , atol=1e-3 ) ) # Test batched _A : Union[str, Any] = feature_extractor(_a , return_tensors="""np""" ).input_features _A : Tuple = feature_extractor(_a , return_tensors="""np""" ).input_features for enc_seq_a, enc_seq_a in zip(_a , _a ): self.assertTrue(np.allclose(_a , _a , atol=1e-3 ) ) # Test 2-D numpy arrays are batched. _A : List[str] = [floats_list((1, x) )[0] for x in (800, 800, 800)] _A : Any = np.asarray(_a ) _A : Union[str, Any] = feature_extractor(_a , return_tensors="""np""" ).input_features _A : int = feature_extractor(_a , return_tensors="""np""" ).input_features for enc_seq_a, enc_seq_a in zip(_a , _a ): self.assertTrue(np.allclose(_a , _a , atol=1e-3 ) ) # Test truncation required _A : List[Any] = [floats_list((1, x) )[0] for x in range(200 , (feature_extractor.n_samples + 500) , 200 )] _A : Union[str, Any] = [np.asarray(_a ) for speech_input in speech_inputs] _A : Tuple = [x[: feature_extractor.n_samples] for x in speech_inputs] _A : Union[str, Any] = [np.asarray(_a ) for speech_input in speech_inputs_truncated] _A : Optional[int] = feature_extractor(_a , return_tensors="""np""" ).input_features _A : List[Any] = feature_extractor(_a , return_tensors="""np""" ).input_features for enc_seq_a, enc_seq_a in zip(_a , _a ): self.assertTrue(np.allclose(_a , _a , atol=1e-3 ) ) def a__ ( self ) -> Dict: import torch _A : Dict = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) _A : Optional[int] = np.random.rand(100 , 32 ).astype(np.floataa ) _A : str = np_speech_inputs.tolist() for inputs in [py_speech_inputs, np_speech_inputs]: _A : Optional[Any] = feature_extractor.pad([{"""input_features""": inputs}] , return_tensors="""np""" ) self.assertTrue(np_processed.input_features.dtype == np.floataa ) _A : Optional[int] = feature_extractor.pad([{"""input_features""": inputs}] , return_tensors="""pt""" ) self.assertTrue(pt_processed.input_features.dtype == torch.floataa ) def a__ ( self , _a ) -> Dict: _A : Optional[Any] = load_dataset("""hf-internal-testing/librispeech_asr_dummy""" , """clean""" , split="""validation""" ) # automatic decoding with librispeech _A : Optional[Any] = ds.sort("""id""" ).select(range(_a ) )[:num_samples]["""audio"""] return [x["array"] for x in speech_samples] def a__ ( self ) -> Tuple: # fmt: off _A : Dict = torch.tensor( [ 0.1193, -0.0946, -0.1098, -0.0196, 0.0225, -0.0690, -0.1736, 0.0951, 0.0971, -0.0817, -0.0702, 0.0162, 0.0260, 0.0017, -0.0192, -0.1678, 0.0709, -0.1867, -0.0655, -0.0274, -0.0234, -0.1884, -0.0516, -0.0554, -0.0274, -0.1425, -0.1423, 0.0837, 0.0377, -0.0854 ] ) # fmt: on _A : Dict = self._load_datasamples(1 ) _A : Optional[Any] = WhisperFeatureExtractor() _A : Optional[Any] = feature_extractor(_a , return_tensors="""pt""" ).input_features self.assertEqual(input_features.shape , (1, 80, 3000) ) self.assertTrue(torch.allclose(input_features[0, 0, :30] , _a , atol=1e-4 ) ) def a__ ( self ) -> str: _A : Dict = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) _A : str = self._load_datasamples(1 )[0] _A : Union[str, Any] = ((audio - audio.min()) / (audio.max() - audio.min())) * 6_5535 # Rescale to [0, 65535] to show issue _A : List[Any] = feat_extract.zero_mean_unit_var_norm([audio] , attention_mask=_a )[0] self.assertTrue(np.all(np.mean(_a ) < 1e-3 ) ) self.assertTrue(np.all(np.abs(np.var(_a ) - 1 ) < 1e-3 ) )
54
0
import argparse import json import os import fairseq import torch from fairseq.data import Dictionary from transformers import ( WavaVecaConformerConfig, WavaVecaConformerForCTC, WavaVecaConformerForPreTraining, WavaVecaCTCTokenizer, WavaVecaFeatureExtractor, WavaVecaProcessor, logging, ) logging.set_verbosity_info() _snake_case = logging.get_logger(__name__) _snake_case = { "post_extract_proj": "feature_projection.projection", "encoder.pos_conv.0": "encoder.pos_conv_embed.conv", "self_attn.linear_k": "encoder.layers.*.self_attn.linear_k", "self_attn.linear_v": "encoder.layers.*.self_attn.linear_v", "self_attn.linear_q": "encoder.layers.*.self_attn.linear_q", "self_attn.pos_bias_u": "encoder.layers.*.self_attn.pos_bias_u", "self_attn.pos_bias_v": "encoder.layers.*.self_attn.pos_bias_v", "self_attn.linear_out": "encoder.layers.*.self_attn.linear_out", "self_attn.linear_pos": "encoder.layers.*.self_attn.linear_pos", "self_attn.rotary_emb": "encoder.embed_positions", "self_attn_layer_norm": "encoder.layers.*.self_attn_layer_norm", "conv_module.pointwise_conv1": "encoder.layers.*.conv_module.pointwise_conv1", "conv_module.pointwise_conv2": "encoder.layers.*.conv_module.pointwise_conv2", "conv_module.depthwise_conv": "encoder.layers.*.conv_module.depthwise_conv", "conv_module.batch_norm": "encoder.layers.*.conv_module.batch_norm", "conv_module.layer_norm": "encoder.layers.*.conv_module.layer_norm", "ffn1.w_1": "encoder.layers.*.ffn1.intermediate_dense", "ffn1.w_2": "encoder.layers.*.ffn1.output_dense", "ffn1.layer_norm": "encoder.layers.*.ffn1_layer_norm", "ffn2.w_1": "encoder.layers.*.ffn2.intermediate_dense", "ffn2.w_2": "encoder.layers.*.ffn2.output_dense", "ffn2.layer_norm": "encoder.layers.*.ffn2_layer_norm", "final_layer_norm": "encoder.layers.*.final_layer_norm", "encoder.layer_norm": "encoder.layer_norm", "w2v_model.layer_norm": "feature_projection.layer_norm", "quantizer.weight_proj": "quantizer.weight_proj", "quantizer.vars": "quantizer.codevectors", "project_q": "project_q", "final_proj": "project_hid", "w2v_encoder.proj": "lm_head", "mask_emb": "masked_spec_embed", } _snake_case = [ "lm_head", "quantizer.weight_proj", "quantizer.codevectors", "project_q", "project_hid", ] def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_,snake_case_,snake_case_ ): for attribute in key.split(""".""" ): _A : Any = getattr(_SCREAMING_SNAKE_CASE,_SCREAMING_SNAKE_CASE ) if weight_type is not None: _A : str = getattr(_SCREAMING_SNAKE_CASE,_SCREAMING_SNAKE_CASE ).shape else: _A : Any = hf_pointer.shape if hf_shape != value.shape: raise ValueError( f'''Shape of hf {key + "." + weight_type if weight_type is not None else ""} is {hf_shape}, but should be''' f''' {value.shape} for {full_name}''' ) if weight_type == "weight": _A : Tuple = value elif weight_type == "weight_g": _A : Any = value elif weight_type == "weight_v": _A : Any = value elif weight_type == "bias": _A : List[Any] = value elif weight_type == "running_mean": _A : Any = value elif weight_type == "running_var": _A : str = value elif weight_type == "num_batches_tracked": _A : List[str] = value elif weight_type == "inv_freq": _A : Any = value else: _A : List[str] = value logger.info(f'''{key + "." + weight_type if weight_type is not None else ""} was initialized from {full_name}.''' ) def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_ ): _A : List[Any] = [] _A : Dict = fairseq_model.state_dict() _A : Dict = hf_model.wavaveca_conformer.feature_extractor for name, value in fairseq_dict.items(): _A : List[Any] = 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""",) _A : Tuple = True else: for key, mapped_key in MAPPING.items(): _A : Dict = """wav2vec2_conformer.""" + mapped_key if mapped_key not in TOP_LEVEL_KEYS else mapped_key if key in name or key.split("""w2v_model.""" )[-1] == name.split(""".""" )[0]: _A : List[str] = True if "*" in mapped_key: _A : Dict = name.split(_SCREAMING_SNAKE_CASE )[0].split(""".""" )[-2] _A : Optional[int] = mapped_key.replace("""*""",_SCREAMING_SNAKE_CASE ) if "pos_bias_u" in name: _A : Tuple = None elif "pos_bias_v" in name: _A : List[Any] = None elif "weight_g" in name: _A : Union[str, Any] = """weight_g""" elif "weight_v" in name: _A : str = """weight_v""" elif "bias" in name: _A : Any = """bias""" elif "weight" in name: # TODO: don't match quantizer.weight_proj _A : Union[str, Any] = """weight""" elif "running_mean" in name: _A : List[str] = """running_mean""" elif "inv_freq" in name: _A : List[Any] = """inv_freq""" elif "running_var" in name: _A : Dict = """running_var""" elif "num_batches_tracked" in name: _A : Any = """num_batches_tracked""" else: _A : List[Any] = 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}''' ) def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_,snake_case_,snake_case_ ): _A : Optional[int] = full_name.split("""conv_layers.""" )[-1] _A : Tuple = name.split(""".""" ) _A : Dict = int(items[0] ) _A : List[str] = int(items[1] ) if type_id == 0: if "bias" in name: if value.shape != feature_extractor.conv_layers[layer_id].conv.bias.data.shape: raise ValueError( f'''{full_name} has size {value.shape}, but''' f''' {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found.''' ) _A : Optional[Any] = value logger.info(f'''Feat extract conv layer {layer_id} was initialized from {full_name}.''' ) elif "weight" in name: if value.shape != feature_extractor.conv_layers[layer_id].conv.weight.data.shape: raise ValueError( f'''{full_name} has size {value.shape}, but''' f''' {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found.''' ) _A : List[str] = value logger.info(f'''Feat extract conv layer {layer_id} was initialized from {full_name}.''' ) elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm): if "bias" in name: if value.shape != feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape: raise ValueError( f'''{full_name} has size {value.shape}, but''' f''' {feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape} was found.''' ) _A : str = value logger.info(f'''Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.''' ) elif "weight" in name: if value.shape != feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape: raise ValueError( f'''{full_name} has size {value.shape}, but''' f''' {feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape} was found.''' ) _A : List[Any] = 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 ) @torch.no_grad() def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_=None,snake_case_=None,snake_case_=True ): if config_path is not None: _A : Optional[int] = WavaVecaConformerConfig.from_pretrained(_SCREAMING_SNAKE_CASE,hidden_act="""swish""" ) else: _A : List[str] = WavaVecaConformerConfig() if "rope" in checkpoint_path: _A : List[Any] = """rotary""" if is_finetuned: if dict_path: _A : Tuple = Dictionary.load(_SCREAMING_SNAKE_CASE ) # important change bos & pad token id since CTC symbol is <pad> and # not <s> as in fairseq _A : Optional[int] = target_dict.pad_index _A : Any = target_dict.bos_index _A : List[str] = target_dict.eos_index _A : Tuple = len(target_dict.symbols ) _A : Any = os.path.join(_SCREAMING_SNAKE_CASE,"""vocab.json""" ) if not os.path.isdir(_SCREAMING_SNAKE_CASE ): logger.error("""--pytorch_dump_folder_path ({}) should be a directory""".format(_SCREAMING_SNAKE_CASE ) ) return os.makedirs(_SCREAMING_SNAKE_CASE,exist_ok=_SCREAMING_SNAKE_CASE ) _A : Tuple = target_dict.indices # fairseq has the <pad> and <s> switched _A : str = 0 _A : List[str] = 1 with open(_SCREAMING_SNAKE_CASE,"""w""",encoding="""utf-8""" ) as vocab_handle: json.dump(_SCREAMING_SNAKE_CASE,_SCREAMING_SNAKE_CASE ) _A : str = WavaVecaCTCTokenizer( _SCREAMING_SNAKE_CASE,unk_token=target_dict.unk_word,pad_token=target_dict.pad_word,bos_token=target_dict.bos_word,eos_token=target_dict.eos_word,word_delimiter_token="""|""",do_lower_case=_SCREAMING_SNAKE_CASE,) _A : List[str] = True if config.feat_extract_norm == """layer""" else False _A : List[str] = WavaVecaFeatureExtractor( feature_size=1,sampling_rate=16000,padding_value=0,do_normalize=_SCREAMING_SNAKE_CASE,return_attention_mask=_SCREAMING_SNAKE_CASE,) _A : Union[str, Any] = WavaVecaProcessor(feature_extractor=_SCREAMING_SNAKE_CASE,tokenizer=_SCREAMING_SNAKE_CASE ) processor.save_pretrained(_SCREAMING_SNAKE_CASE ) _A : int = WavaVecaConformerForCTC(_SCREAMING_SNAKE_CASE ) else: _A : Tuple = WavaVecaConformerForPreTraining(_SCREAMING_SNAKE_CASE ) if is_finetuned: _A , _A , _A : str = fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path],arg_overrides={"""data""": """/""".join(dict_path.split("""/""" )[:-1] )} ) else: _A : List[Any] = argparse.Namespace(task="""audio_pretraining""" ) _A : Optional[Any] = fairseq.tasks.setup_task(_SCREAMING_SNAKE_CASE ) _A , _A , _A : str = fairseq.checkpoint_utils.load_model_ensemble_and_task([checkpoint_path],task=_SCREAMING_SNAKE_CASE ) _A : Optional[int] = model[0].eval() recursively_load_weights(_SCREAMING_SNAKE_CASE,_SCREAMING_SNAKE_CASE,not is_finetuned ) hf_wavavec.save_pretrained(_SCREAMING_SNAKE_CASE ) if __name__ == "__main__": _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("--dict_path", default=None, type=str, help="Path to dict of fine-tuned model") parser.add_argument("--config_path", default=None, type=str, help="Path to hf config.json of model to convert") parser.add_argument( "--not_finetuned", action="store_true", help="Whether the model to convert is a fine-tuned model or not" ) _snake_case = parser.parse_args() convert_wavaveca_conformer_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.dict_path, not args.not_finetuned )
706
def lowerCAmelCase_ ( snake_case_,snake_case_ ): _A : Union[str, Any] = """""" for i in table: res += inp[i - 1] return res def lowerCAmelCase_ ( snake_case_ ): return data[1:] + data[0] def lowerCAmelCase_ ( snake_case_,snake_case_ ): _A : Dict = """""" for i in range(len(snake_case_ ) ): if a[i] == b[i]: res += "0" else: res += "1" return res def lowerCAmelCase_ ( snake_case_,snake_case_ ): _A : int = int("""0b""" + data[0] + data[-1],2 ) _A : Any = int("""0b""" + data[1:3],2 ) return bin(s[row][col] )[2:] def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_,snake_case_,snake_case_ ): _A : List[str] = message[:4] _A : List[Any] = message[4:] _A : Union[str, Any] = apply_table(snake_case_,snake_case_ ) _A : List[Any] = xor(snake_case_,snake_case_ ) _A : Optional[Any] = apply_sbox(snake_case_,temp[:4] ) # noqa: E741 _A : List[Any] = apply_sbox(snake_case_,temp[4:] ) _A : int = """0""" * (2 - len(snake_case_ )) + l # noqa: E741 _A : Union[str, Any] = """0""" * (2 - len(snake_case_ )) + r _A : List[Any] = apply_table(l + r,snake_case_ ) _A : Any = xor(snake_case_,snake_case_ ) return temp + right if __name__ == "__main__": _snake_case = input("Enter 10 bit key: ") _snake_case = input("Enter 8 bit message: ") _snake_case = [6, 3, 7, 4, 8, 5, 10, 9] _snake_case = [3, 5, 2, 7, 4, 10, 1, 9, 8, 6] _snake_case = [2, 4, 3, 1] _snake_case = [2, 6, 3, 1, 4, 8, 5, 7] _snake_case = [4, 1, 3, 5, 7, 2, 8, 6] _snake_case = [4, 1, 2, 3, 2, 3, 4, 1] _snake_case = [[1, 0, 3, 2], [3, 2, 1, 0], [0, 2, 1, 3], [3, 1, 3, 2]] _snake_case = [[0, 1, 2, 3], [2, 0, 1, 3], [3, 0, 1, 0], [2, 1, 0, 3]] # key generation _snake_case = apply_table(key, paa_table) _snake_case = temp[:5] _snake_case = temp[5:] _snake_case = left_shift(left) _snake_case = left_shift(right) _snake_case = apply_table(left + right, pa_table) _snake_case = left_shift(left) _snake_case = left_shift(right) _snake_case = left_shift(left) _snake_case = left_shift(right) _snake_case = apply_table(left + right, pa_table) # encryption _snake_case = apply_table(message, IP) _snake_case = function(expansion, sa, sa, keya, temp) _snake_case = temp[4:] + temp[:4] _snake_case = function(expansion, sa, sa, keya, temp) _snake_case = apply_table(temp, IP_inv) print("Cipher text is:", CT) # decryption _snake_case = apply_table(CT, IP) _snake_case = function(expansion, sa, sa, keya, temp) _snake_case = temp[4:] + temp[:4] _snake_case = function(expansion, sa, sa, keya, temp) _snake_case = apply_table(temp, IP_inv) print("Plain text after decypting is:", PT)
54
0
import argparse import gc import json import os import shutil import warnings import torch from transformers import LlamaConfig, LlamaForCausalLM, LlamaTokenizer try: from transformers import LlamaTokenizerFast except ImportError as e: warnings.warn(e) warnings.warn( "The converted tokenizer will be the `slow` tokenizer. To use the fast, update your `tokenizers` library and re-run the tokenizer conversion" ) _snake_case = None _snake_case = { "7B": 11008, "13B": 13824, "30B": 17920, "65B": 22016, "70B": 28672, } _snake_case = { "7B": 1, "7Bf": 1, "13B": 2, "13Bf": 2, "30B": 4, "65B": 8, "70B": 8, "70Bf": 8, } def lowerCAmelCase_ ( snake_case_,snake_case_=1,snake_case_=256 ): return multiple_of * ((int(ffn_dim_multiplier * int(8 * n / 3 ) ) + multiple_of - 1) // multiple_of) def lowerCAmelCase_ ( snake_case_ ): with open(snake_case_,"""r""" ) as f: return json.load(snake_case_ ) def lowerCAmelCase_ ( snake_case_,snake_case_ ): with open(snake_case_,"""w""" ) as f: json.dump(snake_case_,snake_case_ ) def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_,snake_case_=True ): os.makedirs(snake_case_,exist_ok=snake_case_ ) _A : Tuple = os.path.join(snake_case_,"""tmp""" ) os.makedirs(snake_case_,exist_ok=snake_case_ ) _A : Tuple = read_json(os.path.join(snake_case_,"""params.json""" ) ) _A : Union[str, Any] = NUM_SHARDS[model_size] _A : str = params["""n_layers"""] _A : Any = params["""n_heads"""] _A : List[str] = n_heads // num_shards _A : Optional[int] = params["""dim"""] _A : Optional[Any] = dim // n_heads _A : Any = 10000.0 _A : Any = 1.0 / (base ** (torch.arange(0,snake_case_,2 ).float() / dims_per_head)) if "n_kv_heads" in params: _A : Optional[Any] = params["""n_kv_heads"""] # for GQA / MQA _A : Union[str, Any] = n_heads_per_shard // num_key_value_heads _A : int = dim // num_key_value_heads else: # compatibility with other checkpoints _A : List[Any] = n_heads _A : List[str] = n_heads_per_shard _A : Optional[Any] = dim # permute for sliced rotary def permute(snake_case_,snake_case_=n_heads,snake_case_=dim,snake_case_=dim ): return w.view(snake_case_,dima // n_heads // 2,2,snake_case_ ).transpose(1,2 ).reshape(snake_case_,snake_case_ ) print(f'''Fetching all parameters from the checkpoint at {input_base_path}.''' ) # Load weights if model_size == "7B": # Not sharded # (The sharded implementation would also work, but this is simpler.) _A : str = torch.load(os.path.join(snake_case_,"""consolidated.00.pth""" ),map_location="""cpu""" ) else: # Sharded _A : int = [ torch.load(os.path.join(snake_case_,f'''consolidated.{i:02d}.pth''' ),map_location="""cpu""" ) for i in range(snake_case_ ) ] _A : Optional[int] = 0 _A : int = {"""weight_map""": {}} for layer_i in range(snake_case_ ): _A : List[Any] = f'''pytorch_model-{layer_i + 1}-of-{n_layers + 1}.bin''' if model_size == "7B": # Unsharded _A : str = { f'''model.layers.{layer_i}.self_attn.q_proj.weight''': permute( loaded[f'''layers.{layer_i}.attention.wq.weight'''] ), f'''model.layers.{layer_i}.self_attn.k_proj.weight''': permute( loaded[f'''layers.{layer_i}.attention.wk.weight'''] ), f'''model.layers.{layer_i}.self_attn.v_proj.weight''': loaded[f'''layers.{layer_i}.attention.wv.weight'''], f'''model.layers.{layer_i}.self_attn.o_proj.weight''': loaded[f'''layers.{layer_i}.attention.wo.weight'''], f'''model.layers.{layer_i}.mlp.gate_proj.weight''': loaded[f'''layers.{layer_i}.feed_forward.w1.weight'''], f'''model.layers.{layer_i}.mlp.down_proj.weight''': loaded[f'''layers.{layer_i}.feed_forward.w2.weight'''], f'''model.layers.{layer_i}.mlp.up_proj.weight''': loaded[f'''layers.{layer_i}.feed_forward.w3.weight'''], f'''model.layers.{layer_i}.input_layernorm.weight''': loaded[f'''layers.{layer_i}.attention_norm.weight'''], f'''model.layers.{layer_i}.post_attention_layernorm.weight''': loaded[f'''layers.{layer_i}.ffn_norm.weight'''], } else: # Sharded # Note that attention.w{q,k,v,o}, feed_fordward.w[1,2,3], attention_norm.weight and ffn_norm.weight share # the same storage object, saving attention_norm and ffn_norm will save other weights too, which is # redundant as other weights will be stitched from multiple shards. To avoid that, they are cloned. _A : str = { f'''model.layers.{layer_i}.input_layernorm.weight''': loaded[0][ f'''layers.{layer_i}.attention_norm.weight''' ].clone(), f'''model.layers.{layer_i}.post_attention_layernorm.weight''': loaded[0][ f'''layers.{layer_i}.ffn_norm.weight''' ].clone(), } _A : Union[str, Any] = permute( torch.cat( [ loaded[i][f'''layers.{layer_i}.attention.wq.weight'''].view(snake_case_,snake_case_,snake_case_ ) for i in range(snake_case_ ) ],dim=0,).reshape(snake_case_,snake_case_ ) ) _A : Any = permute( torch.cat( [ loaded[i][f'''layers.{layer_i}.attention.wk.weight'''].view( snake_case_,snake_case_,snake_case_ ) for i in range(snake_case_ ) ],dim=0,).reshape(snake_case_,snake_case_ ),snake_case_,snake_case_,snake_case_,) _A : Dict = torch.cat( [ loaded[i][f'''layers.{layer_i}.attention.wv.weight'''].view( snake_case_,snake_case_,snake_case_ ) for i in range(snake_case_ ) ],dim=0,).reshape(snake_case_,snake_case_ ) _A : Any = torch.cat( [loaded[i][f'''layers.{layer_i}.attention.wo.weight'''] for i in range(snake_case_ )],dim=1 ) _A : int = torch.cat( [loaded[i][f'''layers.{layer_i}.feed_forward.w1.weight'''] for i in range(snake_case_ )],dim=0 ) _A : int = torch.cat( [loaded[i][f'''layers.{layer_i}.feed_forward.w2.weight'''] for i in range(snake_case_ )],dim=1 ) _A : Union[str, Any] = torch.cat( [loaded[i][f'''layers.{layer_i}.feed_forward.w3.weight'''] for i in range(snake_case_ )],dim=0 ) _A : List[str] = inv_freq for k, v in state_dict.items(): _A : List[Any] = filename param_count += v.numel() torch.save(snake_case_,os.path.join(snake_case_,snake_case_ ) ) _A : List[str] = f'''pytorch_model-{n_layers + 1}-of-{n_layers + 1}.bin''' if model_size == "7B": # Unsharded _A : str = { """model.embed_tokens.weight""": loaded["""tok_embeddings.weight"""], """model.norm.weight""": loaded["""norm.weight"""], """lm_head.weight""": loaded["""output.weight"""], } else: _A : Optional[Any] = { """model.norm.weight""": loaded[0]["""norm.weight"""], """model.embed_tokens.weight""": torch.cat( [loaded[i]["""tok_embeddings.weight"""] for i in range(snake_case_ )],dim=1 ), """lm_head.weight""": torch.cat([loaded[i]["""output.weight"""] for i in range(snake_case_ )],dim=0 ), } for k, v in state_dict.items(): _A : Optional[int] = filename param_count += v.numel() torch.save(snake_case_,os.path.join(snake_case_,snake_case_ ) ) # Write configs _A : Any = {"""total_size""": param_count * 2} write_json(snake_case_,os.path.join(snake_case_,"""pytorch_model.bin.index.json""" ) ) _A : List[Any] = params["""ffn_dim_multiplier"""] if """ffn_dim_multiplier""" in params else 1 _A : int = params["""multiple_of"""] if """multiple_of""" in params else 256 _A : Union[str, Any] = LlamaConfig( hidden_size=snake_case_,intermediate_size=compute_intermediate_size(snake_case_,snake_case_,snake_case_ ),num_attention_heads=params["""n_heads"""],num_hidden_layers=params["""n_layers"""],rms_norm_eps=params["""norm_eps"""],num_key_value_heads=snake_case_,) config.save_pretrained(snake_case_ ) # Make space so we can load the model properly now. del state_dict del loaded gc.collect() print("""Loading the checkpoint in a Llama model.""" ) _A : Optional[int] = LlamaForCausalLM.from_pretrained(snake_case_,torch_dtype=torch.floataa,low_cpu_mem_usage=snake_case_ ) # Avoid saving this as part of the config. del model.config._name_or_path print("""Saving in the Transformers format.""" ) model.save_pretrained(snake_case_,safe_serialization=snake_case_ ) shutil.rmtree(snake_case_ ) def lowerCAmelCase_ ( snake_case_,snake_case_ ): _A : Optional[int] = LlamaTokenizer if LlamaTokenizerFast is None else LlamaTokenizerFast print(f'''Saving a {tokenizer_class.__name__} to {tokenizer_path}.''' ) _A : List[Any] = tokenizer_class(snake_case_ ) tokenizer.save_pretrained(snake_case_ ) def lowerCAmelCase_ ( ): _A : int = argparse.ArgumentParser() parser.add_argument( """--input_dir""",help="""Location of LLaMA weights, which contains tokenizer.model and model folders""",) parser.add_argument( """--model_size""",choices=["""7B""", """7Bf""", """13B""", """13Bf""", """30B""", """65B""", """70B""", """70Bf""", """tokenizer_only"""],) parser.add_argument( """--output_dir""",help="""Location to write HF model and tokenizer""",) parser.add_argument("""--safe_serialization""",type=snake_case_,help="""Whether or not to save using `safetensors`.""" ) _A : Union[str, Any] = parser.parse_args() if args.model_size != "tokenizer_only": write_model( model_path=args.output_dir,input_base_path=os.path.join(args.input_dir,args.model_size ),model_size=args.model_size,safe_serialization=args.safe_serialization,) _A : Dict = os.path.join(args.input_dir,"""tokenizer.model""" ) write_tokenizer(args.output_dir,snake_case_ ) if __name__ == "__main__": main()
707
import unittest import numpy as np from transformers.file_utils import is_torch_available, is_vision_available from transformers.testing_utils import require_torch, require_vision 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 DPTImageProcessor class lowercase ( unittest.TestCase ): def __init__( self , _a , _a=7 , _a=3 , _a=18 , _a=30 , _a=400 , _a=True , _a=None , _a=True , _a=[0.5, 0.5, 0.5] , _a=[0.5, 0.5, 0.5] , ) -> Tuple: _A : Any = size if size is not None else {"""height""": 18, """width""": 18} _A : Optional[Any] = parent _A : Union[str, Any] = batch_size _A : List[Any] = num_channels _A : List[str] = image_size _A : Optional[Any] = min_resolution _A : List[Any] = max_resolution _A : Optional[Any] = do_resize _A : str = size _A : List[str] = do_normalize _A : Dict = image_mean _A : int = image_std def a__ ( self ) -> Any: 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 ( UpperCamelCase__,unittest.TestCase ): _a = DPTImageProcessor if is_vision_available() else None def a__ ( self ) -> Optional[int]: _A : Optional[Any] = DPTImageProcessingTester(self ) @property def a__ ( self ) -> Tuple: return self.image_processor_tester.prepare_image_processor_dict() def a__ ( self ) -> Any: _A : List[str] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_a , """image_mean""" ) ) self.assertTrue(hasattr(_a , """image_std""" ) ) self.assertTrue(hasattr(_a , """do_normalize""" ) ) self.assertTrue(hasattr(_a , """do_resize""" ) ) self.assertTrue(hasattr(_a , """size""" ) ) def a__ ( self ) -> Any: _A : List[str] = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"""height""": 18, """width""": 18} ) _A : str = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {"""height""": 42, """width""": 42} ) def a__ ( self ) -> List[Any]: # Initialize image_processing _A : Optional[int] = self.image_processing_class(**self.image_processor_dict ) # create random PIL images _A : Any = prepare_image_inputs(self.image_processor_tester , equal_resolution=_a ) for image in image_inputs: self.assertIsInstance(_a , Image.Image ) # Test not batched input _A : Union[str, 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 : int = image_processing(_a , 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 a__ ( self ) -> Union[str, Any]: # Initialize image_processing _A : List[Any] = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors _A : Optional[Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=_a , numpify=_a ) for image in image_inputs: self.assertIsInstance(_a , np.ndarray ) # Test not batched input _A : Dict = 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(_a , 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 a__ ( self ) -> List[str]: # Initialize image_processing _A : Dict = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors _A : str = prepare_image_inputs(self.image_processor_tester , equal_resolution=_a , torchify=_a ) for image in image_inputs: self.assertIsInstance(_a , torch.Tensor ) # Test not batched input _A : Optional[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 : int = image_processing(_a , 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"""], ) , )
54
0
import string from math import logaa def lowerCAmelCase_ ( snake_case_,snake_case_ ): _A : Tuple = document.translate( str.maketrans("""""","""""",string.punctuation ) ).replace("""\n""","""""" ) _A : int = document_without_punctuation.split(""" """ ) # word tokenization return len([word for word in tokenize_document if word.lower() == term.lower()] ) def lowerCAmelCase_ ( snake_case_,snake_case_ ): _A : Dict = corpus.lower().translate( str.maketrans("""""","""""",string.punctuation ) ) # strip all punctuation and replace it with '' _A : Any = corpus_without_punctuation.split("""\n""" ) _A : Any = term.lower() return (len([doc for doc in docs if term in doc] ), len(lowerCAmelCase__ )) def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_=False ): if smoothing: if n == 0: raise ValueError("""log10(0) is undefined.""" ) return round(1 + logaa(n / (1 + df) ),3 ) if df == 0: raise ZeroDivisionError("""df must be > 0""" ) elif n == 0: raise ValueError("""log10(0) is undefined.""" ) return round(logaa(n / df ),3 ) def lowerCAmelCase_ ( snake_case_,snake_case_ ): return round(tf * idf,3 )
708
from typing import Any, Dict, List, Optional, Tuple, Union import torch from torch import nn from torch.utils.data import DistributedSampler, RandomSampler from transformers import PreTrainedModel, Trainer, logging from transformers.integrations import is_fairscale_available from transformers.models.fsmt.configuration_fsmt import FSMTConfig from transformers.optimization import ( Adafactor, AdamW, get_constant_schedule, get_constant_schedule_with_warmup, get_cosine_schedule_with_warmup, get_cosine_with_hard_restarts_schedule_with_warmup, get_linear_schedule_with_warmup, get_polynomial_decay_schedule_with_warmup, ) from transformers.trainer_pt_utils import get_tpu_sampler from transformers.training_args import ParallelMode from transformers.utils import is_torch_tpu_available if is_fairscale_available(): from fairscale.optim import OSS _snake_case = logging.get_logger(__name__) _snake_case = { "linear": get_linear_schedule_with_warmup, "cosine": get_cosine_schedule_with_warmup, "cosine_w_restarts": get_cosine_with_hard_restarts_schedule_with_warmup, "polynomial": get_polynomial_decay_schedule_with_warmup, "constant": get_constant_schedule, "constant_w_warmup": get_constant_schedule_with_warmup, } class lowercase ( UpperCamelCase__ ): def __init__( self , _a=None , _a=None , *_a , **_a ) -> Optional[int]: super().__init__(*_a , **_a ) if config is None: assert isinstance(self.model , _a ), ( "If no `config` is passed the model to be trained has to be of type `PreTrainedModel`, but is" F''' {self.model.__class__}''' ) _A : Optional[Any] = self.model.config else: _A : int = config _A : Optional[Any] = data_args _A : int = self.config.tgt_vocab_size if isinstance(self.config , _a ) else self.config.vocab_size if self.args.label_smoothing != 0 or (self.data_args is not None and self.data_args.ignore_pad_token_for_loss): assert self.config.pad_token_id is not None, ( "Make sure that `config.pad_token_id` is correcly defined when ignoring `pad_token` for loss" " calculation or doing label smoothing." ) if self.config.pad_token_id is None and self.config.eos_token_id is not None: logger.warning( F'''The `config.pad_token_id` is `None`. Using `config.eos_token_id` = {self.config.eos_token_id} for''' """ padding..""" ) if self.args.label_smoothing == 0: _A : Optional[Any] = torch.nn.CrossEntropyLoss(ignore_index=self.config.pad_token_id ) else: # dynamically import label_smoothed_nll_loss from utils import label_smoothed_nll_loss _A : Union[str, Any] = label_smoothed_nll_loss def a__ ( self , _a ) -> int: if self.optimizer is None: _A : List[str] = ["""bias""", """LayerNorm.weight"""] _A : str = [ { """params""": [p for n, p in self.model.named_parameters() if not any(nd in n for nd in no_decay )], """weight_decay""": self.args.weight_decay, }, { """params""": [p for n, p in self.model.named_parameters() if any(nd in n for nd in no_decay )], """weight_decay""": 0.0, }, ] _A : Optional[Any] = Adafactor if self.args.adafactor else AdamW if self.args.adafactor: _A : Dict = Adafactor _A : int = {"""scale_parameter""": False, """relative_step""": False} else: _A : int = AdamW _A : Any = { """betas""": (self.args.adam_betaa, self.args.adam_betaa), """eps""": self.args.adam_epsilon, } _A : List[str] = self.args.learning_rate if self.sharded_ddp: _A : List[str] = OSS( params=_a , optim=_a , **_a , ) else: _A : Tuple = optimizer_cls(_a , **_a ) if self.lr_scheduler is None: _A : Union[str, Any] = self._get_lr_scheduler(_a ) else: # ignoring --lr_scheduler logger.warning("""scheduler is passed to `Seq2SeqTrainer`, `--lr_scheduler` arg is ignored.""" ) def a__ ( self , _a ) -> Dict: _A : List[Any] = arg_to_scheduler[self.args.lr_scheduler] if self.args.lr_scheduler == "constant": _A : Optional[Any] = schedule_func(self.optimizer ) elif self.args.lr_scheduler == "constant_w_warmup": _A : Optional[int] = schedule_func(self.optimizer , num_warmup_steps=self.args.warmup_steps ) else: _A : List[Any] = schedule_func( self.optimizer , num_warmup_steps=self.args.warmup_steps , num_training_steps=_a ) return scheduler def a__ ( self ) -> Optional[torch.utils.data.Sampler]: if isinstance(self.train_dataset , torch.utils.data.IterableDataset ): return None elif is_torch_tpu_available(): return get_tpu_sampler(self.train_dataset ) else: if self.args.sortish_sampler: self.train_dataset.make_sortish_sampler( self.args.per_device_train_batch_size , distributed=(self.args.parallel_mode == ParallelMode.DISTRIBUTED) , ) return ( RandomSampler(self.train_dataset ) if self.args.local_rank == -1 else DistributedSampler(self.train_dataset ) ) def a__ ( self , _a , _a , _a ) -> List[str]: if self.args.label_smoothing == 0: if self.data_args is not None and self.data_args.ignore_pad_token_for_loss: # force training to ignore pad token _A : List[str] = model(**_a , use_cache=_a )[0] _A : Dict = self.loss_fn(logits.view(-1 , logits.shape[-1] ) , labels.view(-1 ) ) else: # compute usual loss via models _A , _A : str = model(**_a , labels=_a , use_cache=_a )[:2] else: # compute label smoothed loss _A : Any = model(**_a , use_cache=_a )[0] _A : Union[str, Any] = torch.nn.functional.log_softmax(_a , dim=-1 ) _A , _A : List[str] = self.loss_fn(_a , _a , self.args.label_smoothing , ignore_index=self.config.pad_token_id ) return loss, logits def a__ ( self , _a , _a ) -> List[Any]: _A : Optional[int] = inputs.pop("""labels""" ) _A , _A : Dict = self._compute_loss(_a , _a , _a ) return loss def a__ ( self , _a , _a , _a , _a = None , ) -> Tuple[Optional[float], Optional[torch.Tensor], Optional[torch.Tensor]]: _A : int = self._prepare_inputs(_a ) _A : Dict = { """max_length""": self.data_args.val_max_target_length if self.data_args is not None else self.config.max_length, """num_beams""": self.data_args.eval_beams if self.data_args is not None else self.config.num_beams, } if self.args.predict_with_generate and not self.args.prediction_loss_only: _A : List[str] = self.model.generate( inputs["""input_ids"""] , attention_mask=inputs["""attention_mask"""] , **_a , ) # in case the batch is shorter than max length, the output should be padded if generated_tokens.shape[-1] < gen_kwargs["max_length"]: _A : str = self._pad_tensors_to_max_len(_a , gen_kwargs["""max_length"""] ) _A : Any = inputs.pop("""labels""" ) with torch.no_grad(): # compute loss on predict data _A , _A : Tuple = self._compute_loss(_a , _a , _a ) _A : Any = loss.mean().detach() if self.args.prediction_loss_only: return (loss, None, None) _A : List[Any] = generated_tokens if self.args.predict_with_generate else logits if labels.shape[-1] < gen_kwargs["max_length"]: _A : Any = self._pad_tensors_to_max_len(_a , gen_kwargs["""max_length"""] ) return (loss, logits, labels) def a__ ( self , _a , _a ) -> Union[str, Any]: # If PAD token is not defined at least EOS token has to be defined _A : Optional[Any] = self.config.pad_token_id if self.config.pad_token_id is not None else self.config.eos_token_id if pad_token_id is None: raise ValueError( """Make sure that either `config.pad_token_id` or `config.eos_token_id` is defined if tensor has to be""" F''' padded to `max_length`={max_length}''' ) _A : Dict = pad_token_id * torch.ones( (tensor.shape[0], max_length) , dtype=tensor.dtype , device=tensor.device ) _A : Dict = tensor return padded_tensor
54
0
from __future__ import annotations import time from math import sqrt # 1 for manhattan, 0 for euclidean _snake_case = 0 _snake_case = [ [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], ] _snake_case = [[-1, 0], [0, -1], [1, 0], [0, 1]] # up, left, down, right _snake_case = tuple[int, int] class lowercase : def __init__( self , _a , _a , _a , _a , _a , _a , ) -> None: _A : Optional[int] = pos_x _A : str = pos_y _A : List[str] = (pos_y, pos_x) _A : Optional[Any] = goal_x _A : Any = goal_y _A : Optional[Any] = g_cost _A : int = parent _A : List[str] = self.calculate_heuristic() _A : Optional[int] = self.g_cost + self.h_cost def a__ ( self ) -> float: _A : List[str] = self.pos_x - self.goal_x _A : Tuple = self.pos_y - self.goal_y if HEURISTIC == 1: return abs(lowerCamelCase_ ) + abs(lowerCamelCase_ ) else: return sqrt(dy**2 + dx**2 ) def __lt__( self , _a ) -> bool: return self.f_cost < other.f_cost class lowercase : def __init__( self , _a , _a ) -> Union[str, Any]: _A : str = Node(start[1] , start[0] , goal[1] , goal[0] , 0 , lowerCamelCase_ ) _A : Dict = Node(goal[1] , goal[0] , goal[1] , goal[0] , 9_9999 , lowerCamelCase_ ) _A : Optional[Any] = [self.start] _A : Optional[Any] = [] _A : str = False def a__ ( self ) -> list[TPosition]: while self.open_nodes: # Open Nodes are sorted using __lt__ self.open_nodes.sort() _A : Optional[int] = self.open_nodes.pop(0 ) if current_node.pos == self.target.pos: return self.retrace_path(lowerCamelCase_ ) self.closed_nodes.append(lowerCamelCase_ ) _A : Union[str, Any] = 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 _A : Dict = 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 a__ ( self , _a ) -> list[Node]: _A : Union[str, Any] = [] for action in delta: _A : Any = parent.pos_x + action[1] _A : Dict = 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 a__ ( self , _a ) -> list[TPosition]: _A : Any = node _A : Optional[Any] = [] while current_node is not None: path.append((current_node.pos_y, current_node.pos_x) ) _A : Optional[int] = current_node.parent path.reverse() return path class lowercase : def __init__( self , _a , _a ) -> None: _A : Dict = AStar(lowerCamelCase_ , lowerCamelCase_ ) _A : Tuple = AStar(lowerCamelCase_ , lowerCamelCase_ ) _A : List[Any] = False def a__ ( self ) -> list[TPosition]: while self.fwd_astar.open_nodes or self.bwd_astar.open_nodes: self.fwd_astar.open_nodes.sort() self.bwd_astar.open_nodes.sort() _A : Optional[Any] = self.fwd_astar.open_nodes.pop(0 ) _A : List[str] = self.bwd_astar.open_nodes.pop(0 ) if current_bwd_node.pos == current_fwd_node.pos: return self.retrace_bidirectional_path( lowerCamelCase_ , lowerCamelCase_ ) self.fwd_astar.closed_nodes.append(lowerCamelCase_ ) self.bwd_astar.closed_nodes.append(lowerCamelCase_ ) _A : Optional[Any] = current_bwd_node _A : Optional[int] = current_fwd_node _A : Optional[int] = { 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 _A : Optional[Any] = 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 a__ ( self , _a , _a ) -> list[TPosition]: _A : Optional[int] = self.fwd_astar.retrace_path(lowerCamelCase_ ) _A : Dict = self.bwd_astar.retrace_path(lowerCamelCase_ ) bwd_path.pop() bwd_path.reverse() _A : int = fwd_path + bwd_path return path if __name__ == "__main__": # all coordinates are given in format [y,x] _snake_case = (0, 0) _snake_case = (len(grid) - 1, len(grid[0]) - 1) for elem in grid: print(elem) _snake_case = time.time() _snake_case = AStar(init, goal) _snake_case = a_star.search() _snake_case = time.time() - start_time print(f"""AStar execution time = {end_time:f} seconds""") _snake_case = time.time() _snake_case = BidirectionalAStar(init, goal) _snake_case = time.time() - bd_start_time print(f"""BidirectionalAStar execution time = {bd_end_time:f} seconds""")
709
from collections.abc import Callable from math import pi, sqrt from random import uniform from statistics import mean def lowerCAmelCase_ ( snake_case_ ): # A local function to see if a dot lands in the circle. def is_in_circle(snake_case_,snake_case_ ) -> bool: _A : List[str] = sqrt((x**2) + (y**2) ) # Our circle has a radius of 1, so a distance # greater than 1 would land outside the circle. return distance_from_centre <= 1 # The proportion of guesses that landed in the circle _A : Optional[int] = mean( int(is_in_circle(uniform(-1.0,1.0 ),uniform(-1.0,1.0 ) ) ) for _ in range(snake_case_ ) ) # The ratio of the area for circle to square is pi/4. _A : List[str] = proportion * 4 print(f'''The estimated value of pi is {pi_estimate}''' ) print(f'''The numpy value of pi is {pi}''' ) print(f'''The total error is {abs(pi - pi_estimate )}''' ) def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_ = 0.0,snake_case_ = 1.0,): return mean( function_to_integrate(uniform(snake_case_,snake_case_ ) ) for _ in range(snake_case_ ) ) * (max_value - min_value) def lowerCAmelCase_ ( snake_case_,snake_case_ = 0.0,snake_case_ = 1.0 ): def identity_function(snake_case_ ) -> float: return x _A : Any = area_under_curve_estimator( snake_case_,snake_case_,snake_case_,snake_case_ ) _A : Tuple = (max_value * max_value - min_value * min_value) / 2 print("""******************""" ) print(f'''Estimating area under y=x where x varies from {min_value} to {max_value}''' ) print(f'''Estimated value is {estimated_value}''' ) print(f'''Expected value is {expected_value}''' ) print(f'''Total error is {abs(estimated_value - expected_value )}''' ) print("""******************""" ) def lowerCAmelCase_ ( snake_case_ ): def function_to_integrate(snake_case_ ) -> float: return sqrt(4.0 - x * x ) _A : Optional[int] = area_under_curve_estimator( snake_case_,snake_case_,0.0,2.0 ) print("""******************""" ) print("""Estimating pi using area_under_curve_estimator""" ) print(f'''Estimated value is {estimated_value}''' ) print(f'''Expected value is {pi}''' ) print(f'''Total error is {abs(estimated_value - pi )}''' ) print("""******************""" ) if __name__ == "__main__": import doctest doctest.testmod()
54
0
def lowerCAmelCase_ ( snake_case_ ): _A : Any = False while is_sorted is False: # Until all the indices are traversed keep looping _A : Any = True for i in range(0,len(snake_case_ ) - 1,2 ): # iterating over all even indices if input_list[i] > input_list[i + 1]: _A , _A : str = input_list[i + 1], input_list[i] # swapping if elements not in order _A : Optional[int] = False for i in range(1,len(snake_case_ ) - 1,2 ): # iterating over all odd indices if input_list[i] > input_list[i + 1]: _A , _A : Optional[Any] = input_list[i + 1], input_list[i] # swapping if elements not in order _A : int = False return input_list if __name__ == "__main__": print("Enter list to be sorted") _snake_case = [int(x) for x in input().split()] # inputing elements of the list in one line _snake_case = odd_even_sort(input_list) print("The sorted list is") print(sorted_list)
710
import json import os import shutil import tempfile from unittest import TestCase from transformers import BartTokenizer, BartTokenizerFast, DPRQuestionEncoderTokenizer, DPRQuestionEncoderTokenizerFast from transformers.models.bart.configuration_bart import BartConfig from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES as DPR_VOCAB_FILES_NAMES from transformers.models.dpr.configuration_dpr import DPRConfig from transformers.models.roberta.tokenization_roberta import VOCAB_FILES_NAMES as BART_VOCAB_FILES_NAMES from transformers.testing_utils import require_faiss, require_tokenizers, require_torch, slow from transformers.utils import is_datasets_available, is_faiss_available, is_torch_available if is_torch_available() and is_datasets_available() and is_faiss_available(): from transformers.models.rag.configuration_rag import RagConfig from transformers.models.rag.tokenization_rag import RagTokenizer @require_faiss @require_torch class lowercase ( UpperCamelCase__ ): def a__ ( self ) -> int: _A : int = tempfile.mkdtemp() _A : Union[str, Any] = 8 # DPR tok _A : List[str] = [ """[UNK]""", """[CLS]""", """[SEP]""", """[PAD]""", """[MASK]""", """want""", """##want""", """##ed""", """wa""", """un""", """runn""", """##ing""", """,""", """low""", """lowest""", ] _A : List[str] = os.path.join(self.tmpdirname , """dpr_tokenizer""" ) os.makedirs(_a , exist_ok=_a ) _A : str = os.path.join(_a , DPR_VOCAB_FILES_NAMES["""vocab_file"""] ) with open(self.vocab_file , """w""" , encoding="""utf-8""" ) as vocab_writer: vocab_writer.write("""""".join([x + """\n""" for x in vocab_tokens] ) ) # BART tok _A : Dict = [ """l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """\u0120""", """\u0120l""", """\u0120n""", """\u0120lo""", """\u0120low""", """er""", """\u0120lowest""", """\u0120newer""", """\u0120wider""", """<unk>""", ] _A : Optional[Any] = dict(zip(_a , range(len(_a ) ) ) ) _A : Tuple = ["""#version: 0.2""", """\u0120 l""", """\u0120l o""", """\u0120lo w""", """e r""", """"""] _A : Dict = {"""unk_token""": """<unk>"""} _A : Optional[Any] = os.path.join(self.tmpdirname , """bart_tokenizer""" ) os.makedirs(_a , exist_ok=_a ) _A : str = os.path.join(_a , BART_VOCAB_FILES_NAMES["""vocab_file"""] ) _A : List[Any] = os.path.join(_a , BART_VOCAB_FILES_NAMES["""merges_file"""] ) with open(self.vocab_file , """w""" , encoding="""utf-8""" ) as fp: fp.write(json.dumps(_a ) + """\n""" ) with open(self.merges_file , """w""" , encoding="""utf-8""" ) as fp: fp.write("""\n""".join(_a ) ) def a__ ( self ) -> DPRQuestionEncoderTokenizer: return DPRQuestionEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname , """dpr_tokenizer""" ) ) def a__ ( self ) -> BartTokenizer: return BartTokenizer.from_pretrained(os.path.join(self.tmpdirname , """bart_tokenizer""" ) ) def a__ ( self ) -> Optional[Any]: shutil.rmtree(self.tmpdirname ) @require_tokenizers def a__ ( self ) -> str: _A : Optional[Any] = os.path.join(self.tmpdirname , """rag_tokenizer""" ) _A : int = RagConfig(question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() ) _A : Any = RagTokenizer(question_encoder=self.get_dpr_tokenizer() , generator=self.get_bart_tokenizer() ) rag_config.save_pretrained(_a ) rag_tokenizer.save_pretrained(_a ) _A : Optional[Any] = RagTokenizer.from_pretrained(_a , config=_a ) self.assertIsInstance(new_rag_tokenizer.question_encoder , _a ) self.assertEqual(new_rag_tokenizer.question_encoder.get_vocab() , rag_tokenizer.question_encoder.get_vocab() ) self.assertIsInstance(new_rag_tokenizer.generator , _a ) self.assertEqual(new_rag_tokenizer.generator.get_vocab() , rag_tokenizer.generator.get_vocab() ) @slow def a__ ( self ) -> str: _A : Union[str, Any] = RagTokenizer.from_pretrained("""facebook/rag-token-nq""" ) _A : Tuple = [ """who got the first nobel prize in physics""", """when is the next deadpool movie being released""", """which mode is used for short wave broadcast service""", """who is the owner of reading football club""", """when is the next scandal episode coming out""", """when is the last time the philadelphia won the superbowl""", """what is the most current adobe flash player version""", """how many episodes are there in dragon ball z""", """what is the first step in the evolution of the eye""", """where is gall bladder situated in human body""", """what is the main mineral in lithium batteries""", """who is the president of usa right now""", """where do the greasers live in the outsiders""", """panda is a national animal of which country""", """what is the name of manchester united stadium""", ] _A : Tuple = tokenizer(_a ) self.assertIsNotNone(_a ) @slow def a__ ( self ) -> Dict: _A : Dict = RagTokenizer.from_pretrained("""facebook/rag-sequence-nq""" ) _A : str = [ """who got the first nobel prize in physics""", """when is the next deadpool movie being released""", """which mode is used for short wave broadcast service""", """who is the owner of reading football club""", """when is the next scandal episode coming out""", """when is the last time the philadelphia won the superbowl""", """what is the most current adobe flash player version""", """how many episodes are there in dragon ball z""", """what is the first step in the evolution of the eye""", """where is gall bladder situated in human body""", """what is the main mineral in lithium batteries""", """who is the president of usa right now""", """where do the greasers live in the outsiders""", """panda is a national animal of which country""", """what is the name of manchester united stadium""", ] _A : Optional[Any] = tokenizer(_a ) self.assertIsNotNone(_a )
54
0
from typing import List import datasets from datasets.tasks import AudioClassification from ..folder_based_builder import folder_based_builder _snake_case = datasets.utils.logging.get_logger(__name__) class lowercase ( folder_based_builder.FolderBasedBuilderConfig ): _a = None _a = None class lowercase ( folder_based_builder.FolderBasedBuilder ): _a = datasets.Audio() _a = "audio" _a = AudioFolderConfig _a = 4_2 # definition at the bottom of the script _a = AudioClassification(audio_column="audio",label_column="label" ) _snake_case = [ ".aiff", ".au", ".avr", ".caf", ".flac", ".htk", ".svx", ".mat4", ".mat5", ".mpc2k", ".ogg", ".paf", ".pvf", ".raw", ".rf64", ".sd2", ".sds", ".ircam", ".voc", ".w64", ".wav", ".nist", ".wavex", ".wve", ".xi", ".mp3", ".opus", ] _snake_case = AUDIO_EXTENSIONS
711
import os import re import shutil import sys import tempfile import unittest import black _snake_case = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) sys.path.append(os.path.join(git_repo_path, "utils")) import check_copies # noqa: E402 # This is the reference code that will be used in the tests. # If BertLMPredictionHead is changed in modeling_bert.py, this code needs to be manually updated. _snake_case = " def __init__(self, config):\n super().__init__()\n self.transform = BertPredictionHeadTransform(config)\n\n # The output weights are the same as the input embeddings, but there is\n # an output-only bias for each token.\n self.decoder = nn.Linear(config.hidden_size, config.vocab_size, bias=False)\n\n self.bias = nn.Parameter(torch.zeros(config.vocab_size))\n\n # Need a link between the two variables so that the bias is correctly resized with `resize_token_embeddings`\n self.decoder.bias = self.bias\n\n def forward(self, hidden_states):\n hidden_states = self.transform(hidden_states)\n hidden_states = self.decoder(hidden_states)\n return hidden_states\n" class lowercase ( unittest.TestCase ): def a__ ( self ) -> Union[str, Any]: _A : List[str] = tempfile.mkdtemp() os.makedirs(os.path.join(self.transformer_dir , """models/bert/""" ) ) _A : str = self.transformer_dir shutil.copy( os.path.join(_a , """src/transformers/models/bert/modeling_bert.py""" ) , os.path.join(self.transformer_dir , """models/bert/modeling_bert.py""" ) , ) def a__ ( self ) -> Optional[int]: _A : List[str] = """src/transformers""" shutil.rmtree(self.transformer_dir ) def a__ ( self , _a , _a , _a , _a=None ) -> Optional[Any]: _A : Optional[Any] = comment + F'''\nclass {class_name}(nn.Module):\n''' + class_code if overwrite_result is not None: _A : List[str] = comment + F'''\nclass {class_name}(nn.Module):\n''' + overwrite_result _A : List[str] = black.Mode(target_versions={black.TargetVersion.PYaa} , line_length=119 ) _A : Optional[int] = black.format_str(_a , mode=_a ) _A : Optional[Any] = os.path.join(self.transformer_dir , """new_code.py""" ) with open(_a , """w""" , newline="""\n""" ) as f: f.write(_a ) if overwrite_result is None: self.assertTrue(len(check_copies.is_copy_consistent(_a ) ) == 0 ) else: check_copies.is_copy_consistent(f.name , overwrite=_a ) with open(_a , """r""" ) as f: self.assertTrue(f.read() , _a ) def a__ ( self ) -> str: _A : Union[str, Any] = check_copies.find_code_in_transformers("""models.bert.modeling_bert.BertLMPredictionHead""" ) self.assertEqual(_a , _a ) def a__ ( self ) -> int: # Base copy consistency self.check_copy_consistency( """# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead""" , """BertLMPredictionHead""" , REFERENCE_CODE + """\n""" , ) # With no empty line at the end self.check_copy_consistency( """# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead""" , """BertLMPredictionHead""" , _a , ) # Copy consistency with rename self.check_copy_consistency( """# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->TestModel""" , """TestModelLMPredictionHead""" , re.sub("""Bert""" , """TestModel""" , _a ) , ) # Copy consistency with a really long name _A : List[str] = """TestModelWithAReallyLongNameBecauseSomePeopleLikeThatForSomeReason""" self.check_copy_consistency( F'''# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->{long_class_name}''' , F'''{long_class_name}LMPredictionHead''' , re.sub("""Bert""" , _a , _a ) , ) # Copy consistency with overwrite self.check_copy_consistency( """# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->TestModel""" , """TestModelLMPredictionHead""" , _a , overwrite_result=re.sub("""Bert""" , """TestModel""" , _a ) , ) def a__ ( self ) -> Tuple: _A : Union[str, Any] = check_copies.LOCALIZED_READMES["""README_zh-hans.md"""] _A : str = ( """1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (from Google Research and the""" """ Toyota Technological Institute at Chicago) released with the paper [ALBERT: A Lite BERT for""" """ Self-supervised Learning of Language Representations](https://arxiv.org/abs/1909.11942), by Zhenzhong""" """ Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut.\n1.""" """ **[DistilBERT](https://huggingface.co/transformers/model_doc/distilbert.html)** (from HuggingFace),""" """ released together with the paper [DistilBERT, a distilled version of BERT: smaller, faster, cheaper and""" """ lighter](https://arxiv.org/abs/1910.01108) by Victor Sanh, Lysandre Debut and Thomas Wolf. The same""" """ method has been applied to compress GPT2 into""" """ [DistilGPT2](https://github.com/huggingface/transformers/tree/main/examples/distillation), RoBERTa into""" """ [DistilRoBERTa](https://github.com/huggingface/transformers/tree/main/examples/distillation),""" """ Multilingual BERT into""" """ [DistilmBERT](https://github.com/huggingface/transformers/tree/main/examples/distillation) and a German""" """ version of DistilBERT.\n1. **[ELECTRA](https://huggingface.co/transformers/model_doc/electra.html)**""" """ (from Google Research/Stanford University) released with the paper [ELECTRA: Pre-training text encoders""" """ as discriminators rather than generators](https://arxiv.org/abs/2003.10555) by Kevin Clark, Minh-Thang""" """ Luong, Quoc V. Le, Christopher D. Manning.""" ) _A : str = ( """1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the""" """ Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of""" """ Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian""" """ Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n""" ) _A : Any = ( """1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the""" """ Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of""" """ Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian""" """ Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n1.""" """ **[DistilBERT](https://huggingface.co/transformers/model_doc/distilbert.html)** (来自 HuggingFace) 伴随论文""" """ [DistilBERT, a distilled version of BERT: smaller, faster, cheaper and""" """ lighter](https://arxiv.org/abs/1910.01108) 由 Victor Sanh, Lysandre Debut and Thomas Wolf 发布。 The same""" """ method has been applied to compress GPT2 into""" """ [DistilGPT2](https://github.com/huggingface/transformers/tree/main/examples/distillation), RoBERTa into""" """ [DistilRoBERTa](https://github.com/huggingface/transformers/tree/main/examples/distillation),""" """ Multilingual BERT into""" """ [DistilmBERT](https://github.com/huggingface/transformers/tree/main/examples/distillation) and a German""" """ version of DistilBERT.\n1. **[ELECTRA](https://huggingface.co/transformers/model_doc/electra.html)** (来自""" """ Google Research/Stanford University) 伴随论文 [ELECTRA: Pre-training text encoders as discriminators rather""" """ than generators](https://arxiv.org/abs/2003.10555) 由 Kevin Clark, Minh-Thang Luong, Quoc V. Le,""" """ Christopher D. Manning 发布。\n""" ) _A , _A : Tuple = check_copies.convert_to_localized_md( _a , _a , localized_readme["""format_model_list"""] ) self.assertFalse(_a ) self.assertEqual(_a , _a ) _A , _A : List[str] = check_copies.convert_to_localized_md( _a , _a , localized_readme["""format_model_list"""] ) # Check whether the number of models is equal to README.md after conversion. self.assertTrue(_a ) _A : Tuple = ( """1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (from Google Research and the""" """ Toyota Technological Institute at Chicago) released with the paper [ALBERT: A Lite BERT for""" """ Self-supervised Learning of Language Representations](https://arxiv.org/abs/1909.11942), by Zhenzhong""" """ Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut.""" ) _A : Dict = ( """1. **[ALBERT](https://huggingface.co/transformers/main/model_doc/albert.html)** (来自 Google Research and""" """ the Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of""" """ Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian""" """ Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n""" ) _A : Optional[Any] = ( """1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the""" """ Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of""" """ Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian""" """ Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n""" ) _A , _A : Optional[int] = check_copies.convert_to_localized_md( _a , _a , localized_readme["""format_model_list"""] ) # Check if the model link is synchronized. self.assertEqual(_a , _a )
54
0
from google.protobuf import descriptor as _descriptor from google.protobuf import descriptor_pool as _descriptor_pool from google.protobuf import symbol_database as _symbol_database from google.protobuf.internal import builder as _builder # @@protoc_insertion_point(imports) _snake_case = _symbol_database.Default() _snake_case = _descriptor_pool.Default().AddSerializedFile( b"\n\x19sentencepiece_model.proto\x12\rsentencepiece\"\x80\x0c\n\x0bTrainerSpec\x12\r\n\x05input\x18\x01 \x03(\t\x12\x14\n\x0cinput_format\x18\x07 \x01(\t\x12\x14\n\x0cmodel_prefix\x18\x02 \x01(\t\x12\x41\n\nmodel_type\x18\x03 \x01(\x0e\x32$.sentencepiece.TrainerSpec.ModelType:\x07UNIGRAM\x12\x18\n\nvocab_size\x18\x04 \x01(\x05:\x04\x38\x30\x30\x30\x12\x17\n\x0f\x61\x63\x63\x65pt_language\x18\x05 \x03(\t\x12 \n\x15self_test_sample_size\x18\x06 \x01(\x05:\x01\x30\x12*\n\x1b\x65nable_differential_privacy\x18\x32 \x01(\x08:\x05\x66\x61lse\x12+\n differential_privacy_noise_level\x18\x33 \x01(\x02:\x01\x30\x12\x32\n\'differential_privacy_clipping_threshold\x18\x34 \x01(\x04:\x01\x30\x12\"\n\x12\x63haracter_coverage\x18\n \x01(\x02:\x06\x30.9995\x12\x1e\n\x13input_sentence_size\x18\x0b \x01(\x04:\x01\x30\x12$\n\x16shuffle_input_sentence\x18\x13 \x01(\x08:\x04true\x12 \n\x14mining_sentence_size\x18\x0c \x01(\x05\x42\x02\x18\x01\x12\"\n\x16training_sentence_size\x18\r \x01(\x05\x42\x02\x18\x01\x12(\n\x17seed_sentencepiece_size\x18\x0e \x01(\x05:\x07\x31\x30\x30\x30\x30\x30\x30\x12\x1e\n\x10shrinking_factor\x18\x0f \x01(\x02:\x04\x30.75\x12!\n\x13max_sentence_length\x18\x12 \x01(\x05:\x04\x34\x31\x39\x32\x12\x17\n\x0bnum_threads\x18\x10 \x01(\x05:\x02\x31\x36\x12\x1d\n\x12num_sub_iterations\x18\x11 \x01(\x05:\x01\x32\x12$\n\x18max_sentencepiece_length\x18\x14 \x01(\x05:\x02\x31\x36\x12%\n\x17split_by_unicode_script\x18\x15 \x01(\x08:\x04true\x12\x1d\n\x0fsplit_by_number\x18\x17 \x01(\x08:\x04true\x12!\n\x13split_by_whitespace\x18\x16 \x01(\x08:\x04true\x12)\n\x1atreat_whitespace_as_suffix\x18\x18 \x01(\x08:\x05\x66\x61lse\x12+\n\x1c\x61llow_whitespace_only_pieces\x18\x1a \x01(\x08:\x05\x66\x61lse\x12\x1b\n\x0csplit_digits\x18\x19 \x01(\x08:\x05\x66\x61lse\x12#\n\x19pretokenization_delimiter\x18\x35 \x01(\t:\x00\x12\x17\n\x0f\x63ontrol_symbols\x18\x1e \x03(\t\x12\x1c\n\x14user_defined_symbols\x18\x1f \x03(\t\x12\x16\n\x0erequired_chars\x18$ \x01(\t\x12\x1c\n\rbyte_fallback\x18# \x01(\x08:\x05\x66\x61lse\x12+\n\x1dvocabulary_output_piece_score\x18 \x01(\x08:\x04true\x12\x1e\n\x10hard_vocab_limit\x18! \x01(\x08:\x04true\x12\x1c\n\ruse_all_vocab\x18\" \x01(\x08:\x05\x66\x61lse\x12\x11\n\x06unk_id\x18( \x01(\x05:\x01\x30\x12\x11\n\x06\x62os_id\x18) \x01(\x05:\x01\x31\x12\x11\n\x06\x65os_id\x18* \x01(\x05:\x01\x32\x12\x12\n\x06pad_id\x18+ \x01(\x05:\x02-1\x12\x18\n\tunk_piece\x18- \x01(\t:\x05<unk>\x12\x16\n\tbos_piece\x18. \x01(\t:\x03<s>\x12\x17\n\teos_piece\x18/ \x01(\t:\x04</s>\x12\x18\n\tpad_piece\x18\x30 \x01(\t:\x05<pad>\x12\x1a\n\x0bunk_surface\x18, \x01(\t:\x05 \xe2\x81\x87 \x12+\n\x1ctrain_extremely_large_corpus\x18\x31 \x01(\x08:\x05\x66\x61lse\"5\n\tModelType\x12\x0b\n\x07UNIGRAM\x10\x01\x12\x07\n\x03\x42PE\x10\x02\x12\x08\n\x04WORD\x10\x03\x12\x08\n\x04\x43HAR\x10\x04*\t\x08\xc8\x01\x10\x80\x80\x80\x80\x02\"\xd1\x01\n\x0eNormalizerSpec\x12\x0c\n\x04name\x18\x01 \x01(\t\x12\x1c\n\x14precompiled_charsmap\x18\x02 \x01(\x0c\x12\x1e\n\x10\x61\x64\x64_dummy_prefix\x18\x03 \x01(\x08:\x04true\x12&\n\x18remove_extra_whitespaces\x18\x04 \x01(\x08:\x04true\x12 \n\x12\x65scape_whitespaces\x18\x05 \x01(\x08:\x04true\x12\x1e\n\x16normalization_rule_tsv\x18\x06 \x01(\t*\t\x08\xc8\x01\x10\x80\x80\x80\x80\x02\"y\n\x0cSelfTestData\x12\x33\n\x07samples\x18\x01 \x03(\x0b\x32\".sentencepiece.SelfTestData.Sample\x1a)\n\x06Sample\x12\r\n\x05input\x18\x01 \x01(\t\x12\x10\n\x08\x65xpected\x18\x02 \x01(\t*\t\x08\xc8\x01\x10\x80\x80\x80\x80\x02\"\xfe\x03\n\nModelProto\x12\x37\n\x06pieces\x18\x01 \x03(\x0b\x32\'.sentencepiece.ModelProto.SentencePiece\x12\x30\n\x0ctrainer_spec\x18\x02 \x01(\x0b\x32\x1a.sentencepiece.TrainerSpec\x12\x36\n\x0fnormalizer_spec\x18\x03 \x01(\x0b\x32\x1d.sentencepiece.NormalizerSpec\x12\x33\n\x0eself_test_data\x18\x04 \x01(\x0b\x32\x1b.sentencepiece.SelfTestData\x12\x38\n\x11\x64\x65normalizer_spec\x18\x05 \x01(\x0b\x32\x1d.sentencepiece.NormalizerSpec\x1a\xd2\x01\n\rSentencePiece\x12\r\n\x05piece\x18\x01 \x01(\t\x12\r\n\x05score\x18\x02 \x01(\x02\x12\x42\n\x04type\x18\x03 \x01(\x0e\x32,.sentencepiece.ModelProto.SentencePiece.Type:\x06NORMAL\"T\n\x04Type\x12\n\n\x06NORMAL\x10\x01\x12\x0b\n\x07UNKNOWN\x10\x02\x12\x0b\n\x07\x43ONTROL\x10\x03\x12\x10\n\x0cUSER_DEFINED\x10\x04\x12\x08\n\x04\x42YTE\x10\x06\x12\n\n\x06UNUSED\x10\x05*\t\x08\xc8\x01\x10\x80\x80\x80\x80\x02*\t\x08\xc8\x01\x10\x80\x80\x80\x80\x02\x42\x02H\x03" ) _snake_case = globals() _builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, _globals) _builder.BuildTopDescriptorsAndMessages(DESCRIPTOR, "sentencepiece_model_pb2", _globals) if _descriptor._USE_C_DESCRIPTORS is False: _snake_case = None _snake_case = b"H\003" # (generated by protobuf compiler, but `_TRAINERSPEC` is not defined) # _TRAINERSPEC.fields_by_name["mining_sentence_size"]._options = None # _TRAINERSPEC.fields_by_name["mining_sentence_size"]._serialized_options = b"\030\001" # _TRAINERSPEC.fields_by_name["training_sentence_size"]._options = None # _TRAINERSPEC.fields_by_name["training_sentence_size"]._serialized_options = b"\030\001" _snake_case = 45 _snake_case = 1581 _snake_case = 1517 _snake_case = 1570 _snake_case = 1584 _snake_case = 1793 _snake_case = 1795 _snake_case = 1916 _snake_case = 1864 _snake_case = 1905 _snake_case = 1919 _snake_case = 2429 _snake_case = 2208 _snake_case = 2418 _snake_case = 2323 _snake_case = 2407 # @@protoc_insertion_point(module_scope)
712
import itertools import os import random import tempfile import unittest import numpy as np from transformers import TvltFeatureExtractor, is_datasets_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_torch_available(): import torch if is_datasets_available(): from datasets import load_dataset _snake_case = random.Random() def lowerCAmelCase_ ( snake_case_,snake_case_=1.0,snake_case_=None,snake_case_=None ): if rng is None: _A : str = global_rng _A : List[Any] = [] for batch_idx in range(shape[0] ): values.append([] ) for _ in range(shape[1] ): values[-1].append(rng.random() * scale ) return values class lowercase ( unittest.TestCase ): def __init__( self , _a , _a=7 , _a=400 , _a=2000 , _a=2048 , _a=128 , _a=1 , _a=512 , _a=30 , _a=4_4100 , ) -> Tuple: _A : Any = parent _A : str = batch_size _A : Union[str, Any] = min_seq_length _A : int = max_seq_length _A : List[str] = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) _A : Tuple = spectrogram_length _A : int = feature_size _A : str = num_audio_channels _A : Tuple = hop_length _A : List[str] = chunk_length _A : Union[str, Any] = sampling_rate def a__ ( self ) -> Tuple: return { "spectrogram_length": self.spectrogram_length, "feature_size": self.feature_size, "num_audio_channels": self.num_audio_channels, "hop_length": self.hop_length, "chunk_length": self.chunk_length, "sampling_rate": self.sampling_rate, } def a__ ( self , _a=False , _a=False ) -> Optional[int]: def _flatten(_a ): return list(itertools.chain(*_a ) ) if equal_length: _A : List[str] = [floats_list((self.max_seq_length, self.feature_size) ) for _ in range(self.batch_size )] else: # make sure that inputs increase in size _A : Union[str, 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 : List[Any] = [np.asarray(_a ) for x in speech_inputs] return speech_inputs @require_torch @require_torchaudio class lowercase ( UpperCamelCase__,unittest.TestCase ): _a = TvltFeatureExtractor def a__ ( self ) -> Any: _A : int = TvltFeatureExtractionTester(self ) def a__ ( self ) -> List[Any]: _A : Tuple = self.feature_extraction_class(**self.feat_extract_dict ) self.assertTrue(hasattr(_a , """spectrogram_length""" ) ) self.assertTrue(hasattr(_a , """feature_size""" ) ) self.assertTrue(hasattr(_a , """num_audio_channels""" ) ) self.assertTrue(hasattr(_a , """hop_length""" ) ) self.assertTrue(hasattr(_a , """chunk_length""" ) ) self.assertTrue(hasattr(_a , """sampling_rate""" ) ) def a__ ( self ) -> Optional[int]: _A : str = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: _A : Tuple = feat_extract_first.save_pretrained(_a )[0] check_json_file_has_correct_format(_a ) _A : Optional[int] = self.feature_extraction_class.from_pretrained(_a ) _A : Optional[Any] = feat_extract_first.to_dict() _A : int = feat_extract_second.to_dict() _A : int = dict_first.pop("""mel_filters""" ) _A : Optional[int] = dict_second.pop("""mel_filters""" ) self.assertTrue(np.allclose(_a , _a ) ) self.assertEqual(_a , _a ) def a__ ( self ) -> int: _A : str = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: _A : List[str] = os.path.join(_a , """feat_extract.json""" ) feat_extract_first.to_json_file(_a ) _A : Union[str, Any] = self.feature_extraction_class.from_json_file(_a ) _A : Optional[Any] = feat_extract_first.to_dict() _A : Union[str, Any] = feat_extract_second.to_dict() _A : List[Any] = dict_first.pop("""mel_filters""" ) _A : Optional[Any] = dict_second.pop("""mel_filters""" ) self.assertTrue(np.allclose(_a , _a ) ) self.assertEqual(_a , _a ) def a__ ( self ) -> Optional[Any]: # Initialize feature_extractor _A : Tuple = self.feature_extraction_class(**self.feat_extract_dict ) # create three inputs of length 800, 1000, and 1200 _A : Optional[int] = [floats_list((1, x) )[0] for x in range(800 , 1400 , 200 )] _A : List[str] = [np.asarray(_a ) for speech_input in speech_inputs] # Test not batched input _A : Optional[int] = feature_extractor(np_speech_inputs[0] , return_tensors="""np""" , sampling_rate=4_4100 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test batched _A : str = feature_extractor(_a , return_tensors="""np""" , sampling_rate=4_4100 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test audio masking _A : Optional[Any] = feature_extractor( _a , return_tensors="""np""" , sampling_rate=4_4100 , mask_audio=_a ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) # Test 2-D numpy arrays are batched. _A : Dict = [floats_list((1, x) )[0] for x in (800, 800, 800)] _A : Optional[int] = np.asarray(_a ) _A : Optional[Any] = feature_extractor(_a , return_tensors="""np""" , sampling_rate=4_4100 ).audio_values self.assertTrue(encoded_audios.ndim == 4 ) self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size ) self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length ) self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels ) def a__ ( self , _a ) -> str: _A : Optional[Any] = load_dataset("""hf-internal-testing/librispeech_asr_dummy""" , """clean""" , split="""validation""" ) # automatic decoding with librispeech _A : Dict = ds.sort("""id""" ).select(range(_a ) )[:num_samples]["""audio"""] return [x["array"] for x in speech_samples] def a__ ( self ) -> Optional[Any]: _A : List[str] = self._load_datasamples(1 ) _A : List[str] = TvltFeatureExtractor() _A : List[Any] = feature_extractor(_a , return_tensors="""pt""" ).audio_values self.assertEquals(audio_values.shape , (1, 1, 192, 128) ) _A : int = torch.tensor([[-0.3032, -0.2708], [-0.4434, -0.4007]] ) self.assertTrue(torch.allclose(audio_values[0, 0, :2, :2] , _a , atol=1e-4 ) )
54
0
# Usage: # ./gen-card-facebook-wmt19.py import os from pathlib import Path def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_ ): '''simple docstring''' _A : Dict = { """en""": """Machine learning is great, isn\'t it?""", """ru""": """Машинное обучение - это здорово, не так ли?""", """de""": """Maschinelles Lernen ist großartig, oder?""", } # BLUE scores as follows: # "pair": [fairseq, transformers] _A : Union[str, Any] = { """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"""], } _A : Optional[int] = f'''{src_lang}-{tgt_lang}''' _A : List[Any] = 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(UpperCAmelCase__,exist_ok=UpperCAmelCase__ ) _A : Optional[int] = os.path.join(UpperCAmelCase__,"""README.md""" ) print(f'''Generating {path}''' ) with open(UpperCAmelCase__,"""w""",encoding="""utf-8""" ) as f: f.write(UpperCAmelCase__ ) # make sure we are under the root of the project _snake_case = Path(__file__).resolve().parent.parent.parent _snake_case = repo_dir / "model_cards" for model_name in ["wmt19-ru-en", "wmt19-en-ru", "wmt19-en-de", "wmt19-de-en"]: _snake_case , _snake_case , _snake_case = model_name.split("-") _snake_case = model_cards_dir / "facebook" / model_name write_model_card(model_card_dir, src_lang=src_lang, tgt_lang=tgt_lang)
713
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available _snake_case = { "configuration_time_series_transformer": [ "TIME_SERIES_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", "TimeSeriesTransformerConfig", ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = [ "TIME_SERIES_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "TimeSeriesTransformerForPrediction", "TimeSeriesTransformerModel", "TimeSeriesTransformerPreTrainedModel", ] if TYPE_CHECKING: from .configuration_time_series_transformer import ( TIME_SERIES_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TimeSeriesTransformerConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_time_series_transformer import ( TIME_SERIES_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TimeSeriesTransformerForPrediction, TimeSeriesTransformerModel, TimeSeriesTransformerPreTrainedModel, ) else: import sys _snake_case = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
54
0
import argparse import os import re # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/check_dummies.py _snake_case = "src/diffusers" # Matches is_xxx_available() _snake_case = re.compile(r"is\_([a-z_]*)_available\(\)") # Matches from xxx import bla _snake_case = re.compile(r"\s+from\s+\S*\s+import\s+([^\(\s].*)\n") _snake_case = "\n{0} = None\n" _snake_case = "\nclass {0}(metaclass=DummyObject):\n _backends = {1}\n\n def __init__(self, *args, **kwargs):\n requires_backends(self, {1})\n\n @classmethod\n def from_config(cls, *args, **kwargs):\n requires_backends(cls, {1})\n\n @classmethod\n def from_pretrained(cls, *args, **kwargs):\n requires_backends(cls, {1})\n" _snake_case = "\ndef {0}(*args, **kwargs):\n requires_backends({0}, {1})\n" def lowerCAmelCase_ ( snake_case_ ): _A : Tuple = _re_backend.findall(UpperCAmelCase__ ) if len(UpperCAmelCase__ ) == 0: return None return "_and_".join(UpperCAmelCase__ ) def lowerCAmelCase_ ( ): with open(os.path.join(UpperCAmelCase__,"""__init__.py""" ),"""r""",encoding="""utf-8""",newline="""\n""" ) as f: _A : Tuple = f.readlines() # Get to the point we do the actual imports for type checking _A : Optional[int] = 0 _A : List[str] = {} # Go through the end of the file while line_index < len(UpperCAmelCase__ ): # If the line contains is_backend_available, we grab all objects associated with the `else` block _A : int = find_backend(lines[line_index] ) if backend is not None: while not lines[line_index].startswith("""else:""" ): line_index += 1 line_index += 1 _A : int = [] # Until we unindent, add backend objects to the list while line_index < len(UpperCAmelCase__ ) and len(lines[line_index] ) > 1: _A : str = lines[line_index] _A : Dict = _re_single_line_import.search(UpperCAmelCase__ ) if single_line_import_search is not None: objects.extend(single_line_import_search.groups()[0].split(""", """ ) ) elif line.startswith(""" """ * 8 ): objects.append(line[8:-2] ) line_index += 1 if len(UpperCAmelCase__ ) > 0: _A : str = objects else: line_index += 1 return backend_specific_objects def lowerCAmelCase_ ( snake_case_,snake_case_ ): if name.isupper(): return DUMMY_CONSTANT.format(UpperCAmelCase__ ) elif name.islower(): return DUMMY_FUNCTION.format(UpperCAmelCase__,UpperCAmelCase__ ) else: return DUMMY_CLASS.format(UpperCAmelCase__,UpperCAmelCase__ ) def lowerCAmelCase_ ( snake_case_=None ): if backend_specific_objects is None: _A : Any = read_init() # For special correspondence backend to module name as used in the function requires_modulename _A : Optional[int] = {} for backend, objects in backend_specific_objects.items(): _A : Tuple = """[""" + """, """.join(f'''"{b}"''' for b in backend.split("""_and_""" ) ) + """]""" _A : Tuple = """# This file is autogenerated by the command `make fix-copies`, do not edit.\n""" dummy_file += "from ..utils import DummyObject, requires_backends\n\n" dummy_file += "\n".join([create_dummy_object(UpperCAmelCase__,UpperCAmelCase__ ) for o in objects] ) _A : int = dummy_file return dummy_files def lowerCAmelCase_ ( snake_case_=False ): _A : int = create_dummy_files() # For special correspondence backend to shortcut as used in utils/dummy_xxx_objects.py _A : List[Any] = {"""torch""": """pt"""} # Locate actual dummy modules and read their content. _A : Tuple = os.path.join(UpperCAmelCase__,"""utils""" ) _A : Optional[int] = { backend: os.path.join(UpperCAmelCase__,f'''dummy_{short_names.get(UpperCAmelCase__,UpperCAmelCase__ )}_objects.py''' ) for backend in dummy_files.keys() } _A : List[Any] = {} for backend, file_path in dummy_file_paths.items(): if os.path.isfile(UpperCAmelCase__ ): with open(UpperCAmelCase__,"""r""",encoding="""utf-8""",newline="""\n""" ) as f: _A : Dict = f.read() else: _A : List[str] = """""" for backend in dummy_files.keys(): if dummy_files[backend] != actual_dummies[backend]: if overwrite: print( f'''Updating diffusers.utils.dummy_{short_names.get(UpperCAmelCase__,UpperCAmelCase__ )}_objects.py as the main ''' """__init__ has new objects.""" ) with open(dummy_file_paths[backend],"""w""",encoding="""utf-8""",newline="""\n""" ) as f: f.write(dummy_files[backend] ) else: raise ValueError( """The main __init__ has objects that are not present in """ f'''diffusers.utils.dummy_{short_names.get(UpperCAmelCase__,UpperCAmelCase__ )}_objects.py. Run `make fix-copies` ''' """to fix this.""" ) if __name__ == "__main__": _snake_case = argparse.ArgumentParser() parser.add_argument("--fix_and_overwrite", action="store_true", help="Whether to fix inconsistencies.") _snake_case = parser.parse_args() check_dummies(args.fix_and_overwrite)
714
import gc import random import unittest import numpy as np import torch from PIL import Image from diffusers import ( DDIMScheduler, KandinskyVaaImgaImgPipeline, KandinskyVaaPriorPipeline, UNetaDConditionModel, VQModel, ) 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 lowercase ( UpperCamelCase__,unittest.TestCase ): _a = KandinskyVaaImgaImgPipeline _a = ["image_embeds", "negative_image_embeds", "image"] _a = [ "image_embeds", "negative_image_embeds", "image", ] _a = [ "generator", "height", "width", "strength", "guidance_scale", "num_inference_steps", "return_dict", "guidance_scale", "num_images_per_prompt", "output_type", "return_dict", ] _a = False @property def a__ ( self ) -> int: return 32 @property def a__ ( self ) -> Union[str, Any]: return 32 @property def a__ ( self ) -> List[str]: return self.time_input_dim @property def a__ ( self ) -> Union[str, Any]: return self.time_input_dim * 4 @property def a__ ( self ) -> str: return 100 @property def a__ ( self ) -> Tuple: torch.manual_seed(0 ) _A : str = { """in_channels""": 4, # Out channels is double in channels because predicts mean and variance """out_channels""": 8, """addition_embed_type""": """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""": """image_proj""", """cross_attention_dim""": self.cross_attention_dim, """attention_head_dim""": 4, """resnet_time_scale_shift""": """scale_shift""", """class_embed_type""": None, } _A : Union[str, Any] = UNetaDConditionModel(**_a ) return model @property def a__ ( self ) -> int: 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 a__ ( self ) -> Tuple: torch.manual_seed(0 ) _A : Dict = VQModel(**self.dummy_movq_kwargs ) return model def a__ ( self ) -> int: _A : Any = self.dummy_unet _A : List[Any] = self.dummy_movq _A : str = { """num_train_timesteps""": 1000, """beta_schedule""": """linear""", """beta_start""": 0.00085, """beta_end""": 0.012, """clip_sample""": False, """set_alpha_to_one""": False, """steps_offset""": 0, """prediction_type""": """epsilon""", """thresholding""": False, } _A : int = DDIMScheduler(**_a ) _A : Tuple = { """unet""": unet, """scheduler""": scheduler, """movq""": movq, } return components def a__ ( self , _a , _a=0 ) -> str: _A : Optional[int] = floats_tensor((1, self.text_embedder_hidden_size) , rng=random.Random(_a ) ).to(_a ) _A : Dict = floats_tensor((1, self.text_embedder_hidden_size) , rng=random.Random(seed + 1 ) ).to( _a ) # create init_image _A : Optional[Any] = floats_tensor((1, 3, 64, 64) , rng=random.Random(_a ) ).to(_a ) _A : Optional[Any] = image.cpu().permute(0 , 2 , 3 , 1 )[0] _A : Optional[Any] = Image.fromarray(np.uinta(_a ) ).convert("""RGB""" ).resize((256, 256) ) if str(_a ).startswith("""mps""" ): _A : Tuple = torch.manual_seed(_a ) else: _A : str = torch.Generator(device=_a ).manual_seed(_a ) _A : Optional[Any] = { """image""": init_image, """image_embeds""": image_embeds, """negative_image_embeds""": negative_image_embeds, """generator""": generator, """height""": 64, """width""": 64, """num_inference_steps""": 10, """guidance_scale""": 7.0, """strength""": 0.2, """output_type""": """np""", } return inputs def a__ ( self ) -> Union[str, Any]: _A : Dict = """cpu""" _A : int = self.get_dummy_components() _A : Optional[int] = self.pipeline_class(**_a ) _A : Any = pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) _A : List[Any] = pipe(**self.get_dummy_inputs(_a ) ) _A : Dict = output.images _A : List[str] = pipe( **self.get_dummy_inputs(_a ) , return_dict=_a , )[0] _A : Dict = image[0, -3:, -3:, -1] _A : Union[str, Any] = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) _A : Optional[int] = np.array( [0.6199778, 0.63984406, 0.46145785, 0.62944984, 0.5622215, 0.47306132, 0.47441456, 0.4607606, 0.48719263] ) 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()}''' @slow @require_torch_gpu class lowercase ( unittest.TestCase ): def a__ ( self ) -> List[Any]: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def a__ ( self ) -> List[str]: _A : Any = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/kandinskyv22/kandinskyv22_img2img_frog.npy""" ) _A : Union[str, Any] = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/kandinsky/cat.png""" ) _A : Dict = """A red cartoon frog, 4k""" _A : Dict = KandinskyVaaPriorPipeline.from_pretrained( """kandinsky-community/kandinsky-2-2-prior""" , torch_dtype=torch.floataa ) pipe_prior.to(_a ) _A : int = KandinskyVaaImgaImgPipeline.from_pretrained( """kandinsky-community/kandinsky-2-2-decoder""" , torch_dtype=torch.floataa ) _A : Dict = pipeline.to(_a ) pipeline.set_progress_bar_config(disable=_a ) _A : Tuple = torch.Generator(device="""cpu""" ).manual_seed(0 ) _A , _A : List[str] = pipe_prior( _a , generator=_a , num_inference_steps=5 , negative_prompt="""""" , ).to_tuple() _A : int = pipeline( image=_a , image_embeds=_a , negative_image_embeds=_a , generator=_a , num_inference_steps=100 , height=768 , width=768 , strength=0.2 , output_type="""np""" , ) _A : Optional[int] = output.images[0] assert image.shape == (768, 768, 3) assert_mean_pixel_difference(_a , _a )
54
0
import string # frequency taken from https://en.wikipedia.org/wiki/Letter_frequency _snake_case = { "E": 1_2.7_0, "T": 9.0_6, "A": 8.1_7, "O": 7.5_1, "I": 6.9_7, "N": 6.7_5, "S": 6.3_3, "H": 6.0_9, "R": 5.9_9, "D": 4.2_5, "L": 4.0_3, "C": 2.7_8, "U": 2.7_6, "M": 2.4_1, "W": 2.3_6, "F": 2.2_3, "G": 2.0_2, "Y": 1.9_7, "P": 1.9_3, "B": 1.2_9, "V": 0.9_8, "K": 0.7_7, "J": 0.1_5, "X": 0.1_5, "Q": 0.1_0, "Z": 0.0_7, } _snake_case = "ETAOINSHRDLCUMWFGYPBVKJXQZ" _snake_case = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" def lowerCAmelCase_ ( snake_case_ ): _A : Tuple = {letter: 0 for letter in string.ascii_uppercase} for letter in message.upper(): if letter in LETTERS: letter_count[letter] += 1 return letter_count def lowerCAmelCase_ ( snake_case_ ): return x[0] def lowerCAmelCase_ ( snake_case_ ): _A : Optional[int] = get_letter_count(_UpperCamelCase ) _A : str = { freq: [] for letter, freq in letter_to_freq.items() } for letter in LETTERS: freq_to_letter[letter_to_freq[letter]].append(_UpperCamelCase ) _A : int = {} for freq in freq_to_letter: freq_to_letter[freq].sort(key=ETAOIN.find,reverse=_UpperCamelCase ) _A : Tuple = """""".join(freq_to_letter[freq] ) _A : List[str] = list(freq_to_letter_str.items() ) freq_pairs.sort(key=_UpperCamelCase,reverse=_UpperCamelCase ) _A : Any = [freq_pair[1] for freq_pair in freq_pairs] return "".join(_UpperCamelCase ) def lowerCAmelCase_ ( snake_case_ ): _A : Dict = get_frequency_order(_UpperCamelCase ) _A : str = 0 for common_letter in ETAOIN[:6]: if common_letter in freq_order[:6]: match_score += 1 for uncommon_letter in ETAOIN[-6:]: if uncommon_letter in freq_order[-6:]: match_score += 1 return match_score if __name__ == "__main__": import doctest doctest.testmod()
715
def lowerCAmelCase_ ( snake_case_ = 1000000 ): _A : Any = limit + 1 _A : Tuple = [0] * limit for first_term in range(1,snake_case_ ): for n in range(snake_case_,snake_case_,snake_case_ ): _A : Optional[int] = 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 _A : List[str] = sum(1 for x in frequency[1:limit] if x == 10 ) return count if __name__ == "__main__": print(f"""{solution() = }""")
54
0
import argparse import torch from transformers import FunnelBaseModel, FunnelConfig, FunnelModel, load_tf_weights_in_funnel from transformers.utils import logging logging.set_verbosity_info() def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_,snake_case_ ): # Initialise PyTorch model _A : str = FunnelConfig.from_json_file(snake_case_ ) print(f'''Building PyTorch model from configuration: {config}''' ) _A : int = FunnelBaseModel(snake_case_ ) if base_model else FunnelModel(snake_case_ ) # Load weights from tf checkpoint load_tf_weights_in_funnel(snake_case_,snake_case_,snake_case_ ) # Save pytorch-model print(f'''Save PyTorch model to {pytorch_dump_path}''' ) torch.save(model.state_dict(),snake_case_ ) if __name__ == "__main__": _snake_case : Any = 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( "--config_file", default=None, type=str, required=True, help="The config json file corresponding to the pre-trained 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( "--base_model", action="store_true", help="Whether you want just the base model (no decoder) or not." ) _snake_case : Any = parser.parse_args() convert_tf_checkpoint_to_pytorch( args.tf_checkpoint_path, args.config_file, args.pytorch_dump_path, args.base_model )
716
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 lowercase ( tf.keras.layers.Layer ): def __init__( self , _a , _a , _a = None , _a = None ) -> Any: super().__init__() _A : Dict = pad_token_id _A : List[Any] = max_length _A : Optional[int] = vocab _A : Optional[int] = merges _A : Optional[int] = BytePairTokenizer(_a , _a , sequence_length=_a ) @classmethod def a__ ( cls , _a , *_a , **_a ) -> str: _A : Any = [""" """.join(_a ) for m in tokenizer.bpe_ranks.keys()] _A : str = tokenizer.get_vocab() return cls(_a , _a , *_a , **_a ) @classmethod def a__ ( cls , _a , *_a , **_a ) -> List[Any]: _A : Union[str, Any] = GPTaTokenizer.from_pretrained(_a , *_a , **_a ) return cls.from_tokenizer(_a , *_a , **_a ) @classmethod def a__ ( cls , _a ) -> Union[str, Any]: return cls(**_a ) def a__ ( self ) -> Union[str, Any]: return { "vocab": self.vocab, "merges": self.merges, "max_length": self.max_length, "pad_token_id": self.pad_token_id, } def a__ ( self , _a , _a = None ) -> int: _A : Optional[int] = self.tf_tokenizer(_a ) _A : Tuple = tf.ones_like(_a ) if self.pad_token_id is not None: # pad the tokens up to max length _A : Dict = max_length if max_length is not None else self.max_length if max_length is not None: _A , _A : Dict = pad_model_inputs( _a , max_seq_length=_a , pad_value=self.pad_token_id ) return {"attention_mask": attention_mask, "input_ids": input_ids}
54
0
import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, XLMRobertaTokenizer from diffusers import AltDiffusionPipeline, AutoencoderKL, DDIMScheduler, PNDMScheduler, UNetaDConditionModel from diffusers.pipelines.alt_diffusion.modeling_roberta_series import ( RobertaSeriesConfig, RobertaSeriesModelWithTransformation, ) from diffusers.utils import slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class lowercase ( __lowerCAmelCase,__lowerCAmelCase,__lowerCAmelCase,unittest.TestCase ): _a = AltDiffusionPipeline _a = TEXT_TO_IMAGE_PARAMS _a = TEXT_TO_IMAGE_BATCH_PARAMS _a = TEXT_TO_IMAGE_IMAGE_PARAMS _a = TEXT_TO_IMAGE_IMAGE_PARAMS def a__ ( self ) -> Dict: torch.manual_seed(0 ) _A : str = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""") , up_block_types=("""CrossAttnUpBlock2D""", """UpBlock2D""") , cross_attention_dim=32 , ) _A : Tuple = DDIMScheduler( beta_start=0.00085 , beta_end=0.012 , beta_schedule="""scaled_linear""" , clip_sample=lowerCamelCase__ , set_alpha_to_one=lowerCamelCase__ , ) torch.manual_seed(0 ) _A : List[str] = 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 , ) # TODO: address the non-deterministic text encoder (fails for save-load tests) # torch.manual_seed(0) # text_encoder_config = RobertaSeriesConfig( # hidden_size=32, # project_dim=32, # intermediate_size=37, # layer_norm_eps=1e-05, # num_attention_heads=4, # num_hidden_layers=5, # vocab_size=5002, # ) # text_encoder = RobertaSeriesModelWithTransformation(text_encoder_config) torch.manual_seed(0 ) _A : Union[str, Any] = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , projection_dim=32 , intermediate_size=37 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=5002 , ) _A : List[Any] = CLIPTextModel(lowerCamelCase__ ) _A : List[Any] = XLMRobertaTokenizer.from_pretrained("""hf-internal-testing/tiny-xlm-roberta""" ) _A : Tuple = 77 _A : Dict = { '''unet''': unet, '''scheduler''': scheduler, '''vae''': vae, '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''safety_checker''': None, '''feature_extractor''': None, } return components def a__ ( self , _a , _a=0 ) -> Union[str, Any]: if str(lowerCamelCase__ ).startswith("""mps""" ): _A : Optional[int] = torch.manual_seed(lowerCamelCase__ ) else: _A : Dict = torch.Generator(device=lowerCamelCase__ ).manual_seed(lowerCamelCase__ ) _A : Dict = { '''prompt''': '''A painting of a squirrel eating a burger''', '''generator''': generator, '''num_inference_steps''': 2, '''guidance_scale''': 6.0, '''output_type''': '''numpy''', } return inputs def a__ ( self ) -> Union[str, Any]: super().test_attention_slicing_forward_pass(expected_max_diff=3e-3 ) def a__ ( self ) -> Dict: super().test_inference_batch_single_identical(expected_max_diff=3e-3 ) def a__ ( self ) -> Tuple: _A : Any = '''cpu''' # ensure determinism for the device-dependent torch.Generator _A : Optional[int] = self.get_dummy_components() torch.manual_seed(0 ) _A : str = RobertaSeriesConfig( hidden_size=32 , project_dim=32 , intermediate_size=37 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , vocab_size=5002 , ) # TODO: remove after fixing the non-deterministic text encoder _A : int = RobertaSeriesModelWithTransformation(lowerCamelCase__ ) _A : Any = text_encoder _A : Dict = AltDiffusionPipeline(**lowerCamelCase__ ) _A : Any = alt_pipe.to(lowerCamelCase__ ) alt_pipe.set_progress_bar_config(disable=lowerCamelCase__ ) _A : Dict = self.get_dummy_inputs(lowerCamelCase__ ) _A : Optional[int] = '''A photo of an astronaut''' _A : int = alt_pipe(**lowerCamelCase__ ) _A : Tuple = output.images _A : str = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) _A : List[Any] = np.array( [0.5748162, 0.60447145, 0.48821217, 0.50100636, 0.5431185, 0.45763683, 0.49657696, 0.48132733, 0.47573093] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def a__ ( self ) -> Dict: _A : List[str] = '''cpu''' # ensure determinism for the device-dependent torch.Generator _A : Optional[Any] = self.get_dummy_components() _A : Optional[Any] = PNDMScheduler(skip_prk_steps=lowerCamelCase__ ) torch.manual_seed(0 ) _A : Optional[int] = RobertaSeriesConfig( hidden_size=32 , project_dim=32 , intermediate_size=37 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , vocab_size=5002 , ) # TODO: remove after fixing the non-deterministic text encoder _A : int = RobertaSeriesModelWithTransformation(lowerCamelCase__ ) _A : str = text_encoder _A : str = AltDiffusionPipeline(**lowerCamelCase__ ) _A : Optional[Any] = alt_pipe.to(lowerCamelCase__ ) alt_pipe.set_progress_bar_config(disable=lowerCamelCase__ ) _A : List[Any] = self.get_dummy_inputs(lowerCamelCase__ ) _A : Tuple = alt_pipe(**lowerCamelCase__ ) _A : int = output.images _A : Tuple = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) _A : List[Any] = np.array( [0.51605093, 0.5707241, 0.47365507, 0.50578886, 0.5633877, 0.4642503, 0.5182081, 0.48763484, 0.49084237] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 @slow @require_torch_gpu class lowercase ( unittest.TestCase ): def a__ ( self ) -> List[str]: super().tearDown() gc.collect() torch.cuda.empty_cache() def a__ ( self ) -> Optional[Any]: _A : int = AltDiffusionPipeline.from_pretrained("""BAAI/AltDiffusion""" , safety_checker=lowerCamelCase__ ) _A : Union[str, Any] = alt_pipe.to(lowerCamelCase__ ) alt_pipe.set_progress_bar_config(disable=lowerCamelCase__ ) _A : Any = '''A painting of a squirrel eating a burger''' _A : Optional[int] = torch.manual_seed(0 ) _A : Optional[Any] = alt_pipe([prompt] , generator=lowerCamelCase__ , guidance_scale=6.0 , num_inference_steps=20 , output_type="""np""" ) _A : Union[str, Any] = output.images _A : Tuple = image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) _A : int = np.array([0.1010, 0.0800, 0.0794, 0.0885, 0.0843, 0.0762, 0.0769, 0.0729, 0.0586] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def a__ ( self ) -> List[Any]: _A : Any = DDIMScheduler.from_pretrained("""BAAI/AltDiffusion""" , subfolder="""scheduler""" ) _A : Optional[int] = AltDiffusionPipeline.from_pretrained("""BAAI/AltDiffusion""" , scheduler=lowerCamelCase__ , safety_checker=lowerCamelCase__ ) _A : Dict = alt_pipe.to(lowerCamelCase__ ) alt_pipe.set_progress_bar_config(disable=lowerCamelCase__ ) _A : Optional[int] = '''A painting of a squirrel eating a burger''' _A : Dict = torch.manual_seed(0 ) _A : List[str] = alt_pipe([prompt] , generator=lowerCamelCase__ , num_inference_steps=2 , output_type="""numpy""" ) _A : Optional[int] = output.images _A : Any = image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) _A : str = np.array([0.4019, 0.4052, 0.3810, 0.4119, 0.3916, 0.3982, 0.4651, 0.4195, 0.5323] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
717
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 _snake_case = get_tests_dir("fixtures/test_sentencepiece_no_bos.model") @require_sentencepiece @require_tokenizers class lowercase ( UpperCamelCase__,unittest.TestCase ): _a = PegasusTokenizer _a = PegasusTokenizerFast _a = True _a = True def a__ ( self ) -> int: super().setUp() # We have a SentencePiece fixture for testing _A : List[Any] = PegasusTokenizer(_a ) tokenizer.save_pretrained(self.tmpdirname ) @cached_property def a__ ( self ) -> int: return PegasusTokenizer.from_pretrained("""google/pegasus-large""" ) def a__ ( self , **_a ) -> PegasusTokenizer: return PegasusTokenizer.from_pretrained(self.tmpdirname , **_a ) def a__ ( self , _a ) -> List[Any]: return ("This is a test", "This is a test") def a__ ( self ) -> int: _A : Dict = """</s>""" _A : Dict = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(_a ) , _a ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(_a ) , _a ) def a__ ( self ) -> Dict: _A : List[str] = 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(_a ) , 1103 ) def a__ ( self ) -> Optional[int]: self.assertEqual(self.get_tokenizer().vocab_size , 1103 ) def a__ ( self ) -> Tuple: _A : Any = self.rust_tokenizer_class.from_pretrained(self.tmpdirname ) _A : Optional[int] = self.tokenizer_class.from_pretrained(self.tmpdirname ) _A : int = ( """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 : Optional[int] = rust_tokenizer([raw_input_str] , return_tensors=_a , add_special_tokens=_a ).input_ids[0] _A : List[Any] = py_tokenizer([raw_input_str] , return_tensors=_a , add_special_tokens=_a ).input_ids[0] self.assertListEqual(_a , _a ) def a__ ( self ) -> Any: _A : str = self._large_tokenizer # <mask_1> masks whole sentence while <mask_2> masks single word _A : Optional[int] = """<mask_1> To ensure a <mask_2> flow of bank resolutions.""" _A : Union[str, Any] = [2, 413, 615, 114, 3, 1971, 113, 1679, 1_0710, 107, 1] _A : Union[str, Any] = tokenizer([raw_input_str] , return_tensors=_a ).input_ids[0] self.assertListEqual(_a , _a ) def a__ ( self ) -> List[str]: _A : Optional[int] = self._large_tokenizer # The tracebacks for the following asserts are **better** without messages or self.assertEqual assert tokenizer.vocab_size == 9_6103 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 : Any = """To ensure a smooth flow of bank resolutions.""" _A : Optional[int] = [413, 615, 114, 2291, 1971, 113, 1679, 1_0710, 107, 1] _A : Optional[Any] = tokenizer([raw_input_str] , return_tensors=_a ).input_ids[0] self.assertListEqual(_a , _a ) assert tokenizer.convert_ids_to_tokens([0, 1, 2, 3] ) == ["<pad>", "</s>", "<mask_1>", "<mask_2>"] @require_torch def a__ ( self ) -> List[str]: _A : Union[str, Any] = ["""This is going to be way too long.""" * 150, """short example"""] _A : Optional[Any] = ["""not super long but more than 5 tokens""", """tiny"""] _A : Union[str, Any] = self._large_tokenizer(_a , padding=_a , truncation=_a , return_tensors="""pt""" ) _A : Tuple = self._large_tokenizer( text_target=_a , max_length=5 , padding=_a , truncation=_a , 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(_a ) == 2 # input_ids, attention_mask. @slow def a__ ( self ) -> Optional[Any]: # fmt: off _A : List[Any] = {"""input_ids""": [[3_8979, 143, 1_8485, 606, 130, 2_6669, 8_7686, 121, 5_4189, 1129, 111, 2_6669, 8_7686, 121, 9114, 1_4787, 121, 1_3249, 158, 592, 956, 121, 1_4621, 3_1576, 143, 6_2613, 108, 9688, 930, 4_3430, 1_1562, 6_2613, 304, 108, 1_1443, 897, 108, 9314, 1_7415, 6_3399, 108, 1_1443, 7614, 1_8316, 118, 4284, 7148, 1_2430, 143, 1400, 2_5703, 158, 111, 4284, 7148, 1_1772, 143, 2_1297, 1064, 158, 122, 204, 3506, 1754, 1133, 1_4787, 1581, 115, 3_3224, 4482, 111, 1355, 110, 2_9173, 317, 5_0833, 108, 2_0147, 9_4665, 111, 7_7198, 107, 1], [110, 6_2613, 117, 638, 112, 1133, 121, 2_0098, 1355, 7_9050, 1_3872, 135, 1596, 5_3541, 1352, 141, 1_3039, 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, 1_8289, 1_7780, 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=_a , model_name="""google/bigbird-pegasus-large-arxiv""" , revision="""ba85d0851d708441f91440d509690f1ab6353415""" , ) @require_sentencepiece @require_tokenizers class lowercase ( UpperCamelCase__,unittest.TestCase ): _a = PegasusTokenizer _a = PegasusTokenizerFast _a = True _a = True def a__ ( self ) -> Optional[Any]: super().setUp() # We have a SentencePiece fixture for testing _A : Tuple = PegasusTokenizer(_a , offset=0 , mask_token_sent=_a , mask_token="""[MASK]""" ) tokenizer.save_pretrained(self.tmpdirname ) @cached_property def a__ ( self ) -> Optional[Any]: return PegasusTokenizer.from_pretrained("""google/bigbird-pegasus-large-arxiv""" ) def a__ ( self , **_a ) -> PegasusTokenizer: return PegasusTokenizer.from_pretrained(self.tmpdirname , **_a ) def a__ ( self , _a ) -> List[str]: return ("This is a test", "This is a test") def a__ ( self ) -> List[Any]: _A : List[Any] = self.rust_tokenizer_class.from_pretrained(self.tmpdirname ) _A : Dict = self.tokenizer_class.from_pretrained(self.tmpdirname ) _A : Dict = ( """Let's see which <unk> is the better <unk_token> one [MASK] It seems like this [MASK] was important </s>""" """ <pad> <pad> <pad>""" ) _A : Optional[Any] = rust_tokenizer([raw_input_str] , return_tensors=_a , add_special_tokens=_a ).input_ids[0] _A : int = py_tokenizer([raw_input_str] , return_tensors=_a , add_special_tokens=_a ).input_ids[0] self.assertListEqual(_a , _a ) @require_torch def a__ ( self ) -> Optional[int]: _A : Tuple = ["""This is going to be way too long.""" * 1000, """short example"""] _A : Optional[Any] = ["""not super long but more than 5 tokens""", """tiny"""] _A : Tuple = self._large_tokenizer(_a , padding=_a , truncation=_a , return_tensors="""pt""" ) _A : str = self._large_tokenizer( text_target=_a , max_length=5 , padding=_a , truncation=_a , 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(_a ) == 2 # input_ids, attention_mask. def a__ ( self ) -> Dict: _A : Optional[int] = ( """This is an example string that is used to test the original TF implementation against the HF""" """ implementation""" ) _A : Any = self._large_tokenizer(_a ).input_ids self.assertListEqual( _a , [182, 117, 142, 587, 4211, 120, 117, 263, 112, 804, 109, 856, 2_5016, 3137, 464, 109, 2_6955, 3137, 1] , )
54
0
def lowerCAmelCase_ ( snake_case_=28123 ): _A : Tuple = [1] * (limit + 1) for i in range(2,int(limit**0.5 ) + 1 ): sum_divs[i * i] += i for k in range(i + 1,limit // i + 1 ): sum_divs[k * i] += k + i _A : Tuple = set() _A : List[Any] = 0 for n in range(1,limit + 1 ): if sum_divs[n] > n: abundants.add(snake_case_ ) if not any((n - a in abundants) for a in abundants ): res += n return res if __name__ == "__main__": print(solution())
718
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available, is_vision_available, ) _snake_case = { "configuration_efficientformer": [ "EFFICIENTFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", "EfficientFormerConfig", ] } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = ["EfficientFormerImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = [ "EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "EfficientFormerForImageClassification", "EfficientFormerForImageClassificationWithTeacher", "EfficientFormerModel", "EfficientFormerPreTrainedModel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = [ "TF_EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "TFEfficientFormerForImageClassification", "TFEfficientFormerForImageClassificationWithTeacher", "TFEfficientFormerModel", "TFEfficientFormerPreTrainedModel", ] if TYPE_CHECKING: from .configuration_efficientformer import EFFICIENTFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, EfficientFormerConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_efficientformer import EfficientFormerImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_efficientformer import ( EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, EfficientFormerForImageClassification, EfficientFormerForImageClassificationWithTeacher, EfficientFormerModel, EfficientFormerPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_efficientformer import ( TF_EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TFEfficientFormerForImageClassification, TFEfficientFormerForImageClassificationWithTeacher, TFEfficientFormerModel, TFEfficientFormerPreTrainedModel, ) else: import sys _snake_case = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
54
0
# NOTE: This file is deprecated and will be removed in a future version. # It only exists so that temporarely `from diffusers.pipelines import DiffusionPipeline` works from ...utils import deprecate from ..controlnet.pipeline_flax_controlnet import FlaxStableDiffusionControlNetPipeline # noqa: F401 deprecate( "stable diffusion controlnet", "0.22.0", "Importing `FlaxStableDiffusionControlNetPipeline` from diffusers.pipelines.stable_diffusion.flax_pipeline_stable_diffusion_controlnet is deprecated. Please import `from diffusers import FlaxStableDiffusionControlNetPipeline` instead.", standard_warn=False, stacklevel=3, )
719
from unittest import TestCase from datasets import Sequence, Value from datasets.arrow_dataset import Dataset class lowercase ( UpperCamelCase__ ): def a__ ( self ) -> Optional[int]: return [ {"col_1": 3, "col_2": "a"}, {"col_1": 2, "col_2": "b"}, {"col_1": 1, "col_2": "c"}, {"col_1": 0, "col_2": "d"}, ] def a__ ( self ) -> Optional[int]: _A : Union[str, Any] = {"""col_1""": [3, 2, 1, 0], """col_2""": ["""a""", """b""", """c""", """d"""]} return Dataset.from_dict(_a ) def a__ ( self ) -> Any: _A : str = self._create_example_records() _A : List[Any] = Dataset.from_list(_a ) self.assertListEqual(dset.column_names , ["""col_1""", """col_2"""] ) for i, r in enumerate(_a ): self.assertDictEqual(_a , example_records[i] ) def a__ ( self ) -> List[str]: _A : Dict = self._create_example_records() _A : List[str] = Dataset.from_list(_a ) _A : str = Dataset.from_dict({k: [r[k] for r in example_records] for k in example_records[0]} ) self.assertEqual(dset.info , dset_from_dict.info ) def a__ ( self ) -> str: # checks what happens with missing columns _A : List[str] = [{"""col_1""": 1}, {"""col_2""": """x"""}] _A : List[str] = Dataset.from_list(_a ) self.assertDictEqual(dset[0] , {"""col_1""": 1} ) self.assertDictEqual(dset[1] , {"""col_1""": None} ) # NB: first record is used for columns def a__ ( self ) -> Dict: # checks if the type can be inferred from the second record _A : List[Any] = [{"""col_1""": []}, {"""col_1""": [1, 2]}] _A : str = Dataset.from_list(_a ) self.assertEqual(dset.info.features["""col_1"""] , Sequence(Value("""int64""" ) ) ) def a__ ( self ) -> Dict: _A : List[str] = Dataset.from_list([] ) self.assertEqual(len(_a ) , 0 ) self.assertListEqual(dset.column_names , [] )
54
0
from ...configuration_utils import PretrainedConfig from ...utils import logging _snake_case = logging.get_logger(__name__) _snake_case = { "vinvino02/glpn-kitti": "https://huggingface.co/vinvino02/glpn-kitti/resolve/main/config.json", # See all GLPN models at https://huggingface.co/models?filter=glpn } class lowercase ( UpperCamelCase__ ): _a = """glpn""" def __init__( self , _a=3 , _a=4 , _a=[2, 2, 2, 2] , _a=[8, 4, 2, 1] , _a=[32, 64, 160, 256] , _a=[7, 3, 3, 3] , _a=[4, 2, 2, 2] , _a=[1, 2, 5, 8] , _a=[4, 4, 4, 4] , _a="gelu" , _a=0.0 , _a=0.0 , _a=0.02 , _a=0.1 , _a=1e-6 , _a=64 , _a=10 , _a=-1 , **_a , ) -> str: super().__init__(**_a ) _A : Optional[Any] = num_channels _A : str = num_encoder_blocks _A : str = depths _A : Optional[Any] = sr_ratios _A : Optional[int] = hidden_sizes _A : str = patch_sizes _A : Any = strides _A : List[Any] = mlp_ratios _A : Optional[int] = num_attention_heads _A : Any = hidden_act _A : Dict = hidden_dropout_prob _A : str = attention_probs_dropout_prob _A : List[str] = initializer_range _A : Any = drop_path_rate _A : Optional[Any] = layer_norm_eps _A : Tuple = decoder_hidden_size _A : Optional[Any] = max_depth _A : Dict = head_in_index
720
from __future__ import annotations from collections.abc import Sequence from typing import Literal def lowerCAmelCase_ ( snake_case_,snake_case_ ): _A : List[str] = list(snake_case_ ) _A : List[Any] = list(snake_case_ ) _A : Tuple = 0 for i in range(len(snake_case_ ) ): if lista[i] != lista[i]: count += 1 _A : Optional[Any] = """_""" if count > 1: return False else: return "".join(snake_case_ ) def lowerCAmelCase_ ( snake_case_ ): _A : Optional[Any] = [] while True: _A : int = ["""$"""] * len(snake_case_ ) _A : Any = [] for i in range(len(snake_case_ ) ): for j in range(i + 1,len(snake_case_ ) ): _A : Tuple = compare_string(binary[i],binary[j] ) if k is False: _A : str = """*""" _A : str = """*""" temp.append("""X""" ) for i in range(len(snake_case_ ) ): if checka[i] == "$": pi.append(binary[i] ) if len(snake_case_ ) == 0: return pi _A : Dict = list(set(snake_case_ ) ) def lowerCAmelCase_ ( snake_case_,snake_case_ ): _A : List[str] = [] for minterm in minterms: _A : Tuple = """""" for _ in range(snake_case_ ): _A : Optional[Any] = str(minterm % 2 ) + string minterm //= 2 temp.append(snake_case_ ) return temp def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_ ): _A : Dict = list(snake_case_ ) _A : Tuple = list(snake_case_ ) _A : Dict = 0 for i in range(len(snake_case_ ) ): if lista[i] != lista[i]: count_n += 1 return count_n == count def lowerCAmelCase_ ( snake_case_,snake_case_ ): _A : Optional[int] = [] _A : str = [0] * len(snake_case_ ) for i in range(len(chart[0] ) ): _A : Union[str, Any] = 0 _A : Optional[Any] = -1 for j in range(len(snake_case_ ) ): if chart[j][i] == 1: count += 1 _A : Dict = j if count == 1: _A : int = 1 for i in range(len(snake_case_ ) ): if select[i] == 1: for j in range(len(chart[0] ) ): if chart[i][j] == 1: for k in range(len(snake_case_ ) ): _A : int = 0 temp.append(prime_implicants[i] ) while True: _A : Optional[Any] = 0 _A : Tuple = -1 _A : List[Any] = 0 for i in range(len(snake_case_ ) ): _A : List[str] = chart[i].count(1 ) if count_n > max_n: _A : Optional[int] = count_n _A : Tuple = i if max_n == 0: return temp temp.append(prime_implicants[rem] ) for i in range(len(chart[0] ) ): if chart[rem][i] == 1: for j in range(len(snake_case_ ) ): _A : Optional[int] = 0 def lowerCAmelCase_ ( snake_case_,snake_case_ ): _A : Optional[int] = [[0 for x in range(len(snake_case_ ) )] for x in range(len(snake_case_ ) )] for i in range(len(snake_case_ ) ): _A : List[Any] = prime_implicants[i].count("""_""" ) for j in range(len(snake_case_ ) ): if is_for_table(prime_implicants[i],binary[j],snake_case_ ): _A : Union[str, Any] = 1 return chart def lowerCAmelCase_ ( ): _A : Dict = int(input("""Enter the no. of variables\n""" ) ) _A : Dict = [ float(snake_case_ ) for x in input( """Enter the decimal representation of Minterms 'Spaces Separated'\n""" ).split() ] _A : int = decimal_to_binary(snake_case_,snake_case_ ) _A : Optional[Any] = check(snake_case_ ) print("""Prime Implicants are:""" ) print(snake_case_ ) _A : int = prime_implicant_chart(snake_case_,snake_case_ ) _A : int = selection(snake_case_,snake_case_ ) print("""Essential Prime Implicants are:""" ) print(snake_case_ ) if __name__ == "__main__": import doctest doctest.testmod() main()
54
0
def lowerCAmelCase_ ( snake_case_ = 1000 ): _A : int = -1 _A : Any = 0 for a in range(1,n // 3 ): # Solving the two equations a**2+b**2=c**2 and a+b+c=N eliminating c _A : List[Any] = (n * n - 2 * a * n) // (2 * n - 2 * a) _A : Tuple = n - a - b if c * c == (a * a + b * b): _A : Any = a * b * c if candidate >= product: _A : Optional[int] = candidate return product if __name__ == "__main__": print(f"""{solution() = }""")
721
from __future__ import annotations def lowerCAmelCase_ ( snake_case_,snake_case_ = None ): _A : Tuple = word_bank or [] # create a table _A : int = len(snake_case_ ) + 1 _A : list[list[list[str]]] = [] for _ in range(snake_case_ ): table.append([] ) # seed value _A : Dict = [[]] # because empty string has empty combination # iterate through the indices for i in range(snake_case_ ): # condition if table[i] != []: for word in word_bank: # slice condition if target[i : i + len(snake_case_ )] == word: _A : list[list[str]] = [ [word, *way] for way in table[i] ] # adds the word to every combination the current position holds # now,push that combination to the table[i+len(word)] table[i + len(snake_case_ )] += new_combinations # combinations are in reverse order so reverse for better output for combination in table[len(snake_case_ )]: combination.reverse() return table[len(snake_case_ )] if __name__ == "__main__": print(all_construct("jwajalapa", ["jwa", "j", "w", "a", "la", "lapa"])) print(all_construct("rajamati", ["s", "raj", "amat", "raja", "ma", "i", "t"])) print( all_construct( "hexagonosaurus", ["h", "ex", "hex", "ag", "ago", "ru", "auru", "rus", "go", "no", "o", "s"], ) )
54
0
from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, convert_to_rgb, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging _snake_case = logging.get_logger(__name__) if is_vision_available(): import PIL class lowercase ( UpperCamelCase__ ): _a = ["pixel_values"] def __init__( self , _a = True , _a = None , _a = PILImageResampling.BICUBIC , _a = True , _a = None , _a = True , _a = 1 / 255 , _a = True , _a = None , _a = None , _a = True , **_a , ) -> None: super().__init__(**_a ) _A : Optional[Any] = size if size is not None else {"""shortest_edge""": 224} _A : Optional[Any] = get_size_dict(_a , default_to_square=_a ) _A : List[Any] = crop_size if crop_size is not None else {"""height""": 224, """width""": 224} _A : List[str] = get_size_dict(_a , default_to_square=_a , param_name="""crop_size""" ) _A : List[str] = do_resize _A : Optional[Any] = size _A : List[Any] = resample _A : str = do_center_crop _A : int = crop_size _A : Optional[Any] = do_rescale _A : List[Any] = rescale_factor _A : Optional[Any] = do_normalize _A : Tuple = image_mean if image_mean is not None else OPENAI_CLIP_MEAN _A : Dict = image_std if image_std is not None else OPENAI_CLIP_STD _A : Union[str, Any] = do_convert_rgb def a__ ( self , _a , _a , _a = PILImageResampling.BICUBIC , _a = None , **_a , ) -> np.ndarray: _A : Tuple = get_size_dict(_a , default_to_square=_a ) if "shortest_edge" not in size: raise ValueError(F'''The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}''' ) _A : Optional[int] = get_resize_output_image_size(_a , size=size["""shortest_edge"""] , default_to_square=_a ) return resize(_a , size=_a , resample=_a , data_format=_a , **_a ) def a__ ( self , _a , _a , _a = None , **_a , ) -> np.ndarray: _A : Tuple = get_size_dict(_a ) if "height" not in size or "width" not in size: raise ValueError(F'''The `size` parameter must contain the keys (height, width). Got {size.keys()}''' ) return center_crop(_a , size=(size["""height"""], size["""width"""]) , data_format=_a , **_a ) def a__ ( self , _a , _a , _a = None , **_a , ) -> Optional[int]: return rescale(_a , scale=_a , data_format=_a , **_a ) def a__ ( self , _a , _a , _a , _a = None , **_a , ) -> np.ndarray: return normalize(_a , mean=_a , std=_a , data_format=_a , **_a ) def a__ ( self , _a , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = ChannelDimension.FIRST , **_a , ) -> PIL.Image.Image: _A : Optional[int] = do_resize if do_resize is not None else self.do_resize _A : Dict = size if size is not None else self.size _A : List[str] = get_size_dict(_a , param_name="""size""" , default_to_square=_a ) _A : List[str] = resample if resample is not None else self.resample _A : Optional[int] = do_center_crop if do_center_crop is not None else self.do_center_crop _A : int = crop_size if crop_size is not None else self.crop_size _A : List[Any] = get_size_dict(_a , param_name="""crop_size""" , default_to_square=_a ) _A : Tuple = do_rescale if do_rescale is not None else self.do_rescale _A : str = rescale_factor if rescale_factor is not None else self.rescale_factor _A : Tuple = do_normalize if do_normalize is not None else self.do_normalize _A : Any = image_mean if image_mean is not None else self.image_mean _A : List[str] = image_std if image_std is not None else self.image_std _A : List[str] = do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb _A : str = make_list_of_images(_a ) if not valid_images(_a ): raise ValueError( """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """ """torch.Tensor, tf.Tensor or jax.ndarray.""" ) if do_resize and size is None: raise ValueError("""Size must be specified if do_resize is True.""" ) if do_center_crop and crop_size is None: raise ValueError("""Crop size must be specified if do_center_crop is True.""" ) if do_rescale and rescale_factor is None: raise ValueError("""Rescale factor must be specified if do_rescale is True.""" ) if do_normalize and (image_mean is None or image_std is None): raise ValueError("""Image mean and std must be specified if do_normalize is True.""" ) # PIL RGBA images are converted to RGB if do_convert_rgb: _A : Dict = [convert_to_rgb(_a ) for image in images] # All transformations expect numpy arrays. _A : int = [to_numpy_array(_a ) for image in images] if do_resize: _A : int = [self.resize(image=_a , size=_a , resample=_a ) for image in images] if do_center_crop: _A : Optional[Any] = [self.center_crop(image=_a , size=_a ) for image in images] if do_rescale: _A : Union[str, Any] = [self.rescale(image=_a , scale=_a ) for image in images] if do_normalize: _A : int = [self.normalize(image=_a , mean=_a , std=_a ) for image in images] _A : int = [to_channel_dimension_format(_a , _a ) for image in images] _A : Optional[Any] = {"""pixel_values""": images} return BatchFeature(data=_a , tensor_type=_a )
700
import operator def lowerCAmelCase_ ( snake_case_,snake_case_ = False,snake_case_ = None ): _A : str = operator.lt if reverse else operator.gt _A : Optional[Any] = solution or [] if not arr: return solution _A : Dict = [arr.pop(0 )] for i, item in enumerate(snake_case_ ): if _operator(snake_case_,sublist[-1] ): sublist.append(snake_case_ ) arr.pop(snake_case_ ) # merging sublist into solution list if not solution: solution.extend(snake_case_ ) else: while sublist: _A : Union[str, Any] = sublist.pop(0 ) for i, xx in enumerate(snake_case_ ): if not _operator(snake_case_,snake_case_ ): solution.insert(snake_case_,snake_case_ ) break else: solution.append(snake_case_ ) strand_sort(snake_case_,snake_case_,snake_case_ ) return solution if __name__ == "__main__": assert strand_sort([4, 3, 5, 1, 2]) == [1, 2, 3, 4, 5] assert strand_sort([4, 3, 5, 1, 2], reverse=True) == [5, 4, 3, 2, 1]
54
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available _snake_case = { "configuration_time_series_transformer": [ "TIME_SERIES_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", "TimeSeriesTransformerConfig", ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = [ "TIME_SERIES_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "TimeSeriesTransformerForPrediction", "TimeSeriesTransformerModel", "TimeSeriesTransformerPreTrainedModel", ] if TYPE_CHECKING: from .configuration_time_series_transformer import ( TIME_SERIES_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TimeSeriesTransformerConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_time_series_transformer import ( TIME_SERIES_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TimeSeriesTransformerForPrediction, TimeSeriesTransformerModel, TimeSeriesTransformerPreTrainedModel, ) else: import sys _snake_case = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
701
import gc import unittest from transformers import MODEL_FOR_MASKED_LM_MAPPING, TF_MODEL_FOR_MASKED_LM_MAPPING, FillMaskPipeline, pipeline from transformers.pipelines import PipelineException from transformers.testing_utils import ( is_pipeline_test, is_torch_available, nested_simplify, require_tf, require_torch, require_torch_gpu, slow, ) from .test_pipelines_common import ANY @is_pipeline_test class lowercase ( unittest.TestCase ): _a = MODEL_FOR_MASKED_LM_MAPPING _a = TF_MODEL_FOR_MASKED_LM_MAPPING def a__ ( self ) -> Tuple: super().tearDown() # clean-up as much as possible GPU memory occupied by PyTorch gc.collect() if is_torch_available(): import torch torch.cuda.empty_cache() @require_tf def a__ ( self ) -> Any: _A : Optional[Any] = pipeline(task="""fill-mask""" , model="""sshleifer/tiny-distilroberta-base""" , top_k=2 , framework="""tf""" ) _A : Optional[int] = unmasker("""My name is <mask>""" ) self.assertEqual( nested_simplify(_a , decimals=6 ) , [ {"""sequence""": """My name is grouped""", """score""": 2.1e-05, """token""": 3_8015, """token_str""": """ grouped"""}, {"""sequence""": """My name is accuser""", """score""": 2.1e-05, """token""": 2_5506, """token_str""": """ accuser"""}, ] , ) _A : Tuple = unmasker("""The largest city in France is <mask>""" ) self.assertEqual( nested_simplify(_a , decimals=6 ) , [ { """sequence""": """The largest city in France is grouped""", """score""": 2.1e-05, """token""": 3_8015, """token_str""": """ grouped""", }, { """sequence""": """The largest city in France is accuser""", """score""": 2.1e-05, """token""": 2_5506, """token_str""": """ accuser""", }, ] , ) _A : List[str] = unmasker("""My name is <mask>""" , targets=[""" Patrick""", """ Clara""", """ Teven"""] , top_k=3 ) self.assertEqual( nested_simplify(_a , decimals=6 ) , [ {"""sequence""": """My name is Clara""", """score""": 2e-05, """token""": 1_3606, """token_str""": """ Clara"""}, {"""sequence""": """My name is Patrick""", """score""": 2e-05, """token""": 3499, """token_str""": """ Patrick"""}, {"""sequence""": """My name is Te""", """score""": 1.9e-05, """token""": 2941, """token_str""": """ Te"""}, ] , ) @require_torch def a__ ( self ) -> str: _A : Any = pipeline(task="""fill-mask""" , model="""sshleifer/tiny-distilroberta-base""" , top_k=2 , framework="""pt""" ) _A : List[Any] = unmasker("""My name is <mask>""" ) self.assertEqual( nested_simplify(_a , decimals=6 ) , [ {"""sequence""": """My name is Maul""", """score""": 2.2e-05, """token""": 3_5676, """token_str""": """ Maul"""}, {"""sequence""": """My name isELS""", """score""": 2.2e-05, """token""": 1_6416, """token_str""": """ELS"""}, ] , ) _A : Optional[Any] = unmasker("""The largest city in France is <mask>""" ) self.assertEqual( nested_simplify(_a , decimals=6 ) , [ { """sequence""": """The largest city in France is Maul""", """score""": 2.2e-05, """token""": 3_5676, """token_str""": """ Maul""", }, {"""sequence""": """The largest city in France isELS""", """score""": 2.2e-05, """token""": 1_6416, """token_str""": """ELS"""}, ] , ) _A : Optional[int] = unmasker("""My name is <mask>""" , targets=[""" Patrick""", """ Clara""", """ Teven"""] , top_k=3 ) self.assertEqual( nested_simplify(_a , decimals=6 ) , [ {"""sequence""": """My name is Patrick""", """score""": 2.1e-05, """token""": 3499, """token_str""": """ Patrick"""}, {"""sequence""": """My name is Te""", """score""": 2e-05, """token""": 2941, """token_str""": """ Te"""}, {"""sequence""": """My name is Clara""", """score""": 2e-05, """token""": 1_3606, """token_str""": """ Clara"""}, ] , ) _A : str = unmasker("""My name is <mask> <mask>""" , top_k=2 ) self.assertEqual( nested_simplify(_a , decimals=6 ) , [ [ { """score""": 2.2e-05, """token""": 3_5676, """token_str""": """ Maul""", """sequence""": """<s>My name is Maul<mask></s>""", }, {"""score""": 2.2e-05, """token""": 1_6416, """token_str""": """ELS""", """sequence""": """<s>My name isELS<mask></s>"""}, ], [ { """score""": 2.2e-05, """token""": 3_5676, """token_str""": """ Maul""", """sequence""": """<s>My name is<mask> Maul</s>""", }, {"""score""": 2.2e-05, """token""": 1_6416, """token_str""": """ELS""", """sequence""": """<s>My name is<mask>ELS</s>"""}, ], ] , ) @require_torch_gpu def a__ ( self ) -> Union[str, Any]: _A : int = pipeline("""fill-mask""" , model="""hf-internal-testing/tiny-random-distilbert""" , device=0 , framework="""pt""" ) # convert model to fp16 pipe.model.half() _A : Optional[Any] = pipe("""Paris is the [MASK] of France.""" ) # We actually don't care about the result, we just want to make sure # it works, meaning the float16 tensor got casted back to float32 # for postprocessing. self.assertIsInstance(_a , _a ) @slow @require_torch def a__ ( self ) -> Optional[int]: _A : Optional[Any] = pipeline(task="""fill-mask""" , model="""distilroberta-base""" , top_k=2 , framework="""pt""" ) self.run_large_test(_a ) @slow @require_tf def a__ ( self ) -> Tuple: _A : str = pipeline(task="""fill-mask""" , model="""distilroberta-base""" , top_k=2 , framework="""tf""" ) self.run_large_test(_a ) def a__ ( self , _a ) -> Tuple: _A : Optional[int] = unmasker("""My name is <mask>""" ) self.assertEqual( nested_simplify(_a ) , [ {"""sequence""": """My name is John""", """score""": 0.008, """token""": 610, """token_str""": """ John"""}, {"""sequence""": """My name is Chris""", """score""": 0.007, """token""": 1573, """token_str""": """ Chris"""}, ] , ) _A : int = unmasker("""The largest city in France is <mask>""" ) self.assertEqual( nested_simplify(_a ) , [ { """sequence""": """The largest city in France is Paris""", """score""": 0.251, """token""": 2201, """token_str""": """ Paris""", }, { """sequence""": """The largest city in France is Lyon""", """score""": 0.214, """token""": 1_2790, """token_str""": """ Lyon""", }, ] , ) _A : Optional[Any] = unmasker("""My name is <mask>""" , targets=[""" Patrick""", """ Clara""", """ Teven"""] , top_k=3 ) self.assertEqual( nested_simplify(_a ) , [ {"""sequence""": """My name is Patrick""", """score""": 0.005, """token""": 3499, """token_str""": """ Patrick"""}, {"""sequence""": """My name is Clara""", """score""": 0.000, """token""": 1_3606, """token_str""": """ Clara"""}, {"""sequence""": """My name is Te""", """score""": 0.000, """token""": 2941, """token_str""": """ Te"""}, ] , ) @require_torch def a__ ( self ) -> Tuple: _A : List[str] = pipeline(task="""fill-mask""" , model="""sshleifer/tiny-distilroberta-base""" , framework="""pt""" ) _A : str = None _A : Union[str, Any] = None self.run_pipeline_test(_a , [] ) @require_tf def a__ ( self ) -> Union[str, Any]: _A : Tuple = pipeline(task="""fill-mask""" , model="""sshleifer/tiny-distilroberta-base""" , framework="""tf""" ) _A : Any = None _A : Dict = None self.run_pipeline_test(_a , [] ) def a__ ( self , _a , _a , _a ) -> Any: if tokenizer is None or tokenizer.mask_token_id is None: self.skipTest("""The provided tokenizer has no mask token, (probably reformer or wav2vec2)""" ) _A : Optional[Any] = FillMaskPipeline(model=_a , tokenizer=_a ) _A : Tuple = [ F'''This is another {tokenizer.mask_token} test''', ] return fill_masker, examples def a__ ( self , _a , _a ) -> Dict: _A : Dict = fill_masker.tokenizer _A : List[str] = fill_masker.model _A : List[str] = fill_masker( F'''This is a {tokenizer.mask_token}''' , ) self.assertEqual( _a , [ {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, ] , ) _A : Optional[Any] = fill_masker([F'''This is a {tokenizer.mask_token}'''] ) self.assertEqual( _a , [ {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, ] , ) _A : List[str] = fill_masker([F'''This is a {tokenizer.mask_token}''', F'''Another {tokenizer.mask_token} great test.'''] ) self.assertEqual( _a , [ [ {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, ], [ {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, ], ] , ) with self.assertRaises(_a ): fill_masker([None] ) # No mask_token is not supported with self.assertRaises(_a ): fill_masker("""This is""" ) self.run_test_top_k(_a , _a ) self.run_test_targets(_a , _a ) self.run_test_top_k_targets(_a , _a ) self.fill_mask_with_duplicate_targets_and_top_k(_a , _a ) self.fill_mask_with_multiple_masks(_a , _a ) def a__ ( self , _a , _a ) -> List[str]: _A : int = tokenizer.get_vocab() _A : str = sorted(vocab.keys() )[:2] # Pipeline argument _A : Tuple = FillMaskPipeline(model=_a , tokenizer=_a , targets=_a ) _A : Optional[int] = fill_masker(F'''This is a {tokenizer.mask_token}''' ) self.assertEqual( _a , [ {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, ] , ) _A : List[str] = {vocab[el] for el in targets} self.assertEqual({el["""token"""] for el in outputs} , _a ) _A : Union[str, Any] = [tokenizer.decode([x] ) for x in target_ids] self.assertEqual({el["""token_str"""] for el in outputs} , set(_a ) ) # Call argument _A : str = FillMaskPipeline(model=_a , tokenizer=_a ) _A : Optional[int] = fill_masker(F'''This is a {tokenizer.mask_token}''' , targets=_a ) self.assertEqual( _a , [ {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, ] , ) _A : int = {vocab[el] for el in targets} self.assertEqual({el["""token"""] for el in outputs} , _a ) _A : Any = [tokenizer.decode([x] ) for x in target_ids] self.assertEqual({el["""token_str"""] for el in outputs} , set(_a ) ) # Score equivalence _A : int = fill_masker(F'''This is a {tokenizer.mask_token}''' , targets=_a ) _A : Optional[int] = [top_mask["""token_str"""] for top_mask in outputs] _A : Union[str, Any] = [top_mask["""score"""] for top_mask in outputs] # For some BPE tokenizers, `</w>` is removed during decoding, so `token_str` won't be the same as in `targets`. if set(_a ) == set(_a ): _A : Tuple = fill_masker(F'''This is a {tokenizer.mask_token}''' , targets=_a ) _A : Union[str, Any] = [top_mask["""score"""] for top_mask in unmasked_targets] self.assertEqual(nested_simplify(_a ) , nested_simplify(_a ) ) # Raises with invalid with self.assertRaises(_a ): _A : str = fill_masker(F'''This is a {tokenizer.mask_token}''' , targets=[] ) # For some tokenizers, `""` is actually in the vocabulary and the expected error won't raised if "" not in tokenizer.get_vocab(): with self.assertRaises(_a ): _A : Optional[Any] = fill_masker(F'''This is a {tokenizer.mask_token}''' , targets=[""""""] ) with self.assertRaises(_a ): _A : int = fill_masker(F'''This is a {tokenizer.mask_token}''' , targets="""""" ) def a__ ( self , _a , _a ) -> Optional[Any]: _A : str = FillMaskPipeline(model=_a , tokenizer=_a , top_k=2 ) _A : str = fill_masker(F'''This is a {tokenizer.mask_token}''' ) self.assertEqual( _a , [ {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, ] , ) _A : Union[str, Any] = FillMaskPipeline(model=_a , tokenizer=_a ) _A : Union[str, Any] = fill_masker(F'''This is a {tokenizer.mask_token}''' , top_k=2 ) self.assertEqual( _a , [ {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, ] , ) self.assertEqual(nested_simplify(_a ) , nested_simplify(_a ) ) def a__ ( self , _a , _a ) -> List[Any]: _A : Union[str, Any] = tokenizer.get_vocab() _A : int = FillMaskPipeline(model=_a , tokenizer=_a ) # top_k=2, ntargets=3 _A : List[str] = sorted(vocab.keys() )[:3] _A : Tuple = fill_masker(F'''This is a {tokenizer.mask_token}''' , top_k=2 , targets=_a ) # If we use the most probably targets, and filter differently, we should still # have the same results _A : Any = [el["""token_str"""] for el in sorted(_a , key=lambda _a : x["score"] , reverse=_a )] # For some BPE tokenizers, `</w>` is removed during decoding, so `token_str` won't be the same as in `targets`. if set(_a ).issubset(_a ): _A : Any = fill_masker(F'''This is a {tokenizer.mask_token}''' , top_k=3 , targets=_a ) # They should yield exactly the same result self.assertEqual(nested_simplify(_a ) , nested_simplify(_a ) ) def a__ ( self , _a , _a ) -> str: _A : Optional[int] = FillMaskPipeline(model=_a , tokenizer=_a ) _A : List[Any] = tokenizer.get_vocab() # String duplicates + id duplicates _A : Optional[Any] = sorted(vocab.keys() )[:3] _A : Optional[Any] = [targets[0], targets[1], targets[0], targets[2], targets[1]] _A : Union[str, Any] = fill_masker(F'''My name is {tokenizer.mask_token}''' , targets=_a , top_k=10 ) # The target list contains duplicates, so we can't output more # than them self.assertEqual(len(_a ) , 3 ) def a__ ( self , _a , _a ) -> Tuple: _A : Any = FillMaskPipeline(model=_a , tokenizer=_a ) _A : Optional[Any] = fill_masker( F'''This is a {tokenizer.mask_token} {tokenizer.mask_token} {tokenizer.mask_token}''' , top_k=2 ) self.assertEqual( _a , [ [ {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, ], [ {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, ], [ {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, {"""sequence""": ANY(_a ), """score""": ANY(_a ), """token""": ANY(_a ), """token_str""": ANY(_a )}, ], ] , )
54
0
from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging if is_vision_available(): import PIL _snake_case = logging.get_logger(__name__) class lowercase ( UpperCamelCase__ ): _a = ["pixel_values"] def __init__( self , _a = True , _a = None , _a = None , _a = PILImageResampling.BILINEAR , _a = True , _a = 1 / 255 , _a = True , _a = None , _a = None , **_a , ) -> None: super().__init__(**_a ) _A : int = size if size is not None else {"""shortest_edge""": 384} _A : str = get_size_dict(_a , default_to_square=_a ) _A : str = do_resize _A : Optional[Any] = size # Default value set here for backwards compatibility where the value in config is None _A : List[str] = crop_pct if crop_pct is not None else 224 / 256 _A : List[Any] = resample _A : Union[str, Any] = do_rescale _A : Optional[int] = rescale_factor _A : List[Any] = do_normalize _A : int = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN _A : Union[str, Any] = image_std if image_std is not None else IMAGENET_STANDARD_STD def a__ ( self , _a , _a , _a , _a = PILImageResampling.BICUBIC , _a = None , **_a , ) -> np.ndarray: _A : Dict = get_size_dict(_a , default_to_square=_a ) if "shortest_edge" not in size: raise ValueError(F'''Size dictionary must contain \'shortest_edge\' key. Got {size.keys()}''' ) _A : List[str] = size["""shortest_edge"""] if shortest_edge < 384: # maintain same ratio, resizing shortest edge to shortest_edge/crop_pct _A : Any = int(shortest_edge / crop_pct ) _A : Union[str, Any] = get_resize_output_image_size(_a , size=_a , default_to_square=_a ) _A : Any = resize(image=_a , size=_a , resample=_a , data_format=_a , **_a ) # then crop to (shortest_edge, shortest_edge) return center_crop(image=_a , size=(shortest_edge, shortest_edge) , data_format=_a , **_a ) else: # warping (no cropping) when evaluated at 384 or larger return resize( _a , size=(shortest_edge, shortest_edge) , resample=_a , data_format=_a , **_a ) def a__ ( self , _a , _a , _a = None , **_a , ) -> Dict: return rescale(_a , scale=_a , data_format=_a , **_a ) def a__ ( self , _a , _a , _a , _a = None , **_a , ) -> np.ndarray: return normalize(_a , mean=_a , std=_a , data_format=_a , **_a ) def a__ ( self , _a , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = None , _a = ChannelDimension.FIRST , **_a , ) -> PIL.Image.Image: _A : Optional[int] = do_resize if do_resize is not None else self.do_resize _A : str = crop_pct if crop_pct is not None else self.crop_pct _A : Tuple = resample if resample is not None else self.resample _A : Union[str, Any] = do_rescale if do_rescale is not None else self.do_rescale _A : Any = rescale_factor if rescale_factor is not None else self.rescale_factor _A : Union[str, Any] = do_normalize if do_normalize is not None else self.do_normalize _A : Tuple = image_mean if image_mean is not None else self.image_mean _A : List[str] = image_std if image_std is not None else self.image_std _A : Any = size if size is not None else self.size _A : str = get_size_dict(_a , default_to_square=_a ) _A : List[str] = make_list_of_images(_a ) if not valid_images(_a ): 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_resize and size["shortest_edge"] < 384 and crop_pct is None: raise ValueError("""crop_pct must be specified if size < 384.""" ) 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. _A : List[Any] = [to_numpy_array(_a ) for image in images] if do_resize: _A : Dict = [self.resize(image=_a , size=_a , crop_pct=_a , resample=_a ) for image in images] if do_rescale: _A : Optional[int] = [self.rescale(image=_a , scale=_a ) for image in images] if do_normalize: _A : Optional[int] = [self.normalize(image=_a , mean=_a , std=_a ) for image in images] _A : Union[str, Any] = [to_channel_dimension_format(_a , _a ) for image in images] _A : Tuple = {"""pixel_values""": images} return BatchFeature(data=_a , tensor_type=_a )
702
import json import os import unittest from transformers import CLIPTokenizer, CLIPTokenizerFast from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES from transformers.testing_utils import require_ftfy, require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class lowercase ( UpperCamelCase__,unittest.TestCase ): _a = CLIPTokenizer _a = CLIPTokenizerFast _a = True _a = {} _a = False def a__ ( self ) -> Optional[Any]: super().setUp() # fmt: off _A : int = ["""l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """lo""", """l</w>""", """w</w>""", """r</w>""", """t</w>""", """low</w>""", """er</w>""", """lowest</w>""", """newer</w>""", """wider""", """<unk>""", """<|startoftext|>""", """<|endoftext|>"""] # fmt: on _A : str = dict(zip(_a , range(len(_a ) ) ) ) _A : Optional[int] = ["""#version: 0.2""", """l o""", """lo w</w>""", """e r</w>"""] _A : str = {"""unk_token""": """<unk>"""} _A : Union[str, Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""vocab_file"""] ) _A : Union[str, Any] = 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(_a ) + """\n""" ) with open(self.merges_file , """w""" , encoding="""utf-8""" ) as fp: fp.write("""\n""".join(_a ) ) def a__ ( self , **_a ) -> List[Any]: kwargs.update(self.special_tokens_map ) return CLIPTokenizer.from_pretrained(self.tmpdirname , **_a ) def a__ ( self , **_a ) -> Union[str, Any]: kwargs.update(self.special_tokens_map ) return CLIPTokenizerFast.from_pretrained(self.tmpdirname , **_a ) def a__ ( self , _a ) -> str: _A : Tuple = """lower newer""" _A : Optional[Any] = """lower newer""" return input_text, output_text def a__ ( self ) -> List[Any]: _A : Optional[int] = CLIPTokenizer(self.vocab_file , self.merges_file , **self.special_tokens_map ) _A : str = """lower newer""" _A : List[str] = ["""lo""", """w""", """er</w>""", """n""", """e""", """w""", """er</w>"""] _A : int = tokenizer.tokenize(_a ) self.assertListEqual(_a , _a ) _A : str = tokens + [tokenizer.unk_token] _A : Dict = [10, 2, 16, 9, 3, 2, 16, 20] self.assertListEqual(tokenizer.convert_tokens_to_ids(_a ) , _a ) @require_ftfy def a__ ( self ) -> Tuple: for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): _A : Dict = self.tokenizer_class.from_pretrained(_a , **_a ) _A : str = self.rust_tokenizer_class.from_pretrained(_a , **_a ) _A : List[str] = """A\n'll 11p223RF☆ho!!to?'d'd''d of a cat to-$''d.""" _A : Dict = tokenizer_s.tokenize(_a ) _A : Dict = tokenizer_r.tokenize(_a ) self.assertListEqual(_a , _a ) # Test that the tokenization is identical on an example containing a character (Latin Small Letter A # with Tilde) encoded in 2 different ways _A : Tuple = """xa\u0303y""" + """ """ + """x\xe3y""" _A : Dict = tokenizer_s.tokenize(_a ) _A : Dict = tokenizer_r.tokenize(_a ) self.assertListEqual(_a , _a ) # Test that the tokenization is identical on unicode of space type _A : Any = [ """\u0009""", # (horizontal tab, '\t') """\u000B""", # (vertical tab) """\u000C""", # (form feed) """\u0020""", # (space, ' ') """\u200E""", # (left-to-right mark):w """\u200F""", # (right-to-left mark) ] for unicode_seq in spaces_unicodes: _A : Optional[int] = tokenizer_s.tokenize(_a ) _A : List[Any] = tokenizer_r.tokenize(_a ) self.assertListEqual(_a , _a ) # Test that the tokenization is identical on unicode of line break type _A : int = [ """\u000A""", # (line feed, '\n') """\r\n""", # (carriage return and line feed, '\r\n') """\u000D""", # (carriage return, '\r') """\r""", # (carriage return, '\r') """\u000D""", # (carriage return, '\r') """\u2028""", # (line separator) """\u2029""", # (paragraph separator) # "\u0085", # (next line) ] # The tokenization is not identical for the character "\u0085" (next line). The slow version using ftfy transforms # it into the Horizontal Ellipsis character "…" ("\u2026") while the fast version transforms it into a # space (and thus into an empty list). for unicode_seq in line_break_unicodes: _A : Optional[Any] = tokenizer_s.tokenize(_a ) _A : Tuple = tokenizer_r.tokenize(_a ) self.assertListEqual(_a , _a ) def a__ ( self ) -> Any: # Test which aims to verify that the offsets are well adapted to the argument `add_prefix_space` for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): _A : List[Any] = """hello""" # `hello` is a token in the vocabulary of `pretrained_name` _A : str = F'''{text_of_1_token} {text_of_1_token}''' _A : str = self.rust_tokenizer_class.from_pretrained( _a , use_fast=_a , ) _A : Dict = tokenizer_r(_a , return_offsets_mapping=_a , add_special_tokens=_a ) self.assertEqual(encoding.offset_mapping[0] , (0, len(_a )) ) self.assertEqual( encoding.offset_mapping[1] , (len(_a ) + 1, len(_a ) + 1 + len(_a )) , ) _A : Any = F''' {text}''' _A : List[Any] = self.rust_tokenizer_class.from_pretrained( _a , use_fast=_a , ) _A : Optional[int] = tokenizer_r(_a , return_offsets_mapping=_a , add_special_tokens=_a ) self.assertEqual(encoding.offset_mapping[0] , (1, 1 + len(_a )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(_a ) + 1, 1 + len(_a ) + 1 + len(_a )) , ) def a__ ( self ) -> List[Any]: # Test related to the breaking change introduced in transformers v4.17.0 # We need to check that an error in raised when the user try to load a previous version of the tokenizer. with self.assertRaises(_a ) as context: self.rust_tokenizer_class.from_pretrained("""robot-test/old-clip-tokenizer""" ) self.assertTrue( context.exception.args[0].startswith( """The `backend_tokenizer` provided does not match the expected format.""" ) ) @require_ftfy def a__ ( self ) -> str: super().test_tokenization_python_rust_equals() def a__ ( self ) -> Union[str, Any]: # CLIP always lower cases letters pass
54
0
def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_,snake_case_,snake_case_,): _A : Dict = [redshift, radiation_density, matter_density, dark_energy] if any(p < 0 for p in parameters ): raise ValueError("""All input parameters must be positive""" ) if any(p > 1 for p in parameters[1:4] ): raise ValueError("""Relative densities cannot be greater than one""" ) else: _A : str = 1 - (matter_density + radiation_density + dark_energy) _A : Tuple = ( radiation_density * (redshift + 1) ** 4 + matter_density * (redshift + 1) ** 3 + curvature * (redshift + 1) ** 2 + dark_energy ) _A : int = hubble_constant * e_a ** (1 / 2) return hubble if __name__ == "__main__": import doctest # run doctest doctest.testmod() # demo LCDM approximation _snake_case = 0.3 print( hubble_parameter( hubble_constant=68.3, radiation_density=1e-4, matter_density=matter_density, dark_energy=1 - matter_density, redshift=0, ) )
703
from datetime import datetime as dt import os from github import Github _snake_case = [ "good first issue", "good second issue", "good difficult issue", "feature request", "new model", "wip", ] def lowerCAmelCase_ ( ): _A : int = Github(os.environ["""GITHUB_TOKEN"""] ) _A : Tuple = g.get_repo("""huggingface/transformers""" ) _A : Dict = repo.get_issues(state="""open""" ) for issue in open_issues: _A : Optional[Any] = sorted([comment for comment in issue.get_comments()],key=lambda snake_case_ : i.created_at,reverse=snake_case_ ) _A : Dict = comments[0] if len(snake_case_ ) > 0 else None if ( last_comment is not None and last_comment.user.login == "github-actions[bot]" and (dt.utcnow() - issue.updated_at).days > 7 and (dt.utcnow() - issue.created_at).days >= 30 and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels() ) ): # print(f"Would close issue {issue.number} since it has been 7 days of inactivity since bot mention.") issue.edit(state="""closed""" ) elif ( (dt.utcnow() - issue.updated_at).days > 23 and (dt.utcnow() - issue.created_at).days >= 30 and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels() ) ): # print(f"Would add stale comment to {issue.number}") issue.create_comment( """This issue has been automatically marked as stale because it has not had """ """recent activity. If you think this still needs to be addressed """ """please comment on this thread.\n\nPlease note that issues that do not follow the """ """[contributing guidelines](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) """ """are likely to be ignored.""" ) if __name__ == "__main__": main()
54
0
from __future__ import annotations def lowerCAmelCase_ ( snake_case_,snake_case_ ): _A : Union[str, Any] = position _A : Dict = [ (y + 1, x + 2), (y - 1, x + 2), (y + 1, x - 2), (y - 1, x - 2), (y + 2, x + 1), (y + 2, x - 1), (y - 2, x + 1), (y - 2, x - 1), ] _A : int = [] for position in positions: _A : Optional[int] = position if 0 <= y_test < n and 0 <= x_test < n: permissible_positions.append(snake_case_ ) return permissible_positions def lowerCAmelCase_ ( snake_case_ ): return not any(elem == 0 for row in board for elem in row ) def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_ ): if is_complete(snake_case_ ): return True for position in get_valid_pos(snake_case_,len(snake_case_ ) ): _A : List[Any] = position if board[y][x] == 0: _A : Tuple = curr + 1 if open_knight_tour_helper(snake_case_,snake_case_,curr + 1 ): return True _A : int = 0 return False def lowerCAmelCase_ ( snake_case_ ): _A : int = [[0 for i in range(snake_case_ )] for j in range(snake_case_ )] for i in range(snake_case_ ): for j in range(snake_case_ ): _A : int = 1 if open_knight_tour_helper(snake_case_,(i, j),1 ): return board _A : List[str] = 0 _A : List[str] = f'''Open Kight Tour cannot be performed on a board of size {n}''' raise ValueError(snake_case_ ) if __name__ == "__main__": import doctest doctest.testmod()
704
from __future__ import annotations class lowercase : def __init__( self , _a = 0 ) -> str: _A : Any = key def a__ ( self , _a , _a ) -> list[str]: assert isinstance(_a , _a ) and isinstance(_a , _a ) _A : Any = key or self.__key or 1 # make sure key is an appropriate size key %= 255 return [chr(ord(_a ) ^ key ) for ch in content] def a__ ( self , _a , _a ) -> list[str]: assert isinstance(_a , _a ) and isinstance(_a , _a ) _A : List[Any] = key or self.__key or 1 # make sure key is an appropriate size key %= 255 return [chr(ord(_a ) ^ key ) for ch in content] def a__ ( self , _a , _a = 0 ) -> str: assert isinstance(_a , _a ) and isinstance(_a , _a ) _A : List[Any] = key or self.__key or 1 # make sure key can be any size while key > 255: key -= 255 # This will be returned _A : List[str] = """""" for ch in content: ans += chr(ord(_a ) ^ key ) return ans def a__ ( self , _a , _a = 0 ) -> str: assert isinstance(_a , _a ) and isinstance(_a , _a ) _A : List[str] = key or self.__key or 1 # make sure key can be any size while key > 255: key -= 255 # This will be returned _A : List[str] = """""" for ch in content: ans += chr(ord(_a ) ^ key ) return ans def a__ ( self , _a , _a = 0 ) -> bool: assert isinstance(_a , _a ) and isinstance(_a , _a ) try: with open(_a ) as fin, open("""encrypt.out""" , """w+""" ) as fout: # actual encrypt-process for line in fin: fout.write(self.encrypt_string(_a , _a ) ) except OSError: return False return True def a__ ( self , _a , _a ) -> bool: assert isinstance(_a , _a ) and isinstance(_a , _a ) try: with open(_a ) as fin, open("""decrypt.out""" , """w+""" ) as fout: # actual encrypt-process for line in fin: fout.write(self.decrypt_string(_a , _a ) ) except OSError: return False return True # Tests # crypt = XORCipher() # key = 67 # # test encrypt # print(crypt.encrypt("hallo welt",key)) # # test decrypt # print(crypt.decrypt(crypt.encrypt("hallo welt",key), key)) # # test encrypt_string # print(crypt.encrypt_string("hallo welt",key)) # # test decrypt_string # print(crypt.decrypt_string(crypt.encrypt_string("hallo welt",key),key)) # if (crypt.encrypt_file("test.txt",key)): # print("encrypt successful") # else: # print("encrypt unsuccessful") # if (crypt.decrypt_file("encrypt.out",key)): # print("decrypt successful") # else: # print("decrypt unsuccessful")
54
0
import os import re import shutil import sys import tempfile import unittest import black _snake_case = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) sys.path.append(os.path.join(git_repo_path, "utils")) import check_copies # noqa: E402 # This is the reference code that will be used in the tests. # If BertLMPredictionHead is changed in modeling_bert.py, this code needs to be manually updated. _snake_case = " def __init__(self, config):\n super().__init__()\n self.transform = BertPredictionHeadTransform(config)\n\n # The output weights are the same as the input embeddings, but there is\n # an output-only bias for each token.\n self.decoder = nn.Linear(config.hidden_size, config.vocab_size, bias=False)\n\n self.bias = nn.Parameter(torch.zeros(config.vocab_size))\n\n # Need a link between the two variables so that the bias is correctly resized with `resize_token_embeddings`\n self.decoder.bias = self.bias\n\n def forward(self, hidden_states):\n hidden_states = self.transform(hidden_states)\n hidden_states = self.decoder(hidden_states)\n return hidden_states\n" class lowercase ( unittest.TestCase ): def a__ ( self ) -> Union[str, Any]: _A : List[str] = tempfile.mkdtemp() os.makedirs(os.path.join(self.transformer_dir , """models/bert/""" ) ) _A : str = self.transformer_dir shutil.copy( os.path.join(_a , """src/transformers/models/bert/modeling_bert.py""" ) , os.path.join(self.transformer_dir , """models/bert/modeling_bert.py""" ) , ) def a__ ( self ) -> Optional[int]: _A : List[str] = """src/transformers""" shutil.rmtree(self.transformer_dir ) def a__ ( self , _a , _a , _a , _a=None ) -> Optional[Any]: _A : Optional[Any] = comment + F'''\nclass {class_name}(nn.Module):\n''' + class_code if overwrite_result is not None: _A : List[str] = comment + F'''\nclass {class_name}(nn.Module):\n''' + overwrite_result _A : List[str] = black.Mode(target_versions={black.TargetVersion.PYaa} , line_length=119 ) _A : Optional[int] = black.format_str(_a , mode=_a ) _A : Optional[Any] = os.path.join(self.transformer_dir , """new_code.py""" ) with open(_a , """w""" , newline="""\n""" ) as f: f.write(_a ) if overwrite_result is None: self.assertTrue(len(check_copies.is_copy_consistent(_a ) ) == 0 ) else: check_copies.is_copy_consistent(f.name , overwrite=_a ) with open(_a , """r""" ) as f: self.assertTrue(f.read() , _a ) def a__ ( self ) -> str: _A : Union[str, Any] = check_copies.find_code_in_transformers("""models.bert.modeling_bert.BertLMPredictionHead""" ) self.assertEqual(_a , _a ) def a__ ( self ) -> int: # Base copy consistency self.check_copy_consistency( """# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead""" , """BertLMPredictionHead""" , REFERENCE_CODE + """\n""" , ) # With no empty line at the end self.check_copy_consistency( """# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead""" , """BertLMPredictionHead""" , _a , ) # Copy consistency with rename self.check_copy_consistency( """# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->TestModel""" , """TestModelLMPredictionHead""" , re.sub("""Bert""" , """TestModel""" , _a ) , ) # Copy consistency with a really long name _A : List[str] = """TestModelWithAReallyLongNameBecauseSomePeopleLikeThatForSomeReason""" self.check_copy_consistency( F'''# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->{long_class_name}''' , F'''{long_class_name}LMPredictionHead''' , re.sub("""Bert""" , _a , _a ) , ) # Copy consistency with overwrite self.check_copy_consistency( """# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->TestModel""" , """TestModelLMPredictionHead""" , _a , overwrite_result=re.sub("""Bert""" , """TestModel""" , _a ) , ) def a__ ( self ) -> Tuple: _A : Union[str, Any] = check_copies.LOCALIZED_READMES["""README_zh-hans.md"""] _A : str = ( """1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (from Google Research and the""" """ Toyota Technological Institute at Chicago) released with the paper [ALBERT: A Lite BERT for""" """ Self-supervised Learning of Language Representations](https://arxiv.org/abs/1909.11942), by Zhenzhong""" """ Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut.\n1.""" """ **[DistilBERT](https://huggingface.co/transformers/model_doc/distilbert.html)** (from HuggingFace),""" """ released together with the paper [DistilBERT, a distilled version of BERT: smaller, faster, cheaper and""" """ lighter](https://arxiv.org/abs/1910.01108) by Victor Sanh, Lysandre Debut and Thomas Wolf. The same""" """ method has been applied to compress GPT2 into""" """ [DistilGPT2](https://github.com/huggingface/transformers/tree/main/examples/distillation), RoBERTa into""" """ [DistilRoBERTa](https://github.com/huggingface/transformers/tree/main/examples/distillation),""" """ Multilingual BERT into""" """ [DistilmBERT](https://github.com/huggingface/transformers/tree/main/examples/distillation) and a German""" """ version of DistilBERT.\n1. **[ELECTRA](https://huggingface.co/transformers/model_doc/electra.html)**""" """ (from Google Research/Stanford University) released with the paper [ELECTRA: Pre-training text encoders""" """ as discriminators rather than generators](https://arxiv.org/abs/2003.10555) by Kevin Clark, Minh-Thang""" """ Luong, Quoc V. Le, Christopher D. Manning.""" ) _A : str = ( """1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the""" """ Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of""" """ Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian""" """ Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n""" ) _A : Any = ( """1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the""" """ Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of""" """ Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian""" """ Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n1.""" """ **[DistilBERT](https://huggingface.co/transformers/model_doc/distilbert.html)** (来自 HuggingFace) 伴随论文""" """ [DistilBERT, a distilled version of BERT: smaller, faster, cheaper and""" """ lighter](https://arxiv.org/abs/1910.01108) 由 Victor Sanh, Lysandre Debut and Thomas Wolf 发布。 The same""" """ method has been applied to compress GPT2 into""" """ [DistilGPT2](https://github.com/huggingface/transformers/tree/main/examples/distillation), RoBERTa into""" """ [DistilRoBERTa](https://github.com/huggingface/transformers/tree/main/examples/distillation),""" """ Multilingual BERT into""" """ [DistilmBERT](https://github.com/huggingface/transformers/tree/main/examples/distillation) and a German""" """ version of DistilBERT.\n1. **[ELECTRA](https://huggingface.co/transformers/model_doc/electra.html)** (来自""" """ Google Research/Stanford University) 伴随论文 [ELECTRA: Pre-training text encoders as discriminators rather""" """ than generators](https://arxiv.org/abs/2003.10555) 由 Kevin Clark, Minh-Thang Luong, Quoc V. Le,""" """ Christopher D. Manning 发布。\n""" ) _A : Tuple = check_copies.convert_to_localized_md( _a , _a , localized_readme["""format_model_list"""] ) self.assertFalse(_a ) self.assertEqual(_a , _a ) _A : List[str] = check_copies.convert_to_localized_md( _a , _a , localized_readme["""format_model_list"""] ) # Check whether the number of models is equal to README.md after conversion. self.assertTrue(_a ) _A : Tuple = ( """1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (from Google Research and the""" """ Toyota Technological Institute at Chicago) released with the paper [ALBERT: A Lite BERT for""" """ Self-supervised Learning of Language Representations](https://arxiv.org/abs/1909.11942), by Zhenzhong""" """ Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut.""" ) _A : Dict = ( """1. **[ALBERT](https://huggingface.co/transformers/main/model_doc/albert.html)** (来自 Google Research and""" """ the Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of""" """ Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian""" """ Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n""" ) _A : Optional[Any] = ( """1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the""" """ Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of""" """ Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian""" """ Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n""" ) _A : Optional[int] = check_copies.convert_to_localized_md( _a , _a , localized_readme["""format_model_list"""] ) # Check if the model link is synchronized. self.assertEqual(_a , _a )
705
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 = random.Random() def lowerCAmelCase_ ( snake_case_,snake_case_=1.0,snake_case_=None,snake_case_=None ): if rng is None: _A : str = global_rng _A : List[Any] = [] 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 lowercase ( unittest.TestCase ): def __init__( self , _a , _a=7 , _a=400 , _a=2000 , _a=10 , _a=160 , _a=8 , _a=0.0 , _a=4000 , _a=False , _a=True , ) -> Optional[int]: _A : Any = parent _A : List[Any] = batch_size _A : List[Any] = min_seq_length _A : Dict = max_seq_length _A : Optional[Any] = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) _A : Tuple = padding_value _A : Tuple = sampling_rate _A : str = return_attention_mask _A : Any = do_normalize _A : Union[str, Any] = feature_size _A : List[Any] = chunk_length _A : List[Any] = hop_length def a__ ( self ) -> List[str]: 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 a__ ( self , _a=False , _a=False ) -> List[str]: def _flatten(_a ): return list(itertools.chain(*_a ) ) 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 : int = [ 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 : Any = [np.asarray(_a ) for x in speech_inputs] return speech_inputs @require_torch @require_torchaudio class lowercase ( UpperCamelCase__,unittest.TestCase ): _a = WhisperFeatureExtractor if is_speech_available() else None def a__ ( self ) -> Tuple: _A : Optional[int] = WhisperFeatureExtractionTester(self ) def a__ ( self ) -> Optional[Any]: _A : List[Any] = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: _A : List[str] = feat_extract_first.save_pretrained(_a )[0] check_json_file_has_correct_format(_a ) _A : Optional[int] = self.feature_extraction_class.from_pretrained(_a ) _A : Tuple = feat_extract_first.to_dict() _A : List[Any] = feat_extract_second.to_dict() _A : List[Any] = feat_extract_first.mel_filters _A : Union[str, Any] = feat_extract_second.mel_filters self.assertTrue(np.allclose(_a , _a ) ) self.assertEqual(_a , _a ) def a__ ( self ) -> Dict: _A : Any = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: _A : Dict = os.path.join(_a , """feat_extract.json""" ) feat_extract_first.to_json_file(_a ) _A : Optional[int] = self.feature_extraction_class.from_json_file(_a ) _A : str = feat_extract_first.to_dict() _A : Any = feat_extract_second.to_dict() _A : Union[str, Any] = feat_extract_first.mel_filters _A : Union[str, Any] = feat_extract_second.mel_filters self.assertTrue(np.allclose(_a , _a ) ) self.assertEqual(_a , _a ) def a__ ( self ) -> Union[str, Any]: # Tests that all call wrap to encode_plus and batch_encode_plus _A : Optional[int] = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 _A : Optional[Any] = [floats_list((1, x) )[0] for x in range(800 , 1400 , 200 )] _A : Any = [np.asarray(_a ) for speech_input in speech_inputs] # Test feature size _A : Dict = feature_extractor(_a , 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 : List[Any] = feature_extractor(speech_inputs[0] , return_tensors="""np""" ).input_features _A : List[str] = feature_extractor(np_speech_inputs[0] , return_tensors="""np""" ).input_features self.assertTrue(np.allclose(_a , _a , atol=1e-3 ) ) # Test batched _A : Union[str, Any] = feature_extractor(_a , return_tensors="""np""" ).input_features _A : Tuple = feature_extractor(_a , return_tensors="""np""" ).input_features for enc_seq_a, enc_seq_a in zip(_a , _a ): self.assertTrue(np.allclose(_a , _a , atol=1e-3 ) ) # Test 2-D numpy arrays are batched. _A : List[str] = [floats_list((1, x) )[0] for x in (800, 800, 800)] _A : Any = np.asarray(_a ) _A : Union[str, Any] = feature_extractor(_a , return_tensors="""np""" ).input_features _A : int = feature_extractor(_a , return_tensors="""np""" ).input_features for enc_seq_a, enc_seq_a in zip(_a , _a ): self.assertTrue(np.allclose(_a , _a , atol=1e-3 ) ) # Test truncation required _A : List[Any] = [floats_list((1, x) )[0] for x in range(200 , (feature_extractor.n_samples + 500) , 200 )] _A : Union[str, Any] = [np.asarray(_a ) for speech_input in speech_inputs] _A : Tuple = [x[: feature_extractor.n_samples] for x in speech_inputs] _A : Union[str, Any] = [np.asarray(_a ) for speech_input in speech_inputs_truncated] _A : Optional[int] = feature_extractor(_a , return_tensors="""np""" ).input_features _A : List[Any] = feature_extractor(_a , return_tensors="""np""" ).input_features for enc_seq_a, enc_seq_a in zip(_a , _a ): self.assertTrue(np.allclose(_a , _a , atol=1e-3 ) ) def a__ ( self ) -> Dict: import torch _A : Dict = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) _A : Optional[int] = np.random.rand(100 , 32 ).astype(np.floataa ) _A : str = np_speech_inputs.tolist() for inputs in [py_speech_inputs, np_speech_inputs]: _A : Optional[Any] = feature_extractor.pad([{"""input_features""": inputs}] , return_tensors="""np""" ) self.assertTrue(np_processed.input_features.dtype == np.floataa ) _A : Optional[int] = feature_extractor.pad([{"""input_features""": inputs}] , return_tensors="""pt""" ) self.assertTrue(pt_processed.input_features.dtype == torch.floataa ) def a__ ( self , _a ) -> Dict: _A : Optional[Any] = load_dataset("""hf-internal-testing/librispeech_asr_dummy""" , """clean""" , split="""validation""" ) # automatic decoding with librispeech _A : Optional[Any] = ds.sort("""id""" ).select(range(_a ) )[:num_samples]["""audio"""] return [x["array"] for x in speech_samples] def a__ ( self ) -> Tuple: # fmt: off _A : Dict = torch.tensor( [ 0.1193, -0.0946, -0.1098, -0.0196, 0.0225, -0.0690, -0.1736, 0.0951, 0.0971, -0.0817, -0.0702, 0.0162, 0.0260, 0.0017, -0.0192, -0.1678, 0.0709, -0.1867, -0.0655, -0.0274, -0.0234, -0.1884, -0.0516, -0.0554, -0.0274, -0.1425, -0.1423, 0.0837, 0.0377, -0.0854 ] ) # fmt: on _A : Dict = self._load_datasamples(1 ) _A : Optional[Any] = WhisperFeatureExtractor() _A : Optional[Any] = feature_extractor(_a , return_tensors="""pt""" ).input_features self.assertEqual(input_features.shape , (1, 80, 3000) ) self.assertTrue(torch.allclose(input_features[0, 0, :30] , _a , atol=1e-4 ) ) def a__ ( self ) -> str: _A : Dict = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) _A : str = self._load_datasamples(1 )[0] _A : Union[str, Any] = ((audio - audio.min()) / (audio.max() - audio.min())) * 6_5535 # Rescale to [0, 65535] to show issue _A : List[Any] = feat_extract.zero_mean_unit_var_norm([audio] , attention_mask=_a )[0] self.assertTrue(np.all(np.mean(_a ) < 1e-3 ) ) self.assertTrue(np.all(np.abs(np.var(_a ) - 1 ) < 1e-3 ) )
54
0
from dataclasses import dataclass from typing import Optional, Tuple, Union import torch import torch.nn as nn from ..configuration_utils import ConfigMixin, register_to_config from ..utils import BaseOutput from .embeddings import GaussianFourierProjection, TimestepEmbedding, Timesteps from .modeling_utils import ModelMixin from .unet_ad_blocks import get_down_block, get_mid_block, get_out_block, get_up_block @dataclass class lowercase ( UpperCamelCase__ ): _a = 4_2 class lowercase ( UpperCamelCase__,UpperCamelCase__ ): @register_to_config def __init__( self , _a = 6_5536 , _a = None , _a = 2 , _a = 2 , _a = 0 , _a = "fourier" , _a = True , _a = False , _a = 0.0 , _a = ("DownBlock1DNoSkip", "DownBlock1D", "AttnDownBlock1D") , _a = ("AttnUpBlock1D", "UpBlock1D", "UpBlock1DNoSkip") , _a = "UNetMidBlock1D" , _a = None , _a = (32, 32, 64) , _a = None , _a = 8 , _a = 1 , _a = False , ) -> Optional[Any]: super().__init__() _A : Optional[int] = sample_size # time if time_embedding_type == "fourier": _A : Union[str, Any] = GaussianFourierProjection( embedding_size=8 , set_W_to_weight=_a , log=_a , flip_sin_to_cos=_a ) _A : Any = 2 * block_out_channels[0] elif time_embedding_type == "positional": _A : str = Timesteps( block_out_channels[0] , flip_sin_to_cos=_a , downscale_freq_shift=_a ) _A : int = block_out_channels[0] if use_timestep_embedding: _A : List[str] = block_out_channels[0] * 4 _A : str = TimestepEmbedding( in_channels=_a , time_embed_dim=_a , act_fn=_a , out_dim=block_out_channels[0] , ) _A : Dict = nn.ModuleList([] ) _A : Optional[int] = None _A : List[Any] = nn.ModuleList([] ) _A : Optional[int] = None # down _A : Union[str, Any] = in_channels for i, down_block_type in enumerate(_a ): _A : List[str] = output_channel _A : str = block_out_channels[i] if i == 0: input_channel += extra_in_channels _A : Any = i == len(_a ) - 1 _A : List[str] = get_down_block( _a , num_layers=_a , in_channels=_a , out_channels=_a , temb_channels=block_out_channels[0] , add_downsample=not is_final_block or downsample_each_block , ) self.down_blocks.append(_a ) # mid _A : Union[str, Any] = get_mid_block( _a , in_channels=block_out_channels[-1] , mid_channels=block_out_channels[-1] , out_channels=block_out_channels[-1] , embed_dim=block_out_channels[0] , num_layers=_a , add_downsample=_a , ) # up _A : List[Any] = list(reversed(_a ) ) _A : str = reversed_block_out_channels[0] if out_block_type is None: _A : List[str] = out_channels else: _A : Tuple = block_out_channels[0] for i, up_block_type in enumerate(_a ): _A : str = output_channel _A : Union[str, Any] = ( reversed_block_out_channels[i + 1] if i < len(_a ) - 1 else final_upsample_channels ) _A : Any = i == len(_a ) - 1 _A : Any = get_up_block( _a , num_layers=_a , in_channels=_a , out_channels=_a , temb_channels=block_out_channels[0] , add_upsample=not is_final_block , ) self.up_blocks.append(_a ) _A : Tuple = output_channel # out _A : Optional[int] = norm_num_groups if norm_num_groups is not None else min(block_out_channels[0] // 4 , 32 ) _A : Optional[Any] = get_out_block( out_block_type=_a , num_groups_out=_a , embed_dim=block_out_channels[0] , out_channels=_a , act_fn=_a , fc_dim=block_out_channels[-1] // 4 , ) def a__ ( self , _a , _a , _a = True , ) -> Union[UNetaDOutput, Tuple]: _A : str = timestep if not torch.is_tensor(_a ): _A : str = torch.tensor([timesteps] , dtype=torch.long , device=sample.device ) elif torch.is_tensor(_a ) and len(timesteps.shape ) == 0: _A : int = timesteps[None].to(sample.device ) _A : Union[str, Any] = self.time_proj(_a ) if self.config.use_timestep_embedding: _A : List[str] = self.time_mlp(_a ) else: _A : Optional[Any] = timestep_embed[..., None] _A : Any = timestep_embed.repeat([1, 1, sample.shape[2]] ).to(sample.dtype ) _A : str = timestep_embed.broadcast_to((sample.shape[:1] + timestep_embed.shape[1:]) ) # 2. down _A : int = () for downsample_block in self.down_blocks: _A : Optional[Any] = downsample_block(hidden_states=_a , temb=_a ) down_block_res_samples += res_samples # 3. mid if self.mid_block: _A : Dict = self.mid_block(_a , _a ) # 4. up for i, upsample_block in enumerate(self.up_blocks ): _A : str = down_block_res_samples[-1:] _A : Union[str, Any] = down_block_res_samples[:-1] _A : Optional[Any] = upsample_block(_a , res_hidden_states_tuple=_a , temb=_a ) # 5. post-process if self.out_block: _A : Any = self.out_block(_a , _a ) if not return_dict: return (sample,) return UNetaDOutput(sample=_a )
706
def lowerCAmelCase_ ( snake_case_,snake_case_ ): _A : Union[str, Any] = """""" for i in table: res += inp[i - 1] return res def lowerCAmelCase_ ( snake_case_ ): return data[1:] + data[0] def lowerCAmelCase_ ( snake_case_,snake_case_ ): _A : Dict = """""" for i in range(len(snake_case_ ) ): if a[i] == b[i]: res += "0" else: res += "1" return res def lowerCAmelCase_ ( snake_case_,snake_case_ ): _A : int = int("""0b""" + data[0] + data[-1],2 ) _A : Any = int("""0b""" + data[1:3],2 ) return bin(s[row][col] )[2:] def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_,snake_case_,snake_case_ ): _A : List[str] = message[:4] _A : List[Any] = message[4:] _A : Union[str, Any] = apply_table(snake_case_,snake_case_ ) _A : List[Any] = xor(snake_case_,snake_case_ ) _A : Optional[Any] = apply_sbox(snake_case_,temp[:4] ) # noqa: E741 _A : List[Any] = apply_sbox(snake_case_,temp[4:] ) _A : int = """0""" * (2 - len(snake_case_ )) + l # noqa: E741 _A : Union[str, Any] = """0""" * (2 - len(snake_case_ )) + r _A : List[Any] = apply_table(l + r,snake_case_ ) _A : Any = xor(snake_case_,snake_case_ ) return temp + right if __name__ == "__main__": _snake_case = input("Enter 10 bit key: ") _snake_case = input("Enter 8 bit message: ") _snake_case = [6, 3, 7, 4, 8, 5, 10, 9] _snake_case = [3, 5, 2, 7, 4, 10, 1, 9, 8, 6] _snake_case = [2, 4, 3, 1] _snake_case = [2, 6, 3, 1, 4, 8, 5, 7] _snake_case = [4, 1, 3, 5, 7, 2, 8, 6] _snake_case = [4, 1, 2, 3, 2, 3, 4, 1] _snake_case = [[1, 0, 3, 2], [3, 2, 1, 0], [0, 2, 1, 3], [3, 1, 3, 2]] _snake_case = [[0, 1, 2, 3], [2, 0, 1, 3], [3, 0, 1, 0], [2, 1, 0, 3]] # key generation _snake_case = apply_table(key, paa_table) _snake_case = temp[:5] _snake_case = temp[5:] _snake_case = left_shift(left) _snake_case = left_shift(right) _snake_case = apply_table(left + right, pa_table) _snake_case = left_shift(left) _snake_case = left_shift(right) _snake_case = left_shift(left) _snake_case = left_shift(right) _snake_case = apply_table(left + right, pa_table) # encryption _snake_case = apply_table(message, IP) _snake_case = function(expansion, sa, sa, keya, temp) _snake_case = temp[4:] + temp[:4] _snake_case = function(expansion, sa, sa, keya, temp) _snake_case = apply_table(temp, IP_inv) print("Cipher text is:", CT) # decryption _snake_case = apply_table(CT, IP) _snake_case = function(expansion, sa, sa, keya, temp) _snake_case = temp[4:] + temp[:4] _snake_case = function(expansion, sa, sa, keya, temp) _snake_case = apply_table(temp, IP_inv) print("Plain text after decypting is:", PT)
54
0
def lowerCAmelCase_ ( snake_case_ = 1,snake_case_ = 1000 ): _A : Optional[int] = 1 _A : List[Any] = 0 for divide_by_number in range(snake_case_,digit + 1 ): _A : list[int] = [] _A : Any = numerator for _ in range(1,digit + 1 ): if now_divide in has_been_divided: if longest_list_length < len(snake_case_ ): _A : Optional[Any] = len(snake_case_ ) _A : Optional[Any] = divide_by_number else: has_been_divided.append(snake_case_ ) _A : Tuple = now_divide * 10 % divide_by_number return the_digit # Tests if __name__ == "__main__": import doctest doctest.testmod()
707
import unittest import numpy as np from transformers.file_utils import is_torch_available, is_vision_available from transformers.testing_utils import require_torch, require_vision 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 DPTImageProcessor class lowercase ( unittest.TestCase ): def __init__( self , _a , _a=7 , _a=3 , _a=18 , _a=30 , _a=400 , _a=True , _a=None , _a=True , _a=[0.5, 0.5, 0.5] , _a=[0.5, 0.5, 0.5] , ) -> Tuple: _A : Any = size if size is not None else {"""height""": 18, """width""": 18} _A : Optional[Any] = parent _A : Union[str, Any] = batch_size _A : List[Any] = num_channels _A : List[str] = image_size _A : Optional[Any] = min_resolution _A : List[Any] = max_resolution _A : Optional[Any] = do_resize _A : str = size _A : List[str] = do_normalize _A : Dict = image_mean _A : int = image_std def a__ ( self ) -> Any: 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 ( UpperCamelCase__,unittest.TestCase ): _a = DPTImageProcessor if is_vision_available() else None def a__ ( self ) -> Optional[int]: _A : Optional[Any] = DPTImageProcessingTester(self ) @property def a__ ( self ) -> Tuple: return self.image_processor_tester.prepare_image_processor_dict() def a__ ( self ) -> Any: _A : List[str] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_a , """image_mean""" ) ) self.assertTrue(hasattr(_a , """image_std""" ) ) self.assertTrue(hasattr(_a , """do_normalize""" ) ) self.assertTrue(hasattr(_a , """do_resize""" ) ) self.assertTrue(hasattr(_a , """size""" ) ) def a__ ( self ) -> Any: _A : List[str] = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"""height""": 18, """width""": 18} ) _A : str = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {"""height""": 42, """width""": 42} ) def a__ ( self ) -> List[Any]: # Initialize image_processing _A : Optional[int] = self.image_processing_class(**self.image_processor_dict ) # create random PIL images _A : Any = prepare_image_inputs(self.image_processor_tester , equal_resolution=_a ) for image in image_inputs: self.assertIsInstance(_a , Image.Image ) # Test not batched input _A : Union[str, 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 : int = image_processing(_a , 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 a__ ( self ) -> Union[str, Any]: # Initialize image_processing _A : List[Any] = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors _A : Optional[Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=_a , numpify=_a ) for image in image_inputs: self.assertIsInstance(_a , np.ndarray ) # Test not batched input _A : Dict = 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(_a , 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 a__ ( self ) -> List[str]: # Initialize image_processing _A : Dict = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors _A : str = prepare_image_inputs(self.image_processor_tester , equal_resolution=_a , torchify=_a ) for image in image_inputs: self.assertIsInstance(_a , torch.Tensor ) # Test not batched input _A : Optional[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 : int = image_processing(_a , 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"""], ) , )
54
0
from scipy.stats import spearmanr import datasets _snake_case = "\nThe Spearman rank-order correlation coefficient is a measure of the\nrelationship between two datasets. Like other correlation coefficients,\nthis one varies between -1 and +1 with 0 implying no correlation.\nPositive correlations imply that as data in dataset x increases, so\ndoes data in dataset y. Negative correlations imply that as x increases,\ny decreases. Correlations of -1 or +1 imply an exact monotonic relationship.\n\nUnlike the Pearson correlation, the Spearman correlation does not\nassume that both datasets are normally distributed.\n\nThe p-value roughly indicates the probability of an uncorrelated system\nproducing datasets that have a Spearman correlation at least as extreme\nas the one computed from these datasets. The p-values are not entirely\nreliable but are probably reasonable for datasets larger than 500 or so.\n" _snake_case = "\nArgs:\n predictions (`List[float]`): Predicted labels, as returned by a model.\n references (`List[float]`): Ground truth labels.\n return_pvalue (`bool`): If `True`, returns the p-value. If `False`, returns\n only the spearmanr score. Defaults to `False`.\nReturns:\n spearmanr (`float`): Spearman correlation coefficient.\n p-value (`float`): p-value. **Note**: is only returned if `return_pvalue=True` is input.\nExamples:\n Example 1:\n >>> spearmanr_metric = datasets.load_metric(\"spearmanr\")\n >>> results = spearmanr_metric.compute(references=[1, 2, 3, 4, 5], predictions=[10, 9, 2.5, 6, 4])\n >>> print(results)\n {'spearmanr': -0.7}\n\n Example 2:\n >>> spearmanr_metric = datasets.load_metric(\"spearmanr\")\n >>> results = spearmanr_metric.compute(references=[1, 2, 3, 4, 5],\n ... predictions=[10, 9, 2.5, 6, 4],\n ... return_pvalue=True)\n >>> print(results['spearmanr'])\n -0.7\n >>> print(round(results['spearmanr_pvalue'], 2))\n 0.19\n" _snake_case = r"\\n@book{kokoska2000crc,\n title={CRC standard probability and statistics tables and formulae},\n author={Kokoska, Stephen and Zwillinger, Daniel},\n year={2000},\n publisher={Crc Press}\n}\n@article{2020SciPy-NMeth,\n author = {Virtanen, Pauli and Gommers, Ralf and Oliphant, Travis E. and\n Haberland, Matt and Reddy, Tyler and Cournapeau, David and\n Burovski, Evgeni and Peterson, Pearu and Weckesser, Warren and\n Bright, Jonathan and {van der Walt}, St{\'e}fan J. and\n Brett, Matthew and Wilson, Joshua and Millman, K. Jarrod and\n Mayorov, Nikolay and Nelson, Andrew R. J. and Jones, Eric and\n Kern, Robert and Larson, Eric and Carey, C J and\n Polat, {\.I}lhan and Feng, Yu and Moore, Eric W. and\n {VanderPlas}, Jake and Laxalde, Denis and Perktold, Josef and\n Cimrman, Robert and Henriksen, Ian and Quintero, E. A. and\n Harris, Charles R. and Archibald, Anne M. and\n Ribeiro, Ant{\^o}nio H. and Pedregosa, Fabian and\n {van Mulbregt}, Paul and {SciPy 1.0 Contributors}},\n title = {{{SciPy} 1.0: Fundamental Algorithms for Scientific\n Computing in Python}},\n journal = {Nature Methods},\n year = {2020},\n volume = {17},\n pages = {261--272},\n adsurl = {https://rdcu.be/b08Wh},\n doi = {10.1038/s41592-019-0686-2},\n}\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION,_KWARGS_DESCRIPTION ) class lowercase ( datasets.Metric ): def a__ ( self ) -> str: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": datasets.Value("""float""" ), """references""": datasets.Value("""float""" ), } ) , reference_urls=["""https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.spearmanr.html"""] , ) def a__ ( self , _a , _a , _a=False ) -> Dict: _A : int = spearmanr(_a , _a ) if return_pvalue: return {"spearmanr": results[0], "spearmanr_pvalue": results[1]} else: return {"spearmanr": results[0]}
708
from typing import Any, Dict, List, Optional, Tuple, Union import torch from torch import nn from torch.utils.data import DistributedSampler, RandomSampler from transformers import PreTrainedModel, Trainer, logging from transformers.integrations import is_fairscale_available from transformers.models.fsmt.configuration_fsmt import FSMTConfig from transformers.optimization import ( Adafactor, AdamW, get_constant_schedule, get_constant_schedule_with_warmup, get_cosine_schedule_with_warmup, get_cosine_with_hard_restarts_schedule_with_warmup, get_linear_schedule_with_warmup, get_polynomial_decay_schedule_with_warmup, ) from transformers.trainer_pt_utils import get_tpu_sampler from transformers.training_args import ParallelMode from transformers.utils import is_torch_tpu_available if is_fairscale_available(): from fairscale.optim import OSS _snake_case = logging.get_logger(__name__) _snake_case = { "linear": get_linear_schedule_with_warmup, "cosine": get_cosine_schedule_with_warmup, "cosine_w_restarts": get_cosine_with_hard_restarts_schedule_with_warmup, "polynomial": get_polynomial_decay_schedule_with_warmup, "constant": get_constant_schedule, "constant_w_warmup": get_constant_schedule_with_warmup, } class lowercase ( UpperCamelCase__ ): def __init__( self , _a=None , _a=None , *_a , **_a ) -> Optional[int]: super().__init__(*_a , **_a ) if config is None: assert isinstance(self.model , _a ), ( "If no `config` is passed the model to be trained has to be of type `PreTrainedModel`, but is" F''' {self.model.__class__}''' ) _A : Optional[Any] = self.model.config else: _A : int = config _A : Optional[Any] = data_args _A : int = self.config.tgt_vocab_size if isinstance(self.config , _a ) else self.config.vocab_size if self.args.label_smoothing != 0 or (self.data_args is not None and self.data_args.ignore_pad_token_for_loss): assert self.config.pad_token_id is not None, ( "Make sure that `config.pad_token_id` is correcly defined when ignoring `pad_token` for loss" " calculation or doing label smoothing." ) if self.config.pad_token_id is None and self.config.eos_token_id is not None: logger.warning( F'''The `config.pad_token_id` is `None`. Using `config.eos_token_id` = {self.config.eos_token_id} for''' """ padding..""" ) if self.args.label_smoothing == 0: _A : Optional[Any] = torch.nn.CrossEntropyLoss(ignore_index=self.config.pad_token_id ) else: # dynamically import label_smoothed_nll_loss from utils import label_smoothed_nll_loss _A : Union[str, Any] = label_smoothed_nll_loss def a__ ( self , _a ) -> int: if self.optimizer is None: _A : List[str] = ["""bias""", """LayerNorm.weight"""] _A : str = [ { """params""": [p for n, p in self.model.named_parameters() if not any(nd in n for nd in no_decay )], """weight_decay""": self.args.weight_decay, }, { """params""": [p for n, p in self.model.named_parameters() if any(nd in n for nd in no_decay )], """weight_decay""": 0.0, }, ] _A : Optional[Any] = Adafactor if self.args.adafactor else AdamW if self.args.adafactor: _A : Dict = Adafactor _A : int = {"""scale_parameter""": False, """relative_step""": False} else: _A : int = AdamW _A : Any = { """betas""": (self.args.adam_betaa, self.args.adam_betaa), """eps""": self.args.adam_epsilon, } _A : List[str] = self.args.learning_rate if self.sharded_ddp: _A : List[str] = OSS( params=_a , optim=_a , **_a , ) else: _A : Tuple = optimizer_cls(_a , **_a ) if self.lr_scheduler is None: _A : Union[str, Any] = self._get_lr_scheduler(_a ) else: # ignoring --lr_scheduler logger.warning("""scheduler is passed to `Seq2SeqTrainer`, `--lr_scheduler` arg is ignored.""" ) def a__ ( self , _a ) -> Dict: _A : List[Any] = arg_to_scheduler[self.args.lr_scheduler] if self.args.lr_scheduler == "constant": _A : Optional[Any] = schedule_func(self.optimizer ) elif self.args.lr_scheduler == "constant_w_warmup": _A : Optional[int] = schedule_func(self.optimizer , num_warmup_steps=self.args.warmup_steps ) else: _A : List[Any] = schedule_func( self.optimizer , num_warmup_steps=self.args.warmup_steps , num_training_steps=_a ) return scheduler def a__ ( self ) -> Optional[torch.utils.data.Sampler]: if isinstance(self.train_dataset , torch.utils.data.IterableDataset ): return None elif is_torch_tpu_available(): return get_tpu_sampler(self.train_dataset ) else: if self.args.sortish_sampler: self.train_dataset.make_sortish_sampler( self.args.per_device_train_batch_size , distributed=(self.args.parallel_mode == ParallelMode.DISTRIBUTED) , ) return ( RandomSampler(self.train_dataset ) if self.args.local_rank == -1 else DistributedSampler(self.train_dataset ) ) def a__ ( self , _a , _a , _a ) -> List[str]: if self.args.label_smoothing == 0: if self.data_args is not None and self.data_args.ignore_pad_token_for_loss: # force training to ignore pad token _A : List[str] = model(**_a , use_cache=_a )[0] _A : Dict = self.loss_fn(logits.view(-1 , logits.shape[-1] ) , labels.view(-1 ) ) else: # compute usual loss via models _A , _A : str = model(**_a , labels=_a , use_cache=_a )[:2] else: # compute label smoothed loss _A : Any = model(**_a , use_cache=_a )[0] _A : Union[str, Any] = torch.nn.functional.log_softmax(_a , dim=-1 ) _A , _A : List[str] = self.loss_fn(_a , _a , self.args.label_smoothing , ignore_index=self.config.pad_token_id ) return loss, logits def a__ ( self , _a , _a ) -> List[Any]: _A : Optional[int] = inputs.pop("""labels""" ) _A , _A : Dict = self._compute_loss(_a , _a , _a ) return loss def a__ ( self , _a , _a , _a , _a = None , ) -> Tuple[Optional[float], Optional[torch.Tensor], Optional[torch.Tensor]]: _A : int = self._prepare_inputs(_a ) _A : Dict = { """max_length""": self.data_args.val_max_target_length if self.data_args is not None else self.config.max_length, """num_beams""": self.data_args.eval_beams if self.data_args is not None else self.config.num_beams, } if self.args.predict_with_generate and not self.args.prediction_loss_only: _A : List[str] = self.model.generate( inputs["""input_ids"""] , attention_mask=inputs["""attention_mask"""] , **_a , ) # in case the batch is shorter than max length, the output should be padded if generated_tokens.shape[-1] < gen_kwargs["max_length"]: _A : str = self._pad_tensors_to_max_len(_a , gen_kwargs["""max_length"""] ) _A : Any = inputs.pop("""labels""" ) with torch.no_grad(): # compute loss on predict data _A , _A : Tuple = self._compute_loss(_a , _a , _a ) _A : Any = loss.mean().detach() if self.args.prediction_loss_only: return (loss, None, None) _A : List[Any] = generated_tokens if self.args.predict_with_generate else logits if labels.shape[-1] < gen_kwargs["max_length"]: _A : Any = self._pad_tensors_to_max_len(_a , gen_kwargs["""max_length"""] ) return (loss, logits, labels) def a__ ( self , _a , _a ) -> Union[str, Any]: # If PAD token is not defined at least EOS token has to be defined _A : Optional[Any] = self.config.pad_token_id if self.config.pad_token_id is not None else self.config.eos_token_id if pad_token_id is None: raise ValueError( """Make sure that either `config.pad_token_id` or `config.eos_token_id` is defined if tensor has to be""" F''' padded to `max_length`={max_length}''' ) _A : Dict = pad_token_id * torch.ones( (tensor.shape[0], max_length) , dtype=tensor.dtype , device=tensor.device ) _A : Dict = tensor return padded_tensor
54
0
import os from itertools import chain from random import randrange, shuffle import pytest from .sola import PokerHand _snake_case = ( "4S 3H 2C 7S 5H", "9D 8H 2C 6S 7H", "2D 6D 9D TH 7D", "TC 8C 2S JH 6C", "JH 8S TH AH QH", "TS KS 5S 9S AC", "KD 6S 9D TH AD", "KS 8D 4D 9S 4S", # pair "8C 4S KH JS 4D", # pair "QH 8H KD JH 8S", # pair "KC 4H KS 2H 8D", # pair "KD 4S KC 3H 8S", # pair "AH 8S AS KC JH", # pair "3H 4C 4H 3S 2H", # 2 pairs "5S 5D 2C KH KH", # 2 pairs "3C KH 5D 5S KH", # 2 pairs "AS 3C KH AD KH", # 2 pairs "7C 7S 3S 7H 5S", # 3 of a kind "7C 7S KH 2H 7H", # 3 of a kind "AC KH QH AH AS", # 3 of a kind "2H 4D 3C AS 5S", # straight (low ace) "3C 5C 4C 2C 6H", # straight "6S 8S 7S 5H 9H", # straight "JS QS 9H TS KH", # straight "QC KH TS JS AH", # straight (high ace) "8C 9C 5C 3C TC", # flush "3S 8S 9S 5S KS", # flush "4C 5C 9C 8C KC", # flush "JH 8H AH KH QH", # flush "3D 2H 3H 2C 2D", # full house "2H 2C 3S 3H 3D", # full house "KH KC 3S 3H 3D", # full house "JC 6H JS JD JH", # 4 of a kind "JC 7H JS JD JH", # 4 of a kind "JC KH JS JD JH", # 4 of a kind "2S AS 4S 5S 3S", # straight flush (low ace) "2D 6D 3D 4D 5D", # straight flush "5C 6C 3C 7C 4C", # straight flush "JH 9H TH KH QH", # straight flush "JH AH TH KH QH", # royal flush (high ace straight flush) ) _snake_case = ( ("2H 3H 4H 5H 6H", "KS AS TS QS JS", "Loss"), ("2H 3H 4H 5H 6H", "AS AD AC AH JD", "Win"), ("AS AH 2H AD AC", "JS JD JC JH 3D", "Win"), ("2S AH 2H AS AC", "JS JD JC JH AD", "Loss"), ("2S AH 2H AS AC", "2H 3H 5H 6H 7H", "Win"), ("AS 3S 4S 8S 2S", "2H 3H 5H 6H 7H", "Win"), ("2H 3H 5H 6H 7H", "2S 3H 4H 5S 6C", "Win"), ("2S 3H 4H 5S 6C", "3D 4C 5H 6H 2S", "Tie"), ("2S 3H 4H 5S 6C", "AH AC 5H 6H AS", "Win"), ("2S 2H 4H 5S 4C", "AH AC 5H 6H AS", "Loss"), ("2S 2H 4H 5S 4C", "AH AC 5H 6H 7S", "Win"), ("6S AD 7H 4S AS", "AH AC 5H 6H 7S", "Loss"), ("2S AH 4H 5S KC", "AH AC 5H 6H 7S", "Loss"), ("2S 3H 6H 7S 9C", "7H 3C TH 6H 9S", "Loss"), ("4S 5H 6H TS AC", "3S 5H 6H TS AC", "Win"), ("2S AH 4H 5S 6C", "AD 4C 5H 6H 2C", "Tie"), ("AS AH 3H AD AC", "AS AH 2H AD AC", "Win"), ("AH AC 5H 5C QS", "AH AC 5H 5C KS", "Loss"), ("AH AC 5H 5C QS", "KH KC 5H 5C QS", "Win"), ("7C 7S KH 2H 7H", "3C 3S AH 2H 3H", "Win"), ("3C 3S AH 2H 3H", "7C 7S KH 2H 7H", "Loss"), ("6H 5H 4H 3H 2H", "5H 4H 3H 2H AH", "Win"), ("5H 4H 3H 2H AH", "5H 4H 3H 2H AH", "Tie"), ("5H 4H 3H 2H AH", "6H 5H 4H 3H 2H", "Loss"), ("AH AD KS KC AC", "AH KD KH AC KC", "Win"), ("2H 4D 3C AS 5S", "2H 4D 3C 6S 5S", "Loss"), ("2H 3S 3C 3H 2S", "3S 3C 2S 2H 2D", "Win"), ("4D 6D 5D 2D JH", "3S 8S 3H TC KH", "Loss"), ("4S 6C 8S 3S 7S", "AD KS 2D 7D 7C", "Loss"), ("6S 4C 7H 8C 3H", "5H JC AH 9D 9C", "Loss"), ("9D 9H JH TC QH", "3C 2S JS 5C 7H", "Win"), ("2H TC 8S AD 9S", "4H TS 7H 2C 5C", "Win"), ("9D 3S 2C 7S 7C", "JC TD 3C TC 9H", "Loss"), ) _snake_case = ( ("2H 3H 4H 5H 6H", True), ("AS AH 2H AD AC", False), ("2H 3H 5H 6H 7H", True), ("KS AS TS QS JS", True), ("8H 9H QS JS TH", False), ("AS 3S 4S 8S 2S", True), ) _snake_case = ( ("2H 3H 4H 5H 6H", True), ("AS AH 2H AD AC", False), ("2H 3H 5H 6H 7H", False), ("KS AS TS QS JS", True), ("8H 9H QS JS TH", True), ) _snake_case = ( ("2H 4D 3C AS 5S", True, [5, 4, 3, 2, 14]), ("2H 5D 3C AS 5S", False, [14, 5, 5, 3, 2]), ("JH QD KC AS TS", False, [14, 13, 12, 11, 10]), ("9D 3S 2C 7S 7C", False, [9, 7, 7, 3, 2]), ) _snake_case = ( ("JH AH TH KH QH", 0), ("JH 9H TH KH QH", 0), ("JC KH JS JD JH", 7), ("KH KC 3S 3H 3D", 6), ("8C 9C 5C 3C TC", 0), ("JS QS 9H TS KH", 0), ("7C 7S KH 2H 7H", 3), ("3C KH 5D 5S KH", 2), ("QH 8H KD JH 8S", 1), ("2D 6D 9D TH 7D", 0), ) _snake_case = ( ("JH AH TH KH QH", 23), ("JH 9H TH KH QH", 22), ("JC KH JS JD JH", 21), ("KH KC 3S 3H 3D", 20), ("8C 9C 5C 3C TC", 19), ("JS QS 9H TS KH", 18), ("7C 7S KH 2H 7H", 17), ("3C KH 5D 5S KH", 16), ("QH 8H KD JH 8S", 15), ("2D 6D 9D TH 7D", 14), ) def lowerCAmelCase_ ( ): _A : int = randrange(len(snake_case_ ) ), randrange(len(snake_case_ ) ) _A : Dict = ["""Loss""", """Tie""", """Win"""][(play >= oppo) + (play > oppo)] _A : List[Any] = SORTED_HANDS[play], SORTED_HANDS[oppo] return hand, other, expected def lowerCAmelCase_ ( snake_case_ = 100 ): return (generate_random_hand() for _ in range(snake_case_ )) @pytest.mark.parametrize("""hand, expected""",snake_case_ ) def lowerCAmelCase_ ( snake_case_,snake_case_ ): assert PokerHand(snake_case_ )._is_flush() == expected @pytest.mark.parametrize("""hand, expected""",snake_case_ ) def lowerCAmelCase_ ( snake_case_,snake_case_ ): assert PokerHand(snake_case_ )._is_straight() == expected @pytest.mark.parametrize("""hand, expected, card_values""",snake_case_ ) def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_ ): _A : List[str] = PokerHand(snake_case_ ) assert player._is_five_high_straight() == expected assert player._card_values == card_values @pytest.mark.parametrize("""hand, expected""",snake_case_ ) def lowerCAmelCase_ ( snake_case_,snake_case_ ): assert PokerHand(snake_case_ )._is_same_kind() == expected @pytest.mark.parametrize("""hand, expected""",snake_case_ ) def lowerCAmelCase_ ( snake_case_,snake_case_ ): assert PokerHand(snake_case_ )._hand_type == expected @pytest.mark.parametrize("""hand, other, expected""",snake_case_ ) def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_ ): assert PokerHand(snake_case_ ).compare_with(PokerHand(snake_case_ ) ) == expected @pytest.mark.parametrize("""hand, other, expected""",generate_random_hands() ) def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_ ): assert PokerHand(snake_case_ ).compare_with(PokerHand(snake_case_ ) ) == expected def lowerCAmelCase_ ( ): _A : Any = [PokerHand(snake_case_ ) for hand in SORTED_HANDS] _A : Any = poker_hands.copy() shuffle(snake_case_ ) _A : Union[str, Any] = chain(sorted(snake_case_ ) ) for index, hand in enumerate(snake_case_ ): assert hand == poker_hands[index] def lowerCAmelCase_ ( ): # Test that five high straights are compared correctly. _A : List[Any] = [PokerHand("""2D AC 3H 4H 5S""" ), PokerHand("""2S 3H 4H 5S 6C""" )] pokerhands.sort(reverse=snake_case_ ) assert pokerhands[0].__str__() == "2S 3H 4H 5S 6C" def lowerCAmelCase_ ( ): # Multiple calls to five_high_straight function should still return True # and shouldn't mutate the list in every call other than the first. _A : str = PokerHand("""2C 4S AS 3D 5C""" ) _A : Union[str, Any] = True _A : Optional[int] = [5, 4, 3, 2, 14] for _ in range(10 ): assert pokerhand._is_five_high_straight() == expected assert pokerhand._card_values == expected_card_values def lowerCAmelCase_ ( ): # Problem number 54 from Project Euler # Testing from poker_hands.txt file _A : List[str] = 0 _A : int = os.path.abspath(os.path.dirname(snake_case_ ) ) _A : Dict = os.path.join(snake_case_,"""poker_hands.txt""" ) with open(snake_case_ ) as file_hand: for line in file_hand: _A : Optional[Any] = line[:14].strip() _A : List[str] = line[15:].strip() _A : Union[str, Any] = PokerHand(snake_case_ ), PokerHand(snake_case_ ) _A : Tuple = player.compare_with(snake_case_ ) if output == "Win": answer += 1 assert answer == 376
709
from collections.abc import Callable from math import pi, sqrt from random import uniform from statistics import mean def lowerCAmelCase_ ( snake_case_ ): # A local function to see if a dot lands in the circle. def is_in_circle(snake_case_,snake_case_ ) -> bool: _A : List[str] = sqrt((x**2) + (y**2) ) # Our circle has a radius of 1, so a distance # greater than 1 would land outside the circle. return distance_from_centre <= 1 # The proportion of guesses that landed in the circle _A : Optional[int] = mean( int(is_in_circle(uniform(-1.0,1.0 ),uniform(-1.0,1.0 ) ) ) for _ in range(snake_case_ ) ) # The ratio of the area for circle to square is pi/4. _A : List[str] = proportion * 4 print(f'''The estimated value of pi is {pi_estimate}''' ) print(f'''The numpy value of pi is {pi}''' ) print(f'''The total error is {abs(pi - pi_estimate )}''' ) def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_ = 0.0,snake_case_ = 1.0,): return mean( function_to_integrate(uniform(snake_case_,snake_case_ ) ) for _ in range(snake_case_ ) ) * (max_value - min_value) def lowerCAmelCase_ ( snake_case_,snake_case_ = 0.0,snake_case_ = 1.0 ): def identity_function(snake_case_ ) -> float: return x _A : Any = area_under_curve_estimator( snake_case_,snake_case_,snake_case_,snake_case_ ) _A : Tuple = (max_value * max_value - min_value * min_value) / 2 print("""******************""" ) print(f'''Estimating area under y=x where x varies from {min_value} to {max_value}''' ) print(f'''Estimated value is {estimated_value}''' ) print(f'''Expected value is {expected_value}''' ) print(f'''Total error is {abs(estimated_value - expected_value )}''' ) print("""******************""" ) def lowerCAmelCase_ ( snake_case_ ): def function_to_integrate(snake_case_ ) -> float: return sqrt(4.0 - x * x ) _A : Optional[int] = area_under_curve_estimator( snake_case_,snake_case_,0.0,2.0 ) print("""******************""" ) print("""Estimating pi using area_under_curve_estimator""" ) print(f'''Estimated value is {estimated_value}''' ) print(f'''Expected value is {pi}''' ) print(f'''Total error is {abs(estimated_value - pi )}''' ) print("""******************""" ) if __name__ == "__main__": import doctest doctest.testmod()
54
0
import argparse import io import requests import torch from omegaconf import OmegaConf from diffusers import AutoencoderKL from diffusers.pipelines.stable_diffusion.convert_from_ckpt import ( assign_to_checkpoint, conv_attn_to_linear, create_vae_diffusers_config, renew_vae_attention_paths, renew_vae_resnet_paths, ) def lowerCAmelCase_ ( snake_case_,snake_case_ ): _A : Dict = checkpoint _A : Optional[Any] = {} _A : Optional[Any] = vae_state_dict["""encoder.conv_in.weight"""] _A : Any = vae_state_dict["""encoder.conv_in.bias"""] _A : int = vae_state_dict["""encoder.conv_out.weight"""] _A : List[Any] = vae_state_dict["""encoder.conv_out.bias"""] _A : List[str] = vae_state_dict["""encoder.norm_out.weight"""] _A : Any = vae_state_dict["""encoder.norm_out.bias"""] _A : Any = vae_state_dict["""decoder.conv_in.weight"""] _A : List[Any] = vae_state_dict["""decoder.conv_in.bias"""] _A : Union[str, Any] = vae_state_dict["""decoder.conv_out.weight"""] _A : int = vae_state_dict["""decoder.conv_out.bias"""] _A : Any = vae_state_dict["""decoder.norm_out.weight"""] _A : List[str] = vae_state_dict["""decoder.norm_out.bias"""] _A : Any = vae_state_dict["""quant_conv.weight"""] _A : str = vae_state_dict["""quant_conv.bias"""] _A : List[Any] = vae_state_dict["""post_quant_conv.weight"""] _A : Any = vae_state_dict["""post_quant_conv.bias"""] # Retrieves the keys for the encoder down blocks only _A : Any = len({""".""".join(layer.split(""".""" )[:3] ) for layer in vae_state_dict if """encoder.down""" in layer} ) _A : Union[str, Any] = { layer_id: [key for key in vae_state_dict if f'''down.{layer_id}''' in key] for layer_id in range(snake_case_ ) } # Retrieves the keys for the decoder up blocks only _A : List[Any] = len({""".""".join(layer.split(""".""" )[:3] ) for layer in vae_state_dict if """decoder.up""" in layer} ) _A : Tuple = { layer_id: [key for key in vae_state_dict if f'''up.{layer_id}''' in key] for layer_id in range(snake_case_ ) } for i in range(snake_case_ ): _A : Optional[int] = [key for key in down_blocks[i] if f'''down.{i}''' in key and f'''down.{i}.downsample''' not in key] if f'''encoder.down.{i}.downsample.conv.weight''' in vae_state_dict: _A : Dict = vae_state_dict.pop( f'''encoder.down.{i}.downsample.conv.weight''' ) _A : int = vae_state_dict.pop( f'''encoder.down.{i}.downsample.conv.bias''' ) _A : Dict = renew_vae_resnet_paths(snake_case_ ) _A : Tuple = {"""old""": f'''down.{i}.block''', """new""": f'''down_blocks.{i}.resnets'''} assign_to_checkpoint(snake_case_,snake_case_,snake_case_,additional_replacements=[meta_path],config=snake_case_ ) _A : Tuple = [key for key in vae_state_dict if """encoder.mid.block""" in key] _A : List[str] = 2 for i in range(1,num_mid_res_blocks + 1 ): _A : Dict = [key for key in mid_resnets if f'''encoder.mid.block_{i}''' in key] _A : Any = renew_vae_resnet_paths(snake_case_ ) _A : Tuple = {"""old""": f'''mid.block_{i}''', """new""": f'''mid_block.resnets.{i - 1}'''} assign_to_checkpoint(snake_case_,snake_case_,snake_case_,additional_replacements=[meta_path],config=snake_case_ ) _A : List[Any] = [key for key in vae_state_dict if """encoder.mid.attn""" in key] _A : List[str] = renew_vae_attention_paths(snake_case_ ) _A : str = {"""old""": """mid.attn_1""", """new""": """mid_block.attentions.0"""} assign_to_checkpoint(snake_case_,snake_case_,snake_case_,additional_replacements=[meta_path],config=snake_case_ ) conv_attn_to_linear(snake_case_ ) for i in range(snake_case_ ): _A : Dict = num_up_blocks - 1 - i _A : Any = [ key for key in up_blocks[block_id] if f'''up.{block_id}''' in key and f'''up.{block_id}.upsample''' not in key ] if f'''decoder.up.{block_id}.upsample.conv.weight''' in vae_state_dict: _A : Tuple = vae_state_dict[ f'''decoder.up.{block_id}.upsample.conv.weight''' ] _A : Union[str, Any] = vae_state_dict[ f'''decoder.up.{block_id}.upsample.conv.bias''' ] _A : List[str] = renew_vae_resnet_paths(snake_case_ ) _A : Union[str, Any] = {"""old""": f'''up.{block_id}.block''', """new""": f'''up_blocks.{i}.resnets'''} assign_to_checkpoint(snake_case_,snake_case_,snake_case_,additional_replacements=[meta_path],config=snake_case_ ) _A : List[str] = [key for key in vae_state_dict if """decoder.mid.block""" in key] _A : str = 2 for i in range(1,num_mid_res_blocks + 1 ): _A : Dict = [key for key in mid_resnets if f'''decoder.mid.block_{i}''' in key] _A : List[Any] = renew_vae_resnet_paths(snake_case_ ) _A : Any = {"""old""": f'''mid.block_{i}''', """new""": f'''mid_block.resnets.{i - 1}'''} assign_to_checkpoint(snake_case_,snake_case_,snake_case_,additional_replacements=[meta_path],config=snake_case_ ) _A : str = [key for key in vae_state_dict if """decoder.mid.attn""" in key] _A : List[str] = renew_vae_attention_paths(snake_case_ ) _A : Union[str, Any] = {"""old""": """mid.attn_1""", """new""": """mid_block.attentions.0"""} assign_to_checkpoint(snake_case_,snake_case_,snake_case_,additional_replacements=[meta_path],config=snake_case_ ) conv_attn_to_linear(snake_case_ ) return new_checkpoint def lowerCAmelCase_ ( snake_case_,snake_case_,): # Only support V1 _A : str = requests.get( """ https://raw.githubusercontent.com/CompVis/stable-diffusion/main/configs/stable-diffusion/v1-inference.yaml""" ) _A : Any = io.BytesIO(r.content ) _A : Optional[Any] = OmegaConf.load(snake_case_ ) _A : Tuple = 512 _A : str = """cuda""" if torch.cuda.is_available() else """cpu""" if checkpoint_path.endswith("""safetensors""" ): from safetensors import safe_open _A : int = {} with safe_open(snake_case_,framework="""pt""",device="""cpu""" ) as f: for key in f.keys(): _A : Optional[Any] = f.get_tensor(snake_case_ ) else: _A : str = torch.load(snake_case_,map_location=snake_case_ )["""state_dict"""] # Convert the VAE model. _A : int = create_vae_diffusers_config(snake_case_,image_size=snake_case_ ) _A : Optional[int] = custom_convert_ldm_vae_checkpoint(snake_case_,snake_case_ ) _A : List[str] = AutoencoderKL(**snake_case_ ) vae.load_state_dict(snake_case_ ) vae.save_pretrained(snake_case_ ) if __name__ == "__main__": _snake_case = argparse.ArgumentParser() parser.add_argument("--vae_pt_path", default=None, type=str, required=True, help="Path to the VAE.pt to convert.") parser.add_argument("--dump_path", default=None, type=str, required=True, help="Path to the VAE.pt to convert.") _snake_case = parser.parse_args() vae_pt_to_vae_diffuser(args.vae_pt_path, args.dump_path)
710
import json import os import shutil import tempfile from unittest import TestCase from transformers import BartTokenizer, BartTokenizerFast, DPRQuestionEncoderTokenizer, DPRQuestionEncoderTokenizerFast from transformers.models.bart.configuration_bart import BartConfig from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES as DPR_VOCAB_FILES_NAMES from transformers.models.dpr.configuration_dpr import DPRConfig from transformers.models.roberta.tokenization_roberta import VOCAB_FILES_NAMES as BART_VOCAB_FILES_NAMES from transformers.testing_utils import require_faiss, require_tokenizers, require_torch, slow from transformers.utils import is_datasets_available, is_faiss_available, is_torch_available if is_torch_available() and is_datasets_available() and is_faiss_available(): from transformers.models.rag.configuration_rag import RagConfig from transformers.models.rag.tokenization_rag import RagTokenizer @require_faiss @require_torch class lowercase ( UpperCamelCase__ ): def a__ ( self ) -> int: _A : int = tempfile.mkdtemp() _A : Union[str, Any] = 8 # DPR tok _A : List[str] = [ """[UNK]""", """[CLS]""", """[SEP]""", """[PAD]""", """[MASK]""", """want""", """##want""", """##ed""", """wa""", """un""", """runn""", """##ing""", """,""", """low""", """lowest""", ] _A : List[str] = os.path.join(self.tmpdirname , """dpr_tokenizer""" ) os.makedirs(_a , exist_ok=_a ) _A : str = os.path.join(_a , DPR_VOCAB_FILES_NAMES["""vocab_file"""] ) with open(self.vocab_file , """w""" , encoding="""utf-8""" ) as vocab_writer: vocab_writer.write("""""".join([x + """\n""" for x in vocab_tokens] ) ) # BART tok _A : Dict = [ """l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """\u0120""", """\u0120l""", """\u0120n""", """\u0120lo""", """\u0120low""", """er""", """\u0120lowest""", """\u0120newer""", """\u0120wider""", """<unk>""", ] _A : Optional[Any] = dict(zip(_a , range(len(_a ) ) ) ) _A : Tuple = ["""#version: 0.2""", """\u0120 l""", """\u0120l o""", """\u0120lo w""", """e r""", """"""] _A : Dict = {"""unk_token""": """<unk>"""} _A : Optional[Any] = os.path.join(self.tmpdirname , """bart_tokenizer""" ) os.makedirs(_a , exist_ok=_a ) _A : str = os.path.join(_a , BART_VOCAB_FILES_NAMES["""vocab_file"""] ) _A : List[Any] = os.path.join(_a , BART_VOCAB_FILES_NAMES["""merges_file"""] ) with open(self.vocab_file , """w""" , encoding="""utf-8""" ) as fp: fp.write(json.dumps(_a ) + """\n""" ) with open(self.merges_file , """w""" , encoding="""utf-8""" ) as fp: fp.write("""\n""".join(_a ) ) def a__ ( self ) -> DPRQuestionEncoderTokenizer: return DPRQuestionEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname , """dpr_tokenizer""" ) ) def a__ ( self ) -> BartTokenizer: return BartTokenizer.from_pretrained(os.path.join(self.tmpdirname , """bart_tokenizer""" ) ) def a__ ( self ) -> Optional[Any]: shutil.rmtree(self.tmpdirname ) @require_tokenizers def a__ ( self ) -> str: _A : Optional[Any] = os.path.join(self.tmpdirname , """rag_tokenizer""" ) _A : int = RagConfig(question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() ) _A : Any = RagTokenizer(question_encoder=self.get_dpr_tokenizer() , generator=self.get_bart_tokenizer() ) rag_config.save_pretrained(_a ) rag_tokenizer.save_pretrained(_a ) _A : Optional[Any] = RagTokenizer.from_pretrained(_a , config=_a ) self.assertIsInstance(new_rag_tokenizer.question_encoder , _a ) self.assertEqual(new_rag_tokenizer.question_encoder.get_vocab() , rag_tokenizer.question_encoder.get_vocab() ) self.assertIsInstance(new_rag_tokenizer.generator , _a ) self.assertEqual(new_rag_tokenizer.generator.get_vocab() , rag_tokenizer.generator.get_vocab() ) @slow def a__ ( self ) -> str: _A : Union[str, Any] = RagTokenizer.from_pretrained("""facebook/rag-token-nq""" ) _A : Tuple = [ """who got the first nobel prize in physics""", """when is the next deadpool movie being released""", """which mode is used for short wave broadcast service""", """who is the owner of reading football club""", """when is the next scandal episode coming out""", """when is the last time the philadelphia won the superbowl""", """what is the most current adobe flash player version""", """how many episodes are there in dragon ball z""", """what is the first step in the evolution of the eye""", """where is gall bladder situated in human body""", """what is the main mineral in lithium batteries""", """who is the president of usa right now""", """where do the greasers live in the outsiders""", """panda is a national animal of which country""", """what is the name of manchester united stadium""", ] _A : Tuple = tokenizer(_a ) self.assertIsNotNone(_a ) @slow def a__ ( self ) -> Dict: _A : Dict = RagTokenizer.from_pretrained("""facebook/rag-sequence-nq""" ) _A : str = [ """who got the first nobel prize in physics""", """when is the next deadpool movie being released""", """which mode is used for short wave broadcast service""", """who is the owner of reading football club""", """when is the next scandal episode coming out""", """when is the last time the philadelphia won the superbowl""", """what is the most current adobe flash player version""", """how many episodes are there in dragon ball z""", """what is the first step in the evolution of the eye""", """where is gall bladder situated in human body""", """what is the main mineral in lithium batteries""", """who is the president of usa right now""", """where do the greasers live in the outsiders""", """panda is a national animal of which country""", """what is the name of manchester united stadium""", ] _A : Optional[Any] = tokenizer(_a ) self.assertIsNotNone(_a )
54
0