code
stringlengths
82
54.1k
code_codestyle
int64
0
699
style_context
stringlengths
111
35.6k
style_context_codestyle
int64
0
699
label
int64
0
1
import json import os from typing import Optional import numpy as np from ...feature_extraction_utils import BatchFeature from ...processing_utils import ProcessorMixin from ...utils import logging from ...utils.hub import get_file_from_repo from ..auto import AutoTokenizer snake_case : str = logging.get_logger(__name__) class snake_case_ (__SCREAMING_SNAKE_CASE ): UpperCAmelCase__ : int = '''AutoTokenizer''' UpperCAmelCase__ : Any = ['''tokenizer'''] UpperCAmelCase__ : int = { '''semantic_prompt''': 1, '''coarse_prompt''': 2, '''fine_prompt''': 2, } def __init__( self :Optional[int] ,__snake_case :Optional[Any] ,__snake_case :Union[str, Any]=None ) -> str: super().__init__(__SCREAMING_SNAKE_CASE ) a__ = speaker_embeddings @classmethod def lowerCamelCase__( cls :int ,__snake_case :int ,__snake_case :Optional[Any]="speaker_embeddings_path.json" ,**__snake_case :Union[str, Any] ) -> str: if speaker_embeddings_dict_path is not None: a__ = get_file_from_repo( __SCREAMING_SNAKE_CASE ,__SCREAMING_SNAKE_CASE ,subfolder=kwargs.pop('subfolder' ,__SCREAMING_SNAKE_CASE ) ,cache_dir=kwargs.pop('cache_dir' ,__SCREAMING_SNAKE_CASE ) ,force_download=kwargs.pop('force_download' ,__SCREAMING_SNAKE_CASE ) ,proxies=kwargs.pop('proxies' ,__SCREAMING_SNAKE_CASE ) ,resume_download=kwargs.pop('resume_download' ,__SCREAMING_SNAKE_CASE ) ,local_files_only=kwargs.pop('local_files_only' ,__SCREAMING_SNAKE_CASE ) ,use_auth_token=kwargs.pop('use_auth_token' ,__SCREAMING_SNAKE_CASE ) ,revision=kwargs.pop('revision' ,__SCREAMING_SNAKE_CASE ) ,) if speaker_embeddings_path is None: logger.warning( F'`{os.path.join(__SCREAMING_SNAKE_CASE ,__SCREAMING_SNAKE_CASE )}` does not exists\n , no preloaded speaker embeddings will be used - Make sure to provide a correct path to the json\n dictionnary if wanted, otherwise set `speaker_embeddings_dict_path=None`.' ) a__ = None else: with open(__SCREAMING_SNAKE_CASE ) as speaker_embeddings_json: a__ = json.load(__SCREAMING_SNAKE_CASE ) else: a__ = None a__ = AutoTokenizer.from_pretrained(__SCREAMING_SNAKE_CASE ,**__SCREAMING_SNAKE_CASE ) return cls(tokenizer=__SCREAMING_SNAKE_CASE ,speaker_embeddings=__SCREAMING_SNAKE_CASE ) def lowerCamelCase__( self :Dict ,__snake_case :Tuple ,__snake_case :List[str]="speaker_embeddings_path.json" ,__snake_case :List[Any]="speaker_embeddings" ,__snake_case :Optional[Any] = False ,**__snake_case :List[Any] ,) -> int: if self.speaker_embeddings is not None: os.makedirs(os.path.join(__SCREAMING_SNAKE_CASE ,__SCREAMING_SNAKE_CASE ,'v2' ) ,exist_ok=__SCREAMING_SNAKE_CASE ) a__ = {} a__ = save_directory for prompt_key in self.speaker_embeddings: if prompt_key != "repo_or_path": a__ = self._load_voice_preset(__SCREAMING_SNAKE_CASE ) a__ = {} for key in self.speaker_embeddings[prompt_key]: np.save( os.path.join( embeddings_dict['repo_or_path'] ,__SCREAMING_SNAKE_CASE ,F'{prompt_key}_{key}' ) ,voice_preset[key] ,allow_pickle=__SCREAMING_SNAKE_CASE ,) a__ = os.path.join(__SCREAMING_SNAKE_CASE ,F'{prompt_key}_{key}.npy' ) a__ = tmp_dict with open(os.path.join(__SCREAMING_SNAKE_CASE ,__SCREAMING_SNAKE_CASE ) ,'w' ) as fp: json.dump(__SCREAMING_SNAKE_CASE ,__SCREAMING_SNAKE_CASE ) super().save_pretrained(__SCREAMING_SNAKE_CASE ,__SCREAMING_SNAKE_CASE ,**__SCREAMING_SNAKE_CASE ) def lowerCamelCase__( self :Any ,__snake_case :int = None ,**__snake_case :Tuple ) -> List[Any]: a__ = self.speaker_embeddings[voice_preset] a__ = {} for key in ["semantic_prompt", "coarse_prompt", "fine_prompt"]: if key not in voice_preset_paths: raise ValueError( F'Voice preset unrecognized, missing {key} as a key in self.speaker_embeddings[{voice_preset}].' ) a__ = get_file_from_repo( self.speaker_embeddings.get('repo_or_path' ,'/' ) ,voice_preset_paths[key] ,subfolder=kwargs.pop('subfolder' ,__SCREAMING_SNAKE_CASE ) ,cache_dir=kwargs.pop('cache_dir' ,__SCREAMING_SNAKE_CASE ) ,force_download=kwargs.pop('force_download' ,__SCREAMING_SNAKE_CASE ) ,proxies=kwargs.pop('proxies' ,__SCREAMING_SNAKE_CASE ) ,resume_download=kwargs.pop('resume_download' ,__SCREAMING_SNAKE_CASE ) ,local_files_only=kwargs.pop('local_files_only' ,__SCREAMING_SNAKE_CASE ) ,use_auth_token=kwargs.pop('use_auth_token' ,__SCREAMING_SNAKE_CASE ) ,revision=kwargs.pop('revision' ,__SCREAMING_SNAKE_CASE ) ,) if path is None: raise ValueError( F'`{os.path.join(self.speaker_embeddings.get("repo_or_path" ,"/" ) ,voice_preset_paths[key] )}` does not exists\n , no preloaded voice preset will be used - Make sure to provide correct paths to the {voice_preset}\n embeddings.' ) a__ = np.load(__SCREAMING_SNAKE_CASE ) return voice_preset_dict def lowerCamelCase__( self :List[str] ,__snake_case :Optional[int] = None ) -> Optional[int]: for key in ["semantic_prompt", "coarse_prompt", "fine_prompt"]: if key not in voice_preset: raise ValueError(F'Voice preset unrecognized, missing {key} as a key.' ) if not isinstance(voice_preset[key] ,np.ndarray ): raise ValueError(F'{key} voice preset must be a {str(self.preset_shape[key] )}D ndarray.' ) if len(voice_preset[key].shape ) != self.preset_shape[key]: raise ValueError(F'{key} voice preset must be a {str(self.preset_shape[key] )}D ndarray.' ) def __call__( self :Union[str, Any] ,__snake_case :Optional[int]=None ,__snake_case :int=None ,__snake_case :str="pt" ,__snake_case :Optional[int]=2_56 ,__snake_case :List[Any]=False ,__snake_case :List[str]=True ,__snake_case :Dict=False ,**__snake_case :List[Any] ,) -> List[str]: if voice_preset is not None and not isinstance(__SCREAMING_SNAKE_CASE ,__SCREAMING_SNAKE_CASE ): if ( isinstance(__SCREAMING_SNAKE_CASE ,__SCREAMING_SNAKE_CASE ) and self.speaker_embeddings is not None and voice_preset in self.speaker_embeddings ): a__ = self._load_voice_preset(__SCREAMING_SNAKE_CASE ) else: if isinstance(__SCREAMING_SNAKE_CASE ,__SCREAMING_SNAKE_CASE ) and not voice_preset.endswith('.npz' ): a__ = voice_preset + """.npz""" a__ = np.load(__SCREAMING_SNAKE_CASE ) if voice_preset is not None: self._validate_voice_preset_dict(__SCREAMING_SNAKE_CASE ,**__SCREAMING_SNAKE_CASE ) a__ = BatchFeature(data=__SCREAMING_SNAKE_CASE ,tensor_type=__SCREAMING_SNAKE_CASE ) a__ = self.tokenizer( __SCREAMING_SNAKE_CASE ,return_tensors=__SCREAMING_SNAKE_CASE ,padding='max_length' ,max_length=__SCREAMING_SNAKE_CASE ,return_attention_mask=__SCREAMING_SNAKE_CASE ,return_token_type_ids=__SCREAMING_SNAKE_CASE ,add_special_tokens=__SCREAMING_SNAKE_CASE ,**__SCREAMING_SNAKE_CASE ,) if voice_preset is not None: a__ = voice_preset return encoded_text
335
'''simple docstring''' from __future__ import annotations A_ : str = "Muhammad Umer Farooq" A_ : Optional[Any] = "MIT" A_ : int = "1.0.0" A_ : int = "Muhammad Umer Farooq" A_ : int = "[email protected]" A_ : Dict = "Alpha" import re from html.parser import HTMLParser from urllib import parse import requests class __snake_case ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE ): super().__init__() snake_case__ : list[str] = [] snake_case__ : List[Any] = domain def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): # Only parse the 'anchor' tag. if tag == "a": # Check the list of defined attributes. for name, value in attrs: # If href is defined, and not empty nor # print it. if name == "href" and value != "#" and value != "": # If not already in urls. if value not in self.urls: snake_case__ : str = parse.urljoin(self.domain , __SCREAMING_SNAKE_CASE ) self.urls.append(__SCREAMING_SNAKE_CASE ) def UpperCamelCase__ ( __magic_name__ : str ) -> str: '''simple docstring''' return ".".join(get_sub_domain_name(__magic_name__ ).split(""".""" )[-2:] ) def UpperCamelCase__ ( __magic_name__ : str ) -> str: '''simple docstring''' return parse.urlparse(__magic_name__ ).netloc def UpperCamelCase__ ( __magic_name__ : str = "https://github.com" ) -> list[str]: '''simple docstring''' snake_case__ : List[str] = get_domain_name(__magic_name__ ) # Initialize the parser snake_case__ : Optional[Any] = Parser(__magic_name__ ) try: # Open URL snake_case__ : Any = requests.get(__magic_name__ ) # pass the raw HTML to the parser to get links parser.feed(r.text ) # Get links and loop through snake_case__ : List[str] = set() for link in parser.urls: # open URL. # read = requests.get(link) try: snake_case__ : Tuple = requests.get(__magic_name__ ) # Get the valid email. snake_case__ : List[str] = re.findall("""[a-zA-Z0-9]+@""" + domain , read.text ) # If not in list then append it. for email in emails: valid_emails.add(__magic_name__ ) except ValueError: pass except ValueError: raise SystemExit(1 ) # Finally return a sorted list of email addresses with no duplicates. return sorted(__magic_name__ ) if __name__ == "__main__": A_ : str = emails_from_url("https://github.com") print(F'{len(emails)} emails found:') print("\n".join(sorted(emails)))
38
0
import inspect import os import unittest import torch import accelerate from accelerate import Accelerator from accelerate.test_utils import execute_subprocess_async, require_multi_gpu from accelerate.utils import patch_environment class lowercase ( unittest.TestCase ): def lowercase_ ( self ): """simple docstring""" lowerCAmelCase__ : List[str] = inspect.getfile(accelerate.test_utils ) lowerCAmelCase__ : Any = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ['''scripts''', '''test_script.py'''] ) lowerCAmelCase__ : List[str] = os.path.sep.join( mod_file.split(os.path.sep )[:-1] + ['''scripts''', '''test_distributed_data_loop.py'''] ) lowerCAmelCase__ : Optional[int] = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ['''scripts''', '''test_ops.py'''] ) @require_multi_gpu def lowercase_ ( self ): """simple docstring""" print(f'''Found {torch.cuda.device_count()} devices.''' ) lowerCAmelCase__ : List[Any] = ["""torchrun""", f'''--nproc_per_node={torch.cuda.device_count()}''', self.test_file_path] with patch_environment(omp_num_threads=1 ): execute_subprocess_async(__SCREAMING_SNAKE_CASE , env=os.environ.copy() ) @require_multi_gpu def lowercase_ ( self ): """simple docstring""" print(f'''Found {torch.cuda.device_count()} devices.''' ) lowerCAmelCase__ : Union[str, Any] = ["""torchrun""", f'''--nproc_per_node={torch.cuda.device_count()}''', self.operation_file_path] print(f'''Command: {cmd}''' ) with patch_environment(omp_num_threads=1 ): execute_subprocess_async(__SCREAMING_SNAKE_CASE , env=os.environ.copy() ) @require_multi_gpu def lowercase_ ( self ): """simple docstring""" lowerCAmelCase__ : Tuple = ["""torchrun""", f'''--nproc_per_node={torch.cuda.device_count()}''', inspect.getfile(self.__class__ )] with patch_environment(omp_num_threads=1 ): execute_subprocess_async(__SCREAMING_SNAKE_CASE , env=os.environ.copy() ) @require_multi_gpu def lowercase_ ( self ): """simple docstring""" print(f'''Found {torch.cuda.device_count()} devices, using 2 devices only''' ) lowerCAmelCase__ : Optional[Any] = ["""torchrun""", f'''--nproc_per_node={torch.cuda.device_count()}''', self.data_loop_file_path] with patch_environment(omp_num_threads=1 , cuda_visible_devices='''0,1''' ): execute_subprocess_async(__SCREAMING_SNAKE_CASE , env=os.environ.copy() ) if __name__ == "__main__": A__ : Union[str, Any] = Accelerator() A__ : List[Any] = (accelerator.state.process_index + 2, 1_0) A__ : Tuple = torch.randint(0, 1_0, shape).to(accelerator.device) A__ : List[Any] = "" A__ : Any = accelerator.pad_across_processes(tensor) if tensora.shape[0] != accelerator.state.num_processes + 1: error_msg += f"Found shape {tensora.shape} but should have {accelerator.state.num_processes + 1} at dim 0." if not torch.equal(tensora[: accelerator.state.process_index + 2], tensor): error_msg += "Tensors have different values." if not torch.all(tensora[accelerator.state.process_index + 2 :] == 0): error_msg += "Padding was not done with the right value (0)." A__ : Any = accelerator.pad_across_processes(tensor, pad_first=True) if tensora.shape[0] != accelerator.state.num_processes + 1: error_msg += f"Found shape {tensora.shape} but should have {accelerator.state.num_processes + 1} at dim 0." A__ : Optional[Any] = accelerator.state.num_processes - accelerator.state.process_index - 1 if not torch.equal(tensora[index:], tensor): error_msg += "Tensors have different values." if not torch.all(tensora[:index] == 0): error_msg += "Padding was not done with the right value (0)." # Raise error at the end to make sure we don't stop at the first failure. if len(error_msg) > 0: raise ValueError(error_msg)
233
'''simple docstring''' def UpperCamelCase__ ( __magic_name__ : List[Any] ) -> Tuple: '''simple docstring''' if not head: return True # split the list to two parts snake_case__ , snake_case__ : Dict = head.next, head while fast and fast.next: snake_case__ : Any = fast.next.next snake_case__ : int = slow.next snake_case__ : Dict = slow.next snake_case__ : List[str] = None # Don't forget here! But forget still works! # reverse the second part snake_case__ : Tuple = None while second: snake_case__ : Tuple = second.next snake_case__ : Any = node snake_case__ : str = second snake_case__ : Optional[Any] = nxt # compare two parts # second part has the same or one less node while node: if node.val != head.val: return False snake_case__ : List[Any] = node.next snake_case__ : int = head.next return True def UpperCamelCase__ ( __magic_name__ : Any ) -> Optional[Any]: '''simple docstring''' if not head or not head.next: return True # 1. Get the midpoint (slow) snake_case__ : List[Any] = head while fast and fast.next: snake_case__ , snake_case__ : Any = fast.next.next, slow.next # 2. Push the second half into the stack snake_case__ : Tuple = [slow.val] while slow.next: snake_case__ : Optional[Any] = slow.next stack.append(slow.val ) # 3. Comparison while stack: if stack.pop() != cur.val: return False snake_case__ : str = cur.next return True def UpperCamelCase__ ( __magic_name__ : Optional[Any] ) -> Tuple: '''simple docstring''' if not head or not head.next: return True snake_case__ : int = {} snake_case__ : Union[str, Any] = 0 while head: if head.val in d: d[head.val].append(__magic_name__ ) else: snake_case__ : Tuple = [pos] snake_case__ : Optional[Any] = head.next pos += 1 snake_case__ : int = pos - 1 snake_case__ : str = 0 for v in d.values(): if len(__magic_name__ ) % 2 != 0: middle += 1 else: snake_case__ : List[str] = 0 for i in range(0 , len(__magic_name__ ) ): if v[i] + v[len(__magic_name__ ) - 1 - step] != checksum: return False step += 1 if middle > 1: return False return True
38
0
def UpperCAmelCase__ ( lowerCamelCase_ : str , lowerCamelCase_ : list[str] ): __a : List[Any] = """""" for word_or_phrase in separated: if not isinstance(lowerCamelCase_ , lowerCamelCase_ ): raise Exception('join() accepts only strings to be joined' ) joined += word_or_phrase + separator return joined.strip(lowerCamelCase_ ) if __name__ == "__main__": from doctest import testmod testmod()
47
'''simple docstring''' import shutil import tempfile import unittest from transformers import SPIECE_UNDERLINE, BatchEncoding, MBartTokenizer, MBartTokenizerFast, is_torch_available from transformers.testing_utils import ( get_tests_dir, nested_simplify, require_sentencepiece, require_tokenizers, require_torch, ) from ...test_tokenization_common import TokenizerTesterMixin A_ : Union[str, Any] = get_tests_dir("fixtures/test_sentencepiece.model") if is_torch_available(): from transformers.models.mbart.modeling_mbart import shift_tokens_right A_ : str = 250004 A_ : str = 250020 @require_sentencepiece @require_tokenizers class __snake_case ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowerCamelCase__ = MBartTokenizer lowerCamelCase__ = MBartTokenizerFast lowerCamelCase__ = True lowerCamelCase__ = True def __UpperCamelCase ( self ): super().setUp() # We have a SentencePiece fixture for testing snake_case__ : Tuple = MBartTokenizer(__SCREAMING_SNAKE_CASE , keep_accents=__SCREAMING_SNAKE_CASE ) tokenizer.save_pretrained(self.tmpdirname ) def __UpperCamelCase ( self ): snake_case__ : Tuple = MBartTokenizer(__SCREAMING_SNAKE_CASE , keep_accents=__SCREAMING_SNAKE_CASE ) snake_case__ : int = tokenizer.tokenize("""This is a test""" ) self.assertListEqual(__SCREAMING_SNAKE_CASE , ["""▁This""", """▁is""", """▁a""", """▁t""", """est"""] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) , [value + tokenizer.fairseq_offset for value in [2_8_5, 4_6, 1_0, 1_7_0, 3_8_2]] , ) snake_case__ : Optional[int] = tokenizer.tokenize("""I was born in 92000, and this is falsé.""" ) self.assertListEqual( __SCREAMING_SNAKE_CASE , [ SPIECE_UNDERLINE + """I""", SPIECE_UNDERLINE + """was""", SPIECE_UNDERLINE + """b""", """or""", """n""", SPIECE_UNDERLINE + """in""", SPIECE_UNDERLINE + """""", """9""", """2""", """0""", """0""", """0""", """,""", SPIECE_UNDERLINE + """and""", SPIECE_UNDERLINE + """this""", SPIECE_UNDERLINE + """is""", SPIECE_UNDERLINE + """f""", """al""", """s""", """é""", """.""", ] , ) snake_case__ : Optional[int] = tokenizer.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) self.assertListEqual( __SCREAMING_SNAKE_CASE , [ value + tokenizer.fairseq_offset for value in [8, 2_1, 8_4, 5_5, 2_4, 1_9, 7, 2, 6_0_2, 3_4_7, 3_4_7, 3_4_7, 3, 1_2, 6_6, 4_6, 7_2, 8_0, 6, 2, 4] # ^ unk: 2 + 1 = 3 unk: 2 + 1 = 3 ^ ] , ) snake_case__ : Union[str, Any] = tokenizer.convert_ids_to_tokens(__SCREAMING_SNAKE_CASE ) self.assertListEqual( __SCREAMING_SNAKE_CASE , [ SPIECE_UNDERLINE + """I""", SPIECE_UNDERLINE + """was""", SPIECE_UNDERLINE + """b""", """or""", """n""", SPIECE_UNDERLINE + """in""", SPIECE_UNDERLINE + """""", """<unk>""", """2""", """0""", """0""", """0""", """,""", SPIECE_UNDERLINE + """and""", SPIECE_UNDERLINE + """this""", SPIECE_UNDERLINE + """is""", SPIECE_UNDERLINE + """f""", """al""", """s""", """<unk>""", """.""", ] , ) def __UpperCamelCase ( self ): if not self.test_slow_tokenizer: # as we don't have a slow version, we can't compare the outputs between slow and fast versions return snake_case__ : Optional[int] = (self.rust_tokenizer_class, """hf-internal-testing/tiny-random-mbart""", {}) for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f"{tokenizer.__class__.__name__} ({pretrained_name})" ): snake_case__ : Optional[Any] = self.rust_tokenizer_class.from_pretrained(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) snake_case__ : Dict = self.tokenizer_class.from_pretrained(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) snake_case__ : List[str] = tempfile.mkdtemp() snake_case__ : int = tokenizer_r.save_pretrained(__SCREAMING_SNAKE_CASE ) snake_case__ : Dict = tokenizer_p.save_pretrained(__SCREAMING_SNAKE_CASE ) # Checks it save with the same files + the tokenizer.json file for the fast one self.assertTrue(any("""tokenizer.json""" in f for f in tokenizer_r_files ) ) snake_case__ : List[str] = tuple(f for f in tokenizer_r_files if """tokenizer.json""" not in f ) self.assertSequenceEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # Checks everything loads correctly in the same way snake_case__ : Tuple = tokenizer_r.from_pretrained(__SCREAMING_SNAKE_CASE ) snake_case__ : Union[str, Any] = tokenizer_p.from_pretrained(__SCREAMING_SNAKE_CASE ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ) # self.assertEqual(getattr(tokenizer_rp, key), getattr(tokenizer_pp, key)) # self.assertEqual(getattr(tokenizer_rp, key + "_id"), getattr(tokenizer_pp, key + "_id")) shutil.rmtree(__SCREAMING_SNAKE_CASE ) # Save tokenizer rust, legacy_format=True snake_case__ : Any = tempfile.mkdtemp() snake_case__ : Optional[int] = tokenizer_r.save_pretrained(__SCREAMING_SNAKE_CASE , legacy_format=__SCREAMING_SNAKE_CASE ) snake_case__ : int = tokenizer_p.save_pretrained(__SCREAMING_SNAKE_CASE ) # Checks it save with the same files self.assertSequenceEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # Checks everything loads correctly in the same way snake_case__ : List[Any] = tokenizer_r.from_pretrained(__SCREAMING_SNAKE_CASE ) snake_case__ : Dict = tokenizer_p.from_pretrained(__SCREAMING_SNAKE_CASE ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ) shutil.rmtree(__SCREAMING_SNAKE_CASE ) # Save tokenizer rust, legacy_format=False snake_case__ : Dict = tempfile.mkdtemp() snake_case__ : Union[str, Any] = tokenizer_r.save_pretrained(__SCREAMING_SNAKE_CASE , legacy_format=__SCREAMING_SNAKE_CASE ) snake_case__ : Optional[int] = tokenizer_p.save_pretrained(__SCREAMING_SNAKE_CASE ) # Checks it saved the tokenizer.json file self.assertTrue(any("""tokenizer.json""" in f for f in tokenizer_r_files ) ) # Checks everything loads correctly in the same way snake_case__ : Dict = tokenizer_r.from_pretrained(__SCREAMING_SNAKE_CASE ) snake_case__ : Any = tokenizer_p.from_pretrained(__SCREAMING_SNAKE_CASE ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ) shutil.rmtree(__SCREAMING_SNAKE_CASE ) @require_torch @require_sentencepiece @require_tokenizers class __snake_case ( unittest.TestCase ): '''simple docstring''' lowerCamelCase__ = '''facebook/mbart-large-en-ro''' lowerCamelCase__ = [ ''' UN Chief Says There Is No Military Solution in Syria''', ''' Secretary-General Ban Ki-moon says his response to Russia\'s stepped up military support for Syria is that "there is no military solution" to the nearly five-year conflict and more weapons will only worsen the violence and misery for millions of people.''', ] lowerCamelCase__ = [ '''Şeful ONU declară că nu există o soluţie militară în Siria''', '''Secretarul General Ban Ki-moon declară că răspunsul său la intensificarea sprijinului militar al Rusiei''' ''' pentru Siria este că "nu există o soluţie militară" la conflictul de aproape cinci ani şi că noi arme nu vor''' ''' face decât să înrăutăţească violenţele şi mizeria pentru milioane de oameni.''', ] lowerCamelCase__ = [8_274, 127_873, 25_916, 7, 8_622, 2_071, 438, 67_485, 53, 187_895, 23, 51_712, 2, EN_CODE] @classmethod def __UpperCamelCase ( cls ): snake_case__ : MBartTokenizer = MBartTokenizer.from_pretrained( cls.checkpoint_name , src_lang="""en_XX""" , tgt_lang="""ro_RO""" ) snake_case__ : Any = 1 return cls def __UpperCamelCase ( self ): self.assertEqual(self.tokenizer.fairseq_tokens_to_ids["""ar_AR"""] , 2_5_0_0_0_1 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids["""en_EN"""] , 2_5_0_0_0_4 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids["""ro_RO"""] , 2_5_0_0_2_0 ) def __UpperCamelCase ( self ): snake_case__ : Tuple = self.tokenizer.batch_encode_plus(self.src_text ).input_ids[0] self.assertListEqual(self.expected_src_tokens , __SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self ): self.assertIn(__SCREAMING_SNAKE_CASE , self.tokenizer.all_special_ids ) snake_case__ : List[str] = [RO_CODE, 8_8_4, 9_0_1_9, 9_6, 9, 9_1_6, 8_6_7_9_2, 3_6, 1_8_7_4_3, 1_5_5_9_6, 5, 2] snake_case__ : List[Any] = self.tokenizer.decode(__SCREAMING_SNAKE_CASE , skip_special_tokens=__SCREAMING_SNAKE_CASE ) snake_case__ : Dict = self.tokenizer.decode(generated_ids[1:] , skip_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) self.assertNotIn(self.tokenizer.eos_token , __SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self ): snake_case__ : Dict = ["""this is gunna be a long sentence """ * 2_0] assert isinstance(src_text[0] , __SCREAMING_SNAKE_CASE ) snake_case__ : Tuple = 1_0 snake_case__ : int = self.tokenizer(__SCREAMING_SNAKE_CASE , max_length=__SCREAMING_SNAKE_CASE , truncation=__SCREAMING_SNAKE_CASE ).input_ids[0] self.assertEqual(ids[-2] , 2 ) self.assertEqual(ids[-1] , __SCREAMING_SNAKE_CASE ) self.assertEqual(len(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self ): self.assertListEqual(self.tokenizer.convert_tokens_to_ids(["""<mask>""", """ar_AR"""] ) , [2_5_0_0_2_6, 2_5_0_0_0_1] ) def __UpperCamelCase ( self ): snake_case__ : Union[str, Any] = tempfile.mkdtemp() snake_case__ : Dict = self.tokenizer.fairseq_tokens_to_ids self.tokenizer.save_pretrained(__SCREAMING_SNAKE_CASE ) snake_case__ : Any = MBartTokenizer.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertDictEqual(new_tok.fairseq_tokens_to_ids , __SCREAMING_SNAKE_CASE ) @require_torch def __UpperCamelCase ( self ): snake_case__ : Tuple = self.tokenizer(self.src_text , text_target=self.tgt_text , padding=__SCREAMING_SNAKE_CASE , return_tensors="""pt""" ) snake_case__ : int = shift_tokens_right(batch["""labels"""] , self.tokenizer.pad_token_id ) # fairseq batch: https://gist.github.com/sshleifer/cba08bc2109361a74ac3760a7e30e4f4 assert batch.input_ids[1][-2:].tolist() == [2, EN_CODE] assert batch.decoder_input_ids[1][0].tolist() == RO_CODE assert batch.decoder_input_ids[1][-1] == 2 assert batch.labels[1][-2:].tolist() == [2, RO_CODE] @require_torch def __UpperCamelCase ( self ): snake_case__ : Optional[int] = self.tokenizer( self.src_text , text_target=self.tgt_text , padding=__SCREAMING_SNAKE_CASE , truncation=__SCREAMING_SNAKE_CASE , max_length=len(self.expected_src_tokens ) , return_tensors="""pt""" , ) snake_case__ : List[str] = shift_tokens_right(batch["""labels"""] , self.tokenizer.pad_token_id ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) self.assertEqual((2, 1_4) , batch.input_ids.shape ) self.assertEqual((2, 1_4) , batch.attention_mask.shape ) snake_case__ : Tuple = batch.input_ids.tolist()[0] self.assertListEqual(self.expected_src_tokens , __SCREAMING_SNAKE_CASE ) self.assertEqual(2 , batch.decoder_input_ids[0, -1] ) # EOS # Test that special tokens are reset self.assertEqual(self.tokenizer.prefix_tokens , [] ) self.assertEqual(self.tokenizer.suffix_tokens , [self.tokenizer.eos_token_id, EN_CODE] ) def __UpperCamelCase ( self ): snake_case__ : Optional[int] = self.tokenizer(self.src_text , padding=__SCREAMING_SNAKE_CASE , truncation=__SCREAMING_SNAKE_CASE , max_length=3 , return_tensors="""pt""" ) snake_case__ : Optional[int] = self.tokenizer( text_target=self.tgt_text , padding=__SCREAMING_SNAKE_CASE , truncation=__SCREAMING_SNAKE_CASE , max_length=1_0 , return_tensors="""pt""" ) snake_case__ : str = targets["""input_ids"""] snake_case__ : Optional[Any] = shift_tokens_right(__SCREAMING_SNAKE_CASE , self.tokenizer.pad_token_id ) self.assertEqual(batch.input_ids.shape[1] , 3 ) self.assertEqual(batch.decoder_input_ids.shape[1] , 1_0 ) @require_torch def __UpperCamelCase ( self ): snake_case__ : Tuple = self.tokenizer._build_translation_inputs( """A test""" , return_tensors="""pt""" , src_lang="""en_XX""" , tgt_lang="""ar_AR""" ) self.assertEqual( nested_simplify(__SCREAMING_SNAKE_CASE ) , { # A, test, EOS, en_XX """input_ids""": [[6_2, 3_0_3_4, 2, 2_5_0_0_0_4]], """attention_mask""": [[1, 1, 1, 1]], # ar_AR """forced_bos_token_id""": 2_5_0_0_0_1, } , )
38
0
'''simple docstring''' import warnings from ...utils import logging from .image_processing_donut import DonutImageProcessor _a : Union[str, Any] = logging.get_logger(__name__) class _lowercase ( __SCREAMING_SNAKE_CASE ): def __init__( self : Optional[Any] , *SCREAMING_SNAKE_CASE_ : List[str] , **SCREAMING_SNAKE_CASE_ : List[Any] ) -> Tuple: warnings.warn( 'The class DonutFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please' ' use DonutImageProcessor instead.' , __SCREAMING_SNAKE_CASE , ) super().__init__(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE )
56
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices A_ : int = logging.get_logger(__name__) A_ : Dict = { "google/bit-50": "https://huggingface.co/google/bit-50/resolve/main/config.json", } class __snake_case ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): '''simple docstring''' lowerCamelCase__ = '''bit''' lowerCamelCase__ = ['''preactivation''', '''bottleneck'''] lowerCamelCase__ = ['''SAME''', '''VALID'''] def __init__( self , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=6_4 , __SCREAMING_SNAKE_CASE=[2_5_6, 5_1_2, 1_0_2_4, 2_0_4_8] , __SCREAMING_SNAKE_CASE=[3, 4, 6, 3] , __SCREAMING_SNAKE_CASE="preactivation" , __SCREAMING_SNAKE_CASE="relu" , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=3_2 , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=3_2 , __SCREAMING_SNAKE_CASE=1 , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE , ): super().__init__(**__SCREAMING_SNAKE_CASE ) if layer_type not in self.layer_types: raise ValueError(f"layer_type={layer_type} is not one of {','.join(self.layer_types )}" ) if global_padding is not None: if global_padding.upper() in self.supported_padding: snake_case__ : Tuple = global_padding.upper() else: raise ValueError(f"Padding strategy {global_padding} not supported" ) snake_case__ : List[str] = num_channels snake_case__ : Tuple = embedding_size snake_case__ : str = hidden_sizes snake_case__ : Optional[Any] = depths snake_case__ : List[Any] = layer_type snake_case__ : Dict = hidden_act snake_case__ : Union[str, Any] = global_padding snake_case__ : List[str] = num_groups snake_case__ : str = drop_path_rate snake_case__ : List[Any] = embedding_dynamic_padding snake_case__ : List[str] = output_stride snake_case__ : Dict = width_factor snake_case__ : List[str] = ["""stem"""] + [f"stage{idx}" for idx in range(1 , len(__SCREAMING_SNAKE_CASE ) + 1 )] snake_case__ , snake_case__ : Dict = get_aligned_output_features_output_indices( out_features=__SCREAMING_SNAKE_CASE , out_indices=__SCREAMING_SNAKE_CASE , stage_names=self.stage_names )
38
0
from .pipelines import DiffusionPipeline, ImagePipelineOutput # noqa: F401 from .utils import deprecate deprecate( """pipelines_utils""", """0.22.0""", """Importing `DiffusionPipeline` or `ImagePipelineOutput` from diffusers.pipeline_utils is deprecated. Please import from diffusers.pipelines.pipeline_utils instead.""", standard_warn=False, stacklevel=3, )
23
'''simple docstring''' import argparse import json from pathlib import Path import requests import timm import torch from huggingface_hub import hf_hub_download from PIL import Image from timm.data import resolve_data_config from timm.data.transforms_factory import create_transform from transformers import ( BitConfig, ViTHybridConfig, ViTHybridForImageClassification, ViTHybridImageProcessor, ViTHybridModel, ) from transformers.image_utils import PILImageResampling from transformers.utils import logging logging.set_verbosity_info() A_ : Optional[int] = logging.get_logger(__name__) def UpperCamelCase__ ( __magic_name__ : Optional[Any] , __magic_name__ : str=False ) -> Tuple: '''simple docstring''' snake_case__ : int = [] # fmt: off # stem: rename_keys.append(("""cls_token""", """vit.embeddings.cls_token""") ) rename_keys.append(("""pos_embed""", """vit.embeddings.position_embeddings""") ) rename_keys.append(("""patch_embed.proj.weight""", """vit.embeddings.patch_embeddings.projection.weight""") ) rename_keys.append(("""patch_embed.proj.bias""", """vit.embeddings.patch_embeddings.projection.bias""") ) # backbone rename_keys.append(("""patch_embed.backbone.stem.conv.weight""", """vit.embeddings.patch_embeddings.backbone.bit.embedder.convolution.weight""") ) rename_keys.append(("""patch_embed.backbone.stem.norm.weight""", """vit.embeddings.patch_embeddings.backbone.bit.embedder.norm.weight""") ) rename_keys.append(("""patch_embed.backbone.stem.norm.bias""", """vit.embeddings.patch_embeddings.backbone.bit.embedder.norm.bias""") ) for stage_idx in range(len(config.backbone_config.depths ) ): for layer_idx in range(config.backbone_config.depths[stage_idx] ): rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.conv1.weight", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.conv1.weight") ) rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm1.weight", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm1.weight") ) rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm1.bias", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm1.bias") ) rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.conv2.weight", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.conv2.weight") ) rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm2.weight", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm2.weight") ) rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm2.bias", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm2.bias") ) rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.conv3.weight", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.conv3.weight") ) rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm3.weight", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm3.weight") ) rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm3.bias", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm3.bias") ) rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.0.downsample.conv.weight", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.0.downsample.conv.weight") ) rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.0.downsample.norm.weight", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.0.downsample.norm.weight") ) rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.0.downsample.norm.bias", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.0.downsample.norm.bias") ) # transformer encoder for i in range(config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append((f"blocks.{i}.norm1.weight", f"vit.encoder.layer.{i}.layernorm_before.weight") ) rename_keys.append((f"blocks.{i}.norm1.bias", f"vit.encoder.layer.{i}.layernorm_before.bias") ) rename_keys.append((f"blocks.{i}.attn.proj.weight", f"vit.encoder.layer.{i}.attention.output.dense.weight") ) rename_keys.append((f"blocks.{i}.attn.proj.bias", f"vit.encoder.layer.{i}.attention.output.dense.bias") ) rename_keys.append((f"blocks.{i}.norm2.weight", f"vit.encoder.layer.{i}.layernorm_after.weight") ) rename_keys.append((f"blocks.{i}.norm2.bias", f"vit.encoder.layer.{i}.layernorm_after.bias") ) rename_keys.append((f"blocks.{i}.mlp.fc1.weight", f"vit.encoder.layer.{i}.intermediate.dense.weight") ) rename_keys.append((f"blocks.{i}.mlp.fc1.bias", f"vit.encoder.layer.{i}.intermediate.dense.bias") ) rename_keys.append((f"blocks.{i}.mlp.fc2.weight", f"vit.encoder.layer.{i}.output.dense.weight") ) rename_keys.append((f"blocks.{i}.mlp.fc2.bias", f"vit.encoder.layer.{i}.output.dense.bias") ) if base_model: # layernorm + pooler rename_keys.extend( [ ("""norm.weight""", """layernorm.weight"""), ("""norm.bias""", """layernorm.bias"""), ("""pre_logits.fc.weight""", """pooler.dense.weight"""), ("""pre_logits.fc.bias""", """pooler.dense.bias"""), ] ) # if just the base model, we should remove "vit" from all keys that start with "vit" snake_case__ : List[Any] = [(pair[0], pair[1][4:]) if pair[1].startswith("""vit""" ) else pair for pair in rename_keys] else: # layernorm + classification head rename_keys.extend( [ ("""norm.weight""", """vit.layernorm.weight"""), ("""norm.bias""", """vit.layernorm.bias"""), ("""head.weight""", """classifier.weight"""), ("""head.bias""", """classifier.bias"""), ] ) # fmt: on return rename_keys def UpperCamelCase__ ( __magic_name__ : Tuple , __magic_name__ : int , __magic_name__ : Tuple=False ) -> str: '''simple docstring''' for i in range(config.num_hidden_layers ): if base_model: snake_case__ : int = """""" else: snake_case__ : Dict = """vit.""" # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) snake_case__ : int = state_dict.pop(f"blocks.{i}.attn.qkv.weight" ) snake_case__ : Union[str, Any] = state_dict.pop(f"blocks.{i}.attn.qkv.bias" ) # next, add query, keys and values (in that order) to the state dict snake_case__ : Optional[int] = in_proj_weight[ : config.hidden_size, : ] snake_case__ : Optional[Any] = in_proj_bias[: config.hidden_size] snake_case__ : List[Any] = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] snake_case__ : List[Any] = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] snake_case__ : List[Any] = in_proj_weight[ -config.hidden_size :, : ] snake_case__ : Optional[int] = in_proj_bias[-config.hidden_size :] def UpperCamelCase__ ( __magic_name__ : Optional[Any] ) -> List[str]: '''simple docstring''' snake_case__ : str = ["""head.weight""", """head.bias"""] for k in ignore_keys: state_dict.pop(__magic_name__ , __magic_name__ ) def UpperCamelCase__ ( __magic_name__ : List[str] , __magic_name__ : Union[str, Any] , __magic_name__ : str ) -> Union[str, Any]: '''simple docstring''' snake_case__ : List[str] = dct.pop(__magic_name__ ) snake_case__ : Dict = val def UpperCamelCase__ ( ) -> str: '''simple docstring''' snake_case__ : Optional[int] = """http://images.cocodataset.org/val2017/000000039769.jpg""" snake_case__ : Optional[int] = Image.open(requests.get(__magic_name__ , stream=__magic_name__ ).raw ) return im @torch.no_grad() def UpperCamelCase__ ( __magic_name__ : List[Any] , __magic_name__ : Union[str, Any] , __magic_name__ : int=False ) -> Optional[int]: '''simple docstring''' snake_case__ : int = BitConfig( global_padding="""same""" , layer_type="""bottleneck""" , depths=(3, 4, 9) , out_features=["""stage3"""] , embedding_dynamic_padding=__magic_name__ , ) snake_case__ : Optional[int] = ViTHybridConfig(backbone_config=__magic_name__ , image_size=3_84 , num_labels=10_00 ) snake_case__ : Union[str, Any] = False # load original model from timm snake_case__ : List[Any] = timm.create_model(__magic_name__ , pretrained=__magic_name__ ) timm_model.eval() # load state_dict of original model, remove and rename some keys snake_case__ : Optional[int] = timm_model.state_dict() if base_model: remove_classification_head_(__magic_name__ ) snake_case__ : int = create_rename_keys(__magic_name__ , __magic_name__ ) for src, dest in rename_keys: rename_key(__magic_name__ , __magic_name__ , __magic_name__ ) read_in_q_k_v(__magic_name__ , __magic_name__ , __magic_name__ ) snake_case__ : str = """huggingface/label-files""" snake_case__ : Union[str, Any] = """imagenet-1k-id2label.json""" snake_case__ : Dict = json.load(open(hf_hub_download(__magic_name__ , __magic_name__ , repo_type="""dataset""" ) , """r""" ) ) snake_case__ : List[Any] = {int(__magic_name__ ): v for k, v in idalabel.items()} snake_case__ : int = idalabel snake_case__ : str = {v: k for k, v in idalabel.items()} # load HuggingFace model if vit_name[-5:] == "in21k": snake_case__ : str = ViTHybridModel(__magic_name__ ).eval() else: snake_case__ : Union[str, Any] = ViTHybridForImageClassification(__magic_name__ ).eval() model.load_state_dict(__magic_name__ ) # create image processor snake_case__ : Optional[Any] = create_transform(**resolve_data_config({} , model=__magic_name__ ) ) snake_case__ : Union[str, Any] = transform.transforms snake_case__ : Tuple = { """bilinear""": PILImageResampling.BILINEAR, """bicubic""": PILImageResampling.BICUBIC, """nearest""": PILImageResampling.NEAREST, } snake_case__ : Any = ViTHybridImageProcessor( do_resize=__magic_name__ , size={"""shortest_edge""": timm_transforms[0].size} , resample=pillow_resamplings[timm_transforms[0].interpolation.value] , do_center_crop=__magic_name__ , crop_size={"""height""": timm_transforms[1].size[0], """width""": timm_transforms[1].size[1]} , do_normalize=__magic_name__ , image_mean=timm_transforms[-1].mean.tolist() , image_std=timm_transforms[-1].std.tolist() , ) snake_case__ : Any = prepare_img() snake_case__ : int = transform(__magic_name__ ).unsqueeze(0 ) snake_case__ : List[str] = processor(__magic_name__ , return_tensors="""pt""" ).pixel_values # verify pixel values assert torch.allclose(__magic_name__ , __magic_name__ ) # verify logits with torch.no_grad(): snake_case__ : Optional[Any] = model(__magic_name__ ) snake_case__ : Union[str, Any] = outputs.logits print("""Predicted class:""" , logits.argmax(-1 ).item() ) if base_model: snake_case__ : Dict = timm_model.forward_features(__magic_name__ ) assert timm_pooled_output.shape == outputs.pooler_output.shape assert torch.allclose(__magic_name__ , outputs.pooler_output , atol=1E-3 ) else: snake_case__ : int = timm_model(__magic_name__ ) assert timm_logits.shape == outputs.logits.shape assert torch.allclose(__magic_name__ , outputs.logits , atol=1E-3 ) print("""Looks ok!""" ) if pytorch_dump_folder_path is not None: Path(__magic_name__ ).mkdir(exist_ok=__magic_name__ ) print(f"Saving model {vit_name} to {pytorch_dump_folder_path}" ) model.save_pretrained(__magic_name__ ) print(f"Saving processor to {pytorch_dump_folder_path}" ) processor.save_pretrained(__magic_name__ ) if push_to_hub: print(f"Pushing model and processor to the hub {vit_name}" ) model.push_to_hub(f"ybelkada/{vit_name}" ) processor.push_to_hub(f"ybelkada/{vit_name}" ) if __name__ == "__main__": A_ : List[str] = argparse.ArgumentParser() # Required parameters parser.add_argument( "--vit_name", default="vit_base_r50_s16_384", type=str, help="Name of the hybrid ViT timm model you'd like to convert.", ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model directory." ) parser.add_argument( "--push_to_hub", action="store_true", help="Whether to upload the model to the HuggingFace hub." ) A_ : Union[str, Any] = parser.parse_args() convert_vit_checkpoint(args.vit_name, args.pytorch_dump_folder_path, args.push_to_hub)
38
0
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 UpperCamelCase_ ( unittest.TestCase ): '''simple docstring''' def __init__( self , UpperCamelCase , UpperCamelCase=7 , UpperCamelCase=3 , UpperCamelCase=18 , UpperCamelCase=30 , UpperCamelCase=4_00 , UpperCamelCase=True , UpperCamelCase=None , UpperCamelCase=True , UpperCamelCase=[0.5, 0.5, 0.5] , UpperCamelCase=[0.5, 0.5, 0.5] , ) -> List[str]: UpperCamelCase__ : Any = size if size is not None else {"""height""": 18, """width""": 18} UpperCamelCase__ : List[Any] = parent UpperCamelCase__ : int = batch_size UpperCamelCase__ : List[Any] = num_channels UpperCamelCase__ : str = image_size UpperCamelCase__ : Union[str, Any] = min_resolution UpperCamelCase__ : List[Any] = max_resolution UpperCamelCase__ : Tuple = do_resize UpperCamelCase__ : int = size UpperCamelCase__ : Tuple = do_normalize UpperCamelCase__ : Dict = image_mean UpperCamelCase__ : Union[str, Any] = image_std def lowerCAmelCase__ ( self) -> List[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 UpperCamelCase_ ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' UpperCamelCase_ = DPTImageProcessor if is_vision_available() else None def lowerCAmelCase__ ( self) -> List[str]: UpperCamelCase__ : str = DPTImageProcessingTester(self) @property def lowerCAmelCase__ ( self) -> Union[str, Any]: return self.image_processor_tester.prepare_image_processor_dict() def lowerCAmelCase__ ( self) -> Optional[Any]: UpperCamelCase__ : Optional[int] = self.image_processing_class(**self.image_processor_dict) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , 'image_mean')) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , 'image_std')) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , 'do_normalize')) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , 'do_resize')) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , 'size')) def lowerCAmelCase__ ( self) -> str: UpperCamelCase__ : Any = self.image_processing_class.from_dict(self.image_processor_dict) self.assertEqual(image_processor.size , {'height': 18, 'width': 18}) UpperCamelCase__ : List[Any] = self.image_processing_class.from_dict(self.image_processor_dict , size=42) self.assertEqual(image_processor.size , {'height': 42, 'width': 42}) def lowerCAmelCase__ ( self) -> Optional[int]: # Initialize image_processing UpperCamelCase__ : Optional[int] = self.image_processing_class(**self.image_processor_dict) # create random PIL images UpperCamelCase__ : List[Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=__SCREAMING_SNAKE_CASE) for image in image_inputs: self.assertIsInstance(__SCREAMING_SNAKE_CASE , Image.Image) # Test not batched input UpperCamelCase__ : Optional[int] = 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 UpperCamelCase__ : List[str] = image_processing(__SCREAMING_SNAKE_CASE , 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 lowerCAmelCase__ ( self) -> Dict: # Initialize image_processing UpperCamelCase__ : Optional[Any] = self.image_processing_class(**self.image_processor_dict) # create random numpy tensors UpperCamelCase__ : Optional[int] = prepare_image_inputs(self.image_processor_tester , equal_resolution=__SCREAMING_SNAKE_CASE , numpify=__SCREAMING_SNAKE_CASE) for image in image_inputs: self.assertIsInstance(__SCREAMING_SNAKE_CASE , np.ndarray) # Test not batched input UpperCamelCase__ : List[str] = image_processing(image_inputs[0] , return_tensors='pt').pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size['height'], self.image_processor_tester.size['width'], ) , ) # Test batched UpperCamelCase__ : Any = image_processing(__SCREAMING_SNAKE_CASE , 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 lowerCAmelCase__ ( self) -> List[str]: # Initialize image_processing UpperCamelCase__ : List[Any] = self.image_processing_class(**self.image_processor_dict) # create random PyTorch tensors UpperCamelCase__ : List[str] = prepare_image_inputs(self.image_processor_tester , equal_resolution=__SCREAMING_SNAKE_CASE , torchify=__SCREAMING_SNAKE_CASE) for image in image_inputs: self.assertIsInstance(__SCREAMING_SNAKE_CASE , torch.Tensor) # Test not batched input UpperCamelCase__ : List[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 UpperCamelCase__ : List[str] = image_processing(__SCREAMING_SNAKE_CASE , 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'], ) , )
410
'''simple docstring''' from dataclasses import dataclass from typing import Optional import numpy as np import torch import torch.nn as nn from ..utils import BaseOutput, is_torch_version, randn_tensor from .attention_processor import SpatialNorm from .unet_ad_blocks import UNetMidBlockaD, get_down_block, get_up_block @dataclass class __snake_case ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' lowerCamelCase__ = 42 class __snake_case ( nn.Module ): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=("DownEncoderBlock2D",) , __SCREAMING_SNAKE_CASE=(6_4,) , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=3_2 , __SCREAMING_SNAKE_CASE="silu" , __SCREAMING_SNAKE_CASE=True , ): super().__init__() snake_case__ : str = layers_per_block snake_case__ : int = torch.nn.Convad( __SCREAMING_SNAKE_CASE , block_out_channels[0] , kernel_size=3 , stride=1 , padding=1 , ) snake_case__ : List[Any] = None snake_case__ : List[Any] = nn.ModuleList([] ) # down snake_case__ : Union[str, Any] = block_out_channels[0] for i, down_block_type in enumerate(__SCREAMING_SNAKE_CASE ): snake_case__ : Optional[Any] = output_channel snake_case__ : Union[str, Any] = block_out_channels[i] snake_case__ : int = i == len(__SCREAMING_SNAKE_CASE ) - 1 snake_case__ : str = get_down_block( __SCREAMING_SNAKE_CASE , num_layers=self.layers_per_block , in_channels=__SCREAMING_SNAKE_CASE , out_channels=__SCREAMING_SNAKE_CASE , add_downsample=not is_final_block , resnet_eps=1e-6 , downsample_padding=0 , resnet_act_fn=__SCREAMING_SNAKE_CASE , resnet_groups=__SCREAMING_SNAKE_CASE , attention_head_dim=__SCREAMING_SNAKE_CASE , temb_channels=__SCREAMING_SNAKE_CASE , ) self.down_blocks.append(__SCREAMING_SNAKE_CASE ) # mid snake_case__ : Optional[Any] = UNetMidBlockaD( in_channels=block_out_channels[-1] , resnet_eps=1e-6 , resnet_act_fn=__SCREAMING_SNAKE_CASE , output_scale_factor=1 , resnet_time_scale_shift="""default""" , attention_head_dim=block_out_channels[-1] , resnet_groups=__SCREAMING_SNAKE_CASE , temb_channels=__SCREAMING_SNAKE_CASE , ) # out snake_case__ : Tuple = nn.GroupNorm(num_channels=block_out_channels[-1] , num_groups=__SCREAMING_SNAKE_CASE , eps=1e-6 ) snake_case__ : Tuple = nn.SiLU() snake_case__ : str = 2 * out_channels if double_z else out_channels snake_case__ : int = nn.Convad(block_out_channels[-1] , __SCREAMING_SNAKE_CASE , 3 , padding=1 ) snake_case__ : Union[str, Any] = False def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE ): snake_case__ : Optional[Any] = x snake_case__ : int = self.conv_in(__SCREAMING_SNAKE_CASE ) if self.training and self.gradient_checkpointing: def create_custom_forward(__SCREAMING_SNAKE_CASE ): def custom_forward(*__SCREAMING_SNAKE_CASE ): return module(*__SCREAMING_SNAKE_CASE ) return custom_forward # down if is_torch_version(""">=""" , """1.11.0""" ): for down_block in self.down_blocks: snake_case__ : List[Any] = torch.utils.checkpoint.checkpoint( create_custom_forward(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE , use_reentrant=__SCREAMING_SNAKE_CASE ) # middle snake_case__ : List[Any] = torch.utils.checkpoint.checkpoint( create_custom_forward(self.mid_block ) , __SCREAMING_SNAKE_CASE , use_reentrant=__SCREAMING_SNAKE_CASE ) else: for down_block in self.down_blocks: snake_case__ : Dict = torch.utils.checkpoint.checkpoint(create_custom_forward(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) # middle snake_case__ : str = torch.utils.checkpoint.checkpoint(create_custom_forward(self.mid_block ) , __SCREAMING_SNAKE_CASE ) else: # down for down_block in self.down_blocks: snake_case__ : List[str] = down_block(__SCREAMING_SNAKE_CASE ) # middle snake_case__ : str = self.mid_block(__SCREAMING_SNAKE_CASE ) # post-process snake_case__ : Any = self.conv_norm_out(__SCREAMING_SNAKE_CASE ) snake_case__ : List[str] = self.conv_act(__SCREAMING_SNAKE_CASE ) snake_case__ : str = self.conv_out(__SCREAMING_SNAKE_CASE ) return sample class __snake_case ( nn.Module ): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=("UpDecoderBlock2D",) , __SCREAMING_SNAKE_CASE=(6_4,) , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=3_2 , __SCREAMING_SNAKE_CASE="silu" , __SCREAMING_SNAKE_CASE="group" , ): super().__init__() snake_case__ : Any = layers_per_block snake_case__ : Optional[Any] = nn.Convad( __SCREAMING_SNAKE_CASE , block_out_channels[-1] , kernel_size=3 , stride=1 , padding=1 , ) snake_case__ : Union[str, Any] = None snake_case__ : Dict = nn.ModuleList([] ) snake_case__ : Optional[int] = in_channels if norm_type == """spatial""" else None # mid snake_case__ : Tuple = UNetMidBlockaD( in_channels=block_out_channels[-1] , resnet_eps=1e-6 , resnet_act_fn=__SCREAMING_SNAKE_CASE , output_scale_factor=1 , resnet_time_scale_shift="""default""" if norm_type == """group""" else norm_type , attention_head_dim=block_out_channels[-1] , resnet_groups=__SCREAMING_SNAKE_CASE , temb_channels=__SCREAMING_SNAKE_CASE , ) # up snake_case__ : List[Any] = list(reversed(__SCREAMING_SNAKE_CASE ) ) snake_case__ : Optional[Any] = reversed_block_out_channels[0] for i, up_block_type in enumerate(__SCREAMING_SNAKE_CASE ): snake_case__ : List[Any] = output_channel snake_case__ : Optional[Any] = reversed_block_out_channels[i] snake_case__ : List[str] = i == len(__SCREAMING_SNAKE_CASE ) - 1 snake_case__ : int = get_up_block( __SCREAMING_SNAKE_CASE , num_layers=self.layers_per_block + 1 , in_channels=__SCREAMING_SNAKE_CASE , out_channels=__SCREAMING_SNAKE_CASE , prev_output_channel=__SCREAMING_SNAKE_CASE , add_upsample=not is_final_block , resnet_eps=1e-6 , resnet_act_fn=__SCREAMING_SNAKE_CASE , resnet_groups=__SCREAMING_SNAKE_CASE , attention_head_dim=__SCREAMING_SNAKE_CASE , temb_channels=__SCREAMING_SNAKE_CASE , resnet_time_scale_shift=__SCREAMING_SNAKE_CASE , ) self.up_blocks.append(__SCREAMING_SNAKE_CASE ) snake_case__ : int = output_channel # out if norm_type == "spatial": snake_case__ : List[Any] = SpatialNorm(block_out_channels[0] , __SCREAMING_SNAKE_CASE ) else: snake_case__ : Any = nn.GroupNorm(num_channels=block_out_channels[0] , num_groups=__SCREAMING_SNAKE_CASE , eps=1e-6 ) snake_case__ : Tuple = nn.SiLU() snake_case__ : Union[str, Any] = nn.Convad(block_out_channels[0] , __SCREAMING_SNAKE_CASE , 3 , padding=1 ) snake_case__ : int = False def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None ): snake_case__ : Union[str, Any] = z snake_case__ : Any = self.conv_in(__SCREAMING_SNAKE_CASE ) snake_case__ : Optional[Any] = next(iter(self.up_blocks.parameters() ) ).dtype if self.training and self.gradient_checkpointing: def create_custom_forward(__SCREAMING_SNAKE_CASE ): def custom_forward(*__SCREAMING_SNAKE_CASE ): return module(*__SCREAMING_SNAKE_CASE ) return custom_forward if is_torch_version(""">=""" , """1.11.0""" ): # middle snake_case__ : int = torch.utils.checkpoint.checkpoint( create_custom_forward(self.mid_block ) , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , use_reentrant=__SCREAMING_SNAKE_CASE ) snake_case__ : int = sample.to(__SCREAMING_SNAKE_CASE ) # up for up_block in self.up_blocks: snake_case__ : List[str] = torch.utils.checkpoint.checkpoint( create_custom_forward(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , use_reentrant=__SCREAMING_SNAKE_CASE ) else: # middle snake_case__ : Dict = torch.utils.checkpoint.checkpoint( create_custom_forward(self.mid_block ) , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) snake_case__ : Optional[Any] = sample.to(__SCREAMING_SNAKE_CASE ) # up for up_block in self.up_blocks: snake_case__ : str = torch.utils.checkpoint.checkpoint(create_custom_forward(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else: # middle snake_case__ : List[Any] = self.mid_block(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) snake_case__ : List[Any] = sample.to(__SCREAMING_SNAKE_CASE ) # up for up_block in self.up_blocks: snake_case__ : Dict = up_block(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # post-process if latent_embeds is None: snake_case__ : Optional[Any] = self.conv_norm_out(__SCREAMING_SNAKE_CASE ) else: snake_case__ : str = self.conv_norm_out(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) snake_case__ : Any = self.conv_act(__SCREAMING_SNAKE_CASE ) snake_case__ : Optional[Any] = self.conv_out(__SCREAMING_SNAKE_CASE ) return sample class __snake_case ( nn.Module ): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE="random" , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=True ): super().__init__() snake_case__ : int = n_e snake_case__ : Optional[int] = vq_embed_dim snake_case__ : int = beta snake_case__ : Optional[int] = legacy snake_case__ : Dict = nn.Embedding(self.n_e , self.vq_embed_dim ) self.embedding.weight.data.uniform_(-1.0 / self.n_e , 1.0 / self.n_e ) snake_case__ : List[str] = remap if self.remap is not None: self.register_buffer("""used""" , torch.tensor(np.load(self.remap ) ) ) snake_case__ : Optional[Any] = self.used.shape[0] snake_case__ : List[str] = unknown_index # "random" or "extra" or integer if self.unknown_index == "extra": snake_case__ : Dict = self.re_embed snake_case__ : List[str] = self.re_embed + 1 print( f"Remapping {self.n_e} indices to {self.re_embed} indices. " f"Using {self.unknown_index} for unknown indices." ) else: snake_case__ : Union[str, Any] = n_e snake_case__ : str = sane_index_shape def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE ): snake_case__ : Any = inds.shape assert len(__SCREAMING_SNAKE_CASE ) > 1 snake_case__ : Dict = inds.reshape(ishape[0] , -1 ) snake_case__ : Any = self.used.to(__SCREAMING_SNAKE_CASE ) snake_case__ : Dict = (inds[:, :, None] == used[None, None, ...]).long() snake_case__ : List[Any] = match.argmax(-1 ) snake_case__ : List[str] = match.sum(2 ) < 1 if self.unknown_index == "random": snake_case__ : List[str] = torch.randint(0 , self.re_embed , size=new[unknown].shape ).to(device=new.device ) else: snake_case__ : Optional[Any] = self.unknown_index return new.reshape(__SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE ): snake_case__ : List[Any] = inds.shape assert len(__SCREAMING_SNAKE_CASE ) > 1 snake_case__ : int = inds.reshape(ishape[0] , -1 ) snake_case__ : Optional[int] = self.used.to(__SCREAMING_SNAKE_CASE ) if self.re_embed > self.used.shape[0]: # extra token snake_case__ : List[Any] = 0 # simply set to zero snake_case__ : Union[str, Any] = torch.gather(used[None, :][inds.shape[0] * [0], :] , 1 , __SCREAMING_SNAKE_CASE ) return back.reshape(__SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE ): # reshape z -> (batch, height, width, channel) and flatten snake_case__ : Any = z.permute(0 , 2 , 3 , 1 ).contiguous() snake_case__ : Optional[Any] = z.view(-1 , self.vq_embed_dim ) # distances from z to embeddings e_j (z - e)^2 = z^2 + e^2 - 2 e * z snake_case__ : Dict = torch.argmin(torch.cdist(__SCREAMING_SNAKE_CASE , self.embedding.weight ) , dim=1 ) snake_case__ : Union[str, Any] = self.embedding(__SCREAMING_SNAKE_CASE ).view(z.shape ) snake_case__ : List[str] = None snake_case__ : Union[str, Any] = None # compute loss for embedding if not self.legacy: snake_case__ : Tuple = self.beta * torch.mean((z_q.detach() - z) ** 2 ) + torch.mean((z_q - z.detach()) ** 2 ) else: snake_case__ : List[Any] = torch.mean((z_q.detach() - z) ** 2 ) + self.beta * torch.mean((z_q - z.detach()) ** 2 ) # preserve gradients snake_case__ : Any = z + (z_q - z).detach() # reshape back to match original input shape snake_case__ : Union[str, Any] = z_q.permute(0 , 3 , 1 , 2 ).contiguous() if self.remap is not None: snake_case__ : List[Any] = min_encoding_indices.reshape(z.shape[0] , -1 ) # add batch axis snake_case__ : str = self.remap_to_used(__SCREAMING_SNAKE_CASE ) snake_case__ : str = min_encoding_indices.reshape(-1 , 1 ) # flatten if self.sane_index_shape: snake_case__ : Tuple = min_encoding_indices.reshape(z_q.shape[0] , z_q.shape[2] , z_q.shape[3] ) return z_q, loss, (perplexity, min_encodings, min_encoding_indices) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): # shape specifying (batch, height, width, channel) if self.remap is not None: snake_case__ : List[Any] = indices.reshape(shape[0] , -1 ) # add batch axis snake_case__ : Optional[int] = self.unmap_to_all(__SCREAMING_SNAKE_CASE ) snake_case__ : Optional[Any] = indices.reshape(-1 ) # flatten again # get quantized latent vectors snake_case__ : int = self.embedding(__SCREAMING_SNAKE_CASE ) if shape is not None: snake_case__ : str = z_q.view(__SCREAMING_SNAKE_CASE ) # reshape back to match original input shape snake_case__ : str = z_q.permute(0 , 3 , 1 , 2 ).contiguous() return z_q class __snake_case ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=False ): snake_case__ : Tuple = parameters snake_case__ , snake_case__ : Any = torch.chunk(__SCREAMING_SNAKE_CASE , 2 , dim=1 ) snake_case__ : Union[str, Any] = torch.clamp(self.logvar , -30.0 , 20.0 ) snake_case__ : Optional[int] = deterministic snake_case__ : Optional[int] = torch.exp(0.5 * self.logvar ) snake_case__ : Any = torch.exp(self.logvar ) if self.deterministic: snake_case__ : List[str] = torch.zeros_like( self.mean , device=self.parameters.device , dtype=self.parameters.dtype ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE = None ): # make sure sample is on the same device as the parameters and has same dtype snake_case__ : Dict = randn_tensor( self.mean.shape , generator=__SCREAMING_SNAKE_CASE , device=self.parameters.device , dtype=self.parameters.dtype ) snake_case__ : Optional[int] = self.mean + self.std * sample return x def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE=None ): if self.deterministic: return torch.Tensor([0.0] ) else: if other is None: return 0.5 * torch.sum(torch.pow(self.mean , 2 ) + self.var - 1.0 - self.logvar , dim=[1, 2, 3] ) else: return 0.5 * torch.sum( torch.pow(self.mean - other.mean , 2 ) / other.var + self.var / other.var - 1.0 - self.logvar + other.logvar , dim=[1, 2, 3] , ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=[1, 2, 3] ): if self.deterministic: return torch.Tensor([0.0] ) snake_case__ : Any = np.log(2.0 * np.pi ) return 0.5 * torch.sum(logtwopi + self.logvar + torch.pow(sample - self.mean , 2 ) / self.var , dim=__SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self ): return self.mean
38
0
from torch import nn def __A ( _A ): """simple docstring""" if act_fn in ["swish", "silu"]: return nn.SiLU() elif act_fn == "mish": return nn.Mish() elif act_fn == "gelu": return nn.GELU() else: raise ValueError(f"""Unsupported activation function: {act_fn}""" )
197
'''simple docstring''' import inspect import unittest from transformers import DPTConfig from transformers.file_utils import is_torch_available, is_vision_available from transformers.models.auto import get_values from transformers.testing_utils import require_torch, require_vision, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import MODEL_MAPPING, DPTForDepthEstimation, DPTForSemanticSegmentation, DPTModel from transformers.models.dpt.modeling_dpt import DPT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import DPTImageProcessor class __snake_case : '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=3_2 , __SCREAMING_SNAKE_CASE=1_6 , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=3_2 , __SCREAMING_SNAKE_CASE=4 , __SCREAMING_SNAKE_CASE=[0, 1, 2, 3] , __SCREAMING_SNAKE_CASE=4 , __SCREAMING_SNAKE_CASE=3_7 , __SCREAMING_SNAKE_CASE="gelu" , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=0.02 , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=[1, 3_8_4, 2_4, 2_4] , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=None , ): snake_case__ : str = parent snake_case__ : Union[str, Any] = batch_size snake_case__ : Union[str, Any] = image_size snake_case__ : Optional[int] = patch_size snake_case__ : List[str] = num_channels snake_case__ : Any = is_training snake_case__ : int = use_labels snake_case__ : str = hidden_size snake_case__ : Tuple = num_hidden_layers snake_case__ : str = backbone_out_indices snake_case__ : List[Any] = num_attention_heads snake_case__ : Dict = intermediate_size snake_case__ : Optional[Any] = hidden_act snake_case__ : str = hidden_dropout_prob snake_case__ : int = attention_probs_dropout_prob snake_case__ : Dict = initializer_range snake_case__ : Optional[int] = num_labels snake_case__ : str = backbone_featmap_shape snake_case__ : List[Any] = scope snake_case__ : Optional[Any] = is_hybrid # sequence length of DPT = num_patches + 1 (we add 1 for the [CLS] token) snake_case__ : List[Any] = (image_size // patch_size) ** 2 snake_case__ : Union[str, Any] = num_patches + 1 def __UpperCamelCase ( self ): snake_case__ : Tuple = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) snake_case__ : str = None if self.use_labels: snake_case__ : Dict = ids_tensor([self.batch_size, self.image_size, self.image_size] , self.num_labels ) snake_case__ : Union[str, Any] = self.get_config() return config, pixel_values, labels def __UpperCamelCase ( self ): snake_case__ : Any = { """global_padding""": """same""", """layer_type""": """bottleneck""", """depths""": [3, 4, 9], """out_features""": ["""stage1""", """stage2""", """stage3"""], """embedding_dynamic_padding""": True, """hidden_sizes""": [9_6, 1_9_2, 3_8_4, 7_6_8], """num_groups""": 2, } return DPTConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , backbone_out_indices=self.backbone_out_indices , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=__SCREAMING_SNAKE_CASE , initializer_range=self.initializer_range , is_hybrid=self.is_hybrid , backbone_config=__SCREAMING_SNAKE_CASE , backbone_featmap_shape=self.backbone_featmap_shape , ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): snake_case__ : Dict = DPTModel(config=__SCREAMING_SNAKE_CASE ) model.to(__SCREAMING_SNAKE_CASE ) model.eval() snake_case__ : Union[str, Any] = model(__SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): snake_case__ : Optional[Any] = self.num_labels snake_case__ : str = DPTForDepthEstimation(__SCREAMING_SNAKE_CASE ) model.to(__SCREAMING_SNAKE_CASE ) model.eval() snake_case__ : Optional[Any] = model(__SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.predicted_depth.shape , (self.batch_size, self.image_size, self.image_size) ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): snake_case__ : Any = self.num_labels snake_case__ : Dict = DPTForSemanticSegmentation(__SCREAMING_SNAKE_CASE ) model.to(__SCREAMING_SNAKE_CASE ) model.eval() snake_case__ : str = model(__SCREAMING_SNAKE_CASE , labels=__SCREAMING_SNAKE_CASE ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size, self.image_size) ) def __UpperCamelCase ( self ): snake_case__ : Union[str, Any] = self.prepare_config_and_inputs() snake_case__ , snake_case__ , snake_case__ : Any = config_and_inputs snake_case__ : Optional[int] = {"""pixel_values""": pixel_values} return config, inputs_dict @require_torch class __snake_case ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowerCamelCase__ = (DPTModel, DPTForDepthEstimation, DPTForSemanticSegmentation) if is_torch_available() else () lowerCamelCase__ = ( { '''depth-estimation''': DPTForDepthEstimation, '''feature-extraction''': DPTModel, '''image-segmentation''': DPTForSemanticSegmentation, } if is_torch_available() else {} ) lowerCamelCase__ = False lowerCamelCase__ = False lowerCamelCase__ = False def __UpperCamelCase ( self ): snake_case__ : List[Any] = DPTModelTester(self ) snake_case__ : Any = ConfigTester(self , config_class=__SCREAMING_SNAKE_CASE , has_text_modality=__SCREAMING_SNAKE_CASE , hidden_size=3_7 ) def __UpperCamelCase ( self ): self.config_tester.run_common_tests() @unittest.skip(reason="""DPT does not use inputs_embeds""" ) def __UpperCamelCase ( self ): pass def __UpperCamelCase ( self ): snake_case__ , snake_case__ : Tuple = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: snake_case__ : Tuple = model_class(__SCREAMING_SNAKE_CASE ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) snake_case__ : str = model.get_output_embeddings() self.assertTrue(x is None or isinstance(__SCREAMING_SNAKE_CASE , nn.Linear ) ) def __UpperCamelCase ( self ): snake_case__ , snake_case__ : Dict = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: snake_case__ : str = model_class(__SCREAMING_SNAKE_CASE ) snake_case__ : str = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic snake_case__ : List[str] = [*signature.parameters.keys()] snake_case__ : str = ["""pixel_values"""] self.assertListEqual(arg_names[:1] , __SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self ): snake_case__ : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self ): snake_case__ : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_depth_estimation(*__SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self ): snake_case__ : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_semantic_segmentation(*__SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self ): for model_class in self.all_model_classes: if model_class.__name__ == "DPTForDepthEstimation": continue snake_case__ , snake_case__ : str = self.model_tester.prepare_config_and_inputs_for_common() snake_case__ : int = True if model_class in get_values(__SCREAMING_SNAKE_CASE ): continue snake_case__ : Any = model_class(__SCREAMING_SNAKE_CASE ) model.to(__SCREAMING_SNAKE_CASE ) model.train() snake_case__ : Optional[Any] = self._prepare_for_class(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , return_labels=__SCREAMING_SNAKE_CASE ) snake_case__ : Optional[int] = model(**__SCREAMING_SNAKE_CASE ).loss loss.backward() def __UpperCamelCase ( self ): for model_class in self.all_model_classes: if model_class.__name__ == "DPTForDepthEstimation": continue snake_case__ , snake_case__ : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() snake_case__ : Union[str, Any] = False snake_case__ : str = True if model_class in get_values(__SCREAMING_SNAKE_CASE ) or not model_class.supports_gradient_checkpointing: continue snake_case__ : Any = model_class(__SCREAMING_SNAKE_CASE ) model.to(__SCREAMING_SNAKE_CASE ) model.gradient_checkpointing_enable() model.train() snake_case__ : List[str] = self._prepare_for_class(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , return_labels=__SCREAMING_SNAKE_CASE ) snake_case__ : Any = model(**__SCREAMING_SNAKE_CASE ).loss loss.backward() def __UpperCamelCase ( self ): snake_case__ , snake_case__ : str = self.model_tester.prepare_config_and_inputs_for_common() snake_case__ : str = _config_zero_init(__SCREAMING_SNAKE_CASE ) for model_class in self.all_model_classes: snake_case__ : Any = model_class(config=__SCREAMING_SNAKE_CASE ) # Skip the check for the backbone snake_case__ : str = [] for name, module in model.named_modules(): if module.__class__.__name__ == "DPTViTHybridEmbeddings": snake_case__ : Optional[int] = [f"{name}.{key}" for key in module.state_dict().keys()] break for name, param in model.named_parameters(): if param.requires_grad: if name in backbone_params: continue self.assertIn( ((param.data.mean() * 1e9).round() / 1e9).item() , [0.0, 1.0] , msg=f"Parameter {name} of model {model_class} seems not properly initialized" , ) @unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests.""" ) def __UpperCamelCase ( self ): pass @slow def __UpperCamelCase ( self ): for model_name in DPT_PRETRAINED_MODEL_ARCHIVE_LIST[1:]: snake_case__ : List[str] = DPTModel.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsNotNone(__SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self ): # We do this test only for DPTForDepthEstimation since it is the only model that uses readout_type snake_case__ , snake_case__ : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() snake_case__ : Dict = """add""" with self.assertRaises(__SCREAMING_SNAKE_CASE ): snake_case__ : List[str] = DPTForDepthEstimation(__SCREAMING_SNAKE_CASE ) def UpperCamelCase__ ( ) -> Dict: '''simple docstring''' snake_case__ : List[Any] = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) return image @require_torch @require_vision @slow class __snake_case ( unittest.TestCase ): '''simple docstring''' def __UpperCamelCase ( self ): snake_case__ : Dict = DPTImageProcessor.from_pretrained("""Intel/dpt-hybrid-midas""" ) snake_case__ : Union[str, Any] = DPTForDepthEstimation.from_pretrained("""Intel/dpt-hybrid-midas""" ).to(__SCREAMING_SNAKE_CASE ) snake_case__ : Optional[Any] = prepare_img() snake_case__ : Optional[int] = image_processor(images=__SCREAMING_SNAKE_CASE , return_tensors="""pt""" ).to(__SCREAMING_SNAKE_CASE ) # forward pass with torch.no_grad(): snake_case__ : Dict = model(**__SCREAMING_SNAKE_CASE ) snake_case__ : Tuple = outputs.predicted_depth # verify the predicted depth snake_case__ : Any = torch.Size((1, 3_8_4, 3_8_4) ) self.assertEqual(predicted_depth.shape , __SCREAMING_SNAKE_CASE ) snake_case__ : List[Any] = torch.tensor( [[[5.6437, 5.6146, 5.6511], [5.4371, 5.5649, 5.5958], [5.5215, 5.5184, 5.5293]]] ).to(__SCREAMING_SNAKE_CASE ) self.assertTrue(torch.allclose(outputs.predicted_depth[:3, :3, :3] / 1_0_0 , __SCREAMING_SNAKE_CASE , atol=1e-4 ) )
38
0
def __UpperCamelCase (lowerCAmelCase : int = 10 ) -> str: if not isinstance(lowerCAmelCase, lowerCAmelCase ) or n < 0: raise ValueError('Invalid input' ) A = 10**n A = 28_433 * (pow(2, 7_830_457, lowerCAmelCase )) + 1 return str(number % modulus ) if __name__ == "__main__": from doctest import testmod testmod() print(F'''{solution(10) = }''')
699
'''simple docstring''' # Copyright 2021 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import json import os from ...utils.constants import SAGEMAKER_PARALLEL_EC2_INSTANCES, TORCH_DYNAMO_MODES from ...utils.dataclasses import ComputeEnvironment, SageMakerDistributedType from ...utils.imports import is_botoa_available from .config_args import SageMakerConfig from .config_utils import ( DYNAMO_BACKENDS, _ask_field, _ask_options, _convert_dynamo_backend, _convert_mixed_precision, _convert_sagemaker_distributed_mode, _convert_yes_no_to_bool, ) if is_botoa_available(): import botoa # noqa: F401 def UpperCamelCase__ ( __magic_name__ : Optional[Any] ) -> Dict: '''simple docstring''' snake_case__ : int = botoa.client("""iam""" ) snake_case__ : Union[str, Any] = { """Version""": """2012-10-17""", """Statement""": [ {"""Effect""": """Allow""", """Principal""": {"""Service""": """sagemaker.amazonaws.com"""}, """Action""": """sts:AssumeRole"""} ], } try: # create the role, associated with the chosen trust policy iam_client.create_role( RoleName=__magic_name__ , AssumeRolePolicyDocument=json.dumps(__magic_name__ , indent=2 ) ) snake_case__ : Dict = { """Version""": """2012-10-17""", """Statement""": [ { """Effect""": """Allow""", """Action""": [ """sagemaker:*""", """ecr:GetDownloadUrlForLayer""", """ecr:BatchGetImage""", """ecr:BatchCheckLayerAvailability""", """ecr:GetAuthorizationToken""", """cloudwatch:PutMetricData""", """cloudwatch:GetMetricData""", """cloudwatch:GetMetricStatistics""", """cloudwatch:ListMetrics""", """logs:CreateLogGroup""", """logs:CreateLogStream""", """logs:DescribeLogStreams""", """logs:PutLogEvents""", """logs:GetLogEvents""", """s3:CreateBucket""", """s3:ListBucket""", """s3:GetBucketLocation""", """s3:GetObject""", """s3:PutObject""", ], """Resource""": """*""", } ], } # attach policy to role iam_client.put_role_policy( RoleName=__magic_name__ , PolicyName=f"{role_name}_policy_permission" , PolicyDocument=json.dumps(__magic_name__ , indent=2 ) , ) except iam_client.exceptions.EntityAlreadyExistsException: print(f"role {role_name} already exists. Using existing one" ) def UpperCamelCase__ ( __magic_name__ : Any ) -> Tuple: '''simple docstring''' snake_case__ : List[str] = botoa.client("""iam""" ) return iam_client.get_role(RoleName=__magic_name__ )["Role"]["Arn"] def UpperCamelCase__ ( ) -> Tuple: '''simple docstring''' snake_case__ : Union[str, Any] = _ask_options( """How do you want to authorize?""" , ["""AWS Profile""", """Credentials (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) """] , __magic_name__ , ) snake_case__ : List[Any] = None if credentials_configuration == 0: snake_case__ : Dict = _ask_field("""Enter your AWS Profile name: [default] """ , default="""default""" ) snake_case__ : List[str] = aws_profile else: print( """Note you will need to provide AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY when you launch you training script with,""" """`accelerate launch --aws_access_key_id XXX --aws_secret_access_key YYY`""" ) snake_case__ : List[str] = _ask_field("""AWS Access Key ID: """ ) snake_case__ : int = aws_access_key_id snake_case__ : Optional[Any] = _ask_field("""AWS Secret Access Key: """ ) snake_case__ : List[str] = aws_secret_access_key snake_case__ : Tuple = _ask_field("""Enter your AWS Region: [us-east-1]""" , default="""us-east-1""" ) snake_case__ : Optional[int] = aws_region snake_case__ : int = _ask_options( """Do you already have an IAM Role for executing Amazon SageMaker Training Jobs?""" , ["""Provide IAM Role name""", """Create new IAM role using credentials"""] , __magic_name__ , ) if role_management == 0: snake_case__ : Optional[Any] = _ask_field("""Enter your IAM role name: """ ) else: snake_case__ : Optional[int] = """accelerate_sagemaker_execution_role""" print(f"Accelerate will create an iam role \"{iam_role_name}\" using the provided credentials" ) _create_iam_role_for_sagemaker(__magic_name__ ) snake_case__ : Dict = _ask_field( """Do you want to use custom Docker image? [yes/NO]: """ , _convert_yes_no_to_bool , default=__magic_name__ , error_message="""Please enter yes or no.""" , ) snake_case__ : Any = None if is_custom_docker_image: snake_case__ : str = _ask_field("""Enter your Docker image: """ , lambda __magic_name__ : str(__magic_name__ ).lower() ) snake_case__ : Tuple = _ask_field( """Do you want to provide SageMaker input channels with data locations? [yes/NO]: """ , _convert_yes_no_to_bool , default=__magic_name__ , error_message="""Please enter yes or no.""" , ) snake_case__ : List[Any] = None if is_sagemaker_inputs_enabled: snake_case__ : str = _ask_field( """Enter the path to the SageMaker inputs TSV file with columns (channel_name, data_location): """ , lambda __magic_name__ : str(__magic_name__ ).lower() , ) snake_case__ : Optional[int] = _ask_field( """Do you want to enable SageMaker metrics? [yes/NO]: """ , _convert_yes_no_to_bool , default=__magic_name__ , error_message="""Please enter yes or no.""" , ) snake_case__ : Optional[Any] = None if is_sagemaker_metrics_enabled: snake_case__ : List[Any] = _ask_field( """Enter the path to the SageMaker metrics TSV file with columns (metric_name, metric_regex): """ , lambda __magic_name__ : str(__magic_name__ ).lower() , ) snake_case__ : Tuple = _ask_options( """What is the distributed mode?""" , ["""No distributed training""", """Data parallelism"""] , _convert_sagemaker_distributed_mode , ) snake_case__ : Any = {} snake_case__ : List[Any] = _ask_field( """Do you wish to optimize your script with torch dynamo?[yes/NO]:""" , _convert_yes_no_to_bool , default=__magic_name__ , error_message="""Please enter yes or no.""" , ) if use_dynamo: snake_case__ : str = """dynamo_""" snake_case__ : Tuple = _ask_options( """Which dynamo backend would you like to use?""" , [x.lower() for x in DYNAMO_BACKENDS] , _convert_dynamo_backend , default=2 , ) snake_case__ : List[str] = _ask_field( """Do you want to customize the defaults sent to torch.compile? [yes/NO]: """ , _convert_yes_no_to_bool , default=__magic_name__ , error_message="""Please enter yes or no.""" , ) if use_custom_options: snake_case__ : str = _ask_options( """Which mode do you want to use?""" , __magic_name__ , lambda __magic_name__ : TORCH_DYNAMO_MODES[int(__magic_name__ )] , default="""default""" , ) snake_case__ : Union[str, Any] = _ask_field( """Do you want the fullgraph mode or it is ok to break model into several subgraphs? [yes/NO]: """ , _convert_yes_no_to_bool , default=__magic_name__ , error_message="""Please enter yes or no.""" , ) snake_case__ : str = _ask_field( """Do you want to enable dynamic shape tracing? [yes/NO]: """ , _convert_yes_no_to_bool , default=__magic_name__ , error_message="""Please enter yes or no.""" , ) snake_case__ : Dict = """Which EC2 instance type you want to use for your training?""" if distributed_type != SageMakerDistributedType.NO: snake_case__ : List[str] = _ask_options( __magic_name__ , __magic_name__ , lambda __magic_name__ : SAGEMAKER_PARALLEL_EC2_INSTANCES[int(__magic_name__ )] ) else: eca_instance_query += "? [ml.p3.2xlarge]:" snake_case__ : Optional[int] = _ask_field(__magic_name__ , lambda __magic_name__ : str(__magic_name__ ).lower() , default="""ml.p3.2xlarge""" ) snake_case__ : Dict = 1 if distributed_type in (SageMakerDistributedType.DATA_PARALLEL, SageMakerDistributedType.MODEL_PARALLEL): snake_case__ : Optional[Any] = _ask_field( """How many machines do you want use? [1]: """ , __magic_name__ , default=1 , ) snake_case__ : Union[str, Any] = _ask_options( """Do you wish to use FP16 or BF16 (mixed precision)?""" , ["""no""", """fp16""", """bf16""", """fp8"""] , _convert_mixed_precision , ) if use_dynamo and mixed_precision == "no": print( """Torch dynamo used without mixed precision requires TF32 to be efficient. Accelerate will enable it by default when launching your scripts.""" ) return SageMakerConfig( image_uri=__magic_name__ , compute_environment=ComputeEnvironment.AMAZON_SAGEMAKER , distributed_type=__magic_name__ , use_cpu=__magic_name__ , dynamo_config=__magic_name__ , eca_instance_type=__magic_name__ , profile=__magic_name__ , region=__magic_name__ , iam_role_name=__magic_name__ , mixed_precision=__magic_name__ , num_machines=__magic_name__ , sagemaker_inputs_file=__magic_name__ , sagemaker_metrics_file=__magic_name__ , )
38
0
from typing import Callable, Optional from .. import Features from ..packaged_modules.generator.generator import Generator from .abc import AbstractDatasetInputStream class _A ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : Optional[Any] , lowerCamelCase : Optional[int] , lowerCamelCase : str = None , lowerCamelCase : int = None , lowerCamelCase : Dict = False , lowerCamelCase : Dict = False , lowerCamelCase : str = None , lowerCamelCase : str = None , **lowerCamelCase : Union[str, Any] , ): '''simple docstring''' super().__init__( features=__SCREAMING_SNAKE_CASE , cache_dir=__SCREAMING_SNAKE_CASE , keep_in_memory=__SCREAMING_SNAKE_CASE , streaming=__SCREAMING_SNAKE_CASE , num_proc=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) __lowercase = Generator( cache_dir=__SCREAMING_SNAKE_CASE , features=__SCREAMING_SNAKE_CASE , generator=__SCREAMING_SNAKE_CASE , gen_kwargs=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) def _snake_case ( self : int ): '''simple docstring''' if self.streaming: __lowercase = self.builder.as_streaming_dataset(split="train" ) # Build regular (map-style) dataset else: __lowercase = None __lowercase = None __lowercase = None __lowercase = None self.builder.download_and_prepare( download_config=__SCREAMING_SNAKE_CASE , download_mode=__SCREAMING_SNAKE_CASE , verification_mode=__SCREAMING_SNAKE_CASE , base_path=__SCREAMING_SNAKE_CASE , num_proc=self.num_proc , ) __lowercase = self.builder.as_dataset( split="train" , verification_mode=__SCREAMING_SNAKE_CASE , in_memory=self.keep_in_memory ) return dataset
402
'''simple docstring''' from itertools import zip_longest import requests from bsa import BeautifulSoup from pandas import DataFrame def UpperCamelCase__ ( __magic_name__ : str = "laptop" ) -> DataFrame: '''simple docstring''' snake_case__ : Union[str, Any] = f"https://www.amazon.in/laptop/s?k={product}" snake_case__ : List[str] = { """User-Agent""": """Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)Chrome/44.0.2403.157 Safari/537.36""", """Accept-Language""": """en-US, en;q=0.5""", } snake_case__ : int = BeautifulSoup(requests.get(__magic_name__ , headers=__magic_name__ ).text ) # Initialize a Pandas dataframe with the column titles snake_case__ : Optional[Any] = DataFrame( columns=[ """Product Title""", """Product Link""", """Current Price of the product""", """Product Rating""", """MRP of the product""", """Discount""", ] ) # Loop through each entry and store them in the dataframe for item, _ in zip_longest( soup.find_all( """div""" , attrs={"""class""": """s-result-item""", """data-component-type""": """s-search-result"""} , ) , soup.find_all("""div""" , attrs={"""class""": """a-row a-size-base a-color-base"""} ) , ): try: snake_case__ : Optional[int] = item.ha.text snake_case__ : Any = """https://www.amazon.in/""" + item.ha.a["""href"""] snake_case__ : List[str] = item.find("""span""" , attrs={"""class""": """a-offscreen"""} ).text try: snake_case__ : Dict = item.find("""span""" , attrs={"""class""": """a-icon-alt"""} ).text except AttributeError: snake_case__ : Optional[int] = """Not available""" try: snake_case__ : Tuple = ( """₹""" + item.find( """span""" , attrs={"""class""": """a-price a-text-price"""} ).text.split("""₹""" )[1] ) except AttributeError: snake_case__ : Optional[Any] = """""" try: snake_case__ : str = float( ( ( float(product_mrp.strip("""₹""" ).replace(""",""" , """""" ) ) - float(product_price.strip("""₹""" ).replace(""",""" , """""" ) ) ) / float(product_mrp.strip("""₹""" ).replace(""",""" , """""" ) ) ) * 1_00 ) except ValueError: snake_case__ : List[Any] = float("""nan""" ) except AttributeError: pass snake_case__ : str = [ product_title, product_link, product_price, product_rating, product_mrp, discount, ] snake_case__ : List[Any] = """ """ snake_case__ : Union[str, Any] = """ """ data_frame.index += 1 return data_frame if __name__ == "__main__": A_ : int = "headphones" get_amazon_product_data(product).to_csv(F'Amazon Product Data for {product}.csv')
38
0
from ..utils import DummyObject, requires_backends class snake_case ( metaclass=__SCREAMING_SNAKE_CASE ): '''simple docstring''' UpperCAmelCase : Optional[Any] = ["""note_seq"""] def __init__( self : List[str] , *lowerCAmelCase_ : str , **lowerCAmelCase_ : Dict ) -> Any: """simple docstring""" requires_backends(self , ['''note_seq'''] ) @classmethod def _lowercase ( cls : Dict , *lowerCAmelCase_ : str , **lowerCAmelCase_ : List[str] ) -> str: """simple docstring""" requires_backends(cls , ['''note_seq'''] ) @classmethod def _lowercase ( cls : Any , *lowerCAmelCase_ : Any , **lowerCAmelCase_ : Dict ) -> Optional[Any]: """simple docstring""" requires_backends(cls , ['''note_seq'''] )
393
'''simple docstring''' import itertools import json import os import unittest from transformers import AddedToken, LongformerTokenizer, LongformerTokenizerFast from transformers.models.longformer.tokenization_longformer import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class __snake_case ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowerCamelCase__ = LongformerTokenizer lowerCamelCase__ = True lowerCamelCase__ = LongformerTokenizerFast lowerCamelCase__ = True def __UpperCamelCase ( self ): super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt snake_case__ : Union[str, Any] = [ """l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """\u0120""", """\u0120l""", """\u0120n""", """\u0120lo""", """\u0120low""", """er""", """\u0120lowest""", """\u0120newer""", """\u0120wider""", """<unk>""", ] snake_case__ : Optional[int] = dict(zip(__SCREAMING_SNAKE_CASE , range(len(__SCREAMING_SNAKE_CASE ) ) ) ) snake_case__ : int = ["""#version: 0.2""", """\u0120 l""", """\u0120l o""", """\u0120lo w""", """e r""", """"""] snake_case__ : Any = {"""unk_token""": """<unk>"""} snake_case__ : List[Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""vocab_file"""] ) snake_case__ : 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(__SCREAMING_SNAKE_CASE ) + """\n""" ) with open(self.merges_file , """w""" , encoding="""utf-8""" ) as fp: fp.write("""\n""".join(__SCREAMING_SNAKE_CASE ) ) def __UpperCamelCase ( self , **__SCREAMING_SNAKE_CASE ): kwargs.update(self.special_tokens_map ) return self.tokenizer_class.from_pretrained(self.tmpdirname , **__SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self , **__SCREAMING_SNAKE_CASE ): kwargs.update(self.special_tokens_map ) return self.rust_tokenizer_class.from_pretrained(self.tmpdirname , **__SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE ): snake_case__ : str = """lower newer""" snake_case__ : Dict = """lower newer""" return input_text, output_text def __UpperCamelCase ( self ): snake_case__ : int = self.tokenizer_class(self.vocab_file , self.merges_file , **self.special_tokens_map ) snake_case__ : Tuple = """lower newer""" snake_case__ : Optional[Any] = ["""l""", """o""", """w""", """er""", """\u0120""", """n""", """e""", """w""", """er"""] snake_case__ : Tuple = tokenizer.tokenize(__SCREAMING_SNAKE_CASE ) # , add_prefix_space=True) self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) snake_case__ : Tuple = tokens + [tokenizer.unk_token] snake_case__ : List[Any] = [0, 1, 2, 1_5, 1_0, 9, 3, 2, 1_5, 1_9] self.assertListEqual(tokenizer.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self ): snake_case__ : Tuple = self.get_tokenizer() self.assertListEqual(tokenizer.encode("""Hello world!""" , add_special_tokens=__SCREAMING_SNAKE_CASE ) , [0, 3_1_4_1_4, 2_3_2, 3_2_8, 2] ) self.assertListEqual( tokenizer.encode("""Hello world! cécé herlolip 418""" , add_special_tokens=__SCREAMING_SNAKE_CASE ) , [0, 3_1_4_1_4, 2_3_2, 3_2_8, 7_4_0, 1_1_4_0, 1_2_6_9_5, 6_9, 4_6_0_7_8, 1_5_8_8, 2] , ) @slow def __UpperCamelCase ( self ): snake_case__ : List[Any] = self.tokenizer_class.from_pretrained("""allenai/longformer-base-4096""" ) snake_case__ : int = tokenizer.encode("""sequence builders""" , add_special_tokens=__SCREAMING_SNAKE_CASE ) snake_case__ : Dict = tokenizer.encode("""multi-sequence build""" , add_special_tokens=__SCREAMING_SNAKE_CASE ) snake_case__ : Optional[Any] = tokenizer.encode( """sequence builders""" , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) snake_case__ : Dict = tokenizer.encode( """sequence builders""" , """multi-sequence build""" , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) snake_case__ : Dict = tokenizer.build_inputs_with_special_tokens(__SCREAMING_SNAKE_CASE ) snake_case__ : Optional[int] = tokenizer.build_inputs_with_special_tokens(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) assert encoded_sentence == encoded_text_from_decode assert encoded_pair == encoded_pair_from_decode def __UpperCamelCase ( self ): snake_case__ : Optional[int] = self.get_tokenizer() snake_case__ : int = """Encode this sequence.""" snake_case__ : Union[str, Any] = tokenizer.byte_encoder[""" """.encode("""utf-8""" )[0]] # Testing encoder arguments snake_case__ : Optional[int] = tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) snake_case__ : Tuple = tokenizer.convert_ids_to_tokens(encoded[0] )[0] self.assertNotEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) snake_case__ : Optional[Any] = tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) snake_case__ : List[str] = tokenizer.convert_ids_to_tokens(encoded[0] )[0] self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) tokenizer.add_special_tokens({"""bos_token""": """<s>"""} ) snake_case__ : List[str] = tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) snake_case__ : str = tokenizer.convert_ids_to_tokens(encoded[1] )[0] self.assertNotEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # Testing spaces after special tokens snake_case__ : List[str] = """<mask>""" tokenizer.add_special_tokens( {"""mask_token""": AddedToken(__SCREAMING_SNAKE_CASE , lstrip=__SCREAMING_SNAKE_CASE , rstrip=__SCREAMING_SNAKE_CASE )} ) # mask token has a left space snake_case__ : Dict = tokenizer.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) snake_case__ : str = """Encode <mask> sequence""" snake_case__ : Tuple = """Encode <mask>sequence""" snake_case__ : Union[str, Any] = tokenizer.encode(__SCREAMING_SNAKE_CASE ) snake_case__ : List[str] = encoded.index(__SCREAMING_SNAKE_CASE ) snake_case__ : Optional[int] = tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1] )[0] self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) snake_case__ : Tuple = tokenizer.encode(__SCREAMING_SNAKE_CASE ) snake_case__ : str = encoded.index(__SCREAMING_SNAKE_CASE ) snake_case__ : List[Any] = tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1] )[0] self.assertNotEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self ): pass def __UpperCamelCase ( self ): for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f"{tokenizer.__class__.__name__} ({pretrained_name})" ): snake_case__ : List[Any] = self.rust_tokenizer_class.from_pretrained(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) snake_case__ : Any = self.tokenizer_class.from_pretrained(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) snake_case__ : List[str] = """A, <mask> AllenNLP sentence.""" snake_case__ : str = tokenizer_r.encode_plus(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , return_token_type_ids=__SCREAMING_SNAKE_CASE ) snake_case__ : Tuple = tokenizer_p.encode_plus(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , return_token_type_ids=__SCREAMING_SNAKE_CASE ) # token_type_ids should put 0 everywhere self.assertEqual(sum(tokens_r["""token_type_ids"""] ) , sum(tokens_p["""token_type_ids"""] ) ) # attention_mask should put 1 everywhere, so sum over length should be 1 self.assertEqual( sum(tokens_r["""attention_mask"""] ) / len(tokens_r["""attention_mask"""] ) , sum(tokens_p["""attention_mask"""] ) / len(tokens_p["""attention_mask"""] ) , ) snake_case__ : Union[str, Any] = tokenizer_r.convert_ids_to_tokens(tokens_r["""input_ids"""] ) snake_case__ : Dict = tokenizer_p.convert_ids_to_tokens(tokens_p["""input_ids"""] ) # Rust correctly handles the space before the mask while python doesnt self.assertSequenceEqual(tokens_p["""input_ids"""] , [0, 2_5_0, 6, 5_0_2_6_4, 3_8_2_3, 4_8_7, 2_1_9_9_2, 3_6_4_5, 4, 2] ) self.assertSequenceEqual(tokens_r["""input_ids"""] , [0, 2_5_0, 6, 5_0_2_6_4, 3_8_2_3, 4_8_7, 2_1_9_9_2, 3_6_4_5, 4, 2] ) self.assertSequenceEqual( __SCREAMING_SNAKE_CASE , ["""<s>""", """A""", """,""", """<mask>""", """ĠAllen""", """N""", """LP""", """Ġsentence""", """.""", """</s>"""] ) self.assertSequenceEqual( __SCREAMING_SNAKE_CASE , ["""<s>""", """A""", """,""", """<mask>""", """ĠAllen""", """N""", """LP""", """Ġsentence""", """.""", """</s>"""] ) def __UpperCamelCase ( self ): for trim_offsets, add_prefix_space in itertools.product([True, False] , repeat=2 ): snake_case__ : Any = self.rust_tokenizer_class.from_pretrained( self.tmpdirname , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) snake_case__ : Dict = json.loads(tokenizer_r.backend_tokenizer.pre_tokenizer.__getstate__() ) snake_case__ : List[str] = json.loads(tokenizer_r.backend_tokenizer.post_processor.__getstate__() ) self.assertEqual(pre_tokenizer_state["""add_prefix_space"""] , __SCREAMING_SNAKE_CASE ) self.assertEqual(post_processor_state["""add_prefix_space"""] , __SCREAMING_SNAKE_CASE ) self.assertEqual(post_processor_state["""trim_offsets"""] , __SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self ): # Test which aims to verify that the offsets are well adapted to the argument `add_prefix_space` and # `trim_offsets` for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f"{tokenizer.__class__.__name__} ({pretrained_name})" ): snake_case__ : Union[str, Any] = """hello""" # `hello` is a token in the vocabulary of `pretrained_name` snake_case__ : Any = f"{text_of_1_token} {text_of_1_token}" snake_case__ : Union[str, Any] = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) snake_case__ : Union[str, Any] = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (len(__SCREAMING_SNAKE_CASE ) + 1, len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) snake_case__ : List[Any] = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) snake_case__ : str = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (len(__SCREAMING_SNAKE_CASE ) + 1, len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) snake_case__ : Optional[Any] = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) snake_case__ : str = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (len(__SCREAMING_SNAKE_CASE ), len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) snake_case__ : Tuple = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) snake_case__ : List[Any] = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (len(__SCREAMING_SNAKE_CASE ), len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) snake_case__ : Optional[Any] = f" {text}" # tokenizer_r = self.rust_tokenizer_class.from_pretrained( # pretrained_name, use_fast=True, add_prefix_space=True, trim_offsets=True # ) # encoding = tokenizer_r(text, return_offsets_mapping=True, add_special_tokens=False) # self.assertEqual(encoding.offset_mapping[0], (1, 1 + len(text_of_1_token))) # self.assertEqual( # encoding.offset_mapping[1], # (1 + len(text_of_1_token) + 1, 1 + len(text_of_1_token) + 1 + len(text_of_1_token)), # ) snake_case__ : Dict = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) snake_case__ : Optional[Any] = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (1, 1 + len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(__SCREAMING_SNAKE_CASE ) + 1, 1 + len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) snake_case__ : Any = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) snake_case__ : Any = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, 1 + len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(__SCREAMING_SNAKE_CASE ), 1 + len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) snake_case__ : List[Any] = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) snake_case__ : List[Any] = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, 1 + len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(__SCREAMING_SNAKE_CASE ), 1 + len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , )
38
0
"""simple docstring""" import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_rembert import RemBertTokenizer else: SCREAMING_SNAKE_CASE__ : Union[str, Any] =None SCREAMING_SNAKE_CASE__ : Dict =logging.get_logger(__name__) SCREAMING_SNAKE_CASE__ : str ={"vocab_file": "sentencepiece.model", "tokenizer_file": "tokenizer.json"} SCREAMING_SNAKE_CASE__ : str ={ "vocab_file": { "google/rembert": "https://huggingface.co/google/rembert/resolve/main/sentencepiece.model", }, "tokenizer_file": { "google/rembert": "https://huggingface.co/google/rembert/resolve/main/tokenizer.json", }, } SCREAMING_SNAKE_CASE__ : Tuple ={ "google/rembert": 256, } SCREAMING_SNAKE_CASE__ : List[Any] ="▁" class _UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): """simple docstring""" __snake_case = VOCAB_FILES_NAMES __snake_case = PRETRAINED_VOCAB_FILES_MAP __snake_case = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __snake_case = RemBertTokenizer def __init__( self , _lowercase=None , _lowercase=None , _lowercase=True , _lowercase=True , _lowercase=False , _lowercase="[CLS]" , _lowercase="[SEP]" , _lowercase="<unk>" , _lowercase="[SEP]" , _lowercase="<pad>" , _lowercase="[CLS]" , _lowercase="[MASK]" , **_lowercase , ) -> Optional[Any]: # Mask token behave like a normal word, i.e. include the space before it _lowerCamelCase : Union[str, Any] = AddedToken(__SCREAMING_SNAKE_CASE , lstrip=__SCREAMING_SNAKE_CASE , rstrip=__SCREAMING_SNAKE_CASE ) if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else mask_token super().__init__( __SCREAMING_SNAKE_CASE , tokenizer_file=__SCREAMING_SNAKE_CASE , do_lower_case=__SCREAMING_SNAKE_CASE , remove_space=__SCREAMING_SNAKE_CASE , keep_accents=__SCREAMING_SNAKE_CASE , bos_token=__SCREAMING_SNAKE_CASE , eos_token=__SCREAMING_SNAKE_CASE , unk_token=__SCREAMING_SNAKE_CASE , sep_token=__SCREAMING_SNAKE_CASE , pad_token=__SCREAMING_SNAKE_CASE , cls_token=__SCREAMING_SNAKE_CASE , mask_token=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) _lowerCamelCase : Optional[int] = do_lower_case _lowerCamelCase : Tuple = remove_space _lowerCamelCase : List[Any] = keep_accents _lowerCamelCase : List[Any] = vocab_file _lowerCamelCase : str = False if not self.vocab_file else True def a__ ( self , _lowercase , _lowercase = None ) -> Optional[int]: _lowerCamelCase : Optional[int] = [self.sep_token_id] _lowerCamelCase : Any = [self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def a__ ( self , _lowercase , _lowercase = None , _lowercase = False ) -> Dict: if already_has_special_tokens: if token_ids_a is not None: raise ValueError( '''You should not supply a second sequence if the provided sequence of ''' '''ids is already formatted with special tokens for the model.''' ) return [1 if x in [self.sep_token_id, self.cls_token_id] else 0 for x in token_ids_a] if token_ids_a is not None: return [1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1] return [1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1] def a__ ( self , _lowercase , _lowercase = None ) -> Optional[int]: _lowerCamelCase : List[Any] = [self.sep_token_id] _lowerCamelCase : Optional[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 ) * [0] + len(token_ids_a + sep ) * [1] def a__ ( self , _lowercase , _lowercase = None ) -> List[str]: if not os.path.isdir(__SCREAMING_SNAKE_CASE ): logger.error('''Vocabulary path ({}) should be a directory'''.format(__SCREAMING_SNAKE_CASE ) ) return _lowerCamelCase : int = os.path.join( __SCREAMING_SNAKE_CASE , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(__SCREAMING_SNAKE_CASE ): copyfile(self.vocab_file , __SCREAMING_SNAKE_CASE ) return (out_vocab_file,)
434
'''simple docstring''' from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices A_ : int = logging.get_logger(__name__) A_ : Any = { "microsoft/resnet-50": "https://huggingface.co/microsoft/resnet-50/blob/main/config.json", } class __snake_case ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): '''simple docstring''' lowerCamelCase__ = '''resnet''' lowerCamelCase__ = ['''basic''', '''bottleneck'''] def __init__( self , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=6_4 , __SCREAMING_SNAKE_CASE=[2_5_6, 5_1_2, 1_0_2_4, 2_0_4_8] , __SCREAMING_SNAKE_CASE=[3, 4, 6, 3] , __SCREAMING_SNAKE_CASE="bottleneck" , __SCREAMING_SNAKE_CASE="relu" , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE , ): super().__init__(**__SCREAMING_SNAKE_CASE ) if layer_type not in self.layer_types: raise ValueError(f"layer_type={layer_type} is not one of {','.join(self.layer_types )}" ) snake_case__ : List[Any] = num_channels snake_case__ : str = embedding_size snake_case__ : List[Any] = hidden_sizes snake_case__ : Dict = depths snake_case__ : List[Any] = layer_type snake_case__ : int = hidden_act snake_case__ : Union[str, Any] = downsample_in_first_stage snake_case__ : Dict = ["""stem"""] + [f"stage{idx}" for idx in range(1 , len(__SCREAMING_SNAKE_CASE ) + 1 )] snake_case__ , snake_case__ : Any = get_aligned_output_features_output_indices( out_features=__SCREAMING_SNAKE_CASE , out_indices=__SCREAMING_SNAKE_CASE , stage_names=self.stage_names ) class __snake_case ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' lowerCamelCase__ = version.parse('''1.11''' ) @property def __UpperCamelCase ( self ): return OrderedDict( [ ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}), ] ) @property def __UpperCamelCase ( self ): return 1e-3
38
0
from math import factorial def __lowercase ( __lowerCAmelCase : int = 1_0_0 ): return sum(int(__lowerCAmelCase ) for x in str(factorial(__lowerCAmelCase ) ) ) if __name__ == "__main__": print(solution(int(input('''Enter the Number: ''').strip())))
335
'''simple docstring''' # limitations under the License. # NOTE: This file is deprecated and will be removed in a future version. # It only exists so that temporarely `from diffusers.pipelines import DiffusionPipeline` works from .pipelines import DiffusionPipeline, ImagePipelineOutput # noqa: F401 from .utils import deprecate deprecate( "pipelines_utils", "0.22.0", "Importing `DiffusionPipeline` or `ImagePipelineOutput` from diffusers.pipeline_utils is deprecated. Please import from diffusers.pipelines.pipeline_utils instead.", standard_warn=False, stacklevel=3, )
38
0
import logging import torch from accelerate import Accelerator from arguments import EvaluationArguments from datasets import load_dataset from torch.utils.data import IterableDataset from torch.utils.data.dataloader import DataLoader from transformers import AutoModelForCausalLM, AutoTokenizer, HfArgumentParser, set_seed class lowercase ( __SCREAMING_SNAKE_CASE ): def __init__( self , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__=1024 , SCREAMING_SNAKE_CASE__=1024 , SCREAMING_SNAKE_CASE__=3.6 ): """simple docstring""" lowerCAmelCase__ : Union[str, Any] = tokenizer lowerCAmelCase__ : Optional[int] = tokenizer.bos_token_id lowerCAmelCase__ : List[str] = dataset lowerCAmelCase__ : Dict = seq_length lowerCAmelCase__ : Tuple = seq_length * chars_per_token * num_of_sequences def __iter__( self ): """simple docstring""" lowerCAmelCase__ : List[Any] = iter(self.dataset ) lowerCAmelCase__ : Tuple = True while more_examples: lowerCAmelCase__ : Optional[Any] = [], 0 while True: if buffer_len >= self.input_characters: break try: buffer.append(next(__SCREAMING_SNAKE_CASE )['''content'''] ) buffer_len += len(buffer[-1] ) except StopIteration: lowerCAmelCase__ : Dict = False break lowerCAmelCase__ : Any = tokenizer(__SCREAMING_SNAKE_CASE , truncation=__SCREAMING_SNAKE_CASE )["""input_ids"""] lowerCAmelCase__ : List[Any] = [] for tokenized_input in tokenized_inputs: all_token_ids.extend(tokenized_input + [self.concat_token_id] ) for i in range(0 , len(__SCREAMING_SNAKE_CASE ) , self.seq_length ): lowerCAmelCase__ : List[str] = all_token_ids[i : i + self.seq_length] if len(__SCREAMING_SNAKE_CASE ) == self.seq_length: yield torch.tensor(__SCREAMING_SNAKE_CASE ) def _a ( __UpperCamelCase : List[Any] ): lowerCAmelCase__ : int = {"""streaming""": True} lowerCAmelCase__ : List[Any] = load_dataset(args.dataset_name ,split='''train''' ,**__UpperCamelCase ) lowerCAmelCase__ : Dict = ConstantLengthDataset(__UpperCamelCase ,__UpperCamelCase ,seq_length=args.seq_length ) lowerCAmelCase__ : Optional[int] = DataLoader(__UpperCamelCase ,batch_size=args.batch_size ) return eval_dataloader def _a ( __UpperCamelCase : Dict ): model.eval() lowerCAmelCase__ : int = [] for step, batch in enumerate(__UpperCamelCase ): with torch.no_grad(): lowerCAmelCase__ : Optional[Any] = model(__UpperCamelCase ,labels=__UpperCamelCase ) lowerCAmelCase__ : Union[str, Any] = outputs.loss.repeat(args.batch_size ) losses.append(accelerator.gather(__UpperCamelCase ) ) if args.max_eval_steps > 0 and step >= args.max_eval_steps: break lowerCAmelCase__ : Optional[Any] = torch.mean(torch.cat(__UpperCamelCase ) ) try: lowerCAmelCase__ : List[Any] = torch.exp(__UpperCamelCase ) except OverflowError: lowerCAmelCase__ : List[str] = float('''inf''' ) return loss.item(), perplexity.item() # Setup Accelerator A__ : List[str] = Accelerator() # Parse configuration A__ : List[str] = HfArgumentParser(EvaluationArguments) A__ : Optional[int] = parser.parse_args() set_seed(args.seed) # Logging A__ : Union[str, Any] = logging.getLogger(__name__) logging.basicConfig( format="""%(asctime)s - %(levelname)s - %(name)s - %(message)s""", datefmt="""%m/%d/%Y %H:%M:%S""", level=logging.INFO ) # Load model and tokenizer A__ : Optional[int] = AutoModelForCausalLM.from_pretrained(args.model_ckpt) A__ : Tuple = AutoTokenizer.from_pretrained(args.model_ckpt) # Load dataset and dataloader A__ : Dict = create_dataloader(args) # Prepare everything with our `accelerator`. A__ : Union[str, Any] = accelerator.prepare(model, eval_dataloader) # Evaluate and save the last checkpoint logger.info("""Evaluating and saving model after training""") A__ : Optional[Any] = evaluate(args) logger.info(f"""loss/eval: {eval_loss}, perplexity: {perplexity}""")
233
'''simple docstring''' import shutil import tempfile import unittest from unittest.mock import patch from transformers import ( DefaultFlowCallback, IntervalStrategy, PrinterCallback, ProgressCallback, Trainer, TrainerCallback, TrainingArguments, is_torch_available, ) from transformers.testing_utils import require_torch if is_torch_available(): from transformers.trainer import DEFAULT_CALLBACKS from .test_trainer import RegressionDataset, RegressionModelConfig, RegressionPreTrainedModel class __snake_case ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self ): snake_case__ : str = [] def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): self.events.append("""on_init_end""" ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): self.events.append("""on_train_begin""" ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): self.events.append("""on_train_end""" ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): self.events.append("""on_epoch_begin""" ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): self.events.append("""on_epoch_end""" ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): self.events.append("""on_step_begin""" ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): self.events.append("""on_step_end""" ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): self.events.append("""on_evaluate""" ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): self.events.append("""on_predict""" ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): self.events.append("""on_save""" ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): self.events.append("""on_log""" ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): self.events.append("""on_prediction_step""" ) @require_torch class __snake_case ( unittest.TestCase ): '''simple docstring''' def __UpperCamelCase ( self ): snake_case__ : Tuple = tempfile.mkdtemp() def __UpperCamelCase ( self ): shutil.rmtree(self.output_dir ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE=0 , __SCREAMING_SNAKE_CASE=0 , __SCREAMING_SNAKE_CASE=6_4 , __SCREAMING_SNAKE_CASE=6_4 , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=False , **__SCREAMING_SNAKE_CASE ): # disable_tqdm in TrainingArguments has a flaky default since it depends on the level of logging. We make sure # its set to False since the tests later on depend on its value. snake_case__ : List[Any] = RegressionDataset(length=__SCREAMING_SNAKE_CASE ) snake_case__ : List[str] = RegressionDataset(length=__SCREAMING_SNAKE_CASE ) snake_case__ : List[str] = RegressionModelConfig(a=__SCREAMING_SNAKE_CASE , b=__SCREAMING_SNAKE_CASE ) snake_case__ : List[str] = RegressionPreTrainedModel(__SCREAMING_SNAKE_CASE ) snake_case__ : Dict = TrainingArguments(self.output_dir , disable_tqdm=__SCREAMING_SNAKE_CASE , report_to=[] , **__SCREAMING_SNAKE_CASE ) return Trainer( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , train_dataset=__SCREAMING_SNAKE_CASE , eval_dataset=__SCREAMING_SNAKE_CASE , callbacks=__SCREAMING_SNAKE_CASE , ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): self.assertEqual(len(__SCREAMING_SNAKE_CASE ) , len(__SCREAMING_SNAKE_CASE ) ) # Order doesn't matter snake_case__ : Tuple = sorted(__SCREAMING_SNAKE_CASE , key=lambda __SCREAMING_SNAKE_CASE : cb.__name__ if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else cb.__class__.__name__ ) snake_case__ : List[str] = sorted(__SCREAMING_SNAKE_CASE , key=lambda __SCREAMING_SNAKE_CASE : cb.__name__ if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else cb.__class__.__name__ ) for cba, cba in zip(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) and isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) elif isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) and not isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): self.assertEqual(__SCREAMING_SNAKE_CASE , cba.__class__ ) elif not isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) and isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): self.assertEqual(cba.__class__ , __SCREAMING_SNAKE_CASE ) else: self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE ): snake_case__ : Tuple = ["""on_init_end""", """on_train_begin"""] snake_case__ : Union[str, Any] = 0 snake_case__ : Dict = len(trainer.get_eval_dataloader() ) snake_case__ : Any = ["""on_prediction_step"""] * len(trainer.get_eval_dataloader() ) + ["""on_log""", """on_evaluate"""] for _ in range(trainer.state.num_train_epochs ): expected_events.append("""on_epoch_begin""" ) for _ in range(__SCREAMING_SNAKE_CASE ): step += 1 expected_events += ["on_step_begin", "on_step_end"] if step % trainer.args.logging_steps == 0: expected_events.append("""on_log""" ) if trainer.args.evaluation_strategy == IntervalStrategy.STEPS and step % trainer.args.eval_steps == 0: expected_events += evaluation_events.copy() if step % trainer.args.save_steps == 0: expected_events.append("""on_save""" ) expected_events.append("""on_epoch_end""" ) if trainer.args.evaluation_strategy == IntervalStrategy.EPOCH: expected_events += evaluation_events.copy() expected_events += ["on_log", "on_train_end"] return expected_events def __UpperCamelCase ( self ): snake_case__ : Any = self.get_trainer() snake_case__ : str = DEFAULT_CALLBACKS.copy() + [ProgressCallback] self.check_callbacks_equality(trainer.callback_handler.callbacks , __SCREAMING_SNAKE_CASE ) # Callbacks passed at init are added to the default callbacks snake_case__ : List[str] = self.get_trainer(callbacks=[MyTestTrainerCallback] ) expected_callbacks.append(__SCREAMING_SNAKE_CASE ) self.check_callbacks_equality(trainer.callback_handler.callbacks , __SCREAMING_SNAKE_CASE ) # TrainingArguments.disable_tqdm controls if use ProgressCallback or PrinterCallback snake_case__ : Optional[Any] = self.get_trainer(disable_tqdm=__SCREAMING_SNAKE_CASE ) snake_case__ : Tuple = DEFAULT_CALLBACKS.copy() + [PrinterCallback] self.check_callbacks_equality(trainer.callback_handler.callbacks , __SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self ): snake_case__ : str = DEFAULT_CALLBACKS.copy() + [ProgressCallback] snake_case__ : int = self.get_trainer() # We can add, pop, or remove by class name trainer.remove_callback(__SCREAMING_SNAKE_CASE ) expected_callbacks.remove(__SCREAMING_SNAKE_CASE ) self.check_callbacks_equality(trainer.callback_handler.callbacks , __SCREAMING_SNAKE_CASE ) snake_case__ : Union[str, Any] = self.get_trainer() snake_case__ : List[str] = trainer.pop_callback(__SCREAMING_SNAKE_CASE ) self.assertEqual(cb.__class__ , __SCREAMING_SNAKE_CASE ) self.check_callbacks_equality(trainer.callback_handler.callbacks , __SCREAMING_SNAKE_CASE ) trainer.add_callback(__SCREAMING_SNAKE_CASE ) expected_callbacks.insert(0 , __SCREAMING_SNAKE_CASE ) self.check_callbacks_equality(trainer.callback_handler.callbacks , __SCREAMING_SNAKE_CASE ) # We can also add, pop, or remove by instance snake_case__ : List[Any] = self.get_trainer() snake_case__ : List[str] = trainer.callback_handler.callbacks[0] trainer.remove_callback(__SCREAMING_SNAKE_CASE ) expected_callbacks.remove(__SCREAMING_SNAKE_CASE ) self.check_callbacks_equality(trainer.callback_handler.callbacks , __SCREAMING_SNAKE_CASE ) snake_case__ : Optional[int] = self.get_trainer() snake_case__ : Any = trainer.callback_handler.callbacks[0] snake_case__ : Optional[Any] = trainer.pop_callback(__SCREAMING_SNAKE_CASE ) self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) self.check_callbacks_equality(trainer.callback_handler.callbacks , __SCREAMING_SNAKE_CASE ) trainer.add_callback(__SCREAMING_SNAKE_CASE ) expected_callbacks.insert(0 , __SCREAMING_SNAKE_CASE ) self.check_callbacks_equality(trainer.callback_handler.callbacks , __SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self ): import warnings # XXX: for now ignore scatter_gather warnings in this test since it's not relevant to what's being tested warnings.simplefilter(action="""ignore""" , category=__SCREAMING_SNAKE_CASE ) snake_case__ : Any = self.get_trainer(callbacks=[MyTestTrainerCallback] ) trainer.train() snake_case__ : Any = trainer.callback_handler.callbacks[-2].events self.assertEqual(__SCREAMING_SNAKE_CASE , self.get_expected_events(__SCREAMING_SNAKE_CASE ) ) # Independent log/save/eval snake_case__ : Dict = self.get_trainer(callbacks=[MyTestTrainerCallback] , logging_steps=5 ) trainer.train() snake_case__ : int = trainer.callback_handler.callbacks[-2].events self.assertEqual(__SCREAMING_SNAKE_CASE , self.get_expected_events(__SCREAMING_SNAKE_CASE ) ) snake_case__ : Any = self.get_trainer(callbacks=[MyTestTrainerCallback] , save_steps=5 ) trainer.train() snake_case__ : Any = trainer.callback_handler.callbacks[-2].events self.assertEqual(__SCREAMING_SNAKE_CASE , self.get_expected_events(__SCREAMING_SNAKE_CASE ) ) snake_case__ : Tuple = self.get_trainer(callbacks=[MyTestTrainerCallback] , eval_steps=5 , evaluation_strategy="""steps""" ) trainer.train() snake_case__ : str = trainer.callback_handler.callbacks[-2].events self.assertEqual(__SCREAMING_SNAKE_CASE , self.get_expected_events(__SCREAMING_SNAKE_CASE ) ) snake_case__ : Tuple = self.get_trainer(callbacks=[MyTestTrainerCallback] , evaluation_strategy="""epoch""" ) trainer.train() snake_case__ : Any = trainer.callback_handler.callbacks[-2].events self.assertEqual(__SCREAMING_SNAKE_CASE , self.get_expected_events(__SCREAMING_SNAKE_CASE ) ) # A bit of everything snake_case__ : Dict = self.get_trainer( callbacks=[MyTestTrainerCallback] , logging_steps=3 , save_steps=1_0 , eval_steps=5 , evaluation_strategy="""steps""" , ) trainer.train() snake_case__ : Tuple = trainer.callback_handler.callbacks[-2].events self.assertEqual(__SCREAMING_SNAKE_CASE , self.get_expected_events(__SCREAMING_SNAKE_CASE ) ) # warning should be emitted for duplicated callbacks with patch("""transformers.trainer_callback.logger.warning""" ) as warn_mock: snake_case__ : List[str] = self.get_trainer( callbacks=[MyTestTrainerCallback, MyTestTrainerCallback] , ) assert str(__SCREAMING_SNAKE_CASE ) in warn_mock.call_args[0][0]
38
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 from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) SCREAMING_SNAKE_CASE__ = { "microsoft/swin-tiny-patch4-window7-224": ( "https://huggingface.co/microsoft/swin-tiny-patch4-window7-224/resolve/main/config.json" ), # See all Swin models at https://huggingface.co/models?filter=swin } class _UpperCamelCase( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): __SCREAMING_SNAKE_CASE : int = '''swin''' __SCREAMING_SNAKE_CASE : Optional[Any] = { '''num_attention_heads''': '''num_heads''', '''num_hidden_layers''': '''num_layers''', } def __init__( self : Dict , SCREAMING_SNAKE_CASE__ : Union[str, Any]=2_2_4 , SCREAMING_SNAKE_CASE__ : Tuple=4 , SCREAMING_SNAKE_CASE__ : List[str]=3 , SCREAMING_SNAKE_CASE__ : Optional[Any]=9_6 , SCREAMING_SNAKE_CASE__ : Tuple=[2, 2, 6, 2] , SCREAMING_SNAKE_CASE__ : str=[3, 6, 1_2, 2_4] , SCREAMING_SNAKE_CASE__ : Tuple=7 , SCREAMING_SNAKE_CASE__ : Any=4.0 , SCREAMING_SNAKE_CASE__ : Tuple=True , SCREAMING_SNAKE_CASE__ : Tuple=0.0 , SCREAMING_SNAKE_CASE__ : str=0.0 , SCREAMING_SNAKE_CASE__ : Optional[Any]=0.1 , SCREAMING_SNAKE_CASE__ : List[str]="gelu" , SCREAMING_SNAKE_CASE__ : Dict=False , SCREAMING_SNAKE_CASE__ : int=0.02 , SCREAMING_SNAKE_CASE__ : List[Any]=1e-5 , SCREAMING_SNAKE_CASE__ : Tuple=3_2 , SCREAMING_SNAKE_CASE__ : Any=None , SCREAMING_SNAKE_CASE__ : Tuple=None , **SCREAMING_SNAKE_CASE__ : Tuple , ): '''simple docstring''' super().__init__(**__SCREAMING_SNAKE_CASE ) __a : List[str] = image_size __a : Dict = patch_size __a : Union[str, Any] = num_channels __a : List[str] = embed_dim __a : Optional[Any] = depths __a : Dict = len(__SCREAMING_SNAKE_CASE ) __a : Dict = num_heads __a : str = window_size __a : Union[str, Any] = mlp_ratio __a : List[str] = qkv_bias __a : List[str] = hidden_dropout_prob __a : Optional[Any] = attention_probs_dropout_prob __a : Tuple = drop_path_rate __a : Tuple = hidden_act __a : Union[str, Any] = use_absolute_embeddings __a : int = layer_norm_eps __a : List[str] = initializer_range __a : Optional[Any] = encoder_stride # we set the hidden_size attribute in order to make Swin work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model __a : Optional[Any] = int(embed_dim * 2 ** (len(__SCREAMING_SNAKE_CASE ) - 1) ) __a : Tuple = ["""stem"""] + [f'''stage{idx}''' for idx in range(1 , len(__SCREAMING_SNAKE_CASE ) + 1 )] __a : List[Any] = get_aligned_output_features_output_indices( out_features=__SCREAMING_SNAKE_CASE , out_indices=__SCREAMING_SNAKE_CASE , stage_names=self.stage_names ) class _UpperCamelCase( __SCREAMING_SNAKE_CASE ): __SCREAMING_SNAKE_CASE : Tuple = version.parse('''1.11''' ) @property def __lowerCAmelCase ( self : List[str] ): '''simple docstring''' return OrderedDict( [ ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}), ] ) @property def __lowerCAmelCase ( self : str ): '''simple docstring''' return 1e-4
47
'''simple docstring''' 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 __snake_case ( unittest.TestCase ): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=7 , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=1_8 , __SCREAMING_SNAKE_CASE=3_0 , __SCREAMING_SNAKE_CASE=4_0_0 , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=[0.5, 0.5, 0.5] , __SCREAMING_SNAKE_CASE=[0.5, 0.5, 0.5] , ): snake_case__ : Any = size if size is not None else {"""height""": 1_8, """width""": 1_8} snake_case__ : List[Any] = parent snake_case__ : int = batch_size snake_case__ : List[Any] = num_channels snake_case__ : str = image_size snake_case__ : Union[str, Any] = min_resolution snake_case__ : List[Any] = max_resolution snake_case__ : Tuple = do_resize snake_case__ : int = size snake_case__ : Tuple = do_normalize snake_case__ : Dict = image_mean snake_case__ : Union[str, Any] = image_std def __UpperCamelCase ( self ): 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 __snake_case ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowerCamelCase__ = DPTImageProcessor if is_vision_available() else None def __UpperCamelCase ( self ): snake_case__ : str = DPTImageProcessingTester(self ) @property def __UpperCamelCase ( self ): return self.image_processor_tester.prepare_image_processor_dict() def __UpperCamelCase ( self ): snake_case__ : Optional[int] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , """image_mean""" ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , """image_std""" ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , """do_normalize""" ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , """do_resize""" ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , """size""" ) ) def __UpperCamelCase ( self ): snake_case__ : Any = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"""height""": 1_8, """width""": 1_8} ) snake_case__ : List[Any] = self.image_processing_class.from_dict(self.image_processor_dict , size=4_2 ) self.assertEqual(image_processor.size , {"""height""": 4_2, """width""": 4_2} ) def __UpperCamelCase ( self ): # Initialize image_processing snake_case__ : Optional[int] = self.image_processing_class(**self.image_processor_dict ) # create random PIL images snake_case__ : List[Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=__SCREAMING_SNAKE_CASE ) for image in image_inputs: self.assertIsInstance(__SCREAMING_SNAKE_CASE , Image.Image ) # Test not batched input snake_case__ : Optional[int] = 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 snake_case__ : List[str] = image_processing(__SCREAMING_SNAKE_CASE , 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 __UpperCamelCase ( self ): # Initialize image_processing snake_case__ : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors snake_case__ : Optional[int] = prepare_image_inputs(self.image_processor_tester , equal_resolution=__SCREAMING_SNAKE_CASE , numpify=__SCREAMING_SNAKE_CASE ) for image in image_inputs: self.assertIsInstance(__SCREAMING_SNAKE_CASE , np.ndarray ) # Test not batched input snake_case__ : List[str] = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) , ) # Test batched snake_case__ : Any = image_processing(__SCREAMING_SNAKE_CASE , 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 __UpperCamelCase ( self ): # Initialize image_processing snake_case__ : List[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors snake_case__ : List[str] = prepare_image_inputs(self.image_processor_tester , equal_resolution=__SCREAMING_SNAKE_CASE , torchify=__SCREAMING_SNAKE_CASE ) for image in image_inputs: self.assertIsInstance(__SCREAMING_SNAKE_CASE , torch.Tensor ) # Test not batched input snake_case__ : List[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 snake_case__ : List[str] = image_processing(__SCREAMING_SNAKE_CASE , 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"""], ) , )
38
0
'''simple docstring''' import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import BeitConfig, BeitForImageClassification, BeitForMaskedImageModeling, BeitImageProcessor from transformers.image_utils import PILImageResampling from transformers.utils import logging logging.set_verbosity_info() _a : List[Any] = logging.get_logger(__name__) def _a (lowercase__ : str , lowercase__ : Optional[Any]=False , lowercase__ : Dict=False ) -> Optional[int]: """simple docstring""" __snake_case = """backbone.""" if is_semantic else """""" __snake_case = [] for i in range(config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append((f'{prefix}blocks.{i}.norm1.weight', f'beit.encoder.layer.{i}.layernorm_before.weight') ) rename_keys.append((f'{prefix}blocks.{i}.norm1.bias', f'beit.encoder.layer.{i}.layernorm_before.bias') ) rename_keys.append( (f'{prefix}blocks.{i}.attn.proj.weight', f'beit.encoder.layer.{i}.attention.output.dense.weight') ) rename_keys.append( (f'{prefix}blocks.{i}.attn.proj.bias', f'beit.encoder.layer.{i}.attention.output.dense.bias') ) rename_keys.append((f'{prefix}blocks.{i}.norm2.weight', f'beit.encoder.layer.{i}.layernorm_after.weight') ) rename_keys.append((f'{prefix}blocks.{i}.norm2.bias', f'beit.encoder.layer.{i}.layernorm_after.bias') ) rename_keys.append((f'{prefix}blocks.{i}.mlp.fc1.weight', f'beit.encoder.layer.{i}.intermediate.dense.weight') ) rename_keys.append((f'{prefix}blocks.{i}.mlp.fc1.bias', f'beit.encoder.layer.{i}.intermediate.dense.bias') ) rename_keys.append((f'{prefix}blocks.{i}.mlp.fc2.weight', f'beit.encoder.layer.{i}.output.dense.weight') ) rename_keys.append((f'{prefix}blocks.{i}.mlp.fc2.bias', f'beit.encoder.layer.{i}.output.dense.bias') ) # projection layer + position embeddings rename_keys.extend( [ (f'{prefix}cls_token', 'beit.embeddings.cls_token'), (f'{prefix}patch_embed.proj.weight', 'beit.embeddings.patch_embeddings.projection.weight'), (f'{prefix}patch_embed.proj.bias', 'beit.embeddings.patch_embeddings.projection.bias'), (f'{prefix}pos_embed', 'beit.embeddings.position_embeddings'), ] ) if has_lm_head: # mask token + layernorm rename_keys.extend( [ ('mask_token', 'beit.embeddings.mask_token'), ('norm.weight', 'layernorm.weight'), ('norm.bias', 'layernorm.bias'), ] ) else: # layernorm + classification head rename_keys.extend( [ ('fc_norm.weight', 'beit.pooler.layernorm.weight'), ('fc_norm.bias', 'beit.pooler.layernorm.bias'), ('head.weight', 'classifier.weight'), ('head.bias', 'classifier.bias'), ] ) return rename_keys def _a (lowercase__ : int , lowercase__ : Dict , lowercase__ : Tuple=False , lowercase__ : Any=False ) -> Dict: """simple docstring""" for i in range(config.num_hidden_layers ): __snake_case = """backbone.""" if is_semantic else """""" # queries, keys and values __snake_case = state_dict.pop(f'{prefix}blocks.{i}.attn.qkv.weight' ) __snake_case = state_dict.pop(f'{prefix}blocks.{i}.attn.q_bias' ) __snake_case = state_dict.pop(f'{prefix}blocks.{i}.attn.v_bias' ) __snake_case = in_proj_weight[ : config.hidden_size, : ] __snake_case = q_bias __snake_case = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] __snake_case = in_proj_weight[ -config.hidden_size :, : ] __snake_case = v_bias # gamma_1 and gamma_2 # we call them lambda because otherwise they are renamed when using .from_pretrained __snake_case = state_dict.pop(f'{prefix}blocks.{i}.gamma_1' ) __snake_case = state_dict.pop(f'{prefix}blocks.{i}.gamma_2' ) __snake_case = gamma_a __snake_case = gamma_a def _a (lowercase__ : str , lowercase__ : List[Any] , lowercase__ : Any ) -> Optional[int]: """simple docstring""" __snake_case = dct.pop(lowercase__ ) __snake_case = val def _a () -> Dict: """simple docstring""" __snake_case = """http://images.cocodataset.org/val2017/000000039769.jpg""" __snake_case = Image.open(requests.get(lowercase__ , stream=lowercase__ ).raw ) return im @torch.no_grad() def _a (lowercase__ : Union[str, Any] , lowercase__ : Optional[Any] , lowercase__ : Any=False ) -> Any: """simple docstring""" __snake_case = False if """rvlcdip""" in checkpoint_url else True __snake_case = BeitConfig(use_absolute_position_embeddings=lowercase__ , use_mask_token=lowercase__ ) # size of the architecture if "large" in checkpoint_url or "dit-l" in checkpoint_url: __snake_case = 1_0_2_4 __snake_case = 4_0_9_6 __snake_case = 2_4 __snake_case = 1_6 # labels if "rvlcdip" in checkpoint_url: __snake_case = 1_6 __snake_case = """huggingface/label-files""" __snake_case = """rvlcdip-id2label.json""" __snake_case = json.load(open(hf_hub_download(lowercase__ , lowercase__ , repo_type='dataset' ) , 'r' ) ) __snake_case = {int(lowercase__ ): v for k, v in idalabel.items()} __snake_case = idalabel __snake_case = {v: k for k, v in idalabel.items()} # load state_dict of original model, remove and rename some keys __snake_case = torch.hub.load_state_dict_from_url(lowercase__ , map_location='cpu' )["""model"""] __snake_case = create_rename_keys(lowercase__ , has_lm_head=lowercase__ ) for src, dest in rename_keys: rename_key(lowercase__ , lowercase__ , lowercase__ ) read_in_q_k_v(lowercase__ , lowercase__ , has_lm_head=lowercase__ ) # load HuggingFace model __snake_case = BeitForMaskedImageModeling(lowercase__ ) if has_lm_head else BeitForImageClassification(lowercase__ ) model.eval() model.load_state_dict(lowercase__ ) # Check outputs on an image __snake_case = BeitImageProcessor( size=config.image_size , resample=PILImageResampling.BILINEAR , do_center_crop=lowercase__ ) __snake_case = prepare_img() __snake_case = image_processor(images=lowercase__ , return_tensors='pt' ) __snake_case = encoding["""pixel_values"""] __snake_case = model(lowercase__ ) __snake_case = outputs.logits # verify logits __snake_case = [1, 1_6] if """rvlcdip""" in checkpoint_url else [1, 1_9_6, 8_1_9_2] assert logits.shape == torch.Size(lowercase__ ), "Shape of logits not as expected" Path(lowercase__ ).mkdir(exist_ok=lowercase__ ) print(f'Saving model to {pytorch_dump_folder_path}' ) model.save_pretrained(lowercase__ ) print(f'Saving image processor to {pytorch_dump_folder_path}' ) image_processor.save_pretrained(lowercase__ ) if push_to_hub: if has_lm_head: __snake_case = """dit-base""" if """base""" in checkpoint_url else """dit-large""" else: __snake_case = """dit-base-finetuned-rvlcdip""" if """dit-b""" in checkpoint_url else """dit-large-finetuned-rvlcdip""" image_processor.push_to_hub( repo_path_or_name=Path(lowercase__ , lowercase__ ) , organization='nielsr' , commit_message='Add image processor' , use_temp_dir=lowercase__ , ) model.push_to_hub( repo_path_or_name=Path(lowercase__ , lowercase__ ) , organization='nielsr' , commit_message='Add model' , use_temp_dir=lowercase__ , ) if __name__ == "__main__": _a : List[Any] = argparse.ArgumentParser() parser.add_argument( "--checkpoint_url", default="https://layoutlm.blob.core.windows.net/dit/dit-pts/dit-base-224-p16-500k-62d53a.pth", type=str, help="URL to the original PyTorch checkpoint (.pth file).", ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, help="Path to the folder to output PyTorch model." ) parser.add_argument( "--push_to_hub", action="store_true", ) _a : Optional[Any] = parser.parse_args() convert_dit_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path, args.push_to_hub)
56
'''simple docstring''' from __future__ import annotations import inspect import unittest from math import floor import numpy as np from transformers import CvtConfig from transformers.testing_utils import require_tf, require_vision, slow from transformers.utils import cached_property, is_tf_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFCvtForImageClassification, TFCvtModel from transformers.models.cvt.modeling_tf_cvt import TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class __snake_case ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' def __UpperCamelCase ( self ): snake_case__ : Dict = self.config_class(**self.inputs_dict ) self.parent.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , """embed_dim""" ) ) self.parent.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , """num_heads""" ) ) class __snake_case : '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=1_3 , __SCREAMING_SNAKE_CASE=6_4 , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=[1_6, 4_8, 9_6] , __SCREAMING_SNAKE_CASE=[1, 3, 6] , __SCREAMING_SNAKE_CASE=[1, 2, 1_0] , __SCREAMING_SNAKE_CASE=[7, 3, 3] , __SCREAMING_SNAKE_CASE=[4, 2, 2] , __SCREAMING_SNAKE_CASE=[2, 1, 1] , __SCREAMING_SNAKE_CASE=[2, 2, 2] , __SCREAMING_SNAKE_CASE=[False, False, True] , __SCREAMING_SNAKE_CASE=[0.0, 0.0, 0.0] , __SCREAMING_SNAKE_CASE=0.02 , __SCREAMING_SNAKE_CASE=1e-1_2 , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=2 , ): snake_case__ : List[str] = parent snake_case__ : Tuple = batch_size snake_case__ : Union[str, Any] = image_size snake_case__ : List[Any] = patch_sizes snake_case__ : Optional[int] = patch_stride snake_case__ : Optional[Any] = patch_padding snake_case__ : Any = is_training snake_case__ : int = use_labels snake_case__ : Dict = num_labels snake_case__ : Optional[Any] = num_channels snake_case__ : Optional[Any] = embed_dim snake_case__ : Optional[int] = num_heads snake_case__ : Optional[int] = stride_kv snake_case__ : int = depth snake_case__ : Optional[Any] = cls_token snake_case__ : List[Any] = attention_drop_rate snake_case__ : Union[str, Any] = initializer_range snake_case__ : List[Any] = layer_norm_eps def __UpperCamelCase ( self ): snake_case__ : Optional[int] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) snake_case__ : List[Any] = None if self.use_labels: # create a random int32 tensor of given shape snake_case__ : List[str] = ids_tensor([self.batch_size] , self.num_labels ) snake_case__ : List[str] = self.get_config() return config, pixel_values, labels def __UpperCamelCase ( self ): return CvtConfig( image_size=self.image_size , num_labels=self.num_labels , num_channels=self.num_channels , embed_dim=self.embed_dim , num_heads=self.num_heads , patch_sizes=self.patch_sizes , patch_padding=self.patch_padding , patch_stride=self.patch_stride , stride_kv=self.stride_kv , depth=self.depth , cls_token=self.cls_token , attention_drop_rate=self.attention_drop_rate , initializer_range=self.initializer_range , ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): snake_case__ : int = TFCvtModel(config=__SCREAMING_SNAKE_CASE ) snake_case__ : List[Any] = model(__SCREAMING_SNAKE_CASE , training=__SCREAMING_SNAKE_CASE ) snake_case__ : Tuple = (self.image_size, self.image_size) snake_case__ , snake_case__ : str = image_size[0], image_size[1] for i in range(len(self.depth ) ): snake_case__ : Any = floor(((height + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1 ) snake_case__ : Optional[int] = floor(((width + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1 ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.embed_dim[-1], height, width) ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): snake_case__ : Any = self.num_labels snake_case__ : str = TFCvtForImageClassification(__SCREAMING_SNAKE_CASE ) snake_case__ : List[str] = model(__SCREAMING_SNAKE_CASE , labels=__SCREAMING_SNAKE_CASE , training=__SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __UpperCamelCase ( self ): snake_case__ : List[Any] = self.prepare_config_and_inputs() snake_case__ , snake_case__ , snake_case__ : Any = config_and_inputs snake_case__ : Union[str, Any] = {"""pixel_values""": pixel_values} return config, inputs_dict @require_tf class __snake_case ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowerCamelCase__ = (TFCvtModel, TFCvtForImageClassification) if is_tf_available() else () lowerCamelCase__ = ( {'''feature-extraction''': TFCvtModel, '''image-classification''': TFCvtForImageClassification} if is_tf_available() else {} ) lowerCamelCase__ = False lowerCamelCase__ = False lowerCamelCase__ = False lowerCamelCase__ = False lowerCamelCase__ = False def __UpperCamelCase ( self ): snake_case__ : Optional[Any] = TFCvtModelTester(self ) snake_case__ : Any = TFCvtConfigTester(self , config_class=__SCREAMING_SNAKE_CASE , has_text_modality=__SCREAMING_SNAKE_CASE , hidden_size=3_7 ) def __UpperCamelCase ( self ): self.config_tester.create_and_test_config_common_properties() self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() @unittest.skip(reason="""Cvt does not output attentions""" ) def __UpperCamelCase ( self ): pass @unittest.skip(reason="""Cvt does not use inputs_embeds""" ) def __UpperCamelCase ( self ): pass @unittest.skip(reason="""Cvt does not support input and output embeddings""" ) def __UpperCamelCase ( self ): pass @unittest.skipIf( not is_tf_available() or len(tf.config.list_physical_devices("""GPU""" ) ) == 0 , reason="""TF does not support backprop for grouped convolutions on CPU.""" , ) def __UpperCamelCase ( self ): super().test_dataset_conversion() @unittest.skipIf( not is_tf_available() or len(tf.config.list_physical_devices("""GPU""" ) ) == 0 , reason="""TF does not support backprop for grouped convolutions on CPU.""" , ) @slow def __UpperCamelCase ( self ): super().test_keras_fit() @unittest.skip(reason="""Get `Failed to determine best cudnn convolution algo.` error after using TF 2.12+cuda 11.8""" ) def __UpperCamelCase ( self ): snake_case__ : List[str] = tf.keras.mixed_precision.Policy("""mixed_float16""" ) tf.keras.mixed_precision.set_global_policy(__SCREAMING_SNAKE_CASE ) super().test_keras_fit() tf.keras.mixed_precision.set_global_policy("""float32""" ) def __UpperCamelCase ( self ): snake_case__ , snake_case__ : List[str] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: snake_case__ : Any = model_class(__SCREAMING_SNAKE_CASE ) snake_case__ : str = inspect.signature(model.call ) # signature.parameters is an OrderedDict => so arg_names order is deterministic snake_case__ : Optional[Any] = [*signature.parameters.keys()] snake_case__ : Tuple = ["""pixel_values"""] self.assertListEqual(arg_names[:1] , __SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self ): def check_hidden_states_output(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): snake_case__ : str = model_class(__SCREAMING_SNAKE_CASE ) snake_case__ : List[Any] = model(**self._prepare_for_class(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ) snake_case__ : Optional[int] = outputs.hidden_states snake_case__ : Tuple = len(self.model_tester.depth ) self.assertEqual(len(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) # verify the first hidden states (first block) self.assertListEqual( list(hidden_states[0].shape[-3:] ) , [ self.model_tester.embed_dim[0], self.model_tester.image_size // 4, self.model_tester.image_size // 4, ] , ) snake_case__ , snake_case__ : str = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: snake_case__ : List[Any] = True check_hidden_states_output(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] snake_case__ : List[str] = True check_hidden_states_output(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self ): snake_case__ : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self ): snake_case__ : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*__SCREAMING_SNAKE_CASE ) @slow def __UpperCamelCase ( self ): for model_name in TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: snake_case__ : str = TFCvtModel.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsNotNone(__SCREAMING_SNAKE_CASE ) def UpperCamelCase__ ( ) -> str: '''simple docstring''' snake_case__ : Union[str, Any] = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) return image @require_tf @require_vision class __snake_case ( unittest.TestCase ): '''simple docstring''' @cached_property def __UpperCamelCase ( self ): return AutoImageProcessor.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) @slow def __UpperCamelCase ( self ): snake_case__ : Optional[Any] = TFCvtForImageClassification.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) snake_case__ : Union[str, Any] = self.default_image_processor snake_case__ : int = prepare_img() snake_case__ : Dict = image_processor(images=__SCREAMING_SNAKE_CASE , return_tensors="""tf""" ) # forward pass snake_case__ : Optional[int] = model(**__SCREAMING_SNAKE_CASE ) # verify the logits snake_case__ : str = tf.TensorShape((1, 1_0_0_0) ) self.assertEqual(outputs.logits.shape , __SCREAMING_SNAKE_CASE ) snake_case__ : int = tf.constant([0.9285, 0.9015, -0.3150] ) self.assertTrue(np.allclose(outputs.logits[0, :3].numpy() , __SCREAMING_SNAKE_CASE , atol=1e-4 ) )
38
0
from math import asin, atan, cos, radians, sin, sqrt, tan snake_case__ : Dict = 637_8137.0 snake_case__ : Optional[Any] = 635_6752.31_4245 snake_case__ : List[str] = 6_3_7_8_1_3_7 def _snake_case (__lowercase , __lowercase , __lowercase , __lowercase): UpperCamelCase_ = (AXIS_A - AXIS_B) / AXIS_A UpperCamelCase_ = atan((1 - flattening) * tan(radians(__lowercase))) UpperCamelCase_ = atan((1 - flattening) * tan(radians(__lowercase))) UpperCamelCase_ = radians(__lowercase) UpperCamelCase_ = radians(__lowercase) # Equation UpperCamelCase_ = sin((phi_a - phi_a) / 2) UpperCamelCase_ = sin((lambda_a - lambda_a) / 2) # Square both values sin_sq_phi *= sin_sq_phi sin_sq_lambda *= sin_sq_lambda UpperCamelCase_ = sqrt(sin_sq_phi + (cos(__lowercase) * cos(__lowercase) * sin_sq_lambda)) return 2 * RADIUS * asin(__lowercase) if __name__ == "__main__": import doctest doctest.testmod()
23
'''simple docstring''' import unittest from transformers import is_torch_available from transformers.testing_utils import require_torch if is_torch_available(): import torch from transformers.generation import DisjunctiveConstraint @require_torch class __snake_case ( unittest.TestCase ): '''simple docstring''' def __UpperCamelCase ( self ): # For consistency across different places the DisjunctiveConstraint is called, # dc.token_ids is a list of integers. It is also initialized only by integers. snake_case__ : int = [[1, 2, 4], [1, 2, 3, 4]] snake_case__ : Any = DisjunctiveConstraint(__SCREAMING_SNAKE_CASE ) self.assertTrue(isinstance(dc.token_ids , __SCREAMING_SNAKE_CASE ) ) with self.assertRaises(__SCREAMING_SNAKE_CASE ): DisjunctiveConstraint(torch.LongTensor([[1, 2, 4], [1, 2, 3]] ) ) with self.assertRaises(__SCREAMING_SNAKE_CASE ): DisjunctiveConstraint([torch.LongTensor([1, 2, 4] ), torch.LongTensor([1, 2, 3, 4, 5] )] ) def __UpperCamelCase ( self ): # We can't have constraints that are complete subsets of another. This leads to a preverse # interpretation of "constraint fulfillment": does generating [1,2,3] fulfill the constraint? # It would mean that it generated [1,2] which fulfills it, but it's in the middle of potentially # fulfilling [1,2,3,4]. If we believe that [1,2,3] does fulfill the constraint, then the algorithm # will necessarily never reach [1,2,3,4], giving users a false sense of control (better to just not allow it). snake_case__ : Union[str, Any] = [[1, 2], [1, 2, 3, 4]] with self.assertRaises(__SCREAMING_SNAKE_CASE ): DisjunctiveConstraint(__SCREAMING_SNAKE_CASE ) # fails here def __UpperCamelCase ( self ): snake_case__ : List[str] = [[1, 2, 3], [1, 2, 4]] snake_case__ : Optional[int] = DisjunctiveConstraint(__SCREAMING_SNAKE_CASE ) snake_case__ , snake_case__ , snake_case__ : Any = dc.update(1 ) snake_case__ : Any = stepped is True and completed is False and reset is False self.assertTrue(__SCREAMING_SNAKE_CASE ) self.assertTrue(not dc.completed ) self.assertTrue(dc.current_seq == [1] ) snake_case__ , snake_case__ , snake_case__ : Tuple = dc.update(2 ) snake_case__ : Tuple = stepped is True and completed is False and reset is False self.assertTrue(__SCREAMING_SNAKE_CASE ) self.assertTrue(not dc.completed ) self.assertTrue(dc.current_seq == [1, 2] ) snake_case__ , snake_case__ , snake_case__ : Union[str, Any] = dc.update(3 ) snake_case__ : List[str] = stepped is True and completed is True and reset is False self.assertTrue(__SCREAMING_SNAKE_CASE ) self.assertTrue(dc.completed ) # Completed! self.assertTrue(dc.current_seq == [1, 2, 3] ) def __UpperCamelCase ( self ): snake_case__ : Optional[Any] = [[1, 2, 3], [1, 2, 4, 5], [1, 2, 5]] snake_case__ : int = DisjunctiveConstraint(__SCREAMING_SNAKE_CASE ) snake_case__ , snake_case__ , snake_case__ : str = dc.update(1 ) self.assertTrue(not dc.completed ) self.assertTrue(dc.current_seq == [1] ) snake_case__ , snake_case__ , snake_case__ : str = dc.update(2 ) self.assertTrue(not dc.completed ) self.assertTrue(dc.current_seq == [1, 2] ) snake_case__ , snake_case__ , snake_case__ : List[Any] = dc.update(4 ) self.assertTrue(not dc.completed ) self.assertTrue(dc.current_seq == [1, 2, 4] ) snake_case__ , snake_case__ , snake_case__ : Union[str, Any] = dc.update(5 ) self.assertTrue(dc.completed ) # Completed! self.assertTrue(dc.current_seq == [1, 2, 4, 5] ) dc.reset() snake_case__ , snake_case__ , snake_case__ : List[Any] = dc.update(1 ) self.assertTrue(not dc.completed ) self.assertTrue(dc.remaining() == 3 ) self.assertTrue(dc.current_seq == [1] ) snake_case__ , snake_case__ , snake_case__ : List[Any] = dc.update(2 ) self.assertTrue(not dc.completed ) self.assertTrue(dc.remaining() == 2 ) self.assertTrue(dc.current_seq == [1, 2] ) snake_case__ , snake_case__ , snake_case__ : Dict = dc.update(5 ) self.assertTrue(dc.completed ) # Completed! self.assertTrue(dc.remaining() == 0 ) self.assertTrue(dc.current_seq == [1, 2, 5] )
38
0
import logging import os from dataclasses import dataclass from typing import List, Optional, Union import tqdm from filelock import FileLock from transformers import ( BartTokenizer, BartTokenizerFast, DataProcessor, PreTrainedTokenizer, RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, is_tf_available, is_torch_available, ) UpperCAmelCase__ : Any = logging.getLogger(__name__) @dataclass(frozen=__SCREAMING_SNAKE_CASE ) class UpperCamelCase_ : '''simple docstring''' UpperCamelCase_ = 42 UpperCamelCase_ = 42 UpperCamelCase_ = None UpperCamelCase_ = None UpperCamelCase_ = None @dataclass(frozen=__SCREAMING_SNAKE_CASE ) class UpperCamelCase_ : '''simple docstring''' UpperCamelCase_ = 42 UpperCamelCase_ = None UpperCamelCase_ = None UpperCamelCase_ = None UpperCamelCase_ = None if is_torch_available(): import torch from torch.utils.data import Dataset class UpperCamelCase_ ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' UpperCamelCase_ = 42 def __init__( self , UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase = None , UpperCamelCase=False , UpperCamelCase = False , ) -> Tuple: UpperCamelCase__ : Dict = hans_processors[task]() UpperCamelCase__ : Tuple = os.path.join( __SCREAMING_SNAKE_CASE , 'cached_{}_{}_{}_{}'.format( 'dev' if evaluate else 'train' , tokenizer.__class__.__name__ , str(__SCREAMING_SNAKE_CASE) , __SCREAMING_SNAKE_CASE , ) , ) UpperCamelCase__ : Union[str, Any] = processor.get_labels() if tokenizer.__class__ in ( RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, BartTokenizer, BartTokenizerFast, ): # HACK(label indices are swapped in RoBERTa pretrained model) UpperCamelCase__ : List[str] = label_list[2], label_list[1] UpperCamelCase__ : List[str] = label_list # Make sure only the first process in distributed training processes the dataset, # and the others will use the cache. UpperCamelCase__ : Optional[Any] = cached_features_file + """.lock""" with FileLock(__SCREAMING_SNAKE_CASE): if os.path.exists(__SCREAMING_SNAKE_CASE) and not overwrite_cache: logger.info(F"""Loading features from cached file {cached_features_file}""") UpperCamelCase__ : str = torch.load(__SCREAMING_SNAKE_CASE) else: logger.info(F"""Creating features from dataset file at {data_dir}""") UpperCamelCase__ : List[Any] = ( processor.get_dev_examples(__SCREAMING_SNAKE_CASE) if evaluate else processor.get_train_examples(__SCREAMING_SNAKE_CASE) ) logger.info('Training examples: %s' , len(__SCREAMING_SNAKE_CASE)) UpperCamelCase__ : List[Any] = hans_convert_examples_to_features(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE) logger.info('Saving features into cached file %s' , __SCREAMING_SNAKE_CASE) torch.save(self.features , __SCREAMING_SNAKE_CASE) def __len__( self) -> Dict: return len(self.features) def __getitem__( self , UpperCamelCase) -> int: return self.features[i] def lowerCAmelCase__ ( self) -> Optional[int]: return self.label_list if is_tf_available(): import tensorflow as tf class UpperCamelCase_ : '''simple docstring''' UpperCamelCase_ = 42 def __init__( self , UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase = 1_28 , UpperCamelCase=False , UpperCamelCase = False , ) -> str: UpperCamelCase__ : Tuple = hans_processors[task]() UpperCamelCase__ : Any = processor.get_labels() if tokenizer.__class__ in ( RobertaTokenizer, RobertaTokenizerFast, XLMRobertaTokenizer, BartTokenizer, BartTokenizerFast, ): # HACK(label indices are swapped in RoBERTa pretrained model) UpperCamelCase__ : List[Any] = label_list[2], label_list[1] UpperCamelCase__ : int = label_list UpperCamelCase__ : Union[str, Any] = processor.get_dev_examples(__SCREAMING_SNAKE_CASE) if evaluate else processor.get_train_examples(__SCREAMING_SNAKE_CASE) UpperCamelCase__ : Optional[Any] = hans_convert_examples_to_features(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE) def gen(): for ex_index, ex in tqdm.tqdm(enumerate(self.features) , desc='convert examples to features'): if ex_index % 1_00_00 == 0: logger.info('Writing example %d of %d' % (ex_index, len(__SCREAMING_SNAKE_CASE))) yield ( { "example_id": 0, "input_ids": ex.input_ids, "attention_mask": ex.attention_mask, "token_type_ids": ex.token_type_ids, }, ex.label, ) UpperCamelCase__ : Optional[int] = tf.data.Dataset.from_generator( __SCREAMING_SNAKE_CASE , ( { 'example_id': tf.intaa, 'input_ids': tf.intaa, 'attention_mask': tf.intaa, 'token_type_ids': tf.intaa, }, tf.intaa, ) , ( { 'example_id': tf.TensorShape([]), 'input_ids': tf.TensorShape([None, None]), 'attention_mask': tf.TensorShape([None, None]), 'token_type_ids': tf.TensorShape([None, None]), }, tf.TensorShape([]), ) , ) def lowerCAmelCase__ ( self) -> Optional[Any]: return self.dataset def __len__( self) -> Tuple: return len(self.features) def __getitem__( self , UpperCamelCase) -> Union[str, Any]: return self.features[i] def lowerCAmelCase__ ( self) -> Any: return self.label_list class UpperCamelCase_ ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' def lowerCAmelCase__ ( self , UpperCamelCase) -> Union[str, Any]: return self._create_examples(self._read_tsv(os.path.join(__SCREAMING_SNAKE_CASE , 'heuristics_train_set.txt')) , 'train') def lowerCAmelCase__ ( self , UpperCamelCase) -> Any: return self._create_examples(self._read_tsv(os.path.join(__SCREAMING_SNAKE_CASE , 'heuristics_evaluation_set.txt')) , 'dev') def lowerCAmelCase__ ( self) -> List[str]: return ["contradiction", "entailment", "neutral"] def lowerCAmelCase__ ( self , UpperCamelCase , UpperCamelCase) -> Optional[Any]: UpperCamelCase__ : Optional[int] = [] for i, line in enumerate(__SCREAMING_SNAKE_CASE): if i == 0: continue UpperCamelCase__ : int = """%s-%s""" % (set_type, line[0]) UpperCamelCase__ : Optional[Any] = line[5] UpperCamelCase__ : int = line[6] UpperCamelCase__ : Union[str, Any] = line[7][2:] if line[7].startswith('ex') else line[7] UpperCamelCase__ : Optional[Any] = line[0] examples.append(InputExample(guid=__SCREAMING_SNAKE_CASE , text_a=__SCREAMING_SNAKE_CASE , text_b=__SCREAMING_SNAKE_CASE , label=__SCREAMING_SNAKE_CASE , pairID=__SCREAMING_SNAKE_CASE)) return examples def _lowercase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , ) -> Optional[Any]: UpperCamelCase__ : Optional[int] = {label: i for i, label in enumerate(__SCREAMING_SNAKE_CASE )} UpperCamelCase__ : Dict = [] for ex_index, example in tqdm.tqdm(enumerate(__SCREAMING_SNAKE_CASE ) , desc='convert examples to features' ): if ex_index % 1_0000 == 0: logger.info('Writing example %d' % (ex_index) ) UpperCamelCase__ : Any = tokenizer( example.text_a , example.text_b , add_special_tokens=__SCREAMING_SNAKE_CASE , max_length=__SCREAMING_SNAKE_CASE , padding='max_length' , truncation=__SCREAMING_SNAKE_CASE , return_overflowing_tokens=__SCREAMING_SNAKE_CASE , ) UpperCamelCase__ : Tuple = label_map[example.label] if example.label in label_map else 0 UpperCamelCase__ : Tuple = int(example.pairID ) features.append(InputFeatures(**__SCREAMING_SNAKE_CASE , label=__SCREAMING_SNAKE_CASE , pairID=__SCREAMING_SNAKE_CASE ) ) for i, example in enumerate(examples[:5] ): logger.info('*** Example ***' ) logger.info(F"""guid: {example}""" ) logger.info(F"""features: {features[i]}""" ) return features UpperCAmelCase__ : int = { "hans": 3, } UpperCAmelCase__ : Tuple = { "hans": HansProcessor, }
410
'''simple docstring''' import warnings from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging A_ : Optional[int] = logging.get_logger(__name__) A_ : Tuple = { "nvidia/segformer-b0-finetuned-ade-512-512": ( "https://huggingface.co/nvidia/segformer-b0-finetuned-ade-512-512/resolve/main/config.json" ), # See all SegFormer models at https://huggingface.co/models?filter=segformer } class __snake_case ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' lowerCamelCase__ = '''segformer''' def __init__( self , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=4 , __SCREAMING_SNAKE_CASE=[2, 2, 2, 2] , __SCREAMING_SNAKE_CASE=[8, 4, 2, 1] , __SCREAMING_SNAKE_CASE=[3_2, 6_4, 1_6_0, 2_5_6] , __SCREAMING_SNAKE_CASE=[7, 3, 3, 3] , __SCREAMING_SNAKE_CASE=[4, 2, 2, 2] , __SCREAMING_SNAKE_CASE=[1, 2, 5, 8] , __SCREAMING_SNAKE_CASE=[4, 4, 4, 4] , __SCREAMING_SNAKE_CASE="gelu" , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=0.02 , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=1e-6 , __SCREAMING_SNAKE_CASE=2_5_6 , __SCREAMING_SNAKE_CASE=2_5_5 , **__SCREAMING_SNAKE_CASE , ): super().__init__(**__SCREAMING_SNAKE_CASE ) if "reshape_last_stage" in kwargs and kwargs["reshape_last_stage"] is False: warnings.warn( """Reshape_last_stage is set to False in this config. This argument is deprecated and will soon be""" """ removed, as the behaviour will default to that of reshape_last_stage = True.""" , __SCREAMING_SNAKE_CASE , ) snake_case__ : Dict = num_channels snake_case__ : Optional[Any] = num_encoder_blocks snake_case__ : Any = depths snake_case__ : Optional[int] = sr_ratios snake_case__ : Tuple = hidden_sizes snake_case__ : List[str] = patch_sizes snake_case__ : str = strides snake_case__ : Optional[int] = mlp_ratios snake_case__ : Optional[Any] = num_attention_heads snake_case__ : Dict = hidden_act snake_case__ : Optional[int] = hidden_dropout_prob snake_case__ : List[str] = attention_probs_dropout_prob snake_case__ : List[Any] = classifier_dropout_prob snake_case__ : int = initializer_range snake_case__ : List[str] = drop_path_rate snake_case__ : int = layer_norm_eps snake_case__ : List[Any] = decoder_hidden_size snake_case__ : List[Any] = kwargs.get("""reshape_last_stage""" , __SCREAMING_SNAKE_CASE ) snake_case__ : Dict = semantic_loss_ignore_index class __snake_case ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' lowerCamelCase__ = version.parse('''1.11''' ) @property def __UpperCamelCase ( self ): return OrderedDict( [ ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}), ] ) @property def __UpperCamelCase ( self ): return 1e-4 @property def __UpperCamelCase ( self ): return 1_2
38
0
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging SCREAMING_SNAKE_CASE : Tuple = logging.get_logger(__name__) SCREAMING_SNAKE_CASE : List[str] = { "facebook/xlm-roberta-xl": "https://huggingface.co/facebook/xlm-roberta-xl/resolve/main/config.json", "facebook/xlm-roberta-xxl": "https://huggingface.co/facebook/xlm-roberta-xxl/resolve/main/config.json", # See all XLM-RoBERTa-XL models at https://huggingface.co/models?filter=xlm-roberta-xl } class A_ ( __SCREAMING_SNAKE_CASE ): _SCREAMING_SNAKE_CASE = """xlm-roberta-xl""" def __init__( self : Tuple , __SCREAMING_SNAKE_CASE : Tuple=25_08_80 , __SCREAMING_SNAKE_CASE : Optional[int]=25_60 , __SCREAMING_SNAKE_CASE : str=36 , __SCREAMING_SNAKE_CASE : List[Any]=32 , __SCREAMING_SNAKE_CASE : Dict=1_02_40 , __SCREAMING_SNAKE_CASE : Optional[Any]="gelu" , __SCREAMING_SNAKE_CASE : List[Any]=0.1 , __SCREAMING_SNAKE_CASE : Tuple=0.1 , __SCREAMING_SNAKE_CASE : int=5_14 , __SCREAMING_SNAKE_CASE : Optional[Any]=1 , __SCREAMING_SNAKE_CASE : List[str]=0.02 , __SCREAMING_SNAKE_CASE : List[Any]=1E-05 , __SCREAMING_SNAKE_CASE : Tuple=1 , __SCREAMING_SNAKE_CASE : int=0 , __SCREAMING_SNAKE_CASE : List[str]=2 , __SCREAMING_SNAKE_CASE : str="absolute" , __SCREAMING_SNAKE_CASE : str=True , __SCREAMING_SNAKE_CASE : str=None , **__SCREAMING_SNAKE_CASE : List[str] , ): super().__init__(pad_token_id=__SCREAMING_SNAKE_CASE , bos_token_id=__SCREAMING_SNAKE_CASE , eos_token_id=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) __a = vocab_size __a = hidden_size __a = num_hidden_layers __a = num_attention_heads __a = hidden_act __a = intermediate_size __a = hidden_dropout_prob __a = attention_probs_dropout_prob __a = max_position_embeddings __a = type_vocab_size __a = initializer_range __a = layer_norm_eps __a = position_embedding_type __a = use_cache __a = classifier_dropout class A_ ( __SCREAMING_SNAKE_CASE ): @property def _UpperCAmelCase ( self : Any ): if self.task == "multiple-choice": __a = {0: """batch""", 1: """choice""", 2: """sequence"""} else: __a = {0: """batch""", 1: """sequence"""} return OrderedDict( [ ("input_ids", dynamic_axis), ("attention_mask", dynamic_axis), ] )
197
'''simple docstring''' import argparse import json import math import os import time import traceback import zipfile from collections import Counter import requests def UpperCamelCase__ ( __magic_name__ : str , __magic_name__ : List[Any]=None ) -> Union[str, Any]: '''simple docstring''' snake_case__ : str = None if token is not None: snake_case__ : str = {"""Accept""": """application/vnd.github+json""", """Authorization""": f"Bearer {token}"} snake_case__ : List[Any] = f"https://api.github.com/repos/huggingface/transformers/actions/runs/{workflow_run_id}/jobs?per_page=100" snake_case__ : str = requests.get(__magic_name__ , headers=__magic_name__ ).json() snake_case__ : str = {} try: job_links.update({job["""name"""]: job["""html_url"""] for job in result["""jobs"""]} ) snake_case__ : List[Any] = math.ceil((result["""total_count"""] - 1_00) / 1_00 ) for i in range(__magic_name__ ): snake_case__ : Tuple = requests.get(url + f"&page={i + 2}" , headers=__magic_name__ ).json() job_links.update({job["""name"""]: job["""html_url"""] for job in result["""jobs"""]} ) return job_links except Exception: print(f"Unknown error, could not fetch links:\n{traceback.format_exc()}" ) return {} def UpperCamelCase__ ( __magic_name__ : Optional[int] , __magic_name__ : Optional[Any]=None ) -> List[str]: '''simple docstring''' snake_case__ : Optional[Any] = None if token is not None: snake_case__ : Any = {"""Accept""": """application/vnd.github+json""", """Authorization""": f"Bearer {token}"} snake_case__ : Dict = f"https://api.github.com/repos/huggingface/transformers/actions/runs/{worflow_run_id}/artifacts?per_page=100" snake_case__ : Union[str, Any] = requests.get(__magic_name__ , headers=__magic_name__ ).json() snake_case__ : Dict = {} try: artifacts.update({artifact["""name"""]: artifact["""archive_download_url"""] for artifact in result["""artifacts"""]} ) snake_case__ : List[Any] = math.ceil((result["""total_count"""] - 1_00) / 1_00 ) for i in range(__magic_name__ ): snake_case__ : Dict = requests.get(url + f"&page={i + 2}" , headers=__magic_name__ ).json() artifacts.update({artifact["""name"""]: artifact["""archive_download_url"""] for artifact in result["""artifacts"""]} ) return artifacts except Exception: print(f"Unknown error, could not fetch links:\n{traceback.format_exc()}" ) return {} def UpperCamelCase__ ( __magic_name__ : Optional[int] , __magic_name__ : Optional[Any] , __magic_name__ : Optional[int] , __magic_name__ : Dict ) -> Dict: '''simple docstring''' snake_case__ : Optional[Any] = None if token is not None: snake_case__ : Dict = {"""Accept""": """application/vnd.github+json""", """Authorization""": f"Bearer {token}"} snake_case__ : str = requests.get(__magic_name__ , headers=__magic_name__ , allow_redirects=__magic_name__ ) snake_case__ : Any = result.headers["""Location"""] snake_case__ : Tuple = requests.get(__magic_name__ , allow_redirects=__magic_name__ ) snake_case__ : int = os.path.join(__magic_name__ , f"{artifact_name}.zip" ) with open(__magic_name__ , """wb""" ) as fp: fp.write(response.content ) def UpperCamelCase__ ( __magic_name__ : List[Any] , __magic_name__ : str=None ) -> Union[str, Any]: '''simple docstring''' snake_case__ : Any = [] snake_case__ : Union[str, Any] = [] snake_case__ : Any = None with zipfile.ZipFile(__magic_name__ ) as z: for filename in z.namelist(): if not os.path.isdir(__magic_name__ ): # read the file if filename in ["failures_line.txt", "summary_short.txt", "job_name.txt"]: with z.open(__magic_name__ ) as f: for line in f: snake_case__ : Any = line.decode("""UTF-8""" ).strip() if filename == "failures_line.txt": try: # `error_line` is the place where `error` occurs snake_case__ : str = line[: line.index(""": """ )] snake_case__ : Optional[int] = line[line.index(""": """ ) + len(""": """ ) :] errors.append([error_line, error] ) except Exception: # skip un-related lines pass elif filename == "summary_short.txt" and line.startswith("""FAILED """ ): # `test` is the test method that failed snake_case__ : Dict = line[len("""FAILED """ ) :] failed_tests.append(__magic_name__ ) elif filename == "job_name.txt": snake_case__ : Optional[Any] = line if len(__magic_name__ ) != len(__magic_name__ ): raise ValueError( f"`errors` and `failed_tests` should have the same number of elements. Got {len(__magic_name__ )} for `errors` " f"and {len(__magic_name__ )} for `failed_tests` instead. The test reports in {artifact_zip_path} have some" """ problem.""" ) snake_case__ : Optional[Any] = None if job_name and job_links: snake_case__ : Optional[Any] = job_links.get(__magic_name__ , __magic_name__ ) # A list with elements of the form (line of error, error, failed test) snake_case__ : List[Any] = [x + [y] + [job_link] for x, y in zip(__magic_name__ , __magic_name__ )] return result def UpperCamelCase__ ( __magic_name__ : int , __magic_name__ : Union[str, Any]=None ) -> Union[str, Any]: '''simple docstring''' snake_case__ : str = [] snake_case__ : Dict = [os.path.join(__magic_name__ , __magic_name__ ) for p in os.listdir(__magic_name__ ) if p.endswith(""".zip""" )] for p in paths: errors.extend(get_errors_from_single_artifact(__magic_name__ , job_links=__magic_name__ ) ) return errors def UpperCamelCase__ ( __magic_name__ : Optional[Any] , __magic_name__ : str=None ) -> List[Any]: '''simple docstring''' snake_case__ : Any = Counter() counter.update([x[1] for x in logs] ) snake_case__ : Dict = counter.most_common() snake_case__ : Any = {} for error, count in counts: if error_filter is None or error not in error_filter: snake_case__ : int = {"""count""": count, """failed_tests""": [(x[2], x[0]) for x in logs if x[1] == error]} snake_case__ : Union[str, Any] = dict(sorted(r.items() , key=lambda __magic_name__ : item[1]["count"] , reverse=__magic_name__ ) ) return r def UpperCamelCase__ ( __magic_name__ : List[Any] ) -> List[Any]: '''simple docstring''' snake_case__ : str = test.split("""::""" )[0] if test.startswith("""tests/models/""" ): snake_case__ : Tuple = test.split("""/""" )[2] else: snake_case__ : Any = None return test def UpperCamelCase__ ( __magic_name__ : str , __magic_name__ : Union[str, Any]=None ) -> List[str]: '''simple docstring''' snake_case__ : List[str] = [(x[0], x[1], get_model(x[2] )) for x in logs] snake_case__ : List[Any] = [x for x in logs if x[2] is not None] snake_case__ : Any = {x[2] for x in logs} snake_case__ : Optional[Any] = {} for test in tests: snake_case__ : str = Counter() # count by errors in `test` counter.update([x[1] for x in logs if x[2] == test] ) snake_case__ : Optional[int] = counter.most_common() snake_case__ : Optional[int] = {error: count for error, count in counts if (error_filter is None or error not in error_filter)} snake_case__ : int = sum(error_counts.values() ) if n_errors > 0: snake_case__ : str = {"""count""": n_errors, """errors""": error_counts} snake_case__ : Union[str, Any] = dict(sorted(r.items() , key=lambda __magic_name__ : item[1]["count"] , reverse=__magic_name__ ) ) return r def UpperCamelCase__ ( __magic_name__ : int ) -> Optional[int]: '''simple docstring''' snake_case__ : Optional[Any] = """| no. | error | status |""" snake_case__ : int = """|-:|:-|:-|""" snake_case__ : int = [header, sep] for error in reduced_by_error: snake_case__ : Union[str, Any] = reduced_by_error[error]["""count"""] snake_case__ : Dict = f"| {count} | {error[:1_00]} | |" lines.append(__magic_name__ ) return "\n".join(__magic_name__ ) def UpperCamelCase__ ( __magic_name__ : Dict ) -> List[Any]: '''simple docstring''' snake_case__ : List[Any] = """| model | no. of errors | major error | count |""" snake_case__ : Optional[int] = """|-:|-:|-:|-:|""" snake_case__ : Dict = [header, sep] for model in reduced_by_model: snake_case__ : Tuple = reduced_by_model[model]["""count"""] snake_case__ , snake_case__ : Tuple = list(reduced_by_model[model]["""errors"""].items() )[0] snake_case__ : Optional[int] = f"| {model} | {count} | {error[:60]} | {_count} |" lines.append(__magic_name__ ) return "\n".join(__magic_name__ ) if __name__ == "__main__": A_ : Any = argparse.ArgumentParser() # Required parameters parser.add_argument("--workflow_run_id", type=str, required=True, help="A GitHub Actions workflow run id.") parser.add_argument( "--output_dir", type=str, required=True, help="Where to store the downloaded artifacts and other result files.", ) parser.add_argument("--token", default=None, type=str, help="A token that has actions:read permission.") A_ : int = parser.parse_args() os.makedirs(args.output_dir, exist_ok=True) A_ : Optional[int] = get_job_links(args.workflow_run_id, token=args.token) A_ : Optional[Any] = {} # To deal with `workflow_call` event, where a job name is the combination of the job names in the caller and callee. # For example, `PyTorch 1.11 / Model tests (models/albert, single-gpu)`. if _job_links: for k, v in _job_links.items(): # This is how GitHub actions combine job names. if " / " in k: A_ : int = k.find(" / ") A_ : List[Any] = k[index + len(" / ") :] A_ : List[str] = v with open(os.path.join(args.output_dir, "job_links.json"), "w", encoding="UTF-8") as fp: json.dump(job_links, fp, ensure_ascii=False, indent=4) A_ : int = get_artifacts_links(args.workflow_run_id, token=args.token) with open(os.path.join(args.output_dir, "artifacts.json"), "w", encoding="UTF-8") as fp: json.dump(artifacts, fp, ensure_ascii=False, indent=4) for idx, (name, url) in enumerate(artifacts.items()): download_artifact(name, url, args.output_dir, args.token) # Be gentle to GitHub time.sleep(1) A_ : str = get_all_errors(args.output_dir, job_links=job_links) # `e[1]` is the error A_ : List[str] = Counter() counter.update([e[1] for e in errors]) # print the top 30 most common test errors A_ : Any = counter.most_common(30) for item in most_common: print(item) with open(os.path.join(args.output_dir, "errors.json"), "w", encoding="UTF-8") as fp: json.dump(errors, fp, ensure_ascii=False, indent=4) A_ : Any = reduce_by_error(errors) A_ : Union[str, Any] = reduce_by_model(errors) A_ : Any = make_github_table(reduced_by_error) A_ : Optional[Any] = make_github_table_per_model(reduced_by_model) with open(os.path.join(args.output_dir, "reduced_by_error.txt"), "w", encoding="UTF-8") as fp: fp.write(sa) with open(os.path.join(args.output_dir, "reduced_by_model.txt"), "w", encoding="UTF-8") as fp: fp.write(sa)
38
0
from dataclasses import dataclass, field from typing import TYPE_CHECKING, Any, ClassVar, Dict, List, Optional, Union import pyarrow as pa if TYPE_CHECKING: from .features import FeatureType @dataclass class _UpperCAmelCase : '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = 42 SCREAMING_SNAKE_CASE : Optional[Any] = None # Automatically constructed SCREAMING_SNAKE_CASE : Optional[Any] = '''dict''' SCREAMING_SNAKE_CASE : Tuple = None SCREAMING_SNAKE_CASE : List[str] = field(default='''Translation''' , init=__SCREAMING_SNAKE_CASE , repr=__SCREAMING_SNAKE_CASE ) def __call__( self : Tuple ): return pa.struct({lang: pa.string() for lang in sorted(self.languages )} ) def UpperCamelCase ( self : Optional[Any] ): from .features import Value return {k: Value('string' ) for k in sorted(self.languages )} @dataclass class _UpperCAmelCase : '''simple docstring''' SCREAMING_SNAKE_CASE : Any = None SCREAMING_SNAKE_CASE : Optional[int] = None SCREAMING_SNAKE_CASE : Union[str, Any] = None # Automatically constructed SCREAMING_SNAKE_CASE : str = '''dict''' SCREAMING_SNAKE_CASE : List[Any] = None SCREAMING_SNAKE_CASE : Tuple = field(default='''TranslationVariableLanguages''' , init=__SCREAMING_SNAKE_CASE , repr=__SCREAMING_SNAKE_CASE ) def UpperCamelCase ( self : List[str] ): A = sorted(set(self.languages ) ) if self.languages else None A = len(self.languages ) if self.languages else None def __call__( self : Dict ): return pa.struct({'language': pa.list_(pa.string() ), 'translation': pa.list_(pa.string() )} ) def UpperCamelCase ( self : Optional[Any] , UpperCamelCase__ : Optional[Any] ): A = set(self.languages ) if self.languages and set(__SCREAMING_SNAKE_CASE ) - lang_set: raise ValueError( f'''Some languages in example ({", ".join(sorted(set(__SCREAMING_SNAKE_CASE ) - lang_set ) )}) are not in valid set ({", ".join(__SCREAMING_SNAKE_CASE )}).''' ) # Convert dictionary into tuples, splitting out cases where there are # multiple translations for a single language. A = [] for lang, text in translation_dict.items(): if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): translation_tuples.append((lang, text) ) else: translation_tuples.extend([(lang, el) for el in text] ) # Ensure translations are in ascending order by language code. A = zip(*sorted(__SCREAMING_SNAKE_CASE ) ) return {"language": languages, "translation": translations} def UpperCamelCase ( self : Tuple ): from .features import Sequence, Value return { "language": Sequence(Value('string' ) ), "translation": Sequence(Value('string' ) ), }
699
'''simple docstring''' # Lint as: python3 import os import re import urllib.parse from pathlib import Path from typing import Callable, List, Optional, Union from zipfile import ZipFile from ..utils.file_utils import cached_path, hf_github_url from ..utils.logging import get_logger from ..utils.version import Version A_ : Tuple = get_logger(__name__) class __snake_case : '''simple docstring''' lowerCamelCase__ = '''dummy_data''' lowerCamelCase__ = '''datasets''' lowerCamelCase__ = False def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = False , __SCREAMING_SNAKE_CASE = True , __SCREAMING_SNAKE_CASE = None , ): snake_case__ : List[Any] = 0 snake_case__ : Union[str, Any] = dataset_name snake_case__ : Optional[int] = cache_dir snake_case__ : Union[str, Any] = use_local_dummy_data snake_case__ : int = config # download_callbacks take a single url as input snake_case__ : List[Callable] = download_callbacks or [] # if False, it doesn't load existing files and it returns the paths of the dummy files relative # to the dummy_data zip file root snake_case__ : Union[str, Any] = load_existing_dummy_data # TODO(PVP, QL) might need to make this more general snake_case__ : Union[str, Any] = str(__SCREAMING_SNAKE_CASE ) # to be downloaded snake_case__ : List[str] = None snake_case__ : List[str] = None @property def __UpperCamelCase ( self ): if self._dummy_file is None: snake_case__ : List[str] = self.download_dummy_data() return self._dummy_file @property def __UpperCamelCase ( self ): if self.config is not None: # structure is dummy / config_name / version_name return os.path.join("""dummy""" , self.config.name , self.version_name ) # structure is dummy / version_name return os.path.join("""dummy""" , self.version_name ) @property def __UpperCamelCase ( self ): return os.path.join(self.dummy_data_folder , """dummy_data.zip""" ) def __UpperCamelCase ( self ): snake_case__ : Optional[Any] = ( self.local_path_to_dummy_data if self.use_local_dummy_data is True else self.github_path_to_dummy_data ) snake_case__ : Optional[int] = cached_path( __SCREAMING_SNAKE_CASE , cache_dir=self.cache_dir , extract_compressed_file=__SCREAMING_SNAKE_CASE , force_extract=__SCREAMING_SNAKE_CASE ) return os.path.join(__SCREAMING_SNAKE_CASE , self.dummy_file_name ) @property def __UpperCamelCase ( self ): return os.path.join(self.datasets_scripts_dir , self.dataset_name , self.dummy_zip_file ) @property def __UpperCamelCase ( self ): if self._bucket_url is None: snake_case__ : List[str] = hf_github_url(self.dataset_name , self.dummy_zip_file.replace(os.sep , """/""" ) ) return self._bucket_url @property def __UpperCamelCase ( self ): # return full path if its a dir if os.path.isdir(self.dummy_file ): return self.dummy_file # else cut off path to file -> example `xsum`. return "/".join(self.dummy_file.replace(os.sep , """/""" ).split("""/""" )[:-1] ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , *__SCREAMING_SNAKE_CASE ): if self.load_existing_dummy_data: # dummy data is downloaded and tested snake_case__ : List[Any] = self.dummy_file else: # dummy data cannot be downloaded and only the path to dummy file is returned snake_case__ : List[Any] = self.dummy_file_name # special case when data_url is a dict if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): return self.create_dummy_data_dict(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) elif isinstance(__SCREAMING_SNAKE_CASE , (list, tuple) ): return self.create_dummy_data_list(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else: return self.create_dummy_data_single(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , *__SCREAMING_SNAKE_CASE ): return self.download_and_extract(__SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): return self.download_and_extract(__SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): return path def __UpperCamelCase ( self ): return {} def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): snake_case__ : int = {} for key, single_urls in data_url.items(): for download_callback in self.download_callbacks: if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): for single_url in single_urls: download_callback(__SCREAMING_SNAKE_CASE ) else: snake_case__ : List[str] = single_urls download_callback(__SCREAMING_SNAKE_CASE ) # we force the name of each key to be the last file / folder name of the url path # if the url has arguments, we need to encode them with urllib.parse.quote_plus if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): snake_case__ : Tuple = [os.path.join(__SCREAMING_SNAKE_CASE , urllib.parse.quote_plus(Path(__SCREAMING_SNAKE_CASE ).name ) ) for x in single_urls] else: snake_case__ : List[Any] = single_urls snake_case__ : Tuple = os.path.join(__SCREAMING_SNAKE_CASE , urllib.parse.quote_plus(Path(__SCREAMING_SNAKE_CASE ).name ) ) snake_case__ : Optional[int] = value # make sure that values are unique if all(isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) for i in dummy_data_dict.values() ) and len(set(dummy_data_dict.values() ) ) < len( dummy_data_dict.values() ): # append key to value to make its name unique snake_case__ : List[Any] = {key: value + key for key, value in dummy_data_dict.items()} return dummy_data_dict def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): snake_case__ : Dict = [] # trick: if there are many shards named like `data.txt-000001-of-00300`, only use the first one snake_case__ : Tuple = all(bool(re.findall("""[0-9]{3,}-of-[0-9]{3,}""" , __SCREAMING_SNAKE_CASE ) ) for url in data_url ) snake_case__ : List[Any] = all( url.startswith("""https://ftp.ncbi.nlm.nih.gov/pubmed/baseline/pubmed""" ) for url in data_url ) if data_url and (is_tf_records or is_pubmed_records): snake_case__ : List[str] = [data_url[0]] * len(__SCREAMING_SNAKE_CASE ) for single_url in data_url: for download_callback in self.download_callbacks: download_callback(__SCREAMING_SNAKE_CASE ) # we force the name of each key to be the last file / folder name of the url path # if the url has arguments, we need to encode them with urllib.parse.quote_plus snake_case__ : List[Any] = os.path.join(__SCREAMING_SNAKE_CASE , urllib.parse.quote_plus(single_url.split("""/""" )[-1] ) ) dummy_data_list.append(__SCREAMING_SNAKE_CASE ) return dummy_data_list def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): for download_callback in self.download_callbacks: download_callback(__SCREAMING_SNAKE_CASE ) # we force the name of each key to be the last file / folder name of the url path # if the url has arguments, we need to encode them with urllib.parse.quote_plus snake_case__ : Any = os.path.join(__SCREAMING_SNAKE_CASE , urllib.parse.quote_plus(data_url.split("""/""" )[-1] ) ) if os.path.exists(__SCREAMING_SNAKE_CASE ) or not self.load_existing_dummy_data: return value else: # Backward compatibility, maybe deprecate at one point. # For many datasets with single url calls to dl_manager.download_and_extract, # the dummy_data.zip file is actually the zipped downloaded file # while now we expected the dummy_data.zip file to be a directory containing # the downloaded file. return path_to_dummy_data def __UpperCamelCase ( self ): pass def __UpperCamelCase ( self ): pass def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE ): def _iter_archive_members(__SCREAMING_SNAKE_CASE ): # this preserves the order of the members inside the ZIP archive snake_case__ : List[str] = Path(self.dummy_file ).parent snake_case__ : Dict = path.relative_to(__SCREAMING_SNAKE_CASE ) with ZipFile(self.local_path_to_dummy_data ) as zip_file: snake_case__ : Optional[int] = zip_file.namelist() for member in members: if member.startswith(relative_path.as_posix() ): yield dummy_parent_path.joinpath(__SCREAMING_SNAKE_CASE ) snake_case__ : Any = Path(__SCREAMING_SNAKE_CASE ) snake_case__ : int = _iter_archive_members(__SCREAMING_SNAKE_CASE ) if self.use_local_dummy_data else path.rglob("""*""" ) for file_path in file_paths: if file_path.is_file() and not file_path.name.startswith((""".""", """__""") ): yield file_path.relative_to(__SCREAMING_SNAKE_CASE ).as_posix(), file_path.open("""rb""" ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE ): if not isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): snake_case__ : Optional[int] = [paths] for path in paths: if os.path.isfile(__SCREAMING_SNAKE_CASE ): if os.path.basename(__SCREAMING_SNAKE_CASE ).startswith((""".""", """__""") ): return yield path else: for dirpath, dirnames, filenames in os.walk(__SCREAMING_SNAKE_CASE ): if os.path.basename(__SCREAMING_SNAKE_CASE ).startswith((""".""", """__""") ): continue dirnames.sort() for filename in sorted(__SCREAMING_SNAKE_CASE ): if filename.startswith((""".""", """__""") ): continue yield os.path.join(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE )
38
0
import argparse import json import os import re import torch from transformers import BloomConfig, BloomModel from transformers.file_utils import CONFIG_NAME, WEIGHTS_NAME from transformers.utils import logging logging.set_verbosity_info() snake_case__ : Optional[int] = [ "word_embeddings_layernorm.weight", "word_embeddings_layernorm.bias", "input_layernorm.weight", "input_layernorm.bias", "post_attention_layernorm.weight", "post_attention_layernorm.bias", "self_attention.dense.bias", "mlp.dense_4h_to_h.bias", "ln_f.weight", "ln_f.bias", ] snake_case__ : Optional[int] = [ "mlp.dense_4h_to_h.weight", "self_attention.dense.weight", ] def snake_case_ ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): __lowercase = { """word_embeddings.weight""": """word_embeddings.weight""", """word_embeddings.norm.weight""": """word_embeddings_layernorm.weight""", """word_embeddings.norm.bias""": """word_embeddings_layernorm.bias""", """weight""": """ln_f.weight""", """bias""": """ln_f.bias""", } if key in layer_rename_map: return layer_rename_map[key] # Handle transformer blocks __lowercase = int(re.match(R".*layer_(\d*).*" , _SCREAMING_SNAKE_CASE )[1] ) layer_number -= 3 return F"""h.{layer_number}.""" + key def snake_case_ ( _SCREAMING_SNAKE_CASE ): if dtype == torch.bool: return 1 / 8 __lowercase = re.search(R"[^\d](\d+)$" , str(_SCREAMING_SNAKE_CASE ) ) if bit_search is None: raise ValueError(F"""`dtype` is not a valid dtype: {dtype}.""" ) __lowercase = int(bit_search.groups()[0] ) return bit_size // 8 def snake_case_ ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): if bloom_config_file == "": __lowercase = BloomConfig() else: __lowercase = BloomConfig.from_json_file(_SCREAMING_SNAKE_CASE ) if shard_model: __lowercase = os.listdir(_SCREAMING_SNAKE_CASE ) __lowercase = sorted(filter(lambda _SCREAMING_SNAKE_CASE : s.startswith("layer" ) and "model_00" in s , _SCREAMING_SNAKE_CASE ) ) __lowercase = {"""weight_map""": {}, """metadata""": {}} __lowercase = 0 __lowercase = None __lowercase = BloomConfig() for j, file in enumerate(_SCREAMING_SNAKE_CASE ): print("Processing file: {}".format(_SCREAMING_SNAKE_CASE ) ) __lowercase = None for i in range(_SCREAMING_SNAKE_CASE ): # load all TP files __lowercase = file.replace("model_00" , F"""model_0{i}""" ) __lowercase = torch.load(os.path.join(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) , map_location="cpu" ) # Rename keys in the transformers names __lowercase = list(temp.keys() ) for key in keys: __lowercase = temp.pop(_SCREAMING_SNAKE_CASE ) if tensors is None: __lowercase = temp else: for key in tensors.keys(): if any(key.endswith(_SCREAMING_SNAKE_CASE ) for end in WEIGHTS_TO_AVERAGE_ENDSWITH ): # We average (sum and then divide) some weights accross TP ranks (see https://github.com/bigscience-workshop/Megatron-DeepSpeed/blob/olruwase/sync_layer_norms/megatron/training.py#L425) tensors[key] += temp[key] else: # Some weights are RowParallelLinear in Megatron-Deepspeed, others are ColumnParallel __lowercase = 1 if any(text in key for text in WEIGHTS_WITH_ROW_PARALLELISM_CONTAIN ) else 0 # We concatenate these weights accross TP ranks __lowercase = torch.cat([tensors[key], temp[key]] , dim=_SCREAMING_SNAKE_CASE ) # Divide by the number of TP the weights we want to average for key in tensors.keys(): if any(key.endswith(_SCREAMING_SNAKE_CASE ) for end in WEIGHTS_TO_AVERAGE_ENDSWITH ): __lowercase = tensors[key] / pretraining_tp torch.save( _SCREAMING_SNAKE_CASE , os.path.join( _SCREAMING_SNAKE_CASE , "pytorch_model_{}-of-{}.bin".format(str(j + 1 ).zfill(5 ) , str(len(_SCREAMING_SNAKE_CASE ) ).zfill(5 ) ) , ) , ) for key in tensors.keys(): __lowercase = tensors[key] total_size += value.numel() * get_dtype_size(value.dtype ) if key not in index_dict["weight_map"]: __lowercase = """pytorch_model_{}-of-{}.bin""".format( str(j + 1 ).zfill(5 ) , str(len(_SCREAMING_SNAKE_CASE ) ).zfill(5 ) ) __lowercase = BloomConfig() __lowercase = pytorch_dump_folder_path + """/""" + CONFIG_NAME __lowercase = total_size with open(_SCREAMING_SNAKE_CASE , "w" , encoding="utf-8" ) as f: f.write(config.to_json_string() ) with open(os.path.join(_SCREAMING_SNAKE_CASE , WEIGHTS_NAME + ".index.json" ) , "w" , encoding="utf-8" ) as f: __lowercase = json.dumps(_SCREAMING_SNAKE_CASE , indent=2 , sort_keys=_SCREAMING_SNAKE_CASE ) + """\n""" f.write(_SCREAMING_SNAKE_CASE ) else: __lowercase = BloomModel(_SCREAMING_SNAKE_CASE ) __lowercase = os.listdir(_SCREAMING_SNAKE_CASE ) __lowercase = sorted(filter(lambda _SCREAMING_SNAKE_CASE : s.startswith("layer" ) and "model_00" in s , _SCREAMING_SNAKE_CASE ) ) __lowercase = None for i, file in enumerate(_SCREAMING_SNAKE_CASE ): __lowercase = None for i in range(_SCREAMING_SNAKE_CASE ): # load all TP files __lowercase = file.replace("model_00" , F"""model_0{i}""" ) __lowercase = torch.load(os.path.join(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) , map_location="cpu" ) # Rename keys in the transformers names __lowercase = list(temp.keys() ) for key in keys: __lowercase = temp.pop(_SCREAMING_SNAKE_CASE ) if tensors is None: __lowercase = temp else: for key in tensors.keys(): # We average (sum and then divide) some weights accross TP ranks (see https://github.com/bigscience-workshop/Megatron-DeepSpeed/blob/olruwase/sync_layer_norms/megatron/training.py#L425) if any(key.endswith(_SCREAMING_SNAKE_CASE ) for end in WEIGHTS_TO_AVERAGE_ENDSWITH ): tensors[key] += temp[key] else: # Some weights are RowParallelLinear in Megatron-Deepspeed, others are ColumnParallel __lowercase = 1 if any(text in key for text in WEIGHTS_WITH_ROW_PARALLELISM_CONTAIN ) else 0 # We concatenate these weights accross TP ranks __lowercase = torch.cat([tensors[key], temp[key]] , dim=_SCREAMING_SNAKE_CASE ) # Divide by the number of TP the weights we want to average for key in tensors.keys(): if any(key.endswith(_SCREAMING_SNAKE_CASE ) for end in WEIGHTS_TO_AVERAGE_ENDSWITH ): __lowercase = tensors[key] / pretraining_tp __lowercase = model.load_state_dict(_SCREAMING_SNAKE_CASE , strict=_SCREAMING_SNAKE_CASE ) assert not other_keys.unexpected_keys, F"""The keys {other_keys.unexpected_keys} are unexpected""" if missing_keys is None: __lowercase = set(other_keys.missing_keys ) else: __lowercase = missing_keys.intersection(set(other_keys.missing_keys ) ) assert not missing_keys, F"""The keys {missing_keys} are missing""" # Save pytorch-model os.makedirs(_SCREAMING_SNAKE_CASE , exist_ok=_SCREAMING_SNAKE_CASE ) __lowercase = pytorch_dump_folder_path + """/""" + WEIGHTS_NAME __lowercase = pytorch_dump_folder_path + """/""" + CONFIG_NAME print(F"""Save PyTorch model to {pytorch_weights_dump_path} with dtype {config.torch_dtype}""" ) if config.torch_dtype is not None: __lowercase = model.to(config.torch_dtype ) torch.save(model.state_dict() , _SCREAMING_SNAKE_CASE ) print(F"""Save configuration file to {pytorch_config_dump_path}""" ) with open(_SCREAMING_SNAKE_CASE , "w" , encoding="utf-8" ) as f: f.write(config.to_json_string() ) if __name__ == "__main__": snake_case__ : Dict = argparse.ArgumentParser() # Required parameters parser.add_argument( """--bloom_checkpoint_path""", default=None, type=str, required=True, help="""Path to the Megatron-LM checkpoint path.""", ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, required=True, help="""Path to the output PyTorch model.""" ) parser.add_argument( """--bloom_config_file""", default="""""", type=str, help=( """An optional config json file corresponding to the pre-trained model. \n""" """This specifies the model architecture.""" ), ) parser.add_argument( """--shard_model""", action="""store_true""", help="""An optional setting to shard the output model \nThis enables sharding the converted checkpoint""", ) parser.add_argument( """--pretraining_tp""", default=4, type=int, help="""Pretraining TP rank that has been used when training the model in Megatron-LM \n""", ) snake_case__ : Optional[int] = parser.parse_args() convert_bloom_checkpoint_to_pytorch( args.bloom_checkpoint_path, args.bloom_config_file, args.pytorch_dump_folder_path, args.shard_model, args.pretraining_tp, )
402
'''simple docstring''' import random import unittest import torch from diffusers import IFImgaImgSuperResolutionPipeline from diffusers.utils import floats_tensor from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import skip_mps, torch_device from ..pipeline_params import TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS from ..test_pipelines_common import PipelineTesterMixin from . import IFPipelineTesterMixin @skip_mps class __snake_case ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowerCamelCase__ = IFImgaImgSuperResolutionPipeline lowerCamelCase__ = TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {'''width''', '''height'''} lowerCamelCase__ = TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS.union({'''original_image'''} ) lowerCamelCase__ = PipelineTesterMixin.required_optional_params - {'''latents'''} def __UpperCamelCase ( self ): return self._get_superresolution_dummy_components() def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=0 ): if str(__SCREAMING_SNAKE_CASE ).startswith("""mps""" ): snake_case__ : List[Any] = torch.manual_seed(__SCREAMING_SNAKE_CASE ) else: snake_case__ : Tuple = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(__SCREAMING_SNAKE_CASE ) snake_case__ : Tuple = floats_tensor((1, 3, 3_2, 3_2) , rng=random.Random(__SCREAMING_SNAKE_CASE ) ).to(__SCREAMING_SNAKE_CASE ) snake_case__ : Union[str, Any] = floats_tensor((1, 3, 1_6, 1_6) , rng=random.Random(__SCREAMING_SNAKE_CASE ) ).to(__SCREAMING_SNAKE_CASE ) snake_case__ : int = { """prompt""": """A painting of a squirrel eating a burger""", """image""": image, """original_image""": original_image, """generator""": generator, """num_inference_steps""": 2, """output_type""": """numpy""", } return inputs @unittest.skipIf( torch_device != """cuda""" or not is_xformers_available() , reason="""XFormers attention is only available with CUDA and `xformers` installed""" , ) def __UpperCamelCase ( self ): self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1e-3 ) def __UpperCamelCase ( self ): self._test_save_load_optional_components() @unittest.skipIf(torch_device != """cuda""" , reason="""float16 requires CUDA""" ) def __UpperCamelCase ( self ): # Due to non-determinism in save load of the hf-internal-testing/tiny-random-t5 text encoder super().test_save_load_floataa(expected_max_diff=1e-1 ) def __UpperCamelCase ( self ): self._test_attention_slicing_forward_pass(expected_max_diff=1e-2 ) def __UpperCamelCase ( self ): self._test_save_load_local() def __UpperCamelCase ( self ): self._test_inference_batch_single_identical( expected_max_diff=1e-2 , )
38
0
import argparse import pathlib import fairseq import torch from fairseq.models.roberta import RobertaModel as FairseqRobertaModel from fairseq.modules import TransformerSentenceEncoderLayer from packaging import version from transformers import XLMRobertaConfig, XLMRobertaXLForMaskedLM, XLMRobertaXLForSequenceClassification from transformers.models.bert.modeling_bert import ( BertIntermediate, BertLayer, BertOutput, BertSelfAttention, BertSelfOutput, ) from transformers.models.roberta.modeling_roberta import RobertaAttention from transformers.utils import logging if version.parse(fairseq.__version__) < version.parse("1.0.0a"): raise Exception("requires fairseq >= 1.0.0a") logging.set_verbosity_info() A_ = logging.get_logger(__name__) A_ = "Hello world! cécé herlolip" def UpperCAmelCase ( UpperCAmelCase ,UpperCAmelCase ,UpperCAmelCase )-> int: '''simple docstring''' SCREAMING_SNAKE_CASE_ = FairseqRobertaModel.from_pretrained(UpperCAmelCase ) roberta.eval() # disable dropout SCREAMING_SNAKE_CASE_ = roberta.model.encoder.sentence_encoder SCREAMING_SNAKE_CASE_ = XLMRobertaConfig( vocab_size=roberta_sent_encoder.embed_tokens.num_embeddings ,hidden_size=roberta.cfg.model.encoder_embed_dim ,num_hidden_layers=roberta.cfg.model.encoder_layers ,num_attention_heads=roberta.cfg.model.encoder_attention_heads ,intermediate_size=roberta.cfg.model.encoder_ffn_embed_dim ,max_position_embeddings=514 ,type_vocab_size=1 ,layer_norm_eps=1E-5 ,) if classification_head: SCREAMING_SNAKE_CASE_ = roberta.model.classification_heads["""mnli"""].out_proj.weight.shape[0] print('''Our RoBERTa config:''' ,UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = XLMRobertaXLForSequenceClassification(UpperCAmelCase ) if classification_head else XLMRobertaXLForMaskedLM(UpperCAmelCase ) model.eval() # Now let's copy all the weights. # Embeddings SCREAMING_SNAKE_CASE_ = roberta_sent_encoder.embed_tokens.weight SCREAMING_SNAKE_CASE_ = roberta_sent_encoder.embed_positions.weight SCREAMING_SNAKE_CASE_ = torch.zeros_like( model.roberta.embeddings.token_type_embeddings.weight ) # just zero them out b/c RoBERTa doesn't use them. SCREAMING_SNAKE_CASE_ = roberta_sent_encoder.layer_norm.weight SCREAMING_SNAKE_CASE_ = roberta_sent_encoder.layer_norm.bias for i in range(config.num_hidden_layers ): # Encoder: start of layer SCREAMING_SNAKE_CASE_ = model.roberta.encoder.layer[i] SCREAMING_SNAKE_CASE_ = roberta_sent_encoder.layers[i] SCREAMING_SNAKE_CASE_ = layer.attention SCREAMING_SNAKE_CASE_ = roberta_layer.self_attn_layer_norm.weight SCREAMING_SNAKE_CASE_ = roberta_layer.self_attn_layer_norm.bias # self attention SCREAMING_SNAKE_CASE_ = layer.attention.self assert ( roberta_layer.self_attn.k_proj.weight.data.shape == roberta_layer.self_attn.q_proj.weight.data.shape == roberta_layer.self_attn.v_proj.weight.data.shape == torch.Size((config.hidden_size, config.hidden_size) ) ) SCREAMING_SNAKE_CASE_ = roberta_layer.self_attn.q_proj.weight SCREAMING_SNAKE_CASE_ = roberta_layer.self_attn.q_proj.bias SCREAMING_SNAKE_CASE_ = roberta_layer.self_attn.k_proj.weight SCREAMING_SNAKE_CASE_ = roberta_layer.self_attn.k_proj.bias SCREAMING_SNAKE_CASE_ = roberta_layer.self_attn.v_proj.weight SCREAMING_SNAKE_CASE_ = roberta_layer.self_attn.v_proj.bias # self-attention output SCREAMING_SNAKE_CASE_ = layer.attention.output assert self_output.dense.weight.shape == roberta_layer.self_attn.out_proj.weight.shape SCREAMING_SNAKE_CASE_ = roberta_layer.self_attn.out_proj.weight SCREAMING_SNAKE_CASE_ = roberta_layer.self_attn.out_proj.bias # this one is final layer norm SCREAMING_SNAKE_CASE_ = roberta_layer.final_layer_norm.weight SCREAMING_SNAKE_CASE_ = roberta_layer.final_layer_norm.bias # intermediate SCREAMING_SNAKE_CASE_ = layer.intermediate assert intermediate.dense.weight.shape == roberta_layer.fca.weight.shape SCREAMING_SNAKE_CASE_ = roberta_layer.fca.weight SCREAMING_SNAKE_CASE_ = roberta_layer.fca.bias # output SCREAMING_SNAKE_CASE_ = layer.output assert bert_output.dense.weight.shape == roberta_layer.fca.weight.shape SCREAMING_SNAKE_CASE_ = roberta_layer.fca.weight SCREAMING_SNAKE_CASE_ = roberta_layer.fca.bias # end of layer if classification_head: SCREAMING_SNAKE_CASE_ = roberta.model.classification_heads["""mnli"""].dense.weight SCREAMING_SNAKE_CASE_ = roberta.model.classification_heads["""mnli"""].dense.bias SCREAMING_SNAKE_CASE_ = roberta.model.classification_heads["""mnli"""].out_proj.weight SCREAMING_SNAKE_CASE_ = roberta.model.classification_heads["""mnli"""].out_proj.bias else: # LM Head SCREAMING_SNAKE_CASE_ = roberta.model.encoder.lm_head.dense.weight SCREAMING_SNAKE_CASE_ = roberta.model.encoder.lm_head.dense.bias SCREAMING_SNAKE_CASE_ = roberta.model.encoder.lm_head.layer_norm.weight SCREAMING_SNAKE_CASE_ = roberta.model.encoder.lm_head.layer_norm.bias SCREAMING_SNAKE_CASE_ = roberta.model.encoder.lm_head.weight SCREAMING_SNAKE_CASE_ = roberta.model.encoder.lm_head.bias # Let's check that we get the same results. SCREAMING_SNAKE_CASE_ = roberta.encode(UpperCAmelCase ).unsqueeze(0 ) # batch of size 1 SCREAMING_SNAKE_CASE_ = model(UpperCAmelCase )[0] if classification_head: SCREAMING_SNAKE_CASE_ = roberta.model.classification_heads["""mnli"""](roberta.extract_features(UpperCAmelCase ) ) else: SCREAMING_SNAKE_CASE_ = roberta.model(UpperCAmelCase )[0] print(our_output.shape ,their_output.shape ) SCREAMING_SNAKE_CASE_ = torch.max(torch.abs(our_output - their_output ) ).item() print(f'''max_absolute_diff = {max_absolute_diff}''' ) # ~ 1e-7 SCREAMING_SNAKE_CASE_ = torch.allclose(UpperCAmelCase ,UpperCAmelCase ,atol=1E-3 ) print('''Do both models output the same tensors?''' ,'''🔥''' if success else '''💩''' ) if not success: raise Exception('''Something went wRoNg''' ) pathlib.Path(UpperCAmelCase ).mkdir(parents=UpperCAmelCase ,exist_ok=UpperCAmelCase ) print(f'''Saving model to {pytorch_dump_folder_path}''' ) model.save_pretrained(UpperCAmelCase ) if __name__ == "__main__": A_ = argparse.ArgumentParser() # Required parameters parser.add_argument( "--roberta_checkpoint_path", default=None, type=str, required=True, help="Path the official PyTorch dump." ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, required=True, help="Path to the output PyTorch model." ) parser.add_argument( "--classification_head", action="store_true", help="Whether to convert a final classification head." ) A_ = parser.parse_args() convert_xlm_roberta_xl_checkpoint_to_pytorch( args.roberta_checkpoint_path, args.pytorch_dump_folder_path, args.classification_head )
393
'''simple docstring''' # Copyright 2021 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import argparse from ...utils.dataclasses import ( ComputeEnvironment, DistributedType, DynamoBackend, PrecisionType, SageMakerDistributedType, ) from ..menu import BulletMenu A_ : Dict = [ "EAGER", "AOT_EAGER", "INDUCTOR", "NVFUSER", "AOT_NVFUSER", "AOT_CUDAGRAPHS", "OFI", "FX2TRT", "ONNXRT", "IPEX", ] def UpperCamelCase__ ( __magic_name__ : List[Any] , __magic_name__ : List[Any]=None , __magic_name__ : List[str]=None , __magic_name__ : List[str]=None ) -> Dict: '''simple docstring''' snake_case__ : Optional[int] = True while ask_again: snake_case__ : Optional[Any] = input(__magic_name__ ) try: if default is not None and len(__magic_name__ ) == 0: return default return convert_value(__magic_name__ ) if convert_value is not None else result except Exception: if error_message is not None: print(__magic_name__ ) def UpperCamelCase__ ( __magic_name__ : List[str] , __magic_name__ : Any=[] , __magic_name__ : Optional[int]=None , __magic_name__ : int=0 ) -> Optional[int]: '''simple docstring''' snake_case__ : Union[str, Any] = BulletMenu(__magic_name__ , __magic_name__ ) snake_case__ : Optional[Any] = menu.run(default_choice=__magic_name__ ) return convert_value(__magic_name__ ) if convert_value is not None else result def UpperCamelCase__ ( __magic_name__ : Any ) -> int: '''simple docstring''' snake_case__ : Tuple = int(__magic_name__ ) return ComputeEnvironment(["""LOCAL_MACHINE""", """AMAZON_SAGEMAKER"""][value] ) def UpperCamelCase__ ( __magic_name__ : str ) -> Tuple: '''simple docstring''' snake_case__ : List[Any] = int(__magic_name__ ) return DistributedType(["""NO""", """MULTI_CPU""", """MULTI_XPU""", """MULTI_GPU""", """MULTI_NPU""", """TPU"""][value] ) def UpperCamelCase__ ( __magic_name__ : List[str] ) -> List[Any]: '''simple docstring''' snake_case__ : Union[str, Any] = int(__magic_name__ ) return DynamoBackend(DYNAMO_BACKENDS[value] ).value def UpperCamelCase__ ( __magic_name__ : List[str] ) -> Union[str, Any]: '''simple docstring''' snake_case__ : Optional[Any] = int(__magic_name__ ) return PrecisionType(["""no""", """fp16""", """bf16""", """fp8"""][value] ) def UpperCamelCase__ ( __magic_name__ : Optional[int] ) -> List[Any]: '''simple docstring''' snake_case__ : Optional[Any] = int(__magic_name__ ) return SageMakerDistributedType(["""NO""", """DATA_PARALLEL""", """MODEL_PARALLEL"""][value] ) def UpperCamelCase__ ( __magic_name__ : Dict ) -> Tuple: '''simple docstring''' return {"yes": True, "no": False}[value.lower()] class __snake_case ( argparse.RawDescriptionHelpFormatter ): '''simple docstring''' def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): snake_case__ : str = super()._format_usage(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) snake_case__ : str = usage.replace("""<command> [<args>] """ , """""" ) return usage
38
0
"""simple docstring""" from ....configuration_utils import PretrainedConfig from ....utils import logging SCREAMING_SNAKE_CASE__ : Tuple =logging.get_logger(__name__) # TODO: upload to AWS SCREAMING_SNAKE_CASE__ : Tuple ={ "yjernite/retribert-base-uncased": ( "https://huggingface.co/yjernite/retribert-base-uncased/resolve/main/config.json" ), } class _UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): """simple docstring""" __snake_case = """retribert""" def __init__( self , _lowercase=30522 , _lowercase=768 , _lowercase=8 , _lowercase=12 , _lowercase=3072 , _lowercase="gelu" , _lowercase=0.1 , _lowercase=0.1 , _lowercase=512 , _lowercase=2 , _lowercase=0.02 , _lowercase=1E-12 , _lowercase=True , _lowercase=128 , _lowercase=0 , **_lowercase , ) -> Optional[Any]: super().__init__(pad_token_id=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) _lowerCamelCase : str = vocab_size _lowerCamelCase : str = hidden_size _lowerCamelCase : int = num_hidden_layers _lowerCamelCase : List[Any] = num_attention_heads _lowerCamelCase : List[Any] = hidden_act _lowerCamelCase : Optional[Any] = intermediate_size _lowerCamelCase : Union[str, Any] = hidden_dropout_prob _lowerCamelCase : List[str] = attention_probs_dropout_prob _lowerCamelCase : Tuple = max_position_embeddings _lowerCamelCase : Optional[int] = type_vocab_size _lowerCamelCase : str = initializer_range _lowerCamelCase : Any = layer_norm_eps _lowerCamelCase : Dict = share_encoders _lowerCamelCase : str = projection_dim
434
'''simple docstring''' from __future__ import annotations def UpperCamelCase__ ( __magic_name__ : list ) -> float: '''simple docstring''' if not nums: raise ValueError("""List is empty""" ) return sum(__magic_name__ ) / len(__magic_name__ ) if __name__ == "__main__": import doctest doctest.testmod()
38
0
import inspect import unittest from transformers import DPTConfig from transformers.file_utils import is_torch_available, is_vision_available from transformers.models.auto import get_values from transformers.testing_utils import require_torch, require_vision, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import MODEL_MAPPING, DPTForDepthEstimation, DPTForSemanticSegmentation, DPTModel from transformers.models.dpt.modeling_dpt import DPT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import DPTImageProcessor class snake_case_ : def __init__( self :Union[str, Any] ,__snake_case :Union[str, Any] ,__snake_case :str=2 ,__snake_case :int=32 ,__snake_case :List[str]=16 ,__snake_case :int=3 ,__snake_case :Tuple=True ,__snake_case :Union[str, Any]=True ,__snake_case :Union[str, Any]=32 ,__snake_case :List[str]=4 ,__snake_case :List[Any]=[0, 1, 2, 3] ,__snake_case :List[str]=4 ,__snake_case :Tuple=37 ,__snake_case :Optional[Any]="gelu" ,__snake_case :Tuple=0.1 ,__snake_case :Optional[Any]=0.1 ,__snake_case :int=0.02 ,__snake_case :Optional[Any]=3 ,__snake_case :str=[1, 3_84, 24, 24] ,__snake_case :Dict=True ,__snake_case :Optional[Any]=None ,) -> Dict: a__ = parent a__ = batch_size a__ = image_size a__ = patch_size a__ = num_channels a__ = is_training a__ = use_labels a__ = hidden_size a__ = num_hidden_layers a__ = backbone_out_indices a__ = num_attention_heads a__ = intermediate_size a__ = hidden_act a__ = hidden_dropout_prob a__ = attention_probs_dropout_prob a__ = initializer_range a__ = num_labels a__ = backbone_featmap_shape a__ = scope a__ = is_hybrid # sequence length of DPT = num_patches + 1 (we add 1 for the [CLS] token) a__ = (image_size // patch_size) ** 2 a__ = num_patches + 1 def lowerCamelCase__( self :List[Any] ) -> Tuple: a__ = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) a__ = None if self.use_labels: a__ = ids_tensor([self.batch_size, self.image_size, self.image_size] ,self.num_labels ) a__ = self.get_config() return config, pixel_values, labels def lowerCamelCase__( self :Optional[Any] ) -> List[Any]: a__ = { """global_padding""": """same""", """layer_type""": """bottleneck""", """depths""": [3, 4, 9], """out_features""": ["""stage1""", """stage2""", """stage3"""], """embedding_dynamic_padding""": True, """hidden_sizes""": [96, 1_92, 3_84, 7_68], """num_groups""": 2, } return DPTConfig( image_size=self.image_size ,patch_size=self.patch_size ,num_channels=self.num_channels ,hidden_size=self.hidden_size ,num_hidden_layers=self.num_hidden_layers ,backbone_out_indices=self.backbone_out_indices ,num_attention_heads=self.num_attention_heads ,intermediate_size=self.intermediate_size ,hidden_act=self.hidden_act ,hidden_dropout_prob=self.hidden_dropout_prob ,attention_probs_dropout_prob=self.attention_probs_dropout_prob ,is_decoder=__SCREAMING_SNAKE_CASE ,initializer_range=self.initializer_range ,is_hybrid=self.is_hybrid ,backbone_config=__SCREAMING_SNAKE_CASE ,backbone_featmap_shape=self.backbone_featmap_shape ,) def lowerCamelCase__( self :List[str] ,__snake_case :List[Any] ,__snake_case :List[Any] ,__snake_case :Tuple ) -> str: a__ = DPTModel(config=__SCREAMING_SNAKE_CASE ) model.to(__SCREAMING_SNAKE_CASE ) model.eval() a__ = model(__SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.last_hidden_state.shape ,(self.batch_size, self.seq_length, self.hidden_size) ) def lowerCamelCase__( self :Tuple ,__snake_case :Union[str, Any] ,__snake_case :int ,__snake_case :str ) -> Any: a__ = self.num_labels a__ = DPTForDepthEstimation(__SCREAMING_SNAKE_CASE ) model.to(__SCREAMING_SNAKE_CASE ) model.eval() a__ = model(__SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.predicted_depth.shape ,(self.batch_size, self.image_size, self.image_size) ) def lowerCamelCase__( self :str ,__snake_case :Optional[int] ,__snake_case :Optional[int] ,__snake_case :Optional[int] ) -> Any: a__ = self.num_labels a__ = DPTForSemanticSegmentation(__SCREAMING_SNAKE_CASE ) model.to(__SCREAMING_SNAKE_CASE ) model.eval() a__ = model(__SCREAMING_SNAKE_CASE ,labels=__SCREAMING_SNAKE_CASE ) self.parent.assertEqual( result.logits.shape ,(self.batch_size, self.num_labels, self.image_size, self.image_size) ) def lowerCamelCase__( self :Dict ) -> Optional[int]: a__ = self.prepare_config_and_inputs() a__ = config_and_inputs a__ = {"""pixel_values""": pixel_values} return config, inputs_dict @require_torch class snake_case_ (__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): UpperCAmelCase__ : Optional[int] = (DPTModel, DPTForDepthEstimation, DPTForSemanticSegmentation) if is_torch_available() else () UpperCAmelCase__ : Optional[Any] = ( { '''depth-estimation''': DPTForDepthEstimation, '''feature-extraction''': DPTModel, '''image-segmentation''': DPTForSemanticSegmentation, } if is_torch_available() else {} ) UpperCAmelCase__ : Any = False UpperCAmelCase__ : Tuple = False UpperCAmelCase__ : List[Any] = False def lowerCamelCase__( self :Any ) -> Optional[Any]: a__ = DPTModelTester(self ) a__ = ConfigTester(self ,config_class=__SCREAMING_SNAKE_CASE ,has_text_modality=__SCREAMING_SNAKE_CASE ,hidden_size=37 ) def lowerCamelCase__( self :List[Any] ) -> Optional[Any]: self.config_tester.run_common_tests() @unittest.skip(reason='DPT does not use inputs_embeds' ) def lowerCamelCase__( self :int ) -> str: pass def lowerCamelCase__( self :Dict ) -> Any: a__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: a__ = model_class(__SCREAMING_SNAKE_CASE ) self.assertIsInstance(model.get_input_embeddings() ,(nn.Module) ) a__ = model.get_output_embeddings() self.assertTrue(x is None or isinstance(__SCREAMING_SNAKE_CASE ,nn.Linear ) ) def lowerCamelCase__( self :Optional[int] ) -> Tuple: a__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: a__ = model_class(__SCREAMING_SNAKE_CASE ) a__ = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic a__ = [*signature.parameters.keys()] a__ = ["""pixel_values"""] self.assertListEqual(arg_names[:1] ,__SCREAMING_SNAKE_CASE ) def lowerCamelCase__( self :List[Any] ) -> Union[str, Any]: a__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE ) def lowerCamelCase__( self :int ) -> Union[str, Any]: a__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_depth_estimation(*__SCREAMING_SNAKE_CASE ) def lowerCamelCase__( self :List[Any] ) -> str: a__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_semantic_segmentation(*__SCREAMING_SNAKE_CASE ) def lowerCamelCase__( self :Optional[int] ) -> int: for model_class in self.all_model_classes: if model_class.__name__ == "DPTForDepthEstimation": continue a__ = self.model_tester.prepare_config_and_inputs_for_common() a__ = True if model_class in get_values(__SCREAMING_SNAKE_CASE ): continue a__ = model_class(__SCREAMING_SNAKE_CASE ) model.to(__SCREAMING_SNAKE_CASE ) model.train() a__ = self._prepare_for_class(__SCREAMING_SNAKE_CASE ,__SCREAMING_SNAKE_CASE ,return_labels=__SCREAMING_SNAKE_CASE ) a__ = model(**__SCREAMING_SNAKE_CASE ).loss loss.backward() def lowerCamelCase__( self :Any ) -> int: for model_class in self.all_model_classes: if model_class.__name__ == "DPTForDepthEstimation": continue a__ = self.model_tester.prepare_config_and_inputs_for_common() a__ = False a__ = True if model_class in get_values(__SCREAMING_SNAKE_CASE ) or not model_class.supports_gradient_checkpointing: continue a__ = model_class(__SCREAMING_SNAKE_CASE ) model.to(__SCREAMING_SNAKE_CASE ) model.gradient_checkpointing_enable() model.train() a__ = self._prepare_for_class(__SCREAMING_SNAKE_CASE ,__SCREAMING_SNAKE_CASE ,return_labels=__SCREAMING_SNAKE_CASE ) a__ = model(**__SCREAMING_SNAKE_CASE ).loss loss.backward() def lowerCamelCase__( self :int ) -> List[Any]: a__ = self.model_tester.prepare_config_and_inputs_for_common() a__ = _config_zero_init(__SCREAMING_SNAKE_CASE ) for model_class in self.all_model_classes: a__ = model_class(config=__SCREAMING_SNAKE_CASE ) # Skip the check for the backbone a__ = [] for name, module in model.named_modules(): if module.__class__.__name__ == "DPTViTHybridEmbeddings": a__ = [F'{name}.{key}' for key in module.state_dict().keys()] break for name, param in model.named_parameters(): if param.requires_grad: if name in backbone_params: continue self.assertIn( ((param.data.mean() * 1E9).round() / 1E9).item() ,[0.0, 1.0] ,msg=F'Parameter {name} of model {model_class} seems not properly initialized' ,) @unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.' ) def lowerCamelCase__( self :List[Any] ) -> Tuple: pass @slow def lowerCamelCase__( self :Any ) -> Tuple: for model_name in DPT_PRETRAINED_MODEL_ARCHIVE_LIST[1:]: a__ = DPTModel.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsNotNone(__SCREAMING_SNAKE_CASE ) def lowerCamelCase__( self :Dict ) -> List[Any]: # We do this test only for DPTForDepthEstimation since it is the only model that uses readout_type a__ = self.model_tester.prepare_config_and_inputs_for_common() a__ = """add""" with self.assertRaises(__SCREAMING_SNAKE_CASE ): a__ = DPTForDepthEstimation(__SCREAMING_SNAKE_CASE ) def __lowercase ( ): a__ = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_torch @require_vision @slow class snake_case_ (unittest.TestCase ): def lowerCamelCase__( self :Optional[Any] ) -> List[str]: a__ = DPTImageProcessor.from_pretrained('Intel/dpt-hybrid-midas' ) a__ = DPTForDepthEstimation.from_pretrained('Intel/dpt-hybrid-midas' ).to(__SCREAMING_SNAKE_CASE ) a__ = prepare_img() a__ = image_processor(images=__SCREAMING_SNAKE_CASE ,return_tensors='pt' ).to(__SCREAMING_SNAKE_CASE ) # forward pass with torch.no_grad(): a__ = model(**__SCREAMING_SNAKE_CASE ) a__ = outputs.predicted_depth # verify the predicted depth a__ = torch.Size((1, 3_84, 3_84) ) self.assertEqual(predicted_depth.shape ,__SCREAMING_SNAKE_CASE ) a__ = torch.tensor( [[[5.64_37, 5.61_46, 5.65_11], [5.43_71, 5.56_49, 5.59_58], [5.52_15, 5.51_84, 5.52_93]]] ).to(__SCREAMING_SNAKE_CASE ) self.assertTrue(torch.allclose(outputs.predicted_depth[:3, :3, :3] / 1_00 ,__SCREAMING_SNAKE_CASE ,atol=1E-4 ) )
335
'''simple docstring''' from __future__ import annotations A_ : str = "Muhammad Umer Farooq" A_ : Optional[Any] = "MIT" A_ : int = "1.0.0" A_ : int = "Muhammad Umer Farooq" A_ : int = "[email protected]" A_ : Dict = "Alpha" import re from html.parser import HTMLParser from urllib import parse import requests class __snake_case ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE ): super().__init__() snake_case__ : list[str] = [] snake_case__ : List[Any] = domain def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): # Only parse the 'anchor' tag. if tag == "a": # Check the list of defined attributes. for name, value in attrs: # If href is defined, and not empty nor # print it. if name == "href" and value != "#" and value != "": # If not already in urls. if value not in self.urls: snake_case__ : str = parse.urljoin(self.domain , __SCREAMING_SNAKE_CASE ) self.urls.append(__SCREAMING_SNAKE_CASE ) def UpperCamelCase__ ( __magic_name__ : str ) -> str: '''simple docstring''' return ".".join(get_sub_domain_name(__magic_name__ ).split(""".""" )[-2:] ) def UpperCamelCase__ ( __magic_name__ : str ) -> str: '''simple docstring''' return parse.urlparse(__magic_name__ ).netloc def UpperCamelCase__ ( __magic_name__ : str = "https://github.com" ) -> list[str]: '''simple docstring''' snake_case__ : List[str] = get_domain_name(__magic_name__ ) # Initialize the parser snake_case__ : Optional[Any] = Parser(__magic_name__ ) try: # Open URL snake_case__ : Any = requests.get(__magic_name__ ) # pass the raw HTML to the parser to get links parser.feed(r.text ) # Get links and loop through snake_case__ : List[str] = set() for link in parser.urls: # open URL. # read = requests.get(link) try: snake_case__ : Tuple = requests.get(__magic_name__ ) # Get the valid email. snake_case__ : List[str] = re.findall("""[a-zA-Z0-9]+@""" + domain , read.text ) # If not in list then append it. for email in emails: valid_emails.add(__magic_name__ ) except ValueError: pass except ValueError: raise SystemExit(1 ) # Finally return a sorted list of email addresses with no duplicates. return sorted(__magic_name__ ) if __name__ == "__main__": A_ : str = emails_from_url("https://github.com") print(F'{len(emails)} emails found:') print("\n".join(sorted(emails)))
38
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available A__ : Optional[Any] = { "configuration_xmod": [ "XMOD_PRETRAINED_CONFIG_ARCHIVE_MAP", "XmodConfig", "XmodOnnxConfig", ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A__ : str = [ "XMOD_PRETRAINED_MODEL_ARCHIVE_LIST", "XmodForCausalLM", "XmodForMaskedLM", "XmodForMultipleChoice", "XmodForQuestionAnswering", "XmodForSequenceClassification", "XmodForTokenClassification", "XmodModel", "XmodPreTrainedModel", ] if TYPE_CHECKING: from .configuration_xmod import XMOD_PRETRAINED_CONFIG_ARCHIVE_MAP, XmodConfig, XmodOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xmod import ( XMOD_PRETRAINED_MODEL_ARCHIVE_LIST, XmodForCausalLM, XmodForMaskedLM, XmodForMultipleChoice, XmodForQuestionAnswering, XmodForSequenceClassification, XmodForTokenClassification, XmodModel, XmodPreTrainedModel, ) else: import sys A__ : Tuple = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
233
'''simple docstring''' def UpperCamelCase__ ( __magic_name__ : List[Any] ) -> Tuple: '''simple docstring''' if not head: return True # split the list to two parts snake_case__ , snake_case__ : Dict = head.next, head while fast and fast.next: snake_case__ : Any = fast.next.next snake_case__ : int = slow.next snake_case__ : Dict = slow.next snake_case__ : List[str] = None # Don't forget here! But forget still works! # reverse the second part snake_case__ : Tuple = None while second: snake_case__ : Tuple = second.next snake_case__ : Any = node snake_case__ : str = second snake_case__ : Optional[Any] = nxt # compare two parts # second part has the same or one less node while node: if node.val != head.val: return False snake_case__ : List[Any] = node.next snake_case__ : int = head.next return True def UpperCamelCase__ ( __magic_name__ : Any ) -> Optional[Any]: '''simple docstring''' if not head or not head.next: return True # 1. Get the midpoint (slow) snake_case__ : List[Any] = head while fast and fast.next: snake_case__ , snake_case__ : Any = fast.next.next, slow.next # 2. Push the second half into the stack snake_case__ : Tuple = [slow.val] while slow.next: snake_case__ : Optional[Any] = slow.next stack.append(slow.val ) # 3. Comparison while stack: if stack.pop() != cur.val: return False snake_case__ : str = cur.next return True def UpperCamelCase__ ( __magic_name__ : Optional[Any] ) -> Tuple: '''simple docstring''' if not head or not head.next: return True snake_case__ : int = {} snake_case__ : Union[str, Any] = 0 while head: if head.val in d: d[head.val].append(__magic_name__ ) else: snake_case__ : Tuple = [pos] snake_case__ : Optional[Any] = head.next pos += 1 snake_case__ : int = pos - 1 snake_case__ : str = 0 for v in d.values(): if len(__magic_name__ ) % 2 != 0: middle += 1 else: snake_case__ : List[str] = 0 for i in range(0 , len(__magic_name__ ) ): if v[i] + v[len(__magic_name__ ) - 1 - step] != checksum: return False step += 1 if middle > 1: return False return True
38
0
from __future__ import annotations import inspect import unittest from math import floor import numpy as np from transformers import CvtConfig from transformers.testing_utils import require_tf, require_vision, slow from transformers.utils import cached_property, is_tf_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFCvtForImageClassification, TFCvtModel from transformers.models.cvt.modeling_tf_cvt import TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class _UpperCamelCase( __SCREAMING_SNAKE_CASE ): def __lowerCAmelCase ( self : Any ): '''simple docstring''' __a : Dict = self.config_class(**self.inputs_dict ) self.parent.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , 'embed_dim' ) ) self.parent.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , 'num_heads' ) ) class _UpperCamelCase: def __init__( self : List[Any] , SCREAMING_SNAKE_CASE__ : Dict , SCREAMING_SNAKE_CASE__ : int=1_3 , SCREAMING_SNAKE_CASE__ : str=6_4 , SCREAMING_SNAKE_CASE__ : Tuple=3 , SCREAMING_SNAKE_CASE__ : Dict=[1_6, 4_8, 9_6] , SCREAMING_SNAKE_CASE__ : Dict=[1, 3, 6] , SCREAMING_SNAKE_CASE__ : Dict=[1, 2, 1_0] , SCREAMING_SNAKE_CASE__ : List[str]=[7, 3, 3] , SCREAMING_SNAKE_CASE__ : Tuple=[4, 2, 2] , SCREAMING_SNAKE_CASE__ : int=[2, 1, 1] , SCREAMING_SNAKE_CASE__ : List[Any]=[2, 2, 2] , SCREAMING_SNAKE_CASE__ : Dict=[False, False, True] , SCREAMING_SNAKE_CASE__ : List[str]=[0.0, 0.0, 0.0] , SCREAMING_SNAKE_CASE__ : List[Any]=0.02 , SCREAMING_SNAKE_CASE__ : Tuple=1e-12 , SCREAMING_SNAKE_CASE__ : Optional[Any]=True , SCREAMING_SNAKE_CASE__ : Dict=True , SCREAMING_SNAKE_CASE__ : Any=2 , ): '''simple docstring''' __a : List[str] = parent __a : Tuple = batch_size __a : Union[str, Any] = image_size __a : List[Any] = patch_sizes __a : Optional[int] = patch_stride __a : Optional[Any] = patch_padding __a : Any = is_training __a : int = use_labels __a : Dict = num_labels __a : Optional[Any] = num_channels __a : Optional[Any] = embed_dim __a : Optional[int] = num_heads __a : Optional[int] = stride_kv __a : int = depth __a : Optional[Any] = cls_token __a : List[Any] = attention_drop_rate __a : Union[str, Any] = initializer_range __a : List[Any] = layer_norm_eps def __lowerCAmelCase ( self : Union[str, Any] ): '''simple docstring''' __a : Optional[int] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) __a : List[Any] = None if self.use_labels: # create a random int32 tensor of given shape __a : List[str] = ids_tensor([self.batch_size] , self.num_labels ) __a : List[str] = self.get_config() return config, pixel_values, labels def __lowerCAmelCase ( self : Union[str, Any] ): '''simple docstring''' return CvtConfig( image_size=self.image_size , num_labels=self.num_labels , num_channels=self.num_channels , embed_dim=self.embed_dim , num_heads=self.num_heads , patch_sizes=self.patch_sizes , patch_padding=self.patch_padding , patch_stride=self.patch_stride , stride_kv=self.stride_kv , depth=self.depth , cls_token=self.cls_token , attention_drop_rate=self.attention_drop_rate , initializer_range=self.initializer_range , ) def __lowerCAmelCase ( self : Optional[Any] , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : Optional[int] , SCREAMING_SNAKE_CASE__ : List[str] ): '''simple docstring''' __a : int = TFCvtModel(config=__SCREAMING_SNAKE_CASE ) __a : List[Any] = model(__SCREAMING_SNAKE_CASE , training=__SCREAMING_SNAKE_CASE ) __a : Tuple = (self.image_size, self.image_size) __a : str = image_size[0], image_size[1] for i in range(len(self.depth ) ): __a : Any = floor(((height + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1 ) __a : Optional[int] = floor(((width + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1 ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.embed_dim[-1], height, width) ) def __lowerCAmelCase ( self : Optional[Any] , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : Optional[int] , SCREAMING_SNAKE_CASE__ : List[str] ): '''simple docstring''' __a : Any = self.num_labels __a : str = TFCvtForImageClassification(__SCREAMING_SNAKE_CASE ) __a : List[str] = model(__SCREAMING_SNAKE_CASE , labels=__SCREAMING_SNAKE_CASE , training=__SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __lowerCAmelCase ( self : Tuple ): '''simple docstring''' __a : List[Any] = self.prepare_config_and_inputs() __a : Any = config_and_inputs __a : Union[str, Any] = {"""pixel_values""": pixel_values} return config, inputs_dict @require_tf class _UpperCamelCase( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): __SCREAMING_SNAKE_CASE : Union[str, Any] = (TFCvtModel, TFCvtForImageClassification) if is_tf_available() else () __SCREAMING_SNAKE_CASE : str = ( {'''feature-extraction''': TFCvtModel, '''image-classification''': TFCvtForImageClassification} if is_tf_available() else {} ) __SCREAMING_SNAKE_CASE : Any = False __SCREAMING_SNAKE_CASE : List[str] = False __SCREAMING_SNAKE_CASE : List[str] = False __SCREAMING_SNAKE_CASE : Tuple = False __SCREAMING_SNAKE_CASE : Union[str, Any] = False def __lowerCAmelCase ( self : str ): '''simple docstring''' __a : Optional[Any] = TFCvtModelTester(self ) __a : Any = TFCvtConfigTester(self , config_class=__SCREAMING_SNAKE_CASE , has_text_modality=__SCREAMING_SNAKE_CASE , hidden_size=3_7 ) def __lowerCAmelCase ( self : List[Any] ): '''simple docstring''' self.config_tester.create_and_test_config_common_properties() self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() @unittest.skip(reason='Cvt does not output attentions' ) def __lowerCAmelCase ( self : Any ): '''simple docstring''' pass @unittest.skip(reason='Cvt does not use inputs_embeds' ) def __lowerCAmelCase ( self : str ): '''simple docstring''' pass @unittest.skip(reason='Cvt does not support input and output embeddings' ) def __lowerCAmelCase ( self : Tuple ): '''simple docstring''' pass @unittest.skipIf( not is_tf_available() or len(tf.config.list_physical_devices('GPU' ) ) == 0 , reason='TF does not support backprop for grouped convolutions on CPU.' , ) def __lowerCAmelCase ( self : List[str] ): '''simple docstring''' super().test_dataset_conversion() @unittest.skipIf( not is_tf_available() or len(tf.config.list_physical_devices('GPU' ) ) == 0 , reason='TF does not support backprop for grouped convolutions on CPU.' , ) @slow def __lowerCAmelCase ( self : Optional[int] ): '''simple docstring''' super().test_keras_fit() @unittest.skip(reason='Get `Failed to determine best cudnn convolution algo.` error after using TF 2.12+cuda 11.8' ) def __lowerCAmelCase ( self : List[Any] ): '''simple docstring''' __a : List[str] = tf.keras.mixed_precision.Policy('mixed_float16' ) tf.keras.mixed_precision.set_global_policy(__SCREAMING_SNAKE_CASE ) super().test_keras_fit() tf.keras.mixed_precision.set_global_policy('float32' ) def __lowerCAmelCase ( self : int ): '''simple docstring''' __a : List[str] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: __a : Any = model_class(__SCREAMING_SNAKE_CASE ) __a : str = inspect.signature(model.call ) # signature.parameters is an OrderedDict => so arg_names order is deterministic __a : Optional[Any] = [*signature.parameters.keys()] __a : Tuple = ["""pixel_values"""] self.assertListEqual(arg_names[:1] , __SCREAMING_SNAKE_CASE ) def __lowerCAmelCase ( self : Tuple ): '''simple docstring''' def check_hidden_states_output(SCREAMING_SNAKE_CASE__ : Tuple , SCREAMING_SNAKE_CASE__ : Tuple , SCREAMING_SNAKE_CASE__ : Union[str, Any] ): __a : str = model_class(__SCREAMING_SNAKE_CASE ) __a : List[Any] = model(**self._prepare_for_class(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ) __a : Optional[int] = outputs.hidden_states __a : Tuple = len(self.model_tester.depth ) self.assertEqual(len(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) # verify the first hidden states (first block) self.assertListEqual( list(hidden_states[0].shape[-3:] ) , [ self.model_tester.embed_dim[0], self.model_tester.image_size // 4, self.model_tester.image_size // 4, ] , ) __a : str = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: __a : List[Any] = True check_hidden_states_output(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] __a : List[str] = True check_hidden_states_output(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def __lowerCAmelCase ( self : Optional[int] ): '''simple docstring''' __a : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE ) def __lowerCAmelCase ( self : List[Any] ): '''simple docstring''' __a : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*__SCREAMING_SNAKE_CASE ) @slow def __lowerCAmelCase ( self : Optional[Any] ): '''simple docstring''' for model_name in TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __a : str = TFCvtModel.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsNotNone(__SCREAMING_SNAKE_CASE ) def UpperCAmelCase__ ( ): __a : Union[str, Any] = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_tf @require_vision class _UpperCamelCase( unittest.TestCase ): @cached_property def __lowerCAmelCase ( self : int ): '''simple docstring''' return AutoImageProcessor.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) @slow def __lowerCAmelCase ( self : List[Any] ): '''simple docstring''' __a : Optional[Any] = TFCvtForImageClassification.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) __a : Union[str, Any] = self.default_image_processor __a : int = prepare_img() __a : Dict = image_processor(images=__SCREAMING_SNAKE_CASE , return_tensors='tf' ) # forward pass __a : Optional[int] = model(**__SCREAMING_SNAKE_CASE ) # verify the logits __a : str = tf.TensorShape((1, 1_0_0_0) ) self.assertEqual(outputs.logits.shape , __SCREAMING_SNAKE_CASE ) __a : int = tf.constant([0.9_285, 0.9_015, -0.3_150] ) self.assertTrue(np.allclose(outputs.logits[0, :3].numpy() , __SCREAMING_SNAKE_CASE , atol=1e-4 ) )
47
'''simple docstring''' import shutil import tempfile import unittest from transformers import SPIECE_UNDERLINE, BatchEncoding, MBartTokenizer, MBartTokenizerFast, is_torch_available from transformers.testing_utils import ( get_tests_dir, nested_simplify, require_sentencepiece, require_tokenizers, require_torch, ) from ...test_tokenization_common import TokenizerTesterMixin A_ : Union[str, Any] = get_tests_dir("fixtures/test_sentencepiece.model") if is_torch_available(): from transformers.models.mbart.modeling_mbart import shift_tokens_right A_ : str = 250004 A_ : str = 250020 @require_sentencepiece @require_tokenizers class __snake_case ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowerCamelCase__ = MBartTokenizer lowerCamelCase__ = MBartTokenizerFast lowerCamelCase__ = True lowerCamelCase__ = True def __UpperCamelCase ( self ): super().setUp() # We have a SentencePiece fixture for testing snake_case__ : Tuple = MBartTokenizer(__SCREAMING_SNAKE_CASE , keep_accents=__SCREAMING_SNAKE_CASE ) tokenizer.save_pretrained(self.tmpdirname ) def __UpperCamelCase ( self ): snake_case__ : Tuple = MBartTokenizer(__SCREAMING_SNAKE_CASE , keep_accents=__SCREAMING_SNAKE_CASE ) snake_case__ : int = tokenizer.tokenize("""This is a test""" ) self.assertListEqual(__SCREAMING_SNAKE_CASE , ["""▁This""", """▁is""", """▁a""", """▁t""", """est"""] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) , [value + tokenizer.fairseq_offset for value in [2_8_5, 4_6, 1_0, 1_7_0, 3_8_2]] , ) snake_case__ : Optional[int] = tokenizer.tokenize("""I was born in 92000, and this is falsé.""" ) self.assertListEqual( __SCREAMING_SNAKE_CASE , [ SPIECE_UNDERLINE + """I""", SPIECE_UNDERLINE + """was""", SPIECE_UNDERLINE + """b""", """or""", """n""", SPIECE_UNDERLINE + """in""", SPIECE_UNDERLINE + """""", """9""", """2""", """0""", """0""", """0""", """,""", SPIECE_UNDERLINE + """and""", SPIECE_UNDERLINE + """this""", SPIECE_UNDERLINE + """is""", SPIECE_UNDERLINE + """f""", """al""", """s""", """é""", """.""", ] , ) snake_case__ : Optional[int] = tokenizer.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) self.assertListEqual( __SCREAMING_SNAKE_CASE , [ value + tokenizer.fairseq_offset for value in [8, 2_1, 8_4, 5_5, 2_4, 1_9, 7, 2, 6_0_2, 3_4_7, 3_4_7, 3_4_7, 3, 1_2, 6_6, 4_6, 7_2, 8_0, 6, 2, 4] # ^ unk: 2 + 1 = 3 unk: 2 + 1 = 3 ^ ] , ) snake_case__ : Union[str, Any] = tokenizer.convert_ids_to_tokens(__SCREAMING_SNAKE_CASE ) self.assertListEqual( __SCREAMING_SNAKE_CASE , [ SPIECE_UNDERLINE + """I""", SPIECE_UNDERLINE + """was""", SPIECE_UNDERLINE + """b""", """or""", """n""", SPIECE_UNDERLINE + """in""", SPIECE_UNDERLINE + """""", """<unk>""", """2""", """0""", """0""", """0""", """,""", SPIECE_UNDERLINE + """and""", SPIECE_UNDERLINE + """this""", SPIECE_UNDERLINE + """is""", SPIECE_UNDERLINE + """f""", """al""", """s""", """<unk>""", """.""", ] , ) def __UpperCamelCase ( self ): if not self.test_slow_tokenizer: # as we don't have a slow version, we can't compare the outputs between slow and fast versions return snake_case__ : Optional[int] = (self.rust_tokenizer_class, """hf-internal-testing/tiny-random-mbart""", {}) for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f"{tokenizer.__class__.__name__} ({pretrained_name})" ): snake_case__ : Optional[Any] = self.rust_tokenizer_class.from_pretrained(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) snake_case__ : Dict = self.tokenizer_class.from_pretrained(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) snake_case__ : List[str] = tempfile.mkdtemp() snake_case__ : int = tokenizer_r.save_pretrained(__SCREAMING_SNAKE_CASE ) snake_case__ : Dict = tokenizer_p.save_pretrained(__SCREAMING_SNAKE_CASE ) # Checks it save with the same files + the tokenizer.json file for the fast one self.assertTrue(any("""tokenizer.json""" in f for f in tokenizer_r_files ) ) snake_case__ : List[str] = tuple(f for f in tokenizer_r_files if """tokenizer.json""" not in f ) self.assertSequenceEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # Checks everything loads correctly in the same way snake_case__ : Tuple = tokenizer_r.from_pretrained(__SCREAMING_SNAKE_CASE ) snake_case__ : Union[str, Any] = tokenizer_p.from_pretrained(__SCREAMING_SNAKE_CASE ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ) # self.assertEqual(getattr(tokenizer_rp, key), getattr(tokenizer_pp, key)) # self.assertEqual(getattr(tokenizer_rp, key + "_id"), getattr(tokenizer_pp, key + "_id")) shutil.rmtree(__SCREAMING_SNAKE_CASE ) # Save tokenizer rust, legacy_format=True snake_case__ : Any = tempfile.mkdtemp() snake_case__ : Optional[int] = tokenizer_r.save_pretrained(__SCREAMING_SNAKE_CASE , legacy_format=__SCREAMING_SNAKE_CASE ) snake_case__ : int = tokenizer_p.save_pretrained(__SCREAMING_SNAKE_CASE ) # Checks it save with the same files self.assertSequenceEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # Checks everything loads correctly in the same way snake_case__ : List[Any] = tokenizer_r.from_pretrained(__SCREAMING_SNAKE_CASE ) snake_case__ : Dict = tokenizer_p.from_pretrained(__SCREAMING_SNAKE_CASE ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ) shutil.rmtree(__SCREAMING_SNAKE_CASE ) # Save tokenizer rust, legacy_format=False snake_case__ : Dict = tempfile.mkdtemp() snake_case__ : Union[str, Any] = tokenizer_r.save_pretrained(__SCREAMING_SNAKE_CASE , legacy_format=__SCREAMING_SNAKE_CASE ) snake_case__ : Optional[int] = tokenizer_p.save_pretrained(__SCREAMING_SNAKE_CASE ) # Checks it saved the tokenizer.json file self.assertTrue(any("""tokenizer.json""" in f for f in tokenizer_r_files ) ) # Checks everything loads correctly in the same way snake_case__ : Dict = tokenizer_r.from_pretrained(__SCREAMING_SNAKE_CASE ) snake_case__ : Any = tokenizer_p.from_pretrained(__SCREAMING_SNAKE_CASE ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ) shutil.rmtree(__SCREAMING_SNAKE_CASE ) @require_torch @require_sentencepiece @require_tokenizers class __snake_case ( unittest.TestCase ): '''simple docstring''' lowerCamelCase__ = '''facebook/mbart-large-en-ro''' lowerCamelCase__ = [ ''' UN Chief Says There Is No Military Solution in Syria''', ''' Secretary-General Ban Ki-moon says his response to Russia\'s stepped up military support for Syria is that "there is no military solution" to the nearly five-year conflict and more weapons will only worsen the violence and misery for millions of people.''', ] lowerCamelCase__ = [ '''Şeful ONU declară că nu există o soluţie militară în Siria''', '''Secretarul General Ban Ki-moon declară că răspunsul său la intensificarea sprijinului militar al Rusiei''' ''' pentru Siria este că "nu există o soluţie militară" la conflictul de aproape cinci ani şi că noi arme nu vor''' ''' face decât să înrăutăţească violenţele şi mizeria pentru milioane de oameni.''', ] lowerCamelCase__ = [8_274, 127_873, 25_916, 7, 8_622, 2_071, 438, 67_485, 53, 187_895, 23, 51_712, 2, EN_CODE] @classmethod def __UpperCamelCase ( cls ): snake_case__ : MBartTokenizer = MBartTokenizer.from_pretrained( cls.checkpoint_name , src_lang="""en_XX""" , tgt_lang="""ro_RO""" ) snake_case__ : Any = 1 return cls def __UpperCamelCase ( self ): self.assertEqual(self.tokenizer.fairseq_tokens_to_ids["""ar_AR"""] , 2_5_0_0_0_1 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids["""en_EN"""] , 2_5_0_0_0_4 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids["""ro_RO"""] , 2_5_0_0_2_0 ) def __UpperCamelCase ( self ): snake_case__ : Tuple = self.tokenizer.batch_encode_plus(self.src_text ).input_ids[0] self.assertListEqual(self.expected_src_tokens , __SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self ): self.assertIn(__SCREAMING_SNAKE_CASE , self.tokenizer.all_special_ids ) snake_case__ : List[str] = [RO_CODE, 8_8_4, 9_0_1_9, 9_6, 9, 9_1_6, 8_6_7_9_2, 3_6, 1_8_7_4_3, 1_5_5_9_6, 5, 2] snake_case__ : List[Any] = self.tokenizer.decode(__SCREAMING_SNAKE_CASE , skip_special_tokens=__SCREAMING_SNAKE_CASE ) snake_case__ : Dict = self.tokenizer.decode(generated_ids[1:] , skip_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) self.assertNotIn(self.tokenizer.eos_token , __SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self ): snake_case__ : Dict = ["""this is gunna be a long sentence """ * 2_0] assert isinstance(src_text[0] , __SCREAMING_SNAKE_CASE ) snake_case__ : Tuple = 1_0 snake_case__ : int = self.tokenizer(__SCREAMING_SNAKE_CASE , max_length=__SCREAMING_SNAKE_CASE , truncation=__SCREAMING_SNAKE_CASE ).input_ids[0] self.assertEqual(ids[-2] , 2 ) self.assertEqual(ids[-1] , __SCREAMING_SNAKE_CASE ) self.assertEqual(len(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self ): self.assertListEqual(self.tokenizer.convert_tokens_to_ids(["""<mask>""", """ar_AR"""] ) , [2_5_0_0_2_6, 2_5_0_0_0_1] ) def __UpperCamelCase ( self ): snake_case__ : Union[str, Any] = tempfile.mkdtemp() snake_case__ : Dict = self.tokenizer.fairseq_tokens_to_ids self.tokenizer.save_pretrained(__SCREAMING_SNAKE_CASE ) snake_case__ : Any = MBartTokenizer.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertDictEqual(new_tok.fairseq_tokens_to_ids , __SCREAMING_SNAKE_CASE ) @require_torch def __UpperCamelCase ( self ): snake_case__ : Tuple = self.tokenizer(self.src_text , text_target=self.tgt_text , padding=__SCREAMING_SNAKE_CASE , return_tensors="""pt""" ) snake_case__ : int = shift_tokens_right(batch["""labels"""] , self.tokenizer.pad_token_id ) # fairseq batch: https://gist.github.com/sshleifer/cba08bc2109361a74ac3760a7e30e4f4 assert batch.input_ids[1][-2:].tolist() == [2, EN_CODE] assert batch.decoder_input_ids[1][0].tolist() == RO_CODE assert batch.decoder_input_ids[1][-1] == 2 assert batch.labels[1][-2:].tolist() == [2, RO_CODE] @require_torch def __UpperCamelCase ( self ): snake_case__ : Optional[int] = self.tokenizer( self.src_text , text_target=self.tgt_text , padding=__SCREAMING_SNAKE_CASE , truncation=__SCREAMING_SNAKE_CASE , max_length=len(self.expected_src_tokens ) , return_tensors="""pt""" , ) snake_case__ : List[str] = shift_tokens_right(batch["""labels"""] , self.tokenizer.pad_token_id ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) self.assertEqual((2, 1_4) , batch.input_ids.shape ) self.assertEqual((2, 1_4) , batch.attention_mask.shape ) snake_case__ : Tuple = batch.input_ids.tolist()[0] self.assertListEqual(self.expected_src_tokens , __SCREAMING_SNAKE_CASE ) self.assertEqual(2 , batch.decoder_input_ids[0, -1] ) # EOS # Test that special tokens are reset self.assertEqual(self.tokenizer.prefix_tokens , [] ) self.assertEqual(self.tokenizer.suffix_tokens , [self.tokenizer.eos_token_id, EN_CODE] ) def __UpperCamelCase ( self ): snake_case__ : Optional[int] = self.tokenizer(self.src_text , padding=__SCREAMING_SNAKE_CASE , truncation=__SCREAMING_SNAKE_CASE , max_length=3 , return_tensors="""pt""" ) snake_case__ : Optional[int] = self.tokenizer( text_target=self.tgt_text , padding=__SCREAMING_SNAKE_CASE , truncation=__SCREAMING_SNAKE_CASE , max_length=1_0 , return_tensors="""pt""" ) snake_case__ : str = targets["""input_ids"""] snake_case__ : Optional[Any] = shift_tokens_right(__SCREAMING_SNAKE_CASE , self.tokenizer.pad_token_id ) self.assertEqual(batch.input_ids.shape[1] , 3 ) self.assertEqual(batch.decoder_input_ids.shape[1] , 1_0 ) @require_torch def __UpperCamelCase ( self ): snake_case__ : Tuple = self.tokenizer._build_translation_inputs( """A test""" , return_tensors="""pt""" , src_lang="""en_XX""" , tgt_lang="""ar_AR""" ) self.assertEqual( nested_simplify(__SCREAMING_SNAKE_CASE ) , { # A, test, EOS, en_XX """input_ids""": [[6_2, 3_0_3_4, 2, 2_5_0_0_0_4]], """attention_mask""": [[1, 1, 1, 1]], # ar_AR """forced_bos_token_id""": 2_5_0_0_0_1, } , )
38
0
'''simple docstring''' import heapq as hq import math from collections.abc import Iterator class _lowercase : def __init__( self : Any , SCREAMING_SNAKE_CASE_ : List[str] ) -> int: __snake_case = str(id_ ) __snake_case = None __snake_case = None __snake_case = [] __snake_case = {} # {vertex:distance} def __lt__( self : List[Any] , SCREAMING_SNAKE_CASE_ : Tuple ) -> int: return self.key < other.key def __repr__( self : str ) -> Tuple: return self.id def a ( self : Union[str, Any] , SCREAMING_SNAKE_CASE_ : int ) -> Optional[Any]: self.neighbors.append(__SCREAMING_SNAKE_CASE ) def a ( self : Dict , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : Optional[Any] ) -> Dict: __snake_case = weight def _a (lowercase__ : Optional[Any] , lowercase__ : List[str] , lowercase__ : int , lowercase__ : Dict ) -> Union[str, Any]: """simple docstring""" graph[a - 1].add_neighbor(graph[b - 1] ) graph[b - 1].add_neighbor(graph[a - 1] ) # add the edges: graph[a - 1].add_edge(graph[b - 1] , lowercase__ ) graph[b - 1].add_edge(graph[a - 1] , lowercase__ ) def _a (lowercase__ : list , lowercase__ : Vertex ) -> list: """simple docstring""" __snake_case = [] for u in graph: __snake_case = math.inf __snake_case = None __snake_case = 0 __snake_case = graph[:] while q: __snake_case = min(lowercase__ ) q.remove(lowercase__ ) for v in u.neighbors: if (v in q) and (u.edges[v.id] < v.key): __snake_case = u __snake_case = u.edges[v.id] for i in range(1 , len(lowercase__ ) ): a.append((int(graph[i].id ) + 1, int(graph[i].pi.id ) + 1) ) return a def _a (lowercase__ : list , lowercase__ : Vertex ) -> Iterator[tuple]: """simple docstring""" for u in graph: __snake_case = math.inf __snake_case = None __snake_case = 0 __snake_case = list(lowercase__ ) hq.heapify(lowercase__ ) while h: __snake_case = hq.heappop(lowercase__ ) for v in u.neighbors: if (v in h) and (u.edges[v.id] < v.key): __snake_case = u __snake_case = u.edges[v.id] hq.heapify(lowercase__ ) for i in range(1 , len(lowercase__ ) ): yield (int(graph[i].id ) + 1, int(graph[i].pi.id ) + 1) def _a () -> None: """simple docstring""" if __name__ == "__main__": import doctest doctest.testmod()
56
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices A_ : int = logging.get_logger(__name__) A_ : Dict = { "google/bit-50": "https://huggingface.co/google/bit-50/resolve/main/config.json", } class __snake_case ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): '''simple docstring''' lowerCamelCase__ = '''bit''' lowerCamelCase__ = ['''preactivation''', '''bottleneck'''] lowerCamelCase__ = ['''SAME''', '''VALID'''] def __init__( self , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=6_4 , __SCREAMING_SNAKE_CASE=[2_5_6, 5_1_2, 1_0_2_4, 2_0_4_8] , __SCREAMING_SNAKE_CASE=[3, 4, 6, 3] , __SCREAMING_SNAKE_CASE="preactivation" , __SCREAMING_SNAKE_CASE="relu" , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=3_2 , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=3_2 , __SCREAMING_SNAKE_CASE=1 , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE , ): super().__init__(**__SCREAMING_SNAKE_CASE ) if layer_type not in self.layer_types: raise ValueError(f"layer_type={layer_type} is not one of {','.join(self.layer_types )}" ) if global_padding is not None: if global_padding.upper() in self.supported_padding: snake_case__ : Tuple = global_padding.upper() else: raise ValueError(f"Padding strategy {global_padding} not supported" ) snake_case__ : List[str] = num_channels snake_case__ : Tuple = embedding_size snake_case__ : str = hidden_sizes snake_case__ : Optional[Any] = depths snake_case__ : List[Any] = layer_type snake_case__ : Dict = hidden_act snake_case__ : Union[str, Any] = global_padding snake_case__ : List[str] = num_groups snake_case__ : str = drop_path_rate snake_case__ : List[Any] = embedding_dynamic_padding snake_case__ : List[str] = output_stride snake_case__ : Dict = width_factor snake_case__ : List[str] = ["""stem"""] + [f"stage{idx}" for idx in range(1 , len(__SCREAMING_SNAKE_CASE ) + 1 )] snake_case__ , snake_case__ : Dict = get_aligned_output_features_output_indices( out_features=__SCREAMING_SNAKE_CASE , out_indices=__SCREAMING_SNAKE_CASE , stage_names=self.stage_names )
38
0
import unittest from transformers.models.xlm_prophetnet.tokenization_xlm_prophetnet import SPIECE_UNDERLINE, XLMProphetNetTokenizer from transformers.testing_utils import get_tests_dir, require_sentencepiece, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin snake_case__ : Union[str, Any] = get_tests_dir("""fixtures/test_sentencepiece.model""") @require_sentencepiece class _a ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): """simple docstring""" A_ = XLMProphetNetTokenizer A_ = False A_ = True def _UpperCAmelCase ( self ) -> Optional[int]: super().setUp() # We have a SentencePiece fixture for testing UpperCamelCase_ = XLMProphetNetTokenizer(__SCREAMING_SNAKE_CASE , keep_accents=__SCREAMING_SNAKE_CASE ) tokenizer.save_pretrained(self.tmpdirname ) def _UpperCAmelCase ( self ) -> int: UpperCamelCase_ = """[PAD]""" UpperCamelCase_ = 0 self.assertEqual(self.get_tokenizer()._convert_token_to_id(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) def _UpperCAmelCase ( self ) -> Any: UpperCamelCase_ = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '[PAD]' ) self.assertEqual(vocab_keys[1] , '[CLS]' ) self.assertEqual(vocab_keys[-1] , 'j' ) self.assertEqual(len(__SCREAMING_SNAKE_CASE ) , 1012 ) def _UpperCAmelCase ( self ) -> str: self.assertEqual(self.get_tokenizer().vocab_size , 1012 ) def _UpperCAmelCase ( self ) -> int: UpperCamelCase_ = XLMProphetNetTokenizer(__SCREAMING_SNAKE_CASE , keep_accents=__SCREAMING_SNAKE_CASE ) UpperCamelCase_ = tokenizer.tokenize('This is a test' ) self.assertListEqual(__SCREAMING_SNAKE_CASE , ['▁This', '▁is', '▁a', '▁t', 'est'] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) , [value + tokenizer.fairseq_offset for value in [285, 46, 10, 170, 382]] , ) UpperCamelCase_ = tokenizer.tokenize('I was born in 92000, and this is falsé.' ) self.assertListEqual( __SCREAMING_SNAKE_CASE , [ SPIECE_UNDERLINE + 'I', SPIECE_UNDERLINE + 'was', SPIECE_UNDERLINE + 'b', 'or', 'n', SPIECE_UNDERLINE + 'in', SPIECE_UNDERLINE + '', '9', '2', '0', '0', '0', ',', SPIECE_UNDERLINE + 'and', SPIECE_UNDERLINE + 'this', SPIECE_UNDERLINE + 'is', SPIECE_UNDERLINE + 'f', 'al', 's', 'é', '.', ] , ) UpperCamelCase_ = tokenizer.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) self.assertListEqual( __SCREAMING_SNAKE_CASE , [ value + tokenizer.fairseq_offset for value in [8, 21, 84, 55, 24, 19, 7, -9, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, -9, 4] ] , ) UpperCamelCase_ = tokenizer.convert_ids_to_tokens(__SCREAMING_SNAKE_CASE ) self.assertListEqual( __SCREAMING_SNAKE_CASE , [ SPIECE_UNDERLINE + 'I', SPIECE_UNDERLINE + 'was', SPIECE_UNDERLINE + 'b', 'or', 'n', SPIECE_UNDERLINE + 'in', SPIECE_UNDERLINE + '', '[UNK]', '2', '0', '0', '0', ',', SPIECE_UNDERLINE + 'and', SPIECE_UNDERLINE + 'this', SPIECE_UNDERLINE + 'is', SPIECE_UNDERLINE + 'f', 'al', 's', '[UNK]', '.', ] , ) @cached_property def _UpperCAmelCase ( self ) -> Optional[int]: return XLMProphetNetTokenizer.from_pretrained('microsoft/xprophetnet-large-wiki100-cased' ) @slow def _UpperCAmelCase ( self ) -> List[Any]: UpperCamelCase_ = """Hello World!""" UpperCamelCase_ = [35389, 6672, 49, 2] self.assertListEqual(__SCREAMING_SNAKE_CASE , self.big_tokenizer.encode(__SCREAMING_SNAKE_CASE ) ) @slow def _UpperCAmelCase ( self ) -> List[str]: # fmt: off UpperCamelCase_ = {"""input_ids""": [[11073, 82783, 18, 26, 82783, 549, 51540, 248, 17209, 1301, 217, 20, 215186, 1325, 147, 17209, 1301, 217, 20, 56370, 53, 122020, 20, 16477, 27, 87355, 4548, 20, 4728, 78392, 17, 159969, 18, 26, 24491, 629, 15, 538, 22704, 5439, 15, 2788, 24491, 9885, 15, 43534, 605, 15, 814, 18403, 33200, 29, 15, 43534, 24458, 12410, 111, 24966, 83669, 9637, 144068, 26, 850, 22346, 27, 147, 24966, 83669, 83490, 26, 39113, 735, 27, 689, 656, 2800, 1339, 4600, 53, 122020, 115785, 34, 816, 1339, 46887, 18, 147, 53905, 1951, 42238, 41170, 17732, 834, 436, 15, 27523, 98733, 217, 147, 5542, 4981, 930, 17347, 16, 2], [20091, 629, 94, 82786, 58, 490, 20, 1528, 84, 53905, 344, 80592, 110128, 18822, 5267, 1306, 62, 152537, 308, 7997, 401, 124427, 549, 35442, 225, 109, 15055, 25748, 147, 7119, 43712, 34, 767, 135366, 18, 16, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [592, 63784, 119466, 17, 147808, 88214, 18, 656, 81, 32, 3296, 10280, 16, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], """attention_mask""": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=__SCREAMING_SNAKE_CASE , model_name='microsoft/xprophetnet-large-wiki100-cased' , revision='1acad1643ddd54a44df6a1b797ada8373685d90e' , )
23
'''simple docstring''' import argparse import json from pathlib import Path import requests import timm import torch from huggingface_hub import hf_hub_download from PIL import Image from timm.data import resolve_data_config from timm.data.transforms_factory import create_transform from transformers import ( BitConfig, ViTHybridConfig, ViTHybridForImageClassification, ViTHybridImageProcessor, ViTHybridModel, ) from transformers.image_utils import PILImageResampling from transformers.utils import logging logging.set_verbosity_info() A_ : Optional[int] = logging.get_logger(__name__) def UpperCamelCase__ ( __magic_name__ : Optional[Any] , __magic_name__ : str=False ) -> Tuple: '''simple docstring''' snake_case__ : int = [] # fmt: off # stem: rename_keys.append(("""cls_token""", """vit.embeddings.cls_token""") ) rename_keys.append(("""pos_embed""", """vit.embeddings.position_embeddings""") ) rename_keys.append(("""patch_embed.proj.weight""", """vit.embeddings.patch_embeddings.projection.weight""") ) rename_keys.append(("""patch_embed.proj.bias""", """vit.embeddings.patch_embeddings.projection.bias""") ) # backbone rename_keys.append(("""patch_embed.backbone.stem.conv.weight""", """vit.embeddings.patch_embeddings.backbone.bit.embedder.convolution.weight""") ) rename_keys.append(("""patch_embed.backbone.stem.norm.weight""", """vit.embeddings.patch_embeddings.backbone.bit.embedder.norm.weight""") ) rename_keys.append(("""patch_embed.backbone.stem.norm.bias""", """vit.embeddings.patch_embeddings.backbone.bit.embedder.norm.bias""") ) for stage_idx in range(len(config.backbone_config.depths ) ): for layer_idx in range(config.backbone_config.depths[stage_idx] ): rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.conv1.weight", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.conv1.weight") ) rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm1.weight", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm1.weight") ) rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm1.bias", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm1.bias") ) rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.conv2.weight", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.conv2.weight") ) rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm2.weight", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm2.weight") ) rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm2.bias", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm2.bias") ) rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.conv3.weight", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.conv3.weight") ) rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm3.weight", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm3.weight") ) rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm3.bias", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm3.bias") ) rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.0.downsample.conv.weight", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.0.downsample.conv.weight") ) rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.0.downsample.norm.weight", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.0.downsample.norm.weight") ) rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.0.downsample.norm.bias", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.0.downsample.norm.bias") ) # transformer encoder for i in range(config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append((f"blocks.{i}.norm1.weight", f"vit.encoder.layer.{i}.layernorm_before.weight") ) rename_keys.append((f"blocks.{i}.norm1.bias", f"vit.encoder.layer.{i}.layernorm_before.bias") ) rename_keys.append((f"blocks.{i}.attn.proj.weight", f"vit.encoder.layer.{i}.attention.output.dense.weight") ) rename_keys.append((f"blocks.{i}.attn.proj.bias", f"vit.encoder.layer.{i}.attention.output.dense.bias") ) rename_keys.append((f"blocks.{i}.norm2.weight", f"vit.encoder.layer.{i}.layernorm_after.weight") ) rename_keys.append((f"blocks.{i}.norm2.bias", f"vit.encoder.layer.{i}.layernorm_after.bias") ) rename_keys.append((f"blocks.{i}.mlp.fc1.weight", f"vit.encoder.layer.{i}.intermediate.dense.weight") ) rename_keys.append((f"blocks.{i}.mlp.fc1.bias", f"vit.encoder.layer.{i}.intermediate.dense.bias") ) rename_keys.append((f"blocks.{i}.mlp.fc2.weight", f"vit.encoder.layer.{i}.output.dense.weight") ) rename_keys.append((f"blocks.{i}.mlp.fc2.bias", f"vit.encoder.layer.{i}.output.dense.bias") ) if base_model: # layernorm + pooler rename_keys.extend( [ ("""norm.weight""", """layernorm.weight"""), ("""norm.bias""", """layernorm.bias"""), ("""pre_logits.fc.weight""", """pooler.dense.weight"""), ("""pre_logits.fc.bias""", """pooler.dense.bias"""), ] ) # if just the base model, we should remove "vit" from all keys that start with "vit" snake_case__ : List[Any] = [(pair[0], pair[1][4:]) if pair[1].startswith("""vit""" ) else pair for pair in rename_keys] else: # layernorm + classification head rename_keys.extend( [ ("""norm.weight""", """vit.layernorm.weight"""), ("""norm.bias""", """vit.layernorm.bias"""), ("""head.weight""", """classifier.weight"""), ("""head.bias""", """classifier.bias"""), ] ) # fmt: on return rename_keys def UpperCamelCase__ ( __magic_name__ : Tuple , __magic_name__ : int , __magic_name__ : Tuple=False ) -> str: '''simple docstring''' for i in range(config.num_hidden_layers ): if base_model: snake_case__ : int = """""" else: snake_case__ : Dict = """vit.""" # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) snake_case__ : int = state_dict.pop(f"blocks.{i}.attn.qkv.weight" ) snake_case__ : Union[str, Any] = state_dict.pop(f"blocks.{i}.attn.qkv.bias" ) # next, add query, keys and values (in that order) to the state dict snake_case__ : Optional[int] = in_proj_weight[ : config.hidden_size, : ] snake_case__ : Optional[Any] = in_proj_bias[: config.hidden_size] snake_case__ : List[Any] = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] snake_case__ : List[Any] = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] snake_case__ : List[Any] = in_proj_weight[ -config.hidden_size :, : ] snake_case__ : Optional[int] = in_proj_bias[-config.hidden_size :] def UpperCamelCase__ ( __magic_name__ : Optional[Any] ) -> List[str]: '''simple docstring''' snake_case__ : str = ["""head.weight""", """head.bias"""] for k in ignore_keys: state_dict.pop(__magic_name__ , __magic_name__ ) def UpperCamelCase__ ( __magic_name__ : List[str] , __magic_name__ : Union[str, Any] , __magic_name__ : str ) -> Union[str, Any]: '''simple docstring''' snake_case__ : List[str] = dct.pop(__magic_name__ ) snake_case__ : Dict = val def UpperCamelCase__ ( ) -> str: '''simple docstring''' snake_case__ : Optional[int] = """http://images.cocodataset.org/val2017/000000039769.jpg""" snake_case__ : Optional[int] = Image.open(requests.get(__magic_name__ , stream=__magic_name__ ).raw ) return im @torch.no_grad() def UpperCamelCase__ ( __magic_name__ : List[Any] , __magic_name__ : Union[str, Any] , __magic_name__ : int=False ) -> Optional[int]: '''simple docstring''' snake_case__ : int = BitConfig( global_padding="""same""" , layer_type="""bottleneck""" , depths=(3, 4, 9) , out_features=["""stage3"""] , embedding_dynamic_padding=__magic_name__ , ) snake_case__ : Optional[int] = ViTHybridConfig(backbone_config=__magic_name__ , image_size=3_84 , num_labels=10_00 ) snake_case__ : Union[str, Any] = False # load original model from timm snake_case__ : List[Any] = timm.create_model(__magic_name__ , pretrained=__magic_name__ ) timm_model.eval() # load state_dict of original model, remove and rename some keys snake_case__ : Optional[int] = timm_model.state_dict() if base_model: remove_classification_head_(__magic_name__ ) snake_case__ : int = create_rename_keys(__magic_name__ , __magic_name__ ) for src, dest in rename_keys: rename_key(__magic_name__ , __magic_name__ , __magic_name__ ) read_in_q_k_v(__magic_name__ , __magic_name__ , __magic_name__ ) snake_case__ : str = """huggingface/label-files""" snake_case__ : Union[str, Any] = """imagenet-1k-id2label.json""" snake_case__ : Dict = json.load(open(hf_hub_download(__magic_name__ , __magic_name__ , repo_type="""dataset""" ) , """r""" ) ) snake_case__ : List[Any] = {int(__magic_name__ ): v for k, v in idalabel.items()} snake_case__ : int = idalabel snake_case__ : str = {v: k for k, v in idalabel.items()} # load HuggingFace model if vit_name[-5:] == "in21k": snake_case__ : str = ViTHybridModel(__magic_name__ ).eval() else: snake_case__ : Union[str, Any] = ViTHybridForImageClassification(__magic_name__ ).eval() model.load_state_dict(__magic_name__ ) # create image processor snake_case__ : Optional[Any] = create_transform(**resolve_data_config({} , model=__magic_name__ ) ) snake_case__ : Union[str, Any] = transform.transforms snake_case__ : Tuple = { """bilinear""": PILImageResampling.BILINEAR, """bicubic""": PILImageResampling.BICUBIC, """nearest""": PILImageResampling.NEAREST, } snake_case__ : Any = ViTHybridImageProcessor( do_resize=__magic_name__ , size={"""shortest_edge""": timm_transforms[0].size} , resample=pillow_resamplings[timm_transforms[0].interpolation.value] , do_center_crop=__magic_name__ , crop_size={"""height""": timm_transforms[1].size[0], """width""": timm_transforms[1].size[1]} , do_normalize=__magic_name__ , image_mean=timm_transforms[-1].mean.tolist() , image_std=timm_transforms[-1].std.tolist() , ) snake_case__ : Any = prepare_img() snake_case__ : int = transform(__magic_name__ ).unsqueeze(0 ) snake_case__ : List[str] = processor(__magic_name__ , return_tensors="""pt""" ).pixel_values # verify pixel values assert torch.allclose(__magic_name__ , __magic_name__ ) # verify logits with torch.no_grad(): snake_case__ : Optional[Any] = model(__magic_name__ ) snake_case__ : Union[str, Any] = outputs.logits print("""Predicted class:""" , logits.argmax(-1 ).item() ) if base_model: snake_case__ : Dict = timm_model.forward_features(__magic_name__ ) assert timm_pooled_output.shape == outputs.pooler_output.shape assert torch.allclose(__magic_name__ , outputs.pooler_output , atol=1E-3 ) else: snake_case__ : int = timm_model(__magic_name__ ) assert timm_logits.shape == outputs.logits.shape assert torch.allclose(__magic_name__ , outputs.logits , atol=1E-3 ) print("""Looks ok!""" ) if pytorch_dump_folder_path is not None: Path(__magic_name__ ).mkdir(exist_ok=__magic_name__ ) print(f"Saving model {vit_name} to {pytorch_dump_folder_path}" ) model.save_pretrained(__magic_name__ ) print(f"Saving processor to {pytorch_dump_folder_path}" ) processor.save_pretrained(__magic_name__ ) if push_to_hub: print(f"Pushing model and processor to the hub {vit_name}" ) model.push_to_hub(f"ybelkada/{vit_name}" ) processor.push_to_hub(f"ybelkada/{vit_name}" ) if __name__ == "__main__": A_ : List[str] = argparse.ArgumentParser() # Required parameters parser.add_argument( "--vit_name", default="vit_base_r50_s16_384", type=str, help="Name of the hybrid ViT timm model you'd like to convert.", ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model directory." ) parser.add_argument( "--push_to_hub", action="store_true", help="Whether to upload the model to the HuggingFace hub." ) A_ : Union[str, Any] = parser.parse_args() convert_vit_checkpoint(args.vit_name, args.pytorch_dump_folder_path, args.push_to_hub)
38
0
import doctest import logging import os import unittest from pathlib import Path from typing import List, Union import transformers from transformers.testing_utils import require_tf, require_torch, slow UpperCAmelCase__ : Union[str, Any] = logging.getLogger() @unittest.skip("""Temporarily disable the doc tests.""" ) @require_torch @require_tf @slow class UpperCamelCase_ ( unittest.TestCase ): '''simple docstring''' def lowerCAmelCase__ ( self , UpperCamelCase , UpperCamelCase = None , UpperCamelCase = None , UpperCamelCase = None , UpperCamelCase = True , ) -> int: UpperCamelCase__ : str = [file for file in os.listdir(__SCREAMING_SNAKE_CASE) if os.path.isfile(os.path.join(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE))] if identifier is not None: UpperCamelCase__ : Optional[Any] = [file for file in files if identifier in file] if n_identifier is not None: if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE): for n_ in n_identifier: UpperCamelCase__ : Any = [file for file in files if n_ not in file] else: UpperCamelCase__ : int = [file for file in files if n_identifier not in file] UpperCamelCase__ : Dict = ignore_files or [] ignore_files.append('__init__.py') UpperCamelCase__ : str = [file for file in files if file not in ignore_files] for file in files: # Open all files print('Testing' , __SCREAMING_SNAKE_CASE) if only_modules: UpperCamelCase__ : Optional[int] = file.split('.')[0] try: UpperCamelCase__ : Union[str, Any] = getattr(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE) UpperCamelCase__ : List[Any] = doctest.DocTestSuite(__SCREAMING_SNAKE_CASE) UpperCamelCase__ : Any = unittest.TextTestRunner().run(__SCREAMING_SNAKE_CASE) self.assertIs(len(result.failures) , 0) except AttributeError: logger.info(F"""{module_identifier} is not a module.""") else: UpperCamelCase__ : List[Any] = doctest.testfile(str('..' / directory / file) , optionflags=doctest.ELLIPSIS) self.assertIs(result.failed , 0) def lowerCAmelCase__ ( self) -> Dict: UpperCamelCase__ : Any = Path('src/transformers') UpperCamelCase__ : Any = """modeling""" UpperCamelCase__ : Optional[int] = [ """modeling_ctrl.py""", """modeling_tf_ctrl.py""", ] self.analyze_directory(__SCREAMING_SNAKE_CASE , identifier=__SCREAMING_SNAKE_CASE , ignore_files=__SCREAMING_SNAKE_CASE) def lowerCAmelCase__ ( self) -> int: UpperCamelCase__ : Optional[int] = Path('src/transformers') UpperCamelCase__ : Optional[Any] = """tokenization""" self.analyze_directory(__SCREAMING_SNAKE_CASE , identifier=__SCREAMING_SNAKE_CASE) def lowerCAmelCase__ ( self) -> str: UpperCamelCase__ : Dict = Path('src/transformers') UpperCamelCase__ : Dict = """configuration""" self.analyze_directory(__SCREAMING_SNAKE_CASE , identifier=__SCREAMING_SNAKE_CASE) def lowerCAmelCase__ ( self) -> List[Any]: UpperCamelCase__ : str = Path('src/transformers') UpperCamelCase__ : Optional[int] = ["""configuration""", """modeling""", """tokenization"""] self.analyze_directory(__SCREAMING_SNAKE_CASE , n_identifier=__SCREAMING_SNAKE_CASE) def lowerCAmelCase__ ( self) -> List[Any]: UpperCamelCase__ : Optional[Any] = Path('docs/source') UpperCamelCase__ : Dict = ["""favicon.ico"""] self.analyze_directory(__SCREAMING_SNAKE_CASE , ignore_files=__SCREAMING_SNAKE_CASE , only_modules=__SCREAMING_SNAKE_CASE)
410
'''simple docstring''' from dataclasses import dataclass from typing import Optional import numpy as np import torch import torch.nn as nn from ..utils import BaseOutput, is_torch_version, randn_tensor from .attention_processor import SpatialNorm from .unet_ad_blocks import UNetMidBlockaD, get_down_block, get_up_block @dataclass class __snake_case ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' lowerCamelCase__ = 42 class __snake_case ( nn.Module ): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=("DownEncoderBlock2D",) , __SCREAMING_SNAKE_CASE=(6_4,) , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=3_2 , __SCREAMING_SNAKE_CASE="silu" , __SCREAMING_SNAKE_CASE=True , ): super().__init__() snake_case__ : str = layers_per_block snake_case__ : int = torch.nn.Convad( __SCREAMING_SNAKE_CASE , block_out_channels[0] , kernel_size=3 , stride=1 , padding=1 , ) snake_case__ : List[Any] = None snake_case__ : List[Any] = nn.ModuleList([] ) # down snake_case__ : Union[str, Any] = block_out_channels[0] for i, down_block_type in enumerate(__SCREAMING_SNAKE_CASE ): snake_case__ : Optional[Any] = output_channel snake_case__ : Union[str, Any] = block_out_channels[i] snake_case__ : int = i == len(__SCREAMING_SNAKE_CASE ) - 1 snake_case__ : str = get_down_block( __SCREAMING_SNAKE_CASE , num_layers=self.layers_per_block , in_channels=__SCREAMING_SNAKE_CASE , out_channels=__SCREAMING_SNAKE_CASE , add_downsample=not is_final_block , resnet_eps=1e-6 , downsample_padding=0 , resnet_act_fn=__SCREAMING_SNAKE_CASE , resnet_groups=__SCREAMING_SNAKE_CASE , attention_head_dim=__SCREAMING_SNAKE_CASE , temb_channels=__SCREAMING_SNAKE_CASE , ) self.down_blocks.append(__SCREAMING_SNAKE_CASE ) # mid snake_case__ : Optional[Any] = UNetMidBlockaD( in_channels=block_out_channels[-1] , resnet_eps=1e-6 , resnet_act_fn=__SCREAMING_SNAKE_CASE , output_scale_factor=1 , resnet_time_scale_shift="""default""" , attention_head_dim=block_out_channels[-1] , resnet_groups=__SCREAMING_SNAKE_CASE , temb_channels=__SCREAMING_SNAKE_CASE , ) # out snake_case__ : Tuple = nn.GroupNorm(num_channels=block_out_channels[-1] , num_groups=__SCREAMING_SNAKE_CASE , eps=1e-6 ) snake_case__ : Tuple = nn.SiLU() snake_case__ : str = 2 * out_channels if double_z else out_channels snake_case__ : int = nn.Convad(block_out_channels[-1] , __SCREAMING_SNAKE_CASE , 3 , padding=1 ) snake_case__ : Union[str, Any] = False def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE ): snake_case__ : Optional[Any] = x snake_case__ : int = self.conv_in(__SCREAMING_SNAKE_CASE ) if self.training and self.gradient_checkpointing: def create_custom_forward(__SCREAMING_SNAKE_CASE ): def custom_forward(*__SCREAMING_SNAKE_CASE ): return module(*__SCREAMING_SNAKE_CASE ) return custom_forward # down if is_torch_version(""">=""" , """1.11.0""" ): for down_block in self.down_blocks: snake_case__ : List[Any] = torch.utils.checkpoint.checkpoint( create_custom_forward(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE , use_reentrant=__SCREAMING_SNAKE_CASE ) # middle snake_case__ : List[Any] = torch.utils.checkpoint.checkpoint( create_custom_forward(self.mid_block ) , __SCREAMING_SNAKE_CASE , use_reentrant=__SCREAMING_SNAKE_CASE ) else: for down_block in self.down_blocks: snake_case__ : Dict = torch.utils.checkpoint.checkpoint(create_custom_forward(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) # middle snake_case__ : str = torch.utils.checkpoint.checkpoint(create_custom_forward(self.mid_block ) , __SCREAMING_SNAKE_CASE ) else: # down for down_block in self.down_blocks: snake_case__ : List[str] = down_block(__SCREAMING_SNAKE_CASE ) # middle snake_case__ : str = self.mid_block(__SCREAMING_SNAKE_CASE ) # post-process snake_case__ : Any = self.conv_norm_out(__SCREAMING_SNAKE_CASE ) snake_case__ : List[str] = self.conv_act(__SCREAMING_SNAKE_CASE ) snake_case__ : str = self.conv_out(__SCREAMING_SNAKE_CASE ) return sample class __snake_case ( nn.Module ): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=("UpDecoderBlock2D",) , __SCREAMING_SNAKE_CASE=(6_4,) , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=3_2 , __SCREAMING_SNAKE_CASE="silu" , __SCREAMING_SNAKE_CASE="group" , ): super().__init__() snake_case__ : Any = layers_per_block snake_case__ : Optional[Any] = nn.Convad( __SCREAMING_SNAKE_CASE , block_out_channels[-1] , kernel_size=3 , stride=1 , padding=1 , ) snake_case__ : Union[str, Any] = None snake_case__ : Dict = nn.ModuleList([] ) snake_case__ : Optional[int] = in_channels if norm_type == """spatial""" else None # mid snake_case__ : Tuple = UNetMidBlockaD( in_channels=block_out_channels[-1] , resnet_eps=1e-6 , resnet_act_fn=__SCREAMING_SNAKE_CASE , output_scale_factor=1 , resnet_time_scale_shift="""default""" if norm_type == """group""" else norm_type , attention_head_dim=block_out_channels[-1] , resnet_groups=__SCREAMING_SNAKE_CASE , temb_channels=__SCREAMING_SNAKE_CASE , ) # up snake_case__ : List[Any] = list(reversed(__SCREAMING_SNAKE_CASE ) ) snake_case__ : Optional[Any] = reversed_block_out_channels[0] for i, up_block_type in enumerate(__SCREAMING_SNAKE_CASE ): snake_case__ : List[Any] = output_channel snake_case__ : Optional[Any] = reversed_block_out_channels[i] snake_case__ : List[str] = i == len(__SCREAMING_SNAKE_CASE ) - 1 snake_case__ : int = get_up_block( __SCREAMING_SNAKE_CASE , num_layers=self.layers_per_block + 1 , in_channels=__SCREAMING_SNAKE_CASE , out_channels=__SCREAMING_SNAKE_CASE , prev_output_channel=__SCREAMING_SNAKE_CASE , add_upsample=not is_final_block , resnet_eps=1e-6 , resnet_act_fn=__SCREAMING_SNAKE_CASE , resnet_groups=__SCREAMING_SNAKE_CASE , attention_head_dim=__SCREAMING_SNAKE_CASE , temb_channels=__SCREAMING_SNAKE_CASE , resnet_time_scale_shift=__SCREAMING_SNAKE_CASE , ) self.up_blocks.append(__SCREAMING_SNAKE_CASE ) snake_case__ : int = output_channel # out if norm_type == "spatial": snake_case__ : List[Any] = SpatialNorm(block_out_channels[0] , __SCREAMING_SNAKE_CASE ) else: snake_case__ : Any = nn.GroupNorm(num_channels=block_out_channels[0] , num_groups=__SCREAMING_SNAKE_CASE , eps=1e-6 ) snake_case__ : Tuple = nn.SiLU() snake_case__ : Union[str, Any] = nn.Convad(block_out_channels[0] , __SCREAMING_SNAKE_CASE , 3 , padding=1 ) snake_case__ : int = False def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None ): snake_case__ : Union[str, Any] = z snake_case__ : Any = self.conv_in(__SCREAMING_SNAKE_CASE ) snake_case__ : Optional[Any] = next(iter(self.up_blocks.parameters() ) ).dtype if self.training and self.gradient_checkpointing: def create_custom_forward(__SCREAMING_SNAKE_CASE ): def custom_forward(*__SCREAMING_SNAKE_CASE ): return module(*__SCREAMING_SNAKE_CASE ) return custom_forward if is_torch_version(""">=""" , """1.11.0""" ): # middle snake_case__ : int = torch.utils.checkpoint.checkpoint( create_custom_forward(self.mid_block ) , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , use_reentrant=__SCREAMING_SNAKE_CASE ) snake_case__ : int = sample.to(__SCREAMING_SNAKE_CASE ) # up for up_block in self.up_blocks: snake_case__ : List[str] = torch.utils.checkpoint.checkpoint( create_custom_forward(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , use_reentrant=__SCREAMING_SNAKE_CASE ) else: # middle snake_case__ : Dict = torch.utils.checkpoint.checkpoint( create_custom_forward(self.mid_block ) , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) snake_case__ : Optional[Any] = sample.to(__SCREAMING_SNAKE_CASE ) # up for up_block in self.up_blocks: snake_case__ : str = torch.utils.checkpoint.checkpoint(create_custom_forward(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else: # middle snake_case__ : List[Any] = self.mid_block(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) snake_case__ : List[Any] = sample.to(__SCREAMING_SNAKE_CASE ) # up for up_block in self.up_blocks: snake_case__ : Dict = up_block(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # post-process if latent_embeds is None: snake_case__ : Optional[Any] = self.conv_norm_out(__SCREAMING_SNAKE_CASE ) else: snake_case__ : str = self.conv_norm_out(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) snake_case__ : Any = self.conv_act(__SCREAMING_SNAKE_CASE ) snake_case__ : Optional[Any] = self.conv_out(__SCREAMING_SNAKE_CASE ) return sample class __snake_case ( nn.Module ): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE="random" , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=True ): super().__init__() snake_case__ : int = n_e snake_case__ : Optional[int] = vq_embed_dim snake_case__ : int = beta snake_case__ : Optional[int] = legacy snake_case__ : Dict = nn.Embedding(self.n_e , self.vq_embed_dim ) self.embedding.weight.data.uniform_(-1.0 / self.n_e , 1.0 / self.n_e ) snake_case__ : List[str] = remap if self.remap is not None: self.register_buffer("""used""" , torch.tensor(np.load(self.remap ) ) ) snake_case__ : Optional[Any] = self.used.shape[0] snake_case__ : List[str] = unknown_index # "random" or "extra" or integer if self.unknown_index == "extra": snake_case__ : Dict = self.re_embed snake_case__ : List[str] = self.re_embed + 1 print( f"Remapping {self.n_e} indices to {self.re_embed} indices. " f"Using {self.unknown_index} for unknown indices." ) else: snake_case__ : Union[str, Any] = n_e snake_case__ : str = sane_index_shape def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE ): snake_case__ : Any = inds.shape assert len(__SCREAMING_SNAKE_CASE ) > 1 snake_case__ : Dict = inds.reshape(ishape[0] , -1 ) snake_case__ : Any = self.used.to(__SCREAMING_SNAKE_CASE ) snake_case__ : Dict = (inds[:, :, None] == used[None, None, ...]).long() snake_case__ : List[Any] = match.argmax(-1 ) snake_case__ : List[str] = match.sum(2 ) < 1 if self.unknown_index == "random": snake_case__ : List[str] = torch.randint(0 , self.re_embed , size=new[unknown].shape ).to(device=new.device ) else: snake_case__ : Optional[Any] = self.unknown_index return new.reshape(__SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE ): snake_case__ : List[Any] = inds.shape assert len(__SCREAMING_SNAKE_CASE ) > 1 snake_case__ : int = inds.reshape(ishape[0] , -1 ) snake_case__ : Optional[int] = self.used.to(__SCREAMING_SNAKE_CASE ) if self.re_embed > self.used.shape[0]: # extra token snake_case__ : List[Any] = 0 # simply set to zero snake_case__ : Union[str, Any] = torch.gather(used[None, :][inds.shape[0] * [0], :] , 1 , __SCREAMING_SNAKE_CASE ) return back.reshape(__SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE ): # reshape z -> (batch, height, width, channel) and flatten snake_case__ : Any = z.permute(0 , 2 , 3 , 1 ).contiguous() snake_case__ : Optional[Any] = z.view(-1 , self.vq_embed_dim ) # distances from z to embeddings e_j (z - e)^2 = z^2 + e^2 - 2 e * z snake_case__ : Dict = torch.argmin(torch.cdist(__SCREAMING_SNAKE_CASE , self.embedding.weight ) , dim=1 ) snake_case__ : Union[str, Any] = self.embedding(__SCREAMING_SNAKE_CASE ).view(z.shape ) snake_case__ : List[str] = None snake_case__ : Union[str, Any] = None # compute loss for embedding if not self.legacy: snake_case__ : Tuple = self.beta * torch.mean((z_q.detach() - z) ** 2 ) + torch.mean((z_q - z.detach()) ** 2 ) else: snake_case__ : List[Any] = torch.mean((z_q.detach() - z) ** 2 ) + self.beta * torch.mean((z_q - z.detach()) ** 2 ) # preserve gradients snake_case__ : Any = z + (z_q - z).detach() # reshape back to match original input shape snake_case__ : Union[str, Any] = z_q.permute(0 , 3 , 1 , 2 ).contiguous() if self.remap is not None: snake_case__ : List[Any] = min_encoding_indices.reshape(z.shape[0] , -1 ) # add batch axis snake_case__ : str = self.remap_to_used(__SCREAMING_SNAKE_CASE ) snake_case__ : str = min_encoding_indices.reshape(-1 , 1 ) # flatten if self.sane_index_shape: snake_case__ : Tuple = min_encoding_indices.reshape(z_q.shape[0] , z_q.shape[2] , z_q.shape[3] ) return z_q, loss, (perplexity, min_encodings, min_encoding_indices) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): # shape specifying (batch, height, width, channel) if self.remap is not None: snake_case__ : List[Any] = indices.reshape(shape[0] , -1 ) # add batch axis snake_case__ : Optional[int] = self.unmap_to_all(__SCREAMING_SNAKE_CASE ) snake_case__ : Optional[Any] = indices.reshape(-1 ) # flatten again # get quantized latent vectors snake_case__ : int = self.embedding(__SCREAMING_SNAKE_CASE ) if shape is not None: snake_case__ : str = z_q.view(__SCREAMING_SNAKE_CASE ) # reshape back to match original input shape snake_case__ : str = z_q.permute(0 , 3 , 1 , 2 ).contiguous() return z_q class __snake_case ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=False ): snake_case__ : Tuple = parameters snake_case__ , snake_case__ : Any = torch.chunk(__SCREAMING_SNAKE_CASE , 2 , dim=1 ) snake_case__ : Union[str, Any] = torch.clamp(self.logvar , -30.0 , 20.0 ) snake_case__ : Optional[int] = deterministic snake_case__ : Optional[int] = torch.exp(0.5 * self.logvar ) snake_case__ : Any = torch.exp(self.logvar ) if self.deterministic: snake_case__ : List[str] = torch.zeros_like( self.mean , device=self.parameters.device , dtype=self.parameters.dtype ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE = None ): # make sure sample is on the same device as the parameters and has same dtype snake_case__ : Dict = randn_tensor( self.mean.shape , generator=__SCREAMING_SNAKE_CASE , device=self.parameters.device , dtype=self.parameters.dtype ) snake_case__ : Optional[int] = self.mean + self.std * sample return x def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE=None ): if self.deterministic: return torch.Tensor([0.0] ) else: if other is None: return 0.5 * torch.sum(torch.pow(self.mean , 2 ) + self.var - 1.0 - self.logvar , dim=[1, 2, 3] ) else: return 0.5 * torch.sum( torch.pow(self.mean - other.mean , 2 ) / other.var + self.var / other.var - 1.0 - self.logvar + other.logvar , dim=[1, 2, 3] , ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=[1, 2, 3] ): if self.deterministic: return torch.Tensor([0.0] ) snake_case__ : Any = np.log(2.0 * np.pi ) return 0.5 * torch.sum(logtwopi + self.logvar + torch.pow(sample - self.mean , 2 ) / self.var , dim=__SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self ): return self.mean
38
0
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) SCREAMING_SNAKE_CASE : str = {"configuration_xlnet": ["XLNET_PRETRAINED_CONFIG_ARCHIVE_MAP", "XLNetConfig"]} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE : List[str] = ["XLNetTokenizer"] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE : Any = ["XLNetTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE : Tuple = [ "XLNET_PRETRAINED_MODEL_ARCHIVE_LIST", "XLNetForMultipleChoice", "XLNetForQuestionAnswering", "XLNetForQuestionAnsweringSimple", "XLNetForSequenceClassification", "XLNetForTokenClassification", "XLNetLMHeadModel", "XLNetModel", "XLNetPreTrainedModel", "load_tf_weights_in_xlnet", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE : Union[str, Any] = [ "TF_XLNET_PRETRAINED_MODEL_ARCHIVE_LIST", "TFXLNetForMultipleChoice", "TFXLNetForQuestionAnsweringSimple", "TFXLNetForSequenceClassification", "TFXLNetForTokenClassification", "TFXLNetLMHeadModel", "TFXLNetMainLayer", "TFXLNetModel", "TFXLNetPreTrainedModel", ] if TYPE_CHECKING: from .configuration_xlnet import XLNET_PRETRAINED_CONFIG_ARCHIVE_MAP, XLNetConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xlnet import XLNetTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xlnet_fast import XLNetTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xlnet import ( XLNET_PRETRAINED_MODEL_ARCHIVE_LIST, XLNetForMultipleChoice, XLNetForQuestionAnswering, XLNetForQuestionAnsweringSimple, XLNetForSequenceClassification, XLNetForTokenClassification, XLNetLMHeadModel, XLNetModel, XLNetPreTrainedModel, load_tf_weights_in_xlnet, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_xlnet import ( TF_XLNET_PRETRAINED_MODEL_ARCHIVE_LIST, TFXLNetForMultipleChoice, TFXLNetForQuestionAnsweringSimple, TFXLNetForSequenceClassification, TFXLNetForTokenClassification, TFXLNetLMHeadModel, TFXLNetMainLayer, TFXLNetModel, TFXLNetPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE : List[str] = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
197
'''simple docstring''' import inspect import unittest from transformers import DPTConfig from transformers.file_utils import is_torch_available, is_vision_available from transformers.models.auto import get_values from transformers.testing_utils import require_torch, require_vision, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import MODEL_MAPPING, DPTForDepthEstimation, DPTForSemanticSegmentation, DPTModel from transformers.models.dpt.modeling_dpt import DPT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import DPTImageProcessor class __snake_case : '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=3_2 , __SCREAMING_SNAKE_CASE=1_6 , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=3_2 , __SCREAMING_SNAKE_CASE=4 , __SCREAMING_SNAKE_CASE=[0, 1, 2, 3] , __SCREAMING_SNAKE_CASE=4 , __SCREAMING_SNAKE_CASE=3_7 , __SCREAMING_SNAKE_CASE="gelu" , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=0.02 , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=[1, 3_8_4, 2_4, 2_4] , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=None , ): snake_case__ : str = parent snake_case__ : Union[str, Any] = batch_size snake_case__ : Union[str, Any] = image_size snake_case__ : Optional[int] = patch_size snake_case__ : List[str] = num_channels snake_case__ : Any = is_training snake_case__ : int = use_labels snake_case__ : str = hidden_size snake_case__ : Tuple = num_hidden_layers snake_case__ : str = backbone_out_indices snake_case__ : List[Any] = num_attention_heads snake_case__ : Dict = intermediate_size snake_case__ : Optional[Any] = hidden_act snake_case__ : str = hidden_dropout_prob snake_case__ : int = attention_probs_dropout_prob snake_case__ : Dict = initializer_range snake_case__ : Optional[int] = num_labels snake_case__ : str = backbone_featmap_shape snake_case__ : List[Any] = scope snake_case__ : Optional[Any] = is_hybrid # sequence length of DPT = num_patches + 1 (we add 1 for the [CLS] token) snake_case__ : List[Any] = (image_size // patch_size) ** 2 snake_case__ : Union[str, Any] = num_patches + 1 def __UpperCamelCase ( self ): snake_case__ : Tuple = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) snake_case__ : str = None if self.use_labels: snake_case__ : Dict = ids_tensor([self.batch_size, self.image_size, self.image_size] , self.num_labels ) snake_case__ : Union[str, Any] = self.get_config() return config, pixel_values, labels def __UpperCamelCase ( self ): snake_case__ : Any = { """global_padding""": """same""", """layer_type""": """bottleneck""", """depths""": [3, 4, 9], """out_features""": ["""stage1""", """stage2""", """stage3"""], """embedding_dynamic_padding""": True, """hidden_sizes""": [9_6, 1_9_2, 3_8_4, 7_6_8], """num_groups""": 2, } return DPTConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , backbone_out_indices=self.backbone_out_indices , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=__SCREAMING_SNAKE_CASE , initializer_range=self.initializer_range , is_hybrid=self.is_hybrid , backbone_config=__SCREAMING_SNAKE_CASE , backbone_featmap_shape=self.backbone_featmap_shape , ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): snake_case__ : Dict = DPTModel(config=__SCREAMING_SNAKE_CASE ) model.to(__SCREAMING_SNAKE_CASE ) model.eval() snake_case__ : Union[str, Any] = model(__SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): snake_case__ : Optional[Any] = self.num_labels snake_case__ : str = DPTForDepthEstimation(__SCREAMING_SNAKE_CASE ) model.to(__SCREAMING_SNAKE_CASE ) model.eval() snake_case__ : Optional[Any] = model(__SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.predicted_depth.shape , (self.batch_size, self.image_size, self.image_size) ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): snake_case__ : Any = self.num_labels snake_case__ : Dict = DPTForSemanticSegmentation(__SCREAMING_SNAKE_CASE ) model.to(__SCREAMING_SNAKE_CASE ) model.eval() snake_case__ : str = model(__SCREAMING_SNAKE_CASE , labels=__SCREAMING_SNAKE_CASE ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size, self.image_size) ) def __UpperCamelCase ( self ): snake_case__ : Union[str, Any] = self.prepare_config_and_inputs() snake_case__ , snake_case__ , snake_case__ : Any = config_and_inputs snake_case__ : Optional[int] = {"""pixel_values""": pixel_values} return config, inputs_dict @require_torch class __snake_case ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowerCamelCase__ = (DPTModel, DPTForDepthEstimation, DPTForSemanticSegmentation) if is_torch_available() else () lowerCamelCase__ = ( { '''depth-estimation''': DPTForDepthEstimation, '''feature-extraction''': DPTModel, '''image-segmentation''': DPTForSemanticSegmentation, } if is_torch_available() else {} ) lowerCamelCase__ = False lowerCamelCase__ = False lowerCamelCase__ = False def __UpperCamelCase ( self ): snake_case__ : List[Any] = DPTModelTester(self ) snake_case__ : Any = ConfigTester(self , config_class=__SCREAMING_SNAKE_CASE , has_text_modality=__SCREAMING_SNAKE_CASE , hidden_size=3_7 ) def __UpperCamelCase ( self ): self.config_tester.run_common_tests() @unittest.skip(reason="""DPT does not use inputs_embeds""" ) def __UpperCamelCase ( self ): pass def __UpperCamelCase ( self ): snake_case__ , snake_case__ : Tuple = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: snake_case__ : Tuple = model_class(__SCREAMING_SNAKE_CASE ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) snake_case__ : str = model.get_output_embeddings() self.assertTrue(x is None or isinstance(__SCREAMING_SNAKE_CASE , nn.Linear ) ) def __UpperCamelCase ( self ): snake_case__ , snake_case__ : Dict = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: snake_case__ : str = model_class(__SCREAMING_SNAKE_CASE ) snake_case__ : str = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic snake_case__ : List[str] = [*signature.parameters.keys()] snake_case__ : str = ["""pixel_values"""] self.assertListEqual(arg_names[:1] , __SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self ): snake_case__ : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self ): snake_case__ : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_depth_estimation(*__SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self ): snake_case__ : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_semantic_segmentation(*__SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self ): for model_class in self.all_model_classes: if model_class.__name__ == "DPTForDepthEstimation": continue snake_case__ , snake_case__ : str = self.model_tester.prepare_config_and_inputs_for_common() snake_case__ : int = True if model_class in get_values(__SCREAMING_SNAKE_CASE ): continue snake_case__ : Any = model_class(__SCREAMING_SNAKE_CASE ) model.to(__SCREAMING_SNAKE_CASE ) model.train() snake_case__ : Optional[Any] = self._prepare_for_class(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , return_labels=__SCREAMING_SNAKE_CASE ) snake_case__ : Optional[int] = model(**__SCREAMING_SNAKE_CASE ).loss loss.backward() def __UpperCamelCase ( self ): for model_class in self.all_model_classes: if model_class.__name__ == "DPTForDepthEstimation": continue snake_case__ , snake_case__ : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() snake_case__ : Union[str, Any] = False snake_case__ : str = True if model_class in get_values(__SCREAMING_SNAKE_CASE ) or not model_class.supports_gradient_checkpointing: continue snake_case__ : Any = model_class(__SCREAMING_SNAKE_CASE ) model.to(__SCREAMING_SNAKE_CASE ) model.gradient_checkpointing_enable() model.train() snake_case__ : List[str] = self._prepare_for_class(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , return_labels=__SCREAMING_SNAKE_CASE ) snake_case__ : Any = model(**__SCREAMING_SNAKE_CASE ).loss loss.backward() def __UpperCamelCase ( self ): snake_case__ , snake_case__ : str = self.model_tester.prepare_config_and_inputs_for_common() snake_case__ : str = _config_zero_init(__SCREAMING_SNAKE_CASE ) for model_class in self.all_model_classes: snake_case__ : Any = model_class(config=__SCREAMING_SNAKE_CASE ) # Skip the check for the backbone snake_case__ : str = [] for name, module in model.named_modules(): if module.__class__.__name__ == "DPTViTHybridEmbeddings": snake_case__ : Optional[int] = [f"{name}.{key}" for key in module.state_dict().keys()] break for name, param in model.named_parameters(): if param.requires_grad: if name in backbone_params: continue self.assertIn( ((param.data.mean() * 1e9).round() / 1e9).item() , [0.0, 1.0] , msg=f"Parameter {name} of model {model_class} seems not properly initialized" , ) @unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests.""" ) def __UpperCamelCase ( self ): pass @slow def __UpperCamelCase ( self ): for model_name in DPT_PRETRAINED_MODEL_ARCHIVE_LIST[1:]: snake_case__ : List[str] = DPTModel.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsNotNone(__SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self ): # We do this test only for DPTForDepthEstimation since it is the only model that uses readout_type snake_case__ , snake_case__ : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() snake_case__ : Dict = """add""" with self.assertRaises(__SCREAMING_SNAKE_CASE ): snake_case__ : List[str] = DPTForDepthEstimation(__SCREAMING_SNAKE_CASE ) def UpperCamelCase__ ( ) -> Dict: '''simple docstring''' snake_case__ : List[Any] = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) return image @require_torch @require_vision @slow class __snake_case ( unittest.TestCase ): '''simple docstring''' def __UpperCamelCase ( self ): snake_case__ : Dict = DPTImageProcessor.from_pretrained("""Intel/dpt-hybrid-midas""" ) snake_case__ : Union[str, Any] = DPTForDepthEstimation.from_pretrained("""Intel/dpt-hybrid-midas""" ).to(__SCREAMING_SNAKE_CASE ) snake_case__ : Optional[Any] = prepare_img() snake_case__ : Optional[int] = image_processor(images=__SCREAMING_SNAKE_CASE , return_tensors="""pt""" ).to(__SCREAMING_SNAKE_CASE ) # forward pass with torch.no_grad(): snake_case__ : Dict = model(**__SCREAMING_SNAKE_CASE ) snake_case__ : Tuple = outputs.predicted_depth # verify the predicted depth snake_case__ : Any = torch.Size((1, 3_8_4, 3_8_4) ) self.assertEqual(predicted_depth.shape , __SCREAMING_SNAKE_CASE ) snake_case__ : List[Any] = torch.tensor( [[[5.6437, 5.6146, 5.6511], [5.4371, 5.5649, 5.5958], [5.5215, 5.5184, 5.5293]]] ).to(__SCREAMING_SNAKE_CASE ) self.assertTrue(torch.allclose(outputs.predicted_depth[:3, :3, :3] / 1_0_0 , __SCREAMING_SNAKE_CASE , atol=1e-4 ) )
38
0
import os import re from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging _UpperCAmelCase = logging.get_logger(__name__) _UpperCAmelCase = { "vocab_file": "vocab.txt", "merges_file": "bpe.codes", } _UpperCAmelCase = { "vocab_file": { "vinai/phobert-base": "https://huggingface.co/vinai/phobert-base/resolve/main/vocab.txt", "vinai/phobert-large": "https://huggingface.co/vinai/phobert-large/resolve/main/vocab.txt", }, "merges_file": { "vinai/phobert-base": "https://huggingface.co/vinai/phobert-base/resolve/main/bpe.codes", "vinai/phobert-large": "https://huggingface.co/vinai/phobert-large/resolve/main/bpe.codes", }, } _UpperCAmelCase = { "vinai/phobert-base": 256, "vinai/phobert-large": 256, } def __UpperCamelCase (lowerCAmelCase : str ) -> str: A = set() A = word[0] for char in word[1:]: pairs.add((prev_char, char) ) A = char A = set(lowerCAmelCase ) return pairs class _UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = VOCAB_FILES_NAMES SCREAMING_SNAKE_CASE : List[Any] = PRETRAINED_VOCAB_FILES_MAP SCREAMING_SNAKE_CASE : Dict = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES def __init__( self : Union[str, Any] , UpperCamelCase__ : Tuple , UpperCamelCase__ : int , UpperCamelCase__ : Optional[int]="<s>" , UpperCamelCase__ : int="</s>" , UpperCamelCase__ : Dict="</s>" , UpperCamelCase__ : int="<s>" , UpperCamelCase__ : List[Any]="<unk>" , UpperCamelCase__ : Optional[Any]="<pad>" , UpperCamelCase__ : List[str]="<mask>" , **UpperCamelCase__ : Optional[Any] , ): super().__init__( bos_token=__SCREAMING_SNAKE_CASE , eos_token=__SCREAMING_SNAKE_CASE , unk_token=__SCREAMING_SNAKE_CASE , sep_token=__SCREAMING_SNAKE_CASE , cls_token=__SCREAMING_SNAKE_CASE , pad_token=__SCREAMING_SNAKE_CASE , mask_token=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) A = vocab_file A = merges_file A = {} A = 0 A = 1 A = 2 A = 3 self.add_from_file(__SCREAMING_SNAKE_CASE ) A = {v: k for k, v in self.encoder.items()} with open(__SCREAMING_SNAKE_CASE , encoding='utf-8' ) as merges_handle: A = merges_handle.read().split('\n' )[:-1] A = [tuple(merge.split()[:-1] ) for merge in merges] A = dict(zip(__SCREAMING_SNAKE_CASE , range(len(__SCREAMING_SNAKE_CASE ) ) ) ) A = {} def UpperCamelCase ( self : str , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : List[Any] = None ): if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] A = [self.cls_token_id] A = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def UpperCamelCase ( self : int , UpperCamelCase__ : Any , UpperCamelCase__ : Union[str, Any] = None , UpperCamelCase__ : Any = False ): if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=__SCREAMING_SNAKE_CASE , token_ids_a=__SCREAMING_SNAKE_CASE , already_has_special_tokens=__SCREAMING_SNAKE_CASE ) if token_ids_a is None: return [1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1] return [1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1, 1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1] def UpperCamelCase ( self : Any , UpperCamelCase__ : Dict , UpperCamelCase__ : Optional[Any] = None ): A = [self.sep_token_id] A = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] @property def UpperCamelCase ( self : int ): return len(self.encoder ) def UpperCamelCase ( self : List[Any] ): return dict(self.encoder , **self.added_tokens_encoder ) def UpperCamelCase ( self : str , UpperCamelCase__ : Optional[Any] ): if token in self.cache: return self.cache[token] A = tuple(__SCREAMING_SNAKE_CASE ) A = tuple(list(word[:-1] ) + [word[-1] + '</w>'] ) A = get_pairs(__SCREAMING_SNAKE_CASE ) if not pairs: return token while True: A = min(__SCREAMING_SNAKE_CASE , key=lambda UpperCamelCase__ : self.bpe_ranks.get(__SCREAMING_SNAKE_CASE , float('inf' ) ) ) if bigram not in self.bpe_ranks: break A = bigram A = [] A = 0 while i < len(__SCREAMING_SNAKE_CASE ): try: A = word.index(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) except ValueError: new_word.extend(word[i:] ) break else: new_word.extend(word[i:j] ) A = j if word[i] == first and i < len(__SCREAMING_SNAKE_CASE ) - 1 and word[i + 1] == second: new_word.append(first + second ) i += 2 else: new_word.append(word[i] ) i += 1 A = tuple(__SCREAMING_SNAKE_CASE ) A = new_word if len(__SCREAMING_SNAKE_CASE ) == 1: break else: A = get_pairs(__SCREAMING_SNAKE_CASE ) A = """@@ """.join(__SCREAMING_SNAKE_CASE ) A = word[:-4] A = word return word def UpperCamelCase ( self : Tuple , UpperCamelCase__ : List[Any] ): A = [] A = re.findall(R'\S+\n?' , __SCREAMING_SNAKE_CASE ) for token in words: split_tokens.extend(list(self.bpe(__SCREAMING_SNAKE_CASE ).split(' ' ) ) ) return split_tokens def UpperCamelCase ( self : Any , UpperCamelCase__ : int ): return self.encoder.get(__SCREAMING_SNAKE_CASE , self.encoder.get(self.unk_token ) ) def UpperCamelCase ( self : Tuple , UpperCamelCase__ : Optional[int] ): return self.decoder.get(__SCREAMING_SNAKE_CASE , self.unk_token ) def UpperCamelCase ( self : Union[str, Any] , UpperCamelCase__ : Union[str, Any] ): A = """ """.join(__SCREAMING_SNAKE_CASE ).replace('@@ ' , '' ).strip() return out_string def UpperCamelCase ( self : int , UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : str = None ): if not os.path.isdir(__SCREAMING_SNAKE_CASE ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' ) return A = os.path.join( __SCREAMING_SNAKE_CASE , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) A = os.path.join( __SCREAMING_SNAKE_CASE , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['merges_file'] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(__SCREAMING_SNAKE_CASE ): copyfile(self.vocab_file , __SCREAMING_SNAKE_CASE ) if os.path.abspath(self.merges_file ) != os.path.abspath(__SCREAMING_SNAKE_CASE ): copyfile(self.merges_file , __SCREAMING_SNAKE_CASE ) return out_vocab_file, out_merge_file def UpperCamelCase ( self : int , UpperCamelCase__ : List[Any] ): if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): try: with open(__SCREAMING_SNAKE_CASE , 'r' , encoding='utf-8' ) as fd: self.add_from_file(__SCREAMING_SNAKE_CASE ) except FileNotFoundError as fnfe: raise fnfe except UnicodeError: raise Exception(f'''Incorrect encoding detected in {f}, please rebuild the dataset''' ) return A = f.readlines() for lineTmp in lines: A = lineTmp.strip() A = line.rfind(' ' ) if idx == -1: raise ValueError('Incorrect dictionary format, expected \'<token> <cnt>\'' ) A = line[:idx] A = len(self.encoder )
699
'''simple docstring''' # Copyright 2021 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import json import os from ...utils.constants import SAGEMAKER_PARALLEL_EC2_INSTANCES, TORCH_DYNAMO_MODES from ...utils.dataclasses import ComputeEnvironment, SageMakerDistributedType from ...utils.imports import is_botoa_available from .config_args import SageMakerConfig from .config_utils import ( DYNAMO_BACKENDS, _ask_field, _ask_options, _convert_dynamo_backend, _convert_mixed_precision, _convert_sagemaker_distributed_mode, _convert_yes_no_to_bool, ) if is_botoa_available(): import botoa # noqa: F401 def UpperCamelCase__ ( __magic_name__ : Optional[Any] ) -> Dict: '''simple docstring''' snake_case__ : int = botoa.client("""iam""" ) snake_case__ : Union[str, Any] = { """Version""": """2012-10-17""", """Statement""": [ {"""Effect""": """Allow""", """Principal""": {"""Service""": """sagemaker.amazonaws.com"""}, """Action""": """sts:AssumeRole"""} ], } try: # create the role, associated with the chosen trust policy iam_client.create_role( RoleName=__magic_name__ , AssumeRolePolicyDocument=json.dumps(__magic_name__ , indent=2 ) ) snake_case__ : Dict = { """Version""": """2012-10-17""", """Statement""": [ { """Effect""": """Allow""", """Action""": [ """sagemaker:*""", """ecr:GetDownloadUrlForLayer""", """ecr:BatchGetImage""", """ecr:BatchCheckLayerAvailability""", """ecr:GetAuthorizationToken""", """cloudwatch:PutMetricData""", """cloudwatch:GetMetricData""", """cloudwatch:GetMetricStatistics""", """cloudwatch:ListMetrics""", """logs:CreateLogGroup""", """logs:CreateLogStream""", """logs:DescribeLogStreams""", """logs:PutLogEvents""", """logs:GetLogEvents""", """s3:CreateBucket""", """s3:ListBucket""", """s3:GetBucketLocation""", """s3:GetObject""", """s3:PutObject""", ], """Resource""": """*""", } ], } # attach policy to role iam_client.put_role_policy( RoleName=__magic_name__ , PolicyName=f"{role_name}_policy_permission" , PolicyDocument=json.dumps(__magic_name__ , indent=2 ) , ) except iam_client.exceptions.EntityAlreadyExistsException: print(f"role {role_name} already exists. Using existing one" ) def UpperCamelCase__ ( __magic_name__ : Any ) -> Tuple: '''simple docstring''' snake_case__ : List[str] = botoa.client("""iam""" ) return iam_client.get_role(RoleName=__magic_name__ )["Role"]["Arn"] def UpperCamelCase__ ( ) -> Tuple: '''simple docstring''' snake_case__ : Union[str, Any] = _ask_options( """How do you want to authorize?""" , ["""AWS Profile""", """Credentials (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) """] , __magic_name__ , ) snake_case__ : List[Any] = None if credentials_configuration == 0: snake_case__ : Dict = _ask_field("""Enter your AWS Profile name: [default] """ , default="""default""" ) snake_case__ : List[str] = aws_profile else: print( """Note you will need to provide AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY when you launch you training script with,""" """`accelerate launch --aws_access_key_id XXX --aws_secret_access_key YYY`""" ) snake_case__ : List[str] = _ask_field("""AWS Access Key ID: """ ) snake_case__ : int = aws_access_key_id snake_case__ : Optional[Any] = _ask_field("""AWS Secret Access Key: """ ) snake_case__ : List[str] = aws_secret_access_key snake_case__ : Tuple = _ask_field("""Enter your AWS Region: [us-east-1]""" , default="""us-east-1""" ) snake_case__ : Optional[int] = aws_region snake_case__ : int = _ask_options( """Do you already have an IAM Role for executing Amazon SageMaker Training Jobs?""" , ["""Provide IAM Role name""", """Create new IAM role using credentials"""] , __magic_name__ , ) if role_management == 0: snake_case__ : Optional[Any] = _ask_field("""Enter your IAM role name: """ ) else: snake_case__ : Optional[int] = """accelerate_sagemaker_execution_role""" print(f"Accelerate will create an iam role \"{iam_role_name}\" using the provided credentials" ) _create_iam_role_for_sagemaker(__magic_name__ ) snake_case__ : Dict = _ask_field( """Do you want to use custom Docker image? [yes/NO]: """ , _convert_yes_no_to_bool , default=__magic_name__ , error_message="""Please enter yes or no.""" , ) snake_case__ : Any = None if is_custom_docker_image: snake_case__ : str = _ask_field("""Enter your Docker image: """ , lambda __magic_name__ : str(__magic_name__ ).lower() ) snake_case__ : Tuple = _ask_field( """Do you want to provide SageMaker input channels with data locations? [yes/NO]: """ , _convert_yes_no_to_bool , default=__magic_name__ , error_message="""Please enter yes or no.""" , ) snake_case__ : List[Any] = None if is_sagemaker_inputs_enabled: snake_case__ : str = _ask_field( """Enter the path to the SageMaker inputs TSV file with columns (channel_name, data_location): """ , lambda __magic_name__ : str(__magic_name__ ).lower() , ) snake_case__ : Optional[int] = _ask_field( """Do you want to enable SageMaker metrics? [yes/NO]: """ , _convert_yes_no_to_bool , default=__magic_name__ , error_message="""Please enter yes or no.""" , ) snake_case__ : Optional[Any] = None if is_sagemaker_metrics_enabled: snake_case__ : List[Any] = _ask_field( """Enter the path to the SageMaker metrics TSV file with columns (metric_name, metric_regex): """ , lambda __magic_name__ : str(__magic_name__ ).lower() , ) snake_case__ : Tuple = _ask_options( """What is the distributed mode?""" , ["""No distributed training""", """Data parallelism"""] , _convert_sagemaker_distributed_mode , ) snake_case__ : Any = {} snake_case__ : List[Any] = _ask_field( """Do you wish to optimize your script with torch dynamo?[yes/NO]:""" , _convert_yes_no_to_bool , default=__magic_name__ , error_message="""Please enter yes or no.""" , ) if use_dynamo: snake_case__ : str = """dynamo_""" snake_case__ : Tuple = _ask_options( """Which dynamo backend would you like to use?""" , [x.lower() for x in DYNAMO_BACKENDS] , _convert_dynamo_backend , default=2 , ) snake_case__ : List[str] = _ask_field( """Do you want to customize the defaults sent to torch.compile? [yes/NO]: """ , _convert_yes_no_to_bool , default=__magic_name__ , error_message="""Please enter yes or no.""" , ) if use_custom_options: snake_case__ : str = _ask_options( """Which mode do you want to use?""" , __magic_name__ , lambda __magic_name__ : TORCH_DYNAMO_MODES[int(__magic_name__ )] , default="""default""" , ) snake_case__ : Union[str, Any] = _ask_field( """Do you want the fullgraph mode or it is ok to break model into several subgraphs? [yes/NO]: """ , _convert_yes_no_to_bool , default=__magic_name__ , error_message="""Please enter yes or no.""" , ) snake_case__ : str = _ask_field( """Do you want to enable dynamic shape tracing? [yes/NO]: """ , _convert_yes_no_to_bool , default=__magic_name__ , error_message="""Please enter yes or no.""" , ) snake_case__ : Dict = """Which EC2 instance type you want to use for your training?""" if distributed_type != SageMakerDistributedType.NO: snake_case__ : List[str] = _ask_options( __magic_name__ , __magic_name__ , lambda __magic_name__ : SAGEMAKER_PARALLEL_EC2_INSTANCES[int(__magic_name__ )] ) else: eca_instance_query += "? [ml.p3.2xlarge]:" snake_case__ : Optional[int] = _ask_field(__magic_name__ , lambda __magic_name__ : str(__magic_name__ ).lower() , default="""ml.p3.2xlarge""" ) snake_case__ : Dict = 1 if distributed_type in (SageMakerDistributedType.DATA_PARALLEL, SageMakerDistributedType.MODEL_PARALLEL): snake_case__ : Optional[Any] = _ask_field( """How many machines do you want use? [1]: """ , __magic_name__ , default=1 , ) snake_case__ : Union[str, Any] = _ask_options( """Do you wish to use FP16 or BF16 (mixed precision)?""" , ["""no""", """fp16""", """bf16""", """fp8"""] , _convert_mixed_precision , ) if use_dynamo and mixed_precision == "no": print( """Torch dynamo used without mixed precision requires TF32 to be efficient. Accelerate will enable it by default when launching your scripts.""" ) return SageMakerConfig( image_uri=__magic_name__ , compute_environment=ComputeEnvironment.AMAZON_SAGEMAKER , distributed_type=__magic_name__ , use_cpu=__magic_name__ , dynamo_config=__magic_name__ , eca_instance_type=__magic_name__ , profile=__magic_name__ , region=__magic_name__ , iam_role_name=__magic_name__ , mixed_precision=__magic_name__ , num_machines=__magic_name__ , sagemaker_inputs_file=__magic_name__ , sagemaker_metrics_file=__magic_name__ , )
38
0
import numpy as np snake_case__ : str = [ ["a", "b", "c", "d", "e"], ["f", "g", "h", "i", "k"], ["l", "m", "n", "o", "p"], ["q", "r", "s", "t", "u"], ["v", "w", "x", "y", "z"], ] class _A : '''simple docstring''' def __init__( self : Union[str, Any] ): '''simple docstring''' __lowercase = np.array(__SCREAMING_SNAKE_CASE ) def _snake_case ( self : str , lowerCamelCase : Optional[int] ): '''simple docstring''' __lowercase = np.where(letter == self.SQUARE ) __lowercase = np.concatenate([indexa + 1, indexa + 1] ) return indexes def _snake_case ( self : Tuple , lowerCamelCase : Any , lowerCamelCase : Optional[int] ): '''simple docstring''' __lowercase = self.SQUARE[indexa - 1, indexa - 1] return letter def _snake_case ( self : Union[str, Any] , lowerCamelCase : Dict ): '''simple docstring''' __lowercase = message.lower() __lowercase = message.replace(" " , "" ) __lowercase = message.replace("j" , "i" ) __lowercase = np.empty((2, len(__SCREAMING_SNAKE_CASE )) ) for letter_index in range(len(__SCREAMING_SNAKE_CASE ) ): __lowercase = self.letter_to_numbers(message[letter_index] ) __lowercase = numbers[0] __lowercase = numbers[1] __lowercase = first_step.reshape(2 * len(__SCREAMING_SNAKE_CASE ) ) __lowercase = """""" for numbers_index in range(len(__SCREAMING_SNAKE_CASE ) ): __lowercase = int(second_step[numbers_index * 2] ) __lowercase = int(second_step[(numbers_index * 2) + 1] ) __lowercase = self.numbers_to_letter(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) __lowercase = encoded_message + letter return encoded_message def _snake_case ( self : Tuple , lowerCamelCase : Optional[Any] ): '''simple docstring''' __lowercase = message.lower() message.replace(" " , "" ) __lowercase = np.empty(2 * len(__SCREAMING_SNAKE_CASE ) ) for letter_index in range(len(__SCREAMING_SNAKE_CASE ) ): __lowercase = self.letter_to_numbers(message[letter_index] ) __lowercase = numbers[0] __lowercase = numbers[1] __lowercase = first_step.reshape((2, len(__SCREAMING_SNAKE_CASE )) ) __lowercase = """""" for numbers_index in range(len(__SCREAMING_SNAKE_CASE ) ): __lowercase = int(second_step[0, numbers_index] ) __lowercase = int(second_step[1, numbers_index] ) __lowercase = self.numbers_to_letter(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) __lowercase = decoded_message + letter return decoded_message
402
'''simple docstring''' from itertools import zip_longest import requests from bsa import BeautifulSoup from pandas import DataFrame def UpperCamelCase__ ( __magic_name__ : str = "laptop" ) -> DataFrame: '''simple docstring''' snake_case__ : Union[str, Any] = f"https://www.amazon.in/laptop/s?k={product}" snake_case__ : List[str] = { """User-Agent""": """Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)Chrome/44.0.2403.157 Safari/537.36""", """Accept-Language""": """en-US, en;q=0.5""", } snake_case__ : int = BeautifulSoup(requests.get(__magic_name__ , headers=__magic_name__ ).text ) # Initialize a Pandas dataframe with the column titles snake_case__ : Optional[Any] = DataFrame( columns=[ """Product Title""", """Product Link""", """Current Price of the product""", """Product Rating""", """MRP of the product""", """Discount""", ] ) # Loop through each entry and store them in the dataframe for item, _ in zip_longest( soup.find_all( """div""" , attrs={"""class""": """s-result-item""", """data-component-type""": """s-search-result"""} , ) , soup.find_all("""div""" , attrs={"""class""": """a-row a-size-base a-color-base"""} ) , ): try: snake_case__ : Optional[int] = item.ha.text snake_case__ : Any = """https://www.amazon.in/""" + item.ha.a["""href"""] snake_case__ : List[str] = item.find("""span""" , attrs={"""class""": """a-offscreen"""} ).text try: snake_case__ : Dict = item.find("""span""" , attrs={"""class""": """a-icon-alt"""} ).text except AttributeError: snake_case__ : Optional[int] = """Not available""" try: snake_case__ : Tuple = ( """₹""" + item.find( """span""" , attrs={"""class""": """a-price a-text-price"""} ).text.split("""₹""" )[1] ) except AttributeError: snake_case__ : Optional[Any] = """""" try: snake_case__ : str = float( ( ( float(product_mrp.strip("""₹""" ).replace(""",""" , """""" ) ) - float(product_price.strip("""₹""" ).replace(""",""" , """""" ) ) ) / float(product_mrp.strip("""₹""" ).replace(""",""" , """""" ) ) ) * 1_00 ) except ValueError: snake_case__ : List[Any] = float("""nan""" ) except AttributeError: pass snake_case__ : str = [ product_title, product_link, product_price, product_rating, product_mrp, discount, ] snake_case__ : List[Any] = """ """ snake_case__ : Union[str, Any] = """ """ data_frame.index += 1 return data_frame if __name__ == "__main__": A_ : int = "headphones" get_amazon_product_data(product).to_csv(F'Amazon Product Data for {product}.csv')
38
0
from __future__ import annotations import math def UpperCAmelCase ( UpperCAmelCase ,UpperCAmelCase )-> float: '''simple docstring''' SCREAMING_SNAKE_CASE_ = u for i in range(1 ,UpperCAmelCase ): SCREAMING_SNAKE_CASE_ = temp * (u - i) return temp def UpperCAmelCase ( )-> None: '''simple docstring''' SCREAMING_SNAKE_CASE_ = int(input('''enter the numbers of values: ''' ) ) SCREAMING_SNAKE_CASE_ = [] for _ in range(UpperCAmelCase ): y.append([] ) for i in range(UpperCAmelCase ): for j in range(UpperCAmelCase ): y[i].append(UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = 0 print('''enter the values of parameters in a list: ''' ) SCREAMING_SNAKE_CASE_ = list(map(UpperCAmelCase ,input().split() ) ) print('''enter the values of corresponding parameters: ''' ) for i in range(UpperCAmelCase ): SCREAMING_SNAKE_CASE_ = float(input() ) SCREAMING_SNAKE_CASE_ = int(input('''enter the value to interpolate: ''' ) ) SCREAMING_SNAKE_CASE_ = (value - x[0]) / (x[1] - x[0]) # for calculating forward difference table for i in range(1 ,UpperCAmelCase ): for j in range(n - i ): SCREAMING_SNAKE_CASE_ = y[j + 1][i - 1] - y[j][i - 1] SCREAMING_SNAKE_CASE_ = y[0][0] for i in range(1 ,UpperCAmelCase ): summ += (ucal(UpperCAmelCase ,UpperCAmelCase ) * y[0][i]) / math.factorial(UpperCAmelCase ) print(f'''the value at {value} is {summ}''' ) if __name__ == "__main__": main()
393
'''simple docstring''' import itertools import json import os import unittest from transformers import AddedToken, LongformerTokenizer, LongformerTokenizerFast from transformers.models.longformer.tokenization_longformer import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class __snake_case ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowerCamelCase__ = LongformerTokenizer lowerCamelCase__ = True lowerCamelCase__ = LongformerTokenizerFast lowerCamelCase__ = True def __UpperCamelCase ( self ): super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt snake_case__ : Union[str, Any] = [ """l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """\u0120""", """\u0120l""", """\u0120n""", """\u0120lo""", """\u0120low""", """er""", """\u0120lowest""", """\u0120newer""", """\u0120wider""", """<unk>""", ] snake_case__ : Optional[int] = dict(zip(__SCREAMING_SNAKE_CASE , range(len(__SCREAMING_SNAKE_CASE ) ) ) ) snake_case__ : int = ["""#version: 0.2""", """\u0120 l""", """\u0120l o""", """\u0120lo w""", """e r""", """"""] snake_case__ : Any = {"""unk_token""": """<unk>"""} snake_case__ : List[Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""vocab_file"""] ) snake_case__ : 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(__SCREAMING_SNAKE_CASE ) + """\n""" ) with open(self.merges_file , """w""" , encoding="""utf-8""" ) as fp: fp.write("""\n""".join(__SCREAMING_SNAKE_CASE ) ) def __UpperCamelCase ( self , **__SCREAMING_SNAKE_CASE ): kwargs.update(self.special_tokens_map ) return self.tokenizer_class.from_pretrained(self.tmpdirname , **__SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self , **__SCREAMING_SNAKE_CASE ): kwargs.update(self.special_tokens_map ) return self.rust_tokenizer_class.from_pretrained(self.tmpdirname , **__SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE ): snake_case__ : str = """lower newer""" snake_case__ : Dict = """lower newer""" return input_text, output_text def __UpperCamelCase ( self ): snake_case__ : int = self.tokenizer_class(self.vocab_file , self.merges_file , **self.special_tokens_map ) snake_case__ : Tuple = """lower newer""" snake_case__ : Optional[Any] = ["""l""", """o""", """w""", """er""", """\u0120""", """n""", """e""", """w""", """er"""] snake_case__ : Tuple = tokenizer.tokenize(__SCREAMING_SNAKE_CASE ) # , add_prefix_space=True) self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) snake_case__ : Tuple = tokens + [tokenizer.unk_token] snake_case__ : List[Any] = [0, 1, 2, 1_5, 1_0, 9, 3, 2, 1_5, 1_9] self.assertListEqual(tokenizer.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self ): snake_case__ : Tuple = self.get_tokenizer() self.assertListEqual(tokenizer.encode("""Hello world!""" , add_special_tokens=__SCREAMING_SNAKE_CASE ) , [0, 3_1_4_1_4, 2_3_2, 3_2_8, 2] ) self.assertListEqual( tokenizer.encode("""Hello world! cécé herlolip 418""" , add_special_tokens=__SCREAMING_SNAKE_CASE ) , [0, 3_1_4_1_4, 2_3_2, 3_2_8, 7_4_0, 1_1_4_0, 1_2_6_9_5, 6_9, 4_6_0_7_8, 1_5_8_8, 2] , ) @slow def __UpperCamelCase ( self ): snake_case__ : List[Any] = self.tokenizer_class.from_pretrained("""allenai/longformer-base-4096""" ) snake_case__ : int = tokenizer.encode("""sequence builders""" , add_special_tokens=__SCREAMING_SNAKE_CASE ) snake_case__ : Dict = tokenizer.encode("""multi-sequence build""" , add_special_tokens=__SCREAMING_SNAKE_CASE ) snake_case__ : Optional[Any] = tokenizer.encode( """sequence builders""" , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) snake_case__ : Dict = tokenizer.encode( """sequence builders""" , """multi-sequence build""" , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) snake_case__ : Dict = tokenizer.build_inputs_with_special_tokens(__SCREAMING_SNAKE_CASE ) snake_case__ : Optional[int] = tokenizer.build_inputs_with_special_tokens(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) assert encoded_sentence == encoded_text_from_decode assert encoded_pair == encoded_pair_from_decode def __UpperCamelCase ( self ): snake_case__ : Optional[int] = self.get_tokenizer() snake_case__ : int = """Encode this sequence.""" snake_case__ : Union[str, Any] = tokenizer.byte_encoder[""" """.encode("""utf-8""" )[0]] # Testing encoder arguments snake_case__ : Optional[int] = tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) snake_case__ : Tuple = tokenizer.convert_ids_to_tokens(encoded[0] )[0] self.assertNotEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) snake_case__ : Optional[Any] = tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) snake_case__ : List[str] = tokenizer.convert_ids_to_tokens(encoded[0] )[0] self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) tokenizer.add_special_tokens({"""bos_token""": """<s>"""} ) snake_case__ : List[str] = tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) snake_case__ : str = tokenizer.convert_ids_to_tokens(encoded[1] )[0] self.assertNotEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # Testing spaces after special tokens snake_case__ : List[str] = """<mask>""" tokenizer.add_special_tokens( {"""mask_token""": AddedToken(__SCREAMING_SNAKE_CASE , lstrip=__SCREAMING_SNAKE_CASE , rstrip=__SCREAMING_SNAKE_CASE )} ) # mask token has a left space snake_case__ : Dict = tokenizer.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) snake_case__ : str = """Encode <mask> sequence""" snake_case__ : Tuple = """Encode <mask>sequence""" snake_case__ : Union[str, Any] = tokenizer.encode(__SCREAMING_SNAKE_CASE ) snake_case__ : List[str] = encoded.index(__SCREAMING_SNAKE_CASE ) snake_case__ : Optional[int] = tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1] )[0] self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) snake_case__ : Tuple = tokenizer.encode(__SCREAMING_SNAKE_CASE ) snake_case__ : str = encoded.index(__SCREAMING_SNAKE_CASE ) snake_case__ : List[Any] = tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1] )[0] self.assertNotEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self ): pass def __UpperCamelCase ( self ): for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f"{tokenizer.__class__.__name__} ({pretrained_name})" ): snake_case__ : List[Any] = self.rust_tokenizer_class.from_pretrained(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) snake_case__ : Any = self.tokenizer_class.from_pretrained(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) snake_case__ : List[str] = """A, <mask> AllenNLP sentence.""" snake_case__ : str = tokenizer_r.encode_plus(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , return_token_type_ids=__SCREAMING_SNAKE_CASE ) snake_case__ : Tuple = tokenizer_p.encode_plus(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , return_token_type_ids=__SCREAMING_SNAKE_CASE ) # token_type_ids should put 0 everywhere self.assertEqual(sum(tokens_r["""token_type_ids"""] ) , sum(tokens_p["""token_type_ids"""] ) ) # attention_mask should put 1 everywhere, so sum over length should be 1 self.assertEqual( sum(tokens_r["""attention_mask"""] ) / len(tokens_r["""attention_mask"""] ) , sum(tokens_p["""attention_mask"""] ) / len(tokens_p["""attention_mask"""] ) , ) snake_case__ : Union[str, Any] = tokenizer_r.convert_ids_to_tokens(tokens_r["""input_ids"""] ) snake_case__ : Dict = tokenizer_p.convert_ids_to_tokens(tokens_p["""input_ids"""] ) # Rust correctly handles the space before the mask while python doesnt self.assertSequenceEqual(tokens_p["""input_ids"""] , [0, 2_5_0, 6, 5_0_2_6_4, 3_8_2_3, 4_8_7, 2_1_9_9_2, 3_6_4_5, 4, 2] ) self.assertSequenceEqual(tokens_r["""input_ids"""] , [0, 2_5_0, 6, 5_0_2_6_4, 3_8_2_3, 4_8_7, 2_1_9_9_2, 3_6_4_5, 4, 2] ) self.assertSequenceEqual( __SCREAMING_SNAKE_CASE , ["""<s>""", """A""", """,""", """<mask>""", """ĠAllen""", """N""", """LP""", """Ġsentence""", """.""", """</s>"""] ) self.assertSequenceEqual( __SCREAMING_SNAKE_CASE , ["""<s>""", """A""", """,""", """<mask>""", """ĠAllen""", """N""", """LP""", """Ġsentence""", """.""", """</s>"""] ) def __UpperCamelCase ( self ): for trim_offsets, add_prefix_space in itertools.product([True, False] , repeat=2 ): snake_case__ : Any = self.rust_tokenizer_class.from_pretrained( self.tmpdirname , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) snake_case__ : Dict = json.loads(tokenizer_r.backend_tokenizer.pre_tokenizer.__getstate__() ) snake_case__ : List[str] = json.loads(tokenizer_r.backend_tokenizer.post_processor.__getstate__() ) self.assertEqual(pre_tokenizer_state["""add_prefix_space"""] , __SCREAMING_SNAKE_CASE ) self.assertEqual(post_processor_state["""add_prefix_space"""] , __SCREAMING_SNAKE_CASE ) self.assertEqual(post_processor_state["""trim_offsets"""] , __SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self ): # Test which aims to verify that the offsets are well adapted to the argument `add_prefix_space` and # `trim_offsets` for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f"{tokenizer.__class__.__name__} ({pretrained_name})" ): snake_case__ : Union[str, Any] = """hello""" # `hello` is a token in the vocabulary of `pretrained_name` snake_case__ : Any = f"{text_of_1_token} {text_of_1_token}" snake_case__ : Union[str, Any] = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) snake_case__ : Union[str, Any] = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (len(__SCREAMING_SNAKE_CASE ) + 1, len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) snake_case__ : List[Any] = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) snake_case__ : str = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (len(__SCREAMING_SNAKE_CASE ) + 1, len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) snake_case__ : Optional[Any] = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) snake_case__ : str = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (len(__SCREAMING_SNAKE_CASE ), len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) snake_case__ : Tuple = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) snake_case__ : List[Any] = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (len(__SCREAMING_SNAKE_CASE ), len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) snake_case__ : Optional[Any] = f" {text}" # tokenizer_r = self.rust_tokenizer_class.from_pretrained( # pretrained_name, use_fast=True, add_prefix_space=True, trim_offsets=True # ) # encoding = tokenizer_r(text, return_offsets_mapping=True, add_special_tokens=False) # self.assertEqual(encoding.offset_mapping[0], (1, 1 + len(text_of_1_token))) # self.assertEqual( # encoding.offset_mapping[1], # (1 + len(text_of_1_token) + 1, 1 + len(text_of_1_token) + 1 + len(text_of_1_token)), # ) snake_case__ : Dict = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) snake_case__ : Optional[Any] = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (1, 1 + len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(__SCREAMING_SNAKE_CASE ) + 1, 1 + len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) snake_case__ : Any = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) snake_case__ : Any = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, 1 + len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(__SCREAMING_SNAKE_CASE ), 1 + len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) snake_case__ : List[Any] = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) snake_case__ : List[Any] = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, 1 + len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(__SCREAMING_SNAKE_CASE ), 1 + len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , )
38
0
"""simple docstring""" def UpperCamelCase ( ) ->Union[str, Any]: _lowerCamelCase : Any = [] _lowerCamelCase : Tuple = 1 while len(SCREAMING_SNAKE_CASE_ ) < 1e6: constant.append(str(SCREAMING_SNAKE_CASE_ ) ) i += 1 _lowerCamelCase : Optional[Any] = """""".join(SCREAMING_SNAKE_CASE_ ) return ( int(constant[0] ) * int(constant[9] ) * int(constant[99] ) * int(constant[999] ) * int(constant[9999] ) * int(constant[9_9999] ) * int(constant[99_9999] ) ) if __name__ == "__main__": print(solution())
434
'''simple docstring''' from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices A_ : int = logging.get_logger(__name__) A_ : Any = { "microsoft/resnet-50": "https://huggingface.co/microsoft/resnet-50/blob/main/config.json", } class __snake_case ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): '''simple docstring''' lowerCamelCase__ = '''resnet''' lowerCamelCase__ = ['''basic''', '''bottleneck'''] def __init__( self , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=6_4 , __SCREAMING_SNAKE_CASE=[2_5_6, 5_1_2, 1_0_2_4, 2_0_4_8] , __SCREAMING_SNAKE_CASE=[3, 4, 6, 3] , __SCREAMING_SNAKE_CASE="bottleneck" , __SCREAMING_SNAKE_CASE="relu" , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE , ): super().__init__(**__SCREAMING_SNAKE_CASE ) if layer_type not in self.layer_types: raise ValueError(f"layer_type={layer_type} is not one of {','.join(self.layer_types )}" ) snake_case__ : List[Any] = num_channels snake_case__ : str = embedding_size snake_case__ : List[Any] = hidden_sizes snake_case__ : Dict = depths snake_case__ : List[Any] = layer_type snake_case__ : int = hidden_act snake_case__ : Union[str, Any] = downsample_in_first_stage snake_case__ : Dict = ["""stem"""] + [f"stage{idx}" for idx in range(1 , len(__SCREAMING_SNAKE_CASE ) + 1 )] snake_case__ , snake_case__ : Any = get_aligned_output_features_output_indices( out_features=__SCREAMING_SNAKE_CASE , out_indices=__SCREAMING_SNAKE_CASE , stage_names=self.stage_names ) class __snake_case ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' lowerCamelCase__ = version.parse('''1.11''' ) @property def __UpperCamelCase ( self ): return OrderedDict( [ ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}), ] ) @property def __UpperCamelCase ( self ): return 1e-3
38
0
import json import os import subprocess import unittest from ast import literal_eval import pytest from parameterized import parameterized_class from . import is_sagemaker_available if is_sagemaker_available(): from sagemaker import Session, TrainingJobAnalytics from sagemaker.huggingface import HuggingFace @pytest.mark.skipif( literal_eval(os.getenv('''TEST_SAGEMAKER''' , '''False''' ) ) is not True , reason='''Skipping test because should only be run when releasing minor transformers version''' , ) @pytest.mark.usefixtures('''sm_env''' ) @parameterized_class( [ { '''framework''': '''pytorch''', '''script''': '''run_glue.py''', '''model_name_or_path''': '''distilbert-base-cased''', '''instance_type''': '''ml.g4dn.xlarge''', '''results''': {'''train_runtime''': 6_5_0, '''eval_accuracy''': 0.6, '''eval_loss''': 0.9}, }, { '''framework''': '''tensorflow''', '''script''': '''run_tf.py''', '''model_name_or_path''': '''distilbert-base-cased''', '''instance_type''': '''ml.g4dn.xlarge''', '''results''': {'''train_runtime''': 6_0_0, '''eval_accuracy''': 0.3, '''eval_loss''': 0.9}, }, ] ) class snake_case_ (unittest.TestCase ): def lowerCamelCase__( self :Tuple ) -> str: if self.framework == "pytorch": subprocess.run( F'cp ./examples/pytorch/text-classification/run_glue.py {self.env.test_path}/run_glue.py'.split() ,encoding='utf-8' ,check=__SCREAMING_SNAKE_CASE ,) assert hasattr(self ,'env' ) def lowerCamelCase__( self :List[Any] ,__snake_case :Union[str, Any]=1 ) -> Optional[int]: # creates estimator return HuggingFace( entry_point=self.script ,source_dir=self.env.test_path ,role=self.env.role ,image_uri=self.env.image_uri ,base_job_name=F'{self.env.base_job_name}-single' ,instance_count=__SCREAMING_SNAKE_CASE ,instance_type=self.instance_type ,debugger_hook_config=__SCREAMING_SNAKE_CASE ,hyperparameters={**self.env.hyperparameters, 'model_name_or_path': self.model_name_or_path} ,metric_definitions=self.env.metric_definitions ,py_version='py36' ,) def lowerCamelCase__( self :List[str] ,__snake_case :Union[str, Any] ) -> Union[str, Any]: TrainingJobAnalytics(__SCREAMING_SNAKE_CASE ).export_csv(F'{self.env.test_path}/{job_name}_metrics.csv' ) def lowerCamelCase__( self :List[str] ) -> Optional[int]: # create estimator a__ = self.create_estimator() # run training estimator.fit() # result dataframe a__ = TrainingJobAnalytics(estimator.latest_training_job.name ).dataframe() # extract kpis a__ = list(result_metrics_df[result_metrics_df.metric_name == 'eval_accuracy']['value'] ) a__ = list(result_metrics_df[result_metrics_df.metric_name == 'eval_loss']['value'] ) # get train time from SageMaker job, this includes starting, preprocessing, stopping a__ = ( Session().describe_training_job(estimator.latest_training_job.name ).get('TrainingTimeInSeconds' ,99_99_99 ) ) # assert kpis assert train_runtime <= self.results["train_runtime"] assert all(t >= self.results['eval_accuracy'] for t in eval_accuracy ) assert all(t <= self.results['eval_loss'] for t in eval_loss ) # dump tests result into json file to share in PR with open(F'{estimator.latest_training_job.name}.json' ,'w' ) as outfile: json.dump({'train_time': train_runtime, 'eval_accuracy': eval_accuracy, 'eval_loss': eval_loss} ,__SCREAMING_SNAKE_CASE )
335
'''simple docstring''' # limitations under the License. # NOTE: This file is deprecated and will be removed in a future version. # It only exists so that temporarely `from diffusers.pipelines import DiffusionPipeline` works from .pipelines import DiffusionPipeline, ImagePipelineOutput # noqa: F401 from .utils import deprecate deprecate( "pipelines_utils", "0.22.0", "Importing `DiffusionPipeline` or `ImagePipelineOutput` from diffusers.pipeline_utils is deprecated. Please import from diffusers.pipelines.pipeline_utils instead.", standard_warn=False, stacklevel=3, )
38
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available A__ : Optional[int] = { "configuration_rag": ["RagConfig"], "retrieval_rag": ["RagRetriever"], "tokenization_rag": ["RagTokenizer"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A__ : List[Any] = [ "RagModel", "RagPreTrainedModel", "RagSequenceForGeneration", "RagTokenForGeneration", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A__ : Tuple = [ "TFRagModel", "TFRagPreTrainedModel", "TFRagSequenceForGeneration", "TFRagTokenForGeneration", ] if TYPE_CHECKING: from .configuration_rag import RagConfig from .retrieval_rag import RagRetriever from .tokenization_rag import RagTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_rag import RagModel, RagPreTrainedModel, RagSequenceForGeneration, RagTokenForGeneration try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_rag import ( TFRagModel, TFRagPreTrainedModel, TFRagSequenceForGeneration, TFRagTokenForGeneration, ) else: import sys A__ : str = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
233
'''simple docstring''' import shutil import tempfile import unittest from unittest.mock import patch from transformers import ( DefaultFlowCallback, IntervalStrategy, PrinterCallback, ProgressCallback, Trainer, TrainerCallback, TrainingArguments, is_torch_available, ) from transformers.testing_utils import require_torch if is_torch_available(): from transformers.trainer import DEFAULT_CALLBACKS from .test_trainer import RegressionDataset, RegressionModelConfig, RegressionPreTrainedModel class __snake_case ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self ): snake_case__ : str = [] def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): self.events.append("""on_init_end""" ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): self.events.append("""on_train_begin""" ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): self.events.append("""on_train_end""" ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): self.events.append("""on_epoch_begin""" ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): self.events.append("""on_epoch_end""" ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): self.events.append("""on_step_begin""" ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): self.events.append("""on_step_end""" ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): self.events.append("""on_evaluate""" ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): self.events.append("""on_predict""" ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): self.events.append("""on_save""" ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): self.events.append("""on_log""" ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): self.events.append("""on_prediction_step""" ) @require_torch class __snake_case ( unittest.TestCase ): '''simple docstring''' def __UpperCamelCase ( self ): snake_case__ : Tuple = tempfile.mkdtemp() def __UpperCamelCase ( self ): shutil.rmtree(self.output_dir ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE=0 , __SCREAMING_SNAKE_CASE=0 , __SCREAMING_SNAKE_CASE=6_4 , __SCREAMING_SNAKE_CASE=6_4 , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=False , **__SCREAMING_SNAKE_CASE ): # disable_tqdm in TrainingArguments has a flaky default since it depends on the level of logging. We make sure # its set to False since the tests later on depend on its value. snake_case__ : List[Any] = RegressionDataset(length=__SCREAMING_SNAKE_CASE ) snake_case__ : List[str] = RegressionDataset(length=__SCREAMING_SNAKE_CASE ) snake_case__ : List[str] = RegressionModelConfig(a=__SCREAMING_SNAKE_CASE , b=__SCREAMING_SNAKE_CASE ) snake_case__ : List[str] = RegressionPreTrainedModel(__SCREAMING_SNAKE_CASE ) snake_case__ : Dict = TrainingArguments(self.output_dir , disable_tqdm=__SCREAMING_SNAKE_CASE , report_to=[] , **__SCREAMING_SNAKE_CASE ) return Trainer( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , train_dataset=__SCREAMING_SNAKE_CASE , eval_dataset=__SCREAMING_SNAKE_CASE , callbacks=__SCREAMING_SNAKE_CASE , ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): self.assertEqual(len(__SCREAMING_SNAKE_CASE ) , len(__SCREAMING_SNAKE_CASE ) ) # Order doesn't matter snake_case__ : Tuple = sorted(__SCREAMING_SNAKE_CASE , key=lambda __SCREAMING_SNAKE_CASE : cb.__name__ if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else cb.__class__.__name__ ) snake_case__ : List[str] = sorted(__SCREAMING_SNAKE_CASE , key=lambda __SCREAMING_SNAKE_CASE : cb.__name__ if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else cb.__class__.__name__ ) for cba, cba in zip(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) and isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) elif isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) and not isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): self.assertEqual(__SCREAMING_SNAKE_CASE , cba.__class__ ) elif not isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) and isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): self.assertEqual(cba.__class__ , __SCREAMING_SNAKE_CASE ) else: self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE ): snake_case__ : Tuple = ["""on_init_end""", """on_train_begin"""] snake_case__ : Union[str, Any] = 0 snake_case__ : Dict = len(trainer.get_eval_dataloader() ) snake_case__ : Any = ["""on_prediction_step"""] * len(trainer.get_eval_dataloader() ) + ["""on_log""", """on_evaluate"""] for _ in range(trainer.state.num_train_epochs ): expected_events.append("""on_epoch_begin""" ) for _ in range(__SCREAMING_SNAKE_CASE ): step += 1 expected_events += ["on_step_begin", "on_step_end"] if step % trainer.args.logging_steps == 0: expected_events.append("""on_log""" ) if trainer.args.evaluation_strategy == IntervalStrategy.STEPS and step % trainer.args.eval_steps == 0: expected_events += evaluation_events.copy() if step % trainer.args.save_steps == 0: expected_events.append("""on_save""" ) expected_events.append("""on_epoch_end""" ) if trainer.args.evaluation_strategy == IntervalStrategy.EPOCH: expected_events += evaluation_events.copy() expected_events += ["on_log", "on_train_end"] return expected_events def __UpperCamelCase ( self ): snake_case__ : Any = self.get_trainer() snake_case__ : str = DEFAULT_CALLBACKS.copy() + [ProgressCallback] self.check_callbacks_equality(trainer.callback_handler.callbacks , __SCREAMING_SNAKE_CASE ) # Callbacks passed at init are added to the default callbacks snake_case__ : List[str] = self.get_trainer(callbacks=[MyTestTrainerCallback] ) expected_callbacks.append(__SCREAMING_SNAKE_CASE ) self.check_callbacks_equality(trainer.callback_handler.callbacks , __SCREAMING_SNAKE_CASE ) # TrainingArguments.disable_tqdm controls if use ProgressCallback or PrinterCallback snake_case__ : Optional[Any] = self.get_trainer(disable_tqdm=__SCREAMING_SNAKE_CASE ) snake_case__ : Tuple = DEFAULT_CALLBACKS.copy() + [PrinterCallback] self.check_callbacks_equality(trainer.callback_handler.callbacks , __SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self ): snake_case__ : str = DEFAULT_CALLBACKS.copy() + [ProgressCallback] snake_case__ : int = self.get_trainer() # We can add, pop, or remove by class name trainer.remove_callback(__SCREAMING_SNAKE_CASE ) expected_callbacks.remove(__SCREAMING_SNAKE_CASE ) self.check_callbacks_equality(trainer.callback_handler.callbacks , __SCREAMING_SNAKE_CASE ) snake_case__ : Union[str, Any] = self.get_trainer() snake_case__ : List[str] = trainer.pop_callback(__SCREAMING_SNAKE_CASE ) self.assertEqual(cb.__class__ , __SCREAMING_SNAKE_CASE ) self.check_callbacks_equality(trainer.callback_handler.callbacks , __SCREAMING_SNAKE_CASE ) trainer.add_callback(__SCREAMING_SNAKE_CASE ) expected_callbacks.insert(0 , __SCREAMING_SNAKE_CASE ) self.check_callbacks_equality(trainer.callback_handler.callbacks , __SCREAMING_SNAKE_CASE ) # We can also add, pop, or remove by instance snake_case__ : List[Any] = self.get_trainer() snake_case__ : List[str] = trainer.callback_handler.callbacks[0] trainer.remove_callback(__SCREAMING_SNAKE_CASE ) expected_callbacks.remove(__SCREAMING_SNAKE_CASE ) self.check_callbacks_equality(trainer.callback_handler.callbacks , __SCREAMING_SNAKE_CASE ) snake_case__ : Optional[int] = self.get_trainer() snake_case__ : Any = trainer.callback_handler.callbacks[0] snake_case__ : Optional[Any] = trainer.pop_callback(__SCREAMING_SNAKE_CASE ) self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) self.check_callbacks_equality(trainer.callback_handler.callbacks , __SCREAMING_SNAKE_CASE ) trainer.add_callback(__SCREAMING_SNAKE_CASE ) expected_callbacks.insert(0 , __SCREAMING_SNAKE_CASE ) self.check_callbacks_equality(trainer.callback_handler.callbacks , __SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self ): import warnings # XXX: for now ignore scatter_gather warnings in this test since it's not relevant to what's being tested warnings.simplefilter(action="""ignore""" , category=__SCREAMING_SNAKE_CASE ) snake_case__ : Any = self.get_trainer(callbacks=[MyTestTrainerCallback] ) trainer.train() snake_case__ : Any = trainer.callback_handler.callbacks[-2].events self.assertEqual(__SCREAMING_SNAKE_CASE , self.get_expected_events(__SCREAMING_SNAKE_CASE ) ) # Independent log/save/eval snake_case__ : Dict = self.get_trainer(callbacks=[MyTestTrainerCallback] , logging_steps=5 ) trainer.train() snake_case__ : int = trainer.callback_handler.callbacks[-2].events self.assertEqual(__SCREAMING_SNAKE_CASE , self.get_expected_events(__SCREAMING_SNAKE_CASE ) ) snake_case__ : Any = self.get_trainer(callbacks=[MyTestTrainerCallback] , save_steps=5 ) trainer.train() snake_case__ : Any = trainer.callback_handler.callbacks[-2].events self.assertEqual(__SCREAMING_SNAKE_CASE , self.get_expected_events(__SCREAMING_SNAKE_CASE ) ) snake_case__ : Tuple = self.get_trainer(callbacks=[MyTestTrainerCallback] , eval_steps=5 , evaluation_strategy="""steps""" ) trainer.train() snake_case__ : str = trainer.callback_handler.callbacks[-2].events self.assertEqual(__SCREAMING_SNAKE_CASE , self.get_expected_events(__SCREAMING_SNAKE_CASE ) ) snake_case__ : Tuple = self.get_trainer(callbacks=[MyTestTrainerCallback] , evaluation_strategy="""epoch""" ) trainer.train() snake_case__ : Any = trainer.callback_handler.callbacks[-2].events self.assertEqual(__SCREAMING_SNAKE_CASE , self.get_expected_events(__SCREAMING_SNAKE_CASE ) ) # A bit of everything snake_case__ : Dict = self.get_trainer( callbacks=[MyTestTrainerCallback] , logging_steps=3 , save_steps=1_0 , eval_steps=5 , evaluation_strategy="""steps""" , ) trainer.train() snake_case__ : Tuple = trainer.callback_handler.callbacks[-2].events self.assertEqual(__SCREAMING_SNAKE_CASE , self.get_expected_events(__SCREAMING_SNAKE_CASE ) ) # warning should be emitted for duplicated callbacks with patch("""transformers.trainer_callback.logger.warning""" ) as warn_mock: snake_case__ : List[str] = self.get_trainer( callbacks=[MyTestTrainerCallback, MyTestTrainerCallback] , ) assert str(__SCREAMING_SNAKE_CASE ) in warn_mock.call_args[0][0]
38
0
import os from shutil import copyfile from typing import List, Optional, Tuple from tokenizers import processors from ...tokenization_utils import AddedToken, BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_mbart import MBartTokenizer else: SCREAMING_SNAKE_CASE__ = None SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) SCREAMING_SNAKE_CASE__ = {"vocab_file": "sentencepiece.bpe.model", "tokenizer_file": "tokenizer.json"} SCREAMING_SNAKE_CASE__ = { "vocab_file": { "facebook/mbart-large-en-ro": ( "https://huggingface.co/facebook/mbart-large-en-ro/resolve/main/sentencepiece.bpe.model" ), "facebook/mbart-large-cc25": ( "https://huggingface.co/facebook/mbart-large-cc25/resolve/main/sentencepiece.bpe.model" ), }, "tokenizer_file": { "facebook/mbart-large-en-ro": "https://huggingface.co/facebook/mbart-large-en-ro/resolve/main/tokenizer.json", "facebook/mbart-large-cc25": "https://huggingface.co/facebook/mbart-large-cc25/resolve/main/tokenizer.json", }, } SCREAMING_SNAKE_CASE__ = { "facebook/mbart-large-en-ro": 1024, "facebook/mbart-large-cc25": 1024, } # fmt: off SCREAMING_SNAKE_CASE__ = ["ar_AR", "cs_CZ", "de_DE", "en_XX", "es_XX", "et_EE", "fi_FI", "fr_XX", "gu_IN", "hi_IN", "it_IT", "ja_XX", "kk_KZ", "ko_KR", "lt_LT", "lv_LV", "my_MM", "ne_NP", "nl_XX", "ro_RO", "ru_RU", "si_LK", "tr_TR", "vi_VN", "zh_CN"] class _UpperCamelCase( __SCREAMING_SNAKE_CASE ): __SCREAMING_SNAKE_CASE : int = VOCAB_FILES_NAMES __SCREAMING_SNAKE_CASE : Union[str, Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __SCREAMING_SNAKE_CASE : Union[str, Any] = PRETRAINED_VOCAB_FILES_MAP __SCREAMING_SNAKE_CASE : Any = ['''input_ids''', '''attention_mask'''] __SCREAMING_SNAKE_CASE : Tuple = MBartTokenizer __SCREAMING_SNAKE_CASE : Any = [] __SCREAMING_SNAKE_CASE : List[Any] = [] def __init__( self : Optional[int] , SCREAMING_SNAKE_CASE__ : Any=None , SCREAMING_SNAKE_CASE__ : Dict=None , SCREAMING_SNAKE_CASE__ : Any="<s>" , SCREAMING_SNAKE_CASE__ : List[Any]="</s>" , SCREAMING_SNAKE_CASE__ : str="</s>" , SCREAMING_SNAKE_CASE__ : Union[str, Any]="<s>" , SCREAMING_SNAKE_CASE__ : List[Any]="<unk>" , SCREAMING_SNAKE_CASE__ : List[str]="<pad>" , SCREAMING_SNAKE_CASE__ : Union[str, Any]="<mask>" , SCREAMING_SNAKE_CASE__ : List[Any]=None , SCREAMING_SNAKE_CASE__ : Tuple=None , SCREAMING_SNAKE_CASE__ : Union[str, Any]=None , **SCREAMING_SNAKE_CASE__ : Optional[Any] , ): '''simple docstring''' __a : List[Any] = AddedToken(__SCREAMING_SNAKE_CASE , lstrip=__SCREAMING_SNAKE_CASE , rstrip=__SCREAMING_SNAKE_CASE ) if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else mask_token super().__init__( vocab_file=__SCREAMING_SNAKE_CASE , tokenizer_file=__SCREAMING_SNAKE_CASE , bos_token=__SCREAMING_SNAKE_CASE , eos_token=__SCREAMING_SNAKE_CASE , sep_token=__SCREAMING_SNAKE_CASE , cls_token=__SCREAMING_SNAKE_CASE , unk_token=__SCREAMING_SNAKE_CASE , pad_token=__SCREAMING_SNAKE_CASE , mask_token=__SCREAMING_SNAKE_CASE , src_lang=__SCREAMING_SNAKE_CASE , tgt_lang=__SCREAMING_SNAKE_CASE , additional_special_tokens=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) __a : Tuple = vocab_file __a : List[str] = False if not self.vocab_file else True __a : Union[str, Any] = FAIRSEQ_LANGUAGE_CODES.copy() if additional_special_tokens is not None: # Only add those special tokens if they are not already there. _additional_special_tokens.extend( [t for t in additional_special_tokens if t not in _additional_special_tokens] ) self.add_special_tokens({'additional_special_tokens': _additional_special_tokens} ) __a : List[Any] = { lang_code: self.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) for lang_code in FAIRSEQ_LANGUAGE_CODES } __a : Any = src_lang if src_lang is not None else """en_XX""" __a : Optional[Any] = self.convert_tokens_to_ids(self._src_lang ) __a : Dict = tgt_lang self.set_src_lang_special_tokens(self._src_lang ) @property def __lowerCAmelCase ( self : List[Any] ): '''simple docstring''' return self._src_lang @src_lang.setter def __lowerCAmelCase ( self : str , SCREAMING_SNAKE_CASE__ : Tuple ): '''simple docstring''' __a : Dict = new_src_lang self.set_src_lang_special_tokens(self._src_lang ) def __lowerCAmelCase ( self : Optional[int] , SCREAMING_SNAKE_CASE__ : Union[str, Any] , SCREAMING_SNAKE_CASE__ : List[Any] = None ): '''simple docstring''' if token_ids_a is None: return self.prefix_tokens + token_ids_a + self.suffix_tokens # We don't expect to process pairs, but leave the pair logic for API consistency return self.prefix_tokens + token_ids_a + token_ids_a + self.suffix_tokens def __lowerCAmelCase ( self : Optional[int] , SCREAMING_SNAKE_CASE__ : Optional[Any] , SCREAMING_SNAKE_CASE__ : Optional[int] = None ): '''simple docstring''' __a : Tuple = [self.sep_token_id] __a : List[Any] = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def __lowerCAmelCase ( self : Optional[Any] , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : Optional[Any] , SCREAMING_SNAKE_CASE__ : Dict , **SCREAMING_SNAKE_CASE__ : Union[str, Any] ): '''simple docstring''' if src_lang is None or tgt_lang is None: raise ValueError('Translation requires a `src_lang` and a `tgt_lang` for this model' ) __a : List[Any] = src_lang __a : str = self(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , return_tensors=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) __a : Dict = self.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) __a : Optional[Any] = tgt_lang_id return inputs def __lowerCAmelCase ( self : Optional[int] , SCREAMING_SNAKE_CASE__ : Optional[int] , SCREAMING_SNAKE_CASE__ : Tuple = "en_XX" , SCREAMING_SNAKE_CASE__ : Optional[int] = None , SCREAMING_SNAKE_CASE__ : List[Any] = "ro_RO" , **SCREAMING_SNAKE_CASE__ : Optional[Any] , ): '''simple docstring''' __a : Union[str, Any] = src_lang __a : int = tgt_lang return super().prepare_seqaseq_batch(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def __lowerCAmelCase ( self : List[str] ): '''simple docstring''' return self.set_src_lang_special_tokens(self.src_lang ) def __lowerCAmelCase ( self : int ): '''simple docstring''' return self.set_tgt_lang_special_tokens(self.tgt_lang ) def __lowerCAmelCase ( self : List[str] , SCREAMING_SNAKE_CASE__ : int ): '''simple docstring''' __a : Union[str, Any] = self.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) __a : int = [] __a : Dict = [self.eos_token_id, self.cur_lang_code] __a : int = self.convert_ids_to_tokens(self.prefix_tokens ) __a : List[str] = self.convert_ids_to_tokens(self.suffix_tokens ) __a : Optional[Any] = processors.TemplateProcessing( single=prefix_tokens_str + ['$A'] + suffix_tokens_str , pair=prefix_tokens_str + ['$A', '$B'] + suffix_tokens_str , special_tokens=list(zip(prefix_tokens_str + suffix_tokens_str , self.prefix_tokens + self.suffix_tokens ) ) , ) def __lowerCAmelCase ( self : Optional[int] , SCREAMING_SNAKE_CASE__ : Union[str, Any] ): '''simple docstring''' __a : List[Any] = self.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) __a : List[Any] = [] __a : List[Any] = [self.eos_token_id, self.cur_lang_code] __a : Any = self.convert_ids_to_tokens(self.prefix_tokens ) __a : Dict = self.convert_ids_to_tokens(self.suffix_tokens ) __a : Optional[int] = processors.TemplateProcessing( single=prefix_tokens_str + ['$A'] + suffix_tokens_str , pair=prefix_tokens_str + ['$A', '$B'] + suffix_tokens_str , special_tokens=list(zip(prefix_tokens_str + suffix_tokens_str , self.prefix_tokens + self.suffix_tokens ) ) , ) def __lowerCAmelCase ( self : Optional[int] , SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : List[Any] = None ): '''simple docstring''' if not self.can_save_slow_tokenizer: raise ValueError( 'Your fast tokenizer does not have the necessary information to save the vocabulary for a slow ' 'tokenizer.' ) if not os.path.isdir(__SCREAMING_SNAKE_CASE ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory.''' ) return __a : List[str] = os.path.join( __SCREAMING_SNAKE_CASE , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(__SCREAMING_SNAKE_CASE ): copyfile(self.vocab_file , __SCREAMING_SNAKE_CASE ) return (out_vocab_file,)
47
'''simple docstring''' 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 __snake_case ( unittest.TestCase ): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=7 , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=1_8 , __SCREAMING_SNAKE_CASE=3_0 , __SCREAMING_SNAKE_CASE=4_0_0 , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=[0.5, 0.5, 0.5] , __SCREAMING_SNAKE_CASE=[0.5, 0.5, 0.5] , ): snake_case__ : Any = size if size is not None else {"""height""": 1_8, """width""": 1_8} snake_case__ : List[Any] = parent snake_case__ : int = batch_size snake_case__ : List[Any] = num_channels snake_case__ : str = image_size snake_case__ : Union[str, Any] = min_resolution snake_case__ : List[Any] = max_resolution snake_case__ : Tuple = do_resize snake_case__ : int = size snake_case__ : Tuple = do_normalize snake_case__ : Dict = image_mean snake_case__ : Union[str, Any] = image_std def __UpperCamelCase ( self ): 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 __snake_case ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowerCamelCase__ = DPTImageProcessor if is_vision_available() else None def __UpperCamelCase ( self ): snake_case__ : str = DPTImageProcessingTester(self ) @property def __UpperCamelCase ( self ): return self.image_processor_tester.prepare_image_processor_dict() def __UpperCamelCase ( self ): snake_case__ : Optional[int] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , """image_mean""" ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , """image_std""" ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , """do_normalize""" ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , """do_resize""" ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , """size""" ) ) def __UpperCamelCase ( self ): snake_case__ : Any = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"""height""": 1_8, """width""": 1_8} ) snake_case__ : List[Any] = self.image_processing_class.from_dict(self.image_processor_dict , size=4_2 ) self.assertEqual(image_processor.size , {"""height""": 4_2, """width""": 4_2} ) def __UpperCamelCase ( self ): # Initialize image_processing snake_case__ : Optional[int] = self.image_processing_class(**self.image_processor_dict ) # create random PIL images snake_case__ : List[Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=__SCREAMING_SNAKE_CASE ) for image in image_inputs: self.assertIsInstance(__SCREAMING_SNAKE_CASE , Image.Image ) # Test not batched input snake_case__ : Optional[int] = 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 snake_case__ : List[str] = image_processing(__SCREAMING_SNAKE_CASE , 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 __UpperCamelCase ( self ): # Initialize image_processing snake_case__ : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors snake_case__ : Optional[int] = prepare_image_inputs(self.image_processor_tester , equal_resolution=__SCREAMING_SNAKE_CASE , numpify=__SCREAMING_SNAKE_CASE ) for image in image_inputs: self.assertIsInstance(__SCREAMING_SNAKE_CASE , np.ndarray ) # Test not batched input snake_case__ : List[str] = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) , ) # Test batched snake_case__ : Any = image_processing(__SCREAMING_SNAKE_CASE , 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 __UpperCamelCase ( self ): # Initialize image_processing snake_case__ : List[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors snake_case__ : List[str] = prepare_image_inputs(self.image_processor_tester , equal_resolution=__SCREAMING_SNAKE_CASE , torchify=__SCREAMING_SNAKE_CASE ) for image in image_inputs: self.assertIsInstance(__SCREAMING_SNAKE_CASE , torch.Tensor ) # Test not batched input snake_case__ : List[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 snake_case__ : List[str] = image_processing(__SCREAMING_SNAKE_CASE , 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"""], ) , )
38
0
'''simple docstring''' import random from .binary_exp_mod import bin_exp_mod def _a (lowercase__ : List[Any] , lowercase__ : Optional[Any]=1_0_0_0 ) -> List[str]: """simple docstring""" if n < 2: return False if n % 2 == 0: return n == 2 # this means n is odd __snake_case = n - 1 __snake_case = 0 while d % 2 == 0: d /= 2 exp += 1 # n - 1=d*(2**exp) __snake_case = 0 while count < prec: __snake_case = random.randint(2 , n - 1 ) __snake_case = bin_exp_mod(lowercase__ , lowercase__ , lowercase__ ) if b != 1: __snake_case = True for _ in range(lowercase__ ): if b == n - 1: __snake_case = False break __snake_case = b * b b %= n if flag: return False count += 1 return True if __name__ == "__main__": _a : Optional[Any] = abs(int(input("Enter bound : ").strip())) print("Here's the list of primes:") print(", ".join(str(i) for i in range(n + 1) if is_prime_big(i)))
56
'''simple docstring''' from __future__ import annotations import inspect import unittest from math import floor import numpy as np from transformers import CvtConfig from transformers.testing_utils import require_tf, require_vision, slow from transformers.utils import cached_property, is_tf_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFCvtForImageClassification, TFCvtModel from transformers.models.cvt.modeling_tf_cvt import TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class __snake_case ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' def __UpperCamelCase ( self ): snake_case__ : Dict = self.config_class(**self.inputs_dict ) self.parent.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , """embed_dim""" ) ) self.parent.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , """num_heads""" ) ) class __snake_case : '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=1_3 , __SCREAMING_SNAKE_CASE=6_4 , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=[1_6, 4_8, 9_6] , __SCREAMING_SNAKE_CASE=[1, 3, 6] , __SCREAMING_SNAKE_CASE=[1, 2, 1_0] , __SCREAMING_SNAKE_CASE=[7, 3, 3] , __SCREAMING_SNAKE_CASE=[4, 2, 2] , __SCREAMING_SNAKE_CASE=[2, 1, 1] , __SCREAMING_SNAKE_CASE=[2, 2, 2] , __SCREAMING_SNAKE_CASE=[False, False, True] , __SCREAMING_SNAKE_CASE=[0.0, 0.0, 0.0] , __SCREAMING_SNAKE_CASE=0.02 , __SCREAMING_SNAKE_CASE=1e-1_2 , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=2 , ): snake_case__ : List[str] = parent snake_case__ : Tuple = batch_size snake_case__ : Union[str, Any] = image_size snake_case__ : List[Any] = patch_sizes snake_case__ : Optional[int] = patch_stride snake_case__ : Optional[Any] = patch_padding snake_case__ : Any = is_training snake_case__ : int = use_labels snake_case__ : Dict = num_labels snake_case__ : Optional[Any] = num_channels snake_case__ : Optional[Any] = embed_dim snake_case__ : Optional[int] = num_heads snake_case__ : Optional[int] = stride_kv snake_case__ : int = depth snake_case__ : Optional[Any] = cls_token snake_case__ : List[Any] = attention_drop_rate snake_case__ : Union[str, Any] = initializer_range snake_case__ : List[Any] = layer_norm_eps def __UpperCamelCase ( self ): snake_case__ : Optional[int] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) snake_case__ : List[Any] = None if self.use_labels: # create a random int32 tensor of given shape snake_case__ : List[str] = ids_tensor([self.batch_size] , self.num_labels ) snake_case__ : List[str] = self.get_config() return config, pixel_values, labels def __UpperCamelCase ( self ): return CvtConfig( image_size=self.image_size , num_labels=self.num_labels , num_channels=self.num_channels , embed_dim=self.embed_dim , num_heads=self.num_heads , patch_sizes=self.patch_sizes , patch_padding=self.patch_padding , patch_stride=self.patch_stride , stride_kv=self.stride_kv , depth=self.depth , cls_token=self.cls_token , attention_drop_rate=self.attention_drop_rate , initializer_range=self.initializer_range , ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): snake_case__ : int = TFCvtModel(config=__SCREAMING_SNAKE_CASE ) snake_case__ : List[Any] = model(__SCREAMING_SNAKE_CASE , training=__SCREAMING_SNAKE_CASE ) snake_case__ : Tuple = (self.image_size, self.image_size) snake_case__ , snake_case__ : str = image_size[0], image_size[1] for i in range(len(self.depth ) ): snake_case__ : Any = floor(((height + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1 ) snake_case__ : Optional[int] = floor(((width + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1 ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.embed_dim[-1], height, width) ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): snake_case__ : Any = self.num_labels snake_case__ : str = TFCvtForImageClassification(__SCREAMING_SNAKE_CASE ) snake_case__ : List[str] = model(__SCREAMING_SNAKE_CASE , labels=__SCREAMING_SNAKE_CASE , training=__SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __UpperCamelCase ( self ): snake_case__ : List[Any] = self.prepare_config_and_inputs() snake_case__ , snake_case__ , snake_case__ : Any = config_and_inputs snake_case__ : Union[str, Any] = {"""pixel_values""": pixel_values} return config, inputs_dict @require_tf class __snake_case ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowerCamelCase__ = (TFCvtModel, TFCvtForImageClassification) if is_tf_available() else () lowerCamelCase__ = ( {'''feature-extraction''': TFCvtModel, '''image-classification''': TFCvtForImageClassification} if is_tf_available() else {} ) lowerCamelCase__ = False lowerCamelCase__ = False lowerCamelCase__ = False lowerCamelCase__ = False lowerCamelCase__ = False def __UpperCamelCase ( self ): snake_case__ : Optional[Any] = TFCvtModelTester(self ) snake_case__ : Any = TFCvtConfigTester(self , config_class=__SCREAMING_SNAKE_CASE , has_text_modality=__SCREAMING_SNAKE_CASE , hidden_size=3_7 ) def __UpperCamelCase ( self ): self.config_tester.create_and_test_config_common_properties() self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() @unittest.skip(reason="""Cvt does not output attentions""" ) def __UpperCamelCase ( self ): pass @unittest.skip(reason="""Cvt does not use inputs_embeds""" ) def __UpperCamelCase ( self ): pass @unittest.skip(reason="""Cvt does not support input and output embeddings""" ) def __UpperCamelCase ( self ): pass @unittest.skipIf( not is_tf_available() or len(tf.config.list_physical_devices("""GPU""" ) ) == 0 , reason="""TF does not support backprop for grouped convolutions on CPU.""" , ) def __UpperCamelCase ( self ): super().test_dataset_conversion() @unittest.skipIf( not is_tf_available() or len(tf.config.list_physical_devices("""GPU""" ) ) == 0 , reason="""TF does not support backprop for grouped convolutions on CPU.""" , ) @slow def __UpperCamelCase ( self ): super().test_keras_fit() @unittest.skip(reason="""Get `Failed to determine best cudnn convolution algo.` error after using TF 2.12+cuda 11.8""" ) def __UpperCamelCase ( self ): snake_case__ : List[str] = tf.keras.mixed_precision.Policy("""mixed_float16""" ) tf.keras.mixed_precision.set_global_policy(__SCREAMING_SNAKE_CASE ) super().test_keras_fit() tf.keras.mixed_precision.set_global_policy("""float32""" ) def __UpperCamelCase ( self ): snake_case__ , snake_case__ : List[str] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: snake_case__ : Any = model_class(__SCREAMING_SNAKE_CASE ) snake_case__ : str = inspect.signature(model.call ) # signature.parameters is an OrderedDict => so arg_names order is deterministic snake_case__ : Optional[Any] = [*signature.parameters.keys()] snake_case__ : Tuple = ["""pixel_values"""] self.assertListEqual(arg_names[:1] , __SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self ): def check_hidden_states_output(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): snake_case__ : str = model_class(__SCREAMING_SNAKE_CASE ) snake_case__ : List[Any] = model(**self._prepare_for_class(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ) snake_case__ : Optional[int] = outputs.hidden_states snake_case__ : Tuple = len(self.model_tester.depth ) self.assertEqual(len(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) # verify the first hidden states (first block) self.assertListEqual( list(hidden_states[0].shape[-3:] ) , [ self.model_tester.embed_dim[0], self.model_tester.image_size // 4, self.model_tester.image_size // 4, ] , ) snake_case__ , snake_case__ : str = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: snake_case__ : List[Any] = True check_hidden_states_output(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] snake_case__ : List[str] = True check_hidden_states_output(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self ): snake_case__ : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self ): snake_case__ : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*__SCREAMING_SNAKE_CASE ) @slow def __UpperCamelCase ( self ): for model_name in TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: snake_case__ : str = TFCvtModel.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsNotNone(__SCREAMING_SNAKE_CASE ) def UpperCamelCase__ ( ) -> str: '''simple docstring''' snake_case__ : Union[str, Any] = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) return image @require_tf @require_vision class __snake_case ( unittest.TestCase ): '''simple docstring''' @cached_property def __UpperCamelCase ( self ): return AutoImageProcessor.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) @slow def __UpperCamelCase ( self ): snake_case__ : Optional[Any] = TFCvtForImageClassification.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) snake_case__ : Union[str, Any] = self.default_image_processor snake_case__ : int = prepare_img() snake_case__ : Dict = image_processor(images=__SCREAMING_SNAKE_CASE , return_tensors="""tf""" ) # forward pass snake_case__ : Optional[int] = model(**__SCREAMING_SNAKE_CASE ) # verify the logits snake_case__ : str = tf.TensorShape((1, 1_0_0_0) ) self.assertEqual(outputs.logits.shape , __SCREAMING_SNAKE_CASE ) snake_case__ : int = tf.constant([0.9285, 0.9015, -0.3150] ) self.assertTrue(np.allclose(outputs.logits[0, :3].numpy() , __SCREAMING_SNAKE_CASE , atol=1e-4 ) )
38
0
from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices snake_case__ : Dict = logging.get_logger(__name__) snake_case__ : Optional[Any] = { "facebook/convnextv2-tiny-1k-224": "https://huggingface.co/facebook/convnextv2-tiny-1k-224/resolve/main/config.json", } class _a ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): """simple docstring""" A_ = """convnextv2""" def __init__( self , _UpperCAmelCase=3 , _UpperCAmelCase=4 , _UpperCAmelCase=4 , _UpperCAmelCase=None , _UpperCAmelCase=None , _UpperCAmelCase="gelu" , _UpperCAmelCase=0.0_2 , _UpperCAmelCase=1e-12 , _UpperCAmelCase=0.0 , _UpperCAmelCase=224 , _UpperCAmelCase=None , _UpperCAmelCase=None , **_UpperCAmelCase , ) -> Any: super().__init__(**__SCREAMING_SNAKE_CASE ) UpperCamelCase_ = num_channels UpperCamelCase_ = patch_size UpperCamelCase_ = num_stages UpperCamelCase_ = [96, 192, 384, 768] if hidden_sizes is None else hidden_sizes UpperCamelCase_ = [3, 3, 9, 3] if depths is None else depths UpperCamelCase_ = hidden_act UpperCamelCase_ = initializer_range UpperCamelCase_ = layer_norm_eps UpperCamelCase_ = drop_path_rate UpperCamelCase_ = image_size UpperCamelCase_ = ["""stem"""] + [f"""stage{idx}""" for idx in range(1 , len(self.depths ) + 1 )] UpperCamelCase_ = get_aligned_output_features_output_indices( out_features=__SCREAMING_SNAKE_CASE , out_indices=__SCREAMING_SNAKE_CASE , stage_names=self.stage_names )
23
'''simple docstring''' import unittest from transformers import is_torch_available from transformers.testing_utils import require_torch if is_torch_available(): import torch from transformers.generation import DisjunctiveConstraint @require_torch class __snake_case ( unittest.TestCase ): '''simple docstring''' def __UpperCamelCase ( self ): # For consistency across different places the DisjunctiveConstraint is called, # dc.token_ids is a list of integers. It is also initialized only by integers. snake_case__ : int = [[1, 2, 4], [1, 2, 3, 4]] snake_case__ : Any = DisjunctiveConstraint(__SCREAMING_SNAKE_CASE ) self.assertTrue(isinstance(dc.token_ids , __SCREAMING_SNAKE_CASE ) ) with self.assertRaises(__SCREAMING_SNAKE_CASE ): DisjunctiveConstraint(torch.LongTensor([[1, 2, 4], [1, 2, 3]] ) ) with self.assertRaises(__SCREAMING_SNAKE_CASE ): DisjunctiveConstraint([torch.LongTensor([1, 2, 4] ), torch.LongTensor([1, 2, 3, 4, 5] )] ) def __UpperCamelCase ( self ): # We can't have constraints that are complete subsets of another. This leads to a preverse # interpretation of "constraint fulfillment": does generating [1,2,3] fulfill the constraint? # It would mean that it generated [1,2] which fulfills it, but it's in the middle of potentially # fulfilling [1,2,3,4]. If we believe that [1,2,3] does fulfill the constraint, then the algorithm # will necessarily never reach [1,2,3,4], giving users a false sense of control (better to just not allow it). snake_case__ : Union[str, Any] = [[1, 2], [1, 2, 3, 4]] with self.assertRaises(__SCREAMING_SNAKE_CASE ): DisjunctiveConstraint(__SCREAMING_SNAKE_CASE ) # fails here def __UpperCamelCase ( self ): snake_case__ : List[str] = [[1, 2, 3], [1, 2, 4]] snake_case__ : Optional[int] = DisjunctiveConstraint(__SCREAMING_SNAKE_CASE ) snake_case__ , snake_case__ , snake_case__ : Any = dc.update(1 ) snake_case__ : Any = stepped is True and completed is False and reset is False self.assertTrue(__SCREAMING_SNAKE_CASE ) self.assertTrue(not dc.completed ) self.assertTrue(dc.current_seq == [1] ) snake_case__ , snake_case__ , snake_case__ : Tuple = dc.update(2 ) snake_case__ : Tuple = stepped is True and completed is False and reset is False self.assertTrue(__SCREAMING_SNAKE_CASE ) self.assertTrue(not dc.completed ) self.assertTrue(dc.current_seq == [1, 2] ) snake_case__ , snake_case__ , snake_case__ : Union[str, Any] = dc.update(3 ) snake_case__ : List[str] = stepped is True and completed is True and reset is False self.assertTrue(__SCREAMING_SNAKE_CASE ) self.assertTrue(dc.completed ) # Completed! self.assertTrue(dc.current_seq == [1, 2, 3] ) def __UpperCamelCase ( self ): snake_case__ : Optional[Any] = [[1, 2, 3], [1, 2, 4, 5], [1, 2, 5]] snake_case__ : int = DisjunctiveConstraint(__SCREAMING_SNAKE_CASE ) snake_case__ , snake_case__ , snake_case__ : str = dc.update(1 ) self.assertTrue(not dc.completed ) self.assertTrue(dc.current_seq == [1] ) snake_case__ , snake_case__ , snake_case__ : str = dc.update(2 ) self.assertTrue(not dc.completed ) self.assertTrue(dc.current_seq == [1, 2] ) snake_case__ , snake_case__ , snake_case__ : List[Any] = dc.update(4 ) self.assertTrue(not dc.completed ) self.assertTrue(dc.current_seq == [1, 2, 4] ) snake_case__ , snake_case__ , snake_case__ : Union[str, Any] = dc.update(5 ) self.assertTrue(dc.completed ) # Completed! self.assertTrue(dc.current_seq == [1, 2, 4, 5] ) dc.reset() snake_case__ , snake_case__ , snake_case__ : List[Any] = dc.update(1 ) self.assertTrue(not dc.completed ) self.assertTrue(dc.remaining() == 3 ) self.assertTrue(dc.current_seq == [1] ) snake_case__ , snake_case__ , snake_case__ : List[Any] = dc.update(2 ) self.assertTrue(not dc.completed ) self.assertTrue(dc.remaining() == 2 ) self.assertTrue(dc.current_seq == [1, 2] ) snake_case__ , snake_case__ , snake_case__ : Dict = dc.update(5 ) self.assertTrue(dc.completed ) # Completed! self.assertTrue(dc.remaining() == 0 ) self.assertTrue(dc.current_seq == [1, 2, 5] )
38
0
def _lowercase ( __SCREAMING_SNAKE_CASE = "The quick brown fox jumps over the lazy dog" , ) -> bool: UpperCamelCase__ : Tuple = set() # Replace all the whitespace in our sentence UpperCamelCase__ : List[Any] = input_str.replace(' ' , '' ) for alpha in input_str: if "a" <= alpha.lower() <= "z": frequency.add(alpha.lower() ) return len(__SCREAMING_SNAKE_CASE ) == 26 def _lowercase ( __SCREAMING_SNAKE_CASE = "The quick brown fox jumps over the lazy dog" , ) -> bool: UpperCamelCase__ : Optional[Any] = [False] * 26 for char in input_str: if char.islower(): UpperCamelCase__ : int = True elif char.isupper(): UpperCamelCase__ : Optional[Any] = True return all(__SCREAMING_SNAKE_CASE ) def _lowercase ( __SCREAMING_SNAKE_CASE = "The quick brown fox jumps over the lazy dog" , ) -> bool: return len({char for char in input_str.lower() if char.isalpha()} ) == 26 def _lowercase ( ) -> None: from timeit import timeit UpperCamelCase__ : Optional[Any] = """from __main__ import is_pangram, is_pangram_faster, is_pangram_fastest""" print(timeit('is_pangram()' , setup=__SCREAMING_SNAKE_CASE ) ) print(timeit('is_pangram_faster()' , setup=__SCREAMING_SNAKE_CASE ) ) print(timeit('is_pangram_fastest()' , setup=__SCREAMING_SNAKE_CASE ) ) # 5.348480500048026, 2.6477354579837993, 1.8470395830227062 # 5.036091582966037, 2.644472333951853, 1.8869528750656173 if __name__ == "__main__": import doctest doctest.testmod() benchmark()
410
'''simple docstring''' import warnings from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging A_ : Optional[int] = logging.get_logger(__name__) A_ : Tuple = { "nvidia/segformer-b0-finetuned-ade-512-512": ( "https://huggingface.co/nvidia/segformer-b0-finetuned-ade-512-512/resolve/main/config.json" ), # See all SegFormer models at https://huggingface.co/models?filter=segformer } class __snake_case ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' lowerCamelCase__ = '''segformer''' def __init__( self , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=4 , __SCREAMING_SNAKE_CASE=[2, 2, 2, 2] , __SCREAMING_SNAKE_CASE=[8, 4, 2, 1] , __SCREAMING_SNAKE_CASE=[3_2, 6_4, 1_6_0, 2_5_6] , __SCREAMING_SNAKE_CASE=[7, 3, 3, 3] , __SCREAMING_SNAKE_CASE=[4, 2, 2, 2] , __SCREAMING_SNAKE_CASE=[1, 2, 5, 8] , __SCREAMING_SNAKE_CASE=[4, 4, 4, 4] , __SCREAMING_SNAKE_CASE="gelu" , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=0.02 , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=1e-6 , __SCREAMING_SNAKE_CASE=2_5_6 , __SCREAMING_SNAKE_CASE=2_5_5 , **__SCREAMING_SNAKE_CASE , ): super().__init__(**__SCREAMING_SNAKE_CASE ) if "reshape_last_stage" in kwargs and kwargs["reshape_last_stage"] is False: warnings.warn( """Reshape_last_stage is set to False in this config. This argument is deprecated and will soon be""" """ removed, as the behaviour will default to that of reshape_last_stage = True.""" , __SCREAMING_SNAKE_CASE , ) snake_case__ : Dict = num_channels snake_case__ : Optional[Any] = num_encoder_blocks snake_case__ : Any = depths snake_case__ : Optional[int] = sr_ratios snake_case__ : Tuple = hidden_sizes snake_case__ : List[str] = patch_sizes snake_case__ : str = strides snake_case__ : Optional[int] = mlp_ratios snake_case__ : Optional[Any] = num_attention_heads snake_case__ : Dict = hidden_act snake_case__ : Optional[int] = hidden_dropout_prob snake_case__ : List[str] = attention_probs_dropout_prob snake_case__ : List[Any] = classifier_dropout_prob snake_case__ : int = initializer_range snake_case__ : List[str] = drop_path_rate snake_case__ : int = layer_norm_eps snake_case__ : List[Any] = decoder_hidden_size snake_case__ : List[Any] = kwargs.get("""reshape_last_stage""" , __SCREAMING_SNAKE_CASE ) snake_case__ : Dict = semantic_loss_ignore_index class __snake_case ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' lowerCamelCase__ = version.parse('''1.11''' ) @property def __UpperCamelCase ( self ): return OrderedDict( [ ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}), ] ) @property def __UpperCamelCase ( self ): return 1e-4 @property def __UpperCamelCase ( self ): return 1_2
38
0
import gc import unittest import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTextModelWithProjection, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, DDPMScheduler, PriorTransformer, StableUnCLIPPipeline, UNetaDConditionModel, ) from diffusers.pipelines.stable_diffusion.stable_unclip_image_normalizer import StableUnCLIPImageNormalizer from diffusers.utils.testing_utils import enable_full_determinism, load_numpy, require_torch_gpu, slow, torch_device 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, assert_mean_pixel_difference, ) enable_full_determinism() class A_ ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): _SCREAMING_SNAKE_CASE = StableUnCLIPPipeline _SCREAMING_SNAKE_CASE = TEXT_TO_IMAGE_PARAMS _SCREAMING_SNAKE_CASE = TEXT_TO_IMAGE_BATCH_PARAMS _SCREAMING_SNAKE_CASE = TEXT_TO_IMAGE_IMAGE_PARAMS _SCREAMING_SNAKE_CASE = TEXT_TO_IMAGE_IMAGE_PARAMS # TODO(will) Expected attn_bias.stride(1) == 0 to be true, but got false _SCREAMING_SNAKE_CASE = False def _UpperCAmelCase ( self : List[Any] ): __a = 32 __a = embedder_hidden_size # prior components torch.manual_seed(0 ) __a = CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip" ) torch.manual_seed(0 ) __a = CLIPTextModelWithProjection( CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=__SCREAMING_SNAKE_CASE , projection_dim=__SCREAMING_SNAKE_CASE , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=10_00 , ) ) torch.manual_seed(0 ) __a = PriorTransformer( num_attention_heads=2 , attention_head_dim=12 , embedding_dim=__SCREAMING_SNAKE_CASE , num_layers=1 , ) torch.manual_seed(0 ) __a = DDPMScheduler( variance_type="fixed_small_log" , prediction_type="sample" , num_train_timesteps=10_00 , clip_sample=__SCREAMING_SNAKE_CASE , clip_sample_range=5.0 , beta_schedule="squaredcos_cap_v2" , ) # regular denoising components torch.manual_seed(0 ) __a = StableUnCLIPImageNormalizer(embedding_dim=__SCREAMING_SNAKE_CASE ) __a = DDPMScheduler(beta_schedule="squaredcos_cap_v2" ) torch.manual_seed(0 ) __a = CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip" ) torch.manual_seed(0 ) __a = CLIPTextModel( CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=__SCREAMING_SNAKE_CASE , 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=10_00 , ) ) torch.manual_seed(0 ) __a = UNetaDConditionModel( sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=("CrossAttnDownBlock2D", "DownBlock2D") , up_block_types=("UpBlock2D", "CrossAttnUpBlock2D") , block_out_channels=(32, 64) , attention_head_dim=(2, 4) , class_embed_type="projection" , projection_class_embeddings_input_dim=embedder_projection_dim * 2 , cross_attention_dim=__SCREAMING_SNAKE_CASE , layers_per_block=1 , upcast_attention=__SCREAMING_SNAKE_CASE , use_linear_projection=__SCREAMING_SNAKE_CASE , ) torch.manual_seed(0 ) __a = DDIMScheduler( beta_schedule="scaled_linear" , beta_start=0.0_00_85 , beta_end=0.0_12 , prediction_type="v_prediction" , set_alpha_to_one=__SCREAMING_SNAKE_CASE , steps_offset=1 , ) torch.manual_seed(0 ) __a = AutoencoderKL() __a = { # prior components """prior_tokenizer""": prior_tokenizer, """prior_text_encoder""": prior_text_encoder, """prior""": prior, """prior_scheduler""": prior_scheduler, # image noising components """image_normalizer""": image_normalizer, """image_noising_scheduler""": image_noising_scheduler, # regular denoising components """tokenizer""": tokenizer, """text_encoder""": text_encoder, """unet""": unet, """scheduler""": scheduler, """vae""": vae, } return components def _UpperCAmelCase ( self : Optional[int] , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : Optional[int]=0 ): if str(__SCREAMING_SNAKE_CASE ).startswith("mps" ): __a = torch.manual_seed(__SCREAMING_SNAKE_CASE ) else: __a = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(__SCREAMING_SNAKE_CASE ) __a = { """prompt""": """A painting of a squirrel eating a burger""", """generator""": generator, """num_inference_steps""": 2, """prior_num_inference_steps""": 2, """output_type""": """numpy""", } return inputs def _UpperCAmelCase ( self : int ): __a = torch_device == """cpu""" self._test_attention_slicing_forward_pass(test_max_difference=__SCREAMING_SNAKE_CASE ) def _UpperCAmelCase ( self : List[str] ): __a = torch_device in ["""cpu""", """mps"""] self._test_inference_batch_single_identical(test_max_difference=__SCREAMING_SNAKE_CASE ) @slow @require_torch_gpu class A_ ( unittest.TestCase ): def _UpperCAmelCase ( self : int ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def _UpperCAmelCase ( self : Optional[Any] ): __a = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/stable_unclip/stable_unclip_2_1_l_anime_turtle_fp16.npy" ) __a = StableUnCLIPPipeline.from_pretrained("fusing/stable-unclip-2-1-l" , torch_dtype=torch.floataa ) pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) # stable unclip will oom when integration tests are run on a V100, # so turn on memory savings pipe.enable_attention_slicing() pipe.enable_sequential_cpu_offload() __a = torch.Generator(device="cpu" ).manual_seed(0 ) __a = pipe("anime turle" , generator=__SCREAMING_SNAKE_CASE , output_type="np" ) __a = output.images[0] assert image.shape == (7_68, 7_68, 3) assert_mean_pixel_difference(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def _UpperCAmelCase ( self : Dict ): torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats() __a = StableUnCLIPPipeline.from_pretrained("fusing/stable-unclip-2-1-l" , torch_dtype=torch.floataa ) __a = pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) pipe.enable_attention_slicing() pipe.enable_sequential_cpu_offload() __a = pipe( "anime turtle" , prior_num_inference_steps=2 , num_inference_steps=2 , output_type="np" , ) __a = torch.cuda.max_memory_allocated() # make sure that less than 7 GB is allocated assert mem_bytes < 7 * 10**9
197
'''simple docstring''' import argparse import json import math import os import time import traceback import zipfile from collections import Counter import requests def UpperCamelCase__ ( __magic_name__ : str , __magic_name__ : List[Any]=None ) -> Union[str, Any]: '''simple docstring''' snake_case__ : str = None if token is not None: snake_case__ : str = {"""Accept""": """application/vnd.github+json""", """Authorization""": f"Bearer {token}"} snake_case__ : List[Any] = f"https://api.github.com/repos/huggingface/transformers/actions/runs/{workflow_run_id}/jobs?per_page=100" snake_case__ : str = requests.get(__magic_name__ , headers=__magic_name__ ).json() snake_case__ : str = {} try: job_links.update({job["""name"""]: job["""html_url"""] for job in result["""jobs"""]} ) snake_case__ : List[Any] = math.ceil((result["""total_count"""] - 1_00) / 1_00 ) for i in range(__magic_name__ ): snake_case__ : Tuple = requests.get(url + f"&page={i + 2}" , headers=__magic_name__ ).json() job_links.update({job["""name"""]: job["""html_url"""] for job in result["""jobs"""]} ) return job_links except Exception: print(f"Unknown error, could not fetch links:\n{traceback.format_exc()}" ) return {} def UpperCamelCase__ ( __magic_name__ : Optional[int] , __magic_name__ : Optional[Any]=None ) -> List[str]: '''simple docstring''' snake_case__ : Optional[Any] = None if token is not None: snake_case__ : Any = {"""Accept""": """application/vnd.github+json""", """Authorization""": f"Bearer {token}"} snake_case__ : Dict = f"https://api.github.com/repos/huggingface/transformers/actions/runs/{worflow_run_id}/artifacts?per_page=100" snake_case__ : Union[str, Any] = requests.get(__magic_name__ , headers=__magic_name__ ).json() snake_case__ : Dict = {} try: artifacts.update({artifact["""name"""]: artifact["""archive_download_url"""] for artifact in result["""artifacts"""]} ) snake_case__ : List[Any] = math.ceil((result["""total_count"""] - 1_00) / 1_00 ) for i in range(__magic_name__ ): snake_case__ : Dict = requests.get(url + f"&page={i + 2}" , headers=__magic_name__ ).json() artifacts.update({artifact["""name"""]: artifact["""archive_download_url"""] for artifact in result["""artifacts"""]} ) return artifacts except Exception: print(f"Unknown error, could not fetch links:\n{traceback.format_exc()}" ) return {} def UpperCamelCase__ ( __magic_name__ : Optional[int] , __magic_name__ : Optional[Any] , __magic_name__ : Optional[int] , __magic_name__ : Dict ) -> Dict: '''simple docstring''' snake_case__ : Optional[Any] = None if token is not None: snake_case__ : Dict = {"""Accept""": """application/vnd.github+json""", """Authorization""": f"Bearer {token}"} snake_case__ : str = requests.get(__magic_name__ , headers=__magic_name__ , allow_redirects=__magic_name__ ) snake_case__ : Any = result.headers["""Location"""] snake_case__ : Tuple = requests.get(__magic_name__ , allow_redirects=__magic_name__ ) snake_case__ : int = os.path.join(__magic_name__ , f"{artifact_name}.zip" ) with open(__magic_name__ , """wb""" ) as fp: fp.write(response.content ) def UpperCamelCase__ ( __magic_name__ : List[Any] , __magic_name__ : str=None ) -> Union[str, Any]: '''simple docstring''' snake_case__ : Any = [] snake_case__ : Union[str, Any] = [] snake_case__ : Any = None with zipfile.ZipFile(__magic_name__ ) as z: for filename in z.namelist(): if not os.path.isdir(__magic_name__ ): # read the file if filename in ["failures_line.txt", "summary_short.txt", "job_name.txt"]: with z.open(__magic_name__ ) as f: for line in f: snake_case__ : Any = line.decode("""UTF-8""" ).strip() if filename == "failures_line.txt": try: # `error_line` is the place where `error` occurs snake_case__ : str = line[: line.index(""": """ )] snake_case__ : Optional[int] = line[line.index(""": """ ) + len(""": """ ) :] errors.append([error_line, error] ) except Exception: # skip un-related lines pass elif filename == "summary_short.txt" and line.startswith("""FAILED """ ): # `test` is the test method that failed snake_case__ : Dict = line[len("""FAILED """ ) :] failed_tests.append(__magic_name__ ) elif filename == "job_name.txt": snake_case__ : Optional[Any] = line if len(__magic_name__ ) != len(__magic_name__ ): raise ValueError( f"`errors` and `failed_tests` should have the same number of elements. Got {len(__magic_name__ )} for `errors` " f"and {len(__magic_name__ )} for `failed_tests` instead. The test reports in {artifact_zip_path} have some" """ problem.""" ) snake_case__ : Optional[Any] = None if job_name and job_links: snake_case__ : Optional[Any] = job_links.get(__magic_name__ , __magic_name__ ) # A list with elements of the form (line of error, error, failed test) snake_case__ : List[Any] = [x + [y] + [job_link] for x, y in zip(__magic_name__ , __magic_name__ )] return result def UpperCamelCase__ ( __magic_name__ : int , __magic_name__ : Union[str, Any]=None ) -> Union[str, Any]: '''simple docstring''' snake_case__ : str = [] snake_case__ : Dict = [os.path.join(__magic_name__ , __magic_name__ ) for p in os.listdir(__magic_name__ ) if p.endswith(""".zip""" )] for p in paths: errors.extend(get_errors_from_single_artifact(__magic_name__ , job_links=__magic_name__ ) ) return errors def UpperCamelCase__ ( __magic_name__ : Optional[Any] , __magic_name__ : str=None ) -> List[Any]: '''simple docstring''' snake_case__ : Any = Counter() counter.update([x[1] for x in logs] ) snake_case__ : Dict = counter.most_common() snake_case__ : Any = {} for error, count in counts: if error_filter is None or error not in error_filter: snake_case__ : int = {"""count""": count, """failed_tests""": [(x[2], x[0]) for x in logs if x[1] == error]} snake_case__ : Union[str, Any] = dict(sorted(r.items() , key=lambda __magic_name__ : item[1]["count"] , reverse=__magic_name__ ) ) return r def UpperCamelCase__ ( __magic_name__ : List[Any] ) -> List[Any]: '''simple docstring''' snake_case__ : str = test.split("""::""" )[0] if test.startswith("""tests/models/""" ): snake_case__ : Tuple = test.split("""/""" )[2] else: snake_case__ : Any = None return test def UpperCamelCase__ ( __magic_name__ : str , __magic_name__ : Union[str, Any]=None ) -> List[str]: '''simple docstring''' snake_case__ : List[str] = [(x[0], x[1], get_model(x[2] )) for x in logs] snake_case__ : List[Any] = [x for x in logs if x[2] is not None] snake_case__ : Any = {x[2] for x in logs} snake_case__ : Optional[Any] = {} for test in tests: snake_case__ : str = Counter() # count by errors in `test` counter.update([x[1] for x in logs if x[2] == test] ) snake_case__ : Optional[int] = counter.most_common() snake_case__ : Optional[int] = {error: count for error, count in counts if (error_filter is None or error not in error_filter)} snake_case__ : int = sum(error_counts.values() ) if n_errors > 0: snake_case__ : str = {"""count""": n_errors, """errors""": error_counts} snake_case__ : Union[str, Any] = dict(sorted(r.items() , key=lambda __magic_name__ : item[1]["count"] , reverse=__magic_name__ ) ) return r def UpperCamelCase__ ( __magic_name__ : int ) -> Optional[int]: '''simple docstring''' snake_case__ : Optional[Any] = """| no. | error | status |""" snake_case__ : int = """|-:|:-|:-|""" snake_case__ : int = [header, sep] for error in reduced_by_error: snake_case__ : Union[str, Any] = reduced_by_error[error]["""count"""] snake_case__ : Dict = f"| {count} | {error[:1_00]} | |" lines.append(__magic_name__ ) return "\n".join(__magic_name__ ) def UpperCamelCase__ ( __magic_name__ : Dict ) -> List[Any]: '''simple docstring''' snake_case__ : List[Any] = """| model | no. of errors | major error | count |""" snake_case__ : Optional[int] = """|-:|-:|-:|-:|""" snake_case__ : Dict = [header, sep] for model in reduced_by_model: snake_case__ : Tuple = reduced_by_model[model]["""count"""] snake_case__ , snake_case__ : Tuple = list(reduced_by_model[model]["""errors"""].items() )[0] snake_case__ : Optional[int] = f"| {model} | {count} | {error[:60]} | {_count} |" lines.append(__magic_name__ ) return "\n".join(__magic_name__ ) if __name__ == "__main__": A_ : Any = argparse.ArgumentParser() # Required parameters parser.add_argument("--workflow_run_id", type=str, required=True, help="A GitHub Actions workflow run id.") parser.add_argument( "--output_dir", type=str, required=True, help="Where to store the downloaded artifacts and other result files.", ) parser.add_argument("--token", default=None, type=str, help="A token that has actions:read permission.") A_ : int = parser.parse_args() os.makedirs(args.output_dir, exist_ok=True) A_ : Optional[int] = get_job_links(args.workflow_run_id, token=args.token) A_ : Optional[Any] = {} # To deal with `workflow_call` event, where a job name is the combination of the job names in the caller and callee. # For example, `PyTorch 1.11 / Model tests (models/albert, single-gpu)`. if _job_links: for k, v in _job_links.items(): # This is how GitHub actions combine job names. if " / " in k: A_ : int = k.find(" / ") A_ : List[Any] = k[index + len(" / ") :] A_ : List[str] = v with open(os.path.join(args.output_dir, "job_links.json"), "w", encoding="UTF-8") as fp: json.dump(job_links, fp, ensure_ascii=False, indent=4) A_ : int = get_artifacts_links(args.workflow_run_id, token=args.token) with open(os.path.join(args.output_dir, "artifacts.json"), "w", encoding="UTF-8") as fp: json.dump(artifacts, fp, ensure_ascii=False, indent=4) for idx, (name, url) in enumerate(artifacts.items()): download_artifact(name, url, args.output_dir, args.token) # Be gentle to GitHub time.sleep(1) A_ : str = get_all_errors(args.output_dir, job_links=job_links) # `e[1]` is the error A_ : List[str] = Counter() counter.update([e[1] for e in errors]) # print the top 30 most common test errors A_ : Any = counter.most_common(30) for item in most_common: print(item) with open(os.path.join(args.output_dir, "errors.json"), "w", encoding="UTF-8") as fp: json.dump(errors, fp, ensure_ascii=False, indent=4) A_ : Any = reduce_by_error(errors) A_ : Union[str, Any] = reduce_by_model(errors) A_ : Any = make_github_table(reduced_by_error) A_ : Optional[Any] = make_github_table_per_model(reduced_by_model) with open(os.path.join(args.output_dir, "reduced_by_error.txt"), "w", encoding="UTF-8") as fp: fp.write(sa) with open(os.path.join(args.output_dir, "reduced_by_model.txt"), "w", encoding="UTF-8") as fp: fp.write(sa)
38
0
from ...configuration_utils import PretrainedConfig from ...utils import logging _UpperCAmelCase = logging.get_logger(__name__) _UpperCAmelCase = {"openai-gpt": "https://huggingface.co/openai-gpt/resolve/main/config.json"} class _UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = '''openai-gpt''' SCREAMING_SNAKE_CASE : Optional[Any] = { '''max_position_embeddings''': '''n_positions''', '''hidden_size''': '''n_embd''', '''num_attention_heads''': '''n_head''', '''num_hidden_layers''': '''n_layer''', } def __init__( self : Optional[Any] , UpperCamelCase__ : List[str]=40478 , UpperCamelCase__ : List[str]=512 , UpperCamelCase__ : Optional[int]=768 , UpperCamelCase__ : int=12 , UpperCamelCase__ : Optional[int]=12 , UpperCamelCase__ : Any="gelu" , UpperCamelCase__ : List[Any]=0.1 , UpperCamelCase__ : List[Any]=0.1 , UpperCamelCase__ : Any=0.1 , UpperCamelCase__ : Union[str, Any]=1e-5 , UpperCamelCase__ : Optional[Any]=0.02 , UpperCamelCase__ : Optional[Any]="cls_index" , UpperCamelCase__ : Tuple=True , UpperCamelCase__ : str=None , UpperCamelCase__ : int=True , UpperCamelCase__ : Dict=0.1 , **UpperCamelCase__ : List[str] , ): A = vocab_size A = n_positions A = n_embd A = n_layer A = n_head A = afn A = resid_pdrop A = embd_pdrop A = attn_pdrop A = layer_norm_epsilon A = initializer_range A = summary_type A = summary_use_proj A = summary_activation A = summary_first_dropout A = summary_proj_to_labels super().__init__(**__SCREAMING_SNAKE_CASE )
699
'''simple docstring''' # Lint as: python3 import os import re import urllib.parse from pathlib import Path from typing import Callable, List, Optional, Union from zipfile import ZipFile from ..utils.file_utils import cached_path, hf_github_url from ..utils.logging import get_logger from ..utils.version import Version A_ : Tuple = get_logger(__name__) class __snake_case : '''simple docstring''' lowerCamelCase__ = '''dummy_data''' lowerCamelCase__ = '''datasets''' lowerCamelCase__ = False def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = False , __SCREAMING_SNAKE_CASE = True , __SCREAMING_SNAKE_CASE = None , ): snake_case__ : List[Any] = 0 snake_case__ : Union[str, Any] = dataset_name snake_case__ : Optional[int] = cache_dir snake_case__ : Union[str, Any] = use_local_dummy_data snake_case__ : int = config # download_callbacks take a single url as input snake_case__ : List[Callable] = download_callbacks or [] # if False, it doesn't load existing files and it returns the paths of the dummy files relative # to the dummy_data zip file root snake_case__ : Union[str, Any] = load_existing_dummy_data # TODO(PVP, QL) might need to make this more general snake_case__ : Union[str, Any] = str(__SCREAMING_SNAKE_CASE ) # to be downloaded snake_case__ : List[str] = None snake_case__ : List[str] = None @property def __UpperCamelCase ( self ): if self._dummy_file is None: snake_case__ : List[str] = self.download_dummy_data() return self._dummy_file @property def __UpperCamelCase ( self ): if self.config is not None: # structure is dummy / config_name / version_name return os.path.join("""dummy""" , self.config.name , self.version_name ) # structure is dummy / version_name return os.path.join("""dummy""" , self.version_name ) @property def __UpperCamelCase ( self ): return os.path.join(self.dummy_data_folder , """dummy_data.zip""" ) def __UpperCamelCase ( self ): snake_case__ : Optional[Any] = ( self.local_path_to_dummy_data if self.use_local_dummy_data is True else self.github_path_to_dummy_data ) snake_case__ : Optional[int] = cached_path( __SCREAMING_SNAKE_CASE , cache_dir=self.cache_dir , extract_compressed_file=__SCREAMING_SNAKE_CASE , force_extract=__SCREAMING_SNAKE_CASE ) return os.path.join(__SCREAMING_SNAKE_CASE , self.dummy_file_name ) @property def __UpperCamelCase ( self ): return os.path.join(self.datasets_scripts_dir , self.dataset_name , self.dummy_zip_file ) @property def __UpperCamelCase ( self ): if self._bucket_url is None: snake_case__ : List[str] = hf_github_url(self.dataset_name , self.dummy_zip_file.replace(os.sep , """/""" ) ) return self._bucket_url @property def __UpperCamelCase ( self ): # return full path if its a dir if os.path.isdir(self.dummy_file ): return self.dummy_file # else cut off path to file -> example `xsum`. return "/".join(self.dummy_file.replace(os.sep , """/""" ).split("""/""" )[:-1] ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , *__SCREAMING_SNAKE_CASE ): if self.load_existing_dummy_data: # dummy data is downloaded and tested snake_case__ : List[Any] = self.dummy_file else: # dummy data cannot be downloaded and only the path to dummy file is returned snake_case__ : List[Any] = self.dummy_file_name # special case when data_url is a dict if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): return self.create_dummy_data_dict(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) elif isinstance(__SCREAMING_SNAKE_CASE , (list, tuple) ): return self.create_dummy_data_list(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else: return self.create_dummy_data_single(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , *__SCREAMING_SNAKE_CASE ): return self.download_and_extract(__SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): return self.download_and_extract(__SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): return path def __UpperCamelCase ( self ): return {} def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): snake_case__ : int = {} for key, single_urls in data_url.items(): for download_callback in self.download_callbacks: if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): for single_url in single_urls: download_callback(__SCREAMING_SNAKE_CASE ) else: snake_case__ : List[str] = single_urls download_callback(__SCREAMING_SNAKE_CASE ) # we force the name of each key to be the last file / folder name of the url path # if the url has arguments, we need to encode them with urllib.parse.quote_plus if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): snake_case__ : Tuple = [os.path.join(__SCREAMING_SNAKE_CASE , urllib.parse.quote_plus(Path(__SCREAMING_SNAKE_CASE ).name ) ) for x in single_urls] else: snake_case__ : List[Any] = single_urls snake_case__ : Tuple = os.path.join(__SCREAMING_SNAKE_CASE , urllib.parse.quote_plus(Path(__SCREAMING_SNAKE_CASE ).name ) ) snake_case__ : Optional[int] = value # make sure that values are unique if all(isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) for i in dummy_data_dict.values() ) and len(set(dummy_data_dict.values() ) ) < len( dummy_data_dict.values() ): # append key to value to make its name unique snake_case__ : List[Any] = {key: value + key for key, value in dummy_data_dict.items()} return dummy_data_dict def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): snake_case__ : Dict = [] # trick: if there are many shards named like `data.txt-000001-of-00300`, only use the first one snake_case__ : Tuple = all(bool(re.findall("""[0-9]{3,}-of-[0-9]{3,}""" , __SCREAMING_SNAKE_CASE ) ) for url in data_url ) snake_case__ : List[Any] = all( url.startswith("""https://ftp.ncbi.nlm.nih.gov/pubmed/baseline/pubmed""" ) for url in data_url ) if data_url and (is_tf_records or is_pubmed_records): snake_case__ : List[str] = [data_url[0]] * len(__SCREAMING_SNAKE_CASE ) for single_url in data_url: for download_callback in self.download_callbacks: download_callback(__SCREAMING_SNAKE_CASE ) # we force the name of each key to be the last file / folder name of the url path # if the url has arguments, we need to encode them with urllib.parse.quote_plus snake_case__ : List[Any] = os.path.join(__SCREAMING_SNAKE_CASE , urllib.parse.quote_plus(single_url.split("""/""" )[-1] ) ) dummy_data_list.append(__SCREAMING_SNAKE_CASE ) return dummy_data_list def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): for download_callback in self.download_callbacks: download_callback(__SCREAMING_SNAKE_CASE ) # we force the name of each key to be the last file / folder name of the url path # if the url has arguments, we need to encode them with urllib.parse.quote_plus snake_case__ : Any = os.path.join(__SCREAMING_SNAKE_CASE , urllib.parse.quote_plus(data_url.split("""/""" )[-1] ) ) if os.path.exists(__SCREAMING_SNAKE_CASE ) or not self.load_existing_dummy_data: return value else: # Backward compatibility, maybe deprecate at one point. # For many datasets with single url calls to dl_manager.download_and_extract, # the dummy_data.zip file is actually the zipped downloaded file # while now we expected the dummy_data.zip file to be a directory containing # the downloaded file. return path_to_dummy_data def __UpperCamelCase ( self ): pass def __UpperCamelCase ( self ): pass def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE ): def _iter_archive_members(__SCREAMING_SNAKE_CASE ): # this preserves the order of the members inside the ZIP archive snake_case__ : List[str] = Path(self.dummy_file ).parent snake_case__ : Dict = path.relative_to(__SCREAMING_SNAKE_CASE ) with ZipFile(self.local_path_to_dummy_data ) as zip_file: snake_case__ : Optional[int] = zip_file.namelist() for member in members: if member.startswith(relative_path.as_posix() ): yield dummy_parent_path.joinpath(__SCREAMING_SNAKE_CASE ) snake_case__ : Any = Path(__SCREAMING_SNAKE_CASE ) snake_case__ : int = _iter_archive_members(__SCREAMING_SNAKE_CASE ) if self.use_local_dummy_data else path.rglob("""*""" ) for file_path in file_paths: if file_path.is_file() and not file_path.name.startswith((""".""", """__""") ): yield file_path.relative_to(__SCREAMING_SNAKE_CASE ).as_posix(), file_path.open("""rb""" ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE ): if not isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): snake_case__ : Optional[int] = [paths] for path in paths: if os.path.isfile(__SCREAMING_SNAKE_CASE ): if os.path.basename(__SCREAMING_SNAKE_CASE ).startswith((""".""", """__""") ): return yield path else: for dirpath, dirnames, filenames in os.walk(__SCREAMING_SNAKE_CASE ): if os.path.basename(__SCREAMING_SNAKE_CASE ).startswith((""".""", """__""") ): continue dirnames.sort() for filename in sorted(__SCREAMING_SNAKE_CASE ): if filename.startswith((""".""", """__""") ): continue yield os.path.join(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE )
38
0
import math def snake_case_ ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): return math.pow(_SCREAMING_SNAKE_CASE , 2 ) - a def snake_case_ ( _SCREAMING_SNAKE_CASE ): return 2 * x def snake_case_ ( _SCREAMING_SNAKE_CASE ): __lowercase = 2.0 while start <= a: __lowercase = math.pow(_SCREAMING_SNAKE_CASE , 2 ) return start def snake_case_ ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = 9_9_9_9 , _SCREAMING_SNAKE_CASE = 0.0_0_0_0_0_0_0_0_0_0_0_0_0_1 ): if a < 0: raise ValueError("math domain error" ) __lowercase = get_initial_point(_SCREAMING_SNAKE_CASE ) for _ in range(_SCREAMING_SNAKE_CASE ): __lowercase = value __lowercase = value - fx(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) / fx_derivative(_SCREAMING_SNAKE_CASE ) if abs(prev_value - value ) < tolerance: return value return value if __name__ == "__main__": from doctest import testmod testmod()
402
'''simple docstring''' import random import unittest import torch from diffusers import IFImgaImgSuperResolutionPipeline from diffusers.utils import floats_tensor from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import skip_mps, torch_device from ..pipeline_params import TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS from ..test_pipelines_common import PipelineTesterMixin from . import IFPipelineTesterMixin @skip_mps class __snake_case ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowerCamelCase__ = IFImgaImgSuperResolutionPipeline lowerCamelCase__ = TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {'''width''', '''height'''} lowerCamelCase__ = TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS.union({'''original_image'''} ) lowerCamelCase__ = PipelineTesterMixin.required_optional_params - {'''latents'''} def __UpperCamelCase ( self ): return self._get_superresolution_dummy_components() def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=0 ): if str(__SCREAMING_SNAKE_CASE ).startswith("""mps""" ): snake_case__ : List[Any] = torch.manual_seed(__SCREAMING_SNAKE_CASE ) else: snake_case__ : Tuple = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(__SCREAMING_SNAKE_CASE ) snake_case__ : Tuple = floats_tensor((1, 3, 3_2, 3_2) , rng=random.Random(__SCREAMING_SNAKE_CASE ) ).to(__SCREAMING_SNAKE_CASE ) snake_case__ : Union[str, Any] = floats_tensor((1, 3, 1_6, 1_6) , rng=random.Random(__SCREAMING_SNAKE_CASE ) ).to(__SCREAMING_SNAKE_CASE ) snake_case__ : int = { """prompt""": """A painting of a squirrel eating a burger""", """image""": image, """original_image""": original_image, """generator""": generator, """num_inference_steps""": 2, """output_type""": """numpy""", } return inputs @unittest.skipIf( torch_device != """cuda""" or not is_xformers_available() , reason="""XFormers attention is only available with CUDA and `xformers` installed""" , ) def __UpperCamelCase ( self ): self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1e-3 ) def __UpperCamelCase ( self ): self._test_save_load_optional_components() @unittest.skipIf(torch_device != """cuda""" , reason="""float16 requires CUDA""" ) def __UpperCamelCase ( self ): # Due to non-determinism in save load of the hf-internal-testing/tiny-random-t5 text encoder super().test_save_load_floataa(expected_max_diff=1e-1 ) def __UpperCamelCase ( self ): self._test_attention_slicing_forward_pass(expected_max_diff=1e-2 ) def __UpperCamelCase ( self ): self._test_save_load_local() def __UpperCamelCase ( self ): self._test_inference_batch_single_identical( expected_max_diff=1e-2 , )
38
0
import argparse import re import numpy as np import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( SamConfig, SamImageProcessor, SamModel, SamProcessor, SamVisionConfig, ) A_ = { "iou_prediction_head.layers.0": "iou_prediction_head.proj_in", "iou_prediction_head.layers.1": "iou_prediction_head.layers.0", "iou_prediction_head.layers.2": "iou_prediction_head.proj_out", "mask_decoder.output_upscaling.0": "mask_decoder.upscale_conv1", "mask_decoder.output_upscaling.1": "mask_decoder.upscale_layer_norm", "mask_decoder.output_upscaling.3": "mask_decoder.upscale_conv2", "mask_downscaling.0": "mask_embed.conv1", "mask_downscaling.1": "mask_embed.layer_norm1", "mask_downscaling.3": "mask_embed.conv2", "mask_downscaling.4": "mask_embed.layer_norm2", "mask_downscaling.6": "mask_embed.conv3", "point_embeddings": "point_embed", "pe_layer.positional_encoding_gaussian_matrix": "shared_embedding.positional_embedding", "image_encoder": "vision_encoder", "neck.0": "neck.conv1", "neck.1": "neck.layer_norm1", "neck.2": "neck.conv2", "neck.3": "neck.layer_norm2", "patch_embed.proj": "patch_embed.projection", ".norm": ".layer_norm", "blocks": "layers", } def UpperCAmelCase ( UpperCAmelCase )-> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE_ = {} state_dict.pop('''pixel_mean''' ,UpperCAmelCase ) state_dict.pop('''pixel_std''' ,UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = R""".*.output_hypernetworks_mlps.(\d+).layers.(\d+).*""" for key, value in state_dict.items(): for key_to_modify, new_key in KEYS_TO_MODIFY_MAPPING.items(): if key_to_modify in key: SCREAMING_SNAKE_CASE_ = key.replace(UpperCAmelCase ,UpperCAmelCase ) if re.match(UpperCAmelCase ,UpperCAmelCase ): SCREAMING_SNAKE_CASE_ = int(re.match(UpperCAmelCase ,UpperCAmelCase ).group(2 ) ) if layer_nb == 0: SCREAMING_SNAKE_CASE_ = key.replace('''layers.0''' ,'''proj_in''' ) elif layer_nb == 1: SCREAMING_SNAKE_CASE_ = key.replace('''layers.1''' ,'''layers.0''' ) elif layer_nb == 2: SCREAMING_SNAKE_CASE_ = key.replace('''layers.2''' ,'''proj_out''' ) SCREAMING_SNAKE_CASE_ = value SCREAMING_SNAKE_CASE_ = model_state_dict[ """prompt_encoder.shared_embedding.positional_embedding""" ] return model_state_dict def UpperCAmelCase ( UpperCAmelCase ,UpperCAmelCase ,UpperCAmelCase ,UpperCAmelCase="ybelkada/segment-anything" )-> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE_ = hf_hub_download(UpperCAmelCase ,f'''checkpoints/{model_name}.pth''' ) if "sam_vit_b" in model_name: SCREAMING_SNAKE_CASE_ = SamConfig() elif "sam_vit_l" in model_name: SCREAMING_SNAKE_CASE_ = SamVisionConfig( hidden_size=1024 ,num_hidden_layers=24 ,num_attention_heads=16 ,global_attn_indexes=[5, 11, 17, 23] ,) SCREAMING_SNAKE_CASE_ = SamConfig( vision_config=UpperCAmelCase ,) elif "sam_vit_h" in model_name: SCREAMING_SNAKE_CASE_ = SamVisionConfig( hidden_size=1280 ,num_hidden_layers=32 ,num_attention_heads=16 ,global_attn_indexes=[7, 15, 23, 31] ,) SCREAMING_SNAKE_CASE_ = SamConfig( vision_config=UpperCAmelCase ,) SCREAMING_SNAKE_CASE_ = torch.load(UpperCAmelCase ,map_location='''cpu''' ) SCREAMING_SNAKE_CASE_ = replace_keys(UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = SamImageProcessor() SCREAMING_SNAKE_CASE_ = SamProcessor(image_processor=UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = SamModel(UpperCAmelCase ) hf_model.load_state_dict(UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = hf_model.to('''cuda''' ) SCREAMING_SNAKE_CASE_ = """https://huggingface.co/ybelkada/segment-anything/resolve/main/assets/car.png""" SCREAMING_SNAKE_CASE_ = Image.open(requests.get(UpperCAmelCase ,stream=UpperCAmelCase ).raw ).convert('''RGB''' ) SCREAMING_SNAKE_CASE_ = [[[400, 650]]] SCREAMING_SNAKE_CASE_ = [[1]] SCREAMING_SNAKE_CASE_ = processor(images=np.array(UpperCAmelCase ) ,return_tensors='''pt''' ).to('''cuda''' ) with torch.no_grad(): SCREAMING_SNAKE_CASE_ = hf_model(**UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = output.iou_scores.squeeze() if model_name == "sam_vit_h_4b8939": assert scores[-1].item() == 0.5_7_9_8_9_0_2_5_1_1_5_9_6_6_8 SCREAMING_SNAKE_CASE_ = processor( images=np.array(UpperCAmelCase ) ,input_points=UpperCAmelCase ,input_labels=UpperCAmelCase ,return_tensors='''pt''' ).to('''cuda''' ) with torch.no_grad(): SCREAMING_SNAKE_CASE_ = hf_model(**UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = output.iou_scores.squeeze() assert scores[-1].item() == 0.9_7_1_2_6_0_3_0_9_2_1_9_3_6_0_4 SCREAMING_SNAKE_CASE_ = ((75, 275, 1725, 850),) SCREAMING_SNAKE_CASE_ = processor(images=np.array(UpperCAmelCase ) ,input_boxes=UpperCAmelCase ,return_tensors='''pt''' ).to('''cuda''' ) with torch.no_grad(): SCREAMING_SNAKE_CASE_ = hf_model(**UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = output.iou_scores.squeeze() assert scores[-1].item() == 0.8_6_8_6_0_1_5_6_0_5_9_2_6_5_1_4 # Test with 2 points and 1 image. SCREAMING_SNAKE_CASE_ = [[[400, 650], [800, 650]]] SCREAMING_SNAKE_CASE_ = [[1, 1]] SCREAMING_SNAKE_CASE_ = processor( images=np.array(UpperCAmelCase ) ,input_points=UpperCAmelCase ,input_labels=UpperCAmelCase ,return_tensors='''pt''' ).to('''cuda''' ) with torch.no_grad(): SCREAMING_SNAKE_CASE_ = hf_model(**UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = output.iou_scores.squeeze() assert scores[-1].item() == 0.9_9_3_6_0_4_7_7_9_2_4_3_4_6_9_2 if __name__ == "__main__": A_ = argparse.ArgumentParser() A_ = ["sam_vit_b_01ec64", "sam_vit_h_4b8939", "sam_vit_l_0b3195"] parser.add_argument( "--model_name", default="sam_vit_h_4b8939", choices=choices, type=str, help="Path to hf config.json of model to convert", ) parser.add_argument("--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.") parser.add_argument( "--push_to_hub", action="store_true", help="Whether to push the model and processor to the hub after converting", ) parser.add_argument( "--model_hub_id", default="ybelkada/segment-anything", choices=choices, type=str, help="Path to hf config.json of model to convert", ) A_ = parser.parse_args() convert_sam_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub, args.model_hub_id)
393
'''simple docstring''' # Copyright 2021 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import argparse from ...utils.dataclasses import ( ComputeEnvironment, DistributedType, DynamoBackend, PrecisionType, SageMakerDistributedType, ) from ..menu import BulletMenu A_ : Dict = [ "EAGER", "AOT_EAGER", "INDUCTOR", "NVFUSER", "AOT_NVFUSER", "AOT_CUDAGRAPHS", "OFI", "FX2TRT", "ONNXRT", "IPEX", ] def UpperCamelCase__ ( __magic_name__ : List[Any] , __magic_name__ : List[Any]=None , __magic_name__ : List[str]=None , __magic_name__ : List[str]=None ) -> Dict: '''simple docstring''' snake_case__ : Optional[int] = True while ask_again: snake_case__ : Optional[Any] = input(__magic_name__ ) try: if default is not None and len(__magic_name__ ) == 0: return default return convert_value(__magic_name__ ) if convert_value is not None else result except Exception: if error_message is not None: print(__magic_name__ ) def UpperCamelCase__ ( __magic_name__ : List[str] , __magic_name__ : Any=[] , __magic_name__ : Optional[int]=None , __magic_name__ : int=0 ) -> Optional[int]: '''simple docstring''' snake_case__ : Union[str, Any] = BulletMenu(__magic_name__ , __magic_name__ ) snake_case__ : Optional[Any] = menu.run(default_choice=__magic_name__ ) return convert_value(__magic_name__ ) if convert_value is not None else result def UpperCamelCase__ ( __magic_name__ : Any ) -> int: '''simple docstring''' snake_case__ : Tuple = int(__magic_name__ ) return ComputeEnvironment(["""LOCAL_MACHINE""", """AMAZON_SAGEMAKER"""][value] ) def UpperCamelCase__ ( __magic_name__ : str ) -> Tuple: '''simple docstring''' snake_case__ : List[Any] = int(__magic_name__ ) return DistributedType(["""NO""", """MULTI_CPU""", """MULTI_XPU""", """MULTI_GPU""", """MULTI_NPU""", """TPU"""][value] ) def UpperCamelCase__ ( __magic_name__ : List[str] ) -> List[Any]: '''simple docstring''' snake_case__ : Union[str, Any] = int(__magic_name__ ) return DynamoBackend(DYNAMO_BACKENDS[value] ).value def UpperCamelCase__ ( __magic_name__ : List[str] ) -> Union[str, Any]: '''simple docstring''' snake_case__ : Optional[Any] = int(__magic_name__ ) return PrecisionType(["""no""", """fp16""", """bf16""", """fp8"""][value] ) def UpperCamelCase__ ( __magic_name__ : Optional[int] ) -> List[Any]: '''simple docstring''' snake_case__ : Optional[Any] = int(__magic_name__ ) return SageMakerDistributedType(["""NO""", """DATA_PARALLEL""", """MODEL_PARALLEL"""][value] ) def UpperCamelCase__ ( __magic_name__ : Dict ) -> Tuple: '''simple docstring''' return {"yes": True, "no": False}[value.lower()] class __snake_case ( argparse.RawDescriptionHelpFormatter ): '''simple docstring''' def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): snake_case__ : str = super()._format_usage(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) snake_case__ : str = usage.replace("""<command> [<args>] """ , """""" ) return usage
38
0
"""simple docstring""" import logging import os import quant_trainer import torch from torch.utils.data import DataLoader from transformers import Trainer, is_torch_tpu_available from transformers.trainer_utils import PredictionOutput SCREAMING_SNAKE_CASE__ : Dict =logging.getLogger(__name__) if is_torch_tpu_available(check_device=False): import torch_xla.core.xla_model as xm import torch_xla.debug.metrics as met class _UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): """simple docstring""" def __init__( self , *_lowercase , _lowercase=None , _lowercase=None , _lowercase=None , **_lowercase ) -> Union[str, Any]: super().__init__(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) _lowerCamelCase : List[str] = eval_examples _lowerCamelCase : List[str] = post_process_function _lowerCamelCase : Optional[Any] = quant_trainer_args _lowerCamelCase : List[str] = 128 # default number of calibration samples def a__ ( self , _lowercase=None ) -> List[Any]: if calib_dataset is None and self.calib_dataset is None: raise ValueError('''Trainer: calibration requires an calib_dataset.''' ) _lowerCamelCase : List[str] = calib_dataset if calib_dataset is not None else self.calib_dataset _lowerCamelCase : Dict = self._remove_unused_columns(__SCREAMING_SNAKE_CASE , description='''Calibration''' ) return DataLoader( __SCREAMING_SNAKE_CASE , batch_size=self.args.eval_batch_size , collate_fn=self.data_collator , drop_last=self.args.dataloader_drop_last , num_workers=self.args.dataloader_num_workers , pin_memory=self.args.dataloader_pin_memory , shuffle=__SCREAMING_SNAKE_CASE , ) def a__ ( self , _lowercase=None ) -> Optional[Any]: _lowerCamelCase : List[Any] = self.train_dataset if calib_dataset is None else calib_dataset _lowerCamelCase : Dict = self.get_calib_dataloader(__SCREAMING_SNAKE_CASE ) _lowerCamelCase : Optional[int] = self.model quant_trainer.configure_model(__SCREAMING_SNAKE_CASE , self.quant_trainer_args , calib=__SCREAMING_SNAKE_CASE ) model.eval() quant_trainer.enable_calibration(__SCREAMING_SNAKE_CASE ) logger.info('''***** Running calibration *****''' ) logger.info(F''' Num examples = {self.calib_num}''' ) logger.info(F''' Batch size = {calib_dataloader.batch_size}''' ) for step, inputs in enumerate(__SCREAMING_SNAKE_CASE ): # Prediction step _lowerCamelCase : Any = self.prediction_step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , prediction_loss_only=__SCREAMING_SNAKE_CASE ) if (step + 1) * calib_dataloader.batch_size >= self.calib_num: break quant_trainer.finish_calibration(__SCREAMING_SNAKE_CASE , self.quant_trainer_args ) _lowerCamelCase : Optional[Any] = model def a__ ( self , _lowercase=None , _lowercase=None , _lowercase=None , _lowercase = "eval" ) -> Tuple: _lowerCamelCase : Dict = self.eval_dataset if eval_dataset is None else eval_dataset _lowerCamelCase : Dict = self.get_eval_dataloader(__SCREAMING_SNAKE_CASE ) _lowerCamelCase : str = self.eval_examples if eval_examples is None else eval_examples # Temporarily disable metric computation, we will do it in the loop here. _lowerCamelCase : Tuple = self.compute_metrics _lowerCamelCase : Tuple = None _lowerCamelCase : Optional[int] = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop try: _lowerCamelCase : Optional[Any] = eval_loop( __SCREAMING_SNAKE_CASE , description='''Evaluation''' , prediction_loss_only=True if compute_metrics is None else None , ignore_keys=__SCREAMING_SNAKE_CASE , ) finally: _lowerCamelCase : List[str] = compute_metrics if self.post_process_function is not None and self.compute_metrics is not None: _lowerCamelCase : Any = self.post_process_function(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , output.predictions ) _lowerCamelCase : str = self.compute_metrics(__SCREAMING_SNAKE_CASE ) # Prefix all keys with metric_key_prefix + '_' for key in list(metrics.keys() ): if not key.startswith(F'''{metric_key_prefix}_''' ): _lowerCamelCase : str = metrics.pop(__SCREAMING_SNAKE_CASE ) self.log(__SCREAMING_SNAKE_CASE ) else: _lowerCamelCase : Union[str, Any] = {} if self.args.tpu_metrics_debug or self.args.debug: # tpu-comment: Logging debug metrics for PyTorch/XLA (compile, execute times, ops, etc.) xm.master_print(met.metrics_report() ) _lowerCamelCase : int = self.callback_handler.on_evaluate(self.args , self.state , self.control , __SCREAMING_SNAKE_CASE ) return metrics def a__ ( self , _lowercase , _lowercase , _lowercase=None , _lowercase = "test" ) -> int: _lowerCamelCase : int = self.get_test_dataloader(__SCREAMING_SNAKE_CASE ) # Temporarily disable metric computation, we will do it in the loop here. _lowerCamelCase : Dict = self.compute_metrics _lowerCamelCase : List[str] = None _lowerCamelCase : Tuple = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop try: _lowerCamelCase : str = eval_loop( __SCREAMING_SNAKE_CASE , description='''Prediction''' , prediction_loss_only=True if compute_metrics is None else None , ignore_keys=__SCREAMING_SNAKE_CASE , ) finally: _lowerCamelCase : Union[str, Any] = compute_metrics if self.post_process_function is None or self.compute_metrics is None: return output _lowerCamelCase : List[str] = self.post_process_function(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , output.predictions , '''predict''' ) _lowerCamelCase : Any = self.compute_metrics(__SCREAMING_SNAKE_CASE ) # Prefix all keys with metric_key_prefix + '_' for key in list(metrics.keys() ): if not key.startswith(F'''{metric_key_prefix}_''' ): _lowerCamelCase : Optional[int] = metrics.pop(__SCREAMING_SNAKE_CASE ) return PredictionOutput(predictions=predictions.predictions , label_ids=predictions.label_ids , metrics=__SCREAMING_SNAKE_CASE ) def a__ ( self , _lowercase="./" ) -> Tuple: _lowerCamelCase : List[Any] = self.eval_dataset _lowerCamelCase : Union[str, Any] = self.get_eval_dataloader(__SCREAMING_SNAKE_CASE ) _lowerCamelCase : List[str] = next(iter(__SCREAMING_SNAKE_CASE ) ) # saving device - to make it consistent _lowerCamelCase : Union[str, Any] = torch.device('''cuda''' if torch.cuda.is_available() else '''cpu''' ) # convert to tuple _lowerCamelCase : Tuple = tuple(v.to(__SCREAMING_SNAKE_CASE ) for k, v in batch.items() ) logger.info('''Converting model to be onnx compatible''' ) from pytorch_quantization.nn import TensorQuantizer _lowerCamelCase : Optional[Any] = True _lowerCamelCase : Optional[Any] = self.model.to(__SCREAMING_SNAKE_CASE ) model.eval() model.float() _lowerCamelCase : List[Any] = model.module if hasattr(__SCREAMING_SNAKE_CASE , '''module''' ) else model quant_trainer.configure_model(__SCREAMING_SNAKE_CASE , self.quant_trainer_args ) _lowerCamelCase : Tuple = os.path.join(__SCREAMING_SNAKE_CASE , '''model.onnx''' ) logger.info(F'''exporting model to {output_model_file}''' ) _lowerCamelCase : List[str] = {0: """batch_size""", 1: """seq_len"""} torch.onnx.export( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , export_params=__SCREAMING_SNAKE_CASE , opset_version=13 , do_constant_folding=__SCREAMING_SNAKE_CASE , input_names=['''input_ids''', '''attention_mask''', '''token_type_ids'''] , output_names=['''output_start_logits''', '''output_end_logits'''] , dynamic_axes={ '''input_ids''': axes, '''attention_mask''': axes, '''token_type_ids''': axes, '''output_start_logits''': axes, '''output_end_logits''': axes, } , verbose=__SCREAMING_SNAKE_CASE , ) logger.info('''onnx export finished''' )
434
'''simple docstring''' from __future__ import annotations def UpperCamelCase__ ( __magic_name__ : list ) -> float: '''simple docstring''' if not nums: raise ValueError("""List is empty""" ) return sum(__magic_name__ ) / len(__magic_name__ ) if __name__ == "__main__": import doctest doctest.testmod()
38
0
import logging import os from typing import List, TextIO, Union from conllu import parse_incr from utils_ner import InputExample, Split, TokenClassificationTask snake_case : List[str] = logging.getLogger(__name__) class snake_case_ (__SCREAMING_SNAKE_CASE ): def __init__( self :Any ,__snake_case :List[str]=-1 ) -> Optional[int]: # in NER datasets, the last column is usually reserved for NER label a__ = label_idx def lowerCamelCase__( self :Union[str, Any] ,__snake_case :Union[str, Any] ,__snake_case :int ) -> str: if isinstance(__SCREAMING_SNAKE_CASE ,__SCREAMING_SNAKE_CASE ): a__ = mode.value a__ = os.path.join(__SCREAMING_SNAKE_CASE ,F'{mode}.txt' ) a__ = 1 a__ = [] with open(__SCREAMING_SNAKE_CASE ,encoding='utf-8' ) as f: a__ = [] a__ = [] for line in f: if line.startswith('-DOCSTART-' ) or line == "" or line == "\n": if words: examples.append(InputExample(guid=F'{mode}-{guid_index}' ,words=__SCREAMING_SNAKE_CASE ,labels=__SCREAMING_SNAKE_CASE ) ) guid_index += 1 a__ = [] a__ = [] else: a__ = line.split(' ' ) words.append(splits[0] ) if len(__SCREAMING_SNAKE_CASE ) > 1: labels.append(splits[self.label_idx].replace('\n' ,'' ) ) else: # Examples could have no label for mode = "test" labels.append('O' ) if words: examples.append(InputExample(guid=F'{mode}-{guid_index}' ,words=__SCREAMING_SNAKE_CASE ,labels=__SCREAMING_SNAKE_CASE ) ) return examples def lowerCamelCase__( self :List[Any] ,__snake_case :int ,__snake_case :Any ,__snake_case :Tuple ) -> Optional[Any]: a__ = 0 for line in test_input_reader: if line.startswith('-DOCSTART-' ) or line == "" or line == "\n": writer.write(__SCREAMING_SNAKE_CASE ) if not preds_list[example_id]: example_id += 1 elif preds_list[example_id]: a__ = line.split()[0] + """ """ + preds_list[example_id].pop(0 ) + """\n""" writer.write(__SCREAMING_SNAKE_CASE ) else: logger.warning('Maximum sequence length exceeded: No prediction for \'%s\'.' ,line.split()[0] ) def lowerCamelCase__( self :Dict ,__snake_case :int ) -> Tuple: if path: with open(__SCREAMING_SNAKE_CASE ,'r' ) as f: a__ = f.read().splitlines() if "O" not in labels: a__ = ["""O"""] + labels return labels else: return ["O", "B-MISC", "I-MISC", "B-PER", "I-PER", "B-ORG", "I-ORG", "B-LOC", "I-LOC"] class snake_case_ (__SCREAMING_SNAKE_CASE ): def __init__( self :List[str] ) -> Any: # in CONLL2003 dataset chunk column is second-to-last super().__init__(label_idx=-2 ) def lowerCamelCase__( self :Optional[int] ,__snake_case :Any ) -> Tuple: if path: with open(__SCREAMING_SNAKE_CASE ,'r' ) as f: a__ = f.read().splitlines() if "O" not in labels: a__ = ["""O"""] + labels return labels else: return [ "O", "B-ADVP", "B-INTJ", "B-LST", "B-PRT", "B-NP", "B-SBAR", "B-VP", "B-ADJP", "B-CONJP", "B-PP", "I-ADVP", "I-INTJ", "I-LST", "I-PRT", "I-NP", "I-SBAR", "I-VP", "I-ADJP", "I-CONJP", "I-PP", ] class snake_case_ (__SCREAMING_SNAKE_CASE ): def lowerCamelCase__( self :List[str] ,__snake_case :Tuple ,__snake_case :Tuple ) -> List[Any]: if isinstance(__SCREAMING_SNAKE_CASE ,__SCREAMING_SNAKE_CASE ): a__ = mode.value a__ = os.path.join(__SCREAMING_SNAKE_CASE ,F'{mode}.txt' ) a__ = 1 a__ = [] with open(__SCREAMING_SNAKE_CASE ,encoding='utf-8' ) as f: for sentence in parse_incr(__SCREAMING_SNAKE_CASE ): a__ = [] a__ = [] for token in sentence: words.append(token['form'] ) labels.append(token['upos'] ) assert len(__SCREAMING_SNAKE_CASE ) == len(__SCREAMING_SNAKE_CASE ) if words: examples.append(InputExample(guid=F'{mode}-{guid_index}' ,words=__SCREAMING_SNAKE_CASE ,labels=__SCREAMING_SNAKE_CASE ) ) guid_index += 1 return examples def lowerCamelCase__( self :Optional[int] ,__snake_case :Union[str, Any] ,__snake_case :Optional[Any] ,__snake_case :int ) -> List[str]: a__ = 0 for sentence in parse_incr(__SCREAMING_SNAKE_CASE ): a__ = preds_list[example_id] a__ = """""" for token in sentence: out += F'{token["form"]} ({token["upos"]}|{s_p.pop(0 )}) ' out += "\n" writer.write(__SCREAMING_SNAKE_CASE ) example_id += 1 def lowerCamelCase__( self :Optional[int] ,__snake_case :Union[str, Any] ) -> List[Any]: if path: with open(__SCREAMING_SNAKE_CASE ,'r' ) as f: return f.read().splitlines() else: return [ "ADJ", "ADP", "ADV", "AUX", "CCONJ", "DET", "INTJ", "NOUN", "NUM", "PART", "PRON", "PROPN", "PUNCT", "SCONJ", "SYM", "VERB", "X", ]
335
'''simple docstring''' from __future__ import annotations A_ : str = "Muhammad Umer Farooq" A_ : Optional[Any] = "MIT" A_ : int = "1.0.0" A_ : int = "Muhammad Umer Farooq" A_ : int = "[email protected]" A_ : Dict = "Alpha" import re from html.parser import HTMLParser from urllib import parse import requests class __snake_case ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE ): super().__init__() snake_case__ : list[str] = [] snake_case__ : List[Any] = domain def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): # Only parse the 'anchor' tag. if tag == "a": # Check the list of defined attributes. for name, value in attrs: # If href is defined, and not empty nor # print it. if name == "href" and value != "#" and value != "": # If not already in urls. if value not in self.urls: snake_case__ : str = parse.urljoin(self.domain , __SCREAMING_SNAKE_CASE ) self.urls.append(__SCREAMING_SNAKE_CASE ) def UpperCamelCase__ ( __magic_name__ : str ) -> str: '''simple docstring''' return ".".join(get_sub_domain_name(__magic_name__ ).split(""".""" )[-2:] ) def UpperCamelCase__ ( __magic_name__ : str ) -> str: '''simple docstring''' return parse.urlparse(__magic_name__ ).netloc def UpperCamelCase__ ( __magic_name__ : str = "https://github.com" ) -> list[str]: '''simple docstring''' snake_case__ : List[str] = get_domain_name(__magic_name__ ) # Initialize the parser snake_case__ : Optional[Any] = Parser(__magic_name__ ) try: # Open URL snake_case__ : Any = requests.get(__magic_name__ ) # pass the raw HTML to the parser to get links parser.feed(r.text ) # Get links and loop through snake_case__ : List[str] = set() for link in parser.urls: # open URL. # read = requests.get(link) try: snake_case__ : Tuple = requests.get(__magic_name__ ) # Get the valid email. snake_case__ : List[str] = re.findall("""[a-zA-Z0-9]+@""" + domain , read.text ) # If not in list then append it. for email in emails: valid_emails.add(__magic_name__ ) except ValueError: pass except ValueError: raise SystemExit(1 ) # Finally return a sorted list of email addresses with no duplicates. return sorted(__magic_name__ ) if __name__ == "__main__": A_ : str = emails_from_url("https://github.com") print(F'{len(emails)} emails found:') print("\n".join(sorted(emails)))
38
0
import inspect import tempfile from collections import OrderedDict, UserDict from collections.abc import MutableMapping from contextlib import ExitStack, contextmanager from dataclasses import fields from enum import Enum from typing import Any, ContextManager, List, Tuple import numpy as np from .import_utils import is_flax_available, is_tf_available, is_torch_available, is_torch_fx_proxy if is_flax_available(): import jax.numpy as jnp class lowercase ( __SCREAMING_SNAKE_CASE ): def __get__( self , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__=None ): """simple docstring""" if obj is None: return self if self.fget is None: raise AttributeError('''unreadable attribute''' ) lowerCAmelCase__ : List[Any] = """__cached_""" + self.fget.__name__ lowerCAmelCase__ : str = getattr(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) if cached is None: lowerCAmelCase__ : Optional[Any] = self.fget(__SCREAMING_SNAKE_CASE ) setattr(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) return cached def _a ( __UpperCamelCase : List[Any] ): lowerCAmelCase__ : List[Any] = val.lower() if val in {"y", "yes", "t", "true", "on", "1"}: return 1 if val in {"n", "no", "f", "false", "off", "0"}: return 0 raise ValueError(f'''invalid truth value {val!r}''' ) def _a ( __UpperCamelCase : Optional[int] ): if is_torch_fx_proxy(__UpperCamelCase ): return True if is_torch_available(): import torch if isinstance(__UpperCamelCase ,torch.Tensor ): return True if is_tf_available(): import tensorflow as tf if isinstance(__UpperCamelCase ,tf.Tensor ): return True if is_flax_available(): import jax.numpy as jnp from jax.core import Tracer if isinstance(__UpperCamelCase ,(jnp.ndarray, Tracer) ): return True return isinstance(__UpperCamelCase ,np.ndarray ) def _a ( __UpperCamelCase : List[str] ): return isinstance(__UpperCamelCase ,np.ndarray ) def _a ( __UpperCamelCase : Optional[int] ): return _is_numpy(__UpperCamelCase ) def _a ( __UpperCamelCase : Tuple ): import torch return isinstance(__UpperCamelCase ,torch.Tensor ) def _a ( __UpperCamelCase : List[str] ): return False if not is_torch_available() else _is_torch(__UpperCamelCase ) def _a ( __UpperCamelCase : Dict ): import torch return isinstance(__UpperCamelCase ,torch.device ) def _a ( __UpperCamelCase : str ): return False if not is_torch_available() else _is_torch_device(__UpperCamelCase ) def _a ( __UpperCamelCase : Optional[Any] ): import torch if isinstance(__UpperCamelCase ,__UpperCamelCase ): if hasattr(__UpperCamelCase ,__UpperCamelCase ): lowerCAmelCase__ : Optional[Any] = getattr(__UpperCamelCase ,__UpperCamelCase ) else: return False return isinstance(__UpperCamelCase ,torch.dtype ) def _a ( __UpperCamelCase : Union[str, Any] ): return False if not is_torch_available() else _is_torch_dtype(__UpperCamelCase ) def _a ( __UpperCamelCase : Union[str, Any] ): import tensorflow as tf return isinstance(__UpperCamelCase ,tf.Tensor ) def _a ( __UpperCamelCase : List[str] ): return False if not is_tf_available() else _is_tensorflow(__UpperCamelCase ) def _a ( __UpperCamelCase : Optional[Any] ): import tensorflow as tf # the `is_symbolic_tensor` predicate is only available starting with TF 2.14 if hasattr(__UpperCamelCase ,'''is_symbolic_tensor''' ): return tf.is_symbolic_tensor(__UpperCamelCase ) return type(__UpperCamelCase ) == tf.Tensor def _a ( __UpperCamelCase : List[str] ): return False if not is_tf_available() else _is_tf_symbolic_tensor(__UpperCamelCase ) def _a ( __UpperCamelCase : Union[str, Any] ): import jax.numpy as jnp # noqa: F811 return isinstance(__UpperCamelCase ,jnp.ndarray ) def _a ( __UpperCamelCase : List[str] ): return False if not is_flax_available() else _is_jax(__UpperCamelCase ) def _a ( __UpperCamelCase : str ): if isinstance(__UpperCamelCase ,(dict, UserDict) ): return {k: to_py_obj(__UpperCamelCase ) for k, v in obj.items()} elif isinstance(__UpperCamelCase ,(list, tuple) ): return [to_py_obj(__UpperCamelCase ) for o in obj] elif is_tf_tensor(__UpperCamelCase ): return obj.numpy().tolist() elif is_torch_tensor(__UpperCamelCase ): return obj.detach().cpu().tolist() elif is_jax_tensor(__UpperCamelCase ): return np.asarray(__UpperCamelCase ).tolist() elif isinstance(__UpperCamelCase ,(np.ndarray, np.number) ): # tolist also works on 0d np arrays return obj.tolist() else: return obj def _a ( __UpperCamelCase : List[Any] ): if isinstance(__UpperCamelCase ,(dict, UserDict) ): return {k: to_numpy(__UpperCamelCase ) for k, v in obj.items()} elif isinstance(__UpperCamelCase ,(list, tuple) ): return np.array(__UpperCamelCase ) elif is_tf_tensor(__UpperCamelCase ): return obj.numpy() elif is_torch_tensor(__UpperCamelCase ): return obj.detach().cpu().numpy() elif is_jax_tensor(__UpperCamelCase ): return np.asarray(__UpperCamelCase ) else: return obj class lowercase ( __SCREAMING_SNAKE_CASE ): def lowercase_ ( self ): """simple docstring""" lowerCAmelCase__ : List[Any] = fields(self ) # Safety and consistency checks if not len(__SCREAMING_SNAKE_CASE ): raise ValueError(f'''{self.__class__.__name__} has no fields.''' ) if not all(field.default is None for field in class_fields[1:] ): raise ValueError(f'''{self.__class__.__name__} should not have more than one required field.''' ) lowerCAmelCase__ : List[Any] = getattr(self , class_fields[0].name ) lowerCAmelCase__ : Tuple = all(getattr(self , field.name ) is None for field in class_fields[1:] ) if other_fields_are_none and not is_tensor(__SCREAMING_SNAKE_CASE ): if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): lowerCAmelCase__ : List[Any] = first_field.items() lowerCAmelCase__ : Dict = True else: try: lowerCAmelCase__ : Union[str, Any] = iter(__SCREAMING_SNAKE_CASE ) lowerCAmelCase__ : str = True except TypeError: lowerCAmelCase__ : int = False # if we provided an iterator as first field and the iterator is a (key, value) iterator # set the associated fields if first_field_iterator: for idx, element in enumerate(__SCREAMING_SNAKE_CASE ): if ( not isinstance(__SCREAMING_SNAKE_CASE , (list, tuple) ) or not len(__SCREAMING_SNAKE_CASE ) == 2 or not isinstance(element[0] , __SCREAMING_SNAKE_CASE ) ): if idx == 0: # If we do not have an iterator of key/values, set it as attribute lowerCAmelCase__ : Union[str, Any] = first_field else: # If we have a mixed iterator, raise an error raise ValueError( f'''Cannot set key/value for {element}. It needs to be a tuple (key, value).''' ) break setattr(self , element[0] , element[1] ) if element[1] is not None: lowerCAmelCase__ : List[str] = element[1] elif first_field is not None: lowerCAmelCase__ : int = first_field else: for field in class_fields: lowerCAmelCase__ : Union[str, Any] = getattr(self , field.name ) if v is not None: lowerCAmelCase__ : Any = v def __delitem__( self , *SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ): """simple docstring""" raise Exception(f'''You cannot use ``__delitem__`` on a {self.__class__.__name__} instance.''' ) def lowercase_ ( self , *SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ): """simple docstring""" raise Exception(f'''You cannot use ``setdefault`` on a {self.__class__.__name__} instance.''' ) def lowercase_ ( self , *SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ): """simple docstring""" raise Exception(f'''You cannot use ``pop`` on a {self.__class__.__name__} instance.''' ) def lowercase_ ( self , *SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ): """simple docstring""" raise Exception(f'''You cannot use ``update`` on a {self.__class__.__name__} instance.''' ) def __getitem__( self , SCREAMING_SNAKE_CASE__ ): """simple docstring""" if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): lowerCAmelCase__ : Optional[Any] = dict(self.items() ) return inner_dict[k] else: return self.to_tuple()[k] def __setattr__( self , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): """simple docstring""" if name in self.keys() and value is not None: # Don't call self.__setitem__ to avoid recursion errors super().__setitem__(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) super().__setattr__(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def __setitem__( self , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): """simple docstring""" super().__setitem__(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # Don't call self.__setattr__ to avoid recursion errors super().__setattr__(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def lowercase_ ( self ): """simple docstring""" return tuple(self[k] for k in self.keys() ) class lowercase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): @classmethod def lowercase_ ( cls , SCREAMING_SNAKE_CASE__ ): """simple docstring""" raise ValueError( f'''{value} is not a valid {cls.__name__}, please select one of {list(cls._valueamember_map_.keys() )}''' ) class lowercase ( __SCREAMING_SNAKE_CASE ): __a = """longest""" __a = """max_length""" __a = """do_not_pad""" class lowercase ( __SCREAMING_SNAKE_CASE ): __a = """pt""" __a = """tf""" __a = """np""" __a = """jax""" class lowercase : def __init__( self , SCREAMING_SNAKE_CASE__ ): """simple docstring""" lowerCAmelCase__ : Union[str, Any] = context_managers lowerCAmelCase__ : str = ExitStack() def __enter__( self ): """simple docstring""" for context_manager in self.context_managers: self.stack.enter_context(__SCREAMING_SNAKE_CASE ) def __exit__( self , *SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ): """simple docstring""" self.stack.__exit__(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def _a ( __UpperCamelCase : Optional[int] ): lowerCAmelCase__ : Dict = infer_framework(__UpperCamelCase ) if framework == "tf": lowerCAmelCase__ : Union[str, Any] = inspect.signature(model_class.call ) # TensorFlow models elif framework == "pt": lowerCAmelCase__ : int = inspect.signature(model_class.forward ) # PyTorch models else: lowerCAmelCase__ : Optional[int] = inspect.signature(model_class.__call__ ) # Flax models for p in signature.parameters: if p == "return_loss" and signature.parameters[p].default is True: return True return False def _a ( __UpperCamelCase : Any ): lowerCAmelCase__ : List[Any] = model_class.__name__ lowerCAmelCase__ : Union[str, Any] = infer_framework(__UpperCamelCase ) if framework == "tf": lowerCAmelCase__ : int = inspect.signature(model_class.call ) # TensorFlow models elif framework == "pt": lowerCAmelCase__ : Dict = inspect.signature(model_class.forward ) # PyTorch models else: lowerCAmelCase__ : List[str] = inspect.signature(model_class.__call__ ) # Flax models if "QuestionAnswering" in model_name: return [p for p in signature.parameters if "label" in p or p in ("start_positions", "end_positions")] else: return [p for p in signature.parameters if "label" in p] def _a ( __UpperCamelCase : MutableMapping ,__UpperCamelCase : str = "" ,__UpperCamelCase : str = "." ): def _flatten_dict(__UpperCamelCase : str ,__UpperCamelCase : int="" ,__UpperCamelCase : Optional[int]="." ): for k, v in d.items(): lowerCAmelCase__ : Tuple = str(__UpperCamelCase ) + delimiter + str(__UpperCamelCase ) if parent_key else k if v and isinstance(__UpperCamelCase ,__UpperCamelCase ): yield from flatten_dict(__UpperCamelCase ,__UpperCamelCase ,delimiter=__UpperCamelCase ).items() else: yield key, v return dict(_flatten_dict(__UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase ) ) @contextmanager def _a ( __UpperCamelCase : Union[str, Any] ,__UpperCamelCase : bool = False ): if use_temp_dir: with tempfile.TemporaryDirectory() as tmp_dir: yield tmp_dir else: yield working_dir def _a ( __UpperCamelCase : Tuple ,__UpperCamelCase : Any=None ): if is_numpy_array(__UpperCamelCase ): return np.transpose(__UpperCamelCase ,axes=__UpperCamelCase ) elif is_torch_tensor(__UpperCamelCase ): return array.T if axes is None else array.permute(*__UpperCamelCase ) elif is_tf_tensor(__UpperCamelCase ): import tensorflow as tf return tf.transpose(__UpperCamelCase ,perm=__UpperCamelCase ) elif is_jax_tensor(__UpperCamelCase ): return jnp.transpose(__UpperCamelCase ,axes=__UpperCamelCase ) else: raise ValueError(f'''Type not supported for transpose: {type(__UpperCamelCase )}.''' ) def _a ( __UpperCamelCase : int ,__UpperCamelCase : Optional[Any] ): if is_numpy_array(__UpperCamelCase ): return np.reshape(__UpperCamelCase ,__UpperCamelCase ) elif is_torch_tensor(__UpperCamelCase ): return array.reshape(*__UpperCamelCase ) elif is_tf_tensor(__UpperCamelCase ): import tensorflow as tf return tf.reshape(__UpperCamelCase ,__UpperCamelCase ) elif is_jax_tensor(__UpperCamelCase ): return jnp.reshape(__UpperCamelCase ,__UpperCamelCase ) else: raise ValueError(f'''Type not supported for reshape: {type(__UpperCamelCase )}.''' ) def _a ( __UpperCamelCase : List[Any] ,__UpperCamelCase : Optional[Any]=None ): if is_numpy_array(__UpperCamelCase ): return np.squeeze(__UpperCamelCase ,axis=__UpperCamelCase ) elif is_torch_tensor(__UpperCamelCase ): return array.squeeze() if axis is None else array.squeeze(dim=__UpperCamelCase ) elif is_tf_tensor(__UpperCamelCase ): import tensorflow as tf return tf.squeeze(__UpperCamelCase ,axis=__UpperCamelCase ) elif is_jax_tensor(__UpperCamelCase ): return jnp.squeeze(__UpperCamelCase ,axis=__UpperCamelCase ) else: raise ValueError(f'''Type not supported for squeeze: {type(__UpperCamelCase )}.''' ) def _a ( __UpperCamelCase : str ,__UpperCamelCase : int ): if is_numpy_array(__UpperCamelCase ): return np.expand_dims(__UpperCamelCase ,__UpperCamelCase ) elif is_torch_tensor(__UpperCamelCase ): return array.unsqueeze(dim=__UpperCamelCase ) elif is_tf_tensor(__UpperCamelCase ): import tensorflow as tf return tf.expand_dims(__UpperCamelCase ,axis=__UpperCamelCase ) elif is_jax_tensor(__UpperCamelCase ): return jnp.expand_dims(__UpperCamelCase ,axis=__UpperCamelCase ) else: raise ValueError(f'''Type not supported for expand_dims: {type(__UpperCamelCase )}.''' ) def _a ( __UpperCamelCase : List[str] ): if is_numpy_array(__UpperCamelCase ): return np.size(__UpperCamelCase ) elif is_torch_tensor(__UpperCamelCase ): return array.numel() elif is_tf_tensor(__UpperCamelCase ): import tensorflow as tf return tf.size(__UpperCamelCase ) elif is_jax_tensor(__UpperCamelCase ): return array.size else: raise ValueError(f'''Type not supported for expand_dims: {type(__UpperCamelCase )}.''' ) def _a ( __UpperCamelCase : Dict ,__UpperCamelCase : Optional[Any] ): for key, value in auto_map.items(): if isinstance(__UpperCamelCase ,(tuple, list) ): lowerCAmelCase__ : Optional[int] = [f'''{repo_id}--{v}''' if (v is not None and """--""" not in v) else v for v in value] elif value is not None and "--" not in value: lowerCAmelCase__ : Optional[int] = f'''{repo_id}--{value}''' return auto_map def _a ( __UpperCamelCase : str ): for base_class in inspect.getmro(__UpperCamelCase ): lowerCAmelCase__ : List[Any] = base_class.__module__ lowerCAmelCase__ : Tuple = base_class.__name__ if module.startswith('''tensorflow''' ) or module.startswith('''keras''' ) or name == "TFPreTrainedModel": return "tf" elif module.startswith('''torch''' ) or name == "PreTrainedModel": return "pt" elif module.startswith('''flax''' ) or module.startswith('''jax''' ) or name == "FlaxPreTrainedModel": return "flax" else: raise TypeError(f'''Could not infer framework from class {model_class}.''' )
233
'''simple docstring''' def UpperCamelCase__ ( __magic_name__ : List[Any] ) -> Tuple: '''simple docstring''' if not head: return True # split the list to two parts snake_case__ , snake_case__ : Dict = head.next, head while fast and fast.next: snake_case__ : Any = fast.next.next snake_case__ : int = slow.next snake_case__ : Dict = slow.next snake_case__ : List[str] = None # Don't forget here! But forget still works! # reverse the second part snake_case__ : Tuple = None while second: snake_case__ : Tuple = second.next snake_case__ : Any = node snake_case__ : str = second snake_case__ : Optional[Any] = nxt # compare two parts # second part has the same or one less node while node: if node.val != head.val: return False snake_case__ : List[Any] = node.next snake_case__ : int = head.next return True def UpperCamelCase__ ( __magic_name__ : Any ) -> Optional[Any]: '''simple docstring''' if not head or not head.next: return True # 1. Get the midpoint (slow) snake_case__ : List[Any] = head while fast and fast.next: snake_case__ , snake_case__ : Any = fast.next.next, slow.next # 2. Push the second half into the stack snake_case__ : Tuple = [slow.val] while slow.next: snake_case__ : Optional[Any] = slow.next stack.append(slow.val ) # 3. Comparison while stack: if stack.pop() != cur.val: return False snake_case__ : str = cur.next return True def UpperCamelCase__ ( __magic_name__ : Optional[Any] ) -> Tuple: '''simple docstring''' if not head or not head.next: return True snake_case__ : int = {} snake_case__ : Union[str, Any] = 0 while head: if head.val in d: d[head.val].append(__magic_name__ ) else: snake_case__ : Tuple = [pos] snake_case__ : Optional[Any] = head.next pos += 1 snake_case__ : int = pos - 1 snake_case__ : str = 0 for v in d.values(): if len(__magic_name__ ) % 2 != 0: middle += 1 else: snake_case__ : List[str] = 0 for i in range(0 , len(__magic_name__ ) ): if v[i] + v[len(__magic_name__ ) - 1 - step] != checksum: return False step += 1 if middle > 1: return False return True
38
0
import itertools import json import os import unittest from transformers import AddedToken, LongformerTokenizer, LongformerTokenizerFast from transformers.models.longformer.tokenization_longformer import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class _UpperCamelCase( __SCREAMING_SNAKE_CASE , unittest.TestCase ): __SCREAMING_SNAKE_CASE : int = LongformerTokenizer __SCREAMING_SNAKE_CASE : Union[str, Any] = True __SCREAMING_SNAKE_CASE : List[str] = LongformerTokenizerFast __SCREAMING_SNAKE_CASE : Dict = True def __lowerCAmelCase ( self : str ): '''simple docstring''' super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt __a : Union[str, Any] = [ """l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """\u0120""", """\u0120l""", """\u0120n""", """\u0120lo""", """\u0120low""", """er""", """\u0120lowest""", """\u0120newer""", """\u0120wider""", """<unk>""", ] __a : Optional[int] = dict(zip(__SCREAMING_SNAKE_CASE , range(len(__SCREAMING_SNAKE_CASE ) ) ) ) __a : int = ["""#version: 0.2""", """\u0120 l""", """\u0120l o""", """\u0120lo w""", """e r""", """"""] __a : Any = {"""unk_token""": """<unk>"""} __a : List[Any] = 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(__SCREAMING_SNAKE_CASE ) + '\n' ) with open(self.merges_file , 'w' , encoding='utf-8' ) as fp: fp.write('\n'.join(__SCREAMING_SNAKE_CASE ) ) def __lowerCAmelCase ( self : Union[str, Any] , **SCREAMING_SNAKE_CASE__ : Optional[Any] ): '''simple docstring''' kwargs.update(self.special_tokens_map ) return self.tokenizer_class.from_pretrained(self.tmpdirname , **__SCREAMING_SNAKE_CASE ) def __lowerCAmelCase ( self : Optional[int] , **SCREAMING_SNAKE_CASE__ : Optional[int] ): '''simple docstring''' kwargs.update(self.special_tokens_map ) return self.rust_tokenizer_class.from_pretrained(self.tmpdirname , **__SCREAMING_SNAKE_CASE ) def __lowerCAmelCase ( self : Optional[Any] , SCREAMING_SNAKE_CASE__ : Dict ): '''simple docstring''' __a : str = """lower newer""" __a : Dict = """lower newer""" return input_text, output_text def __lowerCAmelCase ( self : Union[str, Any] ): '''simple docstring''' __a : int = self.tokenizer_class(self.vocab_file , self.merges_file , **self.special_tokens_map ) __a : Tuple = """lower newer""" __a : Optional[Any] = ["""l""", """o""", """w""", """er""", """\u0120""", """n""", """e""", """w""", """er"""] __a : Tuple = tokenizer.tokenize(__SCREAMING_SNAKE_CASE ) # , add_prefix_space=True) self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) __a : Tuple = tokens + [tokenizer.unk_token] __a : List[Any] = [0, 1, 2, 1_5, 1_0, 9, 3, 2, 1_5, 1_9] self.assertListEqual(tokenizer.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) def __lowerCAmelCase ( self : List[Any] ): '''simple docstring''' __a : Tuple = self.get_tokenizer() self.assertListEqual(tokenizer.encode('Hello world!' , add_special_tokens=__SCREAMING_SNAKE_CASE ) , [0, 3_1_4_1_4, 2_3_2, 3_2_8, 2] ) self.assertListEqual( tokenizer.encode('Hello world! cécé herlolip 418' , add_special_tokens=__SCREAMING_SNAKE_CASE ) , [0, 3_1_4_1_4, 2_3_2, 3_2_8, 7_4_0, 1_1_4_0, 1_2_6_9_5, 6_9, 4_6_0_7_8, 1_5_8_8, 2] , ) @slow def __lowerCAmelCase ( self : List[Any] ): '''simple docstring''' __a : List[Any] = self.tokenizer_class.from_pretrained('allenai/longformer-base-4096' ) __a : int = tokenizer.encode('sequence builders' , add_special_tokens=__SCREAMING_SNAKE_CASE ) __a : Dict = tokenizer.encode('multi-sequence build' , add_special_tokens=__SCREAMING_SNAKE_CASE ) __a : Optional[Any] = tokenizer.encode( 'sequence builders' , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) __a : Dict = tokenizer.encode( 'sequence builders' , 'multi-sequence build' , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) __a : Dict = tokenizer.build_inputs_with_special_tokens(__SCREAMING_SNAKE_CASE ) __a : Optional[int] = tokenizer.build_inputs_with_special_tokens(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) assert encoded_sentence == encoded_text_from_decode assert encoded_pair == encoded_pair_from_decode def __lowerCAmelCase ( self : Tuple ): '''simple docstring''' __a : Optional[int] = self.get_tokenizer() __a : int = """Encode this sequence.""" __a : Union[str, Any] = tokenizer.byte_encoder[""" """.encode('utf-8' )[0]] # Testing encoder arguments __a : Optional[int] = tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) __a : Tuple = tokenizer.convert_ids_to_tokens(encoded[0] )[0] self.assertNotEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) __a : Optional[Any] = tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) __a : List[str] = tokenizer.convert_ids_to_tokens(encoded[0] )[0] self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) tokenizer.add_special_tokens({'bos_token': '<s>'} ) __a : List[str] = tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) __a : str = tokenizer.convert_ids_to_tokens(encoded[1] )[0] self.assertNotEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # Testing spaces after special tokens __a : List[str] = """<mask>""" tokenizer.add_special_tokens( {'mask_token': AddedToken(__SCREAMING_SNAKE_CASE , lstrip=__SCREAMING_SNAKE_CASE , rstrip=__SCREAMING_SNAKE_CASE )} ) # mask token has a left space __a : Dict = tokenizer.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) __a : str = """Encode <mask> sequence""" __a : Tuple = """Encode <mask>sequence""" __a : Union[str, Any] = tokenizer.encode(__SCREAMING_SNAKE_CASE ) __a : List[str] = encoded.index(__SCREAMING_SNAKE_CASE ) __a : Optional[int] = tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1] )[0] self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) __a : Tuple = tokenizer.encode(__SCREAMING_SNAKE_CASE ) __a : str = encoded.index(__SCREAMING_SNAKE_CASE ) __a : List[Any] = tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1] )[0] self.assertNotEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def __lowerCAmelCase ( self : Dict ): '''simple docstring''' pass def __lowerCAmelCase ( self : Dict ): '''simple docstring''' for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): __a : List[Any] = self.rust_tokenizer_class.from_pretrained(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) __a : Any = self.tokenizer_class.from_pretrained(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) __a : List[str] = """A, <mask> AllenNLP sentence.""" __a : str = tokenizer_r.encode_plus(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , return_token_type_ids=__SCREAMING_SNAKE_CASE ) __a : Tuple = tokenizer_p.encode_plus(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , return_token_type_ids=__SCREAMING_SNAKE_CASE ) # token_type_ids should put 0 everywhere self.assertEqual(sum(tokens_r['token_type_ids'] ) , sum(tokens_p['token_type_ids'] ) ) # attention_mask should put 1 everywhere, so sum over length should be 1 self.assertEqual( sum(tokens_r['attention_mask'] ) / len(tokens_r['attention_mask'] ) , sum(tokens_p['attention_mask'] ) / len(tokens_p['attention_mask'] ) , ) __a : Union[str, Any] = tokenizer_r.convert_ids_to_tokens(tokens_r['input_ids'] ) __a : Dict = tokenizer_p.convert_ids_to_tokens(tokens_p['input_ids'] ) # Rust correctly handles the space before the mask while python doesnt self.assertSequenceEqual(tokens_p['input_ids'] , [0, 2_5_0, 6, 5_0_2_6_4, 3_8_2_3, 4_8_7, 2_1_9_9_2, 3_6_4_5, 4, 2] ) self.assertSequenceEqual(tokens_r['input_ids'] , [0, 2_5_0, 6, 5_0_2_6_4, 3_8_2_3, 4_8_7, 2_1_9_9_2, 3_6_4_5, 4, 2] ) self.assertSequenceEqual( __SCREAMING_SNAKE_CASE , ['<s>', 'A', ',', '<mask>', 'ĠAllen', 'N', 'LP', 'Ġsentence', '.', '</s>'] ) self.assertSequenceEqual( __SCREAMING_SNAKE_CASE , ['<s>', 'A', ',', '<mask>', 'ĠAllen', 'N', 'LP', 'Ġsentence', '.', '</s>'] ) def __lowerCAmelCase ( self : List[Any] ): '''simple docstring''' for trim_offsets, add_prefix_space in itertools.product([True, False] , repeat=2 ): __a : Any = self.rust_tokenizer_class.from_pretrained( self.tmpdirname , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) __a : Dict = json.loads(tokenizer_r.backend_tokenizer.pre_tokenizer.__getstate__() ) __a : List[str] = json.loads(tokenizer_r.backend_tokenizer.post_processor.__getstate__() ) self.assertEqual(pre_tokenizer_state['add_prefix_space'] , __SCREAMING_SNAKE_CASE ) self.assertEqual(post_processor_state['add_prefix_space'] , __SCREAMING_SNAKE_CASE ) self.assertEqual(post_processor_state['trim_offsets'] , __SCREAMING_SNAKE_CASE ) def __lowerCAmelCase ( self : int ): '''simple docstring''' for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): __a : Union[str, Any] = """hello""" # `hello` is a token in the vocabulary of `pretrained_name` __a : Any = f'''{text_of_1_token} {text_of_1_token}''' __a : Union[str, Any] = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) __a : Union[str, Any] = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (len(__SCREAMING_SNAKE_CASE ) + 1, len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) __a : List[Any] = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) __a : str = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (len(__SCREAMING_SNAKE_CASE ) + 1, len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) __a : Optional[Any] = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) __a : str = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (len(__SCREAMING_SNAKE_CASE ), len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) __a : Tuple = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) __a : List[Any] = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (len(__SCREAMING_SNAKE_CASE ), len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) __a : Optional[Any] = f''' {text}''' # tokenizer_r = self.rust_tokenizer_class.from_pretrained( # pretrained_name, use_fast=True, add_prefix_space=True, trim_offsets=True # ) # encoding = tokenizer_r(text, return_offsets_mapping=True, add_special_tokens=False) # self.assertEqual(encoding.offset_mapping[0], (1, 1 + len(text_of_1_token))) # self.assertEqual( # encoding.offset_mapping[1], # (1 + len(text_of_1_token) + 1, 1 + len(text_of_1_token) + 1 + len(text_of_1_token)), # ) __a : Dict = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) __a : Optional[Any] = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (1, 1 + len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(__SCREAMING_SNAKE_CASE ) + 1, 1 + len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) __a : Any = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) __a : Any = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, 1 + len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(__SCREAMING_SNAKE_CASE ), 1 + len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) __a : List[Any] = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) __a : List[Any] = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, 1 + len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(__SCREAMING_SNAKE_CASE ), 1 + len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , )
47
'''simple docstring''' import shutil import tempfile import unittest from transformers import SPIECE_UNDERLINE, BatchEncoding, MBartTokenizer, MBartTokenizerFast, is_torch_available from transformers.testing_utils import ( get_tests_dir, nested_simplify, require_sentencepiece, require_tokenizers, require_torch, ) from ...test_tokenization_common import TokenizerTesterMixin A_ : Union[str, Any] = get_tests_dir("fixtures/test_sentencepiece.model") if is_torch_available(): from transformers.models.mbart.modeling_mbart import shift_tokens_right A_ : str = 250004 A_ : str = 250020 @require_sentencepiece @require_tokenizers class __snake_case ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowerCamelCase__ = MBartTokenizer lowerCamelCase__ = MBartTokenizerFast lowerCamelCase__ = True lowerCamelCase__ = True def __UpperCamelCase ( self ): super().setUp() # We have a SentencePiece fixture for testing snake_case__ : Tuple = MBartTokenizer(__SCREAMING_SNAKE_CASE , keep_accents=__SCREAMING_SNAKE_CASE ) tokenizer.save_pretrained(self.tmpdirname ) def __UpperCamelCase ( self ): snake_case__ : Tuple = MBartTokenizer(__SCREAMING_SNAKE_CASE , keep_accents=__SCREAMING_SNAKE_CASE ) snake_case__ : int = tokenizer.tokenize("""This is a test""" ) self.assertListEqual(__SCREAMING_SNAKE_CASE , ["""▁This""", """▁is""", """▁a""", """▁t""", """est"""] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) , [value + tokenizer.fairseq_offset for value in [2_8_5, 4_6, 1_0, 1_7_0, 3_8_2]] , ) snake_case__ : Optional[int] = tokenizer.tokenize("""I was born in 92000, and this is falsé.""" ) self.assertListEqual( __SCREAMING_SNAKE_CASE , [ SPIECE_UNDERLINE + """I""", SPIECE_UNDERLINE + """was""", SPIECE_UNDERLINE + """b""", """or""", """n""", SPIECE_UNDERLINE + """in""", SPIECE_UNDERLINE + """""", """9""", """2""", """0""", """0""", """0""", """,""", SPIECE_UNDERLINE + """and""", SPIECE_UNDERLINE + """this""", SPIECE_UNDERLINE + """is""", SPIECE_UNDERLINE + """f""", """al""", """s""", """é""", """.""", ] , ) snake_case__ : Optional[int] = tokenizer.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) self.assertListEqual( __SCREAMING_SNAKE_CASE , [ value + tokenizer.fairseq_offset for value in [8, 2_1, 8_4, 5_5, 2_4, 1_9, 7, 2, 6_0_2, 3_4_7, 3_4_7, 3_4_7, 3, 1_2, 6_6, 4_6, 7_2, 8_0, 6, 2, 4] # ^ unk: 2 + 1 = 3 unk: 2 + 1 = 3 ^ ] , ) snake_case__ : Union[str, Any] = tokenizer.convert_ids_to_tokens(__SCREAMING_SNAKE_CASE ) self.assertListEqual( __SCREAMING_SNAKE_CASE , [ SPIECE_UNDERLINE + """I""", SPIECE_UNDERLINE + """was""", SPIECE_UNDERLINE + """b""", """or""", """n""", SPIECE_UNDERLINE + """in""", SPIECE_UNDERLINE + """""", """<unk>""", """2""", """0""", """0""", """0""", """,""", SPIECE_UNDERLINE + """and""", SPIECE_UNDERLINE + """this""", SPIECE_UNDERLINE + """is""", SPIECE_UNDERLINE + """f""", """al""", """s""", """<unk>""", """.""", ] , ) def __UpperCamelCase ( self ): if not self.test_slow_tokenizer: # as we don't have a slow version, we can't compare the outputs between slow and fast versions return snake_case__ : Optional[int] = (self.rust_tokenizer_class, """hf-internal-testing/tiny-random-mbart""", {}) for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f"{tokenizer.__class__.__name__} ({pretrained_name})" ): snake_case__ : Optional[Any] = self.rust_tokenizer_class.from_pretrained(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) snake_case__ : Dict = self.tokenizer_class.from_pretrained(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) snake_case__ : List[str] = tempfile.mkdtemp() snake_case__ : int = tokenizer_r.save_pretrained(__SCREAMING_SNAKE_CASE ) snake_case__ : Dict = tokenizer_p.save_pretrained(__SCREAMING_SNAKE_CASE ) # Checks it save with the same files + the tokenizer.json file for the fast one self.assertTrue(any("""tokenizer.json""" in f for f in tokenizer_r_files ) ) snake_case__ : List[str] = tuple(f for f in tokenizer_r_files if """tokenizer.json""" not in f ) self.assertSequenceEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # Checks everything loads correctly in the same way snake_case__ : Tuple = tokenizer_r.from_pretrained(__SCREAMING_SNAKE_CASE ) snake_case__ : Union[str, Any] = tokenizer_p.from_pretrained(__SCREAMING_SNAKE_CASE ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ) # self.assertEqual(getattr(tokenizer_rp, key), getattr(tokenizer_pp, key)) # self.assertEqual(getattr(tokenizer_rp, key + "_id"), getattr(tokenizer_pp, key + "_id")) shutil.rmtree(__SCREAMING_SNAKE_CASE ) # Save tokenizer rust, legacy_format=True snake_case__ : Any = tempfile.mkdtemp() snake_case__ : Optional[int] = tokenizer_r.save_pretrained(__SCREAMING_SNAKE_CASE , legacy_format=__SCREAMING_SNAKE_CASE ) snake_case__ : int = tokenizer_p.save_pretrained(__SCREAMING_SNAKE_CASE ) # Checks it save with the same files self.assertSequenceEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # Checks everything loads correctly in the same way snake_case__ : List[Any] = tokenizer_r.from_pretrained(__SCREAMING_SNAKE_CASE ) snake_case__ : Dict = tokenizer_p.from_pretrained(__SCREAMING_SNAKE_CASE ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ) shutil.rmtree(__SCREAMING_SNAKE_CASE ) # Save tokenizer rust, legacy_format=False snake_case__ : Dict = tempfile.mkdtemp() snake_case__ : Union[str, Any] = tokenizer_r.save_pretrained(__SCREAMING_SNAKE_CASE , legacy_format=__SCREAMING_SNAKE_CASE ) snake_case__ : Optional[int] = tokenizer_p.save_pretrained(__SCREAMING_SNAKE_CASE ) # Checks it saved the tokenizer.json file self.assertTrue(any("""tokenizer.json""" in f for f in tokenizer_r_files ) ) # Checks everything loads correctly in the same way snake_case__ : Dict = tokenizer_r.from_pretrained(__SCREAMING_SNAKE_CASE ) snake_case__ : Any = tokenizer_p.from_pretrained(__SCREAMING_SNAKE_CASE ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ) shutil.rmtree(__SCREAMING_SNAKE_CASE ) @require_torch @require_sentencepiece @require_tokenizers class __snake_case ( unittest.TestCase ): '''simple docstring''' lowerCamelCase__ = '''facebook/mbart-large-en-ro''' lowerCamelCase__ = [ ''' UN Chief Says There Is No Military Solution in Syria''', ''' Secretary-General Ban Ki-moon says his response to Russia\'s stepped up military support for Syria is that "there is no military solution" to the nearly five-year conflict and more weapons will only worsen the violence and misery for millions of people.''', ] lowerCamelCase__ = [ '''Şeful ONU declară că nu există o soluţie militară în Siria''', '''Secretarul General Ban Ki-moon declară că răspunsul său la intensificarea sprijinului militar al Rusiei''' ''' pentru Siria este că "nu există o soluţie militară" la conflictul de aproape cinci ani şi că noi arme nu vor''' ''' face decât să înrăutăţească violenţele şi mizeria pentru milioane de oameni.''', ] lowerCamelCase__ = [8_274, 127_873, 25_916, 7, 8_622, 2_071, 438, 67_485, 53, 187_895, 23, 51_712, 2, EN_CODE] @classmethod def __UpperCamelCase ( cls ): snake_case__ : MBartTokenizer = MBartTokenizer.from_pretrained( cls.checkpoint_name , src_lang="""en_XX""" , tgt_lang="""ro_RO""" ) snake_case__ : Any = 1 return cls def __UpperCamelCase ( self ): self.assertEqual(self.tokenizer.fairseq_tokens_to_ids["""ar_AR"""] , 2_5_0_0_0_1 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids["""en_EN"""] , 2_5_0_0_0_4 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids["""ro_RO"""] , 2_5_0_0_2_0 ) def __UpperCamelCase ( self ): snake_case__ : Tuple = self.tokenizer.batch_encode_plus(self.src_text ).input_ids[0] self.assertListEqual(self.expected_src_tokens , __SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self ): self.assertIn(__SCREAMING_SNAKE_CASE , self.tokenizer.all_special_ids ) snake_case__ : List[str] = [RO_CODE, 8_8_4, 9_0_1_9, 9_6, 9, 9_1_6, 8_6_7_9_2, 3_6, 1_8_7_4_3, 1_5_5_9_6, 5, 2] snake_case__ : List[Any] = self.tokenizer.decode(__SCREAMING_SNAKE_CASE , skip_special_tokens=__SCREAMING_SNAKE_CASE ) snake_case__ : Dict = self.tokenizer.decode(generated_ids[1:] , skip_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) self.assertNotIn(self.tokenizer.eos_token , __SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self ): snake_case__ : Dict = ["""this is gunna be a long sentence """ * 2_0] assert isinstance(src_text[0] , __SCREAMING_SNAKE_CASE ) snake_case__ : Tuple = 1_0 snake_case__ : int = self.tokenizer(__SCREAMING_SNAKE_CASE , max_length=__SCREAMING_SNAKE_CASE , truncation=__SCREAMING_SNAKE_CASE ).input_ids[0] self.assertEqual(ids[-2] , 2 ) self.assertEqual(ids[-1] , __SCREAMING_SNAKE_CASE ) self.assertEqual(len(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self ): self.assertListEqual(self.tokenizer.convert_tokens_to_ids(["""<mask>""", """ar_AR"""] ) , [2_5_0_0_2_6, 2_5_0_0_0_1] ) def __UpperCamelCase ( self ): snake_case__ : Union[str, Any] = tempfile.mkdtemp() snake_case__ : Dict = self.tokenizer.fairseq_tokens_to_ids self.tokenizer.save_pretrained(__SCREAMING_SNAKE_CASE ) snake_case__ : Any = MBartTokenizer.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertDictEqual(new_tok.fairseq_tokens_to_ids , __SCREAMING_SNAKE_CASE ) @require_torch def __UpperCamelCase ( self ): snake_case__ : Tuple = self.tokenizer(self.src_text , text_target=self.tgt_text , padding=__SCREAMING_SNAKE_CASE , return_tensors="""pt""" ) snake_case__ : int = shift_tokens_right(batch["""labels"""] , self.tokenizer.pad_token_id ) # fairseq batch: https://gist.github.com/sshleifer/cba08bc2109361a74ac3760a7e30e4f4 assert batch.input_ids[1][-2:].tolist() == [2, EN_CODE] assert batch.decoder_input_ids[1][0].tolist() == RO_CODE assert batch.decoder_input_ids[1][-1] == 2 assert batch.labels[1][-2:].tolist() == [2, RO_CODE] @require_torch def __UpperCamelCase ( self ): snake_case__ : Optional[int] = self.tokenizer( self.src_text , text_target=self.tgt_text , padding=__SCREAMING_SNAKE_CASE , truncation=__SCREAMING_SNAKE_CASE , max_length=len(self.expected_src_tokens ) , return_tensors="""pt""" , ) snake_case__ : List[str] = shift_tokens_right(batch["""labels"""] , self.tokenizer.pad_token_id ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) self.assertEqual((2, 1_4) , batch.input_ids.shape ) self.assertEqual((2, 1_4) , batch.attention_mask.shape ) snake_case__ : Tuple = batch.input_ids.tolist()[0] self.assertListEqual(self.expected_src_tokens , __SCREAMING_SNAKE_CASE ) self.assertEqual(2 , batch.decoder_input_ids[0, -1] ) # EOS # Test that special tokens are reset self.assertEqual(self.tokenizer.prefix_tokens , [] ) self.assertEqual(self.tokenizer.suffix_tokens , [self.tokenizer.eos_token_id, EN_CODE] ) def __UpperCamelCase ( self ): snake_case__ : Optional[int] = self.tokenizer(self.src_text , padding=__SCREAMING_SNAKE_CASE , truncation=__SCREAMING_SNAKE_CASE , max_length=3 , return_tensors="""pt""" ) snake_case__ : Optional[int] = self.tokenizer( text_target=self.tgt_text , padding=__SCREAMING_SNAKE_CASE , truncation=__SCREAMING_SNAKE_CASE , max_length=1_0 , return_tensors="""pt""" ) snake_case__ : str = targets["""input_ids"""] snake_case__ : Optional[Any] = shift_tokens_right(__SCREAMING_SNAKE_CASE , self.tokenizer.pad_token_id ) self.assertEqual(batch.input_ids.shape[1] , 3 ) self.assertEqual(batch.decoder_input_ids.shape[1] , 1_0 ) @require_torch def __UpperCamelCase ( self ): snake_case__ : Tuple = self.tokenizer._build_translation_inputs( """A test""" , return_tensors="""pt""" , src_lang="""en_XX""" , tgt_lang="""ar_AR""" ) self.assertEqual( nested_simplify(__SCREAMING_SNAKE_CASE ) , { # A, test, EOS, en_XX """input_ids""": [[6_2, 3_0_3_4, 2, 2_5_0_0_0_4]], """attention_mask""": [[1, 1, 1, 1]], # ar_AR """forced_bos_token_id""": 2_5_0_0_0_1, } , )
38
0
'''simple docstring''' from abc import ABC, abstractmethod from typing import Optional, Union from .. import Dataset, DatasetDict, Features, IterableDataset, IterableDatasetDict, NamedSplit from ..utils.typing import NestedDataStructureLike, PathLike class _lowercase ( __SCREAMING_SNAKE_CASE ): def __init__( self : Any , SCREAMING_SNAKE_CASE_ : int = None , SCREAMING_SNAKE_CASE_ : List[str] = None , SCREAMING_SNAKE_CASE_ : int = None , SCREAMING_SNAKE_CASE_ : Tuple = None , SCREAMING_SNAKE_CASE_ : Optional[Any] = False , SCREAMING_SNAKE_CASE_ : Tuple = False , SCREAMING_SNAKE_CASE_ : Dict = None , **SCREAMING_SNAKE_CASE_ : str , ) -> int: __snake_case = path_or_paths __snake_case = split if split or isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else """train""" __snake_case = features __snake_case = cache_dir __snake_case = keep_in_memory __snake_case = streaming __snake_case = num_proc __snake_case = kwargs @abstractmethod def a ( self : Any ) -> List[Any]: pass class _lowercase ( __SCREAMING_SNAKE_CASE ): def __init__( self : Dict , SCREAMING_SNAKE_CASE_ : Union[str, Any] = None , SCREAMING_SNAKE_CASE_ : int = None , SCREAMING_SNAKE_CASE_ : int = False , SCREAMING_SNAKE_CASE_ : str = False , SCREAMING_SNAKE_CASE_ : int = None , **SCREAMING_SNAKE_CASE_ : Dict , ) -> Optional[int]: __snake_case = features __snake_case = cache_dir __snake_case = keep_in_memory __snake_case = streaming __snake_case = num_proc __snake_case = kwargs @abstractmethod def a ( self : Union[str, Any] ) -> str: pass
56
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices A_ : int = logging.get_logger(__name__) A_ : Dict = { "google/bit-50": "https://huggingface.co/google/bit-50/resolve/main/config.json", } class __snake_case ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): '''simple docstring''' lowerCamelCase__ = '''bit''' lowerCamelCase__ = ['''preactivation''', '''bottleneck'''] lowerCamelCase__ = ['''SAME''', '''VALID'''] def __init__( self , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=6_4 , __SCREAMING_SNAKE_CASE=[2_5_6, 5_1_2, 1_0_2_4, 2_0_4_8] , __SCREAMING_SNAKE_CASE=[3, 4, 6, 3] , __SCREAMING_SNAKE_CASE="preactivation" , __SCREAMING_SNAKE_CASE="relu" , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=3_2 , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=3_2 , __SCREAMING_SNAKE_CASE=1 , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE , ): super().__init__(**__SCREAMING_SNAKE_CASE ) if layer_type not in self.layer_types: raise ValueError(f"layer_type={layer_type} is not one of {','.join(self.layer_types )}" ) if global_padding is not None: if global_padding.upper() in self.supported_padding: snake_case__ : Tuple = global_padding.upper() else: raise ValueError(f"Padding strategy {global_padding} not supported" ) snake_case__ : List[str] = num_channels snake_case__ : Tuple = embedding_size snake_case__ : str = hidden_sizes snake_case__ : Optional[Any] = depths snake_case__ : List[Any] = layer_type snake_case__ : Dict = hidden_act snake_case__ : Union[str, Any] = global_padding snake_case__ : List[str] = num_groups snake_case__ : str = drop_path_rate snake_case__ : List[Any] = embedding_dynamic_padding snake_case__ : List[str] = output_stride snake_case__ : Dict = width_factor snake_case__ : List[str] = ["""stem"""] + [f"stage{idx}" for idx in range(1 , len(__SCREAMING_SNAKE_CASE ) + 1 )] snake_case__ , snake_case__ : Dict = get_aligned_output_features_output_indices( out_features=__SCREAMING_SNAKE_CASE , out_indices=__SCREAMING_SNAKE_CASE , stage_names=self.stage_names )
38
0
from typing import Optional, Tuple, Union import torch from einops import rearrange, reduce from diffusers import DDIMScheduler, DDPMScheduler, DiffusionPipeline, ImagePipelineOutput, UNetaDConditionModel from diffusers.schedulers.scheduling_ddim import DDIMSchedulerOutput from diffusers.schedulers.scheduling_ddpm import DDPMSchedulerOutput snake_case__ : Tuple = 8 def _snake_case (__lowercase , __lowercase=BITS): UpperCamelCase_ = x.device UpperCamelCase_ = (x * 255).int().clamp(0 , 255) UpperCamelCase_ = 2 ** torch.arange(bits - 1 , -1 , -1 , device=__lowercase) UpperCamelCase_ = rearrange(__lowercase , 'd -> d 1 1') UpperCamelCase_ = rearrange(__lowercase , 'b c h w -> b c 1 h w') UpperCamelCase_ = ((x & mask) != 0).float() UpperCamelCase_ = rearrange(__lowercase , 'b c d h w -> b (c d) h w') UpperCamelCase_ = bits * 2 - 1 return bits def _snake_case (__lowercase , __lowercase=BITS): UpperCamelCase_ = x.device UpperCamelCase_ = (x > 0).int() UpperCamelCase_ = 2 ** torch.arange(bits - 1 , -1 , -1 , device=__lowercase , dtype=torch.intaa) UpperCamelCase_ = rearrange(__lowercase , 'd -> d 1 1') UpperCamelCase_ = rearrange(__lowercase , 'b (c d) h w -> b c d h w' , d=8) UpperCamelCase_ = reduce(x * mask , 'b c d h w -> b c h w' , 'sum') return (dec / 255).clamp(0.0 , 1.0) def _snake_case (self , __lowercase , __lowercase , __lowercase , __lowercase = 0.0 , __lowercase = True , __lowercase=None , __lowercase = True , ): if self.num_inference_steps is None: raise ValueError( 'Number of inference steps is \'None\', you need to run \'set_timesteps\' after creating the scheduler') # See formulas (12) and (16) of DDIM paper https://arxiv.org/pdf/2010.02502.pdf # Ideally, read DDIM paper in-detail understanding # Notation (<variable name> -> <name in paper> # - pred_noise_t -> e_theta(x_t, t) # - pred_original_sample -> f_theta(x_t, t) or x_0 # - std_dev_t -> sigma_t # - eta -> η # - pred_sample_direction -> "direction pointing to x_t" # - pred_prev_sample -> "x_t-1" # 1. get previous step value (=t-1) UpperCamelCase_ = timestep - self.config.num_train_timesteps // self.num_inference_steps # 2. compute alphas, betas UpperCamelCase_ = self.alphas_cumprod[timestep] UpperCamelCase_ = self.alphas_cumprod[prev_timestep] if prev_timestep >= 0 else self.final_alpha_cumprod UpperCamelCase_ = 1 - alpha_prod_t # 3. compute predicted original sample from predicted noise also called # "predicted x_0" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf UpperCamelCase_ = (sample - beta_prod_t ** 0.5 * model_output) / alpha_prod_t ** 0.5 # 4. Clip "predicted x_0" UpperCamelCase_ = self.bit_scale if self.config.clip_sample: UpperCamelCase_ = torch.clamp(__lowercase , -scale , __lowercase) # 5. compute variance: "sigma_t(η)" -> see formula (16) # σ_t = sqrt((1 − α_t−1)/(1 − α_t)) * sqrt(1 − α_t/α_t−1) UpperCamelCase_ = self._get_variance(__lowercase , __lowercase) UpperCamelCase_ = eta * variance ** 0.5 if use_clipped_model_output: # the model_output is always re-derived from the clipped x_0 in Glide UpperCamelCase_ = (sample - alpha_prod_t ** 0.5 * pred_original_sample) / beta_prod_t ** 0.5 # 6. compute "direction pointing to x_t" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf UpperCamelCase_ = (1 - alpha_prod_t_prev - std_dev_t**2) ** 0.5 * model_output # 7. compute x_t without "random noise" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf UpperCamelCase_ = alpha_prod_t_prev ** 0.5 * pred_original_sample + pred_sample_direction if eta > 0: # randn_like does not support generator https://github.com/pytorch/pytorch/issues/27072 UpperCamelCase_ = model_output.device if torch.is_tensor(__lowercase) else """cpu""" UpperCamelCase_ = torch.randn(model_output.shape , dtype=model_output.dtype , generator=__lowercase).to(__lowercase) UpperCamelCase_ = self._get_variance(__lowercase , __lowercase) ** 0.5 * eta * noise UpperCamelCase_ = prev_sample + variance if not return_dict: return (prev_sample,) return DDIMSchedulerOutput(prev_sample=__lowercase , pred_original_sample=__lowercase) def _snake_case (self , __lowercase , __lowercase , __lowercase , __lowercase="epsilon" , __lowercase=None , __lowercase = True , ): UpperCamelCase_ = timestep if model_output.shape[1] == sample.shape[1] * 2 and self.variance_type in ["learned", "learned_range"]: UpperCamelCase_ = torch.split(__lowercase , sample.shape[1] , dim=1) else: UpperCamelCase_ = None # 1. compute alphas, betas UpperCamelCase_ = self.alphas_cumprod[t] UpperCamelCase_ = self.alphas_cumprod[t - 1] if t > 0 else self.one UpperCamelCase_ = 1 - alpha_prod_t UpperCamelCase_ = 1 - alpha_prod_t_prev # 2. compute predicted original sample from predicted noise also called # "predicted x_0" of formula (15) from https://arxiv.org/pdf/2006.11239.pdf if prediction_type == "epsilon": UpperCamelCase_ = (sample - beta_prod_t ** 0.5 * model_output) / alpha_prod_t ** 0.5 elif prediction_type == "sample": UpperCamelCase_ = model_output else: raise ValueError(f"""Unsupported prediction_type {prediction_type}.""") # 3. Clip "predicted x_0" UpperCamelCase_ = self.bit_scale if self.config.clip_sample: UpperCamelCase_ = torch.clamp(__lowercase , -scale , __lowercase) # 4. Compute coefficients for pred_original_sample x_0 and current sample x_t # See formula (7) from https://arxiv.org/pdf/2006.11239.pdf UpperCamelCase_ = (alpha_prod_t_prev ** 0.5 * self.betas[t]) / beta_prod_t UpperCamelCase_ = self.alphas[t] ** 0.5 * beta_prod_t_prev / beta_prod_t # 5. Compute predicted previous sample µ_t # See formula (7) from https://arxiv.org/pdf/2006.11239.pdf UpperCamelCase_ = pred_original_sample_coeff * pred_original_sample + current_sample_coeff * sample # 6. Add noise UpperCamelCase_ = 0 if t > 0: UpperCamelCase_ = torch.randn( model_output.size() , dtype=model_output.dtype , layout=model_output.layout , generator=__lowercase).to(model_output.device) UpperCamelCase_ = (self._get_variance(__lowercase , predicted_variance=__lowercase) ** 0.5) * noise UpperCamelCase_ = pred_prev_sample + variance if not return_dict: return (pred_prev_sample,) return DDPMSchedulerOutput(prev_sample=__lowercase , pred_original_sample=__lowercase) class _a ( __SCREAMING_SNAKE_CASE ): """simple docstring""" def __init__( self , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase = 1.0 , ) -> Dict: super().__init__() UpperCamelCase_ = bit_scale UpperCamelCase_ = ( ddim_bit_scheduler_step if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else ddpm_bit_scheduler_step ) self.register_modules(unet=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) @torch.no_grad() def __call__( self , _UpperCAmelCase = 256 , _UpperCAmelCase = 256 , _UpperCAmelCase = 50 , _UpperCAmelCase = None , _UpperCAmelCase = 1 , _UpperCAmelCase = "pil" , _UpperCAmelCase = True , **_UpperCAmelCase , ) -> str: UpperCamelCase_ = torch.randn( (batch_size, self.unet.config.in_channels, height, width) , generator=__SCREAMING_SNAKE_CASE , ) UpperCamelCase_ = decimal_to_bits(__SCREAMING_SNAKE_CASE ) * self.bit_scale UpperCamelCase_ = latents.to(self.device ) self.scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) for t in self.progress_bar(self.scheduler.timesteps ): # predict the noise residual UpperCamelCase_ = self.unet(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ).sample # compute the previous noisy sample x_t -> x_t-1 UpperCamelCase_ = self.scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ).prev_sample UpperCamelCase_ = bits_to_decimal(__SCREAMING_SNAKE_CASE ) if output_type == "pil": UpperCamelCase_ = self.numpy_to_pil(__SCREAMING_SNAKE_CASE ) if not return_dict: return (image,) return ImagePipelineOutput(images=__SCREAMING_SNAKE_CASE )
23
'''simple docstring''' import argparse import json from pathlib import Path import requests import timm import torch from huggingface_hub import hf_hub_download from PIL import Image from timm.data import resolve_data_config from timm.data.transforms_factory import create_transform from transformers import ( BitConfig, ViTHybridConfig, ViTHybridForImageClassification, ViTHybridImageProcessor, ViTHybridModel, ) from transformers.image_utils import PILImageResampling from transformers.utils import logging logging.set_verbosity_info() A_ : Optional[int] = logging.get_logger(__name__) def UpperCamelCase__ ( __magic_name__ : Optional[Any] , __magic_name__ : str=False ) -> Tuple: '''simple docstring''' snake_case__ : int = [] # fmt: off # stem: rename_keys.append(("""cls_token""", """vit.embeddings.cls_token""") ) rename_keys.append(("""pos_embed""", """vit.embeddings.position_embeddings""") ) rename_keys.append(("""patch_embed.proj.weight""", """vit.embeddings.patch_embeddings.projection.weight""") ) rename_keys.append(("""patch_embed.proj.bias""", """vit.embeddings.patch_embeddings.projection.bias""") ) # backbone rename_keys.append(("""patch_embed.backbone.stem.conv.weight""", """vit.embeddings.patch_embeddings.backbone.bit.embedder.convolution.weight""") ) rename_keys.append(("""patch_embed.backbone.stem.norm.weight""", """vit.embeddings.patch_embeddings.backbone.bit.embedder.norm.weight""") ) rename_keys.append(("""patch_embed.backbone.stem.norm.bias""", """vit.embeddings.patch_embeddings.backbone.bit.embedder.norm.bias""") ) for stage_idx in range(len(config.backbone_config.depths ) ): for layer_idx in range(config.backbone_config.depths[stage_idx] ): rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.conv1.weight", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.conv1.weight") ) rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm1.weight", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm1.weight") ) rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm1.bias", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm1.bias") ) rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.conv2.weight", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.conv2.weight") ) rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm2.weight", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm2.weight") ) rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm2.bias", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm2.bias") ) rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.conv3.weight", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.conv3.weight") ) rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm3.weight", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm3.weight") ) rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm3.bias", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm3.bias") ) rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.0.downsample.conv.weight", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.0.downsample.conv.weight") ) rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.0.downsample.norm.weight", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.0.downsample.norm.weight") ) rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.0.downsample.norm.bias", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.0.downsample.norm.bias") ) # transformer encoder for i in range(config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append((f"blocks.{i}.norm1.weight", f"vit.encoder.layer.{i}.layernorm_before.weight") ) rename_keys.append((f"blocks.{i}.norm1.bias", f"vit.encoder.layer.{i}.layernorm_before.bias") ) rename_keys.append((f"blocks.{i}.attn.proj.weight", f"vit.encoder.layer.{i}.attention.output.dense.weight") ) rename_keys.append((f"blocks.{i}.attn.proj.bias", f"vit.encoder.layer.{i}.attention.output.dense.bias") ) rename_keys.append((f"blocks.{i}.norm2.weight", f"vit.encoder.layer.{i}.layernorm_after.weight") ) rename_keys.append((f"blocks.{i}.norm2.bias", f"vit.encoder.layer.{i}.layernorm_after.bias") ) rename_keys.append((f"blocks.{i}.mlp.fc1.weight", f"vit.encoder.layer.{i}.intermediate.dense.weight") ) rename_keys.append((f"blocks.{i}.mlp.fc1.bias", f"vit.encoder.layer.{i}.intermediate.dense.bias") ) rename_keys.append((f"blocks.{i}.mlp.fc2.weight", f"vit.encoder.layer.{i}.output.dense.weight") ) rename_keys.append((f"blocks.{i}.mlp.fc2.bias", f"vit.encoder.layer.{i}.output.dense.bias") ) if base_model: # layernorm + pooler rename_keys.extend( [ ("""norm.weight""", """layernorm.weight"""), ("""norm.bias""", """layernorm.bias"""), ("""pre_logits.fc.weight""", """pooler.dense.weight"""), ("""pre_logits.fc.bias""", """pooler.dense.bias"""), ] ) # if just the base model, we should remove "vit" from all keys that start with "vit" snake_case__ : List[Any] = [(pair[0], pair[1][4:]) if pair[1].startswith("""vit""" ) else pair for pair in rename_keys] else: # layernorm + classification head rename_keys.extend( [ ("""norm.weight""", """vit.layernorm.weight"""), ("""norm.bias""", """vit.layernorm.bias"""), ("""head.weight""", """classifier.weight"""), ("""head.bias""", """classifier.bias"""), ] ) # fmt: on return rename_keys def UpperCamelCase__ ( __magic_name__ : Tuple , __magic_name__ : int , __magic_name__ : Tuple=False ) -> str: '''simple docstring''' for i in range(config.num_hidden_layers ): if base_model: snake_case__ : int = """""" else: snake_case__ : Dict = """vit.""" # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) snake_case__ : int = state_dict.pop(f"blocks.{i}.attn.qkv.weight" ) snake_case__ : Union[str, Any] = state_dict.pop(f"blocks.{i}.attn.qkv.bias" ) # next, add query, keys and values (in that order) to the state dict snake_case__ : Optional[int] = in_proj_weight[ : config.hidden_size, : ] snake_case__ : Optional[Any] = in_proj_bias[: config.hidden_size] snake_case__ : List[Any] = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] snake_case__ : List[Any] = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] snake_case__ : List[Any] = in_proj_weight[ -config.hidden_size :, : ] snake_case__ : Optional[int] = in_proj_bias[-config.hidden_size :] def UpperCamelCase__ ( __magic_name__ : Optional[Any] ) -> List[str]: '''simple docstring''' snake_case__ : str = ["""head.weight""", """head.bias"""] for k in ignore_keys: state_dict.pop(__magic_name__ , __magic_name__ ) def UpperCamelCase__ ( __magic_name__ : List[str] , __magic_name__ : Union[str, Any] , __magic_name__ : str ) -> Union[str, Any]: '''simple docstring''' snake_case__ : List[str] = dct.pop(__magic_name__ ) snake_case__ : Dict = val def UpperCamelCase__ ( ) -> str: '''simple docstring''' snake_case__ : Optional[int] = """http://images.cocodataset.org/val2017/000000039769.jpg""" snake_case__ : Optional[int] = Image.open(requests.get(__magic_name__ , stream=__magic_name__ ).raw ) return im @torch.no_grad() def UpperCamelCase__ ( __magic_name__ : List[Any] , __magic_name__ : Union[str, Any] , __magic_name__ : int=False ) -> Optional[int]: '''simple docstring''' snake_case__ : int = BitConfig( global_padding="""same""" , layer_type="""bottleneck""" , depths=(3, 4, 9) , out_features=["""stage3"""] , embedding_dynamic_padding=__magic_name__ , ) snake_case__ : Optional[int] = ViTHybridConfig(backbone_config=__magic_name__ , image_size=3_84 , num_labels=10_00 ) snake_case__ : Union[str, Any] = False # load original model from timm snake_case__ : List[Any] = timm.create_model(__magic_name__ , pretrained=__magic_name__ ) timm_model.eval() # load state_dict of original model, remove and rename some keys snake_case__ : Optional[int] = timm_model.state_dict() if base_model: remove_classification_head_(__magic_name__ ) snake_case__ : int = create_rename_keys(__magic_name__ , __magic_name__ ) for src, dest in rename_keys: rename_key(__magic_name__ , __magic_name__ , __magic_name__ ) read_in_q_k_v(__magic_name__ , __magic_name__ , __magic_name__ ) snake_case__ : str = """huggingface/label-files""" snake_case__ : Union[str, Any] = """imagenet-1k-id2label.json""" snake_case__ : Dict = json.load(open(hf_hub_download(__magic_name__ , __magic_name__ , repo_type="""dataset""" ) , """r""" ) ) snake_case__ : List[Any] = {int(__magic_name__ ): v for k, v in idalabel.items()} snake_case__ : int = idalabel snake_case__ : str = {v: k for k, v in idalabel.items()} # load HuggingFace model if vit_name[-5:] == "in21k": snake_case__ : str = ViTHybridModel(__magic_name__ ).eval() else: snake_case__ : Union[str, Any] = ViTHybridForImageClassification(__magic_name__ ).eval() model.load_state_dict(__magic_name__ ) # create image processor snake_case__ : Optional[Any] = create_transform(**resolve_data_config({} , model=__magic_name__ ) ) snake_case__ : Union[str, Any] = transform.transforms snake_case__ : Tuple = { """bilinear""": PILImageResampling.BILINEAR, """bicubic""": PILImageResampling.BICUBIC, """nearest""": PILImageResampling.NEAREST, } snake_case__ : Any = ViTHybridImageProcessor( do_resize=__magic_name__ , size={"""shortest_edge""": timm_transforms[0].size} , resample=pillow_resamplings[timm_transforms[0].interpolation.value] , do_center_crop=__magic_name__ , crop_size={"""height""": timm_transforms[1].size[0], """width""": timm_transforms[1].size[1]} , do_normalize=__magic_name__ , image_mean=timm_transforms[-1].mean.tolist() , image_std=timm_transforms[-1].std.tolist() , ) snake_case__ : Any = prepare_img() snake_case__ : int = transform(__magic_name__ ).unsqueeze(0 ) snake_case__ : List[str] = processor(__magic_name__ , return_tensors="""pt""" ).pixel_values # verify pixel values assert torch.allclose(__magic_name__ , __magic_name__ ) # verify logits with torch.no_grad(): snake_case__ : Optional[Any] = model(__magic_name__ ) snake_case__ : Union[str, Any] = outputs.logits print("""Predicted class:""" , logits.argmax(-1 ).item() ) if base_model: snake_case__ : Dict = timm_model.forward_features(__magic_name__ ) assert timm_pooled_output.shape == outputs.pooler_output.shape assert torch.allclose(__magic_name__ , outputs.pooler_output , atol=1E-3 ) else: snake_case__ : int = timm_model(__magic_name__ ) assert timm_logits.shape == outputs.logits.shape assert torch.allclose(__magic_name__ , outputs.logits , atol=1E-3 ) print("""Looks ok!""" ) if pytorch_dump_folder_path is not None: Path(__magic_name__ ).mkdir(exist_ok=__magic_name__ ) print(f"Saving model {vit_name} to {pytorch_dump_folder_path}" ) model.save_pretrained(__magic_name__ ) print(f"Saving processor to {pytorch_dump_folder_path}" ) processor.save_pretrained(__magic_name__ ) if push_to_hub: print(f"Pushing model and processor to the hub {vit_name}" ) model.push_to_hub(f"ybelkada/{vit_name}" ) processor.push_to_hub(f"ybelkada/{vit_name}" ) if __name__ == "__main__": A_ : List[str] = argparse.ArgumentParser() # Required parameters parser.add_argument( "--vit_name", default="vit_base_r50_s16_384", type=str, help="Name of the hybrid ViT timm model you'd like to convert.", ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model directory." ) parser.add_argument( "--push_to_hub", action="store_true", help="Whether to upload the model to the HuggingFace hub." ) A_ : Union[str, Any] = parser.parse_args() convert_vit_checkpoint(args.vit_name, args.pytorch_dump_folder_path, args.push_to_hub)
38
0
def _lowercase ( __SCREAMING_SNAKE_CASE ) -> bool: UpperCamelCase__ : Tuple = n ** (1 / 3) return (val * val * val) == n if __name__ == "__main__": print(perfect_cube(27)) print(perfect_cube(4))
410
'''simple docstring''' from dataclasses import dataclass from typing import Optional import numpy as np import torch import torch.nn as nn from ..utils import BaseOutput, is_torch_version, randn_tensor from .attention_processor import SpatialNorm from .unet_ad_blocks import UNetMidBlockaD, get_down_block, get_up_block @dataclass class __snake_case ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' lowerCamelCase__ = 42 class __snake_case ( nn.Module ): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=("DownEncoderBlock2D",) , __SCREAMING_SNAKE_CASE=(6_4,) , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=3_2 , __SCREAMING_SNAKE_CASE="silu" , __SCREAMING_SNAKE_CASE=True , ): super().__init__() snake_case__ : str = layers_per_block snake_case__ : int = torch.nn.Convad( __SCREAMING_SNAKE_CASE , block_out_channels[0] , kernel_size=3 , stride=1 , padding=1 , ) snake_case__ : List[Any] = None snake_case__ : List[Any] = nn.ModuleList([] ) # down snake_case__ : Union[str, Any] = block_out_channels[0] for i, down_block_type in enumerate(__SCREAMING_SNAKE_CASE ): snake_case__ : Optional[Any] = output_channel snake_case__ : Union[str, Any] = block_out_channels[i] snake_case__ : int = i == len(__SCREAMING_SNAKE_CASE ) - 1 snake_case__ : str = get_down_block( __SCREAMING_SNAKE_CASE , num_layers=self.layers_per_block , in_channels=__SCREAMING_SNAKE_CASE , out_channels=__SCREAMING_SNAKE_CASE , add_downsample=not is_final_block , resnet_eps=1e-6 , downsample_padding=0 , resnet_act_fn=__SCREAMING_SNAKE_CASE , resnet_groups=__SCREAMING_SNAKE_CASE , attention_head_dim=__SCREAMING_SNAKE_CASE , temb_channels=__SCREAMING_SNAKE_CASE , ) self.down_blocks.append(__SCREAMING_SNAKE_CASE ) # mid snake_case__ : Optional[Any] = UNetMidBlockaD( in_channels=block_out_channels[-1] , resnet_eps=1e-6 , resnet_act_fn=__SCREAMING_SNAKE_CASE , output_scale_factor=1 , resnet_time_scale_shift="""default""" , attention_head_dim=block_out_channels[-1] , resnet_groups=__SCREAMING_SNAKE_CASE , temb_channels=__SCREAMING_SNAKE_CASE , ) # out snake_case__ : Tuple = nn.GroupNorm(num_channels=block_out_channels[-1] , num_groups=__SCREAMING_SNAKE_CASE , eps=1e-6 ) snake_case__ : Tuple = nn.SiLU() snake_case__ : str = 2 * out_channels if double_z else out_channels snake_case__ : int = nn.Convad(block_out_channels[-1] , __SCREAMING_SNAKE_CASE , 3 , padding=1 ) snake_case__ : Union[str, Any] = False def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE ): snake_case__ : Optional[Any] = x snake_case__ : int = self.conv_in(__SCREAMING_SNAKE_CASE ) if self.training and self.gradient_checkpointing: def create_custom_forward(__SCREAMING_SNAKE_CASE ): def custom_forward(*__SCREAMING_SNAKE_CASE ): return module(*__SCREAMING_SNAKE_CASE ) return custom_forward # down if is_torch_version(""">=""" , """1.11.0""" ): for down_block in self.down_blocks: snake_case__ : List[Any] = torch.utils.checkpoint.checkpoint( create_custom_forward(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE , use_reentrant=__SCREAMING_SNAKE_CASE ) # middle snake_case__ : List[Any] = torch.utils.checkpoint.checkpoint( create_custom_forward(self.mid_block ) , __SCREAMING_SNAKE_CASE , use_reentrant=__SCREAMING_SNAKE_CASE ) else: for down_block in self.down_blocks: snake_case__ : Dict = torch.utils.checkpoint.checkpoint(create_custom_forward(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) # middle snake_case__ : str = torch.utils.checkpoint.checkpoint(create_custom_forward(self.mid_block ) , __SCREAMING_SNAKE_CASE ) else: # down for down_block in self.down_blocks: snake_case__ : List[str] = down_block(__SCREAMING_SNAKE_CASE ) # middle snake_case__ : str = self.mid_block(__SCREAMING_SNAKE_CASE ) # post-process snake_case__ : Any = self.conv_norm_out(__SCREAMING_SNAKE_CASE ) snake_case__ : List[str] = self.conv_act(__SCREAMING_SNAKE_CASE ) snake_case__ : str = self.conv_out(__SCREAMING_SNAKE_CASE ) return sample class __snake_case ( nn.Module ): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=("UpDecoderBlock2D",) , __SCREAMING_SNAKE_CASE=(6_4,) , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=3_2 , __SCREAMING_SNAKE_CASE="silu" , __SCREAMING_SNAKE_CASE="group" , ): super().__init__() snake_case__ : Any = layers_per_block snake_case__ : Optional[Any] = nn.Convad( __SCREAMING_SNAKE_CASE , block_out_channels[-1] , kernel_size=3 , stride=1 , padding=1 , ) snake_case__ : Union[str, Any] = None snake_case__ : Dict = nn.ModuleList([] ) snake_case__ : Optional[int] = in_channels if norm_type == """spatial""" else None # mid snake_case__ : Tuple = UNetMidBlockaD( in_channels=block_out_channels[-1] , resnet_eps=1e-6 , resnet_act_fn=__SCREAMING_SNAKE_CASE , output_scale_factor=1 , resnet_time_scale_shift="""default""" if norm_type == """group""" else norm_type , attention_head_dim=block_out_channels[-1] , resnet_groups=__SCREAMING_SNAKE_CASE , temb_channels=__SCREAMING_SNAKE_CASE , ) # up snake_case__ : List[Any] = list(reversed(__SCREAMING_SNAKE_CASE ) ) snake_case__ : Optional[Any] = reversed_block_out_channels[0] for i, up_block_type in enumerate(__SCREAMING_SNAKE_CASE ): snake_case__ : List[Any] = output_channel snake_case__ : Optional[Any] = reversed_block_out_channels[i] snake_case__ : List[str] = i == len(__SCREAMING_SNAKE_CASE ) - 1 snake_case__ : int = get_up_block( __SCREAMING_SNAKE_CASE , num_layers=self.layers_per_block + 1 , in_channels=__SCREAMING_SNAKE_CASE , out_channels=__SCREAMING_SNAKE_CASE , prev_output_channel=__SCREAMING_SNAKE_CASE , add_upsample=not is_final_block , resnet_eps=1e-6 , resnet_act_fn=__SCREAMING_SNAKE_CASE , resnet_groups=__SCREAMING_SNAKE_CASE , attention_head_dim=__SCREAMING_SNAKE_CASE , temb_channels=__SCREAMING_SNAKE_CASE , resnet_time_scale_shift=__SCREAMING_SNAKE_CASE , ) self.up_blocks.append(__SCREAMING_SNAKE_CASE ) snake_case__ : int = output_channel # out if norm_type == "spatial": snake_case__ : List[Any] = SpatialNorm(block_out_channels[0] , __SCREAMING_SNAKE_CASE ) else: snake_case__ : Any = nn.GroupNorm(num_channels=block_out_channels[0] , num_groups=__SCREAMING_SNAKE_CASE , eps=1e-6 ) snake_case__ : Tuple = nn.SiLU() snake_case__ : Union[str, Any] = nn.Convad(block_out_channels[0] , __SCREAMING_SNAKE_CASE , 3 , padding=1 ) snake_case__ : int = False def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None ): snake_case__ : Union[str, Any] = z snake_case__ : Any = self.conv_in(__SCREAMING_SNAKE_CASE ) snake_case__ : Optional[Any] = next(iter(self.up_blocks.parameters() ) ).dtype if self.training and self.gradient_checkpointing: def create_custom_forward(__SCREAMING_SNAKE_CASE ): def custom_forward(*__SCREAMING_SNAKE_CASE ): return module(*__SCREAMING_SNAKE_CASE ) return custom_forward if is_torch_version(""">=""" , """1.11.0""" ): # middle snake_case__ : int = torch.utils.checkpoint.checkpoint( create_custom_forward(self.mid_block ) , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , use_reentrant=__SCREAMING_SNAKE_CASE ) snake_case__ : int = sample.to(__SCREAMING_SNAKE_CASE ) # up for up_block in self.up_blocks: snake_case__ : List[str] = torch.utils.checkpoint.checkpoint( create_custom_forward(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , use_reentrant=__SCREAMING_SNAKE_CASE ) else: # middle snake_case__ : Dict = torch.utils.checkpoint.checkpoint( create_custom_forward(self.mid_block ) , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) snake_case__ : Optional[Any] = sample.to(__SCREAMING_SNAKE_CASE ) # up for up_block in self.up_blocks: snake_case__ : str = torch.utils.checkpoint.checkpoint(create_custom_forward(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else: # middle snake_case__ : List[Any] = self.mid_block(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) snake_case__ : List[Any] = sample.to(__SCREAMING_SNAKE_CASE ) # up for up_block in self.up_blocks: snake_case__ : Dict = up_block(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # post-process if latent_embeds is None: snake_case__ : Optional[Any] = self.conv_norm_out(__SCREAMING_SNAKE_CASE ) else: snake_case__ : str = self.conv_norm_out(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) snake_case__ : Any = self.conv_act(__SCREAMING_SNAKE_CASE ) snake_case__ : Optional[Any] = self.conv_out(__SCREAMING_SNAKE_CASE ) return sample class __snake_case ( nn.Module ): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE="random" , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=True ): super().__init__() snake_case__ : int = n_e snake_case__ : Optional[int] = vq_embed_dim snake_case__ : int = beta snake_case__ : Optional[int] = legacy snake_case__ : Dict = nn.Embedding(self.n_e , self.vq_embed_dim ) self.embedding.weight.data.uniform_(-1.0 / self.n_e , 1.0 / self.n_e ) snake_case__ : List[str] = remap if self.remap is not None: self.register_buffer("""used""" , torch.tensor(np.load(self.remap ) ) ) snake_case__ : Optional[Any] = self.used.shape[0] snake_case__ : List[str] = unknown_index # "random" or "extra" or integer if self.unknown_index == "extra": snake_case__ : Dict = self.re_embed snake_case__ : List[str] = self.re_embed + 1 print( f"Remapping {self.n_e} indices to {self.re_embed} indices. " f"Using {self.unknown_index} for unknown indices." ) else: snake_case__ : Union[str, Any] = n_e snake_case__ : str = sane_index_shape def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE ): snake_case__ : Any = inds.shape assert len(__SCREAMING_SNAKE_CASE ) > 1 snake_case__ : Dict = inds.reshape(ishape[0] , -1 ) snake_case__ : Any = self.used.to(__SCREAMING_SNAKE_CASE ) snake_case__ : Dict = (inds[:, :, None] == used[None, None, ...]).long() snake_case__ : List[Any] = match.argmax(-1 ) snake_case__ : List[str] = match.sum(2 ) < 1 if self.unknown_index == "random": snake_case__ : List[str] = torch.randint(0 , self.re_embed , size=new[unknown].shape ).to(device=new.device ) else: snake_case__ : Optional[Any] = self.unknown_index return new.reshape(__SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE ): snake_case__ : List[Any] = inds.shape assert len(__SCREAMING_SNAKE_CASE ) > 1 snake_case__ : int = inds.reshape(ishape[0] , -1 ) snake_case__ : Optional[int] = self.used.to(__SCREAMING_SNAKE_CASE ) if self.re_embed > self.used.shape[0]: # extra token snake_case__ : List[Any] = 0 # simply set to zero snake_case__ : Union[str, Any] = torch.gather(used[None, :][inds.shape[0] * [0], :] , 1 , __SCREAMING_SNAKE_CASE ) return back.reshape(__SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE ): # reshape z -> (batch, height, width, channel) and flatten snake_case__ : Any = z.permute(0 , 2 , 3 , 1 ).contiguous() snake_case__ : Optional[Any] = z.view(-1 , self.vq_embed_dim ) # distances from z to embeddings e_j (z - e)^2 = z^2 + e^2 - 2 e * z snake_case__ : Dict = torch.argmin(torch.cdist(__SCREAMING_SNAKE_CASE , self.embedding.weight ) , dim=1 ) snake_case__ : Union[str, Any] = self.embedding(__SCREAMING_SNAKE_CASE ).view(z.shape ) snake_case__ : List[str] = None snake_case__ : Union[str, Any] = None # compute loss for embedding if not self.legacy: snake_case__ : Tuple = self.beta * torch.mean((z_q.detach() - z) ** 2 ) + torch.mean((z_q - z.detach()) ** 2 ) else: snake_case__ : List[Any] = torch.mean((z_q.detach() - z) ** 2 ) + self.beta * torch.mean((z_q - z.detach()) ** 2 ) # preserve gradients snake_case__ : Any = z + (z_q - z).detach() # reshape back to match original input shape snake_case__ : Union[str, Any] = z_q.permute(0 , 3 , 1 , 2 ).contiguous() if self.remap is not None: snake_case__ : List[Any] = min_encoding_indices.reshape(z.shape[0] , -1 ) # add batch axis snake_case__ : str = self.remap_to_used(__SCREAMING_SNAKE_CASE ) snake_case__ : str = min_encoding_indices.reshape(-1 , 1 ) # flatten if self.sane_index_shape: snake_case__ : Tuple = min_encoding_indices.reshape(z_q.shape[0] , z_q.shape[2] , z_q.shape[3] ) return z_q, loss, (perplexity, min_encodings, min_encoding_indices) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): # shape specifying (batch, height, width, channel) if self.remap is not None: snake_case__ : List[Any] = indices.reshape(shape[0] , -1 ) # add batch axis snake_case__ : Optional[int] = self.unmap_to_all(__SCREAMING_SNAKE_CASE ) snake_case__ : Optional[Any] = indices.reshape(-1 ) # flatten again # get quantized latent vectors snake_case__ : int = self.embedding(__SCREAMING_SNAKE_CASE ) if shape is not None: snake_case__ : str = z_q.view(__SCREAMING_SNAKE_CASE ) # reshape back to match original input shape snake_case__ : str = z_q.permute(0 , 3 , 1 , 2 ).contiguous() return z_q class __snake_case ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=False ): snake_case__ : Tuple = parameters snake_case__ , snake_case__ : Any = torch.chunk(__SCREAMING_SNAKE_CASE , 2 , dim=1 ) snake_case__ : Union[str, Any] = torch.clamp(self.logvar , -30.0 , 20.0 ) snake_case__ : Optional[int] = deterministic snake_case__ : Optional[int] = torch.exp(0.5 * self.logvar ) snake_case__ : Any = torch.exp(self.logvar ) if self.deterministic: snake_case__ : List[str] = torch.zeros_like( self.mean , device=self.parameters.device , dtype=self.parameters.dtype ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE = None ): # make sure sample is on the same device as the parameters and has same dtype snake_case__ : Dict = randn_tensor( self.mean.shape , generator=__SCREAMING_SNAKE_CASE , device=self.parameters.device , dtype=self.parameters.dtype ) snake_case__ : Optional[int] = self.mean + self.std * sample return x def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE=None ): if self.deterministic: return torch.Tensor([0.0] ) else: if other is None: return 0.5 * torch.sum(torch.pow(self.mean , 2 ) + self.var - 1.0 - self.logvar , dim=[1, 2, 3] ) else: return 0.5 * torch.sum( torch.pow(self.mean - other.mean , 2 ) / other.var + self.var / other.var - 1.0 - self.logvar + other.logvar , dim=[1, 2, 3] , ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=[1, 2, 3] ): if self.deterministic: return torch.Tensor([0.0] ) snake_case__ : Any = np.log(2.0 * np.pi ) return 0.5 * torch.sum(logtwopi + self.logvar + torch.pow(sample - self.mean , 2 ) / self.var , dim=__SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self ): return self.mean
38
0
def __A ( _A , _A ): """simple docstring""" return int(input_a == input_a == 0 ) def __A ( ): """simple docstring""" print("Truth Table of NOR Gate:" ) print("| Input 1 | Input 2 | Output |" ) print(f"""| 0 | 0 | {nor_gate(0 , 0 )} |""" ) print(f"""| 0 | 1 | {nor_gate(0 , 1 )} |""" ) print(f"""| 1 | 0 | {nor_gate(1 , 0 )} |""" ) print(f"""| 1 | 1 | {nor_gate(1 , 1 )} |""" ) if __name__ == "__main__": import doctest doctest.testmod() main()
197
'''simple docstring''' import inspect import unittest from transformers import DPTConfig from transformers.file_utils import is_torch_available, is_vision_available from transformers.models.auto import get_values from transformers.testing_utils import require_torch, require_vision, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import MODEL_MAPPING, DPTForDepthEstimation, DPTForSemanticSegmentation, DPTModel from transformers.models.dpt.modeling_dpt import DPT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import DPTImageProcessor class __snake_case : '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=3_2 , __SCREAMING_SNAKE_CASE=1_6 , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=3_2 , __SCREAMING_SNAKE_CASE=4 , __SCREAMING_SNAKE_CASE=[0, 1, 2, 3] , __SCREAMING_SNAKE_CASE=4 , __SCREAMING_SNAKE_CASE=3_7 , __SCREAMING_SNAKE_CASE="gelu" , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=0.02 , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=[1, 3_8_4, 2_4, 2_4] , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=None , ): snake_case__ : str = parent snake_case__ : Union[str, Any] = batch_size snake_case__ : Union[str, Any] = image_size snake_case__ : Optional[int] = patch_size snake_case__ : List[str] = num_channels snake_case__ : Any = is_training snake_case__ : int = use_labels snake_case__ : str = hidden_size snake_case__ : Tuple = num_hidden_layers snake_case__ : str = backbone_out_indices snake_case__ : List[Any] = num_attention_heads snake_case__ : Dict = intermediate_size snake_case__ : Optional[Any] = hidden_act snake_case__ : str = hidden_dropout_prob snake_case__ : int = attention_probs_dropout_prob snake_case__ : Dict = initializer_range snake_case__ : Optional[int] = num_labels snake_case__ : str = backbone_featmap_shape snake_case__ : List[Any] = scope snake_case__ : Optional[Any] = is_hybrid # sequence length of DPT = num_patches + 1 (we add 1 for the [CLS] token) snake_case__ : List[Any] = (image_size // patch_size) ** 2 snake_case__ : Union[str, Any] = num_patches + 1 def __UpperCamelCase ( self ): snake_case__ : Tuple = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) snake_case__ : str = None if self.use_labels: snake_case__ : Dict = ids_tensor([self.batch_size, self.image_size, self.image_size] , self.num_labels ) snake_case__ : Union[str, Any] = self.get_config() return config, pixel_values, labels def __UpperCamelCase ( self ): snake_case__ : Any = { """global_padding""": """same""", """layer_type""": """bottleneck""", """depths""": [3, 4, 9], """out_features""": ["""stage1""", """stage2""", """stage3"""], """embedding_dynamic_padding""": True, """hidden_sizes""": [9_6, 1_9_2, 3_8_4, 7_6_8], """num_groups""": 2, } return DPTConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , backbone_out_indices=self.backbone_out_indices , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=__SCREAMING_SNAKE_CASE , initializer_range=self.initializer_range , is_hybrid=self.is_hybrid , backbone_config=__SCREAMING_SNAKE_CASE , backbone_featmap_shape=self.backbone_featmap_shape , ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): snake_case__ : Dict = DPTModel(config=__SCREAMING_SNAKE_CASE ) model.to(__SCREAMING_SNAKE_CASE ) model.eval() snake_case__ : Union[str, Any] = model(__SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): snake_case__ : Optional[Any] = self.num_labels snake_case__ : str = DPTForDepthEstimation(__SCREAMING_SNAKE_CASE ) model.to(__SCREAMING_SNAKE_CASE ) model.eval() snake_case__ : Optional[Any] = model(__SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.predicted_depth.shape , (self.batch_size, self.image_size, self.image_size) ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): snake_case__ : Any = self.num_labels snake_case__ : Dict = DPTForSemanticSegmentation(__SCREAMING_SNAKE_CASE ) model.to(__SCREAMING_SNAKE_CASE ) model.eval() snake_case__ : str = model(__SCREAMING_SNAKE_CASE , labels=__SCREAMING_SNAKE_CASE ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size, self.image_size) ) def __UpperCamelCase ( self ): snake_case__ : Union[str, Any] = self.prepare_config_and_inputs() snake_case__ , snake_case__ , snake_case__ : Any = config_and_inputs snake_case__ : Optional[int] = {"""pixel_values""": pixel_values} return config, inputs_dict @require_torch class __snake_case ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowerCamelCase__ = (DPTModel, DPTForDepthEstimation, DPTForSemanticSegmentation) if is_torch_available() else () lowerCamelCase__ = ( { '''depth-estimation''': DPTForDepthEstimation, '''feature-extraction''': DPTModel, '''image-segmentation''': DPTForSemanticSegmentation, } if is_torch_available() else {} ) lowerCamelCase__ = False lowerCamelCase__ = False lowerCamelCase__ = False def __UpperCamelCase ( self ): snake_case__ : List[Any] = DPTModelTester(self ) snake_case__ : Any = ConfigTester(self , config_class=__SCREAMING_SNAKE_CASE , has_text_modality=__SCREAMING_SNAKE_CASE , hidden_size=3_7 ) def __UpperCamelCase ( self ): self.config_tester.run_common_tests() @unittest.skip(reason="""DPT does not use inputs_embeds""" ) def __UpperCamelCase ( self ): pass def __UpperCamelCase ( self ): snake_case__ , snake_case__ : Tuple = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: snake_case__ : Tuple = model_class(__SCREAMING_SNAKE_CASE ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) snake_case__ : str = model.get_output_embeddings() self.assertTrue(x is None or isinstance(__SCREAMING_SNAKE_CASE , nn.Linear ) ) def __UpperCamelCase ( self ): snake_case__ , snake_case__ : Dict = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: snake_case__ : str = model_class(__SCREAMING_SNAKE_CASE ) snake_case__ : str = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic snake_case__ : List[str] = [*signature.parameters.keys()] snake_case__ : str = ["""pixel_values"""] self.assertListEqual(arg_names[:1] , __SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self ): snake_case__ : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self ): snake_case__ : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_depth_estimation(*__SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self ): snake_case__ : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_semantic_segmentation(*__SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self ): for model_class in self.all_model_classes: if model_class.__name__ == "DPTForDepthEstimation": continue snake_case__ , snake_case__ : str = self.model_tester.prepare_config_and_inputs_for_common() snake_case__ : int = True if model_class in get_values(__SCREAMING_SNAKE_CASE ): continue snake_case__ : Any = model_class(__SCREAMING_SNAKE_CASE ) model.to(__SCREAMING_SNAKE_CASE ) model.train() snake_case__ : Optional[Any] = self._prepare_for_class(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , return_labels=__SCREAMING_SNAKE_CASE ) snake_case__ : Optional[int] = model(**__SCREAMING_SNAKE_CASE ).loss loss.backward() def __UpperCamelCase ( self ): for model_class in self.all_model_classes: if model_class.__name__ == "DPTForDepthEstimation": continue snake_case__ , snake_case__ : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() snake_case__ : Union[str, Any] = False snake_case__ : str = True if model_class in get_values(__SCREAMING_SNAKE_CASE ) or not model_class.supports_gradient_checkpointing: continue snake_case__ : Any = model_class(__SCREAMING_SNAKE_CASE ) model.to(__SCREAMING_SNAKE_CASE ) model.gradient_checkpointing_enable() model.train() snake_case__ : List[str] = self._prepare_for_class(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , return_labels=__SCREAMING_SNAKE_CASE ) snake_case__ : Any = model(**__SCREAMING_SNAKE_CASE ).loss loss.backward() def __UpperCamelCase ( self ): snake_case__ , snake_case__ : str = self.model_tester.prepare_config_and_inputs_for_common() snake_case__ : str = _config_zero_init(__SCREAMING_SNAKE_CASE ) for model_class in self.all_model_classes: snake_case__ : Any = model_class(config=__SCREAMING_SNAKE_CASE ) # Skip the check for the backbone snake_case__ : str = [] for name, module in model.named_modules(): if module.__class__.__name__ == "DPTViTHybridEmbeddings": snake_case__ : Optional[int] = [f"{name}.{key}" for key in module.state_dict().keys()] break for name, param in model.named_parameters(): if param.requires_grad: if name in backbone_params: continue self.assertIn( ((param.data.mean() * 1e9).round() / 1e9).item() , [0.0, 1.0] , msg=f"Parameter {name} of model {model_class} seems not properly initialized" , ) @unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests.""" ) def __UpperCamelCase ( self ): pass @slow def __UpperCamelCase ( self ): for model_name in DPT_PRETRAINED_MODEL_ARCHIVE_LIST[1:]: snake_case__ : List[str] = DPTModel.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsNotNone(__SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self ): # We do this test only for DPTForDepthEstimation since it is the only model that uses readout_type snake_case__ , snake_case__ : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() snake_case__ : Dict = """add""" with self.assertRaises(__SCREAMING_SNAKE_CASE ): snake_case__ : List[str] = DPTForDepthEstimation(__SCREAMING_SNAKE_CASE ) def UpperCamelCase__ ( ) -> Dict: '''simple docstring''' snake_case__ : List[Any] = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) return image @require_torch @require_vision @slow class __snake_case ( unittest.TestCase ): '''simple docstring''' def __UpperCamelCase ( self ): snake_case__ : Dict = DPTImageProcessor.from_pretrained("""Intel/dpt-hybrid-midas""" ) snake_case__ : Union[str, Any] = DPTForDepthEstimation.from_pretrained("""Intel/dpt-hybrid-midas""" ).to(__SCREAMING_SNAKE_CASE ) snake_case__ : Optional[Any] = prepare_img() snake_case__ : Optional[int] = image_processor(images=__SCREAMING_SNAKE_CASE , return_tensors="""pt""" ).to(__SCREAMING_SNAKE_CASE ) # forward pass with torch.no_grad(): snake_case__ : Dict = model(**__SCREAMING_SNAKE_CASE ) snake_case__ : Tuple = outputs.predicted_depth # verify the predicted depth snake_case__ : Any = torch.Size((1, 3_8_4, 3_8_4) ) self.assertEqual(predicted_depth.shape , __SCREAMING_SNAKE_CASE ) snake_case__ : List[Any] = torch.tensor( [[[5.6437, 5.6146, 5.6511], [5.4371, 5.5649, 5.5958], [5.5215, 5.5184, 5.5293]]] ).to(__SCREAMING_SNAKE_CASE ) self.assertTrue(torch.allclose(outputs.predicted_depth[:3, :3, :3] / 1_0_0 , __SCREAMING_SNAKE_CASE , atol=1e-4 ) )
38
0
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging _UpperCAmelCase = logging.get_logger(__name__) _UpperCAmelCase = { "xlm-mlm-en-2048": "https://huggingface.co/xlm-mlm-en-2048/resolve/main/config.json", "xlm-mlm-ende-1024": "https://huggingface.co/xlm-mlm-ende-1024/resolve/main/config.json", "xlm-mlm-enfr-1024": "https://huggingface.co/xlm-mlm-enfr-1024/resolve/main/config.json", "xlm-mlm-enro-1024": "https://huggingface.co/xlm-mlm-enro-1024/resolve/main/config.json", "xlm-mlm-tlm-xnli15-1024": "https://huggingface.co/xlm-mlm-tlm-xnli15-1024/resolve/main/config.json", "xlm-mlm-xnli15-1024": "https://huggingface.co/xlm-mlm-xnli15-1024/resolve/main/config.json", "xlm-clm-enfr-1024": "https://huggingface.co/xlm-clm-enfr-1024/resolve/main/config.json", "xlm-clm-ende-1024": "https://huggingface.co/xlm-clm-ende-1024/resolve/main/config.json", "xlm-mlm-17-1280": "https://huggingface.co/xlm-mlm-17-1280/resolve/main/config.json", "xlm-mlm-100-1280": "https://huggingface.co/xlm-mlm-100-1280/resolve/main/config.json", } class _UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = '''xlm''' SCREAMING_SNAKE_CASE : Tuple = { '''hidden_size''': '''emb_dim''', '''num_attention_heads''': '''n_heads''', '''num_hidden_layers''': '''n_layers''', '''n_words''': '''vocab_size''', # For backward compatibility } def __init__( self : Tuple , UpperCamelCase__ : List[Any]=30145 , UpperCamelCase__ : Union[str, Any]=2048 , UpperCamelCase__ : Any=12 , UpperCamelCase__ : int=16 , UpperCamelCase__ : str=0.1 , UpperCamelCase__ : Optional[Any]=0.1 , UpperCamelCase__ : List[Any]=True , UpperCamelCase__ : Dict=False , UpperCamelCase__ : Tuple=False , UpperCamelCase__ : List[str]=False , UpperCamelCase__ : Optional[int]=1 , UpperCamelCase__ : List[str]=True , UpperCamelCase__ : Tuple=512 , UpperCamelCase__ : List[Any]=2048**-0.5 , UpperCamelCase__ : Dict=1e-1_2 , UpperCamelCase__ : Optional[Any]=0.02 , UpperCamelCase__ : Tuple=0 , UpperCamelCase__ : Any=1 , UpperCamelCase__ : Optional[int]=2 , UpperCamelCase__ : Optional[int]=3 , UpperCamelCase__ : Tuple=5 , UpperCamelCase__ : Optional[int]=True , UpperCamelCase__ : Tuple="first" , UpperCamelCase__ : List[Any]=True , UpperCamelCase__ : int=None , UpperCamelCase__ : str=True , UpperCamelCase__ : List[Any]=0.1 , UpperCamelCase__ : str=5 , UpperCamelCase__ : Union[str, Any]=5 , UpperCamelCase__ : str=0 , UpperCamelCase__ : str=0 , UpperCamelCase__ : Any=2 , UpperCamelCase__ : Tuple=0 , **UpperCamelCase__ : Optional[int] , ): A = vocab_size A = emb_dim A = n_layers A = n_heads A = dropout A = attention_dropout A = gelu_activation A = sinusoidal_embeddings A = causal A = asm A = n_langs A = use_lang_emb A = layer_norm_eps A = bos_index A = eos_index A = pad_index A = unk_index A = mask_index A = is_encoder A = max_position_embeddings A = embed_init_std A = init_std A = summary_type A = summary_use_proj A = summary_activation A = summary_proj_to_labels A = summary_first_dropout A = start_n_top A = end_n_top A = mask_token_id A = lang_id if "n_words" in kwargs: A = kwargs["""n_words"""] super().__init__(pad_token_id=__SCREAMING_SNAKE_CASE , bos_token_id=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) class _UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' @property def UpperCamelCase ( self : Optional[Any] ): if self.task == "multiple-choice": A = {0: """batch""", 1: """choice""", 2: """sequence"""} else: A = {0: """batch""", 1: """sequence"""} return OrderedDict( [ ('input_ids', dynamic_axis), ('attention_mask', dynamic_axis), ('token_type_ids', dynamic_axis), ] )
699
'''simple docstring''' # Copyright 2021 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import json import os from ...utils.constants import SAGEMAKER_PARALLEL_EC2_INSTANCES, TORCH_DYNAMO_MODES from ...utils.dataclasses import ComputeEnvironment, SageMakerDistributedType from ...utils.imports import is_botoa_available from .config_args import SageMakerConfig from .config_utils import ( DYNAMO_BACKENDS, _ask_field, _ask_options, _convert_dynamo_backend, _convert_mixed_precision, _convert_sagemaker_distributed_mode, _convert_yes_no_to_bool, ) if is_botoa_available(): import botoa # noqa: F401 def UpperCamelCase__ ( __magic_name__ : Optional[Any] ) -> Dict: '''simple docstring''' snake_case__ : int = botoa.client("""iam""" ) snake_case__ : Union[str, Any] = { """Version""": """2012-10-17""", """Statement""": [ {"""Effect""": """Allow""", """Principal""": {"""Service""": """sagemaker.amazonaws.com"""}, """Action""": """sts:AssumeRole"""} ], } try: # create the role, associated with the chosen trust policy iam_client.create_role( RoleName=__magic_name__ , AssumeRolePolicyDocument=json.dumps(__magic_name__ , indent=2 ) ) snake_case__ : Dict = { """Version""": """2012-10-17""", """Statement""": [ { """Effect""": """Allow""", """Action""": [ """sagemaker:*""", """ecr:GetDownloadUrlForLayer""", """ecr:BatchGetImage""", """ecr:BatchCheckLayerAvailability""", """ecr:GetAuthorizationToken""", """cloudwatch:PutMetricData""", """cloudwatch:GetMetricData""", """cloudwatch:GetMetricStatistics""", """cloudwatch:ListMetrics""", """logs:CreateLogGroup""", """logs:CreateLogStream""", """logs:DescribeLogStreams""", """logs:PutLogEvents""", """logs:GetLogEvents""", """s3:CreateBucket""", """s3:ListBucket""", """s3:GetBucketLocation""", """s3:GetObject""", """s3:PutObject""", ], """Resource""": """*""", } ], } # attach policy to role iam_client.put_role_policy( RoleName=__magic_name__ , PolicyName=f"{role_name}_policy_permission" , PolicyDocument=json.dumps(__magic_name__ , indent=2 ) , ) except iam_client.exceptions.EntityAlreadyExistsException: print(f"role {role_name} already exists. Using existing one" ) def UpperCamelCase__ ( __magic_name__ : Any ) -> Tuple: '''simple docstring''' snake_case__ : List[str] = botoa.client("""iam""" ) return iam_client.get_role(RoleName=__magic_name__ )["Role"]["Arn"] def UpperCamelCase__ ( ) -> Tuple: '''simple docstring''' snake_case__ : Union[str, Any] = _ask_options( """How do you want to authorize?""" , ["""AWS Profile""", """Credentials (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) """] , __magic_name__ , ) snake_case__ : List[Any] = None if credentials_configuration == 0: snake_case__ : Dict = _ask_field("""Enter your AWS Profile name: [default] """ , default="""default""" ) snake_case__ : List[str] = aws_profile else: print( """Note you will need to provide AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY when you launch you training script with,""" """`accelerate launch --aws_access_key_id XXX --aws_secret_access_key YYY`""" ) snake_case__ : List[str] = _ask_field("""AWS Access Key ID: """ ) snake_case__ : int = aws_access_key_id snake_case__ : Optional[Any] = _ask_field("""AWS Secret Access Key: """ ) snake_case__ : List[str] = aws_secret_access_key snake_case__ : Tuple = _ask_field("""Enter your AWS Region: [us-east-1]""" , default="""us-east-1""" ) snake_case__ : Optional[int] = aws_region snake_case__ : int = _ask_options( """Do you already have an IAM Role for executing Amazon SageMaker Training Jobs?""" , ["""Provide IAM Role name""", """Create new IAM role using credentials"""] , __magic_name__ , ) if role_management == 0: snake_case__ : Optional[Any] = _ask_field("""Enter your IAM role name: """ ) else: snake_case__ : Optional[int] = """accelerate_sagemaker_execution_role""" print(f"Accelerate will create an iam role \"{iam_role_name}\" using the provided credentials" ) _create_iam_role_for_sagemaker(__magic_name__ ) snake_case__ : Dict = _ask_field( """Do you want to use custom Docker image? [yes/NO]: """ , _convert_yes_no_to_bool , default=__magic_name__ , error_message="""Please enter yes or no.""" , ) snake_case__ : Any = None if is_custom_docker_image: snake_case__ : str = _ask_field("""Enter your Docker image: """ , lambda __magic_name__ : str(__magic_name__ ).lower() ) snake_case__ : Tuple = _ask_field( """Do you want to provide SageMaker input channels with data locations? [yes/NO]: """ , _convert_yes_no_to_bool , default=__magic_name__ , error_message="""Please enter yes or no.""" , ) snake_case__ : List[Any] = None if is_sagemaker_inputs_enabled: snake_case__ : str = _ask_field( """Enter the path to the SageMaker inputs TSV file with columns (channel_name, data_location): """ , lambda __magic_name__ : str(__magic_name__ ).lower() , ) snake_case__ : Optional[int] = _ask_field( """Do you want to enable SageMaker metrics? [yes/NO]: """ , _convert_yes_no_to_bool , default=__magic_name__ , error_message="""Please enter yes or no.""" , ) snake_case__ : Optional[Any] = None if is_sagemaker_metrics_enabled: snake_case__ : List[Any] = _ask_field( """Enter the path to the SageMaker metrics TSV file with columns (metric_name, metric_regex): """ , lambda __magic_name__ : str(__magic_name__ ).lower() , ) snake_case__ : Tuple = _ask_options( """What is the distributed mode?""" , ["""No distributed training""", """Data parallelism"""] , _convert_sagemaker_distributed_mode , ) snake_case__ : Any = {} snake_case__ : List[Any] = _ask_field( """Do you wish to optimize your script with torch dynamo?[yes/NO]:""" , _convert_yes_no_to_bool , default=__magic_name__ , error_message="""Please enter yes or no.""" , ) if use_dynamo: snake_case__ : str = """dynamo_""" snake_case__ : Tuple = _ask_options( """Which dynamo backend would you like to use?""" , [x.lower() for x in DYNAMO_BACKENDS] , _convert_dynamo_backend , default=2 , ) snake_case__ : List[str] = _ask_field( """Do you want to customize the defaults sent to torch.compile? [yes/NO]: """ , _convert_yes_no_to_bool , default=__magic_name__ , error_message="""Please enter yes or no.""" , ) if use_custom_options: snake_case__ : str = _ask_options( """Which mode do you want to use?""" , __magic_name__ , lambda __magic_name__ : TORCH_DYNAMO_MODES[int(__magic_name__ )] , default="""default""" , ) snake_case__ : Union[str, Any] = _ask_field( """Do you want the fullgraph mode or it is ok to break model into several subgraphs? [yes/NO]: """ , _convert_yes_no_to_bool , default=__magic_name__ , error_message="""Please enter yes or no.""" , ) snake_case__ : str = _ask_field( """Do you want to enable dynamic shape tracing? [yes/NO]: """ , _convert_yes_no_to_bool , default=__magic_name__ , error_message="""Please enter yes or no.""" , ) snake_case__ : Dict = """Which EC2 instance type you want to use for your training?""" if distributed_type != SageMakerDistributedType.NO: snake_case__ : List[str] = _ask_options( __magic_name__ , __magic_name__ , lambda __magic_name__ : SAGEMAKER_PARALLEL_EC2_INSTANCES[int(__magic_name__ )] ) else: eca_instance_query += "? [ml.p3.2xlarge]:" snake_case__ : Optional[int] = _ask_field(__magic_name__ , lambda __magic_name__ : str(__magic_name__ ).lower() , default="""ml.p3.2xlarge""" ) snake_case__ : Dict = 1 if distributed_type in (SageMakerDistributedType.DATA_PARALLEL, SageMakerDistributedType.MODEL_PARALLEL): snake_case__ : Optional[Any] = _ask_field( """How many machines do you want use? [1]: """ , __magic_name__ , default=1 , ) snake_case__ : Union[str, Any] = _ask_options( """Do you wish to use FP16 or BF16 (mixed precision)?""" , ["""no""", """fp16""", """bf16""", """fp8"""] , _convert_mixed_precision , ) if use_dynamo and mixed_precision == "no": print( """Torch dynamo used without mixed precision requires TF32 to be efficient. Accelerate will enable it by default when launching your scripts.""" ) return SageMakerConfig( image_uri=__magic_name__ , compute_environment=ComputeEnvironment.AMAZON_SAGEMAKER , distributed_type=__magic_name__ , use_cpu=__magic_name__ , dynamo_config=__magic_name__ , eca_instance_type=__magic_name__ , profile=__magic_name__ , region=__magic_name__ , iam_role_name=__magic_name__ , mixed_precision=__magic_name__ , num_machines=__magic_name__ , sagemaker_inputs_file=__magic_name__ , sagemaker_metrics_file=__magic_name__ , )
38
0
from __future__ import annotations from bisect import bisect_left from functools import total_ordering from heapq import merge @total_ordering class _A ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' def __lt__( self : Dict , lowerCamelCase : Any ): '''simple docstring''' return self[-1] < other[-1] def __eq__( self : List[Any] , lowerCamelCase : List[str] ): '''simple docstring''' return self[-1] == other[-1] def snake_case_ ( _SCREAMING_SNAKE_CASE ): __lowercase = [] # sort into stacks for element in collection: __lowercase = Stack([element] ) __lowercase = bisect_left(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) if i != len(_SCREAMING_SNAKE_CASE ): stacks[i].append(_SCREAMING_SNAKE_CASE ) else: stacks.append(_SCREAMING_SNAKE_CASE ) # use a heap-based merge to merge stack efficiently __lowercase = merge(*(reversed(_SCREAMING_SNAKE_CASE ) for stack in stacks) ) return collection if __name__ == "__main__": snake_case__ : Optional[Any] = input("""Enter numbers separated by a comma:\n""").strip() snake_case__ : Tuple = [int(item) for item in user_input.split(""",""")] print(patience_sort(unsorted))
402
'''simple docstring''' from itertools import zip_longest import requests from bsa import BeautifulSoup from pandas import DataFrame def UpperCamelCase__ ( __magic_name__ : str = "laptop" ) -> DataFrame: '''simple docstring''' snake_case__ : Union[str, Any] = f"https://www.amazon.in/laptop/s?k={product}" snake_case__ : List[str] = { """User-Agent""": """Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)Chrome/44.0.2403.157 Safari/537.36""", """Accept-Language""": """en-US, en;q=0.5""", } snake_case__ : int = BeautifulSoup(requests.get(__magic_name__ , headers=__magic_name__ ).text ) # Initialize a Pandas dataframe with the column titles snake_case__ : Optional[Any] = DataFrame( columns=[ """Product Title""", """Product Link""", """Current Price of the product""", """Product Rating""", """MRP of the product""", """Discount""", ] ) # Loop through each entry and store them in the dataframe for item, _ in zip_longest( soup.find_all( """div""" , attrs={"""class""": """s-result-item""", """data-component-type""": """s-search-result"""} , ) , soup.find_all("""div""" , attrs={"""class""": """a-row a-size-base a-color-base"""} ) , ): try: snake_case__ : Optional[int] = item.ha.text snake_case__ : Any = """https://www.amazon.in/""" + item.ha.a["""href"""] snake_case__ : List[str] = item.find("""span""" , attrs={"""class""": """a-offscreen"""} ).text try: snake_case__ : Dict = item.find("""span""" , attrs={"""class""": """a-icon-alt"""} ).text except AttributeError: snake_case__ : Optional[int] = """Not available""" try: snake_case__ : Tuple = ( """₹""" + item.find( """span""" , attrs={"""class""": """a-price a-text-price"""} ).text.split("""₹""" )[1] ) except AttributeError: snake_case__ : Optional[Any] = """""" try: snake_case__ : str = float( ( ( float(product_mrp.strip("""₹""" ).replace(""",""" , """""" ) ) - float(product_price.strip("""₹""" ).replace(""",""" , """""" ) ) ) / float(product_mrp.strip("""₹""" ).replace(""",""" , """""" ) ) ) * 1_00 ) except ValueError: snake_case__ : List[Any] = float("""nan""" ) except AttributeError: pass snake_case__ : str = [ product_title, product_link, product_price, product_rating, product_mrp, discount, ] snake_case__ : List[Any] = """ """ snake_case__ : Union[str, Any] = """ """ data_frame.index += 1 return data_frame if __name__ == "__main__": A_ : int = "headphones" get_amazon_product_data(product).to_csv(F'Amazon Product Data for {product}.csv')
38
0
from itertools import zip_longest import requests from bsa import BeautifulSoup from pandas import DataFrame def UpperCAmelCase ( UpperCAmelCase = "laptop" )-> DataFrame: '''simple docstring''' SCREAMING_SNAKE_CASE_ = f'''https://www.amazon.in/laptop/s?k={product}''' SCREAMING_SNAKE_CASE_ = { """User-Agent""": """Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)Chrome/44.0.2403.157 Safari/537.36""", """Accept-Language""": """en-US, en;q=0.5""", } SCREAMING_SNAKE_CASE_ = BeautifulSoup(requests.get(UpperCAmelCase ,headers=UpperCAmelCase ).text ) # Initialize a Pandas dataframe with the column titles SCREAMING_SNAKE_CASE_ = DataFrame( columns=[ '''Product Title''', '''Product Link''', '''Current Price of the product''', '''Product Rating''', '''MRP of the product''', '''Discount''', ] ) # Loop through each entry and store them in the dataframe for item, _ in zip_longest( soup.find_all( '''div''' ,attrs={'''class''': '''s-result-item''', '''data-component-type''': '''s-search-result'''} ,) ,soup.find_all('''div''' ,attrs={'''class''': '''a-row a-size-base a-color-base'''} ) ,): try: SCREAMING_SNAKE_CASE_ = item.ha.text SCREAMING_SNAKE_CASE_ = """https://www.amazon.in/""" + item.ha.a["""href"""] SCREAMING_SNAKE_CASE_ = item.find('''span''' ,attrs={'''class''': '''a-offscreen'''} ).text try: SCREAMING_SNAKE_CASE_ = item.find('''span''' ,attrs={'''class''': '''a-icon-alt'''} ).text except AttributeError: SCREAMING_SNAKE_CASE_ = """Not available""" try: SCREAMING_SNAKE_CASE_ = ( """₹""" + item.find( '''span''' ,attrs={'''class''': '''a-price a-text-price'''} ).text.split('''₹''' )[1] ) except AttributeError: SCREAMING_SNAKE_CASE_ = """""" try: SCREAMING_SNAKE_CASE_ = float( ( ( float(product_mrp.strip('''₹''' ).replace(''',''' ,'''''' ) ) - float(product_price.strip('''₹''' ).replace(''',''' ,'''''' ) ) ) / float(product_mrp.strip('''₹''' ).replace(''',''' ,'''''' ) ) ) * 100 ) except ValueError: SCREAMING_SNAKE_CASE_ = float('''nan''' ) except AttributeError: pass SCREAMING_SNAKE_CASE_ = [ product_title, product_link, product_price, product_rating, product_mrp, discount, ] SCREAMING_SNAKE_CASE_ = """ """ SCREAMING_SNAKE_CASE_ = """ """ data_frame.index += 1 return data_frame if __name__ == "__main__": A_ = "headphones" get_amazon_product_data(product).to_csv(F'Amazon Product Data for {product}.csv')
393
'''simple docstring''' import itertools import json import os import unittest from transformers import AddedToken, LongformerTokenizer, LongformerTokenizerFast from transformers.models.longformer.tokenization_longformer import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class __snake_case ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowerCamelCase__ = LongformerTokenizer lowerCamelCase__ = True lowerCamelCase__ = LongformerTokenizerFast lowerCamelCase__ = True def __UpperCamelCase ( self ): super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt snake_case__ : Union[str, Any] = [ """l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """\u0120""", """\u0120l""", """\u0120n""", """\u0120lo""", """\u0120low""", """er""", """\u0120lowest""", """\u0120newer""", """\u0120wider""", """<unk>""", ] snake_case__ : Optional[int] = dict(zip(__SCREAMING_SNAKE_CASE , range(len(__SCREAMING_SNAKE_CASE ) ) ) ) snake_case__ : int = ["""#version: 0.2""", """\u0120 l""", """\u0120l o""", """\u0120lo w""", """e r""", """"""] snake_case__ : Any = {"""unk_token""": """<unk>"""} snake_case__ : List[Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""vocab_file"""] ) snake_case__ : 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(__SCREAMING_SNAKE_CASE ) + """\n""" ) with open(self.merges_file , """w""" , encoding="""utf-8""" ) as fp: fp.write("""\n""".join(__SCREAMING_SNAKE_CASE ) ) def __UpperCamelCase ( self , **__SCREAMING_SNAKE_CASE ): kwargs.update(self.special_tokens_map ) return self.tokenizer_class.from_pretrained(self.tmpdirname , **__SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self , **__SCREAMING_SNAKE_CASE ): kwargs.update(self.special_tokens_map ) return self.rust_tokenizer_class.from_pretrained(self.tmpdirname , **__SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE ): snake_case__ : str = """lower newer""" snake_case__ : Dict = """lower newer""" return input_text, output_text def __UpperCamelCase ( self ): snake_case__ : int = self.tokenizer_class(self.vocab_file , self.merges_file , **self.special_tokens_map ) snake_case__ : Tuple = """lower newer""" snake_case__ : Optional[Any] = ["""l""", """o""", """w""", """er""", """\u0120""", """n""", """e""", """w""", """er"""] snake_case__ : Tuple = tokenizer.tokenize(__SCREAMING_SNAKE_CASE ) # , add_prefix_space=True) self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) snake_case__ : Tuple = tokens + [tokenizer.unk_token] snake_case__ : List[Any] = [0, 1, 2, 1_5, 1_0, 9, 3, 2, 1_5, 1_9] self.assertListEqual(tokenizer.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self ): snake_case__ : Tuple = self.get_tokenizer() self.assertListEqual(tokenizer.encode("""Hello world!""" , add_special_tokens=__SCREAMING_SNAKE_CASE ) , [0, 3_1_4_1_4, 2_3_2, 3_2_8, 2] ) self.assertListEqual( tokenizer.encode("""Hello world! cécé herlolip 418""" , add_special_tokens=__SCREAMING_SNAKE_CASE ) , [0, 3_1_4_1_4, 2_3_2, 3_2_8, 7_4_0, 1_1_4_0, 1_2_6_9_5, 6_9, 4_6_0_7_8, 1_5_8_8, 2] , ) @slow def __UpperCamelCase ( self ): snake_case__ : List[Any] = self.tokenizer_class.from_pretrained("""allenai/longformer-base-4096""" ) snake_case__ : int = tokenizer.encode("""sequence builders""" , add_special_tokens=__SCREAMING_SNAKE_CASE ) snake_case__ : Dict = tokenizer.encode("""multi-sequence build""" , add_special_tokens=__SCREAMING_SNAKE_CASE ) snake_case__ : Optional[Any] = tokenizer.encode( """sequence builders""" , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) snake_case__ : Dict = tokenizer.encode( """sequence builders""" , """multi-sequence build""" , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) snake_case__ : Dict = tokenizer.build_inputs_with_special_tokens(__SCREAMING_SNAKE_CASE ) snake_case__ : Optional[int] = tokenizer.build_inputs_with_special_tokens(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) assert encoded_sentence == encoded_text_from_decode assert encoded_pair == encoded_pair_from_decode def __UpperCamelCase ( self ): snake_case__ : Optional[int] = self.get_tokenizer() snake_case__ : int = """Encode this sequence.""" snake_case__ : Union[str, Any] = tokenizer.byte_encoder[""" """.encode("""utf-8""" )[0]] # Testing encoder arguments snake_case__ : Optional[int] = tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) snake_case__ : Tuple = tokenizer.convert_ids_to_tokens(encoded[0] )[0] self.assertNotEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) snake_case__ : Optional[Any] = tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) snake_case__ : List[str] = tokenizer.convert_ids_to_tokens(encoded[0] )[0] self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) tokenizer.add_special_tokens({"""bos_token""": """<s>"""} ) snake_case__ : List[str] = tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) snake_case__ : str = tokenizer.convert_ids_to_tokens(encoded[1] )[0] self.assertNotEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # Testing spaces after special tokens snake_case__ : List[str] = """<mask>""" tokenizer.add_special_tokens( {"""mask_token""": AddedToken(__SCREAMING_SNAKE_CASE , lstrip=__SCREAMING_SNAKE_CASE , rstrip=__SCREAMING_SNAKE_CASE )} ) # mask token has a left space snake_case__ : Dict = tokenizer.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) snake_case__ : str = """Encode <mask> sequence""" snake_case__ : Tuple = """Encode <mask>sequence""" snake_case__ : Union[str, Any] = tokenizer.encode(__SCREAMING_SNAKE_CASE ) snake_case__ : List[str] = encoded.index(__SCREAMING_SNAKE_CASE ) snake_case__ : Optional[int] = tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1] )[0] self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) snake_case__ : Tuple = tokenizer.encode(__SCREAMING_SNAKE_CASE ) snake_case__ : str = encoded.index(__SCREAMING_SNAKE_CASE ) snake_case__ : List[Any] = tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1] )[0] self.assertNotEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self ): pass def __UpperCamelCase ( self ): for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f"{tokenizer.__class__.__name__} ({pretrained_name})" ): snake_case__ : List[Any] = self.rust_tokenizer_class.from_pretrained(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) snake_case__ : Any = self.tokenizer_class.from_pretrained(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) snake_case__ : List[str] = """A, <mask> AllenNLP sentence.""" snake_case__ : str = tokenizer_r.encode_plus(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , return_token_type_ids=__SCREAMING_SNAKE_CASE ) snake_case__ : Tuple = tokenizer_p.encode_plus(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , return_token_type_ids=__SCREAMING_SNAKE_CASE ) # token_type_ids should put 0 everywhere self.assertEqual(sum(tokens_r["""token_type_ids"""] ) , sum(tokens_p["""token_type_ids"""] ) ) # attention_mask should put 1 everywhere, so sum over length should be 1 self.assertEqual( sum(tokens_r["""attention_mask"""] ) / len(tokens_r["""attention_mask"""] ) , sum(tokens_p["""attention_mask"""] ) / len(tokens_p["""attention_mask"""] ) , ) snake_case__ : Union[str, Any] = tokenizer_r.convert_ids_to_tokens(tokens_r["""input_ids"""] ) snake_case__ : Dict = tokenizer_p.convert_ids_to_tokens(tokens_p["""input_ids"""] ) # Rust correctly handles the space before the mask while python doesnt self.assertSequenceEqual(tokens_p["""input_ids"""] , [0, 2_5_0, 6, 5_0_2_6_4, 3_8_2_3, 4_8_7, 2_1_9_9_2, 3_6_4_5, 4, 2] ) self.assertSequenceEqual(tokens_r["""input_ids"""] , [0, 2_5_0, 6, 5_0_2_6_4, 3_8_2_3, 4_8_7, 2_1_9_9_2, 3_6_4_5, 4, 2] ) self.assertSequenceEqual( __SCREAMING_SNAKE_CASE , ["""<s>""", """A""", """,""", """<mask>""", """ĠAllen""", """N""", """LP""", """Ġsentence""", """.""", """</s>"""] ) self.assertSequenceEqual( __SCREAMING_SNAKE_CASE , ["""<s>""", """A""", """,""", """<mask>""", """ĠAllen""", """N""", """LP""", """Ġsentence""", """.""", """</s>"""] ) def __UpperCamelCase ( self ): for trim_offsets, add_prefix_space in itertools.product([True, False] , repeat=2 ): snake_case__ : Any = self.rust_tokenizer_class.from_pretrained( self.tmpdirname , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) snake_case__ : Dict = json.loads(tokenizer_r.backend_tokenizer.pre_tokenizer.__getstate__() ) snake_case__ : List[str] = json.loads(tokenizer_r.backend_tokenizer.post_processor.__getstate__() ) self.assertEqual(pre_tokenizer_state["""add_prefix_space"""] , __SCREAMING_SNAKE_CASE ) self.assertEqual(post_processor_state["""add_prefix_space"""] , __SCREAMING_SNAKE_CASE ) self.assertEqual(post_processor_state["""trim_offsets"""] , __SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self ): # Test which aims to verify that the offsets are well adapted to the argument `add_prefix_space` and # `trim_offsets` for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f"{tokenizer.__class__.__name__} ({pretrained_name})" ): snake_case__ : Union[str, Any] = """hello""" # `hello` is a token in the vocabulary of `pretrained_name` snake_case__ : Any = f"{text_of_1_token} {text_of_1_token}" snake_case__ : Union[str, Any] = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) snake_case__ : Union[str, Any] = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (len(__SCREAMING_SNAKE_CASE ) + 1, len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) snake_case__ : List[Any] = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) snake_case__ : str = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (len(__SCREAMING_SNAKE_CASE ) + 1, len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) snake_case__ : Optional[Any] = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) snake_case__ : str = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (len(__SCREAMING_SNAKE_CASE ), len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) snake_case__ : Tuple = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) snake_case__ : List[Any] = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (len(__SCREAMING_SNAKE_CASE ), len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) snake_case__ : Optional[Any] = f" {text}" # tokenizer_r = self.rust_tokenizer_class.from_pretrained( # pretrained_name, use_fast=True, add_prefix_space=True, trim_offsets=True # ) # encoding = tokenizer_r(text, return_offsets_mapping=True, add_special_tokens=False) # self.assertEqual(encoding.offset_mapping[0], (1, 1 + len(text_of_1_token))) # self.assertEqual( # encoding.offset_mapping[1], # (1 + len(text_of_1_token) + 1, 1 + len(text_of_1_token) + 1 + len(text_of_1_token)), # ) snake_case__ : Dict = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) snake_case__ : Optional[Any] = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (1, 1 + len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(__SCREAMING_SNAKE_CASE ) + 1, 1 + len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) snake_case__ : Any = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) snake_case__ : Any = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, 1 + len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(__SCREAMING_SNAKE_CASE ), 1 + len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) snake_case__ : List[Any] = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) snake_case__ : List[Any] = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, 1 + len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(__SCREAMING_SNAKE_CASE ), 1 + len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , )
38
0
"""simple docstring""" from . import __version__ # Backward compatibility imports, to make sure all those objects can be found in file_utils from .utils import ( CLOUDFRONT_DISTRIB_PREFIX, CONFIG_NAME, DISABLE_TELEMETRY, DUMMY_INPUTS, DUMMY_MASK, ENV_VARS_TRUE_AND_AUTO_VALUES, ENV_VARS_TRUE_VALUES, FEATURE_EXTRACTOR_NAME, FLAX_WEIGHTS_NAME, HF_MODULES_CACHE, HUGGINGFACE_CO_PREFIX, HUGGINGFACE_CO_RESOLVE_ENDPOINT, MODEL_CARD_NAME, MULTIPLE_CHOICE_DUMMY_INPUTS, PYTORCH_PRETRAINED_BERT_CACHE, PYTORCH_TRANSFORMERS_CACHE, S3_BUCKET_PREFIX, SENTENCEPIECE_UNDERLINE, SPIECE_UNDERLINE, TF2_WEIGHTS_NAME, TF_WEIGHTS_NAME, TORCH_FX_REQUIRED_VERSION, TRANSFORMERS_CACHE, TRANSFORMERS_DYNAMIC_MODULE_NAME, USE_JAX, USE_TF, USE_TORCH, WEIGHTS_INDEX_NAME, WEIGHTS_NAME, ContextManagers, DummyObject, EntryNotFoundError, ExplicitEnum, ModelOutput, PaddingStrategy, PushToHubMixin, RepositoryNotFoundError, RevisionNotFoundError, TensorType, _LazyModule, add_code_sample_docstrings, add_end_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, cached_property, copy_func, default_cache_path, define_sagemaker_information, get_cached_models, get_file_from_repo, get_full_repo_name, get_torch_version, has_file, http_user_agent, is_apex_available, is_bsa_available, is_coloredlogs_available, is_datasets_available, is_detectrona_available, is_faiss_available, is_flax_available, is_ftfy_available, is_in_notebook, is_ipex_available, is_librosa_available, is_offline_mode, is_onnx_available, is_pandas_available, is_phonemizer_available, is_protobuf_available, is_psutil_available, is_pyanvml_available, is_pyctcdecode_available, is_pytesseract_available, is_pytorch_quantization_available, is_rjieba_available, is_sagemaker_dp_enabled, is_sagemaker_mp_enabled, is_scipy_available, is_sentencepiece_available, is_seqio_available, is_sklearn_available, is_soundfile_availble, is_spacy_available, is_speech_available, is_tensor, is_tensorflow_probability_available, is_tfaonnx_available, is_tf_available, is_timm_available, is_tokenizers_available, is_torch_available, is_torch_bfaa_available, is_torch_cuda_available, is_torch_fx_available, is_torch_fx_proxy, is_torch_mps_available, is_torch_tfaa_available, is_torch_tpu_available, is_torchaudio_available, is_training_run_on_sagemaker, is_vision_available, replace_return_docstrings, requires_backends, to_numpy, to_py_obj, torch_only_method, )
434
'''simple docstring''' from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices A_ : int = logging.get_logger(__name__) A_ : Any = { "microsoft/resnet-50": "https://huggingface.co/microsoft/resnet-50/blob/main/config.json", } class __snake_case ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): '''simple docstring''' lowerCamelCase__ = '''resnet''' lowerCamelCase__ = ['''basic''', '''bottleneck'''] def __init__( self , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=6_4 , __SCREAMING_SNAKE_CASE=[2_5_6, 5_1_2, 1_0_2_4, 2_0_4_8] , __SCREAMING_SNAKE_CASE=[3, 4, 6, 3] , __SCREAMING_SNAKE_CASE="bottleneck" , __SCREAMING_SNAKE_CASE="relu" , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE , ): super().__init__(**__SCREAMING_SNAKE_CASE ) if layer_type not in self.layer_types: raise ValueError(f"layer_type={layer_type} is not one of {','.join(self.layer_types )}" ) snake_case__ : List[Any] = num_channels snake_case__ : str = embedding_size snake_case__ : List[Any] = hidden_sizes snake_case__ : Dict = depths snake_case__ : List[Any] = layer_type snake_case__ : int = hidden_act snake_case__ : Union[str, Any] = downsample_in_first_stage snake_case__ : Dict = ["""stem"""] + [f"stage{idx}" for idx in range(1 , len(__SCREAMING_SNAKE_CASE ) + 1 )] snake_case__ , snake_case__ : Any = get_aligned_output_features_output_indices( out_features=__SCREAMING_SNAKE_CASE , out_indices=__SCREAMING_SNAKE_CASE , stage_names=self.stage_names ) class __snake_case ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' lowerCamelCase__ = version.parse('''1.11''' ) @property def __UpperCamelCase ( self ): return OrderedDict( [ ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}), ] ) @property def __UpperCamelCase ( self ): return 1e-3
38
0
import argparse import json from pathlib import Path import requests import timm import torch from huggingface_hub import hf_hub_download from PIL import Image from timm.data import resolve_data_config from timm.data.transforms_factory import create_transform from transformers import ( BitConfig, ViTHybridConfig, ViTHybridForImageClassification, ViTHybridImageProcessor, ViTHybridModel, ) from transformers.image_utils import PILImageResampling from transformers.utils import logging logging.set_verbosity_info() snake_case : Optional[int] = logging.get_logger(__name__) def __lowercase ( __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : str=False ): a__ = [] # fmt: off # stem: rename_keys.append(('cls_token', 'vit.embeddings.cls_token') ) rename_keys.append(('pos_embed', 'vit.embeddings.position_embeddings') ) rename_keys.append(('patch_embed.proj.weight', 'vit.embeddings.patch_embeddings.projection.weight') ) rename_keys.append(('patch_embed.proj.bias', 'vit.embeddings.patch_embeddings.projection.bias') ) # backbone rename_keys.append(('patch_embed.backbone.stem.conv.weight', 'vit.embeddings.patch_embeddings.backbone.bit.embedder.convolution.weight') ) rename_keys.append(('patch_embed.backbone.stem.norm.weight', 'vit.embeddings.patch_embeddings.backbone.bit.embedder.norm.weight') ) rename_keys.append(('patch_embed.backbone.stem.norm.bias', 'vit.embeddings.patch_embeddings.backbone.bit.embedder.norm.bias') ) for stage_idx in range(len(config.backbone_config.depths ) ): for layer_idx in range(config.backbone_config.depths[stage_idx] ): rename_keys.append((F'patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.conv1.weight', F'vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.conv1.weight') ) rename_keys.append((F'patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm1.weight', F'vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm1.weight') ) rename_keys.append((F'patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm1.bias', F'vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm1.bias') ) rename_keys.append((F'patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.conv2.weight', F'vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.conv2.weight') ) rename_keys.append((F'patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm2.weight', F'vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm2.weight') ) rename_keys.append((F'patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm2.bias', F'vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm2.bias') ) rename_keys.append((F'patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.conv3.weight', F'vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.conv3.weight') ) rename_keys.append((F'patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm3.weight', F'vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm3.weight') ) rename_keys.append((F'patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm3.bias', F'vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm3.bias') ) rename_keys.append((F'patch_embed.backbone.stages.{stage_idx}.blocks.0.downsample.conv.weight', F'vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.0.downsample.conv.weight') ) rename_keys.append((F'patch_embed.backbone.stages.{stage_idx}.blocks.0.downsample.norm.weight', F'vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.0.downsample.norm.weight') ) rename_keys.append((F'patch_embed.backbone.stages.{stage_idx}.blocks.0.downsample.norm.bias', F'vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.0.downsample.norm.bias') ) # transformer encoder for i in range(config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append((F'blocks.{i}.norm1.weight', F'vit.encoder.layer.{i}.layernorm_before.weight') ) rename_keys.append((F'blocks.{i}.norm1.bias', F'vit.encoder.layer.{i}.layernorm_before.bias') ) rename_keys.append((F'blocks.{i}.attn.proj.weight', F'vit.encoder.layer.{i}.attention.output.dense.weight') ) rename_keys.append((F'blocks.{i}.attn.proj.bias', F'vit.encoder.layer.{i}.attention.output.dense.bias') ) rename_keys.append((F'blocks.{i}.norm2.weight', F'vit.encoder.layer.{i}.layernorm_after.weight') ) rename_keys.append((F'blocks.{i}.norm2.bias', F'vit.encoder.layer.{i}.layernorm_after.bias') ) rename_keys.append((F'blocks.{i}.mlp.fc1.weight', F'vit.encoder.layer.{i}.intermediate.dense.weight') ) rename_keys.append((F'blocks.{i}.mlp.fc1.bias', F'vit.encoder.layer.{i}.intermediate.dense.bias') ) rename_keys.append((F'blocks.{i}.mlp.fc2.weight', F'vit.encoder.layer.{i}.output.dense.weight') ) rename_keys.append((F'blocks.{i}.mlp.fc2.bias', F'vit.encoder.layer.{i}.output.dense.bias') ) if base_model: # layernorm + pooler rename_keys.extend( [ ('norm.weight', 'layernorm.weight'), ('norm.bias', 'layernorm.bias'), ('pre_logits.fc.weight', 'pooler.dense.weight'), ('pre_logits.fc.bias', 'pooler.dense.bias'), ] ) # if just the base model, we should remove "vit" from all keys that start with "vit" a__ = [(pair[0], pair[1][4:]) if pair[1].startswith('vit' ) else pair for pair in rename_keys] else: # layernorm + classification head rename_keys.extend( [ ('norm.weight', 'vit.layernorm.weight'), ('norm.bias', 'vit.layernorm.bias'), ('head.weight', 'classifier.weight'), ('head.bias', 'classifier.bias'), ] ) # fmt: on return rename_keys def __lowercase ( __lowerCAmelCase : Tuple , __lowerCAmelCase : int , __lowerCAmelCase : Tuple=False ): for i in range(config.num_hidden_layers ): if base_model: a__ = """""" else: a__ = """vit.""" # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) a__ = state_dict.pop(F'blocks.{i}.attn.qkv.weight' ) a__ = state_dict.pop(F'blocks.{i}.attn.qkv.bias' ) # next, add query, keys and values (in that order) to the state dict a__ = in_proj_weight[ : config.hidden_size, : ] a__ = in_proj_bias[: config.hidden_size] a__ = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] a__ = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] a__ = in_proj_weight[ -config.hidden_size :, : ] a__ = in_proj_bias[-config.hidden_size :] def __lowercase ( __lowerCAmelCase : Optional[Any] ): a__ = ["""head.weight""", """head.bias"""] for k in ignore_keys: state_dict.pop(__lowerCAmelCase , __lowerCAmelCase ) def __lowercase ( __lowerCAmelCase : List[str] , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : str ): a__ = dct.pop(__lowerCAmelCase ) a__ = val def __lowercase ( ): a__ = """http://images.cocodataset.org/val2017/000000039769.jpg""" a__ = Image.open(requests.get(__lowerCAmelCase , stream=__lowerCAmelCase ).raw ) return im @torch.no_grad() def __lowercase ( __lowerCAmelCase : List[Any] , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : int=False ): a__ = BitConfig( global_padding='same' , layer_type='bottleneck' , depths=(3, 4, 9) , out_features=['stage3'] , embedding_dynamic_padding=__lowerCAmelCase , ) a__ = ViTHybridConfig(backbone_config=__lowerCAmelCase , image_size=3_8_4 , num_labels=1_0_0_0 ) a__ = False # load original model from timm a__ = timm.create_model(__lowerCAmelCase , pretrained=__lowerCAmelCase ) timm_model.eval() # load state_dict of original model, remove and rename some keys a__ = timm_model.state_dict() if base_model: remove_classification_head_(__lowerCAmelCase ) a__ = create_rename_keys(__lowerCAmelCase , __lowerCAmelCase ) for src, dest in rename_keys: rename_key(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) read_in_q_k_v(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) a__ = """huggingface/label-files""" a__ = """imagenet-1k-id2label.json""" a__ = json.load(open(hf_hub_download(__lowerCAmelCase , __lowerCAmelCase , repo_type='dataset' ) , 'r' ) ) a__ = {int(__lowerCAmelCase ): v for k, v in idalabel.items()} a__ = idalabel a__ = {v: k for k, v in idalabel.items()} # load HuggingFace model if vit_name[-5:] == "in21k": a__ = ViTHybridModel(__lowerCAmelCase ).eval() else: a__ = ViTHybridForImageClassification(__lowerCAmelCase ).eval() model.load_state_dict(__lowerCAmelCase ) # create image processor a__ = create_transform(**resolve_data_config({} , model=__lowerCAmelCase ) ) a__ = transform.transforms a__ = { """bilinear""": PILImageResampling.BILINEAR, """bicubic""": PILImageResampling.BICUBIC, """nearest""": PILImageResampling.NEAREST, } a__ = ViTHybridImageProcessor( do_resize=__lowerCAmelCase , size={'shortest_edge': timm_transforms[0].size} , resample=pillow_resamplings[timm_transforms[0].interpolation.value] , do_center_crop=__lowerCAmelCase , crop_size={'height': timm_transforms[1].size[0], 'width': timm_transforms[1].size[1]} , do_normalize=__lowerCAmelCase , image_mean=timm_transforms[-1].mean.tolist() , image_std=timm_transforms[-1].std.tolist() , ) a__ = prepare_img() a__ = transform(__lowerCAmelCase ).unsqueeze(0 ) a__ = processor(__lowerCAmelCase , return_tensors='pt' ).pixel_values # verify pixel values assert torch.allclose(__lowerCAmelCase , __lowerCAmelCase ) # verify logits with torch.no_grad(): a__ = model(__lowerCAmelCase ) a__ = outputs.logits print('Predicted class:' , logits.argmax(-1 ).item() ) if base_model: a__ = timm_model.forward_features(__lowerCAmelCase ) assert timm_pooled_output.shape == outputs.pooler_output.shape assert torch.allclose(__lowerCAmelCase , outputs.pooler_output , atol=1E-3 ) else: a__ = timm_model(__lowerCAmelCase ) assert timm_logits.shape == outputs.logits.shape assert torch.allclose(__lowerCAmelCase , outputs.logits , atol=1E-3 ) print('Looks ok!' ) if pytorch_dump_folder_path is not None: Path(__lowerCAmelCase ).mkdir(exist_ok=__lowerCAmelCase ) print(F'Saving model {vit_name} to {pytorch_dump_folder_path}' ) model.save_pretrained(__lowerCAmelCase ) print(F'Saving processor to {pytorch_dump_folder_path}' ) processor.save_pretrained(__lowerCAmelCase ) if push_to_hub: print(F'Pushing model and processor to the hub {vit_name}' ) model.push_to_hub(F'ybelkada/{vit_name}' ) processor.push_to_hub(F'ybelkada/{vit_name}' ) if __name__ == "__main__": snake_case : List[str] = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--vit_name''', default='''vit_base_r50_s16_384''', type=str, help='''Name of the hybrid ViT timm model you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) parser.add_argument( '''--push_to_hub''', action='''store_true''', help='''Whether to upload the model to the HuggingFace hub.''' ) snake_case : Union[str, Any] = parser.parse_args() convert_vit_checkpoint(args.vit_name, args.pytorch_dump_folder_path, args.push_to_hub)
335
'''simple docstring''' # limitations under the License. # NOTE: This file is deprecated and will be removed in a future version. # It only exists so that temporarely `from diffusers.pipelines import DiffusionPipeline` works from .pipelines import DiffusionPipeline, ImagePipelineOutput # noqa: F401 from .utils import deprecate deprecate( "pipelines_utils", "0.22.0", "Importing `DiffusionPipeline` or `ImagePipelineOutput` from diffusers.pipeline_utils is deprecated. Please import from diffusers.pipelines.pipeline_utils instead.", standard_warn=False, stacklevel=3, )
38
0
import gc import random import unittest import numpy as np import torch from diffusers import DDIMScheduler, KandinskyVaaPipeline, KandinskyVaaPriorPipeline, UNetaDConditionModel, VQModel from diffusers.utils import floats_tensor, 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 ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): __a = KandinskyVaaPipeline __a = [ """image_embeds""", """negative_image_embeds""", ] __a = ["""image_embeds""", """negative_image_embeds"""] __a = [ """generator""", """height""", """width""", """latents""", """guidance_scale""", """num_inference_steps""", """return_dict""", """guidance_scale""", """num_images_per_prompt""", """output_type""", """return_dict""", ] __a = False @property def lowercase_ ( self ): """simple docstring""" return 32 @property def lowercase_ ( self ): """simple docstring""" return 32 @property def lowercase_ ( self ): """simple docstring""" return self.time_input_dim @property def lowercase_ ( self ): """simple docstring""" return self.time_input_dim * 4 @property def lowercase_ ( self ): """simple docstring""" return 100 @property def lowercase_ ( self ): """simple docstring""" torch.manual_seed(0 ) lowerCAmelCase__ : Union[str, Any] = { """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, } lowerCAmelCase__ : Dict = UNetaDConditionModel(**__SCREAMING_SNAKE_CASE ) return model @property def lowercase_ ( self ): """simple docstring""" return { "block_out_channels": [32, 64], "down_block_types": ["DownEncoderBlock2D", "AttnDownEncoderBlock2D"], "in_channels": 3, "latent_channels": 4, "layers_per_block": 1, "norm_num_groups": 8, "norm_type": "spatial", "num_vq_embeddings": 12, "out_channels": 3, "up_block_types": [ "AttnUpDecoderBlock2D", "UpDecoderBlock2D", ], "vq_embed_dim": 4, } @property def lowercase_ ( self ): """simple docstring""" torch.manual_seed(0 ) lowerCAmelCase__ : Optional[Any] = VQModel(**self.dummy_movq_kwargs ) return model def lowercase_ ( self ): """simple docstring""" lowerCAmelCase__ : Union[str, Any] = self.dummy_unet lowerCAmelCase__ : Union[str, Any] = self.dummy_movq lowerCAmelCase__ : List[Any] = DDIMScheduler( num_train_timesteps=1000 , beta_schedule='''linear''' , beta_start=0.00_085 , beta_end=0.012 , clip_sample=__SCREAMING_SNAKE_CASE , set_alpha_to_one=__SCREAMING_SNAKE_CASE , steps_offset=1 , prediction_type='''epsilon''' , thresholding=__SCREAMING_SNAKE_CASE , ) lowerCAmelCase__ : Optional[Any] = { """unet""": unet, """scheduler""": scheduler, """movq""": movq, } return components def lowercase_ ( self , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__=0 ): """simple docstring""" lowerCAmelCase__ : Tuple = floats_tensor((1, self.text_embedder_hidden_size) , rng=random.Random(__SCREAMING_SNAKE_CASE ) ).to(__SCREAMING_SNAKE_CASE ) lowerCAmelCase__ : Dict = floats_tensor((1, self.text_embedder_hidden_size) , rng=random.Random(seed + 1 ) ).to( __SCREAMING_SNAKE_CASE ) if str(__SCREAMING_SNAKE_CASE ).startswith('''mps''' ): lowerCAmelCase__ : int = torch.manual_seed(__SCREAMING_SNAKE_CASE ) else: lowerCAmelCase__ : Optional[Any] = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(__SCREAMING_SNAKE_CASE ) lowerCAmelCase__ : Union[str, Any] = { """image_embeds""": image_embeds, """negative_image_embeds""": negative_image_embeds, """generator""": generator, """height""": 64, """width""": 64, """guidance_scale""": 4.0, """num_inference_steps""": 2, """output_type""": """np""", } return inputs def lowercase_ ( self ): """simple docstring""" lowerCAmelCase__ : str = """cpu""" lowerCAmelCase__ : Dict = self.get_dummy_components() lowerCAmelCase__ : int = self.pipeline_class(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase__ : Dict = pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) lowerCAmelCase__ : Tuple = pipe(**self.get_dummy_inputs(__SCREAMING_SNAKE_CASE ) ) lowerCAmelCase__ : int = output.images lowerCAmelCase__ : Dict = pipe( **self.get_dummy_inputs(__SCREAMING_SNAKE_CASE ) , return_dict=__SCREAMING_SNAKE_CASE , )[0] lowerCAmelCase__ : Union[str, Any] = image[0, -3:, -3:, -1] lowerCAmelCase__ : List[Any] = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) lowerCAmelCase__ : Tuple = np.array( [0.6_237_976, 1.0, 0.36_441_332, 1.0, 0.70_639_634, 0.29_877_186, 0.85_652_125, 0.5_216_843, 0.54_454_046] ) 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 lowercase_ ( self ): """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def lowercase_ ( self ): """simple docstring""" lowerCAmelCase__ : int = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/kandinskyv22/kandinskyv22_text2img_cat_fp16.npy''' ) lowerCAmelCase__ : int = KandinskyVaaPriorPipeline.from_pretrained( '''kandinsky-community/kandinsky-2-2-prior''' , torch_dtype=torch.floataa ) pipe_prior.to(__SCREAMING_SNAKE_CASE ) lowerCAmelCase__ : str = KandinskyVaaPipeline.from_pretrained( '''kandinsky-community/kandinsky-2-2-decoder''' , torch_dtype=torch.floataa ) lowerCAmelCase__ : List[str] = pipeline.to(__SCREAMING_SNAKE_CASE ) pipeline.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) lowerCAmelCase__ : int = """red cat, 4k photo""" lowerCAmelCase__ : Dict = torch.Generator(device='''cuda''' ).manual_seed(0 ) lowerCAmelCase__ : Optional[Any] = pipe_prior( __SCREAMING_SNAKE_CASE , generator=__SCREAMING_SNAKE_CASE , num_inference_steps=5 , negative_prompt='''''' , ).to_tuple() lowerCAmelCase__ : Dict = torch.Generator(device='''cuda''' ).manual_seed(0 ) lowerCAmelCase__ : Optional[int] = pipeline( image_embeds=__SCREAMING_SNAKE_CASE , negative_image_embeds=__SCREAMING_SNAKE_CASE , generator=__SCREAMING_SNAKE_CASE , num_inference_steps=100 , output_type='''np''' , ) lowerCAmelCase__ : Optional[int] = output.images[0] assert image.shape == (512, 512, 3) assert_mean_pixel_difference(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE )
233
'''simple docstring''' import shutil import tempfile import unittest from unittest.mock import patch from transformers import ( DefaultFlowCallback, IntervalStrategy, PrinterCallback, ProgressCallback, Trainer, TrainerCallback, TrainingArguments, is_torch_available, ) from transformers.testing_utils import require_torch if is_torch_available(): from transformers.trainer import DEFAULT_CALLBACKS from .test_trainer import RegressionDataset, RegressionModelConfig, RegressionPreTrainedModel class __snake_case ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self ): snake_case__ : str = [] def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): self.events.append("""on_init_end""" ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): self.events.append("""on_train_begin""" ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): self.events.append("""on_train_end""" ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): self.events.append("""on_epoch_begin""" ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): self.events.append("""on_epoch_end""" ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): self.events.append("""on_step_begin""" ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): self.events.append("""on_step_end""" ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): self.events.append("""on_evaluate""" ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): self.events.append("""on_predict""" ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): self.events.append("""on_save""" ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): self.events.append("""on_log""" ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ): self.events.append("""on_prediction_step""" ) @require_torch class __snake_case ( unittest.TestCase ): '''simple docstring''' def __UpperCamelCase ( self ): snake_case__ : Tuple = tempfile.mkdtemp() def __UpperCamelCase ( self ): shutil.rmtree(self.output_dir ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE=0 , __SCREAMING_SNAKE_CASE=0 , __SCREAMING_SNAKE_CASE=6_4 , __SCREAMING_SNAKE_CASE=6_4 , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=False , **__SCREAMING_SNAKE_CASE ): # disable_tqdm in TrainingArguments has a flaky default since it depends on the level of logging. We make sure # its set to False since the tests later on depend on its value. snake_case__ : List[Any] = RegressionDataset(length=__SCREAMING_SNAKE_CASE ) snake_case__ : List[str] = RegressionDataset(length=__SCREAMING_SNAKE_CASE ) snake_case__ : List[str] = RegressionModelConfig(a=__SCREAMING_SNAKE_CASE , b=__SCREAMING_SNAKE_CASE ) snake_case__ : List[str] = RegressionPreTrainedModel(__SCREAMING_SNAKE_CASE ) snake_case__ : Dict = TrainingArguments(self.output_dir , disable_tqdm=__SCREAMING_SNAKE_CASE , report_to=[] , **__SCREAMING_SNAKE_CASE ) return Trainer( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , train_dataset=__SCREAMING_SNAKE_CASE , eval_dataset=__SCREAMING_SNAKE_CASE , callbacks=__SCREAMING_SNAKE_CASE , ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): self.assertEqual(len(__SCREAMING_SNAKE_CASE ) , len(__SCREAMING_SNAKE_CASE ) ) # Order doesn't matter snake_case__ : Tuple = sorted(__SCREAMING_SNAKE_CASE , key=lambda __SCREAMING_SNAKE_CASE : cb.__name__ if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else cb.__class__.__name__ ) snake_case__ : List[str] = sorted(__SCREAMING_SNAKE_CASE , key=lambda __SCREAMING_SNAKE_CASE : cb.__name__ if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else cb.__class__.__name__ ) for cba, cba in zip(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) and isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) elif isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) and not isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): self.assertEqual(__SCREAMING_SNAKE_CASE , cba.__class__ ) elif not isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) and isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): self.assertEqual(cba.__class__ , __SCREAMING_SNAKE_CASE ) else: self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE ): snake_case__ : Tuple = ["""on_init_end""", """on_train_begin"""] snake_case__ : Union[str, Any] = 0 snake_case__ : Dict = len(trainer.get_eval_dataloader() ) snake_case__ : Any = ["""on_prediction_step"""] * len(trainer.get_eval_dataloader() ) + ["""on_log""", """on_evaluate"""] for _ in range(trainer.state.num_train_epochs ): expected_events.append("""on_epoch_begin""" ) for _ in range(__SCREAMING_SNAKE_CASE ): step += 1 expected_events += ["on_step_begin", "on_step_end"] if step % trainer.args.logging_steps == 0: expected_events.append("""on_log""" ) if trainer.args.evaluation_strategy == IntervalStrategy.STEPS and step % trainer.args.eval_steps == 0: expected_events += evaluation_events.copy() if step % trainer.args.save_steps == 0: expected_events.append("""on_save""" ) expected_events.append("""on_epoch_end""" ) if trainer.args.evaluation_strategy == IntervalStrategy.EPOCH: expected_events += evaluation_events.copy() expected_events += ["on_log", "on_train_end"] return expected_events def __UpperCamelCase ( self ): snake_case__ : Any = self.get_trainer() snake_case__ : str = DEFAULT_CALLBACKS.copy() + [ProgressCallback] self.check_callbacks_equality(trainer.callback_handler.callbacks , __SCREAMING_SNAKE_CASE ) # Callbacks passed at init are added to the default callbacks snake_case__ : List[str] = self.get_trainer(callbacks=[MyTestTrainerCallback] ) expected_callbacks.append(__SCREAMING_SNAKE_CASE ) self.check_callbacks_equality(trainer.callback_handler.callbacks , __SCREAMING_SNAKE_CASE ) # TrainingArguments.disable_tqdm controls if use ProgressCallback or PrinterCallback snake_case__ : Optional[Any] = self.get_trainer(disable_tqdm=__SCREAMING_SNAKE_CASE ) snake_case__ : Tuple = DEFAULT_CALLBACKS.copy() + [PrinterCallback] self.check_callbacks_equality(trainer.callback_handler.callbacks , __SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self ): snake_case__ : str = DEFAULT_CALLBACKS.copy() + [ProgressCallback] snake_case__ : int = self.get_trainer() # We can add, pop, or remove by class name trainer.remove_callback(__SCREAMING_SNAKE_CASE ) expected_callbacks.remove(__SCREAMING_SNAKE_CASE ) self.check_callbacks_equality(trainer.callback_handler.callbacks , __SCREAMING_SNAKE_CASE ) snake_case__ : Union[str, Any] = self.get_trainer() snake_case__ : List[str] = trainer.pop_callback(__SCREAMING_SNAKE_CASE ) self.assertEqual(cb.__class__ , __SCREAMING_SNAKE_CASE ) self.check_callbacks_equality(trainer.callback_handler.callbacks , __SCREAMING_SNAKE_CASE ) trainer.add_callback(__SCREAMING_SNAKE_CASE ) expected_callbacks.insert(0 , __SCREAMING_SNAKE_CASE ) self.check_callbacks_equality(trainer.callback_handler.callbacks , __SCREAMING_SNAKE_CASE ) # We can also add, pop, or remove by instance snake_case__ : List[Any] = self.get_trainer() snake_case__ : List[str] = trainer.callback_handler.callbacks[0] trainer.remove_callback(__SCREAMING_SNAKE_CASE ) expected_callbacks.remove(__SCREAMING_SNAKE_CASE ) self.check_callbacks_equality(trainer.callback_handler.callbacks , __SCREAMING_SNAKE_CASE ) snake_case__ : Optional[int] = self.get_trainer() snake_case__ : Any = trainer.callback_handler.callbacks[0] snake_case__ : Optional[Any] = trainer.pop_callback(__SCREAMING_SNAKE_CASE ) self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) self.check_callbacks_equality(trainer.callback_handler.callbacks , __SCREAMING_SNAKE_CASE ) trainer.add_callback(__SCREAMING_SNAKE_CASE ) expected_callbacks.insert(0 , __SCREAMING_SNAKE_CASE ) self.check_callbacks_equality(trainer.callback_handler.callbacks , __SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self ): import warnings # XXX: for now ignore scatter_gather warnings in this test since it's not relevant to what's being tested warnings.simplefilter(action="""ignore""" , category=__SCREAMING_SNAKE_CASE ) snake_case__ : Any = self.get_trainer(callbacks=[MyTestTrainerCallback] ) trainer.train() snake_case__ : Any = trainer.callback_handler.callbacks[-2].events self.assertEqual(__SCREAMING_SNAKE_CASE , self.get_expected_events(__SCREAMING_SNAKE_CASE ) ) # Independent log/save/eval snake_case__ : Dict = self.get_trainer(callbacks=[MyTestTrainerCallback] , logging_steps=5 ) trainer.train() snake_case__ : int = trainer.callback_handler.callbacks[-2].events self.assertEqual(__SCREAMING_SNAKE_CASE , self.get_expected_events(__SCREAMING_SNAKE_CASE ) ) snake_case__ : Any = self.get_trainer(callbacks=[MyTestTrainerCallback] , save_steps=5 ) trainer.train() snake_case__ : Any = trainer.callback_handler.callbacks[-2].events self.assertEqual(__SCREAMING_SNAKE_CASE , self.get_expected_events(__SCREAMING_SNAKE_CASE ) ) snake_case__ : Tuple = self.get_trainer(callbacks=[MyTestTrainerCallback] , eval_steps=5 , evaluation_strategy="""steps""" ) trainer.train() snake_case__ : str = trainer.callback_handler.callbacks[-2].events self.assertEqual(__SCREAMING_SNAKE_CASE , self.get_expected_events(__SCREAMING_SNAKE_CASE ) ) snake_case__ : Tuple = self.get_trainer(callbacks=[MyTestTrainerCallback] , evaluation_strategy="""epoch""" ) trainer.train() snake_case__ : Any = trainer.callback_handler.callbacks[-2].events self.assertEqual(__SCREAMING_SNAKE_CASE , self.get_expected_events(__SCREAMING_SNAKE_CASE ) ) # A bit of everything snake_case__ : Dict = self.get_trainer( callbacks=[MyTestTrainerCallback] , logging_steps=3 , save_steps=1_0 , eval_steps=5 , evaluation_strategy="""steps""" , ) trainer.train() snake_case__ : Tuple = trainer.callback_handler.callbacks[-2].events self.assertEqual(__SCREAMING_SNAKE_CASE , self.get_expected_events(__SCREAMING_SNAKE_CASE ) ) # warning should be emitted for duplicated callbacks with patch("""transformers.trainer_callback.logger.warning""" ) as warn_mock: snake_case__ : List[str] = self.get_trainer( callbacks=[MyTestTrainerCallback, MyTestTrainerCallback] , ) assert str(__SCREAMING_SNAKE_CASE ) in warn_mock.call_args[0][0]
38
0
import os from math import logaa def UpperCAmelCase__ ( lowerCamelCase_ : str = "base_exp.txt" ): __a : float = 0 __a : Union[str, Any] = 0 for i, line in enumerate(open(os.path.join(os.path.dirname(lowerCamelCase_ ) , lowerCamelCase_ ) ) ): __a : Tuple = list(map(lowerCamelCase_ , line.split(',' ) ) ) if x * logaa(lowerCamelCase_ ) > largest: __a : Optional[Any] = x * logaa(lowerCamelCase_ ) __a : int = i + 1 return result if __name__ == "__main__": print(solution())
47
'''simple docstring''' 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 __snake_case ( unittest.TestCase ): '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=7 , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=1_8 , __SCREAMING_SNAKE_CASE=3_0 , __SCREAMING_SNAKE_CASE=4_0_0 , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=[0.5, 0.5, 0.5] , __SCREAMING_SNAKE_CASE=[0.5, 0.5, 0.5] , ): snake_case__ : Any = size if size is not None else {"""height""": 1_8, """width""": 1_8} snake_case__ : List[Any] = parent snake_case__ : int = batch_size snake_case__ : List[Any] = num_channels snake_case__ : str = image_size snake_case__ : Union[str, Any] = min_resolution snake_case__ : List[Any] = max_resolution snake_case__ : Tuple = do_resize snake_case__ : int = size snake_case__ : Tuple = do_normalize snake_case__ : Dict = image_mean snake_case__ : Union[str, Any] = image_std def __UpperCamelCase ( self ): 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 __snake_case ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowerCamelCase__ = DPTImageProcessor if is_vision_available() else None def __UpperCamelCase ( self ): snake_case__ : str = DPTImageProcessingTester(self ) @property def __UpperCamelCase ( self ): return self.image_processor_tester.prepare_image_processor_dict() def __UpperCamelCase ( self ): snake_case__ : Optional[int] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , """image_mean""" ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , """image_std""" ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , """do_normalize""" ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , """do_resize""" ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , """size""" ) ) def __UpperCamelCase ( self ): snake_case__ : Any = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"""height""": 1_8, """width""": 1_8} ) snake_case__ : List[Any] = self.image_processing_class.from_dict(self.image_processor_dict , size=4_2 ) self.assertEqual(image_processor.size , {"""height""": 4_2, """width""": 4_2} ) def __UpperCamelCase ( self ): # Initialize image_processing snake_case__ : Optional[int] = self.image_processing_class(**self.image_processor_dict ) # create random PIL images snake_case__ : List[Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=__SCREAMING_SNAKE_CASE ) for image in image_inputs: self.assertIsInstance(__SCREAMING_SNAKE_CASE , Image.Image ) # Test not batched input snake_case__ : Optional[int] = 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 snake_case__ : List[str] = image_processing(__SCREAMING_SNAKE_CASE , 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 __UpperCamelCase ( self ): # Initialize image_processing snake_case__ : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors snake_case__ : Optional[int] = prepare_image_inputs(self.image_processor_tester , equal_resolution=__SCREAMING_SNAKE_CASE , numpify=__SCREAMING_SNAKE_CASE ) for image in image_inputs: self.assertIsInstance(__SCREAMING_SNAKE_CASE , np.ndarray ) # Test not batched input snake_case__ : List[str] = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.size["""height"""], self.image_processor_tester.size["""width"""], ) , ) # Test batched snake_case__ : Any = image_processing(__SCREAMING_SNAKE_CASE , 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 __UpperCamelCase ( self ): # Initialize image_processing snake_case__ : List[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors snake_case__ : List[str] = prepare_image_inputs(self.image_processor_tester , equal_resolution=__SCREAMING_SNAKE_CASE , torchify=__SCREAMING_SNAKE_CASE ) for image in image_inputs: self.assertIsInstance(__SCREAMING_SNAKE_CASE , torch.Tensor ) # Test not batched input snake_case__ : List[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 snake_case__ : List[str] = image_processing(__SCREAMING_SNAKE_CASE , 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"""], ) , )
38
0
'''simple docstring''' import re import tempfile from pathlib import Path import pytest import yaml from datasets.utils.readme import ReadMe # @pytest.fixture # def example_yaml_structure(): _a : List[str] = yaml.safe_load( "\\nname: \"\"\nallow_empty: false\nallow_empty_text: true\nsubsections:\n - name: \"Dataset Card for X\" # First-level markdown heading\n allow_empty: false\n allow_empty_text: true\n subsections:\n - name: \"Table of Contents\"\n allow_empty: false\n allow_empty_text: false\n subsections: null\n - name: \"Dataset Description\"\n allow_empty: false\n allow_empty_text: false\n subsections:\n - name: \"Dataset Summary\"\n allow_empty: false\n allow_empty_text: false\n subsections: null\n - name: \"Supported Tasks and Leaderboards\"\n allow_empty: true\n allow_empty_text: true\n subsections: null\n - name: Languages\n allow_empty: false\n allow_empty_text: true\n subsections: null\n" ) _a : List[str] = { "name": "root", "text": "", "is_empty_text": True, "subsections": [ { "name": "Dataset Card for My Dataset", "text": "", "is_empty_text": True, "subsections": [ {"name": "Table of Contents", "text": "Some text here.", "is_empty_text": False, "subsections": []}, { "name": "Dataset Description", "text": "Some text here.", "is_empty_text": False, "subsections": [ { "name": "Dataset Summary", "text": "Some text here.", "is_empty_text": False, "subsections": [], }, { "name": "Supported Tasks and Leaderboards", "text": "", "is_empty_text": True, "subsections": [], }, {"name": "Languages", "text": "Language Text", "is_empty_text": False, "subsections": []}, ], }, ], } ], } _a : Any = "\\n---\nlanguage:\n- zh\n- en\n---\n\n# Dataset Card for My Dataset\n## Table of Contents\nSome text here.\n## Dataset Description\nSome text here.\n### Dataset Summary\nSome text here.\n### Supported Tasks and Leaderboards\n### Languages\nLanguage Text\n" _a : str = "\\n---\nlanguage:\n- zh\n- en\n---\n\n# Dataset Card for My Dataset\n## Table of Contents\nSome text here.\n## Dataset Description\nSome text here.\n### Dataset Summary\nSome text here.\n#### Extra Ignored Subsection\n### Supported Tasks and Leaderboards\n### Languages\nLanguage Text\n" _a : Optional[Any] = { "name": "root", "text": "", "is_empty_text": True, "subsections": [ { "name": "Dataset Card for My Dataset", "text": "", "is_empty_text": True, "subsections": [ {"name": "Table of Contents", "text": "Some text here.", "is_empty_text": False, "subsections": []}, { "name": "Dataset Description", "text": "Some text here.", "is_empty_text": False, "subsections": [ { "name": "Dataset Summary", "text": "Some text here.", "is_empty_text": False, "subsections": [ { "name": "Extra Ignored Subsection", "text": "", "is_empty_text": True, "subsections": [], } ], }, { "name": "Supported Tasks and Leaderboards", "text": "", "is_empty_text": True, "subsections": [], }, {"name": "Languages", "text": "Language Text", "is_empty_text": False, "subsections": []}, ], }, ], } ], } _a : int = "\\n---\n---\n# Dataset Card for My Dataset\n## Table of Contents\nSome text here.\n## Dataset Description\nSome text here.\n### Dataset Summary\nSome text here.\n### Supported Tasks and Leaderboards\n### Languages\nLanguage Text\n" _a : List[str] = ( "The following issues were found for the README at `{path}`:\n-\tEmpty YAML markers are present in the README." ) _a : Union[str, Any] = "\\n# Dataset Card for My Dataset\n## Table of Contents\nSome text here.\n## Dataset Description\nSome text here.\n### Dataset Summary\nSome text here.\n### Supported Tasks and Leaderboards\n### Languages\nLanguage Text\n" _a : int = ( "The following issues were found for the README at `{path}`:\n-\tNo YAML markers are present in the README." ) _a : Optional[int] = "\\n---\n# Dataset Card for My Dataset\n## Table of Contents\nSome text here.\n## Dataset Description\nSome text here.\n### Dataset Summary\nSome text here.\n### Supported Tasks and Leaderboards\n### Languages\nLanguage Text\n" _a : Any = "The following issues were found for the README at `{path}`:\n-\tOnly the start of YAML tags present in the README." _a : Tuple = "\\n---\nlanguage:\n- zh\n- en\n---\n\n# Dataset Card for My Dataset\n## Table of Contents\nSome text here.\n## Dataset Description\nSome text here.\n### Dataset Summary\n### Supported Tasks and Leaderboards\n### Languages\nLanguage Text\n" _a : Any = "The following issues were found for the README at `{path}`:\n-\tExpected some content in section `Dataset Summary` but it is empty.\n-\tExpected some text in section `Dataset Summary` but it is empty (text in subsections are ignored)." _a : Optional[int] = "\\n---\nlanguage:\n- zh\n- en\n---\n\n# Dataset Card for My Dataset\n" _a : Any = "The following issues were found for the README at `{path}`:\n-\tExpected some content in section `Dataset Card for My Dataset` but it is empty.\n-\tSection `Dataset Card for My Dataset` expected the following subsections: `Table of Contents`, `Dataset Description`. Found 'None'." _a : Union[str, Any] = "\\n---\nlanguage:\n- zh\n- en\n---\n\n# Dataset Card for My Dataset\n## Table of Contents\nSome text here.\n## Dataset Description\nSome text here.\n### Dataset Summary\nSome text here.\n### Languages\nLanguage Text\n" _a : List[Any] = "The following issues were found for the README at `{path}`:\n-\tSection `Dataset Description` is missing subsection: `Supported Tasks and Leaderboards`." _a : Tuple = "\\n---\nlanguage:\n- zh\n- en\n---\n\n# Dataset Card for My Dataset\n## Table of Contents\nSome text here.\n## Dataset Description\nSome text here.\n### Dataset Summary\nSome text here.\n### Supported Tasks and Leaderboards\n### Languages\n" _a : str = "The following issues were found for the README at `{path}`:\n-\tExpected some content in section `Languages` but it is empty." _a : List[str] = "\\n---\nlanguage:\n- zh\n- en\n---\n\n## Table of Contents\nSome text here.\n## Dataset Description\nSome text here.\n### Dataset Summary\nSome text here.\n### Supported Tasks and Leaderboards\n### Languages\nLanguage Text\n" _a : Dict = "The following issues were found for the README at `{path}`:\n-\tThe README has no first-level headings. One heading is expected. Skipping further validation for this README." _a : Any = "\\n---\nlanguage:\n- zh\n- en\n---\n\n# Dataset Card for My Dataset\n## Table of Contents\nSome text here.\n## Dataset Description\nSome text here.\n### Dataset Summary\nSome text here.\n### Supported Tasks and Leaderboards\n### Languages\nLanguage Text\n# Dataset Card My Dataset\n" _a : str = "The following issues were found for the README at `{path}`:\n-\tThe README has several first-level headings: `Dataset Card for My Dataset`, `Dataset Card My Dataset`. Only one heading is expected. Skipping further validation for this README." _a : Dict = "\\n---\nlanguage:\n- zh\n- en\n---\n\n# Dataset Card My Dataset\n## Table of Contents\nSome text here.\n## Dataset Description\nSome text here.\n### Dataset Summary\nSome text here.\n### Supported Tasks and Leaderboards\n### Languages\nLanguage Text\n" _a : List[str] = "The following issues were found for the README at `{path}`:\n-\tNo first-level heading starting with `Dataset Card for` found in README. Skipping further validation for this README." _a : List[str] = "" _a : Union[str, Any] = "The following issues were found for the README at `{path}`:\n-\tThe README has no first-level headings. One heading is expected. Skipping further validation for this README.\n-\tNo YAML markers are present in the README." _a : Tuple = "\\n---\nlanguage:\n- zh\n- en\n---\n\n# Dataset Card for My Dataset\n# Dataset Card for My Dataset\n## Table of Contents\nSome text here.\n## Dataset Description\nSome text here.\n### Dataset Summary\nSome text here.\n### Supported Tasks and Leaderboards\n### Languages\nLanguage Text\n" _a : Tuple = "The following issues were found while parsing the README at `{path}`:\n-\tMultiple sections with the same heading `Dataset Card for My Dataset` have been found. Please keep only one of these sections." @pytest.mark.parametrize( 'readme_md, expected_dict' , [ (README_CORRECT, CORRECT_DICT), (README_CORRECT_FOUR_LEVEL, CORRECT_DICT_FOUR_LEVEL), ] , ) def _a (lowercase__ : Tuple , lowercase__ : Optional[int] ) -> Optional[Any]: """simple docstring""" assert ReadMe.from_string(lowercase__ , lowercase__ ).to_dict() == expected_dict @pytest.mark.parametrize( 'readme_md, expected_error' , [ (README_NO_YAML, EXPECTED_ERROR_README_NO_YAML), (README_EMPTY_YAML, EXPECTED_ERROR_README_EMPTY_YAML), (README_INCORRECT_YAML, EXPECTED_ERROR_README_INCORRECT_YAML), (README_EMPTY, EXPECTED_ERROR_README_EMPTY), (README_NONE_SUBSECTION, EXPECTED_ERROR_README_NONE_SUBSECTION), (README_MISSING_FIRST_LEVEL, EXPECTED_ERROR_README_MISSING_FIRST_LEVEL), (README_MISSING_SUBSECTION, EXPECTED_ERROR_README_MISSING_SUBSECTION), (README_MISSING_TEXT, EXPECTED_ERROR_README_MISSING_TEXT), (README_WRONG_FIRST_LEVEL, EXPECTED_ERROR_README_WRONG_FIRST_LEVEL), (README_MULTIPLE_WRONG_FIRST_LEVEL, EXPECTED_ERROR_README_MULTIPLE_WRONG_FIRST_LEVEL), (README_MISSING_CONTENT, EXPECTED_ERROR_README_MISSING_CONTENT), ] , ) def _a (lowercase__ : int , lowercase__ : List[Any] ) -> Any: """simple docstring""" with pytest.raises(lowercase__ , match=re.escape(expected_error.format(path='root' ) ) ): __snake_case = ReadMe.from_string(lowercase__ , lowercase__ ) readme.validate() @pytest.mark.parametrize( 'readme_md, expected_error' , [ (README_MULTIPLE_SAME_HEADING_1, EXPECTED_ERROR_README_MULTIPLE_SAME_HEADING_1), ] , ) def _a (lowercase__ : Union[str, Any] , lowercase__ : Optional[Any] ) -> Optional[int]: """simple docstring""" with pytest.raises(lowercase__ , match=re.escape(expected_error.format(path='root' ) ) ): ReadMe.from_string(lowercase__ , lowercase__ ) @pytest.mark.parametrize( 'readme_md,' , [ (README_MULTIPLE_SAME_HEADING_1), ] , ) def _a (lowercase__ : Optional[Any] ) -> str: """simple docstring""" ReadMe.from_string(lowercase__ , lowercase__ , suppress_parsing_errors=lowercase__ ) @pytest.mark.parametrize( 'readme_md, expected_dict' , [ (README_CORRECT, CORRECT_DICT), (README_CORRECT_FOUR_LEVEL, CORRECT_DICT_FOUR_LEVEL), ] , ) def _a (lowercase__ : Optional[Any] , lowercase__ : Optional[Any] ) -> int: """simple docstring""" with tempfile.TemporaryDirectory() as tmp_dir: __snake_case = Path(lowercase__ ) / """README.md""" with open(lowercase__ , 'w+' ) as readme_file: readme_file.write(lowercase__ ) __snake_case = ReadMe.from_readme(lowercase__ , lowercase__ ).to_dict() assert out["name"] == path assert out["text"] == "" assert out["is_empty_text"] assert out["subsections"] == expected_dict["subsections"] @pytest.mark.parametrize( 'readme_md, expected_error' , [ (README_NO_YAML, EXPECTED_ERROR_README_NO_YAML), (README_EMPTY_YAML, EXPECTED_ERROR_README_EMPTY_YAML), (README_INCORRECT_YAML, EXPECTED_ERROR_README_INCORRECT_YAML), (README_EMPTY, EXPECTED_ERROR_README_EMPTY), (README_NONE_SUBSECTION, EXPECTED_ERROR_README_NONE_SUBSECTION), (README_MISSING_FIRST_LEVEL, EXPECTED_ERROR_README_MISSING_FIRST_LEVEL), (README_MISSING_SUBSECTION, EXPECTED_ERROR_README_MISSING_SUBSECTION), (README_MISSING_TEXT, EXPECTED_ERROR_README_MISSING_TEXT), (README_WRONG_FIRST_LEVEL, EXPECTED_ERROR_README_WRONG_FIRST_LEVEL), (README_MULTIPLE_WRONG_FIRST_LEVEL, EXPECTED_ERROR_README_MULTIPLE_WRONG_FIRST_LEVEL), (README_MISSING_CONTENT, EXPECTED_ERROR_README_MISSING_CONTENT), ] , ) def _a (lowercase__ : Tuple , lowercase__ : Any ) -> Tuple: """simple docstring""" with tempfile.TemporaryDirectory() as tmp_dir: __snake_case = Path(lowercase__ ) / """README.md""" with open(lowercase__ , 'w+' ) as readme_file: readme_file.write(lowercase__ ) __snake_case = expected_error.format(path=lowercase__ ) with pytest.raises(lowercase__ , match=re.escape(lowercase__ ) ): __snake_case = ReadMe.from_readme(lowercase__ , lowercase__ ) readme.validate() @pytest.mark.parametrize( 'readme_md, expected_error' , [ (README_MULTIPLE_SAME_HEADING_1, EXPECTED_ERROR_README_MULTIPLE_SAME_HEADING_1), ] , ) def _a (lowercase__ : Tuple , lowercase__ : Tuple ) -> int: """simple docstring""" with tempfile.TemporaryDirectory() as tmp_dir: __snake_case = Path(lowercase__ ) / """README.md""" with open(lowercase__ , 'w+' ) as readme_file: readme_file.write(lowercase__ ) __snake_case = expected_error.format(path=lowercase__ ) with pytest.raises(lowercase__ , match=re.escape(lowercase__ ) ): ReadMe.from_readme(lowercase__ , lowercase__ ) @pytest.mark.parametrize( 'readme_md,' , [ (README_MULTIPLE_SAME_HEADING_1), ] , ) def _a (lowercase__ : List[str] ) -> List[Any]: """simple docstring""" with tempfile.TemporaryDirectory() as tmp_dir: __snake_case = Path(lowercase__ ) / """README.md""" with open(lowercase__ , 'w+' ) as readme_file: readme_file.write(lowercase__ ) ReadMe.from_readme(lowercase__ , lowercase__ , suppress_parsing_errors=lowercase__ )
56
'''simple docstring''' from __future__ import annotations import inspect import unittest from math import floor import numpy as np from transformers import CvtConfig from transformers.testing_utils import require_tf, require_vision, slow from transformers.utils import cached_property, is_tf_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFCvtForImageClassification, TFCvtModel from transformers.models.cvt.modeling_tf_cvt import TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class __snake_case ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' def __UpperCamelCase ( self ): snake_case__ : Dict = self.config_class(**self.inputs_dict ) self.parent.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , """embed_dim""" ) ) self.parent.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , """num_heads""" ) ) class __snake_case : '''simple docstring''' def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=1_3 , __SCREAMING_SNAKE_CASE=6_4 , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=[1_6, 4_8, 9_6] , __SCREAMING_SNAKE_CASE=[1, 3, 6] , __SCREAMING_SNAKE_CASE=[1, 2, 1_0] , __SCREAMING_SNAKE_CASE=[7, 3, 3] , __SCREAMING_SNAKE_CASE=[4, 2, 2] , __SCREAMING_SNAKE_CASE=[2, 1, 1] , __SCREAMING_SNAKE_CASE=[2, 2, 2] , __SCREAMING_SNAKE_CASE=[False, False, True] , __SCREAMING_SNAKE_CASE=[0.0, 0.0, 0.0] , __SCREAMING_SNAKE_CASE=0.02 , __SCREAMING_SNAKE_CASE=1e-1_2 , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=2 , ): snake_case__ : List[str] = parent snake_case__ : Tuple = batch_size snake_case__ : Union[str, Any] = image_size snake_case__ : List[Any] = patch_sizes snake_case__ : Optional[int] = patch_stride snake_case__ : Optional[Any] = patch_padding snake_case__ : Any = is_training snake_case__ : int = use_labels snake_case__ : Dict = num_labels snake_case__ : Optional[Any] = num_channels snake_case__ : Optional[Any] = embed_dim snake_case__ : Optional[int] = num_heads snake_case__ : Optional[int] = stride_kv snake_case__ : int = depth snake_case__ : Optional[Any] = cls_token snake_case__ : List[Any] = attention_drop_rate snake_case__ : Union[str, Any] = initializer_range snake_case__ : List[Any] = layer_norm_eps def __UpperCamelCase ( self ): snake_case__ : Optional[int] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) snake_case__ : List[Any] = None if self.use_labels: # create a random int32 tensor of given shape snake_case__ : List[str] = ids_tensor([self.batch_size] , self.num_labels ) snake_case__ : List[str] = self.get_config() return config, pixel_values, labels def __UpperCamelCase ( self ): return CvtConfig( image_size=self.image_size , num_labels=self.num_labels , num_channels=self.num_channels , embed_dim=self.embed_dim , num_heads=self.num_heads , patch_sizes=self.patch_sizes , patch_padding=self.patch_padding , patch_stride=self.patch_stride , stride_kv=self.stride_kv , depth=self.depth , cls_token=self.cls_token , attention_drop_rate=self.attention_drop_rate , initializer_range=self.initializer_range , ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): snake_case__ : int = TFCvtModel(config=__SCREAMING_SNAKE_CASE ) snake_case__ : List[Any] = model(__SCREAMING_SNAKE_CASE , training=__SCREAMING_SNAKE_CASE ) snake_case__ : Tuple = (self.image_size, self.image_size) snake_case__ , snake_case__ : str = image_size[0], image_size[1] for i in range(len(self.depth ) ): snake_case__ : Any = floor(((height + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1 ) snake_case__ : Optional[int] = floor(((width + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1 ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.embed_dim[-1], height, width) ) def __UpperCamelCase ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): snake_case__ : Any = self.num_labels snake_case__ : str = TFCvtForImageClassification(__SCREAMING_SNAKE_CASE ) snake_case__ : List[str] = model(__SCREAMING_SNAKE_CASE , labels=__SCREAMING_SNAKE_CASE , training=__SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __UpperCamelCase ( self ): snake_case__ : List[Any] = self.prepare_config_and_inputs() snake_case__ , snake_case__ , snake_case__ : Any = config_and_inputs snake_case__ : Union[str, Any] = {"""pixel_values""": pixel_values} return config, inputs_dict @require_tf class __snake_case ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowerCamelCase__ = (TFCvtModel, TFCvtForImageClassification) if is_tf_available() else () lowerCamelCase__ = ( {'''feature-extraction''': TFCvtModel, '''image-classification''': TFCvtForImageClassification} if is_tf_available() else {} ) lowerCamelCase__ = False lowerCamelCase__ = False lowerCamelCase__ = False lowerCamelCase__ = False lowerCamelCase__ = False def __UpperCamelCase ( self ): snake_case__ : Optional[Any] = TFCvtModelTester(self ) snake_case__ : Any = TFCvtConfigTester(self , config_class=__SCREAMING_SNAKE_CASE , has_text_modality=__SCREAMING_SNAKE_CASE , hidden_size=3_7 ) def __UpperCamelCase ( self ): self.config_tester.create_and_test_config_common_properties() self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() @unittest.skip(reason="""Cvt does not output attentions""" ) def __UpperCamelCase ( self ): pass @unittest.skip(reason="""Cvt does not use inputs_embeds""" ) def __UpperCamelCase ( self ): pass @unittest.skip(reason="""Cvt does not support input and output embeddings""" ) def __UpperCamelCase ( self ): pass @unittest.skipIf( not is_tf_available() or len(tf.config.list_physical_devices("""GPU""" ) ) == 0 , reason="""TF does not support backprop for grouped convolutions on CPU.""" , ) def __UpperCamelCase ( self ): super().test_dataset_conversion() @unittest.skipIf( not is_tf_available() or len(tf.config.list_physical_devices("""GPU""" ) ) == 0 , reason="""TF does not support backprop for grouped convolutions on CPU.""" , ) @slow def __UpperCamelCase ( self ): super().test_keras_fit() @unittest.skip(reason="""Get `Failed to determine best cudnn convolution algo.` error after using TF 2.12+cuda 11.8""" ) def __UpperCamelCase ( self ): snake_case__ : List[str] = tf.keras.mixed_precision.Policy("""mixed_float16""" ) tf.keras.mixed_precision.set_global_policy(__SCREAMING_SNAKE_CASE ) super().test_keras_fit() tf.keras.mixed_precision.set_global_policy("""float32""" ) def __UpperCamelCase ( self ): snake_case__ , snake_case__ : List[str] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: snake_case__ : Any = model_class(__SCREAMING_SNAKE_CASE ) snake_case__ : str = inspect.signature(model.call ) # signature.parameters is an OrderedDict => so arg_names order is deterministic snake_case__ : Optional[Any] = [*signature.parameters.keys()] snake_case__ : Tuple = ["""pixel_values"""] self.assertListEqual(arg_names[:1] , __SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self ): def check_hidden_states_output(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): snake_case__ : str = model_class(__SCREAMING_SNAKE_CASE ) snake_case__ : List[Any] = model(**self._prepare_for_class(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ) snake_case__ : Optional[int] = outputs.hidden_states snake_case__ : Tuple = len(self.model_tester.depth ) self.assertEqual(len(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) # verify the first hidden states (first block) self.assertListEqual( list(hidden_states[0].shape[-3:] ) , [ self.model_tester.embed_dim[0], self.model_tester.image_size // 4, self.model_tester.image_size // 4, ] , ) snake_case__ , snake_case__ : str = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: snake_case__ : List[Any] = True check_hidden_states_output(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] snake_case__ : List[str] = True check_hidden_states_output(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self ): snake_case__ : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE ) def __UpperCamelCase ( self ): snake_case__ : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*__SCREAMING_SNAKE_CASE ) @slow def __UpperCamelCase ( self ): for model_name in TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: snake_case__ : str = TFCvtModel.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsNotNone(__SCREAMING_SNAKE_CASE ) def UpperCamelCase__ ( ) -> str: '''simple docstring''' snake_case__ : Union[str, Any] = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) return image @require_tf @require_vision class __snake_case ( unittest.TestCase ): '''simple docstring''' @cached_property def __UpperCamelCase ( self ): return AutoImageProcessor.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) @slow def __UpperCamelCase ( self ): snake_case__ : Optional[Any] = TFCvtForImageClassification.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) snake_case__ : Union[str, Any] = self.default_image_processor snake_case__ : int = prepare_img() snake_case__ : Dict = image_processor(images=__SCREAMING_SNAKE_CASE , return_tensors="""tf""" ) # forward pass snake_case__ : Optional[int] = model(**__SCREAMING_SNAKE_CASE ) # verify the logits snake_case__ : str = tf.TensorShape((1, 1_0_0_0) ) self.assertEqual(outputs.logits.shape , __SCREAMING_SNAKE_CASE ) snake_case__ : int = tf.constant([0.9285, 0.9015, -0.3150] ) self.assertTrue(np.allclose(outputs.logits[0, :3].numpy() , __SCREAMING_SNAKE_CASE , atol=1e-4 ) )
38
0
from __future__ import annotations class _a : """simple docstring""" def __init__( self , _UpperCAmelCase ) -> List[Any]: UpperCamelCase_ = order # a_{0} ... a_{k} UpperCamelCase_ = [1.0] + [0.0] * order # b_{0} ... b_{k} UpperCamelCase_ = [1.0] + [0.0] * order # x[n-1] ... x[n-k] UpperCamelCase_ = [0.0] * self.order # y[n-1] ... y[n-k] UpperCamelCase_ = [0.0] * self.order def _UpperCAmelCase ( self , _UpperCAmelCase , _UpperCAmelCase ) -> Optional[int]: if len(__SCREAMING_SNAKE_CASE ) < self.order: UpperCamelCase_ = [1.0, *a_coeffs] if len(__SCREAMING_SNAKE_CASE ) != self.order + 1: UpperCamelCase_ = ( f"""Expected a_coeffs to have {self.order + 1} elements """ f"""for {self.order}-order filter, got {len(__SCREAMING_SNAKE_CASE )}""" ) raise ValueError(__SCREAMING_SNAKE_CASE ) if len(__SCREAMING_SNAKE_CASE ) != self.order + 1: UpperCamelCase_ = ( f"""Expected b_coeffs to have {self.order + 1} elements """ f"""for {self.order}-order filter, got {len(__SCREAMING_SNAKE_CASE )}""" ) raise ValueError(__SCREAMING_SNAKE_CASE ) UpperCamelCase_ = a_coeffs UpperCamelCase_ = b_coeffs def _UpperCAmelCase ( self , _UpperCAmelCase ) -> Optional[Any]: UpperCamelCase_ = 0.0 # Start at index 1 and do index 0 at the end. for i in range(1 , self.order + 1 ): result += ( self.b_coeffs[i] * self.input_history[i - 1] - self.a_coeffs[i] * self.output_history[i - 1] ) UpperCamelCase_ = (result + self.b_coeffs[0] * sample) / self.a_coeffs[0] UpperCamelCase_ = self.input_history[:-1] UpperCamelCase_ = self.output_history[:-1] UpperCamelCase_ = sample UpperCamelCase_ = result return result
23
'''simple docstring''' import unittest from transformers import is_torch_available from transformers.testing_utils import require_torch if is_torch_available(): import torch from transformers.generation import DisjunctiveConstraint @require_torch class __snake_case ( unittest.TestCase ): '''simple docstring''' def __UpperCamelCase ( self ): # For consistency across different places the DisjunctiveConstraint is called, # dc.token_ids is a list of integers. It is also initialized only by integers. snake_case__ : int = [[1, 2, 4], [1, 2, 3, 4]] snake_case__ : Any = DisjunctiveConstraint(__SCREAMING_SNAKE_CASE ) self.assertTrue(isinstance(dc.token_ids , __SCREAMING_SNAKE_CASE ) ) with self.assertRaises(__SCREAMING_SNAKE_CASE ): DisjunctiveConstraint(torch.LongTensor([[1, 2, 4], [1, 2, 3]] ) ) with self.assertRaises(__SCREAMING_SNAKE_CASE ): DisjunctiveConstraint([torch.LongTensor([1, 2, 4] ), torch.LongTensor([1, 2, 3, 4, 5] )] ) def __UpperCamelCase ( self ): # We can't have constraints that are complete subsets of another. This leads to a preverse # interpretation of "constraint fulfillment": does generating [1,2,3] fulfill the constraint? # It would mean that it generated [1,2] which fulfills it, but it's in the middle of potentially # fulfilling [1,2,3,4]. If we believe that [1,2,3] does fulfill the constraint, then the algorithm # will necessarily never reach [1,2,3,4], giving users a false sense of control (better to just not allow it). snake_case__ : Union[str, Any] = [[1, 2], [1, 2, 3, 4]] with self.assertRaises(__SCREAMING_SNAKE_CASE ): DisjunctiveConstraint(__SCREAMING_SNAKE_CASE ) # fails here def __UpperCamelCase ( self ): snake_case__ : List[str] = [[1, 2, 3], [1, 2, 4]] snake_case__ : Optional[int] = DisjunctiveConstraint(__SCREAMING_SNAKE_CASE ) snake_case__ , snake_case__ , snake_case__ : Any = dc.update(1 ) snake_case__ : Any = stepped is True and completed is False and reset is False self.assertTrue(__SCREAMING_SNAKE_CASE ) self.assertTrue(not dc.completed ) self.assertTrue(dc.current_seq == [1] ) snake_case__ , snake_case__ , snake_case__ : Tuple = dc.update(2 ) snake_case__ : Tuple = stepped is True and completed is False and reset is False self.assertTrue(__SCREAMING_SNAKE_CASE ) self.assertTrue(not dc.completed ) self.assertTrue(dc.current_seq == [1, 2] ) snake_case__ , snake_case__ , snake_case__ : Union[str, Any] = dc.update(3 ) snake_case__ : List[str] = stepped is True and completed is True and reset is False self.assertTrue(__SCREAMING_SNAKE_CASE ) self.assertTrue(dc.completed ) # Completed! self.assertTrue(dc.current_seq == [1, 2, 3] ) def __UpperCamelCase ( self ): snake_case__ : Optional[Any] = [[1, 2, 3], [1, 2, 4, 5], [1, 2, 5]] snake_case__ : int = DisjunctiveConstraint(__SCREAMING_SNAKE_CASE ) snake_case__ , snake_case__ , snake_case__ : str = dc.update(1 ) self.assertTrue(not dc.completed ) self.assertTrue(dc.current_seq == [1] ) snake_case__ , snake_case__ , snake_case__ : str = dc.update(2 ) self.assertTrue(not dc.completed ) self.assertTrue(dc.current_seq == [1, 2] ) snake_case__ , snake_case__ , snake_case__ : List[Any] = dc.update(4 ) self.assertTrue(not dc.completed ) self.assertTrue(dc.current_seq == [1, 2, 4] ) snake_case__ , snake_case__ , snake_case__ : Union[str, Any] = dc.update(5 ) self.assertTrue(dc.completed ) # Completed! self.assertTrue(dc.current_seq == [1, 2, 4, 5] ) dc.reset() snake_case__ , snake_case__ , snake_case__ : List[Any] = dc.update(1 ) self.assertTrue(not dc.completed ) self.assertTrue(dc.remaining() == 3 ) self.assertTrue(dc.current_seq == [1] ) snake_case__ , snake_case__ , snake_case__ : List[Any] = dc.update(2 ) self.assertTrue(not dc.completed ) self.assertTrue(dc.remaining() == 2 ) self.assertTrue(dc.current_seq == [1, 2] ) snake_case__ , snake_case__ , snake_case__ : Dict = dc.update(5 ) self.assertTrue(dc.completed ) # Completed! self.assertTrue(dc.remaining() == 0 ) self.assertTrue(dc.current_seq == [1, 2, 5] )
38
0
import importlib import json import os from collections import OrderedDict from typing import Dict, Optional, Union # Build the list of all feature extractors from ...configuration_utils import PretrainedConfig from ...dynamic_module_utils import get_class_from_dynamic_module, resolve_trust_remote_code from ...feature_extraction_utils import FeatureExtractionMixin from ...utils import CONFIG_NAME, FEATURE_EXTRACTOR_NAME, get_file_from_repo, logging from .auto_factory import _LazyAutoMapping from .configuration_auto import ( CONFIG_MAPPING_NAMES, AutoConfig, model_type_to_module_name, replace_list_option_in_docstrings, ) lowerCAmelCase_ = logging.get_logger(__name__) lowerCAmelCase_ = OrderedDict( [ ('''audio-spectrogram-transformer''', '''ASTFeatureExtractor'''), ('''beit''', '''BeitFeatureExtractor'''), ('''chinese_clip''', '''ChineseCLIPFeatureExtractor'''), ('''clap''', '''ClapFeatureExtractor'''), ('''clip''', '''CLIPFeatureExtractor'''), ('''clipseg''', '''ViTFeatureExtractor'''), ('''conditional_detr''', '''ConditionalDetrFeatureExtractor'''), ('''convnext''', '''ConvNextFeatureExtractor'''), ('''cvt''', '''ConvNextFeatureExtractor'''), ('''data2vec-audio''', '''Wav2Vec2FeatureExtractor'''), ('''data2vec-vision''', '''BeitFeatureExtractor'''), ('''deformable_detr''', '''DeformableDetrFeatureExtractor'''), ('''deit''', '''DeiTFeatureExtractor'''), ('''detr''', '''DetrFeatureExtractor'''), ('''dinat''', '''ViTFeatureExtractor'''), ('''donut-swin''', '''DonutFeatureExtractor'''), ('''dpt''', '''DPTFeatureExtractor'''), ('''encodec''', '''EncodecFeatureExtractor'''), ('''flava''', '''FlavaFeatureExtractor'''), ('''glpn''', '''GLPNFeatureExtractor'''), ('''groupvit''', '''CLIPFeatureExtractor'''), ('''hubert''', '''Wav2Vec2FeatureExtractor'''), ('''imagegpt''', '''ImageGPTFeatureExtractor'''), ('''layoutlmv2''', '''LayoutLMv2FeatureExtractor'''), ('''layoutlmv3''', '''LayoutLMv3FeatureExtractor'''), ('''levit''', '''LevitFeatureExtractor'''), ('''maskformer''', '''MaskFormerFeatureExtractor'''), ('''mctct''', '''MCTCTFeatureExtractor'''), ('''mobilenet_v1''', '''MobileNetV1FeatureExtractor'''), ('''mobilenet_v2''', '''MobileNetV2FeatureExtractor'''), ('''mobilevit''', '''MobileViTFeatureExtractor'''), ('''nat''', '''ViTFeatureExtractor'''), ('''owlvit''', '''OwlViTFeatureExtractor'''), ('''perceiver''', '''PerceiverFeatureExtractor'''), ('''poolformer''', '''PoolFormerFeatureExtractor'''), ('''regnet''', '''ConvNextFeatureExtractor'''), ('''resnet''', '''ConvNextFeatureExtractor'''), ('''segformer''', '''SegformerFeatureExtractor'''), ('''sew''', '''Wav2Vec2FeatureExtractor'''), ('''sew-d''', '''Wav2Vec2FeatureExtractor'''), ('''speech_to_text''', '''Speech2TextFeatureExtractor'''), ('''speecht5''', '''SpeechT5FeatureExtractor'''), ('''swiftformer''', '''ViTFeatureExtractor'''), ('''swin''', '''ViTFeatureExtractor'''), ('''swinv2''', '''ViTFeatureExtractor'''), ('''table-transformer''', '''DetrFeatureExtractor'''), ('''timesformer''', '''VideoMAEFeatureExtractor'''), ('''tvlt''', '''TvltFeatureExtractor'''), ('''unispeech''', '''Wav2Vec2FeatureExtractor'''), ('''unispeech-sat''', '''Wav2Vec2FeatureExtractor'''), ('''van''', '''ConvNextFeatureExtractor'''), ('''videomae''', '''VideoMAEFeatureExtractor'''), ('''vilt''', '''ViltFeatureExtractor'''), ('''vit''', '''ViTFeatureExtractor'''), ('''vit_mae''', '''ViTFeatureExtractor'''), ('''vit_msn''', '''ViTFeatureExtractor'''), ('''wav2vec2''', '''Wav2Vec2FeatureExtractor'''), ('''wav2vec2-conformer''', '''Wav2Vec2FeatureExtractor'''), ('''wavlm''', '''Wav2Vec2FeatureExtractor'''), ('''whisper''', '''WhisperFeatureExtractor'''), ('''xclip''', '''CLIPFeatureExtractor'''), ('''yolos''', '''YolosFeatureExtractor'''), ] ) lowerCAmelCase_ = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FEATURE_EXTRACTOR_MAPPING_NAMES) def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): for module_name, extractors in FEATURE_EXTRACTOR_MAPPING_NAMES.items(): if class_name in extractors: snake_case_ = model_type_to_module_name(SCREAMING_SNAKE_CASE__ ) snake_case_ = importlib.import_module(F'''.{module_name}''' , '''transformers.models''' ) try: return getattr(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) except AttributeError: continue for _, extractor in FEATURE_EXTRACTOR_MAPPING._extra_content.items(): if getattr(SCREAMING_SNAKE_CASE__ , '''__name__''' , SCREAMING_SNAKE_CASE__ ) == class_name: return extractor # We did not fine the class, but maybe it's because a dep is missing. In that case, the class will be in the main # init and we return the proper dummy to get an appropriate error message. snake_case_ = importlib.import_module('''transformers''' ) if hasattr(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): return getattr(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) return None def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ = None , SCREAMING_SNAKE_CASE__ = False , SCREAMING_SNAKE_CASE__ = False , SCREAMING_SNAKE_CASE__ = None , SCREAMING_SNAKE_CASE__ = None , SCREAMING_SNAKE_CASE__ = None , SCREAMING_SNAKE_CASE__ = False , **SCREAMING_SNAKE_CASE__ , ): snake_case_ = get_file_from_repo( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , cache_dir=SCREAMING_SNAKE_CASE__ , force_download=SCREAMING_SNAKE_CASE__ , resume_download=SCREAMING_SNAKE_CASE__ , proxies=SCREAMING_SNAKE_CASE__ , use_auth_token=SCREAMING_SNAKE_CASE__ , revision=SCREAMING_SNAKE_CASE__ , local_files_only=SCREAMING_SNAKE_CASE__ , ) if resolved_config_file is None: logger.info( '''Could not locate the feature extractor configuration file, will try to use the model config instead.''' ) return {} with open(SCREAMING_SNAKE_CASE__ , encoding='''utf-8''' ) as reader: return json.load(SCREAMING_SNAKE_CASE__ ) class snake_case_ : '''simple docstring''' def __init__( self : str ) ->Optional[int]: raise EnvironmentError( '''AutoFeatureExtractor is designed to be instantiated ''' '''using the `AutoFeatureExtractor.from_pretrained(pretrained_model_name_or_path)` method.''' ) @classmethod @replace_list_option_in_docstrings(_UpperCamelCase ) def snake_case__( cls : str , _UpperCamelCase : List[Any] , **_UpperCamelCase : Optional[int] ) ->List[str]: snake_case_ = kwargs.pop('''config''' , _UpperCamelCase ) snake_case_ = kwargs.pop('''trust_remote_code''' , _UpperCamelCase ) snake_case_ = True snake_case_, snake_case_ = FeatureExtractionMixin.get_feature_extractor_dict(_UpperCamelCase , **_UpperCamelCase ) snake_case_ = config_dict.get('''feature_extractor_type''' , _UpperCamelCase ) snake_case_ = None if "AutoFeatureExtractor" in config_dict.get('''auto_map''' , {} ): snake_case_ = config_dict['''auto_map''']['''AutoFeatureExtractor'''] # If we don't find the feature extractor class in the feature extractor config, let's try the model config. if feature_extractor_class is None and feature_extractor_auto_map is None: if not isinstance(_UpperCamelCase , _UpperCamelCase ): snake_case_ = AutoConfig.from_pretrained(_UpperCamelCase , **_UpperCamelCase ) # It could be in `config.feature_extractor_type`` snake_case_ = getattr(_UpperCamelCase , '''feature_extractor_type''' , _UpperCamelCase ) if hasattr(_UpperCamelCase , '''auto_map''' ) and "AutoFeatureExtractor" in config.auto_map: snake_case_ = config.auto_map['''AutoFeatureExtractor'''] if feature_extractor_class is not None: snake_case_ = feature_extractor_class_from_name(_UpperCamelCase ) snake_case_ = feature_extractor_auto_map is not None snake_case_ = feature_extractor_class is not None or type(_UpperCamelCase ) in FEATURE_EXTRACTOR_MAPPING snake_case_ = resolve_trust_remote_code( _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) if has_remote_code and trust_remote_code: snake_case_ = get_class_from_dynamic_module( _UpperCamelCase , _UpperCamelCase , **_UpperCamelCase ) snake_case_ = kwargs.pop('''code_revision''' , _UpperCamelCase ) if os.path.isdir(_UpperCamelCase ): feature_extractor_class.register_for_auto_class() return feature_extractor_class.from_dict(_UpperCamelCase , **_UpperCamelCase ) elif feature_extractor_class is not None: return feature_extractor_class.from_dict(_UpperCamelCase , **_UpperCamelCase ) # Last try: we use the FEATURE_EXTRACTOR_MAPPING. elif type(_UpperCamelCase ) in FEATURE_EXTRACTOR_MAPPING: snake_case_ = FEATURE_EXTRACTOR_MAPPING[type(_UpperCamelCase )] return feature_extractor_class.from_dict(_UpperCamelCase , **_UpperCamelCase ) raise ValueError( f'''Unrecognized feature extractor in {pretrained_model_name_or_path}. Should have a ''' f'''`feature_extractor_type` key in its {FEATURE_EXTRACTOR_NAME} of {CONFIG_NAME}, or one of the following ''' f'''`model_type` keys in its {CONFIG_NAME}: {', '.join(c for c in FEATURE_EXTRACTOR_MAPPING_NAMES.keys() )}''' ) @staticmethod def snake_case__( _UpperCamelCase : Optional[int] , _UpperCamelCase : Optional[int] ) ->Dict: FEATURE_EXTRACTOR_MAPPING.register(_UpperCamelCase , _UpperCamelCase )
39
import math import unittest from transformers import BioGptConfig, is_torch_available from transformers.testing_utils import require_torch, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( BioGptForCausalLM, BioGptForSequenceClassification, BioGptForTokenClassification, BioGptModel, BioGptTokenizer, ) from transformers.models.biogpt.modeling_biogpt import BIOGPT_PRETRAINED_MODEL_ARCHIVE_LIST class snake_case_ : '''simple docstring''' def __init__( self : Optional[int] , _UpperCamelCase : Tuple , _UpperCamelCase : Optional[int]=1_3 , _UpperCamelCase : str=7 , _UpperCamelCase : int=True , _UpperCamelCase : Dict=True , _UpperCamelCase : int=False , _UpperCamelCase : Dict=True , _UpperCamelCase : Optional[int]=9_9 , _UpperCamelCase : str=3_2 , _UpperCamelCase : str=5 , _UpperCamelCase : str=4 , _UpperCamelCase : int=3_7 , _UpperCamelCase : int="gelu" , _UpperCamelCase : List[str]=0.1 , _UpperCamelCase : Dict=0.1 , _UpperCamelCase : str=5_1_2 , _UpperCamelCase : Optional[int]=1_6 , _UpperCamelCase : List[str]=2 , _UpperCamelCase : Any=0.02 , _UpperCamelCase : List[str]=3 , _UpperCamelCase : List[str]=4 , _UpperCamelCase : str=None , ) ->Dict: snake_case_ = parent snake_case_ = batch_size snake_case_ = seq_length snake_case_ = is_training snake_case_ = use_input_mask snake_case_ = use_token_type_ids snake_case_ = use_labels snake_case_ = vocab_size snake_case_ = hidden_size snake_case_ = num_hidden_layers snake_case_ = num_attention_heads snake_case_ = intermediate_size snake_case_ = hidden_act snake_case_ = hidden_dropout_prob snake_case_ = attention_probs_dropout_prob snake_case_ = max_position_embeddings snake_case_ = type_vocab_size snake_case_ = type_sequence_label_size snake_case_ = initializer_range snake_case_ = num_labels snake_case_ = num_choices snake_case_ = scope def snake_case__( self : str ) ->List[Any]: snake_case_ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) snake_case_ = None if self.use_input_mask: snake_case_ = random_attention_mask([self.batch_size, self.seq_length] ) snake_case_ = None if self.use_token_type_ids: snake_case_ = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) snake_case_ = None snake_case_ = None snake_case_ = None if self.use_labels: snake_case_ = ids_tensor([self.batch_size] , self.type_sequence_label_size ) snake_case_ = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) snake_case_ = ids_tensor([self.batch_size] , self.num_choices ) snake_case_ = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def snake_case__( self : List[str] ) ->Tuple: return BioGptConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=_UpperCamelCase , initializer_range=self.initializer_range , ) def snake_case__( self : int , _UpperCamelCase : int , _UpperCamelCase : List[str] , _UpperCamelCase : Optional[Any] , _UpperCamelCase : Any , _UpperCamelCase : List[str] , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : Union[str, Any] ) ->Dict: snake_case_ = BioGptModel(config=_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() snake_case_ = model(_UpperCamelCase , attention_mask=_UpperCamelCase ) snake_case_ = model(_UpperCamelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def snake_case__( self : Optional[Any] , _UpperCamelCase : Dict , _UpperCamelCase : List[str] , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : List[Any] , _UpperCamelCase : int , _UpperCamelCase : int , _UpperCamelCase : int , _UpperCamelCase : Optional[int] , _UpperCamelCase : Union[str, Any] , ) ->Optional[int]: snake_case_ = BioGptForCausalLM(config=_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() snake_case_ = 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 snake_case__( self : Dict , _UpperCamelCase : str , _UpperCamelCase : List[str] , _UpperCamelCase : List[Any] , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : str , *_UpperCamelCase : List[Any] ) ->Union[str, Any]: snake_case_ = BioGptModel(config=_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() # create attention mask snake_case_ = torch.ones(input_ids.shape , dtype=torch.long , device=_UpperCamelCase ) snake_case_ = self.seq_length // 2 snake_case_ = 0 # first forward pass snake_case_, snake_case_ = model(_UpperCamelCase , attention_mask=_UpperCamelCase ).to_tuple() # create hypothetical next token and extent to next_input_ids snake_case_ = ids_tensor((self.batch_size, 1) , config.vocab_size ) # change a random masked slice from input_ids snake_case_ = ids_tensor((1,) , _UpperCamelCase ).item() + 1 snake_case_ = ids_tensor((self.batch_size, 1) , config.vocab_size ).squeeze(-1 ) snake_case_ = random_other_next_tokens # append to next input_ids and attn_mask snake_case_ = torch.cat([input_ids, next_tokens] , dim=-1 ) snake_case_ = torch.cat( [attn_mask, torch.ones((attn_mask.shape[0], 1) , dtype=torch.long , device=_UpperCamelCase )] , dim=1 , ) # get two different outputs snake_case_ = model(_UpperCamelCase , attention_mask=_UpperCamelCase )['''last_hidden_state'''] snake_case_ = model(_UpperCamelCase , past_key_values=_UpperCamelCase , attention_mask=_UpperCamelCase )['''last_hidden_state'''] # select random slice snake_case_ = ids_tensor((1,) , output_from_past.shape[-1] ).item() snake_case_ = output_from_no_past[:, -1, random_slice_idx].detach() snake_case_ = output_from_past[:, 0, random_slice_idx].detach() # test that outputs are equal for slice self.parent.assertTrue(torch.allclose(_UpperCamelCase , _UpperCamelCase , atol=1e-3 ) ) def snake_case__( self : Union[str, Any] , _UpperCamelCase : Optional[int] , _UpperCamelCase : Optional[Any] , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : List[Any] , _UpperCamelCase : Dict , *_UpperCamelCase : List[Any] ) ->int: snake_case_ = BioGptModel(config=_UpperCamelCase ).to(_UpperCamelCase ).eval() snake_case_ = torch.ones(input_ids.shape , dtype=torch.long , device=_UpperCamelCase ) # first forward pass snake_case_ = model(_UpperCamelCase , attention_mask=_UpperCamelCase , use_cache=_UpperCamelCase ) snake_case_, snake_case_ = outputs.to_tuple() # create hypothetical multiple next token and extent to next_input_ids snake_case_ = ids_tensor((self.batch_size, 3) , config.vocab_size ) snake_case_ = ids_tensor((self.batch_size, 3) , 2 ) # append to next input_ids and snake_case_ = torch.cat([input_ids, next_tokens] , dim=-1 ) snake_case_ = torch.cat([attention_mask, next_attn_mask] , dim=-1 ) snake_case_ = model(_UpperCamelCase , attention_mask=_UpperCamelCase )['''last_hidden_state'''] snake_case_ = model(_UpperCamelCase , attention_mask=_UpperCamelCase , past_key_values=_UpperCamelCase )[ '''last_hidden_state''' ] # select random slice snake_case_ = ids_tensor((1,) , output_from_past.shape[-1] ).item() snake_case_ = output_from_no_past[:, -3:, random_slice_idx].detach() snake_case_ = output_from_past[:, :, random_slice_idx].detach() self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1] ) # test that outputs are equal for slice self.parent.assertTrue(torch.allclose(_UpperCamelCase , _UpperCamelCase , atol=1e-3 ) ) def snake_case__( self : int , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : str , _UpperCamelCase : str , _UpperCamelCase : Dict , _UpperCamelCase : Optional[Any] , *_UpperCamelCase : List[Any] , _UpperCamelCase : List[str]=False ) ->Dict: snake_case_ = BioGptForCausalLM(_UpperCamelCase ) model.to(_UpperCamelCase ) if gradient_checkpointing: model.gradient_checkpointing_enable() snake_case_ = model(_UpperCamelCase , labels=_UpperCamelCase ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) result.loss.backward() def snake_case__( self : List[Any] , _UpperCamelCase : Optional[int] , *_UpperCamelCase : Dict ) ->Dict: snake_case_ = BioGptModel(_UpperCamelCase ) snake_case_ = model.config.initializer_range / math.sqrt(2 * model.config.num_hidden_layers ) for key in model.state_dict().keys(): if "c_proj" in key and "weight" in key: self.parent.assertLessEqual(abs(torch.std(model.state_dict()[key] ) - model_std ) , 0.001 ) self.parent.assertLessEqual(abs(torch.mean(model.state_dict()[key] ) - 0.0 ) , 0.01 ) def snake_case__( self : Any , _UpperCamelCase : Tuple , _UpperCamelCase : List[str] , _UpperCamelCase : List[Any] , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : int , *_UpperCamelCase : List[str] ) ->int: snake_case_ = self.num_labels snake_case_ = BioGptForTokenClassification(_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() snake_case_ = model(_UpperCamelCase , attention_mask=_UpperCamelCase , token_type_ids=_UpperCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def snake_case__( self : Optional[Any] ) ->int: snake_case_ = self.prepare_config_and_inputs() ( ( snake_case_ ), ( snake_case_ ), ( snake_case_ ), ( snake_case_ ), ( snake_case_ ), ( snake_case_ ), ( snake_case_ ), ) = config_and_inputs snake_case_ = {'''input_ids''': input_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_torch class snake_case_ ( __A , __A , __A , unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = ( (BioGptModel, BioGptForCausalLM, BioGptForSequenceClassification, BioGptForTokenClassification) if is_torch_available() else () ) SCREAMING_SNAKE_CASE : Tuple = (BioGptForCausalLM,) if is_torch_available() else () SCREAMING_SNAKE_CASE : Optional[Any] = ( { "feature-extraction": BioGptModel, "text-classification": BioGptForSequenceClassification, "text-generation": BioGptForCausalLM, "token-classification": BioGptForTokenClassification, "zero-shot": BioGptForSequenceClassification, } if is_torch_available() else {} ) SCREAMING_SNAKE_CASE : Tuple = False def snake_case__( self : List[str] ) ->Union[str, Any]: snake_case_ = BioGptModelTester(self ) snake_case_ = ConfigTester(self , config_class=_UpperCamelCase , hidden_size=3_7 ) def snake_case__( self : str ) ->int: self.config_tester.run_common_tests() def snake_case__( self : str ) ->Tuple: snake_case_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_UpperCamelCase ) def snake_case__( self : Tuple ) ->List[Any]: snake_case_ = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: snake_case_ = type self.model_tester.create_and_check_model(*_UpperCamelCase ) def snake_case__( self : Tuple ) ->str: snake_case_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_biogpt_model_attention_mask_past(*_UpperCamelCase ) def snake_case__( self : Union[str, Any] ) ->Dict: snake_case_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_forward_and_backwards(*_UpperCamelCase , gradient_checkpointing=_UpperCamelCase ) def snake_case__( self : Optional[int] ) ->List[Any]: snake_case_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_biogpt_model_past_large_inputs(*_UpperCamelCase ) def snake_case__( self : List[Any] ) ->Union[str, Any]: snake_case_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_biogpt_weight_initialization(*_UpperCamelCase ) def snake_case__( self : Optional[int] ) ->Optional[int]: snake_case_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_biogpt_for_token_classification(*_UpperCamelCase ) @slow def snake_case__( self : int ) ->Optional[Any]: snake_case_ = BioGptForCausalLM.from_pretrained('''microsoft/biogpt''' ) model.to(_UpperCamelCase ) snake_case_ = BioGptTokenizer.from_pretrained('''microsoft/biogpt''' ) snake_case_ = '''left''' # Define PAD Token = EOS Token = 50256 snake_case_ = tokenizer.eos_token snake_case_ = model.config.eos_token_id # use different length sentences to test batching snake_case_ = [ '''Hello, my dog is a little''', '''Today, I''', ] snake_case_ = tokenizer(_UpperCamelCase , return_tensors='''pt''' , padding=_UpperCamelCase ) snake_case_ = inputs['''input_ids'''].to(_UpperCamelCase ) snake_case_ = model.generate( input_ids=_UpperCamelCase , attention_mask=inputs['''attention_mask'''].to(_UpperCamelCase ) , ) snake_case_ = tokenizer(sentences[0] , return_tensors='''pt''' ).input_ids.to(_UpperCamelCase ) snake_case_ = model.generate(input_ids=_UpperCamelCase ) snake_case_ = inputs_non_padded.shape[-1] - inputs['''attention_mask'''][-1].long().sum().cpu().item() snake_case_ = tokenizer(sentences[1] , return_tensors='''pt''' ).input_ids.to(_UpperCamelCase ) snake_case_ = model.generate(input_ids=_UpperCamelCase , max_length=model.config.max_length - num_paddings ) snake_case_ = tokenizer.batch_decode(_UpperCamelCase , skip_special_tokens=_UpperCamelCase ) snake_case_ = tokenizer.decode(output_non_padded[0] , skip_special_tokens=_UpperCamelCase ) snake_case_ = tokenizer.decode(output_padded[0] , skip_special_tokens=_UpperCamelCase ) snake_case_ = [ '''Hello, my dog is a little bit bigger than a little bit.''', '''Today, I have a good idea of how to use the information''', ] self.assertListEqual(_UpperCamelCase , _UpperCamelCase ) self.assertListEqual(_UpperCamelCase , [non_padded_sentence, padded_sentence] ) @slow def snake_case__( self : Optional[int] ) ->List[str]: for model_name in BIOGPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: snake_case_ = BioGptModel.from_pretrained(_UpperCamelCase ) self.assertIsNotNone(_UpperCamelCase ) def snake_case__( self : Optional[int] ) ->str: snake_case_, snake_case_ = self.model_tester.prepare_config_and_inputs_for_common() snake_case_ = 3 snake_case_ = input_dict['''input_ids'''] snake_case_ = input_ids.ne(1 ).to(_UpperCamelCase ) snake_case_ = ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size ) snake_case_ = BioGptForSequenceClassification(_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() snake_case_ = model(_UpperCamelCase , attention_mask=_UpperCamelCase , labels=_UpperCamelCase ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def snake_case__( self : str ) ->str: snake_case_, snake_case_ = self.model_tester.prepare_config_and_inputs_for_common() snake_case_ = 3 snake_case_ = '''multi_label_classification''' snake_case_ = input_dict['''input_ids'''] snake_case_ = input_ids.ne(1 ).to(_UpperCamelCase ) snake_case_ = ids_tensor( [self.model_tester.batch_size, config.num_labels] , self.model_tester.type_sequence_label_size ).to(torch.float ) snake_case_ = BioGptForSequenceClassification(_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() snake_case_ = model(_UpperCamelCase , attention_mask=_UpperCamelCase , labels=_UpperCamelCase ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) @require_torch class snake_case_ ( unittest.TestCase ): '''simple docstring''' @slow def snake_case__( self : int ) ->Any: snake_case_ = BioGptForCausalLM.from_pretrained('''microsoft/biogpt''' ) snake_case_ = torch.tensor([[2, 4_8_0_5, 9, 6_5_6, 2_1]] ) snake_case_ = model(_UpperCamelCase )[0] snake_case_ = 4_2_3_8_4 snake_case_ = torch.Size((1, 5, vocab_size) ) self.assertEqual(output.shape , _UpperCamelCase ) snake_case_ = torch.tensor( [[[-9.5236, -9.8918, 10.4557], [-11.0469, -9.6423, 8.1022], [-8.8664, -7.8826, 5.5325]]] ) self.assertTrue(torch.allclose(output[:, :3, :3] , _UpperCamelCase , atol=1e-4 ) ) @slow def snake_case__( self : List[str] ) ->Optional[int]: snake_case_ = BioGptTokenizer.from_pretrained('''microsoft/biogpt''' ) snake_case_ = BioGptForCausalLM.from_pretrained('''microsoft/biogpt''' ) model.to(_UpperCamelCase ) torch.manual_seed(0 ) snake_case_ = tokenizer('''COVID-19 is''' , return_tensors='''pt''' ).to(_UpperCamelCase ) snake_case_ = model.generate( **_UpperCamelCase , min_length=1_0_0 , max_length=1_0_2_4 , num_beams=5 , early_stopping=_UpperCamelCase , ) snake_case_ = tokenizer.decode(output_ids[0] , skip_special_tokens=_UpperCamelCase ) snake_case_ = ( '''COVID-19 is a global pandemic caused by severe acute respiratory syndrome coronavirus 2 (SARS-CoV-2), the''' ''' causative agent of coronavirus disease 2019 (COVID-19), which has spread to more than 200 countries and''' ''' territories, including the United States (US), Canada, Australia, New Zealand, the United Kingdom (UK),''' ''' and the United States of America (USA), as of March 11, 2020, with more than 800,000 confirmed cases and''' ''' more than 800,000 deaths.''' ) self.assertEqual(_UpperCamelCase , _UpperCamelCase )
39
1
import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import MobileViTImageProcessor class snake_case_ ( unittest.TestCase ): '''simple docstring''' def __init__( self : Any , _UpperCamelCase : Optional[Any] , _UpperCamelCase : Any=7 , _UpperCamelCase : Tuple=3 , _UpperCamelCase : Dict=1_8 , _UpperCamelCase : Dict=3_0 , _UpperCamelCase : Optional[Any]=4_0_0 , _UpperCamelCase : Optional[int]=True , _UpperCamelCase : List[Any]=None , _UpperCamelCase : Dict=True , _UpperCamelCase : Optional[Any]=None , _UpperCamelCase : Any=True , ) ->Optional[Any]: snake_case_ = size if size is not None else {'''shortest_edge''': 2_0} snake_case_ = crop_size if crop_size is not None else {'''height''': 1_8, '''width''': 1_8} snake_case_ = parent snake_case_ = batch_size snake_case_ = num_channels snake_case_ = image_size snake_case_ = min_resolution snake_case_ = max_resolution snake_case_ = do_resize snake_case_ = size snake_case_ = do_center_crop snake_case_ = crop_size snake_case_ = do_flip_channel_order def snake_case__( self : Any ) ->Optional[Any]: return { "do_resize": self.do_resize, "size": self.size, "do_center_crop": self.do_center_crop, "crop_size": self.crop_size, "do_flip_channel_order": self.do_flip_channel_order, } @require_torch @require_vision class snake_case_ ( __A , unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = MobileViTImageProcessor if is_vision_available() else None def snake_case__( self : Dict ) ->Optional[int]: snake_case_ = MobileViTImageProcessingTester(self ) @property def snake_case__( self : str ) ->List[Any]: return self.image_processor_tester.prepare_image_processor_dict() def snake_case__( self : List[str] ) ->int: snake_case_ = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_UpperCamelCase , '''do_resize''' ) ) self.assertTrue(hasattr(_UpperCamelCase , '''size''' ) ) self.assertTrue(hasattr(_UpperCamelCase , '''do_center_crop''' ) ) self.assertTrue(hasattr(_UpperCamelCase , '''center_crop''' ) ) self.assertTrue(hasattr(_UpperCamelCase , '''do_flip_channel_order''' ) ) def snake_case__( self : Tuple ) ->Any: snake_case_ = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'''shortest_edge''': 2_0} ) self.assertEqual(image_processor.crop_size , {'''height''': 1_8, '''width''': 1_8} ) snake_case_ = self.image_processing_class.from_dict(self.image_processor_dict , size=4_2 , crop_size=8_4 ) self.assertEqual(image_processor.size , {'''shortest_edge''': 4_2} ) self.assertEqual(image_processor.crop_size , {'''height''': 8_4, '''width''': 8_4} ) def snake_case__( self : int ) ->str: pass def snake_case__( self : Optional[int] ) ->Dict: # Initialize image_processing snake_case_ = self.image_processing_class(**self.image_processor_dict ) # create random PIL images snake_case_ = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , Image.Image ) # Test not batched input snake_case_ = 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.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) # Test batched snake_case_ = image_processing(_UpperCamelCase , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) def snake_case__( self : str ) ->Optional[Any]: # Initialize image_processing snake_case_ = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors snake_case_ = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase , numpify=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , np.ndarray ) # Test not batched input snake_case_ = 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.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) # Test batched snake_case_ = image_processing(_UpperCamelCase , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) def snake_case__( self : int ) ->List[str]: # Initialize image_processing snake_case_ = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors snake_case_ = prepare_image_inputs(self.image_processor_tester , equal_resolution=_UpperCamelCase , torchify=_UpperCamelCase ) for image in image_inputs: self.assertIsInstance(_UpperCamelCase , torch.Tensor ) # Test not batched input snake_case_ = 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.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) # Test batched snake_case_ = image_processing(_UpperCamelCase , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , )
39
def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): # "extended trapezoidal rule" # int(f) = dx/2 * (f1 + 2f2 + ... + fn) snake_case_ = (boundary[1] - boundary[0]) / steps snake_case_ = boundary[0] snake_case_ = boundary[1] snake_case_ = make_points(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) snake_case_ = 0.0 y += (h / 2.0) * f(SCREAMING_SNAKE_CASE__ ) for i in x_i: # print(i) y += h * f(SCREAMING_SNAKE_CASE__ ) y += (h / 2.0) * f(SCREAMING_SNAKE_CASE__ ) return y def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): snake_case_ = a + h while x < (b - h): yield x snake_case_ = x + h def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): # enter your function here snake_case_ = (x - 0) * (x - 0) return y def __SCREAMING_SNAKE_CASE (): snake_case_ = 0.0 # Lower bound of integration snake_case_ = 1.0 # Upper bound of integration snake_case_ = 10.0 # define number of steps or resolution snake_case_ = [a, b] # define boundary of integration snake_case_ = method_a(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) print(F'''y = {y}''' ) if __name__ == "__main__": main()
39
1
import os import unittest from transformers.models.cpmant.tokenization_cpmant import VOCAB_FILES_NAMES, CpmAntTokenizer from transformers.testing_utils import require_jieba, tooslow from ...test_tokenization_common import TokenizerTesterMixin @require_jieba class snake_case_ ( __A , unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = CpmAntTokenizer SCREAMING_SNAKE_CASE : Optional[Any] = False def snake_case__( self : Tuple ) ->Any: super().setUp() snake_case_ = [ '''<d>''', '''</d>''', '''<s>''', '''</s>''', '''</_>''', '''<unk>''', '''<pad>''', '''</n>''', '''我''', '''是''', '''C''', '''P''', '''M''', '''A''', '''n''', '''t''', ] snake_case_ = 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] ) ) @tooslow def snake_case__( self : Any ) ->str: snake_case_ = CpmAntTokenizer.from_pretrained('''openbmb/cpm-ant-10b''' ) snake_case_ = '''今天天气真好!''' snake_case_ = ['''今天''', '''天气''', '''真''', '''好''', '''!'''] snake_case_ = tokenizer.tokenize(_UpperCamelCase ) self.assertListEqual(_UpperCamelCase , _UpperCamelCase ) snake_case_ = '''今天天气真好!''' snake_case_ = [tokenizer.bos_token] + tokens snake_case_ = [6, 9_8_0_2, 1_4_9_6_2, 2_0_8_2, 8_3_1, 2_4_4] self.assertListEqual(tokenizer.convert_tokens_to_ids(_UpperCamelCase ) , _UpperCamelCase ) snake_case_ = tokenizer.decode(_UpperCamelCase ) self.assertEqual(_UpperCamelCase , _UpperCamelCase )
39
import os import re import sys import traceback import warnings from pathlib import Path from typing import Dict, Optional, Union from uuid import uuida from huggingface_hub import HfFolder, ModelCard, ModelCardData, hf_hub_download, whoami from huggingface_hub.file_download import REGEX_COMMIT_HASH from huggingface_hub.utils import ( EntryNotFoundError, RepositoryNotFoundError, RevisionNotFoundError, is_jinja_available, ) from packaging import version from requests import HTTPError from .. import __version__ from .constants import ( DEPRECATED_REVISION_ARGS, DIFFUSERS_CACHE, HUGGINGFACE_CO_RESOLVE_ENDPOINT, SAFETENSORS_WEIGHTS_NAME, WEIGHTS_NAME, ) from .import_utils import ( ENV_VARS_TRUE_VALUES, _flax_version, _jax_version, _onnxruntime_version, _torch_version, is_flax_available, is_onnx_available, is_torch_available, ) from .logging import get_logger lowerCAmelCase_ = get_logger(__name__) lowerCAmelCase_ = Path(__file__).parent / '''model_card_template.md''' lowerCAmelCase_ = uuida().hex lowerCAmelCase_ = os.getenv('''HF_HUB_OFFLINE''', '''''').upper() in ENV_VARS_TRUE_VALUES lowerCAmelCase_ = os.getenv('''DISABLE_TELEMETRY''', '''''').upper() in ENV_VARS_TRUE_VALUES lowerCAmelCase_ = HUGGINGFACE_CO_RESOLVE_ENDPOINT + '''/api/telemetry/''' def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ = None ): snake_case_ = F'''diffusers/{__version__}; python/{sys.version.split()[0]}; session_id/{SESSION_ID}''' if DISABLE_TELEMETRY or HF_HUB_OFFLINE: return ua + "; telemetry/off" if is_torch_available(): ua += F'''; torch/{_torch_version}''' if is_flax_available(): ua += F'''; jax/{_jax_version}''' ua += F'''; flax/{_flax_version}''' if is_onnx_available(): ua += F'''; onnxruntime/{_onnxruntime_version}''' # CI will set this value to True if os.environ.get('''DIFFUSERS_IS_CI''' , '''''' ).upper() in ENV_VARS_TRUE_VALUES: ua += "; is_ci/true" if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): ua += "; " + "; ".join(F'''{k}/{v}''' for k, v in user_agent.items() ) elif isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): ua += "; " + user_agent return ua def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ = None , SCREAMING_SNAKE_CASE__ = None ): if token is None: snake_case_ = HfFolder.get_token() if organization is None: snake_case_ = whoami(SCREAMING_SNAKE_CASE__ )['''name'''] return F'''{username}/{model_id}''' else: return F'''{organization}/{model_id}''' def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): if not is_jinja_available(): raise ValueError( '''Modelcard rendering is based on Jinja templates.''' ''' Please make sure to have `jinja` installed before using `create_model_card`.''' ''' To install it, please run `pip install Jinja2`.''' ) if hasattr(SCREAMING_SNAKE_CASE__ , '''local_rank''' ) and args.local_rank not in [-1, 0]: return snake_case_ = args.hub_token if hasattr(SCREAMING_SNAKE_CASE__ , '''hub_token''' ) else None snake_case_ = get_full_repo_name(SCREAMING_SNAKE_CASE__ , token=SCREAMING_SNAKE_CASE__ ) snake_case_ = ModelCard.from_template( card_data=ModelCardData( # Card metadata object that will be converted to YAML block language='''en''' , license='''apache-2.0''' , library_name='''diffusers''' , tags=[] , datasets=args.dataset_name , metrics=[] , ) , template_path=SCREAMING_SNAKE_CASE__ , model_name=SCREAMING_SNAKE_CASE__ , repo_name=SCREAMING_SNAKE_CASE__ , dataset_name=args.dataset_name if hasattr(SCREAMING_SNAKE_CASE__ , '''dataset_name''' ) else None , learning_rate=args.learning_rate , train_batch_size=args.train_batch_size , eval_batch_size=args.eval_batch_size , gradient_accumulation_steps=( args.gradient_accumulation_steps if hasattr(SCREAMING_SNAKE_CASE__ , '''gradient_accumulation_steps''' ) else None ) , adam_betaa=args.adam_betaa if hasattr(SCREAMING_SNAKE_CASE__ , '''adam_beta1''' ) else None , adam_betaa=args.adam_betaa if hasattr(SCREAMING_SNAKE_CASE__ , '''adam_beta2''' ) else None , adam_weight_decay=args.adam_weight_decay if hasattr(SCREAMING_SNAKE_CASE__ , '''adam_weight_decay''' ) else None , adam_epsilon=args.adam_epsilon if hasattr(SCREAMING_SNAKE_CASE__ , '''adam_epsilon''' ) else None , lr_scheduler=args.lr_scheduler if hasattr(SCREAMING_SNAKE_CASE__ , '''lr_scheduler''' ) else None , lr_warmup_steps=args.lr_warmup_steps if hasattr(SCREAMING_SNAKE_CASE__ , '''lr_warmup_steps''' ) else None , ema_inv_gamma=args.ema_inv_gamma if hasattr(SCREAMING_SNAKE_CASE__ , '''ema_inv_gamma''' ) else None , ema_power=args.ema_power if hasattr(SCREAMING_SNAKE_CASE__ , '''ema_power''' ) else None , ema_max_decay=args.ema_max_decay if hasattr(SCREAMING_SNAKE_CASE__ , '''ema_max_decay''' ) else None , mixed_precision=args.mixed_precision , ) snake_case_ = os.path.join(args.output_dir , '''README.md''' ) model_card.save(SCREAMING_SNAKE_CASE__ ) def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ = None ): if resolved_file is None or commit_hash is not None: return commit_hash snake_case_ = str(Path(SCREAMING_SNAKE_CASE__ ).as_posix() ) snake_case_ = re.search(R'''snapshots/([^/]+)/''' , SCREAMING_SNAKE_CASE__ ) if search is None: return None snake_case_ = search.groups()[0] return commit_hash if REGEX_COMMIT_HASH.match(SCREAMING_SNAKE_CASE__ ) else None # Old default cache path, potentially to be migrated. # This logic was more or less taken from `transformers`, with the following differences: # - Diffusers doesn't use custom environment variables to specify the cache path. # - There is no need to migrate the cache format, just move the files to the new location. lowerCAmelCase_ = os.path.expanduser( os.getenv('''HF_HOME''', os.path.join(os.getenv('''XDG_CACHE_HOME''', '''~/.cache'''), '''huggingface''')) ) lowerCAmelCase_ = os.path.join(hf_cache_home, '''diffusers''') def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ = None , SCREAMING_SNAKE_CASE__ = None ): if new_cache_dir is None: snake_case_ = DIFFUSERS_CACHE if old_cache_dir is None: snake_case_ = old_diffusers_cache snake_case_ = Path(SCREAMING_SNAKE_CASE__ ).expanduser() snake_case_ = Path(SCREAMING_SNAKE_CASE__ ).expanduser() for old_blob_path in old_cache_dir.glob('''**/blobs/*''' ): if old_blob_path.is_file() and not old_blob_path.is_symlink(): snake_case_ = new_cache_dir / old_blob_path.relative_to(SCREAMING_SNAKE_CASE__ ) new_blob_path.parent.mkdir(parents=SCREAMING_SNAKE_CASE__ , exist_ok=SCREAMING_SNAKE_CASE__ ) os.replace(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) try: os.symlink(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) except OSError: logger.warning( '''Could not create symlink between old cache and new cache. If you use an older version of diffusers again, files will be re-downloaded.''' ) # At this point, old_cache_dir contains symlinks to the new cache (it can still be used). lowerCAmelCase_ = os.path.join(DIFFUSERS_CACHE, '''version_diffusers_cache.txt''') if not os.path.isfile(cache_version_file): lowerCAmelCase_ = 0 else: with open(cache_version_file) as f: try: lowerCAmelCase_ = int(f.read()) except ValueError: lowerCAmelCase_ = 0 if cache_version < 1: lowerCAmelCase_ = os.path.isdir(old_diffusers_cache) and len(os.listdir(old_diffusers_cache)) > 0 if old_cache_is_not_empty: logger.warning( '''The cache for model files in Diffusers v0.14.0 has moved to a new location. Moving your ''' '''existing cached models. This is a one-time operation, you can interrupt it or run it ''' '''later by calling `diffusers.utils.hub_utils.move_cache()`.''' ) try: move_cache() except Exception as e: lowerCAmelCase_ = '''\n'''.join(traceback.format_tb(e.__traceback__)) logger.error( f"""There was a problem when trying to move your cache:\n\n{trace}\n{e.__class__.__name__}: {e}\n\nPlease """ '''file an issue at https://github.com/huggingface/diffusers/issues/new/choose, copy paste this whole ''' '''message and we will do our best to help.''' ) if cache_version < 1: try: os.makedirs(DIFFUSERS_CACHE, exist_ok=True) with open(cache_version_file, '''w''') as f: f.write('''1''') except Exception: logger.warning( f"""There was a problem when trying to write in your cache folder ({DIFFUSERS_CACHE}). Please, ensure """ '''the directory exists and can be written to.''' ) def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ = None ): if variant is not None: snake_case_ = weights_name.split('''.''' ) snake_case_ = splits[:-1] + [variant] + splits[-1:] snake_case_ = '''.'''.join(SCREAMING_SNAKE_CASE__ ) return weights_name def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , *, SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__=None , ): snake_case_ = str(SCREAMING_SNAKE_CASE__ ) if os.path.isfile(SCREAMING_SNAKE_CASE__ ): return pretrained_model_name_or_path elif os.path.isdir(SCREAMING_SNAKE_CASE__ ): if os.path.isfile(os.path.join(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) ): # Load from a PyTorch checkpoint snake_case_ = os.path.join(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) return model_file elif subfolder is not None and os.path.isfile( os.path.join(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) ): snake_case_ = os.path.join(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) return model_file else: raise EnvironmentError( F'''Error no file named {weights_name} found in directory {pretrained_model_name_or_path}.''' ) else: # 1. First check if deprecated way of loading from branches is used if ( revision in DEPRECATED_REVISION_ARGS and (weights_name == WEIGHTS_NAME or weights_name == SAFETENSORS_WEIGHTS_NAME) and version.parse(version.parse(SCREAMING_SNAKE_CASE__ ).base_version ) >= version.parse('''0.20.0''' ) ): try: snake_case_ = hf_hub_download( SCREAMING_SNAKE_CASE__ , filename=_add_variant(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) , cache_dir=SCREAMING_SNAKE_CASE__ , force_download=SCREAMING_SNAKE_CASE__ , proxies=SCREAMING_SNAKE_CASE__ , resume_download=SCREAMING_SNAKE_CASE__ , local_files_only=SCREAMING_SNAKE_CASE__ , use_auth_token=SCREAMING_SNAKE_CASE__ , user_agent=SCREAMING_SNAKE_CASE__ , subfolder=SCREAMING_SNAKE_CASE__ , revision=revision or commit_hash , ) warnings.warn( F'''Loading the variant {revision} from {pretrained_model_name_or_path} via `revision=\'{revision}\'` is deprecated. Loading instead from `revision=\'main\'` with `variant={revision}`. Loading model variants via `revision=\'{revision}\'` will be removed in diffusers v1. Please use `variant=\'{revision}\'` instead.''' , SCREAMING_SNAKE_CASE__ , ) return model_file except: # noqa: E722 warnings.warn( F'''You are loading the variant {revision} from {pretrained_model_name_or_path} via `revision=\'{revision}\'`. This behavior is deprecated and will be removed in diffusers v1. One should use `variant=\'{revision}\'` instead. However, it appears that {pretrained_model_name_or_path} currently does not have a {_add_variant(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ )} file in the \'main\' branch of {pretrained_model_name_or_path}. \n The Diffusers team and community would be very grateful if you could open an issue: https://github.com/huggingface/diffusers/issues/new with the title \'{pretrained_model_name_or_path} is missing {_add_variant(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ )}\' so that the correct variant file can be added.''' , SCREAMING_SNAKE_CASE__ , ) try: # 2. Load model file as usual snake_case_ = hf_hub_download( SCREAMING_SNAKE_CASE__ , filename=SCREAMING_SNAKE_CASE__ , cache_dir=SCREAMING_SNAKE_CASE__ , force_download=SCREAMING_SNAKE_CASE__ , proxies=SCREAMING_SNAKE_CASE__ , resume_download=SCREAMING_SNAKE_CASE__ , local_files_only=SCREAMING_SNAKE_CASE__ , use_auth_token=SCREAMING_SNAKE_CASE__ , user_agent=SCREAMING_SNAKE_CASE__ , subfolder=SCREAMING_SNAKE_CASE__ , revision=revision or commit_hash , ) return model_file except RepositoryNotFoundError: raise EnvironmentError( F'''{pretrained_model_name_or_path} is not a local folder and is not a valid model identifier ''' '''listed on \'https://huggingface.co/models\'\nIf this is a private repository, make sure to pass a ''' '''token having permission to this repo with `use_auth_token` or log in with `huggingface-cli ''' '''login`.''' ) except RevisionNotFoundError: raise EnvironmentError( F'''{revision} is not a valid git identifier (branch name, tag name or commit id) that exists for ''' '''this model name. Check the model page at ''' F'''\'https://huggingface.co/{pretrained_model_name_or_path}\' for available revisions.''' ) except EntryNotFoundError: raise EnvironmentError( F'''{pretrained_model_name_or_path} does not appear to have a file named {weights_name}.''' ) except HTTPError as err: raise EnvironmentError( F'''There was a specific connection error when trying to load {pretrained_model_name_or_path}:\n{err}''' ) except ValueError: raise EnvironmentError( F'''We couldn\'t connect to \'{HUGGINGFACE_CO_RESOLVE_ENDPOINT}\' to load this model, couldn\'t find it''' F''' in the cached files and it looks like {pretrained_model_name_or_path} is not the path to a''' F''' directory containing a file named {weights_name} or''' ''' \nCheckout your internet connection or see how to run the library in''' ''' offline mode at \'https://huggingface.co/docs/diffusers/installation#offline-mode\'.''' ) except EnvironmentError: raise EnvironmentError( F'''Can\'t load the model for \'{pretrained_model_name_or_path}\'. If you were trying to load it from ''' '''\'https://huggingface.co/models\', make sure you don\'t have a local directory with the same name. ''' F'''Otherwise, make sure \'{pretrained_model_name_or_path}\' is the correct path to a directory ''' F'''containing a file named {weights_name}''' )
39
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available lowerCAmelCase_ = { '''configuration_pix2struct''': [ '''PIX2STRUCT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''Pix2StructConfig''', '''Pix2StructTextConfig''', '''Pix2StructVisionConfig''', ], '''processing_pix2struct''': ['''Pix2StructProcessor'''], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ = ['''Pix2StructImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ = [ '''PIX2STRUCT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''Pix2StructPreTrainedModel''', '''Pix2StructForConditionalGeneration''', '''Pix2StructVisionModel''', '''Pix2StructTextModel''', ] if TYPE_CHECKING: from .configuration_pixastruct import ( PIX2STRUCT_PRETRAINED_CONFIG_ARCHIVE_MAP, PixaStructConfig, PixaStructTextConfig, PixaStructVisionConfig, ) from .processing_pixastruct import PixaStructProcessor try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_pixastruct import PixaStructImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_pixastruct import ( PIX2STRUCT_PRETRAINED_MODEL_ARCHIVE_LIST, PixaStructForConditionalGeneration, PixaStructPreTrainedModel, PixaStructTextModel, PixaStructVisionModel, ) else: import sys lowerCAmelCase_ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
39
import copy from ...configuration_utils import PretrainedConfig from ...utils import logging from ..bit import BitConfig lowerCAmelCase_ = logging.get_logger(__name__) lowerCAmelCase_ = { '''Intel/dpt-large''': '''https://huggingface.co/Intel/dpt-large/resolve/main/config.json''', # See all DPT models at https://huggingface.co/models?filter=dpt } class snake_case_ ( __A ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = "dpt" def __init__( self : Optional[Any] , _UpperCamelCase : Tuple=7_6_8 , _UpperCamelCase : Dict=1_2 , _UpperCamelCase : Union[str, Any]=1_2 , _UpperCamelCase : List[Any]=3_0_7_2 , _UpperCamelCase : Dict="gelu" , _UpperCamelCase : Union[str, Any]=0.0 , _UpperCamelCase : Optional[int]=0.0 , _UpperCamelCase : Optional[int]=0.02 , _UpperCamelCase : List[str]=1e-12 , _UpperCamelCase : Any=3_8_4 , _UpperCamelCase : int=1_6 , _UpperCamelCase : Any=3 , _UpperCamelCase : Dict=False , _UpperCamelCase : str=True , _UpperCamelCase : Union[str, Any]=[2, 5, 8, 1_1] , _UpperCamelCase : List[str]="project" , _UpperCamelCase : Optional[int]=[4, 2, 1, 0.5] , _UpperCamelCase : Dict=[9_6, 1_9_2, 3_8_4, 7_6_8] , _UpperCamelCase : Dict=2_5_6 , _UpperCamelCase : Optional[Any]=-1 , _UpperCamelCase : int=False , _UpperCamelCase : Optional[int]=True , _UpperCamelCase : str=0.4 , _UpperCamelCase : Tuple=2_5_5 , _UpperCamelCase : Union[str, Any]=0.1 , _UpperCamelCase : Tuple=[1, 1_0_2_4, 2_4, 2_4] , _UpperCamelCase : List[str]=[0, 1] , _UpperCamelCase : List[Any]=None , **_UpperCamelCase : Dict , ) ->Any: super().__init__(**_UpperCamelCase ) snake_case_ = hidden_size snake_case_ = is_hybrid if self.is_hybrid: if backbone_config is None: logger.info('''Initializing the config with a `BiT` backbone.''' ) snake_case_ = { '''global_padding''': '''same''', '''layer_type''': '''bottleneck''', '''depths''': [3, 4, 9], '''out_features''': ['''stage1''', '''stage2''', '''stage3'''], '''embedding_dynamic_padding''': True, } snake_case_ = BitConfig(**_UpperCamelCase ) elif isinstance(_UpperCamelCase , _UpperCamelCase ): logger.info('''Initializing the config with a `BiT` backbone.''' ) snake_case_ = BitConfig(**_UpperCamelCase ) elif isinstance(_UpperCamelCase , _UpperCamelCase ): snake_case_ = backbone_config else: raise ValueError( f'''backbone_config must be a dictionary or a `PretrainedConfig`, got {backbone_config.__class__}.''' ) snake_case_ = backbone_featmap_shape snake_case_ = neck_ignore_stages if readout_type != "project": raise ValueError('''Readout type must be \'project\' when using `DPT-hybrid` mode.''' ) else: snake_case_ = None snake_case_ = None snake_case_ = [] snake_case_ = num_hidden_layers snake_case_ = num_attention_heads snake_case_ = intermediate_size snake_case_ = hidden_act snake_case_ = hidden_dropout_prob snake_case_ = attention_probs_dropout_prob snake_case_ = initializer_range snake_case_ = layer_norm_eps snake_case_ = image_size snake_case_ = patch_size snake_case_ = num_channels snake_case_ = qkv_bias snake_case_ = backbone_out_indices if readout_type not in ["ignore", "add", "project"]: raise ValueError('''Readout_type must be one of [\'ignore\', \'add\', \'project\']''' ) snake_case_ = readout_type snake_case_ = reassemble_factors snake_case_ = neck_hidden_sizes snake_case_ = fusion_hidden_size snake_case_ = head_in_index snake_case_ = use_batch_norm_in_fusion_residual # auxiliary head attributes (semantic segmentation) snake_case_ = use_auxiliary_head snake_case_ = auxiliary_loss_weight snake_case_ = semantic_loss_ignore_index snake_case_ = semantic_classifier_dropout def snake_case__( self : List[str] ) ->List[Any]: snake_case_ = copy.deepcopy(self.__dict__ ) if output["backbone_config"] is not None: snake_case_ = self.backbone_config.to_dict() snake_case_ = self.__class__.model_type return output
39
1
from dataclasses import dataclass, field from typing import Tuple from ..utils import cached_property, is_tf_available, logging, requires_backends from .benchmark_args_utils import BenchmarkArguments if is_tf_available(): import tensorflow as tf lowerCAmelCase_ = logging.get_logger(__name__) @dataclass class snake_case_ ( __A ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = [ "no_inference", "no_cuda", "no_tpu", "no_speed", "no_memory", "no_env_print", "no_multi_process", ] def __init__( self : Dict , **_UpperCamelCase : List[Any] ) ->Any: for deprecated_arg in self.deprecated_args: if deprecated_arg in kwargs: snake_case_ = deprecated_arg[3:] snake_case_ = not kwargs.pop(_UpperCamelCase ) logger.warning( f'''{deprecated_arg} is depreciated. Please use --no-{positive_arg} or''' f''' {positive_arg}={kwargs[positive_arg]}''' ) snake_case_ = kwargs.pop('''tpu_name''' , self.tpu_name ) snake_case_ = kwargs.pop('''device_idx''' , self.device_idx ) snake_case_ = kwargs.pop('''eager_mode''' , self.eager_mode ) snake_case_ = kwargs.pop('''use_xla''' , self.use_xla ) super().__init__(**_UpperCamelCase ) SCREAMING_SNAKE_CASE : str = field( default=__A , metadata={"help": "Name of TPU"} , ) SCREAMING_SNAKE_CASE : int = field( default=0 , metadata={"help": "CPU / GPU device index. Defaults to 0."} , ) SCREAMING_SNAKE_CASE : bool = field(default=__A , metadata={"help": "Benchmark models in eager model."} ) SCREAMING_SNAKE_CASE : bool = field( default=__A , metadata={ "help": "Benchmark models using XLA JIT compilation. Note that `eager_model` has to be set to `False`." } , ) @cached_property def snake_case__( self : Optional[Any] ) ->Tuple["tf.distribute.cluster_resolver.TPUClusterResolver"]: requires_backends(self , ['''tf'''] ) snake_case_ = None if self.tpu: try: if self.tpu_name: snake_case_ = tf.distribute.cluster_resolver.TPUClusterResolver(self.tpu_name ) else: snake_case_ = tf.distribute.cluster_resolver.TPUClusterResolver() except ValueError: snake_case_ = None return tpu @cached_property def snake_case__( self : Tuple ) ->Tuple["tf.distribute.Strategy", "tf.distribute.cluster_resolver.TPUClusterResolver"]: requires_backends(self , ['''tf'''] ) if self.is_tpu: tf.config.experimental_connect_to_cluster(self._setup_tpu ) tf.tpu.experimental.initialize_tpu_system(self._setup_tpu ) snake_case_ = tf.distribute.TPUStrategy(self._setup_tpu ) else: # currently no multi gpu is allowed if self.is_gpu: # TODO: Currently only single GPU is supported tf.config.set_visible_devices(self.gpu_list[self.device_idx] , '''GPU''' ) snake_case_ = tf.distribute.OneDeviceStrategy(device=f'''/gpu:{self.device_idx}''' ) else: tf.config.set_visible_devices([] , '''GPU''' ) # disable GPU snake_case_ = tf.distribute.OneDeviceStrategy(device=f'''/cpu:{self.device_idx}''' ) return strategy @property def snake_case__( self : Dict ) ->bool: requires_backends(self , ['''tf'''] ) return self._setup_tpu is not None @property def snake_case__( self : Union[str, Any] ) ->"tf.distribute.Strategy": requires_backends(self , ['''tf'''] ) return self._setup_strategy @property def snake_case__( self : Optional[Any] ) ->Optional[Any]: requires_backends(self , ['''tf'''] ) return tf.config.list_physical_devices('''GPU''' ) @property def snake_case__( self : Dict ) ->int: requires_backends(self , ['''tf'''] ) if self.cuda: return len(self.gpu_list ) return 0 @property def snake_case__( self : Union[str, Any] ) ->bool: return self.n_gpu > 0
39
import argparse import dataclasses import json import logging import os import shutil from typing import List, Optional import datasets from accelerate import Accelerator from datasets import load_dataset from finetuning import finetune from tqdm.auto import tqdm import transformers from transformers import AutoConfig, set_seed from transformers.trainer_utils import IntervalStrategy lowerCAmelCase_ = logging.getLogger(__name__) lowerCAmelCase_ = '''pytorch_model.bin''' @dataclasses.dataclass class snake_case_ : '''simple docstring''' SCREAMING_SNAKE_CASE : str = dataclasses.field( metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models."} ) SCREAMING_SNAKE_CASE : Optional[str] = dataclasses.field( default=__A , metadata={"help": "Where do you want to store the pretrained models downloaded from huggingface.co."} , ) @dataclasses.dataclass class snake_case_ : '''simple docstring''' SCREAMING_SNAKE_CASE : str = dataclasses.field(metadata={"help": "A csv or a json file containing the training data."} ) SCREAMING_SNAKE_CASE : str = dataclasses.field(metadata={"help": "A csv or a json file containing the data to predict on."} ) SCREAMING_SNAKE_CASE : Optional[str] = dataclasses.field( default=__A , metadata={"help": "A csv or a json file containing the validation data."} ) SCREAMING_SNAKE_CASE : Optional[str] = dataclasses.field( default=__A , metadata={"help": "The name of the task to train on."} , ) SCREAMING_SNAKE_CASE : Optional[List[str]] = dataclasses.field( default=__A , metadata={"help": "The list of labels for the task."} ) @dataclasses.dataclass class snake_case_ : '''simple docstring''' SCREAMING_SNAKE_CASE : str = dataclasses.field( metadata={"help": "The output directory where the model predictions and checkpoints will be written."} ) SCREAMING_SNAKE_CASE : Optional[str] = dataclasses.field( default="accuracy" , metadata={"help": "The evaluation metric used for the task."} ) SCREAMING_SNAKE_CASE : Optional[str] = dataclasses.field( default="no" , metadata={ "help": "The evaluation strategy to adopt during training. Possible values are: [\"no\", \"step\", \"epoch]" } , ) SCREAMING_SNAKE_CASE : Optional[int] = dataclasses.field( default=10 , metadata={"help": "Number of evaluation calls with no improvement after which training will be stopped."} , ) SCREAMING_SNAKE_CASE : Optional[float] = dataclasses.field( default=0.0 , metadata={ "help": "How much the specified evaluation metric must improve to satisfy early stopping conditions." } , ) SCREAMING_SNAKE_CASE : Optional[bool] = dataclasses.field( default=__A , metadata={"help": "Whether to filter the pseudo-labeled data based on the confidence score."} , ) SCREAMING_SNAKE_CASE : Optional[bool] = dataclasses.field( default=__A , metadata={"help": "Whether to filter the pseudo-labeled data based on the validation performance."} , ) SCREAMING_SNAKE_CASE : Optional[bool] = dataclasses.field( default=__A , metadata={"help": "Whether to fine-tune on labeled data after pseudo training."} , ) SCREAMING_SNAKE_CASE : Optional[float] = dataclasses.field( default=0.0 , metadata={"help": "Confidence threshold for pseudo-labeled data filtering."} , ) SCREAMING_SNAKE_CASE : Optional[int] = dataclasses.field( default=100 , metadata={"help": "Number of evaluation calls with no improvement after which training will be stopped."} , ) SCREAMING_SNAKE_CASE : Optional[int] = dataclasses.field( default=__A , metadata={"help": "Random seed for initialization."} , ) def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): snake_case_ = datasets.concatenate_datasets([infer_input, infer_output] , axis=1 ) if args.do_filter_by_confidence: snake_case_ = dataset.filter(lambda SCREAMING_SNAKE_CASE__ : example["probability"] > args.confidence_threshold ) if args.do_filter_by_val_performance: assert eval_result >= 0.0 and eval_result <= 1.0 snake_case_ = int(eval_result * len(SCREAMING_SNAKE_CASE__ ) ) print(SCREAMING_SNAKE_CASE__ ) snake_case_ = dataset.sort('''probability''' , reverse=SCREAMING_SNAKE_CASE__ ) snake_case_ = dataset.select(range(SCREAMING_SNAKE_CASE__ ) ) snake_case_ = dataset.remove_columns(['''label''', '''probability'''] ) snake_case_ = dataset.rename_column('''prediction''' , '''label''' ) snake_case_ = dataset.map(lambda SCREAMING_SNAKE_CASE__ : {"label": idalabel[example["label"]]} ) snake_case_ = dataset.shuffle(seed=args.seed ) snake_case_ = os.path.join(SCREAMING_SNAKE_CASE__ , F'''train_pseudo.{args.data_file_extension}''' ) if args.data_file_extension == "csv": dataset.to_csv(SCREAMING_SNAKE_CASE__ , index=SCREAMING_SNAKE_CASE__ ) else: dataset.to_json(SCREAMING_SNAKE_CASE__ ) def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ): snake_case_ = Accelerator() # Make one log on every process with the configuration for debugging. logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''' , datefmt='''%m/%d/%Y %H:%M:%S''' , level=logging.INFO , ) logger.info(accelerator.state ) # Setup logging, we only want one process per machine to log things on the # screen. accelerator.is_local_main_process is only True for one process per # machine. logger.setLevel(logging.INFO if accelerator.is_local_main_process else logging.ERROR ) if accelerator.is_local_main_process: datasets.utils.logging.set_verbosity_warning() transformers.utils.logging.set_verbosity_info() else: datasets.utils.logging.set_verbosity_error() transformers.utils.logging.set_verbosity_error() snake_case_ = STModelArguments(model_name_or_path=SCREAMING_SNAKE_CASE__ ) snake_case_ = STDataArguments(train_file=SCREAMING_SNAKE_CASE__ , infer_file=SCREAMING_SNAKE_CASE__ ) snake_case_ = STTrainingArguments(output_dir=SCREAMING_SNAKE_CASE__ ) snake_case_ = argparse.Namespace() for arg_class in (model_args, data_args, training_args): for key, value in vars(SCREAMING_SNAKE_CASE__ ).items(): setattr(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) for key, value in kwargs.items(): if hasattr(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): setattr(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) # Sanity checks snake_case_ = {} snake_case_ = None # You need to provide the training data and the data to predict on assert args.train_file is not None assert args.infer_file is not None snake_case_ = args.train_file snake_case_ = args.infer_file if args.evaluation_strategy != IntervalStrategy.NO.value: assert args.eval_file is not None snake_case_ = args.eval_file for key in data_files: snake_case_ = data_files[key].split('''.''' )[-1] assert extension in ["csv", "json"], F'''`{key}_file` should be a csv or a json file.''' if args.data_file_extension is None: snake_case_ = extension else: assert extension == args.data_file_extension, F'''`{key}_file` should be a {args.data_file_extension} file`.''' assert ( args.eval_metric in datasets.list_metrics() ), F'''{args.eval_metric} not in the list of supported metrics {datasets.list_metrics()}.''' # If passed along, set the training seed now. if args.seed is not None: set_seed(args.seed ) logger.info('''Creating the initial data directory for self-training...''' ) snake_case_ = F'''{args.output_dir}/self-train_iter-{{}}'''.format snake_case_ = data_dir_format(0 ) if accelerator.is_main_process: if args.output_dir is not None: os.makedirs(args.output_dir , exist_ok=SCREAMING_SNAKE_CASE__ ) os.makedirs(SCREAMING_SNAKE_CASE__ , exist_ok=SCREAMING_SNAKE_CASE__ ) accelerator.wait_for_everyone() snake_case_ = None snake_case_ = None snake_case_ = 0 snake_case_ = False # Show the progress bar snake_case_ = tqdm(range(args.max_selftrain_iterations ) , disable=not accelerator.is_local_main_process ) # Self-train for iteration in range(0 , int(args.max_selftrain_iterations ) ): snake_case_ = data_dir_format(SCREAMING_SNAKE_CASE__ ) assert os.path.exists(SCREAMING_SNAKE_CASE__ ) # Stage 1: initial fine-tuning for iteration = 0 or pseudo-training for # iteration > 0 snake_case_ = os.path.join(SCREAMING_SNAKE_CASE__ , '''stage-1''' ) snake_case_ = { '''accelerator''': accelerator, '''model_name_or_path''': args.model_name_or_path, '''cache_dir''': args.cache_dir, '''do_train''': True, '''train_file''': data_files['''train'''] if iteration == 0 else data_files['''train_pseudo'''], '''do_eval''': True if args.eval_file is not None else False, '''eval_file''': data_files['''eval'''], '''do_predict''': True, '''infer_file''': data_files['''infer'''], '''task_name''': args.task_name, '''label_list''': args.label_list, '''output_dir''': current_output_dir, '''eval_metric''': args.eval_metric, '''evaluation_strategy''': args.evaluation_strategy, '''early_stopping_patience''': args.early_stopping_patience, '''early_stopping_threshold''': args.early_stopping_threshold, '''seed''': args.seed, } # Add additional training arguments for key, value in kwargs.items(): if key not in arguments_dict and not hasattr(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): arguments_dict.update({key: value} ) snake_case_ = os.path.join(SCREAMING_SNAKE_CASE__ , '''best-checkpoint''' , SCREAMING_SNAKE_CASE__ ) if os.path.exists(SCREAMING_SNAKE_CASE__ ): logger.info( '''Found existing model checkpoint at %s. Skipping self-training: iteration: %d, stage: 1.''' , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , ) else: logger.info('''***** Running self-training: iteration: %d, stage: 1 *****''' , SCREAMING_SNAKE_CASE__ ) finetune(**SCREAMING_SNAKE_CASE__ ) accelerator.wait_for_everyone() assert os.path.exists(SCREAMING_SNAKE_CASE__ ) logger.info('''Self-training job completed: iteration: %d, stage: 1.''' , SCREAMING_SNAKE_CASE__ ) if iteration > 0 and args.finetune_on_labeled_data: # Stage 2 (optional): fine-tuning on the original labeled data snake_case_ = os.path.join(SCREAMING_SNAKE_CASE__ , '''best-checkpoint''' ) snake_case_ = os.path.join(SCREAMING_SNAKE_CASE__ , '''stage-2''' ) # Update arguments_dict snake_case_ = model_path snake_case_ = data_files['''train'''] snake_case_ = current_output_dir snake_case_ = os.path.join(SCREAMING_SNAKE_CASE__ , '''best-checkpoint''' , SCREAMING_SNAKE_CASE__ ) if os.path.exists(SCREAMING_SNAKE_CASE__ ): logger.info( '''Found existing model checkpoint at %s. Skipping self-training: iteration: %d, stage: 2.''' , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , ) else: logger.info('''***** Running self-training: iteration: %d, stage: 2 *****''' , SCREAMING_SNAKE_CASE__ ) finetune(**SCREAMING_SNAKE_CASE__ ) accelerator.wait_for_everyone() assert os.path.exists(SCREAMING_SNAKE_CASE__ ) logger.info('''Self-training job completed: iteration: %d, stage: 2.''' , SCREAMING_SNAKE_CASE__ ) snake_case_ = iteration snake_case_ = data_dir_format(iteration + 1 ) snake_case_ = AutoConfig.from_pretrained(os.path.join(SCREAMING_SNAKE_CASE__ , '''best-checkpoint''' ) ) snake_case_ = config.idalabel snake_case_ = os.path.join(SCREAMING_SNAKE_CASE__ , '''eval_results_best-checkpoint.json''' ) snake_case_ = os.path.join(SCREAMING_SNAKE_CASE__ , '''test_results_best-checkpoint.json''' ) assert os.path.exists(SCREAMING_SNAKE_CASE__ ) with open(SCREAMING_SNAKE_CASE__ , '''r''' ) as f: snake_case_ = float(json.load(SCREAMING_SNAKE_CASE__ )[args.eval_metric] ) snake_case_ = os.path.join(SCREAMING_SNAKE_CASE__ , '''infer_output_best-checkpoint.csv''' ) assert os.path.exists(SCREAMING_SNAKE_CASE__ ) # Loading the dataset from local csv or json files. snake_case_ = load_dataset(args.data_file_extension , data_files={'''data''': data_files['''infer''']} )['''data'''] snake_case_ = load_dataset('''csv''' , data_files={'''data''': infer_output_file} )['''data'''] if accelerator.is_main_process: os.makedirs(SCREAMING_SNAKE_CASE__ , exist_ok=SCREAMING_SNAKE_CASE__ ) shutil.copy(SCREAMING_SNAKE_CASE__ , os.path.join(SCREAMING_SNAKE_CASE__ , F'''eval_results_iter-{iteration}.json''' ) ) if os.path.exists(SCREAMING_SNAKE_CASE__ ): shutil.copy(SCREAMING_SNAKE_CASE__ , os.path.join(SCREAMING_SNAKE_CASE__ , F'''test_results_iter-{iteration}.json''' ) ) create_pseudo_labeled_data(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) accelerator.wait_for_everyone() snake_case_ = os.path.join(SCREAMING_SNAKE_CASE__ , F'''train_pseudo.{args.data_file_extension}''' ) if args.evaluation_strategy != IntervalStrategy.NO.value: snake_case_ = eval_result if best_iteration is None: snake_case_ = new_iteration snake_case_ = new_eval_result else: if new_eval_result - best_eval_result > args.early_stopping_threshold: snake_case_ = new_iteration snake_case_ = new_eval_result snake_case_ = 0 else: if new_eval_result == best_eval_result: snake_case_ = new_iteration snake_case_ = new_eval_result early_stopping_patience_counter += 1 if early_stopping_patience_counter >= args.early_stopping_patience: snake_case_ = True progress_bar.update(1 ) if should_training_stop: break if best_iteration is not None: # Save the best iteration logger.info('''Best iteration: %d''' , SCREAMING_SNAKE_CASE__ ) logger.info('''Best evaluation result: %s = %f''' , args.eval_metric , SCREAMING_SNAKE_CASE__ ) accelerator.wait_for_everyone() if accelerator.is_main_process: shutil.copy( os.path.join(SCREAMING_SNAKE_CASE__ , F'''eval_results_iter-{iteration}.json''' ) , os.path.join(SCREAMING_SNAKE_CASE__ , '''eval_results_best-iteration.json''' ) , ) else: # Assume that the last iteration is the best logger.info('''Best iteration: %d''' , args.max_selftrain_iterations - 1 ) logger.info('''Best evaluation result: %s = %f''' , args.eval_metric , SCREAMING_SNAKE_CASE__ ) accelerator.wait_for_everyone() if accelerator.is_main_process: shutil.copy( os.path.join(SCREAMING_SNAKE_CASE__ , F'''eval_results_iter-{args.max_selftrain_iterations - 1}.json''' ) , os.path.join(SCREAMING_SNAKE_CASE__ , '''eval_results_best-iteration.json''' ) , )
39
1
from __future__ import annotations from collections import deque from collections.abc import Sequence from dataclasses import dataclass from typing import Any @dataclass class snake_case_ : '''simple docstring''' SCREAMING_SNAKE_CASE : int SCREAMING_SNAKE_CASE : Node | None = None SCREAMING_SNAKE_CASE : Node | None = None def __SCREAMING_SNAKE_CASE (): snake_case_ = Node(1 ) snake_case_ = Node(2 ) snake_case_ = Node(3 ) snake_case_ = Node(4 ) snake_case_ = Node(5 ) return tree def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): return [root.data, *preorder(root.left ), *preorder(root.right )] if root else [] def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): return postorder(root.left ) + postorder(root.right ) + [root.data] if root else [] def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): return [*inorder(root.left ), root.data, *inorder(root.right )] if root else [] def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): return (max(height(root.left ) , height(root.right ) ) + 1) if root else 0 def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): snake_case_ = [] if root is None: return output snake_case_ = deque([root] ) while process_queue: snake_case_ = process_queue.popleft() output.append(node.data ) if node.left: process_queue.append(node.left ) if node.right: process_queue.append(node.right ) return output def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): snake_case_ = [] def populate_output(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> None: if not root: return if level == 1: output.append(root.data ) elif level > 1: populate_output(root.left , level - 1 ) populate_output(root.right , level - 1 ) populate_output(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) return output def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): snake_case_ = [] def populate_output(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> None: if root is None: return if level == 1: output.append(root.data ) elif level > 1: populate_output(root.right , level - 1 ) populate_output(root.left , level - 1 ) populate_output(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) return output def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): if root is None: return [] snake_case_ = [] snake_case_ = 0 snake_case_ = height(SCREAMING_SNAKE_CASE__ ) for h in range(1 , height_tree + 1 ): if not flag: output.append(get_nodes_from_left_to_right(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) ) snake_case_ = 1 else: output.append(get_nodes_from_right_to_left(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) ) snake_case_ = 0 return output def __SCREAMING_SNAKE_CASE (): # Main function for testing. snake_case_ = make_tree() print(F'''In-order Traversal: {inorder(SCREAMING_SNAKE_CASE__ )}''' ) print(F'''Pre-order Traversal: {preorder(SCREAMING_SNAKE_CASE__ )}''' ) print(F'''Post-order Traversal: {postorder(SCREAMING_SNAKE_CASE__ )}''' , '''\n''' ) print(F'''Height of Tree: {height(SCREAMING_SNAKE_CASE__ )}''' , '''\n''' ) print('''Complete Level Order Traversal: ''' ) print(level_order(SCREAMING_SNAKE_CASE__ ) , '''\n''' ) print('''Level-wise order Traversal: ''' ) for level in range(1 , height(SCREAMING_SNAKE_CASE__ ) + 1 ): print(F'''Level {level}:''' , get_nodes_from_left_to_right(SCREAMING_SNAKE_CASE__ , level=SCREAMING_SNAKE_CASE__ ) ) print('''\nZigZag order Traversal: ''' ) print(zigzag(SCREAMING_SNAKE_CASE__ ) ) if __name__ == "__main__": import doctest doctest.testmod() main()
39
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 snake_case_ ( __A , __A , __A , unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = AltDiffusionPipeline SCREAMING_SNAKE_CASE : Dict = TEXT_TO_IMAGE_PARAMS SCREAMING_SNAKE_CASE : Dict = TEXT_TO_IMAGE_BATCH_PARAMS SCREAMING_SNAKE_CASE : Union[str, Any] = TEXT_TO_IMAGE_IMAGE_PARAMS SCREAMING_SNAKE_CASE : Dict = TEXT_TO_IMAGE_IMAGE_PARAMS def snake_case__( self : Dict ) ->int: torch.manual_seed(0 ) snake_case_ = UNetaDConditionModel( block_out_channels=(3_2, 6_4) , layers_per_block=2 , sample_size=3_2 , in_channels=4 , out_channels=4 , down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''') , up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''') , cross_attention_dim=3_2 , ) snake_case_ = DDIMScheduler( beta_start=0.00085 , beta_end=0.012 , beta_schedule='''scaled_linear''' , clip_sample=_UpperCamelCase , set_alpha_to_one=_UpperCamelCase , ) torch.manual_seed(0 ) snake_case_ = AutoencoderKL( block_out_channels=[3_2, 6_4] , 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 ) snake_case_ = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=3_2 , projection_dim=3_2 , intermediate_size=3_7 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=5_0_0_2 , ) snake_case_ = CLIPTextModel(_UpperCamelCase ) snake_case_ = XLMRobertaTokenizer.from_pretrained('''hf-internal-testing/tiny-xlm-roberta''' ) snake_case_ = 7_7 snake_case_ = { '''unet''': unet, '''scheduler''': scheduler, '''vae''': vae, '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''safety_checker''': None, '''feature_extractor''': None, } return components def snake_case__( self : str , _UpperCamelCase : Optional[int] , _UpperCamelCase : Dict=0 ) ->Any: if str(_UpperCamelCase ).startswith('''mps''' ): snake_case_ = torch.manual_seed(_UpperCamelCase ) else: snake_case_ = torch.Generator(device=_UpperCamelCase ).manual_seed(_UpperCamelCase ) snake_case_ = { '''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 snake_case__( self : Dict ) ->List[str]: super().test_attention_slicing_forward_pass(expected_max_diff=3e-3 ) def snake_case__( self : List[str] ) ->Any: super().test_inference_batch_single_identical(expected_max_diff=3e-3 ) def snake_case__( self : Dict ) ->Any: snake_case_ = '''cpu''' # ensure determinism for the device-dependent torch.Generator snake_case_ = self.get_dummy_components() torch.manual_seed(0 ) snake_case_ = RobertaSeriesConfig( hidden_size=3_2 , project_dim=3_2 , intermediate_size=3_7 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , vocab_size=5_0_0_2 , ) # TODO: remove after fixing the non-deterministic text encoder snake_case_ = RobertaSeriesModelWithTransformation(_UpperCamelCase ) snake_case_ = text_encoder snake_case_ = AltDiffusionPipeline(**_UpperCamelCase ) snake_case_ = alt_pipe.to(_UpperCamelCase ) alt_pipe.set_progress_bar_config(disable=_UpperCamelCase ) snake_case_ = self.get_dummy_inputs(_UpperCamelCase ) snake_case_ = '''A photo of an astronaut''' snake_case_ = alt_pipe(**_UpperCamelCase ) snake_case_ = output.images snake_case_ = image[0, -3:, -3:, -1] assert image.shape == (1, 6_4, 6_4, 3) snake_case_ = 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 snake_case__( self : Tuple ) ->Union[str, Any]: snake_case_ = '''cpu''' # ensure determinism for the device-dependent torch.Generator snake_case_ = self.get_dummy_components() snake_case_ = PNDMScheduler(skip_prk_steps=_UpperCamelCase ) torch.manual_seed(0 ) snake_case_ = RobertaSeriesConfig( hidden_size=3_2 , project_dim=3_2 , intermediate_size=3_7 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , vocab_size=5_0_0_2 , ) # TODO: remove after fixing the non-deterministic text encoder snake_case_ = RobertaSeriesModelWithTransformation(_UpperCamelCase ) snake_case_ = text_encoder snake_case_ = AltDiffusionPipeline(**_UpperCamelCase ) snake_case_ = alt_pipe.to(_UpperCamelCase ) alt_pipe.set_progress_bar_config(disable=_UpperCamelCase ) snake_case_ = self.get_dummy_inputs(_UpperCamelCase ) snake_case_ = alt_pipe(**_UpperCamelCase ) snake_case_ = output.images snake_case_ = image[0, -3:, -3:, -1] assert image.shape == (1, 6_4, 6_4, 3) snake_case_ = 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 snake_case_ ( unittest.TestCase ): '''simple docstring''' def snake_case__( self : int ) ->List[str]: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def snake_case__( self : List[str] ) ->Tuple: # make sure here that pndm scheduler skips prk snake_case_ = AltDiffusionPipeline.from_pretrained('''BAAI/AltDiffusion''' , safety_checker=_UpperCamelCase ) snake_case_ = alt_pipe.to(_UpperCamelCase ) alt_pipe.set_progress_bar_config(disable=_UpperCamelCase ) snake_case_ = '''A painting of a squirrel eating a burger''' snake_case_ = torch.manual_seed(0 ) snake_case_ = alt_pipe([prompt] , generator=_UpperCamelCase , guidance_scale=6.0 , num_inference_steps=2_0 , output_type='''np''' ) snake_case_ = output.images snake_case_ = image[0, -3:, -3:, -1] assert image.shape == (1, 5_1_2, 5_1_2, 3) snake_case_ = 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 snake_case__( self : List[str] ) ->Optional[Any]: snake_case_ = DDIMScheduler.from_pretrained('''BAAI/AltDiffusion''' , subfolder='''scheduler''' ) snake_case_ = AltDiffusionPipeline.from_pretrained('''BAAI/AltDiffusion''' , scheduler=_UpperCamelCase , safety_checker=_UpperCamelCase ) snake_case_ = alt_pipe.to(_UpperCamelCase ) alt_pipe.set_progress_bar_config(disable=_UpperCamelCase ) snake_case_ = '''A painting of a squirrel eating a burger''' snake_case_ = torch.manual_seed(0 ) snake_case_ = alt_pipe([prompt] , generator=_UpperCamelCase , num_inference_steps=2 , output_type='''numpy''' ) snake_case_ = output.images snake_case_ = image[0, -3:, -3:, -1] assert image.shape == (1, 5_1_2, 5_1_2, 3) snake_case_ = 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
39
1
import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import AutoencoderKL, DDIMScheduler, LDMTextToImagePipeline, UNetaDConditionModel from diffusers.utils.testing_utils import ( enable_full_determinism, load_numpy, nightly, require_torch_gpu, slow, torch_device, ) from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class snake_case_ ( __A , unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = LDMTextToImagePipeline SCREAMING_SNAKE_CASE : Tuple = TEXT_TO_IMAGE_PARAMS - { "negative_prompt", "negative_prompt_embeds", "cross_attention_kwargs", "prompt_embeds", } SCREAMING_SNAKE_CASE : Tuple = PipelineTesterMixin.required_optional_params - { "num_images_per_prompt", "callback", "callback_steps", } SCREAMING_SNAKE_CASE : int = TEXT_TO_IMAGE_BATCH_PARAMS SCREAMING_SNAKE_CASE : int = False def snake_case__( self : Optional[Any] ) ->List[str]: torch.manual_seed(0 ) snake_case_ = UNetaDConditionModel( block_out_channels=(3_2, 6_4) , layers_per_block=2 , sample_size=3_2 , in_channels=4 , out_channels=4 , down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''') , up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''') , cross_attention_dim=3_2 , ) snake_case_ = DDIMScheduler( beta_start=0.00085 , beta_end=0.012 , beta_schedule='''scaled_linear''' , clip_sample=_UpperCamelCase , set_alpha_to_one=_UpperCamelCase , ) torch.manual_seed(0 ) snake_case_ = AutoencoderKL( block_out_channels=(3_2, 6_4) , in_channels=3 , out_channels=3 , down_block_types=('''DownEncoderBlock2D''', '''DownEncoderBlock2D''') , up_block_types=('''UpDecoderBlock2D''', '''UpDecoderBlock2D''') , latent_channels=4 , ) torch.manual_seed(0 ) snake_case_ = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=3_2 , intermediate_size=3_7 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1_0_0_0 , ) snake_case_ = CLIPTextModel(_UpperCamelCase ) snake_case_ = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) snake_case_ = { '''unet''': unet, '''scheduler''': scheduler, '''vqvae''': vae, '''bert''': text_encoder, '''tokenizer''': tokenizer, } return components def snake_case__( self : int , _UpperCamelCase : int , _UpperCamelCase : List[Any]=0 ) ->Any: if str(_UpperCamelCase ).startswith('''mps''' ): snake_case_ = torch.manual_seed(_UpperCamelCase ) else: snake_case_ = torch.Generator(device=_UpperCamelCase ).manual_seed(_UpperCamelCase ) snake_case_ = { '''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 snake_case__( self : str ) ->List[Any]: snake_case_ = '''cpu''' # ensure determinism for the device-dependent torch.Generator snake_case_ = self.get_dummy_components() snake_case_ = LDMTextToImagePipeline(**_UpperCamelCase ) pipe.to(_UpperCamelCase ) pipe.set_progress_bar_config(disable=_UpperCamelCase ) snake_case_ = self.get_dummy_inputs(_UpperCamelCase ) snake_case_ = pipe(**_UpperCamelCase ).images snake_case_ = image[0, -3:, -3:, -1] assert image.shape == (1, 1_6, 1_6, 3) snake_case_ = np.array([0.6101, 0.6156, 0.5622, 0.4895, 0.6661, 0.3804, 0.5748, 0.6136, 0.5014] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3 @slow @require_torch_gpu class snake_case_ ( unittest.TestCase ): '''simple docstring''' def snake_case__( self : Optional[int] ) ->str: super().tearDown() gc.collect() torch.cuda.empty_cache() def snake_case__( self : Tuple , _UpperCamelCase : List[Any] , _UpperCamelCase : List[str]=torch.floataa , _UpperCamelCase : List[str]=0 ) ->Tuple: snake_case_ = torch.manual_seed(_UpperCamelCase ) snake_case_ = np.random.RandomState(_UpperCamelCase ).standard_normal((1, 4, 3_2, 3_2) ) snake_case_ = torch.from_numpy(_UpperCamelCase ).to(device=_UpperCamelCase , dtype=_UpperCamelCase ) snake_case_ = { '''prompt''': '''A painting of a squirrel eating a burger''', '''latents''': latents, '''generator''': generator, '''num_inference_steps''': 3, '''guidance_scale''': 6.0, '''output_type''': '''numpy''', } return inputs def snake_case__( self : Any ) ->int: snake_case_ = LDMTextToImagePipeline.from_pretrained('''CompVis/ldm-text2im-large-256''' ).to(_UpperCamelCase ) pipe.set_progress_bar_config(disable=_UpperCamelCase ) snake_case_ = self.get_inputs(_UpperCamelCase ) snake_case_ = pipe(**_UpperCamelCase ).images snake_case_ = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 2_5_6, 2_5_6, 3) snake_case_ = np.array([0.51825, 0.52850, 0.52543, 0.54258, 0.52304, 0.52569, 0.54363, 0.55276, 0.56878] ) snake_case_ = np.abs(expected_slice - image_slice ).max() assert max_diff < 1e-3 @nightly @require_torch_gpu class snake_case_ ( unittest.TestCase ): '''simple docstring''' def snake_case__( self : Dict ) ->List[Any]: super().tearDown() gc.collect() torch.cuda.empty_cache() def snake_case__( self : List[str] , _UpperCamelCase : Optional[int] , _UpperCamelCase : Optional[Any]=torch.floataa , _UpperCamelCase : List[str]=0 ) ->Dict: snake_case_ = torch.manual_seed(_UpperCamelCase ) snake_case_ = np.random.RandomState(_UpperCamelCase ).standard_normal((1, 4, 3_2, 3_2) ) snake_case_ = torch.from_numpy(_UpperCamelCase ).to(device=_UpperCamelCase , dtype=_UpperCamelCase ) snake_case_ = { '''prompt''': '''A painting of a squirrel eating a burger''', '''latents''': latents, '''generator''': generator, '''num_inference_steps''': 5_0, '''guidance_scale''': 6.0, '''output_type''': '''numpy''', } return inputs def snake_case__( self : Optional[int] ) ->List[str]: snake_case_ = LDMTextToImagePipeline.from_pretrained('''CompVis/ldm-text2im-large-256''' ).to(_UpperCamelCase ) pipe.set_progress_bar_config(disable=_UpperCamelCase ) snake_case_ = self.get_inputs(_UpperCamelCase ) snake_case_ = pipe(**_UpperCamelCase ).images[0] snake_case_ = load_numpy( '''https://huggingface.co/datasets/diffusers/test-arrays/resolve/main/ldm_text2img/ldm_large_256_ddim.npy''' ) snake_case_ = np.abs(expected_image - image ).max() assert max_diff < 1e-3
39
from math import factorial def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): # If either of the conditions are true, the function is being asked # to calculate a factorial of a negative number, which is not possible if n < k or k < 0: raise ValueError('''Please enter positive integers for n and k where n >= k''' ) return factorial(SCREAMING_SNAKE_CASE__ ) // (factorial(SCREAMING_SNAKE_CASE__ ) * factorial(n - k )) if __name__ == "__main__": print( '''The number of five-card hands possible from a standard''', f"""fifty-two card deck is: {combinations(52, 5)}\n""", ) print( '''If a class of 40 students must be arranged into groups of''', f"""4 for group projects, there are {combinations(40, 4)} ways""", '''to arrange them.\n''', ) print( '''If 10 teams are competing in a Formula One race, there''', f"""are {combinations(10, 3)} ways that first, second and""", '''third place can be awarded.''', )
39
1
import sys from .dependency_versions_table import deps from .utils.versions import require_version, require_version_core # define which module versions we always want to check at run time # (usually the ones defined in `install_requires` in setup.py) # # order specific notes: # - tqdm must be checked before tokenizers lowerCAmelCase_ = '''python tqdm regex requests packaging filelock numpy tokenizers'''.split() if sys.version_info < (3, 7): pkgs_to_check_at_runtime.append('''dataclasses''') if sys.version_info < (3, 8): pkgs_to_check_at_runtime.append('''importlib_metadata''') for pkg in pkgs_to_check_at_runtime: if pkg in deps: if pkg == "tokenizers": # must be loaded here, or else tqdm check may fail from .utils import is_tokenizers_available if not is_tokenizers_available(): continue # not required, check version only if installed require_version_core(deps[pkg]) else: raise ValueError(f"""can't find {pkg} in {deps.keys()}, check dependency_versions_table.py""") def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__=None ): require_version(deps[pkg] , SCREAMING_SNAKE_CASE__ )
39
import argparse import json import os import sys import tempfile import unittest from argparse import Namespace from dataclasses import dataclass, field from enum import Enum from pathlib import Path from typing import List, Literal, Optional import yaml from transformers import HfArgumentParser, TrainingArguments from transformers.hf_argparser import make_choice_type_function, string_to_bool # Since Python 3.10, we can use the builtin `|` operator for Union types # See PEP 604: https://peps.python.org/pep-0604 lowerCAmelCase_ = sys.version_info >= (3, 10) def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__=None , SCREAMING_SNAKE_CASE__=None ): return field(default_factory=lambda: default , metadata=SCREAMING_SNAKE_CASE__ ) @dataclass class snake_case_ : '''simple docstring''' SCREAMING_SNAKE_CASE : int SCREAMING_SNAKE_CASE : float SCREAMING_SNAKE_CASE : str SCREAMING_SNAKE_CASE : bool @dataclass class snake_case_ : '''simple docstring''' SCREAMING_SNAKE_CASE : int = 42 SCREAMING_SNAKE_CASE : str = field(default="toto" , metadata={"help": "help message"} ) @dataclass class snake_case_ : '''simple docstring''' SCREAMING_SNAKE_CASE : bool = False SCREAMING_SNAKE_CASE : bool = True SCREAMING_SNAKE_CASE : Optional[bool] = None class snake_case_ ( __A ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = "titi" SCREAMING_SNAKE_CASE : Any = "toto" class snake_case_ ( __A ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = "titi" SCREAMING_SNAKE_CASE : Optional[Any] = "toto" SCREAMING_SNAKE_CASE : Any = 42 @dataclass class snake_case_ : '''simple docstring''' SCREAMING_SNAKE_CASE : BasicEnum = "toto" def snake_case__( self : Tuple ) ->List[str]: snake_case_ = BasicEnum(self.foo ) @dataclass class snake_case_ : '''simple docstring''' SCREAMING_SNAKE_CASE : MixedTypeEnum = "toto" def snake_case__( self : Union[str, Any] ) ->Dict: snake_case_ = MixedTypeEnum(self.foo ) @dataclass class snake_case_ : '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = None SCREAMING_SNAKE_CASE : Optional[float] = field(default=__A , metadata={"help": "help message"} ) SCREAMING_SNAKE_CASE : Optional[str] = None SCREAMING_SNAKE_CASE : Optional[List[str]] = list_field(default=[] ) SCREAMING_SNAKE_CASE : Optional[List[int]] = list_field(default=[] ) @dataclass class snake_case_ : '''simple docstring''' SCREAMING_SNAKE_CASE : List[int] = list_field(default=[] ) SCREAMING_SNAKE_CASE : List[int] = list_field(default=[1, 2, 3] ) SCREAMING_SNAKE_CASE : List[str] = list_field(default=["Hallo", "Bonjour", "Hello"] ) SCREAMING_SNAKE_CASE : List[float] = list_field(default=[0.1, 0.2, 0.3] ) @dataclass class snake_case_ : '''simple docstring''' SCREAMING_SNAKE_CASE : List[int] = field() SCREAMING_SNAKE_CASE : str = field() SCREAMING_SNAKE_CASE : BasicEnum = field() def snake_case__( self : Optional[Any] ) ->Tuple: snake_case_ = BasicEnum(self.required_enum ) @dataclass class snake_case_ : '''simple docstring''' SCREAMING_SNAKE_CASE : int SCREAMING_SNAKE_CASE : "BasicEnum" = field() SCREAMING_SNAKE_CASE : "Optional[bool]" = None SCREAMING_SNAKE_CASE : "str" = field(default="toto" , metadata={"help": "help message"} ) SCREAMING_SNAKE_CASE : "List[str]" = list_field(default=["Hallo", "Bonjour", "Hello"] ) if is_python_no_less_than_3_10: @dataclass class snake_case_ : '''simple docstring''' SCREAMING_SNAKE_CASE : bool = False SCREAMING_SNAKE_CASE : bool = True SCREAMING_SNAKE_CASE : bool | None = None @dataclass class snake_case_ : '''simple docstring''' SCREAMING_SNAKE_CASE : int | None = None SCREAMING_SNAKE_CASE : float | None = field(default=__A , metadata={"help": "help message"} ) SCREAMING_SNAKE_CASE : str | None = None SCREAMING_SNAKE_CASE : list[str] | None = list_field(default=[] ) SCREAMING_SNAKE_CASE : list[int] | None = list_field(default=[] ) class snake_case_ ( unittest.TestCase ): '''simple docstring''' def snake_case__( self : Dict , _UpperCamelCase : argparse.ArgumentParser , _UpperCamelCase : argparse.ArgumentParser ) ->str: self.assertEqual(len(a._actions ) , len(b._actions ) ) for x, y in zip(a._actions , b._actions ): snake_case_ = {k: v for k, v in vars(_UpperCamelCase ).items() if k != '''container'''} snake_case_ = {k: v for k, v in vars(_UpperCamelCase ).items() if k != '''container'''} # Choices with mixed type have custom function as "type" # So we need to compare results directly for equality if xx.get('''choices''' , _UpperCamelCase ) and yy.get('''choices''' , _UpperCamelCase ): for expected_choice in yy["choices"] + xx["choices"]: self.assertEqual(xx['''type'''](_UpperCamelCase ) , yy['''type'''](_UpperCamelCase ) ) del xx["type"], yy["type"] self.assertEqual(_UpperCamelCase , _UpperCamelCase ) def snake_case__( self : Optional[Any] ) ->Dict: snake_case_ = HfArgumentParser(_UpperCamelCase ) snake_case_ = argparse.ArgumentParser() expected.add_argument('''--foo''' , type=_UpperCamelCase , required=_UpperCamelCase ) expected.add_argument('''--bar''' , type=_UpperCamelCase , required=_UpperCamelCase ) expected.add_argument('''--baz''' , type=_UpperCamelCase , required=_UpperCamelCase ) expected.add_argument('''--flag''' , type=_UpperCamelCase , default=_UpperCamelCase , const=_UpperCamelCase , nargs='''?''' ) self.argparsersEqual(_UpperCamelCase , _UpperCamelCase ) snake_case_ = ['''--foo''', '''1''', '''--baz''', '''quux''', '''--bar''', '''0.5'''] ((snake_case_), ) = parser.parse_args_into_dataclasses(_UpperCamelCase , look_for_args_file=_UpperCamelCase ) self.assertFalse(example.flag ) def snake_case__( self : Tuple ) ->Optional[int]: snake_case_ = HfArgumentParser(_UpperCamelCase ) snake_case_ = argparse.ArgumentParser() expected.add_argument('''--foo''' , default=4_2 , type=_UpperCamelCase ) expected.add_argument('''--baz''' , default='''toto''' , type=_UpperCamelCase , help='''help message''' ) self.argparsersEqual(_UpperCamelCase , _UpperCamelCase ) def snake_case__( self : Tuple ) ->Tuple: snake_case_ = argparse.ArgumentParser() expected.add_argument('''--foo''' , type=_UpperCamelCase , default=_UpperCamelCase , const=_UpperCamelCase , nargs='''?''' ) expected.add_argument('''--baz''' , type=_UpperCamelCase , default=_UpperCamelCase , const=_UpperCamelCase , nargs='''?''' ) # A boolean no_* argument always has to come after its "default: True" regular counter-part # and its default must be set to False expected.add_argument('''--no_baz''' , action='''store_false''' , default=_UpperCamelCase , dest='''baz''' ) expected.add_argument('''--opt''' , type=_UpperCamelCase , default=_UpperCamelCase ) snake_case_ = [WithDefaultBoolExample] if is_python_no_less_than_3_10: dataclass_types.append(_UpperCamelCase ) for dataclass_type in dataclass_types: snake_case_ = HfArgumentParser(_UpperCamelCase ) self.argparsersEqual(_UpperCamelCase , _UpperCamelCase ) snake_case_ = parser.parse_args([] ) self.assertEqual(_UpperCamelCase , Namespace(foo=_UpperCamelCase , baz=_UpperCamelCase , opt=_UpperCamelCase ) ) snake_case_ = parser.parse_args(['''--foo''', '''--no_baz'''] ) self.assertEqual(_UpperCamelCase , Namespace(foo=_UpperCamelCase , baz=_UpperCamelCase , opt=_UpperCamelCase ) ) snake_case_ = parser.parse_args(['''--foo''', '''--baz'''] ) self.assertEqual(_UpperCamelCase , Namespace(foo=_UpperCamelCase , baz=_UpperCamelCase , opt=_UpperCamelCase ) ) snake_case_ = parser.parse_args(['''--foo''', '''True''', '''--baz''', '''True''', '''--opt''', '''True'''] ) self.assertEqual(_UpperCamelCase , Namespace(foo=_UpperCamelCase , baz=_UpperCamelCase , opt=_UpperCamelCase ) ) snake_case_ = parser.parse_args(['''--foo''', '''False''', '''--baz''', '''False''', '''--opt''', '''False'''] ) self.assertEqual(_UpperCamelCase , Namespace(foo=_UpperCamelCase , baz=_UpperCamelCase , opt=_UpperCamelCase ) ) def snake_case__( self : Tuple ) ->Tuple: snake_case_ = HfArgumentParser(_UpperCamelCase ) snake_case_ = argparse.ArgumentParser() expected.add_argument( '''--foo''' , default='''toto''' , choices=['''titi''', '''toto''', 4_2] , type=make_choice_type_function(['''titi''', '''toto''', 4_2] ) , ) self.argparsersEqual(_UpperCamelCase , _UpperCamelCase ) snake_case_ = parser.parse_args([] ) self.assertEqual(args.foo , '''toto''' ) snake_case_ = parser.parse_args_into_dataclasses([] )[0] self.assertEqual(enum_ex.foo , MixedTypeEnum.toto ) snake_case_ = parser.parse_args(['''--foo''', '''titi'''] ) self.assertEqual(args.foo , '''titi''' ) snake_case_ = parser.parse_args_into_dataclasses(['''--foo''', '''titi'''] )[0] self.assertEqual(enum_ex.foo , MixedTypeEnum.titi ) snake_case_ = parser.parse_args(['''--foo''', '''42'''] ) self.assertEqual(args.foo , 4_2 ) snake_case_ = parser.parse_args_into_dataclasses(['''--foo''', '''42'''] )[0] self.assertEqual(enum_ex.foo , MixedTypeEnum.fourtytwo ) def snake_case__( self : Tuple ) ->Union[str, Any]: @dataclass class snake_case_ : '''simple docstring''' SCREAMING_SNAKE_CASE : Literal["titi", "toto", 42] = "toto" snake_case_ = HfArgumentParser(_UpperCamelCase ) snake_case_ = argparse.ArgumentParser() expected.add_argument( '''--foo''' , default='''toto''' , choices=('''titi''', '''toto''', 4_2) , type=make_choice_type_function(['''titi''', '''toto''', 4_2] ) , ) self.argparsersEqual(_UpperCamelCase , _UpperCamelCase ) snake_case_ = parser.parse_args([] ) self.assertEqual(args.foo , '''toto''' ) snake_case_ = parser.parse_args(['''--foo''', '''titi'''] ) self.assertEqual(args.foo , '''titi''' ) snake_case_ = parser.parse_args(['''--foo''', '''42'''] ) self.assertEqual(args.foo , 4_2 ) def snake_case__( self : List[str] ) ->int: snake_case_ = HfArgumentParser(_UpperCamelCase ) snake_case_ = argparse.ArgumentParser() expected.add_argument('''--foo_int''' , nargs='''+''' , default=[] , type=_UpperCamelCase ) expected.add_argument('''--bar_int''' , nargs='''+''' , default=[1, 2, 3] , type=_UpperCamelCase ) expected.add_argument('''--foo_str''' , nargs='''+''' , default=['''Hallo''', '''Bonjour''', '''Hello'''] , type=_UpperCamelCase ) expected.add_argument('''--foo_float''' , nargs='''+''' , default=[0.1, 0.2, 0.3] , type=_UpperCamelCase ) self.argparsersEqual(_UpperCamelCase , _UpperCamelCase ) snake_case_ = parser.parse_args([] ) self.assertEqual( _UpperCamelCase , Namespace(foo_int=[] , bar_int=[1, 2, 3] , foo_str=['''Hallo''', '''Bonjour''', '''Hello'''] , foo_float=[0.1, 0.2, 0.3] ) , ) snake_case_ = parser.parse_args('''--foo_int 1 --bar_int 2 3 --foo_str a b c --foo_float 0.1 0.7'''.split() ) self.assertEqual(_UpperCamelCase , Namespace(foo_int=[1] , bar_int=[2, 3] , foo_str=['''a''', '''b''', '''c'''] , foo_float=[0.1, 0.7] ) ) def snake_case__( self : Optional[Any] ) ->List[Any]: snake_case_ = argparse.ArgumentParser() expected.add_argument('''--foo''' , default=_UpperCamelCase , type=_UpperCamelCase ) expected.add_argument('''--bar''' , default=_UpperCamelCase , type=_UpperCamelCase , help='''help message''' ) expected.add_argument('''--baz''' , default=_UpperCamelCase , type=_UpperCamelCase ) expected.add_argument('''--ces''' , nargs='''+''' , default=[] , type=_UpperCamelCase ) expected.add_argument('''--des''' , nargs='''+''' , default=[] , type=_UpperCamelCase ) snake_case_ = [OptionalExample] if is_python_no_less_than_3_10: dataclass_types.append(_UpperCamelCase ) for dataclass_type in dataclass_types: snake_case_ = HfArgumentParser(_UpperCamelCase ) self.argparsersEqual(_UpperCamelCase , _UpperCamelCase ) snake_case_ = parser.parse_args([] ) self.assertEqual(_UpperCamelCase , Namespace(foo=_UpperCamelCase , bar=_UpperCamelCase , baz=_UpperCamelCase , ces=[] , des=[] ) ) snake_case_ = parser.parse_args('''--foo 12 --bar 3.14 --baz 42 --ces a b c --des 1 2 3'''.split() ) self.assertEqual(_UpperCamelCase , Namespace(foo=1_2 , bar=3.14 , baz='''42''' , ces=['''a''', '''b''', '''c'''] , des=[1, 2, 3] ) ) def snake_case__( self : Union[str, Any] ) ->Optional[int]: snake_case_ = HfArgumentParser(_UpperCamelCase ) snake_case_ = argparse.ArgumentParser() expected.add_argument('''--required_list''' , nargs='''+''' , type=_UpperCamelCase , required=_UpperCamelCase ) expected.add_argument('''--required_str''' , type=_UpperCamelCase , required=_UpperCamelCase ) expected.add_argument( '''--required_enum''' , type=make_choice_type_function(['''titi''', '''toto'''] ) , choices=['''titi''', '''toto'''] , required=_UpperCamelCase , ) self.argparsersEqual(_UpperCamelCase , _UpperCamelCase ) def snake_case__( self : List[str] ) ->int: snake_case_ = HfArgumentParser(_UpperCamelCase ) snake_case_ = argparse.ArgumentParser() expected.add_argument('''--foo''' , type=_UpperCamelCase , required=_UpperCamelCase ) expected.add_argument( '''--required_enum''' , type=make_choice_type_function(['''titi''', '''toto'''] ) , choices=['''titi''', '''toto'''] , required=_UpperCamelCase , ) expected.add_argument('''--opt''' , type=_UpperCamelCase , default=_UpperCamelCase ) expected.add_argument('''--baz''' , default='''toto''' , type=_UpperCamelCase , help='''help message''' ) expected.add_argument('''--foo_str''' , nargs='''+''' , default=['''Hallo''', '''Bonjour''', '''Hello'''] , type=_UpperCamelCase ) self.argparsersEqual(_UpperCamelCase , _UpperCamelCase ) def snake_case__( self : Dict ) ->Any: snake_case_ = HfArgumentParser(_UpperCamelCase ) snake_case_ = { '''foo''': 1_2, '''bar''': 3.14, '''baz''': '''42''', '''flag''': True, } snake_case_ = parser.parse_dict(_UpperCamelCase )[0] snake_case_ = BasicExample(**_UpperCamelCase ) self.assertEqual(_UpperCamelCase , _UpperCamelCase ) def snake_case__( self : int ) ->Dict: snake_case_ = HfArgumentParser(_UpperCamelCase ) snake_case_ = { '''foo''': 1_2, '''bar''': 3.14, '''baz''': '''42''', '''flag''': True, '''extra''': 4_2, } self.assertRaises(_UpperCamelCase , parser.parse_dict , _UpperCamelCase , allow_extra_keys=_UpperCamelCase ) def snake_case__( self : str ) ->Tuple: snake_case_ = HfArgumentParser(_UpperCamelCase ) snake_case_ = { '''foo''': 1_2, '''bar''': 3.14, '''baz''': '''42''', '''flag''': True, } with tempfile.TemporaryDirectory() as tmp_dir: snake_case_ = os.path.join(_UpperCamelCase , '''temp_json''' ) os.mkdir(_UpperCamelCase ) with open(temp_local_path + '''.json''' , '''w+''' ) as f: json.dump(_UpperCamelCase , _UpperCamelCase ) snake_case_ = parser.parse_yaml_file(Path(temp_local_path + '''.json''' ) )[0] snake_case_ = BasicExample(**_UpperCamelCase ) self.assertEqual(_UpperCamelCase , _UpperCamelCase ) def snake_case__( self : Optional[int] ) ->str: snake_case_ = HfArgumentParser(_UpperCamelCase ) snake_case_ = { '''foo''': 1_2, '''bar''': 3.14, '''baz''': '''42''', '''flag''': True, } with tempfile.TemporaryDirectory() as tmp_dir: snake_case_ = os.path.join(_UpperCamelCase , '''temp_yaml''' ) os.mkdir(_UpperCamelCase ) with open(temp_local_path + '''.yaml''' , '''w+''' ) as f: yaml.dump(_UpperCamelCase , _UpperCamelCase ) snake_case_ = parser.parse_yaml_file(Path(temp_local_path + '''.yaml''' ) )[0] snake_case_ = BasicExample(**_UpperCamelCase ) self.assertEqual(_UpperCamelCase , _UpperCamelCase ) def snake_case__( self : Any ) ->Any: snake_case_ = HfArgumentParser(_UpperCamelCase ) self.assertIsNotNone(_UpperCamelCase )
39
1
import unittest import numpy as np from diffusers import LMSDiscreteScheduler, OnnxStableDiffusionInpaintPipeline from diffusers.utils.testing_utils import ( is_onnx_available, load_image, nightly, require_onnxruntime, require_torch_gpu, ) from ..test_pipelines_onnx_common import OnnxPipelineTesterMixin if is_onnx_available(): import onnxruntime as ort class snake_case_ ( __A , unittest.TestCase ): '''simple docstring''' pass @nightly @require_onnxruntime @require_torch_gpu class snake_case_ ( unittest.TestCase ): '''simple docstring''' @property def snake_case__( self : Tuple ) ->Dict: return ( "CUDAExecutionProvider", { "gpu_mem_limit": "15000000000", # 15GB "arena_extend_strategy": "kSameAsRequested", }, ) @property def snake_case__( self : Optional[Any] ) ->Optional[int]: snake_case_ = ort.SessionOptions() snake_case_ = False return options def snake_case__( self : Optional[int] ) ->Optional[Any]: snake_case_ = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo.png''' ) snake_case_ = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo_mask.png''' ) snake_case_ = OnnxStableDiffusionInpaintPipeline.from_pretrained( '''runwayml/stable-diffusion-inpainting''' , revision='''onnx''' , safety_checker=_UpperCamelCase , feature_extractor=_UpperCamelCase , provider=self.gpu_provider , sess_options=self.gpu_options , ) pipe.set_progress_bar_config(disable=_UpperCamelCase ) snake_case_ = '''A red cat sitting on a park bench''' snake_case_ = np.random.RandomState(0 ) snake_case_ = pipe( prompt=_UpperCamelCase , image=_UpperCamelCase , mask_image=_UpperCamelCase , guidance_scale=7.5 , num_inference_steps=1_0 , generator=_UpperCamelCase , output_type='''np''' , ) snake_case_ = output.images snake_case_ = images[0, 2_5_5:2_5_8, 2_5_5:2_5_8, -1] assert images.shape == (1, 5_1_2, 5_1_2, 3) snake_case_ = np.array([0.2514, 0.3007, 0.3517, 0.1790, 0.2382, 0.3167, 0.1944, 0.2273, 0.2464] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3 def snake_case__( self : Optional[Any] ) ->int: snake_case_ = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo.png''' ) snake_case_ = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo_mask.png''' ) snake_case_ = LMSDiscreteScheduler.from_pretrained( '''runwayml/stable-diffusion-inpainting''' , subfolder='''scheduler''' , revision='''onnx''' ) snake_case_ = OnnxStableDiffusionInpaintPipeline.from_pretrained( '''runwayml/stable-diffusion-inpainting''' , revision='''onnx''' , scheduler=_UpperCamelCase , safety_checker=_UpperCamelCase , feature_extractor=_UpperCamelCase , provider=self.gpu_provider , sess_options=self.gpu_options , ) pipe.set_progress_bar_config(disable=_UpperCamelCase ) snake_case_ = '''A red cat sitting on a park bench''' snake_case_ = np.random.RandomState(0 ) snake_case_ = pipe( prompt=_UpperCamelCase , image=_UpperCamelCase , mask_image=_UpperCamelCase , guidance_scale=7.5 , num_inference_steps=2_0 , generator=_UpperCamelCase , output_type='''np''' , ) snake_case_ = output.images snake_case_ = images[0, 2_5_5:2_5_8, 2_5_5:2_5_8, -1] assert images.shape == (1, 5_1_2, 5_1_2, 3) snake_case_ = np.array([0.0086, 0.0077, 0.0083, 0.0093, 0.0107, 0.0139, 0.0094, 0.0097, 0.0125] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3
39
import warnings from ...utils import logging from .image_processing_chinese_clip import ChineseCLIPImageProcessor lowerCAmelCase_ = logging.get_logger(__name__) class snake_case_ ( __A ): '''simple docstring''' def __init__( self : Dict , *_UpperCamelCase : int , **_UpperCamelCase : Tuple ) ->None: warnings.warn( '''The class ChineseCLIPFeatureExtractor is deprecated and will be removed in version 5 of Transformers.''' ''' Please use ChineseCLIPImageProcessor instead.''' , _UpperCamelCase , ) super().__init__(*_UpperCamelCase , **_UpperCamelCase )
39
1
import argparse import logging import sys from unittest.mock import patch import run_glue_deebert from transformers.testing_utils import TestCasePlus, get_gpu_count, require_torch_non_multi_gpu, slow logging.basicConfig(level=logging.DEBUG) lowerCAmelCase_ = logging.getLogger() def __SCREAMING_SNAKE_CASE (): snake_case_ = argparse.ArgumentParser() parser.add_argument('''-f''' ) snake_case_ = parser.parse_args() return args.f class snake_case_ ( __A ): '''simple docstring''' def snake_case__( self : Dict ) ->None: snake_case_ = logging.StreamHandler(sys.stdout ) logger.addHandler(_UpperCamelCase ) def snake_case__( self : List[str] , _UpperCamelCase : Any ) ->Dict: snake_case_ = get_gpu_count() if n_gpu > 1: pass # XXX: doesn't quite work with n_gpu > 1 https://github.com/huggingface/transformers/issues/10560 # script = f"{self.examples_dir_str}/research_projects/deebert/run_glue_deebert.py" # distributed_args = f"-m torch.distributed.launch --nproc_per_node={n_gpu} {script}".split() # cmd = [sys.executable] + distributed_args + args # execute_subprocess_async(cmd, env=self.get_env()) # XXX: test the results - need to save them first into .json file else: args.insert(0 , '''run_glue_deebert.py''' ) with patch.object(_UpperCamelCase , '''argv''' , _UpperCamelCase ): snake_case_ = run_glue_deebert.main() for value in result.values(): self.assertGreaterEqual(_UpperCamelCase , 0.666 ) @slow @require_torch_non_multi_gpu def snake_case__( self : Union[str, Any] ) ->Union[str, Any]: snake_case_ = ''' --model_type roberta --model_name_or_path roberta-base --task_name MRPC --do_train --do_eval --do_lower_case --data_dir ./tests/fixtures/tests_samples/MRPC/ --max_seq_length 128 --per_gpu_eval_batch_size=1 --per_gpu_train_batch_size=8 --learning_rate 2e-4 --num_train_epochs 3 --overwrite_output_dir --seed 42 --output_dir ./examples/deebert/saved_models/roberta-base/MRPC/two_stage --plot_data_dir ./examples/deebert/results/ --save_steps 0 --overwrite_cache --eval_after_first_stage '''.split() self.run_and_check(_UpperCamelCase ) snake_case_ = ''' --model_type roberta --model_name_or_path ./examples/deebert/saved_models/roberta-base/MRPC/two_stage --task_name MRPC --do_eval --do_lower_case --data_dir ./tests/fixtures/tests_samples/MRPC/ --output_dir ./examples/deebert/saved_models/roberta-base/MRPC/two_stage --plot_data_dir ./examples/deebert/results/ --max_seq_length 128 --eval_each_highway --eval_highway --overwrite_cache --per_gpu_eval_batch_size=1 '''.split() self.run_and_check(_UpperCamelCase ) snake_case_ = ''' --model_type roberta --model_name_or_path ./examples/deebert/saved_models/roberta-base/MRPC/two_stage --task_name MRPC --do_eval --do_lower_case --data_dir ./tests/fixtures/tests_samples/MRPC/ --output_dir ./examples/deebert/saved_models/roberta-base/MRPC/two_stage --plot_data_dir ./examples/deebert/results/ --max_seq_length 128 --early_exit_entropy 0.1 --eval_highway --overwrite_cache --per_gpu_eval_batch_size=1 '''.split() self.run_and_check(_UpperCamelCase )
39
from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase_ = logging.get_logger(__name__) lowerCAmelCase_ = { '''RWKV/rwkv-4-169m-pile''': '''https://huggingface.co/RWKV/rwkv-4-169m-pile/resolve/main/config.json''', '''RWKV/rwkv-4-430m-pile''': '''https://huggingface.co/RWKV/rwkv-4-430m-pile/resolve/main/config.json''', '''RWKV/rwkv-4-1b5-pile''': '''https://huggingface.co/RWKV/rwkv-4-1b5-pile/resolve/main/config.json''', '''RWKV/rwkv-4-3b-pile''': '''https://huggingface.co/RWKV/rwkv-4-3b-pile/resolve/main/config.json''', '''RWKV/rwkv-4-7b-pile''': '''https://huggingface.co/RWKV/rwkv-4-7b-pile/resolve/main/config.json''', '''RWKV/rwkv-4-14b-pile''': '''https://huggingface.co/RWKV/rwkv-4-14b-pile/resolve/main/config.json''', '''RWKV/rwkv-raven-1b5''': '''https://huggingface.co/RWKV/rwkv-raven-1b5/resolve/main/config.json''', '''RWKV/rwkv-raven-3b''': '''https://huggingface.co/RWKV/rwkv-raven-3b/resolve/main/config.json''', '''RWKV/rwkv-raven-7b''': '''https://huggingface.co/RWKV/rwkv-raven-7b/resolve/main/config.json''', '''RWKV/rwkv-raven-14b''': '''https://huggingface.co/RWKV/rwkv-raven-14b/resolve/main/config.json''', } class snake_case_ ( __A ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = "rwkv" SCREAMING_SNAKE_CASE : Any = {"max_position_embeddings": "context_length"} def __init__( self : Union[str, Any] , _UpperCamelCase : Any=5_0_2_7_7 , _UpperCamelCase : Optional[int]=1_0_2_4 , _UpperCamelCase : Optional[int]=4_0_9_6 , _UpperCamelCase : str=3_2 , _UpperCamelCase : Tuple=None , _UpperCamelCase : Dict=None , _UpperCamelCase : Optional[int]=1e-5 , _UpperCamelCase : Any=0 , _UpperCamelCase : Optional[Any]=0 , _UpperCamelCase : int=6 , _UpperCamelCase : Dict=False , _UpperCamelCase : Optional[int]=True , **_UpperCamelCase : int , ) ->List[str]: snake_case_ = vocab_size snake_case_ = context_length snake_case_ = hidden_size snake_case_ = num_hidden_layers snake_case_ = attention_hidden_size if attention_hidden_size is not None else hidden_size snake_case_ = intermediate_size if intermediate_size is not None else 4 * hidden_size snake_case_ = layer_norm_epsilon snake_case_ = rescale_every snake_case_ = use_cache snake_case_ = bos_token_id snake_case_ = eos_token_id super().__init__( tie_word_embeddings=_UpperCamelCase , bos_token_id=_UpperCamelCase , eos_token_id=_UpperCamelCase , **_UpperCamelCase )
39
1
import warnings from ...utils import logging from .image_processing_beit import BeitImageProcessor lowerCAmelCase_ = logging.get_logger(__name__) class snake_case_ ( __A ): '''simple docstring''' def __init__( self : Optional[Any] , *_UpperCamelCase : List[Any] , **_UpperCamelCase : Optional[Any] ) ->None: warnings.warn( '''The class BeitFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please''' ''' use BeitImageProcessor instead.''' , _UpperCamelCase , ) super().__init__(*_UpperCamelCase , **_UpperCamelCase )
39
import bza import gzip import lzma import os import shutil import struct import tarfile import warnings import zipfile from abc import ABC, abstractmethod from pathlib import Path from typing import Dict, List, Optional, Type, Union from .. import config from .filelock import FileLock from .logging import get_logger lowerCAmelCase_ = get_logger(__name__) class snake_case_ : '''simple docstring''' def __init__( self : int , _UpperCamelCase : Optional[str] = None ) ->Tuple: snake_case_ = ( os.path.join(_UpperCamelCase , config.EXTRACTED_DATASETS_DIR ) if cache_dir else config.EXTRACTED_DATASETS_PATH ) snake_case_ = Extractor def snake_case__( self : Any , _UpperCamelCase : str ) ->str: from .file_utils import hash_url_to_filename # Path where we extract compressed archives # We extract in the cache dir, and get the extracted path name by hashing the original path" snake_case_ = os.path.abspath(_UpperCamelCase ) return os.path.join(self.extract_dir , hash_url_to_filename(_UpperCamelCase ) ) def snake_case__( self : int , _UpperCamelCase : str , _UpperCamelCase : bool ) ->bool: return force_extract or ( not os.path.isfile(_UpperCamelCase ) and not (os.path.isdir(_UpperCamelCase ) and os.listdir(_UpperCamelCase )) ) def snake_case__( self : Tuple , _UpperCamelCase : str , _UpperCamelCase : bool = False ) ->str: snake_case_ = self.extractor.infer_extractor_format(_UpperCamelCase ) if not extractor_format: return input_path snake_case_ = self._get_output_path(_UpperCamelCase ) if self._do_extract(_UpperCamelCase , _UpperCamelCase ): self.extractor.extract(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) return output_path class snake_case_ ( __A ): '''simple docstring''' @classmethod @abstractmethod def snake_case__( cls : Optional[int] , _UpperCamelCase : Union[Path, str] , **_UpperCamelCase : str ) ->bool: ... @staticmethod @abstractmethod def snake_case__( _UpperCamelCase : Union[Path, str] , _UpperCamelCase : Union[Path, str] ) ->None: ... class snake_case_ ( __A , __A ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[bytes] = [] @staticmethod def snake_case__( _UpperCamelCase : Union[Path, str] , _UpperCamelCase : int ) ->List[Any]: with open(_UpperCamelCase , '''rb''' ) as f: return f.read(_UpperCamelCase ) @classmethod def snake_case__( cls : Union[str, Any] , _UpperCamelCase : Union[Path, str] , _UpperCamelCase : bytes = b"" ) ->bool: if not magic_number: snake_case_ = max(len(_UpperCamelCase ) for cls_magic_number in cls.magic_numbers ) try: snake_case_ = cls.read_magic_number(_UpperCamelCase , _UpperCamelCase ) except OSError: return False return any(magic_number.startswith(_UpperCamelCase ) for cls_magic_number in cls.magic_numbers ) class snake_case_ ( __A ): '''simple docstring''' @classmethod def snake_case__( cls : Union[str, Any] , _UpperCamelCase : Union[Path, str] , **_UpperCamelCase : Any ) ->bool: return tarfile.is_tarfile(_UpperCamelCase ) @staticmethod def snake_case__( _UpperCamelCase : Union[str, Any] , _UpperCamelCase : Dict ) ->List[str]: def resolved(_UpperCamelCase : str ) -> str: return os.path.realpath(os.path.abspath(_UpperCamelCase ) ) def badpath(_UpperCamelCase : str , _UpperCamelCase : str ) -> bool: # joinpath will ignore base if path is absolute return not resolved(os.path.join(_UpperCamelCase , _UpperCamelCase ) ).startswith(_UpperCamelCase ) def badlink(_UpperCamelCase : Tuple , _UpperCamelCase : str ) -> bool: # Links are interpreted relative to the directory containing the link snake_case_ = resolved(os.path.join(_UpperCamelCase , os.path.dirname(info.name ) ) ) return badpath(info.linkname , base=_UpperCamelCase ) snake_case_ = resolved(_UpperCamelCase ) for finfo in members: if badpath(finfo.name , _UpperCamelCase ): logger.error(f'''Extraction of {finfo.name} is blocked (illegal path)''' ) elif finfo.issym() and badlink(_UpperCamelCase , _UpperCamelCase ): logger.error(f'''Extraction of {finfo.name} is blocked: Symlink to {finfo.linkname}''' ) elif finfo.islnk() and badlink(_UpperCamelCase , _UpperCamelCase ): logger.error(f'''Extraction of {finfo.name} is blocked: Hard link to {finfo.linkname}''' ) else: yield finfo @staticmethod def snake_case__( _UpperCamelCase : Union[Path, str] , _UpperCamelCase : Union[Path, str] ) ->None: os.makedirs(_UpperCamelCase , exist_ok=_UpperCamelCase ) snake_case_ = tarfile.open(_UpperCamelCase ) tar_file.extractall(_UpperCamelCase , members=TarExtractor.safemembers(_UpperCamelCase , _UpperCamelCase ) ) tar_file.close() class snake_case_ ( __A ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = [b"\x1F\x8B"] @staticmethod def snake_case__( _UpperCamelCase : Union[Path, str] , _UpperCamelCase : Union[Path, str] ) ->None: with gzip.open(_UpperCamelCase , '''rb''' ) as gzip_file: with open(_UpperCamelCase , '''wb''' ) as extracted_file: shutil.copyfileobj(_UpperCamelCase , _UpperCamelCase ) class snake_case_ ( __A ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = [ b"PK\x03\x04", b"PK\x05\x06", # empty archive b"PK\x07\x08", # spanned archive ] @classmethod def snake_case__( cls : List[str] , _UpperCamelCase : Union[Path, str] , _UpperCamelCase : bytes = b"" ) ->bool: if super().is_extractable(_UpperCamelCase , magic_number=_UpperCamelCase ): return True try: # Alternative version of zipfile.is_zipfile that has less false positives, but misses executable zip archives. # From: https://github.com/python/cpython/pull/5053 from zipfile import ( _CD_SIGNATURE, _ECD_DISK_NUMBER, _ECD_DISK_START, _ECD_ENTRIES_TOTAL, _ECD_OFFSET, _ECD_SIZE, _EndRecData, sizeCentralDir, stringCentralDir, structCentralDir, ) with open(_UpperCamelCase , '''rb''' ) as fp: snake_case_ = _EndRecData(_UpperCamelCase ) if endrec: if endrec[_ECD_ENTRIES_TOTAL] == 0 and endrec[_ECD_SIZE] == 0 and endrec[_ECD_OFFSET] == 0: return True # Empty zipfiles are still zipfiles elif endrec[_ECD_DISK_NUMBER] == endrec[_ECD_DISK_START]: fp.seek(endrec[_ECD_OFFSET] ) # Central directory is on the same disk if fp.tell() == endrec[_ECD_OFFSET] and endrec[_ECD_SIZE] >= sizeCentralDir: snake_case_ = fp.read(_UpperCamelCase ) # CD is where we expect it to be if len(_UpperCamelCase ) == sizeCentralDir: snake_case_ = struct.unpack(_UpperCamelCase , _UpperCamelCase ) # CD is the right size if centdir[_CD_SIGNATURE] == stringCentralDir: return True # First central directory entry has correct magic number return False except Exception: # catch all errors in case future python versions change the zipfile internals return False @staticmethod def snake_case__( _UpperCamelCase : Union[Path, str] , _UpperCamelCase : Union[Path, str] ) ->None: os.makedirs(_UpperCamelCase , exist_ok=_UpperCamelCase ) with zipfile.ZipFile(_UpperCamelCase , '''r''' ) as zip_file: zip_file.extractall(_UpperCamelCase ) zip_file.close() class snake_case_ ( __A ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = [b"\xFD\x37\x7A\x58\x5A\x00"] @staticmethod def snake_case__( _UpperCamelCase : Union[Path, str] , _UpperCamelCase : Union[Path, str] ) ->None: with lzma.open(_UpperCamelCase ) as compressed_file: with open(_UpperCamelCase , '''wb''' ) as extracted_file: shutil.copyfileobj(_UpperCamelCase , _UpperCamelCase ) class snake_case_ ( __A ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = [b"Rar!\x1a\x07\x00", b"Rar!\x1a\x07\x01\x00"] # RAR_ID # RAR5_ID @staticmethod def snake_case__( _UpperCamelCase : Union[Path, str] , _UpperCamelCase : Union[Path, str] ) ->None: if not config.RARFILE_AVAILABLE: raise ImportError('''Please pip install rarfile''' ) import rarfile os.makedirs(_UpperCamelCase , exist_ok=_UpperCamelCase ) snake_case_ = rarfile.RarFile(_UpperCamelCase ) rf.extractall(_UpperCamelCase ) rf.close() class snake_case_ ( __A ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = [b"\x28\xb5\x2F\xFD"] @staticmethod def snake_case__( _UpperCamelCase : Union[Path, str] , _UpperCamelCase : Union[Path, str] ) ->None: if not config.ZSTANDARD_AVAILABLE: raise ImportError('''Please pip install zstandard''' ) import zstandard as zstd snake_case_ = zstd.ZstdDecompressor() with open(_UpperCamelCase , '''rb''' ) as ifh, open(_UpperCamelCase , '''wb''' ) as ofh: dctx.copy_stream(_UpperCamelCase , _UpperCamelCase ) class snake_case_ ( __A ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = [b"\x42\x5A\x68"] @staticmethod def snake_case__( _UpperCamelCase : Union[Path, str] , _UpperCamelCase : Union[Path, str] ) ->None: with bza.open(_UpperCamelCase , '''rb''' ) as compressed_file: with open(_UpperCamelCase , '''wb''' ) as extracted_file: shutil.copyfileobj(_UpperCamelCase , _UpperCamelCase ) class snake_case_ ( __A ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = [b"\x37\x7A\xBC\xAF\x27\x1C"] @staticmethod def snake_case__( _UpperCamelCase : Union[Path, str] , _UpperCamelCase : Union[Path, str] ) ->None: if not config.PY7ZR_AVAILABLE: raise ImportError('''Please pip install py7zr''' ) import pyazr os.makedirs(_UpperCamelCase , exist_ok=_UpperCamelCase ) with pyazr.SevenZipFile(_UpperCamelCase , '''r''' ) as archive: archive.extractall(_UpperCamelCase ) class snake_case_ ( __A ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = [b"\x04\x22\x4D\x18"] @staticmethod def snake_case__( _UpperCamelCase : Union[Path, str] , _UpperCamelCase : Union[Path, str] ) ->None: if not config.LZ4_AVAILABLE: raise ImportError('''Please pip install lz4''' ) import lza.frame with lza.frame.open(_UpperCamelCase , '''rb''' ) as compressed_file: with open(_UpperCamelCase , '''wb''' ) as extracted_file: shutil.copyfileobj(_UpperCamelCase , _UpperCamelCase ) class snake_case_ : '''simple docstring''' SCREAMING_SNAKE_CASE : Dict[str, Type[BaseExtractor]] = { "tar": TarExtractor, "gzip": GzipExtractor, "zip": ZipExtractor, "xz": XzExtractor, "rar": RarExtractor, "zstd": ZstdExtractor, "bz2": BzipaExtractor, "7z": SevenZipExtractor, # <Added version="2.4.0"/> "lz4": LzaExtractor, # <Added version="2.4.0"/> } @classmethod def snake_case__( cls : List[Any] ) ->List[str]: return max( len(_UpperCamelCase ) for extractor in cls.extractors.values() if issubclass(_UpperCamelCase , _UpperCamelCase ) for extractor_magic_number in extractor.magic_numbers ) @staticmethod def snake_case__( _UpperCamelCase : Union[Path, str] , _UpperCamelCase : int ) ->Tuple: try: return MagicNumberBaseExtractor.read_magic_number(_UpperCamelCase , magic_number_length=_UpperCamelCase ) except OSError: return b"" @classmethod def snake_case__( cls : Optional[Any] , _UpperCamelCase : Union[Path, str] , _UpperCamelCase : bool = False ) ->bool: warnings.warn( '''Method \'is_extractable\' was deprecated in version 2.4.0 and will be removed in 3.0.0. ''' '''Use \'infer_extractor_format\' instead.''' , category=_UpperCamelCase , ) snake_case_ = cls.infer_extractor_format(_UpperCamelCase ) if extractor_format: return True if not return_extractor else (True, cls.extractors[extractor_format]) return False if not return_extractor else (False, None) @classmethod def snake_case__( cls : int , _UpperCamelCase : Union[Path, str] ) ->str: # <Added version="2.4.0"/> snake_case_ = cls._get_magic_number_max_length() snake_case_ = cls._read_magic_number(_UpperCamelCase , _UpperCamelCase ) for extractor_format, extractor in cls.extractors.items(): if extractor.is_extractable(_UpperCamelCase , magic_number=_UpperCamelCase ): return extractor_format @classmethod def snake_case__( cls : Optional[int] , _UpperCamelCase : Union[Path, str] , _UpperCamelCase : Union[Path, str] , _UpperCamelCase : Optional[str] = None , _UpperCamelCase : Optional[BaseExtractor] = "deprecated" , ) ->None: os.makedirs(os.path.dirname(_UpperCamelCase ) , exist_ok=_UpperCamelCase ) # Prevent parallel extractions snake_case_ = str(Path(_UpperCamelCase ).with_suffix('''.lock''' ) ) with FileLock(_UpperCamelCase ): shutil.rmtree(_UpperCamelCase , ignore_errors=_UpperCamelCase ) if extractor_format or extractor != "deprecated": if extractor != "deprecated" or not isinstance(_UpperCamelCase , _UpperCamelCase ): # passed as positional arg warnings.warn( '''Parameter \'extractor\' was deprecated in version 2.4.0 and will be removed in 3.0.0. ''' '''Use \'extractor_format\' instead.''' , category=_UpperCamelCase , ) snake_case_ = extractor if extractor != '''deprecated''' else extractor_format else: snake_case_ = cls.extractors[extractor_format] return extractor.extract(_UpperCamelCase , _UpperCamelCase ) else: warnings.warn( '''Parameter \'extractor_format\' was made required in version 2.4.0 and not passing it will raise an ''' '''exception in 3.0.0.''' , category=_UpperCamelCase , ) for extractor in cls.extractors.values(): if extractor.is_extractable(_UpperCamelCase ): return extractor.extract(_UpperCamelCase , _UpperCamelCase )
39
1
def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): # "extended trapezoidal rule" # int(f) = dx/2 * (f1 + 2f2 + ... + fn) snake_case_ = (boundary[1] - boundary[0]) / steps snake_case_ = boundary[0] snake_case_ = boundary[1] snake_case_ = make_points(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) snake_case_ = 0.0 y += (h / 2.0) * f(SCREAMING_SNAKE_CASE__ ) for i in x_i: # print(i) y += h * f(SCREAMING_SNAKE_CASE__ ) y += (h / 2.0) * f(SCREAMING_SNAKE_CASE__ ) return y def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): snake_case_ = a + h while x < (b - h): yield x snake_case_ = x + h def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): # enter your function here snake_case_ = (x - 0) * (x - 0) return y def __SCREAMING_SNAKE_CASE (): snake_case_ = 0.0 # Lower bound of integration snake_case_ = 1.0 # Upper bound of integration snake_case_ = 10.0 # define number of steps or resolution snake_case_ = [a, b] # define boundary of integration snake_case_ = method_a(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) print(F'''y = {y}''' ) if __name__ == "__main__": main()
39
def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): if any(not isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) or x < 0 for x in sequence ): raise TypeError('''Sequence must be list of non-negative integers''' ) for _ in range(len(SCREAMING_SNAKE_CASE__ ) ): for i, (rod_upper, rod_lower) in enumerate(zip(SCREAMING_SNAKE_CASE__ , sequence[1:] ) ): if rod_upper > rod_lower: sequence[i] -= rod_upper - rod_lower sequence[i + 1] += rod_upper - rod_lower return sequence if __name__ == "__main__": assert bead_sort([5, 4, 3, 2, 1]) == [1, 2, 3, 4, 5] assert bead_sort([7, 9, 4, 3, 5]) == [3, 4, 5, 7, 9]
39
1
import argparse import json import os import sys import tempfile import unittest from argparse import Namespace from dataclasses import dataclass, field from enum import Enum from pathlib import Path from typing import List, Literal, Optional import yaml from transformers import HfArgumentParser, TrainingArguments from transformers.hf_argparser import make_choice_type_function, string_to_bool # Since Python 3.10, we can use the builtin `|` operator for Union types # See PEP 604: https://peps.python.org/pep-0604 lowerCAmelCase_ = sys.version_info >= (3, 10) def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__=None , SCREAMING_SNAKE_CASE__=None ): return field(default_factory=lambda: default , metadata=SCREAMING_SNAKE_CASE__ ) @dataclass class snake_case_ : '''simple docstring''' SCREAMING_SNAKE_CASE : int SCREAMING_SNAKE_CASE : float SCREAMING_SNAKE_CASE : str SCREAMING_SNAKE_CASE : bool @dataclass class snake_case_ : '''simple docstring''' SCREAMING_SNAKE_CASE : int = 42 SCREAMING_SNAKE_CASE : str = field(default="toto" , metadata={"help": "help message"} ) @dataclass class snake_case_ : '''simple docstring''' SCREAMING_SNAKE_CASE : bool = False SCREAMING_SNAKE_CASE : bool = True SCREAMING_SNAKE_CASE : Optional[bool] = None class snake_case_ ( __A ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = "titi" SCREAMING_SNAKE_CASE : Any = "toto" class snake_case_ ( __A ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = "titi" SCREAMING_SNAKE_CASE : Optional[Any] = "toto" SCREAMING_SNAKE_CASE : Any = 42 @dataclass class snake_case_ : '''simple docstring''' SCREAMING_SNAKE_CASE : BasicEnum = "toto" def snake_case__( self : Tuple ) ->List[str]: snake_case_ = BasicEnum(self.foo ) @dataclass class snake_case_ : '''simple docstring''' SCREAMING_SNAKE_CASE : MixedTypeEnum = "toto" def snake_case__( self : Union[str, Any] ) ->Dict: snake_case_ = MixedTypeEnum(self.foo ) @dataclass class snake_case_ : '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = None SCREAMING_SNAKE_CASE : Optional[float] = field(default=__A , metadata={"help": "help message"} ) SCREAMING_SNAKE_CASE : Optional[str] = None SCREAMING_SNAKE_CASE : Optional[List[str]] = list_field(default=[] ) SCREAMING_SNAKE_CASE : Optional[List[int]] = list_field(default=[] ) @dataclass class snake_case_ : '''simple docstring''' SCREAMING_SNAKE_CASE : List[int] = list_field(default=[] ) SCREAMING_SNAKE_CASE : List[int] = list_field(default=[1, 2, 3] ) SCREAMING_SNAKE_CASE : List[str] = list_field(default=["Hallo", "Bonjour", "Hello"] ) SCREAMING_SNAKE_CASE : List[float] = list_field(default=[0.1, 0.2, 0.3] ) @dataclass class snake_case_ : '''simple docstring''' SCREAMING_SNAKE_CASE : List[int] = field() SCREAMING_SNAKE_CASE : str = field() SCREAMING_SNAKE_CASE : BasicEnum = field() def snake_case__( self : Optional[Any] ) ->Tuple: snake_case_ = BasicEnum(self.required_enum ) @dataclass class snake_case_ : '''simple docstring''' SCREAMING_SNAKE_CASE : int SCREAMING_SNAKE_CASE : "BasicEnum" = field() SCREAMING_SNAKE_CASE : "Optional[bool]" = None SCREAMING_SNAKE_CASE : "str" = field(default="toto" , metadata={"help": "help message"} ) SCREAMING_SNAKE_CASE : "List[str]" = list_field(default=["Hallo", "Bonjour", "Hello"] ) if is_python_no_less_than_3_10: @dataclass class snake_case_ : '''simple docstring''' SCREAMING_SNAKE_CASE : bool = False SCREAMING_SNAKE_CASE : bool = True SCREAMING_SNAKE_CASE : bool | None = None @dataclass class snake_case_ : '''simple docstring''' SCREAMING_SNAKE_CASE : int | None = None SCREAMING_SNAKE_CASE : float | None = field(default=__A , metadata={"help": "help message"} ) SCREAMING_SNAKE_CASE : str | None = None SCREAMING_SNAKE_CASE : list[str] | None = list_field(default=[] ) SCREAMING_SNAKE_CASE : list[int] | None = list_field(default=[] ) class snake_case_ ( unittest.TestCase ): '''simple docstring''' def snake_case__( self : Dict , _UpperCamelCase : argparse.ArgumentParser , _UpperCamelCase : argparse.ArgumentParser ) ->str: self.assertEqual(len(a._actions ) , len(b._actions ) ) for x, y in zip(a._actions , b._actions ): snake_case_ = {k: v for k, v in vars(_UpperCamelCase ).items() if k != '''container'''} snake_case_ = {k: v for k, v in vars(_UpperCamelCase ).items() if k != '''container'''} # Choices with mixed type have custom function as "type" # So we need to compare results directly for equality if xx.get('''choices''' , _UpperCamelCase ) and yy.get('''choices''' , _UpperCamelCase ): for expected_choice in yy["choices"] + xx["choices"]: self.assertEqual(xx['''type'''](_UpperCamelCase ) , yy['''type'''](_UpperCamelCase ) ) del xx["type"], yy["type"] self.assertEqual(_UpperCamelCase , _UpperCamelCase ) def snake_case__( self : Optional[Any] ) ->Dict: snake_case_ = HfArgumentParser(_UpperCamelCase ) snake_case_ = argparse.ArgumentParser() expected.add_argument('''--foo''' , type=_UpperCamelCase , required=_UpperCamelCase ) expected.add_argument('''--bar''' , type=_UpperCamelCase , required=_UpperCamelCase ) expected.add_argument('''--baz''' , type=_UpperCamelCase , required=_UpperCamelCase ) expected.add_argument('''--flag''' , type=_UpperCamelCase , default=_UpperCamelCase , const=_UpperCamelCase , nargs='''?''' ) self.argparsersEqual(_UpperCamelCase , _UpperCamelCase ) snake_case_ = ['''--foo''', '''1''', '''--baz''', '''quux''', '''--bar''', '''0.5'''] ((snake_case_), ) = parser.parse_args_into_dataclasses(_UpperCamelCase , look_for_args_file=_UpperCamelCase ) self.assertFalse(example.flag ) def snake_case__( self : Tuple ) ->Optional[int]: snake_case_ = HfArgumentParser(_UpperCamelCase ) snake_case_ = argparse.ArgumentParser() expected.add_argument('''--foo''' , default=4_2 , type=_UpperCamelCase ) expected.add_argument('''--baz''' , default='''toto''' , type=_UpperCamelCase , help='''help message''' ) self.argparsersEqual(_UpperCamelCase , _UpperCamelCase ) def snake_case__( self : Tuple ) ->Tuple: snake_case_ = argparse.ArgumentParser() expected.add_argument('''--foo''' , type=_UpperCamelCase , default=_UpperCamelCase , const=_UpperCamelCase , nargs='''?''' ) expected.add_argument('''--baz''' , type=_UpperCamelCase , default=_UpperCamelCase , const=_UpperCamelCase , nargs='''?''' ) # A boolean no_* argument always has to come after its "default: True" regular counter-part # and its default must be set to False expected.add_argument('''--no_baz''' , action='''store_false''' , default=_UpperCamelCase , dest='''baz''' ) expected.add_argument('''--opt''' , type=_UpperCamelCase , default=_UpperCamelCase ) snake_case_ = [WithDefaultBoolExample] if is_python_no_less_than_3_10: dataclass_types.append(_UpperCamelCase ) for dataclass_type in dataclass_types: snake_case_ = HfArgumentParser(_UpperCamelCase ) self.argparsersEqual(_UpperCamelCase , _UpperCamelCase ) snake_case_ = parser.parse_args([] ) self.assertEqual(_UpperCamelCase , Namespace(foo=_UpperCamelCase , baz=_UpperCamelCase , opt=_UpperCamelCase ) ) snake_case_ = parser.parse_args(['''--foo''', '''--no_baz'''] ) self.assertEqual(_UpperCamelCase , Namespace(foo=_UpperCamelCase , baz=_UpperCamelCase , opt=_UpperCamelCase ) ) snake_case_ = parser.parse_args(['''--foo''', '''--baz'''] ) self.assertEqual(_UpperCamelCase , Namespace(foo=_UpperCamelCase , baz=_UpperCamelCase , opt=_UpperCamelCase ) ) snake_case_ = parser.parse_args(['''--foo''', '''True''', '''--baz''', '''True''', '''--opt''', '''True'''] ) self.assertEqual(_UpperCamelCase , Namespace(foo=_UpperCamelCase , baz=_UpperCamelCase , opt=_UpperCamelCase ) ) snake_case_ = parser.parse_args(['''--foo''', '''False''', '''--baz''', '''False''', '''--opt''', '''False'''] ) self.assertEqual(_UpperCamelCase , Namespace(foo=_UpperCamelCase , baz=_UpperCamelCase , opt=_UpperCamelCase ) ) def snake_case__( self : Tuple ) ->Tuple: snake_case_ = HfArgumentParser(_UpperCamelCase ) snake_case_ = argparse.ArgumentParser() expected.add_argument( '''--foo''' , default='''toto''' , choices=['''titi''', '''toto''', 4_2] , type=make_choice_type_function(['''titi''', '''toto''', 4_2] ) , ) self.argparsersEqual(_UpperCamelCase , _UpperCamelCase ) snake_case_ = parser.parse_args([] ) self.assertEqual(args.foo , '''toto''' ) snake_case_ = parser.parse_args_into_dataclasses([] )[0] self.assertEqual(enum_ex.foo , MixedTypeEnum.toto ) snake_case_ = parser.parse_args(['''--foo''', '''titi'''] ) self.assertEqual(args.foo , '''titi''' ) snake_case_ = parser.parse_args_into_dataclasses(['''--foo''', '''titi'''] )[0] self.assertEqual(enum_ex.foo , MixedTypeEnum.titi ) snake_case_ = parser.parse_args(['''--foo''', '''42'''] ) self.assertEqual(args.foo , 4_2 ) snake_case_ = parser.parse_args_into_dataclasses(['''--foo''', '''42'''] )[0] self.assertEqual(enum_ex.foo , MixedTypeEnum.fourtytwo ) def snake_case__( self : Tuple ) ->Union[str, Any]: @dataclass class snake_case_ : '''simple docstring''' SCREAMING_SNAKE_CASE : Literal["titi", "toto", 42] = "toto" snake_case_ = HfArgumentParser(_UpperCamelCase ) snake_case_ = argparse.ArgumentParser() expected.add_argument( '''--foo''' , default='''toto''' , choices=('''titi''', '''toto''', 4_2) , type=make_choice_type_function(['''titi''', '''toto''', 4_2] ) , ) self.argparsersEqual(_UpperCamelCase , _UpperCamelCase ) snake_case_ = parser.parse_args([] ) self.assertEqual(args.foo , '''toto''' ) snake_case_ = parser.parse_args(['''--foo''', '''titi'''] ) self.assertEqual(args.foo , '''titi''' ) snake_case_ = parser.parse_args(['''--foo''', '''42'''] ) self.assertEqual(args.foo , 4_2 ) def snake_case__( self : List[str] ) ->int: snake_case_ = HfArgumentParser(_UpperCamelCase ) snake_case_ = argparse.ArgumentParser() expected.add_argument('''--foo_int''' , nargs='''+''' , default=[] , type=_UpperCamelCase ) expected.add_argument('''--bar_int''' , nargs='''+''' , default=[1, 2, 3] , type=_UpperCamelCase ) expected.add_argument('''--foo_str''' , nargs='''+''' , default=['''Hallo''', '''Bonjour''', '''Hello'''] , type=_UpperCamelCase ) expected.add_argument('''--foo_float''' , nargs='''+''' , default=[0.1, 0.2, 0.3] , type=_UpperCamelCase ) self.argparsersEqual(_UpperCamelCase , _UpperCamelCase ) snake_case_ = parser.parse_args([] ) self.assertEqual( _UpperCamelCase , Namespace(foo_int=[] , bar_int=[1, 2, 3] , foo_str=['''Hallo''', '''Bonjour''', '''Hello'''] , foo_float=[0.1, 0.2, 0.3] ) , ) snake_case_ = parser.parse_args('''--foo_int 1 --bar_int 2 3 --foo_str a b c --foo_float 0.1 0.7'''.split() ) self.assertEqual(_UpperCamelCase , Namespace(foo_int=[1] , bar_int=[2, 3] , foo_str=['''a''', '''b''', '''c'''] , foo_float=[0.1, 0.7] ) ) def snake_case__( self : Optional[Any] ) ->List[Any]: snake_case_ = argparse.ArgumentParser() expected.add_argument('''--foo''' , default=_UpperCamelCase , type=_UpperCamelCase ) expected.add_argument('''--bar''' , default=_UpperCamelCase , type=_UpperCamelCase , help='''help message''' ) expected.add_argument('''--baz''' , default=_UpperCamelCase , type=_UpperCamelCase ) expected.add_argument('''--ces''' , nargs='''+''' , default=[] , type=_UpperCamelCase ) expected.add_argument('''--des''' , nargs='''+''' , default=[] , type=_UpperCamelCase ) snake_case_ = [OptionalExample] if is_python_no_less_than_3_10: dataclass_types.append(_UpperCamelCase ) for dataclass_type in dataclass_types: snake_case_ = HfArgumentParser(_UpperCamelCase ) self.argparsersEqual(_UpperCamelCase , _UpperCamelCase ) snake_case_ = parser.parse_args([] ) self.assertEqual(_UpperCamelCase , Namespace(foo=_UpperCamelCase , bar=_UpperCamelCase , baz=_UpperCamelCase , ces=[] , des=[] ) ) snake_case_ = parser.parse_args('''--foo 12 --bar 3.14 --baz 42 --ces a b c --des 1 2 3'''.split() ) self.assertEqual(_UpperCamelCase , Namespace(foo=1_2 , bar=3.14 , baz='''42''' , ces=['''a''', '''b''', '''c'''] , des=[1, 2, 3] ) ) def snake_case__( self : Union[str, Any] ) ->Optional[int]: snake_case_ = HfArgumentParser(_UpperCamelCase ) snake_case_ = argparse.ArgumentParser() expected.add_argument('''--required_list''' , nargs='''+''' , type=_UpperCamelCase , required=_UpperCamelCase ) expected.add_argument('''--required_str''' , type=_UpperCamelCase , required=_UpperCamelCase ) expected.add_argument( '''--required_enum''' , type=make_choice_type_function(['''titi''', '''toto'''] ) , choices=['''titi''', '''toto'''] , required=_UpperCamelCase , ) self.argparsersEqual(_UpperCamelCase , _UpperCamelCase ) def snake_case__( self : List[str] ) ->int: snake_case_ = HfArgumentParser(_UpperCamelCase ) snake_case_ = argparse.ArgumentParser() expected.add_argument('''--foo''' , type=_UpperCamelCase , required=_UpperCamelCase ) expected.add_argument( '''--required_enum''' , type=make_choice_type_function(['''titi''', '''toto'''] ) , choices=['''titi''', '''toto'''] , required=_UpperCamelCase , ) expected.add_argument('''--opt''' , type=_UpperCamelCase , default=_UpperCamelCase ) expected.add_argument('''--baz''' , default='''toto''' , type=_UpperCamelCase , help='''help message''' ) expected.add_argument('''--foo_str''' , nargs='''+''' , default=['''Hallo''', '''Bonjour''', '''Hello'''] , type=_UpperCamelCase ) self.argparsersEqual(_UpperCamelCase , _UpperCamelCase ) def snake_case__( self : Dict ) ->Any: snake_case_ = HfArgumentParser(_UpperCamelCase ) snake_case_ = { '''foo''': 1_2, '''bar''': 3.14, '''baz''': '''42''', '''flag''': True, } snake_case_ = parser.parse_dict(_UpperCamelCase )[0] snake_case_ = BasicExample(**_UpperCamelCase ) self.assertEqual(_UpperCamelCase , _UpperCamelCase ) def snake_case__( self : int ) ->Dict: snake_case_ = HfArgumentParser(_UpperCamelCase ) snake_case_ = { '''foo''': 1_2, '''bar''': 3.14, '''baz''': '''42''', '''flag''': True, '''extra''': 4_2, } self.assertRaises(_UpperCamelCase , parser.parse_dict , _UpperCamelCase , allow_extra_keys=_UpperCamelCase ) def snake_case__( self : str ) ->Tuple: snake_case_ = HfArgumentParser(_UpperCamelCase ) snake_case_ = { '''foo''': 1_2, '''bar''': 3.14, '''baz''': '''42''', '''flag''': True, } with tempfile.TemporaryDirectory() as tmp_dir: snake_case_ = os.path.join(_UpperCamelCase , '''temp_json''' ) os.mkdir(_UpperCamelCase ) with open(temp_local_path + '''.json''' , '''w+''' ) as f: json.dump(_UpperCamelCase , _UpperCamelCase ) snake_case_ = parser.parse_yaml_file(Path(temp_local_path + '''.json''' ) )[0] snake_case_ = BasicExample(**_UpperCamelCase ) self.assertEqual(_UpperCamelCase , _UpperCamelCase ) def snake_case__( self : Optional[int] ) ->str: snake_case_ = HfArgumentParser(_UpperCamelCase ) snake_case_ = { '''foo''': 1_2, '''bar''': 3.14, '''baz''': '''42''', '''flag''': True, } with tempfile.TemporaryDirectory() as tmp_dir: snake_case_ = os.path.join(_UpperCamelCase , '''temp_yaml''' ) os.mkdir(_UpperCamelCase ) with open(temp_local_path + '''.yaml''' , '''w+''' ) as f: yaml.dump(_UpperCamelCase , _UpperCamelCase ) snake_case_ = parser.parse_yaml_file(Path(temp_local_path + '''.yaml''' ) )[0] snake_case_ = BasicExample(**_UpperCamelCase ) self.assertEqual(_UpperCamelCase , _UpperCamelCase ) def snake_case__( self : Any ) ->Any: snake_case_ = HfArgumentParser(_UpperCamelCase ) self.assertIsNotNone(_UpperCamelCase )
39
import re from filelock import FileLock try: import nltk lowerCAmelCase_ = True except (ImportError, ModuleNotFoundError): lowerCAmelCase_ = False if NLTK_AVAILABLE: with FileLock('''.lock''') as lock: nltk.download('''punkt''', quiet=True) def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): re.sub('''<n>''' , '''''' , SCREAMING_SNAKE_CASE__ ) # remove pegasus newline char assert NLTK_AVAILABLE, "nltk must be installed to separate newlines between sentences. (pip install nltk)" return "\n".join(nltk.sent_tokenize(SCREAMING_SNAKE_CASE__ ) )
39
1
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ViTConfig, ViTForImageClassification, ViTImageProcessor, ViTModel from transformers.utils import logging logging.set_verbosity_info() lowerCAmelCase_ = logging.get_logger(__name__) def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__=False ): snake_case_ = [] for i in range(config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append((F'''blocks.{i}.norm1.weight''', F'''vit.encoder.layer.{i}.layernorm_before.weight''') ) rename_keys.append((F'''blocks.{i}.norm1.bias''', F'''vit.encoder.layer.{i}.layernorm_before.bias''') ) rename_keys.append((F'''blocks.{i}.attn.proj.weight''', F'''vit.encoder.layer.{i}.attention.output.dense.weight''') ) rename_keys.append((F'''blocks.{i}.attn.proj.bias''', F'''vit.encoder.layer.{i}.attention.output.dense.bias''') ) rename_keys.append((F'''blocks.{i}.norm2.weight''', F'''vit.encoder.layer.{i}.layernorm_after.weight''') ) rename_keys.append((F'''blocks.{i}.norm2.bias''', F'''vit.encoder.layer.{i}.layernorm_after.bias''') ) rename_keys.append((F'''blocks.{i}.mlp.fc1.weight''', F'''vit.encoder.layer.{i}.intermediate.dense.weight''') ) rename_keys.append((F'''blocks.{i}.mlp.fc1.bias''', F'''vit.encoder.layer.{i}.intermediate.dense.bias''') ) rename_keys.append((F'''blocks.{i}.mlp.fc2.weight''', F'''vit.encoder.layer.{i}.output.dense.weight''') ) rename_keys.append((F'''blocks.{i}.mlp.fc2.bias''', F'''vit.encoder.layer.{i}.output.dense.bias''') ) # projection layer + position embeddings rename_keys.extend( [ ('''cls_token''', '''vit.embeddings.cls_token'''), ('''patch_embed.proj.weight''', '''vit.embeddings.patch_embeddings.projection.weight'''), ('''patch_embed.proj.bias''', '''vit.embeddings.patch_embeddings.projection.bias'''), ('''pos_embed''', '''vit.embeddings.position_embeddings'''), ] ) if base_model: # layernorm + pooler rename_keys.extend( [ ('''norm.weight''', '''layernorm.weight'''), ('''norm.bias''', '''layernorm.bias'''), ] ) # if just the base model, we should remove "vit" from all keys that start with "vit" snake_case_ = [(pair[0], pair[1][4:]) if pair[1].startswith('''vit''' ) else pair for pair in rename_keys] else: # layernorm + classification head rename_keys.extend( [ ('''norm.weight''', '''vit.layernorm.weight'''), ('''norm.bias''', '''vit.layernorm.bias'''), ('''head.weight''', '''classifier.weight'''), ('''head.bias''', '''classifier.bias'''), ] ) return rename_keys def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__=False ): for i in range(config.num_hidden_layers ): if base_model: snake_case_ = '''''' else: snake_case_ = '''vit.''' # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) snake_case_ = state_dict.pop(F'''blocks.{i}.attn.qkv.weight''' ) snake_case_ = state_dict.pop(F'''blocks.{i}.attn.qkv.bias''' ) # next, add query, keys and values (in that order) to the state dict snake_case_ = in_proj_weight[ : config.hidden_size, : ] snake_case_ = in_proj_bias[: config.hidden_size] snake_case_ = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] snake_case_ = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] snake_case_ = in_proj_weight[ -config.hidden_size :, : ] snake_case_ = in_proj_bias[-config.hidden_size :] def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): snake_case_ = ['''head.weight''', '''head.bias'''] for k in ignore_keys: state_dict.pop(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): snake_case_ = dct.pop(SCREAMING_SNAKE_CASE__ ) snake_case_ = val def __SCREAMING_SNAKE_CASE (): snake_case_ = '''http://images.cocodataset.org/val2017/000000039769.jpg''' snake_case_ = Image.open(requests.get(SCREAMING_SNAKE_CASE__ , stream=SCREAMING_SNAKE_CASE__ ).raw ) return im @torch.no_grad() def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__=True ): snake_case_ = ViTConfig() # patch_size if model_name[-1] == "8": snake_case_ = 8 # set labels if required if not base_model: snake_case_ = 1000 snake_case_ = '''huggingface/label-files''' snake_case_ = '''imagenet-1k-id2label.json''' snake_case_ = json.load(open(hf_hub_download(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , repo_type='''dataset''' ) , '''r''' ) ) snake_case_ = {int(SCREAMING_SNAKE_CASE__ ): v for k, v in idalabel.items()} snake_case_ = idalabel snake_case_ = {v: k for k, v in idalabel.items()} # size of the architecture if model_name in ["dino_vits8", "dino_vits16"]: snake_case_ = 384 snake_case_ = 1536 snake_case_ = 12 snake_case_ = 6 # load original model from torch hub snake_case_ = torch.hub.load('''facebookresearch/dino:main''' , SCREAMING_SNAKE_CASE__ ) original_model.eval() # load state_dict of original model, remove and rename some keys snake_case_ = original_model.state_dict() if base_model: remove_classification_head_(SCREAMING_SNAKE_CASE__ ) snake_case_ = create_rename_keys(SCREAMING_SNAKE_CASE__ , base_model=SCREAMING_SNAKE_CASE__ ) for src, dest in rename_keys: rename_key(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) read_in_q_k_v(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) # load HuggingFace model if base_model: snake_case_ = ViTModel(SCREAMING_SNAKE_CASE__ , add_pooling_layer=SCREAMING_SNAKE_CASE__ ).eval() else: snake_case_ = ViTForImageClassification(SCREAMING_SNAKE_CASE__ ).eval() model.load_state_dict(SCREAMING_SNAKE_CASE__ ) # Check outputs on an image, prepared by ViTImageProcessor snake_case_ = ViTImageProcessor() snake_case_ = image_processor(images=prepare_img() , return_tensors='''pt''' ) snake_case_ = encoding['''pixel_values'''] snake_case_ = model(SCREAMING_SNAKE_CASE__ ) if base_model: snake_case_ = original_model(SCREAMING_SNAKE_CASE__ ) assert torch.allclose(SCREAMING_SNAKE_CASE__ , outputs.last_hidden_state[:, 0, :] , atol=1E-1 ) else: snake_case_ = original_model(SCREAMING_SNAKE_CASE__ ) assert logits.shape == outputs.logits.shape assert torch.allclose(SCREAMING_SNAKE_CASE__ , outputs.logits , atol=1E-3 ) Path(SCREAMING_SNAKE_CASE__ ).mkdir(exist_ok=SCREAMING_SNAKE_CASE__ ) print(F'''Saving model {model_name} to {pytorch_dump_folder_path}''' ) model.save_pretrained(SCREAMING_SNAKE_CASE__ ) print(F'''Saving image processor to {pytorch_dump_folder_path}''' ) image_processor.save_pretrained(SCREAMING_SNAKE_CASE__ ) if __name__ == "__main__": lowerCAmelCase_ = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default='''dino_vitb16''', type=str, help='''Name of the model trained with DINO you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) parser.add_argument( '''--base_model''', action='''store_true''', help='''Whether to only convert the base model (no projection head weights).''', ) parser.set_defaults(base_model=True) lowerCAmelCase_ = parser.parse_args() convert_vit_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.base_model)
39
def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): snake_case_ = [0 for i in range(r + 1 )] # nc0 = 1 snake_case_ = 1 for i in range(1 , n + 1 ): # to compute current row from previous row. snake_case_ = min(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) while j > 0: c[j] += c[j - 1] j -= 1 return c[r] print(binomial_coefficient(n=10, r=5))
39
1
import argparse import torch from diffusers.pipelines.stable_diffusion.convert_from_ckpt import download_from_original_stable_diffusion_ckpt if __name__ == "__main__": lowerCAmelCase_ = argparse.ArgumentParser() parser.add_argument( '''--checkpoint_path''', default=None, type=str, required=True, help='''Path to the checkpoint to convert.''' ) # !wget https://raw.githubusercontent.com/CompVis/stable-diffusion/main/configs/stable-diffusion/v1-inference.yaml parser.add_argument( '''--original_config_file''', default=None, type=str, help='''The YAML config file corresponding to the original architecture.''', ) parser.add_argument( '''--num_in_channels''', default=None, type=int, help='''The number of input channels. If `None` number of input channels will be automatically inferred.''', ) parser.add_argument( '''--scheduler_type''', default='''pndm''', type=str, help='''Type of scheduler to use. Should be one of [\'pndm\', \'lms\', \'ddim\', \'euler\', \'euler-ancestral\', \'dpm\']''', ) parser.add_argument( '''--pipeline_type''', default=None, type=str, help=( '''The pipeline type. One of \'FrozenOpenCLIPEmbedder\', \'FrozenCLIPEmbedder\', \'PaintByExample\'''' '''. If `None` pipeline will be automatically inferred.''' ), ) parser.add_argument( '''--image_size''', default=None, type=int, help=( '''The image size that the model was trained on. Use 512 for Stable Diffusion v1.X and Stable Siffusion v2''' ''' Base. Use 768 for Stable Diffusion v2.''' ), ) parser.add_argument( '''--prediction_type''', default=None, type=str, help=( '''The prediction type that the model was trained on. Use \'epsilon\' for Stable Diffusion v1.X and Stable''' ''' Diffusion v2 Base. Use \'v_prediction\' for Stable Diffusion v2.''' ), ) parser.add_argument( '''--extract_ema''', action='''store_true''', help=( '''Only relevant for checkpoints that have both EMA and non-EMA weights. Whether to extract the EMA weights''' ''' or not. Defaults to `False`. Add `--extract_ema` to extract the EMA weights. EMA weights usually yield''' ''' higher quality images for inference. Non-EMA weights are usually better to continue fine-tuning.''' ), ) parser.add_argument( '''--upcast_attention''', action='''store_true''', help=( '''Whether the attention computation should always be upcasted. This is necessary when running stable''' ''' diffusion 2.1.''' ), ) parser.add_argument( '''--from_safetensors''', action='''store_true''', help='''If `--checkpoint_path` is in `safetensors` format, load checkpoint with safetensors instead of PyTorch.''', ) parser.add_argument( '''--to_safetensors''', action='''store_true''', help='''Whether to store pipeline in safetensors format or not.''', ) parser.add_argument('''--dump_path''', default=None, type=str, required=True, help='''Path to the output model.''') parser.add_argument('''--device''', type=str, help='''Device to use (e.g. cpu, cuda:0, cuda:1, etc.)''') parser.add_argument( '''--stable_unclip''', type=str, default=None, required=False, help='''Set if this is a stable unCLIP model. One of \'txt2img\' or \'img2img\'.''', ) parser.add_argument( '''--stable_unclip_prior''', type=str, default=None, required=False, help='''Set if this is a stable unCLIP txt2img model. Selects which prior to use. If `--stable_unclip` is set to `txt2img`, the karlo prior (https://huggingface.co/kakaobrain/karlo-v1-alpha/tree/main/prior) is selected by default.''', ) parser.add_argument( '''--clip_stats_path''', type=str, help='''Path to the clip stats file. Only required if the stable unclip model\'s config specifies `model.params.noise_aug_config.params.clip_stats_path`.''', required=False, ) parser.add_argument( '''--controlnet''', action='''store_true''', default=None, help='''Set flag if this is a controlnet checkpoint.''' ) parser.add_argument('''--half''', action='''store_true''', help='''Save weights in half precision.''') parser.add_argument( '''--vae_path''', type=str, default=None, required=False, help='''Set to a path, hub id to an already converted vae to not convert it again.''', ) lowerCAmelCase_ = parser.parse_args() lowerCAmelCase_ = download_from_original_stable_diffusion_ckpt( checkpoint_path=args.checkpoint_path, original_config_file=args.original_config_file, image_size=args.image_size, prediction_type=args.prediction_type, model_type=args.pipeline_type, extract_ema=args.extract_ema, scheduler_type=args.scheduler_type, num_in_channels=args.num_in_channels, upcast_attention=args.upcast_attention, from_safetensors=args.from_safetensors, device=args.device, stable_unclip=args.stable_unclip, stable_unclip_prior=args.stable_unclip_prior, clip_stats_path=args.clip_stats_path, controlnet=args.controlnet, vae_path=args.vae_path, ) if args.half: pipe.to(torch_dtype=torch.floataa) if args.controlnet: # only save the controlnet model pipe.controlnet.save_pretrained(args.dump_path, safe_serialization=args.to_safetensors) else: pipe.save_pretrained(args.dump_path, safe_serialization=args.to_safetensors)
39
import argparse import math import os from copy import deepcopy import torch from audio_diffusion.models import DiffusionAttnUnetaD from diffusion import sampling from torch import nn from diffusers import DanceDiffusionPipeline, IPNDMScheduler, UNetaDModel lowerCAmelCase_ = { '''gwf-440k''': { '''url''': '''https://model-server.zqevans2.workers.dev/gwf-440k.ckpt''', '''sample_rate''': 4_80_00, '''sample_size''': 6_55_36, }, '''jmann-small-190k''': { '''url''': '''https://model-server.zqevans2.workers.dev/jmann-small-190k.ckpt''', '''sample_rate''': 4_80_00, '''sample_size''': 6_55_36, }, '''jmann-large-580k''': { '''url''': '''https://model-server.zqevans2.workers.dev/jmann-large-580k.ckpt''', '''sample_rate''': 4_80_00, '''sample_size''': 13_10_72, }, '''maestro-uncond-150k''': { '''url''': '''https://model-server.zqevans2.workers.dev/maestro-uncond-150k.ckpt''', '''sample_rate''': 1_60_00, '''sample_size''': 6_55_36, }, '''unlocked-uncond-250k''': { '''url''': '''https://model-server.zqevans2.workers.dev/unlocked-uncond-250k.ckpt''', '''sample_rate''': 1_60_00, '''sample_size''': 6_55_36, }, '''honk-140k''': { '''url''': '''https://model-server.zqevans2.workers.dev/honk-140k.ckpt''', '''sample_rate''': 1_60_00, '''sample_size''': 6_55_36, }, } def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): return torch.atana(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) / math.pi * 2 def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): snake_case_ = torch.sin(t * math.pi / 2 ) ** 2 snake_case_ = (1 - sigma**2) ** 0.5 return alpha_sigma_to_t(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) class snake_case_ ( __A ): '''simple docstring''' pass class snake_case_ ( nn.Module ): '''simple docstring''' def __init__( self : List[Any] , _UpperCamelCase : int ) ->Optional[int]: super().__init__() snake_case_ = DiffusionAttnUnetaD(_UpperCamelCase , n_attn_layers=4 ) snake_case_ = deepcopy(self.diffusion ) snake_case_ = torch.quasirandom.SobolEngine(1 , scramble=_UpperCamelCase ) def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): snake_case_ = MODELS_MAP[model_name]['''url'''] os.system(F'''wget {url} ./''' ) return F'''./{model_name}.ckpt''' lowerCAmelCase_ = { '''1''': '''resnets.0''', '''2''': '''attentions.0''', '''3''': '''resnets.1''', '''4''': '''attentions.1''', '''5''': '''resnets.2''', '''6''': '''attentions.2''', } lowerCAmelCase_ = { '''8''': '''resnets.0''', '''9''': '''attentions.0''', '''10''': '''resnets.1''', '''11''': '''attentions.1''', '''12''': '''resnets.2''', '''13''': '''attentions.2''', } lowerCAmelCase_ = { '''1''': '''resnets.0''', '''2''': '''attentions.0''', '''3''': '''resnets.1''', '''4''': '''attentions.1''', '''5''': '''resnets.2''', '''6''': '''attentions.2''', '''8''': '''resnets.3''', '''9''': '''attentions.3''', '''10''': '''resnets.4''', '''11''': '''attentions.4''', '''12''': '''resnets.5''', '''13''': '''attentions.5''', } lowerCAmelCase_ = { '''0''': '''resnets.0''', '''1''': '''resnets.1''', '''2''': '''resnets.2''', '''4''': '''resnets.0''', '''5''': '''resnets.1''', '''6''': '''resnets.2''', } lowerCAmelCase_ = { '''skip''': '''conv_skip''', '''main.0''': '''conv_1''', '''main.1''': '''group_norm_1''', '''main.3''': '''conv_2''', '''main.4''': '''group_norm_2''', } lowerCAmelCase_ = { '''norm''': '''group_norm''', '''qkv_proj''': ['''query''', '''key''', '''value'''], '''out_proj''': ['''proj_attn'''], } def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): if name.startswith('''skip''' ): return name.replace('''skip''' , RES_CONV_MAP['''skip'''] ) # name has to be of format main.{digit} if not name.startswith('''main.''' ): raise ValueError(F'''ResConvBlock error with {name}''' ) return name.replace(name[:6] , RES_CONV_MAP[name[:6]] ) def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): for key, value in ATTN_MAP.items(): if name.startswith(SCREAMING_SNAKE_CASE__ ) and not isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): return name.replace(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) elif name.startswith(SCREAMING_SNAKE_CASE__ ): return [name.replace(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) for v in value] raise ValueError(F'''Attn error with {name}''' ) def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__=13 ): snake_case_ = input_string if string.split('''.''' )[0] == "timestep_embed": return string.replace('''timestep_embed''' , '''time_proj''' ) snake_case_ = 0 if string.startswith('''net.3.''' ): depth += 1 snake_case_ = string[6:] elif string.startswith('''net.''' ): snake_case_ = string[4:] while string.startswith('''main.7.''' ): depth += 1 snake_case_ = string[7:] if string.startswith('''main.''' ): snake_case_ = string[5:] # mid block if string[:2].isdigit(): snake_case_ = string[:2] snake_case_ = string[2:] else: snake_case_ = string[0] snake_case_ = string[1:] if depth == max_depth: snake_case_ = MID_NUM_TO_LAYER[layer_num] snake_case_ = '''mid_block''' elif depth > 0 and int(SCREAMING_SNAKE_CASE__ ) < 7: snake_case_ = DOWN_NUM_TO_LAYER[layer_num] snake_case_ = F'''down_blocks.{depth}''' elif depth > 0 and int(SCREAMING_SNAKE_CASE__ ) > 7: snake_case_ = UP_NUM_TO_LAYER[layer_num] snake_case_ = F'''up_blocks.{max_depth - depth - 1}''' elif depth == 0: snake_case_ = DEPTH_0_TO_LAYER[layer_num] snake_case_ = F'''up_blocks.{max_depth - 1}''' if int(SCREAMING_SNAKE_CASE__ ) > 3 else '''down_blocks.0''' if not string_left.startswith('''.''' ): raise ValueError(F'''Naming error with {input_string} and string_left: {string_left}.''' ) snake_case_ = string_left[1:] if "resnets" in new_layer: snake_case_ = convert_resconv_naming(SCREAMING_SNAKE_CASE__ ) elif "attentions" in new_layer: snake_case_ = convert_attn_naming(SCREAMING_SNAKE_CASE__ ) snake_case_ = new_string_left if not isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): snake_case_ = prefix + '''.''' + new_layer + '''.''' + string_left else: snake_case_ = [prefix + '''.''' + new_layer + '''.''' + s for s in string_left] return new_string def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): snake_case_ = {} for k, v in state_dict.items(): if k.endswith('''kernel''' ): # up- and downsample layers, don't have trainable weights continue snake_case_ = rename(SCREAMING_SNAKE_CASE__ ) # check if we need to transform from Conv => Linear for attention if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): snake_case_ = transform_conv_attns(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) else: snake_case_ = v return new_state_dict def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): if len(SCREAMING_SNAKE_CASE__ ) == 1: if len(v.shape ) == 3: # weight snake_case_ = v[:, :, 0] else: # bias snake_case_ = v else: # qkv matrices snake_case_ = v.shape[0] snake_case_ = trippled_shape // 3 for i in range(3 ): if len(v.shape ) == 3: snake_case_ = v[i * single_shape : (i + 1) * single_shape, :, 0] else: snake_case_ = v[i * single_shape : (i + 1) * single_shape] return new_state_dict def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): snake_case_ = torch.device('''cuda''' if torch.cuda.is_available() else '''cpu''' ) snake_case_ = args.model_path.split('''/''' )[-1].split('''.''' )[0] if not os.path.isfile(args.model_path ): assert ( model_name == args.model_path ), F'''Make sure to provide one of the official model names {MODELS_MAP.keys()}''' snake_case_ = download(SCREAMING_SNAKE_CASE__ ) snake_case_ = MODELS_MAP[model_name]['''sample_rate'''] snake_case_ = MODELS_MAP[model_name]['''sample_size'''] snake_case_ = Object() snake_case_ = sample_size snake_case_ = sample_rate snake_case_ = 0 snake_case_ = UNetaDModel(sample_size=SCREAMING_SNAKE_CASE__ , sample_rate=SCREAMING_SNAKE_CASE__ ) snake_case_ = diffusers_model.state_dict() snake_case_ = DiffusionUncond(SCREAMING_SNAKE_CASE__ ) orig_model.load_state_dict(torch.load(args.model_path , map_location=SCREAMING_SNAKE_CASE__ )['''state_dict'''] ) snake_case_ = orig_model.diffusion_ema.eval() snake_case_ = orig_model.state_dict() snake_case_ = rename_orig_weights(SCREAMING_SNAKE_CASE__ ) snake_case_ = set(renamed_state_dict.keys() ) - set(diffusers_state_dict.keys() ) snake_case_ = set(diffusers_state_dict.keys() ) - set(renamed_state_dict.keys() ) assert len(SCREAMING_SNAKE_CASE__ ) == 0, F'''Problem with {renamed_minus_diffusers}''' assert all(k.endswith('''kernel''' ) for k in list(SCREAMING_SNAKE_CASE__ ) ), F'''Problem with {diffusers_minus_renamed}''' for key, value in renamed_state_dict.items(): assert ( diffusers_state_dict[key].squeeze().shape == value.squeeze().shape ), F'''Shape for {key} doesn\'t match. Diffusers: {diffusers_state_dict[key].shape} vs. {value.shape}''' if key == "time_proj.weight": snake_case_ = value.squeeze() snake_case_ = value diffusers_model.load_state_dict(SCREAMING_SNAKE_CASE__ ) snake_case_ = 100 snake_case_ = 33 snake_case_ = IPNDMScheduler(num_train_timesteps=SCREAMING_SNAKE_CASE__ ) snake_case_ = torch.manual_seed(SCREAMING_SNAKE_CASE__ ) snake_case_ = torch.randn([1, 2, config.sample_size] , generator=SCREAMING_SNAKE_CASE__ ).to(SCREAMING_SNAKE_CASE__ ) snake_case_ = torch.linspace(1 , 0 , steps + 1 , device=SCREAMING_SNAKE_CASE__ )[:-1] snake_case_ = get_crash_schedule(SCREAMING_SNAKE_CASE__ ) snake_case_ = DanceDiffusionPipeline(unet=SCREAMING_SNAKE_CASE__ , scheduler=SCREAMING_SNAKE_CASE__ ) snake_case_ = torch.manual_seed(33 ) snake_case_ = pipe(num_inference_steps=SCREAMING_SNAKE_CASE__ , generator=SCREAMING_SNAKE_CASE__ ).audios snake_case_ = sampling.iplms_sample(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , {} ) snake_case_ = generated.clamp(-1 , 1 ) snake_case_ = (generated - audio).abs().sum() snake_case_ = (generated - audio).abs().max() if args.save: pipe.save_pretrained(args.checkpoint_path ) print('''Diff sum''' , SCREAMING_SNAKE_CASE__ ) print('''Diff max''' , SCREAMING_SNAKE_CASE__ ) assert diff_max < 1E-3, F'''Diff max: {diff_max} is too much :-/''' print(F'''Conversion for {model_name} successful!''' ) if __name__ == "__main__": lowerCAmelCase_ = argparse.ArgumentParser() parser.add_argument('''--model_path''', default=None, type=str, required=True, help='''Path to the model to convert.''') parser.add_argument( '''--save''', default=True, type=bool, required=False, help='''Whether to save the converted model or not.''' ) parser.add_argument('''--checkpoint_path''', default=None, type=str, required=True, help='''Path to the output model.''') lowerCAmelCase_ = parser.parse_args() main(args)
39
1
from typing import Any, Dict, Optional import torch import torch.nn.functional as F from torch import nn from ..utils import maybe_allow_in_graph from .activations import get_activation from .attention_processor import Attention from .embeddings import CombinedTimestepLabelEmbeddings @maybe_allow_in_graph class snake_case_ ( nn.Module ): '''simple docstring''' def __init__( self : str , _UpperCamelCase : int , _UpperCamelCase : int , _UpperCamelCase : int , _UpperCamelCase : List[Any]=0.0 , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : str = "geglu" , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : bool = False , _UpperCamelCase : bool = False , _UpperCamelCase : bool = False , _UpperCamelCase : bool = False , _UpperCamelCase : bool = True , _UpperCamelCase : str = "layer_norm" , _UpperCamelCase : bool = False , ) ->Optional[int]: super().__init__() snake_case_ = only_cross_attention snake_case_ = (num_embeds_ada_norm is not None) and norm_type == '''ada_norm_zero''' snake_case_ = (num_embeds_ada_norm is not None) and norm_type == '''ada_norm''' if norm_type in ("ada_norm", "ada_norm_zero") and num_embeds_ada_norm is None: raise ValueError( f'''`norm_type` is set to {norm_type}, but `num_embeds_ada_norm` is not defined. Please make sure to''' f''' define `num_embeds_ada_norm` if setting `norm_type` to {norm_type}.''' ) # Define 3 blocks. Each block has its own normalization layer. # 1. Self-Attn if self.use_ada_layer_norm: snake_case_ = AdaLayerNorm(_UpperCamelCase , _UpperCamelCase ) elif self.use_ada_layer_norm_zero: snake_case_ = AdaLayerNormZero(_UpperCamelCase , _UpperCamelCase ) else: snake_case_ = nn.LayerNorm(_UpperCamelCase , elementwise_affine=_UpperCamelCase ) snake_case_ = Attention( query_dim=_UpperCamelCase , heads=_UpperCamelCase , dim_head=_UpperCamelCase , dropout=_UpperCamelCase , bias=_UpperCamelCase , cross_attention_dim=cross_attention_dim if only_cross_attention else None , upcast_attention=_UpperCamelCase , ) # 2. Cross-Attn if cross_attention_dim is not None or double_self_attention: # We currently only use AdaLayerNormZero for self attention where there will only be one attention block. # I.e. the number of returned modulation chunks from AdaLayerZero would not make sense if returned during # the second cross attention block. snake_case_ = ( AdaLayerNorm(_UpperCamelCase , _UpperCamelCase ) if self.use_ada_layer_norm else nn.LayerNorm(_UpperCamelCase , elementwise_affine=_UpperCamelCase ) ) snake_case_ = Attention( query_dim=_UpperCamelCase , cross_attention_dim=cross_attention_dim if not double_self_attention else None , heads=_UpperCamelCase , dim_head=_UpperCamelCase , dropout=_UpperCamelCase , bias=_UpperCamelCase , upcast_attention=_UpperCamelCase , ) # is self-attn if encoder_hidden_states is none else: snake_case_ = None snake_case_ = None # 3. Feed-forward snake_case_ = nn.LayerNorm(_UpperCamelCase , elementwise_affine=_UpperCamelCase ) snake_case_ = FeedForward(_UpperCamelCase , dropout=_UpperCamelCase , activation_fn=_UpperCamelCase , final_dropout=_UpperCamelCase ) # let chunk size default to None snake_case_ = None snake_case_ = 0 def snake_case__( self : str , _UpperCamelCase : Optional[int] , _UpperCamelCase : int ) ->str: # Sets chunk feed-forward snake_case_ = chunk_size snake_case_ = dim def snake_case__( self : List[str] , _UpperCamelCase : torch.FloatTensor , _UpperCamelCase : Optional[torch.FloatTensor] = None , _UpperCamelCase : Optional[torch.FloatTensor] = None , _UpperCamelCase : Optional[torch.FloatTensor] = None , _UpperCamelCase : Optional[torch.LongTensor] = None , _UpperCamelCase : Dict[str, Any] = None , _UpperCamelCase : Optional[torch.LongTensor] = None , ) ->str: # Notice that normalization is always applied before the real computation in the following blocks. # 1. Self-Attention if self.use_ada_layer_norm: snake_case_ = self.norma(_UpperCamelCase , _UpperCamelCase ) elif self.use_ada_layer_norm_zero: snake_case_, snake_case_, snake_case_, snake_case_, snake_case_ = self.norma( _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , hidden_dtype=hidden_states.dtype ) else: snake_case_ = self.norma(_UpperCamelCase ) snake_case_ = cross_attention_kwargs if cross_attention_kwargs is not None else {} snake_case_ = self.attna( _UpperCamelCase , encoder_hidden_states=encoder_hidden_states if self.only_cross_attention else None , attention_mask=_UpperCamelCase , **_UpperCamelCase , ) if self.use_ada_layer_norm_zero: snake_case_ = gate_msa.unsqueeze(1 ) * attn_output snake_case_ = attn_output + hidden_states # 2. Cross-Attention if self.attna is not None: snake_case_ = ( self.norma(_UpperCamelCase , _UpperCamelCase ) if self.use_ada_layer_norm else self.norma(_UpperCamelCase ) ) snake_case_ = self.attna( _UpperCamelCase , encoder_hidden_states=_UpperCamelCase , attention_mask=_UpperCamelCase , **_UpperCamelCase , ) snake_case_ = attn_output + hidden_states # 3. Feed-forward snake_case_ = self.norma(_UpperCamelCase ) if self.use_ada_layer_norm_zero: snake_case_ = norm_hidden_states * (1 + scale_mlp[:, None]) + shift_mlp[:, None] if self._chunk_size is not None: # "feed_forward_chunk_size" can be used to save memory if norm_hidden_states.shape[self._chunk_dim] % self._chunk_size != 0: raise ValueError( f'''`hidden_states` dimension to be chunked: {norm_hidden_states.shape[self._chunk_dim]} has to be divisible by chunk size: {self._chunk_size}. Make sure to set an appropriate `chunk_size` when calling `unet.enable_forward_chunking`.''' ) snake_case_ = norm_hidden_states.shape[self._chunk_dim] // self._chunk_size snake_case_ = torch.cat( [self.ff(_UpperCamelCase ) for hid_slice in norm_hidden_states.chunk(_UpperCamelCase , dim=self._chunk_dim )] , dim=self._chunk_dim , ) else: snake_case_ = self.ff(_UpperCamelCase ) if self.use_ada_layer_norm_zero: snake_case_ = gate_mlp.unsqueeze(1 ) * ff_output snake_case_ = ff_output + hidden_states return hidden_states class snake_case_ ( nn.Module ): '''simple docstring''' def __init__( self : str , _UpperCamelCase : int , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : int = 4 , _UpperCamelCase : float = 0.0 , _UpperCamelCase : str = "geglu" , _UpperCamelCase : bool = False , ) ->str: super().__init__() snake_case_ = int(dim * mult ) snake_case_ = dim_out if dim_out is not None else dim if activation_fn == "gelu": snake_case_ = GELU(_UpperCamelCase , _UpperCamelCase ) if activation_fn == "gelu-approximate": snake_case_ = GELU(_UpperCamelCase , _UpperCamelCase , approximate='''tanh''' ) elif activation_fn == "geglu": snake_case_ = GEGLU(_UpperCamelCase , _UpperCamelCase ) elif activation_fn == "geglu-approximate": snake_case_ = ApproximateGELU(_UpperCamelCase , _UpperCamelCase ) snake_case_ = nn.ModuleList([] ) # project in self.net.append(_UpperCamelCase ) # project dropout self.net.append(nn.Dropout(_UpperCamelCase ) ) # project out self.net.append(nn.Linear(_UpperCamelCase , _UpperCamelCase ) ) # FF as used in Vision Transformer, MLP-Mixer, etc. have a final dropout if final_dropout: self.net.append(nn.Dropout(_UpperCamelCase ) ) def snake_case__( self : Any , _UpperCamelCase : Optional[Any] ) ->int: for module in self.net: snake_case_ = module(_UpperCamelCase ) return hidden_states class snake_case_ ( nn.Module ): '''simple docstring''' def __init__( self : Tuple , _UpperCamelCase : int , _UpperCamelCase : int , _UpperCamelCase : str = "none" ) ->Any: super().__init__() snake_case_ = nn.Linear(_UpperCamelCase , _UpperCamelCase ) snake_case_ = approximate def snake_case__( self : List[str] , _UpperCamelCase : Tuple ) ->List[Any]: if gate.device.type != "mps": return F.gelu(_UpperCamelCase , approximate=self.approximate ) # mps: gelu is not implemented for float16 return F.gelu(gate.to(dtype=torch.floataa ) , approximate=self.approximate ).to(dtype=gate.dtype ) def snake_case__( self : List[Any] , _UpperCamelCase : Optional[int] ) ->List[str]: snake_case_ = self.proj(_UpperCamelCase ) snake_case_ = self.gelu(_UpperCamelCase ) return hidden_states class snake_case_ ( nn.Module ): '''simple docstring''' def __init__( self : Union[str, Any] , _UpperCamelCase : int , _UpperCamelCase : int ) ->Union[str, Any]: super().__init__() snake_case_ = nn.Linear(_UpperCamelCase , dim_out * 2 ) def snake_case__( self : Union[str, Any] , _UpperCamelCase : List[str] ) ->str: if gate.device.type != "mps": return F.gelu(_UpperCamelCase ) # mps: gelu is not implemented for float16 return F.gelu(gate.to(dtype=torch.floataa ) ).to(dtype=gate.dtype ) def snake_case__( self : str , _UpperCamelCase : List[str] ) ->Optional[int]: snake_case_, snake_case_ = self.proj(_UpperCamelCase ).chunk(2 , dim=-1 ) return hidden_states * self.gelu(_UpperCamelCase ) class snake_case_ ( nn.Module ): '''simple docstring''' def __init__( self : Optional[Any] , _UpperCamelCase : int , _UpperCamelCase : int ) ->Optional[Any]: super().__init__() snake_case_ = nn.Linear(_UpperCamelCase , _UpperCamelCase ) def snake_case__( self : List[str] , _UpperCamelCase : int ) ->Any: snake_case_ = self.proj(_UpperCamelCase ) return x * torch.sigmoid(1.702 * x ) class snake_case_ ( nn.Module ): '''simple docstring''' def __init__( self : Optional[Any] , _UpperCamelCase : List[str] , _UpperCamelCase : List[Any] ) ->List[Any]: super().__init__() snake_case_ = nn.Embedding(_UpperCamelCase , _UpperCamelCase ) snake_case_ = nn.SiLU() snake_case_ = nn.Linear(_UpperCamelCase , embedding_dim * 2 ) snake_case_ = nn.LayerNorm(_UpperCamelCase , elementwise_affine=_UpperCamelCase ) def snake_case__( self : Dict , _UpperCamelCase : Any , _UpperCamelCase : Optional[Any] ) ->Union[str, Any]: snake_case_ = self.linear(self.silu(self.emb(_UpperCamelCase ) ) ) snake_case_, snake_case_ = torch.chunk(_UpperCamelCase , 2 ) snake_case_ = self.norm(_UpperCamelCase ) * (1 + scale) + shift return x class snake_case_ ( nn.Module ): '''simple docstring''' def __init__( self : Any , _UpperCamelCase : int , _UpperCamelCase : int ) ->str: super().__init__() snake_case_ = CombinedTimestepLabelEmbeddings(_UpperCamelCase , _UpperCamelCase ) snake_case_ = nn.SiLU() snake_case_ = nn.Linear(_UpperCamelCase , 6 * embedding_dim , bias=_UpperCamelCase ) snake_case_ = nn.LayerNorm(_UpperCamelCase , elementwise_affine=_UpperCamelCase , eps=1e-6 ) def snake_case__( self : List[str] , _UpperCamelCase : List[Any] , _UpperCamelCase : List[str] , _UpperCamelCase : Dict , _UpperCamelCase : Dict=None ) ->Optional[int]: snake_case_ = self.linear(self.silu(self.emb(_UpperCamelCase , _UpperCamelCase , hidden_dtype=_UpperCamelCase ) ) ) snake_case_, snake_case_, snake_case_, snake_case_, snake_case_, snake_case_ = emb.chunk(6 , dim=1 ) snake_case_ = self.norm(_UpperCamelCase ) * (1 + scale_msa[:, None]) + shift_msa[:, None] return x, gate_msa, shift_mlp, scale_mlp, gate_mlp class snake_case_ ( nn.Module ): '''simple docstring''' def __init__( self : List[str] , _UpperCamelCase : int , _UpperCamelCase : int , _UpperCamelCase : int , _UpperCamelCase : Optional[str] = None , _UpperCamelCase : float = 1e-5 ) ->Optional[Any]: super().__init__() snake_case_ = num_groups snake_case_ = eps if act_fn is None: snake_case_ = None else: snake_case_ = get_activation(_UpperCamelCase ) snake_case_ = nn.Linear(_UpperCamelCase , out_dim * 2 ) def snake_case__( self : List[str] , _UpperCamelCase : Any , _UpperCamelCase : Tuple ) ->List[Any]: if self.act: snake_case_ = self.act(_UpperCamelCase ) snake_case_ = self.linear(_UpperCamelCase ) snake_case_ = emb[:, :, None, None] snake_case_, snake_case_ = emb.chunk(2 , dim=1 ) snake_case_ = F.group_norm(_UpperCamelCase , self.num_groups , eps=self.eps ) snake_case_ = x * (1 + scale) + shift return x
39
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCAmelCase_ = {'''configuration_vit_msn''': ['''VIT_MSN_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''ViTMSNConfig''']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ = [ '''VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST''', '''ViTMSNModel''', '''ViTMSNForImageClassification''', '''ViTMSNPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_vit_msn import VIT_MSN_PRETRAINED_CONFIG_ARCHIVE_MAP, ViTMSNConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vit_msn import ( VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST, ViTMSNForImageClassification, ViTMSNModel, ViTMSNPreTrainedModel, ) else: import sys lowerCAmelCase_ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
39
1
from __future__ import annotations lowerCAmelCase_ = 1.60_21E-19 # units = C def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_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()
39
from __future__ import annotations import os import tempfile import unittest import numpy as np from huggingface_hub import hf_hub_download from transformers import is_tensorflow_text_available, is_tf_available from transformers.testing_utils import require_tensorflow_text, require_tf, slow from ..test_modeling_tf_common import floats_tensor from .test_framework_agnostic import GenerationIntegrationTestsMixin if is_tf_available(): import tensorflow as tf from transformers import ( AutoTokenizer, TFAutoModelForCausalLM, TFAutoModelForSeqaSeqLM, TFAutoModelForSpeechSeqaSeq, TFAutoModelForVisionaSeq, TFBartForConditionalGeneration, TFLogitsProcessorList, TFMinLengthLogitsProcessor, tf_top_k_top_p_filtering, ) if is_tensorflow_text_available(): import tensorflow_text as text @require_tf class snake_case_ ( unittest.TestCase ): '''simple docstring''' def snake_case__( self : Optional[Any] ) ->Any: snake_case_ = tf.convert_to_tensor( [ [ 8.2220991, # 3rd highest value; idx. 0 -0.5620044, 5.23229752, 4.0386393, -6.8798378, -0.54785802, -3.2012153, 2.92777176, 1.88171953, 7.35341276, # 5th highest value; idx. 9 8.43207833, # 2nd highest value; idx. 10 -9.85711836, -5.96209236, -1.13039161, -7.1115294, -0.8369633, -5.3186408, 7.06427407, 0.81369344, -0.82023817, -5.9179796, 0.58813443, -6.99778438, 4.71551189, -0.18771637, 7.44020759, # 4th highest value; idx. 25 9.38450987, # 1st highest value; idx. 26 2.12662941, -9.32562038, 2.35652522, ], # cummulative prob of 5 highest values <= 0.6 [ 0.58425518, 4.53139238, -5.57510464, -6.28030699, -7.19529503, -4.02122551, 1.39337037, -6.06707057, 1.59480517, -9.643119, 0.03907799, 0.67231762, -8.88206726, 6.27115922, # 4th highest value; idx. 13 2.28520723, 4.82767506, 4.30421368, 8.8275313, # 2nd highest value; idx. 17 5.44029958, # 5th highest value; idx. 18 -4.4735794, 7.38579536, # 3rd highest value; idx. 20 -2.91051663, 2.61946077, -2.5674762, -9.48959302, -4.02922645, -1.35416918, 9.67702323, # 1st highest value; idx. 27 -5.89478553, 1.85370467, ], # cummulative prob of 5 highest values <= 0.6 ] , dtype=tf.floataa , ) snake_case_ = tf.convert_to_tensor( [[0, 0], [0, 9], [0, 1_0], [0, 2_5], [0, 2_6], [1, 1_3], [1, 1_7], [1, 1_8], [1, 2_0], [1, 2_7]] , dtype=tf.intaa , ) # expected non filtered idx as noted above snake_case_ = tf.convert_to_tensor( [8.222099, 7.3534126, 8.432078, 7.4402075, 9.38451, 6.271159, 8.827531, 5.4402995, 7.3857956, 9.677023] , dtype=tf.floataa , ) # expected non filtered values as noted above snake_case_ = tf_top_k_top_p_filtering(_UpperCamelCase , top_k=1_0 , top_p=0.6 , min_tokens_to_keep=4 ) snake_case_ = output[output != -float('''inf''' )] snake_case_ = tf.cast( tf.where(tf.not_equal(_UpperCamelCase , tf.constant(-float('''inf''' ) , dtype=tf.floataa ) ) ) , dtype=tf.intaa , ) tf.debugging.assert_near(_UpperCamelCase , _UpperCamelCase , rtol=1e-12 ) tf.debugging.assert_equal(_UpperCamelCase , _UpperCamelCase ) @require_tf class snake_case_ ( unittest.TestCase , __A ): '''simple docstring''' if is_tf_available(): SCREAMING_SNAKE_CASE : Optional[int] = { "AutoModelForCausalLM": TFAutoModelForCausalLM, "AutoModelForSpeechSeq2Seq": TFAutoModelForSpeechSeqaSeq, "AutoModelForSeq2SeqLM": TFAutoModelForSeqaSeqLM, "AutoModelForVision2Seq": TFAutoModelForVisionaSeq, "LogitsProcessorList": TFLogitsProcessorList, "MinLengthLogitsProcessor": TFMinLengthLogitsProcessor, "create_tensor_fn": tf.convert_to_tensor, "floats_tensor": floats_tensor, "return_tensors": "tf", } @slow def snake_case__( self : List[Any] ) ->Optional[int]: # TF-only test: tf.saved_model export snake_case_ = TFAutoModelForCausalLM.from_pretrained('''hf-internal-testing/tiny-random-gpt2''' ) snake_case_ = 2 snake_case_ = 2 class snake_case_ ( tf.Module ): '''simple docstring''' def __init__( self : Optional[Any] , _UpperCamelCase : Optional[int] ) ->List[Any]: super(_UpperCamelCase , self ).__init__() snake_case_ = model @tf.function( input_signature=( tf.TensorSpec((None, input_length) , tf.intaa , name='''input_ids''' ), tf.TensorSpec((None, input_length) , tf.intaa , name='''attention_mask''' ), ) , jit_compile=_UpperCamelCase , ) def snake_case__( self : List[Any] , _UpperCamelCase : int , _UpperCamelCase : Union[str, Any] ) ->List[Any]: snake_case_ = self.model.generate( input_ids=_UpperCamelCase , attention_mask=_UpperCamelCase , max_new_tokens=_UpperCamelCase , return_dict_in_generate=_UpperCamelCase , ) return {"sequences": outputs["sequences"]} snake_case_ = [[2, 0], [1_0_2, 1_0_3]] snake_case_ = [[1, 0], [1, 1]] snake_case_ = DummyModel(model=_UpperCamelCase ) with tempfile.TemporaryDirectory() as tmp_dir: tf.saved_model.save(_UpperCamelCase , _UpperCamelCase , signatures={'''serving_default''': dummy_model.serving} ) snake_case_ = tf.saved_model.load(_UpperCamelCase ).signatures['''serving_default'''] for batch_size in range(1 , len(_UpperCamelCase ) + 1 ): snake_case_ = { '''input_ids''': tf.constant(dummy_input_ids[:batch_size] ), '''attention_mask''': tf.constant(dummy_attention_masks[:batch_size] ), } snake_case_ = serving_func(**_UpperCamelCase )['''sequences'''] snake_case_ = test_model.generate(**_UpperCamelCase , max_new_tokens=_UpperCamelCase ) tf.debugging.assert_equal(_UpperCamelCase , _UpperCamelCase ) @slow def snake_case__( self : List[str] ) ->int: # TF-only test: tf.saved_model export snake_case_ = TFAutoModelForCausalLM.from_pretrained('''hf-internal-testing/tiny-random-gpt2''' ) snake_case_ = 1 snake_case_ = 2 class snake_case_ ( tf.Module ): '''simple docstring''' def __init__( self : str , _UpperCamelCase : Any ) ->List[str]: super(_UpperCamelCase , self ).__init__() snake_case_ = model @tf.function( input_signature=( tf.TensorSpec((batch_size, None) , tf.intaa , name='''input_ids''' ), tf.TensorSpec((batch_size, None) , tf.intaa , name='''attention_mask''' ), ) , jit_compile=_UpperCamelCase , ) def snake_case__( self : int , _UpperCamelCase : Tuple , _UpperCamelCase : List[Any] ) ->Optional[int]: snake_case_ = self.model.generate( input_ids=_UpperCamelCase , attention_mask=_UpperCamelCase , max_new_tokens=_UpperCamelCase , return_dict_in_generate=_UpperCamelCase , ) return {"sequences": outputs["sequences"]} snake_case_ = [[2], [1_0_2, 1_0_3]] snake_case_ = [[1], [1, 1]] snake_case_ = DummyModel(model=_UpperCamelCase ) with tempfile.TemporaryDirectory() as tmp_dir: tf.saved_model.save(_UpperCamelCase , _UpperCamelCase , signatures={'''serving_default''': dummy_model.serving} ) snake_case_ = tf.saved_model.load(_UpperCamelCase ).signatures['''serving_default'''] for input_row in range(len(_UpperCamelCase ) ): snake_case_ = { '''input_ids''': tf.constant([dummy_input_ids[input_row]] ), '''attention_mask''': tf.constant([dummy_attention_masks[input_row]] ), } snake_case_ = serving_func(**_UpperCamelCase )['''sequences'''] snake_case_ = test_model.generate(**_UpperCamelCase , max_new_tokens=_UpperCamelCase ) tf.debugging.assert_equal(_UpperCamelCase , _UpperCamelCase ) @slow @require_tensorflow_text def snake_case__( self : Optional[Any] ) ->List[Any]: # TF-only test: tf.saved_model export with tempfile.TemporaryDirectory() as tmp_dir: # file needed to load the TF tokenizer hf_hub_download(repo_id='''google/flan-t5-small''' , filename='''spiece.model''' , local_dir=_UpperCamelCase ) class snake_case_ ( tf.keras.layers.Layer ): '''simple docstring''' def __init__( self : Tuple ) ->List[Any]: super().__init__() snake_case_ = text.SentencepieceTokenizer( model=tf.io.gfile.GFile(os.path.join(_UpperCamelCase , '''spiece.model''' ) , '''rb''' ).read() ) snake_case_ = TFAutoModelForSeqaSeqLM.from_pretrained('''hf-internal-testing/tiny-random-t5''' ) def snake_case__( self : Optional[Any] , _UpperCamelCase : List[Any] , *_UpperCamelCase : Optional[int] , **_UpperCamelCase : str ) ->List[Any]: snake_case_ = self.tokenizer.tokenize(_UpperCamelCase ) snake_case_, snake_case_ = text.pad_model_inputs( _UpperCamelCase , max_seq_length=6_4 , pad_value=self.model.config.pad_token_id ) snake_case_ = self.model.generate(input_ids=_UpperCamelCase , attention_mask=_UpperCamelCase ) return self.tokenizer.detokenize(_UpperCamelCase ) snake_case_ = CompleteSentenceTransformer() snake_case_ = tf.keras.layers.Input(shape=(1,) , dtype=tf.string , name='''inputs''' ) snake_case_ = complete_model(_UpperCamelCase ) snake_case_ = tf.keras.Model(_UpperCamelCase , _UpperCamelCase ) keras_model.save(_UpperCamelCase ) def snake_case__( self : Any ) ->List[Any]: # Has PT equivalent: this test relies on random sampling snake_case_ = { '''do_sample''': True, '''num_beams''': 1, '''top_p''': 0.7, '''top_k''': 1_0, '''temperature''': 0.7, } snake_case_ = 1_4 snake_case_ = AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-gpt2''' ) snake_case_ = '''Hello, my dog is cute and''' snake_case_ = tokenizer(_UpperCamelCase , return_tensors='''tf''' ) snake_case_ = TFAutoModelForCausalLM.from_pretrained('''hf-internal-testing/tiny-random-gpt2''' ) snake_case_ = 6_3_8 # forces the generation to happen on CPU, to avoid GPU-related quirks with tf.device(''':/CPU:0''' ): tf.random.set_seed(0 ) snake_case_ = model.generate(**_UpperCamelCase , eos_token_id=_UpperCamelCase , **_UpperCamelCase ) self.assertTrue(expectation == len(generated_tokens[0] ) ) snake_case_ = [6_3_8, 1_9_8] with tf.device(''':/CPU:0''' ): tf.random.set_seed(0 ) snake_case_ = model.generate(**_UpperCamelCase , eos_token_id=_UpperCamelCase , **_UpperCamelCase ) self.assertTrue(expectation == len(generated_tokens[0] ) ) def snake_case__( self : str ) ->Dict: # Has PT equivalent: ample use of framework-specific code snake_case_ = AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-bart''' ) snake_case_ = '''Hugging Face is a technology company based in New York and Paris.''' snake_case_ = bart_tokenizer(_UpperCamelCase , return_tensors='''tf''' ).input_ids snake_case_ = TFBartForConditionalGeneration.from_pretrained('''hf-internal-testing/tiny-random-bart''' ) snake_case_ = bart_model.generate(_UpperCamelCase ).numpy() class snake_case_ ( __A ): '''simple docstring''' def snake_case__( self : str , _UpperCamelCase : Any , _UpperCamelCase : Tuple=None , **_UpperCamelCase : Optional[int] ) ->List[str]: return super().call(_UpperCamelCase , **_UpperCamelCase ) snake_case_ = FakeBart.from_pretrained('''hf-internal-testing/tiny-random-bart''' ) snake_case_ = bart_model.generate(_UpperCamelCase , foo='''bar''' ).numpy() self.assertTrue(np.array_equal(_UpperCamelCase , _UpperCamelCase ) ) class snake_case_ ( bart_model.model.encoder.__class__ ): '''simple docstring''' def snake_case__( self : Union[str, Any] , _UpperCamelCase : str , **_UpperCamelCase : Tuple ) ->Optional[Any]: return super().call(_UpperCamelCase , **_UpperCamelCase ) snake_case_ = FakeEncoder(bart_model.config , bart_model.model.shared ) snake_case_ = fake_encoder # Normal generation still works (the output will be different because the encoder weights are different) snake_case_ = bart_model.generate(_UpperCamelCase ).numpy() with self.assertRaises(_UpperCamelCase ): # FakeEncoder.call() accepts **kwargs -> no filtering -> value error due to unexpected input "foo" bart_model.generate(_UpperCamelCase , foo='''bar''' )
39
1
import unittest from transformers import SPIECE_UNDERLINE from transformers.models.speechta import SpeechTaTokenizer from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow from transformers.tokenization_utils import AddedToken from ...test_tokenization_common import TokenizerTesterMixin lowerCAmelCase_ = get_tests_dir('''fixtures/test_sentencepiece_bpe_char.model''') @require_sentencepiece @require_tokenizers class snake_case_ ( __A , unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = SpeechTaTokenizer SCREAMING_SNAKE_CASE : Tuple = False SCREAMING_SNAKE_CASE : List[Any] = True def snake_case__( self : int ) ->List[Any]: super().setUp() # We have a SentencePiece fixture for testing snake_case_ = SpeechTaTokenizer(_UpperCamelCase ) snake_case_ = AddedToken('''<mask>''' , lstrip=_UpperCamelCase , rstrip=_UpperCamelCase ) snake_case_ = mask_token tokenizer.add_special_tokens({'''mask_token''': mask_token} ) tokenizer.add_tokens(['''<ctc_blank>'''] ) tokenizer.save_pretrained(self.tmpdirname ) def snake_case__( self : List[Any] , _UpperCamelCase : List[Any] ) ->Tuple: snake_case_ = '''this is a test''' snake_case_ = '''this is a test''' return input_text, output_text def snake_case__( self : str , _UpperCamelCase : Optional[Any] , _UpperCamelCase : Optional[int]=False , _UpperCamelCase : Tuple=2_0 , _UpperCamelCase : Dict=5 ) ->Optional[Any]: snake_case_, snake_case_ = self.get_input_output_texts(_UpperCamelCase ) snake_case_ = tokenizer.encode(_UpperCamelCase , add_special_tokens=_UpperCamelCase ) snake_case_ = tokenizer.decode(_UpperCamelCase , clean_up_tokenization_spaces=_UpperCamelCase ) return text, ids def snake_case__( self : str ) ->Union[str, Any]: snake_case_ = '''<pad>''' snake_case_ = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(_UpperCamelCase ) , _UpperCamelCase ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(_UpperCamelCase ) , _UpperCamelCase ) def snake_case__( self : Dict ) ->Union[str, Any]: snake_case_ = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '''<s>''' ) self.assertEqual(vocab_keys[1] , '''<pad>''' ) self.assertEqual(vocab_keys[-4] , '''œ''' ) self.assertEqual(vocab_keys[-2] , '''<mask>''' ) self.assertEqual(vocab_keys[-1] , '''<ctc_blank>''' ) self.assertEqual(len(_UpperCamelCase ) , 8_1 ) def snake_case__( self : Tuple ) ->Union[str, Any]: self.assertEqual(self.get_tokenizer().vocab_size , 7_9 ) def snake_case__( self : int ) ->Optional[int]: snake_case_ = self.get_tokenizers(do_lower_case=_UpperCamelCase ) for tokenizer in tokenizers: with self.subTest(f'''{tokenizer.__class__.__name__}''' ): snake_case_ = tokenizer.vocab_size snake_case_ = len(_UpperCamelCase ) self.assertNotEqual(_UpperCamelCase , 0 ) # We usually have added tokens from the start in tests because our vocab fixtures are # smaller than the original vocabs - let's not assert this # self.assertEqual(vocab_size, all_size) snake_case_ = ['''aaaaa bbbbbb''', '''cccccccccdddddddd'''] snake_case_ = tokenizer.add_tokens(_UpperCamelCase ) snake_case_ = tokenizer.vocab_size snake_case_ = len(_UpperCamelCase ) self.assertNotEqual(_UpperCamelCase , 0 ) self.assertEqual(_UpperCamelCase , _UpperCamelCase ) self.assertEqual(_UpperCamelCase , len(_UpperCamelCase ) ) self.assertEqual(_UpperCamelCase , all_size + len(_UpperCamelCase ) ) snake_case_ = tokenizer.encode('''aaaaa bbbbbb low cccccccccdddddddd l''' , add_special_tokens=_UpperCamelCase ) self.assertGreaterEqual(len(_UpperCamelCase ) , 4 ) self.assertGreater(tokens[0] , tokenizer.vocab_size - 1 ) self.assertGreater(tokens[-3] , tokenizer.vocab_size - 1 ) snake_case_ = {'''eos_token''': '''>>>>|||<||<<|<<''', '''pad_token''': '''<<<<<|||>|>>>>|>'''} snake_case_ = tokenizer.add_special_tokens(_UpperCamelCase ) snake_case_ = tokenizer.vocab_size snake_case_ = len(_UpperCamelCase ) self.assertNotEqual(_UpperCamelCase , 0 ) self.assertEqual(_UpperCamelCase , _UpperCamelCase ) self.assertEqual(_UpperCamelCase , len(_UpperCamelCase ) ) self.assertEqual(_UpperCamelCase , all_size_a + len(_UpperCamelCase ) ) snake_case_ = tokenizer.encode( '''>>>>|||<||<<|<< aaaaabbbbbb low cccccccccdddddddd <<<<<|||>|>>>>|> l''' , add_special_tokens=_UpperCamelCase ) self.assertGreaterEqual(len(_UpperCamelCase ) , 6 ) self.assertGreater(tokens[0] , tokenizer.vocab_size - 1 ) self.assertGreater(tokens[0] , tokens[1] ) self.assertGreater(tokens[-3] , tokenizer.vocab_size - 1 ) self.assertGreater(tokens[-3] , tokens[-4] ) self.assertEqual(tokens[0] , tokenizer.eos_token_id ) self.assertEqual(tokens[-3] , tokenizer.pad_token_id ) def snake_case__( self : Dict ) ->Optional[int]: pass def snake_case__( self : int ) ->List[Any]: pass def snake_case__( self : str ) ->List[Any]: snake_case_ = self.get_tokenizer() snake_case_ = tokenizer.tokenize('''This is a test''' ) # fmt: off self.assertListEqual(_UpperCamelCase , [SPIECE_UNDERLINE, '''T''', '''h''', '''i''', '''s''', SPIECE_UNDERLINE, '''i''', '''s''', SPIECE_UNDERLINE, '''a''', SPIECE_UNDERLINE, '''t''', '''e''', '''s''', '''t'''] ) # fmt: on self.assertListEqual( tokenizer.convert_tokens_to_ids(_UpperCamelCase ) , [4, 3_2, 1_1, 1_0, 1_2, 4, 1_0, 1_2, 4, 7, 4, 6, 5, 1_2, 6] , ) snake_case_ = tokenizer.tokenize('''I was born in 92000, and this is falsé.''' ) self.assertListEqual( _UpperCamelCase , [SPIECE_UNDERLINE, '''I''', SPIECE_UNDERLINE, '''w''', '''a''', '''s''', SPIECE_UNDERLINE, '''b''', '''o''', '''r''', '''n''', SPIECE_UNDERLINE, '''i''', '''n''', SPIECE_UNDERLINE, '''92000''', ''',''', SPIECE_UNDERLINE, '''a''', '''n''', '''d''', SPIECE_UNDERLINE, '''t''', '''h''', '''i''', '''s''', SPIECE_UNDERLINE, '''i''', '''s''', SPIECE_UNDERLINE, '''f''', '''a''', '''l''', '''s''', '''é''', '''.'''] ) snake_case_ = tokenizer.convert_tokens_to_ids(_UpperCamelCase ) # fmt: off self.assertListEqual(_UpperCamelCase , [4, 3_0, 4, 2_0, 7, 1_2, 4, 2_5, 8, 1_3, 9, 4, 1_0, 9, 4, 3, 2_3, 4, 7, 9, 1_4, 4, 6, 1_1, 1_0, 1_2, 4, 1_0, 1_2, 4, 1_9, 7, 1_5, 1_2, 7_3, 2_6] ) # fmt: on snake_case_ = tokenizer.convert_ids_to_tokens(_UpperCamelCase ) self.assertListEqual( _UpperCamelCase , [SPIECE_UNDERLINE, '''I''', SPIECE_UNDERLINE, '''w''', '''a''', '''s''', SPIECE_UNDERLINE, '''b''', '''o''', '''r''', '''n''', SPIECE_UNDERLINE, '''i''', '''n''', SPIECE_UNDERLINE, '''<unk>''', ''',''', SPIECE_UNDERLINE, '''a''', '''n''', '''d''', SPIECE_UNDERLINE, '''t''', '''h''', '''i''', '''s''', SPIECE_UNDERLINE, '''i''', '''s''', SPIECE_UNDERLINE, '''f''', '''a''', '''l''', '''s''', '''é''', '''.'''] ) @slow def snake_case__( self : Tuple ) ->Dict: # Use custom sequence because this tokenizer does not handle numbers. snake_case_ = [ '''Transformers (formerly known as pytorch-transformers and pytorch-pretrained-bert) provides ''' '''general-purpose architectures (BERT, GPT, RoBERTa, XLM, DistilBert, XLNet...) for Natural ''' '''Language Understanding (NLU) and Natural Language Generation (NLG) with over thirty-two pretrained ''' '''models in one hundred plus languages and deep interoperability between Jax, PyTorch and TensorFlow.''', '''BERT is designed to pre-train deep bidirectional representations from unlabeled text by jointly ''' '''conditioning on both left and right context in all layers.''', '''The quick brown fox jumps over the lazy dog.''', ] # fmt: off snake_case_ = { '''input_ids''': [ [4, 3_2, 1_3, 7, 9, 1_2, 1_9, 8, 1_3, 1_8, 5, 1_3, 1_2, 4, 6_4, 1_9, 8, 1_3, 1_8, 5, 1_3, 1_5, 2_2, 4, 2_8, 9, 8, 2_0, 9, 4, 7, 1_2, 4, 2_4, 2_2, 6, 8, 1_3, 1_7, 1_1, 3_9, 6, 1_3, 7, 9, 1_2, 1_9, 8, 1_3, 1_8, 5, 1_3, 1_2, 4, 7, 9, 1_4, 4, 2_4, 2_2, 6, 8, 1_3, 1_7, 1_1, 3_9, 2_4, 1_3, 5, 6, 1_3, 7, 1_0, 9, 5, 1_4, 3_9, 2_5, 5, 1_3, 6, 6_3, 4, 2_4, 1_3, 8, 2_7, 1_0, 1_4, 5, 1_2, 4, 2_1, 5, 9, 5, 1_3, 7, 1_5, 3_9, 2_4, 1_6, 1_3, 2_4, 8, 1_2, 5, 4, 7, 1_3, 1_7, 1_1, 1_0, 6, 5, 1_7, 6, 1_6, 1_3, 5, 1_2, 4, 6_4, 4_0, 4_7, 5_4, 3_2, 2_3, 4, 5_3, 4_9, 3_2, 2_3, 4, 5_4, 8, 4_0, 4_7, 5_4, 3_2, 7, 2_3, 4, 6_9, 5_2, 4_3, 2_3, 4, 5_1, 1_0, 1_2, 6, 1_0, 1_5, 4_0, 5, 1_3, 6, 2_3, 4, 6_9, 5_2, 4_8, 5, 6, 2_6, 2_6, 2_6, 6_3, 4, 1_9, 8, 1_3, 4, 4_8, 7, 6, 1_6, 1_3, 7, 1_5, 4, 5_2, 7, 9, 2_1, 1_6, 7, 2_1, 5, 4, 6_1, 9, 1_4, 5, 1_3, 1_2, 6, 7, 9, 1_4, 1_0, 9, 2_1, 4, 6_4, 4_8, 5_2, 6_1, 6_3, 4, 7, 9, 1_4, 4, 4_8, 7, 6, 1_6, 1_3, 7, 1_5, 4, 5_2, 7, 9, 2_1, 1_6, 7, 2_1, 5, 4, 5_3, 5, 9, 5, 1_3, 7, 6, 1_0, 8, 9, 4, 6_4, 4_8, 5_2, 5_3, 6_3, 4, 2_0, 1_0, 6, 1_1, 4, 8, 2_7, 5, 1_3, 4, 6, 1_1, 1_0, 1_3, 6, 2_2, 3_9, 6, 2_0, 8, 4, 2_4, 1_3, 5, 6, 1_3, 7, 1_0, 9, 5, 1_4, 4, 1_8, 8, 1_4, 5, 1_5, 1_2, 4, 1_0, 9, 4, 8, 9, 5, 4, 1_1, 1_6, 9, 1_4, 1_3, 5, 1_4, 4, 2_4, 1_5, 1_6, 1_2, 4, 1_5, 7, 9, 2_1, 1_6, 7, 2_1, 5, 1_2, 4, 7, 9, 1_4, 4, 1_4, 5, 5, 2_4, 4, 1_0, 9, 6, 5, 1_3, 8, 2_4, 5, 1_3, 7, 2_5, 1_0, 1_5, 1_0, 6, 2_2, 4, 2_5, 5, 6, 2_0, 5, 5, 9, 4, 5_8, 7, 3_7, 2_3, 4, 4_9, 2_2, 3_2, 8, 1_3, 1_7, 1_1, 4, 7, 9, 1_4, 4, 3_2, 5, 9, 1_2, 8, 1_3, 5_5, 1_5, 8, 2_0, 2_6, 2], [4, 4_0, 4_7, 5_4, 3_2, 4, 1_0, 1_2, 4, 1_4, 5, 1_2, 1_0, 2_1, 9, 5, 1_4, 4, 6, 8, 4, 2_4, 1_3, 5, 3_9, 6, 1_3, 7, 1_0, 9, 4, 1_4, 5, 5, 2_4, 4, 2_5, 1_0, 1_4, 1_0, 1_3, 5, 1_7, 6, 1_0, 8, 9, 7, 1_5, 4, 1_3, 5, 2_4, 1_3, 5, 1_2, 5, 9, 6, 7, 6, 1_0, 8, 9, 1_2, 4, 1_9, 1_3, 8, 1_8, 4, 1_6, 9, 1_5, 7, 2_5, 5, 1_5, 5, 1_4, 4, 6, 5, 3_7, 6, 4, 2_5, 2_2, 4, 4_6, 8, 1_0, 9, 6, 1_5, 2_2, 4, 1_7, 8, 9, 1_4, 1_0, 6, 1_0, 8, 9, 1_0, 9, 2_1, 4, 8, 9, 4, 2_5, 8, 6, 1_1, 4, 1_5, 5, 1_9, 6, 4, 7, 9, 1_4, 4, 1_3, 1_0, 2_1, 1_1, 6, 4, 1_7, 8, 9, 6, 5, 3_7, 6, 4, 1_0, 9, 4, 7, 1_5, 1_5, 4, 1_5, 7, 2_2, 5, 1_3, 1_2, 2_6, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [4, 3_2, 1_1, 5, 4, 4_5, 1_6, 1_0, 1_7, 2_8, 4, 2_5, 1_3, 8, 2_0, 9, 4, 1_9, 8, 3_7, 4, 4_6, 1_6, 1_8, 2_4, 1_2, 4, 8, 2_7, 5, 1_3, 4, 6, 1_1, 5, 4, 1_5, 7, 5_7, 2_2, 4, 1_4, 8, 2_1, 2_6, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], ], '''attention_mask''': [ [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], ] } # fmt: on self.tokenizer_integration_test_util( expected_encoding=_UpperCamelCase , model_name='''microsoft/speecht5_asr''' , revision='''c5ef64c71905caeccde0e4462ef3f9077224c524''' , sequences=_UpperCamelCase , )
39
import unittest from transformers import DonutProcessor lowerCAmelCase_ = '''naver-clova-ix/donut-base''' class snake_case_ ( unittest.TestCase ): '''simple docstring''' def snake_case__( self : Union[str, Any] ) ->Any: snake_case_ = DonutProcessor.from_pretrained(_UpperCamelCase ) def snake_case__( self : Dict ) ->str: snake_case_ = { '''name''': '''John Doe''', '''age''': '''99''', '''city''': '''Atlanta''', '''state''': '''GA''', '''zip''': '''30301''', '''phone''': '''123-4567''', '''nicknames''': [{'''nickname''': '''Johnny'''}, {'''nickname''': '''JD'''}], } snake_case_ = ( '''<s_name>John Doe</s_name><s_age>99</s_age><s_city>Atlanta</s_city>''' '''<s_state>GA</s_state><s_zip>30301</s_zip><s_phone>123-4567</s_phone>''' '''<s_nicknames><s_nickname>Johnny</s_nickname>''' '''<sep/><s_nickname>JD</s_nickname></s_nicknames>''' ) snake_case_ = self.processor.tokenajson(_UpperCamelCase ) self.assertDictEqual(_UpperCamelCase , _UpperCamelCase )
39
1
import argparse import os import shutil from pathlib import Path import onnx import torch from packaging import version from torch.onnx import export from diffusers import OnnxRuntimeModel, OnnxStableDiffusionPipeline, StableDiffusionPipeline lowerCAmelCase_ = version.parse(version.parse(torch.__version__).base_version) < version.parse('''1.11''') def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__=False , ): output_path.parent.mkdir(parents=SCREAMING_SNAKE_CASE__ , exist_ok=SCREAMING_SNAKE_CASE__ ) # PyTorch deprecated the `enable_onnx_checker` and `use_external_data_format` arguments in v1.11, # so we check the torch version for backwards compatibility if is_torch_less_than_1_11: export( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , f=output_path.as_posix() , input_names=SCREAMING_SNAKE_CASE__ , output_names=SCREAMING_SNAKE_CASE__ , dynamic_axes=SCREAMING_SNAKE_CASE__ , do_constant_folding=SCREAMING_SNAKE_CASE__ , use_external_data_format=SCREAMING_SNAKE_CASE__ , enable_onnx_checker=SCREAMING_SNAKE_CASE__ , opset_version=SCREAMING_SNAKE_CASE__ , ) else: export( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , f=output_path.as_posix() , input_names=SCREAMING_SNAKE_CASE__ , output_names=SCREAMING_SNAKE_CASE__ , dynamic_axes=SCREAMING_SNAKE_CASE__ , do_constant_folding=SCREAMING_SNAKE_CASE__ , opset_version=SCREAMING_SNAKE_CASE__ , ) @torch.no_grad() def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ = False ): snake_case_ = torch.floataa if fpaa else torch.floataa if fpaa and torch.cuda.is_available(): snake_case_ = '''cuda''' elif fpaa and not torch.cuda.is_available(): raise ValueError('''`float16` model export is only supported on GPUs with CUDA''' ) else: snake_case_ = '''cpu''' snake_case_ = StableDiffusionPipeline.from_pretrained(SCREAMING_SNAKE_CASE__ , torch_dtype=SCREAMING_SNAKE_CASE__ ).to(SCREAMING_SNAKE_CASE__ ) snake_case_ = Path(SCREAMING_SNAKE_CASE__ ) # TEXT ENCODER snake_case_ = pipeline.text_encoder.config.max_position_embeddings snake_case_ = pipeline.text_encoder.config.hidden_size snake_case_ = pipeline.tokenizer( '''A sample prompt''' , padding='''max_length''' , max_length=pipeline.tokenizer.model_max_length , truncation=SCREAMING_SNAKE_CASE__ , return_tensors='''pt''' , ) onnx_export( pipeline.text_encoder , model_args=(text_input.input_ids.to(device=SCREAMING_SNAKE_CASE__ , dtype=torch.intaa )) , output_path=output_path / '''text_encoder''' / '''model.onnx''' , ordered_input_names=['''input_ids'''] , output_names=['''last_hidden_state''', '''pooler_output'''] , dynamic_axes={ '''input_ids''': {0: '''batch''', 1: '''sequence'''}, } , opset=SCREAMING_SNAKE_CASE__ , ) del pipeline.text_encoder # UNET snake_case_ = pipeline.unet.config.in_channels snake_case_ = pipeline.unet.config.sample_size snake_case_ = output_path / '''unet''' / '''model.onnx''' onnx_export( pipeline.unet , model_args=( torch.randn(2 , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ).to(device=SCREAMING_SNAKE_CASE__ , dtype=SCREAMING_SNAKE_CASE__ ), torch.randn(2 ).to(device=SCREAMING_SNAKE_CASE__ , dtype=SCREAMING_SNAKE_CASE__ ), torch.randn(2 , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ).to(device=SCREAMING_SNAKE_CASE__ , dtype=SCREAMING_SNAKE_CASE__ ), False, ) , output_path=SCREAMING_SNAKE_CASE__ , ordered_input_names=['''sample''', '''timestep''', '''encoder_hidden_states''', '''return_dict'''] , output_names=['''out_sample'''] , dynamic_axes={ '''sample''': {0: '''batch''', 1: '''channels''', 2: '''height''', 3: '''width'''}, '''timestep''': {0: '''batch'''}, '''encoder_hidden_states''': {0: '''batch''', 1: '''sequence'''}, } , opset=SCREAMING_SNAKE_CASE__ , use_external_data_format=SCREAMING_SNAKE_CASE__ , ) snake_case_ = str(unet_path.absolute().as_posix() ) snake_case_ = os.path.dirname(SCREAMING_SNAKE_CASE__ ) snake_case_ = onnx.load(SCREAMING_SNAKE_CASE__ ) # clean up existing tensor files shutil.rmtree(SCREAMING_SNAKE_CASE__ ) os.mkdir(SCREAMING_SNAKE_CASE__ ) # collate external tensor files into one onnx.save_model( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , save_as_external_data=SCREAMING_SNAKE_CASE__ , all_tensors_to_one_file=SCREAMING_SNAKE_CASE__ , location='''weights.pb''' , convert_attribute=SCREAMING_SNAKE_CASE__ , ) del pipeline.unet # VAE ENCODER snake_case_ = pipeline.vae snake_case_ = vae_encoder.config.in_channels snake_case_ = vae_encoder.config.sample_size # need to get the raw tensor output (sample) from the encoder snake_case_ = lambda SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ : vae_encoder.encode(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ )[0].sample() onnx_export( SCREAMING_SNAKE_CASE__ , model_args=( torch.randn(1 , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ).to(device=SCREAMING_SNAKE_CASE__ , dtype=SCREAMING_SNAKE_CASE__ ), False, ) , output_path=output_path / '''vae_encoder''' / '''model.onnx''' , ordered_input_names=['''sample''', '''return_dict'''] , output_names=['''latent_sample'''] , dynamic_axes={ '''sample''': {0: '''batch''', 1: '''channels''', 2: '''height''', 3: '''width'''}, } , opset=SCREAMING_SNAKE_CASE__ , ) # VAE DECODER snake_case_ = pipeline.vae snake_case_ = vae_decoder.config.latent_channels snake_case_ = vae_decoder.config.out_channels # forward only through the decoder part snake_case_ = vae_encoder.decode onnx_export( SCREAMING_SNAKE_CASE__ , model_args=( torch.randn(1 , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ).to(device=SCREAMING_SNAKE_CASE__ , dtype=SCREAMING_SNAKE_CASE__ ), False, ) , output_path=output_path / '''vae_decoder''' / '''model.onnx''' , ordered_input_names=['''latent_sample''', '''return_dict'''] , output_names=['''sample'''] , dynamic_axes={ '''latent_sample''': {0: '''batch''', 1: '''channels''', 2: '''height''', 3: '''width'''}, } , opset=SCREAMING_SNAKE_CASE__ , ) del pipeline.vae # SAFETY CHECKER if pipeline.safety_checker is not None: snake_case_ = pipeline.safety_checker snake_case_ = safety_checker.config.vision_config.num_channels snake_case_ = safety_checker.config.vision_config.image_size snake_case_ = safety_checker.forward_onnx onnx_export( pipeline.safety_checker , model_args=( torch.randn( 1 , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , ).to(device=SCREAMING_SNAKE_CASE__ , dtype=SCREAMING_SNAKE_CASE__ ), torch.randn(1 , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ).to(device=SCREAMING_SNAKE_CASE__ , dtype=SCREAMING_SNAKE_CASE__ ), ) , output_path=output_path / '''safety_checker''' / '''model.onnx''' , ordered_input_names=['''clip_input''', '''images'''] , output_names=['''out_images''', '''has_nsfw_concepts'''] , dynamic_axes={ '''clip_input''': {0: '''batch''', 1: '''channels''', 2: '''height''', 3: '''width'''}, '''images''': {0: '''batch''', 1: '''height''', 2: '''width''', 3: '''channels'''}, } , opset=SCREAMING_SNAKE_CASE__ , ) del pipeline.safety_checker snake_case_ = OnnxRuntimeModel.from_pretrained(output_path / '''safety_checker''' ) snake_case_ = pipeline.feature_extractor else: snake_case_ = None snake_case_ = None snake_case_ = OnnxStableDiffusionPipeline( vae_encoder=OnnxRuntimeModel.from_pretrained(output_path / '''vae_encoder''' ) , vae_decoder=OnnxRuntimeModel.from_pretrained(output_path / '''vae_decoder''' ) , text_encoder=OnnxRuntimeModel.from_pretrained(output_path / '''text_encoder''' ) , tokenizer=pipeline.tokenizer , unet=OnnxRuntimeModel.from_pretrained(output_path / '''unet''' ) , scheduler=pipeline.scheduler , safety_checker=SCREAMING_SNAKE_CASE__ , feature_extractor=SCREAMING_SNAKE_CASE__ , requires_safety_checker=safety_checker is not None , ) onnx_pipeline.save_pretrained(SCREAMING_SNAKE_CASE__ ) print('''ONNX pipeline saved to''' , SCREAMING_SNAKE_CASE__ ) del pipeline del onnx_pipeline snake_case_ = OnnxStableDiffusionPipeline.from_pretrained(SCREAMING_SNAKE_CASE__ , provider='''CPUExecutionProvider''' ) print('''ONNX pipeline is loadable''' ) if __name__ == "__main__": lowerCAmelCase_ = argparse.ArgumentParser() parser.add_argument( '''--model_path''', type=str, required=True, help='''Path to the `diffusers` checkpoint to convert (either a local directory or on the Hub).''', ) parser.add_argument('''--output_path''', type=str, required=True, help='''Path to the output model.''') parser.add_argument( '''--opset''', default=14, type=int, help='''The version of the ONNX operator set to use.''', ) parser.add_argument('''--fp16''', action='''store_true''', default=False, help='''Export the models in `float16` mode''') lowerCAmelCase_ = parser.parse_args() convert_models(args.model_path, args.output_path, args.opset, args.fpaa)
39
from __future__ import annotations def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): if not nums: raise ValueError('''List is empty''' ) return sum(SCREAMING_SNAKE_CASE__ ) / len(SCREAMING_SNAKE_CASE__ ) if __name__ == "__main__": import doctest doctest.testmod()
39
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCAmelCase_ = { '''configuration_timesformer''': ['''TIMESFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''TimesformerConfig'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ = [ '''TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TimesformerModel''', '''TimesformerForVideoClassification''', '''TimesformerPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_timesformer import TIMESFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TimesformerConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_timesformer import ( TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TimesformerForVideoClassification, TimesformerModel, TimesformerPreTrainedModel, ) else: import sys lowerCAmelCase_ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
39
import inspect import os import unittest import torch import accelerate from accelerate import debug_launcher from accelerate.test_utils import ( execute_subprocess_async, require_cpu, require_huggingface_suite, require_multi_gpu, require_single_gpu, ) from accelerate.utils import patch_environment @require_huggingface_suite class snake_case_ ( unittest.TestCase ): '''simple docstring''' def snake_case__( self : List[str] ) ->str: snake_case_ = inspect.getfile(accelerate.test_utils ) snake_case_ = os.path.sep.join( mod_file.split(os.path.sep )[:-1] + ['''scripts''', '''external_deps''', '''test_metrics.py'''] ) from accelerate.test_utils.scripts.external_deps import test_metrics # noqa: F401 snake_case_ = test_metrics @require_cpu def snake_case__( self : str ) ->int: debug_launcher(self.test_metrics.main , num_processes=1 ) @require_cpu def snake_case__( self : Union[str, Any] ) ->Any: debug_launcher(self.test_metrics.main ) @require_single_gpu def snake_case__( self : List[Any] ) ->Tuple: self.test_metrics.main() @require_multi_gpu def snake_case__( self : Any ) ->Union[str, Any]: print(f'''Found {torch.cuda.device_count()} devices.''' ) snake_case_ = ['''torchrun''', f'''--nproc_per_node={torch.cuda.device_count()}''', self.test_file_path] with patch_environment(omp_num_threads=1 ): execute_subprocess_async(_UpperCamelCase , env=os.environ.copy() )
39
1
import importlib import os from dataclasses import dataclass from enum import Enum from typing import Any, Dict, Optional, Union import torch from ..utils import BaseOutput lowerCAmelCase_ = '''scheduler_config.json''' class snake_case_ ( __A ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = 1 SCREAMING_SNAKE_CASE : Union[str, Any] = 2 SCREAMING_SNAKE_CASE : Dict = 3 SCREAMING_SNAKE_CASE : Union[str, Any] = 4 SCREAMING_SNAKE_CASE : int = 5 SCREAMING_SNAKE_CASE : Optional[Any] = 6 SCREAMING_SNAKE_CASE : Dict = 7 SCREAMING_SNAKE_CASE : Optional[int] = 8 SCREAMING_SNAKE_CASE : Union[str, Any] = 9 SCREAMING_SNAKE_CASE : Any = 10 SCREAMING_SNAKE_CASE : Optional[int] = 11 SCREAMING_SNAKE_CASE : int = 12 SCREAMING_SNAKE_CASE : Any = 13 SCREAMING_SNAKE_CASE : Any = 14 @dataclass class snake_case_ ( __A ): '''simple docstring''' SCREAMING_SNAKE_CASE : torch.FloatTensor class snake_case_ : '''simple docstring''' SCREAMING_SNAKE_CASE : int = SCHEDULER_CONFIG_NAME SCREAMING_SNAKE_CASE : str = [] SCREAMING_SNAKE_CASE : Optional[int] = True @classmethod def snake_case__( cls : int , _UpperCamelCase : Dict[str, Any] = None , _UpperCamelCase : Optional[str] = None , _UpperCamelCase : Dict=False , **_UpperCamelCase : Tuple , ) ->Union[str, Any]: snake_case_, snake_case_, snake_case_ = cls.load_config( pretrained_model_name_or_path=_UpperCamelCase , subfolder=_UpperCamelCase , return_unused_kwargs=_UpperCamelCase , return_commit_hash=_UpperCamelCase , **_UpperCamelCase , ) return cls.from_config(_UpperCamelCase , return_unused_kwargs=_UpperCamelCase , **_UpperCamelCase ) def snake_case__( self : Optional[int] , _UpperCamelCase : Union[str, os.PathLike] , _UpperCamelCase : bool = False , **_UpperCamelCase : Union[str, Any] ) ->Any: self.save_config(save_directory=_UpperCamelCase , push_to_hub=_UpperCamelCase , **_UpperCamelCase ) @property def snake_case__( self : Dict ) ->List[str]: return self._get_compatibles() @classmethod def snake_case__( cls : List[Any] ) ->List[Any]: snake_case_ = list(set([cls.__name__] + cls._compatibles ) ) snake_case_ = importlib.import_module(__name__.split('''.''' )[0] ) snake_case_ = [ getattr(_UpperCamelCase , _UpperCamelCase ) for c in compatible_classes_str if hasattr(_UpperCamelCase , _UpperCamelCase ) ] return compatible_classes
39
from typing import List, Optional, Union from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase_ = logging.get_logger(__name__) lowerCAmelCase_ = { '''huggingface/informer-tourism-monthly''': ( '''https://huggingface.co/huggingface/informer-tourism-monthly/resolve/main/config.json''' ), # See all Informer models at https://huggingface.co/models?filter=informer } class snake_case_ ( __A ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = "informer" SCREAMING_SNAKE_CASE : int = { "hidden_size": "d_model", "num_attention_heads": "encoder_attention_heads", "num_hidden_layers": "encoder_layers", } def __init__( self : Dict , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : Optional[int] = None , _UpperCamelCase : str = "student_t" , _UpperCamelCase : str = "nll" , _UpperCamelCase : int = 1 , _UpperCamelCase : List[int] = None , _UpperCamelCase : Optional[Union[str, bool]] = "mean" , _UpperCamelCase : int = 0 , _UpperCamelCase : int = 0 , _UpperCamelCase : int = 0 , _UpperCamelCase : int = 0 , _UpperCamelCase : Optional[List[int]] = None , _UpperCamelCase : Optional[List[int]] = None , _UpperCamelCase : int = 6_4 , _UpperCamelCase : int = 3_2 , _UpperCamelCase : int = 3_2 , _UpperCamelCase : int = 2 , _UpperCamelCase : int = 2 , _UpperCamelCase : int = 2 , _UpperCamelCase : int = 2 , _UpperCamelCase : bool = True , _UpperCamelCase : str = "gelu" , _UpperCamelCase : float = 0.05 , _UpperCamelCase : float = 0.1 , _UpperCamelCase : float = 0.1 , _UpperCamelCase : float = 0.1 , _UpperCamelCase : float = 0.1 , _UpperCamelCase : int = 1_0_0 , _UpperCamelCase : float = 0.02 , _UpperCamelCase : Dict=True , _UpperCamelCase : str = "prob" , _UpperCamelCase : int = 5 , _UpperCamelCase : bool = True , **_UpperCamelCase : Optional[Any] , ) ->Optional[int]: # time series specific configuration snake_case_ = prediction_length snake_case_ = context_length or prediction_length snake_case_ = distribution_output snake_case_ = loss snake_case_ = input_size snake_case_ = num_time_features snake_case_ = lags_sequence if lags_sequence is not None else [1, 2, 3, 4, 5, 6, 7] snake_case_ = scaling snake_case_ = num_dynamic_real_features snake_case_ = num_static_real_features snake_case_ = num_static_categorical_features # set cardinality if cardinality and num_static_categorical_features > 0: if len(_UpperCamelCase ) != num_static_categorical_features: raise ValueError( '''The cardinality should be a list of the same length as `num_static_categorical_features`''' ) snake_case_ = cardinality else: snake_case_ = [0] # set embedding_dimension if embedding_dimension and num_static_categorical_features > 0: if len(_UpperCamelCase ) != num_static_categorical_features: raise ValueError( '''The embedding dimension should be a list of the same length as `num_static_categorical_features`''' ) snake_case_ = embedding_dimension else: snake_case_ = [min(5_0 , (cat + 1) // 2 ) for cat in self.cardinality] snake_case_ = num_parallel_samples # Transformer architecture configuration snake_case_ = input_size * len(self.lags_sequence ) + self._number_of_features snake_case_ = d_model snake_case_ = encoder_attention_heads snake_case_ = decoder_attention_heads snake_case_ = encoder_ffn_dim snake_case_ = decoder_ffn_dim snake_case_ = encoder_layers snake_case_ = decoder_layers snake_case_ = dropout snake_case_ = attention_dropout snake_case_ = activation_dropout snake_case_ = encoder_layerdrop snake_case_ = decoder_layerdrop snake_case_ = activation_function snake_case_ = init_std snake_case_ = use_cache # Informer snake_case_ = attention_type snake_case_ = sampling_factor snake_case_ = distil super().__init__(is_encoder_decoder=_UpperCamelCase , **_UpperCamelCase ) @property def snake_case__( self : Optional[Any] ) ->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 )
39
1
import inspect import os import re from transformers.configuration_utils import PretrainedConfig from transformers.utils import direct_transformers_import # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/check_config_docstrings.py lowerCAmelCase_ = '''src/transformers''' # This is to make sure the transformers module imported is the one in the repo. lowerCAmelCase_ = direct_transformers_import(PATH_TO_TRANSFORMERS) lowerCAmelCase_ = transformers.models.auto.configuration_auto.CONFIG_MAPPING lowerCAmelCase_ = { # used to compute the property `self.chunk_length` '''EncodecConfig''': ['''overlap'''], # used as `self.bert_model = BertModel(config, ...)` '''DPRConfig''': True, # not used in modeling files, but it's an important information '''FSMTConfig''': ['''langs'''], # used internally in the configuration class file '''GPTNeoConfig''': ['''attention_types'''], # used internally in the configuration class file '''EsmConfig''': ['''is_folding_model'''], # used during training (despite we don't have training script for these models yet) '''Mask2FormerConfig''': ['''ignore_value'''], # `ignore_value` used during training (despite we don't have training script for these models yet) # `norm` used in conversion script (despite not using in the modeling file) '''OneFormerConfig''': ['''ignore_value''', '''norm'''], # used during preprocessing and collation, see `collating_graphormer.py` '''GraphormerConfig''': ['''spatial_pos_max'''], # used internally in the configuration class file '''T5Config''': ['''feed_forward_proj'''], # used internally in the configuration class file # `tokenizer_class` get default value `T5Tokenizer` intentionally '''MT5Config''': ['''feed_forward_proj''', '''tokenizer_class'''], '''UMT5Config''': ['''feed_forward_proj''', '''tokenizer_class'''], # used internally in the configuration class file '''LongT5Config''': ['''feed_forward_proj'''], # used internally in the configuration class file '''SwitchTransformersConfig''': ['''feed_forward_proj'''], # having default values other than `1e-5` - we can't fix them without breaking '''BioGptConfig''': ['''layer_norm_eps'''], # having default values other than `1e-5` - we can't fix them without breaking '''GLPNConfig''': ['''layer_norm_eps'''], # having default values other than `1e-5` - we can't fix them without breaking '''SegformerConfig''': ['''layer_norm_eps'''], # having default values other than `1e-5` - we can't fix them without breaking '''CvtConfig''': ['''layer_norm_eps'''], # having default values other than `1e-5` - we can't fix them without breaking '''PerceiverConfig''': ['''layer_norm_eps'''], # used internally to calculate the feature size '''InformerConfig''': ['''num_static_real_features''', '''num_time_features'''], # used internally to calculate the feature size '''TimeSeriesTransformerConfig''': ['''num_static_real_features''', '''num_time_features'''], # used internally to calculate the feature size '''AutoformerConfig''': ['''num_static_real_features''', '''num_time_features'''], # used internally to calculate `mlp_dim` '''SamVisionConfig''': ['''mlp_ratio'''], # For (head) training, but so far not implemented '''ClapAudioConfig''': ['''num_classes'''], # Not used, but providing useful information to users '''SpeechT5HifiGanConfig''': ['''sampling_rate'''], } # TODO (ydshieh): Check the failing cases, try to fix them or move some cases to the above block once we are sure SPECIAL_CASES_TO_ALLOW.update( { '''CLIPSegConfig''': True, '''DeformableDetrConfig''': True, '''DetaConfig''': True, '''DinatConfig''': True, '''DonutSwinConfig''': True, '''EfficientFormerConfig''': True, '''FSMTConfig''': True, '''JukeboxConfig''': True, '''LayoutLMv2Config''': True, '''MaskFormerSwinConfig''': True, '''MT5Config''': True, '''NatConfig''': True, '''OneFormerConfig''': True, '''PerceiverConfig''': True, '''RagConfig''': True, '''SpeechT5Config''': True, '''SwinConfig''': True, '''Swin2SRConfig''': True, '''Swinv2Config''': True, '''SwitchTransformersConfig''': True, '''TableTransformerConfig''': True, '''TapasConfig''': True, '''TransfoXLConfig''': True, '''UniSpeechConfig''': True, '''UniSpeechSatConfig''': True, '''WavLMConfig''': True, '''WhisperConfig''': True, # TODO: @Arthur (for `alignment_head` and `alignment_layer`) '''JukeboxPriorConfig''': True, # TODO: @Younes (for `is_decoder`) '''Pix2StructTextConfig''': True, } ) def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): snake_case_ = False for attribute in attributes: for modeling_source in source_strings: # check if we can find `config.xxx`, `getattr(config, "xxx", ...)` or `getattr(self.config, "xxx", ...)` if ( F'''config.{attribute}''' in modeling_source or F'''getattr(config, "{attribute}"''' in modeling_source or F'''getattr(self.config, "{attribute}"''' in modeling_source ): snake_case_ = True # Deal with multi-line cases elif ( re.search( RF'''getattr[ \t\v\n\r\f]*\([ \t\v\n\r\f]*(self\.)?config,[ \t\v\n\r\f]*"{attribute}"''' , SCREAMING_SNAKE_CASE__ , ) is not None ): snake_case_ = True # `SequenceSummary` is called with `SequenceSummary(config)` elif attribute in [ "summary_type", "summary_use_proj", "summary_activation", "summary_last_dropout", "summary_proj_to_labels", "summary_first_dropout", ]: if "SequenceSummary" in modeling_source: snake_case_ = True if attribute_used: break if attribute_used: break # common and important attributes, even if they do not always appear in the modeling files snake_case_ = [ '''bos_index''', '''eos_index''', '''pad_index''', '''unk_index''', '''mask_index''', '''image_size''', '''use_cache''', '''out_features''', '''out_indices''', ] snake_case_ = ['''encoder_no_repeat_ngram_size'''] # Special cases to be allowed snake_case_ = True if not attribute_used: snake_case_ = False for attribute in attributes: # Allow if the default value in the configuration class is different from the one in `PretrainedConfig` if attribute in ["is_encoder_decoder"] and default_value is True: snake_case_ = True elif attribute in ["tie_word_embeddings"] and default_value is False: snake_case_ = True # Allow cases without checking the default value in the configuration class elif attribute in attributes_to_allow + attributes_used_in_generation: snake_case_ = True elif attribute.endswith('''_token_id''' ): snake_case_ = True # configuration class specific cases if not case_allowed: snake_case_ = SPECIAL_CASES_TO_ALLOW.get(config_class.__name__ , [] ) snake_case_ = allowed_cases is True or attribute in allowed_cases return attribute_used or case_allowed def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): snake_case_ = dict(inspect.signature(config_class.__init__ ).parameters ) snake_case_ = [x for x in list(signature.keys() ) if x not in ['''self''', '''kwargs''']] snake_case_ = [signature[param].default for param in parameter_names] # If `attribute_map` exists, an attribute can have different names to be used in the modeling files, and as long # as one variant is used, the test should pass snake_case_ = {} if len(config_class.attribute_map ) > 0: snake_case_ = {v: k for k, v in config_class.attribute_map.items()} # Get the path to modeling source files snake_case_ = inspect.getsourcefile(SCREAMING_SNAKE_CASE__ ) snake_case_ = os.path.dirname(SCREAMING_SNAKE_CASE__ ) # Let's check against all frameworks: as long as one framework uses an attribute, we are good. snake_case_ = [os.path.join(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) for fn in os.listdir(SCREAMING_SNAKE_CASE__ ) if fn.startswith('''modeling_''' )] # Get the source code strings snake_case_ = [] for path in modeling_paths: if os.path.isfile(SCREAMING_SNAKE_CASE__ ): with open(SCREAMING_SNAKE_CASE__ ) as fp: modeling_sources.append(fp.read() ) snake_case_ = [] for config_param, default_value in zip(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): # `attributes` here is all the variant names for `config_param` snake_case_ = [config_param] # some configuration classes have non-empty `attribute_map`, and both names could be used in the # corresponding modeling files. As long as one of them appears, it is fine. if config_param in reversed_attribute_map: attributes.append(reversed_attribute_map[config_param] ) if not check_attribute_being_used(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): unused_attributes.append(attributes[0] ) return sorted(SCREAMING_SNAKE_CASE__ ) def __SCREAMING_SNAKE_CASE (): snake_case_ = {} for _config_class in list(CONFIG_MAPPING.values() ): # Skip deprecated models if "models.deprecated" in _config_class.__module__: continue # Some config classes are not in `CONFIG_MAPPING` (e.g. `CLIPVisionConfig`, `Blip2VisionConfig`, etc.) snake_case_ = [ cls for name, cls in inspect.getmembers( inspect.getmodule(_config_class ) , lambda SCREAMING_SNAKE_CASE__ : inspect.isclass(SCREAMING_SNAKE_CASE__ ) and issubclass(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and inspect.getmodule(SCREAMING_SNAKE_CASE__ ) == inspect.getmodule(_config_class ) , ) ] for config_class in config_classes_in_module: snake_case_ = check_config_attributes_being_used(SCREAMING_SNAKE_CASE__ ) if len(SCREAMING_SNAKE_CASE__ ) > 0: snake_case_ = unused_attributes if len(SCREAMING_SNAKE_CASE__ ) > 0: snake_case_ = '''The following configuration classes contain unused attributes in the corresponding modeling files:\n''' for name, attributes in configs_with_unused_attributes.items(): error += F'''{name}: {attributes}\n''' raise ValueError(SCREAMING_SNAKE_CASE__ ) if __name__ == "__main__": check_config_attributes()
39
import cmath import math def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): snake_case_ = math.radians(SCREAMING_SNAKE_CASE__ ) snake_case_ = math.radians(SCREAMING_SNAKE_CASE__ ) # Convert voltage and current to rectangular form snake_case_ = cmath.rect(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) snake_case_ = cmath.rect(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) # Calculate apparent power return voltage_rect * current_rect if __name__ == "__main__": import doctest doctest.testmod()
39
1
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import BeitConfig, BeitForImageClassification, BeitForMaskedImageModeling, BeitImageProcessor from transformers.image_utils import PILImageResampling from transformers.utils import logging logging.set_verbosity_info() lowerCAmelCase_ = logging.get_logger(__name__) def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__=False , SCREAMING_SNAKE_CASE__=False ): snake_case_ = '''backbone.''' if is_semantic else '''''' snake_case_ = [] for i in range(config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append((F'''{prefix}blocks.{i}.norm1.weight''', F'''beit.encoder.layer.{i}.layernorm_before.weight''') ) rename_keys.append((F'''{prefix}blocks.{i}.norm1.bias''', F'''beit.encoder.layer.{i}.layernorm_before.bias''') ) rename_keys.append( (F'''{prefix}blocks.{i}.attn.proj.weight''', F'''beit.encoder.layer.{i}.attention.output.dense.weight''') ) rename_keys.append( (F'''{prefix}blocks.{i}.attn.proj.bias''', F'''beit.encoder.layer.{i}.attention.output.dense.bias''') ) rename_keys.append((F'''{prefix}blocks.{i}.norm2.weight''', F'''beit.encoder.layer.{i}.layernorm_after.weight''') ) rename_keys.append((F'''{prefix}blocks.{i}.norm2.bias''', F'''beit.encoder.layer.{i}.layernorm_after.bias''') ) rename_keys.append((F'''{prefix}blocks.{i}.mlp.fc1.weight''', F'''beit.encoder.layer.{i}.intermediate.dense.weight''') ) rename_keys.append((F'''{prefix}blocks.{i}.mlp.fc1.bias''', F'''beit.encoder.layer.{i}.intermediate.dense.bias''') ) rename_keys.append((F'''{prefix}blocks.{i}.mlp.fc2.weight''', F'''beit.encoder.layer.{i}.output.dense.weight''') ) rename_keys.append((F'''{prefix}blocks.{i}.mlp.fc2.bias''', F'''beit.encoder.layer.{i}.output.dense.bias''') ) # projection layer + position embeddings rename_keys.extend( [ (F'''{prefix}cls_token''', '''beit.embeddings.cls_token'''), (F'''{prefix}patch_embed.proj.weight''', '''beit.embeddings.patch_embeddings.projection.weight'''), (F'''{prefix}patch_embed.proj.bias''', '''beit.embeddings.patch_embeddings.projection.bias'''), (F'''{prefix}pos_embed''', '''beit.embeddings.position_embeddings'''), ] ) if has_lm_head: # mask token + layernorm rename_keys.extend( [ ('''mask_token''', '''beit.embeddings.mask_token'''), ('''norm.weight''', '''layernorm.weight'''), ('''norm.bias''', '''layernorm.bias'''), ] ) else: # layernorm + classification head rename_keys.extend( [ ('''fc_norm.weight''', '''beit.pooler.layernorm.weight'''), ('''fc_norm.bias''', '''beit.pooler.layernorm.bias'''), ('''head.weight''', '''classifier.weight'''), ('''head.bias''', '''classifier.bias'''), ] ) return rename_keys def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__=False , SCREAMING_SNAKE_CASE__=False ): for i in range(config.num_hidden_layers ): snake_case_ = '''backbone.''' if is_semantic else '''''' # queries, keys and values snake_case_ = state_dict.pop(F'''{prefix}blocks.{i}.attn.qkv.weight''' ) snake_case_ = state_dict.pop(F'''{prefix}blocks.{i}.attn.q_bias''' ) snake_case_ = state_dict.pop(F'''{prefix}blocks.{i}.attn.v_bias''' ) snake_case_ = in_proj_weight[ : config.hidden_size, : ] snake_case_ = q_bias snake_case_ = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] snake_case_ = in_proj_weight[ -config.hidden_size :, : ] snake_case_ = v_bias # gamma_1 and gamma_2 # we call them lambda because otherwise they are renamed when using .from_pretrained snake_case_ = state_dict.pop(F'''{prefix}blocks.{i}.gamma_1''' ) snake_case_ = state_dict.pop(F'''{prefix}blocks.{i}.gamma_2''' ) snake_case_ = gamma_a snake_case_ = gamma_a def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): snake_case_ = dct.pop(SCREAMING_SNAKE_CASE__ ) snake_case_ = val def __SCREAMING_SNAKE_CASE (): snake_case_ = '''http://images.cocodataset.org/val2017/000000039769.jpg''' snake_case_ = Image.open(requests.get(SCREAMING_SNAKE_CASE__ , stream=SCREAMING_SNAKE_CASE__ ).raw ) return im @torch.no_grad() def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__=False ): snake_case_ = False if '''rvlcdip''' in checkpoint_url else True snake_case_ = BeitConfig(use_absolute_position_embeddings=SCREAMING_SNAKE_CASE__ , use_mask_token=SCREAMING_SNAKE_CASE__ ) # size of the architecture if "large" in checkpoint_url or "dit-l" in checkpoint_url: snake_case_ = 1024 snake_case_ = 4096 snake_case_ = 24 snake_case_ = 16 # labels if "rvlcdip" in checkpoint_url: snake_case_ = 16 snake_case_ = '''huggingface/label-files''' snake_case_ = '''rvlcdip-id2label.json''' snake_case_ = json.load(open(hf_hub_download(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , repo_type='''dataset''' ) , '''r''' ) ) snake_case_ = {int(SCREAMING_SNAKE_CASE__ ): v for k, v in idalabel.items()} snake_case_ = idalabel snake_case_ = {v: k for k, v in idalabel.items()} # load state_dict of original model, remove and rename some keys snake_case_ = torch.hub.load_state_dict_from_url(SCREAMING_SNAKE_CASE__ , map_location='''cpu''' )['''model'''] snake_case_ = create_rename_keys(SCREAMING_SNAKE_CASE__ , has_lm_head=SCREAMING_SNAKE_CASE__ ) for src, dest in rename_keys: rename_key(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) read_in_q_k_v(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , has_lm_head=SCREAMING_SNAKE_CASE__ ) # load HuggingFace model snake_case_ = BeitForMaskedImageModeling(SCREAMING_SNAKE_CASE__ ) if has_lm_head else BeitForImageClassification(SCREAMING_SNAKE_CASE__ ) model.eval() model.load_state_dict(SCREAMING_SNAKE_CASE__ ) # Check outputs on an image snake_case_ = BeitImageProcessor( size=config.image_size , resample=PILImageResampling.BILINEAR , do_center_crop=SCREAMING_SNAKE_CASE__ ) snake_case_ = prepare_img() snake_case_ = image_processor(images=SCREAMING_SNAKE_CASE__ , return_tensors='''pt''' ) snake_case_ = encoding['''pixel_values'''] snake_case_ = model(SCREAMING_SNAKE_CASE__ ) snake_case_ = outputs.logits # verify logits snake_case_ = [1, 16] if '''rvlcdip''' in checkpoint_url else [1, 196, 8192] assert logits.shape == torch.Size(SCREAMING_SNAKE_CASE__ ), "Shape of logits not as expected" Path(SCREAMING_SNAKE_CASE__ ).mkdir(exist_ok=SCREAMING_SNAKE_CASE__ ) print(F'''Saving model to {pytorch_dump_folder_path}''' ) model.save_pretrained(SCREAMING_SNAKE_CASE__ ) print(F'''Saving image processor to {pytorch_dump_folder_path}''' ) image_processor.save_pretrained(SCREAMING_SNAKE_CASE__ ) if push_to_hub: if has_lm_head: snake_case_ = '''dit-base''' if '''base''' in checkpoint_url else '''dit-large''' else: snake_case_ = '''dit-base-finetuned-rvlcdip''' if '''dit-b''' in checkpoint_url else '''dit-large-finetuned-rvlcdip''' image_processor.push_to_hub( repo_path_or_name=Path(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) , organization='''nielsr''' , commit_message='''Add image processor''' , use_temp_dir=SCREAMING_SNAKE_CASE__ , ) model.push_to_hub( repo_path_or_name=Path(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) , organization='''nielsr''' , commit_message='''Add model''' , use_temp_dir=SCREAMING_SNAKE_CASE__ , ) if __name__ == "__main__": lowerCAmelCase_ = argparse.ArgumentParser() parser.add_argument( '''--checkpoint_url''', default='''https://layoutlm.blob.core.windows.net/dit/dit-pts/dit-base-224-p16-500k-62d53a.pth''', type=str, help='''URL to the original PyTorch checkpoint (.pth file).''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the folder to output PyTorch model.''' ) parser.add_argument( '''--push_to_hub''', action='''store_true''', ) lowerCAmelCase_ = parser.parse_args() convert_dit_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path, args.push_to_hub)
39
import math import unittest from transformers import BioGptConfig, is_torch_available from transformers.testing_utils import require_torch, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( BioGptForCausalLM, BioGptForSequenceClassification, BioGptForTokenClassification, BioGptModel, BioGptTokenizer, ) from transformers.models.biogpt.modeling_biogpt import BIOGPT_PRETRAINED_MODEL_ARCHIVE_LIST class snake_case_ : '''simple docstring''' def __init__( self : Optional[int] , _UpperCamelCase : Tuple , _UpperCamelCase : Optional[int]=1_3 , _UpperCamelCase : str=7 , _UpperCamelCase : int=True , _UpperCamelCase : Dict=True , _UpperCamelCase : int=False , _UpperCamelCase : Dict=True , _UpperCamelCase : Optional[int]=9_9 , _UpperCamelCase : str=3_2 , _UpperCamelCase : str=5 , _UpperCamelCase : str=4 , _UpperCamelCase : int=3_7 , _UpperCamelCase : int="gelu" , _UpperCamelCase : List[str]=0.1 , _UpperCamelCase : Dict=0.1 , _UpperCamelCase : str=5_1_2 , _UpperCamelCase : Optional[int]=1_6 , _UpperCamelCase : List[str]=2 , _UpperCamelCase : Any=0.02 , _UpperCamelCase : List[str]=3 , _UpperCamelCase : List[str]=4 , _UpperCamelCase : str=None , ) ->Dict: snake_case_ = parent snake_case_ = batch_size snake_case_ = seq_length snake_case_ = is_training snake_case_ = use_input_mask snake_case_ = use_token_type_ids snake_case_ = use_labels snake_case_ = vocab_size snake_case_ = hidden_size snake_case_ = num_hidden_layers snake_case_ = num_attention_heads snake_case_ = intermediate_size snake_case_ = hidden_act snake_case_ = hidden_dropout_prob snake_case_ = attention_probs_dropout_prob snake_case_ = max_position_embeddings snake_case_ = type_vocab_size snake_case_ = type_sequence_label_size snake_case_ = initializer_range snake_case_ = num_labels snake_case_ = num_choices snake_case_ = scope def snake_case__( self : str ) ->List[Any]: snake_case_ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) snake_case_ = None if self.use_input_mask: snake_case_ = random_attention_mask([self.batch_size, self.seq_length] ) snake_case_ = None if self.use_token_type_ids: snake_case_ = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) snake_case_ = None snake_case_ = None snake_case_ = None if self.use_labels: snake_case_ = ids_tensor([self.batch_size] , self.type_sequence_label_size ) snake_case_ = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) snake_case_ = ids_tensor([self.batch_size] , self.num_choices ) snake_case_ = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def snake_case__( self : List[str] ) ->Tuple: return BioGptConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=_UpperCamelCase , initializer_range=self.initializer_range , ) def snake_case__( self : int , _UpperCamelCase : int , _UpperCamelCase : List[str] , _UpperCamelCase : Optional[Any] , _UpperCamelCase : Any , _UpperCamelCase : List[str] , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : Union[str, Any] ) ->Dict: snake_case_ = BioGptModel(config=_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() snake_case_ = model(_UpperCamelCase , attention_mask=_UpperCamelCase ) snake_case_ = model(_UpperCamelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def snake_case__( self : Optional[Any] , _UpperCamelCase : Dict , _UpperCamelCase : List[str] , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : List[Any] , _UpperCamelCase : int , _UpperCamelCase : int , _UpperCamelCase : int , _UpperCamelCase : Optional[int] , _UpperCamelCase : Union[str, Any] , ) ->Optional[int]: snake_case_ = BioGptForCausalLM(config=_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() snake_case_ = 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 snake_case__( self : Dict , _UpperCamelCase : str , _UpperCamelCase : List[str] , _UpperCamelCase : List[Any] , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : str , *_UpperCamelCase : List[Any] ) ->Union[str, Any]: snake_case_ = BioGptModel(config=_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() # create attention mask snake_case_ = torch.ones(input_ids.shape , dtype=torch.long , device=_UpperCamelCase ) snake_case_ = self.seq_length // 2 snake_case_ = 0 # first forward pass snake_case_, snake_case_ = model(_UpperCamelCase , attention_mask=_UpperCamelCase ).to_tuple() # create hypothetical next token and extent to next_input_ids snake_case_ = ids_tensor((self.batch_size, 1) , config.vocab_size ) # change a random masked slice from input_ids snake_case_ = ids_tensor((1,) , _UpperCamelCase ).item() + 1 snake_case_ = ids_tensor((self.batch_size, 1) , config.vocab_size ).squeeze(-1 ) snake_case_ = random_other_next_tokens # append to next input_ids and attn_mask snake_case_ = torch.cat([input_ids, next_tokens] , dim=-1 ) snake_case_ = torch.cat( [attn_mask, torch.ones((attn_mask.shape[0], 1) , dtype=torch.long , device=_UpperCamelCase )] , dim=1 , ) # get two different outputs snake_case_ = model(_UpperCamelCase , attention_mask=_UpperCamelCase )['''last_hidden_state'''] snake_case_ = model(_UpperCamelCase , past_key_values=_UpperCamelCase , attention_mask=_UpperCamelCase )['''last_hidden_state'''] # select random slice snake_case_ = ids_tensor((1,) , output_from_past.shape[-1] ).item() snake_case_ = output_from_no_past[:, -1, random_slice_idx].detach() snake_case_ = output_from_past[:, 0, random_slice_idx].detach() # test that outputs are equal for slice self.parent.assertTrue(torch.allclose(_UpperCamelCase , _UpperCamelCase , atol=1e-3 ) ) def snake_case__( self : Union[str, Any] , _UpperCamelCase : Optional[int] , _UpperCamelCase : Optional[Any] , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : List[Any] , _UpperCamelCase : Dict , *_UpperCamelCase : List[Any] ) ->int: snake_case_ = BioGptModel(config=_UpperCamelCase ).to(_UpperCamelCase ).eval() snake_case_ = torch.ones(input_ids.shape , dtype=torch.long , device=_UpperCamelCase ) # first forward pass snake_case_ = model(_UpperCamelCase , attention_mask=_UpperCamelCase , use_cache=_UpperCamelCase ) snake_case_, snake_case_ = outputs.to_tuple() # create hypothetical multiple next token and extent to next_input_ids snake_case_ = ids_tensor((self.batch_size, 3) , config.vocab_size ) snake_case_ = ids_tensor((self.batch_size, 3) , 2 ) # append to next input_ids and snake_case_ = torch.cat([input_ids, next_tokens] , dim=-1 ) snake_case_ = torch.cat([attention_mask, next_attn_mask] , dim=-1 ) snake_case_ = model(_UpperCamelCase , attention_mask=_UpperCamelCase )['''last_hidden_state'''] snake_case_ = model(_UpperCamelCase , attention_mask=_UpperCamelCase , past_key_values=_UpperCamelCase )[ '''last_hidden_state''' ] # select random slice snake_case_ = ids_tensor((1,) , output_from_past.shape[-1] ).item() snake_case_ = output_from_no_past[:, -3:, random_slice_idx].detach() snake_case_ = output_from_past[:, :, random_slice_idx].detach() self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1] ) # test that outputs are equal for slice self.parent.assertTrue(torch.allclose(_UpperCamelCase , _UpperCamelCase , atol=1e-3 ) ) def snake_case__( self : int , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : str , _UpperCamelCase : str , _UpperCamelCase : Dict , _UpperCamelCase : Optional[Any] , *_UpperCamelCase : List[Any] , _UpperCamelCase : List[str]=False ) ->Dict: snake_case_ = BioGptForCausalLM(_UpperCamelCase ) model.to(_UpperCamelCase ) if gradient_checkpointing: model.gradient_checkpointing_enable() snake_case_ = model(_UpperCamelCase , labels=_UpperCamelCase ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) result.loss.backward() def snake_case__( self : List[Any] , _UpperCamelCase : Optional[int] , *_UpperCamelCase : Dict ) ->Dict: snake_case_ = BioGptModel(_UpperCamelCase ) snake_case_ = model.config.initializer_range / math.sqrt(2 * model.config.num_hidden_layers ) for key in model.state_dict().keys(): if "c_proj" in key and "weight" in key: self.parent.assertLessEqual(abs(torch.std(model.state_dict()[key] ) - model_std ) , 0.001 ) self.parent.assertLessEqual(abs(torch.mean(model.state_dict()[key] ) - 0.0 ) , 0.01 ) def snake_case__( self : Any , _UpperCamelCase : Tuple , _UpperCamelCase : List[str] , _UpperCamelCase : List[Any] , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : int , *_UpperCamelCase : List[str] ) ->int: snake_case_ = self.num_labels snake_case_ = BioGptForTokenClassification(_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() snake_case_ = model(_UpperCamelCase , attention_mask=_UpperCamelCase , token_type_ids=_UpperCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def snake_case__( self : Optional[Any] ) ->int: snake_case_ = self.prepare_config_and_inputs() ( ( snake_case_ ), ( snake_case_ ), ( snake_case_ ), ( snake_case_ ), ( snake_case_ ), ( snake_case_ ), ( snake_case_ ), ) = config_and_inputs snake_case_ = {'''input_ids''': input_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_torch class snake_case_ ( __A , __A , __A , unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = ( (BioGptModel, BioGptForCausalLM, BioGptForSequenceClassification, BioGptForTokenClassification) if is_torch_available() else () ) SCREAMING_SNAKE_CASE : Tuple = (BioGptForCausalLM,) if is_torch_available() else () SCREAMING_SNAKE_CASE : Optional[Any] = ( { "feature-extraction": BioGptModel, "text-classification": BioGptForSequenceClassification, "text-generation": BioGptForCausalLM, "token-classification": BioGptForTokenClassification, "zero-shot": BioGptForSequenceClassification, } if is_torch_available() else {} ) SCREAMING_SNAKE_CASE : Tuple = False def snake_case__( self : List[str] ) ->Union[str, Any]: snake_case_ = BioGptModelTester(self ) snake_case_ = ConfigTester(self , config_class=_UpperCamelCase , hidden_size=3_7 ) def snake_case__( self : str ) ->int: self.config_tester.run_common_tests() def snake_case__( self : str ) ->Tuple: snake_case_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_UpperCamelCase ) def snake_case__( self : Tuple ) ->List[Any]: snake_case_ = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: snake_case_ = type self.model_tester.create_and_check_model(*_UpperCamelCase ) def snake_case__( self : Tuple ) ->str: snake_case_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_biogpt_model_attention_mask_past(*_UpperCamelCase ) def snake_case__( self : Union[str, Any] ) ->Dict: snake_case_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_forward_and_backwards(*_UpperCamelCase , gradient_checkpointing=_UpperCamelCase ) def snake_case__( self : Optional[int] ) ->List[Any]: snake_case_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_biogpt_model_past_large_inputs(*_UpperCamelCase ) def snake_case__( self : List[Any] ) ->Union[str, Any]: snake_case_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_biogpt_weight_initialization(*_UpperCamelCase ) def snake_case__( self : Optional[int] ) ->Optional[int]: snake_case_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_biogpt_for_token_classification(*_UpperCamelCase ) @slow def snake_case__( self : int ) ->Optional[Any]: snake_case_ = BioGptForCausalLM.from_pretrained('''microsoft/biogpt''' ) model.to(_UpperCamelCase ) snake_case_ = BioGptTokenizer.from_pretrained('''microsoft/biogpt''' ) snake_case_ = '''left''' # Define PAD Token = EOS Token = 50256 snake_case_ = tokenizer.eos_token snake_case_ = model.config.eos_token_id # use different length sentences to test batching snake_case_ = [ '''Hello, my dog is a little''', '''Today, I''', ] snake_case_ = tokenizer(_UpperCamelCase , return_tensors='''pt''' , padding=_UpperCamelCase ) snake_case_ = inputs['''input_ids'''].to(_UpperCamelCase ) snake_case_ = model.generate( input_ids=_UpperCamelCase , attention_mask=inputs['''attention_mask'''].to(_UpperCamelCase ) , ) snake_case_ = tokenizer(sentences[0] , return_tensors='''pt''' ).input_ids.to(_UpperCamelCase ) snake_case_ = model.generate(input_ids=_UpperCamelCase ) snake_case_ = inputs_non_padded.shape[-1] - inputs['''attention_mask'''][-1].long().sum().cpu().item() snake_case_ = tokenizer(sentences[1] , return_tensors='''pt''' ).input_ids.to(_UpperCamelCase ) snake_case_ = model.generate(input_ids=_UpperCamelCase , max_length=model.config.max_length - num_paddings ) snake_case_ = tokenizer.batch_decode(_UpperCamelCase , skip_special_tokens=_UpperCamelCase ) snake_case_ = tokenizer.decode(output_non_padded[0] , skip_special_tokens=_UpperCamelCase ) snake_case_ = tokenizer.decode(output_padded[0] , skip_special_tokens=_UpperCamelCase ) snake_case_ = [ '''Hello, my dog is a little bit bigger than a little bit.''', '''Today, I have a good idea of how to use the information''', ] self.assertListEqual(_UpperCamelCase , _UpperCamelCase ) self.assertListEqual(_UpperCamelCase , [non_padded_sentence, padded_sentence] ) @slow def snake_case__( self : Optional[int] ) ->List[str]: for model_name in BIOGPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: snake_case_ = BioGptModel.from_pretrained(_UpperCamelCase ) self.assertIsNotNone(_UpperCamelCase ) def snake_case__( self : Optional[int] ) ->str: snake_case_, snake_case_ = self.model_tester.prepare_config_and_inputs_for_common() snake_case_ = 3 snake_case_ = input_dict['''input_ids'''] snake_case_ = input_ids.ne(1 ).to(_UpperCamelCase ) snake_case_ = ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size ) snake_case_ = BioGptForSequenceClassification(_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() snake_case_ = model(_UpperCamelCase , attention_mask=_UpperCamelCase , labels=_UpperCamelCase ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def snake_case__( self : str ) ->str: snake_case_, snake_case_ = self.model_tester.prepare_config_and_inputs_for_common() snake_case_ = 3 snake_case_ = '''multi_label_classification''' snake_case_ = input_dict['''input_ids'''] snake_case_ = input_ids.ne(1 ).to(_UpperCamelCase ) snake_case_ = ids_tensor( [self.model_tester.batch_size, config.num_labels] , self.model_tester.type_sequence_label_size ).to(torch.float ) snake_case_ = BioGptForSequenceClassification(_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() snake_case_ = model(_UpperCamelCase , attention_mask=_UpperCamelCase , labels=_UpperCamelCase ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) @require_torch class snake_case_ ( unittest.TestCase ): '''simple docstring''' @slow def snake_case__( self : int ) ->Any: snake_case_ = BioGptForCausalLM.from_pretrained('''microsoft/biogpt''' ) snake_case_ = torch.tensor([[2, 4_8_0_5, 9, 6_5_6, 2_1]] ) snake_case_ = model(_UpperCamelCase )[0] snake_case_ = 4_2_3_8_4 snake_case_ = torch.Size((1, 5, vocab_size) ) self.assertEqual(output.shape , _UpperCamelCase ) snake_case_ = torch.tensor( [[[-9.5236, -9.8918, 10.4557], [-11.0469, -9.6423, 8.1022], [-8.8664, -7.8826, 5.5325]]] ) self.assertTrue(torch.allclose(output[:, :3, :3] , _UpperCamelCase , atol=1e-4 ) ) @slow def snake_case__( self : List[str] ) ->Optional[int]: snake_case_ = BioGptTokenizer.from_pretrained('''microsoft/biogpt''' ) snake_case_ = BioGptForCausalLM.from_pretrained('''microsoft/biogpt''' ) model.to(_UpperCamelCase ) torch.manual_seed(0 ) snake_case_ = tokenizer('''COVID-19 is''' , return_tensors='''pt''' ).to(_UpperCamelCase ) snake_case_ = model.generate( **_UpperCamelCase , min_length=1_0_0 , max_length=1_0_2_4 , num_beams=5 , early_stopping=_UpperCamelCase , ) snake_case_ = tokenizer.decode(output_ids[0] , skip_special_tokens=_UpperCamelCase ) snake_case_ = ( '''COVID-19 is a global pandemic caused by severe acute respiratory syndrome coronavirus 2 (SARS-CoV-2), the''' ''' causative agent of coronavirus disease 2019 (COVID-19), which has spread to more than 200 countries and''' ''' territories, including the United States (US), Canada, Australia, New Zealand, the United Kingdom (UK),''' ''' and the United States of America (USA), as of March 11, 2020, with more than 800,000 confirmed cases and''' ''' more than 800,000 deaths.''' ) self.assertEqual(_UpperCamelCase , _UpperCamelCase )
39
1
from __future__ import annotations from random import random from typing import Generic, TypeVar lowerCAmelCase_ = TypeVar('''KT''') lowerCAmelCase_ = TypeVar('''VT''') class snake_case_ ( Generic[KT, VT] ): '''simple docstring''' def __init__( self : Optional[Any] , _UpperCamelCase : KT | str = "root" , _UpperCamelCase : VT | None = None ) ->Dict: snake_case_ = key snake_case_ = value snake_case_ = [] def __repr__( self : str ) ->str: return f'''Node({self.key}: {self.value})''' @property def snake_case__( self : Dict ) ->int: return len(self.forward ) class snake_case_ ( Generic[KT, VT] ): '''simple docstring''' def __init__( self : int , _UpperCamelCase : float = 0.5 , _UpperCamelCase : int = 1_6 ) ->Union[str, Any]: snake_case_ = Node[KT, VT]() snake_case_ = 0 snake_case_ = p snake_case_ = max_level def __str__( self : List[str] ) ->str: snake_case_ = list(self ) if len(_UpperCamelCase ) == 0: return f'''SkipList(level={self.level})''' snake_case_ = max((len(str(_UpperCamelCase ) ) for item in items) , default=4 ) snake_case_ = max(_UpperCamelCase , 4 ) + 4 snake_case_ = self.head snake_case_ = [] snake_case_ = node.forward.copy() lines.append(f'''[{node.key}]'''.ljust(_UpperCamelCase , '''-''' ) + '''* ''' * len(_UpperCamelCase ) ) lines.append(''' ''' * label_size + '''| ''' * len(_UpperCamelCase ) ) while len(node.forward ) != 0: snake_case_ = node.forward[0] lines.append( f'''[{node.key}]'''.ljust(_UpperCamelCase , '''-''' ) + ''' '''.join(str(n.key ) if n.key == node.key else '''|''' for n in forwards ) ) lines.append(''' ''' * label_size + '''| ''' * len(_UpperCamelCase ) ) snake_case_ = node.forward lines.append('''None'''.ljust(_UpperCamelCase ) + '''* ''' * len(_UpperCamelCase ) ) return f'''SkipList(level={self.level})\n''' + "\n".join(_UpperCamelCase ) def __iter__( self : Optional[Any] ) ->List[Any]: snake_case_ = self.head while len(node.forward ) != 0: yield node.forward[0].key snake_case_ = node.forward[0] def snake_case__( self : str ) ->int: snake_case_ = 1 while random() < self.p and level < self.max_level: level += 1 return level def snake_case__( self : str , _UpperCamelCase : Union[str, Any] ) ->tuple[Node[KT, VT] | None, list[Node[KT, VT]]]: snake_case_ = [] snake_case_ = self.head for i in reversed(range(self.level ) ): # i < node.level - When node level is lesser than `i` decrement `i`. # node.forward[i].key < key - Jumping to node with key value higher # or equal to searched key would result # in skipping searched key. while i < node.level and node.forward[i].key < key: snake_case_ = node.forward[i] # Each leftmost node (relative to searched node) will potentially have to # be updated. update_vector.append(_UpperCamelCase ) update_vector.reverse() # Note that we were inserting values in reverse order. # len(node.forward) != 0 - If current node doesn't contain any further # references then searched key is not present. # node.forward[0].key == key - Next node key should be equal to search key # if key is present. if len(node.forward ) != 0 and node.forward[0].key == key: return node.forward[0], update_vector else: return None, update_vector def snake_case__( self : List[Any] , _UpperCamelCase : KT ) ->Any: snake_case_, snake_case_ = self._locate_node(_UpperCamelCase ) if node is not None: for i, update_node in enumerate(_UpperCamelCase ): # Remove or replace all references to removed node. if update_node.level > i and update_node.forward[i].key == key: if node.level > i: snake_case_ = node.forward[i] else: snake_case_ = update_node.forward[:i] def snake_case__( self : str , _UpperCamelCase : KT , _UpperCamelCase : VT ) ->Optional[Any]: snake_case_, snake_case_ = self._locate_node(_UpperCamelCase ) if node is not None: snake_case_ = value else: snake_case_ = self.random_level() if level > self.level: # After level increase we have to add additional nodes to head. for _ in range(self.level - 1 , _UpperCamelCase ): update_vector.append(self.head ) snake_case_ = level snake_case_ = Node(_UpperCamelCase , _UpperCamelCase ) for i, update_node in enumerate(update_vector[:level] ): # Change references to pass through new node. if update_node.level > i: new_node.forward.append(update_node.forward[i] ) if update_node.level < i + 1: update_node.forward.append(_UpperCamelCase ) else: snake_case_ = new_node def snake_case__( self : Union[str, Any] , _UpperCamelCase : VT ) ->VT | None: snake_case_, snake_case_ = self._locate_node(_UpperCamelCase ) if node is not None: return node.value return None def __SCREAMING_SNAKE_CASE (): snake_case_ = SkipList() skip_list.insert('''Key1''' , 3 ) skip_list.insert('''Key2''' , 12 ) skip_list.insert('''Key3''' , 41 ) skip_list.insert('''Key4''' , -19 ) snake_case_ = skip_list.head snake_case_ = {} while node.level != 0: snake_case_ = node.forward[0] snake_case_ = node.value assert len(SCREAMING_SNAKE_CASE__ ) == 4 assert all_values["Key1"] == 3 assert all_values["Key2"] == 12 assert all_values["Key3"] == 41 assert all_values["Key4"] == -19 def __SCREAMING_SNAKE_CASE (): snake_case_ = SkipList() skip_list.insert('''Key1''' , 10 ) skip_list.insert('''Key1''' , 12 ) skip_list.insert('''Key5''' , 7 ) skip_list.insert('''Key7''' , 10 ) skip_list.insert('''Key10''' , 5 ) skip_list.insert('''Key7''' , 7 ) skip_list.insert('''Key5''' , 5 ) skip_list.insert('''Key10''' , 10 ) snake_case_ = skip_list.head snake_case_ = {} while node.level != 0: snake_case_ = node.forward[0] snake_case_ = node.value if len(SCREAMING_SNAKE_CASE__ ) != 4: print() assert len(SCREAMING_SNAKE_CASE__ ) == 4 assert all_values["Key1"] == 12 assert all_values["Key7"] == 7 assert all_values["Key5"] == 5 assert all_values["Key10"] == 10 def __SCREAMING_SNAKE_CASE (): snake_case_ = SkipList() assert skip_list.find('''Some key''' ) is None def __SCREAMING_SNAKE_CASE (): snake_case_ = SkipList() skip_list.insert('''Key2''' , 20 ) assert skip_list.find('''Key2''' ) == 20 skip_list.insert('''Some Key''' , 10 ) skip_list.insert('''Key2''' , 8 ) skip_list.insert('''V''' , 13 ) assert skip_list.find('''Y''' ) is None assert skip_list.find('''Key2''' ) == 8 assert skip_list.find('''Some Key''' ) == 10 assert skip_list.find('''V''' ) == 13 def __SCREAMING_SNAKE_CASE (): snake_case_ = SkipList() skip_list.delete('''Some key''' ) assert len(skip_list.head.forward ) == 0 def __SCREAMING_SNAKE_CASE (): snake_case_ = SkipList() skip_list.insert('''Key1''' , 12 ) skip_list.insert('''V''' , 13 ) skip_list.insert('''X''' , 14 ) skip_list.insert('''Key2''' , 15 ) skip_list.delete('''V''' ) skip_list.delete('''Key2''' ) assert skip_list.find('''V''' ) is None assert skip_list.find('''Key2''' ) is None def __SCREAMING_SNAKE_CASE (): snake_case_ = SkipList() skip_list.insert('''Key1''' , 12 ) skip_list.insert('''V''' , 13 ) skip_list.insert('''X''' , 14 ) skip_list.insert('''Key2''' , 15 ) skip_list.delete('''V''' ) assert skip_list.find('''V''' ) is None assert skip_list.find('''X''' ) == 14 assert skip_list.find('''Key1''' ) == 12 assert skip_list.find('''Key2''' ) == 15 skip_list.delete('''X''' ) assert skip_list.find('''V''' ) is None assert skip_list.find('''X''' ) is None assert skip_list.find('''Key1''' ) == 12 assert skip_list.find('''Key2''' ) == 15 skip_list.delete('''Key1''' ) assert skip_list.find('''V''' ) is None assert skip_list.find('''X''' ) is None assert skip_list.find('''Key1''' ) is None assert skip_list.find('''Key2''' ) == 15 skip_list.delete('''Key2''' ) assert skip_list.find('''V''' ) is None assert skip_list.find('''X''' ) is None assert skip_list.find('''Key1''' ) is None assert skip_list.find('''Key2''' ) is None def __SCREAMING_SNAKE_CASE (): snake_case_ = SkipList() skip_list.insert('''Key1''' , 12 ) skip_list.insert('''V''' , 13 ) skip_list.insert('''X''' , 142 ) skip_list.insert('''Key2''' , 15 ) skip_list.delete('''X''' ) def traverse_keys(SCREAMING_SNAKE_CASE__ ): yield node.key for forward_node in node.forward: yield from traverse_keys(SCREAMING_SNAKE_CASE__ ) assert len(set(traverse_keys(skip_list.head ) ) ) == 4 def __SCREAMING_SNAKE_CASE (): def is_sorted(SCREAMING_SNAKE_CASE__ ): return all(next_item >= item for item, next_item in zip(SCREAMING_SNAKE_CASE__ , lst[1:] ) ) snake_case_ = SkipList() for i in range(10 ): skip_list.insert(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) assert is_sorted(list(SCREAMING_SNAKE_CASE__ ) ) skip_list.delete(5 ) skip_list.delete(8 ) skip_list.delete(2 ) assert is_sorted(list(SCREAMING_SNAKE_CASE__ ) ) skip_list.insert(-12 , -12 ) skip_list.insert(77 , 77 ) assert is_sorted(list(SCREAMING_SNAKE_CASE__ ) ) def __SCREAMING_SNAKE_CASE (): for _ in range(100 ): # Repeat test 100 times due to the probabilistic nature of skip list # random values == random bugs test_insert() test_insert_overrides_existing_value() test_searching_empty_list_returns_none() test_search() test_deleting_item_from_empty_list_do_nothing() test_deleted_items_are_not_founded_by_find_method() test_delete_removes_only_given_key() test_delete_doesnt_leave_dead_nodes() test_iter_always_yields_sorted_values() def __SCREAMING_SNAKE_CASE (): snake_case_ = SkipList() skip_list.insert(2 , '''2''' ) skip_list.insert(4 , '''4''' ) skip_list.insert(6 , '''4''' ) skip_list.insert(4 , '''5''' ) skip_list.insert(8 , '''4''' ) skip_list.insert(9 , '''4''' ) skip_list.delete(4 ) print(SCREAMING_SNAKE_CASE__ ) if __name__ == "__main__": import doctest doctest.testmod() main()
39
def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): # "extended trapezoidal rule" # int(f) = dx/2 * (f1 + 2f2 + ... + fn) snake_case_ = (boundary[1] - boundary[0]) / steps snake_case_ = boundary[0] snake_case_ = boundary[1] snake_case_ = make_points(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) snake_case_ = 0.0 y += (h / 2.0) * f(SCREAMING_SNAKE_CASE__ ) for i in x_i: # print(i) y += h * f(SCREAMING_SNAKE_CASE__ ) y += (h / 2.0) * f(SCREAMING_SNAKE_CASE__ ) return y def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): snake_case_ = a + h while x < (b - h): yield x snake_case_ = x + h def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): # enter your function here snake_case_ = (x - 0) * (x - 0) return y def __SCREAMING_SNAKE_CASE (): snake_case_ = 0.0 # Lower bound of integration snake_case_ = 1.0 # Upper bound of integration snake_case_ = 10.0 # define number of steps or resolution snake_case_ = [a, b] # define boundary of integration snake_case_ = method_a(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) print(F'''y = {y}''' ) if __name__ == "__main__": main()
39
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_speech_available, is_torch_available, ) lowerCAmelCase_ = { '''configuration_trocr''': ['''TROCR_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''TrOCRConfig'''], '''processing_trocr''': ['''TrOCRProcessor'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ = [ '''TROCR_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TrOCRForCausalLM''', '''TrOCRPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_trocr import TROCR_PRETRAINED_CONFIG_ARCHIVE_MAP, TrOCRConfig from .processing_trocr import TrOCRProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_trocr import TROCR_PRETRAINED_MODEL_ARCHIVE_LIST, TrOCRForCausalLM, TrOCRPreTrainedModel else: import sys lowerCAmelCase_ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
39
import os import re import sys import traceback import warnings from pathlib import Path from typing import Dict, Optional, Union from uuid import uuida from huggingface_hub import HfFolder, ModelCard, ModelCardData, hf_hub_download, whoami from huggingface_hub.file_download import REGEX_COMMIT_HASH from huggingface_hub.utils import ( EntryNotFoundError, RepositoryNotFoundError, RevisionNotFoundError, is_jinja_available, ) from packaging import version from requests import HTTPError from .. import __version__ from .constants import ( DEPRECATED_REVISION_ARGS, DIFFUSERS_CACHE, HUGGINGFACE_CO_RESOLVE_ENDPOINT, SAFETENSORS_WEIGHTS_NAME, WEIGHTS_NAME, ) from .import_utils import ( ENV_VARS_TRUE_VALUES, _flax_version, _jax_version, _onnxruntime_version, _torch_version, is_flax_available, is_onnx_available, is_torch_available, ) from .logging import get_logger lowerCAmelCase_ = get_logger(__name__) lowerCAmelCase_ = Path(__file__).parent / '''model_card_template.md''' lowerCAmelCase_ = uuida().hex lowerCAmelCase_ = os.getenv('''HF_HUB_OFFLINE''', '''''').upper() in ENV_VARS_TRUE_VALUES lowerCAmelCase_ = os.getenv('''DISABLE_TELEMETRY''', '''''').upper() in ENV_VARS_TRUE_VALUES lowerCAmelCase_ = HUGGINGFACE_CO_RESOLVE_ENDPOINT + '''/api/telemetry/''' def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ = None ): snake_case_ = F'''diffusers/{__version__}; python/{sys.version.split()[0]}; session_id/{SESSION_ID}''' if DISABLE_TELEMETRY or HF_HUB_OFFLINE: return ua + "; telemetry/off" if is_torch_available(): ua += F'''; torch/{_torch_version}''' if is_flax_available(): ua += F'''; jax/{_jax_version}''' ua += F'''; flax/{_flax_version}''' if is_onnx_available(): ua += F'''; onnxruntime/{_onnxruntime_version}''' # CI will set this value to True if os.environ.get('''DIFFUSERS_IS_CI''' , '''''' ).upper() in ENV_VARS_TRUE_VALUES: ua += "; is_ci/true" if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): ua += "; " + "; ".join(F'''{k}/{v}''' for k, v in user_agent.items() ) elif isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): ua += "; " + user_agent return ua def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ = None , SCREAMING_SNAKE_CASE__ = None ): if token is None: snake_case_ = HfFolder.get_token() if organization is None: snake_case_ = whoami(SCREAMING_SNAKE_CASE__ )['''name'''] return F'''{username}/{model_id}''' else: return F'''{organization}/{model_id}''' def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): if not is_jinja_available(): raise ValueError( '''Modelcard rendering is based on Jinja templates.''' ''' Please make sure to have `jinja` installed before using `create_model_card`.''' ''' To install it, please run `pip install Jinja2`.''' ) if hasattr(SCREAMING_SNAKE_CASE__ , '''local_rank''' ) and args.local_rank not in [-1, 0]: return snake_case_ = args.hub_token if hasattr(SCREAMING_SNAKE_CASE__ , '''hub_token''' ) else None snake_case_ = get_full_repo_name(SCREAMING_SNAKE_CASE__ , token=SCREAMING_SNAKE_CASE__ ) snake_case_ = ModelCard.from_template( card_data=ModelCardData( # Card metadata object that will be converted to YAML block language='''en''' , license='''apache-2.0''' , library_name='''diffusers''' , tags=[] , datasets=args.dataset_name , metrics=[] , ) , template_path=SCREAMING_SNAKE_CASE__ , model_name=SCREAMING_SNAKE_CASE__ , repo_name=SCREAMING_SNAKE_CASE__ , dataset_name=args.dataset_name if hasattr(SCREAMING_SNAKE_CASE__ , '''dataset_name''' ) else None , learning_rate=args.learning_rate , train_batch_size=args.train_batch_size , eval_batch_size=args.eval_batch_size , gradient_accumulation_steps=( args.gradient_accumulation_steps if hasattr(SCREAMING_SNAKE_CASE__ , '''gradient_accumulation_steps''' ) else None ) , adam_betaa=args.adam_betaa if hasattr(SCREAMING_SNAKE_CASE__ , '''adam_beta1''' ) else None , adam_betaa=args.adam_betaa if hasattr(SCREAMING_SNAKE_CASE__ , '''adam_beta2''' ) else None , adam_weight_decay=args.adam_weight_decay if hasattr(SCREAMING_SNAKE_CASE__ , '''adam_weight_decay''' ) else None , adam_epsilon=args.adam_epsilon if hasattr(SCREAMING_SNAKE_CASE__ , '''adam_epsilon''' ) else None , lr_scheduler=args.lr_scheduler if hasattr(SCREAMING_SNAKE_CASE__ , '''lr_scheduler''' ) else None , lr_warmup_steps=args.lr_warmup_steps if hasattr(SCREAMING_SNAKE_CASE__ , '''lr_warmup_steps''' ) else None , ema_inv_gamma=args.ema_inv_gamma if hasattr(SCREAMING_SNAKE_CASE__ , '''ema_inv_gamma''' ) else None , ema_power=args.ema_power if hasattr(SCREAMING_SNAKE_CASE__ , '''ema_power''' ) else None , ema_max_decay=args.ema_max_decay if hasattr(SCREAMING_SNAKE_CASE__ , '''ema_max_decay''' ) else None , mixed_precision=args.mixed_precision , ) snake_case_ = os.path.join(args.output_dir , '''README.md''' ) model_card.save(SCREAMING_SNAKE_CASE__ ) def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ = None ): if resolved_file is None or commit_hash is not None: return commit_hash snake_case_ = str(Path(SCREAMING_SNAKE_CASE__ ).as_posix() ) snake_case_ = re.search(R'''snapshots/([^/]+)/''' , SCREAMING_SNAKE_CASE__ ) if search is None: return None snake_case_ = search.groups()[0] return commit_hash if REGEX_COMMIT_HASH.match(SCREAMING_SNAKE_CASE__ ) else None # Old default cache path, potentially to be migrated. # This logic was more or less taken from `transformers`, with the following differences: # - Diffusers doesn't use custom environment variables to specify the cache path. # - There is no need to migrate the cache format, just move the files to the new location. lowerCAmelCase_ = os.path.expanduser( os.getenv('''HF_HOME''', os.path.join(os.getenv('''XDG_CACHE_HOME''', '''~/.cache'''), '''huggingface''')) ) lowerCAmelCase_ = os.path.join(hf_cache_home, '''diffusers''') def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ = None , SCREAMING_SNAKE_CASE__ = None ): if new_cache_dir is None: snake_case_ = DIFFUSERS_CACHE if old_cache_dir is None: snake_case_ = old_diffusers_cache snake_case_ = Path(SCREAMING_SNAKE_CASE__ ).expanduser() snake_case_ = Path(SCREAMING_SNAKE_CASE__ ).expanduser() for old_blob_path in old_cache_dir.glob('''**/blobs/*''' ): if old_blob_path.is_file() and not old_blob_path.is_symlink(): snake_case_ = new_cache_dir / old_blob_path.relative_to(SCREAMING_SNAKE_CASE__ ) new_blob_path.parent.mkdir(parents=SCREAMING_SNAKE_CASE__ , exist_ok=SCREAMING_SNAKE_CASE__ ) os.replace(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) try: os.symlink(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) except OSError: logger.warning( '''Could not create symlink between old cache and new cache. If you use an older version of diffusers again, files will be re-downloaded.''' ) # At this point, old_cache_dir contains symlinks to the new cache (it can still be used). lowerCAmelCase_ = os.path.join(DIFFUSERS_CACHE, '''version_diffusers_cache.txt''') if not os.path.isfile(cache_version_file): lowerCAmelCase_ = 0 else: with open(cache_version_file) as f: try: lowerCAmelCase_ = int(f.read()) except ValueError: lowerCAmelCase_ = 0 if cache_version < 1: lowerCAmelCase_ = os.path.isdir(old_diffusers_cache) and len(os.listdir(old_diffusers_cache)) > 0 if old_cache_is_not_empty: logger.warning( '''The cache for model files in Diffusers v0.14.0 has moved to a new location. Moving your ''' '''existing cached models. This is a one-time operation, you can interrupt it or run it ''' '''later by calling `diffusers.utils.hub_utils.move_cache()`.''' ) try: move_cache() except Exception as e: lowerCAmelCase_ = '''\n'''.join(traceback.format_tb(e.__traceback__)) logger.error( f"""There was a problem when trying to move your cache:\n\n{trace}\n{e.__class__.__name__}: {e}\n\nPlease """ '''file an issue at https://github.com/huggingface/diffusers/issues/new/choose, copy paste this whole ''' '''message and we will do our best to help.''' ) if cache_version < 1: try: os.makedirs(DIFFUSERS_CACHE, exist_ok=True) with open(cache_version_file, '''w''') as f: f.write('''1''') except Exception: logger.warning( f"""There was a problem when trying to write in your cache folder ({DIFFUSERS_CACHE}). Please, ensure """ '''the directory exists and can be written to.''' ) def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ = None ): if variant is not None: snake_case_ = weights_name.split('''.''' ) snake_case_ = splits[:-1] + [variant] + splits[-1:] snake_case_ = '''.'''.join(SCREAMING_SNAKE_CASE__ ) return weights_name def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , *, SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__=None , ): snake_case_ = str(SCREAMING_SNAKE_CASE__ ) if os.path.isfile(SCREAMING_SNAKE_CASE__ ): return pretrained_model_name_or_path elif os.path.isdir(SCREAMING_SNAKE_CASE__ ): if os.path.isfile(os.path.join(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) ): # Load from a PyTorch checkpoint snake_case_ = os.path.join(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) return model_file elif subfolder is not None and os.path.isfile( os.path.join(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) ): snake_case_ = os.path.join(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) return model_file else: raise EnvironmentError( F'''Error no file named {weights_name} found in directory {pretrained_model_name_or_path}.''' ) else: # 1. First check if deprecated way of loading from branches is used if ( revision in DEPRECATED_REVISION_ARGS and (weights_name == WEIGHTS_NAME or weights_name == SAFETENSORS_WEIGHTS_NAME) and version.parse(version.parse(SCREAMING_SNAKE_CASE__ ).base_version ) >= version.parse('''0.20.0''' ) ): try: snake_case_ = hf_hub_download( SCREAMING_SNAKE_CASE__ , filename=_add_variant(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) , cache_dir=SCREAMING_SNAKE_CASE__ , force_download=SCREAMING_SNAKE_CASE__ , proxies=SCREAMING_SNAKE_CASE__ , resume_download=SCREAMING_SNAKE_CASE__ , local_files_only=SCREAMING_SNAKE_CASE__ , use_auth_token=SCREAMING_SNAKE_CASE__ , user_agent=SCREAMING_SNAKE_CASE__ , subfolder=SCREAMING_SNAKE_CASE__ , revision=revision or commit_hash , ) warnings.warn( F'''Loading the variant {revision} from {pretrained_model_name_or_path} via `revision=\'{revision}\'` is deprecated. Loading instead from `revision=\'main\'` with `variant={revision}`. Loading model variants via `revision=\'{revision}\'` will be removed in diffusers v1. Please use `variant=\'{revision}\'` instead.''' , SCREAMING_SNAKE_CASE__ , ) return model_file except: # noqa: E722 warnings.warn( F'''You are loading the variant {revision} from {pretrained_model_name_or_path} via `revision=\'{revision}\'`. This behavior is deprecated and will be removed in diffusers v1. One should use `variant=\'{revision}\'` instead. However, it appears that {pretrained_model_name_or_path} currently does not have a {_add_variant(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ )} file in the \'main\' branch of {pretrained_model_name_or_path}. \n The Diffusers team and community would be very grateful if you could open an issue: https://github.com/huggingface/diffusers/issues/new with the title \'{pretrained_model_name_or_path} is missing {_add_variant(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ )}\' so that the correct variant file can be added.''' , SCREAMING_SNAKE_CASE__ , ) try: # 2. Load model file as usual snake_case_ = hf_hub_download( SCREAMING_SNAKE_CASE__ , filename=SCREAMING_SNAKE_CASE__ , cache_dir=SCREAMING_SNAKE_CASE__ , force_download=SCREAMING_SNAKE_CASE__ , proxies=SCREAMING_SNAKE_CASE__ , resume_download=SCREAMING_SNAKE_CASE__ , local_files_only=SCREAMING_SNAKE_CASE__ , use_auth_token=SCREAMING_SNAKE_CASE__ , user_agent=SCREAMING_SNAKE_CASE__ , subfolder=SCREAMING_SNAKE_CASE__ , revision=revision or commit_hash , ) return model_file except RepositoryNotFoundError: raise EnvironmentError( F'''{pretrained_model_name_or_path} is not a local folder and is not a valid model identifier ''' '''listed on \'https://huggingface.co/models\'\nIf this is a private repository, make sure to pass a ''' '''token having permission to this repo with `use_auth_token` or log in with `huggingface-cli ''' '''login`.''' ) except RevisionNotFoundError: raise EnvironmentError( F'''{revision} is not a valid git identifier (branch name, tag name or commit id) that exists for ''' '''this model name. Check the model page at ''' F'''\'https://huggingface.co/{pretrained_model_name_or_path}\' for available revisions.''' ) except EntryNotFoundError: raise EnvironmentError( F'''{pretrained_model_name_or_path} does not appear to have a file named {weights_name}.''' ) except HTTPError as err: raise EnvironmentError( F'''There was a specific connection error when trying to load {pretrained_model_name_or_path}:\n{err}''' ) except ValueError: raise EnvironmentError( F'''We couldn\'t connect to \'{HUGGINGFACE_CO_RESOLVE_ENDPOINT}\' to load this model, couldn\'t find it''' F''' in the cached files and it looks like {pretrained_model_name_or_path} is not the path to a''' F''' directory containing a file named {weights_name} or''' ''' \nCheckout your internet connection or see how to run the library in''' ''' offline mode at \'https://huggingface.co/docs/diffusers/installation#offline-mode\'.''' ) except EnvironmentError: raise EnvironmentError( F'''Can\'t load the model for \'{pretrained_model_name_or_path}\'. If you were trying to load it from ''' '''\'https://huggingface.co/models\', make sure you don\'t have a local directory with the same name. ''' F'''Otherwise, make sure \'{pretrained_model_name_or_path}\' is the correct path to a directory ''' F'''containing a file named {weights_name}''' )
39
1
import logging import math from functools import partial from typing import Any, Callable, Dict, Iterable, List, Optional, Sequence, Tuple, Union import torch from .tensor_utils import tensor_tree_map, tree_map def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): snake_case_ = [] if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): for v in tree.values(): shapes.extend(_fetch_dims(SCREAMING_SNAKE_CASE__ ) ) elif isinstance(SCREAMING_SNAKE_CASE__ , (list, tuple) ): for t in tree: shapes.extend(_fetch_dims(SCREAMING_SNAKE_CASE__ ) ) elif isinstance(SCREAMING_SNAKE_CASE__ , torch.Tensor ): shapes.append(tree.shape ) else: raise ValueError('''Not supported''' ) return shapes @torch.jit.ignore def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): snake_case_ = [] for d in reversed(SCREAMING_SNAKE_CASE__ ): idx.append(flat_idx % d ) snake_case_ = flat_idx // d return tuple(reversed(SCREAMING_SNAKE_CASE__ ) ) @torch.jit.ignore def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ = None , SCREAMING_SNAKE_CASE__ = None , ): # start_edges and end_edges both indicate whether, starting from any given # dimension, the start/end index is at the top/bottom edge of the # corresponding tensor, modeled as a tree def reduce_edge_list(SCREAMING_SNAKE_CASE__ ) -> None: snake_case_ = True for i in range(len(SCREAMING_SNAKE_CASE__ ) ): snake_case_ = -1 * (i + 1) l[reversed_idx] &= tally snake_case_ = l[reversed_idx] if start_edges is None: snake_case_ = [s == 0 for s in start] reduce_edge_list(SCREAMING_SNAKE_CASE__ ) if end_edges is None: snake_case_ = [e == (d - 1) for e, d in zip(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ )] reduce_edge_list(SCREAMING_SNAKE_CASE__ ) # Base cases. Either start/end are empty and we're done, or the final, # one-dimensional tensor can be simply sliced if len(SCREAMING_SNAKE_CASE__ ) == 0: return [()] elif len(SCREAMING_SNAKE_CASE__ ) == 1: return [(slice(start[0] , end[0] + 1 ),)] snake_case_ = [] snake_case_ = [] # Dimensions common to start and end can be selected directly for s, e in zip(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): if s == e: path_list.append(slice(SCREAMING_SNAKE_CASE__ , s + 1 ) ) else: break snake_case_ = tuple(SCREAMING_SNAKE_CASE__ ) snake_case_ = len(SCREAMING_SNAKE_CASE__ ) # start == end, and we're done if divergence_idx == len(SCREAMING_SNAKE_CASE__ ): return [path] def upper() -> Tuple[Tuple[slice, ...], ...]: assert start_edges is not None assert end_edges is not None snake_case_ = start[divergence_idx] return tuple( path + (slice(SCREAMING_SNAKE_CASE__ , sdi + 1 ),) + s for s in _get_minimal_slice_set( start[divergence_idx + 1 :] , [d - 1 for d in dims[divergence_idx + 1 :]] , dims[divergence_idx + 1 :] , start_edges=start_edges[divergence_idx + 1 :] , end_edges=[True for _ in end_edges[divergence_idx + 1 :]] , ) ) def lower() -> Tuple[Tuple[slice, ...], ...]: assert start_edges is not None assert end_edges is not None snake_case_ = end[divergence_idx] return tuple( path + (slice(SCREAMING_SNAKE_CASE__ , edi + 1 ),) + s for s in _get_minimal_slice_set( [0 for _ in start[divergence_idx + 1 :]] , end[divergence_idx + 1 :] , dims[divergence_idx + 1 :] , start_edges=[True for _ in start_edges[divergence_idx + 1 :]] , end_edges=end_edges[divergence_idx + 1 :] , ) ) # If both start and end are at the edges of the subtree rooted at # divergence_idx, we can just select the whole subtree at once if start_edges[divergence_idx] and end_edges[divergence_idx]: slices.append(path + (slice(start[divergence_idx] , end[divergence_idx] + 1 ),) ) # If just start is at the edge, we can grab almost all of the subtree, # treating only the ragged bottom edge as an edge case elif start_edges[divergence_idx]: slices.append(path + (slice(start[divergence_idx] , end[divergence_idx] ),) ) slices.extend(lower() ) # Analogous to the previous case, but the top is ragged this time elif end_edges[divergence_idx]: slices.extend(upper() ) slices.append(path + (slice(start[divergence_idx] + 1 , end[divergence_idx] + 1 ),) ) # If both sides of the range are ragged, we need to handle both sides # separately. If there's contiguous meat in between them, we can index it # in one big chunk else: slices.extend(upper() ) snake_case_ = end[divergence_idx] - start[divergence_idx] if middle_ground > 1: slices.append(path + (slice(start[divergence_idx] + 1 , end[divergence_idx] ),) ) slices.extend(lower() ) return slices @torch.jit.ignore def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): snake_case_ = t.shape[:no_batch_dims] snake_case_ = list(_flat_idx_to_idx(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) ) # _get_minimal_slice_set is inclusive snake_case_ = list(_flat_idx_to_idx(flat_end - 1 , SCREAMING_SNAKE_CASE__ ) ) # Get an ordered list of slices to perform snake_case_ = _get_minimal_slice_set( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , ) snake_case_ = [t[s] for s in slices] return torch.cat([s.view((-1,) + t.shape[no_batch_dims:] ) for s in sliced_tensors] ) def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ = False , SCREAMING_SNAKE_CASE__ = None , SCREAMING_SNAKE_CASE__ = False , ): if not (len(SCREAMING_SNAKE_CASE__ ) > 0): raise ValueError('''Must provide at least one input''' ) snake_case_ = [shape[:no_batch_dims] for shape in _fetch_dims(SCREAMING_SNAKE_CASE__ )] snake_case_ = tuple([max(SCREAMING_SNAKE_CASE__ ) for s in zip(*SCREAMING_SNAKE_CASE__ )] ) def _prep_inputs(SCREAMING_SNAKE_CASE__ ) -> torch.Tensor: if not low_mem: if not sum(t.shape[:no_batch_dims] ) == no_batch_dims: snake_case_ = t.expand(orig_batch_dims + t.shape[no_batch_dims:] ) snake_case_ = t.reshape(-1 , *t.shape[no_batch_dims:] ) else: snake_case_ = t.expand(orig_batch_dims + t.shape[no_batch_dims:] ) return t snake_case_ = tensor_tree_map(_prep_inputs , SCREAMING_SNAKE_CASE__ ) snake_case_ = None if _out is not None: snake_case_ = tensor_tree_map(lambda SCREAMING_SNAKE_CASE__ : t.view([-1] + list(t.shape[no_batch_dims:] ) ) , _out ) snake_case_ = 1 for d in orig_batch_dims: flat_batch_dim *= d snake_case_ = flat_batch_dim // chunk_size + (flat_batch_dim % chunk_size != 0) def _select_chunk(SCREAMING_SNAKE_CASE__ ) -> torch.Tensor: return t[i : i + chunk_size] if t.shape[0] != 1 else t snake_case_ = 0 snake_case_ = prepped_outputs for _ in range(SCREAMING_SNAKE_CASE__ ): # Chunk the input if not low_mem: snake_case_ = _select_chunk else: snake_case_ = partial( _chunk_slice , flat_start=SCREAMING_SNAKE_CASE__ , flat_end=min(SCREAMING_SNAKE_CASE__ , i + chunk_size ) , no_batch_dims=len(SCREAMING_SNAKE_CASE__ ) , ) snake_case_ = tensor_tree_map(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) # Run the layer on the chunk snake_case_ = layer(**SCREAMING_SNAKE_CASE__ ) # Allocate space for the output if out is None: snake_case_ = tensor_tree_map(lambda SCREAMING_SNAKE_CASE__ : t.new_zeros((flat_batch_dim,) + t.shape[1:] ) , SCREAMING_SNAKE_CASE__ ) # Put the chunk in its pre-allocated space if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): def assign(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> None: for k, v in da.items(): if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): assign(SCREAMING_SNAKE_CASE__ , da[k] ) else: if _add_into_out: v[i : i + chunk_size] += da[k] else: snake_case_ = da[k] assign(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) elif isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): for xa, xa in zip(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): if _add_into_out: xa[i : i + chunk_size] += xa else: snake_case_ = xa elif isinstance(SCREAMING_SNAKE_CASE__ , torch.Tensor ): if _add_into_out: out[i : i + chunk_size] += output_chunk else: snake_case_ = output_chunk else: raise ValueError('''Not supported''' ) i += chunk_size snake_case_ = tensor_tree_map(lambda SCREAMING_SNAKE_CASE__ : t.view(orig_batch_dims + t.shape[1:] ) , SCREAMING_SNAKE_CASE__ ) return out class snake_case_ : '''simple docstring''' def __init__( self : Optional[Any] , _UpperCamelCase : int = 5_1_2 , ) ->Dict: snake_case_ = max_chunk_size snake_case_ = None snake_case_ = None def snake_case__( self : Any , _UpperCamelCase : Callable , _UpperCamelCase : tuple , _UpperCamelCase : int ) ->int: logging.info('''Tuning chunk size...''' ) if min_chunk_size >= self.max_chunk_size: return min_chunk_size snake_case_ = [2**l for l in range(int(math.log(self.max_chunk_size , 2 ) ) + 1 )] snake_case_ = [c for c in candidates if c > min_chunk_size] snake_case_ = [min_chunk_size] + candidates candidates[-1] += 4 def test_chunk_size(_UpperCamelCase : int ) -> bool: try: with torch.no_grad(): fn(*_UpperCamelCase , chunk_size=_UpperCamelCase ) return True except RuntimeError: return False snake_case_ = 0 snake_case_ = len(_UpperCamelCase ) - 1 while i > min_viable_chunk_size_index: snake_case_ = test_chunk_size(candidates[i] ) if not viable: snake_case_ = (min_viable_chunk_size_index + i) // 2 else: snake_case_ = i snake_case_ = (i + len(_UpperCamelCase ) - 1) // 2 return candidates[min_viable_chunk_size_index] def snake_case__( self : Union[str, Any] , _UpperCamelCase : Iterable , _UpperCamelCase : Iterable ) ->bool: snake_case_ = True for aa, aa in zip(_UpperCamelCase , _UpperCamelCase ): assert type(_UpperCamelCase ) == type(_UpperCamelCase ) if isinstance(_UpperCamelCase , (list, tuple) ): consistent &= self._compare_arg_caches(_UpperCamelCase , _UpperCamelCase ) elif isinstance(_UpperCamelCase , _UpperCamelCase ): snake_case_ = [v for _, v in sorted(aa.items() , key=lambda _UpperCamelCase : x[0] )] snake_case_ = [v for _, v in sorted(aa.items() , key=lambda _UpperCamelCase : x[0] )] consistent &= self._compare_arg_caches(_UpperCamelCase , _UpperCamelCase ) else: consistent &= aa == aa return consistent def snake_case__( self : str , _UpperCamelCase : Callable , _UpperCamelCase : tuple , _UpperCamelCase : int , ) ->int: snake_case_ = True snake_case_ = tree_map(lambda _UpperCamelCase : a.shape if isinstance(_UpperCamelCase , torch.Tensor ) else a , _UpperCamelCase , _UpperCamelCase ) if self.cached_arg_data is not None: # If args have changed shape/value, we need to re-tune assert len(self.cached_arg_data ) == len(_UpperCamelCase ) snake_case_ = self._compare_arg_caches(self.cached_arg_data , _UpperCamelCase ) else: # Otherwise, we can reuse the precomputed value snake_case_ = False if not consistent: snake_case_ = self._determine_favorable_chunk_size( _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , ) snake_case_ = arg_data assert self.cached_chunk_size is not None return self.cached_chunk_size
39
import copy from ...configuration_utils import PretrainedConfig from ...utils import logging from ..bit import BitConfig lowerCAmelCase_ = logging.get_logger(__name__) lowerCAmelCase_ = { '''Intel/dpt-large''': '''https://huggingface.co/Intel/dpt-large/resolve/main/config.json''', # See all DPT models at https://huggingface.co/models?filter=dpt } class snake_case_ ( __A ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = "dpt" def __init__( self : Optional[Any] , _UpperCamelCase : Tuple=7_6_8 , _UpperCamelCase : Dict=1_2 , _UpperCamelCase : Union[str, Any]=1_2 , _UpperCamelCase : List[Any]=3_0_7_2 , _UpperCamelCase : Dict="gelu" , _UpperCamelCase : Union[str, Any]=0.0 , _UpperCamelCase : Optional[int]=0.0 , _UpperCamelCase : Optional[int]=0.02 , _UpperCamelCase : List[str]=1e-12 , _UpperCamelCase : Any=3_8_4 , _UpperCamelCase : int=1_6 , _UpperCamelCase : Any=3 , _UpperCamelCase : Dict=False , _UpperCamelCase : str=True , _UpperCamelCase : Union[str, Any]=[2, 5, 8, 1_1] , _UpperCamelCase : List[str]="project" , _UpperCamelCase : Optional[int]=[4, 2, 1, 0.5] , _UpperCamelCase : Dict=[9_6, 1_9_2, 3_8_4, 7_6_8] , _UpperCamelCase : Dict=2_5_6 , _UpperCamelCase : Optional[Any]=-1 , _UpperCamelCase : int=False , _UpperCamelCase : Optional[int]=True , _UpperCamelCase : str=0.4 , _UpperCamelCase : Tuple=2_5_5 , _UpperCamelCase : Union[str, Any]=0.1 , _UpperCamelCase : Tuple=[1, 1_0_2_4, 2_4, 2_4] , _UpperCamelCase : List[str]=[0, 1] , _UpperCamelCase : List[Any]=None , **_UpperCamelCase : Dict , ) ->Any: super().__init__(**_UpperCamelCase ) snake_case_ = hidden_size snake_case_ = is_hybrid if self.is_hybrid: if backbone_config is None: logger.info('''Initializing the config with a `BiT` backbone.''' ) snake_case_ = { '''global_padding''': '''same''', '''layer_type''': '''bottleneck''', '''depths''': [3, 4, 9], '''out_features''': ['''stage1''', '''stage2''', '''stage3'''], '''embedding_dynamic_padding''': True, } snake_case_ = BitConfig(**_UpperCamelCase ) elif isinstance(_UpperCamelCase , _UpperCamelCase ): logger.info('''Initializing the config with a `BiT` backbone.''' ) snake_case_ = BitConfig(**_UpperCamelCase ) elif isinstance(_UpperCamelCase , _UpperCamelCase ): snake_case_ = backbone_config else: raise ValueError( f'''backbone_config must be a dictionary or a `PretrainedConfig`, got {backbone_config.__class__}.''' ) snake_case_ = backbone_featmap_shape snake_case_ = neck_ignore_stages if readout_type != "project": raise ValueError('''Readout type must be \'project\' when using `DPT-hybrid` mode.''' ) else: snake_case_ = None snake_case_ = None snake_case_ = [] snake_case_ = num_hidden_layers snake_case_ = num_attention_heads snake_case_ = intermediate_size snake_case_ = hidden_act snake_case_ = hidden_dropout_prob snake_case_ = attention_probs_dropout_prob snake_case_ = initializer_range snake_case_ = layer_norm_eps snake_case_ = image_size snake_case_ = patch_size snake_case_ = num_channels snake_case_ = qkv_bias snake_case_ = backbone_out_indices if readout_type not in ["ignore", "add", "project"]: raise ValueError('''Readout_type must be one of [\'ignore\', \'add\', \'project\']''' ) snake_case_ = readout_type snake_case_ = reassemble_factors snake_case_ = neck_hidden_sizes snake_case_ = fusion_hidden_size snake_case_ = head_in_index snake_case_ = use_batch_norm_in_fusion_residual # auxiliary head attributes (semantic segmentation) snake_case_ = use_auxiliary_head snake_case_ = auxiliary_loss_weight snake_case_ = semantic_loss_ignore_index snake_case_ = semantic_classifier_dropout def snake_case__( self : List[str] ) ->List[Any]: snake_case_ = copy.deepcopy(self.__dict__ ) if output["backbone_config"] is not None: snake_case_ = self.backbone_config.to_dict() snake_case_ = self.__class__.model_type return output
39
1
import numpy as np from transformers import BatchFeature from transformers.testing_utils import require_tf, require_torch from .test_feature_extraction_common import FeatureExtractionSavingTestMixin class snake_case_ ( __A ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = None SCREAMING_SNAKE_CASE : int = None @property def snake_case__( self : Optional[Any] ) ->int: return self.feat_extract_tester.prepare_feat_extract_dict() def snake_case__( self : int ) ->Optional[Any]: snake_case_ = self.feature_extraction_class(**self.feat_extract_dict ) self.assertTrue(hasattr(_UpperCamelCase , '''feature_size''' ) ) self.assertTrue(hasattr(_UpperCamelCase , '''sampling_rate''' ) ) self.assertTrue(hasattr(_UpperCamelCase , '''padding_value''' ) ) def snake_case__( self : Tuple ) ->List[str]: snake_case_ = self.feat_extract_tester.prepare_inputs_for_common() snake_case_ = self.feature_extraction_class(**self.feat_extract_dict ) snake_case_ = feat_extract.model_input_names[0] snake_case_ = BatchFeature({input_name: speech_inputs} ) self.assertTrue(all(len(_UpperCamelCase ) == len(_UpperCamelCase ) for x, y in zip(_UpperCamelCase , processed_features[input_name] ) ) ) snake_case_ = self.feat_extract_tester.prepare_inputs_for_common(equal_length=_UpperCamelCase ) snake_case_ = BatchFeature({input_name: speech_inputs} , tensor_type='''np''' ) snake_case_ = processed_features[input_name] if len(batch_features_input.shape ) < 3: snake_case_ = batch_features_input[:, :, None] self.assertTrue( batch_features_input.shape == (self.feat_extract_tester.batch_size, len(speech_inputs[0] ), self.feat_extract_tester.feature_size) ) @require_torch def snake_case__( self : Optional[Any] ) ->Union[str, Any]: snake_case_ = self.feat_extract_tester.prepare_inputs_for_common(equal_length=_UpperCamelCase ) snake_case_ = self.feature_extraction_class(**self.feat_extract_dict ) snake_case_ = feat_extract.model_input_names[0] snake_case_ = BatchFeature({input_name: speech_inputs} , tensor_type='''pt''' ) snake_case_ = processed_features[input_name] if len(batch_features_input.shape ) < 3: snake_case_ = batch_features_input[:, :, None] self.assertTrue( batch_features_input.shape == (self.feat_extract_tester.batch_size, len(speech_inputs[0] ), self.feat_extract_tester.feature_size) ) @require_tf def snake_case__( self : Tuple ) ->str: snake_case_ = self.feat_extract_tester.prepare_inputs_for_common(equal_length=_UpperCamelCase ) snake_case_ = self.feature_extraction_class(**self.feat_extract_dict ) snake_case_ = feat_extract.model_input_names[0] snake_case_ = BatchFeature({input_name: speech_inputs} , tensor_type='''tf''' ) snake_case_ = processed_features[input_name] if len(batch_features_input.shape ) < 3: snake_case_ = batch_features_input[:, :, None] self.assertTrue( batch_features_input.shape == (self.feat_extract_tester.batch_size, len(speech_inputs[0] ), self.feat_extract_tester.feature_size) ) def snake_case__( self : List[str] , _UpperCamelCase : Any=False ) ->List[Any]: def _inputs_have_equal_length(_UpperCamelCase : Dict ): snake_case_ = len(input[0] ) for input_slice in input[1:]: if len(_UpperCamelCase ) != length: return False return True def _inputs_are_equal(_UpperCamelCase : Optional[int] , _UpperCamelCase : Optional[int] ): if len(_UpperCamelCase ) != len(_UpperCamelCase ): return False for input_slice_a, input_slice_a in zip(_UpperCamelCase , _UpperCamelCase ): if not np.allclose(np.asarray(_UpperCamelCase ) , np.asarray(_UpperCamelCase ) , atol=1e-3 ): return False return True snake_case_ = self.feature_extraction_class(**self.feat_extract_dict ) snake_case_ = self.feat_extract_tester.prepare_inputs_for_common(numpify=_UpperCamelCase ) snake_case_ = feat_extract.model_input_names[0] snake_case_ = BatchFeature({input_name: speech_inputs} ) snake_case_ = self.feat_extract_tester.seq_length_diff snake_case_ = self.feat_extract_tester.max_seq_length + pad_diff snake_case_ = self.feat_extract_tester.min_seq_length snake_case_ = self.feat_extract_tester.batch_size snake_case_ = self.feat_extract_tester.feature_size # test padding for List[int] + numpy snake_case_ = feat_extract.pad(_UpperCamelCase , padding=_UpperCamelCase ) snake_case_ = input_a[input_name] snake_case_ = feat_extract.pad(_UpperCamelCase , padding='''longest''' ) snake_case_ = input_a[input_name] snake_case_ = feat_extract.pad(_UpperCamelCase , padding='''max_length''' , max_length=len(speech_inputs[-1] ) ) snake_case_ = input_a[input_name] snake_case_ = feat_extract.pad(_UpperCamelCase , padding='''longest''' , return_tensors='''np''' ) snake_case_ = input_a[input_name] # max_length parameter has to be provided when setting `padding="max_length"` with self.assertRaises(_UpperCamelCase ): feat_extract.pad(_UpperCamelCase , padding='''max_length''' )[input_name] snake_case_ = feat_extract.pad( _UpperCamelCase , padding='''max_length''' , max_length=_UpperCamelCase , return_tensors='''np''' ) snake_case_ = input_a[input_name] self.assertFalse(_inputs_have_equal_length(_UpperCamelCase ) ) self.assertTrue(_inputs_have_equal_length(_UpperCamelCase ) ) self.assertTrue(_inputs_have_equal_length(_UpperCamelCase ) ) self.assertTrue(_inputs_are_equal(_UpperCamelCase , _UpperCamelCase ) ) self.assertTrue(len(input_a[0] ) == pad_min_length ) self.assertTrue(len(input_a[1] ) == pad_min_length + pad_diff ) self.assertTrue(input_a.shape[:2] == (batch_size, len(input_a[0] )) ) self.assertTrue(input_a.shape[:2] == (batch_size, pad_max_length) ) if feature_size > 1: self.assertTrue(input_a.shape[2] == input_a.shape[2] == feature_size ) # test padding for `pad_to_multiple_of` for List[int] + numpy snake_case_ = feat_extract.pad(_UpperCamelCase , pad_to_multiple_of=1_0 ) snake_case_ = input_a[input_name] snake_case_ = feat_extract.pad(_UpperCamelCase , padding='''longest''' , pad_to_multiple_of=1_0 ) snake_case_ = input_a[input_name] snake_case_ = feat_extract.pad( _UpperCamelCase , padding='''max_length''' , pad_to_multiple_of=1_0 , max_length=_UpperCamelCase ) snake_case_ = input_a[input_name] snake_case_ = feat_extract.pad( _UpperCamelCase , padding='''max_length''' , pad_to_multiple_of=1_0 , max_length=_UpperCamelCase , return_tensors='''np''' , ) snake_case_ = input_a[input_name] self.assertTrue(all(len(_UpperCamelCase ) % 1_0 == 0 for x in input_a ) ) self.assertTrue(_inputs_are_equal(_UpperCamelCase , _UpperCamelCase ) ) snake_case_ = pad_max_length if pad_max_length % 1_0 == 0 else (pad_max_length // 1_0 + 1) * 1_0 self.assertTrue(all(len(_UpperCamelCase ) == expected_mult_pad_length for x in input_a ) ) self.assertEqual(input_a.shape[:2] , (batch_size, expected_mult_pad_length) ) if feature_size > 1: self.assertTrue(input_a.shape[2] == feature_size ) # Check padding value is correct snake_case_ = (np.ones(self.feat_extract_tester.feature_size ) * feat_extract.padding_value).sum() self.assertTrue( abs(np.asarray(input_a[0] )[pad_min_length:].sum() - padding_vector_sum * (pad_max_length - pad_min_length) ) < 1e-3 ) self.assertTrue( abs( np.asarray(input_a[1] )[pad_min_length + pad_diff :].sum() - padding_vector_sum * (pad_max_length - pad_min_length - pad_diff) ) < 1e-3 ) self.assertTrue( abs( np.asarray(input_a[2] )[pad_min_length + 2 * pad_diff :].sum() - padding_vector_sum * (pad_max_length - pad_min_length - 2 * pad_diff) ) < 1e-3 ) self.assertTrue( abs(input_a[0, pad_min_length:].sum() - padding_vector_sum * (pad_max_length - pad_min_length) ) < 1e-3 ) self.assertTrue( abs(input_a[0, pad_min_length:].sum() - padding_vector_sum * (expected_mult_pad_length - pad_min_length) ) < 1e-3 ) def snake_case__( self : List[Any] , _UpperCamelCase : str=False ) ->List[Any]: def _inputs_have_equal_length(_UpperCamelCase : Any ): snake_case_ = len(input[0] ) for input_slice in input[1:]: if len(_UpperCamelCase ) != length: return False return True def _inputs_are_equal(_UpperCamelCase : Any , _UpperCamelCase : List[Any] ): if len(_UpperCamelCase ) != len(_UpperCamelCase ): return False for input_slice_a, input_slice_a in zip(_UpperCamelCase , _UpperCamelCase ): if not np.allclose(np.asarray(_UpperCamelCase ) , np.asarray(_UpperCamelCase ) , atol=1e-3 ): return False return True snake_case_ = self.feature_extraction_class(**self.feat_extract_dict ) snake_case_ = self.feat_extract_tester.prepare_inputs_for_common(numpify=_UpperCamelCase ) snake_case_ = feat_extract.model_input_names[0] snake_case_ = BatchFeature({input_name: speech_inputs} ) # truncate to smallest snake_case_ = feat_extract.pad( _UpperCamelCase , padding='''max_length''' , max_length=len(speech_inputs[0] ) , truncation=_UpperCamelCase ) snake_case_ = input_a[input_name] snake_case_ = feat_extract.pad(_UpperCamelCase , padding='''max_length''' , max_length=len(speech_inputs[0] ) ) snake_case_ = input_a[input_name] self.assertTrue(_inputs_have_equal_length(_UpperCamelCase ) ) self.assertFalse(_inputs_have_equal_length(_UpperCamelCase ) ) # truncate to smallest with np snake_case_ = feat_extract.pad( _UpperCamelCase , padding='''max_length''' , max_length=len(speech_inputs[0] ) , return_tensors='''np''' , truncation=_UpperCamelCase , ) snake_case_ = input_a[input_name] snake_case_ = feat_extract.pad( _UpperCamelCase , padding='''max_length''' , max_length=len(speech_inputs[0] ) , return_tensors='''np''' ) snake_case_ = input_a[input_name] self.assertTrue(_inputs_have_equal_length(_UpperCamelCase ) ) self.assertTrue(input_a.shape[1] == len(speech_inputs[0] ) ) # since truncation forces padding to be smaller than longest input # function can't return `np.ndarray`, but has to return list self.assertFalse(_inputs_have_equal_length(_UpperCamelCase ) ) # truncate to middle snake_case_ = feat_extract.pad( _UpperCamelCase , padding='''max_length''' , max_length=len(speech_inputs[1] ) , truncation=_UpperCamelCase , return_tensors='''np''' , ) snake_case_ = input_a[input_name] snake_case_ = feat_extract.pad( _UpperCamelCase , padding='''max_length''' , max_length=len(speech_inputs[1] ) , truncation=_UpperCamelCase ) snake_case_ = input_a[input_name] snake_case_ = feat_extract.pad( _UpperCamelCase , padding='''max_length''' , max_length=len(speech_inputs[1] ) , return_tensors='''np''' ) snake_case_ = input_a[input_name] self.assertTrue(input_a.shape[1] == len(speech_inputs[1] ) ) self.assertTrue(_inputs_have_equal_length(_UpperCamelCase ) ) self.assertTrue(_inputs_have_equal_length(_UpperCamelCase ) ) self.assertTrue(_inputs_are_equal(_UpperCamelCase , _UpperCamelCase ) ) # since truncation forces padding to be smaller than longest input # function can't return `np.ndarray`, but has to return list self.assertFalse(_inputs_have_equal_length(_UpperCamelCase ) ) self.assertTrue(len(input_a[-1] ) == len(speech_inputs[-1] ) ) # padding has to be max_length when setting `truncation=True` with self.assertRaises(_UpperCamelCase ): feat_extract.pad(_UpperCamelCase , truncation=_UpperCamelCase )[input_name] # padding has to be max_length when setting `truncation=True` with self.assertRaises(_UpperCamelCase ): feat_extract.pad(_UpperCamelCase , padding='''longest''' , truncation=_UpperCamelCase )[input_name] # padding has to be max_length when setting `truncation=True` with self.assertRaises(_UpperCamelCase ): feat_extract.pad(_UpperCamelCase , padding='''longest''' , truncation=_UpperCamelCase )[input_name] # max_length parameter has to be provided when setting `truncation=True` and padding="max_length" with self.assertRaises(_UpperCamelCase ): feat_extract.pad(_UpperCamelCase , padding='''max_length''' , truncation=_UpperCamelCase )[input_name] # test truncation for `pad_to_multiple_of` for List[int] + numpy snake_case_ = 1_2 snake_case_ = feat_extract.pad( _UpperCamelCase , padding='''max_length''' , max_length=len(speech_inputs[0] ) , pad_to_multiple_of=_UpperCamelCase , truncation=_UpperCamelCase , ) snake_case_ = input_a[input_name] snake_case_ = feat_extract.pad( _UpperCamelCase , padding='''max_length''' , max_length=len(speech_inputs[0] ) , pad_to_multiple_of=_UpperCamelCase , ) snake_case_ = input_a[input_name] # retrieve expected_length as multiple of pad_to_multiple_of snake_case_ = len(speech_inputs[0] ) if expected_length % pad_to_multiple_of != 0: snake_case_ = ((len(speech_inputs[0] ) // pad_to_multiple_of) + 1) * pad_to_multiple_of self.assertTrue(len(input_a[0] ) == expected_length ) self.assertTrue(_inputs_have_equal_length(_UpperCamelCase ) ) self.assertFalse(_inputs_have_equal_length(_UpperCamelCase ) ) def snake_case__( self : Dict ) ->Optional[int]: self._check_padding(numpify=_UpperCamelCase ) def snake_case__( self : Dict ) ->int: self._check_padding(numpify=_UpperCamelCase ) def snake_case__( self : Dict ) ->List[Any]: self._check_truncation(numpify=_UpperCamelCase ) def snake_case__( self : Tuple ) ->List[Any]: self._check_truncation(numpify=_UpperCamelCase ) @require_torch def snake_case__( self : Union[str, Any] ) ->Any: snake_case_ = self.feature_extraction_class(**self.feat_extract_dict ) snake_case_ = self.feat_extract_tester.prepare_inputs_for_common() snake_case_ = feat_extract.model_input_names[0] snake_case_ = BatchFeature({input_name: speech_inputs} ) snake_case_ = feat_extract.pad(_UpperCamelCase , padding='''longest''' , return_tensors='''np''' )[input_name] snake_case_ = feat_extract.pad(_UpperCamelCase , padding='''longest''' , return_tensors='''pt''' )[input_name] self.assertTrue(abs(input_np.astype(np.floataa ).sum() - input_pt.numpy().astype(np.floataa ).sum() ) < 1e-2 ) @require_tf def snake_case__( self : int ) ->Union[str, Any]: snake_case_ = self.feature_extraction_class(**self.feat_extract_dict ) snake_case_ = self.feat_extract_tester.prepare_inputs_for_common() snake_case_ = feat_extract.model_input_names[0] snake_case_ = BatchFeature({input_name: speech_inputs} ) snake_case_ = feat_extract.pad(_UpperCamelCase , padding='''longest''' , return_tensors='''np''' )[input_name] snake_case_ = feat_extract.pad(_UpperCamelCase , padding='''longest''' , return_tensors='''tf''' )[input_name] self.assertTrue(abs(input_np.astype(np.floataa ).sum() - input_tf.numpy().astype(np.floataa ).sum() ) < 1e-2 ) def snake_case__( self : Tuple ) ->Optional[int]: snake_case_ = self.feat_extract_dict snake_case_ = True snake_case_ = self.feature_extraction_class(**_UpperCamelCase ) snake_case_ = self.feat_extract_tester.prepare_inputs_for_common() snake_case_ = [len(_UpperCamelCase ) for x in speech_inputs] snake_case_ = feat_extract.model_input_names[0] snake_case_ = BatchFeature({input_name: speech_inputs} ) snake_case_ = feat_extract.pad(_UpperCamelCase , padding='''longest''' , return_tensors='''np''' ) self.assertIn('''attention_mask''' , _UpperCamelCase ) self.assertListEqual(list(processed.attention_mask.shape ) , list(processed[input_name].shape[:2] ) ) self.assertListEqual(processed.attention_mask.sum(-1 ).tolist() , _UpperCamelCase ) def snake_case__( self : List[str] ) ->Optional[int]: snake_case_ = self.feat_extract_dict snake_case_ = True snake_case_ = self.feature_extraction_class(**_UpperCamelCase ) snake_case_ = self.feat_extract_tester.prepare_inputs_for_common() snake_case_ = [len(_UpperCamelCase ) for x in speech_inputs] snake_case_ = feat_extract.model_input_names[0] snake_case_ = BatchFeature({input_name: speech_inputs} ) snake_case_ = min(_UpperCamelCase ) snake_case_ = feat_extract.pad( _UpperCamelCase , padding='''max_length''' , max_length=_UpperCamelCase , truncation=_UpperCamelCase , return_tensors='''np''' ) self.assertIn('''attention_mask''' , _UpperCamelCase ) self.assertListEqual( list(processed_pad.attention_mask.shape ) , [processed_pad[input_name].shape[0], max_length] ) self.assertListEqual( processed_pad.attention_mask[:, :max_length].sum(-1 ).tolist() , [max_length for x in speech_inputs] )
39
import argparse import dataclasses import json import logging import os import shutil from typing import List, Optional import datasets from accelerate import Accelerator from datasets import load_dataset from finetuning import finetune from tqdm.auto import tqdm import transformers from transformers import AutoConfig, set_seed from transformers.trainer_utils import IntervalStrategy lowerCAmelCase_ = logging.getLogger(__name__) lowerCAmelCase_ = '''pytorch_model.bin''' @dataclasses.dataclass class snake_case_ : '''simple docstring''' SCREAMING_SNAKE_CASE : str = dataclasses.field( metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models."} ) SCREAMING_SNAKE_CASE : Optional[str] = dataclasses.field( default=__A , metadata={"help": "Where do you want to store the pretrained models downloaded from huggingface.co."} , ) @dataclasses.dataclass class snake_case_ : '''simple docstring''' SCREAMING_SNAKE_CASE : str = dataclasses.field(metadata={"help": "A csv or a json file containing the training data."} ) SCREAMING_SNAKE_CASE : str = dataclasses.field(metadata={"help": "A csv or a json file containing the data to predict on."} ) SCREAMING_SNAKE_CASE : Optional[str] = dataclasses.field( default=__A , metadata={"help": "A csv or a json file containing the validation data."} ) SCREAMING_SNAKE_CASE : Optional[str] = dataclasses.field( default=__A , metadata={"help": "The name of the task to train on."} , ) SCREAMING_SNAKE_CASE : Optional[List[str]] = dataclasses.field( default=__A , metadata={"help": "The list of labels for the task."} ) @dataclasses.dataclass class snake_case_ : '''simple docstring''' SCREAMING_SNAKE_CASE : str = dataclasses.field( metadata={"help": "The output directory where the model predictions and checkpoints will be written."} ) SCREAMING_SNAKE_CASE : Optional[str] = dataclasses.field( default="accuracy" , metadata={"help": "The evaluation metric used for the task."} ) SCREAMING_SNAKE_CASE : Optional[str] = dataclasses.field( default="no" , metadata={ "help": "The evaluation strategy to adopt during training. Possible values are: [\"no\", \"step\", \"epoch]" } , ) SCREAMING_SNAKE_CASE : Optional[int] = dataclasses.field( default=10 , metadata={"help": "Number of evaluation calls with no improvement after which training will be stopped."} , ) SCREAMING_SNAKE_CASE : Optional[float] = dataclasses.field( default=0.0 , metadata={ "help": "How much the specified evaluation metric must improve to satisfy early stopping conditions." } , ) SCREAMING_SNAKE_CASE : Optional[bool] = dataclasses.field( default=__A , metadata={"help": "Whether to filter the pseudo-labeled data based on the confidence score."} , ) SCREAMING_SNAKE_CASE : Optional[bool] = dataclasses.field( default=__A , metadata={"help": "Whether to filter the pseudo-labeled data based on the validation performance."} , ) SCREAMING_SNAKE_CASE : Optional[bool] = dataclasses.field( default=__A , metadata={"help": "Whether to fine-tune on labeled data after pseudo training."} , ) SCREAMING_SNAKE_CASE : Optional[float] = dataclasses.field( default=0.0 , metadata={"help": "Confidence threshold for pseudo-labeled data filtering."} , ) SCREAMING_SNAKE_CASE : Optional[int] = dataclasses.field( default=100 , metadata={"help": "Number of evaluation calls with no improvement after which training will be stopped."} , ) SCREAMING_SNAKE_CASE : Optional[int] = dataclasses.field( default=__A , metadata={"help": "Random seed for initialization."} , ) def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): snake_case_ = datasets.concatenate_datasets([infer_input, infer_output] , axis=1 ) if args.do_filter_by_confidence: snake_case_ = dataset.filter(lambda SCREAMING_SNAKE_CASE__ : example["probability"] > args.confidence_threshold ) if args.do_filter_by_val_performance: assert eval_result >= 0.0 and eval_result <= 1.0 snake_case_ = int(eval_result * len(SCREAMING_SNAKE_CASE__ ) ) print(SCREAMING_SNAKE_CASE__ ) snake_case_ = dataset.sort('''probability''' , reverse=SCREAMING_SNAKE_CASE__ ) snake_case_ = dataset.select(range(SCREAMING_SNAKE_CASE__ ) ) snake_case_ = dataset.remove_columns(['''label''', '''probability'''] ) snake_case_ = dataset.rename_column('''prediction''' , '''label''' ) snake_case_ = dataset.map(lambda SCREAMING_SNAKE_CASE__ : {"label": idalabel[example["label"]]} ) snake_case_ = dataset.shuffle(seed=args.seed ) snake_case_ = os.path.join(SCREAMING_SNAKE_CASE__ , F'''train_pseudo.{args.data_file_extension}''' ) if args.data_file_extension == "csv": dataset.to_csv(SCREAMING_SNAKE_CASE__ , index=SCREAMING_SNAKE_CASE__ ) else: dataset.to_json(SCREAMING_SNAKE_CASE__ ) def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ): snake_case_ = Accelerator() # Make one log on every process with the configuration for debugging. logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''' , datefmt='''%m/%d/%Y %H:%M:%S''' , level=logging.INFO , ) logger.info(accelerator.state ) # Setup logging, we only want one process per machine to log things on the # screen. accelerator.is_local_main_process is only True for one process per # machine. logger.setLevel(logging.INFO if accelerator.is_local_main_process else logging.ERROR ) if accelerator.is_local_main_process: datasets.utils.logging.set_verbosity_warning() transformers.utils.logging.set_verbosity_info() else: datasets.utils.logging.set_verbosity_error() transformers.utils.logging.set_verbosity_error() snake_case_ = STModelArguments(model_name_or_path=SCREAMING_SNAKE_CASE__ ) snake_case_ = STDataArguments(train_file=SCREAMING_SNAKE_CASE__ , infer_file=SCREAMING_SNAKE_CASE__ ) snake_case_ = STTrainingArguments(output_dir=SCREAMING_SNAKE_CASE__ ) snake_case_ = argparse.Namespace() for arg_class in (model_args, data_args, training_args): for key, value in vars(SCREAMING_SNAKE_CASE__ ).items(): setattr(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) for key, value in kwargs.items(): if hasattr(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): setattr(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) # Sanity checks snake_case_ = {} snake_case_ = None # You need to provide the training data and the data to predict on assert args.train_file is not None assert args.infer_file is not None snake_case_ = args.train_file snake_case_ = args.infer_file if args.evaluation_strategy != IntervalStrategy.NO.value: assert args.eval_file is not None snake_case_ = args.eval_file for key in data_files: snake_case_ = data_files[key].split('''.''' )[-1] assert extension in ["csv", "json"], F'''`{key}_file` should be a csv or a json file.''' if args.data_file_extension is None: snake_case_ = extension else: assert extension == args.data_file_extension, F'''`{key}_file` should be a {args.data_file_extension} file`.''' assert ( args.eval_metric in datasets.list_metrics() ), F'''{args.eval_metric} not in the list of supported metrics {datasets.list_metrics()}.''' # If passed along, set the training seed now. if args.seed is not None: set_seed(args.seed ) logger.info('''Creating the initial data directory for self-training...''' ) snake_case_ = F'''{args.output_dir}/self-train_iter-{{}}'''.format snake_case_ = data_dir_format(0 ) if accelerator.is_main_process: if args.output_dir is not None: os.makedirs(args.output_dir , exist_ok=SCREAMING_SNAKE_CASE__ ) os.makedirs(SCREAMING_SNAKE_CASE__ , exist_ok=SCREAMING_SNAKE_CASE__ ) accelerator.wait_for_everyone() snake_case_ = None snake_case_ = None snake_case_ = 0 snake_case_ = False # Show the progress bar snake_case_ = tqdm(range(args.max_selftrain_iterations ) , disable=not accelerator.is_local_main_process ) # Self-train for iteration in range(0 , int(args.max_selftrain_iterations ) ): snake_case_ = data_dir_format(SCREAMING_SNAKE_CASE__ ) assert os.path.exists(SCREAMING_SNAKE_CASE__ ) # Stage 1: initial fine-tuning for iteration = 0 or pseudo-training for # iteration > 0 snake_case_ = os.path.join(SCREAMING_SNAKE_CASE__ , '''stage-1''' ) snake_case_ = { '''accelerator''': accelerator, '''model_name_or_path''': args.model_name_or_path, '''cache_dir''': args.cache_dir, '''do_train''': True, '''train_file''': data_files['''train'''] if iteration == 0 else data_files['''train_pseudo'''], '''do_eval''': True if args.eval_file is not None else False, '''eval_file''': data_files['''eval'''], '''do_predict''': True, '''infer_file''': data_files['''infer'''], '''task_name''': args.task_name, '''label_list''': args.label_list, '''output_dir''': current_output_dir, '''eval_metric''': args.eval_metric, '''evaluation_strategy''': args.evaluation_strategy, '''early_stopping_patience''': args.early_stopping_patience, '''early_stopping_threshold''': args.early_stopping_threshold, '''seed''': args.seed, } # Add additional training arguments for key, value in kwargs.items(): if key not in arguments_dict and not hasattr(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): arguments_dict.update({key: value} ) snake_case_ = os.path.join(SCREAMING_SNAKE_CASE__ , '''best-checkpoint''' , SCREAMING_SNAKE_CASE__ ) if os.path.exists(SCREAMING_SNAKE_CASE__ ): logger.info( '''Found existing model checkpoint at %s. Skipping self-training: iteration: %d, stage: 1.''' , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , ) else: logger.info('''***** Running self-training: iteration: %d, stage: 1 *****''' , SCREAMING_SNAKE_CASE__ ) finetune(**SCREAMING_SNAKE_CASE__ ) accelerator.wait_for_everyone() assert os.path.exists(SCREAMING_SNAKE_CASE__ ) logger.info('''Self-training job completed: iteration: %d, stage: 1.''' , SCREAMING_SNAKE_CASE__ ) if iteration > 0 and args.finetune_on_labeled_data: # Stage 2 (optional): fine-tuning on the original labeled data snake_case_ = os.path.join(SCREAMING_SNAKE_CASE__ , '''best-checkpoint''' ) snake_case_ = os.path.join(SCREAMING_SNAKE_CASE__ , '''stage-2''' ) # Update arguments_dict snake_case_ = model_path snake_case_ = data_files['''train'''] snake_case_ = current_output_dir snake_case_ = os.path.join(SCREAMING_SNAKE_CASE__ , '''best-checkpoint''' , SCREAMING_SNAKE_CASE__ ) if os.path.exists(SCREAMING_SNAKE_CASE__ ): logger.info( '''Found existing model checkpoint at %s. Skipping self-training: iteration: %d, stage: 2.''' , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , ) else: logger.info('''***** Running self-training: iteration: %d, stage: 2 *****''' , SCREAMING_SNAKE_CASE__ ) finetune(**SCREAMING_SNAKE_CASE__ ) accelerator.wait_for_everyone() assert os.path.exists(SCREAMING_SNAKE_CASE__ ) logger.info('''Self-training job completed: iteration: %d, stage: 2.''' , SCREAMING_SNAKE_CASE__ ) snake_case_ = iteration snake_case_ = data_dir_format(iteration + 1 ) snake_case_ = AutoConfig.from_pretrained(os.path.join(SCREAMING_SNAKE_CASE__ , '''best-checkpoint''' ) ) snake_case_ = config.idalabel snake_case_ = os.path.join(SCREAMING_SNAKE_CASE__ , '''eval_results_best-checkpoint.json''' ) snake_case_ = os.path.join(SCREAMING_SNAKE_CASE__ , '''test_results_best-checkpoint.json''' ) assert os.path.exists(SCREAMING_SNAKE_CASE__ ) with open(SCREAMING_SNAKE_CASE__ , '''r''' ) as f: snake_case_ = float(json.load(SCREAMING_SNAKE_CASE__ )[args.eval_metric] ) snake_case_ = os.path.join(SCREAMING_SNAKE_CASE__ , '''infer_output_best-checkpoint.csv''' ) assert os.path.exists(SCREAMING_SNAKE_CASE__ ) # Loading the dataset from local csv or json files. snake_case_ = load_dataset(args.data_file_extension , data_files={'''data''': data_files['''infer''']} )['''data'''] snake_case_ = load_dataset('''csv''' , data_files={'''data''': infer_output_file} )['''data'''] if accelerator.is_main_process: os.makedirs(SCREAMING_SNAKE_CASE__ , exist_ok=SCREAMING_SNAKE_CASE__ ) shutil.copy(SCREAMING_SNAKE_CASE__ , os.path.join(SCREAMING_SNAKE_CASE__ , F'''eval_results_iter-{iteration}.json''' ) ) if os.path.exists(SCREAMING_SNAKE_CASE__ ): shutil.copy(SCREAMING_SNAKE_CASE__ , os.path.join(SCREAMING_SNAKE_CASE__ , F'''test_results_iter-{iteration}.json''' ) ) create_pseudo_labeled_data(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) accelerator.wait_for_everyone() snake_case_ = os.path.join(SCREAMING_SNAKE_CASE__ , F'''train_pseudo.{args.data_file_extension}''' ) if args.evaluation_strategy != IntervalStrategy.NO.value: snake_case_ = eval_result if best_iteration is None: snake_case_ = new_iteration snake_case_ = new_eval_result else: if new_eval_result - best_eval_result > args.early_stopping_threshold: snake_case_ = new_iteration snake_case_ = new_eval_result snake_case_ = 0 else: if new_eval_result == best_eval_result: snake_case_ = new_iteration snake_case_ = new_eval_result early_stopping_patience_counter += 1 if early_stopping_patience_counter >= args.early_stopping_patience: snake_case_ = True progress_bar.update(1 ) if should_training_stop: break if best_iteration is not None: # Save the best iteration logger.info('''Best iteration: %d''' , SCREAMING_SNAKE_CASE__ ) logger.info('''Best evaluation result: %s = %f''' , args.eval_metric , SCREAMING_SNAKE_CASE__ ) accelerator.wait_for_everyone() if accelerator.is_main_process: shutil.copy( os.path.join(SCREAMING_SNAKE_CASE__ , F'''eval_results_iter-{iteration}.json''' ) , os.path.join(SCREAMING_SNAKE_CASE__ , '''eval_results_best-iteration.json''' ) , ) else: # Assume that the last iteration is the best logger.info('''Best iteration: %d''' , args.max_selftrain_iterations - 1 ) logger.info('''Best evaluation result: %s = %f''' , args.eval_metric , SCREAMING_SNAKE_CASE__ ) accelerator.wait_for_everyone() if accelerator.is_main_process: shutil.copy( os.path.join(SCREAMING_SNAKE_CASE__ , F'''eval_results_iter-{args.max_selftrain_iterations - 1}.json''' ) , os.path.join(SCREAMING_SNAKE_CASE__ , '''eval_results_best-iteration.json''' ) , )
39
1
from typing import List, Optional, Union import torch from ...models import UNetaDConditionModel, VQModel from ...pipelines import DiffusionPipeline from ...pipelines.pipeline_utils import ImagePipelineOutput from ...schedulers import DDPMScheduler from ...utils import ( is_accelerate_available, is_accelerate_version, logging, randn_tensor, replace_example_docstring, ) lowerCAmelCase_ = logging.get_logger(__name__) # pylint: disable=invalid-name lowerCAmelCase_ = ''' Examples: ```py >>> from diffusers import KandinskyV22Pipeline, KandinskyV22PriorPipeline >>> import torch >>> pipe_prior = KandinskyV22PriorPipeline.from_pretrained("kandinsky-community/kandinsky-2-2-prior") >>> pipe_prior.to("cuda") >>> prompt = "red cat, 4k photo" >>> out = pipe_prior(prompt) >>> image_emb = out.image_embeds >>> zero_image_emb = out.negative_image_embeds >>> pipe = KandinskyV22Pipeline.from_pretrained("kandinsky-community/kandinsky-2-2-decoder") >>> pipe.to("cuda") >>> image = pipe( ... image_embeds=image_emb, ... negative_image_embeds=zero_image_emb, ... height=768, ... width=768, ... num_inference_steps=50, ... ).images >>> image[0].save("cat.png") ``` ''' def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__=8 ): snake_case_ = height // scale_factor**2 if height % scale_factor**2 != 0: new_height += 1 snake_case_ = width // scale_factor**2 if width % scale_factor**2 != 0: new_width += 1 return new_height * scale_factor, new_width * scale_factor class snake_case_ ( __A ): '''simple docstring''' def __init__( self : Optional[Any] , _UpperCamelCase : UNetaDConditionModel , _UpperCamelCase : DDPMScheduler , _UpperCamelCase : VQModel , ) ->Any: super().__init__() self.register_modules( unet=_UpperCamelCase , scheduler=_UpperCamelCase , movq=_UpperCamelCase , ) snake_case_ = 2 ** (len(self.movq.config.block_out_channels ) - 1) def snake_case__( self : Optional[int] , _UpperCamelCase : str , _UpperCamelCase : List[Any] , _UpperCamelCase : int , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : str , _UpperCamelCase : Tuple ) ->Optional[Any]: if latents is None: snake_case_ = randn_tensor(_UpperCamelCase , generator=_UpperCamelCase , device=_UpperCamelCase , dtype=_UpperCamelCase ) else: if latents.shape != shape: raise ValueError(f'''Unexpected latents shape, got {latents.shape}, expected {shape}''' ) snake_case_ = latents.to(_UpperCamelCase ) snake_case_ = latents * scheduler.init_noise_sigma return latents def snake_case__( self : List[Any] , _UpperCamelCase : Union[str, Any]=0 ) ->Optional[int]: if is_accelerate_available(): from accelerate import cpu_offload else: raise ImportError('''Please install accelerate via `pip install accelerate`''' ) snake_case_ = torch.device(f'''cuda:{gpu_id}''' ) snake_case_ = [ self.unet, self.movq, ] for cpu_offloaded_model in models: if cpu_offloaded_model is not None: cpu_offload(_UpperCamelCase , _UpperCamelCase ) def snake_case__( self : str , _UpperCamelCase : List[str]=0 ) ->Dict: if is_accelerate_available() and is_accelerate_version('''>=''' , '''0.17.0.dev0''' ): from accelerate import cpu_offload_with_hook else: raise ImportError('''`enable_model_cpu_offload` requires `accelerate v0.17.0` or higher.''' ) snake_case_ = torch.device(f'''cuda:{gpu_id}''' ) if self.device.type != "cpu": self.to('''cpu''' , silence_dtype_warnings=_UpperCamelCase ) torch.cuda.empty_cache() # otherwise we don't see the memory savings (but they probably exist) snake_case_ = None for cpu_offloaded_model in [self.unet, self.movq]: snake_case_, snake_case_ = cpu_offload_with_hook(_UpperCamelCase , _UpperCamelCase , prev_module_hook=_UpperCamelCase ) # We'll offload the last model manually. snake_case_ = hook @property # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device def snake_case__( self : List[str] ) ->Union[str, Any]: if not hasattr(self.unet , '''_hf_hook''' ): return self.device for module in self.unet.modules(): if ( hasattr(_UpperCamelCase , '''_hf_hook''' ) and hasattr(module._hf_hook , '''execution_device''' ) and module._hf_hook.execution_device is not None ): return torch.device(module._hf_hook.execution_device ) return self.device @torch.no_grad() @replace_example_docstring(_UpperCamelCase ) def __call__( self : Any , _UpperCamelCase : Union[torch.FloatTensor, List[torch.FloatTensor]] , _UpperCamelCase : Union[torch.FloatTensor, List[torch.FloatTensor]] , _UpperCamelCase : int = 5_1_2 , _UpperCamelCase : int = 5_1_2 , _UpperCamelCase : int = 1_0_0 , _UpperCamelCase : float = 4.0 , _UpperCamelCase : int = 1 , _UpperCamelCase : Optional[Union[torch.Generator, List[torch.Generator]]] = None , _UpperCamelCase : Optional[torch.FloatTensor] = None , _UpperCamelCase : Optional[str] = "pil" , _UpperCamelCase : bool = True , ) ->Optional[Any]: snake_case_ = self._execution_device snake_case_ = guidance_scale > 1.0 if isinstance(_UpperCamelCase , _UpperCamelCase ): snake_case_ = torch.cat(_UpperCamelCase , dim=0 ) snake_case_ = image_embeds.shape[0] * num_images_per_prompt if isinstance(_UpperCamelCase , _UpperCamelCase ): snake_case_ = torch.cat(_UpperCamelCase , dim=0 ) if do_classifier_free_guidance: snake_case_ = image_embeds.repeat_interleave(_UpperCamelCase , dim=0 ) snake_case_ = negative_image_embeds.repeat_interleave(_UpperCamelCase , dim=0 ) snake_case_ = torch.cat([negative_image_embeds, image_embeds] , dim=0 ).to(dtype=self.unet.dtype , device=_UpperCamelCase ) self.scheduler.set_timesteps(_UpperCamelCase , device=_UpperCamelCase ) snake_case_ = self.scheduler.timesteps snake_case_ = self.unet.config.in_channels snake_case_, snake_case_ = downscale_height_and_width(_UpperCamelCase , _UpperCamelCase , self.movq_scale_factor ) # create initial latent snake_case_ = self.prepare_latents( (batch_size, num_channels_latents, height, width) , image_embeds.dtype , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , self.scheduler , ) for i, t in enumerate(self.progress_bar(_UpperCamelCase ) ): # expand the latents if we are doing classifier free guidance snake_case_ = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents snake_case_ = {'''image_embeds''': image_embeds} snake_case_ = self.unet( sample=_UpperCamelCase , timestep=_UpperCamelCase , encoder_hidden_states=_UpperCamelCase , added_cond_kwargs=_UpperCamelCase , return_dict=_UpperCamelCase , )[0] if do_classifier_free_guidance: snake_case_, snake_case_ = noise_pred.split(latents.shape[1] , dim=1 ) snake_case_, snake_case_ = noise_pred.chunk(2 ) snake_case_, snake_case_ = variance_pred.chunk(2 ) snake_case_ = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) snake_case_ = torch.cat([noise_pred, variance_pred_text] , dim=1 ) if not ( hasattr(self.scheduler.config , '''variance_type''' ) and self.scheduler.config.variance_type in ["learned", "learned_range"] ): snake_case_, snake_case_ = noise_pred.split(latents.shape[1] , dim=1 ) # compute the previous noisy sample x_t -> x_t-1 snake_case_ = self.scheduler.step( _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , generator=_UpperCamelCase , )[0] # post-processing snake_case_ = self.movq.decode(_UpperCamelCase , force_not_quantize=_UpperCamelCase )['''sample'''] if output_type not in ["pt", "np", "pil"]: raise ValueError(f'''Only the output types `pt`, `pil` and `np` are supported not output_type={output_type}''' ) if output_type in ["np", "pil"]: snake_case_ = image * 0.5 + 0.5 snake_case_ = image.clamp(0 , 1 ) snake_case_ = image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() if output_type == "pil": snake_case_ = self.numpy_to_pil(_UpperCamelCase ) if not return_dict: return (image,) return ImagePipelineOutput(images=_UpperCamelCase )
39
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 snake_case_ ( __A , __A , __A , unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = AltDiffusionPipeline SCREAMING_SNAKE_CASE : Dict = TEXT_TO_IMAGE_PARAMS SCREAMING_SNAKE_CASE : Dict = TEXT_TO_IMAGE_BATCH_PARAMS SCREAMING_SNAKE_CASE : Union[str, Any] = TEXT_TO_IMAGE_IMAGE_PARAMS SCREAMING_SNAKE_CASE : Dict = TEXT_TO_IMAGE_IMAGE_PARAMS def snake_case__( self : Dict ) ->int: torch.manual_seed(0 ) snake_case_ = UNetaDConditionModel( block_out_channels=(3_2, 6_4) , layers_per_block=2 , sample_size=3_2 , in_channels=4 , out_channels=4 , down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''') , up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''') , cross_attention_dim=3_2 , ) snake_case_ = DDIMScheduler( beta_start=0.00085 , beta_end=0.012 , beta_schedule='''scaled_linear''' , clip_sample=_UpperCamelCase , set_alpha_to_one=_UpperCamelCase , ) torch.manual_seed(0 ) snake_case_ = AutoencoderKL( block_out_channels=[3_2, 6_4] , 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 ) snake_case_ = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=3_2 , projection_dim=3_2 , intermediate_size=3_7 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=5_0_0_2 , ) snake_case_ = CLIPTextModel(_UpperCamelCase ) snake_case_ = XLMRobertaTokenizer.from_pretrained('''hf-internal-testing/tiny-xlm-roberta''' ) snake_case_ = 7_7 snake_case_ = { '''unet''': unet, '''scheduler''': scheduler, '''vae''': vae, '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''safety_checker''': None, '''feature_extractor''': None, } return components def snake_case__( self : str , _UpperCamelCase : Optional[int] , _UpperCamelCase : Dict=0 ) ->Any: if str(_UpperCamelCase ).startswith('''mps''' ): snake_case_ = torch.manual_seed(_UpperCamelCase ) else: snake_case_ = torch.Generator(device=_UpperCamelCase ).manual_seed(_UpperCamelCase ) snake_case_ = { '''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 snake_case__( self : Dict ) ->List[str]: super().test_attention_slicing_forward_pass(expected_max_diff=3e-3 ) def snake_case__( self : List[str] ) ->Any: super().test_inference_batch_single_identical(expected_max_diff=3e-3 ) def snake_case__( self : Dict ) ->Any: snake_case_ = '''cpu''' # ensure determinism for the device-dependent torch.Generator snake_case_ = self.get_dummy_components() torch.manual_seed(0 ) snake_case_ = RobertaSeriesConfig( hidden_size=3_2 , project_dim=3_2 , intermediate_size=3_7 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , vocab_size=5_0_0_2 , ) # TODO: remove after fixing the non-deterministic text encoder snake_case_ = RobertaSeriesModelWithTransformation(_UpperCamelCase ) snake_case_ = text_encoder snake_case_ = AltDiffusionPipeline(**_UpperCamelCase ) snake_case_ = alt_pipe.to(_UpperCamelCase ) alt_pipe.set_progress_bar_config(disable=_UpperCamelCase ) snake_case_ = self.get_dummy_inputs(_UpperCamelCase ) snake_case_ = '''A photo of an astronaut''' snake_case_ = alt_pipe(**_UpperCamelCase ) snake_case_ = output.images snake_case_ = image[0, -3:, -3:, -1] assert image.shape == (1, 6_4, 6_4, 3) snake_case_ = 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 snake_case__( self : Tuple ) ->Union[str, Any]: snake_case_ = '''cpu''' # ensure determinism for the device-dependent torch.Generator snake_case_ = self.get_dummy_components() snake_case_ = PNDMScheduler(skip_prk_steps=_UpperCamelCase ) torch.manual_seed(0 ) snake_case_ = RobertaSeriesConfig( hidden_size=3_2 , project_dim=3_2 , intermediate_size=3_7 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , vocab_size=5_0_0_2 , ) # TODO: remove after fixing the non-deterministic text encoder snake_case_ = RobertaSeriesModelWithTransformation(_UpperCamelCase ) snake_case_ = text_encoder snake_case_ = AltDiffusionPipeline(**_UpperCamelCase ) snake_case_ = alt_pipe.to(_UpperCamelCase ) alt_pipe.set_progress_bar_config(disable=_UpperCamelCase ) snake_case_ = self.get_dummy_inputs(_UpperCamelCase ) snake_case_ = alt_pipe(**_UpperCamelCase ) snake_case_ = output.images snake_case_ = image[0, -3:, -3:, -1] assert image.shape == (1, 6_4, 6_4, 3) snake_case_ = 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 snake_case_ ( unittest.TestCase ): '''simple docstring''' def snake_case__( self : int ) ->List[str]: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def snake_case__( self : List[str] ) ->Tuple: # make sure here that pndm scheduler skips prk snake_case_ = AltDiffusionPipeline.from_pretrained('''BAAI/AltDiffusion''' , safety_checker=_UpperCamelCase ) snake_case_ = alt_pipe.to(_UpperCamelCase ) alt_pipe.set_progress_bar_config(disable=_UpperCamelCase ) snake_case_ = '''A painting of a squirrel eating a burger''' snake_case_ = torch.manual_seed(0 ) snake_case_ = alt_pipe([prompt] , generator=_UpperCamelCase , guidance_scale=6.0 , num_inference_steps=2_0 , output_type='''np''' ) snake_case_ = output.images snake_case_ = image[0, -3:, -3:, -1] assert image.shape == (1, 5_1_2, 5_1_2, 3) snake_case_ = 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 snake_case__( self : List[str] ) ->Optional[Any]: snake_case_ = DDIMScheduler.from_pretrained('''BAAI/AltDiffusion''' , subfolder='''scheduler''' ) snake_case_ = AltDiffusionPipeline.from_pretrained('''BAAI/AltDiffusion''' , scheduler=_UpperCamelCase , safety_checker=_UpperCamelCase ) snake_case_ = alt_pipe.to(_UpperCamelCase ) alt_pipe.set_progress_bar_config(disable=_UpperCamelCase ) snake_case_ = '''A painting of a squirrel eating a burger''' snake_case_ = torch.manual_seed(0 ) snake_case_ = alt_pipe([prompt] , generator=_UpperCamelCase , num_inference_steps=2 , output_type='''numpy''' ) snake_case_ = output.images snake_case_ = image[0, -3:, -3:, -1] assert image.shape == (1, 5_1_2, 5_1_2, 3) snake_case_ = 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
39
1
import coval # From: git+https://github.com/ns-moosavi/coval.git # noqa: F401 from coval.conll import reader, util from coval.eval import evaluator import datasets lowerCAmelCase_ = datasets.logging.get_logger(__name__) lowerCAmelCase_ = '''\ @InProceedings{moosavi2019minimum, author = { Nafise Sadat Moosavi, Leo Born, Massimo Poesio and Michael Strube}, title = {Using Automatically Extracted Minimum Spans to Disentangle Coreference Evaluation from Boundary Detection}, year = {2019}, booktitle = {Proceedings of the 57th Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers)}, publisher = {Association for Computational Linguistics}, address = {Florence, Italy}, } @inproceedings{10.3115/1072399.1072405, author = {Vilain, Marc and Burger, John and Aberdeen, John and Connolly, Dennis and Hirschman, Lynette}, title = {A Model-Theoretic Coreference Scoring Scheme}, year = {1995}, isbn = {1558604022}, publisher = {Association for Computational Linguistics}, address = {USA}, url = {https://doi.org/10.3115/1072399.1072405}, doi = {10.3115/1072399.1072405}, booktitle = {Proceedings of the 6th Conference on Message Understanding}, pages = {45–52}, numpages = {8}, location = {Columbia, Maryland}, series = {MUC6 ’95} } @INPROCEEDINGS{Bagga98algorithmsfor, author = {Amit Bagga and Breck Baldwin}, title = {Algorithms for Scoring Coreference Chains}, booktitle = {In The First International Conference on Language Resources and Evaluation Workshop on Linguistics Coreference}, year = {1998}, pages = {563--566} } @INPROCEEDINGS{Luo05oncoreference, author = {Xiaoqiang Luo}, title = {On coreference resolution performance metrics}, booktitle = {In Proc. of HLT/EMNLP}, year = {2005}, pages = {25--32}, publisher = {URL} } @inproceedings{moosavi-strube-2016-coreference, title = "Which Coreference Evaluation Metric Do You Trust? A Proposal for a Link-based Entity Aware Metric", author = "Moosavi, Nafise Sadat and Strube, Michael", booktitle = "Proceedings of the 54th Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers)", month = aug, year = "2016", address = "Berlin, Germany", publisher = "Association for Computational Linguistics", url = "https://www.aclweb.org/anthology/P16-1060", doi = "10.18653/v1/P16-1060", pages = "632--642", } ''' lowerCAmelCase_ = '''\ CoVal is a coreference evaluation tool for the CoNLL and ARRAU datasets which implements of the common evaluation metrics including MUC [Vilain et al, 1995], B-cubed [Bagga and Baldwin, 1998], CEAFe [Luo et al., 2005], LEA [Moosavi and Strube, 2016] and the averaged CoNLL score (the average of the F1 values of MUC, B-cubed and CEAFe) [Denis and Baldridge, 2009a; Pradhan et al., 2011]. This wrapper of CoVal currently only work with CoNLL line format: The CoNLL format has one word per line with all the annotation for this word in column separated by spaces: Column Type Description 1 Document ID This is a variation on the document filename 2 Part number Some files are divided into multiple parts numbered as 000, 001, 002, ... etc. 3 Word number 4 Word itself This is the token as segmented/tokenized in the Treebank. Initially the *_skel file contain the placeholder [WORD] which gets replaced by the actual token from the Treebank which is part of the OntoNotes release. 5 Part-of-Speech 6 Parse bit This is the bracketed structure broken before the first open parenthesis in the parse, and the word/part-of-speech leaf replaced with a *. The full parse can be created by substituting the asterix with the "([pos] [word])" string (or leaf) and concatenating the items in the rows of that column. 7 Predicate lemma The predicate lemma is mentioned for the rows for which we have semantic role information. All other rows are marked with a "-" 8 Predicate Frameset ID This is the PropBank frameset ID of the predicate in Column 7. 9 Word sense This is the word sense of the word in Column 3. 10 Speaker/Author This is the speaker or author name where available. Mostly in Broadcast Conversation and Web Log data. 11 Named Entities These columns identifies the spans representing various named entities. 12:N Predicate Arguments There is one column each of predicate argument structure information for the predicate mentioned in Column 7. N Coreference Coreference chain information encoded in a parenthesis structure. More informations on the format can be found here (section "*_conll File Format"): http://www.conll.cemantix.org/2012/data.html Details on the evaluation on CoNLL can be found here: https://github.com/ns-moosavi/coval/blob/master/conll/README.md CoVal code was written by @ns-moosavi. Some parts are borrowed from https://github.com/clarkkev/deep-coref/blob/master/evaluation.py The test suite is taken from https://github.com/conll/reference-coreference-scorers/ Mention evaluation and the test suite are added by @andreasvc. Parsing CoNLL files is developed by Leo Born. ''' lowerCAmelCase_ = ''' Calculates coreference evaluation metrics. Args: predictions: list of sentences. Each sentence is a list of word predictions to score in the CoNLL format. Each prediction is a word with its annotations as a string made of columns joined with spaces. Only columns 4, 5, 6 and the last column are used (word, POS, Pars and coreference annotation) See the details on the format in the description of the metric. references: list of sentences. Each sentence is a list of word reference to score in the CoNLL format. Each reference is a word with its annotations as a string made of columns joined with spaces. Only columns 4, 5, 6 and the last column are used (word, POS, Pars and coreference annotation) See the details on the format in the description of the metric. keep_singletons: After extracting all mentions of key or system files, mentions whose corresponding coreference chain is of size one, are considered as singletons. The default evaluation mode will include singletons in evaluations if they are included in the key or the system files. By setting \'keep_singletons=False\', all singletons in the key and system files will be excluded from the evaluation. NP_only: Most of the recent coreference resolvers only resolve NP mentions and leave out the resolution of VPs. By setting the \'NP_only\' option, the scorer will only evaluate the resolution of NPs. min_span: By setting \'min_span\', the scorer reports the results based on automatically detected minimum spans. Minimum spans are determined using the MINA algorithm. Returns: \'mentions\': mentions \'muc\': MUC metric [Vilain et al, 1995] \'bcub\': B-cubed [Bagga and Baldwin, 1998] \'ceafe\': CEAFe [Luo et al., 2005] \'lea\': LEA [Moosavi and Strube, 2016] \'conll_score\': averaged CoNLL score (the average of the F1 values of MUC, B-cubed and CEAFe) Examples: >>> coval = datasets.load_metric(\'coval\') >>> words = [\'bc/cctv/00/cctv_0005 0 0 Thank VBP (TOP(S(VP* thank 01 1 Xu_li * (V*) * -\', ... \'bc/cctv/00/cctv_0005 0 1 you PRP (NP*) - - - Xu_li * (ARG1*) (ARG0*) (116)\', ... \'bc/cctv/00/cctv_0005 0 2 everyone NN (NP*) - - - Xu_li * (ARGM-DIS*) * (116)\', ... \'bc/cctv/00/cctv_0005 0 3 for IN (PP* - - - Xu_li * (ARG2* * -\', ... \'bc/cctv/00/cctv_0005 0 4 watching VBG (S(VP*)))) watch 01 1 Xu_li * *) (V*) -\', ... \'bc/cctv/00/cctv_0005 0 5 . . *)) - - - Xu_li * * * -\'] >>> references = [words] >>> predictions = [words] >>> results = coval.compute(predictions=predictions, references=references) >>> print(results) # doctest:+ELLIPSIS {\'mentions/recall\': 1.0,[...] \'conll_score\': 100.0} ''' def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__=False , SCREAMING_SNAKE_CASE__=False , SCREAMING_SNAKE_CASE__=True , SCREAMING_SNAKE_CASE__=False , SCREAMING_SNAKE_CASE__="dummy_doc" ): snake_case_ = {doc: key_lines} snake_case_ = {doc: sys_lines} snake_case_ = {} snake_case_ = 0 snake_case_ = 0 snake_case_ = 0 snake_case_ = 0 snake_case_ = 0 snake_case_ = 0 snake_case_, snake_case_ = reader.get_doc_mentions(SCREAMING_SNAKE_CASE__ , key_doc_lines[doc] , SCREAMING_SNAKE_CASE__ ) key_singletons_num += singletons_num if NP_only or min_span: snake_case_ = reader.set_annotated_parse_trees(SCREAMING_SNAKE_CASE__ , key_doc_lines[doc] , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) snake_case_, snake_case_ = reader.get_doc_mentions(SCREAMING_SNAKE_CASE__ , sys_doc_lines[doc] , SCREAMING_SNAKE_CASE__ ) sys_singletons_num += singletons_num if NP_only or min_span: snake_case_ = reader.set_annotated_parse_trees(SCREAMING_SNAKE_CASE__ , key_doc_lines[doc] , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) if remove_nested: snake_case_, snake_case_ = reader.remove_nested_coref_mentions(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) key_nested_coref_num += nested_mentions key_removed_nested_clusters += removed_clusters snake_case_, snake_case_ = reader.remove_nested_coref_mentions(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) sys_nested_coref_num += nested_mentions sys_removed_nested_clusters += removed_clusters snake_case_ = reader.get_mention_assignments(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) snake_case_ = reader.get_mention_assignments(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) snake_case_ = (key_clusters, sys_clusters, key_mention_sys_cluster, sys_mention_key_cluster) if remove_nested: logger.info( '''Number of removed nested coreferring mentions in the key ''' F'''annotation: {key_nested_coref_num}; and system annotation: {sys_nested_coref_num}''' ) logger.info( '''Number of resulting singleton clusters in the key ''' F'''annotation: {key_removed_nested_clusters}; and system annotation: {sys_removed_nested_clusters}''' ) if not keep_singletons: logger.info( F'''{key_singletons_num:d} and {sys_singletons_num:d} singletons are removed from the key and system ''' '''files, respectively''' ) return doc_coref_infos def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): snake_case_ = get_coref_infos(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) snake_case_ = {} snake_case_ = 0 snake_case_ = 0 for name, metric in metrics: snake_case_, snake_case_, snake_case_ = evaluator.evaluate_documents(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , beta=1 ) if name in ["muc", "bcub", "ceafe"]: conll += fa conll_subparts_num += 1 output_scores.update({F'''{name}/recall''': recall, F'''{name}/precision''': precision, F'''{name}/f1''': fa} ) logger.info( name.ljust(10 ) , F'''Recall: {recall * 100:.2f}''' , F''' Precision: {precision * 100:.2f}''' , F''' F1: {fa * 100:.2f}''' , ) if conll_subparts_num == 3: snake_case_ = (conll / 3) * 100 logger.info(F'''CoNLL score: {conll:.2f}''' ) output_scores.update({'''conll_score''': conll} ) return output_scores def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): snake_case_ = False for line in key_lines: if not line.startswith('''#''' ): if len(line.split() ) > 6: snake_case_ = line.split()[5] if not parse_col == "-": snake_case_ = True break else: break return has_gold_parse @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class snake_case_ ( datasets.Metric ): '''simple docstring''' def snake_case__( self : List[str] ) ->Optional[int]: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Sequence(datasets.Value('''string''' ) ), '''references''': datasets.Sequence(datasets.Value('''string''' ) ), } ) , codebase_urls=['''https://github.com/ns-moosavi/coval'''] , reference_urls=[ '''https://github.com/ns-moosavi/coval''', '''https://www.aclweb.org/anthology/P16-1060''', '''http://www.conll.cemantix.org/2012/data.html''', ] , ) def snake_case__( self : str , _UpperCamelCase : int , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : Optional[Any]=True , _UpperCamelCase : int=False , _UpperCamelCase : Optional[Any]=False , _UpperCamelCase : int=False ) ->Tuple: snake_case_ = [ ('''mentions''', evaluator.mentions), ('''muc''', evaluator.muc), ('''bcub''', evaluator.b_cubed), ('''ceafe''', evaluator.ceafe), ('''lea''', evaluator.lea), ] if min_span: snake_case_ = util.check_gold_parse_annotation(_UpperCamelCase ) if not has_gold_parse: raise NotImplementedError('''References should have gold parse annotation to use \'min_span\'.''' ) # util.parse_key_file(key_file) # key_file = key_file + ".parsed" snake_case_ = evaluate( key_lines=_UpperCamelCase , sys_lines=_UpperCamelCase , metrics=_UpperCamelCase , NP_only=_UpperCamelCase , remove_nested=_UpperCamelCase , keep_singletons=_UpperCamelCase , min_span=_UpperCamelCase , ) return score
39
from math import factorial def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): # If either of the conditions are true, the function is being asked # to calculate a factorial of a negative number, which is not possible if n < k or k < 0: raise ValueError('''Please enter positive integers for n and k where n >= k''' ) return factorial(SCREAMING_SNAKE_CASE__ ) // (factorial(SCREAMING_SNAKE_CASE__ ) * factorial(n - k )) if __name__ == "__main__": print( '''The number of five-card hands possible from a standard''', f"""fifty-two card deck is: {combinations(52, 5)}\n""", ) print( '''If a class of 40 students must be arranged into groups of''', f"""4 for group projects, there are {combinations(40, 4)} ways""", '''to arrange them.\n''', ) print( '''If 10 teams are competing in a Formula One race, there''', f"""are {combinations(10, 3)} ways that first, second and""", '''third place can be awarded.''', )
39
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCAmelCase_ = {'''configuration_sew''': ['''SEW_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''SEWConfig''']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ = [ '''SEW_PRETRAINED_MODEL_ARCHIVE_LIST''', '''SEWForCTC''', '''SEWForSequenceClassification''', '''SEWModel''', '''SEWPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_sew import SEW_PRETRAINED_CONFIG_ARCHIVE_MAP, SEWConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_sew import ( SEW_PRETRAINED_MODEL_ARCHIVE_LIST, SEWForCTC, SEWForSequenceClassification, SEWModel, SEWPreTrainedModel, ) else: import sys lowerCAmelCase_ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
39
import argparse import json import os import sys import tempfile import unittest from argparse import Namespace from dataclasses import dataclass, field from enum import Enum from pathlib import Path from typing import List, Literal, Optional import yaml from transformers import HfArgumentParser, TrainingArguments from transformers.hf_argparser import make_choice_type_function, string_to_bool # Since Python 3.10, we can use the builtin `|` operator for Union types # See PEP 604: https://peps.python.org/pep-0604 lowerCAmelCase_ = sys.version_info >= (3, 10) def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__=None , SCREAMING_SNAKE_CASE__=None ): return field(default_factory=lambda: default , metadata=SCREAMING_SNAKE_CASE__ ) @dataclass class snake_case_ : '''simple docstring''' SCREAMING_SNAKE_CASE : int SCREAMING_SNAKE_CASE : float SCREAMING_SNAKE_CASE : str SCREAMING_SNAKE_CASE : bool @dataclass class snake_case_ : '''simple docstring''' SCREAMING_SNAKE_CASE : int = 42 SCREAMING_SNAKE_CASE : str = field(default="toto" , metadata={"help": "help message"} ) @dataclass class snake_case_ : '''simple docstring''' SCREAMING_SNAKE_CASE : bool = False SCREAMING_SNAKE_CASE : bool = True SCREAMING_SNAKE_CASE : Optional[bool] = None class snake_case_ ( __A ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = "titi" SCREAMING_SNAKE_CASE : Any = "toto" class snake_case_ ( __A ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = "titi" SCREAMING_SNAKE_CASE : Optional[Any] = "toto" SCREAMING_SNAKE_CASE : Any = 42 @dataclass class snake_case_ : '''simple docstring''' SCREAMING_SNAKE_CASE : BasicEnum = "toto" def snake_case__( self : Tuple ) ->List[str]: snake_case_ = BasicEnum(self.foo ) @dataclass class snake_case_ : '''simple docstring''' SCREAMING_SNAKE_CASE : MixedTypeEnum = "toto" def snake_case__( self : Union[str, Any] ) ->Dict: snake_case_ = MixedTypeEnum(self.foo ) @dataclass class snake_case_ : '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = None SCREAMING_SNAKE_CASE : Optional[float] = field(default=__A , metadata={"help": "help message"} ) SCREAMING_SNAKE_CASE : Optional[str] = None SCREAMING_SNAKE_CASE : Optional[List[str]] = list_field(default=[] ) SCREAMING_SNAKE_CASE : Optional[List[int]] = list_field(default=[] ) @dataclass class snake_case_ : '''simple docstring''' SCREAMING_SNAKE_CASE : List[int] = list_field(default=[] ) SCREAMING_SNAKE_CASE : List[int] = list_field(default=[1, 2, 3] ) SCREAMING_SNAKE_CASE : List[str] = list_field(default=["Hallo", "Bonjour", "Hello"] ) SCREAMING_SNAKE_CASE : List[float] = list_field(default=[0.1, 0.2, 0.3] ) @dataclass class snake_case_ : '''simple docstring''' SCREAMING_SNAKE_CASE : List[int] = field() SCREAMING_SNAKE_CASE : str = field() SCREAMING_SNAKE_CASE : BasicEnum = field() def snake_case__( self : Optional[Any] ) ->Tuple: snake_case_ = BasicEnum(self.required_enum ) @dataclass class snake_case_ : '''simple docstring''' SCREAMING_SNAKE_CASE : int SCREAMING_SNAKE_CASE : "BasicEnum" = field() SCREAMING_SNAKE_CASE : "Optional[bool]" = None SCREAMING_SNAKE_CASE : "str" = field(default="toto" , metadata={"help": "help message"} ) SCREAMING_SNAKE_CASE : "List[str]" = list_field(default=["Hallo", "Bonjour", "Hello"] ) if is_python_no_less_than_3_10: @dataclass class snake_case_ : '''simple docstring''' SCREAMING_SNAKE_CASE : bool = False SCREAMING_SNAKE_CASE : bool = True SCREAMING_SNAKE_CASE : bool | None = None @dataclass class snake_case_ : '''simple docstring''' SCREAMING_SNAKE_CASE : int | None = None SCREAMING_SNAKE_CASE : float | None = field(default=__A , metadata={"help": "help message"} ) SCREAMING_SNAKE_CASE : str | None = None SCREAMING_SNAKE_CASE : list[str] | None = list_field(default=[] ) SCREAMING_SNAKE_CASE : list[int] | None = list_field(default=[] ) class snake_case_ ( unittest.TestCase ): '''simple docstring''' def snake_case__( self : Dict , _UpperCamelCase : argparse.ArgumentParser , _UpperCamelCase : argparse.ArgumentParser ) ->str: self.assertEqual(len(a._actions ) , len(b._actions ) ) for x, y in zip(a._actions , b._actions ): snake_case_ = {k: v for k, v in vars(_UpperCamelCase ).items() if k != '''container'''} snake_case_ = {k: v for k, v in vars(_UpperCamelCase ).items() if k != '''container'''} # Choices with mixed type have custom function as "type" # So we need to compare results directly for equality if xx.get('''choices''' , _UpperCamelCase ) and yy.get('''choices''' , _UpperCamelCase ): for expected_choice in yy["choices"] + xx["choices"]: self.assertEqual(xx['''type'''](_UpperCamelCase ) , yy['''type'''](_UpperCamelCase ) ) del xx["type"], yy["type"] self.assertEqual(_UpperCamelCase , _UpperCamelCase ) def snake_case__( self : Optional[Any] ) ->Dict: snake_case_ = HfArgumentParser(_UpperCamelCase ) snake_case_ = argparse.ArgumentParser() expected.add_argument('''--foo''' , type=_UpperCamelCase , required=_UpperCamelCase ) expected.add_argument('''--bar''' , type=_UpperCamelCase , required=_UpperCamelCase ) expected.add_argument('''--baz''' , type=_UpperCamelCase , required=_UpperCamelCase ) expected.add_argument('''--flag''' , type=_UpperCamelCase , default=_UpperCamelCase , const=_UpperCamelCase , nargs='''?''' ) self.argparsersEqual(_UpperCamelCase , _UpperCamelCase ) snake_case_ = ['''--foo''', '''1''', '''--baz''', '''quux''', '''--bar''', '''0.5'''] ((snake_case_), ) = parser.parse_args_into_dataclasses(_UpperCamelCase , look_for_args_file=_UpperCamelCase ) self.assertFalse(example.flag ) def snake_case__( self : Tuple ) ->Optional[int]: snake_case_ = HfArgumentParser(_UpperCamelCase ) snake_case_ = argparse.ArgumentParser() expected.add_argument('''--foo''' , default=4_2 , type=_UpperCamelCase ) expected.add_argument('''--baz''' , default='''toto''' , type=_UpperCamelCase , help='''help message''' ) self.argparsersEqual(_UpperCamelCase , _UpperCamelCase ) def snake_case__( self : Tuple ) ->Tuple: snake_case_ = argparse.ArgumentParser() expected.add_argument('''--foo''' , type=_UpperCamelCase , default=_UpperCamelCase , const=_UpperCamelCase , nargs='''?''' ) expected.add_argument('''--baz''' , type=_UpperCamelCase , default=_UpperCamelCase , const=_UpperCamelCase , nargs='''?''' ) # A boolean no_* argument always has to come after its "default: True" regular counter-part # and its default must be set to False expected.add_argument('''--no_baz''' , action='''store_false''' , default=_UpperCamelCase , dest='''baz''' ) expected.add_argument('''--opt''' , type=_UpperCamelCase , default=_UpperCamelCase ) snake_case_ = [WithDefaultBoolExample] if is_python_no_less_than_3_10: dataclass_types.append(_UpperCamelCase ) for dataclass_type in dataclass_types: snake_case_ = HfArgumentParser(_UpperCamelCase ) self.argparsersEqual(_UpperCamelCase , _UpperCamelCase ) snake_case_ = parser.parse_args([] ) self.assertEqual(_UpperCamelCase , Namespace(foo=_UpperCamelCase , baz=_UpperCamelCase , opt=_UpperCamelCase ) ) snake_case_ = parser.parse_args(['''--foo''', '''--no_baz'''] ) self.assertEqual(_UpperCamelCase , Namespace(foo=_UpperCamelCase , baz=_UpperCamelCase , opt=_UpperCamelCase ) ) snake_case_ = parser.parse_args(['''--foo''', '''--baz'''] ) self.assertEqual(_UpperCamelCase , Namespace(foo=_UpperCamelCase , baz=_UpperCamelCase , opt=_UpperCamelCase ) ) snake_case_ = parser.parse_args(['''--foo''', '''True''', '''--baz''', '''True''', '''--opt''', '''True'''] ) self.assertEqual(_UpperCamelCase , Namespace(foo=_UpperCamelCase , baz=_UpperCamelCase , opt=_UpperCamelCase ) ) snake_case_ = parser.parse_args(['''--foo''', '''False''', '''--baz''', '''False''', '''--opt''', '''False'''] ) self.assertEqual(_UpperCamelCase , Namespace(foo=_UpperCamelCase , baz=_UpperCamelCase , opt=_UpperCamelCase ) ) def snake_case__( self : Tuple ) ->Tuple: snake_case_ = HfArgumentParser(_UpperCamelCase ) snake_case_ = argparse.ArgumentParser() expected.add_argument( '''--foo''' , default='''toto''' , choices=['''titi''', '''toto''', 4_2] , type=make_choice_type_function(['''titi''', '''toto''', 4_2] ) , ) self.argparsersEqual(_UpperCamelCase , _UpperCamelCase ) snake_case_ = parser.parse_args([] ) self.assertEqual(args.foo , '''toto''' ) snake_case_ = parser.parse_args_into_dataclasses([] )[0] self.assertEqual(enum_ex.foo , MixedTypeEnum.toto ) snake_case_ = parser.parse_args(['''--foo''', '''titi'''] ) self.assertEqual(args.foo , '''titi''' ) snake_case_ = parser.parse_args_into_dataclasses(['''--foo''', '''titi'''] )[0] self.assertEqual(enum_ex.foo , MixedTypeEnum.titi ) snake_case_ = parser.parse_args(['''--foo''', '''42'''] ) self.assertEqual(args.foo , 4_2 ) snake_case_ = parser.parse_args_into_dataclasses(['''--foo''', '''42'''] )[0] self.assertEqual(enum_ex.foo , MixedTypeEnum.fourtytwo ) def snake_case__( self : Tuple ) ->Union[str, Any]: @dataclass class snake_case_ : '''simple docstring''' SCREAMING_SNAKE_CASE : Literal["titi", "toto", 42] = "toto" snake_case_ = HfArgumentParser(_UpperCamelCase ) snake_case_ = argparse.ArgumentParser() expected.add_argument( '''--foo''' , default='''toto''' , choices=('''titi''', '''toto''', 4_2) , type=make_choice_type_function(['''titi''', '''toto''', 4_2] ) , ) self.argparsersEqual(_UpperCamelCase , _UpperCamelCase ) snake_case_ = parser.parse_args([] ) self.assertEqual(args.foo , '''toto''' ) snake_case_ = parser.parse_args(['''--foo''', '''titi'''] ) self.assertEqual(args.foo , '''titi''' ) snake_case_ = parser.parse_args(['''--foo''', '''42'''] ) self.assertEqual(args.foo , 4_2 ) def snake_case__( self : List[str] ) ->int: snake_case_ = HfArgumentParser(_UpperCamelCase ) snake_case_ = argparse.ArgumentParser() expected.add_argument('''--foo_int''' , nargs='''+''' , default=[] , type=_UpperCamelCase ) expected.add_argument('''--bar_int''' , nargs='''+''' , default=[1, 2, 3] , type=_UpperCamelCase ) expected.add_argument('''--foo_str''' , nargs='''+''' , default=['''Hallo''', '''Bonjour''', '''Hello'''] , type=_UpperCamelCase ) expected.add_argument('''--foo_float''' , nargs='''+''' , default=[0.1, 0.2, 0.3] , type=_UpperCamelCase ) self.argparsersEqual(_UpperCamelCase , _UpperCamelCase ) snake_case_ = parser.parse_args([] ) self.assertEqual( _UpperCamelCase , Namespace(foo_int=[] , bar_int=[1, 2, 3] , foo_str=['''Hallo''', '''Bonjour''', '''Hello'''] , foo_float=[0.1, 0.2, 0.3] ) , ) snake_case_ = parser.parse_args('''--foo_int 1 --bar_int 2 3 --foo_str a b c --foo_float 0.1 0.7'''.split() ) self.assertEqual(_UpperCamelCase , Namespace(foo_int=[1] , bar_int=[2, 3] , foo_str=['''a''', '''b''', '''c'''] , foo_float=[0.1, 0.7] ) ) def snake_case__( self : Optional[Any] ) ->List[Any]: snake_case_ = argparse.ArgumentParser() expected.add_argument('''--foo''' , default=_UpperCamelCase , type=_UpperCamelCase ) expected.add_argument('''--bar''' , default=_UpperCamelCase , type=_UpperCamelCase , help='''help message''' ) expected.add_argument('''--baz''' , default=_UpperCamelCase , type=_UpperCamelCase ) expected.add_argument('''--ces''' , nargs='''+''' , default=[] , type=_UpperCamelCase ) expected.add_argument('''--des''' , nargs='''+''' , default=[] , type=_UpperCamelCase ) snake_case_ = [OptionalExample] if is_python_no_less_than_3_10: dataclass_types.append(_UpperCamelCase ) for dataclass_type in dataclass_types: snake_case_ = HfArgumentParser(_UpperCamelCase ) self.argparsersEqual(_UpperCamelCase , _UpperCamelCase ) snake_case_ = parser.parse_args([] ) self.assertEqual(_UpperCamelCase , Namespace(foo=_UpperCamelCase , bar=_UpperCamelCase , baz=_UpperCamelCase , ces=[] , des=[] ) ) snake_case_ = parser.parse_args('''--foo 12 --bar 3.14 --baz 42 --ces a b c --des 1 2 3'''.split() ) self.assertEqual(_UpperCamelCase , Namespace(foo=1_2 , bar=3.14 , baz='''42''' , ces=['''a''', '''b''', '''c'''] , des=[1, 2, 3] ) ) def snake_case__( self : Union[str, Any] ) ->Optional[int]: snake_case_ = HfArgumentParser(_UpperCamelCase ) snake_case_ = argparse.ArgumentParser() expected.add_argument('''--required_list''' , nargs='''+''' , type=_UpperCamelCase , required=_UpperCamelCase ) expected.add_argument('''--required_str''' , type=_UpperCamelCase , required=_UpperCamelCase ) expected.add_argument( '''--required_enum''' , type=make_choice_type_function(['''titi''', '''toto'''] ) , choices=['''titi''', '''toto'''] , required=_UpperCamelCase , ) self.argparsersEqual(_UpperCamelCase , _UpperCamelCase ) def snake_case__( self : List[str] ) ->int: snake_case_ = HfArgumentParser(_UpperCamelCase ) snake_case_ = argparse.ArgumentParser() expected.add_argument('''--foo''' , type=_UpperCamelCase , required=_UpperCamelCase ) expected.add_argument( '''--required_enum''' , type=make_choice_type_function(['''titi''', '''toto'''] ) , choices=['''titi''', '''toto'''] , required=_UpperCamelCase , ) expected.add_argument('''--opt''' , type=_UpperCamelCase , default=_UpperCamelCase ) expected.add_argument('''--baz''' , default='''toto''' , type=_UpperCamelCase , help='''help message''' ) expected.add_argument('''--foo_str''' , nargs='''+''' , default=['''Hallo''', '''Bonjour''', '''Hello'''] , type=_UpperCamelCase ) self.argparsersEqual(_UpperCamelCase , _UpperCamelCase ) def snake_case__( self : Dict ) ->Any: snake_case_ = HfArgumentParser(_UpperCamelCase ) snake_case_ = { '''foo''': 1_2, '''bar''': 3.14, '''baz''': '''42''', '''flag''': True, } snake_case_ = parser.parse_dict(_UpperCamelCase )[0] snake_case_ = BasicExample(**_UpperCamelCase ) self.assertEqual(_UpperCamelCase , _UpperCamelCase ) def snake_case__( self : int ) ->Dict: snake_case_ = HfArgumentParser(_UpperCamelCase ) snake_case_ = { '''foo''': 1_2, '''bar''': 3.14, '''baz''': '''42''', '''flag''': True, '''extra''': 4_2, } self.assertRaises(_UpperCamelCase , parser.parse_dict , _UpperCamelCase , allow_extra_keys=_UpperCamelCase ) def snake_case__( self : str ) ->Tuple: snake_case_ = HfArgumentParser(_UpperCamelCase ) snake_case_ = { '''foo''': 1_2, '''bar''': 3.14, '''baz''': '''42''', '''flag''': True, } with tempfile.TemporaryDirectory() as tmp_dir: snake_case_ = os.path.join(_UpperCamelCase , '''temp_json''' ) os.mkdir(_UpperCamelCase ) with open(temp_local_path + '''.json''' , '''w+''' ) as f: json.dump(_UpperCamelCase , _UpperCamelCase ) snake_case_ = parser.parse_yaml_file(Path(temp_local_path + '''.json''' ) )[0] snake_case_ = BasicExample(**_UpperCamelCase ) self.assertEqual(_UpperCamelCase , _UpperCamelCase ) def snake_case__( self : Optional[int] ) ->str: snake_case_ = HfArgumentParser(_UpperCamelCase ) snake_case_ = { '''foo''': 1_2, '''bar''': 3.14, '''baz''': '''42''', '''flag''': True, } with tempfile.TemporaryDirectory() as tmp_dir: snake_case_ = os.path.join(_UpperCamelCase , '''temp_yaml''' ) os.mkdir(_UpperCamelCase ) with open(temp_local_path + '''.yaml''' , '''w+''' ) as f: yaml.dump(_UpperCamelCase , _UpperCamelCase ) snake_case_ = parser.parse_yaml_file(Path(temp_local_path + '''.yaml''' ) )[0] snake_case_ = BasicExample(**_UpperCamelCase ) self.assertEqual(_UpperCamelCase , _UpperCamelCase ) def snake_case__( self : Any ) ->Any: snake_case_ = HfArgumentParser(_UpperCamelCase ) self.assertIsNotNone(_UpperCamelCase )
39
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) lowerCAmelCase_ = {'''configuration_mbart''': ['''MBART_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''MBartConfig''', '''MBartOnnxConfig''']} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ = ['''MBartTokenizer'''] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ = ['''MBartTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ = [ '''MBART_PRETRAINED_MODEL_ARCHIVE_LIST''', '''MBartForCausalLM''', '''MBartForConditionalGeneration''', '''MBartForQuestionAnswering''', '''MBartForSequenceClassification''', '''MBartModel''', '''MBartPreTrainedModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ = [ '''TFMBartForConditionalGeneration''', '''TFMBartModel''', '''TFMBartPreTrainedModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ = [ '''FlaxMBartForConditionalGeneration''', '''FlaxMBartForQuestionAnswering''', '''FlaxMBartForSequenceClassification''', '''FlaxMBartModel''', '''FlaxMBartPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_mbart import MBART_PRETRAINED_CONFIG_ARCHIVE_MAP, MBartConfig, MBartOnnxConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_mbart import MBartTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_mbart_fast import MBartTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mbart import ( MBART_PRETRAINED_MODEL_ARCHIVE_LIST, MBartForCausalLM, MBartForConditionalGeneration, MBartForQuestionAnswering, MBartForSequenceClassification, MBartModel, MBartPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_mbart import TFMBartForConditionalGeneration, TFMBartModel, TFMBartPreTrainedModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_mbart import ( FlaxMBartForConditionalGeneration, FlaxMBartForQuestionAnswering, FlaxMBartForSequenceClassification, FlaxMBartModel, FlaxMBartPreTrainedModel, ) else: import sys lowerCAmelCase_ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
39
import warnings from ...utils import logging from .image_processing_chinese_clip import ChineseCLIPImageProcessor lowerCAmelCase_ = logging.get_logger(__name__) class snake_case_ ( __A ): '''simple docstring''' def __init__( self : Dict , *_UpperCamelCase : int , **_UpperCamelCase : Tuple ) ->None: warnings.warn( '''The class ChineseCLIPFeatureExtractor is deprecated and will be removed in version 5 of Transformers.''' ''' Please use ChineseCLIPImageProcessor instead.''' , _UpperCamelCase , ) super().__init__(*_UpperCamelCase , **_UpperCamelCase )
39
1
import requests from bsa import BeautifulSoup def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ = "https://www.worldometers.info/coronavirus" ): snake_case_ = BeautifulSoup(requests.get(SCREAMING_SNAKE_CASE__ ).text , '''html.parser''' ) snake_case_ = soup.findAll('''h1''' ) snake_case_ = soup.findAll('''div''' , {'''class''': '''maincounter-number'''} ) keys += soup.findAll('''span''' , {'''class''': '''panel-title'''} ) values += soup.findAll('''div''' , {'''class''': '''number-table-main'''} ) return {key.text.strip(): value.text.strip() for key, value in zip(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ )} if __name__ == "__main__": print('''\033[1m''' + '''COVID-19 Status of the World''' + '''\033[0m\n''') for key, value in world_covidaa_stats().items(): print(f"""{key}\n{value}\n""")
39
from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase_ = logging.get_logger(__name__) lowerCAmelCase_ = { '''RWKV/rwkv-4-169m-pile''': '''https://huggingface.co/RWKV/rwkv-4-169m-pile/resolve/main/config.json''', '''RWKV/rwkv-4-430m-pile''': '''https://huggingface.co/RWKV/rwkv-4-430m-pile/resolve/main/config.json''', '''RWKV/rwkv-4-1b5-pile''': '''https://huggingface.co/RWKV/rwkv-4-1b5-pile/resolve/main/config.json''', '''RWKV/rwkv-4-3b-pile''': '''https://huggingface.co/RWKV/rwkv-4-3b-pile/resolve/main/config.json''', '''RWKV/rwkv-4-7b-pile''': '''https://huggingface.co/RWKV/rwkv-4-7b-pile/resolve/main/config.json''', '''RWKV/rwkv-4-14b-pile''': '''https://huggingface.co/RWKV/rwkv-4-14b-pile/resolve/main/config.json''', '''RWKV/rwkv-raven-1b5''': '''https://huggingface.co/RWKV/rwkv-raven-1b5/resolve/main/config.json''', '''RWKV/rwkv-raven-3b''': '''https://huggingface.co/RWKV/rwkv-raven-3b/resolve/main/config.json''', '''RWKV/rwkv-raven-7b''': '''https://huggingface.co/RWKV/rwkv-raven-7b/resolve/main/config.json''', '''RWKV/rwkv-raven-14b''': '''https://huggingface.co/RWKV/rwkv-raven-14b/resolve/main/config.json''', } class snake_case_ ( __A ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = "rwkv" SCREAMING_SNAKE_CASE : Any = {"max_position_embeddings": "context_length"} def __init__( self : Union[str, Any] , _UpperCamelCase : Any=5_0_2_7_7 , _UpperCamelCase : Optional[int]=1_0_2_4 , _UpperCamelCase : Optional[int]=4_0_9_6 , _UpperCamelCase : str=3_2 , _UpperCamelCase : Tuple=None , _UpperCamelCase : Dict=None , _UpperCamelCase : Optional[int]=1e-5 , _UpperCamelCase : Any=0 , _UpperCamelCase : Optional[Any]=0 , _UpperCamelCase : int=6 , _UpperCamelCase : Dict=False , _UpperCamelCase : Optional[int]=True , **_UpperCamelCase : int , ) ->List[str]: snake_case_ = vocab_size snake_case_ = context_length snake_case_ = hidden_size snake_case_ = num_hidden_layers snake_case_ = attention_hidden_size if attention_hidden_size is not None else hidden_size snake_case_ = intermediate_size if intermediate_size is not None else 4 * hidden_size snake_case_ = layer_norm_epsilon snake_case_ = rescale_every snake_case_ = use_cache snake_case_ = bos_token_id snake_case_ = eos_token_id super().__init__( tie_word_embeddings=_UpperCamelCase , bos_token_id=_UpperCamelCase , eos_token_id=_UpperCamelCase , **_UpperCamelCase )
39
1
import argparse import collections import os import re import tempfile import pandas as pd from datasets import Dataset from huggingface_hub import hf_hub_download, upload_folder from transformers.utils import direct_transformers_import # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/update_metadata.py lowerCAmelCase_ = '''src/transformers''' # This is to make sure the transformers module imported is the one in the repo. lowerCAmelCase_ = direct_transformers_import(TRANSFORMERS_PATH) # Regexes that match TF/Flax/PT model names. lowerCAmelCase_ = re.compile(R'''TF(.*)(?:Model|Encoder|Decoder|ForConditionalGeneration)''') lowerCAmelCase_ = re.compile(R'''Flax(.*)(?:Model|Encoder|Decoder|ForConditionalGeneration)''') # Will match any TF or Flax model too so need to be in an else branch afterthe two previous regexes. lowerCAmelCase_ = re.compile(R'''(.*)(?:Model|Encoder|Decoder|ForConditionalGeneration)''') # Fill this with tuples (pipeline_tag, model_mapping, auto_model) lowerCAmelCase_ = [ ('''pretraining''', '''MODEL_FOR_PRETRAINING_MAPPING_NAMES''', '''AutoModelForPreTraining'''), ('''feature-extraction''', '''MODEL_MAPPING_NAMES''', '''AutoModel'''), ('''audio-classification''', '''MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES''', '''AutoModelForAudioClassification'''), ('''text-generation''', '''MODEL_FOR_CAUSAL_LM_MAPPING_NAMES''', '''AutoModelForCausalLM'''), ('''automatic-speech-recognition''', '''MODEL_FOR_CTC_MAPPING_NAMES''', '''AutoModelForCTC'''), ('''image-classification''', '''MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES''', '''AutoModelForImageClassification'''), ('''image-segmentation''', '''MODEL_FOR_IMAGE_SEGMENTATION_MAPPING_NAMES''', '''AutoModelForImageSegmentation'''), ('''fill-mask''', '''MODEL_FOR_MASKED_LM_MAPPING_NAMES''', '''AutoModelForMaskedLM'''), ('''object-detection''', '''MODEL_FOR_OBJECT_DETECTION_MAPPING_NAMES''', '''AutoModelForObjectDetection'''), ( '''zero-shot-object-detection''', '''MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING_NAMES''', '''AutoModelForZeroShotObjectDetection''', ), ('''question-answering''', '''MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES''', '''AutoModelForQuestionAnswering'''), ('''text2text-generation''', '''MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES''', '''AutoModelForSeq2SeqLM'''), ('''text-classification''', '''MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES''', '''AutoModelForSequenceClassification'''), ('''automatic-speech-recognition''', '''MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING_NAMES''', '''AutoModelForSpeechSeq2Seq'''), ( '''table-question-answering''', '''MODEL_FOR_TABLE_QUESTION_ANSWERING_MAPPING_NAMES''', '''AutoModelForTableQuestionAnswering''', ), ('''token-classification''', '''MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES''', '''AutoModelForTokenClassification'''), ('''multiple-choice''', '''MODEL_FOR_MULTIPLE_CHOICE_MAPPING_NAMES''', '''AutoModelForMultipleChoice'''), ( '''next-sentence-prediction''', '''MODEL_FOR_NEXT_SENTENCE_PREDICTION_MAPPING_NAMES''', '''AutoModelForNextSentencePrediction''', ), ( '''audio-frame-classification''', '''MODEL_FOR_AUDIO_FRAME_CLASSIFICATION_MAPPING_NAMES''', '''AutoModelForAudioFrameClassification''', ), ('''audio-xvector''', '''MODEL_FOR_AUDIO_XVECTOR_MAPPING_NAMES''', '''AutoModelForAudioXVector'''), ( '''document-question-answering''', '''MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING_NAMES''', '''AutoModelForDocumentQuestionAnswering''', ), ( '''visual-question-answering''', '''MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING_NAMES''', '''AutoModelForVisualQuestionAnswering''', ), ('''image-to-text''', '''MODEL_FOR_FOR_VISION_2_SEQ_MAPPING_NAMES''', '''AutoModelForVision2Seq'''), ( '''zero-shot-image-classification''', '''MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING_NAMES''', '''AutoModelForZeroShotImageClassification''', ), ('''depth-estimation''', '''MODEL_FOR_DEPTH_ESTIMATION_MAPPING_NAMES''', '''AutoModelForDepthEstimation'''), ('''video-classification''', '''MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING_NAMES''', '''AutoModelForVideoClassification'''), ('''mask-generation''', '''MODEL_FOR_MASK_GENERATION_MAPPING_NAMES''', '''AutoModelForMaskGeneration'''), ] def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): snake_case_ = re.finditer('''.+?(?:(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])|$)''' , SCREAMING_SNAKE_CASE__ ) return [m.group(0 ) for m in matches] def __SCREAMING_SNAKE_CASE (): snake_case_ = transformers_module.models.auto.configuration_auto.CONFIG_MAPPING_NAMES snake_case_ = { config.replace('''Config''' , '''''' ): model_type for model_type, config in config_maping_names.items() } # Dictionaries flagging if each model prefix has a backend in PT/TF/Flax. snake_case_ = collections.defaultdict(SCREAMING_SNAKE_CASE__ ) snake_case_ = collections.defaultdict(SCREAMING_SNAKE_CASE__ ) snake_case_ = collections.defaultdict(SCREAMING_SNAKE_CASE__ ) # Let's lookup through all transformers object (once) and find if models are supported by a given backend. for attr_name in dir(SCREAMING_SNAKE_CASE__ ): snake_case_ = None if _re_tf_models.match(SCREAMING_SNAKE_CASE__ ) is not None: snake_case_ = tf_models snake_case_ = _re_tf_models.match(SCREAMING_SNAKE_CASE__ ).groups()[0] elif _re_flax_models.match(SCREAMING_SNAKE_CASE__ ) is not None: snake_case_ = flax_models snake_case_ = _re_flax_models.match(SCREAMING_SNAKE_CASE__ ).groups()[0] elif _re_pt_models.match(SCREAMING_SNAKE_CASE__ ) is not None: snake_case_ = pt_models snake_case_ = _re_pt_models.match(SCREAMING_SNAKE_CASE__ ).groups()[0] if lookup_dict is not None: while len(SCREAMING_SNAKE_CASE__ ) > 0: if attr_name in model_prefix_to_model_type: snake_case_ = True break # Try again after removing the last word in the name snake_case_ = ''''''.join(camel_case_split(SCREAMING_SNAKE_CASE__ )[:-1] ) snake_case_ = set(list(pt_models.keys() ) + list(tf_models.keys() ) + list(flax_models.keys() ) ) snake_case_ = list(SCREAMING_SNAKE_CASE__ ) all_models.sort() snake_case_ = {'''model_type''': all_models} snake_case_ = [pt_models[t] for t in all_models] snake_case_ = [tf_models[t] for t in all_models] snake_case_ = [flax_models[t] for t in all_models] # Now let's use the auto-mapping names to make sure snake_case_ = {} for t in all_models: if t in transformers_module.models.auto.processing_auto.PROCESSOR_MAPPING_NAMES: snake_case_ = '''AutoProcessor''' elif t in transformers_module.models.auto.tokenization_auto.TOKENIZER_MAPPING_NAMES: snake_case_ = '''AutoTokenizer''' elif t in transformers_module.models.auto.feature_extraction_auto.FEATURE_EXTRACTOR_MAPPING_NAMES: snake_case_ = '''AutoFeatureExtractor''' else: # Default to AutoTokenizer if a model has nothing, for backward compatibility. snake_case_ = '''AutoTokenizer''' snake_case_ = [processors[t] for t in all_models] return pd.DataFrame(SCREAMING_SNAKE_CASE__ ) def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): snake_case_ = [ transformers_module.models.auto.modeling_auto, transformers_module.models.auto.modeling_tf_auto, transformers_module.models.auto.modeling_flax_auto, ] for pipeline_tag, model_mapping, auto_class in PIPELINE_TAGS_AND_AUTO_MODELS: snake_case_ = [model_mapping, F'''TF_{model_mapping}''', F'''FLAX_{model_mapping}'''] snake_case_ = [auto_class, F'''TF_{auto_class}''', F'''Flax_{auto_class}'''] # Loop through all three frameworks for module, cls, mapping in zip(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): # The type of pipeline may not exist in this framework if not hasattr(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): continue # First extract all model_names snake_case_ = [] for name in getattr(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ).values(): if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): model_names.append(SCREAMING_SNAKE_CASE__ ) else: model_names.extend(list(SCREAMING_SNAKE_CASE__ ) ) # Add pipeline tag and auto model class for those models table.update({model_name: (pipeline_tag, cls) for model_name in model_names} ) return table def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): snake_case_ = get_frameworks_table() snake_case_ = Dataset.from_pandas(SCREAMING_SNAKE_CASE__ ) snake_case_ = hf_hub_download( '''huggingface/transformers-metadata''' , '''pipeline_tags.json''' , repo_type='''dataset''' , token=SCREAMING_SNAKE_CASE__ ) snake_case_ = Dataset.from_json(SCREAMING_SNAKE_CASE__ ) snake_case_ = { tags_dataset[i]['''model_class''']: (tags_dataset[i]['''pipeline_tag'''], tags_dataset[i]['''auto_class''']) for i in range(len(SCREAMING_SNAKE_CASE__ ) ) } snake_case_ = update_pipeline_and_auto_class_table(SCREAMING_SNAKE_CASE__ ) # Sort the model classes to avoid some nondeterministic updates to create false update commits. snake_case_ = sorted(table.keys() ) snake_case_ = pd.DataFrame( { '''model_class''': model_classes, '''pipeline_tag''': [table[m][0] for m in model_classes], '''auto_class''': [table[m][1] for m in model_classes], } ) snake_case_ = Dataset.from_pandas(SCREAMING_SNAKE_CASE__ ) with tempfile.TemporaryDirectory() as tmp_dir: frameworks_dataset.to_json(os.path.join(SCREAMING_SNAKE_CASE__ , '''frameworks.json''' ) ) tags_dataset.to_json(os.path.join(SCREAMING_SNAKE_CASE__ , '''pipeline_tags.json''' ) ) if commit_sha is not None: snake_case_ = ( F'''Update with commit {commit_sha}\n\nSee: ''' F'''https://github.com/huggingface/transformers/commit/{commit_sha}''' ) else: snake_case_ = '''Update''' upload_folder( repo_id='''huggingface/transformers-metadata''' , folder_path=SCREAMING_SNAKE_CASE__ , repo_type='''dataset''' , token=SCREAMING_SNAKE_CASE__ , commit_message=SCREAMING_SNAKE_CASE__ , ) def __SCREAMING_SNAKE_CASE (): snake_case_ = {tag: cls for tag, _, cls in PIPELINE_TAGS_AND_AUTO_MODELS} snake_case_ = transformers_module.pipelines.SUPPORTED_TASKS snake_case_ = [] for key in pipeline_tasks: if key not in in_table: snake_case_ = pipeline_tasks[key]['''pt'''] if isinstance(SCREAMING_SNAKE_CASE__ , (list, tuple) ): snake_case_ = model[0] snake_case_ = model.__name__ if model not in in_table.values(): missing.append(SCREAMING_SNAKE_CASE__ ) if len(SCREAMING_SNAKE_CASE__ ) > 0: snake_case_ = ''', '''.join(SCREAMING_SNAKE_CASE__ ) raise ValueError( '''The following pipeline tags are not present in the `PIPELINE_TAGS_AND_AUTO_MODELS` constant inside ''' F'''`utils/update_metadata.py`: {msg}. Please add them!''' ) if __name__ == "__main__": lowerCAmelCase_ = argparse.ArgumentParser() parser.add_argument('''--token''', type=str, help='''The token to use to push to the transformers-metadata dataset.''') parser.add_argument('''--commit_sha''', type=str, help='''The sha of the commit going with this update.''') parser.add_argument('''--check-only''', action='''store_true''', help='''Activate to just check all pipelines are present.''') lowerCAmelCase_ = parser.parse_args() if args.check_only: check_pipeline_tags() else: update_metadata(args.token, args.commit_sha)
39
import bza import gzip import lzma import os import shutil import struct import tarfile import warnings import zipfile from abc import ABC, abstractmethod from pathlib import Path from typing import Dict, List, Optional, Type, Union from .. import config from .filelock import FileLock from .logging import get_logger lowerCAmelCase_ = get_logger(__name__) class snake_case_ : '''simple docstring''' def __init__( self : int , _UpperCamelCase : Optional[str] = None ) ->Tuple: snake_case_ = ( os.path.join(_UpperCamelCase , config.EXTRACTED_DATASETS_DIR ) if cache_dir else config.EXTRACTED_DATASETS_PATH ) snake_case_ = Extractor def snake_case__( self : Any , _UpperCamelCase : str ) ->str: from .file_utils import hash_url_to_filename # Path where we extract compressed archives # We extract in the cache dir, and get the extracted path name by hashing the original path" snake_case_ = os.path.abspath(_UpperCamelCase ) return os.path.join(self.extract_dir , hash_url_to_filename(_UpperCamelCase ) ) def snake_case__( self : int , _UpperCamelCase : str , _UpperCamelCase : bool ) ->bool: return force_extract or ( not os.path.isfile(_UpperCamelCase ) and not (os.path.isdir(_UpperCamelCase ) and os.listdir(_UpperCamelCase )) ) def snake_case__( self : Tuple , _UpperCamelCase : str , _UpperCamelCase : bool = False ) ->str: snake_case_ = self.extractor.infer_extractor_format(_UpperCamelCase ) if not extractor_format: return input_path snake_case_ = self._get_output_path(_UpperCamelCase ) if self._do_extract(_UpperCamelCase , _UpperCamelCase ): self.extractor.extract(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) return output_path class snake_case_ ( __A ): '''simple docstring''' @classmethod @abstractmethod def snake_case__( cls : Optional[int] , _UpperCamelCase : Union[Path, str] , **_UpperCamelCase : str ) ->bool: ... @staticmethod @abstractmethod def snake_case__( _UpperCamelCase : Union[Path, str] , _UpperCamelCase : Union[Path, str] ) ->None: ... class snake_case_ ( __A , __A ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[bytes] = [] @staticmethod def snake_case__( _UpperCamelCase : Union[Path, str] , _UpperCamelCase : int ) ->List[Any]: with open(_UpperCamelCase , '''rb''' ) as f: return f.read(_UpperCamelCase ) @classmethod def snake_case__( cls : Union[str, Any] , _UpperCamelCase : Union[Path, str] , _UpperCamelCase : bytes = b"" ) ->bool: if not magic_number: snake_case_ = max(len(_UpperCamelCase ) for cls_magic_number in cls.magic_numbers ) try: snake_case_ = cls.read_magic_number(_UpperCamelCase , _UpperCamelCase ) except OSError: return False return any(magic_number.startswith(_UpperCamelCase ) for cls_magic_number in cls.magic_numbers ) class snake_case_ ( __A ): '''simple docstring''' @classmethod def snake_case__( cls : Union[str, Any] , _UpperCamelCase : Union[Path, str] , **_UpperCamelCase : Any ) ->bool: return tarfile.is_tarfile(_UpperCamelCase ) @staticmethod def snake_case__( _UpperCamelCase : Union[str, Any] , _UpperCamelCase : Dict ) ->List[str]: def resolved(_UpperCamelCase : str ) -> str: return os.path.realpath(os.path.abspath(_UpperCamelCase ) ) def badpath(_UpperCamelCase : str , _UpperCamelCase : str ) -> bool: # joinpath will ignore base if path is absolute return not resolved(os.path.join(_UpperCamelCase , _UpperCamelCase ) ).startswith(_UpperCamelCase ) def badlink(_UpperCamelCase : Tuple , _UpperCamelCase : str ) -> bool: # Links are interpreted relative to the directory containing the link snake_case_ = resolved(os.path.join(_UpperCamelCase , os.path.dirname(info.name ) ) ) return badpath(info.linkname , base=_UpperCamelCase ) snake_case_ = resolved(_UpperCamelCase ) for finfo in members: if badpath(finfo.name , _UpperCamelCase ): logger.error(f'''Extraction of {finfo.name} is blocked (illegal path)''' ) elif finfo.issym() and badlink(_UpperCamelCase , _UpperCamelCase ): logger.error(f'''Extraction of {finfo.name} is blocked: Symlink to {finfo.linkname}''' ) elif finfo.islnk() and badlink(_UpperCamelCase , _UpperCamelCase ): logger.error(f'''Extraction of {finfo.name} is blocked: Hard link to {finfo.linkname}''' ) else: yield finfo @staticmethod def snake_case__( _UpperCamelCase : Union[Path, str] , _UpperCamelCase : Union[Path, str] ) ->None: os.makedirs(_UpperCamelCase , exist_ok=_UpperCamelCase ) snake_case_ = tarfile.open(_UpperCamelCase ) tar_file.extractall(_UpperCamelCase , members=TarExtractor.safemembers(_UpperCamelCase , _UpperCamelCase ) ) tar_file.close() class snake_case_ ( __A ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = [b"\x1F\x8B"] @staticmethod def snake_case__( _UpperCamelCase : Union[Path, str] , _UpperCamelCase : Union[Path, str] ) ->None: with gzip.open(_UpperCamelCase , '''rb''' ) as gzip_file: with open(_UpperCamelCase , '''wb''' ) as extracted_file: shutil.copyfileobj(_UpperCamelCase , _UpperCamelCase ) class snake_case_ ( __A ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = [ b"PK\x03\x04", b"PK\x05\x06", # empty archive b"PK\x07\x08", # spanned archive ] @classmethod def snake_case__( cls : List[str] , _UpperCamelCase : Union[Path, str] , _UpperCamelCase : bytes = b"" ) ->bool: if super().is_extractable(_UpperCamelCase , magic_number=_UpperCamelCase ): return True try: # Alternative version of zipfile.is_zipfile that has less false positives, but misses executable zip archives. # From: https://github.com/python/cpython/pull/5053 from zipfile import ( _CD_SIGNATURE, _ECD_DISK_NUMBER, _ECD_DISK_START, _ECD_ENTRIES_TOTAL, _ECD_OFFSET, _ECD_SIZE, _EndRecData, sizeCentralDir, stringCentralDir, structCentralDir, ) with open(_UpperCamelCase , '''rb''' ) as fp: snake_case_ = _EndRecData(_UpperCamelCase ) if endrec: if endrec[_ECD_ENTRIES_TOTAL] == 0 and endrec[_ECD_SIZE] == 0 and endrec[_ECD_OFFSET] == 0: return True # Empty zipfiles are still zipfiles elif endrec[_ECD_DISK_NUMBER] == endrec[_ECD_DISK_START]: fp.seek(endrec[_ECD_OFFSET] ) # Central directory is on the same disk if fp.tell() == endrec[_ECD_OFFSET] and endrec[_ECD_SIZE] >= sizeCentralDir: snake_case_ = fp.read(_UpperCamelCase ) # CD is where we expect it to be if len(_UpperCamelCase ) == sizeCentralDir: snake_case_ = struct.unpack(_UpperCamelCase , _UpperCamelCase ) # CD is the right size if centdir[_CD_SIGNATURE] == stringCentralDir: return True # First central directory entry has correct magic number return False except Exception: # catch all errors in case future python versions change the zipfile internals return False @staticmethod def snake_case__( _UpperCamelCase : Union[Path, str] , _UpperCamelCase : Union[Path, str] ) ->None: os.makedirs(_UpperCamelCase , exist_ok=_UpperCamelCase ) with zipfile.ZipFile(_UpperCamelCase , '''r''' ) as zip_file: zip_file.extractall(_UpperCamelCase ) zip_file.close() class snake_case_ ( __A ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = [b"\xFD\x37\x7A\x58\x5A\x00"] @staticmethod def snake_case__( _UpperCamelCase : Union[Path, str] , _UpperCamelCase : Union[Path, str] ) ->None: with lzma.open(_UpperCamelCase ) as compressed_file: with open(_UpperCamelCase , '''wb''' ) as extracted_file: shutil.copyfileobj(_UpperCamelCase , _UpperCamelCase ) class snake_case_ ( __A ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = [b"Rar!\x1a\x07\x00", b"Rar!\x1a\x07\x01\x00"] # RAR_ID # RAR5_ID @staticmethod def snake_case__( _UpperCamelCase : Union[Path, str] , _UpperCamelCase : Union[Path, str] ) ->None: if not config.RARFILE_AVAILABLE: raise ImportError('''Please pip install rarfile''' ) import rarfile os.makedirs(_UpperCamelCase , exist_ok=_UpperCamelCase ) snake_case_ = rarfile.RarFile(_UpperCamelCase ) rf.extractall(_UpperCamelCase ) rf.close() class snake_case_ ( __A ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = [b"\x28\xb5\x2F\xFD"] @staticmethod def snake_case__( _UpperCamelCase : Union[Path, str] , _UpperCamelCase : Union[Path, str] ) ->None: if not config.ZSTANDARD_AVAILABLE: raise ImportError('''Please pip install zstandard''' ) import zstandard as zstd snake_case_ = zstd.ZstdDecompressor() with open(_UpperCamelCase , '''rb''' ) as ifh, open(_UpperCamelCase , '''wb''' ) as ofh: dctx.copy_stream(_UpperCamelCase , _UpperCamelCase ) class snake_case_ ( __A ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = [b"\x42\x5A\x68"] @staticmethod def snake_case__( _UpperCamelCase : Union[Path, str] , _UpperCamelCase : Union[Path, str] ) ->None: with bza.open(_UpperCamelCase , '''rb''' ) as compressed_file: with open(_UpperCamelCase , '''wb''' ) as extracted_file: shutil.copyfileobj(_UpperCamelCase , _UpperCamelCase ) class snake_case_ ( __A ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = [b"\x37\x7A\xBC\xAF\x27\x1C"] @staticmethod def snake_case__( _UpperCamelCase : Union[Path, str] , _UpperCamelCase : Union[Path, str] ) ->None: if not config.PY7ZR_AVAILABLE: raise ImportError('''Please pip install py7zr''' ) import pyazr os.makedirs(_UpperCamelCase , exist_ok=_UpperCamelCase ) with pyazr.SevenZipFile(_UpperCamelCase , '''r''' ) as archive: archive.extractall(_UpperCamelCase ) class snake_case_ ( __A ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = [b"\x04\x22\x4D\x18"] @staticmethod def snake_case__( _UpperCamelCase : Union[Path, str] , _UpperCamelCase : Union[Path, str] ) ->None: if not config.LZ4_AVAILABLE: raise ImportError('''Please pip install lz4''' ) import lza.frame with lza.frame.open(_UpperCamelCase , '''rb''' ) as compressed_file: with open(_UpperCamelCase , '''wb''' ) as extracted_file: shutil.copyfileobj(_UpperCamelCase , _UpperCamelCase ) class snake_case_ : '''simple docstring''' SCREAMING_SNAKE_CASE : Dict[str, Type[BaseExtractor]] = { "tar": TarExtractor, "gzip": GzipExtractor, "zip": ZipExtractor, "xz": XzExtractor, "rar": RarExtractor, "zstd": ZstdExtractor, "bz2": BzipaExtractor, "7z": SevenZipExtractor, # <Added version="2.4.0"/> "lz4": LzaExtractor, # <Added version="2.4.0"/> } @classmethod def snake_case__( cls : List[Any] ) ->List[str]: return max( len(_UpperCamelCase ) for extractor in cls.extractors.values() if issubclass(_UpperCamelCase , _UpperCamelCase ) for extractor_magic_number in extractor.magic_numbers ) @staticmethod def snake_case__( _UpperCamelCase : Union[Path, str] , _UpperCamelCase : int ) ->Tuple: try: return MagicNumberBaseExtractor.read_magic_number(_UpperCamelCase , magic_number_length=_UpperCamelCase ) except OSError: return b"" @classmethod def snake_case__( cls : Optional[Any] , _UpperCamelCase : Union[Path, str] , _UpperCamelCase : bool = False ) ->bool: warnings.warn( '''Method \'is_extractable\' was deprecated in version 2.4.0 and will be removed in 3.0.0. ''' '''Use \'infer_extractor_format\' instead.''' , category=_UpperCamelCase , ) snake_case_ = cls.infer_extractor_format(_UpperCamelCase ) if extractor_format: return True if not return_extractor else (True, cls.extractors[extractor_format]) return False if not return_extractor else (False, None) @classmethod def snake_case__( cls : int , _UpperCamelCase : Union[Path, str] ) ->str: # <Added version="2.4.0"/> snake_case_ = cls._get_magic_number_max_length() snake_case_ = cls._read_magic_number(_UpperCamelCase , _UpperCamelCase ) for extractor_format, extractor in cls.extractors.items(): if extractor.is_extractable(_UpperCamelCase , magic_number=_UpperCamelCase ): return extractor_format @classmethod def snake_case__( cls : Optional[int] , _UpperCamelCase : Union[Path, str] , _UpperCamelCase : Union[Path, str] , _UpperCamelCase : Optional[str] = None , _UpperCamelCase : Optional[BaseExtractor] = "deprecated" , ) ->None: os.makedirs(os.path.dirname(_UpperCamelCase ) , exist_ok=_UpperCamelCase ) # Prevent parallel extractions snake_case_ = str(Path(_UpperCamelCase ).with_suffix('''.lock''' ) ) with FileLock(_UpperCamelCase ): shutil.rmtree(_UpperCamelCase , ignore_errors=_UpperCamelCase ) if extractor_format or extractor != "deprecated": if extractor != "deprecated" or not isinstance(_UpperCamelCase , _UpperCamelCase ): # passed as positional arg warnings.warn( '''Parameter \'extractor\' was deprecated in version 2.4.0 and will be removed in 3.0.0. ''' '''Use \'extractor_format\' instead.''' , category=_UpperCamelCase , ) snake_case_ = extractor if extractor != '''deprecated''' else extractor_format else: snake_case_ = cls.extractors[extractor_format] return extractor.extract(_UpperCamelCase , _UpperCamelCase ) else: warnings.warn( '''Parameter \'extractor_format\' was made required in version 2.4.0 and not passing it will raise an ''' '''exception in 3.0.0.''' , category=_UpperCamelCase , ) for extractor in cls.extractors.values(): if extractor.is_extractable(_UpperCamelCase ): return extractor.extract(_UpperCamelCase , _UpperCamelCase )
39
1
# DISCLAIMER: This file is strongly influenced by https://github.com/yang-song/score_sde_pytorch import math from typing import Union import torch from ..configuration_utils import ConfigMixin, register_to_config from ..utils import randn_tensor from .scheduling_utils import SchedulerMixin class snake_case_ ( __A , __A ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = 1 @register_to_config def __init__( self : Union[str, Any] , _UpperCamelCase : Tuple=2_0_0_0 , _UpperCamelCase : str=0.1 , _UpperCamelCase : Any=2_0 , _UpperCamelCase : Optional[int]=1e-3 ) ->List[Any]: snake_case_ = None snake_case_ = None snake_case_ = None def snake_case__( self : str , _UpperCamelCase : Dict , _UpperCamelCase : Union[str, torch.device] = None ) ->List[Any]: snake_case_ = torch.linspace(1 , self.config.sampling_eps , _UpperCamelCase , device=_UpperCamelCase ) def snake_case__( self : Optional[Any] , _UpperCamelCase : int , _UpperCamelCase : Optional[Any] , _UpperCamelCase : List[str] , _UpperCamelCase : int=None ) ->Optional[int]: if self.timesteps is None: raise ValueError( '''`self.timesteps` is not set, you need to run \'set_timesteps\' after creating the scheduler''' ) # TODO(Patrick) better comments + non-PyTorch # postprocess model score snake_case_ = ( -0.25 * t**2 * (self.config.beta_max - self.config.beta_min) - 0.5 * t * self.config.beta_min ) snake_case_ = torch.sqrt(1.0 - torch.exp(2.0 * log_mean_coeff ) ) snake_case_ = std.flatten() while len(std.shape ) < len(score.shape ): snake_case_ = std.unsqueeze(-1 ) snake_case_ = -score / std # compute snake_case_ = -1.0 / len(self.timesteps ) snake_case_ = self.config.beta_min + t * (self.config.beta_max - self.config.beta_min) snake_case_ = beta_t.flatten() while len(beta_t.shape ) < len(x.shape ): snake_case_ = beta_t.unsqueeze(-1 ) snake_case_ = -0.5 * beta_t * x snake_case_ = torch.sqrt(_UpperCamelCase ) snake_case_ = drift - diffusion**2 * score snake_case_ = x + drift * dt # add noise snake_case_ = randn_tensor(x.shape , layout=x.layout , generator=_UpperCamelCase , device=x.device , dtype=x.dtype ) snake_case_ = x_mean + diffusion * math.sqrt(-dt ) * noise return x, x_mean def __len__( self : int ) ->Union[str, Any]: return self.config.num_train_timesteps
39
def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): if any(not isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) or x < 0 for x in sequence ): raise TypeError('''Sequence must be list of non-negative integers''' ) for _ in range(len(SCREAMING_SNAKE_CASE__ ) ): for i, (rod_upper, rod_lower) in enumerate(zip(SCREAMING_SNAKE_CASE__ , sequence[1:] ) ): if rod_upper > rod_lower: sequence[i] -= rod_upper - rod_lower sequence[i + 1] += rod_upper - rod_lower return sequence if __name__ == "__main__": assert bead_sort([5, 4, 3, 2, 1]) == [1, 2, 3, 4, 5] assert bead_sort([7, 9, 4, 3, 5]) == [3, 4, 5, 7, 9]
39
1
import numpy as np def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): return 1 / (1 + np.exp(-vector )) def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): return vector * sigmoid(1.702 * vector ) if __name__ == "__main__": import doctest doctest.testmod()
39
import re from filelock import FileLock try: import nltk lowerCAmelCase_ = True except (ImportError, ModuleNotFoundError): lowerCAmelCase_ = False if NLTK_AVAILABLE: with FileLock('''.lock''') as lock: nltk.download('''punkt''', quiet=True) def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): re.sub('''<n>''' , '''''' , SCREAMING_SNAKE_CASE__ ) # remove pegasus newline char assert NLTK_AVAILABLE, "nltk must be installed to separate newlines between sentences. (pip install nltk)" return "\n".join(nltk.sent_tokenize(SCREAMING_SNAKE_CASE__ ) )
39
1
def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): return not any( neighbour == 1 and colored_vertices[i] == color for i, neighbour in enumerate(SCREAMING_SNAKE_CASE__ ) ) def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): # Base Case if index == len(SCREAMING_SNAKE_CASE__ ): return True # Recursive Step for i in range(SCREAMING_SNAKE_CASE__ ): if valid_coloring(graph[index] , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): # Color current vertex snake_case_ = i # Validate coloring if util_color(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , index + 1 ): return True # Backtrack snake_case_ = -1 return False def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): snake_case_ = [-1] * len(SCREAMING_SNAKE_CASE__ ) if util_color(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , 0 ): return colored_vertices return []
39
def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): snake_case_ = [0 for i in range(r + 1 )] # nc0 = 1 snake_case_ = 1 for i in range(1 , n + 1 ): # to compute current row from previous row. snake_case_ = min(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) while j > 0: c[j] += c[j - 1] j -= 1 return c[r] print(binomial_coefficient(n=10, r=5))
39
1
import timeit import numpy as np import datasets from datasets.arrow_writer import ArrowWriter from datasets.features.features import _ArrayXD def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): def wrapper(*SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ): snake_case_ = timeit.default_timer() snake_case_ = func(*SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) snake_case_ = timeit.default_timer() - starttime return delta snake_case_ = func.__name__ return wrapper def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__=100 , SCREAMING_SNAKE_CASE__=None ): snake_case_ = [] snake_case_ = seq_shapes or {} for i in range(SCREAMING_SNAKE_CASE__ ): snake_case_ = {} for col_id, (k, v) in enumerate(features.items() ): if isinstance(SCREAMING_SNAKE_CASE__ , _ArrayXD ): snake_case_ = np.random.rand(*v.shape ).astype(v.dtype ) elif isinstance(SCREAMING_SNAKE_CASE__ , datasets.Value ): if v.dtype == "string": snake_case_ = '''The small grey turtle was surprisingly fast when challenged.''' else: snake_case_ = np.random.randint(10 , size=1 ).astype(v.dtype ).item() elif isinstance(SCREAMING_SNAKE_CASE__ , datasets.Sequence ): while isinstance(SCREAMING_SNAKE_CASE__ , datasets.Sequence ): snake_case_ = v.feature snake_case_ = seq_shapes[k] snake_case_ = np.random.rand(*SCREAMING_SNAKE_CASE__ ).astype(v.dtype ) snake_case_ = data dummy_data.append((i, example) ) return dummy_data def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__=100 , SCREAMING_SNAKE_CASE__=None ): snake_case_ = generate_examples(SCREAMING_SNAKE_CASE__ , num_examples=SCREAMING_SNAKE_CASE__ , seq_shapes=SCREAMING_SNAKE_CASE__ ) with ArrowWriter(features=SCREAMING_SNAKE_CASE__ , path=SCREAMING_SNAKE_CASE__ ) as writer: for key, record in dummy_data: snake_case_ = features.encode_example(SCREAMING_SNAKE_CASE__ ) writer.write(SCREAMING_SNAKE_CASE__ ) snake_case_, snake_case_ = writer.finalize() if not num_final_examples == num_examples: raise ValueError( F'''Error writing the dataset, wrote {num_final_examples} examples but should have written {num_examples}.''' ) snake_case_ = datasets.Dataset.from_file(filename=SCREAMING_SNAKE_CASE__ , info=datasets.DatasetInfo(features=SCREAMING_SNAKE_CASE__ ) ) return dataset
39
import argparse import math import os from copy import deepcopy import torch from audio_diffusion.models import DiffusionAttnUnetaD from diffusion import sampling from torch import nn from diffusers import DanceDiffusionPipeline, IPNDMScheduler, UNetaDModel lowerCAmelCase_ = { '''gwf-440k''': { '''url''': '''https://model-server.zqevans2.workers.dev/gwf-440k.ckpt''', '''sample_rate''': 4_80_00, '''sample_size''': 6_55_36, }, '''jmann-small-190k''': { '''url''': '''https://model-server.zqevans2.workers.dev/jmann-small-190k.ckpt''', '''sample_rate''': 4_80_00, '''sample_size''': 6_55_36, }, '''jmann-large-580k''': { '''url''': '''https://model-server.zqevans2.workers.dev/jmann-large-580k.ckpt''', '''sample_rate''': 4_80_00, '''sample_size''': 13_10_72, }, '''maestro-uncond-150k''': { '''url''': '''https://model-server.zqevans2.workers.dev/maestro-uncond-150k.ckpt''', '''sample_rate''': 1_60_00, '''sample_size''': 6_55_36, }, '''unlocked-uncond-250k''': { '''url''': '''https://model-server.zqevans2.workers.dev/unlocked-uncond-250k.ckpt''', '''sample_rate''': 1_60_00, '''sample_size''': 6_55_36, }, '''honk-140k''': { '''url''': '''https://model-server.zqevans2.workers.dev/honk-140k.ckpt''', '''sample_rate''': 1_60_00, '''sample_size''': 6_55_36, }, } def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): return torch.atana(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) / math.pi * 2 def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): snake_case_ = torch.sin(t * math.pi / 2 ) ** 2 snake_case_ = (1 - sigma**2) ** 0.5 return alpha_sigma_to_t(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) class snake_case_ ( __A ): '''simple docstring''' pass class snake_case_ ( nn.Module ): '''simple docstring''' def __init__( self : List[Any] , _UpperCamelCase : int ) ->Optional[int]: super().__init__() snake_case_ = DiffusionAttnUnetaD(_UpperCamelCase , n_attn_layers=4 ) snake_case_ = deepcopy(self.diffusion ) snake_case_ = torch.quasirandom.SobolEngine(1 , scramble=_UpperCamelCase ) def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): snake_case_ = MODELS_MAP[model_name]['''url'''] os.system(F'''wget {url} ./''' ) return F'''./{model_name}.ckpt''' lowerCAmelCase_ = { '''1''': '''resnets.0''', '''2''': '''attentions.0''', '''3''': '''resnets.1''', '''4''': '''attentions.1''', '''5''': '''resnets.2''', '''6''': '''attentions.2''', } lowerCAmelCase_ = { '''8''': '''resnets.0''', '''9''': '''attentions.0''', '''10''': '''resnets.1''', '''11''': '''attentions.1''', '''12''': '''resnets.2''', '''13''': '''attentions.2''', } lowerCAmelCase_ = { '''1''': '''resnets.0''', '''2''': '''attentions.0''', '''3''': '''resnets.1''', '''4''': '''attentions.1''', '''5''': '''resnets.2''', '''6''': '''attentions.2''', '''8''': '''resnets.3''', '''9''': '''attentions.3''', '''10''': '''resnets.4''', '''11''': '''attentions.4''', '''12''': '''resnets.5''', '''13''': '''attentions.5''', } lowerCAmelCase_ = { '''0''': '''resnets.0''', '''1''': '''resnets.1''', '''2''': '''resnets.2''', '''4''': '''resnets.0''', '''5''': '''resnets.1''', '''6''': '''resnets.2''', } lowerCAmelCase_ = { '''skip''': '''conv_skip''', '''main.0''': '''conv_1''', '''main.1''': '''group_norm_1''', '''main.3''': '''conv_2''', '''main.4''': '''group_norm_2''', } lowerCAmelCase_ = { '''norm''': '''group_norm''', '''qkv_proj''': ['''query''', '''key''', '''value'''], '''out_proj''': ['''proj_attn'''], } def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): if name.startswith('''skip''' ): return name.replace('''skip''' , RES_CONV_MAP['''skip'''] ) # name has to be of format main.{digit} if not name.startswith('''main.''' ): raise ValueError(F'''ResConvBlock error with {name}''' ) return name.replace(name[:6] , RES_CONV_MAP[name[:6]] ) def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): for key, value in ATTN_MAP.items(): if name.startswith(SCREAMING_SNAKE_CASE__ ) and not isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): return name.replace(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) elif name.startswith(SCREAMING_SNAKE_CASE__ ): return [name.replace(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) for v in value] raise ValueError(F'''Attn error with {name}''' ) def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__=13 ): snake_case_ = input_string if string.split('''.''' )[0] == "timestep_embed": return string.replace('''timestep_embed''' , '''time_proj''' ) snake_case_ = 0 if string.startswith('''net.3.''' ): depth += 1 snake_case_ = string[6:] elif string.startswith('''net.''' ): snake_case_ = string[4:] while string.startswith('''main.7.''' ): depth += 1 snake_case_ = string[7:] if string.startswith('''main.''' ): snake_case_ = string[5:] # mid block if string[:2].isdigit(): snake_case_ = string[:2] snake_case_ = string[2:] else: snake_case_ = string[0] snake_case_ = string[1:] if depth == max_depth: snake_case_ = MID_NUM_TO_LAYER[layer_num] snake_case_ = '''mid_block''' elif depth > 0 and int(SCREAMING_SNAKE_CASE__ ) < 7: snake_case_ = DOWN_NUM_TO_LAYER[layer_num] snake_case_ = F'''down_blocks.{depth}''' elif depth > 0 and int(SCREAMING_SNAKE_CASE__ ) > 7: snake_case_ = UP_NUM_TO_LAYER[layer_num] snake_case_ = F'''up_blocks.{max_depth - depth - 1}''' elif depth == 0: snake_case_ = DEPTH_0_TO_LAYER[layer_num] snake_case_ = F'''up_blocks.{max_depth - 1}''' if int(SCREAMING_SNAKE_CASE__ ) > 3 else '''down_blocks.0''' if not string_left.startswith('''.''' ): raise ValueError(F'''Naming error with {input_string} and string_left: {string_left}.''' ) snake_case_ = string_left[1:] if "resnets" in new_layer: snake_case_ = convert_resconv_naming(SCREAMING_SNAKE_CASE__ ) elif "attentions" in new_layer: snake_case_ = convert_attn_naming(SCREAMING_SNAKE_CASE__ ) snake_case_ = new_string_left if not isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): snake_case_ = prefix + '''.''' + new_layer + '''.''' + string_left else: snake_case_ = [prefix + '''.''' + new_layer + '''.''' + s for s in string_left] return new_string def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): snake_case_ = {} for k, v in state_dict.items(): if k.endswith('''kernel''' ): # up- and downsample layers, don't have trainable weights continue snake_case_ = rename(SCREAMING_SNAKE_CASE__ ) # check if we need to transform from Conv => Linear for attention if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): snake_case_ = transform_conv_attns(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) else: snake_case_ = v return new_state_dict def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): if len(SCREAMING_SNAKE_CASE__ ) == 1: if len(v.shape ) == 3: # weight snake_case_ = v[:, :, 0] else: # bias snake_case_ = v else: # qkv matrices snake_case_ = v.shape[0] snake_case_ = trippled_shape // 3 for i in range(3 ): if len(v.shape ) == 3: snake_case_ = v[i * single_shape : (i + 1) * single_shape, :, 0] else: snake_case_ = v[i * single_shape : (i + 1) * single_shape] return new_state_dict def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): snake_case_ = torch.device('''cuda''' if torch.cuda.is_available() else '''cpu''' ) snake_case_ = args.model_path.split('''/''' )[-1].split('''.''' )[0] if not os.path.isfile(args.model_path ): assert ( model_name == args.model_path ), F'''Make sure to provide one of the official model names {MODELS_MAP.keys()}''' snake_case_ = download(SCREAMING_SNAKE_CASE__ ) snake_case_ = MODELS_MAP[model_name]['''sample_rate'''] snake_case_ = MODELS_MAP[model_name]['''sample_size'''] snake_case_ = Object() snake_case_ = sample_size snake_case_ = sample_rate snake_case_ = 0 snake_case_ = UNetaDModel(sample_size=SCREAMING_SNAKE_CASE__ , sample_rate=SCREAMING_SNAKE_CASE__ ) snake_case_ = diffusers_model.state_dict() snake_case_ = DiffusionUncond(SCREAMING_SNAKE_CASE__ ) orig_model.load_state_dict(torch.load(args.model_path , map_location=SCREAMING_SNAKE_CASE__ )['''state_dict'''] ) snake_case_ = orig_model.diffusion_ema.eval() snake_case_ = orig_model.state_dict() snake_case_ = rename_orig_weights(SCREAMING_SNAKE_CASE__ ) snake_case_ = set(renamed_state_dict.keys() ) - set(diffusers_state_dict.keys() ) snake_case_ = set(diffusers_state_dict.keys() ) - set(renamed_state_dict.keys() ) assert len(SCREAMING_SNAKE_CASE__ ) == 0, F'''Problem with {renamed_minus_diffusers}''' assert all(k.endswith('''kernel''' ) for k in list(SCREAMING_SNAKE_CASE__ ) ), F'''Problem with {diffusers_minus_renamed}''' for key, value in renamed_state_dict.items(): assert ( diffusers_state_dict[key].squeeze().shape == value.squeeze().shape ), F'''Shape for {key} doesn\'t match. Diffusers: {diffusers_state_dict[key].shape} vs. {value.shape}''' if key == "time_proj.weight": snake_case_ = value.squeeze() snake_case_ = value diffusers_model.load_state_dict(SCREAMING_SNAKE_CASE__ ) snake_case_ = 100 snake_case_ = 33 snake_case_ = IPNDMScheduler(num_train_timesteps=SCREAMING_SNAKE_CASE__ ) snake_case_ = torch.manual_seed(SCREAMING_SNAKE_CASE__ ) snake_case_ = torch.randn([1, 2, config.sample_size] , generator=SCREAMING_SNAKE_CASE__ ).to(SCREAMING_SNAKE_CASE__ ) snake_case_ = torch.linspace(1 , 0 , steps + 1 , device=SCREAMING_SNAKE_CASE__ )[:-1] snake_case_ = get_crash_schedule(SCREAMING_SNAKE_CASE__ ) snake_case_ = DanceDiffusionPipeline(unet=SCREAMING_SNAKE_CASE__ , scheduler=SCREAMING_SNAKE_CASE__ ) snake_case_ = torch.manual_seed(33 ) snake_case_ = pipe(num_inference_steps=SCREAMING_SNAKE_CASE__ , generator=SCREAMING_SNAKE_CASE__ ).audios snake_case_ = sampling.iplms_sample(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , {} ) snake_case_ = generated.clamp(-1 , 1 ) snake_case_ = (generated - audio).abs().sum() snake_case_ = (generated - audio).abs().max() if args.save: pipe.save_pretrained(args.checkpoint_path ) print('''Diff sum''' , SCREAMING_SNAKE_CASE__ ) print('''Diff max''' , SCREAMING_SNAKE_CASE__ ) assert diff_max < 1E-3, F'''Diff max: {diff_max} is too much :-/''' print(F'''Conversion for {model_name} successful!''' ) if __name__ == "__main__": lowerCAmelCase_ = argparse.ArgumentParser() parser.add_argument('''--model_path''', default=None, type=str, required=True, help='''Path to the model to convert.''') parser.add_argument( '''--save''', default=True, type=bool, required=False, help='''Whether to save the converted model or not.''' ) parser.add_argument('''--checkpoint_path''', default=None, type=str, required=True, help='''Path to the output model.''') lowerCAmelCase_ = parser.parse_args() main(args)
39
1
from collections import defaultdict from math import ceil, sqrt def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ = 1000000 , SCREAMING_SNAKE_CASE__ = 10 ): snake_case_ = defaultdict(SCREAMING_SNAKE_CASE__ ) for outer_width in range(3 , (t_limit // 4) + 2 ): if outer_width * outer_width > t_limit: snake_case_ = max( ceil(sqrt(outer_width * outer_width - t_limit ) ) , 1 ) else: snake_case_ = 1 hole_width_lower_bound += (outer_width - hole_width_lower_bound) % 2 for hole_width in range(SCREAMING_SNAKE_CASE__ , outer_width - 1 , 2 ): count[outer_width * outer_width - hole_width * hole_width] += 1 return sum(1 for n in count.values() if 1 <= n <= 10 ) if __name__ == "__main__": print(f"""{solution() = }""")
39
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCAmelCase_ = {'''configuration_vit_msn''': ['''VIT_MSN_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''ViTMSNConfig''']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ = [ '''VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST''', '''ViTMSNModel''', '''ViTMSNForImageClassification''', '''ViTMSNPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_vit_msn import VIT_MSN_PRETRAINED_CONFIG_ARCHIVE_MAP, ViTMSNConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vit_msn import ( VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST, ViTMSNForImageClassification, ViTMSNModel, ViTMSNPreTrainedModel, ) else: import sys lowerCAmelCase_ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
39
1
from ..utils import DummyObject, requires_backends class snake_case_ ( metaclass=__A ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = ["flax", "transformers"] def __init__( self : Union[str, Any] , *_UpperCamelCase : Optional[int] , **_UpperCamelCase : Dict ) ->int: requires_backends(self , ['''flax''', '''transformers'''] ) @classmethod def snake_case__( cls : Any , *_UpperCamelCase : Optional[Any] , **_UpperCamelCase : str ) ->str: requires_backends(cls , ['''flax''', '''transformers'''] ) @classmethod def snake_case__( cls : Tuple , *_UpperCamelCase : Any , **_UpperCamelCase : Union[str, Any] ) ->Union[str, Any]: requires_backends(cls , ['''flax''', '''transformers'''] ) class snake_case_ ( metaclass=__A ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = ["flax", "transformers"] def __init__( self : Tuple , *_UpperCamelCase : Optional[int] , **_UpperCamelCase : str ) ->Optional[Any]: requires_backends(self , ['''flax''', '''transformers'''] ) @classmethod def snake_case__( cls : str , *_UpperCamelCase : Any , **_UpperCamelCase : Any ) ->str: requires_backends(cls , ['''flax''', '''transformers'''] ) @classmethod def snake_case__( cls : Optional[int] , *_UpperCamelCase : int , **_UpperCamelCase : Optional[int] ) ->int: requires_backends(cls , ['''flax''', '''transformers'''] ) class snake_case_ ( metaclass=__A ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = ["flax", "transformers"] def __init__( self : Optional[Any] , *_UpperCamelCase : Dict , **_UpperCamelCase : Union[str, Any] ) ->str: requires_backends(self , ['''flax''', '''transformers'''] ) @classmethod def snake_case__( cls : Dict , *_UpperCamelCase : str , **_UpperCamelCase : str ) ->int: requires_backends(cls , ['''flax''', '''transformers'''] ) @classmethod def snake_case__( cls : List[Any] , *_UpperCamelCase : Tuple , **_UpperCamelCase : int ) ->Tuple: requires_backends(cls , ['''flax''', '''transformers'''] ) class snake_case_ ( metaclass=__A ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = ["flax", "transformers"] def __init__( self : Tuple , *_UpperCamelCase : str , **_UpperCamelCase : Any ) ->Union[str, Any]: requires_backends(self , ['''flax''', '''transformers'''] ) @classmethod def snake_case__( cls : List[str] , *_UpperCamelCase : Dict , **_UpperCamelCase : List[Any] ) ->Any: requires_backends(cls , ['''flax''', '''transformers'''] ) @classmethod def snake_case__( cls : str , *_UpperCamelCase : Optional[int] , **_UpperCamelCase : str ) ->str: requires_backends(cls , ['''flax''', '''transformers'''] )
39
from __future__ import annotations import os import tempfile import unittest import numpy as np from huggingface_hub import hf_hub_download from transformers import is_tensorflow_text_available, is_tf_available from transformers.testing_utils import require_tensorflow_text, require_tf, slow from ..test_modeling_tf_common import floats_tensor from .test_framework_agnostic import GenerationIntegrationTestsMixin if is_tf_available(): import tensorflow as tf from transformers import ( AutoTokenizer, TFAutoModelForCausalLM, TFAutoModelForSeqaSeqLM, TFAutoModelForSpeechSeqaSeq, TFAutoModelForVisionaSeq, TFBartForConditionalGeneration, TFLogitsProcessorList, TFMinLengthLogitsProcessor, tf_top_k_top_p_filtering, ) if is_tensorflow_text_available(): import tensorflow_text as text @require_tf class snake_case_ ( unittest.TestCase ): '''simple docstring''' def snake_case__( self : Optional[Any] ) ->Any: snake_case_ = tf.convert_to_tensor( [ [ 8.2220991, # 3rd highest value; idx. 0 -0.5620044, 5.23229752, 4.0386393, -6.8798378, -0.54785802, -3.2012153, 2.92777176, 1.88171953, 7.35341276, # 5th highest value; idx. 9 8.43207833, # 2nd highest value; idx. 10 -9.85711836, -5.96209236, -1.13039161, -7.1115294, -0.8369633, -5.3186408, 7.06427407, 0.81369344, -0.82023817, -5.9179796, 0.58813443, -6.99778438, 4.71551189, -0.18771637, 7.44020759, # 4th highest value; idx. 25 9.38450987, # 1st highest value; idx. 26 2.12662941, -9.32562038, 2.35652522, ], # cummulative prob of 5 highest values <= 0.6 [ 0.58425518, 4.53139238, -5.57510464, -6.28030699, -7.19529503, -4.02122551, 1.39337037, -6.06707057, 1.59480517, -9.643119, 0.03907799, 0.67231762, -8.88206726, 6.27115922, # 4th highest value; idx. 13 2.28520723, 4.82767506, 4.30421368, 8.8275313, # 2nd highest value; idx. 17 5.44029958, # 5th highest value; idx. 18 -4.4735794, 7.38579536, # 3rd highest value; idx. 20 -2.91051663, 2.61946077, -2.5674762, -9.48959302, -4.02922645, -1.35416918, 9.67702323, # 1st highest value; idx. 27 -5.89478553, 1.85370467, ], # cummulative prob of 5 highest values <= 0.6 ] , dtype=tf.floataa , ) snake_case_ = tf.convert_to_tensor( [[0, 0], [0, 9], [0, 1_0], [0, 2_5], [0, 2_6], [1, 1_3], [1, 1_7], [1, 1_8], [1, 2_0], [1, 2_7]] , dtype=tf.intaa , ) # expected non filtered idx as noted above snake_case_ = tf.convert_to_tensor( [8.222099, 7.3534126, 8.432078, 7.4402075, 9.38451, 6.271159, 8.827531, 5.4402995, 7.3857956, 9.677023] , dtype=tf.floataa , ) # expected non filtered values as noted above snake_case_ = tf_top_k_top_p_filtering(_UpperCamelCase , top_k=1_0 , top_p=0.6 , min_tokens_to_keep=4 ) snake_case_ = output[output != -float('''inf''' )] snake_case_ = tf.cast( tf.where(tf.not_equal(_UpperCamelCase , tf.constant(-float('''inf''' ) , dtype=tf.floataa ) ) ) , dtype=tf.intaa , ) tf.debugging.assert_near(_UpperCamelCase , _UpperCamelCase , rtol=1e-12 ) tf.debugging.assert_equal(_UpperCamelCase , _UpperCamelCase ) @require_tf class snake_case_ ( unittest.TestCase , __A ): '''simple docstring''' if is_tf_available(): SCREAMING_SNAKE_CASE : Optional[int] = { "AutoModelForCausalLM": TFAutoModelForCausalLM, "AutoModelForSpeechSeq2Seq": TFAutoModelForSpeechSeqaSeq, "AutoModelForSeq2SeqLM": TFAutoModelForSeqaSeqLM, "AutoModelForVision2Seq": TFAutoModelForVisionaSeq, "LogitsProcessorList": TFLogitsProcessorList, "MinLengthLogitsProcessor": TFMinLengthLogitsProcessor, "create_tensor_fn": tf.convert_to_tensor, "floats_tensor": floats_tensor, "return_tensors": "tf", } @slow def snake_case__( self : List[Any] ) ->Optional[int]: # TF-only test: tf.saved_model export snake_case_ = TFAutoModelForCausalLM.from_pretrained('''hf-internal-testing/tiny-random-gpt2''' ) snake_case_ = 2 snake_case_ = 2 class snake_case_ ( tf.Module ): '''simple docstring''' def __init__( self : Optional[Any] , _UpperCamelCase : Optional[int] ) ->List[Any]: super(_UpperCamelCase , self ).__init__() snake_case_ = model @tf.function( input_signature=( tf.TensorSpec((None, input_length) , tf.intaa , name='''input_ids''' ), tf.TensorSpec((None, input_length) , tf.intaa , name='''attention_mask''' ), ) , jit_compile=_UpperCamelCase , ) def snake_case__( self : List[Any] , _UpperCamelCase : int , _UpperCamelCase : Union[str, Any] ) ->List[Any]: snake_case_ = self.model.generate( input_ids=_UpperCamelCase , attention_mask=_UpperCamelCase , max_new_tokens=_UpperCamelCase , return_dict_in_generate=_UpperCamelCase , ) return {"sequences": outputs["sequences"]} snake_case_ = [[2, 0], [1_0_2, 1_0_3]] snake_case_ = [[1, 0], [1, 1]] snake_case_ = DummyModel(model=_UpperCamelCase ) with tempfile.TemporaryDirectory() as tmp_dir: tf.saved_model.save(_UpperCamelCase , _UpperCamelCase , signatures={'''serving_default''': dummy_model.serving} ) snake_case_ = tf.saved_model.load(_UpperCamelCase ).signatures['''serving_default'''] for batch_size in range(1 , len(_UpperCamelCase ) + 1 ): snake_case_ = { '''input_ids''': tf.constant(dummy_input_ids[:batch_size] ), '''attention_mask''': tf.constant(dummy_attention_masks[:batch_size] ), } snake_case_ = serving_func(**_UpperCamelCase )['''sequences'''] snake_case_ = test_model.generate(**_UpperCamelCase , max_new_tokens=_UpperCamelCase ) tf.debugging.assert_equal(_UpperCamelCase , _UpperCamelCase ) @slow def snake_case__( self : List[str] ) ->int: # TF-only test: tf.saved_model export snake_case_ = TFAutoModelForCausalLM.from_pretrained('''hf-internal-testing/tiny-random-gpt2''' ) snake_case_ = 1 snake_case_ = 2 class snake_case_ ( tf.Module ): '''simple docstring''' def __init__( self : str , _UpperCamelCase : Any ) ->List[str]: super(_UpperCamelCase , self ).__init__() snake_case_ = model @tf.function( input_signature=( tf.TensorSpec((batch_size, None) , tf.intaa , name='''input_ids''' ), tf.TensorSpec((batch_size, None) , tf.intaa , name='''attention_mask''' ), ) , jit_compile=_UpperCamelCase , ) def snake_case__( self : int , _UpperCamelCase : Tuple , _UpperCamelCase : List[Any] ) ->Optional[int]: snake_case_ = self.model.generate( input_ids=_UpperCamelCase , attention_mask=_UpperCamelCase , max_new_tokens=_UpperCamelCase , return_dict_in_generate=_UpperCamelCase , ) return {"sequences": outputs["sequences"]} snake_case_ = [[2], [1_0_2, 1_0_3]] snake_case_ = [[1], [1, 1]] snake_case_ = DummyModel(model=_UpperCamelCase ) with tempfile.TemporaryDirectory() as tmp_dir: tf.saved_model.save(_UpperCamelCase , _UpperCamelCase , signatures={'''serving_default''': dummy_model.serving} ) snake_case_ = tf.saved_model.load(_UpperCamelCase ).signatures['''serving_default'''] for input_row in range(len(_UpperCamelCase ) ): snake_case_ = { '''input_ids''': tf.constant([dummy_input_ids[input_row]] ), '''attention_mask''': tf.constant([dummy_attention_masks[input_row]] ), } snake_case_ = serving_func(**_UpperCamelCase )['''sequences'''] snake_case_ = test_model.generate(**_UpperCamelCase , max_new_tokens=_UpperCamelCase ) tf.debugging.assert_equal(_UpperCamelCase , _UpperCamelCase ) @slow @require_tensorflow_text def snake_case__( self : Optional[Any] ) ->List[Any]: # TF-only test: tf.saved_model export with tempfile.TemporaryDirectory() as tmp_dir: # file needed to load the TF tokenizer hf_hub_download(repo_id='''google/flan-t5-small''' , filename='''spiece.model''' , local_dir=_UpperCamelCase ) class snake_case_ ( tf.keras.layers.Layer ): '''simple docstring''' def __init__( self : Tuple ) ->List[Any]: super().__init__() snake_case_ = text.SentencepieceTokenizer( model=tf.io.gfile.GFile(os.path.join(_UpperCamelCase , '''spiece.model''' ) , '''rb''' ).read() ) snake_case_ = TFAutoModelForSeqaSeqLM.from_pretrained('''hf-internal-testing/tiny-random-t5''' ) def snake_case__( self : Optional[Any] , _UpperCamelCase : List[Any] , *_UpperCamelCase : Optional[int] , **_UpperCamelCase : str ) ->List[Any]: snake_case_ = self.tokenizer.tokenize(_UpperCamelCase ) snake_case_, snake_case_ = text.pad_model_inputs( _UpperCamelCase , max_seq_length=6_4 , pad_value=self.model.config.pad_token_id ) snake_case_ = self.model.generate(input_ids=_UpperCamelCase , attention_mask=_UpperCamelCase ) return self.tokenizer.detokenize(_UpperCamelCase ) snake_case_ = CompleteSentenceTransformer() snake_case_ = tf.keras.layers.Input(shape=(1,) , dtype=tf.string , name='''inputs''' ) snake_case_ = complete_model(_UpperCamelCase ) snake_case_ = tf.keras.Model(_UpperCamelCase , _UpperCamelCase ) keras_model.save(_UpperCamelCase ) def snake_case__( self : Any ) ->List[Any]: # Has PT equivalent: this test relies on random sampling snake_case_ = { '''do_sample''': True, '''num_beams''': 1, '''top_p''': 0.7, '''top_k''': 1_0, '''temperature''': 0.7, } snake_case_ = 1_4 snake_case_ = AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-gpt2''' ) snake_case_ = '''Hello, my dog is cute and''' snake_case_ = tokenizer(_UpperCamelCase , return_tensors='''tf''' ) snake_case_ = TFAutoModelForCausalLM.from_pretrained('''hf-internal-testing/tiny-random-gpt2''' ) snake_case_ = 6_3_8 # forces the generation to happen on CPU, to avoid GPU-related quirks with tf.device(''':/CPU:0''' ): tf.random.set_seed(0 ) snake_case_ = model.generate(**_UpperCamelCase , eos_token_id=_UpperCamelCase , **_UpperCamelCase ) self.assertTrue(expectation == len(generated_tokens[0] ) ) snake_case_ = [6_3_8, 1_9_8] with tf.device(''':/CPU:0''' ): tf.random.set_seed(0 ) snake_case_ = model.generate(**_UpperCamelCase , eos_token_id=_UpperCamelCase , **_UpperCamelCase ) self.assertTrue(expectation == len(generated_tokens[0] ) ) def snake_case__( self : str ) ->Dict: # Has PT equivalent: ample use of framework-specific code snake_case_ = AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-bart''' ) snake_case_ = '''Hugging Face is a technology company based in New York and Paris.''' snake_case_ = bart_tokenizer(_UpperCamelCase , return_tensors='''tf''' ).input_ids snake_case_ = TFBartForConditionalGeneration.from_pretrained('''hf-internal-testing/tiny-random-bart''' ) snake_case_ = bart_model.generate(_UpperCamelCase ).numpy() class snake_case_ ( __A ): '''simple docstring''' def snake_case__( self : str , _UpperCamelCase : Any , _UpperCamelCase : Tuple=None , **_UpperCamelCase : Optional[int] ) ->List[str]: return super().call(_UpperCamelCase , **_UpperCamelCase ) snake_case_ = FakeBart.from_pretrained('''hf-internal-testing/tiny-random-bart''' ) snake_case_ = bart_model.generate(_UpperCamelCase , foo='''bar''' ).numpy() self.assertTrue(np.array_equal(_UpperCamelCase , _UpperCamelCase ) ) class snake_case_ ( bart_model.model.encoder.__class__ ): '''simple docstring''' def snake_case__( self : Union[str, Any] , _UpperCamelCase : str , **_UpperCamelCase : Tuple ) ->Optional[Any]: return super().call(_UpperCamelCase , **_UpperCamelCase ) snake_case_ = FakeEncoder(bart_model.config , bart_model.model.shared ) snake_case_ = fake_encoder # Normal generation still works (the output will be different because the encoder weights are different) snake_case_ = bart_model.generate(_UpperCamelCase ).numpy() with self.assertRaises(_UpperCamelCase ): # FakeEncoder.call() accepts **kwargs -> no filtering -> value error due to unexpected input "foo" bart_model.generate(_UpperCamelCase , foo='''bar''' )
39
1
import inspect import unittest from transformers import BitConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_backbone_common import BackboneTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import BitBackbone, BitForImageClassification, BitImageProcessor, BitModel from transformers.models.bit.modeling_bit import BIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image class snake_case_ : '''simple docstring''' def __init__( self : Union[str, Any] , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : str=3 , _UpperCamelCase : List[Any]=3_2 , _UpperCamelCase : Union[str, Any]=3 , _UpperCamelCase : Union[str, Any]=1_0 , _UpperCamelCase : Any=[8, 1_6, 3_2, 6_4] , _UpperCamelCase : Any=[1, 1, 2, 1] , _UpperCamelCase : List[Any]=True , _UpperCamelCase : Any=True , _UpperCamelCase : Any="relu" , _UpperCamelCase : List[Any]=3 , _UpperCamelCase : Any=None , _UpperCamelCase : Dict=["stage2", "stage3", "stage4"] , _UpperCamelCase : str=[2, 3, 4] , _UpperCamelCase : List[str]=1 , ) ->Dict: snake_case_ = parent snake_case_ = batch_size snake_case_ = image_size snake_case_ = num_channels snake_case_ = embeddings_size snake_case_ = hidden_sizes snake_case_ = depths snake_case_ = is_training snake_case_ = use_labels snake_case_ = hidden_act snake_case_ = num_labels snake_case_ = scope snake_case_ = len(_UpperCamelCase ) snake_case_ = out_features snake_case_ = out_indices snake_case_ = num_groups def snake_case__( self : Optional[int] ) ->Optional[int]: snake_case_ = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) snake_case_ = None if self.use_labels: snake_case_ = ids_tensor([self.batch_size] , self.num_labels ) snake_case_ = self.get_config() return config, pixel_values, labels def snake_case__( self : Tuple ) ->List[str]: return BitConfig( num_channels=self.num_channels , embeddings_size=self.embeddings_size , hidden_sizes=self.hidden_sizes , depths=self.depths , hidden_act=self.hidden_act , num_labels=self.num_labels , out_features=self.out_features , out_indices=self.out_indices , num_groups=self.num_groups , ) def snake_case__( self : Dict , _UpperCamelCase : Tuple , _UpperCamelCase : str , _UpperCamelCase : Any ) ->Tuple: snake_case_ = BitModel(config=_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() snake_case_ = model(_UpperCamelCase ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.hidden_sizes[-1], self.image_size // 3_2, self.image_size // 3_2) , ) def snake_case__( self : int , _UpperCamelCase : Tuple , _UpperCamelCase : List[str] , _UpperCamelCase : List[str] ) ->Optional[Any]: snake_case_ = self.num_labels snake_case_ = BitForImageClassification(_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() snake_case_ = model(_UpperCamelCase , labels=_UpperCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def snake_case__( self : List[str] , _UpperCamelCase : int , _UpperCamelCase : Tuple , _UpperCamelCase : str ) ->Optional[Any]: snake_case_ = BitBackbone(config=_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() snake_case_ = model(_UpperCamelCase ) # verify feature maps self.parent.assertEqual(len(result.feature_maps ) , len(config.out_features ) ) self.parent.assertListEqual(list(result.feature_maps[0].shape ) , [self.batch_size, self.hidden_sizes[1], 4, 4] ) # verify channels self.parent.assertEqual(len(model.channels ) , len(config.out_features ) ) self.parent.assertListEqual(model.channels , config.hidden_sizes[1:] ) # verify backbone works with out_features=None snake_case_ = None snake_case_ = BitBackbone(config=_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() snake_case_ = model(_UpperCamelCase ) # verify feature maps self.parent.assertEqual(len(result.feature_maps ) , 1 ) self.parent.assertListEqual(list(result.feature_maps[0].shape ) , [self.batch_size, self.hidden_sizes[-1], 1, 1] ) # verify channels self.parent.assertEqual(len(model.channels ) , 1 ) self.parent.assertListEqual(model.channels , [config.hidden_sizes[-1]] ) def snake_case__( self : str ) ->Optional[Any]: snake_case_ = self.prepare_config_and_inputs() snake_case_, snake_case_, snake_case_ = config_and_inputs snake_case_ = {'''pixel_values''': pixel_values} return config, inputs_dict @require_torch class snake_case_ ( __A , __A , unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = (BitModel, BitForImageClassification, BitBackbone) if is_torch_available() else () SCREAMING_SNAKE_CASE : List[str] = ( {"feature-extraction": BitModel, "image-classification": BitForImageClassification} if is_torch_available() else {} ) SCREAMING_SNAKE_CASE : List[str] = False SCREAMING_SNAKE_CASE : Any = False SCREAMING_SNAKE_CASE : List[str] = False SCREAMING_SNAKE_CASE : Tuple = False SCREAMING_SNAKE_CASE : Dict = False def snake_case__( self : Union[str, Any] ) ->List[Any]: snake_case_ = BitModelTester(self ) snake_case_ = ConfigTester(self , config_class=_UpperCamelCase , has_text_modality=_UpperCamelCase ) def snake_case__( self : List[Any] ) ->Dict: self.create_and_test_config_common_properties() self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() def snake_case__( self : Any ) ->Union[str, Any]: return @unittest.skip(reason='''Bit does not output attentions''' ) def snake_case__( self : int ) ->Tuple: pass @unittest.skip(reason='''Bit does not use inputs_embeds''' ) def snake_case__( self : str ) ->Optional[Any]: pass @unittest.skip(reason='''Bit does not support input and output embeddings''' ) def snake_case__( self : Union[str, Any] ) ->Optional[Any]: pass def snake_case__( self : Optional[Any] ) ->Optional[int]: snake_case_, snake_case_ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: snake_case_ = model_class(_UpperCamelCase ) snake_case_ = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic snake_case_ = [*signature.parameters.keys()] snake_case_ = ['''pixel_values'''] self.assertListEqual(arg_names[:1] , _UpperCamelCase ) def snake_case__( self : Tuple ) ->str: snake_case_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_UpperCamelCase ) def snake_case__( self : str ) ->Optional[Any]: snake_case_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_backbone(*_UpperCamelCase ) def snake_case__( self : Tuple ) ->int: snake_case_, snake_case_ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: snake_case_ = model_class(config=_UpperCamelCase ) for name, module in model.named_modules(): if isinstance(_UpperCamelCase , (nn.BatchNormad, nn.GroupNorm) ): self.assertTrue( torch.all(module.weight == 1 ) , msg=f'''Parameter {name} of model {model_class} seems not properly initialized''' , ) self.assertTrue( torch.all(module.bias == 0 ) , msg=f'''Parameter {name} of model {model_class} seems not properly initialized''' , ) def snake_case__( self : Union[str, Any] ) ->str: def check_hidden_states_output(_UpperCamelCase : List[Any] , _UpperCamelCase : List[Any] , _UpperCamelCase : Optional[Any] ): snake_case_ = model_class(_UpperCamelCase ) model.to(_UpperCamelCase ) model.eval() with torch.no_grad(): snake_case_ = model(**self._prepare_for_class(_UpperCamelCase , _UpperCamelCase ) ) snake_case_ = outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states snake_case_ = self.model_tester.num_stages self.assertEqual(len(_UpperCamelCase ) , expected_num_stages + 1 ) # Bit's feature maps are of shape (batch_size, num_channels, height, width) self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [self.model_tester.image_size // 4, self.model_tester.image_size // 4] , ) snake_case_, snake_case_ = self.model_tester.prepare_config_and_inputs_for_common() snake_case_ = ['''preactivation''', '''bottleneck'''] for model_class in self.all_model_classes: for layer_type in layers_type: snake_case_ = layer_type snake_case_ = True check_hidden_states_output(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] snake_case_ = True check_hidden_states_output(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) @unittest.skip(reason='''Bit does not use feedforward chunking''' ) def snake_case__( self : str ) ->Optional[Any]: pass def snake_case__( self : Dict ) ->Dict: snake_case_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_UpperCamelCase ) @slow def snake_case__( self : Optional[int] ) ->Any: for model_name in BIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: snake_case_ = BitModel.from_pretrained(_UpperCamelCase ) self.assertIsNotNone(_UpperCamelCase ) def __SCREAMING_SNAKE_CASE (): snake_case_ = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_torch @require_vision class snake_case_ ( unittest.TestCase ): '''simple docstring''' @cached_property def snake_case__( self : Optional[Any] ) ->int: return ( BitImageProcessor.from_pretrained(BIT_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) if is_vision_available() else None ) @slow def snake_case__( self : str ) ->Optional[int]: snake_case_ = BitForImageClassification.from_pretrained(BIT_PRETRAINED_MODEL_ARCHIVE_LIST[0] ).to(_UpperCamelCase ) snake_case_ = self.default_image_processor snake_case_ = prepare_img() snake_case_ = image_processor(images=_UpperCamelCase , return_tensors='''pt''' ).to(_UpperCamelCase ) # forward pass with torch.no_grad(): snake_case_ = model(**_UpperCamelCase ) # verify the logits snake_case_ = torch.Size((1, 1_0_0_0) ) self.assertEqual(outputs.logits.shape , _UpperCamelCase ) snake_case_ = torch.tensor([[-0.6526, -0.5263, -1.4398]] ).to(_UpperCamelCase ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , _UpperCamelCase , atol=1e-4 ) ) @require_torch class snake_case_ ( __A , unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = (BitBackbone,) if is_torch_available() else () SCREAMING_SNAKE_CASE : Optional[Any] = BitConfig SCREAMING_SNAKE_CASE : Optional[int] = False def snake_case__( self : Optional[Any] ) ->Tuple: snake_case_ = BitModelTester(self )
39
import unittest from transformers import DonutProcessor lowerCAmelCase_ = '''naver-clova-ix/donut-base''' class snake_case_ ( unittest.TestCase ): '''simple docstring''' def snake_case__( self : Union[str, Any] ) ->Any: snake_case_ = DonutProcessor.from_pretrained(_UpperCamelCase ) def snake_case__( self : Dict ) ->str: snake_case_ = { '''name''': '''John Doe''', '''age''': '''99''', '''city''': '''Atlanta''', '''state''': '''GA''', '''zip''': '''30301''', '''phone''': '''123-4567''', '''nicknames''': [{'''nickname''': '''Johnny'''}, {'''nickname''': '''JD'''}], } snake_case_ = ( '''<s_name>John Doe</s_name><s_age>99</s_age><s_city>Atlanta</s_city>''' '''<s_state>GA</s_state><s_zip>30301</s_zip><s_phone>123-4567</s_phone>''' '''<s_nicknames><s_nickname>Johnny</s_nickname>''' '''<sep/><s_nickname>JD</s_nickname></s_nicknames>''' ) snake_case_ = self.processor.tokenajson(_UpperCamelCase ) self.assertDictEqual(_UpperCamelCase , _UpperCamelCase )
39
1