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""" import importlib import inspect import os import re # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/check_config_docstrings.py __lowerCAmelCase : Optional[int] ="""src/transformers""" # This is to make sure the transformers module imported is the one in the repo. __lowerCAmelCase : Any =importlib.util.spec_from_file_location( """transformers""", os.path.join(PATH_TO_TRANSFORMERS, """__init__.py"""), submodule_search_locations=[PATH_TO_TRANSFORMERS], ) __lowerCAmelCase : List[str] =spec.loader.load_module() __lowerCAmelCase : List[Any] =transformers.models.auto.configuration_auto.CONFIG_MAPPING # Regex pattern used to find the checkpoint mentioned in the docstring of `config_class`. # For example, `[bert-base-uncased](https://huggingface.co/bert-base-uncased)` __lowerCAmelCase : Optional[int] =re.compile("""\[(.+?)\]\((https://huggingface\.co/.+?)\)""") __lowerCAmelCase : Optional[int] ={ """CLIPConfigMixin""", """DecisionTransformerConfigMixin""", """EncoderDecoderConfigMixin""", """RagConfigMixin""", """SpeechEncoderDecoderConfigMixin""", """VisionEncoderDecoderConfigMixin""", """VisionTextDualEncoderConfigMixin""", } def UpperCAmelCase__ ( ) -> int: '''simple docstring''' lowercase = [] for config_class in list(CONFIG_MAPPING.values() ): lowercase = False # source code of `config_class` lowercase = inspect.getsource(SCREAMING_SNAKE_CASE_ ) lowercase = _re_checkpoint.findall(SCREAMING_SNAKE_CASE_ ) for checkpoint in checkpoints: # Each `checkpoint` is a tuple of a checkpoint name and a checkpoint link. # For example, `('bert-base-uncased', 'https://huggingface.co/bert-base-uncased')` lowercase = checkpoint # verify the checkpoint name corresponds to the checkpoint link lowercase = f'https://huggingface.co/{ckpt_name}' if ckpt_link == ckpt_link_from_name: lowercase = True break lowercase = config_class.__name__ if not checkpoint_found and name not in CONFIG_CLASSES_TO_IGNORE_FOR_DOCSTRING_CHECKPOINT_CHECK: configs_without_checkpoint.append(SCREAMING_SNAKE_CASE_ ) if len(SCREAMING_SNAKE_CASE_ ) > 0: lowercase = '\n'.join(sorted(SCREAMING_SNAKE_CASE_ ) ) raise ValueError(f'The following configurations don\'t contain any valid checkpoint:\n{message}' ) if __name__ == "__main__": check_config_docstrings_have_checkpoints()
197
import datetime import platform import subprocess from typing import Optional, Tuple, Union import numpy as np def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> np.array: lowerCAmelCase__ : Dict = F'''{sampling_rate}''' lowerCAmelCase__ : Any = '1' lowerCAmelCase__ : Optional[Any] = 'f32le' lowerCAmelCase__ : Any = [ 'ffmpeg', '-i', 'pipe:0', '-ac', ac, '-ar', ar, '-f', format_for_conversion, '-hide_banner', '-loglevel', 'quiet', 'pipe:1', ] try: with subprocess.Popen(SCREAMING_SNAKE_CASE_ , stdin=subprocess.PIPE , stdout=subprocess.PIPE ) as ffmpeg_process: lowerCAmelCase__ : List[Any] = ffmpeg_process.communicate(SCREAMING_SNAKE_CASE_ ) except FileNotFoundError as error: raise ValueError('ffmpeg was not found but is required to load audio files from filename' ) from error lowerCAmelCase__ : List[str] = output_stream[0] lowerCAmelCase__ : str = np.frombuffer(SCREAMING_SNAKE_CASE_ , np.floataa ) if audio.shape[0] == 0: raise ValueError('Malformed soundfile' ) return audio def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = "f32le" , ) -> Dict: lowerCAmelCase__ : Optional[Any] = F'''{sampling_rate}''' lowerCAmelCase__ : Any = '1' if format_for_conversion == "s16le": lowerCAmelCase__ : Dict = 2 elif format_for_conversion == "f32le": lowerCAmelCase__ : List[str] = 4 else: raise ValueError(F'''Unhandled format `{format_for_conversion}`. Please use `s16le` or `f32le`''' ) lowerCAmelCase__ : Tuple = platform.system() if system == "Linux": lowerCAmelCase__ : str = 'alsa' lowerCAmelCase__ : str = 'default' elif system == "Darwin": lowerCAmelCase__ : Any = 'avfoundation' lowerCAmelCase__ : Tuple = ':0' elif system == "Windows": lowerCAmelCase__ : Any = 'dshow' lowerCAmelCase__ : int = 'default' lowerCAmelCase__ : Any = [ 'ffmpeg', '-f', format_, '-i', input_, '-ac', ac, '-ar', ar, '-f', format_for_conversion, '-fflags', 'nobuffer', '-hide_banner', '-loglevel', 'quiet', 'pipe:1', ] lowerCAmelCase__ : List[str] = int(round(sampling_rate * chunk_length_s ) ) * size_of_sample lowerCAmelCase__ : str = _ffmpeg_stream(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) for item in iterator: yield item def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = None , SCREAMING_SNAKE_CASE_ = None , SCREAMING_SNAKE_CASE_ = "f32le" , ) -> str: if stream_chunk_s is not None: lowerCAmelCase__ : Union[str, Any] = stream_chunk_s else: lowerCAmelCase__ : Tuple = chunk_length_s lowerCAmelCase__ : Any = ffmpeg_microphone(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , format_for_conversion=SCREAMING_SNAKE_CASE_ ) if format_for_conversion == "s16le": lowerCAmelCase__ : Optional[Any] = np.intaa lowerCAmelCase__ : Optional[Any] = 2 elif format_for_conversion == "f32le": lowerCAmelCase__ : Optional[Any] = np.floataa lowerCAmelCase__ : Optional[Any] = 4 else: raise ValueError(F'''Unhandled format `{format_for_conversion}`. Please use `s16le` or `f32le`''' ) if stride_length_s is None: lowerCAmelCase__ : Dict = chunk_length_s / 6 lowerCAmelCase__ : int = int(round(sampling_rate * chunk_length_s ) ) * size_of_sample if isinstance(SCREAMING_SNAKE_CASE_ , (int, float) ): lowerCAmelCase__ : Dict = [stride_length_s, stride_length_s] lowerCAmelCase__ : Union[str, Any] = int(round(sampling_rate * stride_length_s[0] ) ) * size_of_sample lowerCAmelCase__ : List[Any] = int(round(sampling_rate * stride_length_s[1] ) ) * size_of_sample lowerCAmelCase__ : Any = datetime.datetime.now() lowerCAmelCase__ : Any = datetime.timedelta(seconds=SCREAMING_SNAKE_CASE_ ) for item in chunk_bytes_iter(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , stride=(stride_left, stride_right) , stream=SCREAMING_SNAKE_CASE_ ): # Put everything back in numpy scale lowerCAmelCase__ : Any = np.frombuffer(item['raw'] , dtype=SCREAMING_SNAKE_CASE_ ) lowerCAmelCase__ : Optional[Any] = ( item['stride'][0] // size_of_sample, item['stride'][1] // size_of_sample, ) lowerCAmelCase__ : Optional[int] = sampling_rate audio_time += delta if datetime.datetime.now() > audio_time + 10 * delta: # We're late !! SKIP continue yield item def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = False ) -> Optional[int]: lowerCAmelCase__ : Union[str, Any] = b'' lowerCAmelCase__ , lowerCAmelCase__ : Union[str, Any] = stride if stride_left + stride_right >= chunk_len: raise ValueError( F'''Stride needs to be strictly smaller than chunk_len: ({stride_left}, {stride_right}) vs {chunk_len}''' ) lowerCAmelCase__ : List[str] = 0 for raw in iterator: acc += raw if stream and len(SCREAMING_SNAKE_CASE_ ) < chunk_len: lowerCAmelCase__ : Tuple = (_stride_left, 0) yield {"raw": acc[:chunk_len], "stride": stride, "partial": True} else: while len(SCREAMING_SNAKE_CASE_ ) >= chunk_len: # We are flushing the accumulator lowerCAmelCase__ : Dict = (_stride_left, stride_right) lowerCAmelCase__ : Any = {'raw': acc[:chunk_len], 'stride': stride} if stream: lowerCAmelCase__ : Optional[int] = False yield item lowerCAmelCase__ : Optional[int] = stride_left lowerCAmelCase__ : Optional[int] = acc[chunk_len - stride_left - stride_right :] # Last chunk if len(SCREAMING_SNAKE_CASE_ ) > stride_left: lowerCAmelCase__ : Tuple = {'raw': acc, 'stride': (_stride_left, 0)} if stream: lowerCAmelCase__ : Any = False yield item def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> str: lowerCAmelCase__ : int = 2**24 # 16Mo try: with subprocess.Popen(SCREAMING_SNAKE_CASE_ , stdout=subprocess.PIPE , bufsize=SCREAMING_SNAKE_CASE_ ) as ffmpeg_process: while True: lowerCAmelCase__ : List[str] = ffmpeg_process.stdout.read(SCREAMING_SNAKE_CASE_ ) if raw == b"": break yield raw except FileNotFoundError as error: raise ValueError('ffmpeg was not found but is required to stream audio files from filename' ) from error
212
0
"""simple docstring""" def _snake_case ( lowercase__ , lowercase__ , lowercase__ , lowercase__ ): global f # a global dp table for knapsack if f[i][j] < 0: if j < wt[i - 1]: _lowerCamelCase : Optional[int] = mf_knapsack(i - 1 , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else: _lowerCamelCase : Optional[int] = max( mf_knapsack(i - 1 , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) , mf_knapsack(i - 1 , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , j - wt[i - 1] ) + val[i - 1] , ) _lowerCamelCase : Tuple = val return f[i][j] def _snake_case ( lowercase__ , lowercase__ , lowercase__ , lowercase__ ): _lowerCamelCase : Dict = [[0] * (w + 1) for _ in range(n + 1 )] for i in range(1 , n + 1 ): for w_ in range(1 , w + 1 ): if wt[i - 1] <= w_: _lowerCamelCase : int = max(val[i - 1] + dp[i - 1][w_ - wt[i - 1]] , dp[i - 1][w_] ) else: _lowerCamelCase : str = dp[i - 1][w_] return dp[n][w_], dp def _snake_case ( lowercase__ , lowercase__ , lowercase__ ): if not (isinstance(SCREAMING_SNAKE_CASE_ , (list, tuple) ) and isinstance(SCREAMING_SNAKE_CASE_ , (list, tuple) )): raise ValueError( 'Both the weights and values vectors must be either lists or tuples' ) _lowerCamelCase : Union[str, Any] = len(SCREAMING_SNAKE_CASE_ ) if num_items != len(SCREAMING_SNAKE_CASE_ ): _lowerCamelCase : Dict = ( 'The number of weights must be the same as the number of values.\n' f'''But got {num_items} weights and {len(SCREAMING_SNAKE_CASE_ )} values''' ) raise ValueError(SCREAMING_SNAKE_CASE_ ) for i in range(SCREAMING_SNAKE_CASE_ ): if not isinstance(wt[i] , SCREAMING_SNAKE_CASE_ ): _lowerCamelCase : Any = ( 'All weights must be integers but got weight of ' f'''type {type(wt[i] )} at index {i}''' ) raise TypeError(SCREAMING_SNAKE_CASE_ ) _lowerCamelCase, _lowerCamelCase : Dict = knapsack(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) _lowerCamelCase : Any = set() _construct_solution(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) return optimal_val, example_optional_set def _snake_case ( lowercase__ , lowercase__ , lowercase__ , lowercase__ , lowercase__ ): if i > 0 and j > 0: if dp[i - 1][j] == dp[i][j]: _construct_solution(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , i - 1 , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else: optimal_set.add(SCREAMING_SNAKE_CASE_ ) _construct_solution(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , i - 1 , j - wt[i - 1] , SCREAMING_SNAKE_CASE_ ) if __name__ == "__main__": lowercase__ = [3, 2, 4, 4] lowercase__ = [4, 3, 2, 3] lowercase__ = 4 lowercase__ = 6 lowercase__ = [[0] * (w + 1)] + [[0] + [-1] * (w + 1) for _ in range(n + 1)] lowercase__ = knapsack(w, wt, val, n) print(optimal_solution) print(mf_knapsack(n, wt, val, w)) # switched the n and w # testing the dynamic programming problem with example # the optimal subset for the above example are items 3 and 4 lowercase__ = knapsack_with_example_solution(w, wt, val) assert optimal_solution == 8 assert optimal_subset == {3, 4} print("""optimal_value = """, optimal_solution) print("""An optimal subset corresponding to the optimal value""", optimal_subset)
365
"""simple docstring""" import json import os from typing import Optional import numpy as np from ...feature_extraction_utils import BatchFeature from ...processing_utils import ProcessorMixin from ...utils import logging from ...utils.hub import get_file_from_repo from ..auto import AutoTokenizer lowercase__ = logging.get_logger(__name__) class lowerCAmelCase__ ( lowercase ): '''simple docstring''' lowerCamelCase__ = """AutoTokenizer""" lowerCamelCase__ = ["""tokenizer"""] lowerCamelCase__ = { """semantic_prompt""": 1, """coarse_prompt""": 2, """fine_prompt""": 2, } def __init__( self , lowercase , lowercase=None ): super().__init__(lowercase ) _lowerCamelCase : Optional[int] = speaker_embeddings @classmethod def A_ ( cls , lowercase , lowercase="speaker_embeddings_path.json" , **lowercase ): if speaker_embeddings_dict_path is not None: _lowerCamelCase : Optional[Any] = get_file_from_repo( lowercase , lowercase , subfolder=kwargs.pop('subfolder' , lowercase ) , cache_dir=kwargs.pop('cache_dir' , lowercase ) , force_download=kwargs.pop('force_download' , lowercase ) , proxies=kwargs.pop('proxies' , lowercase ) , resume_download=kwargs.pop('resume_download' , lowercase ) , local_files_only=kwargs.pop('local_files_only' , lowercase ) , use_auth_token=kwargs.pop('use_auth_token' , lowercase ) , revision=kwargs.pop('revision' , lowercase ) , ) if speaker_embeddings_path is None: logger.warning( F'''`{os.path.join(lowercase , lowercase )}` does not exists , no preloaded speaker embeddings will be used - Make sure to provide a correct path to the json dictionnary if wanted, otherwise set `speaker_embeddings_dict_path=None`.''' ) _lowerCamelCase : List[Any] = None else: with open(lowercase ) as speaker_embeddings_json: _lowerCamelCase : Union[str, Any] = json.load(lowercase ) else: _lowerCamelCase : Tuple = None _lowerCamelCase : Union[str, Any] = AutoTokenizer.from_pretrained(lowercase , **lowercase ) return cls(tokenizer=lowercase , speaker_embeddings=lowercase ) def A_ ( self , lowercase , lowercase="speaker_embeddings_path.json" , lowercase="speaker_embeddings" , lowercase = False , **lowercase , ): if self.speaker_embeddings is not None: os.makedirs(os.path.join(lowercase , lowercase , 'v2' ) , exist_ok=lowercase ) _lowerCamelCase : int = {} _lowerCamelCase : List[Any] = save_directory for prompt_key in self.speaker_embeddings: if prompt_key != "repo_or_path": _lowerCamelCase : Optional[Any] = self._load_voice_preset(lowercase ) _lowerCamelCase : Any = {} for key in self.speaker_embeddings[prompt_key]: np.save( os.path.join( embeddings_dict['repo_or_path'] , lowercase , F'''{prompt_key}_{key}''' ) , voice_preset[key] , allow_pickle=lowercase , ) _lowerCamelCase : List[str] = os.path.join(lowercase , F'''{prompt_key}_{key}.npy''' ) _lowerCamelCase : Optional[Any] = tmp_dict with open(os.path.join(lowercase , lowercase ) , 'w' ) as fp: json.dump(lowercase , lowercase ) super().save_pretrained(lowercase , lowercase , **lowercase ) def A_ ( self , lowercase = None , **lowercase ): _lowerCamelCase : Tuple = self.speaker_embeddings[voice_preset] _lowerCamelCase : Any = {} for key in ["semantic_prompt", "coarse_prompt", "fine_prompt"]: if key not in voice_preset_paths: raise ValueError( F'''Voice preset unrecognized, missing {key} as a key in self.speaker_embeddings[{voice_preset}].''' ) _lowerCamelCase : Union[str, Any] = get_file_from_repo( self.speaker_embeddings.get('repo_or_path' , '/' ) , voice_preset_paths[key] , subfolder=kwargs.pop('subfolder' , lowercase ) , cache_dir=kwargs.pop('cache_dir' , lowercase ) , force_download=kwargs.pop('force_download' , lowercase ) , proxies=kwargs.pop('proxies' , lowercase ) , resume_download=kwargs.pop('resume_download' , lowercase ) , local_files_only=kwargs.pop('local_files_only' , lowercase ) , use_auth_token=kwargs.pop('use_auth_token' , lowercase ) , revision=kwargs.pop('revision' , lowercase ) , ) if path is None: raise ValueError( F'''`{os.path.join(self.speaker_embeddings.get('repo_or_path' , '/' ) , voice_preset_paths[key] )}` does not exists , no preloaded voice preset will be used - Make sure to provide correct paths to the {voice_preset} embeddings.''' ) _lowerCamelCase : List[str] = np.load(lowercase ) return voice_preset_dict def A_ ( self , lowercase = None ): for key in ["semantic_prompt", "coarse_prompt", "fine_prompt"]: if key not in voice_preset: raise ValueError(F'''Voice preset unrecognized, missing {key} as a key.''' ) if not isinstance(voice_preset[key] , np.ndarray ): raise ValueError(F'''{key} voice preset must be a {str(self.preset_shape[key] )}D ndarray.''' ) if len(voice_preset[key].shape ) != self.preset_shape[key]: raise ValueError(F'''{key} voice preset must be a {str(self.preset_shape[key] )}D ndarray.''' ) def __call__( self , lowercase=None , lowercase=None , lowercase="pt" , lowercase=256 , lowercase=False , lowercase=True , lowercase=False , **lowercase , ): if voice_preset is not None and not isinstance(lowercase , lowercase ): if ( isinstance(lowercase , lowercase ) and self.speaker_embeddings is not None and voice_preset in self.speaker_embeddings ): _lowerCamelCase : Any = self._load_voice_preset(lowercase ) else: if isinstance(lowercase , lowercase ) and not voice_preset.endswith('.npz' ): _lowerCamelCase : Optional[Any] = voice_preset + '.npz' _lowerCamelCase : Union[str, Any] = np.load(lowercase ) if voice_preset is not None: self._validate_voice_preset_dict(lowercase , **lowercase ) _lowerCamelCase : Tuple = BatchFeature(data=lowercase , tensor_type=lowercase ) _lowerCamelCase : Any = self.tokenizer( lowercase , return_tensors=lowercase , padding='max_length' , max_length=lowercase , return_attention_mask=lowercase , return_token_type_ids=lowercase , add_special_tokens=lowercase , **lowercase , ) if voice_preset is not None: _lowerCamelCase : Optional[int] = voice_preset return encoded_text
12
0
'''simple docstring''' import io import itertools import json from dataclasses import dataclass from typing import Optional import pyarrow as pa import pyarrow.json as paj import datasets from datasets.table import table_cast from datasets.utils.file_utils import readline _UpperCamelCase : Dict = datasets.utils.logging.get_logger(__name__) @dataclass class snake_case__ ( datasets.BuilderConfig): a_ = None a_ = "utf-8" a_ = None a_ = None a_ = True # deprecated a_ = None # deprecated a_ = 10 << 20 # 10MB a_ = None class snake_case__ ( datasets.ArrowBasedBuilder): a_ = JsonConfig def A ( self : List[Any] ) -> Union[str, Any]: if self.config.block_size is not None: logger.warning('''The JSON loader parameter `block_size` is deprecated. Please use `chunksize` instead''' ) UpperCAmelCase_ : Union[str, Any] = self.config.block_size if self.config.use_threads is not True: logger.warning( '''The JSON loader parameter `use_threads` is deprecated and doesn\'t have any effect anymore.''' ) if self.config.newlines_in_values is not None: raise ValueError('''The JSON loader parameter `newlines_in_values` is no longer supported''' ) return datasets.DatasetInfo(features=self.config.features ) def A ( self : int , _A : Dict ) -> str: if not self.config.data_files: raise ValueError(F"At least one data file must be specified, but got data_files={self.config.data_files}" ) UpperCAmelCase_ : List[str] = dl_manager.download_and_extract(self.config.data_files ) if isinstance(_A , (str, list, tuple) ): UpperCAmelCase_ : Optional[int] = data_files if isinstance(_A , _A ): UpperCAmelCase_ : List[Any] = [files] UpperCAmelCase_ : Optional[Any] = [dl_manager.iter_files(_A ) for file in files] return [datasets.SplitGenerator(name=datasets.Split.TRAIN , gen_kwargs={'''files''': files} )] UpperCAmelCase_ : Optional[int] = [] for split_name, files in data_files.items(): if isinstance(_A , _A ): UpperCAmelCase_ : int = [files] UpperCAmelCase_ : Dict = [dl_manager.iter_files(_A ) for file in files] splits.append(datasets.SplitGenerator(name=_A , gen_kwargs={'''files''': files} ) ) return splits def A ( self : Union[str, Any] , _A : pa.Table ) -> pa.Table: if self.config.features is not None: # adding missing columns for column_name in set(self.config.features ) - set(pa_table.column_names ): UpperCAmelCase_ : Any = self.config.features.arrow_schema.field(_A ).type UpperCAmelCase_ : List[Any] = pa_table.append_column(_A , pa.array([None] * len(_A ) , type=_A ) ) # more expensive cast to support nested structures with keys in a different order # allows str <-> int/float or str to Audio for example UpperCAmelCase_ : Union[str, Any] = table_cast(_A , self.config.features.arrow_schema ) return pa_table def A ( self : Optional[Any] , _A : List[str] ) -> Any: for file_idx, file in enumerate(itertools.chain.from_iterable(_A ) ): # If the file is one json object and if we need to look at the list of items in one specific field if self.config.field is not None: with open(_A , encoding=self.config.encoding , errors=self.config.encoding_errors ) as f: UpperCAmelCase_ : Tuple = json.load(_A ) # We keep only the field we are interested in UpperCAmelCase_ : str = dataset[self.config.field] # We accept two format: a list of dicts or a dict of lists if isinstance(_A , (list, tuple) ): UpperCAmelCase_ : str = set().union(*[row.keys() for row in dataset] ) UpperCAmelCase_ : Dict = {col: [row.get(_A ) for row in dataset] for col in keys} else: UpperCAmelCase_ : Optional[int] = dataset UpperCAmelCase_ : Dict = pa.Table.from_pydict(_A ) yield file_idx, self._cast_table(_A ) # If the file has one json object per line else: with open(_A , '''rb''' ) as f: UpperCAmelCase_ : Dict = 0 # Use block_size equal to the chunk size divided by 32 to leverage multithreading # Set a default minimum value of 16kB if the chunk size is really small UpperCAmelCase_ : str = max(self.config.chunksize // 32 , 16 << 10 ) UpperCAmelCase_ : str = ( self.config.encoding_errors if self.config.encoding_errors is not None else '''strict''' ) while True: UpperCAmelCase_ : Optional[int] = f.read(self.config.chunksize ) if not batch: break # Finish current line try: batch += f.readline() except (AttributeError, io.UnsupportedOperation): batch += readline(_A ) # PyArrow only accepts utf-8 encoded bytes if self.config.encoding != "utf-8": UpperCAmelCase_ : Dict = batch.decode(self.config.encoding , errors=_A ).encode('''utf-8''' ) try: while True: try: UpperCAmelCase_ : str = paj.read_json( io.BytesIO(_A ) , read_options=paj.ReadOptions(block_size=_A ) ) break except (pa.ArrowInvalid, pa.ArrowNotImplementedError) as e: if ( isinstance(_A , pa.ArrowInvalid ) and "straddling" not in str(_A ) or block_size > len(_A ) ): raise else: # Increase the block size in case it was too small. # The block size will be reset for the next file. logger.debug( F"Batch of {len(_A )} bytes couldn't be parsed with block_size={block_size}. Retrying with block_size={block_size * 2}." ) block_size *= 2 except pa.ArrowInvalid as e: try: with open( _A , encoding=self.config.encoding , errors=self.config.encoding_errors ) as f: UpperCAmelCase_ : int = json.load(_A ) except json.JSONDecodeError: logger.error(F"Failed to read file '{file}' with error {type(_A )}: {e}" ) raise e # If possible, parse the file as a list of json objects and exit the loop if isinstance(_A , _A ): # list is the only sequence type supported in JSON try: UpperCAmelCase_ : Optional[int] = set().union(*[row.keys() for row in dataset] ) UpperCAmelCase_ : Optional[Any] = {col: [row.get(_A ) for row in dataset] for col in keys} UpperCAmelCase_ : int = pa.Table.from_pydict(_A ) except (pa.ArrowInvalid, AttributeError) as e: logger.error(F"Failed to read file '{file}' with error {type(_A )}: {e}" ) raise ValueError(F"Not able to read records in the JSON file at {file}." ) from None yield file_idx, self._cast_table(_A ) break else: logger.error(F"Failed to read file '{file}' with error {type(_A )}: {e}" ) raise ValueError( F"Not able to read records in the JSON file at {file}. " F"You should probably indicate the field of the JSON file containing your records. " F"This JSON file contain the following fields: {str(list(dataset.keys() ) )}. " F"Select the correct one and provide it as `field='XXX'` to the dataset loading method. " ) from None # Uncomment for debugging (will print the Arrow table size and elements) # logger.warning(f"pa_table: {pa_table} num rows: {pa_table.num_rows}") # logger.warning('\n'.join(str(pa_table.slice(i, 1).to_pydict()) for i in range(pa_table.num_rows))) yield (file_idx, batch_idx), self._cast_table(_A ) batch_idx += 1
304
'''simple docstring''' import logging from transformers.configuration_utils import PretrainedConfig _UpperCamelCase : Any = logging.getLogger(__name__) class snake_case__ ( UpperCamelCase): a_ = "masked_bert" def __init__( self : str , _A : Dict=3_05_22 , _A : Dict=7_68 , _A : Union[str, Any]=12 , _A : str=12 , _A : str=30_72 , _A : Dict="gelu" , _A : int=0.1 , _A : Optional[Any]=0.1 , _A : Any=5_12 , _A : Union[str, Any]=2 , _A : Union[str, Any]=0.02 , _A : int=1e-12 , _A : Any=0 , _A : Any="topK" , _A : List[str]="constant" , _A : Dict=0.0 , **_A : int , ) -> Union[str, Any]: super().__init__(pad_token_id=_A , **_A ) UpperCAmelCase_ : Union[str, Any] = vocab_size UpperCAmelCase_ : str = hidden_size UpperCAmelCase_ : Union[str, Any] = num_hidden_layers UpperCAmelCase_ : Optional[int] = num_attention_heads UpperCAmelCase_ : Optional[Any] = hidden_act UpperCAmelCase_ : str = intermediate_size UpperCAmelCase_ : int = hidden_dropout_prob UpperCAmelCase_ : Tuple = attention_probs_dropout_prob UpperCAmelCase_ : Optional[Any] = max_position_embeddings UpperCAmelCase_ : List[str] = type_vocab_size UpperCAmelCase_ : str = initializer_range UpperCAmelCase_ : Union[str, Any] = layer_norm_eps UpperCAmelCase_ : Optional[int] = pruning_method UpperCAmelCase_ : Optional[int] = mask_init UpperCAmelCase_ : List[Any] = mask_scale
304
1
"""simple docstring""" from __future__ import annotations import typing from collections.abc import Iterable import numpy as np lowercase_ = typing.Union[Iterable[float], Iterable[int], np.ndarray] # noqa: UP007 lowercase_ = typing.Union[np.floataa, int, float] # noqa: UP007 def lowercase ( lowerCAmelCase__ : Vector , lowerCAmelCase__ : Vector ) -> VectorOut: return np.sqrt(np.sum((np.asarray(lowerCAmelCase__ ) - np.asarray(lowerCAmelCase__ )) ** 2 ) ) def lowercase ( lowerCAmelCase__ : Vector , lowerCAmelCase__ : Vector ) -> VectorOut: return sum((va - va) ** 2 for va, va in zip(lowerCAmelCase__ , lowerCAmelCase__ ) ) ** (1 / 2) if __name__ == "__main__": def lowercase ( ) -> None: from timeit import timeit print('''Without Numpy''' ) print( timeit( '''euclidean_distance_no_np([1, 2, 3], [4, 5, 6])''' , number=10000 , globals=globals() , ) ) print('''With Numpy''' ) print( timeit( '''euclidean_distance([1, 2, 3], [4, 5, 6])''' , number=10000 , globals=globals() , ) ) benchmark()
11
"""simple docstring""" from typing import Callable, List, Optional, Union import PIL import torch from transformers import ( CLIPImageProcessor, CLIPSegForImageSegmentation, CLIPSegProcessor, CLIPTextModel, CLIPTokenizer, ) from diffusers import DiffusionPipeline from diffusers.configuration_utils import FrozenDict from diffusers.models import AutoencoderKL, UNetaDConditionModel from diffusers.pipelines.stable_diffusion import StableDiffusionInpaintPipeline from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker from diffusers.schedulers import DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler from diffusers.utils import deprecate, is_accelerate_available, logging lowercase_ = logging.get_logger(__name__) # pylint: disable=invalid-name class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self , _a , _a , _a , _a , _a , _a , _a , _a , _a , ): super().__init__() if hasattr(scheduler.config , '''steps_offset''' ) and scheduler.config.steps_offset != 1: __a = ( f'''The configuration file of this scheduler: {scheduler} is outdated. `steps_offset`''' f''' should be set to 1 instead of {scheduler.config.steps_offset}. Please make sure ''' '''to update the config accordingly as leaving `steps_offset` might led to incorrect results''' ''' in future versions. If you have downloaded this checkpoint from the Hugging Face Hub,''' ''' it would be very nice if you could open a Pull request for the `scheduler/scheduler_config.json`''' ''' file''' ) deprecate('''steps_offset!=1''' , '''1.0.0''' , _a , standard_warn=_a ) __a = dict(scheduler.config ) __a = 1 __a = FrozenDict(_a ) if hasattr(scheduler.config , '''skip_prk_steps''' ) and scheduler.config.skip_prk_steps is False: __a = ( f'''The configuration file of this scheduler: {scheduler} has not set the configuration''' ''' `skip_prk_steps`. `skip_prk_steps` should be set to True in the configuration file. Please make''' ''' sure to update the config accordingly as not setting `skip_prk_steps` in the config might lead to''' ''' incorrect results in future versions. If you have downloaded this checkpoint from the Hugging Face''' ''' Hub, it would be very nice if you could open a Pull request for the''' ''' `scheduler/scheduler_config.json` file''' ) deprecate('''skip_prk_steps not set''' , '''1.0.0''' , _a , standard_warn=_a ) __a = dict(scheduler.config ) __a = True __a = FrozenDict(_a ) if safety_checker is None: logger.warning( f'''You have disabled the safety checker for {self.__class__} by passing `safety_checker=None`. Ensure''' ''' that you abide to the conditions of the Stable Diffusion license and do not expose unfiltered''' ''' results in services or applications open to the public. Both the diffusers team and Hugging Face''' ''' strongly recommend to keep the safety filter enabled in all public facing circumstances, disabling''' ''' it only for use-cases that involve analyzing network behavior or auditing its results. For more''' ''' information, please have a look at https://github.com/huggingface/diffusers/pull/254 .''' ) self.register_modules( segmentation_model=_a , segmentation_processor=_a , vae=_a , text_encoder=_a , tokenizer=_a , unet=_a , scheduler=_a , safety_checker=_a , feature_extractor=_a , ) def __UpperCAmelCase ( self , _a = "auto" ): if slice_size == "auto": # half the attention head size is usually a good trade-off between # speed and memory __a = self.unet.config.attention_head_dim // 2 self.unet.set_attention_slice(_a ) def __UpperCAmelCase ( self ): self.enable_attention_slicing(_a ) def __UpperCAmelCase ( self ): if is_accelerate_available(): from accelerate import cpu_offload else: raise ImportError('''Please install accelerate via `pip install accelerate`''' ) __a = torch.device('''cuda''' ) for cpu_offloaded_model in [self.unet, self.text_encoder, self.vae, self.safety_checker]: if cpu_offloaded_model is not None: cpu_offload(_a , _a ) @property # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device def __UpperCAmelCase ( self ): if self.device != torch.device('''meta''' ) or not hasattr(self.unet , '''_hf_hook''' ): return self.device for module in self.unet.modules(): if ( hasattr(_a , '''_hf_hook''' ) and hasattr(module._hf_hook , '''execution_device''' ) and module._hf_hook.execution_device is not None ): return torch.device(module._hf_hook.execution_device ) return self.device @torch.no_grad() def __call__( self , _a , _a , _a , _a = 512 , _a = 512 , _a = 50 , _a = 7.5 , _a = None , _a = 1 , _a = 0.0 , _a = None , _a = None , _a = "pil" , _a = True , _a = None , _a = 1 , **_a , ): __a = self.segmentation_processor( text=[text] , images=[image] , padding='''max_length''' , return_tensors='''pt''' ).to(self.device ) __a = self.segmentation_model(**_a ) __a = torch.sigmoid(outputs.logits ).cpu().detach().unsqueeze(-1 ).numpy() __a = self.numpy_to_pil(_a )[0].resize(image.size ) # Run inpainting pipeline with the generated mask __a = StableDiffusionInpaintPipeline( vae=self.vae , text_encoder=self.text_encoder , tokenizer=self.tokenizer , unet=self.unet , scheduler=self.scheduler , safety_checker=self.safety_checker , feature_extractor=self.feature_extractor , ) return inpainting_pipeline( prompt=_a , image=_a , mask_image=_a , height=_a , width=_a , num_inference_steps=_a , guidance_scale=_a , negative_prompt=_a , num_images_per_prompt=_a , eta=_a , generator=_a , latents=_a , output_type=_a , return_dict=_a , callback=_a , callback_steps=_a , )
11
1
"""simple docstring""" from ....configuration_utils import PretrainedConfig from ....utils import logging __A = logging.get_logger(__name__) # TODO: upload to AWS __A = { '''yjernite/retribert-base-uncased''': ( '''https://huggingface.co/yjernite/retribert-base-uncased/resolve/main/config.json''' ), } class _snake_case ( a__ ): snake_case__ = "retribert" def __init__( self : int , UpperCAmelCase : List[str]=30522 , UpperCAmelCase : Tuple=768 , UpperCAmelCase : List[str]=8 , UpperCAmelCase : str=12 , UpperCAmelCase : List[Any]=3072 , UpperCAmelCase : Any="gelu" , UpperCAmelCase : Optional[Any]=0.1 , UpperCAmelCase : List[str]=0.1 , UpperCAmelCase : Optional[Any]=512 , UpperCAmelCase : int=2 , UpperCAmelCase : int=0.0_2 , UpperCAmelCase : Dict=1E-12 , UpperCAmelCase : Optional[int]=True , UpperCAmelCase : str=128 , UpperCAmelCase : Optional[Any]=0 , **UpperCAmelCase : Tuple , ): super().__init__(pad_token_id=UpperCAmelCase , **UpperCAmelCase ) __lowerCamelCase : int = vocab_size __lowerCamelCase : List[Any] = hidden_size __lowerCamelCase : Tuple = num_hidden_layers __lowerCamelCase : Optional[Any] = num_attention_heads __lowerCamelCase : Optional[int] = hidden_act __lowerCamelCase : Optional[int] = intermediate_size __lowerCamelCase : List[Any] = hidden_dropout_prob __lowerCamelCase : Union[str, Any] = attention_probs_dropout_prob __lowerCamelCase : str = max_position_embeddings __lowerCamelCase : List[Any] = type_vocab_size __lowerCamelCase : Any = initializer_range __lowerCamelCase : Dict = layer_norm_eps __lowerCamelCase : Optional[int] = share_encoders __lowerCamelCase : Optional[Any] = projection_dim
135
"""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, ) __A = '''hf-internal-testing/tiny-random-bert''' __A = os.path.join(TRANSFORMERS_CACHE, '''models--hf-internal-testing--tiny-random-bert''') __A = '''9b8c223d42b2188cb49d29af482996f9d0f3e5a6''' class _snake_case ( unittest.TestCase ): def lowerCamelCase__ ( self : Any ): __lowerCamelCase : Dict = cached_file(UpperCAmelCase , UpperCAmelCase ) # Should have downloaded the file in here self.assertTrue(os.path.isdir(UpperCAmelCase ) ) # Cache should contain at least those three subfolders: for subfolder in ["blobs", "refs", "snapshots"]: self.assertTrue(os.path.isdir(os.path.join(UpperCAmelCase , UpperCAmelCase ) ) ) with open(os.path.join(UpperCAmelCase , "refs" , "main" ) ) as f: __lowerCamelCase : Dict = f.read() self.assertEqual(UpperCAmelCase , os.path.join(UpperCAmelCase , "snapshots" , UpperCAmelCase , UpperCAmelCase ) ) self.assertTrue(os.path.isfile(UpperCAmelCase ) ) # File is cached at the same place the second time. __lowerCamelCase : Tuple = cached_file(UpperCAmelCase , UpperCAmelCase ) self.assertEqual(UpperCAmelCase , UpperCAmelCase ) # Using a specific revision to test the full commit hash. __lowerCamelCase : List[str] = cached_file(UpperCAmelCase , UpperCAmelCase , revision="9b8c223" ) self.assertEqual(UpperCAmelCase , os.path.join(UpperCAmelCase , "snapshots" , UpperCAmelCase , UpperCAmelCase ) ) def lowerCamelCase__ ( self : List[str] ): with self.assertRaisesRegex(UpperCAmelCase , "is not a valid model identifier" ): __lowerCamelCase : Optional[Any] = cached_file("tiny-random-bert" , UpperCAmelCase ) with self.assertRaisesRegex(UpperCAmelCase , "is not a valid git identifier" ): __lowerCamelCase : Dict = cached_file(UpperCAmelCase , UpperCAmelCase , revision="aaaa" ) with self.assertRaisesRegex(UpperCAmelCase , "does not appear to have a file named" ): __lowerCamelCase : List[Any] = cached_file(UpperCAmelCase , "conf" ) def lowerCamelCase__ ( self : str ): with self.assertRaisesRegex(UpperCAmelCase , "does not appear to have a file named" ): __lowerCamelCase : Any = cached_file(UpperCAmelCase , "conf" ) with open(os.path.join(UpperCAmelCase , "refs" , "main" ) ) as f: __lowerCamelCase : List[str] = f.read() self.assertTrue(os.path.isfile(os.path.join(UpperCAmelCase , ".no_exist" , UpperCAmelCase , "conf" ) ) ) __lowerCamelCase : List[str] = cached_file(UpperCAmelCase , "conf" , _raise_exceptions_for_missing_entries=UpperCAmelCase ) self.assertIsNone(UpperCAmelCase ) __lowerCamelCase : Optional[Any] = cached_file(UpperCAmelCase , "conf" , local_files_only=UpperCAmelCase , _raise_exceptions_for_missing_entries=UpperCAmelCase ) self.assertIsNone(UpperCAmelCase ) __lowerCamelCase : str = mock.Mock() __lowerCamelCase : Union[str, Any] = 500 __lowerCamelCase : Tuple = {} __lowerCamelCase : Dict = HTTPError __lowerCamelCase : Any = {} # Under the mock environment we get a 500 error when trying to reach the tokenizer. with mock.patch("requests.Session.request" , return_value=UpperCAmelCase ) as mock_head: __lowerCamelCase : Any = cached_file(UpperCAmelCase , "conf" , _raise_exceptions_for_connection_errors=UpperCAmelCase ) self.assertIsNone(UpperCAmelCase ) # This check we did call the fake head request mock_head.assert_called() def lowerCamelCase__ ( self : str ): self.assertTrue(has_file("hf-internal-testing/tiny-bert-pt-only" , UpperCAmelCase ) ) self.assertFalse(has_file("hf-internal-testing/tiny-bert-pt-only" , UpperCAmelCase ) ) self.assertFalse(has_file("hf-internal-testing/tiny-bert-pt-only" , UpperCAmelCase ) ) def lowerCamelCase__ ( 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(UpperCAmelCase , "is not a valid model identifier" ): get_file_from_repo("bert-base-case" , UpperCAmelCase ) # The function raises if the revision does not exist. with self.assertRaisesRegex(UpperCAmelCase , "is not a valid git identifier" ): get_file_from_repo("bert-base-cased" , UpperCAmelCase , revision="ahaha" ) __lowerCamelCase : str = get_file_from_repo("bert-base-cased" , UpperCAmelCase ) # The name is the cached name which is not very easy to test, so instead we load the content. __lowerCamelCase : Tuple = json.loads(open(UpperCAmelCase , "r" ).read() ) self.assertEqual(config["hidden_size"] , 768 ) def lowerCamelCase__ ( self : Any ): with tempfile.TemporaryDirectory() as tmp_dir: __lowerCamelCase : Union[str, Any] = Path(UpperCAmelCase ) / "a.txt" filename.touch() self.assertEqual(get_file_from_repo(UpperCAmelCase , "a.txt" ) , str(UpperCAmelCase ) ) self.assertIsNone(get_file_from_repo(UpperCAmelCase , "b.txt" ) )
135
1
'''simple docstring''' import argparse import logging import os import sys import numpy as np import onnxruntime import torch from bart_onnx.generation_onnx import BARTBeamSearchGenerator from bart_onnx.reduce_onnx_size import remove_dup_initializers import transformers from transformers import BartForConditionalGeneration, BartTokenizer logging.basicConfig( format="%(asctime)s | %(levelname)s | %(name)s | [%(filename)s:%(lineno)d] %(message)s", datefmt="%Y-%m-%d %H:%M:%S", level=os.environ.get("LOGLEVEL", "INFO").upper(), stream=sys.stdout, ) _SCREAMING_SNAKE_CASE : Dict = logging.getLogger(__name__) _SCREAMING_SNAKE_CASE : List[str] = {"facebook/bart-base": BartForConditionalGeneration} _SCREAMING_SNAKE_CASE : Union[str, Any] = {"facebook/bart-base": BartTokenizer} def UpperCamelCase_( ): '''simple docstring''' snake_case_ = argparse.ArgumentParser(description="Export Bart model + Beam Search to ONNX graph." ) parser.add_argument( "--validation_file" , type=snake_case , default=snake_case , help="A csv or a json file containing the validation data." ) parser.add_argument( "--max_length" , type=snake_case , default=5 , help="The maximum total input sequence length after tokenization." , ) parser.add_argument( "--num_beams" , type=snake_case , default=snake_case , help=( "Number of beams to use for evaluation. This argument will be " "passed to ``model.generate``, which is used during ``evaluate`` and ``predict``." ) , ) parser.add_argument( "--model_name_or_path" , type=snake_case , help="Path to pretrained model or model identifier from huggingface.co/models." , required=snake_case , ) parser.add_argument( "--config_name" , type=snake_case , default=snake_case , help="Pretrained config name or path if not the same as model_name" , ) parser.add_argument( "--device" , type=snake_case , default="cpu" , help="Device where the model will be run" , ) parser.add_argument("--output_file_path" , type=snake_case , default=snake_case , help="Where to store the final ONNX file." ) snake_case_ = parser.parse_args() return args def UpperCamelCase_( snake_case : Union[str, Any] , snake_case : List[str]="cpu" ): '''simple docstring''' snake_case_ = model_dict[model_name].from_pretrained(snake_case ).to(snake_case ) snake_case_ = tokenizer_dict[model_name].from_pretrained(snake_case ) if model_name in ["facebook/bart-base"]: snake_case_ = 0 snake_case_ = None snake_case_ = 0 return huggingface_model, tokenizer def UpperCamelCase_( snake_case : int , snake_case : Tuple , snake_case : Optional[int] , snake_case : Optional[Any] , snake_case : Optional[Any] ): '''simple docstring''' model.eval() snake_case_ = None snake_case_ = torch.jit.script(BARTBeamSearchGenerator(snake_case ) ) with torch.no_grad(): snake_case_ = "My friends are cool but they eat too many carbs." snake_case_ = tokenizer([ARTICLE_TO_SUMMARIZE] , max_length=1_0_2_4 , return_tensors="pt" ).to(model.device ) snake_case_ = model.generate( inputs["input_ids"] , attention_mask=inputs["attention_mask"] , num_beams=snake_case , max_length=snake_case , early_stopping=snake_case , decoder_start_token_id=model.config.decoder_start_token_id , ) torch.onnx.export( snake_case , ( inputs["input_ids"], inputs["attention_mask"], num_beams, max_length, model.config.decoder_start_token_id, ) , snake_case , opset_version=1_4 , input_names=["input_ids", "attention_mask", "num_beams", "max_length", "decoder_start_token_id"] , output_names=["output_ids"] , dynamic_axes={ "input_ids": {0: "batch", 1: "seq"}, "output_ids": {0: "batch", 1: "seq_out"}, } , example_outputs=snake_case , ) logger.info("Model exported to {}".format(snake_case ) ) snake_case_ = remove_dup_initializers(os.path.abspath(snake_case ) ) logger.info("Deduplicated and optimized model written to {}".format(snake_case ) ) snake_case_ = onnxruntime.InferenceSession(snake_case ) snake_case_ = ort_sess.run( snake_case , { "input_ids": inputs["input_ids"].cpu().numpy(), "attention_mask": inputs["attention_mask"].cpu().numpy(), "num_beams": np.array(snake_case ), "max_length": np.array(snake_case ), "decoder_start_token_id": np.array(model.config.decoder_start_token_id ), } , ) np.testing.assert_allclose(summary_ids.cpu().numpy() , ort_out[0] , rtol=1e-3 , atol=1e-3 ) logger.info("Model outputs from torch and ONNX Runtime are similar." ) logger.info("Success." ) def UpperCamelCase_( ): '''simple docstring''' snake_case_ = parse_args() snake_case_ = 5 snake_case_ = 4 # Make one log on every process with the configuration for debugging. logging.basicConfig( format="%(asctime)s - %(levelname)s - %(name)s - %(message)s" , datefmt="%m/%d/%Y %H:%M:%S" , level=logging.INFO , ) logger.setLevel(logging.INFO ) transformers.utils.logging.set_verbosity_error() snake_case_ = torch.device(args.device ) snake_case_ , snake_case_ = load_model_tokenizer(args.model_name_or_path , snake_case ) if model.config.decoder_start_token_id is None: raise ValueError("Make sure that `config.decoder_start_token_id` is correctly defined" ) model.to(snake_case ) if args.max_length: snake_case_ = args.max_length if args.num_beams: snake_case_ = args.num_beams if args.output_file_path: snake_case_ = args.output_file_path else: snake_case_ = "BART.onnx" logger.info("Exporting model to ONNX" ) export_and_validate_model(snake_case , snake_case , snake_case , snake_case , snake_case ) if __name__ == "__main__": main()
371
'''simple docstring''' import unittest import numpy as np import torch from diffusers import VersatileDiffusionImageVariationPipeline from diffusers.utils.testing_utils import load_image, require_torch_gpu, slow, torch_device _SCREAMING_SNAKE_CASE : Any = False class _snake_case ( unittest.TestCase ): pass @slow @require_torch_gpu class _snake_case ( unittest.TestCase ): def lowerCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' snake_case_ = VersatileDiffusionImageVariationPipeline.from_pretrained("shi-labs/versatile-diffusion" ) pipe.to(a__ ) pipe.set_progress_bar_config(disable=a__ ) snake_case_ = load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg" ) snake_case_ = torch.manual_seed(0 ) snake_case_ = pipe( image=a__ , generator=a__ , guidance_scale=7.5 , num_inference_steps=50 , output_type="numpy" , ).images snake_case_ = image[0, 253:256, 253:256, -1] assert image.shape == (1, 512, 512, 3) snake_case_ = np.array([0.0_4_4_1, 0.0_4_6_9, 0.0_5_0_7, 0.0_5_7_5, 0.0_6_3_2, 0.0_6_5_0, 0.0_8_6_5, 0.0_9_0_9, 0.0_9_4_5] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
92
0
"""simple docstring""" import argparse import os import re import packaging.version A : Any = "examples/" A : Optional[Any] = { "examples": (re.compile(R"^check_min_version\(\"[^\"]+\"\)\s*$", re.MULTILINE), "check_min_version(\"VERSION\")\n"), "init": (re.compile(R"^__version__\s+=\s+\"([^\"]+)\"\s*$", re.MULTILINE), "__version__ = \"VERSION\"\n"), "setup": (re.compile(R"^(\s*)version\s*=\s*\"[^\"]+\",", re.MULTILINE), R"\1version=\"VERSION\","), "doc": (re.compile(R"^(\s*)release\s*=\s*\"[^\"]+\"$", re.MULTILINE), "release = \"VERSION\"\n"), } A : Optional[int] = { "init": "src/transformers/__init__.py", "setup": "setup.py", } A : List[Any] = "README.md" def _lowerCamelCase ( _UpperCamelCase , _UpperCamelCase , _UpperCamelCase ): '''simple docstring''' with open(_UpperCamelCase , "r" , encoding="utf-8" , newline="\n" ) as f: __lowerCAmelCase = f.read() __lowerCAmelCase , __lowerCAmelCase = REPLACE_PATTERNS[pattern] __lowerCAmelCase = replace.replace("VERSION" , _UpperCamelCase ) __lowerCAmelCase = re_pattern.sub(_UpperCamelCase , _UpperCamelCase ) with open(_UpperCamelCase , "w" , encoding="utf-8" , newline="\n" ) as f: f.write(_UpperCamelCase ) def _lowerCamelCase ( _UpperCamelCase ): '''simple docstring''' for folder, directories, fnames in os.walk(_UpperCamelCase ): # Removing some of the folders with non-actively maintained examples from the walk if "research_projects" in directories: directories.remove("research_projects" ) if "legacy" in directories: directories.remove("legacy" ) for fname in fnames: if fname.endswith(".py" ): update_version_in_file(os.path.join(_UpperCamelCase , _UpperCamelCase ) , _UpperCamelCase , pattern="examples" ) def _lowerCamelCase ( _UpperCamelCase , _UpperCamelCase=False ): '''simple docstring''' for pattern, fname in REPLACE_FILES.items(): update_version_in_file(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) if not patch: update_version_in_examples(_UpperCamelCase ) def _lowerCamelCase ( ): '''simple docstring''' __lowerCAmelCase = "🤗 Transformers currently provides the following architectures" __lowerCAmelCase = "1. Want to contribute a new model?" with open(_UpperCamelCase , "r" , encoding="utf-8" , newline="\n" ) as f: __lowerCAmelCase = f.readlines() # Find the start of the list. __lowerCAmelCase = 0 while not lines[start_index].startswith(_start_prompt ): start_index += 1 start_index += 1 __lowerCAmelCase = start_index # Update the lines in the model list. while not lines[index].startswith(_end_prompt ): if lines[index].startswith("1." ): __lowerCAmelCase = lines[index].replace( "https://huggingface.co/docs/transformers/main/model_doc" , "https://huggingface.co/docs/transformers/model_doc" , ) index += 1 with open(_UpperCamelCase , "w" , encoding="utf-8" , newline="\n" ) as f: f.writelines(_UpperCamelCase ) def _lowerCamelCase ( ): '''simple docstring''' with open(REPLACE_FILES["init"] , "r" ) as f: __lowerCAmelCase = f.read() __lowerCAmelCase = REPLACE_PATTERNS["init"][0].search(_UpperCamelCase ).groups()[0] return packaging.version.parse(_UpperCamelCase ) def _lowerCamelCase ( _UpperCamelCase=False ): '''simple docstring''' __lowerCAmelCase = get_version() if patch and default_version.is_devrelease: raise ValueError("Can't create a patch version from the dev branch, checkout a released version!" ) if default_version.is_devrelease: __lowerCAmelCase = default_version.base_version elif patch: __lowerCAmelCase = f"{default_version.major}.{default_version.minor}.{default_version.micro + 1}" else: __lowerCAmelCase = f"{default_version.major}.{default_version.minor + 1}.0" # Now let's ask nicely if that's the right one. __lowerCAmelCase = input(f"Which version are you releasing? [{default_version}]" ) if len(_UpperCamelCase ) == 0: __lowerCAmelCase = default_version print(f"Updating version to {version}." ) global_version_update(_UpperCamelCase , patch=_UpperCamelCase ) if not patch: print("Cleaning main README, don't forget to run `make fix-copies`." ) clean_main_ref_in_model_list() def _lowerCamelCase ( ): '''simple docstring''' __lowerCAmelCase = get_version() __lowerCAmelCase = f"{current_version.major}.{current_version.minor + 1}.0.dev0" __lowerCAmelCase = current_version.base_version # Check with the user we got that right. __lowerCAmelCase = input(f"Which version are we developing now? [{dev_version}]" ) if len(_UpperCamelCase ) == 0: __lowerCAmelCase = dev_version print(f"Updating version to {version}." ) global_version_update(_UpperCamelCase ) print("Cleaning main README, don't forget to run `make fix-copies`." ) clean_main_ref_in_model_list() if __name__ == "__main__": A : Union[str, Any] = argparse.ArgumentParser() parser.add_argument("--post_release", action="store_true", help="Whether this is pre or post release.") parser.add_argument("--patch", action="store_true", help="Whether or not this is a patch release.") A : Dict = parser.parse_args() if not args.post_release: pre_release_work(patch=args.patch) elif args.patch: print("Nothing to do after a patch :-)") else: post_release_work()
57
import unittest from transformers import is_torch_available from transformers.testing_utils import require_torch if is_torch_available(): import torch from transformers.activations import gelu_new, gelu_python, get_activation @require_torch class A__ ( unittest.TestCase ): """simple docstring""" def a_ ( self ): snake_case = torch.tensor([-1_0_0, -1, -0.1, 0, 0.1, 1.0, 1_0_0] ) snake_case = get_activation('''gelu''' ) self.assertTrue(torch.allclose(gelu_python(__snake_case ) , torch_builtin(__snake_case ) ) ) self.assertFalse(torch.allclose(gelu_python(__snake_case ) , gelu_new(__snake_case ) ) ) def a_ ( self ): snake_case = torch.tensor([-1_0_0, -1, -0.1, 0, 0.1, 1.0, 1_0_0] ) snake_case = get_activation('''gelu''' ) snake_case = get_activation('''gelu_10''' ) snake_case = torch_builtin(__snake_case ) snake_case = geluaa(__snake_case ) snake_case = torch.where(y_gelu_aa < 10.0 , 1 , 0 ) self.assertTrue(torch.max(__snake_case ).item() == 10.0 ) self.assertTrue(torch.allclose(y_gelu * clipped_mask , y_gelu_aa * clipped_mask ) ) def a_ ( self ): get_activation('''gelu''' ) get_activation('''gelu_10''' ) get_activation('''gelu_fast''' ) get_activation('''gelu_new''' ) get_activation('''gelu_python''' ) get_activation('''gelu_pytorch_tanh''' ) get_activation('''linear''' ) get_activation('''mish''' ) get_activation('''quick_gelu''' ) get_activation('''relu''' ) get_activation('''sigmoid''' ) get_activation('''silu''' ) get_activation('''swish''' ) get_activation('''tanh''' ) with self.assertRaises(__snake_case ): get_activation('''bogus''' ) with self.assertRaises(__snake_case ): get_activation(__snake_case ) def a_ ( self ): snake_case = get_activation('''gelu''' ) snake_case = 1 snake_case = get_activation('''gelu''' ) self.assertEqual(acta.a , 1 ) with self.assertRaises(__snake_case ): snake_case = acta.a
127
0
'''simple docstring''' import math import time from transformers import Trainer, is_torch_tpu_available from transformers.trainer_utils import PredictionOutput, speed_metrics if is_torch_tpu_available(check_device=False): import torch_xla.core.xla_model as xm import torch_xla.debug.metrics as met class a__( lowerCamelCase__ ): def __init__( self : Tuple , *__snake_case : Tuple , __snake_case : Any=None , __snake_case : Optional[int]=None , **__snake_case : Dict ): super().__init__(*__snake_case , **__snake_case ) a : Optional[int] = eval_examples a : str = post_process_function def lowercase_ ( self : Union[str, Any] , __snake_case : Tuple=None , __snake_case : Any=None , __snake_case : int=None , __snake_case : str = "eval" ): a : Union[str, Any] = self.eval_dataset if eval_dataset is None else eval_dataset a : str = self.get_eval_dataloader(__snake_case ) a : int = self.eval_examples if eval_examples is None else eval_examples # Temporarily disable metric computation, we will do it in the loop here. a : Any = self.compute_metrics a : List[Any] = None a : Tuple = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop a : Optional[int] = time.time() try: a : List[Any] = eval_loop( __snake_case , description='Evaluation' , prediction_loss_only=True if compute_metrics is None else None , ignore_keys=__snake_case , metric_key_prefix=__snake_case , ) finally: a : int = compute_metrics a : str = self.args.eval_batch_size * self.args.world_size if F"""{metric_key_prefix}_jit_compilation_time""" in output.metrics: start_time += output.metrics[F"""{metric_key_prefix}_jit_compilation_time"""] output.metrics.update( speed_metrics( __snake_case , __snake_case , num_samples=output.num_samples , num_steps=math.ceil(output.num_samples / total_batch_size ) , ) ) if self.post_process_function is not None and self.compute_metrics is not None and self.args.should_save: # Only the main node write the results by default a : str = self.post_process_function(__snake_case , __snake_case , output.predictions ) a : Optional[int] = self.compute_metrics(__snake_case ) # Prefix all keys with metric_key_prefix + '_' for key in list(metrics.keys() ): if not key.startswith(F"""{metric_key_prefix}_""" ): a : Optional[int] = metrics.pop(__snake_case ) metrics.update(output.metrics ) else: a : str = output.metrics if self.args.should_log: # Only the main node log the results by default self.log(__snake_case ) if self.args.tpu_metrics_debug or self.args.debug: # tpu-comment: Logging debug metrics for PyTorch/XLA (compile, execute times, ops, etc.) xm.master_print(met.metrics_report() ) a : List[Any] = self.callback_handler.on_evaluate(self.args , self.state , self.control , __snake_case ) return metrics def lowercase_ ( self : Union[str, Any] , __snake_case : Dict , __snake_case : List[Any] , __snake_case : int=None , __snake_case : str = "test" ): a : str = self.get_test_dataloader(__snake_case ) # Temporarily disable metric computation, we will do it in the loop here. a : Tuple = self.compute_metrics a : Tuple = None a : Any = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop a : Optional[Any] = time.time() try: a : Any = eval_loop( __snake_case , description='Prediction' , prediction_loss_only=True if compute_metrics is None else None , ignore_keys=__snake_case , metric_key_prefix=__snake_case , ) finally: a : int = compute_metrics a : str = self.args.eval_batch_size * self.args.world_size if F"""{metric_key_prefix}_jit_compilation_time""" in output.metrics: start_time += output.metrics[F"""{metric_key_prefix}_jit_compilation_time"""] output.metrics.update( speed_metrics( __snake_case , __snake_case , num_samples=output.num_samples , num_steps=math.ceil(output.num_samples / total_batch_size ) , ) ) if self.post_process_function is None or self.compute_metrics is None: return output a : Dict = self.post_process_function(__snake_case , __snake_case , output.predictions , 'predict' ) a : Optional[int] = self.compute_metrics(__snake_case ) # Prefix all keys with metric_key_prefix + '_' for key in list(metrics.keys() ): if not key.startswith(F"""{metric_key_prefix}_""" ): a : Union[str, Any] = metrics.pop(__snake_case ) metrics.update(output.metrics ) return PredictionOutput(predictions=predictions.predictions , label_ids=predictions.label_ids , metrics=__snake_case )
96
'''simple docstring''' # Copyright (c) 2021-, NVIDIA CORPORATION. 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. #################################################################################################### # # Note: If when running this conversion script you're getting an exception: # ModuleNotFoundError: No module named 'megatron.model.enums' # you need to tell python where to find the clone of Megatron-LM, e.g.: # # cd /tmp # git clone https://github.com/NVIDIA/Megatron-LM # PYTHONPATH=/tmp/Megatron-LM python src/transformers/models/megatron_gpt2/convert_megatron_gpt2_checkpoint.py ... # # if you already have it cloned elsewhere, simply adjust the path to the existing path # # If the training was done using a Megatron-LM fork, e.g., # https://github.com/microsoft/Megatron-DeepSpeed/ then chances are that you need to have that one # in your path, i.e., /path/to/Megatron-DeepSpeed/ # import argparse import os import re import zipfile import torch from transformers import AutoTokenizer, GPTaConfig def lowerCamelCase__ ( _A , _A , _A=0 ): # Format the message. if name is None: a : Tuple = None else: a : Dict = '.' * max(0 , spaces - 2 ) + '# {:' + str(50 - spaces ) + 's}' a : Tuple = fmt.format(_A ) # Print and recurse (if needed). if isinstance(_A , _A ): if msg is not None: print(_A ) for k in val.keys(): recursive_print(_A , val[k] , spaces + 2 ) elif isinstance(_A , torch.Tensor ): print(_A , ':' , val.size() ) else: print(_A , ':' , _A ) def lowerCamelCase__ ( _A , _A , _A , _A , _A ): # Permutes layout of param tensor to [num_splits * num_heads * hidden_size, :] # for compatibility with later versions of NVIDIA Megatron-LM. # The inverse operation is performed inside Megatron-LM to read checkpoints: # https://github.com/NVIDIA/Megatron-LM/blob/v2.4/megatron/checkpointing.py#L209 # If param is the weight tensor of the self-attention block, the returned tensor # will have to be transposed one more time to be read by HuggingFace GPT2. a : str = param.size() if checkpoint_version == 1.0: # version 1.0 stores [num_heads * hidden_size * num_splits, :] a : List[Any] = (num_heads, hidden_size, num_splits) + input_shape[1:] a : int = param.view(*_A ) a : List[str] = param.transpose(0 , 2 ) a : Union[str, Any] = param.transpose(1 , 2 ).contiguous() elif checkpoint_version >= 2.0: # other versions store [num_heads * num_splits * hidden_size, :] a : Union[str, Any] = (num_heads, num_splits, hidden_size) + input_shape[1:] a : List[str] = param.view(*_A ) a : Union[str, Any] = param.transpose(0 , 1 ).contiguous() a : List[Any] = param.view(*_A ) return param def lowerCamelCase__ ( _A , _A , _A ): # The converted output model. a : Optional[Any] = {} # old versions did not store training args a : Dict = input_state_dict.get('args' , _A ) if ds_args is not None: # do not make the user write a config file when the exact dimensions/sizes are already in the checkpoint # from pprint import pprint # pprint(vars(ds_args)) a : Union[str, Any] = ds_args.padded_vocab_size a : str = ds_args.max_position_embeddings a : Dict = ds_args.hidden_size a : Union[str, Any] = ds_args.num_layers a : Dict = ds_args.num_attention_heads a : int = ds_args.ffn_hidden_size # pprint(config) # The number of heads. a : Any = config.n_head # The hidden_size per head. a : Tuple = config.n_embd // config.n_head # Megatron-LM checkpoint version if "checkpoint_version" in input_state_dict.keys(): a : Any = input_state_dict['checkpoint_version'] else: a : Any = 0.0 # The model. a : Optional[int] = input_state_dict['model'] # The language model. a : Optional[Any] = model['language_model'] # The embeddings. a : List[str] = lm['embedding'] # The word embeddings. a : List[Any] = embeddings['word_embeddings']['weight'] # Truncate the embedding table to vocab_size rows. a : Dict = word_embeddings[: config.vocab_size, :] a : int = word_embeddings # The position embeddings. a : Tuple = embeddings['position_embeddings']['weight'] # Read the causal mask dimension (seqlen). [max_sequence_length, hidden_size] a : List[str] = pos_embeddings.size(0 ) if n_positions != config.n_positions: raise ValueError( f"""pos_embeddings.max_sequence_length={n_positions} and config.n_positions={config.n_positions} don't match""" ) # Store the position embeddings. a : Optional[Any] = pos_embeddings # The transformer. a : Union[str, Any] = lm['transformer'] if 'transformer' in lm.keys() else lm['encoder'] # The regex to extract layer names. a : List[Any] = re.compile(r'layers\.(\d+)\.([a-z0-9_.]+)\.([a-z]+)' ) # The simple map of names for "automated" rules. a : Optional[Any] = { 'attention.dense': '.attn.c_proj.', 'self_attention.dense': '.attn.c_proj.', 'mlp.dense_h_to_4h': '.mlp.c_fc.', 'mlp.dense_4h_to_h': '.mlp.c_proj.', } # Extract the layers. for key, val in transformer.items(): # Match the name. a : Tuple = layer_re.match(_A ) # Stop if that's not a layer if m is None: break # The index of the layer. a : Union[str, Any] = int(m.group(1 ) ) # The name of the operation. a : Optional[int] = m.group(2 ) # Is it a weight or a bias? a : Optional[int] = m.group(3 ) # The name of the layer. a : Any = f"""transformer.h.{layer_idx}""" # For layernorm(s), simply store the layer norm. if op_name.endswith('layernorm' ): a : str = 'ln_1' if op_name.startswith('input' ) else 'ln_2' a : Tuple = val # Transpose the QKV matrix. elif ( op_name == "attention.query_key_value" or op_name == "self_attention.query_key_value" ) and weight_or_bias == "weight": # Insert a tensor of 1x1xDxD bias. a : Dict = torch.tril(torch.ones((n_positions, n_positions) , dtype=torch.floataa ) ).view( 1 , 1 , _A , _A ) a : Optional[Any] = causal_mask # Insert a "dummy" tensor for masked_bias. a : List[Any] = torch.tensor(-1E4 , dtype=torch.floataa ) a : List[str] = masked_bias a : Union[str, Any] = fix_query_key_value_ordering(_A , _A , 3 , _A , _A ) # Megatron stores (3*D) x D but transformers-GPT2 expects D x 3*D. a : int = out_val.transpose(0 , 1 ).contiguous() # Store. a : int = out_val # Transpose the bias. elif ( op_name == "attention.query_key_value" or op_name == "self_attention.query_key_value" ) and weight_or_bias == "bias": a : str = fix_query_key_value_ordering(_A , _A , 3 , _A , _A ) # Store. No change of shape. a : List[str] = out_val # Transpose the weights. elif weight_or_bias == "weight": a : Tuple = megatron_to_transformers[op_name] a : List[str] = val.transpose(0 , 1 ) # Copy the bias. elif weight_or_bias == "bias": a : Dict = megatron_to_transformers[op_name] a : Optional[Any] = val # DEBUG. assert config.n_layer == layer_idx + 1 # The final layernorm. a : str = transformer['final_layernorm.weight'] a : List[str] = transformer['final_layernorm.bias'] # For LM head, transformers' wants the matrix to weight embeddings. a : Optional[int] = word_embeddings # It should be done! return output_state_dict def lowerCamelCase__ ( ): # Create the argument parser. a : Dict = argparse.ArgumentParser() parser.add_argument('--print-checkpoint-structure' , action='store_true' ) parser.add_argument( 'path_to_checkpoint' , type=_A , help='Path to the checkpoint file (.zip archive or direct .pt file)' , ) parser.add_argument( '--config_file' , default='' , type=_A , help='An optional config json file describing the pre-trained model.' , ) a : Union[str, Any] = parser.parse_args() # Extract the basename. a : Optional[Any] = os.path.dirname(args.path_to_checkpoint ) # Load the model. # the .zip is very optional, let's keep it for backward compatibility print(f"""Extracting PyTorch state dictionary from {args.path_to_checkpoint}""" ) if args.path_to_checkpoint.endswith('.zip' ): with zipfile.ZipFile(args.path_to_checkpoint , 'r' ) as checkpoint: with checkpoint.open('release/mp_rank_00/model_optim_rng.pt' ) as pytorch_dict: a : Union[str, Any] = torch.load(_A , map_location='cpu' ) else: a : Any = torch.load(args.path_to_checkpoint , map_location='cpu' ) a : List[Any] = input_state_dict.get('args' , _A ) # Read the config, or default to the model released by NVIDIA. if args.config_file == "": if ds_args is not None: if ds_args.bias_gelu_fusion: a : int = 'gelu_fast' elif ds_args.openai_gelu: a : Dict = 'gelu_new' else: a : Any = 'gelu' else: # in the very early days this used to be "gelu_new" a : Any = 'gelu_new' # Spell out all parameters in case the defaults change. a : Tuple = GPTaConfig( vocab_size=5_0257 , n_positions=1024 , n_embd=1024 , n_layer=24 , n_head=16 , n_inner=4096 , activation_function=_A , resid_pdrop=0.1 , embd_pdrop=0.1 , attn_pdrop=0.1 , layer_norm_epsilon=1E-5 , initializer_range=0.02 , summary_type='cls_index' , summary_use_proj=_A , summary_activation=_A , summary_proj_to_labels=_A , summary_first_dropout=0.1 , scale_attn_weights=_A , use_cache=_A , bos_token_id=5_0256 , eos_token_id=5_0256 , ) else: a : str = GPTaConfig.from_json_file(args.config_file ) a : Any = ['GPT2LMHeadModel'] # Convert. print('Converting' ) a : Union[str, Any] = convert_megatron_checkpoint(_A , _A , _A ) # Print the structure of converted state dict. if args.print_checkpoint_structure: recursive_print(_A , _A ) # Add tokenizer class info to config # see https://github.com/huggingface/transformers/issues/13906) if ds_args is not None: a : Union[str, Any] = ds_args.tokenizer_type if tokenizer_type == "GPT2BPETokenizer": a : Tuple = 'gpt2' elif tokenizer_type == "PretrainedFromHF": a : List[str] = ds_args.tokenizer_name_or_path else: raise ValueError(f"""Unrecognized tokenizer_type {tokenizer_type}""" ) else: a : Optional[Any] = 'gpt2' a : Tuple = AutoTokenizer.from_pretrained(_A ) a : str = type(_A ).__name__ a : List[str] = tokenizer_class # Store the config to file. print('Saving config' ) config.save_pretrained(_A ) # Save tokenizer based on args print(f"""Adding {tokenizer_class} tokenizer files""" ) tokenizer.save_pretrained(_A ) # Store the state_dict to file. a : Optional[int] = os.path.join(_A , 'pytorch_model.bin' ) print(f"""Saving checkpoint to \"{output_checkpoint_file}\"""" ) torch.save(_A , _A ) #################################################################################################### if __name__ == "__main__": main() ####################################################################################################
96
1
"""simple docstring""" import numpy as np import datasets snake_case__ : Dict = ''' Compute the Mahalanobis Distance Mahalonobis distance is the distance between a point and a distribution. And not between two distinct points. It is effectively a multivariate equivalent of the Euclidean distance. It was introduced by Prof. P. C. Mahalanobis in 1936 and has been used in various statistical applications ever since [source: https://www.machinelearningplus.com/statistics/mahalanobis-distance/] ''' snake_case__ : Union[str, Any] = '''\ @article{de2000mahalanobis, title={The mahalanobis distance}, author={De Maesschalck, Roy and Jouan-Rimbaud, Delphine and Massart, D{\'e}sir{\'e} L}, journal={Chemometrics and intelligent laboratory systems}, volume={50}, number={1}, pages={1--18}, year={2000}, publisher={Elsevier} } ''' snake_case__ : Any = ''' Args: X: List of datapoints to be compared with the `reference_distribution`. reference_distribution: List of datapoints from the reference distribution we want to compare to. Returns: mahalanobis: The Mahalonobis distance for each datapoint in `X`. Examples: >>> mahalanobis_metric = datasets.load_metric("mahalanobis") >>> results = mahalanobis_metric.compute(reference_distribution=[[0, 1], [1, 0]], X=[[0, 1]]) >>> print(results) {\'mahalanobis\': array([0.5])} ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class snake_case_( datasets.Metric ): def lowerCamelCase__ ( self : List[str] ): return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''X''': datasets.Sequence(datasets.Value('''float''' , id='''sequence''' ) , id='''X''' ), } ) , ) def lowerCamelCase__ ( self : Optional[int] , UpperCamelCase_ : Optional[int] , UpperCamelCase_ : int ): # convert to numpy arrays lowerCAmelCase : List[str] = np.array(UpperCamelCase_ ) lowerCAmelCase : Optional[Any] = np.array(UpperCamelCase_ ) # Assert that arrays are 2D if len(X.shape ) != 2: raise ValueError('''Expected `X` to be a 2D vector''' ) if len(reference_distribution.shape ) != 2: raise ValueError('''Expected `reference_distribution` to be a 2D vector''' ) if reference_distribution.shape[0] < 2: raise ValueError( '''Expected `reference_distribution` to be a 2D vector with more than one element in the first dimension''' ) # Get mahalanobis distance for each prediction lowerCAmelCase : List[Any] = X - np.mean(UpperCamelCase_ ) lowerCAmelCase : Union[str, Any] = np.cov(reference_distribution.T ) try: lowerCAmelCase : Dict = np.linalg.inv(UpperCamelCase_ ) except np.linalg.LinAlgError: lowerCAmelCase : List[str] = np.linalg.pinv(UpperCamelCase_ ) lowerCAmelCase : Optional[int] = np.dot(UpperCamelCase_ , UpperCamelCase_ ) lowerCAmelCase : Optional[Any] = np.dot(UpperCamelCase_ , X_minus_mu.T ).diagonal() return {"mahalanobis": mahal_dist}
60
# Lint as: python3 import os import re import urllib.parse from pathlib import Path from typing import Callable, List, Optional, Union from zipfile import ZipFile from ..utils.file_utils import cached_path, hf_github_url from ..utils.logging import get_logger from ..utils.version import Version UpperCAmelCase_ = get_logger(__name__) class lowerCamelCase__: UpperCAmelCase__ : List[Any] = 'dummy_data' UpperCAmelCase__ : str = 'datasets' UpperCAmelCase__ : Tuple = False def __init__( self: Optional[Any] , UpperCamelCase_: str , UpperCamelCase_: str , UpperCamelCase_: Union[Version, str] , UpperCamelCase_: Optional[str] = None , UpperCamelCase_: bool = False , UpperCamelCase_: bool = True , UpperCamelCase_: Optional[List[Callable]] = None , ): __lowerCamelCase = 0 __lowerCamelCase = dataset_name __lowerCamelCase = cache_dir __lowerCamelCase = use_local_dummy_data __lowerCamelCase = config # download_callbacks take a single url as input __lowerCamelCase = download_callbacks or [] # if False, it doesn't load existing files and it returns the paths of the dummy files relative # to the dummy_data zip file root __lowerCamelCase = load_existing_dummy_data # TODO(PVP, QL) might need to make this more general __lowerCamelCase = str(UpperCamelCase_ ) # to be downloaded __lowerCamelCase = None __lowerCamelCase = None @property def lowerCAmelCase__ ( self: List[Any] ): if self._dummy_file is None: __lowerCamelCase = self.download_dummy_data() return self._dummy_file @property def lowerCAmelCase__ ( self: str ): if self.config is not None: # structure is dummy / config_name / version_name return os.path.join("""dummy""" , self.config.name , self.version_name ) # structure is dummy / version_name return os.path.join("""dummy""" , self.version_name ) @property def lowerCAmelCase__ ( self: Optional[Any] ): return os.path.join(self.dummy_data_folder , """dummy_data.zip""" ) def lowerCAmelCase__ ( self: Any ): __lowerCamelCase = ( self.local_path_to_dummy_data if self.use_local_dummy_data is True else self.github_path_to_dummy_data ) __lowerCamelCase = cached_path( UpperCamelCase_ , cache_dir=self.cache_dir , extract_compressed_file=UpperCamelCase_ , force_extract=UpperCamelCase_ ) return os.path.join(UpperCamelCase_ , self.dummy_file_name ) @property def lowerCAmelCase__ ( self: Optional[Any] ): return os.path.join(self.datasets_scripts_dir , self.dataset_name , self.dummy_zip_file ) @property def lowerCAmelCase__ ( self: Tuple ): if self._bucket_url is None: __lowerCamelCase = hf_github_url(self.dataset_name , self.dummy_zip_file.replace(os.sep , """/""" ) ) return self._bucket_url @property def lowerCAmelCase__ ( self: str ): # return full path if its a dir if os.path.isdir(self.dummy_file ): return self.dummy_file # else cut off path to file -> example `xsum`. return "/".join(self.dummy_file.replace(os.sep , """/""" ).split("""/""" )[:-1] ) def lowerCAmelCase__ ( self: List[str] , UpperCamelCase_: Dict , *UpperCamelCase_: str ): if self.load_existing_dummy_data: # dummy data is downloaded and tested __lowerCamelCase = self.dummy_file else: # dummy data cannot be downloaded and only the path to dummy file is returned __lowerCamelCase = self.dummy_file_name # special case when data_url is a dict if isinstance(UpperCamelCase_ , UpperCamelCase_ ): return self.create_dummy_data_dict(UpperCamelCase_ , UpperCamelCase_ ) elif isinstance(UpperCamelCase_ , (list, tuple) ): return self.create_dummy_data_list(UpperCamelCase_ , UpperCamelCase_ ) else: return self.create_dummy_data_single(UpperCamelCase_ , UpperCamelCase_ ) def lowerCAmelCase__ ( self: Any , UpperCamelCase_: Optional[Any] , *UpperCamelCase_: str ): return self.download_and_extract(UpperCamelCase_ ) def lowerCAmelCase__ ( self: List[Any] , UpperCamelCase_: Tuple , UpperCamelCase_: str ): return self.download_and_extract(UpperCamelCase_ ) def lowerCAmelCase__ ( self: str , UpperCamelCase_: int , *UpperCamelCase_: List[str] , **UpperCamelCase_: str ): return path def lowerCAmelCase__ ( self: Dict ): return {} def lowerCAmelCase__ ( self: str , UpperCamelCase_: List[Any] , UpperCamelCase_: Union[str, Any] ): __lowerCamelCase = {} for key, single_urls in data_url.items(): for download_callback in self.download_callbacks: if isinstance(UpperCamelCase_ , UpperCamelCase_ ): for single_url in single_urls: download_callback(UpperCamelCase_ ) else: __lowerCamelCase = single_urls download_callback(UpperCamelCase_ ) # we force the name of each key to be the last file / folder name of the url path # if the url has arguments, we need to encode them with urllib.parse.quote_plus if isinstance(UpperCamelCase_ , UpperCamelCase_ ): __lowerCamelCase = [os.path.join(UpperCamelCase_ , urllib.parse.quote_plus(Path(UpperCamelCase_ ).name ) ) for x in single_urls] else: __lowerCamelCase = single_urls __lowerCamelCase = os.path.join(UpperCamelCase_ , urllib.parse.quote_plus(Path(UpperCamelCase_ ).name ) ) __lowerCamelCase = value # make sure that values are unique if all(isinstance(UpperCamelCase_ , UpperCamelCase_ ) for i in dummy_data_dict.values() ) and len(set(dummy_data_dict.values() ) ) < len( dummy_data_dict.values() ): # append key to value to make its name unique __lowerCamelCase = {key: value + key for key, value in dummy_data_dict.items()} return dummy_data_dict def lowerCAmelCase__ ( self: Optional[Any] , UpperCamelCase_: Optional[Any] , UpperCamelCase_: Optional[int] ): __lowerCamelCase = [] # trick: if there are many shards named like `data.txt-000001-of-00300`, only use the first one __lowerCamelCase = all(bool(re.findall("""[0-9]{3,}-of-[0-9]{3,}""" , UpperCamelCase_ ) ) for url in data_url ) __lowerCamelCase = all( url.startswith("""https://ftp.ncbi.nlm.nih.gov/pubmed/baseline/pubmed""" ) for url in data_url ) if data_url and (is_tf_records or is_pubmed_records): __lowerCamelCase = [data_url[0]] * len(UpperCamelCase_ ) for single_url in data_url: for download_callback in self.download_callbacks: download_callback(UpperCamelCase_ ) # we force the name of each key to be the last file / folder name of the url path # if the url has arguments, we need to encode them with urllib.parse.quote_plus __lowerCamelCase = os.path.join(UpperCamelCase_ , urllib.parse.quote_plus(single_url.split("""/""" )[-1] ) ) dummy_data_list.append(UpperCamelCase_ ) return dummy_data_list def lowerCAmelCase__ ( self: Tuple , UpperCamelCase_: Optional[int] , UpperCamelCase_: Optional[Any] ): for download_callback in self.download_callbacks: download_callback(UpperCamelCase_ ) # we force the name of each key to be the last file / folder name of the url path # if the url has arguments, we need to encode them with urllib.parse.quote_plus __lowerCamelCase = os.path.join(UpperCamelCase_ , urllib.parse.quote_plus(data_url.split("""/""" )[-1] ) ) if os.path.exists(UpperCamelCase_ ) or not self.load_existing_dummy_data: return value else: # Backward compatibility, maybe deprecate at one point. # For many datasets with single url calls to dl_manager.download_and_extract, # the dummy_data.zip file is actually the zipped downloaded file # while now we expected the dummy_data.zip file to be a directory containing # the downloaded file. return path_to_dummy_data def lowerCAmelCase__ ( self: Optional[Any] ): pass def lowerCAmelCase__ ( self: List[Any] ): pass def lowerCAmelCase__ ( self: Any , UpperCamelCase_: Dict ): def _iter_archive_members(UpperCamelCase_: Any ): # this preserves the order of the members inside the ZIP archive __lowerCamelCase = Path(self.dummy_file ).parent __lowerCamelCase = path.relative_to(UpperCamelCase_ ) with ZipFile(self.local_path_to_dummy_data ) as zip_file: __lowerCamelCase = zip_file.namelist() for member in members: if member.startswith(relative_path.as_posix() ): yield dummy_parent_path.joinpath(UpperCamelCase_ ) __lowerCamelCase = Path(UpperCamelCase_ ) __lowerCamelCase = _iter_archive_members(UpperCamelCase_ ) if self.use_local_dummy_data else path.rglob("""*""" ) for file_path in file_paths: if file_path.is_file() and not file_path.name.startswith((""".""", """__""") ): yield file_path.relative_to(UpperCamelCase_ ).as_posix(), file_path.open("""rb""" ) def lowerCAmelCase__ ( self: List[str] , UpperCamelCase_: Dict ): if not isinstance(UpperCamelCase_ , UpperCamelCase_ ): __lowerCamelCase = [paths] for path in paths: if os.path.isfile(UpperCamelCase_ ): if os.path.basename(UpperCamelCase_ ).startswith((""".""", """__""") ): return yield path else: for dirpath, dirnames, filenames in os.walk(UpperCamelCase_ ): if os.path.basename(UpperCamelCase_ ).startswith((""".""", """__""") ): continue dirnames.sort() for filename in sorted(UpperCamelCase_ ): if filename.startswith((""".""", """__""") ): continue yield os.path.join(UpperCamelCase_ , UpperCamelCase_ )
12
0
import argparse import requests import torch from PIL import Image from torchvision.transforms import Compose, Normalize, Resize, ToTensor from transformers import SwinaSRConfig, SwinaSRForImageSuperResolution, SwinaSRImageProcessor def _a ( SCREAMING_SNAKE_CASE_ : str ): __lowerCAmelCase = SwinaSRConfig() if "Swin2SR_ClassicalSR_X4_64" in checkpoint_url: __lowerCAmelCase = 4 elif "Swin2SR_CompressedSR_X4_48" in checkpoint_url: __lowerCAmelCase = 4 __lowerCAmelCase = 48 __lowerCAmelCase = "pixelshuffle_aux" elif "Swin2SR_Lightweight_X2_64" in checkpoint_url: __lowerCAmelCase = [6, 6, 6, 6] __lowerCAmelCase = 60 __lowerCAmelCase = [6, 6, 6, 6] __lowerCAmelCase = "pixelshuffledirect" elif "Swin2SR_RealworldSR_X4_64_BSRGAN_PSNR" in checkpoint_url: __lowerCAmelCase = 4 __lowerCAmelCase = "nearest+conv" elif "Swin2SR_Jpeg_dynamic" in checkpoint_url: __lowerCAmelCase = 1 __lowerCAmelCase = 1 __lowerCAmelCase = 1_26 __lowerCAmelCase = 7 __lowerCAmelCase = 2_55.0 __lowerCAmelCase = "" return config def _a ( SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : int ): if "patch_embed.proj" in name and "layers" not in name: __lowerCAmelCase = name.replace("patch_embed.proj" , "embeddings.patch_embeddings.projection" ) if "patch_embed.norm" in name: __lowerCAmelCase = name.replace("patch_embed.norm" , "embeddings.patch_embeddings.layernorm" ) if "layers" in name: __lowerCAmelCase = name.replace("layers" , "encoder.stages" ) if "residual_group.blocks" in name: __lowerCAmelCase = name.replace("residual_group.blocks" , "layers" ) if "attn.proj" in name: __lowerCAmelCase = name.replace("attn.proj" , "attention.output.dense" ) if "attn" in name: __lowerCAmelCase = name.replace("attn" , "attention.self" ) if "norm1" in name: __lowerCAmelCase = name.replace("norm1" , "layernorm_before" ) if "norm2" in name: __lowerCAmelCase = name.replace("norm2" , "layernorm_after" ) if "mlp.fc1" in name: __lowerCAmelCase = name.replace("mlp.fc1" , "intermediate.dense" ) if "mlp.fc2" in name: __lowerCAmelCase = name.replace("mlp.fc2" , "output.dense" ) if "q_bias" in name: __lowerCAmelCase = name.replace("q_bias" , "query.bias" ) if "k_bias" in name: __lowerCAmelCase = name.replace("k_bias" , "key.bias" ) if "v_bias" in name: __lowerCAmelCase = name.replace("v_bias" , "value.bias" ) if "cpb_mlp" in name: __lowerCAmelCase = name.replace("cpb_mlp" , "continuous_position_bias_mlp" ) if "patch_embed.proj" in name: __lowerCAmelCase = name.replace("patch_embed.proj" , "patch_embed.projection" ) if name == "norm.weight": __lowerCAmelCase = "layernorm.weight" if name == "norm.bias": __lowerCAmelCase = "layernorm.bias" if "conv_first" in name: __lowerCAmelCase = name.replace("conv_first" , "first_convolution" ) if ( "upsample" in name or "conv_before_upsample" in name or "conv_bicubic" in name or "conv_up" in name or "conv_hr" in name or "conv_last" in name or "aux" in name ): # heads if "conv_last" in name: __lowerCAmelCase = name.replace("conv_last" , "final_convolution" ) if config.upsampler in ["pixelshuffle", "pixelshuffle_aux", "nearest+conv"]: if "conv_before_upsample.0" in name: __lowerCAmelCase = name.replace("conv_before_upsample.0" , "conv_before_upsample" ) if "upsample.0" in name: __lowerCAmelCase = name.replace("upsample.0" , "upsample.convolution_0" ) if "upsample.2" in name: __lowerCAmelCase = name.replace("upsample.2" , "upsample.convolution_1" ) __lowerCAmelCase = "upsample." + name elif config.upsampler == "pixelshuffledirect": __lowerCAmelCase = name.replace("upsample.0.weight" , "upsample.conv.weight" ) __lowerCAmelCase = name.replace("upsample.0.bias" , "upsample.conv.bias" ) else: pass else: __lowerCAmelCase = "swin2sr." + name return name def _a ( SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : Optional[int] ): for key in orig_state_dict.copy().keys(): __lowerCAmelCase = orig_state_dict.pop(SCREAMING_SNAKE_CASE_ ) if "qkv" in key: __lowerCAmelCase = key.split("." ) __lowerCAmelCase = int(key_split[1] ) __lowerCAmelCase = int(key_split[4] ) __lowerCAmelCase = config.embed_dim if "weight" in key: __lowerCAmelCase = val[:dim, :] __lowerCAmelCase = val[dim : dim * 2, :] __lowerCAmelCase = val[-dim:, :] else: __lowerCAmelCase = val[:dim] __lowerCAmelCase = val[dim : dim * 2] __lowerCAmelCase = val[-dim:] pass else: __lowerCAmelCase = val return orig_state_dict def _a ( SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : Optional[Any] ): __lowerCAmelCase = get_config(SCREAMING_SNAKE_CASE_ ) __lowerCAmelCase = SwinaSRForImageSuperResolution(SCREAMING_SNAKE_CASE_ ) model.eval() __lowerCAmelCase = torch.hub.load_state_dict_from_url(SCREAMING_SNAKE_CASE_ , map_location="cpu" ) __lowerCAmelCase = convert_state_dict(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) __lowerCAmelCase , __lowerCAmelCase = model.load_state_dict(SCREAMING_SNAKE_CASE_ , strict=SCREAMING_SNAKE_CASE_ ) if len(SCREAMING_SNAKE_CASE_ ) > 0: raise ValueError("Missing keys when converting: {}".format(SCREAMING_SNAKE_CASE_ ) ) for key in unexpected_keys: if not ("relative_position_index" in key or "relative_coords_table" in key or "self_mask" in key): raise ValueError(F"""Unexpected key {key} in state_dict""" ) # verify values __lowerCAmelCase = "https://github.com/mv-lab/swin2sr/blob/main/testsets/real-inputs/shanghai.jpg?raw=true" __lowerCAmelCase = Image.open(requests.get(SCREAMING_SNAKE_CASE_ , stream=SCREAMING_SNAKE_CASE_ ).raw ).convert("RGB" ) __lowerCAmelCase = SwinaSRImageProcessor() # pixel_values = processor(image, return_tensors="pt").pixel_values __lowerCAmelCase = 1_26 if "Jpeg" in checkpoint_url else 2_56 __lowerCAmelCase = Compose( [ Resize((image_size, image_size) ), ToTensor(), Normalize(mean=[0.4_85, 0.4_56, 0.4_06] , std=[0.2_29, 0.2_24, 0.2_25] ), ] ) __lowerCAmelCase = transforms(SCREAMING_SNAKE_CASE_ ).unsqueeze(0 ) if config.num_channels == 1: __lowerCAmelCase = pixel_values[:, 0, :, :].unsqueeze(1 ) __lowerCAmelCase = model(SCREAMING_SNAKE_CASE_ ) # assert values if "Swin2SR_ClassicalSR_X2_64" in checkpoint_url: __lowerCAmelCase = torch.Size([1, 3, 5_12, 5_12] ) __lowerCAmelCase = torch.tensor( [[-0.70_87, -0.71_38, -0.67_21], [-0.83_40, -0.80_95, -0.72_98], [-0.91_49, -0.84_14, -0.79_40]] ) elif "Swin2SR_ClassicalSR_X4_64" in checkpoint_url: __lowerCAmelCase = torch.Size([1, 3, 10_24, 10_24] ) __lowerCAmelCase = torch.tensor( [[-0.77_75, -0.81_05, -0.89_33], [-0.77_64, -0.83_56, -0.92_25], [-0.79_76, -0.86_86, -0.95_79]] ) elif "Swin2SR_CompressedSR_X4_48" in checkpoint_url: # TODO values didn't match exactly here __lowerCAmelCase = torch.Size([1, 3, 10_24, 10_24] ) __lowerCAmelCase = torch.tensor( [[-0.80_35, -0.75_04, -0.74_91], [-0.85_38, -0.81_24, -0.77_82], [-0.88_04, -0.86_51, -0.84_93]] ) elif "Swin2SR_Lightweight_X2_64" in checkpoint_url: __lowerCAmelCase = torch.Size([1, 3, 5_12, 5_12] ) __lowerCAmelCase = torch.tensor( [[-0.76_69, -0.86_62, -0.87_67], [-0.88_10, -0.99_62, -0.98_20], [-0.93_40, -1.03_22, -1.11_49]] ) elif "Swin2SR_RealworldSR_X4_64_BSRGAN_PSNR" in checkpoint_url: __lowerCAmelCase = torch.Size([1, 3, 10_24, 10_24] ) __lowerCAmelCase = torch.tensor( [[-0.52_38, -0.55_57, -0.63_21], [-0.60_16, -0.59_03, -0.63_91], [-0.62_44, -0.63_34, -0.68_89]] ) assert ( outputs.reconstruction.shape == expected_shape ), F"""Shape of reconstruction should be {expected_shape}, but is {outputs.reconstruction.shape}""" assert torch.allclose(outputs.reconstruction[0, 0, :3, :3] , SCREAMING_SNAKE_CASE_ , atol=1E-3 ) print("Looks ok!" ) __lowerCAmelCase = { "https://github.com/mv-lab/swin2sr/releases/download/v0.0.1/Swin2SR_ClassicalSR_X2_64.pth": ( "swin2SR-classical-sr-x2-64" ), "https://github.com/mv-lab/swin2sr/releases/download/v0.0.1/Swin2SR_ClassicalSR_X4_64.pth": ( "swin2SR-classical-sr-x4-64" ), "https://github.com/mv-lab/swin2sr/releases/download/v0.0.1/Swin2SR_CompressedSR_X4_48.pth": ( "swin2SR-compressed-sr-x4-48" ), "https://github.com/mv-lab/swin2sr/releases/download/v0.0.1/Swin2SR_Lightweight_X2_64.pth": ( "swin2SR-lightweight-x2-64" ), "https://github.com/mv-lab/swin2sr/releases/download/v0.0.1/Swin2SR_RealworldSR_X4_64_BSRGAN_PSNR.pth": ( "swin2SR-realworld-sr-x4-64-bsrgan-psnr" ), } __lowerCAmelCase = url_to_name[checkpoint_url] if pytorch_dump_folder_path is not None: print(F"""Saving model {model_name} to {pytorch_dump_folder_path}""" ) model.save_pretrained(SCREAMING_SNAKE_CASE_ ) print(F"""Saving image processor to {pytorch_dump_folder_path}""" ) processor.save_pretrained(SCREAMING_SNAKE_CASE_ ) if push_to_hub: model.push_to_hub(F"""caidas/{model_name}""" ) processor.push_to_hub(F"""caidas/{model_name}""" ) if __name__ == "__main__": UpperCamelCase__ = argparse.ArgumentParser() # Required parameters parser.add_argument( """--checkpoint_url""", default="""https://github.com/mv-lab/swin2sr/releases/download/v0.0.1/Swin2SR_ClassicalSR_X2_64.pth""", type=str, help="""URL of the original Swin2SR checkpoint you'd like to convert.""", ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model directory.""" ) parser.add_argument("""--push_to_hub""", action="""store_true""", help="""Whether to push the converted model to the hub.""") UpperCamelCase__ = parser.parse_args() convert_swinasr_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path, args.push_to_hub)
102
import coval # From: git+https://github.com/ns-moosavi/coval.git # noqa: F401 from coval.conll import reader, util from coval.eval import evaluator import datasets UpperCamelCase__ = datasets.logging.get_logger(__name__) UpperCamelCase__ = """\ @InProceedings{moosavi2019minimum, author = { Nafise Sadat Moosavi, Leo Born, Massimo Poesio and Michael Strube}, title = {Using Automatically Extracted Minimum Spans to Disentangle Coreference Evaluation from Boundary Detection}, year = {2019}, booktitle = {Proceedings of the 57th Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers)}, publisher = {Association for Computational Linguistics}, address = {Florence, Italy}, } @inproceedings{10.3115/1072399.1072405, author = {Vilain, Marc and Burger, John and Aberdeen, John and Connolly, Dennis and Hirschman, Lynette}, title = {A Model-Theoretic Coreference Scoring Scheme}, year = {1995}, isbn = {1558604022}, publisher = {Association for Computational Linguistics}, address = {USA}, url = {https://doi.org/10.3115/1072399.1072405}, doi = {10.3115/1072399.1072405}, booktitle = {Proceedings of the 6th Conference on Message Understanding}, pages = {45–52}, numpages = {8}, location = {Columbia, Maryland}, series = {MUC6 ’95} } @INPROCEEDINGS{Bagga98algorithmsfor, author = {Amit Bagga and Breck Baldwin}, title = {Algorithms for Scoring Coreference Chains}, booktitle = {In The First International Conference on Language Resources and Evaluation Workshop on Linguistics Coreference}, year = {1998}, pages = {563--566} } @INPROCEEDINGS{Luo05oncoreference, author = {Xiaoqiang Luo}, title = {On coreference resolution performance metrics}, booktitle = {In Proc. of HLT/EMNLP}, year = {2005}, pages = {25--32}, publisher = {URL} } @inproceedings{moosavi-strube-2016-coreference, title = \"Which Coreference Evaluation Metric Do You Trust? A Proposal for a Link-based Entity Aware Metric\", author = \"Moosavi, Nafise Sadat and Strube, Michael\", booktitle = \"Proceedings of the 54th Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers)\", month = aug, year = \"2016\", address = \"Berlin, Germany\", publisher = \"Association for Computational Linguistics\", url = \"https://www.aclweb.org/anthology/P16-1060\", doi = \"10.18653/v1/P16-1060\", pages = \"632--642\", } """ UpperCamelCase__ = """\ CoVal is a coreference evaluation tool for the CoNLL and ARRAU datasets which implements of the common evaluation metrics including MUC [Vilain et al, 1995], B-cubed [Bagga and Baldwin, 1998], CEAFe [Luo et al., 2005], LEA [Moosavi and Strube, 2016] and the averaged CoNLL score (the average of the F1 values of MUC, B-cubed and CEAFe) [Denis and Baldridge, 2009a; Pradhan et al., 2011]. This wrapper of CoVal currently only work with CoNLL line format: The CoNLL format has one word per line with all the annotation for this word in column separated by spaces: Column Type Description 1 Document ID This is a variation on the document filename 2 Part number Some files are divided into multiple parts numbered as 000, 001, 002, ... etc. 3 Word number 4 Word itself This is the token as segmented/tokenized in the Treebank. Initially the *_skel file contain the placeholder [WORD] which gets replaced by the actual token from the Treebank which is part of the OntoNotes release. 5 Part-of-Speech 6 Parse bit This is the bracketed structure broken before the first open parenthesis in the parse, and the word/part-of-speech leaf replaced with a *. The full parse can be created by substituting the asterix with the \"([pos] [word])\" string (or leaf) and concatenating the items in the rows of that column. 7 Predicate lemma The predicate lemma is mentioned for the rows for which we have semantic role information. All other rows are marked with a \"-\" 8 Predicate Frameset ID This is the PropBank frameset ID of the predicate in Column 7. 9 Word sense This is the word sense of the word in Column 3. 10 Speaker/Author This is the speaker or author name where available. Mostly in Broadcast Conversation and Web Log data. 11 Named Entities These columns identifies the spans representing various named entities. 12:N Predicate Arguments There is one column each of predicate argument structure information for the predicate mentioned in Column 7. N Coreference Coreference chain information encoded in a parenthesis structure. More informations on the format can be found here (section \"*_conll File Format\"): http://www.conll.cemantix.org/2012/data.html Details on the evaluation on CoNLL can be found here: https://github.com/ns-moosavi/coval/blob/master/conll/README.md CoVal code was written by @ns-moosavi. Some parts are borrowed from https://github.com/clarkkev/deep-coref/blob/master/evaluation.py The test suite is taken from https://github.com/conll/reference-coreference-scorers/ Mention evaluation and the test suite are added by @andreasvc. Parsing CoNLL files is developed by Leo Born. """ UpperCamelCase__ = """ Calculates coreference evaluation metrics. Args: predictions: list of sentences. Each sentence is a list of word predictions to score in the CoNLL format. Each prediction is a word with its annotations as a string made of columns joined with spaces. Only columns 4, 5, 6 and the last column are used (word, POS, Pars and coreference annotation) See the details on the format in the description of the metric. references: list of sentences. Each sentence is a list of word reference to score in the CoNLL format. Each reference is a word with its annotations as a string made of columns joined with spaces. Only columns 4, 5, 6 and the last column are used (word, POS, Pars and coreference annotation) See the details on the format in the description of the metric. keep_singletons: After extracting all mentions of key or system files, mentions whose corresponding coreference chain is of size one, are considered as singletons. The default evaluation mode will include singletons in evaluations if they are included in the key or the system files. By setting 'keep_singletons=False', all singletons in the key and system files will be excluded from the evaluation. NP_only: Most of the recent coreference resolvers only resolve NP mentions and leave out the resolution of VPs. By setting the 'NP_only' option, the scorer will only evaluate the resolution of NPs. min_span: By setting 'min_span', the scorer reports the results based on automatically detected minimum spans. Minimum spans are determined using the MINA algorithm. Returns: 'mentions': mentions 'muc': MUC metric [Vilain et al, 1995] 'bcub': B-cubed [Bagga and Baldwin, 1998] 'ceafe': CEAFe [Luo et al., 2005] 'lea': LEA [Moosavi and Strube, 2016] 'conll_score': averaged CoNLL score (the average of the F1 values of MUC, B-cubed and CEAFe) Examples: >>> coval = datasets.load_metric('coval') >>> words = ['bc/cctv/00/cctv_0005 0 0 Thank VBP (TOP(S(VP* thank 01 1 Xu_li * (V*) * -', ... 'bc/cctv/00/cctv_0005 0 1 you PRP (NP*) - - - Xu_li * (ARG1*) (ARG0*) (116)', ... 'bc/cctv/00/cctv_0005 0 2 everyone NN (NP*) - - - Xu_li * (ARGM-DIS*) * (116)', ... 'bc/cctv/00/cctv_0005 0 3 for IN (PP* - - - Xu_li * (ARG2* * -', ... 'bc/cctv/00/cctv_0005 0 4 watching VBG (S(VP*)))) watch 01 1 Xu_li * *) (V*) -', ... 'bc/cctv/00/cctv_0005 0 5 . . *)) - - - Xu_li * * * -'] >>> references = [words] >>> predictions = [words] >>> results = coval.compute(predictions=predictions, references=references) >>> print(results) # doctest:+ELLIPSIS {'mentions/recall': 1.0,[...] 'conll_score': 100.0} """ def _a ( SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : Tuple , SCREAMING_SNAKE_CASE_ : Dict=False , SCREAMING_SNAKE_CASE_ : Any=False , SCREAMING_SNAKE_CASE_ : str=True , SCREAMING_SNAKE_CASE_ : int=False , SCREAMING_SNAKE_CASE_ : Optional[int]="dummy_doc" ): __lowerCAmelCase = {doc: key_lines} __lowerCAmelCase = {doc: sys_lines} __lowerCAmelCase = {} __lowerCAmelCase = 0 __lowerCAmelCase = 0 __lowerCAmelCase = 0 __lowerCAmelCase = 0 __lowerCAmelCase = 0 __lowerCAmelCase = 0 __lowerCAmelCase , __lowerCAmelCase = reader.get_doc_mentions(SCREAMING_SNAKE_CASE_ , key_doc_lines[doc] , SCREAMING_SNAKE_CASE_ ) key_singletons_num += singletons_num if NP_only or min_span: __lowerCAmelCase = reader.set_annotated_parse_trees(SCREAMING_SNAKE_CASE_ , key_doc_lines[doc] , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) __lowerCAmelCase , __lowerCAmelCase = reader.get_doc_mentions(SCREAMING_SNAKE_CASE_ , sys_doc_lines[doc] , SCREAMING_SNAKE_CASE_ ) sys_singletons_num += singletons_num if NP_only or min_span: __lowerCAmelCase = reader.set_annotated_parse_trees(SCREAMING_SNAKE_CASE_ , key_doc_lines[doc] , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if remove_nested: __lowerCAmelCase , __lowerCAmelCase = reader.remove_nested_coref_mentions(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) key_nested_coref_num += nested_mentions key_removed_nested_clusters += removed_clusters __lowerCAmelCase , __lowerCAmelCase = reader.remove_nested_coref_mentions(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) sys_nested_coref_num += nested_mentions sys_removed_nested_clusters += removed_clusters __lowerCAmelCase = reader.get_mention_assignments(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) __lowerCAmelCase = reader.get_mention_assignments(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) __lowerCAmelCase = (key_clusters, sys_clusters, key_mention_sys_cluster, sys_mention_key_cluster) if remove_nested: logger.info( "Number of removed nested coreferring mentions in the key " F"""annotation: {key_nested_coref_num}; and system annotation: {sys_nested_coref_num}""" ) logger.info( "Number of resulting singleton clusters in the key " F"""annotation: {key_removed_nested_clusters}; and system annotation: {sys_removed_nested_clusters}""" ) if not keep_singletons: logger.info( F"""{key_singletons_num:d} and {sys_singletons_num:d} singletons are removed from the key and system """ "files, respectively" ) return doc_coref_infos def _a ( SCREAMING_SNAKE_CASE_ : Tuple , SCREAMING_SNAKE_CASE_ : Any , SCREAMING_SNAKE_CASE_ : Any , SCREAMING_SNAKE_CASE_ : str , SCREAMING_SNAKE_CASE_ : List[Any] , SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : Optional[Any] ): __lowerCAmelCase = get_coref_infos(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) __lowerCAmelCase = {} __lowerCAmelCase = 0 __lowerCAmelCase = 0 for name, metric in metrics: __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase = evaluator.evaluate_documents(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , beta=1 ) if name in ["muc", "bcub", "ceafe"]: conll += fa conll_subparts_num += 1 output_scores.update({F"""{name}/recall""": recall, F"""{name}/precision""": precision, F"""{name}/f1""": fa} ) logger.info( name.ljust(10 ) , F"""Recall: {recall * 1_00:.2f}""" , F""" Precision: {precision * 1_00:.2f}""" , F""" F1: {fa * 1_00:.2f}""" , ) if conll_subparts_num == 3: __lowerCAmelCase = (conll / 3) * 1_00 logger.info(F"""CoNLL score: {conll:.2f}""" ) output_scores.update({"conll_score": conll} ) return output_scores def _a ( SCREAMING_SNAKE_CASE_ : List[str] ): __lowerCAmelCase = False for line in key_lines: if not line.startswith("#" ): if len(line.split() ) > 6: __lowerCAmelCase = line.split()[5] if not parse_col == "-": __lowerCAmelCase = True break else: break return has_gold_parse @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class a__ ( datasets.Metric ): def __SCREAMING_SNAKE_CASE( self ): """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { "predictions": datasets.Sequence(datasets.Value("string" ) ), "references": datasets.Sequence(datasets.Value("string" ) ), } ) , codebase_urls=["https://github.com/ns-moosavi/coval"] , reference_urls=[ "https://github.com/ns-moosavi/coval", "https://www.aclweb.org/anthology/P16-1060", "http://www.conll.cemantix.org/2012/data.html", ] , ) def __SCREAMING_SNAKE_CASE( self , _A , _A , _A=True , _A=False , _A=False , _A=False ): """simple docstring""" __lowerCAmelCase = [ ("mentions", evaluator.mentions), ("muc", evaluator.muc), ("bcub", evaluator.b_cubed), ("ceafe", evaluator.ceafe), ("lea", evaluator.lea), ] if min_span: __lowerCAmelCase = util.check_gold_parse_annotation(_A ) if not has_gold_parse: raise NotImplementedError("References should have gold parse annotation to use 'min_span'." ) # util.parse_key_file(key_file) # key_file = key_file + ".parsed" __lowerCAmelCase = evaluate( key_lines=_A , sys_lines=_A , metrics=_A , NP_only=_A , remove_nested=_A , keep_singletons=_A , min_span=_A , ) return score
102
1
from __future__ import annotations import typing from collections.abc import Iterable import numpy as np lowerCAmelCase__ = typing.Union[Iterable[float], Iterable[int], np.ndarray] # noqa: UP007 lowerCAmelCase__ = typing.Union[np.floataa, int, float] # noqa: UP007 def _UpperCAmelCase (UpperCamelCase__ : Vector , UpperCamelCase__ : Vector ): return np.sqrt(np.sum((np.asarray(UpperCamelCase__ ) - np.asarray(UpperCamelCase__ )) ** 2 ) ) def _UpperCAmelCase (UpperCamelCase__ : Vector , UpperCamelCase__ : Vector ): return sum((va - va) ** 2 for va, va in zip(UpperCamelCase__ , UpperCamelCase__ ) ) ** (1 / 2) if __name__ == "__main__": def _UpperCAmelCase (): from timeit import timeit print("Without Numpy" ) print( timeit( "euclidean_distance_no_np([1, 2, 3], [4, 5, 6])" , number=10000 , globals=globals() , ) ) print("With Numpy" ) print( timeit( "euclidean_distance([1, 2, 3], [4, 5, 6])" , number=10000 , globals=globals() , ) ) benchmark()
11
import warnings from typing import List import numpy as np from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding from ...utils import is_flax_available, is_tf_available, is_torch_available class lowerCAmelCase__ ( a): '''simple docstring''' __SCREAMING_SNAKE_CASE = ["image_processor", "tokenizer"] __SCREAMING_SNAKE_CASE = "OwlViTImageProcessor" __SCREAMING_SNAKE_CASE = ("CLIPTokenizer", "CLIPTokenizerFast") def __init__( self , __lowerCamelCase=None , __lowerCamelCase=None , **__lowerCamelCase) -> Union[str, Any]: _A : int = None if "feature_extractor" in kwargs: warnings.warn( "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`" " instead." , __lowerCamelCase , ) _A : List[Any] = kwargs.pop("feature_extractor") _A : Dict = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError("You need to specify an `image_processor`.") if tokenizer is None: raise ValueError("You need to specify a `tokenizer`.") super().__init__(__lowerCamelCase , __lowerCamelCase) def __call__( self , __lowerCamelCase=None , __lowerCamelCase=None , __lowerCamelCase=None , __lowerCamelCase="max_length" , __lowerCamelCase="np" , **__lowerCamelCase) -> Any: if text is None and query_images is None and images is None: raise ValueError( "You have to specify at least one text or query image or image. All three cannot be none.") if text is not None: if isinstance(__lowerCamelCase , __lowerCamelCase) or (isinstance(__lowerCamelCase , __lowerCamelCase) and not isinstance(text[0] , __lowerCamelCase)): _A : Union[str, Any] = [self.tokenizer(__lowerCamelCase , padding=__lowerCamelCase , return_tensors=__lowerCamelCase , **__lowerCamelCase)] elif isinstance(__lowerCamelCase , __lowerCamelCase) and isinstance(text[0] , __lowerCamelCase): _A : Optional[Any] = [] # Maximum number of queries across batch _A : str = max([len(__lowerCamelCase) for t in text]) # Pad all batch samples to max number of text queries for t in text: if len(__lowerCamelCase) != max_num_queries: _A : Optional[int] = t + [" "] * (max_num_queries - len(__lowerCamelCase)) _A : List[Any] = self.tokenizer(__lowerCamelCase , padding=__lowerCamelCase , return_tensors=__lowerCamelCase , **__lowerCamelCase) encodings.append(__lowerCamelCase) else: raise TypeError("Input text should be a string, a list of strings or a nested list of strings") if return_tensors == "np": _A : Tuple = np.concatenate([encoding["input_ids"] for encoding in encodings] , axis=0) _A : Optional[Any] = np.concatenate([encoding["attention_mask"] for encoding in encodings] , axis=0) elif return_tensors == "jax" and is_flax_available(): import jax.numpy as jnp _A : Optional[int] = jnp.concatenate([encoding["input_ids"] for encoding in encodings] , axis=0) _A : Optional[int] = jnp.concatenate([encoding["attention_mask"] for encoding in encodings] , axis=0) elif return_tensors == "pt" and is_torch_available(): import torch _A : Optional[Any] = torch.cat([encoding["input_ids"] for encoding in encodings] , dim=0) _A : Union[str, Any] = torch.cat([encoding["attention_mask"] for encoding in encodings] , dim=0) elif return_tensors == "tf" and is_tf_available(): import tensorflow as tf _A : Any = tf.stack([encoding["input_ids"] for encoding in encodings] , axis=0) _A : Tuple = tf.stack([encoding["attention_mask"] for encoding in encodings] , axis=0) else: raise ValueError("Target return tensor type could not be returned") _A : Optional[Any] = BatchEncoding() _A : Tuple = input_ids _A : Dict = attention_mask if query_images is not None: _A : Optional[Any] = BatchEncoding() _A : List[str] = self.image_processor( __lowerCamelCase , return_tensors=__lowerCamelCase , **__lowerCamelCase).pixel_values _A : Union[str, Any] = query_pixel_values if images is not None: _A : int = self.image_processor(__lowerCamelCase , return_tensors=__lowerCamelCase , **__lowerCamelCase) if text is not None and images is not None: _A : Tuple = image_features.pixel_values return encoding elif query_images is not None and images is not None: _A : int = image_features.pixel_values return encoding elif text is not None or query_images is not None: return encoding else: return BatchEncoding(data=dict(**__lowerCamelCase) , tensor_type=__lowerCamelCase) def _lowerCamelCase ( self , *__lowerCamelCase , **__lowerCamelCase) -> str: return self.image_processor.post_process(*__lowerCamelCase , **__lowerCamelCase) def _lowerCamelCase ( self , *__lowerCamelCase , **__lowerCamelCase) -> List[str]: return self.image_processor.post_process_object_detection(*__lowerCamelCase , **__lowerCamelCase) def _lowerCamelCase ( self , *__lowerCamelCase , **__lowerCamelCase) -> Optional[int]: return self.image_processor.post_process_image_guided_detection(*__lowerCamelCase , **__lowerCamelCase) def _lowerCamelCase ( self , *__lowerCamelCase , **__lowerCamelCase) -> int: return self.tokenizer.batch_decode(*__lowerCamelCase , **__lowerCamelCase) def _lowerCamelCase ( self , *__lowerCamelCase , **__lowerCamelCase) -> Optional[int]: return self.tokenizer.decode(*__lowerCamelCase , **__lowerCamelCase) @property def _lowerCamelCase ( self) -> int: warnings.warn( "`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead." , __lowerCamelCase , ) return self.image_processor_class @property def _lowerCamelCase ( self) -> List[str]: warnings.warn( "`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead." , __lowerCamelCase , ) return self.image_processor
11
1
import os import pytest from attr import dataclass _snake_case : Dict = 'us-east-1' # defaults region @dataclass class _UpperCAmelCase : """simple docstring""" a_ = 42 a_ = 'arn:aws:iam::558105141721:role/sagemaker_execution_role' a_ = { 'task_name': 'mnli', 'per_device_train_batch_size': 16, 'per_device_eval_batch_size': 16, 'do_train': True, 'do_eval': True, 'do_predict': True, 'output_dir': '/opt/ml/model', 'overwrite_output_dir': True, 'max_steps': 5_00, 'save_steps': 55_00, } a_ = {**hyperparameters, 'max_steps': 10_00} @property def lowercase ( self : Optional[Any] ) -> Optional[Any]: if self.framework == "pytorch": return [ {"Name": "train_runtime", "Regex": r"train_runtime.*=\D*(.*?)$"}, {"Name": "eval_accuracy", "Regex": r"eval_accuracy.*=\D*(.*?)$"}, {"Name": "eval_loss", "Regex": r"eval_loss.*=\D*(.*?)$"}, ] else: return [ {"Name": "train_runtime", "Regex": r"train_runtime.*=\D*(.*?)$"}, {"Name": "eval_accuracy", "Regex": r"loss.*=\D*(.*?)]?$"}, {"Name": "eval_loss", "Regex": r"sparse_categorical_accuracy.*=\D*(.*?)]?$"}, ] @property def lowercase ( self : Dict ) -> Any: return f"""{self.framework}-transfromers-test""" @property def lowercase ( self : Union[str, Any] ) -> Optional[Any]: return f"""./tests/sagemaker/scripts/{self.framework}""" @property def lowercase ( self : Dict ) -> Dict: if self.framework == "pytorch": return "763104351884.dkr.ecr.us-east-1.amazonaws.com/huggingface-pytorch-training:1.7.1-transformers4.6.1-gpu-py36-cu110-ubuntu18.04" else: return "763104351884.dkr.ecr.us-east-1.amazonaws.com/huggingface-tensorflow-training:2.4.1-transformers4.6.1-gpu-py37-cu110-ubuntu18.04" @pytest.fixture(scope='class' ) def a_ ( lowerCAmelCase_ : Any ): __lowerCAmelCase = SageMakerTestEnvironment(framework=request.cls.framework )
358
class _UpperCAmelCase : """simple docstring""" def __init__( self : Union[str, Any] , lowerCAmelCase_ : List[str] , lowerCAmelCase_ : int , lowerCAmelCase_ : Dict ) -> List[str]: __lowerCAmelCase = name __lowerCAmelCase = value __lowerCAmelCase = weight def __repr__( self : Union[str, Any] ) -> List[str]: return f"""{self.__class__.__name__}({self.name}, {self.value}, {self.weight})""" def lowercase ( self : int ) -> Optional[int]: return self.value def lowercase ( self : Optional[Any] ) -> Union[str, Any]: return self.name def lowercase ( self : List[Any] ) -> Tuple: return self.weight def lowercase ( self : int ) -> Dict: return self.value / self.weight def a_ ( lowerCAmelCase_ : Dict, lowerCAmelCase_ : int, lowerCAmelCase_ : Optional[int] ): __lowerCAmelCase = [] for i in range(len(lowerCAmelCase_ ) ): menu.append(Things(name[i], value[i], weight[i] ) ) return menu def a_ ( lowerCAmelCase_ : Optional[int], lowerCAmelCase_ : List[Any], lowerCAmelCase_ : Optional[Any] ): __lowerCAmelCase = sorted(lowerCAmelCase_, key=lowerCAmelCase_, reverse=lowerCAmelCase_ ) __lowerCAmelCase = [] __lowerCAmelCase , __lowerCAmelCase = 0.0, 0.0 for i in range(len(lowerCAmelCase_ ) ): if (total_cost + items_copy[i].get_weight()) <= max_cost: result.append(items_copy[i] ) total_cost += items_copy[i].get_weight() total_value += items_copy[i].get_value() return (result, total_value) def a_ ( ): pass if __name__ == "__main__": import doctest doctest.testmod()
207
0
"""simple docstring""" def __lowerCAmelCase ( lowercase : int , lowercase : int ) -> str: """simple docstring""" while b: snake_case ,snake_case : int = b, a % b return a def __lowerCAmelCase ( lowercase : int , lowercase : int ) -> str: """simple docstring""" return a if b == 0 else euclidean_gcd_recursive(SCREAMING_SNAKE_CASE_ , a % b ) def __lowerCAmelCase ( ) -> List[Any]: """simple docstring""" 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()
203
import unittest from transformers import CamembertTokenizer, CamembertTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow from transformers.utils import is_torch_available from ...test_tokenization_common import TokenizerTesterMixin UpperCamelCase__ = get_tests_dir("""fixtures/test_sentencepiece.model""") UpperCamelCase__ = get_tests_dir("""fixtures/test_sentencepiece_bpe.model""") UpperCamelCase__ = """pt""" if is_torch_available() else """tf""" @require_sentencepiece @require_tokenizers class a__ ( snake_case__ , unittest.TestCase ): _a : int = CamembertTokenizer _a : Dict = CamembertTokenizerFast _a : Tuple = True _a : List[Any] = True def __SCREAMING_SNAKE_CASE( self ): """simple docstring""" super().setUp() # We have a SentencePiece fixture for testing __lowerCAmelCase = CamembertTokenizer(_A ) tokenizer.save_pretrained(self.tmpdirname ) def __SCREAMING_SNAKE_CASE( self ): """simple docstring""" __lowerCAmelCase = "<pad>" __lowerCAmelCase = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(_A ) , _A ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(_A ) , _A ) def __SCREAMING_SNAKE_CASE( self ): """simple docstring""" __lowerCAmelCase = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , "<s>NOTUSED" ) self.assertEqual(vocab_keys[1] , "<pad>" ) self.assertEqual(vocab_keys[-1] , "<mask>" ) self.assertEqual(len(_A ) , 1_0_0_4 ) def __SCREAMING_SNAKE_CASE( self ): """simple docstring""" self.assertEqual(self.get_tokenizer().vocab_size , 1_0_0_5 ) def __SCREAMING_SNAKE_CASE( self ): """simple docstring""" __lowerCAmelCase = CamembertTokenizer(_A ) tokenizer.save_pretrained(self.tmpdirname ) __lowerCAmelCase = CamembertTokenizerFast.from_pretrained(self.tmpdirname ) __lowerCAmelCase = "I was born in 92000, and this is falsé." __lowerCAmelCase = tokenizer.encode(_A ) __lowerCAmelCase = rust_tokenizer.encode(_A ) self.assertListEqual(_A , _A ) __lowerCAmelCase = tokenizer.encode(_A , add_special_tokens=_A ) __lowerCAmelCase = rust_tokenizer.encode(_A , add_special_tokens=_A ) self.assertListEqual(_A , _A ) # <unk> tokens are not the same for `rust` than for `slow`. # Because spm gives back raw token instead of `unk` in EncodeAsPieces # tokens = tokenizer.tokenize(sequence) __lowerCAmelCase = tokenizer.convert_ids_to_tokens(_A ) __lowerCAmelCase = rust_tokenizer.tokenize(_A ) self.assertListEqual(_A , _A ) def __SCREAMING_SNAKE_CASE( self ): """simple docstring""" if not self.test_rust_tokenizer: return __lowerCAmelCase = self.get_tokenizer() __lowerCAmelCase = self.get_rust_tokenizer() __lowerCAmelCase = "I was born in 92000, and this is falsé." __lowerCAmelCase = tokenizer.tokenize(_A ) __lowerCAmelCase = rust_tokenizer.tokenize(_A ) self.assertListEqual(_A , _A ) __lowerCAmelCase = tokenizer.encode(_A , add_special_tokens=_A ) __lowerCAmelCase = rust_tokenizer.encode(_A , add_special_tokens=_A ) self.assertListEqual(_A , _A ) __lowerCAmelCase = self.get_rust_tokenizer() __lowerCAmelCase = tokenizer.encode(_A ) __lowerCAmelCase = rust_tokenizer.encode(_A ) self.assertListEqual(_A , _A ) @slow def __SCREAMING_SNAKE_CASE( self ): """simple docstring""" __lowerCAmelCase = {"input_ids": [[5, 5_4, 7_1_9_6, 2_9_7, 3_0, 2_3, 7_7_6, 1_8, 1_1, 3_2_1_5, 3_7_0_5, 8_2_5_2, 2_2, 3_1_6_4, 1_1_8_1, 2_1_1_6, 2_9, 1_6, 8_1_3, 2_5, 7_9_1, 3_3_1_4, 2_0, 3_4_4_6, 3_8, 2_7_5_7_5, 1_2_0, 6, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [5, 4_6_8, 1_7, 1_1, 9_0_8_8, 2_0, 1_5_1_7, 8, 2_2_8_0_4, 1_8_8_1_8, 1_0, 3_8, 6_2_9, 6_0_7, 6_0_7, 1_4_2, 1_9, 7_1_9_6, 8_6_7, 5_6, 1_0_3_2_6, 2_4, 2_2_6_7, 2_0, 4_1_6, 5_0_7_2, 1_5_6_1_2, 2_3_3, 7_3_4, 7, 2_3_9_9, 2_7, 1_6, 3_0_1_5, 1_6_4_9, 7, 2_4, 2_0, 4_3_3_8, 2_3_9_9, 2_7, 1_3, 3_4_0_0, 1_4, 1_3, 6_1_8_9, 8, 9_3_0, 9, 6]], "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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]} # noqa: E501 # fmt: on # camembert is a french model. So we also use french texts. __lowerCAmelCase = [ "Le transformeur est un modèle d'apprentissage profond introduit en 2017, " "utilisé principalement dans le domaine du traitement automatique des langues (TAL).", "À l'instar des réseaux de neurones récurrents (RNN), les transformeurs sont conçus " "pour gérer des données séquentielles, telles que le langage naturel, pour des tâches " "telles que la traduction et la synthèse de texte.", ] self.tokenizer_integration_test_util( expected_encoding=_A , model_name="camembert-base" , revision="3a0641d9a1aeb7e848a74299e7e4c4bca216b4cf" , sequences=_A , )
92
0
'''simple docstring''' import unittest from transformers import MraConfig, 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, floats_tensor, ids_tensor, random_attention_mask if is_torch_available(): import torch from transformers import ( MraForMaskedLM, MraForMultipleChoice, MraForQuestionAnswering, MraForSequenceClassification, MraForTokenClassification, MraModel, ) from transformers.models.mra.modeling_mra import MRA_PRETRAINED_MODEL_ARCHIVE_LIST class a__: def __init__( self : Union[str, Any] , __snake_case : Optional[int] , __snake_case : Optional[int]=2 , __snake_case : Union[str, Any]=8 , __snake_case : List[str]=True , __snake_case : Dict=True , __snake_case : Optional[Any]=True , __snake_case : List[str]=True , __snake_case : Tuple=99 , __snake_case : int=16 , __snake_case : Optional[int]=5 , __snake_case : int=2 , __snake_case : Tuple=36 , __snake_case : Optional[Any]="gelu" , __snake_case : str=0.0 , __snake_case : Optional[int]=0.0 , __snake_case : Tuple=5_12 , __snake_case : str=16 , __snake_case : str=2 , __snake_case : int=0.02 , __snake_case : Optional[int]=3 , __snake_case : List[Any]=4 , __snake_case : Any=None , ): a : int = parent a : Any = batch_size a : Optional[int] = seq_length a : List[str] = is_training a : Dict = use_input_mask a : Union[str, Any] = use_token_type_ids a : Tuple = use_labels a : Dict = vocab_size a : Optional[int] = hidden_size a : List[Any] = num_hidden_layers a : Optional[Any] = num_attention_heads a : str = intermediate_size a : Dict = hidden_act a : str = hidden_dropout_prob a : Tuple = attention_probs_dropout_prob a : Optional[Any] = max_position_embeddings a : Tuple = type_vocab_size a : int = type_sequence_label_size a : List[Any] = initializer_range a : List[str] = num_labels a : List[str] = num_choices a : Optional[Any] = scope def lowercase_ ( self : Union[str, Any] ): a : List[Any] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) a : Optional[Any] = None if self.use_input_mask: a : List[Any] = random_attention_mask([self.batch_size, self.seq_length] ) a : Tuple = None if self.use_token_type_ids: a : Optional[Any] = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) a : str = None a : int = None a : Any = None if self.use_labels: a : int = ids_tensor([self.batch_size] , self.type_sequence_label_size ) a : Union[str, Any] = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) a : List[str] = ids_tensor([self.batch_size] , self.num_choices ) a : Dict = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def lowercase_ ( self : Union[str, Any] ): return MraConfig( 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=__snake_case , initializer_range=self.initializer_range , ) def lowercase_ ( self : List[str] ): a : List[Any] = self.get_config() a : Optional[Any] = 3_00 return config def lowercase_ ( self : Union[str, Any] ): ( ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ) : Optional[Any] = self.prepare_config_and_inputs() a : Union[str, Any] = True a : Tuple = floats_tensor([self.batch_size, self.seq_length, self.hidden_size] ) a : Dict = 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 lowercase_ ( self : int , __snake_case : int , __snake_case : List[Any] , __snake_case : Union[str, Any] , __snake_case : List[Any] , __snake_case : Union[str, Any] , __snake_case : Dict , __snake_case : Any ): a : Dict = MraModel(config=__snake_case ) model.to(__snake_case ) model.eval() a : str = model(__snake_case , attention_mask=__snake_case , token_type_ids=__snake_case ) a : List[str] = model(__snake_case , token_type_ids=__snake_case ) a : Union[str, Any] = model(__snake_case ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def lowercase_ ( self : List[str] , __snake_case : Tuple , __snake_case : List[str] , __snake_case : str , __snake_case : Optional[Any] , __snake_case : Union[str, Any] , __snake_case : Any , __snake_case : Optional[Any] , __snake_case : Optional[int] , __snake_case : List[Any] , ): a : Optional[Any] = True a : Optional[int] = MraModel(__snake_case ) model.to(__snake_case ) model.eval() a : List[Any] = model( __snake_case , attention_mask=__snake_case , token_type_ids=__snake_case , encoder_hidden_states=__snake_case , encoder_attention_mask=__snake_case , ) a : Any = model( __snake_case , attention_mask=__snake_case , token_type_ids=__snake_case , encoder_hidden_states=__snake_case , ) a : Optional[int] = model(__snake_case , attention_mask=__snake_case , token_type_ids=__snake_case ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def lowercase_ ( self : Optional[Any] , __snake_case : int , __snake_case : List[Any] , __snake_case : Optional[int] , __snake_case : Optional[Any] , __snake_case : List[Any] , __snake_case : Dict , __snake_case : Optional[Any] ): a : Union[str, Any] = MraForMaskedLM(config=__snake_case ) model.to(__snake_case ) model.eval() a : List[str] = model(__snake_case , attention_mask=__snake_case , token_type_ids=__snake_case , labels=__snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def lowercase_ ( self : Tuple , __snake_case : Union[str, Any] , __snake_case : str , __snake_case : Tuple , __snake_case : str , __snake_case : Tuple , __snake_case : Optional[int] , __snake_case : int ): a : Optional[int] = MraForQuestionAnswering(config=__snake_case ) model.to(__snake_case ) model.eval() a : Optional[int] = model( __snake_case , attention_mask=__snake_case , token_type_ids=__snake_case , start_positions=__snake_case , end_positions=__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 lowercase_ ( self : Dict , __snake_case : Tuple , __snake_case : List[Any] , __snake_case : List[Any] , __snake_case : Optional[Any] , __snake_case : Any , __snake_case : List[str] , __snake_case : str ): a : Tuple = self.num_labels a : Dict = MraForSequenceClassification(__snake_case ) model.to(__snake_case ) model.eval() a : Any = model(__snake_case , attention_mask=__snake_case , token_type_ids=__snake_case , labels=__snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def lowercase_ ( self : str , __snake_case : Dict , __snake_case : Optional[Any] , __snake_case : int , __snake_case : List[Any] , __snake_case : Any , __snake_case : List[Any] , __snake_case : int ): a : Tuple = self.num_labels a : Tuple = MraForTokenClassification(config=__snake_case ) model.to(__snake_case ) model.eval() a : List[Any] = model(__snake_case , attention_mask=__snake_case , token_type_ids=__snake_case , labels=__snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def lowercase_ ( self : Any , __snake_case : Any , __snake_case : str , __snake_case : Dict , __snake_case : List[Any] , __snake_case : Any , __snake_case : Any , __snake_case : str ): a : Optional[int] = self.num_choices a : int = MraForMultipleChoice(config=__snake_case ) model.to(__snake_case ) model.eval() a : Tuple = input_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() a : Optional[int] = token_type_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() a : Optional[Any] = input_mask.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() a : int = model( __snake_case , attention_mask=__snake_case , token_type_ids=__snake_case , labels=__snake_case , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def lowercase_ ( self : Optional[Any] ): a : Union[str, Any] = self.prepare_config_and_inputs() ( ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ) : Union[str, Any] = config_and_inputs a : List[str] = {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask} return config, inputs_dict @require_torch class a__( lowerCamelCase__ , unittest.TestCase ): lowercase__ = ( ( MraModel, MraForMaskedLM, MraForMultipleChoice, MraForQuestionAnswering, MraForSequenceClassification, MraForTokenClassification, ) if is_torch_available() else () ) lowercase__ = False lowercase__ = False lowercase__ = False lowercase__ = False lowercase__ = () def lowercase_ ( self : Any ): a : Tuple = MraModelTester(self ) a : str = ConfigTester(self , config_class=__snake_case , hidden_size=37 ) def lowercase_ ( self : List[str] ): self.config_tester.run_common_tests() def lowercase_ ( self : List[str] ): a : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__snake_case ) def lowercase_ ( self : Any ): a : Union[str, Any] = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: a : Dict = type self.model_tester.create_and_check_model(*__snake_case ) def lowercase_ ( self : List[Any] ): a : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*__snake_case ) def lowercase_ ( self : Optional[Any] ): a : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_multiple_choice(*__snake_case ) def lowercase_ ( self : List[Any] ): a : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*__snake_case ) def lowercase_ ( self : Tuple ): a : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*__snake_case ) def lowercase_ ( self : Optional[Any] ): a : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*__snake_case ) @slow def lowercase_ ( self : int ): for model_name in MRA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: a : Dict = MraModel.from_pretrained(__snake_case ) self.assertIsNotNone(__snake_case ) @unittest.skip(reason='MRA does not output attentions' ) def lowercase_ ( self : Union[str, Any] ): return @require_torch class a__( unittest.TestCase ): @slow def lowercase_ ( self : Union[str, Any] ): a : Union[str, Any] = MraModel.from_pretrained('uw-madison/mra-base-512-4' ) a : List[str] = torch.arange(2_56 ).unsqueeze(0 ) with torch.no_grad(): a : Optional[int] = model(__snake_case )[0] a : Any = torch.Size((1, 2_56, 7_68) ) self.assertEqual(output.shape , __snake_case ) a : str = torch.tensor( [[[-0.0140, 0.0830, -0.0381], [0.1546, 0.1402, 0.0220], [0.1162, 0.0851, 0.0165]]] ) self.assertTrue(torch.allclose(output[:, :3, :3] , __snake_case , atol=1e-4 ) ) @slow def lowercase_ ( self : Optional[int] ): a : Dict = MraForMaskedLM.from_pretrained('uw-madison/mra-base-512-4' ) a : Optional[int] = torch.arange(2_56 ).unsqueeze(0 ) with torch.no_grad(): a : Dict = model(__snake_case )[0] a : Union[str, Any] = 5_02_65 a : Dict = torch.Size((1, 2_56, vocab_size) ) self.assertEqual(output.shape , __snake_case ) a : Dict = torch.tensor( [[[9.2595, -3.6038, 11.8819], [9.3869, -3.2693, 11.0956], [11.8524, -3.4938, 13.1210]]] ) self.assertTrue(torch.allclose(output[:, :3, :3] , __snake_case , atol=1e-4 ) ) @slow def lowercase_ ( self : Any ): a : Dict = MraForMaskedLM.from_pretrained('uw-madison/mra-base-4096-8-d3' ) a : Optional[int] = torch.arange(40_96 ).unsqueeze(0 ) with torch.no_grad(): a : Tuple = model(__snake_case )[0] a : List[Any] = 5_02_65 a : str = torch.Size((1, 40_96, vocab_size) ) self.assertEqual(output.shape , __snake_case ) a : int = torch.tensor( [[[5.4789, -2.3564, 7.5064], [7.9067, -1.3369, 9.9668], [9.0712, -1.8106, 7.0380]]] ) self.assertTrue(torch.allclose(output[:, :3, :3] , __snake_case , atol=1e-4 ) )
96
'''simple docstring''' lowerCAmelCase: Union[str, Any] = { 'meter': 'm', 'kilometer': 'km', 'megametre': 'Mm', 'gigametre': 'Gm', 'terametre': 'Tm', 'petametre': 'Pm', 'exametre': 'Em', 'zettametre': 'Zm', 'yottametre': 'Ym', } # Exponent of the factor(meter) lowerCAmelCase: Optional[Any] = { 'm': 0, 'km': 3, 'Mm': 6, 'Gm': 9, 'Tm': 1_2, 'Pm': 1_5, 'Em': 1_8, 'Zm': 2_1, 'Ym': 2_4, } def lowerCamelCase__ ( _A , _A , _A ): a : Optional[int] = from_type.lower().strip('s' ) a : Optional[Any] = to_type.lower().strip('s' ) a : Dict = UNIT_SYMBOL.get(_A , _A ) a : Optional[Any] = UNIT_SYMBOL.get(_A , _A ) if from_sanitized not in METRIC_CONVERSION: a : Optional[Any] = ( f"""Invalid 'from_type' value: {from_type!r}.\n""" f"""Conversion abbreviations are: {", ".join(_A )}""" ) raise ValueError(_A ) if to_sanitized not in METRIC_CONVERSION: a : Union[str, Any] = ( f"""Invalid 'to_type' value: {to_type!r}.\n""" f"""Conversion abbreviations are: {", ".join(_A )}""" ) raise ValueError(_A ) a : List[Any] = METRIC_CONVERSION[from_sanitized] a : int = METRIC_CONVERSION[to_sanitized] a : Tuple = 1 if from_exponent > to_exponent: a : Optional[int] = from_exponent - to_exponent else: a : Optional[Any] = -(to_exponent - from_exponent) return value * pow(10 , _A ) if __name__ == "__main__": from doctest import testmod testmod()
96
1
"""simple docstring""" from typing import List from ...configuration_utils import PretrainedConfig from ...utils import logging lowercase__ = logging.get_logger(__name__) lowercase__ = { """snap-research/efficientformer-l1-300""": ( """https://huggingface.co/snap-research/efficientformer-l1-300/resolve/main/config.json""" ), } class lowerCAmelCase__ ( lowercase ): '''simple docstring''' lowerCamelCase__ = """efficientformer""" def __init__( self , lowercase = [3, 2, 6, 4] , lowercase = [48, 96, 224, 448] , lowercase = [True, True, True, True] , lowercase = 448 , lowercase = 32 , lowercase = 4 , lowercase = 7 , lowercase = 5 , lowercase = 8 , lowercase = 4 , lowercase = 0.0 , lowercase = 16 , lowercase = 3 , lowercase = 3 , lowercase = 3 , lowercase = 2 , lowercase = 1 , lowercase = 0.0 , lowercase = 1 , lowercase = True , lowercase = True , lowercase = 1E-5 , lowercase = "gelu" , lowercase = 0.02 , lowercase = 1E-12 , lowercase = 224 , lowercase = 1E-05 , **lowercase , ): super().__init__(**lowercase ) _lowerCamelCase : Dict = hidden_act _lowerCamelCase : Optional[Any] = hidden_dropout_prob _lowerCamelCase : Optional[Any] = hidden_sizes _lowerCamelCase : Optional[Any] = num_hidden_layers _lowerCamelCase : int = num_attention_heads _lowerCamelCase : Tuple = initializer_range _lowerCamelCase : Tuple = layer_norm_eps _lowerCamelCase : Optional[Any] = patch_size _lowerCamelCase : List[str] = num_channels _lowerCamelCase : List[Any] = depths _lowerCamelCase : List[str] = mlp_expansion_ratio _lowerCamelCase : Union[str, Any] = downsamples _lowerCamelCase : List[str] = dim _lowerCamelCase : str = key_dim _lowerCamelCase : Tuple = attention_ratio _lowerCamelCase : Dict = resolution _lowerCamelCase : Any = pool_size _lowerCamelCase : Any = downsample_patch_size _lowerCamelCase : str = downsample_stride _lowerCamelCase : Tuple = downsample_pad _lowerCamelCase : List[str] = drop_path_rate _lowerCamelCase : Optional[Any] = num_metaad_blocks _lowerCamelCase : Optional[Any] = distillation _lowerCamelCase : int = use_layer_scale _lowerCamelCase : Optional[int] = layer_scale_init_value _lowerCamelCase : List[Any] = image_size _lowerCamelCase : Optional[Any] = batch_norm_eps
96
"""simple docstring""" import functools from typing import Any def _snake_case ( lowercase__ , lowercase__ ): # Validation if not isinstance(lowercase__ , lowercase__ ) or len(lowercase__ ) == 0: raise ValueError('the string should be not empty string' ) if not isinstance(lowercase__ , lowercase__ ) or not all( isinstance(lowercase__ , lowercase__ ) and len(lowercase__ ) > 0 for item in words ): raise ValueError('the words should be a list of non-empty strings' ) # Build trie _lowerCamelCase : dict[str, Any] = {} _lowerCamelCase : List[Any] = 'WORD_KEEPER' for word in words: _lowerCamelCase : Dict = trie for c in word: if c not in trie_node: _lowerCamelCase : Any = {} _lowerCamelCase : str = trie_node[c] _lowerCamelCase : Optional[Any] = True _lowerCamelCase : Dict = len(lowercase__ ) # Dynamic programming method @functools.cache def is_breakable(lowercase__ ) -> bool: if index == len_string: return True _lowerCamelCase : List[Any] = trie for i in range(lowercase__ , lowercase__ ): _lowerCamelCase : Any = trie_node.get(string[i] , lowercase__ ) if trie_node is None: return False if trie_node.get(lowercase__ , lowercase__ ) and is_breakable(i + 1 ): return True return False return is_breakable(0 ) if __name__ == "__main__": import doctest doctest.testmod()
96
1
"""simple docstring""" def lowercase__ ( lowercase_ ,lowercase_ ) -> str: """simple docstring""" if not (isinstance(lowercase_ ,lowercase_ ) and isinstance(lowercase_ ,lowercase_ )): raise ValueError("longest_common_substring() takes two strings for inputs" ) _UpperCamelCase : Union[str, Any] = len(lowercase_ ) _UpperCamelCase : str = len(lowercase_ ) _UpperCamelCase : Any = [[0] * (texta_length + 1) for _ in range(texta_length + 1 )] _UpperCamelCase : Tuple = 0 _UpperCamelCase : List[Any] = 0 for i in range(1 ,texta_length + 1 ): for j in range(1 ,texta_length + 1 ): if texta[i - 1] == texta[j - 1]: _UpperCamelCase : Optional[Any] = 1 + dp[i - 1][j - 1] if dp[i][j] > ans_length: _UpperCamelCase : str = i _UpperCamelCase : Union[str, Any] = dp[i][j] return texta[ans_index - ans_length : ans_index] if __name__ == "__main__": import doctest doctest.testmod()
310
"""simple docstring""" from typing import Dict, List, Optional, Union import numpy as np from transformers.utils import is_vision_available from transformers.utils.generic import TensorType from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, is_valid_image, to_numpy_array, valid_images, ) from ...utils import logging if is_vision_available(): import PIL lowerCamelCase__ = logging.get_logger(__name__) def lowercase__ ( lowercase_ ) -> List[List[ImageInput]]: """simple docstring""" if isinstance(lowercase_ ,(list, tuple) ) and isinstance(videos[0] ,(list, tuple) ) and is_valid_image(videos[0][0] ): return videos elif isinstance(lowercase_ ,(list, tuple) ) and is_valid_image(videos[0] ): return [videos] elif is_valid_image(lowercase_ ): return [[videos]] raise ValueError(F'''Could not make batched video from {videos}''' ) class __SCREAMING_SNAKE_CASE ( _UpperCamelCase ): '''simple docstring''' SCREAMING_SNAKE_CASE__ :str = ["pixel_values"] def __init__( self : List[str] , __a : bool = True , __a : Dict[str, int] = None , __a : PILImageResampling = PILImageResampling.BILINEAR , __a : bool = True , __a : Dict[str, int] = None , __a : bool = True , __a : Union[int, float] = 1 / 255 , __a : bool = True , __a : bool = True , __a : Optional[Union[float, List[float]]] = None , __a : Optional[Union[float, List[float]]] = None , **__a : List[Any] , ) -> None: super().__init__(**__a ) _UpperCamelCase : Union[str, Any] = size if size is not None else {"shortest_edge": 256} _UpperCamelCase : List[Any] = get_size_dict(__a , default_to_square=__a ) _UpperCamelCase : int = crop_size if crop_size is not None else {"height": 224, "width": 224} _UpperCamelCase : Optional[Any] = get_size_dict(__a , param_name="crop_size" ) _UpperCamelCase : str = do_resize _UpperCamelCase : Dict = size _UpperCamelCase : int = do_center_crop _UpperCamelCase : int = crop_size _UpperCamelCase : Optional[Any] = resample _UpperCamelCase : Dict = do_rescale _UpperCamelCase : Any = rescale_factor _UpperCamelCase : Any = offset _UpperCamelCase : Union[str, Any] = do_normalize _UpperCamelCase : Union[str, Any] = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN _UpperCamelCase : str = image_std if image_std is not None else IMAGENET_STANDARD_STD def __SCREAMING_SNAKE_CASE ( self : Any , __a : np.ndarray , __a : Dict[str, int] , __a : PILImageResampling = PILImageResampling.BILINEAR , __a : Optional[Union[str, ChannelDimension]] = None , **__a : Tuple , ) -> np.ndarray: _UpperCamelCase : Any = get_size_dict(__a , default_to_square=__a ) if "shortest_edge" in size: _UpperCamelCase : str = get_resize_output_image_size(__a , size["shortest_edge"] , default_to_square=__a ) elif "height" in size and "width" in size: _UpperCamelCase : Any = (size["height"], size["width"]) else: raise ValueError(F'''Size must have \'height\' and \'width\' or \'shortest_edge\' as keys. Got {size.keys()}''' ) return resize(__a , size=__a , resample=__a , data_format=__a , **__a ) def __SCREAMING_SNAKE_CASE ( self : Dict , __a : np.ndarray , __a : Dict[str, int] , __a : Optional[Union[str, ChannelDimension]] = None , **__a : Optional[int] , ) -> np.ndarray: _UpperCamelCase : List[Any] = get_size_dict(__a ) if "height" not in size or "width" not in size: raise ValueError(F'''Size must have \'height\' and \'width\' as keys. Got {size.keys()}''' ) return center_crop(__a , size=(size["height"], size["width"]) , data_format=__a , **__a ) def __SCREAMING_SNAKE_CASE ( self : Dict , __a : np.ndarray , __a : Union[int, float] , __a : bool = True , __a : Optional[Union[str, ChannelDimension]] = None , **__a : List[str] , ) -> Optional[Any]: _UpperCamelCase : Any = image.astype(np.floataa ) if offset: _UpperCamelCase : Dict = image - (scale / 2) return rescale(__a , scale=__a , data_format=__a , **__a ) def __SCREAMING_SNAKE_CASE ( self : List[Any] , __a : np.ndarray , __a : Union[float, List[float]] , __a : Union[float, List[float]] , __a : Optional[Union[str, ChannelDimension]] = None , **__a : Union[str, Any] , ) -> np.ndarray: return normalize(__a , mean=__a , std=__a , data_format=__a , **__a ) def __SCREAMING_SNAKE_CASE ( self : Any , __a : ImageInput , __a : bool = None , __a : Dict[str, int] = None , __a : PILImageResampling = None , __a : bool = None , __a : Dict[str, int] = None , __a : bool = None , __a : float = None , __a : bool = None , __a : bool = None , __a : Optional[Union[float, List[float]]] = None , __a : Optional[Union[float, List[float]]] = None , __a : Optional[ChannelDimension] = ChannelDimension.FIRST , ) -> np.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_center_crop and crop_size is None: raise ValueError("Crop size must be specified if do_center_crop is True." ) if do_rescale and rescale_factor is None: raise ValueError("Rescale factor must be specified if do_rescale is True." ) if do_normalize and (image_mean is None or image_std is None): raise ValueError("Image mean and std must be specified if do_normalize is True." ) if offset and not do_rescale: raise ValueError("For offset, do_rescale must also be set to True." ) # All transformations expect numpy arrays. _UpperCamelCase : Optional[Any] = to_numpy_array(__a ) if do_resize: _UpperCamelCase : Any = self.resize(image=__a , size=__a , resample=__a ) if do_center_crop: _UpperCamelCase : Dict = self.center_crop(__a , size=__a ) if do_rescale: _UpperCamelCase : Union[str, Any] = self.rescale(image=__a , scale=__a , offset=__a ) if do_normalize: _UpperCamelCase : int = self.normalize(image=__a , mean=__a , std=__a ) _UpperCamelCase : str = to_channel_dimension_format(__a , __a ) return image def __SCREAMING_SNAKE_CASE ( self : Optional[int] , __a : ImageInput , __a : bool = None , __a : Dict[str, int] = None , __a : PILImageResampling = None , __a : bool = None , __a : Dict[str, int] = None , __a : bool = None , __a : float = None , __a : bool = None , __a : bool = None , __a : Optional[Union[float, List[float]]] = None , __a : Optional[Union[float, List[float]]] = None , __a : Optional[Union[str, TensorType]] = None , __a : ChannelDimension = ChannelDimension.FIRST , **__a : List[Any] , ) -> PIL.Image.Image: _UpperCamelCase : List[str] = do_resize if do_resize is not None else self.do_resize _UpperCamelCase : Optional[int] = resample if resample is not None else self.resample _UpperCamelCase : str = do_center_crop if do_center_crop is not None else self.do_center_crop _UpperCamelCase : str = do_rescale if do_rescale is not None else self.do_rescale _UpperCamelCase : int = rescale_factor if rescale_factor is not None else self.rescale_factor _UpperCamelCase : str = offset if offset is not None else self.offset _UpperCamelCase : Optional[Any] = do_normalize if do_normalize is not None else self.do_normalize _UpperCamelCase : str = image_mean if image_mean is not None else self.image_mean _UpperCamelCase : Tuple = image_std if image_std is not None else self.image_std _UpperCamelCase : int = size if size is not None else self.size _UpperCamelCase : Tuple = get_size_dict(__a , default_to_square=__a ) _UpperCamelCase : List[str] = crop_size if crop_size is not None else self.crop_size _UpperCamelCase : Optional[int] = get_size_dict(__a , param_name="crop_size" ) if not valid_images(__a ): raise ValueError( "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, " "torch.Tensor, tf.Tensor or jax.ndarray." ) _UpperCamelCase : Union[str, Any] = make_batched(__a ) _UpperCamelCase : Optional[Any] = [ [ self._preprocess_image( image=__a , do_resize=__a , size=__a , resample=__a , do_center_crop=__a , crop_size=__a , do_rescale=__a , rescale_factor=__a , offset=__a , do_normalize=__a , image_mean=__a , image_std=__a , data_format=__a , ) for img in video ] for video in videos ] _UpperCamelCase : List[Any] = {"pixel_values": videos} return BatchFeature(data=__a , tensor_type=__a )
310
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) SCREAMING_SNAKE_CASE : Tuple = { """configuration_rembert""": ["""REMBERT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """RemBertConfig""", """RemBertOnnxConfig"""] } try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE : List[Any] = ["""RemBertTokenizer"""] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE : Tuple = ["""RemBertTokenizerFast"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE : Optional[int] = [ """REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST""", """RemBertForCausalLM""", """RemBertForMaskedLM""", """RemBertForMultipleChoice""", """RemBertForQuestionAnswering""", """RemBertForSequenceClassification""", """RemBertForTokenClassification""", """RemBertLayer""", """RemBertModel""", """RemBertPreTrainedModel""", """load_tf_weights_in_rembert""", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE : List[Any] = [ """TF_REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST""", """TFRemBertForCausalLM""", """TFRemBertForMaskedLM""", """TFRemBertForMultipleChoice""", """TFRemBertForQuestionAnswering""", """TFRemBertForSequenceClassification""", """TFRemBertForTokenClassification""", """TFRemBertLayer""", """TFRemBertModel""", """TFRemBertPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_rembert import REMBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, RemBertConfig, RemBertOnnxConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_rembert import RemBertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_rembert_fast import RemBertTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_rembert import ( REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST, RemBertForCausalLM, RemBertForMaskedLM, RemBertForMultipleChoice, RemBertForQuestionAnswering, RemBertForSequenceClassification, RemBertForTokenClassification, RemBertLayer, RemBertModel, RemBertPreTrainedModel, load_tf_weights_in_rembert, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_rembert import ( TF_REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST, TFRemBertForCausalLM, TFRemBertForMaskedLM, TFRemBertForMultipleChoice, TFRemBertForQuestionAnswering, TFRemBertForSequenceClassification, TFRemBertForTokenClassification, TFRemBertLayer, TFRemBertModel, TFRemBertPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE : str = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
102
"""simple docstring""" from sklearn.metrics import recall_score import datasets SCREAMING_SNAKE_CASE : Dict = """ 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. """ SCREAMING_SNAKE_CASE : Any = """ 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.])} """ SCREAMING_SNAKE_CASE : Tuple = """ @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 SCREAMING_SNAKE_CASE (self ): '''simple docstring''' 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 SCREAMING_SNAKE_CASE (self , a_ , a_ , a_=None , a_=1 , a_="binary" , a_=None , a_="warn" , ): '''simple docstring''' __snake_case : Any = recall_score( a_ , a_ , labels=a_ , pos_label=a_ , average=a_ , sample_weight=a_ , zero_division=a_ , ) return {"recall": float(a_ ) if score.size == 1 else score}
102
1
'''simple docstring''' import os import random import sys from . import cryptomath_module as cryptomath from . import rabin_miller lowerCAmelCase :Optional[int] = 3 def lowerCamelCase ( lowerCAmelCase : int ): """simple docstring""" print('Generating primitive root of p' ) while True: __magic_name__ : List[Any] = random.randrange(3 , lowerCAmelCase ) if pow(lowerCAmelCase , 2 , lowerCAmelCase ) == 1: continue if pow(lowerCAmelCase , lowerCAmelCase , lowerCAmelCase ) == 1: continue return g def lowerCamelCase ( lowerCAmelCase : int ): """simple docstring""" print('Generating prime p...' ) __magic_name__ : str = rabin_miller.generate_large_prime(lowerCAmelCase ) # select large prime number. __magic_name__ : Dict = primitive_root(lowerCAmelCase ) # one primitive root on modulo p. __magic_name__ : Tuple = random.randrange(3 , lowerCAmelCase ) # private_key -> have to be greater than 2 for safety. __magic_name__ : Optional[int] = cryptomath.find_mod_inverse(pow(lowerCAmelCase , lowerCAmelCase , lowerCAmelCase ) , lowerCAmelCase ) __magic_name__ : Dict = (key_size, e_a, e_a, p) __magic_name__ : str = (key_size, d) return public_key, private_key def lowerCamelCase ( lowerCAmelCase : str , lowerCAmelCase : int ): """simple docstring""" if os.path.exists(f'{name}_pubkey.txt' ) or os.path.exists(f'{name}_privkey.txt' ): print('\nWARNING:' ) print( f'"{name}_pubkey.txt" or "{name}_privkey.txt" already exists. \n' 'Use a different name or delete these files and re-run this program.' ) sys.exit() __magic_name__ : Any = generate_key(lowerCAmelCase ) print(f'\nWriting public key to file {name}_pubkey.txt...' ) with open(f'{name}_pubkey.txt' , 'w' ) as fo: fo.write(f'{public_key[0]},{public_key[1]},{public_key[2]},{public_key[3]}' ) print(f'Writing private key to file {name}_privkey.txt...' ) with open(f'{name}_privkey.txt' , 'w' ) as fo: fo.write(f'{private_key[0]},{private_key[1]}' ) def lowerCamelCase ( ): """simple docstring""" print('Making key files...' ) make_key_files('elgamal' , 2048 ) print('Key files generation successful' ) if __name__ == "__main__": main()
353
'''simple docstring''' import argparse import json import os import fairseq import torch from fairseq.data import Dictionary from transformers import ( WavaVecaConfig, WavaVecaCTCTokenizer, WavaVecaFeatureExtractor, WavaVecaForCTC, WavaVecaForPreTraining, WavaVecaProcessor, logging, ) from transformers.models.wavaveca.modeling_wavaveca import WavaVecaForSequenceClassification logging.set_verbosity_info() lowerCAmelCase :str = logging.get_logger(__name__) lowerCAmelCase :str = { '''post_extract_proj''': '''feature_projection.projection''', '''encoder.pos_conv.0''': '''encoder.pos_conv_embed.conv''', '''self_attn.k_proj''': '''encoder.layers.*.attention.k_proj''', '''self_attn.v_proj''': '''encoder.layers.*.attention.v_proj''', '''self_attn.q_proj''': '''encoder.layers.*.attention.q_proj''', '''self_attn.out_proj''': '''encoder.layers.*.attention.out_proj''', '''self_attn_layer_norm''': '''encoder.layers.*.layer_norm''', '''fc1''': '''encoder.layers.*.feed_forward.intermediate_dense''', '''fc2''': '''encoder.layers.*.feed_forward.output_dense''', '''final_layer_norm''': '''encoder.layers.*.final_layer_norm''', '''encoder.layer_norm''': '''encoder.layer_norm''', '''adapter_layer''': '''encoder.layers.*.adapter_layer''', '''w2v_model.layer_norm''': '''feature_projection.layer_norm''', '''quantizer.weight_proj''': '''quantizer.weight_proj''', '''quantizer.vars''': '''quantizer.codevectors''', '''project_q''': '''project_q''', '''final_proj''': '''project_hid''', '''w2v_encoder.proj''': '''lm_head''', '''mask_emb''': '''masked_spec_embed''', '''pooling_layer.linear''': '''projector''', '''pooling_layer.projection''': '''classifier''', } lowerCAmelCase :List[str] = [ '''lm_head''', '''quantizer.weight_proj''', '''quantizer.codevectors''', '''project_q''', '''project_hid''', '''projector''', '''classifier''', ] def lowerCamelCase ( lowerCAmelCase : Tuple ): """simple docstring""" __magic_name__ : Any = {} with open(lowerCAmelCase , 'r' ) as file: for line_number, line in enumerate(lowerCAmelCase ): __magic_name__ : Optional[Any] = line.strip() if line: __magic_name__ : Optional[int] = line.split() __magic_name__ : Any = line_number __magic_name__ : Union[str, Any] = words[0] __magic_name__ : Dict = value return result def lowerCamelCase ( lowerCAmelCase : Optional[int] , lowerCAmelCase : List[Any] , lowerCAmelCase : Optional[int] , lowerCAmelCase : Optional[Any] , lowerCAmelCase : Tuple ): """simple docstring""" for attribute in key.split('.' ): __magic_name__ : Optional[Any] = getattr(lowerCAmelCase , lowerCAmelCase ) __magic_name__ : Tuple = None for param_key in PARAM_MAPPING.keys(): if full_name.endswith(lowerCAmelCase ): __magic_name__ : Optional[Any] = PARAM_MAPPING[full_name.split('.' )[-1]] __magic_name__ : List[Any] = 'param' if weight_type is not None and weight_type != "param": __magic_name__ : List[str] = getattr(lowerCAmelCase , lowerCAmelCase ).shape elif weight_type is not None and weight_type == "param": __magic_name__ : Tuple = hf_pointer for attribute in hf_param_name.split('.' ): __magic_name__ : str = getattr(lowerCAmelCase , lowerCAmelCase ) __magic_name__ : Union[str, Any] = shape_pointer.shape # let's reduce dimension __magic_name__ : int = value[0] else: __magic_name__ : Optional[Any] = hf_pointer.shape if hf_shape != value.shape: raise ValueError( f'Shape of hf {key + "." + weight_type if weight_type is not None else ""} is {hf_shape}, but should be' f' {value.shape} for {full_name}' ) if weight_type == "weight": __magic_name__ : Optional[Any] = value elif weight_type == "weight_g": __magic_name__ : List[str] = value elif weight_type == "weight_v": __magic_name__ : Optional[int] = value elif weight_type == "bias": __magic_name__ : Optional[Any] = value elif weight_type == "param": for attribute in hf_param_name.split('.' ): __magic_name__ : Optional[int] = getattr(lowerCAmelCase , lowerCAmelCase ) __magic_name__ : List[str] = value else: __magic_name__ : int = value logger.info(f'{key + "." + weight_type if weight_type is not None else ""} was initialized from {full_name}.' ) def lowerCamelCase ( lowerCAmelCase : List[Any] , lowerCAmelCase : str , lowerCAmelCase : List[str] , lowerCAmelCase : str , lowerCAmelCase : str ): """simple docstring""" __magic_name__ : Optional[int] = None for param_key in PARAM_MAPPING.keys(): if full_name.endswith(lowerCAmelCase ): __magic_name__ : List[Any] = PARAM_MAPPING[full_name.split('.' )[-1]] __magic_name__ : Dict = 'param' if weight_type is not None and weight_type != "param": __magic_name__ : Union[str, Any] = '.'.join([key, weight_type] ) elif weight_type is not None and weight_type == "param": __magic_name__ : Optional[Any] = '.'.join([key, hf_param_name] ) else: __magic_name__ : int = key __magic_name__ : int = value if 'lm_head' in full_key else value[0] lowerCAmelCase :int = { '''W_a''': '''linear_1.weight''', '''W_b''': '''linear_2.weight''', '''b_a''': '''linear_1.bias''', '''b_b''': '''linear_2.bias''', '''ln_W''': '''norm.weight''', '''ln_b''': '''norm.bias''', } def lowerCamelCase ( lowerCAmelCase : Optional[Any] , lowerCAmelCase : List[Any] , lowerCAmelCase : Tuple=None , lowerCAmelCase : Tuple=None ): """simple docstring""" __magic_name__ : Dict = False for key, mapped_key in MAPPING.items(): __magic_name__ : int = 'wav2vec2.' + mapped_key if mapped_key not in TOP_LEVEL_KEYS else mapped_key if key in name or key.split('w2v_model.' )[-1] == name.split('.' )[0]: __magic_name__ : Union[str, Any] = True if "*" in mapped_key: __magic_name__ : List[Any] = name.split(lowerCAmelCase )[0].split('.' )[-2] __magic_name__ : List[str] = mapped_key.replace('*' , lowerCAmelCase ) if "weight_g" in name: __magic_name__ : str = 'weight_g' elif "weight_v" in name: __magic_name__ : Optional[int] = 'weight_v' elif "bias" in name: __magic_name__ : int = 'bias' elif "weight" in name: # TODO: don't match quantizer.weight_proj __magic_name__ : List[str] = 'weight' else: __magic_name__ : Any = None if hf_dict is not None: rename_dict(lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase ) else: set_recursively(lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase ) return is_used return is_used def lowerCamelCase ( lowerCAmelCase : Union[str, Any] , lowerCAmelCase : Optional[int] , lowerCAmelCase : Dict ): """simple docstring""" __magic_name__ : Union[str, Any] = [] __magic_name__ : Any = fairseq_model.state_dict() __magic_name__ : Optional[int] = hf_model.wavaveca.feature_extractor for name, value in fairseq_dict.items(): __magic_name__ : Optional[Any] = False if "conv_layers" in name: load_conv_layer( lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , hf_model.config.feat_extract_norm == 'group' , ) __magic_name__ : Optional[int] = True else: __magic_name__ : str = load_wavaveca_layer(lowerCAmelCase , lowerCAmelCase , lowerCAmelCase ) if not is_used: unused_weights.append(lowerCAmelCase ) logger.warning(f'Unused weights: {unused_weights}' ) def lowerCamelCase ( lowerCAmelCase : List[str] , lowerCAmelCase : Dict , lowerCAmelCase : Any , lowerCAmelCase : Any , lowerCAmelCase : int ): """simple docstring""" __magic_name__ : Any = full_name.split('conv_layers.' )[-1] __magic_name__ : int = name.split('.' ) __magic_name__ : Any = int(items[0] ) __magic_name__ : Any = int(items[1] ) if type_id == 0: if "bias" in name: if value.shape != feature_extractor.conv_layers[layer_id].conv.bias.data.shape: raise ValueError( f'{full_name} has size {value.shape}, but' f' {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found.' ) __magic_name__ : Union[str, Any] = value logger.info(f'Feat extract conv layer {layer_id} was initialized from {full_name}.' ) elif "weight" in name: if value.shape != feature_extractor.conv_layers[layer_id].conv.weight.data.shape: raise ValueError( f'{full_name} has size {value.shape}, but' f' {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found.' ) __magic_name__ : str = value logger.info(f'Feat extract conv layer {layer_id} was initialized from {full_name}.' ) elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm): if "bias" in name: if value.shape != feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape: raise ValueError( f'{full_name} has size {value.shape}, but' f' {feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape} was found.' ) __magic_name__ : Optional[int] = value logger.info(f'Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.' ) elif "weight" in name: if value.shape != feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape: raise ValueError( f'{full_name} has size {value.shape}, but' f' {feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape} was found.' ) __magic_name__ : Dict = value logger.info(f'Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.' ) else: unused_weights.append(lowerCAmelCase ) @torch.no_grad() def lowerCamelCase ( lowerCAmelCase : Tuple , lowerCAmelCase : Optional[int] , lowerCAmelCase : Dict=None , lowerCAmelCase : Any=None , lowerCAmelCase : Optional[Any]=True , lowerCAmelCase : Any=False ): """simple docstring""" if config_path is not None: __magic_name__ : int = WavaVecaConfig.from_pretrained(lowerCAmelCase ) else: __magic_name__ : List[str] = WavaVecaConfig() if is_seq_class: __magic_name__ : Any = read_txt_into_dict(lowerCAmelCase ) __magic_name__ : Optional[Any] = idalabel __magic_name__ : Union[str, Any] = WavaVecaForSequenceClassification(lowerCAmelCase ) __magic_name__ : Optional[int] = WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=1_6000 , padding_value=0 , do_normalize=lowerCAmelCase , return_attention_mask=lowerCAmelCase , ) feature_extractor.save_pretrained(lowerCAmelCase ) elif is_finetuned: if dict_path: __magic_name__ : str = Dictionary.load(lowerCAmelCase ) # important change bos & pad token id since CTC symbol is <pad> and # not <s> as in fairseq __magic_name__ : Dict = target_dict.pad_index __magic_name__ : Union[str, Any] = target_dict.bos_index __magic_name__ : Union[str, Any] = target_dict.eos_index __magic_name__ : Union[str, Any] = len(target_dict.symbols ) __magic_name__ : Dict = os.path.join(lowerCAmelCase , 'vocab.json' ) if not os.path.isdir(lowerCAmelCase ): logger.error('--pytorch_dump_folder_path ({}) should be a directory'.format(lowerCAmelCase ) ) return os.makedirs(lowerCAmelCase , exist_ok=lowerCAmelCase ) __magic_name__ : List[Any] = target_dict.indices # fairseq has the <pad> and <s> switched __magic_name__ : Any = 0 __magic_name__ : Optional[int] = 1 with open(lowerCAmelCase , 'w' , encoding='utf-8' ) as vocab_handle: json.dump(lowerCAmelCase , lowerCAmelCase ) __magic_name__ : List[str] = WavaVecaCTCTokenizer( lowerCAmelCase , unk_token=target_dict.unk_word , pad_token=target_dict.pad_word , bos_token=target_dict.bos_word , eos_token=target_dict.eos_word , word_delimiter_token='|' , do_lower_case=lowerCAmelCase , ) __magic_name__ : Tuple = True if config.feat_extract_norm == 'layer' else False __magic_name__ : Optional[Any] = WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=1_6000 , padding_value=0 , do_normalize=lowerCAmelCase , return_attention_mask=lowerCAmelCase , ) __magic_name__ : List[Any] = WavaVecaProcessor(feature_extractor=lowerCAmelCase , tokenizer=lowerCAmelCase ) processor.save_pretrained(lowerCAmelCase ) __magic_name__ : Dict = WavaVecaForCTC(lowerCAmelCase ) else: __magic_name__ : Tuple = WavaVecaForPreTraining(lowerCAmelCase ) if is_finetuned or is_seq_class: __magic_name__ , __magic_name__ , __magic_name__ : str = fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] , arg_overrides={'data': '/'.join(dict_path.split('/' )[:-1] )} ) else: __magic_name__ : Optional[Any] = argparse.Namespace(task='audio_pretraining' ) __magic_name__ : Dict = fairseq.tasks.setup_task(lowerCAmelCase ) __magic_name__ , __magic_name__ , __magic_name__ : Optional[Any] = fairseq.checkpoint_utils.load_model_ensemble_and_task([checkpoint_path] , task=lowerCAmelCase ) __magic_name__ : Any = model[0].eval() recursively_load_weights(lowerCAmelCase , lowerCAmelCase , not is_finetuned ) hf_wavavec.save_pretrained(lowerCAmelCase ) if __name__ == "__main__": lowerCAmelCase :str = argparse.ArgumentParser() parser.add_argument('''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''') parser.add_argument('''--checkpoint_path''', default=None, type=str, help='''Path to fairseq checkpoint''') parser.add_argument('''--dict_path''', default=None, type=str, help='''Path to dict of fine-tuned model''') parser.add_argument('''--config_path''', default=None, type=str, help='''Path to hf config.json of model to convert''') parser.add_argument( '''--not_finetuned''', action='''store_true''', help='''Whether the model to convert is a fine-tuned model or not''' ) parser.add_argument( '''--is_seq_class''', action='''store_true''', help='''Whether the model to convert is a fine-tuned sequence classification model or not''', ) lowerCAmelCase :Dict = parser.parse_args() lowerCAmelCase :Tuple = not args.not_finetuned and not args.is_seq_class convert_wavaveca_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.dict_path, is_finetuned, args.is_seq_class, )
275
0
"""simple docstring""" import json import os import torch from diffusers import UNetaDModel os.makedirs('hub/hopper-medium-v2/unet/hor32', exist_ok=True) os.makedirs('hub/hopper-medium-v2/unet/hor128', exist_ok=True) os.makedirs('hub/hopper-medium-v2/value_function', exist_ok=True) def lowercase_ ( _snake_case ): if hor == 128: SCREAMING_SNAKE_CASE__ : str = ("""DownResnetBlock1D""", """DownResnetBlock1D""", """DownResnetBlock1D""") SCREAMING_SNAKE_CASE__ : Union[str, Any] = (32, 128, 256) SCREAMING_SNAKE_CASE__ : List[Any] = ("""UpResnetBlock1D""", """UpResnetBlock1D""") elif hor == 32: SCREAMING_SNAKE_CASE__ : Dict = ("""DownResnetBlock1D""", """DownResnetBlock1D""", """DownResnetBlock1D""", """DownResnetBlock1D""") SCREAMING_SNAKE_CASE__ : Tuple = (32, 64, 128, 256) SCREAMING_SNAKE_CASE__ : Tuple = ("""UpResnetBlock1D""", """UpResnetBlock1D""", """UpResnetBlock1D""") SCREAMING_SNAKE_CASE__ : Dict = torch.load(f'''/Users/bglickenhaus/Documents/diffuser/temporal_unet-hopper-mediumv2-hor{hor}.torch''' ) SCREAMING_SNAKE_CASE__ : List[str] = model.state_dict() SCREAMING_SNAKE_CASE__ : str = { """down_block_types""": down_block_types, """block_out_channels""": block_out_channels, """up_block_types""": up_block_types, """layers_per_block""": 1, """use_timestep_embedding""": True, """out_block_type""": """OutConv1DBlock""", """norm_num_groups""": 8, """downsample_each_block""": False, """in_channels""": 14, """out_channels""": 14, """extra_in_channels""": 0, """time_embedding_type""": """positional""", """flip_sin_to_cos""": False, """freq_shift""": 1, """sample_size""": 65_536, """mid_block_type""": """MidResTemporalBlock1D""", """act_fn""": """mish""", } SCREAMING_SNAKE_CASE__ : Optional[Any] = UNetaDModel(**_snake_case ) print(f'''length of state dict: {len(state_dict.keys() )}''' ) print(f'''length of value function dict: {len(hf_value_function.state_dict().keys() )}''' ) SCREAMING_SNAKE_CASE__ : Optional[Any] = dict(zip(model.state_dict().keys() ,hf_value_function.state_dict().keys() ) ) for k, v in mapping.items(): SCREAMING_SNAKE_CASE__ : int = state_dict.pop(_snake_case ) hf_value_function.load_state_dict(_snake_case ) torch.save(hf_value_function.state_dict() ,f'''hub/hopper-medium-v2/unet/hor{hor}/diffusion_pytorch_model.bin''' ) with open(f'''hub/hopper-medium-v2/unet/hor{hor}/config.json''' ,"""w""" ) as f: json.dump(_snake_case ,_snake_case ) def lowercase_ ( ): SCREAMING_SNAKE_CASE__ : Dict = { """in_channels""": 14, """down_block_types""": ("""DownResnetBlock1D""", """DownResnetBlock1D""", """DownResnetBlock1D""", """DownResnetBlock1D"""), """up_block_types""": (), """out_block_type""": """ValueFunction""", """mid_block_type""": """ValueFunctionMidBlock1D""", """block_out_channels""": (32, 64, 128, 256), """layers_per_block""": 1, """downsample_each_block""": True, """sample_size""": 65_536, """out_channels""": 14, """extra_in_channels""": 0, """time_embedding_type""": """positional""", """use_timestep_embedding""": True, """flip_sin_to_cos""": False, """freq_shift""": 1, """norm_num_groups""": 8, """act_fn""": """mish""", } SCREAMING_SNAKE_CASE__ : Tuple = torch.load("""/Users/bglickenhaus/Documents/diffuser/value_function-hopper-mediumv2-hor32.torch""" ) SCREAMING_SNAKE_CASE__ : Union[str, Any] = model SCREAMING_SNAKE_CASE__ : str = UNetaDModel(**_snake_case ) print(f'''length of state dict: {len(state_dict.keys() )}''' ) print(f'''length of value function dict: {len(hf_value_function.state_dict().keys() )}''' ) SCREAMING_SNAKE_CASE__ : Optional[Any] = dict(zip(state_dict.keys() ,hf_value_function.state_dict().keys() ) ) for k, v in mapping.items(): SCREAMING_SNAKE_CASE__ : Optional[Any] = state_dict.pop(_snake_case ) hf_value_function.load_state_dict(_snake_case ) torch.save(hf_value_function.state_dict() ,"""hub/hopper-medium-v2/value_function/diffusion_pytorch_model.bin""" ) with open("""hub/hopper-medium-v2/value_function/config.json""" ,"""w""" ) as f: json.dump(_snake_case ,_snake_case ) if __name__ == "__main__": unet(3_2) # unet(128) value_function()
25
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 DeiTImageProcessor, ViTConfig, ViTForImageClassification, ViTImageProcessor, ViTModel from transformers.utils import logging logging.set_verbosity_info() A__ : Optional[Any] = logging.get_logger(__name__) def a ( lowerCamelCase_ , lowerCamelCase_=False ): '''simple docstring''' lowercase__ = [] for i in range(config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append((F"""blocks.{i}.norm1.weight""", F"""vit.encoder.layer.{i}.layernorm_before.weight""") ) rename_keys.append((F"""blocks.{i}.norm1.bias""", F"""vit.encoder.layer.{i}.layernorm_before.bias""") ) rename_keys.append((F"""blocks.{i}.attn.proj.weight""", F"""vit.encoder.layer.{i}.attention.output.dense.weight""") ) rename_keys.append((F"""blocks.{i}.attn.proj.bias""", F"""vit.encoder.layer.{i}.attention.output.dense.bias""") ) rename_keys.append((F"""blocks.{i}.norm2.weight""", F"""vit.encoder.layer.{i}.layernorm_after.weight""") ) rename_keys.append((F"""blocks.{i}.norm2.bias""", F"""vit.encoder.layer.{i}.layernorm_after.bias""") ) rename_keys.append((F"""blocks.{i}.mlp.fc1.weight""", F"""vit.encoder.layer.{i}.intermediate.dense.weight""") ) rename_keys.append((F"""blocks.{i}.mlp.fc1.bias""", F"""vit.encoder.layer.{i}.intermediate.dense.bias""") ) rename_keys.append((F"""blocks.{i}.mlp.fc2.weight""", F"""vit.encoder.layer.{i}.output.dense.weight""") ) rename_keys.append((F"""blocks.{i}.mlp.fc2.bias""", F"""vit.encoder.layer.{i}.output.dense.bias""") ) # projection layer + position embeddings rename_keys.extend( [ ('''cls_token''', '''vit.embeddings.cls_token'''), ('''patch_embed.proj.weight''', '''vit.embeddings.patch_embeddings.projection.weight'''), ('''patch_embed.proj.bias''', '''vit.embeddings.patch_embeddings.projection.bias'''), ('''pos_embed''', '''vit.embeddings.position_embeddings'''), ] ) if base_model: # layernorm + pooler rename_keys.extend( [ ('''norm.weight''', '''layernorm.weight'''), ('''norm.bias''', '''layernorm.bias'''), ('''pre_logits.fc.weight''', '''pooler.dense.weight'''), ('''pre_logits.fc.bias''', '''pooler.dense.bias'''), ] ) # if just the base model, we should remove "vit" from all keys that start with "vit" lowercase__ = [(pair[0], pair[1][4:]) if pair[1].startswith('''vit''' ) else pair for pair in rename_keys] else: # layernorm + classification head rename_keys.extend( [ ('''norm.weight''', '''vit.layernorm.weight'''), ('''norm.bias''', '''vit.layernorm.bias'''), ('''head.weight''', '''classifier.weight'''), ('''head.bias''', '''classifier.bias'''), ] ) return rename_keys def a ( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_=False ): '''simple docstring''' for i in range(config.num_hidden_layers ): if base_model: lowercase__ = '''''' else: lowercase__ = '''vit.''' # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) lowercase__ = state_dict.pop(F"""blocks.{i}.attn.qkv.weight""" ) lowercase__ = state_dict.pop(F"""blocks.{i}.attn.qkv.bias""" ) # next, add query, keys and values (in that order) to the state dict lowercase__ = in_proj_weight[ : config.hidden_size, : ] lowercase__ = in_proj_bias[: config.hidden_size] lowercase__ = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] lowercase__ = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] lowercase__ = in_proj_weight[ -config.hidden_size :, : ] lowercase__ = in_proj_bias[-config.hidden_size :] def a ( lowerCamelCase_ ): '''simple docstring''' lowercase__ = ['''head.weight''', '''head.bias'''] for k in ignore_keys: state_dict.pop(lowerCamelCase_ , lowerCamelCase_ ) def a ( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ): '''simple docstring''' lowercase__ = dct.pop(lowerCamelCase_ ) lowercase__ = val def a ( ): '''simple docstring''' lowercase__ = '''http://images.cocodataset.org/val2017/000000039769.jpg''' lowercase__ = Image.open(requests.get(lowerCamelCase_ , stream=lowerCamelCase_ ).raw ) return im @torch.no_grad() def a ( lowerCamelCase_ , lowerCamelCase_ ): '''simple docstring''' lowercase__ = ViTConfig() lowercase__ = False # dataset (ImageNet-21k only or also fine-tuned on ImageNet 2012), patch_size and image_size if vit_name[-5:] == "in21k": lowercase__ = True lowercase__ = int(vit_name[-12:-10] ) lowercase__ = int(vit_name[-9:-6] ) else: lowercase__ = 1000 lowercase__ = '''huggingface/label-files''' lowercase__ = '''imagenet-1k-id2label.json''' lowercase__ = json.load(open(hf_hub_download(lowerCamelCase_ , lowerCamelCase_ , repo_type='''dataset''' ) , '''r''' ) ) lowercase__ = {int(lowerCamelCase_ ): v for k, v in idalabel.items()} lowercase__ = idalabel lowercase__ = {v: k for k, v in idalabel.items()} lowercase__ = int(vit_name[-6:-4] ) lowercase__ = int(vit_name[-3:] ) # size of the architecture if "deit" in vit_name: if vit_name[9:].startswith('''tiny''' ): lowercase__ = 192 lowercase__ = 768 lowercase__ = 12 lowercase__ = 3 elif vit_name[9:].startswith('''small''' ): lowercase__ = 384 lowercase__ = 1536 lowercase__ = 12 lowercase__ = 6 else: pass else: if vit_name[4:].startswith('''small''' ): lowercase__ = 768 lowercase__ = 2304 lowercase__ = 8 lowercase__ = 8 elif vit_name[4:].startswith('''base''' ): pass elif vit_name[4:].startswith('''large''' ): lowercase__ = 1024 lowercase__ = 4096 lowercase__ = 24 lowercase__ = 16 elif vit_name[4:].startswith('''huge''' ): lowercase__ = 1280 lowercase__ = 5120 lowercase__ = 32 lowercase__ = 16 # load original model from timm lowercase__ = timm.create_model(lowerCamelCase_ , pretrained=lowerCamelCase_ ) timm_model.eval() # load state_dict of original model, remove and rename some keys lowercase__ = timm_model.state_dict() if base_model: remove_classification_head_(lowerCamelCase_ ) lowercase__ = create_rename_keys(lowerCamelCase_ , lowerCamelCase_ ) for src, dest in rename_keys: rename_key(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) read_in_q_k_v(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) # load HuggingFace model if vit_name[-5:] == "in21k": lowercase__ = ViTModel(lowerCamelCase_ ).eval() else: lowercase__ = ViTForImageClassification(lowerCamelCase_ ).eval() model.load_state_dict(lowerCamelCase_ ) # Check outputs on an image, prepared by ViTImageProcessor/DeiTImageProcessor if "deit" in vit_name: lowercase__ = DeiTImageProcessor(size=config.image_size ) else: lowercase__ = ViTImageProcessor(size=config.image_size ) lowercase__ = image_processor(images=prepare_img() , return_tensors='''pt''' ) lowercase__ = encoding['''pixel_values'''] lowercase__ = model(lowerCamelCase_ ) if base_model: lowercase__ = timm_model.forward_features(lowerCamelCase_ ) assert timm_pooled_output.shape == outputs.pooler_output.shape assert torch.allclose(lowerCamelCase_ , outputs.pooler_output , atol=1e-3 ) else: lowercase__ = timm_model(lowerCamelCase_ ) assert timm_logits.shape == outputs.logits.shape assert torch.allclose(lowerCamelCase_ , outputs.logits , atol=1e-3 ) Path(lowerCamelCase_ ).mkdir(exist_ok=lowerCamelCase_ ) print(F"""Saving model {vit_name} to {pytorch_dump_folder_path}""" ) model.save_pretrained(lowerCamelCase_ ) print(F"""Saving image processor to {pytorch_dump_folder_path}""" ) image_processor.save_pretrained(lowerCamelCase_ ) if __name__ == "__main__": A__ : int = argparse.ArgumentParser() # Required parameters parser.add_argument( '--vit_name', default='vit_base_patch16_224', type=str, help='Name of the ViT timm model you\'d like to convert.', ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model directory.' ) A__ : str = parser.parse_args() convert_vit_checkpoint(args.vit_name, args.pytorch_dump_folder_path)
207
0
"""simple docstring""" def lowerCamelCase__ (_UpperCAmelCase , _UpperCAmelCase): if density <= 0: raise ValueError('Impossible fluid density') if bulk_modulus <= 0: raise ValueError('Impossible bulk modulus') return (bulk_modulus / density) ** 0.5 if __name__ == "__main__": import doctest doctest.testmod()
353
from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import convert_to_rgb, normalize, rescale, resize, to_channel_dimension_format from ...image_utils import ( OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging if is_vision_available(): import PIL a_ : Optional[Any] = logging.get_logger(__name__) class _snake_case ( A__ ): _lowercase : Optional[int] = ['''pixel_values'''] def __init__( self , a = True , a = None , a = PILImageResampling.BICUBIC , a = True , a = 1 / 255 , a = True , a = None , a = None , a = True , **a , ) -> None: super().__init__(**a) SCREAMING_SNAKE_CASE = size if size is not None else {'height': 384, 'width': 384} SCREAMING_SNAKE_CASE = get_size_dict(a , default_to_square=a) SCREAMING_SNAKE_CASE = do_resize SCREAMING_SNAKE_CASE = size SCREAMING_SNAKE_CASE = resample SCREAMING_SNAKE_CASE = do_rescale SCREAMING_SNAKE_CASE = rescale_factor SCREAMING_SNAKE_CASE = do_normalize SCREAMING_SNAKE_CASE = image_mean if image_mean is not None else OPENAI_CLIP_MEAN SCREAMING_SNAKE_CASE = image_std if image_std is not None else OPENAI_CLIP_STD SCREAMING_SNAKE_CASE = do_convert_rgb def SCREAMING_SNAKE_CASE__ ( self , a , a , a = PILImageResampling.BICUBIC , a = None , **a , ) -> np.ndarray: SCREAMING_SNAKE_CASE = get_size_dict(a , default_to_square=a) 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()}''') SCREAMING_SNAKE_CASE = (size['height'], size['width']) return resize(a , size=a , resample=a , data_format=a , **a) def SCREAMING_SNAKE_CASE__ ( self , a , a , a = None , **a , ) -> Optional[Any]: return rescale(a , scale=a , data_format=a , **a) def SCREAMING_SNAKE_CASE__ ( self , a , a , a , a = None , **a , ) -> np.ndarray: return normalize(a , mean=a , std=a , data_format=a , **a) def SCREAMING_SNAKE_CASE__ ( self , a , a = None , a = None , a = None , a = None , a = None , a = None , a = None , a = None , a = None , a = None , a = ChannelDimension.FIRST , **a , ) -> PIL.Image.Image: SCREAMING_SNAKE_CASE = do_resize if do_resize is not None else self.do_resize SCREAMING_SNAKE_CASE = resample if resample is not None else self.resample SCREAMING_SNAKE_CASE = do_rescale if do_rescale is not None else self.do_rescale SCREAMING_SNAKE_CASE = rescale_factor if rescale_factor is not None else self.rescale_factor SCREAMING_SNAKE_CASE = do_normalize if do_normalize is not None else self.do_normalize SCREAMING_SNAKE_CASE = image_mean if image_mean is not None else self.image_mean SCREAMING_SNAKE_CASE = image_std if image_std is not None else self.image_std SCREAMING_SNAKE_CASE = do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb SCREAMING_SNAKE_CASE = size if size is not None else self.size SCREAMING_SNAKE_CASE = get_size_dict(a , default_to_square=a) SCREAMING_SNAKE_CASE = make_list_of_images(a) if not valid_images(a): raise ValueError( 'Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ' 'torch.Tensor, tf.Tensor or jax.ndarray.') if do_resize and size is None or resample is None: raise ValueError('Size and resample must be specified if do_resize is True.') if do_rescale and rescale_factor is None: raise ValueError('Rescale factor must be specified if do_rescale is True.') if do_normalize and (image_mean is None or image_std is None): raise ValueError('Image mean and std must be specified if do_normalize is True.') # PIL RGBA images are converted to RGB if do_convert_rgb: SCREAMING_SNAKE_CASE = [convert_to_rgb(a) for image in images] # All transformations expect numpy arrays. SCREAMING_SNAKE_CASE = [to_numpy_array(a) for image in images] if do_resize: SCREAMING_SNAKE_CASE = [self.resize(image=a , size=a , resample=a) for image in images] if do_rescale: SCREAMING_SNAKE_CASE = [self.rescale(image=a , scale=a) for image in images] if do_normalize: SCREAMING_SNAKE_CASE = [self.normalize(image=a , mean=a , std=a) for image in images] SCREAMING_SNAKE_CASE = [to_channel_dimension_format(a , a) for image in images] SCREAMING_SNAKE_CASE = BatchFeature(data={'pixel_values': images} , tensor_type=a) return encoded_outputs
327
0
"""simple docstring""" import argparse import torch from diffusers.pipelines.stable_diffusion.convert_from_ckpt import download_from_original_stable_diffusion_ckpt if __name__ == "__main__": lowercase__ = argparse.ArgumentParser() parser.add_argument( """--checkpoint_path""", default=None, type=str, required=True, help="""Path to the checkpoint to convert.""" ) # !wget https://raw.githubusercontent.com/CompVis/stable-diffusion/main/configs/stable-diffusion/v1-inference.yaml parser.add_argument( """--original_config_file""", default=None, type=str, help="""The YAML config file corresponding to the original architecture.""", ) parser.add_argument( """--num_in_channels""", default=None, type=int, help="""The number of input channels. If `None` number of input channels will be automatically inferred.""", ) parser.add_argument( """--scheduler_type""", default="""pndm""", type=str, help="""Type of scheduler to use. Should be one of ['pndm', 'lms', 'ddim', 'euler', 'euler-ancestral', 'dpm']""", ) parser.add_argument( """--pipeline_type""", default=None, type=str, help=( """The pipeline type. One of 'FrozenOpenCLIPEmbedder', 'FrozenCLIPEmbedder', 'PaintByExample'""" """. If `None` pipeline will be automatically inferred.""" ), ) parser.add_argument( """--image_size""", default=None, type=int, help=( """The image size that the model was trained on. Use 512 for Stable Diffusion v1.X and Stable Siffusion v2""" """ Base. Use 768 for Stable Diffusion v2.""" ), ) parser.add_argument( """--prediction_type""", default=None, type=str, help=( """The prediction type that the model was trained on. Use 'epsilon' for Stable Diffusion v1.X and Stable""" """ Diffusion v2 Base. Use 'v_prediction' for Stable Diffusion v2.""" ), ) parser.add_argument( """--extract_ema""", action="""store_true""", help=( """Only relevant for checkpoints that have both EMA and non-EMA weights. Whether to extract the EMA weights""" """ or not. Defaults to `False`. Add `--extract_ema` to extract the EMA weights. EMA weights usually yield""" """ higher quality images for inference. Non-EMA weights are usually better to continue fine-tuning.""" ), ) parser.add_argument( """--upcast_attention""", action="""store_true""", help=( """Whether the attention computation should always be upcasted. This is necessary when running stable""" """ diffusion 2.1.""" ), ) parser.add_argument( """--from_safetensors""", action="""store_true""", help="""If `--checkpoint_path` is in `safetensors` format, load checkpoint with safetensors instead of PyTorch.""", ) parser.add_argument( """--to_safetensors""", action="""store_true""", help="""Whether to store pipeline in safetensors format or not.""", ) parser.add_argument("""--dump_path""", default=None, type=str, required=True, help="""Path to the output model.""") parser.add_argument("""--device""", type=str, help="""Device to use (e.g. cpu, cuda:0, cuda:1, etc.)""") parser.add_argument( """--stable_unclip""", type=str, default=None, required=False, help="""Set if this is a stable unCLIP model. One of 'txt2img' or 'img2img'.""", ) parser.add_argument( """--stable_unclip_prior""", type=str, default=None, required=False, help="""Set if this is a stable unCLIP txt2img model. Selects which prior to use. If `--stable_unclip` is set to `txt2img`, the karlo prior (https://huggingface.co/kakaobrain/karlo-v1-alpha/tree/main/prior) is selected by default.""", ) parser.add_argument( """--clip_stats_path""", type=str, help="""Path to the clip stats file. Only required if the stable unclip model's config specifies `model.params.noise_aug_config.params.clip_stats_path`.""", required=False, ) parser.add_argument( """--controlnet""", action="""store_true""", default=None, help="""Set flag if this is a controlnet checkpoint.""" ) parser.add_argument("""--half""", action="""store_true""", help="""Save weights in half precision.""") parser.add_argument( """--vae_path""", type=str, default=None, required=False, help="""Set to a path, hub id to an already converted vae to not convert it again.""", ) lowercase__ = parser.parse_args() lowercase__ = download_from_original_stable_diffusion_ckpt( checkpoint_path=args.checkpoint_path, original_config_file=args.original_config_file, image_size=args.image_size, prediction_type=args.prediction_type, model_type=args.pipeline_type, extract_ema=args.extract_ema, scheduler_type=args.scheduler_type, num_in_channels=args.num_in_channels, upcast_attention=args.upcast_attention, from_safetensors=args.from_safetensors, device=args.device, stable_unclip=args.stable_unclip, stable_unclip_prior=args.stable_unclip_prior, clip_stats_path=args.clip_stats_path, controlnet=args.controlnet, vae_path=args.vae_path, ) if args.half: pipe.to(torch_dtype=torch.floataa) if args.controlnet: # only save the controlnet model pipe.controlnet.save_pretrained(args.dump_path, safe_serialization=args.to_safetensors) else: pipe.save_pretrained(args.dump_path, safe_serialization=args.to_safetensors)
96
"""simple docstring""" import socket def _snake_case ( ): _lowerCamelCase : List[Any] = socket.socket(socket.AF_INET , socket.SOCK_STREAM ) _lowerCamelCase : Union[str, Any] = socket.gethostname() _lowerCamelCase : List[Any] = 12312 sock.connect((host, port) ) sock.send(B'Hello server!' ) with open('Received_file' , 'wb' ) as out_file: print('File opened' ) print('Receiving data...' ) while True: _lowerCamelCase : int = sock.recv(1024 ) if not data: break out_file.write(lowercase__ ) print('Successfully received the file' ) sock.close() print('Connection closed' ) if __name__ == "__main__": main()
96
1
from __future__ import annotations from collections.abc import Sequence from typing import Literal def SCREAMING_SNAKE_CASE__ ( snake_case_, snake_case_ ) -> str | Literal[False]: """simple docstring""" a = list(snake_case_ ) a = list(snake_case_ ) a = 0 for i in range(len(snake_case_ ) ): if lista[i] != lista[i]: count += 1 a = '''_''' if count > 1: return False else: return "".join(snake_case_ ) def SCREAMING_SNAKE_CASE__ ( snake_case_ ) -> list[str]: """simple docstring""" a = [] while True: a = ['''$'''] * len(snake_case_ ) a = [] for i in range(len(snake_case_ ) ): for j in range(i + 1, len(snake_case_ ) ): a = compare_string(binary[i], binary[j] ) if k is False: a = '''*''' a = '''*''' temp.append('''X''' ) for i in range(len(snake_case_ ) ): if checka[i] == "$": pi.append(binary[i] ) if len(snake_case_ ) == 0: return pi a = list(set(snake_case_ ) ) def SCREAMING_SNAKE_CASE__ ( snake_case_, snake_case_ ) -> list[str]: """simple docstring""" a = [] for minterm in minterms: a = '''''' for _ in range(snake_case_ ): a = str(minterm % 2 ) + string minterm //= 2 temp.append(snake_case_ ) return temp def SCREAMING_SNAKE_CASE__ ( snake_case_, snake_case_, snake_case_ ) -> bool: """simple docstring""" a = list(snake_case_ ) a = list(snake_case_ ) a = 0 for i in range(len(snake_case_ ) ): if lista[i] != lista[i]: count_n += 1 return count_n == count def SCREAMING_SNAKE_CASE__ ( snake_case_, snake_case_ ) -> list[str]: """simple docstring""" a = [] a = [0] * len(snake_case_ ) for i in range(len(chart[0] ) ): a = 0 a = -1 for j in range(len(snake_case_ ) ): if chart[j][i] == 1: count += 1 a = j if count == 1: a = 1 for i in range(len(snake_case_ ) ): if select[i] == 1: for j in range(len(chart[0] ) ): if chart[i][j] == 1: for k in range(len(snake_case_ ) ): a = 0 temp.append(prime_implicants[i] ) while True: a = 0 a = -1 a = 0 for i in range(len(snake_case_ ) ): a = chart[i].count(1 ) if count_n > max_n: a = count_n a = i if max_n == 0: return temp temp.append(prime_implicants[rem] ) for i in range(len(chart[0] ) ): if chart[rem][i] == 1: for j in range(len(snake_case_ ) ): a = 0 def SCREAMING_SNAKE_CASE__ ( snake_case_, snake_case_ ) -> list[list[int]]: """simple docstring""" a = [[0 for x in range(len(snake_case_ ) )] for x in range(len(snake_case_ ) )] for i in range(len(snake_case_ ) ): a = prime_implicants[i].count('''_''' ) for j in range(len(snake_case_ ) ): if is_for_table(prime_implicants[i], binary[j], snake_case_ ): a = 1 return chart def SCREAMING_SNAKE_CASE__ ( ) -> None: """simple docstring""" a = int(input('''Enter the no. of variables\n''' ) ) a = [ float(snake_case_ ) for x in input( '''Enter the decimal representation of Minterms \'Spaces Separated\'\n''' ).split() ] a = decimal_to_binary(snake_case_, snake_case_ ) a = check(snake_case_ ) print('''Prime Implicants are:''' ) print(snake_case_ ) a = prime_implicant_chart(snake_case_, snake_case_ ) a = selection(snake_case_, snake_case_ ) print('''Essential Prime Implicants are:''' ) print(snake_case_ ) if __name__ == "__main__": import doctest doctest.testmod() main()
366
import argparse from typing import Dict import tensorflow as tf import torch from tqdm import tqdm from transformers import BigBirdPegasusConfig, BigBirdPegasusForConditionalGeneration UpperCamelCase__ : Any = [ # tf -> hf ("""/""", """."""), ("""layer_""", """layers."""), ("""kernel""", """weight"""), ("""beta""", """bias"""), ("""gamma""", """weight"""), ("""pegasus""", """model"""), ] UpperCamelCase__ : Optional[Any] = [ (""".output.dense""", """.fc2"""), ("""intermediate.LayerNorm""", """final_layer_norm"""), ("""intermediate.dense""", """fc1"""), ] UpperCamelCase__ : Optional[Any] = ( INIT_COMMON + [ ("""attention.self.LayerNorm""", """self_attn_layer_norm"""), ("""attention.output.dense""", """self_attn.out_proj"""), ("""attention.self""", """self_attn"""), ("""attention.encdec.LayerNorm""", """encoder_attn_layer_norm"""), ("""attention.encdec_output.dense""", """encoder_attn.out_proj"""), ("""attention.encdec""", """encoder_attn"""), ("""key""", """k_proj"""), ("""value""", """v_proj"""), ("""query""", """q_proj"""), ("""decoder.LayerNorm""", """decoder.layernorm_embedding"""), ] + END_COMMON ) UpperCamelCase__ : List[str] = ( INIT_COMMON + [ ("""embeddings.word_embeddings""", """shared.weight"""), ("""embeddings.position_embeddings""", """embed_positions.weight"""), ("""attention.self.LayerNorm""", """self_attn_layer_norm"""), ("""attention.output.dense""", """self_attn.output"""), ("""attention.self""", """self_attn.self"""), ("""encoder.LayerNorm""", """encoder.layernorm_embedding"""), ] + END_COMMON ) UpperCamelCase__ : Optional[int] = [ """encdec/key/bias""", """encdec/query/bias""", """encdec/value/bias""", """self/key/bias""", """self/query/bias""", """self/value/bias""", """encdec_output/dense/bias""", """attention/output/dense/bias""", ] def SCREAMING_SNAKE_CASE__ ( snake_case_, snake_case_ ) -> List[Any]: """simple docstring""" for tf_name, hf_name in patterns: a = k.replace(snake_case_, snake_case_ ) return k def SCREAMING_SNAKE_CASE__ ( snake_case_, snake_case_ ) -> BigBirdPegasusForConditionalGeneration: """simple docstring""" a = BigBirdPegasusConfig(**snake_case_ ) a = BigBirdPegasusForConditionalGeneration(snake_case_ ) a = torch_model.state_dict() a = {} # separating decoder weights a = {k: tf_weights[k] for k in tf_weights if k.startswith('''pegasus/decoder''' )} a = {k: tf_weights[k] for k in tf_weights if not k.startswith('''pegasus/decoder''' )} for k, v in tqdm(decoder_weights.items(), '''tf -> hf conversion''' ): a = [k.endswith(snake_case_ ) for ending in KEYS_TO_IGNORE] if any(snake_case_ ): continue a = DECODER_PATTERNS a = rename_state_dict_key(snake_case_, snake_case_ ) if new_k not in state_dict: raise ValueError(f"""could not find new key {new_k} in state dict. (converted from {k})""" ) if any(True if i in k else False for i in ['''dense''', '''query''', '''key''', '''value'''] ): a = v.T a = torch.from_numpy(snake_case_ ) assert v.shape == state_dict[new_k].shape, f"""{new_k}, {k}, {v.shape}, {state_dict[new_k].shape}""" for k, v in tqdm(remaining_weights.items(), '''tf -> hf conversion''' ): a = [k.endswith(snake_case_ ) for ending in KEYS_TO_IGNORE] if any(snake_case_ ): continue a = REMAINING_PATTERNS a = rename_state_dict_key(snake_case_, snake_case_ ) if new_k not in state_dict and k != "pegasus/embeddings/position_embeddings": raise ValueError(f"""could not find new key {new_k} in state dict. (converted from {k})""" ) if any(True if i in k else False for i in ['''dense''', '''query''', '''key''', '''value'''] ): a = v.T a = torch.from_numpy(snake_case_ ) if k != "pegasus/embeddings/position_embeddings": assert v.shape == state_dict[new_k].shape, f"""{new_k}, {k}, {v.shape}, {state_dict[new_k].shape}""" a = mapping['''model.embed_positions.weight'''] a = mapping.pop('''model.embed_positions.weight''' ) a , a = torch_model.load_state_dict(snake_case_, strict=snake_case_ ) a = [ k for k in missing if k not in [ '''final_logits_bias''', '''model.encoder.embed_tokens.weight''', '''model.decoder.embed_tokens.weight''', '''lm_head.weight''', ] ] assert unexpected_missing == [], f"""no matches found for the following torch keys {unexpected_missing}""" assert extra == [], f"""no matches found for the following tf keys {extra}""" return torch_model def SCREAMING_SNAKE_CASE__ ( snake_case_ ) -> Dict: """simple docstring""" a = tf.train.list_variables(snake_case_ ) a = {} a = ['''global_step'''] for name, shape in tqdm(snake_case_, desc='''converting tf checkpoint to dict''' ): a = any(pat in name for pat in ignore_name ) if skip_key: continue a = tf.train.load_variable(snake_case_, snake_case_ ) a = array return tf_weights def SCREAMING_SNAKE_CASE__ ( snake_case_, snake_case_, snake_case_ ) -> int: """simple docstring""" a = get_tf_weights_as_numpy(snake_case_ ) a = convert_bigbird_pegasus(snake_case_, snake_case_ ) torch_model.save_pretrained(snake_case_ ) if __name__ == "__main__": UpperCamelCase__ : str = argparse.ArgumentParser() parser.add_argument("""--tf_ckpt_path""", type=str, help="""passed to tf.train.list_variables""") parser.add_argument("""--save_dir""", default=None, type=str, help="""Path to the output PyTorch model.""") UpperCamelCase__ : int = parser.parse_args() UpperCamelCase__ : Tuple = {} convert_bigbird_pegasus_ckpt_to_pytorch(args.tf_ckpt_path, args.save_dir, config_update=config_update)
330
0
def _A ( _lowercase , _lowercase ) -> str: """simple docstring""" if not (isinstance(_lowercase , _lowercase ) and isinstance(_lowercase , _lowercase )): raise ValueError('longest_common_substring() takes two strings for inputs' ) __UpperCamelCase = len(_lowercase ) __UpperCamelCase = len(_lowercase ) __UpperCamelCase = [[0] * (texta_length + 1) for _ in range(texta_length + 1 )] __UpperCamelCase = 0 __UpperCamelCase = 0 for i in range(1 , texta_length + 1 ): for j in range(1 , texta_length + 1 ): if texta[i - 1] == texta[j - 1]: __UpperCamelCase = 1 + dp[i - 1][j - 1] if dp[i][j] > ans_length: __UpperCamelCase = i __UpperCamelCase = dp[i][j] return texta[ans_index - ans_length : ans_index] if __name__ == "__main__": import doctest doctest.testmod()
310
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging __snake_case = logging.get_logger(__name__) __snake_case = { '''xlm-roberta-base''': '''https://huggingface.co/xlm-roberta-base/resolve/main/config.json''', '''xlm-roberta-large''': '''https://huggingface.co/xlm-roberta-large/resolve/main/config.json''', '''xlm-roberta-large-finetuned-conll02-dutch''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll02-dutch/resolve/main/config.json''' ), '''xlm-roberta-large-finetuned-conll02-spanish''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll02-spanish/resolve/main/config.json''' ), '''xlm-roberta-large-finetuned-conll03-english''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll03-english/resolve/main/config.json''' ), '''xlm-roberta-large-finetuned-conll03-german''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll03-german/resolve/main/config.json''' ), } class __lowerCamelCase (_a ): _lowercase = """xlm-roberta""" def __init__( self: Union[str, Any],A_: Union[str, Any]=3_0522,A_: Dict=768,A_: Union[str, Any]=12,A_: Any=12,A_: str=3072,A_: Union[str, Any]="gelu",A_: str=0.1,A_: Optional[int]=0.1,A_: List[Any]=512,A_: Optional[Any]=2,A_: Dict=0.0_2,A_: List[Any]=1E-12,A_: Optional[int]=1,A_: str=0,A_: str=2,A_: Optional[Any]="absolute",A_: Union[str, Any]=True,A_: int=None,**A_: Optional[Any],): '''simple docstring''' super().__init__(pad_token_id=A_,bos_token_id=A_,eos_token_id=A_,**A_ ) __UpperCamelCase = vocab_size __UpperCamelCase = hidden_size __UpperCamelCase = num_hidden_layers __UpperCamelCase = num_attention_heads __UpperCamelCase = hidden_act __UpperCamelCase = intermediate_size __UpperCamelCase = hidden_dropout_prob __UpperCamelCase = attention_probs_dropout_prob __UpperCamelCase = max_position_embeddings __UpperCamelCase = type_vocab_size __UpperCamelCase = initializer_range __UpperCamelCase = layer_norm_eps __UpperCamelCase = position_embedding_type __UpperCamelCase = use_cache __UpperCamelCase = classifier_dropout class __lowerCamelCase (_a ): @property def snake_case_ ( self: Optional[Any] ): '''simple docstring''' if self.task == "multiple-choice": __UpperCamelCase = {0: 'batch', 1: 'choice', 2: 'sequence'} else: __UpperCamelCase = {0: 'batch', 1: 'sequence'} return OrderedDict( [ ('input_ids', dynamic_axis), ('attention_mask', dynamic_axis), ] )
310
1
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices A__ : Optional[int] = logging.get_logger(__name__) A__ : Any = { 'shi-labs/dinat-mini-in1k-224': 'https://huggingface.co/shi-labs/dinat-mini-in1k-224/resolve/main/config.json', # See all Dinat models at https://huggingface.co/models?filter=dinat } class lowercase__ ( snake_case__, snake_case__ ): _UpperCAmelCase :Tuple = "dinat" _UpperCAmelCase :int = { "num_attention_heads": "num_heads", "num_hidden_layers": "num_layers", } def __init__( self : List[str] , snake_case__ : Any=4 , snake_case__ : str=3 , snake_case__ : int=64 , snake_case__ : Union[str, Any]=[3, 4, 6, 5] , snake_case__ : Dict=[2, 4, 8, 16] , snake_case__ : Dict=7 , snake_case__ : str=[[1, 8, 1], [1, 4, 1, 4], [1, 2, 1, 2, 1, 2], [1, 1, 1, 1, 1]] , snake_case__ : Optional[int]=3.0 , snake_case__ : Any=True , snake_case__ : List[str]=0.0 , snake_case__ : List[Any]=0.0 , snake_case__ : List[str]=0.1 , snake_case__ : Union[str, Any]="gelu" , snake_case__ : Tuple=0.02 , snake_case__ : str=1E-5 , snake_case__ : Optional[Any]=0.0 , snake_case__ : int=None , snake_case__ : Tuple=None , **snake_case__ : str , ): super().__init__(**snake_case__ ) lowerCamelCase_ : Tuple =patch_size lowerCamelCase_ : int =num_channels lowerCamelCase_ : Tuple =embed_dim lowerCamelCase_ : str =depths lowerCamelCase_ : Dict =len(snake_case__ ) lowerCamelCase_ : str =num_heads lowerCamelCase_ : Optional[int] =kernel_size lowerCamelCase_ : Optional[Any] =dilations lowerCamelCase_ : Optional[Any] =mlp_ratio lowerCamelCase_ : Tuple =qkv_bias lowerCamelCase_ : Union[str, Any] =hidden_dropout_prob lowerCamelCase_ : Optional[Any] =attention_probs_dropout_prob lowerCamelCase_ : int =drop_path_rate lowerCamelCase_ : List[Any] =hidden_act lowerCamelCase_ : Union[str, Any] =layer_norm_eps lowerCamelCase_ : Tuple =initializer_range # we set the hidden_size attribute in order to make Dinat work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model lowerCamelCase_ : Union[str, Any] =int(embed_dim * 2 ** (len(snake_case__ ) - 1) ) lowerCamelCase_ : Optional[Any] =layer_scale_init_value lowerCamelCase_ : Union[str, Any] =["stem"] + [F"""stage{idx}""" for idx in range(1 , len(snake_case__ ) + 1 )] lowerCamelCase_ , lowerCamelCase_ : Optional[Any] =get_aligned_output_features_output_indices( out_features=snake_case__ , out_indices=snake_case__ , stage_names=self.stage_names )
209
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available A__ : List[Any] = { 'configuration_lilt': ['LILT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'LiltConfig'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A__ : List[str] = [ 'LILT_PRETRAINED_MODEL_ARCHIVE_LIST', 'LiltForQuestionAnswering', 'LiltForSequenceClassification', 'LiltForTokenClassification', 'LiltModel', 'LiltPreTrainedModel', ] if TYPE_CHECKING: from .configuration_lilt import LILT_PRETRAINED_CONFIG_ARCHIVE_MAP, LiltConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_lilt import ( LILT_PRETRAINED_MODEL_ARCHIVE_LIST, LiltForQuestionAnswering, LiltForSequenceClassification, LiltForTokenClassification, LiltModel, LiltPreTrainedModel, ) else: import sys A__ : Optional[Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
209
1
'''simple docstring''' import argparse import os from pathlib import Path import torch from bark.generation import _load_model as _bark_load_model from huggingface_hub import hf_hub_download from transformers import EncodecConfig, EncodecModel, set_seed from transformers.models.bark.configuration_bark import ( BarkCoarseConfig, BarkConfig, BarkFineConfig, BarkSemanticConfig, ) from transformers.models.bark.generation_configuration_bark import ( BarkCoarseGenerationConfig, BarkFineGenerationConfig, BarkGenerationConfig, BarkSemanticGenerationConfig, ) from transformers.models.bark.modeling_bark import BarkCoarseModel, BarkFineModel, BarkModel, BarkSemanticModel from transformers.utils import logging logging.set_verbosity_info() lowerCAmelCase_ : Dict = logging.get_logger(__name__) set_seed(7_70) lowerCAmelCase_ : Optional[Any] = { 'c_attn': 'att_proj', 'c_proj': 'out_proj', 'c_fc': 'in_proj', 'transformer.': '', 'h.': 'layers.', 'ln_1': 'layernorm_1', 'ln_2': 'layernorm_2', 'ln_f': 'layernorm_final', 'wpe': 'position_embeds_layer', 'wte': 'input_embeds_layer', } lowerCAmelCase_ : str = { 'text_small': { 'repo_id': 'suno/bark', 'file_name': 'text.pt', }, 'coarse_small': { 'repo_id': 'suno/bark', 'file_name': 'coarse.pt', }, 'fine_small': { 'repo_id': 'suno/bark', 'file_name': 'fine.pt', }, 'text': { 'repo_id': 'suno/bark', 'file_name': 'text_2.pt', }, 'coarse': { 'repo_id': 'suno/bark', 'file_name': 'coarse_2.pt', }, 'fine': { 'repo_id': 'suno/bark', 'file_name': 'fine_2.pt', }, } lowerCAmelCase_ : Union[str, Any] = os.path.dirname(os.path.abspath(__file__)) lowerCAmelCase_ : Tuple = os.path.join(os.path.expanduser('~'), '.cache') lowerCAmelCase_ : Optional[int] = os.path.join(os.getenv('XDG_CACHE_HOME', default_cache_dir), 'suno', 'bark_v0') def _lowerCamelCase ( lowercase : List[Any] , lowercase : Dict=False ) -> Any: _a = model_type if use_small: key += "_small" return os.path.join(lowercase__ , REMOTE_MODEL_PATHS[key]["file_name"] ) def _lowerCamelCase ( lowercase : str , lowercase : str ) -> List[Any]: os.makedirs(lowercase__ , exist_ok=lowercase__ ) hf_hub_download(repo_id=lowercase__ , filename=lowercase__ , local_dir=lowercase__ ) def _lowerCamelCase ( lowercase : Optional[Any] , lowercase : Tuple , lowercase : Union[str, Any]=False , lowercase : Any="text" ) -> Any: if model_type == "text": _a = BarkSemanticModel _a = BarkSemanticConfig _a = BarkSemanticGenerationConfig elif model_type == "coarse": _a = BarkCoarseModel _a = BarkCoarseConfig _a = BarkCoarseGenerationConfig elif model_type == "fine": _a = BarkFineModel _a = BarkFineConfig _a = BarkFineGenerationConfig else: raise NotImplementedError() _a = F'{model_type}_small' if use_small else model_type _a = REMOTE_MODEL_PATHS[model_key] if not os.path.exists(lowercase__ ): logger.info(F'{model_type} model not found, downloading into `{CACHE_DIR}`.' ) _download(model_info["repo_id"] , model_info["file_name"] ) _a = torch.load(lowercase__ , map_location=lowercase__ ) # this is a hack _a = checkpoint['''model_args'''] if "input_vocab_size" not in model_args: _a = model_args['''vocab_size'''] _a = model_args['''vocab_size'''] del model_args["vocab_size"] # convert Bark model arguments to HF Bark model arguments _a = model_args.pop("n_head" ) _a = model_args.pop("n_embd" ) _a = model_args.pop("n_layer" ) _a = ConfigClass(**checkpoint["model_args"] ) _a = ModelClass(config=lowercase__ ) _a = GenerationConfigClass() _a = model_generation_config _a = checkpoint['''model'''] # fixup checkpoint _a = '''_orig_mod.''' for k, v in list(state_dict.items() ): if k.startswith(lowercase__ ): # replace part of the key with corresponding layer name in HF implementation _a = k[len(lowercase__ ) :] for old_layer_name in new_layer_name_dict: _a = new_k.replace(lowercase__ , new_layer_name_dict[old_layer_name] ) _a = state_dict.pop(lowercase__ ) _a = set(state_dict.keys() ) - set(model.state_dict().keys() ) _a = {k for k in extra_keys if not k.endswith(".attn.bias" )} _a = set(model.state_dict().keys() ) - set(state_dict.keys() ) _a = {k for k in missing_keys if not k.endswith(".attn.bias" )} if len(lowercase__ ) != 0: raise ValueError(F'extra keys found: {extra_keys}' ) if len(lowercase__ ) != 0: raise ValueError(F'missing keys: {missing_keys}' ) model.load_state_dict(lowercase__ , strict=lowercase__ ) _a = model.num_parameters(exclude_embeddings=lowercase__ ) _a = checkpoint['''best_val_loss'''].item() logger.info(F'model loaded: {round(n_params/1E6 , 1 )}M params, {round(lowercase__ , 3 )} loss' ) model.eval() model.to(lowercase__ ) del checkpoint, state_dict return model def _lowerCamelCase ( lowercase : Union[str, Any] , lowercase : Tuple=False , lowercase : Any="text" ) -> List[str]: if model_type not in ("text", "coarse", "fine"): raise NotImplementedError() _a = '''cpu''' # do conversion on cpu _a = _get_ckpt_path(lowercase__ , use_small=lowercase__ ) _a = _load_model(lowercase__ , lowercase__ , model_type=lowercase__ , use_small=lowercase__ ) # load bark initial model _a = _bark_load_model(lowercase__ , "cpu" , model_type=lowercase__ , use_small=lowercase__ ) if model_type == "text": _a = bark_model['''model'''] if model.num_parameters(exclude_embeddings=lowercase__ ) != bark_model.get_num_params(): raise ValueError("initial and new models don\'t have the same number of parameters" ) # check if same output as the bark model _a = 5 _a = 10 if model_type in ["text", "coarse"]: _a = torch.randint(256 , (batch_size, sequence_length) , dtype=torch.int ) _a = bark_model(lowercase__ )[0] _a = model(lowercase__ ) # take last logits _a = output_new_model_total.logits[:, [-1], :] else: _a = 3 _a = 8 _a = torch.randint(256 , (batch_size, sequence_length, n_codes_total) , dtype=torch.int ) _a = model(lowercase__ , lowercase__ ) _a = bark_model(lowercase__ , lowercase__ ) _a = output_new_model_total.logits # output difference should come from the difference of self-attention implementation design if output_new_model.shape != output_old_model.shape: raise ValueError("initial and new outputs don\'t have the same shape" ) if (output_new_model - output_old_model).abs().max().item() > 1E-3: raise ValueError("initial and new outputs are not equal" ) Path(lowercase__ ).mkdir(exist_ok=lowercase__ ) model.save_pretrained(lowercase__ ) def _lowerCamelCase ( lowercase : List[str] , lowercase : Optional[int] , lowercase : Optional[int] , lowercase : Any , lowercase : Optional[Any] , lowercase : Union[str, Any] , ) -> Optional[int]: _a = os.path.join(lowercase__ , lowercase__ ) _a = BarkSemanticConfig.from_pretrained(os.path.join(lowercase__ , "config.json" ) ) _a = BarkCoarseConfig.from_pretrained(os.path.join(lowercase__ , "config.json" ) ) _a = BarkFineConfig.from_pretrained(os.path.join(lowercase__ , "config.json" ) ) _a = EncodecConfig.from_pretrained("facebook/encodec_24khz" ) _a = BarkSemanticModel.from_pretrained(lowercase__ ) _a = BarkCoarseModel.from_pretrained(lowercase__ ) _a = BarkFineModel.from_pretrained(lowercase__ ) _a = EncodecModel.from_pretrained("facebook/encodec_24khz" ) _a = BarkConfig.from_sub_model_configs( lowercase__ , lowercase__ , lowercase__ , lowercase__ ) _a = BarkGenerationConfig.from_sub_model_configs( semantic.generation_config , coarseAcoustic.generation_config , fineAcoustic.generation_config ) _a = BarkModel(lowercase__ ) _a = semantic _a = coarseAcoustic _a = fineAcoustic _a = codec _a = bark_generation_config Path(lowercase__ ).mkdir(exist_ok=lowercase__ ) bark.save_pretrained(lowercase__ , repo_id=lowercase__ , push_to_hub=lowercase__ ) if __name__ == "__main__": lowerCAmelCase_ : Union[str, Any] = argparse.ArgumentParser() # Required parameters parser.add_argument('model_type', type=str, help='text, coarse or fine.') parser.add_argument('pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.') parser.add_argument('--is_small', action='store_true', help='convert the small version instead of the large.') lowerCAmelCase_ : List[Any] = parser.parse_args() load_model(args.pytorch_dump_folder_path, model_type=args.model_type, use_small=args.is_small)
63
def _lowercase ( lowercase__ ): if not all(x.isalpha() for x in string ): raise ValueError('''String must only contain alphabetic characters.''' ) __lowerCAmelCase : int = sorted(string.lower() ) return len(lowercase__ ) == len(set(lowercase__ ) ) if __name__ == "__main__": _UpperCamelCase = input("Enter a string ").strip() _UpperCamelCase = is_isogram(input_str) print(F"{input_str} is {'an' if isogram else 'not an'} isogram.")
275
0
"""simple docstring""" import json import os import shutil 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 AutoConfig, BertConfig, GPTaConfig from transformers.configuration_utils import PretrainedConfig from transformers.testing_utils import TOKEN, USER, is_staging_test sys.path.append(str(Path(__file__).parent.parent / "utils")) from test_module.custom_configuration import CustomConfig # noqa E402 __A : Tuple = { "return_dict": False, "output_hidden_states": True, "output_attentions": True, "torchscript": True, "torch_dtype": "float16", "use_bfloat16": True, "tf_legacy_loss": True, "pruned_heads": {"a": 1}, "tie_word_embeddings": False, "is_decoder": True, "cross_attention_hidden_size": 128, "add_cross_attention": True, "tie_encoder_decoder": True, "max_length": 50, "min_length": 3, "do_sample": True, "early_stopping": True, "num_beams": 3, "num_beam_groups": 3, "diversity_penalty": 0.5, "temperature": 2.0, "top_k": 10, "top_p": 0.7, "typical_p": 0.2, "repetition_penalty": 0.8, "length_penalty": 0.8, "no_repeat_ngram_size": 5, "encoder_no_repeat_ngram_size": 5, "bad_words_ids": [1, 2, 3], "num_return_sequences": 3, "chunk_size_feed_forward": 5, "output_scores": True, "return_dict_in_generate": True, "forced_bos_token_id": 2, "forced_eos_token_id": 3, "remove_invalid_values": True, "architectures": ["BertModel"], "finetuning_task": "translation", "id2label": {0: "label"}, "label2id": {"label": "0"}, "tokenizer_class": "BertTokenizerFast", "prefix": "prefix", "bos_token_id": 6, "pad_token_id": 7, "eos_token_id": 8, "sep_token_id": 9, "decoder_start_token_id": 10, "exponential_decay_length_penalty": (5, 1.01), "suppress_tokens": [0, 1], "begin_suppress_tokens": 2, "task_specific_params": {"translation": "some_params"}, "problem_type": "regression", } @is_staging_test class _a ( unittest.TestCase): """simple docstring""" @classmethod def lowercase__ ( cls : Optional[Any] )->List[str]: _UpperCAmelCase = TOKEN HfFolder.save_token(__UpperCamelCase ) @classmethod def lowercase__ ( cls : str )->str: try: delete_repo(token=cls._token , repo_id='''test-config''' ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id='''valid_org/test-config-org''' ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id='''test-dynamic-config''' ) except HTTPError: pass def lowercase__ ( self : Union[str, Any] )->str: _UpperCAmelCase = BertConfig( vocab_size=9_9 , hidden_size=3_2 , num_hidden_layers=5 , num_attention_heads=4 , intermediate_size=3_7 ) config.push_to_hub('''test-config''' , use_auth_token=self._token ) _UpperCAmelCase = BertConfig.from_pretrained(F'{USER}/test-config' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(__UpperCamelCase , getattr(__UpperCamelCase , __UpperCamelCase ) ) # Reset repo delete_repo(token=self._token , repo_id='''test-config''' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained(__UpperCamelCase , repo_id='''test-config''' , push_to_hub=__UpperCamelCase , use_auth_token=self._token ) _UpperCAmelCase = BertConfig.from_pretrained(F'{USER}/test-config' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(__UpperCamelCase , getattr(__UpperCamelCase , __UpperCamelCase ) ) def lowercase__ ( self : str )->int: _UpperCAmelCase = BertConfig( vocab_size=9_9 , hidden_size=3_2 , num_hidden_layers=5 , num_attention_heads=4 , intermediate_size=3_7 ) config.push_to_hub('''valid_org/test-config-org''' , use_auth_token=self._token ) _UpperCAmelCase = BertConfig.from_pretrained('''valid_org/test-config-org''' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(__UpperCamelCase , getattr(__UpperCamelCase , __UpperCamelCase ) ) # Reset repo delete_repo(token=self._token , repo_id='''valid_org/test-config-org''' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained( __UpperCamelCase , repo_id='''valid_org/test-config-org''' , push_to_hub=__UpperCamelCase , use_auth_token=self._token ) _UpperCAmelCase = BertConfig.from_pretrained('''valid_org/test-config-org''' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(__UpperCamelCase , getattr(__UpperCamelCase , __UpperCamelCase ) ) def lowercase__ ( self : str )->str: CustomConfig.register_for_auto_class() _UpperCAmelCase = CustomConfig(attribute=4_2 ) config.push_to_hub('''test-dynamic-config''' , use_auth_token=self._token ) # This has added the proper auto_map field to the config self.assertDictEqual(config.auto_map , {'''AutoConfig''': '''custom_configuration.CustomConfig'''} ) _UpperCAmelCase = AutoConfig.from_pretrained(F'{USER}/test-dynamic-config' , trust_remote_code=__UpperCamelCase ) # Can't make an isinstance check because the new_config is from the FakeConfig class of a dynamic module self.assertEqual(new_config.__class__.__name__ , '''CustomConfig''' ) self.assertEqual(new_config.attribute , 4_2 ) class _a ( unittest.TestCase): """simple docstring""" def lowercase__ ( self : int )->Tuple: _UpperCAmelCase = GPTaConfig() # attempt to modify each of int/float/bool/str config records and verify they were updated _UpperCAmelCase = c.n_embd + 1 # int _UpperCAmelCase = c.resid_pdrop + 1.0 # float _UpperCAmelCase = not c.scale_attn_weights # bool _UpperCAmelCase = c.summary_type + '''foo''' # str c.update_from_string( F'n_embd={n_embd},resid_pdrop={resid_pdrop},scale_attn_weights={scale_attn_weights},summary_type={summary_type}' ) self.assertEqual(__UpperCamelCase , c.n_embd , '''mismatch for key: n_embd''' ) self.assertEqual(__UpperCamelCase , c.resid_pdrop , '''mismatch for key: resid_pdrop''' ) self.assertEqual(__UpperCamelCase , c.scale_attn_weights , '''mismatch for key: scale_attn_weights''' ) self.assertEqual(__UpperCamelCase , c.summary_type , '''mismatch for key: summary_type''' ) def lowercase__ ( self : Any )->Tuple: _UpperCAmelCase = PretrainedConfig() _UpperCAmelCase = [key for key in base_config.__dict__ if key not in config_common_kwargs] # If this part of the test fails, you have arguments to addin config_common_kwargs above. self.assertListEqual( __UpperCamelCase , ['''is_encoder_decoder''', '''_name_or_path''', '''_commit_hash''', '''transformers_version'''] ) _UpperCAmelCase = [key for key, value in config_common_kwargs.items() if value == getattr(__UpperCamelCase , __UpperCamelCase )] if len(__UpperCamelCase ) > 0: raise ValueError( '''The following keys are set with the default values in''' ''' `test_configuration_common.config_common_kwargs` pick another value for them:''' F' {", ".join(__UpperCamelCase )}.' ) def lowercase__ ( self : Optional[int] )->Optional[Any]: with self.assertRaises(__UpperCamelCase ): # config is in subfolder, the following should not work without specifying the subfolder _UpperCAmelCase = BertConfig.from_pretrained('''hf-internal-testing/tiny-random-bert-subfolder''' ) _UpperCAmelCase = BertConfig.from_pretrained('''hf-internal-testing/tiny-random-bert-subfolder''' , subfolder='''bert''' ) self.assertIsNotNone(__UpperCamelCase ) def lowercase__ ( self : int )->Any: # A mock response for an HTTP head request to emulate server down _UpperCAmelCase = mock.Mock() _UpperCAmelCase = 5_0_0 _UpperCAmelCase = {} _UpperCAmelCase = HTTPError _UpperCAmelCase = {} # Download this model to make sure it's in the cache. _UpperCAmelCase = BertConfig.from_pretrained('''hf-internal-testing/tiny-random-bert''' ) # Under the mock environment we get a 500 error when trying to reach the model. with mock.patch('''requests.Session.request''' , return_value=__UpperCamelCase ) as mock_head: _UpperCAmelCase = BertConfig.from_pretrained('''hf-internal-testing/tiny-random-bert''' ) # This check we did call the fake head request mock_head.assert_called() def lowercase__ ( self : int )->str: # This test is for deprecated behavior and can be removed in v5 _UpperCAmelCase = BertConfig.from_pretrained( '''https://huggingface.co/hf-internal-testing/tiny-random-bert/resolve/main/config.json''' ) def lowercase__ ( self : Tuple )->Optional[int]: _UpperCAmelCase = AutoConfig.from_pretrained('''bert-base-cased''' ) _UpperCAmelCase = ['''config.4.0.0.json'''] with tempfile.TemporaryDirectory() as tmp_dir: configuration.save_pretrained(__UpperCamelCase ) _UpperCAmelCase = 2 json.dump(configuration.to_dict() , open(os.path.join(__UpperCamelCase , '''config.4.0.0.json''' ) , '''w''' ) ) # This should pick the new configuration file as the version of Transformers is > 4.0.0 _UpperCAmelCase = AutoConfig.from_pretrained(__UpperCamelCase ) self.assertEqual(new_configuration.hidden_size , 2 ) # Will need to be adjusted if we reach v42 and this test is still here. # Should pick the old configuration file as the version of Transformers is < 4.42.0 _UpperCAmelCase = ['''config.42.0.0.json'''] _UpperCAmelCase = 7_6_8 configuration.save_pretrained(__UpperCamelCase ) shutil.move(os.path.join(__UpperCamelCase , '''config.4.0.0.json''' ) , os.path.join(__UpperCamelCase , '''config.42.0.0.json''' ) ) _UpperCAmelCase = AutoConfig.from_pretrained(__UpperCamelCase ) self.assertEqual(new_configuration.hidden_size , 7_6_8 ) def lowercase__ ( self : Dict )->Any: # This repo has two configuration files, one for v4.0.0 and above with a different hidden size. _UpperCAmelCase = '''hf-internal-testing/test-two-configs''' import transformers as new_transformers _UpperCAmelCase = '''v4.0.0''' _UpperCAmelCase , _UpperCAmelCase = new_transformers.models.auto.AutoConfig.from_pretrained( __UpperCamelCase , return_unused_kwargs=__UpperCamelCase ) self.assertEqual(new_configuration.hidden_size , 2 ) # This checks `_configuration_file` ia not kept in the kwargs by mistake. self.assertDictEqual(__UpperCamelCase , {} ) # Testing an older version by monkey-patching the version in the module it's used. import transformers as old_transformers _UpperCAmelCase = '''v3.0.0''' _UpperCAmelCase = old_transformers.models.auto.AutoConfig.from_pretrained(__UpperCamelCase ) self.assertEqual(old_configuration.hidden_size , 7_6_8 )
371
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_torch_available __A : List[Any] = { "configuration_gpt_neo": ["GPT_NEO_PRETRAINED_CONFIG_ARCHIVE_MAP", "GPTNeoConfig", "GPTNeoOnnxConfig"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : str = [ "GPT_NEO_PRETRAINED_MODEL_ARCHIVE_LIST", "GPTNeoForCausalLM", "GPTNeoForQuestionAnswering", "GPTNeoForSequenceClassification", "GPTNeoForTokenClassification", "GPTNeoModel", "GPTNeoPreTrainedModel", "load_tf_weights_in_gpt_neo", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : List[Any] = [ "FlaxGPTNeoForCausalLM", "FlaxGPTNeoModel", "FlaxGPTNeoPreTrainedModel", ] if TYPE_CHECKING: from .configuration_gpt_neo import GPT_NEO_PRETRAINED_CONFIG_ARCHIVE_MAP, GPTNeoConfig, GPTNeoOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_gpt_neo import ( GPT_NEO_PRETRAINED_MODEL_ARCHIVE_LIST, GPTNeoForCausalLM, GPTNeoForQuestionAnswering, GPTNeoForSequenceClassification, GPTNeoForTokenClassification, GPTNeoModel, GPTNeoPreTrainedModel, load_tf_weights_in_gpt_neo, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_gpt_neo import FlaxGPTNeoForCausalLM, FlaxGPTNeoModel, FlaxGPTNeoPreTrainedModel else: import sys __A : Dict = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
326
0
import sacrebleu as scb from packaging import version from sacrebleu import TER import datasets a_ = """\ @inproceedings{snover-etal-2006-study, title = \"A Study of Translation Edit Rate with Targeted Human Annotation\", author = \"Snover, Matthew and Dorr, Bonnie and Schwartz, Rich and Micciulla, Linnea and Makhoul, John\", booktitle = \"Proceedings of the 7th Conference of the Association for Machine Translation in the Americas: Technical Papers\", month = aug # \" 8-12\", year = \"2006\", address = \"Cambridge, Massachusetts, USA\", publisher = \"Association for Machine Translation in the Americas\", url = \"https://aclanthology.org/2006.amta-papers.25\", pages = \"223--231\", } @inproceedings{post-2018-call, title = \"A Call for Clarity in Reporting {BLEU} Scores\", author = \"Post, Matt\", booktitle = \"Proceedings of the Third Conference on Machine Translation: Research Papers\", month = oct, year = \"2018\", address = \"Belgium, Brussels\", publisher = \"Association for Computational Linguistics\", url = \"https://www.aclweb.org/anthology/W18-6319\", pages = \"186--191\", } """ a_ = """\ TER (Translation Edit Rate, also called Translation Error Rate) is a metric to quantify the edit operations that a hypothesis requires to match a reference translation. We use the implementation that is already present in sacrebleu (https://github.com/mjpost/sacreBLEU#ter), which in turn is inspired by the TERCOM implementation, which can be found here: https://github.com/jhclark/tercom. The implementation here is slightly different from sacrebleu in terms of the required input format. The length of the references and hypotheses lists need to be the same, so you may need to transpose your references compared to sacrebleu's required input format. See https://github.com/huggingface/datasets/issues/3154#issuecomment-950746534 See the README.md file at https://github.com/mjpost/sacreBLEU#ter for more information. """ a_ = """ Produces TER scores alongside the number of edits and reference length. Args: predictions (list of str): The system stream (a sequence of segments). references (list of list of str): A list of one or more reference streams (each a sequence of segments). normalized (boolean): If `True`, applies basic tokenization and normalization to sentences. Defaults to `False`. ignore_punct (boolean): If `True`, applies basic tokenization and normalization to sentences. Defaults to `False`. support_zh_ja_chars (boolean): If `True`, tokenization/normalization supports processing of Chinese characters, as well as Japanese Kanji, Hiragana, Katakana, and Phonetic Extensions of Katakana. Only applies if `normalized = True`. Defaults to `False`. case_sensitive (boolean): If `False`, makes all predictions and references lowercase to ignore differences in case. Defaults to `False`. Returns: 'score' (float): TER score (num_edits / sum_ref_lengths * 100) 'num_edits' (int): The cumulative number of edits 'ref_length' (float): The cumulative average reference length Examples: Example 1: >>> predictions = [\"does this sentence match??\", ... \"what about this sentence?\", ... \"What did the TER metric user say to the developer?\"] >>> references = [[\"does this sentence match\", \"does this sentence match!?!\"], ... [\"wHaT aBoUt ThIs SeNtEnCe?\", \"wHaT aBoUt ThIs SeNtEnCe?\"], ... [\"Your jokes are...\", \"...TERrible\"]] >>> ter = datasets.load_metric(\"ter\") >>> results = ter.compute(predictions=predictions, ... references=references, ... case_sensitive=True) >>> print(results) {'score': 150.0, 'num_edits': 15, 'ref_length': 10.0} Example 2: >>> predictions = [\"does this sentence match??\", ... \"what about this sentence?\"] >>> references = [[\"does this sentence match\", \"does this sentence match!?!\"], ... [\"wHaT aBoUt ThIs SeNtEnCe?\", \"wHaT aBoUt ThIs SeNtEnCe?\"]] >>> ter = datasets.load_metric(\"ter\") >>> results = ter.compute(predictions=predictions, ... references=references, ... case_sensitive=True) >>> print(results) {'score': 62.5, 'num_edits': 5, 'ref_length': 8.0} Example 3: >>> predictions = [\"does this sentence match??\", ... \"what about this sentence?\"] >>> references = [[\"does this sentence match\", \"does this sentence match!?!\"], ... [\"wHaT aBoUt ThIs SeNtEnCe?\", \"wHaT aBoUt ThIs SeNtEnCe?\"]] >>> ter = datasets.load_metric(\"ter\") >>> results = ter.compute(predictions=predictions, ... references=references, ... normalized=True, ... case_sensitive=True) >>> print(results) {'score': 57.14285714285714, 'num_edits': 6, 'ref_length': 10.5} Example 4: >>> predictions = [\"does this sentence match??\", ... \"what about this sentence?\"] >>> references = [[\"does this sentence match\", \"does this sentence match!?!\"], ... [\"wHaT aBoUt ThIs SeNtEnCe?\", \"wHaT aBoUt ThIs SeNtEnCe?\"]] >>> ter = datasets.load_metric(\"ter\") >>> results = ter.compute(predictions=predictions, ... references=references, ... ignore_punct=True, ... case_sensitive=False) >>> print(results) {'score': 0.0, 'num_edits': 0, 'ref_length': 8.0} Example 5: >>> predictions = [\"does this sentence match??\", ... \"what about this sentence?\", ... \"What did the TER metric user say to the developer?\"] >>> references = [[\"does this sentence match\", \"does this sentence match!?!\"], ... [\"wHaT aBoUt ThIs SeNtEnCe?\", \"wHaT aBoUt ThIs SeNtEnCe?\"], ... [\"Your jokes are...\", \"...TERrible\"]] >>> ter = datasets.load_metric(\"ter\") >>> results = ter.compute(predictions=predictions, ... references=references, ... ignore_punct=True, ... case_sensitive=False) >>> print(results) {'score': 100.0, 'num_edits': 10, 'ref_length': 10.0} """ @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __lowerCAmelCase ( datasets.Metric ): def lowerCamelCase ( self ): '''simple docstring''' if version.parse(scb.__version__ ) < version.parse('''1.4.12''' ): raise ImportWarning( '''To use `sacrebleu`, the module `sacrebleu>=1.4.12` is required, and the current version of `sacrebleu` doesn\'t match this condition.\n''' '''You can install it with `pip install "sacrebleu>=1.4.12"`.''' ) return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , homepage='''http://www.cs.umd.edu/~snover/tercom/''' , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Value('''string''' , id='''sequence''' ), '''references''': datasets.Sequence(datasets.Value('''string''' , id='''sequence''' ) , id='''references''' ), } ) , codebase_urls=['''https://github.com/mjpost/sacreBLEU#ter'''] , reference_urls=[ '''https://github.com/jhclark/tercom''', ] , ) def lowerCamelCase ( self , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase = False , __UpperCAmelCase = False , __UpperCAmelCase = False , __UpperCAmelCase = False , ): '''simple docstring''' __lowerCamelCase = len(references[0] ) if any(len(_A ) != references_per_prediction for refs in references ): raise ValueError('''Sacrebleu requires the same number of references for each prediction''' ) __lowerCamelCase = [[refs[i] for refs in references] for i in range(_A )] __lowerCamelCase = TER( normalized=_A , no_punct=_A , asian_support=_A , case_sensitive=_A , ) __lowerCamelCase = sb_ter.corpus_score(_A , _A ) return {"score": output.score, "num_edits": output.num_edits, "ref_length": output.ref_length}
330
from __future__ import annotations import copy import inspect import json import math import os import tempfile import unittest from importlib import import_module import numpy as np from transformers import ViTMAEConfig from transformers.file_utils import cached_property, is_tf_available, is_vision_available from transformers.testing_utils import require_tf, require_vision, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFViTMAEForPreTraining, TFViTMAEModel if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class SCREAMING_SNAKE_CASE_ : def __init__( self : List[Any] , _A : Optional[Any] , _A : Dict=13 , _A : Union[str, Any]=30 , _A : Tuple=2 , _A : Union[str, Any]=3 , _A : Optional[int]=True , _A : Optional[Any]=True , _A : str=32 , _A : int=2 , _A : List[str]=4 , _A : List[str]=37 , _A : Tuple="gelu" , _A : Dict=0.1 , _A : Optional[Any]=0.1 , _A : Optional[int]=10 , _A : Optional[int]=0.0_2 , _A : Optional[Any]=3 , _A : str=0.6 , _A : Union[str, Any]=None , ) -> Any: """simple docstring""" snake_case_ : Optional[int] = parent snake_case_ : Tuple = batch_size snake_case_ : List[Any] = image_size snake_case_ : List[str] = patch_size snake_case_ : List[str] = num_channels snake_case_ : Optional[Any] = is_training snake_case_ : Any = use_labels snake_case_ : Tuple = hidden_size snake_case_ : Union[str, Any] = num_hidden_layers snake_case_ : List[Any] = num_attention_heads snake_case_ : Optional[Any] = intermediate_size snake_case_ : List[Any] = hidden_act snake_case_ : Union[str, Any] = hidden_dropout_prob snake_case_ : Any = attention_probs_dropout_prob snake_case_ : Tuple = type_sequence_label_size snake_case_ : List[str] = initializer_range snake_case_ : Optional[Any] = mask_ratio snake_case_ : Any = scope # in ViTMAE, the expected sequence length = (num_patches + 1) * (1 - config.mask_ratio), rounded above # (we add 1 for the [CLS] token) snake_case_ : Optional[int] = (image_size // patch_size) ** 2 snake_case_ : str = int(math.ceil((1 - mask_ratio) * (num_patches + 1) ) ) def UpperCAmelCase_ ( self : List[str] ) -> Optional[Any]: """simple docstring""" snake_case_ : List[str] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) snake_case_ : Union[str, Any] = None if self.use_labels: snake_case_ : Optional[Any] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) snake_case_ : Union[str, Any] = self.get_config() return config, pixel_values, labels def UpperCAmelCase_ ( self : int ) -> Optional[Any]: """simple docstring""" 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 , decoder_hidden_size=self.hidden_size , decoder_num_hidden_layers=self.num_hidden_layers , decoder_num_attention_heads=self.num_attention_heads , decoder_intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=_A , initializer_range=self.initializer_range , mask_ratio=self.mask_ratio , ) def UpperCAmelCase_ ( self : List[Any] , _A : int , _A : Dict , _A : str ) -> Dict: """simple docstring""" snake_case_ : Union[str, Any] = TFViTMAEModel(config=_A ) snake_case_ : str = model(_A , training=_A ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def UpperCAmelCase_ ( self : Dict , _A : Dict , _A : Any , _A : List[Any] ) -> int: """simple docstring""" snake_case_ : Any = TFViTMAEForPreTraining(_A ) snake_case_ : Optional[Any] = model(_A , training=_A ) # expected sequence length = num_patches snake_case_ : List[str] = (self.image_size // self.patch_size) ** 2 snake_case_ : Optional[Any] = self.patch_size**2 * self.num_channels self.parent.assertEqual(result.logits.shape , (self.batch_size, num_patches, expected_num_channels) ) # test greyscale images snake_case_ : str = 1 snake_case_ : Dict = TFViTMAEForPreTraining(_A ) snake_case_ : Dict = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) snake_case_ : List[str] = model(_A , training=_A ) snake_case_ : Optional[Any] = self.patch_size**2 self.parent.assertEqual(result.logits.shape , (self.batch_size, num_patches, expected_num_channels) ) def UpperCAmelCase_ ( self : Union[str, Any] ) -> Tuple: """simple docstring""" snake_case_ : List[Any] = self.prepare_config_and_inputs() ((snake_case_) ,(snake_case_) ,(snake_case_)) : Any = config_and_inputs snake_case_ : Optional[Any] = {'pixel_values': pixel_values} return config, inputs_dict @require_tf class SCREAMING_SNAKE_CASE_ ( snake_case_ , snake_case_ , unittest.TestCase ): __magic_name__: List[str] = (TFViTMAEModel, TFViTMAEForPreTraining) if is_tf_available() else () __magic_name__: str = {"feature-extraction": TFViTMAEModel} if is_tf_available() else {} __magic_name__: Dict = False __magic_name__: Dict = False __magic_name__: List[Any] = False __magic_name__: Dict = False def UpperCAmelCase_ ( self : Any ) -> List[Any]: """simple docstring""" snake_case_ : List[Any] = TFViTMAEModelTester(self ) snake_case_ : Tuple = ConfigTester(self , config_class=_A , has_text_modality=_A , hidden_size=37 ) def UpperCAmelCase_ ( self : int ) -> Union[str, Any]: """simple docstring""" self.config_tester.run_common_tests() @unittest.skip(reason='ViTMAE does not use inputs_embeds' ) def UpperCAmelCase_ ( self : Tuple ) -> Union[str, Any]: """simple docstring""" pass def UpperCAmelCase_ ( self : List[str] ) -> Union[str, Any]: """simple docstring""" snake_case_ ,snake_case_ : Any = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: snake_case_ : List[Any] = model_class(_A ) self.assertIsInstance(model.get_input_embeddings() , (tf.keras.layers.Layer) ) snake_case_ : Optional[int] = model.get_output_embeddings() self.assertTrue(x is None or isinstance(_A , tf.keras.layers.Layer ) ) def UpperCAmelCase_ ( self : List[str] ) -> Dict: """simple docstring""" snake_case_ ,snake_case_ : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: snake_case_ : List[str] = model_class(_A ) snake_case_ : Any = inspect.signature(model.call ) # signature.parameters is an OrderedDict => so arg_names order is deterministic snake_case_ : Dict = [*signature.parameters.keys()] snake_case_ : Dict = ['pixel_values'] self.assertListEqual(arg_names[:1] , _A ) def UpperCAmelCase_ ( self : Dict ) -> List[str]: """simple docstring""" snake_case_ : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_A ) def UpperCAmelCase_ ( self : List[Any] ) -> List[str]: """simple docstring""" snake_case_ : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_pretraining(*_A ) def UpperCAmelCase_ ( self : Tuple ) -> Dict: """simple docstring""" np.random.seed(2 ) snake_case_ ,snake_case_ : int = self.model_tester.prepare_config_and_inputs_for_common() snake_case_ : Optional[int] = int((config.image_size // config.patch_size) ** 2 ) snake_case_ : Optional[Any] = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) for model_class in self.all_model_classes: snake_case_ : Optional[Any] = model_class(_A ) snake_case_ : Union[str, Any] = self._prepare_for_class(_A , _A ) snake_case_ : List[str] = model(_A , noise=_A ) snake_case_ : Tuple = copy.deepcopy(self._prepare_for_class(_A , _A ) ) snake_case_ : str = model(**_A , noise=_A ) snake_case_ : Union[str, Any] = outputs_dict[0].numpy() snake_case_ : Optional[Any] = outputs_keywords[0].numpy() self.assertLess(np.sum(np.abs(output_dict - output_keywords ) ) , 1E-6 ) def UpperCAmelCase_ ( self : List[Any] ) -> List[Any]: """simple docstring""" np.random.seed(2 ) snake_case_ ,snake_case_ : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() snake_case_ : Tuple = int((config.image_size // config.patch_size) ** 2 ) snake_case_ : Optional[int] = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) def prepare_numpy_arrays(_A : int ): snake_case_ : Any = {} for k, v in inputs_dict.items(): if tf.is_tensor(_A ): snake_case_ : str = v.numpy() else: snake_case_ : Optional[Any] = np.array(_A ) return inputs_np_dict for model_class in self.all_model_classes: snake_case_ : int = model_class(_A ) snake_case_ : List[Any] = self._prepare_for_class(_A , _A ) snake_case_ : Any = prepare_numpy_arrays(_A ) snake_case_ : List[Any] = model(_A , noise=_A ) snake_case_ : List[Any] = model(**_A , noise=_A ) self.assert_outputs_same(_A , _A ) def UpperCAmelCase_ ( self : Tuple , _A : Union[str, Any] , _A : Union[str, Any] , _A : List[Any] ) -> List[str]: """simple docstring""" np.random.seed(2 ) snake_case_ : Optional[int] = int((tf_model.config.image_size // tf_model.config.patch_size) ** 2 ) snake_case_ : Optional[int] = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) snake_case_ : Optional[int] = tf.constant(_A ) # Add `noise` argument. # PT inputs will be prepared in `super().check_pt_tf_models()` with this added `noise` argument snake_case_ : Optional[Any] = tf_noise super().check_pt_tf_models(_A , _A , _A ) def UpperCAmelCase_ ( self : Optional[Any] ) -> Dict: """simple docstring""" np.random.seed(2 ) snake_case_ ,snake_case_ : Tuple = self.model_tester.prepare_config_and_inputs_for_common() snake_case_ : int = { module_member for model_class in self.all_model_classes for module in (import_module(model_class.__module__ ),) for module_member_name in dir(_A ) if module_member_name.endswith('MainLayer' ) # This condition is required, since `modeling_tf_clip.py` has 3 classes whose names end with `MainLayer`. and module_member_name[: -len('MainLayer' )] == model_class.__name__[: -len('Model' )] for module_member in (getattr(_A , _A ),) if isinstance(_A , _A ) and tf.keras.layers.Layer in module_member.__bases__ and getattr(_A , '_keras_serializable' , _A ) } snake_case_ : List[Any] = int((config.image_size // config.patch_size) ** 2 ) snake_case_ : List[Any] = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) snake_case_ : Optional[int] = tf.convert_to_tensor(_A ) inputs_dict.update({'noise': noise} ) for main_layer_class in tf_main_layer_classes: snake_case_ : Optional[Any] = main_layer_class(_A ) snake_case_ : List[str] = { name: tf.keras.Input(tensor.shape[1:] , dtype=tensor.dtype ) for name, tensor in inputs_dict.items() } snake_case_ : Union[str, Any] = tf.keras.Model(_A , outputs=main_layer(_A ) ) snake_case_ : int = model(_A ) with tempfile.TemporaryDirectory() as tmpdirname: snake_case_ : List[Any] = os.path.join(_A , 'keras_model.h5' ) model.save(_A ) snake_case_ : str = tf.keras.models.load_model( _A , custom_objects={main_layer_class.__name__: main_layer_class} ) assert isinstance(_A , tf.keras.Model ) snake_case_ : List[str] = model(_A ) self.assert_outputs_same(_A , _A ) @slow def UpperCAmelCase_ ( self : Union[str, Any] ) -> Union[str, Any]: """simple docstring""" np.random.seed(2 ) snake_case_ ,snake_case_ : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() snake_case_ : int = int((config.image_size // config.patch_size) ** 2 ) snake_case_ : int = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) for model_class in self.all_model_classes: snake_case_ : Optional[Any] = model_class(_A ) snake_case_ : Optional[Any] = self._prepare_for_class(_A , _A ) snake_case_ : int = model(_A , noise=_A ) if model_class.__name__ == "TFViTMAEModel": snake_case_ : Any = outputs.last_hidden_state.numpy() snake_case_ : Optional[int] = 0 else: snake_case_ : str = outputs.logits.numpy() snake_case_ : Optional[Any] = 0 with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(_A , saved_model=_A ) snake_case_ : Any = model_class.from_pretrained(_A ) snake_case_ : Any = model(_A , noise=_A ) if model_class.__name__ == "TFViTMAEModel": snake_case_ : Dict = after_outputs['last_hidden_state'].numpy() snake_case_ : Dict = 0 else: snake_case_ : Any = after_outputs['logits'].numpy() snake_case_ : Optional[Any] = 0 snake_case_ : Any = np.amax(np.abs(out_a - out_a ) ) self.assertLessEqual(_A , 1E-5 ) def UpperCAmelCase_ ( self : Any ) -> str: """simple docstring""" np.random.seed(2 ) snake_case_ ,snake_case_ : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() snake_case_ : Optional[int] = int((config.image_size // config.patch_size) ** 2 ) snake_case_ : Union[str, Any] = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) for model_class in self.all_model_classes: snake_case_ : str = model_class(_A ) snake_case_ : int = self._prepare_for_class(_A , _A ) snake_case_ : str = model(_A , noise=_A ) snake_case_ : Dict = model.get_config() # make sure that returned config is jsonifiable, which is required by keras json.dumps(_A ) snake_case_ : Any = model_class.from_config(model.get_config() ) # make sure it also accepts a normal config snake_case_ : str = model_class.from_config(model.config ) snake_case_ : Union[str, Any] = new_model(_A ) # Build model new_model.set_weights(model.get_weights() ) snake_case_ : List[str] = new_model(_A , noise=_A ) self.assert_outputs_same(_A , _A ) @unittest.skip( reason='ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load\n to get deterministic results.' ) def UpperCAmelCase_ ( self : List[Any] ) -> Optional[int]: """simple docstring""" pass @unittest.skip(reason='ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load' ) def UpperCAmelCase_ ( self : Optional[int] ) -> Tuple: """simple docstring""" pass @slow def UpperCAmelCase_ ( self : Tuple ) -> Tuple: """simple docstring""" snake_case_ : Optional[Any] = TFViTMAEModel.from_pretrained('google/vit-base-patch16-224' ) self.assertIsNotNone(_A ) def SCREAMING_SNAKE_CASE__ ( ): snake_case_ : Optional[Any] = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_tf @require_vision class SCREAMING_SNAKE_CASE_ ( unittest.TestCase ): @cached_property def UpperCAmelCase_ ( self : str ) -> Dict: """simple docstring""" return ViTImageProcessor.from_pretrained('facebook/vit-mae-base' ) if is_vision_available() else None @slow def UpperCAmelCase_ ( self : str ) -> Dict: """simple docstring""" np.random.seed(2 ) snake_case_ : List[str] = TFViTMAEForPreTraining.from_pretrained('facebook/vit-mae-base' ) snake_case_ : List[Any] = self.default_image_processor snake_case_ : Dict = prepare_img() snake_case_ : Optional[Any] = image_processor(images=_A , return_tensors='tf' ) # 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) snake_case_ : int = ViTMAEConfig() snake_case_ : List[Any] = int((vit_mae_config.image_size // vit_mae_config.patch_size) ** 2 ) snake_case_ : List[Any] = np.random.uniform(size=(1, num_patches) ) # forward pass snake_case_ : Optional[Any] = model(**_A , noise=_A ) # verify the logits snake_case_ : Optional[int] = tf.convert_to_tensor([1, 196, 768] ) self.assertEqual(outputs.logits.shape , _A ) snake_case_ : Any = tf.convert_to_tensor( [[-0.0_5_4_8, -1.7_0_2_3, -0.9_3_2_5], [0.3_7_2_1, -0.5_6_7_0, -0.2_2_3_3], [0.8_2_3_5, -1.3_8_7_8, -0.3_5_2_4]] ) tf.debugging.assert_near(outputs.logits[0, :3, :3] , _A , atol=1E-4 )
327
0
'''simple docstring''' from scipy.stats import pearsonr, spearmanr from sklearn.metrics import fa_score, matthews_corrcoef import datasets __A : Any = "\\n@inproceedings{wang2019glue,\n title={{GLUE}: A Multi-Task Benchmark and Analysis Platform for Natural Language Understanding},\n author={Wang, Alex and Singh, Amanpreet and Michael, Julian and Hill, Felix and Levy, Omer and Bowman, Samuel R.},\n note={In the Proceedings of ICLR.},\n year={2019}\n}\n" __A : str = "\\nGLUE, the General Language Understanding Evaluation benchmark\n(https://gluebenchmark.com/) is a collection of resources for training,\nevaluating, and analyzing natural language understanding systems.\n" __A : Any = "\nCompute GLUE evaluation metric associated to each GLUE dataset.\nArgs:\n predictions: list of predictions to score.\n Each translation should be tokenized into a list of tokens.\n references: list of lists of references for each translation.\n Each reference should be tokenized into a list of tokens.\nReturns: depending on the GLUE subset, one or several of:\n \"accuracy\": Accuracy\n \"f1\": F1 score\n \"pearson\": Pearson Correlation\n \"spearmanr\": Spearman Correlation\n \"matthews_correlation\": Matthew Correlation\nExamples:\n\n >>> glue_metric = datasets.load_metric('glue', 'sst2') # 'sst2' or any of [\"mnli\", \"mnli_mismatched\", \"mnli_matched\", \"qnli\", \"rte\", \"wnli\", \"hans\"]\n >>> references = [0, 1]\n >>> predictions = [0, 1]\n >>> results = glue_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {'accuracy': 1.0}\n\n >>> glue_metric = datasets.load_metric('glue', 'mrpc') # 'mrpc' or 'qqp'\n >>> references = [0, 1]\n >>> predictions = [0, 1]\n >>> results = glue_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {'accuracy': 1.0, 'f1': 1.0}\n\n >>> glue_metric = datasets.load_metric('glue', 'stsb')\n >>> references = [0., 1., 2., 3., 4., 5.]\n >>> predictions = [0., 1., 2., 3., 4., 5.]\n >>> results = glue_metric.compute(predictions=predictions, references=references)\n >>> print({\"pearson\": round(results[\"pearson\"], 2), \"spearmanr\": round(results[\"spearmanr\"], 2)})\n {'pearson': 1.0, 'spearmanr': 1.0}\n\n >>> glue_metric = datasets.load_metric('glue', 'cola')\n >>> references = [0, 1]\n >>> predictions = [0, 1]\n >>> results = glue_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {'matthews_correlation': 1.0}\n" def UpperCamelCase_ ( A__ : Any , A__ : List[str] ): '''simple docstring''' return float((preds == labels).mean() ) def UpperCamelCase_ ( A__ : List[Any] , A__ : str ): '''simple docstring''' lowerCAmelCase_ : int = simple_accuracy(A__ , A__ ) lowerCAmelCase_ : Dict = float(fa_score(y_true=A__ , y_pred=A__ ) ) return { "accuracy": acc, "f1": fa, } def UpperCamelCase_ ( A__ : str , A__ : Optional[Any] ): '''simple docstring''' lowerCAmelCase_ : Union[str, Any] = float(pearsonr(A__ , A__ )[0] ) lowerCAmelCase_ : str = float(spearmanr(A__ , A__ )[0] ) return { "pearson": pearson_corr, "spearmanr": spearman_corr, } @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION ,_KWARGS_DESCRIPTION) class __snake_case ( datasets.Metric): """simple docstring""" def __lowercase ( self : Optional[int] ) -> str: if self.config_name not in [ "sst2", "mnli", "mnli_mismatched", "mnli_matched", "cola", "stsb", "mrpc", "qqp", "qnli", "rte", "wnli", "hans", ]: raise KeyError( """You should supply a configuration name selected in """ """[\"sst2\", \"mnli\", \"mnli_mismatched\", \"mnli_matched\", """ """\"cola\", \"stsb\", \"mrpc\", \"qqp\", \"qnli\", \"rte\", \"wnli\", \"hans\"]""" ) return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": datasets.Value("""int64""" if self.config_name != """stsb""" else """float32""" ), """references""": datasets.Value("""int64""" if self.config_name != """stsb""" else """float32""" ), } ) , codebase_urls=[] , reference_urls=[] , format="""numpy""" , ) def __lowercase ( self : Optional[Any] , lowerCamelCase : List[str] , lowerCamelCase : List[Any] ) -> Optional[Any]: if self.config_name == "cola": return {"matthews_correlation": matthews_corrcoef(lowerCamelCase , lowerCamelCase )} elif self.config_name == "stsb": return pearson_and_spearman(lowerCamelCase , lowerCamelCase ) elif self.config_name in ["mrpc", "qqp"]: return acc_and_fa(lowerCamelCase , lowerCamelCase ) elif self.config_name in ["sst2", "mnli", "mnli_mismatched", "mnli_matched", "qnli", "rte", "wnli", "hans"]: return {"accuracy": simple_accuracy(lowerCamelCase , lowerCamelCase )} else: raise KeyError( """You should supply a configuration name selected in """ """[\"sst2\", \"mnli\", \"mnli_mismatched\", \"mnli_matched\", """ """\"cola\", \"stsb\", \"mrpc\", \"qqp\", \"qnli\", \"rte\", \"wnli\", \"hans\"]""" )
89
'''simple docstring''' import requests def UpperCamelCase_ ( A__ : str , A__ : str ): '''simple docstring''' lowerCAmelCase_ : Optional[Any] = {"""Content-Type""": """application/json"""} lowerCAmelCase_ : Union[str, Any] = requests.post(A__ , json={"""text""": message_body} , headers=A__ ) if response.status_code != 2_00: lowerCAmelCase_ : Dict = ( """Request to slack returned an error """ f'{response.status_code}, the response is:\n{response.text}' ) raise ValueError(A__ ) if __name__ == "__main__": # Set the slack url to the one provided by Slack when you create the webhook at # https://my.slack.com/services/new/incoming-webhook/ send_slack_message("<YOUR MESSAGE BODY>", "<SLACK CHANNEL URL>")
89
1
from collections import defaultdict class a : def __init__( self :List[Any] ,__lowercase :Union[str, Any] ,__lowercase :int ): snake_case__ : str = total # total no of tasks (N) # DP table will have a dimension of (2^M)*N # initially all values are set to -1 snake_case__ : Optional[int] = [ [-1 for i in range(total + 1 )] for j in range(2 ** len(__UpperCAmelCase ) ) ] snake_case__ : Union[str, Any] = defaultdict(__UpperCAmelCase ) # stores the list of persons for each task # final_mask is used to check if all persons are included by setting all bits # to 1 snake_case__ : Optional[Any] = (1 << len(__UpperCAmelCase )) - 1 def __lowerCamelCase ( self :Optional[Any] ,__lowercase :List[Any] ,__lowercase :Tuple ): # if mask == self.finalmask all persons are distributed tasks, return 1 if mask == self.final_mask: return 1 # if not everyone gets the task and no more tasks are available, return 0 if task_no > self.total_tasks: return 0 # if case already considered if self.dp[mask][task_no] != -1: return self.dp[mask][task_no] # Number of ways when we don't this task in the arrangement snake_case__ : List[str] = self.count_ways_until(__UpperCAmelCase ,task_no + 1 ) # now assign the tasks one by one to all possible persons and recursively # assign for the remaining tasks. if task_no in self.task: for p in self.task[task_no]: # if p is already given a task if mask & (1 << p): continue # assign this task to p and change the mask value. And recursively # assign tasks with the new mask value. total_ways_util += self.count_ways_until(mask | (1 << p) ,task_no + 1 ) # save the value. snake_case__ : Dict = total_ways_util return self.dp[mask][task_no] def __lowerCamelCase ( self :Any ,__lowercase :Optional[int] ): # Store the list of persons for each task for i in range(len(__UpperCAmelCase ) ): for j in task_performed[i]: self.task[j].append(__UpperCAmelCase ) # call the function to fill the DP table, final answer is stored in dp[0][1] return self.count_ways_until(0 ,1 ) if __name__ == "__main__": A__ = 5 # total no of tasks (the value of N) # the list of tasks that can be done by M persons. A__ = [[1, 3, 4], [1, 2, 5], [3, 4]] print( AssignmentUsingBitmask(task_performed, total_tasks).count_no_of_ways( task_performed ) )
230
import gc import unittest from parameterized import parameterized from diffusers import FlaxUNetaDConditionModel from diffusers.utils import is_flax_available from diffusers.utils.testing_utils import load_hf_numpy, require_flax, slow if is_flax_available(): import jax import jax.numpy as jnp @slow @require_flax class __lowerCAmelCase ( unittest.TestCase ): def lowerCamelCase ( self , __UpperCAmelCase , __UpperCAmelCase ): '''simple docstring''' return F"""gaussian_noise_s={seed}_shape={"_".join([str(__UpperCAmelCase ) for s in shape] )}.npy""" def lowerCamelCase ( self ): '''simple docstring''' # clean up the VRAM after each test super().tearDown() gc.collect() def lowerCamelCase ( self , __UpperCAmelCase=0 , __UpperCAmelCase=(4, 4, 64, 64) , __UpperCAmelCase=False ): '''simple docstring''' __lowerCamelCase = jnp.bfloataa if fpaa else jnp.floataa __lowerCamelCase = jnp.array(load_hf_numpy(self.get_file_format(__UpperCAmelCase , __UpperCAmelCase ) ) , dtype=__UpperCAmelCase ) return image def lowerCamelCase ( self , __UpperCAmelCase=False , __UpperCAmelCase="CompVis/stable-diffusion-v1-4" ): '''simple docstring''' __lowerCamelCase = jnp.bfloataa if fpaa else jnp.floataa __lowerCamelCase = '''bf16''' if fpaa else None __lowerCamelCase ,__lowerCamelCase = FlaxUNetaDConditionModel.from_pretrained( __UpperCAmelCase , subfolder='''unet''' , dtype=__UpperCAmelCase , revision=__UpperCAmelCase ) return model, params def lowerCamelCase ( self , __UpperCAmelCase=0 , __UpperCAmelCase=(4, 77, 768) , __UpperCAmelCase=False ): '''simple docstring''' __lowerCamelCase = jnp.bfloataa if fpaa else jnp.floataa __lowerCamelCase = jnp.array(load_hf_numpy(self.get_file_format(__UpperCAmelCase , __UpperCAmelCase ) ) , dtype=__UpperCAmelCase ) return hidden_states @parameterized.expand( [ # fmt: off [83, 4, [-0.2_323, -0.1_304, 0.0_813, -0.3_093, -0.0_919, -0.1_571, -0.1_125, -0.5_806]], [17, 0.55, [-0.0_831, -0.2_443, 0.0_901, -0.0_919, 0.3_396, 0.0_103, -0.3_743, 0.0_701]], [8, 0.89, [-0.4_863, 0.0_859, 0.0_875, -0.1_658, 0.9_199, -0.0_114, 0.4_839, 0.4_639]], [3, 1000, [-0.5_649, 0.2_402, -0.5_518, 0.1_248, 1.1_328, -0.2_443, -0.0_325, -1.0_078]], # fmt: on ] ) def lowerCamelCase ( self , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ): '''simple docstring''' __lowerCamelCase ,__lowerCamelCase = self.get_unet_model(model_id='''CompVis/stable-diffusion-v1-4''' , fpaa=__UpperCAmelCase ) __lowerCamelCase = self.get_latents(__UpperCAmelCase , fpaa=__UpperCAmelCase ) __lowerCamelCase = self.get_encoder_hidden_states(__UpperCAmelCase , fpaa=__UpperCAmelCase ) __lowerCamelCase = model.apply( {'''params''': params} , __UpperCAmelCase , jnp.array(__UpperCAmelCase , dtype=jnp.intaa ) , encoder_hidden_states=__UpperCAmelCase , ).sample assert sample.shape == latents.shape __lowerCamelCase = jnp.asarray(jax.device_get((sample[-1, -2:, -2:, :2].flatten()) ) , dtype=jnp.floataa ) __lowerCamelCase = jnp.array(__UpperCAmelCase , dtype=jnp.floataa ) # Found torch (float16) and flax (bfloat16) outputs to be within this tolerance, in the same hardware assert jnp.allclose(__UpperCAmelCase , __UpperCAmelCase , atol=1E-2 ) @parameterized.expand( [ # fmt: off [83, 4, [0.1_514, 0.0_807, 0.1_624, 0.1_016, -0.1_896, 0.0_263, 0.0_677, 0.2_310]], [17, 0.55, [0.1_164, -0.0_216, 0.0_170, 0.1_589, -0.3_120, 0.1_005, -0.0_581, -0.1_458]], [8, 0.89, [-0.1_758, -0.0_169, 0.1_004, -0.1_411, 0.1_312, 0.1_103, -0.1_996, 0.2_139]], [3, 1000, [0.1_214, 0.0_352, -0.0_731, -0.1_562, -0.0_994, -0.0_906, -0.2_340, -0.0_539]], # fmt: on ] ) def lowerCamelCase ( self , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ): '''simple docstring''' __lowerCamelCase ,__lowerCamelCase = self.get_unet_model(model_id='''stabilityai/stable-diffusion-2''' , fpaa=__UpperCAmelCase ) __lowerCamelCase = self.get_latents(__UpperCAmelCase , shape=(4, 4, 96, 96) , fpaa=__UpperCAmelCase ) __lowerCamelCase = self.get_encoder_hidden_states(__UpperCAmelCase , shape=(4, 77, 1024) , fpaa=__UpperCAmelCase ) __lowerCamelCase = model.apply( {'''params''': params} , __UpperCAmelCase , jnp.array(__UpperCAmelCase , dtype=jnp.intaa ) , encoder_hidden_states=__UpperCAmelCase , ).sample assert sample.shape == latents.shape __lowerCamelCase = jnp.asarray(jax.device_get((sample[-1, -2:, -2:, :2].flatten()) ) , dtype=jnp.floataa ) __lowerCamelCase = jnp.array(__UpperCAmelCase , dtype=jnp.floataa ) # Found torch (float16) and flax (bfloat16) outputs to be within this tolerance, on the same hardware assert jnp.allclose(__UpperCAmelCase , __UpperCAmelCase , atol=1E-2 )
330
0
"""simple docstring""" import math class __lowercase : '''simple docstring''' def __init__( self , _UpperCAmelCase=0 ): # a graph with Node 0,1,...,N-1 __a : Tuple = n __a : Tuple = [ [math.inf for j in range(0 , _UpperCAmelCase )] for i in range(0 , _UpperCAmelCase ) ] # adjacency matrix for weight __a : Union[str, Any] = [ [math.inf for j in range(0 , _UpperCAmelCase )] for i in range(0 , _UpperCAmelCase ) ] # dp[i][j] stores minimum distance from i to j def _lowerCamelCase ( self , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ): __a : int = w def _lowerCamelCase ( self ): for k in range(0 , self.n ): for i in range(0 , self.n ): for j in range(0 , self.n ): __a : List[Any] = min(self.dp[i][j] , self.dp[i][k] + self.dp[k][j] ) def _lowerCamelCase ( self , _UpperCAmelCase , _UpperCAmelCase ): return self.dp[u][v] if __name__ == "__main__": A = Graph(5) graph.add_edge(0, 2, 9) graph.add_edge(0, 4, 10) graph.add_edge(1, 3, 5) graph.add_edge(2, 3, 7) graph.add_edge(3, 0, 10) graph.add_edge(3, 1, 2) graph.add_edge(3, 2, 1) graph.add_edge(3, 4, 6) graph.add_edge(4, 1, 3) graph.add_edge(4, 2, 4) graph.add_edge(4, 3, 9) graph.floyd_warshall() graph.show_min(1, 4) graph.show_min(0, 3)
188
"""simple docstring""" from __future__ import annotations import sys from collections import deque from typing import Generic, TypeVar A = TypeVar('''T''') class __lowercase ( Generic[T] ): '''simple docstring''' __lowerCAmelCase = 42 # Cache store of keys __lowerCAmelCase = 42 # References of the keys in cache __lowerCAmelCase = 10 # Maximum capacity of cache def __init__( self , _UpperCAmelCase ): __a : Optional[int] = deque() __a : Dict = set() if not n: __a : List[Any] = sys.maxsize elif n < 0: raise ValueError('''n should be an integer greater than 0.''' ) else: __a : str = n def _lowerCamelCase ( self , _UpperCAmelCase ): if x not in self.key_reference: if len(self.dq_store ) == LRUCache._MAX_CAPACITY: __a : int = self.dq_store.pop() self.key_reference.remove(_UpperCAmelCase ) else: self.dq_store.remove(_UpperCAmelCase ) self.dq_store.appendleft(_UpperCAmelCase ) self.key_reference.add(_UpperCAmelCase ) def _lowerCamelCase ( self ): for k in self.dq_store: print(_UpperCAmelCase ) def __repr__( self ): return f"""LRUCache({self._MAX_CAPACITY}) => {list(self.dq_store )}""" if __name__ == "__main__": import doctest doctest.testmod() A = LRUCache(4) lru_cache.refer('''A''') lru_cache.refer(2) lru_cache.refer(3) lru_cache.refer('''A''') lru_cache.refer(4) lru_cache.refer(5) lru_cache.display() print(lru_cache) assert str(lru_cache) == "LRUCache(4) => [5, 4, 'A', 3]"
188
1
import json import os import shutil import tempfile import unittest import numpy as np from transformers import BertTokenizerFast from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES, BertTokenizer from transformers.testing_utils import require_tokenizers, require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available if is_vision_available(): from PIL import Image from transformers import VisionTextDualEncoderProcessor, ViTImageProcessor @require_tokenizers @require_vision class __A ( unittest.TestCase ): '''simple docstring''' def __lowerCamelCase ( self ): '''simple docstring''' lowerCamelCase__ = tempfile.mkdtemp() # fmt: off lowerCamelCase__ = ['''[UNK]''', '''[CLS]''', '''[SEP]''', '''[PAD]''', '''[MASK]''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing''', ''',''', '''low''', '''lowest'''] # fmt: on lowerCamelCase__ = 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] ) ) lowerCamelCase__ = { '''do_resize''': True, '''size''': {'''height''': 1_8, '''width''': 1_8}, '''do_normalize''': True, '''image_mean''': [0.5, 0.5, 0.5], '''image_std''': [0.5, 0.5, 0.5], } lowerCamelCase__ = os.path.join(self.tmpdirname , __lowerCAmelCase ) with open(self.image_processor_file , '''w''' , encoding='''utf-8''' ) as fp: json.dump(__lowerCAmelCase , __lowerCAmelCase ) def __lowerCamelCase ( self , **__lowerCAmelCase ): '''simple docstring''' return BertTokenizer.from_pretrained(self.tmpdirname , **__lowerCAmelCase ) def __lowerCamelCase ( self , **__lowerCAmelCase ): '''simple docstring''' return ViTImageProcessor.from_pretrained(self.tmpdirname , **__lowerCAmelCase ) def __lowerCamelCase ( self ): '''simple docstring''' shutil.rmtree(self.tmpdirname ) def __lowerCamelCase ( self ): '''simple docstring''' lowerCamelCase__ = [np.random.randint(2_5_5 , size=(3, 3_0, 4_0_0) , dtype=np.uinta )] lowerCamelCase__ = [Image.fromarray(np.moveaxis(__lowerCAmelCase , 0 , -1 ) ) for x in image_inputs] return image_inputs def __lowerCamelCase ( self ): '''simple docstring''' lowerCamelCase__ = self.get_tokenizer() lowerCamelCase__ = self.get_image_processor() lowerCamelCase__ = VisionTextDualEncoderProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) processor.save_pretrained(self.tmpdirname ) lowerCamelCase__ = VisionTextDualEncoderProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer.get_vocab() ) self.assertIsInstance(processor.tokenizer , (BertTokenizer, BertTokenizerFast) ) self.assertEqual(processor.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertIsInstance(processor.image_processor , __lowerCAmelCase ) def __lowerCamelCase ( self ): '''simple docstring''' lowerCamelCase__ = VisionTextDualEncoderProcessor( tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) lowerCamelCase__ = self.get_tokenizer(bos_token='''(BOS)''' , eos_token='''(EOS)''' ) lowerCamelCase__ = self.get_image_processor(do_normalize=__lowerCAmelCase , padding_value=1.0 ) lowerCamelCase__ = VisionTextDualEncoderProcessor.from_pretrained( self.tmpdirname , bos_token='''(BOS)''' , eos_token='''(EOS)''' , do_normalize=__lowerCAmelCase , padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , (BertTokenizer, BertTokenizerFast) ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , __lowerCAmelCase ) def __lowerCamelCase ( self ): '''simple docstring''' lowerCamelCase__ = self.get_image_processor() lowerCamelCase__ = self.get_tokenizer() lowerCamelCase__ = VisionTextDualEncoderProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) lowerCamelCase__ = self.prepare_image_inputs() lowerCamelCase__ = image_processor(__lowerCAmelCase , return_tensors='''np''' ) lowerCamelCase__ = processor(images=__lowerCAmelCase , return_tensors='''np''' ) for key in input_feat_extract.keys(): self.assertAlmostEqual(input_feat_extract[key].sum() , input_processor[key].sum() , delta=1E-2 ) def __lowerCamelCase ( self ): '''simple docstring''' lowerCamelCase__ = self.get_image_processor() lowerCamelCase__ = self.get_tokenizer() lowerCamelCase__ = VisionTextDualEncoderProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) lowerCamelCase__ = '''lower newer''' lowerCamelCase__ = processor(text=__lowerCAmelCase ) lowerCamelCase__ = tokenizer(__lowerCAmelCase ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def __lowerCamelCase ( self ): '''simple docstring''' lowerCamelCase__ = self.get_image_processor() lowerCamelCase__ = self.get_tokenizer() lowerCamelCase__ = VisionTextDualEncoderProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) lowerCamelCase__ = '''lower newer''' lowerCamelCase__ = self.prepare_image_inputs() lowerCamelCase__ = processor(text=__lowerCAmelCase , images=__lowerCAmelCase ) self.assertListEqual(list(inputs.keys() ) , ['''input_ids''', '''token_type_ids''', '''attention_mask''', '''pixel_values'''] ) # test if it raises when no input is passed with self.assertRaises(__lowerCAmelCase ): processor() def __lowerCamelCase ( self ): '''simple docstring''' lowerCamelCase__ = self.get_image_processor() lowerCamelCase__ = self.get_tokenizer() lowerCamelCase__ = VisionTextDualEncoderProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) lowerCamelCase__ = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] lowerCamelCase__ = processor.batch_decode(__lowerCAmelCase ) lowerCamelCase__ = tokenizer.batch_decode(__lowerCAmelCase ) self.assertListEqual(__lowerCAmelCase , __lowerCAmelCase ) def __lowerCamelCase ( self ): '''simple docstring''' lowerCamelCase__ = self.get_image_processor() lowerCamelCase__ = self.get_tokenizer() lowerCamelCase__ = VisionTextDualEncoderProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) lowerCamelCase__ = '''lower newer''' lowerCamelCase__ = self.prepare_image_inputs() lowerCamelCase__ = processor(text=__lowerCAmelCase , images=__lowerCAmelCase ) self.assertListEqual(list(inputs.keys() ) , processor.model_input_names )
209
import logging import os import sys import warnings from dataclasses import dataclass, field from random import randint from typing import Optional import datasets import evaluate import numpy as np from datasets import DatasetDict, load_dataset import transformers from transformers import ( AutoConfig, AutoFeatureExtractor, AutoModelForAudioClassification, 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 _a = 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.14.0", "To fix: pip install -r examples/pytorch/audio-classification/requirements.txt") def lowerCAmelCase__(__snake_case ,__snake_case ,__snake_case = 16000 ) -> Any: '''simple docstring''' lowerCamelCase__ = int(round(sample_rate * max_length ) ) if len(__snake_case ) <= sample_length: return wav lowerCamelCase__ = randint(0 ,len(__snake_case ) - sample_length - 1 ) return wav[random_offset : random_offset + sample_length] @dataclass class __A : '''simple docstring''' lowerCAmelCase_ = field(default=lowerCAmelCase , metadata={"""help""": """Name of a dataset from the datasets package"""} ) lowerCAmelCase_ = field( default=lowerCAmelCase , metadata={"""help""": """The configuration name of the dataset to use (via the datasets library)."""} ) lowerCAmelCase_ = field( default=lowerCAmelCase , metadata={"""help""": """A file containing the training audio paths and labels."""} ) lowerCAmelCase_ = field( default=lowerCAmelCase , metadata={"""help""": """A file containing the validation audio paths and labels."""} ) lowerCAmelCase_ = field( default="""train""" , metadata={ """help""": """The name of the training data set split to use (via the datasets library). Defaults to 'train'""" } , ) lowerCAmelCase_ = field( default="""validation""" , metadata={ """help""": ( """The name of the training data set split to use (via the datasets library). Defaults to 'validation'""" ) } , ) lowerCAmelCase_ = field( default="""audio""" , metadata={"""help""": """The name of the dataset column containing the audio data. Defaults to 'audio'"""} , ) lowerCAmelCase_ = field( default="""label""" , metadata={"""help""": """The name of the dataset column containing the labels. Defaults to 'label'"""} ) lowerCAmelCase_ = field( default=lowerCAmelCase , metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of training examples to this """ """value if set.""" ) } , ) lowerCAmelCase_ = field( default=lowerCAmelCase , metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of evaluation examples to this """ """value if set.""" ) } , ) lowerCAmelCase_ = field( default=20 , metadata={"""help""": """Audio clips will be randomly cut to this length during training if the value is set."""} , ) @dataclass class __A : '''simple docstring''' lowerCAmelCase_ = field( default="""facebook/wav2vec2-base""" , metadata={"""help""": """Path to pretrained model or model identifier from huggingface.co/models"""} , ) lowerCAmelCase_ = field( default=lowerCAmelCase , metadata={"""help""": """Pretrained config name or path if not the same as model_name"""} ) lowerCAmelCase_ = field( default=lowerCAmelCase , metadata={"""help""": """Where do you want to store the pretrained models downloaded from the Hub"""} ) lowerCAmelCase_ = field( default="""main""" , metadata={"""help""": """The specific model version to use (can be a branch name, tag name or commit id)."""} , ) lowerCAmelCase_ = field( default=lowerCAmelCase , metadata={"""help""": """Name or path of preprocessor config."""} ) lowerCAmelCase_ = field( default=lowerCAmelCase , metadata={"""help""": """Whether to freeze the feature encoder layers of the model."""} ) lowerCAmelCase_ = field( default=lowerCAmelCase , metadata={"""help""": """Whether to generate an attention mask in the feature extractor."""} ) lowerCAmelCase_ = field( default=lowerCAmelCase , metadata={ """help""": ( """Will use the token generated when running `huggingface-cli login` (necessary to use this script """ """with private models).""" ) } , ) lowerCAmelCase_ = field( default=lowerCAmelCase , metadata={"""help""": """Whether to freeze the feature extractor layers of the model."""} ) lowerCAmelCase_ = field( default=lowerCAmelCase , metadata={"""help""": """Will enable to load a pretrained model whose head dimensions are different."""} , ) def __lowerCamelCase ( self ): '''simple docstring''' if not self.freeze_feature_extractor and self.freeze_feature_encoder: warnings.warn( '''The argument `--freeze_feature_extractor` is deprecated and ''' '''will be removed in a future version. Use `--freeze_feature_encoder`''' '''instead. Setting `freeze_feature_encoder==True`.''' , __lowerCAmelCase , ) if self.freeze_feature_extractor and not self.freeze_feature_encoder: raise ValueError( '''The argument `--freeze_feature_extractor` is deprecated and ''' '''should not be used in combination with `--freeze_feature_encoder`.''' '''Only make use of `--freeze_feature_encoder`.''' ) def lowerCAmelCase__() -> Optional[int]: '''simple docstring''' lowerCamelCase__ = 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. lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ = 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_audio_classification''' ,__snake_case ,__snake_case ) # Setup logging logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''' ,datefmt='''%m/%d/%Y %H:%M:%S''' ,handlers=[logging.StreamHandler(sys.stdout )] ,) if training_args.should_log: # The default of training_args.log_level is passive, so we set log level at info here to have that default. transformers.utils.logging.set_verbosity_info() lowerCamelCase__ = training_args.get_process_log_level() logger.setLevel(__snake_case ) transformers.utils.logging.set_verbosity(__snake_case ) 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}' ) # Set seed before initializing model. set_seed(training_args.seed ) # Detecting last checkpoint. lowerCamelCase__ = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: lowerCamelCase__ = 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 train from scratch.''' ) 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.''' ) # Initialize our dataset and prepare it for the audio classification task. lowerCamelCase__ = DatasetDict() lowerCamelCase__ = load_dataset( data_args.dataset_name ,data_args.dataset_config_name ,split=data_args.train_split_name ,use_auth_token=True if model_args.use_auth_token else None ,) lowerCamelCase__ = load_dataset( data_args.dataset_name ,data_args.dataset_config_name ,split=data_args.eval_split_name ,use_auth_token=True if model_args.use_auth_token else None ,) if data_args.audio_column_name not in raw_datasets["train"].column_names: raise ValueError( F'--audio_column_name {data_args.audio_column_name} not found in dataset \'{data_args.dataset_name}\'. ' '''Make sure to set `--audio_column_name` to the correct audio column - one of ''' F'{", ".join(raw_datasets["train"].column_names )}.' ) if data_args.label_column_name not in raw_datasets["train"].column_names: raise ValueError( F'--label_column_name {data_args.label_column_name} not found in dataset \'{data_args.dataset_name}\'. ' '''Make sure to set `--label_column_name` to the correct text column - one of ''' F'{", ".join(raw_datasets["train"].column_names )}.' ) # Setting `return_attention_mask=True` is the way to get a correctly masked mean-pooling over # transformer outputs in the classifier, but it doesn't always lead to better accuracy lowerCamelCase__ = AutoFeatureExtractor.from_pretrained( model_args.feature_extractor_name or model_args.model_name_or_path ,return_attention_mask=model_args.attention_mask ,cache_dir=model_args.cache_dir ,revision=model_args.model_revision ,use_auth_token=True if model_args.use_auth_token else None ,) # `datasets` takes care of automatically loading and resampling the audio, # so we just need to set the correct target sampling rate. lowerCamelCase__ = raw_datasets.cast_column( data_args.audio_column_name ,datasets.features.Audio(sampling_rate=feature_extractor.sampling_rate ) ) lowerCamelCase__ = feature_extractor.model_input_names[0] def train_transforms(__snake_case ): lowerCamelCase__ = [] for audio in batch[data_args.audio_column_name]: lowerCamelCase__ = random_subsample( audio['''array'''] ,max_length=data_args.max_length_seconds ,sample_rate=feature_extractor.sampling_rate ) subsampled_wavs.append(__snake_case ) lowerCamelCase__ = feature_extractor(__snake_case ,sampling_rate=feature_extractor.sampling_rate ) lowerCamelCase__ = {model_input_name: inputs.get(__snake_case )} lowerCamelCase__ = list(batch[data_args.label_column_name] ) return output_batch def val_transforms(__snake_case ): lowerCamelCase__ = [audio['''array'''] for audio in batch[data_args.audio_column_name]] lowerCamelCase__ = feature_extractor(__snake_case ,sampling_rate=feature_extractor.sampling_rate ) lowerCamelCase__ = {model_input_name: inputs.get(__snake_case )} lowerCamelCase__ = list(batch[data_args.label_column_name] ) return output_batch # Prepare label mappings. # We'll include these in the model's config to get human readable labels in the Inference API. lowerCamelCase__ = raw_datasets['''train'''].features[data_args.label_column_name].names lowerCamelCase__ , lowerCamelCase__ = {}, {} for i, label in enumerate(__snake_case ): lowerCamelCase__ = str(__snake_case ) lowerCamelCase__ = label # Load the accuracy metric from the datasets package lowerCamelCase__ = evaluate.load('''accuracy''' ) # Define our compute_metrics function. It takes an `EvalPrediction` object (a namedtuple with # `predictions` and `label_ids` fields) and has to return a dictionary string to float. def compute_metrics(__snake_case ): lowerCamelCase__ = np.argmax(eval_pred.predictions ,axis=1 ) return metric.compute(predictions=__snake_case ,references=eval_pred.label_ids ) lowerCamelCase__ = AutoConfig.from_pretrained( model_args.config_name or model_args.model_name_or_path ,num_labels=len(__snake_case ) ,labelaid=__snake_case ,idalabel=__snake_case ,finetuning_task='''audio-classification''' ,cache_dir=model_args.cache_dir ,revision=model_args.model_revision ,use_auth_token=True if model_args.use_auth_token else None ,) lowerCamelCase__ = AutoModelForAudioClassification.from_pretrained( model_args.model_name_or_path ,from_tf=bool('''.ckpt''' in model_args.model_name_or_path ) ,config=__snake_case ,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 ,) # freeze the convolutional waveform encoder if model_args.freeze_feature_encoder: model.freeze_feature_encoder() if training_args.do_train: if data_args.max_train_samples is not None: lowerCamelCase__ = ( raw_datasets['''train'''].shuffle(seed=training_args.seed ).select(range(data_args.max_train_samples ) ) ) # Set the training transforms raw_datasets["train"].set_transform(__snake_case ,output_all_columns=__snake_case ) if training_args.do_eval: if data_args.max_eval_samples is not None: lowerCamelCase__ = ( raw_datasets['''eval'''].shuffle(seed=training_args.seed ).select(range(data_args.max_eval_samples ) ) ) # Set the validation transforms raw_datasets["eval"].set_transform(__snake_case ,output_all_columns=__snake_case ) # Initialize our trainer lowerCamelCase__ = Trainer( model=__snake_case ,args=__snake_case ,train_dataset=raw_datasets['''train'''] if training_args.do_train else None ,eval_dataset=raw_datasets['''eval'''] if training_args.do_eval else None ,compute_metrics=__snake_case ,tokenizer=__snake_case ,) # Training if training_args.do_train: lowerCamelCase__ = None if training_args.resume_from_checkpoint is not None: lowerCamelCase__ = training_args.resume_from_checkpoint elif last_checkpoint is not None: lowerCamelCase__ = last_checkpoint lowerCamelCase__ = trainer.train(resume_from_checkpoint=__snake_case ) 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: lowerCamelCase__ = trainer.evaluate() trainer.log_metrics('''eval''' ,__snake_case ) trainer.save_metrics('''eval''' ,__snake_case ) # Write model card and (optionally) push to hub lowerCamelCase__ = { '''finetuned_from''': model_args.model_name_or_path, '''tasks''': '''audio-classification''', '''dataset''': data_args.dataset_name, '''tags''': ['''audio-classification'''], } if training_args.push_to_hub: trainer.push_to_hub(**__snake_case ) else: trainer.create_model_card(**__snake_case ) if __name__ == "__main__": main()
209
1
import os def lowerCamelCase_ ( ): with open(os.path.dirname(lowerCamelCase__ ) + "/grid.txt" ) as f: lowerCamelCase_ = [] # noqa: E741 for _ in range(2_0 ): l.append([int(lowerCamelCase__ ) for x in f.readline().split()] ) lowerCamelCase_ = 0 # right for i in range(2_0 ): for j in range(1_7 ): lowerCamelCase_ = l[i][j] * l[i][j + 1] * l[i][j + 2] * l[i][j + 3] if temp > maximum: lowerCamelCase_ = temp # down for i in range(1_7 ): for j in range(2_0 ): lowerCamelCase_ = l[i][j] * l[i + 1][j] * l[i + 2][j] * l[i + 3][j] if temp > maximum: lowerCamelCase_ = temp # diagonal 1 for i in range(1_7 ): for j in range(1_7 ): lowerCamelCase_ = l[i][j] * l[i + 1][j + 1] * l[i + 2][j + 2] * l[i + 3][j + 3] if temp > maximum: lowerCamelCase_ = temp # diagonal 2 for i in range(1_7 ): for j in range(3 , 2_0 ): lowerCamelCase_ = l[i][j] * l[i + 1][j - 1] * l[i + 2][j - 2] * l[i + 3][j - 3] if temp > maximum: lowerCamelCase_ = temp return maximum if __name__ == "__main__": print(solution())
47
import importlib import torch import yaml from omegaconf import OmegaConf from taming.models.vqgan import VQModel def lowerCamelCase_ ( lowerCamelCase__ , lowerCamelCase__=False ): lowerCamelCase_ = OmegaConf.load(lowerCamelCase__ ) if display: print(yaml.dump(OmegaConf.to_container(lowerCamelCase__ ) ) ) return config def lowerCamelCase_ ( lowerCamelCase__ , lowerCamelCase__=None , lowerCamelCase__=None ): if conf_path is None: lowerCamelCase_ = "./model_checkpoints/vqgan_only.yaml" lowerCamelCase_ = load_config(lowerCamelCase__ , display=lowerCamelCase__ ) lowerCamelCase_ = VQModel(**config.model.params ) if ckpt_path is None: lowerCamelCase_ = "./model_checkpoints/vqgan_only.pt" lowerCamelCase_ = torch.load(lowerCamelCase__ , map_location=lowerCamelCase__ ) if ".ckpt" in ckpt_path: lowerCamelCase_ = sd["state_dict"] model.load_state_dict(lowerCamelCase__ , strict=lowerCamelCase__ ) model.to(lowerCamelCase__ ) del sd return model def lowerCamelCase_ ( lowerCamelCase__ , lowerCamelCase__ ): lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ = model.encode(lowerCamelCase__ ) print(F'VQGAN --- {model.__class__.__name__}: latent shape: {z.shape[2:]}' ) lowerCamelCase_ = model.decode(lowerCamelCase__ ) return xrec def lowerCamelCase_ ( lowerCamelCase__ , lowerCamelCase__=False ): lowerCamelCase_ , lowerCamelCase_ = string.rsplit("." , 1 ) if reload: lowerCamelCase_ = importlib.import_module(lowerCamelCase__ ) importlib.reload(lowerCamelCase__ ) return getattr(importlib.import_module(lowerCamelCase__ , package=lowerCamelCase__ ) , cls ) def lowerCamelCase_ ( lowerCamelCase__ ): if "target" not in config: raise KeyError("Expected key `target` to instantiate." ) return get_obj_from_str(config["target"] )(**config.get("params" , {} ) ) def lowerCamelCase_ ( lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__=True , lowerCamelCase__=True ): lowerCamelCase_ = instantiate_from_config(lowerCamelCase__ ) if sd is not None: model.load_state_dict(lowerCamelCase__ ) if gpu: model.cuda() if eval_mode: model.eval() return {"model": model} def lowerCamelCase_ ( lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ): # load the specified checkpoint if ckpt: lowerCamelCase_ = torch.load(lowerCamelCase__ , map_location="cpu" ) lowerCamelCase_ = pl_sd["global_step"] print(F'loaded model from global step {global_step}.' ) else: lowerCamelCase_ = {"state_dict": None} lowerCamelCase_ = None lowerCamelCase_ = load_model_from_config(config.model , pl_sd["state_dict"] , gpu=lowerCamelCase__ , eval_mode=lowerCamelCase__ )["model"] return model, global_step
47
1
import argparse import json from pathlib import Path import requests import timm import torch from huggingface_hub import hf_hub_download from PIL import Image from timm.data import resolve_data_config from timm.data.transforms_factory import create_transform from transformers import ( BitConfig, ViTHybridConfig, ViTHybridForImageClassification, ViTHybridImageProcessor, ViTHybridModel, ) from transformers.image_utils import PILImageResampling from transformers.utils import logging logging.set_verbosity_info() lowerCAmelCase : Any = logging.get_logger(__name__) def A_ ( _UpperCAmelCase , _UpperCAmelCase=False ): SCREAMING_SNAKE_CASE_: Any = [] # fmt: off # stem: rename_keys.append(("cls_token", "vit.embeddings.cls_token") ) rename_keys.append(("pos_embed", "vit.embeddings.position_embeddings") ) rename_keys.append(("patch_embed.proj.weight", "vit.embeddings.patch_embeddings.projection.weight") ) rename_keys.append(("patch_embed.proj.bias", "vit.embeddings.patch_embeddings.projection.bias") ) # backbone rename_keys.append(("patch_embed.backbone.stem.conv.weight", "vit.embeddings.patch_embeddings.backbone.bit.embedder.convolution.weight") ) rename_keys.append(("patch_embed.backbone.stem.norm.weight", "vit.embeddings.patch_embeddings.backbone.bit.embedder.norm.weight") ) rename_keys.append(("patch_embed.backbone.stem.norm.bias", "vit.embeddings.patch_embeddings.backbone.bit.embedder.norm.bias") ) for stage_idx in range(len(config.backbone_config.depths ) ): for layer_idx in range(config.backbone_config.depths[stage_idx] ): rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.conv1.weight", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.conv1.weight") ) rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm1.weight", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm1.weight") ) rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm1.bias", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm1.bias") ) rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.conv2.weight", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.conv2.weight") ) rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm2.weight", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm2.weight") ) rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm2.bias", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm2.bias") ) rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.conv3.weight", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.conv3.weight") ) rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm3.weight", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm3.weight") ) rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm3.bias", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm3.bias") ) rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.0.downsample.conv.weight", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.0.downsample.conv.weight") ) rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.0.downsample.norm.weight", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.0.downsample.norm.weight") ) rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.0.downsample.norm.bias", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.0.downsample.norm.bias") ) # transformer encoder for i in range(config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append((f"blocks.{i}.norm1.weight", f"vit.encoder.layer.{i}.layernorm_before.weight") ) rename_keys.append((f"blocks.{i}.norm1.bias", f"vit.encoder.layer.{i}.layernorm_before.bias") ) rename_keys.append((f"blocks.{i}.attn.proj.weight", f"vit.encoder.layer.{i}.attention.output.dense.weight") ) rename_keys.append((f"blocks.{i}.attn.proj.bias", f"vit.encoder.layer.{i}.attention.output.dense.bias") ) rename_keys.append((f"blocks.{i}.norm2.weight", f"vit.encoder.layer.{i}.layernorm_after.weight") ) rename_keys.append((f"blocks.{i}.norm2.bias", f"vit.encoder.layer.{i}.layernorm_after.bias") ) rename_keys.append((f"blocks.{i}.mlp.fc1.weight", f"vit.encoder.layer.{i}.intermediate.dense.weight") ) rename_keys.append((f"blocks.{i}.mlp.fc1.bias", f"vit.encoder.layer.{i}.intermediate.dense.bias") ) rename_keys.append((f"blocks.{i}.mlp.fc2.weight", f"vit.encoder.layer.{i}.output.dense.weight") ) rename_keys.append((f"blocks.{i}.mlp.fc2.bias", f"vit.encoder.layer.{i}.output.dense.bias") ) if base_model: # layernorm + pooler rename_keys.extend( [ ("norm.weight", "layernorm.weight"), ("norm.bias", "layernorm.bias"), ("pre_logits.fc.weight", "pooler.dense.weight"), ("pre_logits.fc.bias", "pooler.dense.bias"), ] ) # if just the base model, we should remove "vit" from all keys that start with "vit" SCREAMING_SNAKE_CASE_: List[str] = [(pair[0], pair[1][4:]) if pair[1].startswith("vit" ) else pair for pair in rename_keys] else: # layernorm + classification head rename_keys.extend( [ ("norm.weight", "vit.layernorm.weight"), ("norm.bias", "vit.layernorm.bias"), ("head.weight", "classifier.weight"), ("head.bias", "classifier.bias"), ] ) # fmt: on return rename_keys def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase=False ): for i in range(config.num_hidden_layers ): if base_model: SCREAMING_SNAKE_CASE_: Dict = "" else: SCREAMING_SNAKE_CASE_: Any = "vit." # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) SCREAMING_SNAKE_CASE_: Dict = state_dict.pop(f"blocks.{i}.attn.qkv.weight" ) SCREAMING_SNAKE_CASE_: List[str] = state_dict.pop(f"blocks.{i}.attn.qkv.bias" ) # next, add query, keys and values (in that order) to the state dict SCREAMING_SNAKE_CASE_: Any = in_proj_weight[ : config.hidden_size, : ] SCREAMING_SNAKE_CASE_: Optional[Any] = in_proj_bias[: config.hidden_size] SCREAMING_SNAKE_CASE_: Dict = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] SCREAMING_SNAKE_CASE_: List[Any] = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] SCREAMING_SNAKE_CASE_: Optional[int] = in_proj_weight[ -config.hidden_size :, : ] SCREAMING_SNAKE_CASE_: Union[str, Any] = in_proj_bias[-config.hidden_size :] def A_ ( _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Any = ["head.weight", "head.bias"] for k in ignore_keys: state_dict.pop(_UpperCAmelCase , _UpperCAmelCase ) def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Optional[int] = dct.pop(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: List[Any] = val def A_ ( ): SCREAMING_SNAKE_CASE_: List[Any] = "http://images.cocodataset.org/val2017/000000039769.jpg" SCREAMING_SNAKE_CASE_: List[str] = Image.open(requests.get(_UpperCAmelCase , stream=_UpperCAmelCase ).raw ) return im @torch.no_grad() def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase=False ): SCREAMING_SNAKE_CASE_: List[Any] = BitConfig( global_padding="same" , layer_type="bottleneck" , depths=(3, 4, 9) , out_features=["stage3"] , embedding_dynamic_padding=_UpperCAmelCase , ) SCREAMING_SNAKE_CASE_: Dict = ViTHybridConfig(backbone_config=_UpperCAmelCase , image_size=3_84 , num_labels=10_00 ) SCREAMING_SNAKE_CASE_: List[Any] = False # load original model from timm SCREAMING_SNAKE_CASE_: List[Any] = timm.create_model(_UpperCAmelCase , pretrained=_UpperCAmelCase ) timm_model.eval() # load state_dict of original model, remove and rename some keys SCREAMING_SNAKE_CASE_: List[Any] = timm_model.state_dict() if base_model: remove_classification_head_(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Tuple = create_rename_keys(_UpperCAmelCase , _UpperCAmelCase ) for src, dest in rename_keys: rename_key(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) read_in_q_k_v(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Optional[int] = "huggingface/label-files" SCREAMING_SNAKE_CASE_: int = "imagenet-1k-id2label.json" SCREAMING_SNAKE_CASE_: Union[str, Any] = json.load(open(hf_hub_download(_UpperCAmelCase , _UpperCAmelCase , repo_type="dataset" ) , "r" ) ) SCREAMING_SNAKE_CASE_: int = {int(_UpperCAmelCase ): v for k, v in idalabel.items()} SCREAMING_SNAKE_CASE_: int = idalabel SCREAMING_SNAKE_CASE_: Dict = {v: k for k, v in idalabel.items()} # load HuggingFace model if vit_name[-5:] == "in21k": SCREAMING_SNAKE_CASE_: str = ViTHybridModel(_UpperCAmelCase ).eval() else: SCREAMING_SNAKE_CASE_: Optional[Any] = ViTHybridForImageClassification(_UpperCAmelCase ).eval() model.load_state_dict(_UpperCAmelCase ) # create image processor SCREAMING_SNAKE_CASE_: Dict = create_transform(**resolve_data_config({} , model=_UpperCAmelCase ) ) SCREAMING_SNAKE_CASE_: List[Any] = transform.transforms SCREAMING_SNAKE_CASE_: Optional[int] = { "bilinear": PILImageResampling.BILINEAR, "bicubic": PILImageResampling.BICUBIC, "nearest": PILImageResampling.NEAREST, } SCREAMING_SNAKE_CASE_: int = ViTHybridImageProcessor( do_resize=_UpperCAmelCase , size={"shortest_edge": timm_transforms[0].size} , resample=pillow_resamplings[timm_transforms[0].interpolation.value] , do_center_crop=_UpperCAmelCase , crop_size={"height": timm_transforms[1].size[0], "width": timm_transforms[1].size[1]} , do_normalize=_UpperCAmelCase , image_mean=timm_transforms[-1].mean.tolist() , image_std=timm_transforms[-1].std.tolist() , ) SCREAMING_SNAKE_CASE_: int = prepare_img() SCREAMING_SNAKE_CASE_: Tuple = transform(_UpperCAmelCase ).unsqueeze(0 ) SCREAMING_SNAKE_CASE_: Optional[int] = processor(_UpperCAmelCase , return_tensors="pt" ).pixel_values # verify pixel values assert torch.allclose(_UpperCAmelCase , _UpperCAmelCase ) # verify logits with torch.no_grad(): SCREAMING_SNAKE_CASE_: Optional[int] = model(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Optional[int] = outputs.logits print("Predicted class:" , logits.argmax(-1 ).item() ) if base_model: SCREAMING_SNAKE_CASE_: Tuple = timm_model.forward_features(_UpperCAmelCase ) assert timm_pooled_output.shape == outputs.pooler_output.shape assert torch.allclose(_UpperCAmelCase , outputs.pooler_output , atol=1e-3 ) else: SCREAMING_SNAKE_CASE_: int = timm_model(_UpperCAmelCase ) assert timm_logits.shape == outputs.logits.shape assert torch.allclose(_UpperCAmelCase , outputs.logits , atol=1e-3 ) print("Looks ok!" ) if pytorch_dump_folder_path is not None: Path(_UpperCAmelCase ).mkdir(exist_ok=_UpperCAmelCase ) print(f"Saving model {vit_name} to {pytorch_dump_folder_path}" ) model.save_pretrained(_UpperCAmelCase ) print(f"Saving processor to {pytorch_dump_folder_path}" ) processor.save_pretrained(_UpperCAmelCase ) if push_to_hub: print(f"Pushing model and processor to the hub {vit_name}" ) model.push_to_hub(f"ybelkada/{vit_name}" ) processor.push_to_hub(f"ybelkada/{vit_name}" ) if __name__ == "__main__": lowerCAmelCase : List[str] = argparse.ArgumentParser() # Required parameters parser.add_argument( """--vit_name""", default="""vit_base_r50_s16_384""", type=str, help="""Name of the hybrid ViT timm model you\'d like to convert.""", ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model directory.""" ) parser.add_argument( """--push_to_hub""", action="""store_true""", help="""Whether to upload the model to the HuggingFace hub.""" ) lowerCAmelCase : Optional[int] = parser.parse_args() convert_vit_checkpoint(args.vit_name, args.pytorch_dump_folder_path, args.push_to_hub)
13
from maths.prime_factors import prime_factors def lowerCAmelCase__( lowercase : int ) -> int: if not isinstance(lowercase , lowercase ): __snake_case : Optional[int] = f"""Input value of [number={number}] must be an integer""" raise TypeError(lowercase ) if number < 1: raise ValueError("Input must be a positive integer" ) return -1 if len(prime_factors(lowercase ) ) % 2 else 1 if __name__ == "__main__": import doctest doctest.testmod()
326
0
'''simple docstring''' import enum import shutil import sys __a , __a = shutil.get_terminal_size() __a = {'UP': 'A', 'DOWN': 'B', 'RIGHT': 'C', 'LEFT': 'D'} class A__ ( enum.Enum ): """simple docstring""" UpperCamelCase_ : Tuple = 0 UpperCamelCase_ : List[str] = 1 def __UpperCAmelCase ( a_: Dict, a_: Any="" ): sys.stdout.write(str(a_ ) + end ) sys.stdout.flush() def __UpperCAmelCase ( a_: str, a_: List[Any], a_: str="" ): forceWrite(f"""\u001b[{color}m{content}\u001b[0m""", a_ ) def __UpperCAmelCase ( ): forceWrite("\r" ) def __UpperCAmelCase ( a_: int, a_: str ): forceWrite(f"""\033[{num_lines}{CURSOR_TO_CHAR[direction.upper()]}""" ) def __UpperCAmelCase ( ): forceWrite(" " * TERMINAL_WIDTH ) reset_cursor() def __UpperCAmelCase ( ): reset_cursor() forceWrite("-" * TERMINAL_WIDTH )
353
'''simple docstring''' import torch from diffusers import EulerDiscreteScheduler from diffusers.utils import torch_device from .test_schedulers import SchedulerCommonTest class A__ ( UpperCamelCase ): """simple docstring""" UpperCamelCase_ : Optional[int] = (EulerDiscreteScheduler,) UpperCamelCase_ : Tuple = 10 def _lowerCAmelCase ( self : Dict , **lowerCAmelCase__ : Tuple ) -> Any: """simple docstring""" _UpperCAmelCase : str = { "num_train_timesteps": 1_1_0_0, "beta_start": 0.0001, "beta_end": 0.02, "beta_schedule": "linear", } config.update(**lowerCAmelCase__ ) return config def _lowerCAmelCase ( self : Optional[Any] ) -> Union[str, Any]: """simple docstring""" for timesteps in [1_0, 5_0, 1_0_0, 1_0_0_0]: self.check_over_configs(num_train_timesteps=lowerCAmelCase__ ) def _lowerCAmelCase ( self : Any ) -> List[str]: """simple docstring""" for beta_start, beta_end in zip([0.0_0001, 0.0001, 0.001] , [0.0002, 0.002, 0.02] ): self.check_over_configs(beta_start=lowerCAmelCase__ , beta_end=lowerCAmelCase__ ) def _lowerCAmelCase ( self : List[str] ) -> List[str]: """simple docstring""" for schedule in ["linear", "scaled_linear"]: self.check_over_configs(beta_schedule=lowerCAmelCase__ ) def _lowerCAmelCase ( self : Union[str, Any] ) -> Optional[Any]: """simple docstring""" for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=lowerCAmelCase__ ) def _lowerCAmelCase ( self : List[Any] ) -> List[Any]: """simple docstring""" _UpperCAmelCase : List[str] = self.scheduler_classes[0] _UpperCAmelCase : int = self.get_scheduler_config() _UpperCAmelCase : Optional[int] = scheduler_class(**lowerCAmelCase__ ) scheduler.set_timesteps(self.num_inference_steps ) _UpperCAmelCase : int = torch.manual_seed(0 ) _UpperCAmelCase : Any = self.dummy_model() _UpperCAmelCase : List[str] = self.dummy_sample_deter * scheduler.init_noise_sigma _UpperCAmelCase : List[Any] = sample.to(lowerCAmelCase__ ) for i, t in enumerate(scheduler.timesteps ): _UpperCAmelCase : List[str] = scheduler.scale_model_input(lowerCAmelCase__ , lowerCAmelCase__ ) _UpperCAmelCase : int = model(lowerCAmelCase__ , lowerCAmelCase__ ) _UpperCAmelCase : int = scheduler.step(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , generator=lowerCAmelCase__ ) _UpperCAmelCase : Optional[int] = output.prev_sample _UpperCAmelCase : Optional[Any] = torch.sum(torch.abs(lowerCAmelCase__ ) ) _UpperCAmelCase : Tuple = torch.mean(torch.abs(lowerCAmelCase__ ) ) assert abs(result_sum.item() - 10.0807 ) < 1e-2 assert abs(result_mean.item() - 0.0131 ) < 1e-3 def _lowerCAmelCase ( self : Union[str, Any] ) -> List[Any]: """simple docstring""" _UpperCAmelCase : Any = self.scheduler_classes[0] _UpperCAmelCase : List[Any] = self.get_scheduler_config(prediction_type="v_prediction" ) _UpperCAmelCase : Any = scheduler_class(**lowerCAmelCase__ ) scheduler.set_timesteps(self.num_inference_steps ) _UpperCAmelCase : str = torch.manual_seed(0 ) _UpperCAmelCase : Optional[Any] = self.dummy_model() _UpperCAmelCase : Union[str, Any] = self.dummy_sample_deter * scheduler.init_noise_sigma _UpperCAmelCase : Tuple = sample.to(lowerCAmelCase__ ) for i, t in enumerate(scheduler.timesteps ): _UpperCAmelCase : Union[str, Any] = scheduler.scale_model_input(lowerCAmelCase__ , lowerCAmelCase__ ) _UpperCAmelCase : int = model(lowerCAmelCase__ , lowerCAmelCase__ ) _UpperCAmelCase : Union[str, Any] = scheduler.step(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , generator=lowerCAmelCase__ ) _UpperCAmelCase : Union[str, Any] = output.prev_sample _UpperCAmelCase : Tuple = torch.sum(torch.abs(lowerCAmelCase__ ) ) _UpperCAmelCase : Any = torch.mean(torch.abs(lowerCAmelCase__ ) ) assert abs(result_sum.item() - 0.0002 ) < 1e-2 assert abs(result_mean.item() - 2.26_76e-06 ) < 1e-3 def _lowerCAmelCase ( self : Tuple ) -> str: """simple docstring""" _UpperCAmelCase : Optional[int] = self.scheduler_classes[0] _UpperCAmelCase : List[Any] = self.get_scheduler_config() _UpperCAmelCase : int = scheduler_class(**lowerCAmelCase__ ) scheduler.set_timesteps(self.num_inference_steps , device=lowerCAmelCase__ ) _UpperCAmelCase : Optional[int] = torch.manual_seed(0 ) _UpperCAmelCase : str = self.dummy_model() _UpperCAmelCase : Any = self.dummy_sample_deter * scheduler.init_noise_sigma.cpu() _UpperCAmelCase : str = sample.to(lowerCAmelCase__ ) for t in scheduler.timesteps: _UpperCAmelCase : List[str] = scheduler.scale_model_input(lowerCAmelCase__ , lowerCAmelCase__ ) _UpperCAmelCase : Any = model(lowerCAmelCase__ , lowerCAmelCase__ ) _UpperCAmelCase : Tuple = scheduler.step(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , generator=lowerCAmelCase__ ) _UpperCAmelCase : int = output.prev_sample _UpperCAmelCase : List[Any] = torch.sum(torch.abs(lowerCAmelCase__ ) ) _UpperCAmelCase : str = torch.mean(torch.abs(lowerCAmelCase__ ) ) assert abs(result_sum.item() - 10.0807 ) < 1e-2 assert abs(result_mean.item() - 0.0131 ) < 1e-3 def _lowerCAmelCase ( self : List[str] ) -> int: """simple docstring""" _UpperCAmelCase : List[Any] = self.scheduler_classes[0] _UpperCAmelCase : int = self.get_scheduler_config() _UpperCAmelCase : Union[str, Any] = scheduler_class(**lowerCAmelCase__ , use_karras_sigmas=lowerCAmelCase__ ) scheduler.set_timesteps(self.num_inference_steps , device=lowerCAmelCase__ ) _UpperCAmelCase : Optional[int] = torch.manual_seed(0 ) _UpperCAmelCase : List[str] = self.dummy_model() _UpperCAmelCase : str = self.dummy_sample_deter * scheduler.init_noise_sigma.cpu() _UpperCAmelCase : Optional[int] = sample.to(lowerCAmelCase__ ) for t in scheduler.timesteps: _UpperCAmelCase : List[Any] = scheduler.scale_model_input(lowerCAmelCase__ , lowerCAmelCase__ ) _UpperCAmelCase : str = model(lowerCAmelCase__ , lowerCAmelCase__ ) _UpperCAmelCase : Optional[Any] = scheduler.step(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , generator=lowerCAmelCase__ ) _UpperCAmelCase : List[Any] = output.prev_sample _UpperCAmelCase : List[Any] = torch.sum(torch.abs(lowerCAmelCase__ ) ) _UpperCAmelCase : Optional[Any] = torch.mean(torch.abs(lowerCAmelCase__ ) ) assert abs(result_sum.item() - 124.52_2994_9951_1719 ) < 1e-2 assert abs(result_mean.item() - 0.1_6213_9326_3339_9963 ) < 1e-3
17
0
'''simple docstring''' # This script creates a super tiny model that is useful inside tests, when we just want to test that # the machinery works, without needing to the check the quality of the outcomes. # # This version creates a tiny vocab first, and then a tiny model - so the outcome is truly tiny - # all files ~60KB. As compared to taking a full-size model, reducing to the minimum its layers and # emb dimensions, but keeping the full vocab + merges files, leading to ~3MB in total for all files. # The latter is done by `fsmt-make-super-tiny-model.py`. # # It will be used then as "stas/tiny-wmt19-en-ru" from pathlib import Path import json import tempfile from transformers import FSMTTokenizer, FSMTConfig, FSMTForConditionalGeneration from transformers.models.fsmt.tokenization_fsmt import VOCAB_FILES_NAMES __lowerCAmelCase = '''tiny-wmt19-en-ru''' # Build # borrowed from a test __lowerCAmelCase = [ '''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''w</w>''', '''r</w>''', '''t</w>''', '''lo''', '''low''', '''er</w>''', '''low</w>''', '''lowest</w>''', '''newer</w>''', '''wider</w>''', '''<unk>''', ] __lowerCAmelCase = dict(zip(vocab, range(len(vocab)))) __lowerCAmelCase = ['''l o 123''', '''lo w 1456''', '''e r</w> 1789''', ''''''] with tempfile.TemporaryDirectory() as tmpdirname: __lowerCAmelCase = Path(tmpdirname) __lowerCAmelCase = build_dir / VOCAB_FILES_NAMES['''src_vocab_file'''] __lowerCAmelCase = build_dir / VOCAB_FILES_NAMES['''tgt_vocab_file'''] __lowerCAmelCase = build_dir / VOCAB_FILES_NAMES['''merges_file'''] with open(src_vocab_file, '''w''') as fp: fp.write(json.dumps(vocab_tokens)) with open(tgt_vocab_file, '''w''') as fp: fp.write(json.dumps(vocab_tokens)) with open(merges_file, '''w''') as fp: fp.write('''\n'''.join(merges)) __lowerCAmelCase = FSMTTokenizer( langs=['''en''', '''ru'''], src_vocab_size=len(vocab), tgt_vocab_size=len(vocab), src_vocab_file=src_vocab_file, tgt_vocab_file=tgt_vocab_file, merges_file=merges_file, ) __lowerCAmelCase = FSMTConfig( langs=['''ru''', '''en'''], src_vocab_size=1_000, tgt_vocab_size=1_000, d_model=4, encoder_layers=1, decoder_layers=1, encoder_ffn_dim=4, decoder_ffn_dim=4, encoder_attention_heads=1, decoder_attention_heads=1, ) __lowerCAmelCase = FSMTForConditionalGeneration(config) print(f"""num of params {tiny_model.num_parameters()}""") # Test __lowerCAmelCase = tokenizer(['''Making tiny model'''], return_tensors='''pt''') __lowerCAmelCase = tiny_model(**batch) print('''test output:''', len(outputs.logits[0])) # Save tiny_model.half() # makes it smaller tiny_model.save_pretrained(mname_tiny) tokenizer.save_pretrained(mname_tiny) print(f"""Generated {mname_tiny}""") # Upload # transformers-cli upload tiny-wmt19-en-ru
89
'''simple docstring''' from typing import Any class __magic_name__ : def __init__( self : List[Any] ,_UpperCAmelCase : Any ): _a : List[Any] = data _a : Union[str, Any] = None def __repr__( self : Any ): return F"""Node({self.data})""" class __magic_name__ : def __init__( self : int ): _a : Tuple = None def __iter__( self : str ): _a : int = self.head while node: yield node.data _a : Union[str, Any] = node.next def __len__( self : Optional[Any] ): return sum(1 for _ in self ) def __repr__( self : str ): return "->".join([str(_UpperCAmelCase ) for item in self] ) def __getitem__( self : Tuple ,_UpperCAmelCase : int ): if not 0 <= index < len(self ): raise ValueError('list index out of range.' ) for i, node in enumerate(self ): if i == index: return node return None def __setitem__( self : Union[str, Any] ,_UpperCAmelCase : int ,_UpperCAmelCase : Any ): if not 0 <= index < len(self ): raise ValueError('list index out of range.' ) _a : Any = self.head for _ in range(_UpperCAmelCase ): _a : Optional[Any] = current.next _a : Optional[int] = data def __lowercase ( self : Optional[int] ,_UpperCAmelCase : Any ): self.insert_nth(len(self ) ,_UpperCAmelCase ) def __lowercase ( self : Union[str, Any] ,_UpperCAmelCase : Any ): self.insert_nth(0 ,_UpperCAmelCase ) def __lowercase ( self : str ,_UpperCAmelCase : int ,_UpperCAmelCase : Any ): if not 0 <= index <= len(self ): raise IndexError('list index out of range' ) _a : int = Node(_UpperCAmelCase ) if self.head is None: _a : str = new_node elif index == 0: _a : List[str] = self.head # link new_node to head _a : Union[str, Any] = new_node else: _a : int = self.head for _ in range(index - 1 ): _a : Union[str, Any] = temp.next _a : List[str] = temp.next _a : Optional[int] = new_node def __lowercase ( self : Optional[int] ): # print every node data print(self ) def __lowercase ( self : str ): return self.delete_nth(0 ) def __lowercase ( self : str ): # delete from tail return self.delete_nth(len(self ) - 1 ) def __lowercase ( self : List[str] ,_UpperCAmelCase : int = 0 ): if not 0 <= index <= len(self ) - 1: # test if index is valid raise IndexError('List index out of range.' ) _a : Optional[Any] = self.head # default first node if index == 0: _a : int = self.head.next else: _a : int = self.head for _ in range(index - 1 ): _a : str = temp.next _a : str = temp.next _a : int = temp.next.next return delete_node.data def __lowercase ( self : List[Any] ): return self.head is None def __lowercase ( self : Tuple ): _a : List[Any] = None _a : Tuple = self.head while current: # Store the current node's next node. _a : Dict = current.next # Make the current node's next point backwards _a : str = prev # Make the previous node be the current node _a : Tuple = current # Make the current node the next node (to progress iteration) _a : Optional[Any] = next_node # Return prev in order to put the head at the end _a : int = prev def __lowerCamelCase ( ) -> None: _a : List[str] = LinkedList() assert linked_list.is_empty() is True assert str(lowerCAmelCase_ ) == "" try: linked_list.delete_head() raise AssertionError # This should not happen. except IndexError: assert True # This should happen. try: linked_list.delete_tail() raise AssertionError # This should not happen. except IndexError: assert True # This should happen. for i in range(10 ): assert len(lowerCAmelCase_ ) == i linked_list.insert_nth(lowerCAmelCase_ , i + 1 ) assert str(lowerCAmelCase_ ) == "->".join(str(lowerCAmelCase_ ) for i in range(1 , 11 ) ) linked_list.insert_head(0 ) linked_list.insert_tail(11 ) assert str(lowerCAmelCase_ ) == "->".join(str(lowerCAmelCase_ ) for i in range(0 , 12 ) ) assert linked_list.delete_head() == 0 assert linked_list.delete_nth(9 ) == 10 assert linked_list.delete_tail() == 11 assert len(lowerCAmelCase_ ) == 9 assert str(lowerCAmelCase_ ) == "->".join(str(lowerCAmelCase_ ) for i in range(1 , 10 ) ) assert all(linked_list[i] == i + 1 for i in range(0 , 9 ) ) is True for i in range(0 , 9 ): _a : Union[str, Any] = -i assert all(linked_list[i] == -i for i in range(0 , 9 ) ) is True linked_list.reverse() assert str(lowerCAmelCase_ ) == "->".join(str(lowerCAmelCase_ ) for i in range(-8 , 1 ) ) def __lowerCamelCase ( ) -> None: _a : Dict = [ -9, 100, Node(77345112 ), 'dlrow olleH', 7, 5555, 0, -192.55_555, 'Hello, world!', 77.9, Node(10 ), None, None, 12.20, ] _a : List[Any] = LinkedList() for i in test_input: linked_list.insert_tail(lowerCAmelCase_ ) # Check if it's empty or not assert linked_list.is_empty() is False assert ( str(lowerCAmelCase_ ) == "-9->100->Node(77345112)->dlrow olleH->7->5555->0->" "-192.55555->Hello, world!->77.9->Node(10)->None->None->12.2" ) # Delete the head _a : List[str] = linked_list.delete_head() assert result == -9 assert ( str(lowerCAmelCase_ ) == "100->Node(77345112)->dlrow olleH->7->5555->0->-192.55555->" "Hello, world!->77.9->Node(10)->None->None->12.2" ) # Delete the tail _a : Dict = linked_list.delete_tail() assert result == 12.2 assert ( str(lowerCAmelCase_ ) == "100->Node(77345112)->dlrow olleH->7->5555->0->-192.55555->" "Hello, world!->77.9->Node(10)->None->None" ) # Delete a node in specific location in linked list _a : Optional[Any] = linked_list.delete_nth(10 ) assert result is None assert ( str(lowerCAmelCase_ ) == "100->Node(77345112)->dlrow olleH->7->5555->0->-192.55555->" "Hello, world!->77.9->Node(10)->None" ) # Add a Node instance to its head linked_list.insert_head(Node('Hello again, world!' ) ) assert ( str(lowerCAmelCase_ ) == "Node(Hello again, world!)->100->Node(77345112)->dlrow olleH->" "7->5555->0->-192.55555->Hello, world!->77.9->Node(10)->None" ) # Add None to its tail linked_list.insert_tail(lowerCAmelCase_ ) assert ( str(lowerCAmelCase_ ) == "Node(Hello again, world!)->100->Node(77345112)->dlrow olleH->" "7->5555->0->-192.55555->Hello, world!->77.9->Node(10)->None->None" ) # Reverse the linked list linked_list.reverse() assert ( str(lowerCAmelCase_ ) == "None->None->Node(10)->77.9->Hello, world!->-192.55555->0->5555->" "7->dlrow olleH->Node(77345112)->100->Node(Hello again, world!)" ) def __lowerCamelCase ( ) -> Union[str, Any]: from doctest import testmod testmod() _a : Optional[int] = LinkedList() linked_list.insert_head(input('Inserting 1st at head ' ).strip() ) linked_list.insert_head(input('Inserting 2nd at head ' ).strip() ) print('\nPrint list:' ) linked_list.print_list() linked_list.insert_tail(input('\nInserting 1st at tail ' ).strip() ) linked_list.insert_tail(input('Inserting 2nd at tail ' ).strip() ) print('\nPrint list:' ) linked_list.print_list() print('\nDelete head' ) linked_list.delete_head() print('Delete tail' ) linked_list.delete_tail() print('\nPrint list:' ) linked_list.print_list() print('\nReverse linked list' ) linked_list.reverse() print('\nPrint list:' ) linked_list.print_list() print('\nString representation of linked list:' ) print(lowerCAmelCase_ ) print('\nReading/changing Node data using indexing:' ) print(f"""Element at Position 1: {linked_list[1]}""" ) _a : Optional[Any] = input('Enter New Value: ' ).strip() print('New list:' ) print(lowerCAmelCase_ ) print(f"""length of linked_list is : {len(lowerCAmelCase_ )}""" ) if __name__ == "__main__": main()
89
1
"""simple docstring""" import sys from .dependency_versions_table import deps from .utils.versions import require_version, require_version_core # define which module versions we always want to check at run time # (usually the ones defined in `install_requires` in setup.py) # # order specific notes: # - tqdm must be checked before tokenizers a_ = """python tqdm regex requests packaging filelock numpy tokenizers""".split() if sys.version_info < (3, 7): pkgs_to_check_at_runtime.append("""dataclasses""") if sys.version_info < (3, 8): pkgs_to_check_at_runtime.append("""importlib_metadata""") for pkg in pkgs_to_check_at_runtime: if pkg in deps: if pkg == "tokenizers": # must be loaded here, or else tqdm check may fail from .utils import is_tokenizers_available if not is_tokenizers_available(): continue # not required, check version only if installed require_version_core(deps[pkg]) else: raise ValueError(f'''can\'t find {pkg} in {deps.keys()}, check dependency_versions_table.py''') def __lowercase ( snake_case_ : List[str] ,snake_case_ : Tuple=None ) ->Union[str, Any]: '''simple docstring''' require_version(deps[pkg] ,snake_case_ )
291
"""simple docstring""" a_ = { """meter""": """m""", """kilometer""": """km""", """megametre""": """Mm""", """gigametre""": """Gm""", """terametre""": """Tm""", """petametre""": """Pm""", """exametre""": """Em""", """zettametre""": """Zm""", """yottametre""": """Ym""", } # Exponent of the factor(meter) a_ = { """m""": 0, """km""": 3, """Mm""": 6, """Gm""": 9, """Tm""": 12, """Pm""": 15, """Em""": 18, """Zm""": 21, """Ym""": 24, } def __lowercase ( snake_case_ : float ,snake_case_ : str ,snake_case_ : str ) ->float: '''simple docstring''' __A : Tuple = from_type.lower().strip('''s''' ) __A : Optional[int] = to_type.lower().strip('''s''' ) __A : List[str] = UNIT_SYMBOL.get(snake_case_ ,snake_case_ ) __A : Any = UNIT_SYMBOL.get(snake_case_ ,snake_case_ ) if from_sanitized not in METRIC_CONVERSION: __A : int = ( F"""Invalid 'from_type' value: {from_type!r}.\n""" F"""Conversion abbreviations are: {', '.join(snake_case_ )}""" ) raise ValueError(snake_case_ ) if to_sanitized not in METRIC_CONVERSION: __A : str = ( F"""Invalid 'to_type' value: {to_type!r}.\n""" F"""Conversion abbreviations are: {', '.join(snake_case_ )}""" ) raise ValueError(snake_case_ ) __A : Optional[Any] = METRIC_CONVERSION[from_sanitized] __A : Optional[int] = METRIC_CONVERSION[to_sanitized] __A : Union[str, Any] = 1 if from_exponent > to_exponent: __A : Dict = from_exponent - to_exponent else: __A : Union[str, Any] = -(to_exponent - from_exponent) return value * pow(10 ,snake_case_ ) if __name__ == "__main__": from doctest import testmod testmod()
291
1
import os import tempfile import unittest from transformers import FlaubertConfig, is_torch_available from transformers.testing_utils import require_torch, require_torch_gpu, 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 ( FlaubertForMultipleChoice, FlaubertForQuestionAnswering, FlaubertForQuestionAnsweringSimple, FlaubertForSequenceClassification, FlaubertForTokenClassification, FlaubertModel, FlaubertWithLMHeadModel, ) from transformers.models.flaubert.modeling_flaubert import FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST class __magic_name__ ( lowerCamelCase__ ): '''simple docstring''' def __init__( self, lowercase_, lowercase_=13, lowercase_=7, lowercase_=True, lowercase_=True, lowercase_=True, lowercase_=True, lowercase_=True, lowercase_=False, lowercase_=False, lowercase_=False, lowercase_=2, lowercase_=99, lowercase_=0, lowercase_=32, lowercase_=5, lowercase_=4, lowercase_=0.1, lowercase_=0.1, lowercase_=512, lowercase_=12, lowercase_=2, lowercase_=0.02, lowercase_=3, lowercase_=4, lowercase_="last", lowercase_=None, lowercase_=None, ) -> List[Any]: """simple docstring""" a__ =parent a__ =batch_size a__ =seq_length a__ =is_training a__ =use_input_lengths a__ =use_token_type_ids a__ =use_labels a__ =gelu_activation a__ =sinusoidal_embeddings a__ =causal a__ =asm a__ =n_langs a__ =vocab_size a__ =n_special a__ =hidden_size a__ =num_hidden_layers a__ =num_attention_heads 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__ =summary_type a__ =use_proj a__ =scope def _UpperCAmelCase ( self ) -> Any: """simple docstring""" a__ =ids_tensor([self.batch_size, self.seq_length], self.vocab_size ) a__ =random_attention_mask([self.batch_size, self.seq_length] ) a__ =None if self.use_input_lengths: a__ =( ids_tensor([self.batch_size], vocab_size=2 ) + self.seq_length - 2 ) # small variation of seq_length a__ =None if self.use_token_type_ids: a__ =ids_tensor([self.batch_size, self.seq_length], self.n_langs ) 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], 2 ).float() a__ =ids_tensor([self.batch_size], self.num_choices ) a__ =self.get_config() return ( config, input_ids, token_type_ids, input_lengths, sequence_labels, token_labels, is_impossible_labels, choice_labels, input_mask, ) def _UpperCAmelCase ( self ) -> Any: """simple docstring""" return FlaubertConfig( vocab_size=self.vocab_size, n_special=self.n_special, emb_dim=self.hidden_size, n_layers=self.num_hidden_layers, n_heads=self.num_attention_heads, dropout=self.hidden_dropout_prob, attention_dropout=self.attention_probs_dropout_prob, gelu_activation=self.gelu_activation, sinusoidal_embeddings=self.sinusoidal_embeddings, asm=self.asm, causal=self.causal, n_langs=self.n_langs, max_position_embeddings=self.max_position_embeddings, initializer_range=self.initializer_range, summary_type=self.summary_type, use_proj=self.use_proj, ) def _UpperCAmelCase ( self, lowercase_, lowercase_, lowercase_, lowercase_, lowercase_, lowercase_, lowercase_, lowercase_, lowercase_, ) -> Dict: """simple docstring""" a__ =FlaubertModel(config=lowercase_ ) model.to(lowercase_ ) model.eval() a__ =model(lowercase_, lengths=lowercase_, langs=lowercase_ ) a__ =model(lowercase_, langs=lowercase_ ) a__ =model(lowercase_ ) self.parent.assertEqual(result.last_hidden_state.shape, (self.batch_size, self.seq_length, self.hidden_size) ) def _UpperCAmelCase ( self, lowercase_, lowercase_, lowercase_, lowercase_, lowercase_, lowercase_, lowercase_, lowercase_, lowercase_, ) -> str: """simple docstring""" a__ =FlaubertWithLMHeadModel(lowercase_ ) model.to(lowercase_ ) model.eval() a__ =model(lowercase_, token_type_ids=lowercase_, labels=lowercase_ ) self.parent.assertEqual(result.loss.shape, () ) self.parent.assertEqual(result.logits.shape, (self.batch_size, self.seq_length, self.vocab_size) ) def _UpperCAmelCase ( self, lowercase_, lowercase_, lowercase_, lowercase_, lowercase_, lowercase_, lowercase_, lowercase_, lowercase_, ) -> Dict: """simple docstring""" a__ =FlaubertForQuestionAnsweringSimple(lowercase_ ) model.to(lowercase_ ) model.eval() a__ =model(lowercase_ ) a__ =model(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 _UpperCAmelCase ( self, lowercase_, lowercase_, lowercase_, lowercase_, lowercase_, lowercase_, lowercase_, lowercase_, lowercase_, ) -> Optional[Any]: """simple docstring""" a__ =FlaubertForQuestionAnswering(lowercase_ ) model.to(lowercase_ ) model.eval() a__ =model(lowercase_ ) a__ =model( lowercase_, start_positions=lowercase_, end_positions=lowercase_, cls_index=lowercase_, is_impossible=lowercase_, p_mask=lowercase_, ) a__ =model( lowercase_, start_positions=lowercase_, end_positions=lowercase_, cls_index=lowercase_, is_impossible=lowercase_, ) ((a__), ) =result_with_labels.to_tuple() a__ =model(lowercase_, start_positions=lowercase_, end_positions=lowercase_ ) ((a__), ) =result_with_labels.to_tuple() self.parent.assertEqual(result_with_labels.loss.shape, () ) self.parent.assertEqual(result.start_top_log_probs.shape, (self.batch_size, model.config.start_n_top) ) self.parent.assertEqual(result.start_top_index.shape, (self.batch_size, model.config.start_n_top) ) self.parent.assertEqual( result.end_top_log_probs.shape, (self.batch_size, model.config.start_n_top * model.config.end_n_top) ) self.parent.assertEqual( result.end_top_index.shape, (self.batch_size, model.config.start_n_top * model.config.end_n_top) ) self.parent.assertEqual(result.cls_logits.shape, (self.batch_size,) ) def _UpperCAmelCase ( self, lowercase_, lowercase_, lowercase_, lowercase_, lowercase_, lowercase_, lowercase_, lowercase_, lowercase_, ) -> Optional[Any]: """simple docstring""" a__ =FlaubertForSequenceClassification(lowercase_ ) model.to(lowercase_ ) model.eval() a__ =model(lowercase_ ) a__ =model(lowercase_, labels=lowercase_ ) self.parent.assertEqual(result.loss.shape, () ) self.parent.assertEqual(result.logits.shape, (self.batch_size, self.type_sequence_label_size) ) def _UpperCAmelCase ( self, lowercase_, lowercase_, lowercase_, lowercase_, lowercase_, lowercase_, lowercase_, lowercase_, lowercase_, ) -> Optional[int]: """simple docstring""" a__ =self.num_labels a__ =FlaubertForTokenClassification(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 _UpperCAmelCase ( self, lowercase_, lowercase_, lowercase_, lowercase_, lowercase_, lowercase_, lowercase_, lowercase_, lowercase_, ) -> Dict: """simple docstring""" a__ =self.num_choices a__ =FlaubertForMultipleChoice(config=lowercase_ ) model.to(lowercase_ ) 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( lowercase_, attention_mask=lowercase_, token_type_ids=lowercase_, labels=lowercase_, ) self.parent.assertEqual(result.logits.shape, (self.batch_size, self.num_choices) ) def _UpperCAmelCase ( self ) -> Dict: """simple docstring""" a__ =self.prepare_config_and_inputs() ( ( a__ ), ( a__ ), ( a__ ), ( a__ ), ( a__ ), ( a__ ), ( a__ ), ( a__ ), ( a__ ), ) =config_and_inputs a__ ={ '''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''lengths''': input_lengths, '''attention_mask''': input_mask, } return config, inputs_dict @require_torch class __magic_name__ ( lowerCamelCase__ , lowerCamelCase__ , unittest.TestCase ): '''simple docstring''' lowerCamelCase__ : str = ( ( FlaubertModel, FlaubertWithLMHeadModel, FlaubertForQuestionAnswering, FlaubertForQuestionAnsweringSimple, FlaubertForSequenceClassification, FlaubertForTokenClassification, FlaubertForMultipleChoice, ) if is_torch_available() else () ) lowerCamelCase__ : Dict = ( { 'feature-extraction': FlaubertModel, 'fill-mask': FlaubertWithLMHeadModel, 'question-answering': FlaubertForQuestionAnsweringSimple, 'text-classification': FlaubertForSequenceClassification, 'token-classification': FlaubertForTokenClassification, 'zero-shot': FlaubertForSequenceClassification, } if is_torch_available() else {} ) def _UpperCAmelCase ( self, lowercase_, lowercase_, lowercase_, lowercase_, lowercase_ ) -> str: """simple docstring""" if ( pipeline_test_casse_name == "QAPipelineTests" and tokenizer_name is not None and not tokenizer_name.endswith('''Fast''' ) ): # `QAPipelineTests` fails for a few models when the slower tokenizer are used. # (The slower tokenizers were never used for pipeline tests before the pipeline testing rework) # TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer return True return False def _UpperCAmelCase ( self, lowercase_, lowercase_, lowercase_=False ) -> str: """simple docstring""" a__ =super()._prepare_for_class(lowercase_, lowercase_, return_labels=lowercase_ ) if return_labels: if model_class.__name__ == "FlaubertForQuestionAnswering": a__ =torch.zeros( self.model_tester.batch_size, dtype=torch.long, device=lowercase_ ) a__ =torch.zeros( self.model_tester.batch_size, dtype=torch.long, device=lowercase_ ) return inputs_dict def _UpperCAmelCase ( self ) -> Optional[int]: """simple docstring""" a__ =FlaubertModelTester(self ) a__ =ConfigTester(self, config_class=lowercase_, emb_dim=37 ) def _UpperCAmelCase ( self ) -> Optional[Any]: """simple docstring""" self.config_tester.run_common_tests() def _UpperCAmelCase ( self ) -> Any: """simple docstring""" a__ =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_model(*lowercase_ ) def _UpperCAmelCase ( self ) -> str: """simple docstring""" a__ =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_lm_head(*lowercase_ ) def _UpperCAmelCase ( self ) -> Dict: """simple docstring""" a__ =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_simple_qa(*lowercase_ ) def _UpperCAmelCase ( self ) -> Dict: """simple docstring""" a__ =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_qa(*lowercase_ ) def _UpperCAmelCase ( self ) -> Any: """simple docstring""" a__ =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_sequence_classif(*lowercase_ ) def _UpperCAmelCase ( self ) -> Any: """simple docstring""" a__ =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_token_classif(*lowercase_ ) def _UpperCAmelCase ( self ) -> Tuple: """simple docstring""" a__ =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_multiple_choice(*lowercase_ ) @slow def _UpperCAmelCase ( self ) -> Tuple: """simple docstring""" for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: a__ =FlaubertModel.from_pretrained(lowercase_ ) self.assertIsNotNone(lowercase_ ) @slow @require_torch_gpu def _UpperCAmelCase ( self ) -> int: """simple docstring""" a__, a__ =self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: # FlauBertForMultipleChoice behaves incorrectly in JIT environments. if model_class == FlaubertForMultipleChoice: return a__ =True a__ =model_class(config=lowercase_ ) a__ =self._prepare_for_class(lowercase_, lowercase_ ) a__ =torch.jit.trace( lowercase_, (inputs_dict['''input_ids'''].to('''cpu''' ), inputs_dict['''attention_mask'''].to('''cpu''' )) ) with tempfile.TemporaryDirectory() as tmp: torch.jit.save(lowercase_, os.path.join(lowercase_, '''traced_model.pt''' ) ) a__ =torch.jit.load(os.path.join(lowercase_, '''traced_model.pt''' ), map_location=lowercase_ ) loaded(inputs_dict['''input_ids'''].to(lowercase_ ), inputs_dict['''attention_mask'''].to(lowercase_ ) ) @require_torch class __magic_name__ ( unittest.TestCase ): '''simple docstring''' @slow def _UpperCAmelCase ( self ) -> List[str]: """simple docstring""" a__ =FlaubertModel.from_pretrained('''flaubert/flaubert_base_cased''' ) a__ =torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]] ) with torch.no_grad(): a__ =model(lowercase_ )[0] a__ =torch.Size((1, 11, 768) ) self.assertEqual(output.shape, lowercase_ ) a__ =torch.tensor( [[[-2.6251, -1.4298, -0.0227], [-2.8510, -1.6387, 0.2258], [-2.8114, -1.1832, -0.3066]]] ) self.assertTrue(torch.allclose(output[:, :3, :3], lowercase_, atol=1E-4 ) )
188
import argparse import hashlib # hashlib is only used inside the Test class import struct class __magic_name__ : '''simple docstring''' def __init__( self, lowercase_ ) -> List[str]: """simple docstring""" a__ =data a__ =[0X67452301, 0Xefcdab89, 0X98badcfe, 0X10325476, 0Xc3d2e1f0] @staticmethod def _UpperCAmelCase ( lowercase_, lowercase_ ) -> Union[str, Any]: """simple docstring""" return ((n << b) | (n >> (32 - b))) & 0Xffffffff def _UpperCAmelCase ( self ) -> Optional[int]: """simple docstring""" a__ =b'''\x80''' + b'''\x00''' * (63 - (len(self.data ) + 8) % 64) a__ =self.data + padding + struct.pack('''>Q''', 8 * len(self.data ) ) return padded_data def _UpperCAmelCase ( self ) -> Any: """simple docstring""" return [ self.padded_data[i : i + 64] for i in range(0, len(self.padded_data ), 64 ) ] def _UpperCAmelCase ( self, lowercase_ ) -> List[Any]: """simple docstring""" a__ =list(struct.unpack('''>16L''', lowercase_ ) ) + [0] * 64 for i in range(16, 80 ): a__ =self.rotate((w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16]), 1 ) return w def _UpperCAmelCase ( self ) -> Any: """simple docstring""" a__ =self.padding() a__ =self.split_blocks() for block in self.blocks: a__ =self.expand_block(lowercase_ ) a__, a__, a__, a__, a__ =self.h for i in range(0, 80 ): if 0 <= i < 20: a__ =(b & c) | ((~b) & d) a__ =0X5a827999 elif 20 <= i < 40: a__ =b ^ c ^ d a__ =0X6ed9eba1 elif 40 <= i < 60: a__ =(b & c) | (b & d) | (c & d) a__ =0X8f1bbcdc elif 60 <= i < 80: a__ =b ^ c ^ d a__ =0Xca62c1d6 a__, a__, a__, a__, a__ =( self.rotate(lowercase_, 5 ) + f + e + k + expanded_block[i] & 0Xffffffff, a, self.rotate(lowercase_, 30 ), c, d, ) a__ =( self.h[0] + a & 0Xffffffff, self.h[1] + b & 0Xffffffff, self.h[2] + c & 0Xffffffff, self.h[3] + d & 0Xffffffff, self.h[4] + e & 0Xffffffff, ) return ("{:08x}" * 5).format(*self.h ) def UpperCAmelCase__ ( ): '''simple docstring''' a__ =b'''Test String''' assert SHAaHash(_A ).final_hash() == hashlib.shaa(_A ).hexdigest() # noqa: S324 def UpperCAmelCase__ ( ): '''simple docstring''' a__ =argparse.ArgumentParser(description='''Process some strings or files''' ) parser.add_argument( '''--string''' , dest='''input_string''' , default='''Hello World!! Welcome to Cryptography''' , help='''Hash the string''' , ) parser.add_argument('''--file''' , dest='''input_file''' , help='''Hash contents of a file''' ) a__ =parser.parse_args() a__ =args.input_string # In any case hash input should be a bytestring if args.input_file: with open(args.input_file , '''rb''' ) as f: a__ =f.read() else: a__ =bytes(_A , '''utf-8''' ) print(SHAaHash(_A ).final_hash() ) if __name__ == "__main__": main() import doctest doctest.testmod()
188
1
'''simple docstring''' import colorsys from PIL import Image # type: ignore def _UpperCamelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ): UpperCAmelCase__ : Optional[Any] = x UpperCAmelCase__ : Optional[int] = y for step in range(UpperCamelCase__ ): # noqa: B007 UpperCAmelCase__ : List[str] = a * a - b * b + x UpperCAmelCase__ : Optional[int] = 2 * a * b + y UpperCAmelCase__ : int = a_new # divergence happens for all complex number with an absolute value # greater than 4 if a * a + b * b > 4: break return step / (max_step - 1) def _UpperCamelCase ( UpperCamelCase__ ): if distance == 1: return (0, 0, 0) else: return (2_5_5, 2_5_5, 2_5_5) def _UpperCamelCase ( UpperCamelCase__ ): if distance == 1: return (0, 0, 0) else: return tuple(round(i * 2_5_5 ) for i in colorsys.hsv_to_rgb(UpperCamelCase__ , 1 , 1 ) ) def _UpperCamelCase ( UpperCamelCase__ = 8_0_0 , UpperCamelCase__ = 6_0_0 , UpperCamelCase__ = -0.6 , UpperCamelCase__ = 0 , UpperCamelCase__ = 3.2 , UpperCamelCase__ = 5_0 , UpperCamelCase__ = True , ): UpperCAmelCase__ : str = Image.new("""RGB""" , (image_width, image_height) ) UpperCAmelCase__ : Optional[int] = img.load() # loop through the image-coordinates for image_x in range(UpperCamelCase__ ): for image_y in range(UpperCamelCase__ ): # determine the figure-coordinates based on the image-coordinates UpperCAmelCase__ : Union[str, Any] = figure_width / image_width * image_height UpperCAmelCase__ : Union[str, Any] = figure_center_x + (image_x / image_width - 0.5) * figure_width UpperCAmelCase__ : Tuple = figure_center_y + (image_y / image_height - 0.5) * figure_height UpperCAmelCase__ : List[str] = get_distance(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) # color the corresponding pixel based on the selected coloring-function if use_distance_color_coding: UpperCAmelCase__ : Any = get_color_coded_rgb(UpperCamelCase__ ) else: UpperCAmelCase__ : Tuple = get_black_and_white_rgb(UpperCamelCase__ ) return img if __name__ == "__main__": import doctest doctest.testmod() # colored version, full figure __A =get_image() # uncomment for colored version, different section, zoomed in # img = get_image(figure_center_x = -0.6, figure_center_y = -0.4, # figure_width = 0.8) # uncomment for black and white version, full figure # img = get_image(use_distance_color_coding = False) # uncomment to save the image # img.save("mandelbrot.png") img.show()
283
'''simple docstring''' import os from datetime import datetime as dt from github import Github __A =[ 'good first issue', 'good second issue', 'good difficult issue', 'enhancement', 'new pipeline/model', 'new scheduler', 'wip', ] def _UpperCamelCase ( ): UpperCAmelCase__ : Union[str, Any] = Github(os.environ["""GITHUB_TOKEN"""] ) UpperCAmelCase__ : Any = g.get_repo("""huggingface/diffusers""" ) UpperCAmelCase__ : Optional[int] = repo.get_issues(state="""open""" ) for issue in open_issues: UpperCAmelCase__ : Any = sorted(issue.get_comments() , key=lambda UpperCamelCase__ : i.created_at , reverse=UpperCamelCase__ ) UpperCAmelCase__ : List[Any] = comments[0] if len(UpperCamelCase__ ) > 0 else None if ( last_comment is not None and last_comment.user.login == "github-actions[bot]" and (dt.utcnow() - issue.updated_at).days > 7 and (dt.utcnow() - issue.created_at).days >= 3_0 and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels() ) ): # Closes the issue after 7 days of inactivity since the Stalebot notification. issue.edit(state="""closed""" ) elif ( "stale" in issue.get_labels() and last_comment is not None and last_comment.user.login != "github-actions[bot]" ): # Opens the issue if someone other than Stalebot commented. issue.edit(state="""open""" ) issue.remove_from_labels("""stale""" ) elif ( (dt.utcnow() - issue.updated_at).days > 2_3 and (dt.utcnow() - issue.created_at).days >= 3_0 and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels() ) ): # Post a Stalebot notification after 23 days of inactivity. issue.create_comment( """This issue has been automatically marked as stale because it has not had """ """recent activity. If you think this still needs to be addressed """ """please comment on this thread.\n\nPlease note that issues that do not follow the """ """[contributing guidelines](https://github.com/huggingface/diffusers/blob/main/CONTRIBUTING.md) """ """are likely to be ignored.""" ) issue.add_to_labels("""stale""" ) if __name__ == "__main__": main()
283
1
'''simple docstring''' import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_base import BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import PaddingStrategy, logging from .tokenization_realm import RealmTokenizer lowerCamelCase : str = logging.get_logger(__name__) lowerCamelCase : int = {"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"} lowerCamelCase : Optional[int] = { "vocab_file": { "google/realm-cc-news-pretrained-embedder": ( "https://huggingface.co/google/realm-cc-news-pretrained-embedder/resolve/main/vocab.txt" ), "google/realm-cc-news-pretrained-encoder": ( "https://huggingface.co/google/realm-cc-news-pretrained-encoder/resolve/main/vocab.txt" ), "google/realm-cc-news-pretrained-scorer": ( "https://huggingface.co/google/realm-cc-news-pretrained-scorer/resolve/main/vocab.txt" ), "google/realm-cc-news-pretrained-openqa": ( "https://huggingface.co/google/realm-cc-news-pretrained-openqa/aresolve/main/vocab.txt" ), "google/realm-orqa-nq-openqa": "https://huggingface.co/google/realm-orqa-nq-openqa/resolve/main/vocab.txt", "google/realm-orqa-nq-reader": "https://huggingface.co/google/realm-orqa-nq-reader/resolve/main/vocab.txt", "google/realm-orqa-wq-openqa": "https://huggingface.co/google/realm-orqa-wq-openqa/resolve/main/vocab.txt", "google/realm-orqa-wq-reader": "https://huggingface.co/google/realm-orqa-wq-reader/resolve/main/vocab.txt", }, "tokenizer_file": { "google/realm-cc-news-pretrained-embedder": ( "https://huggingface.co/google/realm-cc-news-pretrained-embedder/resolve/main/tokenizer.jsont" ), "google/realm-cc-news-pretrained-encoder": ( "https://huggingface.co/google/realm-cc-news-pretrained-encoder/resolve/main/tokenizer.json" ), "google/realm-cc-news-pretrained-scorer": ( "https://huggingface.co/google/realm-cc-news-pretrained-scorer/resolve/main/tokenizer.json" ), "google/realm-cc-news-pretrained-openqa": ( "https://huggingface.co/google/realm-cc-news-pretrained-openqa/aresolve/main/tokenizer.json" ), "google/realm-orqa-nq-openqa": ( "https://huggingface.co/google/realm-orqa-nq-openqa/resolve/main/tokenizer.json" ), "google/realm-orqa-nq-reader": ( "https://huggingface.co/google/realm-orqa-nq-reader/resolve/main/tokenizer.json" ), "google/realm-orqa-wq-openqa": ( "https://huggingface.co/google/realm-orqa-wq-openqa/resolve/main/tokenizer.json" ), "google/realm-orqa-wq-reader": ( "https://huggingface.co/google/realm-orqa-wq-reader/resolve/main/tokenizer.json" ), }, } lowerCamelCase : Dict = { "google/realm-cc-news-pretrained-embedder": 5_1_2, "google/realm-cc-news-pretrained-encoder": 5_1_2, "google/realm-cc-news-pretrained-scorer": 5_1_2, "google/realm-cc-news-pretrained-openqa": 5_1_2, "google/realm-orqa-nq-openqa": 5_1_2, "google/realm-orqa-nq-reader": 5_1_2, "google/realm-orqa-wq-openqa": 5_1_2, "google/realm-orqa-wq-reader": 5_1_2, } lowerCamelCase : int = { "google/realm-cc-news-pretrained-embedder": {"do_lower_case": True}, "google/realm-cc-news-pretrained-encoder": {"do_lower_case": True}, "google/realm-cc-news-pretrained-scorer": {"do_lower_case": True}, "google/realm-cc-news-pretrained-openqa": {"do_lower_case": True}, "google/realm-orqa-nq-openqa": {"do_lower_case": True}, "google/realm-orqa-nq-reader": {"do_lower_case": True}, "google/realm-orqa-wq-openqa": {"do_lower_case": True}, "google/realm-orqa-wq-reader": {"do_lower_case": True}, } class A__ ( A__ ): A__ = VOCAB_FILES_NAMES A__ = PRETRAINED_VOCAB_FILES_MAP A__ = PRETRAINED_INIT_CONFIGURATION A__ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES A__ = RealmTokenizer def __init__( self : Tuple , _a : Union[str, Any]=None , _a : List[Any]=None , _a : str=True , _a : Dict="[UNK]" , _a : List[Any]="[SEP]" , _a : List[str]="[PAD]" , _a : int="[CLS]" , _a : List[Any]="[MASK]" , _a : List[Any]=True , _a : str=None , **_a : Any , ) -> str: '''simple docstring''' super().__init__( _a , tokenizer_file=_a , do_lower_case=_a , unk_token=_a , sep_token=_a , pad_token=_a , cls_token=_a , mask_token=_a , tokenize_chinese_chars=_a , strip_accents=_a , **_a , ) _SCREAMING_SNAKE_CASE =json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get('lowercase' , _a ) != do_lower_case or normalizer_state.get('strip_accents' , _a ) != strip_accents or normalizer_state.get('handle_chinese_chars' , _a ) != tokenize_chinese_chars ): _SCREAMING_SNAKE_CASE =getattr(_a , normalizer_state.pop('type' ) ) _SCREAMING_SNAKE_CASE =do_lower_case _SCREAMING_SNAKE_CASE =strip_accents _SCREAMING_SNAKE_CASE =tokenize_chinese_chars _SCREAMING_SNAKE_CASE =normalizer_class(**_a ) _SCREAMING_SNAKE_CASE =do_lower_case def A ( self : int , _a : Union[str, Any] , **_a : List[Any] ) -> Any: '''simple docstring''' _SCREAMING_SNAKE_CASE =PaddingStrategy.MAX_LENGTH _SCREAMING_SNAKE_CASE =text _SCREAMING_SNAKE_CASE =kwargs.pop('text_pair' , _a ) _SCREAMING_SNAKE_CASE =kwargs.pop('return_tensors' , _a ) _SCREAMING_SNAKE_CASE ={ 'input_ids': [], 'attention_mask': [], 'token_type_ids': [], } for idx, candidate_text in enumerate(_a ): if batch_text_pair is not None: _SCREAMING_SNAKE_CASE =batch_text_pair[idx] else: _SCREAMING_SNAKE_CASE =None _SCREAMING_SNAKE_CASE =super().__call__(_a , _a , return_tensors=_a , **_a ) _SCREAMING_SNAKE_CASE =encoded_candidates.get('input_ids' ) _SCREAMING_SNAKE_CASE =encoded_candidates.get('attention_mask' ) _SCREAMING_SNAKE_CASE =encoded_candidates.get('token_type_ids' ) if encoded_input_ids is not None: output_data["input_ids"].append(_a ) if encoded_attention_mask is not None: output_data["attention_mask"].append(_a ) if encoded_token_type_ids is not None: output_data["token_type_ids"].append(_a ) _SCREAMING_SNAKE_CASE ={key: item for key, item in output_data.items() if len(_a ) != 0} return BatchEncoding(_a , tensor_type=_a ) def A ( self : int , _a : Optional[Any] , _a : Optional[int]=None ) -> Optional[int]: '''simple docstring''' _SCREAMING_SNAKE_CASE =[self.cls_token_id] + token_ids_a + [self.sep_token_id] if token_ids_a: output += token_ids_a + [self.sep_token_id] return output def A ( self : str , _a : List[int] , _a : Optional[List[int]] = None ) -> List[int]: '''simple docstring''' _SCREAMING_SNAKE_CASE =[self.sep_token_id] _SCREAMING_SNAKE_CASE =[self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def A ( self : Dict , _a : str , _a : Optional[str] = None ) -> Tuple[str]: '''simple docstring''' _SCREAMING_SNAKE_CASE =self._tokenizer.model.save(_a , name=_a ) return tuple(_a )
47
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available lowerCamelCase : int = {"configuration_glpn": ["GLPN_PRETRAINED_CONFIG_ARCHIVE_MAP", "GLPNConfig"]} try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase : int = ["GLPNFeatureExtractor"] lowerCamelCase : Optional[int] = ["GLPNImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase : Union[str, Any] = [ "GLPN_PRETRAINED_MODEL_ARCHIVE_LIST", "GLPNForDepthEstimation", "GLPNLayer", "GLPNModel", "GLPNPreTrainedModel", ] if TYPE_CHECKING: from .configuration_glpn import GLPN_PRETRAINED_CONFIG_ARCHIVE_MAP, GLPNConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_glpn import GLPNFeatureExtractor from .image_processing_glpn import GLPNImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_glpn import ( GLPN_PRETRAINED_MODEL_ARCHIVE_LIST, GLPNForDepthEstimation, GLPNLayer, GLPNModel, GLPNPreTrainedModel, ) else: import sys lowerCamelCase : List[Any] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
47
1
from __future__ import annotations import unittest from transformers import AutoTokenizer, MBartConfig, is_tf_available from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFAutoModelForSeqaSeqLM, TFMBartForConditionalGeneration, TFMBartModel @require_tf class __lowerCAmelCase : _a = MBartConfig _a = {} _a = '''gelu''' def __init__( self: Dict , _lowerCAmelCase: Union[str, Any] , _lowerCAmelCase: List[str]=13 , _lowerCAmelCase: List[str]=7 , _lowerCAmelCase: List[str]=True , _lowerCAmelCase: List[Any]=False , _lowerCAmelCase: Any=99 , _lowerCAmelCase: int=32 , _lowerCAmelCase: Any=2 , _lowerCAmelCase: List[Any]=4 , _lowerCAmelCase: Optional[Any]=37 , _lowerCAmelCase: Tuple=0.1 , _lowerCAmelCase: str=0.1 , _lowerCAmelCase: int=20 , _lowerCAmelCase: Tuple=2 , _lowerCAmelCase: Tuple=1 , _lowerCAmelCase: Union[str, Any]=0 , ): lowercase :Any = parent lowercase :int = batch_size lowercase :Optional[int] = seq_length lowercase :int = is_training lowercase :Dict = use_labels lowercase :Optional[Any] = vocab_size lowercase :Union[str, Any] = hidden_size lowercase :Any = num_hidden_layers lowercase :Dict = num_attention_heads lowercase :List[Any] = intermediate_size lowercase :Dict = hidden_dropout_prob lowercase :Optional[Any] = attention_probs_dropout_prob lowercase :Union[str, Any] = max_position_embeddings lowercase :Optional[int] = eos_token_id lowercase :List[str] = pad_token_id lowercase :str = bos_token_id def SCREAMING_SNAKE_CASE ( self: Dict ): lowercase :List[Any] = ids_tensor([self.batch_size, self.seq_length - 1] , self.vocab_size ) lowercase :int = tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size ) , 1 ) lowercase :str = tf.concat([input_ids, eos_tensor] , axis=1 ) lowercase :Optional[Any] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) lowercase :Optional[Any] = self.config_cls( 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_ids=[2] , bos_token_id=self.bos_token_id , pad_token_id=self.pad_token_id , decoder_start_token_id=self.pad_token_id , **self.config_updates , ) lowercase :int = prepare_mbart_inputs_dict(_a , _a , _a ) return config, inputs_dict def SCREAMING_SNAKE_CASE ( self: List[Any] , _lowerCAmelCase: Optional[Any] , _lowerCAmelCase: Optional[Any] ): lowercase :Any = TFMBartModel(config=_a ).get_decoder() lowercase :Tuple = inputs_dict['input_ids'] lowercase :Optional[int] = input_ids[:1, :] lowercase :int = inputs_dict['attention_mask'][:1, :] lowercase :List[str] = inputs_dict['head_mask'] lowercase :List[Any] = 1 # first forward pass lowercase :List[Any] = model(_a , attention_mask=_a , head_mask=_a , use_cache=_a ) lowercase :List[str] = outputs.to_tuple() lowercase :Union[str, Any] = past_key_values[1] def UpperCAmelCase__ ( lowerCamelCase, lowerCamelCase, lowerCamelCase, lowerCamelCase=None, lowerCamelCase=None, lowerCamelCase=None, lowerCamelCase=None, lowerCamelCase=None, ): if attention_mask is None: lowercase :Union[str, Any] = tf.cast(tf.math.not_equal(__a, config.pad_token_id ), tf.inta ) if decoder_attention_mask is None: lowercase :Tuple = tf.concat( [ tf.ones(decoder_input_ids[:, :1].shape, dtype=tf.inta ), tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:], config.pad_token_id ), tf.inta ), ], axis=-1, ) if head_mask is None: lowercase :str = tf.ones((config.encoder_layers, config.encoder_attention_heads) ) if decoder_head_mask is None: lowercase :str = tf.ones((config.decoder_layers, config.decoder_attention_heads) ) if cross_attn_head_mask is None: lowercase :Optional[Any] = tf.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": decoder_attention_mask, "head_mask": head_mask, "decoder_head_mask": decoder_head_mask, "cross_attn_head_mask": cross_attn_head_mask, } @require_tf class __lowerCAmelCase ( lowercase__ , lowercase__ , unittest.TestCase): _a = (TFMBartForConditionalGeneration, TFMBartModel) if is_tf_available() else () _a = (TFMBartForConditionalGeneration,) if is_tf_available() else () _a = ( { '''conversational''': TFMBartForConditionalGeneration, '''feature-extraction''': TFMBartModel, '''summarization''': TFMBartForConditionalGeneration, '''text2text-generation''': TFMBartForConditionalGeneration, '''translation''': TFMBartForConditionalGeneration, } if is_tf_available() else {} ) _a = True _a = False _a = False def SCREAMING_SNAKE_CASE ( self: int , _lowerCAmelCase: List[str] , _lowerCAmelCase: List[str] , _lowerCAmelCase: Optional[int] , _lowerCAmelCase: Any , _lowerCAmelCase: Union[str, Any] ): if pipeline_test_casse_name != "FeatureExtractionPipelineTests": # Exception encountered when calling layer '...' return True return False def SCREAMING_SNAKE_CASE ( self: Union[str, Any] ): lowercase :Tuple = TFMBartModelTester(self ) lowercase :List[Any] = ConfigTester(self , config_class=_a ) def SCREAMING_SNAKE_CASE ( self: Tuple ): self.config_tester.run_common_tests() def SCREAMING_SNAKE_CASE ( self: Union[str, Any] ): lowercase :str = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_decoder_model_past_large_inputs(*_a ) @require_sentencepiece @require_tokenizers @require_tf class __lowerCAmelCase ( unittest.TestCase): _a = [ ''' UN Chief Says There Is No Military Solution in Syria''', ] _a = [ '''Şeful ONU declară că nu există o soluţie militară în Siria''', ] _a = '''facebook/mbart-large-en-ro''' @cached_property def SCREAMING_SNAKE_CASE ( self: List[str] ): return AutoTokenizer.from_pretrained(self.model_name ) @cached_property def SCREAMING_SNAKE_CASE ( self: Tuple ): lowercase :List[Any] = TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name ) return model def SCREAMING_SNAKE_CASE ( self: List[Any] , **_lowerCAmelCase: Tuple ): lowercase :int = self.translate_src_text(**_a ) self.assertListEqual(self.expected_text , _a ) def SCREAMING_SNAKE_CASE ( self: Optional[int] , **_lowerCAmelCase: Any ): lowercase :Dict = self.tokenizer(self.src_text , **_a , return_tensors="tf" ) lowercase :Dict = self.model.generate( model_inputs.input_ids , attention_mask=model_inputs.attention_mask , num_beams=2 ) lowercase :Optional[int] = self.tokenizer.batch_decode(_a , skip_special_tokens=_a ) return generated_words @slow def SCREAMING_SNAKE_CASE ( self: Union[str, Any] ): self._assert_generated_batch_equal_expected()
355
def UpperCAmelCase__ ( lowerCamelCase ): if not isinstance(lowerCamelCase, lowerCamelCase ): raise TypeError("Input value must be an 'int' type" ) lowercase :int = 0 while number: position += 1 number >>= 1 return position if __name__ == "__main__": import doctest doctest.testmod()
158
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available _snake_case = { "configuration_table_transformer": [ "TABLE_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", "TableTransformerConfig", "TableTransformerOnnxConfig", ] } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = [ "TABLE_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "TableTransformerForObjectDetection", "TableTransformerModel", "TableTransformerPreTrainedModel", ] if TYPE_CHECKING: from .configuration_table_transformer import ( TABLE_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TableTransformerConfig, TableTransformerOnnxConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_table_transformer import ( TABLE_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TableTransformerForObjectDetection, TableTransformerModel, TableTransformerPreTrainedModel, ) else: import sys _snake_case = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
26
"""simple docstring""" import baseaa def _A ( UpperCamelCase_ : str) -> bytes: '''simple docstring''' return baseaa.baaencode(string.encode("utf-8")) def _A ( UpperCamelCase_ : bytes) -> str: '''simple docstring''' return baseaa.baadecode(UpperCamelCase_).decode("utf-8") if __name__ == "__main__": _a = 'Hello World!' _a = baseaa_encode(test) print(encoded) _a = baseaa_decode(encoded) print(decoded)
17
0
"""simple docstring""" import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import XLMRobertaTokenizerFast from diffusers import DDIMScheduler, KandinskyInpaintPipeline, KandinskyPriorPipeline, UNetaDConditionModel, VQModel from diffusers.pipelines.kandinsky.text_encoder import MCLIPConfig, MultilingualCLIP from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference enable_full_determinism() class A_ ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): """simple docstring""" __UpperCamelCase = KandinskyInpaintPipeline __UpperCamelCase = ["""prompt""", """image_embeds""", """negative_image_embeds""", """image""", """mask_image"""] __UpperCamelCase = [ """prompt""", """negative_prompt""", """image_embeds""", """negative_image_embeds""", """image""", """mask_image""", ] __UpperCamelCase = [ """generator""", """height""", """width""", """latents""", """guidance_scale""", """negative_prompt""", """num_inference_steps""", """return_dict""", """guidance_scale""", """num_images_per_prompt""", """output_type""", """return_dict""", ] __UpperCamelCase = False @property def UpperCAmelCase__ ( self :Any ) -> Tuple: return 32 @property def UpperCAmelCase__ ( self :str ) -> List[str]: return 32 @property def UpperCAmelCase__ ( self :List[str] ) -> Union[str, Any]: return self.time_input_dim @property def UpperCAmelCase__ ( self :int ) -> Optional[Any]: return self.time_input_dim * 4 @property def UpperCAmelCase__ ( self :List[Any] ) -> Optional[Any]: return 1_00 @property def UpperCAmelCase__ ( self :str ) -> Union[str, Any]: UpperCAmelCase = XLMRobertaTokenizerFast.from_pretrained('YiYiXu/tiny-random-mclip-base' ) return tokenizer @property def UpperCAmelCase__ ( self :Any ) -> List[str]: torch.manual_seed(0 ) UpperCAmelCase = MCLIPConfig( numDims=self.cross_attention_dim , transformerDimensions=self.text_embedder_hidden_size , hidden_size=self.text_embedder_hidden_size , intermediate_size=37 , num_attention_heads=4 , num_hidden_layers=5 , vocab_size=10_05 , ) UpperCAmelCase = MultilingualCLIP(lowercase_ ) UpperCAmelCase = text_encoder.eval() return text_encoder @property def UpperCAmelCase__ ( self :str ) -> Optional[int]: torch.manual_seed(0 ) UpperCAmelCase = { 'in_channels': 9, # Out channels is double in channels because predicts mean and variance 'out_channels': 8, 'addition_embed_type': 'text_image', 'down_block_types': ('ResnetDownsampleBlock2D', 'SimpleCrossAttnDownBlock2D'), 'up_block_types': ('SimpleCrossAttnUpBlock2D', 'ResnetUpsampleBlock2D'), 'mid_block_type': 'UNetMidBlock2DSimpleCrossAttn', 'block_out_channels': (self.block_out_channels_a, self.block_out_channels_a * 2), 'layers_per_block': 1, 'encoder_hid_dim': self.text_embedder_hidden_size, 'encoder_hid_dim_type': 'text_image_proj', 'cross_attention_dim': self.cross_attention_dim, 'attention_head_dim': 4, 'resnet_time_scale_shift': 'scale_shift', 'class_embed_type': None, } UpperCAmelCase = UNetaDConditionModel(**lowercase_ ) return model @property def UpperCAmelCase__ ( self :str ) -> Tuple: return { "block_out_channels": [32, 64], "down_block_types": ["DownEncoderBlock2D", "AttnDownEncoderBlock2D"], "in_channels": 3, "latent_channels": 4, "layers_per_block": 1, "norm_num_groups": 8, "norm_type": "spatial", "num_vq_embeddings": 12, "out_channels": 3, "up_block_types": [ "AttnUpDecoderBlock2D", "UpDecoderBlock2D", ], "vq_embed_dim": 4, } @property def UpperCAmelCase__ ( self :Optional[Any] ) -> Optional[int]: torch.manual_seed(0 ) UpperCAmelCase = VQModel(**self.dummy_movq_kwargs ) return model def UpperCAmelCase__ ( self :List[Any] ) -> List[Any]: UpperCAmelCase = self.dummy_text_encoder UpperCAmelCase = self.dummy_tokenizer UpperCAmelCase = self.dummy_unet UpperCAmelCase = self.dummy_movq UpperCAmelCase = DDIMScheduler( num_train_timesteps=10_00 , beta_schedule='linear' , beta_start=0.0_0085 , beta_end=0.012 , clip_sample=lowercase_ , set_alpha_to_one=lowercase_ , steps_offset=1 , prediction_type='epsilon' , thresholding=lowercase_ , ) UpperCAmelCase = { 'text_encoder': text_encoder, 'tokenizer': tokenizer, 'unet': unet, 'scheduler': scheduler, 'movq': movq, } return components def UpperCAmelCase__ ( self :List[str] , lowercase_ :List[str] , lowercase_ :List[Any]=0 ) -> Dict: UpperCAmelCase = floats_tensor((1, self.cross_attention_dim) , rng=random.Random(lowercase_ ) ).to(lowercase_ ) UpperCAmelCase = floats_tensor((1, self.cross_attention_dim) , rng=random.Random(seed + 1 ) ).to(lowercase_ ) # create init_image UpperCAmelCase = floats_tensor((1, 3, 64, 64) , rng=random.Random(lowercase_ ) ).to(lowercase_ ) UpperCAmelCase = image.cpu().permute(0 , 2 , 3 , 1 )[0] UpperCAmelCase = Image.fromarray(np.uinta(lowercase_ ) ).convert('RGB' ).resize((2_56, 2_56) ) # create mask UpperCAmelCase = np.ones((64, 64) , dtype=np.floataa ) UpperCAmelCase = 0 if str(lowercase_ ).startswith('mps' ): UpperCAmelCase = torch.manual_seed(lowercase_ ) else: UpperCAmelCase = torch.Generator(device=lowercase_ ).manual_seed(lowercase_ ) UpperCAmelCase = { 'prompt': 'horse', 'image': init_image, 'mask_image': mask, 'image_embeds': image_embeds, 'negative_image_embeds': negative_image_embeds, 'generator': generator, 'height': 64, 'width': 64, 'num_inference_steps': 2, 'guidance_scale': 4.0, 'output_type': 'np', } return inputs def UpperCAmelCase__ ( self :List[str] ) -> List[str]: UpperCAmelCase = 'cpu' UpperCAmelCase = self.get_dummy_components() UpperCAmelCase = self.pipeline_class(**lowercase_ ) UpperCAmelCase = pipe.to(lowercase_ ) pipe.set_progress_bar_config(disable=lowercase_ ) UpperCAmelCase = pipe(**self.get_dummy_inputs(lowercase_ ) ) UpperCAmelCase = output.images UpperCAmelCase = pipe( **self.get_dummy_inputs(lowercase_ ) , return_dict=lowercase_ , )[0] UpperCAmelCase = image[0, -3:, -3:, -1] UpperCAmelCase = image_from_tuple[0, -3:, -3:, -1] print(f"""image.shape {image.shape}""" ) assert image.shape == (1, 64, 64, 3) UpperCAmelCase = np.array( [0.832_6919, 0.7379_0467, 0.2091_8581, 0.930_9612, 0.551_1791, 0.4371_3328, 0.551_3321, 0.4992_2934, 0.5949_7786] ) assert ( np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 ), f""" expected_slice {expected_slice}, but got {image_slice.flatten()}""" assert ( np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1E-2 ), f""" expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}""" def UpperCAmelCase__ ( self :Tuple ) -> Union[str, Any]: super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) @slow @require_torch_gpu class A_ ( unittest.TestCase ): """simple docstring""" def UpperCAmelCase__ ( self :int ) -> str: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def UpperCAmelCase__ ( self :Optional[int] ) -> Optional[int]: UpperCAmelCase = load_numpy( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/kandinsky/kandinsky_inpaint_cat_with_hat_fp16.npy' ) UpperCAmelCase = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/kandinsky/cat.png' ) UpperCAmelCase = np.ones((7_68, 7_68) , dtype=np.floataa ) UpperCAmelCase = 0 UpperCAmelCase = 'a hat' UpperCAmelCase = KandinskyPriorPipeline.from_pretrained( 'kandinsky-community/kandinsky-2-1-prior' , torch_dtype=torch.floataa ) pipe_prior.to(lowercase_ ) UpperCAmelCase = KandinskyInpaintPipeline.from_pretrained( 'kandinsky-community/kandinsky-2-1-inpaint' , torch_dtype=torch.floataa ) UpperCAmelCase = pipeline.to(lowercase_ ) pipeline.set_progress_bar_config(disable=lowercase_ ) UpperCAmelCase = torch.Generator(device='cpu' ).manual_seed(0 ) UpperCAmelCase , UpperCAmelCase = pipe_prior( lowercase_ , generator=lowercase_ , num_inference_steps=5 , negative_prompt='' , ).to_tuple() UpperCAmelCase = pipeline( lowercase_ , image=lowercase_ , mask_image=lowercase_ , image_embeds=lowercase_ , negative_image_embeds=lowercase_ , generator=lowercase_ , num_inference_steps=1_00 , height=7_68 , width=7_68 , output_type='np' , ) UpperCAmelCase = output.images[0] assert image.shape == (7_68, 7_68, 3) assert_mean_pixel_difference(lowercase_ , lowercase_ )
181
"""simple docstring""" from math import factorial, radians def _lowerCAmelCase ( lowercase_ , lowercase_ = 18 , lowercase_ = 10 ): UpperCAmelCase = angle_in_degrees - ((angle_in_degrees // 3_6_0.0) * 3_6_0.0) # Converting from degrees to radians UpperCAmelCase = radians(lowercase_ ) UpperCAmelCase = angle_in_radians UpperCAmelCase = 3 UpperCAmelCase = -1 for _ in range(lowercase_ ): result += (b * (angle_in_radians**a)) / factorial(lowercase_ ) UpperCAmelCase = -b # One positive term and the next will be negative and so on... a += 2 # Increased by 2 for every term. return round(lowercase_ , lowercase_ ) if __name__ == "__main__": __import__("""doctest""").testmod()
181
1
"""simple docstring""" import warnings from ...utils import logging from .image_processing_deit import DeiTImageProcessor lowerCAmelCase : Dict = logging.get_logger(__name__) class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' def __init__( self , *_a , **_a ): """simple docstring""" warnings.warn( """The class DeiTFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please""" """ use DeiTImageProcessor instead.""" , _a , ) super().__init__(*_a , **_a )
291
"""simple docstring""" from __future__ import annotations def a__ ( snake_case__ , snake_case__ ) -> bool: if len(snake_case__ ) == 0: return False lowerCamelCase = len(snake_case__ ) // 2 if a_list[midpoint] == item: return True if item < a_list[midpoint]: return binary_search(a_list[:midpoint] , snake_case__ ) else: return binary_search(a_list[midpoint + 1 :] , snake_case__ ) if __name__ == "__main__": lowerCAmelCase : List[Any] = input("""Enter numbers separated by comma:\n""").strip() lowerCAmelCase : Optional[Any] = [int(item.strip()) for item in user_input.split(""",""")] lowerCAmelCase : Optional[int] = int(input("""Enter the number to be found in the list:\n""").strip()) lowerCAmelCase : Union[str, Any] = """""" if binary_search(sequence, target) else """not """ print(F"""{target} was {not_str}found in {sequence}""")
291
1
'''simple docstring''' import warnings from ...utils import logging from .image_processing_perceiver import PerceiverImageProcessor A__ : Optional[Any] = logging.get_logger(__name__) class snake_case__ ( SCREAMING_SNAKE_CASE_ ): def __init__( self : Union[str, Any] , *__a : Optional[Any] , **__a : List[str] ) -> None: '''simple docstring''' warnings.warn( 'The class PerceiverFeatureExtractor is deprecated and will be removed in version 5 of Transformers.' ' Please use PerceiverImageProcessor instead.' , __a , ) super().__init__(*__a , **__a )
0
'''simple docstring''' import math def a_ ( _UpperCAmelCase : int ) -> list: __snake_case : Optional[Any] = [True] * n __snake_case : Optional[int] = False __snake_case : Dict = False __snake_case : List[Any] = True for i in range(3 ,int(n**0.5 + 1 ) ,2 ): __snake_case : Optional[int] = i * 2 while index < n: __snake_case : Union[str, Any] = False __snake_case : int = index + i __snake_case : Dict = [2] for i in range(3 ,_UpperCAmelCase ,2 ): if is_prime[i]: primes.append(_UpperCAmelCase ) return primes def a_ ( _UpperCAmelCase : int = 99_99_66_66_33_33 ) -> int: __snake_case : List[Any] = math.floor(math.sqrt(_UpperCAmelCase ) ) + 1_00 __snake_case : Tuple = prime_sieve(_UpperCAmelCase ) __snake_case : List[Any] = 0 __snake_case : List[Any] = 0 __snake_case : Optional[int] = primes[prime_index] while (last_prime**2) <= limit: __snake_case : Optional[int] = primes[prime_index + 1] __snake_case : Union[str, Any] = last_prime**2 __snake_case : Dict = next_prime**2 # Get numbers divisible by lps(current) __snake_case : Optional[Any] = lower_bound + last_prime while upper_bound > current <= limit: matches_sum += current current += last_prime # Reset the upper_bound while (upper_bound - next_prime) > limit: upper_bound -= next_prime # Add the numbers divisible by ups(current) __snake_case : Optional[Any] = upper_bound - next_prime while current > lower_bound: matches_sum += current current -= next_prime # Remove the numbers divisible by both ups and lps __snake_case : List[str] = 0 while upper_bound > current <= limit: if current <= lower_bound: # Increment the current number current += last_prime * next_prime continue if current > limit: break # Remove twice since it was added by both ups and lps matches_sum -= current * 2 # Increment the current number current += last_prime * next_prime # Setup for next pair __snake_case : Dict = next_prime prime_index += 1 return matches_sum if __name__ == "__main__": print(solution())
0
1
from .glue import glue_convert_examples_to_features, glue_output_modes, glue_processors, glue_tasks_num_labels from .squad import SquadExample, SquadFeatures, SquadVaProcessor, SquadVaProcessor, squad_convert_examples_to_features from .utils import DataProcessor, InputExample, InputFeatures, SingleSentenceClassificationProcessor from .xnli import xnli_output_modes, xnli_processors, xnli_tasks_num_labels
283
from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging _snake_case = logging.get_logger(__name__) _snake_case = { '''google/mobilenet_v2_1.4_224''': '''https://huggingface.co/google/mobilenet_v2_1.4_224/resolve/main/config.json''', '''google/mobilenet_v2_1.0_224''': '''https://huggingface.co/google/mobilenet_v2_1.0_224/resolve/main/config.json''', '''google/mobilenet_v2_0.75_160''': '''https://huggingface.co/google/mobilenet_v2_0.75_160/resolve/main/config.json''', '''google/mobilenet_v2_0.35_96''': '''https://huggingface.co/google/mobilenet_v2_0.35_96/resolve/main/config.json''', # See all MobileNetV2 models at https://huggingface.co/models?filter=mobilenet_v2 } class UpperCAmelCase_ ( UpperCamelCase ): '''simple docstring''' __A : Dict = "mobilenet_v2" def __init__( self , __A=3 , __A=224 , __A=1.0 , __A=8 , __A=8 , __A=6 , __A=32 , __A=True , __A=True , __A="relu6" , __A=True , __A=0.8 , __A=0.02 , __A=0.001 , __A=255 , **__A , ): """simple docstring""" super().__init__(**__A ) if depth_multiplier <= 0: raise ValueError("depth_multiplier must be greater than zero." ) lowerCamelCase : str = num_channels lowerCamelCase : Any = image_size lowerCamelCase : Union[str, Any] = depth_multiplier lowerCamelCase : Tuple = depth_divisible_by lowerCamelCase : Dict = min_depth lowerCamelCase : Dict = expand_ratio lowerCamelCase : Optional[Any] = output_stride lowerCamelCase : int = first_layer_is_expansion lowerCamelCase : Union[str, Any] = finegrained_output lowerCamelCase : Optional[Any] = hidden_act lowerCamelCase : Optional[Any] = tf_padding lowerCamelCase : Optional[Any] = classifier_dropout_prob lowerCamelCase : Dict = initializer_range lowerCamelCase : str = layer_norm_eps lowerCamelCase : Optional[Any] = semantic_loss_ignore_index class UpperCAmelCase_ ( UpperCamelCase ): '''simple docstring''' __A : Union[str, Any] = version.parse("1.11" ) @property def _snake_case ( self ): """simple docstring""" return OrderedDict([("pixel_values", {0: "batch"})] ) @property def _snake_case ( self ): """simple docstring""" if self.task == "image-classification": return OrderedDict([("logits", {0: "batch"})] ) else: return OrderedDict([("last_hidden_state", {0: "batch"}), ("pooler_output", {0: "batch"})] ) @property def _snake_case ( self ): """simple docstring""" return 1e-4
283
1
from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging if TYPE_CHECKING: from ... import FeatureExtractionMixin, TensorType lowerCAmelCase__ :str = logging.get_logger(__name__) lowerCAmelCase__ :Any = { '''openai/imagegpt-small''': '''''', '''openai/imagegpt-medium''': '''''', '''openai/imagegpt-large''': '''''', } class __a ( UpperCAmelCase ): _a : Union[str, Any] = 'imagegpt' _a : Any = ['past_key_values'] _a : Tuple = { 'hidden_size': 'n_embd', 'max_position_embeddings': 'n_positions', 'num_attention_heads': 'n_head', 'num_hidden_layers': 'n_layer', } def __init__( self , _SCREAMING_SNAKE_CASE=512 + 1 , _SCREAMING_SNAKE_CASE=32 * 32 , _SCREAMING_SNAKE_CASE=512 , _SCREAMING_SNAKE_CASE=24 , _SCREAMING_SNAKE_CASE=8 , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE="quick_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=True , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=False , _SCREAMING_SNAKE_CASE=False , _SCREAMING_SNAKE_CASE=False , **_SCREAMING_SNAKE_CASE , ) -> Dict: """simple docstring""" _UpperCAmelCase = vocab_size _UpperCAmelCase = n_positions _UpperCAmelCase = n_embd _UpperCAmelCase = n_layer _UpperCAmelCase = n_head _UpperCAmelCase = n_inner _UpperCAmelCase = activation_function _UpperCAmelCase = resid_pdrop _UpperCAmelCase = embd_pdrop _UpperCAmelCase = attn_pdrop _UpperCAmelCase = layer_norm_epsilon _UpperCAmelCase = initializer_range _UpperCAmelCase = scale_attn_weights _UpperCAmelCase = use_cache _UpperCAmelCase = scale_attn_by_inverse_layer_idx _UpperCAmelCase = reorder_and_upcast_attn _UpperCAmelCase = tie_word_embeddings super().__init__(tie_word_embeddings=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) class __a ( UpperCAmelCase ): @property def UpperCAmelCase__ ( self ) -> Mapping[str, Mapping[int, str]]: """simple docstring""" return OrderedDict( [ ('input_ids', {0: 'batch', 1: 'sequence'}), ] ) def UpperCAmelCase__ ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = 1 , _SCREAMING_SNAKE_CASE = -1 , _SCREAMING_SNAKE_CASE = False , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = 3 , _SCREAMING_SNAKE_CASE = 32 , _SCREAMING_SNAKE_CASE = 32 , ) -> Mapping[str, Any]: """simple docstring""" _UpperCAmelCase = self._generate_dummy_images(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) _UpperCAmelCase = dict(preprocessor(images=_SCREAMING_SNAKE_CASE , return_tensors=_SCREAMING_SNAKE_CASE ) ) return inputs
185
from __future__ import annotations import time from collections.abc import Sequence from random import randint from matplotlib import pyplot as plt def lowerCAmelCase__ ( a__: Sequence[float] , a__: int , a__: int ) -> tuple[int | None, int | None, float]: '''simple docstring''' if not arr: return None, None, 0 if low == high: return low, high, arr[low] _UpperCAmelCase = (low + high) // 2 _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase = max_subarray(a__ , a__ , a__ ) _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase = max_subarray(a__ , mid + 1 , a__ ) _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase = max_cross_sum(a__ , a__ , a__ , a__ ) if left_sum >= right_sum and left_sum >= cross_sum: return left_low, left_high, left_sum elif right_sum >= left_sum and right_sum >= cross_sum: return right_low, right_high, right_sum return cross_left, cross_right, cross_sum def lowerCAmelCase__ ( a__: Sequence[float] , a__: int , a__: int , a__: int ) -> tuple[int, int, float]: '''simple docstring''' _UpperCAmelCase , _UpperCAmelCase = float('-inf' ), -1 _UpperCAmelCase , _UpperCAmelCase = float('-inf' ), -1 _UpperCAmelCase = 0 for i in range(a__ , low - 1 , -1 ): summ += arr[i] if summ > left_sum: _UpperCAmelCase = summ _UpperCAmelCase = i _UpperCAmelCase = 0 for i in range(mid + 1 , high + 1 ): summ += arr[i] if summ > right_sum: _UpperCAmelCase = summ _UpperCAmelCase = i return max_left, max_right, (left_sum + right_sum) def lowerCAmelCase__ ( a__: int ) -> float: '''simple docstring''' _UpperCAmelCase = [randint(1 , a__ ) for _ in range(a__ )] _UpperCAmelCase = time.time() max_subarray(a__ , 0 , input_size - 1 ) _UpperCAmelCase = time.time() return end - start def lowerCAmelCase__ ( ) -> None: '''simple docstring''' _UpperCAmelCase = [1_0, 1_0_0, 1_0_0_0, 1_0_0_0_0, 5_0_0_0_0, 1_0_0_0_0_0, 2_0_0_0_0_0, 3_0_0_0_0_0, 4_0_0_0_0_0, 5_0_0_0_0_0] _UpperCAmelCase = [time_max_subarray(a__ ) for input_size in input_sizes] print('No of Inputs\t\tTime Taken' ) for input_size, runtime in zip(a__ , a__ ): print(a__ , '\t\t' , a__ ) plt.plot(a__ , a__ ) plt.xlabel('Number of Inputs' ) plt.ylabel('Time taken in seconds' ) plt.show() if __name__ == "__main__": from doctest import testmod testmod()
185
1
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase : Tuple = logging.get_logger(__name__) lowerCamelCase : Dict = { 'microsoft/cvt-13': 'https://huggingface.co/microsoft/cvt-13/resolve/main/config.json', # See all Cvt models at https://huggingface.co/models?filter=cvt } class __lowerCAmelCase (lowercase_ ): '''simple docstring''' lowerCAmelCase__ : Tuple = """cvt""" def __init__(self : int , UpperCamelCase : List[Any]=3 , UpperCamelCase : int=[7, 3, 3] , UpperCamelCase : str=[4, 2, 2] , UpperCamelCase : Dict=[2, 1, 1] , UpperCamelCase : Dict=[64, 192, 384] , UpperCamelCase : Dict=[1, 3, 6] , UpperCamelCase : Dict=[1, 2, 10] , UpperCamelCase : Any=[4.0, 4.0, 4.0] , UpperCamelCase : Optional[int]=[0.0, 0.0, 0.0] , UpperCamelCase : Optional[int]=[0.0, 0.0, 0.0] , UpperCamelCase : int=[0.0, 0.0, 0.1] , UpperCamelCase : Any=[True, True, True] , UpperCamelCase : int=[False, False, True] , UpperCamelCase : Union[str, Any]=["dw_bn", "dw_bn", "dw_bn"] , UpperCamelCase : Optional[int]=[3, 3, 3] , UpperCamelCase : Tuple=[1, 1, 1] , UpperCamelCase : Any=[2, 2, 2] , UpperCamelCase : Dict=[1, 1, 1] , UpperCamelCase : List[str]=[1, 1, 1] , UpperCamelCase : str=0.02 , UpperCamelCase : int=1E-12 , **UpperCamelCase : Union[str, Any] , ): '''simple docstring''' super().__init__(**UpperCamelCase ) lowercase__ = num_channels lowercase__ = patch_sizes lowercase__ = patch_stride lowercase__ = patch_padding lowercase__ = embed_dim lowercase__ = num_heads lowercase__ = depth lowercase__ = mlp_ratio lowercase__ = attention_drop_rate lowercase__ = drop_rate lowercase__ = drop_path_rate lowercase__ = qkv_bias lowercase__ = cls_token lowercase__ = qkv_projection_method lowercase__ = kernel_qkv lowercase__ = padding_kv lowercase__ = stride_kv lowercase__ = padding_q lowercase__ = stride_q lowercase__ = initializer_range lowercase__ = layer_norm_eps
2
'''simple docstring''' from argparse import ArgumentParser from datasets.commands.convert import ConvertCommand from datasets.commands.dummy_data import DummyDataCommand from datasets.commands.env import EnvironmentCommand from datasets.commands.run_beam import RunBeamCommand from datasets.commands.test import TestCommand from datasets.utils.logging import set_verbosity_info def __a(SCREAMING_SNAKE_CASE_ : Optional[Any] ): '''simple docstring''' return {key.lstrip("-" ): value for key, value in zip(unknown_args[::2] , unknown_args[1::2] )} def __a(): '''simple docstring''' _lowerCAmelCase = ArgumentParser( "HuggingFace Datasets CLI tool" , usage="datasets-cli <command> [<args>]" , allow_abbrev=SCREAMING_SNAKE_CASE_ ) _lowerCAmelCase = parser.add_subparsers(help="datasets-cli command helpers" ) set_verbosity_info() # Register commands ConvertCommand.register_subcommand(SCREAMING_SNAKE_CASE_ ) EnvironmentCommand.register_subcommand(SCREAMING_SNAKE_CASE_ ) TestCommand.register_subcommand(SCREAMING_SNAKE_CASE_ ) RunBeamCommand.register_subcommand(SCREAMING_SNAKE_CASE_ ) DummyDataCommand.register_subcommand(SCREAMING_SNAKE_CASE_ ) # Parse args _lowerCAmelCase , _lowerCAmelCase = parser.parse_known_args() if not hasattr(SCREAMING_SNAKE_CASE_ , "func" ): parser.print_help() exit(1 ) _lowerCAmelCase = parse_unknown_args(SCREAMING_SNAKE_CASE_ ) # Run _lowerCAmelCase = args.func(SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) service.run() if __name__ == "__main__": main()
158
0
from __future__ import annotations import unittest from transformers import MobileBertConfig, is_tf_available from transformers.models.auto import get_values from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TF_MODEL_FOR_PRETRAINING_MAPPING, TFMobileBertForMaskedLM, TFMobileBertForMultipleChoice, TFMobileBertForNextSentencePrediction, TFMobileBertForPreTraining, TFMobileBertForQuestionAnswering, TFMobileBertForSequenceClassification, TFMobileBertForTokenClassification, TFMobileBertModel, ) @require_tf class __lowerCAmelCase ( lowerCamelCase_ , lowerCamelCase_ , unittest.TestCase ): """simple docstring""" snake_case_ = ( ( TFMobileBertModel, TFMobileBertForMaskedLM, TFMobileBertForNextSentencePrediction, TFMobileBertForPreTraining, TFMobileBertForQuestionAnswering, TFMobileBertForSequenceClassification, TFMobileBertForTokenClassification, TFMobileBertForMultipleChoice, ) if is_tf_available() else () ) snake_case_ = ( { """feature-extraction""": TFMobileBertModel, """fill-mask""": TFMobileBertForMaskedLM, """question-answering""": TFMobileBertForQuestionAnswering, """text-classification""": TFMobileBertForSequenceClassification, """token-classification""": TFMobileBertForTokenClassification, """zero-shot""": TFMobileBertForSequenceClassification, } if is_tf_available() else {} ) snake_case_ = False snake_case_ = False def lowercase_ ( self , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__=False ) -> str: '''simple docstring''' __lowerCamelCase = super()._prepare_for_class(lowerCAmelCase__ , lowerCAmelCase__ , return_labels=lowerCAmelCase__ ) if return_labels: if model_class in get_values(lowerCAmelCase__ ): __lowerCamelCase = tf.zeros(self.model_tester.batch_size , dtype=tf.intaa ) return inputs_dict class __lowerCAmelCase ( lowerCamelCase_ ): """simple docstring""" def __init__( self , lowerCamelCase__ , lowerCamelCase__=13 , lowerCamelCase__=7 , lowerCamelCase__=True , lowerCamelCase__=True , lowerCamelCase__=True , lowerCamelCase__=True , lowerCamelCase__=99 , lowerCamelCase__=32 , lowerCamelCase__=32 , lowerCamelCase__=2 , lowerCamelCase__=4 , lowerCamelCase__=37 , lowerCamelCase__="gelu" , lowerCamelCase__=0.1 , lowerCamelCase__=0.1 , lowerCamelCase__=512 , lowerCamelCase__=16 , lowerCamelCase__=2 , lowerCamelCase__=0.02 , lowerCamelCase__=3 , lowerCamelCase__=4 , lowerCamelCase__=None , ) -> Optional[int]: '''simple docstring''' __lowerCamelCase = parent __lowerCamelCase = batch_size __lowerCamelCase = seq_length __lowerCamelCase = is_training __lowerCamelCase = use_input_mask __lowerCamelCase = use_token_type_ids __lowerCamelCase = use_labels __lowerCamelCase = vocab_size __lowerCamelCase = hidden_size __lowerCamelCase = num_hidden_layers __lowerCamelCase = num_attention_heads __lowerCamelCase = intermediate_size __lowerCamelCase = hidden_act __lowerCamelCase = hidden_dropout_prob __lowerCamelCase = attention_probs_dropout_prob __lowerCamelCase = max_position_embeddings __lowerCamelCase = type_vocab_size __lowerCamelCase = type_sequence_label_size __lowerCamelCase = initializer_range __lowerCamelCase = num_labels __lowerCamelCase = num_choices __lowerCamelCase = scope __lowerCamelCase = embedding_size def lowercase_ ( self ) -> Optional[int]: '''simple docstring''' __lowerCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __lowerCamelCase = None if self.use_input_mask: __lowerCamelCase = random_attention_mask([self.batch_size, self.seq_length] ) __lowerCamelCase = None if self.use_token_type_ids: __lowerCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) __lowerCamelCase = None __lowerCamelCase = None __lowerCamelCase = None if self.use_labels: __lowerCamelCase = ids_tensor([self.batch_size] , self.type_sequence_label_size ) __lowerCamelCase = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) __lowerCamelCase = ids_tensor([self.batch_size] , self.num_choices ) __lowerCamelCase = MobileBertConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , embedding_size=self.embedding_size , ) return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def lowercase_ ( self , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) -> Optional[int]: '''simple docstring''' __lowerCamelCase = TFMobileBertModel(config=lowerCAmelCase__ ) __lowerCamelCase = {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids} __lowerCamelCase = model(lowerCAmelCase__ ) __lowerCamelCase = [input_ids, input_mask] __lowerCamelCase = model(lowerCAmelCase__ ) __lowerCamelCase = model(lowerCAmelCase__ ) 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 lowercase_ ( self , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) -> Optional[int]: '''simple docstring''' __lowerCamelCase = TFMobileBertForMaskedLM(config=lowerCAmelCase__ ) __lowerCamelCase = {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids} __lowerCamelCase = model(lowerCAmelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def lowercase_ ( self , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) -> Union[str, Any]: '''simple docstring''' __lowerCamelCase = TFMobileBertForNextSentencePrediction(config=lowerCAmelCase__ ) __lowerCamelCase = {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids} __lowerCamelCase = model(lowerCAmelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, 2) ) def lowercase_ ( self , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) -> List[Any]: '''simple docstring''' __lowerCamelCase = TFMobileBertForPreTraining(config=lowerCAmelCase__ ) __lowerCamelCase = {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids} __lowerCamelCase = model(lowerCAmelCase__ ) 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 lowercase_ ( self , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) -> Tuple: '''simple docstring''' __lowerCamelCase = self.num_labels __lowerCamelCase = TFMobileBertForSequenceClassification(config=lowerCAmelCase__ ) __lowerCamelCase = {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids} __lowerCamelCase = model(lowerCAmelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def lowercase_ ( self , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) -> str: '''simple docstring''' __lowerCamelCase = self.num_choices __lowerCamelCase = TFMobileBertForMultipleChoice(config=lowerCAmelCase__ ) __lowerCamelCase = tf.tile(tf.expand_dims(lowerCAmelCase__ , 1 ) , (1, self.num_choices, 1) ) __lowerCamelCase = tf.tile(tf.expand_dims(lowerCAmelCase__ , 1 ) , (1, self.num_choices, 1) ) __lowerCamelCase = tf.tile(tf.expand_dims(lowerCAmelCase__ , 1 ) , (1, self.num_choices, 1) ) __lowerCamelCase = { 'input_ids': multiple_choice_inputs_ids, 'attention_mask': multiple_choice_input_mask, 'token_type_ids': multiple_choice_token_type_ids, } __lowerCamelCase = model(lowerCAmelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def lowercase_ ( self , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) -> Optional[Any]: '''simple docstring''' __lowerCamelCase = self.num_labels __lowerCamelCase = TFMobileBertForTokenClassification(config=lowerCAmelCase__ ) __lowerCamelCase = {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids} __lowerCamelCase = model(lowerCAmelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def lowercase_ ( self , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) -> Union[str, Any]: '''simple docstring''' __lowerCamelCase = TFMobileBertForQuestionAnswering(config=lowerCAmelCase__ ) __lowerCamelCase = {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids} __lowerCamelCase = model(lowerCAmelCase__ ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def lowercase_ ( self ) -> Any: '''simple docstring''' __lowerCamelCase = self.prepare_config_and_inputs() ( ( __lowerCamelCase ) , ( __lowerCamelCase ) , ( __lowerCamelCase ) , ( __lowerCamelCase ) , ( __lowerCamelCase ) , ( __lowerCamelCase ) , ( __lowerCamelCase ) , ) = config_and_inputs __lowerCamelCase = {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask} return config, inputs_dict def lowercase_ ( self ) -> str: '''simple docstring''' __lowerCamelCase = TFMobileBertModelTest.TFMobileBertModelTester(self ) __lowerCamelCase = ConfigTester(self , config_class=lowerCAmelCase__ , hidden_size=37 ) def lowercase_ ( self ) -> Optional[Any]: '''simple docstring''' self.config_tester.run_common_tests() def lowercase_ ( self ) -> List[str]: '''simple docstring''' __lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_model(*lowerCAmelCase__ ) def lowercase_ ( self ) -> Optional[int]: '''simple docstring''' __lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_masked_lm(*lowerCAmelCase__ ) def lowercase_ ( self ) -> Dict: '''simple docstring''' __lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_multiple_choice(*lowerCAmelCase__ ) def lowercase_ ( self ) -> Dict: '''simple docstring''' __lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*lowerCAmelCase__ ) def lowercase_ ( self ) -> List[Any]: '''simple docstring''' __lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_pretraining(*lowerCAmelCase__ ) def lowercase_ ( self ) -> Any: '''simple docstring''' __lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_question_answering(*lowerCAmelCase__ ) def lowercase_ ( self ) -> Any: '''simple docstring''' __lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_sequence_classification(*lowerCAmelCase__ ) def lowercase_ ( self ) -> Optional[Any]: '''simple docstring''' __lowerCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_token_classification(*lowerCAmelCase__ ) @slow def lowercase_ ( self ) -> int: '''simple docstring''' # for model_name in TF_MOBILEBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: for model_name in ["google/mobilebert-uncased"]: __lowerCamelCase = TFMobileBertModel.from_pretrained(lowerCAmelCase__ ) self.assertIsNotNone(lowerCAmelCase__ ) @require_tf class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" @slow def lowercase_ ( self ) -> Union[str, Any]: '''simple docstring''' __lowerCamelCase = TFMobileBertForPreTraining.from_pretrained('google/mobilebert-uncased' ) __lowerCamelCase = tf.constant([[0, 1, 2, 3, 4, 5]] ) __lowerCamelCase = model(lowerCAmelCase__ )[0] __lowerCamelCase = [1, 6, 30_522] self.assertEqual(output.shape , lowerCAmelCase__ ) __lowerCamelCase = tf.constant( [ [ [-4.5_91_95_47, -9.24_82_95, -9.64_52_56], [-6.7_30_61_75, -6.44_02_84, -6.6_05_28_37], [-7.2_74_35_06, -6.7_84_79_15, -6.02_46_73], ] ] ) tf.debugging.assert_near(output[:, :3, :3] , lowerCAmelCase__ , atol=1e-4 )
366
import argparse import json import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from torchvision import transforms from transformers import BitImageProcessor, FocalNetConfig, FocalNetForImageClassification from transformers.image_utils import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, PILImageResampling def lowerCamelCase_ ( UpperCamelCase__ : List[Any] ) -> Any: """simple docstring""" __lowerCamelCase = [2, 2, 6, 2] if 'tiny' in model_name else [2, 2, 18, 2] __lowerCamelCase = True if 'large' in model_name or 'huge' in model_name else False __lowerCamelCase = True if 'large' in model_name or 'huge' in model_name else False __lowerCamelCase = True if 'large' in model_name or 'huge' in model_name else False if "large" in model_name or "xlarge" in model_name or "huge" in model_name: if "fl3" in model_name: __lowerCamelCase = [3, 3, 3, 3] __lowerCamelCase = [5, 5, 5, 5] elif "fl4" in model_name: __lowerCamelCase = [4, 4, 4, 4] __lowerCamelCase = [3, 3, 3, 3] if "tiny" in model_name or "small" in model_name or "base" in model_name: __lowerCamelCase = [3, 3, 3, 3] if "lrf" in model_name: __lowerCamelCase = [3, 3, 3, 3] else: __lowerCamelCase = [2, 2, 2, 2] if "tiny" in model_name: __lowerCamelCase = 96 elif "small" in model_name: __lowerCamelCase = 96 elif "base" in model_name: __lowerCamelCase = 128 elif "large" in model_name: __lowerCamelCase = 192 elif "xlarge" in model_name: __lowerCamelCase = 256 elif "huge" in model_name: __lowerCamelCase = 352 # set label information __lowerCamelCase = 'huggingface/label-files' if "large" in model_name or "huge" in model_name: __lowerCamelCase = 'imagenet-22k-id2label.json' else: __lowerCamelCase = 'imagenet-1k-id2label.json' __lowerCamelCase = json.load(open(hf_hub_download(UpperCamelCase__ , UpperCamelCase__ , repo_type='dataset' ) , 'r' ) ) __lowerCamelCase = {int(UpperCamelCase__ ): v for k, v in idalabel.items()} __lowerCamelCase = {v: k for k, v in idalabel.items()} __lowerCamelCase = FocalNetConfig( embed_dim=UpperCamelCase__ , depths=UpperCamelCase__ , focal_levels=UpperCamelCase__ , focal_windows=UpperCamelCase__ , use_conv_embed=UpperCamelCase__ , idalabel=UpperCamelCase__ , labelaid=UpperCamelCase__ , use_post_layernorm=UpperCamelCase__ , use_layerscale=UpperCamelCase__ , ) return config def lowerCamelCase_ ( UpperCamelCase__ : Any ) -> str: """simple docstring""" if "patch_embed.proj" in name: __lowerCamelCase = name.replace('patch_embed.proj' , 'embeddings.patch_embeddings.projection' ) if "patch_embed.norm" in name: __lowerCamelCase = name.replace('patch_embed.norm' , 'embeddings.norm' ) if "layers" in name: __lowerCamelCase = 'encoder.' + name if "encoder.layers" in name: __lowerCamelCase = name.replace('encoder.layers' , 'encoder.stages' ) if "downsample.proj" in name: __lowerCamelCase = name.replace('downsample.proj' , 'downsample.projection' ) if "blocks" in name: __lowerCamelCase = name.replace('blocks' , 'layers' ) if "modulation.f.weight" in name or "modulation.f.bias" in name: __lowerCamelCase = name.replace('modulation.f' , 'modulation.projection_in' ) if "modulation.h.weight" in name or "modulation.h.bias" in name: __lowerCamelCase = name.replace('modulation.h' , 'modulation.projection_context' ) if "modulation.proj.weight" in name or "modulation.proj.bias" in name: __lowerCamelCase = name.replace('modulation.proj' , 'modulation.projection_out' ) if name == "norm.weight": __lowerCamelCase = 'layernorm.weight' if name == "norm.bias": __lowerCamelCase = 'layernorm.bias' if "head" in name: __lowerCamelCase = name.replace('head' , 'classifier' ) else: __lowerCamelCase = 'focalnet.' + name return name def lowerCamelCase_ ( UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : Tuple=False ) -> Dict: """simple docstring""" __lowerCamelCase = { 'focalnet-tiny': 'https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_tiny_srf.pth', 'focalnet-tiny-lrf': 'https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_tiny_lrf.pth', 'focalnet-small': 'https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_small_srf.pth', 'focalnet-small-lrf': 'https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_small_lrf.pth', 'focalnet-base': 'https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_base_srf.pth', 'focalnet-base-lrf': 'https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_base_lrf.pth', 'focalnet-large-lrf-fl3': 'https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_large_lrf_384.pth', 'focalnet-large-lrf-fl4': 'https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_large_lrf_384_fl4.pth', 'focalnet-xlarge-lrf-fl3': 'https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_xlarge_lrf_384.pth', 'focalnet-xlarge-lrf-fl4': 'https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_xlarge_lrf_384_fl4.pth', } # fmt: on __lowerCamelCase = model_name_to_url[model_name] print('Checkpoint URL: ' , UpperCamelCase__ ) __lowerCamelCase = torch.hub.load_state_dict_from_url(UpperCamelCase__ , map_location='cpu' )['model'] # rename keys for key in state_dict.copy().keys(): __lowerCamelCase = state_dict.pop(UpperCamelCase__ ) __lowerCamelCase = val __lowerCamelCase = get_focalnet_config(UpperCamelCase__ ) __lowerCamelCase = FocalNetForImageClassification(UpperCamelCase__ ) model.eval() # load state dict model.load_state_dict(UpperCamelCase__ ) # verify conversion __lowerCamelCase = 'http://images.cocodataset.org/val2017/000000039769.jpg' __lowerCamelCase = BitImageProcessor( do_resize=UpperCamelCase__ , size={'shortest_edge': 256} , resample=PILImageResampling.BILINEAR , do_center_crop=UpperCamelCase__ , crop_size=224 , do_normalize=UpperCamelCase__ , image_mean=UpperCamelCase__ , image_std=UpperCamelCase__ , ) __lowerCamelCase = Image.open(requests.get(UpperCamelCase__ , stream=UpperCamelCase__ ).raw ) __lowerCamelCase = processor(images=UpperCamelCase__ , return_tensors='pt' ) __lowerCamelCase = transforms.Compose( [ transforms.Resize(256 ), transforms.CenterCrop(224 ), transforms.ToTensor(), transforms.Normalize(mean=[0.4_85, 0.4_56, 0.4_06] , std=[0.2_29, 0.2_24, 0.2_25] ), ] ) __lowerCamelCase = image_transforms(UpperCamelCase__ ).unsqueeze(0 ) # verify pixel_values assert torch.allclose(inputs.pixel_values , UpperCamelCase__ , atol=1E-4 ) __lowerCamelCase = model(**UpperCamelCase__ ) __lowerCamelCase = outputs.logits.argmax(-1 ).item() print('Predicted class:' , model.config.idalabel[predicted_class_idx] ) print('First values of logits:' , outputs.logits[0, :3] ) if model_name == "focalnet-tiny": __lowerCamelCase = torch.tensor([0.21_66, -0.43_68, 0.21_91] ) elif model_name == "focalnet-tiny-lrf": __lowerCamelCase = torch.tensor([1.16_69, 0.01_25, -0.16_95] ) elif model_name == "focalnet-small": __lowerCamelCase = torch.tensor([0.49_17, -0.04_30, 0.13_41] ) elif model_name == "focalnet-small-lrf": __lowerCamelCase = torch.tensor([-0.25_88, -0.53_42, -0.23_31] ) elif model_name == "focalnet-base": __lowerCamelCase = torch.tensor([-0.16_55, -0.40_90, -0.17_30] ) elif model_name == "focalnet-base-lrf": __lowerCamelCase = torch.tensor([0.53_06, -0.04_83, -0.39_28] ) assert torch.allclose(outputs.logits[0, :3] , UpperCamelCase__ , atol=1E-4 ) print('Looks ok!' ) if pytorch_dump_folder_path is not None: print(F"""Saving model and processor of {model_name} to {pytorch_dump_folder_path}""" ) model.save_pretrained(UpperCamelCase__ ) processor.save_pretrained(UpperCamelCase__ ) if push_to_hub: print(F"""Pushing model and processor of {model_name} to the hub...""" ) model.push_to_hub(F"""{model_name}""" ) processor.push_to_hub(F"""{model_name}""" ) if __name__ == "__main__": __A = argparse.ArgumentParser() # Required parameters parser.add_argument( "--model_name", default="focalnet-tiny", type=str, help="Name of the FocalNet model you'd like to convert.", ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model directory." ) parser.add_argument( "--push_to_hub", action="store_true", help="Whether to push the model and processor to the hub.", ) __A = parser.parse_args() convert_focalnet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
348
0
'''simple docstring''' import argparse import gdown import numpy as np import torch from huggingface_hub import hf_hub_download from transformers import ( CLIPTokenizer, CLIPTokenizerFast, VideoMAEImageProcessor, XCLIPConfig, XCLIPModel, XCLIPProcessor, XCLIPTextConfig, XCLIPVisionConfig, ) def a__ ( lowerCAmelCase__ , lowerCAmelCase__ ) -> Optional[Any]: UpperCAmelCase__ : Dict = XCLIPTextConfig() # derive patch size from model name UpperCAmelCase__ : Any = model_name.find('''patch''' ) UpperCAmelCase__ : str = int(model_name[start_idx + len('''patch''' ) : start_idx + len('''patch''' ) + 2] ) UpperCAmelCase__ : str = XCLIPVisionConfig(patch_size=lowerCAmelCase__ , num_frames=lowerCAmelCase__ ) if "large" in model_name: UpperCAmelCase__ : Optional[Any] = 7_68 UpperCAmelCase__ : Any = 30_72 UpperCAmelCase__ : str = 12 UpperCAmelCase__ : List[str] = 10_24 UpperCAmelCase__ : Union[str, Any] = 40_96 UpperCAmelCase__ : str = 16 UpperCAmelCase__ : str = 24 UpperCAmelCase__ : List[str] = 7_68 UpperCAmelCase__ : Optional[Any] = 30_72 if model_name == "xclip-large-patch14-16-frames": UpperCAmelCase__ : Tuple = 3_36 UpperCAmelCase__ : Tuple = XCLIPConfig.from_text_vision_configs(lowerCAmelCase__ , lowerCAmelCase__ ) if "large" in model_name: UpperCAmelCase__ : Tuple = 7_68 return config def a__ ( lowerCAmelCase__ ) -> Any: # text encoder if name == "token_embedding.weight": UpperCAmelCase__ : str = name.replace('''token_embedding.weight''' , '''text_model.embeddings.token_embedding.weight''' ) if name == "positional_embedding": UpperCAmelCase__ : Optional[Any] = name.replace('''positional_embedding''' , '''text_model.embeddings.position_embedding.weight''' ) if "ln_1" in name: UpperCAmelCase__ : Any = name.replace('''ln_1''' , '''layer_norm1''' ) if "ln_2" in name: UpperCAmelCase__ : Tuple = name.replace('''ln_2''' , '''layer_norm2''' ) if "c_fc" in name: UpperCAmelCase__ : Tuple = name.replace('''c_fc''' , '''fc1''' ) if "c_proj" in name: UpperCAmelCase__ : int = name.replace('''c_proj''' , '''fc2''' ) if name.startswith('''transformer.resblocks''' ): UpperCAmelCase__ : List[Any] = name.replace('''transformer.resblocks''' , '''text_model.encoder.layers''' ) if "attn.out_proj" in name and "message" not in name: UpperCAmelCase__ : Optional[int] = name.replace('''attn.out_proj''' , '''self_attn.out_proj''' ) if "ln_final" in name: UpperCAmelCase__ : List[str] = name.replace('''ln_final''' , '''text_model.final_layer_norm''' ) # visual encoder if name == "visual.class_embedding": UpperCAmelCase__ : List[str] = name.replace('''visual.class_embedding''' , '''vision_model.embeddings.class_embedding''' ) if name == "visual.positional_embedding": UpperCAmelCase__ : Any = name.replace('''visual.positional_embedding''' , '''vision_model.embeddings.position_embedding.weight''' ) if name.startswith('''visual.transformer.resblocks''' ): UpperCAmelCase__ : int = name.replace('''visual.transformer.resblocks''' , '''vision_model.encoder.layers''' ) if "visual.conv1" in name: UpperCAmelCase__ : List[Any] = name.replace('''visual.conv1''' , '''vision_model.embeddings.patch_embedding''' ) if "visual.ln_pre" in name: UpperCAmelCase__ : str = name.replace('''visual.ln_pre''' , '''vision_model.pre_layernorm''' ) if "visual.ln_post" in name: UpperCAmelCase__ : List[Any] = name.replace('''visual.ln_post''' , '''vision_model.post_layernorm''' ) if "visual.proj" in name: UpperCAmelCase__ : List[Any] = name.replace('''visual.proj''' , '''visual_projection.weight''' ) if "text_projection" in name: UpperCAmelCase__ : List[Any] = name.replace('''text_projection''' , '''text_projection.weight''' ) # things on top if "prompts_visual_proj" in name: UpperCAmelCase__ : List[Any] = name.replace('''prompts_visual_proj''' , '''prompts_visual_projection''' ) if "prompts_visual_ln" in name: UpperCAmelCase__ : Optional[int] = name.replace('''prompts_visual_ln''' , '''prompts_visual_layernorm''' ) # mit if name == "mit.positional_embedding": UpperCAmelCase__ : str = name.replace('''positional''' , '''position''' ) if name.startswith('''mit.resblocks''' ): UpperCAmelCase__ : List[str] = name.replace('''mit.resblocks''' , '''mit.encoder.layers''' ) # prompts generator if name.startswith('''prompts_generator.norm''' ): UpperCAmelCase__ : str = name.replace('''prompts_generator.norm''' , '''prompts_generator.layernorm''' ) return name def a__ ( lowerCAmelCase__ , lowerCAmelCase__ ) -> List[str]: for key in orig_state_dict.copy().keys(): UpperCAmelCase__ : List[str] = orig_state_dict.pop(lowerCAmelCase__ ) if "attn.in_proj" in key: UpperCAmelCase__ : Optional[Any] = key.split('''.''' ) if key.startswith('''visual''' ): UpperCAmelCase__ : int = key_split[3] UpperCAmelCase__ : List[Any] = config.vision_config.hidden_size if "message_attn" in key: if "weight" in key: UpperCAmelCase__ : List[Any] = val[ :dim, : ] UpperCAmelCase__ : Optional[int] = val[ dim : dim * 2, : ] UpperCAmelCase__ : Optional[int] = val[ -dim:, : ] else: UpperCAmelCase__ : List[Any] = val[ :dim ] UpperCAmelCase__ : Union[str, Any] = val[ dim : dim * 2 ] UpperCAmelCase__ : Optional[Any] = val[ -dim: ] else: if "weight" in key: UpperCAmelCase__ : Tuple = val[ :dim, : ] UpperCAmelCase__ : Union[str, Any] = val[ dim : dim * 2, : ] UpperCAmelCase__ : Any = val[ -dim:, : ] else: UpperCAmelCase__ : Any = val[:dim] UpperCAmelCase__ : List[str] = val[ dim : dim * 2 ] UpperCAmelCase__ : Optional[int] = val[-dim:] elif key.startswith('''mit''' ): UpperCAmelCase__ : Any = key_split[2] UpperCAmelCase__ : List[Any] = config.vision_config.mit_hidden_size if "weight" in key: UpperCAmelCase__ : Dict = val[:dim, :] UpperCAmelCase__ : List[Any] = val[dim : dim * 2, :] UpperCAmelCase__ : Dict = val[-dim:, :] else: UpperCAmelCase__ : List[Any] = val[:dim] UpperCAmelCase__ : Any = val[dim : dim * 2] UpperCAmelCase__ : Any = val[-dim:] else: UpperCAmelCase__ : List[str] = key_split[2] UpperCAmelCase__ : int = config.text_config.hidden_size if "weight" in key: UpperCAmelCase__ : str = val[:dim, :] UpperCAmelCase__ : str = val[ dim : dim * 2, : ] UpperCAmelCase__ : Any = val[-dim:, :] else: UpperCAmelCase__ : int = val[:dim] UpperCAmelCase__ : List[str] = val[ dim : dim * 2 ] UpperCAmelCase__ : Tuple = val[-dim:] else: UpperCAmelCase__ : Any = rename_key(lowerCAmelCase__ ) if new_key_name in ["visual_projection.weight", "text_projection.weight"]: UpperCAmelCase__ : Optional[int] = val.T UpperCAmelCase__ : List[Any] = val return orig_state_dict def a__ ( lowerCAmelCase__ ) -> Tuple: if num_frames == 8: UpperCAmelCase__ : Dict = '''eating_spaghetti_8_frames.npy''' elif num_frames == 16: UpperCAmelCase__ : Optional[int] = '''eating_spaghetti.npy''' elif num_frames == 32: UpperCAmelCase__ : List[Any] = '''eating_spaghetti_32_frames.npy''' UpperCAmelCase__ : List[str] = hf_hub_download( repo_id='''hf-internal-testing/spaghetti-video''' , filename=lowerCAmelCase__ , repo_type='''dataset''' , ) UpperCAmelCase__ : Optional[Any] = np.load(lowerCAmelCase__ ) return list(lowerCAmelCase__ ) def a__ ( lowerCAmelCase__ , lowerCAmelCase__=None , lowerCAmelCase__=False ) -> Tuple: UpperCAmelCase__ : Optional[int] = { # fully supervised kinetics-400 checkpoints '''xclip-base-patch32''': '''https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k400_32_8.pth''', '''xclip-base-patch32-16-frames''': ( '''https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k400_32_16.pth''' ), '''xclip-base-patch16''': '''https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k400_16_8.pth''', '''xclip-base-patch16-16-frames''': ( '''https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k400_16_16.pth''' ), '''xclip-large-patch14''': '''https://drive.google.com/u/0/uc?id=1NUOImq0o5DlQTST17iIP3vG7DgmHQuCx&amp;export=download&amp;confirm=t&amp;uuid=b26caedc-88e2-473e-830a-9d158b653cdb''', '''xclip-large-patch14-16-frames''': '''https://drive.google.com/u/0/uc?id=1FOYgnJc097OJ4lGwtRCCydQyVPJEOH7d&amp;export=download&amp;confirm=t&amp;uuid=538fa810-e671-4050-b385-9a623f89804f''', # fully supervised kinetics-600 checkpoints '''xclip-base-patch16-kinetics-600''': ( '''https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k600_16_8.pth''' ), '''xclip-base-patch16-kinetics-600-16-frames''': ( '''https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k600_16_16.pth''' ), '''xclip-large-patch14-kinetics-600''': '''https://drive.google.com/u/0/uc?id=1FV8C1INuM91sLAN4ImjzePLIlpMSihwV&amp;export=download&amp;confirm=t&amp;uuid=141d4977-4a65-44ae-864f-4b0c19f838be''', # few shot '''xclip-base-patch16-hmdb-2-shot''': ( '''https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_hmdb_2.pth''' ), '''xclip-base-patch16-hmdb-4-shot''': ( '''https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_hmdb_4.pth''' ), '''xclip-base-patch16-hmdb-8-shot''': ( '''https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_hmdb_8.pth''' ), '''xclip-base-patch16-hmdb-16-shot''': ( '''https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_hmdb_16.pth''' ), '''xclip-base-patch16-ucf-2-shot''': ( '''https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_ucf_2.pth''' ), '''xclip-base-patch16-ucf-4-shot''': ( '''https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_ucf_4.pth''' ), '''xclip-base-patch16-ucf-8-shot''': ( '''https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_ucf_8.pth''' ), '''xclip-base-patch16-ucf-16-shot''': ( '''https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_ucf_16.pth''' ), # zero shot '''xclip-base-patch16-zero-shot''': '''https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/zero.pth''', } UpperCAmelCase__ : Optional[int] = model_to_url[model_name] UpperCAmelCase__ : Dict = 8 if "16-frames" in model_name: UpperCAmelCase__ : Tuple = 16 elif "shot" in model_name: UpperCAmelCase__ : Optional[int] = 32 UpperCAmelCase__ : Tuple = get_xclip_config(lowerCAmelCase__ , lowerCAmelCase__ ) UpperCAmelCase__ : List[str] = XCLIPModel(lowerCAmelCase__ ) model.eval() if "drive" in checkpoint_url: UpperCAmelCase__ : Union[str, Any] = '''pytorch_model.bin''' gdown.cached_download(lowerCAmelCase__ , lowerCAmelCase__ , quiet=lowerCAmelCase__ ) UpperCAmelCase__ : List[str] = torch.load(lowerCAmelCase__ , map_location='''cpu''' )['''model'''] else: UpperCAmelCase__ : Union[str, Any] = torch.hub.load_state_dict_from_url(lowerCAmelCase__ )['''model'''] UpperCAmelCase__ : Dict = convert_state_dict(lowerCAmelCase__ , lowerCAmelCase__ ) UpperCAmelCase__ : str = XCLIPModel(lowerCAmelCase__ ) UpperCAmelCase__ , UpperCAmelCase__ : Dict = model.load_state_dict(lowerCAmelCase__ , strict=lowerCAmelCase__ ) assert missing_keys == ["text_model.embeddings.position_ids", "vision_model.embeddings.position_ids"] model.eval() UpperCAmelCase__ : Dict = 3_36 if model_name == '''xclip-large-patch14-16-frames''' else 2_24 UpperCAmelCase__ : Any = VideoMAEImageProcessor(size=lowerCAmelCase__ ) UpperCAmelCase__ : Any = CLIPTokenizer.from_pretrained('''openai/clip-vit-base-patch32''' ) UpperCAmelCase__ : str = CLIPTokenizerFast.from_pretrained('''openai/clip-vit-base-patch32''' ) UpperCAmelCase__ : Union[str, Any] = XCLIPProcessor(image_processor=lowerCAmelCase__ , tokenizer=lowerCAmelCase__ ) UpperCAmelCase__ : Dict = prepare_video(lowerCAmelCase__ ) UpperCAmelCase__ : int = processor( text=['''playing sports''', '''eating spaghetti''', '''go shopping'''] , videos=lowerCAmelCase__ , return_tensors='''pt''' , padding=lowerCAmelCase__ ) print('''Shape of pixel values:''' , inputs.pixel_values.shape ) with torch.no_grad(): UpperCAmelCase__ : Optional[Any] = model(**lowerCAmelCase__ ) # Verify outputs UpperCAmelCase__ : List[str] = outputs.logits_per_video UpperCAmelCase__ : int = logits_per_video.softmax(dim=1 ) print('''Probs:''' , lowerCAmelCase__ ) # kinetics-400 if model_name == "xclip-base-patch32": UpperCAmelCase__ : List[Any] = torch.tensor([[0.0_0_1_9, 0.9_9_5_1, 0.0_0_3_0]] ) elif model_name == "xclip-base-patch32-16-frames": UpperCAmelCase__ : str = torch.tensor([[7.0999E-04, 9.9883E-01, 4.5580E-04]] ) elif model_name == "xclip-base-patch16": UpperCAmelCase__ : List[str] = torch.tensor([[0.0_0_8_3, 0.9_6_8_1, 0.0_2_3_6]] ) elif model_name == "xclip-base-patch16-16-frames": UpperCAmelCase__ : Union[str, Any] = torch.tensor([[7.6937E-04, 9.9728E-01, 1.9473E-03]] ) elif model_name == "xclip-large-patch14": UpperCAmelCase__ : List[Any] = torch.tensor([[0.0_0_6_2, 0.9_8_6_4, 0.0_0_7_5]] ) elif model_name == "xclip-large-patch14-16-frames": UpperCAmelCase__ : Dict = torch.tensor([[3.3877E-04, 9.9937E-01, 2.8888E-04]] ) # kinetics-600 elif model_name == "xclip-base-patch16-kinetics-600": UpperCAmelCase__ : List[str] = torch.tensor([[0.0_5_5_5, 0.8_9_1_4, 0.0_5_3_1]] ) elif model_name == "xclip-base-patch16-kinetics-600-16-frames": UpperCAmelCase__ : Union[str, Any] = torch.tensor([[3.8554E-04, 9.9929E-01, 3.2754E-04]] ) elif model_name == "xclip-large-patch14-kinetics-600": UpperCAmelCase__ : Any = torch.tensor([[0.0_0_3_6, 0.9_9_2_0, 0.0_0_4_5]] ) # few shot elif model_name == "xclip-base-patch16-hmdb-2-shot": UpperCAmelCase__ : Union[str, Any] = torch.tensor([[7.1890E-06, 9.9994E-01, 5.6559E-05]] ) elif model_name == "xclip-base-patch16-hmdb-4-shot": UpperCAmelCase__ : List[Any] = torch.tensor([[1.0320E-05, 9.9993E-01, 6.2435E-05]] ) elif model_name == "xclip-base-patch16-hmdb-8-shot": UpperCAmelCase__ : Optional[int] = torch.tensor([[4.1377E-06, 9.9990E-01, 9.8386E-05]] ) elif model_name == "xclip-base-patch16-hmdb-16-shot": UpperCAmelCase__ : List[Any] = torch.tensor([[4.1347E-05, 9.9962E-01, 3.3411E-04]] ) elif model_name == "xclip-base-patch16-ucf-2-shot": UpperCAmelCase__ : List[Any] = torch.tensor([[8.5857E-05, 9.9928E-01, 6.3291E-04]] ) elif model_name == "xclip-base-patch16-ucf-4-shot": UpperCAmelCase__ : str = torch.tensor([[8.5857E-05, 9.9928E-01, 6.3291E-04]] ) elif model_name == "xclip-base-patch16-ucf-8-shot": UpperCAmelCase__ : List[str] = torch.tensor([[0.0_0_2_7, 0.9_9_0_4, 0.0_0_7_0]] ) elif model_name == "xclip-base-patch16-ucf-16-shot": UpperCAmelCase__ : str = torch.tensor([[9.8219E-04, 9.9593E-01, 3.0863E-03]] ) # zero shot elif model_name == "xclip-base-patch16-zero-shot": UpperCAmelCase__ : int = torch.tensor([[3.5082E-04, 9.9785E-01, 1.7966E-03]] ) else: raise ValueError(F"""Model name {model_name} not supported""" ) assert torch.allclose(lowerCAmelCase__ , lowerCAmelCase__ , atol=1E-3 ) 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(lowerCAmelCase__ ) if push_to_hub: print('''Pushing model, processor and slow tokenizer files to the hub...''' ) model.push_to_hub(lowerCAmelCase__ , organization='''nielsr''' ) processor.push_to_hub(lowerCAmelCase__ , organization='''nielsr''' ) slow_tokenizer.push_to_hub(lowerCAmelCase__ , organization='''nielsr''' ) if __name__ == "__main__": UpperCamelCase__ = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default='''xclip-base-patch32''', type=str, help='''Name of the model.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) parser.add_argument( '''--push_to_hub''', action='''store_true''', help='''Whether or not to push the converted model to the 🤗 hub.''' ) UpperCamelCase__ = parser.parse_args() convert_xclip_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
181
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) UpperCamelCase__ = { '''configuration_rembert''': ['''REMBERT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''RemBertConfig''', '''RemBertOnnxConfig'''] } try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ = ['''RemBertTokenizer'''] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ = ['''RemBertTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ = [ '''REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''RemBertForCausalLM''', '''RemBertForMaskedLM''', '''RemBertForMultipleChoice''', '''RemBertForQuestionAnswering''', '''RemBertForSequenceClassification''', '''RemBertForTokenClassification''', '''RemBertLayer''', '''RemBertModel''', '''RemBertPreTrainedModel''', '''load_tf_weights_in_rembert''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ = [ '''TF_REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFRemBertForCausalLM''', '''TFRemBertForMaskedLM''', '''TFRemBertForMultipleChoice''', '''TFRemBertForQuestionAnswering''', '''TFRemBertForSequenceClassification''', '''TFRemBertForTokenClassification''', '''TFRemBertLayer''', '''TFRemBertModel''', '''TFRemBertPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_rembert import REMBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, RemBertConfig, RemBertOnnxConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_rembert import RemBertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_rembert_fast import RemBertTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_rembert import ( REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST, RemBertForCausalLM, RemBertForMaskedLM, RemBertForMultipleChoice, RemBertForQuestionAnswering, RemBertForSequenceClassification, RemBertForTokenClassification, RemBertLayer, RemBertModel, RemBertPreTrainedModel, load_tf_weights_in_rembert, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_rembert import ( TF_REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST, TFRemBertForCausalLM, TFRemBertForMaskedLM, TFRemBertForMultipleChoice, TFRemBertForQuestionAnswering, TFRemBertForSequenceClassification, TFRemBertForTokenClassification, TFRemBertLayer, TFRemBertModel, TFRemBertPreTrainedModel, ) else: import sys UpperCamelCase__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
181
1
_lowerCamelCase : Optional[int] = { 'A': ['B', 'C', 'E'], 'B': ['A', 'D', 'E'], 'C': ['A', 'F', 'G'], 'D': ['B'], 'E': ['A', 'B', 'D'], 'F': ['C'], 'G': ['C'], } def SCREAMING_SNAKE_CASE ( lowercase_ , lowercase_ , lowercase_ ) -> List[str]: """simple docstring""" A__ = set() # keep track of all the paths to be checked A__ = [[start]] # return path if start is goal if start == goal: return [start] # keeps looping until all possible paths have been checked while queue: # pop the first path from the queue A__ = queue.pop(0 ) # get the last node from the path A__ = path[-1] if node not in explored: A__ = graph[node] # go through all neighbour nodes, construct a new path and # push it into the queue for neighbour in neighbours: A__ = list(lowercase__ ) new_path.append(lowercase__ ) queue.append(lowercase__ ) # return path if neighbour is goal if neighbour == goal: return new_path # mark node as explored explored.add(lowercase__ ) # in case there's no path between the 2 nodes return [] def SCREAMING_SNAKE_CASE ( lowercase_ , lowercase_ , lowercase_ ) -> List[str]: """simple docstring""" if not graph or start not in graph or target not in graph: return -1 if start == target: return 0 A__ = [start] A__ = set(lowercase__ ) # Keep tab on distances from `start` node. A__ = {start: 0, target: -1} while queue: A__ = queue.pop(0 ) if node == target: A__ = ( dist[node] if dist[target] == -1 else min(dist[target] , dist[node] ) ) for adjacent in graph[node]: if adjacent not in visited: visited.add(lowercase__ ) queue.append(lowercase__ ) A__ = dist[node] + 1 return dist[target] if __name__ == "__main__": print(bfs_shortest_path(demo_graph, """G""", """D""")) # returns ['G', 'C', 'A', 'B', 'D'] print(bfs_shortest_path_distance(demo_graph, """G""", """D""")) # returns 4
363
def SCREAMING_SNAKE_CASE ( lowercase_ = 50 ) -> int: """simple docstring""" A__ = [1] * (length + 1) for row_length in range(3 , length + 1 ): for block_length in range(3 , row_length + 1 ): for block_start in range(row_length - block_length ): ways_number[row_length] += ways_number[ row_length - block_start - block_length - 1 ] ways_number[row_length] += 1 return ways_number[length] if __name__ == "__main__": print(F'''{solution() = }''')
231
0
import warnings from ...utils import logging from .image_processing_perceiver import PerceiverImageProcessor UpperCAmelCase__ = logging.get_logger(__name__) class lowercase_ ( lowercase ): '''simple docstring''' def __init__( self : Optional[int] , *__UpperCAmelCase : Union[str, Any] , **__UpperCAmelCase : Any ) ->None: """simple docstring""" warnings.warn( '''The class PerceiverFeatureExtractor is deprecated and will be removed in version 5 of Transformers.''' ''' Please use PerceiverImageProcessor instead.''' , __UpperCAmelCase , ) super().__init__(*__UpperCAmelCase , **__UpperCAmelCase )
0
import math def _a ( a :int ) -> list: a = [True] * n a = False a = False a = True for i in range(3 , int(n**0.5 + 1 ) , 2 ): a = i * 2 while index < n: a = False a = index + i a = [2] for i in range(3 , a , 2 ): if is_prime[i]: primes.append(a ) return primes def _a ( a :int = 999_966_663_333 ) -> int: a = math.floor(math.sqrt(a ) ) + 100 a = prime_sieve(a ) a = 0 a = 0 a = primes[prime_index] while (last_prime**2) <= limit: a = primes[prime_index + 1] a = last_prime**2 a = next_prime**2 # Get numbers divisible by lps(current) a = lower_bound + last_prime while upper_bound > current <= limit: matches_sum += current current += last_prime # Reset the upper_bound while (upper_bound - next_prime) > limit: upper_bound -= next_prime # Add the numbers divisible by ups(current) a = upper_bound - next_prime while current > lower_bound: matches_sum += current current -= next_prime # Remove the numbers divisible by both ups and lps a = 0 while upper_bound > current <= limit: if current <= lower_bound: # Increment the current number current += last_prime * next_prime continue if current > limit: break # Remove twice since it was added by both ups and lps matches_sum -= current * 2 # Increment the current number current += last_prime * next_prime # Setup for next pair a = next_prime prime_index += 1 return matches_sum if __name__ == "__main__": print(solution())
0
1
import argparse import dataclasses import json import logging import os import shutil from typing import List, Optional import datasets from accelerate import Accelerator from datasets import load_dataset from finetuning import finetune from tqdm.auto import tqdm import transformers from transformers import AutoConfig, set_seed from transformers.trainer_utils import IntervalStrategy UpperCamelCase_ = logging.getLogger(__name__) UpperCamelCase_ = '''pytorch_model.bin''' @dataclasses.dataclass class _snake_case : '''simple docstring''' A__ : str = dataclasses.field( metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models."} ) A__ : Optional[str] = dataclasses.field( default=__snake_case , metadata={"help": "Where do you want to store the pretrained models downloaded from huggingface.co."} , ) @dataclasses.dataclass class _snake_case : '''simple docstring''' A__ : str = dataclasses.field(metadata={"help": "A csv or a json file containing the training data."} ) A__ : str = dataclasses.field(metadata={"help": "A csv or a json file containing the data to predict on."} ) A__ : Optional[str] = dataclasses.field( default=__snake_case , metadata={"help": "A csv or a json file containing the validation data."} ) A__ : Optional[str] = dataclasses.field( default=__snake_case , metadata={"help": "The name of the task to train on."} , ) A__ : Optional[List[str]] = dataclasses.field( default=__snake_case , metadata={"help": "The list of labels for the task."} ) @dataclasses.dataclass class _snake_case : '''simple docstring''' A__ : str = dataclasses.field( metadata={"help": "The output directory where the model predictions and checkpoints will be written."} ) A__ : Optional[str] = dataclasses.field( default="accuracy" , metadata={"help": "The evaluation metric used for the task."} ) A__ : Optional[str] = dataclasses.field( default="no" , metadata={ "help": "The evaluation strategy to adopt during training. Possible values are: [\"no\", \"step\", \"epoch]" } , ) A__ : Optional[int] = dataclasses.field( default=10 , metadata={"help": "Number of evaluation calls with no improvement after which training will be stopped."} , ) A__ : Optional[float] = dataclasses.field( default=0.0 , metadata={ "help": "How much the specified evaluation metric must improve to satisfy early stopping conditions." } , ) A__ : Optional[bool] = dataclasses.field( default=__snake_case , metadata={"help": "Whether to filter the pseudo-labeled data based on the confidence score."} , ) A__ : Optional[bool] = dataclasses.field( default=__snake_case , metadata={"help": "Whether to filter the pseudo-labeled data based on the validation performance."} , ) A__ : Optional[bool] = dataclasses.field( default=__snake_case , metadata={"help": "Whether to fine-tune on labeled data after pseudo training."} , ) A__ : Optional[float] = dataclasses.field( default=0.0 , metadata={"help": "Confidence threshold for pseudo-labeled data filtering."} , ) A__ : Optional[int] = dataclasses.field( default=100 , metadata={"help": "Number of evaluation calls with no improvement after which training will be stopped."} , ) A__ : Optional[int] = dataclasses.field( default=__snake_case , metadata={"help": "Random seed for initialization."} , ) def lowerCamelCase_ ( _a : str , _a : List[Any] , _a : List[Any] , _a : Dict , _a : int , _a : Tuple ): '''simple docstring''' UpperCAmelCase_ : Tuple = datasets.concatenate_datasets([infer_input, infer_output] , axis=1 ) if args.do_filter_by_confidence: UpperCAmelCase_ : List[str] = dataset.filter(lambda _a : example["probability"] > args.confidence_threshold ) if args.do_filter_by_val_performance: assert eval_result >= 0.0 and eval_result <= 1.0 UpperCAmelCase_ : List[str] = int(eval_result * len(_a ) ) print(_a ) UpperCAmelCase_ : int = dataset.sort("""probability""" , reverse=_a ) UpperCAmelCase_ : Optional[int] = dataset.select(range(_a ) ) UpperCAmelCase_ : List[str] = dataset.remove_columns(["""label""", """probability"""] ) UpperCAmelCase_ : Optional[Any] = dataset.rename_column("""prediction""" , """label""" ) UpperCAmelCase_ : Union[str, Any] = dataset.map(lambda _a : {"label": idalabel[example["label"]]} ) UpperCAmelCase_ : int = dataset.shuffle(seed=args.seed ) UpperCAmelCase_ : int = os.path.join(_a , F'''train_pseudo.{args.data_file_extension}''' ) if args.data_file_extension == "csv": dataset.to_csv(_a , index=_a ) else: dataset.to_json(_a ) def lowerCamelCase_ ( _a : Any , _a : int , _a : Dict , _a : List[Any] , **_a : int ): '''simple docstring''' UpperCAmelCase_ : Optional[int] = Accelerator() # Make one log on every process with the configuration for debugging. logging.basicConfig( format="""%(asctime)s - %(levelname)s - %(name)s - %(message)s""" , datefmt="""%m/%d/%Y %H:%M:%S""" , level=logging.INFO , ) logger.info(accelerator.state ) # Setup logging, we only want one process per machine to log things on the # screen. accelerator.is_local_main_process is only True for one process per # machine. logger.setLevel(logging.INFO if accelerator.is_local_main_process else logging.ERROR ) if accelerator.is_local_main_process: datasets.utils.logging.set_verbosity_warning() transformers.utils.logging.set_verbosity_info() else: datasets.utils.logging.set_verbosity_error() transformers.utils.logging.set_verbosity_error() UpperCAmelCase_ : Tuple = STModelArguments(model_name_or_path=_a ) UpperCAmelCase_ : str = STDataArguments(train_file=_a , infer_file=_a ) UpperCAmelCase_ : Optional[Any] = STTrainingArguments(output_dir=_a ) UpperCAmelCase_ : Optional[Any] = argparse.Namespace() for arg_class in (model_args, data_args, training_args): for key, value in vars(_a ).items(): setattr(_a , _a , _a ) for key, value in kwargs.items(): if hasattr(_a , _a ): setattr(_a , _a , _a ) # Sanity checks UpperCAmelCase_ : List[str] = {} UpperCAmelCase_ : Any = None # You need to provide the training data and the data to predict on assert args.train_file is not None assert args.infer_file is not None UpperCAmelCase_ : List[Any] = args.train_file UpperCAmelCase_ : Tuple = args.infer_file if args.evaluation_strategy != IntervalStrategy.NO.value: assert args.eval_file is not None UpperCAmelCase_ : Dict = args.eval_file for key in data_files: UpperCAmelCase_ : List[str] = data_files[key].split(""".""" )[-1] assert extension in ["csv", "json"], F'''`{key}_file` should be a csv or a json file.''' if args.data_file_extension is None: UpperCAmelCase_ : int = extension else: assert extension == args.data_file_extension, F'''`{key}_file` should be a {args.data_file_extension} file`.''' assert ( args.eval_metric in datasets.list_metrics() ), F'''{args.eval_metric} not in the list of supported metrics {datasets.list_metrics()}.''' # If passed along, set the training seed now. if args.seed is not None: set_seed(args.seed ) logger.info("""Creating the initial data directory for self-training...""" ) UpperCAmelCase_ : int = F'''{args.output_dir}/self-train_iter-{{}}'''.format UpperCAmelCase_ : List[Any] = data_dir_format(0 ) if accelerator.is_main_process: if args.output_dir is not None: os.makedirs(args.output_dir , exist_ok=_a ) os.makedirs(_a , exist_ok=_a ) accelerator.wait_for_everyone() UpperCAmelCase_ : Any = None UpperCAmelCase_ : Tuple = None UpperCAmelCase_ : List[str] = 0 UpperCAmelCase_ : List[Any] = False # Show the progress bar UpperCAmelCase_ : List[str] = tqdm(range(args.max_selftrain_iterations ) , disable=not accelerator.is_local_main_process ) # Self-train for iteration in range(0 , int(args.max_selftrain_iterations ) ): UpperCAmelCase_ : Any = data_dir_format(_a ) assert os.path.exists(_a ) # Stage 1: initial fine-tuning for iteration = 0 or pseudo-training for # iteration > 0 UpperCAmelCase_ : List[str] = os.path.join(_a , """stage-1""" ) UpperCAmelCase_ : Optional[int] = { """accelerator""": accelerator, """model_name_or_path""": args.model_name_or_path, """cache_dir""": args.cache_dir, """do_train""": True, """train_file""": data_files["""train"""] if iteration == 0 else data_files["""train_pseudo"""], """do_eval""": True if args.eval_file is not None else False, """eval_file""": data_files["""eval"""], """do_predict""": True, """infer_file""": data_files["""infer"""], """task_name""": args.task_name, """label_list""": args.label_list, """output_dir""": current_output_dir, """eval_metric""": args.eval_metric, """evaluation_strategy""": args.evaluation_strategy, """early_stopping_patience""": args.early_stopping_patience, """early_stopping_threshold""": args.early_stopping_threshold, """seed""": args.seed, } # Add additional training arguments for key, value in kwargs.items(): if key not in arguments_dict and not hasattr(_a , _a ): arguments_dict.update({key: value} ) UpperCAmelCase_ : Any = os.path.join(_a , """best-checkpoint""" , _a ) if os.path.exists(_a ): logger.info( """Found existing model checkpoint at %s. Skipping self-training: iteration: %d, stage: 1.""" , _a , _a , ) else: logger.info("""***** Running self-training: iteration: %d, stage: 1 *****""" , _a ) finetune(**_a ) accelerator.wait_for_everyone() assert os.path.exists(_a ) logger.info("""Self-training job completed: iteration: %d, stage: 1.""" , _a ) if iteration > 0 and args.finetune_on_labeled_data: # Stage 2 (optional): fine-tuning on the original labeled data UpperCAmelCase_ : Dict = os.path.join(_a , """best-checkpoint""" ) UpperCAmelCase_ : str = os.path.join(_a , """stage-2""" ) # Update arguments_dict UpperCAmelCase_ : Union[str, Any] = model_path UpperCAmelCase_ : Dict = data_files["""train"""] UpperCAmelCase_ : List[str] = current_output_dir UpperCAmelCase_ : str = os.path.join(_a , """best-checkpoint""" , _a ) if os.path.exists(_a ): logger.info( """Found existing model checkpoint at %s. Skipping self-training: iteration: %d, stage: 2.""" , _a , _a , ) else: logger.info("""***** Running self-training: iteration: %d, stage: 2 *****""" , _a ) finetune(**_a ) accelerator.wait_for_everyone() assert os.path.exists(_a ) logger.info("""Self-training job completed: iteration: %d, stage: 2.""" , _a ) UpperCAmelCase_ : Optional[Any] = iteration UpperCAmelCase_ : List[str] = data_dir_format(iteration + 1 ) UpperCAmelCase_ : List[str] = AutoConfig.from_pretrained(os.path.join(_a , """best-checkpoint""" ) ) UpperCAmelCase_ : str = config.idalabel UpperCAmelCase_ : Union[str, Any] = os.path.join(_a , """eval_results_best-checkpoint.json""" ) UpperCAmelCase_ : int = os.path.join(_a , """test_results_best-checkpoint.json""" ) assert os.path.exists(_a ) with open(_a , """r""" ) as f: UpperCAmelCase_ : Optional[int] = float(json.load(_a )[args.eval_metric] ) UpperCAmelCase_ : Dict = os.path.join(_a , """infer_output_best-checkpoint.csv""" ) assert os.path.exists(_a ) # Loading the dataset from local csv or json files. UpperCAmelCase_ : Optional[Any] = load_dataset(args.data_file_extension , data_files={"""data""": data_files["""infer"""]} )["""data"""] UpperCAmelCase_ : List[str] = load_dataset("""csv""" , data_files={"""data""": infer_output_file} )["""data"""] if accelerator.is_main_process: os.makedirs(_a , exist_ok=_a ) shutil.copy(_a , os.path.join(_a , F'''eval_results_iter-{iteration}.json''' ) ) if os.path.exists(_a ): shutil.copy(_a , os.path.join(_a , F'''test_results_iter-{iteration}.json''' ) ) create_pseudo_labeled_data(_a , _a , _a , _a , _a , _a ) accelerator.wait_for_everyone() UpperCAmelCase_ : Tuple = os.path.join(_a , F'''train_pseudo.{args.data_file_extension}''' ) if args.evaluation_strategy != IntervalStrategy.NO.value: UpperCAmelCase_ : Optional[Any] = eval_result if best_iteration is None: UpperCAmelCase_ : Optional[int] = new_iteration UpperCAmelCase_ : Union[str, Any] = new_eval_result else: if new_eval_result - best_eval_result > args.early_stopping_threshold: UpperCAmelCase_ : List[str] = new_iteration UpperCAmelCase_ : Union[str, Any] = new_eval_result UpperCAmelCase_ : int = 0 else: if new_eval_result == best_eval_result: UpperCAmelCase_ : Dict = new_iteration UpperCAmelCase_ : Optional[Any] = new_eval_result early_stopping_patience_counter += 1 if early_stopping_patience_counter >= args.early_stopping_patience: UpperCAmelCase_ : List[Any] = True progress_bar.update(1 ) if should_training_stop: break if best_iteration is not None: # Save the best iteration logger.info("""Best iteration: %d""" , _a ) logger.info("""Best evaluation result: %s = %f""" , args.eval_metric , _a ) accelerator.wait_for_everyone() if accelerator.is_main_process: shutil.copy( os.path.join(_a , F'''eval_results_iter-{iteration}.json''' ) , os.path.join(_a , """eval_results_best-iteration.json""" ) , ) else: # Assume that the last iteration is the best logger.info("""Best iteration: %d""" , args.max_selftrain_iterations - 1 ) logger.info("""Best evaluation result: %s = %f""" , args.eval_metric , _a ) accelerator.wait_for_everyone() if accelerator.is_main_process: shutil.copy( os.path.join(_a , F'''eval_results_iter-{args.max_selftrain_iterations - 1}.json''' ) , os.path.join(_a , """eval_results_best-iteration.json""" ) , )
59
from scipy.stats import spearmanr import datasets UpperCamelCase_ = ''' The Spearman rank-order correlation coefficient is a measure of the relationship between two datasets. Like other correlation coefficients, this one varies between -1 and +1 with 0 implying no correlation. Positive correlations imply that as data in dataset x increases, so does data in dataset y. Negative correlations imply that as x increases, y decreases. Correlations of -1 or +1 imply an exact monotonic relationship. Unlike the Pearson correlation, the Spearman correlation does not assume that both datasets are normally distributed. The p-value roughly indicates the probability of an uncorrelated system producing datasets that have a Spearman correlation at least as extreme as the one computed from these datasets. The p-values are not entirely reliable but are probably reasonable for datasets larger than 500 or so. ''' UpperCamelCase_ = ''' Args: predictions (`List[float]`): Predicted labels, as returned by a model. references (`List[float]`): Ground truth labels. return_pvalue (`bool`): If `True`, returns the p-value. If `False`, returns only the spearmanr score. Defaults to `False`. Returns: spearmanr (`float`): Spearman correlation coefficient. p-value (`float`): p-value. **Note**: is only returned if `return_pvalue=True` is input. Examples: Example 1: >>> spearmanr_metric = datasets.load_metric("spearmanr") >>> results = spearmanr_metric.compute(references=[1, 2, 3, 4, 5], predictions=[10, 9, 2.5, 6, 4]) >>> print(results) {\'spearmanr\': -0.7} Example 2: >>> spearmanr_metric = datasets.load_metric("spearmanr") >>> results = spearmanr_metric.compute(references=[1, 2, 3, 4, 5], ... predictions=[10, 9, 2.5, 6, 4], ... return_pvalue=True) >>> print(results[\'spearmanr\']) -0.7 >>> print(round(results[\'spearmanr_pvalue\'], 2)) 0.19 ''' UpperCamelCase_ = R'''\ @book{kokoska2000crc, title={CRC standard probability and statistics tables and formulae}, author={Kokoska, Stephen and Zwillinger, Daniel}, year={2000}, publisher={Crc Press} } @article{2020SciPy-NMeth, author = {Virtanen, Pauli and Gommers, Ralf and Oliphant, Travis E. and Haberland, Matt and Reddy, Tyler and Cournapeau, David and Burovski, Evgeni and Peterson, Pearu and Weckesser, Warren and Bright, Jonathan and {van der Walt}, St{\'e}fan J. and Brett, Matthew and Wilson, Joshua and Millman, K. Jarrod and Mayorov, Nikolay and Nelson, Andrew R. J. and Jones, Eric and Kern, Robert and Larson, Eric and Carey, C J and Polat, {\.I}lhan and Feng, Yu and Moore, Eric W. and {VanderPlas}, Jake and Laxalde, Denis and Perktold, Josef and Cimrman, Robert and Henriksen, Ian and Quintero, E. A. and Harris, Charles R. and Archibald, Anne M. and Ribeiro, Ant{\^o}nio H. and Pedregosa, Fabian and {van Mulbregt}, Paul and {SciPy 1.0 Contributors}}, title = {{{SciPy} 1.0: Fundamental Algorithms for Scientific Computing in Python}}, journal = {Nature Methods}, year = {2020}, volume = {17}, pages = {261--272}, adsurl = {https://rdcu.be/b08Wh}, doi = {10.1038/s41592-019-0686-2}, } ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class _snake_case ( datasets.Metric ): '''simple docstring''' def A__ ( self: int ) -> Tuple: return datasets.MetricInfo( description=_DESCRIPTION ,citation=_CITATION ,inputs_description=_KWARGS_DESCRIPTION ,features=datasets.Features( { """predictions""": datasets.Value("""float""" ), """references""": datasets.Value("""float""" ), } ) ,reference_urls=["""https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.spearmanr.html"""] ,) def A__ ( self: int ,lowerCamelCase_: int ,lowerCamelCase_: List[Any] ,lowerCamelCase_: List[str]=False ) -> Dict: UpperCAmelCase_ : List[str] = spearmanr(lowerCamelCase_ ,lowerCamelCase_ ) if return_pvalue: return {"spearmanr": results[0], "spearmanr_pvalue": results[1]} else: return {"spearmanr": results[0]}
59
1
'''simple docstring''' def UpperCAmelCase__ ( UpperCAmelCase_ : dict ) -> bool: __lowerCamelCase : set[int] = set() # To detect a back edge, keep track of vertices currently in the recursion stack __lowerCamelCase : set[int] = set() return any( node not in visited and depth_first_search(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ ) for node in graph ) def UpperCAmelCase__ ( UpperCAmelCase_ : dict , UpperCAmelCase_ : int , UpperCAmelCase_ : set , UpperCAmelCase_ : set ) -> bool: visited.add(UpperCAmelCase_ ) rec_stk.add(UpperCAmelCase_ ) for node in graph[vertex]: if node not in visited: if depth_first_search(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ ): return True elif node in rec_stk: return True # The node needs to be removed from recursion stack before function ends rec_stk.remove(UpperCAmelCase_ ) return False if __name__ == "__main__": from doctest import testmod testmod()
185
'''simple docstring''' import inspect import jax import jax.lax as lax import jax.numpy as jnp from ..utils import add_start_docstrings from ..utils.logging import get_logger A__ : List[str] = get_logger(__name__) A__ : str = R""" Args: input_ids (`jnp.ndarray` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the vocabulary. Indices can be obtained using [`PreTrainedTokenizer`]. See [`PreTrainedTokenizer.encode`] and [`PreTrainedTokenizer.__call__`] for details. [What are input IDs?](../glossary#input-ids) scores (`jnp.ndarray` of shape `(batch_size, config.vocab_size)`): Prediction scores of a language modeling head. These can be logits for each vocabulary when not using beam search or log softmax for each vocabulary token when using beam search kwargs (`Dict[str, Any]`, *optional*): Additional logits processor specific kwargs. Return: `jnp.ndarray` of shape `(batch_size, config.vocab_size)`: The processed prediction scores. """ class UpperCAmelCase_ : """simple docstring""" @add_start_docstrings(SCREAMING_SNAKE_CASE_ ) def __call__( self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> jnp.ndarray: raise NotImplementedError( f'{self.__class__} is an abstract class. Only classes inheriting this class can be called.' ) class UpperCAmelCase_ : """simple docstring""" @add_start_docstrings(SCREAMING_SNAKE_CASE_ ) def __call__( self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> jnp.ndarray: raise NotImplementedError( f'{self.__class__} is an abstract class. Only classes inheriting this class can be called.' ) class UpperCAmelCase_ (_UpperCAmelCase ): """simple docstring""" @add_start_docstrings(SCREAMING_SNAKE_CASE_ ) def __call__( self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) -> jnp.ndarray: for processor in self: __lowerCamelCase : str = inspect.signature(processor.__call__ ).parameters if len(SCREAMING_SNAKE_CASE_ ) > 3: if not all(arg in kwargs for arg in list(function_args.keys() )[2:] ): raise ValueError( f'Make sure that all the required parameters: {list(function_args.keys() )} for ' f'{processor.__class__} are passed to the logits processor.' ) __lowerCamelCase : Tuple = processor(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) else: __lowerCamelCase : int = processor(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) return scores class UpperCAmelCase_ (_UpperCAmelCase ): """simple docstring""" def __init__( self , SCREAMING_SNAKE_CASE_ ) -> List[Any]: if not isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) or not (temperature > 0): raise ValueError(f'`temperature` has to be a strictly positive float, but is {temperature}' ) __lowerCamelCase : Optional[int] = temperature def __call__( self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> jnp.ndarray: __lowerCamelCase : Dict = scores / self.temperature return scores class UpperCAmelCase_ (_UpperCAmelCase ): """simple docstring""" def __init__( self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = -float('Inf' ) , SCREAMING_SNAKE_CASE_ = 1 ) -> Union[str, Any]: if not isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) or (top_p < 0 or top_p > 1.0): raise ValueError(f'`top_p` has to be a float > 0 and < 1, but is {top_p}' ) if not isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) or (min_tokens_to_keep < 1): raise ValueError(f'`min_tokens_to_keep` has to be a positive integer, but is {min_tokens_to_keep}' ) __lowerCamelCase : str = top_p __lowerCamelCase : Tuple = filter_value __lowerCamelCase : Tuple = min_tokens_to_keep def __call__( self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> jnp.ndarray: __lowerCamelCase , __lowerCamelCase : Any = lax.top_k(SCREAMING_SNAKE_CASE_ , scores.shape[-1] ) __lowerCamelCase : int = jnp.full_like(SCREAMING_SNAKE_CASE_ , self.filter_value ) __lowerCamelCase : Tuple = jax.nn.softmax(SCREAMING_SNAKE_CASE_ , axis=-1 ).cumsum(axis=-1 ) __lowerCamelCase : List[str] = cumulative_probs < self.top_p # include the token that is higher than top_p as well __lowerCamelCase : Tuple = jnp.roll(SCREAMING_SNAKE_CASE_ , 1 ) score_mask |= score_mask.at[:, 0].set(SCREAMING_SNAKE_CASE_ ) # min tokens to keep __lowerCamelCase : Any = score_mask.at[:, : self.min_tokens_to_keep].set(SCREAMING_SNAKE_CASE_ ) __lowerCamelCase : Any = jnp.where(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) __lowerCamelCase : Tuple = jax.lax.sort_key_val(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ )[-1] return next_scores class UpperCAmelCase_ (_UpperCAmelCase ): """simple docstring""" def __init__( self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = -float('Inf' ) , SCREAMING_SNAKE_CASE_ = 1 ) -> str: if not isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) or top_k <= 0: raise ValueError(f'`top_k` has to be a strictly positive integer, but is {top_k}' ) __lowerCamelCase : List[str] = max(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) __lowerCamelCase : Dict = filter_value def __call__( self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> jnp.ndarray: __lowerCamelCase , __lowerCamelCase : List[Any] = scores.shape __lowerCamelCase : Tuple = jnp.full(batch_size * vocab_size , self.filter_value ) __lowerCamelCase : int = min(self.top_k , scores.shape[-1] ) # Safety check __lowerCamelCase , __lowerCamelCase : Tuple = lax.top_k(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) __lowerCamelCase : Union[str, Any] = jnp.broadcast_to((jnp.arange(SCREAMING_SNAKE_CASE_ ) * vocab_size)[:, None] , (batch_size, topk) ).flatten() __lowerCamelCase : List[Any] = topk_scores.flatten() __lowerCamelCase : Union[str, Any] = topk_indices.flatten() + shift __lowerCamelCase : Tuple = next_scores_flat.at[topk_indices_flat].set(SCREAMING_SNAKE_CASE_ ) __lowerCamelCase : Any = next_scores_flat.reshape(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) return next_scores class UpperCAmelCase_ (_UpperCAmelCase ): """simple docstring""" def __init__( self , SCREAMING_SNAKE_CASE_ ) -> Union[str, Any]: __lowerCamelCase : Any = bos_token_id def __call__( self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> jnp.ndarray: __lowerCamelCase : Optional[Any] = jnp.full(scores.shape , -float('inf' ) ) __lowerCamelCase : Optional[Any] = 1 - jnp.bool_(cur_len - 1 ) __lowerCamelCase : List[Any] = jnp.where(SCREAMING_SNAKE_CASE_ , new_scores.at[:, self.bos_token_id].set(0 ) , SCREAMING_SNAKE_CASE_ ) return scores class UpperCAmelCase_ (_UpperCAmelCase ): """simple docstring""" def __init__( self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Dict: __lowerCamelCase : Tuple = max_length __lowerCamelCase : Any = eos_token_id def __call__( self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> jnp.ndarray: __lowerCamelCase : List[str] = jnp.full(scores.shape , -float('inf' ) ) __lowerCamelCase : Any = 1 - jnp.bool_(cur_len - self.max_length + 1 ) __lowerCamelCase : List[str] = jnp.where(SCREAMING_SNAKE_CASE_ , new_scores.at[:, self.eos_token_id].set(0 ) , SCREAMING_SNAKE_CASE_ ) return scores class UpperCAmelCase_ (_UpperCAmelCase ): """simple docstring""" def __init__( self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> List[str]: if not isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) or min_length < 0: raise ValueError(f'`min_length` has to be a positive integer, but is {min_length}' ) if not isinstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) or eos_token_id < 0: raise ValueError(f'`eos_token_id` has to be a positive integer, but is {eos_token_id}' ) __lowerCamelCase : str = min_length __lowerCamelCase : Optional[int] = eos_token_id def __call__( self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> jnp.ndarray: # create boolean flag to decide if min length penalty should be applied __lowerCamelCase : Optional[Any] = 1 - jnp.clip(cur_len - self.min_length , 0 , 1 ) __lowerCamelCase : str = jnp.where(SCREAMING_SNAKE_CASE_ , scores.at[:, self.eos_token_id].set(-float('inf' ) ) , SCREAMING_SNAKE_CASE_ ) return scores class UpperCAmelCase_ (_UpperCAmelCase ): """simple docstring""" def __init__( self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Dict: __lowerCamelCase : Union[str, Any] = list(SCREAMING_SNAKE_CASE_ ) __lowerCamelCase : str = begin_index def __call__( self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Any: __lowerCamelCase : List[Any] = 1 - jnp.bool_(cur_len - self.begin_index ) __lowerCamelCase : str = jnp.where(SCREAMING_SNAKE_CASE_ , scores.at[:, self.begin_suppress_tokens].set(-float('inf' ) ) , SCREAMING_SNAKE_CASE_ ) return scores class UpperCAmelCase_ (_UpperCAmelCase ): """simple docstring""" def __init__( self , SCREAMING_SNAKE_CASE_ ) -> List[Any]: __lowerCamelCase : Tuple = list(SCREAMING_SNAKE_CASE_ ) def __call__( self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> jnp.ndarray: __lowerCamelCase : int = scores.at[..., self.suppress_tokens].set(-float('inf' ) ) return scores class UpperCAmelCase_ (_UpperCAmelCase ): """simple docstring""" def __init__( self , SCREAMING_SNAKE_CASE_ ) -> Union[str, Any]: __lowerCamelCase : Optional[int] = dict(SCREAMING_SNAKE_CASE_ ) # Converts the dictionary of format {index: token} containing the tokens to be forced to an array, where the # index of the array corresponds to the index of the token to be forced, for XLA compatibility. # Indexes without forced tokens will have a negative value. __lowerCamelCase : Dict = jnp.ones((max(force_token_map.keys() ) + 1) , dtype=jnp.intaa ) * -1 for index, token in force_token_map.items(): if token is not None: __lowerCamelCase : str = force_token_array.at[index].set(SCREAMING_SNAKE_CASE_ ) __lowerCamelCase : List[Any] = jnp.intaa(SCREAMING_SNAKE_CASE_ ) def __call__( self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> jnp.ndarray: def _force_token(SCREAMING_SNAKE_CASE_ ): __lowerCamelCase : List[str] = scores.shape[0] __lowerCamelCase : Tuple = self.force_token_array[generation_idx] __lowerCamelCase : List[Any] = jnp.ones_like(SCREAMING_SNAKE_CASE_ , dtype=scores.dtype ) * -float('inf' ) __lowerCamelCase : Any = jnp.zeros((batch_size, 1) , dtype=scores.dtype ) __lowerCamelCase : str = lax.dynamic_update_slice(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , (0, current_token) ) return new_scores __lowerCamelCase : int = lax.cond( cur_len >= self.force_token_array.shape[0] , lambda: scores , lambda: lax.cond( self.force_token_array[cur_len] >= 0 , lambda: _force_token(SCREAMING_SNAKE_CASE_ ) , lambda: scores , ) , ) return scores class UpperCAmelCase_ (_UpperCAmelCase ): """simple docstring""" def __init__( self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Any: __lowerCamelCase : Any = generate_config.eos_token_id __lowerCamelCase : Dict = generate_config.no_timestamps_token_id __lowerCamelCase : Tuple = generate_config.no_timestamps_token_id + 1 __lowerCamelCase : List[str] = decoder_input_length + 1 if generate_config.is_multilingual: # room for language token and task token self.begin_index += 2 if hasattr(SCREAMING_SNAKE_CASE_ , 'max_initial_timestamp_index' ): __lowerCamelCase : str = generate_config.max_initial_timestamp_index else: __lowerCamelCase : Optional[int] = model_config.vocab_size if self.max_initial_timestamp_index is None: __lowerCamelCase : Tuple = model_config.vocab_size def __call__( self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) -> Any: # suppress <|notimestamps|> which is handled by without_timestamps __lowerCamelCase : Union[str, Any] = scores.at[:, self.no_timestamps_token_id].set(-float('inf' ) ) def handle_pairs(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): __lowerCamelCase : str = jnp.where((cur_len - self.begin_index) >= 1 , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) __lowerCamelCase : List[Any] = jnp.where( input_ids_k[cur_len - 1] >= self.timestamp_begin , True and last_was_timestamp , SCREAMING_SNAKE_CASE_ , ) __lowerCamelCase : Any = jnp.where((cur_len - self.begin_index) < 2 , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) __lowerCamelCase : str = jnp.where( input_ids_k[cur_len - 2] >= self.timestamp_begin , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , ) return jnp.where( SCREAMING_SNAKE_CASE_ , jnp.where( penultimate_was_timestamp > 0 , scores_k.at[self.timestamp_begin :].set(-float('inf' ) ) , scores_k.at[: self.eos_token_id].set(-float('inf' ) ) , ) , SCREAMING_SNAKE_CASE_ , ) __lowerCamelCase : List[Any] = jax.vmap(SCREAMING_SNAKE_CASE_ )(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) __lowerCamelCase : Any = jnp.where(cur_len == self.begin_index , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) __lowerCamelCase : Tuple = jnp.where( self.max_initial_timestamp_index is not None , True and apply_max_initial_timestamp , SCREAMING_SNAKE_CASE_ , ) __lowerCamelCase : Any = self.timestamp_begin + self.max_initial_timestamp_index __lowerCamelCase : str = jnp.where( SCREAMING_SNAKE_CASE_ , scores.at[:, last_allowed + 1 :].set(-float('inf' ) ) , SCREAMING_SNAKE_CASE_ , ) # if sum of probability over timestamps is above any other token, sample timestamp __lowerCamelCase : Any = jax.nn.log_softmax(SCREAMING_SNAKE_CASE_ , axis=-1 ) def handle_cumulative_probs(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): __lowerCamelCase : Union[str, Any] = jax.nn.logsumexp(logprobs_k[self.timestamp_begin :] , axis=-1 ) __lowerCamelCase : List[str] = jnp.max(logprobs_k[: self.timestamp_begin] ) return jnp.where( timestamp_logprob > max_text_token_logprob , scores_k.at[: self.timestamp_begin].set(-float('inf' ) ) , SCREAMING_SNAKE_CASE_ , ) __lowerCamelCase : Dict = jax.vmap(SCREAMING_SNAKE_CASE_ )(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) return scores
185
1
from .configuration_bert_masked import MaskedBertConfig from .modeling_bert_masked import ( MaskedBertForMultipleChoice, MaskedBertForQuestionAnswering, MaskedBertForSequenceClassification, MaskedBertForTokenClassification, MaskedBertModel, ) from .modules import *
204
from __future__ import annotations import copy import inspect import unittest import numpy as np from transformers import is_tf_available, is_vision_available from transformers.models.auto import get_values from transformers.testing_utils import require_tf, slow from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST, TF_MODEL_FOR_MULTIPLE_CHOICE_MAPPING, TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING, TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, LayoutLMvaConfig, TFLayoutLMvaForQuestionAnswering, TFLayoutLMvaForSequenceClassification, TFLayoutLMvaForTokenClassification, TFLayoutLMvaModel, ) if is_vision_available(): from PIL import Image from transformers import LayoutLMvaImageProcessor class __snake_case : def __init__( self : Any , _lowercase : Tuple , _lowercase : str=2 , _lowercase : List[Any]=3 , _lowercase : Optional[Any]=4 , _lowercase : Optional[Any]=2 , _lowercase : str=7 , _lowercase : Dict=True , _lowercase : List[str]=True , _lowercase : Union[str, Any]=True , _lowercase : Optional[int]=True , _lowercase : Dict=99 , _lowercase : Dict=36 , _lowercase : Tuple=2 , _lowercase : Optional[int]=4 , _lowercase : int=37 , _lowercase : Tuple="gelu" , _lowercase : Optional[Any]=0.1 , _lowercase : Tuple=0.1 , _lowercase : str=5_12 , _lowercase : Dict=16 , _lowercase : int=2 , _lowercase : int=0.02 , _lowercase : Any=6 , _lowercase : List[Any]=6 , _lowercase : List[Any]=3 , _lowercase : List[Any]=4 , _lowercase : int=None , _lowercase : Optional[int]=10_00 , ): """simple docstring""" SCREAMING_SNAKE_CASE__ = parent SCREAMING_SNAKE_CASE__ = batch_size SCREAMING_SNAKE_CASE__ = num_channels SCREAMING_SNAKE_CASE__ = image_size SCREAMING_SNAKE_CASE__ = patch_size SCREAMING_SNAKE_CASE__ = is_training SCREAMING_SNAKE_CASE__ = use_input_mask SCREAMING_SNAKE_CASE__ = use_token_type_ids SCREAMING_SNAKE_CASE__ = use_labels SCREAMING_SNAKE_CASE__ = vocab_size SCREAMING_SNAKE_CASE__ = hidden_size SCREAMING_SNAKE_CASE__ = num_hidden_layers SCREAMING_SNAKE_CASE__ = num_attention_heads SCREAMING_SNAKE_CASE__ = intermediate_size SCREAMING_SNAKE_CASE__ = hidden_act SCREAMING_SNAKE_CASE__ = hidden_dropout_prob SCREAMING_SNAKE_CASE__ = attention_probs_dropout_prob SCREAMING_SNAKE_CASE__ = max_position_embeddings SCREAMING_SNAKE_CASE__ = type_vocab_size SCREAMING_SNAKE_CASE__ = type_sequence_label_size SCREAMING_SNAKE_CASE__ = initializer_range SCREAMING_SNAKE_CASE__ = coordinate_size SCREAMING_SNAKE_CASE__ = shape_size SCREAMING_SNAKE_CASE__ = num_labels SCREAMING_SNAKE_CASE__ = num_choices SCREAMING_SNAKE_CASE__ = scope SCREAMING_SNAKE_CASE__ = range_bbox # LayoutLMv3's sequence length equals the number of text tokens + number of patches + 1 (we add 1 for the CLS token) SCREAMING_SNAKE_CASE__ = text_seq_length SCREAMING_SNAKE_CASE__ = (image_size // patch_size) ** 2 + 1 SCREAMING_SNAKE_CASE__ = self.text_seq_length + self.image_seq_length def __a ( self : List[str] ): """simple docstring""" SCREAMING_SNAKE_CASE__ = ids_tensor([self.batch_size, self.text_seq_length] , self.vocab_size ) SCREAMING_SNAKE_CASE__ = ids_tensor([self.batch_size, self.text_seq_length, 4] , self.range_bbox ) SCREAMING_SNAKE_CASE__ = bbox.numpy() # Ensure that bbox is legal for i in range(bbox.shape[0] ): for j in range(bbox.shape[1] ): if bbox[i, j, 3] < bbox[i, j, 1]: SCREAMING_SNAKE_CASE__ = bbox[i, j, 3] SCREAMING_SNAKE_CASE__ = bbox[i, j, 1] SCREAMING_SNAKE_CASE__ = tmp_coordinate if bbox[i, j, 2] < bbox[i, j, 0]: SCREAMING_SNAKE_CASE__ = bbox[i, j, 2] SCREAMING_SNAKE_CASE__ = bbox[i, j, 0] SCREAMING_SNAKE_CASE__ = tmp_coordinate SCREAMING_SNAKE_CASE__ = tf.constant(_lowercase ) SCREAMING_SNAKE_CASE__ = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE__ = None if self.use_input_mask: SCREAMING_SNAKE_CASE__ = random_attention_mask([self.batch_size, self.text_seq_length] ) SCREAMING_SNAKE_CASE__ = None if self.use_token_type_ids: SCREAMING_SNAKE_CASE__ = ids_tensor([self.batch_size, self.text_seq_length] , self.type_vocab_size ) SCREAMING_SNAKE_CASE__ = None SCREAMING_SNAKE_CASE__ = None if self.use_labels: SCREAMING_SNAKE_CASE__ = ids_tensor([self.batch_size] , self.type_sequence_label_size ) SCREAMING_SNAKE_CASE__ = ids_tensor([self.batch_size, self.text_seq_length] , self.num_labels ) SCREAMING_SNAKE_CASE__ = LayoutLMvaConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , coordinate_size=self.coordinate_size , shape_size=self.shape_size , input_size=self.image_size , patch_size=self.patch_size , ) return config, input_ids, bbox, pixel_values, token_type_ids, input_mask, sequence_labels, token_labels def __a ( self : List[str] , _lowercase : Dict , _lowercase : List[Any] , _lowercase : str , _lowercase : Optional[int] , _lowercase : Union[str, Any] , _lowercase : Tuple ): """simple docstring""" SCREAMING_SNAKE_CASE__ = TFLayoutLMvaModel(config=_lowercase ) # text + image SCREAMING_SNAKE_CASE__ = model(_lowercase , pixel_values=_lowercase , training=_lowercase ) SCREAMING_SNAKE_CASE__ = model( _lowercase , bbox=_lowercase , pixel_values=_lowercase , attention_mask=_lowercase , token_type_ids=_lowercase , training=_lowercase , ) SCREAMING_SNAKE_CASE__ = model(_lowercase , bbox=_lowercase , pixel_values=_lowercase , training=_lowercase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) # text only SCREAMING_SNAKE_CASE__ = model(_lowercase , training=_lowercase ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.text_seq_length, self.hidden_size) ) # image only SCREAMING_SNAKE_CASE__ = model({"""pixel_values""": pixel_values} , training=_lowercase ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.image_seq_length, self.hidden_size) ) def __a ( self : int , _lowercase : int , _lowercase : Optional[int] , _lowercase : Optional[Any] , _lowercase : Optional[int] , _lowercase : Tuple , _lowercase : List[Any] , _lowercase : Tuple ): """simple docstring""" SCREAMING_SNAKE_CASE__ = self.num_labels SCREAMING_SNAKE_CASE__ = TFLayoutLMvaForSequenceClassification(config=_lowercase ) SCREAMING_SNAKE_CASE__ = model( _lowercase , bbox=_lowercase , pixel_values=_lowercase , attention_mask=_lowercase , token_type_ids=_lowercase , labels=_lowercase , training=_lowercase , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __a ( self : Any , _lowercase : Dict , _lowercase : Tuple , _lowercase : int , _lowercase : Optional[int] , _lowercase : Optional[int] , _lowercase : Union[str, Any] , _lowercase : Tuple ): """simple docstring""" SCREAMING_SNAKE_CASE__ = self.num_labels SCREAMING_SNAKE_CASE__ = TFLayoutLMvaForTokenClassification(config=_lowercase ) SCREAMING_SNAKE_CASE__ = model( _lowercase , bbox=_lowercase , pixel_values=_lowercase , attention_mask=_lowercase , token_type_ids=_lowercase , labels=_lowercase , training=_lowercase , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.text_seq_length, self.num_labels) ) def __a ( self : str , _lowercase : int , _lowercase : List[str] , _lowercase : str , _lowercase : str , _lowercase : List[str] , _lowercase : List[str] , _lowercase : Union[str, Any] ): """simple docstring""" SCREAMING_SNAKE_CASE__ = 2 SCREAMING_SNAKE_CASE__ = TFLayoutLMvaForQuestionAnswering(config=_lowercase ) SCREAMING_SNAKE_CASE__ = model( _lowercase , bbox=_lowercase , pixel_values=_lowercase , attention_mask=_lowercase , token_type_ids=_lowercase , start_positions=_lowercase , end_positions=_lowercase , training=_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 : List[Any] ): """simple docstring""" SCREAMING_SNAKE_CASE__ = self.prepare_config_and_inputs() ((SCREAMING_SNAKE_CASE__) , (SCREAMING_SNAKE_CASE__) , (SCREAMING_SNAKE_CASE__) , (SCREAMING_SNAKE_CASE__) , (SCREAMING_SNAKE_CASE__) , (SCREAMING_SNAKE_CASE__) , (SCREAMING_SNAKE_CASE__) , (SCREAMING_SNAKE_CASE__)) = config_and_inputs SCREAMING_SNAKE_CASE__ = { """input_ids""": input_ids, """bbox""": bbox, """pixel_values""": pixel_values, """token_type_ids""": token_type_ids, """attention_mask""": input_mask, } return config, inputs_dict @require_tf class __snake_case ( lowerCamelCase_ , lowerCamelCase_ , unittest.TestCase ): lowerCAmelCase_ = ( ( TFLayoutLMvaModel, TFLayoutLMvaForQuestionAnswering, TFLayoutLMvaForSequenceClassification, TFLayoutLMvaForTokenClassification, ) if is_tf_available() else () ) lowerCAmelCase_ = ( {"document-question-answering": TFLayoutLMvaForQuestionAnswering, "feature-extraction": TFLayoutLMvaModel} if is_tf_available() else {} ) lowerCAmelCase_ = False lowerCAmelCase_ = False lowerCAmelCase_ = False def __a ( self : Union[str, Any] , _lowercase : Optional[int] , _lowercase : List[str] , _lowercase : Optional[Any] , _lowercase : Optional[int] , _lowercase : List[Any] ): """simple docstring""" return True def __a ( self : List[str] , _lowercase : List[Any] , _lowercase : str , _lowercase : str=False ): """simple docstring""" SCREAMING_SNAKE_CASE__ = copy.deepcopy(_lowercase ) if model_class in get_values(_lowercase ): SCREAMING_SNAKE_CASE__ = { k: tf.tile(tf.expand_dims(_lowercase , 1 ) , (1, self.model_tester.num_choices) + (1,) * (v.ndim - 1) ) if isinstance(_lowercase , tf.Tensor ) and v.ndim > 0 else v for k, v in inputs_dict.items() } if return_labels: if model_class in get_values(_lowercase ): SCREAMING_SNAKE_CASE__ = tf.ones(self.model_tester.batch_size , dtype=tf.intaa ) elif model_class in get_values(_lowercase ): SCREAMING_SNAKE_CASE__ = tf.zeros(self.model_tester.batch_size , dtype=tf.intaa ) SCREAMING_SNAKE_CASE__ = tf.zeros(self.model_tester.batch_size , dtype=tf.intaa ) elif model_class in get_values(_lowercase ): SCREAMING_SNAKE_CASE__ = tf.zeros(self.model_tester.batch_size , dtype=tf.intaa ) elif model_class in get_values(_lowercase ): SCREAMING_SNAKE_CASE__ = tf.zeros( (self.model_tester.batch_size, self.model_tester.text_seq_length) , dtype=tf.intaa ) return inputs_dict def __a ( self : Tuple ): """simple docstring""" SCREAMING_SNAKE_CASE__ = TFLayoutLMvaModelTester(self ) SCREAMING_SNAKE_CASE__ = ConfigTester(self , config_class=_lowercase , hidden_size=37 ) def __a ( self : Any ): """simple docstring""" self.config_tester.run_common_tests() def __a ( self : Dict ): """simple docstring""" SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE__ = model_class(_lowercase ) if getattr(_lowercase , """hf_compute_loss""" , _lowercase ): # The number of elements in the loss should be the same as the number of elements in the label SCREAMING_SNAKE_CASE__ = self._prepare_for_class(inputs_dict.copy() , _lowercase , return_labels=_lowercase ) SCREAMING_SNAKE_CASE__ = prepared_for_class[ sorted(prepared_for_class.keys() - inputs_dict.keys() , reverse=_lowercase )[0] ] SCREAMING_SNAKE_CASE__ = added_label.shape.as_list()[:1] # Test that model correctly compute the loss with kwargs SCREAMING_SNAKE_CASE__ = self._prepare_for_class(inputs_dict.copy() , _lowercase , return_labels=_lowercase ) SCREAMING_SNAKE_CASE__ = prepared_for_class.pop("""input_ids""" ) SCREAMING_SNAKE_CASE__ = model(_lowercase , **_lowercase )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) # Test that model correctly compute the loss when we mask some positions SCREAMING_SNAKE_CASE__ = self._prepare_for_class(inputs_dict.copy() , _lowercase , return_labels=_lowercase ) SCREAMING_SNAKE_CASE__ = prepared_for_class.pop("""input_ids""" ) if "labels" in prepared_for_class: SCREAMING_SNAKE_CASE__ = prepared_for_class["""labels"""].numpy() if len(labels.shape ) > 1 and labels.shape[1] != 1: SCREAMING_SNAKE_CASE__ = -1_00 SCREAMING_SNAKE_CASE__ = tf.convert_to_tensor(_lowercase ) SCREAMING_SNAKE_CASE__ = model(_lowercase , **_lowercase )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) self.assertTrue(not np.any(np.isnan(loss.numpy() ) ) ) # Test that model correctly compute the loss with a dict SCREAMING_SNAKE_CASE__ = self._prepare_for_class(inputs_dict.copy() , _lowercase , return_labels=_lowercase ) SCREAMING_SNAKE_CASE__ = model(_lowercase )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) # Test that model correctly compute the loss with a tuple SCREAMING_SNAKE_CASE__ = self._prepare_for_class(inputs_dict.copy() , _lowercase , return_labels=_lowercase ) # Get keys that were added with the _prepare_for_class function SCREAMING_SNAKE_CASE__ = prepared_for_class.keys() - inputs_dict.keys() SCREAMING_SNAKE_CASE__ = inspect.signature(model.call ).parameters SCREAMING_SNAKE_CASE__ = list(signature.keys() ) # Create a dictionary holding the location of the tensors in the tuple SCREAMING_SNAKE_CASE__ = {0: """input_ids"""} for label_key in label_keys: SCREAMING_SNAKE_CASE__ = signature_names.index(_lowercase ) SCREAMING_SNAKE_CASE__ = label_key SCREAMING_SNAKE_CASE__ = sorted(tuple_index_mapping.items() ) # Initialize a list with their default values, update the values and convert to a tuple SCREAMING_SNAKE_CASE__ = [] for name in signature_names: if name != "kwargs": list_input.append(signature[name].default ) for index, value in sorted_tuple_index_mapping: SCREAMING_SNAKE_CASE__ = prepared_for_class[value] SCREAMING_SNAKE_CASE__ = tuple(_lowercase ) # Send to model SCREAMING_SNAKE_CASE__ = model(tuple_input[:-1] )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) def __a ( self : Optional[int] ): """simple docstring""" ( ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ) = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(_lowercase , _lowercase , _lowercase , _lowercase , _lowercase , _lowercase ) def __a ( self : int ): """simple docstring""" ( ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ) = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: SCREAMING_SNAKE_CASE__ = type self.model_tester.create_and_check_model(_lowercase , _lowercase , _lowercase , _lowercase , _lowercase , _lowercase ) def __a ( self : int ): """simple docstring""" ( ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ) = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification( _lowercase , _lowercase , _lowercase , _lowercase , _lowercase , _lowercase , _lowercase ) def __a ( self : str ): """simple docstring""" ( ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ) = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification( _lowercase , _lowercase , _lowercase , _lowercase , _lowercase , _lowercase , _lowercase ) def __a ( self : List[str] ): """simple docstring""" ( ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ) = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering( _lowercase , _lowercase , _lowercase , _lowercase , _lowercase , _lowercase , _lowercase ) @slow def __a ( self : Tuple ): """simple docstring""" for model_name in TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE__ = TFLayoutLMvaModel.from_pretrained(_lowercase ) self.assertIsNotNone(_lowercase ) def __SCREAMING_SNAKE_CASE ( ) -> List[Any]: """simple docstring""" SCREAMING_SNAKE_CASE__ = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) return image @require_tf class __snake_case ( unittest.TestCase ): @cached_property def __a ( self : Optional[int] ): """simple docstring""" return LayoutLMvaImageProcessor(apply_ocr=_lowercase ) if is_vision_available() else None @slow def __a ( self : Tuple ): """simple docstring""" SCREAMING_SNAKE_CASE__ = TFLayoutLMvaModel.from_pretrained("""microsoft/layoutlmv3-base""" ) SCREAMING_SNAKE_CASE__ = self.default_image_processor SCREAMING_SNAKE_CASE__ = prepare_img() SCREAMING_SNAKE_CASE__ = image_processor(images=_lowercase , return_tensors="""tf""" ).pixel_values SCREAMING_SNAKE_CASE__ = tf.constant([[1, 2]] ) SCREAMING_SNAKE_CASE__ = tf.expand_dims(tf.constant([[1, 2, 3, 4], [5, 6, 7, 8]] ) , axis=0 ) # forward pass SCREAMING_SNAKE_CASE__ = model(input_ids=_lowercase , bbox=_lowercase , pixel_values=_lowercase , training=_lowercase ) # verify the logits SCREAMING_SNAKE_CASE__ = (1, 1_99, 7_68) self.assertEqual(outputs.last_hidden_state.shape , _lowercase ) SCREAMING_SNAKE_CASE__ = tf.constant( [[-0.05_29, 0.36_18, 0.16_32], [-0.15_87, -0.16_67, -0.04_00], [-0.15_57, -0.16_71, -0.05_05]] ) self.assertTrue(np.allclose(outputs.last_hidden_state[0, :3, :3] , _lowercase , atol=1E-4 ) )
204
1
print((lambda quine: quine % quine)('''print((lambda quine: quine %% quine)(%r))'''))
38
__snake_case = '''Input must be a string of 8 numbers plus letter''' __snake_case = '''TRWAGMYFPDXBNJZSQVHLCKE''' def lowerCAmelCase_ ( __lowerCAmelCase )-> bool: '''simple docstring''' if not isinstance(__lowerCAmelCase , __lowerCAmelCase ): UpperCAmelCase : Optional[Any] =f'''Expected string as input, found {type(__lowerCAmelCase ).__name__}''' raise TypeError(__lowerCAmelCase ) UpperCAmelCase : List[Any] =spanish_id.replace('''-''' , '''''' ).upper() if len(__lowerCAmelCase ) != 9: raise ValueError(__lowerCAmelCase ) try: UpperCAmelCase : int =int(spanish_id_clean[0:8] ) UpperCAmelCase : Optional[int] =spanish_id_clean[8] except ValueError as ex: raise ValueError(__lowerCAmelCase ) from ex if letter.isdigit(): raise ValueError(__lowerCAmelCase ) return letter == LOOKUP_LETTERS[number % 23] if __name__ == "__main__": import doctest doctest.testmod()
348
0
"""simple docstring""" import argparse import json import numpy import torch from transformers.models.xlm.tokenization_xlm import VOCAB_FILES_NAMES from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging logging.set_verbosity_info() def lowerCAmelCase_( lowercase_ : str , lowercase_ : List[str] ) -> Any: # Load checkpoint _lowerCamelCase = torch.load(lowercase_ , map_location='''cpu''' ) _lowerCamelCase = chkpt['''model'''] # We have the base model one level deeper than the original XLM repository _lowerCamelCase = {} for k, v in state_dict.items(): if "pred_layer" in k: _lowerCamelCase = v else: _lowerCamelCase = v _lowerCamelCase = chkpt['''params'''] _lowerCamelCase = {n: v for n, v in config.items() if not isinstance(lowercase_ , (torch.FloatTensor, numpy.ndarray) )} _lowerCamelCase = chkpt['''dico_word2id'''] _lowerCamelCase = {s + '''</w>''' if s.find('''@@''' ) == -1 and i > 13 else s.replace('''@@''' , '''''' ): i for s, i in vocab.items()} # Save pytorch-model _lowerCamelCase = pytorch_dump_folder_path + '''/''' + WEIGHTS_NAME _lowerCamelCase = pytorch_dump_folder_path + '''/''' + CONFIG_NAME _lowerCamelCase = pytorch_dump_folder_path + '''/''' + VOCAB_FILES_NAMES['''vocab_file'''] print(F"""Save PyTorch model to {pytorch_weights_dump_path}""" ) torch.save(lowercase_ , lowercase_ ) print(F"""Save configuration file to {pytorch_config_dump_path}""" ) with open(lowercase_ , '''w''' , encoding='''utf-8''' ) as f: f.write(json.dumps(lowercase_ , indent=2 ) + '''\n''' ) print(F"""Save vocab file to {pytorch_config_dump_path}""" ) with open(lowercase_ , '''w''' , encoding='''utf-8''' ) as f: f.write(json.dumps(lowercase_ , indent=2 ) + '''\n''' ) if __name__ == "__main__": A : Tuple = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--xlm_checkpoint_path''', default=None, type=str, required=True, help='''Path the official PyTorch dump.''' ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.''' ) A : List[Any] = parser.parse_args() convert_xlm_checkpoint_to_pytorch(args.xlm_checkpoint_path, args.pytorch_dump_folder_path)
369
"""simple docstring""" import os from collections.abc import Iterator def lowerCAmelCase_( lowercase_ : str = "." ) -> Iterator[str]: for dir_path, dir_names, filenames in os.walk(lowercase_ ): _lowerCamelCase = [d for d in dir_names if d != '''scripts''' and d[0] not in '''._'''] for filename in filenames: if filename == "__init__.py": continue if os.path.splitext(lowercase_ )[1] in (".py", ".ipynb"): yield os.path.join(lowercase_ , lowercase_ ).lstrip('''./''' ) def lowerCAmelCase_( lowercase_ : Dict ) -> List[Any]: return F"""{i * " "}*""" if i else "\n##" def lowerCAmelCase_( lowercase_ : str , lowercase_ : str ) -> str: _lowerCamelCase = old_path.split(os.sep ) for i, new_part in enumerate(new_path.split(os.sep ) ): if (i + 1 > len(lowercase_ ) or old_parts[i] != new_part) and new_part: print(F"""{md_prefix(lowercase_ )} {new_part.replace("_" , " " ).title()}""" ) return new_path def lowerCAmelCase_( lowercase_ : str = "." ) -> None: _lowerCamelCase = '''''' for filepath in sorted(good_file_paths(lowercase_ ) ): _lowerCamelCase , _lowerCamelCase = os.path.split(lowercase_ ) if filepath != old_path: _lowerCamelCase = print_path(lowercase_ , lowercase_ ) _lowerCamelCase = (filepath.count(os.sep ) + 1) if filepath else 0 _lowerCamelCase = F"""{filepath}/{filename}""".replace(''' ''' , '''%20''' ) _lowerCamelCase = os.path.splitext(filename.replace('''_''' , ''' ''' ).title() )[0] print(F"""{md_prefix(lowercase_ )} [{filename}]({url})""" ) if __name__ == "__main__": print_directory_md('''.''')
73
0
import pytest import datasets # Import fixture modules as plugins snake_case_ : Tuple = ["tests.fixtures.files", "tests.fixtures.hub", "tests.fixtures.fsspec"] def A (__A : Optional[int] , __A : Any ) -> Optional[int]: """simple docstring""" for item in items: if any(marker in item.keywords for marker in ['''integration''', '''unit'''] ): continue item.add_marker(pytest.mark.unit ) def A (__A : Optional[Any] ) -> Optional[int]: """simple docstring""" config.addinivalue_line('''markers''' , '''torchaudio_latest: mark test to run with torchaudio>=0.12''' ) @pytest.fixture(autouse=__A ) def A (__A : Optional[int] , __A : Dict ) -> List[Any]: """simple docstring""" UpperCAmelCase_ = tmp_path_factory.getbasetemp() / '''cache''' UpperCAmelCase_ = test_hf_cache_home / '''datasets''' UpperCAmelCase_ = test_hf_cache_home / '''metrics''' UpperCAmelCase_ = test_hf_cache_home / '''modules''' monkeypatch.setattr('''datasets.config.HF_DATASETS_CACHE''' , str(__A ) ) monkeypatch.setattr('''datasets.config.HF_METRICS_CACHE''' , str(__A ) ) monkeypatch.setattr('''datasets.config.HF_MODULES_CACHE''' , str(__A ) ) UpperCAmelCase_ = test_hf_datasets_cache / '''downloads''' monkeypatch.setattr('''datasets.config.DOWNLOADED_DATASETS_PATH''' , str(__A ) ) UpperCAmelCase_ = test_hf_datasets_cache / '''downloads''' / '''extracted''' monkeypatch.setattr('''datasets.config.EXTRACTED_DATASETS_PATH''' , str(__A ) ) @pytest.fixture(autouse=__A , scope='''session''' ) def A () -> Dict: """simple docstring""" datasets.disable_progress_bar() @pytest.fixture(autouse=__A ) def A (__A : Tuple ) -> str: """simple docstring""" monkeypatch.setattr('''datasets.config.HF_UPDATE_DOWNLOAD_COUNTS''' , __A ) @pytest.fixture def A (__A : Union[str, Any] ) -> int: """simple docstring""" monkeypatch.setattr('''sqlalchemy.util.deprecations.SILENCE_UBER_WARNING''' , __A )
51
from typing import List, Optional, TypeVar from .arrow_dataset import Dataset, _concatenate_map_style_datasets, _interleave_map_style_datasets from .dataset_dict import DatasetDict, IterableDatasetDict from .info import DatasetInfo from .iterable_dataset import IterableDataset, _concatenate_iterable_datasets, _interleave_iterable_datasets from .splits import NamedSplit from .utils import logging from .utils.py_utils import Literal _A = logging.get_logger(__name__) _A = TypeVar("DatasetType", Dataset, IterableDataset) def lowerCamelCase__ ( __lowerCAmelCase : List[DatasetType] , __lowerCAmelCase : Optional[List[float]] = None , __lowerCAmelCase : Optional[int] = None , __lowerCAmelCase : Optional[DatasetInfo] = None , __lowerCAmelCase : Optional[NamedSplit] = None , __lowerCAmelCase : Literal["first_exhausted", "all_exhausted"] = "first_exhausted" , ): """simple docstring""" from .arrow_dataset import Dataset from .iterable_dataset import IterableDataset if not datasets: raise ValueError("Unable to interleave an empty list of datasets." ) for i, dataset in enumerate(__lowerCAmelCase ): if not isinstance(__lowerCAmelCase , (Dataset, IterableDataset) ): if isinstance(__lowerCAmelCase , (DatasetDict, IterableDatasetDict) ): if not dataset: raise ValueError( F"""Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} """ "is an empty dataset dictionary." ) raise ValueError( F"""Dataset at position {i} has at least one split: {list(__lowerCAmelCase )}\n""" F"""Please pick one to interleave with the other datasets, for example: dataset['{next(iter(__lowerCAmelCase ) )}']""" ) raise ValueError( F"""Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} is a {type(__lowerCAmelCase ).__name__}.""" ) if i == 0: lowerCAmelCase_ , lowerCAmelCase_ = ( (Dataset, IterableDataset) if isinstance(__lowerCAmelCase , __lowerCAmelCase ) else (IterableDataset, Dataset) ) elif not isinstance(__lowerCAmelCase , __lowerCAmelCase ): raise ValueError( F"""Unable to interleave a {dataset_type.__name__} (at position 0) with a {other_type.__name__} (at position {i}). Expected a list of Dataset objects or a list of IterableDataset objects.""" ) if stopping_strategy not in ["first_exhausted", "all_exhausted"]: raise ValueError(F"""{stopping_strategy} is not supported. Please enter a valid stopping_strategy.""" ) if dataset_type is Dataset: return _interleave_map_style_datasets( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , info=__lowerCAmelCase , split=__lowerCAmelCase , stopping_strategy=__lowerCAmelCase ) else: return _interleave_iterable_datasets( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , info=__lowerCAmelCase , split=__lowerCAmelCase , stopping_strategy=__lowerCAmelCase ) def lowerCamelCase__ ( __lowerCAmelCase : List[DatasetType] , __lowerCAmelCase : Optional[DatasetInfo] = None , __lowerCAmelCase : Optional[NamedSplit] = None , __lowerCAmelCase : int = 0 , ): """simple docstring""" if not dsets: raise ValueError("Unable to concatenate an empty list of datasets." ) for i, dataset in enumerate(__lowerCAmelCase ): if not isinstance(__lowerCAmelCase , (Dataset, IterableDataset) ): if isinstance(__lowerCAmelCase , (DatasetDict, IterableDatasetDict) ): if not dataset: raise ValueError( F"""Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} """ "is an empty dataset dictionary." ) raise ValueError( F"""Dataset at position {i} has at least one split: {list(__lowerCAmelCase )}\n""" F"""Please pick one to interleave with the other datasets, for example: dataset['{next(iter(__lowerCAmelCase ) )}']""" ) raise ValueError( F"""Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} is a {type(__lowerCAmelCase ).__name__}.""" ) if i == 0: lowerCAmelCase_ , lowerCAmelCase_ = ( (Dataset, IterableDataset) if isinstance(__lowerCAmelCase , __lowerCAmelCase ) else (IterableDataset, Dataset) ) elif not isinstance(__lowerCAmelCase , __lowerCAmelCase ): raise ValueError( F"""Unable to interleave a {dataset_type.__name__} (at position 0) with a {other_type.__name__} (at position {i}). Expected a list of Dataset objects or a list of IterableDataset objects.""" ) if dataset_type is Dataset: return _concatenate_map_style_datasets(__lowerCAmelCase , info=__lowerCAmelCase , split=__lowerCAmelCase , axis=__lowerCAmelCase ) else: return _concatenate_iterable_datasets(__lowerCAmelCase , info=__lowerCAmelCase , split=__lowerCAmelCase , axis=__lowerCAmelCase )
231
0
from typing import Dict, List, Optional, Union import numpy as np from transformers.utils import is_vision_available from transformers.utils.generic import TensorType from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, is_valid_image, to_numpy_array, valid_images, ) from ...utils import logging if is_vision_available(): import PIL _A : str = logging.get_logger(__name__) def _a ( UpperCAmelCase ) -> List[List[ImageInput]]: """simple docstring""" if isinstance(UpperCAmelCase , (list, tuple) ) and isinstance(videos[0] , (list, tuple) ) and is_valid_image(videos[0][0] ): return videos elif isinstance(UpperCAmelCase , (list, tuple) ) and is_valid_image(videos[0] ): return [videos] elif is_valid_image(UpperCAmelCase ): return [[videos]] raise ValueError(f"Could not make batched video from {videos}" ) class __SCREAMING_SNAKE_CASE ( lowerCAmelCase_ ): _UpperCAmelCase : Optional[int] = ["pixel_values"] def __init__( self : int , A : bool = True , A : Dict[str, int] = None , A : PILImageResampling = PILImageResampling.BILINEAR , A : bool = True , A : Dict[str, int] = None , A : bool = True , A : Union[int, float] = 1 / 2_5_5 , A : bool = True , A : bool = True , A : Optional[Union[float, List[float]]] = None , A : Optional[Union[float, List[float]]] = None , **A : List[str] , ) ->None: super().__init__(**A ) lowerCamelCase__ : str = size if size is not None else {'''shortest_edge''': 2_5_6} lowerCamelCase__ : Optional[Any] = get_size_dict(A , default_to_square=A ) lowerCamelCase__ : Dict = crop_size if crop_size is not None else {'''height''': 2_2_4, '''width''': 2_2_4} lowerCamelCase__ : Union[str, Any] = get_size_dict(A , param_name='''crop_size''' ) lowerCamelCase__ : int = do_resize lowerCamelCase__ : int = size lowerCamelCase__ : Tuple = do_center_crop lowerCamelCase__ : int = crop_size lowerCamelCase__ : List[Any] = resample lowerCamelCase__ : Any = do_rescale lowerCamelCase__ : Any = rescale_factor lowerCamelCase__ : str = offset lowerCamelCase__ : Optional[int] = do_normalize lowerCamelCase__ : str = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN lowerCamelCase__ : Union[str, Any] = image_std if image_std is not None else IMAGENET_STANDARD_STD def __lowerCamelCase ( self : Optional[Any] , A : np.ndarray , A : Dict[str, int] , A : PILImageResampling = PILImageResampling.BILINEAR , A : Optional[Union[str, ChannelDimension]] = None , **A : str , ) ->np.ndarray: lowerCamelCase__ : Any = get_size_dict(A , default_to_square=A ) if "shortest_edge" in size: lowerCamelCase__ : Dict = get_resize_output_image_size(A , size['''shortest_edge'''] , default_to_square=A ) elif "height" in size and "width" in size: lowerCamelCase__ : List[Any] = (size['''height'''], size['''width''']) else: raise ValueError(F"Size must have 'height' and 'width' or 'shortest_edge' as keys. Got {size.keys()}" ) return resize(A , size=A , resample=A , data_format=A , **A ) def __lowerCamelCase ( self : Optional[int] , A : np.ndarray , A : Dict[str, int] , A : Optional[Union[str, ChannelDimension]] = None , **A : Dict , ) ->np.ndarray: lowerCamelCase__ : Optional[Any] = get_size_dict(A ) if "height" not in size or "width" not in size: raise ValueError(F"Size must have 'height' and 'width' as keys. Got {size.keys()}" ) return center_crop(A , size=(size['''height'''], size['''width''']) , data_format=A , **A ) def __lowerCamelCase ( self : List[str] , A : np.ndarray , A : Union[int, float] , A : bool = True , A : Optional[Union[str, ChannelDimension]] = None , **A : List[Any] , ) ->Optional[Any]: lowerCamelCase__ : Optional[int] = image.astype(np.floataa ) if offset: lowerCamelCase__ : int = image - (scale / 2) return rescale(A , scale=A , data_format=A , **A ) def __lowerCamelCase ( self : List[str] , A : np.ndarray , A : Union[float, List[float]] , A : Union[float, List[float]] , A : Optional[Union[str, ChannelDimension]] = None , **A : List[str] , ) ->np.ndarray: return normalize(A , mean=A , std=A , data_format=A , **A ) def __lowerCamelCase ( self : Any , A : ImageInput , A : bool = None , A : Dict[str, int] = None , A : PILImageResampling = None , A : bool = None , A : Dict[str, int] = None , A : bool = None , A : float = None , A : bool = None , A : bool = None , A : Optional[Union[float, List[float]]] = None , A : Optional[Union[float, List[float]]] = None , A : Optional[ChannelDimension] = ChannelDimension.FIRST , ) ->np.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_center_crop and crop_size is None: raise ValueError('''Crop size must be specified if do_center_crop is True.''' ) if do_rescale and rescale_factor is None: raise ValueError('''Rescale factor must be specified if do_rescale is True.''' ) if do_normalize and (image_mean is None or image_std is None): raise ValueError('''Image mean and std must be specified if do_normalize is True.''' ) if offset and not do_rescale: raise ValueError('''For offset, do_rescale must also be set to True.''' ) # All transformations expect numpy arrays. lowerCamelCase__ : Optional[int] = to_numpy_array(A ) if do_resize: lowerCamelCase__ : Union[str, Any] = self.resize(image=A , size=A , resample=A ) if do_center_crop: lowerCamelCase__ : int = self.center_crop(A , size=A ) if do_rescale: lowerCamelCase__ : Tuple = self.rescale(image=A , scale=A , offset=A ) if do_normalize: lowerCamelCase__ : Dict = self.normalize(image=A , mean=A , std=A ) lowerCamelCase__ : Dict = to_channel_dimension_format(A , A ) return image def __lowerCamelCase ( self : List[str] , A : ImageInput , A : bool = None , A : Dict[str, int] = None , A : PILImageResampling = None , A : bool = None , A : Dict[str, int] = None , A : bool = None , A : float = None , A : bool = None , A : bool = None , A : Optional[Union[float, List[float]]] = None , A : Optional[Union[float, List[float]]] = None , A : Optional[Union[str, TensorType]] = None , A : ChannelDimension = ChannelDimension.FIRST , **A : Optional[int] , ) ->PIL.Image.Image: lowerCamelCase__ : List[Any] = do_resize if do_resize is not None else self.do_resize lowerCamelCase__ : str = resample if resample is not None else self.resample lowerCamelCase__ : Optional[Any] = do_center_crop if do_center_crop is not None else self.do_center_crop lowerCamelCase__ : Optional[Any] = do_rescale if do_rescale is not None else self.do_rescale lowerCamelCase__ : Any = rescale_factor if rescale_factor is not None else self.rescale_factor lowerCamelCase__ : Dict = offset if offset is not None else self.offset lowerCamelCase__ : List[str] = do_normalize if do_normalize is not None else self.do_normalize lowerCamelCase__ : str = image_mean if image_mean is not None else self.image_mean lowerCamelCase__ : Dict = image_std if image_std is not None else self.image_std lowerCamelCase__ : int = size if size is not None else self.size lowerCamelCase__ : Union[str, Any] = get_size_dict(A , default_to_square=A ) lowerCamelCase__ : List[Any] = crop_size if crop_size is not None else self.crop_size lowerCamelCase__ : int = get_size_dict(A , param_name='''crop_size''' ) if not valid_images(A ): raise ValueError( '''Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ''' '''torch.Tensor, tf.Tensor or jax.ndarray.''' ) lowerCamelCase__ : Union[str, Any] = make_batched(A ) lowerCamelCase__ : Optional[Any] = [ [ self._preprocess_image( image=A , do_resize=A , size=A , resample=A , do_center_crop=A , crop_size=A , do_rescale=A , rescale_factor=A , offset=A , do_normalize=A , image_mean=A , image_std=A , data_format=A , ) for img in video ] for video in videos ] lowerCamelCase__ : List[str] = {'''pixel_values''': videos} return BatchFeature(data=A , tensor_type=A )
362
def _a ( UpperCAmelCase ) -> int: """simple docstring""" if not isinstance(UpperCAmelCase , UpperCAmelCase ): raise TypeError('''only integers accepted as input''' ) else: lowerCamelCase__ : Any = str(abs(UpperCAmelCase ) ) lowerCamelCase__ : Union[str, Any] = [list(UpperCAmelCase ) for char in range(len(UpperCAmelCase ) )] for index in range(len(UpperCAmelCase ) ): num_transpositions[index].pop(UpperCAmelCase ) return max( int(''''''.join(list(UpperCAmelCase ) ) ) for transposition in num_transpositions ) if __name__ == "__main__": __import__('doctest').testmod()
265
0
def UpperCamelCase ( __lowerCamelCase : int ): if a < 0: raise ValueError("Input value must be a positive integer" ) elif isinstance(__lowerCamelCase , __lowerCamelCase ): raise TypeError("Input value must be a 'int' type" ) return bin(__lowerCamelCase ).count("1" ) if __name__ == "__main__": import doctest doctest.testmod()
59
import logging import numpy as np import pytest from scipy.linalg import eigh logging.basicConfig(level=logging.INFO, format="""%(message)s""") def UpperCamelCase ( __lowerCamelCase : np.ndarray ): return input_array.reshape((input_array.size, 1) ) def UpperCamelCase ( __lowerCamelCase : np.ndarray , __lowerCamelCase : np.ndarray , __lowerCamelCase : int ): snake_case : Any = np.nan for i in range(__lowerCamelCase ): snake_case : List[str] = features[:, labels == i] snake_case : Dict = data.mean(1 ) # Centralize the data of class i snake_case : Optional[Any] = data - column_reshape(__lowerCamelCase ) if i > 0: # If covariance_sum is not None covariance_sum += np.dot(__lowerCamelCase , centered_data.T ) else: # If covariance_sum is np.nan (i.e. first loop) snake_case : Optional[Any] = np.dot(__lowerCamelCase , centered_data.T ) return covariance_sum / features.shape[1] def UpperCamelCase ( __lowerCamelCase : np.ndarray , __lowerCamelCase : np.ndarray , __lowerCamelCase : int ): snake_case : Optional[Any] = features.mean(1 ) snake_case : Tuple = np.nan for i in range(__lowerCamelCase ): snake_case : Tuple = features[:, labels == i] snake_case : Tuple = data.shape[1] snake_case : List[str] = data.mean(1 ) if i > 0: # If covariance_sum is not None covariance_sum += device_data * np.dot( column_reshape(__lowerCamelCase ) - column_reshape(__lowerCamelCase ) , (column_reshape(__lowerCamelCase ) - column_reshape(__lowerCamelCase )).T , ) else: # If covariance_sum is np.nan (i.e. first loop) snake_case : Optional[int] = device_data * np.dot( column_reshape(__lowerCamelCase ) - column_reshape(__lowerCamelCase ) , (column_reshape(__lowerCamelCase ) - column_reshape(__lowerCamelCase )).T , ) return covariance_sum / features.shape[1] def UpperCamelCase ( __lowerCamelCase : np.ndarray , __lowerCamelCase : int ): # Check if the features have been loaded if features.any(): snake_case : Tuple = features.mean(1 ) # Center the dataset snake_case : List[str] = features - np.reshape(__lowerCamelCase , (data_mean.size, 1) ) snake_case : Optional[Any] = np.dot(__lowerCamelCase , centered_data.T ) / features.shape[1] snake_case , snake_case : Dict = np.linalg.eigh(__lowerCamelCase ) # Take all the columns in the reverse order (-1), and then takes only the first snake_case : Optional[Any] = eigenvectors[:, ::-1][:, 0:dimensions] # Project the database on the new space snake_case : Union[str, Any] = np.dot(filtered_eigenvectors.T , __lowerCamelCase ) logging.info("Principal Component Analysis computed" ) return projected_data else: logging.basicConfig(level=logging.ERROR , format="%(message)s" , force=__lowerCamelCase ) logging.error("Dataset empty" ) raise AssertionError def UpperCamelCase ( __lowerCamelCase : np.ndarray , __lowerCamelCase : np.ndarray , __lowerCamelCase : int , __lowerCamelCase : int ): assert classes > dimensions # Check if features have been already loaded if features.any: snake_case , snake_case : str = eigh( covariance_between_classes(__lowerCamelCase , __lowerCamelCase , __lowerCamelCase ) , covariance_within_classes(__lowerCamelCase , __lowerCamelCase , __lowerCamelCase ) , ) snake_case : str = eigenvectors[:, ::-1][:, :dimensions] snake_case , snake_case , snake_case : int = np.linalg.svd(__lowerCamelCase ) snake_case : List[Any] = svd_matrix[:, 0:dimensions] snake_case : Optional[Any] = np.dot(filtered_svd_matrix.T , __lowerCamelCase ) logging.info("Linear Discriminant Analysis computed" ) return projected_data else: logging.basicConfig(level=logging.ERROR , format="%(message)s" , force=__lowerCamelCase ) logging.error("Dataset empty" ) raise AssertionError def UpperCamelCase ( ): # Create dummy dataset with 2 classes and 3 features snake_case : str = np.array([[1, 2, 3, 4, 5], [2, 3, 4, 5, 6], [3, 4, 5, 6, 7]] ) snake_case : Union[str, Any] = np.array([0, 0, 0, 1, 1] ) snake_case : List[Any] = 2 snake_case : Any = 2 # Assert that the function raises an AssertionError if dimensions > classes with pytest.raises(__lowerCamelCase ) as error_info: snake_case : str = linear_discriminant_analysis( __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase ) if isinstance(__lowerCamelCase , np.ndarray ): raise AssertionError( "Did not raise AssertionError for dimensions > classes" ) assert error_info.type is AssertionError def UpperCamelCase ( ): snake_case : List[str] = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]] ) snake_case : List[str] = 2 snake_case : int = np.array([[6.9282_0323, 8.6602_5404, 10.3923_0485], [3.0, 3.0, 3.0]] ) with pytest.raises(__lowerCamelCase ) as error_info: snake_case : Union[str, Any] = principal_component_analysis(__lowerCamelCase , __lowerCamelCase ) if not np.allclose(__lowerCamelCase , __lowerCamelCase ): raise AssertionError assert error_info.type is AssertionError if __name__ == "__main__": import doctest doctest.testmod()
59
1
"""simple docstring""" import argparse from pathlib import Path from transformers import AutoConfig, AutoTokenizer, RagConfig, RagSequenceForGeneration, RagTokenForGeneration def A_ ( snake_case_ : Optional[int] ,snake_case_ : str ,snake_case_ : Union[str, Any] ,snake_case_ : int ,snake_case_ : Any = None ,snake_case_ : Union[str, Any] = None ,snake_case_ : int = None ,): '''simple docstring''' if config_name_or_path is None: UpperCamelCase : str = 'facebook/rag-token-base' if model_type == 'rag_token' else 'facebook/rag-sequence-base' if generator_tokenizer_name_or_path is None: UpperCamelCase : int = generator_name_or_path if question_encoder_tokenizer_name_or_path is None: UpperCamelCase : int = question_encoder_name_or_path UpperCamelCase : Tuple = RagTokenForGeneration if model_type == 'rag_token' else RagSequenceForGeneration # Save model. UpperCamelCase : Dict = RagConfig.from_pretrained(lowerCAmelCase_ ) UpperCamelCase : int = AutoConfig.from_pretrained(lowerCAmelCase_ ) UpperCamelCase : Union[str, Any] = AutoConfig.from_pretrained(lowerCAmelCase_ ) UpperCamelCase : Union[str, Any] = gen_config UpperCamelCase : Tuple = question_encoder_config UpperCamelCase : List[str] = model_class.from_pretrained_question_encoder_generator( lowerCAmelCase_ ,lowerCAmelCase_ ,config=lowerCAmelCase_ ) rag_model.save_pretrained(lowerCAmelCase_ ) # Sanity check. model_class.from_pretrained(lowerCAmelCase_ ) # Save tokenizers. UpperCamelCase : Optional[Any] = AutoTokenizer.from_pretrained(lowerCAmelCase_ ) gen_tokenizer.save_pretrained(dest_dir / """generator_tokenizer/""" ) UpperCamelCase : Any = AutoTokenizer.from_pretrained(lowerCAmelCase_ ) question_encoder_tokenizer.save_pretrained(dest_dir / """question_encoder_tokenizer/""" ) if __name__ == "__main__": __A : Union[str, Any] = 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``''' ), ) __A : List[Any] = parser.parse_args() __A : Optional[int] = 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, )
368
"""simple docstring""" from collections.abc import Callable def A_ ( snake_case_ : Callable[[float], float] ,snake_case_ : float ,snake_case_ : float ): '''simple docstring''' UpperCamelCase : float = a UpperCamelCase : float = b if function(snake_case_ ) == 0: # one of the a or b is a root for the function return a elif function(snake_case_ ) == 0: return b elif ( function(snake_case_ ) * function(snake_case_ ) > 0 ): # if none of these are root and they are both positive or negative, # then this algorithm can't find the root raise ValueError("""could not find root in given interval.""" ) else: UpperCamelCase : float = start + (end - start) / 2.0 while abs(start - mid ) > 1_0**-7: # until precisely equals to 10^-7 if function(snake_case_ ) == 0: return mid elif function(snake_case_ ) * function(snake_case_ ) < 0: UpperCamelCase : Dict = mid else: UpperCamelCase : List[str] = mid UpperCamelCase : Tuple = start + (end - start) / 2.0 return mid def A_ ( snake_case_ : float ): '''simple docstring''' return x**3 - 2 * x - 5 if __name__ == "__main__": print(bisection(f, 1, 1000)) import doctest doctest.testmod()
27
0
import unittest from transformers import is_torch_available from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device if is_torch_available(): from transformers import AutoModelForSeqaSeqLM, AutoTokenizer @require_torch @require_sentencepiece @require_tokenizers class A( unittest.TestCase ): '''simple docstring''' @slow def a__ ( self : Dict ) -> Optional[Any]: """simple docstring""" lowerCamelCase_ = AutoModelForSeqaSeqLM.from_pretrained('google/mt5-small' , return_dict=A_ ).to(A_ ) lowerCamelCase_ = AutoTokenizer.from_pretrained('google/mt5-small' ) lowerCamelCase_ = tokenizer('Hello there' , return_tensors='pt' ).input_ids lowerCamelCase_ = tokenizer('Hi I am' , return_tensors='pt' ).input_ids lowerCamelCase_ = model(input_ids.to(A_ ) , labels=labels.to(A_ ) ).loss lowerCamelCase_ = -(labels.shape[-1] * loss.item()) lowerCamelCase_ = -84.9127 self.assertTrue(abs(mtf_score - EXPECTED_SCORE ) < 1E-4 )
204
def _SCREAMING_SNAKE_CASE ( lowercase : int = 10_00 ): '''simple docstring''' lowerCamelCase_ = 2**power lowerCamelCase_ = 0 while n: lowerCamelCase_ , lowerCamelCase_ = r + n % 10, n // 10 return r if __name__ == "__main__": print(solution(int(str(input()).strip())))
204
1
import flax.linen as nn import jax import jax.numpy as jnp class _snake_case ( nn.Module ): lowerCamelCase__: int lowerCamelCase__: jnp.dtype = jnp.floataa def _lowerCamelCase ( self: Tuple ) -> Union[str, Any]: __UpperCAmelCase : List[str] = nn.Conv( self.out_channels , kernel_size=(3, 3) , strides=(1, 1) , padding=((1, 1), (1, 1)) , dtype=self.dtype , ) def __call__( self: Optional[Any] , __lowerCamelCase: Optional[int] ) -> List[Any]: __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase : Union[str, Any] = hidden_states.shape __UpperCAmelCase : Dict = jax.image.resize( __lowerCamelCase , shape=(batch, height * 2, width * 2, channels) , method="nearest" , ) __UpperCAmelCase : Dict = self.conv(__lowerCamelCase ) return hidden_states class _snake_case ( nn.Module ): lowerCamelCase__: int lowerCamelCase__: jnp.dtype = jnp.floataa def _lowerCamelCase ( self: str ) -> Any: __UpperCAmelCase : Optional[int] = nn.Conv( self.out_channels , kernel_size=(3, 3) , strides=(2, 2) , padding=((1, 1), (1, 1)) , dtype=self.dtype , ) def __call__( self: Dict , __lowerCamelCase: str ) -> List[Any]: # pad = ((0, 0), (0, 1), (0, 1), (0, 0)) # pad height and width dim # hidden_states = jnp.pad(hidden_states, pad_width=pad) __UpperCAmelCase : Any = self.conv(__lowerCamelCase ) return hidden_states class _snake_case ( nn.Module ): lowerCamelCase__: int lowerCamelCase__: int = None lowerCamelCase__: float = 0.0 lowerCamelCase__: bool = None lowerCamelCase__: jnp.dtype = jnp.floataa def _lowerCamelCase ( self: str ) -> List[str]: __UpperCAmelCase : str = self.in_channels if self.out_channels is None else self.out_channels __UpperCAmelCase : Dict = nn.GroupNorm(num_groups=32 , epsilon=1e-5 ) __UpperCAmelCase : List[str] = nn.Conv( __lowerCamelCase , kernel_size=(3, 3) , strides=(1, 1) , padding=((1, 1), (1, 1)) , dtype=self.dtype , ) __UpperCAmelCase : Optional[Any] = nn.Dense(__lowerCamelCase , dtype=self.dtype ) __UpperCAmelCase : Any = nn.GroupNorm(num_groups=32 , epsilon=1e-5 ) __UpperCAmelCase : Optional[Any] = nn.Dropout(self.dropout_prob ) __UpperCAmelCase : Tuple = nn.Conv( __lowerCamelCase , kernel_size=(3, 3) , strides=(1, 1) , padding=((1, 1), (1, 1)) , dtype=self.dtype , ) __UpperCAmelCase : Optional[int] = self.in_channels != out_channels if self.use_nin_shortcut is None else self.use_nin_shortcut __UpperCAmelCase : List[Any] = None if use_nin_shortcut: __UpperCAmelCase : Dict = nn.Conv( __lowerCamelCase , kernel_size=(1, 1) , strides=(1, 1) , padding="VALID" , dtype=self.dtype , ) def __call__( self: Tuple , __lowerCamelCase: Tuple , __lowerCamelCase: str , __lowerCamelCase: Union[str, Any]=True ) -> List[Any]: __UpperCAmelCase : Dict = hidden_states __UpperCAmelCase : int = self.norma(__lowerCamelCase ) __UpperCAmelCase : Union[str, Any] = nn.swish(__lowerCamelCase ) __UpperCAmelCase : Tuple = self.conva(__lowerCamelCase ) __UpperCAmelCase : Optional[Any] = self.time_emb_proj(nn.swish(__lowerCamelCase ) ) __UpperCAmelCase : List[str] = jnp.expand_dims(jnp.expand_dims(__lowerCamelCase , 1 ) , 1 ) __UpperCAmelCase : List[str] = hidden_states + temb __UpperCAmelCase : Union[str, Any] = self.norma(__lowerCamelCase ) __UpperCAmelCase : Tuple = nn.swish(__lowerCamelCase ) __UpperCAmelCase : str = self.dropout(__lowerCamelCase , __lowerCamelCase ) __UpperCAmelCase : List[str] = self.conva(__lowerCamelCase ) if self.conv_shortcut is not None: __UpperCAmelCase : Optional[int] = self.conv_shortcut(__lowerCamelCase ) return hidden_states + residual
342
import os from typing import List, Optional, Union from ...image_processing_utils import BatchFeature from ...image_utils import ImageInput from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy from ...utils import TensorType from ..auto import AutoTokenizer class _snake_case ( _lowercase ): lowerCamelCase__: Any = ["image_processor", "tokenizer"] lowerCamelCase__: Optional[Any] = "BlipImageProcessor" lowerCamelCase__: Optional[int] = "AutoTokenizer" def __init__( self: List[str] , __lowerCamelCase: str , __lowerCamelCase: List[str] , __lowerCamelCase: Optional[Any] ) -> Dict: super().__init__(__lowerCamelCase , __lowerCamelCase ) # add QFormer tokenizer __UpperCAmelCase : Dict = qformer_tokenizer def __call__( self: Any , __lowerCamelCase: ImageInput = None , __lowerCamelCase: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None , __lowerCamelCase: bool = True , __lowerCamelCase: Union[bool, str, PaddingStrategy] = False , __lowerCamelCase: Union[bool, str, TruncationStrategy] = None , __lowerCamelCase: Optional[int] = None , __lowerCamelCase: int = 0 , __lowerCamelCase: Optional[int] = None , __lowerCamelCase: Optional[bool] = None , __lowerCamelCase: bool = False , __lowerCamelCase: bool = False , __lowerCamelCase: bool = False , __lowerCamelCase: bool = False , __lowerCamelCase: bool = False , __lowerCamelCase: bool = True , __lowerCamelCase: Optional[Union[str, TensorType]] = None , **__lowerCamelCase: Dict , ) -> BatchFeature: if images is None and text is None: raise ValueError("You have to specify at least images or text." ) __UpperCAmelCase : str = BatchFeature() if text is not None: __UpperCAmelCase : Any = self.tokenizer( text=__lowerCamelCase , add_special_tokens=__lowerCamelCase , padding=__lowerCamelCase , truncation=__lowerCamelCase , max_length=__lowerCamelCase , stride=__lowerCamelCase , pad_to_multiple_of=__lowerCamelCase , return_attention_mask=__lowerCamelCase , return_overflowing_tokens=__lowerCamelCase , return_special_tokens_mask=__lowerCamelCase , return_offsets_mapping=__lowerCamelCase , return_token_type_ids=__lowerCamelCase , return_length=__lowerCamelCase , verbose=__lowerCamelCase , return_tensors=__lowerCamelCase , **__lowerCamelCase , ) encoding.update(__lowerCamelCase ) __UpperCAmelCase : Dict = self.qformer_tokenizer( text=__lowerCamelCase , add_special_tokens=__lowerCamelCase , padding=__lowerCamelCase , truncation=__lowerCamelCase , max_length=__lowerCamelCase , stride=__lowerCamelCase , pad_to_multiple_of=__lowerCamelCase , return_attention_mask=__lowerCamelCase , return_overflowing_tokens=__lowerCamelCase , return_special_tokens_mask=__lowerCamelCase , return_offsets_mapping=__lowerCamelCase , return_token_type_ids=__lowerCamelCase , return_length=__lowerCamelCase , verbose=__lowerCamelCase , return_tensors=__lowerCamelCase , **__lowerCamelCase , ) __UpperCAmelCase : int = qformer_text_encoding.pop("input_ids" ) __UpperCAmelCase : Optional[int] = qformer_text_encoding.pop("attention_mask" ) if images is not None: __UpperCAmelCase : Union[str, Any] = self.image_processor(__lowerCamelCase , return_tensors=__lowerCamelCase ) encoding.update(__lowerCamelCase ) return encoding def _lowerCamelCase ( self: Any , *__lowerCamelCase: Any , **__lowerCamelCase: Any ) -> Optional[Any]: return self.tokenizer.batch_decode(*__lowerCamelCase , **__lowerCamelCase ) def _lowerCamelCase ( self: Tuple , *__lowerCamelCase: Any , **__lowerCamelCase: Dict ) -> Tuple: return self.tokenizer.decode(*__lowerCamelCase , **__lowerCamelCase ) @property # Copied from transformers.models.blip.processing_blip.BlipProcessor.model_input_names def _lowerCamelCase ( self: List[str] ) -> Tuple: __UpperCAmelCase : str = self.tokenizer.model_input_names __UpperCAmelCase : Dict = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) ) def _lowerCamelCase ( self: Union[str, Any] , __lowerCamelCase: Union[str, Any] , **__lowerCamelCase: Optional[Any] ) -> str: if os.path.isfile(__lowerCamelCase ): raise ValueError(f'''Provided path ({save_directory}) should be a directory, not a file''' ) os.makedirs(__lowerCamelCase , exist_ok=__lowerCamelCase ) __UpperCAmelCase : List[str] = os.path.join(__lowerCamelCase , "qformer_tokenizer" ) self.qformer_tokenizer.save_pretrained(__lowerCamelCase ) return super().save_pretrained(__lowerCamelCase , **__lowerCamelCase ) @classmethod def _lowerCamelCase ( cls: Tuple , __lowerCamelCase: Tuple , **__lowerCamelCase: Optional[int] ) -> Union[str, Any]: __UpperCAmelCase : List[Any] = AutoTokenizer.from_pretrained(__lowerCamelCase , subfolder="qformer_tokenizer" ) __UpperCAmelCase : List[Any] = cls._get_arguments_from_pretrained(__lowerCamelCase , **__lowerCamelCase ) args.append(__lowerCamelCase ) return cls(*__lowerCamelCase )
342
1
import logging import os import threading import time try: import warnings except ImportError: lowerCAmelCase_ = None try: import msvcrt except ImportError: lowerCAmelCase_ = None try: import fcntl except ImportError: lowerCAmelCase_ = None # Backward compatibility # ------------------------------------------------ try: TimeoutError except NameError: lowerCAmelCase_ = OSError # Data # ------------------------------------------------ lowerCAmelCase_ = [ '''Timeout''', '''BaseFileLock''', '''WindowsFileLock''', '''UnixFileLock''', '''SoftFileLock''', '''FileLock''', ] lowerCAmelCase_ = '''3.0.12''' lowerCAmelCase_ = None def lowerCamelCase_ ( ) -> int: """simple docstring""" global _logger snake_case_ : int = _logger or logging.getLogger(__name__ ) return _logger class __lowerCAmelCase ( _a ): def __init__(self , __magic_name__ ) -> Optional[int]: '''simple docstring''' snake_case_ : Any = lock_file return None def __str__(self ) -> Optional[Any]: '''simple docstring''' snake_case_ : int = F'''The file lock \'{self.lock_file}\' could not be acquired.''' return temp class __lowerCAmelCase : def __init__(self , __magic_name__ ) -> Tuple: '''simple docstring''' snake_case_ : Optional[Any] = lock return None def __enter__(self ) -> int: '''simple docstring''' return self.lock def __exit__(self , __magic_name__ , __magic_name__ , __magic_name__ ) -> Dict: '''simple docstring''' self.lock.release() return None class __lowerCAmelCase : def __init__(self , __magic_name__ , __magic_name__=-1 , __magic_name__=None ) -> int: '''simple docstring''' snake_case_ : str = max_filename_length if max_filename_length is not None else 255 # Hash the filename if it's too long snake_case_ : Dict = self.hash_filename_if_too_long(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) # The path to the lock file. snake_case_ : int = lock_file # The file descriptor for the *_lock_file* as it is returned by the # os.open() function. # This file lock is only NOT None, if the object currently holds the # lock. snake_case_ : Dict = None # The default timeout value. snake_case_ : List[Any] = timeout # We use this lock primarily for the lock counter. snake_case_ : List[Any] = threading.Lock() # The lock counter is used for implementing the nested locking # mechanism. Whenever the lock is acquired, the counter is increased and # the lock is only released, when this value is 0 again. snake_case_ : Any = 0 return None @property def lowerCamelCase (self ) -> Optional[int]: '''simple docstring''' return self._lock_file @property def lowerCamelCase (self ) -> Tuple: '''simple docstring''' return self._timeout @timeout.setter def lowerCamelCase (self , __magic_name__ ) -> Dict: '''simple docstring''' snake_case_ : Union[str, Any] = float(SCREAMING_SNAKE_CASE__ ) return None def lowerCamelCase (self ) -> Union[str, Any]: '''simple docstring''' raise NotImplementedError() def lowerCamelCase (self ) -> Dict: '''simple docstring''' raise NotImplementedError() @property def lowerCamelCase (self ) -> Dict: '''simple docstring''' return self._lock_file_fd is not None def lowerCamelCase (self , __magic_name__=None , __magic_name__=0.05 ) -> Any: '''simple docstring''' if timeout is None: snake_case_ : str = self.timeout # Increment the number right at the beginning. # We can still undo it, if something fails. with self._thread_lock: self._lock_counter += 1 snake_case_ : Any = id(self ) snake_case_ : Optional[int] = self._lock_file snake_case_ : str = time.time() try: while True: with self._thread_lock: if not self.is_locked: logger().debug(F'''Attempting to acquire lock {lock_id} on {lock_filename}''' ) self._acquire() if self.is_locked: logger().debug(F'''Lock {lock_id} acquired on {lock_filename}''' ) break elif timeout >= 0 and time.time() - start_time > timeout: logger().debug(F'''Timeout on acquiring lock {lock_id} on {lock_filename}''' ) raise Timeout(self._lock_file ) else: logger().debug( F'''Lock {lock_id} not acquired on {lock_filename}, waiting {poll_intervall} seconds ...''' ) time.sleep(SCREAMING_SNAKE_CASE__ ) except: # noqa # Something did go wrong, so decrement the counter. with self._thread_lock: snake_case_ : str = max(0 , self._lock_counter - 1 ) raise return _Acquire_ReturnProxy(lock=self ) def lowerCamelCase (self , __magic_name__=False ) -> Any: '''simple docstring''' with self._thread_lock: if self.is_locked: self._lock_counter -= 1 if self._lock_counter == 0 or force: snake_case_ : str = id(self ) snake_case_ : int = self._lock_file logger().debug(F'''Attempting to release lock {lock_id} on {lock_filename}''' ) self._release() snake_case_ : Optional[int] = 0 logger().debug(F'''Lock {lock_id} released on {lock_filename}''' ) return None def __enter__(self ) -> Union[str, Any]: '''simple docstring''' self.acquire() return self def __exit__(self , __magic_name__ , __magic_name__ , __magic_name__ ) -> Optional[Any]: '''simple docstring''' self.release() return None def __del__(self ) -> Optional[Any]: '''simple docstring''' self.release(force=SCREAMING_SNAKE_CASE__ ) return None def lowerCamelCase (self , __magic_name__ , __magic_name__ ) -> List[Any]: '''simple docstring''' snake_case_ : Optional[Any] = os.path.basename(SCREAMING_SNAKE_CASE__ ) if len(SCREAMING_SNAKE_CASE__ ) > max_length and max_length > 0: snake_case_ : Optional[int] = os.path.dirname(SCREAMING_SNAKE_CASE__ ) snake_case_ : Optional[Any] = str(hash(SCREAMING_SNAKE_CASE__ ) ) snake_case_ : str = filename[: max_length - len(SCREAMING_SNAKE_CASE__ ) - 8] + '...' + hashed_filename + '.lock' return os.path.join(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) else: return path class __lowerCAmelCase ( _a ): def __init__(self , __magic_name__ , __magic_name__=-1 , __magic_name__=None ) -> List[Any]: '''simple docstring''' from .file_utils import relative_to_absolute_path super().__init__(SCREAMING_SNAKE_CASE__ , timeout=SCREAMING_SNAKE_CASE__ , max_filename_length=SCREAMING_SNAKE_CASE__ ) snake_case_ : Optional[int] = '\\\\?\\' + relative_to_absolute_path(self.lock_file ) def lowerCamelCase (self ) -> str: '''simple docstring''' snake_case_ : Tuple = os.O_RDWR | os.O_CREAT | os.O_TRUNC try: snake_case_ : List[Any] = os.open(self._lock_file , SCREAMING_SNAKE_CASE__ ) except OSError: pass else: try: msvcrt.locking(SCREAMING_SNAKE_CASE__ , msvcrt.LK_NBLCK , 1 ) except OSError: os.close(SCREAMING_SNAKE_CASE__ ) else: snake_case_ : Optional[Any] = fd return None def lowerCamelCase (self ) -> int: '''simple docstring''' snake_case_ : List[Any] = self._lock_file_fd snake_case_ : Optional[Any] = None msvcrt.locking(SCREAMING_SNAKE_CASE__ , msvcrt.LK_UNLCK , 1 ) os.close(SCREAMING_SNAKE_CASE__ ) try: os.remove(self._lock_file ) # Probably another instance of the application # that acquired the file lock. except OSError: pass return None class __lowerCAmelCase ( _a ): def __init__(self , __magic_name__ , __magic_name__=-1 , __magic_name__=None ) -> str: '''simple docstring''' snake_case_ : int = os.statvfs(os.path.dirname(SCREAMING_SNAKE_CASE__ ) ).f_namemax super().__init__(SCREAMING_SNAKE_CASE__ , timeout=SCREAMING_SNAKE_CASE__ , max_filename_length=SCREAMING_SNAKE_CASE__ ) def lowerCamelCase (self ) -> str: '''simple docstring''' snake_case_ : int = os.O_RDWR | os.O_CREAT | os.O_TRUNC snake_case_ : List[str] = os.open(self._lock_file , SCREAMING_SNAKE_CASE__ ) try: fcntl.flock(SCREAMING_SNAKE_CASE__ , fcntl.LOCK_EX | fcntl.LOCK_NB ) except OSError: os.close(SCREAMING_SNAKE_CASE__ ) else: snake_case_ : List[Any] = fd return None def lowerCamelCase (self ) -> List[str]: '''simple docstring''' snake_case_ : List[Any] = self._lock_file_fd snake_case_ : Optional[int] = None fcntl.flock(SCREAMING_SNAKE_CASE__ , fcntl.LOCK_UN ) os.close(SCREAMING_SNAKE_CASE__ ) return None class __lowerCAmelCase ( _a ): def lowerCamelCase (self ) -> Dict: '''simple docstring''' snake_case_ : Optional[int] = os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_TRUNC try: snake_case_ : int = os.open(self._lock_file , SCREAMING_SNAKE_CASE__ ) except OSError: pass else: snake_case_ : Dict = fd return None def lowerCamelCase (self ) -> List[str]: '''simple docstring''' os.close(self._lock_file_fd ) snake_case_ : str = None try: os.remove(self._lock_file ) # The file is already deleted and that's what we want. except OSError: pass return None lowerCAmelCase_ = None if msvcrt: lowerCAmelCase_ = WindowsFileLock elif fcntl: lowerCAmelCase_ = UnixFileLock else: lowerCAmelCase_ = SoftFileLock if warnings is not None: warnings.warn('''only soft file lock is available''')
279
from bisect import bisect from itertools import accumulate def SCREAMING_SNAKE_CASE__ ( lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) -> Union[str, Any]: __lowerCamelCase : Optional[Any] = sorted(zip(lowerCamelCase__ , lowerCamelCase__ ) , key=lambda lowerCamelCase__ : x[0] / x[1] , reverse=lowerCamelCase__ ) __lowerCamelCase , __lowerCamelCase : Any = [i[0] for i in r], [i[1] for i in r] __lowerCamelCase : List[str] = list(accumulate(lowerCamelCase__ ) ) __lowerCamelCase : Union[str, Any] = bisect(lowerCamelCase__ , lowerCamelCase__ ) return ( 0 if k == 0 else sum(vl[:k] ) + (w - acc[k - 1]) * (vl[k]) / (wt[k]) if k != n else sum(vl[:k] ) ) if __name__ == "__main__": import doctest doctest.testmod()
73
0
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_xlnet import XLNetTokenizer else: __magic_name__: Any = None __magic_name__: Dict = logging.get_logger(__name__) __magic_name__: Any = {"vocab_file": "spiece.model", "tokenizer_file": "tokenizer.json"} __magic_name__: str = { "vocab_file": { "xlnet-base-cased": "https://huggingface.co/xlnet-base-cased/resolve/main/spiece.model", "xlnet-large-cased": "https://huggingface.co/xlnet-large-cased/resolve/main/spiece.model", }, "tokenizer_file": { "xlnet-base-cased": "https://huggingface.co/xlnet-base-cased/resolve/main/tokenizer.json", "xlnet-large-cased": "https://huggingface.co/xlnet-large-cased/resolve/main/tokenizer.json", }, } __magic_name__: Optional[Any] = { "xlnet-base-cased": None, "xlnet-large-cased": None, } __magic_name__: Optional[Any] = "▁" # Segments (not really needed) __magic_name__: List[Any] = 0 __magic_name__: Dict = 1 __magic_name__: List[str] = 2 __magic_name__: List[Any] = 3 __magic_name__: Optional[int] = 4 class snake_case__ ( _lowerCAmelCase ): lowercase__ : Dict = VOCAB_FILES_NAMES lowercase__ : Union[str, Any] = PRETRAINED_VOCAB_FILES_MAP lowercase__ : Tuple = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase__ : List[str] = '''left''' lowercase__ : List[str] = XLNetTokenizer def __init__( self , lowerCAmelCase__=None , lowerCAmelCase__=None , lowerCAmelCase__=False , lowerCAmelCase__=True , lowerCAmelCase__=False , lowerCAmelCase__="<s>" , lowerCAmelCase__="</s>" , lowerCAmelCase__="<unk>" , lowerCAmelCase__="<sep>" , lowerCAmelCase__="<pad>" , lowerCAmelCase__="<cls>" , lowerCAmelCase__="<mask>" , lowerCAmelCase__=["<eop>", "<eod>"] , **lowerCAmelCase__ , ) -> Tuple: # Mask token behave like a normal word, i.e. include the space before it __magic_name__ : Optional[int] = AddedToken(lowerCAmelCase__ , lstrip=lowerCAmelCase__ , rstrip=lowerCAmelCase__ ) if isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) else mask_token super().__init__( vocab_file=lowerCAmelCase__ , tokenizer_file=lowerCAmelCase__ , do_lower_case=lowerCAmelCase__ , remove_space=lowerCAmelCase__ , keep_accents=lowerCAmelCase__ , bos_token=lowerCAmelCase__ , eos_token=lowerCAmelCase__ , unk_token=lowerCAmelCase__ , sep_token=lowerCAmelCase__ , pad_token=lowerCAmelCase__ , cls_token=lowerCAmelCase__ , mask_token=lowerCAmelCase__ , additional_special_tokens=lowerCAmelCase__ , **lowerCAmelCase__ , ) __magic_name__ : List[str] = 3 __magic_name__ : str = do_lower_case __magic_name__ : Union[str, Any] = remove_space __magic_name__ : str = keep_accents __magic_name__ : Tuple = vocab_file __magic_name__ : List[Any] = False if not self.vocab_file else True def __magic_name__ ( self , lowerCAmelCase__ , lowerCAmelCase__ = None ) -> List[int]: __magic_name__ : Any = [self.sep_token_id] __magic_name__ : Any = [self.cls_token_id] if token_ids_a is None: return token_ids_a + sep + cls return token_ids_a + sep + token_ids_a + sep + cls def __magic_name__ ( self , lowerCAmelCase__ , lowerCAmelCase__ = None ) -> List[int]: __magic_name__ : List[str] = [self.sep_token_id] __magic_name__ : Optional[Any] = [2] if token_ids_a is None: return len(token_ids_a + sep ) * [0] + cls_segment_id return len(token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] + cls_segment_id def __magic_name__ ( self , lowerCAmelCase__ , lowerCAmelCase__ = None ) -> Tuple[str]: if not self.can_save_slow_tokenizer: raise ValueError( """Your fast tokenizer does not have the necessary information to save the vocabulary for a slow """ """tokenizer.""" ) if not os.path.isdir(lowerCAmelCase__ ): logger.error(F'Vocabulary path ({save_directory}) should be a directory' ) return __magic_name__ : List[str] = os.path.join( lowerCAmelCase__ , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(lowerCAmelCase__ ): copyfile(self.vocab_file , lowerCAmelCase__ ) return (out_vocab_file,)
138
def UpperCamelCase ( _A = 1, _A = 1000 ): """simple docstring""" __magic_name__ : Optional[int] = 1 __magic_name__ : Dict = 0 for divide_by_number in range(_A, digit + 1 ): __magic_name__ : list[int] = [] __magic_name__ : Any = numerator for _ in range(1, digit + 1 ): if now_divide in has_been_divided: if longest_list_length < len(_A ): __magic_name__ : int = len(_A ) __magic_name__ : Dict = divide_by_number else: has_been_divided.append(_A ) __magic_name__ : Optional[int] = now_divide * 10 % divide_by_number return the_digit # Tests if __name__ == "__main__": import doctest doctest.testmod()
138
1
"""simple docstring""" import argparse import logging import os import time import timeit import datasets import numpy as np import pycuda.autoinit # noqa: F401 import pycuda.driver as cuda import tensorrt as trt import torch from absl import logging as absl_logging from accelerate import Accelerator from datasets import load_dataset, load_metric from torch.utils.data import DataLoader from utils_qa import postprocess_qa_predictions import transformers from transformers import AutoTokenizer, EvalPrediction, default_data_collator, set_seed from transformers.trainer_pt_utils import nested_concat, nested_truncate UpperCAmelCase = trt.Logger(trt.Logger.WARNING) UpperCAmelCase = absl_logging.get_absl_logger() absl_logger.setLevel(logging.WARNING) UpperCAmelCase = logging.getLogger(__name__) UpperCAmelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( """--onnx_model_path""", default=None, type=str, required=True, help="""Path to ONNX model: """, ) parser.add_argument( """--output_dir""", default=None, type=str, required=True, help="""The output directory where the model checkpoints and predictions will be written.""", ) # Other parameters parser.add_argument( """--tokenizer_name""", default="""""", type=str, required=True, help="""Pretrained tokenizer name or path if not the same as model_name""", ) parser.add_argument( """--version_2_with_negative""", action="""store_true""", help="""If true, the SQuAD examples contain some that do not have an answer.""", ) parser.add_argument( """--null_score_diff_threshold""", type=float, default=0.0, help="""If null_score - best_non_null is greater than the threshold predict null.""", ) parser.add_argument( """--max_seq_length""", default=384, type=int, help=( """The maximum total input sequence length after WordPiece tokenization. Sequences """ """longer than this will be truncated, and sequences shorter than this will be padded.""" ), ) parser.add_argument( """--doc_stride""", default=128, type=int, help="""When splitting up a long document into chunks, how much stride to take between chunks.""", ) parser.add_argument("""--per_device_eval_batch_size""", default=8, type=int, help="""Batch size per GPU/CPU for evaluation.""") parser.add_argument( """--n_best_size""", default=20, type=int, help="""The total number of n-best predictions to generate in the nbest_predictions.json output file.""", ) parser.add_argument( """--max_answer_length""", default=30, type=int, help=( """The maximum length of an answer that can be generated. This is needed because the start """ """and end predictions are not conditioned on one another.""" ), ) parser.add_argument("""--seed""", type=int, default=42, help="""random seed for initialization""") parser.add_argument( """--dataset_name""", type=str, default=None, required=True, help="""The name of the dataset to use (via the datasets library).""", ) parser.add_argument( """--dataset_config_name""", type=str, default=None, help="""The configuration name of the dataset to use (via the datasets library).""", ) parser.add_argument( """--preprocessing_num_workers""", type=int, default=4, help="""A csv or a json file containing the training data.""" ) parser.add_argument("""--overwrite_cache""", action="""store_true""", help="""Overwrite the cached training and evaluation sets""") parser.add_argument( """--fp16""", action="""store_true""", help="""Whether to use 16-bit (mixed) precision instead of 32-bit""", ) parser.add_argument( """--int8""", action="""store_true""", help="""Whether to use INT8""", ) UpperCAmelCase = parser.parse_args() if args.tokenizer_name: UpperCAmelCase = AutoTokenizer.from_pretrained(args.tokenizer_name, use_fast=True) else: raise ValueError( """You are instantiating a new tokenizer from scratch. This is not supported by this script.""" """You can do it from another script, save it, and load it from here, using --tokenizer_name.""" ) logger.info("""Training/evaluation parameters %s""", args) UpperCAmelCase = args.per_device_eval_batch_size UpperCAmelCase = (args.eval_batch_size, args.max_seq_length) # TRT Engine properties UpperCAmelCase = True UpperCAmelCase = """temp_engine/bert-fp32.engine""" if args.fpaa: UpperCAmelCase = """temp_engine/bert-fp16.engine""" if args.inta: UpperCAmelCase = """temp_engine/bert-int8.engine""" # import ONNX file if not os.path.exists("""temp_engine"""): os.makedirs("""temp_engine""") UpperCAmelCase = 1 << (int)(trt.NetworkDefinitionCreationFlag.EXPLICIT_BATCH) with trt.Builder(TRT_LOGGER) as builder, builder.create_network(EXPLICIT_BATCH) as network, trt.OnnxParser( network, TRT_LOGGER ) as parser: with open(args.onnx_model_path, """rb""") as model: if not parser.parse(model.read()): for error in range(parser.num_errors): print(parser.get_error(error)) # Query input names and shapes from parsed TensorRT network UpperCAmelCase = [network.get_input(i) for i in range(network.num_inputs)] UpperCAmelCase = [_input.name for _input in network_inputs] # ex: ["actual_input1"] with builder.create_builder_config() as config: UpperCAmelCase = 1 << 50 if STRICT_TYPES: config.set_flag(trt.BuilderFlag.STRICT_TYPES) if args.fpaa: config.set_flag(trt.BuilderFlag.FPaa) if args.inta: config.set_flag(trt.BuilderFlag.INTa) UpperCAmelCase = builder.create_optimization_profile() config.add_optimization_profile(profile) for i in range(len(input_names)): profile.set_shape(input_names[i], INPUT_SHAPE, INPUT_SHAPE, INPUT_SHAPE) UpperCAmelCase = builder.build_engine(network, config) # serialize_engine and store in file (can be directly loaded and deserialized): with open(engine_name, """wb""") as f: f.write(engine.serialize()) def lowercase ( a__ : Any , a__ : Optional[int] , a__ : Tuple , a__ : Any , a__ : Any , a__ : List[Any] , a__ : Dict , a__ : Union[str, Any] ) -> Optional[int]: _UpperCamelCase = np.asarray(inputs['''input_ids'''] , dtype=np.intaa ) _UpperCamelCase = np.asarray(inputs['''attention_mask'''] , dtype=np.intaa ) _UpperCamelCase = np.asarray(inputs['''token_type_ids'''] , dtype=np.intaa ) # Copy inputs cuda.memcpy_htod_async(d_inputs[0] , input_ids.ravel() , _lowercase ) cuda.memcpy_htod_async(d_inputs[1] , attention_mask.ravel() , _lowercase ) cuda.memcpy_htod_async(d_inputs[2] , token_type_ids.ravel() , _lowercase ) # start time _UpperCamelCase = time.time() # Run inference context.execute_async( bindings=[int(_lowercase ) for d_inp in d_inputs] + [int(_lowercase ), int(_lowercase )] , stream_handle=stream.handle ) # Transfer predictions back from GPU cuda.memcpy_dtoh_async(_lowercase , _lowercase , _lowercase ) cuda.memcpy_dtoh_async(_lowercase , _lowercase , _lowercase ) # Synchronize the stream and take time stream.synchronize() # end time _UpperCamelCase = time.time() _UpperCamelCase = end_time - start_time _UpperCamelCase = (h_outputa, h_outputa) # print(outputs) return outputs, infer_time # Initialize the accelerator. We will let the accelerator handle device placement for us in this example. UpperCAmelCase = Accelerator() # Make one log on every process with the configuration for debugging. logging.basicConfig( format="""%(asctime)s - %(levelname)s - %(name)s - %(message)s""", datefmt="""%m/%d/%Y %H:%M:%S""", level=logging.INFO, ) # Setup logging, we only want one process per machine to log things on the screen. # accelerator.is_local_main_process is only True for one process per machine. logger.setLevel(logging.INFO if accelerator.is_local_main_process else logging.ERROR) if accelerator.is_local_main_process: datasets.utils.logging.set_verbosity_warning() transformers.utils.logging.set_verbosity_info() else: datasets.utils.logging.set_verbosity_error() transformers.utils.logging.set_verbosity_error() # If passed along, set the training seed now. if args.seed is not None: set_seed(args.seed) # Get the datasets: you can either provide your own CSV/JSON/TXT training and evaluation files (see below) # or just provide the name of one of the public datasets available on the hub at https://huggingface.co/datasets/ # (the dataset will be downloaded automatically from the datasets Hub). # # For CSV/JSON files, this script will use the column called 'text' or the first column if no column called # 'text' is found. You can easily tweak this behavior (see below). if args.dataset_name is not None: # Downloading and loading a dataset from the hub. UpperCAmelCase = load_dataset(args.dataset_name, args.dataset_config_name) else: raise ValueError("""Evaluation requires a dataset name""") # See more about loading any type of standard or custom dataset (from files, python dict, pandas DataFrame, etc) at # https://huggingface.co/docs/datasets/loading_datasets.html. # Preprocessing the datasets. # Preprocessing is slighlty different for training and evaluation. UpperCAmelCase = raw_datasets["""validation"""].column_names UpperCAmelCase = """question""" if """question""" in column_names else column_names[0] UpperCAmelCase = """context""" if """context""" in column_names else column_names[1] UpperCAmelCase = """answers""" if """answers""" in column_names else column_names[2] # Padding side determines if we do (question|context) or (context|question). UpperCAmelCase = tokenizer.padding_side == """right""" if args.max_seq_length > tokenizer.model_max_length: logger.warning( F'''The max_seq_length passed ({args.max_seq_length}) is larger than the maximum length for the''' F'''model ({tokenizer.model_max_length}). Using max_seq_length={tokenizer.model_max_length}.''' ) UpperCAmelCase = min(args.max_seq_length, tokenizer.model_max_length) def lowercase ( a__ : Dict ) -> List[Any]: # Some of the questions have lots of whitespace on the left, which is not useful and will make the # truncation of the context fail (the tokenized question will take a lots of space). So we remove that # left whitespace _UpperCamelCase = [q.lstrip() for q in examples[question_column_name]] # Tokenize our examples with truncation and maybe padding, but keep the overflows using a stride. This results # in one example possible giving several features when a context is long, each of those features having a # context that overlaps a bit the context of the previous feature. _UpperCamelCase = tokenizer( examples[question_column_name if pad_on_right else context_column_name] , examples[context_column_name if pad_on_right else question_column_name] , truncation='''only_second''' if pad_on_right else '''only_first''' , max_length=_lowercase , stride=args.doc_stride , return_overflowing_tokens=_lowercase , return_offsets_mapping=_lowercase , padding='''max_length''' , ) # Since one example might give us several features if it has a long context, we need a map from a feature to # its corresponding example. This key gives us just that. _UpperCamelCase = tokenized_examples.pop('''overflow_to_sample_mapping''' ) # For evaluation, we will need to convert our predictions to substrings of the context, so we keep the # corresponding example_id and we will store the offset mappings. _UpperCamelCase = [] for i in range(len(tokenized_examples['''input_ids'''] ) ): # Grab the sequence corresponding to that example (to know what is the context and what is the question). _UpperCamelCase = tokenized_examples.sequence_ids(_lowercase ) _UpperCamelCase = 1 if pad_on_right else 0 # One example can give several spans, this is the index of the example containing this span of text. _UpperCamelCase = sample_mapping[i] tokenized_examples["example_id"].append(examples['''id'''][sample_index] ) # Set to None the offset_mapping that are not part of the context so it's easy to determine if a token # position is part of the context or not. _UpperCamelCase = [ (o if sequence_ids[k] == context_index else None) for k, o in enumerate(tokenized_examples['''offset_mapping'''][i] ) ] return tokenized_examples UpperCAmelCase = raw_datasets["""validation"""] # Validation Feature Creation UpperCAmelCase = eval_examples.map( prepare_validation_features, batched=True, num_proc=args.preprocessing_num_workers, remove_columns=column_names, load_from_cache_file=not args.overwrite_cache, desc="""Running tokenizer on validation dataset""", ) UpperCAmelCase = default_data_collator UpperCAmelCase = eval_dataset.remove_columns(["""example_id""", """offset_mapping"""]) UpperCAmelCase = DataLoader( eval_dataset_for_model, collate_fn=data_collator, batch_size=args.per_device_eval_batch_size ) def lowercase ( a__ : Union[str, Any] , a__ : Union[str, Any] , a__ : int , a__ : Optional[Any]="eval" ) -> Tuple: # Post-processing: we match the start logits and end logits to answers in the original context. _UpperCamelCase = postprocess_qa_predictions( examples=_lowercase , features=_lowercase , predictions=_lowercase , version_2_with_negative=args.version_2_with_negative , n_best_size=args.n_best_size , max_answer_length=args.max_answer_length , null_score_diff_threshold=args.null_score_diff_threshold , output_dir=args.output_dir , prefix=_lowercase , ) # Format the result to the format the metric expects. if args.version_2_with_negative: _UpperCamelCase = [ {"""id""": k, """prediction_text""": v, """no_answer_probability""": 0.0} for k, v in predictions.items() ] else: _UpperCamelCase = [{"""id""": k, """prediction_text""": v} for k, v in predictions.items()] _UpperCamelCase = [{"""id""": ex["""id"""], """answers""": ex[answer_column_name]} for ex in examples] return EvalPrediction(predictions=_lowercase , label_ids=_lowercase ) UpperCAmelCase = load_metric("""squad_v2""" if args.version_2_with_negative else """squad""") # Evaluation! logger.info("""Loading ONNX model %s for evaluation""", args.onnx_model_path) with open(engine_name, """rb""") as f, trt.Runtime(TRT_LOGGER) as runtime, runtime.deserialize_cuda_engine( f.read() ) as engine, engine.create_execution_context() as context: # setup for TRT inferrence for i in range(len(input_names)): context.set_binding_shape(i, INPUT_SHAPE) assert context.all_binding_shapes_specified def lowercase ( a__ : Dict ) -> Union[str, Any]: return trt.volume(engine.get_binding_shape(_lowercase ) ) * engine.get_binding_dtype(_lowercase ).itemsize # Allocate device memory for inputs and outputs. UpperCAmelCase = [cuda.mem_alloc(binding_nbytes(binding)) for binding in engine if engine.binding_is_input(binding)] # Allocate output buffer UpperCAmelCase = cuda.pagelocked_empty(tuple(context.get_binding_shape(3)), dtype=np.floataa) UpperCAmelCase = cuda.pagelocked_empty(tuple(context.get_binding_shape(4)), dtype=np.floataa) UpperCAmelCase = cuda.mem_alloc(h_outputa.nbytes) UpperCAmelCase = cuda.mem_alloc(h_outputa.nbytes) # Create a stream in which to copy inputs/outputs and run inference. UpperCAmelCase = cuda.Stream() # Evaluation logger.info("""***** Running Evaluation *****""") logger.info(F''' Num examples = {len(eval_dataset)}''') logger.info(F''' Batch size = {args.per_device_eval_batch_size}''') UpperCAmelCase = 0.0 UpperCAmelCase = 0 UpperCAmelCase = timeit.default_timer() UpperCAmelCase = None for step, batch in enumerate(eval_dataloader): UpperCAmelCase = model_infer(batch, context, d_inputs, h_outputa, h_outputa, d_outputa, d_outputa, stream) total_time += infer_time niter += 1 UpperCAmelCase = outputs UpperCAmelCase = torch.tensor(start_logits) UpperCAmelCase = torch.tensor(end_logits) # necessary to pad predictions and labels for being gathered UpperCAmelCase = accelerator.pad_across_processes(start_logits, dim=1, pad_index=-100) UpperCAmelCase = accelerator.pad_across_processes(end_logits, dim=1, pad_index=-100) UpperCAmelCase = (accelerator.gather(start_logits).cpu().numpy(), accelerator.gather(end_logits).cpu().numpy()) UpperCAmelCase = logits if all_preds is None else nested_concat(all_preds, logits, padding_index=-100) if all_preds is not None: UpperCAmelCase = nested_truncate(all_preds, len(eval_dataset)) UpperCAmelCase = timeit.default_timer() - start_time logger.info(""" Evaluation done in total %f secs (%f sec per example)""", evalTime, evalTime / len(eval_dataset)) # Inference time from TRT logger.info("""Average Inference Time = {:.3f} ms""".format(total_time * 1_000 / niter)) logger.info("""Total Inference Time = {:.3f} ms""".format(total_time * 1_000)) logger.info("""Total Number of Inference = %d""", niter) UpperCAmelCase = post_processing_function(eval_examples, eval_dataset, all_preds) UpperCAmelCase = metric.compute(predictions=prediction.predictions, references=prediction.label_ids) logger.info(F'''Evaluation metrics: {eval_metric}''')
256
'''simple docstring''' import unittest import torch from torch import nn from accelerate.test_utils import require_cuda from accelerate.utils.memory import find_executable_batch_size, release_memory def __lowerCamelCase ( ) -> Any: raise RuntimeError("""CUDA out of memory.""" ) class UpperCamelCase_ ( nn.Module ): def __init__( self ) -> Any: super().__init__() UpperCAmelCase : Tuple = nn.Linear(3 , 4 ) UpperCAmelCase : Tuple = nn.BatchNormad(4 ) UpperCAmelCase : int = nn.Linear(4 , 5 ) def _lowercase( self , A ) -> Any: return self.lineara(self.batchnorm(self.lineara(A ) ) ) class UpperCamelCase_ ( unittest.TestCase ): def _lowercase( self ) -> Optional[Any]: UpperCAmelCase : Optional[int] = [] @find_executable_batch_size(starting_batch_size=128 ) def mock_training_loop_function(A ): nonlocal batch_sizes batch_sizes.append(A ) if batch_size != 8: raise_fake_out_of_memory() mock_training_loop_function() self.assertListEqual(A , [128, 64, 32, 16, 8] ) def _lowercase( self ) -> Any: UpperCAmelCase : Optional[Any] = [] @find_executable_batch_size(starting_batch_size=128 ) def mock_training_loop_function(A , A ): nonlocal batch_sizes batch_sizes.append(A ) if batch_size != 8: raise_fake_out_of_memory() return batch_size, arga UpperCAmelCase , UpperCAmelCase : Optional[int] = mock_training_loop_function("""hello""" ) self.assertListEqual(A , [128, 64, 32, 16, 8] ) self.assertListEqual([bs, arga] , [8, """hello"""] ) def _lowercase( self ) -> Any: @find_executable_batch_size(starting_batch_size=0 ) def mock_training_loop_function(A ): pass with self.assertRaises(A ) as cm: mock_training_loop_function() self.assertIn("""No executable batch size found, reached zero.""" , cm.exception.args[0] ) def _lowercase( self ) -> Optional[int]: @find_executable_batch_size(starting_batch_size=16 ) def mock_training_loop_function(A ): if batch_size > 0: raise_fake_out_of_memory() pass with self.assertRaises(A ) as cm: mock_training_loop_function() self.assertIn("""No executable batch size found, reached zero.""" , cm.exception.args[0] ) def _lowercase( self ) -> Optional[Any]: @find_executable_batch_size(starting_batch_size=128 ) def mock_training_loop_function(A , A , A ): if batch_size != 8: raise raise_fake_out_of_memory() with self.assertRaises(A ) as cm: mock_training_loop_function(128 , """hello""" , """world""" ) self.assertIn("""Batch size was passed into `f`""" , cm.exception.args[0] ) self.assertIn("""`f(arg1='hello', arg2='world')""" , cm.exception.args[0] ) def _lowercase( self ) -> int: @find_executable_batch_size(starting_batch_size=16 ) def mock_training_loop_function(A ): raise ValueError("""Oops, we had an error!""" ) with self.assertRaises(A ) as cm: mock_training_loop_function() self.assertIn("""Oops, we had an error!""" , cm.exception.args[0] ) @require_cuda def _lowercase( self ) -> Optional[int]: UpperCAmelCase : Optional[Any] = torch.cuda.memory_allocated() UpperCAmelCase : List[str] = ModelForTest() model.cuda() self.assertGreater(torch.cuda.memory_allocated() , A ) UpperCAmelCase : Tuple = release_memory(A ) self.assertEqual(torch.cuda.memory_allocated() , A )
265
0
import os from collections import deque import torch from torch.utils.data import Dataset class __snake_case ( a ): def __init__( self : List[str] , _snake_case : Dict="" , _snake_case : List[Any]="train"): """simple docstring""" assert os.path.isdir(__a) UpperCAmelCase_ = [] UpperCAmelCase_ = os.listdir(__a) for story_filename in story_filenames_list: if "summary" in story_filename: continue UpperCAmelCase_ = os.path.join(__a , __a) if not os.path.isfile(__a): continue self.documents.append(__a) def __len__( self : Union[str, Any]): """simple docstring""" return len(self.documents) def __getitem__( self : int , _snake_case : int): """simple docstring""" UpperCAmelCase_ = self.documents[idx] UpperCAmelCase_ = document_path.split('''/''')[-1] with open(__a , encoding='''utf-8''') as source: UpperCAmelCase_ = source.read() UpperCAmelCase_ , UpperCAmelCase_ = process_story(__a) return document_name, story_lines, summary_lines def A (__A : Dict ) -> Tuple: """simple docstring""" UpperCAmelCase_ = list(filter(lambda __A : len(__snake_case ) != 0 , [line.strip() for line in raw_story.split('''\n''' )] ) ) # for some unknown reason some lines miss a period, add it UpperCAmelCase_ = [_add_missing_period(__snake_case ) for line in nonempty_lines] # gather article lines UpperCAmelCase_ = [] UpperCAmelCase_ = deque(__snake_case ) while True: try: UpperCAmelCase_ = lines.popleft() if element.startswith('''@highlight''' ): break story_lines.append(__snake_case ) except IndexError: # if "@highlight" is absent from the file we pop # all elements until there is None, raising an exception. return story_lines, [] # gather summary lines UpperCAmelCase_ = list(filter(lambda __A : not t.startswith('''@highlight''' ) , __snake_case ) ) return story_lines, summary_lines def A (__A : Union[str, Any] ) -> Union[str, Any]: """simple docstring""" UpperCAmelCase_ = ['''.''', '''!''', '''?''', '''...''', '''\'''', '''`''', '''"''', '''\u2019''', '''\u2019''', ''')'''] if line.startswith('''@highlight''' ): return line if line[-1] in END_TOKENS: return line return line + "." def A (__A : int , __A : Optional[Any] , __A : List[str] ) -> Optional[Any]: """simple docstring""" if len(__snake_case ) > block_size: return sequence[:block_size] else: sequence.extend([pad_token_id] * (block_size - len(__snake_case )) ) return sequence def A (__A : Optional[int] , __A : Union[str, Any] ) -> List[Any]: """simple docstring""" UpperCAmelCase_ = torch.ones_like(__snake_case ) UpperCAmelCase_ = sequence == pad_token_id UpperCAmelCase_ = 0 return mask def A (__A : Union[str, Any] , __A : Dict , __A : str ) -> Any: """simple docstring""" UpperCAmelCase_ = [tokenizer.encode(__snake_case ) for line in story_lines] UpperCAmelCase_ = [token for sentence in story_lines_token_ids for token in sentence] UpperCAmelCase_ = [tokenizer.encode(__snake_case ) for line in summary_lines] UpperCAmelCase_ = [token for sentence in summary_lines_token_ids for token in sentence] return story_token_ids, summary_token_ids def A (__A : Any , __A : Any ) -> Dict: """simple docstring""" UpperCAmelCase_ = [] for sequence in batch: UpperCAmelCase_ = -1 UpperCAmelCase_ = [] for s in sequence: if s == separator_token_id: sentence_num += 1 embeddings.append(sentence_num % 2 ) batch_embeddings.append(__snake_case ) return torch.tensor(__snake_case )
361
import json import os import shutil 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 AutoConfig, BertConfig, GPTaConfig from transformers.configuration_utils import PretrainedConfig from transformers.testing_utils import TOKEN, USER, is_staging_test sys.path.append(str(Path(__file__).parent.parent / "utils")) from test_module.custom_configuration import CustomConfig # noqa E402 snake_case_ : List[str] = { "return_dict": False, "output_hidden_states": True, "output_attentions": True, "torchscript": True, "torch_dtype": "float16", "use_bfloat16": True, "tf_legacy_loss": True, "pruned_heads": {"a": 1}, "tie_word_embeddings": False, "is_decoder": True, "cross_attention_hidden_size": 128, "add_cross_attention": True, "tie_encoder_decoder": True, "max_length": 50, "min_length": 3, "do_sample": True, "early_stopping": True, "num_beams": 3, "num_beam_groups": 3, "diversity_penalty": 0.5, "temperature": 2.0, "top_k": 10, "top_p": 0.7, "typical_p": 0.2, "repetition_penalty": 0.8, "length_penalty": 0.8, "no_repeat_ngram_size": 5, "encoder_no_repeat_ngram_size": 5, "bad_words_ids": [1, 2, 3], "num_return_sequences": 3, "chunk_size_feed_forward": 5, "output_scores": True, "return_dict_in_generate": True, "forced_bos_token_id": 2, "forced_eos_token_id": 3, "remove_invalid_values": True, "architectures": ["BertModel"], "finetuning_task": "translation", "id2label": {0: "label"}, "label2id": {"label": "0"}, "tokenizer_class": "BertTokenizerFast", "prefix": "prefix", "bos_token_id": 6, "pad_token_id": 7, "eos_token_id": 8, "sep_token_id": 9, "decoder_start_token_id": 10, "exponential_decay_length_penalty": (5, 1.01), "suppress_tokens": [0, 1], "begin_suppress_tokens": 2, "task_specific_params": {"translation": "some_params"}, "problem_type": "regression", } @is_staging_test class __snake_case ( unittest.TestCase ): @classmethod def lowerCamelCase ( cls : Optional[Any]): """simple docstring""" UpperCAmelCase_ = TOKEN HfFolder.save_token(_snake_case) @classmethod def lowerCamelCase ( cls : List[str]): """simple docstring""" try: delete_repo(token=cls._token , repo_id='''test-config''') except HTTPError: pass try: delete_repo(token=cls._token , repo_id='''valid_org/test-config-org''') except HTTPError: pass try: delete_repo(token=cls._token , repo_id='''test-dynamic-config''') except HTTPError: pass def lowerCamelCase ( self : Optional[Any]): """simple docstring""" UpperCAmelCase_ = BertConfig( vocab_size=99 , hidden_size=32 , num_hidden_layers=5 , num_attention_heads=4 , intermediate_size=37) config.push_to_hub('''test-config''' , use_auth_token=self._token) UpperCAmelCase_ = BertConfig.from_pretrained(F"""{USER}/test-config""") for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(_snake_case , getattr(_snake_case , _snake_case)) # Reset repo delete_repo(token=self._token , repo_id='''test-config''') # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained(_snake_case , repo_id='''test-config''' , push_to_hub=_snake_case , use_auth_token=self._token) UpperCAmelCase_ = BertConfig.from_pretrained(F"""{USER}/test-config""") for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(_snake_case , getattr(_snake_case , _snake_case)) def lowerCamelCase ( self : Tuple): """simple docstring""" UpperCAmelCase_ = BertConfig( vocab_size=99 , hidden_size=32 , num_hidden_layers=5 , num_attention_heads=4 , intermediate_size=37) config.push_to_hub('''valid_org/test-config-org''' , use_auth_token=self._token) UpperCAmelCase_ = BertConfig.from_pretrained('''valid_org/test-config-org''') for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(_snake_case , getattr(_snake_case , _snake_case)) # Reset repo delete_repo(token=self._token , repo_id='''valid_org/test-config-org''') # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained( _snake_case , repo_id='''valid_org/test-config-org''' , push_to_hub=_snake_case , use_auth_token=self._token) UpperCAmelCase_ = BertConfig.from_pretrained('''valid_org/test-config-org''') for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(_snake_case , getattr(_snake_case , _snake_case)) def lowerCamelCase ( self : Union[str, Any]): """simple docstring""" CustomConfig.register_for_auto_class() UpperCAmelCase_ = CustomConfig(attribute=42) config.push_to_hub('''test-dynamic-config''' , use_auth_token=self._token) # This has added the proper auto_map field to the config self.assertDictEqual(config.auto_map , {'''AutoConfig''': '''custom_configuration.CustomConfig'''}) UpperCAmelCase_ = AutoConfig.from_pretrained(F"""{USER}/test-dynamic-config""" , trust_remote_code=_snake_case) # Can't make an isinstance check because the new_config is from the FakeConfig class of a dynamic module self.assertEqual(new_config.__class__.__name__ , '''CustomConfig''') self.assertEqual(new_config.attribute , 42) class __snake_case ( unittest.TestCase ): def lowerCamelCase ( self : Optional[Any]): """simple docstring""" UpperCAmelCase_ = GPTaConfig() # attempt to modify each of int/float/bool/str config records and verify they were updated UpperCAmelCase_ = c.n_embd + 1 # int UpperCAmelCase_ = c.resid_pdrop + 1.0 # float UpperCAmelCase_ = not c.scale_attn_weights # bool UpperCAmelCase_ = c.summary_type + '''foo''' # str c.update_from_string( F"""n_embd={n_embd},resid_pdrop={resid_pdrop},scale_attn_weights={scale_attn_weights},summary_type={summary_type}""") self.assertEqual(_snake_case , c.n_embd , '''mismatch for key: n_embd''') self.assertEqual(_snake_case , c.resid_pdrop , '''mismatch for key: resid_pdrop''') self.assertEqual(_snake_case , c.scale_attn_weights , '''mismatch for key: scale_attn_weights''') self.assertEqual(_snake_case , c.summary_type , '''mismatch for key: summary_type''') def lowerCamelCase ( self : Dict): """simple docstring""" UpperCAmelCase_ = PretrainedConfig() UpperCAmelCase_ = [key for key in base_config.__dict__ if key not in config_common_kwargs] # If this part of the test fails, you have arguments to addin config_common_kwargs above. self.assertListEqual( _snake_case , ['''is_encoder_decoder''', '''_name_or_path''', '''_commit_hash''', '''transformers_version''']) UpperCAmelCase_ = [key for key, value in config_common_kwargs.items() if value == getattr(_snake_case , _snake_case)] if len(_snake_case) > 0: raise ValueError( '''The following keys are set with the default values in''' ''' `test_configuration_common.config_common_kwargs` pick another value for them:''' F""" {", ".join(_snake_case)}.""") def lowerCamelCase ( self : str): """simple docstring""" with self.assertRaises(_snake_case): # config is in subfolder, the following should not work without specifying the subfolder UpperCAmelCase_ = BertConfig.from_pretrained('''hf-internal-testing/tiny-random-bert-subfolder''') UpperCAmelCase_ = BertConfig.from_pretrained('''hf-internal-testing/tiny-random-bert-subfolder''' , subfolder='''bert''') self.assertIsNotNone(_snake_case) def lowerCamelCase ( self : Any): """simple docstring""" UpperCAmelCase_ = mock.Mock() UpperCAmelCase_ = 500 UpperCAmelCase_ = {} UpperCAmelCase_ = HTTPError UpperCAmelCase_ = {} # Download this model to make sure it's in the cache. UpperCAmelCase_ = BertConfig.from_pretrained('''hf-internal-testing/tiny-random-bert''') # Under the mock environment we get a 500 error when trying to reach the model. with mock.patch('''requests.Session.request''' , return_value=_snake_case) as mock_head: UpperCAmelCase_ = BertConfig.from_pretrained('''hf-internal-testing/tiny-random-bert''') # This check we did call the fake head request mock_head.assert_called() def lowerCamelCase ( self : List[str]): """simple docstring""" UpperCAmelCase_ = BertConfig.from_pretrained( '''https://huggingface.co/hf-internal-testing/tiny-random-bert/resolve/main/config.json''') def lowerCamelCase ( self : str): """simple docstring""" UpperCAmelCase_ = AutoConfig.from_pretrained('''bert-base-cased''') UpperCAmelCase_ = ['''config.4.0.0.json'''] with tempfile.TemporaryDirectory() as tmp_dir: configuration.save_pretrained(_snake_case) UpperCAmelCase_ = 2 json.dump(configuration.to_dict() , open(os.path.join(_snake_case , '''config.4.0.0.json''') , '''w''')) # This should pick the new configuration file as the version of Transformers is > 4.0.0 UpperCAmelCase_ = AutoConfig.from_pretrained(_snake_case) self.assertEqual(new_configuration.hidden_size , 2) # Will need to be adjusted if we reach v42 and this test is still here. # Should pick the old configuration file as the version of Transformers is < 4.42.0 UpperCAmelCase_ = ['''config.42.0.0.json'''] UpperCAmelCase_ = 768 configuration.save_pretrained(_snake_case) shutil.move(os.path.join(_snake_case , '''config.4.0.0.json''') , os.path.join(_snake_case , '''config.42.0.0.json''')) UpperCAmelCase_ = AutoConfig.from_pretrained(_snake_case) self.assertEqual(new_configuration.hidden_size , 768) def lowerCamelCase ( self : Dict): """simple docstring""" UpperCAmelCase_ = '''hf-internal-testing/test-two-configs''' import transformers as new_transformers UpperCAmelCase_ = '''v4.0.0''' UpperCAmelCase_ , UpperCAmelCase_ = new_transformers.models.auto.AutoConfig.from_pretrained( _snake_case , return_unused_kwargs=_snake_case) self.assertEqual(new_configuration.hidden_size , 2) # This checks `_configuration_file` ia not kept in the kwargs by mistake. self.assertDictEqual(_snake_case , {}) # Testing an older version by monkey-patching the version in the module it's used. import transformers as old_transformers UpperCAmelCase_ = '''v3.0.0''' UpperCAmelCase_ = old_transformers.models.auto.AutoConfig.from_pretrained(_snake_case) self.assertEqual(old_configuration.hidden_size , 768)
7
0
"""simple docstring""" import darl # noqa import gym import tqdm from diffusers.experimental import ValueGuidedRLPipeline __UpperCamelCase : Optional[int] = { '''n_samples''': 6_4, '''horizon''': 3_2, '''num_inference_steps''': 2_0, '''n_guide_steps''': 2, # can set to 0 for faster sampling, does not use value network '''scale_grad_by_std''': True, '''scale''': 0.1, '''eta''': 0.0, '''t_grad_cutoff''': 2, '''device''': '''cpu''', } if __name__ == "__main__": __UpperCamelCase : Optional[int] = '''hopper-medium-v2''' __UpperCamelCase : Optional[int] = gym.make(env_name) __UpperCamelCase : List[Any] = ValueGuidedRLPipeline.from_pretrained( '''bglick13/hopper-medium-v2-value-function-hor32''', env=env, ) env.seed(0) __UpperCamelCase : List[Any] = env.reset() __UpperCamelCase : Optional[Any] = 0 __UpperCamelCase : Any = 0 __UpperCamelCase : Any = 1_0_0_0 __UpperCamelCase : Dict = [obs.copy()] try: for t in tqdm.tqdm(range(T)): # call the policy __UpperCamelCase : Optional[int] = pipeline(obs, planning_horizon=3_2) # execute action in environment __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase : Optional[Any] = env.step(denorm_actions) __UpperCamelCase : Optional[Any] = env.get_normalized_score(total_reward) # update return total_reward += reward total_score += score print( F'''Step: {t}, Reward: {reward}, Total Reward: {total_reward}, Score: {score}, Total Score:''' F''' {total_score}''' ) # save observations for rendering rollout.append(next_observation.copy()) __UpperCamelCase : str = next_observation except KeyboardInterrupt: pass print(F'''Total reward: {total_reward}''')
106
'''simple docstring''' import argparse import json import os import fairseq import torch from torch import nn from transformers import ( SpeechaTextaConfig, SpeechaTextaForCausalLM, SpeechaTextaTokenizer, SpeechEncoderDecoderConfig, SpeechEncoderDecoderModel, WavaVecaConfig, WavaVecaFeatureExtractor, WavaVecaModel, logging, ) logging.set_verbosity_info() __lowercase : Union[str, Any] = logging.get_logger(__name__) __lowercase : Optional[int] = { 'post_extract_proj': 'feature_projection.projection', 'encoder.pos_conv.0': 'encoder.pos_conv_embed.conv', 'self_attn.k_proj': 'encoder.layers.*.attention.k_proj', 'self_attn.v_proj': 'encoder.layers.*.attention.v_proj', 'self_attn.q_proj': 'encoder.layers.*.attention.q_proj', 'self_attn.out_proj': 'encoder.layers.*.attention.out_proj', 'self_attn_layer_norm': 'encoder.layers.*.layer_norm', 'fc1': 'encoder.layers.*.feed_forward.intermediate_dense', 'fc2': 'encoder.layers.*.feed_forward.output_dense', 'final_layer_norm': 'encoder.layers.*.final_layer_norm', 'encoder.layer_norm': 'encoder.layer_norm', 'w2v_model.layer_norm': 'feature_projection.layer_norm', 'quantizer.weight_proj': 'quantizer.weight_proj', 'quantizer.vars': 'quantizer.codevectors', 'project_q': 'project_q', 'final_proj': 'project_hid', 'w2v_encoder.proj': 'lm_head', 'mask_emb': 'masked_spec_embed', } __lowercase : Optional[Any] = [ 'lm_head', 'quantizer.weight_proj', 'quantizer.codevectors', 'project_q', 'project_hid', ] def lowerCamelCase (_SCREAMING_SNAKE_CASE : Tuple , _SCREAMING_SNAKE_CASE : Union[str, Any] , _SCREAMING_SNAKE_CASE : Any , _SCREAMING_SNAKE_CASE : List[str] , _SCREAMING_SNAKE_CASE : Dict ): for attribute in key.split('.' ): __a : Any = getattr(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) if weight_type is not None: __a : List[Any] = getattr(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ).shape else: __a : Any = hf_pointer.shape assert hf_shape == value.shape, ( F"""Shape of hf {key + "." + weight_type if weight_type is not None else ""} is {hf_shape}, but should be""" F""" {value.shape} for {full_name}""" ) if weight_type == "weight": __a : Tuple = value elif weight_type == "weight_g": __a : str = value elif weight_type == "weight_v": __a : Optional[Any] = value elif weight_type == "bias": __a : Union[str, Any] = value else: __a : List[Any] = value logger.info(F"""{key + "." + weight_type if weight_type is not None else ""} was initialized from {full_name}.""" ) def lowerCamelCase (_SCREAMING_SNAKE_CASE : Dict , _SCREAMING_SNAKE_CASE : Optional[Any] ): __a : int = [] __a : List[str] = fairseq_model.state_dict() __a : Tuple = hf_model.feature_extractor # if encoder has different dim to decoder -> use proj_weight __a : int = None for name, value in fairseq_dict.items(): __a : List[str] = False if "conv_layers" in name: load_conv_layer( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , hf_model.config.feat_extract_norm == 'group' , ) __a : List[str] = True elif name.split('.' )[0] == "proj": __a : Tuple = fairseq_model.proj __a : str = True else: for key, mapped_key in MAPPING.items(): if key in name or key.split('w2v_model.' )[-1] == name.split('.' )[0]: __a : List[Any] = True if "*" in mapped_key: __a : str = name.split(_SCREAMING_SNAKE_CASE )[0].split('.' )[-2] __a : int = mapped_key.replace('*' , _SCREAMING_SNAKE_CASE ) if "weight_g" in name: __a : List[Any] = 'weight_g' elif "weight_v" in name: __a : List[Any] = 'weight_v' elif "bias" in name: __a : Optional[Any] = 'bias' elif "weight" in name: __a : Tuple = 'weight' else: __a : Optional[Any] = None set_recursively(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) continue if not is_used: unused_weights.append(_SCREAMING_SNAKE_CASE ) logger.warning(F"""Unused weights: {unused_weights}""" ) return proj_weight def lowerCamelCase (_SCREAMING_SNAKE_CASE : Any , _SCREAMING_SNAKE_CASE : Dict , _SCREAMING_SNAKE_CASE : Optional[int] , _SCREAMING_SNAKE_CASE : List[Any] , _SCREAMING_SNAKE_CASE : Optional[Any] ): __a : List[str] = full_name.split('conv_layers.' )[-1] __a : Any = name.split('.' ) __a : List[str] = int(items[0] ) __a : List[Any] = int(items[1] ) if type_id == 0: if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.bias.data.shape, ( F"""{full_name} has size {value.shape}, but""" F""" {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found.""" ) __a : List[str] = value logger.info(F"""Feat extract conv layer {layer_id} was initialized from {full_name}.""" ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.weight.data.shape, ( F"""{full_name} has size {value.shape}, but""" F""" {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found.""" ) __a : str = value logger.info(F"""Feat extract conv layer {layer_id} was initialized from {full_name}.""" ) elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm): if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape, ( F"""{full_name} has size {value.shape}, but {feature_extractor[layer_id].layer_norm.bias.data.shape} was""" " found." ) __a : Tuple = value logger.info(F"""Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.""" ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape, ( F"""{full_name} has size {value.shape}, but""" F""" {feature_extractor[layer_id].layer_norm.weight.data.shape} was found.""" ) __a : List[Any] = value logger.info(F"""Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.""" ) else: unused_weights.append(_SCREAMING_SNAKE_CASE ) def lowerCamelCase (_SCREAMING_SNAKE_CASE : int ): __a , __a : List[str] = emb.weight.shape __a : str = nn.Linear(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , bias=_SCREAMING_SNAKE_CASE ) __a : Optional[int] = emb.weight.data return lin_layer def lowerCamelCase (_SCREAMING_SNAKE_CASE : Any ): with open(_SCREAMING_SNAKE_CASE , 'r' , encoding='utf-8' ) as f: __a : Union[str, Any] = f.readlines() __a : Tuple = [line.split(' ' )[0] for line in lines] __a : int = len(_SCREAMING_SNAKE_CASE ) __a : List[Any] = { '<s>': 0, '<pad>': 1, '</s>': 2, '<unk>': 3, } vocab_dict.update(dict(zip(_SCREAMING_SNAKE_CASE , range(4 , num_words + 4 ) ) ) ) return vocab_dict @torch.no_grad() def lowerCamelCase (_SCREAMING_SNAKE_CASE : List[Any] , _SCREAMING_SNAKE_CASE : List[Any] , _SCREAMING_SNAKE_CASE : str , _SCREAMING_SNAKE_CASE : Any , _SCREAMING_SNAKE_CASE : str , _SCREAMING_SNAKE_CASE : Any , _SCREAMING_SNAKE_CASE : Optional[int] , ): __a : Optional[int] = WavaVecaConfig.from_pretrained(_SCREAMING_SNAKE_CASE ) __a : Any = SpeechaTextaConfig.from_pretrained( _SCREAMING_SNAKE_CASE , vocab_size=_SCREAMING_SNAKE_CASE , decoder_layers=_SCREAMING_SNAKE_CASE , do_stable_layer_norm=_SCREAMING_SNAKE_CASE ) __a : Dict = WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=16_000 , padding_value=0 , do_normalize=_SCREAMING_SNAKE_CASE , return_attention_mask=_SCREAMING_SNAKE_CASE , ) __a , __a , __a : List[Any] = fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] , arg_overrides={'data': '/'.join(dict_path.split('/' )[:-1] )} ) __a : Optional[int] = model[0].eval() # set weights for wav2vec2 encoder __a : Tuple = WavaVecaModel(_SCREAMING_SNAKE_CASE ) __a : int = recursively_load_weights_wavaveca(model.encoder , _SCREAMING_SNAKE_CASE ) __a : Dict = SpeechaTextaForCausalLM(_SCREAMING_SNAKE_CASE ) __a , __a : Optional[int] = hf_decoder.model.decoder.load_state_dict(model.decoder.state_dict() , strict=_SCREAMING_SNAKE_CASE ) # set output linear layer unexpected_keys.remove('embed_out' ) __a : Optional[Any] = nn.Parameter(model.decoder.embed_out.detach() ) # layer norm is init to identity matrix so leaving it is fine logger.warning(F"""The following keys are missing when loading the decoder weights: {missing_keys}""" ) logger.warning(F"""The following keys are unexpected when loading the decoder weights: {unexpected_keys}""" ) __a : Tuple = SpeechEncoderDecoderModel(encoder=_SCREAMING_SNAKE_CASE , decoder=_SCREAMING_SNAKE_CASE ) __a : int = False # add projection layer __a : str = nn.Parameter(projection_layer.weight ) __a : Any = nn.Parameter(projection_layer.bias ) __a : str = create_vocab_dict(_SCREAMING_SNAKE_CASE ) with open(os.path.join(_SCREAMING_SNAKE_CASE , 'vocab.json' ) , 'w' ) as fp: json.dump(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) __a : Optional[Any] = SpeechaTextaTokenizer(os.path.join(_SCREAMING_SNAKE_CASE , 'vocab.json' ) ) tokenizer.save_pretrained(_SCREAMING_SNAKE_CASE ) __a : Union[str, Any] = hf_wavavec.config.to_dict() __a : Tuple = tokenizer.pad_token_id __a : Optional[int] = tokenizer.bos_token_id __a : Union[str, Any] = tokenizer.eos_token_id __a : Tuple = 'speech_to_text_2' __a : Tuple = 'wav2vec2' __a : List[str] = SpeechEncoderDecoderConfig.from_dict(_SCREAMING_SNAKE_CASE ) hf_wavavec.save_pretrained(_SCREAMING_SNAKE_CASE ) feature_extractor.save_pretrained(_SCREAMING_SNAKE_CASE ) if __name__ == "__main__": __lowercase : Dict = argparse.ArgumentParser() parser.add_argument('--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.') parser.add_argument('--checkpoint_path', default=None, type=str, help='Path to fairseq checkpoint') parser.add_argument('--dict_path', default=None, type=str, help='Path to dict of fine-tuned model') parser.add_argument( '--encoder_config_path', default='facebook/wav2vec2-large-lv60', type=str, help='Path to hf encoder wav2vec2 checkpoint config', ) parser.add_argument( '--decoder_config_path', default='facebook/s2t-small-mustc-en-fr-st', type=str, help='Path to hf decoder s2t checkpoint config', ) parser.add_argument('--vocab_size', default=1_02_24, type=int, help='Vocab size of decoder') parser.add_argument('--num_decoder_layers', default=7, type=int, help='Number of decoder layers') __lowercase : Tuple = parser.parse_args() convert_wavaveca_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.dict_path, encoder_config_path=args.encoder_config_path, decoder_config_path=args.decoder_config_path, vocab_size=args.vocab_size, num_decoder_layers=args.num_decoder_layers, )
27
0
"""simple docstring""" import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, PNDMScheduler, StableDiffusionLDMaDPipeline, UNetaDConditionModel, ) from diffusers.utils import nightly, 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 enable_full_determinism() class _lowercase ( unittest.TestCase ): """simple docstring""" lowercase__ = StableDiffusionLDMaDPipeline lowercase__ = TEXT_TO_IMAGE_PARAMS lowercase__ = TEXT_TO_IMAGE_BATCH_PARAMS lowercase__ = TEXT_TO_IMAGE_IMAGE_PARAMS def UpperCAmelCase_ ( self : Optional[int] ) -> List[Any]: '''simple docstring''' torch.manual_seed(0 ) __UpperCamelCase =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 , ) __UpperCamelCase =DDIMScheduler( beta_start=0.0_00_85 , beta_end=0.0_12 , beta_schedule='''scaled_linear''' , clip_sample=UpperCamelCase__ , set_alpha_to_one=UpperCamelCase__ , ) torch.manual_seed(0 ) __UpperCamelCase =AutoencoderKL( block_out_channels=[32, 64] , in_channels=6 , out_channels=6 , down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''] , up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''] , latent_channels=4 , ) torch.manual_seed(0 ) __UpperCamelCase =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 , ) __UpperCamelCase =CLIPTextModel(UpperCamelCase__ ) __UpperCamelCase =CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) __UpperCamelCase ={ '''unet''': unet, '''scheduler''': scheduler, '''vae''': vae, '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''safety_checker''': None, '''feature_extractor''': None, } return components def UpperCAmelCase_ ( self : Dict , UpperCamelCase__ : str , UpperCamelCase__ : str=0 ) -> Union[str, Any]: '''simple docstring''' if str(UpperCamelCase__ ).startswith('''mps''' ): __UpperCamelCase =torch.manual_seed(UpperCamelCase__ ) else: __UpperCamelCase =torch.Generator(device=UpperCamelCase__ ).manual_seed(UpperCamelCase__ ) __UpperCamelCase ={ '''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 UpperCAmelCase_ ( self : int ) -> Any: '''simple docstring''' __UpperCamelCase ='''cpu''' # ensure determinism for the device-dependent torch.Generator __UpperCamelCase =self.get_dummy_components() __UpperCamelCase =StableDiffusionLDMaDPipeline(**UpperCamelCase__ ) __UpperCamelCase =ldmad_pipe.to(UpperCamelCase__ ) ldmad_pipe.set_progress_bar_config(disable=UpperCamelCase__ ) __UpperCamelCase =self.get_dummy_inputs(UpperCamelCase__ ) __UpperCamelCase =ldmad_pipe(**UpperCamelCase__ ) __UpperCamelCase , __UpperCamelCase =output.rgb, output.depth __UpperCamelCase =rgb[0, -3:, -3:, -1] __UpperCamelCase =depth[0, -3:, -1] assert rgb.shape == (1, 64, 64, 3) assert depth.shape == (1, 64, 64) __UpperCamelCase =np.array( [0.37_33_81_76, 0.7_02_47, 0.74_20_31_93, 0.51_64_36_04, 0.58_25_67_93, 0.60_93_21_36, 0.4_18_10_95, 0.48_35_58_77, 0.46_53_52_62] ) __UpperCamelCase =np.array([103.46727, 85.81_20_04, 87.84_92_36] ) assert np.abs(image_slice_rgb.flatten() - expected_slice_rgb ).max() < 1E-2 assert np.abs(image_slice_depth.flatten() - expected_slice_depth ).max() < 1E-2 def UpperCAmelCase_ ( self : Any ) -> Union[str, Any]: '''simple docstring''' __UpperCamelCase =self.get_dummy_components() __UpperCamelCase =StableDiffusionLDMaDPipeline(**UpperCamelCase__ ) __UpperCamelCase =ldmad_pipe.to(UpperCamelCase__ ) ldmad_pipe.set_progress_bar_config(disable=UpperCamelCase__ ) __UpperCamelCase =self.get_dummy_inputs(UpperCamelCase__ ) __UpperCamelCase =3 * [inputs['''prompt''']] # forward __UpperCamelCase =ldmad_pipe(**UpperCamelCase__ ) __UpperCamelCase , __UpperCamelCase =output.rgb, output.depth __UpperCamelCase =rgb_slice_a[0, -3:, -3:, -1] __UpperCamelCase =depth_slice_a[0, -3:, -1] __UpperCamelCase =self.get_dummy_inputs(UpperCamelCase__ ) __UpperCamelCase =3 * [inputs.pop('''prompt''' )] __UpperCamelCase =ldmad_pipe.tokenizer( UpperCamelCase__ , padding='''max_length''' , max_length=ldmad_pipe.tokenizer.model_max_length , truncation=UpperCamelCase__ , return_tensors='''pt''' , ) __UpperCamelCase =text_inputs['''input_ids'''].to(UpperCamelCase__ ) __UpperCamelCase =ldmad_pipe.text_encoder(UpperCamelCase__ )[0] __UpperCamelCase =prompt_embeds # forward __UpperCamelCase =ldmad_pipe(**UpperCamelCase__ ) __UpperCamelCase , __UpperCamelCase =output.rgb, output.depth __UpperCamelCase =rgb_slice_a[0, -3:, -3:, -1] __UpperCamelCase =depth_slice_a[0, -3:, -1] assert np.abs(rgb_slice_a.flatten() - rgb_slice_a.flatten() ).max() < 1E-4 assert np.abs(depth_slice_a.flatten() - depth_slice_a.flatten() ).max() < 1E-4 def UpperCAmelCase_ ( self : List[str] ) -> List[str]: '''simple docstring''' __UpperCamelCase ='''cpu''' # ensure determinism for the device-dependent torch.Generator __UpperCamelCase =self.get_dummy_components() __UpperCamelCase =PNDMScheduler(skip_prk_steps=UpperCamelCase__ ) __UpperCamelCase =StableDiffusionLDMaDPipeline(**UpperCamelCase__ ) __UpperCamelCase =ldmad_pipe.to(UpperCamelCase__ ) ldmad_pipe.set_progress_bar_config(disable=UpperCamelCase__ ) __UpperCamelCase =self.get_dummy_inputs(UpperCamelCase__ ) __UpperCamelCase ='''french fries''' __UpperCamelCase =ldmad_pipe(**UpperCamelCase__ , negative_prompt=UpperCamelCase__ ) __UpperCamelCase , __UpperCamelCase =output.rgb, output.depth __UpperCamelCase =rgb[0, -3:, -3:, -1] __UpperCamelCase =depth[0, -3:, -1] assert rgb.shape == (1, 64, 64, 3) assert depth.shape == (1, 64, 64) __UpperCamelCase =np.array( [0.3_70_44, 0.71_81_15_03, 0.7_22_32_51, 0.48_60_36_75, 0.5_63_83_91, 0.6_36_49_48, 0.42_83_37_04, 0.4_90_13_15, 0.47_92_62_17] ) __UpperCamelCase =np.array([107.84738, 84.6_28_02, 89.96_21_35] ) assert np.abs(rgb_slice.flatten() - expected_slice_rgb ).max() < 1E-2 assert np.abs(depth_slice.flatten() - expected_slice_depth ).max() < 1E-2 @slow @require_torch_gpu class _lowercase ( unittest.TestCase ): """simple docstring""" def UpperCAmelCase_ ( self : str ) -> List[Any]: '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def UpperCAmelCase_ ( self : List[Any] , UpperCamelCase__ : str , UpperCamelCase__ : int="cpu" , UpperCamelCase__ : Union[str, Any]=torch.floataa , UpperCamelCase__ : List[Any]=0 ) -> Any: '''simple docstring''' __UpperCamelCase =torch.Generator(device=UpperCamelCase__ ).manual_seed(UpperCamelCase__ ) __UpperCamelCase =np.random.RandomState(UpperCamelCase__ ).standard_normal((1, 4, 64, 64) ) __UpperCamelCase =torch.from_numpy(UpperCamelCase__ ).to(device=UpperCamelCase__ , dtype=UpperCamelCase__ ) __UpperCamelCase ={ '''prompt''': '''a photograph of an astronaut riding a horse''', '''latents''': latents, '''generator''': generator, '''num_inference_steps''': 3, '''guidance_scale''': 7.5, '''output_type''': '''numpy''', } return inputs def UpperCAmelCase_ ( self : Dict ) -> int: '''simple docstring''' __UpperCamelCase =StableDiffusionLDMaDPipeline.from_pretrained('''Intel/ldm3d''' ) __UpperCamelCase =ldmad_pipe.to(UpperCamelCase__ ) ldmad_pipe.set_progress_bar_config(disable=UpperCamelCase__ ) __UpperCamelCase =self.get_inputs(UpperCamelCase__ ) __UpperCamelCase =ldmad_pipe(**UpperCamelCase__ ) __UpperCamelCase , __UpperCamelCase =output.rgb, output.depth __UpperCamelCase =rgb[0, -3:, -3:, -1].flatten() __UpperCamelCase =rgb[0, -3:, -1].flatten() assert rgb.shape == (1, 512, 512, 3) assert depth.shape == (1, 512, 512) __UpperCamelCase =np.array( [0.53_80_54_65, 0.56_70_73_05, 0.5_48_65_15, 0.57_01_22_36, 0.5_81_45_11, 0.56_25_34_87, 0.54_84_30_14, 0.55_09_22_63, 0.6_45_97_06] ) __UpperCamelCase =np.array( [0.9_26_37_81, 0.6_67_86_72, 0.5_48_65_15, 0.92_20_21_45, 0.67_83_11_35, 0.56_25_34_87, 0.9_24_16_94, 0.7_55_14_78, 0.6_45_97_06] ) assert np.abs(rgb_slice - expected_slice_rgb ).max() < 3E-3 assert np.abs(depth_slice - expected_slice_depth ).max() < 3E-3 @nightly @require_torch_gpu class _lowercase ( unittest.TestCase ): """simple docstring""" def UpperCAmelCase_ ( self : int ) -> Tuple: '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def UpperCAmelCase_ ( self : Tuple , UpperCamelCase__ : Dict , UpperCamelCase__ : Optional[int]="cpu" , UpperCamelCase__ : Union[str, Any]=torch.floataa , UpperCamelCase__ : Any=0 ) -> Dict: '''simple docstring''' __UpperCamelCase =torch.Generator(device=UpperCamelCase__ ).manual_seed(UpperCamelCase__ ) __UpperCamelCase =np.random.RandomState(UpperCamelCase__ ).standard_normal((1, 4, 64, 64) ) __UpperCamelCase =torch.from_numpy(UpperCamelCase__ ).to(device=UpperCamelCase__ , dtype=UpperCamelCase__ ) __UpperCamelCase ={ '''prompt''': '''a photograph of an astronaut riding a horse''', '''latents''': latents, '''generator''': generator, '''num_inference_steps''': 50, '''guidance_scale''': 7.5, '''output_type''': '''numpy''', } return inputs def UpperCAmelCase_ ( self : Optional[int] ) -> Union[str, Any]: '''simple docstring''' __UpperCamelCase =StableDiffusionLDMaDPipeline.from_pretrained('''Intel/ldm3d''' ).to(UpperCamelCase__ ) ldmad_pipe.set_progress_bar_config(disable=UpperCamelCase__ ) __UpperCamelCase =self.get_inputs(UpperCamelCase__ ) __UpperCamelCase =ldmad_pipe(**UpperCamelCase__ ) __UpperCamelCase , __UpperCamelCase =output.rgb, output.depth __UpperCamelCase =0.49_55_86 __UpperCamelCase =0.33_79_55_15 __UpperCamelCase =112.48518 __UpperCamelCase =98.48_97_46 assert np.abs(expected_rgb_mean - rgb.mean() ) < 1E-3 assert np.abs(expected_rgb_std - rgb.std() ) < 1E-3 assert np.abs(expected_depth_mean - depth.mean() ) < 1E-3 assert np.abs(expected_depth_std - depth.std() ) < 1E-3 def UpperCAmelCase_ ( self : List[str] ) -> Tuple: '''simple docstring''' __UpperCamelCase =StableDiffusionLDMaDPipeline.from_pretrained('''Intel/ldm3d-4c''' ).to(UpperCamelCase__ ) ldmad_pipe.set_progress_bar_config(disable=UpperCamelCase__ ) __UpperCamelCase =self.get_inputs(UpperCamelCase__ ) __UpperCamelCase =ldmad_pipe(**UpperCamelCase__ ) __UpperCamelCase , __UpperCamelCase =output.rgb, output.depth __UpperCamelCase =0.4_19_41_27 __UpperCamelCase =0.35_37_55_86 __UpperCamelCase =0.5_63_85_02 __UpperCamelCase =0.34_68_61_03 assert rgb.shape == (1, 512, 512, 3) assert depth.shape == (1, 512, 512, 1) assert np.abs(expected_rgb_mean - rgb.mean() ) < 1E-3 assert np.abs(expected_rgb_std - rgb.std() ) < 1E-3 assert np.abs(expected_depth_mean - depth.mean() ) < 1E-3 assert np.abs(expected_depth_std - depth.std() ) < 1E-3
365
"""simple docstring""" import os from pathlib import Path def lowerCAmelCase (): """simple docstring""" from torch.utils.cpp_extension import load __UpperCamelCase =Path(__UpperCamelCase ).resolve().parent.parent.parent / '''kernels''' / '''deformable_detr''' __UpperCamelCase =[ root / filename for filename in [ '''vision.cpp''', os.path.join('''cpu''' , '''ms_deform_attn_cpu.cpp''' ), os.path.join('''cuda''' , '''ms_deform_attn_cuda.cu''' ), ] ] load( '''MultiScaleDeformableAttention''' , __UpperCamelCase , with_cuda=__UpperCamelCase , extra_include_paths=[str(__UpperCamelCase )] , extra_cflags=['''-DWITH_CUDA=1'''] , extra_cuda_cflags=[ '''-DCUDA_HAS_FP16=1''', '''-D__CUDA_NO_HALF_OPERATORS__''', '''-D__CUDA_NO_HALF_CONVERSIONS__''', '''-D__CUDA_NO_HALF2_OPERATORS__''', ] , ) import MultiScaleDeformableAttention as MSDA return MSDA
85
0
import flax.linen as nn import jax import jax.numpy as jnp class snake_case__ ( nn.Module ): lowercase__ : int lowercase__ : jnp.dtype = jnp.floataa def __magic_name__ ( self ) -> Dict: __magic_name__ : Optional[int] = nn.Conv( self.out_channels , kernel_size=(3, 3) , strides=(1, 1) , padding=((1, 1), (1, 1)) , dtype=self.dtype , ) def __call__( self , lowerCAmelCase__ ) -> Tuple: __magic_name__ ,__magic_name__ ,__magic_name__ ,__magic_name__ : Any = hidden_states.shape __magic_name__ : str = jax.image.resize( lowerCAmelCase__ , shape=(batch, height * 2, width * 2, channels) , method="""nearest""" , ) __magic_name__ : List[str] = self.conv(lowerCAmelCase__ ) return hidden_states class snake_case__ ( nn.Module ): lowercase__ : int lowercase__ : jnp.dtype = jnp.floataa def __magic_name__ ( self ) -> Tuple: __magic_name__ : List[Any] = nn.Conv( self.out_channels , kernel_size=(3, 3) , strides=(2, 2) , padding=((1, 1), (1, 1)) , dtype=self.dtype , ) def __call__( self , lowerCAmelCase__ ) -> Dict: # pad = ((0, 0), (0, 1), (0, 1), (0, 0)) # pad height and width dim # hidden_states = jnp.pad(hidden_states, pad_width=pad) __magic_name__ : str = self.conv(lowerCAmelCase__ ) return hidden_states class snake_case__ ( nn.Module ): lowercase__ : int lowercase__ : int = None lowercase__ : float = 0.0 lowercase__ : bool = None lowercase__ : jnp.dtype = jnp.floataa def __magic_name__ ( self ) -> Tuple: __magic_name__ : List[str] = self.in_channels if self.out_channels is None else self.out_channels __magic_name__ : List[str] = nn.GroupNorm(num_groups=32 , epsilon=1e-5 ) __magic_name__ : List[Any] = nn.Conv( lowerCAmelCase__ , kernel_size=(3, 3) , strides=(1, 1) , padding=((1, 1), (1, 1)) , dtype=self.dtype , ) __magic_name__ : List[Any] = nn.Dense(lowerCAmelCase__ , dtype=self.dtype ) __magic_name__ : List[str] = nn.GroupNorm(num_groups=32 , epsilon=1e-5 ) __magic_name__ : int = nn.Dropout(self.dropout_prob ) __magic_name__ : Dict = nn.Conv( lowerCAmelCase__ , kernel_size=(3, 3) , strides=(1, 1) , padding=((1, 1), (1, 1)) , dtype=self.dtype , ) __magic_name__ : Dict = self.in_channels != out_channels if self.use_nin_shortcut is None else self.use_nin_shortcut __magic_name__ : Dict = None if use_nin_shortcut: __magic_name__ : List[Any] = nn.Conv( lowerCAmelCase__ , kernel_size=(1, 1) , strides=(1, 1) , padding="""VALID""" , dtype=self.dtype , ) def __call__( self , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__=True ) -> Tuple: __magic_name__ : int = hidden_states __magic_name__ : str = self.norma(lowerCAmelCase__ ) __magic_name__ : Optional[int] = nn.swish(lowerCAmelCase__ ) __magic_name__ : Dict = self.conva(lowerCAmelCase__ ) __magic_name__ : Optional[Any] = self.time_emb_proj(nn.swish(lowerCAmelCase__ ) ) __magic_name__ : Optional[Any] = jnp.expand_dims(jnp.expand_dims(lowerCAmelCase__ , 1 ) , 1 ) __magic_name__ : Union[str, Any] = hidden_states + temb __magic_name__ : Union[str, Any] = self.norma(lowerCAmelCase__ ) __magic_name__ : Union[str, Any] = nn.swish(lowerCAmelCase__ ) __magic_name__ : Union[str, Any] = self.dropout(lowerCAmelCase__ , lowerCAmelCase__ ) __magic_name__ : str = self.conva(lowerCAmelCase__ ) if self.conv_shortcut is not None: __magic_name__ : Any = self.conv_shortcut(lowerCAmelCase__ ) return hidden_states + residual
342
import copy import inspect import unittest import numpy as np from huggingface_hub import hf_hub_download from transformers import TimesformerConfig from transformers.models.auto import get_values 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 ( MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING, TimesformerForVideoClassification, TimesformerModel, ) from transformers.models.timesformer.modeling_timesformer import TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from transformers import VideoMAEImageProcessor class snake_case__ : def __init__( self , lowerCAmelCase__ , lowerCAmelCase__=13 , lowerCAmelCase__=10 , lowerCAmelCase__=3 , lowerCAmelCase__=2 , lowerCAmelCase__=2 , lowerCAmelCase__=True , lowerCAmelCase__=True , lowerCAmelCase__=32 , lowerCAmelCase__=5 , lowerCAmelCase__=4 , lowerCAmelCase__=37 , lowerCAmelCase__="gelu" , lowerCAmelCase__=0.1 , lowerCAmelCase__=0.1 , lowerCAmelCase__=10 , lowerCAmelCase__=0.0_2 , lowerCAmelCase__="divided_space_time" , lowerCAmelCase__=None , ) -> List[str]: __magic_name__ : int = parent __magic_name__ : Tuple = batch_size __magic_name__ : int = image_size __magic_name__ : str = num_channels __magic_name__ : Dict = patch_size __magic_name__ : Tuple = num_frames __magic_name__ : List[Any] = is_training __magic_name__ : List[Any] = use_labels __magic_name__ : Dict = hidden_size __magic_name__ : List[Any] = num_hidden_layers __magic_name__ : str = num_attention_heads __magic_name__ : List[Any] = intermediate_size __magic_name__ : Dict = hidden_act __magic_name__ : List[Any] = hidden_dropout_prob __magic_name__ : Union[str, Any] = attention_probs_dropout_prob __magic_name__ : Tuple = attention_type __magic_name__ : List[str] = initializer_range __magic_name__ : Optional[Any] = scope __magic_name__ : Tuple = num_labels # in TimeSformer, the number of spatial tokens equals num_frames * num_patches per frame + 1 CLS token __magic_name__ : str = (image_size // patch_size) ** 2 __magic_name__ : Any = (num_frames) * self.num_patches_per_frame + 1 def __magic_name__ ( self ) -> Dict: __magic_name__ : Optional[Any] = floats_tensor( [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size] ) __magic_name__ : str = None if self.use_labels: __magic_name__ : List[Any] = ids_tensor([self.batch_size] , self.num_labels ) __magic_name__ : Optional[Any] = self.get_config() return config, pixel_values, labels def __magic_name__ ( self ) -> str: __magic_name__ : Dict = TimesformerConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , num_frames=self.num_frames , 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 , initializer_range=self.initializer_range , attention_type=self.attention_type , ) __magic_name__ : Optional[Any] = self.num_labels return config def __magic_name__ ( self , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) -> Optional[Any]: __magic_name__ : List[Any] = TimesformerModel(config=lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() __magic_name__ : Optional[Any] = model(lowerCAmelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __magic_name__ ( self , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) -> Any: __magic_name__ : int = TimesformerForVideoClassification(lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() __magic_name__ : List[Any] = model(lowerCAmelCase__ ) # verify the logits shape __magic_name__ : List[Any] = torch.Size((self.batch_size, self.num_labels) ) self.parent.assertEqual(result.logits.shape , lowerCAmelCase__ ) def __magic_name__ ( self ) -> Any: __magic_name__ : Union[str, Any] = self.prepare_config_and_inputs() __magic_name__ ,__magic_name__ ,__magic_name__ : Tuple = config_and_inputs __magic_name__ : Optional[int] = {"""pixel_values""": pixel_values} return config, inputs_dict @require_torch class snake_case__ ( _lowerCAmelCase , _lowerCAmelCase , unittest.TestCase ): lowercase__ : Tuple = (TimesformerModel, TimesformerForVideoClassification) if is_torch_available() else () lowercase__ : Union[str, Any] = ( {'''feature-extraction''': TimesformerModel, '''video-classification''': TimesformerForVideoClassification} if is_torch_available() else {} ) lowercase__ : int = False lowercase__ : str = False lowercase__ : Tuple = False lowercase__ : Any = False def __magic_name__ ( self ) -> List[Any]: __magic_name__ : List[Any] = TimesformerModelTester(self ) __magic_name__ : List[str] = ConfigTester( self , config_class=lowerCAmelCase__ , has_text_modality=lowerCAmelCase__ , hidden_size=37 ) def __magic_name__ ( self , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__=False ) -> List[str]: __magic_name__ : List[str] = copy.deepcopy(lowerCAmelCase__ ) if return_labels: if model_class in get_values(lowerCAmelCase__ ): __magic_name__ : Union[str, Any] = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=lowerCAmelCase__ ) return inputs_dict def __magic_name__ ( self ) -> List[str]: self.config_tester.run_common_tests() @unittest.skip(reason="""TimeSformer does not use inputs_embeds""" ) def __magic_name__ ( self ) -> str: pass def __magic_name__ ( self ) -> Optional[int]: __magic_name__ ,__magic_name__ : Dict = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: __magic_name__ : List[Any] = model_class(lowerCAmelCase__ ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) __magic_name__ : Tuple = model.get_output_embeddings() self.assertTrue(x is None or isinstance(lowerCAmelCase__ , nn.Linear ) ) def __magic_name__ ( self ) -> Optional[Any]: __magic_name__ ,__magic_name__ : Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: __magic_name__ : Dict = model_class(lowerCAmelCase__ ) __magic_name__ : Optional[Any] = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic __magic_name__ : Optional[int] = [*signature.parameters.keys()] __magic_name__ : Any = ["""pixel_values"""] self.assertListEqual(arg_names[:1] , lowerCAmelCase__ ) def __magic_name__ ( self ) -> List[Any]: __magic_name__ : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCAmelCase__ ) def __magic_name__ ( self ) -> Union[str, Any]: __magic_name__ : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_video_classification(*lowerCAmelCase__ ) @slow def __magic_name__ ( self ) -> Optional[int]: for model_name in TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __magic_name__ : List[str] = TimesformerModel.from_pretrained(lowerCAmelCase__ ) self.assertIsNotNone(lowerCAmelCase__ ) def __magic_name__ ( self ) -> List[Any]: if not self.has_attentions: pass else: __magic_name__ ,__magic_name__ : Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() __magic_name__ : Optional[int] = True for model_class in self.all_model_classes: __magic_name__ : Tuple = self.model_tester.seq_length __magic_name__ : int = self.model_tester.num_frames __magic_name__ : Any = True __magic_name__ : Tuple = False __magic_name__ : Optional[int] = True __magic_name__ : str = model_class(lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() with torch.no_grad(): __magic_name__ : List[str] = model(**self._prepare_for_class(lowerCAmelCase__ , lowerCAmelCase__ ) ) __magic_name__ : List[str] = outputs.attentions self.assertEqual(len(lowerCAmelCase__ ) , self.model_tester.num_hidden_layers ) # check that output_attentions also work using config del inputs_dict["output_attentions"] __magic_name__ : Optional[Any] = True __magic_name__ : Optional[Any] = model_class(lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() with torch.no_grad(): __magic_name__ : Optional[int] = model(**self._prepare_for_class(lowerCAmelCase__ , lowerCAmelCase__ ) ) __magic_name__ : int = outputs.attentions self.assertEqual(len(lowerCAmelCase__ ) , self.model_tester.num_hidden_layers ) # attentions has shape (batch_size x num_frames) x num_heads x (num_patches per frame + 1) x (num_patches per frame + 1) self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, seq_len // num_frames + 1, seq_len // num_frames + 1] , ) __magic_name__ : Union[str, Any] = len(lowerCAmelCase__ ) # Check attention is always last and order is fine __magic_name__ : str = True __magic_name__ : Optional[Any] = True __magic_name__ : int = model_class(lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() with torch.no_grad(): __magic_name__ : List[str] = model(**self._prepare_for_class(lowerCAmelCase__ , lowerCAmelCase__ ) ) self.assertEqual(out_len + 1 , len(lowerCAmelCase__ ) ) __magic_name__ : Union[str, Any] = outputs.attentions self.assertEqual(len(lowerCAmelCase__ ) , self.model_tester.num_hidden_layers ) # attentions has shape (batch_size x num_frames) x num_heads x (num_patches per frame + 1) x (num_patches per frame + 1) self.assertListEqual( list(self_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, seq_len // num_frames + 1, seq_len // num_frames + 1] , ) def __magic_name__ ( self ) -> Any: def check_hidden_states_output(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ): __magic_name__ : Union[str, Any] = model_class(lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() with torch.no_grad(): __magic_name__ : int = model(**self._prepare_for_class(lowerCAmelCase__ , lowerCAmelCase__ ) ) __magic_name__ : Optional[Any] = outputs.hidden_states __magic_name__ : str = self.model_tester.num_hidden_layers + 1 self.assertEqual(len(lowerCAmelCase__ ) , lowerCAmelCase__ ) __magic_name__ : str = self.model_tester.seq_length self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [seq_length, self.model_tester.hidden_size] , ) __magic_name__ ,__magic_name__ : int = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: __magic_name__ : Optional[Any] = True check_hidden_states_output(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] __magic_name__ : Union[str, Any] = True check_hidden_states_output(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) def UpperCamelCase ( ): """simple docstring""" __magic_name__ : List[Any] = hf_hub_download( repo_id="""hf-internal-testing/spaghetti-video""", filename="""eating_spaghetti.npy""", repo_type="""dataset""" ) __magic_name__ : List[str] = np.load(_A ) return list(_A ) @require_torch @require_vision class snake_case__ ( unittest.TestCase ): @cached_property def __magic_name__ ( self ) -> Optional[Any]: # logits were tested with a different mean and std, so we use the same here return ( VideoMAEImageProcessor(image_mean=[0.5, 0.5, 0.5] , image_std=[0.5, 0.5, 0.5] ) if is_vision_available() else None ) @slow def __magic_name__ ( self ) -> List[Any]: __magic_name__ : Dict = TimesformerForVideoClassification.from_pretrained("""facebook/timesformer-base-finetuned-k400""" ).to( lowerCAmelCase__ ) __magic_name__ : str = self.default_image_processor __magic_name__ : Any = prepare_video() __magic_name__ : Dict = image_processor(video[:8] , return_tensors="""pt""" ).to(lowerCAmelCase__ ) # forward pass with torch.no_grad(): __magic_name__ : int = model(**lowerCAmelCase__ ) # verify the logits __magic_name__ : Optional[int] = torch.Size((1, 4_00) ) self.assertEqual(outputs.logits.shape , lowerCAmelCase__ ) __magic_name__ : Union[str, Any] = torch.tensor([-0.3_0_1_6, -0.7_7_1_3, -0.4_2_0_5] ).to(lowerCAmelCase__ ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , lowerCAmelCase__ , atol=1e-4 ) )
342
1
'''simple docstring''' import warnings from diffusers import StableDiffusionImgaImgPipeline # noqa F401 warnings.warn( '''The `image_to_image.py` script is outdated. Please use directly `from diffusers import''' ''' StableDiffusionImg2ImgPipeline` instead.''' )
107
'''simple docstring''' from dataclasses import dataclass, field from typing import Optional from transformers import AutoConfig, AutoImageProcessor, AutoTokenizer, FlaxVisionEncoderDecoderModel, HfArgumentParser @dataclass class __magic_name__ : lowerCAmelCase : str = field( metadata={'help': 'The output directory where the model will be written.'} , ) lowerCAmelCase : str = field( metadata={ 'help': ( 'The encoder model checkpoint for weights initialization.' 'Don\'t set if you want to train an encoder model from scratch.' ) } , ) lowerCAmelCase : str = field( metadata={ 'help': ( 'The decoder model checkpoint for weights initialization.' 'Don\'t set if you want to train a decoder model from scratch.' ) } , ) lowerCAmelCase : Optional[str] = field( default=_UpperCamelCase , metadata={'help': 'Pretrained encoder config name or path if not the same as encoder_model_name'} ) lowerCAmelCase : Optional[str] = field( default=_UpperCamelCase , metadata={'help': 'Pretrained decoder config name or path if not the same as decoder_model_name'} ) def __lowerCamelCase ( ) -> Union[str, Any]: _a : Any = HfArgumentParser((ModelArguments,) ) ((_a) , ) : Dict = parser.parse_args_into_dataclasses() # Load pretrained model and tokenizer # Use explicit specified encoder config if model_args.encoder_config_name: _a : Optional[Any] = AutoConfig.from_pretrained(model_args.encoder_config_name ) # Use pretrained encoder model's config else: _a : Optional[Any] = AutoConfig.from_pretrained(model_args.encoder_model_name_or_path ) # Use explicit specified decoder config if model_args.decoder_config_name: _a : List[str] = AutoConfig.from_pretrained(model_args.decoder_config_name ) # Use pretrained decoder model's config else: _a : Optional[int] = AutoConfig.from_pretrained(model_args.decoder_model_name_or_path ) # necessary for `from_encoder_decoder_pretrained` when `decoder_config` is passed _a : List[Any] = True _a : int = True _a : Any = FlaxVisionEncoderDecoderModel.from_encoder_decoder_pretrained( encoder_pretrained_model_name_or_path=model_args.encoder_model_name_or_path , decoder_pretrained_model_name_or_path=model_args.decoder_model_name_or_path , encoder_config=lowerCAmelCase_ , decoder_config=lowerCAmelCase_ , ) # GPT2 only has bos/eos tokens but not decoder_start/pad tokens _a : List[str] = decoder_config.decoder_start_token_id _a : Optional[int] = decoder_config.pad_token_id if decoder_start_token_id is None: _a : Tuple = decoder_config.bos_token_id if pad_token_id is None: _a : List[Any] = decoder_config.eos_token_id # This is necessary to make Flax's generate() work _a : Any = decoder_config.eos_token_id _a : Tuple = decoder_start_token_id _a : Any = pad_token_id _a : Dict = AutoImageProcessor.from_pretrained(model_args.encoder_model_name_or_path ) _a : Dict = AutoTokenizer.from_pretrained(model_args.decoder_model_name_or_path ) _a : int = tokenizer.convert_ids_to_tokens(model.config.pad_token_id ) model.save_pretrained(model_args.output_dir ) image_processor.save_pretrained(model_args.output_dir ) tokenizer.save_pretrained(model_args.output_dir ) if __name__ == "__main__": main()
107
1
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 KandinskyPipeline, KandinskyPriorPipeline else: from .pipeline_kandinsky import KandinskyPipeline from .pipeline_kandinsky_imgaimg import KandinskyImgaImgPipeline from .pipeline_kandinsky_inpaint import KandinskyInpaintPipeline from .pipeline_kandinsky_prior import KandinskyPriorPipeline, KandinskyPriorPipelineOutput from .text_encoder import MultilingualCLIP
138
from __future__ import annotations from collections.abc import Callable from typing import Any, Generic, TypeVar __A : Any = TypeVar('''T''') class __A ( Generic[T] ): def __init__( self : Dict , UpperCAmelCase_ : list[T] , UpperCAmelCase_ : Callable[[T, T], T] ): lowerCAmelCase : Any | T = None lowerCAmelCase : int = len(UpperCAmelCase_ ) lowerCAmelCase : list[T] = [any_type for _ in range(self.N )] + arr lowerCAmelCase : List[Any] = fnc self.build() def lowercase__ ( self : str ): for p in range(self.N - 1 , 0 , -1 ): lowerCAmelCase : Optional[Any] = self.fn(self.st[p * 2] , self.st[p * 2 + 1] ) def lowercase__ ( self : int , UpperCAmelCase_ : int , UpperCAmelCase_ : T ): p += self.N lowerCAmelCase : int = v while p > 1: lowerCAmelCase : List[Any] = p // 2 lowerCAmelCase : List[Any] = self.fn(self.st[p * 2] , self.st[p * 2 + 1] ) def lowercase__ ( self : Optional[Any] , UpperCAmelCase_ : int , UpperCAmelCase_ : int ): # noqa: E741 lowerCAmelCase , lowerCAmelCase : str = l + self.N, r + self.N lowerCAmelCase : T | None = None while l <= r: if l % 2 == 1: lowerCAmelCase : Any = self.st[l] if res is None else self.fn(UpperCAmelCase_ , self.st[l] ) if r % 2 == 0: lowerCAmelCase : Optional[int] = self.st[r] if res is None else self.fn(UpperCAmelCase_ , self.st[r] ) lowerCAmelCase , lowerCAmelCase : Optional[Any] = (l + 1) // 2, (r - 1) // 2 return res if __name__ == "__main__": from functools import reduce __A : str = [1, 10, -2, 9, -3, 8, 4, -7, 5, 6, 11, -12] __A : List[Any] = { 0: 7, 1: 2, 2: 6, 3: -14, 4: 5, 5: 4, 6: 7, 7: -10, 8: 9, 9: 10, 10: 12, 11: 1, } __A : Optional[int] = SegmentTree(test_array, min) __A : Optional[int] = SegmentTree(test_array, max) __A : Dict = SegmentTree(test_array, lambda a, b: a + b) def SCREAMING_SNAKE_CASE__ ( ) -> None: '''simple docstring''' for i in range(len(_UpperCAmelCase ) ): for j in range(_UpperCAmelCase, len(_UpperCAmelCase ) ): lowerCAmelCase : str = reduce(_UpperCAmelCase, test_array[i : j + 1] ) lowerCAmelCase : Dict = reduce(_UpperCAmelCase, test_array[i : j + 1] ) lowerCAmelCase : str = reduce(lambda _UpperCAmelCase, _UpperCAmelCase : a + b, test_array[i : j + 1] ) assert min_range == min_segment_tree.query(_UpperCAmelCase, _UpperCAmelCase ) assert max_range == max_segment_tree.query(_UpperCAmelCase, _UpperCAmelCase ) assert sum_range == sum_segment_tree.query(_UpperCAmelCase, _UpperCAmelCase ) test_all_segments() for index, value in test_updates.items(): __A : int = value min_segment_tree.update(index, value) max_segment_tree.update(index, value) sum_segment_tree.update(index, value) test_all_segments()
138
1
"""simple docstring""" import unittest from transformers import is_torch_available from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow if is_torch_available(): import torch from transformers import XLMRobertaModel @require_sentencepiece @require_tokenizers @require_torch class __snake_case ( unittest.TestCase ): @slow def lowerCamelCase ( self : Tuple): """simple docstring""" UpperCAmelCase_ = XLMRobertaModel.from_pretrained('''xlm-roberta-base''') UpperCAmelCase_ = torch.tensor([[0, 581, 10269, 83, 99942, 136, 60742, 23, 70, 80583, 18276, 2]]) # The dog is cute and lives in the garden house UpperCAmelCase_ = torch.Size((1, 12, 768)) # batch_size, sequence_length, embedding_vector_dim UpperCAmelCase_ = torch.tensor( [[-0.0_1_0_1, 0.1_2_1_8, -0.0_8_0_3, 0.0_8_0_1, 0.1_3_2_7, 0.0_7_7_6, -0.1_2_1_5, 0.2_3_8_3, 0.3_3_3_8, 0.3_1_0_6, 0.0_3_0_0, 0.0_2_5_2]]) # xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.base') # xlmr.eval() # expected_output_values_last_dim = xlmr.extract_features(input_ids[0])[:, :, -1] with torch.no_grad(): UpperCAmelCase_ = model(_snake_case)['''last_hidden_state'''].detach() self.assertEqual(output.shape , _snake_case) # compare the actual values for a slice of last dim self.assertTrue(torch.allclose(output[:, :, -1] , _snake_case , atol=1e-3)) @slow def lowerCamelCase ( self : List[str]): """simple docstring""" UpperCAmelCase_ = XLMRobertaModel.from_pretrained('''xlm-roberta-large''') UpperCAmelCase_ = torch.tensor([[0, 581, 10269, 83, 99942, 136, 60742, 23, 70, 80583, 18276, 2]]) # The dog is cute and lives in the garden house UpperCAmelCase_ = torch.Size((1, 12, 1024)) # batch_size, sequence_length, embedding_vector_dim UpperCAmelCase_ = torch.tensor( [[-0.0_6_9_9, -0.0_3_1_8, 0.0_7_0_5, -0.1_2_4_1, 0.0_9_9_9, -0.0_5_2_0, 0.1_0_0_4, -0.1_8_3_8, -0.4_7_0_4, 0.1_4_3_7, 0.0_8_2_1, 0.0_1_2_6]]) # xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.large') # xlmr.eval() # expected_output_values_last_dim = xlmr.extract_features(input_ids[0])[:, :, -1] with torch.no_grad(): UpperCAmelCase_ = model(_snake_case)['''last_hidden_state'''].detach() self.assertEqual(output.shape , _snake_case) # compare the actual values for a slice of last dim self.assertTrue(torch.allclose(output[:, :, -1] , _snake_case , atol=1e-3))
366
import os import tempfile import unittest from pathlib import Path from transformers import AutoConfig, is_torch_available from transformers.testing_utils import require_torch, torch_device if is_torch_available(): from transformers import PyTorchBenchmark, PyTorchBenchmarkArguments @require_torch class __snake_case ( unittest.TestCase ): def lowerCamelCase ( self : Optional[int] , _snake_case : Union[str, Any]): """simple docstring""" for model_result in results.values(): for batch_size, sequence_length in zip(model_result['''bs'''] , model_result['''ss''']): UpperCAmelCase_ = model_result['''result'''][batch_size][sequence_length] self.assertIsNotNone(_snake_case) def lowerCamelCase ( self : Optional[Any]): """simple docstring""" UpperCAmelCase_ = '''sshleifer/tiny-gpt2''' UpperCAmelCase_ = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=_snake_case , inference=_snake_case , sequence_lengths=[8] , batch_sizes=[1] , multi_process=_snake_case , ) UpperCAmelCase_ = PyTorchBenchmark(_snake_case) UpperCAmelCase_ = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result) self.check_results_dict_not_empty(results.memory_inference_result) def lowerCamelCase ( self : str): """simple docstring""" UpperCAmelCase_ = '''sgugger/tiny-distilbert-classification''' UpperCAmelCase_ = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=_snake_case , inference=_snake_case , sequence_lengths=[8] , batch_sizes=[1] , multi_process=_snake_case , only_pretrain_model=_snake_case , ) UpperCAmelCase_ = PyTorchBenchmark(_snake_case) UpperCAmelCase_ = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result) self.check_results_dict_not_empty(results.memory_inference_result) def lowerCamelCase ( self : List[str]): """simple docstring""" UpperCAmelCase_ = '''sshleifer/tiny-gpt2''' UpperCAmelCase_ = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=_snake_case , inference=_snake_case , torchscript=_snake_case , sequence_lengths=[8] , batch_sizes=[1] , multi_process=_snake_case , ) UpperCAmelCase_ = PyTorchBenchmark(_snake_case) UpperCAmelCase_ = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result) self.check_results_dict_not_empty(results.memory_inference_result) @unittest.skipIf(torch_device == '''cpu''' , '''Cant do half precision''') def lowerCamelCase ( self : List[str]): """simple docstring""" UpperCAmelCase_ = '''sshleifer/tiny-gpt2''' UpperCAmelCase_ = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=_snake_case , inference=_snake_case , fpaa=_snake_case , sequence_lengths=[8] , batch_sizes=[1] , multi_process=_snake_case , ) UpperCAmelCase_ = PyTorchBenchmark(_snake_case) UpperCAmelCase_ = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result) self.check_results_dict_not_empty(results.memory_inference_result) def lowerCamelCase ( self : Optional[Any]): """simple docstring""" UpperCAmelCase_ = '''sshleifer/tiny-gpt2''' UpperCAmelCase_ = AutoConfig.from_pretrained(_snake_case) # set architectures equal to `None` UpperCAmelCase_ = None UpperCAmelCase_ = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=_snake_case , inference=_snake_case , sequence_lengths=[8] , batch_sizes=[1] , multi_process=_snake_case , ) UpperCAmelCase_ = PyTorchBenchmark(_snake_case , configs=[config]) UpperCAmelCase_ = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result) self.check_results_dict_not_empty(results.memory_inference_result) def lowerCamelCase ( self : Dict): """simple docstring""" UpperCAmelCase_ = '''sshleifer/tiny-gpt2''' UpperCAmelCase_ = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=_snake_case , inference=_snake_case , sequence_lengths=[8] , batch_sizes=[1] , multi_process=_snake_case , ) UpperCAmelCase_ = PyTorchBenchmark(_snake_case) UpperCAmelCase_ = benchmark.run() self.check_results_dict_not_empty(results.time_train_result) self.check_results_dict_not_empty(results.memory_train_result) @unittest.skipIf(torch_device == '''cpu''' , '''Can\'t do half precision''') def lowerCamelCase ( self : Optional[Any]): """simple docstring""" UpperCAmelCase_ = '''sshleifer/tiny-gpt2''' UpperCAmelCase_ = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=_snake_case , inference=_snake_case , sequence_lengths=[8] , batch_sizes=[1] , fpaa=_snake_case , multi_process=_snake_case , ) UpperCAmelCase_ = PyTorchBenchmark(_snake_case) UpperCAmelCase_ = benchmark.run() self.check_results_dict_not_empty(results.time_train_result) self.check_results_dict_not_empty(results.memory_train_result) def lowerCamelCase ( self : List[Any]): """simple docstring""" UpperCAmelCase_ = '''sshleifer/tiny-gpt2''' UpperCAmelCase_ = AutoConfig.from_pretrained(_snake_case) UpperCAmelCase_ = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=_snake_case , inference=_snake_case , sequence_lengths=[8] , batch_sizes=[1] , multi_process=_snake_case , ) UpperCAmelCase_ = PyTorchBenchmark(_snake_case , configs=[config]) UpperCAmelCase_ = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result) self.check_results_dict_not_empty(results.memory_inference_result) def lowerCamelCase ( self : Dict): """simple docstring""" UpperCAmelCase_ = '''sshleifer/tinier_bart''' UpperCAmelCase_ = AutoConfig.from_pretrained(_snake_case) UpperCAmelCase_ = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=_snake_case , inference=_snake_case , sequence_lengths=[8] , batch_sizes=[1] , multi_process=_snake_case , ) UpperCAmelCase_ = PyTorchBenchmark(_snake_case , configs=[config]) UpperCAmelCase_ = benchmark.run() self.check_results_dict_not_empty(results.time_inference_result) self.check_results_dict_not_empty(results.memory_inference_result) def lowerCamelCase ( self : Dict): """simple docstring""" UpperCAmelCase_ = '''sshleifer/tiny-gpt2''' UpperCAmelCase_ = AutoConfig.from_pretrained(_snake_case) UpperCAmelCase_ = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=_snake_case , inference=_snake_case , sequence_lengths=[8] , batch_sizes=[1] , multi_process=_snake_case , ) UpperCAmelCase_ = PyTorchBenchmark(_snake_case , configs=[config]) UpperCAmelCase_ = benchmark.run() self.check_results_dict_not_empty(results.time_train_result) self.check_results_dict_not_empty(results.memory_train_result) def lowerCamelCase ( self : List[Any]): """simple docstring""" UpperCAmelCase_ = '''sshleifer/tinier_bart''' UpperCAmelCase_ = AutoConfig.from_pretrained(_snake_case) UpperCAmelCase_ = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=_snake_case , inference=_snake_case , sequence_lengths=[8] , batch_sizes=[1] , multi_process=_snake_case , ) UpperCAmelCase_ = PyTorchBenchmark(_snake_case , configs=[config]) UpperCAmelCase_ = benchmark.run() self.check_results_dict_not_empty(results.time_train_result) self.check_results_dict_not_empty(results.memory_train_result) def lowerCamelCase ( self : List[Any]): """simple docstring""" UpperCAmelCase_ = '''sshleifer/tiny-gpt2''' with tempfile.TemporaryDirectory() as tmp_dir: UpperCAmelCase_ = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=_snake_case , inference=_snake_case , save_to_csv=_snake_case , sequence_lengths=[8] , batch_sizes=[1] , inference_time_csv_file=os.path.join(_snake_case , '''inf_time.csv''') , train_memory_csv_file=os.path.join(_snake_case , '''train_mem.csv''') , inference_memory_csv_file=os.path.join(_snake_case , '''inf_mem.csv''') , train_time_csv_file=os.path.join(_snake_case , '''train_time.csv''') , env_info_csv_file=os.path.join(_snake_case , '''env.csv''') , multi_process=_snake_case , ) UpperCAmelCase_ = PyTorchBenchmark(_snake_case) benchmark.run() self.assertTrue(Path(os.path.join(_snake_case , '''inf_time.csv''')).exists()) self.assertTrue(Path(os.path.join(_snake_case , '''train_time.csv''')).exists()) self.assertTrue(Path(os.path.join(_snake_case , '''inf_mem.csv''')).exists()) self.assertTrue(Path(os.path.join(_snake_case , '''train_mem.csv''')).exists()) self.assertTrue(Path(os.path.join(_snake_case , '''env.csv''')).exists()) def lowerCamelCase ( self : List[str]): """simple docstring""" UpperCAmelCase_ = '''sshleifer/tiny-gpt2''' def _check_summary_is_not_empty(_snake_case : Tuple): self.assertTrue(hasattr(_snake_case , '''sequential''')) self.assertTrue(hasattr(_snake_case , '''cumulative''')) self.assertTrue(hasattr(_snake_case , '''current''')) self.assertTrue(hasattr(_snake_case , '''total''')) with tempfile.TemporaryDirectory() as tmp_dir: UpperCAmelCase_ = PyTorchBenchmarkArguments( models=[MODEL_ID] , training=_snake_case , inference=_snake_case , sequence_lengths=[8] , batch_sizes=[1] , log_filename=os.path.join(_snake_case , '''log.txt''') , log_print=_snake_case , trace_memory_line_by_line=_snake_case , multi_process=_snake_case , ) UpperCAmelCase_ = PyTorchBenchmark(_snake_case) UpperCAmelCase_ = benchmark.run() _check_summary_is_not_empty(result.inference_summary) _check_summary_is_not_empty(result.train_summary) self.assertTrue(Path(os.path.join(_snake_case , '''log.txt''')).exists())
7
0
"""simple docstring""" import logging import os from dataclasses import dataclass, field from typing import Dict, Optional import numpy as np from utils_multiple_choice import MultipleChoiceDataset, Split, processors import transformers from transformers import ( AutoConfig, AutoModelForMultipleChoice, AutoTokenizer, DataCollatorWithPadding, EvalPrediction, HfArgumentParser, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import is_main_process __magic_name__ = logging.getLogger(__name__) def _lowerCAmelCase ( UpperCamelCase_ , UpperCamelCase_ ): return (preds == labels).mean() @dataclass class SCREAMING_SNAKE_CASE_ : """simple docstring""" __lowercase : str = field( metadata={'''help''': '''Path to pretrained model or model identifier from huggingface.co/models'''} ) __lowercase : Optional[str] = field( default=__a , metadata={'''help''': '''Pretrained config name or path if not the same as model_name'''} ) __lowercase : Optional[str] = field( default=__a , metadata={'''help''': '''Pretrained tokenizer name or path if not the same as model_name'''} ) __lowercase : Optional[str] = field( default=__a , metadata={'''help''': '''Where do you want to store the pretrained models downloaded from huggingface.co'''} , ) @dataclass class SCREAMING_SNAKE_CASE_ : """simple docstring""" __lowercase : str = field(metadata={'''help''': '''The name of the task to train on: ''' + ''', '''.join(processors.keys() )} ) __lowercase : str = field(metadata={'''help''': '''Should contain the data files for the task.'''} ) __lowercase : int = field( default=128 , metadata={ '''help''': ( '''The maximum total input sequence length after tokenization. Sequences longer ''' '''than this will be truncated, sequences shorter will be padded.''' ) } , ) __lowercase : bool = field( default=__a , metadata={'''help''': '''Overwrite the cached training and evaluation sets'''} ) def _lowerCAmelCase ( ): # 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. __SCREAMING_SNAKE_CASE = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) __SCREAMING_SNAKE_CASE ,__SCREAMING_SNAKE_CASE ,__SCREAMING_SNAKE_CASE = parser.parse_args_into_dataclasses() if ( os.path.exists(training_args.output_dir ) and os.listdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir ): raise ValueError( f"Output directory ({training_args.output_dir}) already exists and is not empty. Use" """ --overwrite_output_dir to overcome.""" ) # Setup logging logging.basicConfig( format="""%(asctime)s - %(levelname)s - %(name)s - %(message)s""" , datefmt="""%m/%d/%Y %H:%M:%S""" , level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN , ) logger.warning( """Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s""" , training_args.local_rank , training_args.device , training_args.n_gpu , bool(training_args.local_rank != -1 ) , training_args.fpaa , ) # Set the verbosity to info of the Transformers logger (on main process only): if is_main_process(training_args.local_rank ): transformers.utils.logging.set_verbosity_info() transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() logger.info("""Training/evaluation parameters %s""" , UpperCamelCase_ ) # Set seed set_seed(training_args.seed ) try: __SCREAMING_SNAKE_CASE = processors[data_args.task_name]() __SCREAMING_SNAKE_CASE = processor.get_labels() __SCREAMING_SNAKE_CASE = len(UpperCamelCase_ ) except KeyError: raise ValueError("""Task not found: %s""" % (data_args.task_name) ) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. __SCREAMING_SNAKE_CASE = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , num_labels=UpperCamelCase_ , finetuning_task=data_args.task_name , cache_dir=model_args.cache_dir , ) __SCREAMING_SNAKE_CASE = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , ) __SCREAMING_SNAKE_CASE = AutoModelForMultipleChoice.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 , ) # Get datasets __SCREAMING_SNAKE_CASE = ( MultipleChoiceDataset( data_dir=data_args.data_dir , tokenizer=UpperCamelCase_ , task=data_args.task_name , max_seq_length=data_args.max_seq_length , overwrite_cache=data_args.overwrite_cache , mode=Split.train , ) if training_args.do_train else None ) __SCREAMING_SNAKE_CASE = ( MultipleChoiceDataset( data_dir=data_args.data_dir , tokenizer=UpperCamelCase_ , task=data_args.task_name , max_seq_length=data_args.max_seq_length , overwrite_cache=data_args.overwrite_cache , mode=Split.dev , ) if training_args.do_eval else None ) def compute_metrics(UpperCamelCase_ ) -> Dict: __SCREAMING_SNAKE_CASE = np.argmax(p.predictions , axis=1 ) return {"acc": simple_accuracy(UpperCamelCase_ , p.label_ids )} # Data collator __SCREAMING_SNAKE_CASE = DataCollatorWithPadding(UpperCamelCase_ , pad_to_multiple_of=8 ) if training_args.fpaa else None # Initialize our Trainer __SCREAMING_SNAKE_CASE = Trainer( model=UpperCamelCase_ , args=UpperCamelCase_ , train_dataset=UpperCamelCase_ , eval_dataset=UpperCamelCase_ , compute_metrics=UpperCamelCase_ , data_collator=UpperCamelCase_ , ) # Training if training_args.do_train: trainer.train( model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path ) else None ) trainer.save_model() # For convenience, we also re-save the tokenizer to the same directory, # so that you can share your model easily on huggingface.co/models =) if trainer.is_world_master(): tokenizer.save_pretrained(training_args.output_dir ) # Evaluation __SCREAMING_SNAKE_CASE = {} if training_args.do_eval: logger.info("""*** Evaluate ***""" ) __SCREAMING_SNAKE_CASE = trainer.evaluate() __SCREAMING_SNAKE_CASE = os.path.join(training_args.output_dir , """eval_results.txt""" ) if trainer.is_world_master(): with open(UpperCamelCase_ , """w""" ) as writer: logger.info("""***** Eval results *****""" ) for key, value in result.items(): logger.info(""" %s = %s""" , UpperCamelCase_ , UpperCamelCase_ ) writer.write("""%s = %s\n""" % (key, value) ) results.update(UpperCamelCase_ ) return results def _lowerCAmelCase ( UpperCamelCase_ ): # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
100
def _snake_case( SCREAMING_SNAKE_CASE__ : Union[str, Any] , SCREAMING_SNAKE_CASE__ : Tuple , SCREAMING_SNAKE_CASE__ : Union[str, Any] , SCREAMING_SNAKE_CASE__ : Any ) -> Optional[int]: '''simple docstring''' global f # a global dp table for knapsack if f[i][j] < 0: if j < wt[i - 1]: A__ = mf_knapsack(i - 1 , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) else: A__ = max( mf_knapsack(i - 1 , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) , mf_knapsack(i - 1 , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , j - wt[i - 1] ) + val[i - 1] , ) A__ = val return f[i][j] def _snake_case( SCREAMING_SNAKE_CASE__ : Tuple , SCREAMING_SNAKE_CASE__ : Optional[int] , SCREAMING_SNAKE_CASE__ : Optional[int] , SCREAMING_SNAKE_CASE__ : Optional[int] ) -> Tuple: '''simple docstring''' A__ = [[0] * (w + 1) for _ in range(n + 1 )] for i in range(1 , n + 1 ): for w_ in range(1 , w + 1 ): if wt[i - 1] <= w_: A__ = max(val[i - 1] + dp[i - 1][w_ - wt[i - 1]] , dp[i - 1][w_] ) else: A__ = dp[i - 1][w_] return dp[n][w_], dp def _snake_case( SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : list , SCREAMING_SNAKE_CASE__ : list ) -> Union[str, Any]: '''simple docstring''' if not (isinstance(SCREAMING_SNAKE_CASE__ , (list, tuple) ) and isinstance(SCREAMING_SNAKE_CASE__ , (list, tuple) )): raise ValueError( 'Both the weights and values vectors must be either lists or tuples' ) A__ = len(SCREAMING_SNAKE_CASE__ ) if num_items != len(SCREAMING_SNAKE_CASE__ ): A__ = ( 'The number of weights must be the same as the number of values.\n' f'But got {num_items} weights and {len(SCREAMING_SNAKE_CASE__ )} values' ) raise ValueError(SCREAMING_SNAKE_CASE__ ) for i in range(SCREAMING_SNAKE_CASE__ ): if not isinstance(wt[i] , SCREAMING_SNAKE_CASE__ ): A__ = ( 'All weights must be integers but got weight of ' f'type {type(wt[i] )} at index {i}' ) raise TypeError(SCREAMING_SNAKE_CASE__ ) A__ , A__ = knapsack(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) A__ = set() _construct_solution(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) return optimal_val, example_optional_set def _snake_case( SCREAMING_SNAKE_CASE__ : list , SCREAMING_SNAKE_CASE__ : list , SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : set ) -> Optional[int]: '''simple docstring''' if i > 0 and j > 0: if dp[i - 1][j] == dp[i][j]: _construct_solution(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , i - 1 , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) else: optimal_set.add(SCREAMING_SNAKE_CASE__ ) _construct_solution(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , i - 1 , j - wt[i - 1] , SCREAMING_SNAKE_CASE__ ) if __name__ == "__main__": lowercase_ = [3, 2, 4, 4] lowercase_ = [4, 3, 2, 3] lowercase_ = 4 lowercase_ = 6 lowercase_ = [[0] * (w + 1)] + [[0] + [-1] * (w + 1) for _ in range(n + 1)] lowercase_ , lowercase_ = knapsack(w, wt, val, n) print(optimal_solution) print(mf_knapsack(n, wt, val, w)) # switched the n and w # testing the dynamic programming problem with example # the optimal subset for the above example are items 3 and 4 lowercase_ , lowercase_ = knapsack_with_example_solution(w, wt, val) assert optimal_solution == 8 assert optimal_subset == {3, 4} print("optimal_value = ", optimal_solution) print("An optimal subset corresponding to the optimal value", optimal_subset)
7
0
from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Features, Value from .base import TaskTemplate @dataclass(frozen=_a ) class __lowerCAmelCase ( _a ): # `task` is not a ClassVar since we want it to be part of the `asdict` output for JSON serialization lowerCamelCase_ : str = field(default='''summarization''', metadata={'''include_in_asdict_even_if_is_default''': True} ) lowerCamelCase_ : ClassVar[Features] = Features({'''text''': Value('''string''' )} ) lowerCamelCase_ : ClassVar[Features] = Features({'''summary''': Value('''string''' )} ) lowerCamelCase_ : str = "text" lowerCamelCase_ : str = "summary" @property def lowerCamelCase (self ) -> Dict[str, str]: '''simple docstring''' return {self.text_column: "text", self.summary_column: "summary"}
279
import unittest import numpy as np from transformers import AlbertConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax.numpy as jnp from transformers.models.albert.modeling_flax_albert import ( FlaxAlbertForMaskedLM, FlaxAlbertForMultipleChoice, FlaxAlbertForPreTraining, FlaxAlbertForQuestionAnswering, FlaxAlbertForSequenceClassification, FlaxAlbertForTokenClassification, FlaxAlbertModel, ) class __lowerCAmelCase ( unittest.TestCase ): def __init__(self , __magic_name__ , __magic_name__=13 , __magic_name__=7 , __magic_name__=True , __magic_name__=True , __magic_name__=True , __magic_name__=True , __magic_name__=99 , __magic_name__=32 , __magic_name__=5 , __magic_name__=4 , __magic_name__=37 , __magic_name__="gelu" , __magic_name__=0.1 , __magic_name__=0.1 , __magic_name__=512 , __magic_name__=16 , __magic_name__=2 , __magic_name__=0.02 , __magic_name__=4 , ) -> Tuple: '''simple docstring''' snake_case_ : Union[str, Any] = parent snake_case_ : Optional[Any] = batch_size snake_case_ : List[Any] = seq_length snake_case_ : Tuple = is_training snake_case_ : List[str] = use_attention_mask snake_case_ : Any = use_token_type_ids snake_case_ : Dict = use_labels snake_case_ : Optional[Any] = vocab_size snake_case_ : Dict = hidden_size snake_case_ : List[Any] = num_hidden_layers snake_case_ : Union[str, Any] = num_attention_heads snake_case_ : Any = intermediate_size snake_case_ : Optional[int] = hidden_act snake_case_ : Optional[int] = hidden_dropout_prob snake_case_ : Optional[Any] = attention_probs_dropout_prob snake_case_ : Optional[int] = max_position_embeddings snake_case_ : Optional[int] = type_vocab_size snake_case_ : List[Any] = type_sequence_label_size snake_case_ : Dict = initializer_range snake_case_ : Dict = num_choices def lowerCamelCase (self ) -> Tuple: '''simple docstring''' snake_case_ : int = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) snake_case_ : Any = None if self.use_attention_mask: snake_case_ : List[Any] = random_attention_mask([self.batch_size, self.seq_length] ) snake_case_ : List[Any] = None if self.use_token_type_ids: snake_case_ : Dict = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) snake_case_ : List[Any] = AlbertConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=__magic_name__ , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def lowerCamelCase (self ) -> List[str]: '''simple docstring''' snake_case_ : Tuple = self.prepare_config_and_inputs() snake_case_ , snake_case_ , snake_case_ , snake_case_ : Optional[int] = config_and_inputs snake_case_ : int = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': attention_mask} return config, inputs_dict @require_flax class __lowerCAmelCase ( _a, unittest.TestCase ): lowerCamelCase_ : Optional[int] = ( ( FlaxAlbertModel, FlaxAlbertForPreTraining, FlaxAlbertForMaskedLM, FlaxAlbertForMultipleChoice, FlaxAlbertForQuestionAnswering, FlaxAlbertForSequenceClassification, FlaxAlbertForTokenClassification, FlaxAlbertForQuestionAnswering, ) if is_flax_available() else () ) def lowerCamelCase (self ) -> List[str]: '''simple docstring''' snake_case_ : Optional[Any] = FlaxAlbertModelTester(self ) @slow def lowerCamelCase (self ) -> Tuple: '''simple docstring''' for model_class_name in self.all_model_classes: snake_case_ : Dict = model_class_name.from_pretrained('''albert-base-v2''' ) snake_case_ : Union[str, Any] = model(np.ones((1, 1) ) ) self.assertIsNotNone(__magic_name__ ) @require_flax class __lowerCAmelCase ( unittest.TestCase ): @slow def lowerCamelCase (self ) -> Optional[Any]: '''simple docstring''' snake_case_ : Optional[Any] = FlaxAlbertModel.from_pretrained('''albert-base-v2''' ) snake_case_ : Optional[int] = np.array([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]] ) snake_case_ : Dict = np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) snake_case_ : Union[str, Any] = model(__magic_name__ , attention_mask=__magic_name__ )[0] snake_case_ : Tuple = (1, 11, 768) self.assertEqual(output.shape , __magic_name__ ) snake_case_ : str = np.array( [[[-0.6_513, 1.5_035, -0.2_766], [-0.6_515, 1.5_046, -0.2_780], [-0.6_512, 1.5_049, -0.2_784]]] ) self.assertTrue(jnp.allclose(output[:, 1:4, 1:4] , __magic_name__ , atol=1e-4 ) )
279
1
"""simple docstring""" import os from shutil import copyfile from typing import List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging lowercase__ = logging.get_logger(__name__) lowercase__ = {"vocab_file": "sentencepiece.model"} lowercase__ = { "vocab_file": { "google/rembert": "https://huggingface.co/google/rembert/resolve/main/sentencepiece.model", }, } lowercase__ = { "google/rembert": 256, } class __snake_case ( lowercase_ ): a__ = VOCAB_FILES_NAMES a__ = PRETRAINED_VOCAB_FILES_MAP a__ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES def __init__( self , lowercase , lowercase=False , lowercase=True , lowercase=True , lowercase="[CLS]" , lowercase="[SEP]" , lowercase="[UNK]" , lowercase="[SEP]" , lowercase="[PAD]" , lowercase="[CLS]" , lowercase="[MASK]" , **lowercase , ) -> Tuple: '''simple docstring''' super().__init__( do_lower_case=a__ , remove_space=a__ , keep_accents=a__ , bos_token=a__ , eos_token=a__ , unk_token=a__ , sep_token=a__ , pad_token=a__ , cls_token=a__ , mask_token=a__ , **a__ , ) a__: Tuple = do_lower_case a__: int = remove_space a__: int = keep_accents a__: Union[str, Any] = vocab_file a__: List[Any] = spm.SentencePieceProcessor() self.sp_model.Load(a__) @property def lowerCamelCase_ ( self) -> Optional[int]: '''simple docstring''' return len(self.sp_model) def lowerCamelCase_ ( self) -> Optional[Any]: '''simple docstring''' a__: Dict = {self.convert_ids_to_tokens(a__): i for i in range(self.vocab_size)} vocab.update(self.added_tokens_encoder) return vocab def __getstate__( self) -> Optional[int]: '''simple docstring''' a__: Any = self.__dict__.copy() a__: Dict = None return state def __setstate__( self , lowercase) -> Optional[Any]: '''simple docstring''' a__: List[str] = d a__: int = spm.SentencePieceProcessor() self.sp_model.Load(self.vocab_file) def lowerCamelCase_ ( self , lowercase , lowercase=False) -> int: '''simple docstring''' a__: str = self.sp_model.EncodeAsPieces(a__) return pieces def lowerCamelCase_ ( self , lowercase) -> Tuple: '''simple docstring''' return self.sp_model.PieceToId(a__) def lowerCamelCase_ ( self , lowercase) -> List[Any]: '''simple docstring''' return self.sp_model.IdToPiece(a__) def lowerCamelCase_ ( self , lowercase) -> List[str]: '''simple docstring''' a__: List[Any] = self.sp_model.decode_pieces(a__) return out_string def lowerCamelCase_ ( self , lowercase , lowercase = None) -> List[int]: '''simple docstring''' a__: Union[str, Any] = [self.sep_token_id] a__: Optional[Any] = [self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def lowerCamelCase_ ( self , lowercase , lowercase = None , lowercase = False) -> List[int]: '''simple docstring''' if already_has_special_tokens: if token_ids_a is not None: raise ValueError( 'You should not supply a second sequence if the provided sequence of ' 'ids is already formatted with special tokens for the model.') return [1 if x in [self.sep_token_id, self.cls_token_id] else 0 for x in token_ids_a] if token_ids_a is not None: return [1] + ([0] * len(a__)) + [1] + ([0] * len(a__)) + [1] return [1] + ([0] * len(a__)) + [1] def lowerCamelCase_ ( self , lowercase , lowercase = None) -> List[int]: '''simple docstring''' a__: Union[str, Any] = [self.sep_token_id] a__: int = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep) * [0] return len(cls + token_ids_a + sep) * [0] + len(token_ids_a + sep) * [1] def lowerCamelCase_ ( self , lowercase , lowercase = None) -> Tuple[str]: '''simple docstring''' if not os.path.isdir(a__): logger.error('Vocabulary path ({}) should be a directory'.format(a__)) return a__: Optional[int] = os.path.join( a__ , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file']) if os.path.abspath(self.vocab_file) != os.path.abspath(a__): copyfile(self.vocab_file , a__) return (out_vocab_file,)
290
'''simple docstring''' import warnings from functools import wraps from typing import Callable def UpperCamelCase_( snake_case : Callable ): '''simple docstring''' @wraps(snake_case ) def _inner_fn(*snake_case : Optional[int] , **snake_case : List[Any] ): warnings.warn( (f'\'{fn.__name__}\' is experimental and might be subject to breaking changes in the future.') , snake_case , ) return fn(*snake_case , **snake_case ) return _inner_fn
85
0
'''simple docstring''' import argparse import json from pathlib import Path import requests import torch from huggingface_hub import cached_download, hf_hub_url from PIL import Image from transformers import DPTConfig, DPTForDepthEstimation, DPTForSemanticSegmentation, DPTImageProcessor from transformers.utils import logging logging.set_verbosity_info() UpperCamelCase__ = logging.get_logger(__name__) def a__ ( lowerCAmelCase__ ) -> Optional[Any]: UpperCAmelCase__ : str = DPTConfig() if "large" in checkpoint_url: UpperCAmelCase__ : List[Any] = 10_24 UpperCAmelCase__ : Any = 40_96 UpperCAmelCase__ : Tuple = 24 UpperCAmelCase__ : List[Any] = 16 UpperCAmelCase__ : Optional[Any] = [5, 11, 17, 23] UpperCAmelCase__ : int = [2_56, 5_12, 10_24, 10_24] UpperCAmelCase__ : Optional[Any] = (1, 3_84, 3_84) if "ade" in checkpoint_url: UpperCAmelCase__ : int = True UpperCAmelCase__ : List[Any] = 1_50 UpperCAmelCase__ : int = '''huggingface/label-files''' UpperCAmelCase__ : int = '''ade20k-id2label.json''' UpperCAmelCase__ : Optional[Any] = json.load(open(cached_download(hf_hub_url(lowerCAmelCase__ , lowerCAmelCase__ , repo_type='''dataset''' ) ) , '''r''' ) ) UpperCAmelCase__ : Any = {int(lowerCAmelCase__ ): v for k, v in idalabel.items()} UpperCAmelCase__ : Any = idalabel UpperCAmelCase__ : Optional[Any] = {v: k for k, v in idalabel.items()} UpperCAmelCase__ : Any = [1, 1_50, 4_80, 4_80] return config, expected_shape def a__ ( lowerCAmelCase__ ) -> Dict: UpperCAmelCase__ : Optional[int] = ['''pretrained.model.head.weight''', '''pretrained.model.head.bias'''] for k in ignore_keys: state_dict.pop(lowerCAmelCase__ , lowerCAmelCase__ ) def a__ ( lowerCAmelCase__ ) -> Optional[int]: if ( "pretrained.model" in name and "cls_token" not in name and "pos_embed" not in name and "patch_embed" not in name ): UpperCAmelCase__ : Optional[int] = name.replace('''pretrained.model''' , '''dpt.encoder''' ) if "pretrained.model" in name: UpperCAmelCase__ : Any = name.replace('''pretrained.model''' , '''dpt.embeddings''' ) if "patch_embed" in name: UpperCAmelCase__ : List[str] = name.replace('''patch_embed''' , '''patch_embeddings''' ) if "pos_embed" in name: UpperCAmelCase__ : Optional[int] = name.replace('''pos_embed''' , '''position_embeddings''' ) if "attn.proj" in name: UpperCAmelCase__ : List[Any] = name.replace('''attn.proj''' , '''attention.output.dense''' ) if "proj" in name and "project" not in name: UpperCAmelCase__ : Any = name.replace('''proj''' , '''projection''' ) if "blocks" in name: UpperCAmelCase__ : Dict = name.replace('''blocks''' , '''layer''' ) if "mlp.fc1" in name: UpperCAmelCase__ : List[Any] = name.replace('''mlp.fc1''' , '''intermediate.dense''' ) if "mlp.fc2" in name: UpperCAmelCase__ : Optional[Any] = name.replace('''mlp.fc2''' , '''output.dense''' ) if "norm1" in name: UpperCAmelCase__ : int = name.replace('''norm1''' , '''layernorm_before''' ) if "norm2" in name: UpperCAmelCase__ : Union[str, Any] = name.replace('''norm2''' , '''layernorm_after''' ) if "scratch.output_conv" in name: UpperCAmelCase__ : Optional[int] = name.replace('''scratch.output_conv''' , '''head''' ) if "scratch" in name: UpperCAmelCase__ : int = name.replace('''scratch''' , '''neck''' ) if "layer1_rn" in name: UpperCAmelCase__ : List[Any] = name.replace('''layer1_rn''' , '''convs.0''' ) if "layer2_rn" in name: UpperCAmelCase__ : Optional[Any] = name.replace('''layer2_rn''' , '''convs.1''' ) if "layer3_rn" in name: UpperCAmelCase__ : Any = name.replace('''layer3_rn''' , '''convs.2''' ) if "layer4_rn" in name: UpperCAmelCase__ : Optional[Any] = name.replace('''layer4_rn''' , '''convs.3''' ) if "refinenet" in name: UpperCAmelCase__ : Union[str, Any] = int(name[len('''neck.refinenet''' ) : len('''neck.refinenet''' ) + 1] ) # tricky here: we need to map 4 to 0, 3 to 1, 2 to 2 and 1 to 3 UpperCAmelCase__ : Optional[int] = name.replace(F"""refinenet{layer_idx}""" , F"""fusion_stage.layers.{abs(layer_idx-4 )}""" ) if "out_conv" in name: UpperCAmelCase__ : List[str] = name.replace('''out_conv''' , '''projection''' ) if "resConfUnit1" in name: UpperCAmelCase__ : List[str] = name.replace('''resConfUnit1''' , '''residual_layer1''' ) if "resConfUnit2" in name: UpperCAmelCase__ : str = name.replace('''resConfUnit2''' , '''residual_layer2''' ) if "conv1" in name: UpperCAmelCase__ : Optional[Any] = name.replace('''conv1''' , '''convolution1''' ) if "conv2" in name: UpperCAmelCase__ : Optional[int] = name.replace('''conv2''' , '''convolution2''' ) # readout blocks if "pretrained.act_postprocess1.0.project.0" in name: UpperCAmelCase__ : int = name.replace('''pretrained.act_postprocess1.0.project.0''' , '''neck.reassemble_stage.readout_projects.0.0''' ) if "pretrained.act_postprocess2.0.project.0" in name: UpperCAmelCase__ : Tuple = name.replace('''pretrained.act_postprocess2.0.project.0''' , '''neck.reassemble_stage.readout_projects.1.0''' ) if "pretrained.act_postprocess3.0.project.0" in name: UpperCAmelCase__ : List[Any] = name.replace('''pretrained.act_postprocess3.0.project.0''' , '''neck.reassemble_stage.readout_projects.2.0''' ) if "pretrained.act_postprocess4.0.project.0" in name: UpperCAmelCase__ : Optional[Any] = name.replace('''pretrained.act_postprocess4.0.project.0''' , '''neck.reassemble_stage.readout_projects.3.0''' ) # resize blocks if "pretrained.act_postprocess1.3" in name: UpperCAmelCase__ : List[str] = name.replace('''pretrained.act_postprocess1.3''' , '''neck.reassemble_stage.layers.0.projection''' ) if "pretrained.act_postprocess1.4" in name: UpperCAmelCase__ : Any = name.replace('''pretrained.act_postprocess1.4''' , '''neck.reassemble_stage.layers.0.resize''' ) if "pretrained.act_postprocess2.3" in name: UpperCAmelCase__ : str = name.replace('''pretrained.act_postprocess2.3''' , '''neck.reassemble_stage.layers.1.projection''' ) if "pretrained.act_postprocess2.4" in name: UpperCAmelCase__ : List[Any] = name.replace('''pretrained.act_postprocess2.4''' , '''neck.reassemble_stage.layers.1.resize''' ) if "pretrained.act_postprocess3.3" in name: UpperCAmelCase__ : Tuple = name.replace('''pretrained.act_postprocess3.3''' , '''neck.reassemble_stage.layers.2.projection''' ) if "pretrained.act_postprocess4.3" in name: UpperCAmelCase__ : List[Any] = name.replace('''pretrained.act_postprocess4.3''' , '''neck.reassemble_stage.layers.3.projection''' ) if "pretrained.act_postprocess4.4" in name: UpperCAmelCase__ : List[Any] = name.replace('''pretrained.act_postprocess4.4''' , '''neck.reassemble_stage.layers.3.resize''' ) if "pretrained" in name: UpperCAmelCase__ : int = name.replace('''pretrained''' , '''dpt''' ) if "bn" in name: UpperCAmelCase__ : Optional[int] = name.replace('''bn''' , '''batch_norm''' ) if "head" in name: UpperCAmelCase__ : Union[str, Any] = name.replace('''head''' , '''head.head''' ) if "encoder.norm" in name: UpperCAmelCase__ : Union[str, Any] = name.replace('''encoder.norm''' , '''layernorm''' ) if "auxlayer" in name: UpperCAmelCase__ : Any = name.replace('''auxlayer''' , '''auxiliary_head.head''' ) return name def a__ ( lowerCAmelCase__ , lowerCAmelCase__ ) -> Union[str, Any]: for i in range(config.num_hidden_layers ): # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) UpperCAmelCase__ : int = state_dict.pop(F"""dpt.encoder.layer.{i}.attn.qkv.weight""" ) UpperCAmelCase__ : Dict = state_dict.pop(F"""dpt.encoder.layer.{i}.attn.qkv.bias""" ) # next, add query, keys and values (in that order) to the state dict UpperCAmelCase__ : Union[str, Any] = in_proj_weight[: config.hidden_size, :] UpperCAmelCase__ : List[str] = in_proj_bias[: config.hidden_size] UpperCAmelCase__ : List[str] = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] UpperCAmelCase__ : str = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] UpperCAmelCase__ : Optional[Any] = in_proj_weight[ -config.hidden_size :, : ] UpperCAmelCase__ : Dict = in_proj_bias[-config.hidden_size :] def a__ ( ) -> str: UpperCAmelCase__ : Dict = '''http://images.cocodataset.org/val2017/000000039769.jpg''' UpperCAmelCase__ : int = Image.open(requests.get(lowerCAmelCase__ , stream=lowerCAmelCase__ ).raw ) return im @torch.no_grad() def a__ ( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) -> Any: UpperCAmelCase__ , UpperCAmelCase__ : str = get_dpt_config(lowerCAmelCase__ ) # load original state_dict from URL UpperCAmelCase__ : Any = torch.hub.load_state_dict_from_url(lowerCAmelCase__ , map_location='''cpu''' ) # remove certain keys remove_ignore_keys_(lowerCAmelCase__ ) # rename keys for key in state_dict.copy().keys(): UpperCAmelCase__ : int = state_dict.pop(lowerCAmelCase__ ) UpperCAmelCase__ : List[Any] = val # read in qkv matrices read_in_q_k_v(lowerCAmelCase__ , lowerCAmelCase__ ) # load HuggingFace model UpperCAmelCase__ : Optional[Any] = DPTForSemanticSegmentation(lowerCAmelCase__ ) if '''ade''' in checkpoint_url else DPTForDepthEstimation(lowerCAmelCase__ ) model.load_state_dict(lowerCAmelCase__ ) model.eval() # Check outputs on an image UpperCAmelCase__ : Optional[Any] = 4_80 if '''ade''' in checkpoint_url else 3_84 UpperCAmelCase__ : List[str] = DPTImageProcessor(size=lowerCAmelCase__ ) UpperCAmelCase__ : Dict = prepare_img() UpperCAmelCase__ : Tuple = image_processor(lowerCAmelCase__ , return_tensors='''pt''' ) # forward pass UpperCAmelCase__ : List[str] = model(**lowerCAmelCase__ ).logits if '''ade''' in checkpoint_url else model(**lowerCAmelCase__ ).predicted_depth # Assert logits UpperCAmelCase__ : str = torch.tensor([[6.3_1_9_9, 6.3_6_2_9, 6.4_1_4_8], [6.3_8_5_0, 6.3_6_1_5, 6.4_1_6_6], [6.3_5_1_9, 6.3_1_7_6, 6.3_5_7_5]] ) if "ade" in checkpoint_url: UpperCAmelCase__ : Optional[int] = torch.tensor([[4.0_4_8_0, 4.2_4_2_0, 4.4_3_6_0], [4.3_1_2_4, 4.5_6_9_3, 4.8_2_6_1], [4.5_7_6_8, 4.8_9_6_5, 5.2_1_6_3]] ) assert outputs.shape == torch.Size(lowerCAmelCase__ ) assert ( torch.allclose(outputs[0, 0, :3, :3] , lowerCAmelCase__ , atol=1E-4 ) if "ade" in checkpoint_url else torch.allclose(outputs[0, :3, :3] , lowerCAmelCase__ ) ) Path(lowerCAmelCase__ ).mkdir(exist_ok=lowerCAmelCase__ ) print(F"""Saving model to {pytorch_dump_folder_path}""" ) model.save_pretrained(lowerCAmelCase__ ) print(F"""Saving image processor to {pytorch_dump_folder_path}""" ) image_processor.save_pretrained(lowerCAmelCase__ ) if push_to_hub: print('''Pushing model to hub...''' ) model.push_to_hub( repo_path_or_name=Path(lowerCAmelCase__ , lowerCAmelCase__ ) , organization='''nielsr''' , commit_message='''Add model''' , use_temp_dir=lowerCAmelCase__ , ) image_processor.push_to_hub( repo_path_or_name=Path(lowerCAmelCase__ , lowerCAmelCase__ ) , organization='''nielsr''' , commit_message='''Add image processor''' , use_temp_dir=lowerCAmelCase__ , ) if __name__ == "__main__": UpperCamelCase__ = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--checkpoint_url''', default='''https://github.com/intel-isl/DPT/releases/download/1_0/dpt_large-midas-2f21e586.pt''', type=str, help='''URL of the original DPT checkpoint you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model directory.''', ) parser.add_argument( '''--push_to_hub''', action='''store_true''', ) parser.add_argument( '''--model_name''', default='''dpt-large''', type=str, help='''Name of the model, in case you\'re pushing to the hub.''', ) UpperCamelCase__ = parser.parse_args() convert_dpt_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path, args.push_to_hub, args.model_name)
299
'''simple docstring''' import json import os import shutil import tempfile import unittest from multiprocessing import get_context from pathlib import Path import datasets import numpy as np from datasets import load_dataset from parameterized import parameterized from transformers import AutoProcessor from transformers.models.wavaveca import WavaVecaCTCTokenizer, WavaVecaFeatureExtractor from transformers.models.wavaveca.tokenization_wavaveca import VOCAB_FILES_NAMES from transformers.testing_utils import require_pyctcdecode, require_torch, require_torchaudio, slow from transformers.utils import FEATURE_EXTRACTOR_NAME, is_pyctcdecode_available, is_torch_available from ..wavaveca.test_feature_extraction_wavaveca import floats_list if is_pyctcdecode_available(): from huggingface_hub import snapshot_download from pyctcdecode import BeamSearchDecoderCTC from transformers.models.wavaveca_with_lm import WavaVecaProcessorWithLM from transformers.models.wavaveca_with_lm.processing_wavaveca_with_lm import WavaVecaDecoderWithLMOutput if is_torch_available(): from transformers import WavaVecaForCTC @require_pyctcdecode class lowerCamelCase_ ( unittest.TestCase ): def lowercase_ ( self : Optional[Any] ): '''simple docstring''' UpperCAmelCase__ : Any = '''| <pad> <unk> <s> </s> a b c d e f g h i j k'''.split() UpperCAmelCase__ : Optional[Any] = dict(zip(_A , range(len(_A ) ) ) ) UpperCAmelCase__ : Tuple = { '''unk_token''': '''<unk>''', '''bos_token''': '''<s>''', '''eos_token''': '''</s>''', } UpperCAmelCase__ : Optional[int] = { '''feature_size''': 1, '''padding_value''': 0.0, '''sampling_rate''': 16_000, '''return_attention_mask''': False, '''do_normalize''': True, } UpperCAmelCase__ : Union[str, Any] = tempfile.mkdtemp() UpperCAmelCase__ : Any = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) UpperCAmelCase__ : int = os.path.join(self.tmpdirname , _A ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(_A ) + '''\n''' ) with open(self.feature_extraction_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(_A ) + '''\n''' ) # load decoder from hub UpperCAmelCase__ : Any = '''hf-internal-testing/ngram-beam-search-decoder''' def lowercase_ ( self : int , **_A : Optional[int] ): '''simple docstring''' UpperCAmelCase__ : Dict = self.add_kwargs_tokens_map.copy() kwargs.update(_A ) return WavaVecaCTCTokenizer.from_pretrained(self.tmpdirname , **_A ) def lowercase_ ( self : str , **_A : Any ): '''simple docstring''' return WavaVecaFeatureExtractor.from_pretrained(self.tmpdirname , **_A ) def lowercase_ ( self : str , **_A : Any ): '''simple docstring''' return BeamSearchDecoderCTC.load_from_hf_hub(self.decoder_name , **_A ) def lowercase_ ( self : Any ): '''simple docstring''' shutil.rmtree(self.tmpdirname ) def lowercase_ ( self : Dict ): '''simple docstring''' UpperCAmelCase__ : Union[str, Any] = self.get_tokenizer() UpperCAmelCase__ : Any = self.get_feature_extractor() UpperCAmelCase__ : Tuple = self.get_decoder() UpperCAmelCase__ : Tuple = WavaVecaProcessorWithLM(tokenizer=_A , feature_extractor=_A , decoder=_A ) processor.save_pretrained(self.tmpdirname ) UpperCAmelCase__ : Union[str, Any] = WavaVecaProcessorWithLM.from_pretrained(self.tmpdirname ) # tokenizer self.assertEqual(processor.tokenizer.get_vocab() , tokenizer.get_vocab() ) self.assertIsInstance(processor.tokenizer , _A ) # feature extractor self.assertEqual(processor.feature_extractor.to_json_string() , feature_extractor.to_json_string() ) self.assertIsInstance(processor.feature_extractor , _A ) # decoder self.assertEqual(processor.decoder._alphabet.labels , decoder._alphabet.labels ) self.assertEqual( processor.decoder.model_container[decoder._model_key]._unigram_set , decoder.model_container[decoder._model_key]._unigram_set , ) self.assertIsInstance(processor.decoder , _A ) def lowercase_ ( self : int ): '''simple docstring''' UpperCAmelCase__ : Optional[Any] = WavaVecaProcessorWithLM( tokenizer=self.get_tokenizer() , feature_extractor=self.get_feature_extractor() , decoder=self.get_decoder() ) processor.save_pretrained(self.tmpdirname ) # make sure that error is thrown when decoder alphabet doesn't match UpperCAmelCase__ : Optional[int] = WavaVecaProcessorWithLM.from_pretrained( self.tmpdirname , alpha=5.0 , beta=3.0 , score_boundary=-7.0 , unk_score_offset=3 ) # decoder self.assertEqual(processor.language_model.alpha , 5.0 ) self.assertEqual(processor.language_model.beta , 3.0 ) self.assertEqual(processor.language_model.score_boundary , -7.0 ) self.assertEqual(processor.language_model.unk_score_offset , 3 ) def lowercase_ ( self : Tuple ): '''simple docstring''' UpperCAmelCase__ : Union[str, Any] = self.get_tokenizer() # add token to trigger raise tokenizer.add_tokens(['''xx'''] ) with self.assertRaisesRegex(_A , '''include''' ): WavaVecaProcessorWithLM( tokenizer=_A , feature_extractor=self.get_feature_extractor() , decoder=self.get_decoder() ) def lowercase_ ( self : Optional[int] ): '''simple docstring''' UpperCAmelCase__ : Any = self.get_feature_extractor() UpperCAmelCase__ : Optional[int] = self.get_tokenizer() UpperCAmelCase__ : Any = self.get_decoder() UpperCAmelCase__ : Optional[Any] = WavaVecaProcessorWithLM(tokenizer=_A , feature_extractor=_A , decoder=_A ) UpperCAmelCase__ : List[Any] = floats_list((3, 1_000) ) UpperCAmelCase__ : Dict = feature_extractor(_A , return_tensors='''np''' ) UpperCAmelCase__ : str = processor(_A , return_tensors='''np''' ) for key in input_feat_extract.keys(): self.assertAlmostEqual(input_feat_extract[key].sum() , input_processor[key].sum() , delta=1e-2 ) def lowercase_ ( self : Tuple ): '''simple docstring''' UpperCAmelCase__ : Optional[int] = self.get_feature_extractor() UpperCAmelCase__ : str = self.get_tokenizer() UpperCAmelCase__ : str = self.get_decoder() UpperCAmelCase__ : Union[str, Any] = WavaVecaProcessorWithLM(tokenizer=_A , feature_extractor=_A , decoder=_A ) UpperCAmelCase__ : Union[str, Any] = '''This is a test string''' UpperCAmelCase__ : Optional[int] = processor(text=_A ) UpperCAmelCase__ : List[str] = tokenizer(_A ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def lowercase_ ( self : Dict , _A : Optional[int]=(2, 10, 16) , _A : List[str]=77 ): '''simple docstring''' np.random.seed(_A ) return np.random.rand(*_A ) def lowercase_ ( self : Any ): '''simple docstring''' UpperCAmelCase__ : Optional[int] = self.get_feature_extractor() UpperCAmelCase__ : Union[str, Any] = self.get_tokenizer() UpperCAmelCase__ : Optional[Any] = self.get_decoder() UpperCAmelCase__ : Tuple = WavaVecaProcessorWithLM(tokenizer=_A , feature_extractor=_A , decoder=_A ) UpperCAmelCase__ : int = self._get_dummy_logits(shape=(10, 16) , seed=13 ) UpperCAmelCase__ : List[Any] = processor.decode(_A ) UpperCAmelCase__ : List[Any] = decoder.decode_beams(_A )[0] self.assertEqual(decoded_decoder[0] , decoded_processor.text ) self.assertEqual('''</s> <s> </s>''' , decoded_processor.text ) self.assertEqual(decoded_decoder[-2] , decoded_processor.logit_score ) self.assertEqual(decoded_decoder[-1] , decoded_processor.lm_score ) @parameterized.expand([[None], ['''fork'''], ['''spawn''']] ) def lowercase_ ( self : Any , _A : str ): '''simple docstring''' UpperCAmelCase__ : Any = self.get_feature_extractor() UpperCAmelCase__ : Tuple = self.get_tokenizer() UpperCAmelCase__ : Tuple = self.get_decoder() UpperCAmelCase__ : Any = WavaVecaProcessorWithLM(tokenizer=_A , feature_extractor=_A , decoder=_A ) UpperCAmelCase__ : Optional[Any] = self._get_dummy_logits() # note: pool should be instantiated *after* Wav2Vec2ProcessorWithLM. # otherwise, the LM won't be available to the pool's sub-processes. # manual logic used to allow parameterized test for both pool=None and pool=Pool(...) if pool_context is None: UpperCAmelCase__ : Union[str, Any] = processor.batch_decode(_A ) else: with get_context(_A ).Pool() as pool: UpperCAmelCase__ : Union[str, Any] = processor.batch_decode(_A , _A ) UpperCAmelCase__ : str = list(_A ) with get_context('''fork''' ).Pool() as p: UpperCAmelCase__ : Dict = decoder.decode_beams_batch(_A , _A ) UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ : Dict = [], [], [] for beams in decoded_beams: texts_decoder.append(beams[0][0] ) logit_scores_decoder.append(beams[0][-2] ) lm_scores_decoder.append(beams[0][-1] ) self.assertListEqual(_A , decoded_processor.text ) self.assertListEqual(['''<s> <s> </s>''', '''<s> <s> <s>'''] , decoded_processor.text ) self.assertListEqual(_A , decoded_processor.logit_score ) self.assertListEqual(_A , decoded_processor.lm_score ) def lowercase_ ( self : Union[str, Any] ): '''simple docstring''' UpperCAmelCase__ : str = self.get_feature_extractor() UpperCAmelCase__ : List[Any] = self.get_tokenizer() UpperCAmelCase__ : int = self.get_decoder() UpperCAmelCase__ : Optional[int] = WavaVecaProcessorWithLM(tokenizer=_A , feature_extractor=_A , decoder=_A ) UpperCAmelCase__ : str = self._get_dummy_logits() UpperCAmelCase__ : Optional[int] = 15 UpperCAmelCase__ : Dict = -2_0.0 UpperCAmelCase__ : Optional[Any] = -4.0 UpperCAmelCase__ : Union[str, Any] = processor.batch_decode( _A , beam_width=_A , beam_prune_logp=_A , token_min_logp=_A , ) UpperCAmelCase__ : List[Any] = decoded_processor_out.text UpperCAmelCase__ : List[str] = list(_A ) with get_context('''fork''' ).Pool() as pool: UpperCAmelCase__ : Tuple = decoder.decode_beams_batch( _A , _A , beam_width=_A , beam_prune_logp=_A , token_min_logp=_A , ) UpperCAmelCase__ : Optional[int] = [d[0][0] for d in decoded_decoder_out] UpperCAmelCase__ : Optional[Any] = [d[0][2] for d in decoded_decoder_out] UpperCAmelCase__ : Optional[int] = [d[0][3] for d in decoded_decoder_out] self.assertListEqual(_A , _A ) self.assertListEqual(['''</s> <s> <s>''', '''<s> <s> <s>'''] , _A ) self.assertTrue(np.array_equal(_A , decoded_processor_out.logit_score ) ) self.assertTrue(np.allclose([-2_0.0_5_4, -1_8.4_4_7] , _A , atol=1e-3 ) ) self.assertTrue(np.array_equal(_A , decoded_processor_out.lm_score ) ) self.assertTrue(np.allclose([-1_5.5_5_4, -1_3.9_4_7_4] , _A , atol=1e-3 ) ) def lowercase_ ( self : str ): '''simple docstring''' UpperCAmelCase__ : Optional[Any] = self.get_feature_extractor() UpperCAmelCase__ : Optional[Any] = self.get_tokenizer() UpperCAmelCase__ : Dict = self.get_decoder() UpperCAmelCase__ : int = WavaVecaProcessorWithLM(tokenizer=_A , feature_extractor=_A , decoder=_A ) UpperCAmelCase__ : Optional[int] = self._get_dummy_logits() UpperCAmelCase__ : List[str] = 2.0 UpperCAmelCase__ : Union[str, Any] = 5.0 UpperCAmelCase__ : str = -2_0.0 UpperCAmelCase__ : Optional[int] = True UpperCAmelCase__ : Union[str, Any] = processor.batch_decode( _A , alpha=_A , beta=_A , unk_score_offset=_A , lm_score_boundary=_A , ) UpperCAmelCase__ : Union[str, Any] = decoded_processor_out.text UpperCAmelCase__ : Tuple = list(_A ) decoder.reset_params( alpha=_A , beta=_A , unk_score_offset=_A , lm_score_boundary=_A , ) with get_context('''fork''' ).Pool() as pool: UpperCAmelCase__ : Optional[Any] = decoder.decode_beams_batch( _A , _A , ) UpperCAmelCase__ : str = [d[0][0] for d in decoded_decoder_out] self.assertListEqual(_A , _A ) self.assertListEqual(['''<s> </s> <s> </s> </s>''', '''</s> </s> <s> </s> </s>'''] , _A ) UpperCAmelCase__ : Optional[Any] = processor.decoder.model_container[processor.decoder._model_key] self.assertEqual(lm_model.alpha , 2.0 ) self.assertEqual(lm_model.beta , 5.0 ) self.assertEqual(lm_model.unk_score_offset , -2_0.0 ) self.assertEqual(lm_model.score_boundary , _A ) def lowercase_ ( self : int ): '''simple docstring''' UpperCAmelCase__ : Optional[int] = WavaVecaProcessorWithLM.from_pretrained('''hf-internal-testing/processor_with_lm''' ) UpperCAmelCase__ : Dict = processor.decoder.model_container[processor.decoder._model_key] UpperCAmelCase__ : Optional[int] = Path(language_model._kenlm_model.path.decode('''utf-8''' ) ).parent.parent.absolute() UpperCAmelCase__ : Dict = os.listdir(_A ) UpperCAmelCase__ : Optional[Any] = ['''alphabet.json''', '''language_model'''] downloaded_decoder_files.sort() expected_decoder_files.sort() # test that only decoder relevant files from # https://huggingface.co/hf-internal-testing/processor_with_lm/tree/main # are downloaded and none of the rest (e.g. README.md, ...) self.assertListEqual(_A , _A ) def lowercase_ ( self : Optional[int] ): '''simple docstring''' UpperCAmelCase__ : str = snapshot_download('''hf-internal-testing/processor_with_lm''' ) UpperCAmelCase__ : Any = WavaVecaProcessorWithLM.from_pretrained(_A ) UpperCAmelCase__ : Optional[int] = processor.decoder.model_container[processor.decoder._model_key] UpperCAmelCase__ : str = Path(language_model._kenlm_model.path.decode('''utf-8''' ) ).parent.parent.absolute() UpperCAmelCase__ : List[str] = os.listdir(_A ) UpperCAmelCase__ : Any = os.listdir(_A ) local_decoder_files.sort() expected_decoder_files.sort() # test that both decoder form hub and local files in cache are the same self.assertListEqual(_A , _A ) def lowercase_ ( self : Optional[int] ): '''simple docstring''' UpperCAmelCase__ : Dict = WavaVecaProcessorWithLM.from_pretrained('''hf-internal-testing/processor_with_lm''' ) UpperCAmelCase__ : Dict = AutoProcessor.from_pretrained('''hf-internal-testing/processor_with_lm''' ) UpperCAmelCase__ : Tuple = floats_list((3, 1_000) ) UpperCAmelCase__ : int = processor_wavaveca(_A , return_tensors='''np''' ) UpperCAmelCase__ : List[str] = processor_auto(_A , return_tensors='''np''' ) for key in input_wavaveca.keys(): self.assertAlmostEqual(input_wavaveca[key].sum() , input_auto[key].sum() , delta=1e-2 ) UpperCAmelCase__ : Tuple = self._get_dummy_logits() UpperCAmelCase__ : List[str] = processor_wavaveca.batch_decode(_A ) UpperCAmelCase__ : int = processor_auto.batch_decode(_A ) self.assertListEqual(decoded_wavaveca.text , decoded_auto.text ) def lowercase_ ( self : Optional[int] ): '''simple docstring''' UpperCAmelCase__ : int = self.get_feature_extractor() UpperCAmelCase__ : int = self.get_tokenizer() UpperCAmelCase__ : Optional[Any] = self.get_decoder() UpperCAmelCase__ : Optional[int] = WavaVecaProcessorWithLM(tokenizer=_A , feature_extractor=_A , decoder=_A ) self.assertListEqual( processor.model_input_names , feature_extractor.model_input_names , msg='''`processor` and `feature_extractor` model input names do not match''' , ) @staticmethod def lowercase_ ( _A : Dict , _A : str ): '''simple docstring''' UpperCAmelCase__ : int = [d[key] for d in offsets] return retrieved_list def lowercase_ ( self : Any ): '''simple docstring''' UpperCAmelCase__ : Optional[Any] = WavaVecaProcessorWithLM.from_pretrained('''hf-internal-testing/processor_with_lm''' ) UpperCAmelCase__ : str = self._get_dummy_logits()[0] UpperCAmelCase__ : List[str] = processor.decode(_A , output_word_offsets=_A ) # check Wav2Vec2CTCTokenizerOutput keys for word self.assertEqual(len(outputs.keys() ) , 4 ) self.assertTrue('''text''' in outputs ) self.assertTrue('''word_offsets''' in outputs ) self.assertTrue(isinstance(_A , _A ) ) self.assertEqual(''' '''.join(self.get_from_offsets(outputs['''word_offsets'''] , '''word''' ) ) , outputs.text ) self.assertListEqual(self.get_from_offsets(outputs['''word_offsets'''] , '''word''' ) , ['''<s>''', '''<s>''', '''</s>'''] ) self.assertListEqual(self.get_from_offsets(outputs['''word_offsets'''] , '''start_offset''' ) , [0, 2, 4] ) self.assertListEqual(self.get_from_offsets(outputs['''word_offsets'''] , '''end_offset''' ) , [1, 3, 5] ) def lowercase_ ( self : Optional[int] ): '''simple docstring''' UpperCAmelCase__ : Any = WavaVecaProcessorWithLM.from_pretrained('''hf-internal-testing/processor_with_lm''' ) UpperCAmelCase__ : Dict = self._get_dummy_logits() UpperCAmelCase__ : Dict = processor.batch_decode(_A , output_word_offsets=_A ) # check Wav2Vec2CTCTokenizerOutput keys for word self.assertEqual(len(outputs.keys() ) , 4 ) self.assertTrue('''text''' in outputs ) self.assertTrue('''word_offsets''' in outputs ) self.assertTrue(isinstance(_A , _A ) ) self.assertListEqual( [''' '''.join(self.get_from_offsets(_A , '''word''' ) ) for o in outputs['''word_offsets''']] , outputs.text ) self.assertListEqual(self.get_from_offsets(outputs['''word_offsets'''][0] , '''word''' ) , ['''<s>''', '''<s>''', '''</s>'''] ) self.assertListEqual(self.get_from_offsets(outputs['''word_offsets'''][0] , '''start_offset''' ) , [0, 2, 4] ) self.assertListEqual(self.get_from_offsets(outputs['''word_offsets'''][0] , '''end_offset''' ) , [1, 3, 5] ) @slow @require_torch @require_torchaudio def lowercase_ ( self : Optional[Any] ): '''simple docstring''' import torch UpperCAmelCase__ : Any = load_dataset('''common_voice''' , '''en''' , split='''train''' , streaming=_A ) UpperCAmelCase__ : Dict = ds.cast_column('''audio''' , datasets.Audio(sampling_rate=16_000 ) ) UpperCAmelCase__ : List[Any] = iter(_A ) UpperCAmelCase__ : Optional[Any] = next(_A ) UpperCAmelCase__ : Any = AutoProcessor.from_pretrained('''patrickvonplaten/wav2vec2-base-100h-with-lm''' ) UpperCAmelCase__ : int = WavaVecaForCTC.from_pretrained('''patrickvonplaten/wav2vec2-base-100h-with-lm''' ) # compare to filename `common_voice_en_100038.mp3` of dataset viewer on https://huggingface.co/datasets/common_voice/viewer/en/train UpperCAmelCase__ : int = processor(sample['''audio''']['''array'''] , return_tensors='''pt''' ).input_values with torch.no_grad(): UpperCAmelCase__ : Dict = model(_A ).logits.cpu().numpy() UpperCAmelCase__ : int = processor.decode(logits[0] , output_word_offsets=_A ) UpperCAmelCase__ : Any = model.config.inputs_to_logits_ratio / processor.feature_extractor.sampling_rate UpperCAmelCase__ : Any = [ { '''start_time''': d['''start_offset'''] * time_offset, '''end_time''': d['''end_offset'''] * time_offset, '''word''': d['''word'''], } for d in output['''word_offsets'''] ] UpperCAmelCase__ : int = '''WHY DOES MILISANDRA LOOK LIKE SHE WANTS TO CONSUME JOHN SNOW ON THE RIVER AT THE WALL''' # output words self.assertEqual(''' '''.join(self.get_from_offsets(_A , '''word''' ) ) , _A ) self.assertEqual(''' '''.join(self.get_from_offsets(_A , '''word''' ) ) , output.text ) # output times UpperCAmelCase__ : List[Any] = torch.tensor(self.get_from_offsets(_A , '''start_time''' ) ) UpperCAmelCase__ : List[str] = torch.tensor(self.get_from_offsets(_A , '''end_time''' ) ) # fmt: off UpperCAmelCase__ : int = torch.tensor([1.4_1_9_9, 1.6_5_9_9, 2.2_5_9_9, 3.0, 3.2_4, 3.5_9_9_9, 3.7_9_9_9, 4.0_9_9_9, 4.2_6, 4.9_4, 5.2_8, 5.6_5_9_9, 5.7_8, 5.9_4, 6.3_2, 6.5_3_9_9, 6.6_5_9_9] ) UpperCAmelCase__ : List[str] = torch.tensor([1.5_3_9_9, 1.8_9_9_9, 2.9, 3.1_6, 3.5_3_9_9, 3.7_2, 4.0_1_9_9, 4.1_7_9_9, 4.7_6, 5.1_5_9_9, 5.5_5_9_9, 5.6_9_9_9, 5.8_6, 6.1_9_9_9, 6.3_8, 6.6_1_9_9, 6.9_4] ) # fmt: on self.assertTrue(torch.allclose(_A , _A , atol=0.0_1 ) ) self.assertTrue(torch.allclose(_A , _A , atol=0.0_1 ) )
299
1
import unittest from transformers import AutoTokenizer, NystromformerConfig, 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 ( NystromformerForMaskedLM, NystromformerForMultipleChoice, NystromformerForQuestionAnswering, NystromformerForSequenceClassification, NystromformerForTokenClassification, NystromformerModel, ) from transformers.models.nystromformer.modeling_nystromformer import NYSTROMFORMER_PRETRAINED_MODEL_ARCHIVE_LIST class snake_case__ : """simple docstring""" def __init__( self : Dict , __lowerCamelCase : Union[str, Any] , __lowerCamelCase : Any=13 , __lowerCamelCase : int=7 , __lowerCamelCase : List[Any]=True , __lowerCamelCase : str=True , __lowerCamelCase : Union[str, Any]=True , __lowerCamelCase : str=True , __lowerCamelCase : Dict=99 , __lowerCamelCase : Union[str, Any]=32 , __lowerCamelCase : Tuple=5 , __lowerCamelCase : Optional[Any]=4 , __lowerCamelCase : Union[str, Any]=37 , __lowerCamelCase : List[Any]="gelu" , __lowerCamelCase : List[str]=0.1 , __lowerCamelCase : Optional[Any]=0.1 , __lowerCamelCase : Dict=5_12 , __lowerCamelCase : List[Any]=16 , __lowerCamelCase : List[Any]=2 , __lowerCamelCase : List[Any]=0.02 , __lowerCamelCase : List[str]=3 , __lowerCamelCase : Any=4 , __lowerCamelCase : Optional[Any]=None , ) -> Any: 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 __UpperCAmelCase ( self : Optional[int] ) -> 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 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 __UpperCAmelCase ( self : Optional[Any] ) -> List[str]: return NystromformerConfig( 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=__lowerCamelCase , initializer_range=self.initializer_range , ) def __UpperCAmelCase ( self : Any , __lowerCamelCase : Tuple , __lowerCamelCase : str , __lowerCamelCase : Tuple , __lowerCamelCase : int , __lowerCamelCase : List[Any] , __lowerCamelCase : int , __lowerCamelCase : int ) -> int: a = NystromformerModel(config=__lowerCamelCase ) model.to(__lowerCamelCase ) model.eval() a = model(__lowerCamelCase , attention_mask=__lowerCamelCase , token_type_ids=__lowerCamelCase ) a = model(__lowerCamelCase , token_type_ids=__lowerCamelCase ) a = model(__lowerCamelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __UpperCAmelCase ( self : List[Any] , __lowerCamelCase : List[str] , __lowerCamelCase : List[Any] , __lowerCamelCase : List[Any] , __lowerCamelCase : Any , __lowerCamelCase : Any , __lowerCamelCase : Any , __lowerCamelCase : Any ) -> Dict: a = NystromformerForMaskedLM(config=__lowerCamelCase ) model.to(__lowerCamelCase ) model.eval() a = model(__lowerCamelCase , attention_mask=__lowerCamelCase , token_type_ids=__lowerCamelCase , labels=__lowerCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def __UpperCAmelCase ( self : List[Any] , __lowerCamelCase : str , __lowerCamelCase : Union[str, Any] , __lowerCamelCase : Dict , __lowerCamelCase : Tuple , __lowerCamelCase : Union[str, Any] , __lowerCamelCase : Tuple , __lowerCamelCase : Dict ) -> int: a = NystromformerForQuestionAnswering(config=__lowerCamelCase ) model.to(__lowerCamelCase ) model.eval() a = model( __lowerCamelCase , attention_mask=__lowerCamelCase , token_type_ids=__lowerCamelCase , start_positions=__lowerCamelCase , end_positions=__lowerCamelCase , ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def __UpperCAmelCase ( self : Optional[int] , __lowerCamelCase : List[Any] , __lowerCamelCase : Dict , __lowerCamelCase : str , __lowerCamelCase : Tuple , __lowerCamelCase : Any , __lowerCamelCase : int , __lowerCamelCase : str ) -> Dict: a = self.num_labels a = NystromformerForSequenceClassification(__lowerCamelCase ) model.to(__lowerCamelCase ) model.eval() a = model(__lowerCamelCase , attention_mask=__lowerCamelCase , token_type_ids=__lowerCamelCase , labels=__lowerCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __UpperCAmelCase ( self : List[Any] , __lowerCamelCase : Union[str, Any] , __lowerCamelCase : int , __lowerCamelCase : str , __lowerCamelCase : Dict , __lowerCamelCase : Optional[int] , __lowerCamelCase : Optional[Any] , __lowerCamelCase : Tuple ) -> Any: a = self.num_labels a = NystromformerForTokenClassification(config=__lowerCamelCase ) model.to(__lowerCamelCase ) model.eval() a = model(__lowerCamelCase , attention_mask=__lowerCamelCase , token_type_ids=__lowerCamelCase , labels=__lowerCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def __UpperCAmelCase ( self : List[Any] , __lowerCamelCase : int , __lowerCamelCase : Any , __lowerCamelCase : Optional[Any] , __lowerCamelCase : List[str] , __lowerCamelCase : Tuple , __lowerCamelCase : Dict , __lowerCamelCase : List[Any] ) -> int: a = self.num_choices a = NystromformerForMultipleChoice(config=__lowerCamelCase ) model.to(__lowerCamelCase ) 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( __lowerCamelCase , attention_mask=__lowerCamelCase , token_type_ids=__lowerCamelCase , labels=__lowerCamelCase , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def __UpperCAmelCase ( self : Union[str, Any] ) -> Any: 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 snake_case__ (_UpperCamelCase , _UpperCamelCase , unittest.TestCase ): """simple docstring""" SCREAMING_SNAKE_CASE_ : List[Any] = ( ( NystromformerModel, NystromformerForMaskedLM, NystromformerForMultipleChoice, NystromformerForQuestionAnswering, NystromformerForSequenceClassification, NystromformerForTokenClassification, ) if is_torch_available() else () ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = ( { """feature-extraction""": NystromformerModel, """fill-mask""": NystromformerForMaskedLM, """question-answering""": NystromformerForQuestionAnswering, """text-classification""": NystromformerForSequenceClassification, """token-classification""": NystromformerForTokenClassification, """zero-shot""": NystromformerForSequenceClassification, } if is_torch_available() else {} ) SCREAMING_SNAKE_CASE_ : List[str] = False SCREAMING_SNAKE_CASE_ : Optional[int] = False def __UpperCAmelCase ( self : int ) -> Dict: a = NystromformerModelTester(self ) a = ConfigTester(self , config_class=__lowerCamelCase , hidden_size=37 ) def __UpperCAmelCase ( self : List[str] ) -> Dict: self.config_tester.run_common_tests() def __UpperCAmelCase ( self : Tuple ) -> Optional[Any]: a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__lowerCamelCase ) def __UpperCAmelCase ( self : List[Any] ) -> str: a = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: a = type self.model_tester.create_and_check_model(*__lowerCamelCase ) def __UpperCAmelCase ( self : Dict ) -> Tuple: a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*__lowerCamelCase ) def __UpperCAmelCase ( self : Any ) -> Optional[int]: a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_multiple_choice(*__lowerCamelCase ) def __UpperCAmelCase ( self : Union[str, Any] ) -> List[Any]: a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*__lowerCamelCase ) def __UpperCAmelCase ( self : Optional[int] ) -> Dict: a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*__lowerCamelCase ) def __UpperCAmelCase ( self : Tuple ) -> Optional[Any]: a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*__lowerCamelCase ) @slow def __UpperCAmelCase ( self : str ) -> Tuple: for model_name in NYSTROMFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: a = NystromformerModel.from_pretrained(__lowerCamelCase ) self.assertIsNotNone(__lowerCamelCase ) @require_torch class snake_case__ (unittest.TestCase ): """simple docstring""" @slow def __UpperCAmelCase ( self : Optional[int] ) -> Any: a = NystromformerModel.from_pretrained("uw-madison/nystromformer-512" ) a = torch.tensor([[0, 1, 2, 3, 4, 5]] ) with torch.no_grad(): a = model(__lowerCamelCase )[0] a = torch.Size((1, 6, 7_68) ) self.assertEqual(output.shape , __lowerCamelCase ) a = torch.tensor( [[[-0.4_532, -0.0_936, 0.5_137], [-0.2_676, 0.0_628, 0.6_186], [-0.3_629, -0.1_726, 0.4_716]]] ) self.assertTrue(torch.allclose(output[:, :3, :3] , __lowerCamelCase , atol=1e-4 ) ) @slow def __UpperCAmelCase ( self : Tuple ) -> Dict: a = "the [MASK] of Belgium is Brussels" a = AutoTokenizer.from_pretrained("uw-madison/nystromformer-512" ) a = NystromformerForMaskedLM.from_pretrained("uw-madison/nystromformer-512" ) a = tokenizer(__lowerCamelCase , return_tensors="pt" ) with torch.no_grad(): a = model(encoding.input_ids ).logits a = token_logits[:, 2, :].argmax(-1 )[0] self.assertEqual(tokenizer.decode(__lowerCamelCase ) , "capital" )
107
from dataclasses import dataclass, field from typing import Optional from transformers import AutoConfig, AutoImageProcessor, AutoTokenizer, FlaxVisionEncoderDecoderModel, HfArgumentParser @dataclass class snake_case__ : """simple docstring""" SCREAMING_SNAKE_CASE_ : str = field( metadata={"""help""": """The output directory where the model will be written."""} , ) SCREAMING_SNAKE_CASE_ : str = field( metadata={ """help""": ( """The encoder model checkpoint for weights initialization.""" """Don't set if you want to train an encoder model from scratch.""" ) } , ) SCREAMING_SNAKE_CASE_ : str = field( metadata={ """help""": ( """The decoder model checkpoint for weights initialization.""" """Don't set if you want to train a decoder model from scratch.""" ) } , ) SCREAMING_SNAKE_CASE_ : Optional[str] = field( default=_UpperCamelCase , metadata={"""help""": """Pretrained encoder config name or path if not the same as encoder_model_name"""} ) SCREAMING_SNAKE_CASE_ : Optional[str] = field( default=_UpperCamelCase , metadata={"""help""": """Pretrained decoder config name or path if not the same as decoder_model_name"""} ) def __magic_name__ ( ): '''simple docstring''' a = HfArgumentParser((ModelArguments,) ) ((a) , ) = parser.parse_args_into_dataclasses() # Load pretrained model and tokenizer # Use explicit specified encoder config if model_args.encoder_config_name: a = AutoConfig.from_pretrained(model_args.encoder_config_name ) # Use pretrained encoder model's config else: a = AutoConfig.from_pretrained(model_args.encoder_model_name_or_path ) # Use explicit specified decoder config if model_args.decoder_config_name: a = AutoConfig.from_pretrained(model_args.decoder_config_name ) # Use pretrained decoder model's config else: a = AutoConfig.from_pretrained(model_args.decoder_model_name_or_path ) # necessary for `from_encoder_decoder_pretrained` when `decoder_config` is passed a = True a = True a = FlaxVisionEncoderDecoderModel.from_encoder_decoder_pretrained( encoder_pretrained_model_name_or_path=model_args.encoder_model_name_or_path, decoder_pretrained_model_name_or_path=model_args.decoder_model_name_or_path, encoder_config=A, decoder_config=A, ) # GPT2 only has bos/eos tokens but not decoder_start/pad tokens a = decoder_config.decoder_start_token_id a = decoder_config.pad_token_id if decoder_start_token_id is None: a = decoder_config.bos_token_id if pad_token_id is None: a = decoder_config.eos_token_id # This is necessary to make Flax's generate() work a = decoder_config.eos_token_id a = decoder_start_token_id a = pad_token_id a = AutoImageProcessor.from_pretrained(model_args.encoder_model_name_or_path ) a = AutoTokenizer.from_pretrained(model_args.decoder_model_name_or_path ) a = tokenizer.convert_ids_to_tokens(model.config.pad_token_id ) model.save_pretrained(model_args.output_dir ) image_processor.save_pretrained(model_args.output_dir ) tokenizer.save_pretrained(model_args.output_dir ) if __name__ == "__main__": main()
107
1
"""simple docstring""" from ..utils import DummyObject, requires_backends class snake_case ( metaclass=SCREAMING_SNAKE_CASE_ ): a_ : Any = ["""note_seq"""] def __init__( self , *__UpperCAmelCase , **__UpperCAmelCase) ->Union[str, Any]: requires_backends(self , ["note_seq"]) @classmethod def UpperCAmelCase__ ( cls , *__UpperCAmelCase , **__UpperCAmelCase) ->List[Any]: requires_backends(cls , ["note_seq"]) @classmethod def UpperCAmelCase__ ( cls , *__UpperCAmelCase , **__UpperCAmelCase) ->Dict: requires_backends(cls , ["note_seq"])
303
"""simple docstring""" from heapq import heappop, heappush import numpy as np def UpperCamelCase ( UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , ) ->tuple[float | int, list[tuple[int, int]]]: """simple docstring""" 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()
303
1
import inspect import os import re from transformers.configuration_utils import PretrainedConfig from transformers.utils import direct_transformers_import # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/check_config_docstrings.py UpperCAmelCase_ = 'src/transformers' # This is to make sure the transformers module imported is the one in the repo. UpperCAmelCase_ = direct_transformers_import(PATH_TO_TRANSFORMERS) UpperCAmelCase_ = transformers.models.auto.configuration_auto.CONFIG_MAPPING UpperCAmelCase_ = { # used to compute the property `self.chunk_length` 'EncodecConfig': ['overlap'], # used as `self.bert_model = BertModel(config, ...)` 'DPRConfig': True, # not used in modeling files, but it's an important information 'FSMTConfig': ['langs'], # used internally in the configuration class file 'GPTNeoConfig': ['attention_types'], # used internally in the configuration class file 'EsmConfig': ['is_folding_model'], # used during training (despite we don't have training script for these models yet) 'Mask2FormerConfig': ['ignore_value'], # `ignore_value` used during training (despite we don't have training script for these models yet) # `norm` used in conversion script (despite not using in the modeling file) 'OneFormerConfig': ['ignore_value', 'norm'], # used during preprocessing and collation, see `collating_graphormer.py` 'GraphormerConfig': ['spatial_pos_max'], # used internally in the configuration class file 'T5Config': ['feed_forward_proj'], # used internally in the configuration class file # `tokenizer_class` get default value `T5Tokenizer` intentionally 'MT5Config': ['feed_forward_proj', 'tokenizer_class'], 'UMT5Config': ['feed_forward_proj', 'tokenizer_class'], # used internally in the configuration class file 'LongT5Config': ['feed_forward_proj'], # used internally in the configuration class file 'SwitchTransformersConfig': ['feed_forward_proj'], # having default values other than `1e-5` - we can't fix them without breaking 'BioGptConfig': ['layer_norm_eps'], # having default values other than `1e-5` - we can't fix them without breaking 'GLPNConfig': ['layer_norm_eps'], # having default values other than `1e-5` - we can't fix them without breaking 'SegformerConfig': ['layer_norm_eps'], # having default values other than `1e-5` - we can't fix them without breaking 'CvtConfig': ['layer_norm_eps'], # having default values other than `1e-5` - we can't fix them without breaking 'PerceiverConfig': ['layer_norm_eps'], # used internally to calculate the feature size 'InformerConfig': ['num_static_real_features', 'num_time_features'], # used internally to calculate the feature size 'TimeSeriesTransformerConfig': ['num_static_real_features', 'num_time_features'], # used internally to calculate the feature size 'AutoformerConfig': ['num_static_real_features', 'num_time_features'], # used internally to calculate `mlp_dim` 'SamVisionConfig': ['mlp_ratio'], # For (head) training, but so far not implemented 'ClapAudioConfig': ['num_classes'], # Not used, but providing useful information to users 'SpeechT5HifiGanConfig': ['sampling_rate'], } # TODO (ydshieh): Check the failing cases, try to fix them or move some cases to the above block once we are sure SPECIAL_CASES_TO_ALLOW.update( { 'CLIPSegConfig': True, 'DeformableDetrConfig': True, 'DetaConfig': True, 'DinatConfig': True, 'DonutSwinConfig': True, 'EfficientFormerConfig': True, 'FSMTConfig': True, 'JukeboxConfig': True, 'LayoutLMv2Config': True, 'MaskFormerSwinConfig': True, 'MT5Config': True, 'NatConfig': True, 'OneFormerConfig': True, 'PerceiverConfig': True, 'RagConfig': True, 'SpeechT5Config': True, 'SwinConfig': True, 'Swin2SRConfig': True, 'Swinv2Config': True, 'SwitchTransformersConfig': True, 'TableTransformerConfig': True, 'TapasConfig': True, 'TransfoXLConfig': True, 'UniSpeechConfig': True, 'UniSpeechSatConfig': True, 'WavLMConfig': True, 'WhisperConfig': True, # TODO: @Arthur (for `alignment_head` and `alignment_layer`) 'JukeboxPriorConfig': True, # TODO: @Younes (for `is_decoder`) 'Pix2StructTextConfig': True, } ) def lowerCAmelCase_ ( __UpperCAmelCase: Optional[Any] , __UpperCAmelCase: int , __UpperCAmelCase: int , __UpperCAmelCase: Tuple ) -> int: UpperCamelCase__ : List[str] = False for attribute in attributes: for modeling_source in source_strings: # check if we can find `config.xxx`, `getattr(config, "xxx", ...)` or `getattr(self.config, "xxx", ...)` if ( f"config.{attribute}" in modeling_source or f"getattr(config, \"{attribute}\"" in modeling_source or f"getattr(self.config, \"{attribute}\"" in modeling_source ): UpperCamelCase__ : str = True # Deal with multi-line cases elif ( re.search( rf"getattr[ \t\v\n\r\f]*\([ \t\v\n\r\f]*(self\.)?config,[ \t\v\n\r\f]*\"{attribute}\"" , SCREAMING_SNAKE_CASE__ , ) is not None ): UpperCamelCase__ : Any = True # `SequenceSummary` is called with `SequenceSummary(config)` elif attribute in [ "summary_type", "summary_use_proj", "summary_activation", "summary_last_dropout", "summary_proj_to_labels", "summary_first_dropout", ]: if "SequenceSummary" in modeling_source: UpperCamelCase__ : Union[str, Any] = True if attribute_used: break if attribute_used: break # common and important attributes, even if they do not always appear in the modeling files UpperCamelCase__ : Optional[int] = [ '''bos_index''', '''eos_index''', '''pad_index''', '''unk_index''', '''mask_index''', '''image_size''', '''use_cache''', '''out_features''', '''out_indices''', ] UpperCamelCase__ : Optional[Any] = ['''encoder_no_repeat_ngram_size'''] # Special cases to be allowed UpperCamelCase__ : str = True if not attribute_used: UpperCamelCase__ : str = False for attribute in attributes: # Allow if the default value in the configuration class is different from the one in `PretrainedConfig` if attribute in ["is_encoder_decoder"] and default_value is True: UpperCamelCase__ : Dict = True elif attribute in ["tie_word_embeddings"] and default_value is False: UpperCamelCase__ : Union[str, Any] = True # Allow cases without checking the default value in the configuration class elif attribute in attributes_to_allow + attributes_used_in_generation: UpperCamelCase__ : str = True elif attribute.endswith('''_token_id''' ): UpperCamelCase__ : str = True # configuration class specific cases if not case_allowed: UpperCamelCase__ : int = SPECIAL_CASES_TO_ALLOW.get(config_class.__name__ , [] ) UpperCamelCase__ : Optional[int] = allowed_cases is True or attribute in allowed_cases return attribute_used or case_allowed def lowerCAmelCase_ ( __UpperCAmelCase: Union[str, Any] ) -> Any: UpperCamelCase__ : List[Any] = dict(inspect.signature(config_class.__init__ ).parameters ) UpperCamelCase__ : Tuple = [x for x in list(signature.keys() ) if x not in ['''self''', '''kwargs''']] UpperCamelCase__ : Optional[Any] = [signature[param].default for param in parameter_names] # If `attribute_map` exists, an attribute can have different names to be used in the modeling files, and as long # as one variant is used, the test should pass UpperCamelCase__ : Any = {} if len(config_class.attribute_map ) > 0: UpperCamelCase__ : Tuple = {v: k for k, v in config_class.attribute_map.items()} # Get the path to modeling source files UpperCamelCase__ : Union[str, Any] = inspect.getsourcefile(SCREAMING_SNAKE_CASE__ ) UpperCamelCase__ : str = os.path.dirname(SCREAMING_SNAKE_CASE__ ) # Let's check against all frameworks: as long as one framework uses an attribute, we are good. UpperCamelCase__ : Any = [os.path.join(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) for fn in os.listdir(SCREAMING_SNAKE_CASE__ ) if fn.startswith('''modeling_''' )] # Get the source code strings UpperCamelCase__ : Optional[Any] = [] for path in modeling_paths: if os.path.isfile(SCREAMING_SNAKE_CASE__ ): with open(SCREAMING_SNAKE_CASE__ ) as fp: modeling_sources.append(fp.read() ) UpperCamelCase__ : Tuple = [] for config_param, default_value in zip(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): # `attributes` here is all the variant names for `config_param` UpperCamelCase__ : int = [config_param] # some configuration classes have non-empty `attribute_map`, and both names could be used in the # corresponding modeling files. As long as one of them appears, it is fine. if config_param in reversed_attribute_map: attributes.append(reversed_attribute_map[config_param] ) if not check_attribute_being_used(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): unused_attributes.append(attributes[0] ) return sorted(SCREAMING_SNAKE_CASE__ ) def lowerCAmelCase_ ( ) -> Dict: UpperCamelCase__ : List[Any] = {} for _config_class in list(CONFIG_MAPPING.values() ): # Skip deprecated models if "models.deprecated" in _config_class.__module__: continue # Some config classes are not in `CONFIG_MAPPING` (e.g. `CLIPVisionConfig`, `Blip2VisionConfig`, etc.) UpperCamelCase__ : Any = [ cls for name, cls in inspect.getmembers( inspect.getmodule(_config_class ) , lambda __UpperCAmelCase : inspect.isclass(SCREAMING_SNAKE_CASE__ ) and issubclass(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and inspect.getmodule(SCREAMING_SNAKE_CASE__ ) == inspect.getmodule(_config_class ) , ) ] for config_class in config_classes_in_module: UpperCamelCase__ : Any = check_config_attributes_being_used(SCREAMING_SNAKE_CASE__ ) if len(SCREAMING_SNAKE_CASE__ ) > 0: UpperCamelCase__ : int = unused_attributes if len(SCREAMING_SNAKE_CASE__ ) > 0: UpperCamelCase__ : List[str] = '''The following configuration classes contain unused attributes in the corresponding modeling files:\n''' for name, attributes in configs_with_unused_attributes.items(): error += f"{name}: {attributes}\n" raise ValueError(SCREAMING_SNAKE_CASE__ ) if __name__ == "__main__": check_config_attributes()
201
import math import os from copy import deepcopy import datasets import evaluate import torch import transformers from datasets import load_dataset from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer from accelerate import Accelerator from accelerate.test_utils import RegressionDataset, RegressionModel from accelerate.utils import is_tpu_available, set_seed lowercase_ = "true" def _snake_case( SCREAMING_SNAKE_CASE__ : Optional[int] , SCREAMING_SNAKE_CASE__ : List[Any]=82 , SCREAMING_SNAKE_CASE__ : Optional[int]=16 ) -> Optional[Any]: '''simple docstring''' set_seed(42 ) A__ = RegressionModel() A__ = deepcopy(SCREAMING_SNAKE_CASE__ ) A__ = RegressionDataset(length=SCREAMING_SNAKE_CASE__ ) A__ = DataLoader(SCREAMING_SNAKE_CASE__ , batch_size=SCREAMING_SNAKE_CASE__ ) model.to(accelerator.device ) A__ , A__ = accelerator.prepare(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) return model, ddp_model, dataloader def _snake_case( SCREAMING_SNAKE_CASE__ : Accelerator , SCREAMING_SNAKE_CASE__ : Tuple=False ) -> int: '''simple docstring''' A__ = AutoTokenizer.from_pretrained('hf-internal-testing/mrpc-bert-base-cased' ) A__ = load_dataset('glue' , 'mrpc' , split='validation' ) def tokenize_function(SCREAMING_SNAKE_CASE__ : List[Any] ): A__ = tokenizer(examples['sentence1'] , examples['sentence2'] , truncation=SCREAMING_SNAKE_CASE__ , max_length=SCREAMING_SNAKE_CASE__ ) return outputs with accelerator.main_process_first(): A__ = dataset.map( SCREAMING_SNAKE_CASE__ , batched=SCREAMING_SNAKE_CASE__ , remove_columns=['idx', 'sentence1', 'sentence2'] , ) A__ = tokenized_datasets.rename_column('label' , 'labels' ) def collate_fn(SCREAMING_SNAKE_CASE__ : Dict ): if use_longest: return tokenizer.pad(SCREAMING_SNAKE_CASE__ , padding='longest' , return_tensors='pt' ) return tokenizer.pad(SCREAMING_SNAKE_CASE__ , padding='max_length' , max_length=128 , return_tensors='pt' ) return DataLoader(SCREAMING_SNAKE_CASE__ , shuffle=SCREAMING_SNAKE_CASE__ , collate_fn=SCREAMING_SNAKE_CASE__ , batch_size=16 ) def _snake_case( SCREAMING_SNAKE_CASE__ : Tuple , SCREAMING_SNAKE_CASE__ : Any ) -> str: '''simple docstring''' A__ = Accelerator(dispatch_batches=SCREAMING_SNAKE_CASE__ , split_batches=SCREAMING_SNAKE_CASE__ ) A__ = get_dataloader(SCREAMING_SNAKE_CASE__ , not dispatch_batches ) A__ = AutoModelForSequenceClassification.from_pretrained( 'hf-internal-testing/mrpc-bert-base-cased' , return_dict=SCREAMING_SNAKE_CASE__ ) A__ , A__ = accelerator.prepare(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) return {"ddp": [ddp_model, ddp_dataloader, "cuda:0"], "no": [model, dataloader, accelerator.device]}, accelerator def _snake_case( SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : Dict , SCREAMING_SNAKE_CASE__ : Optional[int] ) -> List[str]: '''simple docstring''' A__ = [] for batch in dataloader: A__ , A__ = batch.values() with torch.no_grad(): A__ = model(SCREAMING_SNAKE_CASE__ ) A__ , A__ = accelerator.gather_for_metrics((logit, target) ) logits_and_targets.append((logit, target) ) A__ , A__ = [], [] for logit, targ in logits_and_targets: logits.append(SCREAMING_SNAKE_CASE__ ) targs.append(SCREAMING_SNAKE_CASE__ ) A__ , A__ = torch.cat(SCREAMING_SNAKE_CASE__ ), torch.cat(SCREAMING_SNAKE_CASE__ ) return logits, targs def _snake_case( SCREAMING_SNAKE_CASE__ : Accelerator , SCREAMING_SNAKE_CASE__ : int=82 , SCREAMING_SNAKE_CASE__ : Optional[Any]=False , SCREAMING_SNAKE_CASE__ : Any=False , SCREAMING_SNAKE_CASE__ : Tuple=16 ) -> List[Any]: '''simple docstring''' A__ , A__ , A__ = get_basic_setup(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) A__ , A__ = generate_predictions(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) assert ( len(SCREAMING_SNAKE_CASE__ ) == num_samples ), f'Unexpected number of inputs:\n Expected: {num_samples}\n Actual: {len(SCREAMING_SNAKE_CASE__ )}' def _snake_case( SCREAMING_SNAKE_CASE__ : bool = False , SCREAMING_SNAKE_CASE__ : bool = False ) -> str: '''simple docstring''' A__ = evaluate.load('glue' , 'mrpc' ) A__ , A__ = get_mrpc_setup(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) # First do baseline A__ , A__ , A__ = setup['no'] model.to(SCREAMING_SNAKE_CASE__ ) model.eval() for batch in dataloader: batch.to(SCREAMING_SNAKE_CASE__ ) with torch.inference_mode(): A__ = model(**SCREAMING_SNAKE_CASE__ ) A__ = outputs.logits.argmax(dim=-1 ) metric.add_batch(predictions=SCREAMING_SNAKE_CASE__ , references=batch['labels'] ) A__ = metric.compute() # Then do distributed A__ , A__ , A__ = setup['ddp'] model.eval() for batch in dataloader: with torch.inference_mode(): A__ = model(**SCREAMING_SNAKE_CASE__ ) A__ = outputs.logits.argmax(dim=-1 ) A__ = batch['labels'] A__ , A__ = accelerator.gather_for_metrics((preds, references) ) metric.add_batch(predictions=SCREAMING_SNAKE_CASE__ , references=SCREAMING_SNAKE_CASE__ ) A__ = metric.compute() for key in "accuracy f1".split(): assert math.isclose( baseline[key] , distributed[key] ), f'Baseline and Distributed are not the same for key {key}:\n\tBaseline: {baseline[key]}\n\tDistributed: {distributed[key]}\n' def _snake_case( ) -> Optional[Any]: '''simple docstring''' A__ = Accelerator(split_batches=SCREAMING_SNAKE_CASE__ , dispatch_batches=SCREAMING_SNAKE_CASE__ ) if accelerator.is_local_main_process: datasets.utils.logging.set_verbosity_warning() transformers.utils.logging.set_verbosity_warning() else: datasets.utils.logging.set_verbosity_error() transformers.utils.logging.set_verbosity_error() # These are a bit slower so they should only be ran on the GPU or TPU if torch.cuda.is_available() or is_tpu_available(): if accelerator.is_local_main_process: print('**Testing gather_for_metrics**' ) for split_batches in [True, False]: for dispatch_batches in [True, False]: if accelerator.is_local_main_process: print(f'With: `split_batches={split_batches}`, `dispatch_batches={dispatch_batches}`' ) test_mrpc(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) accelerator.state._reset_state() if accelerator.is_local_main_process: print('**Test torch metrics**' ) for split_batches in [True, False]: for dispatch_batches in [True, False]: A__ = Accelerator(split_batches=SCREAMING_SNAKE_CASE__ , dispatch_batches=SCREAMING_SNAKE_CASE__ ) if accelerator.is_local_main_process: print(f'With: `split_batches={split_batches}`, `dispatch_batches={dispatch_batches}`, length=99' ) test_torch_metrics(SCREAMING_SNAKE_CASE__ , 99 ) accelerator.state._reset_state() if accelerator.is_local_main_process: print('**Test last batch is not dropped when perfectly divisible**' ) A__ = Accelerator() test_torch_metrics(SCREAMING_SNAKE_CASE__ , 512 ) accelerator.state._reset_state() def _snake_case( SCREAMING_SNAKE_CASE__ : List[Any] ) -> Union[str, Any]: '''simple docstring''' main() if __name__ == "__main__": main()
7
0
"""simple docstring""" import logging import math from functools import partial from typing import Any, Callable, Dict, Iterable, List, Optional, Sequence, Tuple, Union import torch from .tensor_utils import tensor_tree_map, tree_map def _a ( _SCREAMING_SNAKE_CASE ) -> List[Tuple[int, ...]]: snake_case_ = [] if isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): for v in tree.values(): shapes.extend(_fetch_dims(_SCREAMING_SNAKE_CASE ) ) elif isinstance(_SCREAMING_SNAKE_CASE , (list, tuple) ): for t in tree: shapes.extend(_fetch_dims(_SCREAMING_SNAKE_CASE ) ) elif isinstance(_SCREAMING_SNAKE_CASE , torch.Tensor ): shapes.append(tree.shape ) else: raise ValueError("""Not supported""" ) return shapes @torch.jit.ignore def _a ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Tuple[int, ...]: snake_case_ = [] for d in reversed(_SCREAMING_SNAKE_CASE ): idx.append(flat_idx % d ) snake_case_ = flat_idx // d return tuple(reversed(_SCREAMING_SNAKE_CASE ) ) @torch.jit.ignore def _a ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = None , ) -> List[Tuple[slice, ...]]: # start_edges and end_edges both indicate whether, starting from any given # dimension, the start/end index is at the top/bottom edge of the # corresponding tensor, modeled as a tree def reduce_edge_list(_SCREAMING_SNAKE_CASE ) -> None: snake_case_ = True for i in range(len(_SCREAMING_SNAKE_CASE ) ): snake_case_ = -1 * (i + 1) l[reversed_idx] &= tally snake_case_ = l[reversed_idx] if start_edges is None: snake_case_ = [s == 0 for s in start] reduce_edge_list(_SCREAMING_SNAKE_CASE ) if end_edges is None: snake_case_ = [e == (d - 1) for e, d in zip(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE )] reduce_edge_list(_SCREAMING_SNAKE_CASE ) # Base cases. Either start/end are empty and we're done, or the final, # one-dimensional tensor can be simply sliced if len(_SCREAMING_SNAKE_CASE ) == 0: return [()] elif len(_SCREAMING_SNAKE_CASE ) == 1: return [(slice(start[0] , end[0] + 1 ),)] snake_case_ = [] snake_case_ = [] # Dimensions common to start and end can be selected directly for s, e in zip(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): if s == e: path_list.append(slice(_SCREAMING_SNAKE_CASE , s + 1 ) ) else: break snake_case_ = tuple(_SCREAMING_SNAKE_CASE ) snake_case_ = len(_SCREAMING_SNAKE_CASE ) # start == end, and we're done if divergence_idx == len(_SCREAMING_SNAKE_CASE ): return [path] def upper() -> Tuple[Tuple[slice, ...], ...]: assert start_edges is not None assert end_edges is not None snake_case_ = start[divergence_idx] return tuple( path + (slice(_SCREAMING_SNAKE_CASE , sdi + 1 ),) + s for s in _get_minimal_slice_set( start[divergence_idx + 1 :] , [d - 1 for d in dims[divergence_idx + 1 :]] , dims[divergence_idx + 1 :] , start_edges=start_edges[divergence_idx + 1 :] , end_edges=[True for _ in end_edges[divergence_idx + 1 :]] , ) ) def lower() -> Tuple[Tuple[slice, ...], ...]: assert start_edges is not None assert end_edges is not None snake_case_ = end[divergence_idx] return tuple( path + (slice(_SCREAMING_SNAKE_CASE , edi + 1 ),) + s for s in _get_minimal_slice_set( [0 for _ in start[divergence_idx + 1 :]] , end[divergence_idx + 1 :] , dims[divergence_idx + 1 :] , start_edges=[True for _ in start_edges[divergence_idx + 1 :]] , end_edges=end_edges[divergence_idx + 1 :] , ) ) # If both start and end are at the edges of the subtree rooted at # divergence_idx, we can just select the whole subtree at once if start_edges[divergence_idx] and end_edges[divergence_idx]: slices.append(path + (slice(start[divergence_idx] , end[divergence_idx] + 1 ),) ) # If just start is at the edge, we can grab almost all of the subtree, # treating only the ragged bottom edge as an edge case elif start_edges[divergence_idx]: slices.append(path + (slice(start[divergence_idx] , end[divergence_idx] ),) ) slices.extend(lower() ) # Analogous to the previous case, but the top is ragged this time elif end_edges[divergence_idx]: slices.extend(upper() ) slices.append(path + (slice(start[divergence_idx] + 1 , end[divergence_idx] + 1 ),) ) # If both sides of the range are ragged, we need to handle both sides # separately. If there's contiguous meat in between them, we can index it # in one big chunk else: slices.extend(upper() ) snake_case_ = end[divergence_idx] - start[divergence_idx] if middle_ground > 1: slices.append(path + (slice(start[divergence_idx] + 1 , end[divergence_idx] ),) ) slices.extend(lower() ) return slices @torch.jit.ignore def _a ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> torch.Tensor: snake_case_ = t.shape[:no_batch_dims] snake_case_ = list(_flat_idx_to_idx(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) # _get_minimal_slice_set is inclusive snake_case_ = list(_flat_idx_to_idx(flat_end - 1 , _SCREAMING_SNAKE_CASE ) ) # Get an ordered list of slices to perform snake_case_ = _get_minimal_slice_set( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , ) snake_case_ = [t[s] for s in slices] return torch.cat([s.view((-1,) + t.shape[no_batch_dims:] ) for s in sliced_tensors] ) def _a ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = False , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = False , ) -> Any: if not (len(_SCREAMING_SNAKE_CASE ) > 0): raise ValueError("""Must provide at least one input""" ) snake_case_ = [shape[:no_batch_dims] for shape in _fetch_dims(_SCREAMING_SNAKE_CASE )] snake_case_ = tuple([max(_SCREAMING_SNAKE_CASE ) for s in zip(*_SCREAMING_SNAKE_CASE )] ) def _prep_inputs(_SCREAMING_SNAKE_CASE ) -> torch.Tensor: if not low_mem: if not sum(t.shape[:no_batch_dims] ) == no_batch_dims: snake_case_ = t.expand(orig_batch_dims + t.shape[no_batch_dims:] ) snake_case_ = t.reshape(-1 , *t.shape[no_batch_dims:] ) else: snake_case_ = t.expand(orig_batch_dims + t.shape[no_batch_dims:] ) return t snake_case_ = tensor_tree_map(_prep_inputs , _SCREAMING_SNAKE_CASE ) snake_case_ = None if _out is not None: snake_case_ = tensor_tree_map(lambda _SCREAMING_SNAKE_CASE : t.view([-1] + list(t.shape[no_batch_dims:] ) ) , _out ) snake_case_ = 1 for d in orig_batch_dims: flat_batch_dim *= d snake_case_ = flat_batch_dim // chunk_size + (flat_batch_dim % chunk_size != 0) def _select_chunk(_SCREAMING_SNAKE_CASE ) -> torch.Tensor: return t[i : i + chunk_size] if t.shape[0] != 1 else t snake_case_ = 0 snake_case_ = prepped_outputs for _ in range(_SCREAMING_SNAKE_CASE ): # Chunk the input if not low_mem: snake_case_ = _select_chunk else: snake_case_ = partial( _chunk_slice , flat_start=_SCREAMING_SNAKE_CASE , flat_end=min(_SCREAMING_SNAKE_CASE , i + chunk_size ) , no_batch_dims=len(_SCREAMING_SNAKE_CASE ) , ) snake_case_ = tensor_tree_map(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # Run the layer on the chunk snake_case_ = layer(**_SCREAMING_SNAKE_CASE ) # Allocate space for the output if out is None: snake_case_ = tensor_tree_map(lambda _SCREAMING_SNAKE_CASE : t.new_zeros((flat_batch_dim,) + t.shape[1:] ) , _SCREAMING_SNAKE_CASE ) # Put the chunk in its pre-allocated space if isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): def assign(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> None: for k, v in da.items(): if isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): assign(_SCREAMING_SNAKE_CASE , da[k] ) else: if _add_into_out: v[i : i + chunk_size] += da[k] else: snake_case_ = da[k] assign(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) elif isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): for xa, xa in zip(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): if _add_into_out: xa[i : i + chunk_size] += xa else: snake_case_ = xa elif isinstance(_SCREAMING_SNAKE_CASE , torch.Tensor ): if _add_into_out: out[i : i + chunk_size] += output_chunk else: snake_case_ = output_chunk else: raise ValueError("""Not supported""" ) i += chunk_size snake_case_ = tensor_tree_map(lambda _SCREAMING_SNAKE_CASE : t.view(orig_batch_dims + t.shape[1:] ) , _SCREAMING_SNAKE_CASE ) return out class __A : '''simple docstring''' def __init__( self : Optional[int] , UpperCAmelCase_ : int = 512 , ) ->List[Any]: """simple docstring""" snake_case_ = max_chunk_size snake_case_ = None snake_case_ = None def lowerCAmelCase ( self : Optional[int] , UpperCAmelCase_ : Callable , UpperCAmelCase_ : tuple , UpperCAmelCase_ : int ) ->int: """simple docstring""" logging.info("""Tuning chunk size...""" ) if min_chunk_size >= self.max_chunk_size: return min_chunk_size snake_case_ = [2**l for l in range(int(math.log(self.max_chunk_size , 2 ) ) + 1 )] snake_case_ = [c for c in candidates if c > min_chunk_size] snake_case_ = [min_chunk_size] + candidates candidates[-1] += 4 def test_chunk_size(UpperCAmelCase_ : int ) -> bool: try: with torch.no_grad(): fn(*UpperCAmelCase_ , chunk_size=UpperCAmelCase_ ) return True except RuntimeError: return False snake_case_ = 0 snake_case_ = len(UpperCAmelCase_ ) - 1 while i > min_viable_chunk_size_index: snake_case_ = test_chunk_size(candidates[i] ) if not viable: snake_case_ = (min_viable_chunk_size_index + i) // 2 else: snake_case_ = i snake_case_ = (i + len(UpperCAmelCase_ ) - 1) // 2 return candidates[min_viable_chunk_size_index] def lowerCAmelCase ( self : Union[str, Any] , UpperCAmelCase_ : Iterable , UpperCAmelCase_ : Iterable ) ->bool: """simple docstring""" snake_case_ = True for aa, aa in zip(UpperCAmelCase_ , UpperCAmelCase_ ): assert type(UpperCAmelCase_ ) == type(UpperCAmelCase_ ) if isinstance(UpperCAmelCase_ , (list, tuple) ): consistent &= self._compare_arg_caches(UpperCAmelCase_ , UpperCAmelCase_ ) elif isinstance(UpperCAmelCase_ , UpperCAmelCase_ ): snake_case_ = [v for _, v in sorted(aa.items() , key=lambda UpperCAmelCase_ : x[0] )] snake_case_ = [v for _, v in sorted(aa.items() , key=lambda UpperCAmelCase_ : x[0] )] consistent &= self._compare_arg_caches(UpperCAmelCase_ , UpperCAmelCase_ ) else: consistent &= aa == aa return consistent def lowerCAmelCase ( self : List[Any] , UpperCAmelCase_ : Callable , UpperCAmelCase_ : tuple , UpperCAmelCase_ : int , ) ->int: """simple docstring""" snake_case_ = True snake_case_ = tree_map(lambda UpperCAmelCase_ : a.shape if isinstance(UpperCAmelCase_ , torch.Tensor ) else a , UpperCAmelCase_ , UpperCAmelCase_ ) if self.cached_arg_data is not None: # If args have changed shape/value, we need to re-tune assert len(self.cached_arg_data ) == len(UpperCAmelCase_ ) snake_case_ = self._compare_arg_caches(self.cached_arg_data , UpperCAmelCase_ ) else: # Otherwise, we can reuse the precomputed value snake_case_ = False if not consistent: snake_case_ = self._determine_favorable_chunk_size( UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , ) snake_case_ = arg_data assert self.cached_chunk_size is not None return self.cached_chunk_size
368
"""simple docstring""" import ast import os import re import shutil import tempfile import unittest from unittest import mock import torch from accelerate.test_utils.examples import compare_against_test from accelerate.test_utils.testing import TempDirTestCase, require_trackers, run_command, slow from accelerate.utils import write_basic_config # DataLoaders built from `test_samples/MRPC` for quick testing # Should mock `{script_name}.get_dataloaders` via: # @mock.patch("{script_name}.get_dataloaders", mocked_dataloaders) __SCREAMING_SNAKE_CASE : Tuple = [ 'cross_validation.py', 'gradient_accumulation.py', 'local_sgd.py', 'multi_process_metrics.py', 'memory.py', 'automatic_gradient_accumulation.py', 'fsdp_with_peak_mem_tracking.py', 'deepspeed_with_config_support.py', 'megatron_lm_gpt_pretraining.py', ] class __A (unittest.TestCase): '''simple docstring''' def lowerCAmelCase ( self : str , UpperCAmelCase_ : str , UpperCAmelCase_ : bool , UpperCAmelCase_ : str = None , UpperCAmelCase_ : list = None ) ->List[Any]: """simple docstring""" snake_case_ = None snake_case_ = os.path.abspath(os.path.join("""examples""" , """by_feature""" ) ) snake_case_ = os.path.abspath("""examples""" ) for item in os.listdir(UpperCAmelCase_ ): if item not in EXCLUDE_EXAMPLES: snake_case_ = os.path.join(UpperCAmelCase_ , UpperCAmelCase_ ) if os.path.isfile(UpperCAmelCase_ ) and ".py" in item_path: with self.subTest( tested_script=UpperCAmelCase_ , feature_script=UpperCAmelCase_ , tested_section="""main()""" if parser_only else """training_function()""" , ): snake_case_ = compare_against_test( os.path.join(UpperCAmelCase_ , UpperCAmelCase_ ) , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ ) snake_case_ = """\n""".join(UpperCAmelCase_ ) if special_strings is not None: for string in special_strings: snake_case_ = diff.replace(UpperCAmelCase_ , """""" ) self.assertEqual(UpperCAmelCase_ , """""" ) def lowerCAmelCase ( self : Tuple ) ->List[Any]: """simple docstring""" self.one_complete_example("""complete_nlp_example.py""" , UpperCAmelCase_ ) self.one_complete_example("""complete_nlp_example.py""" , UpperCAmelCase_ ) def lowerCAmelCase ( self : Tuple ) ->Dict: """simple docstring""" snake_case_ = os.path.abspath(os.path.join("""examples""" , """cv_example.py""" ) ) snake_case_ = [ """ """ * 16 + """{\n\n""", """ """ * 20 + """\"accuracy\": eval_metric[\"accuracy\"],\n\n""", """ """ * 20 + """\"f1\": eval_metric[\"f1\"],\n\n""", """ """ * 20 + """\"train_loss\": total_loss.item() / len(train_dataloader),\n\n""", """ """ * 20 + """\"epoch\": epoch,\n\n""", """ """ * 16 + """},\n\n""", """ """ * 16 + """step=epoch,\n""", """ """ * 12, """ """ * 8 + """for step, batch in enumerate(active_dataloader):\n""", ] self.one_complete_example("""complete_cv_example.py""" , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ ) self.one_complete_example("""complete_cv_example.py""" , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ ) @mock.patch.dict(os.environ , {"""TESTING_MOCKED_DATALOADERS""": """1"""}) class __A (snake_case__): '''simple docstring''' __lowercase: str = False @classmethod def lowerCAmelCase ( cls : Any ) ->List[str]: """simple docstring""" super().setUpClass() snake_case_ = tempfile.mkdtemp() snake_case_ = os.path.join(cls._tmpdir , """default_config.yml""" ) write_basic_config(save_location=cls.configPath ) snake_case_ = ["""accelerate""", """launch""", """--config_file""", cls.configPath] @classmethod def lowerCAmelCase ( cls : List[str] ) ->int: """simple docstring""" super().tearDownClass() shutil.rmtree(cls._tmpdir ) def lowerCAmelCase ( self : List[str] ) ->Optional[Any]: """simple docstring""" snake_case_ = F""" examples/by_feature/checkpointing.py --checkpointing_steps epoch --output_dir {self.tmpdir} """.split() run_command(self._launch_args + testargs ) self.assertTrue(os.path.exists(os.path.join(self.tmpdir , """epoch_0""" ) ) ) def lowerCAmelCase ( self : Any ) ->Optional[Any]: """simple docstring""" snake_case_ = F""" examples/by_feature/checkpointing.py --checkpointing_steps 1 --output_dir {self.tmpdir} """.split() snake_case_ = run_command(self._launch_args + testargs ) self.assertTrue(os.path.exists(os.path.join(self.tmpdir , """step_2""" ) ) ) def lowerCAmelCase ( self : Union[str, Any] ) ->str: """simple docstring""" snake_case_ = F""" examples/by_feature/checkpointing.py --resume_from_checkpoint {os.path.join(self.tmpdir , "epoch_0" )} """.split() snake_case_ = run_command(self._launch_args + testargs , return_stdout=UpperCAmelCase_ ) self.assertNotIn("""epoch 0:""" , UpperCAmelCase_ ) self.assertIn("""epoch 1:""" , UpperCAmelCase_ ) def lowerCAmelCase ( self : Optional[int] ) ->Tuple: """simple docstring""" snake_case_ = F""" examples/by_feature/checkpointing.py --resume_from_checkpoint {os.path.join(self.tmpdir , "step_2" )} """.split() snake_case_ = run_command(self._launch_args + testargs , return_stdout=UpperCAmelCase_ ) if torch.cuda.is_available(): snake_case_ = torch.cuda.device_count() else: snake_case_ = 1 if num_processes > 1: self.assertNotIn("""epoch 0:""" , UpperCAmelCase_ ) self.assertIn("""epoch 1:""" , UpperCAmelCase_ ) else: self.assertIn("""epoch 0:""" , UpperCAmelCase_ ) self.assertIn("""epoch 1:""" , UpperCAmelCase_ ) @slow def lowerCAmelCase ( self : Dict ) ->Dict: """simple docstring""" snake_case_ = """ examples/by_feature/cross_validation.py --num_folds 2 """.split() with mock.patch.dict(os.environ , {"""TESTING_MOCKED_DATALOADERS""": """0"""} ): snake_case_ = run_command(self._launch_args + testargs , return_stdout=UpperCAmelCase_ ) snake_case_ = re.findall("""({.+})""" , UpperCAmelCase_ ) snake_case_ = [r for r in results if """accuracy""" in r][-1] snake_case_ = ast.literal_eval(UpperCAmelCase_ ) self.assertGreaterEqual(results["""accuracy"""] , 0.75 ) def lowerCAmelCase ( self : Optional[int] ) ->int: """simple docstring""" snake_case_ = ["""examples/by_feature/multi_process_metrics.py"""] run_command(self._launch_args + testargs ) @require_trackers @mock.patch.dict(os.environ , {"""WANDB_MODE""": """offline"""} ) def lowerCAmelCase ( self : Dict ) ->Optional[Any]: """simple docstring""" with tempfile.TemporaryDirectory() as tmpdir: snake_case_ = F""" examples/by_feature/tracking.py --with_tracking --project_dir {tmpdir} """.split() run_command(self._launch_args + testargs ) self.assertTrue(os.path.exists(os.path.join(UpperCAmelCase_ , """tracking""" ) ) ) def lowerCAmelCase ( self : List[str] ) ->Union[str, Any]: """simple docstring""" snake_case_ = ["""examples/by_feature/gradient_accumulation.py"""] run_command(self._launch_args + testargs ) def lowerCAmelCase ( self : str ) ->Dict: """simple docstring""" snake_case_ = ["""examples/by_feature/local_sgd.py"""] run_command(self._launch_args + testargs )
233
0
from __future__ import annotations def lowerCamelCase_ ( _UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) -> tuple[float, list[float]]: """simple docstring""" snake_case_ : Dict = list(range(len(_UpperCamelCase ) ) ) snake_case_ : Dict = [v / w for v, w in zip(_UpperCamelCase , _UpperCamelCase )] index.sort(key=lambda _UpperCamelCase : ratio[i] , reverse=_UpperCamelCase ) snake_case_ : float = 0 snake_case_ : list[float] = [0] * len(_UpperCamelCase ) for i in index: if weight[i] <= capacity: snake_case_ : Dict = 1 max_value += value[i] capacity -= weight[i] else: snake_case_ : Union[str, Any] = capacity / weight[i] max_value += value[i] * capacity / weight[i] break return max_value, fractions if __name__ == "__main__": import doctest doctest.testmod()
279
import argparse import hashlib import os import urllib import warnings import torch from torch import nn from tqdm import tqdm from transformers import WhisperConfig, WhisperForConditionalGeneration lowerCAmelCase_ = { '''tiny.en''': '''https://openaipublic.azureedge.net/main/whisper/models/d3dd57d32accea0b295c96e26691aa14d8822fac7d9d27d5dc00b4ca2826dd03/tiny.en.pt''', '''tiny''': '''https://openaipublic.azureedge.net/main/whisper/models/65147644a518d12f04e32d6f3b26facc3f8dd46e5390956a9424a650c0ce22b9/tiny.pt''', '''base.en''': '''https://openaipublic.azureedge.net/main/whisper/models/25a8566e1d0c1e2231d1c762132cd20e0f96a85d16145c3a00adf5d1ac670ead/base.en.pt''', '''base''': '''https://openaipublic.azureedge.net/main/whisper/models/ed3a0b6b1c0edf879ad9b11b1af5a0e6ab5db9205f891f668f8b0e6c6326e34e/base.pt''', '''small.en''': '''https://openaipublic.azureedge.net/main/whisper/models/f953ad0fd29cacd07d5a9eda5624af0f6bcf2258be67c92b79389873d91e0872/small.en.pt''', '''small''': '''https://openaipublic.azureedge.net/main/whisper/models/9ecf779972d90ba49c06d968637d720dd632c55bbf19d441fb42bf17a411e794/small.pt''', '''medium.en''': '''https://openaipublic.azureedge.net/main/whisper/models/d7440d1dc186f76616474e0ff0b3b6b879abc9d1a4926b7adfa41db2d497ab4f/medium.en.pt''', '''medium''': '''https://openaipublic.azureedge.net/main/whisper/models/345ae4da62f9b3d59415adc60127b97c714f32e89e936602e85993674d08dcb1/medium.pt''', '''large''': '''https://openaipublic.azureedge.net/main/whisper/models/e4b87e7e0bf463eb8e6956e646f1e277e901512310def2c24bf0e11bd3c28e9a/large.pt''', '''large-v2''': '''https://openaipublic.azureedge.net/main/whisper/models/81f7c96c852ee8fc832187b0132e569d6c3065a3252ed18e56effd0b6a73e524/large-v2.pt''', } def lowerCamelCase_ ( _UpperCamelCase ) -> List[Any]: """simple docstring""" snake_case_ : List[str] = ['''layers''', '''blocks'''] for k in ignore_keys: state_dict.pop(_UpperCamelCase , _UpperCamelCase ) lowerCAmelCase_ = { '''blocks''': '''layers''', '''mlp.0''': '''fc1''', '''mlp.2''': '''fc2''', '''mlp_ln''': '''final_layer_norm''', '''.attn.query''': '''.self_attn.q_proj''', '''.attn.key''': '''.self_attn.k_proj''', '''.attn.value''': '''.self_attn.v_proj''', '''.attn_ln''': '''.self_attn_layer_norm''', '''.attn.out''': '''.self_attn.out_proj''', '''.cross_attn.query''': '''.encoder_attn.q_proj''', '''.cross_attn.key''': '''.encoder_attn.k_proj''', '''.cross_attn.value''': '''.encoder_attn.v_proj''', '''.cross_attn_ln''': '''.encoder_attn_layer_norm''', '''.cross_attn.out''': '''.encoder_attn.out_proj''', '''decoder.ln.''': '''decoder.layer_norm.''', '''encoder.ln.''': '''encoder.layer_norm.''', '''token_embedding''': '''embed_tokens''', '''encoder.positional_embedding''': '''encoder.embed_positions.weight''', '''decoder.positional_embedding''': '''decoder.embed_positions.weight''', '''ln_post''': '''layer_norm''', } def lowerCamelCase_ ( _UpperCamelCase ) -> int: """simple docstring""" snake_case_ : str = list(s_dict.keys() ) for key in keys: snake_case_ : Optional[int] = key for k, v in WHISPER_MAPPING.items(): if k in key: snake_case_ : List[str] = new_key.replace(_UpperCamelCase , _UpperCamelCase ) print(f'''{key} -> {new_key}''' ) snake_case_ : Tuple = s_dict.pop(_UpperCamelCase ) return s_dict def lowerCamelCase_ ( _UpperCamelCase ) -> int: """simple docstring""" snake_case_ , snake_case_ : Dict = emb.weight.shape snake_case_ : Tuple = nn.Linear(_UpperCamelCase , _UpperCamelCase , bias=_UpperCamelCase ) snake_case_ : Any = emb.weight.data return lin_layer def lowerCamelCase_ ( _UpperCamelCase , _UpperCamelCase ) -> bytes: """simple docstring""" os.makedirs(_UpperCamelCase , exist_ok=_UpperCamelCase ) snake_case_ : List[Any] = os.path.basename(_UpperCamelCase ) snake_case_ : Any = url.split('''/''' )[-2] snake_case_ : str = os.path.join(_UpperCamelCase , _UpperCamelCase ) if os.path.exists(_UpperCamelCase ) and not os.path.isfile(_UpperCamelCase ): raise RuntimeError(f'''{download_target} exists and is not a regular file''' ) if os.path.isfile(_UpperCamelCase ): snake_case_ : Union[str, Any] = open(_UpperCamelCase , '''rb''' ).read() if hashlib.shaaaa(_UpperCamelCase ).hexdigest() == expected_shaaaa: return model_bytes else: warnings.warn(f'''{download_target} exists, but the SHA256 checksum does not match; re-downloading the file''' ) with urllib.request.urlopen(_UpperCamelCase ) as source, open(_UpperCamelCase , '''wb''' ) as output: with tqdm( total=int(source.info().get('''Content-Length''' ) ) , ncols=80 , unit='''iB''' , unit_scale=_UpperCamelCase , unit_divisor=1_024 ) as loop: while True: snake_case_ : Dict = source.read(8_192 ) if not buffer: break output.write(_UpperCamelCase ) loop.update(len(_UpperCamelCase ) ) snake_case_ : Any = open(_UpperCamelCase , '''rb''' ).read() if hashlib.shaaaa(_UpperCamelCase ).hexdigest() != expected_shaaaa: raise RuntimeError( '''Model has been downloaded but the SHA256 checksum does not not match. Please retry loading the model.''' ) return model_bytes def lowerCamelCase_ ( _UpperCamelCase , _UpperCamelCase ) -> int: """simple docstring""" if ".pt" not in checkpoint_path: snake_case_ : str = _download(_MODELS[checkpoint_path] ) else: snake_case_ : Union[str, Any] = torch.load(_UpperCamelCase , map_location='''cpu''' ) snake_case_ : int = original_checkpoint['''dims'''] snake_case_ : List[str] = original_checkpoint['''model_state_dict'''] snake_case_ : str = state_dict['''decoder.token_embedding.weight'''] remove_ignore_keys_(_UpperCamelCase ) rename_keys(_UpperCamelCase ) snake_case_ : Optional[int] = True snake_case_ : int = state_dict['''decoder.layers.0.fc1.weight'''].shape[0] snake_case_ : List[str] = WhisperConfig( vocab_size=dimensions['''n_vocab'''] , encoder_ffn_dim=_UpperCamelCase , decoder_ffn_dim=_UpperCamelCase , num_mel_bins=dimensions['''n_mels'''] , d_model=dimensions['''n_audio_state'''] , max_target_positions=dimensions['''n_text_ctx'''] , encoder_layers=dimensions['''n_audio_layer'''] , encoder_attention_heads=dimensions['''n_audio_head'''] , decoder_layers=dimensions['''n_text_layer'''] , decoder_attention_heads=dimensions['''n_text_state'''] , max_source_positions=dimensions['''n_audio_ctx'''] , ) snake_case_ : Union[str, Any] = WhisperForConditionalGeneration(_UpperCamelCase ) snake_case_ , snake_case_ : List[Any] = model.model.load_state_dict(_UpperCamelCase , strict=_UpperCamelCase ) if len(_UpperCamelCase ) > 0 and not set(_UpperCamelCase ) <= { "encoder.embed_positions.weights", "decoder.embed_positions.weights", }: raise ValueError( '''Only `encoder.embed_positions.weights` and `decoder.embed_positions.weights` are allowed to be missing,''' f''' but all the following weights are missing {missing}''' ) if tie_embeds: snake_case_ : List[str] = make_linear_from_emb(model.model.decoder.embed_tokens ) else: snake_case_ : Any = proj_out_weights model.save_pretrained(_UpperCamelCase ) if __name__ == "__main__": lowerCAmelCase_ = argparse.ArgumentParser() # # Required parameters parser.add_argument('''--checkpoint_path''', type=str, help='''Patht to the downloaded checkpoints''') parser.add_argument('''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''') lowerCAmelCase_ = parser.parse_args() convert_openai_whisper_to_tfms(args.checkpoint_path, args.pytorch_dump_folder_path)
279
1
from dataclasses import dataclass from typing import Optional, Tuple, Union import torch import torch.nn as nn from ..configuration_utils import ConfigMixin, register_to_config from ..utils import BaseOutput from .embeddings import GaussianFourierProjection, TimestepEmbedding, Timesteps from .modeling_utils import ModelMixin from .unet_ad_blocks import get_down_block, get_mid_block, get_out_block, get_up_block @dataclass class lowerCamelCase (_a ): """simple docstring""" UpperCAmelCase_ = 42 class lowerCamelCase (_a , _a ): """simple docstring""" @register_to_config def __init__( self : str, _UpperCAmelCase : int = 6_5_5_3_6, _UpperCAmelCase : Optional[int] = None, _UpperCAmelCase : int = 2, _UpperCAmelCase : int = 2, _UpperCAmelCase : int = 0, _UpperCAmelCase : str = "fourier", _UpperCAmelCase : bool = True, _UpperCAmelCase : bool = False, _UpperCAmelCase : float = 0.0, _UpperCAmelCase : Tuple[str] = ("DownBlock1DNoSkip", "DownBlock1D", "AttnDownBlock1D"), _UpperCAmelCase : Tuple[str] = ("AttnUpBlock1D", "UpBlock1D", "UpBlock1DNoSkip"), _UpperCAmelCase : Tuple[str] = "UNetMidBlock1D", _UpperCAmelCase : str = None, _UpperCAmelCase : Tuple[int] = (3_2, 3_2, 6_4), _UpperCAmelCase : str = None, _UpperCAmelCase : int = 8, _UpperCAmelCase : int = 1, _UpperCAmelCase : bool = False, ) -> List[Any]: """simple docstring""" super().__init__() SCREAMING_SNAKE_CASE__ : List[str] = sample_size # time if time_embedding_type == "fourier": SCREAMING_SNAKE_CASE__ : str = GaussianFourierProjection( embedding_size=8, set_W_to_weight=_a, log=_a, flip_sin_to_cos=_a ) SCREAMING_SNAKE_CASE__ : str = 2 * block_out_channels[0] elif time_embedding_type == "positional": SCREAMING_SNAKE_CASE__ : Optional[int] = Timesteps( block_out_channels[0], flip_sin_to_cos=_a, downscale_freq_shift=_a ) SCREAMING_SNAKE_CASE__ : str = block_out_channels[0] if use_timestep_embedding: SCREAMING_SNAKE_CASE__ : Optional[Any] = block_out_channels[0] * 4 SCREAMING_SNAKE_CASE__ : Any = TimestepEmbedding( in_channels=_a, time_embed_dim=_a, act_fn=_a, out_dim=block_out_channels[0], ) SCREAMING_SNAKE_CASE__ : Optional[int] = nn.ModuleList([] ) SCREAMING_SNAKE_CASE__ : Optional[Any] = None SCREAMING_SNAKE_CASE__ : List[Any] = nn.ModuleList([] ) SCREAMING_SNAKE_CASE__ : Union[str, Any] = None # down SCREAMING_SNAKE_CASE__ : List[Any] = in_channels for i, down_block_type in enumerate(_a ): SCREAMING_SNAKE_CASE__ : Optional[Any] = output_channel SCREAMING_SNAKE_CASE__ : Optional[int] = block_out_channels[i] if i == 0: input_channel += extra_in_channels SCREAMING_SNAKE_CASE__ : Optional[Any] = i == len(_a ) - 1 SCREAMING_SNAKE_CASE__ : Tuple = get_down_block( _a, num_layers=_a, in_channels=_a, out_channels=_a, temb_channels=block_out_channels[0], add_downsample=not is_final_block or downsample_each_block, ) self.down_blocks.append(_a ) # mid SCREAMING_SNAKE_CASE__ : Optional[Any] = get_mid_block( _a, in_channels=block_out_channels[-1], mid_channels=block_out_channels[-1], out_channels=block_out_channels[-1], embed_dim=block_out_channels[0], num_layers=_a, add_downsample=_a, ) # up SCREAMING_SNAKE_CASE__ : Union[str, Any] = list(reversed(_a ) ) SCREAMING_SNAKE_CASE__ : Optional[int] = reversed_block_out_channels[0] if out_block_type is None: SCREAMING_SNAKE_CASE__ : Union[str, Any] = out_channels else: SCREAMING_SNAKE_CASE__ : str = block_out_channels[0] for i, up_block_type in enumerate(_a ): SCREAMING_SNAKE_CASE__ : Optional[int] = output_channel SCREAMING_SNAKE_CASE__ : Optional[Any] = ( reversed_block_out_channels[i + 1] if i < len(_a ) - 1 else final_upsample_channels ) SCREAMING_SNAKE_CASE__ : Tuple = i == len(_a ) - 1 SCREAMING_SNAKE_CASE__ : Optional[Any] = get_up_block( _a, num_layers=_a, in_channels=_a, out_channels=_a, temb_channels=block_out_channels[0], add_upsample=not is_final_block, ) self.up_blocks.append(_a ) SCREAMING_SNAKE_CASE__ : Tuple = output_channel # out SCREAMING_SNAKE_CASE__ : int = norm_num_groups if norm_num_groups is not None else min(block_out_channels[0] // 4, 3_2 ) SCREAMING_SNAKE_CASE__ : Dict = get_out_block( out_block_type=_a, num_groups_out=_a, embed_dim=block_out_channels[0], out_channels=_a, act_fn=_a, fc_dim=block_out_channels[-1] // 4, ) def A_ ( self : Optional[Any], _UpperCAmelCase : torch.FloatTensor, _UpperCAmelCase : Union[torch.Tensor, float, int], _UpperCAmelCase : bool = True, ) -> List[Any]: """simple docstring""" SCREAMING_SNAKE_CASE__ : Dict = timestep if not torch.is_tensor(_a ): SCREAMING_SNAKE_CASE__ : List[Any] = torch.tensor([timesteps], dtype=torch.long, device=sample.device ) elif torch.is_tensor(_a ) and len(timesteps.shape ) == 0: SCREAMING_SNAKE_CASE__ : Union[str, Any] = timesteps[None].to(sample.device ) SCREAMING_SNAKE_CASE__ : Any = self.time_proj(_a ) if self.config.use_timestep_embedding: SCREAMING_SNAKE_CASE__ : Any = self.time_mlp(_a ) else: SCREAMING_SNAKE_CASE__ : int = timestep_embed[..., None] SCREAMING_SNAKE_CASE__ : Optional[int] = timestep_embed.repeat([1, 1, sample.shape[2]] ).to(sample.dtype ) SCREAMING_SNAKE_CASE__ : Tuple = timestep_embed.broadcast_to((sample.shape[:1] + timestep_embed.shape[1:]) ) # 2. down SCREAMING_SNAKE_CASE__ : Dict = () for downsample_block in self.down_blocks: SCREAMING_SNAKE_CASE__ : int = downsample_block(hidden_states=_a, temb=_a ) down_block_res_samples += res_samples # 3. mid if self.mid_block: SCREAMING_SNAKE_CASE__ : Tuple = self.mid_block(_a, _a ) # 4. up for i, upsample_block in enumerate(self.up_blocks ): SCREAMING_SNAKE_CASE__ : List[str] = down_block_res_samples[-1:] SCREAMING_SNAKE_CASE__ : Tuple = down_block_res_samples[:-1] SCREAMING_SNAKE_CASE__ : Optional[int] = upsample_block(_a, res_hidden_states_tuple=_a, temb=_a ) # 5. post-process if self.out_block: SCREAMING_SNAKE_CASE__ : Tuple = self.out_block(_a, _a ) if not return_dict: return (sample,) return UNetaDOutput(sample=_a )
370
def _a ( SCREAMING_SNAKE_CASE__ : int = 50_00_00_00 ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE__ : str = set() SCREAMING_SNAKE_CASE__ : Dict = int((limit - 24) ** (1 / 2) ) SCREAMING_SNAKE_CASE__ : Any = set(range(3 , prime_square_limit + 1 , 2 ) ) primes.add(2 ) for p in range(3 , prime_square_limit + 1 , 2 ): if p not in primes: continue primes.difference_update(set(range(p * p , prime_square_limit + 1 , SCREAMING_SNAKE_CASE__ ) ) ) for primea in primes: SCREAMING_SNAKE_CASE__ : Optional[int] = primea * primea for primea in primes: SCREAMING_SNAKE_CASE__ : Union[str, Any] = primea * primea * primea if square + cube >= limit - 16: break for primea in primes: SCREAMING_SNAKE_CASE__ : List[str] = primea * primea * primea * primea SCREAMING_SNAKE_CASE__ : Optional[int] = square + cube + tetr if total >= limit: break ret.add(SCREAMING_SNAKE_CASE__ ) return len(SCREAMING_SNAKE_CASE__ ) if __name__ == "__main__": print(f"{solution() = }")
191
0
'''simple docstring''' class UpperCamelCase__ : """simple docstring""" def __init__( self : List[Any] , lowerCamelCase_ : int , lowerCamelCase_ : int=None , lowerCamelCase_ : Tuple=None ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = data SCREAMING_SNAKE_CASE : int = previous SCREAMING_SNAKE_CASE : Optional[int] = next_node def __str__( self : Optional[Any] ): '''simple docstring''' return f'''{self.data}''' def lowerCamelCase_ ( self : str ): '''simple docstring''' return self.data def lowerCamelCase_ ( self : List[str] ): '''simple docstring''' return self.next def lowerCamelCase_ ( self : str ): '''simple docstring''' return self.previous class UpperCamelCase__ : """simple docstring""" def __init__( self : Optional[int] , lowerCamelCase_ : Optional[int] ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = head def __iter__( self : Any ): '''simple docstring''' return self def lowerCamelCase_ ( self : str ): '''simple docstring''' if not self.current: raise StopIteration else: SCREAMING_SNAKE_CASE : Any = self.current.get_data() SCREAMING_SNAKE_CASE : Optional[Any] = self.current.get_next() return value class UpperCamelCase__ : """simple docstring""" def __init__( self : Any ): '''simple docstring''' SCREAMING_SNAKE_CASE : str = None # First node in list SCREAMING_SNAKE_CASE : Tuple = None # Last node in list def __str__( self : Tuple ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = self.head SCREAMING_SNAKE_CASE : str = [] while current is not None: nodes.append(current.get_data() ) SCREAMING_SNAKE_CASE : List[str] = current.get_next() return " ".join(str(lowerCamelCase_ ) for node in nodes ) def __contains__( self : Dict , lowerCamelCase_ : int ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = self.head while current: if current.get_data() == value: return True SCREAMING_SNAKE_CASE : Optional[int] = current.get_next() return False def __iter__( self : Dict ): '''simple docstring''' return LinkedListIterator(self.head ) def lowerCamelCase_ ( self : Union[str, Any] ): '''simple docstring''' if self.head: return self.head.get_data() return None def lowerCamelCase_ ( self : List[Any] ): '''simple docstring''' if self.tail: return self.tail.get_data() return None def lowerCamelCase_ ( self : str , lowerCamelCase_ : Node ): '''simple docstring''' if self.head is None: SCREAMING_SNAKE_CASE : Tuple = node SCREAMING_SNAKE_CASE : Optional[int] = node else: self.insert_before_node(self.head , lowerCamelCase_ ) def lowerCamelCase_ ( self : Dict , lowerCamelCase_ : Node ): '''simple docstring''' if self.head is None: self.set_head(lowerCamelCase_ ) else: self.insert_after_node(self.tail , lowerCamelCase_ ) def lowerCamelCase_ ( self : Optional[int] , lowerCamelCase_ : int ): '''simple docstring''' SCREAMING_SNAKE_CASE : int = Node(lowerCamelCase_ ) if self.head is None: self.set_head(lowerCamelCase_ ) else: self.set_tail(lowerCamelCase_ ) def lowerCamelCase_ ( self : Tuple , lowerCamelCase_ : Node , lowerCamelCase_ : Node ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = node SCREAMING_SNAKE_CASE : str = node.previous if node.get_previous() is None: SCREAMING_SNAKE_CASE : Tuple = node_to_insert else: SCREAMING_SNAKE_CASE : int = node_to_insert SCREAMING_SNAKE_CASE : int = node_to_insert def lowerCamelCase_ ( self : int , lowerCamelCase_ : Node , lowerCamelCase_ : Node ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = node SCREAMING_SNAKE_CASE : Any = node.next if node.get_next() is None: SCREAMING_SNAKE_CASE : Tuple = node_to_insert else: SCREAMING_SNAKE_CASE : Optional[Any] = node_to_insert SCREAMING_SNAKE_CASE : Dict = node_to_insert def lowerCamelCase_ ( self : str , lowerCamelCase_ : int , lowerCamelCase_ : int ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = 1 SCREAMING_SNAKE_CASE : Tuple = Node(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = self.head while node: if current_position == position: self.insert_before_node(lowerCamelCase_ , lowerCamelCase_ ) return current_position += 1 SCREAMING_SNAKE_CASE : int = node.next self.insert_after_node(self.tail , lowerCamelCase_ ) def lowerCamelCase_ ( self : List[Any] , lowerCamelCase_ : int ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = self.head while node: if node.get_data() == item: return node SCREAMING_SNAKE_CASE : int = node.get_next() raise Exception("""Node not found""" ) def lowerCamelCase_ ( self : Any , lowerCamelCase_ : List[str] ): '''simple docstring''' if (node := self.get_node(lowerCamelCase_ )) is not None: if node == self.head: SCREAMING_SNAKE_CASE : Any = self.head.get_next() if node == self.tail: SCREAMING_SNAKE_CASE : Tuple = self.tail.get_previous() self.remove_node_pointers(lowerCamelCase_ ) @staticmethod def lowerCamelCase_ ( lowerCamelCase_ : Node ): '''simple docstring''' if node.get_next(): SCREAMING_SNAKE_CASE : Optional[int] = node.previous if node.get_previous(): SCREAMING_SNAKE_CASE : List[Any] = node.next SCREAMING_SNAKE_CASE : Any = None SCREAMING_SNAKE_CASE : List[Any] = None def lowerCamelCase_ ( self : Tuple ): '''simple docstring''' return self.head is None def __A ( ): """simple docstring""" if __name__ == "__main__": import doctest doctest.testmod()
323
'''simple docstring''' import os from typing import Dict, List, Union import tensorflow as tf from keras_nlp.tokenizers import BytePairTokenizer from tensorflow_text import pad_model_inputs from .tokenization_gpta import GPTaTokenizer class UpperCamelCase__ ( tf.keras.layers.Layer ): """simple docstring""" def __init__( self : Dict , lowerCamelCase_ : Dict[str, int] , lowerCamelCase_ : List[str] , lowerCamelCase_ : int = None , lowerCamelCase_ : int = None ): '''simple docstring''' super().__init__() SCREAMING_SNAKE_CASE : Any = pad_token_id SCREAMING_SNAKE_CASE : List[Any] = max_length SCREAMING_SNAKE_CASE : Optional[int] = vocab SCREAMING_SNAKE_CASE : List[Any] = merges SCREAMING_SNAKE_CASE : Tuple = BytePairTokenizer(lowerCamelCase_ , lowerCamelCase_ , sequence_length=lowerCamelCase_ ) @classmethod def lowerCamelCase_ ( cls : Any , lowerCamelCase_ : GPTaTokenizer , *lowerCamelCase_ : str , **lowerCamelCase_ : str ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = [""" """.join(lowerCamelCase_ ) for m in tokenizer.bpe_ranks.keys()] SCREAMING_SNAKE_CASE : List[str] = tokenizer.get_vocab() return cls(lowerCamelCase_ , lowerCamelCase_ , *lowerCamelCase_ , **lowerCamelCase_ ) @classmethod def lowerCamelCase_ ( cls : List[Any] , lowerCamelCase_ : Union[str, os.PathLike] , *lowerCamelCase_ : str , **lowerCamelCase_ : Union[str, Any] ): '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = GPTaTokenizer.from_pretrained(lowerCamelCase_ , *lowerCamelCase_ , **lowerCamelCase_ ) return cls.from_tokenizer(lowerCamelCase_ , *lowerCamelCase_ , **lowerCamelCase_ ) @classmethod def lowerCamelCase_ ( cls : List[str] , lowerCamelCase_ : Tuple ): '''simple docstring''' return cls(**lowerCamelCase_ ) def lowerCamelCase_ ( self : Dict ): '''simple docstring''' return { "vocab": self.vocab, "merges": self.merges, "max_length": self.max_length, "pad_token_id": self.pad_token_id, } def lowerCamelCase_ ( self : Union[str, Any] , lowerCamelCase_ : Dict , lowerCamelCase_ : int = None ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = self.tf_tokenizer(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[int] = tf.ones_like(lowerCamelCase_ ) if self.pad_token_id is not None: # pad the tokens up to max length SCREAMING_SNAKE_CASE : Optional[int] = max_length if max_length is not None else self.max_length if max_length is not None: SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Union[str, Any] = pad_model_inputs( lowerCamelCase_ , max_seq_length=lowerCamelCase_ , pad_value=self.pad_token_id ) return {"attention_mask": attention_mask, "input_ids": input_ids}
323
1
"""simple docstring""" import argparse from diffusers.pipelines.stable_diffusion.convert_from_ckpt import download_controlnet_from_original_ckpt if __name__ == "__main__": __SCREAMING_SNAKE_CASE : Optional[Any] = argparse.ArgumentParser() parser.add_argument( '--checkpoint_path', default=None, type=str, required=True, help='Path to the checkpoint to convert.' ) parser.add_argument( '--original_config_file', type=str, required=True, help='The YAML config file corresponding to the original architecture.', ) parser.add_argument( '--num_in_channels', default=None, type=int, help='The number of input channels. If `None` number of input channels will be automatically inferred.', ) parser.add_argument( '--image_size', default=512, type=int, help=( 'The image size that the model was trained on. Use 512 for Stable Diffusion v1.X and Stable Siffusion v2' ' Base. Use 768 for Stable Diffusion v2.' ), ) parser.add_argument( '--extract_ema', action='store_true', help=( 'Only relevant for checkpoints that have both EMA and non-EMA weights. Whether to extract the EMA weights' ' or not. Defaults to `False`. Add `--extract_ema` to extract the EMA weights. EMA weights usually yield' ' higher quality images for inference. Non-EMA weights are usually better to continue fine-tuning.' ), ) parser.add_argument( '--upcast_attention', action='store_true', help=( 'Whether the attention computation should always be upcasted. This is necessary when running stable' ' diffusion 2.1.' ), ) parser.add_argument( '--from_safetensors', action='store_true', help='If `--checkpoint_path` is in `safetensors` format, load checkpoint with safetensors instead of PyTorch.', ) parser.add_argument( '--to_safetensors', action='store_true', help='Whether to store pipeline in safetensors format or not.', ) parser.add_argument('--dump_path', default=None, type=str, required=True, help='Path to the output model.') parser.add_argument('--device', type=str, help='Device to use (e.g. cpu, cuda:0, cuda:1, etc.)') def _a ( _SCREAMING_SNAKE_CASE ) -> Optional[Any]: if string == "True": return True elif string == "False": return False else: raise ValueError(f"""could not parse string as bool {string}""" ) parser.add_argument( '--use_linear_projection', help='Override for use linear projection', required=False, type=parse_bool ) parser.add_argument('--cross_attention_dim', help='Override for cross attention_dim', required=False, type=int) __SCREAMING_SNAKE_CASE : List[str] = parser.parse_args() __SCREAMING_SNAKE_CASE : List[str] = download_controlnet_from_original_ckpt( checkpoint_path=args.checkpoint_path, original_config_file=args.original_config_file, image_size=args.image_size, extract_ema=args.extract_ema, num_in_channels=args.num_in_channels, upcast_attention=args.upcast_attention, from_safetensors=args.from_safetensors, device=args.device, use_linear_projection=args.use_linear_projection, cross_attention_dim=args.cross_attention_dim, ) controlnet.save_pretrained(args.dump_path, safe_serialization=args.to_safetensors)
364
"""simple docstring""" import secrets from random import shuffle from string import ascii_letters, ascii_lowercase, ascii_uppercase, digits, punctuation def _a ( _SCREAMING_SNAKE_CASE = 8 ) -> str: snake_case_ = ascii_letters + digits + punctuation return "".join(secrets.choice(_SCREAMING_SNAKE_CASE ) for _ in range(_SCREAMING_SNAKE_CASE ) ) def _a ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> str: # Password Generator = full boot with random_number, random_letters, and # random_character FUNCTIONS # Put your code here... i -= len(_SCREAMING_SNAKE_CASE ) snake_case_ = i // 3 snake_case_ = i % 3 # chars = chars_incl + random_letters(ascii_letters, i / 3 + remainder) + # random_number(digits, i / 3) + random_characters(punctuation, i / 3) snake_case_ = ( chars_incl + random(_SCREAMING_SNAKE_CASE , quotient + remainder ) + random(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) + random(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) snake_case_ = list(_SCREAMING_SNAKE_CASE ) shuffle(_SCREAMING_SNAKE_CASE ) return "".join(_SCREAMING_SNAKE_CASE ) # random is a generalised function for letters, characters and numbers def _a ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> str: return "".join(secrets.choice(_SCREAMING_SNAKE_CASE ) for _ in range(_SCREAMING_SNAKE_CASE ) ) def _a ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Any: pass # Put your code here... def _a ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Optional[int]: pass # Put your code here... def _a ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> List[Any]: pass # Put your code here... def _a ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = 8 ) -> bool: if len(_SCREAMING_SNAKE_CASE ) < min_length: # Your Password must be at least 8 characters long return False snake_case_ = any(char in ascii_uppercase for char in password ) snake_case_ = any(char in ascii_lowercase for char in password ) snake_case_ = any(char in digits for char in password ) snake_case_ = any(char in punctuation for char in password ) return upper and lower and num and spec_char # Passwords should contain UPPERCASE, lowerase # numbers, and special characters def _a ( ) -> Union[str, Any]: snake_case_ = int(input("""Please indicate the max length of your password: """ ).strip() ) snake_case_ = input( """Please indicate the characters that must be in your password: """ ).strip() print("""Password generated:""" , password_generator(_SCREAMING_SNAKE_CASE ) ) print( """Alternative Password generated:""" , alternative_password_generator(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) , ) print("""[If you are thinking of using this passsword, You better save it.]""" ) if __name__ == "__main__": main()
233
0
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( BertTokenizer, ViltConfig, ViltForImageAndTextRetrieval, ViltForImagesAndTextClassification, ViltForMaskedLM, ViltForQuestionAnswering, ViltImageProcessor, ViltProcessor, ) from transformers.utils import logging logging.set_verbosity_info() _lowerCamelCase =logging.get_logger(__name__) def snake_case__ ( lowerCAmelCase_, lowerCAmelCase_=False, lowerCAmelCase_=False, lowerCAmelCase_=False ): """simple docstring""" SCREAMING_SNAKE_CASE =[] for i in range(config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append((F'transformer.blocks.{i}.norm1.weight', F'vilt.encoder.layer.{i}.layernorm_before.weight') ) rename_keys.append((F'transformer.blocks.{i}.norm1.bias', F'vilt.encoder.layer.{i}.layernorm_before.bias') ) rename_keys.append( (F'transformer.blocks.{i}.attn.proj.weight', F'vilt.encoder.layer.{i}.attention.output.dense.weight') ) rename_keys.append( (F'transformer.blocks.{i}.attn.proj.bias', F'vilt.encoder.layer.{i}.attention.output.dense.bias') ) rename_keys.append((F'transformer.blocks.{i}.norm2.weight', F'vilt.encoder.layer.{i}.layernorm_after.weight') ) rename_keys.append((F'transformer.blocks.{i}.norm2.bias', F'vilt.encoder.layer.{i}.layernorm_after.bias') ) rename_keys.append( (F'transformer.blocks.{i}.mlp.fc1.weight', F'vilt.encoder.layer.{i}.intermediate.dense.weight') ) rename_keys.append((F'transformer.blocks.{i}.mlp.fc1.bias', F'vilt.encoder.layer.{i}.intermediate.dense.bias') ) rename_keys.append((F'transformer.blocks.{i}.mlp.fc2.weight', F'vilt.encoder.layer.{i}.output.dense.weight') ) rename_keys.append((F'transformer.blocks.{i}.mlp.fc2.bias', F'vilt.encoder.layer.{i}.output.dense.bias') ) # embeddings rename_keys.extend( [ # text embeddings ('text_embeddings.word_embeddings.weight', 'vilt.embeddings.text_embeddings.word_embeddings.weight'), ( 'text_embeddings.position_embeddings.weight', 'vilt.embeddings.text_embeddings.position_embeddings.weight', ), ('text_embeddings.position_ids', 'vilt.embeddings.text_embeddings.position_ids'), ( 'text_embeddings.token_type_embeddings.weight', 'vilt.embeddings.text_embeddings.token_type_embeddings.weight', ), ('text_embeddings.LayerNorm.weight', 'vilt.embeddings.text_embeddings.LayerNorm.weight'), ('text_embeddings.LayerNorm.bias', 'vilt.embeddings.text_embeddings.LayerNorm.bias'), # patch embeddings ('transformer.cls_token', 'vilt.embeddings.cls_token'), ('transformer.patch_embed.proj.weight', 'vilt.embeddings.patch_embeddings.projection.weight'), ('transformer.patch_embed.proj.bias', 'vilt.embeddings.patch_embeddings.projection.bias'), ('transformer.pos_embed', 'vilt.embeddings.position_embeddings'), # token type embeddings ('token_type_embeddings.weight', 'vilt.embeddings.token_type_embeddings.weight'), ] ) # final layernorm + pooler rename_keys.extend( [ ('transformer.norm.weight', 'vilt.layernorm.weight'), ('transformer.norm.bias', 'vilt.layernorm.bias'), ('pooler.dense.weight', 'vilt.pooler.dense.weight'), ('pooler.dense.bias', 'vilt.pooler.dense.bias'), ] ) # classifier head(s) if vqa_model: # classification head rename_keys.extend( [ ('vqa_classifier.0.weight', 'classifier.0.weight'), ('vqa_classifier.0.bias', 'classifier.0.bias'), ('vqa_classifier.1.weight', 'classifier.1.weight'), ('vqa_classifier.1.bias', 'classifier.1.bias'), ('vqa_classifier.3.weight', 'classifier.3.weight'), ('vqa_classifier.3.bias', 'classifier.3.bias'), ] ) elif nlvr_model: # classification head rename_keys.extend( [ ('nlvr2_classifier.0.weight', 'classifier.0.weight'), ('nlvr2_classifier.0.bias', 'classifier.0.bias'), ('nlvr2_classifier.1.weight', 'classifier.1.weight'), ('nlvr2_classifier.1.bias', 'classifier.1.bias'), ('nlvr2_classifier.3.weight', 'classifier.3.weight'), ('nlvr2_classifier.3.bias', 'classifier.3.bias'), ] ) else: pass return rename_keys def snake_case__ ( lowerCAmelCase_, lowerCAmelCase_ ): """simple docstring""" for i in range(config.num_hidden_layers ): SCREAMING_SNAKE_CASE ='vilt.' # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) SCREAMING_SNAKE_CASE =state_dict.pop(F'transformer.blocks.{i}.attn.qkv.weight' ) SCREAMING_SNAKE_CASE =state_dict.pop(F'transformer.blocks.{i}.attn.qkv.bias' ) # next, add query, keys and values (in that order) to the state dict SCREAMING_SNAKE_CASE =in_proj_weight[ : config.hidden_size, : ] SCREAMING_SNAKE_CASE =in_proj_bias[: config.hidden_size] SCREAMING_SNAKE_CASE =in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] SCREAMING_SNAKE_CASE =in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] SCREAMING_SNAKE_CASE =in_proj_weight[ -config.hidden_size :, : ] SCREAMING_SNAKE_CASE =in_proj_bias[-config.hidden_size :] def snake_case__ ( lowerCAmelCase_ ): """simple docstring""" SCREAMING_SNAKE_CASE =['head.weight', 'head.bias'] for k in ignore_keys: state_dict.pop(lowerCAmelCase_, lowerCAmelCase_ ) def snake_case__ ( lowerCAmelCase_, lowerCAmelCase_, lowerCAmelCase_ ): """simple docstring""" SCREAMING_SNAKE_CASE =dct.pop(lowerCAmelCase_ ) SCREAMING_SNAKE_CASE =val @torch.no_grad() def snake_case__ ( lowerCAmelCase_, lowerCAmelCase_ ): """simple docstring""" SCREAMING_SNAKE_CASE =ViltConfig(image_size=384, patch_size=32, tie_word_embeddings=lowerCAmelCase_ ) SCREAMING_SNAKE_CASE =False SCREAMING_SNAKE_CASE =False SCREAMING_SNAKE_CASE =False SCREAMING_SNAKE_CASE =False if "vqa" in checkpoint_url: SCREAMING_SNAKE_CASE =True SCREAMING_SNAKE_CASE =3129 SCREAMING_SNAKE_CASE ='huggingface/label-files' SCREAMING_SNAKE_CASE ='vqa2-id2label.json' SCREAMING_SNAKE_CASE =json.load(open(hf_hub_download(lowerCAmelCase_, lowerCAmelCase_, repo_type='dataset' ), 'r' ) ) SCREAMING_SNAKE_CASE ={int(lowerCAmelCase_ ): v for k, v in idalabel.items()} SCREAMING_SNAKE_CASE =idalabel SCREAMING_SNAKE_CASE ={v: k for k, v in idalabel.items()} SCREAMING_SNAKE_CASE =ViltForQuestionAnswering(lowerCAmelCase_ ) elif "nlvr" in checkpoint_url: SCREAMING_SNAKE_CASE =True SCREAMING_SNAKE_CASE =2 SCREAMING_SNAKE_CASE ={0: 'False', 1: 'True'} SCREAMING_SNAKE_CASE ={v: k for k, v in config.idalabel.items()} SCREAMING_SNAKE_CASE =3 SCREAMING_SNAKE_CASE =ViltForImagesAndTextClassification(lowerCAmelCase_ ) elif "irtr" in checkpoint_url: SCREAMING_SNAKE_CASE =True SCREAMING_SNAKE_CASE =ViltForImageAndTextRetrieval(lowerCAmelCase_ ) elif "mlm_itm" in checkpoint_url: SCREAMING_SNAKE_CASE =True SCREAMING_SNAKE_CASE =ViltForMaskedLM(lowerCAmelCase_ ) else: raise ValueError('Unknown model type' ) # load state_dict of original model, remove and rename some keys SCREAMING_SNAKE_CASE =torch.hub.load_state_dict_from_url(lowerCAmelCase_, map_location='cpu' )['state_dict'] SCREAMING_SNAKE_CASE =create_rename_keys(lowerCAmelCase_, lowerCAmelCase_, lowerCAmelCase_, lowerCAmelCase_ ) for src, dest in rename_keys: rename_key(lowerCAmelCase_, lowerCAmelCase_, lowerCAmelCase_ ) read_in_q_k_v(lowerCAmelCase_, lowerCAmelCase_ ) if mlm_model or irtr_model: SCREAMING_SNAKE_CASE =['itm_score.fc.weight', 'itm_score.fc.bias'] for k in ignore_keys: state_dict.pop(lowerCAmelCase_, lowerCAmelCase_ ) # load state dict into HuggingFace model model.eval() if mlm_model: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE =model.load_state_dict(lowerCAmelCase_, strict=lowerCAmelCase_ ) assert missing_keys == ["mlm_score.decoder.bias"] else: model.load_state_dict(lowerCAmelCase_ ) # Define processor SCREAMING_SNAKE_CASE =ViltImageProcessor(size=384 ) SCREAMING_SNAKE_CASE =BertTokenizer.from_pretrained('bert-base-uncased' ) SCREAMING_SNAKE_CASE =ViltProcessor(lowerCAmelCase_, lowerCAmelCase_ ) # Forward pass on example inputs (image + text) if nlvr_model: SCREAMING_SNAKE_CASE =Image.open(requests.get('https://lil.nlp.cornell.edu/nlvr/exs/ex0_0.jpg', stream=lowerCAmelCase_ ).raw ) SCREAMING_SNAKE_CASE =Image.open(requests.get('https://lil.nlp.cornell.edu/nlvr/exs/ex0_0.jpg', stream=lowerCAmelCase_ ).raw ) SCREAMING_SNAKE_CASE =( 'The left image contains twice the number of dogs as the right image, and at least two dogs in total are' ' standing.' ) SCREAMING_SNAKE_CASE =processor(lowerCAmelCase_, lowerCAmelCase_, return_tensors='pt' ) SCREAMING_SNAKE_CASE =processor(lowerCAmelCase_, lowerCAmelCase_, return_tensors='pt' ) SCREAMING_SNAKE_CASE =model( input_ids=encoding_a.input_ids, pixel_values=encoding_a.pixel_values, pixel_values_a=encoding_a.pixel_values, ) else: SCREAMING_SNAKE_CASE =Image.open(requests.get('http://images.cocodataset.org/val2017/000000039769.jpg', stream=lowerCAmelCase_ ).raw ) if mlm_model: SCREAMING_SNAKE_CASE ='a bunch of [MASK] laying on a [MASK].' else: SCREAMING_SNAKE_CASE ='How many cats are there?' SCREAMING_SNAKE_CASE =processor(lowerCAmelCase_, lowerCAmelCase_, return_tensors='pt' ) SCREAMING_SNAKE_CASE =model(**lowerCAmelCase_ ) # Verify outputs if mlm_model: SCREAMING_SNAKE_CASE =torch.Size([1, 11, 30522] ) SCREAMING_SNAKE_CASE =torch.tensor([-12.5061, -12.5123, -12.5174] ) assert outputs.logits.shape == expected_shape assert torch.allclose(outputs.logits[0, 0, :3], lowerCAmelCase_, atol=1e-4 ) # verify masked token prediction equals "cats" SCREAMING_SNAKE_CASE =outputs.logits[0, 4, :].argmax(-1 ).item() assert tokenizer.decode([predicted_id] ) == "cats" elif vqa_model: SCREAMING_SNAKE_CASE =torch.Size([1, 3129] ) SCREAMING_SNAKE_CASE =torch.tensor([-15.9495, -18.1472, -10.3041] ) assert torch.allclose(outputs.logits[0, :3], lowerCAmelCase_, atol=1e-4 ) assert outputs.logits.shape == expected_shape assert torch.allclose(outputs.logits[0, 0, :3], lowerCAmelCase_, atol=1e-4 ) # verify vqa prediction equals "2" SCREAMING_SNAKE_CASE =outputs.logits.argmax(-1 ).item() assert model.config.idalabel[predicted_idx] == "2" elif nlvr_model: SCREAMING_SNAKE_CASE =torch.Size([1, 2] ) SCREAMING_SNAKE_CASE =torch.tensor([-2.8721, 2.1291] ) assert torch.allclose(outputs.logits[0, :3], lowerCAmelCase_, atol=1e-4 ) assert outputs.logits.shape == expected_shape Path(lowerCAmelCase_ ).mkdir(exist_ok=lowerCAmelCase_ ) print(F'Saving model and processor to {pytorch_dump_folder_path}' ) model.save_pretrained(lowerCAmelCase_ ) processor.save_pretrained(lowerCAmelCase_ ) if __name__ == "__main__": _lowerCamelCase =argparse.ArgumentParser() # Required parameters parser.add_argument( "--checkpoint_url", default="https://github.com/dandelin/ViLT/releases/download/200k/vilt_200k_mlm_itm.ckpt", type=str, help="URL of the checkpoint you'd like to convert.", ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model directory." ) _lowerCamelCase =parser.parse_args() convert_vilt_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)
334
from __future__ import annotations import random # Maximum size of the population. Bigger could be faster but is more memory expensive. _lowerCamelCase =2_00 # 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. _lowerCamelCase =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. _lowerCamelCase =0.4 # Just a seed to improve randomness required by the algorithm. random.seed(random.randint(0, 10_00)) def snake_case__ ( lowerCAmelCase_, lowerCAmelCase_ ): """simple docstring""" SCREAMING_SNAKE_CASE =len([g for position, g in enumerate(lowerCAmelCase_ ) if g == main_target[position]] ) return (item, float(lowerCAmelCase_ )) def snake_case__ ( lowerCAmelCase_, lowerCAmelCase_ ): """simple docstring""" SCREAMING_SNAKE_CASE =random.randint(0, len(lowerCAmelCase_ ) - 1 ) SCREAMING_SNAKE_CASE =parent_a[:random_slice] + parent_a[random_slice:] SCREAMING_SNAKE_CASE =parent_a[:random_slice] + parent_a[random_slice:] return (child_a, child_a) def snake_case__ ( lowerCAmelCase_, lowerCAmelCase_ ): """simple docstring""" SCREAMING_SNAKE_CASE =list(lowerCAmelCase_ ) if random.uniform(0, 1 ) < MUTATION_PROBABILITY: SCREAMING_SNAKE_CASE =random.choice(lowerCAmelCase_ ) return "".join(lowerCAmelCase_ ) def snake_case__ ( lowerCAmelCase_, lowerCAmelCase_, lowerCAmelCase_, ): """simple docstring""" SCREAMING_SNAKE_CASE =[] # Generate more children proportionally to the fitness score. SCREAMING_SNAKE_CASE =int(parent_a[1] * 100 ) + 1 SCREAMING_SNAKE_CASE =10 if child_n >= 10 else child_n for _ in range(lowerCAmelCase_ ): SCREAMING_SNAKE_CASE =population_score[random.randint(0, lowerCAmelCase_ )][0] SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE =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 snake_case__ ( lowerCAmelCase_, lowerCAmelCase_, lowerCAmelCase_ = True ): """simple docstring""" if N_POPULATION < N_SELECTED: SCREAMING_SNAKE_CASE =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. SCREAMING_SNAKE_CASE =sorted({c for c in target if c not in genes} ) if not_in_genes_list: SCREAMING_SNAKE_CASE =F'{not_in_genes_list} is not in genes list, evolution cannot converge' raise ValueError(lowerCAmelCase_ ) # Generate random starting population. SCREAMING_SNAKE_CASE =[] 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. SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE =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. SCREAMING_SNAKE_CASE =[evaluate(lowerCAmelCase_, lowerCAmelCase_ ) for item in population] # Check if there is a matching evolution. SCREAMING_SNAKE_CASE =sorted(lowerCAmelCase_, key=lambda lowerCAmelCase_ : 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. SCREAMING_SNAKE_CASE =population[: int(N_POPULATION / 3 )] population.clear() population.extend(lowerCAmelCase_ ) # Normalize population score to be between 0 and 1. SCREAMING_SNAKE_CASE =[ (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__": _lowerCamelCase =( "This is a genetic algorithm to evaluate, combine, evolve, and mutate a string!" ) _lowerCamelCase =list( " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklm" "nopqrstuvwxyz.,;!?+-*#@^'èéòà€ù=)(&%$£/\\" ) _lowerCamelCase , _lowerCamelCase , _lowerCamelCase =basic(target_str, genes_list) print( f'\nGeneration: {generation}\nTotal Population: {population}\nTarget: {target}' )
334
1
import torch from transformers import PreTrainedModel, XLMRobertaConfig, XLMRobertaModel class UpperCamelCase_ (__A ): __magic_name__ = '''M-CLIP''' def __init__( self : Any , lowerCAmelCase_ : str=1_024 , lowerCAmelCase_ : str=768 , **lowerCAmelCase_ : Union[str, Any] ) -> List[str]: UpperCAmelCase_ : Tuple = transformerDimSize UpperCAmelCase_ : List[str] = imageDimSize super().__init__(**lowerCAmelCase_ ) class UpperCamelCase_ (__A ): __magic_name__ = MCLIPConfig def __init__( self : str , lowerCAmelCase_ : int , *lowerCAmelCase_ : int , **lowerCAmelCase_ : List[Any] ) -> Any: super().__init__(lowerCAmelCase_ , *lowerCAmelCase_ , **lowerCAmelCase_ ) UpperCAmelCase_ : Union[str, Any] = XLMRobertaModel(lowerCAmelCase_ ) UpperCAmelCase_ : Optional[int] = torch.nn.Linear( in_features=config.transformerDimensions , out_features=config.numDims ) def _SCREAMING_SNAKE_CASE ( self : int , lowerCAmelCase_ : List[Any] , lowerCAmelCase_ : Union[str, Any] ) -> List[str]: UpperCAmelCase_ : Any = self.transformer(input_ids=lowerCAmelCase_ , attention_mask=lowerCAmelCase_ )[0] UpperCAmelCase_ : Tuple = (embs * attention_mask.unsqueeze(2 )).sum(dim=1 ) / attention_mask.sum(dim=1 )[:, None] return self.LinearTransformation(lowerCAmelCase_ ), embs
364
"""simple docstring""" import math import sys import cva import numpy as np def snake_case ( A__ ,A__ ): # For applying gaussian function for each element in matrix. UpperCAmelCase_ : int = math.sqrt(A__ ) UpperCAmelCase_ : Tuple = 1 / (sigma * math.sqrt(2 * math.pi )) return cons * np.exp(-((img / sigma) ** 2) * 0.5 ) def snake_case ( A__ ,A__ ,A__ ,A__ ): UpperCAmelCase_ : int = kernel_size // 2 return img[x - half : x + half + 1, y - half : y + half + 1] def snake_case ( A__ ,A__ ): # Creates a gaussian kernel of given dimension. UpperCAmelCase_ : List[Any] = np.zeros((kernel_size, kernel_size) ) for i in range(0 ,A__ ): for j in range(0 ,A__ ): UpperCAmelCase_ : List[Any] = math.sqrt( abs(i - kernel_size // 2 ) ** 2 + abs(j - kernel_size // 2 ) ** 2 ) return vec_gaussian(A__ ,A__ ) def snake_case ( A__ ,A__ ,A__ ,A__ ,): UpperCAmelCase_ : Union[str, Any] = np.zeros(img.shape ) UpperCAmelCase_ : Tuple = get_gauss_kernel(A__ ,A__ ) UpperCAmelCase_ , UpperCAmelCase_ : Union[str, Any] = img.shape for i in range(kernel_size // 2 ,size_x - kernel_size // 2 ): for j in range(kernel_size // 2 ,size_y - kernel_size // 2 ): UpperCAmelCase_ : str = get_slice(A__ ,A__ ,A__ ,A__ ) UpperCAmelCase_ : int = img_s - img_s[kernel_size // 2, kernel_size // 2] UpperCAmelCase_ : Optional[Any] = vec_gaussian(A__ ,A__ ) UpperCAmelCase_ : int = np.multiply(A__ ,A__ ) UpperCAmelCase_ : List[Any] = np.multiply(A__ ,A__ ) UpperCAmelCase_ : str = np.sum(A__ ) / np.sum(A__ ) UpperCAmelCase_ : Tuple = val return imga def snake_case ( A__ ): UpperCAmelCase_ : Optional[int] = args[1] if args[1:] else "../image_data/lena.jpg" UpperCAmelCase_ : Tuple = float(args[2] ) if args[2:] else 1.0 UpperCAmelCase_ : Optional[int] = float(args[3] ) if args[3:] else 1.0 if args[4:]: UpperCAmelCase_ : Dict = int(args[4] ) UpperCAmelCase_ : List[str] = kernel_size + abs(kernel_size % 2 - 1 ) else: UpperCAmelCase_ : Dict = 5 return filename, spatial_variance, intensity_variance, kernel_size if __name__ == "__main__": lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ = parse_args(sys.argv) lowerCamelCase_ = cva.imread(filename, 0) cva.imshow('''input image''', img) lowerCamelCase_ = img / 255 lowerCamelCase_ = out.astype('''float32''') lowerCamelCase_ = bilateral_filter(out, spatial_variance, intensity_variance, kernel_size) lowerCamelCase_ = out * 255 lowerCamelCase_ = np.uinta(out) cva.imshow('''output image''', out) cva.waitKey(0) cva.destroyAllWindows()
253
0
"""simple docstring""" import itertools import string from collections.abc import Generator, Iterable def _A (__a , __a ) -> Dict: """simple docstring""" SCREAMING_SNAKE_CASE_ : str = iter(lowerCAmelCase_ ) while True: SCREAMING_SNAKE_CASE_ : int = tuple(itertools.islice(lowerCAmelCase_ , lowerCAmelCase_ ) ) if not chunk: return yield chunk def _A (__a ) -> List[Any]: """simple docstring""" SCREAMING_SNAKE_CASE_ : List[Any] = """""".join([c.upper() for c in dirty if c in string.ascii_letters] ) SCREAMING_SNAKE_CASE_ : Optional[int] = """""" if len(lowerCAmelCase_ ) < 2: return dirty for i in range(len(lowerCAmelCase_ ) - 1 ): clean += dirty[i] if dirty[i] == dirty[i + 1]: clean += "X" clean += dirty[-1] if len(lowerCAmelCase_ ) & 1: clean += "X" return clean def _A (__a ) -> Tuple: """simple docstring""" SCREAMING_SNAKE_CASE_ : Dict = """ABCDEFGHIKLMNOPQRSTUVWXYZ""" # we're using a list instead of a '2d' array because it makes the math # for setting up the table and doing the actual encoding/decoding simpler SCREAMING_SNAKE_CASE_ : Optional[int] = [] # copy key chars into the table if they are in `alphabet` ignoring duplicates for char in key.upper(): if char not in table and char in alphabet: table.append(lowerCAmelCase_ ) # fill the rest of the table in with the remaining alphabet chars for char in alphabet: if char not in table: table.append(lowerCAmelCase_ ) return table def _A (__a , __a ) -> int: """simple docstring""" SCREAMING_SNAKE_CASE_ : Optional[Any] = generate_table(lowerCAmelCase_ ) SCREAMING_SNAKE_CASE_ : Optional[int] = prepare_input(lowerCAmelCase_ ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = """""" # https://en.wikipedia.org/wiki/Playfair_cipher#Description for chara, chara in chunker(lowerCAmelCase_ , 2 ): SCREAMING_SNAKE_CASE_ : Any = divmod(table.index(lowerCAmelCase_ ) , 5 ) SCREAMING_SNAKE_CASE_ : Tuple = divmod(table.index(lowerCAmelCase_ ) , 5 ) if rowa == rowa: ciphertext += table[rowa * 5 + (cola + 1) % 5] ciphertext += table[rowa * 5 + (cola + 1) % 5] elif cola == cola: ciphertext += table[((rowa + 1) % 5) * 5 + cola] ciphertext += table[((rowa + 1) % 5) * 5 + cola] else: # rectangle ciphertext += table[rowa * 5 + cola] ciphertext += table[rowa * 5 + cola] return ciphertext def _A (__a , __a ) -> Optional[int]: """simple docstring""" SCREAMING_SNAKE_CASE_ : Optional[int] = generate_table(lowerCAmelCase_ ) SCREAMING_SNAKE_CASE_ : Tuple = """""" # https://en.wikipedia.org/wiki/Playfair_cipher#Description for chara, chara in chunker(lowerCAmelCase_ , 2 ): SCREAMING_SNAKE_CASE_ : Optional[int] = divmod(table.index(lowerCAmelCase_ ) , 5 ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = divmod(table.index(lowerCAmelCase_ ) , 5 ) if rowa == rowa: plaintext += table[rowa * 5 + (cola - 1) % 5] plaintext += table[rowa * 5 + (cola - 1) % 5] elif cola == cola: plaintext += table[((rowa - 1) % 5) * 5 + cola] plaintext += table[((rowa - 1) % 5) * 5 + cola] else: # rectangle plaintext += table[rowa * 5 + cola] plaintext += table[rowa * 5 + cola] return plaintext
91
import random import unittest import numpy as np from diffusers import ( DPMSolverMultistepScheduler, EulerAncestralDiscreteScheduler, EulerDiscreteScheduler, LMSDiscreteScheduler, OnnxStableDiffusionImgaImgPipeline, PNDMScheduler, ) from diffusers.utils import floats_tensor from diffusers.utils.testing_utils import ( is_onnx_available, load_image, nightly, require_onnxruntime, require_torch_gpu, ) from ..test_pipelines_onnx_common import OnnxPipelineTesterMixin if is_onnx_available(): import onnxruntime as ort class lowerCAmelCase ( __a , unittest.TestCase ): '''simple docstring''' _A : List[Any] = '''hf-internal-testing/tiny-random-OnnxStableDiffusionPipeline''' def lowerCAmelCase ( self : int , __a : List[Any]=0 ) -> Optional[int]: """simple docstring""" __lowercase : Any = floats_tensor((1, 3, 128, 128) , rng=random.Random(__a ) ) __lowercase : Any = np.random.RandomState(__a ) __lowercase : Union[str, Any] = { """prompt""": """A painting of a squirrel eating a burger""", """image""": image, """generator""": generator, """num_inference_steps""": 3, """strength""": 0.75, """guidance_scale""": 7.5, """output_type""": """numpy""", } return inputs def lowerCAmelCase ( self : Union[str, Any] ) -> Any: """simple docstring""" __lowercase : List[Any] = OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint , provider="""CPUExecutionProvider""" ) pipe.set_progress_bar_config(disable=__a ) __lowercase : Optional[Any] = self.get_dummy_inputs() __lowercase : str = pipe(**__a ).images __lowercase : Union[str, Any] = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 128, 128, 3) __lowercase : List[Any] = np.array([0.69643, 0.58484, 0.50314, 0.58760, 0.55368, 0.59643, 0.51529, 0.41217, 0.49087] ) assert np.abs(image_slice - expected_slice ).max() < 1E-1 def lowerCAmelCase ( self : Any ) -> Any: """simple docstring""" __lowercase : str = OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint , provider="""CPUExecutionProvider""" ) __lowercase : Union[str, Any] = PNDMScheduler.from_config(pipe.scheduler.config , skip_prk_steps=__a ) pipe.set_progress_bar_config(disable=__a ) __lowercase : Dict = self.get_dummy_inputs() __lowercase : Optional[int] = pipe(**__a ).images __lowercase : Union[str, Any] = image[0, -3:, -3:, -1] assert image.shape == (1, 128, 128, 3) __lowercase : Any = np.array([0.61737, 0.54642, 0.53183, 0.54465, 0.52742, 0.60525, 0.49969, 0.40655, 0.48154] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1 def lowerCAmelCase ( self : int ) -> str: """simple docstring""" __lowercase : int = OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint , provider="""CPUExecutionProvider""" ) __lowercase : Union[str, Any] = LMSDiscreteScheduler.from_config(pipe.scheduler.config ) pipe.set_progress_bar_config(disable=__a ) # warmup pass to apply optimizations __lowercase : Optional[int] = pipe(**self.get_dummy_inputs() ) __lowercase : Dict = self.get_dummy_inputs() __lowercase : str = pipe(**__a ).images __lowercase : Optional[Any] = image[0, -3:, -3:, -1] assert image.shape == (1, 128, 128, 3) __lowercase : Any = np.array([0.52761, 0.59977, 0.49033, 0.49619, 0.54282, 0.50311, 0.47600, 0.40918, 0.45203] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1 def lowerCAmelCase ( self : Optional[int] ) -> Optional[Any]: """simple docstring""" __lowercase : List[str] = OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint , provider="""CPUExecutionProvider""" ) __lowercase : List[Any] = EulerDiscreteScheduler.from_config(pipe.scheduler.config ) pipe.set_progress_bar_config(disable=__a ) __lowercase : Any = self.get_dummy_inputs() __lowercase : int = pipe(**__a ).images __lowercase : Dict = image[0, -3:, -3:, -1] assert image.shape == (1, 128, 128, 3) __lowercase : Optional[int] = np.array([0.52911, 0.60004, 0.49229, 0.49805, 0.54502, 0.50680, 0.47777, 0.41028, 0.45304] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1 def lowerCAmelCase ( self : List[Any] ) -> str: """simple docstring""" __lowercase : List[Any] = OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint , provider="""CPUExecutionProvider""" ) __lowercase : Dict = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config ) pipe.set_progress_bar_config(disable=__a ) __lowercase : Tuple = self.get_dummy_inputs() __lowercase : Union[str, Any] = pipe(**__a ).images __lowercase : int = image[0, -3:, -3:, -1] assert image.shape == (1, 128, 128, 3) __lowercase : Union[str, Any] = np.array([0.52911, 0.60004, 0.49229, 0.49805, 0.54502, 0.50680, 0.47777, 0.41028, 0.45304] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1 def lowerCAmelCase ( self : Dict ) -> Union[str, Any]: """simple docstring""" __lowercase : Optional[Any] = OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint , provider="""CPUExecutionProvider""" ) __lowercase : Tuple = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config ) pipe.set_progress_bar_config(disable=__a ) __lowercase : str = self.get_dummy_inputs() __lowercase : Dict = pipe(**__a ).images __lowercase : int = image[0, -3:, -3:, -1] assert image.shape == (1, 128, 128, 3) __lowercase : Dict = np.array([0.65331, 0.58277, 0.48204, 0.56059, 0.53665, 0.56235, 0.50969, 0.40009, 0.46552] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1 @nightly @require_onnxruntime @require_torch_gpu class lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' @property def lowerCAmelCase ( self : Tuple ) -> Optional[int]: """simple docstring""" return ( "CUDAExecutionProvider", { "gpu_mem_limit": "15000000000", # 15GB "arena_extend_strategy": "kSameAsRequested", }, ) @property def lowerCAmelCase ( self : str ) -> Tuple: """simple docstring""" __lowercase : Union[str, Any] = ort.SessionOptions() __lowercase : Dict = False return options def lowerCAmelCase ( self : int ) -> Dict: """simple docstring""" __lowercase : List[Any] = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/img2img/sketch-mountains-input.jpg""" ) __lowercase : Union[str, Any] = init_image.resize((768, 512) ) # using the PNDM scheduler by default __lowercase : Optional[int] = OnnxStableDiffusionImgaImgPipeline.from_pretrained( """CompVis/stable-diffusion-v1-4""" , revision="""onnx""" , safety_checker=__a , feature_extractor=__a , provider=self.gpu_provider , sess_options=self.gpu_options , ) pipe.set_progress_bar_config(disable=__a ) __lowercase : int = """A fantasy landscape, trending on artstation""" __lowercase : int = np.random.RandomState(0 ) __lowercase : List[str] = pipe( prompt=__a , image=__a , strength=0.75 , guidance_scale=7.5 , num_inference_steps=10 , generator=__a , output_type="""np""" , ) __lowercase : Any = output.images __lowercase : Optional[int] = images[0, 255:258, 383:386, -1] assert images.shape == (1, 512, 768, 3) __lowercase : Optional[int] = np.array([0.4909, 0.5059, 0.5372, 0.4623, 0.4876, 0.5049, 0.4820, 0.4956, 0.5019] ) # TODO: lower the tolerance after finding the cause of onnxruntime reproducibility issues assert np.abs(image_slice.flatten() - expected_slice ).max() < 2E-2 def lowerCAmelCase ( self : List[Any] ) -> Optional[Any]: """simple docstring""" __lowercase : Union[str, Any] = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/img2img/sketch-mountains-input.jpg""" ) __lowercase : Any = init_image.resize((768, 512) ) __lowercase : int = LMSDiscreteScheduler.from_pretrained( """runwayml/stable-diffusion-v1-5""" , subfolder="""scheduler""" , revision="""onnx""" ) __lowercase : str = OnnxStableDiffusionImgaImgPipeline.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 , ) pipe.set_progress_bar_config(disable=__a ) __lowercase : Union[str, Any] = """A fantasy landscape, trending on artstation""" __lowercase : Any = np.random.RandomState(0 ) __lowercase : Optional[Any] = pipe( prompt=__a , image=__a , strength=0.75 , guidance_scale=7.5 , num_inference_steps=20 , generator=__a , output_type="""np""" , ) __lowercase : str = output.images __lowercase : Dict = images[0, 255:258, 383:386, -1] assert images.shape == (1, 512, 768, 3) __lowercase : Union[str, Any] = np.array([0.8043, 0.926, 0.9581, 0.8119, 0.8954, 0.913, 0.7209, 0.7463, 0.7431] ) # TODO: lower the tolerance after finding the cause of onnxruntime reproducibility issues assert np.abs(image_slice.flatten() - expected_slice ).max() < 2E-2
233
0
"""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''' __UpperCAmelCase : List[str] __UpperCAmelCase : Optional[str] =None # Automatically constructed __UpperCAmelCase : ClassVar[str] ="dict" __UpperCAmelCase : ClassVar[Any] =None __UpperCAmelCase : str =field(default="""Translation""" ,init=__SCREAMING_SNAKE_CASE ,repr=__SCREAMING_SNAKE_CASE ) def __call__( self ): return pa.struct({lang: pa.string() for lang in sorted(self.languages )} ) def snake_case ( self ): from .features import Value return {k: Value("string" ) for k in sorted(self.languages )} @dataclass class _UpperCamelCase : '''simple docstring''' __UpperCAmelCase : Optional[List] =None __UpperCAmelCase : Optional[int] =None __UpperCAmelCase : Optional[str] =None # Automatically constructed __UpperCAmelCase : ClassVar[str] ="dict" __UpperCAmelCase : ClassVar[Any] =None __UpperCAmelCase : str =field(default="""TranslationVariableLanguages""" ,init=__SCREAMING_SNAKE_CASE ,repr=__SCREAMING_SNAKE_CASE ) def snake_case ( self ): __lowerCAmelCase = sorted(set(self.languages ) ) if self.languages else None __lowerCAmelCase = len(self.languages ) if self.languages else None def __call__( self ): return pa.struct({"language": pa.list_(pa.string() ), "translation": pa.list_(pa.string() )} ) def snake_case ( self , __a ): __lowerCAmelCase = set(self.languages ) if self.languages and set(__UpperCAmelCase ) - lang_set: raise ValueError( f"Some languages in example ({', '.join(sorted(set(__UpperCAmelCase ) - lang_set ) )}) are not in valid set ({', '.join(__UpperCAmelCase )})." ) # Convert dictionary into tuples, splitting out cases where there are # multiple translations for a single language. __lowerCAmelCase = [] for lang, text in translation_dict.items(): if isinstance(__UpperCAmelCase , __UpperCAmelCase ): translation_tuples.append((lang, text) ) else: translation_tuples.extend([(lang, el) for el in text] ) # Ensure translations are in ascending order by language code. __lowerCAmelCase = zip(*sorted(__UpperCAmelCase ) ) return {"language": languages, "translation": translations} def snake_case ( self ): from .features import Sequence, Value return { "language": Sequence(Value("string" ) ), "translation": Sequence(Value("string" ) ), }
366
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available A : str = { "configuration_timesformer": ["TIMESFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", "TimesformerConfig"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Any = [ "TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "TimesformerModel", "TimesformerForVideoClassification", "TimesformerPreTrainedModel", ] if TYPE_CHECKING: from .configuration_timesformer import TIMESFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TimesformerConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_timesformer import ( TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TimesformerForVideoClassification, TimesformerModel, TimesformerPreTrainedModel, ) else: import sys A : Union[str, Any] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
259
0
# Copyright 2023 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from ..models.whisper import WhisperForConditionalGeneration, WhisperProcessor from .base import PipelineTool class _SCREAMING_SNAKE_CASE ( _a ): snake_case__ : Any = """openai/whisper-base""" snake_case__ : Optional[int] = ( """This is a tool that transcribes an audio into text. It takes an input named `audio` and returns the """ """transcribed text.""" ) snake_case__ : Any = """transcriber""" snake_case__ : Optional[int] = WhisperProcessor snake_case__ : str = WhisperForConditionalGeneration snake_case__ : Optional[Any] = ["""audio"""] snake_case__ : Any = ["""text"""] def _A ( self : str , __lowerCamelCase : Dict ): return self.pre_processor(__lowerCamelCase , return_tensors="""pt""" ).input_features def _A ( self : Dict , __lowerCamelCase : List[Any] ): return self.model.generate(inputs=__lowerCamelCase ) def _A ( self : Any , __lowerCamelCase : Optional[Any] ): return self.pre_processor.batch_decode(__lowerCamelCase , skip_special_tokens=__lowerCamelCase )[0]
38
"""simple docstring""" import argparse import logging import pickle import random import time import numpy as np from transformers import BertTokenizer, GPTaTokenizer, RobertaTokenizer logging.basicConfig( format="%(asctime)s - %(levelname)s - %(name)s - %(message)s", datefmt="%m/%d/%Y %H:%M:%S", level=logging.INFO ) lowerCamelCase_ = logging.getLogger(__name__) def __lowerCamelCase ( ) -> int: __SCREAMING_SNAKE_CASE :Union[str, Any] = argparse.ArgumentParser( description='''Preprocess the data to avoid re-doing it several times by (tokenization + token_to_ids).''' ) parser.add_argument('''--file_path''' , type=a_ , default='''data/dump.txt''' , help='''The path to the data.''' ) parser.add_argument('''--tokenizer_type''' , type=a_ , default='''bert''' , choices=['''bert''', '''roberta''', '''gpt2'''] ) parser.add_argument('''--tokenizer_name''' , type=a_ , default='''bert-base-uncased''' , help='''The tokenizer to use.''' ) parser.add_argument('''--dump_file''' , type=a_ , default='''data/dump''' , help='''The dump file prefix.''' ) __SCREAMING_SNAKE_CASE :Any = parser.parse_args() logger.info(f'''Loading Tokenizer ({args.tokenizer_name})''' ) if args.tokenizer_type == "bert": __SCREAMING_SNAKE_CASE :Union[str, Any] = BertTokenizer.from_pretrained(args.tokenizer_name ) __SCREAMING_SNAKE_CASE :Optional[int] = tokenizer.special_tokens_map['''cls_token'''] # `[CLS]` __SCREAMING_SNAKE_CASE :Union[str, Any] = tokenizer.special_tokens_map['''sep_token'''] # `[SEP]` elif args.tokenizer_type == "roberta": __SCREAMING_SNAKE_CASE :str = RobertaTokenizer.from_pretrained(args.tokenizer_name ) __SCREAMING_SNAKE_CASE :str = tokenizer.special_tokens_map['''cls_token'''] # `<s>` __SCREAMING_SNAKE_CASE :str = tokenizer.special_tokens_map['''sep_token'''] # `</s>` elif args.tokenizer_type == "gpt2": __SCREAMING_SNAKE_CASE :Union[str, Any] = GPTaTokenizer.from_pretrained(args.tokenizer_name ) __SCREAMING_SNAKE_CASE :Optional[int] = tokenizer.special_tokens_map['''bos_token'''] # `<|endoftext|>` __SCREAMING_SNAKE_CASE :str = tokenizer.special_tokens_map['''eos_token'''] # `<|endoftext|>` logger.info(f'''Loading text from {args.file_path}''' ) with open(args.file_path , '''r''' , encoding='''utf8''' ) as fp: __SCREAMING_SNAKE_CASE :Union[str, Any] = fp.readlines() logger.info('''Start encoding''' ) logger.info(f'''{len(a_ )} examples to process.''' ) __SCREAMING_SNAKE_CASE :Optional[int] = [] __SCREAMING_SNAKE_CASE :List[str] = 0 __SCREAMING_SNAKE_CASE :Optional[Any] = 1_00_00 __SCREAMING_SNAKE_CASE :List[Any] = time.time() for text in data: __SCREAMING_SNAKE_CASE :Any = f'''{bos} {text.strip()} {sep}''' __SCREAMING_SNAKE_CASE :int = tokenizer.encode(a_ , add_special_tokens=a_ ) rslt.append(a_ ) iter += 1 if iter % interval == 0: __SCREAMING_SNAKE_CASE :Any = time.time() logger.info(f'''{iter} examples processed. - {(end-start):.2f}s/{interval}expl''' ) __SCREAMING_SNAKE_CASE :Any = time.time() logger.info('''Finished binarization''' ) logger.info(f'''{len(a_ )} examples processed.''' ) __SCREAMING_SNAKE_CASE :Optional[int] = f'''{args.dump_file}.{args.tokenizer_name}.pickle''' __SCREAMING_SNAKE_CASE :str = tokenizer.vocab_size if vocab_size < (1 << 16): __SCREAMING_SNAKE_CASE :Union[str, Any] = [np.uintaa(a_ ) for d in rslt] else: __SCREAMING_SNAKE_CASE :List[Any] = [np.intaa(a_ ) for d in rslt] random.shuffle(rslt_ ) logger.info(f'''Dump to {dp_file}''' ) with open(a_ , '''wb''' ) as handle: pickle.dump(rslt_ , a_ , protocol=pickle.HIGHEST_PROTOCOL ) if __name__ == "__main__": main()
191
0
'''simple docstring''' import torch from diffusers import DDIMParallelScheduler from .test_schedulers import SchedulerCommonTest class UpperCamelCase__( lowerCAmelCase ): __magic_name__ : Optional[int] = (DDIMParallelScheduler,) __magic_name__ : Any = (("eta", 0.0), ("num_inference_steps", 50)) def a__( self : List[Any] , **lowerCAmelCase : Optional[int] )-> Dict: """simple docstring""" UpperCAmelCase = { '''num_train_timesteps''': 1000, '''beta_start''': 0.0001, '''beta_end''': 0.02, '''beta_schedule''': '''linear''', '''clip_sample''': True, } config.update(**lowerCAmelCase ) return config def a__( self : Union[str, Any] , **lowerCAmelCase : Optional[Any] )-> Tuple: """simple docstring""" UpperCAmelCase = self.scheduler_classes[0] UpperCAmelCase = self.get_scheduler_config(**lowerCAmelCase ) UpperCAmelCase = scheduler_class(**lowerCAmelCase ) UpperCAmelCase , UpperCAmelCase = 10, 0.0 UpperCAmelCase = self.dummy_model() UpperCAmelCase = self.dummy_sample_deter scheduler.set_timesteps(lowerCAmelCase ) for t in scheduler.timesteps: UpperCAmelCase = model(lowerCAmelCase , lowerCAmelCase ) UpperCAmelCase = scheduler.step(lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase ).prev_sample return sample def a__( self : str )-> str: """simple docstring""" for timesteps in [100, 500, 1000]: self.check_over_configs(num_train_timesteps=lowerCAmelCase ) def a__( self : Optional[int] )-> List[Any]: """simple docstring""" for steps_offset in [0, 1]: self.check_over_configs(steps_offset=lowerCAmelCase ) UpperCAmelCase = self.scheduler_classes[0] UpperCAmelCase = self.get_scheduler_config(steps_offset=1 ) UpperCAmelCase = scheduler_class(**lowerCAmelCase ) scheduler.set_timesteps(5 ) assert torch.equal(scheduler.timesteps , torch.LongTensor([801, 601, 401, 201, 1] ) ) def a__( self : Union[str, Any] )-> Optional[Any]: """simple docstring""" for beta_start, beta_end in zip([0.0001, 0.001, 0.01, 0.1] , [0.002, 0.02, 0.2, 2] ): self.check_over_configs(beta_start=lowerCAmelCase , beta_end=lowerCAmelCase ) def a__( self : str )-> List[Any]: """simple docstring""" for schedule in ["linear", "squaredcos_cap_v2"]: self.check_over_configs(beta_schedule=lowerCAmelCase ) def a__( self : int )-> str: """simple docstring""" for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=lowerCAmelCase ) def a__( self : List[Any] )-> Any: """simple docstring""" for clip_sample in [True, False]: self.check_over_configs(clip_sample=lowerCAmelCase ) def a__( self : Union[str, Any] )-> List[str]: """simple docstring""" for timestep_spacing in ["trailing", "leading"]: self.check_over_configs(timestep_spacing=lowerCAmelCase ) def a__( self : int )-> Tuple: """simple docstring""" for rescale_betas_zero_snr in [True, False]: self.check_over_configs(rescale_betas_zero_snr=lowerCAmelCase ) def a__( self : int )-> Tuple: """simple docstring""" self.check_over_configs(thresholding=lowerCAmelCase ) for threshold in [0.5, 1.0, 2.0]: for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs( thresholding=lowerCAmelCase , prediction_type=lowerCAmelCase , sample_max_value=lowerCAmelCase , ) def a__( self : Union[str, Any] )-> Tuple: """simple docstring""" for t in [1, 10, 49]: self.check_over_forward(time_step=lowerCAmelCase ) def a__( self : Tuple )-> Dict: """simple docstring""" for t, num_inference_steps in zip([1, 10, 50] , [10, 50, 500] ): self.check_over_forward(time_step=lowerCAmelCase , num_inference_steps=lowerCAmelCase ) def a__( self : Dict )-> Tuple: """simple docstring""" for t, eta in zip([1, 10, 49] , [0.0, 0.5, 1.0] ): self.check_over_forward(time_step=lowerCAmelCase , eta=lowerCAmelCase ) def a__( self : Optional[int] )-> Optional[int]: """simple docstring""" UpperCAmelCase = self.scheduler_classes[0] UpperCAmelCase = self.get_scheduler_config() UpperCAmelCase = scheduler_class(**lowerCAmelCase ) assert torch.sum(torch.abs(scheduler._get_variance(0 , 0 ) - 0.0 ) ) < 1E-5 assert torch.sum(torch.abs(scheduler._get_variance(420 , 400 ) - 0.14771 ) ) < 1E-5 assert torch.sum(torch.abs(scheduler._get_variance(980 , 960 ) - 0.32460 ) ) < 1E-5 assert torch.sum(torch.abs(scheduler._get_variance(0 , 0 ) - 0.0 ) ) < 1E-5 assert torch.sum(torch.abs(scheduler._get_variance(487 , 486 ) - 0.00979 ) ) < 1E-5 assert torch.sum(torch.abs(scheduler._get_variance(999 , 998 ) - 0.02 ) ) < 1E-5 def a__( self : str )-> List[str]: """simple docstring""" UpperCAmelCase = self.scheduler_classes[0] UpperCAmelCase = self.get_scheduler_config() UpperCAmelCase = scheduler_class(**lowerCAmelCase ) UpperCAmelCase , UpperCAmelCase = 10, 0.0 scheduler.set_timesteps(lowerCAmelCase ) UpperCAmelCase = self.dummy_model() UpperCAmelCase = self.dummy_sample_deter UpperCAmelCase = self.dummy_sample_deter + 0.1 UpperCAmelCase = self.dummy_sample_deter - 0.1 UpperCAmelCase = samplea.shape[0] UpperCAmelCase = torch.stack([samplea, samplea, samplea] , dim=0 ) UpperCAmelCase = torch.arange(lowerCAmelCase )[0:3, None].repeat(1 , lowerCAmelCase ) UpperCAmelCase = model(samples.flatten(0 , 1 ) , timesteps.flatten(0 , 1 ) ) UpperCAmelCase = scheduler.batch_step_no_noise(lowerCAmelCase , timesteps.flatten(0 , 1 ) , samples.flatten(0 , 1 ) , lowerCAmelCase ) UpperCAmelCase = torch.sum(torch.abs(lowerCAmelCase ) ) UpperCAmelCase = torch.mean(torch.abs(lowerCAmelCase ) ) assert abs(result_sum.item() - 1147.7904 ) < 1E-2 assert abs(result_mean.item() - 0.4982 ) < 1E-3 def a__( self : Union[str, Any] )-> Optional[Any]: """simple docstring""" UpperCAmelCase = self.full_loop() UpperCAmelCase = torch.sum(torch.abs(lowerCAmelCase ) ) UpperCAmelCase = torch.mean(torch.abs(lowerCAmelCase ) ) assert abs(result_sum.item() - 172.0067 ) < 1E-2 assert abs(result_mean.item() - 0.223967 ) < 1E-3 def a__( self : Optional[Any] )-> Dict: """simple docstring""" UpperCAmelCase = self.full_loop(prediction_type='''v_prediction''' ) UpperCAmelCase = torch.sum(torch.abs(lowerCAmelCase ) ) UpperCAmelCase = torch.mean(torch.abs(lowerCAmelCase ) ) assert abs(result_sum.item() - 52.5302 ) < 1E-2 assert abs(result_mean.item() - 0.0684 ) < 1E-3 def a__( self : Dict )-> Any: """simple docstring""" UpperCAmelCase = self.full_loop(set_alpha_to_one=lowerCAmelCase , beta_start=0.01 ) UpperCAmelCase = torch.sum(torch.abs(lowerCAmelCase ) ) UpperCAmelCase = torch.mean(torch.abs(lowerCAmelCase ) ) assert abs(result_sum.item() - 149.8295 ) < 1E-2 assert abs(result_mean.item() - 0.1951 ) < 1E-3 def a__( self : Optional[int] )-> int: """simple docstring""" UpperCAmelCase = self.full_loop(set_alpha_to_one=lowerCAmelCase , beta_start=0.01 ) UpperCAmelCase = torch.sum(torch.abs(lowerCAmelCase ) ) UpperCAmelCase = torch.mean(torch.abs(lowerCAmelCase ) ) assert abs(result_sum.item() - 149.0784 ) < 1E-2 assert abs(result_mean.item() - 0.1941 ) < 1E-3
91
'''simple docstring''' import numpy as np from cva import COLOR_BGR2GRAY, CV_8UC3, cvtColor, filteraD, imread, imshow, waitKey def lowerCamelCase__ ( A : int , A : int , A : int , A : int , A : int , A : int ): '''simple docstring''' if (ksize % 2) == 0: UpperCAmelCase = ksize + 1 UpperCAmelCase = np.zeros((ksize, ksize) , dtype=np.floataa ) # each value for y in range(A ): for x in range(A ): # distance from center UpperCAmelCase = x - ksize // 2 UpperCAmelCase = y - ksize // 2 # degree to radiant UpperCAmelCase = theta / 1_80 * np.pi UpperCAmelCase = np.cos(_theta ) UpperCAmelCase = np.sin(_theta ) # get kernel x UpperCAmelCase = cos_theta * px + sin_theta * py # get kernel y UpperCAmelCase = -sin_theta * px + cos_theta * py # fill kernel UpperCAmelCase = np.exp( -(_x**2 + gamma**2 * _y**2) / (2 * sigma**2) ) * np.cos(2 * np.pi * _x / lambd + psi ) return gabor if __name__ == "__main__": import doctest doctest.testmod() # read original image _lowercase : Tuple = imread("""../image_data/lena.jpg""") # turn image in gray scale value _lowercase : int = cvtColor(img, COLOR_BGR2GRAY) # Apply multiple Kernel to detect edges _lowercase : List[str] = np.zeros(gray.shape[:2]) for theta in [0, 30, 60, 90, 120, 150]: _lowercase : List[Any] = gabor_filter_kernel(10, 8, theta, 10, 0, 0) out += filteraD(gray, CV_8UC3, kernel_aa) _lowercase : Optional[int] = out / out.max() * 255 _lowercase : Optional[int] = out.astype(np.uinta) imshow("""Original""", gray) imshow("""Gabor filter with 20x20 mask and 6 directions""", out) waitKey(0)
91
1
"""simple docstring""" def _snake_case ( lowercase__ ): stooge(lowercase__ , 0 , len(lowercase__ ) - 1 ) return arr def _snake_case ( lowercase__ , lowercase__ , lowercase__ ): if i >= h: return # If first element is smaller than the last then swap them if arr[i] > arr[h]: _lowerCamelCase, _lowerCamelCase : Optional[Any] = arr[h], arr[i] # If there are more than 2 elements in the array if h - i + 1 > 2: _lowerCamelCase : Union[str, Any] = (int)((h - i + 1) / 3 ) # Recursively sort first 2/3 elements stooge(lowercase__ , lowercase__ , (h - t) ) # Recursively sort last 2/3 elements stooge(lowercase__ , i + t , (lowercase__) ) # Recursively sort first 2/3 elements stooge(lowercase__ , lowercase__ , (h - t) ) if __name__ == "__main__": lowercase__ = input("""Enter numbers separated by a comma:\n""").strip() lowercase__ = [int(item) for item in user_input.split(""",""")] print(stooge_sort(unsorted))
96
lowerCamelCase : Optional[Any] = '''ABCDEFGHIJKLMNOPQRSTUVWXYZ''' def snake_case_ ( ): __lowercase : List[str] = input("""Enter message: """ ) __lowercase : int = input("""Enter key [alphanumeric]: """ ) __lowercase : Optional[Any] = input("""Encrypt/Decrypt [e/d]: """ ) if mode.lower().startswith("""e""" ): __lowercase : Optional[int] = """encrypt""" __lowercase : Dict = encrypt_message(lowerCAmelCase_ , lowerCAmelCase_ ) elif mode.lower().startswith("""d""" ): __lowercase : Union[str, Any] = """decrypt""" __lowercase : Optional[int] = decrypt_message(lowerCAmelCase_ , lowerCAmelCase_ ) print(F"\n{mode.title()}ed message:" ) print(lowerCAmelCase_ ) def snake_case_ ( lowerCAmelCase_ : str , lowerCAmelCase_ : str ): return translate_message(lowerCAmelCase_ , lowerCAmelCase_ , """encrypt""" ) def snake_case_ ( lowerCAmelCase_ : str , lowerCAmelCase_ : str ): return translate_message(lowerCAmelCase_ , lowerCAmelCase_ , """decrypt""" ) def snake_case_ ( lowerCAmelCase_ : str , lowerCAmelCase_ : str , lowerCAmelCase_ : str ): __lowercase : Union[str, Any] = [] __lowercase : Tuple = 0 __lowercase : Dict = key.upper() for symbol in message: __lowercase : Optional[Any] = LETTERS.find(symbol.upper() ) if num != -1: if mode == "encrypt": num += LETTERS.find(key[key_index] ) elif mode == "decrypt": num -= LETTERS.find(key[key_index] ) num %= len(lowerCAmelCase_ ) if symbol.isupper(): translated.append(LETTERS[num] ) elif symbol.islower(): translated.append(LETTERS[num].lower() ) key_index += 1 if key_index == len(lowerCAmelCase_ ): __lowercase : str = 0 else: translated.append(lowerCAmelCase_ ) return "".join(lowerCAmelCase_ ) if __name__ == "__main__": main()
233
0
"""simple docstring""" from __future__ import annotations import math def lowerCamelCase_ (UpperCamelCase__ : int ): if 1 < number < 4: # 2 and 3 are primes return True elif number < 2 or number % 2 == 0 or number % 3 == 0: # Negatives, 0, 1, all even numbers, all multiples of 3 are not primes return False # All primes number are in format of 6k +/- 1 for i in range(5 , int(math.sqrt(_lowerCAmelCase ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True _lowerCAmelCase :Tuple = [num for num in range(3, 100_001, 2) if not is_prime(num)] def lowerCamelCase_ (UpperCamelCase__ : int ): if not isinstance(_lowerCAmelCase , _lowerCAmelCase ): raise ValueError('''n must be an integer''' ) if n <= 0: raise ValueError('''n must be >= 0''' ) _UpperCAmelCase : str = [] for num in range(len(_lowerCAmelCase ) ): _UpperCAmelCase : Optional[int] = 0 while 2 * i * i <= odd_composites[num]: _UpperCAmelCase : Optional[Any] = odd_composites[num] - 2 * i * i if is_prime(_lowerCAmelCase ): break i += 1 else: list_nums.append(odd_composites[num] ) if len(_lowerCAmelCase ) == n: return list_nums return [] def lowerCamelCase_ (): return compute_nums(1 )[0] if __name__ == "__main__": print(f"{solution() = }")
353
"""simple docstring""" _lowerCAmelCase :List[str] = 0 # The first color of the flag. _lowerCAmelCase :Optional[Any] = 1 # The second color of the flag. _lowerCAmelCase :List[Any] = 2 # The third color of the flag. _lowerCAmelCase :Any = (red, white, blue) def lowerCamelCase_ (UpperCamelCase__ : list ): if not sequence: return [] if len(UpperCamelCase__ ) == 1: return list(UpperCamelCase__ ) _UpperCAmelCase : List[Any] = 0 _UpperCAmelCase : str = len(UpperCamelCase__ ) - 1 _UpperCAmelCase : Optional[int] = 0 while mid <= high: if sequence[mid] == colors[0]: _UpperCAmelCase , _UpperCAmelCase : Dict = sequence[mid], sequence[low] low += 1 mid += 1 elif sequence[mid] == colors[1]: mid += 1 elif sequence[mid] == colors[2]: _UpperCAmelCase , _UpperCAmelCase : List[Any] = sequence[high], sequence[mid] high -= 1 else: _UpperCAmelCase : Tuple = F'The elements inside the sequence must contains only {colors} values' raise ValueError(UpperCamelCase__ ) return sequence if __name__ == "__main__": import doctest doctest.testmod() _lowerCAmelCase :Tuple = input('Enter numbers separated by commas:\n').strip() _lowerCAmelCase :Dict = [int(item.strip()) for item in user_input.split(',')] print(f"{dutch_national_flag_sort(unsorted)}")
68
0
from typing import Dict, List from nltk.translate import gleu_score import datasets from datasets import MetricInfo _UpperCAmelCase : Dict ='\\n@misc{wu2016googles,\n title={Google\'s Neural Machine Translation System: Bridging the Gap between Human and Machine Translation},\n author={Yonghui Wu and Mike Schuster and Zhifeng Chen and Quoc V. Le and Mohammad Norouzi and Wolfgang Macherey\n and Maxim Krikun and Yuan Cao and Qin Gao and Klaus Macherey and Jeff Klingner and Apurva Shah and Melvin\n Johnson and Xiaobing Liu and Łukasz Kaiser and Stephan Gouws and Yoshikiyo Kato and Taku Kudo and Hideto\n Kazawa and Keith Stevens and George Kurian and Nishant Patil and Wei Wang and Cliff Young and\n Jason Smith and Jason Riesa and Alex Rudnick and Oriol Vinyals and Greg Corrado and Macduff Hughes\n and Jeffrey Dean},\n year={2016},\n eprint={1609.08144},\n archivePrefix={arXiv},\n primaryClass={cs.CL}\n}\n' _UpperCAmelCase : Any ='\\nThe BLEU score has some undesirable properties when used for single\nsentences, as it was designed to be a corpus measure. We therefore\nuse a slightly different score for our RL experiments which we call\nthe \'GLEU score\'. For the GLEU score, we record all sub-sequences of\n1, 2, 3 or 4 tokens in output and target sequence (n-grams). We then\ncompute a recall, which is the ratio of the number of matching n-grams\nto the number of total n-grams in the target (ground truth) sequence,\nand a precision, which is the ratio of the number of matching n-grams\nto the number of total n-grams in the generated output sequence. Then\nGLEU score is simply the minimum of recall and precision. This GLEU\nscore\'s range is always between 0 (no matches) and 1 (all match) and\nit is symmetrical when switching output and target. According to\nour experiments, GLEU score correlates quite well with the BLEU\nmetric on a corpus level but does not have its drawbacks for our per\nsentence reward objective.\n' _UpperCAmelCase : str ='\\nComputes corpus-level Google BLEU (GLEU) score of translated segments against one or more references.\nInstead of averaging the sentence level GLEU scores (i.e. macro-average precision), Wu et al. (2016) sum up the matching\ntokens and the max of hypothesis and reference tokens for each sentence, then compute using the aggregate values.\n\nArgs:\n predictions (list of str): list of translations to score.\n Each translation should be tokenized into a list of tokens.\n references (list of list of str): list of lists of references for each translation.\n Each reference should be tokenized into a list of tokens.\n min_len (int): The minimum order of n-gram this function should extract. Defaults to 1.\n max_len (int): The maximum order of n-gram this function should extract. Defaults to 4.\n\nReturns:\n \'google_bleu\': google_bleu score\n\nExamples:\n Example 1:\n >>> hyp1 = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'which\',\n ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'always\',\n ... \'disobeys\', \'the\', \'commands\', \'of\', \'the\', \'cat\']\n >>> ref1a = [\'It\', \'is\', \'the\', \'guiding\', \'principle\', \'which\',\n ... \'guarantees\', \'the\', \'rubber\', \'duck\', \'forces\', \'never\',\n ... \'being\', \'under\', \'the\', \'command\', \'of\', \'the\', \'cat\']\n\n >>> hyp2 = [\'he\', \'read\', \'the\', \'book\', \'because\', \'he\', \'was\',\n ... \'interested\', \'in\', \'world\', \'history\']\n >>> ref2a = [\'he\', \'was\', \'interested\', \'in\', \'world\', \'history\',\n ... \'because\', \'he\', \'read\', \'the\', \'book\']\n\n >>> list_of_references = [[ref1a], [ref2a]]\n >>> hypotheses = [hyp1, hyp2]\n >>> google_bleu = datasets.load_metric("google_bleu")\n >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references)\n >>> print(round(results["google_bleu"], 2))\n 0.44\n\n Example 2:\n >>> hyp1 = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'which\',\n ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'always\',\n ... \'disobeys\', \'the\', \'commands\', \'of\', \'the\', \'cat\']\n >>> ref1a = [\'It\', \'is\', \'the\', \'guiding\', \'principle\', \'which\',\n ... \'guarantees\', \'the\', \'rubber\', \'duck\', \'forces\', \'never\',\n ... \'being\', \'under\', \'the\', \'command\', \'of\', \'the\', \'cat\']\n >>> ref1b = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'that\',\n ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'will\', \'never\',\n ... \'heed\', \'the\', \'cat\', \'commands\']\n >>> ref1c = [\'It\', \'is\', \'the\', \'practical\', \'guide\', \'for\', \'the\',\n ... \'rubber\', \'duck\', \'army\', \'never\', \'to\', \'heed\', \'the\', \'directions\',\n ... \'of\', \'the\', \'cat\']\n\n >>> hyp2 = [\'he\', \'read\', \'the\', \'book\', \'because\', \'he\', \'was\',\n ... \'interested\', \'in\', \'world\', \'history\']\n >>> ref2a = [\'he\', \'was\', \'interested\', \'in\', \'world\', \'history\',\n ... \'because\', \'he\', \'read\', \'the\', \'book\']\n\n >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]]\n >>> hypotheses = [hyp1, hyp2]\n >>> google_bleu = datasets.load_metric("google_bleu")\n >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references)\n >>> print(round(results["google_bleu"], 2))\n 0.61\n\n Example 3:\n >>> hyp1 = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'which\',\n ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'always\',\n ... \'disobeys\', \'the\', \'commands\', \'of\', \'the\', \'cat\']\n >>> ref1a = [\'It\', \'is\', \'the\', \'guiding\', \'principle\', \'which\',\n ... \'guarantees\', \'the\', \'rubber\', \'duck\', \'forces\', \'never\',\n ... \'being\', \'under\', \'the\', \'command\', \'of\', \'the\', \'cat\']\n >>> ref1b = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'that\',\n ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'will\', \'never\',\n ... \'heed\', \'the\', \'cat\', \'commands\']\n >>> ref1c = [\'It\', \'is\', \'the\', \'practical\', \'guide\', \'for\', \'the\',\n ... \'rubber\', \'duck\', \'army\', \'never\', \'to\', \'heed\', \'the\', \'directions\',\n ... \'of\', \'the\', \'cat\']\n\n >>> hyp2 = [\'he\', \'read\', \'the\', \'book\', \'because\', \'he\', \'was\',\n ... \'interested\', \'in\', \'world\', \'history\']\n >>> ref2a = [\'he\', \'was\', \'interested\', \'in\', \'world\', \'history\',\n ... \'because\', \'he\', \'read\', \'the\', \'book\']\n\n >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]]\n >>> hypotheses = [hyp1, hyp2]\n >>> google_bleu = datasets.load_metric("google_bleu")\n >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references, min_len=2)\n >>> print(round(results["google_bleu"], 2))\n 0.53\n\n Example 4:\n >>> hyp1 = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'which\',\n ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'always\',\n ... \'disobeys\', \'the\', \'commands\', \'of\', \'the\', \'cat\']\n >>> ref1a = [\'It\', \'is\', \'the\', \'guiding\', \'principle\', \'which\',\n ... \'guarantees\', \'the\', \'rubber\', \'duck\', \'forces\', \'never\',\n ... \'being\', \'under\', \'the\', \'command\', \'of\', \'the\', \'cat\']\n >>> ref1b = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'that\',\n ... \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'will\', \'never\',\n ... \'heed\', \'the\', \'cat\', \'commands\']\n >>> ref1c = [\'It\', \'is\', \'the\', \'practical\', \'guide\', \'for\', \'the\',\n ... \'rubber\', \'duck\', \'army\', \'never\', \'to\', \'heed\', \'the\', \'directions\',\n ... \'of\', \'the\', \'cat\']\n\n >>> hyp2 = [\'he\', \'read\', \'the\', \'book\', \'because\', \'he\', \'was\',\n ... \'interested\', \'in\', \'world\', \'history\']\n >>> ref2a = [\'he\', \'was\', \'interested\', \'in\', \'world\', \'history\',\n ... \'because\', \'he\', \'read\', \'the\', \'book\']\n\n >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]]\n >>> hypotheses = [hyp1, hyp2]\n >>> google_bleu = datasets.load_metric("google_bleu")\n >>> results = google_bleu.compute(predictions=hypotheses,references=list_of_references, min_len=2, max_len=6)\n >>> print(round(results["google_bleu"], 2))\n 0.4\n' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION ) class snake_case__( datasets.Metric ): '''simple docstring''' def lowercase_ ( self ) -> int: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Sequence(datasets.Value('''string''' , id='''token''' ) , id='''sequence''' ), '''references''': datasets.Sequence( datasets.Sequence(datasets.Value('''string''' , id='''token''' ) , id='''sequence''' ) , id='''references''' ), } ) , ) def lowercase_ ( self , __lowercase , __lowercase , __lowercase = 1 , __lowercase = 4 , ) -> Optional[Any]: return { "google_bleu": gleu_score.corpus_gleu( list_of_references=_SCREAMING_SNAKE_CASE , hypotheses=_SCREAMING_SNAKE_CASE , min_len=_SCREAMING_SNAKE_CASE , max_len=_SCREAMING_SNAKE_CASE ) }
262
from collections import Counter import numpy as np from sklearn import datasets from sklearn.model_selection import train_test_split lowerCAmelCase : List[Any] = datasets.load_iris() lowerCAmelCase : List[str] = np.array(data['data']) lowerCAmelCase : Any = np.array(data['target']) lowerCAmelCase : Dict = data['target_names'] lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase : str = train_test_split(X, y) def A_ ( a , a ): """simple docstring""" return np.linalg.norm(np.array(a ) - np.array(a ) ) def A_ ( a , a , a , a , a=5 ): """simple docstring""" SCREAMING_SNAKE_CASE_ : Optional[int] = zip(a , a ) # List of distances of all points from the point to be classified SCREAMING_SNAKE_CASE_ : List[Any] = [] for data_point in data: SCREAMING_SNAKE_CASE_ : Optional[int] = euclidean_distance(data_point[0] , a ) distances.append((distance, data_point[1]) ) # Choosing 'k' points with the least distances. SCREAMING_SNAKE_CASE_ : List[str] = [i[1] for i in sorted(a )[:k]] # Most commonly occurring class among them # is the class into which the point is classified SCREAMING_SNAKE_CASE_ : List[Any] = Counter(a ).most_common(1 )[0][0] return classes[result] if __name__ == "__main__": print(classifier(X_train, y_train, classes, [4.4, 3.1, 1.3, 1.4]))
253
0
from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCAmelCase : Optional[Any] = logging.get_logger(__name__) UpperCAmelCase : List[Any] = { """microsoft/swinv2-tiny-patch4-window8-256""": ( """https://huggingface.co/microsoft/swinv2-tiny-patch4-window8-256/resolve/main/config.json""" ), } class __lowerCAmelCase ( UpperCamelCase__): _lowercase : Optional[Any] = """swinv2""" _lowercase : str = { """num_attention_heads""": """num_heads""", """num_hidden_layers""": """num_layers""", } def __init__( self , lowerCAmelCase__=2_2_4 , lowerCAmelCase__=4 , lowerCAmelCase__=3 , lowerCAmelCase__=9_6 , lowerCAmelCase__=[2, 2, 6, 2] , lowerCAmelCase__=[3, 6, 1_2, 2_4] , lowerCAmelCase__=7 , lowerCAmelCase__=4.0 , lowerCAmelCase__=True , lowerCAmelCase__=0.0 , lowerCAmelCase__=0.0 , lowerCAmelCase__=0.1 , lowerCAmelCase__="gelu" , lowerCAmelCase__=False , lowerCAmelCase__=0.02 , lowerCAmelCase__=1E-5 , lowerCAmelCase__=3_2 , **lowerCAmelCase__ , ) -> List[str]: '''simple docstring''' super().__init__(**_SCREAMING_SNAKE_CASE ) a__ : Tuple =image_size a__ : Tuple =patch_size a__ : Dict =num_channels a__ : str =embed_dim a__ : Union[str, Any] =depths a__ : Dict =len(_SCREAMING_SNAKE_CASE ) a__ : List[Any] =num_heads a__ : Dict =window_size a__ : List[Any] =mlp_ratio a__ : Dict =qkv_bias a__ : Optional[Any] =hidden_dropout_prob a__ : Any =attention_probs_dropout_prob a__ : Tuple =drop_path_rate a__ : Any =hidden_act a__ : Any =use_absolute_embeddings a__ : Optional[int] =layer_norm_eps a__ : int =initializer_range a__ : str =encoder_stride # we set the hidden_size attribute in order to make Swinv2 work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model a__ : str =int(embed_dim * 2 ** (len(_SCREAMING_SNAKE_CASE ) - 1) ) a__ : List[Any] =(0, 0, 0, 0)
371
import os import time from dataclasses import dataclass, field from enum import Enum from typing import Dict, List, Optional, Union import torch from filelock import FileLock from torch.utils.data import Dataset from ...models.auto.modeling_auto import MODEL_FOR_QUESTION_ANSWERING_MAPPING from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging from ..processors.squad import SquadFeatures, SquadVaProcessor, SquadVaProcessor, squad_convert_examples_to_features UpperCAmelCase : Tuple = logging.get_logger(__name__) UpperCAmelCase : List[Any] = list(MODEL_FOR_QUESTION_ANSWERING_MAPPING.keys()) UpperCAmelCase : Dict = tuple(conf.model_type for conf in MODEL_CONFIG_CLASSES) @dataclass class __lowerCAmelCase : _lowercase : str = field( default=UpperCamelCase__ , metadata={"""help""": """Model type selected in the list: """ + """, """.join(UpperCamelCase__)}) _lowercase : str = field( default=UpperCamelCase__ , metadata={"""help""": """The input data dir. Should contain the .json files for the SQuAD task."""}) _lowercase : int = field( default=128 , metadata={ """help""": ( """The maximum total input sequence length after tokenization. Sequences longer """ """than this will be truncated, sequences shorter will be padded.""" ) } , ) _lowercase : int = field( default=128 , metadata={"""help""": """When splitting up a long document into chunks, how much stride to take between chunks."""} , ) _lowercase : int = field( default=64 , metadata={ """help""": ( """The maximum number of tokens for the question. Questions longer than this will """ """be truncated to this length.""" ) } , ) _lowercase : int = field( default=30 , metadata={ """help""": ( """The maximum length of an answer that can be generated. This is needed because the start """ """and end predictions are not conditioned on one another.""" ) } , ) _lowercase : bool = field( default=UpperCamelCase__ , metadata={"""help""": """Overwrite the cached training and evaluation sets"""}) _lowercase : bool = field( default=UpperCamelCase__ , metadata={"""help""": """If true, the SQuAD examples contain some that do not have an answer."""}) _lowercase : float = field( default=0.0 , metadata={"""help""": """If null_score - best_non_null is greater than the threshold predict null."""}) _lowercase : int = field( default=20 , metadata={"""help""": """If null_score - best_non_null is greater than the threshold predict null."""}) _lowercase : int = field( default=0 , metadata={ """help""": ( """language id of input for language-specific xlm models (see""" """ tokenization_xlm.PRETRAINED_INIT_CONFIGURATION)""" ) } , ) _lowercase : int = field(default=1 , metadata={"""help""": """multiple threads for converting example to features"""}) class __lowerCAmelCase ( UpperCamelCase__): _lowercase : List[Any] = """train""" _lowercase : Any = """dev""" class __lowerCAmelCase ( UpperCamelCase__): _lowercase : SquadDataTrainingArguments _lowercase : List[SquadFeatures] _lowercase : Split _lowercase : bool def __init__( self , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ = None , lowerCAmelCase__ = Split.train , lowerCAmelCase__ = False , lowerCAmelCase__ = None , lowerCAmelCase__ = "pt" , ) -> str: '''simple docstring''' a__ : List[Any] =args a__ : int =is_language_sensitive a__ : List[str] =SquadVaProcessor() if args.version_2_with_negative else SquadVaProcessor() if isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): try: a__ : str =Split[mode] except KeyError: raise KeyError("mode is not a valid split name" ) a__ : Any =mode # Load data features from cache or dataset file a__ : str ="v2" if args.version_2_with_negative else "v1" a__ : str =os.path.join( cache_dir if cache_dir is not None else args.data_dir , F'''cached_{mode.value}_{tokenizer.__class__.__name__}_{args.max_seq_length}_{version_tag}''' , ) # Make sure only the first process in distributed training processes the dataset, # and the others will use the cache. a__ : Dict =cached_features_file + ".lock" with FileLock(lowerCAmelCase__ ): if os.path.exists(lowerCAmelCase__ ) and not args.overwrite_cache: a__ : Any =time.time() a__ : List[Any] =torch.load(lowerCAmelCase__ ) # Legacy cache files have only features, while new cache files # will have dataset and examples also. a__ : str =self.old_features["features"] a__ : str =self.old_features.get("dataset" , lowerCAmelCase__ ) a__ : Optional[int] =self.old_features.get("examples" , lowerCAmelCase__ ) logger.info( F'''Loading features from cached file {cached_features_file} [took %.3f s]''' , time.time() - start ) if self.dataset is None or self.examples is None: logger.warning( F'''Deleting cached file {cached_features_file} will allow dataset and examples to be cached in''' " future run" ) else: if mode == Split.dev: a__ : Dict =self.processor.get_dev_examples(args.data_dir ) else: a__ : str =self.processor.get_train_examples(args.data_dir ) a__ , a__ : Union[str, Any] =squad_convert_examples_to_features( examples=self.examples , tokenizer=lowerCAmelCase__ , max_seq_length=args.max_seq_length , doc_stride=args.doc_stride , max_query_length=args.max_query_length , is_training=mode == Split.train , threads=args.threads , return_dataset=lowerCAmelCase__ , ) a__ : Any =time.time() torch.save( {"features": self.features, "dataset": self.dataset, "examples": self.examples} , lowerCAmelCase__ , ) # ^ This seems to take a lot of time so I want to investigate why and how we can improve. logger.info( F'''Saving features into cached file {cached_features_file} [took {time.time() - start:.3f} s]''' ) def __len__( self ) -> Tuple: '''simple docstring''' return len(self.features ) def __getitem__( self , lowerCAmelCase__ ) -> Dict[str, torch.Tensor]: '''simple docstring''' a__ : str =self.features[i] a__ : Optional[Any] =torch.tensor(feature.input_ids , dtype=torch.long ) a__ : List[Any] =torch.tensor(feature.attention_mask , dtype=torch.long ) a__ : int =torch.tensor(feature.token_type_ids , dtype=torch.long ) a__ : List[str] =torch.tensor(feature.cls_index , dtype=torch.long ) a__ : int =torch.tensor(feature.p_mask , dtype=torch.float ) a__ : Tuple =torch.tensor(feature.is_impossible , dtype=torch.float ) a__ : Tuple ={ "input_ids": input_ids, "attention_mask": attention_mask, "token_type_ids": token_type_ids, } if self.args.model_type in ["xlm", "roberta", "distilbert", "camembert"]: del inputs["token_type_ids"] if self.args.model_type in ["xlnet", "xlm"]: inputs.update({"cls_index": cls_index, "p_mask": p_mask} ) if self.args.version_2_with_negative: inputs.update({"is_impossible": is_impossible} ) if self.is_language_sensitive: inputs.update({"langs": (torch.ones(input_ids.shape , dtype=torch.intaa ) * self.args.lang_id)} ) if self.mode == Split.train: a__ : int =torch.tensor(feature.start_position , dtype=torch.long ) a__ : Any =torch.tensor(feature.end_position , dtype=torch.long ) inputs.update({"start_positions": start_positions, "end_positions": end_positions} ) return inputs
148
0
from __future__ import annotations from collections.abc import Iterable, Iterator from dataclasses import dataclass __lowerCAmelCase : Dict =(3, 9, -1_1, 0, 7, 5, 1, -1) __lowerCAmelCase : int =(4, 6, 2, 0, 8, 1_0, 3, -2) @dataclass class _lowercase : '''simple docstring''' SCREAMING_SNAKE_CASE__ : int SCREAMING_SNAKE_CASE__ : Node | None class _lowercase : '''simple docstring''' def __init__( self :str , lowerCAmelCase__ :Iterable[int] ) -> None: __SCREAMING_SNAKE_CASE : Node | None = None for i in sorted(lowerCAmelCase__ , reverse=lowerCAmelCase__ ): __SCREAMING_SNAKE_CASE : int = Node(lowerCAmelCase__ , self.head ) def __iter__( self :Optional[Any] ) -> Iterator[int]: __SCREAMING_SNAKE_CASE : Tuple = self.head while node: yield node.data __SCREAMING_SNAKE_CASE : int = node.next_node def __len__( self :Optional[int] ) -> int: return sum(1 for _ in self ) def __str__( self :List[Any] ) -> str: return " -> ".join([str(lowerCAmelCase__ ) for node in self] ) def _UpperCamelCase ( lowercase__ , lowercase__ ): return SortedLinkedList(list(lowercase__ ) + list(lowercase__ ) ) if __name__ == "__main__": import doctest doctest.testmod() __lowerCAmelCase : Union[str, Any] =SortedLinkedList print(merge_lists(SSL(test_data_odd), SSL(test_data_even)))
9
from json import JSONDecodeError # Workaround for requests.exceptions.JSONDecodeError import requests def _A ( SCREAMING_SNAKE_CASE__ : str = "isbn/0140328726" ): UpperCamelCase :Optional[int] = olid.strip().strip('''/''' ) # Remove leading/trailing whitespace & slashes if new_olid.count('''/''' ) != 1: UpperCamelCase :str = F'''{olid} is not a valid Open Library olid''' raise ValueError(SCREAMING_SNAKE_CASE__ ) return requests.get(F'''https://openlibrary.org/{new_olid}.json''' ).json() def _A ( SCREAMING_SNAKE_CASE__ : dict ): UpperCamelCase :str = { '''title''': '''Title''', '''publish_date''': '''Publish date''', '''authors''': '''Authors''', '''number_of_pages''': '''Number of pages:''', '''first_sentence''': '''First sentence''', '''isbn_10''': '''ISBN (10)''', '''isbn_13''': '''ISBN (13)''', } UpperCamelCase :Optional[Any] = {better_key: ol_book_data[key] for key, better_key in desired_keys.items()} UpperCamelCase :List[str] = [ get_openlibrary_data(author['''key'''] )['''name'''] for author in data['''Authors'''] ] UpperCamelCase :int = data['''First sentence''']['''value'''] for key, value in data.items(): if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): UpperCamelCase :List[str] = ''', '''.join(SCREAMING_SNAKE_CASE__ ) return data if __name__ == "__main__": import doctest doctest.testmod() while True: __snake_case = input("""\nEnter the ISBN code to search (or 'quit' to stop): """).strip() if isbn.lower() in ("", "q", "quit", "exit", "stop"): break if len(isbn) not in (10, 13) or not isbn.isdigit(): print(f'''Sorry, {isbn} is not a valid ISBN. Please, input a valid ISBN.''') continue print(f'''\nSearching Open Library for ISBN: {isbn}...\n''') try: __snake_case = summarize_book(get_openlibrary_data(f'''isbn/{isbn}''')) print("""\n""".join(f'''{key}: {value}''' for key, value in book_summary.items())) except JSONDecodeError: # Workaround for requests.exceptions.RequestException: print(f'''Sorry, there are no results for ISBN: {isbn}.''')
259
0
'''simple docstring''' import argparse import os import re # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/check_dummies.py _UpperCAmelCase : Any = """src/diffusers""" # Matches is_xxx_available() _UpperCAmelCase : Dict = re.compile(r"""is\_([a-z_]*)_available\(\)""") # Matches from xxx import bla _UpperCAmelCase : List[str] = re.compile(r"""\s+from\s+\S*\s+import\s+([^\(\s].*)\n""") _UpperCAmelCase : Tuple = """ {0} = None """ _UpperCAmelCase : Optional[Any] = """ class {0}(metaclass=DummyObject): _backends = {1} def __init__(self, *args, **kwargs): requires_backends(self, {1}) @classmethod def from_config(cls, *args, **kwargs): requires_backends(cls, {1}) @classmethod def from_pretrained(cls, *args, **kwargs): requires_backends(cls, {1}) """ _UpperCAmelCase : Optional[int] = """ def {0}(*args, **kwargs): requires_backends({0}, {1}) """ def __magic_name__( lowerCamelCase): __lowerCAmelCase = _re_backend.findall(lowerCamelCase) if len(lowerCamelCase) == 0: return None return "_and_".join(lowerCamelCase) def __magic_name__( ): with open(os.path.join(lowerCamelCase, '''__init__.py'''), '''r''', encoding='''utf-8''', newline='''\n''') as f: __lowerCAmelCase = f.readlines() # Get to the point we do the actual imports for type checking __lowerCAmelCase = 0 __lowerCAmelCase = {} # Go through the end of the file while line_index < len(lowerCamelCase): # If the line contains is_backend_available, we grab all objects associated with the `else` block __lowerCAmelCase = find_backend(lines[line_index]) if backend is not None: while not lines[line_index].startswith('''else:'''): line_index += 1 line_index += 1 __lowerCAmelCase = [] # Until we unindent, add backend objects to the list while line_index < len(lowerCamelCase) and len(lines[line_index]) > 1: __lowerCAmelCase = lines[line_index] __lowerCAmelCase = _re_single_line_import.search(lowerCamelCase) if single_line_import_search is not None: objects.extend(single_line_import_search.groups()[0].split(''', ''')) elif line.startswith(''' ''' * 8): objects.append(line[8:-2]) line_index += 1 if len(lowerCamelCase) > 0: __lowerCAmelCase = objects else: line_index += 1 return backend_specific_objects def __magic_name__( lowerCamelCase, lowerCamelCase): if name.isupper(): return DUMMY_CONSTANT.format(lowerCamelCase) elif name.islower(): return DUMMY_FUNCTION.format(lowerCamelCase, lowerCamelCase) else: return DUMMY_CLASS.format(lowerCamelCase, lowerCamelCase) def __magic_name__( lowerCamelCase=None): if backend_specific_objects is None: __lowerCAmelCase = read_init() # For special correspondence backend to module name as used in the function requires_modulename __lowerCAmelCase = {} for backend, objects in backend_specific_objects.items(): __lowerCAmelCase = '''[''' + ''', '''.join(F"""\"{b}\"""" for b in backend.split('''_and_''')) + ''']''' __lowerCAmelCase = '''# This file is autogenerated by the command `make fix-copies`, do not edit.\n''' dummy_file += "from ..utils import DummyObject, requires_backends\n\n" dummy_file += "\n".join([create_dummy_object(lowerCamelCase, lowerCamelCase) for o in objects]) __lowerCAmelCase = dummy_file return dummy_files def __magic_name__( lowerCamelCase=False): __lowerCAmelCase = create_dummy_files() # For special correspondence backend to shortcut as used in utils/dummy_xxx_objects.py __lowerCAmelCase = {'''torch''': '''pt'''} # Locate actual dummy modules and read their content. __lowerCAmelCase = os.path.join(lowerCamelCase, '''utils''') __lowerCAmelCase = { backend: os.path.join(lowerCamelCase, F"""dummy_{short_names.get(lowerCamelCase, lowerCamelCase)}_objects.py""") for backend in dummy_files.keys() } __lowerCAmelCase = {} for backend, file_path in dummy_file_paths.items(): if os.path.isfile(lowerCamelCase): with open(lowerCamelCase, '''r''', encoding='''utf-8''', newline='''\n''') as f: __lowerCAmelCase = f.read() else: __lowerCAmelCase = '''''' for backend in dummy_files.keys(): if dummy_files[backend] != actual_dummies[backend]: if overwrite: print( F"""Updating diffusers.utils.dummy_{short_names.get(lowerCamelCase, lowerCamelCase)}_objects.py as the main """ '''__init__ has new objects.''') with open(dummy_file_paths[backend], '''w''', encoding='''utf-8''', newline='''\n''') as f: f.write(dummy_files[backend]) else: raise ValueError( '''The main __init__ has objects that are not present in ''' F"""diffusers.utils.dummy_{short_names.get(lowerCamelCase, lowerCamelCase)}_objects.py. Run `make fix-copies` """ '''to fix this.''') if __name__ == "__main__": _UpperCAmelCase : int = argparse.ArgumentParser() parser.add_argument("""--fix_and_overwrite""", action="""store_true""", help="""Whether to fix inconsistencies.""") _UpperCAmelCase : str = parser.parse_args() check_dummies(args.fix_and_overwrite)
9
'''simple docstring''' # Imports import numpy as np class a__ : """simple docstring""" def __init__(self , __lowercase=None , __lowercase=None , __lowercase=None , __lowercase=None , __lowercase=None ): self.set_matricies(red=__lowercase , green=__lowercase , blue=__lowercase , red_edge=__lowercase , nir=__lowercase ) def _snake_case (self , __lowercase=None , __lowercase=None , __lowercase=None , __lowercase=None , __lowercase=None ): if red is not None: __lowerCAmelCase = red if green is not None: __lowerCAmelCase = green if blue is not None: __lowerCAmelCase = blue if red_edge is not None: __lowerCAmelCase = red_edge if nir is not None: __lowerCAmelCase = nir return True def _snake_case (self , __lowercase="" , __lowercase=None , __lowercase=None , __lowercase=None , __lowercase=None , __lowercase=None ): self.set_matricies(red=__lowercase , green=__lowercase , blue=__lowercase , red_edge=__lowercase , nir=__lowercase ) __lowerCAmelCase = { '''ARVI2''': self.arvaa, '''CCCI''': self.ccci, '''CVI''': self.cvi, '''GLI''': self.gli, '''NDVI''': self.ndvi, '''BNDVI''': self.bndvi, '''redEdgeNDVI''': self.red_edge_ndvi, '''GNDVI''': self.gndvi, '''GBNDVI''': self.gbndvi, '''GRNDVI''': self.grndvi, '''RBNDVI''': self.rbndvi, '''PNDVI''': self.pndvi, '''ATSAVI''': self.atsavi, '''BWDRVI''': self.bwdrvi, '''CIgreen''': self.ci_green, '''CIrededge''': self.ci_rededge, '''CI''': self.ci, '''CTVI''': self.ctvi, '''GDVI''': self.gdvi, '''EVI''': self.evi, '''GEMI''': self.gemi, '''GOSAVI''': self.gosavi, '''GSAVI''': self.gsavi, '''Hue''': self.hue, '''IVI''': self.ivi, '''IPVI''': self.ipvi, '''I''': self.i, '''RVI''': self.rvi, '''MRVI''': self.mrvi, '''MSAVI''': self.m_savi, '''NormG''': self.norm_g, '''NormNIR''': self.norm_nir, '''NormR''': self.norm_r, '''NGRDI''': self.ngrdi, '''RI''': self.ri, '''S''': self.s, '''IF''': self._if, '''DVI''': self.dvi, '''TVI''': self.tvi, '''NDRE''': self.ndre, } try: return funcs[index]() except KeyError: print('''Index not in the list!''' ) return False def _snake_case (self ): return -0.1_8 + (1.1_7 * ((self.nir - self.red) / (self.nir + self.red))) def _snake_case (self ): return ((self.nir - self.redEdge) / (self.nir + self.redEdge)) / ( (self.nir - self.red) / (self.nir + self.red) ) def _snake_case (self ): return self.nir * (self.red / (self.green**2)) def _snake_case (self ): return (2 * self.green - self.red - self.blue) / ( 2 * self.green + self.red + self.blue ) def _snake_case (self ): return (self.nir - self.red) / (self.nir + self.red) def _snake_case (self ): return (self.nir - self.blue) / (self.nir + self.blue) def _snake_case (self ): return (self.redEdge - self.red) / (self.redEdge + self.red) def _snake_case (self ): return (self.nir - self.green) / (self.nir + self.green) def _snake_case (self ): return (self.nir - (self.green + self.blue)) / ( self.nir + (self.green + self.blue) ) def _snake_case (self ): return (self.nir - (self.green + self.red)) / ( self.nir + (self.green + self.red) ) def _snake_case (self ): return (self.nir - (self.blue + self.red)) / (self.nir + (self.blue + self.red)) def _snake_case (self ): return (self.nir - (self.green + self.red + self.blue)) / ( self.nir + (self.green + self.red + self.blue) ) def _snake_case (self , __lowercase=0.0_8 , __lowercase=1.2_2 , __lowercase=0.0_3 ): return a * ( (self.nir - a * self.red - b) / (a * self.nir + self.red - a * b + x * (1 + a**2)) ) def _snake_case (self ): return (0.1 * self.nir - self.blue) / (0.1 * self.nir + self.blue) def _snake_case (self ): return (self.nir / self.green) - 1 def _snake_case (self ): return (self.nir / self.redEdge) - 1 def _snake_case (self ): return (self.red - self.blue) / self.red def _snake_case (self ): __lowerCAmelCase = self.ndvi() return ((ndvi + 0.5) / (abs(ndvi + 0.5 ))) * (abs(ndvi + 0.5 ) ** (1 / 2)) def _snake_case (self ): return self.nir - self.green def _snake_case (self ): return 2.5 * ( (self.nir - self.red) / (self.nir + 6 * self.red - 7.5 * self.blue + 1) ) def _snake_case (self ): __lowerCAmelCase = (2 * (self.nir**2 - self.red**2) + 1.5 * self.nir + 0.5 * self.red) / ( self.nir + self.red + 0.5 ) return n * (1 - 0.2_5 * n) - (self.red - 0.1_2_5) / (1 - self.red) def _snake_case (self , __lowercase=0.1_6 ): return (self.nir - self.green) / (self.nir + self.green + y) def _snake_case (self , __lowercase=0.5 ): return ((self.nir - self.green) / (self.nir + self.green + n)) * (1 + n) def _snake_case (self ): return np.arctan( ((2 * self.red - self.green - self.blue) / 3_0.5) * (self.green - self.blue) ) def _snake_case (self , __lowercase=None , __lowercase=None ): return (self.nir - b) / (a * self.red) def _snake_case (self ): return (self.nir / ((self.nir + self.red) / 2)) * (self.ndvi() + 1) def _snake_case (self ): return (self.red + self.green + self.blue) / 3_0.5 def _snake_case (self ): return self.nir / self.red def _snake_case (self ): return (self.rvi() - 1) / (self.rvi() + 1) def _snake_case (self ): return ( (2 * self.nir + 1) - ((2 * self.nir + 1) ** 2 - 8 * (self.nir - self.red)) ** (1 / 2) ) / 2 def _snake_case (self ): return self.green / (self.nir + self.red + self.green) def _snake_case (self ): return self.nir / (self.nir + self.red + self.green) def _snake_case (self ): return self.red / (self.nir + self.red + self.green) def _snake_case (self ): return (self.green - self.red) / (self.green + self.red) def _snake_case (self ): return (self.red - self.green) / (self.red + self.green) def _snake_case (self ): __lowerCAmelCase = np.max([np.max(self.red ), np.max(self.green ), np.max(self.blue )] ) __lowerCAmelCase = np.min([np.min(self.red ), np.min(self.green ), np.min(self.blue )] ) return (max_value - min_value) / max_value def _snake_case (self ): return (2 * self.red - self.green - self.blue) / (self.green - self.blue) def _snake_case (self ): return self.nir / self.red def _snake_case (self ): return (self.ndvi() + 0.5) ** (1 / 2) def _snake_case (self ): return (self.nir - self.redEdge) / (self.nir + self.redEdge)
9
1
"""simple docstring""" # Lint as: python3 import dataclasses import re from dataclasses import dataclass from functools import total_ordering from typing import Optional, Union UpperCAmelCase_ : List[str] = re.compile(r"""^(?P<major>\d+)""" r"""\.(?P<minor>\d+)""" r"""\.(?P<patch>\d+)$""") @total_ordering @dataclass class lowerCAmelCase__ : '''simple docstring''' __UpperCamelCase = 42 __UpperCamelCase = None __UpperCamelCase = None __UpperCamelCase = None __UpperCamelCase = None def _SCREAMING_SNAKE_CASE ( self : Optional[int]): '''simple docstring''' SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Union[str, Any] = _str_to_version_tuple(self.version_str) def __repr__( self : Optional[Any]): '''simple docstring''' return F'{self.tuple[0]}.{self.tuple[1]}.{self.tuple[2]}' @property def _SCREAMING_SNAKE_CASE ( self : Tuple): '''simple docstring''' return self.major, self.minor, self.patch def _SCREAMING_SNAKE_CASE ( self : Tuple , lowercase_ : List[Any]): '''simple docstring''' if isinstance(lowercase_ , lowercase_): return Version(lowercase_) elif isinstance(lowercase_ , lowercase_): return other raise TypeError(F'{other} (type {type(lowercase_)}) cannot be compared to version.') def __eq__( self : str , lowercase_ : Optional[Any]): '''simple docstring''' try: SCREAMING_SNAKE_CASE_ : List[str] = self._validate_operand(lowercase_) except (TypeError, ValueError): return False else: return self.tuple == other.tuple def __lt__( self : Dict , lowercase_ : List[Any]): '''simple docstring''' SCREAMING_SNAKE_CASE_ : str = self._validate_operand(lowercase_) return self.tuple < other.tuple def __hash__( self : List[Any]): '''simple docstring''' return hash(_version_tuple_to_str(self.tuple)) @classmethod def _SCREAMING_SNAKE_CASE ( cls : Tuple , lowercase_ : List[str]): '''simple docstring''' SCREAMING_SNAKE_CASE_ : Union[str, Any] = {f.name for f in dataclasses.fields(cls)} return cls(**{k: v for k, v in dic.items() if k in field_names}) def _SCREAMING_SNAKE_CASE ( self : List[Any]): '''simple docstring''' return self.version_str def _A (__a ) -> List[Any]: """simple docstring""" SCREAMING_SNAKE_CASE_ : str = _VERSION_REG.match(__a ) if not res: raise ValueError(f'Invalid version \'{version_str}\'. Format should be x.y.z with {{x,y,z}} being digits.' ) return tuple(int(__a ) for v in [res.group('''major''' ), res.group('''minor''' ), res.group('''patch''' )] ) def _A (__a ) -> List[str]: """simple docstring""" return ".".join(str(__a ) for v in version_tuple )
91
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCAmelCase_ : Optional[int] = logging.get_logger(__name__) UpperCAmelCase_ : Any = {"""openai-gpt""": """https://huggingface.co/openai-gpt/resolve/main/config.json"""} class lowerCAmelCase__ ( UpperCAmelCase__ ): '''simple docstring''' __UpperCamelCase = "openai-gpt" __UpperCamelCase = { "max_position_embeddings": "n_positions", "hidden_size": "n_embd", "num_attention_heads": "n_head", "num_hidden_layers": "n_layer", } def __init__( self : List[str] , lowercase_ : List[str]=40478 , lowercase_ : List[str]=512 , lowercase_ : Optional[Any]=768 , lowercase_ : Tuple=12 , lowercase_ : Tuple=12 , lowercase_ : Union[str, Any]="gelu" , lowercase_ : List[Any]=0.1 , lowercase_ : Tuple=0.1 , lowercase_ : List[Any]=0.1 , lowercase_ : List[Any]=1e-5 , lowercase_ : int=0.02 , lowercase_ : Optional[int]="cls_index" , lowercase_ : Any=True , lowercase_ : List[Any]=None , lowercase_ : List[str]=True , lowercase_ : Optional[Any]=0.1 , **lowercase_ : List[str] , ): '''simple docstring''' SCREAMING_SNAKE_CASE_ : Optional[int] = vocab_size SCREAMING_SNAKE_CASE_ : Tuple = n_positions SCREAMING_SNAKE_CASE_ : Optional[int] = n_embd SCREAMING_SNAKE_CASE_ : Dict = n_layer SCREAMING_SNAKE_CASE_ : Any = n_head SCREAMING_SNAKE_CASE_ : Union[str, Any] = afn SCREAMING_SNAKE_CASE_ : int = resid_pdrop SCREAMING_SNAKE_CASE_ : List[str] = embd_pdrop SCREAMING_SNAKE_CASE_ : Union[str, Any] = attn_pdrop SCREAMING_SNAKE_CASE_ : Union[str, Any] = layer_norm_epsilon SCREAMING_SNAKE_CASE_ : List[Any] = initializer_range SCREAMING_SNAKE_CASE_ : List[str] = summary_type SCREAMING_SNAKE_CASE_ : Tuple = summary_use_proj SCREAMING_SNAKE_CASE_ : Union[str, Any] = summary_activation SCREAMING_SNAKE_CASE_ : Any = summary_first_dropout SCREAMING_SNAKE_CASE_ : List[str] = summary_proj_to_labels super().__init__(**lowercase_)
91
1
import os # noqa: this is just for tests import os as renamed_os # noqa: this is just for tests from os import path # noqa: this is just for tests from os import path as renamed_path # noqa: this is just for tests from os.path import join # noqa: this is just for tests from os.path import join as renamed_join # noqa: this is just for tests UpperCamelCase_ = open # noqa: we just need to have a builtin inside this module to test it properly
356
"""simple docstring""" import math UpperCamelCase_ = 10 UpperCamelCase_ = 7 UpperCamelCase_ = BALLS_PER_COLOUR * NUM_COLOURS def UpperCamelCase ( UpperCAmelCase = 20 ) ->str: """simple docstring""" a_ = math.comb(UpperCAmelCase , UpperCAmelCase ) a_ = math.comb(NUM_BALLS - BALLS_PER_COLOUR , UpperCAmelCase ) a_ = NUM_COLOURS * (1 - missing_colour / total) return F'''{result:.9f}''' if __name__ == "__main__": print(solution(20))
303
0
'''simple docstring''' import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, EulerAncestralDiscreteScheduler, LMSDiscreteScheduler, PNDMScheduler, StableDiffusionPanoramaPipeline, UNetaDConditionModel, ) from diffusers.utils import slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, skip_mps 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() @skip_mps class a__ ( UpperCAmelCase__ , UpperCAmelCase__ , unittest.TestCase ): lowerCamelCase : int =StableDiffusionPanoramaPipeline lowerCamelCase : Dict =TEXT_TO_IMAGE_PARAMS lowerCamelCase : Union[str, Any] =TEXT_TO_IMAGE_BATCH_PARAMS lowerCamelCase : Dict =TEXT_TO_IMAGE_IMAGE_PARAMS lowerCamelCase : int =TEXT_TO_IMAGE_IMAGE_PARAMS def SCREAMING_SNAKE_CASE__ ( self : Dict ): """simple docstring""" torch.manual_seed(0 ) __lowerCamelCase = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=1 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''') , up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''') , cross_attention_dim=32 , ) __lowerCamelCase = DDIMScheduler() torch.manual_seed(0 ) __lowerCamelCase = 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 ) __lowerCamelCase = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1e-0_5 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=10_00 , ) __lowerCamelCase = CLIPTextModel(a ) __lowerCamelCase = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) __lowerCamelCase = { '''unet''': unet, '''scheduler''': scheduler, '''vae''': vae, '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''safety_checker''': None, '''feature_extractor''': None, } return components def SCREAMING_SNAKE_CASE__ ( self : List[str] , a : Dict , a : Union[str, Any]=0 ): """simple docstring""" __lowerCamelCase = torch.manual_seed(a ) __lowerCamelCase = { '''prompt''': '''a photo of the dolomites''', '''generator''': generator, # Setting height and width to None to prevent OOMs on CPU. '''height''': None, '''width''': None, '''num_inference_steps''': 1, '''guidance_scale''': 6.0, '''output_type''': '''numpy''', } return inputs def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): """simple docstring""" __lowerCamelCase = '''cpu''' # ensure determinism for the device-dependent torch.Generator __lowerCamelCase = self.get_dummy_components() __lowerCamelCase = StableDiffusionPanoramaPipeline(**a ) __lowerCamelCase = sd_pipe.to(a ) sd_pipe.set_progress_bar_config(disable=a ) __lowerCamelCase = self.get_dummy_inputs(a ) __lowerCamelCase = sd_pipe(**a ).images __lowerCamelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) __lowerCamelCase = np.array([0.61_86, 0.53_74, 0.49_15, 0.41_35, 0.41_14, 0.45_63, 0.51_28, 0.49_77, 0.47_57] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def SCREAMING_SNAKE_CASE__ ( self : Tuple ): """simple docstring""" super().test_inference_batch_consistent(batch_sizes=[1, 2] ) def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ): """simple docstring""" super().test_inference_batch_single_identical(batch_size=2 , expected_max_diff=3.2_5e-3 ) def SCREAMING_SNAKE_CASE__ ( self : List[Any] ): """simple docstring""" __lowerCamelCase = '''cpu''' # ensure determinism for the device-dependent torch.Generator __lowerCamelCase = self.get_dummy_components() __lowerCamelCase = StableDiffusionPanoramaPipeline(**a ) __lowerCamelCase = sd_pipe.to(a ) sd_pipe.set_progress_bar_config(disable=a ) __lowerCamelCase = self.get_dummy_inputs(a ) __lowerCamelCase = '''french fries''' __lowerCamelCase = sd_pipe(**a , negative_prompt=a ) __lowerCamelCase = output.images __lowerCamelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) __lowerCamelCase = np.array([0.61_87, 0.53_75, 0.49_15, 0.41_36, 0.41_14, 0.45_63, 0.51_28, 0.49_76, 0.47_57] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def SCREAMING_SNAKE_CASE__ ( self : Dict ): """simple docstring""" __lowerCamelCase = '''cpu''' # ensure determinism for the device-dependent torch.Generator __lowerCamelCase = self.get_dummy_components() __lowerCamelCase = StableDiffusionPanoramaPipeline(**a ) __lowerCamelCase = sd_pipe.to(a ) sd_pipe.set_progress_bar_config(disable=a ) __lowerCamelCase = self.get_dummy_inputs(a ) __lowerCamelCase = sd_pipe(**a , view_batch_size=2 ) __lowerCamelCase = output.images __lowerCamelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) __lowerCamelCase = np.array([0.61_87, 0.53_75, 0.49_15, 0.41_36, 0.41_14, 0.45_63, 0.51_28, 0.49_76, 0.47_57] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def SCREAMING_SNAKE_CASE__ ( self : Any ): """simple docstring""" __lowerCamelCase = '''cpu''' # ensure determinism for the device-dependent torch.Generator __lowerCamelCase = self.get_dummy_components() __lowerCamelCase = EulerAncestralDiscreteScheduler( beta_start=0.0_00_85 , beta_end=0.0_12 , beta_schedule='''scaled_linear''' ) __lowerCamelCase = StableDiffusionPanoramaPipeline(**a ) __lowerCamelCase = sd_pipe.to(a ) sd_pipe.set_progress_bar_config(disable=a ) __lowerCamelCase = self.get_dummy_inputs(a ) __lowerCamelCase = sd_pipe(**a ).images __lowerCamelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) __lowerCamelCase = np.array([0.40_24, 0.65_10, 0.49_01, 0.53_78, 0.58_13, 0.56_22, 0.47_95, 0.44_67, 0.49_52] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): """simple docstring""" __lowerCamelCase = '''cpu''' # ensure determinism for the device-dependent torch.Generator __lowerCamelCase = self.get_dummy_components() __lowerCamelCase = PNDMScheduler( beta_start=0.0_00_85 , beta_end=0.0_12 , beta_schedule='''scaled_linear''' , skip_prk_steps=a ) __lowerCamelCase = StableDiffusionPanoramaPipeline(**a ) __lowerCamelCase = sd_pipe.to(a ) sd_pipe.set_progress_bar_config(disable=a ) __lowerCamelCase = self.get_dummy_inputs(a ) __lowerCamelCase = sd_pipe(**a ).images __lowerCamelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) __lowerCamelCase = np.array([0.63_91, 0.62_91, 0.48_61, 0.51_34, 0.55_52, 0.45_78, 0.50_32, 0.50_23, 0.45_39] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 @slow @require_torch_gpu class a__ ( unittest.TestCase ): def SCREAMING_SNAKE_CASE__ ( self : Any ): """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def SCREAMING_SNAKE_CASE__ ( self : str , a : str=0 ): """simple docstring""" __lowerCamelCase = torch.manual_seed(a ) __lowerCamelCase = { '''prompt''': '''a photo of the dolomites''', '''generator''': generator, '''num_inference_steps''': 3, '''guidance_scale''': 7.5, '''output_type''': '''numpy''', } return inputs def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ): """simple docstring""" __lowerCamelCase = '''stabilityai/stable-diffusion-2-base''' __lowerCamelCase = DDIMScheduler.from_pretrained(a , subfolder='''scheduler''' ) __lowerCamelCase = StableDiffusionPanoramaPipeline.from_pretrained(a , scheduler=a , safety_checker=a ) pipe.to(a ) pipe.set_progress_bar_config(disable=a ) pipe.enable_attention_slicing() __lowerCamelCase = self.get_inputs() __lowerCamelCase = pipe(**a ).images __lowerCamelCase = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 5_12, 20_48, 3) __lowerCamelCase = np.array( [ 0.36_96_83_92, 0.27_02_53_72, 0.32_44_67_66, 0.28_37_93_87, 0.36_36_32_74, 0.30_73_33_47, 0.27_10_00_27, 0.27_05_41_25, 0.25_53_60_96, ] ) assert np.abs(expected_slice - image_slice ).max() < 1e-2 def SCREAMING_SNAKE_CASE__ ( self : Any ): """simple docstring""" __lowerCamelCase = StableDiffusionPanoramaPipeline.from_pretrained( '''stabilityai/stable-diffusion-2-base''' , safety_checker=a ) __lowerCamelCase = LMSDiscreteScheduler.from_config(pipe.scheduler.config ) pipe.to(a ) pipe.set_progress_bar_config(disable=a ) pipe.enable_attention_slicing() __lowerCamelCase = self.get_inputs() __lowerCamelCase = pipe(**a ).images __lowerCamelCase = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 5_12, 20_48, 3) __lowerCamelCase = np.array( [ [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ] ] ) assert np.abs(expected_slice - image_slice ).max() < 1e-3 def SCREAMING_SNAKE_CASE__ ( self : Dict ): """simple docstring""" __lowerCamelCase = 0 def callback_fn(a : int , a : int , a : torch.FloatTensor ) -> None: __lowerCamelCase = True nonlocal number_of_steps number_of_steps += 1 if step == 1: __lowerCamelCase = latents.detach().cpu().numpy() assert latents.shape == (1, 4, 64, 2_56) __lowerCamelCase = latents[0, -3:, -3:, -1] __lowerCamelCase = np.array( [ 0.18_68_18_69, 0.33_90_78_16, 0.5_36_12_76, 0.14_43_28_65, -0.02_85_66_11, -0.73_94_11_23, 0.23_39_79_87, 0.47_32_26_82, -0.37_82_31_64, ] ) assert np.abs(latents_slice.flatten() - expected_slice ).max() < 5e-2 elif step == 2: __lowerCamelCase = latents.detach().cpu().numpy() assert latents.shape == (1, 4, 64, 2_56) __lowerCamelCase = latents[0, -3:, -3:, -1] __lowerCamelCase = np.array( [ 0.18_53_96_45, 0.33_98_72_48, 0.5_37_85_59, 0.14_43_71_42, -0.02_45_52_61, -0.7_33_83_17, 0.23_99_07_55, 0.47_35_62_72, -0.3_78_65_05, ] ) assert np.abs(latents_slice.flatten() - expected_slice ).max() < 5e-2 __lowerCamelCase = False __lowerCamelCase = '''stabilityai/stable-diffusion-2-base''' __lowerCamelCase = DDIMScheduler.from_pretrained(a , subfolder='''scheduler''' ) __lowerCamelCase = StableDiffusionPanoramaPipeline.from_pretrained(a , scheduler=a , safety_checker=a ) __lowerCamelCase = pipe.to(a ) pipe.set_progress_bar_config(disable=a ) pipe.enable_attention_slicing() __lowerCamelCase = self.get_inputs() pipe(**a , callback=a , callback_steps=1 ) assert callback_fn.has_been_called assert number_of_steps == 3 def SCREAMING_SNAKE_CASE__ ( self : Tuple ): """simple docstring""" torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats() __lowerCamelCase = '''stabilityai/stable-diffusion-2-base''' __lowerCamelCase = DDIMScheduler.from_pretrained(a , subfolder='''scheduler''' ) __lowerCamelCase = StableDiffusionPanoramaPipeline.from_pretrained(a , scheduler=a , safety_checker=a ) __lowerCamelCase = pipe.to(a ) pipe.set_progress_bar_config(disable=a ) pipe.enable_attention_slicing(1 ) pipe.enable_sequential_cpu_offload() __lowerCamelCase = self.get_inputs() __lowerCamelCase = pipe(**a ) __lowerCamelCase = torch.cuda.max_memory_allocated() # make sure that less than 5.2 GB is allocated assert mem_bytes < 5.5 * 10**9
67
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 lowerCAmelCase__ = get_tests_dir() + """/test_data/fsmt/fsmt_val_data.json""" with io.open(filename, """r""", encoding="""utf-8""") as f: lowerCAmelCase__ = json.load(f) @require_torch class a__ ( unittest.TestCase ): """simple docstring""" def UpperCamelCase ( self , lowercase ) -> int: '''simple docstring''' return FSMTTokenizer.from_pretrained(lowercase ) def UpperCamelCase ( self , lowercase ) -> Optional[int]: '''simple docstring''' A__ = FSMTForConditionalGeneration.from_pretrained(lowercase ).to(lowercase ) 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 UpperCamelCase ( self , lowercase , lowercase ) -> List[Any]: '''simple docstring''' A__ = F'facebook/wmt19-{pair}' A__ = self.get_tokenizer(lowercase ) A__ = self.get_model(lowercase ) A__ = bleu_data[pair]["src"] A__ = bleu_data[pair]["tgt"] A__ = tokenizer(lowercase , return_tensors="pt" , truncation=lowercase , padding="longest" ).to(lowercase ) A__ = model.generate( input_ids=batch.input_ids , num_beams=8 , ) A__ = tokenizer.batch_decode( lowercase , skip_special_tokens=lowercase , clean_up_tokenization_spaces=lowercase ) A__ = calculate_bleu(lowercase , lowercase ) print(lowercase ) self.assertGreaterEqual(scores["bleu"] , lowercase )
68
0
UpperCAmelCase_ : Union[str, Any] = [4, 1, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5] UpperCAmelCase_ : int = [3, 7, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5] UpperCAmelCase_ : int = { 0: """Sunday""", 1: """Monday""", 2: """Tuesday""", 3: """Wednesday""", 4: """Thursday""", 5: """Friday""", 6: """Saturday""", } def SCREAMING_SNAKE_CASE_ ( __A : str , __A : Optional[int] , __A : str ) -> str: """simple docstring""" assert len(str(_UpperCAmelCase ) ) > 2, "year should be in YYYY format" assert 1 <= month <= 12, "month should be between 1 to 12" assert 1 <= day <= 31, "day should be between 1 to 31" # Doomsday algorithm: a_ : Optional[int] = year // 1_00 a_ : Any = (5 * (century % 4) + 2) % 7 a_ : str = year % 1_00 a_ : int = centurian % 12 a_ : Dict = ( (centurian // 12) + centurian_m + (centurian_m // 4) + century_anchor ) % 7 a_ : str = ( DOOMSDAY_NOT_LEAP[month - 1] if (year % 4 != 0) or (centurian == 0 and (year % 4_00) == 0) else DOOMSDAY_LEAP[month - 1] ) a_ : List[Any] = (dooms_day + day - day_anchor) % 7 return WEEK_DAY_NAMES[week_day] if __name__ == "__main__": import doctest doctest.testmod()
351
from typing import List, Optional, Tuple, Union import torch from torch import nn from torch.nn import CrossEntropyLoss from ... import AutoBackbone from ...modeling_outputs import SemanticSegmenterOutput from ...modeling_utils import PreTrainedModel from ...utils import add_start_docstrings, add_start_docstrings_to_model_forward, replace_return_docstrings from ...utils.backbone_utils import BackboneMixin from .configuration_upernet import UperNetConfig UpperCAmelCase_ : Dict = [ 'openmmlab/upernet-convnext-tiny', # See all UperNet models at https://huggingface.co/models?filter=upernet ] # General docstring UpperCAmelCase_ : Dict = 'UperNetConfig' class SCREAMING_SNAKE_CASE__ ( nn.Module ): def __init__( self : int , SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : Union[int, Tuple[int, int]] , SCREAMING_SNAKE_CASE__ : Union[int, Tuple[int, int], str] = 0 , SCREAMING_SNAKE_CASE__ : bool = False , SCREAMING_SNAKE_CASE__ : Union[int, Tuple[int, int]] = 1 , ) -> None: super().__init__() a_ : Dict = nn.Convad( in_channels=SCREAMING_SNAKE_CASE__ , out_channels=SCREAMING_SNAKE_CASE__ , kernel_size=SCREAMING_SNAKE_CASE__ , padding=SCREAMING_SNAKE_CASE__ , bias=SCREAMING_SNAKE_CASE__ , dilation=SCREAMING_SNAKE_CASE__ , ) a_ : List[str] = nn.BatchNormad(SCREAMING_SNAKE_CASE__ ) a_ : Tuple = nn.ReLU() def SCREAMING_SNAKE_CASE ( self : Optional[int] , SCREAMING_SNAKE_CASE__ : torch.Tensor ) -> torch.Tensor: a_ : int = self.conv(SCREAMING_SNAKE_CASE__ ) a_ : Optional[int] = self.batch_norm(SCREAMING_SNAKE_CASE__ ) a_ : List[Any] = self.activation(SCREAMING_SNAKE_CASE__ ) return output class SCREAMING_SNAKE_CASE__ ( nn.Module ): def __init__( self : str , SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : int ) -> None: super().__init__() a_ : str = [ nn.AdaptiveAvgPoolad(SCREAMING_SNAKE_CASE__ ), UperNetConvModule(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , kernel_size=1 ), ] for i, layer in enumerate(self.layers ): self.add_module(str(SCREAMING_SNAKE_CASE__ ) , SCREAMING_SNAKE_CASE__ ) def SCREAMING_SNAKE_CASE ( self : Any , SCREAMING_SNAKE_CASE__ : torch.Tensor ) -> torch.Tensor: a_ : Optional[int] = input for layer in self.layers: a_ : int = layer(SCREAMING_SNAKE_CASE__ ) return hidden_state class SCREAMING_SNAKE_CASE__ ( nn.Module ): def __init__( self : Tuple , SCREAMING_SNAKE_CASE__ : Tuple[int, ...] , SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : bool ) -> None: super().__init__() a_ : Tuple = pool_scales a_ : List[Any] = align_corners a_ : Tuple = in_channels a_ : Optional[Any] = channels a_ : Tuple = [] for i, pool_scale in enumerate(SCREAMING_SNAKE_CASE__ ): a_ : Optional[int] = UperNetPyramidPoolingBlock(pool_scale=SCREAMING_SNAKE_CASE__ , in_channels=SCREAMING_SNAKE_CASE__ , channels=SCREAMING_SNAKE_CASE__ ) self.blocks.append(SCREAMING_SNAKE_CASE__ ) self.add_module(str(SCREAMING_SNAKE_CASE__ ) , SCREAMING_SNAKE_CASE__ ) def SCREAMING_SNAKE_CASE ( self : Any , SCREAMING_SNAKE_CASE__ : torch.Tensor ) -> List[torch.Tensor]: a_ : List[Any] = [] for ppm in self.blocks: a_ : List[Any] = ppm(SCREAMING_SNAKE_CASE__ ) a_ : str = nn.functional.interpolate( SCREAMING_SNAKE_CASE__ , size=x.size()[2:] , mode='bilinear' , align_corners=self.align_corners ) ppm_outs.append(SCREAMING_SNAKE_CASE__ ) return ppm_outs class SCREAMING_SNAKE_CASE__ ( nn.Module ): def __init__( self : Tuple , SCREAMING_SNAKE_CASE__ : Any , SCREAMING_SNAKE_CASE__ : str ) -> Tuple: super().__init__() a_ : List[str] = config a_ : Optional[int] = config.pool_scales # e.g. (1, 2, 3, 6) a_ : int = in_channels a_ : Dict = config.hidden_size a_ : Optional[Any] = False a_ : Tuple = nn.Convad(self.channels , config.num_labels , kernel_size=1 ) # PSP Module a_ : int = UperNetPyramidPoolingModule( self.pool_scales , self.in_channels[-1] , self.channels , align_corners=self.align_corners , ) a_ : int = UperNetConvModule( self.in_channels[-1] + len(self.pool_scales ) * self.channels , self.channels , kernel_size=3 , padding=1 , ) # FPN Module a_ : str = nn.ModuleList() a_ : Dict = nn.ModuleList() for in_channels in self.in_channels[:-1]: # skip the top layer a_ : str = UperNetConvModule(SCREAMING_SNAKE_CASE__ , self.channels , kernel_size=1 ) a_ : Union[str, Any] = UperNetConvModule(self.channels , self.channels , kernel_size=3 , padding=1 ) self.lateral_convs.append(SCREAMING_SNAKE_CASE__ ) self.fpn_convs.append(SCREAMING_SNAKE_CASE__ ) a_ : Optional[int] = UperNetConvModule( len(self.in_channels ) * self.channels , self.channels , kernel_size=3 , padding=1 , ) def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> str: self.apply(self._init_weights ) def SCREAMING_SNAKE_CASE ( self : List[str] , SCREAMING_SNAKE_CASE__ : Dict ) -> str: if isinstance(SCREAMING_SNAKE_CASE__ , nn.Convad ): module.weight.data.normal_(mean=0.0 , std=self.config.initializer_range ) if module.bias is not None: module.bias.data.zero_() def SCREAMING_SNAKE_CASE ( self : Optional[Any] , SCREAMING_SNAKE_CASE__ : Tuple ) -> Dict: a_ : Dict = inputs[-1] a_ : str = [x] psp_outs.extend(self.psp_modules(SCREAMING_SNAKE_CASE__ ) ) a_ : List[Any] = torch.cat(SCREAMING_SNAKE_CASE__ , dim=1 ) a_ : Dict = self.bottleneck(SCREAMING_SNAKE_CASE__ ) return output def SCREAMING_SNAKE_CASE ( self : str , SCREAMING_SNAKE_CASE__ : torch.Tensor ) -> torch.Tensor: # build laterals a_ : List[str] = [lateral_conv(encoder_hidden_states[i] ) for i, lateral_conv in enumerate(self.lateral_convs )] laterals.append(self.psp_forward(SCREAMING_SNAKE_CASE__ ) ) # build top-down path a_ : List[str] = len(SCREAMING_SNAKE_CASE__ ) for i in range(used_backbone_levels - 1 , 0 , -1 ): a_ : Optional[Any] = laterals[i - 1].shape[2:] a_ : List[Any] = laterals[i - 1] + nn.functional.interpolate( laterals[i] , size=SCREAMING_SNAKE_CASE__ , mode='bilinear' , align_corners=self.align_corners ) # build outputs a_ : List[Any] = [self.fpn_convs[i](laterals[i] ) for i in range(used_backbone_levels - 1 )] # append psp feature fpn_outs.append(laterals[-1] ) for i in range(used_backbone_levels - 1 , 0 , -1 ): a_ : int = nn.functional.interpolate( fpn_outs[i] , size=fpn_outs[0].shape[2:] , mode='bilinear' , align_corners=self.align_corners ) a_ : Optional[int] = torch.cat(SCREAMING_SNAKE_CASE__ , dim=1 ) a_ : Tuple = self.fpn_bottleneck(SCREAMING_SNAKE_CASE__ ) a_ : Dict = self.classifier(SCREAMING_SNAKE_CASE__ ) return output class SCREAMING_SNAKE_CASE__ ( nn.Module ): def __init__( self : int , SCREAMING_SNAKE_CASE__ : Any , SCREAMING_SNAKE_CASE__ : int = 2 , SCREAMING_SNAKE_CASE__ : int = 3 , SCREAMING_SNAKE_CASE__ : Union[int, Tuple[int, int]] = 1 ) -> None: super().__init__() a_ : Optional[int] = config a_ : str = config.auxiliary_in_channels a_ : Optional[int] = config.auxiliary_channels a_ : List[str] = config.auxiliary_num_convs a_ : Dict = config.auxiliary_concat_input a_ : int = in_index a_ : List[Any] = (kernel_size // 2) * dilation a_ : List[Any] = [] convs.append( UperNetConvModule( self.in_channels , self.channels , kernel_size=SCREAMING_SNAKE_CASE__ , padding=SCREAMING_SNAKE_CASE__ , dilation=SCREAMING_SNAKE_CASE__ ) ) for i in range(self.num_convs - 1 ): convs.append( UperNetConvModule( self.channels , self.channels , kernel_size=SCREAMING_SNAKE_CASE__ , padding=SCREAMING_SNAKE_CASE__ , dilation=SCREAMING_SNAKE_CASE__ ) ) if self.num_convs == 0: a_ : Optional[int] = nn.Identity() else: a_ : int = nn.Sequential(*SCREAMING_SNAKE_CASE__ ) if self.concat_input: a_ : Union[str, Any] = UperNetConvModule( self.in_channels + self.channels , self.channels , kernel_size=SCREAMING_SNAKE_CASE__ , padding=kernel_size // 2 ) a_ : List[str] = nn.Convad(self.channels , config.num_labels , kernel_size=1 ) def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> List[str]: self.apply(self._init_weights ) def SCREAMING_SNAKE_CASE ( self : Union[str, Any] , SCREAMING_SNAKE_CASE__ : Any ) -> List[str]: if isinstance(SCREAMING_SNAKE_CASE__ , nn.Convad ): module.weight.data.normal_(mean=0.0 , std=self.config.initializer_range ) if module.bias is not None: module.bias.data.zero_() def SCREAMING_SNAKE_CASE ( self : Union[str, Any] , SCREAMING_SNAKE_CASE__ : torch.Tensor ) -> torch.Tensor: # just take the relevant feature maps a_ : Optional[int] = encoder_hidden_states[self.in_index] a_ : int = self.convs(SCREAMING_SNAKE_CASE__ ) if self.concat_input: a_ : Optional[Any] = self.conv_cat(torch.cat([hidden_states, output] , dim=1 ) ) a_ : Dict = self.classifier(SCREAMING_SNAKE_CASE__ ) return output class SCREAMING_SNAKE_CASE__ ( lowercase__ ): snake_case__ : Any = UperNetConfig snake_case__ : Optional[int] = '''pixel_values''' snake_case__ : Optional[int] = True def SCREAMING_SNAKE_CASE ( self : List[str] , SCREAMING_SNAKE_CASE__ : Optional[Any] ) -> str: if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): module.backbone.init_weights() module.decode_head.init_weights() module.auxiliary_head.init_weights() def SCREAMING_SNAKE_CASE ( self : Dict ) -> Dict: self.backbone.init_weights() self.decode_head.init_weights() self.auxiliary_head.init_weights() def SCREAMING_SNAKE_CASE ( self : Optional[int] , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : int=False ) -> Any: if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): a_ : Dict = value UpperCAmelCase_ : int = R'\n Parameters:\n This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) sub-class. Use\n it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and\n behavior.\n config ([`UperNetConfig`]): Model configuration class with all the parameters of the model.\n Initializing with a config file does not load the weights associated with the model, only the\n configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.\n' UpperCAmelCase_ : Tuple = R'\n Args:\n pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):\n Pixel values. Padding will be ignored by default should you provide it. Pixel values can be obtained using\n [`AutoImageProcessor`]. See [`SegformerImageProcessor.__call__`] for details.\n output_attentions (`bool`, *optional*):\n Whether or not to return the attentions tensors of all attention layers in case the backbone has them. See\n `attentions` under returned tensors for more detail.\n output_hidden_states (`bool`, *optional*):\n Whether or not to return the hidden states of all layers of the backbone. See `hidden_states` under\n returned tensors for more detail.\n return_dict (`bool`, *optional*):\n Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.\n' @add_start_docstrings( '''UperNet framework leveraging any vision backbone e.g. for ADE20k, CityScapes.''' , lowercase__ , ) class SCREAMING_SNAKE_CASE__ ( lowercase__ ): def __init__( self : Union[str, Any] , SCREAMING_SNAKE_CASE__ : Optional[int] ) -> str: super().__init__(SCREAMING_SNAKE_CASE__ ) a_ : List[str] = AutoBackbone.from_config(config.backbone_config ) # Semantic segmentation head(s) a_ : List[Any] = UperNetHead(SCREAMING_SNAKE_CASE__ , in_channels=self.backbone.channels ) a_ : Dict = UperNetFCNHead(SCREAMING_SNAKE_CASE__ ) if config.use_auxiliary_head else None # Initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(UPERNET_INPUTS_DOCSTRING.format('batch_size, sequence_length' ) ) @replace_return_docstrings(output_type=SCREAMING_SNAKE_CASE__ , config_class=_CONFIG_FOR_DOC ) def SCREAMING_SNAKE_CASE ( self : List[Any] , SCREAMING_SNAKE_CASE__ : Optional[torch.Tensor] = None , SCREAMING_SNAKE_CASE__ : Optional[bool] = None , SCREAMING_SNAKE_CASE__ : Optional[bool] = None , SCREAMING_SNAKE_CASE__ : Optional[torch.Tensor] = None , SCREAMING_SNAKE_CASE__ : Optional[bool] = None , ) -> Union[tuple, SemanticSegmenterOutput]: a_ : Optional[Any] = return_dict if return_dict is not None else self.config.use_return_dict a_ : Tuple = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) a_ : int = output_attentions if output_attentions is not None else self.config.output_attentions a_ : str = self.backbone.forward_with_filtered_kwargs( SCREAMING_SNAKE_CASE__ , output_hidden_states=SCREAMING_SNAKE_CASE__ , output_attentions=SCREAMING_SNAKE_CASE__ ) a_ : Optional[int] = outputs.feature_maps a_ : Tuple = self.decode_head(SCREAMING_SNAKE_CASE__ ) a_ : List[Any] = nn.functional.interpolate(SCREAMING_SNAKE_CASE__ , size=pixel_values.shape[2:] , mode='bilinear' , align_corners=SCREAMING_SNAKE_CASE__ ) a_ : Union[str, Any] = None if self.auxiliary_head is not None: a_ : Any = self.auxiliary_head(SCREAMING_SNAKE_CASE__ ) a_ : int = nn.functional.interpolate( SCREAMING_SNAKE_CASE__ , size=pixel_values.shape[2:] , mode='bilinear' , align_corners=SCREAMING_SNAKE_CASE__ ) a_ : List[Any] = None if labels is not None: if self.config.num_labels == 1: raise ValueError('The number of labels should be greater than one' ) else: # compute weighted loss a_ : List[str] = CrossEntropyLoss(ignore_index=self.config.loss_ignore_index ) a_ : Optional[Any] = loss_fct(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) a_ : str = loss_fct(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) a_ : str = main_loss + self.config.auxiliary_loss_weight * auxiliary_loss if not return_dict: if output_hidden_states: a_ : Optional[Any] = (logits,) + outputs[1:] else: a_ : List[str] = (logits,) + outputs[2:] return ((loss,) + output) if loss is not None else output return SemanticSegmenterOutput( loss=SCREAMING_SNAKE_CASE__ , logits=SCREAMING_SNAKE_CASE__ , hidden_states=outputs.hidden_states , attentions=outputs.attentions , )
120
0