code
stringlengths
86
54.5k
code_codestyle
int64
0
371
style_context
stringlengths
87
49.2k
style_context_codestyle
int64
0
349
label
int64
0
1
'''simple docstring''' from dataclasses import dataclass from typing import List, Optional, Union import numpy as np import PIL from ...utils import BaseOutput, OptionalDependencyNotAvailable, is_torch_available, is_transformers_available from .timesteps import ( fastaa_timesteps, smartaa_timesteps, smartaa_timesteps, smartaaa_timesteps, smartaaa_timesteps, superaa_timesteps, superaa_timesteps, superaaa_timesteps, ) @dataclass class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' __lowercase : Union[List[PIL.Image.Image], np.ndarray] __lowercase : Optional[List[bool]] __lowercase : Optional[List[bool]] try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import * # noqa F403 else: from .pipeline_if import IFPipeline from .pipeline_if_imgaimg import IFImgaImgPipeline from .pipeline_if_imgaimg_superresolution import IFImgaImgSuperResolutionPipeline from .pipeline_if_inpainting import IFInpaintingPipeline from .pipeline_if_inpainting_superresolution import IFInpaintingSuperResolutionPipeline from .pipeline_if_superresolution import IFSuperResolutionPipeline from .safety_checker import IFSafetyChecker from .watermark import IFWatermarker
18
'''simple docstring''' from ...utils import ( OptionalDependencyNotAvailable, is_torch_available, is_transformers_available, is_transformers_version, ) try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import ShapEPipeline else: from .camera import create_pan_cameras from .pipeline_shap_e import ShapEPipeline from .pipeline_shap_e_img2img import ShapEImgaImgPipeline from .renderer import ( BoundingBoxVolume, ImportanceRaySampler, MLPNeRFModelOutput, MLPNeRSTFModel, ShapEParamsProjModel, ShapERenderer, StratifiedRaySampler, VoidNeRFModel, )
18
1
'''simple docstring''' import os from bleurt import score # From: git+https://github.com/google-research/bleurt.git import datasets __snake_case : Optional[Any] = datasets.logging.get_logger(__name__) __snake_case : Optional[Any] = '\\n@inproceedings{bleurt,\n title={BLEURT: Learning Robust Metrics for Text Generation},\n author={Thibault Sellam and Dipanjan Das and Ankur P. Parikh},\n booktitle={ACL},\n year={2020},\n url={https://arxiv.org/abs/2004.04696}\n}\n' __snake_case : Optional[int] = '\\nBLEURT a learnt evaluation metric for Natural Language Generation. It is built using multiple phases of transfer learning starting from a pretrained BERT model (Devlin et al. 2018)\nand then employing another pre-training phrase using synthetic data. Finally it is trained on WMT human annotations. You may run BLEURT out-of-the-box or fine-tune\nit for your specific application (the latter is expected to perform better).\n\nSee the project\'s README at https://github.com/google-research/bleurt#readme for more information.\n' __snake_case : int = '\nBLEURT score.\n\nArgs:\n `predictions` (list of str): prediction/candidate sentences\n `references` (list of str): reference sentences\n `checkpoint` BLEURT checkpoint. Will default to BLEURT-tiny if None.\n\nReturns:\n \'scores\': List of scores.\nExamples:\n\n >>> predictions = ["hello there", "general kenobi"]\n >>> references = ["hello there", "general kenobi"]\n >>> bleurt = datasets.load_metric("bleurt")\n >>> results = bleurt.compute(predictions=predictions, references=references)\n >>> print([round(v, 2) for v in results["scores"]])\n [1.03, 1.04]\n' __snake_case : str = { 'bleurt-tiny-128': 'https://storage.googleapis.com/bleurt-oss/bleurt-tiny-128.zip', 'bleurt-tiny-512': 'https://storage.googleapis.com/bleurt-oss/bleurt-tiny-512.zip', 'bleurt-base-128': 'https://storage.googleapis.com/bleurt-oss/bleurt-base-128.zip', 'bleurt-base-512': 'https://storage.googleapis.com/bleurt-oss/bleurt-base-512.zip', 'bleurt-large-128': 'https://storage.googleapis.com/bleurt-oss/bleurt-large-128.zip', 'bleurt-large-512': 'https://storage.googleapis.com/bleurt-oss/bleurt-large-512.zip', 'BLEURT-20-D3': 'https://storage.googleapis.com/bleurt-oss-21/BLEURT-20-D3.zip', 'BLEURT-20-D6': 'https://storage.googleapis.com/bleurt-oss-21/BLEURT-20-D6.zip', 'BLEURT-20-D12': 'https://storage.googleapis.com/bleurt-oss-21/BLEURT-20-D12.zip', 'BLEURT-20': 'https://storage.googleapis.com/bleurt-oss-21/BLEURT-20.zip', } @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __UpperCAmelCase ( datasets.Metric ): '''simple docstring''' def __A ( self ) -> Optional[Any]: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , homepage='''https://github.com/google-research/bleurt''' , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Value('''string''' , id='''sequence''' ), '''references''': datasets.Value('''string''' , id='''sequence''' ), } ) , codebase_urls=['''https://github.com/google-research/bleurt'''] , reference_urls=['''https://github.com/google-research/bleurt''', '''https://arxiv.org/abs/2004.04696'''] , ) def __A ( self , _SCREAMING_SNAKE_CASE ) -> Optional[int]: # check that config name specifies a valid BLEURT model if self.config_name == "default": logger.warning( '''Using default BLEURT-Base checkpoint for sequence maximum length 128. ''' '''You can use a bigger model for better results with e.g.: datasets.load_metric(\'bleurt\', \'bleurt-large-512\').''' ) A_ = '''bleurt-base-128''' if self.config_name.lower() in CHECKPOINT_URLS: A_ = self.config_name.lower() elif self.config_name.upper() in CHECKPOINT_URLS: A_ = self.config_name.upper() else: raise KeyError( F'''{self.config_name} model not found. You should supply the name of a model checkpoint for bleurt in {CHECKPOINT_URLS.keys()}''' ) # download the model checkpoint specified by self.config_name and set up the scorer A_ = dl_manager.download_and_extract(CHECKPOINT_URLS[checkpoint_name] ) A_ = score.BleurtScorer(os.path.join(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Optional[Any]: A_ = self.scorer.score(references=_SCREAMING_SNAKE_CASE , candidates=_SCREAMING_SNAKE_CASE ) return {"scores": scores}
18
'''simple docstring''' import argparse from pathlib import Path import torch from transformers import OPTConfig, OPTModel from transformers.utils import logging logging.set_verbosity_info() __snake_case : Any = logging.get_logger(__name__) def _UpperCAmelCase ( _UpperCamelCase : Dict ) -> List[str]: A_ = torch.load(_UpperCamelCase, map_location='''cpu''' ) if "model" in sd.keys(): A_ = torch.load(_UpperCamelCase, map_location='''cpu''' )['''model'''] # pop unnecessary weights A_ = [ '''decoder.version''', '''decoder.output_projection.weight''', ] for key in keys_to_delete: if key in sd: sd.pop(_UpperCamelCase ) A_ = { '''decoder.project_in_dim.weight''': '''decoder.project_in.weight''', '''decoder.project_out_dim.weight''': '''decoder.project_out.weight''', '''decoder.layer_norm.weight''': '''decoder.final_layer_norm.weight''', '''decoder.layer_norm.bias''': '''decoder.final_layer_norm.bias''', } for old_key, new_key in keys_to_rename.items(): if old_key in sd: A_ = sd.pop(_UpperCamelCase ) A_ = list(sd.keys() ) for key in keys: if ".qkv_proj." in key: A_ = sd[key] # We split QKV in separate Q,K,V A_ = key.replace('''.qkv_proj.''', '''.q_proj.''' ) A_ = key.replace('''.qkv_proj.''', '''.k_proj.''' ) A_ = key.replace('''.qkv_proj.''', '''.v_proj.''' ) A_ = value.shape[0] assert depth % 3 == 0 # `SequeuceParallelTransformerBlock` has QKV weight is separated in K,V,Q despite the naming: # https://cs.github.com/facebookresearch/metaseq/blob/51871bd73cd04c038f239ea2a26db1d7f6b37927/metaseq/modules/sequence_parallel_transformer_layer.py#L97 A_ ,A_ ,A_ = torch.split(_UpperCamelCase, depth // 3, dim=0 ) A_ = q A_ = k A_ = v del sd[key] return sd @torch.no_grad() def _UpperCAmelCase ( _UpperCamelCase : Optional[int], _UpperCamelCase : Optional[Any], _UpperCamelCase : List[str]=None ) -> Dict: A_ = load_checkpoint(_UpperCamelCase ) if config is not None: A_ = OPTConfig.from_pretrained(_UpperCamelCase ) else: A_ = OPTConfig() A_ = OPTModel(_UpperCamelCase ).half().eval() model.load_state_dict(_UpperCamelCase ) # Check results Path(_UpperCamelCase ).mkdir(exist_ok=_UpperCamelCase ) model.save_pretrained(_UpperCamelCase ) if __name__ == "__main__": __snake_case : Optional[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '--fairseq_path', type=str, help=( 'path to fairseq checkpoint in correct format. You can find all checkpoints in the correct format here:' ' https://huggingface.co/models?other=opt_metasq' ), ) parser.add_argument('--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.') parser.add_argument('--hf_config', default=None, type=str, help='Define HF config.') __snake_case : Optional[Any] = parser.parse_args() convert_opt_checkpoint(args.fairseq_path, args.pytorch_dump_folder_path, config=args.hf_config)
18
1
'''simple docstring''' from math import sqrt def _UpperCAmelCase ( _UpperCamelCase : int ) -> bool: assert isinstance(_UpperCamelCase, _UpperCamelCase ) and ( number >= 0 ), "'number' must been an int and positive" A_ = True # 0 and 1 are none primes. if number <= 1: A_ = False for divisor in range(2, int(round(sqrt(_UpperCamelCase ) ) ) + 1 ): # if 'number' divisible by 'divisor' then sets 'status' # of false and break up the loop. if number % divisor == 0: A_ = False break # precondition assert isinstance(_UpperCamelCase, _UpperCamelCase ), "'status' must been from type bool" return status def _UpperCAmelCase ( _UpperCamelCase : Tuple ) -> Union[str, Any]: assert isinstance(_UpperCamelCase, _UpperCamelCase ) and (n > 2), "'N' must been an int and > 2" # beginList: contains all natural numbers from 2 up to N A_ = list(range(2, n + 1 ) ) A_ = [] # this list will be returns. # actual sieve of erathostenes for i in range(len(_UpperCamelCase ) ): for j in range(i + 1, len(_UpperCamelCase ) ): if (begin_list[i] != 0) and (begin_list[j] % begin_list[i] == 0): A_ = 0 # filters actual prime numbers. A_ = [x for x in begin_list if x != 0] # precondition assert isinstance(_UpperCamelCase, _UpperCamelCase ), "'ans' must been from type list" return ans def _UpperCAmelCase ( _UpperCamelCase : List[str] ) -> List[Any]: assert isinstance(_UpperCamelCase, _UpperCamelCase ) and (n > 2), "'N' must been an int and > 2" A_ = [] # iterates over all numbers between 2 up to N+1 # if a number is prime then appends to list 'ans' for number in range(2, n + 1 ): if is_prime(_UpperCamelCase ): ans.append(_UpperCamelCase ) # precondition assert isinstance(_UpperCamelCase, _UpperCamelCase ), "'ans' must been from type list" return ans def _UpperCAmelCase ( _UpperCamelCase : Union[str, Any] ) -> Dict: assert isinstance(_UpperCamelCase, _UpperCamelCase ) and number >= 0, "'number' must been an int and >= 0" A_ = [] # this list will be returns of the function. # potential prime number factors. A_ = 2 A_ = number if number == 0 or number == 1: ans.append(_UpperCamelCase ) # if 'number' not prime then builds the prime factorization of 'number' elif not is_prime(_UpperCamelCase ): while quotient != 1: if is_prime(_UpperCamelCase ) and (quotient % factor == 0): ans.append(_UpperCamelCase ) quotient /= factor else: factor += 1 else: ans.append(_UpperCamelCase ) # precondition assert isinstance(_UpperCamelCase, _UpperCamelCase ), "'ans' must been from type list" return ans def _UpperCAmelCase ( _UpperCamelCase : Optional[int] ) -> List[str]: assert isinstance(_UpperCamelCase, _UpperCamelCase ) and ( number >= 0 ), "'number' bust been an int and >= 0" A_ = 0 # prime factorization of 'number' A_ = prime_factorization(_UpperCamelCase ) A_ = max(_UpperCamelCase ) # precondition assert isinstance(_UpperCamelCase, _UpperCamelCase ), "'ans' must been from type int" return ans def _UpperCAmelCase ( _UpperCamelCase : Union[str, Any] ) -> int: assert isinstance(_UpperCamelCase, _UpperCamelCase ) and ( number >= 0 ), "'number' bust been an int and >= 0" A_ = 0 # prime factorization of 'number' A_ = prime_factorization(_UpperCamelCase ) A_ = min(_UpperCamelCase ) # precondition assert isinstance(_UpperCamelCase, _UpperCamelCase ), "'ans' must been from type int" return ans def _UpperCAmelCase ( _UpperCamelCase : Tuple ) -> Any: assert isinstance(_UpperCamelCase, _UpperCamelCase ), "'number' must been an int" assert isinstance(number % 2 == 0, _UpperCamelCase ), "compare bust been from type bool" return number % 2 == 0 def _UpperCAmelCase ( _UpperCamelCase : Any ) -> Union[str, Any]: assert isinstance(_UpperCamelCase, _UpperCamelCase ), "'number' must been an int" assert isinstance(number % 2 != 0, _UpperCamelCase ), "compare bust been from type bool" return number % 2 != 0 def _UpperCAmelCase ( _UpperCamelCase : Optional[int] ) -> Optional[int]: assert ( isinstance(_UpperCamelCase, _UpperCamelCase ) and (number > 2) and is_even(_UpperCamelCase ) ), "'number' must been an int, even and > 2" A_ = [] # this list will returned # creates a list of prime numbers between 2 up to 'number' A_ = get_prime_numbers(_UpperCamelCase ) A_ = len(_UpperCamelCase ) # run variable for while-loops. A_ = 0 A_ = None # exit variable. for break up the loops A_ = True while i < len_pn and loop: A_ = i + 1 while j < len_pn and loop: if prime_numbers[i] + prime_numbers[j] == number: A_ = False ans.append(prime_numbers[i] ) ans.append(prime_numbers[j] ) j += 1 i += 1 # precondition assert ( isinstance(_UpperCamelCase, _UpperCamelCase ) and (len(_UpperCamelCase ) == 2) and (ans[0] + ans[1] == number) and is_prime(ans[0] ) and is_prime(ans[1] ) ), "'ans' must contains two primes. And sum of elements must been eq 'number'" return ans def _UpperCAmelCase ( _UpperCamelCase : List[str], _UpperCamelCase : Dict ) -> Union[str, Any]: assert ( isinstance(_UpperCamelCase, _UpperCamelCase ) and isinstance(_UpperCamelCase, _UpperCamelCase ) and (numbera >= 0) and (numbera >= 0) ), "'number1' and 'number2' must been positive integer." A_ = 0 while numbera != 0: A_ = numbera % numbera A_ = numbera A_ = rest # precondition assert isinstance(_UpperCamelCase, _UpperCamelCase ) and ( numbera >= 0 ), "'number' must been from type int and positive" return numbera def _UpperCAmelCase ( _UpperCamelCase : Optional[Any], _UpperCamelCase : List[str] ) -> int: assert ( isinstance(_UpperCamelCase, _UpperCamelCase ) and isinstance(_UpperCamelCase, _UpperCamelCase ) and (numbera >= 1) and (numbera >= 1) ), "'number1' and 'number2' must been positive integer." A_ = 1 # actual answer that will be return. # for kgV (x,1) if numbera > 1 and numbera > 1: # builds the prime factorization of 'number1' and 'number2' A_ = prime_factorization(_UpperCamelCase ) A_ = prime_factorization(_UpperCamelCase ) elif numbera == 1 or numbera == 1: A_ = [] A_ = [] A_ = max(_UpperCamelCase, _UpperCamelCase ) A_ = 0 A_ = 0 A_ = [] # captured numbers int both 'primeFac1' and 'primeFac2' # iterates through primeFac1 for n in prime_fac_a: if n not in done: if n in prime_fac_a: A_ = prime_fac_a.count(_UpperCamelCase ) A_ = prime_fac_a.count(_UpperCamelCase ) for _ in range(max(_UpperCamelCase, _UpperCamelCase ) ): ans *= n else: A_ = prime_fac_a.count(_UpperCamelCase ) for _ in range(_UpperCamelCase ): ans *= n done.append(_UpperCamelCase ) # iterates through primeFac2 for n in prime_fac_a: if n not in done: A_ = prime_fac_a.count(_UpperCamelCase ) for _ in range(_UpperCamelCase ): ans *= n done.append(_UpperCamelCase ) # precondition assert isinstance(_UpperCamelCase, _UpperCamelCase ) and ( ans >= 0 ), "'ans' must been from type int and positive" return ans def _UpperCAmelCase ( _UpperCamelCase : Optional[Any] ) -> int: assert isinstance(_UpperCamelCase, _UpperCamelCase ) and (n >= 0), "'number' must been a positive int" A_ = 0 A_ = 2 # this variable holds the answer while index < n: index += 1 ans += 1 # counts to the next number # if ans not prime then # runs to the next prime number. while not is_prime(_UpperCamelCase ): ans += 1 # precondition assert isinstance(_UpperCamelCase, _UpperCamelCase ) and is_prime( _UpperCamelCase ), "'ans' must been a prime number and from type int" return ans def _UpperCAmelCase ( _UpperCamelCase : List[str], _UpperCamelCase : Optional[Any] ) -> Tuple: assert ( is_prime(_UpperCamelCase ) and is_prime(_UpperCamelCase ) and (p_number_a < p_number_a) ), "The arguments must been prime numbers and 'pNumber1' < 'pNumber2'" A_ = p_number_a + 1 # jump to the next number A_ = [] # this list will be returns. # if number is not prime then # fetch the next prime number. while not is_prime(_UpperCamelCase ): number += 1 while number < p_number_a: ans.append(_UpperCamelCase ) number += 1 # fetch the next prime number. while not is_prime(_UpperCamelCase ): number += 1 # precondition assert ( isinstance(_UpperCamelCase, _UpperCamelCase ) and ans[0] != p_number_a and ans[len(_UpperCamelCase ) - 1] != p_number_a ), "'ans' must been a list without the arguments" # 'ans' contains not 'pNumber1' and 'pNumber2' ! return ans def _UpperCAmelCase ( _UpperCamelCase : Any ) -> Union[str, Any]: assert isinstance(_UpperCamelCase, _UpperCamelCase ) and (n >= 1), "'n' must been int and >= 1" A_ = [] # will be returned. for divisor in range(1, n + 1 ): if n % divisor == 0: ans.append(_UpperCamelCase ) # precondition assert ans[0] == 1 and ans[len(_UpperCamelCase ) - 1] == n, "Error in function getDivisiors(...)" return ans def _UpperCAmelCase ( _UpperCamelCase : Optional[int] ) -> Union[str, Any]: assert isinstance(_UpperCamelCase, _UpperCamelCase ) and ( number > 1 ), "'number' must been an int and >= 1" A_ = get_divisors(_UpperCamelCase ) # precondition assert ( isinstance(_UpperCamelCase, _UpperCamelCase ) and (divisors[0] == 1) and (divisors[len(_UpperCamelCase ) - 1] == number) ), "Error in help-function getDivisiors(...)" # summed all divisors up to 'number' (exclusive), hence [:-1] return sum(divisors[:-1] ) == number def _UpperCAmelCase ( _UpperCamelCase : Dict, _UpperCamelCase : Dict ) -> Tuple: assert ( isinstance(_UpperCamelCase, _UpperCamelCase ) and isinstance(_UpperCamelCase, _UpperCamelCase ) and (denominator != 0) ), "The arguments must been from type int and 'denominator' != 0" # build the greatest common divisor of numerator and denominator. A_ = gcd(abs(_UpperCamelCase ), abs(_UpperCamelCase ) ) # precondition assert ( isinstance(_UpperCamelCase, _UpperCamelCase ) and (numerator % gcd_of_fraction == 0) and (denominator % gcd_of_fraction == 0) ), "Error in function gcd(...,...)" return (numerator // gcd_of_fraction, denominator // gcd_of_fraction) def _UpperCAmelCase ( _UpperCamelCase : Dict ) -> Union[str, Any]: assert isinstance(_UpperCamelCase, _UpperCamelCase ) and (n >= 0), "'n' must been a int and >= 0" A_ = 1 # this will be return. for factor in range(1, n + 1 ): ans *= factor return ans def _UpperCAmelCase ( _UpperCamelCase : Tuple ) -> Any: assert isinstance(_UpperCamelCase, _UpperCamelCase ) and (n >= 0), "'n' must been an int and >= 0" A_ = 0 A_ = 1 A_ = 1 # this will be return for _ in range(n - 1 ): A_ = ans ans += fiba A_ = tmp return ans
18
'''simple docstring''' from typing import List, Optional, Tuple from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_herbert import HerbertTokenizer __snake_case : Optional[Any] = logging.get_logger(__name__) __snake_case : Tuple = {'vocab_file': 'vocab.json', 'merges_file': 'merges.txt', 'tokenizer_file': 'tokenizer.json'} __snake_case : Optional[Any] = { 'vocab_file': { 'allegro/herbert-base-cased': 'https://huggingface.co/allegro/herbert-base-cased/resolve/main/vocab.json' }, 'merges_file': { 'allegro/herbert-base-cased': 'https://huggingface.co/allegro/herbert-base-cased/resolve/main/merges.txt' }, } __snake_case : Tuple = {'allegro/herbert-base-cased': 514} __snake_case : List[str] = {} class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' __lowercase : Any = VOCAB_FILES_NAMES __lowercase : Union[str, Any] = PRETRAINED_VOCAB_FILES_MAP __lowercase : Dict = PRETRAINED_INIT_CONFIGURATION __lowercase : List[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowercase : Optional[int] = HerbertTokenizer def __init__( self , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE="<s>" , _SCREAMING_SNAKE_CASE="<unk>" , _SCREAMING_SNAKE_CASE="<pad>" , _SCREAMING_SNAKE_CASE="<mask>" , _SCREAMING_SNAKE_CASE="</s>" , **_SCREAMING_SNAKE_CASE , ) -> int: super().__init__( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , tokenizer_file=_SCREAMING_SNAKE_CASE , cls_token=_SCREAMING_SNAKE_CASE , unk_token=_SCREAMING_SNAKE_CASE , pad_token=_SCREAMING_SNAKE_CASE , mask_token=_SCREAMING_SNAKE_CASE , sep_token=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE , ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None ) -> List[int]: A_ = [self.cls_token_id] A_ = [self.sep_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 , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = False ) -> List[int]: 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] + ([0] * len(_SCREAMING_SNAKE_CASE )) + [1] def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None ) -> List[int]: 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 ) * [0] + len(token_ids_a + sep ) * [1] def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None ) -> Tuple[str]: A_ = self._tokenizer.model.save(_SCREAMING_SNAKE_CASE , name=_SCREAMING_SNAKE_CASE ) return tuple(_SCREAMING_SNAKE_CASE )
18
1
'''simple docstring''' # Copyright 2022 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 import os import platform import numpy as np import psutil import torch from accelerate import __version__ as version from accelerate.commands.config import default_config_file, load_config_from_file from ..utils import is_npu_available, is_xpu_available def _UpperCAmelCase ( _UpperCamelCase : Union[str, Any]=None ) -> List[Any]: if subparsers is not None: A_ = subparsers.add_parser('''env''' ) else: A_ = argparse.ArgumentParser('''Accelerate env command''' ) parser.add_argument( '''--config_file''', default=_UpperCamelCase, help='''The config file to use for the default values in the launching script.''' ) if subparsers is not None: parser.set_defaults(func=_UpperCamelCase ) return parser def _UpperCAmelCase ( _UpperCamelCase : Dict ) -> Dict: A_ = torch.__version__ A_ = torch.cuda.is_available() A_ = is_xpu_available() A_ = is_npu_available() A_ = '''Not found''' # Get the default from the config file. if args.config_file is not None or os.path.isfile(_UpperCamelCase ): A_ = load_config_from_file(args.config_file ).to_dict() A_ = { '''`Accelerate` version''': version, '''Platform''': platform.platform(), '''Python version''': platform.python_version(), '''Numpy version''': np.__version__, '''PyTorch version (GPU?)''': F'''{pt_version} ({pt_cuda_available})''', '''PyTorch XPU available''': str(_UpperCamelCase ), '''PyTorch NPU available''': str(_UpperCamelCase ), '''System RAM''': F'''{psutil.virtual_memory().total / 10_24 ** 3:.2f} GB''', } if pt_cuda_available: A_ = torch.cuda.get_device_name() print('''\nCopy-and-paste the text below in your GitHub issue\n''' ) print('''\n'''.join([F'''- {prop}: {val}''' for prop, val in info.items()] ) ) print('''- `Accelerate` default config:''' if args.config_file is None else '''- `Accelerate` config passed:''' ) A_ = ( '''\n'''.join([F'''\t- {prop}: {val}''' for prop, val in accelerate_config.items()] ) if isinstance(_UpperCamelCase, _UpperCamelCase ) else F'''\t{accelerate_config}''' ) print(_UpperCamelCase ) A_ = accelerate_config return info def _UpperCAmelCase ( ) -> int: A_ = env_command_parser() A_ = parser.parse_args() env_command(_UpperCamelCase ) return 0 if __name__ == "__main__": raise SystemExit(main())
18
'''simple docstring''' # Copyright 2022 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 import os import platform import numpy as np import psutil import torch from accelerate import __version__ as version from accelerate.commands.config import default_config_file, load_config_from_file from ..utils import is_npu_available, is_xpu_available def _UpperCAmelCase ( _UpperCamelCase : Union[str, Any]=None ) -> List[Any]: if subparsers is not None: A_ = subparsers.add_parser('''env''' ) else: A_ = argparse.ArgumentParser('''Accelerate env command''' ) parser.add_argument( '''--config_file''', default=_UpperCamelCase, help='''The config file to use for the default values in the launching script.''' ) if subparsers is not None: parser.set_defaults(func=_UpperCamelCase ) return parser def _UpperCAmelCase ( _UpperCamelCase : Dict ) -> Dict: A_ = torch.__version__ A_ = torch.cuda.is_available() A_ = is_xpu_available() A_ = is_npu_available() A_ = '''Not found''' # Get the default from the config file. if args.config_file is not None or os.path.isfile(_UpperCamelCase ): A_ = load_config_from_file(args.config_file ).to_dict() A_ = { '''`Accelerate` version''': version, '''Platform''': platform.platform(), '''Python version''': platform.python_version(), '''Numpy version''': np.__version__, '''PyTorch version (GPU?)''': F'''{pt_version} ({pt_cuda_available})''', '''PyTorch XPU available''': str(_UpperCamelCase ), '''PyTorch NPU available''': str(_UpperCamelCase ), '''System RAM''': F'''{psutil.virtual_memory().total / 10_24 ** 3:.2f} GB''', } if pt_cuda_available: A_ = torch.cuda.get_device_name() print('''\nCopy-and-paste the text below in your GitHub issue\n''' ) print('''\n'''.join([F'''- {prop}: {val}''' for prop, val in info.items()] ) ) print('''- `Accelerate` default config:''' if args.config_file is None else '''- `Accelerate` config passed:''' ) A_ = ( '''\n'''.join([F'''\t- {prop}: {val}''' for prop, val in accelerate_config.items()] ) if isinstance(_UpperCamelCase, _UpperCamelCase ) else F'''\t{accelerate_config}''' ) print(_UpperCamelCase ) A_ = accelerate_config return info def _UpperCAmelCase ( ) -> int: A_ = env_command_parser() A_ = parser.parse_args() env_command(_UpperCamelCase ) return 0 if __name__ == "__main__": raise SystemExit(main())
18
1
'''simple docstring''' from transformers import BertTokenizer, EncoderDecoderModel, SeqaSeqTrainer, SeqaSeqTrainingArguments from transformers.testing_utils import TestCasePlus, require_torch, slow from transformers.utils import is_datasets_available if is_datasets_available(): import datasets class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' @slow @require_torch def __A ( self ) -> int: A_ = EncoderDecoderModel.from_encoder_decoder_pretrained('''prajjwal1/bert-tiny''' , '''prajjwal1/bert-tiny''' ) A_ = BertTokenizer.from_pretrained('''bert-base-uncased''' ) A_ = bertabert.config.encoder.vocab_size A_ = tokenizer.sep_token_id A_ = tokenizer.cls_token_id A_ = 128 A_ = datasets.load_dataset('''cnn_dailymail''' , '''3.0.0''' , split='''train[:1%]''' ) A_ = datasets.load_dataset('''cnn_dailymail''' , '''3.0.0''' , split='''validation[:1%]''' ) A_ = train_dataset.select(range(32 ) ) A_ = val_dataset.select(range(16 ) ) A_ = 4 def _map_to_encoder_decoder_inputs(_SCREAMING_SNAKE_CASE ): # Tokenizer will automatically set [BOS] <text> [EOS] A_ = tokenizer(batch['''article'''] , padding='''max_length''' , truncation=_SCREAMING_SNAKE_CASE , max_length=512 ) A_ = tokenizer(batch['''highlights'''] , padding='''max_length''' , truncation=_SCREAMING_SNAKE_CASE , max_length=128 ) A_ = inputs.input_ids A_ = inputs.attention_mask A_ = outputs.input_ids A_ = outputs.input_ids.copy() A_ = [ [-100 if token == tokenizer.pad_token_id else token for token in labels] for labels in batch['''labels'''] ] A_ = outputs.attention_mask assert all(len(_SCREAMING_SNAKE_CASE ) == 512 for x in inputs.input_ids ) assert all(len(_SCREAMING_SNAKE_CASE ) == 128 for x in outputs.input_ids ) return batch def _compute_metrics(_SCREAMING_SNAKE_CASE ): A_ = pred.label_ids A_ = pred.predictions # all unnecessary tokens are removed A_ = tokenizer.batch_decode(_SCREAMING_SNAKE_CASE , skip_special_tokens=_SCREAMING_SNAKE_CASE ) A_ = tokenizer.batch_decode(_SCREAMING_SNAKE_CASE , skip_special_tokens=_SCREAMING_SNAKE_CASE ) A_ = sum([int(pred_str[i] == label_str[i] ) for i in range(len(_SCREAMING_SNAKE_CASE ) )] ) / len(_SCREAMING_SNAKE_CASE ) return {"accuracy": accuracy} # map train dataset A_ = train_dataset.map( _map_to_encoder_decoder_inputs , batched=_SCREAMING_SNAKE_CASE , batch_size=_SCREAMING_SNAKE_CASE , remove_columns=['''article''', '''highlights'''] , ) train_dataset.set_format( type='''torch''' , columns=['''input_ids''', '''attention_mask''', '''decoder_input_ids''', '''decoder_attention_mask''', '''labels'''] , ) # same for validation dataset A_ = val_dataset.map( _map_to_encoder_decoder_inputs , batched=_SCREAMING_SNAKE_CASE , batch_size=_SCREAMING_SNAKE_CASE , remove_columns=['''article''', '''highlights'''] , ) val_dataset.set_format( type='''torch''' , columns=['''input_ids''', '''attention_mask''', '''decoder_input_ids''', '''decoder_attention_mask''', '''labels'''] , ) A_ = self.get_auto_remove_tmp_dir() A_ = SeqaSeqTrainingArguments( output_dir=_SCREAMING_SNAKE_CASE , per_device_train_batch_size=_SCREAMING_SNAKE_CASE , per_device_eval_batch_size=_SCREAMING_SNAKE_CASE , predict_with_generate=_SCREAMING_SNAKE_CASE , evaluation_strategy='''steps''' , do_train=_SCREAMING_SNAKE_CASE , do_eval=_SCREAMING_SNAKE_CASE , warmup_steps=0 , eval_steps=2 , logging_steps=2 , ) # instantiate trainer A_ = SeqaSeqTrainer( model=_SCREAMING_SNAKE_CASE , args=_SCREAMING_SNAKE_CASE , compute_metrics=_compute_metrics , train_dataset=_SCREAMING_SNAKE_CASE , eval_dataset=_SCREAMING_SNAKE_CASE , tokenizer=_SCREAMING_SNAKE_CASE , ) # start training trainer.train()
18
'''simple docstring''' import inspect import math import tempfile import unittest import numpy as np from transformers import ViTMAEConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTMAEForPreTraining, ViTMAEModel from transformers.models.vit.modeling_vit import VIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class __UpperCAmelCase : '''simple docstring''' def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=13 , _SCREAMING_SNAKE_CASE=30 , _SCREAMING_SNAKE_CASE=2 , _SCREAMING_SNAKE_CASE=3 , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=32 , _SCREAMING_SNAKE_CASE=5 , _SCREAMING_SNAKE_CASE=4 , _SCREAMING_SNAKE_CASE=37 , _SCREAMING_SNAKE_CASE="gelu" , _SCREAMING_SNAKE_CASE=0.1 , _SCREAMING_SNAKE_CASE=0.1 , _SCREAMING_SNAKE_CASE=10 , _SCREAMING_SNAKE_CASE=0.02 , _SCREAMING_SNAKE_CASE=3 , _SCREAMING_SNAKE_CASE=0.6 , _SCREAMING_SNAKE_CASE=None , ) -> Tuple: 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_ = num_attention_heads A_ = intermediate_size A_ = hidden_act A_ = hidden_dropout_prob A_ = attention_probs_dropout_prob A_ = type_sequence_label_size A_ = initializer_range A_ = mask_ratio A_ = scope # in ViTMAE, the expected sequence length = (num_patches + 1) * (1 - config.mask_ratio), rounded above # (we add 1 for the [CLS] token) A_ = (image_size // patch_size) ** 2 A_ = int(math.ceil((1 - mask_ratio) * (num_patches + 1) ) ) def __A ( self ) -> Union[str, Any]: 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.type_sequence_label_size ) A_ = self.get_config() return config, pixel_values, labels def __A ( self ) -> Dict: return ViTMAEConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=_SCREAMING_SNAKE_CASE , initializer_range=self.initializer_range , mask_ratio=self.mask_ratio , ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Tuple: A_ = ViTMAEModel(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 __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Optional[Any]: A_ = ViTMAEForPreTraining(_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() A_ = model(_SCREAMING_SNAKE_CASE ) A_ = (self.image_size // self.patch_size) ** 2 A_ = self.patch_size**2 * self.num_channels self.parent.assertEqual(result.logits.shape , (self.batch_size, num_patches, expected_num_channels) ) # test greyscale images A_ = 1 A_ = ViTMAEForPreTraining(_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() A_ = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) A_ = model(_SCREAMING_SNAKE_CASE ) A_ = self.patch_size**2 self.parent.assertEqual(result.logits.shape , (self.batch_size, num_patches, expected_num_channels) ) def __A ( self ) -> int: A_ = self.prepare_config_and_inputs() A_ ,A_ ,A_ = config_and_inputs A_ = {'''pixel_values''': pixel_values} return config, inputs_dict @require_torch class __UpperCAmelCase ( _UpperCamelCase , _UpperCamelCase , unittest.TestCase ): '''simple docstring''' __lowercase : int = (ViTMAEModel, ViTMAEForPreTraining) if is_torch_available() else () __lowercase : List[Any] = {'feature-extraction': ViTMAEModel} if is_torch_available() else {} __lowercase : Union[str, Any] = False __lowercase : List[Any] = False __lowercase : List[str] = False __lowercase : List[str] = False def __A ( self ) -> Any: A_ = ViTMAEModelTester(self ) A_ = ConfigTester(self , config_class=_SCREAMING_SNAKE_CASE , has_text_modality=_SCREAMING_SNAKE_CASE , hidden_size=37 ) def __A ( self ) -> Optional[int]: self.config_tester.run_common_tests() @unittest.skip(reason='''ViTMAE does not use inputs_embeds''' ) def __A ( self ) -> int: pass def __A ( self ) -> int: A_ ,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 __A ( self ) -> int: A_ ,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 __A ( self ) -> Union[str, Any]: A_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE ) def __A ( self ) -> Optional[int]: A_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_pretraining(*_SCREAMING_SNAKE_CASE ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> int: # make masks reproducible np.random.seed(2 ) A_ = int((pt_model.config.image_size // pt_model.config.patch_size) ** 2 ) A_ = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) A_ = torch.from_numpy(_SCREAMING_SNAKE_CASE ) # Add `noise` argument. # PT inputs will be prepared in `super().check_pt_tf_models()` with this added `noise` argument A_ = pt_noise super().check_pt_tf_models(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) def __A ( self ) -> str: A_ ,A_ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A_ = model_class(_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() # make random mask reproducible torch.manual_seed(2 ) with torch.no_grad(): A_ = model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) A_ = outputs[0].cpu().numpy() A_ = 0 with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(_SCREAMING_SNAKE_CASE ) A_ = model_class.from_pretrained(_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) # make random mask reproducible torch.manual_seed(2 ) with torch.no_grad(): A_ = model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) # Make sure we don't have nans A_ = after_outputs[0].cpu().numpy() A_ = 0 A_ = np.amax(np.abs(out_a - out_a ) ) self.assertLessEqual(_SCREAMING_SNAKE_CASE , 1E-5 ) @unittest.skip( reason='''ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load to get deterministic results.''' ) def __A ( self ) -> List[str]: pass @unittest.skip( reason='''ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load to get deterministic results.''' ) def __A ( self ) -> Dict: pass @unittest.skip( reason='''ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load to get deterministic results.''' ) def __A ( self ) -> Tuple: pass @unittest.skip(reason='''ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load''' ) def __A ( self ) -> str: pass @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.''' ) def __A ( self ) -> Union[str, Any]: pass @slow def __A ( self ) -> Dict: for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A_ = ViTMAEModel.from_pretrained(_SCREAMING_SNAKE_CASE ) self.assertIsNotNone(_SCREAMING_SNAKE_CASE ) def _UpperCAmelCase ( ) -> Dict: A_ = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_torch @require_vision class __UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' @cached_property def __A ( self ) -> List[str]: return ViTImageProcessor.from_pretrained('''facebook/vit-mae-base''' ) if is_vision_available() else None @slow def __A ( self ) -> List[str]: # make random mask reproducible across the PT and TF model np.random.seed(2 ) A_ = ViTMAEForPreTraining.from_pretrained('''facebook/vit-mae-base''' ).to(_SCREAMING_SNAKE_CASE ) A_ = self.default_image_processor A_ = prepare_img() A_ = image_processor(images=_SCREAMING_SNAKE_CASE , return_tensors='''pt''' ).to(_SCREAMING_SNAKE_CASE ) # prepare a noise vector that will be also used for testing the TF model # (this way we can ensure that the PT and TF models operate on the same inputs) A_ = ViTMAEConfig() A_ = int((vit_mae_config.image_size // vit_mae_config.patch_size) ** 2 ) A_ = np.random.uniform(size=(1, num_patches) ) # forward pass with torch.no_grad(): A_ = model(**_SCREAMING_SNAKE_CASE , noise=torch.from_numpy(_SCREAMING_SNAKE_CASE ).to(device=_SCREAMING_SNAKE_CASE ) ) # verify the logits A_ = torch.Size((1, 196, 768) ) self.assertEqual(outputs.logits.shape , _SCREAMING_SNAKE_CASE ) A_ = torch.tensor( [[-0.0_548, -1.7_023, -0.9_325], [0.3_721, -0.5_670, -0.2_233], [0.8_235, -1.3_878, -0.3_524]] ) self.assertTrue(torch.allclose(outputs.logits[0, :3, :3] , expected_slice.to(_SCREAMING_SNAKE_CASE ) , atol=1E-4 ) )
18
1
'''simple docstring''' import argparse from pathlib import Path from transformers import AutoConfig, AutoTokenizer, RagConfig, RagSequenceForGeneration, RagTokenForGeneration def _UpperCAmelCase ( _UpperCamelCase : str, _UpperCamelCase : str, _UpperCamelCase : str, _UpperCamelCase : Path, _UpperCamelCase : str = None, _UpperCamelCase : str = None, _UpperCamelCase : str = None, ) -> List[Any]: if config_name_or_path is None: A_ = '''facebook/rag-token-base''' if model_type == '''rag_token''' else '''facebook/rag-sequence-base''' if generator_tokenizer_name_or_path is None: A_ = generator_name_or_path if question_encoder_tokenizer_name_or_path is None: A_ = question_encoder_name_or_path A_ = RagTokenForGeneration if model_type == '''rag_token''' else RagSequenceForGeneration # Save model. A_ = RagConfig.from_pretrained(_UpperCamelCase ) A_ = AutoConfig.from_pretrained(_UpperCamelCase ) A_ = AutoConfig.from_pretrained(_UpperCamelCase ) A_ = gen_config A_ = question_encoder_config A_ = model_class.from_pretrained_question_encoder_generator( _UpperCamelCase, _UpperCamelCase, config=_UpperCamelCase ) rag_model.save_pretrained(_UpperCamelCase ) # Sanity check. model_class.from_pretrained(_UpperCamelCase ) # Save tokenizers. A_ = AutoTokenizer.from_pretrained(_UpperCamelCase ) gen_tokenizer.save_pretrained(dest_dir / '''generator_tokenizer/''' ) A_ = AutoTokenizer.from_pretrained(_UpperCamelCase ) question_encoder_tokenizer.save_pretrained(dest_dir / '''question_encoder_tokenizer/''' ) if __name__ == "__main__": __snake_case : str = argparse.ArgumentParser() parser.add_argument( '--model_type', choices=['rag_sequence', 'rag_token'], required=True, type=str, help='RAG model type: rag_sequence, rag_token', ) parser.add_argument('--dest', type=str, required=True, help='Path to the output checkpoint directory.') parser.add_argument('--generator_name_or_path', type=str, required=True, help='Generator model identifier') parser.add_argument( '--question_encoder_name_or_path', type=str, required=True, help='Question encoder model identifier' ) parser.add_argument( '--generator_tokenizer_name_or_path', type=str, help='Generator tokenizer identifier, if not specified, resolves to ``generator_name_or_path``', ) parser.add_argument( '--question_encoder_tokenizer_name_or_path', type=str, help='Question encoder tokenizer identifier, if not specified, resolves to ``question_encoder_name_or_path``', ) parser.add_argument( '--config_name_or_path', type=str, help=( 'Identifier of the model config to use, if not provided, resolves to a base config for a given' ' ``model_type``' ), ) __snake_case : Optional[int] = parser.parse_args() __snake_case : List[Any] = Path(args.dest) dest_dir.mkdir(exist_ok=True) consolidate( args.model_type, args.generator_name_or_path, args.question_encoder_name_or_path, dest_dir, args.config_name_or_path, args.generator_tokenizer_name_or_path, args.question_encoder_tokenizer_name_or_path, )
18
'''simple docstring''' from typing import Callable, Optional, Union from ...configuration_utils import PretrainedConfig from ...utils import logging __snake_case : int = logging.get_logger(__name__) __snake_case : str = { 'microsoft/xprophetnet-large-wiki100-cased': ( 'https://huggingface.co/microsoft/xprophetnet-large-wiki100-cased/resolve/main/config.json' ), } class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' __lowercase : Optional[Any] = 'xlm-prophetnet' __lowercase : Optional[int] = ['past_key_values'] __lowercase : int = { 'num_attention_heads': 'num_encoder_attention_heads', } def __init__( self , _SCREAMING_SNAKE_CASE = 0.1 , _SCREAMING_SNAKE_CASE = "gelu" , _SCREAMING_SNAKE_CASE = 3_0522 , _SCREAMING_SNAKE_CASE = 1024 , _SCREAMING_SNAKE_CASE = 4096 , _SCREAMING_SNAKE_CASE = 12 , _SCREAMING_SNAKE_CASE = 16 , _SCREAMING_SNAKE_CASE = 4096 , _SCREAMING_SNAKE_CASE = 12 , _SCREAMING_SNAKE_CASE = 16 , _SCREAMING_SNAKE_CASE = 0.1 , _SCREAMING_SNAKE_CASE = 0.1 , _SCREAMING_SNAKE_CASE = 512 , _SCREAMING_SNAKE_CASE = 0.02 , _SCREAMING_SNAKE_CASE = True , _SCREAMING_SNAKE_CASE = True , _SCREAMING_SNAKE_CASE = 0 , _SCREAMING_SNAKE_CASE = 2 , _SCREAMING_SNAKE_CASE = 32 , _SCREAMING_SNAKE_CASE = 128 , _SCREAMING_SNAKE_CASE = False , _SCREAMING_SNAKE_CASE = 0.0 , _SCREAMING_SNAKE_CASE = True , _SCREAMING_SNAKE_CASE = 0 , _SCREAMING_SNAKE_CASE = 1 , _SCREAMING_SNAKE_CASE = 2 , **_SCREAMING_SNAKE_CASE , ) -> int: A_ = vocab_size A_ = hidden_size A_ = encoder_ffn_dim A_ = num_encoder_layers A_ = num_encoder_attention_heads A_ = decoder_ffn_dim A_ = num_decoder_layers A_ = num_decoder_attention_heads A_ = max_position_embeddings A_ = init_std # Normal(0, this parameter) A_ = activation_function # parameters for xlmprophetnet A_ = ngram A_ = num_buckets A_ = relative_max_distance A_ = disable_ngram_loss A_ = eps # 3 Types of Dropout A_ = attention_dropout A_ = activation_dropout A_ = dropout A_ = use_cache super().__init__( pad_token_id=_SCREAMING_SNAKE_CASE , bos_token_id=_SCREAMING_SNAKE_CASE , eos_token_id=_SCREAMING_SNAKE_CASE , is_encoder_decoder=_SCREAMING_SNAKE_CASE , add_cross_attention=_SCREAMING_SNAKE_CASE , decoder_start_token_id=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE , ) @property def __A ( self ) -> int: return self.num_encoder_layers + self.num_decoder_layers @num_hidden_layers.setter def __A ( self , _SCREAMING_SNAKE_CASE ) -> List[Any]: raise NotImplementedError( '''This model does not support the setting of `num_hidden_layers`. Please set `num_encoder_layers` and''' ''' `num_decoder_layers`.''' )
18
1
'''simple docstring''' def _UpperCAmelCase ( _UpperCamelCase : int ) -> int: return 1 if digit in (0, 1) else (digit * factorial(digit - 1 )) def _UpperCAmelCase ( _UpperCamelCase : int ) -> bool: A_ = 0 A_ = number while duplicate > 0: A_ ,A_ = divmod(_UpperCamelCase, 10 ) fact_sum += factorial(_UpperCamelCase ) return fact_sum == number if __name__ == "__main__": print('Program to check whether a number is a Krisnamurthy Number or not.') __snake_case : Optional[int] = int(input('Enter number: ').strip()) print( F"""{number} is {'' if krishnamurthy(number) else 'not '}a Krishnamurthy Number.""" )
18
'''simple docstring''' def _UpperCAmelCase ( _UpperCamelCase : float, _UpperCamelCase : list[float] ) -> float: if discount_rate < 0: raise ValueError('''Discount rate cannot be negative''' ) if not cash_flows: raise ValueError('''Cash flows list cannot be empty''' ) A_ = sum( cash_flow / ((1 + discount_rate) ** i) for i, cash_flow in enumerate(_UpperCamelCase ) ) return round(_UpperCamelCase, ndigits=2 ) if __name__ == "__main__": import doctest doctest.testmod()
18
1
'''simple docstring''' from math import pow, sqrt def _UpperCAmelCase ( *_UpperCamelCase : float ) -> bool: A_ = len(_UpperCamelCase ) > 0 and all(value > 0.0 for value in values ) return result def _UpperCAmelCase ( _UpperCamelCase : float, _UpperCamelCase : float ) -> float | ValueError: return ( round(sqrt(molar_mass_a / molar_mass_a ), 6 ) if validate(_UpperCamelCase, _UpperCamelCase ) else ValueError('''Input Error: Molar mass values must greater than 0.''' ) ) def _UpperCAmelCase ( _UpperCamelCase : float, _UpperCamelCase : float, _UpperCamelCase : float ) -> float | ValueError: return ( round(effusion_rate * sqrt(molar_mass_a / molar_mass_a ), 6 ) if validate(_UpperCamelCase, _UpperCamelCase, _UpperCamelCase ) else ValueError( '''Input Error: Molar mass and effusion rate values must greater than 0.''' ) ) def _UpperCAmelCase ( _UpperCamelCase : float, _UpperCamelCase : float, _UpperCamelCase : float ) -> float | ValueError: return ( round(effusion_rate / sqrt(molar_mass_a / molar_mass_a ), 6 ) if validate(_UpperCamelCase, _UpperCamelCase, _UpperCamelCase ) else ValueError( '''Input Error: Molar mass and effusion rate values must greater than 0.''' ) ) def _UpperCAmelCase ( _UpperCamelCase : float, _UpperCamelCase : float, _UpperCamelCase : float ) -> float | ValueError: return ( round(molar_mass / pow(effusion_rate_a / effusion_rate_a, 2 ), 6 ) if validate(_UpperCamelCase, _UpperCamelCase, _UpperCamelCase ) else ValueError( '''Input Error: Molar mass and effusion rate values must greater than 0.''' ) ) def _UpperCAmelCase ( _UpperCamelCase : float, _UpperCamelCase : float, _UpperCamelCase : float ) -> float | ValueError: return ( round(pow(effusion_rate_a / effusion_rate_a, 2 ) / molar_mass, 6 ) if validate(_UpperCamelCase, _UpperCamelCase, _UpperCamelCase ) else ValueError( '''Input Error: Molar mass and effusion rate values must greater than 0.''' ) )
18
'''simple docstring''' from __future__ import annotations def _UpperCAmelCase ( _UpperCamelCase : int | str ) -> bool: A_ = str(_UpperCamelCase ) return n == n[::-1] def _UpperCAmelCase ( _UpperCamelCase : int = 1_00_00_00 ) -> Any: A_ = 0 for i in range(1, _UpperCamelCase ): if is_palindrome(_UpperCamelCase ) and is_palindrome(bin(_UpperCamelCase ).split('''b''' )[1] ): total += i return total if __name__ == "__main__": print(solution(int(str(input().strip()))))
18
1
'''simple docstring''' import os from distutils.util import strtobool def _UpperCAmelCase ( _UpperCamelCase : Any, _UpperCamelCase : Optional[Any] ) -> str: for e in env_keys: A_ = int(os.environ.get(_UpperCamelCase, -1 ) ) if val >= 0: return val return default def _UpperCAmelCase ( _UpperCamelCase : Union[str, Any], _UpperCamelCase : Optional[Any]=False ) -> int: A_ = os.environ.get(_UpperCamelCase, str(_UpperCamelCase ) ) return strtobool(_UpperCamelCase ) == 1 # As its name indicates `strtobool` actually returns an int... def _UpperCAmelCase ( _UpperCamelCase : List[str], _UpperCamelCase : List[str]="no" ) -> Any: A_ = os.environ.get(_UpperCamelCase, str(_UpperCamelCase ) ) return value
18
'''simple docstring''' # Usage: # ./gen-card-facebook-wmt19.py import os from pathlib import Path def _UpperCAmelCase ( _UpperCamelCase : Tuple, _UpperCamelCase : Tuple, _UpperCamelCase : List[str] ) -> int: A_ = { '''en''': '''Machine learning is great, isn\'t it?''', '''ru''': '''Машинное обучение - это здорово, не так ли?''', '''de''': '''Maschinelles Lernen ist großartig, oder?''', } # BLUE scores as follows: # "pair": [fairseq, transformers] A_ = { '''ru-en''': ['''[41.3](http://matrix.statmt.org/matrix/output/1907?run_id=6937)''', '''39.20'''], '''en-ru''': ['''[36.4](http://matrix.statmt.org/matrix/output/1914?run_id=6724)''', '''33.47'''], '''en-de''': ['''[43.1](http://matrix.statmt.org/matrix/output/1909?run_id=6862)''', '''42.83'''], '''de-en''': ['''[42.3](http://matrix.statmt.org/matrix/output/1902?run_id=6750)''', '''41.35'''], } A_ = F'''{src_lang}-{tgt_lang}''' A_ = F''' --- language: - {src_lang} - {tgt_lang} thumbnail: tags: - translation - wmt19 - facebook license: apache-2.0 datasets: - wmt19 metrics: - bleu --- # FSMT ## Model description This is a ported version of [fairseq wmt19 transformer](https://github.com/pytorch/fairseq/blob/master/examples/wmt19/README.md) for {src_lang}-{tgt_lang}. For more details, please see, [Facebook FAIR\'s WMT19 News Translation Task Submission](https://arxiv.org/abs/1907.06616). The abbreviation FSMT stands for FairSeqMachineTranslation All four models are available: * [wmt19-en-ru](https://huggingface.co/facebook/wmt19-en-ru) * [wmt19-ru-en](https://huggingface.co/facebook/wmt19-ru-en) * [wmt19-en-de](https://huggingface.co/facebook/wmt19-en-de) * [wmt19-de-en](https://huggingface.co/facebook/wmt19-de-en) ## Intended uses & limitations #### How to use ```python from transformers import FSMTForConditionalGeneration, FSMTTokenizer mname = "facebook/wmt19-{src_lang}-{tgt_lang}" tokenizer = FSMTTokenizer.from_pretrained(mname) model = FSMTForConditionalGeneration.from_pretrained(mname) input = "{texts[src_lang]}" input_ids = tokenizer.encode(input, return_tensors="pt") outputs = model.generate(input_ids) decoded = tokenizer.decode(outputs[0], skip_special_tokens=True) print(decoded) # {texts[tgt_lang]} ``` #### Limitations and bias - The original (and this ported model) doesn\'t seem to handle well inputs with repeated sub-phrases, [content gets truncated](https://discuss.huggingface.co/t/issues-with-translating-inputs-containing-repeated-phrases/981) ## Training data Pretrained weights were left identical to the original model released by fairseq. For more details, please, see the [paper](https://arxiv.org/abs/1907.06616). ## Eval results pair | fairseq | transformers -------|---------|---------- {pair} | {scores[pair][0]} | {scores[pair][1]} The score is slightly below the score reported by `fairseq`, since `transformers`` currently doesn\'t support: - model ensemble, therefore the best performing checkpoint was ported (``model4.pt``). - re-ranking The score was calculated using this code: ```bash git clone https://github.com/huggingface/transformers cd transformers export PAIR={pair} export DATA_DIR=data/$PAIR export SAVE_DIR=data/$PAIR export BS=8 export NUM_BEAMS=15 mkdir -p $DATA_DIR sacrebleu -t wmt19 -l $PAIR --echo src > $DATA_DIR/val.source sacrebleu -t wmt19 -l $PAIR --echo ref > $DATA_DIR/val.target echo $PAIR PYTHONPATH="src:examples/seq2seq" python examples/seq2seq/run_eval.py facebook/wmt19-$PAIR $DATA_DIR/val.source $SAVE_DIR/test_translations.txt --reference_path $DATA_DIR/val.target --score_path $SAVE_DIR/test_bleu.json --bs $BS --task translation --num_beams $NUM_BEAMS ``` note: fairseq reports using a beam of 50, so you should get a slightly higher score if re-run with `--num_beams 50`. ## Data Sources - [training, etc.](http://www.statmt.org/wmt19/) - [test set](http://matrix.statmt.org/test_sets/newstest2019.tgz?1556572561) ### BibTeX entry and citation info ```bibtex @inproceedings{{..., year={{2020}}, title={{Facebook FAIR\'s WMT19 News Translation Task Submission}}, author={{Ng, Nathan and Yee, Kyra and Baevski, Alexei and Ott, Myle and Auli, Michael and Edunov, Sergey}}, booktitle={{Proc. of WMT}}, }} ``` ## TODO - port model ensemble (fairseq uses 4 model checkpoints) ''' os.makedirs(_UpperCamelCase, exist_ok=_UpperCamelCase ) A_ = os.path.join(_UpperCamelCase, '''README.md''' ) print(F'''Generating {path}''' ) with open(_UpperCamelCase, '''w''', encoding='''utf-8''' ) as f: f.write(_UpperCamelCase ) # make sure we are under the root of the project __snake_case : Any = Path(__file__).resolve().parent.parent.parent __snake_case : Tuple = repo_dir / 'model_cards' for model_name in ["wmt19-ru-en", "wmt19-en-ru", "wmt19-en-de", "wmt19-de-en"]: __snake_case , __snake_case , __snake_case : Any = model_name.split('-') __snake_case : int = model_cards_dir / 'facebook' / model_name write_model_card(model_card_dir, src_lang=src_lang, tgt_lang=tgt_lang)
18
1
'''simple docstring''' import argparse import os import pickle import sys import torch from transformers import TransfoXLConfig, TransfoXLLMHeadModel, load_tf_weights_in_transfo_xl from transformers.models.transfo_xl import tokenization_transfo_xl as data_utils from transformers.models.transfo_xl.tokenization_transfo_xl import CORPUS_NAME, VOCAB_FILES_NAMES from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging logging.set_verbosity_info() # We do this to be able to load python 2 datasets pickles # See e.g. https://stackoverflow.com/questions/2121874/python-pickling-after-changing-a-modules-directory/2121918#2121918 __snake_case : Optional[int] = data_utils.TransfoXLTokenizer __snake_case : Any = data_utils.TransfoXLCorpus __snake_case : List[str] = data_utils __snake_case : List[Any] = data_utils def _UpperCAmelCase ( _UpperCamelCase : Optional[Any], _UpperCamelCase : int, _UpperCamelCase : Optional[Any], _UpperCamelCase : int ) -> Dict: if transfo_xl_dataset_file: # Convert a pre-processed corpus (see original TensorFlow repo) with open(_UpperCamelCase, '''rb''' ) as fp: A_ = pickle.load(_UpperCamelCase, encoding='''latin1''' ) # Save vocabulary and dataset cache as Dictionaries (should be better than pickles for the long-term) A_ = pytorch_dump_folder_path + '''/''' + VOCAB_FILES_NAMES['''pretrained_vocab_file'''] print(F'''Save vocabulary to {pytorch_vocab_dump_path}''' ) A_ = corpus.vocab.__dict__ torch.save(_UpperCamelCase, _UpperCamelCase ) A_ = corpus.__dict__ corpus_dict_no_vocab.pop('''vocab''', _UpperCamelCase ) A_ = pytorch_dump_folder_path + '''/''' + CORPUS_NAME print(F'''Save dataset to {pytorch_dataset_dump_path}''' ) torch.save(_UpperCamelCase, _UpperCamelCase ) if tf_checkpoint_path: # Convert a pre-trained TensorFlow model A_ = os.path.abspath(_UpperCamelCase ) A_ = os.path.abspath(_UpperCamelCase ) print(F'''Converting Transformer XL checkpoint from {tf_path} with config at {config_path}.''' ) # Initialise PyTorch model if transfo_xl_config_file == "": A_ = TransfoXLConfig() else: A_ = TransfoXLConfig.from_json_file(_UpperCamelCase ) print(F'''Building PyTorch model from configuration: {config}''' ) A_ = TransfoXLLMHeadModel(_UpperCamelCase ) A_ = load_tf_weights_in_transfo_xl(_UpperCamelCase, _UpperCamelCase, _UpperCamelCase ) # Save pytorch-model A_ = os.path.join(_UpperCamelCase, _UpperCamelCase ) A_ = os.path.join(_UpperCamelCase, _UpperCamelCase ) print(F'''Save PyTorch model to {os.path.abspath(_UpperCamelCase )}''' ) torch.save(model.state_dict(), _UpperCamelCase ) print(F'''Save configuration file to {os.path.abspath(_UpperCamelCase )}''' ) with open(_UpperCamelCase, '''w''', encoding='''utf-8''' ) as f: f.write(config.to_json_string() ) if __name__ == "__main__": __snake_case : Dict = argparse.ArgumentParser() parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, required=True, help='Path to the folder to store the PyTorch model or dataset/vocab.', ) parser.add_argument( '--tf_checkpoint_path', default='', type=str, help='An optional path to a TensorFlow checkpoint path to be converted.', ) parser.add_argument( '--transfo_xl_config_file', default='', type=str, help=( 'An optional config json file corresponding to the pre-trained BERT model. \n' 'This specifies the model architecture.' ), ) parser.add_argument( '--transfo_xl_dataset_file', default='', type=str, help='An optional dataset file to be converted in a vocabulary.', ) __snake_case : Dict = parser.parse_args() convert_transfo_xl_checkpoint_to_pytorch( args.tf_checkpoint_path, args.transfo_xl_config_file, args.pytorch_dump_folder_path, args.transfo_xl_dataset_file, )
18
'''simple docstring''' from collections import defaultdict def _UpperCAmelCase ( _UpperCamelCase : int ) -> int: A_ = 1 A_ = True for v in tree[start]: if v not in visited: ret += dfs(_UpperCamelCase ) if ret % 2 == 0: cuts.append(_UpperCamelCase ) return ret def _UpperCAmelCase ( ) -> Optional[Any]: dfs(1 ) if __name__ == "__main__": __snake_case , __snake_case : Union[str, Any] = 10, 9 __snake_case : int = defaultdict(list) __snake_case : dict[int, bool] = {} __snake_case : list[int] = [] __snake_case : Union[str, Any] = 0 __snake_case : int = [(2, 1), (3, 1), (4, 3), (5, 2), (6, 1), (7, 2), (8, 6), (9, 8), (10, 8)] for u, v in edges: tree[u].append(v) tree[v].append(u) even_tree() print(len(cuts) - 1)
18
1
'''simple docstring''' import argparse import json from typing import List from ltp import LTP from transformers import BertTokenizer def _UpperCAmelCase ( _UpperCamelCase : Dict ) -> Union[str, Any]: # This defines a "chinese character" as anything in the CJK Unicode block: # https://en.wikipedia.org/wiki/CJK_Unified_Ideographs_(Unicode_block) # # Note that the CJK Unicode block is NOT all Japanese and Korean characters, # despite its name. The modern Korean Hangul alphabet is a different block, # as is Japanese Hiragana and Katakana. Those alphabets are used to write # space-separated words, so they are not treated specially and handled # like the all of the other languages. if ( (cp >= 0x4E_00 and cp <= 0x9F_FF) or (cp >= 0x34_00 and cp <= 0x4D_BF) # or (cp >= 0x2_00_00 and cp <= 0x2_A6_DF) # or (cp >= 0x2_A7_00 and cp <= 0x2_B7_3F) # or (cp >= 0x2_B7_40 and cp <= 0x2_B8_1F) # or (cp >= 0x2_B8_20 and cp <= 0x2_CE_AF) # or (cp >= 0xF9_00 and cp <= 0xFA_FF) or (cp >= 0x2_F8_00 and cp <= 0x2_FA_1F) # ): # return True return False def _UpperCAmelCase ( _UpperCamelCase : str ) -> str: # word like '180' or '身高' or '神' for char in word: A_ = ord(_UpperCamelCase ) if not _is_chinese_char(_UpperCamelCase ): return 0 return 1 def _UpperCAmelCase ( _UpperCamelCase : List[str] ) -> Any: A_ = set() for token in tokens: A_ = len(_UpperCamelCase ) > 1 and is_chinese(_UpperCamelCase ) if chinese_word: word_set.add(_UpperCamelCase ) A_ = list(_UpperCamelCase ) return word_list def _UpperCAmelCase ( _UpperCamelCase : List[str], _UpperCamelCase : set() ) -> Tuple: if not chinese_word_set: return bert_tokens A_ = max([len(_UpperCamelCase ) for w in chinese_word_set] ) A_ = bert_tokens A_ ,A_ = 0, len(_UpperCamelCase ) while start < end: A_ = True if is_chinese(bert_word[start] ): A_ = min(end - start, _UpperCamelCase ) for i in range(_UpperCamelCase, 1, -1 ): A_ = ''''''.join(bert_word[start : start + i] ) if whole_word in chinese_word_set: for j in range(start + 1, start + i ): A_ = '''##''' + bert_word[j] A_ = start + i A_ = False break if single_word: start += 1 return bert_word def _UpperCAmelCase ( _UpperCamelCase : List[str], _UpperCamelCase : LTP, _UpperCamelCase : BertTokenizer ) -> Dict: A_ = [] for i in range(0, len(_UpperCamelCase ), 1_00 ): A_ = ltp_tokenizer.seg(lines[i : i + 1_00] )[0] A_ = [get_chinese_word(_UpperCamelCase ) for r in res] ltp_res.extend(_UpperCamelCase ) assert len(_UpperCamelCase ) == len(_UpperCamelCase ) A_ = [] for i in range(0, len(_UpperCamelCase ), 1_00 ): A_ = bert_tokenizer(lines[i : i + 1_00], add_special_tokens=_UpperCamelCase, truncation=_UpperCamelCase, max_length=5_12 ) bert_res.extend(res['''input_ids'''] ) assert len(_UpperCamelCase ) == len(_UpperCamelCase ) A_ = [] for input_ids, chinese_word in zip(_UpperCamelCase, _UpperCamelCase ): A_ = [] for id in input_ids: A_ = bert_tokenizer._convert_id_to_token(_UpperCamelCase ) input_tokens.append(_UpperCamelCase ) A_ = add_sub_symbol(_UpperCamelCase, _UpperCamelCase ) A_ = [] # We only save pos of chinese subwords start with ##, which mean is part of a whole word. for i, token in enumerate(_UpperCamelCase ): if token[:2] == "##": A_ = token[2:] # save chinese tokens' pos if len(_UpperCamelCase ) == 1 and _is_chinese_char(ord(_UpperCamelCase ) ): ref_id.append(_UpperCamelCase ) ref_ids.append(_UpperCamelCase ) assert len(_UpperCamelCase ) == len(_UpperCamelCase ) return ref_ids def _UpperCAmelCase ( _UpperCamelCase : Tuple ) -> Any: # For Chinese (Ro)Bert, the best result is from : RoBERTa-wwm-ext (https://github.com/ymcui/Chinese-BERT-wwm) # If we want to fine-tune these model, we have to use same tokenizer : LTP (https://github.com/HIT-SCIR/ltp) with open(args.file_name, '''r''', encoding='''utf-8''' ) as f: A_ = f.readlines() A_ = [line.strip() for line in data if len(_UpperCamelCase ) > 0 and not line.isspace()] # avoid delimiter like '\u2029' A_ = LTP(args.ltp ) # faster in GPU device A_ = BertTokenizer.from_pretrained(args.bert ) A_ = prepare_ref(_UpperCamelCase, _UpperCamelCase, _UpperCamelCase ) with open(args.save_path, '''w''', encoding='''utf-8''' ) as f: A_ = [json.dumps(_UpperCamelCase ) + '''\n''' for ref in ref_ids] f.writelines(_UpperCamelCase ) if __name__ == "__main__": __snake_case : Optional[Any] = argparse.ArgumentParser(description='prepare_chinese_ref') parser.add_argument( '--file_name', type=str, default='./resources/chinese-demo.txt', help='file need process, same as training data in lm', ) parser.add_argument( '--ltp', type=str, default='./resources/ltp', help='resources for LTP tokenizer, usually a path' ) parser.add_argument('--bert', type=str, default='./resources/robert', help='resources for Bert tokenizer') parser.add_argument('--save_path', type=str, default='./resources/ref.txt', help='path to save res') __snake_case : Any = parser.parse_args() main(args)
18
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging __snake_case : List[str] = logging.get_logger(__name__) __snake_case : Union[str, Any] = { 'alibaba-damo/mgp-str-base': 'https://huggingface.co/alibaba-damo/mgp-str-base/resolve/main/config.json', } class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' __lowercase : Optional[int] = 'mgp-str' def __init__( self , _SCREAMING_SNAKE_CASE=[32, 128] , _SCREAMING_SNAKE_CASE=4 , _SCREAMING_SNAKE_CASE=3 , _SCREAMING_SNAKE_CASE=27 , _SCREAMING_SNAKE_CASE=38 , _SCREAMING_SNAKE_CASE=5_0257 , _SCREAMING_SNAKE_CASE=3_0522 , _SCREAMING_SNAKE_CASE=768 , _SCREAMING_SNAKE_CASE=12 , _SCREAMING_SNAKE_CASE=12 , _SCREAMING_SNAKE_CASE=4.0 , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=False , _SCREAMING_SNAKE_CASE=1E-5 , _SCREAMING_SNAKE_CASE=0.0 , _SCREAMING_SNAKE_CASE=0.0 , _SCREAMING_SNAKE_CASE=0.0 , _SCREAMING_SNAKE_CASE=False , _SCREAMING_SNAKE_CASE=0.02 , **_SCREAMING_SNAKE_CASE , ) -> List[Any]: super().__init__(**_SCREAMING_SNAKE_CASE ) A_ = image_size A_ = patch_size A_ = num_channels A_ = max_token_length A_ = num_character_labels A_ = num_bpe_labels A_ = num_wordpiece_labels A_ = hidden_size A_ = num_hidden_layers A_ = num_attention_heads A_ = mlp_ratio A_ = distilled A_ = layer_norm_eps A_ = drop_rate A_ = qkv_bias A_ = attn_drop_rate A_ = drop_path_rate A_ = output_aa_attentions A_ = initializer_range
18
1
'''simple docstring''' def _UpperCAmelCase ( _UpperCamelCase : float, _UpperCamelCase : list[float] ) -> float: if discount_rate < 0: raise ValueError('''Discount rate cannot be negative''' ) if not cash_flows: raise ValueError('''Cash flows list cannot be empty''' ) A_ = sum( cash_flow / ((1 + discount_rate) ** i) for i, cash_flow in enumerate(_UpperCamelCase ) ) return round(_UpperCamelCase, ndigits=2 ) if __name__ == "__main__": import doctest doctest.testmod()
18
'''simple docstring''' from diffusers.utils.testing_utils import require_onnxruntime @require_onnxruntime class __UpperCAmelCase : '''simple docstring''' pass
18
1
'''simple docstring''' import cva import numpy as np class __UpperCAmelCase : '''simple docstring''' def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> List[str]: if k in (0.04, 0.06): A_ = k A_ = window_size else: raise ValueError('''invalid k value''' ) def __str__( self ) -> str: return str(self.k ) def __A ( self , _SCREAMING_SNAKE_CASE ) -> tuple[cva.Mat, list[list[int]]]: A_ = cva.imread(_SCREAMING_SNAKE_CASE , 0 ) A_ ,A_ = img.shape A_ = [] A_ = img.copy() A_ = cva.cvtColor(_SCREAMING_SNAKE_CASE , cva.COLOR_GRAY2RGB ) A_ ,A_ = np.gradient(_SCREAMING_SNAKE_CASE ) A_ = dx**2 A_ = dy**2 A_ = dx * dy A_ = 0.04 A_ = self.window_size // 2 for y in range(_SCREAMING_SNAKE_CASE , h - offset ): for x in range(_SCREAMING_SNAKE_CASE , w - offset ): A_ = ixx[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() A_ = iyy[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() A_ = ixy[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() A_ = (wxx * wyy) - (wxy**2) A_ = wxx + wyy A_ = det - k * (trace**2) # Can change the value if r > 0.5: corner_list.append([x, y, r] ) color_img.itemset((y, x, 0) , 0 ) color_img.itemset((y, x, 1) , 0 ) color_img.itemset((y, x, 2) , 255 ) return color_img, corner_list if __name__ == "__main__": __snake_case : Dict = HarrisCorner(0.04, 3) __snake_case , __snake_case : List[str] = edge_detect.detect('path_to_image') cva.imwrite('detect.png', color_img)
18
'''simple docstring''' # Copyright 2021 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from argparse import ArgumentParser from accelerate.commands.config import get_config_parser from accelerate.commands.env import env_command_parser from accelerate.commands.launch import launch_command_parser from accelerate.commands.test import test_command_parser from accelerate.commands.tpu import tpu_command_parser def _UpperCAmelCase ( ) -> Dict: A_ = ArgumentParser('''Accelerate CLI tool''', usage='''accelerate <command> [<args>]''', allow_abbrev=_UpperCamelCase ) A_ = parser.add_subparsers(help='''accelerate command helpers''' ) # Register commands get_config_parser(subparsers=_UpperCamelCase ) env_command_parser(subparsers=_UpperCamelCase ) launch_command_parser(subparsers=_UpperCamelCase ) tpu_command_parser(subparsers=_UpperCamelCase ) test_command_parser(subparsers=_UpperCamelCase ) # Let's go A_ = parser.parse_args() if not hasattr(_UpperCamelCase, '''func''' ): parser.print_help() exit(1 ) # Run args.func(_UpperCamelCase ) if __name__ == "__main__": main()
18
1
'''simple docstring''' import json import os import tempfile import unittest import unittest.mock as mock from pathlib import Path from requests.exceptions import HTTPError from transformers.utils import ( CONFIG_NAME, FLAX_WEIGHTS_NAME, TF2_WEIGHTS_NAME, TRANSFORMERS_CACHE, WEIGHTS_NAME, cached_file, get_file_from_repo, has_file, ) __snake_case : Dict = 'hf-internal-testing/tiny-random-bert' __snake_case : int = os.path.join(TRANSFORMERS_CACHE, 'models--hf-internal-testing--tiny-random-bert') __snake_case : Optional[int] = '9b8c223d42b2188cb49d29af482996f9d0f3e5a6' class __UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' def __A ( self ) -> Optional[int]: A_ = cached_file(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # Should have downloaded the file in here self.assertTrue(os.path.isdir(_SCREAMING_SNAKE_CASE ) ) # Cache should contain at least those three subfolders: for subfolder in ["blobs", "refs", "snapshots"]: self.assertTrue(os.path.isdir(os.path.join(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) ) with open(os.path.join(_SCREAMING_SNAKE_CASE , '''refs''' , '''main''' ) ) as f: A_ = f.read() self.assertEqual(_SCREAMING_SNAKE_CASE , os.path.join(_SCREAMING_SNAKE_CASE , '''snapshots''' , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) self.assertTrue(os.path.isfile(_SCREAMING_SNAKE_CASE ) ) # File is cached at the same place the second time. A_ = cached_file(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) self.assertEqual(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # Using a specific revision to test the full commit hash. A_ = cached_file(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , revision='''9b8c223''' ) self.assertEqual(_SCREAMING_SNAKE_CASE , os.path.join(_SCREAMING_SNAKE_CASE , '''snapshots''' , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) def __A ( self ) -> Tuple: with self.assertRaisesRegex(_SCREAMING_SNAKE_CASE , '''is not a valid model identifier''' ): A_ = cached_file('''tiny-random-bert''' , _SCREAMING_SNAKE_CASE ) with self.assertRaisesRegex(_SCREAMING_SNAKE_CASE , '''is not a valid git identifier''' ): A_ = cached_file(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , revision='''aaaa''' ) with self.assertRaisesRegex(_SCREAMING_SNAKE_CASE , '''does not appear to have a file named''' ): A_ = cached_file(_SCREAMING_SNAKE_CASE , '''conf''' ) def __A ( self ) -> List[Any]: with self.assertRaisesRegex(_SCREAMING_SNAKE_CASE , '''does not appear to have a file named''' ): A_ = cached_file(_SCREAMING_SNAKE_CASE , '''conf''' ) with open(os.path.join(_SCREAMING_SNAKE_CASE , '''refs''' , '''main''' ) ) as f: A_ = f.read() self.assertTrue(os.path.isfile(os.path.join(_SCREAMING_SNAKE_CASE , '''.no_exist''' , _SCREAMING_SNAKE_CASE , '''conf''' ) ) ) A_ = cached_file(_SCREAMING_SNAKE_CASE , '''conf''' , _raise_exceptions_for_missing_entries=_SCREAMING_SNAKE_CASE ) self.assertIsNone(_SCREAMING_SNAKE_CASE ) A_ = cached_file(_SCREAMING_SNAKE_CASE , '''conf''' , local_files_only=_SCREAMING_SNAKE_CASE , _raise_exceptions_for_missing_entries=_SCREAMING_SNAKE_CASE ) self.assertIsNone(_SCREAMING_SNAKE_CASE ) A_ = mock.Mock() A_ = 500 A_ = {} A_ = HTTPError A_ = {} # Under the mock environment we get a 500 error when trying to reach the tokenizer. with mock.patch('''requests.Session.request''' , return_value=_SCREAMING_SNAKE_CASE ) as mock_head: A_ = cached_file(_SCREAMING_SNAKE_CASE , '''conf''' , _raise_exceptions_for_connection_errors=_SCREAMING_SNAKE_CASE ) self.assertIsNone(_SCREAMING_SNAKE_CASE ) # This check we did call the fake head request mock_head.assert_called() def __A ( self ) -> Union[str, Any]: self.assertTrue(has_file('''hf-internal-testing/tiny-bert-pt-only''' , _SCREAMING_SNAKE_CASE ) ) self.assertFalse(has_file('''hf-internal-testing/tiny-bert-pt-only''' , _SCREAMING_SNAKE_CASE ) ) self.assertFalse(has_file('''hf-internal-testing/tiny-bert-pt-only''' , _SCREAMING_SNAKE_CASE ) ) def __A ( self ) -> Any: # `get_file_from_repo` returns None if the file does not exist self.assertIsNone(get_file_from_repo('''bert-base-cased''' , '''ahah.txt''' ) ) # The function raises if the repository does not exist. with self.assertRaisesRegex(_SCREAMING_SNAKE_CASE , '''is not a valid model identifier''' ): get_file_from_repo('''bert-base-case''' , _SCREAMING_SNAKE_CASE ) # The function raises if the revision does not exist. with self.assertRaisesRegex(_SCREAMING_SNAKE_CASE , '''is not a valid git identifier''' ): get_file_from_repo('''bert-base-cased''' , _SCREAMING_SNAKE_CASE , revision='''ahaha''' ) A_ = get_file_from_repo('''bert-base-cased''' , _SCREAMING_SNAKE_CASE ) # The name is the cached name which is not very easy to test, so instead we load the content. A_ = json.loads(open(_SCREAMING_SNAKE_CASE , '''r''' ).read() ) self.assertEqual(config['''hidden_size'''] , 768 ) def __A ( self ) -> str: with tempfile.TemporaryDirectory() as tmp_dir: A_ = Path(_SCREAMING_SNAKE_CASE ) / '''a.txt''' filename.touch() self.assertEqual(get_file_from_repo(_SCREAMING_SNAKE_CASE , '''a.txt''' ) , str(_SCREAMING_SNAKE_CASE ) ) self.assertIsNone(get_file_from_repo(_SCREAMING_SNAKE_CASE , '''b.txt''' ) )
18
'''simple docstring''' import absl # noqa: F401 # Here to have a nice missing dependency error message early on import nltk # noqa: F401 # Here to have a nice missing dependency error message early on import numpy # noqa: F401 # Here to have a nice missing dependency error message early on import six # noqa: F401 # Here to have a nice missing dependency error message early on from rouge_score import rouge_scorer, scoring import datasets __snake_case : Any = '\\n@inproceedings{lin-2004-rouge,\n title = "{ROUGE}: A Package for Automatic Evaluation of Summaries",\n author = "Lin, Chin-Yew",\n booktitle = "Text Summarization Branches Out",\n month = jul,\n year = "2004",\n address = "Barcelona, Spain",\n publisher = "Association for Computational Linguistics",\n url = "https://www.aclweb.org/anthology/W04-1013",\n pages = "74--81",\n}\n' __snake_case : Dict = '\\nROUGE, or Recall-Oriented Understudy for Gisting Evaluation, is a set of metrics and a software package used for\nevaluating automatic summarization and machine translation software in natural language processing.\nThe metrics compare an automatically produced summary or translation against a reference or a set of references (human-produced) summary or translation.\n\nNote that ROUGE is case insensitive, meaning that upper case letters are treated the same way as lower case letters.\n\nThis metrics is a wrapper around Google Research reimplementation of ROUGE:\nhttps://github.com/google-research/google-research/tree/master/rouge\n' __snake_case : Optional[int] = '\nCalculates average rouge scores for a list of hypotheses and references\nArgs:\n predictions: list of predictions to score. Each prediction\n should be a string with tokens separated by spaces.\n references: list of reference for each prediction. Each\n reference should be a string with tokens separated by spaces.\n rouge_types: A list of rouge types to calculate.\n Valid names:\n `"rouge{n}"` (e.g. `"rouge1"`, `"rouge2"`) where: {n} is the n-gram based scoring,\n `"rougeL"`: Longest common subsequence based scoring.\n `"rougeLSum"`: rougeLsum splits text using `"\n"`.\n See details in https://github.com/huggingface/datasets/issues/617\n use_stemmer: Bool indicating whether Porter stemmer should be used to strip word suffixes.\n use_aggregator: Return aggregates if this is set to True\nReturns:\n rouge1: rouge_1 (precision, recall, f1),\n rouge2: rouge_2 (precision, recall, f1),\n rougeL: rouge_l (precision, recall, f1),\n rougeLsum: rouge_lsum (precision, recall, f1)\nExamples:\n\n >>> rouge = datasets.load_metric(\'rouge\')\n >>> predictions = ["hello there", "general kenobi"]\n >>> references = ["hello there", "general kenobi"]\n >>> results = rouge.compute(predictions=predictions, references=references)\n >>> print(list(results.keys()))\n [\'rouge1\', \'rouge2\', \'rougeL\', \'rougeLsum\']\n >>> print(results["rouge1"])\n AggregateScore(low=Score(precision=1.0, recall=1.0, fmeasure=1.0), mid=Score(precision=1.0, recall=1.0, fmeasure=1.0), high=Score(precision=1.0, recall=1.0, fmeasure=1.0))\n >>> print(results["rouge1"].mid.fmeasure)\n 1.0\n' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __UpperCAmelCase ( datasets.Metric ): '''simple docstring''' def __A ( self ) -> List[str]: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Value('''string''' , id='''sequence''' ), '''references''': datasets.Value('''string''' , id='''sequence''' ), } ) , codebase_urls=['''https://github.com/google-research/google-research/tree/master/rouge'''] , reference_urls=[ '''https://en.wikipedia.org/wiki/ROUGE_(metric)''', '''https://github.com/google-research/google-research/tree/master/rouge''', ] , ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=False ) -> Optional[int]: if rouge_types is None: A_ = ['''rouge1''', '''rouge2''', '''rougeL''', '''rougeLsum'''] A_ = rouge_scorer.RougeScorer(rouge_types=_SCREAMING_SNAKE_CASE , use_stemmer=_SCREAMING_SNAKE_CASE ) if use_aggregator: A_ = scoring.BootstrapAggregator() else: A_ = [] for ref, pred in zip(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): A_ = scorer.score(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) if use_aggregator: aggregator.add_scores(_SCREAMING_SNAKE_CASE ) else: scores.append(_SCREAMING_SNAKE_CASE ) if use_aggregator: A_ = aggregator.aggregate() else: A_ = {} for key in scores[0]: A_ = [score[key] for score in scores] return result
18
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available __snake_case : Optional[int] = {} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case : List[str] = ['BartphoTokenizer'] if TYPE_CHECKING: try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_bartpho import BartphoTokenizer else: import sys __snake_case : Tuple = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
18
'''simple docstring''' import gc import importlib.metadata import tempfile import unittest from packaging import version from transformers import ( AutoModel, AutoModelForCausalLM, AutoModelForSeqaSeqLM, AutoModelForSequenceClassification, AutoTokenizer, BitsAndBytesConfig, pipeline, ) from transformers.testing_utils import ( is_torch_available, require_accelerate, require_bitsandbytes, require_torch, require_torch_gpu, require_torch_multi_gpu, slow, ) def _UpperCAmelCase ( _UpperCamelCase : str ) -> int: if model.config.model_type == "gpt2": return model.transformer.h[0].mlp.c_fc return model.transformer.h[0].mlp.dense_ah_to_h if is_torch_available(): import torch import torch.nn as nn class __UpperCAmelCase ( nn.Module ): '''simple docstring''' def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Dict: super().__init__() A_ = module A_ = nn.Sequential( nn.Linear(module.in_features , _SCREAMING_SNAKE_CASE , bias=_SCREAMING_SNAKE_CASE ) , nn.Linear(_SCREAMING_SNAKE_CASE , module.out_features , bias=_SCREAMING_SNAKE_CASE ) , ) A_ = (2.0 / (5 * min(module.in_features , module.out_features ))) ** 0.5 nn.init.normal_(self.adapter[0].weight , std=_SCREAMING_SNAKE_CASE ) nn.init.zeros_(self.adapter[1].weight ) self.adapter.to(module.weight.device ) def __A ( self , _SCREAMING_SNAKE_CASE , *_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) -> Tuple: return self.module(_SCREAMING_SNAKE_CASE , *_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) + self.adapter(_SCREAMING_SNAKE_CASE ) @require_bitsandbytes @require_accelerate @require_torch @require_torch_gpu @slow class __UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' __lowercase : Dict = 'bigscience/bloom-1b7' # Constant values __lowercase : str = 2.109659552692574 __lowercase : int = 'Hello my name is' __lowercase : Optional[Any] = set() EXPECTED_OUTPUTS.add('Hello my name is John and I am a professional photographer. I' ) EXPECTED_OUTPUTS.add('Hello my name is John.\nI am a friend of your father.\n' ) EXPECTED_OUTPUTS.add('Hello my name is John Doe, I am a student at the University' ) __lowercase : Optional[Any] = 10 def __A ( self ) -> List[str]: # Models and tokenizer A_ = AutoTokenizer.from_pretrained(self.model_name ) class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' def __A ( self ) -> List[Any]: super().setUp() # Models and tokenizer A_ = AutoModelForCausalLM.from_pretrained( self.model_name , torch_dtype=torch.floataa , device_map='''auto''' ) A_ = AutoModelForCausalLM.from_pretrained(self.model_name , load_in_abit=_SCREAMING_SNAKE_CASE , device_map='''auto''' ) def __A ( self ) -> List[str]: del self.model_fpaa del self.model_abit gc.collect() torch.cuda.empty_cache() def __A ( self ) -> Tuple: A_ = self.model_abit.config self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE , '''quantization_config''' ) ) A_ = config.to_dict() A_ = config.to_diff_dict() A_ = config.to_json_string() def __A ( self ) -> Union[str, Any]: from bitsandbytes.nn import Paramsabit A_ = self.model_fpaa.get_memory_footprint() A_ = self.model_abit.get_memory_footprint() self.assertAlmostEqual(mem_fpaa / mem_abit , self.EXPECTED_RELATIVE_DIFFERENCE ) A_ = get_some_linear_layer(self.model_abit ) self.assertTrue(linear.weight.__class__ == Paramsabit ) def __A ( self ) -> Union[str, Any]: from transformers import TaPreTrainedModel self.model_fpaa.get_memory_footprint() self.model_abit.get_memory_footprint() for name, module in self.model_abit.named_modules(): if isinstance(_SCREAMING_SNAKE_CASE , torch.nn.Linear ): if name not in ["lm_head"] + TaPreTrainedModel._keep_in_fpaa_modules: # 4-bit parameters are packed in uint8 variables self.assertTrue(module.weight.dtype == torch.uinta ) def __A ( self ) -> Optional[int]: A_ = self.tokenizer(self.input_text , return_tensors='''pt''' ) A_ = self.model_abit.generate(input_ids=encoded_input['''input_ids'''].to(0 ) , max_new_tokens=10 ) self.assertIn(self.tokenizer.decode(output_sequences[0] , skip_special_tokens=_SCREAMING_SNAKE_CASE ) , self.EXPECTED_OUTPUTS ) def __A ( self ) -> Optional[int]: A_ = BitsAndBytesConfig() A_ = True A_ = AutoModelForCausalLM.from_pretrained( self.model_name , quantization_config=_SCREAMING_SNAKE_CASE , device_map='''auto''' ) A_ = self.tokenizer(self.input_text , return_tensors='''pt''' ) A_ = model_abit_from_config.generate( input_ids=encoded_input['''input_ids'''].to(0 ) , max_new_tokens=10 ) self.assertIn(self.tokenizer.decode(output_sequences[0] , skip_special_tokens=_SCREAMING_SNAKE_CASE ) , self.EXPECTED_OUTPUTS ) def __A ( self ) -> Tuple: with self.assertRaises(_SCREAMING_SNAKE_CASE ), tempfile.TemporaryDirectory() as tmpdirname: self.model_abit.save_pretrained(_SCREAMING_SNAKE_CASE ) def __A ( self ) -> Tuple: A_ = BitsAndBytesConfig() with self.assertRaises(_SCREAMING_SNAKE_CASE ): A_ = AutoModelForCausalLM.from_pretrained( self.model_name , quantization_config=_SCREAMING_SNAKE_CASE , load_in_abit=_SCREAMING_SNAKE_CASE , device_map='''auto''' , bnb_abit_quant_type='''nf4''' , ) def __A ( self ) -> Dict: with self.assertRaises(_SCREAMING_SNAKE_CASE ): # Tries with `str` self.model_abit.to('''cpu''' ) with self.assertRaises(_SCREAMING_SNAKE_CASE ): # Tries with a `dtype`` self.model_abit.to(torch.floataa ) with self.assertRaises(_SCREAMING_SNAKE_CASE ): # Tries with a `device` self.model_abit.to(torch.device('''cuda:0''' ) ) with self.assertRaises(_SCREAMING_SNAKE_CASE ): # Tries with a `device` self.model_abit.float() with self.assertRaises(_SCREAMING_SNAKE_CASE ): # Tries with a `device` self.model_abit.half() # Test if we did not break anything A_ = self.tokenizer(self.input_text , return_tensors='''pt''' ) A_ = self.model_fpaa.to(torch.floataa ) A_ = self.model_fpaa.generate(input_ids=encoded_input['''input_ids'''].to(0 ) , max_new_tokens=10 ) # Check this does not throw an error A_ = self.model_fpaa.to('''cpu''' ) # Check this does not throw an error A_ = self.model_fpaa.half() # Check this does not throw an error A_ = self.model_fpaa.float() def __A ( self ) -> Optional[int]: A_ = AutoModelForSeqaSeqLM.from_pretrained('''t5-small''' , load_in_abit=_SCREAMING_SNAKE_CASE , device_map='''auto''' ) self.assertTrue(model.decoder.block[0].layer[2].DenseReluDense.wo.weight.dtype == torch.floataa ) @require_bitsandbytes @require_accelerate @require_torch @require_torch_gpu @slow class __UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' @classmethod def __A ( cls ) -> Optional[Any]: A_ = '''t5-small''' A_ = '''google/flan-t5-small''' # flan-t5 uses dense-act instead of dense-relu-dense A_ = AutoTokenizer.from_pretrained(cls.model_name ) A_ = '''Translate in German: Hello, my dog is cute''' def __A ( self ) -> Any: gc.collect() torch.cuda.empty_cache() def __A ( self ) -> Tuple: from transformers import TaForConditionalGeneration A_ = TaForConditionalGeneration._keep_in_fpaa_modules A_ = None # test with `t5-small` A_ = TaForConditionalGeneration.from_pretrained(self.model_name , load_in_abit=_SCREAMING_SNAKE_CASE , device_map='''auto''' ) A_ = self.tokenizer(self.input_text , return_tensors='''pt''' ).to(0 ) A_ = model.generate(**_SCREAMING_SNAKE_CASE ) # test with `flan-t5-small` A_ = TaForConditionalGeneration.from_pretrained( self.dense_act_model_name , load_in_abit=_SCREAMING_SNAKE_CASE , device_map='''auto''' ) A_ = self.tokenizer(self.input_text , return_tensors='''pt''' ).to(0 ) A_ = model.generate(**_SCREAMING_SNAKE_CASE ) A_ = modules def __A ( self ) -> Dict: import bitsandbytes as bnb from transformers import TaForConditionalGeneration # test with `t5-small` A_ = TaForConditionalGeneration.from_pretrained(self.model_name , load_in_abit=_SCREAMING_SNAKE_CASE , device_map='''auto''' ) # there was a bug with decoders - this test checks that it is fixed self.assertTrue(isinstance(model.decoder.block[0].layer[0].SelfAttention.q , bnb.nn.Linearabit ) ) A_ = self.tokenizer(self.input_text , return_tensors='''pt''' ).to(0 ) A_ = model.generate(**_SCREAMING_SNAKE_CASE ) # test with `flan-t5-small` A_ = TaForConditionalGeneration.from_pretrained( self.dense_act_model_name , load_in_abit=_SCREAMING_SNAKE_CASE , device_map='''auto''' ) A_ = self.tokenizer(self.input_text , return_tensors='''pt''' ).to(0 ) A_ = model.generate(**_SCREAMING_SNAKE_CASE ) class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' def __A ( self ) -> int: super().setUp() # model_name A_ = '''bigscience/bloom-560m''' A_ = '''t5-small''' # Different types of model A_ = AutoModel.from_pretrained(self.model_name , load_in_abit=_SCREAMING_SNAKE_CASE , device_map='''auto''' ) # Sequence classification model A_ = AutoModelForSequenceClassification.from_pretrained( self.model_name , load_in_abit=_SCREAMING_SNAKE_CASE , device_map='''auto''' ) # CausalLM model A_ = AutoModelForCausalLM.from_pretrained(self.model_name , load_in_abit=_SCREAMING_SNAKE_CASE , device_map='''auto''' ) # Seq2seq model A_ = AutoModelForSeqaSeqLM.from_pretrained( self.seq_to_seq_name , load_in_abit=_SCREAMING_SNAKE_CASE , device_map='''auto''' ) def __A ( self ) -> Union[str, Any]: del self.base_model del self.sequence_model del self.model_abit del self.seq_to_seq_model gc.collect() torch.cuda.empty_cache() def __A ( self ) -> List[str]: from bitsandbytes.nn import Paramsabit self.assertTrue(self.base_model.h[-1].mlp.dense_ah_to_h.weight.__class__ == Paramsabit ) # Other heads should be nn.Parameter self.assertTrue(self.model_abit.lm_head.weight.__class__ == torch.nn.Parameter ) self.assertTrue(self.sequence_model.score.weight.__class__ == torch.nn.Parameter ) self.assertTrue(self.seq_to_seq_model.lm_head.weight.__class__ == torch.nn.Parameter ) class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' def __A ( self ) -> Tuple: super().setUp() def __A ( self ) -> List[Any]: del self.pipe gc.collect() torch.cuda.empty_cache() def __A ( self ) -> Optional[Any]: A_ = pipeline( '''text-generation''' , model=self.model_name , model_kwargs={'''device_map''': '''auto''', '''load_in_4bit''': True, '''torch_dtype''': torch.floataa} , max_new_tokens=self.MAX_NEW_TOKENS , ) # Real second forward pass A_ = self.pipe(self.input_text ) self.assertIn(pipeline_output[0]['''generated_text'''] , self.EXPECTED_OUTPUTS ) @require_torch_multi_gpu class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' def __A ( self ) -> List[str]: super().setUp() def __A ( self ) -> Optional[int]: A_ = AutoModelForCausalLM.from_pretrained( self.model_name , load_in_abit=_SCREAMING_SNAKE_CASE , device_map='''balanced''' ) # Check correct device map self.assertEqual(set(model_parallel.hf_device_map.values() ) , {0, 1} ) # Check that inference pass works on the model A_ = self.tokenizer(self.input_text , return_tensors='''pt''' ) # Second real batch A_ = model_parallel.generate(input_ids=encoded_input['''input_ids'''].to(0 ) , max_new_tokens=10 ) self.assertIn(self.tokenizer.decode(output_parallel[0] , skip_special_tokens=_SCREAMING_SNAKE_CASE ) , self.EXPECTED_OUTPUTS ) class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' def __A ( self ) -> str: A_ = '''facebook/opt-350m''' super().setUp() def __A ( self ) -> Optional[int]: if version.parse(importlib.metadata.version('''bitsandbytes''' ) ) < version.parse('''0.37.0''' ): return # Step 1: freeze all parameters A_ = AutoModelForCausalLM.from_pretrained(self.model_name , load_in_abit=_SCREAMING_SNAKE_CASE ) self.assertEqual(set(model.hf_device_map.values() ) , {torch.cuda.current_device()} ) for param in model.parameters(): A_ = False # freeze the model - train adapters later if param.ndim == 1: # cast the small parameters (e.g. layernorm) to fp32 for stability A_ = param.data.to(torch.floataa ) # Step 2: add adapters for _, module in model.named_modules(): if "OPTAttention" in repr(type(_SCREAMING_SNAKE_CASE ) ): A_ = LoRALayer(module.q_proj , rank=16 ) A_ = LoRALayer(module.k_proj , rank=16 ) A_ = LoRALayer(module.v_proj , rank=16 ) # Step 3: dummy batch A_ = self.tokenizer('''Test batch ''' , return_tensors='''pt''' ).to(0 ) # Step 4: Check if the gradient is not None with torch.cuda.amp.autocast(): A_ = model.forward(**_SCREAMING_SNAKE_CASE ) out.logits.norm().backward() for module in model.modules(): if isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): self.assertTrue(module.adapter[1].weight.grad is not None ) self.assertTrue(module.adapter[1].weight.grad.norm().item() > 0 ) elif isinstance(_SCREAMING_SNAKE_CASE , nn.Embedding ): self.assertTrue(module.weight.grad is None ) class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' __lowercase : int = 'gpt2-xl' __lowercase : List[Any] = 3.3191854854152187
18
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available __snake_case : Dict = { 'configuration_bridgetower': [ 'BRIDGETOWER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'BridgeTowerConfig', 'BridgeTowerTextConfig', 'BridgeTowerVisionConfig', ], 'processing_bridgetower': ['BridgeTowerProcessor'], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case : str = ['BridgeTowerImageProcessor'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case : Union[str, Any] = [ 'BRIDGETOWER_PRETRAINED_MODEL_ARCHIVE_LIST', 'BridgeTowerForContrastiveLearning', 'BridgeTowerForImageAndTextRetrieval', 'BridgeTowerForMaskedLM', 'BridgeTowerModel', 'BridgeTowerPreTrainedModel', ] if TYPE_CHECKING: from .configuration_bridgetower import ( BRIDGETOWER_PRETRAINED_CONFIG_ARCHIVE_MAP, BridgeTowerConfig, BridgeTowerTextConfig, BridgeTowerVisionConfig, ) from .processing_bridgetower import BridgeTowerProcessor try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_bridgetower import BridgeTowerImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_bridgetower import ( BRIDGETOWER_PRETRAINED_MODEL_ARCHIVE_LIST, BridgeTowerForContrastiveLearning, BridgeTowerForImageAndTextRetrieval, BridgeTowerForMaskedLM, BridgeTowerModel, BridgeTowerPreTrainedModel, ) else: import sys __snake_case : Optional[int] = _LazyModule(__name__, globals()['__file__'], _import_structure)
18
'''simple docstring''' import os from typing import BinaryIO, Optional, Union import numpy as np import pyarrow.parquet as pq from .. import Audio, Dataset, Features, Image, NamedSplit, Value, config from ..features.features import FeatureType, _visit from ..formatting import query_table from ..packaged_modules import _PACKAGED_DATASETS_MODULES from ..packaged_modules.parquet.parquet import Parquet from ..utils import logging from ..utils.typing import NestedDataStructureLike, PathLike from .abc import AbstractDatasetReader def _UpperCAmelCase ( _UpperCamelCase : Features ) -> Optional[int]: A_ = np.inf def set_batch_size(_UpperCamelCase : FeatureType ) -> None: nonlocal batch_size if isinstance(_UpperCamelCase, _UpperCamelCase ): A_ = min(_UpperCamelCase, config.PARQUET_ROW_GROUP_SIZE_FOR_IMAGE_DATASETS ) elif isinstance(_UpperCamelCase, _UpperCamelCase ): A_ = min(_UpperCamelCase, config.PARQUET_ROW_GROUP_SIZE_FOR_AUDIO_DATASETS ) elif isinstance(_UpperCamelCase, _UpperCamelCase ) and feature.dtype == "binary": A_ = min(_UpperCamelCase, config.PARQUET_ROW_GROUP_SIZE_FOR_BINARY_DATASETS ) _visit(_UpperCamelCase, _UpperCamelCase ) return None if batch_size is np.inf else batch_size class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = False , _SCREAMING_SNAKE_CASE = False , _SCREAMING_SNAKE_CASE = None , **_SCREAMING_SNAKE_CASE , ) -> int: super().__init__( _SCREAMING_SNAKE_CASE , split=_SCREAMING_SNAKE_CASE , 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 , ) A_ = path_or_paths if isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) else {self.split: path_or_paths} A_ = _PACKAGED_DATASETS_MODULES['''parquet'''][1] A_ = Parquet( cache_dir=_SCREAMING_SNAKE_CASE , data_files=_SCREAMING_SNAKE_CASE , features=_SCREAMING_SNAKE_CASE , hash=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE , ) def __A ( self ) -> str: # Build iterable dataset if self.streaming: A_ = self.builder.as_streaming_dataset(split=self.split ) # Build regular (map-style) dataset else: A_ = None A_ = None A_ = None A_ = 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 , ) A_ = self.builder.as_dataset( split=self.split , verification_mode=_SCREAMING_SNAKE_CASE , in_memory=self.keep_in_memory ) return dataset class __UpperCAmelCase : '''simple docstring''' def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None , **_SCREAMING_SNAKE_CASE , ) -> Dict: A_ = dataset A_ = path_or_buf A_ = batch_size or get_writer_batch_size(dataset.features ) A_ = parquet_writer_kwargs def __A ( self ) -> int: A_ = self.batch_size if self.batch_size else config.DEFAULT_MAX_BATCH_SIZE if isinstance(self.path_or_buf , (str, bytes, os.PathLike) ): with open(self.path_or_buf , '''wb+''' ) as buffer: A_ = self._write(file_obj=_SCREAMING_SNAKE_CASE , batch_size=_SCREAMING_SNAKE_CASE , **self.parquet_writer_kwargs ) else: A_ = self._write(file_obj=self.path_or_buf , batch_size=_SCREAMING_SNAKE_CASE , **self.parquet_writer_kwargs ) return written def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) -> int: A_ = 0 A_ = parquet_writer_kwargs.pop('''path_or_buf''' , _SCREAMING_SNAKE_CASE ) A_ = self.dataset.features.arrow_schema A_ = pq.ParquetWriter(_SCREAMING_SNAKE_CASE , schema=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) for offset in logging.tqdm( range(0 , len(self.dataset ) , _SCREAMING_SNAKE_CASE ) , unit='''ba''' , disable=not logging.is_progress_bar_enabled() , desc='''Creating parquet from Arrow format''' , ): A_ = query_table( table=self.dataset._data , key=slice(_SCREAMING_SNAKE_CASE , offset + batch_size ) , indices=self.dataset._indices if self.dataset._indices is not None else None , ) writer.write_table(_SCREAMING_SNAKE_CASE ) written += batch.nbytes writer.close() return written
18
1
'''simple docstring''' import absl # noqa: F401 # Here to have a nice missing dependency error message early on import nltk # noqa: F401 # Here to have a nice missing dependency error message early on import numpy # noqa: F401 # Here to have a nice missing dependency error message early on import six # noqa: F401 # Here to have a nice missing dependency error message early on from rouge_score import rouge_scorer, scoring import datasets __snake_case : Any = '\\n@inproceedings{lin-2004-rouge,\n title = "{ROUGE}: A Package for Automatic Evaluation of Summaries",\n author = "Lin, Chin-Yew",\n booktitle = "Text Summarization Branches Out",\n month = jul,\n year = "2004",\n address = "Barcelona, Spain",\n publisher = "Association for Computational Linguistics",\n url = "https://www.aclweb.org/anthology/W04-1013",\n pages = "74--81",\n}\n' __snake_case : Dict = '\\nROUGE, or Recall-Oriented Understudy for Gisting Evaluation, is a set of metrics and a software package used for\nevaluating automatic summarization and machine translation software in natural language processing.\nThe metrics compare an automatically produced summary or translation against a reference or a set of references (human-produced) summary or translation.\n\nNote that ROUGE is case insensitive, meaning that upper case letters are treated the same way as lower case letters.\n\nThis metrics is a wrapper around Google Research reimplementation of ROUGE:\nhttps://github.com/google-research/google-research/tree/master/rouge\n' __snake_case : Optional[int] = '\nCalculates average rouge scores for a list of hypotheses and references\nArgs:\n predictions: list of predictions to score. Each prediction\n should be a string with tokens separated by spaces.\n references: list of reference for each prediction. Each\n reference should be a string with tokens separated by spaces.\n rouge_types: A list of rouge types to calculate.\n Valid names:\n `"rouge{n}"` (e.g. `"rouge1"`, `"rouge2"`) where: {n} is the n-gram based scoring,\n `"rougeL"`: Longest common subsequence based scoring.\n `"rougeLSum"`: rougeLsum splits text using `"\n"`.\n See details in https://github.com/huggingface/datasets/issues/617\n use_stemmer: Bool indicating whether Porter stemmer should be used to strip word suffixes.\n use_aggregator: Return aggregates if this is set to True\nReturns:\n rouge1: rouge_1 (precision, recall, f1),\n rouge2: rouge_2 (precision, recall, f1),\n rougeL: rouge_l (precision, recall, f1),\n rougeLsum: rouge_lsum (precision, recall, f1)\nExamples:\n\n >>> rouge = datasets.load_metric(\'rouge\')\n >>> predictions = ["hello there", "general kenobi"]\n >>> references = ["hello there", "general kenobi"]\n >>> results = rouge.compute(predictions=predictions, references=references)\n >>> print(list(results.keys()))\n [\'rouge1\', \'rouge2\', \'rougeL\', \'rougeLsum\']\n >>> print(results["rouge1"])\n AggregateScore(low=Score(precision=1.0, recall=1.0, fmeasure=1.0), mid=Score(precision=1.0, recall=1.0, fmeasure=1.0), high=Score(precision=1.0, recall=1.0, fmeasure=1.0))\n >>> print(results["rouge1"].mid.fmeasure)\n 1.0\n' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __UpperCAmelCase ( datasets.Metric ): '''simple docstring''' def __A ( self ) -> List[str]: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Value('''string''' , id='''sequence''' ), '''references''': datasets.Value('''string''' , id='''sequence''' ), } ) , codebase_urls=['''https://github.com/google-research/google-research/tree/master/rouge'''] , reference_urls=[ '''https://en.wikipedia.org/wiki/ROUGE_(metric)''', '''https://github.com/google-research/google-research/tree/master/rouge''', ] , ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=False ) -> Optional[int]: if rouge_types is None: A_ = ['''rouge1''', '''rouge2''', '''rougeL''', '''rougeLsum'''] A_ = rouge_scorer.RougeScorer(rouge_types=_SCREAMING_SNAKE_CASE , use_stemmer=_SCREAMING_SNAKE_CASE ) if use_aggregator: A_ = scoring.BootstrapAggregator() else: A_ = [] for ref, pred in zip(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): A_ = scorer.score(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) if use_aggregator: aggregator.add_scores(_SCREAMING_SNAKE_CASE ) else: scores.append(_SCREAMING_SNAKE_CASE ) if use_aggregator: A_ = aggregator.aggregate() else: A_ = {} for key in scores[0]: A_ = [score[key] for score in scores] return result
18
'''simple docstring''' from statistics import mean, stdev def _UpperCAmelCase ( _UpperCamelCase : list, _UpperCamelCase : int = 3 ) -> list: A_ = min(_UpperCamelCase ) A_ = max(_UpperCamelCase ) # normalize data return [round((x - x_min) / (x_max - x_min), _UpperCamelCase ) for x in data] def _UpperCAmelCase ( _UpperCamelCase : list, _UpperCamelCase : int = 3 ) -> list: A_ = mean(_UpperCamelCase ) A_ = stdev(_UpperCamelCase ) # standardize data return [round((x - mu) / (sigma), _UpperCamelCase ) for x in data]
18
1
'''simple docstring''' 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''' __lowercase : List[str] __lowercase : Optional[str] = None # Automatically constructed __lowercase : ClassVar[str] = "dict" __lowercase : ClassVar[Any] = None __lowercase : str = field(default='Translation' , init=_UpperCamelCase , repr=_UpperCamelCase ) def __call__( self ) -> List[str]: return pa.struct({lang: pa.string() for lang in sorted(self.languages )} ) def __A ( self ) -> Union["FeatureType", Dict[str, "FeatureType"]]: from .features import Value return {k: Value('''string''' ) for k in sorted(self.languages )} @dataclass class __UpperCAmelCase : '''simple docstring''' __lowercase : Optional[List] = None __lowercase : Optional[int] = None __lowercase : Optional[str] = None # Automatically constructed __lowercase : ClassVar[str] = "dict" __lowercase : ClassVar[Any] = None __lowercase : str = field(default='TranslationVariableLanguages' , init=_UpperCamelCase , repr=_UpperCamelCase ) def __A ( self ) -> Optional[Any]: A_ = sorted(set(self.languages ) ) if self.languages else None A_ = len(self.languages ) if self.languages else None def __call__( self ) -> Optional[int]: return pa.struct({'''language''': pa.list_(pa.string() ), '''translation''': pa.list_(pa.string() )} ) def __A ( self , _SCREAMING_SNAKE_CASE ) -> int: 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_ ,A_ = zip(*sorted(_SCREAMING_SNAKE_CASE ) ) return {"language": languages, "translation": translations} def __A ( self ) -> Union["FeatureType", Dict[str, "FeatureType"]]: from .features import Sequence, Value return { "language": Sequence(Value('''string''' ) ), "translation": Sequence(Value('''string''' ) ), }
18
'''simple docstring''' import logging import os import sys from dataclasses import dataclass, field from typing import Optional import evaluate import numpy as np import torch from datasets import load_dataset from PIL import Image from torchvision.transforms import ( CenterCrop, Compose, Normalize, RandomHorizontalFlip, RandomResizedCrop, Resize, ToTensor, ) import transformers from transformers import ( MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING, AutoConfig, AutoImageProcessor, AutoModelForImageClassification, HfArgumentParser, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import get_last_checkpoint from transformers.utils import check_min_version, send_example_telemetry from transformers.utils.versions import require_version __snake_case : Optional[int] = logging.getLogger(__name__) # Will error if the minimal version of Transformers is not installed. Remove at your own risks. check_min_version('4.31.0') require_version('datasets>=1.8.0', 'To fix: pip install -r examples/pytorch/image-classification/requirements.txt') __snake_case : str = list(MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING.keys()) __snake_case : int = tuple(conf.model_type for conf in MODEL_CONFIG_CLASSES) def _UpperCAmelCase ( _UpperCamelCase : str ) -> int: with open(_UpperCamelCase, '''rb''' ) as f: A_ = Image.open(_UpperCamelCase ) return im.convert('''RGB''' ) @dataclass class __UpperCAmelCase : '''simple docstring''' __lowercase : Optional[str] = field( default=_UpperCamelCase , metadata={ 'help': 'Name of a dataset from the hub (could be your own, possibly private dataset hosted on the hub).' } , ) __lowercase : Optional[str] = field( default=_UpperCamelCase , metadata={'help': 'The configuration name of the dataset to use (via the datasets library).'} ) __lowercase : Optional[str] = field(default=_UpperCamelCase , metadata={'help': 'A folder containing the training data.'} ) __lowercase : Optional[str] = field(default=_UpperCamelCase , metadata={'help': 'A folder containing the validation data.'} ) __lowercase : Optional[float] = field( default=0.15 , metadata={'help': 'Percent to split off of train for validation.'} ) __lowercase : Optional[int] = field( default=_UpperCamelCase , metadata={ 'help': ( 'For debugging purposes or quicker training, truncate the number of training examples to this ' 'value if set.' ) } , ) __lowercase : Optional[int] = field( default=_UpperCamelCase , metadata={ 'help': ( 'For debugging purposes or quicker training, truncate the number of evaluation examples to this ' 'value if set.' ) } , ) def __A ( self ) -> int: if self.dataset_name is None and (self.train_dir is None and self.validation_dir is None): raise ValueError( '''You must specify either a dataset name from the hub or a train and/or validation directory.''' ) @dataclass class __UpperCAmelCase : '''simple docstring''' __lowercase : str = field( default='google/vit-base-patch16-224-in21k' , metadata={'help': 'Path to pretrained model or model identifier from huggingface.co/models'} , ) __lowercase : Optional[str] = field( default=_UpperCamelCase , metadata={'help': 'If training from scratch, pass a model type from the list: ' + ', '.join(_UpperCamelCase )} , ) __lowercase : Optional[str] = field( default=_UpperCamelCase , metadata={'help': 'Pretrained config name or path if not the same as model_name'} ) __lowercase : Optional[str] = field( default=_UpperCamelCase , metadata={'help': 'Where do you want to store the pretrained models downloaded from s3'} ) __lowercase : str = field( default='main' , metadata={'help': 'The specific model version to use (can be a branch name, tag name or commit id).'} , ) __lowercase : str = field(default=_UpperCamelCase , metadata={'help': 'Name or path of preprocessor config.'} ) __lowercase : bool = field( default=_UpperCamelCase , metadata={ 'help': ( 'Will use the token generated when running `huggingface-cli login` (necessary to use this script ' 'with private models).' ) } , ) __lowercase : bool = field( default=_UpperCamelCase , metadata={'help': 'Will enable to load a pretrained model whose head dimensions are different.'} , ) def _UpperCAmelCase ( _UpperCamelCase : str ) -> Dict: A_ = torch.stack([example['''pixel_values'''] for example in examples] ) A_ = torch.tensor([example['''labels'''] for example in examples] ) return {"pixel_values": pixel_values, "labels": labels} def _UpperCAmelCase ( ) -> Tuple: # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. A_ = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith('''.json''' ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. A_ ,A_ ,A_ = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: A_ ,A_ ,A_ = parser.parse_args_into_dataclasses() # Sending telemetry. Tracking the example usage helps us better allocate resources to maintain them. The # information sent is the one passed as arguments along with your Python/PyTorch versions. send_example_telemetry('''run_image_classification''', _UpperCamelCase, _UpperCamelCase ) # Setup logging logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''', datefmt='''%m/%d/%Y %H:%M:%S''', handlers=[logging.StreamHandler(sys.stdout )], ) if training_args.should_log: # The default of training_args.log_level is passive, so we set log level at info here to have that default. transformers.utils.logging.set_verbosity_info() A_ = training_args.get_process_log_level() logger.setLevel(_UpperCamelCase ) transformers.utils.logging.set_verbosity(_UpperCamelCase ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Log on each process the small summary: logger.warning( F'''Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}''' + F'''distributed training: {bool(training_args.local_rank != -1 )}, 16-bits training: {training_args.fpaa}''' ) logger.info(F'''Training/evaluation parameters {training_args}''' ) # Detecting last checkpoint. A_ = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: A_ = get_last_checkpoint(training_args.output_dir ) if last_checkpoint is None and len(os.listdir(training_args.output_dir ) ) > 0: raise ValueError( F'''Output directory ({training_args.output_dir}) already exists and is not empty. ''' '''Use --overwrite_output_dir to overcome.''' ) elif last_checkpoint is not None and training_args.resume_from_checkpoint is None: logger.info( F'''Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change ''' '''the `--output_dir` or add `--overwrite_output_dir` to train from scratch.''' ) # Set seed before initializing model. set_seed(training_args.seed ) # Initialize our dataset and prepare it for the 'image-classification' task. if data_args.dataset_name is not None: A_ = load_dataset( data_args.dataset_name, data_args.dataset_config_name, cache_dir=model_args.cache_dir, task='''image-classification''', use_auth_token=True if model_args.use_auth_token else None, ) else: A_ = {} if data_args.train_dir is not None: A_ = os.path.join(data_args.train_dir, '''**''' ) if data_args.validation_dir is not None: A_ = os.path.join(data_args.validation_dir, '''**''' ) A_ = load_dataset( '''imagefolder''', data_files=_UpperCamelCase, cache_dir=model_args.cache_dir, task='''image-classification''', ) # If we don't have a validation split, split off a percentage of train as validation. A_ = None if '''validation''' in dataset.keys() else data_args.train_val_split if isinstance(data_args.train_val_split, _UpperCamelCase ) and data_args.train_val_split > 0.0: A_ = dataset['''train'''].train_test_split(data_args.train_val_split ) A_ = split['''train'''] A_ = split['''test'''] # Prepare label mappings. # We'll include these in the model's config to get human readable labels in the Inference API. A_ = dataset['''train'''].features['''labels'''].names A_ ,A_ = {}, {} for i, label in enumerate(_UpperCamelCase ): A_ = str(_UpperCamelCase ) A_ = label # Load the accuracy metric from the datasets package A_ = evaluate.load('''accuracy''' ) # Define our compute_metrics function. It takes an `EvalPrediction` object (a namedtuple with a # predictions and label_ids field) and has to return a dictionary string to float. def compute_metrics(_UpperCamelCase : Optional[Any] ): return metric.compute(predictions=np.argmax(p.predictions, axis=1 ), references=p.label_ids ) A_ = AutoConfig.from_pretrained( model_args.config_name or model_args.model_name_or_path, num_labels=len(_UpperCamelCase ), labelaid=_UpperCamelCase, idalabel=_UpperCamelCase, finetuning_task='''image-classification''', cache_dir=model_args.cache_dir, revision=model_args.model_revision, use_auth_token=True if model_args.use_auth_token else None, ) A_ = AutoModelForImageClassification.from_pretrained( model_args.model_name_or_path, from_tf=bool('''.ckpt''' in model_args.model_name_or_path ), config=_UpperCamelCase, cache_dir=model_args.cache_dir, revision=model_args.model_revision, use_auth_token=True if model_args.use_auth_token else None, ignore_mismatched_sizes=model_args.ignore_mismatched_sizes, ) A_ = AutoImageProcessor.from_pretrained( model_args.image_processor_name or model_args.model_name_or_path, cache_dir=model_args.cache_dir, revision=model_args.model_revision, use_auth_token=True if model_args.use_auth_token else None, ) # Define torchvision transforms to be applied to each image. if "shortest_edge" in image_processor.size: A_ = image_processor.size['''shortest_edge'''] else: A_ = (image_processor.size['''height'''], image_processor.size['''width''']) A_ = Normalize(mean=image_processor.image_mean, std=image_processor.image_std ) A_ = Compose( [ RandomResizedCrop(_UpperCamelCase ), RandomHorizontalFlip(), ToTensor(), normalize, ] ) A_ = Compose( [ Resize(_UpperCamelCase ), CenterCrop(_UpperCamelCase ), ToTensor(), normalize, ] ) def train_transforms(_UpperCamelCase : Dict ): A_ = [ _train_transforms(pil_img.convert('''RGB''' ) ) for pil_img in example_batch['''image'''] ] return example_batch def val_transforms(_UpperCamelCase : Any ): A_ = [_val_transforms(pil_img.convert('''RGB''' ) ) for pil_img in example_batch['''image''']] return example_batch if training_args.do_train: if "train" not in dataset: raise ValueError('''--do_train requires a train dataset''' ) if data_args.max_train_samples is not None: A_ = ( dataset['''train'''].shuffle(seed=training_args.seed ).select(range(data_args.max_train_samples ) ) ) # Set the training transforms dataset["train"].set_transform(_UpperCamelCase ) if training_args.do_eval: if "validation" not in dataset: raise ValueError('''--do_eval requires a validation dataset''' ) if data_args.max_eval_samples is not None: A_ = ( dataset['''validation'''].shuffle(seed=training_args.seed ).select(range(data_args.max_eval_samples ) ) ) # Set the validation transforms dataset["validation"].set_transform(_UpperCamelCase ) # Initalize our trainer A_ = Trainer( model=_UpperCamelCase, args=_UpperCamelCase, train_dataset=dataset['''train'''] if training_args.do_train else None, eval_dataset=dataset['''validation'''] if training_args.do_eval else None, compute_metrics=_UpperCamelCase, tokenizer=_UpperCamelCase, data_collator=_UpperCamelCase, ) # Training if training_args.do_train: A_ = None if training_args.resume_from_checkpoint is not None: A_ = training_args.resume_from_checkpoint elif last_checkpoint is not None: A_ = last_checkpoint A_ = trainer.train(resume_from_checkpoint=_UpperCamelCase ) trainer.save_model() trainer.log_metrics('''train''', train_result.metrics ) trainer.save_metrics('''train''', train_result.metrics ) trainer.save_state() # Evaluation if training_args.do_eval: A_ = trainer.evaluate() trainer.log_metrics('''eval''', _UpperCamelCase ) trainer.save_metrics('''eval''', _UpperCamelCase ) # Write model card and (optionally) push to hub A_ = { '''finetuned_from''': model_args.model_name_or_path, '''tasks''': '''image-classification''', '''dataset''': data_args.dataset_name, '''tags''': ['''image-classification''', '''vision'''], } if training_args.push_to_hub: trainer.push_to_hub(**_UpperCamelCase ) else: trainer.create_model_card(**_UpperCamelCase ) if __name__ == "__main__": main()
18
1
'''simple docstring''' import argparse import os import re __snake_case : List[str] = 'src/diffusers' # Pattern that looks at the indentation in a line. __snake_case : Tuple = re.compile(R'^(\s*)\S') # Pattern that matches `"key":" and puts `key` in group 0. __snake_case : Optional[Any] = re.compile(R'^\s*"([^"]+)":') # Pattern that matches `_import_structure["key"]` and puts `key` in group 0. __snake_case : Optional[Any] = re.compile(R'^\s*_import_structure\["([^"]+)"\]') # Pattern that matches `"key",` and puts `key` in group 0. __snake_case : int = re.compile(R'^\s*"([^"]+)",\s*$') # Pattern that matches any `[stuff]` and puts `stuff` in group 0. __snake_case : Union[str, Any] = re.compile(R'\[([^\]]+)\]') def _UpperCAmelCase ( _UpperCamelCase : str ) -> List[str]: A_ = _re_indent.search(_UpperCamelCase ) return "" if search is None else search.groups()[0] def _UpperCAmelCase ( _UpperCamelCase : str, _UpperCamelCase : str="", _UpperCamelCase : Union[str, Any]=None, _UpperCamelCase : Optional[int]=None ) -> Dict: A_ = 0 A_ = code.split('''\n''' ) if start_prompt is not None: while not lines[index].startswith(_UpperCamelCase ): index += 1 A_ = ['''\n'''.join(lines[:index] )] else: A_ = [] # We split into blocks until we get to the `end_prompt` (or the end of the block). A_ = [lines[index]] index += 1 while index < len(_UpperCamelCase ) and (end_prompt is None or not lines[index].startswith(_UpperCamelCase )): if len(lines[index] ) > 0 and get_indent(lines[index] ) == indent_level: if len(_UpperCamelCase ) > 0 and get_indent(current_block[-1] ).startswith(indent_level + ''' ''' ): current_block.append(lines[index] ) blocks.append('''\n'''.join(_UpperCamelCase ) ) if index < len(_UpperCamelCase ) - 1: A_ = [lines[index + 1]] index += 1 else: A_ = [] else: blocks.append('''\n'''.join(_UpperCamelCase ) ) A_ = [lines[index]] else: current_block.append(lines[index] ) index += 1 # Adds current block if it's nonempty. if len(_UpperCamelCase ) > 0: blocks.append('''\n'''.join(_UpperCamelCase ) ) # Add final block after end_prompt if provided. if end_prompt is not None and index < len(_UpperCamelCase ): blocks.append('''\n'''.join(lines[index:] ) ) return blocks def _UpperCAmelCase ( _UpperCamelCase : Optional[int] ) -> Union[str, Any]: def _inner(_UpperCamelCase : int ): return key(_UpperCamelCase ).lower().replace('''_''', '''''' ) return _inner def _UpperCAmelCase ( _UpperCamelCase : List[Any], _UpperCamelCase : Any=None ) -> Tuple: # If no key is provided, we use a noop. def noop(_UpperCamelCase : Dict ): return x if key is None: A_ = noop # Constants are all uppercase, they go first. A_ = [obj for obj in objects if key(_UpperCamelCase ).isupper()] # Classes are not all uppercase but start with a capital, they go second. A_ = [obj for obj in objects if key(_UpperCamelCase )[0].isupper() and not key(_UpperCamelCase ).isupper()] # Functions begin with a lowercase, they go last. A_ = [obj for obj in objects if not key(_UpperCamelCase )[0].isupper()] A_ = ignore_underscore(_UpperCamelCase ) return sorted(_UpperCamelCase, key=_UpperCamelCase ) + sorted(_UpperCamelCase, key=_UpperCamelCase ) + sorted(_UpperCamelCase, key=_UpperCamelCase ) def _UpperCAmelCase ( _UpperCamelCase : List[str] ) -> Optional[int]: # This inner function sort imports between [ ]. def _replace(_UpperCamelCase : Dict ): A_ = match.groups()[0] if "," not in imports: return F'''[{imports}]''' A_ = [part.strip().replace('''"''', '''''' ) for part in imports.split(''',''' )] # We will have a final empty element if the line finished with a comma. if len(keys[-1] ) == 0: A_ = keys[:-1] return "[" + ", ".join([F'''"{k}"''' for k in sort_objects(_UpperCamelCase )] ) + "]" A_ = import_statement.split('''\n''' ) if len(_UpperCamelCase ) > 3: # Here we have to sort internal imports that are on several lines (one per name): # key: [ # "object1", # "object2", # ... # ] # We may have to ignore one or two lines on each side. A_ = 2 if lines[1].strip() == '''[''' else 1 A_ = [(i, _re_strip_line.search(_UpperCamelCase ).groups()[0]) for i, line in enumerate(lines[idx:-idx] )] A_ = sort_objects(_UpperCamelCase, key=lambda _UpperCamelCase : x[1] ) A_ = [lines[x[0] + idx] for x in sorted_indices] return "\n".join(lines[:idx] + sorted_lines + lines[-idx:] ) elif len(_UpperCamelCase ) == 3: # Here we have to sort internal imports that are on one separate line: # key: [ # "object1", "object2", ... # ] if _re_bracket_content.search(lines[1] ) is not None: A_ = _re_bracket_content.sub(_replace, lines[1] ) else: A_ = [part.strip().replace('''"''', '''''' ) for part in lines[1].split(''',''' )] # We will have a final empty element if the line finished with a comma. if len(keys[-1] ) == 0: A_ = keys[:-1] A_ = get_indent(lines[1] ) + ''', '''.join([F'''"{k}"''' for k in sort_objects(_UpperCamelCase )] ) return "\n".join(_UpperCamelCase ) else: # Finally we have to deal with imports fitting on one line A_ = _re_bracket_content.sub(_replace, _UpperCamelCase ) return import_statement def _UpperCAmelCase ( _UpperCamelCase : int, _UpperCamelCase : Optional[int]=True ) -> Union[str, Any]: with open(_UpperCamelCase, '''r''' ) as f: A_ = f.read() if "_import_structure" not in code: return # Blocks of indent level 0 A_ = split_code_in_indented_blocks( _UpperCamelCase, start_prompt='''_import_structure = {''', end_prompt='''if TYPE_CHECKING:''' ) # We ignore block 0 (everything until start_prompt) and the last block (everything after end_prompt). for block_idx in range(1, len(_UpperCamelCase ) - 1 ): # Check if the block contains some `_import_structure`s thingy to sort. A_ = main_blocks[block_idx] A_ = block.split('''\n''' ) # Get to the start of the imports. A_ = 0 while line_idx < len(_UpperCamelCase ) and "_import_structure" not in block_lines[line_idx]: # Skip dummy import blocks if "import dummy" in block_lines[line_idx]: A_ = len(_UpperCamelCase ) else: line_idx += 1 if line_idx >= len(_UpperCamelCase ): continue # Ignore beginning and last line: they don't contain anything. A_ = '''\n'''.join(block_lines[line_idx:-1] ) A_ = get_indent(block_lines[1] ) # Slit the internal block into blocks of indent level 1. A_ = split_code_in_indented_blocks(_UpperCamelCase, indent_level=_UpperCamelCase ) # We have two categories of import key: list or _import_structure[key].append/extend A_ = _re_direct_key if '''_import_structure''' in block_lines[0] else _re_indirect_key # Grab the keys, but there is a trap: some lines are empty or just comments. A_ = [(pattern.search(_UpperCamelCase ).groups()[0] if pattern.search(_UpperCamelCase ) is not None else None) for b in internal_blocks] # We only sort the lines with a key. A_ = [(i, key) for i, key in enumerate(_UpperCamelCase ) if key is not None] A_ = [x[0] for x in sorted(_UpperCamelCase, key=lambda _UpperCamelCase : x[1] )] # We reorder the blocks by leaving empty lines/comments as they were and reorder the rest. A_ = 0 A_ = [] for i in range(len(_UpperCamelCase ) ): if keys[i] is None: reordered_blocks.append(internal_blocks[i] ) else: A_ = sort_objects_in_import(internal_blocks[sorted_indices[count]] ) reordered_blocks.append(_UpperCamelCase ) count += 1 # And we put our main block back together with its first and last line. A_ = '''\n'''.join(block_lines[:line_idx] + reordered_blocks + [block_lines[-1]] ) if code != "\n".join(_UpperCamelCase ): if check_only: return True else: print(F'''Overwriting {file}.''' ) with open(_UpperCamelCase, '''w''' ) as f: f.write('''\n'''.join(_UpperCamelCase ) ) def _UpperCAmelCase ( _UpperCamelCase : Optional[Any]=True ) -> int: A_ = [] for root, _, files in os.walk(_UpperCamelCase ): if "__init__.py" in files: A_ = sort_imports(os.path.join(_UpperCamelCase, '''__init__.py''' ), check_only=_UpperCamelCase ) if result: A_ = [os.path.join(_UpperCamelCase, '''__init__.py''' )] if len(_UpperCamelCase ) > 0: raise ValueError(F'''Would overwrite {len(_UpperCamelCase )} files, run `make style`.''' ) if __name__ == "__main__": __snake_case : Optional[Any] = argparse.ArgumentParser() parser.add_argument('--check_only', action='store_true', help='Whether to only check or fix style.') __snake_case : List[Any] = parser.parse_args() sort_imports_in_all_inits(check_only=args.check_only)
18
'''simple docstring''' import tempfile import unittest import numpy as np from huggingface_hub import HfFolder, delete_repo from requests.exceptions import HTTPError from transformers import BertConfig, is_flax_available from transformers.testing_utils import TOKEN, USER, is_staging_test, require_flax if is_flax_available(): import os from flax.core.frozen_dict import unfreeze from flax.traverse_util import flatten_dict from transformers import FlaxBertModel __snake_case : str = '0.12' # assumed parallelism: 8 @require_flax @is_staging_test class __UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' @classmethod def __A ( cls ) -> Dict: A_ = TOKEN HfFolder.save_token(_SCREAMING_SNAKE_CASE ) @classmethod def __A ( cls ) -> Optional[int]: try: delete_repo(token=cls._token , repo_id='''test-model-flax''' ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id='''valid_org/test-model-flax-org''' ) except HTTPError: pass def __A ( self ) -> str: A_ = BertConfig( vocab_size=99 , hidden_size=32 , num_hidden_layers=5 , num_attention_heads=4 , intermediate_size=37 ) A_ = FlaxBertModel(_SCREAMING_SNAKE_CASE ) model.push_to_hub('''test-model-flax''' , use_auth_token=self._token ) A_ = FlaxBertModel.from_pretrained(F'''{USER}/test-model-flax''' ) A_ = flatten_dict(unfreeze(model.params ) ) A_ = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): A_ = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(_SCREAMING_SNAKE_CASE , 1E-3 , msg=F'''{key} not identical''' ) # Reset repo delete_repo(token=self._token , repo_id='''test-model-flax''' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(_SCREAMING_SNAKE_CASE , repo_id='''test-model-flax''' , push_to_hub=_SCREAMING_SNAKE_CASE , use_auth_token=self._token ) A_ = FlaxBertModel.from_pretrained(F'''{USER}/test-model-flax''' ) A_ = flatten_dict(unfreeze(model.params ) ) A_ = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): A_ = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(_SCREAMING_SNAKE_CASE , 1E-3 , msg=F'''{key} not identical''' ) def __A ( self ) -> List[str]: A_ = BertConfig( vocab_size=99 , hidden_size=32 , num_hidden_layers=5 , num_attention_heads=4 , intermediate_size=37 ) A_ = FlaxBertModel(_SCREAMING_SNAKE_CASE ) model.push_to_hub('''valid_org/test-model-flax-org''' , use_auth_token=self._token ) A_ = FlaxBertModel.from_pretrained('''valid_org/test-model-flax-org''' ) A_ = flatten_dict(unfreeze(model.params ) ) A_ = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): A_ = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(_SCREAMING_SNAKE_CASE , 1E-3 , msg=F'''{key} not identical''' ) # Reset repo delete_repo(token=self._token , repo_id='''valid_org/test-model-flax-org''' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained( _SCREAMING_SNAKE_CASE , repo_id='''valid_org/test-model-flax-org''' , push_to_hub=_SCREAMING_SNAKE_CASE , use_auth_token=self._token ) A_ = FlaxBertModel.from_pretrained('''valid_org/test-model-flax-org''' ) A_ = flatten_dict(unfreeze(model.params ) ) A_ = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): A_ = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(_SCREAMING_SNAKE_CASE , 1E-3 , msg=F'''{key} not identical''' ) def _UpperCAmelCase ( _UpperCamelCase : Union[str, Any], _UpperCamelCase : Tuple ) -> Dict: A_ = True A_ = flatten_dict(modela.params ) A_ = flatten_dict(modela.params ) for key in flat_params_a.keys(): if np.sum(np.abs(flat_params_a[key] - flat_params_a[key] ) ) > 1E-4: A_ = False return models_are_equal @require_flax class __UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' def __A ( self ) -> List[str]: A_ = BertConfig.from_pretrained('''hf-internal-testing/tiny-bert-flax-only''' ) A_ = FlaxBertModel(_SCREAMING_SNAKE_CASE ) A_ = '''bert''' with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(os.path.join(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) with self.assertRaises(_SCREAMING_SNAKE_CASE ): A_ = FlaxBertModel.from_pretrained(_SCREAMING_SNAKE_CASE ) A_ = FlaxBertModel.from_pretrained(_SCREAMING_SNAKE_CASE , subfolder=_SCREAMING_SNAKE_CASE ) self.assertTrue(check_models_equal(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) def __A ( self ) -> List[Any]: A_ = BertConfig.from_pretrained('''hf-internal-testing/tiny-bert-flax-only''' ) A_ = FlaxBertModel(_SCREAMING_SNAKE_CASE ) A_ = '''bert''' with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(os.path.join(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) , max_shard_size='''10KB''' ) with self.assertRaises(_SCREAMING_SNAKE_CASE ): A_ = FlaxBertModel.from_pretrained(_SCREAMING_SNAKE_CASE ) A_ = FlaxBertModel.from_pretrained(_SCREAMING_SNAKE_CASE , subfolder=_SCREAMING_SNAKE_CASE ) self.assertTrue(check_models_equal(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) def __A ( self ) -> Dict: A_ = '''bert''' A_ = '''hf-internal-testing/tiny-random-bert-subfolder''' with self.assertRaises(_SCREAMING_SNAKE_CASE ): A_ = FlaxBertModel.from_pretrained(_SCREAMING_SNAKE_CASE ) A_ = FlaxBertModel.from_pretrained(_SCREAMING_SNAKE_CASE , subfolder=_SCREAMING_SNAKE_CASE ) self.assertIsNotNone(_SCREAMING_SNAKE_CASE ) def __A ( self ) -> Optional[Any]: A_ = '''bert''' A_ = '''hf-internal-testing/tiny-random-bert-sharded-subfolder''' with self.assertRaises(_SCREAMING_SNAKE_CASE ): A_ = FlaxBertModel.from_pretrained(_SCREAMING_SNAKE_CASE ) A_ = FlaxBertModel.from_pretrained(_SCREAMING_SNAKE_CASE , subfolder=_SCREAMING_SNAKE_CASE ) self.assertIsNotNone(_SCREAMING_SNAKE_CASE )
18
1
'''simple docstring''' import json import os from dataclasses import dataclass from functools import partial from typing import Callable import flax.linen as nn import jax import jax.numpy as jnp import joblib import optax import wandb from flax import jax_utils, struct, traverse_util from flax.serialization import from_bytes, to_bytes from flax.training import train_state from flax.training.common_utils import shard from tqdm.auto import tqdm from transformers import BigBirdConfig, FlaxBigBirdForQuestionAnswering from transformers.models.big_bird.modeling_flax_big_bird import FlaxBigBirdForQuestionAnsweringModule class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' __lowercase : BigBirdConfig __lowercase : jnp.dtype = jnp.floataa __lowercase : bool = True def __A ( self ) -> Union[str, Any]: super().setup() A_ = nn.Dense(5 , dtype=self.dtype ) def __call__( self , *_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) -> Any: A_ = super().__call__(*_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) A_ = self.cls(outputs[2] ) return outputs[:2] + (cls_out,) class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' __lowercase : List[Any] = FlaxBigBirdForNaturalQuestionsModule def _UpperCAmelCase ( _UpperCamelCase : Optional[Any], _UpperCamelCase : Tuple, _UpperCamelCase : Any, _UpperCamelCase : Tuple, _UpperCamelCase : Any, _UpperCamelCase : List[str] ) -> str: def cross_entropy(_UpperCamelCase : Optional[Any], _UpperCamelCase : Tuple, _UpperCamelCase : Any=None ): A_ = logits.shape[-1] A_ = (labels[..., None] == jnp.arange(_UpperCamelCase )[None]).astype('''f4''' ) A_ = jax.nn.log_softmax(_UpperCamelCase, axis=-1 ) A_ = -jnp.sum(labels * logits, axis=-1 ) if reduction is not None: A_ = reduction(_UpperCamelCase ) return loss A_ = partial(_UpperCamelCase, reduction=jnp.mean ) A_ = cross_entropy(_UpperCamelCase, _UpperCamelCase ) A_ = cross_entropy(_UpperCamelCase, _UpperCamelCase ) A_ = cross_entropy(_UpperCamelCase, _UpperCamelCase ) return (start_loss + end_loss + pooled_loss) / 3 @dataclass class __UpperCAmelCase : '''simple docstring''' __lowercase : str = "google/bigbird-roberta-base" __lowercase : int = 3000 __lowercase : int = 10500 __lowercase : int = 128 __lowercase : int = 3 __lowercase : int = 1 __lowercase : int = 5 # tx_args __lowercase : float = 3E-5 __lowercase : float = 0.0 __lowercase : int = 20000 __lowercase : float = 0.0095 __lowercase : str = "bigbird-roberta-natural-questions" __lowercase : str = "training-expt" __lowercase : str = "data/nq-training.jsonl" __lowercase : str = "data/nq-validation.jsonl" def __A ( self ) -> Optional[int]: os.makedirs(self.base_dir , exist_ok=_SCREAMING_SNAKE_CASE ) A_ = os.path.join(self.base_dir , self.save_dir ) A_ = self.batch_size_per_device * jax.device_count() @dataclass class __UpperCAmelCase : '''simple docstring''' __lowercase : int __lowercase : int = 4096 # no dynamic padding on TPUs def __call__( self , _SCREAMING_SNAKE_CASE ) -> Dict: A_ = self.collate_fn(_SCREAMING_SNAKE_CASE ) A_ = jax.tree_util.tree_map(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) return batch def __A ( self , _SCREAMING_SNAKE_CASE ) -> Any: A_ ,A_ = self.fetch_inputs(features['''input_ids'''] ) A_ = { '''input_ids''': jnp.array(_SCREAMING_SNAKE_CASE , dtype=jnp.intaa ), '''attention_mask''': jnp.array(_SCREAMING_SNAKE_CASE , dtype=jnp.intaa ), '''start_labels''': jnp.array(features['''start_token'''] , dtype=jnp.intaa ), '''end_labels''': jnp.array(features['''end_token'''] , dtype=jnp.intaa ), '''pooled_labels''': jnp.array(features['''category'''] , dtype=jnp.intaa ), } return batch def __A ( self , _SCREAMING_SNAKE_CASE ) -> Dict: A_ = [self._fetch_inputs(_SCREAMING_SNAKE_CASE ) for ids in input_ids] return zip(*_SCREAMING_SNAKE_CASE ) def __A ( self , _SCREAMING_SNAKE_CASE ) -> str: A_ = [1 for _ in range(len(_SCREAMING_SNAKE_CASE ) )] while len(_SCREAMING_SNAKE_CASE ) < self.max_length: input_ids.append(self.pad_id ) attention_mask.append(0 ) return input_ids, attention_mask def _UpperCAmelCase ( _UpperCamelCase : Union[str, Any], _UpperCamelCase : Optional[int], _UpperCamelCase : Dict=None ) -> Union[str, Any]: if seed is not None: A_ = dataset.shuffle(seed=_UpperCamelCase ) for i in range(len(_UpperCamelCase ) // batch_size ): A_ = dataset[i * batch_size : (i + 1) * batch_size] yield dict(_UpperCamelCase ) @partial(jax.pmap, axis_name='''batch''' ) def _UpperCAmelCase ( _UpperCamelCase : Dict, _UpperCamelCase : Optional[Any], **_UpperCamelCase : Optional[int] ) -> Optional[int]: def loss_fn(_UpperCamelCase : Tuple ): A_ = model_inputs.pop('''start_labels''' ) A_ = model_inputs.pop('''end_labels''' ) A_ = model_inputs.pop('''pooled_labels''' ) A_ = state.apply_fn(**_UpperCamelCase, params=_UpperCamelCase, dropout_rng=_UpperCamelCase, train=_UpperCamelCase ) A_ ,A_ ,A_ = outputs return state.loss_fn( _UpperCamelCase, _UpperCamelCase, _UpperCamelCase, _UpperCamelCase, _UpperCamelCase, _UpperCamelCase, ) A_ ,A_ = jax.random.split(_UpperCamelCase ) A_ = jax.value_and_grad(_UpperCamelCase ) A_ ,A_ = grad_fn(state.params ) A_ = jax.lax.pmean({'''loss''': loss}, axis_name='''batch''' ) A_ = jax.lax.pmean(_UpperCamelCase, '''batch''' ) A_ = state.apply_gradients(grads=_UpperCamelCase ) return state, metrics, new_drp_rng @partial(jax.pmap, axis_name='''batch''' ) def _UpperCAmelCase ( _UpperCamelCase : str, **_UpperCamelCase : str ) -> Union[str, Any]: A_ = model_inputs.pop('''start_labels''' ) A_ = model_inputs.pop('''end_labels''' ) A_ = model_inputs.pop('''pooled_labels''' ) A_ = state.apply_fn(**_UpperCamelCase, params=state.params, train=_UpperCamelCase ) A_ ,A_ ,A_ = outputs A_ = state.loss_fn(_UpperCamelCase, _UpperCamelCase, _UpperCamelCase, _UpperCamelCase, _UpperCamelCase, _UpperCamelCase ) A_ = jax.lax.pmean({'''loss''': loss}, axis_name='''batch''' ) return metrics class __UpperCAmelCase ( train_state.TrainState ): '''simple docstring''' __lowercase : Callable = struct.field(pytree_node=_UpperCamelCase ) @dataclass class __UpperCAmelCase : '''simple docstring''' __lowercase : Args __lowercase : Callable __lowercase : Callable __lowercase : Callable __lowercase : Callable __lowercase : wandb __lowercase : Callable = None def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=None ) -> Union[str, Any]: A_ = model.params A_ = TrainState.create( apply_fn=model.__call__ , params=_SCREAMING_SNAKE_CASE , tx=_SCREAMING_SNAKE_CASE , loss_fn=_SCREAMING_SNAKE_CASE , ) if ckpt_dir is not None: A_ ,A_ ,A_ ,A_ ,A_ = restore_checkpoint(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) A_ = { '''lr''': args.lr, '''init_lr''': args.init_lr, '''warmup_steps''': args.warmup_steps, '''num_train_steps''': num_train_steps, '''weight_decay''': args.weight_decay, } A_ ,A_ = build_tx(**_SCREAMING_SNAKE_CASE ) A_ = train_state.TrainState( step=_SCREAMING_SNAKE_CASE , apply_fn=model.__call__ , params=_SCREAMING_SNAKE_CASE , tx=_SCREAMING_SNAKE_CASE , opt_state=_SCREAMING_SNAKE_CASE , ) A_ = args A_ = data_collator A_ = lr A_ = params A_ = jax_utils.replicate(_SCREAMING_SNAKE_CASE ) return state def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Dict: A_ = self.args A_ = len(_SCREAMING_SNAKE_CASE ) // args.batch_size A_ = jax.random.PRNGKey(0 ) A_ = jax.random.split(_SCREAMING_SNAKE_CASE , jax.device_count() ) for epoch in range(args.max_epochs ): A_ = jnp.array(0 , dtype=jnp.floataa ) A_ = get_batched_dataset(_SCREAMING_SNAKE_CASE , args.batch_size , seed=_SCREAMING_SNAKE_CASE ) A_ = 0 for batch in tqdm(_SCREAMING_SNAKE_CASE , total=_SCREAMING_SNAKE_CASE , desc=F'''Running EPOCH-{epoch}''' ): A_ = self.data_collator(_SCREAMING_SNAKE_CASE ) A_ ,A_ ,A_ = self.train_step_fn(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) running_loss += jax_utils.unreplicate(metrics['''loss'''] ) i += 1 if i % args.logging_steps == 0: A_ = jax_utils.unreplicate(state.step ) A_ = running_loss.item() / i A_ = self.scheduler_fn(state_step - 1 ) A_ = self.evaluate(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) A_ = { '''step''': state_step.item(), '''eval_loss''': eval_loss.item(), '''tr_loss''': tr_loss, '''lr''': lr.item(), } tqdm.write(str(_SCREAMING_SNAKE_CASE ) ) self.logger.log(_SCREAMING_SNAKE_CASE , commit=_SCREAMING_SNAKE_CASE ) if i % args.save_steps == 0: self.save_checkpoint(args.save_dir + F'''-e{epoch}-s{i}''' , state=_SCREAMING_SNAKE_CASE ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Union[str, Any]: A_ = get_batched_dataset(_SCREAMING_SNAKE_CASE , self.args.batch_size ) A_ = len(_SCREAMING_SNAKE_CASE ) // self.args.batch_size A_ = jnp.array(0 , dtype=jnp.floataa ) A_ = 0 for batch in tqdm(_SCREAMING_SNAKE_CASE , total=_SCREAMING_SNAKE_CASE , desc='''Evaluating ... ''' ): A_ = self.data_collator(_SCREAMING_SNAKE_CASE ) A_ = self.val_step_fn(_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) running_loss += jax_utils.unreplicate(metrics['''loss'''] ) i += 1 return running_loss / i def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> List[Any]: A_ = jax_utils.unreplicate(_SCREAMING_SNAKE_CASE ) print(F'''SAVING CHECKPOINT IN {save_dir}''' , end=''' ... ''' ) self.model_save_fn(_SCREAMING_SNAKE_CASE , params=state.params ) with open(os.path.join(_SCREAMING_SNAKE_CASE , '''opt_state.msgpack''' ) , '''wb''' ) as f: f.write(to_bytes(state.opt_state ) ) joblib.dump(self.args , os.path.join(_SCREAMING_SNAKE_CASE , '''args.joblib''' ) ) joblib.dump(self.data_collator , os.path.join(_SCREAMING_SNAKE_CASE , '''data_collator.joblib''' ) ) with open(os.path.join(_SCREAMING_SNAKE_CASE , '''training_state.json''' ) , '''w''' ) as f: json.dump({'''step''': state.step.item()} , _SCREAMING_SNAKE_CASE ) print('''DONE''' ) def _UpperCAmelCase ( _UpperCamelCase : Dict, _UpperCamelCase : Tuple ) -> List[str]: print(F'''RESTORING CHECKPOINT FROM {save_dir}''', end=''' ... ''' ) with open(os.path.join(_UpperCamelCase, '''flax_model.msgpack''' ), '''rb''' ) as f: A_ = from_bytes(state.params, f.read() ) with open(os.path.join(_UpperCamelCase, '''opt_state.msgpack''' ), '''rb''' ) as f: A_ = from_bytes(state.opt_state, f.read() ) A_ = joblib.load(os.path.join(_UpperCamelCase, '''args.joblib''' ) ) A_ = joblib.load(os.path.join(_UpperCamelCase, '''data_collator.joblib''' ) ) with open(os.path.join(_UpperCamelCase, '''training_state.json''' ), '''r''' ) as f: A_ = json.load(_UpperCamelCase ) A_ = training_state['''step'''] print('''DONE''' ) return params, opt_state, step, args, data_collator def _UpperCAmelCase ( _UpperCamelCase : Tuple, _UpperCamelCase : Dict, _UpperCamelCase : Optional[int], _UpperCamelCase : List[Any] ) -> Optional[Any]: A_ = num_train_steps - warmup_steps A_ = optax.linear_schedule(init_value=_UpperCamelCase, end_value=_UpperCamelCase, transition_steps=_UpperCamelCase ) A_ = optax.linear_schedule(init_value=_UpperCamelCase, end_value=1E-7, transition_steps=_UpperCamelCase ) A_ = optax.join_schedules(schedules=[warmup_fn, decay_fn], boundaries=[warmup_steps] ) return lr def _UpperCAmelCase ( _UpperCamelCase : int, _UpperCamelCase : Optional[Any], _UpperCamelCase : Optional[int], _UpperCamelCase : List[str], _UpperCamelCase : Tuple ) -> Dict: def weight_decay_mask(_UpperCamelCase : Optional[int] ): A_ = traverse_util.flatten_dict(_UpperCamelCase ) A_ = {k: (v[-1] != '''bias''' and v[-2:] != ('''LayerNorm''', '''scale''')) for k, v in params.items()} return traverse_util.unflatten_dict(_UpperCamelCase ) A_ = scheduler_fn(_UpperCamelCase, _UpperCamelCase, _UpperCamelCase, _UpperCamelCase ) A_ = optax.adamw(learning_rate=_UpperCamelCase, weight_decay=_UpperCamelCase, mask=_UpperCamelCase ) return tx, lr
18
'''simple docstring''' def _UpperCAmelCase ( _UpperCamelCase : Union[str, Any] ) -> Dict: A_ = 1 A_ = 2 while i * i <= n: A_ = 0 while n % i == 0: n //= i multiplicity += 1 n_divisors *= multiplicity + 1 i += 1 if n > 1: n_divisors *= 2 return n_divisors def _UpperCAmelCase ( ) -> Optional[int]: A_ = 1 A_ = 1 while True: i += 1 t_num += i if count_divisors(_UpperCamelCase ) > 5_00: break return t_num if __name__ == "__main__": print(solution())
18
1
'''simple docstring''' from __future__ import annotations def _UpperCAmelCase ( _UpperCamelCase : list[list[int]] ) -> bool: A_ = len(_UpperCamelCase ) # We need to create solution object to save path. A_ = [[0 for _ in range(_UpperCamelCase )] for _ in range(_UpperCamelCase )] A_ = run_maze(_UpperCamelCase, 0, 0, _UpperCamelCase ) if solved: print('''\n'''.join(str(_UpperCamelCase ) for row in solutions ) ) else: print('''No solution exists!''' ) return solved def _UpperCAmelCase ( _UpperCamelCase : list[list[int]], _UpperCamelCase : int, _UpperCamelCase : int, _UpperCamelCase : list[list[int]] ) -> bool: A_ = len(_UpperCamelCase ) # Final check point. if i == j == (size - 1): A_ = 1 return True A_ = (not i < 0) and (not j < 0) # Check lower bounds A_ = (i < size) and (j < size) # Check upper bounds if lower_flag and upper_flag: # check for already visited and block points. A_ = (not solutions[i][j]) and (not maze[i][j]) if block_flag: # check visited A_ = 1 # check for directions if ( run_maze(_UpperCamelCase, i + 1, _UpperCamelCase, _UpperCamelCase ) or run_maze(_UpperCamelCase, _UpperCamelCase, j + 1, _UpperCamelCase ) or run_maze(_UpperCamelCase, i - 1, _UpperCamelCase, _UpperCamelCase ) or run_maze(_UpperCamelCase, _UpperCamelCase, j - 1, _UpperCamelCase ) ): return True A_ = 0 return False return False if __name__ == "__main__": import doctest doctest.testmod()
18
'''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 __UpperCAmelCase : '''simple docstring''' def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=2 , _SCREAMING_SNAKE_CASE=32 , _SCREAMING_SNAKE_CASE=16 , _SCREAMING_SNAKE_CASE=3 , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=32 , _SCREAMING_SNAKE_CASE=4 , _SCREAMING_SNAKE_CASE=[0, 1, 2, 3] , _SCREAMING_SNAKE_CASE=4 , _SCREAMING_SNAKE_CASE=37 , _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, 384, 24, 24] , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=None , ) -> Tuple: 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 __A ( self ) -> Optional[Any]: 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 __A ( self ) -> Optional[Any]: A_ = { '''global_padding''': '''same''', '''layer_type''': '''bottleneck''', '''depths''': [3, 4, 9], '''out_features''': ['''stage1''', '''stage2''', '''stage3'''], '''embedding_dynamic_padding''': True, '''hidden_sizes''': [96, 192, 384, 768], '''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 __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Optional[Any]: 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 __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> 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 __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Union[str, 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 __A ( self ) -> Optional[int]: A_ = self.prepare_config_and_inputs() A_ ,A_ ,A_ = config_and_inputs A_ = {'''pixel_values''': pixel_values} return config, inputs_dict @require_torch class __UpperCAmelCase ( _UpperCamelCase , _UpperCamelCase , unittest.TestCase ): '''simple docstring''' __lowercase : Optional[int] = (DPTModel, DPTForDepthEstimation, DPTForSemanticSegmentation) if is_torch_available() else () __lowercase : Optional[int] = ( { 'depth-estimation': DPTForDepthEstimation, 'feature-extraction': DPTModel, 'image-segmentation': DPTForSemanticSegmentation, } if is_torch_available() else {} ) __lowercase : Any = False __lowercase : Tuple = False __lowercase : List[Any] = False def __A ( self ) -> Tuple: A_ = DPTModelTester(self ) A_ = ConfigTester(self , config_class=_SCREAMING_SNAKE_CASE , has_text_modality=_SCREAMING_SNAKE_CASE , hidden_size=37 ) def __A ( self ) -> Dict: self.config_tester.run_common_tests() @unittest.skip(reason='''DPT does not use inputs_embeds''' ) def __A ( self ) -> Union[str, Any]: pass def __A ( self ) -> Dict: A_ ,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 __A ( self ) -> Optional[int]: A_ ,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 __A ( self ) -> str: A_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE ) def __A ( self ) -> str: A_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_depth_estimation(*_SCREAMING_SNAKE_CASE ) def __A ( self ) -> Optional[Any]: A_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_semantic_segmentation(*_SCREAMING_SNAKE_CASE ) def __A ( self ) -> Any: for model_class in self.all_model_classes: if model_class.__name__ == "DPTForDepthEstimation": continue A_ ,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 __A ( self ) -> Any: for model_class in self.all_model_classes: if model_class.__name__ == "DPTForDepthEstimation": continue A_ ,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 __A ( self ) -> Tuple: A_ ,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 __A ( self ) -> int: pass @slow def __A ( self ) -> Dict: for model_name in DPT_PRETRAINED_MODEL_ARCHIVE_LIST[1:]: A_ = DPTModel.from_pretrained(_SCREAMING_SNAKE_CASE ) self.assertIsNotNone(_SCREAMING_SNAKE_CASE ) def __A ( self ) -> Optional[int]: # We do this test only for DPTForDepthEstimation since it is the only model that uses readout_type A_ ,A_ = self.model_tester.prepare_config_and_inputs_for_common() A_ = '''add''' with self.assertRaises(_SCREAMING_SNAKE_CASE ): A_ = DPTForDepthEstimation(_SCREAMING_SNAKE_CASE ) def _UpperCAmelCase ( ) -> Optional[int]: A_ = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_torch @require_vision @slow class __UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' def __A ( self ) -> Any: 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, 384, 384) ) self.assertEqual(predicted_depth.shape , _SCREAMING_SNAKE_CASE ) A_ = torch.tensor( [[[5.6_437, 5.6_146, 5.6_511], [5.4_371, 5.5_649, 5.5_958], [5.5_215, 5.5_184, 5.5_293]]] ).to(_SCREAMING_SNAKE_CASE ) self.assertTrue(torch.allclose(outputs.predicted_depth[:3, :3, :3] / 100 , _SCREAMING_SNAKE_CASE , atol=1E-4 ) )
18
1
'''simple docstring''' import json import os from functools import lru_cache from typing import List, Optional, Tuple import regex as re from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging __snake_case : Tuple = logging.get_logger(__name__) __snake_case : str = {'vocab_file': 'vocab.json', 'merges_file': 'merges.txt'} __snake_case : Optional[Any] = { 'vocab_file': { 'allenai/longformer-base-4096': 'https://huggingface.co/allenai/longformer-base-4096/resolve/main/vocab.json', 'allenai/longformer-large-4096': ( 'https://huggingface.co/allenai/longformer-large-4096/resolve/main/vocab.json' ), 'allenai/longformer-large-4096-finetuned-triviaqa': ( 'https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/vocab.json' ), 'allenai/longformer-base-4096-extra.pos.embd.only': ( 'https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/vocab.json' ), 'allenai/longformer-large-4096-extra.pos.embd.only': ( 'https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/vocab.json' ), }, 'merges_file': { 'allenai/longformer-base-4096': 'https://huggingface.co/allenai/longformer-base-4096/resolve/main/merges.txt', 'allenai/longformer-large-4096': ( 'https://huggingface.co/allenai/longformer-large-4096/resolve/main/merges.txt' ), 'allenai/longformer-large-4096-finetuned-triviaqa': ( 'https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/merges.txt' ), 'allenai/longformer-base-4096-extra.pos.embd.only': ( 'https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/merges.txt' ), 'allenai/longformer-large-4096-extra.pos.embd.only': ( 'https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/merges.txt' ), }, } __snake_case : List[str] = { 'allenai/longformer-base-4096': 4_096, 'allenai/longformer-large-4096': 4_096, 'allenai/longformer-large-4096-finetuned-triviaqa': 4_096, 'allenai/longformer-base-4096-extra.pos.embd.only': 4_096, 'allenai/longformer-large-4096-extra.pos.embd.only': 4_096, } @lru_cache() # Copied from transformers.models.roberta.tokenization_roberta.bytes_to_unicode def _UpperCAmelCase ( ) -> int: A_ = ( list(range(ord('''!''' ), ord('''~''' ) + 1 ) ) + list(range(ord('''¡''' ), ord('''¬''' ) + 1 ) ) + list(range(ord('''®''' ), ord('''ÿ''' ) + 1 ) ) ) A_ = bs[:] A_ = 0 for b in range(2**8 ): if b not in bs: bs.append(_UpperCamelCase ) cs.append(2**8 + n ) n += 1 A_ = [chr(_UpperCamelCase ) for n in cs] return dict(zip(_UpperCamelCase, _UpperCamelCase ) ) def _UpperCAmelCase ( _UpperCamelCase : int ) -> Optional[Any]: A_ = set() A_ = word[0] for char in word[1:]: pairs.add((prev_char, char) ) A_ = char return pairs class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' __lowercase : Tuple = VOCAB_FILES_NAMES __lowercase : Tuple = PRETRAINED_VOCAB_FILES_MAP __lowercase : int = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowercase : Tuple = ['input_ids', 'attention_mask'] def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE="replace" , _SCREAMING_SNAKE_CASE="<s>" , _SCREAMING_SNAKE_CASE="</s>" , _SCREAMING_SNAKE_CASE="</s>" , _SCREAMING_SNAKE_CASE="<s>" , _SCREAMING_SNAKE_CASE="<unk>" , _SCREAMING_SNAKE_CASE="<pad>" , _SCREAMING_SNAKE_CASE="<mask>" , _SCREAMING_SNAKE_CASE=False , **_SCREAMING_SNAKE_CASE , ) -> Tuple: A_ = AddedToken(_SCREAMING_SNAKE_CASE , lstrip=_SCREAMING_SNAKE_CASE , rstrip=_SCREAMING_SNAKE_CASE ) if isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) else bos_token A_ = AddedToken(_SCREAMING_SNAKE_CASE , lstrip=_SCREAMING_SNAKE_CASE , rstrip=_SCREAMING_SNAKE_CASE ) if isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) else eos_token A_ = AddedToken(_SCREAMING_SNAKE_CASE , lstrip=_SCREAMING_SNAKE_CASE , rstrip=_SCREAMING_SNAKE_CASE ) if isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) else sep_token A_ = AddedToken(_SCREAMING_SNAKE_CASE , lstrip=_SCREAMING_SNAKE_CASE , rstrip=_SCREAMING_SNAKE_CASE ) if isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) else cls_token A_ = AddedToken(_SCREAMING_SNAKE_CASE , lstrip=_SCREAMING_SNAKE_CASE , rstrip=_SCREAMING_SNAKE_CASE ) if isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) else unk_token A_ = AddedToken(_SCREAMING_SNAKE_CASE , lstrip=_SCREAMING_SNAKE_CASE , rstrip=_SCREAMING_SNAKE_CASE ) if isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) else pad_token # Mask token behave like a normal word, i.e. include the space before it A_ = AddedToken(_SCREAMING_SNAKE_CASE , lstrip=_SCREAMING_SNAKE_CASE , rstrip=_SCREAMING_SNAKE_CASE ) if isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) else mask_token super().__init__( errors=_SCREAMING_SNAKE_CASE , bos_token=_SCREAMING_SNAKE_CASE , eos_token=_SCREAMING_SNAKE_CASE , unk_token=_SCREAMING_SNAKE_CASE , sep_token=_SCREAMING_SNAKE_CASE , cls_token=_SCREAMING_SNAKE_CASE , pad_token=_SCREAMING_SNAKE_CASE , mask_token=_SCREAMING_SNAKE_CASE , add_prefix_space=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE , ) with open(_SCREAMING_SNAKE_CASE , encoding='''utf-8''' ) as vocab_handle: A_ = json.load(_SCREAMING_SNAKE_CASE ) A_ = {v: k for k, v in self.encoder.items()} A_ = errors # how to handle errors in decoding A_ = bytes_to_unicode() A_ = {v: k for k, v in self.byte_encoder.items()} with open(_SCREAMING_SNAKE_CASE , encoding='''utf-8''' ) as merges_handle: A_ = merges_handle.read().split('''\n''' )[1:-1] A_ = [tuple(merge.split() ) for merge in bpe_merges] A_ = dict(zip(_SCREAMING_SNAKE_CASE , range(len(_SCREAMING_SNAKE_CASE ) ) ) ) A_ = {} A_ = add_prefix_space # Should have added re.IGNORECASE so BPE merges can happen for capitalized versions of contractions A_ = re.compile(R'''\'s|\'t|\'re|\'ve|\'m|\'ll|\'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+''' ) @property def __A ( self ) -> Optional[Any]: return len(self.encoder ) def __A ( self ) -> Dict: return dict(self.encoder , **self.added_tokens_encoder ) def __A ( self , _SCREAMING_SNAKE_CASE ) -> Dict: if token in self.cache: return self.cache[token] A_ = tuple(_SCREAMING_SNAKE_CASE ) A_ = get_pairs(_SCREAMING_SNAKE_CASE ) if not pairs: return token while True: A_ = min(_SCREAMING_SNAKE_CASE , key=lambda _SCREAMING_SNAKE_CASE : self.bpe_ranks.get(_SCREAMING_SNAKE_CASE , float('''inf''' ) ) ) if bigram not in self.bpe_ranks: break A_ ,A_ = 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 return word def __A ( self , _SCREAMING_SNAKE_CASE ) -> Tuple: A_ = [] for token in re.findall(self.pat , _SCREAMING_SNAKE_CASE ): A_ = ''''''.join( self.byte_encoder[b] for b in token.encode('''utf-8''' ) ) # Maps all our bytes to unicode strings, avoiding control tokens of the BPE (spaces in our case) bpe_tokens.extend(bpe_token for bpe_token in self.bpe(_SCREAMING_SNAKE_CASE ).split(''' ''' ) ) return bpe_tokens def __A ( self , _SCREAMING_SNAKE_CASE ) -> Optional[int]: return self.encoder.get(_SCREAMING_SNAKE_CASE , self.encoder.get(self.unk_token ) ) def __A ( self , _SCREAMING_SNAKE_CASE ) -> Optional[Any]: return self.decoder.get(_SCREAMING_SNAKE_CASE ) def __A ( self , _SCREAMING_SNAKE_CASE ) -> Optional[Any]: A_ = ''''''.join(_SCREAMING_SNAKE_CASE ) A_ = bytearray([self.byte_decoder[c] for c in text] ).decode('''utf-8''' , errors=self.errors ) return text def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None ) -> Tuple[str]: 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'''] ) with open(_SCREAMING_SNAKE_CASE , '''w''' , encoding='''utf-8''' ) as f: f.write(json.dumps(self.encoder , indent=2 , sort_keys=_SCREAMING_SNAKE_CASE , ensure_ascii=_SCREAMING_SNAKE_CASE ) + '''\n''' ) A_ = 0 with open(_SCREAMING_SNAKE_CASE , '''w''' , encoding='''utf-8''' ) as writer: writer.write('''#version: 0.2\n''' ) for bpe_tokens, token_index in sorted(self.bpe_ranks.items() , key=lambda _SCREAMING_SNAKE_CASE : kv[1] ): if index != token_index: logger.warning( F'''Saving vocabulary to {merge_file}: BPE merge indices are not consecutive.''' ''' Please check that the tokenizer is not corrupted!''' ) A_ = token_index writer.write(''' '''.join(_SCREAMING_SNAKE_CASE ) + '''\n''' ) index += 1 return vocab_file, merge_file def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None ) -> List[int]: 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 __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = False ) -> List[int]: 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 __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None ) -> List[int]: 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] def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=False , **_SCREAMING_SNAKE_CASE ) -> List[str]: A_ = kwargs.pop('''add_prefix_space''' , self.add_prefix_space ) if (is_split_into_words or add_prefix_space) and (len(_SCREAMING_SNAKE_CASE ) > 0 and not text[0].isspace()): A_ = ''' ''' + text return (text, kwargs)
18
'''simple docstring''' import math def _UpperCAmelCase ( _UpperCamelCase : float, _UpperCamelCase : float ) -> float: if initial_intensity < 0: raise ValueError('''The value of intensity cannot be negative''' ) # handling of negative values of initial intensity if angle < 0 or angle > 3_60: raise ValueError('''In Malus Law, the angle is in the range 0-360 degrees''' ) # handling of values out of allowed range return initial_intensity * (math.cos(math.radians(_UpperCamelCase ) ) ** 2) if __name__ == "__main__": import doctest doctest.testmod(name='malus_law')
18
1
'''simple docstring''' from math import factorial, pi def _UpperCAmelCase ( _UpperCamelCase : float, _UpperCamelCase : int = 30 ) -> float: if not isinstance(_UpperCamelCase, (int, float) ): raise ValueError('''maclaurin_sin() requires either an int or float for theta''' ) if not isinstance(_UpperCamelCase, _UpperCamelCase ) or accuracy <= 0: raise ValueError('''maclaurin_sin() requires a positive int for accuracy''' ) A_ = float(_UpperCamelCase ) A_ = theta // (2 * pi) theta -= 2 * div * pi return sum( (-1) ** r * theta ** (2 * r + 1) / factorial(2 * r + 1 ) for r in range(_UpperCamelCase ) ) def _UpperCAmelCase ( _UpperCamelCase : float, _UpperCamelCase : int = 30 ) -> float: if not isinstance(_UpperCamelCase, (int, float) ): raise ValueError('''maclaurin_cos() requires either an int or float for theta''' ) if not isinstance(_UpperCamelCase, _UpperCamelCase ) or accuracy <= 0: raise ValueError('''maclaurin_cos() requires a positive int for accuracy''' ) A_ = float(_UpperCamelCase ) A_ = theta // (2 * pi) theta -= 2 * div * pi return sum((-1) ** r * theta ** (2 * r) / factorial(2 * r ) for r in range(_UpperCamelCase ) ) if __name__ == "__main__": import doctest doctest.testmod() print(maclaurin_sin(10)) print(maclaurin_sin(-10)) print(maclaurin_sin(10, 15)) print(maclaurin_sin(-10, 15)) print(maclaurin_cos(5)) print(maclaurin_cos(-5)) print(maclaurin_cos(10, 15)) print(maclaurin_cos(-10, 15))
18
'''simple docstring''' from ...utils import ( OptionalDependencyNotAvailable, is_torch_available, is_transformers_available, is_transformers_version, ) try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import ShapEPipeline else: from .camera import create_pan_cameras from .pipeline_shap_e import ShapEPipeline from .pipeline_shap_e_img2img import ShapEImgaImgPipeline from .renderer import ( BoundingBoxVolume, ImportanceRaySampler, MLPNeRFModelOutput, MLPNeRSTFModel, ShapEParamsProjModel, ShapERenderer, StratifiedRaySampler, VoidNeRFModel, )
18
1
'''simple docstring''' from __future__ import annotations import requests __snake_case : List[Any] = set( 'approved_at_utc approved_by author_flair_background_color\nauthor_flair_css_class author_flair_richtext author_flair_template_id author_fullname\nauthor_premium can_mod_post category clicked content_categories created_utc downs\nedited gilded gildings hidden hide_score is_created_from_ads_ui is_meta\nis_original_content is_reddit_media_domain is_video link_flair_css_class\nlink_flair_richtext link_flair_text link_flair_text_color media_embed mod_reason_title\nname permalink pwls quarantine saved score secure_media secure_media_embed selftext\nsubreddit subreddit_name_prefixed subreddit_type thumbnail title top_awarded_type\ntotal_awards_received ups upvote_ratio url user_reports'.split() ) def _UpperCAmelCase ( _UpperCamelCase : str, _UpperCamelCase : int = 1, _UpperCamelCase : str = "new", _UpperCamelCase : list | None = None ) -> dict: A_ = wanted_data or [] if invalid_search_terms := ", ".join(sorted(set(_UpperCamelCase ) - valid_terms ) ): A_ = F'''Invalid search term: {invalid_search_terms}''' raise ValueError(_UpperCamelCase ) A_ = requests.get( F'''https://reddit.com/r/{subreddit}/{age}.json?limit={limit}''', headers={'''User-agent''': '''A random string'''}, ) if response.status_code == 4_29: raise requests.HTTPError A_ = response.json() if not wanted_data: return {id_: data["data"]["children"][id_] for id_ in range(_UpperCamelCase )} A_ = {} for id_ in range(_UpperCamelCase ): A_ = { item: data['''data''']['''children'''][id_]['''data'''][item] for item in wanted_data } return data_dict if __name__ == "__main__": # If you get Error 429, that means you are rate limited.Try after some time print(get_subreddit_data('learnpython', wanted_data=['title', 'url', 'selftext']))
18
'''simple docstring''' import argparse from pathlib import Path import torch from transformers import OPTConfig, OPTModel from transformers.utils import logging logging.set_verbosity_info() __snake_case : Any = logging.get_logger(__name__) def _UpperCAmelCase ( _UpperCamelCase : Dict ) -> List[str]: A_ = torch.load(_UpperCamelCase, map_location='''cpu''' ) if "model" in sd.keys(): A_ = torch.load(_UpperCamelCase, map_location='''cpu''' )['''model'''] # pop unnecessary weights A_ = [ '''decoder.version''', '''decoder.output_projection.weight''', ] for key in keys_to_delete: if key in sd: sd.pop(_UpperCamelCase ) A_ = { '''decoder.project_in_dim.weight''': '''decoder.project_in.weight''', '''decoder.project_out_dim.weight''': '''decoder.project_out.weight''', '''decoder.layer_norm.weight''': '''decoder.final_layer_norm.weight''', '''decoder.layer_norm.bias''': '''decoder.final_layer_norm.bias''', } for old_key, new_key in keys_to_rename.items(): if old_key in sd: A_ = sd.pop(_UpperCamelCase ) A_ = list(sd.keys() ) for key in keys: if ".qkv_proj." in key: A_ = sd[key] # We split QKV in separate Q,K,V A_ = key.replace('''.qkv_proj.''', '''.q_proj.''' ) A_ = key.replace('''.qkv_proj.''', '''.k_proj.''' ) A_ = key.replace('''.qkv_proj.''', '''.v_proj.''' ) A_ = value.shape[0] assert depth % 3 == 0 # `SequeuceParallelTransformerBlock` has QKV weight is separated in K,V,Q despite the naming: # https://cs.github.com/facebookresearch/metaseq/blob/51871bd73cd04c038f239ea2a26db1d7f6b37927/metaseq/modules/sequence_parallel_transformer_layer.py#L97 A_ ,A_ ,A_ = torch.split(_UpperCamelCase, depth // 3, dim=0 ) A_ = q A_ = k A_ = v del sd[key] return sd @torch.no_grad() def _UpperCAmelCase ( _UpperCamelCase : Optional[int], _UpperCamelCase : Optional[Any], _UpperCamelCase : List[str]=None ) -> Dict: A_ = load_checkpoint(_UpperCamelCase ) if config is not None: A_ = OPTConfig.from_pretrained(_UpperCamelCase ) else: A_ = OPTConfig() A_ = OPTModel(_UpperCamelCase ).half().eval() model.load_state_dict(_UpperCamelCase ) # Check results Path(_UpperCamelCase ).mkdir(exist_ok=_UpperCamelCase ) model.save_pretrained(_UpperCamelCase ) if __name__ == "__main__": __snake_case : Optional[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '--fairseq_path', type=str, help=( 'path to fairseq checkpoint in correct format. You can find all checkpoints in the correct format here:' ' https://huggingface.co/models?other=opt_metasq' ), ) parser.add_argument('--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.') parser.add_argument('--hf_config', default=None, type=str, help='Define HF config.') __snake_case : Optional[Any] = parser.parse_args() convert_opt_checkpoint(args.fairseq_path, args.pytorch_dump_folder_path, config=args.hf_config)
18
1
'''simple docstring''' from collections import defaultdict from math import gcd def _UpperCAmelCase ( _UpperCamelCase : int = 1_50_00_00 ) -> int: A_ = defaultdict(_UpperCamelCase ) A_ = 2 while 2 * euclid_m * (euclid_m + 1) <= limit: for euclid_n in range((euclid_m % 2) + 1, _UpperCamelCase, 2 ): if gcd(_UpperCamelCase, _UpperCamelCase ) > 1: continue A_ = 2 * euclid_m * (euclid_m + euclid_n) for perimeter in range(_UpperCamelCase, limit + 1, _UpperCamelCase ): frequencies[perimeter] += 1 euclid_m += 1 return sum(1 for frequency in frequencies.values() if frequency == 1 ) if __name__ == "__main__": print(F"""{solution() = }""")
18
'''simple docstring''' from typing import List, Optional, Tuple from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_herbert import HerbertTokenizer __snake_case : Optional[Any] = logging.get_logger(__name__) __snake_case : Tuple = {'vocab_file': 'vocab.json', 'merges_file': 'merges.txt', 'tokenizer_file': 'tokenizer.json'} __snake_case : Optional[Any] = { 'vocab_file': { 'allegro/herbert-base-cased': 'https://huggingface.co/allegro/herbert-base-cased/resolve/main/vocab.json' }, 'merges_file': { 'allegro/herbert-base-cased': 'https://huggingface.co/allegro/herbert-base-cased/resolve/main/merges.txt' }, } __snake_case : Tuple = {'allegro/herbert-base-cased': 514} __snake_case : List[str] = {} class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' __lowercase : Any = VOCAB_FILES_NAMES __lowercase : Union[str, Any] = PRETRAINED_VOCAB_FILES_MAP __lowercase : Dict = PRETRAINED_INIT_CONFIGURATION __lowercase : List[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowercase : Optional[int] = HerbertTokenizer def __init__( self , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE="<s>" , _SCREAMING_SNAKE_CASE="<unk>" , _SCREAMING_SNAKE_CASE="<pad>" , _SCREAMING_SNAKE_CASE="<mask>" , _SCREAMING_SNAKE_CASE="</s>" , **_SCREAMING_SNAKE_CASE , ) -> int: super().__init__( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , tokenizer_file=_SCREAMING_SNAKE_CASE , cls_token=_SCREAMING_SNAKE_CASE , unk_token=_SCREAMING_SNAKE_CASE , pad_token=_SCREAMING_SNAKE_CASE , mask_token=_SCREAMING_SNAKE_CASE , sep_token=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE , ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None ) -> List[int]: A_ = [self.cls_token_id] A_ = [self.sep_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 , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = False ) -> List[int]: 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] + ([0] * len(_SCREAMING_SNAKE_CASE )) + [1] def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None ) -> List[int]: 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 ) * [0] + len(token_ids_a + sep ) * [1] def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None ) -> Tuple[str]: A_ = self._tokenizer.model.save(_SCREAMING_SNAKE_CASE , name=_SCREAMING_SNAKE_CASE ) return tuple(_SCREAMING_SNAKE_CASE )
18
1
'''simple docstring''' def _UpperCAmelCase ( _UpperCamelCase : int ) -> bool: return sum(i for i in range(1, number // 2 + 1 ) if number % i == 0 ) == number if __name__ == "__main__": print('Program to check whether a number is a Perfect number or not...') __snake_case : Any = int(input('Enter number: ').strip()) print(F"""{number} is {'' if perfect(number) else 'not '}a Perfect Number.""")
18
'''simple docstring''' # Copyright 2022 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 import os import platform import numpy as np import psutil import torch from accelerate import __version__ as version from accelerate.commands.config import default_config_file, load_config_from_file from ..utils import is_npu_available, is_xpu_available def _UpperCAmelCase ( _UpperCamelCase : Union[str, Any]=None ) -> List[Any]: if subparsers is not None: A_ = subparsers.add_parser('''env''' ) else: A_ = argparse.ArgumentParser('''Accelerate env command''' ) parser.add_argument( '''--config_file''', default=_UpperCamelCase, help='''The config file to use for the default values in the launching script.''' ) if subparsers is not None: parser.set_defaults(func=_UpperCamelCase ) return parser def _UpperCAmelCase ( _UpperCamelCase : Dict ) -> Dict: A_ = torch.__version__ A_ = torch.cuda.is_available() A_ = is_xpu_available() A_ = is_npu_available() A_ = '''Not found''' # Get the default from the config file. if args.config_file is not None or os.path.isfile(_UpperCamelCase ): A_ = load_config_from_file(args.config_file ).to_dict() A_ = { '''`Accelerate` version''': version, '''Platform''': platform.platform(), '''Python version''': platform.python_version(), '''Numpy version''': np.__version__, '''PyTorch version (GPU?)''': F'''{pt_version} ({pt_cuda_available})''', '''PyTorch XPU available''': str(_UpperCamelCase ), '''PyTorch NPU available''': str(_UpperCamelCase ), '''System RAM''': F'''{psutil.virtual_memory().total / 10_24 ** 3:.2f} GB''', } if pt_cuda_available: A_ = torch.cuda.get_device_name() print('''\nCopy-and-paste the text below in your GitHub issue\n''' ) print('''\n'''.join([F'''- {prop}: {val}''' for prop, val in info.items()] ) ) print('''- `Accelerate` default config:''' if args.config_file is None else '''- `Accelerate` config passed:''' ) A_ = ( '''\n'''.join([F'''\t- {prop}: {val}''' for prop, val in accelerate_config.items()] ) if isinstance(_UpperCamelCase, _UpperCamelCase ) else F'''\t{accelerate_config}''' ) print(_UpperCamelCase ) A_ = accelerate_config return info def _UpperCAmelCase ( ) -> int: A_ = env_command_parser() A_ = parser.parse_args() env_command(_UpperCamelCase ) return 0 if __name__ == "__main__": raise SystemExit(main())
18
1
'''simple docstring''' def _UpperCAmelCase ( _UpperCamelCase : int, _UpperCamelCase : int ) -> int: while b: A_ ,A_ = b, a % b return a def _UpperCAmelCase ( _UpperCamelCase : int, _UpperCamelCase : int ) -> int: return a if b == 0 else euclidean_gcd_recursive(_UpperCamelCase, a % b ) def _UpperCAmelCase ( ) -> List[str]: print(F'''euclidean_gcd(3, 5) = {euclidean_gcd(3, 5 )}''' ) print(F'''euclidean_gcd(5, 3) = {euclidean_gcd(5, 3 )}''' ) print(F'''euclidean_gcd(1, 3) = {euclidean_gcd(1, 3 )}''' ) print(F'''euclidean_gcd(3, 6) = {euclidean_gcd(3, 6 )}''' ) print(F'''euclidean_gcd(6, 3) = {euclidean_gcd(6, 3 )}''' ) print(F'''euclidean_gcd_recursive(3, 5) = {euclidean_gcd_recursive(3, 5 )}''' ) print(F'''euclidean_gcd_recursive(5, 3) = {euclidean_gcd_recursive(5, 3 )}''' ) print(F'''euclidean_gcd_recursive(1, 3) = {euclidean_gcd_recursive(1, 3 )}''' ) print(F'''euclidean_gcd_recursive(3, 6) = {euclidean_gcd_recursive(3, 6 )}''' ) print(F'''euclidean_gcd_recursive(6, 3) = {euclidean_gcd_recursive(6, 3 )}''' ) if __name__ == "__main__": main()
18
'''simple docstring''' import inspect import math import tempfile import unittest import numpy as np from transformers import ViTMAEConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTMAEForPreTraining, ViTMAEModel from transformers.models.vit.modeling_vit import VIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class __UpperCAmelCase : '''simple docstring''' def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=13 , _SCREAMING_SNAKE_CASE=30 , _SCREAMING_SNAKE_CASE=2 , _SCREAMING_SNAKE_CASE=3 , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=32 , _SCREAMING_SNAKE_CASE=5 , _SCREAMING_SNAKE_CASE=4 , _SCREAMING_SNAKE_CASE=37 , _SCREAMING_SNAKE_CASE="gelu" , _SCREAMING_SNAKE_CASE=0.1 , _SCREAMING_SNAKE_CASE=0.1 , _SCREAMING_SNAKE_CASE=10 , _SCREAMING_SNAKE_CASE=0.02 , _SCREAMING_SNAKE_CASE=3 , _SCREAMING_SNAKE_CASE=0.6 , _SCREAMING_SNAKE_CASE=None , ) -> Tuple: 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_ = num_attention_heads A_ = intermediate_size A_ = hidden_act A_ = hidden_dropout_prob A_ = attention_probs_dropout_prob A_ = type_sequence_label_size A_ = initializer_range A_ = mask_ratio A_ = scope # in ViTMAE, the expected sequence length = (num_patches + 1) * (1 - config.mask_ratio), rounded above # (we add 1 for the [CLS] token) A_ = (image_size // patch_size) ** 2 A_ = int(math.ceil((1 - mask_ratio) * (num_patches + 1) ) ) def __A ( self ) -> Union[str, Any]: 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.type_sequence_label_size ) A_ = self.get_config() return config, pixel_values, labels def __A ( self ) -> Dict: return ViTMAEConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=_SCREAMING_SNAKE_CASE , initializer_range=self.initializer_range , mask_ratio=self.mask_ratio , ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Tuple: A_ = ViTMAEModel(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 __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Optional[Any]: A_ = ViTMAEForPreTraining(_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() A_ = model(_SCREAMING_SNAKE_CASE ) A_ = (self.image_size // self.patch_size) ** 2 A_ = self.patch_size**2 * self.num_channels self.parent.assertEqual(result.logits.shape , (self.batch_size, num_patches, expected_num_channels) ) # test greyscale images A_ = 1 A_ = ViTMAEForPreTraining(_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() A_ = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) A_ = model(_SCREAMING_SNAKE_CASE ) A_ = self.patch_size**2 self.parent.assertEqual(result.logits.shape , (self.batch_size, num_patches, expected_num_channels) ) def __A ( self ) -> int: A_ = self.prepare_config_and_inputs() A_ ,A_ ,A_ = config_and_inputs A_ = {'''pixel_values''': pixel_values} return config, inputs_dict @require_torch class __UpperCAmelCase ( _UpperCamelCase , _UpperCamelCase , unittest.TestCase ): '''simple docstring''' __lowercase : int = (ViTMAEModel, ViTMAEForPreTraining) if is_torch_available() else () __lowercase : List[Any] = {'feature-extraction': ViTMAEModel} if is_torch_available() else {} __lowercase : Union[str, Any] = False __lowercase : List[Any] = False __lowercase : List[str] = False __lowercase : List[str] = False def __A ( self ) -> Any: A_ = ViTMAEModelTester(self ) A_ = ConfigTester(self , config_class=_SCREAMING_SNAKE_CASE , has_text_modality=_SCREAMING_SNAKE_CASE , hidden_size=37 ) def __A ( self ) -> Optional[int]: self.config_tester.run_common_tests() @unittest.skip(reason='''ViTMAE does not use inputs_embeds''' ) def __A ( self ) -> int: pass def __A ( self ) -> int: A_ ,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 __A ( self ) -> int: A_ ,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 __A ( self ) -> Union[str, Any]: A_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE ) def __A ( self ) -> Optional[int]: A_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_pretraining(*_SCREAMING_SNAKE_CASE ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> int: # make masks reproducible np.random.seed(2 ) A_ = int((pt_model.config.image_size // pt_model.config.patch_size) ** 2 ) A_ = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) A_ = torch.from_numpy(_SCREAMING_SNAKE_CASE ) # Add `noise` argument. # PT inputs will be prepared in `super().check_pt_tf_models()` with this added `noise` argument A_ = pt_noise super().check_pt_tf_models(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) def __A ( self ) -> str: A_ ,A_ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A_ = model_class(_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() # make random mask reproducible torch.manual_seed(2 ) with torch.no_grad(): A_ = model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) A_ = outputs[0].cpu().numpy() A_ = 0 with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(_SCREAMING_SNAKE_CASE ) A_ = model_class.from_pretrained(_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) # make random mask reproducible torch.manual_seed(2 ) with torch.no_grad(): A_ = model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) # Make sure we don't have nans A_ = after_outputs[0].cpu().numpy() A_ = 0 A_ = np.amax(np.abs(out_a - out_a ) ) self.assertLessEqual(_SCREAMING_SNAKE_CASE , 1E-5 ) @unittest.skip( reason='''ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load to get deterministic results.''' ) def __A ( self ) -> List[str]: pass @unittest.skip( reason='''ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load to get deterministic results.''' ) def __A ( self ) -> Dict: pass @unittest.skip( reason='''ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load to get deterministic results.''' ) def __A ( self ) -> Tuple: pass @unittest.skip(reason='''ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load''' ) def __A ( self ) -> str: pass @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.''' ) def __A ( self ) -> Union[str, Any]: pass @slow def __A ( self ) -> Dict: for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A_ = ViTMAEModel.from_pretrained(_SCREAMING_SNAKE_CASE ) self.assertIsNotNone(_SCREAMING_SNAKE_CASE ) def _UpperCAmelCase ( ) -> Dict: A_ = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_torch @require_vision class __UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' @cached_property def __A ( self ) -> List[str]: return ViTImageProcessor.from_pretrained('''facebook/vit-mae-base''' ) if is_vision_available() else None @slow def __A ( self ) -> List[str]: # make random mask reproducible across the PT and TF model np.random.seed(2 ) A_ = ViTMAEForPreTraining.from_pretrained('''facebook/vit-mae-base''' ).to(_SCREAMING_SNAKE_CASE ) A_ = self.default_image_processor A_ = prepare_img() A_ = image_processor(images=_SCREAMING_SNAKE_CASE , return_tensors='''pt''' ).to(_SCREAMING_SNAKE_CASE ) # prepare a noise vector that will be also used for testing the TF model # (this way we can ensure that the PT and TF models operate on the same inputs) A_ = ViTMAEConfig() A_ = int((vit_mae_config.image_size // vit_mae_config.patch_size) ** 2 ) A_ = np.random.uniform(size=(1, num_patches) ) # forward pass with torch.no_grad(): A_ = model(**_SCREAMING_SNAKE_CASE , noise=torch.from_numpy(_SCREAMING_SNAKE_CASE ).to(device=_SCREAMING_SNAKE_CASE ) ) # verify the logits A_ = torch.Size((1, 196, 768) ) self.assertEqual(outputs.logits.shape , _SCREAMING_SNAKE_CASE ) A_ = torch.tensor( [[-0.0_548, -1.7_023, -0.9_325], [0.3_721, -0.5_670, -0.2_233], [0.8_235, -1.3_878, -0.3_524]] ) self.assertTrue(torch.allclose(outputs.logits[0, :3, :3] , expected_slice.to(_SCREAMING_SNAKE_CASE ) , atol=1E-4 ) )
18
1
'''simple docstring''' import platform from argparse import ArgumentParser import huggingface_hub from .. import __version__ as version from ..utils import is_accelerate_available, is_torch_available, is_transformers_available, is_xformers_available from . import BaseDiffusersCLICommand def _UpperCAmelCase ( _UpperCamelCase : Union[str, Any] ) -> int: return EnvironmentCommand() class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' @staticmethod def __A ( _SCREAMING_SNAKE_CASE ) -> Any: A_ = parser.add_parser('''env''' ) download_parser.set_defaults(func=_SCREAMING_SNAKE_CASE ) def __A ( self ) -> Dict: A_ = huggingface_hub.__version__ A_ = '''not installed''' A_ = '''NA''' if is_torch_available(): import torch A_ = torch.__version__ A_ = torch.cuda.is_available() A_ = '''not installed''' if is_transformers_available(): import transformers A_ = transformers.__version__ A_ = '''not installed''' if is_accelerate_available(): import accelerate A_ = accelerate.__version__ A_ = '''not installed''' if is_xformers_available(): import xformers A_ = xformers.__version__ A_ = { '''`diffusers` version''': version, '''Platform''': platform.platform(), '''Python version''': platform.python_version(), '''PyTorch version (GPU?)''': F'''{pt_version} ({pt_cuda_available})''', '''Huggingface_hub version''': hub_version, '''Transformers version''': transformers_version, '''Accelerate version''': accelerate_version, '''xFormers version''': xformers_version, '''Using GPU in script?''': '''<fill in>''', '''Using distributed or parallel set-up in script?''': '''<fill in>''', } print('''\nCopy-and-paste the text below in your GitHub issue and FILL OUT the two last points.\n''' ) print(self.format_dict(_SCREAMING_SNAKE_CASE ) ) return info @staticmethod def __A ( _SCREAMING_SNAKE_CASE ) -> List[str]: return "\n".join([F'''- {prop}: {val}''' for prop, val in d.items()] ) + "\n"
18
'''simple docstring''' from typing import Callable, Optional, Union from ...configuration_utils import PretrainedConfig from ...utils import logging __snake_case : int = logging.get_logger(__name__) __snake_case : str = { 'microsoft/xprophetnet-large-wiki100-cased': ( 'https://huggingface.co/microsoft/xprophetnet-large-wiki100-cased/resolve/main/config.json' ), } class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' __lowercase : Optional[Any] = 'xlm-prophetnet' __lowercase : Optional[int] = ['past_key_values'] __lowercase : int = { 'num_attention_heads': 'num_encoder_attention_heads', } def __init__( self , _SCREAMING_SNAKE_CASE = 0.1 , _SCREAMING_SNAKE_CASE = "gelu" , _SCREAMING_SNAKE_CASE = 3_0522 , _SCREAMING_SNAKE_CASE = 1024 , _SCREAMING_SNAKE_CASE = 4096 , _SCREAMING_SNAKE_CASE = 12 , _SCREAMING_SNAKE_CASE = 16 , _SCREAMING_SNAKE_CASE = 4096 , _SCREAMING_SNAKE_CASE = 12 , _SCREAMING_SNAKE_CASE = 16 , _SCREAMING_SNAKE_CASE = 0.1 , _SCREAMING_SNAKE_CASE = 0.1 , _SCREAMING_SNAKE_CASE = 512 , _SCREAMING_SNAKE_CASE = 0.02 , _SCREAMING_SNAKE_CASE = True , _SCREAMING_SNAKE_CASE = True , _SCREAMING_SNAKE_CASE = 0 , _SCREAMING_SNAKE_CASE = 2 , _SCREAMING_SNAKE_CASE = 32 , _SCREAMING_SNAKE_CASE = 128 , _SCREAMING_SNAKE_CASE = False , _SCREAMING_SNAKE_CASE = 0.0 , _SCREAMING_SNAKE_CASE = True , _SCREAMING_SNAKE_CASE = 0 , _SCREAMING_SNAKE_CASE = 1 , _SCREAMING_SNAKE_CASE = 2 , **_SCREAMING_SNAKE_CASE , ) -> int: A_ = vocab_size A_ = hidden_size A_ = encoder_ffn_dim A_ = num_encoder_layers A_ = num_encoder_attention_heads A_ = decoder_ffn_dim A_ = num_decoder_layers A_ = num_decoder_attention_heads A_ = max_position_embeddings A_ = init_std # Normal(0, this parameter) A_ = activation_function # parameters for xlmprophetnet A_ = ngram A_ = num_buckets A_ = relative_max_distance A_ = disable_ngram_loss A_ = eps # 3 Types of Dropout A_ = attention_dropout A_ = activation_dropout A_ = dropout A_ = use_cache super().__init__( pad_token_id=_SCREAMING_SNAKE_CASE , bos_token_id=_SCREAMING_SNAKE_CASE , eos_token_id=_SCREAMING_SNAKE_CASE , is_encoder_decoder=_SCREAMING_SNAKE_CASE , add_cross_attention=_SCREAMING_SNAKE_CASE , decoder_start_token_id=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE , ) @property def __A ( self ) -> int: return self.num_encoder_layers + self.num_decoder_layers @num_hidden_layers.setter def __A ( self , _SCREAMING_SNAKE_CASE ) -> List[Any]: raise NotImplementedError( '''This model does not support the setting of `num_hidden_layers`. Please set `num_encoder_layers` and''' ''' `num_decoder_layers`.''' )
18
1
'''simple docstring''' import pickle import shutil import tempfile import unittest from transformers import SPIECE_UNDERLINE, XLMRobertaTokenizer, XLMRobertaTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin __snake_case : Optional[Any] = get_tests_dir('fixtures/test_sentencepiece.model') @require_sentencepiece @require_tokenizers class __UpperCAmelCase ( _UpperCamelCase , unittest.TestCase ): '''simple docstring''' __lowercase : str = XLMRobertaTokenizer __lowercase : Optional[int] = XLMRobertaTokenizerFast __lowercase : Optional[Any] = True __lowercase : Tuple = True def __A ( self ) -> str: super().setUp() # We have a SentencePiece fixture for testing A_ = XLMRobertaTokenizer(_SCREAMING_SNAKE_CASE , keep_accents=_SCREAMING_SNAKE_CASE ) tokenizer.save_pretrained(self.tmpdirname ) def __A ( self ) -> Any: A_ = '''<pad>''' A_ = 1 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 __A ( self ) -> Optional[int]: A_ = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '''<s>''' ) self.assertEqual(vocab_keys[1] , '''<pad>''' ) self.assertEqual(vocab_keys[-1] , '''<mask>''' ) self.assertEqual(len(_SCREAMING_SNAKE_CASE ) , 1002 ) def __A ( self ) -> Dict: self.assertEqual(self.get_tokenizer().vocab_size , 1002 ) def __A ( self ) -> int: A_ = XLMRobertaTokenizer(_SCREAMING_SNAKE_CASE , keep_accents=_SCREAMING_SNAKE_CASE ) A_ = 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]] , ) A_ = tokenizer.tokenize('''I was born in 92000, and this is falsé.''' ) self.assertListEqual( _SCREAMING_SNAKE_CASE , [ SPIECE_UNDERLINE + '''I''', SPIECE_UNDERLINE + '''was''', SPIECE_UNDERLINE + '''b''', '''or''', '''n''', SPIECE_UNDERLINE + '''in''', SPIECE_UNDERLINE + '''''', '''9''', '''2''', '''0''', '''0''', '''0''', ''',''', SPIECE_UNDERLINE + '''and''', SPIECE_UNDERLINE + '''this''', SPIECE_UNDERLINE + '''is''', SPIECE_UNDERLINE + '''f''', '''al''', '''s''', '''é''', '''.''', ] , ) A_ = tokenizer.convert_tokens_to_ids(_SCREAMING_SNAKE_CASE ) self.assertListEqual( _SCREAMING_SNAKE_CASE , [ value + tokenizer.fairseq_offset for value in [8, 21, 84, 55, 24, 19, 7, 2, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 2, 4] # ^ unk: 2 + 1 = 3 unk: 2 + 1 = 3 ^ ] , ) A_ = 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 __A ( self ) -> Tuple: if not self.test_slow_tokenizer: # as we don't have a slow version, we can't compare the outputs between slow and fast versions return A_ = (self.rust_tokenizer_class, '''hf-internal-testing/tiny-xlm-roberta''', {}) for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): A_ = self.rust_tokenizer_class.from_pretrained(_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) A_ = self.tokenizer_class.from_pretrained(_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) A_ = tempfile.mkdtemp() A_ = tokenizer_r.save_pretrained(_SCREAMING_SNAKE_CASE ) A_ = tokenizer_p.save_pretrained(_SCREAMING_SNAKE_CASE ) # Checks it save with the same files + the tokenizer.json file for the fast one self.assertTrue(any('''tokenizer.json''' in f for f in tokenizer_r_files ) ) A_ = tuple(f for f in tokenizer_r_files if '''tokenizer.json''' not in f ) self.assertSequenceEqual(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # Checks everything loads correctly in the same way A_ = tokenizer_r.from_pretrained(_SCREAMING_SNAKE_CASE ) A_ = tokenizer_p.from_pretrained(_SCREAMING_SNAKE_CASE ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) # self.assertEqual(getattr(tokenizer_rp, key), getattr(tokenizer_pp, key)) # self.assertEqual(getattr(tokenizer_rp, key + "_id"), getattr(tokenizer_pp, key + "_id")) shutil.rmtree(_SCREAMING_SNAKE_CASE ) # Save tokenizer rust, legacy_format=True A_ = tempfile.mkdtemp() A_ = tokenizer_r.save_pretrained(_SCREAMING_SNAKE_CASE , legacy_format=_SCREAMING_SNAKE_CASE ) A_ = tokenizer_p.save_pretrained(_SCREAMING_SNAKE_CASE ) # Checks it save with the same files self.assertSequenceEqual(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # Checks everything loads correctly in the same way A_ = tokenizer_r.from_pretrained(_SCREAMING_SNAKE_CASE ) A_ = tokenizer_p.from_pretrained(_SCREAMING_SNAKE_CASE ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) shutil.rmtree(_SCREAMING_SNAKE_CASE ) # Save tokenizer rust, legacy_format=False A_ = tempfile.mkdtemp() A_ = tokenizer_r.save_pretrained(_SCREAMING_SNAKE_CASE , legacy_format=_SCREAMING_SNAKE_CASE ) A_ = tokenizer_p.save_pretrained(_SCREAMING_SNAKE_CASE ) # Checks it saved the tokenizer.json file self.assertTrue(any('''tokenizer.json''' in f for f in tokenizer_r_files ) ) # Checks everything loads correctly in the same way A_ = tokenizer_r.from_pretrained(_SCREAMING_SNAKE_CASE ) A_ = 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 ) @cached_property def __A ( self ) -> Optional[int]: return XLMRobertaTokenizer.from_pretrained('''xlm-roberta-base''' ) def __A ( self ) -> Any: with tempfile.NamedTemporaryFile() as f: shutil.copyfile(_SCREAMING_SNAKE_CASE , f.name ) A_ = XLMRobertaTokenizer(f.name , keep_accents=_SCREAMING_SNAKE_CASE ) A_ = pickle.dumps(_SCREAMING_SNAKE_CASE ) pickle.loads(_SCREAMING_SNAKE_CASE ) def __A ( self ) -> List[Any]: if not self.test_rust_tokenizer: return A_ = self.get_tokenizer() A_ = self.get_rust_tokenizer() A_ = '''I was born in 92000, and this is falsé.''' A_ = tokenizer.tokenize(_SCREAMING_SNAKE_CASE ) A_ = rust_tokenizer.tokenize(_SCREAMING_SNAKE_CASE ) self.assertListEqual(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) A_ = tokenizer.encode(_SCREAMING_SNAKE_CASE , add_special_tokens=_SCREAMING_SNAKE_CASE ) A_ = rust_tokenizer.encode(_SCREAMING_SNAKE_CASE , add_special_tokens=_SCREAMING_SNAKE_CASE ) self.assertListEqual(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) A_ = self.get_rust_tokenizer() A_ = tokenizer.encode(_SCREAMING_SNAKE_CASE ) A_ = rust_tokenizer.encode(_SCREAMING_SNAKE_CASE ) self.assertListEqual(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) @slow def __A ( self ) -> Optional[int]: A_ = '''Hello World!''' A_ = [0, 3_5378, 6661, 38, 2] # xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.base') # xlmr.large has same tokenizer # xlmr.eval() # xlmr.encode(symbols) self.assertListEqual(_SCREAMING_SNAKE_CASE , self.big_tokenizer.encode(_SCREAMING_SNAKE_CASE ) ) @slow def __A ( self ) -> str: A_ = ( '''This is a very long text with a lot of weird characters, such as: . , ~ ? ( ) " [ ] ! : - . Also we will''' ''' add words that should not exsist and be tokenized to <unk>, such as saoneuhaoesuth''' ) A_ = [ 0, 3293, 83, 10, 4552, 4989, 7986, 678, 10, 5915, 111, 17_9459, 12_4850, 4, 6044, 237, 12, 6, 5, 6, 4, 6780, 705, 15, 1388, 44, 378, 1_0114, 711, 152, 20, 6, 5, 2_2376, 642, 1221, 1_5190, 3_4153, 450, 5608, 959, 1119, 5_7702, 136, 186, 47, 1098, 2_9367, 47, # 4426, # What fairseq tokenizes from "<unk>": "_<" # 3678, # What fairseq tokenizes from "<unk>": "unk" # 2740, # What fairseq tokenizes from "<unk>": ">" 3, # What we tokenize from "<unk>": "<unk>" 6, # Residue from the tokenization: an extra sentencepiece underline 4, 6044, 237, 6284, 5_0901, 528, 31, 90, 34, 927, 2, ] # xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.base') # xlmr.large has same tokenizer # xlmr.eval() # xlmr.encode(symbols) self.assertListEqual(_SCREAMING_SNAKE_CASE , self.big_tokenizer.encode(_SCREAMING_SNAKE_CASE ) ) @slow def __A ( self ) -> Optional[int]: # fmt: off A_ = {'''input_ids''': [[0, 1_1062, 8_2772, 7, 15, 8_2772, 538, 5_1529, 237, 1_7198, 1290, 206, 9, 21_5175, 1314, 136, 1_7198, 1290, 206, 9, 5_6359, 42, 12_2009, 9, 1_6466, 16, 8_7344, 4537, 9, 4717, 7_8381, 6, 15_9958, 7, 15, 2_4480, 618, 4, 527, 2_2693, 5428, 4, 2777, 2_4480, 9874, 4, 4_3523, 594, 4, 803, 1_8392, 3_3189, 18, 4, 4_3523, 2_4447, 1_2399, 100, 2_4955, 8_3658, 9626, 14_4057, 15, 839, 2_2335, 16, 136, 2_4955, 8_3658, 8_3479, 15, 3_9102, 724, 16, 678, 645, 2789, 1328, 4589, 42, 12_2009, 11_5774, 23, 805, 1328, 4_6876, 7, 136, 5_3894, 1940, 4_2227, 4_1159, 1_7721, 823, 425, 4, 2_7512, 9_8722, 206, 136, 5531, 4970, 919, 1_7336, 5, 2], [0, 2_0080, 618, 83, 8_2775, 47, 479, 9, 1517, 73, 5_3894, 333, 8_0581, 11_0117, 1_8811, 5256, 1295, 51, 15_2526, 297, 7986, 390, 12_4416, 538, 3_5431, 214, 98, 1_5044, 2_5737, 136, 7108, 4_3701, 23, 756, 13_5355, 7, 5, 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], [0, 581, 6_3773, 11_9455, 6, 14_7797, 8_8203, 7, 645, 70, 21, 3285, 1_0269, 5, 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]], '''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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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='''xlm-roberta-base''' , revision='''d9d8a8ea5eb94b1c6654ae9249df7793cd2933d3''' , )
18
'''simple docstring''' def _UpperCAmelCase ( _UpperCamelCase : float, _UpperCamelCase : list[float] ) -> float: if discount_rate < 0: raise ValueError('''Discount rate cannot be negative''' ) if not cash_flows: raise ValueError('''Cash flows list cannot be empty''' ) A_ = sum( cash_flow / ((1 + discount_rate) ** i) for i, cash_flow in enumerate(_UpperCamelCase ) ) return round(_UpperCamelCase, ndigits=2 ) if __name__ == "__main__": import doctest doctest.testmod()
18
1
'''simple docstring''' from typing import Callable, Optional, Union from ...configuration_utils import PretrainedConfig from ...utils import logging __snake_case : int = logging.get_logger(__name__) __snake_case : str = { 'microsoft/xprophetnet-large-wiki100-cased': ( 'https://huggingface.co/microsoft/xprophetnet-large-wiki100-cased/resolve/main/config.json' ), } class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' __lowercase : Optional[Any] = 'xlm-prophetnet' __lowercase : Optional[int] = ['past_key_values'] __lowercase : int = { 'num_attention_heads': 'num_encoder_attention_heads', } def __init__( self , _SCREAMING_SNAKE_CASE = 0.1 , _SCREAMING_SNAKE_CASE = "gelu" , _SCREAMING_SNAKE_CASE = 3_0522 , _SCREAMING_SNAKE_CASE = 1024 , _SCREAMING_SNAKE_CASE = 4096 , _SCREAMING_SNAKE_CASE = 12 , _SCREAMING_SNAKE_CASE = 16 , _SCREAMING_SNAKE_CASE = 4096 , _SCREAMING_SNAKE_CASE = 12 , _SCREAMING_SNAKE_CASE = 16 , _SCREAMING_SNAKE_CASE = 0.1 , _SCREAMING_SNAKE_CASE = 0.1 , _SCREAMING_SNAKE_CASE = 512 , _SCREAMING_SNAKE_CASE = 0.02 , _SCREAMING_SNAKE_CASE = True , _SCREAMING_SNAKE_CASE = True , _SCREAMING_SNAKE_CASE = 0 , _SCREAMING_SNAKE_CASE = 2 , _SCREAMING_SNAKE_CASE = 32 , _SCREAMING_SNAKE_CASE = 128 , _SCREAMING_SNAKE_CASE = False , _SCREAMING_SNAKE_CASE = 0.0 , _SCREAMING_SNAKE_CASE = True , _SCREAMING_SNAKE_CASE = 0 , _SCREAMING_SNAKE_CASE = 1 , _SCREAMING_SNAKE_CASE = 2 , **_SCREAMING_SNAKE_CASE , ) -> int: A_ = vocab_size A_ = hidden_size A_ = encoder_ffn_dim A_ = num_encoder_layers A_ = num_encoder_attention_heads A_ = decoder_ffn_dim A_ = num_decoder_layers A_ = num_decoder_attention_heads A_ = max_position_embeddings A_ = init_std # Normal(0, this parameter) A_ = activation_function # parameters for xlmprophetnet A_ = ngram A_ = num_buckets A_ = relative_max_distance A_ = disable_ngram_loss A_ = eps # 3 Types of Dropout A_ = attention_dropout A_ = activation_dropout A_ = dropout A_ = use_cache super().__init__( pad_token_id=_SCREAMING_SNAKE_CASE , bos_token_id=_SCREAMING_SNAKE_CASE , eos_token_id=_SCREAMING_SNAKE_CASE , is_encoder_decoder=_SCREAMING_SNAKE_CASE , add_cross_attention=_SCREAMING_SNAKE_CASE , decoder_start_token_id=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE , ) @property def __A ( self ) -> int: return self.num_encoder_layers + self.num_decoder_layers @num_hidden_layers.setter def __A ( self , _SCREAMING_SNAKE_CASE ) -> List[Any]: raise NotImplementedError( '''This model does not support the setting of `num_hidden_layers`. Please set `num_encoder_layers` and''' ''' `num_decoder_layers`.''' )
18
'''simple docstring''' from __future__ import annotations def _UpperCAmelCase ( _UpperCamelCase : int | str ) -> bool: A_ = str(_UpperCamelCase ) return n == n[::-1] def _UpperCAmelCase ( _UpperCamelCase : int = 1_00_00_00 ) -> Any: A_ = 0 for i in range(1, _UpperCamelCase ): if is_palindrome(_UpperCamelCase ) and is_palindrome(bin(_UpperCamelCase ).split('''b''' )[1] ): total += i return total if __name__ == "__main__": print(solution(int(str(input().strip()))))
18
1
'''simple docstring''' import json import os import sys import tempfile import unittest from pathlib import Path from shutil import copyfile from huggingface_hub import HfFolder, Repository, create_repo, delete_repo from requests.exceptions import HTTPError import transformers from transformers import ( CONFIG_MAPPING, FEATURE_EXTRACTOR_MAPPING, PROCESSOR_MAPPING, TOKENIZER_MAPPING, AutoConfig, AutoFeatureExtractor, AutoProcessor, AutoTokenizer, BertTokenizer, ProcessorMixin, WavaVecaConfig, WavaVecaFeatureExtractor, WavaVecaProcessor, ) from transformers.testing_utils import TOKEN, USER, get_tests_dir, is_staging_test from transformers.tokenization_utils import TOKENIZER_CONFIG_FILE from transformers.utils import FEATURE_EXTRACTOR_NAME, is_tokenizers_available sys.path.append(str(Path(__file__).parent.parent.parent.parent / 'utils')) from test_module.custom_configuration import CustomConfig # noqa E402 from test_module.custom_feature_extraction import CustomFeatureExtractor # noqa E402 from test_module.custom_processing import CustomProcessor # noqa E402 from test_module.custom_tokenization import CustomTokenizer # noqa E402 __snake_case : Tuple = get_tests_dir('fixtures/dummy_feature_extractor_config.json') __snake_case : List[str] = get_tests_dir('fixtures/vocab.json') __snake_case : Any = get_tests_dir('fixtures') class __UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' __lowercase : str = ['[UNK]', '[CLS]', '[SEP]', '[PAD]', '[MASK]', 'bla', 'blou'] def __A ( self ) -> Any: A_ = 0 def __A ( self ) -> List[Any]: A_ = AutoProcessor.from_pretrained('''facebook/wav2vec2-base-960h''' ) self.assertIsInstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) def __A ( self ) -> List[str]: with tempfile.TemporaryDirectory() as tmpdirname: A_ = WavaVecaConfig() A_ = AutoProcessor.from_pretrained('''facebook/wav2vec2-base-960h''' ) # save in new folder model_config.save_pretrained(_SCREAMING_SNAKE_CASE ) processor.save_pretrained(_SCREAMING_SNAKE_CASE ) A_ = AutoProcessor.from_pretrained(_SCREAMING_SNAKE_CASE ) self.assertIsInstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) def __A ( self ) -> Optional[int]: with tempfile.TemporaryDirectory() as tmpdirname: # copy relevant files copyfile(_SCREAMING_SNAKE_CASE , os.path.join(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) copyfile(_SCREAMING_SNAKE_CASE , os.path.join(_SCREAMING_SNAKE_CASE , '''vocab.json''' ) ) A_ = AutoProcessor.from_pretrained(_SCREAMING_SNAKE_CASE ) self.assertIsInstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) def __A ( self ) -> int: with tempfile.TemporaryDirectory() as tmpdirname: A_ = WavaVecaFeatureExtractor() A_ = AutoTokenizer.from_pretrained('''facebook/wav2vec2-base-960h''' ) A_ = WavaVecaProcessor(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # save in new folder processor.save_pretrained(_SCREAMING_SNAKE_CASE ) # drop `processor_class` in tokenizer with open(os.path.join(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) , '''r''' ) as f: A_ = json.load(_SCREAMING_SNAKE_CASE ) config_dict.pop('''processor_class''' ) with open(os.path.join(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) , '''w''' ) as f: f.write(json.dumps(_SCREAMING_SNAKE_CASE ) ) A_ = AutoProcessor.from_pretrained(_SCREAMING_SNAKE_CASE ) self.assertIsInstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) def __A ( self ) -> Optional[Any]: with tempfile.TemporaryDirectory() as tmpdirname: A_ = WavaVecaFeatureExtractor() A_ = AutoTokenizer.from_pretrained('''facebook/wav2vec2-base-960h''' ) A_ = WavaVecaProcessor(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # save in new folder processor.save_pretrained(_SCREAMING_SNAKE_CASE ) # drop `processor_class` in feature extractor with open(os.path.join(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) , '''r''' ) as f: A_ = json.load(_SCREAMING_SNAKE_CASE ) config_dict.pop('''processor_class''' ) with open(os.path.join(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) , '''w''' ) as f: f.write(json.dumps(_SCREAMING_SNAKE_CASE ) ) A_ = AutoProcessor.from_pretrained(_SCREAMING_SNAKE_CASE ) self.assertIsInstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) def __A ( self ) -> Tuple: with tempfile.TemporaryDirectory() as tmpdirname: A_ = WavaVecaConfig(processor_class='''Wav2Vec2Processor''' ) model_config.save_pretrained(_SCREAMING_SNAKE_CASE ) # copy relevant files copyfile(_SCREAMING_SNAKE_CASE , os.path.join(_SCREAMING_SNAKE_CASE , '''vocab.json''' ) ) # create emtpy sample processor with open(os.path.join(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) , '''w''' ) as f: f.write('''{}''' ) A_ = AutoProcessor.from_pretrained(_SCREAMING_SNAKE_CASE ) self.assertIsInstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) def __A ( self ) -> str: # If remote code is not set, we will time out when asking whether to load the model. with self.assertRaises(_SCREAMING_SNAKE_CASE ): A_ = AutoProcessor.from_pretrained('''hf-internal-testing/test_dynamic_processor''' ) # If remote code is disabled, we can't load this config. with self.assertRaises(_SCREAMING_SNAKE_CASE ): A_ = AutoProcessor.from_pretrained( '''hf-internal-testing/test_dynamic_processor''' , trust_remote_code=_SCREAMING_SNAKE_CASE ) A_ = AutoProcessor.from_pretrained('''hf-internal-testing/test_dynamic_processor''' , trust_remote_code=_SCREAMING_SNAKE_CASE ) self.assertTrue(processor.special_attribute_present ) self.assertEqual(processor.__class__.__name__ , '''NewProcessor''' ) A_ = processor.feature_extractor self.assertTrue(feature_extractor.special_attribute_present ) self.assertEqual(feature_extractor.__class__.__name__ , '''NewFeatureExtractor''' ) A_ = processor.tokenizer self.assertTrue(tokenizer.special_attribute_present ) if is_tokenizers_available(): self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizerFast''' ) # Test we can also load the slow version A_ = AutoProcessor.from_pretrained( '''hf-internal-testing/test_dynamic_processor''' , trust_remote_code=_SCREAMING_SNAKE_CASE , use_fast=_SCREAMING_SNAKE_CASE ) A_ = new_processor.tokenizer self.assertTrue(new_tokenizer.special_attribute_present ) self.assertEqual(new_tokenizer.__class__.__name__ , '''NewTokenizer''' ) else: self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizer''' ) def __A ( self ) -> Optional[int]: try: AutoConfig.register('''custom''' , _SCREAMING_SNAKE_CASE ) AutoFeatureExtractor.register(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) AutoTokenizer.register(_SCREAMING_SNAKE_CASE , slow_tokenizer_class=_SCREAMING_SNAKE_CASE ) AutoProcessor.register(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(_SCREAMING_SNAKE_CASE ): AutoProcessor.register(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # Now that the config is registered, it can be used as any other config with the auto-API A_ = CustomFeatureExtractor.from_pretrained(_SCREAMING_SNAKE_CASE ) with tempfile.TemporaryDirectory() as tmp_dir: A_ = os.path.join(_SCREAMING_SNAKE_CASE , '''vocab.txt''' ) with open(_SCREAMING_SNAKE_CASE , '''w''' , encoding='''utf-8''' ) as vocab_writer: vocab_writer.write(''''''.join([x + '''\n''' for x in self.vocab_tokens] ) ) A_ = CustomTokenizer(_SCREAMING_SNAKE_CASE ) A_ = CustomProcessor(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) with tempfile.TemporaryDirectory() as tmp_dir: processor.save_pretrained(_SCREAMING_SNAKE_CASE ) A_ = AutoProcessor.from_pretrained(_SCREAMING_SNAKE_CASE ) self.assertIsInstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content: del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig] if CustomConfig in TOKENIZER_MAPPING._extra_content: del TOKENIZER_MAPPING._extra_content[CustomConfig] if CustomConfig in PROCESSOR_MAPPING._extra_content: del PROCESSOR_MAPPING._extra_content[CustomConfig] def __A ( self ) -> Tuple: class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' __lowercase : Optional[int] = False class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' __lowercase : Any = False class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' __lowercase : Any = 'AutoFeatureExtractor' __lowercase : List[str] = 'AutoTokenizer' __lowercase : Dict = False try: AutoConfig.register('''custom''' , _SCREAMING_SNAKE_CASE ) AutoFeatureExtractor.register(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) AutoTokenizer.register(_SCREAMING_SNAKE_CASE , slow_tokenizer_class=_SCREAMING_SNAKE_CASE ) AutoProcessor.register(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # If remote code is not set, the default is to use local classes. A_ = AutoProcessor.from_pretrained('''hf-internal-testing/test_dynamic_processor''' ) self.assertEqual(processor.__class__.__name__ , '''NewProcessor''' ) self.assertFalse(processor.special_attribute_present ) self.assertFalse(processor.feature_extractor.special_attribute_present ) self.assertFalse(processor.tokenizer.special_attribute_present ) # If remote code is disabled, we load the local ones. A_ = AutoProcessor.from_pretrained( '''hf-internal-testing/test_dynamic_processor''' , trust_remote_code=_SCREAMING_SNAKE_CASE ) self.assertEqual(processor.__class__.__name__ , '''NewProcessor''' ) self.assertFalse(processor.special_attribute_present ) self.assertFalse(processor.feature_extractor.special_attribute_present ) self.assertFalse(processor.tokenizer.special_attribute_present ) # If remote is enabled, we load from the Hub. A_ = AutoProcessor.from_pretrained( '''hf-internal-testing/test_dynamic_processor''' , trust_remote_code=_SCREAMING_SNAKE_CASE ) self.assertEqual(processor.__class__.__name__ , '''NewProcessor''' ) self.assertTrue(processor.special_attribute_present ) self.assertTrue(processor.feature_extractor.special_attribute_present ) self.assertTrue(processor.tokenizer.special_attribute_present ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content: del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig] if CustomConfig in TOKENIZER_MAPPING._extra_content: del TOKENIZER_MAPPING._extra_content[CustomConfig] if CustomConfig in PROCESSOR_MAPPING._extra_content: del PROCESSOR_MAPPING._extra_content[CustomConfig] def __A ( self ) -> List[Any]: A_ = AutoProcessor.from_pretrained('''hf-internal-testing/tiny-random-bert''' ) self.assertEqual(processor.__class__.__name__ , '''BertTokenizerFast''' ) def __A ( self ) -> Any: A_ = AutoProcessor.from_pretrained('''hf-internal-testing/tiny-random-convnext''' ) self.assertEqual(processor.__class__.__name__ , '''ConvNextImageProcessor''' ) @is_staging_test class __UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' __lowercase : str = ['[UNK]', '[CLS]', '[SEP]', '[PAD]', '[MASK]', 'bla', 'blou'] @classmethod def __A ( cls ) -> str: A_ = TOKEN HfFolder.save_token(_SCREAMING_SNAKE_CASE ) @classmethod def __A ( cls ) -> Optional[Any]: try: delete_repo(token=cls._token , repo_id='''test-processor''' ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id='''valid_org/test-processor-org''' ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id='''test-dynamic-processor''' ) except HTTPError: pass def __A ( self ) -> Optional[Any]: A_ = WavaVecaProcessor.from_pretrained(_SCREAMING_SNAKE_CASE ) with tempfile.TemporaryDirectory() as tmp_dir: processor.save_pretrained( os.path.join(_SCREAMING_SNAKE_CASE , '''test-processor''' ) , push_to_hub=_SCREAMING_SNAKE_CASE , use_auth_token=self._token ) A_ = WavaVecaProcessor.from_pretrained(F'''{USER}/test-processor''' ) for k, v in processor.feature_extractor.__dict__.items(): self.assertEqual(_SCREAMING_SNAKE_CASE , getattr(new_processor.feature_extractor , _SCREAMING_SNAKE_CASE ) ) self.assertDictEqual(new_processor.tokenizer.get_vocab() , processor.tokenizer.get_vocab() ) def __A ( self ) -> Optional[int]: A_ = WavaVecaProcessor.from_pretrained(_SCREAMING_SNAKE_CASE ) with tempfile.TemporaryDirectory() as tmp_dir: processor.save_pretrained( os.path.join(_SCREAMING_SNAKE_CASE , '''test-processor-org''' ) , push_to_hub=_SCREAMING_SNAKE_CASE , use_auth_token=self._token , organization='''valid_org''' , ) A_ = WavaVecaProcessor.from_pretrained('''valid_org/test-processor-org''' ) for k, v in processor.feature_extractor.__dict__.items(): self.assertEqual(_SCREAMING_SNAKE_CASE , getattr(new_processor.feature_extractor , _SCREAMING_SNAKE_CASE ) ) self.assertDictEqual(new_processor.tokenizer.get_vocab() , processor.tokenizer.get_vocab() ) def __A ( self ) -> List[Any]: CustomFeatureExtractor.register_for_auto_class() CustomTokenizer.register_for_auto_class() CustomProcessor.register_for_auto_class() A_ = CustomFeatureExtractor.from_pretrained(_SCREAMING_SNAKE_CASE ) with tempfile.TemporaryDirectory() as tmp_dir: A_ = os.path.join(_SCREAMING_SNAKE_CASE , '''vocab.txt''' ) with open(_SCREAMING_SNAKE_CASE , '''w''' , encoding='''utf-8''' ) as vocab_writer: vocab_writer.write(''''''.join([x + '''\n''' for x in self.vocab_tokens] ) ) A_ = CustomTokenizer(_SCREAMING_SNAKE_CASE ) A_ = CustomProcessor(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) with tempfile.TemporaryDirectory() as tmp_dir: create_repo(F'''{USER}/test-dynamic-processor''' , token=self._token ) A_ = Repository(_SCREAMING_SNAKE_CASE , clone_from=F'''{USER}/test-dynamic-processor''' , token=self._token ) processor.save_pretrained(_SCREAMING_SNAKE_CASE ) # This has added the proper auto_map field to the feature extractor config self.assertDictEqual( processor.feature_extractor.auto_map , { '''AutoFeatureExtractor''': '''custom_feature_extraction.CustomFeatureExtractor''', '''AutoProcessor''': '''custom_processing.CustomProcessor''', } , ) # This has added the proper auto_map field to the tokenizer config with open(os.path.join(_SCREAMING_SNAKE_CASE , '''tokenizer_config.json''' ) ) as f: A_ = json.load(_SCREAMING_SNAKE_CASE ) self.assertDictEqual( tokenizer_config['''auto_map'''] , { '''AutoTokenizer''': ['''custom_tokenization.CustomTokenizer''', None], '''AutoProcessor''': '''custom_processing.CustomProcessor''', } , ) # The code has been copied from fixtures self.assertTrue(os.path.isfile(os.path.join(_SCREAMING_SNAKE_CASE , '''custom_feature_extraction.py''' ) ) ) self.assertTrue(os.path.isfile(os.path.join(_SCREAMING_SNAKE_CASE , '''custom_tokenization.py''' ) ) ) self.assertTrue(os.path.isfile(os.path.join(_SCREAMING_SNAKE_CASE , '''custom_processing.py''' ) ) ) repo.push_to_hub() A_ = AutoProcessor.from_pretrained(F'''{USER}/test-dynamic-processor''' , trust_remote_code=_SCREAMING_SNAKE_CASE ) # Can't make an isinstance check because the new_processor is from the CustomProcessor class of a dynamic module self.assertEqual(new_processor.__class__.__name__ , '''CustomProcessor''' )
18
'''simple docstring''' # Usage: # ./gen-card-facebook-wmt19.py import os from pathlib import Path def _UpperCAmelCase ( _UpperCamelCase : Tuple, _UpperCamelCase : Tuple, _UpperCamelCase : List[str] ) -> int: A_ = { '''en''': '''Machine learning is great, isn\'t it?''', '''ru''': '''Машинное обучение - это здорово, не так ли?''', '''de''': '''Maschinelles Lernen ist großartig, oder?''', } # BLUE scores as follows: # "pair": [fairseq, transformers] A_ = { '''ru-en''': ['''[41.3](http://matrix.statmt.org/matrix/output/1907?run_id=6937)''', '''39.20'''], '''en-ru''': ['''[36.4](http://matrix.statmt.org/matrix/output/1914?run_id=6724)''', '''33.47'''], '''en-de''': ['''[43.1](http://matrix.statmt.org/matrix/output/1909?run_id=6862)''', '''42.83'''], '''de-en''': ['''[42.3](http://matrix.statmt.org/matrix/output/1902?run_id=6750)''', '''41.35'''], } A_ = F'''{src_lang}-{tgt_lang}''' A_ = F''' --- language: - {src_lang} - {tgt_lang} thumbnail: tags: - translation - wmt19 - facebook license: apache-2.0 datasets: - wmt19 metrics: - bleu --- # FSMT ## Model description This is a ported version of [fairseq wmt19 transformer](https://github.com/pytorch/fairseq/blob/master/examples/wmt19/README.md) for {src_lang}-{tgt_lang}. For more details, please see, [Facebook FAIR\'s WMT19 News Translation Task Submission](https://arxiv.org/abs/1907.06616). The abbreviation FSMT stands for FairSeqMachineTranslation All four models are available: * [wmt19-en-ru](https://huggingface.co/facebook/wmt19-en-ru) * [wmt19-ru-en](https://huggingface.co/facebook/wmt19-ru-en) * [wmt19-en-de](https://huggingface.co/facebook/wmt19-en-de) * [wmt19-de-en](https://huggingface.co/facebook/wmt19-de-en) ## Intended uses & limitations #### How to use ```python from transformers import FSMTForConditionalGeneration, FSMTTokenizer mname = "facebook/wmt19-{src_lang}-{tgt_lang}" tokenizer = FSMTTokenizer.from_pretrained(mname) model = FSMTForConditionalGeneration.from_pretrained(mname) input = "{texts[src_lang]}" input_ids = tokenizer.encode(input, return_tensors="pt") outputs = model.generate(input_ids) decoded = tokenizer.decode(outputs[0], skip_special_tokens=True) print(decoded) # {texts[tgt_lang]} ``` #### Limitations and bias - The original (and this ported model) doesn\'t seem to handle well inputs with repeated sub-phrases, [content gets truncated](https://discuss.huggingface.co/t/issues-with-translating-inputs-containing-repeated-phrases/981) ## Training data Pretrained weights were left identical to the original model released by fairseq. For more details, please, see the [paper](https://arxiv.org/abs/1907.06616). ## Eval results pair | fairseq | transformers -------|---------|---------- {pair} | {scores[pair][0]} | {scores[pair][1]} The score is slightly below the score reported by `fairseq`, since `transformers`` currently doesn\'t support: - model ensemble, therefore the best performing checkpoint was ported (``model4.pt``). - re-ranking The score was calculated using this code: ```bash git clone https://github.com/huggingface/transformers cd transformers export PAIR={pair} export DATA_DIR=data/$PAIR export SAVE_DIR=data/$PAIR export BS=8 export NUM_BEAMS=15 mkdir -p $DATA_DIR sacrebleu -t wmt19 -l $PAIR --echo src > $DATA_DIR/val.source sacrebleu -t wmt19 -l $PAIR --echo ref > $DATA_DIR/val.target echo $PAIR PYTHONPATH="src:examples/seq2seq" python examples/seq2seq/run_eval.py facebook/wmt19-$PAIR $DATA_DIR/val.source $SAVE_DIR/test_translations.txt --reference_path $DATA_DIR/val.target --score_path $SAVE_DIR/test_bleu.json --bs $BS --task translation --num_beams $NUM_BEAMS ``` note: fairseq reports using a beam of 50, so you should get a slightly higher score if re-run with `--num_beams 50`. ## Data Sources - [training, etc.](http://www.statmt.org/wmt19/) - [test set](http://matrix.statmt.org/test_sets/newstest2019.tgz?1556572561) ### BibTeX entry and citation info ```bibtex @inproceedings{{..., year={{2020}}, title={{Facebook FAIR\'s WMT19 News Translation Task Submission}}, author={{Ng, Nathan and Yee, Kyra and Baevski, Alexei and Ott, Myle and Auli, Michael and Edunov, Sergey}}, booktitle={{Proc. of WMT}}, }} ``` ## TODO - port model ensemble (fairseq uses 4 model checkpoints) ''' os.makedirs(_UpperCamelCase, exist_ok=_UpperCamelCase ) A_ = os.path.join(_UpperCamelCase, '''README.md''' ) print(F'''Generating {path}''' ) with open(_UpperCamelCase, '''w''', encoding='''utf-8''' ) as f: f.write(_UpperCamelCase ) # make sure we are under the root of the project __snake_case : Any = Path(__file__).resolve().parent.parent.parent __snake_case : Tuple = repo_dir / 'model_cards' for model_name in ["wmt19-ru-en", "wmt19-en-ru", "wmt19-en-de", "wmt19-de-en"]: __snake_case , __snake_case , __snake_case : Any = model_name.split('-') __snake_case : int = model_cards_dir / 'facebook' / model_name write_model_card(model_card_dir, src_lang=src_lang, tgt_lang=tgt_lang)
18
1
'''simple docstring''' __snake_case : List[Any] = 256 # Modulus to hash a string __snake_case : Tuple = 1_000_003 def _UpperCAmelCase ( _UpperCamelCase : str, _UpperCamelCase : str ) -> bool: A_ = len(_UpperCamelCase ) A_ = len(_UpperCamelCase ) if p_len > t_len: return False A_ = 0 A_ = 0 A_ = 1 # Calculating the hash of pattern and substring of text for i in range(_UpperCamelCase ): A_ = (ord(pattern[i] ) + p_hash * alphabet_size) % modulus A_ = (ord(text[i] ) + text_hash * alphabet_size) % modulus if i == p_len - 1: continue A_ = (modulus_power * alphabet_size) % modulus for i in range(0, t_len - p_len + 1 ): if text_hash == p_hash and text[i : i + p_len] == pattern: return True if i == t_len - p_len: continue # Calculate the https://en.wikipedia.org/wiki/Rolling_hash A_ = ( (text_hash - ord(text[i] ) * modulus_power) * alphabet_size + ord(text[i + p_len] ) ) % modulus return False def _UpperCAmelCase ( ) -> None: A_ = '''abc1abc12''' A_ = '''alskfjaldsabc1abc1abc12k23adsfabcabc''' A_ = '''alskfjaldsk23adsfabcabc''' assert rabin_karp(_UpperCamelCase, _UpperCamelCase ) and not rabin_karp(_UpperCamelCase, _UpperCamelCase ) # Test 2) A_ = '''ABABX''' A_ = '''ABABZABABYABABX''' assert rabin_karp(_UpperCamelCase, _UpperCamelCase ) # Test 3) A_ = '''AAAB''' A_ = '''ABAAAAAB''' assert rabin_karp(_UpperCamelCase, _UpperCamelCase ) # Test 4) A_ = '''abcdabcy''' A_ = '''abcxabcdabxabcdabcdabcy''' assert rabin_karp(_UpperCamelCase, _UpperCamelCase ) # Test 5) A_ = '''Lü''' A_ = '''Lüsai''' assert rabin_karp(_UpperCamelCase, _UpperCamelCase ) A_ = '''Lue''' assert not rabin_karp(_UpperCamelCase, _UpperCamelCase ) print('''Success.''' ) if __name__ == "__main__": test_rabin_karp()
18
'''simple docstring''' from collections import defaultdict def _UpperCAmelCase ( _UpperCamelCase : int ) -> int: A_ = 1 A_ = True for v in tree[start]: if v not in visited: ret += dfs(_UpperCamelCase ) if ret % 2 == 0: cuts.append(_UpperCamelCase ) return ret def _UpperCAmelCase ( ) -> Optional[Any]: dfs(1 ) if __name__ == "__main__": __snake_case , __snake_case : Union[str, Any] = 10, 9 __snake_case : int = defaultdict(list) __snake_case : dict[int, bool] = {} __snake_case : list[int] = [] __snake_case : Union[str, Any] = 0 __snake_case : int = [(2, 1), (3, 1), (4, 3), (5, 2), (6, 1), (7, 2), (8, 6), (9, 8), (10, 8)] for u, v in edges: tree[u].append(v) tree[v].append(u) even_tree() print(len(cuts) - 1)
18
1
'''simple docstring''' import os from tempfile import TemporaryDirectory from unittest import TestCase import pytest from absl.testing import parameterized from datasets import config from datasets.arrow_reader import HF_GCP_BASE_URL from datasets.builder import DatasetBuilder from datasets.dataset_dict import IterableDatasetDict from datasets.iterable_dataset import IterableDataset from datasets.load import dataset_module_factory, import_main_class from datasets.utils.file_utils import cached_path __snake_case : str = [ {'dataset': 'wikipedia', 'config_name': '20220301.de'}, {'dataset': 'wikipedia', 'config_name': '20220301.en'}, {'dataset': 'wikipedia', 'config_name': '20220301.fr'}, {'dataset': 'wikipedia', 'config_name': '20220301.frr'}, {'dataset': 'wikipedia', 'config_name': '20220301.it'}, {'dataset': 'wikipedia', 'config_name': '20220301.simple'}, {'dataset': 'snli', 'config_name': 'plain_text'}, {'dataset': 'eli5', 'config_name': 'LFQA_reddit'}, {'dataset': 'wiki40b', 'config_name': 'en'}, {'dataset': 'wiki_dpr', 'config_name': 'psgs_w100.nq.compressed'}, {'dataset': 'wiki_dpr', 'config_name': 'psgs_w100.nq.no_index'}, {'dataset': 'wiki_dpr', 'config_name': 'psgs_w100.multiset.no_index'}, {'dataset': 'natural_questions', 'config_name': 'default'}, ] def _UpperCAmelCase ( _UpperCamelCase : Tuple=True ) -> List[Any]: if with_config: return [ { "testcase_name": d["dataset"] + "/" + d["config_name"], "dataset": d["dataset"], "config_name": d["config_name"], } for d in DATASETS_ON_HF_GCP ] else: return [ {"testcase_name": dataset, "dataset": dataset} for dataset in {d["dataset"] for d in DATASETS_ON_HF_GCP} ] @parameterized.named_parameters(list_datasets_on_hf_gcp_parameters(with_config=_UpperCamelCase ) ) class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' __lowercase : List[Any] = None __lowercase : List[str] = None def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Optional[int]: with TemporaryDirectory() as tmp_dir: A_ = dataset_module_factory(_SCREAMING_SNAKE_CASE , cache_dir=_SCREAMING_SNAKE_CASE ) A_ = import_main_class(dataset_module.module_path , dataset=_SCREAMING_SNAKE_CASE ) A_ = builder_cls( cache_dir=_SCREAMING_SNAKE_CASE , config_name=_SCREAMING_SNAKE_CASE , hash=dataset_module.hash , ) A_ = '''/'''.join( [ HF_GCP_BASE_URL, builder_instance._relative_data_dir(with_hash=_SCREAMING_SNAKE_CASE ).replace(os.sep , '''/''' ), config.DATASET_INFO_FILENAME, ] ) A_ = cached_path(_SCREAMING_SNAKE_CASE , cache_dir=_SCREAMING_SNAKE_CASE ) self.assertTrue(os.path.exists(_SCREAMING_SNAKE_CASE ) ) @pytest.mark.integration def _UpperCAmelCase ( _UpperCamelCase : int ) -> Optional[int]: A_ = tmp_path_factory.mktemp('''test_hf_gcp''' ) / '''test_wikipedia_simple''' A_ = dataset_module_factory('''wikipedia''', cache_dir=_UpperCamelCase ) A_ = import_main_class(dataset_module.module_path ) A_ = builder_cls( cache_dir=_UpperCamelCase, config_name='''20220301.frr''', hash=dataset_module.hash, ) # use the HF cloud storage, not the original download_and_prepare that uses apache-beam A_ = None builder_instance.download_and_prepare() A_ = builder_instance.as_dataset() assert ds @pytest.mark.integration def _UpperCAmelCase ( _UpperCamelCase : Optional[int] ) -> Dict: A_ = dataset_module_factory('''wikipedia''', cache_dir=_UpperCamelCase ) A_ = import_main_class(dataset_module.module_path, dataset=_UpperCamelCase ) A_ = builder_cls( cache_dir=_UpperCamelCase, config_name='''20220301.frr''', hash=dataset_module.hash, ) A_ = builder_instance.as_streaming_dataset() assert ds assert isinstance(_UpperCamelCase, _UpperCamelCase ) assert "train" in ds assert isinstance(ds['''train'''], _UpperCamelCase ) assert next(iter(ds['''train'''] ) )
18
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging __snake_case : List[str] = logging.get_logger(__name__) __snake_case : Union[str, Any] = { 'alibaba-damo/mgp-str-base': 'https://huggingface.co/alibaba-damo/mgp-str-base/resolve/main/config.json', } class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' __lowercase : Optional[int] = 'mgp-str' def __init__( self , _SCREAMING_SNAKE_CASE=[32, 128] , _SCREAMING_SNAKE_CASE=4 , _SCREAMING_SNAKE_CASE=3 , _SCREAMING_SNAKE_CASE=27 , _SCREAMING_SNAKE_CASE=38 , _SCREAMING_SNAKE_CASE=5_0257 , _SCREAMING_SNAKE_CASE=3_0522 , _SCREAMING_SNAKE_CASE=768 , _SCREAMING_SNAKE_CASE=12 , _SCREAMING_SNAKE_CASE=12 , _SCREAMING_SNAKE_CASE=4.0 , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=False , _SCREAMING_SNAKE_CASE=1E-5 , _SCREAMING_SNAKE_CASE=0.0 , _SCREAMING_SNAKE_CASE=0.0 , _SCREAMING_SNAKE_CASE=0.0 , _SCREAMING_SNAKE_CASE=False , _SCREAMING_SNAKE_CASE=0.02 , **_SCREAMING_SNAKE_CASE , ) -> List[Any]: super().__init__(**_SCREAMING_SNAKE_CASE ) A_ = image_size A_ = patch_size A_ = num_channels A_ = max_token_length A_ = num_character_labels A_ = num_bpe_labels A_ = num_wordpiece_labels A_ = hidden_size A_ = num_hidden_layers A_ = num_attention_heads A_ = mlp_ratio A_ = distilled A_ = layer_norm_eps A_ = drop_rate A_ = qkv_bias A_ = attn_drop_rate A_ = drop_path_rate A_ = output_aa_attentions A_ = initializer_range
18
1
'''simple docstring''' def _UpperCAmelCase ( _UpperCamelCase : list ) -> list: if len(_UpperCamelCase ) <= 1: return [tuple(_UpperCamelCase )] A_ = [] def generate(_UpperCamelCase : int, _UpperCamelCase : list ): if k == 1: res.append(tuple(arr[:] ) ) return generate(k - 1, _UpperCamelCase ) for i in range(k - 1 ): if k % 2 == 0: # k is even A_ ,A_ = arr[k - 1], arr[i] else: # k is odd A_ ,A_ = arr[k - 1], arr[0] generate(k - 1, _UpperCamelCase ) generate(len(_UpperCamelCase ), _UpperCamelCase ) return res if __name__ == "__main__": __snake_case : List[Any] = input('Enter numbers separated by a comma:\n').strip() __snake_case : List[str] = [int(item) for item in user_input.split(',')] print(heaps(arr))
18
'''simple docstring''' from diffusers.utils.testing_utils import require_onnxruntime @require_onnxruntime class __UpperCAmelCase : '''simple docstring''' pass
18
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available __snake_case : Dict = { 'configuration_biogpt': ['BIOGPT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'BioGptConfig'], 'tokenization_biogpt': ['BioGptTokenizer'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case : Dict = [ 'BIOGPT_PRETRAINED_MODEL_ARCHIVE_LIST', 'BioGptForCausalLM', 'BioGptForTokenClassification', 'BioGptForSequenceClassification', 'BioGptModel', 'BioGptPreTrainedModel', ] if TYPE_CHECKING: from .configuration_biogpt import BIOGPT_PRETRAINED_CONFIG_ARCHIVE_MAP, BioGptConfig from .tokenization_biogpt import BioGptTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_biogpt import ( BIOGPT_PRETRAINED_MODEL_ARCHIVE_LIST, BioGptForCausalLM, BioGptForSequenceClassification, BioGptForTokenClassification, BioGptModel, BioGptPreTrainedModel, ) else: import sys __snake_case : List[Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
18
'''simple docstring''' # Copyright 2021 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from argparse import ArgumentParser from accelerate.commands.config import get_config_parser from accelerate.commands.env import env_command_parser from accelerate.commands.launch import launch_command_parser from accelerate.commands.test import test_command_parser from accelerate.commands.tpu import tpu_command_parser def _UpperCAmelCase ( ) -> Dict: A_ = ArgumentParser('''Accelerate CLI tool''', usage='''accelerate <command> [<args>]''', allow_abbrev=_UpperCamelCase ) A_ = parser.add_subparsers(help='''accelerate command helpers''' ) # Register commands get_config_parser(subparsers=_UpperCamelCase ) env_command_parser(subparsers=_UpperCamelCase ) launch_command_parser(subparsers=_UpperCamelCase ) tpu_command_parser(subparsers=_UpperCamelCase ) test_command_parser(subparsers=_UpperCamelCase ) # Let's go A_ = parser.parse_args() if not hasattr(_UpperCamelCase, '''func''' ): parser.print_help() exit(1 ) # Run args.func(_UpperCamelCase ) if __name__ == "__main__": main()
18
1
'''simple docstring''' import mpmath # for roots of unity import numpy as np class __UpperCAmelCase : '''simple docstring''' def __init__( self , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE=None ) -> Optional[Any]: # Input as list A_ = list(poly_a or [0] )[:] A_ = list(poly_b or [0] )[:] # Remove leading zero coefficients while self.polyA[-1] == 0: self.polyA.pop() A_ = len(self.polyA ) while self.polyB[-1] == 0: self.polyB.pop() A_ = len(self.polyB ) # Add 0 to make lengths equal a power of 2 A_ = int( 2 ** np.ceil(np.loga(len(self.polyA ) + len(self.polyB ) - 1 ) ) ) while len(self.polyA ) < self.c_max_length: self.polyA.append(0 ) while len(self.polyB ) < self.c_max_length: self.polyB.append(0 ) # A complex root used for the fourier transform A_ = complex(mpmath.root(x=1 , n=self.c_max_length , k=1 ) ) # The product A_ = self.__multiply() def __A ( self , _SCREAMING_SNAKE_CASE ) -> List[str]: A_ = [[x] for x in self.polyA] if which == '''A''' else [[x] for x in self.polyB] # Corner case if len(_SCREAMING_SNAKE_CASE ) <= 1: return dft[0] # A_ = self.c_max_length // 2 while next_ncol > 0: A_ = [[] for i in range(_SCREAMING_SNAKE_CASE )] A_ = self.root**next_ncol # First half of next step A_ = 1 for j in range(self.c_max_length // (next_ncol * 2) ): for i in range(_SCREAMING_SNAKE_CASE ): new_dft[i].append(dft[i][j] + current_root * dft[i + next_ncol][j] ) current_root *= root # Second half of next step A_ = 1 for j in range(self.c_max_length // (next_ncol * 2) ): for i in range(_SCREAMING_SNAKE_CASE ): new_dft[i].append(dft[i][j] - current_root * dft[i + next_ncol][j] ) current_root *= root # Update A_ = new_dft A_ = next_ncol // 2 return dft[0] def __A ( self ) -> Tuple: A_ = self.__dft('''A''' ) A_ = self.__dft('''B''' ) A_ = [[dft_a[i] * dft_b[i] for i in range(self.c_max_length )]] del dft_a del dft_b # Corner Case if len(inverce_c[0] ) <= 1: return inverce_c[0] # Inverse DFT A_ = 2 while next_ncol <= self.c_max_length: A_ = [[] for i in range(_SCREAMING_SNAKE_CASE )] A_ = self.root ** (next_ncol // 2) A_ = 1 # First half of next step for j in range(self.c_max_length // next_ncol ): for i in range(next_ncol // 2 ): # Even positions new_inverse_c[i].append( ( inverce_c[i][j] + inverce_c[i][j + self.c_max_length // next_ncol] ) / 2 ) # Odd positions new_inverse_c[i + next_ncol // 2].append( ( inverce_c[i][j] - inverce_c[i][j + self.c_max_length // next_ncol] ) / (2 * current_root) ) current_root *= root # Update A_ = new_inverse_c next_ncol *= 2 # Unpack A_ = [round(x[0].real , 8 ) + round(x[0].imag , 8 ) * 1J for x in inverce_c] # Remove leading 0's while inverce_c[-1] == 0: inverce_c.pop() return inverce_c def __str__( self ) -> Tuple: A_ = '''A = ''' + ''' + '''.join( F'''{coef}*x^{i}''' for coef, i in enumerate(self.polyA[: self.len_A] ) ) A_ = '''B = ''' + ''' + '''.join( F'''{coef}*x^{i}''' for coef, i in enumerate(self.polyB[: self.len_B] ) ) A_ = '''A*B = ''' + ''' + '''.join( F'''{coef}*x^{i}''' for coef, i in enumerate(self.product ) ) return F'''{a}\n{b}\n{c}''' # Unit tests if __name__ == "__main__": import doctest doctest.testmod()
18
'''simple docstring''' import absl # noqa: F401 # Here to have a nice missing dependency error message early on import nltk # noqa: F401 # Here to have a nice missing dependency error message early on import numpy # noqa: F401 # Here to have a nice missing dependency error message early on import six # noqa: F401 # Here to have a nice missing dependency error message early on from rouge_score import rouge_scorer, scoring import datasets __snake_case : Any = '\\n@inproceedings{lin-2004-rouge,\n title = "{ROUGE}: A Package for Automatic Evaluation of Summaries",\n author = "Lin, Chin-Yew",\n booktitle = "Text Summarization Branches Out",\n month = jul,\n year = "2004",\n address = "Barcelona, Spain",\n publisher = "Association for Computational Linguistics",\n url = "https://www.aclweb.org/anthology/W04-1013",\n pages = "74--81",\n}\n' __snake_case : Dict = '\\nROUGE, or Recall-Oriented Understudy for Gisting Evaluation, is a set of metrics and a software package used for\nevaluating automatic summarization and machine translation software in natural language processing.\nThe metrics compare an automatically produced summary or translation against a reference or a set of references (human-produced) summary or translation.\n\nNote that ROUGE is case insensitive, meaning that upper case letters are treated the same way as lower case letters.\n\nThis metrics is a wrapper around Google Research reimplementation of ROUGE:\nhttps://github.com/google-research/google-research/tree/master/rouge\n' __snake_case : Optional[int] = '\nCalculates average rouge scores for a list of hypotheses and references\nArgs:\n predictions: list of predictions to score. Each prediction\n should be a string with tokens separated by spaces.\n references: list of reference for each prediction. Each\n reference should be a string with tokens separated by spaces.\n rouge_types: A list of rouge types to calculate.\n Valid names:\n `"rouge{n}"` (e.g. `"rouge1"`, `"rouge2"`) where: {n} is the n-gram based scoring,\n `"rougeL"`: Longest common subsequence based scoring.\n `"rougeLSum"`: rougeLsum splits text using `"\n"`.\n See details in https://github.com/huggingface/datasets/issues/617\n use_stemmer: Bool indicating whether Porter stemmer should be used to strip word suffixes.\n use_aggregator: Return aggregates if this is set to True\nReturns:\n rouge1: rouge_1 (precision, recall, f1),\n rouge2: rouge_2 (precision, recall, f1),\n rougeL: rouge_l (precision, recall, f1),\n rougeLsum: rouge_lsum (precision, recall, f1)\nExamples:\n\n >>> rouge = datasets.load_metric(\'rouge\')\n >>> predictions = ["hello there", "general kenobi"]\n >>> references = ["hello there", "general kenobi"]\n >>> results = rouge.compute(predictions=predictions, references=references)\n >>> print(list(results.keys()))\n [\'rouge1\', \'rouge2\', \'rougeL\', \'rougeLsum\']\n >>> print(results["rouge1"])\n AggregateScore(low=Score(precision=1.0, recall=1.0, fmeasure=1.0), mid=Score(precision=1.0, recall=1.0, fmeasure=1.0), high=Score(precision=1.0, recall=1.0, fmeasure=1.0))\n >>> print(results["rouge1"].mid.fmeasure)\n 1.0\n' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __UpperCAmelCase ( datasets.Metric ): '''simple docstring''' def __A ( self ) -> List[str]: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Value('''string''' , id='''sequence''' ), '''references''': datasets.Value('''string''' , id='''sequence''' ), } ) , codebase_urls=['''https://github.com/google-research/google-research/tree/master/rouge'''] , reference_urls=[ '''https://en.wikipedia.org/wiki/ROUGE_(metric)''', '''https://github.com/google-research/google-research/tree/master/rouge''', ] , ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=False ) -> Optional[int]: if rouge_types is None: A_ = ['''rouge1''', '''rouge2''', '''rougeL''', '''rougeLsum'''] A_ = rouge_scorer.RougeScorer(rouge_types=_SCREAMING_SNAKE_CASE , use_stemmer=_SCREAMING_SNAKE_CASE ) if use_aggregator: A_ = scoring.BootstrapAggregator() else: A_ = [] for ref, pred in zip(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): A_ = scorer.score(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) if use_aggregator: aggregator.add_scores(_SCREAMING_SNAKE_CASE ) else: scores.append(_SCREAMING_SNAKE_CASE ) if use_aggregator: A_ = aggregator.aggregate() else: A_ = {} for key in scores[0]: A_ = [score[key] for score in scores] return result
18
1
'''simple docstring''' class __UpperCAmelCase : '''simple docstring''' def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE=None ) -> List[Any]: A_ = data A_ = previous A_ = next_node def __str__( self ) -> str: return F'''{self.data}''' def __A ( self ) -> int: return self.data def __A ( self ) -> Any: return self.next def __A ( self ) -> Optional[Any]: return self.previous class __UpperCAmelCase : '''simple docstring''' def __init__( self , _SCREAMING_SNAKE_CASE ) -> str: A_ = head def __iter__( self ) -> Any: return self def __A ( self ) -> str: if not self.current: raise StopIteration else: A_ = self.current.get_data() A_ = self.current.get_next() return value class __UpperCAmelCase : '''simple docstring''' def __init__( self ) -> Dict: A_ = None # First node in list A_ = None # Last node in list def __str__( self ) -> List[Any]: A_ = self.head A_ = [] while current is not None: nodes.append(current.get_data() ) A_ = current.get_next() return " ".join(str(_SCREAMING_SNAKE_CASE ) for node in nodes ) def __contains__( self , _SCREAMING_SNAKE_CASE ) -> Optional[int]: A_ = self.head while current: if current.get_data() == value: return True A_ = current.get_next() return False def __iter__( self ) -> int: return LinkedListIterator(self.head ) def __A ( self ) -> Any: if self.head: return self.head.get_data() return None def __A ( self ) -> List[str]: if self.tail: return self.tail.get_data() return None def __A ( self , _SCREAMING_SNAKE_CASE ) -> None: if self.head is None: A_ = node A_ = node else: self.insert_before_node(self.head , _SCREAMING_SNAKE_CASE ) def __A ( self , _SCREAMING_SNAKE_CASE ) -> None: if self.head is None: self.set_head(_SCREAMING_SNAKE_CASE ) else: self.insert_after_node(self.tail , _SCREAMING_SNAKE_CASE ) def __A ( self , _SCREAMING_SNAKE_CASE ) -> None: A_ = Node(_SCREAMING_SNAKE_CASE ) if self.head is None: self.set_head(_SCREAMING_SNAKE_CASE ) else: self.set_tail(_SCREAMING_SNAKE_CASE ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> None: A_ = node A_ = node.previous if node.get_previous() is None: A_ = node_to_insert else: A_ = node_to_insert A_ = node_to_insert def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> None: A_ = node A_ = node.next if node.get_next() is None: A_ = node_to_insert else: A_ = node_to_insert A_ = node_to_insert def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> None: A_ = 1 A_ = Node(_SCREAMING_SNAKE_CASE ) A_ = self.head while node: if current_position == position: self.insert_before_node(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) return current_position += 1 A_ = node.next self.insert_after_node(self.tail , _SCREAMING_SNAKE_CASE ) def __A ( self , _SCREAMING_SNAKE_CASE ) -> Node: A_ = self.head while node: if node.get_data() == item: return node A_ = node.get_next() raise Exception('''Node not found''' ) def __A ( self , _SCREAMING_SNAKE_CASE ) -> Optional[int]: if (node := self.get_node(_SCREAMING_SNAKE_CASE )) is not None: if node == self.head: A_ = self.head.get_next() if node == self.tail: A_ = self.tail.get_previous() self.remove_node_pointers(_SCREAMING_SNAKE_CASE ) @staticmethod def __A ( _SCREAMING_SNAKE_CASE ) -> None: if node.get_next(): A_ = node.previous if node.get_previous(): A_ = node.next A_ = None A_ = None def __A ( self ) -> Any: return self.head is None def _UpperCAmelCase ( ) -> None: pass if __name__ == "__main__": import doctest doctest.testmod()
18
'''simple docstring''' import gc import importlib.metadata import tempfile import unittest from packaging import version from transformers import ( AutoModel, AutoModelForCausalLM, AutoModelForSeqaSeqLM, AutoModelForSequenceClassification, AutoTokenizer, BitsAndBytesConfig, pipeline, ) from transformers.testing_utils import ( is_torch_available, require_accelerate, require_bitsandbytes, require_torch, require_torch_gpu, require_torch_multi_gpu, slow, ) def _UpperCAmelCase ( _UpperCamelCase : str ) -> int: if model.config.model_type == "gpt2": return model.transformer.h[0].mlp.c_fc return model.transformer.h[0].mlp.dense_ah_to_h if is_torch_available(): import torch import torch.nn as nn class __UpperCAmelCase ( nn.Module ): '''simple docstring''' def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Dict: super().__init__() A_ = module A_ = nn.Sequential( nn.Linear(module.in_features , _SCREAMING_SNAKE_CASE , bias=_SCREAMING_SNAKE_CASE ) , nn.Linear(_SCREAMING_SNAKE_CASE , module.out_features , bias=_SCREAMING_SNAKE_CASE ) , ) A_ = (2.0 / (5 * min(module.in_features , module.out_features ))) ** 0.5 nn.init.normal_(self.adapter[0].weight , std=_SCREAMING_SNAKE_CASE ) nn.init.zeros_(self.adapter[1].weight ) self.adapter.to(module.weight.device ) def __A ( self , _SCREAMING_SNAKE_CASE , *_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) -> Tuple: return self.module(_SCREAMING_SNAKE_CASE , *_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) + self.adapter(_SCREAMING_SNAKE_CASE ) @require_bitsandbytes @require_accelerate @require_torch @require_torch_gpu @slow class __UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' __lowercase : Dict = 'bigscience/bloom-1b7' # Constant values __lowercase : str = 2.109659552692574 __lowercase : int = 'Hello my name is' __lowercase : Optional[Any] = set() EXPECTED_OUTPUTS.add('Hello my name is John and I am a professional photographer. I' ) EXPECTED_OUTPUTS.add('Hello my name is John.\nI am a friend of your father.\n' ) EXPECTED_OUTPUTS.add('Hello my name is John Doe, I am a student at the University' ) __lowercase : Optional[Any] = 10 def __A ( self ) -> List[str]: # Models and tokenizer A_ = AutoTokenizer.from_pretrained(self.model_name ) class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' def __A ( self ) -> List[Any]: super().setUp() # Models and tokenizer A_ = AutoModelForCausalLM.from_pretrained( self.model_name , torch_dtype=torch.floataa , device_map='''auto''' ) A_ = AutoModelForCausalLM.from_pretrained(self.model_name , load_in_abit=_SCREAMING_SNAKE_CASE , device_map='''auto''' ) def __A ( self ) -> List[str]: del self.model_fpaa del self.model_abit gc.collect() torch.cuda.empty_cache() def __A ( self ) -> Tuple: A_ = self.model_abit.config self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE , '''quantization_config''' ) ) A_ = config.to_dict() A_ = config.to_diff_dict() A_ = config.to_json_string() def __A ( self ) -> Union[str, Any]: from bitsandbytes.nn import Paramsabit A_ = self.model_fpaa.get_memory_footprint() A_ = self.model_abit.get_memory_footprint() self.assertAlmostEqual(mem_fpaa / mem_abit , self.EXPECTED_RELATIVE_DIFFERENCE ) A_ = get_some_linear_layer(self.model_abit ) self.assertTrue(linear.weight.__class__ == Paramsabit ) def __A ( self ) -> Union[str, Any]: from transformers import TaPreTrainedModel self.model_fpaa.get_memory_footprint() self.model_abit.get_memory_footprint() for name, module in self.model_abit.named_modules(): if isinstance(_SCREAMING_SNAKE_CASE , torch.nn.Linear ): if name not in ["lm_head"] + TaPreTrainedModel._keep_in_fpaa_modules: # 4-bit parameters are packed in uint8 variables self.assertTrue(module.weight.dtype == torch.uinta ) def __A ( self ) -> Optional[int]: A_ = self.tokenizer(self.input_text , return_tensors='''pt''' ) A_ = self.model_abit.generate(input_ids=encoded_input['''input_ids'''].to(0 ) , max_new_tokens=10 ) self.assertIn(self.tokenizer.decode(output_sequences[0] , skip_special_tokens=_SCREAMING_SNAKE_CASE ) , self.EXPECTED_OUTPUTS ) def __A ( self ) -> Optional[int]: A_ = BitsAndBytesConfig() A_ = True A_ = AutoModelForCausalLM.from_pretrained( self.model_name , quantization_config=_SCREAMING_SNAKE_CASE , device_map='''auto''' ) A_ = self.tokenizer(self.input_text , return_tensors='''pt''' ) A_ = model_abit_from_config.generate( input_ids=encoded_input['''input_ids'''].to(0 ) , max_new_tokens=10 ) self.assertIn(self.tokenizer.decode(output_sequences[0] , skip_special_tokens=_SCREAMING_SNAKE_CASE ) , self.EXPECTED_OUTPUTS ) def __A ( self ) -> Tuple: with self.assertRaises(_SCREAMING_SNAKE_CASE ), tempfile.TemporaryDirectory() as tmpdirname: self.model_abit.save_pretrained(_SCREAMING_SNAKE_CASE ) def __A ( self ) -> Tuple: A_ = BitsAndBytesConfig() with self.assertRaises(_SCREAMING_SNAKE_CASE ): A_ = AutoModelForCausalLM.from_pretrained( self.model_name , quantization_config=_SCREAMING_SNAKE_CASE , load_in_abit=_SCREAMING_SNAKE_CASE , device_map='''auto''' , bnb_abit_quant_type='''nf4''' , ) def __A ( self ) -> Dict: with self.assertRaises(_SCREAMING_SNAKE_CASE ): # Tries with `str` self.model_abit.to('''cpu''' ) with self.assertRaises(_SCREAMING_SNAKE_CASE ): # Tries with a `dtype`` self.model_abit.to(torch.floataa ) with self.assertRaises(_SCREAMING_SNAKE_CASE ): # Tries with a `device` self.model_abit.to(torch.device('''cuda:0''' ) ) with self.assertRaises(_SCREAMING_SNAKE_CASE ): # Tries with a `device` self.model_abit.float() with self.assertRaises(_SCREAMING_SNAKE_CASE ): # Tries with a `device` self.model_abit.half() # Test if we did not break anything A_ = self.tokenizer(self.input_text , return_tensors='''pt''' ) A_ = self.model_fpaa.to(torch.floataa ) A_ = self.model_fpaa.generate(input_ids=encoded_input['''input_ids'''].to(0 ) , max_new_tokens=10 ) # Check this does not throw an error A_ = self.model_fpaa.to('''cpu''' ) # Check this does not throw an error A_ = self.model_fpaa.half() # Check this does not throw an error A_ = self.model_fpaa.float() def __A ( self ) -> Optional[int]: A_ = AutoModelForSeqaSeqLM.from_pretrained('''t5-small''' , load_in_abit=_SCREAMING_SNAKE_CASE , device_map='''auto''' ) self.assertTrue(model.decoder.block[0].layer[2].DenseReluDense.wo.weight.dtype == torch.floataa ) @require_bitsandbytes @require_accelerate @require_torch @require_torch_gpu @slow class __UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' @classmethod def __A ( cls ) -> Optional[Any]: A_ = '''t5-small''' A_ = '''google/flan-t5-small''' # flan-t5 uses dense-act instead of dense-relu-dense A_ = AutoTokenizer.from_pretrained(cls.model_name ) A_ = '''Translate in German: Hello, my dog is cute''' def __A ( self ) -> Any: gc.collect() torch.cuda.empty_cache() def __A ( self ) -> Tuple: from transformers import TaForConditionalGeneration A_ = TaForConditionalGeneration._keep_in_fpaa_modules A_ = None # test with `t5-small` A_ = TaForConditionalGeneration.from_pretrained(self.model_name , load_in_abit=_SCREAMING_SNAKE_CASE , device_map='''auto''' ) A_ = self.tokenizer(self.input_text , return_tensors='''pt''' ).to(0 ) A_ = model.generate(**_SCREAMING_SNAKE_CASE ) # test with `flan-t5-small` A_ = TaForConditionalGeneration.from_pretrained( self.dense_act_model_name , load_in_abit=_SCREAMING_SNAKE_CASE , device_map='''auto''' ) A_ = self.tokenizer(self.input_text , return_tensors='''pt''' ).to(0 ) A_ = model.generate(**_SCREAMING_SNAKE_CASE ) A_ = modules def __A ( self ) -> Dict: import bitsandbytes as bnb from transformers import TaForConditionalGeneration # test with `t5-small` A_ = TaForConditionalGeneration.from_pretrained(self.model_name , load_in_abit=_SCREAMING_SNAKE_CASE , device_map='''auto''' ) # there was a bug with decoders - this test checks that it is fixed self.assertTrue(isinstance(model.decoder.block[0].layer[0].SelfAttention.q , bnb.nn.Linearabit ) ) A_ = self.tokenizer(self.input_text , return_tensors='''pt''' ).to(0 ) A_ = model.generate(**_SCREAMING_SNAKE_CASE ) # test with `flan-t5-small` A_ = TaForConditionalGeneration.from_pretrained( self.dense_act_model_name , load_in_abit=_SCREAMING_SNAKE_CASE , device_map='''auto''' ) A_ = self.tokenizer(self.input_text , return_tensors='''pt''' ).to(0 ) A_ = model.generate(**_SCREAMING_SNAKE_CASE ) class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' def __A ( self ) -> int: super().setUp() # model_name A_ = '''bigscience/bloom-560m''' A_ = '''t5-small''' # Different types of model A_ = AutoModel.from_pretrained(self.model_name , load_in_abit=_SCREAMING_SNAKE_CASE , device_map='''auto''' ) # Sequence classification model A_ = AutoModelForSequenceClassification.from_pretrained( self.model_name , load_in_abit=_SCREAMING_SNAKE_CASE , device_map='''auto''' ) # CausalLM model A_ = AutoModelForCausalLM.from_pretrained(self.model_name , load_in_abit=_SCREAMING_SNAKE_CASE , device_map='''auto''' ) # Seq2seq model A_ = AutoModelForSeqaSeqLM.from_pretrained( self.seq_to_seq_name , load_in_abit=_SCREAMING_SNAKE_CASE , device_map='''auto''' ) def __A ( self ) -> Union[str, Any]: del self.base_model del self.sequence_model del self.model_abit del self.seq_to_seq_model gc.collect() torch.cuda.empty_cache() def __A ( self ) -> List[str]: from bitsandbytes.nn import Paramsabit self.assertTrue(self.base_model.h[-1].mlp.dense_ah_to_h.weight.__class__ == Paramsabit ) # Other heads should be nn.Parameter self.assertTrue(self.model_abit.lm_head.weight.__class__ == torch.nn.Parameter ) self.assertTrue(self.sequence_model.score.weight.__class__ == torch.nn.Parameter ) self.assertTrue(self.seq_to_seq_model.lm_head.weight.__class__ == torch.nn.Parameter ) class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' def __A ( self ) -> Tuple: super().setUp() def __A ( self ) -> List[Any]: del self.pipe gc.collect() torch.cuda.empty_cache() def __A ( self ) -> Optional[Any]: A_ = pipeline( '''text-generation''' , model=self.model_name , model_kwargs={'''device_map''': '''auto''', '''load_in_4bit''': True, '''torch_dtype''': torch.floataa} , max_new_tokens=self.MAX_NEW_TOKENS , ) # Real second forward pass A_ = self.pipe(self.input_text ) self.assertIn(pipeline_output[0]['''generated_text'''] , self.EXPECTED_OUTPUTS ) @require_torch_multi_gpu class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' def __A ( self ) -> List[str]: super().setUp() def __A ( self ) -> Optional[int]: A_ = AutoModelForCausalLM.from_pretrained( self.model_name , load_in_abit=_SCREAMING_SNAKE_CASE , device_map='''balanced''' ) # Check correct device map self.assertEqual(set(model_parallel.hf_device_map.values() ) , {0, 1} ) # Check that inference pass works on the model A_ = self.tokenizer(self.input_text , return_tensors='''pt''' ) # Second real batch A_ = model_parallel.generate(input_ids=encoded_input['''input_ids'''].to(0 ) , max_new_tokens=10 ) self.assertIn(self.tokenizer.decode(output_parallel[0] , skip_special_tokens=_SCREAMING_SNAKE_CASE ) , self.EXPECTED_OUTPUTS ) class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' def __A ( self ) -> str: A_ = '''facebook/opt-350m''' super().setUp() def __A ( self ) -> Optional[int]: if version.parse(importlib.metadata.version('''bitsandbytes''' ) ) < version.parse('''0.37.0''' ): return # Step 1: freeze all parameters A_ = AutoModelForCausalLM.from_pretrained(self.model_name , load_in_abit=_SCREAMING_SNAKE_CASE ) self.assertEqual(set(model.hf_device_map.values() ) , {torch.cuda.current_device()} ) for param in model.parameters(): A_ = False # freeze the model - train adapters later if param.ndim == 1: # cast the small parameters (e.g. layernorm) to fp32 for stability A_ = param.data.to(torch.floataa ) # Step 2: add adapters for _, module in model.named_modules(): if "OPTAttention" in repr(type(_SCREAMING_SNAKE_CASE ) ): A_ = LoRALayer(module.q_proj , rank=16 ) A_ = LoRALayer(module.k_proj , rank=16 ) A_ = LoRALayer(module.v_proj , rank=16 ) # Step 3: dummy batch A_ = self.tokenizer('''Test batch ''' , return_tensors='''pt''' ).to(0 ) # Step 4: Check if the gradient is not None with torch.cuda.amp.autocast(): A_ = model.forward(**_SCREAMING_SNAKE_CASE ) out.logits.norm().backward() for module in model.modules(): if isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): self.assertTrue(module.adapter[1].weight.grad is not None ) self.assertTrue(module.adapter[1].weight.grad.norm().item() > 0 ) elif isinstance(_SCREAMING_SNAKE_CASE , nn.Embedding ): self.assertTrue(module.weight.grad is None ) class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' __lowercase : int = 'gpt2-xl' __lowercase : List[Any] = 3.3191854854152187
18
1
'''simple docstring''' import baseaa def _UpperCAmelCase ( _UpperCamelCase : str ) -> bytes: return baseaa.aaaencode(string.encode('''utf-8''' ) ) def _UpperCAmelCase ( _UpperCamelCase : bytes ) -> str: return baseaa.aaadecode(_UpperCamelCase ).decode('''utf-8''' ) if __name__ == "__main__": import doctest doctest.testmod()
18
'''simple docstring''' import os from typing import BinaryIO, Optional, Union import numpy as np import pyarrow.parquet as pq from .. import Audio, Dataset, Features, Image, NamedSplit, Value, config from ..features.features import FeatureType, _visit from ..formatting import query_table from ..packaged_modules import _PACKAGED_DATASETS_MODULES from ..packaged_modules.parquet.parquet import Parquet from ..utils import logging from ..utils.typing import NestedDataStructureLike, PathLike from .abc import AbstractDatasetReader def _UpperCAmelCase ( _UpperCamelCase : Features ) -> Optional[int]: A_ = np.inf def set_batch_size(_UpperCamelCase : FeatureType ) -> None: nonlocal batch_size if isinstance(_UpperCamelCase, _UpperCamelCase ): A_ = min(_UpperCamelCase, config.PARQUET_ROW_GROUP_SIZE_FOR_IMAGE_DATASETS ) elif isinstance(_UpperCamelCase, _UpperCamelCase ): A_ = min(_UpperCamelCase, config.PARQUET_ROW_GROUP_SIZE_FOR_AUDIO_DATASETS ) elif isinstance(_UpperCamelCase, _UpperCamelCase ) and feature.dtype == "binary": A_ = min(_UpperCamelCase, config.PARQUET_ROW_GROUP_SIZE_FOR_BINARY_DATASETS ) _visit(_UpperCamelCase, _UpperCamelCase ) return None if batch_size is np.inf else batch_size class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = False , _SCREAMING_SNAKE_CASE = False , _SCREAMING_SNAKE_CASE = None , **_SCREAMING_SNAKE_CASE , ) -> int: super().__init__( _SCREAMING_SNAKE_CASE , split=_SCREAMING_SNAKE_CASE , 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 , ) A_ = path_or_paths if isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) else {self.split: path_or_paths} A_ = _PACKAGED_DATASETS_MODULES['''parquet'''][1] A_ = Parquet( cache_dir=_SCREAMING_SNAKE_CASE , data_files=_SCREAMING_SNAKE_CASE , features=_SCREAMING_SNAKE_CASE , hash=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE , ) def __A ( self ) -> str: # Build iterable dataset if self.streaming: A_ = self.builder.as_streaming_dataset(split=self.split ) # Build regular (map-style) dataset else: A_ = None A_ = None A_ = None A_ = 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 , ) A_ = self.builder.as_dataset( split=self.split , verification_mode=_SCREAMING_SNAKE_CASE , in_memory=self.keep_in_memory ) return dataset class __UpperCAmelCase : '''simple docstring''' def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None , **_SCREAMING_SNAKE_CASE , ) -> Dict: A_ = dataset A_ = path_or_buf A_ = batch_size or get_writer_batch_size(dataset.features ) A_ = parquet_writer_kwargs def __A ( self ) -> int: A_ = self.batch_size if self.batch_size else config.DEFAULT_MAX_BATCH_SIZE if isinstance(self.path_or_buf , (str, bytes, os.PathLike) ): with open(self.path_or_buf , '''wb+''' ) as buffer: A_ = self._write(file_obj=_SCREAMING_SNAKE_CASE , batch_size=_SCREAMING_SNAKE_CASE , **self.parquet_writer_kwargs ) else: A_ = self._write(file_obj=self.path_or_buf , batch_size=_SCREAMING_SNAKE_CASE , **self.parquet_writer_kwargs ) return written def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) -> int: A_ = 0 A_ = parquet_writer_kwargs.pop('''path_or_buf''' , _SCREAMING_SNAKE_CASE ) A_ = self.dataset.features.arrow_schema A_ = pq.ParquetWriter(_SCREAMING_SNAKE_CASE , schema=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) for offset in logging.tqdm( range(0 , len(self.dataset ) , _SCREAMING_SNAKE_CASE ) , unit='''ba''' , disable=not logging.is_progress_bar_enabled() , desc='''Creating parquet from Arrow format''' , ): A_ = query_table( table=self.dataset._data , key=slice(_SCREAMING_SNAKE_CASE , offset + batch_size ) , indices=self.dataset._indices if self.dataset._indices is not None else None , ) writer.write_table(_SCREAMING_SNAKE_CASE ) written += batch.nbytes writer.close() return written
18
1
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging __snake_case : Dict = logging.get_logger(__name__) __snake_case : Any = { 'google/vivit-b-16x2-kinetics400': ( 'https://huggingface.co/google/vivit-b-16x2-kinetics400/resolve/main/config.json' ), # See all Vivit models at https://huggingface.co/models?filter=vivit } class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' __lowercase : str = 'vivit' def __init__( self , _SCREAMING_SNAKE_CASE=224 , _SCREAMING_SNAKE_CASE=32 , _SCREAMING_SNAKE_CASE=[2, 16, 16] , _SCREAMING_SNAKE_CASE=3 , _SCREAMING_SNAKE_CASE=768 , _SCREAMING_SNAKE_CASE=12 , _SCREAMING_SNAKE_CASE=12 , _SCREAMING_SNAKE_CASE=3072 , _SCREAMING_SNAKE_CASE="gelu_fast" , _SCREAMING_SNAKE_CASE=0.0 , _SCREAMING_SNAKE_CASE=0.0 , _SCREAMING_SNAKE_CASE=0.02 , _SCREAMING_SNAKE_CASE=1E-06 , _SCREAMING_SNAKE_CASE=True , **_SCREAMING_SNAKE_CASE , ) -> int: A_ = hidden_size A_ = num_hidden_layers A_ = num_attention_heads A_ = intermediate_size A_ = hidden_act A_ = hidden_dropout_prob A_ = attention_probs_dropout_prob A_ = initializer_range A_ = layer_norm_eps A_ = image_size A_ = num_frames A_ = tubelet_size A_ = num_channels A_ = qkv_bias super().__init__(**_SCREAMING_SNAKE_CASE )
18
'''simple docstring''' from statistics import mean, stdev def _UpperCAmelCase ( _UpperCamelCase : list, _UpperCamelCase : int = 3 ) -> list: A_ = min(_UpperCamelCase ) A_ = max(_UpperCamelCase ) # normalize data return [round((x - x_min) / (x_max - x_min), _UpperCamelCase ) for x in data] def _UpperCAmelCase ( _UpperCamelCase : list, _UpperCamelCase : int = 3 ) -> list: A_ = mean(_UpperCamelCase ) A_ = stdev(_UpperCamelCase ) # standardize data return [round((x - mu) / (sigma), _UpperCamelCase ) for x in data]
18
1
'''simple docstring''' import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging __snake_case : List[Any] = logging.get_logger(__name__) __snake_case : Dict = { '''microsoft/wavlm-base''': '''https://huggingface.co/microsoft/wavlm-base/resolve/main/config.json''', # See all WavLM models at https://huggingface.co/models?filter=wavlm } class __UpperCAmelCase ( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' __lowercase : Optional[Any] = 'wavlm' def __init__( self , _SCREAMING_SNAKE_CASE=32 , _SCREAMING_SNAKE_CASE=768 , _SCREAMING_SNAKE_CASE=12 , _SCREAMING_SNAKE_CASE=12 , _SCREAMING_SNAKE_CASE=3072 , _SCREAMING_SNAKE_CASE="gelu" , _SCREAMING_SNAKE_CASE=0.1 , _SCREAMING_SNAKE_CASE=0.1 , _SCREAMING_SNAKE_CASE=0.1 , _SCREAMING_SNAKE_CASE=0.0 , _SCREAMING_SNAKE_CASE=0.1 , _SCREAMING_SNAKE_CASE=0.1 , _SCREAMING_SNAKE_CASE=0.02 , _SCREAMING_SNAKE_CASE=1E-5 , _SCREAMING_SNAKE_CASE="group" , _SCREAMING_SNAKE_CASE="gelu" , _SCREAMING_SNAKE_CASE=(512, 512, 512, 512, 512, 512, 512) , _SCREAMING_SNAKE_CASE=(5, 2, 2, 2, 2, 2, 2) , _SCREAMING_SNAKE_CASE=(10, 3, 3, 3, 3, 2, 2) , _SCREAMING_SNAKE_CASE=False , _SCREAMING_SNAKE_CASE=128 , _SCREAMING_SNAKE_CASE=16 , _SCREAMING_SNAKE_CASE=320 , _SCREAMING_SNAKE_CASE=800 , _SCREAMING_SNAKE_CASE=False , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=0.05 , _SCREAMING_SNAKE_CASE=10 , _SCREAMING_SNAKE_CASE=2 , _SCREAMING_SNAKE_CASE=0.0 , _SCREAMING_SNAKE_CASE=10 , _SCREAMING_SNAKE_CASE=320 , _SCREAMING_SNAKE_CASE=2 , _SCREAMING_SNAKE_CASE=0.1 , _SCREAMING_SNAKE_CASE=100 , _SCREAMING_SNAKE_CASE=256 , _SCREAMING_SNAKE_CASE=256 , _SCREAMING_SNAKE_CASE=0.1 , _SCREAMING_SNAKE_CASE="mean" , _SCREAMING_SNAKE_CASE=False , _SCREAMING_SNAKE_CASE=False , _SCREAMING_SNAKE_CASE=256 , _SCREAMING_SNAKE_CASE=(512, 512, 512, 512, 1500) , _SCREAMING_SNAKE_CASE=(5, 3, 3, 1, 1) , _SCREAMING_SNAKE_CASE=(1, 2, 3, 1, 1) , _SCREAMING_SNAKE_CASE=512 , _SCREAMING_SNAKE_CASE=80 , _SCREAMING_SNAKE_CASE=0 , _SCREAMING_SNAKE_CASE=1 , _SCREAMING_SNAKE_CASE=2 , _SCREAMING_SNAKE_CASE=False , _SCREAMING_SNAKE_CASE=3 , _SCREAMING_SNAKE_CASE=2 , _SCREAMING_SNAKE_CASE=3 , _SCREAMING_SNAKE_CASE=None , **_SCREAMING_SNAKE_CASE , ) -> List[Any]: super().__init__(**__a , pad_token_id=__a , bos_token_id=__a , eos_token_id=__a ) A_ = hidden_size A_ = feat_extract_norm A_ = feat_extract_activation A_ = list(__a ) A_ = list(__a ) A_ = list(__a ) A_ = conv_bias A_ = num_buckets A_ = max_bucket_distance A_ = num_conv_pos_embeddings A_ = num_conv_pos_embedding_groups A_ = len(self.conv_dim ) A_ = num_hidden_layers A_ = intermediate_size A_ = hidden_act A_ = num_attention_heads A_ = hidden_dropout A_ = attention_dropout A_ = activation_dropout A_ = feat_proj_dropout A_ = final_dropout A_ = layerdrop A_ = layer_norm_eps A_ = initializer_range A_ = num_ctc_classes A_ = vocab_size A_ = do_stable_layer_norm A_ = use_weighted_layer_sum A_ = classifier_proj_size if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( '''Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` ==''' ''' `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) =''' F''' {len(self.conv_dim )}`, `len(config.conv_stride) = {len(self.conv_stride )}`,''' F''' `len(config.conv_kernel) = {len(self.conv_kernel )}`.''' ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 A_ = apply_spec_augment A_ = mask_time_prob A_ = mask_time_length A_ = mask_time_min_masks A_ = mask_feature_prob A_ = mask_feature_length # parameters for pretraining with codevector quantized representations A_ = num_codevectors_per_group A_ = num_codevector_groups A_ = contrastive_logits_temperature A_ = num_negatives A_ = codevector_dim A_ = proj_codevector_dim A_ = diversity_loss_weight # ctc loss A_ = ctc_loss_reduction A_ = ctc_zero_infinity # adapter A_ = add_adapter A_ = adapter_kernel_size A_ = adapter_stride A_ = num_adapter_layers A_ = output_hidden_size or hidden_size # SequenceClassification-specific parameter. Feel free to ignore for other classes. A_ = classifier_proj_size # XVector-specific parameters. Feel free to ignore for other classes. A_ = list(__a ) A_ = list(__a ) A_ = list(__a ) A_ = xvector_output_dim @property def __A ( self ) -> str: return functools.reduce(operator.mul , self.conv_stride , 1 )
350
'''simple docstring''' import logging import os import sys from dataclasses import dataclass, field from typing import Optional import evaluate import numpy as np import torch from datasets import load_dataset from PIL import Image from torchvision.transforms import ( CenterCrop, Compose, Normalize, RandomHorizontalFlip, RandomResizedCrop, Resize, ToTensor, ) import transformers from transformers import ( MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING, AutoConfig, AutoImageProcessor, AutoModelForImageClassification, HfArgumentParser, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import get_last_checkpoint from transformers.utils import check_min_version, send_example_telemetry from transformers.utils.versions import require_version __snake_case : Optional[int] = logging.getLogger(__name__) # Will error if the minimal version of Transformers is not installed. Remove at your own risks. check_min_version('4.31.0') require_version('datasets>=1.8.0', 'To fix: pip install -r examples/pytorch/image-classification/requirements.txt') __snake_case : str = list(MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING.keys()) __snake_case : int = tuple(conf.model_type for conf in MODEL_CONFIG_CLASSES) def _UpperCAmelCase ( _UpperCamelCase : str ) -> int: with open(_UpperCamelCase, '''rb''' ) as f: A_ = Image.open(_UpperCamelCase ) return im.convert('''RGB''' ) @dataclass class __UpperCAmelCase : '''simple docstring''' __lowercase : Optional[str] = field( default=_UpperCamelCase , metadata={ 'help': 'Name of a dataset from the hub (could be your own, possibly private dataset hosted on the hub).' } , ) __lowercase : Optional[str] = field( default=_UpperCamelCase , metadata={'help': 'The configuration name of the dataset to use (via the datasets library).'} ) __lowercase : Optional[str] = field(default=_UpperCamelCase , metadata={'help': 'A folder containing the training data.'} ) __lowercase : Optional[str] = field(default=_UpperCamelCase , metadata={'help': 'A folder containing the validation data.'} ) __lowercase : Optional[float] = field( default=0.15 , metadata={'help': 'Percent to split off of train for validation.'} ) __lowercase : Optional[int] = field( default=_UpperCamelCase , metadata={ 'help': ( 'For debugging purposes or quicker training, truncate the number of training examples to this ' 'value if set.' ) } , ) __lowercase : Optional[int] = field( default=_UpperCamelCase , metadata={ 'help': ( 'For debugging purposes or quicker training, truncate the number of evaluation examples to this ' 'value if set.' ) } , ) def __A ( self ) -> int: if self.dataset_name is None and (self.train_dir is None and self.validation_dir is None): raise ValueError( '''You must specify either a dataset name from the hub or a train and/or validation directory.''' ) @dataclass class __UpperCAmelCase : '''simple docstring''' __lowercase : str = field( default='google/vit-base-patch16-224-in21k' , metadata={'help': 'Path to pretrained model or model identifier from huggingface.co/models'} , ) __lowercase : Optional[str] = field( default=_UpperCamelCase , metadata={'help': 'If training from scratch, pass a model type from the list: ' + ', '.join(_UpperCamelCase )} , ) __lowercase : Optional[str] = field( default=_UpperCamelCase , metadata={'help': 'Pretrained config name or path if not the same as model_name'} ) __lowercase : Optional[str] = field( default=_UpperCamelCase , metadata={'help': 'Where do you want to store the pretrained models downloaded from s3'} ) __lowercase : str = field( default='main' , metadata={'help': 'The specific model version to use (can be a branch name, tag name or commit id).'} , ) __lowercase : str = field(default=_UpperCamelCase , metadata={'help': 'Name or path of preprocessor config.'} ) __lowercase : bool = field( default=_UpperCamelCase , metadata={ 'help': ( 'Will use the token generated when running `huggingface-cli login` (necessary to use this script ' 'with private models).' ) } , ) __lowercase : bool = field( default=_UpperCamelCase , metadata={'help': 'Will enable to load a pretrained model whose head dimensions are different.'} , ) def _UpperCAmelCase ( _UpperCamelCase : str ) -> Dict: A_ = torch.stack([example['''pixel_values'''] for example in examples] ) A_ = torch.tensor([example['''labels'''] for example in examples] ) return {"pixel_values": pixel_values, "labels": labels} def _UpperCAmelCase ( ) -> Tuple: # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. A_ = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith('''.json''' ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. A_ ,A_ ,A_ = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: A_ ,A_ ,A_ = parser.parse_args_into_dataclasses() # Sending telemetry. Tracking the example usage helps us better allocate resources to maintain them. The # information sent is the one passed as arguments along with your Python/PyTorch versions. send_example_telemetry('''run_image_classification''', _UpperCamelCase, _UpperCamelCase ) # Setup logging logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''', datefmt='''%m/%d/%Y %H:%M:%S''', handlers=[logging.StreamHandler(sys.stdout )], ) if training_args.should_log: # The default of training_args.log_level is passive, so we set log level at info here to have that default. transformers.utils.logging.set_verbosity_info() A_ = training_args.get_process_log_level() logger.setLevel(_UpperCamelCase ) transformers.utils.logging.set_verbosity(_UpperCamelCase ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Log on each process the small summary: logger.warning( F'''Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}''' + F'''distributed training: {bool(training_args.local_rank != -1 )}, 16-bits training: {training_args.fpaa}''' ) logger.info(F'''Training/evaluation parameters {training_args}''' ) # Detecting last checkpoint. A_ = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: A_ = get_last_checkpoint(training_args.output_dir ) if last_checkpoint is None and len(os.listdir(training_args.output_dir ) ) > 0: raise ValueError( F'''Output directory ({training_args.output_dir}) already exists and is not empty. ''' '''Use --overwrite_output_dir to overcome.''' ) elif last_checkpoint is not None and training_args.resume_from_checkpoint is None: logger.info( F'''Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change ''' '''the `--output_dir` or add `--overwrite_output_dir` to train from scratch.''' ) # Set seed before initializing model. set_seed(training_args.seed ) # Initialize our dataset and prepare it for the 'image-classification' task. if data_args.dataset_name is not None: A_ = load_dataset( data_args.dataset_name, data_args.dataset_config_name, cache_dir=model_args.cache_dir, task='''image-classification''', use_auth_token=True if model_args.use_auth_token else None, ) else: A_ = {} if data_args.train_dir is not None: A_ = os.path.join(data_args.train_dir, '''**''' ) if data_args.validation_dir is not None: A_ = os.path.join(data_args.validation_dir, '''**''' ) A_ = load_dataset( '''imagefolder''', data_files=_UpperCamelCase, cache_dir=model_args.cache_dir, task='''image-classification''', ) # If we don't have a validation split, split off a percentage of train as validation. A_ = None if '''validation''' in dataset.keys() else data_args.train_val_split if isinstance(data_args.train_val_split, _UpperCamelCase ) and data_args.train_val_split > 0.0: A_ = dataset['''train'''].train_test_split(data_args.train_val_split ) A_ = split['''train'''] A_ = split['''test'''] # Prepare label mappings. # We'll include these in the model's config to get human readable labels in the Inference API. A_ = dataset['''train'''].features['''labels'''].names A_ ,A_ = {}, {} for i, label in enumerate(_UpperCamelCase ): A_ = str(_UpperCamelCase ) A_ = label # Load the accuracy metric from the datasets package A_ = evaluate.load('''accuracy''' ) # Define our compute_metrics function. It takes an `EvalPrediction` object (a namedtuple with a # predictions and label_ids field) and has to return a dictionary string to float. def compute_metrics(_UpperCamelCase : Optional[Any] ): return metric.compute(predictions=np.argmax(p.predictions, axis=1 ), references=p.label_ids ) A_ = AutoConfig.from_pretrained( model_args.config_name or model_args.model_name_or_path, num_labels=len(_UpperCamelCase ), labelaid=_UpperCamelCase, idalabel=_UpperCamelCase, finetuning_task='''image-classification''', cache_dir=model_args.cache_dir, revision=model_args.model_revision, use_auth_token=True if model_args.use_auth_token else None, ) A_ = AutoModelForImageClassification.from_pretrained( model_args.model_name_or_path, from_tf=bool('''.ckpt''' in model_args.model_name_or_path ), config=_UpperCamelCase, cache_dir=model_args.cache_dir, revision=model_args.model_revision, use_auth_token=True if model_args.use_auth_token else None, ignore_mismatched_sizes=model_args.ignore_mismatched_sizes, ) A_ = AutoImageProcessor.from_pretrained( model_args.image_processor_name or model_args.model_name_or_path, cache_dir=model_args.cache_dir, revision=model_args.model_revision, use_auth_token=True if model_args.use_auth_token else None, ) # Define torchvision transforms to be applied to each image. if "shortest_edge" in image_processor.size: A_ = image_processor.size['''shortest_edge'''] else: A_ = (image_processor.size['''height'''], image_processor.size['''width''']) A_ = Normalize(mean=image_processor.image_mean, std=image_processor.image_std ) A_ = Compose( [ RandomResizedCrop(_UpperCamelCase ), RandomHorizontalFlip(), ToTensor(), normalize, ] ) A_ = Compose( [ Resize(_UpperCamelCase ), CenterCrop(_UpperCamelCase ), ToTensor(), normalize, ] ) def train_transforms(_UpperCamelCase : Dict ): A_ = [ _train_transforms(pil_img.convert('''RGB''' ) ) for pil_img in example_batch['''image'''] ] return example_batch def val_transforms(_UpperCamelCase : Any ): A_ = [_val_transforms(pil_img.convert('''RGB''' ) ) for pil_img in example_batch['''image''']] return example_batch if training_args.do_train: if "train" not in dataset: raise ValueError('''--do_train requires a train dataset''' ) if data_args.max_train_samples is not None: A_ = ( dataset['''train'''].shuffle(seed=training_args.seed ).select(range(data_args.max_train_samples ) ) ) # Set the training transforms dataset["train"].set_transform(_UpperCamelCase ) if training_args.do_eval: if "validation" not in dataset: raise ValueError('''--do_eval requires a validation dataset''' ) if data_args.max_eval_samples is not None: A_ = ( dataset['''validation'''].shuffle(seed=training_args.seed ).select(range(data_args.max_eval_samples ) ) ) # Set the validation transforms dataset["validation"].set_transform(_UpperCamelCase ) # Initalize our trainer A_ = Trainer( model=_UpperCamelCase, args=_UpperCamelCase, train_dataset=dataset['''train'''] if training_args.do_train else None, eval_dataset=dataset['''validation'''] if training_args.do_eval else None, compute_metrics=_UpperCamelCase, tokenizer=_UpperCamelCase, data_collator=_UpperCamelCase, ) # Training if training_args.do_train: A_ = None if training_args.resume_from_checkpoint is not None: A_ = training_args.resume_from_checkpoint elif last_checkpoint is not None: A_ = last_checkpoint A_ = trainer.train(resume_from_checkpoint=_UpperCamelCase ) trainer.save_model() trainer.log_metrics('''train''', train_result.metrics ) trainer.save_metrics('''train''', train_result.metrics ) trainer.save_state() # Evaluation if training_args.do_eval: A_ = trainer.evaluate() trainer.log_metrics('''eval''', _UpperCamelCase ) trainer.save_metrics('''eval''', _UpperCamelCase ) # Write model card and (optionally) push to hub A_ = { '''finetuned_from''': model_args.model_name_or_path, '''tasks''': '''image-classification''', '''dataset''': data_args.dataset_name, '''tags''': ['''image-classification''', '''vision'''], } if training_args.push_to_hub: trainer.push_to_hub(**_UpperCamelCase ) else: trainer.create_model_card(**_UpperCamelCase ) if __name__ == "__main__": main()
18
0
'''simple docstring''' import tempfile import unittest import numpy as np from diffusers import ( DDIMScheduler, DPMSolverMultistepScheduler, EulerAncestralDiscreteScheduler, EulerDiscreteScheduler, LMSDiscreteScheduler, OnnxStableDiffusionPipeline, PNDMScheduler, ) from diffusers.utils.testing_utils import is_onnx_available, nightly, require_onnxruntime, require_torch_gpu from ..test_pipelines_onnx_common import OnnxPipelineTesterMixin if is_onnx_available(): import onnxruntime as ort class __UpperCAmelCase ( A__ , unittest.TestCase ): '''simple docstring''' __lowercase : Union[str, Any] = "hf-internal-testing/tiny-random-OnnxStableDiffusionPipeline" def __A ( self , _SCREAMING_SNAKE_CASE=0 ) -> int: A_ = np.random.RandomState(__A ) A_ = { """prompt""": """A painting of a squirrel eating a burger""", """generator""": generator, """num_inference_steps""": 2, """guidance_scale""": 7.5, """output_type""": """numpy""", } return inputs def __A ( self ) -> List[Any]: A_ = OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint , provider='''CPUExecutionProvider''' ) pipe.set_progress_bar_config(disable=__A ) A_ = self.get_dummy_inputs() A_ = pipe(**__A ).images A_ = image[0, -3:, -3:, -1] assert image.shape == (1, 128, 128, 3) A_ = np.array([0.65_072, 0.58_492, 0.48_219, 0.55_521, 0.53_180, 0.55_939, 0.50_697, 0.39_800, 0.46_455] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def __A ( self ) -> Tuple: A_ = OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint , provider='''CPUExecutionProvider''' ) A_ = PNDMScheduler.from_config(pipe.scheduler.config , skip_prk_steps=__A ) pipe.set_progress_bar_config(disable=__A ) A_ = self.get_dummy_inputs() A_ = pipe(**__A ).images A_ = image[0, -3:, -3:, -1] assert image.shape == (1, 128, 128, 3) A_ = np.array([0.65_863, 0.59_425, 0.49_326, 0.56_313, 0.53_875, 0.56_627, 0.51_065, 0.39_777, 0.46_330] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def __A ( self ) -> Any: A_ = OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint , provider='''CPUExecutionProvider''' ) A_ = LMSDiscreteScheduler.from_config(pipe.scheduler.config ) pipe.set_progress_bar_config(disable=__A ) A_ = self.get_dummy_inputs() A_ = pipe(**__A ).images A_ = image[0, -3:, -3:, -1] assert image.shape == (1, 128, 128, 3) A_ = np.array([0.53_755, 0.60_786, 0.47_402, 0.49_488, 0.51_869, 0.49_819, 0.47_985, 0.38_957, 0.44_279] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def __A ( self ) -> List[Any]: A_ = OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint , provider='''CPUExecutionProvider''' ) A_ = EulerDiscreteScheduler.from_config(pipe.scheduler.config ) pipe.set_progress_bar_config(disable=__A ) A_ = self.get_dummy_inputs() A_ = pipe(**__A ).images A_ = image[0, -3:, -3:, -1] assert image.shape == (1, 128, 128, 3) A_ = np.array([0.53_755, 0.60_786, 0.47_402, 0.49_488, 0.51_869, 0.49_819, 0.47_985, 0.38_957, 0.44_279] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def __A ( self ) -> Any: A_ = OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint , provider='''CPUExecutionProvider''' ) A_ = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config ) pipe.set_progress_bar_config(disable=__A ) A_ = self.get_dummy_inputs() A_ = pipe(**__A ).images A_ = image[0, -3:, -3:, -1] assert image.shape == (1, 128, 128, 3) A_ = np.array([0.53_817, 0.60_812, 0.47_384, 0.49_530, 0.51_894, 0.49_814, 0.47_984, 0.38_958, 0.44_271] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def __A ( self ) -> int: A_ = OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint , provider='''CPUExecutionProvider''' ) A_ = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config ) pipe.set_progress_bar_config(disable=__A ) A_ = self.get_dummy_inputs() A_ = pipe(**__A ).images A_ = image[0, -3:, -3:, -1] assert image.shape == (1, 128, 128, 3) A_ = np.array([0.53_895, 0.60_808, 0.47_933, 0.49_608, 0.51_886, 0.49_950, 0.48_053, 0.38_957, 0.44_200] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def __A ( self ) -> str: A_ = OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint , provider='''CPUExecutionProvider''' ) pipe.set_progress_bar_config(disable=__A ) A_ = self.get_dummy_inputs() A_ = 3 * [inputs["""prompt"""]] # forward A_ = pipe(**__A ) A_ = output.images[0, -3:, -3:, -1] A_ = self.get_dummy_inputs() A_ = 3 * [inputs.pop('''prompt''' )] A_ = pipe.tokenizer( __A , padding='''max_length''' , max_length=pipe.tokenizer.model_max_length , truncation=__A , return_tensors='''np''' , ) A_ = text_inputs["""input_ids"""] A_ = pipe.text_encoder(input_ids=text_inputs.astype(np.intaa ) )[0] A_ = prompt_embeds # forward A_ = pipe(**__A ) A_ = output.images[0, -3:, -3:, -1] assert np.abs(image_slice_a.flatten() - image_slice_a.flatten() ).max() < 1E-4 def __A ( self ) -> str: A_ = OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint , provider='''CPUExecutionProvider''' ) pipe.set_progress_bar_config(disable=__A ) A_ = self.get_dummy_inputs() A_ = 3 * ["""this is a negative prompt"""] A_ = negative_prompt A_ = 3 * [inputs["""prompt"""]] # forward A_ = pipe(**__A ) A_ = output.images[0, -3:, -3:, -1] A_ = self.get_dummy_inputs() A_ = 3 * [inputs.pop('''prompt''' )] A_ = [] for p in [prompt, negative_prompt]: A_ = pipe.tokenizer( __A , padding='''max_length''' , max_length=pipe.tokenizer.model_max_length , truncation=__A , return_tensors='''np''' , ) A_ = text_inputs["""input_ids"""] embeds.append(pipe.text_encoder(input_ids=text_inputs.astype(np.intaa ) )[0] ) A_ = embeds # forward A_ = pipe(**__A ) A_ = output.images[0, -3:, -3:, -1] assert np.abs(image_slice_a.flatten() - image_slice_a.flatten() ).max() < 1E-4 @nightly @require_onnxruntime @require_torch_gpu class __UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' @property def __A ( self ) -> List[Any]: return ( "CUDAExecutionProvider", { "gpu_mem_limit": "15000000000", # 15GB "arena_extend_strategy": "kSameAsRequested", }, ) @property def __A ( self ) -> Optional[Any]: A_ = ort.SessionOptions() A_ = False return options def __A ( self ) -> Dict: # using the PNDM scheduler by default A_ = OnnxStableDiffusionPipeline.from_pretrained( '''CompVis/stable-diffusion-v1-4''' , revision='''onnx''' , safety_checker=__A , feature_extractor=__A , provider=self.gpu_provider , sess_options=self.gpu_options , ) sd_pipe.set_progress_bar_config(disable=__A ) A_ = """A painting of a squirrel eating a burger""" np.random.seed(0 ) A_ = sd_pipe([prompt] , guidance_scale=6.0 , num_inference_steps=10 , output_type='''np''' ) A_ = output.images A_ = image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) A_ = np.array([0.0_452, 0.0_390, 0.0_087, 0.0_350, 0.0_617, 0.0_364, 0.0_544, 0.0_523, 0.0_720] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 def __A ( self ) -> Tuple: A_ = DDIMScheduler.from_pretrained( '''runwayml/stable-diffusion-v1-5''' , subfolder='''scheduler''' , revision='''onnx''' ) A_ = OnnxStableDiffusionPipeline.from_pretrained( '''runwayml/stable-diffusion-v1-5''' , revision='''onnx''' , scheduler=__A , safety_checker=__A , feature_extractor=__A , provider=self.gpu_provider , sess_options=self.gpu_options , ) sd_pipe.set_progress_bar_config(disable=__A ) A_ = """open neural network exchange""" A_ = np.random.RandomState(0 ) A_ = sd_pipe([prompt] , guidance_scale=7.5 , num_inference_steps=10 , generator=__A , output_type='''np''' ) A_ = output.images A_ = image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) A_ = np.array([0.2_867, 0.1_974, 0.1_481, 0.7_294, 0.7_251, 0.6_667, 0.4_194, 0.5_642, 0.6_486] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 def __A ( self ) -> str: A_ = LMSDiscreteScheduler.from_pretrained( '''runwayml/stable-diffusion-v1-5''' , subfolder='''scheduler''' , revision='''onnx''' ) A_ = OnnxStableDiffusionPipeline.from_pretrained( '''runwayml/stable-diffusion-v1-5''' , revision='''onnx''' , scheduler=__A , safety_checker=__A , feature_extractor=__A , provider=self.gpu_provider , sess_options=self.gpu_options , ) sd_pipe.set_progress_bar_config(disable=__A ) A_ = """open neural network exchange""" A_ = np.random.RandomState(0 ) A_ = sd_pipe([prompt] , guidance_scale=7.5 , num_inference_steps=10 , generator=__A , output_type='''np''' ) A_ = output.images A_ = image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) A_ = np.array([0.2_306, 0.1_959, 0.1_593, 0.6_549, 0.6_394, 0.5_408, 0.5_065, 0.6_010, 0.6_161] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 def __A ( self ) -> Tuple: A_ = 0 def test_callback_fn(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> None: A_ = True nonlocal number_of_steps number_of_steps += 1 if step == 0: assert latents.shape == (1, 4, 64, 64) A_ = latents[0, -3:, -3:, -1] A_ = np.array( [-0.6_772, -0.3_835, -1.2_456, 0.1_905, -1.0_974, 0.6_967, -1.9_353, 0.0_178, 1.0_167] ) assert np.abs(latents_slice.flatten() - expected_slice ).max() < 1E-3 elif step == 5: assert latents.shape == (1, 4, 64, 64) A_ = latents[0, -3:, -3:, -1] A_ = np.array( [-0.3_351, 0.2_241, -0.1_837, -0.2_325, -0.6_577, 0.3_393, -0.0_241, 0.5_899, 1.3_875] ) assert np.abs(latents_slice.flatten() - expected_slice ).max() < 1E-3 A_ = False A_ = OnnxStableDiffusionPipeline.from_pretrained( '''runwayml/stable-diffusion-v1-5''' , revision='''onnx''' , safety_checker=__A , feature_extractor=__A , provider=self.gpu_provider , sess_options=self.gpu_options , ) pipe.set_progress_bar_config(disable=__A ) A_ = """Andromeda galaxy in a bottle""" A_ = np.random.RandomState(0 ) pipe( prompt=__A , num_inference_steps=5 , guidance_scale=7.5 , generator=__A , callback=__A , callback_steps=1 , ) assert test_callback_fn.has_been_called assert number_of_steps == 6 def __A ( self ) -> List[str]: A_ = OnnxStableDiffusionPipeline.from_pretrained( '''runwayml/stable-diffusion-v1-5''' , revision='''onnx''' , safety_checker=__A , feature_extractor=__A , provider=self.gpu_provider , sess_options=self.gpu_options , ) assert isinstance(__A , __A ) assert pipe.safety_checker is None A_ = pipe('''example prompt''' , num_inference_steps=2 ).images[0] assert image is not None # check that there's no error when saving a pipeline with one of the models being None with tempfile.TemporaryDirectory() as tmpdirname: pipe.save_pretrained(__A ) A_ = OnnxStableDiffusionPipeline.from_pretrained(__A ) # sanity check that the pipeline still works assert pipe.safety_checker is None A_ = pipe('''example prompt''' , num_inference_steps=2 ).images[0] assert image is not None
351
'''simple docstring''' import tempfile import unittest import numpy as np from huggingface_hub import HfFolder, delete_repo from requests.exceptions import HTTPError from transformers import BertConfig, is_flax_available from transformers.testing_utils import TOKEN, USER, is_staging_test, require_flax if is_flax_available(): import os from flax.core.frozen_dict import unfreeze from flax.traverse_util import flatten_dict from transformers import FlaxBertModel __snake_case : str = '0.12' # assumed parallelism: 8 @require_flax @is_staging_test class __UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' @classmethod def __A ( cls ) -> Dict: A_ = TOKEN HfFolder.save_token(_SCREAMING_SNAKE_CASE ) @classmethod def __A ( cls ) -> Optional[int]: try: delete_repo(token=cls._token , repo_id='''test-model-flax''' ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id='''valid_org/test-model-flax-org''' ) except HTTPError: pass def __A ( self ) -> str: A_ = BertConfig( vocab_size=99 , hidden_size=32 , num_hidden_layers=5 , num_attention_heads=4 , intermediate_size=37 ) A_ = FlaxBertModel(_SCREAMING_SNAKE_CASE ) model.push_to_hub('''test-model-flax''' , use_auth_token=self._token ) A_ = FlaxBertModel.from_pretrained(F'''{USER}/test-model-flax''' ) A_ = flatten_dict(unfreeze(model.params ) ) A_ = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): A_ = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(_SCREAMING_SNAKE_CASE , 1E-3 , msg=F'''{key} not identical''' ) # Reset repo delete_repo(token=self._token , repo_id='''test-model-flax''' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(_SCREAMING_SNAKE_CASE , repo_id='''test-model-flax''' , push_to_hub=_SCREAMING_SNAKE_CASE , use_auth_token=self._token ) A_ = FlaxBertModel.from_pretrained(F'''{USER}/test-model-flax''' ) A_ = flatten_dict(unfreeze(model.params ) ) A_ = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): A_ = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(_SCREAMING_SNAKE_CASE , 1E-3 , msg=F'''{key} not identical''' ) def __A ( self ) -> List[str]: A_ = BertConfig( vocab_size=99 , hidden_size=32 , num_hidden_layers=5 , num_attention_heads=4 , intermediate_size=37 ) A_ = FlaxBertModel(_SCREAMING_SNAKE_CASE ) model.push_to_hub('''valid_org/test-model-flax-org''' , use_auth_token=self._token ) A_ = FlaxBertModel.from_pretrained('''valid_org/test-model-flax-org''' ) A_ = flatten_dict(unfreeze(model.params ) ) A_ = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): A_ = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(_SCREAMING_SNAKE_CASE , 1E-3 , msg=F'''{key} not identical''' ) # Reset repo delete_repo(token=self._token , repo_id='''valid_org/test-model-flax-org''' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained( _SCREAMING_SNAKE_CASE , repo_id='''valid_org/test-model-flax-org''' , push_to_hub=_SCREAMING_SNAKE_CASE , use_auth_token=self._token ) A_ = FlaxBertModel.from_pretrained('''valid_org/test-model-flax-org''' ) A_ = flatten_dict(unfreeze(model.params ) ) A_ = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): A_ = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(_SCREAMING_SNAKE_CASE , 1E-3 , msg=F'''{key} not identical''' ) def _UpperCAmelCase ( _UpperCamelCase : Union[str, Any], _UpperCamelCase : Tuple ) -> Dict: A_ = True A_ = flatten_dict(modela.params ) A_ = flatten_dict(modela.params ) for key in flat_params_a.keys(): if np.sum(np.abs(flat_params_a[key] - flat_params_a[key] ) ) > 1E-4: A_ = False return models_are_equal @require_flax class __UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' def __A ( self ) -> List[str]: A_ = BertConfig.from_pretrained('''hf-internal-testing/tiny-bert-flax-only''' ) A_ = FlaxBertModel(_SCREAMING_SNAKE_CASE ) A_ = '''bert''' with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(os.path.join(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) with self.assertRaises(_SCREAMING_SNAKE_CASE ): A_ = FlaxBertModel.from_pretrained(_SCREAMING_SNAKE_CASE ) A_ = FlaxBertModel.from_pretrained(_SCREAMING_SNAKE_CASE , subfolder=_SCREAMING_SNAKE_CASE ) self.assertTrue(check_models_equal(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) def __A ( self ) -> List[Any]: A_ = BertConfig.from_pretrained('''hf-internal-testing/tiny-bert-flax-only''' ) A_ = FlaxBertModel(_SCREAMING_SNAKE_CASE ) A_ = '''bert''' with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(os.path.join(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) , max_shard_size='''10KB''' ) with self.assertRaises(_SCREAMING_SNAKE_CASE ): A_ = FlaxBertModel.from_pretrained(_SCREAMING_SNAKE_CASE ) A_ = FlaxBertModel.from_pretrained(_SCREAMING_SNAKE_CASE , subfolder=_SCREAMING_SNAKE_CASE ) self.assertTrue(check_models_equal(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) def __A ( self ) -> Dict: A_ = '''bert''' A_ = '''hf-internal-testing/tiny-random-bert-subfolder''' with self.assertRaises(_SCREAMING_SNAKE_CASE ): A_ = FlaxBertModel.from_pretrained(_SCREAMING_SNAKE_CASE ) A_ = FlaxBertModel.from_pretrained(_SCREAMING_SNAKE_CASE , subfolder=_SCREAMING_SNAKE_CASE ) self.assertIsNotNone(_SCREAMING_SNAKE_CASE ) def __A ( self ) -> Optional[Any]: A_ = '''bert''' A_ = '''hf-internal-testing/tiny-random-bert-sharded-subfolder''' with self.assertRaises(_SCREAMING_SNAKE_CASE ): A_ = FlaxBertModel.from_pretrained(_SCREAMING_SNAKE_CASE ) A_ = FlaxBertModel.from_pretrained(_SCREAMING_SNAKE_CASE , subfolder=_SCREAMING_SNAKE_CASE ) self.assertIsNotNone(_SCREAMING_SNAKE_CASE )
18
0
'''simple docstring''' import unittest import numpy as np import timeout_decorator # noqa from transformers import BlenderbotConfig, is_flax_available from transformers.testing_utils import jax_device, require_flax, slow from ...generation.test_flax_utils import FlaxGenerationTesterMixin from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor if is_flax_available(): import os # The slow tests are often failing with OOM error on GPU # This makes JAX allocate exactly what is needed on demand, and deallocate memory that is no longer needed # but will be slower as stated here https://jax.readthedocs.io/en/latest/gpu_memory_allocation.html __snake_case : Tuple = 'platform' import jax import jax.numpy as jnp from transformers import BlenderbotTokenizer from transformers.models.blenderbot.modeling_flax_blenderbot import ( FlaxBlenderbotForConditionalGeneration, FlaxBlenderbotModel, shift_tokens_right, ) def _UpperCAmelCase ( _UpperCamelCase : Dict, _UpperCamelCase : Optional[int], _UpperCamelCase : Optional[int]=None, _UpperCamelCase : Any=None, _UpperCamelCase : Optional[Any]=None, _UpperCamelCase : List[str]=None, _UpperCamelCase : Tuple=None, _UpperCamelCase : Union[str, Any]=None, ) -> Optional[int]: if attention_mask is None: A_ = np.where(input_ids != config.pad_token_id, 1, 0 ) if decoder_attention_mask is None: A_ = np.where(decoder_input_ids != config.pad_token_id, 1, 0 ) if head_mask is None: A_ = np.ones((config.encoder_layers, config.encoder_attention_heads) ) if decoder_head_mask is None: A_ = np.ones((config.decoder_layers, config.decoder_attention_heads) ) if cross_attn_head_mask is None: A_ = np.ones((config.decoder_layers, config.decoder_attention_heads) ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": attention_mask, } class __UpperCAmelCase : '''simple docstring''' def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=13 , _SCREAMING_SNAKE_CASE=7 , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=False , _SCREAMING_SNAKE_CASE=99 , _SCREAMING_SNAKE_CASE=16 , _SCREAMING_SNAKE_CASE=2 , _SCREAMING_SNAKE_CASE=4 , _SCREAMING_SNAKE_CASE=4 , _SCREAMING_SNAKE_CASE="gelu" , _SCREAMING_SNAKE_CASE=0.1 , _SCREAMING_SNAKE_CASE=0.1 , _SCREAMING_SNAKE_CASE=32 , _SCREAMING_SNAKE_CASE=2 , _SCREAMING_SNAKE_CASE=1 , _SCREAMING_SNAKE_CASE=0 , _SCREAMING_SNAKE_CASE=0.02 , ) -> Dict: A_ = parent A_ = batch_size A_ = seq_length A_ = is_training A_ = use_labels A_ = vocab_size A_ = hidden_size A_ = num_hidden_layers A_ = num_attention_heads A_ = intermediate_size A_ = hidden_act A_ = hidden_dropout_prob A_ = attention_probs_dropout_prob A_ = max_position_embeddings A_ = eos_token_id A_ = pad_token_id A_ = bos_token_id A_ = initializer_range def __A ( self ) -> List[str]: A_ = np.clip(ids_tensor([self.batch_size, self.seq_length - 1] , self.vocab_size ) , 3 , self.vocab_size ) A_ = np.concatenate((input_ids, 2 * np.ones((self.batch_size, 1) , dtype=np.intaa )) , -1 ) A_ = shift_tokens_right(_UpperCAmelCase , 1 , 2 ) A_ = BlenderbotConfig( vocab_size=self.vocab_size , d_model=self.hidden_size , encoder_layers=self.num_hidden_layers , decoder_layers=self.num_hidden_layers , encoder_attention_heads=self.num_attention_heads , decoder_attention_heads=self.num_attention_heads , encoder_ffn_dim=self.intermediate_size , decoder_ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , eos_token_id=self.eos_token_id , bos_token_id=self.bos_token_id , pad_token_id=self.pad_token_id , initializer_range=self.initializer_range , use_cache=_UpperCAmelCase , ) A_ = prepare_blenderbot_inputs_dict(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) return config, inputs_dict def __A ( self ) -> Tuple: A_ = self.prepare_config_and_inputs() return config, inputs_dict def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Tuple: A_ = 20 A_ = model_class_name(_UpperCAmelCase ) A_ = model.encode(inputs_dict['''input_ids'''] ) A_ = ( inputs_dict['''decoder_input_ids'''], inputs_dict['''decoder_attention_mask'''], ) A_ = model.init_cache(decoder_input_ids.shape[0] , _UpperCAmelCase , _UpperCAmelCase ) A_ = jnp.ones((decoder_input_ids.shape[0], max_decoder_length) , dtype='''i4''' ) A_ = jnp.broadcast_to( jnp.arange(decoder_input_ids.shape[-1] - 1 )[None, :] , (decoder_input_ids.shape[0], decoder_input_ids.shape[-1] - 1) , ) A_ = model.decode( decoder_input_ids[:, :-1] , _UpperCAmelCase , decoder_attention_mask=_UpperCAmelCase , past_key_values=_UpperCAmelCase , decoder_position_ids=_UpperCAmelCase , ) A_ = jnp.array(decoder_input_ids.shape[0] * [[decoder_input_ids.shape[-1] - 1]] , dtype='''i4''' ) A_ = model.decode( decoder_input_ids[:, -1:] , _UpperCAmelCase , decoder_attention_mask=_UpperCAmelCase , past_key_values=outputs_cache.past_key_values , decoder_position_ids=_UpperCAmelCase , ) A_ = model.decode(_UpperCAmelCase , _UpperCAmelCase ) A_ = np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) ) self.parent.assertTrue(diff < 1E-3 , msg=F'''Max diff is {diff}''' ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> List[Any]: A_ = 20 A_ = model_class_name(_UpperCAmelCase ) A_ = model.encode(inputs_dict['''input_ids'''] ) A_ = ( inputs_dict['''decoder_input_ids'''], inputs_dict['''decoder_attention_mask'''], ) A_ = jnp.concatenate( [ decoder_attention_mask, jnp.zeros((decoder_attention_mask.shape[0], max_decoder_length - decoder_attention_mask.shape[1]) ), ] , axis=-1 , ) A_ = model.init_cache(decoder_input_ids.shape[0] , _UpperCAmelCase , _UpperCAmelCase ) A_ = jnp.broadcast_to( jnp.arange(decoder_input_ids.shape[-1] - 1 )[None, :] , (decoder_input_ids.shape[0], decoder_input_ids.shape[-1] - 1) , ) A_ = model.decode( decoder_input_ids[:, :-1] , _UpperCAmelCase , decoder_attention_mask=_UpperCAmelCase , past_key_values=_UpperCAmelCase , decoder_position_ids=_UpperCAmelCase , ) A_ = jnp.array(decoder_input_ids.shape[0] * [[decoder_input_ids.shape[-1] - 1]] , dtype='''i4''' ) A_ = model.decode( decoder_input_ids[:, -1:] , _UpperCAmelCase , past_key_values=outputs_cache.past_key_values , decoder_attention_mask=_UpperCAmelCase , decoder_position_ids=_UpperCAmelCase , ) A_ = model.decode(_UpperCAmelCase , _UpperCAmelCase , decoder_attention_mask=_UpperCAmelCase ) A_ = np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) ) self.parent.assertTrue(diff < 1E-3 , msg=F'''Max diff is {diff}''' ) @require_flax class __UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' __lowercase : Tuple = 99 def __A ( self ) -> Tuple: A_ = np.array( [ [71, 82, 18, 33, 46, 91, 2], [68, 34, 26, 58, 30, 82, 2], [5, 97, 17, 39, 94, 40, 2], [76, 83, 94, 25, 70, 78, 2], [87, 59, 41, 35, 48, 66, 2], [55, 13, 16, 58, 5, 2, 1], # note padding [64, 27, 31, 51, 12, 75, 2], [52, 64, 86, 17, 83, 39, 2], [48, 61, 9, 24, 71, 82, 2], [26, 1, 60, 48, 22, 13, 2], [21, 5, 62, 28, 14, 76, 2], [45, 98, 37, 86, 59, 48, 2], [70, 70, 50, 9, 28, 0, 2], ] , dtype=np.intaa , ) A_ = input_ids.shape[0] A_ = BlenderbotConfig( vocab_size=self.vocab_size , d_model=24 , encoder_layers=2 , decoder_layers=2 , encoder_attention_heads=2 , decoder_attention_heads=2 , encoder_ffn_dim=32 , decoder_ffn_dim=32 , max_position_embeddings=48 , eos_token_id=2 , pad_token_id=1 , bos_token_id=0 , ) return config, input_ids, batch_size def __A ( self ) -> List[str]: A_ = self._get_config_and_data() A_ = FlaxBlenderbotForConditionalGeneration(_UpperCAmelCase ) A_ = lm_model(input_ids=_UpperCAmelCase ) A_ = (batch_size, input_ids.shape[1], config.vocab_size) self.assertEqual(outputs['''logits'''].shape , _UpperCAmelCase ) def __A ( self ) -> Tuple: A_ = BlenderbotConfig( vocab_size=self.vocab_size , d_model=14 , encoder_layers=2 , decoder_layers=2 , encoder_attention_heads=2 , decoder_attention_heads=2 , encoder_ffn_dim=8 , decoder_ffn_dim=8 , max_position_embeddings=48 , ) A_ = FlaxBlenderbotForConditionalGeneration(_UpperCAmelCase ) A_ = np.array([[71, 82, 18, 33, 46, 91, 2], [68, 34, 26, 58, 30, 2, 1]] , dtype=np.intaa ) A_ = np.array([[82, 71, 82, 18, 2], [58, 68, 2, 1, 1]] , dtype=np.intaa ) A_ = lm_model(input_ids=_UpperCAmelCase , decoder_input_ids=_UpperCAmelCase ) A_ = (*summary.shape, config.vocab_size) self.assertEqual(outputs['''logits'''].shape , _UpperCAmelCase ) def __A ( self ) -> Any: A_ = np.array([[71, 82, 18, 33, 2, 1, 1], [68, 34, 26, 58, 30, 82, 2]] , dtype=np.intaa ) A_ = shift_tokens_right(_UpperCAmelCase , 1 , 2 ) A_ = np.equal(_UpperCAmelCase , 1 ).astype(np.floataa ).sum() A_ = np.equal(_UpperCAmelCase , 1 ).astype(np.floataa ).sum() self.assertEqual(shifted.shape , input_ids.shape ) self.assertEqual(_UpperCAmelCase , n_pad_before - 1 ) self.assertTrue(np.equal(shifted[:, 0] , 2 ).all() ) @require_flax class __UpperCAmelCase ( _UpperCAmelCase , unittest.TestCase , _UpperCAmelCase ): '''simple docstring''' __lowercase : List[Any] = True __lowercase : List[Any] = ( ( FlaxBlenderbotModel, FlaxBlenderbotForConditionalGeneration, ) if is_flax_available() else () ) __lowercase : Optional[int] = (FlaxBlenderbotForConditionalGeneration,) if is_flax_available() else () def __A ( self ) -> List[Any]: A_ = FlaxBlenderbotModelTester(self ) def __A ( self ) -> Union[str, Any]: A_ = self.model_tester.prepare_config_and_inputs() for model_class in self.all_model_classes: self.model_tester.check_use_cache_forward(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) def __A ( self ) -> Tuple: A_ = self.model_tester.prepare_config_and_inputs() for model_class in self.all_model_classes: self.model_tester.check_use_cache_forward_with_attn_mask(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) def __A ( self ) -> str: A_ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): A_ = self._prepare_for_class(_UpperCAmelCase , _UpperCAmelCase ) A_ = model_class(_UpperCAmelCase ) @jax.jit def encode_jitted(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=None , **_SCREAMING_SNAKE_CASE ): return model.encode(input_ids=_UpperCAmelCase , attention_mask=_UpperCAmelCase ) with self.subTest('''JIT Enabled''' ): A_ = encode_jitted(**_UpperCAmelCase ).to_tuple() with self.subTest('''JIT Disabled''' ): with jax.disable_jit(): A_ = encode_jitted(**_UpperCAmelCase ).to_tuple() self.assertEqual(len(_UpperCAmelCase ) , len(_UpperCAmelCase ) ) for jitted_output, output in zip(_UpperCAmelCase , _UpperCAmelCase ): self.assertEqual(jitted_output.shape , output.shape ) def __A ( self ) -> Tuple: A_ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): A_ = model_class(_UpperCAmelCase ) A_ = model.encode(inputs_dict['''input_ids'''] , inputs_dict['''attention_mask'''] ) A_ = { '''decoder_input_ids''': inputs_dict['''decoder_input_ids'''], '''decoder_attention_mask''': inputs_dict['''decoder_attention_mask'''], '''encoder_outputs''': encoder_outputs, } @jax.jit def decode_jitted(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): return model.decode( decoder_input_ids=_UpperCAmelCase , decoder_attention_mask=_UpperCAmelCase , encoder_outputs=_UpperCAmelCase , ) with self.subTest('''JIT Enabled''' ): A_ = decode_jitted(**_UpperCAmelCase ).to_tuple() with self.subTest('''JIT Disabled''' ): with jax.disable_jit(): A_ = decode_jitted(**_UpperCAmelCase ).to_tuple() self.assertEqual(len(_UpperCAmelCase ) , len(_UpperCAmelCase ) ) for jitted_output, output in zip(_UpperCAmelCase , _UpperCAmelCase ): self.assertEqual(jitted_output.shape , output.shape ) @slow def __A ( self ) -> Any: for model_class_name in self.all_model_classes: A_ = model_class_name.from_pretrained('''facebook/blenderbot-400M-distill''' ) # FlaxBlenderbotForSequenceClassification expects eos token in input_ids A_ = np.ones((1, 1) ) * model.config.eos_token_id A_ = model(_UpperCAmelCase ) self.assertIsNotNone(_UpperCAmelCase ) @unittest.skipUnless(jax_device != '''cpu''' , '''3B test too slow on CPU.''' ) @slow def __A ( self ) -> List[str]: A_ = {'''num_beams''': 1, '''early_stopping''': True, '''min_length''': 15, '''max_length''': 25} A_ = {'''skip_special_tokens''': True, '''clean_up_tokenization_spaces''': True} A_ = FlaxBlenderbotForConditionalGeneration.from_pretrained('''facebook/blenderbot-3B''' , from_pt=_UpperCAmelCase ) A_ = BlenderbotTokenizer.from_pretrained('''facebook/blenderbot-3B''' ) A_ = ['''Sam'''] A_ = tokenizer(_UpperCAmelCase , return_tensors='''jax''' ) A_ = model.generate(**_UpperCAmelCase , **_UpperCAmelCase ) A_ = '''Sam is a great name. It means "sun" in Gaelic.''' A_ = tokenizer.batch_decode(_UpperCAmelCase , **_UpperCAmelCase ) assert generated_txt[0].strip() == tgt_text
352
'''simple docstring''' def _UpperCAmelCase ( _UpperCamelCase : Union[str, Any] ) -> Dict: A_ = 1 A_ = 2 while i * i <= n: A_ = 0 while n % i == 0: n //= i multiplicity += 1 n_divisors *= multiplicity + 1 i += 1 if n > 1: n_divisors *= 2 return n_divisors def _UpperCAmelCase ( ) -> Optional[int]: A_ = 1 A_ = 1 while True: i += 1 t_num += i if count_divisors(_UpperCamelCase ) > 5_00: break return t_num if __name__ == "__main__": print(solution())
18
0
'''simple docstring''' import json import sys import tempfile import unittest from pathlib import Path import transformers from transformers import ( CONFIG_MAPPING, IMAGE_PROCESSOR_MAPPING, AutoConfig, AutoImageProcessor, CLIPConfig, CLIPImageProcessor, ) from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER sys.path.append(str(Path(__file__).parent.parent.parent.parent / 'utils')) from test_module.custom_configuration import CustomConfig # noqa E402 from test_module.custom_image_processing import CustomImageProcessor # noqa E402 class __UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' def __A ( self ) -> Optional[Any]: A_ = 0 def __A ( self ) -> Any: A_ = AutoImageProcessor.from_pretrained('''openai/clip-vit-base-patch32''' ) self.assertIsInstance(__snake_case , __snake_case ) def __A ( self ) -> str: with tempfile.TemporaryDirectory() as tmpdirname: A_ = Path(__snake_case ) / '''preprocessor_config.json''' A_ = Path(__snake_case ) / '''config.json''' json.dump( {'''image_processor_type''': '''CLIPImageProcessor''', '''processor_class''': '''CLIPProcessor'''} , open(__snake_case , '''w''' ) , ) json.dump({'''model_type''': '''clip'''} , open(__snake_case , '''w''' ) ) A_ = AutoImageProcessor.from_pretrained(__snake_case ) self.assertIsInstance(__snake_case , __snake_case ) def __A ( self ) -> Optional[Any]: # Ensure we can load the image processor from the feature extractor config with tempfile.TemporaryDirectory() as tmpdirname: A_ = Path(__snake_case ) / '''preprocessor_config.json''' A_ = Path(__snake_case ) / '''config.json''' json.dump( {'''feature_extractor_type''': '''CLIPFeatureExtractor''', '''processor_class''': '''CLIPProcessor'''} , open(__snake_case , '''w''' ) , ) json.dump({'''model_type''': '''clip'''} , open(__snake_case , '''w''' ) ) A_ = AutoImageProcessor.from_pretrained(__snake_case ) self.assertIsInstance(__snake_case , __snake_case ) def __A ( self ) -> Optional[int]: with tempfile.TemporaryDirectory() as tmpdirname: A_ = CLIPConfig() # Create a dummy config file with image_proceesor_type A_ = Path(__snake_case ) / '''preprocessor_config.json''' A_ = Path(__snake_case ) / '''config.json''' json.dump( {'''image_processor_type''': '''CLIPImageProcessor''', '''processor_class''': '''CLIPProcessor'''} , open(__snake_case , '''w''' ) , ) json.dump({'''model_type''': '''clip'''} , open(__snake_case , '''w''' ) ) # remove image_processor_type to make sure config.json alone is enough to load image processor locally A_ = AutoImageProcessor.from_pretrained(__snake_case ).to_dict() config_dict.pop('''image_processor_type''' ) A_ = CLIPImageProcessor(**__snake_case ) # save in new folder model_config.save_pretrained(__snake_case ) config.save_pretrained(__snake_case ) A_ = AutoImageProcessor.from_pretrained(__snake_case ) # make sure private variable is not incorrectly saved A_ = json.loads(config.to_json_string() ) self.assertTrue('''_processor_class''' not in dict_as_saved ) self.assertIsInstance(__snake_case , __snake_case ) def __A ( self ) -> Optional[Any]: with tempfile.TemporaryDirectory() as tmpdirname: A_ = Path(__snake_case ) / '''preprocessor_config.json''' json.dump( {'''image_processor_type''': '''CLIPImageProcessor''', '''processor_class''': '''CLIPProcessor'''} , open(__snake_case , '''w''' ) , ) A_ = AutoImageProcessor.from_pretrained(__snake_case ) self.assertIsInstance(__snake_case , __snake_case ) def __A ( self ) -> Dict: with self.assertRaisesRegex( __snake_case , '''clip-base is not a local folder and is not a valid model identifier''' ): A_ = AutoImageProcessor.from_pretrained('''clip-base''' ) def __A ( self ) -> int: with self.assertRaisesRegex( __snake_case , R'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)''' ): A_ = AutoImageProcessor.from_pretrained(__snake_case , revision='''aaaaaa''' ) def __A ( self ) -> Union[str, Any]: with self.assertRaisesRegex( __snake_case , '''hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json.''' , ): A_ = AutoImageProcessor.from_pretrained('''hf-internal-testing/config-no-model''' ) def __A ( self ) -> List[str]: # If remote code is not set, we will time out when asking whether to load the model. with self.assertRaises(__snake_case ): A_ = AutoImageProcessor.from_pretrained('''hf-internal-testing/test_dynamic_image_processor''' ) # If remote code is disabled, we can't load this config. with self.assertRaises(__snake_case ): A_ = AutoImageProcessor.from_pretrained( '''hf-internal-testing/test_dynamic_image_processor''' , trust_remote_code=__snake_case ) A_ = AutoImageProcessor.from_pretrained( '''hf-internal-testing/test_dynamic_image_processor''' , trust_remote_code=__snake_case ) self.assertEqual(image_processor.__class__.__name__ , '''NewImageProcessor''' ) # Test image processor can be reloaded. with tempfile.TemporaryDirectory() as tmp_dir: image_processor.save_pretrained(__snake_case ) A_ = AutoImageProcessor.from_pretrained(__snake_case , trust_remote_code=__snake_case ) self.assertEqual(reloaded_image_processor.__class__.__name__ , '''NewImageProcessor''' ) def __A ( self ) -> Dict: try: AutoConfig.register('''custom''' , __snake_case ) AutoImageProcessor.register(__snake_case , __snake_case ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(__snake_case ): AutoImageProcessor.register(__snake_case , __snake_case ) with tempfile.TemporaryDirectory() as tmpdirname: A_ = Path(__snake_case ) / '''preprocessor_config.json''' A_ = Path(__snake_case ) / '''config.json''' json.dump( {'''feature_extractor_type''': '''CLIPFeatureExtractor''', '''processor_class''': '''CLIPProcessor'''} , open(__snake_case , '''w''' ) , ) json.dump({'''model_type''': '''clip'''} , open(__snake_case , '''w''' ) ) A_ = CustomImageProcessor.from_pretrained(__snake_case ) # Now that the config is registered, it can be used as any other config with the auto-API with tempfile.TemporaryDirectory() as tmp_dir: image_processor.save_pretrained(__snake_case ) A_ = AutoImageProcessor.from_pretrained(__snake_case ) self.assertIsInstance(__snake_case , __snake_case ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in IMAGE_PROCESSOR_MAPPING._extra_content: del IMAGE_PROCESSOR_MAPPING._extra_content[CustomConfig] def __A ( self ) -> Optional[int]: class __UpperCAmelCase ( A__ ): '''simple docstring''' __lowercase : Tuple = True try: AutoConfig.register('''custom''' , __snake_case ) AutoImageProcessor.register(__snake_case , __snake_case ) # If remote code is not set, the default is to use local A_ = AutoImageProcessor.from_pretrained('''hf-internal-testing/test_dynamic_image_processor''' ) self.assertEqual(image_processor.__class__.__name__ , '''NewImageProcessor''' ) self.assertTrue(image_processor.is_local ) # If remote code is disabled, we load the local one. A_ = AutoImageProcessor.from_pretrained( '''hf-internal-testing/test_dynamic_image_processor''' , trust_remote_code=__snake_case ) self.assertEqual(image_processor.__class__.__name__ , '''NewImageProcessor''' ) self.assertTrue(image_processor.is_local ) # If remote is enabled, we load from the Hub A_ = AutoImageProcessor.from_pretrained( '''hf-internal-testing/test_dynamic_image_processor''' , trust_remote_code=__snake_case ) self.assertEqual(image_processor.__class__.__name__ , '''NewImageProcessor''' ) self.assertTrue(not hasattr(__snake_case , '''is_local''' ) ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in IMAGE_PROCESSOR_MAPPING._extra_content: del IMAGE_PROCESSOR_MAPPING._extra_content[CustomConfig]
353
'''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 __UpperCAmelCase : '''simple docstring''' def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=2 , _SCREAMING_SNAKE_CASE=32 , _SCREAMING_SNAKE_CASE=16 , _SCREAMING_SNAKE_CASE=3 , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=32 , _SCREAMING_SNAKE_CASE=4 , _SCREAMING_SNAKE_CASE=[0, 1, 2, 3] , _SCREAMING_SNAKE_CASE=4 , _SCREAMING_SNAKE_CASE=37 , _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, 384, 24, 24] , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=None , ) -> Tuple: 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 __A ( self ) -> Optional[Any]: 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 __A ( self ) -> Optional[Any]: A_ = { '''global_padding''': '''same''', '''layer_type''': '''bottleneck''', '''depths''': [3, 4, 9], '''out_features''': ['''stage1''', '''stage2''', '''stage3'''], '''embedding_dynamic_padding''': True, '''hidden_sizes''': [96, 192, 384, 768], '''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 __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Optional[Any]: 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 __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> 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 __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Union[str, 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 __A ( self ) -> Optional[int]: A_ = self.prepare_config_and_inputs() A_ ,A_ ,A_ = config_and_inputs A_ = {'''pixel_values''': pixel_values} return config, inputs_dict @require_torch class __UpperCAmelCase ( _UpperCamelCase , _UpperCamelCase , unittest.TestCase ): '''simple docstring''' __lowercase : Optional[int] = (DPTModel, DPTForDepthEstimation, DPTForSemanticSegmentation) if is_torch_available() else () __lowercase : Optional[int] = ( { 'depth-estimation': DPTForDepthEstimation, 'feature-extraction': DPTModel, 'image-segmentation': DPTForSemanticSegmentation, } if is_torch_available() else {} ) __lowercase : Any = False __lowercase : Tuple = False __lowercase : List[Any] = False def __A ( self ) -> Tuple: A_ = DPTModelTester(self ) A_ = ConfigTester(self , config_class=_SCREAMING_SNAKE_CASE , has_text_modality=_SCREAMING_SNAKE_CASE , hidden_size=37 ) def __A ( self ) -> Dict: self.config_tester.run_common_tests() @unittest.skip(reason='''DPT does not use inputs_embeds''' ) def __A ( self ) -> Union[str, Any]: pass def __A ( self ) -> Dict: A_ ,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 __A ( self ) -> Optional[int]: A_ ,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 __A ( self ) -> str: A_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE ) def __A ( self ) -> str: A_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_depth_estimation(*_SCREAMING_SNAKE_CASE ) def __A ( self ) -> Optional[Any]: A_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_semantic_segmentation(*_SCREAMING_SNAKE_CASE ) def __A ( self ) -> Any: for model_class in self.all_model_classes: if model_class.__name__ == "DPTForDepthEstimation": continue A_ ,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 __A ( self ) -> Any: for model_class in self.all_model_classes: if model_class.__name__ == "DPTForDepthEstimation": continue A_ ,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 __A ( self ) -> Tuple: A_ ,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 __A ( self ) -> int: pass @slow def __A ( self ) -> Dict: for model_name in DPT_PRETRAINED_MODEL_ARCHIVE_LIST[1:]: A_ = DPTModel.from_pretrained(_SCREAMING_SNAKE_CASE ) self.assertIsNotNone(_SCREAMING_SNAKE_CASE ) def __A ( self ) -> Optional[int]: # We do this test only for DPTForDepthEstimation since it is the only model that uses readout_type A_ ,A_ = self.model_tester.prepare_config_and_inputs_for_common() A_ = '''add''' with self.assertRaises(_SCREAMING_SNAKE_CASE ): A_ = DPTForDepthEstimation(_SCREAMING_SNAKE_CASE ) def _UpperCAmelCase ( ) -> Optional[int]: A_ = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_torch @require_vision @slow class __UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' def __A ( self ) -> Any: 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, 384, 384) ) self.assertEqual(predicted_depth.shape , _SCREAMING_SNAKE_CASE ) A_ = torch.tensor( [[[5.6_437, 5.6_146, 5.6_511], [5.4_371, 5.5_649, 5.5_958], [5.5_215, 5.5_184, 5.5_293]]] ).to(_SCREAMING_SNAKE_CASE ) self.assertTrue(torch.allclose(outputs.predicted_depth[:3, :3, :3] / 100 , _SCREAMING_SNAKE_CASE , atol=1E-4 ) )
18
0
import itertools import random import unittest import numpy as np from transformers import WAV_2_VEC_2_PRETRAINED_MODEL_ARCHIVE_LIST, WavaVecaConfig, WavaVecaFeatureExtractor from transformers.testing_utils import require_torch, slow from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin __snake_case : Optional[int] = random.Random() def _UpperCAmelCase ( _UpperCamelCase : str, _UpperCamelCase : Optional[Any]=1.0, _UpperCamelCase : Optional[Any]=None, _UpperCamelCase : List[str]=None ) -> Optional[Any]: if rng is None: A_ = global_rng A_ = [] for batch_idx in range(shape[0] ): values.append([] ) for _ in range(shape[1] ): values[-1].append(rng.random() * scale ) return values class __UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=7 , _SCREAMING_SNAKE_CASE=400 , _SCREAMING_SNAKE_CASE=2000 , _SCREAMING_SNAKE_CASE=1 , _SCREAMING_SNAKE_CASE=0.0 , _SCREAMING_SNAKE_CASE=1_6000 , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=True , ) -> Tuple: A_ = parent A_ = batch_size A_ = min_seq_length A_ = max_seq_length A_ = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) A_ = feature_size A_ = padding_value A_ = sampling_rate A_ = return_attention_mask A_ = do_normalize def __A ( self ) -> Any: return { "feature_size": self.feature_size, "padding_value": self.padding_value, "sampling_rate": self.sampling_rate, "return_attention_mask": self.return_attention_mask, "do_normalize": self.do_normalize, } def __A ( self , _SCREAMING_SNAKE_CASE=False , _SCREAMING_SNAKE_CASE=False ) -> str: def _flatten(_SCREAMING_SNAKE_CASE ): return list(itertools.chain(*_lowerCamelCase ) ) if equal_length: A_ = floats_list((self.batch_size, self.max_seq_length) ) else: # make sure that inputs increase in size A_ = [ _flatten(floats_list((x, self.feature_size) ) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: A_ = [np.asarray(_lowerCamelCase ) for x in speech_inputs] return speech_inputs class __UpperCAmelCase ( a__ , unittest.TestCase ): '''simple docstring''' __lowercase : Dict = WavaVecaFeatureExtractor def __A ( self ) -> Union[str, Any]: A_ = WavaVecaFeatureExtractionTester(self ) def __A ( self , _SCREAMING_SNAKE_CASE ) -> str: self.assertTrue(np.all(np.mean(_lowerCamelCase , axis=0 ) < 1E-3 ) ) self.assertTrue(np.all(np.abs(np.var(_lowerCamelCase , axis=0 ) - 1 ) < 1E-3 ) ) def __A ( self ) -> Any: A_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 A_ = [floats_list((1, x) )[0] for x in range(800 , 1400 , 200 )] A_ = [np.asarray(_lowerCamelCase ) for speech_input in speech_inputs] # Test not batched input A_ = feat_extract(speech_inputs[0] , return_tensors='''np''' ).input_values A_ = feat_extract(np_speech_inputs[0] , return_tensors='''np''' ).input_values self.assertTrue(np.allclose(_lowerCamelCase , _lowerCamelCase , atol=1E-3 ) ) # Test batched A_ = feat_extract(_lowerCamelCase , return_tensors='''np''' ).input_values A_ = feat_extract(_lowerCamelCase , return_tensors='''np''' ).input_values for enc_seq_a, enc_seq_a in zip(_lowerCamelCase , _lowerCamelCase ): self.assertTrue(np.allclose(_lowerCamelCase , _lowerCamelCase , atol=1E-3 ) ) # Test 2-D numpy arrays are batched. A_ = [floats_list((1, x) )[0] for x in (800, 800, 800)] A_ = np.asarray(_lowerCamelCase ) A_ = feat_extract(_lowerCamelCase , return_tensors='''np''' ).input_values A_ = feat_extract(_lowerCamelCase , return_tensors='''np''' ).input_values for enc_seq_a, enc_seq_a in zip(_lowerCamelCase , _lowerCamelCase ): self.assertTrue(np.allclose(_lowerCamelCase , _lowerCamelCase , atol=1E-3 ) ) def __A ( self ) -> Any: A_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) A_ = [floats_list((1, x) )[0] for x in range(800 , 1400 , 200 )] A_ = ['''longest''', '''max_length''', '''do_not_pad'''] A_ = [None, 1600, None] for max_length, padding in zip(_lowerCamelCase , _lowerCamelCase ): A_ = feat_extract(_lowerCamelCase , padding=_lowerCamelCase , max_length=_lowerCamelCase , return_tensors='''np''' ) A_ = processed.input_values self._check_zero_mean_unit_variance(input_values[0][:800] ) self.assertTrue(input_values[0][800:].sum() < 1E-6 ) self._check_zero_mean_unit_variance(input_values[1][:1000] ) self.assertTrue(input_values[0][1000:].sum() < 1E-6 ) self._check_zero_mean_unit_variance(input_values[2][:1200] ) def __A ( self ) -> Tuple: A_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) A_ = range(800 , 1400 , 200 ) A_ = [floats_list((1, x) )[0] for x in lengths] A_ = ['''longest''', '''max_length''', '''do_not_pad'''] A_ = [None, 1600, None] for max_length, padding in zip(_lowerCamelCase , _lowerCamelCase ): A_ = feat_extract(_lowerCamelCase , max_length=_lowerCamelCase , padding=_lowerCamelCase ) A_ = processed.input_values self._check_zero_mean_unit_variance(input_values[0][:800] ) self._check_zero_mean_unit_variance(input_values[1][:1000] ) self._check_zero_mean_unit_variance(input_values[2][:1200] ) def __A ( self ) -> Any: A_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) A_ = [floats_list((1, x) )[0] for x in range(800 , 1400 , 200 )] A_ = feat_extract( _lowerCamelCase , truncation=_lowerCamelCase , max_length=1000 , padding='''max_length''' , return_tensors='''np''' ) A_ = processed.input_values self._check_zero_mean_unit_variance(input_values[0, :800] ) self._check_zero_mean_unit_variance(input_values[1] ) self._check_zero_mean_unit_variance(input_values[2] ) def __A ( self ) -> str: A_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) A_ = [floats_list((1, x) )[0] for x in range(800 , 1400 , 200 )] A_ = feat_extract( _lowerCamelCase , truncation=_lowerCamelCase , max_length=1000 , padding='''longest''' , return_tensors='''np''' ) A_ = processed.input_values self._check_zero_mean_unit_variance(input_values[0, :800] ) self._check_zero_mean_unit_variance(input_values[1, :1000] ) self._check_zero_mean_unit_variance(input_values[2] ) # make sure that if max_length < longest -> then pad to max_length self.assertTrue(input_values.shape == (3, 1000) ) A_ = [floats_list((1, x) )[0] for x in range(800 , 1400 , 200 )] A_ = feat_extract( _lowerCamelCase , truncation=_lowerCamelCase , max_length=2000 , padding='''longest''' , return_tensors='''np''' ) A_ = processed.input_values self._check_zero_mean_unit_variance(input_values[0, :800] ) self._check_zero_mean_unit_variance(input_values[1, :1000] ) self._check_zero_mean_unit_variance(input_values[2] ) # make sure that if max_length > longest -> then pad to longest self.assertTrue(input_values.shape == (3, 1200) ) @require_torch def __A ( self ) -> int: import torch A_ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) A_ = np.random.rand(100 ).astype(np.floataa ) A_ = np_speech_inputs.tolist() for inputs in [py_speech_inputs, np_speech_inputs]: A_ = feature_extractor.pad([{'''input_values''': inputs}] , return_tensors='''np''' ) self.assertTrue(np_processed.input_values.dtype == np.floataa ) A_ = feature_extractor.pad([{'''input_values''': inputs}] , return_tensors='''pt''' ) self.assertTrue(pt_processed.input_values.dtype == torch.floataa ) @slow @require_torch def __A ( self ) -> str: for model_id in WAV_2_VEC_2_PRETRAINED_MODEL_ARCHIVE_LIST: A_ = WavaVecaConfig.from_pretrained(_lowerCamelCase ) A_ = WavaVecaFeatureExtractor.from_pretrained(_lowerCamelCase ) # only "layer" feature extraction norm should make use of # attention_mask self.assertEqual(feat_extract.return_attention_mask , config.feat_extract_norm == '''layer''' )
354
'''simple docstring''' import math def _UpperCAmelCase ( _UpperCamelCase : float, _UpperCamelCase : float ) -> float: if initial_intensity < 0: raise ValueError('''The value of intensity cannot be negative''' ) # handling of negative values of initial intensity if angle < 0 or angle > 3_60: raise ValueError('''In Malus Law, the angle is in the range 0-360 degrees''' ) # handling of values out of allowed range return initial_intensity * (math.cos(math.radians(_UpperCamelCase ) ) ** 2) if __name__ == "__main__": import doctest doctest.testmod(name='malus_law')
18
0
import warnings from ...utils import logging from .image_processing_flava import FlavaImageProcessor __snake_case : List[str] = logging.get_logger(__name__) class __UpperCAmelCase ( lowercase__ ): '''simple docstring''' def __init__( self , *_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) -> str: warnings.warn( '''The class FlavaFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please''' ''' use FlavaImageProcessor instead.''' , _a , ) super().__init__(*_a , **_a )
355
'''simple docstring''' from ...utils import ( OptionalDependencyNotAvailable, is_torch_available, is_transformers_available, is_transformers_version, ) try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import ShapEPipeline else: from .camera import create_pan_cameras from .pipeline_shap_e import ShapEPipeline from .pipeline_shap_e_img2img import ShapEImgaImgPipeline from .renderer import ( BoundingBoxVolume, ImportanceRaySampler, MLPNeRFModelOutput, MLPNeRSTFModel, ShapEParamsProjModel, ShapERenderer, StratifiedRaySampler, VoidNeRFModel, )
18
0
'''simple docstring''' class __UpperCAmelCase : '''simple docstring''' def __init__( self , _SCREAMING_SNAKE_CASE = "" , _SCREAMING_SNAKE_CASE = False ) -> Optional[int]: A_ = {} # A node will be a leaf if the tree contains its word A_ = is_leaf A_ = prefix def __A ( self , _SCREAMING_SNAKE_CASE ) -> Any: A_ = 0 for q, w in zip(self.prefix , _SCREAMING_SNAKE_CASE ): if q != w: break x += 1 return self.prefix[:x], self.prefix[x:], word[x:] def __A ( self , _SCREAMING_SNAKE_CASE ) -> Tuple: for word in words: self.insert(_SCREAMING_SNAKE_CASE ) def __A ( self , _SCREAMING_SNAKE_CASE ) -> Any: if self.prefix == word: A_ = True # Case 2: The node has no edges that have a prefix to the word # Solution: We create an edge from the current node to a new one # containing the word elif word[0] not in self.nodes: A_ = RadixNode(prefix=_SCREAMING_SNAKE_CASE , is_leaf=_SCREAMING_SNAKE_CASE ) else: A_ = self.nodes[word[0]] A_ ,A_ ,A_ = incoming_node.match( _SCREAMING_SNAKE_CASE ) # Case 3: The node prefix is equal to the matching # Solution: We insert remaining word on the next node if remaining_prefix == "": self.nodes[matching_string[0]].insert(_SCREAMING_SNAKE_CASE ) # Case 4: The word is greater equal to the matching # Solution: Create a node in between both nodes, change # prefixes and add the new node for the remaining word else: A_ = remaining_prefix A_ = self.nodes[matching_string[0]] A_ = RadixNode(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) A_ = aux_node if remaining_word == "": A_ = True else: self.nodes[matching_string[0]].insert(_SCREAMING_SNAKE_CASE ) def __A ( self , _SCREAMING_SNAKE_CASE ) -> Any: A_ = self.nodes.get(word[0] , _SCREAMING_SNAKE_CASE ) if not incoming_node: return False else: A_ ,A_ ,A_ = incoming_node.match( _SCREAMING_SNAKE_CASE ) # If there is remaining prefix, the word can't be on the tree if remaining_prefix != "": return False # This applies when the word and the prefix are equal elif remaining_word == "": return incoming_node.is_leaf # We have word remaining so we check the next node else: return incoming_node.find(_SCREAMING_SNAKE_CASE ) def __A ( self , _SCREAMING_SNAKE_CASE ) -> Tuple: A_ = self.nodes.get(word[0] , _SCREAMING_SNAKE_CASE ) if not incoming_node: return False else: A_ ,A_ ,A_ = incoming_node.match( _SCREAMING_SNAKE_CASE ) # If there is remaining prefix, the word can't be on the tree if remaining_prefix != "": return False # We have word remaining so we check the next node elif remaining_word != "": return incoming_node.delete(_SCREAMING_SNAKE_CASE ) else: # If it is not a leaf, we don't have to delete if not incoming_node.is_leaf: return False else: # We delete the nodes if no edges go from it if len(incoming_node.nodes ) == 0: del self.nodes[word[0]] # We merge the current node with its only child if len(self.nodes ) == 1 and not self.is_leaf: A_ = list(self.nodes.values() )[0] A_ = merging_node.is_leaf self.prefix += merging_node.prefix A_ = merging_node.nodes # If there is more than 1 edge, we just mark it as non-leaf elif len(incoming_node.nodes ) > 1: A_ = False # If there is 1 edge, we merge it with its child else: A_ = list(incoming_node.nodes.values() )[0] A_ = merging_node.is_leaf incoming_node.prefix += merging_node.prefix A_ = merging_node.nodes return True def __A ( self , _SCREAMING_SNAKE_CASE = 0 ) -> Dict: if self.prefix != "": print('''-''' * height , self.prefix , ''' (leaf)''' if self.is_leaf else '''''' ) for value in self.nodes.values(): value.print_tree(height + 1 ) def _UpperCAmelCase ( ) -> bool: A_ = '''banana bananas bandana band apple all beast'''.split() A_ = RadixNode() root.insert_many(__snake_case ) assert all(root.find(__snake_case ) for word in words ) assert not root.find('''bandanas''' ) assert not root.find('''apps''' ) root.delete('''all''' ) assert not root.find('''all''' ) root.delete('''banana''' ) assert not root.find('''banana''' ) assert root.find('''bananas''' ) return True def _UpperCAmelCase ( ) -> None: assert test_trie() def _UpperCAmelCase ( ) -> None: A_ = RadixNode() A_ = '''banana bananas bandanas bandana band apple all beast'''.split() root.insert_many(__snake_case ) print('''Words:''', __snake_case ) print('''Tree:''' ) root.print_tree() if __name__ == "__main__": main()
356
'''simple docstring''' import argparse from pathlib import Path import torch from transformers import OPTConfig, OPTModel from transformers.utils import logging logging.set_verbosity_info() __snake_case : Any = logging.get_logger(__name__) def _UpperCAmelCase ( _UpperCamelCase : Dict ) -> List[str]: A_ = torch.load(_UpperCamelCase, map_location='''cpu''' ) if "model" in sd.keys(): A_ = torch.load(_UpperCamelCase, map_location='''cpu''' )['''model'''] # pop unnecessary weights A_ = [ '''decoder.version''', '''decoder.output_projection.weight''', ] for key in keys_to_delete: if key in sd: sd.pop(_UpperCamelCase ) A_ = { '''decoder.project_in_dim.weight''': '''decoder.project_in.weight''', '''decoder.project_out_dim.weight''': '''decoder.project_out.weight''', '''decoder.layer_norm.weight''': '''decoder.final_layer_norm.weight''', '''decoder.layer_norm.bias''': '''decoder.final_layer_norm.bias''', } for old_key, new_key in keys_to_rename.items(): if old_key in sd: A_ = sd.pop(_UpperCamelCase ) A_ = list(sd.keys() ) for key in keys: if ".qkv_proj." in key: A_ = sd[key] # We split QKV in separate Q,K,V A_ = key.replace('''.qkv_proj.''', '''.q_proj.''' ) A_ = key.replace('''.qkv_proj.''', '''.k_proj.''' ) A_ = key.replace('''.qkv_proj.''', '''.v_proj.''' ) A_ = value.shape[0] assert depth % 3 == 0 # `SequeuceParallelTransformerBlock` has QKV weight is separated in K,V,Q despite the naming: # https://cs.github.com/facebookresearch/metaseq/blob/51871bd73cd04c038f239ea2a26db1d7f6b37927/metaseq/modules/sequence_parallel_transformer_layer.py#L97 A_ ,A_ ,A_ = torch.split(_UpperCamelCase, depth // 3, dim=0 ) A_ = q A_ = k A_ = v del sd[key] return sd @torch.no_grad() def _UpperCAmelCase ( _UpperCamelCase : Optional[int], _UpperCamelCase : Optional[Any], _UpperCamelCase : List[str]=None ) -> Dict: A_ = load_checkpoint(_UpperCamelCase ) if config is not None: A_ = OPTConfig.from_pretrained(_UpperCamelCase ) else: A_ = OPTConfig() A_ = OPTModel(_UpperCamelCase ).half().eval() model.load_state_dict(_UpperCamelCase ) # Check results Path(_UpperCamelCase ).mkdir(exist_ok=_UpperCamelCase ) model.save_pretrained(_UpperCamelCase ) if __name__ == "__main__": __snake_case : Optional[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '--fairseq_path', type=str, help=( 'path to fairseq checkpoint in correct format. You can find all checkpoints in the correct format here:' ' https://huggingface.co/models?other=opt_metasq' ), ) parser.add_argument('--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.') parser.add_argument('--hf_config', default=None, type=str, help='Define HF config.') __snake_case : Optional[Any] = parser.parse_args() convert_opt_checkpoint(args.fairseq_path, args.pytorch_dump_folder_path, config=args.hf_config)
18
0
'''simple docstring''' import multiprocessing import os from typing import BinaryIO, Optional, Union import fsspec from .. import Dataset, Features, NamedSplit, config from ..formatting import query_table from ..packaged_modules.json.json import Json from ..utils import logging from ..utils.typing import NestedDataStructureLike, PathLike from .abc import AbstractDatasetReader class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = False , _SCREAMING_SNAKE_CASE = False , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , **_SCREAMING_SNAKE_CASE , ) -> Optional[Any]: super().__init__( _snake_case , split=_snake_case , features=_snake_case , cache_dir=_snake_case , keep_in_memory=_snake_case , streaming=_snake_case , num_proc=_snake_case , **_snake_case , ) A_ = field A_ = path_or_paths if isinstance(_snake_case , _snake_case ) else {self.split: path_or_paths} A_ = Json( cache_dir=_snake_case , data_files=_snake_case , features=_snake_case , field=_snake_case , **_snake_case , ) def __A ( self ) -> Any: if self.streaming: A_ = self.builder.as_streaming_dataset(split=self.split ) # Build regular (map-style) dataset else: A_ = None A_ = None A_ = None A_ = None self.builder.download_and_prepare( download_config=_snake_case , download_mode=_snake_case , verification_mode=_snake_case , base_path=_snake_case , num_proc=self.num_proc , ) A_ = self.builder.as_dataset( split=self.split , verification_mode=_snake_case , in_memory=self.keep_in_memory ) return dataset class __UpperCAmelCase : '''simple docstring''' def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , **_SCREAMING_SNAKE_CASE , ) -> str: if num_proc is not None and num_proc <= 0: raise ValueError(F'''num_proc {num_proc} must be an integer > 0.''' ) A_ = dataset A_ = path_or_buf A_ = batch_size if batch_size else config.DEFAULT_MAX_BATCH_SIZE A_ = num_proc A_ = '''utf-8''' A_ = to_json_kwargs def __A ( self ) -> Optional[int]: A_ = self.to_json_kwargs.pop('''path_or_buf''' , _snake_case ) A_ = self.to_json_kwargs.pop('''orient''' , '''records''' ) A_ = self.to_json_kwargs.pop('''lines''' , True if orient == '''records''' else False ) A_ = self.to_json_kwargs.pop('''index''' , False if orient in ['''split''', '''table'''] else True ) A_ = self.to_json_kwargs.pop('''compression''' , _snake_case ) if compression not in [None, "infer", "gzip", "bz2", "xz"]: raise NotImplementedError(F'''`datasets` currently does not support {compression} compression''' ) if isinstance(self.path_or_buf , (str, bytes, os.PathLike) ): with fsspec.open(self.path_or_buf , '''wb''' , compression=_snake_case ) as buffer: A_ = self._write(file_obj=_snake_case , orient=_snake_case , lines=_snake_case , index=_snake_case , **self.to_json_kwargs ) else: if compression: raise NotImplementedError( F'''The compression parameter is not supported when writing to a buffer, but compression={compression}''' ''' was passed. Please provide a local path instead.''' ) A_ = self._write( file_obj=self.path_or_buf , orient=_snake_case , lines=_snake_case , index=_snake_case , **self.to_json_kwargs ) return written def __A ( self , _SCREAMING_SNAKE_CASE ) -> Any: A_ ,A_ ,A_ ,A_ ,A_ = args A_ = query_table( table=self.dataset.data , key=slice(_snake_case , offset + self.batch_size ) , indices=self.dataset._indices , ) A_ = batch.to_pandas().to_json( path_or_buf=_snake_case , orient=_snake_case , lines=_snake_case , index=_snake_case , **_snake_case ) if not json_str.endswith('''\n''' ): json_str += "\n" return json_str.encode(self.encoding ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE , ) -> str: A_ = 0 if self.num_proc is None or self.num_proc == 1: for offset in logging.tqdm( range(0 , len(self.dataset ) , self.batch_size ) , unit='''ba''' , disable=not logging.is_progress_bar_enabled() , desc='''Creating json from Arrow format''' , ): A_ = self._batch_json((offset, orient, lines, index, to_json_kwargs) ) written += file_obj.write(_snake_case ) else: A_ ,A_ = len(self.dataset ), self.batch_size with multiprocessing.Pool(self.num_proc ) as pool: for json_str in logging.tqdm( pool.imap( self._batch_json , [(offset, orient, lines, index, to_json_kwargs) for offset in range(0 , _snake_case , _snake_case )] , ) , total=(num_rows // batch_size) + 1 if num_rows % batch_size else num_rows // batch_size , unit='''ba''' , disable=not logging.is_progress_bar_enabled() , desc='''Creating json from Arrow format''' , ): written += file_obj.write(_snake_case ) return written
357
'''simple docstring''' from typing import List, Optional, Tuple from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_herbert import HerbertTokenizer __snake_case : Optional[Any] = logging.get_logger(__name__) __snake_case : Tuple = {'vocab_file': 'vocab.json', 'merges_file': 'merges.txt', 'tokenizer_file': 'tokenizer.json'} __snake_case : Optional[Any] = { 'vocab_file': { 'allegro/herbert-base-cased': 'https://huggingface.co/allegro/herbert-base-cased/resolve/main/vocab.json' }, 'merges_file': { 'allegro/herbert-base-cased': 'https://huggingface.co/allegro/herbert-base-cased/resolve/main/merges.txt' }, } __snake_case : Tuple = {'allegro/herbert-base-cased': 514} __snake_case : List[str] = {} class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' __lowercase : Any = VOCAB_FILES_NAMES __lowercase : Union[str, Any] = PRETRAINED_VOCAB_FILES_MAP __lowercase : Dict = PRETRAINED_INIT_CONFIGURATION __lowercase : List[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowercase : Optional[int] = HerbertTokenizer def __init__( self , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE="<s>" , _SCREAMING_SNAKE_CASE="<unk>" , _SCREAMING_SNAKE_CASE="<pad>" , _SCREAMING_SNAKE_CASE="<mask>" , _SCREAMING_SNAKE_CASE="</s>" , **_SCREAMING_SNAKE_CASE , ) -> int: super().__init__( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , tokenizer_file=_SCREAMING_SNAKE_CASE , cls_token=_SCREAMING_SNAKE_CASE , unk_token=_SCREAMING_SNAKE_CASE , pad_token=_SCREAMING_SNAKE_CASE , mask_token=_SCREAMING_SNAKE_CASE , sep_token=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE , ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None ) -> List[int]: A_ = [self.cls_token_id] A_ = [self.sep_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 , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = False ) -> List[int]: 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] + ([0] * len(_SCREAMING_SNAKE_CASE )) + [1] def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None ) -> List[int]: 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 ) * [0] + len(token_ids_a + sep ) * [1] def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None ) -> Tuple[str]: A_ = self._tokenizer.model.save(_SCREAMING_SNAKE_CASE , name=_SCREAMING_SNAKE_CASE ) return tuple(_SCREAMING_SNAKE_CASE )
18
0
'''simple docstring''' import warnings from contextlib import contextmanager from ...processing_utils import ProcessorMixin from .feature_extraction_wavaveca import WavaVecaFeatureExtractor from .tokenization_wavaveca import WavaVecaCTCTokenizer class __UpperCAmelCase ( lowercase__ ): '''simple docstring''' __lowercase : List[str] = 'Wav2Vec2FeatureExtractor' __lowercase : Union[str, Any] = 'AutoTokenizer' def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Any: super().__init__(_UpperCamelCase , _UpperCamelCase ) A_ = self.feature_extractor A_ = False @classmethod def __A ( cls , _SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) -> Dict: try: return super().from_pretrained(_UpperCamelCase , **_UpperCamelCase ) except OSError: warnings.warn( F'''Loading a tokenizer inside {cls.__name__} from a config that does not''' ''' include a `tokenizer_class` attribute is deprecated and will be ''' '''removed in v5. Please add `\'tokenizer_class\': \'Wav2Vec2CTCTokenizer\'`''' ''' attribute to either your `config.json` or `tokenizer_config.json` ''' '''file to suppress this warning: ''' , _UpperCamelCase , ) A_ = WavaVecaFeatureExtractor.from_pretrained(_UpperCamelCase , **_UpperCamelCase ) A_ = WavaVecaCTCTokenizer.from_pretrained(_UpperCamelCase , **_UpperCamelCase ) return cls(feature_extractor=_UpperCamelCase , tokenizer=_UpperCamelCase ) def __call__( self , *_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) -> Tuple: # For backward compatibility if self._in_target_context_manager: return self.current_processor(*_UpperCamelCase , **_UpperCamelCase ) if "raw_speech" in kwargs: warnings.warn('''Using `raw_speech` as a keyword argument is deprecated. Use `audio` instead.''' ) A_ = kwargs.pop('''raw_speech''' ) else: A_ = kwargs.pop('''audio''' , _UpperCamelCase ) A_ = kwargs.pop('''sampling_rate''' , _UpperCamelCase ) A_ = kwargs.pop('''text''' , _UpperCamelCase ) if len(_UpperCamelCase ) > 0: A_ = args[0] A_ = args[1:] if audio is None and text is None: raise ValueError('''You need to specify either an `audio` or `text` input to process.''' ) if audio is not None: A_ = self.feature_extractor(_UpperCamelCase , *_UpperCamelCase , sampling_rate=_UpperCamelCase , **_UpperCamelCase ) if text is not None: A_ = self.tokenizer(_UpperCamelCase , **_UpperCamelCase ) if text is None: return inputs elif audio is None: return encodings else: A_ = encodings["""input_ids"""] return inputs def __A ( self , *_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) -> Optional[int]: # For backward compatibility if self._in_target_context_manager: return self.current_processor.pad(*_UpperCamelCase , **_UpperCamelCase ) A_ = kwargs.pop('''input_features''' , _UpperCamelCase ) A_ = kwargs.pop('''labels''' , _UpperCamelCase ) if len(_UpperCamelCase ) > 0: A_ = args[0] A_ = args[1:] if input_features is not None: A_ = self.feature_extractor.pad(_UpperCamelCase , *_UpperCamelCase , **_UpperCamelCase ) if labels is not None: A_ = self.tokenizer.pad(_UpperCamelCase , **_UpperCamelCase ) if labels is None: return input_features elif input_features is None: return labels else: A_ = labels["""input_ids"""] return input_features def __A ( self , *_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) -> str: return self.tokenizer.batch_decode(*_UpperCamelCase , **_UpperCamelCase ) def __A ( self , *_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) -> str: return self.tokenizer.decode(*_UpperCamelCase , **_UpperCamelCase ) @contextmanager def __A ( self ) -> Tuple: warnings.warn( '''`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your ''' '''labels by using the argument `text` of the regular `__call__` method (either in the same call as ''' '''your audio inputs, or in a separate call.''' ) A_ = True A_ = self.tokenizer yield A_ = self.feature_extractor A_ = False
358
'''simple docstring''' # Copyright 2022 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 import os import platform import numpy as np import psutil import torch from accelerate import __version__ as version from accelerate.commands.config import default_config_file, load_config_from_file from ..utils import is_npu_available, is_xpu_available def _UpperCAmelCase ( _UpperCamelCase : Union[str, Any]=None ) -> List[Any]: if subparsers is not None: A_ = subparsers.add_parser('''env''' ) else: A_ = argparse.ArgumentParser('''Accelerate env command''' ) parser.add_argument( '''--config_file''', default=_UpperCamelCase, help='''The config file to use for the default values in the launching script.''' ) if subparsers is not None: parser.set_defaults(func=_UpperCamelCase ) return parser def _UpperCAmelCase ( _UpperCamelCase : Dict ) -> Dict: A_ = torch.__version__ A_ = torch.cuda.is_available() A_ = is_xpu_available() A_ = is_npu_available() A_ = '''Not found''' # Get the default from the config file. if args.config_file is not None or os.path.isfile(_UpperCamelCase ): A_ = load_config_from_file(args.config_file ).to_dict() A_ = { '''`Accelerate` version''': version, '''Platform''': platform.platform(), '''Python version''': platform.python_version(), '''Numpy version''': np.__version__, '''PyTorch version (GPU?)''': F'''{pt_version} ({pt_cuda_available})''', '''PyTorch XPU available''': str(_UpperCamelCase ), '''PyTorch NPU available''': str(_UpperCamelCase ), '''System RAM''': F'''{psutil.virtual_memory().total / 10_24 ** 3:.2f} GB''', } if pt_cuda_available: A_ = torch.cuda.get_device_name() print('''\nCopy-and-paste the text below in your GitHub issue\n''' ) print('''\n'''.join([F'''- {prop}: {val}''' for prop, val in info.items()] ) ) print('''- `Accelerate` default config:''' if args.config_file is None else '''- `Accelerate` config passed:''' ) A_ = ( '''\n'''.join([F'''\t- {prop}: {val}''' for prop, val in accelerate_config.items()] ) if isinstance(_UpperCamelCase, _UpperCamelCase ) else F'''\t{accelerate_config}''' ) print(_UpperCamelCase ) A_ = accelerate_config return info def _UpperCAmelCase ( ) -> int: A_ = env_command_parser() A_ = parser.parse_args() env_command(_UpperCamelCase ) return 0 if __name__ == "__main__": raise SystemExit(main())
18
0
'''simple docstring''' import os from typing import List, Optional, Union from ...tokenization_utils import PreTrainedTokenizer from ...tokenization_utils_base import AddedToken from ...utils import logging __snake_case : Tuple = logging.get_logger(__name__) __snake_case : Dict = {"""vocab_file""": """vocab.txt"""} __snake_case : Union[str, Any] = { """vocab_file""": { """facebook/esm2_t6_8M_UR50D""": """https://huggingface.co/facebook/esm2_t6_8M_UR50D/resolve/main/vocab.txt""", """facebook/esm2_t12_35M_UR50D""": """https://huggingface.co/facebook/esm2_t12_35M_UR50D/resolve/main/vocab.txt""", }, } __snake_case : List[str] = { """facebook/esm2_t6_8M_UR50D""": 1_024, """facebook/esm2_t12_35M_UR50D""": 1_024, } def _UpperCAmelCase ( _UpperCamelCase : Tuple ) -> Union[str, Any]: with open(_UpperCamelCase, '''r''' ) as f: A_ = f.read().splitlines() return [l.strip() for l in lines] class __UpperCAmelCase ( __A ): '''simple docstring''' __lowercase : List[Any] = VOCAB_FILES_NAMES __lowercase : List[str] = PRETRAINED_VOCAB_FILES_MAP __lowercase : Any = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowercase : Tuple = ['input_ids', 'attention_mask'] def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE="<unk>" , _SCREAMING_SNAKE_CASE="<cls>" , _SCREAMING_SNAKE_CASE="<pad>" , _SCREAMING_SNAKE_CASE="<mask>" , _SCREAMING_SNAKE_CASE="<eos>" , **_SCREAMING_SNAKE_CASE , ) -> List[Any]: super().__init__(**__lowercase ) A_ = load_vocab_file(__lowercase ) A_ = dict(enumerate(self.all_tokens ) ) A_ = {tok: ind for ind, tok in enumerate(self.all_tokens )} A_ = unk_token A_ = cls_token A_ = pad_token A_ = mask_token A_ = eos_token A_ = self.all_tokens self._create_trie(self.unique_no_split_tokens ) def __A ( self , _SCREAMING_SNAKE_CASE ) -> List[Any]: return self._id_to_token.get(__lowercase , self.unk_token ) def __A ( self , _SCREAMING_SNAKE_CASE ) -> int: return self._token_to_id.get(__lowercase , self._token_to_id.get(self.unk_token ) ) def __A ( self , _SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) -> Any: return text.split() def __A ( self , _SCREAMING_SNAKE_CASE=False ) -> str: return len(self._id_to_token ) def __A ( self ) -> Tuple: return {token: i for i, token in enumerate(self.all_tokens )} def __A ( self , _SCREAMING_SNAKE_CASE ) -> Optional[int]: return self._token_to_id.get(__lowercase , self._token_to_id.get(self.unk_token ) ) def __A ( self , _SCREAMING_SNAKE_CASE ) -> Optional[int]: return self._id_to_token.get(__lowercase , self.unk_token ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None ) -> List[str]: A_ = [self.cls_token_id] A_ = [self.eos_token_id] # No sep token in ESM vocabulary if token_ids_a is None: if self.eos_token_id is None: return cls + token_ids_a else: return cls + token_ids_a + sep elif self.eos_token_id is None: raise ValueError('''Cannot tokenize multiple sequences when EOS token is not set!''' ) return cls + token_ids_a + sep + token_ids_a + sep # Multiple inputs always have an EOS token def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = False ) -> List[str]: 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 token in self.all_special_ids else 0 for token in token_ids_a] A_ = [1] + ([0] * len(__lowercase )) + [1] if token_ids_a is not None: mask += [0] * len(__lowercase ) + [1] return mask def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Any: A_ = os.path.join(__lowercase , (filename_prefix + '''-''' if filename_prefix else '''''') + '''vocab.txt''' ) with open(__lowercase , '''w''' ) as f: f.write('''\n'''.join(self.all_tokens ) ) return (vocab_file,) @property def __A ( self ) -> Dict: return self.get_vocab_size(with_added_tokens=__lowercase ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = False ) -> List[Any]: return super()._add_tokens(__lowercase , special_tokens=__lowercase )
359
'''simple docstring''' import inspect import math import tempfile import unittest import numpy as np from transformers import ViTMAEConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTMAEForPreTraining, ViTMAEModel from transformers.models.vit.modeling_vit import VIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class __UpperCAmelCase : '''simple docstring''' def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=13 , _SCREAMING_SNAKE_CASE=30 , _SCREAMING_SNAKE_CASE=2 , _SCREAMING_SNAKE_CASE=3 , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=32 , _SCREAMING_SNAKE_CASE=5 , _SCREAMING_SNAKE_CASE=4 , _SCREAMING_SNAKE_CASE=37 , _SCREAMING_SNAKE_CASE="gelu" , _SCREAMING_SNAKE_CASE=0.1 , _SCREAMING_SNAKE_CASE=0.1 , _SCREAMING_SNAKE_CASE=10 , _SCREAMING_SNAKE_CASE=0.02 , _SCREAMING_SNAKE_CASE=3 , _SCREAMING_SNAKE_CASE=0.6 , _SCREAMING_SNAKE_CASE=None , ) -> Tuple: 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_ = num_attention_heads A_ = intermediate_size A_ = hidden_act A_ = hidden_dropout_prob A_ = attention_probs_dropout_prob A_ = type_sequence_label_size A_ = initializer_range A_ = mask_ratio A_ = scope # in ViTMAE, the expected sequence length = (num_patches + 1) * (1 - config.mask_ratio), rounded above # (we add 1 for the [CLS] token) A_ = (image_size // patch_size) ** 2 A_ = int(math.ceil((1 - mask_ratio) * (num_patches + 1) ) ) def __A ( self ) -> Union[str, Any]: 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.type_sequence_label_size ) A_ = self.get_config() return config, pixel_values, labels def __A ( self ) -> Dict: return ViTMAEConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=_SCREAMING_SNAKE_CASE , initializer_range=self.initializer_range , mask_ratio=self.mask_ratio , ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Tuple: A_ = ViTMAEModel(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 __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Optional[Any]: A_ = ViTMAEForPreTraining(_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() A_ = model(_SCREAMING_SNAKE_CASE ) A_ = (self.image_size // self.patch_size) ** 2 A_ = self.patch_size**2 * self.num_channels self.parent.assertEqual(result.logits.shape , (self.batch_size, num_patches, expected_num_channels) ) # test greyscale images A_ = 1 A_ = ViTMAEForPreTraining(_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() A_ = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) A_ = model(_SCREAMING_SNAKE_CASE ) A_ = self.patch_size**2 self.parent.assertEqual(result.logits.shape , (self.batch_size, num_patches, expected_num_channels) ) def __A ( self ) -> int: A_ = self.prepare_config_and_inputs() A_ ,A_ ,A_ = config_and_inputs A_ = {'''pixel_values''': pixel_values} return config, inputs_dict @require_torch class __UpperCAmelCase ( _UpperCamelCase , _UpperCamelCase , unittest.TestCase ): '''simple docstring''' __lowercase : int = (ViTMAEModel, ViTMAEForPreTraining) if is_torch_available() else () __lowercase : List[Any] = {'feature-extraction': ViTMAEModel} if is_torch_available() else {} __lowercase : Union[str, Any] = False __lowercase : List[Any] = False __lowercase : List[str] = False __lowercase : List[str] = False def __A ( self ) -> Any: A_ = ViTMAEModelTester(self ) A_ = ConfigTester(self , config_class=_SCREAMING_SNAKE_CASE , has_text_modality=_SCREAMING_SNAKE_CASE , hidden_size=37 ) def __A ( self ) -> Optional[int]: self.config_tester.run_common_tests() @unittest.skip(reason='''ViTMAE does not use inputs_embeds''' ) def __A ( self ) -> int: pass def __A ( self ) -> int: A_ ,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 __A ( self ) -> int: A_ ,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 __A ( self ) -> Union[str, Any]: A_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE ) def __A ( self ) -> Optional[int]: A_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_pretraining(*_SCREAMING_SNAKE_CASE ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> int: # make masks reproducible np.random.seed(2 ) A_ = int((pt_model.config.image_size // pt_model.config.patch_size) ** 2 ) A_ = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) A_ = torch.from_numpy(_SCREAMING_SNAKE_CASE ) # Add `noise` argument. # PT inputs will be prepared in `super().check_pt_tf_models()` with this added `noise` argument A_ = pt_noise super().check_pt_tf_models(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) def __A ( self ) -> str: A_ ,A_ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A_ = model_class(_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() # make random mask reproducible torch.manual_seed(2 ) with torch.no_grad(): A_ = model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) A_ = outputs[0].cpu().numpy() A_ = 0 with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(_SCREAMING_SNAKE_CASE ) A_ = model_class.from_pretrained(_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) # make random mask reproducible torch.manual_seed(2 ) with torch.no_grad(): A_ = model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) # Make sure we don't have nans A_ = after_outputs[0].cpu().numpy() A_ = 0 A_ = np.amax(np.abs(out_a - out_a ) ) self.assertLessEqual(_SCREAMING_SNAKE_CASE , 1E-5 ) @unittest.skip( reason='''ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load to get deterministic results.''' ) def __A ( self ) -> List[str]: pass @unittest.skip( reason='''ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load to get deterministic results.''' ) def __A ( self ) -> Dict: pass @unittest.skip( reason='''ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load to get deterministic results.''' ) def __A ( self ) -> Tuple: pass @unittest.skip(reason='''ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load''' ) def __A ( self ) -> str: pass @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.''' ) def __A ( self ) -> Union[str, Any]: pass @slow def __A ( self ) -> Dict: for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A_ = ViTMAEModel.from_pretrained(_SCREAMING_SNAKE_CASE ) self.assertIsNotNone(_SCREAMING_SNAKE_CASE ) def _UpperCAmelCase ( ) -> Dict: A_ = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_torch @require_vision class __UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' @cached_property def __A ( self ) -> List[str]: return ViTImageProcessor.from_pretrained('''facebook/vit-mae-base''' ) if is_vision_available() else None @slow def __A ( self ) -> List[str]: # make random mask reproducible across the PT and TF model np.random.seed(2 ) A_ = ViTMAEForPreTraining.from_pretrained('''facebook/vit-mae-base''' ).to(_SCREAMING_SNAKE_CASE ) A_ = self.default_image_processor A_ = prepare_img() A_ = image_processor(images=_SCREAMING_SNAKE_CASE , return_tensors='''pt''' ).to(_SCREAMING_SNAKE_CASE ) # prepare a noise vector that will be also used for testing the TF model # (this way we can ensure that the PT and TF models operate on the same inputs) A_ = ViTMAEConfig() A_ = int((vit_mae_config.image_size // vit_mae_config.patch_size) ** 2 ) A_ = np.random.uniform(size=(1, num_patches) ) # forward pass with torch.no_grad(): A_ = model(**_SCREAMING_SNAKE_CASE , noise=torch.from_numpy(_SCREAMING_SNAKE_CASE ).to(device=_SCREAMING_SNAKE_CASE ) ) # verify the logits A_ = torch.Size((1, 196, 768) ) self.assertEqual(outputs.logits.shape , _SCREAMING_SNAKE_CASE ) A_ = torch.tensor( [[-0.0_548, -1.7_023, -0.9_325], [0.3_721, -0.5_670, -0.2_233], [0.8_235, -1.3_878, -0.3_524]] ) self.assertTrue(torch.allclose(outputs.logits[0, :3, :3] , expected_slice.to(_SCREAMING_SNAKE_CASE ) , atol=1E-4 ) )
18
0
'''simple docstring''' import os import unittest from transformers import MobileBertTokenizer, MobileBertTokenizerFast from transformers.models.bert.tokenization_bert import ( VOCAB_FILES_NAMES, BasicTokenizer, WordpieceTokenizer, _is_control, _is_punctuation, _is_whitespace, ) from transformers.testing_utils import require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin, filter_non_english @require_tokenizers class __UpperCAmelCase ( A_ , unittest.TestCase ): '''simple docstring''' __lowercase : Union[str, Any] = MobileBertTokenizer __lowercase : Optional[int] = MobileBertTokenizerFast __lowercase : Dict = True __lowercase : Tuple = True __lowercase : Optional[Any] = filter_non_english __lowercase : List[str] = "google/mobilebert-uncased" def __A ( self ) -> List[Any]: super().setUp() A_ = [ "[UNK]", "[CLS]", "[SEP]", "[PAD]", "[MASK]", "want", "##want", "##ed", "wa", "un", "runn", "##ing", ",", "low", "lowest", ] A_ = 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] ) ) A_ = [ (tokenizer_def[0], self.pre_trained_model_path, tokenizer_def[2]) # else the 'google/' prefix is stripped for tokenizer_def in self.tokenizers_list ] def __A ( self , _SCREAMING_SNAKE_CASE ) -> Optional[int]: A_ = "UNwant\u00E9d,running" A_ = "unwanted, running" return input_text, output_text def __A ( self ) -> Optional[int]: A_ = self.tokenizer_class(self.vocab_file ) A_ = tokenizer.tokenize('''UNwant\u00E9d,running''' ) self.assertListEqual(snake_case__ , ['''un''', '''##want''', '''##ed''', ''',''', '''runn''', '''##ing'''] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(snake_case__ ) , [9, 6, 7, 12, 10, 11] ) def __A ( self ) -> Optional[Any]: if not self.test_rust_tokenizer: return A_ = self.get_tokenizer() A_ = self.get_rust_tokenizer() A_ = "UNwant\u00E9d,running" A_ = tokenizer.tokenize(snake_case__ ) A_ = rust_tokenizer.tokenize(snake_case__ ) self.assertListEqual(snake_case__ , snake_case__ ) A_ = tokenizer.encode(snake_case__ , add_special_tokens=snake_case__ ) A_ = rust_tokenizer.encode(snake_case__ , add_special_tokens=snake_case__ ) self.assertListEqual(snake_case__ , snake_case__ ) A_ = self.get_rust_tokenizer() A_ = tokenizer.encode(snake_case__ ) A_ = rust_tokenizer.encode(snake_case__ ) self.assertListEqual(snake_case__ , snake_case__ ) # With lower casing A_ = self.get_tokenizer(do_lower_case=snake_case__ ) A_ = self.get_rust_tokenizer(do_lower_case=snake_case__ ) A_ = "UNwant\u00E9d,running" A_ = tokenizer.tokenize(snake_case__ ) A_ = rust_tokenizer.tokenize(snake_case__ ) self.assertListEqual(snake_case__ , snake_case__ ) A_ = tokenizer.encode(snake_case__ , add_special_tokens=snake_case__ ) A_ = rust_tokenizer.encode(snake_case__ , add_special_tokens=snake_case__ ) self.assertListEqual(snake_case__ , snake_case__ ) A_ = self.get_rust_tokenizer() A_ = tokenizer.encode(snake_case__ ) A_ = rust_tokenizer.encode(snake_case__ ) self.assertListEqual(snake_case__ , snake_case__ ) def __A ( self ) -> Optional[int]: A_ = BasicTokenizer() self.assertListEqual(tokenizer.tokenize('''ah\u535A\u63A8zz''' ) , ['''ah''', '''\u535A''', '''\u63A8''', '''zz'''] ) def __A ( self ) -> Union[str, Any]: A_ = BasicTokenizer(do_lower_case=snake_case__ ) self.assertListEqual( tokenizer.tokenize(''' \tHeLLo!how \n Are yoU? ''' ) , ['''hello''', '''!''', '''how''', '''are''', '''you''', '''?'''] ) self.assertListEqual(tokenizer.tokenize('''H\u00E9llo''' ) , ['''hello'''] ) def __A ( self ) -> int: A_ = BasicTokenizer(do_lower_case=snake_case__ , strip_accents=snake_case__ ) self.assertListEqual( tokenizer.tokenize(''' \tHäLLo!how \n Are yoU? ''' ) , ['''hällo''', '''!''', '''how''', '''are''', '''you''', '''?'''] ) self.assertListEqual(tokenizer.tokenize('''H\u00E9llo''' ) , ['''h\u00E9llo'''] ) def __A ( self ) -> Union[str, Any]: A_ = BasicTokenizer(do_lower_case=snake_case__ , strip_accents=snake_case__ ) self.assertListEqual( tokenizer.tokenize(''' \tHäLLo!how \n Are yoU? ''' ) , ['''hallo''', '''!''', '''how''', '''are''', '''you''', '''?'''] ) self.assertListEqual(tokenizer.tokenize('''H\u00E9llo''' ) , ['''hello'''] ) def __A ( self ) -> List[str]: A_ = BasicTokenizer(do_lower_case=snake_case__ ) self.assertListEqual( tokenizer.tokenize(''' \tHäLLo!how \n Are yoU? ''' ) , ['''hallo''', '''!''', '''how''', '''are''', '''you''', '''?'''] ) self.assertListEqual(tokenizer.tokenize('''H\u00E9llo''' ) , ['''hello'''] ) def __A ( self ) -> List[Any]: A_ = BasicTokenizer(do_lower_case=snake_case__ ) self.assertListEqual( tokenizer.tokenize(''' \tHeLLo!how \n Are yoU? ''' ) , ['''HeLLo''', '''!''', '''how''', '''Are''', '''yoU''', '''?'''] ) def __A ( self ) -> Dict: A_ = BasicTokenizer(do_lower_case=snake_case__ , strip_accents=snake_case__ ) self.assertListEqual( tokenizer.tokenize(''' \tHäLLo!how \n Are yoU? ''' ) , ['''HäLLo''', '''!''', '''how''', '''Are''', '''yoU''', '''?'''] ) def __A ( self ) -> Optional[int]: A_ = BasicTokenizer(do_lower_case=snake_case__ , strip_accents=snake_case__ ) self.assertListEqual( tokenizer.tokenize(''' \tHäLLo!how \n Are yoU? ''' ) , ['''HaLLo''', '''!''', '''how''', '''Are''', '''yoU''', '''?'''] ) def __A ( self ) -> Any: A_ = BasicTokenizer(do_lower_case=snake_case__ , never_split=['''[UNK]'''] ) self.assertListEqual( tokenizer.tokenize(''' \tHeLLo!how \n Are yoU? [UNK]''' ) , ['''HeLLo''', '''!''', '''how''', '''Are''', '''yoU''', '''?''', '''[UNK]'''] ) def __A ( self ) -> List[str]: A_ = ["[UNK]", "[CLS]", "[SEP]", "want", "##want", "##ed", "wa", "un", "runn", "##ing"] A_ = {} for i, token in enumerate(snake_case__ ): A_ = i A_ = WordpieceTokenizer(vocab=snake_case__ , unk_token='''[UNK]''' ) self.assertListEqual(tokenizer.tokenize('''''' ) , [] ) self.assertListEqual(tokenizer.tokenize('''unwanted running''' ) , ['''un''', '''##want''', '''##ed''', '''runn''', '''##ing'''] ) self.assertListEqual(tokenizer.tokenize('''unwantedX running''' ) , ['''[UNK]''', '''runn''', '''##ing'''] ) def __A ( self ) -> Union[str, Any]: self.assertTrue(_is_whitespace(''' ''' ) ) self.assertTrue(_is_whitespace('''\t''' ) ) self.assertTrue(_is_whitespace('''\r''' ) ) self.assertTrue(_is_whitespace('''\n''' ) ) self.assertTrue(_is_whitespace('''\u00A0''' ) ) self.assertFalse(_is_whitespace('''A''' ) ) self.assertFalse(_is_whitespace('''-''' ) ) def __A ( self ) -> Dict: self.assertTrue(_is_control('''\u0005''' ) ) self.assertFalse(_is_control('''A''' ) ) self.assertFalse(_is_control(''' ''' ) ) self.assertFalse(_is_control('''\t''' ) ) self.assertFalse(_is_control('''\r''' ) ) def __A ( self ) -> Dict: self.assertTrue(_is_punctuation('''-''' ) ) self.assertTrue(_is_punctuation('''$''' ) ) self.assertTrue(_is_punctuation('''`''' ) ) self.assertTrue(_is_punctuation('''.''' ) ) self.assertFalse(_is_punctuation('''A''' ) ) self.assertFalse(_is_punctuation(''' ''' ) ) def __A ( self ) -> int: A_ = self.get_tokenizer() A_ = self.get_rust_tokenizer() # Example taken from the issue https://github.com/huggingface/tokenizers/issues/340 self.assertListEqual([tokenizer.tokenize(snake_case__ ) for t in ['''Test''', '''\xad''', '''test''']] , [['''[UNK]'''], [], ['''[UNK]''']] ) self.assertListEqual( [rust_tokenizer.tokenize(snake_case__ ) for t in ['''Test''', '''\xad''', '''test''']] , [['''[UNK]'''], [], ['''[UNK]''']] ) @slow def __A ( self ) -> Dict: A_ = self.tokenizer_class.from_pretrained('''google/mobilebert-uncased''' ) A_ = tokenizer.encode('''sequence builders''' , add_special_tokens=snake_case__ ) A_ = tokenizer.encode('''multi-sequence build''' , add_special_tokens=snake_case__ ) A_ = tokenizer.build_inputs_with_special_tokens(snake_case__ ) A_ = tokenizer.build_inputs_with_special_tokens(snake_case__ , snake_case__ ) assert encoded_sentence == [101] + text + [102] assert encoded_pair == [101] + text + [102] + text_a + [102] def __A ( self ) -> int: for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): A_ = self.rust_tokenizer_class.from_pretrained(snake_case__ , **snake_case__ ) A_ = F'''A, naïve {tokenizer_r.mask_token} AllenNLP sentence.''' A_ = tokenizer_r.encode_plus( snake_case__ , return_attention_mask=snake_case__ , return_token_type_ids=snake_case__ , return_offsets_mapping=snake_case__ , add_special_tokens=snake_case__ , ) A_ = tokenizer_r.do_lower_case if hasattr(snake_case__ , '''do_lower_case''' ) else False A_ = ( [ ((0, 0), tokenizer_r.cls_token), ((0, 1), "A"), ((1, 2), ","), ((3, 5), "na"), ((5, 6), "##ï"), ((6, 8), "##ve"), ((9, 15), tokenizer_r.mask_token), ((16, 21), "Allen"), ((21, 23), "##NL"), ((23, 24), "##P"), ((25, 33), "sentence"), ((33, 34), "."), ((0, 0), tokenizer_r.sep_token), ] if not do_lower_case else [ ((0, 0), tokenizer_r.cls_token), ((0, 1), "a"), ((1, 2), ","), ((3, 8), "naive"), ((9, 15), tokenizer_r.mask_token), ((16, 21), "allen"), ((21, 23), "##nl"), ((23, 24), "##p"), ((25, 33), "sentence"), ((33, 34), "."), ((0, 0), tokenizer_r.sep_token), ] ) self.assertEqual( [e[1] for e in expected_results] , tokenizer_r.convert_ids_to_tokens(tokens['''input_ids'''] ) ) self.assertEqual([e[0] for e in expected_results] , tokens['''offset_mapping'''] ) def __A ( self ) -> List[str]: A_ = ["的", "人", "有"] A_ = "".join(snake_case__ ) for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): A_ = True A_ = self.tokenizer_class.from_pretrained(snake_case__ , **snake_case__ ) A_ = self.rust_tokenizer_class.from_pretrained(snake_case__ , **snake_case__ ) A_ = tokenizer_p.encode(snake_case__ , add_special_tokens=snake_case__ ) A_ = tokenizer_r.encode(snake_case__ , add_special_tokens=snake_case__ ) A_ = tokenizer_r.convert_ids_to_tokens(snake_case__ ) A_ = tokenizer_p.convert_ids_to_tokens(snake_case__ ) # it is expected that each Chinese character is not preceded by "##" self.assertListEqual(snake_case__ , snake_case__ ) self.assertListEqual(snake_case__ , snake_case__ ) A_ = False A_ = self.rust_tokenizer_class.from_pretrained(snake_case__ , **snake_case__ ) A_ = self.tokenizer_class.from_pretrained(snake_case__ , **snake_case__ ) A_ = tokenizer_r.encode(snake_case__ , add_special_tokens=snake_case__ ) A_ = tokenizer_p.encode(snake_case__ , add_special_tokens=snake_case__ ) A_ = tokenizer_r.convert_ids_to_tokens(snake_case__ ) A_ = tokenizer_p.convert_ids_to_tokens(snake_case__ ) # it is expected that only the first Chinese character is not preceded by "##". A_ = [ F'''##{token}''' if idx != 0 else token for idx, token in enumerate(snake_case__ ) ] self.assertListEqual(snake_case__ , snake_case__ ) self.assertListEqual(snake_case__ , snake_case__ )
360
'''simple docstring''' from typing import Callable, Optional, Union from ...configuration_utils import PretrainedConfig from ...utils import logging __snake_case : int = logging.get_logger(__name__) __snake_case : str = { 'microsoft/xprophetnet-large-wiki100-cased': ( 'https://huggingface.co/microsoft/xprophetnet-large-wiki100-cased/resolve/main/config.json' ), } class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' __lowercase : Optional[Any] = 'xlm-prophetnet' __lowercase : Optional[int] = ['past_key_values'] __lowercase : int = { 'num_attention_heads': 'num_encoder_attention_heads', } def __init__( self , _SCREAMING_SNAKE_CASE = 0.1 , _SCREAMING_SNAKE_CASE = "gelu" , _SCREAMING_SNAKE_CASE = 3_0522 , _SCREAMING_SNAKE_CASE = 1024 , _SCREAMING_SNAKE_CASE = 4096 , _SCREAMING_SNAKE_CASE = 12 , _SCREAMING_SNAKE_CASE = 16 , _SCREAMING_SNAKE_CASE = 4096 , _SCREAMING_SNAKE_CASE = 12 , _SCREAMING_SNAKE_CASE = 16 , _SCREAMING_SNAKE_CASE = 0.1 , _SCREAMING_SNAKE_CASE = 0.1 , _SCREAMING_SNAKE_CASE = 512 , _SCREAMING_SNAKE_CASE = 0.02 , _SCREAMING_SNAKE_CASE = True , _SCREAMING_SNAKE_CASE = True , _SCREAMING_SNAKE_CASE = 0 , _SCREAMING_SNAKE_CASE = 2 , _SCREAMING_SNAKE_CASE = 32 , _SCREAMING_SNAKE_CASE = 128 , _SCREAMING_SNAKE_CASE = False , _SCREAMING_SNAKE_CASE = 0.0 , _SCREAMING_SNAKE_CASE = True , _SCREAMING_SNAKE_CASE = 0 , _SCREAMING_SNAKE_CASE = 1 , _SCREAMING_SNAKE_CASE = 2 , **_SCREAMING_SNAKE_CASE , ) -> int: A_ = vocab_size A_ = hidden_size A_ = encoder_ffn_dim A_ = num_encoder_layers A_ = num_encoder_attention_heads A_ = decoder_ffn_dim A_ = num_decoder_layers A_ = num_decoder_attention_heads A_ = max_position_embeddings A_ = init_std # Normal(0, this parameter) A_ = activation_function # parameters for xlmprophetnet A_ = ngram A_ = num_buckets A_ = relative_max_distance A_ = disable_ngram_loss A_ = eps # 3 Types of Dropout A_ = attention_dropout A_ = activation_dropout A_ = dropout A_ = use_cache super().__init__( pad_token_id=_SCREAMING_SNAKE_CASE , bos_token_id=_SCREAMING_SNAKE_CASE , eos_token_id=_SCREAMING_SNAKE_CASE , is_encoder_decoder=_SCREAMING_SNAKE_CASE , add_cross_attention=_SCREAMING_SNAKE_CASE , decoder_start_token_id=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE , ) @property def __A ( self ) -> int: return self.num_encoder_layers + self.num_decoder_layers @num_hidden_layers.setter def __A ( self , _SCREAMING_SNAKE_CASE ) -> List[Any]: raise NotImplementedError( '''This model does not support the setting of `num_hidden_layers`. Please set `num_encoder_layers` and''' ''' `num_decoder_layers`.''' )
18
0
'''simple docstring''' from typing import List, Optional, Union from ...configuration_utils import PretrainedConfig from ...utils import logging __snake_case : Union[str, Any] = logging.get_logger(__name__) __snake_case : Optional[int] = { 'huggingface/time-series-transformer-tourism-monthly': ( 'https://huggingface.co/huggingface/time-series-transformer-tourism-monthly/resolve/main/config.json' ), # See all TimeSeriesTransformer models at https://huggingface.co/models?filter=time_series_transformer } class __UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowercase : Tuple = 'time_series_transformer' __lowercase : Tuple = { 'hidden_size': 'd_model', 'num_attention_heads': 'encoder_attention_heads', 'num_hidden_layers': 'encoder_layers', } def __init__( self , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = "student_t" , _SCREAMING_SNAKE_CASE = "nll" , _SCREAMING_SNAKE_CASE = 1 , _SCREAMING_SNAKE_CASE = [1, 2, 3, 4, 5, 6, 7] , _SCREAMING_SNAKE_CASE = "mean" , _SCREAMING_SNAKE_CASE = 0 , _SCREAMING_SNAKE_CASE = 0 , _SCREAMING_SNAKE_CASE = 0 , _SCREAMING_SNAKE_CASE = 0 , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = 32 , _SCREAMING_SNAKE_CASE = 32 , _SCREAMING_SNAKE_CASE = 2 , _SCREAMING_SNAKE_CASE = 2 , _SCREAMING_SNAKE_CASE = 2 , _SCREAMING_SNAKE_CASE = 2 , _SCREAMING_SNAKE_CASE = True , _SCREAMING_SNAKE_CASE = "gelu" , _SCREAMING_SNAKE_CASE = 64 , _SCREAMING_SNAKE_CASE = 0.1 , _SCREAMING_SNAKE_CASE = 0.1 , _SCREAMING_SNAKE_CASE = 0.1 , _SCREAMING_SNAKE_CASE = 0.1 , _SCREAMING_SNAKE_CASE = 0.1 , _SCREAMING_SNAKE_CASE = 100 , _SCREAMING_SNAKE_CASE = 0.02 , _SCREAMING_SNAKE_CASE=True , **_SCREAMING_SNAKE_CASE , ) -> Optional[Any]: # time series specific configuration A_ = prediction_length A_ = context_length or prediction_length A_ = distribution_output A_ = loss A_ = input_size A_ = num_time_features A_ = lags_sequence A_ = scaling A_ = num_dynamic_real_features A_ = num_static_real_features A_ = num_static_categorical_features if cardinality and num_static_categorical_features > 0: if len(_a ) != num_static_categorical_features: raise ValueError( '''The cardinality should be a list of the same length as `num_static_categorical_features`''' ) A_ = cardinality else: A_ = [0] if embedding_dimension and num_static_categorical_features > 0: if len(_a ) != num_static_categorical_features: raise ValueError( '''The embedding dimension should be a list of the same length as `num_static_categorical_features`''' ) A_ = embedding_dimension else: A_ = [min(50 , (cat + 1) // 2 ) for cat in self.cardinality] A_ = num_parallel_samples # Transformer architecture configuration A_ = input_size * len(_a ) + self._number_of_features A_ = d_model A_ = encoder_attention_heads A_ = decoder_attention_heads A_ = encoder_ffn_dim A_ = decoder_ffn_dim A_ = encoder_layers A_ = decoder_layers A_ = dropout A_ = attention_dropout A_ = activation_dropout A_ = encoder_layerdrop A_ = decoder_layerdrop A_ = activation_function A_ = init_std A_ = use_cache super().__init__(is_encoder_decoder=_a , **_a ) @property def __A ( self ) -> Optional[int]: return ( sum(self.embedding_dimension ) + self.num_dynamic_real_features + self.num_time_features + self.num_static_real_features + self.input_size * 2 # the log1p(abs(loc)) and log(scale) features )
361
'''simple docstring''' def _UpperCAmelCase ( _UpperCamelCase : float, _UpperCamelCase : list[float] ) -> float: if discount_rate < 0: raise ValueError('''Discount rate cannot be negative''' ) if not cash_flows: raise ValueError('''Cash flows list cannot be empty''' ) A_ = sum( cash_flow / ((1 + discount_rate) ** i) for i, cash_flow in enumerate(_UpperCamelCase ) ) return round(_UpperCamelCase, ndigits=2 ) if __name__ == "__main__": import doctest doctest.testmod()
18
0
'''simple docstring''' from PIL import Image def _UpperCAmelCase ( _UpperCamelCase : List[str], _UpperCamelCase : str ) -> Any: def brightness(_UpperCamelCase : int ) -> float: return 1_28 + level + (c - 1_28) if not -2_55.0 <= level <= 2_55.0: raise ValueError('''level must be between -255.0 (black) and 255.0 (white)''' ) return img.point(lowercase__ ) if __name__ == "__main__": # Load image with Image.open('image_data/lena.jpg') as img: # Change brightness to 100 __snake_case : Dict = change_brightness(img, 100) brigt_img.save('image_data/lena_brightness.png', format='png')
362
'''simple docstring''' from __future__ import annotations def _UpperCAmelCase ( _UpperCamelCase : int | str ) -> bool: A_ = str(_UpperCamelCase ) return n == n[::-1] def _UpperCAmelCase ( _UpperCamelCase : int = 1_00_00_00 ) -> Any: A_ = 0 for i in range(1, _UpperCamelCase ): if is_palindrome(_UpperCamelCase ) and is_palindrome(bin(_UpperCamelCase ).split('''b''' )[1] ): total += i return total if __name__ == "__main__": print(solution(int(str(input().strip()))))
18
0
'''simple docstring''' import unittest from transformers import is_vision_available from transformers.pipelines import pipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_vision_available(): from PIL import Image else: class __UpperCAmelCase : '''simple docstring''' @staticmethod def __A ( *_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) -> Union[str, Any]: pass @is_pipeline_test @require_vision class __UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' @require_torch def __A ( self ) -> Optional[Any]: A_ = pipeline( model='''hf-internal-testing/tiny-random-clip-zero-shot-image-classification''' , ) A_ = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) A_ = image_classifier(_SCREAMING_SNAKE_CASE , candidate_labels=['''a''', '''b''', '''c'''] ) # The floating scores are so close, we enter floating error approximation and the order is not guaranteed across # python and torch versions. self.assertIn( nested_simplify(_SCREAMING_SNAKE_CASE ) , [ [{'''score''': 0.333, '''label''': '''a'''}, {'''score''': 0.333, '''label''': '''b'''}, {'''score''': 0.333, '''label''': '''c'''}], [{'''score''': 0.333, '''label''': '''a'''}, {'''score''': 0.333, '''label''': '''c'''}, {'''score''': 0.333, '''label''': '''b'''}], ] , ) A_ = image_classifier([image] * 5 , candidate_labels=['''A''', '''B''', '''C'''] , batch_size=2 ) self.assertEqual( nested_simplify(_SCREAMING_SNAKE_CASE ) , [ [ {'''score''': 0.333, '''label''': ANY(_SCREAMING_SNAKE_CASE )}, {'''score''': 0.333, '''label''': ANY(_SCREAMING_SNAKE_CASE )}, {'''score''': 0.333, '''label''': ANY(_SCREAMING_SNAKE_CASE )}, ], [ {'''score''': 0.333, '''label''': ANY(_SCREAMING_SNAKE_CASE )}, {'''score''': 0.333, '''label''': ANY(_SCREAMING_SNAKE_CASE )}, {'''score''': 0.333, '''label''': ANY(_SCREAMING_SNAKE_CASE )}, ], [ {'''score''': 0.333, '''label''': ANY(_SCREAMING_SNAKE_CASE )}, {'''score''': 0.333, '''label''': ANY(_SCREAMING_SNAKE_CASE )}, {'''score''': 0.333, '''label''': ANY(_SCREAMING_SNAKE_CASE )}, ], [ {'''score''': 0.333, '''label''': ANY(_SCREAMING_SNAKE_CASE )}, {'''score''': 0.333, '''label''': ANY(_SCREAMING_SNAKE_CASE )}, {'''score''': 0.333, '''label''': ANY(_SCREAMING_SNAKE_CASE )}, ], [ {'''score''': 0.333, '''label''': ANY(_SCREAMING_SNAKE_CASE )}, {'''score''': 0.333, '''label''': ANY(_SCREAMING_SNAKE_CASE )}, {'''score''': 0.333, '''label''': ANY(_SCREAMING_SNAKE_CASE )}, ], ] , ) @require_tf def __A ( self ) -> Optional[Any]: A_ = pipeline( model='''hf-internal-testing/tiny-random-clip-zero-shot-image-classification''' , framework='''tf''' ) A_ = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) A_ = image_classifier(_SCREAMING_SNAKE_CASE , candidate_labels=['''a''', '''b''', '''c'''] ) self.assertEqual( nested_simplify(_SCREAMING_SNAKE_CASE ) , [{'''score''': 0.333, '''label''': '''a'''}, {'''score''': 0.333, '''label''': '''b'''}, {'''score''': 0.333, '''label''': '''c'''}] , ) A_ = image_classifier([image] * 5 , candidate_labels=['''A''', '''B''', '''C'''] , batch_size=2 ) self.assertEqual( nested_simplify(_SCREAMING_SNAKE_CASE ) , [ [ {'''score''': 0.333, '''label''': ANY(_SCREAMING_SNAKE_CASE )}, {'''score''': 0.333, '''label''': ANY(_SCREAMING_SNAKE_CASE )}, {'''score''': 0.333, '''label''': ANY(_SCREAMING_SNAKE_CASE )}, ], [ {'''score''': 0.333, '''label''': ANY(_SCREAMING_SNAKE_CASE )}, {'''score''': 0.333, '''label''': ANY(_SCREAMING_SNAKE_CASE )}, {'''score''': 0.333, '''label''': ANY(_SCREAMING_SNAKE_CASE )}, ], [ {'''score''': 0.333, '''label''': ANY(_SCREAMING_SNAKE_CASE )}, {'''score''': 0.333, '''label''': ANY(_SCREAMING_SNAKE_CASE )}, {'''score''': 0.333, '''label''': ANY(_SCREAMING_SNAKE_CASE )}, ], [ {'''score''': 0.333, '''label''': ANY(_SCREAMING_SNAKE_CASE )}, {'''score''': 0.333, '''label''': ANY(_SCREAMING_SNAKE_CASE )}, {'''score''': 0.333, '''label''': ANY(_SCREAMING_SNAKE_CASE )}, ], [ {'''score''': 0.333, '''label''': ANY(_SCREAMING_SNAKE_CASE )}, {'''score''': 0.333, '''label''': ANY(_SCREAMING_SNAKE_CASE )}, {'''score''': 0.333, '''label''': ANY(_SCREAMING_SNAKE_CASE )}, ], ] , ) @slow @require_torch def __A ( self ) -> Any: A_ = pipeline( task='''zero-shot-image-classification''' , model='''openai/clip-vit-base-patch32''' , ) # This is an image of 2 cats with remotes and no planes A_ = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) A_ = image_classifier(_SCREAMING_SNAKE_CASE , candidate_labels=['''cat''', '''plane''', '''remote'''] ) self.assertEqual( nested_simplify(_SCREAMING_SNAKE_CASE ) , [ {'''score''': 0.511, '''label''': '''remote'''}, {'''score''': 0.485, '''label''': '''cat'''}, {'''score''': 0.004, '''label''': '''plane'''}, ] , ) A_ = image_classifier([image] * 5 , candidate_labels=['''cat''', '''plane''', '''remote'''] , batch_size=2 ) self.assertEqual( nested_simplify(_SCREAMING_SNAKE_CASE ) , [ [ {'''score''': 0.511, '''label''': '''remote'''}, {'''score''': 0.485, '''label''': '''cat'''}, {'''score''': 0.004, '''label''': '''plane'''}, ], ] * 5 , ) @slow @require_tf def __A ( self ) -> Tuple: A_ = pipeline( task='''zero-shot-image-classification''' , model='''openai/clip-vit-base-patch32''' , framework='''tf''' ) # This is an image of 2 cats with remotes and no planes A_ = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) A_ = image_classifier(_SCREAMING_SNAKE_CASE , candidate_labels=['''cat''', '''plane''', '''remote'''] ) self.assertEqual( nested_simplify(_SCREAMING_SNAKE_CASE ) , [ {'''score''': 0.511, '''label''': '''remote'''}, {'''score''': 0.485, '''label''': '''cat'''}, {'''score''': 0.004, '''label''': '''plane'''}, ] , ) A_ = image_classifier([image] * 5 , candidate_labels=['''cat''', '''plane''', '''remote'''] , batch_size=2 ) self.assertEqual( nested_simplify(_SCREAMING_SNAKE_CASE ) , [ [ {'''score''': 0.511, '''label''': '''remote'''}, {'''score''': 0.485, '''label''': '''cat'''}, {'''score''': 0.004, '''label''': '''plane'''}, ], ] * 5 , )
363
'''simple docstring''' # Usage: # ./gen-card-facebook-wmt19.py import os from pathlib import Path def _UpperCAmelCase ( _UpperCamelCase : Tuple, _UpperCamelCase : Tuple, _UpperCamelCase : List[str] ) -> int: A_ = { '''en''': '''Machine learning is great, isn\'t it?''', '''ru''': '''Машинное обучение - это здорово, не так ли?''', '''de''': '''Maschinelles Lernen ist großartig, oder?''', } # BLUE scores as follows: # "pair": [fairseq, transformers] A_ = { '''ru-en''': ['''[41.3](http://matrix.statmt.org/matrix/output/1907?run_id=6937)''', '''39.20'''], '''en-ru''': ['''[36.4](http://matrix.statmt.org/matrix/output/1914?run_id=6724)''', '''33.47'''], '''en-de''': ['''[43.1](http://matrix.statmt.org/matrix/output/1909?run_id=6862)''', '''42.83'''], '''de-en''': ['''[42.3](http://matrix.statmt.org/matrix/output/1902?run_id=6750)''', '''41.35'''], } A_ = F'''{src_lang}-{tgt_lang}''' A_ = F''' --- language: - {src_lang} - {tgt_lang} thumbnail: tags: - translation - wmt19 - facebook license: apache-2.0 datasets: - wmt19 metrics: - bleu --- # FSMT ## Model description This is a ported version of [fairseq wmt19 transformer](https://github.com/pytorch/fairseq/blob/master/examples/wmt19/README.md) for {src_lang}-{tgt_lang}. For more details, please see, [Facebook FAIR\'s WMT19 News Translation Task Submission](https://arxiv.org/abs/1907.06616). The abbreviation FSMT stands for FairSeqMachineTranslation All four models are available: * [wmt19-en-ru](https://huggingface.co/facebook/wmt19-en-ru) * [wmt19-ru-en](https://huggingface.co/facebook/wmt19-ru-en) * [wmt19-en-de](https://huggingface.co/facebook/wmt19-en-de) * [wmt19-de-en](https://huggingface.co/facebook/wmt19-de-en) ## Intended uses & limitations #### How to use ```python from transformers import FSMTForConditionalGeneration, FSMTTokenizer mname = "facebook/wmt19-{src_lang}-{tgt_lang}" tokenizer = FSMTTokenizer.from_pretrained(mname) model = FSMTForConditionalGeneration.from_pretrained(mname) input = "{texts[src_lang]}" input_ids = tokenizer.encode(input, return_tensors="pt") outputs = model.generate(input_ids) decoded = tokenizer.decode(outputs[0], skip_special_tokens=True) print(decoded) # {texts[tgt_lang]} ``` #### Limitations and bias - The original (and this ported model) doesn\'t seem to handle well inputs with repeated sub-phrases, [content gets truncated](https://discuss.huggingface.co/t/issues-with-translating-inputs-containing-repeated-phrases/981) ## Training data Pretrained weights were left identical to the original model released by fairseq. For more details, please, see the [paper](https://arxiv.org/abs/1907.06616). ## Eval results pair | fairseq | transformers -------|---------|---------- {pair} | {scores[pair][0]} | {scores[pair][1]} The score is slightly below the score reported by `fairseq`, since `transformers`` currently doesn\'t support: - model ensemble, therefore the best performing checkpoint was ported (``model4.pt``). - re-ranking The score was calculated using this code: ```bash git clone https://github.com/huggingface/transformers cd transformers export PAIR={pair} export DATA_DIR=data/$PAIR export SAVE_DIR=data/$PAIR export BS=8 export NUM_BEAMS=15 mkdir -p $DATA_DIR sacrebleu -t wmt19 -l $PAIR --echo src > $DATA_DIR/val.source sacrebleu -t wmt19 -l $PAIR --echo ref > $DATA_DIR/val.target echo $PAIR PYTHONPATH="src:examples/seq2seq" python examples/seq2seq/run_eval.py facebook/wmt19-$PAIR $DATA_DIR/val.source $SAVE_DIR/test_translations.txt --reference_path $DATA_DIR/val.target --score_path $SAVE_DIR/test_bleu.json --bs $BS --task translation --num_beams $NUM_BEAMS ``` note: fairseq reports using a beam of 50, so you should get a slightly higher score if re-run with `--num_beams 50`. ## Data Sources - [training, etc.](http://www.statmt.org/wmt19/) - [test set](http://matrix.statmt.org/test_sets/newstest2019.tgz?1556572561) ### BibTeX entry and citation info ```bibtex @inproceedings{{..., year={{2020}}, title={{Facebook FAIR\'s WMT19 News Translation Task Submission}}, author={{Ng, Nathan and Yee, Kyra and Baevski, Alexei and Ott, Myle and Auli, Michael and Edunov, Sergey}}, booktitle={{Proc. of WMT}}, }} ``` ## TODO - port model ensemble (fairseq uses 4 model checkpoints) ''' os.makedirs(_UpperCamelCase, exist_ok=_UpperCamelCase ) A_ = os.path.join(_UpperCamelCase, '''README.md''' ) print(F'''Generating {path}''' ) with open(_UpperCamelCase, '''w''', encoding='''utf-8''' ) as f: f.write(_UpperCamelCase ) # make sure we are under the root of the project __snake_case : Any = Path(__file__).resolve().parent.parent.parent __snake_case : Tuple = repo_dir / 'model_cards' for model_name in ["wmt19-ru-en", "wmt19-en-ru", "wmt19-en-de", "wmt19-de-en"]: __snake_case , __snake_case , __snake_case : Any = model_name.split('-') __snake_case : int = model_cards_dir / 'facebook' / model_name write_model_card(model_card_dir, src_lang=src_lang, tgt_lang=tgt_lang)
18
0
'''simple docstring''' import os import tempfile import unittest from transformers import is_torch_available from transformers.testing_utils import require_torch if is_torch_available(): import torch from torch import nn from transformers import ( Adafactor, AdamW, get_constant_schedule, get_constant_schedule_with_warmup, get_cosine_schedule_with_warmup, get_cosine_with_hard_restarts_schedule_with_warmup, get_inverse_sqrt_schedule, get_linear_schedule_with_warmup, get_polynomial_decay_schedule_with_warmup, ) def _UpperCAmelCase ( _UpperCamelCase : List[str], _UpperCamelCase : Optional[Any]=10 ) -> Optional[int]: A_ = [] for _ in range(a_ ): lrs.append(scheduler.get_lr()[0] ) scheduler.step() return lrs def _UpperCAmelCase ( _UpperCamelCase : Dict, _UpperCamelCase : List[Any]=10 ) -> Optional[Any]: A_ = [] for step in range(a_ ): lrs.append(scheduler.get_lr()[0] ) scheduler.step() if step == num_steps // 2: with tempfile.TemporaryDirectory() as tmpdirname: A_ = os.path.join(a_, '''schedule.bin''' ) torch.save(scheduler.state_dict(), a_ ) A_ = torch.load(a_ ) scheduler.load_state_dict(a_ ) return lrs @require_torch class __UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> str: self.assertEqual(len(lowercase_ ) , len(lowercase_ ) ) for a, b in zip(lowercase_ , lowercase_ ): self.assertAlmostEqual(lowercase_ , lowercase_ , delta=lowercase_ ) def __A ( self ) -> List[str]: A_ = torch.tensor([0.1, -0.2, -0.1] , requires_grad=lowercase_ ) A_ = torch.tensor([0.4, 0.2, -0.5] ) A_ = nn.MSELoss() # No warmup, constant schedule, no gradient clipping A_ = AdamW(params=[w] , lr=2E-1 , weight_decay=0.0 ) for _ in range(100 ): A_ = criterion(lowercase_ , lowercase_ ) loss.backward() optimizer.step() w.grad.detach_() # No zero_grad() function on simple tensors. we do it ourselves. w.grad.zero_() self.assertListAlmostEqual(w.tolist() , [0.4, 0.2, -0.5] , tol=1E-2 ) def __A ( self ) -> List[Any]: A_ = torch.tensor([0.1, -0.2, -0.1] , requires_grad=lowercase_ ) A_ = torch.tensor([0.4, 0.2, -0.5] ) A_ = nn.MSELoss() # No warmup, constant schedule, no gradient clipping A_ = Adafactor( params=[w] , lr=1E-2 , eps=(1E-30, 1E-3) , clip_threshold=1.0 , decay_rate=-0.8 , betaa=lowercase_ , weight_decay=0.0 , relative_step=lowercase_ , scale_parameter=lowercase_ , warmup_init=lowercase_ , ) for _ in range(1000 ): A_ = criterion(lowercase_ , lowercase_ ) loss.backward() optimizer.step() w.grad.detach_() # No zero_grad() function on simple tensors. we do it ourselves. w.grad.zero_() self.assertListAlmostEqual(w.tolist() , [0.4, 0.2, -0.5] , tol=1E-2 ) @require_torch class __UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' __lowercase : Tuple = nn.Linear(50 , 50 ) if is_torch_available() else None __lowercase : Any = AdamW(m.parameters() , lr=10.0 ) if is_torch_available() else None __lowercase : str = 10 def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=None ) -> Union[str, Any]: self.assertEqual(len(lowercase_ ) , len(lowercase_ ) ) for a, b in zip(lowercase_ , lowercase_ ): self.assertAlmostEqual(lowercase_ , lowercase_ , delta=lowercase_ , msg=lowercase_ ) def __A ( self ) -> Dict: A_ = {'''num_warmup_steps''': 2, '''num_training_steps''': 10} # schedulers doct format # function: (sched_args_dict, expected_learning_rates) A_ = { get_constant_schedule: ({}, [10.0] * self.num_steps), get_constant_schedule_with_warmup: ( {'''num_warmup_steps''': 4}, [0.0, 2.5, 5.0, 7.5, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0], ), get_linear_schedule_with_warmup: ( {**common_kwargs}, [0.0, 5.0, 10.0, 8.75, 7.5, 6.25, 5.0, 3.75, 2.5, 1.25], ), get_cosine_schedule_with_warmup: ( {**common_kwargs}, [0.0, 5.0, 10.0, 9.61, 8.53, 6.91, 5.0, 3.08, 1.46, 0.38], ), get_cosine_with_hard_restarts_schedule_with_warmup: ( {**common_kwargs, '''num_cycles''': 2}, [0.0, 5.0, 10.0, 8.53, 5.0, 1.46, 10.0, 8.53, 5.0, 1.46], ), get_polynomial_decay_schedule_with_warmup: ( {**common_kwargs, '''power''': 2.0, '''lr_end''': 1E-7}, [0.0, 5.0, 10.0, 7.656, 5.625, 3.906, 2.5, 1.406, 0.625, 0.156], ), get_inverse_sqrt_schedule: ( {'''num_warmup_steps''': 2}, [0.0, 5.0, 10.0, 8.165, 7.071, 6.325, 5.774, 5.345, 5.0, 4.714], ), } for scheduler_func, data in scheds.items(): A_ , A_ = data A_ = scheduler_func(self.optimizer , **lowercase_ ) self.assertEqual(len([scheduler.get_lr()[0]] ) , 1 ) A_ = unwrap_schedule(lowercase_ , self.num_steps ) self.assertListAlmostEqual( lowercase_ , lowercase_ , tol=1E-2 , msg=F'''failed for {scheduler_func} in normal scheduler''' , ) A_ = scheduler_func(self.optimizer , **lowercase_ ) if scheduler_func.__name__ != "get_constant_schedule": LambdaScheduleWrapper.wrap_scheduler(lowercase_ ) # wrap to test picklability of the schedule A_ = unwrap_and_save_reload_schedule(lowercase_ , self.num_steps ) self.assertListEqual(lowercase_ , lowercase_ , msg=F'''failed for {scheduler_func} in save and reload''' ) class __UpperCAmelCase : '''simple docstring''' def __init__( self , _SCREAMING_SNAKE_CASE ) -> Tuple: A_ = fn def __call__( self , *_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) -> List[str]: return self.fn(*lowercase_ , **lowercase_ ) @classmethod def __A ( self , _SCREAMING_SNAKE_CASE ) -> Union[str, Any]: A_ = list(map(self , scheduler.lr_lambdas ) )
364
'''simple docstring''' from collections import defaultdict def _UpperCAmelCase ( _UpperCamelCase : int ) -> int: A_ = 1 A_ = True for v in tree[start]: if v not in visited: ret += dfs(_UpperCamelCase ) if ret % 2 == 0: cuts.append(_UpperCamelCase ) return ret def _UpperCAmelCase ( ) -> Optional[Any]: dfs(1 ) if __name__ == "__main__": __snake_case , __snake_case : Union[str, Any] = 10, 9 __snake_case : int = defaultdict(list) __snake_case : dict[int, bool] = {} __snake_case : list[int] = [] __snake_case : Union[str, Any] = 0 __snake_case : int = [(2, 1), (3, 1), (4, 3), (5, 2), (6, 1), (7, 2), (8, 6), (9, 8), (10, 8)] for u, v in edges: tree[u].append(v) tree[v].append(u) even_tree() print(len(cuts) - 1)
18
0
'''simple docstring''' import sys import tempfile import unittest import unittest.mock as mock from pathlib import Path from huggingface_hub import HfFolder, delete_repo from requests.exceptions import HTTPError from transformers import AutoFeatureExtractor, WavaVecaFeatureExtractor from transformers.testing_utils import TOKEN, USER, get_tests_dir, is_staging_test sys.path.append(str(Path(__file__).parent.parent / 'utils')) from test_module.custom_feature_extraction import CustomFeatureExtractor # noqa E402 __snake_case : str = get_tests_dir('fixtures') class __UpperCAmelCase ( unittest.TestCase ): def __A ( self ) -> List[Any]: # A mock response for an HTTP head request to emulate server down A_ = mock.Mock() A_ = 500 A_ = {} A_ = HTTPError A_ = {} # Download this model to make sure it's in the cache. A_ = WavaVecaFeatureExtractor.from_pretrained('''hf-internal-testing/tiny-random-wav2vec2''' ) # Under the mock environment we get a 500 error when trying to reach the model. with mock.patch('''requests.Session.request''' , return_value=_a ) as mock_head: A_ = WavaVecaFeatureExtractor.from_pretrained('''hf-internal-testing/tiny-random-wav2vec2''' ) # This check we did call the fake head request mock_head.assert_called() def __A ( self ) -> Any: # This test is for deprecated behavior and can be removed in v5 A_ = WavaVecaFeatureExtractor.from_pretrained( '''https://huggingface.co/hf-internal-testing/tiny-random-wav2vec2/resolve/main/preprocessor_config.json''' ) @is_staging_test class __UpperCAmelCase ( unittest.TestCase ): @classmethod def __A ( cls ) -> Tuple: A_ = TOKEN HfFolder.save_token(_a ) @classmethod def __A ( cls ) -> int: try: delete_repo(token=cls._token , repo_id='''test-feature-extractor''' ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id='''valid_org/test-feature-extractor-org''' ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id='''test-dynamic-feature-extractor''' ) except HTTPError: pass def __A ( self ) -> str: A_ = WavaVecaFeatureExtractor.from_pretrained(_a ) feature_extractor.push_to_hub('''test-feature-extractor''' , use_auth_token=self._token ) A_ = WavaVecaFeatureExtractor.from_pretrained(F'''{USER}/test-feature-extractor''' ) for k, v in feature_extractor.__dict__.items(): self.assertEqual(_a , getattr(_a , _a ) ) # Reset repo delete_repo(token=self._token , repo_id='''test-feature-extractor''' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: feature_extractor.save_pretrained( _a , repo_id='''test-feature-extractor''' , push_to_hub=_a , use_auth_token=self._token ) A_ = WavaVecaFeatureExtractor.from_pretrained(F'''{USER}/test-feature-extractor''' ) for k, v in feature_extractor.__dict__.items(): self.assertEqual(_a , getattr(_a , _a ) ) def __A ( self ) -> List[Any]: A_ = WavaVecaFeatureExtractor.from_pretrained(_a ) feature_extractor.push_to_hub('''valid_org/test-feature-extractor''' , use_auth_token=self._token ) A_ = WavaVecaFeatureExtractor.from_pretrained('''valid_org/test-feature-extractor''' ) for k, v in feature_extractor.__dict__.items(): self.assertEqual(_a , getattr(_a , _a ) ) # Reset repo delete_repo(token=self._token , repo_id='''valid_org/test-feature-extractor''' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: feature_extractor.save_pretrained( _a , repo_id='''valid_org/test-feature-extractor-org''' , push_to_hub=_a , use_auth_token=self._token ) A_ = WavaVecaFeatureExtractor.from_pretrained('''valid_org/test-feature-extractor-org''' ) for k, v in feature_extractor.__dict__.items(): self.assertEqual(_a , getattr(_a , _a ) ) def __A ( self ) -> Union[str, Any]: CustomFeatureExtractor.register_for_auto_class() A_ = CustomFeatureExtractor.from_pretrained(_a ) feature_extractor.push_to_hub('''test-dynamic-feature-extractor''' , use_auth_token=self._token ) # This has added the proper auto_map field to the config self.assertDictEqual( feature_extractor.auto_map , {'''AutoFeatureExtractor''': '''custom_feature_extraction.CustomFeatureExtractor'''} , ) A_ = AutoFeatureExtractor.from_pretrained( F'''{USER}/test-dynamic-feature-extractor''' , trust_remote_code=_a ) # Can't make an isinstance check because the new_feature_extractor is from the CustomFeatureExtractor class of a dynamic module self.assertEqual(new_feature_extractor.__class__.__name__ , '''CustomFeatureExtractor''' )
365
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging __snake_case : List[str] = logging.get_logger(__name__) __snake_case : Union[str, Any] = { 'alibaba-damo/mgp-str-base': 'https://huggingface.co/alibaba-damo/mgp-str-base/resolve/main/config.json', } class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' __lowercase : Optional[int] = 'mgp-str' def __init__( self , _SCREAMING_SNAKE_CASE=[32, 128] , _SCREAMING_SNAKE_CASE=4 , _SCREAMING_SNAKE_CASE=3 , _SCREAMING_SNAKE_CASE=27 , _SCREAMING_SNAKE_CASE=38 , _SCREAMING_SNAKE_CASE=5_0257 , _SCREAMING_SNAKE_CASE=3_0522 , _SCREAMING_SNAKE_CASE=768 , _SCREAMING_SNAKE_CASE=12 , _SCREAMING_SNAKE_CASE=12 , _SCREAMING_SNAKE_CASE=4.0 , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=False , _SCREAMING_SNAKE_CASE=1E-5 , _SCREAMING_SNAKE_CASE=0.0 , _SCREAMING_SNAKE_CASE=0.0 , _SCREAMING_SNAKE_CASE=0.0 , _SCREAMING_SNAKE_CASE=False , _SCREAMING_SNAKE_CASE=0.02 , **_SCREAMING_SNAKE_CASE , ) -> List[Any]: super().__init__(**_SCREAMING_SNAKE_CASE ) A_ = image_size A_ = patch_size A_ = num_channels A_ = max_token_length A_ = num_character_labels A_ = num_bpe_labels A_ = num_wordpiece_labels A_ = hidden_size A_ = num_hidden_layers A_ = num_attention_heads A_ = mlp_ratio A_ = distilled A_ = layer_norm_eps A_ = drop_rate A_ = qkv_bias A_ = attn_drop_rate A_ = drop_path_rate A_ = output_aa_attentions A_ = initializer_range
18
0
'''simple docstring''' import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, StableDiffusionSAGPipeline, UNetaDConditionModel, ) 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 PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class __UpperCAmelCase ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __lowercase : str = StableDiffusionSAGPipeline __lowercase : Dict = TEXT_TO_IMAGE_PARAMS __lowercase : List[Any] = TEXT_TO_IMAGE_BATCH_PARAMS __lowercase : Optional[int] = TEXT_TO_IMAGE_IMAGE_PARAMS __lowercase : List[Any] = TEXT_TO_IMAGE_IMAGE_PARAMS __lowercase : int = False def __A ( self ) -> int: torch.manual_seed(0 ) A_ = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''') , up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''') , cross_attention_dim=32 , ) A_ = DDIMScheduler( beta_start=0.00_085 , beta_end=0.012 , beta_schedule='''scaled_linear''' , clip_sample=__UpperCAmelCase , set_alpha_to_one=__UpperCAmelCase , ) torch.manual_seed(0 ) A_ = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''] , up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''] , latent_channels=4 , ) torch.manual_seed(0 ) A_ = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , ) A_ = CLIPTextModel(__UpperCAmelCase ) A_ = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) A_ = { """unet""": unet, """scheduler""": scheduler, """vae""": vae, """text_encoder""": text_encoder, """tokenizer""": tokenizer, """safety_checker""": None, """feature_extractor""": None, } return components def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=0 ) -> Dict: if str(__UpperCAmelCase ).startswith('''mps''' ): A_ = torch.manual_seed(__UpperCAmelCase ) else: A_ = torch.Generator(device=__UpperCAmelCase ).manual_seed(__UpperCAmelCase ) A_ = { """prompt""": """.""", """generator""": generator, """num_inference_steps""": 2, """guidance_scale""": 1.0, """sag_scale""": 1.0, """output_type""": """numpy""", } return inputs def __A ( self ) -> List[str]: super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) @slow @require_torch_gpu class __UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' def __A ( self ) -> Tuple: super().tearDown() gc.collect() torch.cuda.empty_cache() def __A ( self ) -> Optional[Any]: A_ = StableDiffusionSAGPipeline.from_pretrained('''CompVis/stable-diffusion-v1-4''' ) A_ = sag_pipe.to(__UpperCAmelCase ) sag_pipe.set_progress_bar_config(disable=__UpperCAmelCase ) A_ = """.""" A_ = torch.manual_seed(0 ) A_ = sag_pipe( [prompt] , generator=__UpperCAmelCase , guidance_scale=7.5 , sag_scale=1.0 , num_inference_steps=20 , output_type='''np''' ) A_ = output.images A_ = image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) A_ = np.array([0.1_568, 0.1_738, 0.1_695, 0.1_693, 0.1_507, 0.1_705, 0.1_547, 0.1_751, 0.1_949] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5E-2 def __A ( self ) -> Any: A_ = StableDiffusionSAGPipeline.from_pretrained('''stabilityai/stable-diffusion-2-1-base''' ) A_ = sag_pipe.to(__UpperCAmelCase ) sag_pipe.set_progress_bar_config(disable=__UpperCAmelCase ) A_ = """.""" A_ = torch.manual_seed(0 ) A_ = sag_pipe( [prompt] , generator=__UpperCAmelCase , guidance_scale=7.5 , sag_scale=1.0 , num_inference_steps=20 , output_type='''np''' ) A_ = output.images A_ = image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) A_ = np.array([0.3_459, 0.2_876, 0.2_537, 0.3_002, 0.2_671, 0.2_160, 0.3_026, 0.2_262, 0.2_371] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5E-2 def __A ( self ) -> List[str]: A_ = StableDiffusionSAGPipeline.from_pretrained('''stabilityai/stable-diffusion-2-1-base''' ) A_ = sag_pipe.to(__UpperCAmelCase ) sag_pipe.set_progress_bar_config(disable=__UpperCAmelCase ) A_ = """.""" A_ = torch.manual_seed(0 ) A_ = sag_pipe( [prompt] , width=768 , height=512 , generator=__UpperCAmelCase , guidance_scale=7.5 , sag_scale=1.0 , num_inference_steps=20 , output_type='''np''' , ) A_ = output.images assert image.shape == (1, 512, 768, 3)
366
'''simple docstring''' from diffusers.utils.testing_utils import require_onnxruntime @require_onnxruntime class __UpperCAmelCase : '''simple docstring''' pass
18
0
'''simple docstring''' class __UpperCAmelCase : '''simple docstring''' def __init__( self ) -> str: A_ = {} def __A ( self ) -> None: print(self.vertex ) for i in self.vertex: print(lowerCAmelCase__ , ''' -> ''' , ''' -> '''.join([str(lowerCAmelCase__ ) for j in self.vertex[i]] ) ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> None: if from_vertex in self.vertex: self.vertex[from_vertex].append(lowerCAmelCase__ ) else: # else make a new vertex A_ = [to_vertex] def __A ( self ) -> None: A_ = [False] * len(self.vertex ) # call the recursive helper function for i in range(len(self.vertex ) ): if not visited[i]: self.dfs_recursive(lowerCAmelCase__ , lowerCAmelCase__ ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> None: A_ = True print(lowerCAmelCase__ , end=''' ''' ) # Recur for all the vertices that are adjacent to this node for i in self.vertex: if not visited[i]: self.dfs_recursive(lowerCAmelCase__ , lowerCAmelCase__ ) if __name__ == "__main__": __snake_case : Dict = Graph() g.add_edge(0, 1) g.add_edge(0, 2) g.add_edge(1, 2) g.add_edge(2, 0) g.add_edge(2, 3) g.add_edge(3, 3) g.print_graph() print('DFS:') g.dfs() # OUTPUT: # 0 -> 1 -> 2 # 1 -> 2 # 2 -> 0 -> 3 # 3 -> 3 # DFS: # 0 1 2 3
367
'''simple docstring''' # Copyright 2021 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from argparse import ArgumentParser from accelerate.commands.config import get_config_parser from accelerate.commands.env import env_command_parser from accelerate.commands.launch import launch_command_parser from accelerate.commands.test import test_command_parser from accelerate.commands.tpu import tpu_command_parser def _UpperCAmelCase ( ) -> Dict: A_ = ArgumentParser('''Accelerate CLI tool''', usage='''accelerate <command> [<args>]''', allow_abbrev=_UpperCamelCase ) A_ = parser.add_subparsers(help='''accelerate command helpers''' ) # Register commands get_config_parser(subparsers=_UpperCamelCase ) env_command_parser(subparsers=_UpperCamelCase ) launch_command_parser(subparsers=_UpperCamelCase ) tpu_command_parser(subparsers=_UpperCamelCase ) test_command_parser(subparsers=_UpperCamelCase ) # Let's go A_ = parser.parse_args() if not hasattr(_UpperCamelCase, '''func''' ): parser.print_help() exit(1 ) # Run args.func(_UpperCamelCase ) if __name__ == "__main__": main()
18
0
'''simple docstring''' import io import json import unittest from parameterized import parameterized from transformers import FSMTForConditionalGeneration, FSMTTokenizer from transformers.testing_utils import get_tests_dir, require_torch, slow, torch_device from utils import calculate_bleu __snake_case : str = get_tests_dir() + '''/test_data/fsmt/fsmt_val_data.json''' with io.open(filename, 'r', encoding='utf-8') as f: __snake_case : Optional[Any] = json.load(f) @require_torch class __UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' def __A ( self , _SCREAMING_SNAKE_CASE ) -> List[str]: return FSMTTokenizer.from_pretrained(_lowerCamelCase ) def __A ( self , _SCREAMING_SNAKE_CASE ) -> Tuple: A_ = FSMTForConditionalGeneration.from_pretrained(_lowerCamelCase ).to(_lowerCamelCase ) if torch_device == "cuda": model.half() return model @parameterized.expand( [ ['''en-ru''', 26.0], ['''ru-en''', 22.0], ['''en-de''', 22.0], ['''de-en''', 29.0], ] ) @slow def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> str: # note: this test is not testing the best performance since it only evals a small batch # but it should be enough to detect a regression in the output quality A_ = F'''facebook/wmt19-{pair}''' A_ = self.get_tokenizer(_lowerCamelCase ) A_ = self.get_model(_lowerCamelCase ) A_ = bleu_data[pair]['''src'''] A_ = bleu_data[pair]['''tgt'''] A_ = tokenizer(_lowerCamelCase , return_tensors='''pt''' , truncation=_lowerCamelCase , padding='''longest''' ).to(_lowerCamelCase ) A_ = model.generate( input_ids=batch.input_ids , num_beams=8 , ) A_ = tokenizer.batch_decode( _lowerCamelCase , skip_special_tokens=_lowerCamelCase , clean_up_tokenization_spaces=_lowerCamelCase ) A_ = calculate_bleu(_lowerCamelCase , _lowerCamelCase ) print(_lowerCamelCase ) self.assertGreaterEqual(scores['''bleu'''] , _lowerCamelCase )
368
'''simple docstring''' import absl # noqa: F401 # Here to have a nice missing dependency error message early on import nltk # noqa: F401 # Here to have a nice missing dependency error message early on import numpy # noqa: F401 # Here to have a nice missing dependency error message early on import six # noqa: F401 # Here to have a nice missing dependency error message early on from rouge_score import rouge_scorer, scoring import datasets __snake_case : Any = '\\n@inproceedings{lin-2004-rouge,\n title = "{ROUGE}: A Package for Automatic Evaluation of Summaries",\n author = "Lin, Chin-Yew",\n booktitle = "Text Summarization Branches Out",\n month = jul,\n year = "2004",\n address = "Barcelona, Spain",\n publisher = "Association for Computational Linguistics",\n url = "https://www.aclweb.org/anthology/W04-1013",\n pages = "74--81",\n}\n' __snake_case : Dict = '\\nROUGE, or Recall-Oriented Understudy for Gisting Evaluation, is a set of metrics and a software package used for\nevaluating automatic summarization and machine translation software in natural language processing.\nThe metrics compare an automatically produced summary or translation against a reference or a set of references (human-produced) summary or translation.\n\nNote that ROUGE is case insensitive, meaning that upper case letters are treated the same way as lower case letters.\n\nThis metrics is a wrapper around Google Research reimplementation of ROUGE:\nhttps://github.com/google-research/google-research/tree/master/rouge\n' __snake_case : Optional[int] = '\nCalculates average rouge scores for a list of hypotheses and references\nArgs:\n predictions: list of predictions to score. Each prediction\n should be a string with tokens separated by spaces.\n references: list of reference for each prediction. Each\n reference should be a string with tokens separated by spaces.\n rouge_types: A list of rouge types to calculate.\n Valid names:\n `"rouge{n}"` (e.g. `"rouge1"`, `"rouge2"`) where: {n} is the n-gram based scoring,\n `"rougeL"`: Longest common subsequence based scoring.\n `"rougeLSum"`: rougeLsum splits text using `"\n"`.\n See details in https://github.com/huggingface/datasets/issues/617\n use_stemmer: Bool indicating whether Porter stemmer should be used to strip word suffixes.\n use_aggregator: Return aggregates if this is set to True\nReturns:\n rouge1: rouge_1 (precision, recall, f1),\n rouge2: rouge_2 (precision, recall, f1),\n rougeL: rouge_l (precision, recall, f1),\n rougeLsum: rouge_lsum (precision, recall, f1)\nExamples:\n\n >>> rouge = datasets.load_metric(\'rouge\')\n >>> predictions = ["hello there", "general kenobi"]\n >>> references = ["hello there", "general kenobi"]\n >>> results = rouge.compute(predictions=predictions, references=references)\n >>> print(list(results.keys()))\n [\'rouge1\', \'rouge2\', \'rougeL\', \'rougeLsum\']\n >>> print(results["rouge1"])\n AggregateScore(low=Score(precision=1.0, recall=1.0, fmeasure=1.0), mid=Score(precision=1.0, recall=1.0, fmeasure=1.0), high=Score(precision=1.0, recall=1.0, fmeasure=1.0))\n >>> print(results["rouge1"].mid.fmeasure)\n 1.0\n' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __UpperCAmelCase ( datasets.Metric ): '''simple docstring''' def __A ( self ) -> List[str]: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Value('''string''' , id='''sequence''' ), '''references''': datasets.Value('''string''' , id='''sequence''' ), } ) , codebase_urls=['''https://github.com/google-research/google-research/tree/master/rouge'''] , reference_urls=[ '''https://en.wikipedia.org/wiki/ROUGE_(metric)''', '''https://github.com/google-research/google-research/tree/master/rouge''', ] , ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=False ) -> Optional[int]: if rouge_types is None: A_ = ['''rouge1''', '''rouge2''', '''rougeL''', '''rougeLsum'''] A_ = rouge_scorer.RougeScorer(rouge_types=_SCREAMING_SNAKE_CASE , use_stemmer=_SCREAMING_SNAKE_CASE ) if use_aggregator: A_ = scoring.BootstrapAggregator() else: A_ = [] for ref, pred in zip(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): A_ = scorer.score(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) if use_aggregator: aggregator.add_scores(_SCREAMING_SNAKE_CASE ) else: scores.append(_SCREAMING_SNAKE_CASE ) if use_aggregator: A_ = aggregator.aggregate() else: A_ = {} for key in scores[0]: A_ = [score[key] for score in scores] return result
18
0
'''simple docstring''' import inspect import warnings from typing import Any, Dict, Optional, Union from packaging import version def _UpperCAmelCase ( *_UpperCamelCase : int, _UpperCamelCase : Optional[Union[Dict, Any]] = None, _UpperCamelCase : Any=True, _UpperCamelCase : int=2 ) -> List[Any]: from .. import __version__ A_ = take_from A_ = () if not isinstance(args[0], __SCREAMING_SNAKE_CASE ): A_ = (args,) for attribute, version_name, message in args: if version.parse(version.parse(__SCREAMING_SNAKE_CASE ).base_version ) >= version.parse(__SCREAMING_SNAKE_CASE ): raise ValueError( F'''The deprecation tuple {(attribute, version_name, message)} should be removed since diffusers\'''' F''' version {__version__} is >= {version_name}''' ) A_ = None if isinstance(__SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE ) and attribute in deprecated_kwargs: values += (deprecated_kwargs.pop(__SCREAMING_SNAKE_CASE ),) A_ = F'''The `{attribute}` argument is deprecated and will be removed in version {version_name}.''' elif hasattr(__SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE ): values += (getattr(__SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE ),) A_ = F'''The `{attribute}` attribute is deprecated and will be removed in version {version_name}.''' elif deprecated_kwargs is None: A_ = F'''`{attribute}` is deprecated and will be removed in version {version_name}.''' if warning is not None: A_ = warning + """ """ if standard_warn else """""" warnings.warn(warning + message, __SCREAMING_SNAKE_CASE, stacklevel=__SCREAMING_SNAKE_CASE ) if isinstance(__SCREAMING_SNAKE_CASE, __SCREAMING_SNAKE_CASE ) and len(__SCREAMING_SNAKE_CASE ) > 0: A_ = inspect.getouterframes(inspect.currentframe() )[1] A_ = call_frame.filename A_ = call_frame.lineno A_ = call_frame.function A_ = next(iter(deprecated_kwargs.items() ) ) raise TypeError(F'''{function} in {filename} line {line_number-1} got an unexpected keyword argument `{key}`''' ) if len(__SCREAMING_SNAKE_CASE ) == 0: return elif len(__SCREAMING_SNAKE_CASE ) == 1: return values[0] return values
369
'''simple docstring''' import gc import importlib.metadata import tempfile import unittest from packaging import version from transformers import ( AutoModel, AutoModelForCausalLM, AutoModelForSeqaSeqLM, AutoModelForSequenceClassification, AutoTokenizer, BitsAndBytesConfig, pipeline, ) from transformers.testing_utils import ( is_torch_available, require_accelerate, require_bitsandbytes, require_torch, require_torch_gpu, require_torch_multi_gpu, slow, ) def _UpperCAmelCase ( _UpperCamelCase : str ) -> int: if model.config.model_type == "gpt2": return model.transformer.h[0].mlp.c_fc return model.transformer.h[0].mlp.dense_ah_to_h if is_torch_available(): import torch import torch.nn as nn class __UpperCAmelCase ( nn.Module ): '''simple docstring''' def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Dict: super().__init__() A_ = module A_ = nn.Sequential( nn.Linear(module.in_features , _SCREAMING_SNAKE_CASE , bias=_SCREAMING_SNAKE_CASE ) , nn.Linear(_SCREAMING_SNAKE_CASE , module.out_features , bias=_SCREAMING_SNAKE_CASE ) , ) A_ = (2.0 / (5 * min(module.in_features , module.out_features ))) ** 0.5 nn.init.normal_(self.adapter[0].weight , std=_SCREAMING_SNAKE_CASE ) nn.init.zeros_(self.adapter[1].weight ) self.adapter.to(module.weight.device ) def __A ( self , _SCREAMING_SNAKE_CASE , *_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) -> Tuple: return self.module(_SCREAMING_SNAKE_CASE , *_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) + self.adapter(_SCREAMING_SNAKE_CASE ) @require_bitsandbytes @require_accelerate @require_torch @require_torch_gpu @slow class __UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' __lowercase : Dict = 'bigscience/bloom-1b7' # Constant values __lowercase : str = 2.109659552692574 __lowercase : int = 'Hello my name is' __lowercase : Optional[Any] = set() EXPECTED_OUTPUTS.add('Hello my name is John and I am a professional photographer. I' ) EXPECTED_OUTPUTS.add('Hello my name is John.\nI am a friend of your father.\n' ) EXPECTED_OUTPUTS.add('Hello my name is John Doe, I am a student at the University' ) __lowercase : Optional[Any] = 10 def __A ( self ) -> List[str]: # Models and tokenizer A_ = AutoTokenizer.from_pretrained(self.model_name ) class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' def __A ( self ) -> List[Any]: super().setUp() # Models and tokenizer A_ = AutoModelForCausalLM.from_pretrained( self.model_name , torch_dtype=torch.floataa , device_map='''auto''' ) A_ = AutoModelForCausalLM.from_pretrained(self.model_name , load_in_abit=_SCREAMING_SNAKE_CASE , device_map='''auto''' ) def __A ( self ) -> List[str]: del self.model_fpaa del self.model_abit gc.collect() torch.cuda.empty_cache() def __A ( self ) -> Tuple: A_ = self.model_abit.config self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE , '''quantization_config''' ) ) A_ = config.to_dict() A_ = config.to_diff_dict() A_ = config.to_json_string() def __A ( self ) -> Union[str, Any]: from bitsandbytes.nn import Paramsabit A_ = self.model_fpaa.get_memory_footprint() A_ = self.model_abit.get_memory_footprint() self.assertAlmostEqual(mem_fpaa / mem_abit , self.EXPECTED_RELATIVE_DIFFERENCE ) A_ = get_some_linear_layer(self.model_abit ) self.assertTrue(linear.weight.__class__ == Paramsabit ) def __A ( self ) -> Union[str, Any]: from transformers import TaPreTrainedModel self.model_fpaa.get_memory_footprint() self.model_abit.get_memory_footprint() for name, module in self.model_abit.named_modules(): if isinstance(_SCREAMING_SNAKE_CASE , torch.nn.Linear ): if name not in ["lm_head"] + TaPreTrainedModel._keep_in_fpaa_modules: # 4-bit parameters are packed in uint8 variables self.assertTrue(module.weight.dtype == torch.uinta ) def __A ( self ) -> Optional[int]: A_ = self.tokenizer(self.input_text , return_tensors='''pt''' ) A_ = self.model_abit.generate(input_ids=encoded_input['''input_ids'''].to(0 ) , max_new_tokens=10 ) self.assertIn(self.tokenizer.decode(output_sequences[0] , skip_special_tokens=_SCREAMING_SNAKE_CASE ) , self.EXPECTED_OUTPUTS ) def __A ( self ) -> Optional[int]: A_ = BitsAndBytesConfig() A_ = True A_ = AutoModelForCausalLM.from_pretrained( self.model_name , quantization_config=_SCREAMING_SNAKE_CASE , device_map='''auto''' ) A_ = self.tokenizer(self.input_text , return_tensors='''pt''' ) A_ = model_abit_from_config.generate( input_ids=encoded_input['''input_ids'''].to(0 ) , max_new_tokens=10 ) self.assertIn(self.tokenizer.decode(output_sequences[0] , skip_special_tokens=_SCREAMING_SNAKE_CASE ) , self.EXPECTED_OUTPUTS ) def __A ( self ) -> Tuple: with self.assertRaises(_SCREAMING_SNAKE_CASE ), tempfile.TemporaryDirectory() as tmpdirname: self.model_abit.save_pretrained(_SCREAMING_SNAKE_CASE ) def __A ( self ) -> Tuple: A_ = BitsAndBytesConfig() with self.assertRaises(_SCREAMING_SNAKE_CASE ): A_ = AutoModelForCausalLM.from_pretrained( self.model_name , quantization_config=_SCREAMING_SNAKE_CASE , load_in_abit=_SCREAMING_SNAKE_CASE , device_map='''auto''' , bnb_abit_quant_type='''nf4''' , ) def __A ( self ) -> Dict: with self.assertRaises(_SCREAMING_SNAKE_CASE ): # Tries with `str` self.model_abit.to('''cpu''' ) with self.assertRaises(_SCREAMING_SNAKE_CASE ): # Tries with a `dtype`` self.model_abit.to(torch.floataa ) with self.assertRaises(_SCREAMING_SNAKE_CASE ): # Tries with a `device` self.model_abit.to(torch.device('''cuda:0''' ) ) with self.assertRaises(_SCREAMING_SNAKE_CASE ): # Tries with a `device` self.model_abit.float() with self.assertRaises(_SCREAMING_SNAKE_CASE ): # Tries with a `device` self.model_abit.half() # Test if we did not break anything A_ = self.tokenizer(self.input_text , return_tensors='''pt''' ) A_ = self.model_fpaa.to(torch.floataa ) A_ = self.model_fpaa.generate(input_ids=encoded_input['''input_ids'''].to(0 ) , max_new_tokens=10 ) # Check this does not throw an error A_ = self.model_fpaa.to('''cpu''' ) # Check this does not throw an error A_ = self.model_fpaa.half() # Check this does not throw an error A_ = self.model_fpaa.float() def __A ( self ) -> Optional[int]: A_ = AutoModelForSeqaSeqLM.from_pretrained('''t5-small''' , load_in_abit=_SCREAMING_SNAKE_CASE , device_map='''auto''' ) self.assertTrue(model.decoder.block[0].layer[2].DenseReluDense.wo.weight.dtype == torch.floataa ) @require_bitsandbytes @require_accelerate @require_torch @require_torch_gpu @slow class __UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' @classmethod def __A ( cls ) -> Optional[Any]: A_ = '''t5-small''' A_ = '''google/flan-t5-small''' # flan-t5 uses dense-act instead of dense-relu-dense A_ = AutoTokenizer.from_pretrained(cls.model_name ) A_ = '''Translate in German: Hello, my dog is cute''' def __A ( self ) -> Any: gc.collect() torch.cuda.empty_cache() def __A ( self ) -> Tuple: from transformers import TaForConditionalGeneration A_ = TaForConditionalGeneration._keep_in_fpaa_modules A_ = None # test with `t5-small` A_ = TaForConditionalGeneration.from_pretrained(self.model_name , load_in_abit=_SCREAMING_SNAKE_CASE , device_map='''auto''' ) A_ = self.tokenizer(self.input_text , return_tensors='''pt''' ).to(0 ) A_ = model.generate(**_SCREAMING_SNAKE_CASE ) # test with `flan-t5-small` A_ = TaForConditionalGeneration.from_pretrained( self.dense_act_model_name , load_in_abit=_SCREAMING_SNAKE_CASE , device_map='''auto''' ) A_ = self.tokenizer(self.input_text , return_tensors='''pt''' ).to(0 ) A_ = model.generate(**_SCREAMING_SNAKE_CASE ) A_ = modules def __A ( self ) -> Dict: import bitsandbytes as bnb from transformers import TaForConditionalGeneration # test with `t5-small` A_ = TaForConditionalGeneration.from_pretrained(self.model_name , load_in_abit=_SCREAMING_SNAKE_CASE , device_map='''auto''' ) # there was a bug with decoders - this test checks that it is fixed self.assertTrue(isinstance(model.decoder.block[0].layer[0].SelfAttention.q , bnb.nn.Linearabit ) ) A_ = self.tokenizer(self.input_text , return_tensors='''pt''' ).to(0 ) A_ = model.generate(**_SCREAMING_SNAKE_CASE ) # test with `flan-t5-small` A_ = TaForConditionalGeneration.from_pretrained( self.dense_act_model_name , load_in_abit=_SCREAMING_SNAKE_CASE , device_map='''auto''' ) A_ = self.tokenizer(self.input_text , return_tensors='''pt''' ).to(0 ) A_ = model.generate(**_SCREAMING_SNAKE_CASE ) class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' def __A ( self ) -> int: super().setUp() # model_name A_ = '''bigscience/bloom-560m''' A_ = '''t5-small''' # Different types of model A_ = AutoModel.from_pretrained(self.model_name , load_in_abit=_SCREAMING_SNAKE_CASE , device_map='''auto''' ) # Sequence classification model A_ = AutoModelForSequenceClassification.from_pretrained( self.model_name , load_in_abit=_SCREAMING_SNAKE_CASE , device_map='''auto''' ) # CausalLM model A_ = AutoModelForCausalLM.from_pretrained(self.model_name , load_in_abit=_SCREAMING_SNAKE_CASE , device_map='''auto''' ) # Seq2seq model A_ = AutoModelForSeqaSeqLM.from_pretrained( self.seq_to_seq_name , load_in_abit=_SCREAMING_SNAKE_CASE , device_map='''auto''' ) def __A ( self ) -> Union[str, Any]: del self.base_model del self.sequence_model del self.model_abit del self.seq_to_seq_model gc.collect() torch.cuda.empty_cache() def __A ( self ) -> List[str]: from bitsandbytes.nn import Paramsabit self.assertTrue(self.base_model.h[-1].mlp.dense_ah_to_h.weight.__class__ == Paramsabit ) # Other heads should be nn.Parameter self.assertTrue(self.model_abit.lm_head.weight.__class__ == torch.nn.Parameter ) self.assertTrue(self.sequence_model.score.weight.__class__ == torch.nn.Parameter ) self.assertTrue(self.seq_to_seq_model.lm_head.weight.__class__ == torch.nn.Parameter ) class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' def __A ( self ) -> Tuple: super().setUp() def __A ( self ) -> List[Any]: del self.pipe gc.collect() torch.cuda.empty_cache() def __A ( self ) -> Optional[Any]: A_ = pipeline( '''text-generation''' , model=self.model_name , model_kwargs={'''device_map''': '''auto''', '''load_in_4bit''': True, '''torch_dtype''': torch.floataa} , max_new_tokens=self.MAX_NEW_TOKENS , ) # Real second forward pass A_ = self.pipe(self.input_text ) self.assertIn(pipeline_output[0]['''generated_text'''] , self.EXPECTED_OUTPUTS ) @require_torch_multi_gpu class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' def __A ( self ) -> List[str]: super().setUp() def __A ( self ) -> Optional[int]: A_ = AutoModelForCausalLM.from_pretrained( self.model_name , load_in_abit=_SCREAMING_SNAKE_CASE , device_map='''balanced''' ) # Check correct device map self.assertEqual(set(model_parallel.hf_device_map.values() ) , {0, 1} ) # Check that inference pass works on the model A_ = self.tokenizer(self.input_text , return_tensors='''pt''' ) # Second real batch A_ = model_parallel.generate(input_ids=encoded_input['''input_ids'''].to(0 ) , max_new_tokens=10 ) self.assertIn(self.tokenizer.decode(output_parallel[0] , skip_special_tokens=_SCREAMING_SNAKE_CASE ) , self.EXPECTED_OUTPUTS ) class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' def __A ( self ) -> str: A_ = '''facebook/opt-350m''' super().setUp() def __A ( self ) -> Optional[int]: if version.parse(importlib.metadata.version('''bitsandbytes''' ) ) < version.parse('''0.37.0''' ): return # Step 1: freeze all parameters A_ = AutoModelForCausalLM.from_pretrained(self.model_name , load_in_abit=_SCREAMING_SNAKE_CASE ) self.assertEqual(set(model.hf_device_map.values() ) , {torch.cuda.current_device()} ) for param in model.parameters(): A_ = False # freeze the model - train adapters later if param.ndim == 1: # cast the small parameters (e.g. layernorm) to fp32 for stability A_ = param.data.to(torch.floataa ) # Step 2: add adapters for _, module in model.named_modules(): if "OPTAttention" in repr(type(_SCREAMING_SNAKE_CASE ) ): A_ = LoRALayer(module.q_proj , rank=16 ) A_ = LoRALayer(module.k_proj , rank=16 ) A_ = LoRALayer(module.v_proj , rank=16 ) # Step 3: dummy batch A_ = self.tokenizer('''Test batch ''' , return_tensors='''pt''' ).to(0 ) # Step 4: Check if the gradient is not None with torch.cuda.amp.autocast(): A_ = model.forward(**_SCREAMING_SNAKE_CASE ) out.logits.norm().backward() for module in model.modules(): if isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): self.assertTrue(module.adapter[1].weight.grad is not None ) self.assertTrue(module.adapter[1].weight.grad.norm().item() > 0 ) elif isinstance(_SCREAMING_SNAKE_CASE , nn.Embedding ): self.assertTrue(module.weight.grad is None ) class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' __lowercase : int = 'gpt2-xl' __lowercase : List[Any] = 3.3191854854152187
18
0
'''simple docstring''' import unittest from transformers import SqueezeBertConfig, is_torch_available from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST, SqueezeBertForMaskedLM, SqueezeBertForMultipleChoice, SqueezeBertForQuestionAnswering, SqueezeBertForSequenceClassification, SqueezeBertForTokenClassification, SqueezeBertModel, ) class __UpperCAmelCase ( _UpperCAmelCase ): '''simple docstring''' def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=13 , _SCREAMING_SNAKE_CASE=7 , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=False , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=99 , _SCREAMING_SNAKE_CASE=32 , _SCREAMING_SNAKE_CASE=5 , _SCREAMING_SNAKE_CASE=4 , _SCREAMING_SNAKE_CASE=64 , _SCREAMING_SNAKE_CASE="gelu" , _SCREAMING_SNAKE_CASE=0.1 , _SCREAMING_SNAKE_CASE=0.1 , _SCREAMING_SNAKE_CASE=512 , _SCREAMING_SNAKE_CASE=16 , _SCREAMING_SNAKE_CASE=2 , _SCREAMING_SNAKE_CASE=0.02 , _SCREAMING_SNAKE_CASE=3 , _SCREAMING_SNAKE_CASE=4 , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE=2 , _SCREAMING_SNAKE_CASE=2 , _SCREAMING_SNAKE_CASE=2 , _SCREAMING_SNAKE_CASE=2 , _SCREAMING_SNAKE_CASE=4 , _SCREAMING_SNAKE_CASE=1 , ) -> int: A_ = parent A_ = batch_size A_ = seq_length A_ = is_training A_ = use_input_mask A_ = use_token_type_ids A_ = use_labels A_ = vocab_size A_ = hidden_size A_ = num_hidden_layers A_ = num_attention_heads A_ = intermediate_size A_ = hidden_act A_ = hidden_dropout_prob A_ = attention_probs_dropout_prob A_ = max_position_embeddings A_ = type_vocab_size A_ = type_sequence_label_size A_ = initializer_range A_ = num_labels A_ = num_choices A_ = scope A_ = q_groups A_ = k_groups A_ = v_groups A_ = post_attention_groups A_ = intermediate_groups A_ = output_groups def __A ( self ) -> Dict: A_ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) A_ = None if self.use_input_mask: A_ = random_attention_mask([self.batch_size, self.seq_length] ) A_ = None A_ = None A_ = None if self.use_labels: A_ = ids_tensor([self.batch_size] , self.type_sequence_label_size ) A_ = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) A_ = ids_tensor([self.batch_size] , self.num_choices ) A_ = self.get_config() return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels def __A ( self ) -> Optional[Any]: return SqueezeBertConfig( embedding_size=self.hidden_size , 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 , attention_probs_dropout_prob=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , q_groups=self.q_groups , k_groups=self.k_groups , v_groups=self.v_groups , post_attention_groups=self.post_attention_groups , intermediate_groups=self.intermediate_groups , output_groups=self.output_groups , ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Union[str, Any]: A_ = SqueezeBertModel(config=lowercase_ ) model.to(lowercase_ ) model.eval() A_ = model(lowercase_ , lowercase_ ) A_ = model(lowercase_ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Dict: A_ = SqueezeBertForMaskedLM(config=lowercase_ ) model.to(lowercase_ ) model.eval() A_ = model(lowercase_ , attention_mask=lowercase_ , labels=lowercase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> int: A_ = SqueezeBertForQuestionAnswering(config=lowercase_ ) model.to(lowercase_ ) model.eval() A_ = model( lowercase_ , attention_mask=lowercase_ , start_positions=lowercase_ , end_positions=lowercase_ ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Union[str, Any]: A_ = self.num_labels A_ = SqueezeBertForSequenceClassification(lowercase_ ) model.to(lowercase_ ) model.eval() A_ = model(lowercase_ , attention_mask=lowercase_ , labels=lowercase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Optional[int]: A_ = self.num_labels A_ = SqueezeBertForTokenClassification(config=lowercase_ ) model.to(lowercase_ ) model.eval() A_ = model(lowercase_ , attention_mask=lowercase_ , labels=lowercase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Union[str, Any]: A_ = self.num_choices A_ = SqueezeBertForMultipleChoice(config=lowercase_ ) model.to(lowercase_ ) model.eval() A_ = input_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() A_ = input_mask.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() A_ = model( lowercase_ , attention_mask=lowercase_ , labels=lowercase_ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def __A ( self ) -> Tuple: A_ = self.prepare_config_and_inputs() (A_) = config_and_inputs A_ = {"""input_ids""": input_ids, """attention_mask""": input_mask} return config, inputs_dict @require_torch class __UpperCAmelCase ( _UpperCAmelCase , _UpperCAmelCase , unittest.TestCase ): '''simple docstring''' __lowercase : List[str] = ( ( SqueezeBertModel, SqueezeBertForMaskedLM, SqueezeBertForMultipleChoice, SqueezeBertForQuestionAnswering, SqueezeBertForSequenceClassification, SqueezeBertForTokenClassification, ) if is_torch_available() else None ) __lowercase : List[Any] = ( { 'feature-extraction': SqueezeBertModel, 'fill-mask': SqueezeBertForMaskedLM, 'question-answering': SqueezeBertForQuestionAnswering, 'text-classification': SqueezeBertForSequenceClassification, 'token-classification': SqueezeBertForTokenClassification, 'zero-shot': SqueezeBertForSequenceClassification, } if is_torch_available() else {} ) __lowercase : List[str] = False __lowercase : List[str] = True __lowercase : List[str] = False def __A ( self ) -> Any: A_ = SqueezeBertModelTester(self ) A_ = ConfigTester(self , config_class=lowercase_ , dim=37 ) def __A ( self ) -> Tuple: self.config_tester.run_common_tests() def __A ( self ) -> Union[str, Any]: A_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_squeezebert_model(*lowercase_ ) def __A ( self ) -> str: A_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_squeezebert_for_masked_lm(*lowercase_ ) def __A ( self ) -> str: A_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_squeezebert_for_question_answering(*lowercase_ ) def __A ( self ) -> Tuple: A_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_squeezebert_for_sequence_classification(*lowercase_ ) def __A ( self ) -> Optional[Any]: A_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_squeezebert_for_token_classification(*lowercase_ ) def __A ( self ) -> str: A_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_squeezebert_for_multiple_choice(*lowercase_ ) @slow def __A ( self ) -> List[str]: for model_name in SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A_ = SqueezeBertModel.from_pretrained(lowercase_ ) self.assertIsNotNone(lowercase_ ) @require_sentencepiece @require_tokenizers @require_torch class __UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' @slow def __A ( self ) -> List[Any]: A_ = SqueezeBertForSequenceClassification.from_pretrained('''squeezebert/squeezebert-mnli''' ) A_ = torch.tensor([[1, 2_9414, 232, 328, 740, 1140, 1_2695, 69, 13, 1588, 2]] ) A_ = model(lowercase_ )[0] A_ = torch.Size((1, 3) ) self.assertEqual(output.shape , lowercase_ ) A_ = torch.tensor([[0.6_401, -0.0_349, -0.6_041]] ) self.assertTrue(torch.allclose(lowercase_ , lowercase_ , atol=1E-4 ) )
370
'''simple docstring''' import os from typing import BinaryIO, Optional, Union import numpy as np import pyarrow.parquet as pq from .. import Audio, Dataset, Features, Image, NamedSplit, Value, config from ..features.features import FeatureType, _visit from ..formatting import query_table from ..packaged_modules import _PACKAGED_DATASETS_MODULES from ..packaged_modules.parquet.parquet import Parquet from ..utils import logging from ..utils.typing import NestedDataStructureLike, PathLike from .abc import AbstractDatasetReader def _UpperCAmelCase ( _UpperCamelCase : Features ) -> Optional[int]: A_ = np.inf def set_batch_size(_UpperCamelCase : FeatureType ) -> None: nonlocal batch_size if isinstance(_UpperCamelCase, _UpperCamelCase ): A_ = min(_UpperCamelCase, config.PARQUET_ROW_GROUP_SIZE_FOR_IMAGE_DATASETS ) elif isinstance(_UpperCamelCase, _UpperCamelCase ): A_ = min(_UpperCamelCase, config.PARQUET_ROW_GROUP_SIZE_FOR_AUDIO_DATASETS ) elif isinstance(_UpperCamelCase, _UpperCamelCase ) and feature.dtype == "binary": A_ = min(_UpperCamelCase, config.PARQUET_ROW_GROUP_SIZE_FOR_BINARY_DATASETS ) _visit(_UpperCamelCase, _UpperCamelCase ) return None if batch_size is np.inf else batch_size class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = False , _SCREAMING_SNAKE_CASE = False , _SCREAMING_SNAKE_CASE = None , **_SCREAMING_SNAKE_CASE , ) -> int: super().__init__( _SCREAMING_SNAKE_CASE , split=_SCREAMING_SNAKE_CASE , 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 , ) A_ = path_or_paths if isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) else {self.split: path_or_paths} A_ = _PACKAGED_DATASETS_MODULES['''parquet'''][1] A_ = Parquet( cache_dir=_SCREAMING_SNAKE_CASE , data_files=_SCREAMING_SNAKE_CASE , features=_SCREAMING_SNAKE_CASE , hash=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE , ) def __A ( self ) -> str: # Build iterable dataset if self.streaming: A_ = self.builder.as_streaming_dataset(split=self.split ) # Build regular (map-style) dataset else: A_ = None A_ = None A_ = None A_ = 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 , ) A_ = self.builder.as_dataset( split=self.split , verification_mode=_SCREAMING_SNAKE_CASE , in_memory=self.keep_in_memory ) return dataset class __UpperCAmelCase : '''simple docstring''' def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None , **_SCREAMING_SNAKE_CASE , ) -> Dict: A_ = dataset A_ = path_or_buf A_ = batch_size or get_writer_batch_size(dataset.features ) A_ = parquet_writer_kwargs def __A ( self ) -> int: A_ = self.batch_size if self.batch_size else config.DEFAULT_MAX_BATCH_SIZE if isinstance(self.path_or_buf , (str, bytes, os.PathLike) ): with open(self.path_or_buf , '''wb+''' ) as buffer: A_ = self._write(file_obj=_SCREAMING_SNAKE_CASE , batch_size=_SCREAMING_SNAKE_CASE , **self.parquet_writer_kwargs ) else: A_ = self._write(file_obj=self.path_or_buf , batch_size=_SCREAMING_SNAKE_CASE , **self.parquet_writer_kwargs ) return written def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) -> int: A_ = 0 A_ = parquet_writer_kwargs.pop('''path_or_buf''' , _SCREAMING_SNAKE_CASE ) A_ = self.dataset.features.arrow_schema A_ = pq.ParquetWriter(_SCREAMING_SNAKE_CASE , schema=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) for offset in logging.tqdm( range(0 , len(self.dataset ) , _SCREAMING_SNAKE_CASE ) , unit='''ba''' , disable=not logging.is_progress_bar_enabled() , desc='''Creating parquet from Arrow format''' , ): A_ = query_table( table=self.dataset._data , key=slice(_SCREAMING_SNAKE_CASE , offset + batch_size ) , indices=self.dataset._indices if self.dataset._indices is not None else None , ) writer.write_table(_SCREAMING_SNAKE_CASE ) written += batch.nbytes writer.close() return written
18
0
'''simple docstring''' import pytest from datasets import inspect_metric, list_metrics, load_metric @pytest.fixture def _UpperCAmelCase ( _UpperCamelCase : Tuple ) -> Optional[int]: monkeypatch.setattr('''datasets.utils.deprecation_utils._emitted_deprecation_warnings''', set() ) @pytest.fixture def _UpperCAmelCase ( _UpperCamelCase : int ) -> Optional[Any]: class __UpperCAmelCase : '''simple docstring''' def __init__( self , _SCREAMING_SNAKE_CASE ) -> List[str]: A_ = metric_id class __UpperCAmelCase : '''simple docstring''' __lowercase : Any = [MetricMock(__lowercase ) for metric_id in ['accuracy', 'mse', 'precision', 'codeparrot/apps_metric']] def __A ( self ) -> Tuple: return self._metrics monkeypatch.setattr('''datasets.inspect.huggingface_hub''', HfhMock() ) @pytest.mark.parametrize( '''func, args''', [(load_metric, ('''metrics/mse''',)), (list_metrics, ()), (inspect_metric, ('''metrics/mse''', '''tmp_path'''))] ) def _UpperCAmelCase ( _UpperCamelCase : str, _UpperCamelCase : Tuple, _UpperCamelCase : Tuple, _UpperCamelCase : int, _UpperCamelCase : Union[str, Any] ) -> Optional[Any]: if "tmp_path" in args: A_ = tuple(arg if arg != '''tmp_path''' else tmp_path for arg in args ) with pytest.warns(__a, match='''https://huggingface.co/docs/evaluate''' ): func(*__a )
371
'''simple docstring''' from statistics import mean, stdev def _UpperCAmelCase ( _UpperCamelCase : list, _UpperCamelCase : int = 3 ) -> list: A_ = min(_UpperCamelCase ) A_ = max(_UpperCamelCase ) # normalize data return [round((x - x_min) / (x_max - x_min), _UpperCamelCase ) for x in data] def _UpperCAmelCase ( _UpperCamelCase : list, _UpperCamelCase : int = 3 ) -> list: A_ = mean(_UpperCamelCase ) A_ = stdev(_UpperCamelCase ) # standardize data return [round((x - mu) / (sigma), _UpperCamelCase ) for x in data]
18
0
'''simple docstring''' import sys import turtle def _UpperCAmelCase ( _UpperCamelCase : str, _UpperCamelCase : int ) -> Optional[Any]: return (pa[0] + pa[0]) / 2, (pa[1] + pa[1]) / 2 def _UpperCAmelCase ( _UpperCamelCase : str, _UpperCamelCase : Any, _UpperCamelCase : List[Any], _UpperCamelCase : Tuple, ) -> str: my_pen.up() my_pen.goto(vertexa[0], vertexa[1] ) my_pen.down() my_pen.goto(vertexa[0], vertexa[1] ) my_pen.goto(vertexa[0], vertexa[1] ) my_pen.goto(vertexa[0], vertexa[1] ) if depth == 0: return triangle(__lowerCamelCase, get_mid(__lowerCamelCase, __lowerCamelCase ), get_mid(__lowerCamelCase, __lowerCamelCase ), depth - 1 ) triangle(__lowerCamelCase, get_mid(__lowerCamelCase, __lowerCamelCase ), get_mid(__lowerCamelCase, __lowerCamelCase ), depth - 1 ) triangle(__lowerCamelCase, get_mid(__lowerCamelCase, __lowerCamelCase ), get_mid(__lowerCamelCase, __lowerCamelCase ), depth - 1 ) if __name__ == "__main__": if len(sys.argv) != 2: raise ValueError( 'Correct format for using this script: ' 'python fractals.py <int:depth_for_fractal>' ) __snake_case : List[Any] = turtle.Turtle() my_pen.ht() my_pen.speed(5) my_pen.pencolor('red') __snake_case : Optional[int] = [(-175, -125), (0, 175), (175, -125)] # vertices of triangle triangle(vertices[0], vertices[1], vertices[2], int(sys.argv[1]))
350
'''simple docstring''' import logging import os import sys from dataclasses import dataclass, field from typing import Optional import evaluate import numpy as np import torch from datasets import load_dataset from PIL import Image from torchvision.transforms import ( CenterCrop, Compose, Normalize, RandomHorizontalFlip, RandomResizedCrop, Resize, ToTensor, ) import transformers from transformers import ( MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING, AutoConfig, AutoImageProcessor, AutoModelForImageClassification, HfArgumentParser, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import get_last_checkpoint from transformers.utils import check_min_version, send_example_telemetry from transformers.utils.versions import require_version __snake_case : Optional[int] = logging.getLogger(__name__) # Will error if the minimal version of Transformers is not installed. Remove at your own risks. check_min_version('4.31.0') require_version('datasets>=1.8.0', 'To fix: pip install -r examples/pytorch/image-classification/requirements.txt') __snake_case : str = list(MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING.keys()) __snake_case : int = tuple(conf.model_type for conf in MODEL_CONFIG_CLASSES) def _UpperCAmelCase ( _UpperCamelCase : str ) -> int: with open(_UpperCamelCase, '''rb''' ) as f: A_ = Image.open(_UpperCamelCase ) return im.convert('''RGB''' ) @dataclass class __UpperCAmelCase : '''simple docstring''' __lowercase : Optional[str] = field( default=_UpperCamelCase , metadata={ 'help': 'Name of a dataset from the hub (could be your own, possibly private dataset hosted on the hub).' } , ) __lowercase : Optional[str] = field( default=_UpperCamelCase , metadata={'help': 'The configuration name of the dataset to use (via the datasets library).'} ) __lowercase : Optional[str] = field(default=_UpperCamelCase , metadata={'help': 'A folder containing the training data.'} ) __lowercase : Optional[str] = field(default=_UpperCamelCase , metadata={'help': 'A folder containing the validation data.'} ) __lowercase : Optional[float] = field( default=0.15 , metadata={'help': 'Percent to split off of train for validation.'} ) __lowercase : Optional[int] = field( default=_UpperCamelCase , metadata={ 'help': ( 'For debugging purposes or quicker training, truncate the number of training examples to this ' 'value if set.' ) } , ) __lowercase : Optional[int] = field( default=_UpperCamelCase , metadata={ 'help': ( 'For debugging purposes or quicker training, truncate the number of evaluation examples to this ' 'value if set.' ) } , ) def __A ( self ) -> int: if self.dataset_name is None and (self.train_dir is None and self.validation_dir is None): raise ValueError( '''You must specify either a dataset name from the hub or a train and/or validation directory.''' ) @dataclass class __UpperCAmelCase : '''simple docstring''' __lowercase : str = field( default='google/vit-base-patch16-224-in21k' , metadata={'help': 'Path to pretrained model or model identifier from huggingface.co/models'} , ) __lowercase : Optional[str] = field( default=_UpperCamelCase , metadata={'help': 'If training from scratch, pass a model type from the list: ' + ', '.join(_UpperCamelCase )} , ) __lowercase : Optional[str] = field( default=_UpperCamelCase , metadata={'help': 'Pretrained config name or path if not the same as model_name'} ) __lowercase : Optional[str] = field( default=_UpperCamelCase , metadata={'help': 'Where do you want to store the pretrained models downloaded from s3'} ) __lowercase : str = field( default='main' , metadata={'help': 'The specific model version to use (can be a branch name, tag name or commit id).'} , ) __lowercase : str = field(default=_UpperCamelCase , metadata={'help': 'Name or path of preprocessor config.'} ) __lowercase : bool = field( default=_UpperCamelCase , metadata={ 'help': ( 'Will use the token generated when running `huggingface-cli login` (necessary to use this script ' 'with private models).' ) } , ) __lowercase : bool = field( default=_UpperCamelCase , metadata={'help': 'Will enable to load a pretrained model whose head dimensions are different.'} , ) def _UpperCAmelCase ( _UpperCamelCase : str ) -> Dict: A_ = torch.stack([example['''pixel_values'''] for example in examples] ) A_ = torch.tensor([example['''labels'''] for example in examples] ) return {"pixel_values": pixel_values, "labels": labels} def _UpperCAmelCase ( ) -> Tuple: # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. A_ = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith('''.json''' ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. A_ ,A_ ,A_ = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: A_ ,A_ ,A_ = parser.parse_args_into_dataclasses() # Sending telemetry. Tracking the example usage helps us better allocate resources to maintain them. The # information sent is the one passed as arguments along with your Python/PyTorch versions. send_example_telemetry('''run_image_classification''', _UpperCamelCase, _UpperCamelCase ) # Setup logging logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''', datefmt='''%m/%d/%Y %H:%M:%S''', handlers=[logging.StreamHandler(sys.stdout )], ) if training_args.should_log: # The default of training_args.log_level is passive, so we set log level at info here to have that default. transformers.utils.logging.set_verbosity_info() A_ = training_args.get_process_log_level() logger.setLevel(_UpperCamelCase ) transformers.utils.logging.set_verbosity(_UpperCamelCase ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Log on each process the small summary: logger.warning( F'''Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}''' + F'''distributed training: {bool(training_args.local_rank != -1 )}, 16-bits training: {training_args.fpaa}''' ) logger.info(F'''Training/evaluation parameters {training_args}''' ) # Detecting last checkpoint. A_ = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: A_ = get_last_checkpoint(training_args.output_dir ) if last_checkpoint is None and len(os.listdir(training_args.output_dir ) ) > 0: raise ValueError( F'''Output directory ({training_args.output_dir}) already exists and is not empty. ''' '''Use --overwrite_output_dir to overcome.''' ) elif last_checkpoint is not None and training_args.resume_from_checkpoint is None: logger.info( F'''Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change ''' '''the `--output_dir` or add `--overwrite_output_dir` to train from scratch.''' ) # Set seed before initializing model. set_seed(training_args.seed ) # Initialize our dataset and prepare it for the 'image-classification' task. if data_args.dataset_name is not None: A_ = load_dataset( data_args.dataset_name, data_args.dataset_config_name, cache_dir=model_args.cache_dir, task='''image-classification''', use_auth_token=True if model_args.use_auth_token else None, ) else: A_ = {} if data_args.train_dir is not None: A_ = os.path.join(data_args.train_dir, '''**''' ) if data_args.validation_dir is not None: A_ = os.path.join(data_args.validation_dir, '''**''' ) A_ = load_dataset( '''imagefolder''', data_files=_UpperCamelCase, cache_dir=model_args.cache_dir, task='''image-classification''', ) # If we don't have a validation split, split off a percentage of train as validation. A_ = None if '''validation''' in dataset.keys() else data_args.train_val_split if isinstance(data_args.train_val_split, _UpperCamelCase ) and data_args.train_val_split > 0.0: A_ = dataset['''train'''].train_test_split(data_args.train_val_split ) A_ = split['''train'''] A_ = split['''test'''] # Prepare label mappings. # We'll include these in the model's config to get human readable labels in the Inference API. A_ = dataset['''train'''].features['''labels'''].names A_ ,A_ = {}, {} for i, label in enumerate(_UpperCamelCase ): A_ = str(_UpperCamelCase ) A_ = label # Load the accuracy metric from the datasets package A_ = evaluate.load('''accuracy''' ) # Define our compute_metrics function. It takes an `EvalPrediction` object (a namedtuple with a # predictions and label_ids field) and has to return a dictionary string to float. def compute_metrics(_UpperCamelCase : Optional[Any] ): return metric.compute(predictions=np.argmax(p.predictions, axis=1 ), references=p.label_ids ) A_ = AutoConfig.from_pretrained( model_args.config_name or model_args.model_name_or_path, num_labels=len(_UpperCamelCase ), labelaid=_UpperCamelCase, idalabel=_UpperCamelCase, finetuning_task='''image-classification''', cache_dir=model_args.cache_dir, revision=model_args.model_revision, use_auth_token=True if model_args.use_auth_token else None, ) A_ = AutoModelForImageClassification.from_pretrained( model_args.model_name_or_path, from_tf=bool('''.ckpt''' in model_args.model_name_or_path ), config=_UpperCamelCase, cache_dir=model_args.cache_dir, revision=model_args.model_revision, use_auth_token=True if model_args.use_auth_token else None, ignore_mismatched_sizes=model_args.ignore_mismatched_sizes, ) A_ = AutoImageProcessor.from_pretrained( model_args.image_processor_name or model_args.model_name_or_path, cache_dir=model_args.cache_dir, revision=model_args.model_revision, use_auth_token=True if model_args.use_auth_token else None, ) # Define torchvision transforms to be applied to each image. if "shortest_edge" in image_processor.size: A_ = image_processor.size['''shortest_edge'''] else: A_ = (image_processor.size['''height'''], image_processor.size['''width''']) A_ = Normalize(mean=image_processor.image_mean, std=image_processor.image_std ) A_ = Compose( [ RandomResizedCrop(_UpperCamelCase ), RandomHorizontalFlip(), ToTensor(), normalize, ] ) A_ = Compose( [ Resize(_UpperCamelCase ), CenterCrop(_UpperCamelCase ), ToTensor(), normalize, ] ) def train_transforms(_UpperCamelCase : Dict ): A_ = [ _train_transforms(pil_img.convert('''RGB''' ) ) for pil_img in example_batch['''image'''] ] return example_batch def val_transforms(_UpperCamelCase : Any ): A_ = [_val_transforms(pil_img.convert('''RGB''' ) ) for pil_img in example_batch['''image''']] return example_batch if training_args.do_train: if "train" not in dataset: raise ValueError('''--do_train requires a train dataset''' ) if data_args.max_train_samples is not None: A_ = ( dataset['''train'''].shuffle(seed=training_args.seed ).select(range(data_args.max_train_samples ) ) ) # Set the training transforms dataset["train"].set_transform(_UpperCamelCase ) if training_args.do_eval: if "validation" not in dataset: raise ValueError('''--do_eval requires a validation dataset''' ) if data_args.max_eval_samples is not None: A_ = ( dataset['''validation'''].shuffle(seed=training_args.seed ).select(range(data_args.max_eval_samples ) ) ) # Set the validation transforms dataset["validation"].set_transform(_UpperCamelCase ) # Initalize our trainer A_ = Trainer( model=_UpperCamelCase, args=_UpperCamelCase, train_dataset=dataset['''train'''] if training_args.do_train else None, eval_dataset=dataset['''validation'''] if training_args.do_eval else None, compute_metrics=_UpperCamelCase, tokenizer=_UpperCamelCase, data_collator=_UpperCamelCase, ) # Training if training_args.do_train: A_ = None if training_args.resume_from_checkpoint is not None: A_ = training_args.resume_from_checkpoint elif last_checkpoint is not None: A_ = last_checkpoint A_ = trainer.train(resume_from_checkpoint=_UpperCamelCase ) trainer.save_model() trainer.log_metrics('''train''', train_result.metrics ) trainer.save_metrics('''train''', train_result.metrics ) trainer.save_state() # Evaluation if training_args.do_eval: A_ = trainer.evaluate() trainer.log_metrics('''eval''', _UpperCamelCase ) trainer.save_metrics('''eval''', _UpperCamelCase ) # Write model card and (optionally) push to hub A_ = { '''finetuned_from''': model_args.model_name_or_path, '''tasks''': '''image-classification''', '''dataset''': data_args.dataset_name, '''tags''': ['''image-classification''', '''vision'''], } if training_args.push_to_hub: trainer.push_to_hub(**_UpperCamelCase ) else: trainer.create_model_card(**_UpperCamelCase ) if __name__ == "__main__": main()
18
0
'''simple docstring''' 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, ) __snake_case : Optional[Any] = {"""configuration_xglm""": ["""XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP""", """XGLMConfig"""]} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case : str = ["""XGLMTokenizer"""] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case : Dict = ["""XGLMTokenizerFast"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case : Union[str, Any] = [ """XGLM_PRETRAINED_MODEL_ARCHIVE_LIST""", """XGLMForCausalLM""", """XGLMModel""", """XGLMPreTrainedModel""", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case : int = [ """FlaxXGLMForCausalLM""", """FlaxXGLMModel""", """FlaxXGLMPreTrainedModel""", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case : int = [ """TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST""", """TFXGLMForCausalLM""", """TFXGLMModel""", """TFXGLMPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_xglm import XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XGLMConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xglm import XGLMTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xglm_fast import XGLMTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xglm import XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, XGLMForCausalLM, XGLMModel, XGLMPreTrainedModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_xglm import FlaxXGLMForCausalLM, FlaxXGLMModel, FlaxXGLMPreTrainedModel try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_xglm import ( TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFXGLMForCausalLM, TFXGLMModel, TFXGLMPreTrainedModel, ) else: import sys __snake_case : Optional[Any] = _LazyModule(__name__, globals()['__file__'], _import_structure)
351
'''simple docstring''' import tempfile import unittest import numpy as np from huggingface_hub import HfFolder, delete_repo from requests.exceptions import HTTPError from transformers import BertConfig, is_flax_available from transformers.testing_utils import TOKEN, USER, is_staging_test, require_flax if is_flax_available(): import os from flax.core.frozen_dict import unfreeze from flax.traverse_util import flatten_dict from transformers import FlaxBertModel __snake_case : str = '0.12' # assumed parallelism: 8 @require_flax @is_staging_test class __UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' @classmethod def __A ( cls ) -> Dict: A_ = TOKEN HfFolder.save_token(_SCREAMING_SNAKE_CASE ) @classmethod def __A ( cls ) -> Optional[int]: try: delete_repo(token=cls._token , repo_id='''test-model-flax''' ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id='''valid_org/test-model-flax-org''' ) except HTTPError: pass def __A ( self ) -> str: A_ = BertConfig( vocab_size=99 , hidden_size=32 , num_hidden_layers=5 , num_attention_heads=4 , intermediate_size=37 ) A_ = FlaxBertModel(_SCREAMING_SNAKE_CASE ) model.push_to_hub('''test-model-flax''' , use_auth_token=self._token ) A_ = FlaxBertModel.from_pretrained(F'''{USER}/test-model-flax''' ) A_ = flatten_dict(unfreeze(model.params ) ) A_ = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): A_ = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(_SCREAMING_SNAKE_CASE , 1E-3 , msg=F'''{key} not identical''' ) # Reset repo delete_repo(token=self._token , repo_id='''test-model-flax''' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(_SCREAMING_SNAKE_CASE , repo_id='''test-model-flax''' , push_to_hub=_SCREAMING_SNAKE_CASE , use_auth_token=self._token ) A_ = FlaxBertModel.from_pretrained(F'''{USER}/test-model-flax''' ) A_ = flatten_dict(unfreeze(model.params ) ) A_ = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): A_ = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(_SCREAMING_SNAKE_CASE , 1E-3 , msg=F'''{key} not identical''' ) def __A ( self ) -> List[str]: A_ = BertConfig( vocab_size=99 , hidden_size=32 , num_hidden_layers=5 , num_attention_heads=4 , intermediate_size=37 ) A_ = FlaxBertModel(_SCREAMING_SNAKE_CASE ) model.push_to_hub('''valid_org/test-model-flax-org''' , use_auth_token=self._token ) A_ = FlaxBertModel.from_pretrained('''valid_org/test-model-flax-org''' ) A_ = flatten_dict(unfreeze(model.params ) ) A_ = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): A_ = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(_SCREAMING_SNAKE_CASE , 1E-3 , msg=F'''{key} not identical''' ) # Reset repo delete_repo(token=self._token , repo_id='''valid_org/test-model-flax-org''' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained( _SCREAMING_SNAKE_CASE , repo_id='''valid_org/test-model-flax-org''' , push_to_hub=_SCREAMING_SNAKE_CASE , use_auth_token=self._token ) A_ = FlaxBertModel.from_pretrained('''valid_org/test-model-flax-org''' ) A_ = flatten_dict(unfreeze(model.params ) ) A_ = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): A_ = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(_SCREAMING_SNAKE_CASE , 1E-3 , msg=F'''{key} not identical''' ) def _UpperCAmelCase ( _UpperCamelCase : Union[str, Any], _UpperCamelCase : Tuple ) -> Dict: A_ = True A_ = flatten_dict(modela.params ) A_ = flatten_dict(modela.params ) for key in flat_params_a.keys(): if np.sum(np.abs(flat_params_a[key] - flat_params_a[key] ) ) > 1E-4: A_ = False return models_are_equal @require_flax class __UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' def __A ( self ) -> List[str]: A_ = BertConfig.from_pretrained('''hf-internal-testing/tiny-bert-flax-only''' ) A_ = FlaxBertModel(_SCREAMING_SNAKE_CASE ) A_ = '''bert''' with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(os.path.join(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) with self.assertRaises(_SCREAMING_SNAKE_CASE ): A_ = FlaxBertModel.from_pretrained(_SCREAMING_SNAKE_CASE ) A_ = FlaxBertModel.from_pretrained(_SCREAMING_SNAKE_CASE , subfolder=_SCREAMING_SNAKE_CASE ) self.assertTrue(check_models_equal(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) def __A ( self ) -> List[Any]: A_ = BertConfig.from_pretrained('''hf-internal-testing/tiny-bert-flax-only''' ) A_ = FlaxBertModel(_SCREAMING_SNAKE_CASE ) A_ = '''bert''' with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(os.path.join(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) , max_shard_size='''10KB''' ) with self.assertRaises(_SCREAMING_SNAKE_CASE ): A_ = FlaxBertModel.from_pretrained(_SCREAMING_SNAKE_CASE ) A_ = FlaxBertModel.from_pretrained(_SCREAMING_SNAKE_CASE , subfolder=_SCREAMING_SNAKE_CASE ) self.assertTrue(check_models_equal(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) def __A ( self ) -> Dict: A_ = '''bert''' A_ = '''hf-internal-testing/tiny-random-bert-subfolder''' with self.assertRaises(_SCREAMING_SNAKE_CASE ): A_ = FlaxBertModel.from_pretrained(_SCREAMING_SNAKE_CASE ) A_ = FlaxBertModel.from_pretrained(_SCREAMING_SNAKE_CASE , subfolder=_SCREAMING_SNAKE_CASE ) self.assertIsNotNone(_SCREAMING_SNAKE_CASE ) def __A ( self ) -> Optional[Any]: A_ = '''bert''' A_ = '''hf-internal-testing/tiny-random-bert-sharded-subfolder''' with self.assertRaises(_SCREAMING_SNAKE_CASE ): A_ = FlaxBertModel.from_pretrained(_SCREAMING_SNAKE_CASE ) A_ = FlaxBertModel.from_pretrained(_SCREAMING_SNAKE_CASE , subfolder=_SCREAMING_SNAKE_CASE ) self.assertIsNotNone(_SCREAMING_SNAKE_CASE )
18
0
'''simple docstring''' import argparse import json import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import SegformerImageProcessor, SwinConfig, UperNetConfig, UperNetForSemanticSegmentation def _UpperCAmelCase ( _UpperCamelCase : Optional[int] ) -> Dict: A_ = 3_84 A_ = 7 if "tiny" in model_name: A_ = 96 A_ = (2, 2, 6, 2) A_ = (3, 6, 12, 24) elif "small" in model_name: A_ = 96 A_ = (2, 2, 18, 2) A_ = (3, 6, 12, 24) elif "base" in model_name: A_ = 1_28 A_ = (2, 2, 18, 2) A_ = (4, 8, 16, 32) A_ = 12 A_ = 5_12 elif "large" in model_name: A_ = 1_92 A_ = (2, 2, 18, 2) A_ = (6, 12, 24, 48) A_ = 12 A_ = 7_68 # set label information A_ = 1_50 A_ = 'huggingface/label-files' A_ = 'ade20k-id2label.json' A_ = json.load(open(hf_hub_download(_A, _A, repo_type='''dataset''' ), '''r''' ) ) A_ = {int(_A ): v for k, v in idalabel.items()} A_ = {v: k for k, v in idalabel.items()} A_ = SwinConfig( embed_dim=_A, depths=_A, num_heads=_A, window_size=_A, out_features=['''stage1''', '''stage2''', '''stage3''', '''stage4'''], ) A_ = UperNetConfig( backbone_config=_A, auxiliary_in_channels=_A, num_labels=_A, idalabel=_A, labelaid=_A, ) return config def _UpperCAmelCase ( _UpperCamelCase : List[str] ) -> Optional[int]: A_ = [] # fmt: off # stem rename_keys.append(('''backbone.patch_embed.projection.weight''', '''backbone.embeddings.patch_embeddings.projection.weight''') ) rename_keys.append(('''backbone.patch_embed.projection.bias''', '''backbone.embeddings.patch_embeddings.projection.bias''') ) rename_keys.append(('''backbone.patch_embed.norm.weight''', '''backbone.embeddings.norm.weight''') ) rename_keys.append(('''backbone.patch_embed.norm.bias''', '''backbone.embeddings.norm.bias''') ) # stages for i in range(len(config.backbone_config.depths ) ): for j in range(config.backbone_config.depths[i] ): rename_keys.append((F'''backbone.stages.{i}.blocks.{j}.norm1.weight''', F'''backbone.encoder.layers.{i}.blocks.{j}.layernorm_before.weight''') ) rename_keys.append((F'''backbone.stages.{i}.blocks.{j}.norm1.bias''', F'''backbone.encoder.layers.{i}.blocks.{j}.layernorm_before.bias''') ) rename_keys.append((F'''backbone.stages.{i}.blocks.{j}.attn.w_msa.relative_position_bias_table''', F'''backbone.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_bias_table''') ) rename_keys.append((F'''backbone.stages.{i}.blocks.{j}.attn.w_msa.relative_position_index''', F'''backbone.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_index''') ) rename_keys.append((F'''backbone.stages.{i}.blocks.{j}.attn.w_msa.proj.weight''', F'''backbone.encoder.layers.{i}.blocks.{j}.attention.output.dense.weight''') ) rename_keys.append((F'''backbone.stages.{i}.blocks.{j}.attn.w_msa.proj.bias''', F'''backbone.encoder.layers.{i}.blocks.{j}.attention.output.dense.bias''') ) rename_keys.append((F'''backbone.stages.{i}.blocks.{j}.norm2.weight''', F'''backbone.encoder.layers.{i}.blocks.{j}.layernorm_after.weight''') ) rename_keys.append((F'''backbone.stages.{i}.blocks.{j}.norm2.bias''', F'''backbone.encoder.layers.{i}.blocks.{j}.layernorm_after.bias''') ) rename_keys.append((F'''backbone.stages.{i}.blocks.{j}.ffn.layers.0.0.weight''', F'''backbone.encoder.layers.{i}.blocks.{j}.intermediate.dense.weight''') ) rename_keys.append((F'''backbone.stages.{i}.blocks.{j}.ffn.layers.0.0.bias''', F'''backbone.encoder.layers.{i}.blocks.{j}.intermediate.dense.bias''') ) rename_keys.append((F'''backbone.stages.{i}.blocks.{j}.ffn.layers.1.weight''', F'''backbone.encoder.layers.{i}.blocks.{j}.output.dense.weight''') ) rename_keys.append((F'''backbone.stages.{i}.blocks.{j}.ffn.layers.1.bias''', F'''backbone.encoder.layers.{i}.blocks.{j}.output.dense.bias''') ) if i < 3: rename_keys.append((F'''backbone.stages.{i}.downsample.reduction.weight''', F'''backbone.encoder.layers.{i}.downsample.reduction.weight''') ) rename_keys.append((F'''backbone.stages.{i}.downsample.norm.weight''', F'''backbone.encoder.layers.{i}.downsample.norm.weight''') ) rename_keys.append((F'''backbone.stages.{i}.downsample.norm.bias''', F'''backbone.encoder.layers.{i}.downsample.norm.bias''') ) rename_keys.append((F'''backbone.norm{i}.weight''', F'''backbone.hidden_states_norms.stage{i+1}.weight''') ) rename_keys.append((F'''backbone.norm{i}.bias''', F'''backbone.hidden_states_norms.stage{i+1}.bias''') ) # decode head rename_keys.extend( [ ('''decode_head.conv_seg.weight''', '''decode_head.classifier.weight'''), ('''decode_head.conv_seg.bias''', '''decode_head.classifier.bias'''), ('''auxiliary_head.conv_seg.weight''', '''auxiliary_head.classifier.weight'''), ('''auxiliary_head.conv_seg.bias''', '''auxiliary_head.classifier.bias'''), ] ) # fmt: on return rename_keys def _UpperCAmelCase ( _UpperCamelCase : str, _UpperCamelCase : int, _UpperCamelCase : Optional[Any] ) -> int: A_ = dct.pop(_A ) A_ = val def _UpperCAmelCase ( _UpperCamelCase : Union[str, Any], _UpperCamelCase : Any ) -> Optional[int]: A_ = [int(backbone_config.embed_dim * 2**i ) for i in range(len(backbone_config.depths ) )] for i in range(len(backbone_config.depths ) ): A_ = num_features[i] for j in range(backbone_config.depths[i] ): # fmt: off # read in weights + bias of input projection layer (in original implementation, this is a single matrix + bias) A_ = state_dict.pop(F'''backbone.stages.{i}.blocks.{j}.attn.w_msa.qkv.weight''' ) A_ = state_dict.pop(F'''backbone.stages.{i}.blocks.{j}.attn.w_msa.qkv.bias''' ) # next, add query, keys and values (in that order) to the state dict A_ = in_proj_weight[:dim, :] A_ = in_proj_bias[: dim] A_ = in_proj_weight[ dim : dim * 2, : ] A_ = in_proj_bias[ dim : dim * 2 ] A_ = in_proj_weight[ -dim :, : ] A_ = in_proj_bias[-dim :] # fmt: on def _UpperCAmelCase ( _UpperCamelCase : str ) -> Any: A_ = x.shape A_ = x.reshape(_A, 4, in_channel // 4 ) A_ = x[:, [0, 2, 1, 3], :].transpose(1, 2 ).reshape(_A, _A ) return x def _UpperCAmelCase ( _UpperCamelCase : Dict ) -> List[Any]: A_ = x.shape A_ = x.reshape(_A, in_channel // 4, 4 ) A_ = x[:, :, [0, 2, 1, 3]].transpose(1, 2 ).reshape(_A, _A ) return x def _UpperCAmelCase ( _UpperCamelCase : Dict ) -> str: A_ = x.shape[0] A_ = x.reshape(4, in_channel // 4 ) A_ = x[[0, 2, 1, 3], :].transpose(0, 1 ).reshape(_A ) return x def _UpperCAmelCase ( _UpperCamelCase : Dict ) -> Dict: A_ = x.shape[0] A_ = x.reshape(in_channel // 4, 4 ) A_ = x[:, [0, 2, 1, 3]].transpose(0, 1 ).reshape(_A ) return x def _UpperCAmelCase ( _UpperCamelCase : Optional[int], _UpperCamelCase : Optional[int], _UpperCamelCase : Dict ) -> List[str]: A_ = { 'upernet-swin-tiny': 'https://download.openmmlab.com/mmsegmentation/v0.5/swin/upernet_swin_tiny_patch4_window7_512x512_160k_ade20k_pretrain_224x224_1K/upernet_swin_tiny_patch4_window7_512x512_160k_ade20k_pretrain_224x224_1K_20210531_112542-e380ad3e.pth', 'upernet-swin-small': 'https://download.openmmlab.com/mmsegmentation/v0.5/swin/upernet_swin_small_patch4_window7_512x512_160k_ade20k_pretrain_224x224_1K/upernet_swin_small_patch4_window7_512x512_160k_ade20k_pretrain_224x224_1K_20210526_192015-ee2fff1c.pth', 'upernet-swin-base': 'https://download.openmmlab.com/mmsegmentation/v0.5/swin/upernet_swin_base_patch4_window12_512x512_160k_ade20k_pretrain_384x384_22K/upernet_swin_base_patch4_window12_512x512_160k_ade20k_pretrain_384x384_22K_20210531_125459-429057bf.pth', 'upernet-swin-large': 'https://download.openmmlab.com/mmsegmentation/v0.5/swin/upernet_swin_large_patch4_window12_512x512_pretrain_384x384_22K_160k_ade20k/upernet_swin_large_patch4_window12_512x512_pretrain_384x384_22K_160k_ade20k_20220318_091743-9ba68901.pth', } A_ = model_name_to_url[model_name] A_ = torch.hub.load_state_dict_from_url(_A, map_location='''cpu''', file_name=_A )[ 'state_dict' ] for name, param in state_dict.items(): print(_A, param.shape ) A_ = get_upernet_config(_A ) A_ = UperNetForSemanticSegmentation(_A ) model.eval() # replace "bn" => "batch_norm" for key in state_dict.copy().keys(): A_ = state_dict.pop(_A ) if "bn" in key: A_ = key.replace('''bn''', '''batch_norm''' ) A_ = val # rename keys A_ = create_rename_keys(_A ) for src, dest in rename_keys: rename_key(_A, _A, _A ) read_in_q_k_v(_A, config.backbone_config ) # fix downsample parameters for key, value in state_dict.items(): if "downsample" in key: if "reduction" in key: A_ = reverse_correct_unfold_reduction_order(_A ) if "norm" in key: A_ = reverse_correct_unfold_norm_order(_A ) model.load_state_dict(_A ) # verify on image A_ = 'https://huggingface.co/datasets/hf-internal-testing/fixtures_ade20k/resolve/main/ADE_val_00000001.jpg' A_ = Image.open(requests.get(_A, stream=_A ).raw ).convert('''RGB''' ) A_ = SegformerImageProcessor() A_ = processor(_A, return_tensors='''pt''' ).pixel_values with torch.no_grad(): A_ = model(_A ) A_ = outputs.logits print(logits.shape ) print('''First values of logits:''', logits[0, 0, :3, :3] ) # assert values if model_name == "upernet-swin-tiny": A_ = torch.tensor( [[-7.5_9_5_8, -7.5_9_5_8, -7.4_3_0_2], [-7.5_9_5_8, -7.5_9_5_8, -7.4_3_0_2], [-7.4_7_9_7, -7.4_7_9_7, -7.3_0_6_8]] ) elif model_name == "upernet-swin-small": A_ = torch.tensor( [[-7.1_9_2_1, -7.1_9_2_1, -6.9_5_3_2], [-7.1_9_2_1, -7.1_9_2_1, -6.9_5_3_2], [-7.0_9_0_8, -7.0_9_0_8, -6.8_5_3_4]] ) elif model_name == "upernet-swin-base": A_ = torch.tensor( [[-6.5_8_5_1, -6.5_8_5_1, -6.4_3_3_0], [-6.5_8_5_1, -6.5_8_5_1, -6.4_3_3_0], [-6.4_7_6_3, -6.4_7_6_3, -6.3_2_5_4]] ) elif model_name == "upernet-swin-large": A_ = torch.tensor( [[-7.5_2_9_7, -7.5_2_9_7, -7.3_8_0_2], [-7.5_2_9_7, -7.5_2_9_7, -7.3_8_0_2], [-7.4_0_4_4, -7.4_0_4_4, -7.2_5_8_6]] ) print('''Logits:''', outputs.logits[0, 0, :3, :3] ) assert torch.allclose(outputs.logits[0, 0, :3, :3], _A, atol=1E-4 ) print('''Looks ok!''' ) if pytorch_dump_folder_path is not None: print(F'''Saving model {model_name} to {pytorch_dump_folder_path}''' ) model.save_pretrained(_A ) print(F'''Saving processor to {pytorch_dump_folder_path}''' ) processor.save_pretrained(_A ) if push_to_hub: print(F'''Pushing model and processor for {model_name} to hub''' ) model.push_to_hub(F'''openmmlab/{model_name}''' ) processor.push_to_hub(F'''openmmlab/{model_name}''' ) if __name__ == "__main__": __snake_case : Any = argparse.ArgumentParser() # Required parameters parser.add_argument( '--model_name', default='upernet-swin-tiny', type=str, choices=[F"""upernet-swin-{size}""" for size in ['tiny', 'small', 'base', 'large']], help='Name of the Swin + UperNet 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 or not to push the converted model to the 🤗 hub.' ) __snake_case : List[str] = parser.parse_args() convert_upernet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
352
'''simple docstring''' def _UpperCAmelCase ( _UpperCamelCase : Union[str, Any] ) -> Dict: A_ = 1 A_ = 2 while i * i <= n: A_ = 0 while n % i == 0: n //= i multiplicity += 1 n_divisors *= multiplicity + 1 i += 1 if n > 1: n_divisors *= 2 return n_divisors def _UpperCAmelCase ( ) -> Optional[int]: A_ = 1 A_ = 1 while True: i += 1 t_num += i if count_divisors(_UpperCamelCase ) > 5_00: break return t_num if __name__ == "__main__": print(solution())
18
0
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging __snake_case : List[str] = logging.get_logger(__name__) __snake_case : int = {'openai-gpt': 'https://huggingface.co/openai-gpt/resolve/main/config.json'} class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' __lowercase : str = 'openai-gpt' __lowercase : Optional[int] = { 'max_position_embeddings': 'n_positions', 'hidden_size': 'n_embd', 'num_attention_heads': 'n_head', 'num_hidden_layers': 'n_layer', } def __init__( self , _SCREAMING_SNAKE_CASE=4_0478 , _SCREAMING_SNAKE_CASE=512 , _SCREAMING_SNAKE_CASE=768 , _SCREAMING_SNAKE_CASE=12 , _SCREAMING_SNAKE_CASE=12 , _SCREAMING_SNAKE_CASE="gelu" , _SCREAMING_SNAKE_CASE=0.1 , _SCREAMING_SNAKE_CASE=0.1 , _SCREAMING_SNAKE_CASE=0.1 , _SCREAMING_SNAKE_CASE=1E-5 , _SCREAMING_SNAKE_CASE=0.02 , _SCREAMING_SNAKE_CASE="cls_index" , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=0.1 , **_SCREAMING_SNAKE_CASE , ) -> Any: 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__(**__UpperCAmelCase )
353
'''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 __UpperCAmelCase : '''simple docstring''' def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=2 , _SCREAMING_SNAKE_CASE=32 , _SCREAMING_SNAKE_CASE=16 , _SCREAMING_SNAKE_CASE=3 , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=32 , _SCREAMING_SNAKE_CASE=4 , _SCREAMING_SNAKE_CASE=[0, 1, 2, 3] , _SCREAMING_SNAKE_CASE=4 , _SCREAMING_SNAKE_CASE=37 , _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, 384, 24, 24] , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=None , ) -> Tuple: 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 __A ( self ) -> Optional[Any]: 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 __A ( self ) -> Optional[Any]: A_ = { '''global_padding''': '''same''', '''layer_type''': '''bottleneck''', '''depths''': [3, 4, 9], '''out_features''': ['''stage1''', '''stage2''', '''stage3'''], '''embedding_dynamic_padding''': True, '''hidden_sizes''': [96, 192, 384, 768], '''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 __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Optional[Any]: 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 __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> 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 __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Union[str, 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 __A ( self ) -> Optional[int]: A_ = self.prepare_config_and_inputs() A_ ,A_ ,A_ = config_and_inputs A_ = {'''pixel_values''': pixel_values} return config, inputs_dict @require_torch class __UpperCAmelCase ( _UpperCamelCase , _UpperCamelCase , unittest.TestCase ): '''simple docstring''' __lowercase : Optional[int] = (DPTModel, DPTForDepthEstimation, DPTForSemanticSegmentation) if is_torch_available() else () __lowercase : Optional[int] = ( { 'depth-estimation': DPTForDepthEstimation, 'feature-extraction': DPTModel, 'image-segmentation': DPTForSemanticSegmentation, } if is_torch_available() else {} ) __lowercase : Any = False __lowercase : Tuple = False __lowercase : List[Any] = False def __A ( self ) -> Tuple: A_ = DPTModelTester(self ) A_ = ConfigTester(self , config_class=_SCREAMING_SNAKE_CASE , has_text_modality=_SCREAMING_SNAKE_CASE , hidden_size=37 ) def __A ( self ) -> Dict: self.config_tester.run_common_tests() @unittest.skip(reason='''DPT does not use inputs_embeds''' ) def __A ( self ) -> Union[str, Any]: pass def __A ( self ) -> Dict: A_ ,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 __A ( self ) -> Optional[int]: A_ ,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 __A ( self ) -> str: A_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE ) def __A ( self ) -> str: A_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_depth_estimation(*_SCREAMING_SNAKE_CASE ) def __A ( self ) -> Optional[Any]: A_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_semantic_segmentation(*_SCREAMING_SNAKE_CASE ) def __A ( self ) -> Any: for model_class in self.all_model_classes: if model_class.__name__ == "DPTForDepthEstimation": continue A_ ,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 __A ( self ) -> Any: for model_class in self.all_model_classes: if model_class.__name__ == "DPTForDepthEstimation": continue A_ ,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 __A ( self ) -> Tuple: A_ ,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 __A ( self ) -> int: pass @slow def __A ( self ) -> Dict: for model_name in DPT_PRETRAINED_MODEL_ARCHIVE_LIST[1:]: A_ = DPTModel.from_pretrained(_SCREAMING_SNAKE_CASE ) self.assertIsNotNone(_SCREAMING_SNAKE_CASE ) def __A ( self ) -> Optional[int]: # We do this test only for DPTForDepthEstimation since it is the only model that uses readout_type A_ ,A_ = self.model_tester.prepare_config_and_inputs_for_common() A_ = '''add''' with self.assertRaises(_SCREAMING_SNAKE_CASE ): A_ = DPTForDepthEstimation(_SCREAMING_SNAKE_CASE ) def _UpperCAmelCase ( ) -> Optional[int]: A_ = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_torch @require_vision @slow class __UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' def __A ( self ) -> Any: 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, 384, 384) ) self.assertEqual(predicted_depth.shape , _SCREAMING_SNAKE_CASE ) A_ = torch.tensor( [[[5.6_437, 5.6_146, 5.6_511], [5.4_371, 5.5_649, 5.5_958], [5.5_215, 5.5_184, 5.5_293]]] ).to(_SCREAMING_SNAKE_CASE ) self.assertTrue(torch.allclose(outputs.predicted_depth[:3, :3, :3] / 100 , _SCREAMING_SNAKE_CASE , atol=1E-4 ) )
18
0
import math from typing import Dict, Iterable, List, Optional, Tuple, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import normalize, rescale, resize, to_channel_dimension_format from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, get_image_size, is_torch_available, is_torch_tensor, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging if is_torch_available(): import torch if is_vision_available(): import PIL __snake_case : Optional[Any] = logging.get_logger(__name__) def _UpperCAmelCase ( _UpperCamelCase : np.ndarray, _UpperCamelCase : Union[int, Iterable[int]], _UpperCamelCase : bool, _UpperCamelCase : int ) -> int: def constraint_to_multiple_of(_UpperCamelCase : Optional[Any], _UpperCamelCase : Optional[int], _UpperCamelCase : List[str]=0, _UpperCamelCase : Optional[Any]=None ): A_ = round(val / multiple ) * multiple if max_val is not None and x > max_val: A_ = math.floor(val / multiple ) * multiple if x < min_val: A_ = math.ceil(val / multiple ) * multiple return x A_ = (output_size, output_size) if isinstance(snake_case_, snake_case_ ) else output_size A_ = get_image_size(snake_case_ ) A_ = output_size # determine new height and width A_ = output_height / input_height A_ = output_width / input_width if keep_aspect_ratio: # scale as little as possible if abs(1 - scale_width ) < abs(1 - scale_height ): # fit width A_ = scale_width else: # fit height A_ = scale_height A_ = constraint_to_multiple_of(scale_height * input_height, multiple=snake_case_ ) A_ = constraint_to_multiple_of(scale_width * input_width, multiple=snake_case_ ) return (new_height, new_width) class __UpperCAmelCase ( _UpperCAmelCase ): '''simple docstring''' __lowercase : str = ['pixel_values'] def __init__( self , _SCREAMING_SNAKE_CASE = True , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = PILImageResampling.BILINEAR , _SCREAMING_SNAKE_CASE = False , _SCREAMING_SNAKE_CASE = 1 , _SCREAMING_SNAKE_CASE = True , _SCREAMING_SNAKE_CASE = 1 / 255 , _SCREAMING_SNAKE_CASE = True , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , **_SCREAMING_SNAKE_CASE , ) -> Dict: super().__init__(**SCREAMING_SNAKE_CASE_ ) A_ = size if size is not None else {"""height""": 384, """width""": 384} A_ = get_size_dict(SCREAMING_SNAKE_CASE_ ) A_ = do_resize A_ = size A_ = keep_aspect_ratio A_ = ensure_multiple_of A_ = resample A_ = do_rescale A_ = rescale_factor A_ = do_normalize A_ = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN A_ = image_std if image_std is not None else IMAGENET_STANDARD_STD def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = False , _SCREAMING_SNAKE_CASE = 1 , _SCREAMING_SNAKE_CASE = PILImageResampling.BICUBIC , _SCREAMING_SNAKE_CASE = None , **_SCREAMING_SNAKE_CASE , ) -> List[Any]: A_ = get_size_dict(SCREAMING_SNAKE_CASE_ ) if "height" not in size or "width" not in size: raise ValueError(F'''The size dictionary must contain the keys \'height\' and \'width\'. Got {size.keys()}''' ) A_ = get_resize_output_image_size( SCREAMING_SNAKE_CASE_ , output_size=(size['''height'''], size['''width''']) , keep_aspect_ratio=SCREAMING_SNAKE_CASE_ , multiple=SCREAMING_SNAKE_CASE_ , ) return resize(SCREAMING_SNAKE_CASE_ , size=SCREAMING_SNAKE_CASE_ , resample=SCREAMING_SNAKE_CASE_ , data_format=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None , **_SCREAMING_SNAKE_CASE , ) -> Tuple: return rescale(SCREAMING_SNAKE_CASE_ , scale=SCREAMING_SNAKE_CASE_ , data_format=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None , **_SCREAMING_SNAKE_CASE , ) -> Dict: return normalize(SCREAMING_SNAKE_CASE_ , mean=SCREAMING_SNAKE_CASE_ , std=SCREAMING_SNAKE_CASE_ , data_format=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = ChannelDimension.FIRST , **_SCREAMING_SNAKE_CASE , ) -> str: A_ = do_resize if do_resize is not None else self.do_resize A_ = size if size is not None else self.size A_ = get_size_dict(SCREAMING_SNAKE_CASE_ ) A_ = keep_aspect_ratio if keep_aspect_ratio is not None else self.keep_aspect_ratio A_ = ensure_multiple_of if ensure_multiple_of is not None else self.ensure_multiple_of A_ = resample if resample is not None else self.resample A_ = do_rescale if do_rescale is not None else self.do_rescale A_ = rescale_factor if rescale_factor is not None else self.rescale_factor A_ = do_normalize if do_normalize is not None else self.do_normalize A_ = image_mean if image_mean is not None else self.image_mean A_ = image_std if image_std is not None else self.image_std A_ = make_list_of_images(SCREAMING_SNAKE_CASE_ ) if not valid_images(SCREAMING_SNAKE_CASE_ ): raise ValueError( '''Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ''' '''torch.Tensor, tf.Tensor or jax.ndarray.''' ) if do_resize and size is None or resample is None: raise ValueError('''Size and resample must be specified if do_resize is True.''' ) if do_rescale and rescale_factor is None: raise ValueError('''Rescale factor must be specified if do_rescale is True.''' ) if do_normalize and (image_mean is None or image_std is None): raise ValueError('''Image mean and std must be specified if do_normalize is True.''' ) # All transformations expect numpy arrays. A_ = [to_numpy_array(SCREAMING_SNAKE_CASE_ ) for image in images] if do_resize: A_ = [self.resize(image=SCREAMING_SNAKE_CASE_ , size=SCREAMING_SNAKE_CASE_ , resample=SCREAMING_SNAKE_CASE_ ) for image in images] if do_rescale: A_ = [self.rescale(image=SCREAMING_SNAKE_CASE_ , scale=SCREAMING_SNAKE_CASE_ ) for image in images] if do_normalize: A_ = [self.normalize(image=SCREAMING_SNAKE_CASE_ , mean=SCREAMING_SNAKE_CASE_ , std=SCREAMING_SNAKE_CASE_ ) for image in images] A_ = [to_channel_dimension_format(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) for image in images] A_ = {"""pixel_values""": images} return BatchFeature(data=SCREAMING_SNAKE_CASE_ , tensor_type=SCREAMING_SNAKE_CASE_ ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None ) -> List[str]: A_ = outputs.logits # Resize logits and compute semantic segmentation maps if target_sizes is not None: if len(SCREAMING_SNAKE_CASE_ ) != len(SCREAMING_SNAKE_CASE_ ): raise ValueError( '''Make sure that you pass in as many target sizes as the batch dimension of the logits''' ) if is_torch_tensor(SCREAMING_SNAKE_CASE_ ): A_ = target_sizes.numpy() A_ = [] for idx in range(len(SCREAMING_SNAKE_CASE_ ) ): A_ = torch.nn.functional.interpolate( logits[idx].unsqueeze(dim=0 ) , size=target_sizes[idx] , mode='''bilinear''' , align_corners=SCREAMING_SNAKE_CASE_ ) A_ = resized_logits[0].argmax(dim=0 ) semantic_segmentation.append(SCREAMING_SNAKE_CASE_ ) else: A_ = logits.argmax(dim=1 ) A_ = [semantic_segmentation[i] for i in range(semantic_segmentation.shape[0] )] return semantic_segmentation
354
'''simple docstring''' import math def _UpperCAmelCase ( _UpperCamelCase : float, _UpperCamelCase : float ) -> float: if initial_intensity < 0: raise ValueError('''The value of intensity cannot be negative''' ) # handling of negative values of initial intensity if angle < 0 or angle > 3_60: raise ValueError('''In Malus Law, the angle is in the range 0-360 degrees''' ) # handling of values out of allowed range return initial_intensity * (math.cos(math.radians(_UpperCamelCase ) ) ** 2) if __name__ == "__main__": import doctest doctest.testmod(name='malus_law')
18
0
import unittest import numpy as np from diffusers import OnnxStableDiffusionInpaintPipelineLegacy from diffusers.utils.testing_utils import ( is_onnx_available, load_image, load_numpy, nightly, require_onnxruntime, require_torch_gpu, ) if is_onnx_available(): import onnxruntime as ort @nightly @require_onnxruntime @require_torch_gpu class __UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' @property def __A ( self ) -> Any: return ( "CUDAExecutionProvider", { "gpu_mem_limit": "15000000000", # 15GB "arena_extend_strategy": "kSameAsRequested", }, ) @property def __A ( self ) -> Union[str, Any]: A_ = ort.SessionOptions() A_ = False return options def __A ( self ) -> Optional[Any]: A_ = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo.png''' ) A_ = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo_mask.png''' ) A_ = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/red_cat_sitting_on_a_park_bench_onnx.npy''' ) # using the PNDM scheduler by default A_ = OnnxStableDiffusionInpaintPipelineLegacy.from_pretrained( '''CompVis/stable-diffusion-v1-4''' , revision='''onnx''' , safety_checker=_SCREAMING_SNAKE_CASE , feature_extractor=_SCREAMING_SNAKE_CASE , provider=self.gpu_provider , sess_options=self.gpu_options , ) pipe.set_progress_bar_config(disable=_SCREAMING_SNAKE_CASE ) A_ = """A red cat sitting on a park bench""" A_ = np.random.RandomState(0 ) A_ = pipe( prompt=_SCREAMING_SNAKE_CASE , image=_SCREAMING_SNAKE_CASE , mask_image=_SCREAMING_SNAKE_CASE , strength=0.75 , guidance_scale=7.5 , num_inference_steps=15 , generator=_SCREAMING_SNAKE_CASE , output_type='''np''' , ) A_ = output.images[0] assert image.shape == (512, 512, 3) assert np.abs(expected_image - image ).max() < 1E-2
355
'''simple docstring''' from ...utils import ( OptionalDependencyNotAvailable, is_torch_available, is_transformers_available, is_transformers_version, ) try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import ShapEPipeline else: from .camera import create_pan_cameras from .pipeline_shap_e import ShapEPipeline from .pipeline_shap_e_img2img import ShapEImgaImgPipeline from .renderer import ( BoundingBoxVolume, ImportanceRaySampler, MLPNeRFModelOutput, MLPNeRSTFModel, ShapEParamsProjModel, ShapERenderer, StratifiedRaySampler, VoidNeRFModel, )
18
0
'''simple docstring''' from typing import List, Optional from ...configuration_utils import PretrainedConfig from ...utils import logging __snake_case : Dict = logging.get_logger(__name__) __snake_case : str = { 'huggingface/autoformer-tourism-monthly': 'https://huggingface.co/huggingface/autoformer-tourism-monthly/resolve/main/config.json', } class __UpperCAmelCase ( __lowerCamelCase ): '''simple docstring''' __lowercase : int = 'autoformer' __lowercase : int = { 'hidden_size': 'd_model', 'num_attention_heads': 'encoder_attention_heads', 'num_hidden_layers': 'encoder_layers', } def __init__( self , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = "student_t" , _SCREAMING_SNAKE_CASE = "nll" , _SCREAMING_SNAKE_CASE = 1 , _SCREAMING_SNAKE_CASE = [1, 2, 3, 4, 5, 6, 7] , _SCREAMING_SNAKE_CASE = True , _SCREAMING_SNAKE_CASE = 0 , _SCREAMING_SNAKE_CASE = 0 , _SCREAMING_SNAKE_CASE = 0 , _SCREAMING_SNAKE_CASE = 0 , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = 64 , _SCREAMING_SNAKE_CASE = 2 , _SCREAMING_SNAKE_CASE = 2 , _SCREAMING_SNAKE_CASE = 2 , _SCREAMING_SNAKE_CASE = 2 , _SCREAMING_SNAKE_CASE = 32 , _SCREAMING_SNAKE_CASE = 32 , _SCREAMING_SNAKE_CASE = "gelu" , _SCREAMING_SNAKE_CASE = 0.1 , _SCREAMING_SNAKE_CASE = 0.1 , _SCREAMING_SNAKE_CASE = 0.1 , _SCREAMING_SNAKE_CASE = 0.1 , _SCREAMING_SNAKE_CASE = 0.1 , _SCREAMING_SNAKE_CASE = 100 , _SCREAMING_SNAKE_CASE = 0.02 , _SCREAMING_SNAKE_CASE = True , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE = 10 , _SCREAMING_SNAKE_CASE = 25 , _SCREAMING_SNAKE_CASE = 3 , **_SCREAMING_SNAKE_CASE , ) -> Dict: # time series specific configuration A_ = prediction_length A_ = context_length if context_length is not None else prediction_length A_ = distribution_output A_ = loss A_ = input_size A_ = num_time_features A_ = lags_sequence A_ = scaling A_ = num_dynamic_real_features A_ = num_static_real_features A_ = num_static_categorical_features if cardinality is not None and num_static_categorical_features > 0: if len(UpperCamelCase_ ) != num_static_categorical_features: raise ValueError( '''The cardinality should be a list of the same length as `num_static_categorical_features`''' ) A_ = cardinality else: A_ = [0] if embedding_dimension is not None and num_static_categorical_features > 0: if len(UpperCamelCase_ ) != num_static_categorical_features: raise ValueError( '''The embedding dimension should be a list of the same length as `num_static_categorical_features`''' ) A_ = embedding_dimension else: A_ = [min(50 , (cat + 1) // 2 ) for cat in self.cardinality] A_ = num_parallel_samples # Transformer architecture configuration A_ = input_size * len(self.lags_sequence ) + self._number_of_features A_ = d_model A_ = encoder_attention_heads A_ = decoder_attention_heads A_ = encoder_ffn_dim A_ = decoder_ffn_dim A_ = encoder_layers A_ = decoder_layers A_ = dropout A_ = attention_dropout A_ = activation_dropout A_ = encoder_layerdrop A_ = decoder_layerdrop A_ = activation_function A_ = init_std A_ = use_cache # Autoformer A_ = label_length A_ = moving_average A_ = autocorrelation_factor super().__init__(is_encoder_decoder=UpperCamelCase_ , **UpperCamelCase_ ) @property def __A ( self ) -> Any: 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 )
356
'''simple docstring''' import argparse from pathlib import Path import torch from transformers import OPTConfig, OPTModel from transformers.utils import logging logging.set_verbosity_info() __snake_case : Any = logging.get_logger(__name__) def _UpperCAmelCase ( _UpperCamelCase : Dict ) -> List[str]: A_ = torch.load(_UpperCamelCase, map_location='''cpu''' ) if "model" in sd.keys(): A_ = torch.load(_UpperCamelCase, map_location='''cpu''' )['''model'''] # pop unnecessary weights A_ = [ '''decoder.version''', '''decoder.output_projection.weight''', ] for key in keys_to_delete: if key in sd: sd.pop(_UpperCamelCase ) A_ = { '''decoder.project_in_dim.weight''': '''decoder.project_in.weight''', '''decoder.project_out_dim.weight''': '''decoder.project_out.weight''', '''decoder.layer_norm.weight''': '''decoder.final_layer_norm.weight''', '''decoder.layer_norm.bias''': '''decoder.final_layer_norm.bias''', } for old_key, new_key in keys_to_rename.items(): if old_key in sd: A_ = sd.pop(_UpperCamelCase ) A_ = list(sd.keys() ) for key in keys: if ".qkv_proj." in key: A_ = sd[key] # We split QKV in separate Q,K,V A_ = key.replace('''.qkv_proj.''', '''.q_proj.''' ) A_ = key.replace('''.qkv_proj.''', '''.k_proj.''' ) A_ = key.replace('''.qkv_proj.''', '''.v_proj.''' ) A_ = value.shape[0] assert depth % 3 == 0 # `SequeuceParallelTransformerBlock` has QKV weight is separated in K,V,Q despite the naming: # https://cs.github.com/facebookresearch/metaseq/blob/51871bd73cd04c038f239ea2a26db1d7f6b37927/metaseq/modules/sequence_parallel_transformer_layer.py#L97 A_ ,A_ ,A_ = torch.split(_UpperCamelCase, depth // 3, dim=0 ) A_ = q A_ = k A_ = v del sd[key] return sd @torch.no_grad() def _UpperCAmelCase ( _UpperCamelCase : Optional[int], _UpperCamelCase : Optional[Any], _UpperCamelCase : List[str]=None ) -> Dict: A_ = load_checkpoint(_UpperCamelCase ) if config is not None: A_ = OPTConfig.from_pretrained(_UpperCamelCase ) else: A_ = OPTConfig() A_ = OPTModel(_UpperCamelCase ).half().eval() model.load_state_dict(_UpperCamelCase ) # Check results Path(_UpperCamelCase ).mkdir(exist_ok=_UpperCamelCase ) model.save_pretrained(_UpperCamelCase ) if __name__ == "__main__": __snake_case : Optional[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '--fairseq_path', type=str, help=( 'path to fairseq checkpoint in correct format. You can find all checkpoints in the correct format here:' ' https://huggingface.co/models?other=opt_metasq' ), ) parser.add_argument('--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.') parser.add_argument('--hf_config', default=None, type=str, help='Define HF config.') __snake_case : Optional[Any] = parser.parse_args() convert_opt_checkpoint(args.fairseq_path, args.pytorch_dump_folder_path, config=args.hf_config)
18
0
'''simple docstring''' import string import numpy def _UpperCAmelCase ( _UpperCamelCase : str, _UpperCamelCase : int ) -> Optional[int]: return b if a == 0 else greatest_common_divisor(b % a, _UpperCamelCase ) class __UpperCAmelCase : '''simple docstring''' __lowercase : List[Any] = string.ascii_uppercase + string.digits # This cipher takes alphanumerics into account # i.e. a total of 36 characters # take x and return x % len(key_string) __lowercase : List[Any] = numpy.vectorize(lambda _UpperCamelCase : x % 36 ) __lowercase : Union[str, Any] = numpy.vectorize(lowerCAmelCase_ ) def __init__( self , _SCREAMING_SNAKE_CASE ) -> Dict: A_ = self.modulus(__lowerCAmelCase ) # mod36 calc's on the encrypt key self.check_determinant() # validate the determinant of the encryption key A_ = encrypt_key.shape[0] def __A ( self , _SCREAMING_SNAKE_CASE ) -> Tuple: return self.key_string.index(__lowerCAmelCase ) def __A ( self , _SCREAMING_SNAKE_CASE ) -> Any: return self.key_string[round(__lowerCAmelCase )] def __A ( self ) -> List[Any]: A_ = round(numpy.linalg.det(self.encrypt_key ) ) if det < 0: A_ = det % len(self.key_string ) A_ = len(self.key_string ) if greatest_common_divisor(__lowerCAmelCase , len(self.key_string ) ) != 1: A_ = ( F'''determinant modular {req_l} of encryption key({det}) ''' F'''is not co prime w.r.t {req_l}.\nTry another key.''' ) raise ValueError(__lowerCAmelCase ) def __A ( self , _SCREAMING_SNAKE_CASE ) -> Any: A_ = [char for char in text.upper() if char in self.key_string] A_ = chars[-1] while len(__lowerCAmelCase ) % self.break_key != 0: chars.append(__lowerCAmelCase ) return "".join(__lowerCAmelCase ) def __A ( self , _SCREAMING_SNAKE_CASE ) -> int: A_ = self.process_text(text.upper() ) A_ = '''''' for i in range(0 , len(__lowerCAmelCase ) - self.break_key + 1 , self.break_key ): A_ = text[i : i + self.break_key] A_ = [self.replace_letters(__lowerCAmelCase ) for char in batch] A_ = numpy.array([vec] ).T A_ = self.modulus(self.encrypt_key.dot(__lowerCAmelCase ) ).T.tolist()[ 0 ] A_ = ''''''.join( self.replace_digits(__lowerCAmelCase ) for num in batch_encrypted ) encrypted += encrypted_batch return encrypted def __A ( self ) -> Any: A_ = round(numpy.linalg.det(self.encrypt_key ) ) if det < 0: A_ = det % len(self.key_string ) A_ = None for i in range(len(self.key_string ) ): if (det * i) % len(self.key_string ) == 1: A_ = i break A_ = ( det_inv * numpy.linalg.det(self.encrypt_key ) * numpy.linalg.inv(self.encrypt_key ) ) return self.to_int(self.modulus(__lowerCAmelCase ) ) def __A ( self , _SCREAMING_SNAKE_CASE ) -> Union[str, Any]: A_ = self.make_decrypt_key() A_ = self.process_text(text.upper() ) A_ = '''''' for i in range(0 , len(__lowerCAmelCase ) - self.break_key + 1 , self.break_key ): A_ = text[i : i + self.break_key] A_ = [self.replace_letters(__lowerCAmelCase ) for char in batch] A_ = numpy.array([vec] ).T A_ = self.modulus(decrypt_key.dot(__lowerCAmelCase ) ).T.tolist()[0] A_ = ''''''.join( self.replace_digits(__lowerCAmelCase ) for num in batch_decrypted ) decrypted += decrypted_batch return decrypted def _UpperCAmelCase ( ) -> Optional[int]: A_ = int(input('''Enter the order of the encryption key: ''' ) ) A_ = [] print('''Enter each row of the encryption key with space separated integers''' ) for _ in range(_UpperCamelCase ): A_ = [int(_UpperCamelCase ) for x in input().split()] hill_matrix.append(_UpperCamelCase ) A_ = HillCipher(numpy.array(_UpperCamelCase ) ) print('''Would you like to encrypt or decrypt some text? (1 or 2)''' ) A_ = input('''\n1. Encrypt\n2. Decrypt\n''' ) if option == "1": A_ = input('''What text would you like to encrypt?: ''' ) print('''Your encrypted text is:''' ) print(hc.encrypt(_UpperCamelCase ) ) elif option == "2": A_ = input('''What text would you like to decrypt?: ''' ) print('''Your decrypted text is:''' ) print(hc.decrypt(_UpperCamelCase ) ) if __name__ == "__main__": import doctest doctest.testmod() main()
357
'''simple docstring''' from typing import List, Optional, Tuple from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_herbert import HerbertTokenizer __snake_case : Optional[Any] = logging.get_logger(__name__) __snake_case : Tuple = {'vocab_file': 'vocab.json', 'merges_file': 'merges.txt', 'tokenizer_file': 'tokenizer.json'} __snake_case : Optional[Any] = { 'vocab_file': { 'allegro/herbert-base-cased': 'https://huggingface.co/allegro/herbert-base-cased/resolve/main/vocab.json' }, 'merges_file': { 'allegro/herbert-base-cased': 'https://huggingface.co/allegro/herbert-base-cased/resolve/main/merges.txt' }, } __snake_case : Tuple = {'allegro/herbert-base-cased': 514} __snake_case : List[str] = {} class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' __lowercase : Any = VOCAB_FILES_NAMES __lowercase : Union[str, Any] = PRETRAINED_VOCAB_FILES_MAP __lowercase : Dict = PRETRAINED_INIT_CONFIGURATION __lowercase : List[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowercase : Optional[int] = HerbertTokenizer def __init__( self , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE="<s>" , _SCREAMING_SNAKE_CASE="<unk>" , _SCREAMING_SNAKE_CASE="<pad>" , _SCREAMING_SNAKE_CASE="<mask>" , _SCREAMING_SNAKE_CASE="</s>" , **_SCREAMING_SNAKE_CASE , ) -> int: super().__init__( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , tokenizer_file=_SCREAMING_SNAKE_CASE , cls_token=_SCREAMING_SNAKE_CASE , unk_token=_SCREAMING_SNAKE_CASE , pad_token=_SCREAMING_SNAKE_CASE , mask_token=_SCREAMING_SNAKE_CASE , sep_token=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE , ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None ) -> List[int]: A_ = [self.cls_token_id] A_ = [self.sep_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 , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = False ) -> List[int]: 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] + ([0] * len(_SCREAMING_SNAKE_CASE )) + [1] def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None ) -> List[int]: 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 ) * [0] + len(token_ids_a + sep ) * [1] def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None ) -> Tuple[str]: A_ = self._tokenizer.model.save(_SCREAMING_SNAKE_CASE , name=_SCREAMING_SNAKE_CASE ) return tuple(_SCREAMING_SNAKE_CASE )
18
0
'''simple docstring''' import argparse __snake_case : List[Any] = "docs/source/_static/js/custom.js" def _UpperCAmelCase ( _UpperCamelCase : str ) -> List[Any]: with open(_UpperCamelCase, encoding='''utf-8''', newline='''\n''' ) as f: A_ = f.readlines() A_ = 0 # First let's put the right version while not lines[index].startswith('''const stableVersion =''' ): index += 1 A_ = F'''const stableVersion = "v{version}"\n''' # Then update the dictionary while not lines[index].startswith('''const versionMapping = {''' ): index += 1 # We go until the end while not lines[index].startswith('''}''' ): index += 1 # We add the new version at the end lines[index - 1] += F''' "v{version}": "v{version}",\n''' with open(_UpperCamelCase, '''w''', encoding='''utf-8''', newline='''\n''' ) as f: f.writelines(_UpperCamelCase ) if __name__ == "__main__": __snake_case : List[Any] = argparse.ArgumentParser() parser.add_argument('--version', help='Release version.') __snake_case : List[Any] = parser.parse_args() update_custom_js(args.version)
358
'''simple docstring''' # Copyright 2022 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 import os import platform import numpy as np import psutil import torch from accelerate import __version__ as version from accelerate.commands.config import default_config_file, load_config_from_file from ..utils import is_npu_available, is_xpu_available def _UpperCAmelCase ( _UpperCamelCase : Union[str, Any]=None ) -> List[Any]: if subparsers is not None: A_ = subparsers.add_parser('''env''' ) else: A_ = argparse.ArgumentParser('''Accelerate env command''' ) parser.add_argument( '''--config_file''', default=_UpperCamelCase, help='''The config file to use for the default values in the launching script.''' ) if subparsers is not None: parser.set_defaults(func=_UpperCamelCase ) return parser def _UpperCAmelCase ( _UpperCamelCase : Dict ) -> Dict: A_ = torch.__version__ A_ = torch.cuda.is_available() A_ = is_xpu_available() A_ = is_npu_available() A_ = '''Not found''' # Get the default from the config file. if args.config_file is not None or os.path.isfile(_UpperCamelCase ): A_ = load_config_from_file(args.config_file ).to_dict() A_ = { '''`Accelerate` version''': version, '''Platform''': platform.platform(), '''Python version''': platform.python_version(), '''Numpy version''': np.__version__, '''PyTorch version (GPU?)''': F'''{pt_version} ({pt_cuda_available})''', '''PyTorch XPU available''': str(_UpperCamelCase ), '''PyTorch NPU available''': str(_UpperCamelCase ), '''System RAM''': F'''{psutil.virtual_memory().total / 10_24 ** 3:.2f} GB''', } if pt_cuda_available: A_ = torch.cuda.get_device_name() print('''\nCopy-and-paste the text below in your GitHub issue\n''' ) print('''\n'''.join([F'''- {prop}: {val}''' for prop, val in info.items()] ) ) print('''- `Accelerate` default config:''' if args.config_file is None else '''- `Accelerate` config passed:''' ) A_ = ( '''\n'''.join([F'''\t- {prop}: {val}''' for prop, val in accelerate_config.items()] ) if isinstance(_UpperCamelCase, _UpperCamelCase ) else F'''\t{accelerate_config}''' ) print(_UpperCamelCase ) A_ = accelerate_config return info def _UpperCAmelCase ( ) -> int: A_ = env_command_parser() A_ = parser.parse_args() env_command(_UpperCamelCase ) return 0 if __name__ == "__main__": raise SystemExit(main())
18
0
'''simple docstring''' import argparse import pytorch_lightning as pl import torch from torch import nn from transformers import LongformerForQuestionAnswering, LongformerModel class __UpperCAmelCase ( pl.LightningModule ): '''simple docstring''' def __init__( self , _SCREAMING_SNAKE_CASE ) -> Optional[Any]: super().__init__() A_ = model A_ = 2 A_ = nn.Linear(self.model.config.hidden_size , self.num_labels ) def __A ( self ) -> Any: pass def _UpperCAmelCase ( _UpperCamelCase : str, _UpperCamelCase : str, _UpperCamelCase : str ) -> Any: A_ = LongformerModel.from_pretrained(lowerCAmelCase__ ) A_ = LightningModel(lowerCAmelCase__ ) A_ = torch.load(lowerCAmelCase__, map_location=torch.device('''cpu''' ) ) lightning_model.load_state_dict(ckpt['''state_dict'''] ) # init longformer question answering model A_ = LongformerForQuestionAnswering.from_pretrained(lowerCAmelCase__ ) # transfer weights longformer_for_qa.longformer.load_state_dict(lightning_model.model.state_dict() ) longformer_for_qa.qa_outputs.load_state_dict(lightning_model.qa_outputs.state_dict() ) longformer_for_qa.eval() # save model longformer_for_qa.save_pretrained(lowerCAmelCase__ ) print(F'''Conversion successful. Model saved under {pytorch_dump_folder_path}''' ) if __name__ == "__main__": __snake_case : Any = argparse.ArgumentParser() # Required parameters parser.add_argument( '--longformer_model', default=None, type=str, required=True, help='model identifier of longformer. Should be either `longformer-base-4096` or `longformer-large-4096`.', ) parser.add_argument( '--longformer_question_answering_ckpt_path', default=None, type=str, required=True, help='Path the official PyTorch Lightning Checkpoint.', ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, required=True, help='Path to the output PyTorch model.' ) __snake_case : Tuple = parser.parse_args() convert_longformer_qa_checkpoint_to_pytorch( args.longformer_model, args.longformer_question_answering_ckpt_path, args.pytorch_dump_folder_path )
359
'''simple docstring''' import inspect import math import tempfile import unittest import numpy as np from transformers import ViTMAEConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTMAEForPreTraining, ViTMAEModel from transformers.models.vit.modeling_vit import VIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class __UpperCAmelCase : '''simple docstring''' def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=13 , _SCREAMING_SNAKE_CASE=30 , _SCREAMING_SNAKE_CASE=2 , _SCREAMING_SNAKE_CASE=3 , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=32 , _SCREAMING_SNAKE_CASE=5 , _SCREAMING_SNAKE_CASE=4 , _SCREAMING_SNAKE_CASE=37 , _SCREAMING_SNAKE_CASE="gelu" , _SCREAMING_SNAKE_CASE=0.1 , _SCREAMING_SNAKE_CASE=0.1 , _SCREAMING_SNAKE_CASE=10 , _SCREAMING_SNAKE_CASE=0.02 , _SCREAMING_SNAKE_CASE=3 , _SCREAMING_SNAKE_CASE=0.6 , _SCREAMING_SNAKE_CASE=None , ) -> Tuple: 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_ = num_attention_heads A_ = intermediate_size A_ = hidden_act A_ = hidden_dropout_prob A_ = attention_probs_dropout_prob A_ = type_sequence_label_size A_ = initializer_range A_ = mask_ratio A_ = scope # in ViTMAE, the expected sequence length = (num_patches + 1) * (1 - config.mask_ratio), rounded above # (we add 1 for the [CLS] token) A_ = (image_size // patch_size) ** 2 A_ = int(math.ceil((1 - mask_ratio) * (num_patches + 1) ) ) def __A ( self ) -> Union[str, Any]: 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.type_sequence_label_size ) A_ = self.get_config() return config, pixel_values, labels def __A ( self ) -> Dict: return ViTMAEConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=_SCREAMING_SNAKE_CASE , initializer_range=self.initializer_range , mask_ratio=self.mask_ratio , ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Tuple: A_ = ViTMAEModel(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 __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Optional[Any]: A_ = ViTMAEForPreTraining(_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() A_ = model(_SCREAMING_SNAKE_CASE ) A_ = (self.image_size // self.patch_size) ** 2 A_ = self.patch_size**2 * self.num_channels self.parent.assertEqual(result.logits.shape , (self.batch_size, num_patches, expected_num_channels) ) # test greyscale images A_ = 1 A_ = ViTMAEForPreTraining(_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() A_ = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) A_ = model(_SCREAMING_SNAKE_CASE ) A_ = self.patch_size**2 self.parent.assertEqual(result.logits.shape , (self.batch_size, num_patches, expected_num_channels) ) def __A ( self ) -> int: A_ = self.prepare_config_and_inputs() A_ ,A_ ,A_ = config_and_inputs A_ = {'''pixel_values''': pixel_values} return config, inputs_dict @require_torch class __UpperCAmelCase ( _UpperCamelCase , _UpperCamelCase , unittest.TestCase ): '''simple docstring''' __lowercase : int = (ViTMAEModel, ViTMAEForPreTraining) if is_torch_available() else () __lowercase : List[Any] = {'feature-extraction': ViTMAEModel} if is_torch_available() else {} __lowercase : Union[str, Any] = False __lowercase : List[Any] = False __lowercase : List[str] = False __lowercase : List[str] = False def __A ( self ) -> Any: A_ = ViTMAEModelTester(self ) A_ = ConfigTester(self , config_class=_SCREAMING_SNAKE_CASE , has_text_modality=_SCREAMING_SNAKE_CASE , hidden_size=37 ) def __A ( self ) -> Optional[int]: self.config_tester.run_common_tests() @unittest.skip(reason='''ViTMAE does not use inputs_embeds''' ) def __A ( self ) -> int: pass def __A ( self ) -> int: A_ ,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 __A ( self ) -> int: A_ ,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 __A ( self ) -> Union[str, Any]: A_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE ) def __A ( self ) -> Optional[int]: A_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_pretraining(*_SCREAMING_SNAKE_CASE ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> int: # make masks reproducible np.random.seed(2 ) A_ = int((pt_model.config.image_size // pt_model.config.patch_size) ** 2 ) A_ = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) A_ = torch.from_numpy(_SCREAMING_SNAKE_CASE ) # Add `noise` argument. # PT inputs will be prepared in `super().check_pt_tf_models()` with this added `noise` argument A_ = pt_noise super().check_pt_tf_models(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) def __A ( self ) -> str: A_ ,A_ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A_ = model_class(_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() # make random mask reproducible torch.manual_seed(2 ) with torch.no_grad(): A_ = model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) A_ = outputs[0].cpu().numpy() A_ = 0 with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(_SCREAMING_SNAKE_CASE ) A_ = model_class.from_pretrained(_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) # make random mask reproducible torch.manual_seed(2 ) with torch.no_grad(): A_ = model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) # Make sure we don't have nans A_ = after_outputs[0].cpu().numpy() A_ = 0 A_ = np.amax(np.abs(out_a - out_a ) ) self.assertLessEqual(_SCREAMING_SNAKE_CASE , 1E-5 ) @unittest.skip( reason='''ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load to get deterministic results.''' ) def __A ( self ) -> List[str]: pass @unittest.skip( reason='''ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load to get deterministic results.''' ) def __A ( self ) -> Dict: pass @unittest.skip( reason='''ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load to get deterministic results.''' ) def __A ( self ) -> Tuple: pass @unittest.skip(reason='''ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load''' ) def __A ( self ) -> str: pass @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.''' ) def __A ( self ) -> Union[str, Any]: pass @slow def __A ( self ) -> Dict: for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A_ = ViTMAEModel.from_pretrained(_SCREAMING_SNAKE_CASE ) self.assertIsNotNone(_SCREAMING_SNAKE_CASE ) def _UpperCAmelCase ( ) -> Dict: A_ = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_torch @require_vision class __UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' @cached_property def __A ( self ) -> List[str]: return ViTImageProcessor.from_pretrained('''facebook/vit-mae-base''' ) if is_vision_available() else None @slow def __A ( self ) -> List[str]: # make random mask reproducible across the PT and TF model np.random.seed(2 ) A_ = ViTMAEForPreTraining.from_pretrained('''facebook/vit-mae-base''' ).to(_SCREAMING_SNAKE_CASE ) A_ = self.default_image_processor A_ = prepare_img() A_ = image_processor(images=_SCREAMING_SNAKE_CASE , return_tensors='''pt''' ).to(_SCREAMING_SNAKE_CASE ) # prepare a noise vector that will be also used for testing the TF model # (this way we can ensure that the PT and TF models operate on the same inputs) A_ = ViTMAEConfig() A_ = int((vit_mae_config.image_size // vit_mae_config.patch_size) ** 2 ) A_ = np.random.uniform(size=(1, num_patches) ) # forward pass with torch.no_grad(): A_ = model(**_SCREAMING_SNAKE_CASE , noise=torch.from_numpy(_SCREAMING_SNAKE_CASE ).to(device=_SCREAMING_SNAKE_CASE ) ) # verify the logits A_ = torch.Size((1, 196, 768) ) self.assertEqual(outputs.logits.shape , _SCREAMING_SNAKE_CASE ) A_ = torch.tensor( [[-0.0_548, -1.7_023, -0.9_325], [0.3_721, -0.5_670, -0.2_233], [0.8_235, -1.3_878, -0.3_524]] ) self.assertTrue(torch.allclose(outputs.logits[0, :3, :3] , expected_slice.to(_SCREAMING_SNAKE_CASE ) , atol=1E-4 ) )
18
0
'''simple docstring''' from math import acos, sin from typing import List, Tuple, Union import numpy as np import torch from PIL import Image from ...models import AutoencoderKL, UNetaDConditionModel from ...schedulers import DDIMScheduler, DDPMScheduler from ...utils import randn_tensor from ..pipeline_utils import AudioPipelineOutput, BaseOutput, DiffusionPipeline, ImagePipelineOutput from .mel import Mel class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' __lowercase : Optional[Any] = ['vqvae'] def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , ) -> Tuple: super().__init__() self.register_modules(unet=_snake_case , scheduler=_snake_case , mel=_snake_case , vqvae=_snake_case ) def __A ( self ) -> int: return 50 if isinstance(self.scheduler , _snake_case ) else 1000 @torch.no_grad() def __call__( self , _SCREAMING_SNAKE_CASE = 1 , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = 0 , _SCREAMING_SNAKE_CASE = 0 , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = 0 , _SCREAMING_SNAKE_CASE = 0 , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = 0 , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE=True , ) -> Union[ Union[AudioPipelineOutput, ImagePipelineOutput], Tuple[List[Image.Image], Tuple[int, List[np.ndarray]]], ]: A_ = steps or self.get_default_steps() self.scheduler.set_timesteps(_snake_case ) A_ = step_generator or generator # For backwards compatibility if type(self.unet.config.sample_size ) == int: A_ = (self.unet.config.sample_size, self.unet.config.sample_size) if noise is None: A_ = randn_tensor( ( batch_size, self.unet.config.in_channels, self.unet.config.sample_size[0], self.unet.config.sample_size[1], ) , generator=_snake_case , device=self.device , ) A_ = noise A_ = None if audio_file is not None or raw_audio is not None: self.mel.load_audio(_snake_case , _snake_case ) A_ = self.mel.audio_slice_to_image(_snake_case ) A_ = np.frombuffer(input_image.tobytes() , dtype='''uint8''' ).reshape( (input_image.height, input_image.width) ) A_ = (input_image / 255) * 2 - 1 A_ = torch.tensor(input_image[np.newaxis, :, :] , dtype=torch.float ).to(self.device ) if self.vqvae is not None: A_ = self.vqvae.encode(torch.unsqueeze(_snake_case , 0 ) ).latent_dist.sample( generator=_snake_case )[0] A_ = self.vqvae.config.scaling_factor * input_images if start_step > 0: A_ = self.scheduler.add_noise(_snake_case , _snake_case , self.scheduler.timesteps[start_step - 1] ) A_ = ( self.unet.config.sample_size[1] * self.mel.get_sample_rate() / self.mel.x_res / self.mel.hop_length ) A_ = int(mask_start_secs * pixels_per_second ) A_ = int(mask_end_secs * pixels_per_second ) A_ = self.scheduler.add_noise(_snake_case , _snake_case , torch.tensor(self.scheduler.timesteps[start_step:] ) ) for step, t in enumerate(self.progress_bar(self.scheduler.timesteps[start_step:] ) ): if isinstance(self.unet , _snake_case ): A_ = self.unet(_snake_case , _snake_case , _snake_case )['''sample'''] else: A_ = self.unet(_snake_case , _snake_case )['''sample'''] if isinstance(self.scheduler , _snake_case ): A_ = self.scheduler.step( model_output=_snake_case , timestep=_snake_case , sample=_snake_case , eta=_snake_case , generator=_snake_case , )['''prev_sample'''] else: A_ = self.scheduler.step( model_output=_snake_case , timestep=_snake_case , sample=_snake_case , generator=_snake_case , )['''prev_sample'''] if mask is not None: if mask_start > 0: A_ = mask[:, step, :, :mask_start] if mask_end > 0: A_ = mask[:, step, :, -mask_end:] if self.vqvae is not None: # 0.18215 was scaling factor used in training to ensure unit variance A_ = 1 / self.vqvae.config.scaling_factor * images A_ = self.vqvae.decode(_snake_case )['''sample'''] A_ = (images / 2 + 0.5).clamp(0 , 1 ) A_ = images.cpu().permute(0 , 2 , 3 , 1 ).numpy() A_ = (images * 255).round().astype('''uint8''' ) A_ = list( (Image.fromarray(_[:, :, 0] ) for _ in images) if images.shape[3] == 1 else (Image.fromarray(_snake_case , mode='''RGB''' ).convert('''L''' ) for _ in images) ) A_ = [self.mel.image_to_audio(_snake_case ) for _ in images] if not return_dict: return images, (self.mel.get_sample_rate(), audios) return BaseOutput(**AudioPipelineOutput(np.array(_snake_case )[:, np.newaxis, :] ) , **ImagePipelineOutput(_snake_case ) ) @torch.no_grad() def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = 50 ) -> np.ndarray: assert isinstance(self.scheduler , _snake_case ) self.scheduler.set_timesteps(_snake_case ) A_ = np.array( [np.frombuffer(image.tobytes() , dtype='''uint8''' ).reshape((1, image.height, image.width) ) for image in images] ) A_ = (sample / 255) * 2 - 1 A_ = torch.Tensor(_snake_case ).to(self.device ) for t in self.progress_bar(torch.flip(self.scheduler.timesteps , (0,) ) ): A_ = t - self.scheduler.config.num_train_timesteps // self.scheduler.num_inference_steps A_ = self.scheduler.alphas_cumprod[t] A_ = ( self.scheduler.alphas_cumprod[prev_timestep] if prev_timestep >= 0 else self.scheduler.final_alpha_cumprod ) A_ = 1 - alpha_prod_t A_ = self.unet(_snake_case , _snake_case )['''sample'''] A_ = (1 - alpha_prod_t_prev) ** 0.5 * model_output A_ = (sample - pred_sample_direction) * alpha_prod_t_prev ** (-0.5) A_ = sample * alpha_prod_t ** 0.5 + beta_prod_t ** 0.5 * model_output return sample @staticmethod def __A ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> torch.Tensor: A_ = acos(torch.dot(torch.flatten(_snake_case ) , torch.flatten(_snake_case ) ) / torch.norm(_snake_case ) / torch.norm(_snake_case ) ) return sin((1 - alpha) * theta ) * xa / sin(_snake_case ) + sin(alpha * theta ) * xa / sin(_snake_case )
360
'''simple docstring''' from typing import Callable, Optional, Union from ...configuration_utils import PretrainedConfig from ...utils import logging __snake_case : int = logging.get_logger(__name__) __snake_case : str = { 'microsoft/xprophetnet-large-wiki100-cased': ( 'https://huggingface.co/microsoft/xprophetnet-large-wiki100-cased/resolve/main/config.json' ), } class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' __lowercase : Optional[Any] = 'xlm-prophetnet' __lowercase : Optional[int] = ['past_key_values'] __lowercase : int = { 'num_attention_heads': 'num_encoder_attention_heads', } def __init__( self , _SCREAMING_SNAKE_CASE = 0.1 , _SCREAMING_SNAKE_CASE = "gelu" , _SCREAMING_SNAKE_CASE = 3_0522 , _SCREAMING_SNAKE_CASE = 1024 , _SCREAMING_SNAKE_CASE = 4096 , _SCREAMING_SNAKE_CASE = 12 , _SCREAMING_SNAKE_CASE = 16 , _SCREAMING_SNAKE_CASE = 4096 , _SCREAMING_SNAKE_CASE = 12 , _SCREAMING_SNAKE_CASE = 16 , _SCREAMING_SNAKE_CASE = 0.1 , _SCREAMING_SNAKE_CASE = 0.1 , _SCREAMING_SNAKE_CASE = 512 , _SCREAMING_SNAKE_CASE = 0.02 , _SCREAMING_SNAKE_CASE = True , _SCREAMING_SNAKE_CASE = True , _SCREAMING_SNAKE_CASE = 0 , _SCREAMING_SNAKE_CASE = 2 , _SCREAMING_SNAKE_CASE = 32 , _SCREAMING_SNAKE_CASE = 128 , _SCREAMING_SNAKE_CASE = False , _SCREAMING_SNAKE_CASE = 0.0 , _SCREAMING_SNAKE_CASE = True , _SCREAMING_SNAKE_CASE = 0 , _SCREAMING_SNAKE_CASE = 1 , _SCREAMING_SNAKE_CASE = 2 , **_SCREAMING_SNAKE_CASE , ) -> int: A_ = vocab_size A_ = hidden_size A_ = encoder_ffn_dim A_ = num_encoder_layers A_ = num_encoder_attention_heads A_ = decoder_ffn_dim A_ = num_decoder_layers A_ = num_decoder_attention_heads A_ = max_position_embeddings A_ = init_std # Normal(0, this parameter) A_ = activation_function # parameters for xlmprophetnet A_ = ngram A_ = num_buckets A_ = relative_max_distance A_ = disable_ngram_loss A_ = eps # 3 Types of Dropout A_ = attention_dropout A_ = activation_dropout A_ = dropout A_ = use_cache super().__init__( pad_token_id=_SCREAMING_SNAKE_CASE , bos_token_id=_SCREAMING_SNAKE_CASE , eos_token_id=_SCREAMING_SNAKE_CASE , is_encoder_decoder=_SCREAMING_SNAKE_CASE , add_cross_attention=_SCREAMING_SNAKE_CASE , decoder_start_token_id=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE , ) @property def __A ( self ) -> int: return self.num_encoder_layers + self.num_decoder_layers @num_hidden_layers.setter def __A ( self , _SCREAMING_SNAKE_CASE ) -> List[Any]: raise NotImplementedError( '''This model does not support the setting of `num_hidden_layers`. Please set `num_encoder_layers` and''' ''' `num_decoder_layers`.''' )
18
0
'''simple docstring''' import unittest from transformers import MPNetConfig, is_torch_available from transformers.testing_utils import require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( MPNetForMaskedLM, MPNetForMultipleChoice, MPNetForQuestionAnswering, MPNetForSequenceClassification, MPNetForTokenClassification, MPNetModel, ) class __UpperCAmelCase : '''simple docstring''' def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=13 , _SCREAMING_SNAKE_CASE=7 , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=False , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=99 , _SCREAMING_SNAKE_CASE=64 , _SCREAMING_SNAKE_CASE=5 , _SCREAMING_SNAKE_CASE=4 , _SCREAMING_SNAKE_CASE=64 , _SCREAMING_SNAKE_CASE="gelu" , _SCREAMING_SNAKE_CASE=0.1 , _SCREAMING_SNAKE_CASE=0.1 , _SCREAMING_SNAKE_CASE=512 , _SCREAMING_SNAKE_CASE=16 , _SCREAMING_SNAKE_CASE=2 , _SCREAMING_SNAKE_CASE=0.02 , _SCREAMING_SNAKE_CASE=3 , _SCREAMING_SNAKE_CASE=4 , _SCREAMING_SNAKE_CASE=None , ) -> str: A_ = parent A_ = batch_size A_ = seq_length A_ = is_training A_ = use_input_mask A_ = use_token_type_ids A_ = use_labels A_ = vocab_size A_ = hidden_size A_ = num_hidden_layers A_ = num_attention_heads A_ = intermediate_size A_ = hidden_act A_ = hidden_dropout_prob A_ = attention_probs_dropout_prob A_ = max_position_embeddings A_ = type_vocab_size A_ = type_sequence_label_size A_ = initializer_range A_ = num_labels A_ = num_choices A_ = scope def __A ( self ) -> Any: return MPNetConfig.from_pretrained('''microsoft/mpnet-base''' ) def __A ( self ) -> List[str]: A_ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) A_ = None if self.use_input_mask: A_ = random_attention_mask([self.batch_size, self.seq_length] ) A_ = None A_ = None A_ = None if self.use_labels: A_ = ids_tensor([self.batch_size] , self.type_sequence_label_size ) A_ = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) A_ = ids_tensor([self.batch_size] , self.num_choices ) A_ = self.get_config() return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels def __A ( self ) -> List[Any]: return MPNetConfig( 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 , initializer_range=self.initializer_range , ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Dict: A_ = MPNetModel(config=_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() A_ = model(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) A_ = model(_SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size) ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Dict: A_ = MPNetForQuestionAnswering(config=_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() A_ = model( _SCREAMING_SNAKE_CASE , attention_mask=_SCREAMING_SNAKE_CASE , start_positions=_SCREAMING_SNAKE_CASE , end_positions=_SCREAMING_SNAKE_CASE , ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> List[str]: A_ = self.num_labels A_ = MPNetForSequenceClassification(_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() A_ = model(_SCREAMING_SNAKE_CASE , attention_mask=_SCREAMING_SNAKE_CASE , labels=_SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> List[str]: A_ = self.num_choices A_ = MPNetForMultipleChoice(config=_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() A_ = input_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() A_ = input_mask.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() A_ = model( _SCREAMING_SNAKE_CASE , attention_mask=_SCREAMING_SNAKE_CASE , labels=_SCREAMING_SNAKE_CASE , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> List[Any]: A_ = self.num_labels A_ = MPNetForTokenClassification(config=_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() A_ = model(_SCREAMING_SNAKE_CASE , attention_mask=_SCREAMING_SNAKE_CASE , labels=_SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def __A ( self ) -> Tuple: A_ = self.prepare_config_and_inputs() ((A_) ,(A_) ,(A_) ,(A_) ,(A_) ,(A_)) = config_and_inputs A_ = {'''input_ids''': input_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_torch class __UpperCAmelCase ( __snake_case , __snake_case , unittest.TestCase ): '''simple docstring''' __lowercase : str = ( ( MPNetForMaskedLM, MPNetForMultipleChoice, MPNetForQuestionAnswering, MPNetForSequenceClassification, MPNetForTokenClassification, MPNetModel, ) if is_torch_available() else () ) __lowercase : Optional[Any] = ( { 'feature-extraction': MPNetModel, 'fill-mask': MPNetForMaskedLM, 'question-answering': MPNetForQuestionAnswering, 'text-classification': MPNetForSequenceClassification, 'token-classification': MPNetForTokenClassification, 'zero-shot': MPNetForSequenceClassification, } if is_torch_available() else {} ) __lowercase : Optional[Any] = False __lowercase : Optional[Any] = True def __A ( self ) -> List[str]: A_ = MPNetModelTester(self ) A_ = ConfigTester(self , config_class=_SCREAMING_SNAKE_CASE , hidden_size=37 ) def __A ( self ) -> Union[str, Any]: self.config_tester.run_common_tests() def __A ( self ) -> List[str]: A_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mpnet_model(*_SCREAMING_SNAKE_CASE ) def __A ( self ) -> int: A_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mpnet_for_sequence_classification(*_SCREAMING_SNAKE_CASE ) def __A ( self ) -> List[Any]: A_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mpnet_for_multiple_choice(*_SCREAMING_SNAKE_CASE ) def __A ( self ) -> Any: A_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mpnet_for_token_classification(*_SCREAMING_SNAKE_CASE ) def __A ( self ) -> Optional[Any]: A_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mpnet_for_question_answering(*_SCREAMING_SNAKE_CASE ) @require_torch class __UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' @slow def __A ( self ) -> str: A_ = MPNetModel.from_pretrained('''microsoft/mpnet-base''' ) A_ = torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]] ) A_ = model(_SCREAMING_SNAKE_CASE )[0] A_ = torch.Size((1, 11, 768) ) self.assertEqual(output.shape , _SCREAMING_SNAKE_CASE ) A_ = torch.tensor( [[[-0.0_550, 0.1_943, -0.0_740], [-0.0_562, 0.2_211, -0.0_579], [-0.0_437, 0.3_337, -0.0_641]]] ) # compare the actual values for a slice. self.assertTrue(torch.allclose(output[:, :3, :3] , _SCREAMING_SNAKE_CASE , atol=1E-4 ) )
361
'''simple docstring''' def _UpperCAmelCase ( _UpperCamelCase : float, _UpperCamelCase : list[float] ) -> float: if discount_rate < 0: raise ValueError('''Discount rate cannot be negative''' ) if not cash_flows: raise ValueError('''Cash flows list cannot be empty''' ) A_ = sum( cash_flow / ((1 + discount_rate) ** i) for i, cash_flow in enumerate(_UpperCamelCase ) ) return round(_UpperCamelCase, ndigits=2 ) if __name__ == "__main__": import doctest doctest.testmod()
18
0
'''simple docstring''' import string from math import logaa def _UpperCAmelCase ( _UpperCamelCase : str, _UpperCamelCase : str ) -> List[Any]: A_ = document.translate( str.maketrans('''''', '''''', string.punctuation ) ).replace('''\n''', '''''' ) A_ = document_without_punctuation.split(''' ''' ) # word tokenization return len([word for word in tokenize_document if word.lower() == term.lower()] ) def _UpperCAmelCase ( _UpperCamelCase : str, _UpperCamelCase : str ) -> int: A_ = corpus.lower().translate( str.maketrans('''''', '''''', string.punctuation ) ) # strip all punctuation and replace it with '' A_ = corpus_without_punctuation.split('''\n''' ) A_ = term.lower() return (len([doc for doc in docs if term in doc] ), len(UpperCamelCase__ )) def _UpperCAmelCase ( _UpperCamelCase : int, _UpperCamelCase : int, _UpperCamelCase : List[str]=False ) -> Optional[int]: if smoothing: if n == 0: raise ValueError('''log10(0) is undefined.''' ) return round(1 + logaa(n / (1 + df) ), 3 ) if df == 0: raise ZeroDivisionError('''df must be > 0''' ) elif n == 0: raise ValueError('''log10(0) is undefined.''' ) return round(logaa(n / df ), 3 ) def _UpperCAmelCase ( _UpperCamelCase : int, _UpperCamelCase : int ) -> Any: return round(tf * idf, 3 )
362
'''simple docstring''' from __future__ import annotations def _UpperCAmelCase ( _UpperCamelCase : int | str ) -> bool: A_ = str(_UpperCamelCase ) return n == n[::-1] def _UpperCAmelCase ( _UpperCamelCase : int = 1_00_00_00 ) -> Any: A_ = 0 for i in range(1, _UpperCamelCase ): if is_palindrome(_UpperCamelCase ) and is_palindrome(bin(_UpperCamelCase ).split('''b''' )[1] ): total += i return total if __name__ == "__main__": print(solution(int(str(input().strip()))))
18
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_fnet import FNetTokenizer else: __snake_case : List[Any] = None __snake_case : Union[str, Any] = logging.get_logger(__name__) __snake_case : Any = {'''vocab_file''': '''spiece.model''', '''tokenizer_file''': '''tokenizer.json'''} __snake_case : Tuple = { '''vocab_file''': { '''google/fnet-base''': '''https://huggingface.co/google/fnet-base/resolve/main/spiece.model''', '''google/fnet-large''': '''https://huggingface.co/google/fnet-large/resolve/main/spiece.model''', }, '''tokenizer_file''': { '''google/fnet-base''': '''https://huggingface.co/google/fnet-base/resolve/main/tokenizer.json''', '''google/fnet-large''': '''https://huggingface.co/google/fnet-large/resolve/main/tokenizer.json''', }, } __snake_case : Tuple = { '''google/fnet-base''': 512, '''google/fnet-large''': 512, } __snake_case : Optional[Any] = '''▁''' class __UpperCAmelCase ( SCREAMING_SNAKE_CASE__ ): '''simple docstring''' __lowercase : List[str] = VOCAB_FILES_NAMES __lowercase : Optional[int] = PRETRAINED_VOCAB_FILES_MAP __lowercase : Tuple = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowercase : List[str] = ['input_ids', 'token_type_ids'] __lowercase : int = FNetTokenizer def __init__( self , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE=False , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE="<unk>" , _SCREAMING_SNAKE_CASE="[SEP]" , _SCREAMING_SNAKE_CASE="<pad>" , _SCREAMING_SNAKE_CASE="[CLS]" , _SCREAMING_SNAKE_CASE="[MASK]" , **_SCREAMING_SNAKE_CASE , ) -> Union[str, Any]: # Mask token behave like a normal word, i.e. include the space before it and # is included in the raw text, there should be a match in a non-normalized sentence. A_ = ( AddedToken(_SCREAMING_SNAKE_CASE , lstrip=_SCREAMING_SNAKE_CASE , rstrip=_SCREAMING_SNAKE_CASE , normalized=_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 , 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 , ) A_ = do_lower_case A_ = remove_space A_ = keep_accents A_ = vocab_file A_ = False if not self.vocab_file else True def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None ) -> List[int]: A_ = [self.sep_token_id] A_ = [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 , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None ) -> List[int]: 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 ) * [0] + len(token_ids_a + sep ) * [1] def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None ) -> Tuple[str]: 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'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_SCREAMING_SNAKE_CASE ): copyfile(self.vocab_file , _SCREAMING_SNAKE_CASE ) return (out_vocab_file,)
363
'''simple docstring''' # Usage: # ./gen-card-facebook-wmt19.py import os from pathlib import Path def _UpperCAmelCase ( _UpperCamelCase : Tuple, _UpperCamelCase : Tuple, _UpperCamelCase : List[str] ) -> int: A_ = { '''en''': '''Machine learning is great, isn\'t it?''', '''ru''': '''Машинное обучение - это здорово, не так ли?''', '''de''': '''Maschinelles Lernen ist großartig, oder?''', } # BLUE scores as follows: # "pair": [fairseq, transformers] A_ = { '''ru-en''': ['''[41.3](http://matrix.statmt.org/matrix/output/1907?run_id=6937)''', '''39.20'''], '''en-ru''': ['''[36.4](http://matrix.statmt.org/matrix/output/1914?run_id=6724)''', '''33.47'''], '''en-de''': ['''[43.1](http://matrix.statmt.org/matrix/output/1909?run_id=6862)''', '''42.83'''], '''de-en''': ['''[42.3](http://matrix.statmt.org/matrix/output/1902?run_id=6750)''', '''41.35'''], } A_ = F'''{src_lang}-{tgt_lang}''' A_ = F''' --- language: - {src_lang} - {tgt_lang} thumbnail: tags: - translation - wmt19 - facebook license: apache-2.0 datasets: - wmt19 metrics: - bleu --- # FSMT ## Model description This is a ported version of [fairseq wmt19 transformer](https://github.com/pytorch/fairseq/blob/master/examples/wmt19/README.md) for {src_lang}-{tgt_lang}. For more details, please see, [Facebook FAIR\'s WMT19 News Translation Task Submission](https://arxiv.org/abs/1907.06616). The abbreviation FSMT stands for FairSeqMachineTranslation All four models are available: * [wmt19-en-ru](https://huggingface.co/facebook/wmt19-en-ru) * [wmt19-ru-en](https://huggingface.co/facebook/wmt19-ru-en) * [wmt19-en-de](https://huggingface.co/facebook/wmt19-en-de) * [wmt19-de-en](https://huggingface.co/facebook/wmt19-de-en) ## Intended uses & limitations #### How to use ```python from transformers import FSMTForConditionalGeneration, FSMTTokenizer mname = "facebook/wmt19-{src_lang}-{tgt_lang}" tokenizer = FSMTTokenizer.from_pretrained(mname) model = FSMTForConditionalGeneration.from_pretrained(mname) input = "{texts[src_lang]}" input_ids = tokenizer.encode(input, return_tensors="pt") outputs = model.generate(input_ids) decoded = tokenizer.decode(outputs[0], skip_special_tokens=True) print(decoded) # {texts[tgt_lang]} ``` #### Limitations and bias - The original (and this ported model) doesn\'t seem to handle well inputs with repeated sub-phrases, [content gets truncated](https://discuss.huggingface.co/t/issues-with-translating-inputs-containing-repeated-phrases/981) ## Training data Pretrained weights were left identical to the original model released by fairseq. For more details, please, see the [paper](https://arxiv.org/abs/1907.06616). ## Eval results pair | fairseq | transformers -------|---------|---------- {pair} | {scores[pair][0]} | {scores[pair][1]} The score is slightly below the score reported by `fairseq`, since `transformers`` currently doesn\'t support: - model ensemble, therefore the best performing checkpoint was ported (``model4.pt``). - re-ranking The score was calculated using this code: ```bash git clone https://github.com/huggingface/transformers cd transformers export PAIR={pair} export DATA_DIR=data/$PAIR export SAVE_DIR=data/$PAIR export BS=8 export NUM_BEAMS=15 mkdir -p $DATA_DIR sacrebleu -t wmt19 -l $PAIR --echo src > $DATA_DIR/val.source sacrebleu -t wmt19 -l $PAIR --echo ref > $DATA_DIR/val.target echo $PAIR PYTHONPATH="src:examples/seq2seq" python examples/seq2seq/run_eval.py facebook/wmt19-$PAIR $DATA_DIR/val.source $SAVE_DIR/test_translations.txt --reference_path $DATA_DIR/val.target --score_path $SAVE_DIR/test_bleu.json --bs $BS --task translation --num_beams $NUM_BEAMS ``` note: fairseq reports using a beam of 50, so you should get a slightly higher score if re-run with `--num_beams 50`. ## Data Sources - [training, etc.](http://www.statmt.org/wmt19/) - [test set](http://matrix.statmt.org/test_sets/newstest2019.tgz?1556572561) ### BibTeX entry and citation info ```bibtex @inproceedings{{..., year={{2020}}, title={{Facebook FAIR\'s WMT19 News Translation Task Submission}}, author={{Ng, Nathan and Yee, Kyra and Baevski, Alexei and Ott, Myle and Auli, Michael and Edunov, Sergey}}, booktitle={{Proc. of WMT}}, }} ``` ## TODO - port model ensemble (fairseq uses 4 model checkpoints) ''' os.makedirs(_UpperCamelCase, exist_ok=_UpperCamelCase ) A_ = os.path.join(_UpperCamelCase, '''README.md''' ) print(F'''Generating {path}''' ) with open(_UpperCamelCase, '''w''', encoding='''utf-8''' ) as f: f.write(_UpperCamelCase ) # make sure we are under the root of the project __snake_case : Any = Path(__file__).resolve().parent.parent.parent __snake_case : Tuple = repo_dir / 'model_cards' for model_name in ["wmt19-ru-en", "wmt19-en-ru", "wmt19-en-de", "wmt19-de-en"]: __snake_case , __snake_case , __snake_case : Any = model_name.split('-') __snake_case : int = model_cards_dir / 'facebook' / model_name write_model_card(model_card_dir, src_lang=src_lang, tgt_lang=tgt_lang)
18
0
'''simple docstring''' import unittest from transformers import ( MODEL_FOR_OBJECT_DETECTION_MAPPING, AutoFeatureExtractor, AutoModelForObjectDetection, ObjectDetectionPipeline, is_vision_available, pipeline, ) from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_pytesseract, require_tf, require_timm, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_vision_available(): from PIL import Image else: class __UpperCAmelCase : '''simple docstring''' @staticmethod def __A ( *_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) -> List[Any]: pass @is_pipeline_test @require_vision @require_timm @require_torch class __UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' __lowercase : Optional[Any] = MODEL_FOR_OBJECT_DETECTION_MAPPING def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> List[Any]: A_ = ObjectDetectionPipeline(model=UpperCAmelCase_ , image_processor=UpperCAmelCase_ ) return object_detector, ["./tests/fixtures/tests_samples/COCO/000000039769.png"] def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Optional[Any]: A_ = object_detector('''./tests/fixtures/tests_samples/COCO/000000039769.png''' , threshold=0.0 ) self.assertGreater(len(UpperCAmelCase_ ) , 0 ) for detected_object in outputs: self.assertEqual( UpperCAmelCase_ , { '''score''': ANY(UpperCAmelCase_ ), '''label''': ANY(UpperCAmelCase_ ), '''box''': {'''xmin''': ANY(UpperCAmelCase_ ), '''ymin''': ANY(UpperCAmelCase_ ), '''xmax''': ANY(UpperCAmelCase_ ), '''ymax''': ANY(UpperCAmelCase_ )}, } , ) import datasets A_ = datasets.load_dataset('''hf-internal-testing/fixtures_image_utils''' , '''image''' , split='''test''' ) A_ = [ Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ), "http://images.cocodataset.org/val2017/000000039769.jpg", # RGBA dataset[0]["file"], # LA dataset[1]["file"], # L dataset[2]["file"], ] A_ = object_detector(UpperCAmelCase_ , threshold=0.0 ) self.assertEqual(len(UpperCAmelCase_ ) , len(UpperCAmelCase_ ) ) for outputs in batch_outputs: self.assertGreater(len(UpperCAmelCase_ ) , 0 ) for detected_object in outputs: self.assertEqual( UpperCAmelCase_ , { '''score''': ANY(UpperCAmelCase_ ), '''label''': ANY(UpperCAmelCase_ ), '''box''': {'''xmin''': ANY(UpperCAmelCase_ ), '''ymin''': ANY(UpperCAmelCase_ ), '''xmax''': ANY(UpperCAmelCase_ ), '''ymax''': ANY(UpperCAmelCase_ )}, } , ) @require_tf @unittest.skip('''Object detection not implemented in TF''' ) def __A ( self ) -> List[str]: pass @require_torch def __A ( self ) -> Optional[Any]: A_ = "hf-internal-testing/tiny-detr-mobilenetsv3" A_ = AutoModelForObjectDetection.from_pretrained(UpperCAmelCase_ ) A_ = AutoFeatureExtractor.from_pretrained(UpperCAmelCase_ ) A_ = ObjectDetectionPipeline(model=UpperCAmelCase_ , feature_extractor=UpperCAmelCase_ ) A_ = object_detector('''http://images.cocodataset.org/val2017/000000039769.jpg''' , threshold=0.0 ) self.assertEqual( nested_simplify(UpperCAmelCase_ , decimals=4 ) , [ {'''score''': 0.3_376, '''label''': '''LABEL_0''', '''box''': {'''xmin''': 159, '''ymin''': 120, '''xmax''': 480, '''ymax''': 359}}, {'''score''': 0.3_376, '''label''': '''LABEL_0''', '''box''': {'''xmin''': 159, '''ymin''': 120, '''xmax''': 480, '''ymax''': 359}}, ] , ) A_ = object_detector( [ '''http://images.cocodataset.org/val2017/000000039769.jpg''', '''http://images.cocodataset.org/val2017/000000039769.jpg''', ] , threshold=0.0 , ) self.assertEqual( nested_simplify(UpperCAmelCase_ , decimals=4 ) , [ [ {'''score''': 0.3_376, '''label''': '''LABEL_0''', '''box''': {'''xmin''': 159, '''ymin''': 120, '''xmax''': 480, '''ymax''': 359}}, {'''score''': 0.3_376, '''label''': '''LABEL_0''', '''box''': {'''xmin''': 159, '''ymin''': 120, '''xmax''': 480, '''ymax''': 359}}, ], [ {'''score''': 0.3_376, '''label''': '''LABEL_0''', '''box''': {'''xmin''': 159, '''ymin''': 120, '''xmax''': 480, '''ymax''': 359}}, {'''score''': 0.3_376, '''label''': '''LABEL_0''', '''box''': {'''xmin''': 159, '''ymin''': 120, '''xmax''': 480, '''ymax''': 359}}, ], ] , ) @require_torch @slow def __A ( self ) -> Dict: A_ = "facebook/detr-resnet-50" A_ = AutoModelForObjectDetection.from_pretrained(UpperCAmelCase_ ) A_ = AutoFeatureExtractor.from_pretrained(UpperCAmelCase_ ) A_ = ObjectDetectionPipeline(model=UpperCAmelCase_ , feature_extractor=UpperCAmelCase_ ) A_ = object_detector('''http://images.cocodataset.org/val2017/000000039769.jpg''' ) self.assertEqual( nested_simplify(UpperCAmelCase_ , decimals=4 ) , [ {'''score''': 0.9_982, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 70, '''xmax''': 175, '''ymax''': 117}}, {'''score''': 0.9_960, '''label''': '''remote''', '''box''': {'''xmin''': 333, '''ymin''': 72, '''xmax''': 368, '''ymax''': 187}}, {'''score''': 0.9_955, '''label''': '''couch''', '''box''': {'''xmin''': 0, '''ymin''': 1, '''xmax''': 639, '''ymax''': 473}}, {'''score''': 0.9_988, '''label''': '''cat''', '''box''': {'''xmin''': 13, '''ymin''': 52, '''xmax''': 314, '''ymax''': 470}}, {'''score''': 0.9_987, '''label''': '''cat''', '''box''': {'''xmin''': 345, '''ymin''': 23, '''xmax''': 640, '''ymax''': 368}}, ] , ) A_ = object_detector( [ '''http://images.cocodataset.org/val2017/000000039769.jpg''', '''http://images.cocodataset.org/val2017/000000039769.jpg''', ] ) self.assertEqual( nested_simplify(UpperCAmelCase_ , decimals=4 ) , [ [ {'''score''': 0.9_982, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 70, '''xmax''': 175, '''ymax''': 117}}, {'''score''': 0.9_960, '''label''': '''remote''', '''box''': {'''xmin''': 333, '''ymin''': 72, '''xmax''': 368, '''ymax''': 187}}, {'''score''': 0.9_955, '''label''': '''couch''', '''box''': {'''xmin''': 0, '''ymin''': 1, '''xmax''': 639, '''ymax''': 473}}, {'''score''': 0.9_988, '''label''': '''cat''', '''box''': {'''xmin''': 13, '''ymin''': 52, '''xmax''': 314, '''ymax''': 470}}, {'''score''': 0.9_987, '''label''': '''cat''', '''box''': {'''xmin''': 345, '''ymin''': 23, '''xmax''': 640, '''ymax''': 368}}, ], [ {'''score''': 0.9_982, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 70, '''xmax''': 175, '''ymax''': 117}}, {'''score''': 0.9_960, '''label''': '''remote''', '''box''': {'''xmin''': 333, '''ymin''': 72, '''xmax''': 368, '''ymax''': 187}}, {'''score''': 0.9_955, '''label''': '''couch''', '''box''': {'''xmin''': 0, '''ymin''': 1, '''xmax''': 639, '''ymax''': 473}}, {'''score''': 0.9_988, '''label''': '''cat''', '''box''': {'''xmin''': 13, '''ymin''': 52, '''xmax''': 314, '''ymax''': 470}}, {'''score''': 0.9_987, '''label''': '''cat''', '''box''': {'''xmin''': 345, '''ymin''': 23, '''xmax''': 640, '''ymax''': 368}}, ], ] , ) @require_torch @slow def __A ( self ) -> Tuple: A_ = "facebook/detr-resnet-50" A_ = pipeline('''object-detection''' , model=UpperCAmelCase_ ) A_ = object_detector('''http://images.cocodataset.org/val2017/000000039769.jpg''' ) self.assertEqual( nested_simplify(UpperCAmelCase_ , decimals=4 ) , [ {'''score''': 0.9_982, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 70, '''xmax''': 175, '''ymax''': 117}}, {'''score''': 0.9_960, '''label''': '''remote''', '''box''': {'''xmin''': 333, '''ymin''': 72, '''xmax''': 368, '''ymax''': 187}}, {'''score''': 0.9_955, '''label''': '''couch''', '''box''': {'''xmin''': 0, '''ymin''': 1, '''xmax''': 639, '''ymax''': 473}}, {'''score''': 0.9_988, '''label''': '''cat''', '''box''': {'''xmin''': 13, '''ymin''': 52, '''xmax''': 314, '''ymax''': 470}}, {'''score''': 0.9_987, '''label''': '''cat''', '''box''': {'''xmin''': 345, '''ymin''': 23, '''xmax''': 640, '''ymax''': 368}}, ] , ) A_ = object_detector( [ '''http://images.cocodataset.org/val2017/000000039769.jpg''', '''http://images.cocodataset.org/val2017/000000039769.jpg''', ] ) self.assertEqual( nested_simplify(UpperCAmelCase_ , decimals=4 ) , [ [ {'''score''': 0.9_982, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 70, '''xmax''': 175, '''ymax''': 117}}, {'''score''': 0.9_960, '''label''': '''remote''', '''box''': {'''xmin''': 333, '''ymin''': 72, '''xmax''': 368, '''ymax''': 187}}, {'''score''': 0.9_955, '''label''': '''couch''', '''box''': {'''xmin''': 0, '''ymin''': 1, '''xmax''': 639, '''ymax''': 473}}, {'''score''': 0.9_988, '''label''': '''cat''', '''box''': {'''xmin''': 13, '''ymin''': 52, '''xmax''': 314, '''ymax''': 470}}, {'''score''': 0.9_987, '''label''': '''cat''', '''box''': {'''xmin''': 345, '''ymin''': 23, '''xmax''': 640, '''ymax''': 368}}, ], [ {'''score''': 0.9_982, '''label''': '''remote''', '''box''': {'''xmin''': 40, '''ymin''': 70, '''xmax''': 175, '''ymax''': 117}}, {'''score''': 0.9_960, '''label''': '''remote''', '''box''': {'''xmin''': 333, '''ymin''': 72, '''xmax''': 368, '''ymax''': 187}}, {'''score''': 0.9_955, '''label''': '''couch''', '''box''': {'''xmin''': 0, '''ymin''': 1, '''xmax''': 639, '''ymax''': 473}}, {'''score''': 0.9_988, '''label''': '''cat''', '''box''': {'''xmin''': 13, '''ymin''': 52, '''xmax''': 314, '''ymax''': 470}}, {'''score''': 0.9_987, '''label''': '''cat''', '''box''': {'''xmin''': 345, '''ymin''': 23, '''xmax''': 640, '''ymax''': 368}}, ], ] , ) @require_torch @slow def __A ( self ) -> List[str]: A_ = 0.9_985 A_ = "facebook/detr-resnet-50" A_ = pipeline('''object-detection''' , model=UpperCAmelCase_ ) A_ = object_detector('''http://images.cocodataset.org/val2017/000000039769.jpg''' , threshold=UpperCAmelCase_ ) self.assertEqual( nested_simplify(UpperCAmelCase_ , decimals=4 ) , [ {'''score''': 0.9_988, '''label''': '''cat''', '''box''': {'''xmin''': 13, '''ymin''': 52, '''xmax''': 314, '''ymax''': 470}}, {'''score''': 0.9_987, '''label''': '''cat''', '''box''': {'''xmin''': 345, '''ymin''': 23, '''xmax''': 640, '''ymax''': 368}}, ] , ) @require_torch @require_pytesseract @slow def __A ( self ) -> Dict: A_ = "Narsil/layoutlmv3-finetuned-funsd" A_ = 0.9_993 A_ = pipeline('''object-detection''' , model=UpperCAmelCase_ , threshold=UpperCAmelCase_ ) A_ = object_detector( '''https://huggingface.co/spaces/impira/docquery/resolve/2359223c1837a7587402bda0f2643382a6eefeab/invoice.png''' ) self.assertEqual( nested_simplify(UpperCAmelCase_ , decimals=4 ) , [ {'''score''': 0.9_993, '''label''': '''I-ANSWER''', '''box''': {'''xmin''': 294, '''ymin''': 254, '''xmax''': 343, '''ymax''': 264}}, {'''score''': 0.9_993, '''label''': '''I-ANSWER''', '''box''': {'''xmin''': 294, '''ymin''': 254, '''xmax''': 343, '''ymax''': 264}}, ] , )
364
'''simple docstring''' from collections import defaultdict def _UpperCAmelCase ( _UpperCamelCase : int ) -> int: A_ = 1 A_ = True for v in tree[start]: if v not in visited: ret += dfs(_UpperCamelCase ) if ret % 2 == 0: cuts.append(_UpperCamelCase ) return ret def _UpperCAmelCase ( ) -> Optional[Any]: dfs(1 ) if __name__ == "__main__": __snake_case , __snake_case : Union[str, Any] = 10, 9 __snake_case : int = defaultdict(list) __snake_case : dict[int, bool] = {} __snake_case : list[int] = [] __snake_case : Union[str, Any] = 0 __snake_case : int = [(2, 1), (3, 1), (4, 3), (5, 2), (6, 1), (7, 2), (8, 6), (9, 8), (10, 8)] for u, v in edges: tree[u].append(v) tree[v].append(u) even_tree() print(len(cuts) - 1)
18
0
'''simple docstring''' import html from ...feature_extraction_utils import BatchFeature, FeatureExtractionMixin from ...utils import is_bsa_available, logging, requires_backends if is_bsa_available(): import bsa from bsa import BeautifulSoup __snake_case : str = logging.get_logger(__name__) class __UpperCAmelCase ( lowerCamelCase_ ): def __init__( self , **_SCREAMING_SNAKE_CASE ) -> Dict: requires_backends(self , ['''bs4'''] ) super().__init__(**lowerCAmelCase__ ) def __A ( self , _SCREAMING_SNAKE_CASE ) -> Dict: A_ = [] A_ = [] A_ = element if element.name else element.parent for parent in child.parents: # type: bs4.element.Tag A_ = parent.find_all(child.name , recursive=lowerCAmelCase__ ) xpath_tags.append(child.name ) xpath_subscripts.append( 0 if 1 == len(lowerCAmelCase__ ) else next(i for i, s in enumerate(lowerCAmelCase__ , 1 ) if s is child ) ) A_ = parent xpath_tags.reverse() xpath_subscripts.reverse() return xpath_tags, xpath_subscripts def __A ( self , _SCREAMING_SNAKE_CASE ) -> Optional[Any]: A_ = BeautifulSoup(lowerCAmelCase__ , '''html.parser''' ) A_ = [] A_ = [] A_ = [] for element in html_code.descendants: if type(lowerCAmelCase__ ) == bsa.element.NavigableString: if type(element.parent ) != bsa.element.Tag: continue A_ = html.unescape(lowerCAmelCase__ ).strip() if not text_in_this_tag: continue all_doc_strings.append(lowerCAmelCase__ ) A_ ,A_ = self.xpath_soup(lowerCAmelCase__ ) stringaxtag_seq.append(lowerCAmelCase__ ) stringaxsubs_seq.append(lowerCAmelCase__ ) if len(lowerCAmelCase__ ) != len(lowerCAmelCase__ ): raise ValueError('''Number of doc strings and xtags does not correspond''' ) if len(lowerCAmelCase__ ) != len(lowerCAmelCase__ ): raise ValueError('''Number of doc strings and xsubs does not correspond''' ) return all_doc_strings, stringaxtag_seq, stringaxsubs_seq def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> List[Any]: A_ = '''''' for tagname, subs in zip(lowerCAmelCase__ , lowerCAmelCase__ ): xpath += F'''/{tagname}''' if subs != 0: xpath += F'''[{subs}]''' return xpath def __call__( self , _SCREAMING_SNAKE_CASE ) -> BatchFeature: A_ = False # Check that strings has a valid type if isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): A_ = True elif isinstance(lowerCAmelCase__ , (list, tuple) ): if len(lowerCAmelCase__ ) == 0 or isinstance(html_strings[0] , lowerCAmelCase__ ): A_ = True if not valid_strings: raise ValueError( '''HTML strings must of type `str`, `List[str]` (batch of examples), ''' F'''but is of type {type(lowerCAmelCase__ )}.''' ) A_ = bool(isinstance(lowerCAmelCase__ , (list, tuple) ) and (isinstance(html_strings[0] , lowerCAmelCase__ )) ) if not is_batched: A_ = [html_strings] # Get nodes + xpaths A_ = [] A_ = [] for html_string in html_strings: A_ ,A_ ,A_ = self.get_three_from_single(lowerCAmelCase__ ) nodes.append(lowerCAmelCase__ ) A_ = [] for node, tag_list, sub_list in zip(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ): A_ = self.construct_xpath(lowerCAmelCase__ , lowerCAmelCase__ ) xpath_strings.append(lowerCAmelCase__ ) xpaths.append(lowerCAmelCase__ ) # return as Dict A_ = {'''nodes''': nodes, '''xpaths''': xpaths} A_ = BatchFeature(data=lowerCAmelCase__ , tensor_type=lowerCAmelCase__ ) return encoded_inputs
365
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging __snake_case : List[str] = logging.get_logger(__name__) __snake_case : Union[str, Any] = { 'alibaba-damo/mgp-str-base': 'https://huggingface.co/alibaba-damo/mgp-str-base/resolve/main/config.json', } class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' __lowercase : Optional[int] = 'mgp-str' def __init__( self , _SCREAMING_SNAKE_CASE=[32, 128] , _SCREAMING_SNAKE_CASE=4 , _SCREAMING_SNAKE_CASE=3 , _SCREAMING_SNAKE_CASE=27 , _SCREAMING_SNAKE_CASE=38 , _SCREAMING_SNAKE_CASE=5_0257 , _SCREAMING_SNAKE_CASE=3_0522 , _SCREAMING_SNAKE_CASE=768 , _SCREAMING_SNAKE_CASE=12 , _SCREAMING_SNAKE_CASE=12 , _SCREAMING_SNAKE_CASE=4.0 , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=False , _SCREAMING_SNAKE_CASE=1E-5 , _SCREAMING_SNAKE_CASE=0.0 , _SCREAMING_SNAKE_CASE=0.0 , _SCREAMING_SNAKE_CASE=0.0 , _SCREAMING_SNAKE_CASE=False , _SCREAMING_SNAKE_CASE=0.02 , **_SCREAMING_SNAKE_CASE , ) -> List[Any]: super().__init__(**_SCREAMING_SNAKE_CASE ) A_ = image_size A_ = patch_size A_ = num_channels A_ = max_token_length A_ = num_character_labels A_ = num_bpe_labels A_ = num_wordpiece_labels A_ = hidden_size A_ = num_hidden_layers A_ = num_attention_heads A_ = mlp_ratio A_ = distilled A_ = layer_norm_eps A_ = drop_rate A_ = qkv_bias A_ = attn_drop_rate A_ = drop_path_rate A_ = output_aa_attentions A_ = initializer_range
18
0
'''simple docstring''' from heapq import heappop, heappush import numpy as np def _UpperCAmelCase ( _UpperCamelCase : np.ndarray, _UpperCamelCase : tuple[int, int], _UpperCamelCase : tuple[int, int], _UpperCamelCase : bool, ) -> tuple[float | int, list[tuple[int, int]]]: A_ ,A_ = grid.shape A_ = [-1, 1, 0, 0] A_ = [0, 0, -1, 1] if allow_diagonal: dx += [-1, -1, 1, 1] dy += [-1, 1, -1, 1] A_ ,A_ = [(0, source)], set() A_ = np.full((rows, cols), np.inf ) A_ = 0 A_ = np.empty((rows, cols), dtype=UpperCamelCase__ ) A_ = None while queue: ((A_) ,(A_)) = heappop(UpperCamelCase__ ) if (x, y) in visited: continue visited.add((x, y) ) if (x, y) == destination: A_ = [] while (x, y) != source: path.append((x, y) ) A_ ,A_ = predecessors[x, y] path.append(UpperCamelCase__ ) # add the source manually path.reverse() return matrix[destination], path for i in range(len(UpperCamelCase__ ) ): A_ ,A_ = x + dx[i], y + dy[i] if 0 <= nx < rows and 0 <= ny < cols: A_ = grid[nx][ny] if next_node == 1 and matrix[nx, ny] > dist + 1: heappush(UpperCamelCase__, (dist + 1, (nx, ny)) ) A_ = dist + 1 A_ = (x, y) return np.inf, [] if __name__ == "__main__": import doctest doctest.testmod()
366
'''simple docstring''' from diffusers.utils.testing_utils import require_onnxruntime @require_onnxruntime class __UpperCAmelCase : '''simple docstring''' pass
18
0
'''simple docstring''' import os import tempfile import unittest import uuid from pathlib import Path from transformers.testing_utils import get_tests_dir, require_soundfile, require_torch, require_vision from transformers.tools.agent_types import AgentAudio, AgentImage, AgentText from transformers.utils import is_soundfile_availble, is_torch_available, is_vision_available if is_torch_available(): import torch if is_soundfile_availble(): import soundfile as sf if is_vision_available(): from PIL import Image def _UpperCAmelCase ( _UpperCamelCase : Union[str, Any]="" ) -> str: A_ = tempfile.mkdtemp() return os.path.join(__lowerCamelCase, str(uuid.uuida() ) + suffix ) @require_soundfile @require_torch class __UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' def __A ( self ) -> List[Any]: A_ = torch.rand(12 , dtype=torch.floataa ) - 0.5 A_ = AgentAudio(_lowerCamelCase ) A_ = str(agent_type.to_string() ) # Ensure that the tensor and the agent_type's tensor are the same self.assertTrue(torch.allclose(_lowerCamelCase , agent_type.to_raw() , atol=1E-4 ) ) del agent_type # Ensure the path remains even after the object deletion self.assertTrue(os.path.exists(_lowerCamelCase ) ) # Ensure that the file contains the same value as the original tensor A_ ,A_ = sf.read(_lowerCamelCase ) self.assertTrue(torch.allclose(_lowerCamelCase , torch.tensor(_lowerCamelCase ) , atol=1E-4 ) ) def __A ( self ) -> Tuple: A_ = torch.rand(12 , dtype=torch.floataa ) - 0.5 A_ = get_new_path(suffix='''.wav''' ) sf.write(_lowerCamelCase , _lowerCamelCase , 1_6000 ) A_ = AgentAudio(_lowerCamelCase ) self.assertTrue(torch.allclose(_lowerCamelCase , agent_type.to_raw() , atol=1E-4 ) ) self.assertEqual(agent_type.to_string() , _lowerCamelCase ) @require_vision @require_torch class __UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' def __A ( self ) -> Optional[Any]: A_ = torch.randint(0 , 256 , (64, 64, 3) ) A_ = AgentImage(_lowerCamelCase ) A_ = str(agent_type.to_string() ) # Ensure that the tensor and the agent_type's tensor are the same self.assertTrue(torch.allclose(_lowerCamelCase , agent_type._tensor , atol=1E-4 ) ) self.assertIsInstance(agent_type.to_raw() , Image.Image ) # Ensure the path remains even after the object deletion del agent_type self.assertTrue(os.path.exists(_lowerCamelCase ) ) def __A ( self ) -> str: A_ = Path(get_tests_dir('''fixtures/tests_samples/COCO''' ) ) / '''000000039769.png''' A_ = Image.open(_lowerCamelCase ) A_ = AgentImage(_lowerCamelCase ) self.assertTrue(path.samefile(agent_type.to_string() ) ) self.assertTrue(image == agent_type.to_raw() ) # Ensure the path remains even after the object deletion del agent_type self.assertTrue(os.path.exists(_lowerCamelCase ) ) def __A ( self ) -> Union[str, Any]: A_ = Path(get_tests_dir('''fixtures/tests_samples/COCO''' ) ) / '''000000039769.png''' A_ = Image.open(_lowerCamelCase ) A_ = AgentImage(_lowerCamelCase ) self.assertFalse(path.samefile(agent_type.to_string() ) ) self.assertTrue(image == agent_type.to_raw() ) # Ensure the path remains even after the object deletion del agent_type self.assertTrue(os.path.exists(_lowerCamelCase ) ) class __UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' def __A ( self ) -> Union[str, Any]: A_ = '''Hey!''' A_ = AgentText(_lowerCamelCase ) self.assertEqual(_lowerCamelCase , agent_type.to_string() ) self.assertEqual(_lowerCamelCase , agent_type.to_raw() ) self.assertEqual(_lowerCamelCase , _lowerCamelCase )
367
'''simple docstring''' # Copyright 2021 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from argparse import ArgumentParser from accelerate.commands.config import get_config_parser from accelerate.commands.env import env_command_parser from accelerate.commands.launch import launch_command_parser from accelerate.commands.test import test_command_parser from accelerate.commands.tpu import tpu_command_parser def _UpperCAmelCase ( ) -> Dict: A_ = ArgumentParser('''Accelerate CLI tool''', usage='''accelerate <command> [<args>]''', allow_abbrev=_UpperCamelCase ) A_ = parser.add_subparsers(help='''accelerate command helpers''' ) # Register commands get_config_parser(subparsers=_UpperCamelCase ) env_command_parser(subparsers=_UpperCamelCase ) launch_command_parser(subparsers=_UpperCamelCase ) tpu_command_parser(subparsers=_UpperCamelCase ) test_command_parser(subparsers=_UpperCamelCase ) # Let's go A_ = parser.parse_args() if not hasattr(_UpperCamelCase, '''func''' ): parser.print_help() exit(1 ) # Run args.func(_UpperCamelCase ) if __name__ == "__main__": main()
18
0
'''simple docstring''' def _UpperCAmelCase ( _UpperCamelCase : int ) -> str: A_ = int(_snake_case ) if decimal in (0, 1): # Exit cases for the recursion return str(_snake_case ) A_ = divmod(_snake_case, 2 ) return binary_recursive(_snake_case ) + str(_snake_case ) def _UpperCAmelCase ( _UpperCamelCase : str ) -> str: A_ = str(_snake_case ).strip() if not number: raise ValueError('''No input value was provided''' ) A_ = "-" if number.startswith('''-''' ) else "" A_ = number.lstrip('''-''' ) if not number.isnumeric(): raise ValueError('''Input value is not an integer''' ) return F'''{negative}0b{binary_recursive(int(_snake_case ) )}''' if __name__ == "__main__": from doctest import testmod testmod()
368
'''simple docstring''' import absl # noqa: F401 # Here to have a nice missing dependency error message early on import nltk # noqa: F401 # Here to have a nice missing dependency error message early on import numpy # noqa: F401 # Here to have a nice missing dependency error message early on import six # noqa: F401 # Here to have a nice missing dependency error message early on from rouge_score import rouge_scorer, scoring import datasets __snake_case : Any = '\\n@inproceedings{lin-2004-rouge,\n title = "{ROUGE}: A Package for Automatic Evaluation of Summaries",\n author = "Lin, Chin-Yew",\n booktitle = "Text Summarization Branches Out",\n month = jul,\n year = "2004",\n address = "Barcelona, Spain",\n publisher = "Association for Computational Linguistics",\n url = "https://www.aclweb.org/anthology/W04-1013",\n pages = "74--81",\n}\n' __snake_case : Dict = '\\nROUGE, or Recall-Oriented Understudy for Gisting Evaluation, is a set of metrics and a software package used for\nevaluating automatic summarization and machine translation software in natural language processing.\nThe metrics compare an automatically produced summary or translation against a reference or a set of references (human-produced) summary or translation.\n\nNote that ROUGE is case insensitive, meaning that upper case letters are treated the same way as lower case letters.\n\nThis metrics is a wrapper around Google Research reimplementation of ROUGE:\nhttps://github.com/google-research/google-research/tree/master/rouge\n' __snake_case : Optional[int] = '\nCalculates average rouge scores for a list of hypotheses and references\nArgs:\n predictions: list of predictions to score. Each prediction\n should be a string with tokens separated by spaces.\n references: list of reference for each prediction. Each\n reference should be a string with tokens separated by spaces.\n rouge_types: A list of rouge types to calculate.\n Valid names:\n `"rouge{n}"` (e.g. `"rouge1"`, `"rouge2"`) where: {n} is the n-gram based scoring,\n `"rougeL"`: Longest common subsequence based scoring.\n `"rougeLSum"`: rougeLsum splits text using `"\n"`.\n See details in https://github.com/huggingface/datasets/issues/617\n use_stemmer: Bool indicating whether Porter stemmer should be used to strip word suffixes.\n use_aggregator: Return aggregates if this is set to True\nReturns:\n rouge1: rouge_1 (precision, recall, f1),\n rouge2: rouge_2 (precision, recall, f1),\n rougeL: rouge_l (precision, recall, f1),\n rougeLsum: rouge_lsum (precision, recall, f1)\nExamples:\n\n >>> rouge = datasets.load_metric(\'rouge\')\n >>> predictions = ["hello there", "general kenobi"]\n >>> references = ["hello there", "general kenobi"]\n >>> results = rouge.compute(predictions=predictions, references=references)\n >>> print(list(results.keys()))\n [\'rouge1\', \'rouge2\', \'rougeL\', \'rougeLsum\']\n >>> print(results["rouge1"])\n AggregateScore(low=Score(precision=1.0, recall=1.0, fmeasure=1.0), mid=Score(precision=1.0, recall=1.0, fmeasure=1.0), high=Score(precision=1.0, recall=1.0, fmeasure=1.0))\n >>> print(results["rouge1"].mid.fmeasure)\n 1.0\n' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __UpperCAmelCase ( datasets.Metric ): '''simple docstring''' def __A ( self ) -> List[str]: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Value('''string''' , id='''sequence''' ), '''references''': datasets.Value('''string''' , id='''sequence''' ), } ) , codebase_urls=['''https://github.com/google-research/google-research/tree/master/rouge'''] , reference_urls=[ '''https://en.wikipedia.org/wiki/ROUGE_(metric)''', '''https://github.com/google-research/google-research/tree/master/rouge''', ] , ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=False ) -> Optional[int]: if rouge_types is None: A_ = ['''rouge1''', '''rouge2''', '''rougeL''', '''rougeLsum'''] A_ = rouge_scorer.RougeScorer(rouge_types=_SCREAMING_SNAKE_CASE , use_stemmer=_SCREAMING_SNAKE_CASE ) if use_aggregator: A_ = scoring.BootstrapAggregator() else: A_ = [] for ref, pred in zip(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): A_ = scorer.score(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) if use_aggregator: aggregator.add_scores(_SCREAMING_SNAKE_CASE ) else: scores.append(_SCREAMING_SNAKE_CASE ) if use_aggregator: A_ = aggregator.aggregate() else: A_ = {} for key in scores[0]: A_ = [score[key] for score in scores] return result
18
0
'''simple docstring''' import argparse import json from pathlib import Path import requests import timm import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import AutoImageProcessor, SwinvaConfig, SwinvaForImageClassification def _UpperCAmelCase ( _UpperCamelCase : Tuple ) -> List[Any]: A_ = SwinvaConfig() A_ = swinva_name.split('''_''' ) A_ = name_split[1] if "to" in name_split[3]: A_ = int(name_split[3][-3:] ) else: A_ = int(name_split[3] ) if "to" in name_split[2]: A_ = int(name_split[2][-2:] ) else: A_ = int(name_split[2][6:] ) if model_size == "tiny": A_ = 96 A_ = (2, 2, 6, 2) A_ = (3, 6, 12, 24) elif model_size == "small": A_ = 96 A_ = (2, 2, 18, 2) A_ = (3, 6, 12, 24) elif model_size == "base": A_ = 1_28 A_ = (2, 2, 18, 2) A_ = (4, 8, 16, 32) else: A_ = 1_92 A_ = (2, 2, 18, 2) A_ = (6, 12, 24, 48) if "to" in swinva_name: A_ = (12, 12, 12, 6) if ("22k" in swinva_name) and ("to" not in swinva_name): A_ = 2_18_41 A_ = "huggingface/label-files" A_ = "imagenet-22k-id2label.json" A_ = json.load(open(hf_hub_download(_UpperCamelCase, _UpperCamelCase, repo_type='''dataset''' ), '''r''' ) ) A_ = {int(_UpperCamelCase ): v for k, v in idalabel.items()} A_ = idalabel A_ = {v: k for k, v in idalabel.items()} else: A_ = 10_00 A_ = "huggingface/label-files" A_ = "imagenet-1k-id2label.json" A_ = json.load(open(hf_hub_download(_UpperCamelCase, _UpperCamelCase, repo_type='''dataset''' ), '''r''' ) ) A_ = {int(_UpperCamelCase ): v for k, v in idalabel.items()} A_ = idalabel A_ = {v: k for k, v in idalabel.items()} A_ = img_size A_ = num_classes A_ = embed_dim A_ = depths A_ = num_heads A_ = window_size return config def _UpperCAmelCase ( _UpperCamelCase : Dict ) -> str: if "patch_embed.proj" in name: A_ = name.replace('''patch_embed.proj''', '''embeddings.patch_embeddings.projection''' ) if "patch_embed.norm" in name: A_ = name.replace('''patch_embed.norm''', '''embeddings.norm''' ) if "layers" in name: A_ = "encoder." + name if "attn.proj" in name: A_ = name.replace('''attn.proj''', '''attention.output.dense''' ) if "attn" in name: A_ = name.replace('''attn''', '''attention.self''' ) if "norm1" in name: A_ = name.replace('''norm1''', '''layernorm_before''' ) if "norm2" in name: A_ = name.replace('''norm2''', '''layernorm_after''' ) if "mlp.fc1" in name: A_ = name.replace('''mlp.fc1''', '''intermediate.dense''' ) if "mlp.fc2" in name: A_ = name.replace('''mlp.fc2''', '''output.dense''' ) if "q_bias" in name: A_ = name.replace('''q_bias''', '''query.bias''' ) if "k_bias" in name: A_ = name.replace('''k_bias''', '''key.bias''' ) if "v_bias" in name: A_ = name.replace('''v_bias''', '''value.bias''' ) if "cpb_mlp" in name: A_ = name.replace('''cpb_mlp''', '''continuous_position_bias_mlp''' ) if name == "norm.weight": A_ = "layernorm.weight" if name == "norm.bias": A_ = "layernorm.bias" if "head" in name: A_ = name.replace('''head''', '''classifier''' ) else: A_ = "swinv2." + name return name def _UpperCAmelCase ( _UpperCamelCase : Tuple, _UpperCamelCase : Optional[Any] ) -> str: for key in orig_state_dict.copy().keys(): A_ = orig_state_dict.pop(_UpperCamelCase ) if "mask" in key: continue elif "qkv" in key: A_ = key.split('''.''' ) A_ = int(key_split[1] ) A_ = int(key_split[3] ) A_ = model.swinva.encoder.layers[layer_num].blocks[block_num].attention.self.all_head_size if "weight" in key: A_ = val[:dim, :] A_ = val[dim : dim * 2, :] A_ = val[-dim:, :] else: A_ = val[:dim] A_ = val[ dim : dim * 2 ] A_ = val[-dim:] else: A_ = val return orig_state_dict def _UpperCAmelCase ( _UpperCamelCase : str, _UpperCamelCase : Optional[Any] ) -> Union[str, Any]: A_ = timm.create_model(_UpperCamelCase, pretrained=_UpperCamelCase ) timm_model.eval() A_ = get_swinva_config(_UpperCamelCase ) A_ = SwinvaForImageClassification(_UpperCamelCase ) model.eval() A_ = convert_state_dict(timm_model.state_dict(), _UpperCamelCase ) model.load_state_dict(_UpperCamelCase ) A_ = "http://images.cocodataset.org/val2017/000000039769.jpg" A_ = AutoImageProcessor.from_pretrained('''microsoft/{}'''.format(swinva_name.replace('''_''', '''-''' ) ) ) A_ = Image.open(requests.get(_UpperCamelCase, stream=_UpperCamelCase ).raw ) A_ = image_processor(images=_UpperCamelCase, return_tensors='''pt''' ) A_ = timm_model(inputs['''pixel_values'''] ) A_ = model(**_UpperCamelCase ).logits assert torch.allclose(_UpperCamelCase, _UpperCamelCase, atol=1E-3 ) print(F'''Saving model {swinva_name} to {pytorch_dump_folder_path}''' ) model.save_pretrained(_UpperCamelCase ) print(F'''Saving image processor to {pytorch_dump_folder_path}''' ) image_processor.save_pretrained(_UpperCamelCase ) model.push_to_hub( repo_path_or_name=Path(_UpperCamelCase, _UpperCamelCase ), organization='''nandwalritik''', commit_message='''Add model''', ) if __name__ == "__main__": __snake_case : Optional[int] = argparse.ArgumentParser() # Required parameters parser.add_argument( '--swinv2_name', default='swinv2_tiny_patch4_window8_256', type=str, help='Name of the Swinv2 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.' ) __snake_case : int = parser.parse_args() convert_swinva_checkpoint(args.swinva_name, args.pytorch_dump_folder_path)
369
'''simple docstring''' import gc import importlib.metadata import tempfile import unittest from packaging import version from transformers import ( AutoModel, AutoModelForCausalLM, AutoModelForSeqaSeqLM, AutoModelForSequenceClassification, AutoTokenizer, BitsAndBytesConfig, pipeline, ) from transformers.testing_utils import ( is_torch_available, require_accelerate, require_bitsandbytes, require_torch, require_torch_gpu, require_torch_multi_gpu, slow, ) def _UpperCAmelCase ( _UpperCamelCase : str ) -> int: if model.config.model_type == "gpt2": return model.transformer.h[0].mlp.c_fc return model.transformer.h[0].mlp.dense_ah_to_h if is_torch_available(): import torch import torch.nn as nn class __UpperCAmelCase ( nn.Module ): '''simple docstring''' def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Dict: super().__init__() A_ = module A_ = nn.Sequential( nn.Linear(module.in_features , _SCREAMING_SNAKE_CASE , bias=_SCREAMING_SNAKE_CASE ) , nn.Linear(_SCREAMING_SNAKE_CASE , module.out_features , bias=_SCREAMING_SNAKE_CASE ) , ) A_ = (2.0 / (5 * min(module.in_features , module.out_features ))) ** 0.5 nn.init.normal_(self.adapter[0].weight , std=_SCREAMING_SNAKE_CASE ) nn.init.zeros_(self.adapter[1].weight ) self.adapter.to(module.weight.device ) def __A ( self , _SCREAMING_SNAKE_CASE , *_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) -> Tuple: return self.module(_SCREAMING_SNAKE_CASE , *_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) + self.adapter(_SCREAMING_SNAKE_CASE ) @require_bitsandbytes @require_accelerate @require_torch @require_torch_gpu @slow class __UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' __lowercase : Dict = 'bigscience/bloom-1b7' # Constant values __lowercase : str = 2.109659552692574 __lowercase : int = 'Hello my name is' __lowercase : Optional[Any] = set() EXPECTED_OUTPUTS.add('Hello my name is John and I am a professional photographer. I' ) EXPECTED_OUTPUTS.add('Hello my name is John.\nI am a friend of your father.\n' ) EXPECTED_OUTPUTS.add('Hello my name is John Doe, I am a student at the University' ) __lowercase : Optional[Any] = 10 def __A ( self ) -> List[str]: # Models and tokenizer A_ = AutoTokenizer.from_pretrained(self.model_name ) class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' def __A ( self ) -> List[Any]: super().setUp() # Models and tokenizer A_ = AutoModelForCausalLM.from_pretrained( self.model_name , torch_dtype=torch.floataa , device_map='''auto''' ) A_ = AutoModelForCausalLM.from_pretrained(self.model_name , load_in_abit=_SCREAMING_SNAKE_CASE , device_map='''auto''' ) def __A ( self ) -> List[str]: del self.model_fpaa del self.model_abit gc.collect() torch.cuda.empty_cache() def __A ( self ) -> Tuple: A_ = self.model_abit.config self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE , '''quantization_config''' ) ) A_ = config.to_dict() A_ = config.to_diff_dict() A_ = config.to_json_string() def __A ( self ) -> Union[str, Any]: from bitsandbytes.nn import Paramsabit A_ = self.model_fpaa.get_memory_footprint() A_ = self.model_abit.get_memory_footprint() self.assertAlmostEqual(mem_fpaa / mem_abit , self.EXPECTED_RELATIVE_DIFFERENCE ) A_ = get_some_linear_layer(self.model_abit ) self.assertTrue(linear.weight.__class__ == Paramsabit ) def __A ( self ) -> Union[str, Any]: from transformers import TaPreTrainedModel self.model_fpaa.get_memory_footprint() self.model_abit.get_memory_footprint() for name, module in self.model_abit.named_modules(): if isinstance(_SCREAMING_SNAKE_CASE , torch.nn.Linear ): if name not in ["lm_head"] + TaPreTrainedModel._keep_in_fpaa_modules: # 4-bit parameters are packed in uint8 variables self.assertTrue(module.weight.dtype == torch.uinta ) def __A ( self ) -> Optional[int]: A_ = self.tokenizer(self.input_text , return_tensors='''pt''' ) A_ = self.model_abit.generate(input_ids=encoded_input['''input_ids'''].to(0 ) , max_new_tokens=10 ) self.assertIn(self.tokenizer.decode(output_sequences[0] , skip_special_tokens=_SCREAMING_SNAKE_CASE ) , self.EXPECTED_OUTPUTS ) def __A ( self ) -> Optional[int]: A_ = BitsAndBytesConfig() A_ = True A_ = AutoModelForCausalLM.from_pretrained( self.model_name , quantization_config=_SCREAMING_SNAKE_CASE , device_map='''auto''' ) A_ = self.tokenizer(self.input_text , return_tensors='''pt''' ) A_ = model_abit_from_config.generate( input_ids=encoded_input['''input_ids'''].to(0 ) , max_new_tokens=10 ) self.assertIn(self.tokenizer.decode(output_sequences[0] , skip_special_tokens=_SCREAMING_SNAKE_CASE ) , self.EXPECTED_OUTPUTS ) def __A ( self ) -> Tuple: with self.assertRaises(_SCREAMING_SNAKE_CASE ), tempfile.TemporaryDirectory() as tmpdirname: self.model_abit.save_pretrained(_SCREAMING_SNAKE_CASE ) def __A ( self ) -> Tuple: A_ = BitsAndBytesConfig() with self.assertRaises(_SCREAMING_SNAKE_CASE ): A_ = AutoModelForCausalLM.from_pretrained( self.model_name , quantization_config=_SCREAMING_SNAKE_CASE , load_in_abit=_SCREAMING_SNAKE_CASE , device_map='''auto''' , bnb_abit_quant_type='''nf4''' , ) def __A ( self ) -> Dict: with self.assertRaises(_SCREAMING_SNAKE_CASE ): # Tries with `str` self.model_abit.to('''cpu''' ) with self.assertRaises(_SCREAMING_SNAKE_CASE ): # Tries with a `dtype`` self.model_abit.to(torch.floataa ) with self.assertRaises(_SCREAMING_SNAKE_CASE ): # Tries with a `device` self.model_abit.to(torch.device('''cuda:0''' ) ) with self.assertRaises(_SCREAMING_SNAKE_CASE ): # Tries with a `device` self.model_abit.float() with self.assertRaises(_SCREAMING_SNAKE_CASE ): # Tries with a `device` self.model_abit.half() # Test if we did not break anything A_ = self.tokenizer(self.input_text , return_tensors='''pt''' ) A_ = self.model_fpaa.to(torch.floataa ) A_ = self.model_fpaa.generate(input_ids=encoded_input['''input_ids'''].to(0 ) , max_new_tokens=10 ) # Check this does not throw an error A_ = self.model_fpaa.to('''cpu''' ) # Check this does not throw an error A_ = self.model_fpaa.half() # Check this does not throw an error A_ = self.model_fpaa.float() def __A ( self ) -> Optional[int]: A_ = AutoModelForSeqaSeqLM.from_pretrained('''t5-small''' , load_in_abit=_SCREAMING_SNAKE_CASE , device_map='''auto''' ) self.assertTrue(model.decoder.block[0].layer[2].DenseReluDense.wo.weight.dtype == torch.floataa ) @require_bitsandbytes @require_accelerate @require_torch @require_torch_gpu @slow class __UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' @classmethod def __A ( cls ) -> Optional[Any]: A_ = '''t5-small''' A_ = '''google/flan-t5-small''' # flan-t5 uses dense-act instead of dense-relu-dense A_ = AutoTokenizer.from_pretrained(cls.model_name ) A_ = '''Translate in German: Hello, my dog is cute''' def __A ( self ) -> Any: gc.collect() torch.cuda.empty_cache() def __A ( self ) -> Tuple: from transformers import TaForConditionalGeneration A_ = TaForConditionalGeneration._keep_in_fpaa_modules A_ = None # test with `t5-small` A_ = TaForConditionalGeneration.from_pretrained(self.model_name , load_in_abit=_SCREAMING_SNAKE_CASE , device_map='''auto''' ) A_ = self.tokenizer(self.input_text , return_tensors='''pt''' ).to(0 ) A_ = model.generate(**_SCREAMING_SNAKE_CASE ) # test with `flan-t5-small` A_ = TaForConditionalGeneration.from_pretrained( self.dense_act_model_name , load_in_abit=_SCREAMING_SNAKE_CASE , device_map='''auto''' ) A_ = self.tokenizer(self.input_text , return_tensors='''pt''' ).to(0 ) A_ = model.generate(**_SCREAMING_SNAKE_CASE ) A_ = modules def __A ( self ) -> Dict: import bitsandbytes as bnb from transformers import TaForConditionalGeneration # test with `t5-small` A_ = TaForConditionalGeneration.from_pretrained(self.model_name , load_in_abit=_SCREAMING_SNAKE_CASE , device_map='''auto''' ) # there was a bug with decoders - this test checks that it is fixed self.assertTrue(isinstance(model.decoder.block[0].layer[0].SelfAttention.q , bnb.nn.Linearabit ) ) A_ = self.tokenizer(self.input_text , return_tensors='''pt''' ).to(0 ) A_ = model.generate(**_SCREAMING_SNAKE_CASE ) # test with `flan-t5-small` A_ = TaForConditionalGeneration.from_pretrained( self.dense_act_model_name , load_in_abit=_SCREAMING_SNAKE_CASE , device_map='''auto''' ) A_ = self.tokenizer(self.input_text , return_tensors='''pt''' ).to(0 ) A_ = model.generate(**_SCREAMING_SNAKE_CASE ) class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' def __A ( self ) -> int: super().setUp() # model_name A_ = '''bigscience/bloom-560m''' A_ = '''t5-small''' # Different types of model A_ = AutoModel.from_pretrained(self.model_name , load_in_abit=_SCREAMING_SNAKE_CASE , device_map='''auto''' ) # Sequence classification model A_ = AutoModelForSequenceClassification.from_pretrained( self.model_name , load_in_abit=_SCREAMING_SNAKE_CASE , device_map='''auto''' ) # CausalLM model A_ = AutoModelForCausalLM.from_pretrained(self.model_name , load_in_abit=_SCREAMING_SNAKE_CASE , device_map='''auto''' ) # Seq2seq model A_ = AutoModelForSeqaSeqLM.from_pretrained( self.seq_to_seq_name , load_in_abit=_SCREAMING_SNAKE_CASE , device_map='''auto''' ) def __A ( self ) -> Union[str, Any]: del self.base_model del self.sequence_model del self.model_abit del self.seq_to_seq_model gc.collect() torch.cuda.empty_cache() def __A ( self ) -> List[str]: from bitsandbytes.nn import Paramsabit self.assertTrue(self.base_model.h[-1].mlp.dense_ah_to_h.weight.__class__ == Paramsabit ) # Other heads should be nn.Parameter self.assertTrue(self.model_abit.lm_head.weight.__class__ == torch.nn.Parameter ) self.assertTrue(self.sequence_model.score.weight.__class__ == torch.nn.Parameter ) self.assertTrue(self.seq_to_seq_model.lm_head.weight.__class__ == torch.nn.Parameter ) class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' def __A ( self ) -> Tuple: super().setUp() def __A ( self ) -> List[Any]: del self.pipe gc.collect() torch.cuda.empty_cache() def __A ( self ) -> Optional[Any]: A_ = pipeline( '''text-generation''' , model=self.model_name , model_kwargs={'''device_map''': '''auto''', '''load_in_4bit''': True, '''torch_dtype''': torch.floataa} , max_new_tokens=self.MAX_NEW_TOKENS , ) # Real second forward pass A_ = self.pipe(self.input_text ) self.assertIn(pipeline_output[0]['''generated_text'''] , self.EXPECTED_OUTPUTS ) @require_torch_multi_gpu class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' def __A ( self ) -> List[str]: super().setUp() def __A ( self ) -> Optional[int]: A_ = AutoModelForCausalLM.from_pretrained( self.model_name , load_in_abit=_SCREAMING_SNAKE_CASE , device_map='''balanced''' ) # Check correct device map self.assertEqual(set(model_parallel.hf_device_map.values() ) , {0, 1} ) # Check that inference pass works on the model A_ = self.tokenizer(self.input_text , return_tensors='''pt''' ) # Second real batch A_ = model_parallel.generate(input_ids=encoded_input['''input_ids'''].to(0 ) , max_new_tokens=10 ) self.assertIn(self.tokenizer.decode(output_parallel[0] , skip_special_tokens=_SCREAMING_SNAKE_CASE ) , self.EXPECTED_OUTPUTS ) class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' def __A ( self ) -> str: A_ = '''facebook/opt-350m''' super().setUp() def __A ( self ) -> Optional[int]: if version.parse(importlib.metadata.version('''bitsandbytes''' ) ) < version.parse('''0.37.0''' ): return # Step 1: freeze all parameters A_ = AutoModelForCausalLM.from_pretrained(self.model_name , load_in_abit=_SCREAMING_SNAKE_CASE ) self.assertEqual(set(model.hf_device_map.values() ) , {torch.cuda.current_device()} ) for param in model.parameters(): A_ = False # freeze the model - train adapters later if param.ndim == 1: # cast the small parameters (e.g. layernorm) to fp32 for stability A_ = param.data.to(torch.floataa ) # Step 2: add adapters for _, module in model.named_modules(): if "OPTAttention" in repr(type(_SCREAMING_SNAKE_CASE ) ): A_ = LoRALayer(module.q_proj , rank=16 ) A_ = LoRALayer(module.k_proj , rank=16 ) A_ = LoRALayer(module.v_proj , rank=16 ) # Step 3: dummy batch A_ = self.tokenizer('''Test batch ''' , return_tensors='''pt''' ).to(0 ) # Step 4: Check if the gradient is not None with torch.cuda.amp.autocast(): A_ = model.forward(**_SCREAMING_SNAKE_CASE ) out.logits.norm().backward() for module in model.modules(): if isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): self.assertTrue(module.adapter[1].weight.grad is not None ) self.assertTrue(module.adapter[1].weight.grad.norm().item() > 0 ) elif isinstance(_SCREAMING_SNAKE_CASE , nn.Embedding ): self.assertTrue(module.weight.grad is None ) class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' __lowercase : int = 'gpt2-xl' __lowercase : List[Any] = 3.3191854854152187
18
0
'''simple docstring''' from __future__ import annotations import random # Maximum size of the population. Bigger could be faster but is more memory expensive. __snake_case : Any = 200 # Number of elements selected in every generation of evolution. The selection takes # place from best to worst of that generation and must be smaller than N_POPULATION. __snake_case : List[str] = 50 # Probability that an element of a generation can mutate, changing one of its genes. # This will guarantee that all genes will be used during evolution. __snake_case : Union[str, Any] = 0.4 # Just a seed to improve randomness required by the algorithm. random.seed(random.randint(0, 1_000)) def _UpperCAmelCase ( _UpperCamelCase : Dict, _UpperCamelCase : Any ) -> tuple[str, float]: A_ = len([g for position, g in enumerate(_lowerCAmelCase ) if g == main_target[position]] ) return (item, float(_lowerCAmelCase )) def _UpperCAmelCase ( _UpperCamelCase : List[Any], _UpperCamelCase : int ) -> tuple[str, str]: A_ = random.randint(0, len(_lowerCAmelCase ) - 1 ) A_ = parent_a[:random_slice] + parent_a[random_slice:] A_ = parent_a[:random_slice] + parent_a[random_slice:] return (child_a, child_a) def _UpperCAmelCase ( _UpperCamelCase : Tuple, _UpperCamelCase : Union[str, Any] ) -> str: A_ = list(_lowerCAmelCase ) if random.uniform(0, 1 ) < MUTATION_PROBABILITY: A_ = random.choice(_lowerCAmelCase ) return "".join(_lowerCAmelCase ) def _UpperCAmelCase ( _UpperCamelCase : Optional[int], _UpperCamelCase : Any, _UpperCamelCase : List[str], ) -> list[str]: A_ = [] # Generate more children proportionally to the fitness score. A_ = int(parent_a[1] * 1_00 ) + 1 A_ = 10 if child_n >= 10 else child_n for _ in range(_lowerCAmelCase ): A_ = population_score[random.randint(0, _lowerCAmelCase )][0] A_ = crossover(parent_a[0], _lowerCAmelCase ) # Append new string to the population list. pop.append(mutate(_lowerCAmelCase, _lowerCAmelCase ) ) pop.append(mutate(_lowerCAmelCase, _lowerCAmelCase ) ) return pop def _UpperCAmelCase ( _UpperCamelCase : List[str], _UpperCamelCase : Union[str, Any], _UpperCamelCase : Any = True ) -> tuple[int, int, str]: # Verify if N_POPULATION is bigger than N_SELECTED if N_POPULATION < N_SELECTED: A_ = F'''{N_POPULATION} must be bigger than {N_SELECTED}''' raise ValueError(_lowerCAmelCase ) # Verify that the target contains no genes besides the ones inside genes variable. A_ = sorted({c for c in target if c not in genes} ) if not_in_genes_list: A_ = F'''{not_in_genes_list} is not in genes list, evolution cannot converge''' raise ValueError(_lowerCAmelCase ) # Generate random starting population. A_ = [] for _ in range(_lowerCAmelCase ): population.append(''''''.join([random.choice(_lowerCAmelCase ) for i in range(len(_lowerCAmelCase ) )] ) ) # Just some logs to know what the algorithms is doing. A_ = 0, 0 # This loop will end when we find a perfect match for our target. while True: generation += 1 total_population += len(_lowerCAmelCase ) # Random population created. Now it's time to evaluate. # Adding a bit of concurrency can make everything faster, # # import concurrent.futures # population_score: list[tuple[str, float]] = [] # with concurrent.futures.ThreadPoolExecutor( # max_workers=NUM_WORKERS) as executor: # futures = {executor.submit(evaluate, item) for item in population} # concurrent.futures.wait(futures) # population_score = [item.result() for item in futures] # # but with a simple algorithm like this, it will probably be slower. # We just need to call evaluate for every item inside the population. A_ = [evaluate(_lowerCAmelCase, _lowerCAmelCase ) for item in population] # Check if there is a matching evolution. A_ = sorted(_lowerCAmelCase, key=lambda _UpperCamelCase : x[1], reverse=_lowerCAmelCase ) if population_score[0][0] == target: return (generation, total_population, population_score[0][0]) # Print the best result every 10 generation. # Just to know that the algorithm is working. if debug and generation % 10 == 0: print( F'''\nGeneration: {generation}''' F'''\nTotal Population:{total_population}''' F'''\nBest score: {population_score[0][1]}''' F'''\nBest string: {population_score[0][0]}''' ) # Flush the old population, keeping some of the best evolutions. # Keeping this avoid regression of evolution. A_ = population[: int(N_POPULATION / 3 )] population.clear() population.extend(_lowerCAmelCase ) # Normalize population score to be between 0 and 1. A_ = [ (item, score / len(_lowerCAmelCase )) for item, score in population_score ] # This is selection for i in range(_lowerCAmelCase ): population.extend(select(population_score[int(_lowerCAmelCase )], _lowerCAmelCase, _lowerCAmelCase ) ) # Check if the population has already reached the maximum value and if so, # break the cycle. If this check is disabled, the algorithm will take # forever to compute large strings, but will also calculate small strings in # a far fewer generations. if len(_lowerCAmelCase ) > N_POPULATION: break if __name__ == "__main__": __snake_case : Any = ( 'This is a genetic algorithm to evaluate, combine, evolve, and mutate a string!' ) __snake_case : str = list( ' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklm' 'nopqrstuvwxyz.,;!?+-*#@^\'èéòà€ù=)(&%$£/\\' ) __snake_case , __snake_case , __snake_case : Optional[int] = basic(target_str, genes_list) print( F"""\nGeneration: {generation}\nTotal Population: {population}\nTarget: {target}""" )
370
'''simple docstring''' import os from typing import BinaryIO, Optional, Union import numpy as np import pyarrow.parquet as pq from .. import Audio, Dataset, Features, Image, NamedSplit, Value, config from ..features.features import FeatureType, _visit from ..formatting import query_table from ..packaged_modules import _PACKAGED_DATASETS_MODULES from ..packaged_modules.parquet.parquet import Parquet from ..utils import logging from ..utils.typing import NestedDataStructureLike, PathLike from .abc import AbstractDatasetReader def _UpperCAmelCase ( _UpperCamelCase : Features ) -> Optional[int]: A_ = np.inf def set_batch_size(_UpperCamelCase : FeatureType ) -> None: nonlocal batch_size if isinstance(_UpperCamelCase, _UpperCamelCase ): A_ = min(_UpperCamelCase, config.PARQUET_ROW_GROUP_SIZE_FOR_IMAGE_DATASETS ) elif isinstance(_UpperCamelCase, _UpperCamelCase ): A_ = min(_UpperCamelCase, config.PARQUET_ROW_GROUP_SIZE_FOR_AUDIO_DATASETS ) elif isinstance(_UpperCamelCase, _UpperCamelCase ) and feature.dtype == "binary": A_ = min(_UpperCamelCase, config.PARQUET_ROW_GROUP_SIZE_FOR_BINARY_DATASETS ) _visit(_UpperCamelCase, _UpperCamelCase ) return None if batch_size is np.inf else batch_size class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = False , _SCREAMING_SNAKE_CASE = False , _SCREAMING_SNAKE_CASE = None , **_SCREAMING_SNAKE_CASE , ) -> int: super().__init__( _SCREAMING_SNAKE_CASE , split=_SCREAMING_SNAKE_CASE , 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 , ) A_ = path_or_paths if isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) else {self.split: path_or_paths} A_ = _PACKAGED_DATASETS_MODULES['''parquet'''][1] A_ = Parquet( cache_dir=_SCREAMING_SNAKE_CASE , data_files=_SCREAMING_SNAKE_CASE , features=_SCREAMING_SNAKE_CASE , hash=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE , ) def __A ( self ) -> str: # Build iterable dataset if self.streaming: A_ = self.builder.as_streaming_dataset(split=self.split ) # Build regular (map-style) dataset else: A_ = None A_ = None A_ = None A_ = 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 , ) A_ = self.builder.as_dataset( split=self.split , verification_mode=_SCREAMING_SNAKE_CASE , in_memory=self.keep_in_memory ) return dataset class __UpperCAmelCase : '''simple docstring''' def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None , **_SCREAMING_SNAKE_CASE , ) -> Dict: A_ = dataset A_ = path_or_buf A_ = batch_size or get_writer_batch_size(dataset.features ) A_ = parquet_writer_kwargs def __A ( self ) -> int: A_ = self.batch_size if self.batch_size else config.DEFAULT_MAX_BATCH_SIZE if isinstance(self.path_or_buf , (str, bytes, os.PathLike) ): with open(self.path_or_buf , '''wb+''' ) as buffer: A_ = self._write(file_obj=_SCREAMING_SNAKE_CASE , batch_size=_SCREAMING_SNAKE_CASE , **self.parquet_writer_kwargs ) else: A_ = self._write(file_obj=self.path_or_buf , batch_size=_SCREAMING_SNAKE_CASE , **self.parquet_writer_kwargs ) return written def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) -> int: A_ = 0 A_ = parquet_writer_kwargs.pop('''path_or_buf''' , _SCREAMING_SNAKE_CASE ) A_ = self.dataset.features.arrow_schema A_ = pq.ParquetWriter(_SCREAMING_SNAKE_CASE , schema=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) for offset in logging.tqdm( range(0 , len(self.dataset ) , _SCREAMING_SNAKE_CASE ) , unit='''ba''' , disable=not logging.is_progress_bar_enabled() , desc='''Creating parquet from Arrow format''' , ): A_ = query_table( table=self.dataset._data , key=slice(_SCREAMING_SNAKE_CASE , offset + batch_size ) , indices=self.dataset._indices if self.dataset._indices is not None else None , ) writer.write_table(_SCREAMING_SNAKE_CASE ) written += batch.nbytes writer.close() return written
18
0
'''simple docstring''' import argparse import json from tqdm import tqdm def _UpperCAmelCase ( ) -> List[str]: A_ = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--src_path''', type=__lowerCAmelCase, default='''biencoder-nq-dev.json''', help='''Path to raw DPR training data''', ) parser.add_argument( '''--evaluation_set''', type=__lowerCAmelCase, help='''where to store parsed evaluation_set file''', ) parser.add_argument( '''--gold_data_path''', type=__lowerCAmelCase, help='''where to store parsed gold_data_path file''', ) A_ = parser.parse_args() with open(args.src_path, '''r''' ) as src_file, open(args.evaluation_set, '''w''' ) as eval_file, open( args.gold_data_path, '''w''' ) as gold_file: A_ = json.load(__lowerCAmelCase ) for dpr_record in tqdm(__lowerCAmelCase ): A_ = dpr_record['''question'''] A_ = [context['''title'''] for context in dpr_record['''positive_ctxs''']] eval_file.write(question + '''\n''' ) gold_file.write('''\t'''.join(__lowerCAmelCase ) + '''\n''' ) if __name__ == "__main__": main()
371
'''simple docstring''' from statistics import mean, stdev def _UpperCAmelCase ( _UpperCamelCase : list, _UpperCamelCase : int = 3 ) -> list: A_ = min(_UpperCamelCase ) A_ = max(_UpperCamelCase ) # normalize data return [round((x - x_min) / (x_max - x_min), _UpperCamelCase ) for x in data] def _UpperCAmelCase ( _UpperCamelCase : list, _UpperCamelCase : int = 3 ) -> list: A_ = mean(_UpperCamelCase ) A_ = stdev(_UpperCamelCase ) # standardize data return [round((x - mu) / (sigma), _UpperCamelCase ) for x in data]
18
0
'''simple docstring''' from sklearn.metrics import recall_score import datasets __snake_case : str = """ Recall is the fraction of the positive examples that were correctly labeled by the model as positive. It can be computed with the equation: Recall = TP / (TP + FN) Where TP is the true positives and FN is the false negatives. """ __snake_case : int = """ Args: - **predictions** (`list` of `int`): The predicted labels. - **references** (`list` of `int`): The ground truth labels. - **labels** (`list` of `int`): The set of labels to include when `average` is not set to `binary`, and their order when average is `None`. Labels present in the data can be excluded in this input, for example to calculate a multiclass average ignoring a majority negative class, while labels not present in the data will result in 0 components in a macro average. For multilabel targets, labels are column indices. By default, all labels in y_true and y_pred are used in sorted order. Defaults to None. - **pos_label** (`int`): The class label to use as the 'positive class' when calculating the recall. Defaults to `1`. - **average** (`string`): This parameter is required for multiclass/multilabel targets. If None, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data. Defaults to `'binary'`. - `'binary'`: Only report results for the class specified by `pos_label`. This is applicable only if the target labels and predictions are binary. - `'micro'`: Calculate metrics globally by counting the total true positives, false negatives, and false positives. - `'macro'`: Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account. - `'weighted'`: Calculate metrics for each label, and find their average weighted by support (the number of true instances for each label). This alters `'macro'` to account for label imbalance. Note that it can result in an F-score that is not between precision and recall. - `'samples'`: Calculate metrics for each instance, and find their average (only meaningful for multilabel classification). - **sample_weight** (`list` of `float`): Sample weights Defaults to `None`. - **zero_division** (): Sets the value to return when there is a zero division. Defaults to . - `'warn'`: If there is a zero division, the return value is `0`, but warnings are also raised. - `0`: If there is a zero division, the return value is `0`. - `1`: If there is a zero division, the return value is `1`. Returns: - **recall** (`float`, or `array` of `float`): Either the general recall score, or the recall scores for individual classes, depending on the values input to `labels` and `average`. Minimum possible value is 0. Maximum possible value is 1. A higher recall means that more of the positive examples have been labeled correctly. Therefore, a higher recall is generally considered better. Examples: Example 1-A simple example with some errors >>> recall_metric = datasets.load_metric('recall') >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1]) >>> print(results) {'recall': 0.6666666666666666} Example 2-The same example as Example 1, but with `pos_label=0` instead of the default `pos_label=1`. >>> recall_metric = datasets.load_metric('recall') >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1], pos_label=0) >>> print(results) {'recall': 0.5} Example 3-The same example as Example 1, but with `sample_weight` included. >>> recall_metric = datasets.load_metric('recall') >>> sample_weight = [0.9, 0.2, 0.9, 0.3, 0.8] >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1], sample_weight=sample_weight) >>> print(results) {'recall': 0.55} Example 4-A multiclass example, using different averages. >>> recall_metric = datasets.load_metric('recall') >>> predictions = [0, 2, 1, 0, 0, 1] >>> references = [0, 1, 2, 0, 1, 2] >>> results = recall_metric.compute(predictions=predictions, references=references, average='macro') >>> print(results) {'recall': 0.3333333333333333} >>> results = recall_metric.compute(predictions=predictions, references=references, average='micro') >>> print(results) {'recall': 0.3333333333333333} >>> results = recall_metric.compute(predictions=predictions, references=references, average='weighted') >>> print(results) {'recall': 0.3333333333333333} >>> results = recall_metric.compute(predictions=predictions, references=references, average=None) >>> print(results) {'recall': array([1., 0., 0.])} """ __snake_case : List[Any] = """ @article{scikit-learn, title={Scikit-learn: Machine Learning in {P}ython}, author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V. and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P. and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.}, journal={Journal of Machine Learning Research}, volume={12}, pages={2825--2830}, year={2011} """ @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __UpperCAmelCase ( datasets.Metric ): '''simple docstring''' def __A ( self ) -> Optional[Any]: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Sequence(datasets.Value('''int32''' ) ), '''references''': datasets.Sequence(datasets.Value('''int32''' ) ), } if self.config_name == '''multilabel''' else { '''predictions''': datasets.Value('''int32''' ), '''references''': datasets.Value('''int32''' ), } ) , reference_urls=['''https://scikit-learn.org/stable/modules/generated/sklearn.metrics.recall_score.html'''] , ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE=1 , _SCREAMING_SNAKE_CASE="binary" , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE="warn" , ) -> Optional[Any]: A_ = recall_score( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , labels=_SCREAMING_SNAKE_CASE , pos_label=_SCREAMING_SNAKE_CASE , average=_SCREAMING_SNAKE_CASE , sample_weight=_SCREAMING_SNAKE_CASE , zero_division=_SCREAMING_SNAKE_CASE , ) return {"recall": float(_SCREAMING_SNAKE_CASE ) if score.size == 1 else score}
350
'''simple docstring''' import logging import os import sys from dataclasses import dataclass, field from typing import Optional import evaluate import numpy as np import torch from datasets import load_dataset from PIL import Image from torchvision.transforms import ( CenterCrop, Compose, Normalize, RandomHorizontalFlip, RandomResizedCrop, Resize, ToTensor, ) import transformers from transformers import ( MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING, AutoConfig, AutoImageProcessor, AutoModelForImageClassification, HfArgumentParser, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import get_last_checkpoint from transformers.utils import check_min_version, send_example_telemetry from transformers.utils.versions import require_version __snake_case : Optional[int] = logging.getLogger(__name__) # Will error if the minimal version of Transformers is not installed. Remove at your own risks. check_min_version('4.31.0') require_version('datasets>=1.8.0', 'To fix: pip install -r examples/pytorch/image-classification/requirements.txt') __snake_case : str = list(MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING.keys()) __snake_case : int = tuple(conf.model_type for conf in MODEL_CONFIG_CLASSES) def _UpperCAmelCase ( _UpperCamelCase : str ) -> int: with open(_UpperCamelCase, '''rb''' ) as f: A_ = Image.open(_UpperCamelCase ) return im.convert('''RGB''' ) @dataclass class __UpperCAmelCase : '''simple docstring''' __lowercase : Optional[str] = field( default=_UpperCamelCase , metadata={ 'help': 'Name of a dataset from the hub (could be your own, possibly private dataset hosted on the hub).' } , ) __lowercase : Optional[str] = field( default=_UpperCamelCase , metadata={'help': 'The configuration name of the dataset to use (via the datasets library).'} ) __lowercase : Optional[str] = field(default=_UpperCamelCase , metadata={'help': 'A folder containing the training data.'} ) __lowercase : Optional[str] = field(default=_UpperCamelCase , metadata={'help': 'A folder containing the validation data.'} ) __lowercase : Optional[float] = field( default=0.15 , metadata={'help': 'Percent to split off of train for validation.'} ) __lowercase : Optional[int] = field( default=_UpperCamelCase , metadata={ 'help': ( 'For debugging purposes or quicker training, truncate the number of training examples to this ' 'value if set.' ) } , ) __lowercase : Optional[int] = field( default=_UpperCamelCase , metadata={ 'help': ( 'For debugging purposes or quicker training, truncate the number of evaluation examples to this ' 'value if set.' ) } , ) def __A ( self ) -> int: if self.dataset_name is None and (self.train_dir is None and self.validation_dir is None): raise ValueError( '''You must specify either a dataset name from the hub or a train and/or validation directory.''' ) @dataclass class __UpperCAmelCase : '''simple docstring''' __lowercase : str = field( default='google/vit-base-patch16-224-in21k' , metadata={'help': 'Path to pretrained model or model identifier from huggingface.co/models'} , ) __lowercase : Optional[str] = field( default=_UpperCamelCase , metadata={'help': 'If training from scratch, pass a model type from the list: ' + ', '.join(_UpperCamelCase )} , ) __lowercase : Optional[str] = field( default=_UpperCamelCase , metadata={'help': 'Pretrained config name or path if not the same as model_name'} ) __lowercase : Optional[str] = field( default=_UpperCamelCase , metadata={'help': 'Where do you want to store the pretrained models downloaded from s3'} ) __lowercase : str = field( default='main' , metadata={'help': 'The specific model version to use (can be a branch name, tag name or commit id).'} , ) __lowercase : str = field(default=_UpperCamelCase , metadata={'help': 'Name or path of preprocessor config.'} ) __lowercase : bool = field( default=_UpperCamelCase , metadata={ 'help': ( 'Will use the token generated when running `huggingface-cli login` (necessary to use this script ' 'with private models).' ) } , ) __lowercase : bool = field( default=_UpperCamelCase , metadata={'help': 'Will enable to load a pretrained model whose head dimensions are different.'} , ) def _UpperCAmelCase ( _UpperCamelCase : str ) -> Dict: A_ = torch.stack([example['''pixel_values'''] for example in examples] ) A_ = torch.tensor([example['''labels'''] for example in examples] ) return {"pixel_values": pixel_values, "labels": labels} def _UpperCAmelCase ( ) -> Tuple: # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. A_ = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith('''.json''' ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. A_ ,A_ ,A_ = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: A_ ,A_ ,A_ = parser.parse_args_into_dataclasses() # Sending telemetry. Tracking the example usage helps us better allocate resources to maintain them. The # information sent is the one passed as arguments along with your Python/PyTorch versions. send_example_telemetry('''run_image_classification''', _UpperCamelCase, _UpperCamelCase ) # Setup logging logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''', datefmt='''%m/%d/%Y %H:%M:%S''', handlers=[logging.StreamHandler(sys.stdout )], ) if training_args.should_log: # The default of training_args.log_level is passive, so we set log level at info here to have that default. transformers.utils.logging.set_verbosity_info() A_ = training_args.get_process_log_level() logger.setLevel(_UpperCamelCase ) transformers.utils.logging.set_verbosity(_UpperCamelCase ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Log on each process the small summary: logger.warning( F'''Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}''' + F'''distributed training: {bool(training_args.local_rank != -1 )}, 16-bits training: {training_args.fpaa}''' ) logger.info(F'''Training/evaluation parameters {training_args}''' ) # Detecting last checkpoint. A_ = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: A_ = get_last_checkpoint(training_args.output_dir ) if last_checkpoint is None and len(os.listdir(training_args.output_dir ) ) > 0: raise ValueError( F'''Output directory ({training_args.output_dir}) already exists and is not empty. ''' '''Use --overwrite_output_dir to overcome.''' ) elif last_checkpoint is not None and training_args.resume_from_checkpoint is None: logger.info( F'''Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change ''' '''the `--output_dir` or add `--overwrite_output_dir` to train from scratch.''' ) # Set seed before initializing model. set_seed(training_args.seed ) # Initialize our dataset and prepare it for the 'image-classification' task. if data_args.dataset_name is not None: A_ = load_dataset( data_args.dataset_name, data_args.dataset_config_name, cache_dir=model_args.cache_dir, task='''image-classification''', use_auth_token=True if model_args.use_auth_token else None, ) else: A_ = {} if data_args.train_dir is not None: A_ = os.path.join(data_args.train_dir, '''**''' ) if data_args.validation_dir is not None: A_ = os.path.join(data_args.validation_dir, '''**''' ) A_ = load_dataset( '''imagefolder''', data_files=_UpperCamelCase, cache_dir=model_args.cache_dir, task='''image-classification''', ) # If we don't have a validation split, split off a percentage of train as validation. A_ = None if '''validation''' in dataset.keys() else data_args.train_val_split if isinstance(data_args.train_val_split, _UpperCamelCase ) and data_args.train_val_split > 0.0: A_ = dataset['''train'''].train_test_split(data_args.train_val_split ) A_ = split['''train'''] A_ = split['''test'''] # Prepare label mappings. # We'll include these in the model's config to get human readable labels in the Inference API. A_ = dataset['''train'''].features['''labels'''].names A_ ,A_ = {}, {} for i, label in enumerate(_UpperCamelCase ): A_ = str(_UpperCamelCase ) A_ = label # Load the accuracy metric from the datasets package A_ = evaluate.load('''accuracy''' ) # Define our compute_metrics function. It takes an `EvalPrediction` object (a namedtuple with a # predictions and label_ids field) and has to return a dictionary string to float. def compute_metrics(_UpperCamelCase : Optional[Any] ): return metric.compute(predictions=np.argmax(p.predictions, axis=1 ), references=p.label_ids ) A_ = AutoConfig.from_pretrained( model_args.config_name or model_args.model_name_or_path, num_labels=len(_UpperCamelCase ), labelaid=_UpperCamelCase, idalabel=_UpperCamelCase, finetuning_task='''image-classification''', cache_dir=model_args.cache_dir, revision=model_args.model_revision, use_auth_token=True if model_args.use_auth_token else None, ) A_ = AutoModelForImageClassification.from_pretrained( model_args.model_name_or_path, from_tf=bool('''.ckpt''' in model_args.model_name_or_path ), config=_UpperCamelCase, cache_dir=model_args.cache_dir, revision=model_args.model_revision, use_auth_token=True if model_args.use_auth_token else None, ignore_mismatched_sizes=model_args.ignore_mismatched_sizes, ) A_ = AutoImageProcessor.from_pretrained( model_args.image_processor_name or model_args.model_name_or_path, cache_dir=model_args.cache_dir, revision=model_args.model_revision, use_auth_token=True if model_args.use_auth_token else None, ) # Define torchvision transforms to be applied to each image. if "shortest_edge" in image_processor.size: A_ = image_processor.size['''shortest_edge'''] else: A_ = (image_processor.size['''height'''], image_processor.size['''width''']) A_ = Normalize(mean=image_processor.image_mean, std=image_processor.image_std ) A_ = Compose( [ RandomResizedCrop(_UpperCamelCase ), RandomHorizontalFlip(), ToTensor(), normalize, ] ) A_ = Compose( [ Resize(_UpperCamelCase ), CenterCrop(_UpperCamelCase ), ToTensor(), normalize, ] ) def train_transforms(_UpperCamelCase : Dict ): A_ = [ _train_transforms(pil_img.convert('''RGB''' ) ) for pil_img in example_batch['''image'''] ] return example_batch def val_transforms(_UpperCamelCase : Any ): A_ = [_val_transforms(pil_img.convert('''RGB''' ) ) for pil_img in example_batch['''image''']] return example_batch if training_args.do_train: if "train" not in dataset: raise ValueError('''--do_train requires a train dataset''' ) if data_args.max_train_samples is not None: A_ = ( dataset['''train'''].shuffle(seed=training_args.seed ).select(range(data_args.max_train_samples ) ) ) # Set the training transforms dataset["train"].set_transform(_UpperCamelCase ) if training_args.do_eval: if "validation" not in dataset: raise ValueError('''--do_eval requires a validation dataset''' ) if data_args.max_eval_samples is not None: A_ = ( dataset['''validation'''].shuffle(seed=training_args.seed ).select(range(data_args.max_eval_samples ) ) ) # Set the validation transforms dataset["validation"].set_transform(_UpperCamelCase ) # Initalize our trainer A_ = Trainer( model=_UpperCamelCase, args=_UpperCamelCase, train_dataset=dataset['''train'''] if training_args.do_train else None, eval_dataset=dataset['''validation'''] if training_args.do_eval else None, compute_metrics=_UpperCamelCase, tokenizer=_UpperCamelCase, data_collator=_UpperCamelCase, ) # Training if training_args.do_train: A_ = None if training_args.resume_from_checkpoint is not None: A_ = training_args.resume_from_checkpoint elif last_checkpoint is not None: A_ = last_checkpoint A_ = trainer.train(resume_from_checkpoint=_UpperCamelCase ) trainer.save_model() trainer.log_metrics('''train''', train_result.metrics ) trainer.save_metrics('''train''', train_result.metrics ) trainer.save_state() # Evaluation if training_args.do_eval: A_ = trainer.evaluate() trainer.log_metrics('''eval''', _UpperCamelCase ) trainer.save_metrics('''eval''', _UpperCamelCase ) # Write model card and (optionally) push to hub A_ = { '''finetuned_from''': model_args.model_name_or_path, '''tasks''': '''image-classification''', '''dataset''': data_args.dataset_name, '''tags''': ['''image-classification''', '''vision'''], } if training_args.push_to_hub: trainer.push_to_hub(**_UpperCamelCase ) else: trainer.create_model_card(**_UpperCamelCase ) if __name__ == "__main__": main()
18
0
'''simple docstring''' import torch def _UpperCAmelCase ( ) -> Tuple: if torch.cuda.is_available(): A_ = torch.cuda.device_count() else: A_ = 0 print(F'''Successfully ran on {num_gpus} GPUs''' ) if __name__ == "__main__": main()
351
'''simple docstring''' import tempfile import unittest import numpy as np from huggingface_hub import HfFolder, delete_repo from requests.exceptions import HTTPError from transformers import BertConfig, is_flax_available from transformers.testing_utils import TOKEN, USER, is_staging_test, require_flax if is_flax_available(): import os from flax.core.frozen_dict import unfreeze from flax.traverse_util import flatten_dict from transformers import FlaxBertModel __snake_case : str = '0.12' # assumed parallelism: 8 @require_flax @is_staging_test class __UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' @classmethod def __A ( cls ) -> Dict: A_ = TOKEN HfFolder.save_token(_SCREAMING_SNAKE_CASE ) @classmethod def __A ( cls ) -> Optional[int]: try: delete_repo(token=cls._token , repo_id='''test-model-flax''' ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id='''valid_org/test-model-flax-org''' ) except HTTPError: pass def __A ( self ) -> str: A_ = BertConfig( vocab_size=99 , hidden_size=32 , num_hidden_layers=5 , num_attention_heads=4 , intermediate_size=37 ) A_ = FlaxBertModel(_SCREAMING_SNAKE_CASE ) model.push_to_hub('''test-model-flax''' , use_auth_token=self._token ) A_ = FlaxBertModel.from_pretrained(F'''{USER}/test-model-flax''' ) A_ = flatten_dict(unfreeze(model.params ) ) A_ = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): A_ = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(_SCREAMING_SNAKE_CASE , 1E-3 , msg=F'''{key} not identical''' ) # Reset repo delete_repo(token=self._token , repo_id='''test-model-flax''' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(_SCREAMING_SNAKE_CASE , repo_id='''test-model-flax''' , push_to_hub=_SCREAMING_SNAKE_CASE , use_auth_token=self._token ) A_ = FlaxBertModel.from_pretrained(F'''{USER}/test-model-flax''' ) A_ = flatten_dict(unfreeze(model.params ) ) A_ = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): A_ = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(_SCREAMING_SNAKE_CASE , 1E-3 , msg=F'''{key} not identical''' ) def __A ( self ) -> List[str]: A_ = BertConfig( vocab_size=99 , hidden_size=32 , num_hidden_layers=5 , num_attention_heads=4 , intermediate_size=37 ) A_ = FlaxBertModel(_SCREAMING_SNAKE_CASE ) model.push_to_hub('''valid_org/test-model-flax-org''' , use_auth_token=self._token ) A_ = FlaxBertModel.from_pretrained('''valid_org/test-model-flax-org''' ) A_ = flatten_dict(unfreeze(model.params ) ) A_ = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): A_ = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(_SCREAMING_SNAKE_CASE , 1E-3 , msg=F'''{key} not identical''' ) # Reset repo delete_repo(token=self._token , repo_id='''valid_org/test-model-flax-org''' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained( _SCREAMING_SNAKE_CASE , repo_id='''valid_org/test-model-flax-org''' , push_to_hub=_SCREAMING_SNAKE_CASE , use_auth_token=self._token ) A_ = FlaxBertModel.from_pretrained('''valid_org/test-model-flax-org''' ) A_ = flatten_dict(unfreeze(model.params ) ) A_ = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): A_ = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(_SCREAMING_SNAKE_CASE , 1E-3 , msg=F'''{key} not identical''' ) def _UpperCAmelCase ( _UpperCamelCase : Union[str, Any], _UpperCamelCase : Tuple ) -> Dict: A_ = True A_ = flatten_dict(modela.params ) A_ = flatten_dict(modela.params ) for key in flat_params_a.keys(): if np.sum(np.abs(flat_params_a[key] - flat_params_a[key] ) ) > 1E-4: A_ = False return models_are_equal @require_flax class __UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' def __A ( self ) -> List[str]: A_ = BertConfig.from_pretrained('''hf-internal-testing/tiny-bert-flax-only''' ) A_ = FlaxBertModel(_SCREAMING_SNAKE_CASE ) A_ = '''bert''' with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(os.path.join(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) with self.assertRaises(_SCREAMING_SNAKE_CASE ): A_ = FlaxBertModel.from_pretrained(_SCREAMING_SNAKE_CASE ) A_ = FlaxBertModel.from_pretrained(_SCREAMING_SNAKE_CASE , subfolder=_SCREAMING_SNAKE_CASE ) self.assertTrue(check_models_equal(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) def __A ( self ) -> List[Any]: A_ = BertConfig.from_pretrained('''hf-internal-testing/tiny-bert-flax-only''' ) A_ = FlaxBertModel(_SCREAMING_SNAKE_CASE ) A_ = '''bert''' with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(os.path.join(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) , max_shard_size='''10KB''' ) with self.assertRaises(_SCREAMING_SNAKE_CASE ): A_ = FlaxBertModel.from_pretrained(_SCREAMING_SNAKE_CASE ) A_ = FlaxBertModel.from_pretrained(_SCREAMING_SNAKE_CASE , subfolder=_SCREAMING_SNAKE_CASE ) self.assertTrue(check_models_equal(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) def __A ( self ) -> Dict: A_ = '''bert''' A_ = '''hf-internal-testing/tiny-random-bert-subfolder''' with self.assertRaises(_SCREAMING_SNAKE_CASE ): A_ = FlaxBertModel.from_pretrained(_SCREAMING_SNAKE_CASE ) A_ = FlaxBertModel.from_pretrained(_SCREAMING_SNAKE_CASE , subfolder=_SCREAMING_SNAKE_CASE ) self.assertIsNotNone(_SCREAMING_SNAKE_CASE ) def __A ( self ) -> Optional[Any]: A_ = '''bert''' A_ = '''hf-internal-testing/tiny-random-bert-sharded-subfolder''' with self.assertRaises(_SCREAMING_SNAKE_CASE ): A_ = FlaxBertModel.from_pretrained(_SCREAMING_SNAKE_CASE ) A_ = FlaxBertModel.from_pretrained(_SCREAMING_SNAKE_CASE , subfolder=_SCREAMING_SNAKE_CASE ) self.assertIsNotNone(_SCREAMING_SNAKE_CASE )
18
0
'''simple docstring''' def _UpperCAmelCase ( _UpperCamelCase : Union[str, Any] ) -> List[Any]: # noqa: E741 A_ = len(lowerCamelCase__ ) A_ = 0 A_ = [0] * n A_ = [False] * n A_ = [False] * n def dfs(_UpperCamelCase : List[str], _UpperCamelCase : Tuple, _UpperCamelCase : Dict, _UpperCamelCase : Tuple ): if parent == root: out_edge_count += 1 A_ = True A_ = at for to in l[at]: if to == parent: pass elif not visited[to]: A_ = dfs(lowerCamelCase__, lowerCamelCase__, lowerCamelCase__, lowerCamelCase__ ) A_ = min(low[at], low[to] ) # AP found via bridge if at < low[to]: A_ = True # AP found via cycle if at == low[to]: A_ = True else: A_ = min(low[at], lowerCamelCase__ ) return out_edge_count for i in range(lowerCamelCase__ ): if not visited[i]: A_ = 0 A_ = dfs(lowerCamelCase__, lowerCamelCase__, -1, lowerCamelCase__ ) A_ = out_edge_count > 1 for x in range(len(lowerCamelCase__ ) ): if is_art[x] is True: print(lowerCamelCase__ ) # Adjacency list of graph __snake_case : Any = { 0: [1, 2], 1: [0, 2], 2: [0, 1, 3, 5], 3: [2, 4], 4: [3], 5: [2, 6, 8], 6: [5, 7], 7: [6, 8], 8: [5, 7], } compute_ap(data)
352
'''simple docstring''' def _UpperCAmelCase ( _UpperCamelCase : Union[str, Any] ) -> Dict: A_ = 1 A_ = 2 while i * i <= n: A_ = 0 while n % i == 0: n //= i multiplicity += 1 n_divisors *= multiplicity + 1 i += 1 if n > 1: n_divisors *= 2 return n_divisors def _UpperCAmelCase ( ) -> Optional[int]: A_ = 1 A_ = 1 while True: i += 1 t_num += i if count_divisors(_UpperCamelCase ) > 5_00: break return t_num if __name__ == "__main__": print(solution())
18
0
'''simple docstring''' from diffusers.utils.testing_utils import require_onnxruntime @require_onnxruntime class __UpperCAmelCase : '''simple docstring''' pass
353
'''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 __UpperCAmelCase : '''simple docstring''' def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=2 , _SCREAMING_SNAKE_CASE=32 , _SCREAMING_SNAKE_CASE=16 , _SCREAMING_SNAKE_CASE=3 , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=32 , _SCREAMING_SNAKE_CASE=4 , _SCREAMING_SNAKE_CASE=[0, 1, 2, 3] , _SCREAMING_SNAKE_CASE=4 , _SCREAMING_SNAKE_CASE=37 , _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, 384, 24, 24] , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=None , ) -> Tuple: 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 __A ( self ) -> Optional[Any]: 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 __A ( self ) -> Optional[Any]: A_ = { '''global_padding''': '''same''', '''layer_type''': '''bottleneck''', '''depths''': [3, 4, 9], '''out_features''': ['''stage1''', '''stage2''', '''stage3'''], '''embedding_dynamic_padding''': True, '''hidden_sizes''': [96, 192, 384, 768], '''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 __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Optional[Any]: 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 __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> 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 __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Union[str, 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 __A ( self ) -> Optional[int]: A_ = self.prepare_config_and_inputs() A_ ,A_ ,A_ = config_and_inputs A_ = {'''pixel_values''': pixel_values} return config, inputs_dict @require_torch class __UpperCAmelCase ( _UpperCamelCase , _UpperCamelCase , unittest.TestCase ): '''simple docstring''' __lowercase : Optional[int] = (DPTModel, DPTForDepthEstimation, DPTForSemanticSegmentation) if is_torch_available() else () __lowercase : Optional[int] = ( { 'depth-estimation': DPTForDepthEstimation, 'feature-extraction': DPTModel, 'image-segmentation': DPTForSemanticSegmentation, } if is_torch_available() else {} ) __lowercase : Any = False __lowercase : Tuple = False __lowercase : List[Any] = False def __A ( self ) -> Tuple: A_ = DPTModelTester(self ) A_ = ConfigTester(self , config_class=_SCREAMING_SNAKE_CASE , has_text_modality=_SCREAMING_SNAKE_CASE , hidden_size=37 ) def __A ( self ) -> Dict: self.config_tester.run_common_tests() @unittest.skip(reason='''DPT does not use inputs_embeds''' ) def __A ( self ) -> Union[str, Any]: pass def __A ( self ) -> Dict: A_ ,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 __A ( self ) -> Optional[int]: A_ ,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 __A ( self ) -> str: A_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE ) def __A ( self ) -> str: A_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_depth_estimation(*_SCREAMING_SNAKE_CASE ) def __A ( self ) -> Optional[Any]: A_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_semantic_segmentation(*_SCREAMING_SNAKE_CASE ) def __A ( self ) -> Any: for model_class in self.all_model_classes: if model_class.__name__ == "DPTForDepthEstimation": continue A_ ,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 __A ( self ) -> Any: for model_class in self.all_model_classes: if model_class.__name__ == "DPTForDepthEstimation": continue A_ ,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 __A ( self ) -> Tuple: A_ ,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 __A ( self ) -> int: pass @slow def __A ( self ) -> Dict: for model_name in DPT_PRETRAINED_MODEL_ARCHIVE_LIST[1:]: A_ = DPTModel.from_pretrained(_SCREAMING_SNAKE_CASE ) self.assertIsNotNone(_SCREAMING_SNAKE_CASE ) def __A ( self ) -> Optional[int]: # We do this test only for DPTForDepthEstimation since it is the only model that uses readout_type A_ ,A_ = self.model_tester.prepare_config_and_inputs_for_common() A_ = '''add''' with self.assertRaises(_SCREAMING_SNAKE_CASE ): A_ = DPTForDepthEstimation(_SCREAMING_SNAKE_CASE ) def _UpperCAmelCase ( ) -> Optional[int]: A_ = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_torch @require_vision @slow class __UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' def __A ( self ) -> Any: 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, 384, 384) ) self.assertEqual(predicted_depth.shape , _SCREAMING_SNAKE_CASE ) A_ = torch.tensor( [[[5.6_437, 5.6_146, 5.6_511], [5.4_371, 5.5_649, 5.5_958], [5.5_215, 5.5_184, 5.5_293]]] ).to(_SCREAMING_SNAKE_CASE ) self.assertTrue(torch.allclose(outputs.predicted_depth[:3, :3, :3] / 100 , _SCREAMING_SNAKE_CASE , atol=1E-4 ) )
18
0
import os import tempfile import unittest from transformers import NezhaConfig, is_torch_available from transformers.models.auto import get_values from transformers.testing_utils import require_torch, require_torch_gpu, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( MODEL_FOR_PRETRAINING_MAPPING, NezhaForMaskedLM, NezhaForMultipleChoice, NezhaForNextSentencePrediction, NezhaForPreTraining, NezhaForQuestionAnswering, NezhaForSequenceClassification, NezhaForTokenClassification, NezhaModel, ) from transformers.models.nezha.modeling_nezha import NEZHA_PRETRAINED_MODEL_ARCHIVE_LIST class __UpperCAmelCase : '''simple docstring''' def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=13 , _SCREAMING_SNAKE_CASE=7 , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=99 , _SCREAMING_SNAKE_CASE=32 , _SCREAMING_SNAKE_CASE=5 , _SCREAMING_SNAKE_CASE=4 , _SCREAMING_SNAKE_CASE=37 , _SCREAMING_SNAKE_CASE="gelu" , _SCREAMING_SNAKE_CASE=0.1 , _SCREAMING_SNAKE_CASE=0.1 , _SCREAMING_SNAKE_CASE=128 , _SCREAMING_SNAKE_CASE=32 , _SCREAMING_SNAKE_CASE=16 , _SCREAMING_SNAKE_CASE=2 , _SCREAMING_SNAKE_CASE=0.02 , _SCREAMING_SNAKE_CASE=3 , _SCREAMING_SNAKE_CASE=4 , _SCREAMING_SNAKE_CASE=None , ) -> str: A_ = parent A_ = batch_size A_ = seq_length A_ = is_training A_ = use_input_mask A_ = use_token_type_ids A_ = use_labels A_ = vocab_size A_ = hidden_size A_ = num_hidden_layers A_ = num_attention_heads A_ = intermediate_size A_ = hidden_act A_ = hidden_dropout_prob A_ = attention_probs_dropout_prob A_ = max_position_embeddings A_ = type_vocab_size A_ = type_sequence_label_size A_ = initializer_range A_ = num_labels A_ = num_choices A_ = scope def __A ( self ) -> int: A_ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) A_ = None if self.use_input_mask: A_ = random_attention_mask([self.batch_size, self.seq_length] ) A_ = None if self.use_token_type_ids: A_ = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) A_ = None A_ = None A_ = None if self.use_labels: A_ = ids_tensor([self.batch_size] , self.type_sequence_label_size ) A_ = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) A_ = ids_tensor([self.batch_size] , self.num_choices ) A_ = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def __A ( self ) -> Dict: return NezhaConfig( 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=_SCREAMING_SNAKE_CASE , initializer_range=self.initializer_range , ) def __A ( self ) -> Tuple: ( ( A_ ) ,( A_ ) ,( A_ ) ,( A_ ) ,( A_ ) ,( A_ ) ,( A_ ) , ) = self.prepare_config_and_inputs() A_ = True A_ = floats_tensor([self.batch_size, self.seq_length, self.hidden_size] ) A_ = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2 ) return ( config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels, encoder_hidden_states, encoder_attention_mask, ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Optional[int]: A_ = NezhaModel(config=_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() A_ = model(_SCREAMING_SNAKE_CASE , attention_mask=_SCREAMING_SNAKE_CASE , token_type_ids=_SCREAMING_SNAKE_CASE ) A_ = model(_SCREAMING_SNAKE_CASE , token_type_ids=_SCREAMING_SNAKE_CASE ) A_ = model(_SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size) ) def __A ( self , _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 , ) -> Tuple: A_ = True A_ = NezhaModel(_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() A_ = model( _SCREAMING_SNAKE_CASE , attention_mask=_SCREAMING_SNAKE_CASE , token_type_ids=_SCREAMING_SNAKE_CASE , encoder_hidden_states=_SCREAMING_SNAKE_CASE , encoder_attention_mask=_SCREAMING_SNAKE_CASE , ) A_ = model( _SCREAMING_SNAKE_CASE , attention_mask=_SCREAMING_SNAKE_CASE , token_type_ids=_SCREAMING_SNAKE_CASE , encoder_hidden_states=_SCREAMING_SNAKE_CASE , ) A_ = model(_SCREAMING_SNAKE_CASE , attention_mask=_SCREAMING_SNAKE_CASE , token_type_ids=_SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size) ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> List[str]: A_ = NezhaForMaskedLM(config=_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() A_ = model(_SCREAMING_SNAKE_CASE , attention_mask=_SCREAMING_SNAKE_CASE , token_type_ids=_SCREAMING_SNAKE_CASE , labels=_SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Tuple: A_ = NezhaForNextSentencePrediction(config=_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() A_ = model( _SCREAMING_SNAKE_CASE , attention_mask=_SCREAMING_SNAKE_CASE , token_type_ids=_SCREAMING_SNAKE_CASE , labels=_SCREAMING_SNAKE_CASE , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, 2) ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> int: A_ = NezhaForPreTraining(config=_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() A_ = model( _SCREAMING_SNAKE_CASE , attention_mask=_SCREAMING_SNAKE_CASE , token_type_ids=_SCREAMING_SNAKE_CASE , labels=_SCREAMING_SNAKE_CASE , next_sentence_label=_SCREAMING_SNAKE_CASE , ) self.parent.assertEqual(result.prediction_logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) self.parent.assertEqual(result.seq_relationship_logits.shape , (self.batch_size, 2) ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Dict: A_ = NezhaForQuestionAnswering(config=_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() A_ = model( _SCREAMING_SNAKE_CASE , attention_mask=_SCREAMING_SNAKE_CASE , token_type_ids=_SCREAMING_SNAKE_CASE , start_positions=_SCREAMING_SNAKE_CASE , end_positions=_SCREAMING_SNAKE_CASE , ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> int: A_ = self.num_labels A_ = NezhaForSequenceClassification(_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() A_ = model(_SCREAMING_SNAKE_CASE , attention_mask=_SCREAMING_SNAKE_CASE , token_type_ids=_SCREAMING_SNAKE_CASE , labels=_SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Dict: A_ = self.num_labels A_ = NezhaForTokenClassification(config=_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() A_ = model(_SCREAMING_SNAKE_CASE , attention_mask=_SCREAMING_SNAKE_CASE , token_type_ids=_SCREAMING_SNAKE_CASE , labels=_SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Tuple: A_ = self.num_choices A_ = NezhaForMultipleChoice(config=_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() A_ = input_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() A_ = token_type_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() A_ = input_mask.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() A_ = model( _SCREAMING_SNAKE_CASE , attention_mask=_SCREAMING_SNAKE_CASE , token_type_ids=_SCREAMING_SNAKE_CASE , labels=_SCREAMING_SNAKE_CASE , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def __A ( self ) -> List[str]: A_ = self.prepare_config_and_inputs() ( ( A_ ) ,( A_ ) ,( A_ ) ,( A_ ) ,( A_ ) ,( A_ ) ,( A_ ) , ) = config_and_inputs A_ = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_torch class __UpperCAmelCase ( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , unittest.TestCase ): '''simple docstring''' __lowercase : Optional[int] = ( ( NezhaModel, NezhaForMaskedLM, NezhaForMultipleChoice, NezhaForNextSentencePrediction, NezhaForPreTraining, NezhaForQuestionAnswering, NezhaForSequenceClassification, NezhaForTokenClassification, ) if is_torch_available() else () ) __lowercase : int = ( { "feature-extraction": NezhaModel, "fill-mask": NezhaForMaskedLM, "question-answering": NezhaForQuestionAnswering, "text-classification": NezhaForSequenceClassification, "token-classification": NezhaForTokenClassification, "zero-shot": NezhaForSequenceClassification, } if is_torch_available() else {} ) __lowercase : Tuple = True def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=False ) -> Optional[Any]: A_ = super()._prepare_for_class(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , return_labels=_SCREAMING_SNAKE_CASE ) if return_labels: if model_class in get_values(_SCREAMING_SNAKE_CASE ): A_ = torch.zeros( (self.model_tester.batch_size, self.model_tester.seq_length) , dtype=torch.long , device=_SCREAMING_SNAKE_CASE ) A_ = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=_SCREAMING_SNAKE_CASE ) return inputs_dict def __A ( self ) -> Dict: A_ = NezhaModelTester(self ) A_ = ConfigTester(self , config_class=_SCREAMING_SNAKE_CASE , hidden_size=37 ) def __A ( self ) -> List[str]: self.config_tester.run_common_tests() def __A ( self ) -> str: A_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE ) def __A ( self ) -> Union[str, Any]: A_ = self.model_tester.prepare_config_and_inputs_for_decoder() self.model_tester.create_and_check_model_as_decoder(*_SCREAMING_SNAKE_CASE ) def __A ( self ) -> List[Any]: # This regression test was failing with PyTorch < 1.3 ( ( A_ ) ,( A_ ) ,( A_ ) ,( A_ ) ,( A_ ) ,( A_ ) ,( A_ ) ,( A_ ) ,( A_ ) , ) = self.model_tester.prepare_config_and_inputs_for_decoder() A_ = None self.model_tester.create_and_check_model_as_decoder( _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 , ) def __A ( self ) -> Dict: A_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*_SCREAMING_SNAKE_CASE ) def __A ( self ) -> List[Any]: A_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_multiple_choice(*_SCREAMING_SNAKE_CASE ) def __A ( self ) -> List[Any]: A_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_next_sequence_prediction(*_SCREAMING_SNAKE_CASE ) def __A ( self ) -> Optional[Any]: A_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_pretraining(*_SCREAMING_SNAKE_CASE ) def __A ( self ) -> Union[str, Any]: A_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*_SCREAMING_SNAKE_CASE ) def __A ( self ) -> Tuple: A_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*_SCREAMING_SNAKE_CASE ) def __A ( self ) -> Any: A_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*_SCREAMING_SNAKE_CASE ) @slow def __A ( self ) -> Union[str, Any]: for model_name in NEZHA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A_ = NezhaModel.from_pretrained(_SCREAMING_SNAKE_CASE ) self.assertIsNotNone(_SCREAMING_SNAKE_CASE ) @slow @require_torch_gpu def __A ( self ) -> Any: A_ ,A_ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: # NezhaForMultipleChoice behaves incorrectly in JIT environments. if model_class == NezhaForMultipleChoice: return A_ = True A_ = model_class(config=_SCREAMING_SNAKE_CASE ) A_ = self._prepare_for_class(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) A_ = torch.jit.trace( _SCREAMING_SNAKE_CASE , (inputs_dict['''input_ids'''].to('''cpu''' ), inputs_dict['''attention_mask'''].to('''cpu''' )) ) with tempfile.TemporaryDirectory() as tmp: torch.jit.save(_SCREAMING_SNAKE_CASE , os.path.join(_SCREAMING_SNAKE_CASE , '''bert.pt''' ) ) A_ = torch.jit.load(os.path.join(_SCREAMING_SNAKE_CASE , '''bert.pt''' ) , map_location=_SCREAMING_SNAKE_CASE ) loaded(inputs_dict['''input_ids'''].to(_SCREAMING_SNAKE_CASE ) , inputs_dict['''attention_mask'''].to(_SCREAMING_SNAKE_CASE ) ) @require_torch class __UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' @slow def __A ( self ) -> Dict: A_ = NezhaModel.from_pretrained('''sijunhe/nezha-cn-base''' ) A_ = torch.tensor([[0, 1, 2, 3, 4, 5]] ) A_ = torch.tensor([[0, 1, 1, 1, 1, 1]] ) with torch.no_grad(): A_ = model(_SCREAMING_SNAKE_CASE , attention_mask=_SCREAMING_SNAKE_CASE )[0] A_ = torch.Size((1, 6, 768) ) self.assertEqual(output.shape , _SCREAMING_SNAKE_CASE ) A_ = torch.tensor([[[0.0_685, 0.2_441, 0.1_102], [0.0_600, 0.1_906, 0.1_349], [0.0_221, 0.0_819, 0.0_586]]] ) self.assertTrue(torch.allclose(output[:, 1:4, 1:4] , _SCREAMING_SNAKE_CASE , atol=1E-4 ) ) @slow def __A ( self ) -> Optional[Any]: A_ = NezhaForMaskedLM.from_pretrained('''sijunhe/nezha-cn-base''' ) A_ = torch.tensor([[0, 1, 2, 3, 4, 5]] ) A_ = torch.tensor([[1, 1, 1, 1, 1, 1]] ) with torch.no_grad(): A_ = model(_SCREAMING_SNAKE_CASE , attention_mask=_SCREAMING_SNAKE_CASE )[0] A_ = torch.Size((1, 6, 2_1128) ) self.assertEqual(output.shape , _SCREAMING_SNAKE_CASE ) A_ = torch.tensor( [[-2.7_939, -1.7_902, -2.2_189], [-2.8_585, -1.8_908, -2.3_723], [-2.6_499, -1.7_750, -2.2_558]] ) self.assertTrue(torch.allclose(output[:, 1:4, 1:4] , _SCREAMING_SNAKE_CASE , atol=1E-4 ) )
354
'''simple docstring''' import math def _UpperCAmelCase ( _UpperCamelCase : float, _UpperCamelCase : float ) -> float: if initial_intensity < 0: raise ValueError('''The value of intensity cannot be negative''' ) # handling of negative values of initial intensity if angle < 0 or angle > 3_60: raise ValueError('''In Malus Law, the angle is in the range 0-360 degrees''' ) # handling of values out of allowed range return initial_intensity * (math.cos(math.radians(_UpperCamelCase ) ) ** 2) if __name__ == "__main__": import doctest doctest.testmod(name='malus_law')
18
0
from itertools import count def _UpperCAmelCase ( _UpperCamelCase : int = 50 ) -> List[str]: A_ = [1] * min_block_length for n in count(_UpperCamelCase ): fill_count_functions.append(1 ) for block_length in range(_UpperCamelCase, n + 1 ): for block_start in range(n - block_length ): fill_count_functions[n] += fill_count_functions[ n - block_start - block_length - 1 ] fill_count_functions[n] += 1 if fill_count_functions[n] > 1_00_00_00: break return n if __name__ == "__main__": print(F"""{solution() = }""")
355
'''simple docstring''' from ...utils import ( OptionalDependencyNotAvailable, is_torch_available, is_transformers_available, is_transformers_version, ) try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import ShapEPipeline else: from .camera import create_pan_cameras from .pipeline_shap_e import ShapEPipeline from .pipeline_shap_e_img2img import ShapEImgaImgPipeline from .renderer import ( BoundingBoxVolume, ImportanceRaySampler, MLPNeRFModelOutput, MLPNeRSTFModel, ShapEParamsProjModel, ShapERenderer, StratifiedRaySampler, VoidNeRFModel, )
18
0
'''simple docstring''' import unittest from dataclasses import dataclass import pytest from accelerate.commands.config.config_args import SageMakerConfig from accelerate.utils import ComputeEnvironment from accelerate.utils.launch import _convert_nargs_to_dict @dataclass class __UpperCAmelCase ( __lowerCAmelCase ): '''simple docstring''' __lowercase : Optional[int] = ComputeEnvironment.AMAZON_SAGEMAKER __lowercase : str = True __lowercase : Tuple = "ml.p3.2xlarge" __lowercase : int = "accelerate_sagemaker_execution_role" __lowercase : List[Any] = "hf-sm" __lowercase : str = "us-east-1" __lowercase : int = 1 __lowercase : Tuple = "accelerate-sagemaker-1" __lowercase : Optional[int] = "1.6" __lowercase : Dict = "4.4" __lowercase : Dict = "train.py" __lowercase : List[Any] = [ "--model_name_or_path", "bert", "--do_train", "False", "--epochs", "3", "--learning_rate", "5e-5", "--max_steps", "50.5", ] __lowercase : Optional[Any] = [ "--model_name_or_path", "bert", "--do_train", "--do_test", "False", "--do_predict", "--epochs", "3", "--learning_rate", "5e-5", "--max_steps", "50.5", ] class __UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' def __A ( self ) -> int: A_ = _convert_nargs_to_dict(MockLaunchConfig.success_training_script_args ) assert isinstance(converted_args['''model_name_or_path'''] , lowerCamelCase__ ) assert isinstance(converted_args['''do_train'''] , lowerCamelCase__ ) assert isinstance(converted_args['''epochs'''] , lowerCamelCase__ ) assert isinstance(converted_args['''learning_rate'''] , lowerCamelCase__ ) assert isinstance(converted_args['''max_steps'''] , lowerCamelCase__ ) with pytest.raises(lowerCamelCase__ ): _convert_nargs_to_dict(MockLaunchConfig.fail_training_script_args )
356
'''simple docstring''' import argparse from pathlib import Path import torch from transformers import OPTConfig, OPTModel from transformers.utils import logging logging.set_verbosity_info() __snake_case : Any = logging.get_logger(__name__) def _UpperCAmelCase ( _UpperCamelCase : Dict ) -> List[str]: A_ = torch.load(_UpperCamelCase, map_location='''cpu''' ) if "model" in sd.keys(): A_ = torch.load(_UpperCamelCase, map_location='''cpu''' )['''model'''] # pop unnecessary weights A_ = [ '''decoder.version''', '''decoder.output_projection.weight''', ] for key in keys_to_delete: if key in sd: sd.pop(_UpperCamelCase ) A_ = { '''decoder.project_in_dim.weight''': '''decoder.project_in.weight''', '''decoder.project_out_dim.weight''': '''decoder.project_out.weight''', '''decoder.layer_norm.weight''': '''decoder.final_layer_norm.weight''', '''decoder.layer_norm.bias''': '''decoder.final_layer_norm.bias''', } for old_key, new_key in keys_to_rename.items(): if old_key in sd: A_ = sd.pop(_UpperCamelCase ) A_ = list(sd.keys() ) for key in keys: if ".qkv_proj." in key: A_ = sd[key] # We split QKV in separate Q,K,V A_ = key.replace('''.qkv_proj.''', '''.q_proj.''' ) A_ = key.replace('''.qkv_proj.''', '''.k_proj.''' ) A_ = key.replace('''.qkv_proj.''', '''.v_proj.''' ) A_ = value.shape[0] assert depth % 3 == 0 # `SequeuceParallelTransformerBlock` has QKV weight is separated in K,V,Q despite the naming: # https://cs.github.com/facebookresearch/metaseq/blob/51871bd73cd04c038f239ea2a26db1d7f6b37927/metaseq/modules/sequence_parallel_transformer_layer.py#L97 A_ ,A_ ,A_ = torch.split(_UpperCamelCase, depth // 3, dim=0 ) A_ = q A_ = k A_ = v del sd[key] return sd @torch.no_grad() def _UpperCAmelCase ( _UpperCamelCase : Optional[int], _UpperCamelCase : Optional[Any], _UpperCamelCase : List[str]=None ) -> Dict: A_ = load_checkpoint(_UpperCamelCase ) if config is not None: A_ = OPTConfig.from_pretrained(_UpperCamelCase ) else: A_ = OPTConfig() A_ = OPTModel(_UpperCamelCase ).half().eval() model.load_state_dict(_UpperCamelCase ) # Check results Path(_UpperCamelCase ).mkdir(exist_ok=_UpperCamelCase ) model.save_pretrained(_UpperCamelCase ) if __name__ == "__main__": __snake_case : Optional[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '--fairseq_path', type=str, help=( 'path to fairseq checkpoint in correct format. You can find all checkpoints in the correct format here:' ' https://huggingface.co/models?other=opt_metasq' ), ) parser.add_argument('--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.') parser.add_argument('--hf_config', default=None, type=str, help='Define HF config.') __snake_case : Optional[Any] = parser.parse_args() convert_opt_checkpoint(args.fairseq_path, args.pytorch_dump_folder_path, config=args.hf_config)
18
0
'''simple docstring''' from __future__ import absolute_import, division, print_function, unicode_literals from torch import nn from torch.nn import CrossEntropyLoss, MSELoss from transformers import RobertaConfig from transformers.file_utils import add_start_docstrings, add_start_docstrings_to_model_forward from transformers.models.roberta.modeling_roberta import ( ROBERTA_INPUTS_DOCSTRING, ROBERTA_START_DOCSTRING, RobertaEmbeddings, ) from .modeling_highway_bert import BertPreTrainedModel, DeeBertModel, HighwayException, entropy @add_start_docstrings( 'The RoBERTa Model transformer with early exiting (DeeRoBERTa). ' , lowerCamelCase__ , ) class __UpperCAmelCase ( lowerCamelCase__ ): '''simple docstring''' __lowercase : Optional[Any] = RobertaConfig __lowercase : Optional[Any] = 'roberta' def __init__( self , _SCREAMING_SNAKE_CASE ) -> List[str]: super().__init__(lowercase__ ) A_ = RobertaEmbeddings(lowercase__ ) self.init_weights() @add_start_docstrings( 'RoBERTa Model (with early exiting - DeeRoBERTa) with a classifier on top,\n also takes care of multi-layer training. ' , lowerCamelCase__ , ) class __UpperCAmelCase ( lowerCamelCase__ ): '''simple docstring''' __lowercase : Any = RobertaConfig __lowercase : Any = 'roberta' def __init__( self , _SCREAMING_SNAKE_CASE ) -> Optional[Any]: super().__init__(lowercase__ ) A_ = config.num_labels A_ = config.num_hidden_layers A_ = DeeRobertaModel(lowercase__ ) A_ = nn.Dropout(config.hidden_dropout_prob ) A_ = nn.Linear(config.hidden_size , self.config.num_labels ) @add_start_docstrings_to_model_forward(lowercase__ ) def __A ( self , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE=-1 , _SCREAMING_SNAKE_CASE=False , ) -> Optional[Any]: A_ = self.num_layers try: A_ = self.roberta( lowercase__ , attention_mask=lowercase__ , token_type_ids=lowercase__ , position_ids=lowercase__ , head_mask=lowercase__ , inputs_embeds=lowercase__ , ) A_ = outputs[1] A_ = self.dropout(lowercase__ ) A_ = self.classifier(lowercase__ ) A_ = (logits,) + outputs[2:] # add hidden states and attention if they are here except HighwayException as e: A_ = e.message A_ = e.exit_layer A_ = outputs[0] if not self.training: A_ = entropy(lowercase__ ) A_ = [] A_ = [] if labels is not None: if self.num_labels == 1: # We are doing regression A_ = MSELoss() A_ = loss_fct(logits.view(-1 ) , labels.view(-1 ) ) else: A_ = CrossEntropyLoss() A_ = loss_fct(logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) # work with highway exits A_ = [] for highway_exit in outputs[-1]: A_ = highway_exit[0] if not self.training: highway_logits_all.append(lowercase__ ) highway_entropy.append(highway_exit[2] ) if self.num_labels == 1: # We are doing regression A_ = MSELoss() A_ = loss_fct(highway_logits.view(-1 ) , labels.view(-1 ) ) else: A_ = CrossEntropyLoss() A_ = loss_fct(highway_logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) highway_losses.append(lowercase__ ) if train_highway: A_ = (sum(highway_losses[:-1] ),) + outputs # exclude the final highway, of course else: A_ = (loss,) + outputs if not self.training: A_ = outputs + ((original_entropy, highway_entropy), exit_layer) if output_layer >= 0: A_ = ( (outputs[0],) + (highway_logits_all[output_layer],) + outputs[2:] ) # use the highway of the last layer return outputs # (loss), logits, (hidden_states), (attentions), entropy
357
'''simple docstring''' from typing import List, Optional, Tuple from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_herbert import HerbertTokenizer __snake_case : Optional[Any] = logging.get_logger(__name__) __snake_case : Tuple = {'vocab_file': 'vocab.json', 'merges_file': 'merges.txt', 'tokenizer_file': 'tokenizer.json'} __snake_case : Optional[Any] = { 'vocab_file': { 'allegro/herbert-base-cased': 'https://huggingface.co/allegro/herbert-base-cased/resolve/main/vocab.json' }, 'merges_file': { 'allegro/herbert-base-cased': 'https://huggingface.co/allegro/herbert-base-cased/resolve/main/merges.txt' }, } __snake_case : Tuple = {'allegro/herbert-base-cased': 514} __snake_case : List[str] = {} class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' __lowercase : Any = VOCAB_FILES_NAMES __lowercase : Union[str, Any] = PRETRAINED_VOCAB_FILES_MAP __lowercase : Dict = PRETRAINED_INIT_CONFIGURATION __lowercase : List[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowercase : Optional[int] = HerbertTokenizer def __init__( self , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE="<s>" , _SCREAMING_SNAKE_CASE="<unk>" , _SCREAMING_SNAKE_CASE="<pad>" , _SCREAMING_SNAKE_CASE="<mask>" , _SCREAMING_SNAKE_CASE="</s>" , **_SCREAMING_SNAKE_CASE , ) -> int: super().__init__( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , tokenizer_file=_SCREAMING_SNAKE_CASE , cls_token=_SCREAMING_SNAKE_CASE , unk_token=_SCREAMING_SNAKE_CASE , pad_token=_SCREAMING_SNAKE_CASE , mask_token=_SCREAMING_SNAKE_CASE , sep_token=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE , ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None ) -> List[int]: A_ = [self.cls_token_id] A_ = [self.sep_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 , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = False ) -> List[int]: 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] + ([0] * len(_SCREAMING_SNAKE_CASE )) + [1] def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None ) -> List[int]: 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 ) * [0] + len(token_ids_a + sep ) * [1] def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None ) -> Tuple[str]: A_ = self._tokenizer.model.save(_SCREAMING_SNAKE_CASE , name=_SCREAMING_SNAKE_CASE ) return tuple(_SCREAMING_SNAKE_CASE )
18
0
'''simple docstring''' import os import tempfile from functools import partial from unittest import TestCase from unittest.mock import patch import datasets import datasets.config from .utils import require_beam class __UpperCAmelCase ( datasets.BeamBasedBuilder ): '''simple docstring''' def __A ( self ) -> str: return datasets.DatasetInfo( features=datasets.Features({'''content''': datasets.Value('''string''' )} ) , supervised_keys=_SCREAMING_SNAKE_CASE , ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> List[str]: return [datasets.SplitGenerator(name=datasets.Split.TRAIN , gen_kwargs={'''examples''': get_test_dummy_examples()} )] def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Optional[int]: import apache_beam as beam return pipeline | "Load Examples" >> beam.Create(_SCREAMING_SNAKE_CASE ) class __UpperCAmelCase ( datasets.BeamBasedBuilder ): '''simple docstring''' def __A ( self ) -> Optional[Any]: return datasets.DatasetInfo( features=datasets.Features({'''a''': datasets.Sequence({'''b''': datasets.Value('''string''' )} )} ) , supervised_keys=_SCREAMING_SNAKE_CASE , ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Optional[Any]: return [ datasets.SplitGenerator(name=datasets.Split.TRAIN , gen_kwargs={'''examples''': get_test_nested_examples()} ) ] def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> int: import apache_beam as beam return pipeline | "Load Examples" >> beam.Create(_SCREAMING_SNAKE_CASE ) def _UpperCAmelCase ( ) -> List[Any]: return [(i, {"content": content}) for i, content in enumerate(['''foo''', '''bar''', '''foobar'''] )] def _UpperCAmelCase ( ) -> Optional[int]: return [(i, {"a": {"b": [content]}}) for i, content in enumerate(['''foo''', '''bar''', '''foobar'''] )] class __UpperCAmelCase ( SCREAMING_SNAKE_CASE__ ): '''simple docstring''' @require_beam def __A ( self ) -> int: A_ = len(get_test_dummy_examples() ) with tempfile.TemporaryDirectory() as tmp_cache_dir: A_ = DummyBeamDataset(cache_dir=_SCREAMING_SNAKE_CASE , beam_runner='''DirectRunner''' ) builder.download_and_prepare() self.assertTrue( os.path.exists( os.path.join(_SCREAMING_SNAKE_CASE , builder.name , '''default''' , '''0.0.0''' , F'''{builder.name}-train.arrow''' ) ) ) self.assertDictEqual(builder.info.features , datasets.Features({'''content''': datasets.Value('''string''' )} ) ) A_ = builder.as_dataset() self.assertEqual(dset['''train'''].num_rows , _SCREAMING_SNAKE_CASE ) self.assertEqual(dset['''train'''].info.splits['''train'''].num_examples , _SCREAMING_SNAKE_CASE ) self.assertDictEqual(dset['''train'''][0] , get_test_dummy_examples()[0][1] ) self.assertDictEqual( dset['''train'''][expected_num_examples - 1] , get_test_dummy_examples()[expected_num_examples - 1][1] ) self.assertTrue( os.path.exists(os.path.join(_SCREAMING_SNAKE_CASE , builder.name , '''default''' , '''0.0.0''' , '''dataset_info.json''' ) ) ) del dset @require_beam def __A ( self ) -> List[Any]: import apache_beam as beam A_ = beam.io.parquetio.WriteToParquet A_ = len(get_test_dummy_examples() ) with tempfile.TemporaryDirectory() as tmp_cache_dir: A_ = DummyBeamDataset(cache_dir=_SCREAMING_SNAKE_CASE , beam_runner='''DirectRunner''' ) with patch('''apache_beam.io.parquetio.WriteToParquet''' ) as write_parquet_mock: A_ = partial(_SCREAMING_SNAKE_CASE , num_shards=2 ) builder.download_and_prepare() self.assertTrue( os.path.exists( os.path.join( _SCREAMING_SNAKE_CASE , builder.name , '''default''' , '''0.0.0''' , F'''{builder.name}-train-00000-of-00002.arrow''' ) ) ) self.assertTrue( os.path.exists( os.path.join( _SCREAMING_SNAKE_CASE , builder.name , '''default''' , '''0.0.0''' , F'''{builder.name}-train-00000-of-00002.arrow''' ) ) ) self.assertDictEqual(builder.info.features , datasets.Features({'''content''': datasets.Value('''string''' )} ) ) A_ = builder.as_dataset() self.assertEqual(dset['''train'''].num_rows , _SCREAMING_SNAKE_CASE ) self.assertEqual(dset['''train'''].info.splits['''train'''].num_examples , _SCREAMING_SNAKE_CASE ) # Order is not preserved when sharding, so we just check that all the elements are there self.assertListEqual(sorted(dset['''train''']['''content'''] ) , sorted(['''foo''', '''bar''', '''foobar'''] ) ) self.assertTrue( os.path.exists(os.path.join(_SCREAMING_SNAKE_CASE , builder.name , '''default''' , '''0.0.0''' , '''dataset_info.json''' ) ) ) del dset @require_beam def __A ( self ) -> int: with tempfile.TemporaryDirectory() as tmp_cache_dir: A_ = DummyBeamDataset(cache_dir=_SCREAMING_SNAKE_CASE ) self.assertRaises(datasets.builder.MissingBeamOptions , builder.download_and_prepare ) @require_beam def __A ( self ) -> Dict: A_ = len(get_test_nested_examples() ) with tempfile.TemporaryDirectory() as tmp_cache_dir: A_ = NestedBeamDataset(cache_dir=_SCREAMING_SNAKE_CASE , beam_runner='''DirectRunner''' ) builder.download_and_prepare() self.assertTrue( os.path.exists( os.path.join(_SCREAMING_SNAKE_CASE , builder.name , '''default''' , '''0.0.0''' , F'''{builder.name}-train.arrow''' ) ) ) self.assertDictEqual( builder.info.features , datasets.Features({'''a''': datasets.Sequence({'''b''': datasets.Value('''string''' )} )} ) ) A_ = builder.as_dataset() self.assertEqual(dset['''train'''].num_rows , _SCREAMING_SNAKE_CASE ) self.assertEqual(dset['''train'''].info.splits['''train'''].num_examples , _SCREAMING_SNAKE_CASE ) self.assertDictEqual(dset['''train'''][0] , get_test_nested_examples()[0][1] ) self.assertDictEqual( dset['''train'''][expected_num_examples - 1] , get_test_nested_examples()[expected_num_examples - 1][1] ) self.assertTrue( os.path.exists(os.path.join(_SCREAMING_SNAKE_CASE , builder.name , '''default''' , '''0.0.0''' , '''dataset_info.json''' ) ) ) del dset
358
'''simple docstring''' # Copyright 2022 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 import os import platform import numpy as np import psutil import torch from accelerate import __version__ as version from accelerate.commands.config import default_config_file, load_config_from_file from ..utils import is_npu_available, is_xpu_available def _UpperCAmelCase ( _UpperCamelCase : Union[str, Any]=None ) -> List[Any]: if subparsers is not None: A_ = subparsers.add_parser('''env''' ) else: A_ = argparse.ArgumentParser('''Accelerate env command''' ) parser.add_argument( '''--config_file''', default=_UpperCamelCase, help='''The config file to use for the default values in the launching script.''' ) if subparsers is not None: parser.set_defaults(func=_UpperCamelCase ) return parser def _UpperCAmelCase ( _UpperCamelCase : Dict ) -> Dict: A_ = torch.__version__ A_ = torch.cuda.is_available() A_ = is_xpu_available() A_ = is_npu_available() A_ = '''Not found''' # Get the default from the config file. if args.config_file is not None or os.path.isfile(_UpperCamelCase ): A_ = load_config_from_file(args.config_file ).to_dict() A_ = { '''`Accelerate` version''': version, '''Platform''': platform.platform(), '''Python version''': platform.python_version(), '''Numpy version''': np.__version__, '''PyTorch version (GPU?)''': F'''{pt_version} ({pt_cuda_available})''', '''PyTorch XPU available''': str(_UpperCamelCase ), '''PyTorch NPU available''': str(_UpperCamelCase ), '''System RAM''': F'''{psutil.virtual_memory().total / 10_24 ** 3:.2f} GB''', } if pt_cuda_available: A_ = torch.cuda.get_device_name() print('''\nCopy-and-paste the text below in your GitHub issue\n''' ) print('''\n'''.join([F'''- {prop}: {val}''' for prop, val in info.items()] ) ) print('''- `Accelerate` default config:''' if args.config_file is None else '''- `Accelerate` config passed:''' ) A_ = ( '''\n'''.join([F'''\t- {prop}: {val}''' for prop, val in accelerate_config.items()] ) if isinstance(_UpperCamelCase, _UpperCamelCase ) else F'''\t{accelerate_config}''' ) print(_UpperCamelCase ) A_ = accelerate_config return info def _UpperCAmelCase ( ) -> int: A_ = env_command_parser() A_ = parser.parse_args() env_command(_UpperCamelCase ) return 0 if __name__ == "__main__": raise SystemExit(main())
18
0
'''simple docstring''' 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 __UpperCAmelCase ( _lowercase , unittest.TestCase ): '''simple docstring''' __lowercase : Optional[Any] = LDMTextToImagePipeline __lowercase : Optional[int] = TEXT_TO_IMAGE_PARAMS - { 'negative_prompt', 'negative_prompt_embeds', 'cross_attention_kwargs', 'prompt_embeds', } __lowercase : Any = PipelineTesterMixin.required_optional_params - { 'num_images_per_prompt', 'callback', 'callback_steps', } __lowercase : Union[str, Any] = TEXT_TO_IMAGE_BATCH_PARAMS __lowercase : Union[str, Any] = False def __A ( self ) -> List[str]: torch.manual_seed(0 ) A_ = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''') , up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''') , cross_attention_dim=32 , ) A_ = DDIMScheduler( beta_start=0.00_085 , beta_end=0.012 , beta_schedule='''scaled_linear''' , clip_sample=__UpperCamelCase , set_alpha_to_one=__UpperCamelCase , ) torch.manual_seed(0 ) A_ = AutoencoderKL( block_out_channels=(32, 64) , in_channels=3 , out_channels=3 , down_block_types=('''DownEncoderBlock2D''', '''DownEncoderBlock2D''') , up_block_types=('''UpDecoderBlock2D''', '''UpDecoderBlock2D''') , latent_channels=4 , ) torch.manual_seed(0 ) A_ = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , ) A_ = CLIPTextModel(__UpperCamelCase ) A_ = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) A_ = { '''unet''': unet, '''scheduler''': scheduler, '''vqvae''': vae, '''bert''': text_encoder, '''tokenizer''': tokenizer, } return components def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=0 ) -> int: if str(__UpperCamelCase ).startswith('''mps''' ): A_ = torch.manual_seed(__UpperCamelCase ) else: A_ = torch.Generator(device=__UpperCamelCase ).manual_seed(__UpperCamelCase ) A_ = { '''prompt''': '''A painting of a squirrel eating a burger''', '''generator''': generator, '''num_inference_steps''': 2, '''guidance_scale''': 6.0, '''output_type''': '''numpy''', } return inputs def __A ( self ) -> Optional[int]: A_ = '''cpu''' # ensure determinism for the device-dependent torch.Generator A_ = self.get_dummy_components() A_ = LDMTextToImagePipeline(**__UpperCamelCase ) pipe.to(__UpperCamelCase ) pipe.set_progress_bar_config(disable=__UpperCamelCase ) A_ = self.get_dummy_inputs(__UpperCamelCase ) A_ = pipe(**__UpperCamelCase ).images A_ = image[0, -3:, -3:, -1] assert image.shape == (1, 16, 16, 3) A_ = np.array([0.6_101, 0.6_156, 0.5_622, 0.4_895, 0.6_661, 0.3_804, 0.5_748, 0.6_136, 0.5_014] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 @slow @require_torch_gpu class __UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' def __A ( self ) -> Union[str, Any]: super().tearDown() gc.collect() torch.cuda.empty_cache() def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=torch.floataa , _SCREAMING_SNAKE_CASE=0 ) -> Optional[int]: A_ = torch.manual_seed(__UpperCamelCase ) A_ = np.random.RandomState(__UpperCamelCase ).standard_normal((1, 4, 32, 32) ) A_ = torch.from_numpy(__UpperCamelCase ).to(device=__UpperCamelCase , dtype=__UpperCamelCase ) A_ = { '''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 __A ( self ) -> Optional[int]: A_ = LDMTextToImagePipeline.from_pretrained('''CompVis/ldm-text2im-large-256''' ).to(__UpperCamelCase ) pipe.set_progress_bar_config(disable=__UpperCamelCase ) A_ = self.get_inputs(__UpperCamelCase ) A_ = pipe(**__UpperCamelCase ).images A_ = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 256, 256, 3) A_ = np.array([0.51_825, 0.52_850, 0.52_543, 0.54_258, 0.52_304, 0.52_569, 0.54_363, 0.55_276, 0.56_878] ) A_ = np.abs(expected_slice - image_slice ).max() assert max_diff < 1E-3 @nightly @require_torch_gpu class __UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' def __A ( self ) -> Optional[Any]: super().tearDown() gc.collect() torch.cuda.empty_cache() def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=torch.floataa , _SCREAMING_SNAKE_CASE=0 ) -> int: A_ = torch.manual_seed(__UpperCamelCase ) A_ = np.random.RandomState(__UpperCamelCase ).standard_normal((1, 4, 32, 32) ) A_ = torch.from_numpy(__UpperCamelCase ).to(device=__UpperCamelCase , dtype=__UpperCamelCase ) A_ = { '''prompt''': '''A painting of a squirrel eating a burger''', '''latents''': latents, '''generator''': generator, '''num_inference_steps''': 50, '''guidance_scale''': 6.0, '''output_type''': '''numpy''', } return inputs def __A ( self ) -> Optional[int]: A_ = LDMTextToImagePipeline.from_pretrained('''CompVis/ldm-text2im-large-256''' ).to(__UpperCamelCase ) pipe.set_progress_bar_config(disable=__UpperCamelCase ) A_ = self.get_inputs(__UpperCamelCase ) A_ = pipe(**__UpperCamelCase ).images[0] A_ = load_numpy( '''https://huggingface.co/datasets/diffusers/test-arrays/resolve/main/ldm_text2img/ldm_large_256_ddim.npy''' ) A_ = np.abs(expected_image - image ).max() assert max_diff < 1E-3
359
'''simple docstring''' import inspect import math import tempfile import unittest import numpy as np from transformers import ViTMAEConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTMAEForPreTraining, ViTMAEModel from transformers.models.vit.modeling_vit import VIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class __UpperCAmelCase : '''simple docstring''' def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=13 , _SCREAMING_SNAKE_CASE=30 , _SCREAMING_SNAKE_CASE=2 , _SCREAMING_SNAKE_CASE=3 , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=32 , _SCREAMING_SNAKE_CASE=5 , _SCREAMING_SNAKE_CASE=4 , _SCREAMING_SNAKE_CASE=37 , _SCREAMING_SNAKE_CASE="gelu" , _SCREAMING_SNAKE_CASE=0.1 , _SCREAMING_SNAKE_CASE=0.1 , _SCREAMING_SNAKE_CASE=10 , _SCREAMING_SNAKE_CASE=0.02 , _SCREAMING_SNAKE_CASE=3 , _SCREAMING_SNAKE_CASE=0.6 , _SCREAMING_SNAKE_CASE=None , ) -> Tuple: 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_ = num_attention_heads A_ = intermediate_size A_ = hidden_act A_ = hidden_dropout_prob A_ = attention_probs_dropout_prob A_ = type_sequence_label_size A_ = initializer_range A_ = mask_ratio A_ = scope # in ViTMAE, the expected sequence length = (num_patches + 1) * (1 - config.mask_ratio), rounded above # (we add 1 for the [CLS] token) A_ = (image_size // patch_size) ** 2 A_ = int(math.ceil((1 - mask_ratio) * (num_patches + 1) ) ) def __A ( self ) -> Union[str, Any]: 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.type_sequence_label_size ) A_ = self.get_config() return config, pixel_values, labels def __A ( self ) -> Dict: return ViTMAEConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=_SCREAMING_SNAKE_CASE , initializer_range=self.initializer_range , mask_ratio=self.mask_ratio , ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Tuple: A_ = ViTMAEModel(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 __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Optional[Any]: A_ = ViTMAEForPreTraining(_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() A_ = model(_SCREAMING_SNAKE_CASE ) A_ = (self.image_size // self.patch_size) ** 2 A_ = self.patch_size**2 * self.num_channels self.parent.assertEqual(result.logits.shape , (self.batch_size, num_patches, expected_num_channels) ) # test greyscale images A_ = 1 A_ = ViTMAEForPreTraining(_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() A_ = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) A_ = model(_SCREAMING_SNAKE_CASE ) A_ = self.patch_size**2 self.parent.assertEqual(result.logits.shape , (self.batch_size, num_patches, expected_num_channels) ) def __A ( self ) -> int: A_ = self.prepare_config_and_inputs() A_ ,A_ ,A_ = config_and_inputs A_ = {'''pixel_values''': pixel_values} return config, inputs_dict @require_torch class __UpperCAmelCase ( _UpperCamelCase , _UpperCamelCase , unittest.TestCase ): '''simple docstring''' __lowercase : int = (ViTMAEModel, ViTMAEForPreTraining) if is_torch_available() else () __lowercase : List[Any] = {'feature-extraction': ViTMAEModel} if is_torch_available() else {} __lowercase : Union[str, Any] = False __lowercase : List[Any] = False __lowercase : List[str] = False __lowercase : List[str] = False def __A ( self ) -> Any: A_ = ViTMAEModelTester(self ) A_ = ConfigTester(self , config_class=_SCREAMING_SNAKE_CASE , has_text_modality=_SCREAMING_SNAKE_CASE , hidden_size=37 ) def __A ( self ) -> Optional[int]: self.config_tester.run_common_tests() @unittest.skip(reason='''ViTMAE does not use inputs_embeds''' ) def __A ( self ) -> int: pass def __A ( self ) -> int: A_ ,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 __A ( self ) -> int: A_ ,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 __A ( self ) -> Union[str, Any]: A_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE ) def __A ( self ) -> Optional[int]: A_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_pretraining(*_SCREAMING_SNAKE_CASE ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> int: # make masks reproducible np.random.seed(2 ) A_ = int((pt_model.config.image_size // pt_model.config.patch_size) ** 2 ) A_ = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) A_ = torch.from_numpy(_SCREAMING_SNAKE_CASE ) # Add `noise` argument. # PT inputs will be prepared in `super().check_pt_tf_models()` with this added `noise` argument A_ = pt_noise super().check_pt_tf_models(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) def __A ( self ) -> str: A_ ,A_ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A_ = model_class(_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() # make random mask reproducible torch.manual_seed(2 ) with torch.no_grad(): A_ = model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) A_ = outputs[0].cpu().numpy() A_ = 0 with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(_SCREAMING_SNAKE_CASE ) A_ = model_class.from_pretrained(_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) # make random mask reproducible torch.manual_seed(2 ) with torch.no_grad(): A_ = model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) # Make sure we don't have nans A_ = after_outputs[0].cpu().numpy() A_ = 0 A_ = np.amax(np.abs(out_a - out_a ) ) self.assertLessEqual(_SCREAMING_SNAKE_CASE , 1E-5 ) @unittest.skip( reason='''ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load to get deterministic results.''' ) def __A ( self ) -> List[str]: pass @unittest.skip( reason='''ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load to get deterministic results.''' ) def __A ( self ) -> Dict: pass @unittest.skip( reason='''ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load to get deterministic results.''' ) def __A ( self ) -> Tuple: pass @unittest.skip(reason='''ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load''' ) def __A ( self ) -> str: pass @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.''' ) def __A ( self ) -> Union[str, Any]: pass @slow def __A ( self ) -> Dict: for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A_ = ViTMAEModel.from_pretrained(_SCREAMING_SNAKE_CASE ) self.assertIsNotNone(_SCREAMING_SNAKE_CASE ) def _UpperCAmelCase ( ) -> Dict: A_ = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_torch @require_vision class __UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' @cached_property def __A ( self ) -> List[str]: return ViTImageProcessor.from_pretrained('''facebook/vit-mae-base''' ) if is_vision_available() else None @slow def __A ( self ) -> List[str]: # make random mask reproducible across the PT and TF model np.random.seed(2 ) A_ = ViTMAEForPreTraining.from_pretrained('''facebook/vit-mae-base''' ).to(_SCREAMING_SNAKE_CASE ) A_ = self.default_image_processor A_ = prepare_img() A_ = image_processor(images=_SCREAMING_SNAKE_CASE , return_tensors='''pt''' ).to(_SCREAMING_SNAKE_CASE ) # prepare a noise vector that will be also used for testing the TF model # (this way we can ensure that the PT and TF models operate on the same inputs) A_ = ViTMAEConfig() A_ = int((vit_mae_config.image_size // vit_mae_config.patch_size) ** 2 ) A_ = np.random.uniform(size=(1, num_patches) ) # forward pass with torch.no_grad(): A_ = model(**_SCREAMING_SNAKE_CASE , noise=torch.from_numpy(_SCREAMING_SNAKE_CASE ).to(device=_SCREAMING_SNAKE_CASE ) ) # verify the logits A_ = torch.Size((1, 196, 768) ) self.assertEqual(outputs.logits.shape , _SCREAMING_SNAKE_CASE ) A_ = torch.tensor( [[-0.0_548, -1.7_023, -0.9_325], [0.3_721, -0.5_670, -0.2_233], [0.8_235, -1.3_878, -0.3_524]] ) self.assertTrue(torch.allclose(outputs.logits[0, :3, :3] , expected_slice.to(_SCREAMING_SNAKE_CASE ) , atol=1E-4 ) )
18
0
'''simple docstring''' def _UpperCAmelCase ( _UpperCamelCase : int ) -> List[str]: if not head: return True # split the list to two parts A_ ,A_ = head.next, head while fast and fast.next: A_ = fast.next.next A_ = slow.next A_ = slow.next A_ = None # Don't forget here! But forget still works! # reverse the second part A_ = None while second: A_ = second.next A_ = node A_ = second A_ = nxt # compare two parts # second part has the same or one less node while node: if node.val != head.val: return False A_ = node.next A_ = head.next return True def _UpperCAmelCase ( _UpperCamelCase : Dict ) -> Optional[int]: if not head or not head.next: return True # 1. Get the midpoint (slow) A_ = A_ = A_ = head while fast and fast.next: A_ ,A_ = fast.next.next, slow.next # 2. Push the second half into the stack A_ = [slow.val] while slow.next: A_ = slow.next stack.append(slow.val ) # 3. Comparison while stack: if stack.pop() != cur.val: return False A_ = cur.next return True def _UpperCAmelCase ( _UpperCamelCase : List[str] ) -> List[Any]: if not head or not head.next: return True A_ = {} A_ = 0 while head: if head.val in d: d[head.val].append(_UpperCamelCase ) else: A_ = [pos] A_ = head.next pos += 1 A_ = pos - 1 A_ = 0 for v in d.values(): if len(_UpperCamelCase ) % 2 != 0: middle += 1 else: A_ = 0 for i in range(0, len(_UpperCamelCase ) ): if v[i] + v[len(_UpperCamelCase ) - 1 - step] != checksum: return False step += 1 if middle > 1: return False return True
360
'''simple docstring''' from typing import Callable, Optional, Union from ...configuration_utils import PretrainedConfig from ...utils import logging __snake_case : int = logging.get_logger(__name__) __snake_case : str = { 'microsoft/xprophetnet-large-wiki100-cased': ( 'https://huggingface.co/microsoft/xprophetnet-large-wiki100-cased/resolve/main/config.json' ), } class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' __lowercase : Optional[Any] = 'xlm-prophetnet' __lowercase : Optional[int] = ['past_key_values'] __lowercase : int = { 'num_attention_heads': 'num_encoder_attention_heads', } def __init__( self , _SCREAMING_SNAKE_CASE = 0.1 , _SCREAMING_SNAKE_CASE = "gelu" , _SCREAMING_SNAKE_CASE = 3_0522 , _SCREAMING_SNAKE_CASE = 1024 , _SCREAMING_SNAKE_CASE = 4096 , _SCREAMING_SNAKE_CASE = 12 , _SCREAMING_SNAKE_CASE = 16 , _SCREAMING_SNAKE_CASE = 4096 , _SCREAMING_SNAKE_CASE = 12 , _SCREAMING_SNAKE_CASE = 16 , _SCREAMING_SNAKE_CASE = 0.1 , _SCREAMING_SNAKE_CASE = 0.1 , _SCREAMING_SNAKE_CASE = 512 , _SCREAMING_SNAKE_CASE = 0.02 , _SCREAMING_SNAKE_CASE = True , _SCREAMING_SNAKE_CASE = True , _SCREAMING_SNAKE_CASE = 0 , _SCREAMING_SNAKE_CASE = 2 , _SCREAMING_SNAKE_CASE = 32 , _SCREAMING_SNAKE_CASE = 128 , _SCREAMING_SNAKE_CASE = False , _SCREAMING_SNAKE_CASE = 0.0 , _SCREAMING_SNAKE_CASE = True , _SCREAMING_SNAKE_CASE = 0 , _SCREAMING_SNAKE_CASE = 1 , _SCREAMING_SNAKE_CASE = 2 , **_SCREAMING_SNAKE_CASE , ) -> int: A_ = vocab_size A_ = hidden_size A_ = encoder_ffn_dim A_ = num_encoder_layers A_ = num_encoder_attention_heads A_ = decoder_ffn_dim A_ = num_decoder_layers A_ = num_decoder_attention_heads A_ = max_position_embeddings A_ = init_std # Normal(0, this parameter) A_ = activation_function # parameters for xlmprophetnet A_ = ngram A_ = num_buckets A_ = relative_max_distance A_ = disable_ngram_loss A_ = eps # 3 Types of Dropout A_ = attention_dropout A_ = activation_dropout A_ = dropout A_ = use_cache super().__init__( pad_token_id=_SCREAMING_SNAKE_CASE , bos_token_id=_SCREAMING_SNAKE_CASE , eos_token_id=_SCREAMING_SNAKE_CASE , is_encoder_decoder=_SCREAMING_SNAKE_CASE , add_cross_attention=_SCREAMING_SNAKE_CASE , decoder_start_token_id=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE , ) @property def __A ( self ) -> int: return self.num_encoder_layers + self.num_decoder_layers @num_hidden_layers.setter def __A ( self , _SCREAMING_SNAKE_CASE ) -> List[Any]: raise NotImplementedError( '''This model does not support the setting of `num_hidden_layers`. Please set `num_encoder_layers` and''' ''' `num_decoder_layers`.''' )
18
0
'''simple docstring''' import tempfile import torch from diffusers import PNDMScheduler from .test_schedulers import SchedulerCommonTest class __UpperCAmelCase ( UpperCamelCase_ ): '''simple docstring''' __lowercase : str = (PNDMScheduler,) __lowercase : int = (("""num_inference_steps""", 50),) def __A ( self , **_SCREAMING_SNAKE_CASE ) -> List[str]: A_ = { """num_train_timesteps""": 1000, """beta_start""": 0.0_001, """beta_end""": 0.02, """beta_schedule""": """linear""", } config.update(**_a ) return config def __A ( self , _SCREAMING_SNAKE_CASE=0 , **_SCREAMING_SNAKE_CASE ) -> List[str]: A_ = dict(self.forward_default_kwargs ) A_ = kwargs.pop('''num_inference_steps''' , _a ) A_ = self.dummy_sample A_ = 0.1 * sample A_ = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] for scheduler_class in self.scheduler_classes: A_ = self.get_scheduler_config(**_a ) A_ = scheduler_class(**_a ) scheduler.set_timesteps(_a ) # copy over dummy past residuals A_ = dummy_past_residuals[:] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(_a ) A_ = scheduler_class.from_pretrained(_a ) new_scheduler.set_timesteps(_a ) # copy over dummy past residuals A_ = dummy_past_residuals[:] A_ = scheduler.step_prk(_a , _a , _a , **_a ).prev_sample A_ = new_scheduler.step_prk(_a , _a , _a , **_a ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" A_ = scheduler.step_plms(_a , _a , _a , **_a ).prev_sample A_ = new_scheduler.step_plms(_a , _a , _a , **_a ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" def __A ( self ) -> Tuple: pass def __A ( self , _SCREAMING_SNAKE_CASE=0 , **_SCREAMING_SNAKE_CASE ) -> List[Any]: A_ = dict(self.forward_default_kwargs ) A_ = kwargs.pop('''num_inference_steps''' , _a ) A_ = self.dummy_sample A_ = 0.1 * sample A_ = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] for scheduler_class in self.scheduler_classes: A_ = self.get_scheduler_config() A_ = scheduler_class(**_a ) scheduler.set_timesteps(_a ) # copy over dummy past residuals (must be after setting timesteps) A_ = dummy_past_residuals[:] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(_a ) A_ = scheduler_class.from_pretrained(_a ) # copy over dummy past residuals new_scheduler.set_timesteps(_a ) # copy over dummy past residual (must be after setting timesteps) A_ = dummy_past_residuals[:] A_ = scheduler.step_prk(_a , _a , _a , **_a ).prev_sample A_ = new_scheduler.step_prk(_a , _a , _a , **_a ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" A_ = scheduler.step_plms(_a , _a , _a , **_a ).prev_sample A_ = new_scheduler.step_plms(_a , _a , _a , **_a ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" def __A ( self , **_SCREAMING_SNAKE_CASE ) -> Dict: A_ = self.scheduler_classes[0] A_ = self.get_scheduler_config(**_a ) A_ = scheduler_class(**_a ) A_ = 10 A_ = self.dummy_model() A_ = self.dummy_sample_deter scheduler.set_timesteps(_a ) for i, t in enumerate(scheduler.prk_timesteps ): A_ = model(_a , _a ) A_ = scheduler.step_prk(_a , _a , _a ).prev_sample for i, t in enumerate(scheduler.plms_timesteps ): A_ = model(_a , _a ) A_ = scheduler.step_plms(_a , _a , _a ).prev_sample return sample def __A ( self ) -> str: A_ = dict(self.forward_default_kwargs ) A_ = kwargs.pop('''num_inference_steps''' , _a ) for scheduler_class in self.scheduler_classes: A_ = self.get_scheduler_config() A_ = scheduler_class(**_a ) A_ = self.dummy_sample A_ = 0.1 * sample if num_inference_steps is not None and hasattr(_a , '''set_timesteps''' ): scheduler.set_timesteps(_a ) elif num_inference_steps is not None and not hasattr(_a , '''set_timesteps''' ): A_ = num_inference_steps # copy over dummy past residuals (must be done after set_timesteps) A_ = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] A_ = dummy_past_residuals[:] A_ = scheduler.step_prk(_a , 0 , _a , **_a ).prev_sample A_ = scheduler.step_prk(_a , 1 , _a , **_a ).prev_sample self.assertEqual(output_a.shape , sample.shape ) self.assertEqual(output_a.shape , output_a.shape ) A_ = scheduler.step_plms(_a , 0 , _a , **_a ).prev_sample A_ = scheduler.step_plms(_a , 1 , _a , **_a ).prev_sample self.assertEqual(output_a.shape , sample.shape ) self.assertEqual(output_a.shape , output_a.shape ) def __A ( self ) -> List[Any]: for timesteps in [100, 1000]: self.check_over_configs(num_train_timesteps=_a ) def __A ( self ) -> Optional[Any]: for steps_offset in [0, 1]: self.check_over_configs(steps_offset=_a ) A_ = self.scheduler_classes[0] A_ = self.get_scheduler_config(steps_offset=1 ) A_ = scheduler_class(**_a ) scheduler.set_timesteps(10 ) assert torch.equal( scheduler.timesteps , torch.LongTensor( [901, 851, 851, 801, 801, 751, 751, 701, 701, 651, 651, 601, 601, 501, 401, 301, 201, 101, 1] ) , ) def __A ( self ) -> Optional[int]: for beta_start, beta_end in zip([0.0_001, 0.001] , [0.002, 0.02] ): self.check_over_configs(beta_start=_a , beta_end=_a ) def __A ( self ) -> List[Any]: for schedule in ["linear", "squaredcos_cap_v2"]: self.check_over_configs(beta_schedule=_a ) def __A ( self ) -> Optional[int]: for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=_a ) def __A ( self ) -> Optional[Any]: for t in [1, 5, 10]: self.check_over_forward(time_step=_a ) def __A ( self ) -> Dict: for t, num_inference_steps in zip([1, 5, 10] , [10, 50, 100] ): self.check_over_forward(num_inference_steps=_a ) def __A ( self ) -> int: A_ = 27 for scheduler_class in self.scheduler_classes: A_ = self.dummy_sample A_ = 0.1 * sample A_ = self.get_scheduler_config() A_ = scheduler_class(**_a ) scheduler.set_timesteps(_a ) # before power of 3 fix, would error on first step, so we only need to do two for i, t in enumerate(scheduler.prk_timesteps[:2] ): A_ = scheduler.step_prk(_a , _a , _a ).prev_sample def __A ( self ) -> int: with self.assertRaises(_a ): A_ = self.scheduler_classes[0] A_ = self.get_scheduler_config() A_ = scheduler_class(**_a ) scheduler.step_plms(self.dummy_sample , 1 , self.dummy_sample ).prev_sample def __A ( self ) -> str: A_ = self.full_loop() A_ = torch.sum(torch.abs(_a ) ) A_ = torch.mean(torch.abs(_a ) ) assert abs(result_sum.item() - 198.1_318 ) < 1E-2 assert abs(result_mean.item() - 0.2_580 ) < 1E-3 def __A ( self ) -> str: A_ = self.full_loop(prediction_type='''v_prediction''' ) A_ = torch.sum(torch.abs(_a ) ) A_ = torch.mean(torch.abs(_a ) ) assert abs(result_sum.item() - 67.3_986 ) < 1E-2 assert abs(result_mean.item() - 0.0_878 ) < 1E-3 def __A ( self ) -> str: A_ = self.full_loop(set_alpha_to_one=_a , beta_start=0.01 ) A_ = torch.sum(torch.abs(_a ) ) A_ = torch.mean(torch.abs(_a ) ) assert abs(result_sum.item() - 230.0_399 ) < 1E-2 assert abs(result_mean.item() - 0.2_995 ) < 1E-3 def __A ( self ) -> Optional[Any]: A_ = self.full_loop(set_alpha_to_one=_a , beta_start=0.01 ) A_ = torch.sum(torch.abs(_a ) ) A_ = torch.mean(torch.abs(_a ) ) assert abs(result_sum.item() - 186.9_482 ) < 1E-2 assert abs(result_mean.item() - 0.2_434 ) < 1E-3
361
'''simple docstring''' def _UpperCAmelCase ( _UpperCamelCase : float, _UpperCamelCase : list[float] ) -> float: if discount_rate < 0: raise ValueError('''Discount rate cannot be negative''' ) if not cash_flows: raise ValueError('''Cash flows list cannot be empty''' ) A_ = sum( cash_flow / ((1 + discount_rate) ** i) for i, cash_flow in enumerate(_UpperCamelCase ) ) return round(_UpperCamelCase, ndigits=2 ) if __name__ == "__main__": import doctest doctest.testmod()
18
0
'''simple docstring''' import tempfile import torch from diffusers import ( DEISMultistepScheduler, DPMSolverMultistepScheduler, DPMSolverSinglestepScheduler, UniPCMultistepScheduler, ) from .test_schedulers import SchedulerCommonTest class __UpperCAmelCase ( _A ): '''simple docstring''' __lowercase : str = (DEISMultistepScheduler,) __lowercase : Union[str, Any] = (('num_inference_steps', 25),) def __A ( self , **_SCREAMING_SNAKE_CASE ) -> Optional[Any]: A_ = { '''num_train_timesteps''': 1000, '''beta_start''': 0.0_001, '''beta_end''': 0.02, '''beta_schedule''': '''linear''', '''solver_order''': 2, } config.update(**__SCREAMING_SNAKE_CASE ) return config def __A ( self , _SCREAMING_SNAKE_CASE=0 , **_SCREAMING_SNAKE_CASE ) -> int: A_ = dict(self.forward_default_kwargs ) A_ = kwargs.pop('''num_inference_steps''' , __SCREAMING_SNAKE_CASE ) A_ = self.dummy_sample A_ = 0.1 * sample A_ = [residual + 0.2, residual + 0.15, residual + 0.10] for scheduler_class in self.scheduler_classes: A_ = self.get_scheduler_config(**__SCREAMING_SNAKE_CASE ) A_ = scheduler_class(**__SCREAMING_SNAKE_CASE ) scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) # copy over dummy past residuals A_ = dummy_past_residuals[: scheduler.config.solver_order] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(__SCREAMING_SNAKE_CASE ) A_ = scheduler_class.from_pretrained(__SCREAMING_SNAKE_CASE ) new_scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) # copy over dummy past residuals A_ = dummy_past_residuals[: new_scheduler.config.solver_order] A_ ,A_ = sample, sample for t in range(__SCREAMING_SNAKE_CASE , time_step + scheduler.config.solver_order + 1 ): A_ = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).prev_sample A_ = new_scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" def __A ( self ) -> int: pass def __A ( self , _SCREAMING_SNAKE_CASE=0 , **_SCREAMING_SNAKE_CASE ) -> Optional[Any]: A_ = dict(self.forward_default_kwargs ) A_ = kwargs.pop('''num_inference_steps''' , __SCREAMING_SNAKE_CASE ) A_ = self.dummy_sample A_ = 0.1 * sample A_ = [residual + 0.2, residual + 0.15, residual + 0.10] for scheduler_class in self.scheduler_classes: A_ = self.get_scheduler_config() A_ = scheduler_class(**__SCREAMING_SNAKE_CASE ) scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) # copy over dummy past residuals (must be after setting timesteps) A_ = dummy_past_residuals[: scheduler.config.solver_order] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(__SCREAMING_SNAKE_CASE ) A_ = scheduler_class.from_pretrained(__SCREAMING_SNAKE_CASE ) # copy over dummy past residuals new_scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) # copy over dummy past residual (must be after setting timesteps) A_ = dummy_past_residuals[: new_scheduler.config.solver_order] A_ = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).prev_sample A_ = new_scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" def __A ( self , _SCREAMING_SNAKE_CASE=None , **_SCREAMING_SNAKE_CASE ) -> Dict: if scheduler is None: A_ = self.scheduler_classes[0] A_ = self.get_scheduler_config(**__SCREAMING_SNAKE_CASE ) A_ = scheduler_class(**__SCREAMING_SNAKE_CASE ) A_ = self.scheduler_classes[0] A_ = self.get_scheduler_config(**__SCREAMING_SNAKE_CASE ) A_ = scheduler_class(**__SCREAMING_SNAKE_CASE ) A_ = 10 A_ = self.dummy_model() A_ = self.dummy_sample_deter scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) for i, t in enumerate(scheduler.timesteps ): A_ = model(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) A_ = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ).prev_sample return sample def __A ( self ) -> Dict: A_ = dict(self.forward_default_kwargs ) A_ = kwargs.pop('''num_inference_steps''' , __SCREAMING_SNAKE_CASE ) for scheduler_class in self.scheduler_classes: A_ = self.get_scheduler_config() A_ = scheduler_class(**__SCREAMING_SNAKE_CASE ) A_ = self.dummy_sample A_ = 0.1 * sample if num_inference_steps is not None and hasattr(__SCREAMING_SNAKE_CASE , '''set_timesteps''' ): scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) elif num_inference_steps is not None and not hasattr(__SCREAMING_SNAKE_CASE , '''set_timesteps''' ): A_ = num_inference_steps # copy over dummy past residuals (must be done after set_timesteps) A_ = [residual + 0.2, residual + 0.15, residual + 0.10] A_ = dummy_past_residuals[: scheduler.config.solver_order] A_ = scheduler.timesteps[5] A_ = scheduler.timesteps[6] A_ = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).prev_sample A_ = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).prev_sample self.assertEqual(output_a.shape , sample.shape ) self.assertEqual(output_a.shape , output_a.shape ) def __A ( self ) -> Tuple: # make sure that iterating over schedulers with same config names gives same results # for defaults A_ = DEISMultistepScheduler(**self.get_scheduler_config() ) A_ = self.full_loop(scheduler=__SCREAMING_SNAKE_CASE ) A_ = torch.mean(torch.abs(__SCREAMING_SNAKE_CASE ) ) assert abs(result_mean.item() - 0.23_916 ) < 1E-3 A_ = DPMSolverSinglestepScheduler.from_config(scheduler.config ) A_ = DPMSolverMultistepScheduler.from_config(scheduler.config ) A_ = UniPCMultistepScheduler.from_config(scheduler.config ) A_ = DEISMultistepScheduler.from_config(scheduler.config ) A_ = self.full_loop(scheduler=__SCREAMING_SNAKE_CASE ) A_ = torch.mean(torch.abs(__SCREAMING_SNAKE_CASE ) ) assert abs(result_mean.item() - 0.23_916 ) < 1E-3 def __A ( self ) -> Dict: for timesteps in [25, 50, 100, 999, 1000]: self.check_over_configs(num_train_timesteps=__SCREAMING_SNAKE_CASE ) def __A ( self ) -> List[str]: self.check_over_configs(thresholding=__SCREAMING_SNAKE_CASE ) for order in [1, 2, 3]: for solver_type in ["logrho"]: for threshold in [0.5, 1.0, 2.0]: for prediction_type in ["epsilon", "sample"]: self.check_over_configs( thresholding=__SCREAMING_SNAKE_CASE , prediction_type=__SCREAMING_SNAKE_CASE , sample_max_value=__SCREAMING_SNAKE_CASE , algorithm_type='''deis''' , solver_order=__SCREAMING_SNAKE_CASE , solver_type=__SCREAMING_SNAKE_CASE , ) def __A ( self ) -> Union[str, Any]: for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=__SCREAMING_SNAKE_CASE ) def __A ( self ) -> Tuple: for algorithm_type in ["deis"]: for solver_type in ["logrho"]: for order in [1, 2, 3]: for prediction_type in ["epsilon", "sample"]: self.check_over_configs( solver_order=__SCREAMING_SNAKE_CASE , solver_type=__SCREAMING_SNAKE_CASE , prediction_type=__SCREAMING_SNAKE_CASE , algorithm_type=__SCREAMING_SNAKE_CASE , ) A_ = self.full_loop( solver_order=__SCREAMING_SNAKE_CASE , solver_type=__SCREAMING_SNAKE_CASE , prediction_type=__SCREAMING_SNAKE_CASE , algorithm_type=__SCREAMING_SNAKE_CASE , ) assert not torch.isnan(__SCREAMING_SNAKE_CASE ).any(), "Samples have nan numbers" def __A ( self ) -> Any: self.check_over_configs(lower_order_final=__SCREAMING_SNAKE_CASE ) self.check_over_configs(lower_order_final=__SCREAMING_SNAKE_CASE ) def __A ( self ) -> List[str]: for num_inference_steps in [1, 2, 3, 5, 10, 50, 100, 999, 1000]: self.check_over_forward(num_inference_steps=__SCREAMING_SNAKE_CASE , time_step=0 ) def __A ( self ) -> Optional[int]: A_ = self.full_loop() A_ = torch.mean(torch.abs(__SCREAMING_SNAKE_CASE ) ) assert abs(result_mean.item() - 0.23_916 ) < 1E-3 def __A ( self ) -> Optional[Any]: A_ = self.full_loop(prediction_type='''v_prediction''' ) A_ = torch.mean(torch.abs(__SCREAMING_SNAKE_CASE ) ) assert abs(result_mean.item() - 0.091 ) < 1E-3 def __A ( self ) -> int: A_ = self.scheduler_classes[0] A_ = self.get_scheduler_config(thresholding=__SCREAMING_SNAKE_CASE , dynamic_thresholding_ratio=0 ) A_ = scheduler_class(**__SCREAMING_SNAKE_CASE ) A_ = 10 A_ = self.dummy_model() A_ = self.dummy_sample_deter.half() scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) for i, t in enumerate(scheduler.timesteps ): A_ = model(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) A_ = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ).prev_sample assert sample.dtype == torch.floataa
362
'''simple docstring''' from __future__ import annotations def _UpperCAmelCase ( _UpperCamelCase : int | str ) -> bool: A_ = str(_UpperCamelCase ) return n == n[::-1] def _UpperCAmelCase ( _UpperCamelCase : int = 1_00_00_00 ) -> Any: A_ = 0 for i in range(1, _UpperCamelCase ): if is_palindrome(_UpperCamelCase ) and is_palindrome(bin(_UpperCamelCase ).split('''b''' )[1] ): total += i return total if __name__ == "__main__": print(solution(int(str(input().strip()))))
18
0
'''simple docstring''' 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 __snake_case : Optional[int] = 'scheduler_config.json' class __UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowercase : Tuple = 1 __lowercase : int = 2 __lowercase : Tuple = 3 __lowercase : Any = 4 __lowercase : List[Any] = 5 __lowercase : int = 6 __lowercase : Union[str, Any] = 7 __lowercase : Union[str, Any] = 8 __lowercase : Union[str, Any] = 9 __lowercase : Optional[int] = 10 __lowercase : Union[str, Any] = 11 __lowercase : Tuple = 12 __lowercase : Optional[int] = 13 __lowercase : Tuple = 14 @dataclass class __UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowercase : List[Any] = 42 class __UpperCAmelCase : '''simple docstring''' __lowercase : List[Any] = SCHEDULER_CONFIG_NAME __lowercase : Dict = [] __lowercase : Any = True @classmethod def __A ( cls , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE=False , **_SCREAMING_SNAKE_CASE , ) -> Optional[int]: A_ = cls.load_config( pretrained_model_name_or_path=_SCREAMING_SNAKE_CASE , subfolder=_SCREAMING_SNAKE_CASE , return_unused_kwargs=_SCREAMING_SNAKE_CASE , return_commit_hash=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE , ) return cls.from_config(_SCREAMING_SNAKE_CASE , return_unused_kwargs=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = False , **_SCREAMING_SNAKE_CASE ) -> List[Any]: self.save_config(save_directory=_SCREAMING_SNAKE_CASE , push_to_hub=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) @property def __A ( self ) -> Optional[int]: return self._get_compatibles() @classmethod def __A ( cls ) -> Optional[Any]: A_ = list(set([cls.__name__] + cls._compatibles ) ) A_ = importlib.import_module(__name__.split('''.''' )[0] ) A_ = [ getattr(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) for c in compatible_classes_str if hasattr(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ] return compatible_classes
363
'''simple docstring''' # Usage: # ./gen-card-facebook-wmt19.py import os from pathlib import Path def _UpperCAmelCase ( _UpperCamelCase : Tuple, _UpperCamelCase : Tuple, _UpperCamelCase : List[str] ) -> int: A_ = { '''en''': '''Machine learning is great, isn\'t it?''', '''ru''': '''Машинное обучение - это здорово, не так ли?''', '''de''': '''Maschinelles Lernen ist großartig, oder?''', } # BLUE scores as follows: # "pair": [fairseq, transformers] A_ = { '''ru-en''': ['''[41.3](http://matrix.statmt.org/matrix/output/1907?run_id=6937)''', '''39.20'''], '''en-ru''': ['''[36.4](http://matrix.statmt.org/matrix/output/1914?run_id=6724)''', '''33.47'''], '''en-de''': ['''[43.1](http://matrix.statmt.org/matrix/output/1909?run_id=6862)''', '''42.83'''], '''de-en''': ['''[42.3](http://matrix.statmt.org/matrix/output/1902?run_id=6750)''', '''41.35'''], } A_ = F'''{src_lang}-{tgt_lang}''' A_ = F''' --- language: - {src_lang} - {tgt_lang} thumbnail: tags: - translation - wmt19 - facebook license: apache-2.0 datasets: - wmt19 metrics: - bleu --- # FSMT ## Model description This is a ported version of [fairseq wmt19 transformer](https://github.com/pytorch/fairseq/blob/master/examples/wmt19/README.md) for {src_lang}-{tgt_lang}. For more details, please see, [Facebook FAIR\'s WMT19 News Translation Task Submission](https://arxiv.org/abs/1907.06616). The abbreviation FSMT stands for FairSeqMachineTranslation All four models are available: * [wmt19-en-ru](https://huggingface.co/facebook/wmt19-en-ru) * [wmt19-ru-en](https://huggingface.co/facebook/wmt19-ru-en) * [wmt19-en-de](https://huggingface.co/facebook/wmt19-en-de) * [wmt19-de-en](https://huggingface.co/facebook/wmt19-de-en) ## Intended uses & limitations #### How to use ```python from transformers import FSMTForConditionalGeneration, FSMTTokenizer mname = "facebook/wmt19-{src_lang}-{tgt_lang}" tokenizer = FSMTTokenizer.from_pretrained(mname) model = FSMTForConditionalGeneration.from_pretrained(mname) input = "{texts[src_lang]}" input_ids = tokenizer.encode(input, return_tensors="pt") outputs = model.generate(input_ids) decoded = tokenizer.decode(outputs[0], skip_special_tokens=True) print(decoded) # {texts[tgt_lang]} ``` #### Limitations and bias - The original (and this ported model) doesn\'t seem to handle well inputs with repeated sub-phrases, [content gets truncated](https://discuss.huggingface.co/t/issues-with-translating-inputs-containing-repeated-phrases/981) ## Training data Pretrained weights were left identical to the original model released by fairseq. For more details, please, see the [paper](https://arxiv.org/abs/1907.06616). ## Eval results pair | fairseq | transformers -------|---------|---------- {pair} | {scores[pair][0]} | {scores[pair][1]} The score is slightly below the score reported by `fairseq`, since `transformers`` currently doesn\'t support: - model ensemble, therefore the best performing checkpoint was ported (``model4.pt``). - re-ranking The score was calculated using this code: ```bash git clone https://github.com/huggingface/transformers cd transformers export PAIR={pair} export DATA_DIR=data/$PAIR export SAVE_DIR=data/$PAIR export BS=8 export NUM_BEAMS=15 mkdir -p $DATA_DIR sacrebleu -t wmt19 -l $PAIR --echo src > $DATA_DIR/val.source sacrebleu -t wmt19 -l $PAIR --echo ref > $DATA_DIR/val.target echo $PAIR PYTHONPATH="src:examples/seq2seq" python examples/seq2seq/run_eval.py facebook/wmt19-$PAIR $DATA_DIR/val.source $SAVE_DIR/test_translations.txt --reference_path $DATA_DIR/val.target --score_path $SAVE_DIR/test_bleu.json --bs $BS --task translation --num_beams $NUM_BEAMS ``` note: fairseq reports using a beam of 50, so you should get a slightly higher score if re-run with `--num_beams 50`. ## Data Sources - [training, etc.](http://www.statmt.org/wmt19/) - [test set](http://matrix.statmt.org/test_sets/newstest2019.tgz?1556572561) ### BibTeX entry and citation info ```bibtex @inproceedings{{..., year={{2020}}, title={{Facebook FAIR\'s WMT19 News Translation Task Submission}}, author={{Ng, Nathan and Yee, Kyra and Baevski, Alexei and Ott, Myle and Auli, Michael and Edunov, Sergey}}, booktitle={{Proc. of WMT}}, }} ``` ## TODO - port model ensemble (fairseq uses 4 model checkpoints) ''' os.makedirs(_UpperCamelCase, exist_ok=_UpperCamelCase ) A_ = os.path.join(_UpperCamelCase, '''README.md''' ) print(F'''Generating {path}''' ) with open(_UpperCamelCase, '''w''', encoding='''utf-8''' ) as f: f.write(_UpperCamelCase ) # make sure we are under the root of the project __snake_case : Any = Path(__file__).resolve().parent.parent.parent __snake_case : Tuple = repo_dir / 'model_cards' for model_name in ["wmt19-ru-en", "wmt19-en-ru", "wmt19-en-de", "wmt19-de-en"]: __snake_case , __snake_case , __snake_case : Any = model_name.split('-') __snake_case : int = model_cards_dir / 'facebook' / model_name write_model_card(model_card_dir, src_lang=src_lang, tgt_lang=tgt_lang)
18
0
'''simple docstring''' from collections import OrderedDict from typing import List, Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging __snake_case : int = logging.get_logger(__name__) __snake_case : Optional[int] = { 'google/efficientnet-b7': 'https://huggingface.co/google/efficientnet-b7/resolve/main/config.json', } class __UpperCAmelCase ( __snake_case ): '''simple docstring''' __lowercase : str = "efficientnet" def __init__( self , _SCREAMING_SNAKE_CASE = 3 , _SCREAMING_SNAKE_CASE = 600 , _SCREAMING_SNAKE_CASE = 2.0 , _SCREAMING_SNAKE_CASE = 3.1 , _SCREAMING_SNAKE_CASE = 8 , _SCREAMING_SNAKE_CASE = [3, 3, 5, 3, 5, 5, 3] , _SCREAMING_SNAKE_CASE = [32, 16, 24, 40, 80, 112, 192] , _SCREAMING_SNAKE_CASE = [16, 24, 40, 80, 112, 192, 320] , _SCREAMING_SNAKE_CASE = [] , _SCREAMING_SNAKE_CASE = [1, 2, 2, 2, 1, 2, 1] , _SCREAMING_SNAKE_CASE = [1, 2, 2, 3, 3, 4, 1] , _SCREAMING_SNAKE_CASE = [1, 6, 6, 6, 6, 6, 6] , _SCREAMING_SNAKE_CASE = 0.25 , _SCREAMING_SNAKE_CASE = "swish" , _SCREAMING_SNAKE_CASE = 2560 , _SCREAMING_SNAKE_CASE = "mean" , _SCREAMING_SNAKE_CASE = 0.02 , _SCREAMING_SNAKE_CASE = 0.001 , _SCREAMING_SNAKE_CASE = 0.99 , _SCREAMING_SNAKE_CASE = 0.5 , _SCREAMING_SNAKE_CASE = 0.2 , **_SCREAMING_SNAKE_CASE , ) -> Optional[int]: super().__init__(**lowerCamelCase_ ) A_ = num_channels A_ = image_size A_ = width_coefficient A_ = depth_coefficient A_ = depth_divisor A_ = kernel_sizes A_ = in_channels A_ = out_channels A_ = depthwise_padding A_ = strides A_ = num_block_repeats A_ = expand_ratios A_ = squeeze_expansion_ratio A_ = hidden_act A_ = hidden_dim A_ = pooling_type A_ = initializer_range A_ = batch_norm_eps A_ = batch_norm_momentum A_ = dropout_rate A_ = drop_connect_rate A_ = sum(lowerCamelCase_ ) * 4 class __UpperCAmelCase ( __snake_case ): '''simple docstring''' __lowercase : Tuple = version.parse('1.11' ) @property def __A ( self ) -> Mapping[str, Mapping[int, str]]: return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ] ) @property def __A ( self ) -> float: return 1E-5
364
'''simple docstring''' from collections import defaultdict def _UpperCAmelCase ( _UpperCamelCase : int ) -> int: A_ = 1 A_ = True for v in tree[start]: if v not in visited: ret += dfs(_UpperCamelCase ) if ret % 2 == 0: cuts.append(_UpperCamelCase ) return ret def _UpperCAmelCase ( ) -> Optional[Any]: dfs(1 ) if __name__ == "__main__": __snake_case , __snake_case : Union[str, Any] = 10, 9 __snake_case : int = defaultdict(list) __snake_case : dict[int, bool] = {} __snake_case : list[int] = [] __snake_case : Union[str, Any] = 0 __snake_case : int = [(2, 1), (3, 1), (4, 3), (5, 2), (6, 1), (7, 2), (8, 6), (9, 8), (10, 8)] for u, v in edges: tree[u].append(v) tree[v].append(u) even_tree() print(len(cuts) - 1)
18
0
'''simple docstring''' def _UpperCAmelCase ( _UpperCamelCase : List[Any] = 1_00 ) -> int: A_ = (n * (n + 1) // 2) ** 2 A_ = n * (n + 1) * (2 * n + 1) // 6 return sum_cubes - sum_squares if __name__ == "__main__": print(F"""{solution() = }""")
365
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging __snake_case : List[str] = logging.get_logger(__name__) __snake_case : Union[str, Any] = { 'alibaba-damo/mgp-str-base': 'https://huggingface.co/alibaba-damo/mgp-str-base/resolve/main/config.json', } class __UpperCAmelCase ( _UpperCamelCase ): '''simple docstring''' __lowercase : Optional[int] = 'mgp-str' def __init__( self , _SCREAMING_SNAKE_CASE=[32, 128] , _SCREAMING_SNAKE_CASE=4 , _SCREAMING_SNAKE_CASE=3 , _SCREAMING_SNAKE_CASE=27 , _SCREAMING_SNAKE_CASE=38 , _SCREAMING_SNAKE_CASE=5_0257 , _SCREAMING_SNAKE_CASE=3_0522 , _SCREAMING_SNAKE_CASE=768 , _SCREAMING_SNAKE_CASE=12 , _SCREAMING_SNAKE_CASE=12 , _SCREAMING_SNAKE_CASE=4.0 , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=False , _SCREAMING_SNAKE_CASE=1E-5 , _SCREAMING_SNAKE_CASE=0.0 , _SCREAMING_SNAKE_CASE=0.0 , _SCREAMING_SNAKE_CASE=0.0 , _SCREAMING_SNAKE_CASE=False , _SCREAMING_SNAKE_CASE=0.02 , **_SCREAMING_SNAKE_CASE , ) -> List[Any]: super().__init__(**_SCREAMING_SNAKE_CASE ) A_ = image_size A_ = patch_size A_ = num_channels A_ = max_token_length A_ = num_character_labels A_ = num_bpe_labels A_ = num_wordpiece_labels A_ = hidden_size A_ = num_hidden_layers A_ = num_attention_heads A_ = mlp_ratio A_ = distilled A_ = layer_norm_eps A_ = drop_rate A_ = qkv_bias A_ = attn_drop_rate A_ = drop_path_rate A_ = output_aa_attentions A_ = initializer_range
18
0
'''simple docstring''' from typing import Union from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging from .base import PIPELINE_INIT_ARGS, Pipeline if is_vision_available(): from PIL import Image from ..image_utils import load_image if is_torch_available(): from ..models.auto.modeling_auto import MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING __snake_case : Dict = logging.get_logger(__name__) @add_end_docstrings(lowerCamelCase_ ) class __UpperCAmelCase ( lowerCamelCase_ ): '''simple docstring''' def __init__( self , *_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) -> List[str]: super().__init__(*_UpperCAmelCase , **_UpperCAmelCase ) self.check_model_type(_UpperCAmelCase ) def __A ( self , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE=None , **_SCREAMING_SNAKE_CASE ) -> Dict: A_ ,A_ = {}, {} if padding is not None: A_ = padding if truncation is not None: A_ = truncation if top_k is not None: A_ = top_k return preprocess_params, {}, postprocess_params def __call__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None , **_SCREAMING_SNAKE_CASE ) -> Dict: if isinstance(_UpperCAmelCase , (Image.Image, str) ) and isinstance(_UpperCAmelCase , _UpperCAmelCase ): A_ = {'''image''': image, '''question''': question} else: A_ = image A_ = super().__call__(_UpperCAmelCase , **_UpperCAmelCase ) return results def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=False , _SCREAMING_SNAKE_CASE=False ) -> int: A_ = load_image(inputs['''image'''] ) A_ = self.tokenizer( inputs['''question'''] , return_tensors=self.framework , padding=_UpperCAmelCase , truncation=_UpperCAmelCase ) A_ = self.image_processor(images=_UpperCAmelCase , return_tensors=self.framework ) model_inputs.update(_UpperCAmelCase ) return model_inputs def __A ( self , _SCREAMING_SNAKE_CASE ) -> Optional[Any]: A_ = self.model(**_UpperCAmelCase ) return model_outputs def __A ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=5 ) -> List[str]: if top_k > self.model.config.num_labels: A_ = self.model.config.num_labels if self.framework == "pt": A_ = model_outputs.logits.sigmoid()[0] A_ ,A_ = probs.topk(_UpperCAmelCase ) else: raise ValueError(F'''Unsupported framework: {self.framework}''' ) A_ = scores.tolist() A_ = ids.tolist() return [{"score": score, "answer": self.model.config.idalabel[_id]} for score, _id in zip(_UpperCAmelCase , _UpperCAmelCase )]
366
'''simple docstring''' from diffusers.utils.testing_utils import require_onnxruntime @require_onnxruntime class __UpperCAmelCase : '''simple docstring''' pass
18
0