code
stringlengths
86
54.5k
code_codestyle
int64
0
371
style_context
stringlengths
87
49.2k
style_context_codestyle
int64
0
349
label
int64
0
1
"""simple docstring""" from __future__ import annotations import math from collections.abc import Callable def lowercase__( __SCREAMING_SNAKE_CASE : Callable[[int | float], int | float] , __SCREAMING_SNAKE_CASE : int | float , __SCREAMING_SNAKE_CASE : int | float , __SCREAMING_SNAKE_CASE : int = 1_00 , ): lowercase_ : Dict = x_start lowercase_ : Optional[Any] = fnc(__SCREAMING_SNAKE_CASE ) lowercase_ : Optional[Any] = 0.0 for _ in range(__SCREAMING_SNAKE_CASE ): # Approximates curve as a sequence of linear lines and sums their length lowercase_ : Union[str, Any] = (x_end - x_start) / steps + xa lowercase_ : str = fnc(__SCREAMING_SNAKE_CASE ) length += math.hypot(xa - xa , fxa - fxa ) # Increment step lowercase_ : List[Any] = xa lowercase_ : Optional[Any] = fxa return length if __name__ == "__main__": def lowercase__( __SCREAMING_SNAKE_CASE : Any ): return math.sin(10 * x ) print("f(x) = sin(10 * x)") print("The length of the curve from x = -10 to x = 10 is:") __SCREAMING_SNAKE_CASE =10 while i <= 10_0000: print(F"With {i} steps: {line_length(f, -10, 10, i)}") i *= 10
213
"""simple docstring""" __SCREAMING_SNAKE_CASE =[ "DownloadConfig", "DownloadManager", "DownloadMode", "StreamingDownloadManager", ] from .download_config import DownloadConfig from .download_manager import DownloadManager, DownloadMode from .streaming_download_manager import StreamingDownloadManager
213
1
from collections import defaultdict def snake_case_(_UpperCamelCase , _UpperCamelCase ) -> bool: """simple docstring""" _snake_case = first_str.lower().strip() _snake_case = second_str.lower().strip() # Remove whitespace _snake_case = first_str.replace(''' ''' , '''''' ) _snake_case = second_str.replace(''' ''' , '''''' ) # Strings of different lengths are not anagrams if len(_UpperCamelCase ) != len(_UpperCamelCase ): return False # Default values for count should be 0 _snake_case = defaultdict(_UpperCamelCase ) # For each character in input strings, # increment count in the corresponding for i in range(len(_UpperCamelCase ) ): count[first_str[i]] += 1 count[second_str[i]] -= 1 return all(_count == 0 for _count in count.values() ) if __name__ == "__main__": from doctest import testmod testmod() __A = input('''Enter the first string ''').strip() __A = input('''Enter the second string ''').strip() __A = check_anagrams(input_a, input_b) print(f'''{input_a} and {input_b} are {"" if status else "not "}anagrams.''')
362
import argparse import os import re import torch from flax.traverse_util import flatten_dict from tax import checkpoints from transformers import ( AutoTokenizer, PixaStructConfig, PixaStructForConditionalGeneration, PixaStructImageProcessor, PixaStructProcessor, PixaStructTextConfig, PixaStructVisionConfig, ) def snake_case_(_UpperCamelCase ) -> Optional[int]: """simple docstring""" _snake_case = checkpoints.load_tax_checkpoint(_UpperCamelCase ) _snake_case = flatten_dict(_UpperCamelCase ) return flax_params def snake_case_(_UpperCamelCase ) -> List[str]: """simple docstring""" _snake_case = {} _snake_case = { '''token_embedder''': '''embeddings''', '''encoder_norm''': '''layernorm''', '''kernel''': '''weight''', '''.out''': '''.output''', '''scale''': '''weight''', '''embedders_0.pos_embedding''': '''row_embedder.weight''', '''embedders_1.pos_embedding''': '''column_embedder.weight''', } _snake_case = { '''query''': '''attention.query''', '''key''': '''attention.key''', '''value''': '''attention.value''', '''output.dense''': '''output''', '''encoder_decoder_attention.o''': '''encoder_decoder_attention.attention.o''', '''pre_self_attention_layer_norm''': '''self_attention.layer_norm''', '''pre_cross_attention_layer_norm''': '''encoder_decoder_attention.layer_norm''', '''mlp.''': '''mlp.DenseReluDense.''', '''pre_mlp_layer_norm''': '''mlp.layer_norm''', '''self_attention.o''': '''self_attention.attention.o''', '''decoder.embeddings.embedding''': '''decoder.embed_tokens.weight''', '''decoder.relpos_bias.rel_embedding''': '''decoder.layer.0.self_attention.attention.relative_attention_bias.weight''', '''decoder.decoder_norm.weight''': '''decoder.final_layer_norm.weight''', '''decoder.logits_dense.weight''': '''decoder.lm_head.weight''', } for key in flax_dict.keys(): if "target" in key: # remove the first prefix from the key _snake_case = '''.'''.join(key[1:] ) # rename the key for old, new in CONVERSION_MAPPING.items(): _snake_case = new_key.replace(_UpperCamelCase , _UpperCamelCase ) if "decoder" in new_key: for old, new in DECODER_CONVERSION_MAPPING.items(): _snake_case = new_key.replace(_UpperCamelCase , _UpperCamelCase ) if "layers" in new_key and "decoder" not in new_key: # use regex to replace the layer number _snake_case = re.sub(R'''layers_(\d+)''' , R'''layer.\1''' , _UpperCamelCase ) _snake_case = new_key.replace('''encoder''' , '''encoder.encoder''' ) elif "layers" in new_key and "decoder" in new_key: # use regex to replace the layer number _snake_case = re.sub(R'''layers_(\d+)''' , R'''layer.\1''' , _UpperCamelCase ) _snake_case = flax_dict[key] _snake_case = {} # convert converted_dict into torch format for key in converted_dict.keys(): if ("embed_tokens" not in key) and ("embedder" not in key): _snake_case = torch.from_numpy(converted_dict[key].T ) else: _snake_case = torch.from_numpy(converted_dict[key] ) return converted_torch_dict def snake_case_(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase=False , _UpperCamelCase=False ) -> List[Any]: """simple docstring""" _snake_case = get_flax_param(_UpperCamelCase ) if not use_large: _snake_case = PixaStructVisionConfig() _snake_case = PixaStructTextConfig() else: _snake_case = PixaStructVisionConfig( hidden_size=1_536 , d_ff=3_968 , num_attention_heads=24 , num_hidden_layers=18 ) _snake_case = PixaStructTextConfig(hidden_size=1_536 , d_ff=3_968 , num_heads=24 , num_layers=18 ) _snake_case = PixaStructConfig( vision_config=encoder_config.to_dict() , text_config=decoder_config.to_dict() , is_vqa=_UpperCamelCase ) _snake_case = PixaStructForConditionalGeneration(_UpperCamelCase ) _snake_case = rename_and_convert_flax_params(_UpperCamelCase ) model.load_state_dict(_UpperCamelCase ) _snake_case = AutoTokenizer.from_pretrained('''ybelkada/test-pix2struct-tokenizer''' ) _snake_case = PixaStructImageProcessor() _snake_case = PixaStructProcessor(image_processor=_UpperCamelCase , tokenizer=_UpperCamelCase ) if use_large: _snake_case = 4_096 _snake_case = True # mkdir if needed os.makedirs(_UpperCamelCase , exist_ok=_UpperCamelCase ) model.save_pretrained(_UpperCamelCase ) processor.save_pretrained(_UpperCamelCase ) print('''Model saved in {}'''.format(_UpperCamelCase ) ) if __name__ == "__main__": __A = argparse.ArgumentParser() parser.add_argument('''--t5x_checkpoint_path''', default=None, type=str, help='''Path to the original T5x checkpoint.''') parser.add_argument('''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''') parser.add_argument('''--use_large''', action='''store_true''', help='''Use large model.''') parser.add_argument('''--is_vqa''', action='''store_true''', help='''Use large model.''') __A = parser.parse_args() convert_pixastruct_original_pytorch_checkpoint_to_hf( args.tax_checkpoint_path, args.pytorch_dump_folder_path, args.use_large )
278
0
import argparse from pathlib import Path import torch from packaging import version from torch.onnx import export from diffusers import AutoencoderKL A : Optional[int] = version.parse(version.parse(torch.__version__).base_version) < version.parse('1.11') def __lowerCAmelCase ( a__ , a__ , a__ , a__ , a__ , a__ , a__ , a__=False , ) -> Optional[Any]: output_path.parent.mkdir(parents=a__ , exist_ok=a__ ) # PyTorch deprecated the `enable_onnx_checker` and `use_external_data_format` arguments in v1.11, # so we check the torch version for backwards compatibility if is_torch_less_than_1_11: export( a__ , a__ , f=output_path.as_posix() , input_names=a__ , output_names=a__ , dynamic_axes=a__ , do_constant_folding=a__ , use_external_data_format=a__ , enable_onnx_checker=a__ , opset_version=a__ , ) else: export( a__ , a__ , f=output_path.as_posix() , input_names=a__ , output_names=a__ , dynamic_axes=a__ , do_constant_folding=a__ , opset_version=a__ , ) @torch.no_grad() def __lowerCAmelCase ( a__ , a__ , a__ , a__ = False ) -> Tuple: __a = torch.floataa if fpaa else torch.floataa if fpaa and torch.cuda.is_available(): __a = 'cuda' elif fpaa and not torch.cuda.is_available(): raise ValueError('''`float16` model export is only supported on GPUs with CUDA''' ) else: __a = 'cpu' __a = Path(a__ ) # VAE DECODER __a = AutoencoderKL.from_pretrained(model_path + '''/vae''' ) __a = vae_decoder.config.latent_channels # forward only through the decoder part __a = vae_decoder.decode onnx_export( a__ , model_args=( torch.randn(1 , a__ , 25 , 25 ).to(device=a__ , dtype=a__ ), False, ) , output_path=output_path / '''vae_decoder''' / '''model.onnx''' , ordered_input_names=['''latent_sample''', '''return_dict'''] , output_names=['''sample'''] , dynamic_axes={ '''latent_sample''': {0: '''batch''', 1: '''channels''', 2: '''height''', 3: '''width'''}, } , opset=a__ , ) del vae_decoder if __name__ == "__main__": A : Optional[Any] = argparse.ArgumentParser() parser.add_argument( '--model_path', type=str, required=True, help='Path to the `diffusers` checkpoint to convert (either a local directory or on the Hub).', ) parser.add_argument('--output_path', type=str, required=True, help='Path to the output model.') parser.add_argument( '--opset', default=1_4, type=int, help='The version of the ONNX operator set to use.', ) parser.add_argument('--fp16', action='store_true', default=False, help='Export the models in `float16` mode') A : Union[str, Any] = parser.parse_args() print(args.output_path) convert_models(args.model_path, args.output_path, args.opset, args.fpaa) print('SD: Done: ONNX')
6
'''simple docstring''' from __future__ import annotations from math import pi # Define the Reduced Planck Constant ℏ (H bar), speed of light C, value of # Pi and the function lowerCAmelCase :Tuple = 1.0_5_4_5_7_1_8_1_7E-3_4 # unit of ℏ : J * s lowerCAmelCase :Union[str, Any] = 3E8 # unit of c : m * s^-1 def lowerCamelCase ( lowerCAmelCase : float , lowerCAmelCase : float , lowerCAmelCase : float ): """simple docstring""" if (force, area, distance).count(0 ) != 1: raise ValueError('One and only one argument must be 0' ) if force < 0: raise ValueError('Magnitude of force can not be negative' ) if distance < 0: raise ValueError('Distance can not be negative' ) if area < 0: raise ValueError('Area can not be negative' ) if force == 0: __magic_name__ : Any = (REDUCED_PLANCK_CONSTANT * SPEED_OF_LIGHT * pi**2 * area) / ( 240 * (distance) ** 4 ) return {"force": force} elif area == 0: __magic_name__ : Optional[int] = (240 * force * (distance) ** 4) / ( REDUCED_PLANCK_CONSTANT * SPEED_OF_LIGHT * pi**2 ) return {"area": area} elif distance == 0: __magic_name__ : Union[str, Any] = ( (REDUCED_PLANCK_CONSTANT * SPEED_OF_LIGHT * pi**2 * area) / (240 * force) ) ** (1 / 4) return {"distance": distance} raise ValueError('One and only one argument must be 0' ) # Run doctest if __name__ == "__main__": import doctest doctest.testmod()
331
0
from collections import OrderedDict from typing import TYPE_CHECKING, Any, List, Mapping, Optional from packaging import version if TYPE_CHECKING: from ... import PreTrainedTokenizer, TensorType from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfigWithPast, PatchingSpec from ...utils import is_torch_available, logging __SCREAMING_SNAKE_CASE : List[Any] = logging.get_logger(__name__) __SCREAMING_SNAKE_CASE : int = { 'bigscience/bloom': 'https://huggingface.co/bigscience/bloom/resolve/main/config.json', 'bigscience/bloom-560m': 'https://huggingface.co/bigscience/bloom-560m/blob/main/config.json', 'bigscience/bloom-1b1': 'https://huggingface.co/bigscience/bloom-1b1/blob/main/config.json', 'bigscience/bloom-1b7': 'https://huggingface.co/bigscience/bloom-1b7/blob/main/config.json', 'bigscience/bloom-3b': 'https://huggingface.co/bigscience/bloom-3b/blob/main/config.json', 'bigscience/bloom-7b1': 'https://huggingface.co/bigscience/bloom-7b1/blob/main/config.json', } class lowercase_ ( _lowerCAmelCase ): _lowerCamelCase = "bloom" _lowerCamelCase = ["past_key_values"] _lowerCamelCase = { "num_hidden_layers": "n_layer", "num_attention_heads": "n_head", } def __init__( self , lowercase_=250_880 , lowercase_=64 , lowercase_=2 , lowercase_=8 , lowercase_=1e-5 , lowercase_=0.02 , lowercase_=True , lowercase_=1 , lowercase_=2 , lowercase_=False , lowercase_=0.0 , lowercase_=0.0 , lowercase_=1 , lowercase_=False , **lowercase_ , ): _snake_case : List[str] = vocab_size # Backward compatibility with n_embed kwarg _snake_case : int = kwargs.pop("n_embed" , _lowercase ) _snake_case : Any = hidden_size if n_embed is None else n_embed _snake_case : Any = n_layer _snake_case : Optional[Any] = n_head _snake_case : List[str] = layer_norm_epsilon _snake_case : Dict = initializer_range _snake_case : Optional[Any] = use_cache _snake_case : str = pretraining_tp _snake_case : Optional[int] = apply_residual_connection_post_layernorm _snake_case : Any = hidden_dropout _snake_case : int = attention_dropout _snake_case : int = bos_token_id _snake_case : Union[str, Any] = eos_token_id _snake_case : List[str] = slow_but_exact super().__init__(bos_token_id=_lowercase , eos_token_id=_lowercase , **_lowercase ) class lowercase_ ( _lowerCAmelCase ): _lowerCamelCase = version.parse('1.12' ) def __init__( self , lowercase_ , lowercase_ = "default" , lowercase_ = None , lowercase_ = False , ): super().__init__(_lowercase , task=_lowercase , patching_specs=_lowercase , use_past=_lowercase ) if not getattr(self._config , "pad_token_id" , _lowercase ): # TODO: how to do that better? _snake_case : Optional[Any] = 0 @property def UpperCamelCase ( self ): _snake_case : str = OrderedDict({"input_ids": {0: "batch", 1: "sequence"}} ) if self.use_past: # BLOOM stores values on dynamic axis 2. For more details see: https://github.com/huggingface/transformers/pull/18344 self.fill_with_past_key_values_(_lowercase , direction="inputs" , inverted_values_shape=_lowercase ) _snake_case : Tuple = {0: "batch", 1: "past_sequence + sequence"} else: _snake_case : Optional[Any] = {0: "batch", 1: "sequence"} return common_inputs @property def UpperCamelCase ( self ): return self._config.n_layer @property def UpperCamelCase ( self ): return self._config.n_head @property def UpperCamelCase ( self ): return 1e-3 def UpperCamelCase ( self , lowercase_ , lowercase_ = -1 , lowercase_ = -1 , lowercase_ = False , lowercase_ = None , ): _snake_case : List[Any] = super(_lowercase , self ).generate_dummy_inputs( _lowercase , batch_size=_lowercase , seq_length=_lowercase , is_pair=_lowercase , framework=_lowercase ) # We need to order the input in the way they appears in the forward() _snake_case : List[str] = OrderedDict({"input_ids": common_inputs["input_ids"]} ) # Need to add the past_keys if self.use_past: if not is_torch_available(): raise ValueError("Cannot generate dummy past_keys inputs without PyTorch installed." ) else: import torch _snake_case ,_snake_case : List[Any] = common_inputs["input_ids"].shape # Not using the same length for past_key_values _snake_case : Union[str, Any] = seqlen + 2 _snake_case : List[Any] = self._config.hidden_size // self.num_attention_heads _snake_case : Union[str, Any] = ( batch * self.num_attention_heads, head_dim, past_key_values_length, ) _snake_case : Any = ( batch * self.num_attention_heads, past_key_values_length, head_dim, ) _snake_case : Optional[Any] = [ (torch.zeros(_lowercase ), torch.zeros(_lowercase )) for _ in range(self.num_layers ) ] _snake_case : Dict = common_inputs["attention_mask"] if self.use_past: _snake_case : Optional[Any] = ordered_inputs["attention_mask"].dtype _snake_case : str = torch.cat( [ordered_inputs["attention_mask"], torch.ones(_lowercase , _lowercase , dtype=_lowercase )] , dim=1 ) return ordered_inputs @property def UpperCamelCase ( self ): return 13
370
import logging from transformers import PretrainedConfig __SCREAMING_SNAKE_CASE : Any = logging.getLogger(__name__) __SCREAMING_SNAKE_CASE : int = { 'bertabs-finetuned-cnndm': 'https://huggingface.co/remi/bertabs-finetuned-cnndm-extractive-abstractive-summarization/resolve/main/config.json', } class lowercase_ ( __snake_case ): _lowerCamelCase = 'bertabs' def __init__( self , lowercase_=30_522 , lowercase_=512 , lowercase_=6 , lowercase_=512 , lowercase_=8 , lowercase_=512 , lowercase_=0.2 , lowercase_=6 , lowercase_=768 , lowercase_=8 , lowercase_=2_048 , lowercase_=0.2 , **lowercase_ , ): super().__init__(**lowercase_ ) _snake_case : List[Any] = vocab_size _snake_case : int = max_pos _snake_case : Tuple = enc_layers _snake_case : Optional[Any] = enc_hidden_size _snake_case : Union[str, Any] = enc_heads _snake_case : str = enc_ff_size _snake_case : Any = enc_dropout _snake_case : Tuple = dec_layers _snake_case : Optional[Any] = dec_hidden_size _snake_case : Dict = dec_heads _snake_case : str = dec_ff_size _snake_case : List[str] = dec_dropout
284
0
"""simple docstring""" import copy import os from typing import Union from ...configuration_utils import PretrainedConfig from ...utils import logging __a = logging.get_logger(__name__) __a = { "BAAI/AltCLIP": "https://huggingface.co/BAAI/AltCLIP/resolve/main/config.json", # See all AltCLIP models at https://huggingface.co/models?filter=altclip } class lowerCamelCase ( _lowerCAmelCase ): '''simple docstring''' _A : Dict = """altclip_text_model""" def __init__( self: Tuple , snake_case: List[Any]=250_002 , snake_case: Dict=1_024 , snake_case: Tuple=24 , snake_case: int=16 , snake_case: Any=4_096 , snake_case: Optional[int]="gelu" , snake_case: List[Any]=0.1 , snake_case: Union[str, Any]=0.1 , snake_case: List[str]=514 , snake_case: List[Any]=1 , snake_case: Any=0.0_2 , snake_case: Optional[int]=0.0_2 , snake_case: Union[str, Any]=1E-05 , snake_case: List[Any]=1 , snake_case: Tuple=0 , snake_case: int=2 , snake_case: Dict="absolute" , snake_case: Optional[int]=True , snake_case: Tuple=768 , **snake_case: Union[str, Any] , ) -> Union[str, Any]: super().__init__(pad_token_id=snake_case , bos_token_id=snake_case , eos_token_id=snake_case , **snake_case ) snake_case_ :Dict = vocab_size snake_case_ :str = hidden_size snake_case_ :List[Any] = num_hidden_layers snake_case_ :List[Any] = num_attention_heads snake_case_ :Union[str, Any] = hidden_act snake_case_ :Tuple = intermediate_size snake_case_ :List[str] = hidden_dropout_prob snake_case_ :int = attention_probs_dropout_prob snake_case_ :int = max_position_embeddings snake_case_ :Optional[int] = type_vocab_size snake_case_ :Union[str, Any] = initializer_range snake_case_ :str = initializer_factor snake_case_ :List[str] = layer_norm_eps snake_case_ :Tuple = position_embedding_type snake_case_ :str = use_cache snake_case_ :List[str] = project_dim class lowerCamelCase ( _lowerCAmelCase ): '''simple docstring''' _A : Optional[Any] = """altclip_vision_model""" def __init__( self: Dict , snake_case: Dict=768 , snake_case: List[Any]=3_072 , snake_case: int=512 , snake_case: List[Any]=12 , snake_case: List[str]=12 , snake_case: Optional[int]=3 , snake_case: int=224 , snake_case: Optional[Any]=32 , snake_case: Optional[int]="quick_gelu" , snake_case: Tuple=1E-5 , snake_case: Tuple=0.0 , snake_case: List[str]=0.0_2 , snake_case: Union[str, Any]=1.0 , **snake_case: str , ) -> Optional[Any]: super().__init__(**snake_case ) snake_case_ :int = hidden_size snake_case_ :Optional[int] = intermediate_size snake_case_ :List[str] = projection_dim snake_case_ :Any = num_hidden_layers snake_case_ :Optional[Any] = num_attention_heads snake_case_ :Union[str, Any] = num_channels snake_case_ :int = patch_size snake_case_ :List[Any] = image_size snake_case_ :int = initializer_range snake_case_ :Optional[Any] = initializer_factor snake_case_ :str = attention_dropout snake_case_ :List[Any] = layer_norm_eps snake_case_ :List[str] = hidden_act @classmethod def lowerCAmelCase_ ( cls: List[str] , snake_case: Union[str, os.PathLike] , **snake_case: List[Any] ) -> "PretrainedConfig": cls._set_token_in_kwargs(snake_case ) snake_case_, snake_case_ :List[str] = cls.get_config_dict(snake_case , **snake_case ) # get the vision config dict if we are loading from AltCLIPConfig if config_dict.get("""model_type""" ) == "altclip": snake_case_ :Optional[Any] = config_dict["""vision_config"""] if "model_type" in config_dict and hasattr(cls , """model_type""" ) and config_dict["model_type"] != cls.model_type: logger.warning( f"""You are using a model of type {config_dict['model_type']} to instantiate a model of type """ f"""{cls.model_type}. This is not supported for all configurations of models and can yield errors.""" ) return cls.from_dict(snake_case , **snake_case ) class lowerCamelCase ( _lowerCAmelCase ): '''simple docstring''' _A : str = """altclip""" _A : Any = True def __init__( self: int , snake_case: Any=None , snake_case: Optional[Any]=None , snake_case: str=768 , snake_case: int=2.6_5_9_2 , **snake_case: Union[str, Any] ) -> str: # If `_config_dict` exist, we use them for the backward compatibility. # We pop out these 2 attributes before calling `super().__init__` to avoid them being saved (which causes a lot # of confusion!). snake_case_ :str = kwargs.pop("""text_config_dict""" , snake_case ) snake_case_ :List[str] = kwargs.pop("""vision_config_dict""" , snake_case ) super().__init__(**snake_case ) # Instead of simply assigning `[text|vision]_config_dict` to `[text|vision]_config`, we use the values in # `[text|vision]_config_dict` to update the values in `[text|vision]_config`. The values should be same in most # cases, but we don't want to break anything regarding `_config_dict` that existed before commit `8827e1b2`. if text_config_dict is not None: if text_config is None: snake_case_ :Tuple = {} # This is the complete result when using `text_config_dict`. snake_case_ :str = AltCLIPTextConfig(**snake_case ).to_dict() # Give a warning if the values exist in both `_text_config_dict` and `text_config` but being different. for key, value in _text_config_dict.items(): if key in text_config and value != text_config[key] and key not in ["transformers_version"]: # If specified in `text_config_dict` if key in text_config_dict: snake_case_ :List[str] = ( f"""`{key}` is found in both `text_config_dict` and `text_config` but with different values. """ f"""The value `text_config_dict[\"{key}\"]` will be used instead.""" ) # If inferred from default argument values (just to be super careful) else: snake_case_ :str = ( f"""`text_config_dict` is provided which will be used to initialize `AltCLIPTextConfig`. The """ f"""value `text_config[\"{key}\"]` will be overriden.""" ) logger.warning(snake_case ) # Update all values in `text_config` with the ones in `_text_config_dict`. text_config.update(_text_config_dict ) if vision_config_dict is not None: if vision_config is None: snake_case_ :Tuple = {} # This is the complete result when using `vision_config_dict`. snake_case_ :Dict = AltCLIPVisionConfig(**snake_case ).to_dict() # convert keys to string instead of integer if "id2label" in _vision_config_dict: snake_case_ :Union[str, Any] = { str(snake_case ): value for key, value in _vision_config_dict["""id2label"""].items() } # Give a warning if the values exist in both `_vision_config_dict` and `vision_config` but being different. for key, value in _vision_config_dict.items(): if key in vision_config and value != vision_config[key] and key not in ["transformers_version"]: # If specified in `vision_config_dict` if key in vision_config_dict: snake_case_ :Dict = ( f"""`{key}` is found in both `vision_config_dict` and `vision_config` but with different """ f"""values. The value `vision_config_dict[\"{key}\"]` will be used instead.""" ) # If inferred from default argument values (just to be super careful) else: snake_case_ :Dict = ( f"""`vision_config_dict` is provided which will be used to initialize `AltCLIPVisionConfig`. """ f"""The value `vision_config[\"{key}\"]` will be overriden.""" ) logger.warning(snake_case ) # Update all values in `vision_config` with the ones in `_vision_config_dict`. vision_config.update(_vision_config_dict ) if text_config is None: snake_case_ :Optional[Any] = {} logger.info("""`text_config` is `None`. Initializing the `AltCLIPTextConfig` with default values.""" ) if vision_config is None: snake_case_ :List[str] = {} logger.info("""`vision_config` is `None`. initializing the `AltCLIPVisionConfig` with default values.""" ) snake_case_ :Tuple = AltCLIPTextConfig(**snake_case ) snake_case_ :str = AltCLIPVisionConfig(**snake_case ) snake_case_ :int = projection_dim snake_case_ :Union[str, Any] = logit_scale_init_value snake_case_ :Dict = 1.0 @classmethod def lowerCAmelCase_ ( cls: str , snake_case: AltCLIPTextConfig , snake_case: AltCLIPVisionConfig , **snake_case: int ) -> Union[str, Any]: return cls(text_config=text_config.to_dict() , vision_config=vision_config.to_dict() , **snake_case ) def lowerCAmelCase_ ( self: int ) -> Tuple: snake_case_ :str = copy.deepcopy(self.__dict__ ) snake_case_ :Any = self.text_config.to_dict() snake_case_ :Optional[int] = self.vision_config.to_dict() snake_case_ :List[Any] = self.__class__.model_type return output
66
"""simple docstring""" import collections import inspect import unittest from transformers import SwinvaConfig 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, _config_zero_init, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import SwinvaForImageClassification, SwinvaForMaskedImageModeling, SwinvaModel from transformers.models.swinva.modeling_swinva import SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class lowerCamelCase : '''simple docstring''' def __init__( self: Optional[int] , snake_case: Any , snake_case: Optional[Any]=13 , snake_case: Tuple=32 , snake_case: Optional[int]=2 , snake_case: Tuple=3 , snake_case: Tuple=16 , snake_case: Optional[Any]=[1, 2, 1] , snake_case: Optional[int]=[2, 2, 4] , snake_case: Optional[int]=2 , snake_case: int=2.0 , snake_case: Union[str, Any]=True , snake_case: List[str]=0.0 , snake_case: List[Any]=0.0 , snake_case: Optional[Any]=0.1 , snake_case: List[Any]="gelu" , snake_case: Optional[int]=False , snake_case: Union[str, Any]=True , snake_case: Union[str, Any]=0.0_2 , snake_case: Optional[int]=1E-5 , snake_case: Optional[Any]=True , snake_case: List[Any]=None , snake_case: List[Any]=True , snake_case: Optional[Any]=10 , snake_case: str=8 , ) -> Tuple: snake_case_ :Dict = parent snake_case_ :Any = batch_size snake_case_ :List[Any] = image_size snake_case_ :List[Any] = patch_size snake_case_ :int = num_channels snake_case_ :Tuple = embed_dim snake_case_ :str = depths snake_case_ :str = num_heads snake_case_ :Optional[int] = window_size snake_case_ :Tuple = mlp_ratio snake_case_ :Any = qkv_bias snake_case_ :List[Any] = hidden_dropout_prob snake_case_ :Optional[Any] = attention_probs_dropout_prob snake_case_ :Union[str, Any] = drop_path_rate snake_case_ :Any = hidden_act snake_case_ :Optional[Any] = use_absolute_embeddings snake_case_ :Union[str, Any] = patch_norm snake_case_ :Dict = layer_norm_eps snake_case_ :str = initializer_range snake_case_ :Tuple = is_training snake_case_ :Tuple = scope snake_case_ :Union[str, Any] = use_labels snake_case_ :Optional[Any] = type_sequence_label_size snake_case_ :Dict = encoder_stride def lowerCAmelCase_ ( self: int ) -> int: snake_case_ :List[str] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) snake_case_ :Any = None if self.use_labels: snake_case_ :str = ids_tensor([self.batch_size] , self.type_sequence_label_size ) snake_case_ :int = self.get_config() return config, pixel_values, labels def lowerCAmelCase_ ( self: str ) -> Union[str, Any]: return SwinvaConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , embed_dim=self.embed_dim , depths=self.depths , num_heads=self.num_heads , window_size=self.window_size , mlp_ratio=self.mlp_ratio , qkv_bias=self.qkv_bias , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , drop_path_rate=self.drop_path_rate , hidden_act=self.hidden_act , use_absolute_embeddings=self.use_absolute_embeddings , path_norm=self.patch_norm , layer_norm_eps=self.layer_norm_eps , initializer_range=self.initializer_range , encoder_stride=self.encoder_stride , ) def lowerCAmelCase_ ( self: str , snake_case: Optional[int] , snake_case: Dict , snake_case: str ) -> List[Any]: snake_case_ :Union[str, Any] = SwinvaModel(config=snake_case ) model.to(snake_case ) model.eval() snake_case_ :Optional[int] = model(snake_case ) snake_case_ :Optional[Any] = ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths ) - 1)) snake_case_ :int = int(config.embed_dim * 2 ** (len(config.depths ) - 1) ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, expected_seq_len, expected_dim) ) def lowerCAmelCase_ ( self: int , snake_case: List[str] , snake_case: Tuple , snake_case: int ) -> Any: snake_case_ :Dict = SwinvaForMaskedImageModeling(config=snake_case ) model.to(snake_case ) model.eval() snake_case_ :Tuple = model(snake_case ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_channels, self.image_size, self.image_size) ) # test greyscale images snake_case_ :List[Any] = 1 snake_case_ :int = SwinvaForMaskedImageModeling(snake_case ) model.to(snake_case ) model.eval() snake_case_ :Union[str, Any] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) snake_case_ :int = model(snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, 1, self.image_size, self.image_size) ) def lowerCAmelCase_ ( self: List[Any] , snake_case: Any , snake_case: List[str] , snake_case: Union[str, Any] ) -> Tuple: snake_case_ :int = self.type_sequence_label_size snake_case_ :List[Any] = SwinvaForImageClassification(snake_case ) model.to(snake_case ) model.eval() snake_case_ :Dict = model(snake_case , labels=snake_case ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def lowerCAmelCase_ ( self: int ) -> str: snake_case_ :Any = self.prepare_config_and_inputs() snake_case_, snake_case_, snake_case_ :List[str] = config_and_inputs snake_case_ :List[Any] = {"""pixel_values""": pixel_values} return config, inputs_dict @require_torch class lowerCamelCase ( _lowerCAmelCase , _lowerCAmelCase , unittest.TestCase ): '''simple docstring''' _A : Optional[Any] = ( (SwinvaModel, SwinvaForImageClassification, SwinvaForMaskedImageModeling) if is_torch_available() else () ) _A : Any = ( {"""feature-extraction""": SwinvaModel, """image-classification""": SwinvaForImageClassification} if is_torch_available() else {} ) _A : List[Any] = False _A : List[str] = False _A : Tuple = False _A : List[str] = False def lowerCAmelCase_ ( self: Dict ) -> List[Any]: snake_case_ :Optional[int] = SwinvaModelTester(self ) snake_case_ :List[str] = ConfigTester(self , config_class=snake_case , embed_dim=37 ) def lowerCAmelCase_ ( self: Union[str, Any] ) -> List[Any]: self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() def lowerCAmelCase_ ( self: Union[str, Any] ) -> Tuple: snake_case_ :List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*snake_case ) @unittest.skip(reason="""Got `CUDA error: misaligned address` with PyTorch 2.0.0.""" ) def lowerCAmelCase_ ( self: Union[str, Any] ) -> str: pass @unittest.skip(reason="""Swinv2 does not use inputs_embeds""" ) def lowerCAmelCase_ ( self: int ) -> Dict: pass def lowerCAmelCase_ ( self: List[str] ) -> Union[str, Any]: snake_case_, snake_case_ :List[str] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: snake_case_ :Optional[int] = model_class(snake_case ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) snake_case_ :List[Any] = model.get_output_embeddings() self.assertTrue(x is None or isinstance(snake_case , nn.Linear ) ) def lowerCAmelCase_ ( self: Dict ) -> Optional[int]: snake_case_, snake_case_ :Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: snake_case_ :Optional[int] = model_class(snake_case ) snake_case_ :List[Any] = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic snake_case_ :int = [*signature.parameters.keys()] snake_case_ :List[Any] = ["""pixel_values"""] self.assertListEqual(arg_names[:1] , snake_case ) def lowerCAmelCase_ ( self: List[str] ) -> Optional[Any]: snake_case_, snake_case_ :List[str] = self.model_tester.prepare_config_and_inputs_for_common() snake_case_ :List[str] = True for model_class in self.all_model_classes: snake_case_ :List[Any] = True snake_case_ :Any = False snake_case_ :Optional[int] = True snake_case_ :Tuple = model_class(snake_case ) model.to(snake_case ) model.eval() with torch.no_grad(): snake_case_ :Any = model(**self._prepare_for_class(snake_case , snake_case ) ) snake_case_ :str = outputs.attentions snake_case_ :Dict = len(self.model_tester.depths ) self.assertEqual(len(snake_case ) , snake_case ) # check that output_attentions also work using config del inputs_dict["output_attentions"] snake_case_ :Union[str, Any] = True snake_case_ :Tuple = config.window_size**2 snake_case_ :Any = model_class(snake_case ) model.to(snake_case ) model.eval() with torch.no_grad(): snake_case_ :Union[str, Any] = model(**self._prepare_for_class(snake_case , snake_case ) ) snake_case_ :int = outputs.attentions self.assertEqual(len(snake_case ) , snake_case ) self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_heads[0], window_size_squared, window_size_squared] , ) snake_case_ :Any = len(snake_case ) # Check attention is always last and order is fine snake_case_ :int = True snake_case_ :Dict = True snake_case_ :Optional[int] = model_class(snake_case ) model.to(snake_case ) model.eval() with torch.no_grad(): snake_case_ :Dict = model(**self._prepare_for_class(snake_case , snake_case ) ) if hasattr(self.model_tester , """num_hidden_states_types""" ): snake_case_ :Any = self.model_tester.num_hidden_states_types else: # also another +1 for reshaped_hidden_states snake_case_ :int = 2 self.assertEqual(out_len + added_hidden_states , len(snake_case ) ) snake_case_ :str = outputs.attentions self.assertEqual(len(snake_case ) , snake_case ) self.assertListEqual( list(self_attentions[0].shape[-3:] ) , [self.model_tester.num_heads[0], window_size_squared, window_size_squared] , ) def lowerCAmelCase_ ( self: int , snake_case: Dict , snake_case: Dict , snake_case: Optional[Any] , snake_case: Dict ) -> List[str]: snake_case_ :Dict = model_class(snake_case ) model.to(snake_case ) model.eval() with torch.no_grad(): snake_case_ :Optional[int] = model(**self._prepare_for_class(snake_case , snake_case ) ) snake_case_ :str = outputs.hidden_states snake_case_ :List[Any] = getattr( self.model_tester , """expected_num_hidden_layers""" , len(self.model_tester.depths ) + 1 ) self.assertEqual(len(snake_case ) , snake_case ) # Swinv2 has a different seq_length snake_case_ :List[Any] = ( config.patch_size if isinstance(config.patch_size , collections.abc.Iterable ) else (config.patch_size, config.patch_size) ) snake_case_ :Optional[int] = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0]) self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [num_patches, self.model_tester.embed_dim] , ) snake_case_ :str = outputs.reshaped_hidden_states self.assertEqual(len(snake_case ) , snake_case ) snake_case_, snake_case_, snake_case_, snake_case_ :Any = reshaped_hidden_states[0].shape snake_case_ :int = ( reshaped_hidden_states[0].view(snake_case , snake_case , height * width ).permute(0 , 2 , 1 ) ) self.assertListEqual( list(reshaped_hidden_states.shape[-2:] ) , [num_patches, self.model_tester.embed_dim] , ) def lowerCAmelCase_ ( self: Any ) -> Any: snake_case_, snake_case_ :List[Any] = self.model_tester.prepare_config_and_inputs_for_common() snake_case_ :Union[str, Any] = ( self.model_tester.image_size if isinstance(self.model_tester.image_size , collections.abc.Iterable ) else (self.model_tester.image_size, self.model_tester.image_size) ) for model_class in self.all_model_classes: snake_case_ :Union[str, Any] = True self.check_hidden_states_output(snake_case , snake_case , snake_case , snake_case ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] snake_case_ :List[str] = True self.check_hidden_states_output(snake_case , snake_case , snake_case , snake_case ) def lowerCAmelCase_ ( self: Tuple ) -> Any: snake_case_, snake_case_ :Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() snake_case_ :Optional[int] = 3 snake_case_ :Union[str, Any] = ( self.model_tester.image_size if isinstance(self.model_tester.image_size , collections.abc.Iterable ) else (self.model_tester.image_size, self.model_tester.image_size) ) snake_case_ :str = ( config.patch_size if isinstance(config.patch_size , collections.abc.Iterable ) else (config.patch_size, config.patch_size) ) snake_case_ :Any = image_size[0] + patch_size[0] - (image_size[0] % patch_size[0]) snake_case_ :int = image_size[1] + patch_size[1] - (image_size[1] % patch_size[1]) for model_class in self.all_model_classes: snake_case_ :str = True self.check_hidden_states_output(snake_case , snake_case , snake_case , (padded_height, padded_width) ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] snake_case_ :Tuple = True self.check_hidden_states_output(snake_case , snake_case , snake_case , (padded_height, padded_width) ) def lowerCAmelCase_ ( self: Any ) -> Tuple: snake_case_ :int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_image_modeling(*snake_case ) def lowerCAmelCase_ ( self: Optional[int] ) -> Dict: snake_case_ :Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*snake_case ) @slow def lowerCAmelCase_ ( self: List[Any] ) -> Dict: for model_name in SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: snake_case_ :List[str] = SwinvaModel.from_pretrained(snake_case ) self.assertIsNotNone(snake_case ) def lowerCAmelCase_ ( self: Optional[int] ) -> List[Any]: snake_case_, snake_case_ :str = self.model_tester.prepare_config_and_inputs_for_common() snake_case_ :Optional[int] = _config_zero_init(snake_case ) for model_class in self.all_model_classes: snake_case_ :Tuple = model_class(config=snake_case ) for name, param in model.named_parameters(): if "embeddings" not in name and "logit_scale" not in name and param.requires_grad: self.assertIn( ((param.data.mean() * 1E9).round() / 1E9).item() , [0.0, 1.0] , msg=f"""Parameter {name} of model {model_class} seems not properly initialized""" , ) @require_vision @require_torch class lowerCamelCase ( unittest.TestCase ): '''simple docstring''' @cached_property def lowerCAmelCase_ ( self: Optional[int] ) -> List[Any]: return ( AutoImageProcessor.from_pretrained("""microsoft/swinv2-tiny-patch4-window8-256""" ) if is_vision_available() else None ) @slow def lowerCAmelCase_ ( self: List[str] ) -> List[str]: snake_case_ :Tuple = SwinvaForImageClassification.from_pretrained("""microsoft/swinv2-tiny-patch4-window8-256""" ).to( snake_case ) snake_case_ :str = self.default_image_processor snake_case_ :List[Any] = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) snake_case_ :str = image_processor(images=snake_case , return_tensors="""pt""" ).to(snake_case ) # forward pass with torch.no_grad(): snake_case_ :Tuple = model(**snake_case ) # verify the logits snake_case_ :Dict = torch.Size((1, 1_000) ) self.assertEqual(outputs.logits.shape , snake_case ) snake_case_ :int = torch.tensor([-0.3_9_4_7, -0.4_3_0_6, 0.0_0_2_6] ).to(snake_case ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , snake_case , atol=1E-4 ) )
66
1
'''simple docstring''' def lowerCamelCase__ ( __lowerCamelCase : int , __lowerCamelCase : int ): '''simple docstring''' return x if y == 0 else greatest_common_divisor(UpperCAmelCase__ , x % y ) def lowerCamelCase__ ( __lowerCamelCase : int , __lowerCamelCase : int ): '''simple docstring''' return (x * y) // greatest_common_divisor(UpperCAmelCase__ , UpperCAmelCase__ ) def lowerCamelCase__ ( __lowerCamelCase : int = 2_0 ): '''simple docstring''' _UpperCAmelCase : Dict =1 for i in range(1 , n + 1 ): _UpperCAmelCase : Optional[int] =lcm(UpperCAmelCase__ , UpperCAmelCase__ ) return g if __name__ == "__main__": print(F"""{solution() = }""")
371
'''simple docstring''' import shutil import tempfile import unittest import numpy as np import pytest from transformers import is_speech_available, is_vision_available from transformers.testing_utils import require_torch if is_vision_available(): from transformers import TvltImageProcessor if is_speech_available(): from transformers import TvltFeatureExtractor from transformers import TvltProcessor @require_torch class __magic_name__ ( unittest.TestCase ): def lowerCAmelCase ( self) -> str: '''simple docstring''' _UpperCAmelCase : Dict ='ZinengTang/tvlt-base' _UpperCAmelCase : Dict =tempfile.mkdtemp() def lowerCAmelCase ( self , **snake_case) -> Union[str, Any]: '''simple docstring''' return TvltImageProcessor.from_pretrained(self.checkpoint , **snake_case) def lowerCAmelCase ( self , **snake_case) -> Dict: '''simple docstring''' return TvltFeatureExtractor.from_pretrained(self.checkpoint , **snake_case) def lowerCAmelCase ( self) -> Any: '''simple docstring''' shutil.rmtree(self.tmpdirname) def lowerCAmelCase ( self) -> Optional[int]: '''simple docstring''' _UpperCAmelCase : Any =self.get_image_processor() _UpperCAmelCase : Optional[Any] =self.get_feature_extractor() _UpperCAmelCase : str =TvltProcessor(image_processor=snake_case , feature_extractor=snake_case) processor.save_pretrained(self.tmpdirname) _UpperCAmelCase : str =TvltProcessor.from_pretrained(self.tmpdirname) self.assertIsInstance(processor.feature_extractor , snake_case) self.assertIsInstance(processor.image_processor , snake_case) def lowerCAmelCase ( self) -> Union[str, Any]: '''simple docstring''' _UpperCAmelCase : str =self.get_image_processor() _UpperCAmelCase : List[Any] =self.get_feature_extractor() _UpperCAmelCase : str =TvltProcessor(image_processor=snake_case , feature_extractor=snake_case) _UpperCAmelCase : Optional[int] =np.ones([1_2_0_0_0]) _UpperCAmelCase : str =feature_extractor(snake_case , return_tensors='np') _UpperCAmelCase : Dict =processor(audio=snake_case , return_tensors='np') for key in audio_dict.keys(): self.assertAlmostEqual(audio_dict[key].sum() , input_processor[key].sum() , delta=1E-2) def lowerCAmelCase ( self) -> int: '''simple docstring''' _UpperCAmelCase : Dict =self.get_image_processor() _UpperCAmelCase : int =self.get_feature_extractor() _UpperCAmelCase : int =TvltProcessor(image_processor=snake_case , feature_extractor=snake_case) _UpperCAmelCase : Union[str, Any] =np.ones([3, 2_2_4, 2_2_4]) _UpperCAmelCase : Optional[Any] =image_processor(snake_case , return_tensors='np') _UpperCAmelCase : List[Any] =processor(images=snake_case , return_tensors='np') for key in image_dict.keys(): self.assertAlmostEqual(image_dict[key].sum() , input_processor[key].sum() , delta=1E-2) def lowerCAmelCase ( self) -> List[str]: '''simple docstring''' _UpperCAmelCase : List[str] =self.get_image_processor() _UpperCAmelCase : Dict =self.get_feature_extractor() _UpperCAmelCase : Optional[int] =TvltProcessor(image_processor=snake_case , feature_extractor=snake_case) _UpperCAmelCase : Optional[int] =np.ones([1_2_0_0_0]) _UpperCAmelCase : str =np.ones([3, 2_2_4, 2_2_4]) _UpperCAmelCase : Optional[int] =processor(audio=snake_case , images=snake_case) self.assertListEqual(list(inputs.keys()) , ['audio_values', 'audio_mask', 'pixel_values', 'pixel_mask']) # test if it raises when no input is passed with pytest.raises(snake_case): processor() def lowerCAmelCase ( self) -> Optional[int]: '''simple docstring''' _UpperCAmelCase : Optional[int] =self.get_image_processor() _UpperCAmelCase : Tuple =self.get_feature_extractor() _UpperCAmelCase : Dict =TvltProcessor(image_processor=snake_case , feature_extractor=snake_case) self.assertListEqual( processor.model_input_names , image_processor.model_input_names + feature_extractor.model_input_names , msg='`processor` and `image_processor`+`feature_extractor` model input names do not match' , )
242
0
'''simple docstring''' from typing import List, Optional import numpy as np from ...processing_utils import ProcessorMixin from ...utils import to_numpy class a_ ( snake_case_ ): '''simple docstring''' UpperCamelCase = '''EncodecFeatureExtractor''' UpperCamelCase = ('''T5Tokenizer''', '''T5TokenizerFast''') def __init__( self , A , A ) -> Tuple: super().__init__(A , A ) _SCREAMING_SNAKE_CASE = self.feature_extractor _SCREAMING_SNAKE_CASE = False def snake_case_( self , A=None , A=None , A=True ) -> Dict: return self.tokenizer.get_decoder_prompt_ids(task=A , language=A , no_timestamps=A ) def __call__( self , *A , **A ) -> Optional[Any]: # For backward compatibility if self._in_target_context_manager: return self.current_processor(*A , **A ) _SCREAMING_SNAKE_CASE = kwargs.pop("""audio""" , A ) _SCREAMING_SNAKE_CASE = kwargs.pop("""sampling_rate""" , A ) _SCREAMING_SNAKE_CASE = kwargs.pop("""text""" , A ) if len(A ) > 0: _SCREAMING_SNAKE_CASE = args[0] _SCREAMING_SNAKE_CASE = args[1:] if audio is None and text is None: raise ValueError("""You need to specify either an `audio` or `text` input to process.""" ) if text is not None: _SCREAMING_SNAKE_CASE = self.tokenizer(A , **A ) if audio is not None: _SCREAMING_SNAKE_CASE = self.feature_extractor(A , *A , sampling_rate=A , **A ) if audio is None: return inputs elif text is None: return audio_inputs else: _SCREAMING_SNAKE_CASE = audio_inputs["""input_values"""] if "padding_mask" in audio_inputs: _SCREAMING_SNAKE_CASE = audio_inputs["""padding_mask"""] return inputs def snake_case_( self , *A , **A ) -> Dict: _SCREAMING_SNAKE_CASE = kwargs.pop("""audio""" , A ) _SCREAMING_SNAKE_CASE = kwargs.pop("""padding_mask""" , A ) if len(A ) > 0: _SCREAMING_SNAKE_CASE = args[0] _SCREAMING_SNAKE_CASE = args[1:] if audio_values is not None: return self._decode_audio(A , padding_mask=A ) else: return self.tokenizer.batch_decode(*A , **A ) def snake_case_( self , *A , **A ) -> Dict: return self.tokenizer.decode(*A , **A ) def snake_case_( self , A , A = None ) -> List[np.ndarray]: _SCREAMING_SNAKE_CASE = to_numpy(A ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = audio_values.shape if padding_mask is None: return list(A ) _SCREAMING_SNAKE_CASE = to_numpy(A ) # match the sequence length of the padding mask to the generated audio arrays by padding with the **non-padding** # token (so that the generated audio values are **not** treated as padded tokens) _SCREAMING_SNAKE_CASE = seq_len - padding_mask.shape[-1] _SCREAMING_SNAKE_CASE = 1 - self.feature_extractor.padding_value _SCREAMING_SNAKE_CASE = np.pad(A , ((0, 0), (0, difference)) , """constant""" , constant_values=A ) _SCREAMING_SNAKE_CASE = audio_values.tolist() for i in range(A ): _SCREAMING_SNAKE_CASE = np.asarray(audio_values[i] )[ padding_mask[i][None, :] != self.feature_extractor.padding_value ] _SCREAMING_SNAKE_CASE = sliced_audio.reshape(A , -1 ) return audio_values
58
import argparse import os import torch from transformers import ( XLNetConfig, XLNetForQuestionAnswering, XLNetForSequenceClassification, XLNetLMHeadModel, load_tf_weights_in_xlnet, ) from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging UpperCAmelCase : int = { """cola""": 2, """mnli""": 3, """mrpc""": 2, """sst-2""": 2, """sts-b""": 1, """qqp""": 2, """qnli""": 2, """rte""": 2, """wnli""": 2, } logging.set_verbosity_info() def _A ( SCREAMING_SNAKE_CASE : List[Any] , SCREAMING_SNAKE_CASE : Dict , SCREAMING_SNAKE_CASE : Dict , SCREAMING_SNAKE_CASE : Union[str, Any]=None ): """simple docstring""" a__ : Optional[int] =XLNetConfig.from_json_file(SCREAMING_SNAKE_CASE ) a__ : Dict =finetuning_task.lower() if finetuning_task is not None else "" if finetuning_task in GLUE_TASKS_NUM_LABELS: print(f'''Building PyTorch XLNetForSequenceClassification model from configuration: {config}''' ) a__ : List[str] =finetuning_task a__ : Tuple =GLUE_TASKS_NUM_LABELS[finetuning_task] a__ : List[Any] =XLNetForSequenceClassification(SCREAMING_SNAKE_CASE ) elif "squad" in finetuning_task: a__ : Optional[int] =finetuning_task a__ : Dict =XLNetForQuestionAnswering(SCREAMING_SNAKE_CASE ) else: a__ : List[Any] =XLNetLMHeadModel(SCREAMING_SNAKE_CASE ) # Load weights from tf checkpoint load_tf_weights_in_xlnet(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) # Save pytorch-model a__ : Dict =os.path.join(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) a__ : Dict =os.path.join(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) print(f'''Save PyTorch model to {os.path.abspath(SCREAMING_SNAKE_CASE )}''' ) torch.save(model.state_dict() , SCREAMING_SNAKE_CASE ) print(f'''Save configuration file to {os.path.abspath(SCREAMING_SNAKE_CASE )}''' ) with open(SCREAMING_SNAKE_CASE , "w" , encoding="utf-8" ) as f: f.write(config.to_json_string() ) if __name__ == "__main__": UpperCAmelCase : List[str] = argparse.ArgumentParser() # Required parameters parser.add_argument( """--tf_checkpoint_path""", default=None, type=str, required=True, help="""Path to the TensorFlow checkpoint path.""" ) parser.add_argument( """--xlnet_config_file""", default=None, type=str, required=True, help=( """The config json file corresponding to the pre-trained XLNet model. \n""" """This specifies the model architecture.""" ), ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, required=True, help="""Path to the folder to store the PyTorch model or dataset/vocab.""", ) parser.add_argument( """--finetuning_task""", default=None, type=str, help="""Name of a task on which the XLNet TensorFlow model was fine-tuned""", ) UpperCAmelCase : int = parser.parse_args() print(args) convert_xlnet_checkpoint_to_pytorch( args.tf_checkpoint_path, args.xlnet_config_file, args.pytorch_dump_folder_path, args.finetuning_task )
95
0
"""simple docstring""" # Copyright 2023 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available UpperCAmelCase_ : List[Any] = { '''configuration_vivit''': ['''VIVIT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''VivitConfig'''], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase_ : Optional[Any] = ['''VivitImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase_ : Optional[Any] = [ '''VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''VivitModel''', '''VivitPreTrainedModel''', '''VivitForVideoClassification''', ] if TYPE_CHECKING: from .configuration_vivit import VIVIT_PRETRAINED_CONFIG_ARCHIVE_MAP, VivitConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_vivit import VivitImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vivit import ( VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST, VivitForVideoClassification, VivitModel, VivitPreTrainedModel, ) else: import sys UpperCAmelCase_ : Optional[Any] = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
367
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available UpperCAmelCase_ : Union[str, Any] = {'''configuration_swin''': ['''SWIN_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''SwinConfig''', '''SwinOnnxConfig''']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase_ : Dict = [ '''SWIN_PRETRAINED_MODEL_ARCHIVE_LIST''', '''SwinForImageClassification''', '''SwinForMaskedImageModeling''', '''SwinModel''', '''SwinPreTrainedModel''', '''SwinBackbone''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase_ : Union[str, Any] = [ '''TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFSwinForImageClassification''', '''TFSwinForMaskedImageModeling''', '''TFSwinModel''', '''TFSwinPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_swin import SWIN_PRETRAINED_CONFIG_ARCHIVE_MAP, SwinConfig, SwinOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_swin import ( SWIN_PRETRAINED_MODEL_ARCHIVE_LIST, SwinBackbone, SwinForImageClassification, SwinForMaskedImageModeling, SwinModel, SwinPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_swin import ( TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST, TFSwinForImageClassification, TFSwinForMaskedImageModeling, TFSwinModel, TFSwinPreTrainedModel, ) else: import sys UpperCAmelCase_ : str = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
62
0
"""simple docstring""" from typing import Callable, Dict, Optional, Tuple import torch from torch import nn from torch.distributions import ( AffineTransform, Distribution, Independent, NegativeBinomial, Normal, StudentT, TransformedDistribution, ) class _UpperCAmelCase ( lowercase_ ): def __init__( self :int , __UpperCamelCase :Distribution , __UpperCamelCase :Dict=None , __UpperCamelCase :Optional[int]=None , __UpperCamelCase :List[str]=0 ): A = 1.0 if scale is None else scale A = 0.0 if loc is None else loc super().__init__(__UpperCamelCase , [AffineTransform(loc=self.loc , scale=self.scale , event_dim=__UpperCamelCase )] ) @property def lowerCamelCase ( self :Any ): return self.base_dist.mean * self.scale + self.loc @property def lowerCamelCase ( self :Optional[int] ): return self.base_dist.variance * self.scale**2 @property def lowerCamelCase ( self :Dict ): return self.variance.sqrt() class _UpperCAmelCase ( nn.Module ): def __init__( self :Dict , __UpperCamelCase :int , __UpperCamelCase :Dict[str, int] , __UpperCamelCase :Callable[..., Tuple[torch.Tensor]] , **__UpperCamelCase :str ): super().__init__(**__UpperCamelCase ) A = args_dim A = nn.ModuleList([nn.Linear(__UpperCamelCase , __UpperCamelCase ) for dim in args_dim.values()] ) A = domain_map def lowerCamelCase ( self :int , __UpperCamelCase :torch.Tensor ): A = [proj(__UpperCamelCase ) for proj in self.proj] return self.domain_map(*__UpperCamelCase ) class _UpperCAmelCase ( nn.Module ): def __init__( self :Dict , __UpperCamelCase :int ): super().__init__() A = function def lowerCamelCase ( self :List[str] , __UpperCamelCase :Any , *__UpperCamelCase :Any ): return self.function(__UpperCamelCase , *__UpperCamelCase ) class _UpperCAmelCase : UpperCamelCase = 42 UpperCamelCase = 42 UpperCamelCase = 42 def __init__( self :Any , __UpperCamelCase :int = 1 ): A = dim A = {k: dim * self.args_dim[k] for k in self.args_dim} def lowerCamelCase ( self :List[Any] , __UpperCamelCase :Dict ): if self.dim == 1: return self.distribution_class(*__UpperCamelCase ) else: return Independent(self.distribution_class(*__UpperCamelCase ) , 1 ) def lowerCamelCase ( self :int , __UpperCamelCase :List[str] , __UpperCamelCase :Optional[torch.Tensor] = None , __UpperCamelCase :Optional[torch.Tensor] = None , ): A = self._base_distribution(__UpperCamelCase ) if loc is None and scale is None: return distr else: return AffineTransformed(__UpperCamelCase , loc=__UpperCamelCase , scale=__UpperCamelCase , event_dim=self.event_dim ) @property def lowerCamelCase ( self :List[Any] ): return () if self.dim == 1 else (self.dim,) @property def lowerCamelCase ( self :Tuple ): return len(self.event_shape ) @property def lowerCamelCase ( self :int ): return 0.0 def lowerCamelCase ( self :str , __UpperCamelCase :int ): return ParameterProjection( in_features=__UpperCamelCase , args_dim=self.args_dim , domain_map=LambdaLayer(self.domain_map ) , ) def lowerCamelCase ( self :List[Any] , *__UpperCamelCase :torch.Tensor ): raise NotImplementedError() @staticmethod def lowerCamelCase ( __UpperCamelCase :torch.Tensor ): return (x + torch.sqrt(torch.square(__UpperCamelCase ) + 4.0 )) / 2.0 class _UpperCAmelCase ( lowercase_ ): UpperCamelCase = {"df": 1, "loc": 1, "scale": 1} UpperCamelCase = StudentT @classmethod def lowerCamelCase ( cls :List[str] , __UpperCamelCase :torch.Tensor , __UpperCamelCase :torch.Tensor , __UpperCamelCase :torch.Tensor ): A = cls.squareplus(__UpperCamelCase ).clamp_min(torch.finfo(scale.dtype ).eps ) A = 2.0 + cls.squareplus(__UpperCamelCase ) return df.squeeze(-1 ), loc.squeeze(-1 ), scale.squeeze(-1 ) class _UpperCAmelCase ( lowercase_ ): UpperCamelCase = {"loc": 1, "scale": 1} UpperCamelCase = Normal @classmethod def lowerCamelCase ( cls :List[Any] , __UpperCamelCase :torch.Tensor , __UpperCamelCase :torch.Tensor ): A = cls.squareplus(__UpperCamelCase ).clamp_min(torch.finfo(scale.dtype ).eps ) return loc.squeeze(-1 ), scale.squeeze(-1 ) class _UpperCAmelCase ( lowercase_ ): UpperCamelCase = {"total_count": 1, "logits": 1} UpperCamelCase = NegativeBinomial @classmethod def lowerCamelCase ( cls :str , __UpperCamelCase :torch.Tensor , __UpperCamelCase :torch.Tensor ): A = cls.squareplus(__UpperCamelCase ) return total_count.squeeze(-1 ), logits.squeeze(-1 ) def lowerCamelCase ( self :Tuple , __UpperCamelCase :List[str] ): A, A = distr_args if self.dim == 1: return self.distribution_class(total_count=__UpperCamelCase , logits=__UpperCamelCase ) else: return Independent(self.distribution_class(total_count=__UpperCamelCase , logits=__UpperCamelCase ) , 1 ) def lowerCamelCase ( self :List[str] , __UpperCamelCase :str , __UpperCamelCase :Optional[torch.Tensor] = None , __UpperCamelCase :Optional[torch.Tensor] = None ): A, A = distr_args if scale is not None: # See scaling property of Gamma. logits += scale.log() return self._base_distribution((total_count, logits) )
292
"""simple docstring""" def A__ ( UpperCamelCase , UpperCamelCase = False ): if not isinstance(UpperCamelCase , UpperCamelCase ): A = F"Expected string as input, found {type(UpperCamelCase )}" raise ValueError(UpperCamelCase ) if not isinstance(UpperCamelCase , UpperCamelCase ): A = F"Expected boolean as use_pascal parameter, found {type(UpperCamelCase )}" raise ValueError(UpperCamelCase ) A = input_str.split("_" ) A = 0 if use_pascal else 1 A = words[start_index:] A = [word[0].upper() + word[1:] for word in words_to_capitalize] A = "" if use_pascal else words[0] return "".join([initial_word, *capitalized_words] ) if __name__ == "__main__": from doctest import testmod testmod()
292
1
from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging _snake_case : Optional[Any] = logging.get_logger(__name__) _snake_case : List[str] = { "hustvl/yolos-small": "https://huggingface.co/hustvl/yolos-small/resolve/main/config.json", # See all YOLOS models at https://huggingface.co/models?filter=yolos } class a (_lowerCAmelCase ): """simple docstring""" __UpperCAmelCase : Dict = "yolos" def __init__( self : str , lowerCamelCase : str=768 , lowerCamelCase : List[str]=12 , lowerCamelCase : int=12 , lowerCamelCase : Optional[int]=3072 , lowerCamelCase : List[Any]="gelu" , lowerCamelCase : List[Any]=0.0 , lowerCamelCase : str=0.0 , lowerCamelCase : Tuple=0.02 , lowerCamelCase : List[str]=1E-12 , lowerCamelCase : Optional[int]=[512, 864] , lowerCamelCase : int=16 , lowerCamelCase : int=3 , lowerCamelCase : Optional[int]=True , lowerCamelCase : List[Any]=100 , lowerCamelCase : Optional[Any]=True , lowerCamelCase : List[Any]=False , lowerCamelCase : List[Any]=1 , lowerCamelCase : Any=5 , lowerCamelCase : Tuple=2 , lowerCamelCase : Dict=5 , lowerCamelCase : Any=2 , lowerCamelCase : str=0.1 , **lowerCamelCase : Tuple , ) -> List[Any]: super().__init__(**lowerCamelCase ) __snake_case : Optional[int] = hidden_size __snake_case : Optional[Any] = num_hidden_layers __snake_case : Union[str, Any] = num_attention_heads __snake_case : str = intermediate_size __snake_case : Any = hidden_act __snake_case : Any = hidden_dropout_prob __snake_case : Any = attention_probs_dropout_prob __snake_case : Union[str, Any] = initializer_range __snake_case : List[str] = layer_norm_eps __snake_case : Union[str, Any] = image_size __snake_case : Union[str, Any] = patch_size __snake_case : List[str] = num_channels __snake_case : List[Any] = qkv_bias __snake_case : List[str] = num_detection_tokens __snake_case : List[str] = use_mid_position_embeddings __snake_case : str = auxiliary_loss # Hungarian matcher __snake_case : List[str] = class_cost __snake_case : Union[str, Any] = bbox_cost __snake_case : Optional[Any] = giou_cost # Loss coefficients __snake_case : Optional[Any] = bbox_loss_coefficient __snake_case : Optional[Any] = giou_loss_coefficient __snake_case : Tuple = eos_coefficient class a (_lowerCAmelCase ): """simple docstring""" __UpperCAmelCase : Any = version.parse("1.11" ) @property def __snake_case ( self : Optional[int] ) -> Union[str, Any]: return OrderedDict( [ ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}), ] ) @property def __snake_case ( self : Dict ) -> int: return 1E-4 @property def __snake_case ( self : List[Any] ) -> int: return 12
362
from __future__ import annotations _snake_case : Union[str, Any] = [] def lowerCAmelCase_ ( __lowerCamelCase , __lowerCamelCase , __lowerCamelCase ): for i in range(len(__lowerCamelCase ) ): if board[row][i] == 1: return False for i in range(len(__lowerCamelCase ) ): if board[i][column] == 1: return False for i, j in zip(range(__lowerCamelCase , -1 , -1 ) , range(__lowerCamelCase , -1 , -1 ) ): if board[i][j] == 1: return False for i, j in zip(range(__lowerCamelCase , -1 , -1 ) , range(__lowerCamelCase , len(__lowerCamelCase ) ) ): if board[i][j] == 1: return False return True def lowerCAmelCase_ ( __lowerCamelCase , __lowerCamelCase ): if row >= len(__lowerCamelCase ): solution.append(__lowerCamelCase ) printboard(__lowerCamelCase ) print() return True for i in range(len(__lowerCamelCase ) ): if is_safe(__lowerCamelCase , __lowerCamelCase , __lowerCamelCase ): __snake_case : Optional[Any] = 1 solve(__lowerCamelCase , row + 1 ) __snake_case : Union[str, Any] = 0 return False def lowerCAmelCase_ ( __lowerCamelCase ): for i in range(len(__lowerCamelCase ) ): for j in range(len(__lowerCamelCase ) ): if board[i][j] == 1: print("Q" , end=" " ) else: print("." , end=" " ) print() # n=int(input("The no. of queens")) _snake_case : List[str] = 8 _snake_case : Optional[int] = [[0 for i in range(n)] for j in range(n)] solve(board, 0) print("The total no. of solutions are :", len(solution))
134
0
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__ : List[str] = [ '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 UpperCamelCase__ (unittest.TestCase ): '''simple docstring''' def _lowercase ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = None , UpperCamelCase__ = None ) -> str: lowerCamelCase : Tuple = None lowerCamelCase : str = os.path.abspath(os.path.join("examples" , "by_feature" ) ) lowerCamelCase : str = os.path.abspath("examples" ) for item in os.listdir(UpperCamelCase__ ): if item not in EXCLUDE_EXAMPLES: lowerCamelCase : List[str] = 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()" , ): lowerCamelCase : Any = compare_against_test( os.path.join(UpperCamelCase__ , UpperCamelCase__ ) , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) lowerCamelCase : Dict = "\n".join(UpperCamelCase__ ) if special_strings is not None: for string in special_strings: lowerCamelCase : Tuple = diff.replace(UpperCamelCase__ , "" ) self.assertEqual(UpperCamelCase__ , "" ) def _lowercase ( self ) -> List[Any]: self.one_complete_example("complete_nlp_example.py" , UpperCamelCase__ ) self.one_complete_example("complete_nlp_example.py" , UpperCamelCase__ ) def _lowercase ( self ) -> Union[str, Any]: lowerCamelCase : Optional[Any] = os.path.abspath(os.path.join("examples" , "cv_example.py" ) ) lowerCamelCase : Tuple = [ " " * 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 UpperCamelCase__ (lowerCAmelCase__ ): '''simple docstring''' lowerCamelCase_ : Union[str, Any] = False @classmethod def _lowercase ( cls ) -> str: super().setUpClass() lowerCamelCase : str = tempfile.mkdtemp() lowerCamelCase : Tuple = os.path.join(cls._tmpdir , "default_config.yml" ) write_basic_config(save_location=cls.configPath ) lowerCamelCase : Optional[Any] = ["accelerate", "launch", "--config_file", cls.configPath] @classmethod def _lowercase ( cls ) -> List[Any]: super().tearDownClass() shutil.rmtree(cls._tmpdir ) def _lowercase ( self ) -> Union[str, Any]: lowerCamelCase : Union[str, Any] = 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 _lowercase ( self ) -> List[Any]: lowerCamelCase : Optional[int] = F''' examples/by_feature/checkpointing.py --checkpointing_steps 1 --output_dir {self.tmpdir} '''.split() lowerCamelCase : Optional[Any] = run_command(self._launch_args + testargs ) self.assertTrue(os.path.exists(os.path.join(self.tmpdir , "step_2" ) ) ) def _lowercase ( self ) -> Any: lowerCamelCase : Optional[int] = F''' examples/by_feature/checkpointing.py --resume_from_checkpoint {os.path.join(self.tmpdir , "epoch_0" )} '''.split() lowerCamelCase : List[Any] = run_command(self._launch_args + testargs , return_stdout=UpperCamelCase__ ) self.assertNotIn("epoch 0:" , UpperCamelCase__ ) self.assertIn("epoch 1:" , UpperCamelCase__ ) def _lowercase ( self ) -> str: lowerCamelCase : int = F''' examples/by_feature/checkpointing.py --resume_from_checkpoint {os.path.join(self.tmpdir , "step_2" )} '''.split() lowerCamelCase : Optional[int] = run_command(self._launch_args + testargs , return_stdout=UpperCamelCase__ ) if torch.cuda.is_available(): lowerCamelCase : Optional[Any] = torch.cuda.device_count() else: lowerCamelCase : str = 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 _lowercase ( self ) -> Tuple: lowerCamelCase : Union[str, Any] = "\n examples/by_feature/cross_validation.py\n --num_folds 2\n ".split() with mock.patch.dict(os.environ , {"TESTING_MOCKED_DATALOADERS": "0"} ): lowerCamelCase : Tuple = run_command(self._launch_args + testargs , return_stdout=UpperCamelCase__ ) lowerCamelCase : Optional[int] = re.findall("({.+})" , UpperCamelCase__ ) lowerCamelCase : Any = [r for r in results if "accuracy" in r][-1] lowerCamelCase : List[str] = ast.literal_eval(UpperCamelCase__ ) self.assertGreaterEqual(results["accuracy"] , 0.75 ) def _lowercase ( self ) -> str: lowerCamelCase : str = ["examples/by_feature/multi_process_metrics.py"] run_command(self._launch_args + testargs ) @require_trackers @mock.patch.dict(os.environ , {"WANDB_MODE": "offline"} ) def _lowercase ( self ) -> Optional[int]: with tempfile.TemporaryDirectory() as tmpdir: lowerCamelCase : Optional[int] = 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 _lowercase ( self ) -> Optional[int]: lowerCamelCase : Union[str, Any] = ["examples/by_feature/gradient_accumulation.py"] run_command(self._launch_args + testargs ) def _lowercase ( self ) -> Any: lowerCamelCase : Tuple = ["examples/by_feature/local_sgd.py"] run_command(self._launch_args + testargs )
48
import colorsys from PIL import Image # type: ignore def _SCREAMING_SNAKE_CASE ( a , a , a ) -> float: __A : List[str] = x __A : str = y for step in range(a ): # noqa: B007 __A : Union[str, Any] = a * a - b * b + x __A : Optional[int] = 2 * a * b + y __A : List[str] = 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 _SCREAMING_SNAKE_CASE ( a ) -> tuple: if distance == 1: return (0, 0, 0) else: return (2_55, 2_55, 2_55) def _SCREAMING_SNAKE_CASE ( a ) -> tuple: if distance == 1: return (0, 0, 0) else: return tuple(round(i * 2_55 ) for i in colorsys.hsv_to_rgb(a , 1 , 1 ) ) def _SCREAMING_SNAKE_CASE ( a = 8_00 , a = 6_00 , a = -0.6 , a = 0 , a = 3.2 , a = 50 , a = True , ) -> Image.Image: __A : str = Image.new('RGB' , (image_width, image_height) ) __A : Dict = img.load() # loop through the image-coordinates for image_x in range(a ): for image_y in range(a ): # determine the figure-coordinates based on the image-coordinates __A : Dict = figure_width / image_width * image_height __A : Union[str, Any] = figure_center_x + (image_x / image_width - 0.5) * figure_width __A : Optional[Any] = figure_center_y + (image_y / image_height - 0.5) * figure_height __A : Union[str, Any] = get_distance(a , a , a ) # color the corresponding pixel based on the selected coloring-function if use_distance_color_coding: __A : Optional[Any] = get_color_coded_rgb(a ) else: __A : Dict = get_black_and_white_rgb(a ) return img if __name__ == "__main__": import doctest doctest.testmod() # colored version, full figure UpperCAmelCase : str = 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()
280
0
import argparse import json from collections import OrderedDict from functools import partial from pathlib import Path import timm import torch from huggingface_hub import hf_hub_download from transformers import LevitConfig, LevitForImageClassificationWithTeacher, LevitImageProcessor from transformers.utils import logging logging.set_verbosity_info() __UpperCamelCase : Union[str, Any] = logging.get_logger() def __A ( __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase = True ) -> int: print(f'Converting {name}...' ) with torch.no_grad(): if hidden_sizes == 128: if name[-1] == "S": a = timm.create_model("""levit_128s""" , pretrained=__lowerCamelCase ) else: a = timm.create_model("""levit_128""" , pretrained=__lowerCamelCase ) if hidden_sizes == 192: a = timm.create_model("""levit_192""" , pretrained=__lowerCamelCase ) if hidden_sizes == 256: a = timm.create_model("""levit_256""" , pretrained=__lowerCamelCase ) if hidden_sizes == 384: a = timm.create_model("""levit_384""" , pretrained=__lowerCamelCase ) from_model.eval() a = LevitForImageClassificationWithTeacher(__lowerCamelCase ).eval() a = OrderedDict() a = from_model.state_dict() a = list(from_model.state_dict().keys() ) a = list(our_model.state_dict().keys() ) print(len(__lowerCamelCase ) , len(__lowerCamelCase ) ) for i in range(len(__lowerCamelCase ) ): a = weights[og_keys[i]] our_model.load_state_dict(__lowerCamelCase ) a = torch.randn((2, 3, 224, 224) ) a = from_model(__lowerCamelCase ) a = our_model(__lowerCamelCase ).logits assert torch.allclose(__lowerCamelCase , __lowerCamelCase ), "The model logits don't match the original one." a = name print(__lowerCamelCase ) if push_to_hub: our_model.save_pretrained(save_directory / checkpoint_name ) a = LevitImageProcessor() image_processor.save_pretrained(save_directory / checkpoint_name ) print(f'Pushed {checkpoint_name}' ) def __A ( __lowerCamelCase , __lowerCamelCase = None , __lowerCamelCase = True ) -> Tuple: a = """imagenet-1k-id2label.json""" a = 1000 a = (1, num_labels) a = """huggingface/label-files""" a = num_labels a = json.load(open(hf_hub_download(__lowerCamelCase , __lowerCamelCase , repo_type="""dataset""" ) , """r""" ) ) a = {int(__lowerCamelCase ): v for k, v in idalabel.items()} a = idalabel a = {v: k for k, v in idalabel.items()} a = partial(__lowerCamelCase , num_labels=__lowerCamelCase , idalabel=__lowerCamelCase , labelaid=__lowerCamelCase ) a = { """levit-128S""": 128, """levit-128""": 128, """levit-192""": 192, """levit-256""": 256, """levit-384""": 384, } a = { """levit-128S""": ImageNetPreTrainedConfig( hidden_sizes=[128, 256, 384] , num_attention_heads=[4, 6, 8] , depths=[2, 3, 4] , key_dim=[16, 16, 16] , drop_path_rate=0 , ), """levit-128""": ImageNetPreTrainedConfig( hidden_sizes=[128, 256, 384] , num_attention_heads=[4, 8, 12] , depths=[4, 4, 4] , key_dim=[16, 16, 16] , drop_path_rate=0 , ), """levit-192""": ImageNetPreTrainedConfig( hidden_sizes=[192, 288, 384] , num_attention_heads=[3, 5, 6] , depths=[4, 4, 4] , key_dim=[32, 32, 32] , drop_path_rate=0 , ), """levit-256""": ImageNetPreTrainedConfig( hidden_sizes=[256, 384, 512] , num_attention_heads=[4, 6, 8] , depths=[4, 4, 4] , key_dim=[32, 32, 32] , drop_path_rate=0 , ), """levit-384""": ImageNetPreTrainedConfig( hidden_sizes=[384, 512, 768] , num_attention_heads=[6, 9, 12] , depths=[4, 4, 4] , key_dim=[32, 32, 32] , drop_path_rate=0.1 , ), } if model_name: convert_weight_and_push( names_to_hidden_sizes[model_name] , __lowerCamelCase , names_to_config[model_name] , __lowerCamelCase , __lowerCamelCase ) else: for model_name, config in names_to_config.items(): convert_weight_and_push(names_to_hidden_sizes[model_name] , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase ) return config, expected_shape if __name__ == "__main__": __UpperCamelCase : List[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( "--model_name", default=None, type=str, help="The name of the model you wish to convert, it must be one of the supported Levit* architecture,", ) parser.add_argument( "--pytorch_dump_folder_path", default="levit-dump-folder/", type=Path, required=False, help="Path to the output PyTorch model directory.", ) parser.add_argument("--push_to_hub", action="store_true", help="Push model and image processor to the hub") parser.add_argument( "--no-push_to_hub", dest="push_to_hub", action="store_false", help="Do not push model and image processor to the hub", ) __UpperCamelCase : Optional[Any] = parser.parse_args() __UpperCamelCase : Path = args.pytorch_dump_folder_path pytorch_dump_folder_path.mkdir(exist_ok=True, parents=True) convert_weights_and_push(pytorch_dump_folder_path, args.model_name, args.push_to_hub)
370
__UpperCamelCase : Dict = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" def __A ( ) -> None: a = input("""Enter message: """ ) a = input("""Enter key [alphanumeric]: """ ) a = input("""Encrypt/Decrypt [e/d]: """ ) if mode.lower().startswith("""e""" ): a = """encrypt""" a = encrypt_message(__lowerCamelCase , __lowerCamelCase ) elif mode.lower().startswith("""d""" ): a = """decrypt""" a = decrypt_message(__lowerCamelCase , __lowerCamelCase ) print(f'\n{mode.title()}ed message:' ) print(__lowerCamelCase ) def __A ( __lowerCamelCase , __lowerCamelCase ) -> str: return translate_message(__lowerCamelCase , __lowerCamelCase , """encrypt""" ) def __A ( __lowerCamelCase , __lowerCamelCase ) -> str: return translate_message(__lowerCamelCase , __lowerCamelCase , """decrypt""" ) def __A ( __lowerCamelCase , __lowerCamelCase , __lowerCamelCase ) -> str: a = [] a = 0 a = key.upper() for symbol in message: a = 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 ): a = 0 else: translated.append(__lowerCamelCase ) return "".join(__lowerCamelCase ) if __name__ == "__main__": main()
347
0
"""simple docstring""" import os import tempfile import unittest from transformers.models.marian.convert_marian_tatoeba_to_pytorch import DEFAULT_REPO, TatoebaConverter from transformers.testing_utils import slow from transformers.utils import cached_property @unittest.skipUnless(os.path.exists(_lowercase ), """Tatoeba directory does not exist.""" ) class snake_case ( unittest.TestCase ): @cached_property def lowercase_ ( self : Optional[Any])-> Optional[int]: '''simple docstring''' __lowerCAmelCase: Any = tempfile.mkdtemp() return TatoebaConverter(save_dir=UpperCamelCase__) @slow def lowercase_ ( self : Union[str, Any])-> Optional[Any]: '''simple docstring''' self.resolver.convert_models(["heb-eng"]) @slow def lowercase_ ( self : List[Any])-> str: '''simple docstring''' __lowerCAmelCase: Dict = self.resolver.write_model_card("opus-mt-he-en" , dry_run=UpperCamelCase__) assert mmeta["long_pair"] == "heb-eng"
217
'''simple docstring''' import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( BertTokenizer, ViltConfig, ViltForImageAndTextRetrieval, ViltForImagesAndTextClassification, ViltForMaskedLM, ViltForQuestionAnswering, ViltImageProcessor, ViltProcessor, ) from transformers.utils import logging logging.set_verbosity_info() _A : List[str] =logging.get_logger(__name__) def SCREAMING_SNAKE_CASE_ (UpperCamelCase , UpperCamelCase=False , UpperCamelCase=False , UpperCamelCase=False ) -> Union[str, Any]: lowerCamelCase__ : str = [] 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 SCREAMING_SNAKE_CASE_ (UpperCamelCase , UpperCamelCase ) -> Optional[Any]: for i in range(config.num_hidden_layers ): lowerCamelCase__ : str = """vilt.""" # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) lowerCamelCase__ : List[str] = state_dict.pop(f'''transformer.blocks.{i}.attn.qkv.weight''' ) lowerCamelCase__ : str = state_dict.pop(f'''transformer.blocks.{i}.attn.qkv.bias''' ) # next, add query, keys and values (in that order) to the state dict lowerCamelCase__ : Union[str, Any] = in_proj_weight[ : config.hidden_size, : ] lowerCamelCase__ : Union[str, Any] = in_proj_bias[: config.hidden_size] lowerCamelCase__ : Optional[int] = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] lowerCamelCase__ : Tuple = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] lowerCamelCase__ : Optional[int] = in_proj_weight[ -config.hidden_size :, : ] lowerCamelCase__ : List[str] = in_proj_bias[-config.hidden_size :] def SCREAMING_SNAKE_CASE_ (UpperCamelCase ) -> Any: lowerCamelCase__ : Dict = ["""head.weight""", """head.bias"""] for k in ignore_keys: state_dict.pop(UpperCamelCase , UpperCamelCase ) def SCREAMING_SNAKE_CASE_ (UpperCamelCase , UpperCamelCase , UpperCamelCase ) -> Dict: lowerCamelCase__ : int = dct.pop(UpperCamelCase ) lowerCamelCase__ : Optional[Any] = val @torch.no_grad() def SCREAMING_SNAKE_CASE_ (UpperCamelCase , UpperCamelCase ) -> Tuple: lowerCamelCase__ : List[str] = ViltConfig(image_size=384 , patch_size=32 , tie_word_embeddings=UpperCamelCase ) lowerCamelCase__ : Union[str, Any] = False lowerCamelCase__ : List[str] = False lowerCamelCase__ : Any = False lowerCamelCase__ : int = False if "vqa" in checkpoint_url: lowerCamelCase__ : List[Any] = True lowerCamelCase__ : Any = 3129 lowerCamelCase__ : Tuple = """huggingface/label-files""" lowerCamelCase__ : List[str] = """vqa2-id2label.json""" lowerCamelCase__ : str = json.load(open(hf_hub_download(UpperCamelCase , UpperCamelCase , repo_type="""dataset""" ) , """r""" ) ) lowerCamelCase__ : Any = {int(UpperCamelCase ): v for k, v in idalabel.items()} lowerCamelCase__ : Union[str, Any] = idalabel lowerCamelCase__ : int = {v: k for k, v in idalabel.items()} lowerCamelCase__ : Tuple = ViltForQuestionAnswering(UpperCamelCase ) elif "nlvr" in checkpoint_url: lowerCamelCase__ : Optional[Any] = True lowerCamelCase__ : List[Any] = 2 lowerCamelCase__ : Any = {0: """False""", 1: """True"""} lowerCamelCase__ : int = {v: k for k, v in config.idalabel.items()} lowerCamelCase__ : Any = 3 lowerCamelCase__ : List[str] = ViltForImagesAndTextClassification(UpperCamelCase ) elif "irtr" in checkpoint_url: lowerCamelCase__ : List[str] = True lowerCamelCase__ : Optional[int] = ViltForImageAndTextRetrieval(UpperCamelCase ) elif "mlm_itm" in checkpoint_url: lowerCamelCase__ : Optional[Any] = True lowerCamelCase__ : Optional[Any] = ViltForMaskedLM(UpperCamelCase ) else: raise ValueError("""Unknown model type""" ) # load state_dict of original model, remove and rename some keys lowerCamelCase__ : Dict = torch.hub.load_state_dict_from_url(UpperCamelCase , map_location="""cpu""" )["""state_dict"""] lowerCamelCase__ : List[Any] = create_rename_keys(UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase ) for src, dest in rename_keys: rename_key(UpperCamelCase , UpperCamelCase , UpperCamelCase ) read_in_q_k_v(UpperCamelCase , UpperCamelCase ) if mlm_model or irtr_model: lowerCamelCase__ : List[str] = ["""itm_score.fc.weight""", """itm_score.fc.bias"""] for k in ignore_keys: state_dict.pop(UpperCamelCase , UpperCamelCase ) # load state dict into HuggingFace model model.eval() if mlm_model: lowerCamelCase__ , lowerCamelCase__ : List[Any] = model.load_state_dict(UpperCamelCase , strict=UpperCamelCase ) assert missing_keys == ["mlm_score.decoder.bias"] else: model.load_state_dict(UpperCamelCase ) # Define processor lowerCamelCase__ : Optional[int] = ViltImageProcessor(size=384 ) lowerCamelCase__ : List[str] = BertTokenizer.from_pretrained("""bert-base-uncased""" ) lowerCamelCase__ : Union[str, Any] = ViltProcessor(UpperCamelCase , UpperCamelCase ) # Forward pass on example inputs (image + text) if nlvr_model: lowerCamelCase__ : int = Image.open(requests.get("""https://lil.nlp.cornell.edu/nlvr/exs/ex0_0.jpg""" , stream=UpperCamelCase ).raw ) lowerCamelCase__ : int = Image.open(requests.get("""https://lil.nlp.cornell.edu/nlvr/exs/ex0_0.jpg""" , stream=UpperCamelCase ).raw ) lowerCamelCase__ : Dict = ( """The left image contains twice the number of dogs as the right image, and at least two dogs in total are""" """ standing.""" ) lowerCamelCase__ : Optional[int] = processor(UpperCamelCase , UpperCamelCase , return_tensors="""pt""" ) lowerCamelCase__ : Dict = processor(UpperCamelCase , UpperCamelCase , return_tensors="""pt""" ) lowerCamelCase__ : List[str] = model( input_ids=encoding_a.input_ids , pixel_values=encoding_a.pixel_values , pixel_values_a=encoding_a.pixel_values , ) else: lowerCamelCase__ : str = Image.open(requests.get("""http://images.cocodataset.org/val2017/000000039769.jpg""" , stream=UpperCamelCase ).raw ) if mlm_model: lowerCamelCase__ : str = """a bunch of [MASK] laying on a [MASK].""" else: lowerCamelCase__ : Optional[int] = """How many cats are there?""" lowerCamelCase__ : List[str] = processor(UpperCamelCase , UpperCamelCase , return_tensors="""pt""" ) lowerCamelCase__ : Union[str, Any] = model(**UpperCamelCase ) # Verify outputs if mlm_model: lowerCamelCase__ : Tuple = torch.Size([1, 11, 30522] ) lowerCamelCase__ : int = torch.tensor([-12.5061, -12.5123, -12.5174] ) assert outputs.logits.shape == expected_shape assert torch.allclose(outputs.logits[0, 0, :3] , UpperCamelCase , atol=1E-4 ) # verify masked token prediction equals "cats" lowerCamelCase__ : int = outputs.logits[0, 4, :].argmax(-1 ).item() assert tokenizer.decode([predicted_id] ) == "cats" elif vqa_model: lowerCamelCase__ : str = torch.Size([1, 3129] ) lowerCamelCase__ : Any = torch.tensor([-15.9495, -18.1472, -10.3041] ) assert torch.allclose(outputs.logits[0, :3] , UpperCamelCase , atol=1E-4 ) assert outputs.logits.shape == expected_shape assert torch.allclose(outputs.logits[0, 0, :3] , UpperCamelCase , atol=1E-4 ) # verify vqa prediction equals "2" lowerCamelCase__ : Tuple = outputs.logits.argmax(-1 ).item() assert model.config.idalabel[predicted_idx] == "2" elif nlvr_model: lowerCamelCase__ : str = torch.Size([1, 2] ) lowerCamelCase__ : Optional[Any] = torch.tensor([-2.8721, 2.1291] ) assert torch.allclose(outputs.logits[0, :3] , UpperCamelCase , atol=1E-4 ) assert outputs.logits.shape == expected_shape Path(UpperCamelCase ).mkdir(exist_ok=UpperCamelCase ) print(f'''Saving model and processor to {pytorch_dump_folder_path}''' ) model.save_pretrained(UpperCamelCase ) processor.save_pretrained(UpperCamelCase ) if __name__ == "__main__": _A : str =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.''' ) _A : Tuple =parser.parse_args() convert_vilt_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)
41
0
'''simple docstring''' from __future__ import annotations def A__ ( UpperCAmelCase_ , UpperCAmelCase_ ): # Checks if the entire collection has been sorted if len(UpperCAmelCase_ ) <= 1 or n <= 1: return insert_next(UpperCAmelCase_ , n - 1 ) rec_insertion_sort(UpperCAmelCase_ , n - 1 ) def A__ ( UpperCAmelCase_ , UpperCAmelCase_ ): # Checks order between adjacent elements if index >= len(UpperCAmelCase_ ) or collection[index - 1] <= collection[index]: return # Swaps adjacent elements since they are not in ascending order _UpperCamelCase , _UpperCamelCase : str = ( collection[index], collection[index - 1], ) insert_next(UpperCAmelCase_ , index + 1 ) if __name__ == "__main__": snake_case_ : Dict = input('Enter integers separated by spaces: ') snake_case_ : list[int] = [int(num) for num in numbers.split()] rec_insertion_sort(number_list, len(number_list)) print(number_list)
236
'''simple docstring''' import argparse import torch from transformers import LxmertConfig, LxmertForPreTraining, load_tf_weights_in_lxmert from transformers.utils import logging logging.set_verbosity_info() def A__ ( UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ ): # Initialise PyTorch model _UpperCamelCase : Any = LxmertConfig.from_json_file(UpperCAmelCase_ ) print(f'Building PyTorch model from configuration: {config}' ) _UpperCamelCase : int = LxmertForPreTraining(UpperCAmelCase_ ) # Load weights from tf checkpoint load_tf_weights_in_lxmert(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ ) # Save pytorch-model print(f'Save PyTorch model to {pytorch_dump_path}' ) torch.save(model.state_dict() , UpperCAmelCase_ ) if __name__ == "__main__": snake_case_ : int = argparse.ArgumentParser() # Required parameters parser.add_argument( '--tf_checkpoint_path', default=None, type=str, required=True, help='Path to the TensorFlow checkpoint path.' ) parser.add_argument( '--config_file', default=None, type=str, required=True, help='The config json file corresponding to the pre-trained model. \nThis specifies the model architecture.', ) parser.add_argument( '--pytorch_dump_path', default=None, type=str, required=True, help='Path to the output PyTorch model.' ) snake_case_ : List[str] = parser.parse_args() convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.config_file, args.pytorch_dump_path)
236
1
import torch import torch.nn as nn from transformers import CLIPConfig, CLIPVisionModel, PreTrainedModel from ...utils import logging __A : Tuple = logging.get_logger(__name__) def __UpperCamelCase ( _A : Optional[Any] , _A : List[str] ) ->Dict: """simple docstring""" lowerCamelCase_ =nn.functional.normalize(_A ) lowerCamelCase_ =nn.functional.normalize(_A ) return torch.mm(_A , normalized_text_embeds.t() ) class _SCREAMING_SNAKE_CASE ( lowerCAmelCase__): _UpperCamelCase:Tuple = CLIPConfig _UpperCamelCase:List[Any] = ["CLIPEncoderLayer"] def __init__( self , _SCREAMING_SNAKE_CASE )-> Optional[int]: super().__init__(_SCREAMING_SNAKE_CASE ) lowerCamelCase_ =CLIPVisionModel(config.vision_config ) lowerCamelCase_ =nn.Linear(config.vision_config.hidden_size , config.projection_dim , bias=_SCREAMING_SNAKE_CASE ) lowerCamelCase_ =nn.Parameter(torch.ones(17 , config.projection_dim ) , requires_grad=_SCREAMING_SNAKE_CASE ) lowerCamelCase_ =nn.Parameter(torch.ones(3 , config.projection_dim ) , requires_grad=_SCREAMING_SNAKE_CASE ) lowerCamelCase_ =nn.Parameter(torch.ones(17 ) , requires_grad=_SCREAMING_SNAKE_CASE ) lowerCamelCase_ =nn.Parameter(torch.ones(3 ) , requires_grad=_SCREAMING_SNAKE_CASE ) @torch.no_grad() def _snake_case ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE )-> Union[str, Any]: lowerCamelCase_ =self.vision_model(_SCREAMING_SNAKE_CASE )[1] # pooled_output lowerCamelCase_ =self.visual_projection(_SCREAMING_SNAKE_CASE ) # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16 lowerCamelCase_ =cosine_distance(_SCREAMING_SNAKE_CASE , self.special_care_embeds ).cpu().float().numpy() lowerCamelCase_ =cosine_distance(_SCREAMING_SNAKE_CASE , self.concept_embeds ).cpu().float().numpy() lowerCamelCase_ =[] lowerCamelCase_ =image_embeds.shape[0] for i in range(_SCREAMING_SNAKE_CASE ): lowerCamelCase_ ={"""special_scores""": {}, """special_care""": [], """concept_scores""": {}, """bad_concepts""": []} # increase this value to create a stronger `nfsw` filter # at the cost of increasing the possibility of filtering benign images lowerCamelCase_ =0.0 for concept_idx in range(len(special_cos_dist[0] ) ): lowerCamelCase_ =special_cos_dist[i][concept_idx] lowerCamelCase_ =self.special_care_embeds_weights[concept_idx].item() lowerCamelCase_ =round(concept_cos - concept_threshold + adjustment , 3 ) if result_img["special_scores"][concept_idx] > 0: result_img["special_care"].append({concept_idx, result_img["""special_scores"""][concept_idx]} ) lowerCamelCase_ =0.0_1 for concept_idx in range(len(cos_dist[0] ) ): lowerCamelCase_ =cos_dist[i][concept_idx] lowerCamelCase_ =self.concept_embeds_weights[concept_idx].item() lowerCamelCase_ =round(concept_cos - concept_threshold + adjustment , 3 ) if result_img["concept_scores"][concept_idx] > 0: result_img["bad_concepts"].append(_SCREAMING_SNAKE_CASE ) result.append(_SCREAMING_SNAKE_CASE ) lowerCamelCase_ =[len(res["""bad_concepts"""] ) > 0 for res in result] return images, has_nsfw_concepts @torch.no_grad() def _snake_case ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE )-> Optional[int]: lowerCamelCase_ =self.vision_model(_SCREAMING_SNAKE_CASE )[1] # pooled_output lowerCamelCase_ =self.visual_projection(_SCREAMING_SNAKE_CASE ) lowerCamelCase_ =cosine_distance(_SCREAMING_SNAKE_CASE , self.special_care_embeds ) lowerCamelCase_ =cosine_distance(_SCREAMING_SNAKE_CASE , self.concept_embeds ) # increase this value to create a stronger `nsfw` filter # at the cost of increasing the possibility of filtering benign images lowerCamelCase_ =0.0 lowerCamelCase_ =special_cos_dist - self.special_care_embeds_weights + adjustment # special_scores = special_scores.round(decimals=3) lowerCamelCase_ =torch.any(special_scores > 0 , dim=1 ) lowerCamelCase_ =special_care * 0.0_1 lowerCamelCase_ =special_adjustment.unsqueeze(1 ).expand(-1 , cos_dist.shape[1] ) lowerCamelCase_ =(cos_dist - self.concept_embeds_weights) + special_adjustment # concept_scores = concept_scores.round(decimals=3) lowerCamelCase_ =torch.any(concept_scores > 0 , dim=1 ) return images, has_nsfw_concepts
154
from __future__ import annotations import math def __UpperCamelCase ( _A : int , _A : int , _A : bool , _A : list[int] , _A : float ) ->int: """simple docstring""" if depth < 0: raise ValueError("""Depth cannot be less than 0""" ) if len(_A ) == 0: raise ValueError("""Scores cannot be empty""" ) if depth == height: return scores[node_index] if is_max: return max( minimax(depth + 1 , node_index * 2 , _A , _A , _A ) , minimax(depth + 1 , node_index * 2 + 1 , _A , _A , _A ) , ) return min( minimax(depth + 1 , node_index * 2 , _A , _A , _A ) , minimax(depth + 1 , node_index * 2 + 1 , _A , _A , _A ) , ) def __UpperCamelCase ( ) ->None: """simple docstring""" lowerCamelCase_ =[90, 23, 6, 33, 21, 65, 123, 34423] lowerCamelCase_ =math.log(len(_A ) , 2 ) print("""Optimal value : """ , end="""""" ) print(minimax(0 , 0 , _A , _A , _A ) ) if __name__ == "__main__": import doctest doctest.testmod() main()
154
1
"""simple docstring""" import argparse import struct import unittest class lowerCamelCase__ : '''simple docstring''' def __init__( self , __UpperCAmelCase ) -> None: _lowerCAmelCase =data # Initialize hash values _lowerCAmelCase =[ 0x6_a_0_9_e_6_6_7, 0xb_b_6_7_a_e_8_5, 0x3_c_6_e_f_3_7_2, 0xa_5_4_f_f_5_3_a, 0x5_1_0_e_5_2_7_f, 0x9_b_0_5_6_8_8_c, 0x1_f_8_3_d_9_a_b, 0x5_b_e_0_c_d_1_9, ] # Initialize round constants _lowerCAmelCase =[ 0x4_2_8_a_2_f_9_8, 0x7_1_3_7_4_4_9_1, 0xb_5_c_0_f_b_c_f, 0xe_9_b_5_d_b_a_5, 0x3_9_5_6_c_2_5_b, 0x5_9_f_1_1_1_f_1, 0x9_2_3_f_8_2_a_4, 0xa_b_1_c_5_e_d_5, 0xd_8_0_7_a_a_9_8, 0x1_2_8_3_5_b_0_1, 0x2_4_3_1_8_5_b_e, 0x5_5_0_c_7_d_c_3, 0x7_2_b_e_5_d_7_4, 0x8_0_d_e_b_1_f_e, 0x9_b_d_c_0_6_a_7, 0xc_1_9_b_f_1_7_4, 0xe_4_9_b_6_9_c_1, 0xe_f_b_e_4_7_8_6, 0x0_f_c_1_9_d_c_6, 0x2_4_0_c_a_1_c_c, 0x2_d_e_9_2_c_6_f, 0x4_a_7_4_8_4_a_a, 0x5_c_b_0_a_9_d_c, 0x7_6_f_9_8_8_d_a, 0x9_8_3_e_5_1_5_2, 0xa_8_3_1_c_6_6_d, 0xb_0_0_3_2_7_c_8, 0xb_f_5_9_7_f_c_7, 0xc_6_e_0_0_b_f_3, 0xd_5_a_7_9_1_4_7, 0x0_6_c_a_6_3_5_1, 0x1_4_2_9_2_9_6_7, 0x2_7_b_7_0_a_8_5, 0x2_e_1_b_2_1_3_8, 0x4_d_2_c_6_d_f_c, 0x5_3_3_8_0_d_1_3, 0x6_5_0_a_7_3_5_4, 0x7_6_6_a_0_a_b_b, 0x8_1_c_2_c_9_2_e, 0x9_2_7_2_2_c_8_5, 0xa_2_b_f_e_8_a_1, 0xa_8_1_a_6_6_4_b, 0xc_2_4_b_8_b_7_0, 0xc_7_6_c_5_1_a_3, 0xd_1_9_2_e_8_1_9, 0xd_6_9_9_0_6_2_4, 0xf_4_0_e_3_5_8_5, 0x1_0_6_a_a_0_7_0, 0x1_9_a_4_c_1_1_6, 0x1_e_3_7_6_c_0_8, 0x2_7_4_8_7_7_4_c, 0x3_4_b_0_b_c_b_5, 0x3_9_1_c_0_c_b_3, 0x4_e_d_8_a_a_4_a, 0x5_b_9_c_c_a_4_f, 0x6_8_2_e_6_f_f_3, 0x7_4_8_f_8_2_e_e, 0x7_8_a_5_6_3_6_f, 0x8_4_c_8_7_8_1_4, 0x8_c_c_7_0_2_0_8, 0x9_0_b_e_f_f_f_a, 0xa_4_5_0_6_c_e_b, 0xb_e_f_9_a_3_f_7, 0xc_6_7_1_7_8_f_2, ] _lowerCAmelCase =self.preprocessing(self.data ) self.final_hash() @staticmethod def _lowerCAmelCase ( __UpperCAmelCase ) -> bytes: _lowerCAmelCase =b"""\x80""" + (b"""\x00""" * (63 - (len(__UpperCAmelCase ) + 8) % 64)) _lowerCAmelCase =struct.pack(""">Q""" , (len(__UpperCAmelCase ) * 8) ) return data + padding + big_endian_integer def _lowerCAmelCase ( self ) -> None: # Convert into blocks of 64 bytes _lowerCAmelCase =[ self.preprocessed_data[x : x + 64] for x in range(0 , len(self.preprocessed_data ) , 64 ) ] for block in self.blocks: # Convert the given block into a list of 4 byte integers _lowerCAmelCase =list(struct.unpack(""">16L""" , __UpperCAmelCase ) ) # add 48 0-ed integers words += [0] * 48 _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase =self.hashes for index in range(0 , 64 ): if index > 15: # modify the zero-ed indexes at the end of the array _lowerCAmelCase =( self.ror(words[index - 15] , 7 ) ^ self.ror(words[index - 15] , 18 ) ^ (words[index - 15] >> 3) ) _lowerCAmelCase =( self.ror(words[index - 2] , 17 ) ^ self.ror(words[index - 2] , 19 ) ^ (words[index - 2] >> 10) ) _lowerCAmelCase =( words[index - 16] + sa + words[index - 7] + sa ) % 0x1_0_0_0_0_0_0_0_0 # Compression _lowerCAmelCase =self.ror(__UpperCAmelCase , 6 ) ^ self.ror(__UpperCAmelCase , 11 ) ^ self.ror(__UpperCAmelCase , 25 ) _lowerCAmelCase =(e & f) ^ ((~e & 0xf_f_f_f_f_f_f_f) & g) _lowerCAmelCase =( h + sa + ch + self.round_constants[index] + words[index] ) % 0x1_0_0_0_0_0_0_0_0 _lowerCAmelCase =self.ror(__UpperCAmelCase , 2 ) ^ self.ror(__UpperCAmelCase , 13 ) ^ self.ror(__UpperCAmelCase , 22 ) _lowerCAmelCase =(a & b) ^ (a & c) ^ (b & c) _lowerCAmelCase =(sa + maj) % 0x1_0_0_0_0_0_0_0_0 _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase =( g, f, e, ((d + tempa) % 0x1_0_0_0_0_0_0_0_0), c, b, a, ((tempa + tempa) % 0x1_0_0_0_0_0_0_0_0), ) _lowerCAmelCase =[a, b, c, d, e, f, g, h] # Modify final values _lowerCAmelCase =[ ((element + mutated_hash_values[index]) % 0x1_0_0_0_0_0_0_0_0) for index, element in enumerate(self.hashes ) ] _lowerCAmelCase ="""""".join([hex(__UpperCAmelCase )[2:].zfill(8 ) for value in self.hashes] ) def _lowerCAmelCase ( self , __UpperCAmelCase , __UpperCAmelCase ) -> int: return 0xf_f_f_f_f_f_f_f & (value << (32 - rotations)) | (value >> rotations) class lowerCamelCase__ ( unittest.TestCase ): '''simple docstring''' def _lowerCAmelCase ( self ) -> None: import hashlib _lowerCAmelCase =bytes("""Test String""" , """utf-8""" ) self.assertEqual(SHAaaa(__UpperCAmelCase ).hash , hashlib.shaaaa(__UpperCAmelCase ).hexdigest() ) def _lowerCamelCase() -> None: import doctest doctest.testmod() _lowerCAmelCase =argparse.ArgumentParser() parser.add_argument( """-s""" , """--string""" , dest="""input_string""" , default="""Hello World!! Welcome to Cryptography""" , help="""Hash the string""" , ) parser.add_argument( """-f""" , """--file""" , dest="""input_file""" , help="""Hash contents of a file""" ) _lowerCAmelCase =parser.parse_args() _lowerCAmelCase =args.input_string # hash input should be a bytestring if args.input_file: with open(args.input_file , """rb""" ) as f: _lowerCAmelCase =f.read() else: _lowerCAmelCase =bytes(__UpperCamelCase , """utf-8""" ) print(SHAaaa(__UpperCamelCase ).hash ) if __name__ == "__main__": main()
341
"""simple docstring""" import unittest from transformers import TrOCRConfig from transformers.testing_utils import is_torch_available, require_torch, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers.models.trocr.modeling_trocr import TrOCRDecoder, TrOCRForCausalLM @require_torch class lowerCamelCase__ : '''simple docstring''' def __init__( self , __UpperCAmelCase , __UpperCAmelCase=99 , __UpperCAmelCase=13 , __UpperCAmelCase=16 , __UpperCAmelCase=7 , __UpperCAmelCase=True , __UpperCAmelCase=True , __UpperCAmelCase=True , __UpperCAmelCase=False , __UpperCAmelCase=True , __UpperCAmelCase=2 , __UpperCAmelCase=32 , __UpperCAmelCase=4 , __UpperCAmelCase=4 , __UpperCAmelCase=30 , __UpperCAmelCase=0 , __UpperCAmelCase=1 , __UpperCAmelCase=2 , __UpperCAmelCase=None , ) -> Any: _lowerCAmelCase =parent _lowerCAmelCase =batch_size _lowerCAmelCase =decoder_seq_length # For common tests _lowerCAmelCase =self.decoder_seq_length _lowerCAmelCase =is_training _lowerCAmelCase =use_attention_mask _lowerCAmelCase =use_labels _lowerCAmelCase =vocab_size _lowerCAmelCase =d_model _lowerCAmelCase =d_model _lowerCAmelCase =decoder_layers _lowerCAmelCase =decoder_layers _lowerCAmelCase =decoder_ffn_dim _lowerCAmelCase =decoder_attention_heads _lowerCAmelCase =decoder_attention_heads _lowerCAmelCase =eos_token_id _lowerCAmelCase =bos_token_id _lowerCAmelCase =pad_token_id _lowerCAmelCase =decoder_start_token_id _lowerCAmelCase =use_cache _lowerCAmelCase =max_position_embeddings _lowerCAmelCase =None _lowerCAmelCase =decoder_seq_length _lowerCAmelCase =2 _lowerCAmelCase =1 def _lowerCAmelCase ( self ) -> Tuple: _lowerCAmelCase =ids_tensor([self.batch_size, self.decoder_seq_length] , self.vocab_size ) _lowerCAmelCase =None if self.use_attention_mask: _lowerCAmelCase =ids_tensor([self.batch_size, self.decoder_seq_length] , vocab_size=2 ) _lowerCAmelCase =None if self.use_labels: _lowerCAmelCase =ids_tensor([self.batch_size, self.decoder_seq_length] , self.vocab_size ) _lowerCAmelCase =TrOCRConfig( vocab_size=self.vocab_size , d_model=self.d_model , decoder_layers=self.decoder_layers , decoder_ffn_dim=self.decoder_ffn_dim , decoder_attention_heads=self.decoder_attention_heads , eos_token_id=self.eos_token_id , bos_token_id=self.bos_token_id , use_cache=self.use_cache , pad_token_id=self.pad_token_id , decoder_start_token_id=self.decoder_start_token_id , max_position_embeddings=self.max_position_embeddings , ) return (config, input_ids, attention_mask, lm_labels) def _lowerCAmelCase ( self , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , ) -> List[Any]: _lowerCAmelCase =True _lowerCAmelCase =TrOCRDecoder(config=__UpperCAmelCase ).to(__UpperCAmelCase ).eval() _lowerCAmelCase =input_ids[:2] input_ids[input_ids == 0] += 1 # first forward pass _lowerCAmelCase =model(__UpperCAmelCase , use_cache=__UpperCAmelCase ) _lowerCAmelCase =model(__UpperCAmelCase ) _lowerCAmelCase =model(__UpperCAmelCase , use_cache=__UpperCAmelCase ) self.parent.assertTrue(len(__UpperCAmelCase ) == len(__UpperCAmelCase ) ) self.parent.assertTrue(len(__UpperCAmelCase ) == len(__UpperCAmelCase ) + 1 ) _lowerCAmelCase =outputs["""past_key_values"""] # create hypothetical next token and extent to next_input_ids _lowerCAmelCase =ids_tensor((2, 1) , config.vocab_size - 1 ) + 1 # append to next input_ids and _lowerCAmelCase =torch.cat([input_ids, next_tokens] , dim=-1 ) _lowerCAmelCase =model(__UpperCAmelCase )["""last_hidden_state"""] _lowerCAmelCase =model(__UpperCAmelCase , past_key_values=__UpperCAmelCase )["""last_hidden_state"""] # select random slice _lowerCAmelCase =ids_tensor((1,) , output_from_past.shape[-1] ).item() _lowerCAmelCase =output_from_no_past[:, next_input_ids.shape[-1] - 1, random_slice_idx].detach() _lowerCAmelCase =output_from_past[:, 0, random_slice_idx].detach() # test that outputs are equal for slice assert torch.allclose(__UpperCAmelCase , __UpperCAmelCase , atol=1e-3 ) def _lowerCAmelCase ( self ) -> List[str]: _lowerCAmelCase =self.prepare_config_and_inputs() _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase =config_and_inputs _lowerCAmelCase ={"""input_ids""": input_ids, """attention_mask""": attention_mask} return config, inputs_dict @require_torch class lowerCamelCase__ ( __magic_name__ , __magic_name__ , __magic_name__ , unittest.TestCase ): '''simple docstring''' lowerCamelCase = (TrOCRDecoder, TrOCRForCausalLM) if is_torch_available() else () lowerCamelCase = (TrOCRForCausalLM,) if is_torch_available() else () lowerCamelCase = {'''text-generation''': TrOCRForCausalLM} if is_torch_available() else {} lowerCamelCase = True lowerCamelCase = False def _lowerCAmelCase ( self ) -> int: _lowerCAmelCase =TrOCRStandaloneDecoderModelTester(self , is_training=__UpperCAmelCase ) _lowerCAmelCase =ConfigTester(self , config_class=__UpperCAmelCase ) def _lowerCAmelCase ( self ) -> List[str]: pass def _lowerCAmelCase ( self ) -> List[Any]: pass def _lowerCAmelCase ( self ) -> Any: pass def _lowerCAmelCase ( self ) -> Optional[Any]: self.config_tester.run_common_tests() def _lowerCAmelCase ( self ) -> Any: _lowerCAmelCase =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_decoder_model_past(*__UpperCAmelCase ) def _lowerCAmelCase ( self ) -> Tuple: return @unittest.skip("""The model doesn't support left padding""" ) # and it's not used enough to be worth fixing :) def _lowerCAmelCase ( self ) -> str: pass
341
1
import functools import gc import inspect import torch from .imports import is_npu_available, is_xpu_available def snake_case ( *snake_case__ :Optional[Any]) -> Any: if not isinstance(_A , _A): _A = list(_A) for i in range(len(_A)): _A = None gc.collect() if is_xpu_available(): torch.xpu.empty_cache() elif is_npu_available(): torch.npu.empty_cache() else: torch.cuda.empty_cache() return objects def snake_case ( snake_case__ :Any) -> Tuple: _A = [ """CUDA out of memory.""", # CUDA OOM """cuDNN error: CUDNN_STATUS_NOT_SUPPORTED.""", # CUDNN SNAFU """DefaultCPUAllocator: can\'t allocate memory""", # CPU OOM ] if isinstance(_A , _A) and len(exception.args) == 1: return any(err in exception.args[0] for err in _statements) return False def snake_case ( snake_case__ :str = None , snake_case__ :Optional[Any] = 128) -> Tuple: if function is None: return functools.partial(_A , starting_batch_size=_A) _A = starting_batch_size def decorator(*snake_case__ :Any , **snake_case__ :str): nonlocal batch_size gc.collect() if is_xpu_available(): torch.xpu.empty_cache() elif is_npu_available(): torch.npu.empty_cache() else: torch.cuda.empty_cache() _A = list(inspect.signature(_A).parameters.keys()) # Guard against user error if len(_A) < (len(_A) + 1): _A = """, """.join([F'''{arg}={value}''' for arg, value in zip(params[1:] , args[1:])]) raise TypeError( F'''Batch size was passed into `{function.__name__}` as the first argument when called.''' F'''Remove this as the decorator already does so: `{function.__name__}({arg_str})`''') while True: if batch_size == 0: raise RuntimeError("""No executable batch size found, reached zero.""") try: return function(_A , *_A , **_A) except Exception as e: if should_reduce_batch_size(_A): gc.collect() if is_xpu_available(): torch.xpu.empty_cache() elif is_npu_available(): torch.npu.empty_cache() else: torch.cuda.empty_cache() batch_size //= 2 else: raise return decorator
180
import importlib import os from dataclasses import dataclass from enum import Enum from typing import Any, Dict, Optional, Union import torch from ..utils import BaseOutput _A = '''scheduler_config.json''' class A ( __UpperCAmelCase ): __snake_case = 1 __snake_case = 2 __snake_case = 3 __snake_case = 4 __snake_case = 5 __snake_case = 6 __snake_case = 7 __snake_case = 8 __snake_case = 9 __snake_case = 10 __snake_case = 11 __snake_case = 12 __snake_case = 13 __snake_case = 14 @dataclass class A ( __UpperCAmelCase ): __snake_case = 42 class A : __snake_case = SCHEDULER_CONFIG_NAME __snake_case = [] __snake_case = True @classmethod def SCREAMING_SNAKE_CASE__ ( cls, UpperCamelCase__ = None, UpperCamelCase__ = None, UpperCamelCase__=False, **UpperCamelCase__, ): """simple docstring""" lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ = cls.load_config( pretrained_model_name_or_path=UpperCamelCase__, subfolder=UpperCamelCase__, return_unused_kwargs=UpperCamelCase__, return_commit_hash=UpperCamelCase__, **UpperCamelCase__, ) return cls.from_config(UpperCamelCase__, return_unused_kwargs=UpperCamelCase__, **UpperCamelCase__ ) def SCREAMING_SNAKE_CASE__ ( self, UpperCamelCase__, UpperCamelCase__ = False, **UpperCamelCase__ ): """simple docstring""" self.save_config(save_directory=UpperCamelCase__, push_to_hub=UpperCamelCase__, **UpperCamelCase__ ) @property def SCREAMING_SNAKE_CASE__ ( self ): """simple docstring""" return self._get_compatibles() @classmethod def SCREAMING_SNAKE_CASE__ ( cls ): """simple docstring""" lowerCAmelCase_ = list(set([cls.__name__] + cls._compatibles ) ) lowerCAmelCase_ = importlib.import_module(__name__.split('''.''' )[0] ) lowerCAmelCase_ = [ getattr(UpperCamelCase__, UpperCamelCase__ ) for c in compatible_classes_str if hasattr(UpperCamelCase__, UpperCamelCase__ ) ] return compatible_classes
278
0
'''simple docstring''' import unittest from parameterized import parameterized from transformers import OpenLlamaConfig, is_torch_available, set_seed from transformers.testing_utils import require_torch, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import OpenLlamaForCausalLM, OpenLlamaForSequenceClassification, OpenLlamaModel class lowerCAmelCase_: '''simple docstring''' def __init__( self ,__UpperCAmelCase ,__UpperCAmelCase=13 ,__UpperCAmelCase=7 ,__UpperCAmelCase=True ,__UpperCAmelCase=True ,__UpperCAmelCase=False ,__UpperCAmelCase=True ,__UpperCAmelCase=99 ,__UpperCAmelCase=32 ,__UpperCAmelCase=5 ,__UpperCAmelCase=4 ,__UpperCAmelCase=37 ,__UpperCAmelCase="gelu" ,__UpperCAmelCase=0.1 ,__UpperCAmelCase=0.1 ,__UpperCAmelCase=512 ,__UpperCAmelCase=16 ,__UpperCAmelCase=2 ,__UpperCAmelCase=0.0_2 ,__UpperCAmelCase=3 ,__UpperCAmelCase=4 ,__UpperCAmelCase=None ,) -> Tuple: lowerCAmelCase__ : List[Any] = parent lowerCAmelCase__ : Optional[Any] = batch_size lowerCAmelCase__ : Optional[Any] = seq_length lowerCAmelCase__ : Any = is_training lowerCAmelCase__ : Tuple = use_input_mask lowerCAmelCase__ : List[str] = use_token_type_ids lowerCAmelCase__ : Any = use_labels lowerCAmelCase__ : Dict = vocab_size lowerCAmelCase__ : str = hidden_size lowerCAmelCase__ : Dict = num_hidden_layers lowerCAmelCase__ : int = num_attention_heads lowerCAmelCase__ : Tuple = intermediate_size lowerCAmelCase__ : Any = hidden_act lowerCAmelCase__ : Any = hidden_dropout_prob lowerCAmelCase__ : Any = attention_probs_dropout_prob lowerCAmelCase__ : Union[str, Any] = max_position_embeddings lowerCAmelCase__ : Dict = type_vocab_size lowerCAmelCase__ : Tuple = type_sequence_label_size lowerCAmelCase__ : Any = initializer_range lowerCAmelCase__ : Any = num_labels lowerCAmelCase__ : Optional[int] = num_choices lowerCAmelCase__ : List[Any] = scope def UpperCAmelCase_ ( self ) -> Union[str, Any]: lowerCAmelCase__ : Tuple = ids_tensor([self.batch_size, self.seq_length] ,self.vocab_size ) lowerCAmelCase__ : Any = None if self.use_input_mask: lowerCAmelCase__ : Optional[int] = random_attention_mask([self.batch_size, self.seq_length] ) lowerCAmelCase__ : List[str] = None if self.use_token_type_ids: lowerCAmelCase__ : List[str] = ids_tensor([self.batch_size, self.seq_length] ,self.type_vocab_size ) lowerCAmelCase__ : Optional[Any] = None lowerCAmelCase__ : Tuple = None lowerCAmelCase__ : Tuple = None if self.use_labels: lowerCAmelCase__ : str = ids_tensor([self.batch_size] ,self.type_sequence_label_size ) lowerCAmelCase__ : Union[str, Any] = ids_tensor([self.batch_size, self.seq_length] ,self.num_labels ) lowerCAmelCase__ : str = ids_tensor([self.batch_size] ,self.num_choices ) lowerCAmelCase__ : Any = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def UpperCAmelCase_ ( self ) -> Tuple: return OpenLlamaConfig( 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 ,use_stable_embedding=lowerCamelCase__ ,) def UpperCAmelCase_ ( self ,__UpperCAmelCase ,__UpperCAmelCase ,__UpperCAmelCase ,__UpperCAmelCase ,__UpperCAmelCase ,__UpperCAmelCase ,__UpperCAmelCase ) -> Union[str, Any]: lowerCAmelCase__ : Tuple = OpenLlamaModel(config=lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() lowerCAmelCase__ : int = model(lowerCamelCase__ ,attention_mask=lowerCamelCase__ ) lowerCAmelCase__ : int = model(lowerCamelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape ,(self.batch_size, self.seq_length, self.hidden_size) ) def UpperCAmelCase_ ( self ,__UpperCAmelCase ,__UpperCAmelCase ,__UpperCAmelCase ,__UpperCAmelCase ,__UpperCAmelCase ,__UpperCAmelCase ,__UpperCAmelCase ,__UpperCAmelCase ,__UpperCAmelCase ,) -> List[Any]: lowerCAmelCase__ : Optional[Any] = True lowerCAmelCase__ : Optional[int] = OpenLlamaModel(lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() lowerCAmelCase__ : Tuple = model( lowerCamelCase__ ,attention_mask=lowerCamelCase__ ,encoder_hidden_states=lowerCamelCase__ ,encoder_attention_mask=lowerCamelCase__ ,) lowerCAmelCase__ : Optional[int] = model( lowerCamelCase__ ,attention_mask=lowerCamelCase__ ,encoder_hidden_states=lowerCamelCase__ ,) lowerCAmelCase__ : int = model(lowerCamelCase__ ,attention_mask=lowerCamelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape ,(self.batch_size, self.seq_length, self.hidden_size) ) def UpperCAmelCase_ ( self ,__UpperCAmelCase ,__UpperCAmelCase ,__UpperCAmelCase ,__UpperCAmelCase ,__UpperCAmelCase ,__UpperCAmelCase ,__UpperCAmelCase ,__UpperCAmelCase ,__UpperCAmelCase ,) -> Tuple: lowerCAmelCase__ : Tuple = OpenLlamaForCausalLM(config=lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() lowerCAmelCase__ : Optional[Any] = model(lowerCamelCase__ ,attention_mask=lowerCamelCase__ ,labels=lowerCamelCase__ ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.seq_length, self.vocab_size) ) def UpperCAmelCase_ ( self ,__UpperCAmelCase ,__UpperCAmelCase ,__UpperCAmelCase ,__UpperCAmelCase ,__UpperCAmelCase ,__UpperCAmelCase ,__UpperCAmelCase ,__UpperCAmelCase ,__UpperCAmelCase ,) -> List[str]: lowerCAmelCase__ : List[str] = True lowerCAmelCase__ : List[Any] = True lowerCAmelCase__ : List[str] = OpenLlamaForCausalLM(config=lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() # first forward pass lowerCAmelCase__ : int = model( lowerCamelCase__ ,attention_mask=lowerCamelCase__ ,encoder_hidden_states=lowerCamelCase__ ,encoder_attention_mask=lowerCamelCase__ ,use_cache=lowerCamelCase__ ,) lowerCAmelCase__ : Union[str, Any] = outputs.past_key_values # create hypothetical multiple next token and extent to next_input_ids lowerCAmelCase__ : int = ids_tensor((self.batch_size, 3) ,config.vocab_size ) lowerCAmelCase__ : Optional[int] = ids_tensor((self.batch_size, 3) ,vocab_size=2 ) # append to next input_ids and lowerCAmelCase__ : Optional[int] = torch.cat([input_ids, next_tokens] ,dim=-1 ) lowerCAmelCase__ : int = torch.cat([input_mask, next_mask] ,dim=-1 ) lowerCAmelCase__ : Optional[Any] = model( lowerCamelCase__ ,attention_mask=lowerCamelCase__ ,encoder_hidden_states=lowerCamelCase__ ,encoder_attention_mask=lowerCamelCase__ ,output_hidden_states=lowerCamelCase__ ,)['''hidden_states'''][0] lowerCAmelCase__ : Union[str, Any] = model( lowerCamelCase__ ,attention_mask=lowerCamelCase__ ,encoder_hidden_states=lowerCamelCase__ ,encoder_attention_mask=lowerCamelCase__ ,past_key_values=lowerCamelCase__ ,output_hidden_states=lowerCamelCase__ ,)['''hidden_states'''][0] # select random slice lowerCAmelCase__ : Dict = ids_tensor((1,) ,output_from_past.shape[-1] ).item() lowerCAmelCase__ : List[Any] = output_from_no_past[:, -3:, random_slice_idx].detach() lowerCAmelCase__ : Dict = output_from_past[:, :, random_slice_idx].detach() self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1] ) # test that outputs are equal for slice self.parent.assertTrue(torch.allclose(lowerCamelCase__ ,lowerCamelCase__ ,atol=1E-3 ) ) def UpperCAmelCase_ ( self ) -> Any: lowerCAmelCase__ : Optional[Any] = self.prepare_config_and_inputs() ( lowerCAmelCase__ ) : Dict = config_and_inputs lowerCAmelCase__ : int = {'''input_ids''': input_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_torch class lowerCAmelCase_( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , unittest.TestCase ): '''simple docstring''' __lowercase : int = ( (OpenLlamaModel, OpenLlamaForCausalLM, OpenLlamaForSequenceClassification) if is_torch_available() else () ) __lowercase : Dict = (OpenLlamaForCausalLM,) if is_torch_available() else () __lowercase : Any = ( { "feature-extraction": OpenLlamaModel, "text-classification": OpenLlamaForSequenceClassification, "text-generation": OpenLlamaForCausalLM, "zero-shot": OpenLlamaForSequenceClassification, } if is_torch_available() else {} ) __lowercase : str = False __lowercase : Any = False def UpperCAmelCase_ ( self ) -> Optional[int]: lowerCAmelCase__ : Dict = OpenLlamaModelTester(self ) lowerCAmelCase__ : int = ConfigTester(self ,config_class=lowerCamelCase__ ,hidden_size=37 ) def UpperCAmelCase_ ( self ) -> Tuple: self.config_tester.run_common_tests() def UpperCAmelCase_ ( self ) -> str: lowerCAmelCase__ : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCamelCase__ ) def UpperCAmelCase_ ( self ) -> Optional[Any]: lowerCAmelCase__ : Union[str, Any] = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: lowerCAmelCase__ : List[Any] = type self.model_tester.create_and_check_model(*lowerCamelCase__ ) def UpperCAmelCase_ ( self ) -> str: lowerCAmelCase__ : Dict = self.model_tester.prepare_config_and_inputs_for_common() lowerCAmelCase__ : str = 3 lowerCAmelCase__ : Optional[Any] = input_dict['''input_ids'''] lowerCAmelCase__ : str = input_ids.ne(1 ).to(lowerCamelCase__ ) lowerCAmelCase__ : int = ids_tensor([self.model_tester.batch_size] ,self.model_tester.type_sequence_label_size ) lowerCAmelCase__ : str = OpenLlamaForSequenceClassification(lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() lowerCAmelCase__ : Dict = model(lowerCamelCase__ ,attention_mask=lowerCamelCase__ ,labels=lowerCamelCase__ ) self.assertEqual(result.logits.shape ,(self.model_tester.batch_size, self.model_tester.num_labels) ) def UpperCAmelCase_ ( self ) -> str: lowerCAmelCase__ : Dict = self.model_tester.prepare_config_and_inputs_for_common() lowerCAmelCase__ : str = 3 lowerCAmelCase__ : Union[str, Any] = '''single_label_classification''' lowerCAmelCase__ : Any = input_dict['''input_ids'''] lowerCAmelCase__ : str = input_ids.ne(1 ).to(lowerCamelCase__ ) lowerCAmelCase__ : Dict = ids_tensor([self.model_tester.batch_size] ,self.model_tester.type_sequence_label_size ) lowerCAmelCase__ : Dict = OpenLlamaForSequenceClassification(lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() lowerCAmelCase__ : Dict = model(lowerCamelCase__ ,attention_mask=lowerCamelCase__ ,labels=lowerCamelCase__ ) self.assertEqual(result.logits.shape ,(self.model_tester.batch_size, self.model_tester.num_labels) ) def UpperCAmelCase_ ( self ) -> List[Any]: lowerCAmelCase__ : Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() lowerCAmelCase__ : Optional[Any] = 3 lowerCAmelCase__ : Any = '''multi_label_classification''' lowerCAmelCase__ : Union[str, Any] = input_dict['''input_ids'''] lowerCAmelCase__ : List[Any] = input_ids.ne(1 ).to(lowerCamelCase__ ) lowerCAmelCase__ : str = ids_tensor( [self.model_tester.batch_size, config.num_labels] ,self.model_tester.type_sequence_label_size ).to(torch.float ) lowerCAmelCase__ : Union[str, Any] = OpenLlamaForSequenceClassification(lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() lowerCAmelCase__ : str = model(lowerCamelCase__ ,attention_mask=lowerCamelCase__ ,labels=lowerCamelCase__ ) self.assertEqual(result.logits.shape ,(self.model_tester.batch_size, self.model_tester.num_labels) ) @unittest.skip("""Open-Llama buffers include complex numbers, which breaks this test""" ) def UpperCAmelCase_ ( self ) -> int: pass @parameterized.expand([("""linear""",), ("""dynamic""",)] ) def UpperCAmelCase_ ( self ,__UpperCAmelCase ) -> Union[str, Any]: lowerCAmelCase__ : str = self.model_tester.prepare_config_and_inputs_for_common() lowerCAmelCase__ : Union[str, Any] = ids_tensor([1, 10] ,config.vocab_size ) lowerCAmelCase__ : str = ids_tensor([1, int(config.max_position_embeddings * 1.5 )] ,config.vocab_size ) set_seed(42 ) # Fixed seed at init time so the two models get the same random weights lowerCAmelCase__ : Optional[Any] = OpenLlamaModel(lowerCamelCase__ ) original_model.to(lowerCamelCase__ ) original_model.eval() lowerCAmelCase__ : List[str] = original_model(lowerCamelCase__ ).last_hidden_state lowerCAmelCase__ : Optional[Any] = original_model(lowerCamelCase__ ).last_hidden_state set_seed(42 ) # Fixed seed at init time so the two models get the same random weights lowerCAmelCase__ : List[Any] = {'''type''': scaling_type, '''factor''': 1_0.0} lowerCAmelCase__ : List[str] = OpenLlamaModel(lowerCamelCase__ ) scaled_model.to(lowerCamelCase__ ) scaled_model.eval() lowerCAmelCase__ : Any = scaled_model(lowerCamelCase__ ).last_hidden_state lowerCAmelCase__ : Tuple = scaled_model(lowerCamelCase__ ).last_hidden_state # Dynamic scaling does not change the RoPE embeddings until it receives an input longer than the original # maximum sequence length, so the outputs for the short input should match. if scaling_type == "dynamic": self.assertTrue(torch.allclose(lowerCamelCase__ ,lowerCamelCase__ ,atol=1E-5 ) ) else: self.assertFalse(torch.allclose(lowerCamelCase__ ,lowerCamelCase__ ,atol=1E-5 ) ) # The output should be different for long inputs self.assertFalse(torch.allclose(lowerCamelCase__ ,lowerCamelCase__ ,atol=1E-5 ) )
357
'''simple docstring''' from ..utils import DummyObject, requires_backends class lowerCAmelCase_( metaclass=SCREAMING_SNAKE_CASE_ ): '''simple docstring''' __lowercase : int = ['''sentencepiece'''] def __init__( self ,*__UpperCAmelCase ,**__UpperCAmelCase ) -> Optional[int]: requires_backends(self ,["""sentencepiece"""] ) class lowerCAmelCase_( metaclass=SCREAMING_SNAKE_CASE_ ): '''simple docstring''' __lowercase : Optional[Any] = ['''sentencepiece'''] def __init__( self ,*__UpperCAmelCase ,**__UpperCAmelCase ) -> str: requires_backends(self ,["""sentencepiece"""] ) class lowerCAmelCase_( metaclass=SCREAMING_SNAKE_CASE_ ): '''simple docstring''' __lowercase : str = ['''sentencepiece'''] def __init__( self ,*__UpperCAmelCase ,**__UpperCAmelCase ) -> Optional[Any]: requires_backends(self ,["""sentencepiece"""] ) class lowerCAmelCase_( metaclass=SCREAMING_SNAKE_CASE_ ): '''simple docstring''' __lowercase : Union[str, Any] = ['''sentencepiece'''] def __init__( self ,*__UpperCAmelCase ,**__UpperCAmelCase ) -> List[Any]: requires_backends(self ,["""sentencepiece"""] ) class lowerCAmelCase_( metaclass=SCREAMING_SNAKE_CASE_ ): '''simple docstring''' __lowercase : int = ['''sentencepiece'''] def __init__( self ,*__UpperCAmelCase ,**__UpperCAmelCase ) -> Tuple: requires_backends(self ,["""sentencepiece"""] ) class lowerCAmelCase_( metaclass=SCREAMING_SNAKE_CASE_ ): '''simple docstring''' __lowercase : Dict = ['''sentencepiece'''] def __init__( self ,*__UpperCAmelCase ,**__UpperCAmelCase ) -> Dict: requires_backends(self ,["""sentencepiece"""] ) class lowerCAmelCase_( metaclass=SCREAMING_SNAKE_CASE_ ): '''simple docstring''' __lowercase : Optional[Any] = ['''sentencepiece'''] def __init__( self ,*__UpperCAmelCase ,**__UpperCAmelCase ) -> List[str]: requires_backends(self ,["""sentencepiece"""] ) class lowerCAmelCase_( metaclass=SCREAMING_SNAKE_CASE_ ): '''simple docstring''' __lowercase : Optional[Any] = ['''sentencepiece'''] def __init__( self ,*__UpperCAmelCase ,**__UpperCAmelCase ) -> List[Any]: requires_backends(self ,["""sentencepiece"""] ) class lowerCAmelCase_( metaclass=SCREAMING_SNAKE_CASE_ ): '''simple docstring''' __lowercase : Dict = ['''sentencepiece'''] def __init__( self ,*__UpperCAmelCase ,**__UpperCAmelCase ) -> Any: requires_backends(self ,["""sentencepiece"""] ) class lowerCAmelCase_( metaclass=SCREAMING_SNAKE_CASE_ ): '''simple docstring''' __lowercase : Tuple = ['''sentencepiece'''] def __init__( self ,*__UpperCAmelCase ,**__UpperCAmelCase ) -> Union[str, Any]: requires_backends(self ,["""sentencepiece"""] ) class lowerCAmelCase_( metaclass=SCREAMING_SNAKE_CASE_ ): '''simple docstring''' __lowercase : Optional[int] = ['''sentencepiece'''] def __init__( self ,*__UpperCAmelCase ,**__UpperCAmelCase ) -> List[str]: requires_backends(self ,["""sentencepiece"""] ) class lowerCAmelCase_( metaclass=SCREAMING_SNAKE_CASE_ ): '''simple docstring''' __lowercase : Optional[Any] = ['''sentencepiece'''] def __init__( self ,*__UpperCAmelCase ,**__UpperCAmelCase ) -> Union[str, Any]: requires_backends(self ,["""sentencepiece"""] ) class lowerCAmelCase_( metaclass=SCREAMING_SNAKE_CASE_ ): '''simple docstring''' __lowercase : Optional[int] = ['''sentencepiece'''] def __init__( self ,*__UpperCAmelCase ,**__UpperCAmelCase ) -> List[str]: requires_backends(self ,["""sentencepiece"""] ) class lowerCAmelCase_( metaclass=SCREAMING_SNAKE_CASE_ ): '''simple docstring''' __lowercase : List[str] = ['''sentencepiece'''] def __init__( self ,*__UpperCAmelCase ,**__UpperCAmelCase ) -> Any: requires_backends(self ,["""sentencepiece"""] ) class lowerCAmelCase_( metaclass=SCREAMING_SNAKE_CASE_ ): '''simple docstring''' __lowercase : Tuple = ['''sentencepiece'''] def __init__( self ,*__UpperCAmelCase ,**__UpperCAmelCase ) -> List[str]: requires_backends(self ,["""sentencepiece"""] ) class lowerCAmelCase_( metaclass=SCREAMING_SNAKE_CASE_ ): '''simple docstring''' __lowercase : Dict = ['''sentencepiece'''] def __init__( self ,*__UpperCAmelCase ,**__UpperCAmelCase ) -> List[Any]: requires_backends(self ,["""sentencepiece"""] ) class lowerCAmelCase_( metaclass=SCREAMING_SNAKE_CASE_ ): '''simple docstring''' __lowercase : Union[str, Any] = ['''sentencepiece'''] def __init__( self ,*__UpperCAmelCase ,**__UpperCAmelCase ) -> List[Any]: requires_backends(self ,["""sentencepiece"""] ) class lowerCAmelCase_( metaclass=SCREAMING_SNAKE_CASE_ ): '''simple docstring''' __lowercase : Tuple = ['''sentencepiece'''] def __init__( self ,*__UpperCAmelCase ,**__UpperCAmelCase ) -> str: requires_backends(self ,["""sentencepiece"""] ) class lowerCAmelCase_( metaclass=SCREAMING_SNAKE_CASE_ ): '''simple docstring''' __lowercase : List[Any] = ['''sentencepiece'''] def __init__( self ,*__UpperCAmelCase ,**__UpperCAmelCase ) -> Optional[int]: requires_backends(self ,["""sentencepiece"""] ) class lowerCAmelCase_( metaclass=SCREAMING_SNAKE_CASE_ ): '''simple docstring''' __lowercase : int = ['''sentencepiece'''] def __init__( self ,*__UpperCAmelCase ,**__UpperCAmelCase ) -> Optional[Any]: requires_backends(self ,["""sentencepiece"""] ) class lowerCAmelCase_( metaclass=SCREAMING_SNAKE_CASE_ ): '''simple docstring''' __lowercase : Tuple = ['''sentencepiece'''] def __init__( self ,*__UpperCAmelCase ,**__UpperCAmelCase ) -> Dict: requires_backends(self ,["""sentencepiece"""] ) class lowerCAmelCase_( metaclass=SCREAMING_SNAKE_CASE_ ): '''simple docstring''' __lowercase : str = ['''sentencepiece'''] def __init__( self ,*__UpperCAmelCase ,**__UpperCAmelCase ) -> List[Any]: requires_backends(self ,["""sentencepiece"""] ) class lowerCAmelCase_( metaclass=SCREAMING_SNAKE_CASE_ ): '''simple docstring''' __lowercase : Union[str, Any] = ['''sentencepiece'''] def __init__( self ,*__UpperCAmelCase ,**__UpperCAmelCase ) -> List[Any]: requires_backends(self ,["""sentencepiece"""] ) class lowerCAmelCase_( metaclass=SCREAMING_SNAKE_CASE_ ): '''simple docstring''' __lowercase : List[str] = ['''sentencepiece'''] def __init__( self ,*__UpperCAmelCase ,**__UpperCAmelCase ) -> Any: requires_backends(self ,["""sentencepiece"""] ) class lowerCAmelCase_( metaclass=SCREAMING_SNAKE_CASE_ ): '''simple docstring''' __lowercase : Optional[Any] = ['''sentencepiece'''] def __init__( self ,*__UpperCAmelCase ,**__UpperCAmelCase ) -> Optional[Any]: requires_backends(self ,["""sentencepiece"""] ) class lowerCAmelCase_( metaclass=SCREAMING_SNAKE_CASE_ ): '''simple docstring''' __lowercase : Optional[int] = ['''sentencepiece'''] def __init__( self ,*__UpperCAmelCase ,**__UpperCAmelCase ) -> str: requires_backends(self ,["""sentencepiece"""] ) class lowerCAmelCase_( metaclass=SCREAMING_SNAKE_CASE_ ): '''simple docstring''' __lowercase : Any = ['''sentencepiece'''] def __init__( self ,*__UpperCAmelCase ,**__UpperCAmelCase ) -> int: requires_backends(self ,["""sentencepiece"""] ) class lowerCAmelCase_( metaclass=SCREAMING_SNAKE_CASE_ ): '''simple docstring''' __lowercase : List[Any] = ['''sentencepiece'''] def __init__( self ,*__UpperCAmelCase ,**__UpperCAmelCase ) -> List[str]: requires_backends(self ,["""sentencepiece"""] ) class lowerCAmelCase_( metaclass=SCREAMING_SNAKE_CASE_ ): '''simple docstring''' __lowercase : Tuple = ['''sentencepiece'''] def __init__( self ,*__UpperCAmelCase ,**__UpperCAmelCase ) -> str: requires_backends(self ,["""sentencepiece"""] ) class lowerCAmelCase_( metaclass=SCREAMING_SNAKE_CASE_ ): '''simple docstring''' __lowercase : Optional[Any] = ['''sentencepiece'''] def __init__( self ,*__UpperCAmelCase ,**__UpperCAmelCase ) -> Optional[int]: requires_backends(self ,["""sentencepiece"""] ) class lowerCAmelCase_( metaclass=SCREAMING_SNAKE_CASE_ ): '''simple docstring''' __lowercase : List[Any] = ['''sentencepiece'''] def __init__( self ,*__UpperCAmelCase ,**__UpperCAmelCase ) -> List[Any]: requires_backends(self ,["""sentencepiece"""] )
184
0
import bza import gzip import lzma import os import shutil import struct import tarfile import warnings import zipfile from abc import ABC, abstractmethod from pathlib import Path from typing import Dict, List, Optional, Type, Union from .. import config from .filelock import FileLock from .logging import get_logger lowercase : Dict = get_logger(__name__) class A__ : """simple docstring""" def __init__( self , lowercase = None) -> str: '''simple docstring''' a__ : Union[str, Any] = ( os.path.join(lowercase , config.EXTRACTED_DATASETS_DIR) if cache_dir else config.EXTRACTED_DATASETS_PATH ) a__ : Union[str, Any] = Extractor def __lowercase ( self , lowercase) -> str: '''simple docstring''' from .file_utils import hash_url_to_filename # Path where we extract compressed archives # We extract in the cache dir, and get the extracted path name by hashing the original path" a__ : str = os.path.abspath(lowercase) return os.path.join(self.extract_dir , hash_url_to_filename(lowercase)) def __lowercase ( self , lowercase , lowercase) -> bool: '''simple docstring''' return force_extract or ( not os.path.isfile(lowercase) and not (os.path.isdir(lowercase) and os.listdir(lowercase)) ) def __lowercase ( self , lowercase , lowercase = False) -> str: '''simple docstring''' a__ : List[Any] = self.extractor.infer_extractor_format(lowercase) if not extractor_format: return input_path a__ : Union[str, Any] = self._get_output_path(lowercase) if self._do_extract(lowercase , lowercase): self.extractor.extract(lowercase , lowercase , lowercase) return output_path class A__ ( __UpperCAmelCase ): """simple docstring""" @classmethod @abstractmethod def __lowercase ( cls , lowercase , **lowercase) -> bool: '''simple docstring''' ... @staticmethod @abstractmethod def __lowercase ( lowercase , lowercase) -> None: '''simple docstring''' ... class A__ ( __UpperCAmelCase , __UpperCAmelCase ): """simple docstring""" __A : List[bytes] = [] @staticmethod def __lowercase ( lowercase , lowercase) -> Union[str, Any]: '''simple docstring''' with open(lowercase , 'rb') as f: return f.read(lowercase) @classmethod def __lowercase ( cls , lowercase , lowercase = b"") -> bool: '''simple docstring''' if not magic_number: a__ : Dict = max(len(lowercase) for cls_magic_number in cls.magic_numbers) try: a__ : List[Any] = cls.read_magic_number(lowercase , lowercase) except OSError: return False return any(magic_number.startswith(lowercase) for cls_magic_number in cls.magic_numbers) class A__ ( __UpperCAmelCase ): """simple docstring""" @classmethod def __lowercase ( cls , lowercase , **lowercase) -> bool: '''simple docstring''' return tarfile.is_tarfile(lowercase) @staticmethod def __lowercase ( lowercase , lowercase) -> Dict: '''simple docstring''' def resolved(lowercase) -> str: return os.path.realpath(os.path.abspath(lowercase)) def badpath(lowercase , lowercase) -> bool: # joinpath will ignore base if path is absolute return not resolved(os.path.join(lowercase , lowercase)).startswith(lowercase) def badlink(lowercase , lowercase) -> bool: # Links are interpreted relative to the directory containing the link a__ : Optional[int] = resolved(os.path.join(lowercase , os.path.dirname(info.name))) return badpath(info.linkname , base=lowercase) a__ : int = resolved(lowercase) for finfo in members: if badpath(finfo.name , lowercase): logger.error(F'Extraction of {finfo.name} is blocked (illegal path)') elif finfo.issym() and badlink(lowercase , lowercase): logger.error(F'Extraction of {finfo.name} is blocked: Symlink to {finfo.linkname}') elif finfo.islnk() and badlink(lowercase , lowercase): logger.error(F'Extraction of {finfo.name} is blocked: Hard link to {finfo.linkname}') else: yield finfo @staticmethod def __lowercase ( lowercase , lowercase) -> None: '''simple docstring''' os.makedirs(lowercase , exist_ok=lowercase) a__ : Optional[Any] = tarfile.open(lowercase) tar_file.extractall(lowercase , members=TarExtractor.safemembers(lowercase , lowercase)) tar_file.close() class A__ ( __UpperCAmelCase ): """simple docstring""" __A : List[str] = [b'''\x1F\x8B'''] @staticmethod def __lowercase ( lowercase , lowercase) -> None: '''simple docstring''' with gzip.open(lowercase , 'rb') as gzip_file: with open(lowercase , 'wb') as extracted_file: shutil.copyfileobj(lowercase , lowercase) class A__ ( __UpperCAmelCase ): """simple docstring""" __A : Optional[int] = [ b'''PK\x03\x04''', b'''PK\x05\x06''', # empty archive b'''PK\x07\x08''', # spanned archive ] @classmethod def __lowercase ( cls , lowercase , lowercase = b"") -> bool: '''simple docstring''' if super().is_extractable(lowercase , magic_number=lowercase): return True try: # Alternative version of zipfile.is_zipfile that has less false positives, but misses executable zip archives. # From: https://github.com/python/cpython/pull/5053 from zipfile import ( _CD_SIGNATURE, _ECD_DISK_NUMBER, _ECD_DISK_START, _ECD_ENTRIES_TOTAL, _ECD_OFFSET, _ECD_SIZE, _EndRecData, sizeCentralDir, stringCentralDir, structCentralDir, ) with open(lowercase , 'rb') as fp: a__ : Tuple = _EndRecData(lowercase) if endrec: if endrec[_ECD_ENTRIES_TOTAL] == 0 and endrec[_ECD_SIZE] == 0 and endrec[_ECD_OFFSET] == 0: return True # Empty zipfiles are still zipfiles elif endrec[_ECD_DISK_NUMBER] == endrec[_ECD_DISK_START]: fp.seek(endrec[_ECD_OFFSET]) # Central directory is on the same disk if fp.tell() == endrec[_ECD_OFFSET] and endrec[_ECD_SIZE] >= sizeCentralDir: a__ : Dict = fp.read(lowercase) # CD is where we expect it to be if len(lowercase) == sizeCentralDir: a__ : Any = struct.unpack(lowercase , lowercase) # CD is the right size if centdir[_CD_SIGNATURE] == stringCentralDir: return True # First central directory entry has correct magic number return False except Exception: # catch all errors in case future python versions change the zipfile internals return False @staticmethod def __lowercase ( lowercase , lowercase) -> None: '''simple docstring''' os.makedirs(lowercase , exist_ok=lowercase) with zipfile.ZipFile(lowercase , 'r') as zip_file: zip_file.extractall(lowercase) zip_file.close() class A__ ( __UpperCAmelCase ): """simple docstring""" __A : List[Any] = [b'''\xFD\x37\x7A\x58\x5A\x00'''] @staticmethod def __lowercase ( lowercase , lowercase) -> None: '''simple docstring''' with lzma.open(lowercase) as compressed_file: with open(lowercase , 'wb') as extracted_file: shutil.copyfileobj(lowercase , lowercase) class A__ ( __UpperCAmelCase ): """simple docstring""" __A : int = [b'''Rar!\x1a\x07\x00''', b'''Rar!\x1a\x07\x01\x00'''] # RAR_ID # RAR5_ID @staticmethod def __lowercase ( lowercase , lowercase) -> None: '''simple docstring''' if not config.RARFILE_AVAILABLE: raise ImportError('Please pip install rarfile') import rarfile os.makedirs(lowercase , exist_ok=lowercase) a__ : Union[str, Any] = rarfile.RarFile(lowercase) rf.extractall(lowercase) rf.close() class A__ ( __UpperCAmelCase ): """simple docstring""" __A : Optional[int] = [b'''\x28\xb5\x2F\xFD'''] @staticmethod def __lowercase ( lowercase , lowercase) -> None: '''simple docstring''' if not config.ZSTANDARD_AVAILABLE: raise ImportError('Please pip install zstandard') import zstandard as zstd a__ : str = zstd.ZstdDecompressor() with open(lowercase , 'rb') as ifh, open(lowercase , 'wb') as ofh: dctx.copy_stream(lowercase , lowercase) class A__ ( __UpperCAmelCase ): """simple docstring""" __A : Optional[Any] = [b'''\x42\x5A\x68'''] @staticmethod def __lowercase ( lowercase , lowercase) -> None: '''simple docstring''' with bza.open(lowercase , 'rb') as compressed_file: with open(lowercase , 'wb') as extracted_file: shutil.copyfileobj(lowercase , lowercase) class A__ ( __UpperCAmelCase ): """simple docstring""" __A : Union[str, Any] = [b'''\x37\x7A\xBC\xAF\x27\x1C'''] @staticmethod def __lowercase ( lowercase , lowercase) -> None: '''simple docstring''' if not config.PY7ZR_AVAILABLE: raise ImportError('Please pip install py7zr') import pyazr os.makedirs(lowercase , exist_ok=lowercase) with pyazr.SevenZipFile(lowercase , 'r') as archive: archive.extractall(lowercase) class A__ ( __UpperCAmelCase ): """simple docstring""" __A : Any = [b'''\x04\x22\x4D\x18'''] @staticmethod def __lowercase ( lowercase , lowercase) -> None: '''simple docstring''' if not config.LZ4_AVAILABLE: raise ImportError('Please pip install lz4') import lza.frame with lza.frame.open(lowercase , 'rb') as compressed_file: with open(lowercase , 'wb') as extracted_file: shutil.copyfileobj(lowercase , lowercase) class A__ : """simple docstring""" __A : Dict[str, Type[BaseExtractor]] = { "tar": TarExtractor, "gzip": GzipExtractor, "zip": ZipExtractor, "xz": XzExtractor, "rar": RarExtractor, "zstd": ZstdExtractor, "bz2": BzipaExtractor, "7z": SevenZipExtractor, # <Added version="2.4.0"/> "lz4": LzaExtractor, # <Added version="2.4.0"/> } @classmethod def __lowercase ( cls) -> str: '''simple docstring''' return max( len(lowercase) for extractor in cls.extractors.values() if issubclass(lowercase , lowercase) for extractor_magic_number in extractor.magic_numbers) @staticmethod def __lowercase ( lowercase , lowercase) -> str: '''simple docstring''' try: return MagicNumberBaseExtractor.read_magic_number(lowercase , magic_number_length=lowercase) except OSError: return b"" @classmethod def __lowercase ( cls , lowercase , lowercase = False) -> bool: '''simple docstring''' warnings.warn( 'Method \'is_extractable\' was deprecated in version 2.4.0 and will be removed in 3.0.0. ' 'Use \'infer_extractor_format\' instead.' , category=lowercase , ) a__ : Dict = cls.infer_extractor_format(lowercase) if extractor_format: return True if not return_extractor else (True, cls.extractors[extractor_format]) return False if not return_extractor else (False, None) @classmethod def __lowercase ( cls , lowercase) -> str: # <Added version="2.4.0"/> '''simple docstring''' a__ : int = cls._get_magic_number_max_length() a__ : Optional[Any] = cls._read_magic_number(lowercase , lowercase) for extractor_format, extractor in cls.extractors.items(): if extractor.is_extractable(lowercase , magic_number=lowercase): return extractor_format @classmethod def __lowercase ( cls , lowercase , lowercase , lowercase = None , lowercase = "deprecated" , ) -> None: '''simple docstring''' os.makedirs(os.path.dirname(lowercase) , exist_ok=lowercase) # Prevent parallel extractions a__ : Optional[Any] = str(Path(lowercase).with_suffix('.lock')) with FileLock(lowercase): shutil.rmtree(lowercase , ignore_errors=lowercase) if extractor_format or extractor != "deprecated": if extractor != "deprecated" or not isinstance(lowercase , lowercase): # passed as positional arg warnings.warn( 'Parameter \'extractor\' was deprecated in version 2.4.0 and will be removed in 3.0.0. ' 'Use \'extractor_format\' instead.' , category=lowercase , ) a__ : Optional[int] = extractor if extractor != 'deprecated' else extractor_format else: a__ : Dict = cls.extractors[extractor_format] return extractor.extract(lowercase , lowercase) else: warnings.warn( 'Parameter \'extractor_format\' was made required in version 2.4.0 and not passing it will raise an ' 'exception in 3.0.0.' , category=lowercase , ) for extractor in cls.extractors.values(): if extractor.is_extractable(lowercase): return extractor.extract(lowercase , lowercase)
99
import pytest from datasets.utils.sharding import _distribute_shards, _number_of_shards_in_gen_kwargs, _split_gen_kwargs @pytest.mark.parametrize( 'kwargs, expected', [ ({'num_shards': 0, 'max_num_jobs': 1}, []), ({'num_shards': 10, 'max_num_jobs': 1}, [range(10 )]), ({'num_shards': 10, 'max_num_jobs': 10}, [range(lowerCAmelCase_, i + 1 ) for i in range(10 )]), ({'num_shards': 1, 'max_num_jobs': 10}, [range(1 )]), ({'num_shards': 10, 'max_num_jobs': 3}, [range(0, 4 ), range(4, 7 ), range(7, 10 )]), ({'num_shards': 3, 'max_num_jobs': 10}, [range(0, 1 ), range(1, 2 ), range(2, 3 )]), ], ) def a_ ( lowerCAmelCase_ : Optional[Any], lowerCAmelCase_ : Any ): __lowerCAmelCase = _distribute_shards(**lowerCAmelCase_ ) assert out == expected @pytest.mark.parametrize( 'gen_kwargs, max_num_jobs, expected', [ ({'foo': 0}, 10, [{'foo': 0}]), ({'shards': [0, 1, 2, 3]}, 1, [{'shards': [0, 1, 2, 3]}]), ({'shards': [0, 1, 2, 3]}, 4, [{'shards': [0]}, {'shards': [1]}, {'shards': [2]}, {'shards': [3]}]), ({'shards': [0, 1]}, 4, [{'shards': [0]}, {'shards': [1]}]), ({'shards': [0, 1, 2, 3]}, 2, [{'shards': [0, 1]}, {'shards': [2, 3]}]), ], ) def a_ ( lowerCAmelCase_ : Optional[Any], lowerCAmelCase_ : List[str], lowerCAmelCase_ : Optional[int] ): __lowerCAmelCase = _split_gen_kwargs(lowerCAmelCase_, lowerCAmelCase_ ) assert out == expected @pytest.mark.parametrize( 'gen_kwargs, expected', [ ({'foo': 0}, 1), ({'shards': [0]}, 1), ({'shards': [0, 1, 2, 3]}, 4), ({'shards': [0, 1, 2, 3], 'foo': 0}, 4), ({'shards': [0, 1, 2, 3], 'other': (0, 1)}, 4), ({'shards': [0, 1, 2, 3], 'shards2': [0, 1]}, RuntimeError), ], ) def a_ ( lowerCAmelCase_ : int, lowerCAmelCase_ : Any ): if expected is RuntimeError: with pytest.raises(lowerCAmelCase_ ): _number_of_shards_in_gen_kwargs(lowerCAmelCase_ ) else: __lowerCAmelCase = _number_of_shards_in_gen_kwargs(lowerCAmelCase_ ) assert out == expected
284
0
"""simple docstring""" def __SCREAMING_SNAKE_CASE ( __UpperCAmelCase ): if length <= 0 or not isinstance(__UpperCAmelCase , __UpperCAmelCase ): raise ValueError("""Length must be a positive integer.""" ) return [n * (2 * n - 1) for n in range(__UpperCAmelCase )] if __name__ == "__main__": print(hexagonal_numbers(length=5)) print(hexagonal_numbers(length=10))
336
"""simple docstring""" import cva import numpy as np class UpperCamelCase : """simple docstring""" def __init__( self ,UpperCAmelCase_ ,UpperCAmelCase_ ): if k in (0.04, 0.06): _lowercase : Optional[Any] = k _lowercase : Optional[Any] = window_size else: raise ValueError("""invalid k value""" ) def __str__( self ): return str(self.k ) def lowerCamelCase__ ( self ,UpperCAmelCase_ ): _lowercase : List[str] = cva.imread(UpperCAmelCase_ ,0 ) _lowercase , _lowercase : Dict = img.shape _lowercase : list[list[int]] = [] _lowercase : int = img.copy() _lowercase : List[str] = cva.cvtColor(UpperCAmelCase_ ,cva.COLOR_GRAY2RGB ) _lowercase , _lowercase : Optional[Any] = np.gradient(UpperCAmelCase_ ) _lowercase : Optional[int] = dx**2 _lowercase : Optional[Any] = dy**2 _lowercase : Optional[Any] = dx * dy _lowercase : List[str] = 0.04 _lowercase : Optional[Any] = self.window_size // 2 for y in range(UpperCAmelCase_ ,h - offset ): for x in range(UpperCAmelCase_ ,w - offset ): _lowercase : Optional[Any] = ixx[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() _lowercase : Dict = iyy[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() _lowercase : Union[str, Any] = ixy[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() _lowercase : int = (wxx * wyy) - (wxy**2) _lowercase : Union[str, Any] = wxx + wyy _lowercase : Union[str, Any] = det - k * (trace**2) # Can change the value if r > 0.5: corner_list.append([x, y, r] ) color_img.itemset((y, x, 0) ,0 ) color_img.itemset((y, x, 1) ,0 ) color_img.itemset((y, x, 2) ,2_55 ) return color_img, corner_list if __name__ == "__main__": UpperCAmelCase: Optional[int] = HarrisCorner(0.04, 3) UpperCAmelCase , UpperCAmelCase: List[Any] = edge_detect.detect("""path_to_image""") cva.imwrite("""detect.png""", color_img)
336
1
from typing import List import numpy as np def UpperCAmelCase ( a_ ) -> int: """simple docstring""" __A = {key: len(a_ ) for key, value in gen_kwargs.items() if isinstance(a_ , a_ )} if len(set(lists_lengths.values() ) ) > 1: raise RuntimeError( ( "Sharding is ambiguous for this dataset: " + "we found several data sources lists of different lengths, and we don't know over which list we should parallelize:\n" + "\n".join(F'''\t- key {key} has length {length}''' for key, length in lists_lengths.items() ) + "\nTo fix this, check the 'gen_kwargs' and make sure to use lists only for data sources, " + "and use tuples otherwise. In the end there should only be one single list, or several lists with the same length." ) ) __A = max(lists_lengths.values() , default=0 ) return max(1 , a_ ) def UpperCAmelCase ( a_ , a_ ) -> List[range]: """simple docstring""" __A = [] for group_idx in range(a_ ): __A = num_shards // max_num_jobs + (group_idx < (num_shards % max_num_jobs)) if num_shards_to_add == 0: break __A = shards_indices_per_group[-1].stop if shards_indices_per_group else 0 __A = range(a_ , start + num_shards_to_add ) shards_indices_per_group.append(a_ ) return shards_indices_per_group def UpperCAmelCase ( a_ , a_ ) -> List[dict]: """simple docstring""" __A = _number_of_shards_in_gen_kwargs(a_ ) if num_shards == 1: return [dict(a_ )] else: __A = _distribute_shards(num_shards=a_ , max_num_jobs=a_ ) return [ { key: [value[shard_idx] for shard_idx in shard_indices_per_group[group_idx]] if isinstance(a_ , a_ ) else value for key, value in gen_kwargs.items() } for group_idx in range(len(a_ ) ) ] def UpperCAmelCase ( a_ ) -> dict: """simple docstring""" return { key: [value for gen_kwargs in gen_kwargs_list for value in gen_kwargs[key]] if isinstance(gen_kwargs_list[0][key] , a_ ) else gen_kwargs_list[0][key] for key in gen_kwargs_list[0] } def UpperCAmelCase ( a_ , a_ ) -> dict: """simple docstring""" __A = {len(a_ ) for value in gen_kwargs.values() if isinstance(a_ , a_ )} __A = {} for size in list_sizes: __A = list(range(a_ ) ) rng.shuffle(indices_per_size[size] ) # Now let's copy the gen_kwargs and shuffle the lists based on their sizes __A = dict(a_ ) for key, value in shuffled_kwargs.items(): if isinstance(a_ , a_ ): __A = [value[i] for i in indices_per_size[len(a_ )]] return shuffled_kwargs
15
"""simple docstring""" import numpy as np from sklearn.datasets import fetch_california_housing from sklearn.metrics import mean_absolute_error, mean_squared_error from sklearn.model_selection import train_test_split from xgboost import XGBRegressor def lowercase_ ( __UpperCAmelCase ) -> tuple: return (data["data"], data["target"]) def lowercase_ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> np.ndarray: lowerCAmelCase__ : List[Any] = XGBRegressor(verbosity=0 , random_state=42 ) xgb.fit(__UpperCAmelCase , __UpperCAmelCase ) # Predict target for test data lowerCAmelCase__ : Dict = xgb.predict(__UpperCAmelCase ) lowerCAmelCase__ : Any = predictions.reshape(len(__UpperCAmelCase ) , 1 ) return predictions def lowercase_ ( ) -> None: lowerCAmelCase__ : Optional[Any] = fetch_california_housing() lowerCAmelCase__ , lowerCAmelCase__ : Union[str, Any] = data_handling(__UpperCAmelCase ) lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ : List[Any] = train_test_split( __UpperCAmelCase , __UpperCAmelCase , test_size=0.25 , random_state=1 ) lowerCAmelCase__ : Optional[Any] = xgboost(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) # Error printing print(f"""Mean Absolute Error : {mean_absolute_error(__UpperCAmelCase , __UpperCAmelCase )}""" ) print(f"""Mean Square Error : {mean_squared_error(__UpperCAmelCase , __UpperCAmelCase )}""" ) if __name__ == "__main__": import doctest doctest.testmod(verbose=True) main()
242
0
import copy from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import ClassLabel, Features, Image from .base import TaskTemplate @dataclass(frozen=UpperCAmelCase ) class a ( UpperCAmelCase ): _lowercase = field(default="image-classification" , metadata={"include_in_asdict_even_if_is_default": True} ) _lowercase = Features({"image": Image()} ) _lowercase = Features({"labels": ClassLabel} ) _lowercase = "image" _lowercase = "labels" def _UpperCAmelCase ( self , A_ ): '''simple docstring''' if self.label_column not in features: raise ValueError(f'Column {self.label_column} is not present in features.' ) if not isinstance(features[self.label_column] , A_ ): raise ValueError(f'Column {self.label_column} is not a ClassLabel.' ) _UpperCAmelCase : Optional[int] = copy.deepcopy(self ) _UpperCAmelCase : Union[str, Any] = self.label_schema.copy() _UpperCAmelCase : Union[str, Any] = features[self.label_column] _UpperCAmelCase : Union[str, Any] = label_schema return task_template @property def _UpperCAmelCase ( self ): '''simple docstring''' return { self.image_column: "image", self.label_column: "labels", }
352
from __future__ import annotations from random import choice def __SCREAMING_SNAKE_CASE ( lowerCAmelCase: Any ) -> Optional[int]: return choice(lowerCAmelCase ) def __SCREAMING_SNAKE_CASE ( lowerCAmelCase: list[int] , lowerCAmelCase: int ) -> int: _UpperCAmelCase : List[Any] = random_pivot(lowerCAmelCase ) # partition based on pivot # linear time _UpperCAmelCase : List[str] = [e for e in lst if e < pivot] _UpperCAmelCase : Any = [e for e in lst if e > pivot] # if we get lucky, pivot might be the element we want. # we can easily see this: # small (elements smaller than k) # + pivot (kth element) # + big (elements larger than k) if len(lowerCAmelCase ) == k - 1: return pivot # pivot is in elements bigger than k elif len(lowerCAmelCase ) < k - 1: return kth_number(lowerCAmelCase , k - len(lowerCAmelCase ) - 1 ) # pivot is in elements smaller than k else: return kth_number(lowerCAmelCase , lowerCAmelCase ) if __name__ == "__main__": import doctest doctest.testmod()
189
0
'''simple docstring''' from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, convert_to_rgb, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging __SCREAMING_SNAKE_CASE :Optional[int] = logging.get_logger(__name__) if is_vision_available(): import PIL class A_ ( A_ ): _lowerCamelCase : Union[str, Any] = ["pixel_values"] def __init__( self : Tuple , snake_case_ : str = True , snake_case_ : List[str] = None , snake_case_ : Union[str, Any] = PILImageResampling.BICUBIC , snake_case_ : Dict = True , snake_case_ : Optional[int] = None , snake_case_ : Any = True , snake_case_ : str = 1 / 2_5_5 , snake_case_ : str = True , snake_case_ : Optional[Any] = None , snake_case_ : Union[str, Any] = None , snake_case_ : Union[str, Any] = True , **snake_case_ : List[str] , ): super().__init__(**A_ ) _UpperCAmelCase = size if size is not None else {"shortest_edge": 2_2_4} _UpperCAmelCase = get_size_dict(A_ , default_to_square=A_ ) _UpperCAmelCase = crop_size if crop_size is not None else {"height": 2_2_4, "width": 2_2_4} _UpperCAmelCase = get_size_dict(A_ , default_to_square=A_ , param_name="crop_size" ) _UpperCAmelCase = do_resize _UpperCAmelCase = size _UpperCAmelCase = resample _UpperCAmelCase = do_center_crop _UpperCAmelCase = crop_size _UpperCAmelCase = do_rescale _UpperCAmelCase = rescale_factor _UpperCAmelCase = do_normalize _UpperCAmelCase = image_mean if image_mean is not None else OPENAI_CLIP_MEAN _UpperCAmelCase = image_std if image_std is not None else OPENAI_CLIP_STD _UpperCAmelCase = do_convert_rgb def lowercase ( self : Tuple , snake_case_ : Tuple , snake_case_ : Optional[Any] , snake_case_ : Dict = PILImageResampling.BICUBIC , snake_case_ : List[Any] = None , **snake_case_ : List[str] , ): _UpperCAmelCase = get_size_dict(A_ , default_to_square=A_ ) if "shortest_edge" not in size: raise ValueError(f'The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}' ) _UpperCAmelCase = get_resize_output_image_size(A_ , size=size["shortest_edge"] , default_to_square=A_ ) return resize(A_ , size=A_ , resample=A_ , data_format=A_ , **A_ ) def lowercase ( self : int , snake_case_ : int , snake_case_ : int , snake_case_ : str = None , **snake_case_ : Tuple , ): _UpperCAmelCase = get_size_dict(A_ ) if "height" not in size or "width" not in size: raise ValueError(f'The `size` parameter must contain the keys (height, width). Got {size.keys()}' ) return center_crop(A_ , size=(size["height"], size["width"]) , data_format=A_ , **A_ ) def lowercase ( self : Any , snake_case_ : Dict , snake_case_ : Union[str, Any] , snake_case_ : List[Any] = None , **snake_case_ : Optional[int] , ): return rescale(A_ , scale=A_ , data_format=A_ , **A_ ) def lowercase ( self : int , snake_case_ : Tuple , snake_case_ : Any , snake_case_ : Union[str, Any] , snake_case_ : Optional[int] = None , **snake_case_ : Tuple , ): return normalize(A_ , mean=A_ , std=A_ , data_format=A_ , **A_ ) def lowercase ( self : Union[str, Any] , snake_case_ : Optional[Any] , snake_case_ : Optional[Any] = None , snake_case_ : int = None , snake_case_ : str = None , snake_case_ : Tuple = None , snake_case_ : Tuple = None , snake_case_ : Any = None , snake_case_ : Dict = None , snake_case_ : Any = None , snake_case_ : Any = None , snake_case_ : Optional[int] = None , snake_case_ : Optional[int] = None , snake_case_ : int = None , snake_case_ : List[str] = ChannelDimension.FIRST , **snake_case_ : int , ): _UpperCAmelCase = do_resize if do_resize is not None else self.do_resize _UpperCAmelCase = size if size is not None else self.size _UpperCAmelCase = get_size_dict(A_ , param_name="size" , default_to_square=A_ ) _UpperCAmelCase = resample if resample is not None else self.resample _UpperCAmelCase = do_center_crop if do_center_crop is not None else self.do_center_crop _UpperCAmelCase = crop_size if crop_size is not None else self.crop_size _UpperCAmelCase = get_size_dict(A_ , param_name="crop_size" , default_to_square=A_ ) _UpperCAmelCase = do_rescale if do_rescale is not None else self.do_rescale _UpperCAmelCase = rescale_factor if rescale_factor is not None else self.rescale_factor _UpperCAmelCase = do_normalize if do_normalize is not None else self.do_normalize _UpperCAmelCase = image_mean if image_mean is not None else self.image_mean _UpperCAmelCase = image_std if image_std is not None else self.image_std _UpperCAmelCase = do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb _UpperCAmelCase = make_list_of_images(A_ ) if not valid_images(A_ ): raise ValueError( "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, " "torch.Tensor, tf.Tensor or jax.ndarray." ) if do_resize and size is None: raise ValueError("Size must be specified if do_resize is True." ) if do_center_crop and crop_size is None: raise ValueError("Crop size must be specified if do_center_crop is True." ) if do_rescale and rescale_factor is None: raise ValueError("Rescale factor must be specified if do_rescale is True." ) if do_normalize and (image_mean is None or image_std is None): raise ValueError("Image mean and std must be specified if do_normalize is True." ) # PIL RGBA images are converted to RGB if do_convert_rgb: _UpperCAmelCase = [convert_to_rgb(A_ ) for image in images] # All transformations expect numpy arrays. _UpperCAmelCase = [to_numpy_array(A_ ) for image in images] if do_resize: _UpperCAmelCase = [self.resize(image=A_ , size=A_ , resample=A_ ) for image in images] if do_center_crop: _UpperCAmelCase = [self.center_crop(image=A_ , size=A_ ) for image in images] if do_rescale: _UpperCAmelCase = [self.rescale(image=A_ , scale=A_ ) for image in images] if do_normalize: _UpperCAmelCase = [self.normalize(image=A_ , mean=A_ , std=A_ ) for image in images] _UpperCAmelCase = [to_channel_dimension_format(A_ , A_ ) for image in images] _UpperCAmelCase = {"pixel_values": images} return BatchFeature(data=A_ , tensor_type=A_ )
22
from __future__ import annotations import math class UpperCAmelCase__ : """simple docstring""" def __init__( self , A_ ) -> None: __UpperCamelCase =size # approximate the overall size of segment tree with given value __UpperCamelCase =[0 for i in range(0 , 4 * size )] # create array to store lazy update __UpperCamelCase =[0 for i in range(0 , 4 * size )] __UpperCamelCase =[0 for i in range(0 , 4 * size )] # flag for lazy update def _a ( self , A_ ) -> int: return idx * 2 def _a ( self , A_ ) -> int: return idx * 2 + 1 def _a ( self , A_ , A_ , A_ , A_ ) -> None: if left_element == right_element: __UpperCamelCase =a[left_element - 1] else: __UpperCamelCase =(left_element + right_element) // 2 self.build(self.left(A_ ) , A_ , A_ , A_ ) self.build(self.right(A_ ) , mid + 1 , A_ , A_ ) __UpperCamelCase =max( self.segment_tree[self.left(A_ )] , self.segment_tree[self.right(A_ )] ) def _a ( self , A_ , A_ , A_ , A_ , A_ , A_ ) -> bool: if self.flag[idx] is True: __UpperCamelCase =self.lazy[idx] __UpperCamelCase =False if left_element != right_element: __UpperCamelCase =self.lazy[idx] __UpperCamelCase =self.lazy[idx] __UpperCamelCase =True __UpperCamelCase =True if right_element < a or left_element > b: return True if left_element >= a and right_element <= b: __UpperCamelCase =val if left_element != right_element: __UpperCamelCase =val __UpperCamelCase =val __UpperCamelCase =True __UpperCamelCase =True return True __UpperCamelCase =(left_element + right_element) // 2 self.update(self.left(A_ ) , A_ , A_ , A_ , A_ , A_ ) self.update(self.right(A_ ) , mid + 1 , A_ , A_ , A_ , A_ ) __UpperCamelCase =max( self.segment_tree[self.left(A_ )] , self.segment_tree[self.right(A_ )] ) return True def _a ( self , A_ , A_ , A_ , A_ , A_ ) -> int | float: if self.flag[idx] is True: __UpperCamelCase =self.lazy[idx] __UpperCamelCase =False if left_element != right_element: __UpperCamelCase =self.lazy[idx] __UpperCamelCase =self.lazy[idx] __UpperCamelCase =True __UpperCamelCase =True if right_element < a or left_element > b: return -math.inf if left_element >= a and right_element <= b: return self.segment_tree[idx] __UpperCamelCase =(left_element + right_element) // 2 __UpperCamelCase =self.query(self.left(A_ ) , A_ , A_ , A_ , A_ ) __UpperCamelCase =self.query(self.right(A_ ) , mid + 1 , A_ , A_ , A_ ) return max(A_ , A_ ) def __str__( self ) -> str: return str([self.query(1 , 1 , self.size , A_ , A_ ) for i in range(1 , self.size + 1 )] ) if __name__ == "__main__": _A = [1, 2, -4, 7, 3, -5, 6, 11, -20, 9, 14, 15, 5, 2, -8] _A = 15 _A = SegmentTree(size) segt.build(1, 1, size, A) print(segt.query(1, 1, size, 4, 6)) print(segt.query(1, 1, size, 7, 11)) print(segt.query(1, 1, size, 7, 12)) segt.update(1, 1, size, 1, 3, 111) print(segt.query(1, 1, size, 1, 15)) segt.update(1, 1, size, 7, 8, 235) print(segt)
62
0
'''simple docstring''' from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, ChannelDimension, ImageInput, PILImageResampling, is_batched, to_numpy_array, valid_images, ) from ...utils import TensorType, logging lowerCAmelCase : List[str] =logging.get_logger(__name__) class a_ ( _lowerCAmelCase ): __A = ["pixel_values"] def __init__( self : List[str] , lowercase : bool = True , lowercase : Optional[Dict[str, int]] = None , lowercase : PILImageResampling = PILImageResampling.BICUBIC , lowercase : bool = True , lowercase : bool = True , lowercase : Union[int, float] = 1 / 255 , lowercase : Dict[str, int] = None , lowercase : bool = True , lowercase : Optional[Union[float, List[float]]] = None , lowercase : Optional[Union[float, List[float]]] = None , **lowercase : int , ): """simple docstring""" super().__init__(**lowercase ) lowercase_ :int = size if size is not None else {"height": 224, "width": 224} lowercase_ :Union[str, Any] = get_size_dict(lowercase ) lowercase_ :Optional[int] = crop_size if crop_size is not None else {"height": 224, "width": 224} lowercase_ :Any = get_size_dict(lowercase , default_to_square=lowercase , param_name="crop_size" ) lowercase_ :Optional[int] = do_resize lowercase_ :Tuple = do_rescale lowercase_ :Optional[Any] = do_normalize lowercase_ :List[Any] = do_center_crop lowercase_ :Optional[Any] = crop_size lowercase_ :int = size lowercase_ :Union[str, Any] = resample lowercase_ :Any = rescale_factor lowercase_ :Any = image_mean if image_mean is not None else IMAGENET_DEFAULT_MEAN lowercase_ :List[Any] = image_std if image_std is not None else IMAGENET_DEFAULT_STD def lowercase__ ( self : int , lowercase : np.ndarray , lowercase : Dict[str, int] , lowercase : PILImageResampling = PILImageResampling.BILINEAR , lowercase : Optional[Union[str, ChannelDimension]] = None , **lowercase : Tuple , ): """simple docstring""" lowercase_ :Tuple = get_size_dict(lowercase ) if "shortest_edge" in size: lowercase_ :List[str] = get_resize_output_image_size(lowercase , size=size["shortest_edge"] , default_to_square=lowercase ) # size = get_resize_output_image_size(image, size["shortest_edge"], size["longest_edge"]) elif "height" in size and "width" in size: lowercase_ :Tuple = (size["height"], size["width"]) else: raise ValueError(F'Size must contain \'height\' and \'width\' keys or \'shortest_edge\' key. Got {size.keys()}' ) return resize(lowercase , size=lowercase , resample=lowercase , data_format=lowercase , **lowercase ) def lowercase__ ( self : Optional[int] , lowercase : np.ndarray , lowercase : Dict[str, int] , lowercase : Optional[Union[str, ChannelDimension]] = None , **lowercase : List[Any] , ): """simple docstring""" lowercase_ :Any = get_size_dict(lowercase ) if "height" not in size or "width" not in size: raise ValueError(F'The `size` parameter must contain the keys (height, width). Got {size.keys()}' ) return center_crop(lowercase , size=(size["height"], size["width"]) , data_format=lowercase , **lowercase ) def lowercase__ ( self : Union[str, Any] , lowercase : np.ndarray , lowercase : float , lowercase : Optional[Union[str, ChannelDimension]] = None , **lowercase : Tuple ): """simple docstring""" return rescale(lowercase , scale=lowercase , data_format=lowercase , **lowercase ) def lowercase__ ( self : Any , lowercase : np.ndarray , lowercase : Union[float, List[float]] , lowercase : Union[float, List[float]] , lowercase : Optional[Union[str, ChannelDimension]] = None , **lowercase : Optional[int] , ): """simple docstring""" return normalize(lowercase , mean=lowercase , std=lowercase , data_format=lowercase , **lowercase ) def lowercase__ ( self : Optional[Any] , lowercase : ImageInput , lowercase : Optional[bool] = None , lowercase : Dict[str, int] = None , lowercase : PILImageResampling = None , lowercase : bool = None , lowercase : int = None , lowercase : Optional[bool] = None , lowercase : Optional[float] = None , lowercase : Optional[bool] = None , lowercase : Optional[Union[float, List[float]]] = None , lowercase : Optional[Union[float, List[float]]] = None , lowercase : Optional[Union[str, TensorType]] = None , lowercase : Union[str, ChannelDimension] = ChannelDimension.FIRST , **lowercase : str , ): """simple docstring""" lowercase_ :Any = do_resize if do_resize is not None else self.do_resize lowercase_ :Any = do_rescale if do_rescale is not None else self.do_rescale lowercase_ :Tuple = do_normalize if do_normalize is not None else self.do_normalize lowercase_ :str = do_center_crop if do_center_crop is not None else self.do_center_crop lowercase_ :str = crop_size if crop_size is not None else self.crop_size lowercase_ :str = get_size_dict(lowercase , param_name="crop_size" , default_to_square=lowercase ) lowercase_ :Tuple = resample if resample is not None else self.resample lowercase_ :Dict = rescale_factor if rescale_factor is not None else self.rescale_factor lowercase_ :Union[str, Any] = image_mean if image_mean is not None else self.image_mean lowercase_ :Union[str, Any] = image_std if image_std is not None else self.image_std lowercase_ :Union[str, Any] = size if size is not None else self.size lowercase_ :str = get_size_dict(lowercase ) if not is_batched(lowercase ): lowercase_ :str = [images] if not valid_images(lowercase ): raise ValueError( "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, " "torch.Tensor, tf.Tensor or jax.ndarray." ) if do_resize and size is None: raise ValueError("Size must be specified if do_resize is True." ) if do_center_crop and crop_size is None: raise ValueError("Crop size must be specified if do_center_crop is True." ) if do_rescale and rescale_factor is None: raise ValueError("Rescale factor must be specified if do_rescale is True." ) # All transformations expect numpy arrays. lowercase_ :List[Any] = [to_numpy_array(lowercase ) for image in images] if do_resize: lowercase_ :List[Any] = [self.resize(image=lowercase , size=lowercase , resample=lowercase ) for image in images] if do_center_crop: lowercase_ :Dict = [self.center_crop(image=lowercase , size=lowercase ) for image in images] if do_rescale: lowercase_ :Tuple = [self.rescale(image=lowercase , scale=lowercase ) for image in images] if do_normalize: lowercase_ :Any = [self.normalize(image=lowercase , mean=lowercase , std=lowercase ) for image in images] lowercase_ :List[Any] = [to_channel_dimension_format(lowercase , lowercase ) for image in images] lowercase_ :Any = {"pixel_values": images} return BatchFeature(data=lowercase , tensor_type=lowercase )
147
'''simple docstring''' # Copyright 2023 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCAmelCase : Dict ={ '''configuration_xmod''': [ '''XMOD_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''XmodConfig''', '''XmodOnnxConfig''', ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase : Union[str, Any] =[ '''XMOD_PRETRAINED_MODEL_ARCHIVE_LIST''', '''XmodForCausalLM''', '''XmodForMaskedLM''', '''XmodForMultipleChoice''', '''XmodForQuestionAnswering''', '''XmodForSequenceClassification''', '''XmodForTokenClassification''', '''XmodModel''', '''XmodPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_xmod import XMOD_PRETRAINED_CONFIG_ARCHIVE_MAP, XmodConfig, XmodOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xmod import ( XMOD_PRETRAINED_MODEL_ARCHIVE_LIST, XmodForCausalLM, XmodForMaskedLM, XmodForMultipleChoice, XmodForQuestionAnswering, XmodForSequenceClassification, XmodForTokenClassification, XmodModel, XmodPreTrainedModel, ) else: import sys lowerCAmelCase : Optional[Any] =_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
147
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) __lowerCAmelCase : Dict ={ 'configuration_albert': ['ALBERT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'AlbertConfig', 'AlbertOnnxConfig'], } try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase : List[str] =['AlbertTokenizer'] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase : Any =['AlbertTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase : Optional[Any] =[ 'ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST', 'AlbertForMaskedLM', 'AlbertForMultipleChoice', 'AlbertForPreTraining', 'AlbertForQuestionAnswering', 'AlbertForSequenceClassification', 'AlbertForTokenClassification', 'AlbertModel', 'AlbertPreTrainedModel', 'load_tf_weights_in_albert', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase : Any =[ 'TF_ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFAlbertForMaskedLM', 'TFAlbertForMultipleChoice', 'TFAlbertForPreTraining', 'TFAlbertForQuestionAnswering', 'TFAlbertForSequenceClassification', 'TFAlbertForTokenClassification', 'TFAlbertMainLayer', 'TFAlbertModel', 'TFAlbertPreTrainedModel', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase : Optional[int] =[ 'FlaxAlbertForMaskedLM', 'FlaxAlbertForMultipleChoice', 'FlaxAlbertForPreTraining', 'FlaxAlbertForQuestionAnswering', 'FlaxAlbertForSequenceClassification', 'FlaxAlbertForTokenClassification', 'FlaxAlbertModel', 'FlaxAlbertPreTrainedModel', ] if TYPE_CHECKING: from .configuration_albert import ALBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, AlbertConfig, AlbertOnnxConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_albert import AlbertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_albert_fast import AlbertTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_albert import ( ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST, AlbertForMaskedLM, AlbertForMultipleChoice, AlbertForPreTraining, AlbertForQuestionAnswering, AlbertForSequenceClassification, AlbertForTokenClassification, AlbertModel, AlbertPreTrainedModel, load_tf_weights_in_albert, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_albert import ( TF_ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST, TFAlbertForMaskedLM, TFAlbertForMultipleChoice, TFAlbertForPreTraining, TFAlbertForQuestionAnswering, TFAlbertForSequenceClassification, TFAlbertForTokenClassification, TFAlbertMainLayer, TFAlbertModel, TFAlbertPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_albert import ( FlaxAlbertForMaskedLM, FlaxAlbertForMultipleChoice, FlaxAlbertForPreTraining, FlaxAlbertForQuestionAnswering, FlaxAlbertForSequenceClassification, FlaxAlbertForTokenClassification, FlaxAlbertModel, FlaxAlbertPreTrainedModel, ) else: import sys __lowerCAmelCase : Tuple =_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
9
'''simple docstring''' import importlib.metadata import operator import re import sys from typing import Optional from packaging import version __snake_case : Tuple = { '<': operator.lt, '<=': operator.le, '==': operator.eq, '!=': operator.ne, '>=': operator.ge, '>': operator.gt, } def __lowerCamelCase ( __snake_case : List[str], __snake_case : List[Any], __snake_case : Optional[Any], __snake_case : List[Any], __snake_case : Any, __snake_case : Any ) -> int: """simple docstring""" if got_ver is None or want_ver is None: raise ValueError( f"Unable to compare versions for {requirement}: need={want_ver} found={got_ver}. This is unusual. Consider" f" reinstalling {pkg}." ) if not ops[op](version.parse(__snake_case ), version.parse(__snake_case ) ): raise ImportError( f"{requirement} is required for a normal functioning of this module, but found {pkg}=={got_ver}.{hint}" ) def __lowerCamelCase ( __snake_case : str, __snake_case : Optional[str] = None ) -> None: """simple docstring""" A__ : int =f"\n{hint}" if hint is not None else """""" # non-versioned check if re.match(r"""^[\w_\-\d]+$""", __snake_case ): A__ , A__ , A__ : Union[str, Any] =requirement, None, None else: A__ : List[str] =re.findall(r"""^([^!=<>\s]+)([\s!=<>]{1,2}.+)""", __snake_case ) if not match: raise ValueError( """requirement needs to be in the pip package format, .e.g., package_a==1.23, or package_b>=1.23, but""" f" got {requirement}" ) A__ , A__ : Optional[int] =match[0] A__ : Tuple =want_full.split(""",""" ) # there could be multiple requirements A__ : Optional[Any] ={} for w in want_range: A__ : Tuple =re.findall(r"""^([\s!=<>]{1,2})(.+)""", __snake_case ) if not match: raise ValueError( """requirement needs to be in the pip package format, .e.g., package_a==1.23, or package_b>=1.23,""" f" but got {requirement}" ) A__ , A__ : str =match[0] A__ : Optional[Any] =want_ver if op not in ops: raise ValueError(f"{requirement}: need one of {list(ops.keys() )}, but got {op}" ) # special case if pkg == "python": A__ : Any =""".""".join([str(__snake_case ) for x in sys.version_info[:3]] ) for op, want_ver in wanted.items(): _compare_versions(__snake_case, __snake_case, __snake_case, __snake_case, __snake_case, __snake_case ) return # check if any version is installed try: A__ : Optional[int] =importlib.metadata.version(__snake_case ) except importlib.metadata.PackageNotFoundError: raise importlib.metadata.PackageNotFoundError( f"The '{requirement}' distribution was not found and is required by this application. {hint}" ) # check that the right version is installed if version number or a range was provided if want_ver is not None: for op, want_ver in wanted.items(): _compare_versions(__snake_case, __snake_case, __snake_case, __snake_case, __snake_case, __snake_case ) def __lowerCamelCase ( __snake_case : List[Any] ) -> int: """simple docstring""" A__ : str ="""Try: pip install transformers -U or pip install -e '.[dev]' if you're working with git main""" return require_version(__snake_case, __snake_case )
134
0
'''simple docstring''' import itertools from dataclasses import dataclass from typing import List, Optional import pyarrow as pa import pyarrow.parquet as pq import datasets from datasets.table import table_cast __SCREAMING_SNAKE_CASE :List[str] = datasets.utils.logging.get_logger(__name__) @dataclass class A_ ( datasets.BuilderConfig ): _lowerCamelCase : int = 1_00_00 _lowerCamelCase : Optional[List[str]] = None _lowerCamelCase : Optional[datasets.Features] = None class A_ ( datasets.ArrowBasedBuilder ): _lowerCamelCase : List[str] = ParquetConfig def lowercase ( self : List[str] ): return datasets.DatasetInfo(features=self.config.features ) def lowercase ( self : Tuple , snake_case_ : Union[str, Any] ): if not self.config.data_files: raise ValueError(f'At least one data file must be specified, but got data_files={self.config.data_files}' ) _UpperCAmelCase = dl_manager.download_and_extract(self.config.data_files ) if isinstance(snake_case_ , (str, list, tuple) ): _UpperCAmelCase = data_files if isinstance(snake_case_ , snake_case_ ): _UpperCAmelCase = [files] # Use `dl_manager.iter_files` to skip hidden files in an extracted archive _UpperCAmelCase = [dl_manager.iter_files(snake_case_ ) for file in files] return [datasets.SplitGenerator(name=datasets.Split.TRAIN , gen_kwargs={"files": files} )] _UpperCAmelCase = [] for split_name, files in data_files.items(): if isinstance(snake_case_ , snake_case_ ): _UpperCAmelCase = [files] # Use `dl_manager.iter_files` to skip hidden files in an extracted archive _UpperCAmelCase = [dl_manager.iter_files(snake_case_ ) for file in files] # Infer features is they are stoed in the arrow schema if self.info.features is None: for file in itertools.chain.from_iterable(snake_case_ ): with open(snake_case_ , "rb" ) as f: _UpperCAmelCase = datasets.Features.from_arrow_schema(pq.read_schema(snake_case_ ) ) break splits.append(datasets.SplitGenerator(name=snake_case_ , gen_kwargs={"files": files} ) ) return splits def lowercase ( self : List[str] , snake_case_ : pa.Table ): if self.info.features is not None: # more expensive cast to support nested features with keys in a different order # allows str <-> int/float or str to Audio for example _UpperCAmelCase = table_cast(snake_case_ , self.info.features.arrow_schema ) return pa_table def lowercase ( self : Optional[Any] , snake_case_ : Optional[int] ): _UpperCAmelCase = self.info.features.arrow_schema if self.info.features is not None else None if self.info.features is not None and self.config.columns is not None: if sorted(field.name for field in schema ) != sorted(self.config.columns ): raise ValueError( f'Tried to load parquet data with columns \'{self.config.columns}\' with mismatching features \'{self.info.features}\'' ) for file_idx, file in enumerate(itertools.chain.from_iterable(snake_case_ ) ): with open(snake_case_ , "rb" ) as f: _UpperCAmelCase = pq.ParquetFile(snake_case_ ) try: for batch_idx, record_batch in enumerate( parquet_file.iter_batches(batch_size=self.config.batch_size , columns=self.config.columns ) ): _UpperCAmelCase = pa.Table.from_batches([record_batch] ) # 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 f'{file_idx}_{batch_idx}', self._cast_table(snake_case_ ) except ValueError as e: logger.error(f'Failed to read file \'{file}\' with error {type(snake_case_ )}: {e}' ) raise
156
'''simple docstring''' import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_bert import BertTokenizer __SCREAMING_SNAKE_CASE :List[str] = logging.get_logger(__name__) __SCREAMING_SNAKE_CASE :Any = {'''vocab_file''': '''vocab.txt''', '''tokenizer_file''': '''tokenizer.json'''} __SCREAMING_SNAKE_CASE :List[Any] = { '''vocab_file''': { '''bert-base-uncased''': '''https://huggingface.co/bert-base-uncased/resolve/main/vocab.txt''', '''bert-large-uncased''': '''https://huggingface.co/bert-large-uncased/resolve/main/vocab.txt''', '''bert-base-cased''': '''https://huggingface.co/bert-base-cased/resolve/main/vocab.txt''', '''bert-large-cased''': '''https://huggingface.co/bert-large-cased/resolve/main/vocab.txt''', '''bert-base-multilingual-uncased''': ( '''https://huggingface.co/bert-base-multilingual-uncased/resolve/main/vocab.txt''' ), '''bert-base-multilingual-cased''': '''https://huggingface.co/bert-base-multilingual-cased/resolve/main/vocab.txt''', '''bert-base-chinese''': '''https://huggingface.co/bert-base-chinese/resolve/main/vocab.txt''', '''bert-base-german-cased''': '''https://huggingface.co/bert-base-german-cased/resolve/main/vocab.txt''', '''bert-large-uncased-whole-word-masking''': ( '''https://huggingface.co/bert-large-uncased-whole-word-masking/resolve/main/vocab.txt''' ), '''bert-large-cased-whole-word-masking''': ( '''https://huggingface.co/bert-large-cased-whole-word-masking/resolve/main/vocab.txt''' ), '''bert-large-uncased-whole-word-masking-finetuned-squad''': ( '''https://huggingface.co/bert-large-uncased-whole-word-masking-finetuned-squad/resolve/main/vocab.txt''' ), '''bert-large-cased-whole-word-masking-finetuned-squad''': ( '''https://huggingface.co/bert-large-cased-whole-word-masking-finetuned-squad/resolve/main/vocab.txt''' ), '''bert-base-cased-finetuned-mrpc''': ( '''https://huggingface.co/bert-base-cased-finetuned-mrpc/resolve/main/vocab.txt''' ), '''bert-base-german-dbmdz-cased''': '''https://huggingface.co/bert-base-german-dbmdz-cased/resolve/main/vocab.txt''', '''bert-base-german-dbmdz-uncased''': ( '''https://huggingface.co/bert-base-german-dbmdz-uncased/resolve/main/vocab.txt''' ), '''TurkuNLP/bert-base-finnish-cased-v1''': ( '''https://huggingface.co/TurkuNLP/bert-base-finnish-cased-v1/resolve/main/vocab.txt''' ), '''TurkuNLP/bert-base-finnish-uncased-v1''': ( '''https://huggingface.co/TurkuNLP/bert-base-finnish-uncased-v1/resolve/main/vocab.txt''' ), '''wietsedv/bert-base-dutch-cased''': ( '''https://huggingface.co/wietsedv/bert-base-dutch-cased/resolve/main/vocab.txt''' ), }, '''tokenizer_file''': { '''bert-base-uncased''': '''https://huggingface.co/bert-base-uncased/resolve/main/tokenizer.json''', '''bert-large-uncased''': '''https://huggingface.co/bert-large-uncased/resolve/main/tokenizer.json''', '''bert-base-cased''': '''https://huggingface.co/bert-base-cased/resolve/main/tokenizer.json''', '''bert-large-cased''': '''https://huggingface.co/bert-large-cased/resolve/main/tokenizer.json''', '''bert-base-multilingual-uncased''': ( '''https://huggingface.co/bert-base-multilingual-uncased/resolve/main/tokenizer.json''' ), '''bert-base-multilingual-cased''': ( '''https://huggingface.co/bert-base-multilingual-cased/resolve/main/tokenizer.json''' ), '''bert-base-chinese''': '''https://huggingface.co/bert-base-chinese/resolve/main/tokenizer.json''', '''bert-base-german-cased''': '''https://huggingface.co/bert-base-german-cased/resolve/main/tokenizer.json''', '''bert-large-uncased-whole-word-masking''': ( '''https://huggingface.co/bert-large-uncased-whole-word-masking/resolve/main/tokenizer.json''' ), '''bert-large-cased-whole-word-masking''': ( '''https://huggingface.co/bert-large-cased-whole-word-masking/resolve/main/tokenizer.json''' ), '''bert-large-uncased-whole-word-masking-finetuned-squad''': ( '''https://huggingface.co/bert-large-uncased-whole-word-masking-finetuned-squad/resolve/main/tokenizer.json''' ), '''bert-large-cased-whole-word-masking-finetuned-squad''': ( '''https://huggingface.co/bert-large-cased-whole-word-masking-finetuned-squad/resolve/main/tokenizer.json''' ), '''bert-base-cased-finetuned-mrpc''': ( '''https://huggingface.co/bert-base-cased-finetuned-mrpc/resolve/main/tokenizer.json''' ), '''bert-base-german-dbmdz-cased''': ( '''https://huggingface.co/bert-base-german-dbmdz-cased/resolve/main/tokenizer.json''' ), '''bert-base-german-dbmdz-uncased''': ( '''https://huggingface.co/bert-base-german-dbmdz-uncased/resolve/main/tokenizer.json''' ), '''TurkuNLP/bert-base-finnish-cased-v1''': ( '''https://huggingface.co/TurkuNLP/bert-base-finnish-cased-v1/resolve/main/tokenizer.json''' ), '''TurkuNLP/bert-base-finnish-uncased-v1''': ( '''https://huggingface.co/TurkuNLP/bert-base-finnish-uncased-v1/resolve/main/tokenizer.json''' ), '''wietsedv/bert-base-dutch-cased''': ( '''https://huggingface.co/wietsedv/bert-base-dutch-cased/resolve/main/tokenizer.json''' ), }, } __SCREAMING_SNAKE_CASE :str = { '''bert-base-uncased''': 512, '''bert-large-uncased''': 512, '''bert-base-cased''': 512, '''bert-large-cased''': 512, '''bert-base-multilingual-uncased''': 512, '''bert-base-multilingual-cased''': 512, '''bert-base-chinese''': 512, '''bert-base-german-cased''': 512, '''bert-large-uncased-whole-word-masking''': 512, '''bert-large-cased-whole-word-masking''': 512, '''bert-large-uncased-whole-word-masking-finetuned-squad''': 512, '''bert-large-cased-whole-word-masking-finetuned-squad''': 512, '''bert-base-cased-finetuned-mrpc''': 512, '''bert-base-german-dbmdz-cased''': 512, '''bert-base-german-dbmdz-uncased''': 512, '''TurkuNLP/bert-base-finnish-cased-v1''': 512, '''TurkuNLP/bert-base-finnish-uncased-v1''': 512, '''wietsedv/bert-base-dutch-cased''': 512, } __SCREAMING_SNAKE_CASE :int = { '''bert-base-uncased''': {'''do_lower_case''': True}, '''bert-large-uncased''': {'''do_lower_case''': True}, '''bert-base-cased''': {'''do_lower_case''': False}, '''bert-large-cased''': {'''do_lower_case''': False}, '''bert-base-multilingual-uncased''': {'''do_lower_case''': True}, '''bert-base-multilingual-cased''': {'''do_lower_case''': False}, '''bert-base-chinese''': {'''do_lower_case''': False}, '''bert-base-german-cased''': {'''do_lower_case''': False}, '''bert-large-uncased-whole-word-masking''': {'''do_lower_case''': True}, '''bert-large-cased-whole-word-masking''': {'''do_lower_case''': False}, '''bert-large-uncased-whole-word-masking-finetuned-squad''': {'''do_lower_case''': True}, '''bert-large-cased-whole-word-masking-finetuned-squad''': {'''do_lower_case''': False}, '''bert-base-cased-finetuned-mrpc''': {'''do_lower_case''': False}, '''bert-base-german-dbmdz-cased''': {'''do_lower_case''': False}, '''bert-base-german-dbmdz-uncased''': {'''do_lower_case''': True}, '''TurkuNLP/bert-base-finnish-cased-v1''': {'''do_lower_case''': False}, '''TurkuNLP/bert-base-finnish-uncased-v1''': {'''do_lower_case''': True}, '''wietsedv/bert-base-dutch-cased''': {'''do_lower_case''': False}, } class A_ ( lowerCAmelCase_ ): _lowerCamelCase : Dict = VOCAB_FILES_NAMES _lowerCamelCase : Optional[Any] = PRETRAINED_VOCAB_FILES_MAP _lowerCamelCase : Optional[Any] = PRETRAINED_INIT_CONFIGURATION _lowerCamelCase : Optional[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _lowerCamelCase : int = BertTokenizer def __init__( self : Union[str, Any] , snake_case_ : List[str]=None , snake_case_ : List[str]=None , snake_case_ : Optional[int]=True , snake_case_ : Optional[Any]="[UNK]" , snake_case_ : List[str]="[SEP]" , snake_case_ : List[Any]="[PAD]" , snake_case_ : int="[CLS]" , snake_case_ : Dict="[MASK]" , snake_case_ : Any=True , snake_case_ : int=None , **snake_case_ : Optional[int] , ): super().__init__( snake_case_ , tokenizer_file=snake_case_ , do_lower_case=snake_case_ , unk_token=snake_case_ , sep_token=snake_case_ , pad_token=snake_case_ , cls_token=snake_case_ , mask_token=snake_case_ , tokenize_chinese_chars=snake_case_ , strip_accents=snake_case_ , **snake_case_ , ) _UpperCAmelCase = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get("lowercase" , snake_case_ ) != do_lower_case or normalizer_state.get("strip_accents" , snake_case_ ) != strip_accents or normalizer_state.get("handle_chinese_chars" , snake_case_ ) != tokenize_chinese_chars ): _UpperCAmelCase = getattr(snake_case_ , normalizer_state.pop("type" ) ) _UpperCAmelCase = do_lower_case _UpperCAmelCase = strip_accents _UpperCAmelCase = tokenize_chinese_chars _UpperCAmelCase = normalizer_class(**snake_case_ ) _UpperCAmelCase = do_lower_case def lowercase ( self : str , snake_case_ : str , snake_case_ : Any=None ): _UpperCAmelCase = [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 lowercase ( self : Optional[Any] , snake_case_ : List[int] , snake_case_ : Optional[List[int]] = None ): _UpperCAmelCase = [self.sep_token_id] _UpperCAmelCase = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def lowercase ( self : Any , snake_case_ : str , snake_case_ : Optional[str] = None ): _UpperCAmelCase = self._tokenizer.model.save(snake_case_ , name=snake_case_ ) return tuple(snake_case_ )
156
1
'''simple docstring''' a__ : int = { 0: '0', 1: '1', 2: '2', 3: '3', 4: '4', 5: '5', 6: '6', 7: '7', 8: '8', 9: '9', 1_0: 'a', 1_1: 'b', 1_2: 'c', 1_3: 'd', 1_4: 'e', 1_5: 'f', } def _lowercase ( __A ): '''simple docstring''' assert type(_SCREAMING_SNAKE_CASE ) in (int, float) and decimal == int(_SCREAMING_SNAKE_CASE ) __UpperCamelCase = int(_SCREAMING_SNAKE_CASE ) __UpperCamelCase = """""" __UpperCamelCase = False if decimal < 0: __UpperCamelCase = True decimal *= -1 while decimal > 0: __UpperCamelCase , __UpperCamelCase = divmod(_SCREAMING_SNAKE_CASE ,16 ) __UpperCamelCase = values[remainder] + hexadecimal __UpperCamelCase = """0x""" + hexadecimal if negative: __UpperCamelCase = """-""" + hexadecimal return hexadecimal if __name__ == "__main__": import doctest doctest.testmod()
349
"""simple docstring""" import os import re import warnings from shutil import copyfile from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import PreTrainedTokenizer if TYPE_CHECKING: from ...tokenization_utils_base import TextInput from ...utils import logging __SCREAMING_SNAKE_CASE : Union[str, Any] = logging.get_logger(__name__) __SCREAMING_SNAKE_CASE : List[Any] = {'vocab_file': 'spiece.model'} __SCREAMING_SNAKE_CASE : int = { 'vocab_file': { 't5-small': 'https://huggingface.co/t5-small/resolve/main/spiece.model', 't5-base': 'https://huggingface.co/t5-base/resolve/main/spiece.model', 't5-large': 'https://huggingface.co/t5-large/resolve/main/spiece.model', 't5-3b': 'https://huggingface.co/t5-3b/resolve/main/spiece.model', 't5-11b': 'https://huggingface.co/t5-11b/resolve/main/spiece.model', } } # TODO(PVP) - this should be removed in Transformers v5 __SCREAMING_SNAKE_CASE : Dict = { 't5-small': 512, 't5-base': 512, 't5-large': 512, 't5-3b': 512, 't5-11b': 512, } __SCREAMING_SNAKE_CASE : Optional[int] = '▁' class __A (snake_case__): '''simple docstring''' __lowercase: Optional[int] = VOCAB_FILES_NAMES __lowercase: Any = PRETRAINED_VOCAB_FILES_MAP __lowercase: Any = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowercase: List[str] = ["""input_ids""", """attention_mask"""] def __init__( self : Optional[int] , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : Optional[Any]="</s>" , UpperCAmelCase_ : Optional[Any]="<unk>" , UpperCAmelCase_ : Any="<pad>" , UpperCAmelCase_ : Tuple=100 , UpperCAmelCase_ : Optional[Any]=None , UpperCAmelCase_ : Optional[Dict[str, Any]] = None , UpperCAmelCase_ : Optional[int]=True , **UpperCAmelCase_ : Dict , ) ->None: """simple docstring""" if extra_ids > 0 and additional_special_tokens is None: snake_case_ = [F"""<extra_id_{i}>""" for i in range(UpperCAmelCase_ )] elif extra_ids > 0 and additional_special_tokens is not None: # Check that we have the right number of extra_id special tokens snake_case_ = len(set(filter(lambda UpperCAmelCase_ : bool("""extra_id""" in str(UpperCAmelCase_ ) ) , UpperCAmelCase_ ) ) ) if extra_tokens != extra_ids: raise ValueError( F"""Both extra_ids ({extra_ids}) and additional_special_tokens ({additional_special_tokens}) are""" """ provided to T5Tokenizer. In this case the additional_special_tokens must include the extra_ids""" """ tokens""" ) if legacy: logger.warning_once( F"""You are using the legacy behaviour of the {self.__class__}. This means that tokens that come after special tokens will not be properly handled. We recommend you to""" """ read the related pull request available at https://github.com/huggingface/transformers/pull/24565""" ) snake_case_ = legacy snake_case_ = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( eos_token=UpperCAmelCase_ , unk_token=UpperCAmelCase_ , pad_token=UpperCAmelCase_ , extra_ids=UpperCAmelCase_ , additional_special_tokens=UpperCAmelCase_ , sp_model_kwargs=self.sp_model_kwargs , legacy=UpperCAmelCase_ , **UpperCAmelCase_ , ) snake_case_ = vocab_file snake_case_ = extra_ids snake_case_ = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(UpperCAmelCase_ ) @staticmethod def lowerCAmelCase ( UpperCAmelCase_ : str , UpperCAmelCase_ : Dict , UpperCAmelCase_ : Optional[Any] ) ->Union[str, Any]: """simple docstring""" if pretrained_model_name_or_path in TaTokenizer.max_model_input_sizes: snake_case_ = TaTokenizer.max_model_input_sizes[pretrained_model_name_or_path] if init_max_model_length is not None and init_max_model_length != max_model_length: return init_max_model_length elif init_max_model_length is None: warnings.warn( """This tokenizer was incorrectly instantiated with a model max length of""" F""" {deprecated_max_model_length} which will be corrected in Transformers v5.\nFor now, this""" """ behavior is kept to avoid breaking backwards compatibility when padding/encoding with""" """ `truncation is True`.\n- Be aware that you SHOULD NOT rely on""" F""" {pretrained_model_name_or_path} automatically truncating your input to""" F""" {deprecated_max_model_length} when padding/encoding.\n- If you want to encode/pad to sequences""" F""" longer than {deprecated_max_model_length} you can either instantiate this tokenizer with""" """ `model_max_length` or pass `max_length` when encoding/padding.\n- To avoid this warning, please""" """ instantiate this tokenizer with `model_max_length` set to your preferred value.""" , UpperCAmelCase_ , ) return max_model_length @property def lowerCAmelCase ( self : Optional[Any] ) ->Optional[Any]: """simple docstring""" return self.sp_model.get_piece_size() + self._extra_ids def lowerCAmelCase ( self : Any ) ->Optional[int]: """simple docstring""" snake_case_ = {self.convert_ids_to_tokens(UpperCAmelCase_ ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def lowerCAmelCase ( self : List[str] , UpperCAmelCase_ : List[int] , UpperCAmelCase_ : Optional[List[int]] = None , UpperCAmelCase_ : bool = False ) ->List[int]: """simple docstring""" if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=UpperCAmelCase_ , token_ids_a=UpperCAmelCase_ , already_has_special_tokens=UpperCAmelCase_ ) # normal case: some special tokens if token_ids_a is None: return ([0] * len(UpperCAmelCase_ )) + [1] return ([0] * len(UpperCAmelCase_ )) + [1] + ([0] * len(UpperCAmelCase_ )) + [1] def lowerCAmelCase ( self : Any ) ->List[str]: """simple docstring""" return list( set(filter(lambda UpperCAmelCase_ : bool(re.search(R"""<extra_id_\d+>""" , UpperCAmelCase_ ) ) is not None , self.additional_special_tokens ) ) ) def lowerCAmelCase ( self : Dict ) ->str: """simple docstring""" return [self._convert_token_to_id(UpperCAmelCase_ ) for token in self.get_sentinel_tokens()] def lowerCAmelCase ( self : Dict , UpperCAmelCase_ : List[int] ) ->List[int]: """simple docstring""" if len(UpperCAmelCase_ ) > 0 and token_ids[-1] == self.eos_token_id: warnings.warn( F"""This sequence already has {self.eos_token}. In future versions this behavior may lead to duplicated""" """ eos tokens being added.""" ) return token_ids else: return token_ids + [self.eos_token_id] def lowerCAmelCase ( self : str , UpperCAmelCase_ : List[int] , UpperCAmelCase_ : Optional[List[int]] = None ) ->List[int]: """simple docstring""" snake_case_ = [self.eos_token_id] if token_ids_a is None: return len(token_ids_a + eos ) * [0] return len(token_ids_a + eos + token_ids_a + eos ) * [0] def lowerCAmelCase ( self : Optional[int] , UpperCAmelCase_ : List[int] , UpperCAmelCase_ : Optional[List[int]] = None ) ->List[int]: """simple docstring""" snake_case_ = self._add_eos_if_not_present(UpperCAmelCase_ ) if token_ids_a is None: return token_ids_a else: snake_case_ = self._add_eos_if_not_present(UpperCAmelCase_ ) return token_ids_a + token_ids_a def __getstate__( self : Optional[Any] ) ->Tuple: """simple docstring""" snake_case_ = self.__dict__.copy() snake_case_ = None return state def __setstate__( self : Optional[Any] , UpperCAmelCase_ : List[Any] ) ->List[Any]: """simple docstring""" snake_case_ = d # for backward compatibility if not hasattr(self , """sp_model_kwargs""" ): snake_case_ = {} snake_case_ = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def lowerCAmelCase ( self : int , UpperCAmelCase_ : "TextInput" , **UpperCAmelCase_ : Tuple ) ->List[str]: """simple docstring""" if not self.legacy: snake_case_ = SPIECE_UNDERLINE + text.replace(UpperCAmelCase_ , """ """ ) return super().tokenize(UpperCAmelCase_ , **UpperCAmelCase_ ) def lowerCAmelCase ( self : Dict , UpperCAmelCase_ : Tuple , **UpperCAmelCase_ : Any ) ->Tuple: """simple docstring""" if not self.legacy: snake_case_ = text.startswith(UpperCAmelCase_ ) if is_first: snake_case_ = text[1:] snake_case_ = self.sp_model.encode(UpperCAmelCase_ , out_type=UpperCAmelCase_ ) if not self.legacy and not is_first and not text.startswith(""" """ ) and tokens[0].startswith(UpperCAmelCase_ ): snake_case_ = ([tokens[0][1:]] if len(tokens[0] ) > 1 else []) + tokens[1:] return tokens def lowerCAmelCase ( self : List[str] , UpperCAmelCase_ : List[Any] ) ->Tuple: """simple docstring""" if token.startswith("""<extra_id_""" ): snake_case_ = re.match(R"""<extra_id_(\d+)>""" , UpperCAmelCase_ ) snake_case_ = int(match.group(1 ) ) return self.vocab_size - num - 1 return self.sp_model.piece_to_id(UpperCAmelCase_ ) def lowerCAmelCase ( self : List[str] , UpperCAmelCase_ : Optional[Any] ) ->List[Any]: """simple docstring""" if index < self.sp_model.get_piece_size(): snake_case_ = self.sp_model.IdToPiece(UpperCAmelCase_ ) else: snake_case_ = F"""<extra_id_{self.vocab_size - 1 - index}>""" return token def lowerCAmelCase ( self : List[Any] , UpperCAmelCase_ : List[str] ) ->Optional[Any]: """simple docstring""" snake_case_ = [] snake_case_ = """""" snake_case_ = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: if not prev_is_special: out_string += " " out_string += self.sp_model.decode(UpperCAmelCase_ ) + token snake_case_ = True snake_case_ = [] else: current_sub_tokens.append(UpperCAmelCase_ ) snake_case_ = False out_string += self.sp_model.decode(UpperCAmelCase_ ) return out_string.strip() def lowerCAmelCase ( self : str , UpperCAmelCase_ : str , UpperCAmelCase_ : Optional[str] = None ) ->Tuple[str]: """simple docstring""" if not os.path.isdir(UpperCAmelCase_ ): logger.error(F"""Vocabulary path ({save_directory}) should be a directory""" ) return snake_case_ = os.path.join( UpperCAmelCase_ , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(UpperCAmelCase_ ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , UpperCAmelCase_ ) elif not os.path.isfile(self.vocab_file ): with open(UpperCAmelCase_ , """wb""" ) as fi: snake_case_ = self.sp_model.serialized_model_proto() fi.write(UpperCAmelCase_ ) return (out_vocab_file,)
347
0
"""simple docstring""" import baseaa def a__ ( A__ ): return baseaa.baaencode(string.encode('utf-8' ) ) def a__ ( A__ ): return baseaa.baadecode(A__ ).decode('utf-8' ) if __name__ == "__main__": lowerCAmelCase__ : str ='Hello World!' lowerCAmelCase__ : int =baseaa_encode(test) print(encoded) lowerCAmelCase__ : Dict =baseaa_decode(encoded) print(decoded)
364
import math import unittest def a__ ( A__ ): assert isinstance(A__, A__ ) and ( number >= 0 ), "'number' must been an int and positive" 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(A__ ) + 1 ), 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True class __lowercase (unittest.TestCase ): """simple docstring""" def UpperCamelCase__ ( self ): """simple docstring""" self.assertTrue(is_prime(2 ) ) self.assertTrue(is_prime(3 ) ) self.assertTrue(is_prime(5 ) ) self.assertTrue(is_prime(7 ) ) self.assertTrue(is_prime(1_1 ) ) self.assertTrue(is_prime(1_3 ) ) self.assertTrue(is_prime(1_7 ) ) self.assertTrue(is_prime(1_9 ) ) self.assertTrue(is_prime(2_3 ) ) self.assertTrue(is_prime(2_9 ) ) def UpperCamelCase__ ( self ): """simple docstring""" with self.assertRaises(lowerCAmelCase__ ): is_prime(-1_9 ) self.assertFalse( is_prime(0 ) , 'Zero doesn\'t have any positive factors, primes must have exactly two.' , ) self.assertFalse( is_prime(1 ) , 'One only has 1 positive factor, primes must have exactly two.' , ) self.assertFalse(is_prime(2 * 2 ) ) self.assertFalse(is_prime(2 * 3 ) ) self.assertFalse(is_prime(3 * 3 ) ) self.assertFalse(is_prime(3 * 5 ) ) self.assertFalse(is_prime(3 * 5 * 7 ) ) if __name__ == "__main__": unittest.main()
162
0
from __future__ import annotations class __lowerCAmelCase : def __init__( self: Union[str, Any] , _lowerCAmelCase: int ): lowercase :Optional[int] = data lowercase :Node | None = None lowercase :Node | None = None def UpperCAmelCase__ ( lowerCamelCase ): # In Order traversal of the tree if tree: display(tree.left ) print(tree.data ) display(tree.right ) def UpperCAmelCase__ ( lowerCamelCase ): return 1 + max(depth_of_tree(tree.left ), depth_of_tree(tree.right ) ) if tree else 0 def UpperCAmelCase__ ( lowerCamelCase ): if not tree: return True if tree.left and tree.right: return is_full_binary_tree(tree.left ) and is_full_binary_tree(tree.right ) else: return not tree.left and not tree.right def UpperCAmelCase__ ( ): # Main function for testing. lowercase :Optional[Any] = Node(1 ) lowercase :Dict = Node(2 ) lowercase :Optional[int] = Node(3 ) lowercase :Dict = Node(4 ) lowercase :int = Node(5 ) lowercase :Union[str, Any] = Node(6 ) lowercase :Optional[int] = Node(7 ) lowercase :Union[str, Any] = Node(8 ) lowercase :List[Any] = Node(9 ) print(is_full_binary_tree(lowerCamelCase ) ) print(depth_of_tree(lowerCamelCase ) ) print("Tree is: " ) display(lowerCamelCase ) if __name__ == "__main__": main()
236
from typing import Union import fire import torch from tqdm import tqdm def UpperCAmelCase__ ( lowerCamelCase, lowerCamelCase = "cpu", lowerCamelCase = None ): lowercase :Optional[Any] = torch.load(lowerCamelCase, map_location=lowerCamelCase ) for k, v in tqdm(state_dict.items() ): if not isinstance(lowerCamelCase, torch.Tensor ): raise TypeError("FP16 conversion only works on paths that are saved state dicts, like pytorch_model.bin" ) lowercase :List[Any] = v.half() if save_path is None: # overwrite src_path lowercase :Optional[Any] = src_path torch.save(lowerCamelCase, lowerCamelCase ) if __name__ == "__main__": fire.Fire(convert)
236
1
'''simple docstring''' def SCREAMING_SNAKE_CASE__ ( snake_case : int ) -> int: """simple docstring""" if n == 1 or not isinstance(snake_case , snake_case ): return 0 elif n == 2: return 1 else: a : Any = [0, 1] for i in range(2 , n + 1 ): sequence.append(sequence[i - 1] + sequence[i - 2] ) return sequence[n] def SCREAMING_SNAKE_CASE__ ( snake_case : int ) -> int: """simple docstring""" a : Dict = 0 a : List[Any] = 2 while digits < n: index += 1 a : str = len(str(fibonacci(snake_case ) ) ) return index def SCREAMING_SNAKE_CASE__ ( snake_case : int = 1_000 ) -> int: """simple docstring""" return fibonacci_digits_index(snake_case ) if __name__ == "__main__": print(solution(int(str(input()).strip())))
363
'''simple docstring''' import math from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCamelCase : str = logging.get_logger(__name__) UpperCamelCase : List[str] = { """facebook/data2vec-base-960h""": """https://huggingface.co/facebook/data2vec-audio-base-960h/resolve/main/config.json""", # See all Data2VecAudio models at https://huggingface.co/models?filter=data2vec-audio } class UpperCamelCase ( a_ ): """simple docstring""" A : Optional[int] = "data2vec-audio" def __init__( self : Dict , UpperCAmelCase_ : Optional[int]=3_2 , UpperCAmelCase_ : Union[str, Any]=7_6_8 , UpperCAmelCase_ : Dict=1_2 , UpperCAmelCase_ : str=1_2 , UpperCAmelCase_ : Optional[Any]=3_0_7_2 , UpperCAmelCase_ : Dict="gelu" , UpperCAmelCase_ : Union[str, Any]=0.1 , UpperCAmelCase_ : Optional[int]=0.1 , UpperCAmelCase_ : int=0.1 , UpperCAmelCase_ : Union[str, Any]=0.0 , UpperCAmelCase_ : Union[str, Any]=0.1 , UpperCAmelCase_ : Dict=0.1 , UpperCAmelCase_ : Optional[Any]=0.02 , UpperCAmelCase_ : str=1e-5 , UpperCAmelCase_ : List[Any]="gelu" , UpperCAmelCase_ : Optional[Any]=(5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2) , UpperCAmelCase_ : Dict=(5, 2, 2, 2, 2, 2, 2) , UpperCAmelCase_ : int=(1_0, 3, 3, 3, 3, 2, 2) , UpperCAmelCase_ : List[Any]=False , UpperCAmelCase_ : List[Any]=1_6 , UpperCAmelCase_ : Optional[Any]=1_9 , UpperCAmelCase_ : int=5 , UpperCAmelCase_ : Any=0.05 , UpperCAmelCase_ : Dict=1_0 , UpperCAmelCase_ : Optional[Any]=2 , UpperCAmelCase_ : Optional[int]=0.0 , UpperCAmelCase_ : Tuple=1_0 , UpperCAmelCase_ : int=0 , UpperCAmelCase_ : Any="sum" , UpperCAmelCase_ : Optional[int]=False , UpperCAmelCase_ : Dict=False , UpperCAmelCase_ : Optional[int]=2_5_6 , UpperCAmelCase_ : Any=(5_1_2, 5_1_2, 5_1_2, 5_1_2, 1_5_0_0) , UpperCAmelCase_ : Optional[Any]=(5, 3, 3, 1, 1) , UpperCAmelCase_ : Optional[int]=(1, 2, 3, 1, 1) , UpperCAmelCase_ : int=5_1_2 , UpperCAmelCase_ : str=0 , UpperCAmelCase_ : Tuple=1 , UpperCAmelCase_ : Union[str, Any]=2 , UpperCAmelCase_ : List[str]=False , UpperCAmelCase_ : Any=3 , UpperCAmelCase_ : Dict=2 , UpperCAmelCase_ : int=3 , UpperCAmelCase_ : List[Any]=None , **UpperCAmelCase_ : int , ): """simple docstring""" super().__init__(**UpperCAmelCase_ , pad_token_id=UpperCAmelCase_ , bos_token_id=UpperCAmelCase_ , eos_token_id=UpperCAmelCase_) a : List[Any] = hidden_size a : Any = feat_extract_activation a : Any = list(UpperCAmelCase_) a : Optional[int] = list(UpperCAmelCase_) a : Dict = list(UpperCAmelCase_) a : Tuple = conv_bias a : str = num_conv_pos_embeddings a : Dict = num_conv_pos_embedding_groups a : Optional[Any] = conv_pos_kernel_size a : Any = len(self.conv_dim) a : Tuple = num_hidden_layers a : Any = intermediate_size a : Any = hidden_act a : Dict = num_attention_heads a : Dict = hidden_dropout a : Union[str, Any] = attention_dropout a : Dict = activation_dropout a : Optional[int] = feat_proj_dropout a : Tuple = final_dropout a : Union[str, Any] = layerdrop a : Tuple = layer_norm_eps a : Dict = initializer_range a : Tuple = vocab_size a : int = use_weighted_layer_sum if ( (len(self.conv_stride) != self.num_feat_extract_layers) or (len(self.conv_kernel) != self.num_feat_extract_layers) or (len(self.conv_dim) != self.num_feat_extract_layers) ): raise ValueError( 'Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` ==' ' `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) =' f""" {len(self.conv_dim)}`, `len(config.conv_stride) = {len(self.conv_stride)}`,""" f""" `len(config.conv_kernel) = {len(self.conv_kernel)}`.""") # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 a : List[str] = mask_time_prob a : int = mask_time_length a : Optional[int] = mask_time_min_masks a : Dict = mask_feature_prob a : List[str] = mask_feature_length a : str = mask_feature_min_masks # ctc loss a : str = ctc_loss_reduction a : Optional[Any] = ctc_zero_infinity # adapter a : List[str] = add_adapter a : Optional[Any] = adapter_kernel_size a : int = adapter_stride a : str = num_adapter_layers a : Optional[Any] = output_hidden_size or hidden_size # SequenceClassification-specific parameter. Feel free to ignore for other classes. a : str = classifier_proj_size # XVector-specific parameters. Feel free to ignore for other classes. a : List[Any] = list(UpperCAmelCase_) a : List[str] = list(UpperCAmelCase_) a : str = list(UpperCAmelCase_) a : Optional[Any] = xvector_output_dim @property def SCREAMING_SNAKE_CASE_ ( self : Dict): """simple docstring""" return math.prod(self.conv_stride)
345
0
from typing import Optional, Tuple, Union import flax import flax.linen as nn import jax import jax.numpy as jnp from flax.core.frozen_dict import FrozenDict from ..configuration_utils import ConfigMixin, flax_register_to_config from ..utils import BaseOutput from .embeddings_flax import FlaxTimestepEmbedding, FlaxTimesteps from .modeling_flax_utils import FlaxModelMixin from .unet_ad_blocks_flax import ( FlaxCrossAttnDownBlockaD, FlaxDownBlockaD, FlaxUNetMidBlockaDCrossAttn, ) @flax.struct.dataclass class lowercase ( SCREAMING_SNAKE_CASE__ ): lowercase_ : jnp.ndarray lowercase_ : jnp.ndarray class lowercase ( nn.Module ): lowercase_ : int lowercase_ : Tuple[int] =(16, 32, 96, 256) lowercase_ : jnp.dtype =jnp.floataa def A__ ( self): lowercase = nn.Conv( self.block_out_channels[0] ,kernel_size=(3, 3) ,padding=((1, 1), (1, 1)) ,dtype=self.dtype ,) lowercase = [] for i in range(len(self.block_out_channels) - 1): lowercase = self.block_out_channels[i] lowercase = self.block_out_channels[i + 1] lowercase = nn.Conv( A__ ,kernel_size=(3, 3) ,padding=((1, 1), (1, 1)) ,dtype=self.dtype ,) blocks.append(A__) lowercase = nn.Conv( A__ ,kernel_size=(3, 3) ,strides=(2, 2) ,padding=((1, 1), (1, 1)) ,dtype=self.dtype ,) blocks.append(A__) lowercase = blocks lowercase = nn.Conv( self.conditioning_embedding_channels ,kernel_size=(3, 3) ,padding=((1, 1), (1, 1)) ,kernel_init=nn.initializers.zeros_init() ,bias_init=nn.initializers.zeros_init() ,dtype=self.dtype ,) def __call__( self ,A__): lowercase = self.conv_in(A__) lowercase = nn.silu(A__) for block in self.blocks: lowercase = block(A__) lowercase = nn.silu(A__) lowercase = self.conv_out(A__) return embedding @flax_register_to_config class lowercase ( nn.Module , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): lowercase_ : int =32 lowercase_ : int =4 lowercase_ : Tuple[str] =( "CrossAttnDownBlock2D", "CrossAttnDownBlock2D", "CrossAttnDownBlock2D", "DownBlock2D", ) lowercase_ : Union[bool, Tuple[bool]] =False lowercase_ : Tuple[int] =(320, 640, 1280, 1280) lowercase_ : int =2 lowercase_ : Union[int, Tuple[int]] =8 lowercase_ : Optional[Union[int, Tuple[int]]] =None lowercase_ : int =1280 lowercase_ : float =0.0 lowercase_ : bool =False lowercase_ : jnp.dtype =jnp.floataa lowercase_ : bool =True lowercase_ : int =0 lowercase_ : str ="rgb" lowercase_ : Tuple[int] =(16, 32, 96, 256) def A__ ( self ,A__): # init input tensors lowercase = (1, self.in_channels, self.sample_size, self.sample_size) lowercase = jnp.zeros(A__ ,dtype=jnp.floataa) lowercase = jnp.ones((1,) ,dtype=jnp.intaa) lowercase = jnp.zeros((1, 1, self.cross_attention_dim) ,dtype=jnp.floataa) lowercase = (1, 3, self.sample_size * 8, self.sample_size * 8) lowercase = jnp.zeros(A__ ,dtype=jnp.floataa) lowercase , lowercase = jax.random.split(A__) lowercase = {'''params''': params_rng, '''dropout''': dropout_rng} return self.init(A__ ,A__ ,A__ ,A__ ,A__)["params"] def A__ ( self): lowercase = self.block_out_channels lowercase = block_out_channels[0] * 4 # If `num_attention_heads` is not defined (which is the case for most models) # it will default to `attention_head_dim`. This looks weird upon first reading it and it is. # The reason for this behavior is to correct for incorrectly named variables that were introduced # when this library was created. The incorrect naming was only discovered much later in https://github.com/huggingface/diffusers/issues/2011#issuecomment-1547958131 # Changing `attention_head_dim` to `num_attention_heads` for 40,000+ configurations is too backwards breaking # which is why we correct for the naming here. lowercase = self.num_attention_heads or self.attention_head_dim # input lowercase = nn.Conv( block_out_channels[0] ,kernel_size=(3, 3) ,strides=(1, 1) ,padding=((1, 1), (1, 1)) ,dtype=self.dtype ,) # time lowercase = FlaxTimesteps( block_out_channels[0] ,flip_sin_to_cos=self.flip_sin_to_cos ,freq_shift=self.config.freq_shift) lowercase = FlaxTimestepEmbedding(A__ ,dtype=self.dtype) lowercase = FlaxControlNetConditioningEmbedding( conditioning_embedding_channels=block_out_channels[0] ,block_out_channels=self.conditioning_embedding_out_channels ,) lowercase = self.only_cross_attention if isinstance(A__ ,A__): lowercase = (only_cross_attention,) * len(self.down_block_types) if isinstance(A__ ,A__): lowercase = (num_attention_heads,) * len(self.down_block_types) # down lowercase = [] lowercase = [] lowercase = block_out_channels[0] lowercase = nn.Conv( A__ ,kernel_size=(1, 1) ,padding='''VALID''' ,kernel_init=nn.initializers.zeros_init() ,bias_init=nn.initializers.zeros_init() ,dtype=self.dtype ,) controlnet_down_blocks.append(A__) for i, down_block_type in enumerate(self.down_block_types): lowercase = output_channel lowercase = block_out_channels[i] lowercase = i == len(A__) - 1 if down_block_type == "CrossAttnDownBlock2D": lowercase = FlaxCrossAttnDownBlockaD( in_channels=A__ ,out_channels=A__ ,dropout=self.dropout ,num_layers=self.layers_per_block ,num_attention_heads=num_attention_heads[i] ,add_downsample=not is_final_block ,use_linear_projection=self.use_linear_projection ,only_cross_attention=only_cross_attention[i] ,dtype=self.dtype ,) else: lowercase = FlaxDownBlockaD( in_channels=A__ ,out_channels=A__ ,dropout=self.dropout ,num_layers=self.layers_per_block ,add_downsample=not is_final_block ,dtype=self.dtype ,) down_blocks.append(A__) for _ in range(self.layers_per_block): lowercase = nn.Conv( A__ ,kernel_size=(1, 1) ,padding='''VALID''' ,kernel_init=nn.initializers.zeros_init() ,bias_init=nn.initializers.zeros_init() ,dtype=self.dtype ,) controlnet_down_blocks.append(A__) if not is_final_block: lowercase = nn.Conv( A__ ,kernel_size=(1, 1) ,padding='''VALID''' ,kernel_init=nn.initializers.zeros_init() ,bias_init=nn.initializers.zeros_init() ,dtype=self.dtype ,) controlnet_down_blocks.append(A__) lowercase = down_blocks lowercase = controlnet_down_blocks # mid lowercase = block_out_channels[-1] lowercase = FlaxUNetMidBlockaDCrossAttn( in_channels=A__ ,dropout=self.dropout ,num_attention_heads=num_attention_heads[-1] ,use_linear_projection=self.use_linear_projection ,dtype=self.dtype ,) lowercase = nn.Conv( A__ ,kernel_size=(1, 1) ,padding='''VALID''' ,kernel_init=nn.initializers.zeros_init() ,bias_init=nn.initializers.zeros_init() ,dtype=self.dtype ,) def __call__( self ,A__ ,A__ ,A__ ,A__ ,A__ = 1.0 ,A__ = True ,A__ = False ,): lowercase = self.controlnet_conditioning_channel_order if channel_order == "bgr": lowercase = jnp.flip(A__ ,axis=1) # 1. time if not isinstance(A__ ,jnp.ndarray): lowercase = jnp.array([timesteps] ,dtype=jnp.intaa) elif isinstance(A__ ,jnp.ndarray) and len(timesteps.shape) == 0: lowercase = timesteps.astype(dtype=jnp.floataa) lowercase = jnp.expand_dims(A__ ,0) lowercase = self.time_proj(A__) lowercase = self.time_embedding(A__) # 2. pre-process lowercase = jnp.transpose(A__ ,(0, 2, 3, 1)) lowercase = self.conv_in(A__) lowercase = jnp.transpose(A__ ,(0, 2, 3, 1)) lowercase = self.controlnet_cond_embedding(A__) sample += controlnet_cond # 3. down lowercase = (sample,) for down_block in self.down_blocks: if isinstance(A__ ,A__): lowercase , lowercase = down_block(A__ ,A__ ,A__ ,deterministic=not train) else: lowercase , lowercase = down_block(A__ ,A__ ,deterministic=not train) down_block_res_samples += res_samples # 4. mid lowercase = self.mid_block(A__ ,A__ ,A__ ,deterministic=not train) # 5. contronet blocks lowercase = () for down_block_res_sample, controlnet_block in zip(A__ ,self.controlnet_down_blocks): lowercase = controlnet_block(A__) controlnet_down_block_res_samples += (down_block_res_sample,) lowercase = controlnet_down_block_res_samples lowercase = self.controlnet_mid_block(A__) # 6. scaling lowercase = [sample * conditioning_scale for sample in down_block_res_samples] mid_block_res_sample *= conditioning_scale if not return_dict: return (down_block_res_samples, mid_block_res_sample) return FlaxControlNetOutput( down_block_res_samples=A__ ,mid_block_res_sample=A__)
101
import argparse import json from pathlib import Path import torch import torchaudio from datasets import load_dataset from huggingface_hub import hf_hub_download from transformers import ASTConfig, ASTFeatureExtractor, ASTForAudioClassification from transformers.utils import logging logging.set_verbosity_info() lowerCamelCase__ = logging.get_logger(__name__) def lowerCAmelCase__ ( _SCREAMING_SNAKE_CASE : Any ): """simple docstring""" __a = ASTConfig() if "10-10" in model_name: pass elif "speech-commands" in model_name: __a = 128 elif "12-12" in model_name: __a = 12 __a = 12 elif "14-14" in model_name: __a = 14 __a = 14 elif "16-16" in model_name: __a = 16 __a = 16 else: raise ValueError("""Model not supported""" ) __a = """huggingface/label-files""" if "speech-commands" in model_name: __a = 35 __a = """speech-commands-v2-id2label.json""" else: __a = 527 __a = """audioset-id2label.json""" __a = json.load(open(hf_hub_download(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , repo_type="""dataset""" ) , """r""" ) ) __a = {int(_SCREAMING_SNAKE_CASE ): v for k, v in idalabel.items()} __a = idalabel __a = {v: k for k, v in idalabel.items()} return config def lowerCAmelCase__ ( _SCREAMING_SNAKE_CASE : Dict ): """simple docstring""" if "module.v" in name: __a = name.replace("""module.v""" , """audio_spectrogram_transformer""" ) if "cls_token" in name: __a = name.replace("""cls_token""" , """embeddings.cls_token""" ) if "dist_token" in name: __a = name.replace("""dist_token""" , """embeddings.distillation_token""" ) if "pos_embed" in name: __a = name.replace("""pos_embed""" , """embeddings.position_embeddings""" ) if "patch_embed.proj" in name: __a = name.replace("""patch_embed.proj""" , """embeddings.patch_embeddings.projection""" ) # transformer blocks if "blocks" in name: __a = name.replace("""blocks""" , """encoder.layer""" ) if "attn.proj" in name: __a = name.replace("""attn.proj""" , """attention.output.dense""" ) if "attn" in name: __a = name.replace("""attn""" , """attention.self""" ) if "norm1" in name: __a = name.replace("""norm1""" , """layernorm_before""" ) if "norm2" in name: __a = name.replace("""norm2""" , """layernorm_after""" ) if "mlp.fc1" in name: __a = name.replace("""mlp.fc1""" , """intermediate.dense""" ) if "mlp.fc2" in name: __a = name.replace("""mlp.fc2""" , """output.dense""" ) # final layernorm if "audio_spectrogram_transformer.norm" in name: __a = name.replace("""audio_spectrogram_transformer.norm""" , """audio_spectrogram_transformer.layernorm""" ) # classifier head if "module.mlp_head.0" in name: __a = name.replace("""module.mlp_head.0""" , """classifier.layernorm""" ) if "module.mlp_head.1" in name: __a = name.replace("""module.mlp_head.1""" , """classifier.dense""" ) return name def lowerCAmelCase__ ( _SCREAMING_SNAKE_CASE : str , _SCREAMING_SNAKE_CASE : Union[str, Any] ): """simple docstring""" for key in orig_state_dict.copy().keys(): __a = orig_state_dict.pop(_SCREAMING_SNAKE_CASE ) if "qkv" in key: __a = key.split(""".""" ) __a = int(key_split[3] ) __a = config.hidden_size if "weight" in key: __a = val[:dim, :] __a = val[dim : dim * 2, :] __a = val[-dim:, :] else: __a = val[:dim] __a = val[dim : dim * 2] __a = val[-dim:] else: __a = val return orig_state_dict def lowerCAmelCase__ ( _SCREAMING_SNAKE_CASE : List[str] ): """simple docstring""" __a = [ """module.v.head.weight""", """module.v.head.bias""", """module.v.head_dist.weight""", """module.v.head_dist.bias""", ] for k in ignore_keys: state_dict.pop(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) @torch.no_grad() def lowerCAmelCase__ ( _SCREAMING_SNAKE_CASE : int , _SCREAMING_SNAKE_CASE : List[str] , _SCREAMING_SNAKE_CASE : List[str]=False ): """simple docstring""" __a = get_audio_spectrogram_transformer_config(_SCREAMING_SNAKE_CASE ) __a = { """ast-finetuned-audioset-10-10-0.4593""": ( """https://www.dropbox.com/s/ca0b1v2nlxzyeb4/audioset_10_10_0.4593.pth?dl=1""" ), """ast-finetuned-audioset-10-10-0.450""": ( """https://www.dropbox.com/s/1tv0hovue1bxupk/audioset_10_10_0.4495.pth?dl=1""" ), """ast-finetuned-audioset-10-10-0.448""": ( """https://www.dropbox.com/s/6u5sikl4b9wo4u5/audioset_10_10_0.4483.pth?dl=1""" ), """ast-finetuned-audioset-10-10-0.448-v2""": ( """https://www.dropbox.com/s/kt6i0v9fvfm1mbq/audioset_10_10_0.4475.pth?dl=1""" ), """ast-finetuned-audioset-12-12-0.447""": ( """https://www.dropbox.com/s/snfhx3tizr4nuc8/audioset_12_12_0.4467.pth?dl=1""" ), """ast-finetuned-audioset-14-14-0.443""": ( """https://www.dropbox.com/s/z18s6pemtnxm4k7/audioset_14_14_0.4431.pth?dl=1""" ), """ast-finetuned-audioset-16-16-0.442""": ( """https://www.dropbox.com/s/mdsa4t1xmcimia6/audioset_16_16_0.4422.pth?dl=1""" ), """ast-finetuned-speech-commands-v2""": ( """https://www.dropbox.com/s/q0tbqpwv44pquwy/speechcommands_10_10_0.9812.pth?dl=1""" ), } # load original state_dict __a = model_name_to_url[model_name] __a = torch.hub.load_state_dict_from_url(_SCREAMING_SNAKE_CASE , map_location="""cpu""" ) # remove some keys remove_keys(_SCREAMING_SNAKE_CASE ) # rename some keys __a = convert_state_dict(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # load 🤗 model __a = ASTForAudioClassification(_SCREAMING_SNAKE_CASE ) model.eval() model.load_state_dict(_SCREAMING_SNAKE_CASE ) # verify outputs on dummy input # source: https://github.com/YuanGongND/ast/blob/79e873b8a54d0a3b330dd522584ff2b9926cd581/src/run.py#L62 __a = -4.267_7393 if """speech-commands""" not in model_name else -6.84_5978 __a = 4.568_9974 if """speech-commands""" not in model_name else 5.565_4526 __a = 1024 if """speech-commands""" not in model_name else 128 __a = ASTFeatureExtractor(mean=_SCREAMING_SNAKE_CASE , std=_SCREAMING_SNAKE_CASE , max_length=_SCREAMING_SNAKE_CASE ) if "speech-commands" in model_name: __a = load_dataset("""speech_commands""" , """v0.02""" , split="""validation""" ) __a = dataset[0]["""audio"""]["""array"""] else: __a = hf_hub_download( repo_id="""nielsr/audio-spectogram-transformer-checkpoint""" , filename="""sample_audio.flac""" , repo_type="""dataset""" , ) __a , __a = torchaudio.load(_SCREAMING_SNAKE_CASE ) __a = waveform.squeeze().numpy() __a = feature_extractor(_SCREAMING_SNAKE_CASE , sampling_rate=1_6000 , return_tensors="""pt""" ) # forward pass __a = model(**_SCREAMING_SNAKE_CASE ) __a = outputs.logits if model_name == "ast-finetuned-audioset-10-10-0.4593": __a = torch.tensor([-0.8760, -7.0042, -8.6602] ) elif model_name == "ast-finetuned-audioset-10-10-0.450": __a = torch.tensor([-1.1986, -7.0903, -8.2718] ) elif model_name == "ast-finetuned-audioset-10-10-0.448": __a = torch.tensor([-2.6128, -8.0080, -9.4344] ) elif model_name == "ast-finetuned-audioset-10-10-0.448-v2": __a = torch.tensor([-1.5080, -7.4534, -8.8917] ) elif model_name == "ast-finetuned-audioset-12-12-0.447": __a = torch.tensor([-0.5050, -6.5833, -8.0843] ) elif model_name == "ast-finetuned-audioset-14-14-0.443": __a = torch.tensor([-0.3826, -7.0336, -8.2413] ) elif model_name == "ast-finetuned-audioset-16-16-0.442": __a = torch.tensor([-1.2113, -6.9101, -8.3470] ) elif model_name == "ast-finetuned-speech-commands-v2": __a = torch.tensor([6.1589, -8.0566, -8.7984] ) else: raise ValueError("""Unknown model name""" ) if not torch.allclose(logits[0, :3] , _SCREAMING_SNAKE_CASE , atol=1e-4 ): raise ValueError("""Logits don't match""" ) print("""Looks ok!""" ) if pytorch_dump_folder_path is not None: Path(_SCREAMING_SNAKE_CASE ).mkdir(exist_ok=_SCREAMING_SNAKE_CASE ) print(f"Saving model {model_name} to {pytorch_dump_folder_path}" ) model.save_pretrained(_SCREAMING_SNAKE_CASE ) print(f"Saving feature extractor to {pytorch_dump_folder_path}" ) feature_extractor.save_pretrained(_SCREAMING_SNAKE_CASE ) if push_to_hub: print("""Pushing model and feature extractor to the hub...""" ) model.push_to_hub(f"MIT/{model_name}" ) feature_extractor.push_to_hub(f"MIT/{model_name}" ) if __name__ == "__main__": lowerCamelCase__ = argparse.ArgumentParser() # Required parameters parser.add_argument( """--model_name""", default="""ast-finetuned-audioset-10-10-0.4593""", type=str, help="""Name of the Audio Spectrogram Transformer model you'd like to convert.""", ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model directory.""" ) parser.add_argument( """--push_to_hub""", action="""store_true""", help="""Whether or not to push the converted model to the 🤗 hub.""" ) lowerCamelCase__ = parser.parse_args() convert_audio_spectrogram_transformer_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
302
0
import torch from torch import nn class __snake_case ( nn.Module): def __init__( self : List[Any] , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : Optional[int] , __lowerCAmelCase : List[str] , __lowerCAmelCase : int , __lowerCAmelCase : List[str]=1 , __lowerCAmelCase : List[str]=False ): """simple docstring""" super().__init__() _lowerCamelCase : List[str] = n_token _lowerCamelCase : Optional[Any] = d_embed _lowerCamelCase : Dict = d_proj _lowerCamelCase : Optional[int] = cutoffs + [n_token] _lowerCamelCase : Any = [0] + self.cutoffs _lowerCamelCase : List[str] = div_val _lowerCamelCase : str = self.cutoffs[0] _lowerCamelCase : Union[str, Any] = len(self.cutoffs ) - 1 _lowerCamelCase : Optional[int] = self.shortlist_size + self.n_clusters if self.n_clusters > 0: _lowerCamelCase : Dict = nn.Parameter(torch.zeros(self.n_clusters , self.d_embed ) ) _lowerCamelCase : Tuple = nn.Parameter(torch.zeros(self.n_clusters ) ) _lowerCamelCase : Tuple = nn.ModuleList() _lowerCamelCase : Optional[int] = nn.ParameterList() if div_val == 1: for i in range(len(self.cutoffs ) ): if d_proj != d_embed: self.out_projs.append(nn.Parameter(torch.FloatTensor(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) ) else: self.out_projs.append(_SCREAMING_SNAKE_CASE ) self.out_layers.append(nn.Linear(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) else: for i in range(len(self.cutoffs ) ): _lowerCamelCase : Optional[int] = self.cutoff_ends[i], self.cutoff_ends[i + 1] _lowerCamelCase : List[str] = d_embed // (div_val**i) self.out_projs.append(nn.Parameter(torch.FloatTensor(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) ) self.out_layers.append(nn.Linear(_SCREAMING_SNAKE_CASE , r_idx - l_idx ) ) _lowerCamelCase : List[str] = keep_order def SCREAMING_SNAKE_CASE ( self : str , __lowerCAmelCase : int , __lowerCAmelCase : Optional[int] , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : Tuple ): """simple docstring""" if proj is None: _lowerCamelCase : Optional[int] = nn.functional.linear(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , bias=_SCREAMING_SNAKE_CASE ) else: # if CUDA_MAJOR <= 9 and CUDA_MINOR <= 1: _lowerCamelCase : Optional[Any] = nn.functional.linear(_SCREAMING_SNAKE_CASE , proj.t().contiguous() ) _lowerCamelCase : Optional[int] = nn.functional.linear(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , bias=_SCREAMING_SNAKE_CASE ) # else: # logit = torch.einsum('bd,de,ev->bv', (hidden, proj, weight.t())) # if bias is not None: # logit = logit + bias return logit def SCREAMING_SNAKE_CASE ( self : List[Any] , __lowerCAmelCase : List[str] , __lowerCAmelCase : str=None , __lowerCAmelCase : Optional[Any]=False ): """simple docstring""" if labels is not None: # Shift so that tokens < n predict n _lowerCamelCase : Optional[int] = hidden[..., :-1, :].contiguous() _lowerCamelCase : Union[str, Any] = labels[..., 1:].contiguous() _lowerCamelCase : Union[str, Any] = hidden.view(-1 , hidden.size(-1 ) ) _lowerCamelCase : str = labels.view(-1 ) if hidden.size(0 ) != labels.size(0 ): raise RuntimeError('''Input and labels should have the same size in the batch dimension.''' ) else: _lowerCamelCase : Tuple = hidden.view(-1 , hidden.size(-1 ) ) if self.n_clusters == 0: _lowerCamelCase : str = self._compute_logit(_SCREAMING_SNAKE_CASE , self.out_layers[0].weight , self.out_layers[0].bias , self.out_projs[0] ) if labels is not None: _lowerCamelCase : Union[str, Any] = labels != -1_0_0 _lowerCamelCase : str = torch.zeros_like(_SCREAMING_SNAKE_CASE , dtype=hidden.dtype , device=hidden.device ) _lowerCamelCase : Union[str, Any] = ( -nn.functional.log_softmax(_SCREAMING_SNAKE_CASE , dim=-1 )[mask].gather(1 , labels[mask].unsqueeze(1 ) ).squeeze(1 ) ) else: _lowerCamelCase : List[str] = nn.functional.log_softmax(_SCREAMING_SNAKE_CASE , dim=-1 ) else: # construct weights and biases _lowerCamelCase : List[Any] = [], [] for i in range(len(self.cutoffs ) ): if self.div_val == 1: _lowerCamelCase : Union[str, Any] = self.cutoff_ends[i], self.cutoff_ends[i + 1] _lowerCamelCase : Tuple = self.out_layers[0].weight[l_idx:r_idx] _lowerCamelCase : str = self.out_layers[0].bias[l_idx:r_idx] else: _lowerCamelCase : Optional[Any] = self.out_layers[i].weight _lowerCamelCase : str = self.out_layers[i].bias if i == 0: _lowerCamelCase : List[str] = torch.cat([weight_i, self.cluster_weight] , dim=0 ) _lowerCamelCase : List[str] = torch.cat([bias_i, self.cluster_bias] , dim=0 ) weights.append(_SCREAMING_SNAKE_CASE ) biases.append(_SCREAMING_SNAKE_CASE ) _lowerCamelCase : Optional[int] = weights[0], biases[0], self.out_projs[0] _lowerCamelCase : Union[str, Any] = self._compute_logit(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) _lowerCamelCase : Optional[int] = nn.functional.log_softmax(_SCREAMING_SNAKE_CASE , dim=1 ) if labels is None: _lowerCamelCase : str = hidden.new_empty((head_logit.size(0 ), self.n_token) ) else: _lowerCamelCase : Tuple = torch.zeros_like(_SCREAMING_SNAKE_CASE , dtype=hidden.dtype , device=hidden.device ) _lowerCamelCase : Any = 0 _lowerCamelCase : Union[str, Any] = [0] + self.cutoffs for i in range(len(_SCREAMING_SNAKE_CASE ) - 1 ): _lowerCamelCase : Any = cutoff_values[i], cutoff_values[i + 1] if labels is not None: _lowerCamelCase : int = (labels >= l_idx) & (labels < r_idx) _lowerCamelCase : Union[str, Any] = mask_i.nonzero().squeeze() if indices_i.numel() == 0: continue _lowerCamelCase : Tuple = labels.index_select(0 , _SCREAMING_SNAKE_CASE ) - l_idx _lowerCamelCase : List[str] = head_logprob.index_select(0 , _SCREAMING_SNAKE_CASE ) _lowerCamelCase : Optional[int] = hidden.index_select(0 , _SCREAMING_SNAKE_CASE ) else: _lowerCamelCase : Optional[Any] = hidden if i == 0: if labels is not None: _lowerCamelCase : Tuple = head_logprob_i.gather(1 , target_i[:, None] ).squeeze(1 ) else: _lowerCamelCase : Optional[int] = head_logprob[:, : self.cutoffs[0]] else: _lowerCamelCase : Any = weights[i], biases[i], self.out_projs[i] _lowerCamelCase : str = self._compute_logit(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) _lowerCamelCase : List[str] = nn.functional.log_softmax(_SCREAMING_SNAKE_CASE , dim=1 ) _lowerCamelCase : Optional[int] = self.cutoffs[0] + i - 1 # No probability for the head cluster if labels is not None: _lowerCamelCase : Optional[Any] = head_logprob_i[:, cluster_prob_idx] + tail_logprob_i.gather( 1 , target_i[:, None] ).squeeze(1 ) else: _lowerCamelCase : Union[str, Any] = head_logprob[:, cluster_prob_idx, None] + tail_logprob_i _lowerCamelCase : Optional[Any] = logprob_i if labels is not None: if (hasattr(self , '''keep_order''' ) and self.keep_order) or keep_order: out.index_copy_(0 , _SCREAMING_SNAKE_CASE , -logprob_i ) else: out[offset : offset + logprob_i.size(0 )].copy_(-logprob_i ) offset += logprob_i.size(0 ) return out def SCREAMING_SNAKE_CASE ( self : int , __lowerCAmelCase : int ): """simple docstring""" if self.n_clusters == 0: _lowerCamelCase : Optional[int] = self._compute_logit(_SCREAMING_SNAKE_CASE , self.out_layers[0].weight , self.out_layers[0].bias , self.out_projs[0] ) return nn.functional.log_softmax(_SCREAMING_SNAKE_CASE , dim=-1 ) else: # construct weights and biases _lowerCamelCase : Any = [], [] for i in range(len(self.cutoffs ) ): if self.div_val == 1: _lowerCamelCase : Optional[int] = self.cutoff_ends[i], self.cutoff_ends[i + 1] _lowerCamelCase : Tuple = self.out_layers[0].weight[l_idx:r_idx] _lowerCamelCase : List[str] = self.out_layers[0].bias[l_idx:r_idx] else: _lowerCamelCase : str = self.out_layers[i].weight _lowerCamelCase : int = self.out_layers[i].bias if i == 0: _lowerCamelCase : Optional[int] = torch.cat([weight_i, self.cluster_weight] , dim=0 ) _lowerCamelCase : Optional[int] = torch.cat([bias_i, self.cluster_bias] , dim=0 ) weights.append(_SCREAMING_SNAKE_CASE ) biases.append(_SCREAMING_SNAKE_CASE ) _lowerCamelCase : str = weights[0], biases[0], self.out_projs[0] _lowerCamelCase : str = self._compute_logit(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) _lowerCamelCase : int = hidden.new_empty((head_logit.size(0 ), self.n_token) ) _lowerCamelCase : Union[str, Any] = nn.functional.log_softmax(_SCREAMING_SNAKE_CASE , dim=1 ) _lowerCamelCase : Any = [0] + self.cutoffs for i in range(len(_SCREAMING_SNAKE_CASE ) - 1 ): _lowerCamelCase : Union[str, Any] = cutoff_values[i], cutoff_values[i + 1] if i == 0: _lowerCamelCase : List[str] = head_logprob[:, : self.cutoffs[0]] else: _lowerCamelCase : Optional[Any] = weights[i], biases[i], self.out_projs[i] _lowerCamelCase : Tuple = self._compute_logit(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) _lowerCamelCase : Tuple = nn.functional.log_softmax(_SCREAMING_SNAKE_CASE , dim=1 ) _lowerCamelCase : int = head_logprob[:, -i] + tail_logprob_i _lowerCamelCase : List[Any] = logprob_i return out
371
"""simple docstring""" import math def snake_case_ ( A_ : int ): '''simple docstring''' return math.sqrt(A_ ) * math.sqrt(A_ ) == num def snake_case_ ( A_ : int ): '''simple docstring''' _lowerCamelCase : Dict = 0 _lowerCamelCase : Tuple = n while left <= right: _lowerCamelCase : List[str] = (left + right) // 2 if mid**2 == n: return True elif mid**2 > n: _lowerCamelCase : int = mid - 1 else: _lowerCamelCase : Optional[Any] = mid + 1 return False if __name__ == "__main__": import doctest doctest.testmod()
175
0
'''simple docstring''' import logging import os import sys from dataclasses import dataclass, field from typing import Optional import torch from datasets import load_dataset from torchvision.transforms import Compose, Lambda, Normalize, RandomHorizontalFlip, RandomResizedCrop, ToTensor from torchvision.transforms.functional import InterpolationMode import transformers from transformers import ( HfArgumentParser, Trainer, TrainingArguments, ViTImageProcessor, ViTMAEConfig, ViTMAEForPreTraining, ) 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 lowerCamelCase : Optional[int] = logging.getLogger(__name__) # Will error if the minimal version of Transformers is not installed. Remove at your own risks. check_min_version("4.31.0") require_version("datasets>=1.8.0", "To fix: pip install -r examples/pytorch/image-pretraining/requirements.txt") @dataclass class A__ : A__ = field( default='cifar10' , metadata={'help': 'Name of a dataset from the datasets package'} ) A__ = field( default=A__ , metadata={'help': 'The configuration name of the dataset to use (via the datasets library).'} ) A__ = field( default=A__ , metadata={'help': 'The column name of the images in the files.'} ) A__ = field(default=A__ , metadata={'help': 'A folder containing the training data.'} ) A__ = field(default=A__ , metadata={'help': 'A folder containing the validation data.'} ) A__ = field( default=0.15 , metadata={'help': 'Percent to split off of train for validation.'} ) A__ = field( default=A__ , metadata={ 'help': ( 'For debugging purposes or quicker training, truncate the number of training examples to this ' 'value if set.' ) } , ) A__ = field( default=A__ , metadata={ 'help': ( 'For debugging purposes or quicker training, truncate the number of evaluation examples to this ' 'value if set.' ) } , ) def A ( self : Union[str, Any] ) -> int: '''simple docstring''' _SCREAMING_SNAKE_CASE ={} if self.train_dir is not None: _SCREAMING_SNAKE_CASE =self.train_dir if self.validation_dir is not None: _SCREAMING_SNAKE_CASE =self.validation_dir _SCREAMING_SNAKE_CASE =data_files if data_files else None @dataclass class A__ : A__ = field( default=A__ , metadata={ 'help': ( 'The model checkpoint for weights initialization.Don\'t set if you want to train a model from scratch.' ) } , ) A__ = field( default=A__ , metadata={'help': 'Pretrained config name or path if not the same as model_name_or_path'} ) A__ = field( default=A__ , metadata={ 'help': ( 'Override some existing default config settings when a model is trained from scratch. Example: ' 'n_embd=10,resid_pdrop=0.2,scale_attn_weights=false,summary_type=cls_index' ) } , ) A__ = field( default=A__ , metadata={'help': 'Where do you want to store the pretrained models downloaded from s3'} ) A__ = field( default='main' , metadata={'help': 'The specific model version to use (can be a branch name, tag name or commit id).'} , ) A__ = field(default=A__ , metadata={'help': 'Name or path of preprocessor config.'} ) A__ = field( default=A__ , metadata={ 'help': ( 'Will use the token generated when running `huggingface-cli login` (necessary to use this script ' 'with private models).' ) } , ) A__ = field( default=0.75 , metadata={'help': 'The ratio of the number of masked tokens in the input sequence.'} ) A__ = field( default=A__ , metadata={'help': 'Whether or not to train with normalized pixel values as target.'} ) @dataclass class A__ ( A__ ): A__ = field( default=1E-3 , metadata={'help': 'Base learning rate: absolute_lr = base_lr * total_batch_size / 256.'} ) def _lowerCAmelCase ( _UpperCamelCase : int ) -> Tuple: """simple docstring""" _SCREAMING_SNAKE_CASE =torch.stack([example['pixel_values'] for example in examples] ) return {"pixel_values": pixel_values} def _lowerCAmelCase ( ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE =HfArgumentParser((ModelArguments, DataTrainingArguments, CustomTrainingArguments) ) 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. _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =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_mae' , _UpperCamelCase , _UpperCamelCase ) # Setup logging logging.basicConfig( format='%(asctime)s - %(levelname)s - %(name)s - %(message)s' , datefmt='%m/%d/%Y %H:%M:%S' , handlers=[logging.StreamHandler(sys.stdout )] , ) if training_args.should_log: # The default of training_args.log_level is passive, so we set log level at info here to have that default. transformers.utils.logging.set_verbosity_info() _SCREAMING_SNAKE_CASE =training_args.get_process_log_level() logger.setLevel(_UpperCamelCase ) transformers.utils.logging.set_verbosity(_UpperCamelCase ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Log on each process the small summary: logger.warning( f"Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}" + f"distributed training: {bool(training_args.local_rank != -1 )}, 16-bits training: {training_args.fpaa}" ) logger.info(f"Training/evaluation parameters {training_args}" ) # Detecting last checkpoint. _SCREAMING_SNAKE_CASE =None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: _SCREAMING_SNAKE_CASE =get_last_checkpoint(training_args.output_dir ) if last_checkpoint is None and len(os.listdir(training_args.output_dir ) ) > 0: raise ValueError( f"Output directory ({training_args.output_dir}) already exists and is not empty. " 'Use --overwrite_output_dir to overcome.' ) elif last_checkpoint is not None and training_args.resume_from_checkpoint is None: logger.info( f"Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change " 'the `--output_dir` or add `--overwrite_output_dir` to train from scratch.' ) # Initialize our dataset. _SCREAMING_SNAKE_CASE =load_dataset( data_args.dataset_name , data_args.dataset_config_name , data_files=data_args.data_files , cache_dir=model_args.cache_dir , use_auth_token=True if model_args.use_auth_token else None , ) # If we don't have a validation split, split off a percentage of train as validation. _SCREAMING_SNAKE_CASE =None if 'validation' in ds.keys() else data_args.train_val_split if isinstance(data_args.train_val_split , _UpperCamelCase ) and data_args.train_val_split > 0.0: _SCREAMING_SNAKE_CASE =ds['train'].train_test_split(data_args.train_val_split ) _SCREAMING_SNAKE_CASE =split['train'] _SCREAMING_SNAKE_CASE =split['test'] # Load pretrained model and image processor # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. _SCREAMING_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, } if model_args.config_name: _SCREAMING_SNAKE_CASE =ViTMAEConfig.from_pretrained(model_args.config_name , **_UpperCamelCase ) elif model_args.model_name_or_path: _SCREAMING_SNAKE_CASE =ViTMAEConfig.from_pretrained(model_args.model_name_or_path , **_UpperCamelCase ) else: _SCREAMING_SNAKE_CASE =ViTMAEConfig() logger.warning('You are instantiating a new config instance from scratch.' ) if model_args.config_overrides is not None: logger.info(f"Overriding config: {model_args.config_overrides}" ) config.update_from_string(model_args.config_overrides ) logger.info(f"New config: {config}" ) # adapt config config.update( { 'mask_ratio': model_args.mask_ratio, 'norm_pix_loss': model_args.norm_pix_loss, } ) # create image processor if model_args.image_processor_name: _SCREAMING_SNAKE_CASE =ViTImageProcessor.from_pretrained(model_args.image_processor_name , **_UpperCamelCase ) elif model_args.model_name_or_path: _SCREAMING_SNAKE_CASE =ViTImageProcessor.from_pretrained(model_args.model_name_or_path , **_UpperCamelCase ) else: _SCREAMING_SNAKE_CASE =ViTImageProcessor() # create model if model_args.model_name_or_path: _SCREAMING_SNAKE_CASE =ViTMAEForPreTraining.from_pretrained( model_args.model_name_or_path , from_tf=bool('.ckpt' in model_args.model_name_or_path ) , config=_UpperCamelCase , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) else: logger.info('Training new model from scratch' ) _SCREAMING_SNAKE_CASE =ViTMAEForPreTraining(_UpperCamelCase ) if training_args.do_train: _SCREAMING_SNAKE_CASE =ds['train'].column_names else: _SCREAMING_SNAKE_CASE =ds['validation'].column_names if data_args.image_column_name is not None: _SCREAMING_SNAKE_CASE =data_args.image_column_name elif "image" in column_names: _SCREAMING_SNAKE_CASE ='image' elif "img" in column_names: _SCREAMING_SNAKE_CASE ='img' else: _SCREAMING_SNAKE_CASE =column_names[0] # transformations as done in original MAE paper # source: https://github.com/facebookresearch/mae/blob/main/main_pretrain.py if "shortest_edge" in image_processor.size: _SCREAMING_SNAKE_CASE =image_processor.size['shortest_edge'] else: _SCREAMING_SNAKE_CASE =(image_processor.size['height'], image_processor.size['width']) _SCREAMING_SNAKE_CASE =Compose( [ Lambda(lambda _UpperCamelCase : img.convert('RGB' ) if img.mode != "RGB" else img ), RandomResizedCrop(_UpperCamelCase , scale=(0.2, 1.0) , interpolation=InterpolationMode.BICUBIC ), RandomHorizontalFlip(), ToTensor(), Normalize(mean=image_processor.image_mean , std=image_processor.image_std ), ] ) def preprocess_images(_UpperCamelCase : Dict ): _SCREAMING_SNAKE_CASE =[transforms(_UpperCamelCase ) for image in examples[image_column_name]] return examples if training_args.do_train: if "train" not in ds: raise ValueError('--do_train requires a train dataset' ) if data_args.max_train_samples is not None: _SCREAMING_SNAKE_CASE =ds['train'].shuffle(seed=training_args.seed ).select(range(data_args.max_train_samples ) ) # Set the training transforms ds["train"].set_transform(_UpperCamelCase ) if training_args.do_eval: if "validation" not in ds: raise ValueError('--do_eval requires a validation dataset' ) if data_args.max_eval_samples is not None: _SCREAMING_SNAKE_CASE =( ds['validation'].shuffle(seed=training_args.seed ).select(range(data_args.max_eval_samples ) ) ) # Set the validation transforms ds["validation"].set_transform(_UpperCamelCase ) # Compute absolute learning rate _SCREAMING_SNAKE_CASE =( training_args.train_batch_size * training_args.gradient_accumulation_steps * training_args.world_size ) if training_args.base_learning_rate is not None: _SCREAMING_SNAKE_CASE =training_args.base_learning_rate * total_train_batch_size / 2_56 # Initialize our trainer _SCREAMING_SNAKE_CASE =Trainer( model=_UpperCamelCase , args=_UpperCamelCase , train_dataset=ds['train'] if training_args.do_train else None , eval_dataset=ds['validation'] if training_args.do_eval else None , tokenizer=_UpperCamelCase , data_collator=_UpperCamelCase , ) # Training if training_args.do_train: _SCREAMING_SNAKE_CASE =None if training_args.resume_from_checkpoint is not None: _SCREAMING_SNAKE_CASE =training_args.resume_from_checkpoint elif last_checkpoint is not None: _SCREAMING_SNAKE_CASE =last_checkpoint _SCREAMING_SNAKE_CASE =trainer.train(resume_from_checkpoint=_UpperCamelCase ) trainer.save_model() trainer.log_metrics('train' , train_result.metrics ) trainer.save_metrics('train' , train_result.metrics ) trainer.save_state() # Evaluation if training_args.do_eval: _SCREAMING_SNAKE_CASE =trainer.evaluate() trainer.log_metrics('eval' , _UpperCamelCase ) trainer.save_metrics('eval' , _UpperCamelCase ) # Write model card and (optionally) push to hub _SCREAMING_SNAKE_CASE ={ 'tasks': 'masked-auto-encoding', 'dataset': data_args.dataset_name, 'tags': ['masked-auto-encoding'], } if training_args.push_to_hub: trainer.push_to_hub(**_UpperCamelCase ) else: trainer.create_model_card(**_UpperCamelCase ) def _lowerCAmelCase ( _UpperCamelCase : List[str] ) -> Optional[int]: """simple docstring""" main() if __name__ == "__main__": main()
47
import os import sys import warnings from dataclasses import dataclass, field from io import BytesIO from typing import TYPE_CHECKING, Any, ClassVar, Dict, List, Optional, Union import numpy as np import pyarrow as pa from .. import config from ..download.streaming_download_manager import xopen from ..table import array_cast from ..utils.file_utils import is_local_path from ..utils.py_utils import first_non_null_value, no_op_if_value_is_null, string_to_dict if TYPE_CHECKING: import PIL.Image from .features import FeatureType A : Optional[List[str]] = None A : str = "<" if sys.byteorder == "little" else ">" # Origin: https://github.com/python-pillow/Pillow/blob/698951e19e19972aeed56df686868f1329981c12/src/PIL/Image.py#L3126 minus "|i1" which values are not preserved correctly when saving and loading an image A : str = [ np.dtype("|b1"), np.dtype("|u1"), np.dtype("<u2"), np.dtype(">u2"), np.dtype("<i2"), np.dtype(">i2"), np.dtype("<u4"), np.dtype(">u4"), np.dtype("<i4"), np.dtype(">i4"), np.dtype("<f4"), np.dtype(">f4"), np.dtype("<f8"), np.dtype(">f8"), ] @dataclass class _lowercase : """simple docstring""" A__ = True A__ = None # Automatically constructed A__ = "PIL.Image.Image" A__ = pa.struct({"bytes": pa.binary(), "path": pa.string()}) A__ = field(default="Image" , init=lowercase__ , repr=lowercase__) def __call__( self : Any ): '''simple docstring''' return self.pa_type def lowerCAmelCase ( self : Optional[Any] , __lowerCamelCase : Union[str, bytes, dict, np.ndarray, "PIL.Image.Image"] ): '''simple docstring''' if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError("To support encoding images, please install 'Pillow'." ) if isinstance(__lowerCamelCase , __lowerCamelCase ): lowerCamelCase__ : str = np.array(__lowerCamelCase ) if isinstance(__lowerCamelCase , __lowerCamelCase ): return {"path": value, "bytes": None} elif isinstance(__lowerCamelCase , __lowerCamelCase ): return {"path": None, "bytes": value} elif isinstance(__lowerCamelCase , np.ndarray ): # convert the image array to PNG/TIFF bytes return encode_np_array(__lowerCamelCase ) elif isinstance(__lowerCamelCase , PIL.Image.Image ): # convert the PIL image to bytes (default format is PNG/TIFF) return encode_pil_image(__lowerCamelCase ) elif value.get("path" ) is not None and os.path.isfile(value["path"] ): # we set "bytes": None to not duplicate the data if they're already available locally return {"bytes": None, "path": value.get("path" )} elif value.get("bytes" ) is not None or value.get("path" ) is not None: # store the image bytes, and path is used to infer the image format using the file extension return {"bytes": value.get("bytes" ), "path": value.get("path" )} else: raise ValueError( f"An image sample should have one of 'path' or 'bytes' but they are missing or None in {value}." ) def lowerCAmelCase ( self : Any , __lowerCamelCase : dict , __lowerCamelCase : List[Any]=None ): '''simple docstring''' if not self.decode: raise RuntimeError("Decoding is disabled for this feature. Please use Image(decode=True) instead." ) if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError("To support decoding images, please install 'Pillow'." ) if token_per_repo_id is None: lowerCamelCase__ : Union[str, Any] = {} lowerCamelCase__ , lowerCamelCase__ : Optional[int] = value["path"], value["bytes"] if bytes_ is None: if path is None: raise ValueError(f"An image should have one of 'path' or 'bytes' but both are None in {value}." ) else: if is_local_path(__lowerCamelCase ): lowerCamelCase__ : Union[str, Any] = PIL.Image.open(__lowerCamelCase ) else: lowerCamelCase__ : Tuple = path.split("::" )[-1] try: lowerCamelCase__ : str = string_to_dict(__lowerCamelCase , config.HUB_DATASETS_URL )["repo_id"] lowerCamelCase__ : Any = token_per_repo_id.get(__lowerCamelCase ) except ValueError: lowerCamelCase__ : int = None with xopen(__lowerCamelCase , "rb" , use_auth_token=__lowerCamelCase ) as f: lowerCamelCase__ : List[str] = BytesIO(f.read() ) lowerCamelCase__ : Optional[int] = PIL.Image.open(bytes_ ) else: lowerCamelCase__ : Dict = PIL.Image.open(BytesIO(bytes_ ) ) image.load() # to avoid "Too many open files" errors return image def lowerCAmelCase ( self : Dict ): '''simple docstring''' from .features import Value return ( self if self.decode else { "bytes": Value("binary" ), "path": Value("string" ), } ) def lowerCAmelCase ( self : Optional[Any] , __lowerCamelCase : Union[pa.StringArray, pa.StructArray, pa.ListArray] ): '''simple docstring''' if pa.types.is_string(storage.type ): lowerCamelCase__ : Dict = pa.array([None] * len(__lowerCamelCase ) , type=pa.binary() ) lowerCamelCase__ : List[str] = pa.StructArray.from_arrays([bytes_array, storage] , ["bytes", "path"] , mask=storage.is_null() ) elif pa.types.is_binary(storage.type ): lowerCamelCase__ : List[Any] = pa.array([None] * len(__lowerCamelCase ) , type=pa.string() ) lowerCamelCase__ : Any = pa.StructArray.from_arrays([storage, path_array] , ["bytes", "path"] , mask=storage.is_null() ) elif pa.types.is_struct(storage.type ): if storage.type.get_field_index("bytes" ) >= 0: lowerCamelCase__ : Dict = storage.field("bytes" ) else: lowerCamelCase__ : Optional[int] = pa.array([None] * len(__lowerCamelCase ) , type=pa.binary() ) if storage.type.get_field_index("path" ) >= 0: lowerCamelCase__ : Dict = storage.field("path" ) else: lowerCamelCase__ : Dict = pa.array([None] * len(__lowerCamelCase ) , type=pa.string() ) lowerCamelCase__ : int = pa.StructArray.from_arrays([bytes_array, path_array] , ["bytes", "path"] , mask=storage.is_null() ) elif pa.types.is_list(storage.type ): lowerCamelCase__ : Union[str, Any] = pa.array( [encode_np_array(np.array(__lowerCamelCase ) )["bytes"] if arr is not None else None for arr in storage.to_pylist()] , type=pa.binary() , ) lowerCamelCase__ : Dict = pa.array([None] * len(__lowerCamelCase ) , type=pa.string() ) lowerCamelCase__ : Dict = pa.StructArray.from_arrays( [bytes_array, path_array] , ["bytes", "path"] , mask=bytes_array.is_null() ) return array_cast(__lowerCamelCase , self.pa_type ) def lowerCAmelCase ( self : int , __lowerCamelCase : pa.StructArray ): '''simple docstring''' @no_op_if_value_is_null def path_to_bytes(__lowerCamelCase : Union[str, Any] ): with xopen(__lowerCamelCase , "rb" ) as f: lowerCamelCase__ : str = f.read() return bytes_ lowerCamelCase__ : List[Any] = pa.array( [ (path_to_bytes(x["path"] ) if x["bytes"] is None else x["bytes"]) if x is not None else None for x in storage.to_pylist() ] , type=pa.binary() , ) lowerCamelCase__ : Optional[int] = pa.array( [os.path.basename(__lowerCamelCase ) if path is not None else None for path in storage.field("path" ).to_pylist()] , type=pa.string() , ) lowerCamelCase__ : Tuple = pa.StructArray.from_arrays([bytes_array, path_array] , ["bytes", "path"] , mask=bytes_array.is_null() ) return array_cast(__lowerCamelCase , self.pa_type ) def lowercase_ ( ): """simple docstring""" if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError("To support encoding images, please install 'Pillow'." ) global _IMAGE_COMPRESSION_FORMATS if _IMAGE_COMPRESSION_FORMATS is None: PIL.Image.init() lowerCamelCase__ : List[str] = list(set(PIL.Image.OPEN.keys() ) & set(PIL.Image.SAVE.keys() ) ) return _IMAGE_COMPRESSION_FORMATS def lowercase_ ( _A : "PIL.Image.Image" ): """simple docstring""" lowerCamelCase__ : Optional[Any] = BytesIO() if image.format in list_image_compression_formats(): lowerCamelCase__ : int = image.format else: lowerCamelCase__ : int = "PNG" if image.mode in ["1", "L", "LA", "RGB", "RGBA"] else "TIFF" image.save(_A , format=_A ) return buffer.getvalue() def lowercase_ ( _A : "PIL.Image.Image" ): """simple docstring""" if hasattr(_A , "filename" ) and image.filename != "": return {"path": image.filename, "bytes": None} else: return {"path": None, "bytes": image_to_bytes(_A )} def lowercase_ ( _A : np.ndarray ): """simple docstring""" if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError("To support encoding images, please install 'Pillow'." ) lowerCamelCase__ : int = array.dtype lowerCamelCase__ : List[str] = dtype.byteorder if dtype.byteorder != "=" else _NATIVE_BYTEORDER lowerCamelCase__ : List[str] = dtype.kind lowerCamelCase__ : Optional[Any] = dtype.itemsize lowerCamelCase__ : Dict = None # Multi-channel array case (only np.dtype("|u1") is allowed) if array.shape[2:]: lowerCamelCase__ : List[Any] = np.dtype("|u1" ) if dtype_kind not in ["u", "i"]: raise TypeError( F"Unsupported array dtype {dtype} for image encoding. Only {dest_dtype} is supported for multi-channel arrays." ) if dtype is not dest_dtype: warnings.warn(F"Downcasting array dtype {dtype} to {dest_dtype} to be compatible with 'Pillow'" ) # Exact match elif dtype in _VALID_IMAGE_ARRAY_DTPYES: lowerCamelCase__ : Any = dtype else: # Downcast the type within the kind (np.can_cast(from_type, to_type, casting="same_kind") doesn't behave as expected, so do it manually) while dtype_itemsize >= 1: lowerCamelCase__ : Optional[Any] = dtype_byteorder + dtype_kind + str(_A ) lowerCamelCase__ : int = np.dtype(_A ) if dest_dtype in _VALID_IMAGE_ARRAY_DTPYES: warnings.warn(F"Downcasting array dtype {dtype} to {dest_dtype} to be compatible with 'Pillow'" ) break else: dtype_itemsize //= 2 if dest_dtype is None: raise TypeError( F"Cannot convert dtype {dtype} to a valid image dtype. Valid image dtypes: {_VALID_IMAGE_ARRAY_DTPYES}" ) lowerCamelCase__ : List[Any] = PIL.Image.fromarray(array.astype(_A ) ) return {"path": None, "bytes": image_to_bytes(_A )} def lowercase_ ( _A : Union[List[str], List[dict], List[np.ndarray], List["PIL.Image.Image"]] ): """simple docstring""" if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError("To support encoding images, please install 'Pillow'." ) if objs: lowerCamelCase__ , lowerCamelCase__ : int = first_non_null_value(_A ) if isinstance(_A , _A ): return [{"path": obj, "bytes": None} if obj is not None else None for obj in objs] if isinstance(_A , np.ndarray ): lowerCamelCase__ : Optional[Any] = no_op_if_value_is_null(_A ) return [obj_to_image_dict_func(_A ) for obj in objs] elif isinstance(_A , PIL.Image.Image ): lowerCamelCase__ : int = no_op_if_value_is_null(_A ) return [obj_to_image_dict_func(_A ) for obj in objs] else: return objs else: return objs
184
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_torch_available, is_vision_available, ) _A : int ={'''configuration_vit''': ['''VIT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''ViTConfig''', '''ViTOnnxConfig''']} try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _A : Tuple =['''ViTFeatureExtractor'''] _A : List[Any] =['''ViTImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _A : int =[ '''VIT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''ViTForImageClassification''', '''ViTForMaskedImageModeling''', '''ViTModel''', '''ViTPreTrainedModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _A : int =[ '''TFViTForImageClassification''', '''TFViTModel''', '''TFViTPreTrainedModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _A : Any =[ '''FlaxViTForImageClassification''', '''FlaxViTModel''', '''FlaxViTPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_vit import VIT_PRETRAINED_CONFIG_ARCHIVE_MAP, ViTConfig, ViTOnnxConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_vit import ViTFeatureExtractor from .image_processing_vit import ViTImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vit import ( VIT_PRETRAINED_MODEL_ARCHIVE_LIST, ViTForImageClassification, ViTForMaskedImageModeling, ViTModel, ViTPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_vit import TFViTForImageClassification, TFViTModel, TFViTPreTrainedModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_vit import FlaxViTForImageClassification, FlaxViTModel, FlaxViTPreTrainedModel else: import sys _A : Optional[Any] =_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
359
'''simple docstring''' from math import asin, atan, cos, radians, sin, sqrt, tan _A : List[str] =637_8137.0 _A : Dict =635_6752.31_4245 _A : int =6_378_137 def SCREAMING_SNAKE_CASE_ (UpperCamelCase , UpperCamelCase , UpperCamelCase , UpperCamelCase ) -> float: lowerCamelCase__ : str = (AXIS_A - AXIS_B) / AXIS_A lowerCamelCase__ : Dict = atan((1 - flattening) * tan(radians(UpperCamelCase ) ) ) lowerCamelCase__ : Dict = atan((1 - flattening) * tan(radians(UpperCamelCase ) ) ) lowerCamelCase__ : Optional[Any] = radians(UpperCamelCase ) lowerCamelCase__ : List[Any] = radians(UpperCamelCase ) # Equation lowerCamelCase__ : Tuple = sin((phi_a - phi_a) / 2 ) lowerCamelCase__ : List[Any] = sin((lambda_a - lambda_a) / 2 ) # Square both values sin_sq_phi *= sin_sq_phi sin_sq_lambda *= sin_sq_lambda lowerCamelCase__ : Tuple = sqrt(sin_sq_phi + (cos(UpperCamelCase ) * cos(UpperCamelCase ) * sin_sq_lambda) ) return 2 * RADIUS * asin(UpperCamelCase ) if __name__ == "__main__": import doctest doctest.testmod()
129
0
def a__ ( UpperCAmelCase : int ) -> list[int]: if length <= 0 or not isinstance(UpperCAmelCase , UpperCAmelCase ): raise ValueError('''Length must be a positive integer.''' ) return [n * (2 * n - 1) for n in range(UpperCAmelCase )] if __name__ == "__main__": print(hexagonal_numbers(length=5)) print(hexagonal_numbers(length=1_0))
336
from __future__ import annotations def a__ ( UpperCAmelCase : list[list[int]] ) -> bool: UpperCAmelCase : Union[str, Any] = len(UpperCAmelCase ) # We need to create solution object to save path. UpperCAmelCase : int = [[0 for _ in range(UpperCAmelCase )] for _ in range(UpperCAmelCase )] UpperCAmelCase : Union[str, Any] = run_maze(UpperCAmelCase , 0 , 0 , UpperCAmelCase ) if solved: print('''\n'''.join(str(UpperCAmelCase ) for row in solutions ) ) else: print('''No solution exists!''' ) return solved def a__ ( UpperCAmelCase : list[list[int]] , UpperCAmelCase : int , UpperCAmelCase : int , UpperCAmelCase : list[list[int]] ) -> bool: UpperCAmelCase : Dict = len(UpperCAmelCase ) # Final check point. if i == j == (size - 1): UpperCAmelCase : Dict = 1 return True UpperCAmelCase : Union[str, Any] = (not i < 0) and (not j < 0) # Check lower bounds UpperCAmelCase : List[Any] = (i < size) and (j < size) # Check upper bounds if lower_flag and upper_flag: # check for already visited and block points. UpperCAmelCase : Any = (not solutions[i][j]) and (not maze[i][j]) if block_flag: # check visited UpperCAmelCase : str = 1 # check for directions if ( run_maze(UpperCAmelCase , i + 1 , UpperCAmelCase , UpperCAmelCase ) or run_maze(UpperCAmelCase , UpperCAmelCase , j + 1 , UpperCAmelCase ) or run_maze(UpperCAmelCase , i - 1 , UpperCAmelCase , UpperCAmelCase ) or run_maze(UpperCAmelCase , UpperCAmelCase , j - 1 , UpperCAmelCase ) ): return True UpperCAmelCase : Any = 0 return False return False if __name__ == "__main__": import doctest doctest.testmod()
336
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) UpperCamelCase : Any = { """configuration_roformer""": ["""ROFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP""", """RoFormerConfig""", """RoFormerOnnxConfig"""], """tokenization_roformer""": ["""RoFormerTokenizer"""], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase : Union[str, Any] = ["""RoFormerTokenizerFast"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase : Any = [ """ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST""", """RoFormerForCausalLM""", """RoFormerForMaskedLM""", """RoFormerForMultipleChoice""", """RoFormerForQuestionAnswering""", """RoFormerForSequenceClassification""", """RoFormerForTokenClassification""", """RoFormerLayer""", """RoFormerModel""", """RoFormerPreTrainedModel""", """load_tf_weights_in_roformer""", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase : int = [ """TF_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST""", """TFRoFormerForCausalLM""", """TFRoFormerForMaskedLM""", """TFRoFormerForMultipleChoice""", """TFRoFormerForQuestionAnswering""", """TFRoFormerForSequenceClassification""", """TFRoFormerForTokenClassification""", """TFRoFormerLayer""", """TFRoFormerModel""", """TFRoFormerPreTrainedModel""", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase : Any = [ """FLAX_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST""", """FlaxRoFormerForMaskedLM""", """FlaxRoFormerForMultipleChoice""", """FlaxRoFormerForQuestionAnswering""", """FlaxRoFormerForSequenceClassification""", """FlaxRoFormerForTokenClassification""", """FlaxRoFormerModel""", """FlaxRoFormerPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_roformer import ROFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, RoFormerConfig, RoFormerOnnxConfig from .tokenization_roformer import RoFormerTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_roformer_fast import RoFormerTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_roformer import ( ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, RoFormerForCausalLM, RoFormerForMaskedLM, RoFormerForMultipleChoice, RoFormerForQuestionAnswering, RoFormerForSequenceClassification, RoFormerForTokenClassification, RoFormerLayer, RoFormerModel, RoFormerPreTrainedModel, load_tf_weights_in_roformer, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_roformer import ( TF_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TFRoFormerForCausalLM, TFRoFormerForMaskedLM, TFRoFormerForMultipleChoice, TFRoFormerForQuestionAnswering, TFRoFormerForSequenceClassification, TFRoFormerForTokenClassification, TFRoFormerLayer, TFRoFormerModel, TFRoFormerPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_roformer import ( FLAX_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, FlaxRoFormerForMaskedLM, FlaxRoFormerForMultipleChoice, FlaxRoFormerForQuestionAnswering, FlaxRoFormerForSequenceClassification, FlaxRoFormerForTokenClassification, FlaxRoFormerModel, FlaxRoFormerPreTrainedModel, ) else: import sys UpperCamelCase : Any = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
345
'''simple docstring''' import logging from transformers.configuration_utils import PretrainedConfig UpperCamelCase : Optional[Any] = logging.getLogger(__name__) class UpperCamelCase ( a_ ): """simple docstring""" A : Tuple = "masked_bert" def __init__( self : Tuple , UpperCAmelCase_ : List[Any]=3_0_5_2_2 , UpperCAmelCase_ : str=7_6_8 , UpperCAmelCase_ : Optional[Any]=1_2 , UpperCAmelCase_ : Optional[int]=1_2 , UpperCAmelCase_ : Union[str, Any]=3_0_7_2 , UpperCAmelCase_ : Union[str, Any]="gelu" , UpperCAmelCase_ : Optional[Any]=0.1 , UpperCAmelCase_ : List[Any]=0.1 , UpperCAmelCase_ : Optional[int]=5_1_2 , UpperCAmelCase_ : List[str]=2 , UpperCAmelCase_ : str=0.02 , UpperCAmelCase_ : Optional[Any]=1e-12 , UpperCAmelCase_ : Dict=0 , UpperCAmelCase_ : Dict="topK" , UpperCAmelCase_ : str="constant" , UpperCAmelCase_ : Optional[Any]=0.0 , **UpperCAmelCase_ : Optional[Any] , ): """simple docstring""" super().__init__(pad_token_id=UpperCAmelCase_ , **UpperCAmelCase_) a : Union[str, Any] = vocab_size a : List[Any] = hidden_size a : List[str] = num_hidden_layers a : Any = num_attention_heads a : Optional[Any] = hidden_act a : str = intermediate_size a : Dict = hidden_dropout_prob a : Any = attention_probs_dropout_prob a : Any = max_position_embeddings a : Dict = type_vocab_size a : List[str] = initializer_range a : int = layer_norm_eps a : Dict = pruning_method a : List[str] = mask_init a : Union[str, Any] = mask_scale
345
1
"""simple docstring""" import requests A_ = '''YOUR API KEY''' def UpperCAmelCase__ (snake_case__ : str , snake_case__ : str = giphy_api_key ): """simple docstring""" _snake_case : List[Any] = """+""".join(query.split() ) _snake_case : Optional[int] = F"https://api.giphy.com/v1/gifs/search?q={formatted_query}&api_key={api_key}" _snake_case : int = requests.get(snake_case__ ).json()["""data"""] return [gif["url"] for gif in gifs] if __name__ == "__main__": print('''\n'''.join(get_gifs('''space ship''')))
64
from typing import Dict, List, Optional, Tuple, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, flip_channel_order, get_resize_output_image_size, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_torch_available, is_torch_tensor, is_vision_available, logging if is_vision_available(): import PIL if is_torch_available(): import torch lowerCamelCase : Any =logging.get_logger(__name__) class __a ( A__ ): _lowerCAmelCase : List[str] = ['''pixel_values'''] def __init__( self : List[str] , SCREAMING_SNAKE_CASE : bool = True , SCREAMING_SNAKE_CASE : Dict[str, int] = None , SCREAMING_SNAKE_CASE : PILImageResampling = PILImageResampling.BILINEAR , SCREAMING_SNAKE_CASE : bool = True , SCREAMING_SNAKE_CASE : Union[int, float] = 1 / 2_55 , SCREAMING_SNAKE_CASE : bool = True , SCREAMING_SNAKE_CASE : Dict[str, int] = None , SCREAMING_SNAKE_CASE : bool = True , **SCREAMING_SNAKE_CASE : Tuple , ): '''simple docstring''' super().__init__(**SCREAMING_SNAKE_CASE ) UpperCamelCase__ : List[str] = size if size is not None else {"shortest_edge": 2_24} UpperCamelCase__ : Any = get_size_dict(SCREAMING_SNAKE_CASE , default_to_square=SCREAMING_SNAKE_CASE ) UpperCamelCase__ : List[Any] = crop_size if crop_size is not None else {"height": 2_56, "width": 2_56} UpperCamelCase__ : int = get_size_dict(SCREAMING_SNAKE_CASE , param_name="crop_size" ) UpperCamelCase__ : Dict = do_resize UpperCamelCase__ : List[str] = size UpperCamelCase__ : int = resample UpperCamelCase__ : Optional[int] = do_rescale UpperCamelCase__ : List[Any] = rescale_factor UpperCamelCase__ : Union[str, Any] = do_center_crop UpperCamelCase__ : int = crop_size UpperCamelCase__ : Optional[int] = do_flip_channel_order def __lowercase ( self : Optional[Any] , SCREAMING_SNAKE_CASE : np.ndarray , SCREAMING_SNAKE_CASE : Dict[str, int] , SCREAMING_SNAKE_CASE : PILImageResampling = PIL.Image.BILINEAR , SCREAMING_SNAKE_CASE : Optional[Union[str, ChannelDimension]] = None , **SCREAMING_SNAKE_CASE : Optional[Any] , ): '''simple docstring''' UpperCamelCase__ : Union[str, Any] = get_size_dict(SCREAMING_SNAKE_CASE , default_to_square=SCREAMING_SNAKE_CASE ) if "shortest_edge" not in size: raise ValueError(F'The `size` dictionary must contain the key `shortest_edge`. Got {size.keys()}' ) UpperCamelCase__ : Any = get_resize_output_image_size(SCREAMING_SNAKE_CASE , size=size["shortest_edge"] , default_to_square=SCREAMING_SNAKE_CASE ) return resize(SCREAMING_SNAKE_CASE , size=SCREAMING_SNAKE_CASE , resample=SCREAMING_SNAKE_CASE , data_format=SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) def __lowercase ( self : Optional[int] , SCREAMING_SNAKE_CASE : np.ndarray , SCREAMING_SNAKE_CASE : Dict[str, int] , SCREAMING_SNAKE_CASE : Optional[Union[str, ChannelDimension]] = None , **SCREAMING_SNAKE_CASE : Optional[Any] , ): '''simple docstring''' UpperCamelCase__ : List[Any] = get_size_dict(SCREAMING_SNAKE_CASE ) if "height" not in size or "width" not in size: raise ValueError(F'The `size` dictionary must contain the keys `height` and `width`. Got {size.keys()}' ) return center_crop(SCREAMING_SNAKE_CASE , size=(size["height"], size["width"]) , data_format=SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) def __lowercase ( self : int , SCREAMING_SNAKE_CASE : np.ndarray , SCREAMING_SNAKE_CASE : Union[int, float] , SCREAMING_SNAKE_CASE : Optional[Union[str, ChannelDimension]] = None , **SCREAMING_SNAKE_CASE : Union[str, Any] , ): '''simple docstring''' return rescale(SCREAMING_SNAKE_CASE , scale=SCREAMING_SNAKE_CASE , data_format=SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) def __lowercase ( self : Optional[int] , SCREAMING_SNAKE_CASE : np.ndarray , SCREAMING_SNAKE_CASE : Optional[Union[str, ChannelDimension]] = None ): '''simple docstring''' return flip_channel_order(SCREAMING_SNAKE_CASE , data_format=SCREAMING_SNAKE_CASE ) def __lowercase ( self : Optional[Any] , SCREAMING_SNAKE_CASE : ImageInput , SCREAMING_SNAKE_CASE : bool = None , SCREAMING_SNAKE_CASE : Dict[str, int] = None , SCREAMING_SNAKE_CASE : PILImageResampling = None , SCREAMING_SNAKE_CASE : bool = None , SCREAMING_SNAKE_CASE : float = None , SCREAMING_SNAKE_CASE : bool = None , SCREAMING_SNAKE_CASE : Dict[str, int] = None , SCREAMING_SNAKE_CASE : bool = None , SCREAMING_SNAKE_CASE : Optional[Union[str, TensorType]] = None , SCREAMING_SNAKE_CASE : ChannelDimension = ChannelDimension.FIRST , **SCREAMING_SNAKE_CASE : Dict , ): '''simple docstring''' UpperCamelCase__ : List[str] = do_resize if do_resize is not None else self.do_resize UpperCamelCase__ : List[str] = resample if resample is not None else self.resample UpperCamelCase__ : Any = do_rescale if do_rescale is not None else self.do_rescale UpperCamelCase__ : Tuple = rescale_factor if rescale_factor is not None else self.rescale_factor UpperCamelCase__ : Dict = do_center_crop if do_center_crop is not None else self.do_center_crop UpperCamelCase__ : Any = ( do_flip_channel_order if do_flip_channel_order is not None else self.do_flip_channel_order ) UpperCamelCase__ : Optional[int] = size if size is not None else self.size UpperCamelCase__ : List[str] = get_size_dict(SCREAMING_SNAKE_CASE , default_to_square=SCREAMING_SNAKE_CASE ) UpperCamelCase__ : Any = crop_size if crop_size is not None else self.crop_size UpperCamelCase__ : Union[str, Any] = get_size_dict(SCREAMING_SNAKE_CASE , param_name="crop_size" ) UpperCamelCase__ : int = make_list_of_images(SCREAMING_SNAKE_CASE ) if not valid_images(SCREAMING_SNAKE_CASE ): raise ValueError( "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, " "torch.Tensor, tf.Tensor or jax.ndarray." ) if do_resize and size is None: raise ValueError("Size 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_center_crop and crop_size is None: raise ValueError("Crop size must be specified if do_center_crop is True." ) # All transformations expect numpy arrays. UpperCamelCase__ : Tuple = [to_numpy_array(SCREAMING_SNAKE_CASE ) for image in images] if do_resize: UpperCamelCase__ : Optional[int] = [self.resize(image=SCREAMING_SNAKE_CASE , size=SCREAMING_SNAKE_CASE , resample=SCREAMING_SNAKE_CASE ) for image in images] if do_center_crop: UpperCamelCase__ : Any = [self.center_crop(image=SCREAMING_SNAKE_CASE , size=SCREAMING_SNAKE_CASE ) for image in images] if do_rescale: UpperCamelCase__ : str = [self.rescale(image=SCREAMING_SNAKE_CASE , scale=SCREAMING_SNAKE_CASE ) for image in images] # the pretrained checkpoints assume images are BGR, not RGB if do_flip_channel_order: UpperCamelCase__ : Any = [self.flip_channel_order(image=SCREAMING_SNAKE_CASE ) for image in images] UpperCamelCase__ : Optional[int] = [to_channel_dimension_format(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) for image in images] UpperCamelCase__ : Optional[int] = {"pixel_values": images} return BatchFeature(data=SCREAMING_SNAKE_CASE , tensor_type=SCREAMING_SNAKE_CASE ) def __lowercase ( self : Dict , SCREAMING_SNAKE_CASE : List[Any] , SCREAMING_SNAKE_CASE : List[Tuple] = None ): '''simple docstring''' UpperCamelCase__ : Optional[int] = outputs.logits # Resize logits and compute semantic segmentation maps if target_sizes is not None: if len(SCREAMING_SNAKE_CASE ) != len(SCREAMING_SNAKE_CASE ): raise ValueError( "Make sure that you pass in as many target sizes as the batch dimension of the logits" ) if is_torch_tensor(SCREAMING_SNAKE_CASE ): UpperCamelCase__ : Optional[Any] = target_sizes.numpy() UpperCamelCase__ : Any = [] for idx in range(len(SCREAMING_SNAKE_CASE ) ): UpperCamelCase__ : Optional[Any] = torch.nn.functional.interpolate( logits[idx].unsqueeze(dim=0 ) , size=target_sizes[idx] , mode="bilinear" , align_corners=SCREAMING_SNAKE_CASE ) UpperCamelCase__ : List[str] = resized_logits[0].argmax(dim=0 ) semantic_segmentation.append(SCREAMING_SNAKE_CASE ) else: UpperCamelCase__ : List[str] = logits.argmax(dim=1 ) UpperCamelCase__ : Dict = [semantic_segmentation[i] for i in range(semantic_segmentation.shape[0] )] return semantic_segmentation
189
0
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 _a ( _lowerCAmelCase , unittest.TestCase ): A = '''hf-internal-testing/tiny-random-OnnxStableDiffusionPipeline''' def __snake_case (self, SCREAMING_SNAKE_CASE_=0 ) -> List[Any]: UpperCAmelCase_: List[Any] = floats_tensor((1, 3, 128, 128), rng=random.Random(SCREAMING_SNAKE_CASE_ ) ) UpperCAmelCase_: List[str] = np.random.RandomState(SCREAMING_SNAKE_CASE_ ) UpperCAmelCase_: Optional[Any] = { """prompt""": """A painting of a squirrel eating a burger""", """image""": image, """generator""": generator, """num_inference_steps""": 3, """strength""": 0.7_5, """guidance_scale""": 7.5, """output_type""": """numpy""", } return inputs def __snake_case (self ) -> List[Any]: UpperCAmelCase_: List[str] = OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint, provider="""CPUExecutionProvider""" ) pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_ ) UpperCAmelCase_: Dict = self.get_dummy_inputs() UpperCAmelCase_: Dict = pipe(**SCREAMING_SNAKE_CASE_ ).images UpperCAmelCase_: Union[str, Any] = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 128, 128, 3) UpperCAmelCase_: Tuple = np.array([0.6_9_6_4_3, 0.5_8_4_8_4, 0.5_0_3_1_4, 0.5_8_7_6_0, 0.5_5_3_6_8, 0.5_9_6_4_3, 0.5_1_5_2_9, 0.4_1_2_1_7, 0.4_9_0_8_7] ) assert np.abs(image_slice - expected_slice ).max() < 1E-1 def __snake_case (self ) -> Any: UpperCAmelCase_: Optional[int] = OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint, provider="""CPUExecutionProvider""" ) UpperCAmelCase_: str = PNDMScheduler.from_config(pipe.scheduler.config, skip_prk_steps=SCREAMING_SNAKE_CASE_ ) pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_ ) UpperCAmelCase_: int = self.get_dummy_inputs() UpperCAmelCase_: Dict = pipe(**SCREAMING_SNAKE_CASE_ ).images UpperCAmelCase_: int = image[0, -3:, -3:, -1] assert image.shape == (1, 128, 128, 3) UpperCAmelCase_: List[Any] = np.array([0.6_1_7_3_7, 0.5_4_6_4_2, 0.5_3_1_8_3, 0.5_4_4_6_5, 0.5_2_7_4_2, 0.6_0_5_2_5, 0.4_9_9_6_9, 0.4_0_6_5_5, 0.4_8_1_5_4] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1 def __snake_case (self ) -> Optional[int]: UpperCAmelCase_: int = OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint, provider="""CPUExecutionProvider""" ) UpperCAmelCase_: Dict = LMSDiscreteScheduler.from_config(pipe.scheduler.config ) pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_ ) # warmup pass to apply optimizations UpperCAmelCase_: Tuple = pipe(**self.get_dummy_inputs() ) UpperCAmelCase_: Tuple = self.get_dummy_inputs() UpperCAmelCase_: Optional[int] = pipe(**SCREAMING_SNAKE_CASE_ ).images UpperCAmelCase_: int = image[0, -3:, -3:, -1] assert image.shape == (1, 128, 128, 3) UpperCAmelCase_: List[Any] = np.array([0.5_2_7_6_1, 0.5_9_9_7_7, 0.4_9_0_3_3, 0.4_9_6_1_9, 0.5_4_2_8_2, 0.5_0_3_1_1, 0.4_7_6_0_0, 0.4_0_9_1_8, 0.4_5_2_0_3] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1 def __snake_case (self ) -> Dict: UpperCAmelCase_: Optional[int] = OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint, provider="""CPUExecutionProvider""" ) UpperCAmelCase_: str = EulerDiscreteScheduler.from_config(pipe.scheduler.config ) pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_ ) UpperCAmelCase_: Any = self.get_dummy_inputs() UpperCAmelCase_: str = pipe(**SCREAMING_SNAKE_CASE_ ).images UpperCAmelCase_: str = image[0, -3:, -3:, -1] assert image.shape == (1, 128, 128, 3) UpperCAmelCase_: List[Any] = np.array([0.5_2_9_1_1, 0.6_0_0_0_4, 0.4_9_2_2_9, 0.4_9_8_0_5, 0.5_4_5_0_2, 0.5_0_6_8_0, 0.4_7_7_7_7, 0.4_1_0_2_8, 0.4_5_3_0_4] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1 def __snake_case (self ) -> Any: UpperCAmelCase_: Optional[Any] = OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint, provider="""CPUExecutionProvider""" ) UpperCAmelCase_: int = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config ) pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_ ) UpperCAmelCase_: int = self.get_dummy_inputs() UpperCAmelCase_: Optional[Any] = pipe(**SCREAMING_SNAKE_CASE_ ).images UpperCAmelCase_: Tuple = image[0, -3:, -3:, -1] assert image.shape == (1, 128, 128, 3) UpperCAmelCase_: int = np.array([0.5_2_9_1_1, 0.6_0_0_0_4, 0.4_9_2_2_9, 0.4_9_8_0_5, 0.5_4_5_0_2, 0.5_0_6_8_0, 0.4_7_7_7_7, 0.4_1_0_2_8, 0.4_5_3_0_4] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1 def __snake_case (self ) -> List[str]: UpperCAmelCase_: Dict = OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint, provider="""CPUExecutionProvider""" ) UpperCAmelCase_: List[str] = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config ) pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_ ) UpperCAmelCase_: Union[str, Any] = self.get_dummy_inputs() UpperCAmelCase_: Optional[int] = pipe(**SCREAMING_SNAKE_CASE_ ).images UpperCAmelCase_: Dict = image[0, -3:, -3:, -1] assert image.shape == (1, 128, 128, 3) UpperCAmelCase_: Optional[Any] = np.array([0.6_5_3_3_1, 0.5_8_2_7_7, 0.4_8_2_0_4, 0.5_6_0_5_9, 0.5_3_6_6_5, 0.5_6_2_3_5, 0.5_0_9_6_9, 0.4_0_0_0_9, 0.4_6_5_5_2] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1 @nightly @require_onnxruntime @require_torch_gpu class _a ( unittest.TestCase ): @property def __snake_case (self ) -> Tuple: return ( "CUDAExecutionProvider", { "gpu_mem_limit": "15000000000", # 15GB "arena_extend_strategy": "kSameAsRequested", }, ) @property def __snake_case (self ) -> Dict: UpperCAmelCase_: List[str] = ort.SessionOptions() UpperCAmelCase_: Dict = False return options def __snake_case (self ) -> List[str]: UpperCAmelCase_: int = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/img2img/sketch-mountains-input.jpg""" ) UpperCAmelCase_: Tuple = init_image.resize((768, 512) ) # using the PNDM scheduler by default UpperCAmelCase_: List[str] = OnnxStableDiffusionImgaImgPipeline.from_pretrained( """CompVis/stable-diffusion-v1-4""", revision="""onnx""", safety_checker=SCREAMING_SNAKE_CASE_, feature_extractor=SCREAMING_SNAKE_CASE_, provider=self.gpu_provider, sess_options=self.gpu_options, ) pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_ ) UpperCAmelCase_: str = """A fantasy landscape, trending on artstation""" UpperCAmelCase_: Any = np.random.RandomState(0 ) UpperCAmelCase_: List[Any] = pipe( prompt=SCREAMING_SNAKE_CASE_, image=SCREAMING_SNAKE_CASE_, strength=0.7_5, guidance_scale=7.5, num_inference_steps=10, generator=SCREAMING_SNAKE_CASE_, output_type="""np""", ) UpperCAmelCase_: int = output.images UpperCAmelCase_: Optional[int] = images[0, 255:258, 383:386, -1] assert images.shape == (1, 512, 768, 3) UpperCAmelCase_: Optional[int] = np.array([0.4_9_0_9, 0.5_0_5_9, 0.5_3_7_2, 0.4_6_2_3, 0.4_8_7_6, 0.5_0_4_9, 0.4_8_2_0, 0.4_9_5_6, 0.5_0_1_9] ) # TODO: lower the tolerance after finding the cause of onnxruntime reproducibility issues assert np.abs(image_slice.flatten() - expected_slice ).max() < 2E-2 def __snake_case (self ) -> str: UpperCAmelCase_: str = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/img2img/sketch-mountains-input.jpg""" ) UpperCAmelCase_: Dict = init_image.resize((768, 512) ) UpperCAmelCase_: Optional[Any] = LMSDiscreteScheduler.from_pretrained( """runwayml/stable-diffusion-v1-5""", subfolder="""scheduler""", revision="""onnx""" ) UpperCAmelCase_: List[Any] = OnnxStableDiffusionImgaImgPipeline.from_pretrained( """runwayml/stable-diffusion-v1-5""", revision="""onnx""", scheduler=SCREAMING_SNAKE_CASE_, safety_checker=SCREAMING_SNAKE_CASE_, feature_extractor=SCREAMING_SNAKE_CASE_, provider=self.gpu_provider, sess_options=self.gpu_options, ) pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_ ) UpperCAmelCase_: Dict = """A fantasy landscape, trending on artstation""" UpperCAmelCase_: Optional[Any] = np.random.RandomState(0 ) UpperCAmelCase_: Optional[Any] = pipe( prompt=SCREAMING_SNAKE_CASE_, image=SCREAMING_SNAKE_CASE_, strength=0.7_5, guidance_scale=7.5, num_inference_steps=20, generator=SCREAMING_SNAKE_CASE_, output_type="""np""", ) UpperCAmelCase_: int = output.images UpperCAmelCase_: List[str] = images[0, 255:258, 383:386, -1] assert images.shape == (1, 512, 768, 3) UpperCAmelCase_: Optional[int] = np.array([0.8_0_4_3, 0.9_2_6, 0.9_5_8_1, 0.8_1_1_9, 0.8_9_5_4, 0.9_1_3, 0.7_2_0_9, 0.7_4_6_3, 0.7_4_3_1] ) # TODO: lower the tolerance after finding the cause of onnxruntime reproducibility issues assert np.abs(image_slice.flatten() - expected_slice ).max() < 2E-2
82
import unittest from transformers import ( MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, Pipeline, ZeroShotClassificationPipeline, pipeline, ) from transformers.testing_utils import is_pipeline_test, nested_simplify, require_tf, require_torch, slow from .test_pipelines_common import ANY # These 2 model types require different inputs than those of the usual text models. a : Tuple = {'LayoutLMv2Config', 'LayoutLMv3Config'} @is_pipeline_test class _a ( unittest.TestCase ): A = MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING A = TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING if model_mapping is not None: A = {config: model for config, model in model_mapping.items() if config.__name__ not in _TO_SKIP} if tf_model_mapping is not None: A = { config: model for config, model in tf_model_mapping.items() if config.__name__ not in _TO_SKIP } def __snake_case (self, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ ) -> Dict: UpperCAmelCase_: Dict = ZeroShotClassificationPipeline( model=SCREAMING_SNAKE_CASE_, tokenizer=SCREAMING_SNAKE_CASE_, candidate_labels=["""polics""", """health"""] ) return classifier, ["Who are you voting for in 2020?", "My stomach hurts."] def __snake_case (self, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ ) -> Dict: UpperCAmelCase_: Dict = classifier("""Who are you voting for in 2020?""", candidate_labels="""politics""" ) self.assertEqual(SCREAMING_SNAKE_CASE_, {"""sequence""": ANY(SCREAMING_SNAKE_CASE_ ), """labels""": [ANY(SCREAMING_SNAKE_CASE_ )], """scores""": [ANY(SCREAMING_SNAKE_CASE_ )]} ) # No kwarg UpperCAmelCase_: Optional[int] = classifier("""Who are you voting for in 2020?""", ["""politics"""] ) self.assertEqual(SCREAMING_SNAKE_CASE_, {"""sequence""": ANY(SCREAMING_SNAKE_CASE_ ), """labels""": [ANY(SCREAMING_SNAKE_CASE_ )], """scores""": [ANY(SCREAMING_SNAKE_CASE_ )]} ) UpperCAmelCase_: Optional[int] = classifier("""Who are you voting for in 2020?""", candidate_labels=["""politics"""] ) self.assertEqual(SCREAMING_SNAKE_CASE_, {"""sequence""": ANY(SCREAMING_SNAKE_CASE_ ), """labels""": [ANY(SCREAMING_SNAKE_CASE_ )], """scores""": [ANY(SCREAMING_SNAKE_CASE_ )]} ) UpperCAmelCase_: List[Any] = classifier("""Who are you voting for in 2020?""", candidate_labels="""politics, public health""" ) self.assertEqual( SCREAMING_SNAKE_CASE_, {"""sequence""": ANY(SCREAMING_SNAKE_CASE_ ), """labels""": [ANY(SCREAMING_SNAKE_CASE_ ), ANY(SCREAMING_SNAKE_CASE_ )], """scores""": [ANY(SCREAMING_SNAKE_CASE_ ), ANY(SCREAMING_SNAKE_CASE_ )]} ) self.assertAlmostEqual(sum(nested_simplify(outputs["""scores"""] ) ), 1.0 ) UpperCAmelCase_: Tuple = classifier("""Who are you voting for in 2020?""", candidate_labels=["""politics""", """public health"""] ) self.assertEqual( SCREAMING_SNAKE_CASE_, {"""sequence""": ANY(SCREAMING_SNAKE_CASE_ ), """labels""": [ANY(SCREAMING_SNAKE_CASE_ ), ANY(SCREAMING_SNAKE_CASE_ )], """scores""": [ANY(SCREAMING_SNAKE_CASE_ ), ANY(SCREAMING_SNAKE_CASE_ )]} ) self.assertAlmostEqual(sum(nested_simplify(outputs["""scores"""] ) ), 1.0 ) UpperCAmelCase_: str = classifier( """Who are you voting for in 2020?""", candidate_labels="""politics""", hypothesis_template="""This text is about {}""" ) self.assertEqual(SCREAMING_SNAKE_CASE_, {"""sequence""": ANY(SCREAMING_SNAKE_CASE_ ), """labels""": [ANY(SCREAMING_SNAKE_CASE_ )], """scores""": [ANY(SCREAMING_SNAKE_CASE_ )]} ) # https://github.com/huggingface/transformers/issues/13846 UpperCAmelCase_: Union[str, Any] = classifier(["""I am happy"""], ["""positive""", """negative"""] ) self.assertEqual( SCREAMING_SNAKE_CASE_, [ {"""sequence""": ANY(SCREAMING_SNAKE_CASE_ ), """labels""": [ANY(SCREAMING_SNAKE_CASE_ ), ANY(SCREAMING_SNAKE_CASE_ )], """scores""": [ANY(SCREAMING_SNAKE_CASE_ ), ANY(SCREAMING_SNAKE_CASE_ )]} for i in range(1 ) ], ) UpperCAmelCase_: Dict = classifier(["""I am happy""", """I am sad"""], ["""positive""", """negative"""] ) self.assertEqual( SCREAMING_SNAKE_CASE_, [ {"""sequence""": ANY(SCREAMING_SNAKE_CASE_ ), """labels""": [ANY(SCREAMING_SNAKE_CASE_ ), ANY(SCREAMING_SNAKE_CASE_ )], """scores""": [ANY(SCREAMING_SNAKE_CASE_ ), ANY(SCREAMING_SNAKE_CASE_ )]} for i in range(2 ) ], ) with self.assertRaises(SCREAMING_SNAKE_CASE_ ): classifier("""""", candidate_labels="""politics""" ) with self.assertRaises(SCREAMING_SNAKE_CASE_ ): classifier(SCREAMING_SNAKE_CASE_, candidate_labels="""politics""" ) with self.assertRaises(SCREAMING_SNAKE_CASE_ ): classifier("""Who are you voting for in 2020?""", candidate_labels="""""" ) with self.assertRaises(SCREAMING_SNAKE_CASE_ ): classifier("""Who are you voting for in 2020?""", candidate_labels=SCREAMING_SNAKE_CASE_ ) with self.assertRaises(SCREAMING_SNAKE_CASE_ ): classifier( """Who are you voting for in 2020?""", candidate_labels="""politics""", hypothesis_template="""Not formatting template""", ) with self.assertRaises(SCREAMING_SNAKE_CASE_ ): classifier( """Who are you voting for in 2020?""", candidate_labels="""politics""", hypothesis_template=SCREAMING_SNAKE_CASE_, ) self.run_entailment_id(SCREAMING_SNAKE_CASE_ ) def __snake_case (self, SCREAMING_SNAKE_CASE_ ) -> Union[str, Any]: UpperCAmelCase_: int = zero_shot_classifier.model.config UpperCAmelCase_: Optional[int] = config.labelaid UpperCAmelCase_: str = zero_shot_classifier.entailment_id UpperCAmelCase_: Union[str, Any] = {"""LABEL_0""": 0, """LABEL_1""": 1, """LABEL_2""": 2} self.assertEqual(zero_shot_classifier.entailment_id, -1 ) UpperCAmelCase_: int = {"""entailment""": 0, """neutral""": 1, """contradiction""": 2} self.assertEqual(zero_shot_classifier.entailment_id, 0 ) UpperCAmelCase_: Dict = {"""ENTAIL""": 0, """NON-ENTAIL""": 1} self.assertEqual(zero_shot_classifier.entailment_id, 0 ) UpperCAmelCase_: Tuple = {"""ENTAIL""": 2, """NEUTRAL""": 1, """CONTR""": 0} self.assertEqual(zero_shot_classifier.entailment_id, 2 ) UpperCAmelCase_: Any = original_labelaid self.assertEqual(SCREAMING_SNAKE_CASE_, zero_shot_classifier.entailment_id ) @require_torch def __snake_case (self ) -> str: UpperCAmelCase_: Any = pipeline( """zero-shot-classification""", model="""sshleifer/tiny-distilbert-base-cased-distilled-squad""", framework="""pt""", ) # There was a regression in 4.10 for this # Adding a test so we don't make the mistake again. # https://github.com/huggingface/transformers/issues/13381#issuecomment-912343499 zero_shot_classifier( """Who are you voting for in 2020?""" * 100, candidate_labels=["""politics""", """public health""", """science"""] ) @require_torch def __snake_case (self ) -> Union[str, Any]: UpperCAmelCase_: str = pipeline( """zero-shot-classification""", model="""sshleifer/tiny-distilbert-base-cased-distilled-squad""", framework="""pt""", ) UpperCAmelCase_: Tuple = zero_shot_classifier( """Who are you voting for in 2020?""", candidate_labels=["""politics""", """public health""", """science"""] ) self.assertEqual( nested_simplify(SCREAMING_SNAKE_CASE_ ), { """sequence""": """Who are you voting for in 2020?""", """labels""": ["""science""", """public health""", """politics"""], """scores""": [0.3_3_3, 0.3_3_3, 0.3_3_3], }, ) @require_tf def __snake_case (self ) -> int: UpperCAmelCase_: List[Any] = pipeline( """zero-shot-classification""", model="""sshleifer/tiny-distilbert-base-cased-distilled-squad""", framework="""tf""", ) UpperCAmelCase_: Optional[Any] = zero_shot_classifier( """Who are you voting for in 2020?""", candidate_labels=["""politics""", """public health""", """science"""] ) self.assertEqual( nested_simplify(SCREAMING_SNAKE_CASE_ ), { """sequence""": """Who are you voting for in 2020?""", """labels""": ["""science""", """public health""", """politics"""], """scores""": [0.3_3_3, 0.3_3_3, 0.3_3_3], }, ) @slow @require_torch def __snake_case (self ) -> Optional[int]: UpperCAmelCase_: List[Any] = pipeline("""zero-shot-classification""", model="""roberta-large-mnli""", framework="""pt""" ) UpperCAmelCase_: Optional[int] = zero_shot_classifier( """Who are you voting for in 2020?""", candidate_labels=["""politics""", """public health""", """science"""] ) self.assertEqual( nested_simplify(SCREAMING_SNAKE_CASE_ ), { """sequence""": """Who are you voting for in 2020?""", """labels""": ["""politics""", """public health""", """science"""], """scores""": [0.9_7_6, 0.0_1_5, 0.0_0_9], }, ) UpperCAmelCase_: Optional[Any] = zero_shot_classifier( """The dominant sequence transduction models are based on complex recurrent or convolutional neural networks""" """ in an encoder-decoder configuration. The best performing models also connect the encoder and decoder""" """ through an attention mechanism. We propose a new simple network architecture, the Transformer, based""" """ solely on attention mechanisms, dispensing with recurrence and convolutions entirely. Experiments on two""" """ machine translation tasks show these models to be superior in quality while being more parallelizable""" """ and requiring significantly less time to train. Our model achieves 28.4 BLEU on the WMT 2014""" """ English-to-German translation task, improving over the existing best results, including ensembles by""" """ over 2 BLEU. On the WMT 2014 English-to-French translation task, our model establishes a new""" """ single-model state-of-the-art BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small""" """ fraction of the training costs of the best models from the literature. We show that the Transformer""" """ generalizes well to other tasks by applying it successfully to English constituency parsing both with""" """ large and limited training data.""", candidate_labels=["""machine learning""", """statistics""", """translation""", """vision"""], multi_label=SCREAMING_SNAKE_CASE_, ) self.assertEqual( nested_simplify(SCREAMING_SNAKE_CASE_ ), { """sequence""": ( """The dominant sequence transduction models are based on complex recurrent or convolutional neural""" """ networks in an encoder-decoder configuration. The best performing models also connect the""" """ encoder and decoder through an attention mechanism. We propose a new simple network""" """ architecture, the Transformer, based solely on attention mechanisms, dispensing with recurrence""" """ and convolutions entirely. Experiments on two machine translation tasks show these models to be""" """ superior in quality while being more parallelizable and requiring significantly less time to""" """ train. Our model achieves 28.4 BLEU on the WMT 2014 English-to-German translation task,""" """ improving over the existing best results, including ensembles by over 2 BLEU. On the WMT 2014""" """ English-to-French translation task, our model establishes a new single-model state-of-the-art""" """ BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small fraction of the training""" """ costs of the best models from the literature. We show that the Transformer generalizes well to""" """ other tasks by applying it successfully to English constituency parsing both with large and""" """ limited training data.""" ), """labels""": ["""translation""", """machine learning""", """vision""", """statistics"""], """scores""": [0.8_1_7, 0.7_1_3, 0.0_1_8, 0.0_1_8], }, ) @slow @require_tf def __snake_case (self ) -> Optional[int]: UpperCAmelCase_: List[str] = pipeline("""zero-shot-classification""", model="""roberta-large-mnli""", framework="""tf""" ) UpperCAmelCase_: Optional[Any] = zero_shot_classifier( """Who are you voting for in 2020?""", candidate_labels=["""politics""", """public health""", """science"""] ) self.assertEqual( nested_simplify(SCREAMING_SNAKE_CASE_ ), { """sequence""": """Who are you voting for in 2020?""", """labels""": ["""politics""", """public health""", """science"""], """scores""": [0.9_7_6, 0.0_1_5, 0.0_0_9], }, ) UpperCAmelCase_: Any = zero_shot_classifier( """The dominant sequence transduction models are based on complex recurrent or convolutional neural networks""" """ in an encoder-decoder configuration. The best performing models also connect the encoder and decoder""" """ through an attention mechanism. We propose a new simple network architecture, the Transformer, based""" """ solely on attention mechanisms, dispensing with recurrence and convolutions entirely. Experiments on two""" """ machine translation tasks show these models to be superior in quality while being more parallelizable""" """ and requiring significantly less time to train. Our model achieves 28.4 BLEU on the WMT 2014""" """ English-to-German translation task, improving over the existing best results, including ensembles by""" """ over 2 BLEU. On the WMT 2014 English-to-French translation task, our model establishes a new""" """ single-model state-of-the-art BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small""" """ fraction of the training costs of the best models from the literature. We show that the Transformer""" """ generalizes well to other tasks by applying it successfully to English constituency parsing both with""" """ large and limited training data.""", candidate_labels=["""machine learning""", """statistics""", """translation""", """vision"""], multi_label=SCREAMING_SNAKE_CASE_, ) self.assertEqual( nested_simplify(SCREAMING_SNAKE_CASE_ ), { """sequence""": ( """The dominant sequence transduction models are based on complex recurrent or convolutional neural""" """ networks in an encoder-decoder configuration. The best performing models also connect the""" """ encoder and decoder through an attention mechanism. We propose a new simple network""" """ architecture, the Transformer, based solely on attention mechanisms, dispensing with recurrence""" """ and convolutions entirely. Experiments on two machine translation tasks show these models to be""" """ superior in quality while being more parallelizable and requiring significantly less time to""" """ train. Our model achieves 28.4 BLEU on the WMT 2014 English-to-German translation task,""" """ improving over the existing best results, including ensembles by over 2 BLEU. On the WMT 2014""" """ English-to-French translation task, our model establishes a new single-model state-of-the-art""" """ BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small fraction of the training""" """ costs of the best models from the literature. We show that the Transformer generalizes well to""" """ other tasks by applying it successfully to English constituency parsing both with large and""" """ limited training data.""" ), """labels""": ["""translation""", """machine learning""", """vision""", """statistics"""], """scores""": [0.8_1_7, 0.7_1_3, 0.0_1_8, 0.0_1_8], }, )
82
1
from typing import List, Optional, Union from ...image_utils import ImageInput from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy from ...utils import TensorType class _a ( _lowerCAmelCase ): A = ['''image_processor''', '''tokenizer'''] A = '''BlipImageProcessor''' A = '''AutoTokenizer''' def __init__(self, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ ) -> Optional[Any]: UpperCAmelCase_: Optional[Any] = False super().__init__(SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ ) UpperCAmelCase_: Any = self.image_processor def __call__(self, SCREAMING_SNAKE_CASE_ = None, SCREAMING_SNAKE_CASE_ = None, SCREAMING_SNAKE_CASE_ = True, SCREAMING_SNAKE_CASE_ = False, SCREAMING_SNAKE_CASE_ = None, SCREAMING_SNAKE_CASE_ = None, SCREAMING_SNAKE_CASE_ = 0, SCREAMING_SNAKE_CASE_ = None, SCREAMING_SNAKE_CASE_ = None, SCREAMING_SNAKE_CASE_ = False, SCREAMING_SNAKE_CASE_ = False, SCREAMING_SNAKE_CASE_ = False, SCREAMING_SNAKE_CASE_ = False, SCREAMING_SNAKE_CASE_ = False, SCREAMING_SNAKE_CASE_ = True, SCREAMING_SNAKE_CASE_ = None, **SCREAMING_SNAKE_CASE_, ) -> BatchEncoding: if images is None and text is None: raise ValueError("""You have to specify either images or text.""" ) # Get only text if images is None: UpperCAmelCase_: str = self.tokenizer UpperCAmelCase_: Optional[int] = self.tokenizer( text=SCREAMING_SNAKE_CASE_, add_special_tokens=SCREAMING_SNAKE_CASE_, padding=SCREAMING_SNAKE_CASE_, truncation=SCREAMING_SNAKE_CASE_, max_length=SCREAMING_SNAKE_CASE_, stride=SCREAMING_SNAKE_CASE_, pad_to_multiple_of=SCREAMING_SNAKE_CASE_, return_attention_mask=SCREAMING_SNAKE_CASE_, return_overflowing_tokens=SCREAMING_SNAKE_CASE_, return_special_tokens_mask=SCREAMING_SNAKE_CASE_, return_offsets_mapping=SCREAMING_SNAKE_CASE_, return_token_type_ids=SCREAMING_SNAKE_CASE_, return_length=SCREAMING_SNAKE_CASE_, verbose=SCREAMING_SNAKE_CASE_, return_tensors=SCREAMING_SNAKE_CASE_, **SCREAMING_SNAKE_CASE_, ) return text_encoding # add pixel_values UpperCAmelCase_: Optional[Any] = self.image_processor(SCREAMING_SNAKE_CASE_, return_tensors=SCREAMING_SNAKE_CASE_ ) if text is not None: UpperCAmelCase_: Optional[Any] = self.tokenizer( text=SCREAMING_SNAKE_CASE_, add_special_tokens=SCREAMING_SNAKE_CASE_, padding=SCREAMING_SNAKE_CASE_, truncation=SCREAMING_SNAKE_CASE_, max_length=SCREAMING_SNAKE_CASE_, stride=SCREAMING_SNAKE_CASE_, pad_to_multiple_of=SCREAMING_SNAKE_CASE_, return_attention_mask=SCREAMING_SNAKE_CASE_, return_overflowing_tokens=SCREAMING_SNAKE_CASE_, return_special_tokens_mask=SCREAMING_SNAKE_CASE_, return_offsets_mapping=SCREAMING_SNAKE_CASE_, return_token_type_ids=SCREAMING_SNAKE_CASE_, return_length=SCREAMING_SNAKE_CASE_, verbose=SCREAMING_SNAKE_CASE_, return_tensors=SCREAMING_SNAKE_CASE_, **SCREAMING_SNAKE_CASE_, ) else: UpperCAmelCase_: List[str] = None if text_encoding is not None: encoding_image_processor.update(SCREAMING_SNAKE_CASE_ ) return encoding_image_processor def __snake_case (self, *SCREAMING_SNAKE_CASE_, **SCREAMING_SNAKE_CASE_ ) -> Dict: return self.tokenizer.batch_decode(*SCREAMING_SNAKE_CASE_, **SCREAMING_SNAKE_CASE_ ) def __snake_case (self, *SCREAMING_SNAKE_CASE_, **SCREAMING_SNAKE_CASE_ ) -> int: return self.tokenizer.decode(*SCREAMING_SNAKE_CASE_, **SCREAMING_SNAKE_CASE_ ) @property # Copied from transformers.models.blip.processing_blip.BlipProcessor.model_input_names def __snake_case (self ) -> Tuple: UpperCAmelCase_: Union[str, Any] = self.tokenizer.model_input_names UpperCAmelCase_: Union[str, Any] = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) )
147
from __future__ import annotations import unittest from transformers import EsmConfig, is_tf_available from transformers.testing_utils import require_tf, slow 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 numpy import tensorflow as tf from transformers.models.esm.modeling_tf_esm import ( TF_ESM_PRETRAINED_MODEL_ARCHIVE_LIST, TFEsmForMaskedLM, TFEsmForSequenceClassification, TFEsmForTokenClassification, TFEsmModel, ) class _a : def __init__(self, SCREAMING_SNAKE_CASE_, ) -> Optional[Any]: UpperCAmelCase_: Optional[int] = parent UpperCAmelCase_: List[Any] = 13 UpperCAmelCase_: Union[str, Any] = 7 UpperCAmelCase_: Optional[Any] = True UpperCAmelCase_: Tuple = True UpperCAmelCase_: Dict = True UpperCAmelCase_: str = 99 UpperCAmelCase_: Tuple = 32 UpperCAmelCase_: Optional[int] = 2 UpperCAmelCase_: Union[str, Any] = 4 UpperCAmelCase_: List[Any] = 37 UpperCAmelCase_: str = """gelu""" UpperCAmelCase_: Dict = 0.1 UpperCAmelCase_: Optional[Any] = 0.1 UpperCAmelCase_: Optional[Any] = 512 UpperCAmelCase_: List[str] = 16 UpperCAmelCase_: Any = 2 UpperCAmelCase_: Union[str, Any] = 0.0_2 UpperCAmelCase_: List[str] = 3 UpperCAmelCase_: Tuple = 4 UpperCAmelCase_: Any = None def __snake_case (self ) -> str: UpperCAmelCase_: List[Any] = ids_tensor([self.batch_size, self.seq_length], self.vocab_size ) UpperCAmelCase_: Optional[int] = None if self.use_input_mask: UpperCAmelCase_: Union[str, Any] = random_attention_mask([self.batch_size, self.seq_length] ) UpperCAmelCase_: List[Any] = None UpperCAmelCase_: Optional[int] = None UpperCAmelCase_: Tuple = None if self.use_labels: UpperCAmelCase_: List[Any] = ids_tensor([self.batch_size], self.type_sequence_label_size ) UpperCAmelCase_: int = ids_tensor([self.batch_size, self.seq_length], self.num_labels ) UpperCAmelCase_: List[Any] = ids_tensor([self.batch_size], self.num_choices ) UpperCAmelCase_: List[str] = EsmConfig( vocab_size=self.vocab_size, hidden_size=self.hidden_size, num_hidden_layers=self.num_hidden_layers, pad_token_id=1, 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, ) return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels def __snake_case (self ) -> Optional[int]: ( ( UpperCAmelCase_ ) , ( UpperCAmelCase_ ) , ( UpperCAmelCase_ ) , ( UpperCAmelCase_ ) , ( UpperCAmelCase_ ) , ( UpperCAmelCase_ ) , ): Optional[Any] = self.prepare_config_and_inputs() UpperCAmelCase_: Dict = True UpperCAmelCase_: Dict = floats_tensor([self.batch_size, self.seq_length, self.hidden_size] ) UpperCAmelCase_: Dict = ids_tensor([self.batch_size, self.seq_length], vocab_size=2 ) return ( config, input_ids, input_mask, sequence_labels, token_labels, choice_labels, encoder_hidden_states, encoder_attention_mask, ) def __snake_case (self, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ ) -> Tuple: UpperCAmelCase_: Optional[int] = TFEsmModel(config=SCREAMING_SNAKE_CASE_ ) UpperCAmelCase_: str = {"""input_ids""": input_ids, """attention_mask""": input_mask} UpperCAmelCase_: Dict = model(SCREAMING_SNAKE_CASE_ ) UpperCAmelCase_: Dict = [input_ids, input_mask] UpperCAmelCase_: Dict = model(SCREAMING_SNAKE_CASE_ ) UpperCAmelCase_: List[Any] = model(SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual(result.last_hidden_state.shape, (self.batch_size, self.seq_length, self.hidden_size) ) def __snake_case (self, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, ) -> List[str]: UpperCAmelCase_: Tuple = True UpperCAmelCase_: List[Any] = TFEsmModel(config=SCREAMING_SNAKE_CASE_ ) UpperCAmelCase_: Optional[int] = { """input_ids""": input_ids, """attention_mask""": input_mask, """encoder_hidden_states""": encoder_hidden_states, """encoder_attention_mask""": encoder_attention_mask, } UpperCAmelCase_: Optional[Any] = model(SCREAMING_SNAKE_CASE_ ) UpperCAmelCase_: Tuple = [input_ids, input_mask] UpperCAmelCase_: List[Any] = model(SCREAMING_SNAKE_CASE_, encoder_hidden_states=SCREAMING_SNAKE_CASE_ ) # Also check the case where encoder outputs are not passed UpperCAmelCase_: Tuple = model(SCREAMING_SNAKE_CASE_, attention_mask=SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual(result.last_hidden_state.shape, (self.batch_size, self.seq_length, self.hidden_size) ) def __snake_case (self, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ ) -> Optional[int]: UpperCAmelCase_: List[Any] = TFEsmForMaskedLM(config=SCREAMING_SNAKE_CASE_ ) UpperCAmelCase_: Optional[int] = model([input_ids, input_mask] ) self.parent.assertEqual(result.logits.shape, (self.batch_size, self.seq_length, self.vocab_size) ) def __snake_case (self, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ ) -> Optional[Any]: UpperCAmelCase_: int = self.num_labels UpperCAmelCase_: Dict = TFEsmForTokenClassification(config=SCREAMING_SNAKE_CASE_ ) UpperCAmelCase_: List[Any] = {"""input_ids""": input_ids, """attention_mask""": input_mask} UpperCAmelCase_: Tuple = model(SCREAMING_SNAKE_CASE_ ) self.parent.assertEqual(result.logits.shape, (self.batch_size, self.seq_length, self.num_labels) ) def __snake_case (self ) -> Any: UpperCAmelCase_: Dict = self.prepare_config_and_inputs() ( ( UpperCAmelCase_ ) , ( UpperCAmelCase_ ) , ( UpperCAmelCase_ ) , ( UpperCAmelCase_ ) , ( UpperCAmelCase_ ) , ( UpperCAmelCase_ ) , ): str = config_and_inputs UpperCAmelCase_: int = {"""input_ids""": input_ids, """attention_mask""": input_mask} return config, inputs_dict @require_tf class _a ( _lowerCAmelCase , _lowerCAmelCase , unittest.TestCase ): A = ( ( TFEsmModel, TFEsmForMaskedLM, TFEsmForSequenceClassification, TFEsmForTokenClassification, ) if is_tf_available() else () ) A = ( { '''feature-extraction''': TFEsmModel, '''fill-mask''': TFEsmForMaskedLM, '''text-classification''': TFEsmForSequenceClassification, '''token-classification''': TFEsmForTokenClassification, '''zero-shot''': TFEsmForSequenceClassification, } if is_tf_available() else {} ) A = False A = False def __snake_case (self ) -> Tuple: UpperCAmelCase_: List[Any] = TFEsmModelTester(self ) UpperCAmelCase_: Union[str, Any] = ConfigTester(self, config_class=SCREAMING_SNAKE_CASE_, hidden_size=37 ) def __snake_case (self ) -> Any: self.config_tester.run_common_tests() def __snake_case (self ) -> List[str]: UpperCAmelCase_: int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE_ ) def __snake_case (self ) -> List[Any]: UpperCAmelCase_: Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_decoder() self.model_tester.create_and_check_model_as_decoder(*SCREAMING_SNAKE_CASE_ ) def __snake_case (self ) -> Tuple: UpperCAmelCase_: Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*SCREAMING_SNAKE_CASE_ ) def __snake_case (self ) -> List[str]: UpperCAmelCase_: Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*SCREAMING_SNAKE_CASE_ ) @slow def __snake_case (self ) -> str: for model_name in TF_ESM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCAmelCase_: Dict = TFEsmModel.from_pretrained(SCREAMING_SNAKE_CASE_ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE_ ) @unittest.skip("""Protein models do not support embedding resizing.""" ) def __snake_case (self ) -> Tuple: pass @unittest.skip("""Protein models do not support embedding resizing.""" ) def __snake_case (self ) -> Optional[Any]: pass def __snake_case (self ) -> List[Any]: UpperCAmelCase_ , UpperCAmelCase_: Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCAmelCase_: List[Any] = model_class(SCREAMING_SNAKE_CASE_ ) assert isinstance(model.get_input_embeddings(), tf.keras.layers.Layer ) if model_class is TFEsmForMaskedLM: # Output embedding test differs from the main test because they're a matrix, not a layer UpperCAmelCase_: Any = model.get_bias() assert isinstance(SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ ) for k, v in name.items(): assert isinstance(SCREAMING_SNAKE_CASE_, tf.Variable ) else: UpperCAmelCase_: Union[str, Any] = model.get_output_embeddings() assert x is None UpperCAmelCase_: Optional[int] = model.get_bias() assert name is None @require_tf class _a ( unittest.TestCase ): @slow def __snake_case (self ) -> str: UpperCAmelCase_: str = TFEsmForMaskedLM.from_pretrained("""facebook/esm2_t6_8M_UR50D""" ) UpperCAmelCase_: Tuple = tf.constant([[0, 1, 2, 3, 4, 5]] ) UpperCAmelCase_: int = model(SCREAMING_SNAKE_CASE_ )[0] UpperCAmelCase_: Optional[int] = [1, 6, 33] self.assertEqual(list(output.numpy().shape ), SCREAMING_SNAKE_CASE_ ) # compare the actual values for a slice. UpperCAmelCase_: List[str] = tf.constant( [ [ [8.9_2_1_5_1_8, -1_0.5_8_9_8_1_4, -6.4_6_7_1_3_0_7], [-6.3_9_6_7_1_5_6, -1_3.9_1_1_3_7_7, -1.1_2_1_1_9_1_5], [-7.7_8_1_2_4_7, -1_3.9_5_1_5_5_7, -3.7_4_0_5_9_2], ] ] ) self.assertTrue(numpy.allclose(output[:, :3, :3].numpy(), expected_slice.numpy(), atol=1E-2 ) ) @slow def __snake_case (self ) -> Optional[int]: UpperCAmelCase_: List[Any] = TFEsmModel.from_pretrained("""facebook/esm2_t6_8M_UR50D""" ) UpperCAmelCase_: Any = tf.constant([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]] ) UpperCAmelCase_: Optional[int] = model(SCREAMING_SNAKE_CASE_ )[0] # compare the actual values for a slice. UpperCAmelCase_: str = tf.constant( [ [ [0.1_4_4_4_3_0_9_2, 0.5_4_1_2_5_3_2_7, 0.3_2_4_7_7_3_9], [0.3_0_3_4_0_4_8_4, 0.0_0_5_2_6_6_7_6, 0.3_1_0_7_7_7_2_2], [0.3_2_2_7_8_0_4_3, -0.2_4_9_8_7_0_9_6, 0.3_4_1_4_6_2_8], ] ] ) self.assertTrue(numpy.allclose(output[:, :3, :3].numpy(), expected_slice.numpy(), atol=1E-4 ) )
147
1
"""simple docstring""" from . import ( albert, align, altclip, audio_spectrogram_transformer, auto, autoformer, bark, bart, barthez, bartpho, beit, bert, bert_generation, bert_japanese, bertweet, big_bird, bigbird_pegasus, biogpt, bit, blenderbot, blenderbot_small, blip, blip_a, bloom, bridgetower, byta, camembert, canine, chinese_clip, clap, clip, clipseg, codegen, conditional_detr, convbert, convnext, convnextva, cpm, cpmant, ctrl, cvt, dataavec, deberta, deberta_va, decision_transformer, deformable_detr, deit, deprecated, deta, detr, dialogpt, dinat, distilbert, dit, donut, dpr, dpt, efficientformer, efficientnet, electra, encodec, encoder_decoder, ernie, ernie_m, esm, falcon, flaubert, flava, fnet, focalnet, fsmt, funnel, git, glpn, gpta, gpt_bigcode, gpt_neo, gpt_neox, gpt_neox_japanese, gpt_swa, gptj, gptsan_japanese, graphormer, groupvit, herbert, hubert, ibert, imagegpt, informer, instructblip, jukebox, layoutlm, layoutlmva, layoutlmva, layoutxlm, led, levit, lilt, llama, longformer, longta, luke, lxmert, mam_aaa, marian, markuplm, maskaformer, maskformer, mbart, mbartaa, mega, megatron_bert, megatron_gpta, mgp_str, mluke, mobilebert, mobilenet_va, mobilenet_va, mobilevit, mobilevitva, mpnet, mra, mta, musicgen, mvp, nat, nezha, nllb, nllb_moe, nystromformer, oneformer, open_llama, openai, opt, owlvit, pegasus, pegasus_x, perceiver, phobert, pixastruct, plbart, poolformer, prophetnet, qdqbert, rag, realm, reformer, regnet, rembert, resnet, roberta, roberta_prelayernorm, roc_bert, roformer, rwkv, sam, segformer, sew, sew_d, speech_encoder_decoder, speech_to_text, speech_to_text_a, speechta, splinter, squeezebert, swiftformer, swin, swinasr, swinva, switch_transformers, ta, table_transformer, tapas, time_series_transformer, timesformer, timm_backbone, transfo_xl, trocr, tvlt, umta, unispeech, unispeech_sat, upernet, videomae, vilt, vision_encoder_decoder, vision_text_dual_encoder, visual_bert, vit, vit_hybrid, vit_mae, vit_msn, vivit, wavaveca, wavaveca_conformer, wavaveca_phoneme, wavaveca_with_lm, wavlm, whisper, x_clip, xglm, xlm, xlm_prophetnet, xlm_roberta, xlm_roberta_xl, xlnet, xmod, yolos, yoso, )
356
"""simple docstring""" import argparse import os import torch from transformers.utils import WEIGHTS_NAME __UpperCamelCase : int = ['''small''', '''medium''', '''large'''] __UpperCamelCase : str = '''lm_head.decoder.weight''' __UpperCamelCase : Dict = '''lm_head.weight''' def _SCREAMING_SNAKE_CASE (_UpperCAmelCase : str , _UpperCAmelCase : str ): lowerCAmelCase = torch.load(_UpperCAmelCase ) lowerCAmelCase = d.pop(_UpperCAmelCase ) os.makedirs(_UpperCAmelCase , exist_ok=_UpperCAmelCase ) torch.save(_UpperCAmelCase , os.path.join(_UpperCAmelCase , _UpperCAmelCase ) ) if __name__ == "__main__": __UpperCamelCase : Optional[int] = argparse.ArgumentParser() parser.add_argument('''--dialogpt_path''', default='''.''', type=str) __UpperCamelCase : Optional[int] = parser.parse_args() for MODEL in DIALOGPT_MODELS: __UpperCamelCase : Dict = os.path.join(args.dialogpt_path, f'''{MODEL}_ft.pkl''') __UpperCamelCase : str = f'''./DialoGPT-{MODEL}''' convert_dialogpt_checkpoint( checkpoint_path, pytorch_dump_folder_path, )
309
0
import inspect import unittest from transformers import DPTConfig from transformers.file_utils import is_torch_available, is_vision_available from transformers.models.auto import get_values from transformers.testing_utils import require_torch, require_vision, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import MODEL_MAPPING, DPTForDepthEstimation, DPTForSemanticSegmentation, DPTModel from transformers.models.dpt.modeling_dpt import DPT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import DPTImageProcessor class __lowerCAmelCase : """simple docstring""" def __init__( self : str , _snake_case : Union[str, Any] , _snake_case : Dict=2 , _snake_case : str=32 , _snake_case : Dict=16 , _snake_case : int=3 , _snake_case : Optional[Any]=True , _snake_case : Optional[Any]=True , _snake_case : Union[str, Any]=32 , _snake_case : Dict=4 , _snake_case : Union[str, Any]=[0, 1, 2, 3] , _snake_case : Any=4 , _snake_case : Union[str, Any]=37 , _snake_case : str="gelu" , _snake_case : Optional[int]=0.1 , _snake_case : Tuple=0.1 , _snake_case : Dict=0.02 , _snake_case : Any=3 , _snake_case : Any=[1, 384, 24, 24] , _snake_case : Dict=True , _snake_case : Any=None , ): __lowercase : Any = parent __lowercase : Dict = batch_size __lowercase : Optional[int] = image_size __lowercase : int = patch_size __lowercase : Tuple = num_channels __lowercase : str = is_training __lowercase : List[str] = use_labels __lowercase : Optional[Any] = hidden_size __lowercase : Union[str, Any] = num_hidden_layers __lowercase : Optional[Any] = backbone_out_indices __lowercase : Union[str, Any] = num_attention_heads __lowercase : List[str] = intermediate_size __lowercase : str = hidden_act __lowercase : Dict = hidden_dropout_prob __lowercase : Tuple = attention_probs_dropout_prob __lowercase : Optional[Any] = initializer_range __lowercase : Tuple = num_labels __lowercase : Any = backbone_featmap_shape __lowercase : Dict = scope __lowercase : Any = is_hybrid # sequence length of DPT = num_patches + 1 (we add 1 for the [CLS] token) __lowercase : Tuple = (image_size // patch_size) ** 2 __lowercase : Optional[Any] = num_patches + 1 def snake_case_ ( self : List[Any] ): __lowercase : List[str] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) __lowercase : List[str] = None if self.use_labels: __lowercase : Any = ids_tensor([self.batch_size, self.image_size, self.image_size] , self.num_labels ) __lowercase : int = self.get_config() return config, pixel_values, labels def snake_case_ ( self : Optional[Any] ): __lowercase : Optional[int] = { '''global_padding''': '''same''', '''layer_type''': '''bottleneck''', '''depths''': [3, 4, 9], '''out_features''': ['''stage1''', '''stage2''', '''stage3'''], '''embedding_dynamic_padding''': True, '''hidden_sizes''': [96, 192, 384, 768], '''num_groups''': 2, } return DPTConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , backbone_out_indices=self.backbone_out_indices , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=_snake_case , initializer_range=self.initializer_range , is_hybrid=self.is_hybrid , backbone_config=_snake_case , backbone_featmap_shape=self.backbone_featmap_shape , ) def snake_case_ ( self : Union[str, Any] , _snake_case : Optional[Any] , _snake_case : List[Any] , _snake_case : int ): __lowercase : Dict = DPTModel(config=_snake_case ) model.to(_snake_case ) model.eval() __lowercase : int = model(_snake_case ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def snake_case_ ( self : List[str] , _snake_case : str , _snake_case : Any , _snake_case : List[str] ): __lowercase : List[Any] = self.num_labels __lowercase : Any = DPTForDepthEstimation(_snake_case ) model.to(_snake_case ) model.eval() __lowercase : Tuple = model(_snake_case ) self.parent.assertEqual(result.predicted_depth.shape , (self.batch_size, self.image_size, self.image_size) ) def snake_case_ ( self : Optional[Any] , _snake_case : Dict , _snake_case : List[str] , _snake_case : int ): __lowercase : Dict = self.num_labels __lowercase : Any = DPTForSemanticSegmentation(_snake_case ) model.to(_snake_case ) model.eval() __lowercase : int = model(_snake_case , labels=_snake_case ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size, self.image_size) ) def snake_case_ ( self : Union[str, Any] ): __lowercase : List[Any] = self.prepare_config_and_inputs() __lowercase , __lowercase , __lowercase : Tuple = config_and_inputs __lowercase : Union[str, Any] = {'''pixel_values''': pixel_values} return config, inputs_dict @require_torch class __lowerCAmelCase ( lowerCAmelCase_ , lowerCAmelCase_ , unittest.TestCase ): """simple docstring""" A__ : Union[str, Any] = (DPTModel, DPTForDepthEstimation, DPTForSemanticSegmentation) if is_torch_available() else () A__ : Union[str, Any] = ( { '''depth-estimation''': DPTForDepthEstimation, '''feature-extraction''': DPTModel, '''image-segmentation''': DPTForSemanticSegmentation, } if is_torch_available() else {} ) A__ : Tuple = False A__ : Dict = False A__ : Optional[Any] = False def snake_case_ ( self : str ): __lowercase : Union[str, Any] = DPTModelTester(self ) __lowercase : List[Any] = ConfigTester(self , config_class=_snake_case , has_text_modality=_snake_case , hidden_size=37 ) def snake_case_ ( self : Any ): self.config_tester.run_common_tests() @unittest.skip(reason='''DPT does not use inputs_embeds''' ) def snake_case_ ( self : Any ): pass def snake_case_ ( self : Any ): __lowercase , __lowercase : List[str] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: __lowercase : Union[str, Any] = model_class(_snake_case ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) __lowercase : str = model.get_output_embeddings() self.assertTrue(x is None or isinstance(_snake_case , nn.Linear ) ) def snake_case_ ( self : Tuple ): __lowercase , __lowercase : Tuple = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: __lowercase : str = model_class(_snake_case ) __lowercase : int = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic __lowercase : List[Any] = [*signature.parameters.keys()] __lowercase : Tuple = ['''pixel_values'''] self.assertListEqual(arg_names[:1] , _snake_case ) def snake_case_ ( self : List[str] ): __lowercase : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_snake_case ) def snake_case_ ( self : List[Any] ): __lowercase : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_depth_estimation(*_snake_case ) def snake_case_ ( self : Union[str, Any] ): __lowercase : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_semantic_segmentation(*_snake_case ) def snake_case_ ( self : Optional[Any] ): for model_class in self.all_model_classes: if model_class.__name__ == "DPTForDepthEstimation": continue __lowercase , __lowercase : Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() __lowercase : Optional[Any] = True if model_class in get_values(_snake_case ): continue __lowercase : Optional[Any] = model_class(_snake_case ) model.to(_snake_case ) model.train() __lowercase : int = self._prepare_for_class(_snake_case , _snake_case , return_labels=_snake_case ) __lowercase : Any = model(**_snake_case ).loss loss.backward() def snake_case_ ( self : Union[str, Any] ): for model_class in self.all_model_classes: if model_class.__name__ == "DPTForDepthEstimation": continue __lowercase , __lowercase : int = self.model_tester.prepare_config_and_inputs_for_common() __lowercase : List[Any] = False __lowercase : Optional[Any] = True if model_class in get_values(_snake_case ) or not model_class.supports_gradient_checkpointing: continue __lowercase : List[Any] = model_class(_snake_case ) model.to(_snake_case ) model.gradient_checkpointing_enable() model.train() __lowercase : Optional[Any] = self._prepare_for_class(_snake_case , _snake_case , return_labels=_snake_case ) __lowercase : Union[str, Any] = model(**_snake_case ).loss loss.backward() def snake_case_ ( self : Optional[Any] ): __lowercase , __lowercase : Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() __lowercase : Tuple = _config_zero_init(_snake_case ) for model_class in self.all_model_classes: __lowercase : Dict = model_class(config=_snake_case ) # Skip the check for the backbone __lowercase : List[Any] = [] for name, module in model.named_modules(): if module.__class__.__name__ == "DPTViTHybridEmbeddings": __lowercase : Dict = [F'{name}.{key}' for key in module.state_dict().keys()] break for name, param in model.named_parameters(): if param.requires_grad: if name in backbone_params: continue self.assertIn( ((param.data.mean() * 1E9).round() / 1E9).item() , [0.0, 1.0] , msg=F'Parameter {name} of model {model_class} seems not properly initialized' , ) @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.''' ) def snake_case_ ( self : Dict ): pass @slow def snake_case_ ( self : Optional[Any] ): for model_name in DPT_PRETRAINED_MODEL_ARCHIVE_LIST[1:]: __lowercase : List[str] = DPTModel.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) def snake_case_ ( self : int ): # We do this test only for DPTForDepthEstimation since it is the only model that uses readout_type __lowercase , __lowercase : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() __lowercase : Optional[Any] = '''add''' with self.assertRaises(_snake_case ): __lowercase : int = DPTForDepthEstimation(_snake_case ) def UpperCAmelCase_ ( ) -> List[Any]: __lowercase : str = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_torch @require_vision @slow class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" def snake_case_ ( self : Any ): __lowercase : str = DPTImageProcessor.from_pretrained('''Intel/dpt-hybrid-midas''' ) __lowercase : int = DPTForDepthEstimation.from_pretrained('''Intel/dpt-hybrid-midas''' ).to(_snake_case ) __lowercase : Tuple = prepare_img() __lowercase : List[Any] = image_processor(images=_snake_case , return_tensors='''pt''' ).to(_snake_case ) # forward pass with torch.no_grad(): __lowercase : List[Any] = model(**_snake_case ) __lowercase : Optional[int] = outputs.predicted_depth # verify the predicted depth __lowercase : List[Any] = torch.Size((1, 384, 384) ) self.assertEqual(predicted_depth.shape , _snake_case ) __lowercase : Optional[int] = torch.tensor( [[[5.64_37, 5.61_46, 5.65_11], [5.43_71, 5.56_49, 5.59_58], [5.52_15, 5.51_84, 5.52_93]]] ).to(_snake_case ) self.assertTrue(torch.allclose(outputs.predicted_depth[:3, :3, :3] / 100 , _snake_case , atol=1E-4 ) )
156
from copy import deepcopy from typing import Optional, Union import numpy as np from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding from ...utils import TensorType, is_tf_available, is_torch_available if is_torch_available(): import torch if is_tf_available(): import tensorflow as tf class __lowerCAmelCase ( lowerCAmelCase_ ): """simple docstring""" A__ : Union[str, Any] = ['''image_processor'''] A__ : Any = '''SamImageProcessor''' def __init__( self : Tuple , _snake_case : Tuple ): super().__init__(_snake_case ) __lowercase : str = self.image_processor __lowercase : Any = -10 __lowercase : Dict = self.image_processor.size['''longest_edge'''] def __call__( self : Dict , _snake_case : str=None , _snake_case : Any=None , _snake_case : List[str]=None , _snake_case : Any=None , _snake_case : Optional[Union[str, TensorType]] = None , **_snake_case : List[Any] , ): __lowercase : List[str] = self.image_processor( _snake_case , return_tensors=_snake_case , **_snake_case , ) # pop arguments that are not used in the foward but used nevertheless __lowercase : Optional[int] = encoding_image_processor['''original_sizes'''] if hasattr(_snake_case , '''numpy''' ): # Checks if Torch or TF tensor __lowercase : Optional[int] = original_sizes.numpy() __lowercase , __lowercase , __lowercase : str = self._check_and_preprocess_points( input_points=_snake_case , input_labels=_snake_case , input_boxes=_snake_case , ) __lowercase : int = self._normalize_and_convert( _snake_case , _snake_case , input_points=_snake_case , input_labels=_snake_case , input_boxes=_snake_case , return_tensors=_snake_case , ) return encoding_image_processor def snake_case_ ( self : List[str] , _snake_case : int , _snake_case : Optional[int] , _snake_case : Any=None , _snake_case : Optional[Any]=None , _snake_case : Any=None , _snake_case : str="pt" , ): if input_points is not None: if len(_snake_case ) != len(_snake_case ): __lowercase : Optional[Any] = [ self._normalize_coordinates(self.target_size , _snake_case , original_sizes[0] ) for point in input_points ] else: __lowercase : List[Any] = [ self._normalize_coordinates(self.target_size , _snake_case , _snake_case ) for point, original_size in zip(_snake_case , _snake_case ) ] # check that all arrays have the same shape if not all(point.shape == input_points[0].shape for point in input_points ): if input_labels is not None: __lowercase , __lowercase : Tuple = self._pad_points_and_labels(_snake_case , _snake_case ) __lowercase : Dict = np.array(_snake_case ) if input_labels is not None: __lowercase : Dict = np.array(_snake_case ) if input_boxes is not None: if len(_snake_case ) != len(_snake_case ): __lowercase : Union[str, Any] = [ self._normalize_coordinates(self.target_size , _snake_case , original_sizes[0] , is_bounding_box=_snake_case ) for box in input_boxes ] else: __lowercase : Tuple = [ self._normalize_coordinates(self.target_size , _snake_case , _snake_case , is_bounding_box=_snake_case ) for box, original_size in zip(_snake_case , _snake_case ) ] __lowercase : Dict = np.array(_snake_case ) if input_boxes is not None: if return_tensors == "pt": __lowercase : int = torch.from_numpy(_snake_case ) # boxes batch size of 1 by default __lowercase : List[Any] = input_boxes.unsqueeze(1 ) if len(input_boxes.shape ) != 3 else input_boxes elif return_tensors == "tf": __lowercase : Dict = tf.convert_to_tensor(_snake_case ) # boxes batch size of 1 by default __lowercase : int = tf.expand_dims(_snake_case , 1 ) if len(input_boxes.shape ) != 3 else input_boxes encoding_image_processor.update({'''input_boxes''': input_boxes} ) if input_points is not None: if return_tensors == "pt": __lowercase : Tuple = torch.from_numpy(_snake_case ) # point batch size of 1 by default __lowercase : Tuple = input_points.unsqueeze(1 ) if len(input_points.shape ) != 4 else input_points elif return_tensors == "tf": __lowercase : List[Any] = tf.convert_to_tensor(_snake_case ) # point batch size of 1 by default __lowercase : Optional[int] = tf.expand_dims(_snake_case , 1 ) if len(input_points.shape ) != 4 else input_points encoding_image_processor.update({'''input_points''': input_points} ) if input_labels is not None: if return_tensors == "pt": __lowercase : int = torch.from_numpy(_snake_case ) # point batch size of 1 by default __lowercase : Any = input_labels.unsqueeze(1 ) if len(input_labels.shape ) != 3 else input_labels elif return_tensors == "tf": __lowercase : Any = tf.convert_to_tensor(_snake_case ) # point batch size of 1 by default __lowercase : Union[str, Any] = tf.expand_dims(_snake_case , 1 ) if len(input_labels.shape ) != 3 else input_labels encoding_image_processor.update({'''input_labels''': input_labels} ) return encoding_image_processor def snake_case_ ( self : int , _snake_case : Any , _snake_case : str ): __lowercase : Union[str, Any] = max([point.shape[0] for point in input_points] ) __lowercase : List[Any] = [] for i, point in enumerate(_snake_case ): if point.shape[0] != expected_nb_points: __lowercase : Optional[Any] = np.concatenate( [point, np.zeros((expected_nb_points - point.shape[0], 2) ) + self.point_pad_value] , axis=0 ) __lowercase : Tuple = np.append(input_labels[i] , [self.point_pad_value] ) processed_input_points.append(_snake_case ) __lowercase : List[Any] = processed_input_points return input_points, input_labels def snake_case_ ( self : Dict , _snake_case : int , _snake_case : np.ndarray , _snake_case : Any , _snake_case : Any=False ): __lowercase , __lowercase : Tuple = original_size __lowercase , __lowercase : Optional[Any] = self.image_processor._get_preprocess_shape(_snake_case , longest_edge=_snake_case ) __lowercase : Optional[int] = deepcopy(_snake_case ).astype(_snake_case ) if is_bounding_box: __lowercase : str = coords.reshape(-1 , 2 , 2 ) __lowercase : Dict = coords[..., 0] * (new_w / old_w) __lowercase : int = coords[..., 1] * (new_h / old_h) if is_bounding_box: __lowercase : Optional[Any] = coords.reshape(-1 , 4 ) return coords def snake_case_ ( self : List[str] , _snake_case : List[Any]=None , _snake_case : Any=None , _snake_case : int=None , ): if input_points is not None: if hasattr(_snake_case , '''numpy''' ): # Checks for TF or Torch tensor __lowercase : Tuple = input_points.numpy().tolist() if not isinstance(_snake_case , _snake_case ) or not isinstance(input_points[0] , _snake_case ): raise ValueError('''Input points must be a list of list of floating points.''' ) __lowercase : str = [np.array(_snake_case ) for input_point in input_points] else: __lowercase : str = None if input_labels is not None: if hasattr(_snake_case , '''numpy''' ): __lowercase : Any = input_labels.numpy().tolist() if not isinstance(_snake_case , _snake_case ) or not isinstance(input_labels[0] , _snake_case ): raise ValueError('''Input labels must be a list of list integers.''' ) __lowercase : List[Any] = [np.array(_snake_case ) for label in input_labels] else: __lowercase : Tuple = None if input_boxes is not None: if hasattr(_snake_case , '''numpy''' ): __lowercase : str = input_boxes.numpy().tolist() if ( not isinstance(_snake_case , _snake_case ) or not isinstance(input_boxes[0] , _snake_case ) or not isinstance(input_boxes[0][0] , _snake_case ) ): raise ValueError('''Input boxes must be a list of list of list of floating points.''' ) __lowercase : List[Any] = [np.array(_snake_case ).astype(np.floataa ) for box in input_boxes] else: __lowercase : Dict = None return input_points, input_labels, input_boxes @property def snake_case_ ( self : List[Any] ): __lowercase : List[Any] = self.image_processor.model_input_names return list(dict.fromkeys(_snake_case ) ) def snake_case_ ( self : str , *_snake_case : Union[str, Any] , **_snake_case : Dict ): return self.image_processor.post_process_masks(*_snake_case , **_snake_case )
156
1
"""simple docstring""" import argparse import re import torch from CLAP import create_model from transformers import AutoFeatureExtractor, ClapConfig, ClapModel _lowercase : Tuple = { 'text_branch': 'text_model', 'audio_branch': 'audio_model.audio_encoder', 'attn': 'attention.self', 'self.proj': 'output.dense', 'attention.self_mask': 'attn_mask', 'mlp.fc1': 'intermediate.dense', 'mlp.fc2': 'output.dense', 'norm1': 'layernorm_before', 'norm2': 'layernorm_after', 'bn0': 'batch_norm', } _lowercase : int = AutoFeatureExtractor.from_pretrained('laion/clap-htsat-unfused', truncation='rand_trunc') def lowercase__ ( snake_case_ :Union[str, Any] , snake_case_ :Optional[Any]=False ): __UpperCAmelCase , __UpperCAmelCase = create_model( '''HTSAT-tiny''' , '''roberta''' , snake_case_ , precision='''fp32''' , device='''cuda:0''' if torch.cuda.is_available() else '''cpu''' , enable_fusion=snake_case_ , fusion_type='''aff_2d''' if enable_fusion else None , ) return model, model_cfg def lowercase__ ( snake_case_ :Tuple ): __UpperCAmelCase = {} __UpperCAmelCase = r'''.*sequential.(\d+).*''' __UpperCAmelCase = r'''.*_projection.(\d+).*''' for key, value in state_dict.items(): # check if any key needs to be modified for key_to_modify, new_key in KEYS_TO_MODIFY_MAPPING.items(): if key_to_modify in key: __UpperCAmelCase = key.replace(snake_case_ , snake_case_ ) if re.match(snake_case_ , snake_case_ ): # replace sequential layers with list __UpperCAmelCase = re.match(snake_case_ , snake_case_ ).group(1 ) __UpperCAmelCase = key.replace(F'''sequential.{sequential_layer}.''' , F'''layers.{int(snake_case_ )//3}.linear.''' ) elif re.match(snake_case_ , snake_case_ ): __UpperCAmelCase = int(re.match(snake_case_ , snake_case_ ).group(1 ) ) # Because in CLAP they use `nn.Sequential`... __UpperCAmelCase = 1 if projecton_layer == 0 else 2 __UpperCAmelCase = key.replace(F'''_projection.{projecton_layer}.''' , F'''_projection.linear{transformers_projection_layer}.''' ) if "audio" and "qkv" in key: # split qkv into query key and value __UpperCAmelCase = value __UpperCAmelCase = mixed_qkv.size(0 ) // 3 __UpperCAmelCase = mixed_qkv[:qkv_dim] __UpperCAmelCase = mixed_qkv[qkv_dim : qkv_dim * 2] __UpperCAmelCase = mixed_qkv[qkv_dim * 2 :] __UpperCAmelCase = query_layer __UpperCAmelCase = key_layer __UpperCAmelCase = value_layer else: __UpperCAmelCase = value return model_state_dict def lowercase__ ( snake_case_ :Union[str, Any] , snake_case_ :Optional[int] , snake_case_ :Union[str, Any] , snake_case_ :Optional[Any]=False ): __UpperCAmelCase , __UpperCAmelCase = init_clap(snake_case_ , enable_fusion=snake_case_ ) clap_model.eval() __UpperCAmelCase = clap_model.state_dict() __UpperCAmelCase = rename_state_dict(snake_case_ ) __UpperCAmelCase = ClapConfig() __UpperCAmelCase = enable_fusion __UpperCAmelCase = ClapModel(snake_case_ ) # ignore the spectrogram embedding layer model.load_state_dict(snake_case_ , strict=snake_case_ ) model.save_pretrained(snake_case_ ) transformers_config.save_pretrained(snake_case_ ) if __name__ == "__main__": _lowercase : List[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('--config_path', default=None, type=str, help='Path to hf config.json of model to convert') parser.add_argument('--enable_fusion', action='store_true', help='Whether to enable fusion or not') _lowercase : int = parser.parse_args() convert_clap_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.enable_fusion)
86
"""simple docstring""" from typing import Union from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging from .base import PIPELINE_INIT_ARGS, Pipeline if is_vision_available(): from PIL import Image from ..image_utils import load_image if is_torch_available(): from ..models.auto.modeling_auto import MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING _lowercase : List[Any] = logging.get_logger(__name__) @add_end_docstrings(_lowerCAmelCase ) class _UpperCAmelCase ( _lowerCAmelCase ): def __init__( self : str , *_lowercase : Tuple , **_lowercase : List[Any] ): super().__init__(*_lowercase , **_lowercase ) self.check_model_type(_lowercase ) def a ( self : int , _lowercase : Dict=None , _lowercase : List[Any]=None , _lowercase : int=None , **_lowercase : Dict ): __UpperCAmelCase , __UpperCAmelCase = {}, {} if padding is not None: __UpperCAmelCase = padding if truncation is not None: __UpperCAmelCase = truncation if top_k is not None: __UpperCAmelCase = top_k return preprocess_params, {}, postprocess_params def __call__( self : List[str] , _lowercase : Union["Image.Image", str] , _lowercase : str = None , **_lowercase : Optional[Any] ): if isinstance(_lowercase , (Image.Image, str) ) and isinstance(_lowercase , _lowercase ): __UpperCAmelCase = {'''image''': image, '''question''': question} else: __UpperCAmelCase = image __UpperCAmelCase = super().__call__(_lowercase , **_lowercase ) return results def a ( self : Union[str, Any] , _lowercase : List[str] , _lowercase : Any=False , _lowercase : Union[str, Any]=False ): __UpperCAmelCase = load_image(inputs['''image'''] ) __UpperCAmelCase = self.tokenizer( inputs['''question'''] , return_tensors=self.framework , padding=_lowercase , truncation=_lowercase ) __UpperCAmelCase = self.image_processor(images=_lowercase , return_tensors=self.framework ) model_inputs.update(_lowercase ) return model_inputs def a ( self : Optional[Any] , _lowercase : str ): __UpperCAmelCase = self.model(**_lowercase ) return model_outputs def a ( self : str , _lowercase : Optional[int] , _lowercase : Any=5 ): if top_k > self.model.config.num_labels: __UpperCAmelCase = self.model.config.num_labels if self.framework == "pt": __UpperCAmelCase = model_outputs.logits.sigmoid()[0] __UpperCAmelCase , __UpperCAmelCase = probs.topk(_lowercase ) else: raise ValueError(F'''Unsupported framework: {self.framework}''' ) __UpperCAmelCase = scores.tolist() __UpperCAmelCase = ids.tolist() return [{"score": score, "answer": self.model.config.idalabel[_id]} for score, _id in zip(_lowercase , _lowercase )]
86
1
"""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 UpperCAmelCase = False class UpperCAmelCase_ ( unittest.TestCase): pass @slow @require_torch_gpu class UpperCAmelCase_ ( unittest.TestCase): def _UpperCamelCase ( self : str ) -> Dict: _UpperCamelCase = VersatileDiffusionImageVariationPipeline.from_pretrained('''shi-labs/versatile-diffusion''' ) pipe.to(UpperCamelCase__ ) pipe.set_progress_bar_config(disable=UpperCamelCase__ ) _UpperCamelCase = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg''' ) _UpperCamelCase = torch.manual_seed(0 ) _UpperCamelCase = pipe( image=UpperCamelCase__ , generator=UpperCamelCase__ , guidance_scale=7.5 , num_inference_steps=50 , output_type='''numpy''' , ).images _UpperCamelCase = image[0, 253:256, 253:256, -1] assert image.shape == (1, 512, 512, 3) _UpperCamelCase = 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
256
'''simple docstring''' from __future__ import annotations class A__ : def __init__( self , UpperCamelCase__=None ) -> Any: '''simple docstring''' A_ = data A_ = None def __repr__( self ) -> List[str]: '''simple docstring''' A_ = [] A_ = self while temp: string_rep.append(f'''{temp.data}''' ) A_ = temp.next return "->".join(UpperCamelCase__ ) def UpperCAmelCase__ ( UpperCAmelCase__ ) -> Tuple: if not elements_list: raise Exception("""The Elements List is empty""" ) A_ = A_ = Node(elements_list[0] ) for i in range(1, len(UpperCAmelCase__ ) ): A_ = Node(elements_list[i] ) A_ = current.next return head def UpperCAmelCase__ ( UpperCAmelCase__ ) -> None: if head_node is not None and isinstance(UpperCAmelCase__, UpperCAmelCase__ ): print_reverse(head_node.next ) print(head_node.data ) def UpperCAmelCase__ ( ) -> Optional[Any]: from doctest import testmod testmod() A_ = make_linked_list([14, 52, 14, 12, 43] ) print("""Linked List:""" ) print(UpperCAmelCase__ ) print("""Elements in Reverse:""" ) print_reverse(UpperCAmelCase__ ) if __name__ == "__main__": main()
162
0
from __future__ import annotations def __UpperCamelCase ( _lowerCAmelCase , _lowerCAmelCase ) -> list[int]: """simple docstring""" __UpperCamelCase : str = 0 __UpperCamelCase : Dict = len(_lowerCAmelCase ) - 1 while i < j: if nums[i] + nums[j] == target: return [i, j] elif nums[i] + nums[j] < target: __UpperCamelCase : Tuple = i + 1 else: __UpperCamelCase : Union[str, Any] = j - 1 return [] if __name__ == "__main__": import doctest doctest.testmod() print(F"""{two_pointer([2, 7, 11, 15], 9) = }""")
355
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 SCREAMING_SNAKE_CASE_:Any = """src/diffusers""" # Matches is_xxx_available() SCREAMING_SNAKE_CASE_:Optional[Any] = re.compile(R"""is\_([a-z_]*)_available\(\)""") # Matches from xxx import bla SCREAMING_SNAKE_CASE_:Union[str, Any] = re.compile(R"""\s+from\s+\S*\s+import\s+([^\(\s].*)\n""") SCREAMING_SNAKE_CASE_:List[Any] = """ {0} = None """ SCREAMING_SNAKE_CASE_: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}) """ SCREAMING_SNAKE_CASE_:int = """ def {0}(*args, **kwargs): requires_backends({0}, {1}) """ def __UpperCamelCase ( _lowerCAmelCase ) -> Tuple: """simple docstring""" A : Union[str, Any] = _re_backend.findall(_lowerCAmelCase ) if len(_lowerCAmelCase ) == 0: return None return "_and_".join(_lowerCAmelCase ) def __UpperCamelCase ( ) -> str: """simple docstring""" with open(os.path.join(_lowerCAmelCase , """__init__.py""" ) , """r""" , encoding="""utf-8""" , newline="""\n""" ) as f: A : Dict = f.readlines() # Get to the point we do the actual imports for type checking A : Dict = 0 A : List[Any] = {} # 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 A : Optional[int] = find_backend(lines[line_index] ) if backend is not None: while not lines[line_index].startswith("""else:""" ): line_index += 1 line_index += 1 A : str = [] # Until we unindent, add backend objects to the list while line_index < len(_lowerCAmelCase ) and len(lines[line_index] ) > 1: A : Tuple = lines[line_index] A : List[str] = _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: A : List[str] = objects else: line_index += 1 return backend_specific_objects def __UpperCamelCase ( _lowerCAmelCase , _lowerCAmelCase ) -> str: """simple docstring""" 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 __UpperCamelCase ( _lowerCAmelCase=None ) -> Tuple: """simple docstring""" if backend_specific_objects is None: A : Union[str, Any] = read_init() # For special correspondence backend to module name as used in the function requires_modulename A : Any = {} for backend, objects in backend_specific_objects.items(): A : str = """[""" + """, """.join(f'''"{b}"''' for b in backend.split("""_and_""" ) ) + """]""" A : Any = """# 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] ) A : Optional[Any] = dummy_file return dummy_files def __UpperCamelCase ( _lowerCAmelCase=False ) -> str: """simple docstring""" A : str = create_dummy_files() # For special correspondence backend to shortcut as used in utils/dummy_xxx_objects.py A : List[str] = {"""torch""": """pt"""} # Locate actual dummy modules and read their content. A : Union[str, Any] = os.path.join(_lowerCAmelCase , """utils""" ) A : Any = { backend: os.path.join(_lowerCAmelCase , f'''dummy_{short_names.get(_lowerCAmelCase , _lowerCAmelCase )}_objects.py''' ) for backend in dummy_files.keys() } A : List[Any] = {} 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: A : Dict = f.read() else: A : str = """""" for backend in dummy_files.keys(): if dummy_files[backend] != actual_dummies[backend]: if overwrite: print( f'''Updating diffusers.utils.dummy_{short_names.get(_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__": SCREAMING_SNAKE_CASE_:List[Any] = argparse.ArgumentParser() parser.add_argument("""--fix_and_overwrite""", action="""store_true""", help="""Whether to fix inconsistencies.""") SCREAMING_SNAKE_CASE_:Any = parser.parse_args() check_dummies(args.fix_and_overwrite)
115
0
def A ( a_ ) -> int: if not isinstance(a_ ,a_ ): raise ValueError('multiplicative_persistence() only accepts integral values' ) if num < 0: raise ValueError('multiplicative_persistence() does not accept negative values' ) __UpperCamelCase : Any =0 __UpperCamelCase : List[str] =str(a_ ) while len(a_ ) != 1: __UpperCamelCase : Optional[int] =[int(a_ ) for i in num_string] __UpperCamelCase : List[Any] =1 for i in range(0 ,len(a_ ) ): total *= numbers[i] __UpperCamelCase : List[str] =str(a_ ) steps += 1 return steps def A ( a_ ) -> int: if not isinstance(a_ ,a_ ): raise ValueError('additive_persistence() only accepts integral values' ) if num < 0: raise ValueError('additive_persistence() does not accept negative values' ) __UpperCamelCase : Union[str, Any] =0 __UpperCamelCase : str =str(a_ ) while len(a_ ) != 1: __UpperCamelCase : Any =[int(a_ ) for i in num_string] __UpperCamelCase : List[Any] =0 for i in range(0 ,len(a_ ) ): total += numbers[i] __UpperCamelCase : Any =str(a_ ) steps += 1 return steps if __name__ == "__main__": import doctest doctest.testmod()
71
'''simple docstring''' class A__ : def __init__( self :List[str] ) -> List[Any]: '''simple docstring''' _a : Tuple =0 _a : Any =0 _a : int ={} def __UpperCAmelCase ( self :Any , SCREAMING_SNAKE_CASE :List[str] ) -> Optional[int]: '''simple docstring''' if vertex not in self.adjacency: _a : Dict ={} self.num_vertices += 1 def __UpperCAmelCase ( self :Optional[Any] , SCREAMING_SNAKE_CASE :str , SCREAMING_SNAKE_CASE :str , SCREAMING_SNAKE_CASE :Any ) -> List[str]: '''simple docstring''' self.add_vertex(SCREAMING_SNAKE_CASE ) self.add_vertex(SCREAMING_SNAKE_CASE ) if head == tail: return _a : Any =weight _a : Tuple =weight def __UpperCAmelCase ( self :Dict ) -> Optional[int]: '''simple docstring''' _a : Union[str, Any] =self.get_edges() for edge in edges: _a , _a , _a : List[str] =edge edges.remove((tail, head, weight) ) for i in range(len(SCREAMING_SNAKE_CASE ) ): _a : str =list(edges[i] ) edges.sort(key=lambda SCREAMING_SNAKE_CASE : e[2] ) for i in range(len(SCREAMING_SNAKE_CASE ) - 1 ): if edges[i][2] >= edges[i + 1][2]: _a : Union[str, Any] =edges[i][2] + 1 for edge in edges: _a , _a , _a : Tuple =edge _a : Tuple =weight _a : List[Any] =weight def __str__( self :int ) -> str: '''simple docstring''' _a : int ="""""" for tail in self.adjacency: for head in self.adjacency[tail]: _a : str =self.adjacency[head][tail] string += f"{head} -> {tail} == {weight}\n" return string.rstrip("""\n""" ) def __UpperCAmelCase ( self :Optional[int] ) -> Optional[Any]: '''simple docstring''' _a : Union[str, Any] =[] for tail in self.adjacency: for head in self.adjacency[tail]: output.append((tail, head, self.adjacency[head][tail]) ) return output def __UpperCAmelCase ( self :List[Any] ) -> List[Any]: '''simple docstring''' return self.adjacency.keys() @staticmethod def __UpperCAmelCase ( SCREAMING_SNAKE_CASE :Dict=None , SCREAMING_SNAKE_CASE :List[Any]=None ) -> Optional[int]: '''simple docstring''' _a : str =Graph() if vertices is None: _a : Union[str, Any] =[] if edges is None: _a : List[Any] =[] for vertex in vertices: g.add_vertex(SCREAMING_SNAKE_CASE ) for edge in edges: g.add_edge(*SCREAMING_SNAKE_CASE ) return g class A__ : def __init__( self :Optional[int] ) -> Union[str, Any]: '''simple docstring''' _a : Optional[int] ={} _a : List[str] ={} def __len__( self :List[Any] ) -> List[Any]: '''simple docstring''' return len(self.parent ) def __UpperCAmelCase ( self :Tuple , SCREAMING_SNAKE_CASE :Tuple ) -> Dict: '''simple docstring''' if item in self.parent: return self.find(SCREAMING_SNAKE_CASE ) _a : Optional[Any] =item _a : List[str] =0 return item def __UpperCAmelCase ( self :int , SCREAMING_SNAKE_CASE :Dict ) -> List[str]: '''simple docstring''' if item not in self.parent: return self.make_set(SCREAMING_SNAKE_CASE ) if item != self.parent[item]: _a : str =self.find(self.parent[item] ) return self.parent[item] def __UpperCAmelCase ( self :Optional[int] , SCREAMING_SNAKE_CASE :Tuple , SCREAMING_SNAKE_CASE :List[Any] ) -> Optional[Any]: '''simple docstring''' _a : Optional[int] =self.find(SCREAMING_SNAKE_CASE ) _a : Dict =self.find(SCREAMING_SNAKE_CASE ) if roota == roota: return roota if self.rank[roota] > self.rank[roota]: _a : Any =roota return roota if self.rank[roota] < self.rank[roota]: _a : List[str] =roota return roota if self.rank[roota] == self.rank[roota]: self.rank[roota] += 1 _a : List[Any] =roota return roota return None @staticmethod def __UpperCAmelCase ( SCREAMING_SNAKE_CASE :Dict ) -> Union[str, Any]: '''simple docstring''' _a : Any =graph.num_vertices _a : Union[str, Any] =Graph.UnionFind() _a : Optional[int] =[] while num_components > 1: _a : str ={} for vertex in graph.get_vertices(): _a : List[str] =-1 _a : Any =graph.get_edges() for edge in edges: _a , _a , _a : Tuple =edge edges.remove((tail, head, weight) ) for edge in edges: _a , _a , _a : Any =edge _a : Any =union_find.find(SCREAMING_SNAKE_CASE ) _a : List[Any] =union_find.find(SCREAMING_SNAKE_CASE ) if seta != seta: if cheap_edge[seta] == -1 or cheap_edge[seta][2] > weight: _a : Optional[int] =[head, tail, weight] if cheap_edge[seta] == -1 or cheap_edge[seta][2] > weight: _a : List[Any] =[head, tail, weight] for vertex in cheap_edge: if cheap_edge[vertex] != -1: _a , _a , _a : Optional[Any] =cheap_edge[vertex] if union_find.find(SCREAMING_SNAKE_CASE ) != union_find.find(SCREAMING_SNAKE_CASE ): union_find.union(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) mst_edges.append(cheap_edge[vertex] ) _a : str =num_components - 1 _a : str =Graph.build(edges=SCREAMING_SNAKE_CASE ) return mst
276
0
import copy from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging from ..auto import CONFIG_MAPPING _lowercase : Optional[int] = logging.get_logger(__name__) _lowercase : List[Any] = { '''microsoft/conditional-detr-resnet-50''': ( '''https://huggingface.co/microsoft/conditional-detr-resnet-50/resolve/main/config.json''' ), } class UpperCamelCase__( _a ): __magic_name__ : Optional[Any] = """conditional_detr""" __magic_name__ : str = ["""past_key_values"""] __magic_name__ : List[Any] = { """hidden_size""": """d_model""", """num_attention_heads""": """encoder_attention_heads""", } def __init__( self : List[Any] , lowerCAmelCase : Tuple=True , lowerCAmelCase : List[str]=None , lowerCAmelCase : str=3 , lowerCAmelCase : List[Any]=300 , lowerCAmelCase : Union[str, Any]=6 , lowerCAmelCase : Optional[int]=2048 , lowerCAmelCase : Tuple=8 , lowerCAmelCase : Dict=6 , lowerCAmelCase : Dict=2048 , lowerCAmelCase : Union[str, Any]=8 , lowerCAmelCase : Dict=0.0 , lowerCAmelCase : Tuple=0.0 , lowerCAmelCase : Optional[Any]=True , lowerCAmelCase : List[Any]="relu" , lowerCAmelCase : Dict=256 , lowerCAmelCase : Tuple=0.1 , lowerCAmelCase : Tuple=0.0 , lowerCAmelCase : Any=0.0 , lowerCAmelCase : List[Any]=0.02 , lowerCAmelCase : Any=1.0 , lowerCAmelCase : str=False , lowerCAmelCase : Dict="sine" , lowerCAmelCase : int="resnet50" , lowerCAmelCase : Any=True , lowerCAmelCase : Any=False , lowerCAmelCase : Dict=2 , lowerCAmelCase : Tuple=5 , lowerCAmelCase : Any=2 , lowerCAmelCase : List[str]=1 , lowerCAmelCase : Dict=1 , lowerCAmelCase : Dict=2 , lowerCAmelCase : Optional[int]=5 , lowerCAmelCase : int=2 , lowerCAmelCase : Optional[int]=0.25 , **lowerCAmelCase : Tuple , )-> Dict: """simple docstring""" if backbone_config is not None and use_timm_backbone: raise ValueError('''You can\'t specify both `backbone_config` and `use_timm_backbone`.''' ) if not use_timm_backbone: if backbone_config is None: logger.info('''`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone.''' ) UpperCAmelCase = CONFIG_MAPPING["""resnet"""](out_features=['''stage4'''] ) elif isinstance(__lowerCamelCase , __lowerCamelCase ): UpperCAmelCase = backbone_config.get('''model_type''' ) UpperCAmelCase = CONFIG_MAPPING[backbone_model_type] UpperCAmelCase = config_class.from_dict(__lowerCamelCase ) UpperCAmelCase = use_timm_backbone UpperCAmelCase = backbone_config UpperCAmelCase = num_channels UpperCAmelCase = num_queries UpperCAmelCase = d_model UpperCAmelCase = encoder_ffn_dim UpperCAmelCase = encoder_layers UpperCAmelCase = encoder_attention_heads UpperCAmelCase = decoder_ffn_dim UpperCAmelCase = decoder_layers UpperCAmelCase = decoder_attention_heads UpperCAmelCase = dropout UpperCAmelCase = attention_dropout UpperCAmelCase = activation_dropout UpperCAmelCase = activation_function UpperCAmelCase = init_std UpperCAmelCase = init_xavier_std UpperCAmelCase = encoder_layerdrop UpperCAmelCase = decoder_layerdrop UpperCAmelCase = encoder_layers UpperCAmelCase = auxiliary_loss UpperCAmelCase = position_embedding_type UpperCAmelCase = backbone UpperCAmelCase = use_pretrained_backbone UpperCAmelCase = dilation # Hungarian matcher UpperCAmelCase = class_cost UpperCAmelCase = bbox_cost UpperCAmelCase = giou_cost # Loss coefficients UpperCAmelCase = mask_loss_coefficient UpperCAmelCase = dice_loss_coefficient UpperCAmelCase = cls_loss_coefficient UpperCAmelCase = bbox_loss_coefficient UpperCAmelCase = giou_loss_coefficient UpperCAmelCase = focal_alpha super().__init__(is_encoder_decoder=__lowerCamelCase , **__lowerCamelCase ) @property def a__( self : Optional[Any] )-> str: """simple docstring""" return self.encoder_attention_heads @property def a__( self : Optional[int] )-> Tuple: """simple docstring""" return self.d_model def a__( self : str )-> str: """simple docstring""" UpperCAmelCase = copy.deepcopy(self.__dict__ ) if self.backbone_config is not None: UpperCAmelCase = self.backbone_config.to_dict() UpperCAmelCase = self.__class__.model_type return output class UpperCamelCase__( _a ): __magic_name__ : Optional[int] = version.parse("1.11" ) @property def a__( self : Optional[Any] )-> Tuple: """simple docstring""" return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ('''pixel_mask''', {0: '''batch'''}), ] ) @property def a__( self : Any )-> Optional[Any]: """simple docstring""" return 1E-5 @property def a__( self : Any )-> Optional[Any]: """simple docstring""" return 12
368
'''simple docstring''' import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, StableDiffusionSAGPipeline, UNetaDConditionModel, ) from diffusers.utils import slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class UpperCamelCase__( lowerCAmelCase , lowerCAmelCase , unittest.TestCase ): __magic_name__ : List[str] = StableDiffusionSAGPipeline __magic_name__ : str = TEXT_TO_IMAGE_PARAMS __magic_name__ : Any = TEXT_TO_IMAGE_BATCH_PARAMS __magic_name__ : Optional[int] = TEXT_TO_IMAGE_IMAGE_PARAMS __magic_name__ : List[Any] = TEXT_TO_IMAGE_IMAGE_PARAMS __magic_name__ : str = False def a__( self : Union[str, Any] )-> Tuple: """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.00085 , beta_end=0.012 , beta_schedule='''scaled_linear''' , clip_sample=lowerCAmelCase , set_alpha_to_one=lowerCAmelCase , ) torch.manual_seed(0 ) UpperCAmelCase = 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 ) 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(lowerCAmelCase ) 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 a__( self : Optional[Any] , lowerCAmelCase : str , lowerCAmelCase : Tuple=0 )-> str: """simple docstring""" if str(lowerCAmelCase ).startswith('''mps''' ): UpperCAmelCase = torch.manual_seed(lowerCAmelCase ) else: UpperCAmelCase = torch.Generator(device=lowerCAmelCase ).manual_seed(lowerCAmelCase ) UpperCAmelCase = { '''prompt''': '''.''', '''generator''': generator, '''num_inference_steps''': 2, '''guidance_scale''': 1.0, '''sag_scale''': 1.0, '''output_type''': '''numpy''', } return inputs def a__( self : Any )-> List[str]: """simple docstring""" super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) @slow @require_torch_gpu class UpperCamelCase__( unittest.TestCase ): def a__( self : Union[str, Any] )-> Any: """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def a__( self : Union[str, Any] )-> Tuple: """simple docstring""" UpperCAmelCase = StableDiffusionSAGPipeline.from_pretrained('''CompVis/stable-diffusion-v1-4''' ) UpperCAmelCase = sag_pipe.to(lowerCAmelCase ) sag_pipe.set_progress_bar_config(disable=lowerCAmelCase ) UpperCAmelCase = '''.''' UpperCAmelCase = torch.manual_seed(0 ) UpperCAmelCase = sag_pipe( [prompt] , generator=lowerCAmelCase , guidance_scale=7.5 , sag_scale=1.0 , num_inference_steps=20 , output_type='''np''' ) UpperCAmelCase = output.images UpperCAmelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) UpperCAmelCase = np.array([0.1568, 0.1738, 0.1695, 0.1693, 0.1507, 0.1705, 0.1547, 0.1751, 0.1949] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5E-2 def a__( self : int )-> Union[str, Any]: """simple docstring""" UpperCAmelCase = StableDiffusionSAGPipeline.from_pretrained('''stabilityai/stable-diffusion-2-1-base''' ) UpperCAmelCase = sag_pipe.to(lowerCAmelCase ) sag_pipe.set_progress_bar_config(disable=lowerCAmelCase ) UpperCAmelCase = '''.''' UpperCAmelCase = torch.manual_seed(0 ) UpperCAmelCase = sag_pipe( [prompt] , generator=lowerCAmelCase , guidance_scale=7.5 , sag_scale=1.0 , num_inference_steps=20 , output_type='''np''' ) UpperCAmelCase = output.images UpperCAmelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) UpperCAmelCase = np.array([0.3459, 0.2876, 0.2537, 0.3002, 0.2671, 0.2160, 0.3026, 0.2262, 0.2371] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5E-2 def a__( self : Optional[Any] )-> Union[str, Any]: """simple docstring""" UpperCAmelCase = StableDiffusionSAGPipeline.from_pretrained('''stabilityai/stable-diffusion-2-1-base''' ) UpperCAmelCase = sag_pipe.to(lowerCAmelCase ) sag_pipe.set_progress_bar_config(disable=lowerCAmelCase ) UpperCAmelCase = '''.''' UpperCAmelCase = torch.manual_seed(0 ) UpperCAmelCase = sag_pipe( [prompt] , width=768 , height=512 , generator=lowerCAmelCase , guidance_scale=7.5 , sag_scale=1.0 , num_inference_steps=20 , output_type='''np''' , ) UpperCAmelCase = output.images assert image.shape == (1, 512, 768, 3)
91
0
import json import pathlib import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision, slow from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import ConditionalDetrImageProcessor class UpperCAmelCase__ ( unittest.TestCase ): """simple docstring""" def __init__( self , A_ , A_=7 , A_=3 , A_=30 , A_=400 , A_=True , A_=None , A_=True , A_=[0.5, 0.5, 0.5] , A_=[0.5, 0.5, 0.5] , A_=True , A_=1 / 255 , A_=True , ) -> Optional[int]: # by setting size["longest_edge"] > max_resolution we're effectively not testing this :p __UpperCamelCase =size if size is not None else {'shortest_edge': 18, 'longest_edge': 1333} __UpperCamelCase =parent __UpperCamelCase =batch_size __UpperCamelCase =num_channels __UpperCamelCase =min_resolution __UpperCamelCase =max_resolution __UpperCamelCase =do_resize __UpperCamelCase =size __UpperCamelCase =do_normalize __UpperCamelCase =image_mean __UpperCamelCase =image_std __UpperCamelCase =do_rescale __UpperCamelCase =rescale_factor __UpperCamelCase =do_pad def _a ( self ) -> Union[str, Any]: return { "do_resize": self.do_resize, "size": self.size, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, "do_rescale": self.do_rescale, "rescale_factor": self.rescale_factor, "do_pad": self.do_pad, } def _a ( self , A_ , A_=False ) -> Optional[int]: if not batched: __UpperCamelCase =image_inputs[0] if isinstance(A_ , Image.Image ): __UpperCamelCase , __UpperCamelCase =image.size else: __UpperCamelCase , __UpperCamelCase =image.shape[1], image.shape[2] if w < h: __UpperCamelCase =int(self.size['shortest_edge'] * h / w ) __UpperCamelCase =self.size['shortest_edge'] elif w > h: __UpperCamelCase =self.size['shortest_edge'] __UpperCamelCase =int(self.size['shortest_edge'] * w / h ) else: __UpperCamelCase =self.size['shortest_edge'] __UpperCamelCase =self.size['shortest_edge'] else: __UpperCamelCase =[] for image in image_inputs: __UpperCamelCase , __UpperCamelCase =self.get_expected_values([image] ) expected_values.append((expected_height, expected_width) ) __UpperCamelCase =max(A_ , key=lambda A_ : item[0] )[0] __UpperCamelCase =max(A_ , key=lambda A_ : item[1] )[1] return expected_height, expected_width @require_torch @require_vision class UpperCAmelCase__ ( A_ , unittest.TestCase ): """simple docstring""" UpperCAmelCase__ : List[str] = ConditionalDetrImageProcessor if is_vision_available() else None def _a ( self ) -> Optional[Any]: __UpperCamelCase =ConditionalDetrImageProcessingTester(self ) @property def _a ( self ) -> Any: return self.image_processor_tester.prepare_image_processor_dict() def _a ( self ) -> Optional[Any]: __UpperCamelCase =self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(A_ , 'image_mean' ) ) self.assertTrue(hasattr(A_ , 'image_std' ) ) self.assertTrue(hasattr(A_ , 'do_normalize' ) ) self.assertTrue(hasattr(A_ , 'do_resize' ) ) self.assertTrue(hasattr(A_ , 'size' ) ) def _a ( self ) -> List[str]: __UpperCamelCase =self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'shortest_edge': 18, 'longest_edge': 1333} ) self.assertEqual(image_processor.do_pad , A_ ) __UpperCamelCase =self.image_processing_class.from_dict( self.image_processor_dict , size=42 , max_size=84 , pad_and_return_pixel_mask=A_ ) self.assertEqual(image_processor.size , {'shortest_edge': 42, 'longest_edge': 84} ) self.assertEqual(image_processor.do_pad , A_ ) def _a ( self ) -> Dict: pass def _a ( self ) -> List[Any]: # Initialize image_processing __UpperCamelCase =self.image_processing_class(**self.image_processor_dict ) # create random PIL images __UpperCamelCase =prepare_image_inputs(self.image_processor_tester , equal_resolution=A_ ) for image in image_inputs: self.assertIsInstance(A_ , Image.Image ) # Test not batched input __UpperCamelCase =image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values __UpperCamelCase , __UpperCamelCase =self.image_processor_tester.get_expected_values(A_ ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched __UpperCamelCase , __UpperCamelCase =self.image_processor_tester.get_expected_values(A_ , batched=A_ ) __UpperCamelCase =image_processing(A_ , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def _a ( self ) -> Any: # Initialize image_processing __UpperCamelCase =self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors __UpperCamelCase =prepare_image_inputs(self.image_processor_tester , equal_resolution=A_ , numpify=A_ ) for image in image_inputs: self.assertIsInstance(A_ , np.ndarray ) # Test not batched input __UpperCamelCase =image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values __UpperCamelCase , __UpperCamelCase =self.image_processor_tester.get_expected_values(A_ ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched __UpperCamelCase =image_processing(A_ , return_tensors='pt' ).pixel_values __UpperCamelCase , __UpperCamelCase =self.image_processor_tester.get_expected_values(A_ , batched=A_ ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def _a ( self ) -> Tuple: # Initialize image_processing __UpperCamelCase =self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors __UpperCamelCase =prepare_image_inputs(self.image_processor_tester , equal_resolution=A_ , torchify=A_ ) for image in image_inputs: self.assertIsInstance(A_ , torch.Tensor ) # Test not batched input __UpperCamelCase =image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values __UpperCamelCase , __UpperCamelCase =self.image_processor_tester.get_expected_values(A_ ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched __UpperCamelCase =image_processing(A_ , return_tensors='pt' ).pixel_values __UpperCamelCase , __UpperCamelCase =self.image_processor_tester.get_expected_values(A_ , batched=A_ ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) @slow def _a ( self ) -> Union[str, Any]: # prepare image and target __UpperCamelCase =Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) with open('./tests/fixtures/tests_samples/COCO/coco_annotations.txt' , 'r' ) as f: __UpperCamelCase =json.loads(f.read() ) __UpperCamelCase ={'image_id': 39769, 'annotations': target} # encode them __UpperCamelCase =ConditionalDetrImageProcessor.from_pretrained('microsoft/conditional-detr-resnet-50' ) __UpperCamelCase =image_processing(images=A_ , annotations=A_ , return_tensors='pt' ) # verify pixel values __UpperCamelCase =torch.Size([1, 3, 800, 1066] ) self.assertEqual(encoding['pixel_values'].shape , A_ ) __UpperCamelCase =torch.tensor([0.2796, 0.3138, 0.3481] ) self.assertTrue(torch.allclose(encoding['pixel_values'][0, 0, 0, :3] , A_ , atol=1E-4 ) ) # verify area __UpperCamelCase =torch.tensor([5887.9600, 1_1250.2061, 48_9353.8438, 83_7122.7500, 14_7967.5156, 16_5732.3438] ) self.assertTrue(torch.allclose(encoding['labels'][0]['area'] , A_ ) ) # verify boxes __UpperCamelCase =torch.Size([6, 4] ) self.assertEqual(encoding['labels'][0]['boxes'].shape , A_ ) __UpperCamelCase =torch.tensor([0.5503, 0.2765, 0.0604, 0.2215] ) self.assertTrue(torch.allclose(encoding['labels'][0]['boxes'][0] , A_ , atol=1E-3 ) ) # verify image_id __UpperCamelCase =torch.tensor([39769] ) self.assertTrue(torch.allclose(encoding['labels'][0]['image_id'] , A_ ) ) # verify is_crowd __UpperCamelCase =torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding['labels'][0]['iscrowd'] , A_ ) ) # verify class_labels __UpperCamelCase =torch.tensor([75, 75, 63, 65, 17, 17] ) self.assertTrue(torch.allclose(encoding['labels'][0]['class_labels'] , A_ ) ) # verify orig_size __UpperCamelCase =torch.tensor([480, 640] ) self.assertTrue(torch.allclose(encoding['labels'][0]['orig_size'] , A_ ) ) # verify size __UpperCamelCase =torch.tensor([800, 1066] ) self.assertTrue(torch.allclose(encoding['labels'][0]['size'] , A_ ) ) @slow def _a ( self ) -> Dict: # prepare image, target and masks_path __UpperCamelCase =Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) with open('./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt' , 'r' ) as f: __UpperCamelCase =json.loads(f.read() ) __UpperCamelCase ={'file_name': '000000039769.png', 'image_id': 39769, 'segments_info': target} __UpperCamelCase =pathlib.Path('./tests/fixtures/tests_samples/COCO/coco_panoptic' ) # encode them __UpperCamelCase =ConditionalDetrImageProcessor(format='coco_panoptic' ) __UpperCamelCase =image_processing(images=A_ , annotations=A_ , masks_path=A_ , return_tensors='pt' ) # verify pixel values __UpperCamelCase =torch.Size([1, 3, 800, 1066] ) self.assertEqual(encoding['pixel_values'].shape , A_ ) __UpperCamelCase =torch.tensor([0.2796, 0.3138, 0.3481] ) self.assertTrue(torch.allclose(encoding['pixel_values'][0, 0, 0, :3] , A_ , atol=1E-4 ) ) # verify area __UpperCamelCase =torch.tensor([14_7979.6875, 16_5527.0469, 48_4638.5938, 1_1292.9375, 5879.6562, 7634.1147] ) self.assertTrue(torch.allclose(encoding['labels'][0]['area'] , A_ ) ) # verify boxes __UpperCamelCase =torch.Size([6, 4] ) self.assertEqual(encoding['labels'][0]['boxes'].shape , A_ ) __UpperCamelCase =torch.tensor([0.2625, 0.5437, 0.4688, 0.8625] ) self.assertTrue(torch.allclose(encoding['labels'][0]['boxes'][0] , A_ , atol=1E-3 ) ) # verify image_id __UpperCamelCase =torch.tensor([39769] ) self.assertTrue(torch.allclose(encoding['labels'][0]['image_id'] , A_ ) ) # verify is_crowd __UpperCamelCase =torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding['labels'][0]['iscrowd'] , A_ ) ) # verify class_labels __UpperCamelCase =torch.tensor([17, 17, 63, 75, 75, 93] ) self.assertTrue(torch.allclose(encoding['labels'][0]['class_labels'] , A_ ) ) # verify masks __UpperCamelCase =822873 self.assertEqual(encoding['labels'][0]['masks'].sum().item() , A_ ) # verify orig_size __UpperCamelCase =torch.tensor([480, 640] ) self.assertTrue(torch.allclose(encoding['labels'][0]['orig_size'] , A_ ) ) # verify size __UpperCamelCase =torch.tensor([800, 1066] ) self.assertTrue(torch.allclose(encoding['labels'][0]['size'] , A_ ) )
62
import builtins import sys from ...utils.imports import _is_package_available from . import cursor, input from .helpers import Direction, clear_line, forceWrite, linebreak, move_cursor, reset_cursor, writeColor from .keymap import KEYMAP a_ = False try: a_ = _is_package_available('google.colab') except ModuleNotFoundError: pass @input.register class _lowercase : def __init__( self : Dict , snake_case : str = None , snake_case : list = [] ) -> Union[str, Any]: """simple docstring""" UpperCamelCase_ : Any = 0 UpperCamelCase_ : Optional[Any] = choices UpperCamelCase_ : Any = prompt if sys.platform == "win32": UpperCamelCase_ : Optional[Any] = '*' else: UpperCamelCase_ : str = '➔ ' def SCREAMING_SNAKE_CASE__ ( self : Optional[int] , snake_case : List[str] , snake_case : str = "" ) -> List[Any]: """simple docstring""" if sys.platform != "win32": writeColor(self.choices[index] , 3_2 , snake_case ) else: forceWrite(self.choices[index] , snake_case ) def SCREAMING_SNAKE_CASE__ ( self : List[str] , snake_case : int ) -> List[Any]: """simple docstring""" if index == self.position: forceWrite(f" {self.arrow_char} " ) self.write_choice(snake_case ) else: forceWrite(f" {self.choices[index]}" ) reset_cursor() def SCREAMING_SNAKE_CASE__ ( self : Any , snake_case : Direction , snake_case : int = 1 ) -> List[str]: """simple docstring""" UpperCamelCase_ : Any = self.position if direction == Direction.DOWN: if self.position + 1 >= len(self.choices ): return self.position += num_spaces else: if self.position - 1 < 0: return self.position -= num_spaces clear_line() self.print_choice(snake_case ) move_cursor(snake_case , direction.name ) self.print_choice(self.position ) @input.mark(KEYMAP['up'] ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ) -> List[Any]: """simple docstring""" self.move_direction(Direction.UP ) @input.mark(KEYMAP['down'] ) def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ) -> Optional[Any]: """simple docstring""" self.move_direction(Direction.DOWN ) @input.mark(KEYMAP['newline'] ) def SCREAMING_SNAKE_CASE__ ( self : str ) -> Optional[int]: """simple docstring""" move_cursor(len(self.choices ) - self.position , 'DOWN' ) return self.position @input.mark(KEYMAP['interrupt'] ) def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ) -> Optional[Any]: """simple docstring""" move_cursor(len(self.choices ) - self.position , 'DOWN' ) raise KeyboardInterrupt @input.mark_multiple(*[KEYMAP[str(snake_case )] for number in range(1_0 )] ) def SCREAMING_SNAKE_CASE__ ( self : int ) -> Dict: """simple docstring""" UpperCamelCase_ : List[Any] = int(chr(self.current_selection ) ) UpperCamelCase_ : Optional[int] = index - self.position if index == self.position: return if index < len(self.choices ): if self.position > index: self.move_direction(Direction.UP , -movement ) elif self.position < index: self.move_direction(Direction.DOWN , snake_case ) else: return else: return def SCREAMING_SNAKE_CASE__ ( self : List[Any] , snake_case : int = 0 ) -> Union[str, Any]: """simple docstring""" if self.prompt: linebreak() forceWrite(self.prompt , '\n' ) if in_colab: forceWrite('Please input a choice index (starting from 0), and press enter' , '\n' ) else: forceWrite('Please select a choice using the arrow or number keys, and selecting with enter' , '\n' ) UpperCamelCase_ : Optional[int] = default_choice for i in range(len(self.choices ) ): self.print_choice(snake_case ) forceWrite('\n' ) move_cursor(len(self.choices ) - self.position , 'UP' ) with cursor.hide(): while True: if in_colab: try: UpperCamelCase_ : Tuple = int(builtins.input() ) except ValueError: UpperCamelCase_ : Tuple = default_choice else: UpperCamelCase_ : Optional[Any] = self.handle_input() if choice is not None: reset_cursor() for _ in range(len(self.choices ) + 1 ): move_cursor(1 , 'UP' ) clear_line() self.write_choice(snake_case , '\n' ) return choice
175
0
import os import warnings from typing import List, Optional from ...tokenization_utils_base import BatchEncoding from ...utils import logging from .configuration_rag import RagConfig lowerCamelCase_ : List[Any] = logging.get_logger(__name__) class a__ : def __init__( self , UpperCAmelCase , UpperCAmelCase ) -> List[str]: __a = question_encoder __a = generator __a = self.question_encoder def __SCREAMING_SNAKE_CASE ( self , UpperCAmelCase ) -> Optional[int]: if os.path.isfile(UpperCAmelCase ): raise ValueError(f'''Provided path ({save_directory}) should be a directory, not a file''' ) os.makedirs(UpperCAmelCase , exist_ok=UpperCAmelCase ) __a = os.path.join(UpperCAmelCase , 'question_encoder_tokenizer' ) __a = os.path.join(UpperCAmelCase , 'generator_tokenizer' ) self.question_encoder.save_pretrained(UpperCAmelCase ) self.generator.save_pretrained(UpperCAmelCase ) @classmethod def __SCREAMING_SNAKE_CASE ( cls , UpperCAmelCase , **UpperCAmelCase ) -> Optional[int]: # dynamically import AutoTokenizer from ..auto.tokenization_auto import AutoTokenizer __a = kwargs.pop('config' , UpperCAmelCase ) if config is None: __a = RagConfig.from_pretrained(UpperCAmelCase ) __a = AutoTokenizer.from_pretrained( UpperCAmelCase , config=config.question_encoder , subfolder='question_encoder_tokenizer' ) __a = AutoTokenizer.from_pretrained( UpperCAmelCase , config=config.generator , subfolder='generator_tokenizer' ) return cls(question_encoder=UpperCAmelCase , generator=UpperCAmelCase ) def __call__( self , *UpperCAmelCase , **UpperCAmelCase ) -> Optional[int]: return self.current_tokenizer(*UpperCAmelCase , **UpperCAmelCase ) def __SCREAMING_SNAKE_CASE ( self , *UpperCAmelCase , **UpperCAmelCase ) -> Optional[int]: return self.generator.batch_decode(*UpperCAmelCase , **UpperCAmelCase ) def __SCREAMING_SNAKE_CASE ( self , *UpperCAmelCase , **UpperCAmelCase ) -> List[str]: return self.generator.decode(*UpperCAmelCase , **UpperCAmelCase ) def __SCREAMING_SNAKE_CASE ( self ) -> List[Any]: __a = self.question_encoder def __SCREAMING_SNAKE_CASE ( self ) -> Any: __a = self.generator def __SCREAMING_SNAKE_CASE ( self , UpperCAmelCase , UpperCAmelCase = None , UpperCAmelCase = None , UpperCAmelCase = None , UpperCAmelCase = "longest" , UpperCAmelCase = None , UpperCAmelCase = True , **UpperCAmelCase , ) -> BatchEncoding: warnings.warn( '`prepare_seq2seq_batch` is deprecated and will be removed in version 5 of 🤗 Transformers. Use the ' 'regular `__call__` method to prepare your inputs and the tokenizer under the `with_target_tokenizer` ' 'context manager to prepare your targets. See the documentation of your specific tokenizer for more ' 'details' , UpperCAmelCase , ) if max_length is None: __a = self.current_tokenizer.model_max_length __a = self( UpperCAmelCase , add_special_tokens=UpperCAmelCase , return_tensors=UpperCAmelCase , max_length=UpperCAmelCase , padding=UpperCAmelCase , truncation=UpperCAmelCase , **UpperCAmelCase , ) if tgt_texts is None: return model_inputs # Process tgt_texts if max_target_length is None: __a = self.current_tokenizer.model_max_length __a = self( text_target=UpperCAmelCase , add_special_tokens=UpperCAmelCase , return_tensors=UpperCAmelCase , padding=UpperCAmelCase , max_length=UpperCAmelCase , truncation=UpperCAmelCase , **UpperCAmelCase , ) __a = labels['input_ids'] return model_inputs
366
import tempfile import numpy as np import torch from transformers import AutoTokenizer, TaEncoderModel from diffusers import DDPMScheduler, UNetaDConditionModel from diffusers.models.attention_processor import AttnAddedKVProcessor from diffusers.pipelines.deepfloyd_if import IFWatermarker from diffusers.utils.testing_utils import torch_device from ..test_pipelines_common import to_np class a__ : def __SCREAMING_SNAKE_CASE ( self ) -> Optional[Any]: torch.manual_seed(0 ) __a = TaEncoderModel.from_pretrained('hf-internal-testing/tiny-random-t5' ) torch.manual_seed(0 ) __a = AutoTokenizer.from_pretrained('hf-internal-testing/tiny-random-t5' ) torch.manual_seed(0 ) __a = UNetaDConditionModel( sample_size=3_2 , layers_per_block=1 , block_out_channels=[3_2, 6_4] , down_block_types=[ 'ResnetDownsampleBlock2D', 'SimpleCrossAttnDownBlock2D', ] , mid_block_type='UNetMidBlock2DSimpleCrossAttn' , up_block_types=['SimpleCrossAttnUpBlock2D', 'ResnetUpsampleBlock2D'] , in_channels=3 , out_channels=6 , cross_attention_dim=3_2 , encoder_hid_dim=3_2 , attention_head_dim=8 , addition_embed_type='text' , addition_embed_type_num_heads=2 , cross_attention_norm='group_norm' , resnet_time_scale_shift='scale_shift' , act_fn='gelu' , ) unet.set_attn_processor(AttnAddedKVProcessor() ) # For reproducibility tests torch.manual_seed(0 ) __a = DDPMScheduler( num_train_timesteps=1_0_0_0 , beta_schedule='squaredcos_cap_v2' , beta_start=0.0_001 , beta_end=0.02 , thresholding=UpperCAmelCase , dynamic_thresholding_ratio=0.95 , sample_max_value=1.0 , prediction_type='epsilon' , variance_type='learned_range' , ) torch.manual_seed(0 ) __a = IFWatermarker() return { "text_encoder": text_encoder, "tokenizer": tokenizer, "unet": unet, "scheduler": scheduler, "watermarker": watermarker, "safety_checker": None, "feature_extractor": None, } def __SCREAMING_SNAKE_CASE ( self ) -> Optional[int]: torch.manual_seed(0 ) __a = TaEncoderModel.from_pretrained('hf-internal-testing/tiny-random-t5' ) torch.manual_seed(0 ) __a = AutoTokenizer.from_pretrained('hf-internal-testing/tiny-random-t5' ) torch.manual_seed(0 ) __a = UNetaDConditionModel( sample_size=3_2 , layers_per_block=[1, 2] , block_out_channels=[3_2, 6_4] , down_block_types=[ 'ResnetDownsampleBlock2D', 'SimpleCrossAttnDownBlock2D', ] , mid_block_type='UNetMidBlock2DSimpleCrossAttn' , up_block_types=['SimpleCrossAttnUpBlock2D', 'ResnetUpsampleBlock2D'] , in_channels=6 , out_channels=6 , cross_attention_dim=3_2 , encoder_hid_dim=3_2 , attention_head_dim=8 , addition_embed_type='text' , addition_embed_type_num_heads=2 , cross_attention_norm='group_norm' , resnet_time_scale_shift='scale_shift' , act_fn='gelu' , class_embed_type='timestep' , mid_block_scale_factor=1.414 , time_embedding_act_fn='gelu' , time_embedding_dim=3_2 , ) unet.set_attn_processor(AttnAddedKVProcessor() ) # For reproducibility tests torch.manual_seed(0 ) __a = DDPMScheduler( num_train_timesteps=1_0_0_0 , beta_schedule='squaredcos_cap_v2' , beta_start=0.0_001 , beta_end=0.02 , thresholding=UpperCAmelCase , dynamic_thresholding_ratio=0.95 , sample_max_value=1.0 , prediction_type='epsilon' , variance_type='learned_range' , ) torch.manual_seed(0 ) __a = DDPMScheduler( num_train_timesteps=1_0_0_0 , beta_schedule='squaredcos_cap_v2' , beta_start=0.0_001 , beta_end=0.02 , ) torch.manual_seed(0 ) __a = IFWatermarker() return { "text_encoder": text_encoder, "tokenizer": tokenizer, "unet": unet, "scheduler": scheduler, "image_noising_scheduler": image_noising_scheduler, "watermarker": watermarker, "safety_checker": None, "feature_extractor": None, } def __SCREAMING_SNAKE_CASE ( self ) -> str: __a = self.get_dummy_components() __a = self.pipeline_class(**UpperCAmelCase ) pipe.to(UpperCAmelCase ) pipe.set_progress_bar_config(disable=UpperCAmelCase ) __a = self.get_dummy_inputs(UpperCAmelCase ) __a = inputs['prompt'] __a = inputs['generator'] __a = inputs['num_inference_steps'] __a = inputs['output_type'] if "image" in inputs: __a = inputs['image'] else: __a = None if "mask_image" in inputs: __a = inputs['mask_image'] else: __a = None if "original_image" in inputs: __a = inputs['original_image'] else: __a = None __a , __a = pipe.encode_prompt(UpperCAmelCase ) # inputs with prompt converted to embeddings __a = { 'prompt_embeds': prompt_embeds, 'negative_prompt_embeds': negative_prompt_embeds, 'generator': generator, 'num_inference_steps': num_inference_steps, 'output_type': output_type, } if image is not None: __a = image if mask_image is not None: __a = mask_image if original_image is not None: __a = original_image # set all optional components to None for optional_component in pipe._optional_components: setattr(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) __a = pipe(**UpperCAmelCase )[0] with tempfile.TemporaryDirectory() as tmpdir: pipe.save_pretrained(UpperCAmelCase ) __a = self.pipeline_class.from_pretrained(UpperCAmelCase ) pipe_loaded.to(UpperCAmelCase ) pipe_loaded.set_progress_bar_config(disable=UpperCAmelCase ) pipe_loaded.unet.set_attn_processor(AttnAddedKVProcessor() ) # For reproducibility tests for optional_component in pipe._optional_components: self.assertTrue( getattr(UpperCAmelCase , UpperCAmelCase ) is None , f'''`{optional_component}` did not stay set to None after loading.''' , ) __a = self.get_dummy_inputs(UpperCAmelCase ) __a = inputs['generator'] __a = inputs['num_inference_steps'] __a = inputs['output_type'] # inputs with prompt converted to embeddings __a = { 'prompt_embeds': prompt_embeds, 'negative_prompt_embeds': negative_prompt_embeds, 'generator': generator, 'num_inference_steps': num_inference_steps, 'output_type': output_type, } if image is not None: __a = image if mask_image is not None: __a = mask_image if original_image is not None: __a = original_image __a = pipe_loaded(**UpperCAmelCase )[0] __a = np.abs(to_np(UpperCAmelCase ) - to_np(UpperCAmelCase ) ).max() self.assertLess(UpperCAmelCase , 1e-4 ) def __SCREAMING_SNAKE_CASE ( self ) -> int: __a = self.get_dummy_components() __a = self.pipeline_class(**UpperCAmelCase ) pipe.to(UpperCAmelCase ) pipe.set_progress_bar_config(disable=UpperCAmelCase ) __a = self.get_dummy_inputs(UpperCAmelCase ) __a = pipe(**UpperCAmelCase )[0] with tempfile.TemporaryDirectory() as tmpdir: pipe.save_pretrained(UpperCAmelCase ) __a = self.pipeline_class.from_pretrained(UpperCAmelCase ) pipe_loaded.to(UpperCAmelCase ) pipe_loaded.set_progress_bar_config(disable=UpperCAmelCase ) pipe_loaded.unet.set_attn_processor(AttnAddedKVProcessor() ) # For reproducibility tests __a = self.get_dummy_inputs(UpperCAmelCase ) __a = pipe_loaded(**UpperCAmelCase )[0] __a = np.abs(to_np(UpperCAmelCase ) - to_np(UpperCAmelCase ) ).max() self.assertLess(UpperCAmelCase , 1e-4 )
197
0
'''simple docstring''' from __future__ import annotations import typing from collections.abc import Iterable import numpy as np __snake_case =typing.Union[Iterable[float], Iterable[int], np.ndarray] # noqa: UP007 __snake_case =typing.Union[np.floataa, int, float] # noqa: UP007 def a_ ( lowerCamelCase : Vector , lowerCamelCase : Vector ): return np.sqrt(np.sum((np.asarray(lowerCamelCase_ ) - np.asarray(lowerCamelCase_ )) ** 2 ) ) def a_ ( lowerCamelCase : Vector , lowerCamelCase : Vector ): return sum((va - va) ** 2 for va, va in zip(lowerCamelCase_ , lowerCamelCase_ ) ) ** (1 / 2) if __name__ == "__main__": def a_ ( ): 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()
4
import pytest from datasets.parallel import ParallelBackendConfig, parallel_backend from datasets.utils.py_utils import map_nested from .utils import require_dill_gt_0_3_2, require_joblibspark, require_not_windows def lowerCAmelCase__ ( lowerCamelCase_ : Optional[Any]): # picklable for multiprocessing '''simple docstring''' return i + 1 @require_dill_gt_0_3_2 @require_joblibspark @require_not_windows def lowerCAmelCase__ ( ): '''simple docstring''' with parallel_backend('''spark'''): assert ParallelBackendConfig.backend_name == "spark" lowerCAmelCase__ : Any = [1, 2, 3] with pytest.raises(lowerCamelCase_): with parallel_backend('''unsupported backend'''): map_nested(lowerCamelCase_ ,lowerCamelCase_ ,num_proc=2) with pytest.raises(lowerCamelCase_): with parallel_backend('''unsupported backend'''): map_nested(lowerCamelCase_ ,lowerCamelCase_ ,num_proc=-1) @require_dill_gt_0_3_2 @require_joblibspark @require_not_windows @pytest.mark.parametrize('''num_proc''' ,[2, -1]) def lowerCAmelCase__ ( lowerCamelCase_ : List[Any]): '''simple docstring''' lowerCAmelCase__ : List[str] = [1, 2] lowerCAmelCase__ : Tuple = {'''a''': 1, '''b''': 2} lowerCAmelCase__ : Union[str, Any] = {'''a''': [1, 2], '''b''': [3, 4]} lowerCAmelCase__ : Any = {'''a''': {'''1''': 1}, '''b''': 2} lowerCAmelCase__ : Union[str, Any] = {'''a''': 1, '''b''': 2, '''c''': 3, '''d''': 4} lowerCAmelCase__ : int = [2, 3] lowerCAmelCase__ : List[str] = {'''a''': 2, '''b''': 3} lowerCAmelCase__ : str = {'''a''': [2, 3], '''b''': [4, 5]} lowerCAmelCase__ : List[str] = {'''a''': {'''1''': 2}, '''b''': 3} lowerCAmelCase__ : Optional[int] = {'''a''': 2, '''b''': 3, '''c''': 4, '''d''': 5} with parallel_backend('''spark'''): assert map_nested(lowerCamelCase_ ,lowerCamelCase_ ,num_proc=lowerCamelCase_) == expected_map_nested_sa assert map_nested(lowerCamelCase_ ,lowerCamelCase_ ,num_proc=lowerCamelCase_) == expected_map_nested_sa assert map_nested(lowerCamelCase_ ,lowerCamelCase_ ,num_proc=lowerCamelCase_) == expected_map_nested_sa assert map_nested(lowerCamelCase_ ,lowerCamelCase_ ,num_proc=lowerCamelCase_) == expected_map_nested_sa assert map_nested(lowerCamelCase_ ,lowerCamelCase_ ,num_proc=lowerCamelCase_) == expected_map_nested_sa
129
0
"""simple docstring""" import unittest import numpy as np import torch from diffusers import PNDMPipeline, PNDMScheduler, UNetaDModel from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device enable_full_determinism() class A_ (unittest.TestCase ): '''simple docstring''' @property def UpperCamelCase__ ( self ): """simple docstring""" torch.manual_seed(0 ) UpperCAmelCase_ : List[str] = UNetaDModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=3 , out_channels=3 , down_block_types=("DownBlock2D", "AttnDownBlock2D") , up_block_types=("AttnUpBlock2D", "UpBlock2D") , ) return model def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : Optional[int] = self.dummy_uncond_unet UpperCAmelCase_ : Union[str, Any] = PNDMScheduler() UpperCAmelCase_ : Any = PNDMPipeline(unet=lowercase_ , scheduler=lowercase_ ) pndm.to(lowercase_ ) pndm.set_progress_bar_config(disable=lowercase_ ) UpperCAmelCase_ : Dict = torch.manual_seed(0 ) UpperCAmelCase_ : List[Any] = pndm(generator=lowercase_ , num_inference_steps=20 , output_type="numpy" ).images UpperCAmelCase_ : Tuple = torch.manual_seed(0 ) UpperCAmelCase_ : Union[str, Any] = pndm(generator=lowercase_ , num_inference_steps=20 , output_type="numpy" , return_dict=lowercase_ )[0] UpperCAmelCase_ : Optional[int] = image[0, -3:, -3:, -1] UpperCAmelCase_ : Union[str, Any] = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) UpperCAmelCase_ : Tuple = np.array([1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1E-2 @slow @require_torch class A_ (unittest.TestCase ): '''simple docstring''' def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : Optional[int] = "google/ddpm-cifar10-32" UpperCAmelCase_ : Optional[Any] = UNetaDModel.from_pretrained(lowercase_ ) UpperCAmelCase_ : List[str] = PNDMScheduler() UpperCAmelCase_ : Optional[int] = PNDMPipeline(unet=lowercase_ , scheduler=lowercase_ ) pndm.to(lowercase_ ) pndm.set_progress_bar_config(disable=lowercase_ ) UpperCAmelCase_ : Optional[Any] = torch.manual_seed(0 ) UpperCAmelCase_ : Optional[int] = pndm(generator=lowercase_ , output_type="numpy" ).images UpperCAmelCase_ : str = image[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) UpperCAmelCase_ : Union[str, Any] = np.array([0.15_64, 0.1_46_45, 0.14_06, 0.1_47_15, 0.1_24_25, 0.1_40_45, 0.1_31_15, 0.1_21_75, 0.1_25] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2
351
"""simple docstring""" import argparse import logging import sys from unittest.mock import patch import run_glue_deebert from transformers.testing_utils import TestCasePlus, get_gpu_count, require_torch_non_multi_gpu, slow logging.basicConfig(level=logging.DEBUG) _a = logging.getLogger() def __a ( ): UpperCAmelCase_ : Tuple = argparse.ArgumentParser() parser.add_argument("-f" ) UpperCAmelCase_ : Dict = parser.parse_args() return args.f class A_ (lowercase__ ): '''simple docstring''' def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : Tuple = logging.StreamHandler(sys.stdout ) logger.addHandler(lowercase_ ) def UpperCamelCase__ ( self , lowercase_ ): """simple docstring""" UpperCAmelCase_ : Optional[int] = get_gpu_count() if n_gpu > 1: pass # XXX: doesn't quite work with n_gpu > 1 https://github.com/huggingface/transformers/issues/10560 # script = f"{self.examples_dir_str}/research_projects/deebert/run_glue_deebert.py" # distributed_args = f"-m torch.distributed.launch --nproc_per_node={n_gpu} {script}".split() # cmd = [sys.executable] + distributed_args + args # execute_subprocess_async(cmd, env=self.get_env()) # XXX: test the results - need to save them first into .json file else: args.insert(0 , "run_glue_deebert.py" ) with patch.object(lowercase_ , "argv" , lowercase_ ): UpperCAmelCase_ : List[str] = run_glue_deebert.main() for value in result.values(): self.assertGreaterEqual(lowercase_ , 0.6_66 ) @slow @require_torch_non_multi_gpu def UpperCamelCase__ ( self ): """simple docstring""" UpperCAmelCase_ : Optional[Any] = "\n --model_type roberta\n --model_name_or_path roberta-base\n --task_name MRPC\n --do_train\n --do_eval\n --do_lower_case\n --data_dir ./tests/fixtures/tests_samples/MRPC/\n --max_seq_length 128\n --per_gpu_eval_batch_size=1\n --per_gpu_train_batch_size=8\n --learning_rate 2e-4\n --num_train_epochs 3\n --overwrite_output_dir\n --seed 42\n --output_dir ./examples/deebert/saved_models/roberta-base/MRPC/two_stage\n --plot_data_dir ./examples/deebert/results/\n --save_steps 0\n --overwrite_cache\n --eval_after_first_stage\n ".split() self.run_and_check(lowercase_ ) UpperCAmelCase_ : Optional[Any] = "\n --model_type roberta\n --model_name_or_path ./examples/deebert/saved_models/roberta-base/MRPC/two_stage\n --task_name MRPC\n --do_eval\n --do_lower_case\n --data_dir ./tests/fixtures/tests_samples/MRPC/\n --output_dir ./examples/deebert/saved_models/roberta-base/MRPC/two_stage\n --plot_data_dir ./examples/deebert/results/\n --max_seq_length 128\n --eval_each_highway\n --eval_highway\n --overwrite_cache\n --per_gpu_eval_batch_size=1\n ".split() self.run_and_check(lowercase_ ) UpperCAmelCase_ : Dict = "\n --model_type roberta\n --model_name_or_path ./examples/deebert/saved_models/roberta-base/MRPC/two_stage\n --task_name MRPC\n --do_eval\n --do_lower_case\n --data_dir ./tests/fixtures/tests_samples/MRPC/\n --output_dir ./examples/deebert/saved_models/roberta-base/MRPC/two_stage\n --plot_data_dir ./examples/deebert/results/\n --max_seq_length 128\n --early_exit_entropy 0.1\n --eval_highway\n --overwrite_cache\n --per_gpu_eval_batch_size=1\n ".split() self.run_and_check(lowercase_ )
23
0
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) UpperCamelCase_ = { '''configuration_roformer''': ['''ROFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''RoFormerConfig''', '''RoFormerOnnxConfig'''], '''tokenization_roformer''': ['''RoFormerTokenizer'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase_ = ['''RoFormerTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase_ = [ '''ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''RoFormerForCausalLM''', '''RoFormerForMaskedLM''', '''RoFormerForMultipleChoice''', '''RoFormerForQuestionAnswering''', '''RoFormerForSequenceClassification''', '''RoFormerForTokenClassification''', '''RoFormerLayer''', '''RoFormerModel''', '''RoFormerPreTrainedModel''', '''load_tf_weights_in_roformer''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase_ = [ '''TF_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFRoFormerForCausalLM''', '''TFRoFormerForMaskedLM''', '''TFRoFormerForMultipleChoice''', '''TFRoFormerForQuestionAnswering''', '''TFRoFormerForSequenceClassification''', '''TFRoFormerForTokenClassification''', '''TFRoFormerLayer''', '''TFRoFormerModel''', '''TFRoFormerPreTrainedModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase_ = [ '''FLAX_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''FlaxRoFormerForMaskedLM''', '''FlaxRoFormerForMultipleChoice''', '''FlaxRoFormerForQuestionAnswering''', '''FlaxRoFormerForSequenceClassification''', '''FlaxRoFormerForTokenClassification''', '''FlaxRoFormerModel''', '''FlaxRoFormerPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_roformer import ROFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, RoFormerConfig, RoFormerOnnxConfig from .tokenization_roformer import RoFormerTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_roformer_fast import RoFormerTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_roformer import ( ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, RoFormerForCausalLM, RoFormerForMaskedLM, RoFormerForMultipleChoice, RoFormerForQuestionAnswering, RoFormerForSequenceClassification, RoFormerForTokenClassification, RoFormerLayer, RoFormerModel, RoFormerPreTrainedModel, load_tf_weights_in_roformer, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_roformer import ( TF_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TFRoFormerForCausalLM, TFRoFormerForMaskedLM, TFRoFormerForMultipleChoice, TFRoFormerForQuestionAnswering, TFRoFormerForSequenceClassification, TFRoFormerForTokenClassification, TFRoFormerLayer, TFRoFormerModel, TFRoFormerPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_roformer import ( FLAX_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, FlaxRoFormerForMaskedLM, FlaxRoFormerForMultipleChoice, FlaxRoFormerForQuestionAnswering, FlaxRoFormerForSequenceClassification, FlaxRoFormerForTokenClassification, FlaxRoFormerModel, FlaxRoFormerPreTrainedModel, ) else: import sys UpperCamelCase_ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
345
import json import os import unittest from transformers.models.ctrl.tokenization_ctrl import VOCAB_FILES_NAMES, CTRLTokenizer from ...test_tokenization_common import TokenizerTesterMixin class _snake_case ( __snake_case , unittest.TestCase ): '''simple docstring''' A__ : Optional[Any] = CTRLTokenizer A__ : Optional[Any] = False A__ : str = False def A__ ( self: Optional[int] ) -> List[Any]: super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt UpperCAmelCase_ : Dict = ["""adapt""", """re@@""", """a@@""", """apt""", """c@@""", """t""", """<unk>"""] UpperCAmelCase_ : Union[str, Any] = dict(zip(lowerCamelCase_ ,range(len(lowerCamelCase_ ) ) ) ) UpperCAmelCase_ : List[Any] = ["""#version: 0.2""", """a p""", """ap t</w>""", """r e""", """a d""", """ad apt</w>""", """"""] UpperCAmelCase_ : Optional[Any] = {"""unk_token""": """<unk>"""} UpperCAmelCase_ : Union[str, Any] = os.path.join(self.tmpdirname ,VOCAB_FILES_NAMES["""vocab_file"""] ) UpperCAmelCase_ : Optional[Any] = os.path.join(self.tmpdirname ,VOCAB_FILES_NAMES["""merges_file"""] ) with open(self.vocab_file ,"""w""" ,encoding="""utf-8""" ) as fp: fp.write(json.dumps(lowerCamelCase_ ) + """\n""" ) with open(self.merges_file ,"""w""" ,encoding="""utf-8""" ) as fp: fp.write("""\n""".join(lowerCamelCase_ ) ) def A__ ( self: Optional[int] ,**lowerCamelCase_: Any ) -> str: kwargs.update(self.special_tokens_map ) return CTRLTokenizer.from_pretrained(self.tmpdirname ,**lowerCamelCase_ ) def A__ ( self: int ,lowerCamelCase_: int ) -> str: UpperCAmelCase_ : List[str] = """adapt react readapt apt""" UpperCAmelCase_ : List[Any] = """adapt react readapt apt""" return input_text, output_text def A__ ( self: Union[str, Any] ) -> Optional[int]: UpperCAmelCase_ : Union[str, Any] = CTRLTokenizer(self.vocab_file ,self.merges_file ,**self.special_tokens_map ) UpperCAmelCase_ : List[Any] = """adapt react readapt apt""" UpperCAmelCase_ : Optional[int] = """adapt re@@ a@@ c@@ t re@@ adapt apt""".split() UpperCAmelCase_ : Tuple = tokenizer.tokenize(lowerCamelCase_ ) self.assertListEqual(lowerCamelCase_ ,lowerCamelCase_ ) UpperCAmelCase_ : Union[str, Any] = tokens + [tokenizer.unk_token] UpperCAmelCase_ : List[str] = [0, 1, 2, 4, 5, 1, 0, 3, 6] self.assertListEqual(tokenizer.convert_tokens_to_ids(lowerCamelCase_ ) ,lowerCamelCase_ )
345
1
'''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 UpperCamelCase_ ( A__ : Tuple ): '''simple docstring''' if hor == 1_28: lowerCAmelCase_ : Optional[Any] = ("""DownResnetBlock1D""", """DownResnetBlock1D""", """DownResnetBlock1D""") lowerCAmelCase_ : List[Any] = (32, 1_28, 2_56) lowerCAmelCase_ : int = ("""UpResnetBlock1D""", """UpResnetBlock1D""") elif hor == 32: lowerCAmelCase_ : str = ("""DownResnetBlock1D""", """DownResnetBlock1D""", """DownResnetBlock1D""", """DownResnetBlock1D""") lowerCAmelCase_ : List[Any] = (32, 64, 1_28, 2_56) lowerCAmelCase_ : Dict = ("""UpResnetBlock1D""", """UpResnetBlock1D""", """UpResnetBlock1D""") lowerCAmelCase_ : List[Any] = torch.load(f'/Users/bglickenhaus/Documents/diffuser/temporal_unet-hopper-mediumv2-hor{hor}.torch' ) lowerCAmelCase_ : Union[str, Any] = model.state_dict() lowerCAmelCase_ : List[Any] = { """down_block_types""": down_block_types, """block_out_channels""": block_out_channels, """up_block_types""": up_block_types, """layers_per_block""": 1, """use_timestep_embedding""": True, """out_block_type""": """OutConv1DBlock""", """norm_num_groups""": 8, """downsample_each_block""": False, """in_channels""": 14, """out_channels""": 14, """extra_in_channels""": 0, """time_embedding_type""": """positional""", """flip_sin_to_cos""": False, """freq_shift""": 1, """sample_size""": 6_55_36, """mid_block_type""": """MidResTemporalBlock1D""", """act_fn""": """mish""", } lowerCAmelCase_ : Dict = UNetaDModel(**A__ ) print(f'length of state dict: {len(state_dict.keys() )}' ) print(f'length of value function dict: {len(hf_value_function.state_dict().keys() )}' ) lowerCAmelCase_ : Optional[int] = dict(zip(model.state_dict().keys() , hf_value_function.state_dict().keys() ) ) for k, v in mapping.items(): lowerCAmelCase_ : Union[str, Any] = state_dict.pop(A__ ) hf_value_function.load_state_dict(A__ ) torch.save(hf_value_function.state_dict() , f'hub/hopper-medium-v2/unet/hor{hor}/diffusion_pytorch_model.bin' ) with open(f'hub/hopper-medium-v2/unet/hor{hor}/config.json' , """w""" ) as f: json.dump(A__ , A__ ) def UpperCamelCase_ ( ): '''simple docstring''' lowerCAmelCase_ : str = { """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, 1_28, 2_56), """layers_per_block""": 1, """downsample_each_block""": True, """sample_size""": 6_55_36, """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""", } lowerCAmelCase_ : Optional[int] = torch.load("""/Users/bglickenhaus/Documents/diffuser/value_function-hopper-mediumv2-hor32.torch""" ) lowerCAmelCase_ : Union[str, Any] = model lowerCAmelCase_ : List[str] = UNetaDModel(**A__ ) print(f'length of state dict: {len(state_dict.keys() )}' ) print(f'length of value function dict: {len(hf_value_function.state_dict().keys() )}' ) lowerCAmelCase_ : List[str] = dict(zip(state_dict.keys() , hf_value_function.state_dict().keys() ) ) for k, v in mapping.items(): lowerCAmelCase_ : List[Any] = state_dict.pop(A__ ) hf_value_function.load_state_dict(A__ ) torch.save(hf_value_function.state_dict() , """hub/hopper-medium-v2/value_function/diffusion_pytorch_model.bin""" ) with open("""hub/hopper-medium-v2/value_function/config.json""" , """w""" ) as f: json.dump(A__ , A__ ) if __name__ == "__main__": unet(32) # unet(128) value_function()
89
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, is_vision_available, ) __A : List[str] = { "configuration_owlvit": [ "OWLVIT_PRETRAINED_CONFIG_ARCHIVE_MAP", "OwlViTConfig", "OwlViTOnnxConfig", "OwlViTTextConfig", "OwlViTVisionConfig", ], "processing_owlvit": ["OwlViTProcessor"], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Optional[int] = ["OwlViTFeatureExtractor"] __A : str = ["OwlViTImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Tuple = [ "OWLVIT_PRETRAINED_MODEL_ARCHIVE_LIST", "OwlViTModel", "OwlViTPreTrainedModel", "OwlViTTextModel", "OwlViTVisionModel", "OwlViTForObjectDetection", ] if TYPE_CHECKING: from .configuration_owlvit import ( OWLVIT_PRETRAINED_CONFIG_ARCHIVE_MAP, OwlViTConfig, OwlViTOnnxConfig, OwlViTTextConfig, OwlViTVisionConfig, ) from .processing_owlvit import OwlViTProcessor try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_owlvit import OwlViTFeatureExtractor from .image_processing_owlvit import OwlViTImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_owlvit import ( OWLVIT_PRETRAINED_MODEL_ARCHIVE_LIST, OwlViTForObjectDetection, OwlViTModel, OwlViTPreTrainedModel, OwlViTTextModel, OwlViTVisionModel, ) else: import sys __A : List[Any] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
89
1
import argparse import math import os import torch from neural_compressor.utils.pytorch import load from PIL import Image from transformers import CLIPTextModel, CLIPTokenizer from diffusers import AutoencoderKL, StableDiffusionPipeline, UNetaDConditionModel def _UpperCAmelCase ( ): """simple docstring""" _lowerCAmelCase = argparse.ArgumentParser() parser.add_argument( """-m""" , """--pretrained_model_name_or_path""" , type=snake_case , default=snake_case , required=snake_case , help="""Path to pretrained model or model identifier from huggingface.co/models.""" , ) parser.add_argument( """-c""" , """--caption""" , type=snake_case , default="""robotic cat with wings""" , help="""Text used to generate images.""" , ) parser.add_argument( """-n""" , """--images_num""" , type=snake_case , default=4 , help="""How much images to generate.""" , ) parser.add_argument( """-s""" , """--seed""" , type=snake_case , default=42 , help="""Seed for random process.""" , ) parser.add_argument( """-ci""" , """--cuda_id""" , type=snake_case , default=0 , help="""cuda_id.""" , ) _lowerCAmelCase = parser.parse_args() return args def _UpperCAmelCase ( snake_case , snake_case , snake_case ): """simple docstring""" if not len(snake_case ) == rows * cols: raise ValueError("""The specified number of rows and columns are not correct.""" ) _lowerCAmelCase , _lowerCAmelCase = imgs[0].size _lowerCAmelCase = Image.new("""RGB""" , size=(cols * w, rows * h) ) _lowerCAmelCase , _lowerCAmelCase = grid.size for i, img in enumerate(snake_case ): grid.paste(snake_case , box=(i % cols * w, i // cols * h) ) return grid def _UpperCAmelCase ( snake_case , snake_case="robotic cat with wings" , snake_case=7.5 , snake_case=50 , snake_case=1 , snake_case=42 , ): """simple docstring""" _lowerCAmelCase = torch.Generator(pipeline.device ).manual_seed(snake_case ) _lowerCAmelCase = pipeline( snake_case , guidance_scale=snake_case , num_inference_steps=snake_case , generator=snake_case , num_images_per_prompt=snake_case , ).images _lowerCAmelCase = int(math.sqrt(snake_case ) ) _lowerCAmelCase = image_grid(snake_case , rows=_rows , cols=num_images_per_prompt // _rows ) return grid, images A__ = parse_args() # Load models and create wrapper for stable diffusion A__ = CLIPTokenizer.from_pretrained(args.pretrained_model_name_or_path, subfolder="""tokenizer""") A__ = CLIPTextModel.from_pretrained(args.pretrained_model_name_or_path, subfolder="""text_encoder""") A__ = AutoencoderKL.from_pretrained(args.pretrained_model_name_or_path, subfolder="""vae""") A__ = UNetaDConditionModel.from_pretrained(args.pretrained_model_name_or_path, subfolder="""unet""") A__ = StableDiffusionPipeline.from_pretrained( args.pretrained_model_name_or_path, text_encoder=text_encoder, vae=vae, unet=unet, tokenizer=tokenizer ) A__ = lambda images, clip_input: (images, False) if os.path.exists(os.path.join(args.pretrained_model_name_or_path, """best_model.pt""")): A__ = load(args.pretrained_model_name_or_path, model=unet) unet.eval() setattr(pipeline, """unet""", unet) else: A__ = unet.to(torch.device("""cuda""", args.cuda_id)) A__ = pipeline.to(unet.device) A__ , A__ = generate_images(pipeline, prompt=args.caption, num_images_per_prompt=args.images_num, seed=args.seed) grid.save(os.path.join(args.pretrained_model_name_or_path, """{}.png""".format("""_""".join(args.caption.split())))) A__ = os.path.join(args.pretrained_model_name_or_path, """_""".join(args.caption.split())) os.makedirs(dirname, exist_ok=True) for idx, image in enumerate(images): image.save(os.path.join(dirname, """{}.png""".format(idx + 1)))
82
A__ = [0, 2, 4, 6, 8] A__ = [1, 3, 5, 7, 9] def _UpperCAmelCase ( snake_case , snake_case , snake_case , snake_case ): """simple docstring""" if remaining_length == 0: if digits[0] == 0 or digits[-1] == 0: return 0 for i in range(length // 2 - 1 , -1 , -1 ): remainder += digits[i] + digits[length - i - 1] if remainder % 2 == 0: return 0 remainder //= 10 return 1 if remaining_length == 1: if remainder % 2 == 0: return 0 _lowerCAmelCase = 0 for digit in range(10 ): _lowerCAmelCase = digit result += reversible_numbers( 0 , (remainder + 2 * digit) // 10 , snake_case , snake_case ) return result _lowerCAmelCase = 0 for digita in range(10 ): _lowerCAmelCase = digita if (remainder + digita) % 2 == 0: _lowerCAmelCase = ODD_DIGITS else: _lowerCAmelCase = EVEN_DIGITS for digita in other_parity_digits: _lowerCAmelCase = digita result += reversible_numbers( remaining_length - 2 , (remainder + digita + digita) // 10 , snake_case , snake_case , ) return result def _UpperCAmelCase ( snake_case = 9 ): """simple docstring""" _lowerCAmelCase = 0 for length in range(1 , max_power + 1 ): result += reversible_numbers(snake_case , 0 , [0] * length , snake_case ) return result if __name__ == "__main__": print(f"{solution() = }")
82
1
import argparse import json import os import sys import tempfile import unittest from argparse import Namespace from dataclasses import dataclass, field from enum import Enum from pathlib import Path from typing import List, Literal, Optional import yaml from transformers import HfArgumentParser, TrainingArguments from transformers.hf_argparser import make_choice_type_function, string_to_bool # Since Python 3.10, we can use the builtin `|` operator for Union types # See PEP 604: https://peps.python.org/pep-0604 A_ : Tuple = sys.version_info >= (3, 10) def snake_case (UpperCAmelCase__=None , UpperCAmelCase__=None ) -> Tuple: return field(default_factory=lambda: default , metadata=SCREAMING_SNAKE_CASE__ ) @dataclass class _lowerCAmelCase: """simple docstring""" a : Optional[int] =42 a : List[str] =42 a : Optional[int] =42 a : Optional[Any] =42 @dataclass class _lowerCAmelCase: """simple docstring""" a : List[str] =42 a : Union[str, Any] =field(default='''toto''' , metadata={'''help''': '''help message'''} ) @dataclass class _lowerCAmelCase: """simple docstring""" a : Tuple =False a : Union[str, Any] =True a : str =None class _lowerCAmelCase( __SCREAMING_SNAKE_CASE ): """simple docstring""" a : List[Any] ='''titi''' a : Optional[Any] ='''toto''' class _lowerCAmelCase( __SCREAMING_SNAKE_CASE ): """simple docstring""" a : List[str] ='''titi''' a : Union[str, Any] ='''toto''' a : Any =42 @dataclass class _lowerCAmelCase: """simple docstring""" a : List[str] ='''toto''' def _a ( self ): UpperCamelCase_: Any = BasicEnum(self.foo ) @dataclass class _lowerCAmelCase: """simple docstring""" a : int ='''toto''' def _a ( self ): UpperCamelCase_: int = MixedTypeEnum(self.foo ) @dataclass class _lowerCAmelCase: """simple docstring""" a : List[str] =None a : Optional[Any] =field(default=__SCREAMING_SNAKE_CASE , metadata={'''help''': '''help message'''} ) a : Optional[Any] =None a : Optional[int] =list_field(default=[] ) a : Optional[int] =list_field(default=[] ) @dataclass class _lowerCAmelCase: """simple docstring""" a : Union[str, Any] =list_field(default=[] ) a : Any =list_field(default=[1, 2, 3] ) a : Optional[Any] =list_field(default=['''Hallo''', '''Bonjour''', '''Hello'''] ) a : List[str] =list_field(default=[0.1, 0.2, 0.3] ) @dataclass class _lowerCAmelCase: """simple docstring""" a : Optional[int] =field() a : str =field() a : List[Any] =field() def _a ( self ): UpperCamelCase_: List[Any] = BasicEnum(self.required_enum ) @dataclass class _lowerCAmelCase: """simple docstring""" a : List[Any] =42 a : Optional[int] =field() a : Any =None a : Dict =field(default='''toto''' , metadata={'''help''': '''help message'''} ) a : Tuple =list_field(default=['''Hallo''', '''Bonjour''', '''Hello'''] ) if is_python_no_less_than_3_10: @dataclass class _lowerCAmelCase: """simple docstring""" a : Any =False a : List[Any] =True a : Union[str, Any] =None @dataclass class _lowerCAmelCase: """simple docstring""" a : Optional[int] =None a : Union[str, Any] =field(default=__SCREAMING_SNAKE_CASE , metadata={'''help''': '''help message'''} ) a : Optional[int] =None a : int =list_field(default=[] ) a : List[Any] =list_field(default=[] ) class _lowerCAmelCase( unittest.TestCase ): """simple docstring""" def _a ( self , _lowerCamelCase , _lowerCamelCase ): self.assertEqual(len(a._actions ) , len(b._actions ) ) for x, y in zip(a._actions , b._actions ): UpperCamelCase_: List[Any] = {k: v for k, v in vars(UpperCamelCase__ ).items() if k != '''container'''} UpperCamelCase_: List[Any] = {k: v for k, v in vars(UpperCamelCase__ ).items() if k != '''container'''} # Choices with mixed type have custom function as "type" # So we need to compare results directly for equality if xx.get('choices' , UpperCamelCase__ ) and yy.get('choices' , UpperCamelCase__ ): for expected_choice in yy["choices"] + xx["choices"]: self.assertEqual(xx['type'](UpperCamelCase__ ) , yy['type'](UpperCamelCase__ ) ) del xx["type"], yy["type"] self.assertEqual(UpperCamelCase__ , UpperCamelCase__ ) def _a ( self ): UpperCamelCase_: Dict = HfArgumentParser(UpperCamelCase__ ) UpperCamelCase_: int = argparse.ArgumentParser() expected.add_argument('--foo' , type=UpperCamelCase__ , required=UpperCamelCase__ ) expected.add_argument('--bar' , type=UpperCamelCase__ , required=UpperCamelCase__ ) expected.add_argument('--baz' , type=UpperCamelCase__ , required=UpperCamelCase__ ) expected.add_argument('--flag' , type=UpperCamelCase__ , default=UpperCamelCase__ , const=UpperCamelCase__ , nargs='?' ) self.argparsersEqual(UpperCamelCase__ , UpperCamelCase__ ) UpperCamelCase_: Any = ['''--foo''', '''1''', '''--baz''', '''quux''', '''--bar''', '''0.5'''] (UpperCamelCase_): Tuple = parser.parse_args_into_dataclasses(UpperCamelCase__ , look_for_args_file=UpperCamelCase__ ) self.assertFalse(example.flag ) def _a ( self ): UpperCamelCase_: Optional[Any] = HfArgumentParser(UpperCamelCase__ ) UpperCamelCase_: Optional[int] = argparse.ArgumentParser() expected.add_argument('--foo' , default=4_2 , type=UpperCamelCase__ ) expected.add_argument('--baz' , default='toto' , type=UpperCamelCase__ , help='help message' ) self.argparsersEqual(UpperCamelCase__ , UpperCamelCase__ ) def _a ( self ): UpperCamelCase_: Any = argparse.ArgumentParser() expected.add_argument('--foo' , type=UpperCamelCase__ , default=UpperCamelCase__ , const=UpperCamelCase__ , nargs='?' ) expected.add_argument('--baz' , type=UpperCamelCase__ , default=UpperCamelCase__ , const=UpperCamelCase__ , nargs='?' ) # A boolean no_* argument always has to come after its "default: True" regular counter-part # and its default must be set to False expected.add_argument('--no_baz' , action='store_false' , default=UpperCamelCase__ , dest='baz' ) expected.add_argument('--opt' , type=UpperCamelCase__ , default=UpperCamelCase__ ) UpperCamelCase_: str = [WithDefaultBoolExample] if is_python_no_less_than_3_10: dataclass_types.append(UpperCamelCase__ ) for dataclass_type in dataclass_types: UpperCamelCase_: Tuple = HfArgumentParser(UpperCamelCase__ ) self.argparsersEqual(UpperCamelCase__ , UpperCamelCase__ ) UpperCamelCase_: Tuple = parser.parse_args([] ) self.assertEqual(UpperCamelCase__ , Namespace(foo=UpperCamelCase__ , baz=UpperCamelCase__ , opt=UpperCamelCase__ ) ) UpperCamelCase_: Any = parser.parse_args(['--foo', '--no_baz'] ) self.assertEqual(UpperCamelCase__ , Namespace(foo=UpperCamelCase__ , baz=UpperCamelCase__ , opt=UpperCamelCase__ ) ) UpperCamelCase_: Optional[Any] = parser.parse_args(['--foo', '--baz'] ) self.assertEqual(UpperCamelCase__ , Namespace(foo=UpperCamelCase__ , baz=UpperCamelCase__ , opt=UpperCamelCase__ ) ) UpperCamelCase_: int = parser.parse_args(['--foo', 'True', '--baz', 'True', '--opt', 'True'] ) self.assertEqual(UpperCamelCase__ , Namespace(foo=UpperCamelCase__ , baz=UpperCamelCase__ , opt=UpperCamelCase__ ) ) UpperCamelCase_: int = parser.parse_args(['--foo', 'False', '--baz', 'False', '--opt', 'False'] ) self.assertEqual(UpperCamelCase__ , Namespace(foo=UpperCamelCase__ , baz=UpperCamelCase__ , opt=UpperCamelCase__ ) ) def _a ( self ): UpperCamelCase_: Union[str, Any] = HfArgumentParser(UpperCamelCase__ ) UpperCamelCase_: Optional[Any] = argparse.ArgumentParser() expected.add_argument( '--foo' , default='toto' , choices=['titi', 'toto', 4_2] , type=make_choice_type_function(['titi', 'toto', 4_2] ) , ) self.argparsersEqual(UpperCamelCase__ , UpperCamelCase__ ) UpperCamelCase_: List[Any] = parser.parse_args([] ) self.assertEqual(args.foo , 'toto' ) UpperCamelCase_: Tuple = parser.parse_args_into_dataclasses([] )[0] self.assertEqual(enum_ex.foo , MixedTypeEnum.toto ) UpperCamelCase_: int = parser.parse_args(['--foo', 'titi'] ) self.assertEqual(args.foo , 'titi' ) UpperCamelCase_: Dict = parser.parse_args_into_dataclasses(['--foo', 'titi'] )[0] self.assertEqual(enum_ex.foo , MixedTypeEnum.titi ) UpperCamelCase_: Dict = parser.parse_args(['--foo', '42'] ) self.assertEqual(args.foo , 4_2 ) UpperCamelCase_: Union[str, Any] = parser.parse_args_into_dataclasses(['--foo', '42'] )[0] self.assertEqual(enum_ex.foo , MixedTypeEnum.fourtytwo ) def _a ( self ): @dataclass class _lowerCAmelCase: """simple docstring""" a : Dict ='''toto''' UpperCamelCase_: int = HfArgumentParser(UpperCamelCase__ ) UpperCamelCase_: Optional[int] = argparse.ArgumentParser() expected.add_argument( '--foo' , default='toto' , choices=('titi', 'toto', 4_2) , type=make_choice_type_function(['titi', 'toto', 4_2] ) , ) self.argparsersEqual(UpperCamelCase__ , UpperCamelCase__ ) UpperCamelCase_: Tuple = parser.parse_args([] ) self.assertEqual(args.foo , 'toto' ) UpperCamelCase_: List[Any] = parser.parse_args(['--foo', 'titi'] ) self.assertEqual(args.foo , 'titi' ) UpperCamelCase_: Tuple = parser.parse_args(['--foo', '42'] ) self.assertEqual(args.foo , 4_2 ) def _a ( self ): UpperCamelCase_: Union[str, Any] = HfArgumentParser(UpperCamelCase__ ) UpperCamelCase_: Any = argparse.ArgumentParser() expected.add_argument('--foo_int' , nargs='+' , default=[] , type=UpperCamelCase__ ) expected.add_argument('--bar_int' , nargs='+' , default=[1, 2, 3] , type=UpperCamelCase__ ) expected.add_argument('--foo_str' , nargs='+' , default=['Hallo', 'Bonjour', 'Hello'] , type=UpperCamelCase__ ) expected.add_argument('--foo_float' , nargs='+' , default=[0.1, 0.2, 0.3] , type=UpperCamelCase__ ) self.argparsersEqual(UpperCamelCase__ , UpperCamelCase__ ) UpperCamelCase_: str = parser.parse_args([] ) self.assertEqual( UpperCamelCase__ , Namespace(foo_int=[] , bar_int=[1, 2, 3] , foo_str=['Hallo', 'Bonjour', 'Hello'] , foo_float=[0.1, 0.2, 0.3] ) , ) UpperCamelCase_: Union[str, Any] = parser.parse_args('--foo_int 1 --bar_int 2 3 --foo_str a b c --foo_float 0.1 0.7'.split() ) self.assertEqual(UpperCamelCase__ , Namespace(foo_int=[1] , bar_int=[2, 3] , foo_str=['a', 'b', 'c'] , foo_float=[0.1, 0.7] ) ) def _a ( self ): UpperCamelCase_: Dict = argparse.ArgumentParser() expected.add_argument('--foo' , default=UpperCamelCase__ , type=UpperCamelCase__ ) expected.add_argument('--bar' , default=UpperCamelCase__ , type=UpperCamelCase__ , help='help message' ) expected.add_argument('--baz' , default=UpperCamelCase__ , type=UpperCamelCase__ ) expected.add_argument('--ces' , nargs='+' , default=[] , type=UpperCamelCase__ ) expected.add_argument('--des' , nargs='+' , default=[] , type=UpperCamelCase__ ) UpperCamelCase_: Any = [OptionalExample] if is_python_no_less_than_3_10: dataclass_types.append(UpperCamelCase__ ) for dataclass_type in dataclass_types: UpperCamelCase_: List[str] = HfArgumentParser(UpperCamelCase__ ) self.argparsersEqual(UpperCamelCase__ , UpperCamelCase__ ) UpperCamelCase_: Union[str, Any] = parser.parse_args([] ) self.assertEqual(UpperCamelCase__ , Namespace(foo=UpperCamelCase__ , bar=UpperCamelCase__ , baz=UpperCamelCase__ , ces=[] , des=[] ) ) UpperCamelCase_: Any = parser.parse_args('--foo 12 --bar 3.14 --baz 42 --ces a b c --des 1 2 3'.split() ) self.assertEqual(UpperCamelCase__ , Namespace(foo=1_2 , bar=3.1_4 , baz='42' , ces=['a', 'b', 'c'] , des=[1, 2, 3] ) ) def _a ( self ): UpperCamelCase_: int = HfArgumentParser(UpperCamelCase__ ) UpperCamelCase_: Dict = argparse.ArgumentParser() expected.add_argument('--required_list' , nargs='+' , type=UpperCamelCase__ , required=UpperCamelCase__ ) expected.add_argument('--required_str' , type=UpperCamelCase__ , required=UpperCamelCase__ ) expected.add_argument( '--required_enum' , type=make_choice_type_function(['titi', 'toto'] ) , choices=['titi', 'toto'] , required=UpperCamelCase__ , ) self.argparsersEqual(UpperCamelCase__ , UpperCamelCase__ ) def _a ( self ): UpperCamelCase_: Tuple = HfArgumentParser(UpperCamelCase__ ) UpperCamelCase_: Dict = argparse.ArgumentParser() expected.add_argument('--foo' , type=UpperCamelCase__ , required=UpperCamelCase__ ) expected.add_argument( '--required_enum' , type=make_choice_type_function(['titi', 'toto'] ) , choices=['titi', 'toto'] , required=UpperCamelCase__ , ) expected.add_argument('--opt' , type=UpperCamelCase__ , default=UpperCamelCase__ ) expected.add_argument('--baz' , default='toto' , type=UpperCamelCase__ , help='help message' ) expected.add_argument('--foo_str' , nargs='+' , default=['Hallo', 'Bonjour', 'Hello'] , type=UpperCamelCase__ ) self.argparsersEqual(UpperCamelCase__ , UpperCamelCase__ ) def _a ( self ): UpperCamelCase_: Dict = HfArgumentParser(UpperCamelCase__ ) UpperCamelCase_: Any = { '''foo''': 1_2, '''bar''': 3.1_4, '''baz''': '''42''', '''flag''': True, } UpperCamelCase_: Optional[int] = parser.parse_dict(UpperCamelCase__ )[0] UpperCamelCase_: List[str] = BasicExample(**UpperCamelCase__ ) self.assertEqual(UpperCamelCase__ , UpperCamelCase__ ) def _a ( self ): UpperCamelCase_: Dict = HfArgumentParser(UpperCamelCase__ ) UpperCamelCase_: Optional[Any] = { '''foo''': 1_2, '''bar''': 3.1_4, '''baz''': '''42''', '''flag''': True, '''extra''': 4_2, } self.assertRaises(UpperCamelCase__ , parser.parse_dict , UpperCamelCase__ , allow_extra_keys=UpperCamelCase__ ) def _a ( self ): UpperCamelCase_: str = HfArgumentParser(UpperCamelCase__ ) UpperCamelCase_: Dict = { '''foo''': 1_2, '''bar''': 3.1_4, '''baz''': '''42''', '''flag''': True, } with tempfile.TemporaryDirectory() as tmp_dir: UpperCamelCase_: str = os.path.join(UpperCamelCase__ , 'temp_json' ) os.mkdir(UpperCamelCase__ ) with open(temp_local_path + '.json' , 'w+' ) as f: json.dump(UpperCamelCase__ , UpperCamelCase__ ) UpperCamelCase_: Optional[Any] = parser.parse_yaml_file(Path(temp_local_path + '.json' ) )[0] UpperCamelCase_: Optional[int] = BasicExample(**UpperCamelCase__ ) self.assertEqual(UpperCamelCase__ , UpperCamelCase__ ) def _a ( self ): UpperCamelCase_: Tuple = HfArgumentParser(UpperCamelCase__ ) UpperCamelCase_: Tuple = { '''foo''': 1_2, '''bar''': 3.1_4, '''baz''': '''42''', '''flag''': True, } with tempfile.TemporaryDirectory() as tmp_dir: UpperCamelCase_: Any = os.path.join(UpperCamelCase__ , 'temp_yaml' ) os.mkdir(UpperCamelCase__ ) with open(temp_local_path + '.yaml' , 'w+' ) as f: yaml.dump(UpperCamelCase__ , UpperCamelCase__ ) UpperCamelCase_: List[str] = parser.parse_yaml_file(Path(temp_local_path + '.yaml' ) )[0] UpperCamelCase_: str = BasicExample(**UpperCamelCase__ ) self.assertEqual(UpperCamelCase__ , UpperCamelCase__ ) def _a ( self ): UpperCamelCase_: Union[str, Any] = HfArgumentParser(UpperCamelCase__ ) self.assertIsNotNone(UpperCamelCase__ )
367
def snake_case (UpperCAmelCase__ ) -> int: assert isinstance(UpperCAmelCase__ , UpperCAmelCase__ ), F'''The input value of [n={number}] is not an integer''' if number == 1: return 2 elif number < 1: UpperCamelCase_: List[Any] = F'''The input value of [n={number}] has to be > 0''' raise ValueError(UpperCAmelCase__ ) else: UpperCamelCase_: str = sylvester(number - 1 ) UpperCamelCase_: str = num - 1 UpperCamelCase_: Any = num return lower * upper + 1 if __name__ == "__main__": print(F'''The 8th number in Sylvester\'s sequence: {sylvester(8)}''')
292
0
'''simple docstring''' import doctest import logging import os import unittest from pathlib import Path from typing import List, Union import transformers from transformers.testing_utils import require_tf, require_torch, slow _lowerCamelCase : Dict = logging.getLogger() @unittest.skip("""Temporarily disable the doc tests.""" ) @require_torch @require_tf @slow class SCREAMING_SNAKE_CASE ( unittest.TestCase ): """simple docstring""" def A ( self : Any , UpperCamelCase__ : Path , UpperCamelCase__ : Union[str, None] = None , UpperCamelCase__ : Union[List[str], None] = None , UpperCamelCase__ : Union[str, List[str], None] = None , UpperCamelCase__ : bool = True , ): """simple docstring""" UpperCamelCase = [file for file in os.listdir(UpperCamelCase__ ) if os.path.isfile(os.path.join(UpperCamelCase__ , UpperCamelCase__ ) )] if identifier is not None: UpperCamelCase = [file for file in files if identifier in file] if n_identifier is not None: if isinstance(UpperCamelCase__ , UpperCamelCase__ ): for n_ in n_identifier: UpperCamelCase = [file for file in files if n_ not in file] else: UpperCamelCase = [file for file in files if n_identifier not in file] UpperCamelCase = ignore_files or [] ignore_files.append('__init__.py' ) UpperCamelCase = [file for file in files if file not in ignore_files] for file in files: # Open all files print('Testing' , UpperCamelCase__ ) if only_modules: UpperCamelCase = file.split('.' )[0] try: UpperCamelCase = getattr(UpperCamelCase__ , UpperCamelCase__ ) UpperCamelCase = doctest.DocTestSuite(UpperCamelCase__ ) UpperCamelCase = unittest.TextTestRunner().run(UpperCamelCase__ ) self.assertIs(len(result.failures ) , 0 ) except AttributeError: logger.info(f"""{module_identifier} is not a module.""" ) else: UpperCamelCase = doctest.testfile(str('..' / directory / file ) , optionflags=doctest.ELLIPSIS ) self.assertIs(result.failed , 0 ) def A ( self : Optional[int] ): """simple docstring""" UpperCamelCase = Path('src/transformers' ) UpperCamelCase = 'modeling' UpperCamelCase = [ 'modeling_ctrl.py', 'modeling_tf_ctrl.py', ] self.analyze_directory(UpperCamelCase__ , identifier=UpperCamelCase__ , ignore_files=UpperCamelCase__ ) def A ( self : Union[str, Any] ): """simple docstring""" UpperCamelCase = Path('src/transformers' ) UpperCamelCase = 'tokenization' self.analyze_directory(UpperCamelCase__ , identifier=UpperCamelCase__ ) def A ( self : str ): """simple docstring""" UpperCamelCase = Path('src/transformers' ) UpperCamelCase = 'configuration' self.analyze_directory(UpperCamelCase__ , identifier=UpperCamelCase__ ) def A ( self : Optional[int] ): """simple docstring""" UpperCamelCase = Path('src/transformers' ) UpperCamelCase = ['configuration', 'modeling', 'tokenization'] self.analyze_directory(UpperCamelCase__ , n_identifier=UpperCamelCase__ ) def A ( self : Tuple ): """simple docstring""" UpperCamelCase = Path('docs/source' ) UpperCamelCase = ['favicon.ico'] self.analyze_directory(UpperCamelCase__ , ignore_files=UpperCamelCase__ , only_modules=UpperCamelCase__ )
28
'''simple docstring''' from unittest.mock import Mock, patch from file_transfer.send_file import send_file @patch("""socket.socket""" ) @patch("""builtins.open""" ) def _UpperCAmelCase ( _lowerCamelCase : Dict , _lowerCamelCase : Optional[int] ) -> Union[str, Any]: # ===== initialization ===== _lowerCAmelCase : Tuple = Mock() _lowerCAmelCase : Any = conn, Mock() _lowerCAmelCase : Optional[Any] = iter([1, None] ) _lowerCAmelCase : str = lambda _lowerCamelCase : next(_lowerCamelCase ) # ===== invoke ===== send_file(filename="""mytext.txt""" , testing=_lowerCamelCase ) # ===== ensurance ===== sock.assert_called_once() sock.return_value.bind.assert_called_once() sock.return_value.listen.assert_called_once() sock.return_value.accept.assert_called_once() conn.recv.assert_called_once() file.return_value.__enter__.assert_called_once() file.return_value.__enter__.return_value.read.assert_called() conn.send.assert_called_once() conn.close.assert_called_once() sock.return_value.shutdown.assert_called_once() sock.return_value.close.assert_called_once()
309
0
def SCREAMING_SNAKE_CASE_ ( __A : list ) -> list: """simple docstring""" a_ : Optional[int] = len(_UpperCamelCase ) for _ in range(_UpperCamelCase ): for i in range(_ % 2 , arr_size - 1 , 2 ): if arr[i + 1] < arr[i]: a_ , a_ : Union[str, Any] = arr[i + 1], arr[i] return arr if __name__ == "__main__": UpperCAmelCase_ : List[str] = list(range(10, 0, -1)) print(F'Original: {arr}. Sorted: {odd_even_transposition(arr)}')
359
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tokenizers_available, is_torch_available, ) UpperCAmelCase_ : Optional[Any] = {'configuration_plbart': ['PLBART_PRETRAINED_CONFIG_ARCHIVE_MAP', 'PLBartConfig']} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase_ : Union[str, Any] = ['PLBartTokenizer'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase_ : List[str] = [ 'PLBART_PRETRAINED_MODEL_ARCHIVE_LIST', 'PLBartForCausalLM', 'PLBartForConditionalGeneration', 'PLBartForSequenceClassification', 'PLBartModel', 'PLBartPreTrainedModel', ] if TYPE_CHECKING: from .configuration_plbart import PLBART_PRETRAINED_CONFIG_ARCHIVE_MAP, PLBartConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_plbart import PLBartTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_plbart import ( PLBART_PRETRAINED_MODEL_ARCHIVE_LIST, PLBartForCausalLM, PLBartForConditionalGeneration, PLBartForSequenceClassification, PLBartModel, PLBartPreTrainedModel, ) else: import sys UpperCAmelCase_ : Union[str, Any] = _LazyModule(__name__, globals()['__file__'], _import_structure)
120
0
"""simple docstring""" from typing import List, Optional, Tuple from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_herbert import HerbertTokenizer lowerCamelCase__ = logging.get_logger(__name__) lowerCamelCase__ = {"""vocab_file""": """vocab.json""", """merges_file""": """merges.txt""", """tokenizer_file""": """tokenizer.json"""} lowerCamelCase__ = { """vocab_file""": { """allegro/herbert-base-cased""": """https://huggingface.co/allegro/herbert-base-cased/resolve/main/vocab.json""" }, """merges_file""": { """allegro/herbert-base-cased""": """https://huggingface.co/allegro/herbert-base-cased/resolve/main/merges.txt""" }, } lowerCamelCase__ = {"""allegro/herbert-base-cased""": 514} lowerCamelCase__ = {} class A__ ( _lowerCamelCase): A_ : Dict = VOCAB_FILES_NAMES A_ : str = PRETRAINED_VOCAB_FILES_MAP A_ : Dict = PRETRAINED_INIT_CONFIGURATION A_ : List[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES A_ : List[Any] = HerbertTokenizer def __init__( self , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE="<s>" , _SCREAMING_SNAKE_CASE="<unk>" , _SCREAMING_SNAKE_CASE="<pad>" , _SCREAMING_SNAKE_CASE="<mask>" , _SCREAMING_SNAKE_CASE="</s>" , **_SCREAMING_SNAKE_CASE , ): super().__init__( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , tokenizer_file=_SCREAMING_SNAKE_CASE , cls_token=_SCREAMING_SNAKE_CASE , unk_token=_SCREAMING_SNAKE_CASE , pad_token=_SCREAMING_SNAKE_CASE , mask_token=_SCREAMING_SNAKE_CASE , sep_token=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE , ) def __lowerCamelCase ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None ): __lowerCAmelCase : Union[str, Any] = [self.cls_token_id] __lowerCAmelCase : Dict = [self.sep_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def __lowerCamelCase ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = False ): if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=_SCREAMING_SNAKE_CASE , token_ids_a=_SCREAMING_SNAKE_CASE , already_has_special_tokens=_SCREAMING_SNAKE_CASE ) if token_ids_a is None: return [1] + ([0] * len(_SCREAMING_SNAKE_CASE )) + [1] return [1] + ([0] * len(_SCREAMING_SNAKE_CASE )) + [1] + ([0] * len(_SCREAMING_SNAKE_CASE )) + [1] def __lowerCamelCase ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None ): __lowerCAmelCase : Optional[int] = [self.sep_token_id] __lowerCAmelCase : List[Any] = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def __lowerCamelCase ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None ): __lowerCAmelCase : Union[str, Any] = self._tokenizer.model.save(_SCREAMING_SNAKE_CASE , name=_SCREAMING_SNAKE_CASE ) return tuple(_SCREAMING_SNAKE_CASE )
86
"""simple docstring""" from __future__ import annotations import os import tempfile import unittest from transformers import ConvBertConfig, is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TFConvBertForMaskedLM, TFConvBertForMultipleChoice, TFConvBertForQuestionAnswering, TFConvBertForSequenceClassification, TFConvBertForTokenClassification, TFConvBertModel, ) class A__ : def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=13 , _SCREAMING_SNAKE_CASE=7 , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=True , _SCREAMING_SNAKE_CASE=99 , _SCREAMING_SNAKE_CASE=32 , _SCREAMING_SNAKE_CASE=2 , _SCREAMING_SNAKE_CASE=4 , _SCREAMING_SNAKE_CASE=37 , _SCREAMING_SNAKE_CASE="gelu" , _SCREAMING_SNAKE_CASE=0.1 , _SCREAMING_SNAKE_CASE=0.1 , _SCREAMING_SNAKE_CASE=5_12 , _SCREAMING_SNAKE_CASE=16 , _SCREAMING_SNAKE_CASE=2 , _SCREAMING_SNAKE_CASE=0.02 , _SCREAMING_SNAKE_CASE=3 , _SCREAMING_SNAKE_CASE=4 , _SCREAMING_SNAKE_CASE=None , ): __lowerCAmelCase : Tuple = parent __lowerCAmelCase : Optional[int] = 13 __lowerCAmelCase : List[Any] = 7 __lowerCAmelCase : int = True __lowerCAmelCase : Optional[int] = True __lowerCAmelCase : List[Any] = True __lowerCAmelCase : Optional[int] = True __lowerCAmelCase : Optional[Any] = 99 __lowerCAmelCase : int = 3_84 __lowerCAmelCase : Union[str, Any] = 2 __lowerCAmelCase : Tuple = 4 __lowerCAmelCase : str = 37 __lowerCAmelCase : Any = 'gelu' __lowerCAmelCase : List[str] = 0.1 __lowerCAmelCase : Any = 0.1 __lowerCAmelCase : Union[str, Any] = 5_12 __lowerCAmelCase : int = 16 __lowerCAmelCase : Union[str, Any] = 2 __lowerCAmelCase : int = 0.02 __lowerCAmelCase : Dict = 3 __lowerCAmelCase : Tuple = 4 __lowerCAmelCase : Tuple = 1_28 __lowerCAmelCase : Optional[int] = 2 __lowerCAmelCase : List[str] = 9 __lowerCAmelCase : int = 1 __lowerCAmelCase : int = None def __lowerCamelCase ( self ): __lowerCAmelCase : Any = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __lowerCAmelCase : Optional[int] = None if self.use_input_mask: __lowerCAmelCase : str = random_attention_mask([self.batch_size, self.seq_length] ) __lowerCAmelCase : Tuple = None if self.use_token_type_ids: __lowerCAmelCase : Dict = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) __lowerCAmelCase : Optional[Any] = None __lowerCAmelCase : Dict = None __lowerCAmelCase : Union[str, Any] = None if self.use_labels: __lowerCAmelCase : List[Any] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) __lowerCAmelCase : Dict = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) __lowerCAmelCase : List[Any] = ids_tensor([self.batch_size] , self.num_choices ) __lowerCAmelCase : Union[str, Any] = ConvBertConfig( 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 , return_dict=_SCREAMING_SNAKE_CASE , ) return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def __lowerCamelCase ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): __lowerCAmelCase : Optional[int] = TFConvBertModel(config=_SCREAMING_SNAKE_CASE ) __lowerCAmelCase : Optional[int] = {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids} __lowerCAmelCase : Tuple = [input_ids, input_mask] __lowerCAmelCase : Any = model(_SCREAMING_SNAKE_CASE ) __lowerCAmelCase : Any = model(_SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __lowerCamelCase ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): __lowerCAmelCase : Any = TFConvBertForMaskedLM(config=_SCREAMING_SNAKE_CASE ) __lowerCAmelCase : Optional[int] = { 'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids, } __lowerCAmelCase : Optional[Any] = model(_SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def __lowerCamelCase ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): __lowerCAmelCase : Tuple = self.num_labels __lowerCAmelCase : Optional[Any] = TFConvBertForSequenceClassification(config=_SCREAMING_SNAKE_CASE ) __lowerCAmelCase : int = { 'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids, } __lowerCAmelCase : Union[str, Any] = model(_SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __lowerCamelCase ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): __lowerCAmelCase : int = self.num_choices __lowerCAmelCase : List[str] = TFConvBertForMultipleChoice(config=_SCREAMING_SNAKE_CASE ) __lowerCAmelCase : Dict = tf.tile(tf.expand_dims(_SCREAMING_SNAKE_CASE , 1 ) , (1, self.num_choices, 1) ) __lowerCAmelCase : Dict = tf.tile(tf.expand_dims(_SCREAMING_SNAKE_CASE , 1 ) , (1, self.num_choices, 1) ) __lowerCAmelCase : Union[str, Any] = tf.tile(tf.expand_dims(_SCREAMING_SNAKE_CASE , 1 ) , (1, self.num_choices, 1) ) __lowerCAmelCase : Tuple = { 'input_ids': multiple_choice_inputs_ids, 'attention_mask': multiple_choice_input_mask, 'token_type_ids': multiple_choice_token_type_ids, } __lowerCAmelCase : str = model(_SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def __lowerCamelCase ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): __lowerCAmelCase : str = self.num_labels __lowerCAmelCase : Any = TFConvBertForTokenClassification(config=_SCREAMING_SNAKE_CASE ) __lowerCAmelCase : Optional[int] = { 'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids, } __lowerCAmelCase : Union[str, Any] = model(_SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def __lowerCamelCase ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): __lowerCAmelCase : str = TFConvBertForQuestionAnswering(config=_SCREAMING_SNAKE_CASE ) __lowerCAmelCase : Union[str, Any] = { 'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids, } __lowerCAmelCase : Optional[int] = model(_SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def __lowerCamelCase ( self ): __lowerCAmelCase : Union[str, Any] = self.prepare_config_and_inputs() ( ( __lowerCAmelCase ) , ( __lowerCAmelCase ) , ( __lowerCAmelCase ) , ( __lowerCAmelCase ) , ( __lowerCAmelCase ) , ( __lowerCAmelCase ) , ( __lowerCAmelCase ) , ) : List[str] = config_and_inputs __lowerCAmelCase : Any = {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask} return config, inputs_dict @require_tf class A__ ( _lowerCamelCase , _lowerCamelCase , unittest.TestCase): A_ : List[str] = ( ( TFConvBertModel, TFConvBertForMaskedLM, TFConvBertForQuestionAnswering, TFConvBertForSequenceClassification, TFConvBertForTokenClassification, TFConvBertForMultipleChoice, ) if is_tf_available() else () ) A_ : str = ( { 'feature-extraction': TFConvBertModel, 'fill-mask': TFConvBertForMaskedLM, 'question-answering': TFConvBertForQuestionAnswering, 'text-classification': TFConvBertForSequenceClassification, 'token-classification': TFConvBertForTokenClassification, 'zero-shot': TFConvBertForSequenceClassification, } if is_tf_available() else {} ) A_ : List[Any] = False A_ : str = False A_ : List[Any] = False def __lowerCamelCase ( self ): __lowerCAmelCase : Dict = TFConvBertModelTester(self ) __lowerCAmelCase : Any = ConfigTester(self , config_class=_SCREAMING_SNAKE_CASE , hidden_size=37 ) def __lowerCamelCase ( self ): self.config_tester.run_common_tests() def __lowerCamelCase ( self ): __lowerCAmelCase : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE ) def __lowerCamelCase ( self ): __lowerCAmelCase : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*_SCREAMING_SNAKE_CASE ) def __lowerCamelCase ( self ): __lowerCAmelCase : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_multiple_choice(*_SCREAMING_SNAKE_CASE ) def __lowerCamelCase ( self ): __lowerCAmelCase : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*_SCREAMING_SNAKE_CASE ) def __lowerCamelCase ( self ): __lowerCAmelCase : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*_SCREAMING_SNAKE_CASE ) def __lowerCamelCase ( self ): __lowerCAmelCase : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*_SCREAMING_SNAKE_CASE ) @slow def __lowerCamelCase ( self ): __lowerCAmelCase , __lowerCAmelCase : Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() __lowerCAmelCase : Any = True __lowerCAmelCase : Dict = True if hasattr(_SCREAMING_SNAKE_CASE , 'use_cache' ): __lowerCAmelCase : int = True __lowerCAmelCase : List[str] = getattr(self.model_tester , 'encoder_seq_length' , self.model_tester.seq_length ) __lowerCAmelCase : str = getattr(self.model_tester , 'key_length' , _SCREAMING_SNAKE_CASE ) for model_class in self.all_model_classes: __lowerCAmelCase : str = self._prepare_for_class(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) __lowerCAmelCase : str = model_class(_SCREAMING_SNAKE_CASE ) __lowerCAmelCase : Optional[Any] = len(model(_SCREAMING_SNAKE_CASE ) ) with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(_SCREAMING_SNAKE_CASE , saved_model=_SCREAMING_SNAKE_CASE ) __lowerCAmelCase : List[Any] = os.path.join(_SCREAMING_SNAKE_CASE , 'saved_model' , '1' ) __lowerCAmelCase : int = tf.keras.models.load_model(_SCREAMING_SNAKE_CASE ) __lowerCAmelCase : Dict = model(_SCREAMING_SNAKE_CASE ) if self.is_encoder_decoder: __lowerCAmelCase : List[str] = outputs['encoder_hidden_states'] __lowerCAmelCase : Tuple = outputs['encoder_attentions'] else: __lowerCAmelCase : Optional[int] = outputs['hidden_states'] __lowerCAmelCase : Tuple = outputs['attentions'] self.assertEqual(len(_SCREAMING_SNAKE_CASE ) , _SCREAMING_SNAKE_CASE ) __lowerCAmelCase : Union[str, Any] = getattr( self.model_tester , 'expected_num_hidden_layers' , self.model_tester.num_hidden_layers + 1 ) self.assertEqual(len(_SCREAMING_SNAKE_CASE ) , _SCREAMING_SNAKE_CASE ) self.assertListEqual( list(output_hidden_states[0].shape[-2:] ) , [self.model_tester.seq_length, self.model_tester.hidden_size] , ) self.assertEqual(len(_SCREAMING_SNAKE_CASE ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(output_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length] , ) @slow def __lowerCamelCase ( self ): __lowerCAmelCase : Tuple = TFConvBertModel.from_pretrained('YituTech/conv-bert-base' ) self.assertIsNotNone(_SCREAMING_SNAKE_CASE ) def __lowerCamelCase ( self ): __lowerCAmelCase , __lowerCAmelCase : List[str] = self.model_tester.prepare_config_and_inputs_for_common() __lowerCAmelCase : Optional[Any] = True __lowerCAmelCase : List[Any] = getattr(self.model_tester , 'decoder_seq_length' , self.model_tester.seq_length ) __lowerCAmelCase : str = getattr(self.model_tester , 'encoder_seq_length' , self.model_tester.seq_length ) __lowerCAmelCase : Tuple = getattr(self.model_tester , 'key_length' , _SCREAMING_SNAKE_CASE ) __lowerCAmelCase : Optional[Any] = getattr(self.model_tester , 'key_length' , _SCREAMING_SNAKE_CASE ) def check_decoder_attentions_output(_SCREAMING_SNAKE_CASE ): __lowerCAmelCase : Union[str, Any] = len(_SCREAMING_SNAKE_CASE ) self.assertEqual(out_len % 2 , 0 ) __lowerCAmelCase : Optional[Any] = outputs.decoder_attentions self.assertEqual(len(_SCREAMING_SNAKE_CASE ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(decoder_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads / 2, decoder_seq_length, decoder_key_length] , ) def check_encoder_attentions_output(_SCREAMING_SNAKE_CASE ): __lowerCAmelCase : str = [ t.numpy() for t in (outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions) ] self.assertEqual(len(_SCREAMING_SNAKE_CASE ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length] , ) for model_class in self.all_model_classes: __lowerCAmelCase : List[str] = True __lowerCAmelCase : Optional[int] = False __lowerCAmelCase : List[Any] = model_class(_SCREAMING_SNAKE_CASE ) __lowerCAmelCase : Optional[int] = model(self._prepare_for_class(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) __lowerCAmelCase : Tuple = len(_SCREAMING_SNAKE_CASE ) self.assertEqual(config.output_hidden_states , _SCREAMING_SNAKE_CASE ) check_encoder_attentions_output(_SCREAMING_SNAKE_CASE ) if self.is_encoder_decoder: __lowerCAmelCase : Any = model_class(_SCREAMING_SNAKE_CASE ) __lowerCAmelCase : List[str] = model(self._prepare_for_class(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) self.assertEqual(config.output_hidden_states , _SCREAMING_SNAKE_CASE ) check_decoder_attentions_output(_SCREAMING_SNAKE_CASE ) # Check that output attentions can also be changed via the config del inputs_dict["output_attentions"] __lowerCAmelCase : Optional[Any] = True __lowerCAmelCase : str = model_class(_SCREAMING_SNAKE_CASE ) __lowerCAmelCase : Optional[int] = model(self._prepare_for_class(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) self.assertEqual(config.output_hidden_states , _SCREAMING_SNAKE_CASE ) check_encoder_attentions_output(_SCREAMING_SNAKE_CASE ) # Check attention is always last and order is fine __lowerCAmelCase : Dict = True __lowerCAmelCase : Optional[Any] = True __lowerCAmelCase : List[Any] = model_class(_SCREAMING_SNAKE_CASE ) __lowerCAmelCase : Dict = model(self._prepare_for_class(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) self.assertEqual(out_len + (2 if self.is_encoder_decoder else 1) , len(_SCREAMING_SNAKE_CASE ) ) self.assertEqual(model.config.output_hidden_states , _SCREAMING_SNAKE_CASE ) check_encoder_attentions_output(_SCREAMING_SNAKE_CASE ) @require_tf class A__ ( unittest.TestCase): @slow def __lowerCamelCase ( self ): __lowerCAmelCase : Dict = TFConvBertModel.from_pretrained('YituTech/conv-bert-base' ) __lowerCAmelCase : int = tf.constant([[0, 1, 2, 3, 4, 5]] ) __lowerCAmelCase : Tuple = model(_SCREAMING_SNAKE_CASE )[0] __lowerCAmelCase : Tuple = [1, 6, 7_68] self.assertEqual(output.shape , _SCREAMING_SNAKE_CASE ) __lowerCAmelCase : List[Any] = tf.constant( [ [ [-0.0347_5493, -0.468_6034, -0.3063_8832], [0.2263_7248, -0.2698_8646, -0.742_3424], [0.1032_4868, -0.4501_3508, -0.5828_0784], ] ] ) tf.debugging.assert_near(output[:, :3, :3] , _SCREAMING_SNAKE_CASE , atol=1E-4 )
86
1
from __future__ import annotations import math def _lowercase ( UpperCamelCase_ ) -> list[int]: '''simple docstring''' if num <= 0: SCREAMING_SNAKE_CASE__ = F'{num}: Invalid input, please enter a positive integer.' raise ValueError(UpperCamelCase_ ) SCREAMING_SNAKE_CASE__ = [True] * (num + 1) SCREAMING_SNAKE_CASE__ = [] SCREAMING_SNAKE_CASE__ = 2 SCREAMING_SNAKE_CASE__ = int(math.sqrt(UpperCamelCase_ ) ) while start <= end: # If start is a prime if sieve[start] is True: prime.append(UpperCamelCase_ ) # Set multiples of start be False for i in range(start * start , num + 1 , UpperCamelCase_ ): if sieve[i] is True: SCREAMING_SNAKE_CASE__ = False start += 1 for j in range(end + 1 , num + 1 ): if sieve[j] is True: prime.append(UpperCamelCase_ ) return prime if __name__ == "__main__": print(prime_sieve(int(input("""Enter a positive integer: """).strip())))
358
from __future__ import annotations from statistics import mean def _lowercase ( UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ ) -> list[int]: '''simple docstring''' SCREAMING_SNAKE_CASE__ = [0] * no_of_processes SCREAMING_SNAKE_CASE__ = [0] * no_of_processes # Initialize remaining_time to waiting_time. for i in range(UpperCamelCase_ ): SCREAMING_SNAKE_CASE__ = burst_time[i] SCREAMING_SNAKE_CASE__ = [] SCREAMING_SNAKE_CASE__ = 0 SCREAMING_SNAKE_CASE__ = 0 # When processes are not completed, # A process whose arrival time has passed \ # and has remaining execution time is put into the ready_process. # The shortest process in the ready_process, target_process is executed. while completed != no_of_processes: SCREAMING_SNAKE_CASE__ = [] SCREAMING_SNAKE_CASE__ = -1 for i in range(UpperCamelCase_ ): if (arrival_time[i] <= total_time) and (remaining_time[i] > 0): ready_process.append(UpperCamelCase_ ) if len(UpperCamelCase_ ) > 0: SCREAMING_SNAKE_CASE__ = ready_process[0] for i in ready_process: if remaining_time[i] < remaining_time[target_process]: SCREAMING_SNAKE_CASE__ = i total_time += burst_time[target_process] completed += 1 SCREAMING_SNAKE_CASE__ = 0 SCREAMING_SNAKE_CASE__ = ( total_time - arrival_time[target_process] - burst_time[target_process] ) else: total_time += 1 return waiting_time def _lowercase ( UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ ) -> list[int]: '''simple docstring''' SCREAMING_SNAKE_CASE__ = [0] * no_of_processes for i in range(UpperCamelCase_ ): SCREAMING_SNAKE_CASE__ = burst_time[i] + waiting_time[i] return turn_around_time if __name__ == "__main__": print("""[TEST CASE 01]""") __snake_case = 4 __snake_case = [2, 5, 3, 7] __snake_case = [0, 0, 0, 0] __snake_case = calculate_waitingtime(arrival_time, burst_time, no_of_processes) __snake_case = calculate_turnaroundtime( burst_time, no_of_processes, waiting_time ) # Printing the Result print("""PID\tBurst Time\tArrival Time\tWaiting Time\tTurnaround Time""") for i, process_id in enumerate(list(range(1, 5))): print( F"""{process_id}\t{burst_time[i]}\t\t\t{arrival_time[i]}\t\t\t\t""" F"""{waiting_time[i]}\t\t\t\t{turn_around_time[i]}""" ) print(F"""\nAverage waiting time = {mean(waiting_time):.5f}""") print(F"""Average turnaround time = {mean(turn_around_time):.5f}""")
169
0
"""simple docstring""" def lowercase_ ( _lowerCamelCase: float , _lowerCamelCase: float , _lowerCamelCase: int ) -> float: '''simple docstring''' if principal <= 0: raise Exception("Principal borrowed must be > 0" ) if rate_per_annum < 0: raise Exception("Rate of interest must be >= 0" ) if years_to_repay <= 0 or not isinstance(_UpperCamelCase , _UpperCamelCase ): raise Exception("Years to repay must be an integer > 0" ) # Yearly rate is divided by 12 to get monthly rate __lowerCamelCase : List[Any] = rate_per_annum / 12 # Years to repay is multiplied by 12 to get number of payments as payment is monthly __lowerCamelCase : Optional[int] = years_to_repay * 12 return ( principal * rate_per_month * (1 + rate_per_month) ** number_of_payments / ((1 + rate_per_month) ** number_of_payments - 1) ) if __name__ == "__main__": import doctest doctest.testmod()
135
"""simple docstring""" 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 rescale, resize, to_channel_dimension_format from ...image_utils import ( 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 UpperCAmelCase : Optional[Any] = logging.get_logger(__name__) def lowerCamelCase ( _UpperCamelCase : Any , _UpperCamelCase : Optional[Any] ) -> int: '''simple docstring''' __UpperCAmelCase : int = b.T __UpperCAmelCase : Union[str, Any] = np.sum(np.square(_UpperCamelCase ) , axis=1 ) __UpperCAmelCase : List[Any] = np.sum(np.square(_UpperCamelCase ) , axis=0 ) __UpperCAmelCase : int = np.matmul(_UpperCamelCase , _UpperCamelCase ) __UpperCAmelCase : Tuple = aa[:, None] - 2 * ab + ba[None, :] return d def lowerCamelCase ( _UpperCamelCase : Tuple , _UpperCamelCase : Optional[Any] ) -> Optional[int]: '''simple docstring''' __UpperCAmelCase : Tuple = x.reshape(-1 , 3 ) __UpperCAmelCase : Optional[Any] = squared_euclidean_distance(_UpperCamelCase , _UpperCamelCase ) return np.argmin(_UpperCamelCase , axis=1 ) class lowerCamelCase__ ( A ): """simple docstring""" __a = ["""pixel_values"""] def __init__( self : List[str] , UpperCamelCase : Optional[Union[List[List[int]], np.ndarray]] = None , UpperCamelCase : bool = True , UpperCamelCase : Dict[str, int] = None , UpperCamelCase : PILImageResampling = PILImageResampling.BILINEAR , UpperCamelCase : bool = True , UpperCamelCase : bool = True , **UpperCamelCase : int , ): '''simple docstring''' super().__init__(**UpperCamelCase ) __UpperCAmelCase : Optional[Any] = size if size is not None else {"""height""": 256, """width""": 256} __UpperCAmelCase : Tuple = get_size_dict(UpperCamelCase ) __UpperCAmelCase : Union[str, Any] = np.array(UpperCamelCase ) if clusters is not None else None __UpperCAmelCase : int = do_resize __UpperCAmelCase : List[str] = size __UpperCAmelCase : Dict = resample __UpperCAmelCase : int = do_normalize __UpperCAmelCase : List[Any] = do_color_quantize def lowerCamelCase__ ( self : List[str] , UpperCamelCase : np.ndarray , UpperCamelCase : Dict[str, int] , UpperCamelCase : PILImageResampling = PILImageResampling.BILINEAR , UpperCamelCase : Optional[Union[str, ChannelDimension]] = None , **UpperCamelCase : str , ): '''simple docstring''' __UpperCAmelCase : Any = get_size_dict(UpperCamelCase ) if "height" not in size or "width" not in size: raise ValueError(f'''Size dictionary must contain both height and width keys. Got {size.keys()}''' ) return resize( UpperCamelCase , size=(size["""height"""], size["""width"""]) , resample=UpperCamelCase , data_format=UpperCamelCase , **UpperCamelCase ) def lowerCamelCase__ ( self : Dict , UpperCamelCase : np.ndarray , UpperCamelCase : Optional[Union[str, ChannelDimension]] = None , ): '''simple docstring''' __UpperCAmelCase : Optional[Any] = rescale(image=UpperCamelCase , scale=1 / 127.5 , data_format=UpperCamelCase ) __UpperCAmelCase : Optional[Any] = image - 1 return image def lowerCamelCase__ ( self : Dict , UpperCamelCase : ImageInput , UpperCamelCase : bool = None , UpperCamelCase : Dict[str, int] = None , UpperCamelCase : PILImageResampling = None , UpperCamelCase : bool = None , UpperCamelCase : Optional[bool] = None , UpperCamelCase : Optional[Union[List[List[int]], np.ndarray]] = None , UpperCamelCase : Optional[Union[str, TensorType]] = None , UpperCamelCase : Optional[Union[str, ChannelDimension]] = ChannelDimension.FIRST , **UpperCamelCase : Optional[int] , ): '''simple docstring''' __UpperCAmelCase : List[str] = do_resize if do_resize is not None else self.do_resize __UpperCAmelCase : Any = size if size is not None else self.size __UpperCAmelCase : Any = get_size_dict(UpperCamelCase ) __UpperCAmelCase : Optional[Any] = resample if resample is not None else self.resample __UpperCAmelCase : Any = do_normalize if do_normalize is not None else self.do_normalize __UpperCAmelCase : Any = do_color_quantize if do_color_quantize is not None else self.do_color_quantize __UpperCAmelCase : Tuple = clusters if clusters is not None else self.clusters __UpperCAmelCase : Tuple = np.array(UpperCamelCase ) __UpperCAmelCase : int = make_list_of_images(UpperCamelCase ) if not valid_images(UpperCamelCase ): 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_color_quantize and clusters is None: raise ValueError("""Clusters must be specified if do_color_quantize is True.""" ) # All transformations expect numpy arrays. __UpperCAmelCase : Tuple = [to_numpy_array(UpperCamelCase ) for image in images] if do_resize: __UpperCAmelCase : Dict = [self.resize(image=UpperCamelCase , size=UpperCamelCase , resample=UpperCamelCase ) for image in images] if do_normalize: __UpperCAmelCase : Any = [self.normalize(image=UpperCamelCase ) for image in images] if do_color_quantize: __UpperCAmelCase : str = [to_channel_dimension_format(UpperCamelCase , ChannelDimension.LAST ) for image in images] # color quantize from (batch_size, height, width, 3) to (batch_size, height, width) __UpperCAmelCase : Optional[int] = np.array(UpperCamelCase ) __UpperCAmelCase : Any = color_quantize(UpperCamelCase , UpperCamelCase ).reshape(images.shape[:-1] ) # flatten to (batch_size, height*width) __UpperCAmelCase : Dict = images.shape[0] __UpperCAmelCase : Optional[Any] = images.reshape(UpperCamelCase , -1 ) # We need to convert back to a list of images to keep consistent behaviour across processors. __UpperCAmelCase : int = list(UpperCamelCase ) else: __UpperCAmelCase : List[Any] = [to_channel_dimension_format(UpperCamelCase , UpperCamelCase ) for image in images] __UpperCAmelCase : List[str] = {"""input_ids""": images} return BatchFeature(data=UpperCamelCase , tensor_type=UpperCamelCase )
115
0
'''simple docstring''' from typing import List, Optional, Union import numpy as np import PIL.Image from ...image_processing_utils import BaseImageProcessor, BatchFeature from ...image_transforms import rescale, resize, to_channel_dimension_format from ...image_utils import ( ChannelDimension, PILImageResampling, get_image_size, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, logging UpperCAmelCase = logging.get_logger(__name__) class __snake_case( lowerCamelCase_ ): '''simple docstring''' UpperCAmelCase : int = ["""pixel_values"""] def __init__( self , A_ = True , A_ = 32 , A_=PILImageResampling.BILINEAR , A_ = True , **A_ , ) -> None: lowerCAmelCase = do_resize lowerCAmelCase = do_rescale lowerCAmelCase = size_divisor lowerCAmelCase = resample super().__init__(**lowerCAmelCase__ ) def __snake_case ( self , A_ , A_ , A_ , A_ = None , **A_ ) -> np.ndarray: lowerCAmelCase, lowerCAmelCase = get_image_size(lowerCAmelCase__ ) # Rounds the height and width down to the closest multiple of size_divisor lowerCAmelCase = height // size_divisor * size_divisor lowerCAmelCase = width // size_divisor * size_divisor lowerCAmelCase = resize(lowerCAmelCase__ , (new_h, new_w) , resample=lowerCAmelCase__ , data_format=lowerCAmelCase__ , **lowerCAmelCase__ ) return image def __snake_case ( self , A_ , A_ , A_ = None , **A_ ) -> np.ndarray: return rescale(image=lowerCAmelCase__ , scale=lowerCAmelCase__ , data_format=lowerCAmelCase__ , **lowerCAmelCase__ ) def __snake_case ( self , A_ , A_ = None , A_ = None , A_=None , A_ = None , A_ = None , A_ = ChannelDimension.FIRST , **A_ , ) -> BatchFeature: lowerCAmelCase = do_resize if do_resize is not None else self.do_resize lowerCAmelCase = do_rescale if do_rescale is not None else self.do_rescale lowerCAmelCase = size_divisor if size_divisor is not None else self.size_divisor lowerCAmelCase = resample if resample is not None else self.resample if do_resize and size_divisor is None: raise ValueError("""size_divisor is required for resizing""" ) lowerCAmelCase = make_list_of_images(lowerCAmelCase__ ) if not valid_images(lowerCAmelCase__ ): raise ValueError("""Invalid image(s)""" ) # All transformations expect numpy arrays. lowerCAmelCase = [to_numpy_array(lowerCAmelCase__ ) for img in images] if do_resize: lowerCAmelCase = [self.resize(lowerCAmelCase__ , size_divisor=lowerCAmelCase__ , resample=lowerCAmelCase__ ) for image in images] if do_rescale: lowerCAmelCase = [self.rescale(lowerCAmelCase__ , scale=1 / 255 ) for image in images] lowerCAmelCase = [to_channel_dimension_format(lowerCAmelCase__ , lowerCAmelCase__ ) for image in images] lowerCAmelCase = {"""pixel_values""": images} return BatchFeature(data=lowerCAmelCase__ , tensor_type=lowerCAmelCase__ )
366
'''simple docstring''' def _snake_case ( _SCREAMING_SNAKE_CASE : list ) -> list: """simple docstring""" if len(_SCREAMING_SNAKE_CASE ) < 2: return collection def circle_sort_util(_SCREAMING_SNAKE_CASE : list , _SCREAMING_SNAKE_CASE : int , _SCREAMING_SNAKE_CASE : int ) -> bool: lowerCAmelCase = False if low == high: return swapped lowerCAmelCase = low lowerCAmelCase = high while left < right: if collection[left] > collection[right]: lowerCAmelCase, lowerCAmelCase = ( collection[right], collection[left], ) lowerCAmelCase = True left += 1 right -= 1 if left == right and collection[left] > collection[right + 1]: lowerCAmelCase, lowerCAmelCase = ( collection[right + 1], collection[left], ) lowerCAmelCase = True lowerCAmelCase = low + int((high - low) / 2 ) lowerCAmelCase = circle_sort_util(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) lowerCAmelCase = circle_sort_util(_SCREAMING_SNAKE_CASE , mid + 1 , _SCREAMING_SNAKE_CASE ) return swapped or left_swap or right_swap lowerCAmelCase = True while is_not_sorted is True: lowerCAmelCase = circle_sort_util(_SCREAMING_SNAKE_CASE , 0 , len(_SCREAMING_SNAKE_CASE ) - 1 ) return collection if __name__ == "__main__": UpperCAmelCase = input('Enter numbers separated by a comma:\n').strip() UpperCAmelCase = [int(item) for item in user_input.split(',')] print(circle_sort(unsorted))
187
0
"""simple docstring""" import dataclasses import json import sys import types from argparse import ArgumentDefaultsHelpFormatter, ArgumentParser, ArgumentTypeError from copy import copy from enum import Enum from inspect import isclass from pathlib import Path from typing import Any, Callable, Dict, Iterable, List, Literal, NewType, Optional, Tuple, Union, get_type_hints import yaml snake_case__ : List[str] = NewType('''DataClass''', Any) snake_case__ : Optional[Any] = NewType('''DataClassType''', Any) def _snake_case ( _snake_case : Dict ): if isinstance(_snake_case , _snake_case ): return v if v.lower() in ("yes", "true", "t", "y", "1"): return True elif v.lower() in ("no", "false", "f", "n", "0"): return False else: raise ArgumentTypeError( f'''Truthy value expected: got {v} but expected one of yes/no, true/false, t/f, y/n, 1/0 (case insensitive).''' ) def _snake_case ( _snake_case : list ): lowerCAmelCase : Dict = {str(_snake_case ): choice for choice in choices} return lambda _snake_case : str_to_choice.get(_snake_case , _snake_case ) def _snake_case ( *, _snake_case : Union[str, List[str]] = None , _snake_case : str = None , _snake_case : Any = dataclasses.MISSING , _snake_case : Callable[[], Any] = dataclasses.MISSING , _snake_case : dict = None , **_snake_case : Any , ): if metadata is None: # Important, don't use as default param in function signature because dict is mutable and shared across function calls lowerCAmelCase : Optional[int] = {} if aliases is not None: lowerCAmelCase : Optional[Any] = aliases if help is not None: lowerCAmelCase : Optional[Any] = help return dataclasses.field(metadata=_snake_case , default=_snake_case , default_factory=_snake_case , **_snake_case ) class snake_case_( a__ ): __UpperCamelCase = 42 def __init__( self : str , UpperCamelCase_ : Union[DataClassType, Iterable[DataClassType]] , **UpperCamelCase_ : List[Any] ): # To make the default appear when using --help if "formatter_class" not in kwargs: lowerCAmelCase : Union[str, Any] = ArgumentDefaultsHelpFormatter super().__init__(**UpperCamelCase_ ) if dataclasses.is_dataclass(UpperCamelCase_ ): lowerCAmelCase : Optional[int] = [dataclass_types] lowerCAmelCase : Optional[Any] = list(UpperCamelCase_ ) for dtype in self.dataclass_types: self._add_dataclass_arguments(UpperCamelCase_ ) @staticmethod def lowerCamelCase__ ( UpperCamelCase_ : ArgumentParser , UpperCamelCase_ : dataclasses.Field ): lowerCAmelCase : Optional[int] = F'''--{field.name}''' lowerCAmelCase : Tuple = field.metadata.copy() # field.metadata is not used at all by Data Classes, # it is provided as a third-party extension mechanism. if isinstance(field.type , UpperCamelCase_ ): raise RuntimeError( '''Unresolved type detected, which should have been done with the help of ''' '''`typing.get_type_hints` method by default''' ) lowerCAmelCase : List[str] = kwargs.pop('''aliases''' , [] ) if isinstance(UpperCamelCase_ , UpperCamelCase_ ): lowerCAmelCase : Dict = [aliases] lowerCAmelCase : Tuple = getattr(field.type , '''__origin__''' , field.type ) if origin_type is Union or (hasattr(UpperCamelCase_ , '''UnionType''' ) and isinstance(UpperCamelCase_ , types.UnionType )): if str not in field.type.__args__ and ( len(field.type.__args__ ) != 2 or type(UpperCamelCase_ ) not in field.type.__args__ ): raise ValueError( '''Only `Union[X, NoneType]` (i.e., `Optional[X]`) is allowed for `Union` because''' ''' the argument parser only supports one type per argument.''' F''' Problem encountered in field \'{field.name}\'.''' ) if type(UpperCamelCase_ ) not in field.type.__args__: # filter `str` in Union lowerCAmelCase : str = field.type.__args__[0] if field.type.__args__[1] == str else field.type.__args__[1] lowerCAmelCase : Tuple = getattr(field.type , '''__origin__''' , field.type ) elif bool not in field.type.__args__: # filter `NoneType` in Union (except for `Union[bool, NoneType]`) lowerCAmelCase : str = ( field.type.__args__[0] if isinstance(UpperCamelCase_ , field.type.__args__[1] ) else field.type.__args__[1] ) lowerCAmelCase : Union[str, Any] = getattr(field.type , '''__origin__''' , field.type ) # A variable to store kwargs for a boolean field, if needed # so that we can init a `no_*` complement argument (see below) lowerCAmelCase : Optional[Any] = {} if origin_type is Literal or (isinstance(field.type , UpperCamelCase_ ) and issubclass(field.type , UpperCamelCase_ )): if origin_type is Literal: lowerCAmelCase : Dict = field.type.__args__ else: lowerCAmelCase : Tuple = [x.value for x in field.type] lowerCAmelCase : Tuple = make_choice_type_function(kwargs['''choices'''] ) if field.default is not dataclasses.MISSING: lowerCAmelCase : str = field.default else: lowerCAmelCase : Optional[Any] = True elif field.type is bool or field.type == Optional[bool]: # Copy the currect kwargs to use to instantiate a `no_*` complement argument below. # We do not initialize it here because the `no_*` alternative must be instantiated after the real argument lowerCAmelCase : Any = copy(UpperCamelCase_ ) # Hack because type=bool in argparse does not behave as we want. lowerCAmelCase : List[Any] = string_to_bool if field.type is bool or (field.default is not None and field.default is not dataclasses.MISSING): # Default value is False if we have no default when of type bool. lowerCAmelCase : int = False if field.default is dataclasses.MISSING else field.default # This is the value that will get picked if we don't include --field_name in any way lowerCAmelCase : List[str] = default # This tells argparse we accept 0 or 1 value after --field_name lowerCAmelCase : List[Any] = '''?''' # This is the value that will get picked if we do --field_name (without value) lowerCAmelCase : Optional[Any] = True elif isclass(UpperCamelCase_ ) and issubclass(UpperCamelCase_ , UpperCamelCase_ ): lowerCAmelCase : List[Any] = field.type.__args__[0] lowerCAmelCase : int = '''+''' if field.default_factory is not dataclasses.MISSING: lowerCAmelCase : int = field.default_factory() elif field.default is dataclasses.MISSING: lowerCAmelCase : Any = True else: lowerCAmelCase : Tuple = field.type if field.default is not dataclasses.MISSING: lowerCAmelCase : Union[str, Any] = field.default elif field.default_factory is not dataclasses.MISSING: lowerCAmelCase : Tuple = field.default_factory() else: lowerCAmelCase : Union[str, Any] = True parser.add_argument(UpperCamelCase_ , *UpperCamelCase_ , **UpperCamelCase_ ) # Add a complement `no_*` argument for a boolean field AFTER the initial field has already been added. # Order is important for arguments with the same destination! # We use a copy of earlier kwargs because the original kwargs have changed a lot before reaching down # here and we do not need those changes/additional keys. if field.default is True and (field.type is bool or field.type == Optional[bool]): lowerCAmelCase : Optional[Any] = False parser.add_argument(F'''--no_{field.name}''' , action='''store_false''' , dest=field.name , **UpperCamelCase_ ) def lowerCamelCase__ ( self : List[Any] , UpperCamelCase_ : DataClassType ): if hasattr(UpperCamelCase_ , '''_argument_group_name''' ): lowerCAmelCase : Union[str, Any] = self.add_argument_group(dtype._argument_group_name ) else: lowerCAmelCase : Dict = self try: lowerCAmelCase : Dict[str, type] = get_type_hints(UpperCamelCase_ ) except NameError: raise RuntimeError( F'''Type resolution failed for {dtype}. Try declaring the class in global scope or ''' '''removing line of `from __future__ import annotations` which opts in Postponed ''' '''Evaluation of Annotations (PEP 563)''' ) except TypeError as ex: # Remove this block when we drop Python 3.9 support if sys.version_info[:2] < (3, 1_0) and "unsupported operand type(s) for |" in str(UpperCamelCase_ ): lowerCAmelCase : List[Any] = '''.'''.join(map(UpperCamelCase_ , sys.version_info[:3] ) ) raise RuntimeError( F'''Type resolution failed for {dtype} on Python {python_version}. Try removing ''' '''line of `from __future__ import annotations` which opts in union types as ''' '''`X | Y` (PEP 604) via Postponed Evaluation of Annotations (PEP 563). To ''' '''support Python versions that lower than 3.10, you need to use ''' '''`typing.Union[X, Y]` instead of `X | Y` and `typing.Optional[X]` instead of ''' '''`X | None`.''' ) from ex raise for field in dataclasses.fields(UpperCamelCase_ ): if not field.init: continue lowerCAmelCase : Any = type_hints[field.name] self._parse_dataclass_field(UpperCamelCase_ , UpperCamelCase_ ) def lowerCamelCase__ ( self : Tuple , UpperCamelCase_ : str=None , UpperCamelCase_ : List[Any]=False , UpperCamelCase_ : Any=True , UpperCamelCase_ : List[Any]=None , UpperCamelCase_ : int=None , ): if args_file_flag or args_filename or (look_for_args_file and len(sys.argv )): lowerCAmelCase : Optional[Any] = [] if args_filename: args_files.append(Path(UpperCamelCase_ ) ) elif look_for_args_file and len(sys.argv ): args_files.append(Path(sys.argv[0] ).with_suffix('''.args''' ) ) # args files specified via command line flag should overwrite default args files so we add them last if args_file_flag: # Create special parser just to extract the args_file_flag values lowerCAmelCase : Any = ArgumentParser() args_file_parser.add_argument(UpperCamelCase_ , type=UpperCamelCase_ , action='''append''' ) # Use only remaining args for further parsing (remove the args_file_flag) lowerCAmelCase, lowerCAmelCase : Optional[Any] = args_file_parser.parse_known_args(args=UpperCamelCase_ ) lowerCAmelCase : str = vars(UpperCamelCase_ ).get(args_file_flag.lstrip('''-''' ) , UpperCamelCase_ ) if cmd_args_file_paths: args_files.extend([Path(UpperCamelCase_ ) for p in cmd_args_file_paths] ) lowerCAmelCase : str = [] for args_file in args_files: if args_file.exists(): file_args += args_file.read_text().split() # in case of duplicate arguments the last one has precedence # args specified via the command line should overwrite args from files, so we add them last lowerCAmelCase : Optional[int] = file_args + args if args is not None else file_args + sys.argv[1:] lowerCAmelCase, lowerCAmelCase : Union[str, Any] = self.parse_known_args(args=UpperCamelCase_ ) lowerCAmelCase : Any = [] for dtype in self.dataclass_types: lowerCAmelCase : Tuple = {f.name for f in dataclasses.fields(UpperCamelCase_ ) if f.init} lowerCAmelCase : Tuple = {k: v for k, v in vars(UpperCamelCase_ ).items() if k in keys} for k in keys: delattr(UpperCamelCase_ , UpperCamelCase_ ) lowerCAmelCase : Union[str, Any] = dtype(**UpperCamelCase_ ) outputs.append(UpperCamelCase_ ) if len(namespace.__dict__ ) > 0: # additional namespace. outputs.append(UpperCamelCase_ ) if return_remaining_strings: return (*outputs, remaining_args) else: if remaining_args: raise ValueError(F'''Some specified arguments are not used by the HfArgumentParser: {remaining_args}''' ) return (*outputs,) def lowerCamelCase__ ( self : Optional[int] , UpperCamelCase_ : Dict[str, Any] , UpperCamelCase_ : bool = False ): lowerCAmelCase : List[Any] = set(args.keys() ) lowerCAmelCase : Optional[int] = [] for dtype in self.dataclass_types: lowerCAmelCase : int = {f.name for f in dataclasses.fields(UpperCamelCase_ ) if f.init} lowerCAmelCase : List[Any] = {k: v for k, v in args.items() if k in keys} unused_keys.difference_update(inputs.keys() ) lowerCAmelCase : List[Any] = dtype(**UpperCamelCase_ ) outputs.append(UpperCamelCase_ ) if not allow_extra_keys and unused_keys: raise ValueError(F'''Some keys are not used by the HfArgumentParser: {sorted(UpperCamelCase_ )}''' ) return tuple(UpperCamelCase_ ) def lowerCamelCase__ ( self : List[str] , UpperCamelCase_ : str , UpperCamelCase_ : bool = False ): with open(Path(UpperCamelCase_ ) , encoding='''utf-8''' ) as open_json_file: lowerCAmelCase : str = json.loads(open_json_file.read() ) lowerCAmelCase : Tuple = self.parse_dict(UpperCamelCase_ , allow_extra_keys=UpperCamelCase_ ) return tuple(UpperCamelCase_ ) def lowerCamelCase__ ( self : Any , UpperCamelCase_ : str , UpperCamelCase_ : bool = False ): lowerCAmelCase : Optional[int] = self.parse_dict(yaml.safe_load(Path(UpperCamelCase_ ).read_text() ) , allow_extra_keys=UpperCamelCase_ ) return tuple(UpperCamelCase_ )
60
"""simple docstring""" from . import ( albert, align, altclip, audio_spectrogram_transformer, auto, autoformer, bark, bart, barthez, bartpho, beit, bert, bert_generation, bert_japanese, bertweet, big_bird, bigbird_pegasus, biogpt, bit, blenderbot, blenderbot_small, blip, blip_a, bloom, bridgetower, byta, camembert, canine, chinese_clip, clap, clip, clipseg, codegen, conditional_detr, convbert, convnext, convnextva, cpm, cpmant, ctrl, cvt, dataavec, deberta, deberta_va, decision_transformer, deformable_detr, deit, deprecated, deta, detr, dialogpt, dinat, distilbert, dit, donut, dpr, dpt, efficientformer, efficientnet, electra, encodec, encoder_decoder, ernie, ernie_m, esm, falcon, flaubert, flava, fnet, focalnet, fsmt, funnel, git, glpn, gpta, gpt_bigcode, gpt_neo, gpt_neox, gpt_neox_japanese, gpt_swa, gptj, gptsan_japanese, graphormer, groupvit, herbert, hubert, ibert, imagegpt, informer, instructblip, jukebox, layoutlm, layoutlmva, layoutlmva, layoutxlm, led, levit, lilt, llama, longformer, longta, luke, lxmert, mam_aaa, marian, markuplm, maskaformer, maskformer, mbart, mbartaa, mega, megatron_bert, megatron_gpta, mgp_str, mluke, mobilebert, mobilenet_va, mobilenet_va, mobilevit, mobilevitva, mpnet, mra, mta, musicgen, mvp, nat, nezha, nllb, nllb_moe, nystromformer, oneformer, open_llama, openai, opt, owlvit, pegasus, pegasus_x, perceiver, phobert, pixastruct, plbart, poolformer, prophetnet, qdqbert, rag, realm, reformer, regnet, rembert, resnet, roberta, roberta_prelayernorm, roc_bert, roformer, rwkv, sam, segformer, sew, sew_d, speech_encoder_decoder, speech_to_text, speech_to_text_a, speechta, splinter, squeezebert, swiftformer, swin, swinasr, swinva, switch_transformers, ta, table_transformer, tapas, time_series_transformer, timesformer, timm_backbone, transfo_xl, trocr, tvlt, umta, unispeech, unispeech_sat, upernet, videomae, vilt, vision_encoder_decoder, vision_text_dual_encoder, visual_bert, vit, vit_hybrid, vit_mae, vit_msn, vivit, wavaveca, wavaveca_conformer, wavaveca_phoneme, wavaveca_with_lm, wavlm, whisper, x_clip, xglm, xlm, xlm_prophetnet, xlm_roberta, xlm_roberta_xl, xlnet, xmod, yolos, yoso, )
91
0
"""simple docstring""" import numpy as np lowerCamelCase_ : Union[str, Any] = [ ["""a""", """b""", """c""", """d""", """e"""], ["""f""", """g""", """h""", """i""", """k"""], ["""l""", """m""", """n""", """o""", """p"""], ["""q""", """r""", """s""", """t""", """u"""], ["""v""", """w""", """x""", """y""", """z"""], ] class __A : """simple docstring""" def __init__( self ) -> None: a =np.array(__A ) def SCREAMING_SNAKE_CASE ( self , __A ) -> np.ndarray: a , a =np.where(letter == self.SQUARE ) a =np.concatenate([indexa + 1, indexa + 1] ) return indexes def SCREAMING_SNAKE_CASE ( self , __A , __A ) -> str: a =self.SQUARE[indexa - 1, indexa - 1] return letter def SCREAMING_SNAKE_CASE ( self , __A ) -> str: a =message.lower() a =message.replace(''' ''' , '''''' ) a =message.replace('''j''' , '''i''' ) a =np.empty((2, len(__A )) ) for letter_index in range(len(__A ) ): a =self.letter_to_numbers(message[letter_index] ) a =numbers[0] a =numbers[1] a =first_step.reshape(2 * len(__A ) ) a ='''''' for numbers_index in range(len(__A ) ): a =int(second_step[numbers_index * 2] ) a =int(second_step[(numbers_index * 2) + 1] ) a =self.numbers_to_letter(__A , __A ) a =encoded_message + letter return encoded_message def SCREAMING_SNAKE_CASE ( self , __A ) -> str: a =message.lower() message.replace(''' ''' , '''''' ) a =np.empty(2 * len(__A ) ) for letter_index in range(len(__A ) ): a =self.letter_to_numbers(message[letter_index] ) a =numbers[0] a =numbers[1] a =first_step.reshape((2, len(__A )) ) a ='''''' for numbers_index in range(len(__A ) ): a =int(second_step[0, numbers_index] ) a =int(second_step[1, numbers_index] ) a =self.numbers_to_letter(__A , __A ) a =decoded_message + letter return decoded_message
215
"""simple docstring""" from __future__ import annotations from typing import Generic, TypeVar lowerCamelCase_ : List[Any] = TypeVar("""T""") class __A ( Generic[T] ): """simple docstring""" def __init__( self , __A ) -> None: a =data a =self a =0 class __A ( Generic[T] ): """simple docstring""" def __init__( self ) -> None: # map from node name to the node object a ={} def SCREAMING_SNAKE_CASE ( self , __A ) -> None: # create a new set with x as its member a =DisjointSetTreeNode(__A ) def SCREAMING_SNAKE_CASE ( self , __A ) -> DisjointSetTreeNode[T]: # find the set x belongs to (with path-compression) a =self.map[data] if elem_ref != elem_ref.parent: a =self.find_set(elem_ref.parent.data ) return elem_ref.parent def SCREAMING_SNAKE_CASE ( self , __A , __A ) -> None: # helper function for union operation if nodea.rank > nodea.rank: a =nodea else: a =nodea if nodea.rank == nodea.rank: nodea.rank += 1 def SCREAMING_SNAKE_CASE ( self , __A , __A ) -> None: # merge 2 disjoint sets self.link(self.find_set(__A ) , self.find_set(__A ) ) class __A ( Generic[T] ): """simple docstring""" def __init__( self ) -> None: # connections: map from the node to the neighbouring nodes (with weights) a ={} def SCREAMING_SNAKE_CASE ( self , __A ) -> None: # add a node ONLY if its not present in the graph if node not in self.connections: a ={} def SCREAMING_SNAKE_CASE ( self , __A , __A , __A ) -> None: # add an edge with the given weight self.add_node(__A ) self.add_node(__A ) a =weight a =weight def SCREAMING_SNAKE_CASE ( self ) -> GraphUndirectedWeighted[T]: a =[] a =set() for start in self.connections: for end in self.connections[start]: if (start, end) not in seen: seen.add((end, start) ) edges.append((start, end, self.connections[start][end]) ) edges.sort(key=lambda __A : x[2] ) # creating the disjoint set a =DisjointSetTree[T]() for node in self.connections: disjoint_set.make_set(__A ) # MST generation a =0 a =0 a =GraphUndirectedWeighted[T]() while num_edges < len(self.connections ) - 1: a , a , a =edges[index] index += 1 a =disjoint_set.find_set(__A ) a =disjoint_set.find_set(__A ) if parent_u != parent_v: num_edges += 1 graph.add_edge(__A , __A , __A ) disjoint_set.union(__A , __A ) return graph
215
1
"""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 __a ( __lowerCamelCase ): if hor == 128: UpperCAmelCase_ : Dict = ("DownResnetBlock1D", "DownResnetBlock1D", "DownResnetBlock1D") UpperCAmelCase_ : List[Any] = (32, 128, 256) UpperCAmelCase_ : Optional[int] = ("UpResnetBlock1D", "UpResnetBlock1D") elif hor == 32: UpperCAmelCase_ : int = ("DownResnetBlock1D", "DownResnetBlock1D", "DownResnetBlock1D", "DownResnetBlock1D") UpperCAmelCase_ : Dict = (32, 64, 128, 256) UpperCAmelCase_ : Union[str, Any] = ("UpResnetBlock1D", "UpResnetBlock1D", "UpResnetBlock1D") UpperCAmelCase_ : Optional[Any] = torch.load(f"""/Users/bglickenhaus/Documents/diffuser/temporal_unet-hopper-mediumv2-hor{hor}.torch""" ) UpperCAmelCase_ : Dict = model.state_dict() UpperCAmelCase_ : 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": 6_5536, "mid_block_type": "MidResTemporalBlock1D", "act_fn": "mish", } UpperCAmelCase_ : Dict = UNetaDModel(**lowerCAmelCase__ ) print(f"""length of state dict: {len(state_dict.keys() )}""" ) print(f"""length of value function dict: {len(hf_value_function.state_dict().keys() )}""" ) UpperCAmelCase_ : int = dict(zip(model.state_dict().keys(), hf_value_function.state_dict().keys() ) ) for k, v in mapping.items(): UpperCAmelCase_ : str = state_dict.pop(lowerCAmelCase__ ) hf_value_function.load_state_dict(lowerCAmelCase__ ) 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(lowerCAmelCase__, lowerCAmelCase__ ) def __a ( ): UpperCAmelCase_ : List[Any] = { "in_channels": 14, "down_block_types": ("DownResnetBlock1D", "DownResnetBlock1D", "DownResnetBlock1D", "DownResnetBlock1D"), "up_block_types": (), "out_block_type": "ValueFunction", "mid_block_type": "ValueFunctionMidBlock1D", "block_out_channels": (32, 64, 128, 256), "layers_per_block": 1, "downsample_each_block": True, "sample_size": 6_5536, "out_channels": 14, "extra_in_channels": 0, "time_embedding_type": "positional", "use_timestep_embedding": True, "flip_sin_to_cos": False, "freq_shift": 1, "norm_num_groups": 8, "act_fn": "mish", } UpperCAmelCase_ : Optional[int] = torch.load("/Users/bglickenhaus/Documents/diffuser/value_function-hopper-mediumv2-hor32.torch" ) UpperCAmelCase_ : Dict = model UpperCAmelCase_ : int = UNetaDModel(**lowerCAmelCase__ ) print(f"""length of state dict: {len(state_dict.keys() )}""" ) print(f"""length of value function dict: {len(hf_value_function.state_dict().keys() )}""" ) UpperCAmelCase_ : str = dict(zip(state_dict.keys(), hf_value_function.state_dict().keys() ) ) for k, v in mapping.items(): UpperCAmelCase_ : Tuple = state_dict.pop(lowerCAmelCase__ ) hf_value_function.load_state_dict(lowerCAmelCase__ ) 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(lowerCAmelCase__, lowerCAmelCase__ ) if __name__ == "__main__": unet(32) # unet(128) value_function()
61
"""simple docstring""" from scipy.stats import pearsonr import datasets __lowerCAmelCase : List[Any] =""" Pearson correlation coefficient and p-value for testing non-correlation. The Pearson correlation coefficient measures the linear relationship between two datasets. The calculation of the p-value relies on the assumption that each dataset is normally distributed. Like other correlation coefficients, this one varies between -1 and +1 with 0 implying no correlation. Correlations of -1 or +1 imply an exact linear relationship. Positive correlations imply that as x increases, so does y. Negative correlations imply that as x increases, y decreases. The p-value roughly indicates the probability of an uncorrelated system producing datasets that have a Pearson correlation at least as extreme as the one computed from these datasets. """ __lowerCAmelCase : Optional[int] =""" Args: predictions (`list` of `int`): Predicted class labels, as returned by a model. references (`list` of `int`): Ground truth labels. return_pvalue (`boolean`): If `True`, returns the p-value, along with the correlation coefficient. If `False`, returns only the correlation coefficient. Defaults to `False`. Returns: pearsonr (`float`): Pearson correlation coefficient. Minimum possible value is -1. Maximum possible value is 1. Values of 1 and -1 indicate exact linear positive and negative relationships, respectively. A value of 0 implies no correlation. p-value (`float`): P-value, which roughly indicates the probability of an The p-value roughly indicates the probability of an uncorrelated system producing datasets that have a Pearson correlation at least as extreme as the one computed from these datasets. Minimum possible value is 0. Maximum possible value is 1. Higher values indicate higher probabilities. Examples: Example 1-A simple example using only predictions and references. >>> pearsonr_metric = datasets.load_metric(\"pearsonr\") >>> results = pearsonr_metric.compute(predictions=[10, 9, 2.5, 6, 4], references=[1, 2, 3, 4, 5]) >>> print(round(results['pearsonr'], 2)) -0.74 Example 2-The same as Example 1, but that also returns the `p-value`. >>> pearsonr_metric = datasets.load_metric(\"pearsonr\") >>> results = pearsonr_metric.compute(predictions=[10, 9, 2.5, 6, 4], references=[1, 2, 3, 4, 5], return_pvalue=True) >>> print(sorted(list(results.keys()))) ['p-value', 'pearsonr'] >>> print(round(results['pearsonr'], 2)) -0.74 >>> print(round(results['p-value'], 2)) 0.15 """ __lowerCAmelCase : str =""" @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, Ilhan 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, Antonio 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 _A ( datasets.Metric ): def A__ ( self ): """simple docstring""" 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.pearsonr.html"""] , ) def A__ ( self , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase=False ): """simple docstring""" if return_pvalue: lowercase = pearsonr(__lowerCAmelCase , __lowerCAmelCase ) return {"pearsonr": results[0], "p-value": results[1]} else: return {"pearsonr": float(pearsonr(__lowerCAmelCase , __lowerCAmelCase )[0] )}
197
0
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 UpperCamelCase : def __init__( self , UpperCAmelCase__ , UpperCAmelCase__=13 , UpperCAmelCase__=10 , UpperCAmelCase__=3 , UpperCAmelCase__=2 , UpperCAmelCase__=2 , UpperCAmelCase__=True , UpperCAmelCase__=True , UpperCAmelCase__=32 , UpperCAmelCase__=5 , UpperCAmelCase__=4 , UpperCAmelCase__=37 , UpperCAmelCase__="gelu" , UpperCAmelCase__=0.1 , UpperCAmelCase__=0.1 , UpperCAmelCase__=10 , UpperCAmelCase__=0.02 , UpperCAmelCase__="divided_space_time" , UpperCAmelCase__=None , ): A__ = parent A__ = batch_size A__ = image_size A__ = num_channels A__ = patch_size A__ = num_frames A__ = is_training A__ = use_labels A__ = hidden_size A__ = num_hidden_layers A__ = num_attention_heads A__ = intermediate_size A__ = hidden_act A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = attention_type A__ = initializer_range A__ = scope A__ = num_labels # in TimeSformer, the number of spatial tokens equals num_frames * num_patches per frame + 1 CLS token A__ = (image_size // patch_size) ** 2 A__ = (num_frames) * self.num_patches_per_frame + 1 def __A ( self ): A__ = floats_tensor( [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size] ) A__ = None if self.use_labels: A__ = ids_tensor([self.batch_size] , self.num_labels ) A__ = self.get_config() return config, pixel_values, labels def __A ( self ): A__ = 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 , ) A__ = self.num_labels return config def __A ( self , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ): A__ = TimesformerModel(config=UpperCAmelCase__ ) model.to(UpperCAmelCase__ ) model.eval() A__ = model(UpperCAmelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __A ( self , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ): A__ = TimesformerForVideoClassification(UpperCAmelCase__ ) model.to(UpperCAmelCase__ ) model.eval() A__ = model(UpperCAmelCase__ ) # verify the logits shape A__ = torch.Size((self.batch_size, self.num_labels) ) self.parent.assertEqual(result.logits.shape , UpperCAmelCase__ ) def __A ( self ): A__ = self.prepare_config_and_inputs() A__ , A__ , A__ = config_and_inputs A__ = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class UpperCamelCase ( _UpperCAmelCase , _UpperCAmelCase , unittest.TestCase ): lowerCAmelCase : Optional[Any] = (TimesformerModel, TimesformerForVideoClassification) if is_torch_available() else () lowerCAmelCase : Dict = ( {"""feature-extraction""": TimesformerModel, """video-classification""": TimesformerForVideoClassification} if is_torch_available() else {} ) lowerCAmelCase : List[str] = False lowerCAmelCase : Optional[int] = False lowerCAmelCase : Optional[Any] = False lowerCAmelCase : Any = False def __A ( self ): A__ = TimesformerModelTester(self ) A__ = ConfigTester( self , config_class=UpperCAmelCase__ , has_text_modality=UpperCAmelCase__ , hidden_size=37 ) def __A ( self , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__=False ): A__ = copy.deepcopy(UpperCAmelCase__ ) if return_labels: if model_class in get_values(UpperCAmelCase__ ): A__ = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=UpperCAmelCase__ ) return inputs_dict def __A ( self ): self.config_tester.run_common_tests() @unittest.skip(reason="TimeSformer does not use inputs_embeds" ) def __A ( self ): pass def __A ( self ): A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ = model_class(UpperCAmelCase__ ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) A__ = model.get_output_embeddings() self.assertTrue(x is None or isinstance(UpperCAmelCase__ , nn.Linear ) ) def __A ( self ): A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ = model_class(UpperCAmelCase__ ) A__ = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic A__ = [*signature.parameters.keys()] A__ = ["pixel_values"] self.assertListEqual(arg_names[:1] , UpperCAmelCase__ ) def __A ( self ): A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*UpperCAmelCase__ ) def __A ( self ): A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_video_classification(*UpperCAmelCase__ ) @slow def __A ( self ): for model_name in TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A__ = TimesformerModel.from_pretrained(UpperCAmelCase__ ) self.assertIsNotNone(UpperCAmelCase__ ) def __A ( self ): if not self.has_attentions: pass else: A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() A__ = True for model_class in self.all_model_classes: A__ = self.model_tester.seq_length A__ = self.model_tester.num_frames A__ = True A__ = False A__ = True A__ = model_class(UpperCAmelCase__ ) model.to(UpperCAmelCase__ ) model.eval() with torch.no_grad(): A__ = model(**self._prepare_for_class(UpperCAmelCase__ , UpperCAmelCase__ ) ) A__ = outputs.attentions self.assertEqual(len(UpperCAmelCase__ ) , self.model_tester.num_hidden_layers ) # check that output_attentions also work using config del inputs_dict["output_attentions"] A__ = True A__ = model_class(UpperCAmelCase__ ) model.to(UpperCAmelCase__ ) model.eval() with torch.no_grad(): A__ = model(**self._prepare_for_class(UpperCAmelCase__ , UpperCAmelCase__ ) ) A__ = outputs.attentions self.assertEqual(len(UpperCAmelCase__ ) , 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] , ) A__ = len(UpperCAmelCase__ ) # Check attention is always last and order is fine A__ = True A__ = True A__ = model_class(UpperCAmelCase__ ) model.to(UpperCAmelCase__ ) model.eval() with torch.no_grad(): A__ = model(**self._prepare_for_class(UpperCAmelCase__ , UpperCAmelCase__ ) ) self.assertEqual(out_len + 1 , len(UpperCAmelCase__ ) ) A__ = outputs.attentions self.assertEqual(len(UpperCAmelCase__ ) , 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 __A ( self ): def check_hidden_states_output(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ): A__ = model_class(UpperCAmelCase__ ) model.to(UpperCAmelCase__ ) model.eval() with torch.no_grad(): A__ = model(**self._prepare_for_class(UpperCAmelCase__ , UpperCAmelCase__ ) ) A__ = outputs.hidden_states A__ = self.model_tester.num_hidden_layers + 1 self.assertEqual(len(UpperCAmelCase__ ) , UpperCAmelCase__ ) A__ = self.model_tester.seq_length self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [seq_length, self.model_tester.hidden_size] , ) A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A__ = True check_hidden_states_output(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] A__ = True check_hidden_states_output(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ) def UpperCamelCase ( )-> Dict: """simple docstring""" A__ = hf_hub_download( repo_id="hf-internal-testing/spaghetti-video" , filename="eating_spaghetti.npy" , repo_type="dataset" ) A__ = np.load(_A ) return list(_A ) @require_torch @require_vision class UpperCamelCase ( unittest.TestCase ): @cached_property def __A ( self ): # 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 __A ( self ): A__ = TimesformerForVideoClassification.from_pretrained("facebook/timesformer-base-finetuned-k400" ).to( UpperCAmelCase__ ) A__ = self.default_image_processor A__ = prepare_video() A__ = image_processor(video[:8] , return_tensors="pt" ).to(UpperCAmelCase__ ) # forward pass with torch.no_grad(): A__ = model(**UpperCAmelCase__ ) # verify the logits A__ = torch.Size((1, 400) ) self.assertEqual(outputs.logits.shape , UpperCAmelCase__ ) A__ = torch.tensor([-0.3_016, -0.7_713, -0.4_205] ).to(UpperCAmelCase__ ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , UpperCAmelCase__ , atol=1e-4 ) )
198
def UpperCamelCase ( _A : str , _A : str )-> str: """simple docstring""" A__ = len(_A ) A__ = len(_A ) A__ = ( first_str_length if first_str_length > second_str_length else second_str_length ) A__ = [] for char_count in range(_A ): if char_count < first_str_length: output_list.append(first_str[char_count] ) if char_count < second_str_length: output_list.append(second_str[char_count] ) return "".join(_A ) if __name__ == "__main__": print(alternative_string_arrange("AB", "XYZ"), end=" ")
198
1
import os import numpy import onnx def lowerCamelCase__ ( a__ : str , a__ : Optional[Any] ) -> int: UpperCamelCase_ = a.name UpperCamelCase_ = b.name UpperCamelCase_ = '''''' UpperCamelCase_ = '''''' UpperCamelCase_ = a == b UpperCamelCase_ = name_a UpperCamelCase_ = name_b return res def lowerCamelCase__ ( a__ : str , a__ : Any , a__ : Tuple ) -> Any: for i, input_name in enumerate(node_proto.input ): if input_name == name: node_proto.input.insert(_lowerCAmelCase , _lowerCAmelCase ) node_proto.input.pop(i + 1 ) if node_proto.op_type == "If": _graph_replace_input_with(node_proto.attribute[0].g , _lowerCAmelCase , _lowerCAmelCase ) _graph_replace_input_with(node_proto.attribute[1].g , _lowerCAmelCase , _lowerCAmelCase ) if node_proto.op_type == "Loop": _graph_replace_input_with(node_proto.attribute[0].g , _lowerCAmelCase , _lowerCAmelCase ) def lowerCamelCase__ ( a__ : int , a__ : Any , a__ : Optional[Any] ) -> Any: for n in graph_proto.node: _node_replace_input_with(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) def lowerCamelCase__ ( a__ : str , a__ : List[Any] , a__ : List[str] ) -> int: UpperCamelCase_ = list(model.graph.initializer ) UpperCamelCase_ = list(model_without_ext.graph.initializer ) for i, ref_i in ind_to_replace: assert inits_with_data[i].name == inits[i].name assert inits_with_data[ref_i].name == inits[ref_i].name assert i > ref_i UpperCamelCase_ = inits[i].name UpperCamelCase_ = inits[ref_i].name model_without_ext.graph.initializer.remove(inits[i] ) # for n in model.graph.node: _graph_replace_input_with(model_without_ext.graph , _lowerCAmelCase , _lowerCAmelCase ) def lowerCamelCase__ ( a__ : int ) -> Tuple: UpperCamelCase_ = os.path.dirname(_lowerCAmelCase ) UpperCamelCase_ = os.path.basename(_lowerCAmelCase ) UpperCamelCase_ = onnx.load(os.path.join(_lowerCAmelCase , _lowerCAmelCase ) ) UpperCamelCase_ = list(model.graph.initializer ) UpperCamelCase_ = set() UpperCamelCase_ = {} UpperCamelCase_ = [] UpperCamelCase_ = 0 for i in range(len(_lowerCAmelCase ) ): if i in dup_set: continue for j in range(i + 1 , len(_lowerCAmelCase ) ): if j in dup_set: continue if _is_equal_tensor_proto(inits[i] , inits[j] ): dup_set.add(_lowerCAmelCase ) dup_set.add(_lowerCAmelCase ) UpperCamelCase_ = inits[j].data_type UpperCamelCase_ = numpy.prod(inits[j].dims ) if dtype == 1: mem_size *= 4 elif dtype == 6: mem_size *= 4 elif dtype == 7 or dtype == 11: mem_size *= 8 else: print("""unexpected data type: """ , _lowerCAmelCase ) total_reduced_size += mem_size UpperCamelCase_ = inits[i].name UpperCamelCase_ = inits[j].name if name_i in dup_map: dup_map[name_i].append(_lowerCAmelCase ) else: UpperCamelCase_ = [name_j] ind_to_replace.append((j, i) ) print("""total reduced size: """ , total_reduced_size / 1024 / 1024 / 1024 , """GB""" ) UpperCamelCase_ = sorted(_lowerCAmelCase ) _remove_dup_initializers_from_model(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) UpperCamelCase_ = '''optimized_''' + model_file_name UpperCamelCase_ = os.path.join(_lowerCAmelCase , _lowerCAmelCase ) onnx.save(_lowerCAmelCase , _lowerCAmelCase ) return new_model
122
'''simple docstring''' from typing import List, Optional, Tuple, Union import torch from ...models import UNetaDModel from ...schedulers import ScoreSdeVeScheduler from ...utils import randn_tensor from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput class SCREAMING_SNAKE_CASE( A__ ): """simple docstring""" lowerCamelCase__ = 42 lowerCamelCase__ = 42 def __init__( self : Union[str, Any] , __snake_case : UNetaDModel , __snake_case : ScoreSdeVeScheduler ) -> int: super().__init__() self.register_modules(unet=__snake_case , scheduler=__snake_case ) @torch.no_grad() def __call__( self : Optional[int] , __snake_case : int = 1 , __snake_case : int = 2000 , __snake_case : Optional[Union[torch.Generator, List[torch.Generator]]] = None , __snake_case : Optional[str] = "pil" , __snake_case : bool = True , **__snake_case : Optional[int] , ) -> Union[ImagePipelineOutput, Tuple]: UpperCAmelCase : str = self.unet.config.sample_size UpperCAmelCase : Union[str, Any] = (batch_size, 3, img_size, img_size) UpperCAmelCase : int = self.unet UpperCAmelCase : Any = randn_tensor(__snake_case , generator=__snake_case ) * self.scheduler.init_noise_sigma UpperCAmelCase : List[Any] = sample.to(self.device ) self.scheduler.set_timesteps(__snake_case ) self.scheduler.set_sigmas(__snake_case ) for i, t in enumerate(self.progress_bar(self.scheduler.timesteps ) ): UpperCAmelCase : Any = self.scheduler.sigmas[i] * torch.ones(shape[0] , device=self.device ) # correction step for _ in range(self.scheduler.config.correct_steps ): UpperCAmelCase : Union[str, Any] = self.unet(__snake_case , __snake_case ).sample UpperCAmelCase : Optional[Any] = self.scheduler.step_correct(__snake_case , __snake_case , generator=__snake_case ).prev_sample # prediction step UpperCAmelCase : Optional[Any] = model(__snake_case , __snake_case ).sample UpperCAmelCase : List[str] = self.scheduler.step_pred(__snake_case , __snake_case , __snake_case , generator=__snake_case ) UpperCAmelCase , UpperCAmelCase : Optional[Any] = output.prev_sample, output.prev_sample_mean UpperCAmelCase : int = sample_mean.clamp(0 , 1 ) UpperCAmelCase : Union[str, Any] = sample.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": UpperCAmelCase : Optional[Any] = self.numpy_to_pil(__snake_case ) if not return_dict: return (sample,) return ImagePipelineOutput(images=__snake_case )
23
0
import numpy as np from nltk.translate import meteor_score import datasets from datasets.config import importlib_metadata, version UpperCAmelCase_ : Dict = version.parse(importlib_metadata.version("nltk")) if NLTK_VERSION >= version.Version("3.6.4"): from nltk import word_tokenize UpperCAmelCase_ : List[Any] = "\\n@inproceedings{banarjee2005,\n title = {{METEOR}: An Automatic Metric for {MT} Evaluation with Improved Correlation with Human Judgments},\n author = {Banerjee, Satanjeev and Lavie, Alon},\n booktitle = {Proceedings of the {ACL} Workshop on Intrinsic and Extrinsic Evaluation Measures for Machine Translation and/or Summarization},\n month = jun,\n year = {2005},\n address = {Ann Arbor, Michigan},\n publisher = {Association for Computational Linguistics},\n url = {https://www.aclweb.org/anthology/W05-0909},\n pages = {65--72},\n}\n" UpperCAmelCase_ : Any = "\\nMETEOR, an automatic metric for machine translation evaluation\nthat is based on a generalized concept of unigram matching between the\nmachine-produced translation and human-produced reference translations.\nUnigrams can be matched based on their surface forms, stemmed forms,\nand meanings; furthermore, METEOR can be easily extended to include more\nadvanced matching strategies. Once all generalized unigram matches\nbetween the two strings have been found, METEOR computes a score for\nthis matching using a combination of unigram-precision, unigram-recall, and\na measure of fragmentation that is designed to directly capture how\nwell-ordered the matched words in the machine translation are in relation\nto the reference.\n\nMETEOR gets an R correlation value of 0.347 with human evaluation on the Arabic\ndata and 0.331 on the Chinese data. This is shown to be an improvement on\nusing simply unigram-precision, unigram-recall and their harmonic F1\ncombination.\n" UpperCAmelCase_ : int = "\nComputes METEOR score of translated segments against one or more references.\nArgs:\n predictions: list of predictions to score. Each prediction\n should be a string with tokens separated by spaces.\n references: list of reference for each prediction. Each\n reference should be a string with tokens separated by spaces.\n alpha: Parameter for controlling relative weights of precision and recall. default: 0.9\n beta: Parameter for controlling shape of penalty as a function of fragmentation. default: 3\n gamma: Relative weight assigned to fragmentation penalty. default: 0.5\nReturns:\n 'meteor': meteor score.\nExamples:\n\n >>> meteor = datasets.load_metric('meteor')\n >>> predictions = [\"It is a guide to action which ensures that the military always obeys the commands of the party\"]\n >>> references = [\"It is a guide to action that ensures that the military will forever heed Party commands\"]\n >>> results = meteor.compute(predictions=predictions, references=references)\n >>> print(round(results[\"meteor\"], 4))\n 0.6944\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class UpperCamelCase ( datasets.Metric ): def __A ( self ): return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { "predictions": datasets.Value("string" , id="sequence" ), "references": datasets.Value("string" , id="sequence" ), } ) , codebase_urls=["https://github.com/nltk/nltk/blob/develop/nltk/translate/meteor_score.py"] , reference_urls=[ "https://www.nltk.org/api/nltk.translate.html#module-nltk.translate.meteor_score", "https://en.wikipedia.org/wiki/METEOR", ] , ) def __A ( self , UpperCAmelCase__ ): import nltk nltk.download("wordnet" ) if NLTK_VERSION >= version.Version("3.6.5" ): nltk.download("punkt" ) if NLTK_VERSION >= version.Version("3.6.6" ): nltk.download("omw-1.4" ) def __A ( self , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__=0.9 , UpperCAmelCase__=3 , UpperCAmelCase__=0.5 ): if NLTK_VERSION >= version.Version("3.6.5" ): A__ = [ meteor_score.single_meteor_score( word_tokenize(UpperCAmelCase__ ) , word_tokenize(UpperCAmelCase__ ) , alpha=UpperCAmelCase__ , beta=UpperCAmelCase__ , gamma=UpperCAmelCase__ ) for ref, pred in zip(UpperCAmelCase__ , UpperCAmelCase__ ) ] else: A__ = [ meteor_score.single_meteor_score(UpperCAmelCase__ , UpperCAmelCase__ , alpha=UpperCAmelCase__ , beta=UpperCAmelCase__ , gamma=UpperCAmelCase__ ) for ref, pred in zip(UpperCAmelCase__ , UpperCAmelCase__ ) ] return {"meteor": np.mean(UpperCAmelCase__ )}
198
import os import zipfile import requests from get_ci_error_statistics import download_artifact, get_artifacts_links def UpperCamelCase ( _A : List[Any] , _A : List[str]=7 )-> Optional[Any]: """simple docstring""" A__ = None if token is not None: A__ = {"Accept": "application/vnd.github+json", "Authorization": f"""Bearer {token}"""} # The id of a workflow (not of a workflow run) A__ = "636036" A__ = f"""https://api.github.com/repos/huggingface/transformers/actions/workflows/{workflow_id}/runs""" # On `main` branch + event being `schedule` + not returning PRs + only `num_runs` results url += f"""?branch=main&event=schedule&exclude_pull_requests=true&per_page={num_runs}""" A__ = requests.get(_A , headers=_A ).json() return result["workflow_runs"] def UpperCamelCase ( _A : str )-> Dict: """simple docstring""" A__ = get_daily_ci_runs(_A ) A__ = None for workflow_run in workflow_runs: if workflow_run["status"] == "completed": A__ = workflow_run["id"] break return workflow_run_id def UpperCamelCase ( _A : int , _A : List[str] , _A : str )-> Any: """simple docstring""" A__ = get_last_daily_ci_runs(_A ) if workflow_run_id is not None: A__ = get_artifacts_links(worflow_run_id=_A , token=_A ) for artifact_name in artifact_names: if artifact_name in artifacts_links: A__ = artifacts_links[artifact_name] download_artifact( artifact_name=_A , artifact_url=_A , output_dir=_A , token=_A ) def UpperCamelCase ( _A : Optional[Any] , _A : Any , _A : List[Any] )-> Optional[int]: """simple docstring""" get_last_daily_ci_artifacts(_A , _A , _A ) A__ = {} for artifact_name in artifact_names: A__ = os.path.join(_A , f"""{artifact_name}.zip""" ) if os.path.isfile(_A ): A__ = {} with zipfile.ZipFile(_A ) as z: for filename in z.namelist(): if not os.path.isdir(_A ): # read the file with z.open(_A ) as f: A__ = f.read().decode("UTF-8" ) return results
198
1
'''simple docstring''' import argparse import json from collections import OrderedDict from functools import partial from pathlib import Path import timm import torch from huggingface_hub import hf_hub_download from transformers import LevitConfig, LevitForImageClassificationWithTeacher, LevitImageProcessor from transformers.utils import logging logging.set_verbosity_info() __lowerCAmelCase = logging.get_logger() def __lowerCamelCase ( lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ = True ) -> str: print(f"""Converting {name}...""" ) with torch.no_grad(): if hidden_sizes == 128: if name[-1] == "S": _a : Optional[Any] = timm.create_model('levit_128s' , pretrained=lowerCAmelCase_ ) else: _a : List[str] = timm.create_model('levit_128' , pretrained=lowerCAmelCase_ ) if hidden_sizes == 192: _a : Optional[int] = timm.create_model('levit_192' , pretrained=lowerCAmelCase_ ) if hidden_sizes == 256: _a : Any = timm.create_model('levit_256' , pretrained=lowerCAmelCase_ ) if hidden_sizes == 384: _a : Optional[int] = timm.create_model('levit_384' , pretrained=lowerCAmelCase_ ) from_model.eval() _a : Optional[Any] = LevitForImageClassificationWithTeacher(lowerCAmelCase_ ).eval() _a : List[str] = OrderedDict() _a : Optional[int] = from_model.state_dict() _a : int = list(from_model.state_dict().keys() ) _a : int = list(our_model.state_dict().keys() ) print(len(lowerCAmelCase_ ) , len(lowerCAmelCase_ ) ) for i in range(len(lowerCAmelCase_ ) ): _a : Any = weights[og_keys[i]] our_model.load_state_dict(lowerCAmelCase_ ) _a : Optional[Any] = torch.randn((2, 3, 224, 224) ) _a : Optional[Any] = from_model(lowerCAmelCase_ ) _a : Optional[Any] = our_model(lowerCAmelCase_ ).logits assert torch.allclose(lowerCAmelCase_ , lowerCAmelCase_ ), "The model logits don't match the original one." _a : Dict = name print(lowerCAmelCase_ ) if push_to_hub: our_model.save_pretrained(save_directory / checkpoint_name ) _a : Union[str, Any] = LevitImageProcessor() image_processor.save_pretrained(save_directory / checkpoint_name ) print(f"""Pushed {checkpoint_name}""" ) def __lowerCamelCase ( lowerCAmelCase_ , lowerCAmelCase_ = None , lowerCAmelCase_ = True ) -> Any: _a : Optional[int] = 'imagenet-1k-id2label.json' _a : Dict = 1000 _a : Union[str, Any] = (1, num_labels) _a : Optional[int] = 'huggingface/label-files' _a : Tuple = num_labels _a : Optional[int] = json.load(open(hf_hub_download(lowerCAmelCase_ , lowerCAmelCase_ , repo_type='dataset' ) , 'r' ) ) _a : Any = {int(lowerCAmelCase_ ): v for k, v in idalabel.items()} _a : str = idalabel _a : Tuple = {v: k for k, v in idalabel.items()} _a : Tuple = partial(lowerCAmelCase_ , num_labels=lowerCAmelCase_ , idalabel=lowerCAmelCase_ , labelaid=lowerCAmelCase_ ) _a : List[str] = { 'levit-128S': 128, 'levit-128': 128, 'levit-192': 192, 'levit-256': 256, 'levit-384': 384, } _a : Optional[int] = { 'levit-128S': ImageNetPreTrainedConfig( hidden_sizes=[128, 256, 384] , num_attention_heads=[4, 6, 8] , depths=[2, 3, 4] , key_dim=[16, 16, 16] , drop_path_rate=0 , ), 'levit-128': ImageNetPreTrainedConfig( hidden_sizes=[128, 256, 384] , num_attention_heads=[4, 8, 12] , depths=[4, 4, 4] , key_dim=[16, 16, 16] , drop_path_rate=0 , ), 'levit-192': ImageNetPreTrainedConfig( hidden_sizes=[192, 288, 384] , num_attention_heads=[3, 5, 6] , depths=[4, 4, 4] , key_dim=[32, 32, 32] , drop_path_rate=0 , ), 'levit-256': ImageNetPreTrainedConfig( hidden_sizes=[256, 384, 512] , num_attention_heads=[4, 6, 8] , depths=[4, 4, 4] , key_dim=[32, 32, 32] , drop_path_rate=0 , ), 'levit-384': ImageNetPreTrainedConfig( hidden_sizes=[384, 512, 768] , num_attention_heads=[6, 9, 12] , depths=[4, 4, 4] , key_dim=[32, 32, 32] , drop_path_rate=0.1 , ), } if model_name: convert_weight_and_push( names_to_hidden_sizes[model_name] , lowerCAmelCase_ , names_to_config[model_name] , lowerCAmelCase_ , lowerCAmelCase_ ) else: for model_name, config in names_to_config.items(): convert_weight_and_push(names_to_hidden_sizes[model_name] , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ) return config, expected_shape if __name__ == "__main__": __lowerCAmelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default=None, type=str, help='''The name of the model you wish to convert, it must be one of the supported Levit* architecture,''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default='''levit-dump-folder/''', type=Path, required=False, help='''Path to the output PyTorch model directory.''', ) parser.add_argument('''--push_to_hub''', action='''store_true''', help='''Push model and image processor to the hub''') parser.add_argument( '''--no-push_to_hub''', dest='''push_to_hub''', action='''store_false''', help='''Do not push model and image processor to the hub''', ) __lowerCAmelCase = parser.parse_args() __lowerCAmelCase = args.pytorch_dump_folder_path pytorch_dump_folder_path.mkdir(exist_ok=True, parents=True) convert_weights_and_push(pytorch_dump_folder_path, args.model_name, args.push_to_hub)
89
'''simple docstring''' def __lowerCamelCase ( ) -> Tuple: for n in range(1 , 1000000 ): yield n * (n + 1) // 2 def __lowerCamelCase ( lowerCAmelCase_ ) -> List[Any]: _a : Any = 1 _a : Tuple = 2 while i * i <= n: _a : Tuple = 0 while n % i == 0: n //= i multiplicity += 1 divisors_count *= multiplicity + 1 i += 1 if n > 1: divisors_count *= 2 return divisors_count def __lowerCamelCase ( ) -> str: return next(i for i in triangle_number_generator() if count_divisors(lowerCAmelCase_ ) > 500 ) if __name__ == "__main__": print(solution())
89
1
def lowercase_ ( lowerCAmelCase__ : int , lowerCAmelCase__ : int ): """simple docstring""" return int((input_a, input_a).count(0 ) != 0 ) def lowercase_ ( ): """simple docstring""" assert nand_gate(0 , 0 ) == 1 assert nand_gate(0 , 1 ) == 1 assert nand_gate(1 , 0 ) == 1 assert nand_gate(1 , 1 ) == 0 if __name__ == "__main__": print(nand_gate(0, 0)) print(nand_gate(0, 1)) print(nand_gate(1, 0)) print(nand_gate(1, 1))
371
'''simple docstring''' import warnings from ...utils import logging from .image_processing_layoutlmva import LayoutLMvaImageProcessor _UpperCamelCase = logging.get_logger(__name__) class _A ( __SCREAMING_SNAKE_CASE ): def __init__( self , *__UpperCAmelCase , **__UpperCAmelCase ) -> None: '''simple docstring''' warnings.warn( """The class LayoutLMv2FeatureExtractor is deprecated and will be removed in version 5 of Transformers.""" """ Please use LayoutLMv2ImageProcessor instead.""" , __UpperCAmelCase , ) super().__init__(*__UpperCAmelCase , **__UpperCAmelCase )
16
0
'''simple docstring''' def a ( __a ) -> Any: '''simple docstring''' UpperCamelCase__ :Any = 1 UpperCamelCase__ :Optional[int] = 2 while i * i <= n: UpperCamelCase__ :Optional[Any] = 0 while n % i == 0: n //= i multiplicity += 1 n_divisors *= multiplicity + 1 i += 1 if n > 1: n_divisors *= 2 return n_divisors def a ( ) -> int: '''simple docstring''' UpperCamelCase__ :Any = 1 UpperCamelCase__ :Optional[Any] = 1 while True: i += 1 t_num += i if count_divisors(__a ) > 500: break return t_num if __name__ == "__main__": print(solution())
97
"""simple docstring""" _snake_case : Optional[int] = [ 'DownloadConfig', 'DownloadManager', 'DownloadMode', 'StreamingDownloadManager', ] from .download_config import DownloadConfig from .download_manager import DownloadManager, DownloadMode from .streaming_download_manager import StreamingDownloadManager
292
0
"""simple docstring""" 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 __magic_name__ = get_tests_dir("fixtures/test_sentencepiece.model") __magic_name__ = get_tests_dir("fixtures/test_sentencepiece_bpe.model") __magic_name__ = "pt" if is_torch_available() else "tf" @require_sentencepiece @require_tokenizers class SCREAMING_SNAKE_CASE_ ( __a , unittest.TestCase ): """simple docstring""" __lowercase : Optional[int] = CamembertTokenizer __lowercase : Optional[Any] = CamembertTokenizerFast __lowercase : Dict = True __lowercase : List[Any] = True def snake_case_ ( self): super().setUp() # We have a SentencePiece fixture for testing __SCREAMING_SNAKE_CASE = CamembertTokenizer(__UpperCAmelCase) tokenizer.save_pretrained(self.tmpdirname) def snake_case_ ( self): __SCREAMING_SNAKE_CASE = """<pad>""" __SCREAMING_SNAKE_CASE = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(__UpperCAmelCase) , __UpperCAmelCase) self.assertEqual(self.get_tokenizer()._convert_id_to_token(__UpperCAmelCase) , __UpperCAmelCase) def snake_case_ ( self): __SCREAMING_SNAKE_CASE = 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(__UpperCAmelCase) , 1_0_0_4) def snake_case_ ( self): self.assertEqual(self.get_tokenizer().vocab_size , 1_0_0_5) def snake_case_ ( self): __SCREAMING_SNAKE_CASE = CamembertTokenizer(__UpperCAmelCase) tokenizer.save_pretrained(self.tmpdirname) __SCREAMING_SNAKE_CASE = CamembertTokenizerFast.from_pretrained(self.tmpdirname) __SCREAMING_SNAKE_CASE = """I was born in 92000, and this is falsé.""" __SCREAMING_SNAKE_CASE = tokenizer.encode(__UpperCAmelCase) __SCREAMING_SNAKE_CASE = rust_tokenizer.encode(__UpperCAmelCase) self.assertListEqual(__UpperCAmelCase , __UpperCAmelCase) __SCREAMING_SNAKE_CASE = tokenizer.encode(__UpperCAmelCase , add_special_tokens=__UpperCAmelCase) __SCREAMING_SNAKE_CASE = rust_tokenizer.encode(__UpperCAmelCase , add_special_tokens=__UpperCAmelCase) self.assertListEqual(__UpperCAmelCase , __UpperCAmelCase) # <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) __SCREAMING_SNAKE_CASE = tokenizer.convert_ids_to_tokens(__UpperCAmelCase) __SCREAMING_SNAKE_CASE = rust_tokenizer.tokenize(__UpperCAmelCase) self.assertListEqual(__UpperCAmelCase , __UpperCAmelCase) def snake_case_ ( self): if not self.test_rust_tokenizer: return __SCREAMING_SNAKE_CASE = self.get_tokenizer() __SCREAMING_SNAKE_CASE = self.get_rust_tokenizer() __SCREAMING_SNAKE_CASE = """I was born in 92000, and this is falsé.""" __SCREAMING_SNAKE_CASE = tokenizer.tokenize(__UpperCAmelCase) __SCREAMING_SNAKE_CASE = rust_tokenizer.tokenize(__UpperCAmelCase) self.assertListEqual(__UpperCAmelCase , __UpperCAmelCase) __SCREAMING_SNAKE_CASE = tokenizer.encode(__UpperCAmelCase , add_special_tokens=__UpperCAmelCase) __SCREAMING_SNAKE_CASE = rust_tokenizer.encode(__UpperCAmelCase , add_special_tokens=__UpperCAmelCase) self.assertListEqual(__UpperCAmelCase , __UpperCAmelCase) __SCREAMING_SNAKE_CASE = self.get_rust_tokenizer() __SCREAMING_SNAKE_CASE = tokenizer.encode(__UpperCAmelCase) __SCREAMING_SNAKE_CASE = rust_tokenizer.encode(__UpperCAmelCase) self.assertListEqual(__UpperCAmelCase , __UpperCAmelCase) @slow def snake_case_ ( self): __SCREAMING_SNAKE_CASE = {"""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. __SCREAMING_SNAKE_CASE = [ """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=__UpperCAmelCase , model_name="""camembert-base""" , revision="""3a0641d9a1aeb7e848a74299e7e4c4bca216b4cf""" , sequences=__UpperCAmelCase , )
354
"""simple docstring""" def _lowerCAmelCase ( UpperCamelCase_ , UpperCamelCase_ ): while b: __SCREAMING_SNAKE_CASE ,__SCREAMING_SNAKE_CASE = b, a % b return a def _lowerCAmelCase ( UpperCamelCase_ , UpperCamelCase_ ): return a if b == 0 else euclidean_gcd_recursive(UpperCamelCase_ , a % b ) def _lowerCAmelCase ( ): 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()
255
0
'''simple docstring''' import unittest from transformers import is_flax_available from transformers.testing_utils import require_flax, require_sentencepiece, require_tokenizers, require_torch, slow if is_flax_available(): import optax from flax.training.common_utils import onehot from transformers import AutoTokenizer, FlaxMTaForConditionalGeneration from transformers.models.ta.modeling_flax_ta import shift_tokens_right @require_torch @require_sentencepiece @require_tokenizers @require_flax class SCREAMING_SNAKE_CASE ( unittest.TestCase ): """simple docstring""" @slow def A ( self : List[str] ): """simple docstring""" UpperCamelCase = FlaxMTaForConditionalGeneration.from_pretrained('google/mt5-small' ) UpperCamelCase = AutoTokenizer.from_pretrained('google/mt5-small' ) UpperCamelCase = tokenizer('Hello there' , return_tensors='np' ).input_ids UpperCamelCase = tokenizer('Hi I am' , return_tensors='np' ).input_ids UpperCamelCase = shift_tokens_right(UpperCamelCase__ , model.config.pad_token_id , model.config.decoder_start_token_id ) UpperCamelCase = model(UpperCamelCase__ , decoder_input_ids=UpperCamelCase__ ).logits UpperCamelCase = optax.softmax_cross_entropy(UpperCamelCase__ , onehot(UpperCamelCase__ , logits.shape[-1] ) ).mean() UpperCamelCase = -(labels.shape[-1] * loss.item()) UpperCamelCase = -8_4.9_1_2_7 self.assertTrue(abs(mtf_score - EXPECTED_SCORE ) < 1E-4 )
28
'''simple docstring''' import json import os import shutil import warnings from argparse import ArgumentParser, Namespace from pathlib import Path from typing import List from ..utils import logging from . import BaseTransformersCLICommand try: from cookiecutter.main import cookiecutter __A : List[Any] = True except ImportError: __A : int = False __A : str = logging.get_logger(__name__) # pylint: disable=invalid-name def UpperCamelCase_ ( A__ : Namespace ): '''simple docstring''' return AddNewModelCommand(args.testing , args.testing_file , path=args.path ) class __snake_case ( _SCREAMING_SNAKE_CASE): """simple docstring""" @staticmethod def __lowercase ( lowerCamelCase : ArgumentParser ) -> int: lowerCAmelCase_ : Optional[int] = parser.add_parser("""add-new-model""" ) add_new_model_parser.add_argument("""--testing""" , action="""store_true""" , help="""If in testing mode.""" ) add_new_model_parser.add_argument("""--testing_file""" , type=lowerCamelCase , help="""Configuration file on which to run.""" ) add_new_model_parser.add_argument( """--path""" , type=lowerCamelCase , help="""Path to cookiecutter. Should only be used for testing purposes.""" ) add_new_model_parser.set_defaults(func=lowerCamelCase ) def __init__( self : List[str] , lowerCamelCase : bool , lowerCamelCase : str , lowerCamelCase : Any=None , *lowerCamelCase : List[str] ) -> Optional[Any]: lowerCAmelCase_ : int = testing lowerCAmelCase_ : Union[str, Any] = testing_file lowerCAmelCase_ : Tuple = path def __lowercase ( self : Tuple ) -> int: warnings.warn( """The command `transformers-cli add-new-model` is deprecated and will be removed in v5 of Transformers. """ """It is not actively maintained anymore, so might give a result that won't pass all tests and quality """ """checks, you should use `transformers-cli add-new-model-like` instead.""" ) if not _has_cookiecutter: raise ImportError( """Model creation dependencies are required to use the `add_new_model` command. Install them by running """ """the following at the root of your `transformers` clone:\n\n\t$ pip install -e .[modelcreation]\n""" ) # Ensure that there is no other `cookiecutter-template-xxx` directory in the current working directory lowerCAmelCase_ : int = [directory for directory in os.listdir() if """cookiecutter-template-""" == directory[:22]] if len(lowerCamelCase ) > 0: raise ValueError( """Several directories starting with `cookiecutter-template-` in current working directory. """ """Please clean your directory by removing all folders starting with `cookiecutter-template-` or """ """change your working directory.""" ) lowerCAmelCase_ : List[Any] = ( Path(lowerCamelCase ).parent.parent.parent.parent if self._path is None else Path(self._path ).parent.parent ) lowerCAmelCase_ : Dict = path_to_transformer_root / """templates""" / """adding_a_new_model""" # Execute cookiecutter if not self._testing: cookiecutter(str(lowerCamelCase ) ) else: with open(self._testing_file , """r""" ) as configuration_file: lowerCAmelCase_ : Tuple = json.load(lowerCamelCase ) cookiecutter( str(path_to_cookiecutter if self._path is None else self._path ) , no_input=lowerCamelCase , extra_context=lowerCamelCase , ) lowerCAmelCase_ : List[str] = [directory for directory in os.listdir() if """cookiecutter-template-""" in directory[:22]][0] # Retrieve configuration with open(directory + """/configuration.json""" , """r""" ) as configuration_file: lowerCAmelCase_ : Tuple = json.load(lowerCamelCase ) lowerCAmelCase_ : str = configuration["""lowercase_modelname"""] lowerCAmelCase_ : List[str] = configuration["""generate_tensorflow_pytorch_and_flax"""] os.remove(F'{directory}/configuration.json' ) lowerCAmelCase_ : Dict = """PyTorch""" in generate_tensorflow_pytorch_and_flax lowerCAmelCase_ : Optional[int] = """TensorFlow""" in generate_tensorflow_pytorch_and_flax lowerCAmelCase_ : List[str] = """Flax""" in generate_tensorflow_pytorch_and_flax lowerCAmelCase_ : Union[str, Any] = F'{path_to_transformer_root}/src/transformers/models/{lowercase_model_name}' os.makedirs(lowerCamelCase , exist_ok=lowerCamelCase ) os.makedirs(F'{path_to_transformer_root}/tests/models/{lowercase_model_name}' , exist_ok=lowerCamelCase ) # Tests require submodules as they have parent imports with open(F'{path_to_transformer_root}/tests/models/{lowercase_model_name}/__init__.py' , """w""" ): pass shutil.move( F'{directory}/__init__.py' , F'{model_dir}/__init__.py' , ) shutil.move( F'{directory}/configuration_{lowercase_model_name}.py' , F'{model_dir}/configuration_{lowercase_model_name}.py' , ) def remove_copy_lines(lowerCamelCase : Any ): with open(lowerCamelCase , """r""" ) as f: lowerCAmelCase_ : List[str] = f.readlines() with open(lowerCamelCase , """w""" ) as f: for line in lines: if "# Copied from transformers." not in line: f.write(lowerCamelCase ) if output_pytorch: if not self._testing: remove_copy_lines(F'{directory}/modeling_{lowercase_model_name}.py' ) shutil.move( F'{directory}/modeling_{lowercase_model_name}.py' , F'{model_dir}/modeling_{lowercase_model_name}.py' , ) shutil.move( F'{directory}/test_modeling_{lowercase_model_name}.py' , F'{path_to_transformer_root}/tests/models/{lowercase_model_name}/test_modeling_{lowercase_model_name}.py' , ) else: os.remove(F'{directory}/modeling_{lowercase_model_name}.py' ) os.remove(F'{directory}/test_modeling_{lowercase_model_name}.py' ) if output_tensorflow: if not self._testing: remove_copy_lines(F'{directory}/modeling_tf_{lowercase_model_name}.py' ) shutil.move( F'{directory}/modeling_tf_{lowercase_model_name}.py' , F'{model_dir}/modeling_tf_{lowercase_model_name}.py' , ) shutil.move( F'{directory}/test_modeling_tf_{lowercase_model_name}.py' , F'{path_to_transformer_root}/tests/models/{lowercase_model_name}/test_modeling_tf_{lowercase_model_name}.py' , ) else: os.remove(F'{directory}/modeling_tf_{lowercase_model_name}.py' ) os.remove(F'{directory}/test_modeling_tf_{lowercase_model_name}.py' ) if output_flax: if not self._testing: remove_copy_lines(F'{directory}/modeling_flax_{lowercase_model_name}.py' ) shutil.move( F'{directory}/modeling_flax_{lowercase_model_name}.py' , F'{model_dir}/modeling_flax_{lowercase_model_name}.py' , ) shutil.move( F'{directory}/test_modeling_flax_{lowercase_model_name}.py' , F'{path_to_transformer_root}/tests/models/{lowercase_model_name}/test_modeling_flax_{lowercase_model_name}.py' , ) else: os.remove(F'{directory}/modeling_flax_{lowercase_model_name}.py' ) os.remove(F'{directory}/test_modeling_flax_{lowercase_model_name}.py' ) shutil.move( F'{directory}/{lowercase_model_name}.md' , F'{path_to_transformer_root}/docs/source/en/model_doc/{lowercase_model_name}.md' , ) shutil.move( F'{directory}/tokenization_{lowercase_model_name}.py' , F'{model_dir}/tokenization_{lowercase_model_name}.py' , ) shutil.move( F'{directory}/tokenization_fast_{lowercase_model_name}.py' , F'{model_dir}/tokenization_{lowercase_model_name}_fast.py' , ) from os import fdopen, remove from shutil import copymode, move from tempfile import mkstemp def replace(lowerCamelCase : str , lowerCamelCase : str , lowerCamelCase : List[str] ): # Create temp file lowerCAmelCase_, lowerCAmelCase_ : int = mkstemp() lowerCAmelCase_ : List[Any] = False with fdopen(lowerCamelCase , """w""" ) as new_file: with open(lowerCamelCase ) as old_file: for line in old_file: new_file.write(lowerCamelCase ) if line_to_copy_below in line: lowerCAmelCase_ : List[str] = True for line_to_copy in lines_to_copy: new_file.write(lowerCamelCase ) if not line_found: raise ValueError(F'Line {line_to_copy_below} was not found in file.' ) # Copy the file permissions from the old file to the new file copymode(lowerCamelCase , lowerCamelCase ) # Remove original file remove(lowerCamelCase ) # Move new file move(lowerCamelCase , lowerCamelCase ) def skip_units(lowerCamelCase : Optional[int] ): return ( ("generating PyTorch" in line and not output_pytorch) or ("generating TensorFlow" in line and not output_tensorflow) or ("generating Flax" in line and not output_flax) ) def replace_in_files(lowerCamelCase : Any ): with open(lowerCamelCase ) as datafile: lowerCAmelCase_ : Dict = [] lowerCAmelCase_ : List[str] = False lowerCAmelCase_ : str = False for line in datafile: if "# To replace in: " in line and "##" not in line: lowerCAmelCase_ : Dict = line.split("""\"""" )[1] lowerCAmelCase_ : int = skip_units(lowerCamelCase ) elif "# Below: " in line and "##" not in line: lowerCAmelCase_ : Any = line.split("""\"""" )[1] lowerCAmelCase_ : Tuple = skip_units(lowerCamelCase ) elif "# End." in line and "##" not in line: if not skip_file and not skip_snippet: replace(lowerCamelCase , lowerCamelCase , lowerCamelCase ) lowerCAmelCase_ : Dict = [] elif "# Replace with" in line and "##" not in line: lowerCAmelCase_ : int = [] elif "##" not in line: lines_to_copy.append(lowerCamelCase ) remove(lowerCamelCase ) replace_in_files(F'{directory}/to_replace_{lowercase_model_name}.py' ) os.rmdir(lowerCamelCase )
120
0
_lowerCamelCase : List[Any] = { '''Pillow''': '''Pillow''', '''accelerate''': '''accelerate>=0.11.0''', '''compel''': '''compel==0.1.8''', '''black''': '''black~=23.1''', '''datasets''': '''datasets''', '''filelock''': '''filelock''', '''flax''': '''flax>=0.4.1''', '''hf-doc-builder''': '''hf-doc-builder>=0.3.0''', '''huggingface-hub''': '''huggingface-hub>=0.13.2''', '''requests-mock''': '''requests-mock==1.10.0''', '''importlib_metadata''': '''importlib_metadata''', '''invisible-watermark''': '''invisible-watermark''', '''isort''': '''isort>=5.5.4''', '''jax''': '''jax>=0.2.8,!=0.3.2''', '''jaxlib''': '''jaxlib>=0.1.65''', '''Jinja2''': '''Jinja2''', '''k-diffusion''': '''k-diffusion>=0.0.12''', '''torchsde''': '''torchsde''', '''note_seq''': '''note_seq''', '''librosa''': '''librosa''', '''numpy''': '''numpy''', '''omegaconf''': '''omegaconf''', '''parameterized''': '''parameterized''', '''protobuf''': '''protobuf>=3.20.3,<4''', '''pytest''': '''pytest''', '''pytest-timeout''': '''pytest-timeout''', '''pytest-xdist''': '''pytest-xdist''', '''ruff''': '''ruff>=0.0.241''', '''safetensors''': '''safetensors''', '''sentencepiece''': '''sentencepiece>=0.1.91,!=0.1.92''', '''scipy''': '''scipy''', '''onnx''': '''onnx''', '''regex''': '''regex!=2019.12.17''', '''requests''': '''requests''', '''tensorboard''': '''tensorboard''', '''torch''': '''torch>=1.4''', '''torchvision''': '''torchvision''', '''transformers''': '''transformers>=4.25.1''', '''urllib3''': '''urllib3<=2.0.0''', }
191
class lowerCamelCase : """simple docstring""" def __init__( self : int, _UpperCAmelCase : Dict, _UpperCAmelCase : str ) -> Optional[Any]: """simple docstring""" SCREAMING_SNAKE_CASE__ : str = name SCREAMING_SNAKE_CASE__ : Tuple = val def __str__( self : str ) -> List[Any]: """simple docstring""" return F'''{self.__class__.__name__}({self.name}, {self.val})''' def __lt__( self : str, _UpperCAmelCase : Dict ) -> Union[str, Any]: """simple docstring""" return self.val < other.val class lowerCamelCase : """simple docstring""" def __init__( self : Optional[Any], _UpperCAmelCase : str ) -> Tuple: """simple docstring""" SCREAMING_SNAKE_CASE__ : Optional[Any] = {} SCREAMING_SNAKE_CASE__ : str = {} SCREAMING_SNAKE_CASE__ : Union[str, Any] = self.build_heap(_UpperCAmelCase ) def __getitem__( self : Union[str, Any], _UpperCAmelCase : Any ) -> Dict: """simple docstring""" return self.get_value(_UpperCAmelCase ) def A_ ( self : int, _UpperCAmelCase : Optional[int] ) -> Any: """simple docstring""" return (idx - 1) // 2 def A_ ( self : Optional[Any], _UpperCAmelCase : Any ) -> Dict: """simple docstring""" return idx * 2 + 1 def A_ ( self : str, _UpperCAmelCase : List[str] ) -> Union[str, Any]: """simple docstring""" return idx * 2 + 2 def A_ ( self : Tuple, _UpperCAmelCase : int ) -> Optional[int]: """simple docstring""" return self.heap_dict[key] def A_ ( self : Optional[int], _UpperCAmelCase : Dict ) -> Optional[Any]: """simple docstring""" SCREAMING_SNAKE_CASE__ : List[str] = len(_UpperCAmelCase ) - 1 SCREAMING_SNAKE_CASE__ : Optional[int] = self.get_parent_idx(_UpperCAmelCase ) for idx, i in enumerate(_UpperCAmelCase ): SCREAMING_SNAKE_CASE__ : Optional[int] = idx SCREAMING_SNAKE_CASE__ : Optional[Any] = i.val for i in range(_UpperCAmelCase, -1, -1 ): self.sift_down(_UpperCAmelCase, _UpperCAmelCase ) return array def A_ ( self : List[Any], _UpperCAmelCase : Dict, _UpperCAmelCase : Tuple ) -> Tuple: """simple docstring""" while True: SCREAMING_SNAKE_CASE__ : str = self.get_left_child_idx(_UpperCAmelCase ) # noqa: E741 SCREAMING_SNAKE_CASE__ : Tuple = self.get_right_child_idx(_UpperCAmelCase ) SCREAMING_SNAKE_CASE__ : Tuple = idx if l < len(_UpperCAmelCase ) and array[l] < array[idx]: SCREAMING_SNAKE_CASE__ : List[Any] = l if r < len(_UpperCAmelCase ) and array[r] < array[smallest]: SCREAMING_SNAKE_CASE__ : int = r if smallest != idx: SCREAMING_SNAKE_CASE__ ,SCREAMING_SNAKE_CASE__ : Any = array[smallest], array[idx] ( ( SCREAMING_SNAKE_CASE__ ) ,( SCREAMING_SNAKE_CASE__ ) , ) : Optional[Any] = ( self.idx_of_element[array[smallest]], self.idx_of_element[array[idx]], ) SCREAMING_SNAKE_CASE__ : Optional[Any] = smallest else: break def A_ ( self : Union[str, Any], _UpperCAmelCase : str ) -> Any: """simple docstring""" SCREAMING_SNAKE_CASE__ : Any = self.get_parent_idx(_UpperCAmelCase ) while p >= 0 and self.heap[p] > self.heap[idx]: SCREAMING_SNAKE_CASE__ ,SCREAMING_SNAKE_CASE__ : Any = self.heap[idx], self.heap[p] SCREAMING_SNAKE_CASE__ ,SCREAMING_SNAKE_CASE__ : int = ( self.idx_of_element[self.heap[idx]], self.idx_of_element[self.heap[p]], ) SCREAMING_SNAKE_CASE__ : Dict = p SCREAMING_SNAKE_CASE__ : List[str] = self.get_parent_idx(_UpperCAmelCase ) def A_ ( self : str ) -> List[str]: """simple docstring""" return self.heap[0] def A_ ( self : Tuple ) -> List[Any]: """simple docstring""" SCREAMING_SNAKE_CASE__ ,SCREAMING_SNAKE_CASE__ : Optional[int] = self.heap[-1], self.heap[0] SCREAMING_SNAKE_CASE__ ,SCREAMING_SNAKE_CASE__ : List[Any] = ( self.idx_of_element[self.heap[-1]], self.idx_of_element[self.heap[0]], ) SCREAMING_SNAKE_CASE__ : Any = self.heap.pop() del self.idx_of_element[x] self.sift_down(0, self.heap ) return x def A_ ( self : Union[str, Any], _UpperCAmelCase : str ) -> Optional[Any]: """simple docstring""" self.heap.append(_UpperCAmelCase ) SCREAMING_SNAKE_CASE__ : str = len(self.heap ) - 1 SCREAMING_SNAKE_CASE__ : Optional[Any] = node.val self.sift_up(len(self.heap ) - 1 ) def A_ ( self : Optional[int] ) -> int: """simple docstring""" return len(self.heap ) == 0 def A_ ( self : Any, _UpperCAmelCase : Tuple, _UpperCAmelCase : str ) -> Dict: """simple docstring""" assert ( self.heap[self.idx_of_element[node]].val > new_value ), "newValue must be less that current value" SCREAMING_SNAKE_CASE__ : Tuple = new_value SCREAMING_SNAKE_CASE__ : List[Any] = new_value self.sift_up(self.idx_of_element[node] ) _lowerCamelCase : Tuple = Node('''R''', -1) _lowerCamelCase : int = Node('''B''', 6) _lowerCamelCase : str = Node('''A''', 3) _lowerCamelCase : Optional[Any] = Node('''X''', 1) _lowerCamelCase : str = Node('''E''', 4) # Use one of these two ways to generate Min-Heap # Generating Min-Heap from array _lowerCamelCase : int = MinHeap([r, b, a, x, e]) # Generating Min-Heap by Insert method # myMinHeap.insert(a) # myMinHeap.insert(b) # myMinHeap.insert(x) # myMinHeap.insert(r) # myMinHeap.insert(e) # Before print('''Min Heap - before decrease key''') for i in my_min_heap.heap: print(i) print('''Min Heap - After decrease key of node [B -> -17]''') my_min_heap.decrease_key(b, -1_7) # After for i in my_min_heap.heap: print(i) if __name__ == "__main__": import doctest doctest.testmod()
191
1
"""simple docstring""" import importlib import math import os from dataclasses import dataclass from enum import Enum from typing import Any, Dict, Optional, Tuple, Union import flax import jax.numpy as jnp from ..utils import BaseOutput __A = "scheduler_config.json" class lowerCamelCase__ ( lowerCamelCase_ ): a__ : Any = 1 a__ : int = 2 a__ : List[Any] = 3 a__ : Any = 4 a__ : str = 5 @dataclass class lowerCamelCase__ ( lowerCamelCase_ ): a__ : Optional[Any] = 42 class lowerCamelCase__ : a__ : Optional[int] = SCHEDULER_CONFIG_NAME a__ : List[Any] = ["""dtype"""] a__ : Tuple = [] a__ : List[str] = True @classmethod def lowerCamelCase_ ( cls , SCREAMING_SNAKE_CASE = None , SCREAMING_SNAKE_CASE = None , SCREAMING_SNAKE_CASE=False , **SCREAMING_SNAKE_CASE , ): """simple docstring""" snake_case , snake_case : Dict = cls.load_config( pretrained_model_name_or_path=SCREAMING_SNAKE_CASE , subfolder=SCREAMING_SNAKE_CASE , return_unused_kwargs=SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE , ) snake_case , snake_case : Optional[Any] = cls.from_config(SCREAMING_SNAKE_CASE , return_unused_kwargs=SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) if hasattr(SCREAMING_SNAKE_CASE , "create_state" ) and getattr(SCREAMING_SNAKE_CASE , "has_state" , SCREAMING_SNAKE_CASE ): snake_case : Dict = scheduler.create_state() if return_unused_kwargs: return scheduler, state, unused_kwargs return scheduler, state def lowerCamelCase_ ( self , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = False , **SCREAMING_SNAKE_CASE ): """simple docstring""" self.save_config(save_directory=SCREAMING_SNAKE_CASE , push_to_hub=SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) @property def lowerCamelCase_ ( self ): """simple docstring""" return self._get_compatibles() @classmethod def lowerCamelCase_ ( cls ): """simple docstring""" snake_case : Optional[Any] = list(set([cls.__name__] + cls._compatibles ) ) snake_case : Tuple = importlib.import_module(__name__.split("." )[0] ) snake_case : int = [ getattr(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) for c in compatible_classes_str if hasattr(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) ] return compatible_classes def UpperCamelCase__ ( lowercase__ : jnp.ndarray , lowercase__ : Tuple[int] ): assert len(_lowerCAmelCase ) >= x.ndim return jnp.broadcast_to(x.reshape(x.shape + (1,) * (len(_lowerCAmelCase ) - x.ndim) ) , _lowerCAmelCase ) def UpperCamelCase__ ( lowercase__ : int , lowercase__ : List[str]=0.9_99 , lowercase__ : Optional[int]=jnp.floataa ): def alpha_bar(lowercase__ : Tuple ): return math.cos((time_step + 0.0_08) / 1.0_08 * math.pi / 2 ) ** 2 snake_case : Optional[int] = [] for i in range(_lowerCAmelCase ): snake_case : Optional[Any] = i / num_diffusion_timesteps snake_case : Tuple = (i + 1) / num_diffusion_timesteps betas.append(min(1 - alpha_bar(_lowerCAmelCase ) / alpha_bar(_lowerCAmelCase ) , _lowerCAmelCase ) ) return jnp.array(_lowerCAmelCase , dtype=_lowerCAmelCase ) @flax.struct.dataclass class lowerCamelCase__ : a__ : Any = 42 a__ : List[Any] = 42 a__ : Union[str, Any] = 42 @classmethod def lowerCamelCase_ ( cls , SCREAMING_SNAKE_CASE ): """simple docstring""" snake_case : Optional[Any] = scheduler.config if config.trained_betas is not None: snake_case : Dict = jnp.asarray(config.trained_betas , dtype=scheduler.dtype ) elif config.beta_schedule == "linear": snake_case : Optional[Any] = jnp.linspace(config.beta_start , config.beta_end , config.num_train_timesteps , dtype=scheduler.dtype ) elif config.beta_schedule == "scaled_linear": # this schedule is very specific to the latent diffusion model. snake_case : Dict = ( jnp.linspace( config.beta_start**0.5 , config.beta_end**0.5 , config.num_train_timesteps , dtype=scheduler.dtype ) ** 2 ) elif config.beta_schedule == "squaredcos_cap_v2": # Glide cosine schedule snake_case : List[str] = betas_for_alpha_bar(config.num_train_timesteps , dtype=scheduler.dtype ) else: raise NotImplementedError( F'''beta_schedule {config.beta_schedule} is not implemented for scheduler {scheduler.__class__.__name__}''' ) snake_case : List[Any] = 1.0 - betas snake_case : Tuple = jnp.cumprod(SCREAMING_SNAKE_CASE , axis=0 ) return cls( alphas=SCREAMING_SNAKE_CASE , betas=SCREAMING_SNAKE_CASE , alphas_cumprod=SCREAMING_SNAKE_CASE , ) def UpperCamelCase__ ( lowercase__ : CommonSchedulerState , lowercase__ : jnp.ndarray , lowercase__ : jnp.ndarray , lowercase__ : jnp.ndarray ): snake_case : List[Any] = state.alphas_cumprod snake_case : str = alphas_cumprod[timesteps] ** 0.5 snake_case : List[Any] = sqrt_alpha_prod.flatten() snake_case : str = broadcast_to_shape_from_left(_lowerCAmelCase , original_samples.shape ) snake_case : List[str] = (1 - alphas_cumprod[timesteps]) ** 0.5 snake_case : Optional[int] = sqrt_one_minus_alpha_prod.flatten() snake_case : Dict = broadcast_to_shape_from_left(_lowerCAmelCase , original_samples.shape ) return sqrt_alpha_prod, sqrt_one_minus_alpha_prod def UpperCamelCase__ ( lowercase__ : CommonSchedulerState , lowercase__ : jnp.ndarray , lowercase__ : jnp.ndarray , lowercase__ : jnp.ndarray ): snake_case , snake_case : Optional[Any] = get_sqrt_alpha_prod(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) snake_case : List[str] = sqrt_alpha_prod * original_samples + sqrt_one_minus_alpha_prod * noise return noisy_samples def UpperCamelCase__ ( lowercase__ : CommonSchedulerState , lowercase__ : jnp.ndarray , lowercase__ : jnp.ndarray , lowercase__ : jnp.ndarray ): snake_case , snake_case : str = get_sqrt_alpha_prod(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) snake_case : List[str] = sqrt_alpha_prod * noise - sqrt_one_minus_alpha_prod * sample return velocity
148
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) _lowerCAmelCase : List[str] = [ "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 _UpperCamelCase ( unittest.TestCase ): def UpperCAmelCase_ ( self :Dict , lowerCamelCase :str , lowerCamelCase :bool , lowerCamelCase :str = None , lowerCamelCase :list = None ) -> Tuple: UpperCAmelCase__ = None UpperCAmelCase__ = os.path.abspath(os.path.join("examples" , "by_feature" ) ) UpperCAmelCase__ = os.path.abspath("examples" ) for item in os.listdir(lowerCamelCase ): if item not in EXCLUDE_EXAMPLES: UpperCAmelCase__ = os.path.join(lowerCamelCase , lowerCamelCase ) if os.path.isfile(lowerCamelCase ) and ".py" in item_path: with self.subTest( tested_script=lowerCamelCase , feature_script=lowerCamelCase , tested_section="main()" if parser_only else "training_function()" , ): UpperCAmelCase__ = compare_against_test( os.path.join(lowerCamelCase , lowerCamelCase ) , lowerCamelCase , lowerCamelCase , lowerCamelCase ) UpperCAmelCase__ = "\n".join(lowerCamelCase ) if special_strings is not None: for string in special_strings: UpperCAmelCase__ = diff.replace(lowerCamelCase , "" ) self.assertEqual(lowerCamelCase , "" ) def UpperCAmelCase_ ( self :List[str] ) -> Any: self.one_complete_example("complete_nlp_example.py" , lowerCamelCase ) self.one_complete_example("complete_nlp_example.py" , lowerCamelCase ) def UpperCAmelCase_ ( self :str ) -> int: UpperCAmelCase__ = os.path.abspath(os.path.join("examples" , "cv_example.py" ) ) UpperCAmelCase__ = [ " " * 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" , lowerCamelCase , lowerCamelCase , lowerCamelCase ) self.one_complete_example("complete_cv_example.py" , lowerCamelCase , lowerCamelCase , lowerCamelCase ) @mock.patch.dict(os.environ , {"""TESTING_MOCKED_DATALOADERS""": """1"""} ) class _UpperCamelCase ( lowerCAmelCase ): UpperCAmelCase_ = False @classmethod def UpperCAmelCase_ ( cls :List[Any] ) -> Any: super().setUpClass() UpperCAmelCase__ = tempfile.mkdtemp() UpperCAmelCase__ = os.path.join(cls._tmpdir , "default_config.yml" ) write_basic_config(save_location=cls.configPath ) UpperCAmelCase__ = ["accelerate", "launch", "--config_file", cls.configPath] @classmethod def UpperCAmelCase_ ( cls :Union[str, Any] ) -> Optional[int]: super().tearDownClass() shutil.rmtree(cls._tmpdir ) def UpperCAmelCase_ ( self :Dict ) -> Dict: UpperCAmelCase__ = 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 UpperCAmelCase_ ( self :Optional[int] ) -> Any: UpperCAmelCase__ = f''' examples/by_feature/checkpointing.py --checkpointing_steps 1 --output_dir {self.tmpdir} '''.split() UpperCAmelCase__ = run_command(self._launch_args + testargs ) self.assertTrue(os.path.exists(os.path.join(self.tmpdir , "step_2" ) ) ) def UpperCAmelCase_ ( self :Tuple ) -> Dict: UpperCAmelCase__ = f''' examples/by_feature/checkpointing.py --resume_from_checkpoint {os.path.join(self.tmpdir , "epoch_0" )} '''.split() UpperCAmelCase__ = run_command(self._launch_args + testargs , return_stdout=lowerCamelCase ) self.assertNotIn("epoch 0:" , lowerCamelCase ) self.assertIn("epoch 1:" , lowerCamelCase ) def UpperCAmelCase_ ( self :Dict ) -> int: UpperCAmelCase__ = f''' examples/by_feature/checkpointing.py --resume_from_checkpoint {os.path.join(self.tmpdir , "step_2" )} '''.split() UpperCAmelCase__ = run_command(self._launch_args + testargs , return_stdout=lowerCamelCase ) if torch.cuda.is_available(): UpperCAmelCase__ = torch.cuda.device_count() else: UpperCAmelCase__ = 1 if num_processes > 1: self.assertNotIn("epoch 0:" , lowerCamelCase ) self.assertIn("epoch 1:" , lowerCamelCase ) else: self.assertIn("epoch 0:" , lowerCamelCase ) self.assertIn("epoch 1:" , lowerCamelCase ) @slow def UpperCAmelCase_ ( self :Dict ) -> Optional[int]: UpperCAmelCase__ = "\n examples/by_feature/cross_validation.py\n --num_folds 2\n ".split() with mock.patch.dict(os.environ , {"TESTING_MOCKED_DATALOADERS": "0"} ): UpperCAmelCase__ = run_command(self._launch_args + testargs , return_stdout=lowerCamelCase ) UpperCAmelCase__ = re.findall("({.+})" , lowerCamelCase ) UpperCAmelCase__ = [r for r in results if "accuracy" in r][-1] UpperCAmelCase__ = ast.literal_eval(lowerCamelCase ) self.assertGreaterEqual(results["accuracy"] , 0.75 ) def UpperCAmelCase_ ( self :int ) -> Optional[int]: UpperCAmelCase__ = ["examples/by_feature/multi_process_metrics.py"] run_command(self._launch_args + testargs ) @require_trackers @mock.patch.dict(os.environ , {"WANDB_MODE": "offline"} ) def UpperCAmelCase_ ( self :List[Any] ) -> Dict: with tempfile.TemporaryDirectory() as tmpdir: UpperCAmelCase__ = 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(lowerCamelCase , "tracking" ) ) ) def UpperCAmelCase_ ( self :Any ) -> Dict: UpperCAmelCase__ = ["examples/by_feature/gradient_accumulation.py"] run_command(self._launch_args + testargs ) def UpperCAmelCase_ ( self :Any ) -> Optional[int]: UpperCAmelCase__ = ["examples/by_feature/local_sgd.py"] run_command(self._launch_args + testargs )
169
0
"""simple docstring""" from __future__ import annotations from random import choice def snake_case (A_ :Union[str, Any] ): '''simple docstring''' return choice(A_ ) def snake_case (A_ :list[int] , A_ :int ): '''simple docstring''' a : Union[str, Any] = random_pivot(A_ ) # partition based on pivot # linear time a : Tuple = [e for e in lst if e < pivot] a : Tuple = [e for e in lst if e > pivot] # if we get lucky, pivot might be the element we want. # we can easily see this: # small (elements smaller than k) # + pivot (kth element) # + big (elements larger than k) if len(A_ ) == k - 1: return pivot # pivot is in elements bigger than k elif len(A_ ) < k - 1: return kth_number(A_ , k - len(A_ ) - 1 ) # pivot is in elements smaller than k else: return kth_number(A_ , A_ ) if __name__ == "__main__": import doctest doctest.testmod()
186
"""simple docstring""" from __future__ import annotations import math import numpy as np from numpy.linalg import norm def snake_case (A_ :np.ndarray , A_ :np.ndarray ): '''simple docstring''' return math.sqrt(sum(pow(a - b , 2 ) for a, b in zip(A_ , A_ ) ) ) def snake_case (A_ :np.ndarray , A_ :np.ndarray ): '''simple docstring''' if dataset.ndim != value_array.ndim: a : Optional[Any] = ( 'Wrong input data\'s dimensions... ' f'''dataset : {dataset.ndim}, value_array : {value_array.ndim}''' ) raise ValueError(A_ ) try: if dataset.shape[1] != value_array.shape[1]: a : Optional[int] = ( 'Wrong input data\'s shape... ' f'''dataset : {dataset.shape[1]}, value_array : {value_array.shape[1]}''' ) raise ValueError(A_ ) except IndexError: if dataset.ndim != value_array.ndim: raise TypeError('Wrong shape' ) if dataset.dtype != value_array.dtype: a : Optional[Any] = ( 'Input data have different datatype... ' f'''dataset : {dataset.dtype}, value_array : {value_array.dtype}''' ) raise TypeError(A_ ) a : Tuple = [] for value in value_array: a : List[Any] = euclidean(A_ , dataset[0] ) a : int = dataset[0].tolist() for dataset_value in dataset[1:]: a : Optional[int] = euclidean(A_ , A_ ) if dist > temp_dist: a : List[str] = temp_dist a : int = dataset_value.tolist() answer.append([vector, dist] ) return answer def snake_case (A_ :np.ndarray , A_ :np.ndarray ): '''simple docstring''' return np.dot(A_ , A_ ) / (norm(A_ ) * norm(A_ )) if __name__ == "__main__": import doctest doctest.testmod()
186
1
'''simple docstring''' import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, StableDiffusionSAGPipeline, UNetaDConditionModel, ) from diffusers.utils import slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class _a ( __a , __a , unittest.TestCase ): __a : str = StableDiffusionSAGPipeline __a : List[Any] = TEXT_TO_IMAGE_PARAMS __a : Dict = TEXT_TO_IMAGE_BATCH_PARAMS __a : Optional[Any] = TEXT_TO_IMAGE_IMAGE_PARAMS __a : Tuple = TEXT_TO_IMAGE_IMAGE_PARAMS __a : int = False def A ( self : Optional[int] ): '''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_0085 , beta_end=0.012 , beta_schedule='''scaled_linear''' , clip_sample=lowercase , set_alpha_to_one=lowercase , ) torch.manual_seed(0 ) UpperCAmelCase = 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 ) 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=1_000 , ) UpperCAmelCase = CLIPTextModel(lowercase ) 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 A ( self : List[str] , lowercase : Dict , lowercase : Optional[int]=0 ): '''simple docstring''' if str(lowercase ).startswith('''mps''' ): UpperCAmelCase = torch.manual_seed(lowercase ) else: UpperCAmelCase = torch.Generator(device=lowercase ).manual_seed(lowercase ) UpperCAmelCase = { '''prompt''': '''.''', '''generator''': generator, '''num_inference_steps''': 2, '''guidance_scale''': 1.0, '''sag_scale''': 1.0, '''output_type''': '''numpy''', } return inputs def A ( self : List[str] ): '''simple docstring''' super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) @slow @require_torch_gpu class _a ( unittest.TestCase ): def A ( self : Any ): '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def A ( self : Union[str, Any] ): '''simple docstring''' UpperCAmelCase = StableDiffusionSAGPipeline.from_pretrained('''CompVis/stable-diffusion-v1-4''' ) UpperCAmelCase = sag_pipe.to(lowercase ) sag_pipe.set_progress_bar_config(disable=lowercase ) UpperCAmelCase = '''.''' UpperCAmelCase = torch.manual_seed(0 ) UpperCAmelCase = sag_pipe( [prompt] , generator=lowercase , guidance_scale=7.5 , sag_scale=1.0 , num_inference_steps=20 , output_type='''np''' ) UpperCAmelCase = output.images UpperCAmelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) UpperCAmelCase = np.array([0.1568, 0.1738, 0.1695, 0.1693, 0.1507, 0.1705, 0.1547, 0.1751, 0.1949] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5E-2 def A ( self : Dict ): '''simple docstring''' UpperCAmelCase = StableDiffusionSAGPipeline.from_pretrained('''stabilityai/stable-diffusion-2-1-base''' ) UpperCAmelCase = sag_pipe.to(lowercase ) sag_pipe.set_progress_bar_config(disable=lowercase ) UpperCAmelCase = '''.''' UpperCAmelCase = torch.manual_seed(0 ) UpperCAmelCase = sag_pipe( [prompt] , generator=lowercase , guidance_scale=7.5 , sag_scale=1.0 , num_inference_steps=20 , output_type='''np''' ) UpperCAmelCase = output.images UpperCAmelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) UpperCAmelCase = np.array([0.3459, 0.2876, 0.2537, 0.3002, 0.2671, 0.2160, 0.3026, 0.2262, 0.2371] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5E-2 def A ( self : Tuple ): '''simple docstring''' UpperCAmelCase = StableDiffusionSAGPipeline.from_pretrained('''stabilityai/stable-diffusion-2-1-base''' ) UpperCAmelCase = sag_pipe.to(lowercase ) sag_pipe.set_progress_bar_config(disable=lowercase ) UpperCAmelCase = '''.''' UpperCAmelCase = torch.manual_seed(0 ) UpperCAmelCase = sag_pipe( [prompt] , width=768 , height=512 , generator=lowercase , guidance_scale=7.5 , sag_scale=1.0 , num_inference_steps=20 , output_type='''np''' , ) UpperCAmelCase = output.images assert image.shape == (1, 512, 768, 3)
34
from __future__ import annotations lowercase__ : str = [ [-1, 0], # left [0, -1], # down [1, 0], # right [0, 1], # up ] def lowerCamelCase__ ( _A , _A , _A , _A , _A , ): '''simple docstring''' snake_case_ = [ [0 for col in range(len(grid[0] ) )] for row in range(len(_A ) ) ] # the reference grid snake_case_ = 1 snake_case_ = [ [0 for col in range(len(grid[0] ) )] for row in range(len(_A ) ) ] # the action grid snake_case_ = init[0] snake_case_ = init[1] snake_case_ = 0 snake_case_ = g + heuristic[x][y] # cost from starting cell to destination cell snake_case_ = [[f, g, x, y]] snake_case_ = False # flag that is set when search is complete snake_case_ = False # flag set if we can't find expand while not found and not resign: if len(_A ) == 0: raise ValueError("Algorithm is unable to find solution" ) else: # to choose the least costliest action so as to move closer to the goal cell.sort() cell.reverse() snake_case_ = cell.pop() snake_case_ = next_cell[2] snake_case_ = next_cell[3] snake_case_ = next_cell[1] if x == goal[0] and y == goal[1]: snake_case_ = True else: for i in range(len(_A ) ): # to try out different valid actions snake_case_ = x + DIRECTIONS[i][0] snake_case_ = y + DIRECTIONS[i][1] if xa >= 0 and xa < len(_A ) and ya >= 0 and ya < len(grid[0] ): if closed[xa][ya] == 0 and grid[xa][ya] == 0: snake_case_ = g + cost snake_case_ = ga + heuristic[xa][ya] cell.append([fa, ga, xa, ya] ) snake_case_ = 1 snake_case_ = i snake_case_ = [] snake_case_ = goal[0] snake_case_ = goal[1] invpath.append([x, y] ) # we get the reverse path from here while x != init[0] or y != init[1]: snake_case_ = x - DIRECTIONS[action[x][y]][0] snake_case_ = y - DIRECTIONS[action[x][y]][1] snake_case_ = xa snake_case_ = ya invpath.append([x, y] ) snake_case_ = [] for i in range(len(_A ) ): path.append(invpath[len(_A ) - 1 - i] ) return path, action if __name__ == "__main__": lowercase__ : Union[str, Any] = [ [0, 1, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles [0, 1, 0, 0, 0, 0], [0, 1, 0, 0, 1, 0], [0, 0, 0, 0, 1, 0], ] lowercase__ : Optional[Any] = [0, 0] # all coordinates are given in format [y,x] lowercase__ : Tuple = [len(grid) - 1, len(grid[0]) - 1] lowercase__ : Dict = 1 # the cost map which pushes the path closer to the goal lowercase__ : Any = [[0 for row in range(len(grid[0]))] for col in range(len(grid))] for i in range(len(grid)): for j in range(len(grid[0])): lowercase__ : Union[str, Any] = abs(i - goal[0]) + abs(j - goal[1]) if grid[i][j] == 1: # added extra penalty in the heuristic map lowercase__ : int = 99 lowercase__ , lowercase__ : Tuple = search(grid, init, goal, cost, heuristic) print("ACTION MAP") for i in range(len(action)): print(action[i]) for i in range(len(path)): print(path[i])
187
0
import torch import torch.nn as nn from transformers.modeling_utils import ModuleUtilsMixin from transformers.models.ta.modeling_ta import TaBlock, TaConfig, TaLayerNorm from ...configuration_utils import ConfigMixin, register_to_config from ...models import ModelMixin class __A ( a , a , a ): @register_to_config def __init__( self , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ = False , ): super().__init__() lowerCamelCase =nn.Embedding(UpperCAmelCase_ , UpperCAmelCase_ ) lowerCamelCase =nn.Embedding(UpperCAmelCase_ , UpperCAmelCase_ ) lowerCamelCase =False lowerCamelCase =nn.Dropout(p=UpperCAmelCase_ ) lowerCamelCase =TaConfig( vocab_size=UpperCAmelCase_ , d_model=UpperCAmelCase_ , num_heads=UpperCAmelCase_ , d_kv=UpperCAmelCase_ , d_ff=UpperCAmelCase_ , dropout_rate=UpperCAmelCase_ , feed_forward_proj=UpperCAmelCase_ , is_decoder=UpperCAmelCase_ , is_encoder_decoder=UpperCAmelCase_ , ) lowerCamelCase =nn.ModuleList() for lyr_num in range(UpperCAmelCase_ ): lowerCamelCase =TaBlock(UpperCAmelCase_ ) self.encoders.append(UpperCAmelCase_ ) lowerCamelCase =TaLayerNorm(UpperCAmelCase_ ) lowerCamelCase =nn.Dropout(p=UpperCAmelCase_ ) def _snake_case ( self , UpperCAmelCase_ , UpperCAmelCase_ ): lowerCamelCase =self.token_embedder(UpperCAmelCase_ ) lowerCamelCase =encoder_input_tokens.shape[1] lowerCamelCase =torch.arange(UpperCAmelCase_ , device=encoder_input_tokens.device ) x += self.position_encoding(UpperCAmelCase_ ) lowerCamelCase =self.dropout_pre(UpperCAmelCase_ ) # inverted the attention mask lowerCamelCase =encoder_input_tokens.size() lowerCamelCase =self.get_extended_attention_mask(UpperCAmelCase_ , UpperCAmelCase_ ) for lyr in self.encoders: lowerCamelCase =lyr(UpperCAmelCase_ , UpperCAmelCase_ )[0] lowerCamelCase =self.layer_norm(UpperCAmelCase_ ) return self.dropout_post(UpperCAmelCase_ ), encoder_inputs_mask
262
import argparse import os import re import packaging.version UpperCAmelCase__ : List[Any] ='''examples/''' UpperCAmelCase__ : List[str] ={ '''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'''), } UpperCAmelCase__ : List[Any] ={ '''init''': '''src/transformers/__init__.py''', '''setup''': '''setup.py''', } UpperCAmelCase__ : Union[str, Any] ='''README.md''' def _lowercase ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) -> str: 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 _lowercase ( _UpperCAmelCase ) -> int: 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 _lowercase ( _UpperCAmelCase , _UpperCAmelCase=False ) -> Any: for pattern, fname in REPLACE_FILES.items(): update_version_in_file(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) if not patch: update_version_in_examples(_UpperCAmelCase ) def _lowercase ( ) -> Dict: 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 _lowercase ( ) -> Optional[int]: 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 _lowercase ( _UpperCAmelCase=False ) -> List[str]: 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 _lowercase ( ) -> str: 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__": UpperCAmelCase__ : Optional[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.''') UpperCAmelCase__ : str =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()
262
1
'''simple docstring''' from .configuration_bert_masked import MaskedBertConfig from .modeling_bert_masked import ( MaskedBertForMultipleChoice, MaskedBertForQuestionAnswering, MaskedBertForSequenceClassification, MaskedBertForTokenClassification, MaskedBertModel, ) from .modules import *
215
'''simple docstring''' # using dfs for finding eulerian path traversal def snake_case_ ( lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_=None )-> List[str]: '''simple docstring''' _UpperCAmelCase : Any = (path or []) + [u] for v in graph[u]: if visited_edge[u][v] is False: _UpperCAmelCase ,_UpperCAmelCase : Tuple = True, True _UpperCAmelCase : List[Any] = dfs(lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ) return path def snake_case_ ( lowerCAmelCase_ , lowerCAmelCase_ )-> Optional[int]: '''simple docstring''' _UpperCAmelCase : Optional[Any] = 0 _UpperCAmelCase : Optional[int] = -1 for i in range(lowerCAmelCase_ ): if i not in graph.keys(): continue if len(graph[i] ) % 2 == 1: odd_degree_nodes += 1 _UpperCAmelCase : Optional[int] = i if odd_degree_nodes == 0: return 1, odd_node if odd_degree_nodes == 2: return 2, odd_node return 3, odd_node def snake_case_ ( lowerCAmelCase_ , lowerCAmelCase_ )-> List[Any]: '''simple docstring''' _UpperCAmelCase : str = [[False for _ in range(max_node + 1 )] for _ in range(max_node + 1 )] _UpperCAmelCase ,_UpperCAmelCase : int = check_circuit_or_path(lowerCAmelCase_ , lowerCAmelCase_ ) if check == 3: print("""graph is not Eulerian""" ) print("""no path""" ) return _UpperCAmelCase : Dict = 1 if check == 2: _UpperCAmelCase : Dict = odd_node print("""graph has a Euler path""" ) if check == 1: print("""graph has a Euler cycle""" ) _UpperCAmelCase : Dict = dfs(lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ) print(lowerCAmelCase_ ) def snake_case_ ( )-> Union[str, Any]: '''simple docstring''' _UpperCAmelCase : Any = {1: [2, 3, 4], 2: [1, 3], 3: [1, 2], 4: [1, 5], 5: [4]} _UpperCAmelCase : int = {1: [2, 3, 4, 5], 2: [1, 3], 3: [1, 2], 4: [1, 5], 5: [1, 4]} _UpperCAmelCase : Tuple = {1: [2, 3, 4], 2: [1, 3, 4], 3: [1, 2], 4: [1, 2, 5], 5: [4]} _UpperCAmelCase : List[Any] = {1: [2, 3], 2: [1, 3], 3: [1, 2]} _UpperCAmelCase : List[str] = { 1: [], 2: [] # all degree is zero } _UpperCAmelCase : Union[str, Any] = 10 check_euler(lowerCAmelCase_ , lowerCAmelCase_ ) check_euler(lowerCAmelCase_ , lowerCAmelCase_ ) check_euler(lowerCAmelCase_ , lowerCAmelCase_ ) check_euler(lowerCAmelCase_ , lowerCAmelCase_ ) check_euler(lowerCAmelCase_ , lowerCAmelCase_ ) if __name__ == "__main__": main()
215
1
def SCREAMING_SNAKE_CASE__ ( snake_case_ ) -> bool: """simple docstring""" a = n ** (1 / 3) return (val * val * val) == n if __name__ == "__main__": print(perfect_cube(27)) print(perfect_cube(4))
330
import pytest from datasets import inspect_metric, list_metrics, load_metric @pytest.fixture def SCREAMING_SNAKE_CASE__ ( snake_case_ ) -> List[str]: """simple docstring""" monkeypatch.setattr('''datasets.utils.deprecation_utils._emitted_deprecation_warnings''', set() ) @pytest.fixture def SCREAMING_SNAKE_CASE__ ( snake_case_ ) -> Optional[int]: """simple docstring""" class lowerCamelCase_ : def __init__( self : Dict ,__lowerCamelCase : List[str] ): '''simple docstring''' a = metric_id class lowerCamelCase_ : SCREAMING_SNAKE_CASE_ = [MetricMock(a_ ) for metric_id in ['accuracy', 'mse', 'precision', 'codeparrot/apps_metric']] def SCREAMING_SNAKE_CASE_ ( self : Dict ): '''simple docstring''' return self._metrics monkeypatch.setattr('''datasets.inspect.huggingface_hub''', HfhMock() ) @pytest.mark.parametrize( '''func, args''', [(load_metric, ('''metrics/mse''',)), (list_metrics, ()), (inspect_metric, ('''metrics/mse''', '''tmp_path'''))] ) def SCREAMING_SNAKE_CASE__ ( snake_case_, snake_case_, snake_case_, snake_case_, snake_case_ ) -> Tuple: """simple docstring""" if "tmp_path" in args: a = tuple(arg if arg != '''tmp_path''' else tmp_path for arg in args ) with pytest.warns(snake_case_, match='''https://huggingface.co/docs/evaluate''' ): func(*snake_case_ )
330
1
'''simple docstring''' from argparse import ArgumentParser, Namespace from ..utils import logging from . import BaseTransformersCLICommand def __UpperCamelCase ( UpperCAmelCase ): return ConvertCommand( args.model_type , args.tf_checkpoint , args.pytorch_dump_output , args.config , args.finetuning_task_name ) __a: Optional[int] = """ transformers can only be used from the commandline to convert TensorFlow models in PyTorch, In that case, it requires TensorFlow to be installed. Please see https://www.tensorflow.org/install/ for installation instructions. """ class UpperCAmelCase ( a__ ): '''simple docstring''' @staticmethod def _lowerCAmelCase( __lowerCAmelCase ) -> List[str]: lowercase__ : Optional[Any] = parser.add_parser( '''convert''' , help='''CLI tool to run convert model from original author checkpoints to Transformers PyTorch checkpoints.''' , ) train_parser.add_argument('''--model_type''' , type=__lowerCAmelCase , required=__lowerCAmelCase , help='''Model\'s type.''' ) train_parser.add_argument( '''--tf_checkpoint''' , type=__lowerCAmelCase , required=__lowerCAmelCase , help='''TensorFlow checkpoint path or folder.''' ) train_parser.add_argument( '''--pytorch_dump_output''' , type=__lowerCAmelCase , required=__lowerCAmelCase , help='''Path to the PyTorch saved model output.''' ) train_parser.add_argument('''--config''' , type=__lowerCAmelCase , default='''''' , help='''Configuration file path or folder.''' ) train_parser.add_argument( '''--finetuning_task_name''' , type=__lowerCAmelCase , default=__lowerCAmelCase , help='''Optional fine-tuning task name if the TF model was a finetuned model.''' , ) train_parser.set_defaults(func=__lowerCAmelCase ) def __init__( self , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , *__lowerCAmelCase , ) -> Dict: lowercase__ : int = logging.get_logger('''transformers-cli/converting''' ) self._logger.info(F"""Loading model {model_type}""" ) lowercase__ : Dict = model_type lowercase__ : Any = tf_checkpoint lowercase__ : Optional[Any] = pytorch_dump_output lowercase__ : Optional[int] = config lowercase__ : List[Any] = finetuning_task_name def _lowerCAmelCase( self ) -> List[str]: if self._model_type == "albert": try: from ..models.albert.convert_albert_original_tf_checkpoint_to_pytorch import ( convert_tf_checkpoint_to_pytorch, ) except ImportError: raise ImportError(__lowerCAmelCase ) convert_tf_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) elif self._model_type == "bert": try: from ..models.bert.convert_bert_original_tf_checkpoint_to_pytorch import ( convert_tf_checkpoint_to_pytorch, ) except ImportError: raise ImportError(__lowerCAmelCase ) convert_tf_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) elif self._model_type == "funnel": try: from ..models.funnel.convert_funnel_original_tf_checkpoint_to_pytorch import ( convert_tf_checkpoint_to_pytorch, ) except ImportError: raise ImportError(__lowerCAmelCase ) convert_tf_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) elif self._model_type == "t5": try: from ..models.ta.convert_ta_original_tf_checkpoint_to_pytorch import convert_tf_checkpoint_to_pytorch except ImportError: raise ImportError(__lowerCAmelCase ) convert_tf_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) elif self._model_type == "gpt": from ..models.openai.convert_openai_original_tf_checkpoint_to_pytorch import ( convert_openai_checkpoint_to_pytorch, ) convert_openai_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) elif self._model_type == "transfo_xl": try: from ..models.transfo_xl.convert_transfo_xl_original_tf_checkpoint_to_pytorch import ( convert_transfo_xl_checkpoint_to_pytorch, ) except ImportError: raise ImportError(__lowerCAmelCase ) if "ckpt" in self._tf_checkpoint.lower(): lowercase__ : Optional[Any] = self._tf_checkpoint lowercase__ : Tuple = '''''' else: lowercase__ : Any = self._tf_checkpoint lowercase__ : List[Any] = '''''' convert_transfo_xl_checkpoint_to_pytorch( __lowerCAmelCase , self._config , self._pytorch_dump_output , __lowerCAmelCase ) elif self._model_type == "gpt2": try: from ..models.gpta.convert_gpta_original_tf_checkpoint_to_pytorch import ( convert_gpta_checkpoint_to_pytorch, ) except ImportError: raise ImportError(__lowerCAmelCase ) convert_gpta_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) elif self._model_type == "xlnet": try: from ..models.xlnet.convert_xlnet_original_tf_checkpoint_to_pytorch import ( convert_xlnet_checkpoint_to_pytorch, ) except ImportError: raise ImportError(__lowerCAmelCase ) convert_xlnet_checkpoint_to_pytorch( self._tf_checkpoint , self._config , self._pytorch_dump_output , self._finetuning_task_name ) elif self._model_type == "xlm": from ..models.xlm.convert_xlm_original_pytorch_checkpoint_to_pytorch import ( convert_xlm_checkpoint_to_pytorch, ) convert_xlm_checkpoint_to_pytorch(self._tf_checkpoint , self._pytorch_dump_output ) elif self._model_type == "lxmert": from ..models.lxmert.convert_lxmert_original_tf_checkpoint_to_pytorch import ( convert_lxmert_checkpoint_to_pytorch, ) convert_lxmert_checkpoint_to_pytorch(self._tf_checkpoint , self._pytorch_dump_output ) elif self._model_type == "rembert": from ..models.rembert.convert_rembert_tf_checkpoint_to_pytorch import ( convert_rembert_tf_checkpoint_to_pytorch, ) convert_rembert_tf_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) else: raise ValueError( '''--model_type should be selected in the list [bert, gpt, gpt2, t5, transfo_xl, xlnet, xlm, lxmert]''' )
198
'''simple docstring''' from __future__ import annotations from random import random class UpperCAmelCase : '''simple docstring''' def __init__( self , __lowerCAmelCase = None ) -> str: lowercase__ : Any = value lowercase__ : Union[str, Any] = random() lowercase__ : Node | None = None lowercase__ : Node | None = None def __repr__( self ) -> str: from pprint import pformat if self.left is None and self.right is None: return F"""'{self.value}: {self.prior:.5}'""" else: return pformat( {F"""{self.value}: {self.prior:.5}""": (self.left, self.right)} , indent=1 ) def __str__( self ) -> str: lowercase__ : Any = str(self.value ) + ''' ''' lowercase__ : str = str(self.left or '''''' ) lowercase__ : List[str] = str(self.right or '''''' ) return value + left + right def __UpperCamelCase ( UpperCAmelCase , UpperCAmelCase ): if root is None: # None tree is split into 2 Nones return None, None elif root.value is None: return None, None else: if value < root.value: lowercase__ , lowercase__ : int = split(root.left , UpperCAmelCase ) return left, root else: lowercase__ , lowercase__ : Optional[Any] = split(root.right , UpperCAmelCase ) return root, right def __UpperCamelCase ( UpperCAmelCase , UpperCAmelCase ): if (not left) or (not right): # If one node is None, return the other return left or right elif left.prior < right.prior: lowercase__ : Any = merge(left.right , UpperCAmelCase ) return left else: lowercase__ : Dict = merge(UpperCAmelCase , right.left ) return right def __UpperCamelCase ( UpperCAmelCase , UpperCAmelCase ): lowercase__ : Dict = Node(UpperCAmelCase ) lowercase__ , lowercase__ : Tuple = split(UpperCAmelCase , UpperCAmelCase ) return merge(merge(UpperCAmelCase , UpperCAmelCase ) , UpperCAmelCase ) def __UpperCamelCase ( UpperCAmelCase , UpperCAmelCase ): lowercase__ , lowercase__ : Dict = split(UpperCAmelCase , value - 1 ) lowercase__ , lowercase__ : int = split(UpperCAmelCase , UpperCAmelCase ) return merge(UpperCAmelCase , UpperCAmelCase ) def __UpperCamelCase ( UpperCAmelCase ): if not root: # None return else: inorder(root.left ) print(root.value , end=''',''' ) inorder(root.right ) def __UpperCamelCase ( UpperCAmelCase , UpperCAmelCase ): for arg in args.split(): if arg[0] == "+": lowercase__ : Tuple = insert(UpperCAmelCase , int(arg[1:] ) ) elif arg[0] == "-": lowercase__ : Union[str, Any] = erase(UpperCAmelCase , int(arg[1:] ) ) else: print('''Unknown command''' ) return root def __UpperCamelCase ( ): lowercase__ : Tuple = None print( '''enter numbers to create a tree, + value to add value into treap, ''' '''- value to erase all nodes with value. \'q\' to quit. ''' ) lowercase__ : Dict = input() while args != "q": lowercase__ : Any = interact_treap(UpperCAmelCase , UpperCAmelCase ) print(UpperCAmelCase ) lowercase__ : List[Any] = input() print('''good by!''' ) if __name__ == "__main__": import doctest doctest.testmod() main()
198
1
import pytest from datasets.utils.sharding import _distribute_shards, _number_of_shards_in_gen_kwargs, _split_gen_kwargs @pytest.mark.parametrize( '''kwargs, expected''' , [ ({'''num_shards''': 0, '''max_num_jobs''': 1}, []), ({'''num_shards''': 10, '''max_num_jobs''': 1}, [range(10 )]), ({'''num_shards''': 10, '''max_num_jobs''': 10}, [range(_A , i + 1 ) for i in range(10 )]), ({'''num_shards''': 1, '''max_num_jobs''': 10}, [range(1 )]), ({'''num_shards''': 10, '''max_num_jobs''': 3}, [range(0 , 4 ), range(4 , 7 ), range(7 , 10 )]), ({'''num_shards''': 3, '''max_num_jobs''': 10}, [range(0 , 1 ), range(1 , 2 ), range(2 , 3 )]), ] , ) def __UpperCamelCase ( _A , _A ): lowerCAmelCase_ = _distribute_shards(**_A ) assert out == expected @pytest.mark.parametrize( '''gen_kwargs, max_num_jobs, expected''' , [ ({'''foo''': 0}, 10, [{'''foo''': 0}]), ({'''shards''': [0, 1, 2, 3]}, 1, [{'''shards''': [0, 1, 2, 3]}]), ({'''shards''': [0, 1, 2, 3]}, 4, [{'''shards''': [0]}, {'''shards''': [1]}, {'''shards''': [2]}, {'''shards''': [3]}]), ({'''shards''': [0, 1]}, 4, [{'''shards''': [0]}, {'''shards''': [1]}]), ({'''shards''': [0, 1, 2, 3]}, 2, [{'''shards''': [0, 1]}, {'''shards''': [2, 3]}]), ] , ) def __UpperCamelCase ( _A , _A , _A ): lowerCAmelCase_ = _split_gen_kwargs(_A , _A ) assert out == expected @pytest.mark.parametrize( '''gen_kwargs, expected''' , [ ({'''foo''': 0}, 1), ({'''shards''': [0]}, 1), ({'''shards''': [0, 1, 2, 3]}, 4), ({'''shards''': [0, 1, 2, 3], '''foo''': 0}, 4), ({'''shards''': [0, 1, 2, 3], '''other''': (0, 1)}, 4), ({'''shards''': [0, 1, 2, 3], '''shards2''': [0, 1]}, RuntimeError), ] , ) def __UpperCamelCase ( _A , _A ): if expected is RuntimeError: with pytest.raises(_A ): _number_of_shards_in_gen_kwargs(_A ) else: lowerCAmelCase_ = _number_of_shards_in_gen_kwargs(_A ) assert out == expected
167
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 A ( __UpperCAmelCase ): __snake_case = ['image_processor', 'tokenizer'] __snake_case = 'OwlViTImageProcessor' __snake_case = ('CLIPTokenizer', 'CLIPTokenizerFast') def __init__( self, UpperCamelCase__=None, UpperCamelCase__=None, **UpperCamelCase__ ): """simple docstring""" lowerCAmelCase_ = None if "feature_extractor" in kwargs: warnings.warn( '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`''' ''' instead.''', UpperCamelCase__, ) lowerCAmelCase_ = kwargs.pop('''feature_extractor''' ) lowerCAmelCase_ = 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__(UpperCamelCase__, UpperCamelCase__ ) def __call__( self, UpperCamelCase__=None, UpperCamelCase__=None, UpperCamelCase__=None, UpperCamelCase__="max_length", UpperCamelCase__="np", **UpperCamelCase__ ): """simple docstring""" 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(UpperCamelCase__, UpperCamelCase__ ) or (isinstance(UpperCamelCase__, UpperCamelCase__ ) and not isinstance(text[0], UpperCamelCase__ )): lowerCAmelCase_ = [self.tokenizer(UpperCamelCase__, padding=UpperCamelCase__, return_tensors=UpperCamelCase__, **UpperCamelCase__ )] elif isinstance(UpperCamelCase__, UpperCamelCase__ ) and isinstance(text[0], UpperCamelCase__ ): lowerCAmelCase_ = [] # Maximum number of queries across batch lowerCAmelCase_ = max([len(UpperCamelCase__ ) for t in text] ) # Pad all batch samples to max number of text queries for t in text: if len(UpperCamelCase__ ) != max_num_queries: lowerCAmelCase_ = t + [''' '''] * (max_num_queries - len(UpperCamelCase__ )) lowerCAmelCase_ = self.tokenizer(UpperCamelCase__, padding=UpperCamelCase__, return_tensors=UpperCamelCase__, **UpperCamelCase__ ) encodings.append(UpperCamelCase__ ) else: raise TypeError('''Input text should be a string, a list of strings or a nested list of strings''' ) if return_tensors == "np": lowerCAmelCase_ = np.concatenate([encoding['''input_ids'''] for encoding in encodings], axis=0 ) lowerCAmelCase_ = np.concatenate([encoding['''attention_mask'''] for encoding in encodings], axis=0 ) elif return_tensors == "jax" and is_flax_available(): import jax.numpy as jnp lowerCAmelCase_ = jnp.concatenate([encoding['''input_ids'''] for encoding in encodings], axis=0 ) lowerCAmelCase_ = jnp.concatenate([encoding['''attention_mask'''] for encoding in encodings], axis=0 ) elif return_tensors == "pt" and is_torch_available(): import torch lowerCAmelCase_ = torch.cat([encoding['''input_ids'''] for encoding in encodings], dim=0 ) lowerCAmelCase_ = torch.cat([encoding['''attention_mask'''] for encoding in encodings], dim=0 ) elif return_tensors == "tf" and is_tf_available(): import tensorflow as tf lowerCAmelCase_ = tf.stack([encoding['''input_ids'''] for encoding in encodings], axis=0 ) lowerCAmelCase_ = tf.stack([encoding['''attention_mask'''] for encoding in encodings], axis=0 ) else: raise ValueError('''Target return tensor type could not be returned''' ) lowerCAmelCase_ = BatchEncoding() lowerCAmelCase_ = input_ids lowerCAmelCase_ = attention_mask if query_images is not None: lowerCAmelCase_ = BatchEncoding() lowerCAmelCase_ = self.image_processor( UpperCamelCase__, return_tensors=UpperCamelCase__, **UpperCamelCase__ ).pixel_values lowerCAmelCase_ = query_pixel_values if images is not None: lowerCAmelCase_ = self.image_processor(UpperCamelCase__, return_tensors=UpperCamelCase__, **UpperCamelCase__ ) if text is not None and images is not None: lowerCAmelCase_ = image_features.pixel_values return encoding elif query_images is not None and images is not None: lowerCAmelCase_ = image_features.pixel_values return encoding elif text is not None or query_images is not None: return encoding else: return BatchEncoding(data=dict(**UpperCamelCase__ ), tensor_type=UpperCamelCase__ ) def SCREAMING_SNAKE_CASE__ ( self, *UpperCamelCase__, **UpperCamelCase__ ): """simple docstring""" return self.image_processor.post_process(*UpperCamelCase__, **UpperCamelCase__ ) def SCREAMING_SNAKE_CASE__ ( self, *UpperCamelCase__, **UpperCamelCase__ ): """simple docstring""" return self.image_processor.post_process_object_detection(*UpperCamelCase__, **UpperCamelCase__ ) def SCREAMING_SNAKE_CASE__ ( self, *UpperCamelCase__, **UpperCamelCase__ ): """simple docstring""" return self.image_processor.post_process_image_guided_detection(*UpperCamelCase__, **UpperCamelCase__ ) def SCREAMING_SNAKE_CASE__ ( self, *UpperCamelCase__, **UpperCamelCase__ ): """simple docstring""" return self.tokenizer.batch_decode(*UpperCamelCase__, **UpperCamelCase__ ) def SCREAMING_SNAKE_CASE__ ( self, *UpperCamelCase__, **UpperCamelCase__ ): """simple docstring""" return self.tokenizer.decode(*UpperCamelCase__, **UpperCamelCase__ ) @property def SCREAMING_SNAKE_CASE__ ( self ): """simple docstring""" warnings.warn( '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.''', UpperCamelCase__, ) return self.image_processor_class @property def SCREAMING_SNAKE_CASE__ ( self ): """simple docstring""" warnings.warn( '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.''', UpperCamelCase__, ) return self.image_processor
167
1
'''simple docstring''' def __UpperCamelCase ( UpperCAmelCase , UpperCAmelCase ): if mass < 0: raise ValueError('''The mass of a body cannot be negative''' ) return 0.5 * mass * abs(UpperCAmelCase ) * abs(UpperCAmelCase ) if __name__ == "__main__": import doctest doctest.testmod(verbose=True)
198
'''simple docstring''' import os from argparse import ArgumentParser from typing import List import torch.utils.data from datasets import Dataset, IterableDataset from datasets.distributed import split_dataset_by_node __a: Optional[int] = 4 __a: Optional[Any] = 3 class UpperCAmelCase ( a__ ): '''simple docstring''' pass def __UpperCamelCase ( UpperCAmelCase ): for shard in shards: for i in range(UpperCAmelCase ): yield {"i": i, "shard": shard} def __UpperCamelCase ( ): lowercase__ : Tuple = int(os.environ['''RANK'''] ) lowercase__ : List[str] = int(os.environ['''WORLD_SIZE'''] ) lowercase__ : Optional[Any] = ArgumentParser() parser.add_argument('''--streaming''' , type=UpperCAmelCase ) parser.add_argument('''--local_rank''' , type=UpperCAmelCase ) parser.add_argument('''--num_workers''' , type=UpperCAmelCase , default=0 ) lowercase__ : List[Any] = parser.parse_args() lowercase__ : List[str] = args.streaming lowercase__ : str = args.num_workers lowercase__ : Optional[int] = {'''shards''': [F"""shard_{shard_idx}""" for shard_idx in range(UpperCAmelCase )]} lowercase__ : Tuple = IterableDataset.from_generator(UpperCAmelCase , gen_kwargs=UpperCAmelCase ) if not streaming: lowercase__ : int = Dataset.from_list(list(UpperCAmelCase ) ) lowercase__ : str = split_dataset_by_node(UpperCAmelCase , rank=UpperCAmelCase , world_size=UpperCAmelCase ) lowercase__ : Optional[int] = torch.utils.data.DataLoader(UpperCAmelCase , num_workers=UpperCAmelCase ) lowercase__ : Dict = NUM_SHARDS * NUM_ITEMS_PER_SHARD lowercase__ : Optional[Any] = full_size // world_size expected_local_size += int(rank < (full_size % world_size) ) lowercase__ : Union[str, Any] = sum(1 for _ in dataloader ) if local_size != expected_local_size: raise FailedTestError(F"""local_size {local_size} != expected_local_size {expected_local_size}""" ) if __name__ == "__main__": main()
198
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available, is_vision_available, ) lowerCamelCase_ = { '''configuration_perceiver''': ['''PERCEIVER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''PerceiverConfig''', '''PerceiverOnnxConfig'''], '''tokenization_perceiver''': ['''PerceiverTokenizer'''], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase_ = ['''PerceiverFeatureExtractor'''] lowerCamelCase_ = ['''PerceiverImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase_ = [ '''PERCEIVER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''PerceiverForImageClassificationConvProcessing''', '''PerceiverForImageClassificationFourier''', '''PerceiverForImageClassificationLearned''', '''PerceiverForMaskedLM''', '''PerceiverForMultimodalAutoencoding''', '''PerceiverForOpticalFlow''', '''PerceiverForSequenceClassification''', '''PerceiverLayer''', '''PerceiverModel''', '''PerceiverPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_perceiver import PERCEIVER_PRETRAINED_CONFIG_ARCHIVE_MAP, PerceiverConfig, PerceiverOnnxConfig from .tokenization_perceiver import PerceiverTokenizer try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_perceiver import PerceiverFeatureExtractor from .image_processing_perceiver import PerceiverImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_perceiver import ( PERCEIVER_PRETRAINED_MODEL_ARCHIVE_LIST, PerceiverForImageClassificationConvProcessing, PerceiverForImageClassificationFourier, PerceiverForImageClassificationLearned, PerceiverForMaskedLM, PerceiverForMultimodalAutoencoding, PerceiverForOpticalFlow, PerceiverForSequenceClassification, PerceiverLayer, PerceiverModel, PerceiverPreTrainedModel, ) else: import sys lowerCamelCase_ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
34
from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging lowerCamelCase_ = logging.get_logger(__name__) lowerCamelCase_ = { '''google/mobilenet_v1_1.0_224''': '''https://huggingface.co/google/mobilenet_v1_1.0_224/resolve/main/config.json''', '''google/mobilenet_v1_0.75_192''': '''https://huggingface.co/google/mobilenet_v1_0.75_192/resolve/main/config.json''', # See all MobileNetV1 models at https://huggingface.co/models?filter=mobilenet_v1 } class __lowerCamelCase ( __snake_case ): lowerCamelCase_ : List[str] = 'mobilenet_v1' def __init__( self , lowerCamelCase=3 , lowerCamelCase=224 , lowerCamelCase=1.0 , lowerCamelCase=8 , lowerCamelCase="relu6" , lowerCamelCase=True , lowerCamelCase=0.999 , lowerCamelCase=0.02 , lowerCamelCase=0.001 , **lowerCamelCase , ) -> List[str]: super().__init__(**lowerCamelCase ) if depth_multiplier <= 0: raise ValueError("""depth_multiplier must be greater than zero.""" ) snake_case_ = num_channels snake_case_ = image_size snake_case_ = depth_multiplier snake_case_ = min_depth snake_case_ = hidden_act snake_case_ = tf_padding snake_case_ = classifier_dropout_prob snake_case_ = initializer_range snake_case_ = layer_norm_eps class __lowerCamelCase ( __snake_case ): lowerCamelCase_ : str = version.parse('1.11' ) @property def lowerCAmelCase_ ( self ) -> Mapping[str, Mapping[int, str]]: return OrderedDict([("""pixel_values""", {0: """batch"""})] ) @property def lowerCAmelCase_ ( self ) -> Mapping[str, Mapping[int, str]]: if self.task == "image-classification": return OrderedDict([("""logits""", {0: """batch"""})] ) else: return OrderedDict([("""last_hidden_state""", {0: """batch"""}), ("""pooler_output""", {0: """batch"""})] ) @property def lowerCAmelCase_ ( self ) -> float: return 1e-4
34
1
from ...configuration_utils import PretrainedConfig from ...utils import logging __lowerCamelCase = logging.get_logger(__name__) __lowerCamelCase = { """unc-nlp/lxmert-base-uncased""": """https://huggingface.co/unc-nlp/lxmert-base-uncased/resolve/main/config.json""", } class UpperCAmelCase ( A_ ): A__ : Any = "lxmert" A__ : Union[str, Any] = {} def __init__(self : List[str] , snake_case__ : Any=3_05_22 , snake_case__ : Optional[Any]=7_68 , snake_case__ : Any=12 , snake_case__ : Optional[int]=95_00 , snake_case__ : Optional[Any]=16_00 , snake_case__ : Union[str, Any]=4_00 , snake_case__ : Optional[int]=30_72 , snake_case__ : List[Any]="gelu" , snake_case__ : Dict=0.1 , snake_case__ : Union[str, Any]=0.1 , snake_case__ : Optional[int]=5_12 , snake_case__ : Any=2 , snake_case__ : Tuple=0.02 , snake_case__ : Any=1e-12 , snake_case__ : str=9 , snake_case__ : Optional[int]=5 , snake_case__ : List[str]=5 , snake_case__ : int=20_48 , snake_case__ : Optional[int]=4 , snake_case__ : str=6.67 , snake_case__ : Optional[int]=True , snake_case__ : Optional[Any]=True , snake_case__ : str=True , snake_case__ : List[str]=True , snake_case__ : int=True , snake_case__ : Any=True , snake_case__ : Any=True , **snake_case__ : Optional[int] , ) -> Dict: '''simple docstring''' snake_case : Tuple = vocab_size snake_case : str = hidden_size snake_case : List[Any] = num_attention_heads snake_case : Optional[Any] = hidden_act snake_case : Optional[int] = intermediate_size snake_case : List[str] = hidden_dropout_prob snake_case : Dict = attention_probs_dropout_prob snake_case : List[str] = max_position_embeddings snake_case : Optional[Any] = type_vocab_size snake_case : List[Any] = initializer_range snake_case : List[Any] = layer_norm_eps snake_case : Optional[int] = num_qa_labels snake_case : List[Any] = num_object_labels snake_case : int = num_attr_labels snake_case : Optional[int] = l_layers snake_case : Any = x_layers snake_case : Optional[Any] = r_layers snake_case : int = visual_feat_dim snake_case : Optional[Any] = visual_pos_dim snake_case : Optional[int] = visual_loss_normalizer snake_case : List[str] = task_matched snake_case : Optional[Any] = task_mask_lm snake_case : Tuple = task_obj_predict snake_case : int = task_qa snake_case : Any = visual_obj_loss snake_case : Optional[Any] = visual_attr_loss snake_case : Tuple = visual_feat_loss snake_case : str = {'''vision''': r_layers, '''cross_encoder''': x_layers, '''language''': l_layers} super().__init__(**_snake_case )
59
"""simple docstring""" from __future__ import annotations import unittest from transformers import is_tf_available, is_torch_available from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, SMALL_MODEL_IDENTIFIER, is_pt_tf_cross_test, slow if is_tf_available(): from transformers import ( AutoConfig, BertConfig, GPTaConfig, TaConfig, TFAutoModel, TFAutoModelForCausalLM, TFAutoModelForMaskedLM, TFAutoModelForPreTraining, TFAutoModelForQuestionAnswering, TFAutoModelForSeqaSeqLM, TFAutoModelForSequenceClassification, TFAutoModelWithLMHead, TFBertForMaskedLM, TFBertForPreTraining, TFBertForQuestionAnswering, TFBertForSequenceClassification, TFBertModel, TFGPTaLMHeadModel, TFRobertaForMaskedLM, TFTaForConditionalGeneration, ) from transformers.models.bert.modeling_tf_bert import TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST from transformers.models.gpta.modeling_tf_gpta import TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST from transformers.models.ta.modeling_tf_ta import TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST if is_torch_available(): from transformers import ( AutoModel, AutoModelForCausalLM, AutoModelForMaskedLM, AutoModelForPreTraining, AutoModelForQuestionAnswering, AutoModelForSeqaSeqLM, AutoModelForSequenceClassification, AutoModelWithLMHead, BertForMaskedLM, BertForPreTraining, BertForQuestionAnswering, BertForSequenceClassification, BertModel, GPTaLMHeadModel, RobertaForMaskedLM, TaForConditionalGeneration, ) @is_pt_tf_cross_test class __A ( unittest.TestCase ): '''simple docstring''' @slow def UpperCAmelCase ( self : Optional[Any] ) -> Union[str, Any]: """simple docstring""" for model_name in ["bert-base-uncased"]: lowercase__ : Tuple = AutoConfig.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) lowercase__ : Dict = TFAutoModel.from_pretrained(_snake_case ,from_pt=_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) lowercase__ : List[str] = AutoModel.from_pretrained(_snake_case ,from_tf=_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) @slow def UpperCAmelCase ( self : Union[str, Any] ) -> Tuple: """simple docstring""" for model_name in ["bert-base-uncased"]: lowercase__ : Dict = AutoConfig.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) lowercase__ : str = TFAutoModelForPreTraining.from_pretrained(_snake_case ,from_pt=_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) lowercase__ : Optional[Any] = AutoModelForPreTraining.from_pretrained(_snake_case ,from_tf=_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) @slow def UpperCAmelCase ( self : Tuple ) -> Dict: """simple docstring""" for model_name in TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowercase__ : Any = AutoConfig.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) lowercase__ : List[str] = TFAutoModelForCausalLM.from_pretrained(_snake_case ,from_pt=_snake_case ) lowercase__ , lowercase__ : Optional[Any] = TFAutoModelForCausalLM.from_pretrained( _snake_case ,output_loading_info=_snake_case ,from_pt=_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) lowercase__ : Union[str, Any] = AutoModelForCausalLM.from_pretrained(_snake_case ,from_tf=_snake_case ) lowercase__ , lowercase__ : Optional[Any] = AutoModelForCausalLM.from_pretrained( _snake_case ,output_loading_info=_snake_case ,from_tf=_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) @slow def UpperCAmelCase ( self : Any ) -> Tuple: """simple docstring""" for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowercase__ : Any = AutoConfig.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) lowercase__ : Optional[Any] = TFAutoModelWithLMHead.from_pretrained(_snake_case ,from_pt=_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) lowercase__ : Any = AutoModelWithLMHead.from_pretrained(_snake_case ,from_tf=_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) @slow def UpperCAmelCase ( self : List[str] ) -> Optional[Any]: """simple docstring""" for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowercase__ : str = AutoConfig.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) lowercase__ : Union[str, Any] = TFAutoModelForMaskedLM.from_pretrained(_snake_case ,from_pt=_snake_case ) lowercase__ , lowercase__ : str = TFAutoModelForMaskedLM.from_pretrained( _snake_case ,output_loading_info=_snake_case ,from_pt=_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) lowercase__ : List[str] = AutoModelForMaskedLM.from_pretrained(_snake_case ,from_tf=_snake_case ) lowercase__ , lowercase__ : Any = AutoModelForMaskedLM.from_pretrained( _snake_case ,output_loading_info=_snake_case ,from_tf=_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) @slow def UpperCAmelCase ( self : List[Any] ) -> Dict: """simple docstring""" for model_name in TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowercase__ : Union[str, Any] = AutoConfig.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) lowercase__ : List[str] = TFAutoModelForSeqaSeqLM.from_pretrained(_snake_case ,from_pt=_snake_case ) lowercase__ , lowercase__ : List[str] = TFAutoModelForSeqaSeqLM.from_pretrained( _snake_case ,output_loading_info=_snake_case ,from_pt=_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) lowercase__ : Any = AutoModelForSeqaSeqLM.from_pretrained(_snake_case ,from_tf=_snake_case ) lowercase__ , lowercase__ : Optional[int] = AutoModelForSeqaSeqLM.from_pretrained( _snake_case ,output_loading_info=_snake_case ,from_tf=_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) @slow def UpperCAmelCase ( self : List[str] ) -> Union[str, Any]: """simple docstring""" for model_name in ["bert-base-uncased"]: lowercase__ : Tuple = AutoConfig.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) lowercase__ : Any = TFAutoModelForSequenceClassification.from_pretrained(_snake_case ,from_pt=_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) lowercase__ : Union[str, Any] = AutoModelForSequenceClassification.from_pretrained(_snake_case ,from_tf=_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) @slow def UpperCAmelCase ( self : Tuple ) -> Optional[Any]: """simple docstring""" for model_name in ["bert-base-uncased"]: lowercase__ : List[Any] = AutoConfig.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) lowercase__ : str = TFAutoModelForQuestionAnswering.from_pretrained(_snake_case ,from_pt=_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) lowercase__ : Any = AutoModelForQuestionAnswering.from_pretrained(_snake_case ,from_tf=_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) def UpperCAmelCase ( self : Dict ) -> Any: """simple docstring""" lowercase__ : Optional[Any] = TFAutoModelWithLMHead.from_pretrained(_snake_case ,from_pt=_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) self.assertEqual(model.num_parameters() ,14_410 ) self.assertEqual(model.num_parameters(only_trainable=_snake_case ) ,14_410 ) lowercase__ : Union[str, Any] = AutoModelWithLMHead.from_pretrained(_snake_case ,from_tf=_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) self.assertEqual(model.num_parameters() ,14_410 ) self.assertEqual(model.num_parameters(only_trainable=_snake_case ) ,14_410 ) def UpperCAmelCase ( self : int ) -> List[Any]: """simple docstring""" lowercase__ : List[Any] = TFAutoModelWithLMHead.from_pretrained(_snake_case ,from_pt=_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) self.assertEqual(model.num_parameters() ,14_410 ) self.assertEqual(model.num_parameters(only_trainable=_snake_case ) ,14_410 ) lowercase__ : int = AutoModelWithLMHead.from_pretrained(_snake_case ,from_tf=_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) self.assertEqual(model.num_parameters() ,14_410 ) self.assertEqual(model.num_parameters(only_trainable=_snake_case ) ,14_410 )
16
0
"""simple docstring""" UpperCAmelCase_ : int = """ # Transformers installation ! pip install transformers datasets # To install from source instead of the last release, comment the command above and uncomment the following one. # ! pip install git+https://github.com/huggingface/transformers.git """ UpperCAmelCase_ : Optional[Any] = [{"""type""": """code""", """content""": INSTALL_CONTENT}] UpperCAmelCase_ : Tuple = { """{processor_class}""": """FakeProcessorClass""", """{model_class}""": """FakeModelClass""", """{object_class}""": """FakeObjectClass""", }
318
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCAmelCase_ : Dict = logging.get_logger(__name__) UpperCAmelCase_ : List[str] = { """RWKV/rwkv-4-169m-pile""": """https://huggingface.co/RWKV/rwkv-4-169m-pile/resolve/main/config.json""", """RWKV/rwkv-4-430m-pile""": """https://huggingface.co/RWKV/rwkv-4-430m-pile/resolve/main/config.json""", """RWKV/rwkv-4-1b5-pile""": """https://huggingface.co/RWKV/rwkv-4-1b5-pile/resolve/main/config.json""", """RWKV/rwkv-4-3b-pile""": """https://huggingface.co/RWKV/rwkv-4-3b-pile/resolve/main/config.json""", """RWKV/rwkv-4-7b-pile""": """https://huggingface.co/RWKV/rwkv-4-7b-pile/resolve/main/config.json""", """RWKV/rwkv-4-14b-pile""": """https://huggingface.co/RWKV/rwkv-4-14b-pile/resolve/main/config.json""", """RWKV/rwkv-raven-1b5""": """https://huggingface.co/RWKV/rwkv-raven-1b5/resolve/main/config.json""", """RWKV/rwkv-raven-3b""": """https://huggingface.co/RWKV/rwkv-raven-3b/resolve/main/config.json""", """RWKV/rwkv-raven-7b""": """https://huggingface.co/RWKV/rwkv-raven-7b/resolve/main/config.json""", """RWKV/rwkv-raven-14b""": """https://huggingface.co/RWKV/rwkv-raven-14b/resolve/main/config.json""", } class lowerCAmelCase__ ( UpperCAmelCase__ ): '''simple docstring''' __UpperCamelCase = "rwkv" __UpperCamelCase = {"max_position_embeddings": "context_length"} def __init__( self : Union[str, Any] , lowercase_ : Any=50277 , lowercase_ : str=1024 , lowercase_ : List[str]=4096 , lowercase_ : Optional[Any]=32 , lowercase_ : Any=None , lowercase_ : Any=None , lowercase_ : List[Any]=1e-5 , lowercase_ : Union[str, Any]=0 , lowercase_ : Union[str, Any]=0 , lowercase_ : int=6 , lowercase_ : Tuple=False , lowercase_ : Any=True , **lowercase_ : Any , ): '''simple docstring''' SCREAMING_SNAKE_CASE_ : Union[str, Any] = vocab_size SCREAMING_SNAKE_CASE_ : Any = context_length SCREAMING_SNAKE_CASE_ : int = hidden_size SCREAMING_SNAKE_CASE_ : int = num_hidden_layers SCREAMING_SNAKE_CASE_ : List[str] = attention_hidden_size if attention_hidden_size is not None else hidden_size SCREAMING_SNAKE_CASE_ : int = intermediate_size if intermediate_size is not None else 4 * hidden_size SCREAMING_SNAKE_CASE_ : int = layer_norm_epsilon SCREAMING_SNAKE_CASE_ : Optional[int] = rescale_every SCREAMING_SNAKE_CASE_ : Dict = use_cache SCREAMING_SNAKE_CASE_ : Dict = bos_token_id SCREAMING_SNAKE_CASE_ : Any = eos_token_id super().__init__( tie_word_embeddings=lowercase_ , bos_token_id=lowercase_ , eos_token_id=lowercase_ , **lowercase_)
318
1
def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> int: lowercase , lowercase : Union[str, Any] = len(SCREAMING_SNAKE_CASE__ ), len(grid[0] ) if ( min(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) < 0 or row == row_length or col == col_length or (row, col) in visit or grid[row][col] == 1 ): return 0 if row == row_length - 1 and col == col_length - 1: return 1 visit.add((row, col) ) lowercase : str = 0 count += depth_first_search(SCREAMING_SNAKE_CASE__ , row + 1 , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) count += depth_first_search(SCREAMING_SNAKE_CASE__ , row - 1 , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) count += depth_first_search(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , col + 1 , SCREAMING_SNAKE_CASE__ ) count += depth_first_search(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , col - 1 , SCREAMING_SNAKE_CASE__ ) visit.remove((row, col) ) return count if __name__ == "__main__": import doctest doctest.testmod()
20
"""simple docstring""" def lowercase__ ( _UpperCAmelCase ) -> None: '''simple docstring''' lowercase : Union[str, Any] = generate_pascal_triangle(_UpperCAmelCase ) for row_idx in range(_UpperCAmelCase ): # Print left spaces for _ in range(num_rows - row_idx - 1 ): print(end=' ' ) # Print row values for col_idx in range(row_idx + 1 ): if col_idx != row_idx: print(triangle[row_idx][col_idx] , end=' ' ) else: print(triangle[row_idx][col_idx] , end='' ) print() def lowercase__ ( _UpperCAmelCase ) -> list[list[int]]: '''simple docstring''' if not isinstance(_UpperCAmelCase , _UpperCAmelCase ): raise TypeError('The input value of \'num_rows\' should be \'int\'' ) if num_rows == 0: return [] elif num_rows < 0: raise ValueError( 'The input value of \'num_rows\' should be greater than or equal to 0' ) lowercase : list[list[int]] = [] for current_row_idx in range(_UpperCAmelCase ): lowercase : Optional[Any] = populate_current_row(_UpperCAmelCase , _UpperCAmelCase ) triangle.append(_UpperCAmelCase ) return triangle def lowercase__ ( _UpperCAmelCase , _UpperCAmelCase ) -> list[int]: '''simple docstring''' lowercase : Union[str, Any] = [-1] * (current_row_idx + 1) # first and last elements of current row are equal to 1 lowercase , lowercase : List[str] = 1, 1 for current_col_idx in range(1 , _UpperCAmelCase ): calculate_current_element( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) return current_row def lowercase__ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , ) -> None: '''simple docstring''' lowercase : List[str] = triangle[current_row_idx - 1][current_col_idx - 1] lowercase : Optional[Any] = triangle[current_row_idx - 1][current_col_idx] lowercase : Any = above_to_left_elt + above_to_right_elt def lowercase__ ( _UpperCAmelCase ) -> list[list[int]]: '''simple docstring''' if not isinstance(_UpperCAmelCase , _UpperCAmelCase ): raise TypeError('The input value of \'num_rows\' should be \'int\'' ) if num_rows == 0: return [] elif num_rows < 0: raise ValueError( 'The input value of \'num_rows\' should be greater than or equal to 0' ) lowercase : list[list[int]] = [[1]] for row_index in range(1 , _UpperCAmelCase ): lowercase : List[str] = [0] + result[-1] + [0] lowercase : List[Any] = row_index + 1 # Calculate the number of distinct elements in a row lowercase : Union[str, Any] = sum(divmod(_UpperCAmelCase , 2 ) ) lowercase : Tuple = [ temp_row[i - 1] + temp_row[i] for i in range(1 , distinct_elements + 1 ) ] lowercase : str = row_first_half[: (row_index + 1) // 2] row_second_half.reverse() lowercase : List[Any] = row_first_half + row_second_half result.append(_UpperCAmelCase ) return result def lowercase__ ( ) -> None: '''simple docstring''' from collections.abc import Callable from timeit import timeit def benchmark_a_function(_UpperCAmelCase , _UpperCAmelCase ) -> None: lowercase : Dict = f'''{func.__name__}({value})''' lowercase : Optional[Any] = timeit(f'''__main__.{call}''' , setup='import __main__' ) # print(f"{call:38} = {func(value)} -- {timing:.4f} seconds") print(f'''{call:38} -- {timing:.4f} seconds''' ) for value in range(15 ): # (1, 7, 14): for func in (generate_pascal_triangle, generate_pascal_triangle_optimized): benchmark_a_function(_UpperCAmelCase , _UpperCAmelCase ) print() if __name__ == "__main__": import doctest doctest.testmod() benchmark()
255
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available lowerCAmelCase__ = { '''configuration_mobilenet_v2''': [ '''MOBILENET_V2_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''MobileNetV2Config''', '''MobileNetV2OnnxConfig''', ], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = ['''MobileNetV2FeatureExtractor'''] lowerCAmelCase__ = ['''MobileNetV2ImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ '''MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST''', '''MobileNetV2ForImageClassification''', '''MobileNetV2ForSemanticSegmentation''', '''MobileNetV2Model''', '''MobileNetV2PreTrainedModel''', '''load_tf_weights_in_mobilenet_v2''', ] if TYPE_CHECKING: from .configuration_mobilenet_va import ( MOBILENET_V2_PRETRAINED_CONFIG_ARCHIVE_MAP, MobileNetVaConfig, MobileNetVaOnnxConfig, ) try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_mobilenet_va import MobileNetVaFeatureExtractor from .image_processing_mobilenet_va import MobileNetVaImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mobilenet_va import ( MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST, MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation, MobileNetVaModel, MobileNetVaPreTrainedModel, load_tf_weights_in_mobilenet_va, ) else: import sys lowerCAmelCase__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
358
"""simple docstring""" import tempfile import torch from diffusers import ( DEISMultistepScheduler, DPMSolverMultistepScheduler, DPMSolverSinglestepScheduler, UniPCMultistepScheduler, ) from .test_schedulers import SchedulerCommonTest class SCREAMING_SNAKE_CASE__ ( lowercase ): """simple docstring""" a : Optional[int] =(DEISMultistepScheduler,) a : str =(("num_inference_steps", 25),) def lowercase__ ( self , **snake_case__ ): """simple docstring""" lowerCAmelCase : Tuple = { "num_train_timesteps": 1_000, "beta_start": 0.0001, "beta_end": 0.02, "beta_schedule": "linear", "solver_order": 2, } config.update(**snake_case__ ) return config def lowercase__ ( self , snake_case__=0 , **snake_case__ ): """simple docstring""" lowerCAmelCase : List[str] = dict(self.forward_default_kwargs ) lowerCAmelCase : List[str] = kwargs.pop("num_inference_steps" , snake_case__ ) lowerCAmelCase : List[str] = self.dummy_sample lowerCAmelCase : int = 0.1 * sample lowerCAmelCase : Tuple = [residual + 0.2, residual + 0.15, residual + 0.10] for scheduler_class in self.scheduler_classes: lowerCAmelCase : Any = self.get_scheduler_config(**snake_case__ ) lowerCAmelCase : List[str] = scheduler_class(**snake_case__ ) scheduler.set_timesteps(snake_case__ ) # copy over dummy past residuals lowerCAmelCase : str = dummy_past_residuals[: scheduler.config.solver_order] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(snake_case__ ) lowerCAmelCase : List[str] = scheduler_class.from_pretrained(snake_case__ ) new_scheduler.set_timesteps(snake_case__ ) # copy over dummy past residuals lowerCAmelCase : Optional[int] = dummy_past_residuals[: new_scheduler.config.solver_order] lowerCAmelCase , lowerCAmelCase : Union[str, Any] = sample, sample for t in range(snake_case__ , time_step + scheduler.config.solver_order + 1 ): lowerCAmelCase : Any = scheduler.step(snake_case__ , snake_case__ , snake_case__ , **snake_case__ ).prev_sample lowerCAmelCase : str = new_scheduler.step(snake_case__ , snake_case__ , snake_case__ , **snake_case__ ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1e-5, "Scheduler outputs are not identical" def lowercase__ ( self ): """simple docstring""" pass def lowercase__ ( self , snake_case__=0 , **snake_case__ ): """simple docstring""" lowerCAmelCase : Dict = dict(self.forward_default_kwargs ) lowerCAmelCase : List[str] = kwargs.pop("num_inference_steps" , snake_case__ ) lowerCAmelCase : List[str] = self.dummy_sample lowerCAmelCase : Optional[int] = 0.1 * sample lowerCAmelCase : List[Any] = [residual + 0.2, residual + 0.15, residual + 0.10] for scheduler_class in self.scheduler_classes: lowerCAmelCase : int = self.get_scheduler_config() lowerCAmelCase : Any = scheduler_class(**snake_case__ ) scheduler.set_timesteps(snake_case__ ) # copy over dummy past residuals (must be after setting timesteps) lowerCAmelCase : Union[str, Any] = dummy_past_residuals[: scheduler.config.solver_order] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(snake_case__ ) lowerCAmelCase : Any = scheduler_class.from_pretrained(snake_case__ ) # copy over dummy past residuals new_scheduler.set_timesteps(snake_case__ ) # copy over dummy past residual (must be after setting timesteps) lowerCAmelCase : Optional[int] = dummy_past_residuals[: new_scheduler.config.solver_order] lowerCAmelCase : Union[str, Any] = scheduler.step(snake_case__ , snake_case__ , snake_case__ , **snake_case__ ).prev_sample lowerCAmelCase : int = new_scheduler.step(snake_case__ , snake_case__ , snake_case__ , **snake_case__ ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1e-5, "Scheduler outputs are not identical" def lowercase__ ( self , snake_case__=None , **snake_case__ ): """simple docstring""" if scheduler is None: lowerCAmelCase : List[str] = self.scheduler_classes[0] lowerCAmelCase : Optional[int] = self.get_scheduler_config(**snake_case__ ) lowerCAmelCase : Any = scheduler_class(**snake_case__ ) lowerCAmelCase : List[str] = self.scheduler_classes[0] lowerCAmelCase : Tuple = self.get_scheduler_config(**snake_case__ ) lowerCAmelCase : List[str] = scheduler_class(**snake_case__ ) lowerCAmelCase : int = 10 lowerCAmelCase : List[str] = self.dummy_model() lowerCAmelCase : List[Any] = self.dummy_sample_deter scheduler.set_timesteps(snake_case__ ) for i, t in enumerate(scheduler.timesteps ): lowerCAmelCase : Any = model(snake_case__ , snake_case__ ) lowerCAmelCase : List[Any] = scheduler.step(snake_case__ , snake_case__ , snake_case__ ).prev_sample return sample def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Dict = dict(self.forward_default_kwargs ) lowerCAmelCase : Optional[Any] = kwargs.pop("num_inference_steps" , snake_case__ ) for scheduler_class in self.scheduler_classes: lowerCAmelCase : Dict = self.get_scheduler_config() lowerCAmelCase : List[Any] = scheduler_class(**snake_case__ ) lowerCAmelCase : int = self.dummy_sample lowerCAmelCase : int = 0.1 * sample if num_inference_steps is not None and hasattr(snake_case__ , "set_timesteps" ): scheduler.set_timesteps(snake_case__ ) elif num_inference_steps is not None and not hasattr(snake_case__ , "set_timesteps" ): lowerCAmelCase : List[str] = num_inference_steps # copy over dummy past residuals (must be done after set_timesteps) lowerCAmelCase : int = [residual + 0.2, residual + 0.15, residual + 0.10] lowerCAmelCase : Tuple = dummy_past_residuals[: scheduler.config.solver_order] lowerCAmelCase : int = scheduler.timesteps[5] lowerCAmelCase : str = scheduler.timesteps[6] lowerCAmelCase : str = scheduler.step(snake_case__ , snake_case__ , snake_case__ , **snake_case__ ).prev_sample lowerCAmelCase : Union[str, Any] = scheduler.step(snake_case__ , snake_case__ , snake_case__ , **snake_case__ ).prev_sample self.assertEqual(output_a.shape , sample.shape ) self.assertEqual(output_a.shape , output_a.shape ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : int = DEISMultistepScheduler(**self.get_scheduler_config() ) lowerCAmelCase : Dict = self.full_loop(scheduler=snake_case__ ) lowerCAmelCase : Optional[Any] = torch.mean(torch.abs(snake_case__ ) ) assert abs(result_mean.item() - 0.23916 ) < 1e-3 lowerCAmelCase : List[str] = DPMSolverSinglestepScheduler.from_config(scheduler.config ) lowerCAmelCase : Any = DPMSolverMultistepScheduler.from_config(scheduler.config ) lowerCAmelCase : str = UniPCMultistepScheduler.from_config(scheduler.config ) lowerCAmelCase : Union[str, Any] = DEISMultistepScheduler.from_config(scheduler.config ) lowerCAmelCase : Union[str, Any] = self.full_loop(scheduler=snake_case__ ) lowerCAmelCase : str = torch.mean(torch.abs(snake_case__ ) ) assert abs(result_mean.item() - 0.23916 ) < 1e-3 def lowercase__ ( self ): """simple docstring""" for timesteps in [25, 50, 100, 999, 1_000]: self.check_over_configs(num_train_timesteps=snake_case__ ) def lowercase__ ( self ): """simple docstring""" self.check_over_configs(thresholding=snake_case__ ) for order in [1, 2, 3]: for solver_type in ["logrho"]: for threshold in [0.5, 1.0, 2.0]: for prediction_type in ["epsilon", "sample"]: self.check_over_configs( thresholding=snake_case__ , prediction_type=snake_case__ , sample_max_value=snake_case__ , algorithm_type="deis" , solver_order=snake_case__ , solver_type=snake_case__ , ) def lowercase__ ( self ): """simple docstring""" for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=snake_case__ ) def lowercase__ ( self ): """simple docstring""" for algorithm_type in ["deis"]: for solver_type in ["logrho"]: for order in [1, 2, 3]: for prediction_type in ["epsilon", "sample"]: self.check_over_configs( solver_order=snake_case__ , solver_type=snake_case__ , prediction_type=snake_case__ , algorithm_type=snake_case__ , ) lowerCAmelCase : Any = self.full_loop( solver_order=snake_case__ , solver_type=snake_case__ , prediction_type=snake_case__ , algorithm_type=snake_case__ , ) assert not torch.isnan(snake_case__ ).any(), "Samples have nan numbers" def lowercase__ ( self ): """simple docstring""" self.check_over_configs(lower_order_final=snake_case__ ) self.check_over_configs(lower_order_final=snake_case__ ) def lowercase__ ( self ): """simple docstring""" for num_inference_steps in [1, 2, 3, 5, 10, 50, 100, 999, 1_000]: self.check_over_forward(num_inference_steps=snake_case__ , time_step=0 ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : List[str] = self.full_loop() lowerCAmelCase : Dict = torch.mean(torch.abs(snake_case__ ) ) assert abs(result_mean.item() - 0.23916 ) < 1e-3 def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Optional[Any] = self.full_loop(prediction_type="v_prediction" ) lowerCAmelCase : List[Any] = torch.mean(torch.abs(snake_case__ ) ) assert abs(result_mean.item() - 0.091 ) < 1e-3 def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : Dict = self.scheduler_classes[0] lowerCAmelCase : str = self.get_scheduler_config(thresholding=snake_case__ , dynamic_thresholding_ratio=0 ) lowerCAmelCase : Optional[Any] = scheduler_class(**snake_case__ ) lowerCAmelCase : Optional[Any] = 10 lowerCAmelCase : Tuple = self.dummy_model() lowerCAmelCase : List[str] = self.dummy_sample_deter.half() scheduler.set_timesteps(snake_case__ ) for i, t in enumerate(scheduler.timesteps ): lowerCAmelCase : Union[str, Any] = model(snake_case__ , snake_case__ ) lowerCAmelCase : Tuple = scheduler.step(snake_case__ , snake_case__ , snake_case__ ).prev_sample assert sample.dtype == torch.floataa
133
0
"""simple docstring""" import gzip import hashlib import json import multiprocessing import os import re import shutil import time from pathlib import Path import numpy as np from arguments import PreprocessingArguments from datasets import load_dataset from minhash_deduplication import deduplicate_dataset from transformers import AutoTokenizer, HfArgumentParser lowerCamelCase_ = re.compile(r"\s+") def __lowerCamelCase ( a_ : List[str] ) -> Optional[int]: return {"hash": hashlib.mda(re.sub(a_ , '''''' , example['''content'''] ).encode('''utf-8''' ) ).hexdigest()} def __lowerCamelCase ( a_ : Union[str, Any] ) -> Any: __SCREAMING_SNAKE_CASE :Optional[Any] = [len(a_ ) for line in example['''content'''].splitlines()] return {"line_mean": np.mean(a_ ), "line_max": max(a_ )} def __lowerCamelCase ( a_ : Tuple ) -> List[str]: __SCREAMING_SNAKE_CASE :List[Any] = np.mean([c.isalnum() for c in example['''content''']] ) return {"alpha_frac": alpha_frac} def __lowerCamelCase ( a_ : Optional[Any] , a_ : str ) -> Optional[Any]: if example["hash"] in uniques: uniques.remove(example['''hash'''] ) return True else: return False def __lowerCamelCase ( a_ : int , a_ : Optional[int]=5 ) -> Optional[int]: __SCREAMING_SNAKE_CASE :Optional[int] = ['''auto-generated''', '''autogenerated''', '''automatically generated'''] __SCREAMING_SNAKE_CASE :int = example['''content'''].splitlines() for _, line in zip(range(a_ ) , a_ ): for keyword in keywords: if keyword in line.lower(): return {"autogenerated": True} else: return {"autogenerated": False} def __lowerCamelCase ( a_ : List[Any] , a_ : Dict=5 , a_ : Any=0.05 ) -> Any: __SCREAMING_SNAKE_CASE :Tuple = ['''unit tests''', '''test file''', '''configuration file'''] __SCREAMING_SNAKE_CASE :List[Any] = example['''content'''].splitlines() __SCREAMING_SNAKE_CASE :Union[str, Any] = 0 __SCREAMING_SNAKE_CASE :Optional[Any] = 0 # first test for _, line in zip(range(a_ ) , a_ ): for keyword in keywords: if keyword in line.lower(): return {"config_or_test": True} # second test __SCREAMING_SNAKE_CASE :List[Any] = example['''content'''].count('''\n''' ) __SCREAMING_SNAKE_CASE :Union[str, Any] = int(coeff * nlines ) for line in lines: count_config += line.lower().count('''config''' ) count_test += line.lower().count('''test''' ) if count_config > threshold or count_test > threshold: return {"config_or_test": True} return {"config_or_test": False} def __lowerCamelCase ( a_ : Dict ) -> int: __SCREAMING_SNAKE_CASE :str = ['''def ''', '''class ''', '''for ''', '''while '''] __SCREAMING_SNAKE_CASE :List[str] = example['''content'''].splitlines() for line in lines: for keyword in keywords: if keyword in line.lower(): return {"has_no_keywords": False} return {"has_no_keywords": True} def __lowerCamelCase ( a_ : Any , a_ : str=4 ) -> Dict: __SCREAMING_SNAKE_CASE :Any = example['''content'''].splitlines() __SCREAMING_SNAKE_CASE :List[str] = 0 for line in lines: counter += line.lower().count('''=''' ) if counter > minimum: return {"has_few_assignments": False} return {"has_few_assignments": True} def __lowerCamelCase ( a_ : Tuple ) -> Union[str, Any]: __SCREAMING_SNAKE_CASE :Any = tokenizer(example['''content'''] , truncation=a_ )['''input_ids'''] __SCREAMING_SNAKE_CASE :List[str] = len(example['''content'''] ) / len(a_ ) return {"ratio": ratio} def __lowerCamelCase ( a_ : Dict ) -> int: __SCREAMING_SNAKE_CASE :Dict = {} results.update(get_hash(a_ ) ) results.update(line_stats(a_ ) ) results.update(alpha_stats(a_ ) ) results.update(char_token_ratio(a_ ) ) results.update(is_autogenerated(a_ ) ) results.update(is_config_or_test(a_ ) ) results.update(has_no_keywords(a_ ) ) results.update(has_few_assignments(a_ ) ) return results def __lowerCamelCase ( a_ : Union[str, Any] , a_ : Tuple , a_ : str ) -> Any: if not check_uniques(a_ , a_ ): return False elif example["autogenerated"]: return False elif example["line_max"] > args.line_max: return False elif example["line_mean"] > args.line_mean: return False elif example["alpha_frac"] < args.alpha_frac: return False elif example["ratio"] < args.min_token_ratio: return False elif example["config_or_test"] and np.random.rand() <= args.filter_proba: return False elif example["has_no_keywords"] and np.random.rand() <= args.filter_proba: return False elif example["has_few_assignments"]: return False else: return True def __lowerCamelCase ( a_ : Dict ) -> List[str]: with open(a_ , '''rb''' ) as f_in: with gzip.open(str(a_ ) + '''.gz''' , '''wb''' , compresslevel=6 ) as f_out: shutil.copyfileobj(a_ , a_ ) os.unlink(a_ ) # Settings lowerCamelCase_ = HfArgumentParser(PreprocessingArguments) lowerCamelCase_ = parser.parse_args() if args.num_workers is None: lowerCamelCase_ = multiprocessing.cpu_count() lowerCamelCase_ = AutoTokenizer.from_pretrained(args.tokenizer_dir) # Load dataset lowerCamelCase_ = time.time() lowerCamelCase_ = load_dataset(args.dataset_name, split="train") print(f'Time to load dataset: {time.time()-t_start:.2f}') # Run preprocessing lowerCamelCase_ = time.time() lowerCamelCase_ = ds.map(preprocess, num_proc=args.num_workers) print(f'Time to preprocess dataset: {time.time()-t_start:.2f}') # Deduplicate hashes lowerCamelCase_ = set(ds.unique("hash")) lowerCamelCase_ = len(uniques) / len(ds) print(f'Fraction of duplicates: {1-frac:.2%}') # Deduplicate data and apply heuristics lowerCamelCase_ = time.time() lowerCamelCase_ = ds.filter(filter, fn_kwargs={"uniques": uniques, "args": args}) print(f'Time to filter dataset: {time.time()-t_start:.2f}') print(f'Size of filtered dataset: {len(ds_filter)}') # Deduplicate with minhash and jaccard similarity if args.near_deduplication: lowerCamelCase_ = time.time() lowerCamelCase_ , lowerCamelCase_ = deduplicate_dataset(ds_filter, args.jaccard_threshold) print(f'Time to deduplicate dataset: {time.time()-t_start:.2f}') print(f'Size of deduplicate dataset: {len(ds_filter)}') # Save data in batches of samples_per_file lowerCamelCase_ = Path(args.output_dir) output_dir.mkdir(exist_ok=True) # save duplicate_clusters in the output_dir as artifacts # not sure it is the right place the save it if args.near_deduplication: with open(output_dir / "duplicate_clusters.json", "w") as f: json.dump(duplicate_clusters, f) lowerCamelCase_ = output_dir / "data" data_dir.mkdir(exist_ok=True) lowerCamelCase_ = time.time() for file_number, index in enumerate(range(0, len(ds_filter), args.samples_per_file)): lowerCamelCase_ = str(data_dir / f'file-{file_number+1:012}.json') lowerCamelCase_ = min(len(ds_filter), index + args.samples_per_file) ds_filter.select(list(range(index, end_index))).to_json(file_path) compress_file(file_path) print(f'Time to save dataset: {time.time()-t_start:.2f}')
191
"""simple docstring""" def __lowerCamelCase ( a_ : Union[str, Any] ) -> Optional[int]: __SCREAMING_SNAKE_CASE :List[str] = 1 __SCREAMING_SNAKE_CASE :Dict = 2 while i * i <= n: __SCREAMING_SNAKE_CASE :Tuple = 0 while n % i == 0: n //= i multiplicity += 1 n_divisors *= multiplicity + 1 i += 1 if n > 1: n_divisors *= 2 return n_divisors def __lowerCamelCase ( ) -> int: __SCREAMING_SNAKE_CASE :Dict = 1 __SCREAMING_SNAKE_CASE :Dict = 1 while True: i += 1 t_num += i if count_divisors(a_ ) > 5_00: break return t_num if __name__ == "__main__": print(solution())
191
1
def _A ( SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : int ): """simple docstring""" return x if y == 0 else greatest_common_divisor(SCREAMING_SNAKE_CASE , x % y ) def _A ( SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : int ): """simple docstring""" return (x * y) // greatest_common_divisor(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) def _A ( SCREAMING_SNAKE_CASE : int = 20 ): """simple docstring""" a__ : List[str] =1 for i in range(1 , n + 1 ): a__ : Optional[int] =lcm(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) return g if __name__ == "__main__": print(F"""{solution() = }""")
148
# Usage: # ./gen-card-facebook-wmt19.py import os from pathlib import Path def _A ( SCREAMING_SNAKE_CASE : Optional[int] , SCREAMING_SNAKE_CASE : Any , SCREAMING_SNAKE_CASE : Tuple ): """simple docstring""" a__ : List[Any] ={ "en": "Machine learning is great, isn't it?", "ru": "Машинное обучение - это здорово, не так ли?", "de": "Maschinelles Lernen ist großartig, oder?", } # BLUE scores as follows: # "pair": [fairseq, transformers] a__ : List[Any] ={ "ru-en": ["[41.3](http://matrix.statmt.org/matrix/output/1907?run_id=6937)", "39.20"], "en-ru": ["[36.4](http://matrix.statmt.org/matrix/output/1914?run_id=6724)", "33.47"], "en-de": ["[43.1](http://matrix.statmt.org/matrix/output/1909?run_id=6862)", "42.83"], "de-en": ["[42.3](http://matrix.statmt.org/matrix/output/1902?run_id=6750)", "41.35"], } a__ : Optional[int] =f'''{src_lang}-{tgt_lang}''' a__ : Any =f''' --- language: - {src_lang} - {tgt_lang} thumbnail: tags: - translation - wmt19 - facebook license: apache-2.0 datasets: - wmt19 metrics: - bleu --- # FSMT ## Model description This is a ported version of [fairseq wmt19 transformer](https://github.com/pytorch/fairseq/blob/master/examples/wmt19/README.md) for {src_lang}-{tgt_lang}. For more details, please see, [Facebook FAIR\'s WMT19 News Translation Task Submission](https://arxiv.org/abs/1907.06616). The abbreviation FSMT stands for FairSeqMachineTranslation All four models are available: * [wmt19-en-ru](https://huggingface.co/facebook/wmt19-en-ru) * [wmt19-ru-en](https://huggingface.co/facebook/wmt19-ru-en) * [wmt19-en-de](https://huggingface.co/facebook/wmt19-en-de) * [wmt19-de-en](https://huggingface.co/facebook/wmt19-de-en) ## Intended uses & limitations #### How to use ```python from transformers import FSMTForConditionalGeneration, FSMTTokenizer mname = "facebook/wmt19-{src_lang}-{tgt_lang}" tokenizer = FSMTTokenizer.from_pretrained(mname) model = FSMTForConditionalGeneration.from_pretrained(mname) input = "{texts[src_lang]}" input_ids = tokenizer.encode(input, return_tensors="pt") outputs = model.generate(input_ids) decoded = tokenizer.decode(outputs[0], skip_special_tokens=True) print(decoded) # {texts[tgt_lang]} ``` #### Limitations and bias - The original (and this ported model) doesn\'t seem to handle well inputs with repeated sub-phrases, [content gets truncated](https://discuss.huggingface.co/t/issues-with-translating-inputs-containing-repeated-phrases/981) ## Training data Pretrained weights were left identical to the original model released by fairseq. For more details, please, see the [paper](https://arxiv.org/abs/1907.06616). ## Eval results pair | fairseq | transformers -------|---------|---------- {pair} | {scores[pair][0]} | {scores[pair][1]} The score is slightly below the score reported by `fairseq`, since `transformers`` currently doesn\'t support: - model ensemble, therefore the best performing checkpoint was ported (``model4.pt``). - re-ranking The score was calculated using this code: ```bash git clone https://github.com/huggingface/transformers cd transformers export PAIR={pair} export DATA_DIR=data/$PAIR export SAVE_DIR=data/$PAIR export BS=8 export NUM_BEAMS=15 mkdir -p $DATA_DIR sacrebleu -t wmt19 -l $PAIR --echo src > $DATA_DIR/val.source sacrebleu -t wmt19 -l $PAIR --echo ref > $DATA_DIR/val.target echo $PAIR PYTHONPATH="src:examples/seq2seq" python examples/seq2seq/run_eval.py facebook/wmt19-$PAIR $DATA_DIR/val.source $SAVE_DIR/test_translations.txt --reference_path $DATA_DIR/val.target --score_path $SAVE_DIR/test_bleu.json --bs $BS --task translation --num_beams $NUM_BEAMS ``` note: fairseq reports using a beam of 50, so you should get a slightly higher score if re-run with `--num_beams 50`. ## Data Sources - [training, etc.](http://www.statmt.org/wmt19/) - [test set](http://matrix.statmt.org/test_sets/newstest2019.tgz?1556572561) ### BibTeX entry and citation info ```bibtex @inproceedings{{..., year={{2020}}, title={{Facebook FAIR\'s WMT19 News Translation Task Submission}}, author={{Ng, Nathan and Yee, Kyra and Baevski, Alexei and Ott, Myle and Auli, Michael and Edunov, Sergey}}, booktitle={{Proc. of WMT}}, }} ``` ## TODO - port model ensemble (fairseq uses 4 model checkpoints) ''' os.makedirs(SCREAMING_SNAKE_CASE , exist_ok=SCREAMING_SNAKE_CASE ) a__ : Tuple =os.path.join(SCREAMING_SNAKE_CASE , "README.md" ) print(f'''Generating {path}''' ) with open(SCREAMING_SNAKE_CASE , "w" , encoding="utf-8" ) as f: f.write(SCREAMING_SNAKE_CASE ) # make sure we are under the root of the project UpperCAmelCase : str = Path(__file__).resolve().parent.parent.parent UpperCAmelCase : Dict = repo_dir / """model_cards""" for model_name in ["wmt19-ru-en", "wmt19-en-ru", "wmt19-en-de", "wmt19-de-en"]: UpperCAmelCase , UpperCAmelCase , UpperCAmelCase : List[str] = model_name.split("""-""") UpperCAmelCase : Tuple = model_cards_dir / """facebook""" / model_name write_model_card(model_card_dir, src_lang=src_lang, tgt_lang=tgt_lang)
148
1
import argparse import math import traceback import dateutil.parser as date_parser import requests def _SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE ): A_ : Optional[Any] = {} A_ : str = job['''started_at'''] A_ : Optional[Any] = job['''completed_at'''] A_ : List[str] = date_parser.parse(SCREAMING_SNAKE_CASE ) A_ : Tuple = date_parser.parse(SCREAMING_SNAKE_CASE ) A_ : str = round((end_datetime - start_datetime).total_seconds() / 6_0.0 ) A_ : Any = start A_ : str = end A_ : Dict = duration_in_min return job_info def _SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE=None ): A_ : int = None if token is not None: A_ : List[Any] = {'''Accept''': '''application/vnd.github+json''', '''Authorization''': f'''Bearer {token}'''} A_ : Union[str, Any] = f'''https://api.github.com/repos/huggingface/transformers/actions/runs/{workflow_run_id}/jobs?per_page=100''' A_ : str = requests.get(SCREAMING_SNAKE_CASE , headers=SCREAMING_SNAKE_CASE ).json() A_ : int = {} try: job_time.update({job['''name''']: extract_time_from_single_job(SCREAMING_SNAKE_CASE ) for job in result['''jobs''']} ) A_ : List[Any] = math.ceil((result['''total_count'''] - 100) / 100 ) for i in range(SCREAMING_SNAKE_CASE ): A_ : Optional[int] = requests.get(url + f'''&page={i + 2}''' , headers=SCREAMING_SNAKE_CASE ).json() job_time.update({job['''name''']: extract_time_from_single_job(SCREAMING_SNAKE_CASE ) for job in result['''jobs''']} ) return job_time except Exception: print(f'''Unknown error, could not fetch links:\n{traceback.format_exc()}''' ) return {} if __name__ == "__main__": UpperCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument("""--workflow_run_id""", type=str, required=True, help="""A GitHub Actions workflow run id.""") UpperCamelCase = parser.parse_args() UpperCamelCase = get_job_time(args.workflow_run_id) UpperCamelCase = dict(sorted(job_time.items(), key=lambda item: item[1]["duration"], reverse=True)) for k, v in job_time.items(): print(F'''{k}: {v['duration']}''')
186
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available UpperCamelCase = { """configuration_nezha""": ["""NEZHA_PRETRAINED_CONFIG_ARCHIVE_MAP""", """NezhaConfig"""], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ """NEZHA_PRETRAINED_MODEL_ARCHIVE_LIST""", """NezhaForNextSentencePrediction""", """NezhaForMaskedLM""", """NezhaForPreTraining""", """NezhaForMultipleChoice""", """NezhaForQuestionAnswering""", """NezhaForSequenceClassification""", """NezhaForTokenClassification""", """NezhaModel""", """NezhaPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_nezha import NEZHA_PRETRAINED_CONFIG_ARCHIVE_MAP, NezhaConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_nezha import ( NEZHA_PRETRAINED_MODEL_ARCHIVE_LIST, NezhaForMaskedLM, NezhaForMultipleChoice, NezhaForNextSentencePrediction, NezhaForPreTraining, NezhaForQuestionAnswering, NezhaForSequenceClassification, NezhaForTokenClassification, NezhaModel, NezhaPreTrainedModel, ) else: import sys UpperCamelCase = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
186
1
"""simple docstring""" import math from typing import List, Optional, Tuple, Union import numpy as np import torch from ..configuration_utils import ConfigMixin, register_to_config from .scheduling_utils import SchedulerMixin, SchedulerOutput class lowerCamelCase__ ( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): '''simple docstring''' _lowerCamelCase = 1 @register_to_config def __init__( self ,lowerCamelCase_ = 1_0_0_0 ,lowerCamelCase_ = None ) -> Union[str, Any]: # set `betas`, `alphas`, `timesteps` self.set_timesteps(lowerCamelCase_ ) # standard deviation of the initial noise distribution A = 1.0 # For now we only support F-PNDM, i.e. the runge-kutta method # For more information on the algorithm please take a look at the paper: https://arxiv.org/pdf/2202.09778.pdf # mainly at formula (9), (12), (13) and the Algorithm 2. A = 4 # running values A = [] def UpperCamelCase__ ( self ,lowerCamelCase_ ,lowerCamelCase_ = None ) -> Optional[Any]: A = num_inference_steps A = torch.linspace(1 ,0 ,num_inference_steps + 1 )[:-1] A = torch.cat([steps, torch.tensor([0.0] )] ) if self.config.trained_betas is not None: A = torch.tensor(self.config.trained_betas ,dtype=torch.floataa ) else: A = torch.sin(steps * math.pi / 2 ) ** 2 A = (1.0 - self.betas**2) ** 0.5 A = (torch.atana(self.betas ,self.alphas ) / math.pi * 2)[:-1] A = timesteps.to(lowerCamelCase_ ) A = [] def UpperCamelCase__ ( self ,lowerCamelCase_ ,lowerCamelCase_ ,lowerCamelCase_ ,lowerCamelCase_ = True ,) -> Union[SchedulerOutput, Tuple]: if self.num_inference_steps is None: raise ValueError( """Number of inference steps is 'None', you need to run 'set_timesteps' after creating the scheduler""" ) A = (self.timesteps == timestep).nonzero().item() A = timestep_index + 1 A = sample * self.betas[timestep_index] + model_output * self.alphas[timestep_index] self.ets.append(lowerCamelCase_ ) if len(self.ets ) == 1: A = self.ets[-1] elif len(self.ets ) == 2: A = (3 * self.ets[-1] - self.ets[-2]) / 2 elif len(self.ets ) == 3: A = (2_3 * self.ets[-1] - 1_6 * self.ets[-2] + 5 * self.ets[-3]) / 1_2 else: A = (1 / 2_4) * (5_5 * self.ets[-1] - 5_9 * self.ets[-2] + 3_7 * self.ets[-3] - 9 * self.ets[-4]) A = self._get_prev_sample(lowerCamelCase_ ,lowerCamelCase_ ,lowerCamelCase_ ,lowerCamelCase_ ) if not return_dict: return (prev_sample,) return SchedulerOutput(prev_sample=lowerCamelCase_ ) def UpperCamelCase__ ( self ,lowerCamelCase_ ,*lowerCamelCase_ ,**lowerCamelCase_ ) -> torch.FloatTensor: return sample def UpperCamelCase__ ( self ,lowerCamelCase_ ,lowerCamelCase_ ,lowerCamelCase_ ,lowerCamelCase_ ) -> List[str]: A = self.alphas[timestep_index] A = self.betas[timestep_index] A = self.alphas[prev_timestep_index] A = self.betas[prev_timestep_index] A = (sample - sigma * ets) / max(lowerCamelCase_ ,1E-8 ) A = next_alpha * pred + ets * next_sigma return prev_sample def __len__( self ) -> Union[str, Any]: return self.config.num_train_timesteps
362
"""simple docstring""" from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging UpperCAmelCase =logging.get_logger(__name__) UpperCAmelCase ={ "distilbert-base-uncased": "https://huggingface.co/distilbert-base-uncased/resolve/main/config.json", "distilbert-base-uncased-distilled-squad": ( "https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/config.json" ), "distilbert-base-cased": "https://huggingface.co/distilbert-base-cased/resolve/main/config.json", "distilbert-base-cased-distilled-squad": ( "https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/config.json" ), "distilbert-base-german-cased": "https://huggingface.co/distilbert-base-german-cased/resolve/main/config.json", "distilbert-base-multilingual-cased": ( "https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/config.json" ), "distilbert-base-uncased-finetuned-sst-2-english": ( "https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english/resolve/main/config.json" ), } class lowerCamelCase__ ( SCREAMING_SNAKE_CASE ): '''simple docstring''' _lowerCamelCase = '''distilbert''' _lowerCamelCase = { '''hidden_size''': '''dim''', '''num_attention_heads''': '''n_heads''', '''num_hidden_layers''': '''n_layers''', } def __init__( self ,lowerCamelCase_=3_0_5_2_2 ,lowerCamelCase_=5_1_2 ,lowerCamelCase_=False ,lowerCamelCase_=6 ,lowerCamelCase_=1_2 ,lowerCamelCase_=7_6_8 ,lowerCamelCase_=4 * 7_6_8 ,lowerCamelCase_=0.1 ,lowerCamelCase_=0.1 ,lowerCamelCase_="gelu" ,lowerCamelCase_=0.02 ,lowerCamelCase_=0.1 ,lowerCamelCase_=0.2 ,lowerCamelCase_=0 ,**lowerCamelCase_ ,) -> Dict: A = vocab_size A = max_position_embeddings A = sinusoidal_pos_embds A = n_layers A = n_heads A = dim A = hidden_dim A = dropout A = attention_dropout A = activation A = initializer_range A = qa_dropout A = seq_classif_dropout super().__init__(**lowerCamelCase_ ,pad_token_id=lowerCamelCase_ ) class lowerCamelCase__ ( SCREAMING_SNAKE_CASE ): '''simple docstring''' @property def UpperCamelCase__ ( self ) -> Mapping[str, Mapping[int, str]]: if self.task == "multiple-choice": A = {0: """batch""", 1: """choice""", 2: """sequence"""} else: A = {0: """batch""", 1: """sequence"""} return OrderedDict( [ ("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ] )
77
0
# this script reports modified .py files under the desired list of top-level sub-dirs passed as a list of arguments, e.g.: # python ./utils/get_modified_files.py utils src tests examples # # it uses git to find the forking point and which files were modified - i.e. files not under git won't be considered # since the output of this script is fed into Makefile commands it doesn't print a newline after the results import re import subprocess import sys _UpperCAmelCase : Dict =subprocess.check_output("""git merge-base main HEAD""".split()).decode("""utf-8""") _UpperCAmelCase : str =( subprocess.check_output(f"""git diff --diff-filter=d --name-only {fork_point_sha}""".split()).decode("""utf-8""").split() ) _UpperCAmelCase : Tuple ="""|""".join(sys.argv[1:]) _UpperCAmelCase : Optional[Any] =re.compile(Rf"""^({joined_dirs}).*?\.py$""") _UpperCAmelCase : Any =[x for x in modified_files if regex.match(x)] print(""" """.join(relevant_modified_files), end="""""")
262
import unittest from transformers import JukeboxTokenizer from transformers.testing_utils import require_torch class snake_case__( unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : Tuple = JukeboxTokenizer SCREAMING_SNAKE_CASE__ : int = { """artist""": """Zac Brown Band""", """genres""": """Country""", """lyrics""": """I met a traveller from an antique land, Who said \"Two vast and trunkless legs of stone Stand in the desert. . . . Near them, on the sand, Half sunk a shattered visage lies, whose frown, And wrinkled lip, and sneer of cold command, Tell that its sculptor well those passions read Which yet survive, stamped on these lifeless things, The hand that mocked them, and the heart that fed; And on the pedestal, these words appear: My name is Ozymandias, King of Kings; Look on my Works, ye Mighty, and despair! Nothing beside remains. Round the decay Of that colossal Wreck, boundless and bare The lone and level sands stretch far away """, } @require_torch def lowercase_ ( self ) -> Union[str, Any]: import torch lowerCAmelCase_ : Union[str, Any] = JukeboxTokenizer.from_pretrained('''openai/jukebox-1b-lyrics''' ) lowerCAmelCase_ : Any = tokenizer(**self.metas )['''input_ids'''] # fmt: off lowerCAmelCase_ : List[str] = [ torch.tensor([[ 0, 0, 0, 7_1_6_9, 5_0_7, 9, 7_6, 3_9, 3_1, 4_6, 7_6, 2_7, 7_6, 4_6, 4_4, 2_7, 4_8, 3_1, 3_8, 3_8, 3_1, 4_4, 7_6, 3_2, 4_4, 4_1, 3_9, 7_6, 2_7, 4_0, 7_6, 2_7, 4_0, 4_6, 3_5, 4_3, 4_7, 3_1, 7_6, 3_8, 2_7, 4_0, 3_0, 6_4, 7_8, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 2_3, 3_4, 4_1, 7_6, 4_5, 2_7, 3_5, 3_0, 7_6, 7_1, 2_0, 4_9, 4_1, 7_6, 4_8, 2_7, 4_5, 4_6, 7_6, 2_7, 4_0, 3_0, 7_6, 4_6, 4_4, 4_7, 4_0, 3_7, 3_8, 3_1, 4_5, 4_5, 7_6, 3_8, 3_1, 3_3, 4_5, 7_6, 4_1, 3_2, 7_6, 4_5, 4_6, 4_1, 4_0, 3_1, 7_8, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 1_9, 4_6, 2_7, 4_0, 3_0, 7_6, 3_5, 4_0, 7_6, 4_6, 3_4, 3_1, 7_6, 3_0, 3_1, 4_5, 3_1, 4_4, 4_6, 6_3, 7_6, 6_3, 7_6, 6_3, 7_6, 6_3, 7_6, 1_4, 3_1, 2_7, 4_4, 7_6, 4_6, 3_4, 3_1, 3_9, 6_4, 7_6, 4_1, 4_0, 7_6, 4_6, 3_4, 3_1, 7_6, 4_5, 2_7, 4_0, 3_0, 6_4, 7_8, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 8, 2_7, 3_8, 3_2, 7_6, 4_5, 4_7, 4_0, 3_7, 7_6, 2_7, 7_6, 4_5, 3_4, 2_7, 4_6, 4_6, 3_1, 4_4, 3_1, 3_0, 7_6, 4_8, 3_5, 4_5, 2_7, 3_3, 3_1, 7_6, 3_8, 3_5, 3_1, 4_5, 6_4, 7_6, 4_9, 3_4, 4_1, 4_5, 3_1, 7_6, 3_2, 4_4, 4_1, 4_9, 4_0, 6_4, 7_8, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 1, 4_0, 3_0, 7_6, 4_9, 4_4, 3_5, 4_0, 3_7, 3_8, 3_1, 3_0, 7_6, 3_8, 3_5, 4_2, 6_4, 7_6, 2_7, 4_0, 3_0, 7_6, 4_5, 4_0, 3_1, 3_1, 4_4, 7_6, 4_1, 3_2, 7_6, 2_9, 4_1, 3_8, 3_0, 7_6, 2_9, 4_1, 3_9, 3_9, 2_7, 4_0, 3_0, 6_4, 7_8, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 2_0, 3_1, 3_8, 3_8, 7_6, 4_6, 3_4, 2_7, 4_6, 7_6, 3_5, 4_6, 4_5, 7_6, 4_5, 2_9, 4_7, 3_8, 4_2, 4_6, 4_1, 4_4, 7_6, 4_9, 3_1, 3_8, 3_8, 7_6, 4_6, 3_4, 4_1, 4_5, 3_1, 7_6, 4_2, 2_7, 4_5, 4_5, 3_5, 4_1, 4_0, 4_5, 7_6, 4_4, 3_1, 2_7, 3_0, 7_8, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 2_3, 3_4, 3_5, 2_9, 3_4, 7_6, 5_1, 3_1, 4_6, 7_6, 4_5, 4_7, 4_4, 4_8, 3_5, 4_8, 3_1, 6_4, 7_6, 4_5, 4_6, 2_7, 3_9, 4_2, 3_1, 3_0, 7_6, 4_1, 4_0, 7_6, 4_6, 3_4, 3_1, 4_5, 3_1, 7_6, 3_8, 3_5, 3_2, 3_1, 3_8, 3_1, 4_5, 4_5, 7_6, 4_6, 3_4, 3_5, 4_0, 3_3, 4_5, 6_4, 7_8, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 2_0, 3_4, 3_1, 7_6, 3_4, 2_7, 4_0, 3_0, 7_6, 4_6, 3_4, 2_7, 4_6, 7_6, 3_9, 4_1, 2_9, 3_7, 3_1, 3_0, 7_6, 4_6, 3_4, 3_1, 3_9, 6_4, 7_6, 2_7, 4_0, 3_0, 7_6, 4_6, 3_4, 3_1, 7_6, 3_4, 3_1, 2_7, 4_4, 4_6, 7_6, 4_6, 3_4, 2_7, 4_6, 7_6, 3_2, 3_1, 3_0, 6_6, 7_8, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 1, 4_0, 3_0, 7_6, 4_1, 4_0, 7_6, 4_6, 3_4, 3_1, 7_6, 4_2, 3_1, 3_0, 3_1, 4_5, 4_6, 2_7, 3_8, 6_4, 7_6, 4_6, 3_4, 3_1, 4_5, 3_1, 7_6, 4_9, 4_1, 4_4, 3_0, 4_5, 7_6, 2_7, 4_2, 4_2, 3_1, 2_7, 4_4, 6_5, 7_8, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 1_3, 5_1, 7_6, 4_0, 2_7, 3_9, 3_1, 7_6, 3_5, 4_5, 7_6, 1_5, 5_2, 5_1, 3_9, 2_7, 4_0, 3_0, 3_5, 2_7, 4_5, 6_4, 7_6, 1_1, 3_5, 4_0, 3_3, 7_6, 4_1, 3_2, 7_6, 1_1, 3_5, 4_0, 3_3, 4_5, 6_6, 7_8, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 1_2, 4_1, 4_1, 3_7, 7_6, 4_1, 4_0, 7_6, 3_9, 5_1, 7_6, 2_3, 4_1, 4_4, 3_7, 4_5, 6_4, 7_6, 5_1, 3_1, 7_6, 1_3, 3_5, 3_3, 3_4, 4_6, 5_1, 6_4, 7_6, 2_7, 4_0, 3_0, 7_6, 3_0, 3_1, 4_5, 4_2, 2_7, 3_5, 4_4, 6_7, 7_8, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 1_4, 4_1, 4_6, 3_4, 3_5, 4_0, 3_3, 7_6, 2_8, 3_1, 4_5, 3_5, 3_0, 3_1, 7_6, 4_4, 3_1, 3_9, 2_7, 3_5, 4_0, 4_5, 6_3, 7_6, 1_8, 4_1, 4_7, 4_0, 3_0, 7_6, 4_6, 3_4, 3_1, 7_6, 3_0, 3_1, 2_9, 2_7, 5_1, 7_8, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 1_5, 3_2, 7_6, 4_6, 3_4, 2_7, 4_6, 7_6, 2_9, 4_1, 3_8, 4_1, 4_5, 4_5, 2_7, 3_8, 7_6, 2_3, 4_4, 3_1, 2_9, 3_7, 6_4, 7_6, 2_8, 4_1, 4_7, 4_0, 3_0, 3_8, 3_1, 4_5, 4_5, 7_6, 2_7, 4_0, 3_0, 7_6, 2_8, 2_7, 4_4, 3_1, 7_8, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 2_0, 3_4, 3_1, 7_6, 3_8, 4_1, 4_0, 3_1, 7_6, 2_7, 4_0, 3_0, 7_6, 3_8, 3_1, 4_8, 3_1, 3_8, 7_6, 4_5, 2_7, 4_0, 3_0, 4_5, 7_6, 4_5, 4_6, 4_4, 3_1, 4_6, 2_9, 3_4, 7_6, 3_2, 2_7, 4_4, 7_6, 2_7, 4_9, 2_7, 5_1, 7_8, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6, 7_6]] ), torch.tensor([[0, 0, 0, 1_0_6_9, 1_1]] ), torch.tensor([[0, 0, 0, 1_0_6_9, 1_1]] ), ] # fmt: on self.assertTrue(torch.allclose(tokens[0] , EXPECTED_OUTPUT[0] ) ) self.assertTrue(torch.allclose(tokens[1] , EXPECTED_OUTPUT[1] ) ) self.assertTrue(torch.allclose(tokens[2] , EXPECTED_OUTPUT[2] ) ) @require_torch def lowercase_ ( self ) -> List[Any]: import torch lowerCAmelCase_ : Any = JukeboxTokenizer.from_pretrained('''openai/jukebox-5b-lyrics''' ) lowerCAmelCase_ : str = tokenizer(**self.metas )['''input_ids'''] # fmt: off lowerCAmelCase_ : Tuple = [ torch.tensor([[ 0, 0, 0, 1_0_6_9, 1_1, -1, -1, -1, -1, 9, 7_7, 3_9, 3_1, 4_6, 7_7, 2_7, 7_7, 4_6, 4_4, 2_7, 4_8, 3_1, 3_8, 3_8, 3_1, 4_4, 7_7, 3_2, 4_4, 4_1, 3_9, 7_7, 2_7, 4_0, 7_7, 2_7, 4_0, 4_6, 3_5, 4_3, 4_7, 3_1, 7_7, 3_8, 2_7, 4_0, 3_0, 6_4, 7_9, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 2_3, 3_4, 4_1, 7_7, 4_5, 2_7, 3_5, 3_0, 7_7, 7_2, 2_0, 4_9, 4_1, 7_7, 4_8, 2_7, 4_5, 4_6, 7_7, 2_7, 4_0, 3_0, 7_7, 4_6, 4_4, 4_7, 4_0, 3_7, 3_8, 3_1, 4_5, 4_5, 7_7, 3_8, 3_1, 3_3, 4_5, 7_7, 4_1, 3_2, 7_7, 4_5, 4_6, 4_1, 4_0, 3_1, 7_9, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 1_9, 4_6, 2_7, 4_0, 3_0, 7_7, 3_5, 4_0, 7_7, 4_6, 3_4, 3_1, 7_7, 3_0, 3_1, 4_5, 3_1, 4_4, 4_6, 6_3, 7_7, 6_3, 7_7, 6_3, 7_7, 6_3, 7_7, 1_4, 3_1, 2_7, 4_4, 7_7, 4_6, 3_4, 3_1, 3_9, 6_4, 7_7, 4_1, 4_0, 7_7, 4_6, 3_4, 3_1, 7_7, 4_5, 2_7, 4_0, 3_0, 6_4, 7_9, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 8, 2_7, 3_8, 3_2, 7_7, 4_5, 4_7, 4_0, 3_7, 7_7, 2_7, 7_7, 4_5, 3_4, 2_7, 4_6, 4_6, 3_1, 4_4, 3_1, 3_0, 7_7, 4_8, 3_5, 4_5, 2_7, 3_3, 3_1, 7_7, 3_8, 3_5, 3_1, 4_5, 6_4, 7_7, 4_9, 3_4, 4_1, 4_5, 3_1, 7_7, 3_2, 4_4, 4_1, 4_9, 4_0, 6_4, 7_9, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 1, 4_0, 3_0, 7_7, 4_9, 4_4, 3_5, 4_0, 3_7, 3_8, 3_1, 3_0, 7_7, 3_8, 3_5, 4_2, 6_4, 7_7, 2_7, 4_0, 3_0, 7_7, 4_5, 4_0, 3_1, 3_1, 4_4, 7_7, 4_1, 3_2, 7_7, 2_9, 4_1, 3_8, 3_0, 7_7, 2_9, 4_1, 3_9, 3_9, 2_7, 4_0, 3_0, 6_4, 7_9, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 2_0, 3_1, 3_8, 3_8, 7_7, 4_6, 3_4, 2_7, 4_6, 7_7, 3_5, 4_6, 4_5, 7_7, 4_5, 2_9, 4_7, 3_8, 4_2, 4_6, 4_1, 4_4, 7_7, 4_9, 3_1, 3_8, 3_8, 7_7, 4_6, 3_4, 4_1, 4_5, 3_1, 7_7, 4_2, 2_7, 4_5, 4_5, 3_5, 4_1, 4_0, 4_5, 7_7, 4_4, 3_1, 2_7, 3_0, 7_9, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 2_3, 3_4, 3_5, 2_9, 3_4, 7_7, 5_1, 3_1, 4_6, 7_7, 4_5, 4_7, 4_4, 4_8, 3_5, 4_8, 3_1, 6_4, 7_7, 4_5, 4_6, 2_7, 3_9, 4_2, 3_1, 3_0, 7_7, 4_1, 4_0, 7_7, 4_6, 3_4, 3_1, 4_5, 3_1, 7_7, 3_8, 3_5, 3_2, 3_1, 3_8, 3_1, 4_5, 4_5, 7_7, 4_6, 3_4, 3_5, 4_0, 3_3, 4_5, 6_4, 7_9, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 2_0, 3_4, 3_1, 7_7, 3_4, 2_7, 4_0, 3_0, 7_7, 4_6, 3_4, 2_7, 4_6, 7_7, 3_9, 4_1, 2_9, 3_7, 3_1, 3_0, 7_7, 4_6, 3_4, 3_1, 3_9, 6_4, 7_7, 2_7, 4_0, 3_0, 7_7, 4_6, 3_4, 3_1, 7_7, 3_4, 3_1, 2_7, 4_4, 4_6, 7_7, 4_6, 3_4, 2_7, 4_6, 7_7, 3_2, 3_1, 3_0, 6_6, 7_9, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 1, 4_0, 3_0, 7_7, 4_1, 4_0, 7_7, 4_6, 3_4, 3_1, 7_7, 4_2, 3_1, 3_0, 3_1, 4_5, 4_6, 2_7, 3_8, 6_4, 7_7, 4_6, 3_4, 3_1, 4_5, 3_1, 7_7, 4_9, 4_1, 4_4, 3_0, 4_5, 7_7, 2_7, 4_2, 4_2, 3_1, 2_7, 4_4, 6_5, 7_9, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 1_3, 5_1, 7_7, 4_0, 2_7, 3_9, 3_1, 7_7, 3_5, 4_5, 7_7, 1_5, 5_2, 5_1, 3_9, 2_7, 4_0, 3_0, 3_5, 2_7, 4_5, 6_4, 7_7, 1_1, 3_5, 4_0, 3_3, 7_7, 4_1, 3_2, 7_7, 1_1, 3_5, 4_0, 3_3, 4_5, 6_6, 7_9, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 1_2, 4_1, 4_1, 3_7, 7_7, 4_1, 4_0, 7_7, 3_9, 5_1, 7_7, 2_3, 4_1, 4_4, 3_7, 4_5, 6_4, 7_7, 5_1, 3_1, 7_7, 1_3, 3_5, 3_3, 3_4, 4_6, 5_1, 6_4, 7_7, 2_7, 4_0, 3_0, 7_7, 3_0, 3_1, 4_5, 4_2, 2_7, 3_5, 4_4, 6_7, 7_9, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 1_4, 4_1, 4_6, 3_4, 3_5, 4_0, 3_3, 7_7, 2_8, 3_1, 4_5, 3_5, 3_0, 3_1, 7_7, 4_4, 3_1, 3_9, 2_7, 3_5, 4_0, 4_5, 6_3, 7_7, 1_8, 4_1, 4_7, 4_0, 3_0, 7_7, 4_6, 3_4, 3_1, 7_7, 3_0, 3_1, 2_9, 2_7, 5_1, 7_9, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 1_5, 3_2, 7_7, 4_6, 3_4, 2_7, 4_6, 7_7, 2_9, 4_1, 3_8, 4_1, 4_5, 4_5, 2_7, 3_8, 7_7, 2_3, 4_4, 3_1, 2_9, 3_7, 6_4, 7_7, 2_8, 4_1, 4_7, 4_0, 3_0, 3_8, 3_1, 4_5, 4_5, 7_7, 2_7, 4_0, 3_0, 7_7, 2_8, 2_7, 4_4, 3_1, 7_9, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 2_0, 3_4, 3_1, 7_7, 3_8, 4_1, 4_0, 3_1, 7_7, 2_7, 4_0, 3_0, 7_7, 3_8, 3_1, 4_8, 3_1, 3_8, 7_7, 4_5, 2_7, 4_0, 3_0, 4_5, 7_7, 4_5, 4_6, 4_4, 3_1, 4_6, 2_9, 3_4, 7_7, 3_2, 2_7, 4_4, 7_7, 2_7, 4_9, 2_7, 5_1, 7_9, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7, 7_7]] ), torch.tensor([[0, 0, 0, 1_0_6_9, 1_1, -1, -1, -1, -1]] ), torch.tensor([[0, 0, 0, 1_0_6_9, 1_1, -1, -1, -1, -1]] ), ] # fmt: on self.assertTrue(torch.allclose(tokens[0] , EXPECTED_OUTPUT[0] ) ) self.assertTrue(torch.allclose(tokens[1] , EXPECTED_OUTPUT[1] ) ) self.assertTrue(torch.allclose(tokens[2] , EXPECTED_OUTPUT[2] ) )
262
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available A_ : int = { """configuration_squeezebert""": [ """SQUEEZEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """SqueezeBertConfig""", """SqueezeBertOnnxConfig""", ], """tokenization_squeezebert""": ["""SqueezeBertTokenizer"""], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ : Union[str, Any] = ["""SqueezeBertTokenizerFast"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ : Tuple = [ """SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST""", """SqueezeBertForMaskedLM""", """SqueezeBertForMultipleChoice""", """SqueezeBertForQuestionAnswering""", """SqueezeBertForSequenceClassification""", """SqueezeBertForTokenClassification""", """SqueezeBertModel""", """SqueezeBertModule""", """SqueezeBertPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_squeezebert import ( SQUEEZEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, SqueezeBertConfig, SqueezeBertOnnxConfig, ) from .tokenization_squeezebert import SqueezeBertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_squeezebert_fast import SqueezeBertTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_squeezebert import ( SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST, SqueezeBertForMaskedLM, SqueezeBertForMultipleChoice, SqueezeBertForQuestionAnswering, SqueezeBertForSequenceClassification, SqueezeBertForTokenClassification, SqueezeBertModel, SqueezeBertModule, SqueezeBertPreTrainedModel, ) else: import sys A_ : Optional[Any] = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
349
'''simple docstring''' from typing import Optional import pyspark from .. import Features, NamedSplit from ..download import DownloadMode from ..packaged_modules.spark.spark import Spark from .abc import AbstractDatasetReader class lowercase ( _lowerCamelCase ): """simple docstring""" def __init__( self ,a_ ,a_ = None ,a_ = None ,a_ = True ,a_ = None ,a_ = False ,a_ = None ,a_ = True ,a_ = "arrow" ,**a_ ,) -> str: super().__init__( split=a_ ,features=a_ ,cache_dir=a_ ,keep_in_memory=a_ ,streaming=a_ ,**a_ ,) _UpperCAmelCase : Any = load_from_cache_file _UpperCAmelCase : Optional[int] = file_format _UpperCAmelCase : int = Spark( df=a_ ,features=a_ ,cache_dir=a_ ,working_dir=a_ ,**a_ ,) def _snake_case ( self ) -> int: if self.streaming: return self.builder.as_streaming_dataset(split=self.split ) _UpperCAmelCase : str = None if self._load_from_cache_file else DownloadMode.FORCE_REDOWNLOAD self.builder.download_and_prepare( download_mode=a_ ,file_format=self._file_format ,) return self.builder.as_dataset(split=self.split )
349
1
def a__ ( _UpperCamelCase : int ): __lowerCamelCase = n ** (1 / 3) return (val * val * val) == n if __name__ == "__main__": print(perfect_cube(27)) print(perfect_cube(4))
330
from __future__ import annotations from typing import Generic, TypeVar a_ = TypeVar("""T""") class __lowerCAmelCase ( Generic[T] ): def __init__( self , __UpperCAmelCase ): '''simple docstring''' __lowerCamelCase = data __lowerCamelCase = self __lowerCamelCase = 0 class __lowerCAmelCase ( Generic[T] ): def __init__( self ): '''simple docstring''' # map from node name to the node object __lowerCamelCase = {} def lowerCamelCase ( self , __UpperCAmelCase ): '''simple docstring''' # create a new set with x as its member __lowerCamelCase = DisjointSetTreeNode(__UpperCAmelCase ) def lowerCamelCase ( self , __UpperCAmelCase ): '''simple docstring''' # find the set x belongs to (with path-compression) __lowerCamelCase = self.map[data] if elem_ref != elem_ref.parent: __lowerCamelCase = self.find_set(elem_ref.parent.data ) return elem_ref.parent def lowerCamelCase ( self , __UpperCAmelCase , __UpperCAmelCase ): '''simple docstring''' # helper function for union operation if nodea.rank > nodea.rank: __lowerCamelCase = nodea else: __lowerCamelCase = nodea if nodea.rank == nodea.rank: nodea.rank += 1 def lowerCamelCase ( self , __UpperCAmelCase , __UpperCAmelCase ): '''simple docstring''' # merge 2 disjoint sets self.link(self.find_set(__UpperCAmelCase ) , self.find_set(__UpperCAmelCase ) ) class __lowerCAmelCase ( Generic[T] ): def __init__( self ): '''simple docstring''' # connections: map from the node to the neighbouring nodes (with weights) __lowerCamelCase = {} def lowerCamelCase ( self , __UpperCAmelCase ): '''simple docstring''' # add a node ONLY if its not present in the graph if node not in self.connections: __lowerCamelCase = {} def lowerCamelCase ( self , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ): '''simple docstring''' # add an edge with the given weight self.add_node(__UpperCAmelCase ) self.add_node(__UpperCAmelCase ) __lowerCamelCase = weight __lowerCamelCase = weight def lowerCamelCase ( self ): '''simple docstring''' __lowerCamelCase = [] __lowerCamelCase = set() for start in self.connections: for end in self.connections[start]: if (start, end) not in seen: seen.add((end, start) ) edges.append((start, end, self.connections[start][end]) ) edges.sort(key=lambda __UpperCAmelCase : x[2] ) # creating the disjoint set __lowerCamelCase = DisjointSetTree[T]() for node in self.connections: disjoint_set.make_set(__UpperCAmelCase ) # MST generation __lowerCamelCase = 0 __lowerCamelCase = 0 __lowerCamelCase = GraphUndirectedWeighted[T]() while num_edges < len(self.connections ) - 1: __lowerCamelCase ,__lowerCamelCase ,__lowerCamelCase = edges[index] index += 1 __lowerCamelCase = disjoint_set.find_set(__UpperCAmelCase ) __lowerCamelCase = disjoint_set.find_set(__UpperCAmelCase ) if parent_u != parent_v: num_edges += 1 graph.add_edge(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) disjoint_set.union(__UpperCAmelCase , __UpperCAmelCase ) return graph
330
1
from __future__ import annotations def __lowerCamelCase ( _lowercase , _lowercase ) -> list[int]: UpperCAmelCase : Dict = 0 UpperCAmelCase : Union[str, Any] = len(_lowercase ) - 1 while i < j: if nums[i] + nums[j] == target: return [i, j] elif nums[i] + nums[j] < target: UpperCAmelCase : Dict = i + 1 else: UpperCAmelCase : List[str] = j - 1 return [] if __name__ == "__main__": import doctest doctest.testmod() print(F'''{two_pointer([2, 7, 1_1, 1_5], 9) = }''')
355
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available a : List[Any] = { """configuration_m2m_100""": ["""M2M_100_PRETRAINED_CONFIG_ARCHIVE_MAP""", """M2M100Config""", """M2M100OnnxConfig"""], """tokenization_m2m_100""": ["""M2M100Tokenizer"""], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a : Any = [ """M2M_100_PRETRAINED_MODEL_ARCHIVE_LIST""", """M2M100ForConditionalGeneration""", """M2M100Model""", """M2M100PreTrainedModel""", ] if TYPE_CHECKING: from .configuration_mam_aaa import M2M_100_PRETRAINED_CONFIG_ARCHIVE_MAP, MaMaaaConfig, MaMaaaOnnxConfig from .tokenization_mam_aaa import MaMaaaTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mam_aaa import ( M2M_100_PRETRAINED_MODEL_ARCHIVE_LIST, MaMaaaForConditionalGeneration, MaMaaaModel, MaMaaaPreTrainedModel, ) else: import sys a : List[Any] = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
338
0
"""simple docstring""" import argparse import json import torch from diffusers import DDPMScheduler, LDMPipeline, UNetaDModel, VQModel def lowercase_ ( _UpperCAmelCase , _UpperCAmelCase=1 ): """simple docstring""" if n_shave_prefix_segments >= 0: return ".".join(path.split('''.''' )[n_shave_prefix_segments:] ) else: return ".".join(path.split('''.''' )[:n_shave_prefix_segments] ) def lowercase_ ( _UpperCAmelCase , _UpperCAmelCase=0 ): """simple docstring""" A_ : Dict = [] for old_item in old_list: A_ : List[Any] = old_item.replace('''in_layers.0''' , '''norm1''' ) A_ : Tuple = new_item.replace('''in_layers.2''' , '''conv1''' ) A_ : Optional[int] = new_item.replace('''out_layers.0''' , '''norm2''' ) A_ : Optional[int] = new_item.replace('''out_layers.3''' , '''conv2''' ) A_ : Any = new_item.replace('''emb_layers.1''' , '''time_emb_proj''' ) A_ : str = new_item.replace('''skip_connection''' , '''conv_shortcut''' ) A_ : Union[str, Any] = shave_segments(_UpperCAmelCase , n_shave_prefix_segments=_UpperCAmelCase ) mapping.append({'''old''': old_item, '''new''': new_item} ) return mapping def lowercase_ ( _UpperCAmelCase , _UpperCAmelCase=0 ): """simple docstring""" A_ : Dict = [] for old_item in old_list: A_ : Tuple = old_item A_ : Optional[int] = new_item.replace('''norm.weight''' , '''group_norm.weight''' ) A_ : List[Any] = new_item.replace('''norm.bias''' , '''group_norm.bias''' ) A_ : Union[str, Any] = new_item.replace('''proj_out.weight''' , '''proj_attn.weight''' ) A_ : str = new_item.replace('''proj_out.bias''' , '''proj_attn.bias''' ) A_ : int = shave_segments(_UpperCAmelCase , n_shave_prefix_segments=_UpperCAmelCase ) mapping.append({'''old''': old_item, '''new''': new_item} ) return mapping def lowercase_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase=None , _UpperCAmelCase=None , _UpperCAmelCase=None ): """simple docstring""" assert isinstance(_UpperCAmelCase , _UpperCAmelCase ), "Paths should be a list of dicts containing 'old' and 'new' keys." # Splits the attention layers into three variables. if attention_paths_to_split is not None: for path, path_map in attention_paths_to_split.items(): A_ : List[Any] = old_checkpoint[path] A_ : Tuple = old_tensor.shape[0] // 3 A_ : Union[str, Any] = (-1, channels) if len(old_tensor.shape ) == 3 else (-1) A_ : Union[str, Any] = old_tensor.shape[0] // config['''num_head_channels'''] // 3 A_ : Optional[Any] = old_tensor.reshape((num_heads, 3 * channels // num_heads) + old_tensor.shape[1:] ) A_ , A_ , A_ : Optional[Any] = old_tensor.split(channels // num_heads , dim=1 ) A_ : int = query.reshape(_UpperCAmelCase ) A_ : Optional[Any] = key.reshape(_UpperCAmelCase ) A_ : Any = value.reshape(_UpperCAmelCase ) for path in paths: A_ : Optional[Any] = path['''new'''] # These have already been assigned if attention_paths_to_split is not None and new_path in attention_paths_to_split: continue # Global renaming happens here A_ : Tuple = new_path.replace('''middle_block.0''' , '''mid_block.resnets.0''' ) A_ : Tuple = new_path.replace('''middle_block.1''' , '''mid_block.attentions.0''' ) A_ : List[str] = new_path.replace('''middle_block.2''' , '''mid_block.resnets.1''' ) if additional_replacements is not None: for replacement in additional_replacements: A_ : List[Any] = new_path.replace(replacement['''old'''] , replacement['''new'''] ) # proj_attn.weight has to be converted from conv 1D to linear if "proj_attn.weight" in new_path: A_ : Union[str, Any] = old_checkpoint[path['''old''']][:, :, 0] else: A_ : Optional[Any] = old_checkpoint[path['''old''']] def lowercase_ ( _UpperCAmelCase , _UpperCAmelCase ): """simple docstring""" A_ : Any = {} A_ : Union[str, Any] = checkpoint['''time_embed.0.weight'''] A_ : Optional[Any] = checkpoint['''time_embed.0.bias'''] A_ : Optional[int] = checkpoint['''time_embed.2.weight'''] A_ : Dict = checkpoint['''time_embed.2.bias'''] A_ : Optional[int] = checkpoint['''input_blocks.0.0.weight'''] A_ : Union[str, Any] = checkpoint['''input_blocks.0.0.bias'''] A_ : List[str] = checkpoint['''out.0.weight'''] A_ : Union[str, Any] = checkpoint['''out.0.bias'''] A_ : str = checkpoint['''out.2.weight'''] A_ : str = checkpoint['''out.2.bias'''] # Retrieves the keys for the input blocks only A_ : Tuple = len({'''.'''.join(layer.split('''.''' )[:2] ) for layer in checkpoint if '''input_blocks''' in layer} ) A_ : str = { layer_id: [key for key in checkpoint if f"""input_blocks.{layer_id}""" in key] for layer_id in range(_UpperCAmelCase ) } # Retrieves the keys for the middle blocks only A_ : Tuple = len({'''.'''.join(layer.split('''.''' )[:2] ) for layer in checkpoint if '''middle_block''' in layer} ) A_ : List[str] = { layer_id: [key for key in checkpoint if f"""middle_block.{layer_id}""" in key] for layer_id in range(_UpperCAmelCase ) } # Retrieves the keys for the output blocks only A_ : Union[str, Any] = len({'''.'''.join(layer.split('''.''' )[:2] ) for layer in checkpoint if '''output_blocks''' in layer} ) A_ : Tuple = { layer_id: [key for key in checkpoint if f"""output_blocks.{layer_id}""" in key] for layer_id in range(_UpperCAmelCase ) } for i in range(1 , _UpperCAmelCase ): A_ : Dict = (i - 1) // (config['''num_res_blocks'''] + 1) A_ : int = (i - 1) % (config['''num_res_blocks'''] + 1) A_ : Union[str, Any] = [key for key in input_blocks[i] if f"""input_blocks.{i}.0""" in key] A_ : Optional[Any] = [key for key in input_blocks[i] if f"""input_blocks.{i}.1""" in key] if f"""input_blocks.{i}.0.op.weight""" in checkpoint: A_ : List[str] = checkpoint[ f"""input_blocks.{i}.0.op.weight""" ] A_ : List[Any] = checkpoint[ f"""input_blocks.{i}.0.op.bias""" ] continue A_ : Tuple = renew_resnet_paths(_UpperCAmelCase ) A_ : Tuple = {'''old''': f"""input_blocks.{i}.0""", '''new''': f"""down_blocks.{block_id}.resnets.{layer_in_block_id}"""} A_ : Union[str, Any] = {'''old''': '''resnets.2.op''', '''new''': '''downsamplers.0.op'''} assign_to_checkpoint( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , additional_replacements=[meta_path, resnet_op] , config=_UpperCAmelCase ) if len(_UpperCAmelCase ): A_ : Dict = renew_attention_paths(_UpperCAmelCase ) A_ : Union[str, Any] = { '''old''': f"""input_blocks.{i}.1""", '''new''': f"""down_blocks.{block_id}.attentions.{layer_in_block_id}""", } A_ : Any = { f"""input_blocks.{i}.1.qkv.bias""": { '''key''': f"""down_blocks.{block_id}.attentions.{layer_in_block_id}.key.bias""", '''query''': f"""down_blocks.{block_id}.attentions.{layer_in_block_id}.query.bias""", '''value''': f"""down_blocks.{block_id}.attentions.{layer_in_block_id}.value.bias""", }, f"""input_blocks.{i}.1.qkv.weight""": { '''key''': f"""down_blocks.{block_id}.attentions.{layer_in_block_id}.key.weight""", '''query''': f"""down_blocks.{block_id}.attentions.{layer_in_block_id}.query.weight""", '''value''': f"""down_blocks.{block_id}.attentions.{layer_in_block_id}.value.weight""", }, } assign_to_checkpoint( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , additional_replacements=[meta_path] , attention_paths_to_split=_UpperCAmelCase , config=_UpperCAmelCase , ) A_ : Any = middle_blocks[0] A_ : Tuple = middle_blocks[1] A_ : Dict = middle_blocks[2] A_ : str = renew_resnet_paths(_UpperCAmelCase ) assign_to_checkpoint(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , config=_UpperCAmelCase ) A_ : Union[str, Any] = renew_resnet_paths(_UpperCAmelCase ) assign_to_checkpoint(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , config=_UpperCAmelCase ) A_ : Optional[Any] = renew_attention_paths(_UpperCAmelCase ) A_ : Tuple = { '''middle_block.1.qkv.bias''': { '''key''': '''mid_block.attentions.0.key.bias''', '''query''': '''mid_block.attentions.0.query.bias''', '''value''': '''mid_block.attentions.0.value.bias''', }, '''middle_block.1.qkv.weight''': { '''key''': '''mid_block.attentions.0.key.weight''', '''query''': '''mid_block.attentions.0.query.weight''', '''value''': '''mid_block.attentions.0.value.weight''', }, } assign_to_checkpoint( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , attention_paths_to_split=_UpperCAmelCase , config=_UpperCAmelCase ) for i in range(_UpperCAmelCase ): A_ : Any = i // (config['''num_res_blocks'''] + 1) A_ : List[str] = i % (config['''num_res_blocks'''] + 1) A_ : Union[str, Any] = [shave_segments(_UpperCAmelCase , 2 ) for name in output_blocks[i]] A_ : Dict = {} for layer in output_block_layers: A_ , A_ : int = layer.split('''.''' )[0], shave_segments(_UpperCAmelCase , 1 ) if layer_id in output_block_list: output_block_list[layer_id].append(_UpperCAmelCase ) else: A_ : int = [layer_name] if len(_UpperCAmelCase ) > 1: A_ : Optional[Any] = [key for key in output_blocks[i] if f"""output_blocks.{i}.0""" in key] A_ : Optional[int] = [key for key in output_blocks[i] if f"""output_blocks.{i}.1""" in key] A_ : int = renew_resnet_paths(_UpperCAmelCase ) A_ : Union[str, Any] = renew_resnet_paths(_UpperCAmelCase ) A_ : Optional[int] = {'''old''': f"""output_blocks.{i}.0""", '''new''': f"""up_blocks.{block_id}.resnets.{layer_in_block_id}"""} assign_to_checkpoint(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , additional_replacements=[meta_path] , config=_UpperCAmelCase ) if ["conv.weight", "conv.bias"] in output_block_list.values(): A_ : List[str] = list(output_block_list.values() ).index(['''conv.weight''', '''conv.bias'''] ) A_ : Optional[Any] = checkpoint[ f"""output_blocks.{i}.{index}.conv.weight""" ] A_ : Optional[int] = checkpoint[ f"""output_blocks.{i}.{index}.conv.bias""" ] # Clear attentions as they have been attributed above. if len(_UpperCAmelCase ) == 2: A_ : Optional[int] = [] if len(_UpperCAmelCase ): A_ : Union[str, Any] = renew_attention_paths(_UpperCAmelCase ) A_ : Optional[Any] = { '''old''': f"""output_blocks.{i}.1""", '''new''': f"""up_blocks.{block_id}.attentions.{layer_in_block_id}""", } A_ : Optional[int] = { f"""output_blocks.{i}.1.qkv.bias""": { '''key''': f"""up_blocks.{block_id}.attentions.{layer_in_block_id}.key.bias""", '''query''': f"""up_blocks.{block_id}.attentions.{layer_in_block_id}.query.bias""", '''value''': f"""up_blocks.{block_id}.attentions.{layer_in_block_id}.value.bias""", }, f"""output_blocks.{i}.1.qkv.weight""": { '''key''': f"""up_blocks.{block_id}.attentions.{layer_in_block_id}.key.weight""", '''query''': f"""up_blocks.{block_id}.attentions.{layer_in_block_id}.query.weight""", '''value''': f"""up_blocks.{block_id}.attentions.{layer_in_block_id}.value.weight""", }, } assign_to_checkpoint( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , additional_replacements=[meta_path] , attention_paths_to_split=to_split if any('''qkv''' in key for key in attentions ) else None , config=_UpperCAmelCase , ) else: A_ : int = renew_resnet_paths(_UpperCAmelCase , n_shave_prefix_segments=1 ) for path in resnet_0_paths: A_ : Optional[int] = '''.'''.join(['''output_blocks''', str(_UpperCAmelCase ), path['''old''']] ) A_ : int = '''.'''.join(['''up_blocks''', str(_UpperCAmelCase ), '''resnets''', str(_UpperCAmelCase ), path['''new''']] ) A_ : Tuple = checkpoint[old_path] return new_checkpoint if __name__ == "__main__": _lowerCamelCase : List[Any] = argparse.ArgumentParser() parser.add_argument( '--checkpoint_path', default=None, type=str, required=True, help='Path to the checkpoint to convert.' ) parser.add_argument( '--config_file', default=None, type=str, required=True, help='The config json file corresponding to the architecture.', ) parser.add_argument('--dump_path', default=None, type=str, required=True, help='Path to the output model.') _lowerCamelCase : Dict = parser.parse_args() _lowerCamelCase : Dict = torch.load(args.checkpoint_path) with open(args.config_file) as f: _lowerCamelCase : List[Any] = json.loads(f.read()) _lowerCamelCase : str = convert_ldm_checkpoint(checkpoint, config) if "ldm" in config: del config["ldm"] _lowerCamelCase : Union[str, Any] = UNetaDModel(**config) model.load_state_dict(converted_checkpoint) try: _lowerCamelCase : int = DDPMScheduler.from_config('/'.join(args.checkpoint_path.split('/')[:-1])) _lowerCamelCase : int = VQModel.from_pretrained('/'.join(args.checkpoint_path.split('/')[:-1])) _lowerCamelCase : Dict = LDMPipeline(unet=model, scheduler=scheduler, vae=vqvae) pipe.save_pretrained(args.dump_path) except: # noqa: E722 model.save_pretrained(args.dump_path)
167
"""simple docstring""" import unittest import numpy as np import timeout_decorator # noqa from transformers import BlenderbotSmallConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...generation.test_flax_utils import FlaxGenerationTesterMixin from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor if is_flax_available(): import os # The slow tests are often failing with OOM error on GPU # This makes JAX allocate exactly what is needed on demand, and deallocate memory that is no longer needed # but will be slower as stated here https://jax.readthedocs.io/en/latest/gpu_memory_allocation.html _lowerCamelCase : Tuple = 'platform' import jax import jax.numpy as jnp from transformers.models.blenderbot_small.modeling_flax_blenderbot_small import ( FlaxBlenderbotSmallForConditionalGeneration, FlaxBlenderbotSmallModel, shift_tokens_right, ) def lowercase_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase=None , _UpperCAmelCase=None , _UpperCAmelCase=None , _UpperCAmelCase=None , _UpperCAmelCase=None , _UpperCAmelCase=None , ): """simple docstring""" if attention_mask is None: A_ : int = np.where(input_ids != config.pad_token_id , 1 , 0 ) if decoder_attention_mask is None: A_ : List[Any] = np.where(decoder_input_ids != config.pad_token_id , 1 , 0 ) if head_mask is None: A_ : List[Any] = np.ones((config.encoder_layers, config.encoder_attention_heads) ) if decoder_head_mask is None: A_ : Dict = np.ones((config.decoder_layers, config.decoder_attention_heads) ) if cross_attn_head_mask is None: A_ : Optional[Any] = np.ones((config.decoder_layers, config.decoder_attention_heads) ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": attention_mask, } class lowercase : def __init__( self : int , _lowerCamelCase : Optional[Any] , _lowerCamelCase : List[Any]=13 , _lowerCamelCase : Optional[int]=7 , _lowerCamelCase : Optional[int]=True , _lowerCamelCase : Tuple=False , _lowerCamelCase : Dict=99 , _lowerCamelCase : List[Any]=16 , _lowerCamelCase : Any=2 , _lowerCamelCase : Union[str, Any]=4 , _lowerCamelCase : Dict=4 , _lowerCamelCase : Any="gelu" , _lowerCamelCase : Any=0.1 , _lowerCamelCase : Tuple=0.1 , _lowerCamelCase : List[Any]=32 , _lowerCamelCase : str=2 , _lowerCamelCase : List[Any]=1 , _lowerCamelCase : Optional[int]=0 , _lowerCamelCase : Optional[Any]=0.02 , ): """simple docstring""" A_ : Any = parent A_ : Any = batch_size A_ : Optional[Any] = seq_length A_ : Union[str, Any] = is_training A_ : Optional[Any] = use_labels A_ : str = vocab_size A_ : Optional[Any] = hidden_size A_ : Dict = num_hidden_layers A_ : List[str] = num_attention_heads A_ : List[str] = intermediate_size A_ : int = hidden_act A_ : List[Any] = hidden_dropout_prob A_ : List[Any] = attention_probs_dropout_prob A_ : List[Any] = max_position_embeddings A_ : Tuple = eos_token_id A_ : int = pad_token_id A_ : int = bos_token_id A_ : str = initializer_range def a_ ( self : List[Any] ): """simple docstring""" A_ : Optional[Any] = np.clip(ids_tensor([self.batch_size, self.seq_length - 1] , self.vocab_size ) , 3 , self.vocab_size ) A_ : Optional[int] = np.concatenate((input_ids, 2 * np.ones((self.batch_size, 1) , dtype=np.intaa )) , -1 ) A_ : Optional[Any] = shift_tokens_right(_lowerCamelCase , 1 , 2 ) A_ : Optional[Any] = BlenderbotSmallConfig( vocab_size=self.vocab_size , d_model=self.hidden_size , encoder_layers=self.num_hidden_layers , decoder_layers=self.num_hidden_layers , encoder_attention_heads=self.num_attention_heads , decoder_attention_heads=self.num_attention_heads , encoder_ffn_dim=self.intermediate_size , decoder_ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , eos_token_id=self.eos_token_id , bos_token_id=self.bos_token_id , pad_token_id=self.pad_token_id , initializer_range=self.initializer_range , use_cache=_lowerCamelCase , ) A_ : Any = prepare_blenderbot_inputs_dict(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) return config, inputs_dict def a_ ( self : Optional[int] ): """simple docstring""" A_ , A_ : str = self.prepare_config_and_inputs() return config, inputs_dict def a_ ( self : int , _lowerCamelCase : Union[str, Any] , _lowerCamelCase : Dict , _lowerCamelCase : Dict ): """simple docstring""" A_ : str = 20 A_ : Any = model_class_name(_lowerCamelCase ) A_ : List[Any] = model.encode(inputs_dict['''input_ids'''] ) A_ , A_ : int = ( inputs_dict['''decoder_input_ids'''], inputs_dict['''decoder_attention_mask'''], ) A_ : int = model.init_cache(decoder_input_ids.shape[0] , _lowerCamelCase , _lowerCamelCase ) A_ : List[Any] = jnp.ones((decoder_input_ids.shape[0], max_decoder_length) , dtype='''i4''' ) A_ : Dict = jnp.broadcast_to( jnp.arange(decoder_input_ids.shape[-1] - 1 )[None, :] , (decoder_input_ids.shape[0], decoder_input_ids.shape[-1] - 1) , ) A_ : Optional[int] = model.decode( decoder_input_ids[:, :-1] , _lowerCamelCase , decoder_attention_mask=_lowerCamelCase , past_key_values=_lowerCamelCase , decoder_position_ids=_lowerCamelCase , ) A_ : List[str] = jnp.array(decoder_input_ids.shape[0] * [[decoder_input_ids.shape[-1] - 1]] , dtype='''i4''' ) A_ : Tuple = model.decode( decoder_input_ids[:, -1:] , _lowerCamelCase , decoder_attention_mask=_lowerCamelCase , past_key_values=outputs_cache.past_key_values , decoder_position_ids=_lowerCamelCase , ) A_ : str = model.decode(_lowerCamelCase , _lowerCamelCase ) A_ : List[str] = np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) ) self.parent.assertTrue(diff < 1E-3 , msg=F"""Max diff is {diff}""" ) def a_ ( self : List[Any] , _lowerCamelCase : Any , _lowerCamelCase : int , _lowerCamelCase : Optional[Any] ): """simple docstring""" A_ : Union[str, Any] = 20 A_ : Dict = model_class_name(_lowerCamelCase ) A_ : Dict = model.encode(inputs_dict['''input_ids'''] ) A_ , A_ : Optional[int] = ( inputs_dict['''decoder_input_ids'''], inputs_dict['''decoder_attention_mask'''], ) A_ : Union[str, Any] = jnp.concatenate( [ decoder_attention_mask, jnp.zeros((decoder_attention_mask.shape[0], max_decoder_length - decoder_attention_mask.shape[1]) ), ] , axis=-1 , ) A_ : Dict = model.init_cache(decoder_input_ids.shape[0] , _lowerCamelCase , _lowerCamelCase ) A_ : Union[str, Any] = jnp.broadcast_to( jnp.arange(decoder_input_ids.shape[-1] - 1 )[None, :] , (decoder_input_ids.shape[0], decoder_input_ids.shape[-1] - 1) , ) A_ : Optional[int] = model.decode( decoder_input_ids[:, :-1] , _lowerCamelCase , decoder_attention_mask=_lowerCamelCase , past_key_values=_lowerCamelCase , decoder_position_ids=_lowerCamelCase , ) A_ : Optional[Any] = jnp.array(decoder_input_ids.shape[0] * [[decoder_input_ids.shape[-1] - 1]] , dtype='''i4''' ) A_ : List[str] = model.decode( decoder_input_ids[:, -1:] , _lowerCamelCase , past_key_values=outputs_cache.past_key_values , decoder_attention_mask=_lowerCamelCase , decoder_position_ids=_lowerCamelCase , ) A_ : Tuple = model.decode(_lowerCamelCase , _lowerCamelCase , decoder_attention_mask=_lowerCamelCase ) A_ : Dict = np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) ) self.parent.assertTrue(diff < 1E-3 , msg=F"""Max diff is {diff}""" ) @require_flax class lowercase ( unittest.TestCase): __lowerCAmelCase : Dict = 99 def a_ ( self : str ): """simple docstring""" A_ : List[str] = np.array( [ [71, 82, 18, 33, 46, 91, 2], [68, 34, 26, 58, 30, 82, 2], [5, 97, 17, 39, 94, 40, 2], [76, 83, 94, 25, 70, 78, 2], [87, 59, 41, 35, 48, 66, 2], [55, 13, 16, 58, 5, 2, 1], # note padding [64, 27, 31, 51, 12, 75, 2], [52, 64, 86, 17, 83, 39, 2], [48, 61, 9, 24, 71, 82, 2], [26, 1, 60, 48, 22, 13, 2], [21, 5, 62, 28, 14, 76, 2], [45, 98, 37, 86, 59, 48, 2], [70, 70, 50, 9, 28, 0, 2], ] , dtype=np.intaa , ) A_ : List[str] = input_ids.shape[0] A_ : Optional[int] = BlenderbotSmallConfig( vocab_size=self.vocab_size , d_model=24 , encoder_layers=2 , decoder_layers=2 , encoder_attention_heads=2 , decoder_attention_heads=2 , encoder_ffn_dim=32 , decoder_ffn_dim=32 , max_position_embeddings=48 , eos_token_id=2 , pad_token_id=1 , bos_token_id=0 , ) return config, input_ids, batch_size def a_ ( self : List[str] ): """simple docstring""" A_ , A_ , A_ : List[Any] = self._get_config_and_data() A_ : Dict = FlaxBlenderbotSmallForConditionalGeneration(_lowerCamelCase ) A_ : Optional[int] = lm_model(input_ids=_lowerCamelCase ) A_ : Optional[Any] = (batch_size, input_ids.shape[1], config.vocab_size) self.assertEqual(outputs['''logits'''].shape , _lowerCamelCase ) def a_ ( self : str ): """simple docstring""" A_ : Tuple = BlenderbotSmallConfig( vocab_size=self.vocab_size , d_model=14 , encoder_layers=2 , decoder_layers=2 , encoder_attention_heads=2 , decoder_attention_heads=2 , encoder_ffn_dim=8 , decoder_ffn_dim=8 , max_position_embeddings=48 , ) A_ : Optional[int] = FlaxBlenderbotSmallForConditionalGeneration(_lowerCamelCase ) A_ : List[str] = np.array([[71, 82, 18, 33, 46, 91, 2], [68, 34, 26, 58, 30, 2, 1]] , dtype=np.intaa ) A_ : Optional[int] = np.array([[82, 71, 82, 18, 2], [58, 68, 2, 1, 1]] , dtype=np.intaa ) A_ : Dict = lm_model(input_ids=_lowerCamelCase , decoder_input_ids=_lowerCamelCase ) A_ : Any = (*summary.shape, config.vocab_size) self.assertEqual(outputs['''logits'''].shape , _lowerCamelCase ) def a_ ( self : Union[str, Any] ): """simple docstring""" A_ : int = np.array([[71, 82, 18, 33, 2, 1, 1], [68, 34, 26, 58, 30, 82, 2]] , dtype=np.intaa ) A_ : Tuple = shift_tokens_right(_lowerCamelCase , 1 , 2 ) A_ : Optional[int] = np.equal(_lowerCamelCase , 1 ).astype(np.floataa ).sum() A_ : Tuple = np.equal(_lowerCamelCase , 1 ).astype(np.floataa ).sum() self.assertEqual(shifted.shape , input_ids.shape ) self.assertEqual(_lowerCamelCase , n_pad_before - 1 ) self.assertTrue(np.equal(shifted[:, 0] , 2 ).all() ) @require_flax class lowercase ( __UpperCAmelCase , unittest.TestCase , __UpperCAmelCase): __lowerCAmelCase : Any = True __lowerCAmelCase : List[Any] = ( ( FlaxBlenderbotSmallModel, FlaxBlenderbotSmallForConditionalGeneration, ) if is_flax_available() else () ) __lowerCAmelCase : List[str] = (FlaxBlenderbotSmallForConditionalGeneration,) if is_flax_available() else () def a_ ( self : Tuple ): """simple docstring""" A_ : Optional[int] = FlaxBlenderbotSmallModelTester(self ) def a_ ( self : List[str] ): """simple docstring""" A_ , A_ : int = self.model_tester.prepare_config_and_inputs() for model_class in self.all_model_classes: self.model_tester.check_use_cache_forward(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) def a_ ( self : Tuple ): """simple docstring""" A_ , A_ : Tuple = self.model_tester.prepare_config_and_inputs() for model_class in self.all_model_classes: self.model_tester.check_use_cache_forward_with_attn_mask(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) def a_ ( self : List[Any] ): """simple docstring""" A_ , A_ : Dict = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): A_ : str = self._prepare_for_class(_lowerCamelCase , _lowerCamelCase ) A_ : Tuple = model_class(_lowerCamelCase ) @jax.jit def encode_jitted(_lowerCamelCase : Optional[int] , _lowerCamelCase : Union[str, Any]=None , **_lowerCamelCase : List[str] ): return model.encode(input_ids=_lowerCamelCase , attention_mask=_lowerCamelCase ) with self.subTest('''JIT Enabled''' ): A_ : Optional[Any] = encode_jitted(**_lowerCamelCase ).to_tuple() with self.subTest('''JIT Disabled''' ): with jax.disable_jit(): A_ : List[Any] = encode_jitted(**_lowerCamelCase ).to_tuple() self.assertEqual(len(_lowerCamelCase ) , len(_lowerCamelCase ) ) for jitted_output, output in zip(_lowerCamelCase , _lowerCamelCase ): self.assertEqual(jitted_output.shape , output.shape ) def a_ ( self : Tuple ): """simple docstring""" A_ , A_ : Dict = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): A_ : Union[str, Any] = model_class(_lowerCamelCase ) A_ : Optional[Any] = model.encode(inputs_dict['''input_ids'''] , inputs_dict['''attention_mask'''] ) A_ : Tuple = { '''decoder_input_ids''': inputs_dict['''decoder_input_ids'''], '''decoder_attention_mask''': inputs_dict['''decoder_attention_mask'''], '''encoder_outputs''': encoder_outputs, } @jax.jit def decode_jitted(_lowerCamelCase : List[str] , _lowerCamelCase : int , _lowerCamelCase : Dict ): return model.decode( decoder_input_ids=_lowerCamelCase , decoder_attention_mask=_lowerCamelCase , encoder_outputs=_lowerCamelCase , ) with self.subTest('''JIT Enabled''' ): A_ : Union[str, Any] = decode_jitted(**_lowerCamelCase ).to_tuple() with self.subTest('''JIT Disabled''' ): with jax.disable_jit(): A_ : Optional[Any] = decode_jitted(**_lowerCamelCase ).to_tuple() self.assertEqual(len(_lowerCamelCase ) , len(_lowerCamelCase ) ) for jitted_output, output in zip(_lowerCamelCase , _lowerCamelCase ): self.assertEqual(jitted_output.shape , output.shape ) @slow def a_ ( self : Tuple ): """simple docstring""" for model_class_name in self.all_model_classes: A_ : str = model_class_name.from_pretrained('''facebook/blenderbot_small-90M''' ) # FlaxBlenderbotForSequenceClassification expects eos token in input_ids A_ : str = np.ones((1, 1) ) * model.config.eos_token_id A_ : List[Any] = model(_lowerCamelCase ) self.assertIsNotNone(_lowerCamelCase )
167
1
"""simple docstring""" import argparse import logging import os from pathlib import Path from typing import Any, Dict import pytorch_lightning as pl from pytorch_lightning.utilities import rank_zero_info from transformers import ( AdamW, AutoConfig, AutoModel, AutoModelForPreTraining, AutoModelForQuestionAnswering, AutoModelForSeqaSeqLM, AutoModelForSequenceClassification, AutoModelForTokenClassification, AutoModelWithLMHead, AutoTokenizer, PretrainedConfig, PreTrainedTokenizer, ) from transformers.optimization import ( Adafactor, get_cosine_schedule_with_warmup, get_cosine_with_hard_restarts_schedule_with_warmup, get_linear_schedule_with_warmup, get_polynomial_decay_schedule_with_warmup, ) from transformers.utils.versions import require_version a : int = logging.getLogger(__name__) require_version('''pytorch_lightning>=1.0.4''') a : Optional[int] = { '''base''': AutoModel, '''sequence-classification''': AutoModelForSequenceClassification, '''question-answering''': AutoModelForQuestionAnswering, '''pretraining''': AutoModelForPreTraining, '''token-classification''': AutoModelForTokenClassification, '''language-modeling''': AutoModelWithLMHead, '''summarization''': AutoModelForSeqaSeqLM, '''translation''': AutoModelForSeqaSeqLM, } # update this and the import above to support new schedulers from transformers.optimization a : int = { '''linear''': get_linear_schedule_with_warmup, '''cosine''': get_cosine_schedule_with_warmup, '''cosine_w_restarts''': get_cosine_with_hard_restarts_schedule_with_warmup, '''polynomial''': get_polynomial_decay_schedule_with_warmup, # '': get_constant_schedule, # not supported for now # '': get_constant_schedule_with_warmup, # not supported for now } a : Optional[Any] = sorted(arg_to_scheduler.keys()) a : List[str] = '''{''' + ''', '''.join(arg_to_scheduler_choices) + '''}''' class __UpperCamelCase ( pl.LightningModule ): def __init__( self , lowerCAmelCase__ , lowerCAmelCase__=None , lowerCAmelCase__="base" , lowerCAmelCase__=None , lowerCAmelCase__=None , lowerCAmelCase__=None , **lowerCAmelCase__ , ) -> Dict: super().__init__() # TODO: move to self.save_hyperparameters() # self.save_hyperparameters() # can also expand arguments into trainer signature for easier reading self.save_hyperparameters(lowerCAmelCase__ ) a : List[Any] = 0 a : str = Path(self.hparams.output_dir ) a : str = self.hparams.cache_dir if self.hparams.cache_dir else None if config is None: a : Any = AutoConfig.from_pretrained( self.hparams.config_name if self.hparams.config_name else self.hparams.model_name_or_path , **({"num_labels": num_labels} if num_labels is not None else {}) , cache_dir=lowerCAmelCase__ , **lowerCAmelCase__ , ) else: a : PretrainedConfig = config a : Tuple = ("encoder_layerdrop", "decoder_layerdrop", "dropout", "attention_dropout") for p in extra_model_params: if getattr(self.hparams , lowerCAmelCase__ , lowerCAmelCase__ ): assert hasattr(self.config , lowerCAmelCase__ ), f"""model config doesn't have a `{p}` attribute""" setattr(self.config , lowerCAmelCase__ , getattr(self.hparams , lowerCAmelCase__ ) ) if tokenizer is None: a : Union[str, Any] = AutoTokenizer.from_pretrained( self.hparams.tokenizer_name if self.hparams.tokenizer_name else self.hparams.model_name_or_path , cache_dir=lowerCAmelCase__ , ) else: a : PreTrainedTokenizer = tokenizer a : Any = MODEL_MODES[mode] if model is None: a : int = self.model_type.from_pretrained( self.hparams.model_name_or_path , from_tf=bool(".ckpt" in self.hparams.model_name_or_path ) , config=self.config , cache_dir=lowerCAmelCase__ , ) else: a : List[str] = model def __a ( self , *lowerCAmelCase__ , **lowerCAmelCase__ ) -> List[str]: a : str = self.model_type.from_pretrained(*lowerCAmelCase__ , **lowerCAmelCase__ ) def __a ( self ) -> Optional[int]: a : Any = arg_to_scheduler[self.hparams.lr_scheduler] a : Optional[Any] = get_schedule_func( self.opt , num_warmup_steps=self.hparams.warmup_steps , num_training_steps=self.total_steps() ) a : List[Any] = {"scheduler": scheduler, "interval": "step", "frequency": 1} return scheduler def __a ( self ) -> Optional[int]: a : List[str] = self.model a : Dict = ["bias", "LayerNorm.weight"] a : List[str] = [ { "params": [ p for n, p in model.named_parameters() if not any(nd in n for nd in no_decay ) ], # check this named paramters "weight_decay": self.hparams.weight_decay, }, { "params": [p for n, p in model.named_parameters() if any(nd in n for nd in no_decay )], "weight_decay": 0.0, }, ] if self.hparams.adafactor: a : Dict = Adafactor( lowerCAmelCase__ , lr=self.hparams.learning_rate , scale_parameter=lowerCAmelCase__ , relative_step=lowerCAmelCase__ ) else: a : Optional[int] = AdamW( lowerCAmelCase__ , lr=self.hparams.learning_rate , eps=self.hparams.adam_epsilon ) a : Optional[Any] = optimizer a : Union[str, Any] = self.get_lr_scheduler() return [optimizer], [scheduler] def __a ( self , lowerCAmelCase__ , lowerCAmelCase__ ) -> Optional[Any]: return self.validation_step(lowerCAmelCase__ , lowerCAmelCase__ ) def __a ( self , lowerCAmelCase__ ) -> str: return self.validation_end(lowerCAmelCase__ ) def __a ( self ) -> int: a : Union[str, Any] = max(1 , self.hparams.gpus ) # TODO: consider num_tpu_cores a : Union[str, Any] = self.hparams.train_batch_size * self.hparams.accumulate_grad_batches * num_devices return (self.dataset_size / effective_batch_size) * self.hparams.max_epochs def __a ( self , lowerCAmelCase__ ) -> Union[str, Any]: if stage == "test": a : int = len(self.test_dataloader().dataset ) else: a : Optional[Any] = self.get_dataloader("train" , self.hparams.train_batch_size , shuffle=lowerCAmelCase__ ) a : Union[str, Any] = len(self.train_dataloader().dataset ) def __a ( self , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ = False ) -> List[str]: raise NotImplementedError("You must implement this for your task" ) def __a ( self ) -> List[Any]: return self.train_loader def __a ( self ) -> Any: return self.get_dataloader("dev" , self.hparams.eval_batch_size , shuffle=lowerCAmelCase__ ) def __a ( self ) -> int: return self.get_dataloader("test" , self.hparams.eval_batch_size , shuffle=lowerCAmelCase__ ) def __a ( self , lowerCAmelCase__ ) -> Optional[Any]: return os.path.join( self.hparams.data_dir , "cached_{}_{}_{}".format( lowerCAmelCase__ , list(filter(lowerCAmelCase__ , self.hparams.model_name_or_path.split("/" ) ) ).pop() , str(self.hparams.max_seq_length ) , ) , ) @pl.utilities.rank_zero_only def __a ( self , lowerCAmelCase__ ) -> None: a : Tuple = self.output_dir.joinpath("best_tfmr" ) a : str = self.step_count self.model.save_pretrained(lowerCAmelCase__ ) self.tokenizer.save_pretrained(lowerCAmelCase__ ) @staticmethod def __a ( lowerCAmelCase__ , lowerCAmelCase__ ) -> Union[str, Any]: parser.add_argument( "--model_name_or_path" , default=lowerCAmelCase__ , type=lowerCAmelCase__ , required=lowerCAmelCase__ , help="Path to pretrained model or model identifier from huggingface.co/models" , ) parser.add_argument( "--config_name" , default="" , type=lowerCAmelCase__ , help="Pretrained config name or path if not the same as model_name" ) parser.add_argument( "--tokenizer_name" , default=lowerCAmelCase__ , type=lowerCAmelCase__ , help="Pretrained tokenizer name or path if not the same as model_name" , ) parser.add_argument( "--cache_dir" , default=str(Path(lowerCAmelCase__ ).parent / "test_run" / "cache" ) , type=lowerCAmelCase__ , help="Where do you want to store the pre-trained models downloaded from huggingface.co" , ) parser.add_argument( "--encoder_layerdrop" , type=lowerCAmelCase__ , help="Encoder layer dropout probability (Optional). Goes into model.config" , ) parser.add_argument( "--decoder_layerdrop" , type=lowerCAmelCase__ , help="Decoder layer dropout probability (Optional). Goes into model.config" , ) parser.add_argument( "--dropout" , type=lowerCAmelCase__ , help="Dropout probability (Optional). Goes into model.config" , ) parser.add_argument( "--attention_dropout" , type=lowerCAmelCase__ , help="Attention dropout probability (Optional). Goes into model.config" , ) parser.add_argument("--learning_rate" , default=5E-5 , type=lowerCAmelCase__ , help="The initial learning rate for Adam." ) parser.add_argument( "--lr_scheduler" , default="linear" , choices=lowerCAmelCase__ , metavar=lowerCAmelCase__ , type=lowerCAmelCase__ , help="Learning rate scheduler" , ) parser.add_argument("--weight_decay" , default=0.0 , type=lowerCAmelCase__ , help="Weight decay if we apply some." ) parser.add_argument("--adam_epsilon" , default=1E-8 , type=lowerCAmelCase__ , help="Epsilon for Adam optimizer." ) parser.add_argument("--warmup_steps" , default=0 , type=lowerCAmelCase__ , help="Linear warmup over warmup_steps." ) parser.add_argument("--num_workers" , default=4 , type=lowerCAmelCase__ , help="kwarg passed to DataLoader" ) parser.add_argument("--num_train_epochs" , dest="max_epochs" , default=3 , type=lowerCAmelCase__ ) parser.add_argument("--train_batch_size" , default=32 , type=lowerCAmelCase__ ) parser.add_argument("--eval_batch_size" , default=32 , type=lowerCAmelCase__ ) parser.add_argument("--adafactor" , action="store_true" ) class __UpperCamelCase ( pl.Callback ): def __a ( self , lowerCAmelCase__ , lowerCAmelCase__ ) -> Optional[Any]: if ( trainer.is_global_zero and trainer.global_rank == 0 ): # we initialize the retriever only on master worker with RAY. In new pytorch-lightning accelorators are removed. pl_module.model.rag.retriever.init_retrieval() # better to use hook functions. class __UpperCamelCase ( pl.Callback ): def __a ( self , lowerCAmelCase__ , lowerCAmelCase__ ) -> List[Any]: # print(pl_module.model.rag) for name, param in pl_module.model.rag.named_parameters(): if param.grad is None: print(lowerCAmelCase__ ) class __UpperCamelCase ( pl.Callback ): def __a ( self , lowerCAmelCase__ , lowerCAmelCase__ ) -> str: a : Any = trainer.lr_schedulers[0]["scheduler"] a : Union[str, Any] = {f"""lr_group_{i}""": lr for i, lr in enumerate(lr_scheduler.get_lr() )} pl_module.logger.log_metrics(lowerCAmelCase__ ) def __a ( self , lowerCAmelCase__ , lowerCAmelCase__ ) -> Optional[int]: rank_zero_info("***** Validation results *****" ) a : Dict = trainer.callback_metrics # Log results for key in sorted(lowerCAmelCase__ ): if key not in ["log", "progress_bar"]: rank_zero_info("{} = {}\n".format(lowerCAmelCase__ , str(metrics[key] ) ) ) def __a ( self , lowerCAmelCase__ , lowerCAmelCase__ ) -> List[str]: rank_zero_info("***** Test results *****" ) a : Any = trainer.callback_metrics # Log and save results to file a : Union[str, Any] = os.path.join(pl_module.hparams.output_dir , "test_results.txt" ) with open(lowerCAmelCase__ , "w" ) as writer: for key in sorted(lowerCAmelCase__ ): if key not in ["log", "progress_bar"]: rank_zero_info("{} = {}\n".format(lowerCAmelCase__ , str(metrics[key] ) ) ) writer.write("{} = {}\n".format(lowerCAmelCase__ , str(metrics[key] ) ) ) def _SCREAMING_SNAKE_CASE ( _lowercase : Tuple , _lowercase : Tuple ) ->None: '''simple docstring''' parser.add_argument( "--output_dir" , default=str(Path(_lowercase ).parent / "test_run" / "model_checkpoints" ) , type=_lowercase , help="The output directory where the model predictions and checkpoints will be written." , ) parser.add_argument( "--fp16" , action="store_true" , help="Whether to use 16-bit (mixed) precision (through NVIDIA apex) instead of 32-bit" , ) parser.add_argument( "--fp16_opt_level" , type=_lowercase , default="O2" , help=( "For fp16: Apex AMP optimization level selected in ['O0', 'O1', 'O2', and 'O3']." "See details at https://nvidia.github.io/apex/amp.html" ) , ) parser.add_argument("--n_tpu_cores" , dest="tpu_cores" , type=_lowercase ) parser.add_argument("--max_grad_norm" , dest="gradient_clip_val" , default=1.0 , type=_lowercase , help="Max gradient norm" ) parser.add_argument("--do_train" , action="store_true" , help="Whether to run training." ) parser.add_argument("--do_predict" , action="store_true" , help="Whether to run predictions on the test set." ) parser.add_argument( "--gradient_accumulation_steps" , dest="accumulate_grad_batches" , type=_lowercase , default=1 , help="Number of updates steps to accumulate before performing a backward/update pass." , ) parser.add_argument("--seed" , type=_lowercase , default=42 , help="random seed for initialization" ) parser.add_argument( "--data_dir" , default=str(Path(_lowercase ).parent / "test_run" / "dummy-train-data" ) , type=_lowercase , help="The input data dir. Should contain the training files for the CoNLL-2003 NER task." , ) def _SCREAMING_SNAKE_CASE ( _lowercase : BaseTransformer , _lowercase : argparse.Namespace , _lowercase : Tuple=None , _lowercase : int=True , _lowercase : List[str]=[] , _lowercase : Union[str, Any]=None , _lowercase : Any=None , **_lowercase : Any , ) ->Tuple: '''simple docstring''' pl.seed_everything(args.seed ) # init model a : int = Path(model.hparams.output_dir ) odir.mkdir(exist_ok=_lowercase ) # add custom checkpoints if checkpoint_callback is None: a : Optional[int] = pl.callbacks.ModelCheckpoint( filepath=args.output_dir , prefix="checkpoint" , monitor="val_loss" , mode="min" , save_top_k=1 ) if early_stopping_callback: extra_callbacks.append(_lowercase ) if logging_callback is None: a : str = LoggingCallback() a : Optional[Any] = {} if args.fpaa: a : Dict = 16 if args.gpus > 1: a : Dict = "auto" a : Tuple = "ddp" a : Optional[int] = args.accumulate_grad_batches a : Tuple = None a : str = "auto" a : Union[str, Any] = pl.Trainer.from_argparse_args( _lowercase , weights_summary=_lowercase , callbacks=[logging_callback] + extra_callbacks + [InitCallback()] + [checkpoint_callback] , logger=_lowercase , val_check_interval=1 , num_sanity_val_steps=2 , **_lowercase , ) if args.do_train: trainer.fit(_lowercase ) else: print("RAG modeling tests with new set functions successfuly executed!" ) return trainer
79
"""simple docstring""" import sacrebleu as scb from packaging import version from sacrebleu import TER import datasets a : Tuple = '''\ @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 : List[str] = '''\ 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 : List[Any] = ''' 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 __UpperCamelCase ( datasets.Metric ): def __a ( self ) -> Dict: 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 __a ( self , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ = False , lowerCAmelCase__ = False , lowerCAmelCase__ = False , lowerCAmelCase__ = False , ) -> Any: a : Optional[int] = len(references[0] ) if any(len(lowerCAmelCase__ ) != references_per_prediction for refs in references ): raise ValueError("Sacrebleu requires the same number of references for each prediction" ) a : List[str] = [[refs[i] for refs in references] for i in range(lowerCAmelCase__ )] a : Union[str, Any] = TER( normalized=lowerCAmelCase__ , no_punct=lowerCAmelCase__ , asian_support=lowerCAmelCase__ , case_sensitive=lowerCAmelCase__ , ) a : Optional[Any] = sb_ter.corpus_score(lowerCAmelCase__ , lowerCAmelCase__ ) return {"score": output.score, "num_edits": output.num_edits, "ref_length": output.ref_length}
79
1
'''simple docstring''' from ...utils import ( OptionalDependencyNotAvailable, is_flax_available, is_torch_available, is_transformers_available, ) try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import * # noqa F403 else: from .multicontrolnet import MultiControlNetModel from .pipeline_controlnet import StableDiffusionControlNetPipeline from .pipeline_controlnet_imgaimg import StableDiffusionControlNetImgaImgPipeline from .pipeline_controlnet_inpaint import StableDiffusionControlNetInpaintPipeline if is_transformers_available() and is_flax_available(): from .pipeline_flax_controlnet import FlaxStableDiffusionControlNetPipeline
34
'''simple docstring''' from math import acos, sin from typing import List, Tuple, Union import numpy as np import torch from PIL import Image from ...models import AutoencoderKL, UNetaDConditionModel from ...schedulers import DDIMScheduler, DDPMScheduler from ...utils import randn_tensor from ..pipeline_utils import AudioPipelineOutput, BaseOutput, DiffusionPipeline, ImagePipelineOutput from .mel import Mel class _a ( __a ): __a : str = ["""vqvae"""] def __init__( self : str , lowercase : AutoencoderKL , lowercase : UNetaDConditionModel , lowercase : Mel , lowercase : Union[DDIMScheduler, DDPMScheduler] , ): '''simple docstring''' super().__init__() self.register_modules(unet=lowercase , scheduler=lowercase , mel=lowercase , vqvae=lowercase ) def A ( self : Optional[Any] ): '''simple docstring''' return 50 if isinstance(self.scheduler , lowercase ) else 1_000 @torch.no_grad() def __call__( self : Optional[Any] , lowercase : int = 1 , lowercase : str = None , lowercase : np.ndarray = None , lowercase : int = 0 , lowercase : int = 0 , lowercase : int = None , lowercase : torch.Generator = None , lowercase : float = 0 , lowercase : float = 0 , lowercase : torch.Generator = None , lowercase : float = 0 , lowercase : torch.Tensor = None , lowercase : torch.Tensor = None , lowercase : Tuple=True , ): '''simple docstring''' UpperCAmelCase = steps or self.get_default_steps() self.scheduler.set_timesteps(lowercase ) UpperCAmelCase = step_generator or generator # For backwards compatibility if type(self.unet.config.sample_size ) == int: UpperCAmelCase = (self.unet.config.sample_size, self.unet.config.sample_size) if noise is None: UpperCAmelCase = randn_tensor( ( batch_size, self.unet.config.in_channels, self.unet.config.sample_size[0], self.unet.config.sample_size[1], ) , generator=lowercase , device=self.device , ) UpperCAmelCase = noise UpperCAmelCase = None if audio_file is not None or raw_audio is not None: self.mel.load_audio(lowercase , lowercase ) UpperCAmelCase = self.mel.audio_slice_to_image(lowercase ) UpperCAmelCase = np.frombuffer(input_image.tobytes() , dtype='''uint8''' ).reshape( (input_image.height, input_image.width) ) UpperCAmelCase = (input_image / 255) * 2 - 1 UpperCAmelCase = torch.tensor(input_image[np.newaxis, :, :] , dtype=torch.float ).to(self.device ) if self.vqvae is not None: UpperCAmelCase = self.vqvae.encode(torch.unsqueeze(lowercase , 0 ) ).latent_dist.sample( generator=lowercase )[0] UpperCAmelCase = self.vqvae.config.scaling_factor * input_images if start_step > 0: UpperCAmelCase = self.scheduler.add_noise(lowercase , lowercase , self.scheduler.timesteps[start_step - 1] ) UpperCAmelCase = ( self.unet.config.sample_size[1] * self.mel.get_sample_rate() / self.mel.x_res / self.mel.hop_length ) UpperCAmelCase = int(mask_start_secs * pixels_per_second ) UpperCAmelCase = int(mask_end_secs * pixels_per_second ) UpperCAmelCase = self.scheduler.add_noise(lowercase , lowercase , torch.tensor(self.scheduler.timesteps[start_step:] ) ) for step, t in enumerate(self.progress_bar(self.scheduler.timesteps[start_step:] ) ): if isinstance(self.unet , lowercase ): UpperCAmelCase = self.unet(lowercase , lowercase , lowercase )['''sample'''] else: UpperCAmelCase = self.unet(lowercase , lowercase )['''sample'''] if isinstance(self.scheduler , lowercase ): UpperCAmelCase = self.scheduler.step( model_output=lowercase , timestep=lowercase , sample=lowercase , eta=lowercase , generator=lowercase , )['''prev_sample'''] else: UpperCAmelCase = self.scheduler.step( model_output=lowercase , timestep=lowercase , sample=lowercase , generator=lowercase , )['''prev_sample'''] if mask is not None: if mask_start > 0: UpperCAmelCase = mask[:, step, :, :mask_start] if mask_end > 0: UpperCAmelCase = mask[:, step, :, -mask_end:] if self.vqvae is not None: # 0.18215 was scaling factor used in training to ensure unit variance UpperCAmelCase = 1 / self.vqvae.config.scaling_factor * images UpperCAmelCase = self.vqvae.decode(lowercase )['''sample'''] UpperCAmelCase = (images / 2 + 0.5).clamp(0 , 1 ) UpperCAmelCase = images.cpu().permute(0 , 2 , 3 , 1 ).numpy() UpperCAmelCase = (images * 255).round().astype('''uint8''' ) UpperCAmelCase = list( (Image.fromarray(_[:, :, 0] ) for _ in images) if images.shape[3] == 1 else (Image.fromarray(lowercase , mode='''RGB''' ).convert('''L''' ) for _ in images) ) UpperCAmelCase = [self.mel.image_to_audio(lowercase ) for _ in images] if not return_dict: return images, (self.mel.get_sample_rate(), audios) return BaseOutput(**AudioPipelineOutput(np.array(lowercase )[:, np.newaxis, :] ) , **ImagePipelineOutput(lowercase ) ) @torch.no_grad() def A ( self : Dict , lowercase : List[Image.Image] , lowercase : int = 50 ): '''simple docstring''' assert isinstance(self.scheduler , lowercase ) self.scheduler.set_timesteps(lowercase ) UpperCAmelCase = np.array( [np.frombuffer(image.tobytes() , dtype='''uint8''' ).reshape((1, image.height, image.width) ) for image in images] ) UpperCAmelCase = (sample / 255) * 2 - 1 UpperCAmelCase = torch.Tensor(lowercase ).to(self.device ) for t in self.progress_bar(torch.flip(self.scheduler.timesteps , (0,) ) ): UpperCAmelCase = t - self.scheduler.config.num_train_timesteps // self.scheduler.num_inference_steps UpperCAmelCase = self.scheduler.alphas_cumprod[t] UpperCAmelCase = ( self.scheduler.alphas_cumprod[prev_timestep] if prev_timestep >= 0 else self.scheduler.final_alpha_cumprod ) UpperCAmelCase = 1 - alpha_prod_t UpperCAmelCase = self.unet(lowercase , lowercase )['''sample'''] UpperCAmelCase = (1 - alpha_prod_t_prev) ** 0.5 * model_output UpperCAmelCase = (sample - pred_sample_direction) * alpha_prod_t_prev ** (-0.5) UpperCAmelCase = sample * alpha_prod_t ** 0.5 + beta_prod_t ** 0.5 * model_output return sample @staticmethod def A ( lowercase : torch.Tensor , lowercase : torch.Tensor , lowercase : float ): '''simple docstring''' UpperCAmelCase = acos(torch.dot(torch.flatten(lowercase ) , torch.flatten(lowercase ) ) / torch.norm(lowercase ) / torch.norm(lowercase ) ) return sin((1 - alpha) * theta ) * xa / sin(lowercase ) + sin(alpha * theta ) * xa / sin(lowercase )
34
1
'''simple docstring''' UpperCamelCase__ = ''' # Transformers installation ! pip install transformers datasets # To install from source instead of the last release, comment the command above and uncomment the following one. # ! pip install git+https://github.com/huggingface/transformers.git ''' UpperCamelCase__ = [{'''type''': '''code''', '''content''': INSTALL_CONTENT}] UpperCamelCase__ = { '''{processor_class}''': '''FakeProcessorClass''', '''{model_class}''': '''FakeModelClass''', '''{object_class}''': '''FakeObjectClass''', }
358
'''simple docstring''' import os import shutil import sys import tempfile import unittest from pathlib import Path import pytest import transformers from transformers import ( BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, GPT2_PRETRAINED_CONFIG_ARCHIVE_MAP, AutoTokenizer, BertConfig, BertTokenizer, BertTokenizerFast, CTRLTokenizer, GPTaTokenizer, GPTaTokenizerFast, PreTrainedTokenizerFast, RobertaTokenizer, RobertaTokenizerFast, is_tokenizers_available, ) from transformers.models.auto.configuration_auto import CONFIG_MAPPING, AutoConfig from transformers.models.auto.tokenization_auto import ( TOKENIZER_MAPPING, get_tokenizer_config, tokenizer_class_from_name, ) from transformers.models.roberta.configuration_roberta import RobertaConfig from transformers.testing_utils import ( DUMMY_DIFF_TOKENIZER_IDENTIFIER, DUMMY_UNKNOWN_IDENTIFIER, SMALL_MODEL_IDENTIFIER, RequestCounter, require_tokenizers, slow, ) sys.path.append(str(Path(__file__).parent.parent.parent.parent / '''utils''')) from test_module.custom_configuration import CustomConfig # noqa E402 from test_module.custom_tokenization import CustomTokenizer # noqa E402 if is_tokenizers_available(): from test_module.custom_tokenization_fast import CustomTokenizerFast class lowerCamelCase_ ( unittest.TestCase ): def lowercase_ ( self : int ): '''simple docstring''' UpperCAmelCase__ : Union[str, Any] = 0 @slow def lowercase_ ( self : Dict ): '''simple docstring''' for model_name in (x for x in BERT_PRETRAINED_CONFIG_ARCHIVE_MAP.keys() if "japanese" not in x): UpperCAmelCase__ : int = AutoTokenizer.from_pretrained(_A ) self.assertIsNotNone(_A ) self.assertIsInstance(_A , (BertTokenizer, BertTokenizerFast) ) self.assertGreater(len(_A ) , 0 ) for model_name in GPT2_PRETRAINED_CONFIG_ARCHIVE_MAP.keys(): UpperCAmelCase__ : Tuple = AutoTokenizer.from_pretrained(_A ) self.assertIsNotNone(_A ) self.assertIsInstance(_A , (GPTaTokenizer, GPTaTokenizerFast) ) self.assertGreater(len(_A ) , 0 ) def lowercase_ ( self : Optional[int] ): '''simple docstring''' UpperCAmelCase__ : int = AutoTokenizer.from_pretrained(_A ) self.assertIsInstance(_A , (BertTokenizer, BertTokenizerFast) ) self.assertEqual(tokenizer.vocab_size , 12 ) def lowercase_ ( self : Optional[int] ): '''simple docstring''' UpperCAmelCase__ : int = AutoTokenizer.from_pretrained(_A ) self.assertIsInstance(_A , (RobertaTokenizer, RobertaTokenizerFast) ) self.assertEqual(tokenizer.vocab_size , 20 ) def lowercase_ ( self : Any ): '''simple docstring''' UpperCAmelCase__ : Tuple = AutoConfig.from_pretrained(_A ) self.assertIsInstance(_A , _A ) # Check that tokenizer_type ≠ model_type UpperCAmelCase__ : Dict = AutoTokenizer.from_pretrained(_A , config=_A ) self.assertIsInstance(_A , (BertTokenizer, BertTokenizerFast) ) self.assertEqual(tokenizer.vocab_size , 12 ) def lowercase_ ( self : str ): '''simple docstring''' with tempfile.TemporaryDirectory() as tmp_dir: shutil.copy('''./tests/fixtures/vocab.txt''' , os.path.join(_A , '''vocab.txt''' ) ) UpperCAmelCase__ : Dict = AutoTokenizer.from_pretrained(_A , tokenizer_type='''bert''' , use_fast=_A ) self.assertIsInstance(_A , _A ) with tempfile.TemporaryDirectory() as tmp_dir: shutil.copy('''./tests/fixtures/vocab.json''' , os.path.join(_A , '''vocab.json''' ) ) shutil.copy('''./tests/fixtures/merges.txt''' , os.path.join(_A , '''merges.txt''' ) ) UpperCAmelCase__ : Optional[int] = AutoTokenizer.from_pretrained(_A , tokenizer_type='''gpt2''' , use_fast=_A ) self.assertIsInstance(_A , _A ) @require_tokenizers def lowercase_ ( self : str ): '''simple docstring''' with tempfile.TemporaryDirectory() as tmp_dir: shutil.copy('''./tests/fixtures/vocab.txt''' , os.path.join(_A , '''vocab.txt''' ) ) UpperCAmelCase__ : str = AutoTokenizer.from_pretrained(_A , tokenizer_type='''bert''' ) self.assertIsInstance(_A , _A ) with tempfile.TemporaryDirectory() as tmp_dir: shutil.copy('''./tests/fixtures/vocab.json''' , os.path.join(_A , '''vocab.json''' ) ) shutil.copy('''./tests/fixtures/merges.txt''' , os.path.join(_A , '''merges.txt''' ) ) UpperCAmelCase__ : Any = AutoTokenizer.from_pretrained(_A , tokenizer_type='''gpt2''' ) self.assertIsInstance(_A , _A ) def lowercase_ ( self : Optional[Any] ): '''simple docstring''' with pytest.raises(_A ): AutoTokenizer.from_pretrained('''./''' , tokenizer_type='''xxx''' ) @require_tokenizers def lowercase_ ( self : int ): '''simple docstring''' for tokenizer_class in [BertTokenizer, BertTokenizerFast, AutoTokenizer]: UpperCAmelCase__ : Optional[int] = tokenizer_class.from_pretrained('''wietsedv/bert-base-dutch-cased''' ) self.assertIsInstance(_A , (BertTokenizer, BertTokenizerFast) ) if isinstance(_A , _A ): self.assertEqual(tokenizer.basic_tokenizer.do_lower_case , _A ) else: self.assertEqual(tokenizer.do_lower_case , _A ) self.assertEqual(tokenizer.model_max_length , 512 ) @require_tokenizers def lowercase_ ( self : List[str] ): '''simple docstring''' for tokenizer_class in [BertTokenizer, BertTokenizerFast, AutoTokenizer]: with self.assertRaisesRegex( _A , '''julien-c/herlolip-not-exists is not a local folder and is not a valid model identifier''' , ): UpperCAmelCase__ : Dict = tokenizer_class.from_pretrained('''julien-c/herlolip-not-exists''' ) def lowercase_ ( self : Any ): '''simple docstring''' UpperCAmelCase__ : List[Any] = TOKENIZER_MAPPING.values() UpperCAmelCase__ : Any = [] for slow_tok, fast_tok in tokenizers: if slow_tok is not None: tokenizer_names.append(slow_tok.__name__ ) if fast_tok is not None: tokenizer_names.append(fast_tok.__name__ ) for tokenizer_name in tokenizer_names: # must find the right class tokenizer_class_from_name(_A ) @require_tokenizers def lowercase_ ( self : Optional[int] ): '''simple docstring''' self.assertIsInstance(AutoTokenizer.from_pretrained('''bert-base-cased''' , use_fast=_A ) , _A ) self.assertIsInstance(AutoTokenizer.from_pretrained('''bert-base-cased''' ) , _A ) @require_tokenizers def lowercase_ ( self : Optional[Any] ): '''simple docstring''' UpperCAmelCase__ : int = AutoTokenizer.from_pretrained('''distilbert-base-uncased''' , do_lower_case=_A ) UpperCAmelCase__ : Any = '''Hello, world. How are you?''' UpperCAmelCase__ : Dict = tokenizer.tokenize(_A ) self.assertEqual('''[UNK]''' , tokens[0] ) UpperCAmelCase__ : Union[str, Any] = AutoTokenizer.from_pretrained('''microsoft/mpnet-base''' , do_lower_case=_A ) UpperCAmelCase__ : Union[str, Any] = tokenizer.tokenize(_A ) self.assertEqual('''[UNK]''' , tokens[0] ) @require_tokenizers def lowercase_ ( self : str ): '''simple docstring''' UpperCAmelCase__ : List[str] = AutoTokenizer.from_pretrained('''robot-test/dummy-tokenizer-fast-with-model-config''' ) self.assertEqual(type(_A ) , _A ) self.assertEqual(tokenizer.model_max_length , 512 ) self.assertEqual(tokenizer.vocab_size , 30_000 ) self.assertEqual(tokenizer.unk_token , '''[UNK]''' ) self.assertEqual(tokenizer.padding_side , '''right''' ) self.assertEqual(tokenizer.truncation_side , '''right''' ) def lowercase_ ( self : Optional[int] ): '''simple docstring''' UpperCAmelCase__ : Optional[int] = AutoTokenizer.from_pretrained(_A ) self.assertIsInstance(_A , (BertTokenizer, BertTokenizerFast) ) with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(_A ) UpperCAmelCase__ : int = AutoTokenizer.from_pretrained(_A ) self.assertIsInstance(_A , tokenizer.__class__ ) self.assertEqual(tokenizera.vocab_size , 12 ) def lowercase_ ( self : Union[str, Any] ): '''simple docstring''' UpperCAmelCase__ : List[Any] = AutoTokenizer.from_pretrained('''ctrl''' ) # There is no fast CTRL so this always gives us a slow tokenizer. self.assertIsInstance(_A , _A ) def lowercase_ ( self : Dict ): '''simple docstring''' UpperCAmelCase__ : str = get_tokenizer_config('''bert-base-cased''' ) UpperCAmelCase__ : Optional[int] = config.pop('''_commit_hash''' , _A ) # If we ever update bert-base-cased tokenizer config, this dict here will need to be updated. self.assertEqual(_A , {'''do_lower_case''': False} ) # This model does not have a tokenizer_config so we get back an empty dict. UpperCAmelCase__ : Tuple = get_tokenizer_config(_A ) self.assertDictEqual(_A , {} ) # A tokenizer saved with `save_pretrained` always creates a tokenizer config. UpperCAmelCase__ : Optional[int] = AutoTokenizer.from_pretrained(_A ) with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(_A ) UpperCAmelCase__ : List[Any] = get_tokenizer_config(_A ) # Check the class of the tokenizer was properly saved (note that it always saves the slow class). self.assertEqual(config['''tokenizer_class'''] , '''BertTokenizer''' ) def lowercase_ ( self : Dict ): '''simple docstring''' try: AutoConfig.register('''custom''' , _A ) AutoTokenizer.register(_A , slow_tokenizer_class=_A ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(_A ): AutoTokenizer.register(_A , slow_tokenizer_class=_A ) UpperCAmelCase__ : Optional[int] = CustomTokenizer.from_pretrained(_A ) with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(_A ) UpperCAmelCase__ : List[Any] = AutoTokenizer.from_pretrained(_A ) self.assertIsInstance(_A , _A ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in TOKENIZER_MAPPING._extra_content: del TOKENIZER_MAPPING._extra_content[CustomConfig] @require_tokenizers def lowercase_ ( self : Any ): '''simple docstring''' try: AutoConfig.register('''custom''' , _A ) # Can register in two steps AutoTokenizer.register(_A , slow_tokenizer_class=_A ) self.assertEqual(TOKENIZER_MAPPING[CustomConfig] , (CustomTokenizer, None) ) AutoTokenizer.register(_A , fast_tokenizer_class=_A ) self.assertEqual(TOKENIZER_MAPPING[CustomConfig] , (CustomTokenizer, CustomTokenizerFast) ) del TOKENIZER_MAPPING._extra_content[CustomConfig] # Can register in one step AutoTokenizer.register( _A , slow_tokenizer_class=_A , fast_tokenizer_class=_A ) self.assertEqual(TOKENIZER_MAPPING[CustomConfig] , (CustomTokenizer, CustomTokenizerFast) ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(_A ): AutoTokenizer.register(_A , fast_tokenizer_class=_A ) # We pass through a bert tokenizer fast cause there is no converter slow to fast for our new toknizer # and that model does not have a tokenizer.json with tempfile.TemporaryDirectory() as tmp_dir: UpperCAmelCase__ : Any = BertTokenizerFast.from_pretrained(_A ) bert_tokenizer.save_pretrained(_A ) UpperCAmelCase__ : Optional[int] = CustomTokenizerFast.from_pretrained(_A ) with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(_A ) UpperCAmelCase__ : List[Any] = AutoTokenizer.from_pretrained(_A ) self.assertIsInstance(_A , _A ) UpperCAmelCase__ : Union[str, Any] = AutoTokenizer.from_pretrained(_A , use_fast=_A ) self.assertIsInstance(_A , _A ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in TOKENIZER_MAPPING._extra_content: del TOKENIZER_MAPPING._extra_content[CustomConfig] def lowercase_ ( self : Optional[int] ): '''simple docstring''' with self.assertRaises(_A ): UpperCAmelCase__ : Tuple = AutoTokenizer.from_pretrained('''hf-internal-testing/test_dynamic_tokenizer''' ) # If remote code is disabled, we can't load this config. with self.assertRaises(_A ): UpperCAmelCase__ : Optional[int] = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer''' , trust_remote_code=_A ) UpperCAmelCase__ : Dict = AutoTokenizer.from_pretrained('''hf-internal-testing/test_dynamic_tokenizer''' , trust_remote_code=_A ) self.assertTrue(tokenizer.special_attribute_present ) # Test tokenizer can be reloaded. with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(_A ) UpperCAmelCase__ : List[Any] = AutoTokenizer.from_pretrained(_A , trust_remote_code=_A ) self.assertTrue(reloaded_tokenizer.special_attribute_present ) if is_tokenizers_available(): self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizerFast''' ) self.assertEqual(reloaded_tokenizer.__class__.__name__ , '''NewTokenizerFast''' ) # Test we can also load the slow version UpperCAmelCase__ : Dict = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer''' , trust_remote_code=_A , use_fast=_A ) self.assertTrue(tokenizer.special_attribute_present ) self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizer''' ) # Test tokenizer can be reloaded. with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(_A ) UpperCAmelCase__ : Any = AutoTokenizer.from_pretrained(_A , trust_remote_code=_A , use_fast=_A ) self.assertEqual(reloaded_tokenizer.__class__.__name__ , '''NewTokenizer''' ) self.assertTrue(reloaded_tokenizer.special_attribute_present ) else: self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizer''' ) self.assertEqual(reloaded_tokenizer.__class__.__name__ , '''NewTokenizer''' ) @require_tokenizers def lowercase_ ( self : int ): '''simple docstring''' class lowerCamelCase_ ( __a ): lowerCAmelCase__ = False class lowerCamelCase_ ( __a ): lowerCAmelCase__ = NewTokenizer lowerCAmelCase__ = False try: AutoConfig.register('''custom''' , _A ) AutoTokenizer.register(_A , slow_tokenizer_class=_A ) AutoTokenizer.register(_A , fast_tokenizer_class=_A ) # If remote code is not set, the default is to use local UpperCAmelCase__ : Dict = AutoTokenizer.from_pretrained('''hf-internal-testing/test_dynamic_tokenizer''' ) self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizerFast''' ) self.assertFalse(tokenizer.special_attribute_present ) UpperCAmelCase__ : List[Any] = AutoTokenizer.from_pretrained('''hf-internal-testing/test_dynamic_tokenizer''' , use_fast=_A ) self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizer''' ) self.assertFalse(tokenizer.special_attribute_present ) # If remote code is disabled, we load the local one. UpperCAmelCase__ : Tuple = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer''' , trust_remote_code=_A ) self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizerFast''' ) self.assertFalse(tokenizer.special_attribute_present ) UpperCAmelCase__ : str = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer''' , trust_remote_code=_A , use_fast=_A ) self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizer''' ) self.assertFalse(tokenizer.special_attribute_present ) # If remote is enabled, we load from the Hub UpperCAmelCase__ : Dict = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer''' , trust_remote_code=_A ) self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizerFast''' ) self.assertTrue(tokenizer.special_attribute_present ) UpperCAmelCase__ : Any = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer''' , trust_remote_code=_A , use_fast=_A ) self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizer''' ) self.assertTrue(tokenizer.special_attribute_present ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in TOKENIZER_MAPPING._extra_content: del TOKENIZER_MAPPING._extra_content[CustomConfig] def lowercase_ ( self : Tuple ): '''simple docstring''' UpperCAmelCase__ : Optional[int] = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer_legacy''' , trust_remote_code=_A ) self.assertTrue(tokenizer.special_attribute_present ) if is_tokenizers_available(): self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizerFast''' ) # Test we can also load the slow version UpperCAmelCase__ : Optional[Any] = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer_legacy''' , trust_remote_code=_A , use_fast=_A ) self.assertTrue(tokenizer.special_attribute_present ) self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizer''' ) else: self.assertEqual(tokenizer.__class__.__name__ , '''NewTokenizer''' ) def lowercase_ ( self : Tuple ): '''simple docstring''' with self.assertRaisesRegex( _A , '''bert-base is not a local folder and is not a valid model identifier''' ): UpperCAmelCase__ : Tuple = AutoTokenizer.from_pretrained('''bert-base''' ) def lowercase_ ( self : Dict ): '''simple docstring''' with self.assertRaisesRegex( _A , R'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)''' ): UpperCAmelCase__ : Optional[int] = AutoTokenizer.from_pretrained(_A , revision='''aaaaaa''' ) def lowercase_ ( self : Union[str, Any] ): '''simple docstring''' UpperCAmelCase__ : Optional[int] = AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-bert''' ) with RequestCounter() as counter: UpperCAmelCase__ : Optional[int] = AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-bert''' ) self.assertEqual(counter.get_request_count , 0 ) self.assertEqual(counter.head_request_count , 1 ) self.assertEqual(counter.other_request_count , 0 )
299
0
from math import factorial, pi def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ = 30 ) -> float: if not isinstance(SCREAMING_SNAKE_CASE__ , (int, float) ): raise ValueError("""maclaurin_sin() requires either an int or float for theta""" ) if not isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) or accuracy <= 0: raise ValueError("""maclaurin_sin() requires a positive int for accuracy""" ) lowercase : List[str] = float(SCREAMING_SNAKE_CASE__ ) lowercase : Optional[int] = theta // (2 * pi) theta -= 2 * div * pi return sum( (-1) ** r * theta ** (2 * r + 1) / factorial(2 * r + 1 ) for r in range(SCREAMING_SNAKE_CASE__ ) ) def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ = 30 ) -> float: if not isinstance(SCREAMING_SNAKE_CASE__ , (int, float) ): raise ValueError("""maclaurin_cos() requires either an int or float for theta""" ) if not isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) or accuracy <= 0: raise ValueError("""maclaurin_cos() requires a positive int for accuracy""" ) lowercase : Dict = float(SCREAMING_SNAKE_CASE__ ) lowercase : Optional[int] = theta // (2 * pi) theta -= 2 * div * pi return sum((-1) ** r * theta ** (2 * r) / factorial(2 * r ) for r in range(SCREAMING_SNAKE_CASE__ ) ) if __name__ == "__main__": import doctest doctest.testmod() print(maclaurin_sin(10)) print(maclaurin_sin(-10)) print(maclaurin_sin(10, 15)) print(maclaurin_sin(-10, 15)) print(maclaurin_cos(5)) print(maclaurin_cos(-5)) print(maclaurin_cos(10, 15)) print(maclaurin_cos(-10, 15))
20
"""simple docstring""" from scipy.stats import spearmanr import datasets a = ''' 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. ''' a = ''' 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 ''' a = 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 lowercase_ ( datasets.Metric ): '''simple docstring''' def lowerCAmelCase_ ( self : Optional[int] ): 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 lowerCAmelCase_ ( self : Dict , _UpperCAmelCase : Optional[Any] , _UpperCAmelCase : int , _UpperCAmelCase : Optional[int]=False ): _A = spearmanr(_UpperCAmelCase , _UpperCAmelCase ) if return_pvalue: return {"spearmanr": results[0], "spearmanr_pvalue": results[1]} else: return {"spearmanr": results[0]}
315
0
from ...configuration_utils import PretrainedConfig from ...utils import logging _lowerCamelCase : Optional[int] = logging.get_logger(__name__) _lowerCamelCase : Union[str, Any] = { """microsoft/trocr-base-handwritten""": ( """https://huggingface.co/microsoft/trocr-base-handwritten/resolve/main/config.json""" ), # See all TrOCR models at https://huggingface.co/models?filter=trocr } class UpperCamelCase_ ( UpperCAmelCase__ ): '''simple docstring''' UpperCAmelCase__ = '''trocr''' UpperCAmelCase__ = ['''past_key_values'''] UpperCAmelCase__ = { '''num_attention_heads''': '''decoder_attention_heads''', '''hidden_size''': '''d_model''', '''num_hidden_layers''': '''decoder_layers''', } def __init__( self : Any , UpperCAmelCase__ : Tuple=50_265 , UpperCAmelCase__ : Optional[int]=1_024 , UpperCAmelCase__ : str=12 , UpperCAmelCase__ : Optional[int]=16 , UpperCAmelCase__ : Tuple=4_096 , UpperCAmelCase__ : List[str]="gelu" , UpperCAmelCase__ : Any=512 , UpperCAmelCase__ : Optional[int]=0.1 , UpperCAmelCase__ : Union[str, Any]=0.0 , UpperCAmelCase__ : int=0.0 , UpperCAmelCase__ : Optional[int]=2 , UpperCAmelCase__ : int=0.02 , UpperCAmelCase__ : Optional[int]=0.0 , UpperCAmelCase__ : Dict=True , UpperCAmelCase__ : Union[str, Any]=False , UpperCAmelCase__ : Union[str, Any]=True , UpperCAmelCase__ : List[Any]=True , UpperCAmelCase__ : List[str]=1 , UpperCAmelCase__ : List[Any]=0 , UpperCAmelCase__ : Optional[Any]=2 , **UpperCAmelCase__ : Union[str, Any] , ) ->List[str]: '''simple docstring''' A__ = vocab_size A__ = d_model A__ = decoder_layers A__ = decoder_attention_heads A__ = decoder_ffn_dim A__ = activation_function A__ = max_position_embeddings A__ = dropout A__ = attention_dropout A__ = activation_dropout A__ = init_std A__ = decoder_layerdrop A__ = use_cache A__ = scale_embedding A__ = use_learned_position_embeddings A__ = layernorm_embedding super().__init__( pad_token_id=UpperCAmelCase__ , bos_token_id=UpperCAmelCase__ , eos_token_id=UpperCAmelCase__ , decoder_start_token_id=UpperCAmelCase__ , **UpperCAmelCase__ , )
231
from __future__ import annotations from typing import Any class UpperCamelCase_ : '''simple docstring''' def __init__( self : str , UpperCAmelCase__ : int , UpperCAmelCase__ : int , UpperCAmelCase__ : float = 0) ->None: '''simple docstring''' A__ , A__ = row, column A__ = [[default_value for c in range(UpperCAmelCase__)] for r in range(UpperCAmelCase__)] def __str__( self : List[str]) ->str: '''simple docstring''' A__ = f"""Matrix consist of {self.row} rows and {self.column} columns\n""" # Make string identifier A__ = 0 for row_vector in self.array: for obj in row_vector: A__ = max(UpperCAmelCase__ , len(str(UpperCAmelCase__))) A__ = f"""%{max_element_length}s""" # Make string and return def single_line(UpperCAmelCase__ : list[float]) -> str: nonlocal string_format_identifier A__ = '''[''' line += ", ".join(string_format_identifier % (obj,) for obj in row_vector) line += "]" return line s += "\n".join(single_line(UpperCAmelCase__) for row_vector in self.array) return s def __repr__( self : Tuple) ->str: '''simple docstring''' return str(self) def SCREAMING_SNAKE_CASE ( self : List[str] , UpperCAmelCase__ : tuple[int, int]) ->bool: '''simple docstring''' if not (isinstance(UpperCAmelCase__ , (list, tuple)) and len(UpperCAmelCase__) == 2): return False elif not (0 <= loc[0] < self.row and 0 <= loc[1] < self.column): return False else: return True def __getitem__( self : List[Any] , UpperCAmelCase__ : tuple[int, int]) ->Any: '''simple docstring''' assert self.validate_indicies(UpperCAmelCase__) return self.array[loc[0]][loc[1]] def __setitem__( self : List[Any] , UpperCAmelCase__ : tuple[int, int] , UpperCAmelCase__ : float) ->None: '''simple docstring''' assert self.validate_indicies(UpperCAmelCase__) A__ = value def __add__( self : Optional[int] , UpperCAmelCase__ : Matrix) ->Matrix: '''simple docstring''' assert isinstance(UpperCAmelCase__ , UpperCAmelCase__) assert self.row == another.row and self.column == another.column # Add A__ = Matrix(self.row , self.column) for r in range(self.row): for c in range(self.column): A__ = self[r, c] + another[r, c] return result def __neg__( self : str) ->Matrix: '''simple docstring''' A__ = Matrix(self.row , self.column) for r in range(self.row): for c in range(self.column): A__ = -self[r, c] return result def __sub__( self : str , UpperCAmelCase__ : Matrix) ->Matrix: '''simple docstring''' return self + (-another) def __mul__( self : Union[str, Any] , UpperCAmelCase__ : int | float | Matrix) ->Matrix: '''simple docstring''' if isinstance(UpperCAmelCase__ , (int, float)): # Scalar multiplication A__ = Matrix(self.row , self.column) for r in range(self.row): for c in range(self.column): A__ = self[r, c] * another return result elif isinstance(UpperCAmelCase__ , UpperCAmelCase__): # Matrix multiplication assert self.column == another.row A__ = Matrix(self.row , another.column) for r in range(self.row): for c in range(another.column): for i in range(self.column): result[r, c] += self[r, i] * another[i, c] return result else: A__ = f"""Unsupported type given for another ({type(UpperCAmelCase__)})""" raise TypeError(UpperCAmelCase__) def SCREAMING_SNAKE_CASE ( self : List[str]) ->Matrix: '''simple docstring''' A__ = Matrix(self.column , self.row) for r in range(self.row): for c in range(self.column): A__ = self[r, c] return result def SCREAMING_SNAKE_CASE ( self : Tuple , UpperCAmelCase__ : Matrix , UpperCAmelCase__ : Matrix) ->Any: '''simple docstring''' assert isinstance(UpperCAmelCase__ , UpperCAmelCase__) and isinstance(UpperCAmelCase__ , UpperCAmelCase__) assert self.row == self.column == u.row == v.row # u, v should be column vector assert u.column == v.column == 1 # u, v should be column vector # Calculate A__ = v.transpose() A__ = (v_t * self * u)[0, 0] + 1 if numerator_factor == 0: return None # It's not invertable return self - ((self * u) * (v_t * self) * (1.0 / numerator_factor)) # Testing if __name__ == "__main__": def SCREAMING_SNAKE_CASE ( ) -> None: """simple docstring""" A__ = Matrix(3 , 3 , 0 ) for i in range(3 ): A__ = 1 print(f"""a^(-1) is {ainv}""" ) # u, v A__ = Matrix(3 , 1 , 0 ) A__ , A__ , A__ = 1, 2, -3 A__ = Matrix(3 , 1 , 0 ) A__ , A__ , A__ = 4, -2, 5 print(f"""u is {u}""" ) print(f"""v is {v}""" ) print(f"""uv^T is {u * v.transpose()}""" ) # Sherman Morrison print(f"""(a + uv^T)^(-1) is {ainv.sherman_morrison(lowercase_ , lowercase_ )}""" ) def SCREAMING_SNAKE_CASE ( ) -> None: """simple docstring""" import doctest doctest.testmod() testa()
231
1