code
stringlengths
81
54k
code_codestyle
int64
0
721
style_context
stringlengths
91
41.9k
style_context_codestyle
int64
0
699
label
int64
0
1
import argparse import json import requests import timm import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import AutoImageProcessor, SwinConfig, SwinForImageClassification def UpperCamelCase__ ( A__ ) -> Dict: snake_case__ : Any = SwinConfig() snake_case__ : Optional[int] = swin_name.split('_' ) snake_case__ : Dict = name_split[1] snake_case__ : Tuple = int(name_split[4] ) snake_case__ : List[Any] = int(name_split[3][-1] ) if model_size == "tiny": snake_case__ : Union[str, Any] = 96 snake_case__ : Union[str, Any] = (2, 2, 6, 2) snake_case__ : str = (3, 6, 12, 24) elif model_size == "small": snake_case__ : List[Any] = 96 snake_case__ : str = (2, 2, 18, 2) snake_case__ : Tuple = (3, 6, 12, 24) elif model_size == "base": snake_case__ : Optional[Any] = 128 snake_case__ : Tuple = (2, 2, 18, 2) snake_case__ : List[str] = (4, 8, 16, 32) else: snake_case__ : List[str] = 192 snake_case__ : Optional[int] = (2, 2, 18, 2) snake_case__ : Optional[Any] = (6, 12, 24, 48) if "in22k" in swin_name: snake_case__ : List[Any] = 2_1841 else: snake_case__ : List[Any] = 1000 snake_case__ : Tuple = 'huggingface/label-files' snake_case__ : Optional[int] = 'imagenet-1k-id2label.json' snake_case__ : List[str] = json.load(open(hf_hub_download(_snake_case , _snake_case , repo_type='dataset' ) , 'r' ) ) snake_case__ : str = {int(_snake_case ): v for k, v in idalabel.items()} snake_case__ : Union[str, Any] = idalabel snake_case__ : str = {v: k for k, v in idalabel.items()} snake_case__ : Dict = img_size snake_case__ : Tuple = num_classes snake_case__ : List[str] = embed_dim snake_case__ : int = depths snake_case__ : Dict = num_heads snake_case__ : Tuple = window_size return config def UpperCamelCase__ ( A__ ) -> Union[str, Any]: if "patch_embed.proj" in name: snake_case__ : str = name.replace('patch_embed.proj' , 'embeddings.patch_embeddings.projection' ) if "patch_embed.norm" in name: snake_case__ : List[Any] = name.replace('patch_embed.norm' , 'embeddings.norm' ) if "layers" in name: snake_case__ : Any = 'encoder.' + name if "attn.proj" in name: snake_case__ : Optional[Any] = name.replace('attn.proj' , 'attention.output.dense' ) if "attn" in name: snake_case__ : str = name.replace('attn' , 'attention.self' ) if "norm1" in name: snake_case__ : Optional[int] = name.replace('norm1' , 'layernorm_before' ) if "norm2" in name: snake_case__ : Any = name.replace('norm2' , 'layernorm_after' ) if "mlp.fc1" in name: snake_case__ : Any = name.replace('mlp.fc1' , 'intermediate.dense' ) if "mlp.fc2" in name: snake_case__ : int = name.replace('mlp.fc2' , 'output.dense' ) if name == "norm.weight": snake_case__ : Tuple = 'layernorm.weight' if name == "norm.bias": snake_case__ : str = 'layernorm.bias' if "head" in name: snake_case__ : Union[str, Any] = name.replace('head' , 'classifier' ) else: snake_case__ : Any = 'swin.' + name return name def UpperCamelCase__ ( A__ , A__ ) -> Any: for key in orig_state_dict.copy().keys(): snake_case__ : str = orig_state_dict.pop(_snake_case ) if "mask" in key: continue elif "qkv" in key: snake_case__ : int = key.split('.' ) snake_case__ : Dict = int(key_split[1] ) snake_case__ : Union[str, Any] = int(key_split[3] ) snake_case__ : Optional[int] = model.swin.encoder.layers[layer_num].blocks[block_num].attention.self.all_head_size if "weight" in key: snake_case__ : int = val[:dim, :] snake_case__ : List[str] = val[ dim : dim * 2, : ] snake_case__ : Optional[Any] = val[-dim:, :] else: snake_case__ : Union[str, Any] = val[ :dim ] snake_case__ : Optional[Any] = val[ dim : dim * 2 ] snake_case__ : Optional[Any] = val[ -dim: ] else: snake_case__ : Tuple = val return orig_state_dict def UpperCamelCase__ ( A__ , A__ ) -> List[Any]: snake_case__ : Optional[int] = timm.create_model(_snake_case , pretrained=_snake_case ) timm_model.eval() snake_case__ : Optional[int] = get_swin_config(_snake_case ) snake_case__ : Dict = SwinForImageClassification(_snake_case ) model.eval() snake_case__ : Optional[Any] = convert_state_dict(timm_model.state_dict() , _snake_case ) model.load_state_dict(_snake_case ) snake_case__ : Union[str, Any] = 'http://images.cocodataset.org/val2017/000000039769.jpg' snake_case__ : Optional[Any] = AutoImageProcessor.from_pretrained('microsoft/{}'.format(swin_name.replace('_' , '-' ) ) ) snake_case__ : Union[str, Any] = Image.open(requests.get(_snake_case , stream=_snake_case ).raw ) snake_case__ : str = image_processor(images=_snake_case , return_tensors='pt' ) snake_case__ : Dict = timm_model(inputs['pixel_values'] ) snake_case__ : int = model(**_snake_case ).logits assert torch.allclose(_snake_case , _snake_case , atol=1e-3 ) print(F"""Saving model {swin_name} to {pytorch_dump_folder_path}""" ) model.save_pretrained(_snake_case ) print(F"""Saving image processor to {pytorch_dump_folder_path}""" ) image_processor.save_pretrained(_snake_case ) if __name__ == "__main__": lowerCAmelCase__ : Dict = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--swin_name''', default='''swin_tiny_patch4_window7_224''', type=str, help='''Name of the Swin timm model you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) lowerCAmelCase__ : Any = parser.parse_args() convert_swin_checkpoint(args.swin_name, args.pytorch_dump_folder_path)
711
from .data_collator import ( DataCollatorForLanguageModeling, DataCollatorForPermutationLanguageModeling, DataCollatorForSeqaSeq, DataCollatorForSOP, DataCollatorForTokenClassification, DataCollatorForWholeWordMask, DataCollatorWithPadding, DefaultDataCollator, default_data_collator, ) from .metrics import glue_compute_metrics, xnli_compute_metrics from .processors import ( DataProcessor, InputExample, InputFeatures, SingleSentenceClassificationProcessor, SquadExample, SquadFeatures, SquadVaProcessor, SquadVaProcessor, glue_convert_examples_to_features, glue_output_modes, glue_processors, glue_tasks_num_labels, squad_convert_examples_to_features, xnli_output_modes, xnli_processors, xnli_tasks_num_labels, )
699
0
import importlib.metadata import warnings from copy import deepcopy from packaging import version from ..utils import logging from .import_utils import is_accelerate_available, is_bitsandbytes_available if is_bitsandbytes_available(): import bitsandbytes as bnb import torch import torch.nn as nn from ..pytorch_utils import ConvaD if is_accelerate_available(): from accelerate import init_empty_weights from accelerate.utils import find_tied_parameters lowerCAmelCase__ : int = logging.get_logger(__name__) def UpperCamelCase__ ( A__ , A__ , A__ , A__=None , A__=None ) -> List[Any]: # Recurse if needed if "." in tensor_name: snake_case__ : Dict = tensor_name.split('.' ) for split in splits[:-1]: snake_case__ : int = getattr(_lowercase , _lowercase ) if new_module is None: raise ValueError(F"""{module} has no attribute {split}.""" ) snake_case__ : List[str] = new_module snake_case__ : Dict = splits[-1] if tensor_name not in module._parameters and tensor_name not in module._buffers: raise ValueError(F"""{module} does not have a parameter or a buffer named {tensor_name}.""" ) snake_case__ : Union[str, Any] = tensor_name in module._buffers snake_case__ : Optional[Any] = getattr(_lowercase , _lowercase ) if old_value.device == torch.device('meta' ) and device not in ["meta", torch.device('meta' )] and value is None: raise ValueError(F"""{tensor_name} is on the meta device, we need a `value` to put in on {device}.""" ) snake_case__ : Optional[int] = False snake_case__ : Dict = False if is_buffer or not is_bitsandbytes_available(): snake_case__ : List[str] = False snake_case__ : List[str] = False else: snake_case__ : Dict = hasattr(bnb.nn , 'Params4bit' ) and isinstance(module._parameters[tensor_name] , bnb.nn.Paramsabit ) snake_case__ : Any = isinstance(module._parameters[tensor_name] , bnb.nn.IntaParams ) if is_abit or is_abit: snake_case__ : List[str] = module._parameters[tensor_name] if param.device.type != "cuda": if value is None: snake_case__ : Any = old_value.to(_lowercase ) elif isinstance(_lowercase , torch.Tensor ): snake_case__ : List[Any] = value.to('cpu' ) if value.dtype == torch.inta: snake_case__ : List[Any] = version.parse(importlib.metadata.version('bitsandbytes' ) ) > version.parse( '0.37.2' ) if not is_abit_serializable: raise ValueError( 'Detected int8 weights but the version of bitsandbytes is not compatible with int8 serialization. ' 'Make sure to download the latest `bitsandbytes` version. `pip install --upgrade bitsandbytes`.' ) else: snake_case__ : Dict = torch.tensor(_lowercase , device='cpu' ) # Support models using `Conv1D` in place of `nn.Linear` (e.g. gpt2) by transposing the weight matrix prior to quantization. # Since weights are saved in the correct "orientation", we skip transposing when loading. if issubclass(module.source_cls , _lowercase ) and fpaa_statistics is None: snake_case__ : Optional[int] = new_value.T snake_case__ : Dict = old_value.__dict__ if is_abit: snake_case__ : List[Any] = bnb.nn.IntaParams(_lowercase , requires_grad=_lowercase , **_lowercase ).to(_lowercase ) elif is_abit: snake_case__ : str = bnb.nn.Paramsabit(_lowercase , requires_grad=_lowercase , **_lowercase ).to(_lowercase ) snake_case__ : Tuple = new_value if fpaa_statistics is not None: setattr(module.weight , 'SCB' , fpaa_statistics.to(_lowercase ) ) else: if value is None: snake_case__ : Union[str, Any] = old_value.to(_lowercase ) elif isinstance(_lowercase , torch.Tensor ): snake_case__ : List[Any] = value.to(_lowercase ) else: snake_case__ : List[Any] = torch.tensor(_lowercase , device=_lowercase ) if is_buffer: snake_case__ : List[Any] = new_value else: snake_case__ : Dict = nn.Parameter(_lowercase , requires_grad=old_value.requires_grad ) snake_case__ : Dict = new_value def UpperCamelCase__ ( A__ , A__=None , A__=None , A__=None , A__=False ) -> Tuple: for name, module in model.named_children(): if current_key_name is None: snake_case__ : List[str] = [] current_key_name.append(_lowercase ) if (isinstance(_lowercase , nn.Linear ) or isinstance(_lowercase , _lowercase )) and name not in modules_to_not_convert: # Check if the current key is not in the `modules_to_not_convert` if not any(key in '.'.join(_lowercase ) for key in modules_to_not_convert ): with init_empty_weights(): if isinstance(_lowercase , _lowercase ): snake_case__ : Optional[Any] = module.weight.shape else: snake_case__ : Tuple = module.in_features snake_case__ : Optional[int] = module.out_features if quantization_config.quantization_method() == "llm_int8": snake_case__ : str = bnb.nn.LinearabitLt( _lowercase , _lowercase , module.bias is not None , has_fpaa_weights=quantization_config.llm_inta_has_fpaa_weight , threshold=quantization_config.llm_inta_threshold , ) snake_case__ : Optional[int] = True else: if ( quantization_config.llm_inta_skip_modules is not None and name in quantization_config.llm_inta_skip_modules ): pass else: snake_case__ : List[Any] = bnb.nn.Linearabit( _lowercase , _lowercase , module.bias is not None , quantization_config.bnb_abit_compute_dtype , compress_statistics=quantization_config.bnb_abit_use_double_quant , quant_type=quantization_config.bnb_abit_quant_type , ) snake_case__ : int = True # Store the module class in case we need to transpose the weight later snake_case__ : Union[str, Any] = type(_lowercase ) # Force requires grad to False to avoid unexpected errors model._modules[name].requires_grad_(_lowercase ) if len(list(module.children() ) ) > 0: snake_case__ : Union[str, Any] = _replace_with_bnb_linear( _lowercase , _lowercase , _lowercase , _lowercase , has_been_replaced=_lowercase , ) # Remove the last key for recursion current_key_name.pop(-1 ) return model, has_been_replaced def UpperCamelCase__ ( A__ , A__=None , A__=None , A__=None ) -> Union[str, Any]: snake_case__ : Union[str, Any] = ['''lm_head'''] if modules_to_not_convert is None else modules_to_not_convert snake_case__ : Union[str, Any] = _replace_with_bnb_linear( _lowercase , _lowercase , _lowercase , _lowercase ) if not has_been_replaced: logger.warning( 'You are loading your model in 8bit or 4bit but no linear modules were found in your model.' ' Please double check your model architecture, or submit an issue on github if you think this is' ' a bug.' ) return model def UpperCamelCase__ ( *A__ , **A__ ) -> List[Any]: warnings.warn( '`replace_8bit_linear` will be deprecated in a future version, please use `replace_with_bnb_linear` instead' , _lowercase , ) return replace_with_bnb_linear(*_lowercase , **_lowercase ) def UpperCamelCase__ ( *A__ , **A__ ) -> Optional[Any]: warnings.warn( '`set_module_8bit_tensor_to_device` will be deprecated in a future version, please use `set_module_quantized_tensor_to_device` instead' , _lowercase , ) return set_module_quantized_tensor_to_device(*_lowercase , **_lowercase ) def UpperCamelCase__ ( A__ ) -> Any: snake_case__ : Tuple = deepcopy(_lowercase ) # this has 0 cost since it is done inside `init_empty_weights` context manager` tied_model.tie_weights() snake_case__ : str = find_tied_parameters(_lowercase ) # For compatibility with Accelerate < 0.18 if isinstance(_lowercase , _lowercase ): snake_case__ : Union[str, Any] = sum(list(tied_params.values() ) , [] ) + list(tied_params.keys() ) else: snake_case__ : Optional[int] = sum(_lowercase , [] ) snake_case__ : Optional[int] = len(_lowercase ) > 0 # Check if it is a base model snake_case__ : List[Any] = not hasattr(_lowercase , model.base_model_prefix ) # Ignore this for base models (BertModel, GPT2Model, etc.) if (not has_tied_params) and is_base_model: return [] # otherwise they have an attached head snake_case__ : List[Any] = list(model.named_children() ) snake_case__ : Tuple = [list_modules[-1][0]] # add last module together with tied weights snake_case__ : Union[str, Any] = set(_lowercase ) - set(_lowercase ) snake_case__ : List[Any] = list(set(_lowercase ) ) + list(_lowercase ) # remove ".weight" from the keys snake_case__ : int = ['''.weight''', '''.bias'''] snake_case__ : Union[str, Any] = [] for name in list_untouched: for name_to_remove in names_to_remove: if name_to_remove in name: snake_case__ : int = name.replace(_lowercase , '' ) filtered_module_names.append(_lowercase ) return filtered_module_names
712
from dataclasses import dataclass, field from typing import Optional from transformers import AutoConfig, AutoImageProcessor, AutoTokenizer, FlaxVisionEncoderDecoderModel, HfArgumentParser @dataclass class __snake_case : __lowerCamelCase = field( metadata={"""help""": """The output directory where the model will be written."""} ,) __lowerCamelCase = field( metadata={ """help""": ( """The encoder model checkpoint for weights initialization.""" """Don't set if you want to train an encoder model from scratch.""" ) } ,) __lowerCamelCase = field( metadata={ """help""": ( """The decoder model checkpoint for weights initialization.""" """Don't set if you want to train a decoder model from scratch.""" ) } ,) __lowerCamelCase = field( default=_lowerCamelCase ,metadata={"""help""": """Pretrained encoder config name or path if not the same as encoder_model_name"""} ) __lowerCamelCase = field( default=_lowerCamelCase ,metadata={"""help""": """Pretrained decoder config name or path if not the same as decoder_model_name"""} ) def UpperCamelCase__ ( ) -> Union[str, Any]: snake_case__ : str = HfArgumentParser((ModelArguments,) ) ((snake_case__) , ) : Dict = parser.parse_args_into_dataclasses() # Load pretrained model and tokenizer # Use explicit specified encoder config if model_args.encoder_config_name: snake_case__ : List[str] = AutoConfig.from_pretrained(model_args.encoder_config_name ) # Use pretrained encoder model's config else: snake_case__ : Optional[int] = AutoConfig.from_pretrained(model_args.encoder_model_name_or_path ) # Use explicit specified decoder config if model_args.decoder_config_name: snake_case__ : Optional[Any] = AutoConfig.from_pretrained(model_args.decoder_config_name ) # Use pretrained decoder model's config else: snake_case__ : List[str] = AutoConfig.from_pretrained(model_args.decoder_model_name_or_path ) # necessary for `from_encoder_decoder_pretrained` when `decoder_config` is passed snake_case__ : Any = True snake_case__ : Dict = True snake_case__ : Tuple = FlaxVisionEncoderDecoderModel.from_encoder_decoder_pretrained( encoder_pretrained_model_name_or_path=model_args.encoder_model_name_or_path , decoder_pretrained_model_name_or_path=model_args.decoder_model_name_or_path , encoder_config=A__ , decoder_config=A__ , ) # GPT2 only has bos/eos tokens but not decoder_start/pad tokens snake_case__ : Optional[Any] = decoder_config.decoder_start_token_id snake_case__ : Tuple = decoder_config.pad_token_id if decoder_start_token_id is None: snake_case__ : Optional[Any] = decoder_config.bos_token_id if pad_token_id is None: snake_case__ : int = decoder_config.eos_token_id # This is necessary to make Flax's generate() work snake_case__ : Union[str, Any] = decoder_config.eos_token_id snake_case__ : Optional[int] = decoder_start_token_id snake_case__ : int = pad_token_id snake_case__ : Tuple = AutoImageProcessor.from_pretrained(model_args.encoder_model_name_or_path ) snake_case__ : int = AutoTokenizer.from_pretrained(model_args.decoder_model_name_or_path ) snake_case__ : Tuple = tokenizer.convert_ids_to_tokens(model.config.pad_token_id ) model.save_pretrained(model_args.output_dir ) image_processor.save_pretrained(model_args.output_dir ) tokenizer.save_pretrained(model_args.output_dir ) if __name__ == "__main__": main()
699
0
class __snake_case : def __init__( self ) -> Tuple: '''simple docstring''' snake_case__ : Optional[int] = '' snake_case__ : List[Any] = '' snake_case__ : List[str] = [] def __a ( self , __UpperCamelCase , __UpperCamelCase ) -> int: '''simple docstring''' if m == -1: return n + 1 elif n == -1: return m + 1 elif self.dp[m][n] > -1: return self.dp[m][n] else: if self.worda[m] == self.worda[n]: snake_case__ : Any = self.__min_dist_top_down_dp(m - 1 , n - 1 ) else: snake_case__ : Optional[int] = self.__min_dist_top_down_dp(_SCREAMING_SNAKE_CASE , n - 1 ) snake_case__ : int = self.__min_dist_top_down_dp(m - 1 , _SCREAMING_SNAKE_CASE ) snake_case__ : List[str] = self.__min_dist_top_down_dp(m - 1 , n - 1 ) snake_case__ : Optional[Any] = 1 + min(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) return self.dp[m][n] def __a ( self , __UpperCamelCase , __UpperCamelCase ) -> int: '''simple docstring''' snake_case__ : Optional[int] = worda snake_case__ : Optional[int] = worda snake_case__ : Optional[int] = [[-1 for _ in range(len(_SCREAMING_SNAKE_CASE ) )] for _ in range(len(_SCREAMING_SNAKE_CASE ) )] return self.__min_dist_top_down_dp(len(_SCREAMING_SNAKE_CASE ) - 1 , len(_SCREAMING_SNAKE_CASE ) - 1 ) def __a ( self , __UpperCamelCase , __UpperCamelCase ) -> int: '''simple docstring''' snake_case__ : Optional[Any] = worda snake_case__ : Dict = worda snake_case__ : Optional[Any] = len(_SCREAMING_SNAKE_CASE ) snake_case__ : Union[str, Any] = len(_SCREAMING_SNAKE_CASE ) snake_case__ : Dict = [[0 for _ in range(n + 1 )] for _ in range(m + 1 )] for i in range(m + 1 ): for j in range(n + 1 ): if i == 0: # first string is empty snake_case__ : Tuple = j elif j == 0: # second string is empty snake_case__ : Tuple = i elif worda[i - 1] == worda[j - 1]: # last characters are equal snake_case__ : Any = self.dp[i - 1][j - 1] else: snake_case__ : List[str] = self.dp[i][j - 1] snake_case__ : Dict = self.dp[i - 1][j] snake_case__ : List[str] = self.dp[i - 1][j - 1] snake_case__ : Optional[Any] = 1 + min(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) return self.dp[m][n] if __name__ == "__main__": lowerCAmelCase__ : List[Any] = EditDistance() print('''****************** Testing Edit Distance DP Algorithm ******************''') print() lowerCAmelCase__ : Union[str, Any] = input('''Enter the first string: ''').strip() lowerCAmelCase__ : Union[str, Any] = input('''Enter the second string: ''').strip() print() print(F'''The minimum edit distance is: {solver.min_dist_top_down(Sa, Sa)}''') print(F'''The minimum edit distance is: {solver.min_dist_bottom_up(Sa, Sa)}''') print() print('''*************** End of Testing Edit Distance DP Algorithm ***************''')
713
import logging import os from dataclasses import dataclass, field from typing import Dict, Optional import datasets import numpy as np import tensorflow as tf from transformers import ( AutoConfig, AutoTokenizer, EvalPrediction, HfArgumentParser, PreTrainedTokenizer, TFAutoModelForSequenceClassification, TFTrainer, TFTrainingArguments, ) from transformers.utils import logging as hf_logging hf_logging.set_verbosity_info() hf_logging.enable_default_handler() hf_logging.enable_explicit_format() def UpperCamelCase__ ( A__ , A__ , A__ , A__ , A__ , A__ = None , ) -> Optional[int]: snake_case__ : List[str] = {} if train_file is not None: snake_case__ : Tuple = [train_file] if eval_file is not None: snake_case__ : Dict = [eval_file] if test_file is not None: snake_case__ : str = [test_file] snake_case__ : Optional[Any] = datasets.load_dataset('csv' , data_files=A__ ) snake_case__ : Any = list(ds[list(files.keys() )[0]].features.keys() ) snake_case__ : Optional[Any] = features_name.pop(A__ ) snake_case__ : Optional[Any] = list(set(ds[list(files.keys() )[0]][label_name] ) ) snake_case__ : str = {label: i for i, label in enumerate(A__ )} snake_case__ : int = tokenizer.model_input_names snake_case__ : int = {} if len(A__ ) == 1: for k in files.keys(): snake_case__ : str = ds[k].map( lambda A__ : tokenizer.batch_encode_plus( example[features_name[0]] , truncation=A__ , max_length=A__ , padding='max_length' ) , batched=A__ , ) elif len(A__ ) == 2: for k in files.keys(): snake_case__ : Optional[int] = ds[k].map( lambda A__ : tokenizer.batch_encode_plus( (example[features_name[0]], example[features_name[1]]) , truncation=A__ , max_length=A__ , padding='max_length' , ) , batched=A__ , ) def gen_train(): for ex in transformed_ds[datasets.Split.TRAIN]: snake_case__ : int = {k: v for k, v in ex.items() if k in input_names} snake_case__ : Any = labelaid[ex[label_name]] yield (d, label) def gen_val(): for ex in transformed_ds[datasets.Split.VALIDATION]: snake_case__ : int = {k: v for k, v in ex.items() if k in input_names} snake_case__ : Union[str, Any] = labelaid[ex[label_name]] yield (d, label) def gen_test(): for ex in transformed_ds[datasets.Split.TEST]: snake_case__ : Dict = {k: v for k, v in ex.items() if k in input_names} snake_case__ : List[str] = labelaid[ex[label_name]] yield (d, label) snake_case__ : Any = ( tf.data.Dataset.from_generator( A__ , ({k: tf.intaa for k in input_names}, tf.intaa) , ({k: tf.TensorShape([None] ) for k in input_names}, tf.TensorShape([] )) , ) if datasets.Split.TRAIN in transformed_ds else None ) if train_ds is not None: snake_case__ : str = train_ds.apply(tf.data.experimental.assert_cardinality(len(ds[datasets.Split.TRAIN] ) ) ) snake_case__ : Optional[int] = ( tf.data.Dataset.from_generator( A__ , ({k: tf.intaa for k in input_names}, tf.intaa) , ({k: tf.TensorShape([None] ) for k in input_names}, tf.TensorShape([] )) , ) if datasets.Split.VALIDATION in transformed_ds else None ) if val_ds is not None: snake_case__ : Optional[int] = val_ds.apply(tf.data.experimental.assert_cardinality(len(ds[datasets.Split.VALIDATION] ) ) ) snake_case__ : List[str] = ( tf.data.Dataset.from_generator( A__ , ({k: tf.intaa for k in input_names}, tf.intaa) , ({k: tf.TensorShape([None] ) for k in input_names}, tf.TensorShape([] )) , ) if datasets.Split.TEST in transformed_ds else None ) if test_ds is not None: snake_case__ : str = test_ds.apply(tf.data.experimental.assert_cardinality(len(ds[datasets.Split.TEST] ) ) ) return train_ds, val_ds, test_ds, labelaid lowerCAmelCase__ : List[str] = logging.getLogger(__name__) @dataclass class __snake_case : __lowerCamelCase = field(metadata={"""help""": """Which column contains the label"""} ) __lowerCamelCase = field(default=_lowerCamelCase ,metadata={"""help""": """The path of the training file"""} ) __lowerCamelCase = field(default=_lowerCamelCase ,metadata={"""help""": """The path of the development file"""} ) __lowerCamelCase = field(default=_lowerCamelCase ,metadata={"""help""": """The path of the test file"""} ) __lowerCamelCase = field( default=128 ,metadata={ """help""": ( """The maximum total input sequence length after tokenization. Sequences longer """ """than this will be truncated, sequences shorter will be padded.""" ) } ,) __lowerCamelCase = field( default=_lowerCamelCase ,metadata={"""help""": """Overwrite the cached training and evaluation sets"""} ) @dataclass class __snake_case : __lowerCamelCase = field( metadata={"""help""": """Path to pretrained model or model identifier from huggingface.co/models"""} ) __lowerCamelCase = field( default=_lowerCamelCase ,metadata={"""help""": """Pretrained config name or path if not the same as model_name"""} ) __lowerCamelCase = field( default=_lowerCamelCase ,metadata={"""help""": """Pretrained tokenizer name or path if not the same as model_name"""} ) __lowerCamelCase = field(default=_lowerCamelCase ,metadata={"""help""": """Set this flag to use fast tokenization."""} ) # If you want to tweak more attributes on your tokenizer, you should do it in a distinct script, # or just modify its tokenizer_config.json. __lowerCamelCase = field( default=_lowerCamelCase ,metadata={"""help""": """Where do you want to store the pretrained models downloaded from huggingface.co"""} ,) def UpperCamelCase__ ( ) -> Union[str, Any]: # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. snake_case__ : int = HfArgumentParser((ModelArguments, DataTrainingArguments, TFTrainingArguments) ) snake_case__ , snake_case__ , snake_case__ : Dict = parser.parse_args_into_dataclasses() if ( os.path.exists(training_args.output_dir ) and os.listdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir ): raise ValueError( F"""Output directory ({training_args.output_dir}) already exists and is not empty. Use""" ' --overwrite_output_dir to overcome.' ) # Setup logging logging.basicConfig( format='%(asctime)s - %(levelname)s - %(name)s - %(message)s' , datefmt='%m/%d/%Y %H:%M:%S' , level=logging.INFO , ) logger.info( F"""n_replicas: {training_args.n_replicas}, distributed training: {bool(training_args.n_replicas > 1 )}, """ F"""16-bits training: {training_args.fpaa}""" ) logger.info(F"""Training/evaluation parameters {training_args}""" ) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. snake_case__ : Dict = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , ) snake_case__ , snake_case__ , snake_case__ , snake_case__ : Dict = get_tfds( train_file=data_args.train_file , eval_file=data_args.dev_file , test_file=data_args.test_file , tokenizer=A__ , label_column_id=data_args.label_column_id , max_seq_length=data_args.max_seq_length , ) snake_case__ : Dict = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , num_labels=len(A__ ) , labelaid=A__ , idalabel={id: label for label, id in labelaid.items()} , finetuning_task='text-classification' , cache_dir=model_args.cache_dir , ) with training_args.strategy.scope(): snake_case__ : Optional[Any] = TFAutoModelForSequenceClassification.from_pretrained( model_args.model_name_or_path , from_pt=bool('.bin' in model_args.model_name_or_path ) , config=A__ , cache_dir=model_args.cache_dir , ) def compute_metrics(A__ ) -> Dict: snake_case__ : Optional[Any] = np.argmax(p.predictions , axis=1 ) return {"acc": (preds == p.label_ids).mean()} # Initialize our Trainer snake_case__ : Any = TFTrainer( model=A__ , args=A__ , train_dataset=A__ , eval_dataset=A__ , compute_metrics=A__ , ) # Training if training_args.do_train: trainer.train() trainer.save_model() tokenizer.save_pretrained(training_args.output_dir ) # Evaluation snake_case__ : Dict = {} if training_args.do_eval: logger.info('*** Evaluate ***' ) snake_case__ : Tuple = trainer.evaluate() snake_case__ : Any = os.path.join(training_args.output_dir , 'eval_results.txt' ) with open(A__ , 'w' ) as writer: logger.info('***** Eval results *****' ) for key, value in result.items(): logger.info(F""" {key} = {value}""" ) writer.write(F"""{key} = {value}\n""" ) results.update(A__ ) return results if __name__ == "__main__": main()
699
0
def UpperCamelCase__ ( A__ = 100 ) -> Any: snake_case__ : str = set() snake_case__ : int = 0 snake_case__ : Optional[Any] = n + 1 # maximum limit for a in range(2 , A__ ): for b in range(2 , A__ ): snake_case__ : List[Any] = a**b # calculates the current power collect_powers.add(A__ ) # adds the result to the set return len(A__ ) if __name__ == "__main__": print('''Number of terms ''', solution(int(str(input()).strip())))
714
from typing import List import datasets from datasets.tasks import AudioClassification from ..folder_based_builder import folder_based_builder lowerCAmelCase__ : List[Any] = datasets.utils.logging.get_logger(__name__) class __snake_case ( folder_based_builder.FolderBasedBuilderConfig ): __lowerCamelCase = None __lowerCamelCase = None class __snake_case ( folder_based_builder.FolderBasedBuilder ): __lowerCamelCase = datasets.Audio() __lowerCamelCase = """audio""" __lowerCamelCase = AudioFolderConfig __lowerCamelCase = 42 # definition at the bottom of the script __lowerCamelCase = AudioClassification(audio_column="""audio""" ,label_column="""label""" ) lowerCAmelCase__ : Tuple = [ '''.aiff''', '''.au''', '''.avr''', '''.caf''', '''.flac''', '''.htk''', '''.svx''', '''.mat4''', '''.mat5''', '''.mpc2k''', '''.ogg''', '''.paf''', '''.pvf''', '''.raw''', '''.rf64''', '''.sd2''', '''.sds''', '''.ircam''', '''.voc''', '''.w64''', '''.wav''', '''.nist''', '''.wavex''', '''.wve''', '''.xi''', '''.mp3''', '''.opus''', ] lowerCAmelCase__ : List[Any] = AUDIO_EXTENSIONS
699
0
import argparse import json import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ConvNextConfig, SegformerImageProcessor, UperNetConfig, UperNetForSemanticSegmentation def UpperCamelCase__ ( A__ ) -> Optional[int]: snake_case__ : List[Any] = 384 if "tiny" in model_name: snake_case__ : Tuple = [3, 3, 9, 3] snake_case__ : Dict = [96, 192, 384, 768] if "small" in model_name: snake_case__ : Optional[int] = [3, 3, 27, 3] snake_case__ : Any = [96, 192, 384, 768] if "base" in model_name: snake_case__ : Union[str, Any] = [3, 3, 27, 3] snake_case__ : Dict = [128, 256, 512, 1024] snake_case__ : str = 512 if "large" in model_name: snake_case__ : List[Any] = [3, 3, 27, 3] snake_case__ : Tuple = [192, 384, 768, 1536] snake_case__ : Any = 768 if "xlarge" in model_name: snake_case__ : Any = [3, 3, 27, 3] snake_case__ : List[str] = [256, 512, 1024, 2048] snake_case__ : str = 1024 # set label information snake_case__ : Union[str, Any] = 150 snake_case__ : Optional[int] = 'huggingface/label-files' snake_case__ : Optional[Any] = 'ade20k-id2label.json' snake_case__ : List[Any] = json.load(open(hf_hub_download(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , repo_type='dataset' ) , 'r' ) ) snake_case__ : str = {int(SCREAMING_SNAKE_CASE_ ): v for k, v in idalabel.items()} snake_case__ : Any = {v: k for k, v in idalabel.items()} snake_case__ : Any = ConvNextConfig( depths=SCREAMING_SNAKE_CASE_ , hidden_sizes=SCREAMING_SNAKE_CASE_ , out_features=['stage1', 'stage2', 'stage3', 'stage4'] ) snake_case__ : List[str] = UperNetConfig( backbone_config=SCREAMING_SNAKE_CASE_ , auxiliary_in_channels=SCREAMING_SNAKE_CASE_ , num_labels=SCREAMING_SNAKE_CASE_ , idalabel=SCREAMING_SNAKE_CASE_ , labelaid=SCREAMING_SNAKE_CASE_ , ) return config def UpperCamelCase__ ( A__ ) -> List[str]: snake_case__ : int = [] # fmt: off # stem rename_keys.append(('backbone.downsample_layers.0.0.weight', 'backbone.embeddings.patch_embeddings.weight') ) rename_keys.append(('backbone.downsample_layers.0.0.bias', 'backbone.embeddings.patch_embeddings.bias') ) rename_keys.append(('backbone.downsample_layers.0.1.weight', 'backbone.embeddings.layernorm.weight') ) rename_keys.append(('backbone.downsample_layers.0.1.bias', 'backbone.embeddings.layernorm.bias') ) # stages for i in range(len(config.backbone_config.depths ) ): for j in range(config.backbone_config.depths[i] ): rename_keys.append((F"""backbone.stages.{i}.{j}.gamma""", F"""backbone.encoder.stages.{i}.layers.{j}.layer_scale_parameter""") ) rename_keys.append((F"""backbone.stages.{i}.{j}.depthwise_conv.weight""", F"""backbone.encoder.stages.{i}.layers.{j}.dwconv.weight""") ) rename_keys.append((F"""backbone.stages.{i}.{j}.depthwise_conv.bias""", F"""backbone.encoder.stages.{i}.layers.{j}.dwconv.bias""") ) rename_keys.append((F"""backbone.stages.{i}.{j}.norm.weight""", F"""backbone.encoder.stages.{i}.layers.{j}.layernorm.weight""") ) rename_keys.append((F"""backbone.stages.{i}.{j}.norm.bias""", F"""backbone.encoder.stages.{i}.layers.{j}.layernorm.bias""") ) rename_keys.append((F"""backbone.stages.{i}.{j}.pointwise_conv1.weight""", F"""backbone.encoder.stages.{i}.layers.{j}.pwconv1.weight""") ) rename_keys.append((F"""backbone.stages.{i}.{j}.pointwise_conv1.bias""", F"""backbone.encoder.stages.{i}.layers.{j}.pwconv1.bias""") ) rename_keys.append((F"""backbone.stages.{i}.{j}.pointwise_conv2.weight""", F"""backbone.encoder.stages.{i}.layers.{j}.pwconv2.weight""") ) rename_keys.append((F"""backbone.stages.{i}.{j}.pointwise_conv2.bias""", F"""backbone.encoder.stages.{i}.layers.{j}.pwconv2.bias""") ) if i > 0: rename_keys.append((F"""backbone.downsample_layers.{i}.0.weight""", F"""backbone.encoder.stages.{i}.downsampling_layer.0.weight""") ) rename_keys.append((F"""backbone.downsample_layers.{i}.0.bias""", F"""backbone.encoder.stages.{i}.downsampling_layer.0.bias""") ) rename_keys.append((F"""backbone.downsample_layers.{i}.1.weight""", F"""backbone.encoder.stages.{i}.downsampling_layer.1.weight""") ) rename_keys.append((F"""backbone.downsample_layers.{i}.1.bias""", F"""backbone.encoder.stages.{i}.downsampling_layer.1.bias""") ) rename_keys.append((F"""backbone.norm{i}.weight""", F"""backbone.hidden_states_norms.stage{i+1}.weight""") ) rename_keys.append((F"""backbone.norm{i}.bias""", F"""backbone.hidden_states_norms.stage{i+1}.bias""") ) # decode head rename_keys.extend( [ ('decode_head.conv_seg.weight', 'decode_head.classifier.weight'), ('decode_head.conv_seg.bias', 'decode_head.classifier.bias'), ('auxiliary_head.conv_seg.weight', 'auxiliary_head.classifier.weight'), ('auxiliary_head.conv_seg.bias', 'auxiliary_head.classifier.bias'), ] ) # fmt: on return rename_keys def UpperCamelCase__ ( A__ , A__ , A__ ) -> Any: snake_case__ : int = dct.pop(SCREAMING_SNAKE_CASE_ ) snake_case__ : Optional[int] = val def UpperCamelCase__ ( A__ , A__ , A__ ) -> List[str]: snake_case__ : str = { 'upernet-convnext-tiny': 'https://download.openmmlab.com/mmsegmentation/v0.5/convnext/upernet_convnext_tiny_fp16_512x512_160k_ade20k/upernet_convnext_tiny_fp16_512x512_160k_ade20k_20220227_124553-cad485de.pth', 'upernet-convnext-small': 'https://download.openmmlab.com/mmsegmentation/v0.5/convnext/upernet_convnext_small_fp16_512x512_160k_ade20k/upernet_convnext_small_fp16_512x512_160k_ade20k_20220227_131208-1b1e394f.pth', 'upernet-convnext-base': 'https://download.openmmlab.com/mmsegmentation/v0.5/convnext/upernet_convnext_base_fp16_512x512_160k_ade20k/upernet_convnext_base_fp16_512x512_160k_ade20k_20220227_181227-02a24fc6.pth', 'upernet-convnext-large': 'https://download.openmmlab.com/mmsegmentation/v0.5/convnext/upernet_convnext_large_fp16_640x640_160k_ade20k/upernet_convnext_large_fp16_640x640_160k_ade20k_20220226_040532-e57aa54d.pth', 'upernet-convnext-xlarge': 'https://download.openmmlab.com/mmsegmentation/v0.5/convnext/upernet_convnext_xlarge_fp16_640x640_160k_ade20k/upernet_convnext_xlarge_fp16_640x640_160k_ade20k_20220226_080344-95fc38c2.pth', } snake_case__ : Dict = model_name_to_url[model_name] snake_case__ : Optional[int] = torch.hub.load_state_dict_from_url(SCREAMING_SNAKE_CASE_ , map_location='cpu' )['state_dict'] snake_case__ : Union[str, Any] = get_upernet_config(SCREAMING_SNAKE_CASE_ ) snake_case__ : Union[str, Any] = UperNetForSemanticSegmentation(SCREAMING_SNAKE_CASE_ ) model.eval() # replace "bn" => "batch_norm" for key in state_dict.copy().keys(): snake_case__ : List[Any] = state_dict.pop(SCREAMING_SNAKE_CASE_ ) if "bn" in key: snake_case__ : List[Any] = key.replace('bn' , 'batch_norm' ) snake_case__ : Optional[Any] = val # rename keys snake_case__ : Optional[Any] = create_rename_keys(SCREAMING_SNAKE_CASE_ ) for src, dest in rename_keys: rename_key(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) model.load_state_dict(SCREAMING_SNAKE_CASE_ ) # verify on image snake_case__ : List[Any] = 'https://huggingface.co/datasets/hf-internal-testing/fixtures_ade20k/resolve/main/ADE_val_00000001.jpg' snake_case__ : int = Image.open(requests.get(SCREAMING_SNAKE_CASE_ , stream=SCREAMING_SNAKE_CASE_ ).raw ).convert('RGB' ) snake_case__ : Optional[int] = SegformerImageProcessor() snake_case__ : Optional[int] = processor(SCREAMING_SNAKE_CASE_ , return_tensors='pt' ).pixel_values with torch.no_grad(): snake_case__ : str = model(SCREAMING_SNAKE_CASE_ ) if model_name == "upernet-convnext-tiny": snake_case__ : List[str] = torch.tensor( [[-8.8_1_1_0, -8.8_1_1_0, -8.6_5_2_1], [-8.8_1_1_0, -8.8_1_1_0, -8.6_5_2_1], [-8.7_7_4_6, -8.7_7_4_6, -8.6_1_3_0]] ) elif model_name == "upernet-convnext-small": snake_case__ : Dict = torch.tensor( [[-8.8_2_3_6, -8.8_2_3_6, -8.6_7_7_1], [-8.8_2_3_6, -8.8_2_3_6, -8.6_7_7_1], [-8.7_6_3_8, -8.7_6_3_8, -8.6_2_4_0]] ) elif model_name == "upernet-convnext-base": snake_case__ : Any = torch.tensor( [[-8.8_5_5_8, -8.8_5_5_8, -8.6_9_0_5], [-8.8_5_5_8, -8.8_5_5_8, -8.6_9_0_5], [-8.7_6_6_9, -8.7_6_6_9, -8.6_0_2_1]] ) elif model_name == "upernet-convnext-large": snake_case__ : int = torch.tensor( [[-8.6_6_6_0, -8.6_6_6_0, -8.6_2_1_0], [-8.6_6_6_0, -8.6_6_6_0, -8.6_2_1_0], [-8.6_3_1_0, -8.6_3_1_0, -8.5_9_6_4]] ) elif model_name == "upernet-convnext-xlarge": snake_case__ : Tuple = torch.tensor( [[-8.4_9_8_0, -8.4_9_8_0, -8.3_9_7_7], [-8.4_9_8_0, -8.4_9_8_0, -8.3_9_7_7], [-8.4_3_7_9, -8.4_3_7_9, -8.3_4_1_2]] ) print('Logits:' , outputs.logits[0, 0, :3, :3] ) assert torch.allclose(outputs.logits[0, 0, :3, :3] , SCREAMING_SNAKE_CASE_ , atol=1e-4 ) print('Looks ok!' ) if pytorch_dump_folder_path is not None: print(F"""Saving model {model_name} to {pytorch_dump_folder_path}""" ) model.save_pretrained(SCREAMING_SNAKE_CASE_ ) print(F"""Saving processor to {pytorch_dump_folder_path}""" ) processor.save_pretrained(SCREAMING_SNAKE_CASE_ ) if push_to_hub: print(F"""Pushing model and processor for {model_name} to hub""" ) model.push_to_hub(F"""openmmlab/{model_name}""" ) processor.push_to_hub(F"""openmmlab/{model_name}""" ) if __name__ == "__main__": lowerCAmelCase__ : List[str] = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default='''upernet-convnext-tiny''', type=str, choices=[F'''upernet-convnext-{size}''' for size in ['''tiny''', '''small''', '''base''', '''large''', '''xlarge''']], help='''Name of the ConvNext UperNet model you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) parser.add_argument( '''--push_to_hub''', action='''store_true''', help='''Whether or not to push the converted model to the 🤗 hub.''' ) lowerCAmelCase__ : int = parser.parse_args() convert_upernet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
715
import random import unittest import torch from diffusers import IFInpaintingPipeline from diffusers.utils import floats_tensor from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import skip_mps, torch_device from ..pipeline_params import ( TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS, ) from ..test_pipelines_common import PipelineTesterMixin from . import IFPipelineTesterMixin @skip_mps class __snake_case ( _lowerCamelCase ,_lowerCamelCase ,unittest.TestCase ): __lowerCamelCase = IFInpaintingPipeline __lowerCamelCase = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {"""width""", """height"""} __lowerCamelCase = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS __lowerCamelCase = PipelineTesterMixin.required_optional_params - {"""latents"""} def __a ( self ) -> Optional[Any]: '''simple docstring''' return self._get_dummy_components() def __a ( self , __UpperCamelCase , __UpperCamelCase=0 ) -> str: '''simple docstring''' if str(__UpperCamelCase ).startswith('mps' ): snake_case__ : int = torch.manual_seed(__UpperCamelCase ) else: snake_case__ : Union[str, Any] = torch.Generator(device=__UpperCamelCase ).manual_seed(__UpperCamelCase ) snake_case__ : Optional[int] = floats_tensor((1, 3, 32, 32) , rng=random.Random(__UpperCamelCase ) ).to(__UpperCamelCase ) snake_case__ : Dict = floats_tensor((1, 3, 32, 32) , rng=random.Random(__UpperCamelCase ) ).to(__UpperCamelCase ) snake_case__ : Optional[Any] = { 'prompt': 'A painting of a squirrel eating a burger', 'image': image, 'mask_image': mask_image, 'generator': generator, 'num_inference_steps': 2, 'output_type': 'numpy', } return inputs @unittest.skipIf( torch_device != 'cuda' or not is_xformers_available() , reason='XFormers attention is only available with CUDA and `xformers` installed' , ) def __a ( self ) -> List[Any]: '''simple docstring''' self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3 ) def __a ( self ) -> Optional[int]: '''simple docstring''' self._test_save_load_optional_components() @unittest.skipIf(torch_device != 'cuda' , reason='float16 requires CUDA' ) def __a ( self ) -> List[str]: '''simple docstring''' super().test_save_load_floataa(expected_max_diff=1E-1 ) def __a ( self ) -> List[str]: '''simple docstring''' self._test_attention_slicing_forward_pass(expected_max_diff=1E-2 ) def __a ( self ) -> int: '''simple docstring''' self._test_save_load_local() def __a ( self ) -> List[str]: '''simple docstring''' self._test_inference_batch_single_identical( expected_max_diff=1E-2 , )
699
0
import requests def UpperCamelCase__ ( A__ , A__ ) -> None: snake_case__ : List[Any] = {"Content-Type": "application/json"} snake_case__ : Dict = requests.post(_lowerCAmelCase , json={'text': message_body} , headers=_lowerCAmelCase ) if response.status_code != 200: snake_case__ : Tuple = ( "Request to slack returned an error " F"""{response.status_code}, the response is:\n{response.text}""" ) raise ValueError(_lowerCAmelCase ) if __name__ == "__main__": # Set the slack url to the one provided by Slack when you create the webhook at # https://my.slack.com/services/new/incoming-webhook/ send_slack_message('''<YOUR MESSAGE BODY>''', '''<SLACK CHANNEL URL>''')
716
import unittest from transformers import BertGenerationTokenizer from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_torch, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin lowerCAmelCase__ : List[Any] = '''▁''' lowerCAmelCase__ : int = get_tests_dir('''fixtures/test_sentencepiece.model''') @require_sentencepiece class __snake_case ( _lowerCamelCase ,unittest.TestCase ): __lowerCamelCase = BertGenerationTokenizer __lowerCamelCase = False __lowerCamelCase = True def __a ( self ) -> Optional[int]: '''simple docstring''' super().setUp() snake_case__ : str = BertGenerationTokenizer(__UpperCamelCase , keep_accents=__UpperCamelCase ) tokenizer.save_pretrained(self.tmpdirname ) def __a ( self ) -> Optional[int]: '''simple docstring''' snake_case__ : List[str] = '<s>' snake_case__ : Dict = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(__UpperCamelCase ) , __UpperCamelCase ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(__UpperCamelCase ) , __UpperCamelCase ) def __a ( self ) -> List[str]: '''simple docstring''' snake_case__ : Tuple = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '<unk>' ) self.assertEqual(vocab_keys[1] , '<s>' ) self.assertEqual(vocab_keys[-1] , '<pad>' ) self.assertEqual(len(__UpperCamelCase ) , 1002 ) def __a ( self ) -> int: '''simple docstring''' self.assertEqual(self.get_tokenizer().vocab_size , 1000 ) def __a ( self ) -> Tuple: '''simple docstring''' snake_case__ : Optional[Any] = BertGenerationTokenizer(__UpperCamelCase , keep_accents=__UpperCamelCase ) snake_case__ : int = tokenizer.tokenize('This is a test' ) self.assertListEqual(__UpperCamelCase , ['▁This', '▁is', '▁a', '▁t', 'est'] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(__UpperCamelCase ) , [285, 46, 10, 170, 382] , ) snake_case__ : Any = tokenizer.tokenize('I was born in 92000, and this is falsé.' ) self.assertListEqual( __UpperCamelCase , [ SPIECE_UNDERLINE + 'I', SPIECE_UNDERLINE + 'was', SPIECE_UNDERLINE + 'b', 'or', 'n', SPIECE_UNDERLINE + 'in', SPIECE_UNDERLINE + '', '9', '2', '0', '0', '0', ',', SPIECE_UNDERLINE + 'and', SPIECE_UNDERLINE + 'this', SPIECE_UNDERLINE + 'is', SPIECE_UNDERLINE + 'f', 'al', 's', 'é', '.', ] , ) snake_case__ : Optional[Any] = tokenizer.convert_tokens_to_ids(__UpperCamelCase ) self.assertListEqual( __UpperCamelCase , [8, 21, 84, 55, 24, 19, 7, 0, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 0, 4] , ) snake_case__ : int = tokenizer.convert_ids_to_tokens(__UpperCamelCase ) self.assertListEqual( __UpperCamelCase , [ SPIECE_UNDERLINE + 'I', SPIECE_UNDERLINE + 'was', SPIECE_UNDERLINE + 'b', 'or', 'n', SPIECE_UNDERLINE + 'in', SPIECE_UNDERLINE + '', '<unk>', '2', '0', '0', '0', ',', SPIECE_UNDERLINE + 'and', SPIECE_UNDERLINE + 'this', SPIECE_UNDERLINE + 'is', SPIECE_UNDERLINE + 'f', 'al', 's', '<unk>', '.', ] , ) @cached_property def __a ( self ) -> Dict: '''simple docstring''' return BertGenerationTokenizer.from_pretrained('google/bert_for_seq_generation_L-24_bbc_encoder' ) @slow def __a ( self ) -> Any: '''simple docstring''' snake_case__ : int = 'Hello World!' snake_case__ : Union[str, Any] = [18536, 2260, 101] self.assertListEqual(__UpperCamelCase , self.big_tokenizer.encode(__UpperCamelCase ) ) @slow def __a ( self ) -> Optional[int]: '''simple docstring''' snake_case__ : str = ( 'This is a very long text with a lot of weird characters, such as: . , ~ ? ( ) " [ ] ! : - . Also we will' ' add words that should not exsist and be tokenized to <unk>, such as saoneuhaoesuth' ) snake_case__ : List[Any] = [ 871, 419, 358, 946, 991, 2521, 452, 358, 1357, 387, 7751, 3536, 112, 985, 456, 126, 865, 938, 5400, 5734, 458, 1368, 467, 786, 2462, 5246, 1159, 633, 865, 4519, 457, 582, 852, 2557, 427, 916, 508, 405, 34324, 497, 391, 408, 11342, 1244, 385, 100, 938, 985, 456, 574, 362, 12597, 3200, 3129, 1172, ] self.assertListEqual(__UpperCamelCase , self.big_tokenizer.encode(__UpperCamelCase ) ) @require_torch @slow def __a ( self ) -> List[str]: '''simple docstring''' import torch from transformers import BertGenerationConfig, BertGenerationEncoder # Build sequence snake_case__ : Optional[int] = list(self.big_tokenizer.get_vocab().keys() )[:10] snake_case__ : Optional[int] = ' '.join(__UpperCamelCase ) snake_case__ : int = self.big_tokenizer.encode_plus(__UpperCamelCase , return_tensors='pt' , return_token_type_ids=__UpperCamelCase ) snake_case__ : Tuple = self.big_tokenizer.batch_encode_plus( [sequence + ' ' + sequence] , return_tensors='pt' , return_token_type_ids=__UpperCamelCase ) snake_case__ : Dict = BertGenerationConfig() snake_case__ : List[str] = BertGenerationEncoder(__UpperCamelCase ) assert model.get_input_embeddings().weight.shape[0] >= self.big_tokenizer.vocab_size with torch.no_grad(): model(**__UpperCamelCase ) model(**__UpperCamelCase ) @slow def __a ( self ) -> Dict: '''simple docstring''' snake_case__ : Optional[int] = {'input_ids': [[39286, 458, 36335, 2001, 456, 13073, 13266, 455, 113, 7746, 1741, 11157, 391, 13073, 13266, 455, 113, 3967, 35412, 113, 4936, 109, 3870, 2377, 113, 30084, 45720, 458, 134, 17496, 112, 503, 11672, 113, 118, 112, 5665, 13347, 38687, 112, 1496, 31389, 112, 3268, 47264, 134, 962, 112, 16377, 8035, 23130, 430, 12169, 15518, 28592, 458, 146, 41697, 109, 391, 12169, 15518, 16689, 458, 146, 41358, 109, 452, 726, 4034, 111, 763, 35412, 5082, 388, 1903, 111, 9051, 391, 2870, 48918, 1900, 1123, 550, 998, 112, 9586, 15985, 455, 391, 410, 22955, 37636, 114], [448, 17496, 419, 3663, 385, 763, 113, 27533, 2870, 3283, 13043, 1639, 24713, 523, 656, 24013, 18550, 2521, 517, 27014, 21244, 420, 1212, 1465, 391, 927, 4833, 388, 578, 11786, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [484, 2169, 7687, 21932, 18146, 726, 363, 17032, 3391, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], 'attention_mask': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=__UpperCamelCase , model_name='google/bert_for_seq_generation_L-24_bbc_encoder' , revision='c817d1fd1be2ffa69431227a1fe320544943d4db' , )
699
0
import argparse import os from pathlib import Path import fairseq import torch from packaging import version from torch import nn from transformers import ( BartConfig, BartForConditionalGeneration, BartForSequenceClassification, BartModel, BartTokenizer, ) from transformers.utils import logging lowerCAmelCase__ : Any = ['''bart.large''', '''bart.large.mnli''', '''bart.large.cnn''', '''bart_xsum/model.pt'''] lowerCAmelCase__ : Dict = {'''bart.large''': BartModel, '''bart.large.mnli''': BartForSequenceClassification} if version.parse(fairseq.__version__) < version.parse('''0.9.0'''): raise Exception('''requires fairseq >= 0.9.0''') logging.set_verbosity_info() lowerCAmelCase__ : Union[str, Any] = logging.get_logger(__name__) lowerCAmelCase__ : Dict = ''' Hello world! cécé herlolip''' lowerCAmelCase__ : Optional[Any] = [ ('''model.classification_heads.mnli.dense.weight''', '''classification_head.dense.weight'''), ('''model.classification_heads.mnli.dense.bias''', '''classification_head.dense.bias'''), ('''model.classification_heads.mnli.out_proj.weight''', '''classification_head.out_proj.weight'''), ('''model.classification_heads.mnli.out_proj.bias''', '''classification_head.out_proj.bias'''), ] def UpperCamelCase__ ( A__ ) -> Tuple: snake_case__ : List[str] = [ 'encoder.version', 'decoder.version', 'model.encoder.version', 'model.decoder.version', '_float_tensor', ] for k in ignore_keys: state_dict.pop(A__ , A__ ) def UpperCamelCase__ ( A__ , A__ , A__ ) -> Union[str, Any]: snake_case__ : Union[str, Any] = dct.pop(A__ ) snake_case__ : List[str] = val def UpperCamelCase__ ( A__ ) -> List[Any]: snake_case__ : Optional[Any] = torch.load(A__ , map_location='cpu' ) snake_case__ : Optional[Any] = torch.hub.load('pytorch/fairseq' , 'bart.large.cnn' ).eval() hub_interface.model.load_state_dict(sd['model'] ) return hub_interface def UpperCamelCase__ ( A__ ) -> List[Any]: snake_case__ , snake_case__ : Optional[Any] = emb.weight.shape snake_case__ : List[str] = nn.Linear(A__ , A__ , bias=A__ ) snake_case__ : int = emb.weight.data return lin_layer @torch.no_grad() def UpperCamelCase__ ( A__ , A__ , A__=None ) -> str: if not os.path.exists(A__ ): snake_case__ : Optional[int] = torch.hub.load('pytorch/fairseq' , A__ ).eval() else: snake_case__ : List[Any] = load_xsum_checkpoint(A__ ) bart.model.upgrade_state_dict(bart.model.state_dict() ) if hf_checkpoint_name is None: snake_case__ : str = checkpoint_path.replace('.' , '-' ) snake_case__ : Union[str, Any] = BartConfig.from_pretrained(A__ ) snake_case__ : str = bart.encode(A__ ).unsqueeze(0 ) snake_case__ : Any = BartTokenizer.from_pretrained(A__ ).encode(A__ , return_tensors='pt' ).unsqueeze(0 ) if not torch.eq(A__ , A__ ).all(): raise ValueError( F"""converted tokenizer and pretrained tokenizer returned different output: {tokens} != {tokensa}""" ) if checkpoint_path == "bart.large.mnli": snake_case__ : List[str] = bart.state_dict() remove_ignore_keys_(A__ ) snake_case__ : List[Any] = state_dict['model.decoder.embed_tokens.weight'] for src, dest in mnli_rename_keys: rename_key(A__ , A__ , A__ ) snake_case__ : Dict = BartForSequenceClassification(A__ ).eval() model.load_state_dict(A__ ) snake_case__ : Union[str, Any] = bart.predict('mnli' , A__ , return_logits=A__ ) snake_case__ : Dict = model(A__ )[0] # logits else: # no classification heads to worry about snake_case__ : Dict = bart.model.state_dict() remove_ignore_keys_(A__ ) snake_case__ : Tuple = state_dict['decoder.embed_tokens.weight'] snake_case__ : int = bart.extract_features(A__ ) if hf_checkpoint_name == "facebook/bart-large": snake_case__ : Optional[Any] = BartModel(A__ ).eval() model.load_state_dict(A__ ) snake_case__ : str = model(A__ ).model[0] else: snake_case__ : List[Any] = BartForConditionalGeneration(A__ ).eval() # an existing summarization ckpt model.model.load_state_dict(A__ ) if hasattr(A__ , 'lm_head' ): snake_case__ : Any = make_linear_from_emb(model.model.shared ) snake_case__ : List[Any] = model.model(A__ )[0] # Check results if fairseq_output.shape != new_model_outputs.shape: raise ValueError( F"""`fairseq_output` shape and `new_model_output` shape are different: {fairseq_output.shape=}, {new_model_outputs.shape}""" ) if (fairseq_output != new_model_outputs).any().item(): raise ValueError('Some values in `fairseq_output` are different from `new_model_outputs`' ) Path(A__ ).mkdir(exist_ok=A__ ) model.save_pretrained(A__ ) if __name__ == "__main__": lowerCAmelCase__ : Optional[int] = argparse.ArgumentParser() # Required parameters parser.add_argument( '''fairseq_path''', type=str, help='''bart.large, bart.large.cnn or a path to a model.pt on local filesystem.''' ) parser.add_argument('''pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''') parser.add_argument( '''--hf_config''', default=None, type=str, help='''Which huggingface architecture to use: bart-large-xsum''' ) lowerCAmelCase__ : List[str] = parser.parse_args() convert_bart_checkpoint(args.fairseq_path, args.pytorch_dump_folder_path, hf_checkpoint_name=args.hf_config)
717
import random import torch from huggingface_hub import HfApi from diffusers import UNetaDModel lowerCAmelCase__ : List[str] = HfApi() lowerCAmelCase__ : str = {} # fmt: off lowerCAmelCase__ : int = torch.tensor([ -0.75_15, -1.68_83, 0.24_20, 0.03_00, 0.63_47, 1.34_33, -1.17_43, -3.74_67, 1.23_42, -2.24_85, 0.46_36, 0.80_76, -0.79_91, 0.39_69, 0.84_98, 0.91_89, -1.88_87, -3.35_22, 0.76_39, 0.20_40, 0.62_71, -2.71_48, -1.63_16, 3.08_39, 0.31_86, 0.27_21, -0.97_59, -1.24_61, 2.62_57, 1.35_57 ]) lowerCAmelCase__ : Dict = torch.tensor([ -2.36_39, -2.53_44, 0.00_54, -0.66_74, 1.59_90, 1.01_58, 0.31_24, -2.14_36, 1.87_95, -2.54_29, -0.15_66, -0.39_73, 1.24_90, 2.64_47, 1.22_83, -0.52_08, -2.81_54, -3.51_19, 2.38_38, 1.20_33, 1.72_01, -2.12_56, -1.45_76, 2.79_48, 2.42_04, -0.97_52, -1.25_46, 0.80_27, 3.27_58, 3.13_65 ]) lowerCAmelCase__ : Dict = torch.tensor([ -0.65_31, -0.68_91, -0.31_72, -0.53_75, -0.91_40, -0.53_67, -0.11_75, -0.78_69, -0.38_08, -0.45_13, -0.20_98, -0.00_83, 0.31_83, 0.51_40, 0.22_47, -0.13_04, -0.13_02, -0.28_02, -0.20_84, -0.20_25, -0.49_67, -0.48_73, -0.08_61, 0.69_25, 0.02_50, 0.12_90, -0.15_43, 0.63_16, 1.04_60, 1.49_43 ]) lowerCAmelCase__ : List[str] = torch.tensor([ 0.09_11, 0.11_07, 0.01_82, 0.04_35, -0.08_05, -0.06_08, 0.03_81, 0.21_72, -0.02_80, 0.13_27, -0.02_99, -0.02_55, -0.00_50, -0.11_70, -0.10_46, 0.03_09, 0.13_67, 0.17_28, -0.05_33, -0.07_48, -0.05_34, 0.16_24, 0.03_84, -0.18_05, -0.07_07, 0.06_42, 0.02_20, -0.01_34, -0.13_33, -0.15_05 ]) lowerCAmelCase__ : Union[str, Any] = torch.tensor([ 0.13_21, 0.13_37, 0.04_40, 0.06_22, -0.05_91, -0.03_70, 0.05_03, 0.21_33, -0.01_77, 0.14_15, -0.01_16, -0.01_12, 0.00_44, -0.09_80, -0.07_89, 0.03_95, 0.15_02, 0.17_85, -0.04_88, -0.05_14, -0.04_04, 0.15_39, 0.04_54, -0.15_59, -0.06_65, 0.06_59, 0.03_83, -0.00_05, -0.12_66, -0.13_86 ]) lowerCAmelCase__ : List[Any] = torch.tensor([ 0.11_54, 0.12_18, 0.03_07, 0.05_26, -0.07_11, -0.05_41, 0.03_66, 0.20_78, -0.02_67, 0.13_17, -0.02_26, -0.01_93, -0.00_14, -0.10_55, -0.09_02, 0.03_30, 0.13_91, 0.17_09, -0.05_62, -0.06_93, -0.05_60, 0.14_82, 0.03_81, -0.16_83, -0.06_81, 0.06_61, 0.03_31, -0.00_46, -0.12_68, -0.14_31 ]) lowerCAmelCase__ : Optional[Any] = torch.tensor([ 0.11_92, 0.12_40, 0.04_14, 0.06_06, -0.05_57, -0.04_12, 0.04_30, 0.20_42, -0.02_00, 0.13_85, -0.01_15, -0.01_32, 0.00_17, -0.09_65, -0.08_02, 0.03_98, 0.14_33, 0.17_47, -0.04_58, -0.05_33, -0.04_07, 0.15_45, 0.04_19, -0.15_74, -0.06_45, 0.06_26, 0.03_41, -0.00_10, -0.11_99, -0.13_90 ]) lowerCAmelCase__ : List[str] = torch.tensor([ 0.10_75, 0.10_74, 0.02_05, 0.04_31, -0.07_74, -0.06_07, 0.02_98, 0.20_42, -0.03_20, 0.12_67, -0.02_81, -0.02_50, -0.00_64, -0.10_91, -0.09_46, 0.02_90, 0.13_28, 0.16_50, -0.05_80, -0.07_38, -0.05_86, 0.14_40, 0.03_37, -0.17_46, -0.07_12, 0.06_05, 0.02_50, -0.00_99, -0.13_16, -0.14_73 ]) lowerCAmelCase__ : List[str] = torch.tensor([ -1.45_72, -2.04_81, -0.04_14, -0.60_05, 1.41_36, 0.58_48, 0.40_28, -2.73_30, 1.22_12, -2.12_28, 0.21_55, 0.40_39, 0.76_62, 2.05_35, 0.74_77, -0.32_43, -2.17_58, -2.76_48, 1.69_47, 0.70_26, 1.23_38, -1.60_78, -0.86_82, 2.28_10, 1.85_74, -0.57_18, -0.55_86, -0.01_86, 2.34_15, 2.12_51]) lowerCAmelCase__ : List[Any] = torch.tensor([ -1.36_90, -1.97_20, -0.40_90, -0.69_66, 1.46_60, 0.99_38, -0.13_85, -2.73_24, 0.77_36, -1.89_17, 0.29_23, 0.42_93, 0.16_93, 1.41_12, 1.18_87, -0.31_81, -2.21_60, -2.63_81, 1.31_70, 0.81_63, 0.92_40, -1.65_44, -0.60_99, 2.52_59, 1.64_30, -0.90_90, -0.93_92, -0.01_26, 2.42_68, 2.32_66 ]) lowerCAmelCase__ : Tuple = torch.tensor([ -1.35_25, -1.96_28, -0.39_56, -0.68_60, 1.46_64, 1.00_14, -0.12_59, -2.72_12, 0.77_72, -1.88_11, 0.29_96, 0.43_88, 0.17_04, 1.40_29, 1.17_01, -0.30_27, -2.20_53, -2.62_87, 1.33_50, 0.81_31, 0.92_74, -1.62_92, -0.60_98, 2.51_31, 1.65_05, -0.89_58, -0.92_98, -0.01_51, 2.42_57, 2.33_55 ]) lowerCAmelCase__ : List[str] = torch.tensor([ -2.05_85, -2.78_97, -0.28_50, -0.89_40, 1.90_52, 0.57_02, 0.63_45, -3.89_59, 1.59_32, -3.23_19, 0.19_74, 0.02_87, 1.75_66, 2.65_43, 0.83_87, -0.53_51, -3.27_36, -4.33_75, 2.90_29, 1.63_90, 1.46_40, -2.17_01, -1.90_13, 2.93_41, 3.49_81, -0.62_55, -1.16_44, -0.15_91, 3.70_97, 3.20_66 ]) lowerCAmelCase__ : Dict = torch.tensor([ -2.31_39, -2.55_94, -0.01_97, -0.67_85, 1.70_01, 1.16_06, 0.30_75, -2.17_40, 1.80_71, -2.56_30, -0.09_26, -0.38_11, 1.21_16, 2.62_46, 1.27_31, -0.53_98, -2.81_53, -3.61_40, 2.38_93, 1.32_62, 1.62_58, -2.18_56, -1.32_67, 2.83_95, 2.37_79, -1.06_23, -1.24_68, 0.89_59, 3.33_67, 3.22_43 ]) lowerCAmelCase__ : Dict = torch.tensor([ -2.06_28, -2.76_67, -0.20_89, -0.82_63, 2.05_39, 0.59_92, 0.64_95, -3.83_36, 1.60_25, -3.28_17, 0.17_21, -0.06_33, 1.75_16, 2.70_39, 0.81_00, -0.59_08, -3.21_13, -4.43_43, 2.92_57, 1.36_32, 1.55_62, -2.14_89, -1.98_94, 3.05_60, 3.33_96, -0.73_28, -1.04_17, 0.03_83, 3.70_93, 3.23_43 ]) lowerCAmelCase__ : Any = torch.tensor([ -1.45_74, -2.05_69, -0.04_73, -0.61_17, 1.40_18, 0.57_69, 0.41_29, -2.73_44, 1.22_41, -2.13_97, 0.20_00, 0.39_37, 0.76_16, 2.04_53, 0.73_24, -0.33_91, -2.17_46, -2.77_44, 1.69_63, 0.69_21, 1.21_87, -1.61_72, -0.88_77, 2.24_39, 1.84_71, -0.58_39, -0.56_05, -0.04_64, 2.32_50, 2.12_19 ]) # fmt: on lowerCAmelCase__ : Any = api.list_models(filter='''diffusers''') for mod in models: if "google" in mod.author or mod.modelId == "CompVis/ldm-celebahq-256": lowerCAmelCase__ : List[str] = '''/home/patrick/google_checkpoints/''' + mod.modelId.split('''/''')[-1] print(F'''Started running {mod.modelId}!!!''') if mod.modelId.startswith('''CompVis'''): lowerCAmelCase__ : int = UNetaDModel.from_pretrained(local_checkpoint, subfolder='''unet''') else: lowerCAmelCase__ : str = UNetaDModel.from_pretrained(local_checkpoint) torch.manual_seed(0) random.seed(0) lowerCAmelCase__ : Any = torch.randn(1, model.config.in_channels, model.config.sample_size, model.config.sample_size) lowerCAmelCase__ : List[str] = torch.tensor([10] * noise.shape[0]) with torch.no_grad(): lowerCAmelCase__ : int = model(noise, time_step).sample assert torch.allclose( logits[0, 0, 0, :30], results['''_'''.join('''_'''.join(mod.modelId.split('''/''')).split('''-'''))], atol=1E-3 ) print(F'''{mod.modelId} has passed successfully!!!''')
699
0
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__ : List[str] = {'''configuration_mbart''': ['''MBART_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''MBartConfig''', '''MBartOnnxConfig''']} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : Dict = ['''MBartTokenizer'''] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : List[str] = ['''MBartTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : List[Any] = [ '''MBART_PRETRAINED_MODEL_ARCHIVE_LIST''', '''MBartForCausalLM''', '''MBartForConditionalGeneration''', '''MBartForQuestionAnswering''', '''MBartForSequenceClassification''', '''MBartModel''', '''MBartPreTrainedModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : Any = [ '''TFMBartForConditionalGeneration''', '''TFMBartModel''', '''TFMBartPreTrainedModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : Tuple = [ '''FlaxMBartForConditionalGeneration''', '''FlaxMBartForQuestionAnswering''', '''FlaxMBartForSequenceClassification''', '''FlaxMBartModel''', '''FlaxMBartPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_mbart import MBART_PRETRAINED_CONFIG_ARCHIVE_MAP, MBartConfig, MBartOnnxConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_mbart import MBartTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_mbart_fast import MBartTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mbart import ( MBART_PRETRAINED_MODEL_ARCHIVE_LIST, MBartForCausalLM, MBartForConditionalGeneration, MBartForQuestionAnswering, MBartForSequenceClassification, MBartModel, MBartPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_mbart import TFMBartForConditionalGeneration, TFMBartModel, TFMBartPreTrainedModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_mbart import ( FlaxMBartForConditionalGeneration, FlaxMBartForQuestionAnswering, FlaxMBartForSequenceClassification, FlaxMBartModel, FlaxMBartPreTrainedModel, ) else: import sys lowerCAmelCase__ : Any = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
718
import warnings from ...utils import logging from .image_processing_perceiver import PerceiverImageProcessor lowerCAmelCase__ : Dict = logging.get_logger(__name__) class __snake_case ( _lowerCamelCase ): def __init__( self , *__UpperCamelCase , **__UpperCamelCase ) -> None: '''simple docstring''' warnings.warn( 'The class PerceiverFeatureExtractor is deprecated and will be removed in version 5 of Transformers.' ' Please use PerceiverImageProcessor instead.' , __UpperCamelCase , ) super().__init__(*__UpperCamelCase , **__UpperCamelCase )
699
0
import math import numpy as np import qiskit from qiskit import Aer, ClassicalRegister, QuantumCircuit, QuantumRegister, execute def UpperCamelCase__ ( A__ = 3 ) -> List[Any]: if isinstance(lowercase__ , lowercase__ ): raise TypeError('number of qubits must be a integer.' ) if number_of_qubits <= 0: raise ValueError('number of qubits must be > 0.' ) if math.floor(lowercase__ ) != number_of_qubits: raise ValueError('number of qubits must be exact integer.' ) if number_of_qubits > 10: raise ValueError('number of qubits too large to simulate(>10).' ) snake_case__ : Optional[Any] = QuantumRegister(lowercase__ , 'qr' ) snake_case__ : Any = ClassicalRegister(lowercase__ , 'cr' ) snake_case__ : Optional[Any] = QuantumCircuit(lowercase__ , lowercase__ ) snake_case__ : str = number_of_qubits for i in range(lowercase__ ): quantum_circuit.h(number_of_qubits - i - 1 ) counter -= 1 for j in range(lowercase__ ): quantum_circuit.cp(np.pi / 2 ** (counter - j) , lowercase__ , lowercase__ ) for k in range(number_of_qubits // 2 ): quantum_circuit.swap(lowercase__ , number_of_qubits - k - 1 ) # measure all the qubits quantum_circuit.measure(lowercase__ , lowercase__ ) # simulate with 10000 shots snake_case__ : Dict = Aer.get_backend('qasm_simulator' ) snake_case__ : List[str] = execute(lowercase__ , lowercase__ , shots=1_0000 ) return job.result().get_counts(lowercase__ ) if __name__ == "__main__": print( F'''Total count for quantum fourier transform state is: \ {quantum_fourier_transform(3)}''' )
719
import io import itertools import json from dataclasses import dataclass from typing import Optional import pyarrow as pa import pyarrow.json as paj import datasets from datasets.table import table_cast from datasets.utils.file_utils import readline lowerCAmelCase__ : List[Any] = datasets.utils.logging.get_logger(__name__) @dataclass class __snake_case ( datasets.BuilderConfig ): __lowerCamelCase = None __lowerCamelCase = "utf-8" __lowerCamelCase = None __lowerCamelCase = None __lowerCamelCase = True # deprecated __lowerCamelCase = None # deprecated __lowerCamelCase = 10 << 20 # 10MB __lowerCamelCase = None class __snake_case ( datasets.ArrowBasedBuilder ): __lowerCamelCase = JsonConfig def __a ( self ) -> Optional[Any]: '''simple docstring''' if self.config.block_size is not None: logger.warning('The JSON loader parameter `block_size` is deprecated. Please use `chunksize` instead' ) snake_case__ : str = self.config.block_size if self.config.use_threads is not True: logger.warning( 'The JSON loader parameter `use_threads` is deprecated and doesn\'t have any effect anymore.' ) if self.config.newlines_in_values is not None: raise ValueError('The JSON loader parameter `newlines_in_values` is no longer supported' ) return datasets.DatasetInfo(features=self.config.features ) def __a ( self , __UpperCamelCase ) -> Dict: '''simple docstring''' 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}""" ) snake_case__ : int = dl_manager.download_and_extract(self.config.data_files ) if isinstance(__UpperCamelCase , (str, list, tuple) ): snake_case__ : Any = data_files if isinstance(__UpperCamelCase , __UpperCamelCase ): snake_case__ : Optional[Any] = [files] snake_case__ : List[str] = [dl_manager.iter_files(__UpperCamelCase ) for file in files] return [datasets.SplitGenerator(name=datasets.Split.TRAIN , gen_kwargs={'files': files} )] snake_case__ : List[Any] = [] for split_name, files in data_files.items(): if isinstance(__UpperCamelCase , __UpperCamelCase ): snake_case__ : List[Any] = [files] snake_case__ : Any = [dl_manager.iter_files(__UpperCamelCase ) for file in files] splits.append(datasets.SplitGenerator(name=__UpperCamelCase , gen_kwargs={'files': files} ) ) return splits def __a ( self , __UpperCamelCase ) -> pa.Table: '''simple docstring''' if self.config.features is not None: # adding missing columns for column_name in set(self.config.features ) - set(pa_table.column_names ): snake_case__ : List[Any] = self.config.features.arrow_schema.field(__UpperCamelCase ).type snake_case__ : List[str] = pa_table.append_column(__UpperCamelCase , pa.array([None] * len(__UpperCamelCase ) , type=__UpperCamelCase ) ) # more expensive cast to support nested structures with keys in a different order # allows str <-> int/float or str to Audio for example snake_case__ : List[str] = table_cast(__UpperCamelCase , self.config.features.arrow_schema ) return pa_table def __a ( self , __UpperCamelCase ) -> int: '''simple docstring''' for file_idx, file in enumerate(itertools.chain.from_iterable(__UpperCamelCase ) ): # If the file is one json object and if we need to look at the list of items in one specific field if self.config.field is not None: with open(__UpperCamelCase , encoding=self.config.encoding , errors=self.config.encoding_errors ) as f: snake_case__ : Union[str, Any] = json.load(__UpperCamelCase ) # We keep only the field we are interested in snake_case__ : Tuple = dataset[self.config.field] # We accept two format: a list of dicts or a dict of lists if isinstance(__UpperCamelCase , (list, tuple) ): snake_case__ : List[Any] = set().union(*[row.keys() for row in dataset] ) snake_case__ : List[Any] = {col: [row.get(__UpperCamelCase ) for row in dataset] for col in keys} else: snake_case__ : List[Any] = dataset snake_case__ : Dict = pa.Table.from_pydict(__UpperCamelCase ) yield file_idx, self._cast_table(__UpperCamelCase ) # If the file has one json object per line else: with open(__UpperCamelCase , 'rb' ) as f: snake_case__ : Optional[int] = 0 # Use block_size equal to the chunk size divided by 32 to leverage multithreading # Set a default minimum value of 16kB if the chunk size is really small snake_case__ : Tuple = max(self.config.chunksize // 32 , 16 << 10 ) snake_case__ : Optional[Any] = ( self.config.encoding_errors if self.config.encoding_errors is not None else 'strict' ) while True: snake_case__ : Optional[int] = f.read(self.config.chunksize ) if not batch: break # Finish current line try: batch += f.readline() except (AttributeError, io.UnsupportedOperation): batch += readline(__UpperCamelCase ) # PyArrow only accepts utf-8 encoded bytes if self.config.encoding != "utf-8": snake_case__ : int = batch.decode(self.config.encoding , errors=__UpperCamelCase ).encode('utf-8' ) try: while True: try: snake_case__ : List[str] = paj.read_json( io.BytesIO(__UpperCamelCase ) , read_options=paj.ReadOptions(block_size=__UpperCamelCase ) ) break except (pa.ArrowInvalid, pa.ArrowNotImplementedError) as e: if ( isinstance(__UpperCamelCase , pa.ArrowInvalid ) and "straddling" not in str(__UpperCamelCase ) or block_size > len(__UpperCamelCase ) ): raise else: # Increase the block size in case it was too small. # The block size will be reset for the next file. logger.debug( F"""Batch of {len(__UpperCamelCase )} bytes couldn't be parsed with block_size={block_size}. Retrying with block_size={block_size * 2}.""" ) block_size *= 2 except pa.ArrowInvalid as e: try: with open( __UpperCamelCase , encoding=self.config.encoding , errors=self.config.encoding_errors ) as f: snake_case__ : Tuple = json.load(__UpperCamelCase ) except json.JSONDecodeError: logger.error(F"""Failed to read file '{file}' with error {type(__UpperCamelCase )}: {e}""" ) raise e # If possible, parse the file as a list of json objects and exit the loop if isinstance(__UpperCamelCase , __UpperCamelCase ): # list is the only sequence type supported in JSON try: snake_case__ : str = set().union(*[row.keys() for row in dataset] ) snake_case__ : Union[str, Any] = {col: [row.get(__UpperCamelCase ) for row in dataset] for col in keys} snake_case__ : List[str] = pa.Table.from_pydict(__UpperCamelCase ) except (pa.ArrowInvalid, AttributeError) as e: logger.error(F"""Failed to read file '{file}' with error {type(__UpperCamelCase )}: {e}""" ) raise ValueError(F"""Not able to read records in the JSON file at {file}.""" ) from None yield file_idx, self._cast_table(__UpperCamelCase ) break else: logger.error(F"""Failed to read file '{file}' with error {type(__UpperCamelCase )}: {e}""" ) raise ValueError( F"""Not able to read records in the JSON file at {file}. """ F"""You should probably indicate the field of the JSON file containing your records. """ F"""This JSON file contain the following fields: {str(list(dataset.keys() ) )}. """ F"""Select the correct one and provide it as `field='XXX'` to the dataset loading method. """ ) from None # Uncomment for debugging (will print the Arrow table size and elements) # logger.warning(f"pa_table: {pa_table} num rows: {pa_table.num_rows}") # logger.warning('\n'.join(str(pa_table.slice(i, 1).to_pydict()) for i in range(pa_table.num_rows))) yield (file_idx, batch_idx), self._cast_table(__UpperCamelCase ) batch_idx += 1
699
0
from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices lowerCAmelCase__ : List[str] = logging.get_logger(__name__) lowerCAmelCase__ : str = { 'shi-labs/nat-mini-in1k-224': 'https://huggingface.co/shi-labs/nat-mini-in1k-224/resolve/main/config.json', # See all Nat models at https://huggingface.co/models?filter=nat } class __snake_case ( __lowercase ,__lowercase ): __lowerCamelCase = '''nat''' __lowerCamelCase = { '''num_attention_heads''': '''num_heads''', '''num_hidden_layers''': '''num_layers''', } def __init__( self , __UpperCamelCase=4 , __UpperCamelCase=3 , __UpperCamelCase=64 , __UpperCamelCase=[3, 4, 6, 5] , __UpperCamelCase=[2, 4, 8, 16] , __UpperCamelCase=7 , __UpperCamelCase=3.0 , __UpperCamelCase=True , __UpperCamelCase=0.0 , __UpperCamelCase=0.0 , __UpperCamelCase=0.1 , __UpperCamelCase="gelu" , __UpperCamelCase=0.0_2 , __UpperCamelCase=1E-5 , __UpperCamelCase=0.0 , __UpperCamelCase=None , __UpperCamelCase=None , **__UpperCamelCase , ) -> List[str]: '''simple docstring''' super().__init__(**__a ) snake_case__ : Any = patch_size snake_case__ : Any = num_channels snake_case__ : int = embed_dim snake_case__ : Tuple = depths snake_case__ : Optional[int] = len(__a ) snake_case__ : Union[str, Any] = num_heads snake_case__ : Any = kernel_size snake_case__ : Dict = mlp_ratio snake_case__ : Tuple = qkv_bias snake_case__ : Optional[int] = hidden_dropout_prob snake_case__ : Tuple = attention_probs_dropout_prob snake_case__ : Optional[int] = drop_path_rate snake_case__ : Union[str, Any] = hidden_act snake_case__ : int = layer_norm_eps snake_case__ : List[Any] = initializer_range # we set the hidden_size attribute in order to make Nat work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model snake_case__ : int = int(embed_dim * 2 ** (len(__a ) - 1) ) snake_case__ : Dict = layer_scale_init_value snake_case__ : int = ["""stem"""] + [F"""stage{idx}""" for idx in range(1 , len(__a ) + 1 )] snake_case__ : Optional[int] = get_aligned_output_features_output_indices( out_features=__a , out_indices=__a , stage_names=self.stage_names )
720
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__ : Any = {'''configuration_xglm''': ['''XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''XGLMConfig''']} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : str = ['''XGLMTokenizer'''] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : Dict = ['''XGLMTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : Optional[int] = [ '''XGLM_PRETRAINED_MODEL_ARCHIVE_LIST''', '''XGLMForCausalLM''', '''XGLMModel''', '''XGLMPreTrainedModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : Dict = [ '''FlaxXGLMForCausalLM''', '''FlaxXGLMModel''', '''FlaxXGLMPreTrainedModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : Dict = [ '''TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFXGLMForCausalLM''', '''TFXGLMModel''', '''TFXGLMPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_xglm import XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XGLMConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xglm import XGLMTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xglm_fast import XGLMTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xglm import XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, XGLMForCausalLM, XGLMModel, XGLMPreTrainedModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_xglm import FlaxXGLMForCausalLM, FlaxXGLMModel, FlaxXGLMPreTrainedModel try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_xglm import ( TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFXGLMForCausalLM, TFXGLMModel, TFXGLMPreTrainedModel, ) else: import sys lowerCAmelCase__ : Tuple = _LazyModule(__name__, globals()['''__file__'''], _import_structure)
699
0
from __future__ import annotations def UpperCamelCase__ ( A__ , A__ ) -> str: snake_case__ , snake_case__ : Optional[Any] = position snake_case__ : int = [ (y + 1, x + 2), (y - 1, x + 2), (y + 1, x - 2), (y - 1, x - 2), (y + 2, x + 1), (y + 2, x - 1), (y - 2, x + 1), (y - 2, x - 1), ] snake_case__ : str = [] for position in positions: snake_case__ , snake_case__ : str = position if 0 <= y_test < n and 0 <= x_test < n: permissible_positions.append(_UpperCamelCase ) return permissible_positions def UpperCamelCase__ ( A__ ) -> Optional[Any]: return not any(elem == 0 for row in board for elem in row ) def UpperCamelCase__ ( A__ , A__ , A__ ) -> Any: if is_complete(_UpperCamelCase ): return True for position in get_valid_pos(_UpperCamelCase , len(_UpperCamelCase ) ): snake_case__ , snake_case__ : Optional[int] = position if board[y][x] == 0: snake_case__ : Union[str, Any] = curr + 1 if open_knight_tour_helper(_UpperCamelCase , _UpperCamelCase , curr + 1 ): return True snake_case__ : Dict = 0 return False def UpperCamelCase__ ( A__ ) -> Optional[int]: snake_case__ : Optional[int] = [[0 for i in range(_UpperCamelCase )] for j in range(_UpperCamelCase )] for i in range(_UpperCamelCase ): for j in range(_UpperCamelCase ): snake_case__ : Tuple = 1 if open_knight_tour_helper(_UpperCamelCase , (i, j) , 1 ): return board snake_case__ : Tuple = 0 snake_case__ : int = F"""Open Kight Tour cannot be performed on a board of size {n}""" raise ValueError(_UpperCamelCase ) if __name__ == "__main__": import doctest doctest.testmod()
721
from __future__ import annotations import random # Maximum size of the population. Bigger could be faster but is more memory expensive. lowerCAmelCase__ : Dict = 2_00 # Number of elements selected in every generation of evolution. The selection takes # place from best to worst of that generation and must be smaller than N_POPULATION. lowerCAmelCase__ : List[str] = 50 # Probability that an element of a generation can mutate, changing one of its genes. # This will guarantee that all genes will be used during evolution. lowerCAmelCase__ : List[str] = 0.4 # Just a seed to improve randomness required by the algorithm. random.seed(random.randint(0, 10_00)) def UpperCamelCase__ ( A__ , A__ ) -> tuple[str, float]: snake_case__ : Tuple = len([g for position, g in enumerate(A__ ) if g == main_target[position]] ) return (item, float(A__ )) def UpperCamelCase__ ( A__ , A__ ) -> tuple[str, str]: snake_case__ : str = random.randint(0 , len(A__ ) - 1 ) snake_case__ : int = parent_a[:random_slice] + parent_a[random_slice:] snake_case__ : Any = parent_a[:random_slice] + parent_a[random_slice:] return (child_a, child_a) def UpperCamelCase__ ( A__ , A__ ) -> str: snake_case__ : List[Any] = list(A__ ) if random.uniform(0 , 1 ) < MUTATION_PROBABILITY: snake_case__ : Optional[Any] = random.choice(A__ ) return "".join(A__ ) def UpperCamelCase__ ( A__ , A__ , A__ , ) -> list[str]: snake_case__ : Tuple = [] # Generate more children proportionally to the fitness score. snake_case__ : Optional[Any] = int(parent_a[1] * 100 ) + 1 snake_case__ : str = 10 if child_n >= 10 else child_n for _ in range(A__ ): snake_case__ : Any = population_score[random.randint(0 , A__ )][0] snake_case__ , snake_case__ : int = crossover(parent_a[0] , A__ ) # Append new string to the population list. pop.append(mutate(A__ , A__ ) ) pop.append(mutate(A__ , A__ ) ) return pop def UpperCamelCase__ ( A__ , A__ , A__ = True ) -> tuple[int, int, str]: # Verify if N_POPULATION is bigger than N_SELECTED if N_POPULATION < N_SELECTED: snake_case__ : Union[str, Any] = F"""{N_POPULATION} must be bigger than {N_SELECTED}""" raise ValueError(A__ ) # Verify that the target contains no genes besides the ones inside genes variable. snake_case__ : Tuple = sorted({c for c in target if c not in genes} ) if not_in_genes_list: snake_case__ : int = F"""{not_in_genes_list} is not in genes list, evolution cannot converge""" raise ValueError(A__ ) # Generate random starting population. snake_case__ : Union[str, Any] = [] for _ in range(A__ ): population.append(''.join([random.choice(A__ ) for i in range(len(A__ ) )] ) ) # Just some logs to know what the algorithms is doing. snake_case__ , snake_case__ : str = 0, 0 # This loop will end when we find a perfect match for our target. while True: generation += 1 total_population += len(A__ ) # Random population created. Now it's time to evaluate. # Adding a bit of concurrency can make everything faster, # # import concurrent.futures # population_score: list[tuple[str, float]] = [] # with concurrent.futures.ThreadPoolExecutor( # max_workers=NUM_WORKERS) as executor: # futures = {executor.submit(evaluate, item) for item in population} # concurrent.futures.wait(futures) # population_score = [item.result() for item in futures] # # but with a simple algorithm like this, it will probably be slower. # We just need to call evaluate for every item inside the population. snake_case__ : List[Any] = [evaluate(A__ , A__ ) for item in population] # Check if there is a matching evolution. snake_case__ : int = sorted(A__ , key=lambda A__ : x[1] , reverse=A__ ) if population_score[0][0] == target: return (generation, total_population, population_score[0][0]) # Print the best result every 10 generation. # Just to know that the algorithm is working. if debug and generation % 10 == 0: print( F"""\nGeneration: {generation}""" F"""\nTotal Population:{total_population}""" F"""\nBest score: {population_score[0][1]}""" F"""\nBest string: {population_score[0][0]}""" ) # Flush the old population, keeping some of the best evolutions. # Keeping this avoid regression of evolution. snake_case__ : Optional[int] = population[: int(N_POPULATION / 3 )] population.clear() population.extend(A__ ) # Normalize population score to be between 0 and 1. snake_case__ : str = [ (item, score / len(A__ )) for item, score in population_score ] # This is selection for i in range(A__ ): population.extend(select(population_score[int(A__ )] , A__ , A__ ) ) # Check if the population has already reached the maximum value and if so, # break the cycle. If this check is disabled, the algorithm will take # forever to compute large strings, but will also calculate small strings in # a far fewer generations. if len(A__ ) > N_POPULATION: break if __name__ == "__main__": lowerCAmelCase__ : str = ( '''This is a genetic algorithm to evaluate, combine, evolve, and mutate a string!''' ) lowerCAmelCase__ : Optional[Any] = list( ''' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklm''' '''nopqrstuvwxyz.,;!?+-*#@^\'èéòà€ù=)(&%$£/\\''' ) lowerCAmelCase__, lowerCAmelCase__, lowerCAmelCase__ : List[str] = basic(target_str, genes_list) print( F'''\nGeneration: {generation}\nTotal Population: {population}\nTarget: {target}''' )
699
0
import unittest from transformers import AutoTokenizer, NystromformerConfig, is_torch_available from transformers.testing_utils import require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( NystromformerForMaskedLM, NystromformerForMultipleChoice, NystromformerForQuestionAnswering, NystromformerForSequenceClassification, NystromformerForTokenClassification, NystromformerModel, ) from transformers.models.nystromformer.modeling_nystromformer import NYSTROMFORMER_PRETRAINED_MODEL_ARCHIVE_LIST class __snake_case : def __init__( self , __UpperCamelCase , __UpperCamelCase=13 , __UpperCamelCase=7 , __UpperCamelCase=True , __UpperCamelCase=True , __UpperCamelCase=True , __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 , ) -> Optional[Any]: '''simple docstring''' snake_case__ : List[Any] = parent snake_case__ : Optional[int] = batch_size snake_case__ : Optional[int] = seq_length snake_case__ : Dict = is_training snake_case__ : List[str] = use_input_mask snake_case__ : int = use_token_type_ids snake_case__ : List[Any] = use_labels snake_case__ : Tuple = vocab_size snake_case__ : Optional[Any] = hidden_size snake_case__ : Dict = num_hidden_layers snake_case__ : List[str] = num_attention_heads snake_case__ : Optional[Any] = intermediate_size snake_case__ : Any = hidden_act snake_case__ : Optional[Any] = hidden_dropout_prob snake_case__ : Any = attention_probs_dropout_prob snake_case__ : Union[str, Any] = max_position_embeddings snake_case__ : Any = type_vocab_size snake_case__ : Optional[int] = type_sequence_label_size snake_case__ : List[str] = initializer_range snake_case__ : Tuple = num_labels snake_case__ : Optional[int] = num_choices snake_case__ : str = scope def __a ( self ) -> Optional[int]: '''simple docstring''' snake_case__ : Union[str, Any] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) snake_case__ : Tuple = None if self.use_input_mask: snake_case__ : Optional[Any] = random_attention_mask([self.batch_size, self.seq_length] ) snake_case__ : List[Any] = None if self.use_token_type_ids: snake_case__ : Any = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) snake_case__ : List[str] = None snake_case__ : Union[str, Any] = None snake_case__ : Any = None if self.use_labels: snake_case__ : Optional[int] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) snake_case__ : Dict = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) snake_case__ : List[Any] = ids_tensor([self.batch_size] , self.num_choices ) snake_case__ : Any = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def __a ( self ) -> str: '''simple docstring''' return NystromformerConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=A__ , initializer_range=self.initializer_range , ) def __a ( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> str: '''simple docstring''' snake_case__ : Any = NystromformerModel(config=A__ ) model.to(A__ ) model.eval() snake_case__ : Any = model(A__ , attention_mask=A__ , token_type_ids=A__ ) snake_case__ : Any = model(A__ , token_type_ids=A__ ) snake_case__ : List[Any] = model(A__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __a ( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> Optional[int]: '''simple docstring''' snake_case__ : Any = NystromformerForMaskedLM(config=A__ ) model.to(A__ ) model.eval() snake_case__ : Optional[int] = model(A__ , attention_mask=A__ , token_type_ids=A__ , labels=A__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def __a ( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> List[Any]: '''simple docstring''' snake_case__ : int = NystromformerForQuestionAnswering(config=A__ ) model.to(A__ ) model.eval() snake_case__ : List[str] = model( A__ , attention_mask=A__ , token_type_ids=A__ , start_positions=A__ , end_positions=A__ , ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def __a ( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> Dict: '''simple docstring''' snake_case__ : str = self.num_labels snake_case__ : Dict = NystromformerForSequenceClassification(A__ ) model.to(A__ ) model.eval() snake_case__ : Tuple = model(A__ , attention_mask=A__ , token_type_ids=A__ , labels=A__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __a ( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> Any: '''simple docstring''' snake_case__ : str = self.num_labels snake_case__ : Dict = NystromformerForTokenClassification(config=A__ ) model.to(A__ ) model.eval() snake_case__ : Dict = model(A__ , attention_mask=A__ , token_type_ids=A__ , labels=A__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def __a ( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> int: '''simple docstring''' snake_case__ : Any = self.num_choices snake_case__ : Dict = NystromformerForMultipleChoice(config=A__ ) model.to(A__ ) model.eval() snake_case__ : List[str] = input_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() snake_case__ : Optional[Any] = token_type_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() snake_case__ : Union[str, Any] = input_mask.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() snake_case__ : int = model( A__ , attention_mask=A__ , token_type_ids=A__ , labels=A__ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def __a ( self ) -> List[Any]: '''simple docstring''' snake_case__ : Optional[int] = self.prepare_config_and_inputs() ( ( snake_case__ ) , ( snake_case__ ) , ( snake_case__ ) , ( snake_case__ ) , ( snake_case__ ) , ( snake_case__ ) , ( snake_case__ ) , ) : Any = config_and_inputs snake_case__ : Dict = {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask} return config, inputs_dict @require_torch class __snake_case ( __a ,__a ,unittest.TestCase ): __lowerCamelCase = ( ( NystromformerModel, NystromformerForMaskedLM, NystromformerForMultipleChoice, NystromformerForQuestionAnswering, NystromformerForSequenceClassification, NystromformerForTokenClassification, ) if is_torch_available() else () ) __lowerCamelCase = ( { """feature-extraction""": NystromformerModel, """fill-mask""": NystromformerForMaskedLM, """question-answering""": NystromformerForQuestionAnswering, """text-classification""": NystromformerForSequenceClassification, """token-classification""": NystromformerForTokenClassification, """zero-shot""": NystromformerForSequenceClassification, } if is_torch_available() else {} ) __lowerCamelCase = False __lowerCamelCase = False def __a ( self ) -> Union[str, Any]: '''simple docstring''' snake_case__ : Optional[Any] = NystromformerModelTester(self ) snake_case__ : Union[str, Any] = ConfigTester(self , config_class=A__ , hidden_size=37 ) def __a ( self ) -> Any: '''simple docstring''' self.config_tester.run_common_tests() def __a ( self ) -> Union[str, Any]: '''simple docstring''' snake_case__ : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*A__ ) def __a ( self ) -> str: '''simple docstring''' snake_case__ : Optional[Any] = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: snake_case__ : Any = type self.model_tester.create_and_check_model(*A__ ) def __a ( self ) -> List[Any]: '''simple docstring''' snake_case__ : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*A__ ) def __a ( self ) -> Optional[Any]: '''simple docstring''' snake_case__ : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_multiple_choice(*A__ ) def __a ( self ) -> Tuple: '''simple docstring''' snake_case__ : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*A__ ) def __a ( self ) -> Optional[Any]: '''simple docstring''' snake_case__ : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*A__ ) def __a ( self ) -> Dict: '''simple docstring''' snake_case__ : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*A__ ) @slow def __a ( self ) -> Tuple: '''simple docstring''' for model_name in NYSTROMFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: snake_case__ : str = NystromformerModel.from_pretrained(A__ ) self.assertIsNotNone(A__ ) @require_torch class __snake_case ( unittest.TestCase ): @slow def __a ( self ) -> List[str]: '''simple docstring''' snake_case__ : str = NystromformerModel.from_pretrained('uw-madison/nystromformer-512' ) snake_case__ : Optional[Any] = torch.tensor([[0, 1, 2, 3, 4, 5]] ) with torch.no_grad(): snake_case__ : List[Any] = model(A__ )[0] snake_case__ : List[str] = torch.Size((1, 6, 768) ) self.assertEqual(output.shape , A__ ) snake_case__ : Tuple = torch.tensor( [[[-0.4_5_3_2, -0.0_9_3_6, 0.5_1_3_7], [-0.2_6_7_6, 0.0_6_2_8, 0.6_1_8_6], [-0.3_6_2_9, -0.1_7_2_6, 0.4_7_1_6]]] ) self.assertTrue(torch.allclose(output[:, :3, :3] , A__ , atol=1E-4 ) ) @slow def __a ( self ) -> Union[str, Any]: '''simple docstring''' snake_case__ : str = 'the [MASK] of Belgium is Brussels' snake_case__ : Dict = AutoTokenizer.from_pretrained('uw-madison/nystromformer-512' ) snake_case__ : int = NystromformerForMaskedLM.from_pretrained('uw-madison/nystromformer-512' ) snake_case__ : Dict = tokenizer(A__ , return_tensors='pt' ) with torch.no_grad(): snake_case__ : Dict = model(encoding.input_ids ).logits snake_case__ : Any = token_logits[:, 2, :].argmax(-1 )[0] self.assertEqual(tokenizer.decode(A__ ) , 'capital' )
700
from __future__ import annotations from collections.abc import Iterator from typing import Generic, TypeVar lowerCAmelCase__ : Optional[int] = TypeVar('''T''') class __snake_case ( Generic[T] ): def __init__( self , __UpperCamelCase ) -> Any: '''simple docstring''' snake_case__ : Optional[int] = data snake_case__ : Node[T] | None = None def __str__( self ) -> str: '''simple docstring''' return F"""{self.data}""" class __snake_case ( Generic[T] ): def __init__( self ) -> None: '''simple docstring''' snake_case__ : Node[T] | None = None def __iter__( self ) -> Iterator[T]: '''simple docstring''' snake_case__ : str = self.top while node: yield node.data snake_case__ : Dict = node.next def __str__( self ) -> str: '''simple docstring''' return "->".join([str(__UpperCamelCase ) for item in self] ) def __len__( self ) -> int: '''simple docstring''' return len(tuple(iter(self ) ) ) def __a ( self ) -> bool: '''simple docstring''' return self.top is None def __a ( self , __UpperCamelCase ) -> None: '''simple docstring''' snake_case__ : str = Node(__UpperCamelCase ) if not self.is_empty(): snake_case__ : List[str] = self.top snake_case__ : Tuple = node def __a ( self ) -> T: '''simple docstring''' if self.is_empty(): raise IndexError('pop from empty stack' ) assert isinstance(self.top , __UpperCamelCase ) snake_case__ : List[str] = self.top snake_case__ : Union[str, Any] = self.top.next return pop_node.data def __a ( self ) -> T: '''simple docstring''' if self.is_empty(): raise IndexError('peek from empty stack' ) assert self.top is not None return self.top.data def __a ( self ) -> None: '''simple docstring''' snake_case__ : Any = None if __name__ == "__main__": from doctest import testmod testmod()
699
0
import importlib.metadata import operator import re import sys from typing import Optional from packaging import version lowerCAmelCase__ : List[Any] = { '''<''': operator.lt, '''<=''': operator.le, '''==''': operator.eq, '''!=''': operator.ne, '''>=''': operator.ge, '''>''': operator.gt, } def UpperCamelCase__ ( A__ , A__ , A__ , A__ , A__ , A__ ) -> Optional[int]: 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(_SCREAMING_SNAKE_CASE ) , version.parse(_SCREAMING_SNAKE_CASE ) ): raise ImportError( F"""{requirement} is required for a normal functioning of this module, but found {pkg}=={got_ver}.{hint}""" ) def UpperCamelCase__ ( A__ , A__ = None ) -> None: snake_case__ : Union[str, Any] = F"""\n{hint}""" if hint is not None else '' # non-versioned check if re.match(r'^[\w_\-\d]+$' , _SCREAMING_SNAKE_CASE ): snake_case__ , snake_case__ , snake_case__ : List[Any] = requirement, None, None else: snake_case__ : List[Any] = re.findall(r'^([^!=<>\s]+)([\s!=<>]{1,2}.+)' , _SCREAMING_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}""" ) snake_case__ , snake_case__ : Optional[int] = match[0] snake_case__ : Tuple = want_full.split(',' ) # there could be multiple requirements snake_case__ : Tuple = {} for w in want_range: snake_case__ : Optional[int] = re.findall(r'^([\s!=<>]{1,2})(.+)' , _SCREAMING_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}""" ) snake_case__ , snake_case__ : Optional[Any] = match[0] snake_case__ : List[str] = 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": snake_case__ : Dict = '.'.join([str(_SCREAMING_SNAKE_CASE ) for x in sys.version_info[:3]] ) for op, want_ver in wanted.items(): _compare_versions(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) return # check if any version is installed try: snake_case__ : Union[str, Any] = importlib.metadata.version(_SCREAMING_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(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) def UpperCamelCase__ ( A__ ) -> Optional[Any]: snake_case__ : str = 'Try: pip install transformers -U or pip install -e \'.[dev]\' if you\'re working with git main' return require_version(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE )
701
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__ : Dict = logging.get_logger(__name__) lowerCAmelCase__ : int = { '''sail/poolformer_s12''': '''https://huggingface.co/sail/poolformer_s12/resolve/main/config.json''', # See all PoolFormer models at https://huggingface.co/models?filter=poolformer } class __snake_case ( _lowerCamelCase ): __lowerCamelCase = """poolformer""" def __init__( self , __UpperCamelCase=3 , __UpperCamelCase=16 , __UpperCamelCase=16 , __UpperCamelCase=3 , __UpperCamelCase=4.0 , __UpperCamelCase=[2, 2, 6, 2] , __UpperCamelCase=[64, 128, 320, 512] , __UpperCamelCase=[7, 3, 3, 3] , __UpperCamelCase=[4, 2, 2, 2] , __UpperCamelCase=[2, 1, 1, 1] , __UpperCamelCase=4 , __UpperCamelCase=0.0 , __UpperCamelCase="gelu" , __UpperCamelCase=True , __UpperCamelCase=1E-5 , __UpperCamelCase=0.0_2 , **__UpperCamelCase , ) -> Any: '''simple docstring''' snake_case__ : List[str] = num_channels snake_case__ : Dict = patch_size snake_case__ : Optional[int] = stride snake_case__ : str = padding snake_case__ : List[str] = pool_size snake_case__ : List[Any] = hidden_sizes snake_case__ : List[Any] = mlp_ratio snake_case__ : Union[str, Any] = depths snake_case__ : Dict = patch_sizes snake_case__ : Dict = strides snake_case__ : Dict = num_encoder_blocks snake_case__ : Union[str, Any] = drop_path_rate snake_case__ : List[str] = hidden_act snake_case__ : Optional[Any] = use_layer_scale snake_case__ : int = layer_scale_init_value snake_case__ : Dict = initializer_range super().__init__(**__UpperCamelCase ) class __snake_case ( _lowerCamelCase ): __lowerCamelCase = version.parse("""1.11""" ) @property def __a ( self ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' return OrderedDict( [ ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}), ] ) @property def __a ( self ) -> float: '''simple docstring''' return 2E-3
699
0
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ViTConfig, ViTForImageClassification, ViTImageProcessor, ViTModel from transformers.utils import logging logging.set_verbosity_info() lowerCAmelCase__ : Union[str, Any] = logging.get_logger(__name__) def UpperCamelCase__ ( A__ , A__=False ) -> str: snake_case__ : Tuple = [] for i in range(config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append((F"""blocks.{i}.norm1.weight""", F"""vit.encoder.layer.{i}.layernorm_before.weight""") ) rename_keys.append((F"""blocks.{i}.norm1.bias""", F"""vit.encoder.layer.{i}.layernorm_before.bias""") ) rename_keys.append((F"""blocks.{i}.attn.proj.weight""", F"""vit.encoder.layer.{i}.attention.output.dense.weight""") ) rename_keys.append((F"""blocks.{i}.attn.proj.bias""", F"""vit.encoder.layer.{i}.attention.output.dense.bias""") ) rename_keys.append((F"""blocks.{i}.norm2.weight""", F"""vit.encoder.layer.{i}.layernorm_after.weight""") ) rename_keys.append((F"""blocks.{i}.norm2.bias""", F"""vit.encoder.layer.{i}.layernorm_after.bias""") ) rename_keys.append((F"""blocks.{i}.mlp.fc1.weight""", F"""vit.encoder.layer.{i}.intermediate.dense.weight""") ) rename_keys.append((F"""blocks.{i}.mlp.fc1.bias""", F"""vit.encoder.layer.{i}.intermediate.dense.bias""") ) rename_keys.append((F"""blocks.{i}.mlp.fc2.weight""", F"""vit.encoder.layer.{i}.output.dense.weight""") ) rename_keys.append((F"""blocks.{i}.mlp.fc2.bias""", F"""vit.encoder.layer.{i}.output.dense.bias""") ) # projection layer + position embeddings rename_keys.extend( [ ('cls_token', 'vit.embeddings.cls_token'), ('patch_embed.proj.weight', 'vit.embeddings.patch_embeddings.projection.weight'), ('patch_embed.proj.bias', 'vit.embeddings.patch_embeddings.projection.bias'), ('pos_embed', 'vit.embeddings.position_embeddings'), ] ) if base_model: # layernorm + pooler rename_keys.extend( [ ('norm.weight', 'layernorm.weight'), ('norm.bias', 'layernorm.bias'), ] ) # if just the base model, we should remove "vit" from all keys that start with "vit" snake_case__ : Optional[Any] = [(pair[0], pair[1][4:]) if pair[1].startswith('vit' ) else pair for pair in rename_keys] else: # layernorm + classification head rename_keys.extend( [ ('norm.weight', 'vit.layernorm.weight'), ('norm.bias', 'vit.layernorm.bias'), ('head.weight', 'classifier.weight'), ('head.bias', 'classifier.bias'), ] ) return rename_keys def UpperCamelCase__ ( A__ , A__ , A__=False ) -> str: for i in range(config.num_hidden_layers ): if base_model: snake_case__ : Tuple = '' else: snake_case__ : Dict = 'vit.' # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) snake_case__ : Union[str, Any] = state_dict.pop(F"""blocks.{i}.attn.qkv.weight""" ) snake_case__ : Union[str, Any] = state_dict.pop(F"""blocks.{i}.attn.qkv.bias""" ) # next, add query, keys and values (in that order) to the state dict snake_case__ : str = in_proj_weight[ : config.hidden_size, : ] snake_case__ : int = in_proj_bias[: config.hidden_size] snake_case__ : List[Any] = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] snake_case__ : Union[str, Any] = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] snake_case__ : int = in_proj_weight[ -config.hidden_size :, : ] snake_case__ : Optional[Any] = in_proj_bias[-config.hidden_size :] def UpperCamelCase__ ( A__ ) -> Tuple: snake_case__ : Any = ['head.weight', 'head.bias'] for k in ignore_keys: state_dict.pop(lowercase__ , lowercase__ ) def UpperCamelCase__ ( A__ , A__ , A__ ) -> Union[str, Any]: snake_case__ : Dict = dct.pop(lowercase__ ) snake_case__ : str = val def UpperCamelCase__ ( ) -> Any: snake_case__ : str = 'http://images.cocodataset.org/val2017/000000039769.jpg' snake_case__ : Dict = Image.open(requests.get(lowercase__ , stream=lowercase__ ).raw ) return im @torch.no_grad() def UpperCamelCase__ ( A__ , A__ , A__=True ) -> str: snake_case__ : Union[str, Any] = ViTConfig() # patch_size if model_name[-1] == "8": snake_case__ : int = 8 # set labels if required if not base_model: snake_case__ : List[Any] = 1000 snake_case__ : Dict = 'huggingface/label-files' snake_case__ : List[str] = 'imagenet-1k-id2label.json' snake_case__ : Tuple = json.load(open(hf_hub_download(lowercase__ , lowercase__ , repo_type='dataset' ) , 'r' ) ) snake_case__ : Optional[int] = {int(lowercase__ ): v for k, v in idalabel.items()} snake_case__ : Union[str, Any] = idalabel snake_case__ : Any = {v: k for k, v in idalabel.items()} # size of the architecture if model_name in ["dino_vits8", "dino_vits16"]: snake_case__ : Dict = 384 snake_case__ : Optional[Any] = 1536 snake_case__ : List[Any] = 12 snake_case__ : str = 6 # load original model from torch hub snake_case__ : int = torch.hub.load('facebookresearch/dino:main' , lowercase__ ) original_model.eval() # load state_dict of original model, remove and rename some keys snake_case__ : Optional[Any] = original_model.state_dict() if base_model: remove_classification_head_(lowercase__ ) snake_case__ : Optional[Any] = create_rename_keys(lowercase__ , base_model=lowercase__ ) for src, dest in rename_keys: rename_key(lowercase__ , lowercase__ , lowercase__ ) read_in_q_k_v(lowercase__ , lowercase__ , lowercase__ ) # load HuggingFace model if base_model: snake_case__ : Union[str, Any] = ViTModel(lowercase__ , add_pooling_layer=lowercase__ ).eval() else: snake_case__ : Optional[Any] = ViTForImageClassification(lowercase__ ).eval() model.load_state_dict(lowercase__ ) # Check outputs on an image, prepared by ViTImageProcessor snake_case__ : Optional[Any] = ViTImageProcessor() snake_case__ : Any = image_processor(images=prepare_img() , return_tensors='pt' ) snake_case__ : Union[str, Any] = encoding['pixel_values'] snake_case__ : List[Any] = model(lowercase__ ) if base_model: snake_case__ : Dict = original_model(lowercase__ ) assert torch.allclose(lowercase__ , outputs.last_hidden_state[:, 0, :] , atol=1e-1 ) else: snake_case__ : Union[str, Any] = original_model(lowercase__ ) assert logits.shape == outputs.logits.shape assert torch.allclose(lowercase__ , outputs.logits , atol=1e-3 ) Path(lowercase__ ).mkdir(exist_ok=lowercase__ ) print(F"""Saving model {model_name} to {pytorch_dump_folder_path}""" ) model.save_pretrained(lowercase__ ) print(F"""Saving image processor to {pytorch_dump_folder_path}""" ) image_processor.save_pretrained(lowercase__ ) if __name__ == "__main__": lowerCAmelCase__ : Union[str, Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default='''dino_vitb16''', type=str, help='''Name of the model trained with DINO you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) parser.add_argument( '''--base_model''', action='''store_true''', help='''Whether to only convert the base model (no projection head weights).''', ) parser.set_defaults(base_model=True) lowerCAmelCase__ : Optional[Any] = parser.parse_args() convert_vit_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.base_model)
702
import numpy as np import qiskit def UpperCamelCase__ ( A__ = 8 , A__ = None ) -> str: snake_case__ : Optional[int] = np.random.default_rng(seed=A__ ) # Roughly 25% of the qubits will contribute to the key. # So we take more than we need. snake_case__ : Tuple = 6 * key_len # Measurement basis for Alice's qubits. snake_case__ : Tuple = rng.integers(2 , size=A__ ) # The set of states Alice will prepare. snake_case__ : List[str] = rng.integers(2 , size=A__ ) # Measurement basis for Bob's qubits. snake_case__ : List[Any] = rng.integers(2 , size=A__ ) # Quantum Circuit to simulate BB84 snake_case__ : Any = qiskit.QuantumCircuit(A__ , name='BB84' ) # Alice prepares her qubits according to rules above. for index, _ in enumerate(A__ ): if alice_state[index] == 1: bbaa_circ.x(A__ ) if alice_basis[index] == 1: bbaa_circ.h(A__ ) bbaa_circ.barrier() # Bob measures the received qubits according to rules above. for index, _ in enumerate(A__ ): if bob_basis[index] == 1: bbaa_circ.h(A__ ) bbaa_circ.barrier() bbaa_circ.measure_all() # Simulate the quantum circuit. snake_case__ : List[str] = qiskit.Aer.get_backend('aer_simulator' ) # We only need to run one shot because the key is unique. # Multiple shots will produce the same key. snake_case__ : Optional[Any] = qiskit.execute(A__ , A__ , shots=1 , seed_simulator=A__ ) # Returns the result of measurement. snake_case__ : Union[str, Any] = job.result().get_counts(A__ ).most_frequent() # Extracting the generated key from the simulation results. # Only keep measurement results where Alice and Bob chose the same basis. snake_case__ : Optional[Any] = ''.join( [ result_bit for alice_basis_bit, bob_basis_bit, result_bit in zip( A__ , A__ , A__ ) if alice_basis_bit == bob_basis_bit ] ) # Get final key. Pad with 0 if too short, otherwise truncate. snake_case__ : Tuple = gen_key[:key_len] if len(A__ ) >= key_len else gen_key.ljust(A__ , '0' ) return key if __name__ == "__main__": print(F'''The generated key is : {bbaa(8, seed=0)}''') from doctest import testmod testmod()
699
0
'''simple docstring''' def UpperCamelCase__ ( A__ , A__ ) -> str: if not len(lowerCAmelCase_ ) == len(lowerCAmelCase_ ) == 3: raise ValueError('Please enter a valid equation.' ) if equationa[0] == equationa[1] == equationa[0] == equationa[1] == 0: raise ValueError('Both a & b of two equations can\'t be zero.' ) # Extract the coefficients snake_case__ : Optional[Any] = equationa snake_case__ : Tuple = equationa # Calculate the determinants of the matrices snake_case__ : List[Any] = aa * ba - aa * ba snake_case__ : Dict = ca * ba - ca * ba snake_case__ : int = aa * ca - aa * ca # Check if the system of linear equations has a solution (using Cramer's rule) if determinant == 0: if determinant_x == determinant_y == 0: raise ValueError('Infinite solutions. (Consistent system)' ) else: raise ValueError('No solution. (Inconsistent system)' ) else: if determinant_x == determinant_y == 0: # Trivial solution (Inconsistent system) return (0.0, 0.0) else: snake_case__ : Optional[int] = determinant_x / determinant snake_case__ : Optional[Any] = determinant_y / determinant # Non-Trivial Solution (Consistent system) return (x, y)
703
def UpperCamelCase__ ( A__ , A__ , A__ ) -> int: if exponent == 1: return base if exponent % 2 == 0: snake_case__ : Dict = _modexpt(A__ , exponent // 2 , A__ ) % modulo_value return (x * x) % modulo_value else: return (base * _modexpt(A__ , exponent - 1 , A__ )) % modulo_value def UpperCamelCase__ ( A__ = 1777 , A__ = 1855 , A__ = 8 ) -> int: snake_case__ : Tuple = base for _ in range(1 , A__ ): snake_case__ : Any = _modexpt(A__ , A__ , 10**digits ) return result if __name__ == "__main__": print(F'''{solution() = }''')
699
0
import re from filelock import FileLock try: import nltk lowerCAmelCase__ : List[str] = True except (ImportError, ModuleNotFoundError): lowerCAmelCase__ : Tuple = False if NLTK_AVAILABLE: with FileLock('''.lock''') as lock: nltk.download('''punkt''', quiet=True) def UpperCamelCase__ ( A__ ) -> Optional[int]: '''simple docstring''' re.sub('<n>' , '' , SCREAMING_SNAKE_CASE__ ) # remove pegasus newline char assert NLTK_AVAILABLE, "nltk must be installed to separate newlines between sentences. (pip install nltk)" return "\n".join(nltk.sent_tokenize(SCREAMING_SNAKE_CASE__ ) )
704
# tests directory-specific settings - this file is run automatically # by pytest before any tests are run import sys import warnings from os.path import abspath, dirname, join # allow having multiple repository checkouts and not needing to remember to rerun # 'pip install -e .[dev]' when switching between checkouts and running tests. lowerCAmelCase__ : Tuple = abspath(join(dirname(dirname(__file__)), '''src''')) sys.path.insert(1, git_repo_path) # silence FutureWarning warnings in tests since often we can't act on them until # they become normal warnings - i.e. the tests still need to test the current functionality warnings.simplefilter(action='''ignore''', category=FutureWarning) def UpperCamelCase__ ( A__ ) -> Optional[Any]: from diffusers.utils.testing_utils import pytest_addoption_shared pytest_addoption_shared(A__ ) def UpperCamelCase__ ( A__ ) -> Optional[Any]: from diffusers.utils.testing_utils import pytest_terminal_summary_main snake_case__ : Union[str, Any] = terminalreporter.config.getoption('--make-reports' ) if make_reports: pytest_terminal_summary_main(A__ , id=A__ )
699
0
def UpperCamelCase__ ( A__ = 100 ) -> Optional[int]: snake_case__ : int = set() snake_case__ : Dict = 0 snake_case__ : Union[str, Any] = n + 1 # maximum limit for a in range(2 , a_ ): for b in range(2 , a_ ): snake_case__ : Tuple = a**b # calculates the current power collect_powers.add(a_ ) # adds the result to the set return len(a_ ) if __name__ == "__main__": print('''Number of terms ''', solution(int(str(input()).strip())))
705
def UpperCamelCase__ ( A__ ) -> list[int]: if length <= 0 or not isinstance(A__ , A__ ): raise ValueError('Length must be a positive integer.' ) return [n * (2 * n - 1) for n in range(A__ )] if __name__ == "__main__": print(hexagonal_numbers(length=5)) print(hexagonal_numbers(length=10))
699
0
from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging lowerCAmelCase__ : List[str] = logging.get_logger(__name__) lowerCAmelCase__ : Tuple = { """facebook/data2vec-vision-base-ft""": ( """https://huggingface.co/facebook/data2vec-vision-base-ft/resolve/main/config.json""" ), } class __snake_case ( _lowerCamelCase ): __lowerCamelCase = "data2vec-vision" def __init__( self , __UpperCamelCase=768 , __UpperCamelCase=12 , __UpperCamelCase=12 , __UpperCamelCase=3072 , __UpperCamelCase="gelu" , __UpperCamelCase=0.0 , __UpperCamelCase=0.0 , __UpperCamelCase=0.0_2 , __UpperCamelCase=1E-12 , __UpperCamelCase=224 , __UpperCamelCase=16 , __UpperCamelCase=3 , __UpperCamelCase=False , __UpperCamelCase=False , __UpperCamelCase=False , __UpperCamelCase=False , __UpperCamelCase=0.1 , __UpperCamelCase=0.1 , __UpperCamelCase=True , __UpperCamelCase=[3, 5, 7, 11] , __UpperCamelCase=[1, 2, 3, 6] , __UpperCamelCase=True , __UpperCamelCase=0.4 , __UpperCamelCase=256 , __UpperCamelCase=1 , __UpperCamelCase=False , __UpperCamelCase=255 , **__UpperCamelCase , ) -> Any: '''simple docstring''' super().__init__(**__UpperCamelCase ) snake_case__ : int = hidden_size snake_case__ : List[Any] = num_hidden_layers snake_case__ : Any = num_attention_heads snake_case__ : Optional[int] = intermediate_size snake_case__ : Dict = hidden_act snake_case__ : List[str] = hidden_dropout_prob snake_case__ : Dict = attention_probs_dropout_prob snake_case__ : List[Any] = initializer_range snake_case__ : str = layer_norm_eps snake_case__ : Any = image_size snake_case__ : Union[str, Any] = patch_size snake_case__ : Optional[Any] = num_channels snake_case__ : Optional[int] = use_mask_token snake_case__ : Optional[int] = use_absolute_position_embeddings snake_case__ : Union[str, Any] = use_relative_position_bias snake_case__ : Optional[int] = use_shared_relative_position_bias snake_case__ : Any = layer_scale_init_value snake_case__ : int = drop_path_rate snake_case__ : Optional[Any] = use_mean_pooling # decode head attributes (semantic segmentation) snake_case__ : Dict = out_indices snake_case__ : Any = pool_scales # auxiliary head attributes (semantic segmentation) snake_case__ : Union[str, Any] = use_auxiliary_head snake_case__ : Tuple = auxiliary_loss_weight snake_case__ : Dict = auxiliary_channels snake_case__ : int = auxiliary_num_convs snake_case__ : Tuple = auxiliary_concat_input snake_case__ : int = semantic_loss_ignore_index class __snake_case ( _lowerCamelCase ): __lowerCamelCase = version.parse("""1.11""" ) @property def __a ( self ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' return OrderedDict( [ ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}), ] ) @property def __a ( self ) -> float: '''simple docstring''' return 1E-4
706
import argparse import json from collections import OrderedDict from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( ConditionalDetrConfig, ConditionalDetrForObjectDetection, ConditionalDetrForSegmentation, ConditionalDetrImageProcessor, ) from transformers.utils import logging logging.set_verbosity_info() lowerCAmelCase__ : Dict = logging.get_logger(__name__) # here we list all keys to be renamed (original name on the left, our name on the right) lowerCAmelCase__ : Optional[Any] = [] for i in range(6): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append( (F'''transformer.encoder.layers.{i}.self_attn.out_proj.weight''', F'''encoder.layers.{i}.self_attn.out_proj.weight''') ) rename_keys.append( (F'''transformer.encoder.layers.{i}.self_attn.out_proj.bias''', F'''encoder.layers.{i}.self_attn.out_proj.bias''') ) rename_keys.append((F'''transformer.encoder.layers.{i}.linear1.weight''', F'''encoder.layers.{i}.fc1.weight''')) rename_keys.append((F'''transformer.encoder.layers.{i}.linear1.bias''', F'''encoder.layers.{i}.fc1.bias''')) rename_keys.append((F'''transformer.encoder.layers.{i}.linear2.weight''', F'''encoder.layers.{i}.fc2.weight''')) rename_keys.append((F'''transformer.encoder.layers.{i}.linear2.bias''', F'''encoder.layers.{i}.fc2.bias''')) rename_keys.append( (F'''transformer.encoder.layers.{i}.norm1.weight''', F'''encoder.layers.{i}.self_attn_layer_norm.weight''') ) rename_keys.append((F'''transformer.encoder.layers.{i}.norm1.bias''', F'''encoder.layers.{i}.self_attn_layer_norm.bias''')) rename_keys.append((F'''transformer.encoder.layers.{i}.norm2.weight''', F'''encoder.layers.{i}.final_layer_norm.weight''')) rename_keys.append((F'''transformer.encoder.layers.{i}.norm2.bias''', F'''encoder.layers.{i}.final_layer_norm.bias''')) # decoder layers: 2 times output projection, 2 feedforward neural networks and 3 layernorms rename_keys.append( (F'''transformer.decoder.layers.{i}.self_attn.out_proj.weight''', F'''decoder.layers.{i}.self_attn.out_proj.weight''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.self_attn.out_proj.bias''', F'''decoder.layers.{i}.self_attn.out_proj.bias''') ) rename_keys.append( ( F'''transformer.decoder.layers.{i}.cross_attn.out_proj.weight''', F'''decoder.layers.{i}.encoder_attn.out_proj.weight''', ) ) rename_keys.append( ( F'''transformer.decoder.layers.{i}.cross_attn.out_proj.bias''', F'''decoder.layers.{i}.encoder_attn.out_proj.bias''', ) ) rename_keys.append((F'''transformer.decoder.layers.{i}.linear1.weight''', F'''decoder.layers.{i}.fc1.weight''')) rename_keys.append((F'''transformer.decoder.layers.{i}.linear1.bias''', F'''decoder.layers.{i}.fc1.bias''')) rename_keys.append((F'''transformer.decoder.layers.{i}.linear2.weight''', F'''decoder.layers.{i}.fc2.weight''')) rename_keys.append((F'''transformer.decoder.layers.{i}.linear2.bias''', F'''decoder.layers.{i}.fc2.bias''')) rename_keys.append( (F'''transformer.decoder.layers.{i}.norm1.weight''', F'''decoder.layers.{i}.self_attn_layer_norm.weight''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.norm1.bias''', F'''decoder.layers.{i}.self_attn_layer_norm.bias''')) rename_keys.append( (F'''transformer.decoder.layers.{i}.norm2.weight''', F'''decoder.layers.{i}.encoder_attn_layer_norm.weight''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.norm2.bias''', F'''decoder.layers.{i}.encoder_attn_layer_norm.bias''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.norm3.weight''', F'''decoder.layers.{i}.final_layer_norm.weight''')) rename_keys.append((F'''transformer.decoder.layers.{i}.norm3.bias''', F'''decoder.layers.{i}.final_layer_norm.bias''')) # q, k, v projections in self/cross-attention in decoder for conditional DETR rename_keys.append( (F'''transformer.decoder.layers.{i}.sa_qcontent_proj.weight''', F'''decoder.layers.{i}.sa_qcontent_proj.weight''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.sa_kcontent_proj.weight''', F'''decoder.layers.{i}.sa_kcontent_proj.weight''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.sa_qpos_proj.weight''', F'''decoder.layers.{i}.sa_qpos_proj.weight''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.sa_kpos_proj.weight''', F'''decoder.layers.{i}.sa_kpos_proj.weight''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.sa_v_proj.weight''', F'''decoder.layers.{i}.sa_v_proj.weight''')) rename_keys.append( (F'''transformer.decoder.layers.{i}.ca_qcontent_proj.weight''', F'''decoder.layers.{i}.ca_qcontent_proj.weight''') ) # rename_keys.append((f"transformer.decoder.layers.{i}.ca_qpos_proj.weight", f"decoder.layers.{i}.ca_qpos_proj.weight")) rename_keys.append( (F'''transformer.decoder.layers.{i}.ca_kcontent_proj.weight''', F'''decoder.layers.{i}.ca_kcontent_proj.weight''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.ca_kpos_proj.weight''', F'''decoder.layers.{i}.ca_kpos_proj.weight''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.ca_v_proj.weight''', F'''decoder.layers.{i}.ca_v_proj.weight''')) rename_keys.append( (F'''transformer.decoder.layers.{i}.ca_qpos_sine_proj.weight''', F'''decoder.layers.{i}.ca_qpos_sine_proj.weight''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.sa_qcontent_proj.bias''', F'''decoder.layers.{i}.sa_qcontent_proj.bias''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.sa_kcontent_proj.bias''', F'''decoder.layers.{i}.sa_kcontent_proj.bias''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.sa_qpos_proj.bias''', F'''decoder.layers.{i}.sa_qpos_proj.bias''')) rename_keys.append((F'''transformer.decoder.layers.{i}.sa_kpos_proj.bias''', F'''decoder.layers.{i}.sa_kpos_proj.bias''')) rename_keys.append((F'''transformer.decoder.layers.{i}.sa_v_proj.bias''', F'''decoder.layers.{i}.sa_v_proj.bias''')) rename_keys.append( (F'''transformer.decoder.layers.{i}.ca_qcontent_proj.bias''', F'''decoder.layers.{i}.ca_qcontent_proj.bias''') ) # rename_keys.append((f"transformer.decoder.layers.{i}.ca_qpos_proj.bias", f"decoder.layers.{i}.ca_qpos_proj.bias")) rename_keys.append( (F'''transformer.decoder.layers.{i}.ca_kcontent_proj.bias''', F'''decoder.layers.{i}.ca_kcontent_proj.bias''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.ca_kpos_proj.bias''', F'''decoder.layers.{i}.ca_kpos_proj.bias''')) rename_keys.append((F'''transformer.decoder.layers.{i}.ca_v_proj.bias''', F'''decoder.layers.{i}.ca_v_proj.bias''')) rename_keys.append( (F'''transformer.decoder.layers.{i}.ca_qpos_sine_proj.bias''', F'''decoder.layers.{i}.ca_qpos_sine_proj.bias''') ) # convolutional projection + query embeddings + layernorm of decoder + class and bounding box heads # for conditional DETR, also convert reference point head and query scale MLP rename_keys.extend( [ ('''input_proj.weight''', '''input_projection.weight'''), ('''input_proj.bias''', '''input_projection.bias'''), ('''query_embed.weight''', '''query_position_embeddings.weight'''), ('''transformer.decoder.norm.weight''', '''decoder.layernorm.weight'''), ('''transformer.decoder.norm.bias''', '''decoder.layernorm.bias'''), ('''class_embed.weight''', '''class_labels_classifier.weight'''), ('''class_embed.bias''', '''class_labels_classifier.bias'''), ('''bbox_embed.layers.0.weight''', '''bbox_predictor.layers.0.weight'''), ('''bbox_embed.layers.0.bias''', '''bbox_predictor.layers.0.bias'''), ('''bbox_embed.layers.1.weight''', '''bbox_predictor.layers.1.weight'''), ('''bbox_embed.layers.1.bias''', '''bbox_predictor.layers.1.bias'''), ('''bbox_embed.layers.2.weight''', '''bbox_predictor.layers.2.weight'''), ('''bbox_embed.layers.2.bias''', '''bbox_predictor.layers.2.bias'''), ('''transformer.decoder.ref_point_head.layers.0.weight''', '''decoder.ref_point_head.layers.0.weight'''), ('''transformer.decoder.ref_point_head.layers.0.bias''', '''decoder.ref_point_head.layers.0.bias'''), ('''transformer.decoder.ref_point_head.layers.1.weight''', '''decoder.ref_point_head.layers.1.weight'''), ('''transformer.decoder.ref_point_head.layers.1.bias''', '''decoder.ref_point_head.layers.1.bias'''), ('''transformer.decoder.query_scale.layers.0.weight''', '''decoder.query_scale.layers.0.weight'''), ('''transformer.decoder.query_scale.layers.0.bias''', '''decoder.query_scale.layers.0.bias'''), ('''transformer.decoder.query_scale.layers.1.weight''', '''decoder.query_scale.layers.1.weight'''), ('''transformer.decoder.query_scale.layers.1.bias''', '''decoder.query_scale.layers.1.bias'''), ('''transformer.decoder.layers.0.ca_qpos_proj.weight''', '''decoder.layers.0.ca_qpos_proj.weight'''), ('''transformer.decoder.layers.0.ca_qpos_proj.bias''', '''decoder.layers.0.ca_qpos_proj.bias'''), ] ) def UpperCamelCase__ ( A__ , A__ , A__ ) -> List[str]: snake_case__ : int = state_dict.pop(A__ ) snake_case__ : Union[str, Any] = val def UpperCamelCase__ ( A__ ) -> int: snake_case__ : List[Any] = OrderedDict() for key, value in state_dict.items(): if "backbone.0.body" in key: snake_case__ : Any = key.replace('backbone.0.body' , 'backbone.conv_encoder.model' ) snake_case__ : Optional[int] = value else: snake_case__ : Optional[int] = value return new_state_dict def UpperCamelCase__ ( A__ , A__=False ) -> Optional[int]: snake_case__ : Optional[int] = '' if is_panoptic: snake_case__ : Tuple = 'conditional_detr.' # first: transformer encoder for i in range(6 ): # read in weights + bias of input projection layer (in PyTorch's MultiHeadAttention, this is a single matrix + bias) snake_case__ : int = state_dict.pop(F"""{prefix}transformer.encoder.layers.{i}.self_attn.in_proj_weight""" ) snake_case__ : str = state_dict.pop(F"""{prefix}transformer.encoder.layers.{i}.self_attn.in_proj_bias""" ) # next, add query, keys and values (in that order) to the state dict snake_case__ : Union[str, Any] = in_proj_weight[:256, :] snake_case__ : Union[str, Any] = in_proj_bias[:256] snake_case__ : Union[str, Any] = in_proj_weight[256:512, :] snake_case__ : Optional[Any] = in_proj_bias[256:512] snake_case__ : List[str] = in_proj_weight[-256:, :] snake_case__ : Tuple = in_proj_bias[-256:] def UpperCamelCase__ ( ) -> Tuple: snake_case__ : int = 'http://images.cocodataset.org/val2017/000000039769.jpg' snake_case__ : str = Image.open(requests.get(A__ , stream=A__ ).raw ) return im @torch.no_grad() def UpperCamelCase__ ( A__ , A__ ) -> str: snake_case__ : List[Any] = ConditionalDetrConfig() # set backbone and dilation attributes if "resnet101" in model_name: snake_case__ : Any = 'resnet101' if "dc5" in model_name: snake_case__ : Any = True snake_case__ : int = 'panoptic' in model_name if is_panoptic: snake_case__ : str = 250 else: snake_case__ : Union[str, Any] = 91 snake_case__ : Optional[int] = 'huggingface/label-files' snake_case__ : Optional[Any] = 'coco-detection-id2label.json' snake_case__ : str = json.load(open(hf_hub_download(A__ , A__ , repo_type='dataset' ) , 'r' ) ) snake_case__ : List[Any] = {int(A__ ): v for k, v in idalabel.items()} snake_case__ : Any = idalabel snake_case__ : int = {v: k for k, v in idalabel.items()} # load image processor snake_case__ : List[Any] = 'coco_panoptic' if is_panoptic else 'coco_detection' snake_case__ : List[Any] = ConditionalDetrImageProcessor(format=A__ ) # prepare image snake_case__ : List[str] = prepare_img() snake_case__ : Any = image_processor(images=A__ , return_tensors='pt' ) snake_case__ : Dict = encoding['pixel_values'] logger.info(F"""Converting model {model_name}...""" ) # load original model from torch hub snake_case__ : Any = torch.hub.load('DeppMeng/ConditionalDETR' , A__ , pretrained=A__ ).eval() snake_case__ : Tuple = conditional_detr.state_dict() # rename keys for src, dest in rename_keys: if is_panoptic: snake_case__ : List[Any] = 'conditional_detr.' + src rename_key(A__ , A__ , A__ ) snake_case__ : Dict = rename_backbone_keys(A__ ) # query, key and value matrices need special treatment read_in_q_k_v(A__ , is_panoptic=A__ ) # important: we need to prepend a prefix to each of the base model keys as the head models use different attributes for them snake_case__ : Optional[int] = 'conditional_detr.model.' if is_panoptic else 'model.' for key in state_dict.copy().keys(): if is_panoptic: if ( key.startswith('conditional_detr' ) and not key.startswith('class_labels_classifier' ) and not key.startswith('bbox_predictor' ) ): snake_case__ : List[Any] = state_dict.pop(A__ ) snake_case__ : Optional[int] = val elif "class_labels_classifier" in key or "bbox_predictor" in key: snake_case__ : str = state_dict.pop(A__ ) snake_case__ : List[Any] = val elif key.startswith('bbox_attention' ) or key.startswith('mask_head' ): continue else: snake_case__ : Union[str, Any] = state_dict.pop(A__ ) snake_case__ : Dict = val else: if not key.startswith('class_labels_classifier' ) and not key.startswith('bbox_predictor' ): snake_case__ : List[Any] = state_dict.pop(A__ ) snake_case__ : Optional[int] = val # finally, create HuggingFace model and load state dict snake_case__ : Union[str, Any] = ConditionalDetrForSegmentation(A__ ) if is_panoptic else ConditionalDetrForObjectDetection(A__ ) model.load_state_dict(A__ ) model.eval() model.push_to_hub(repo_id=A__ , organization='DepuMeng' , commit_message='Add model' ) # verify our conversion snake_case__ : Tuple = conditional_detr(A__ ) snake_case__ : str = model(A__ ) assert torch.allclose(outputs.logits , original_outputs['pred_logits'] , atol=1e-4 ) assert torch.allclose(outputs.pred_boxes , original_outputs['pred_boxes'] , atol=1e-4 ) if is_panoptic: assert torch.allclose(outputs.pred_masks , original_outputs['pred_masks'] , atol=1e-4 ) # Save model and image processor logger.info(F"""Saving PyTorch model and image processor to {pytorch_dump_folder_path}...""" ) Path(A__ ).mkdir(exist_ok=A__ ) model.save_pretrained(A__ ) image_processor.save_pretrained(A__ ) if __name__ == "__main__": lowerCAmelCase__ : Any = argparse.ArgumentParser() parser.add_argument( '''--model_name''', default='''conditional_detr_resnet50''', type=str, help='''Name of the CONDITIONAL_DETR model you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the folder to output PyTorch model.''' ) lowerCAmelCase__ : int = parser.parse_args() convert_conditional_detr_checkpoint(args.model_name, args.pytorch_dump_folder_path)
699
0
from typing import Callable, List, Optional, Tuple, Union import torch from transformers import CLIPTextModel, CLIPTokenizer from ...configuration_utils import ConfigMixin, register_to_config from ...models import ModelMixin, TransformeraDModel, VQModel from ...schedulers import VQDiffusionScheduler from ...utils import logging from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput lowerCAmelCase__ : Optional[Any] = logging.get_logger(__name__) # pylint: disable=invalid-name class __snake_case ( _UpperCamelCase ,_UpperCamelCase ): @register_to_config def __init__( self , __UpperCamelCase , __UpperCamelCase = None , __UpperCamelCase = None ) -> Any: '''simple docstring''' super().__init__() snake_case__ : Any = learnable if self.learnable: assert hidden_size is not None, "learnable=True requires `hidden_size` to be set" assert length is not None, "learnable=True requires `length` to be set" snake_case__ : int = torch.zeros(__a , __a ) else: snake_case__ : List[str] = None snake_case__ : str = torch.nn.Parameter(__a ) class __snake_case ( _UpperCamelCase ): __lowerCamelCase = 42 __lowerCamelCase = 42 __lowerCamelCase = 42 __lowerCamelCase = 42 __lowerCamelCase = 42 __lowerCamelCase = 42 def __init__( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , ) -> List[Any]: '''simple docstring''' super().__init__() self.register_modules( vqvae=__a , transformer=__a , text_encoder=__a , tokenizer=__a , scheduler=__a , learned_classifier_free_sampling_embeddings=__a , ) def __a ( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> List[str]: '''simple docstring''' snake_case__ : int = len(__a ) if isinstance(__a , __a ) else 1 # get prompt text embeddings snake_case__ : Dict = self.tokenizer( __a , padding='max_length' , max_length=self.tokenizer.model_max_length , return_tensors='pt' , ) snake_case__ : Optional[int] = text_inputs.input_ids if text_input_ids.shape[-1] > self.tokenizer.model_max_length: snake_case__ : Optional[Any] = self.tokenizer.batch_decode(text_input_ids[:, self.tokenizer.model_max_length :] ) logger.warning( 'The following part of your input was truncated because CLIP can only handle sequences up to' F""" {self.tokenizer.model_max_length} tokens: {removed_text}""" ) snake_case__ : List[str] = text_input_ids[:, : self.tokenizer.model_max_length] snake_case__ : List[Any] = self.text_encoder(text_input_ids.to(self.device ) )[0] # NOTE: This additional step of normalizing the text embeddings is from VQ-Diffusion. # While CLIP does normalize the pooled output of the text transformer when combining # the image and text embeddings, CLIP does not directly normalize the last hidden state. # # CLIP normalizing the pooled output. # https://github.com/huggingface/transformers/blob/d92e22d1f28324f513f3080e5c47c071a3916721/src/transformers/models/clip/modeling_clip.py#L1052-L1053 snake_case__ : int = prompt_embeds / prompt_embeds.norm(dim=-1 , keepdim=__a ) # duplicate text embeddings for each generation per prompt snake_case__ : List[str] = prompt_embeds.repeat_interleave(__a , dim=0 ) if do_classifier_free_guidance: if self.learned_classifier_free_sampling_embeddings.learnable: snake_case__ : Any = self.learned_classifier_free_sampling_embeddings.embeddings snake_case__ : str = negative_prompt_embeds.unsqueeze(0 ).repeat(__a , 1 , 1 ) else: snake_case__ : Tuple = [""] * batch_size snake_case__ : Any = text_input_ids.shape[-1] snake_case__ : Optional[int] = self.tokenizer( __a , padding='max_length' , max_length=__a , truncation=__a , return_tensors='pt' , ) snake_case__ : Optional[int] = self.text_encoder(uncond_input.input_ids.to(self.device ) )[0] # See comment for normalizing text embeddings snake_case__ : Optional[int] = negative_prompt_embeds / negative_prompt_embeds.norm(dim=-1 , keepdim=__a ) # duplicate unconditional embeddings for each generation per prompt, using mps friendly method snake_case__ : Tuple = negative_prompt_embeds.shape[1] snake_case__ : Dict = negative_prompt_embeds.repeat(1 , __a , 1 ) snake_case__ : List[Any] = negative_prompt_embeds.view(batch_size * num_images_per_prompt , __a , -1 ) # For classifier free guidance, we need to do two forward passes. # Here we concatenate the unconditional and text embeddings into a single batch # to avoid doing two forward passes snake_case__ : str = torch.cat([negative_prompt_embeds, prompt_embeds] ) return prompt_embeds @torch.no_grad() def __call__( self , __UpperCamelCase , __UpperCamelCase = 100 , __UpperCamelCase = 5.0 , __UpperCamelCase = 1.0 , __UpperCamelCase = 1 , __UpperCamelCase = None , __UpperCamelCase = None , __UpperCamelCase = "pil" , __UpperCamelCase = True , __UpperCamelCase = None , __UpperCamelCase = 1 , ) -> Union[ImagePipelineOutput, Tuple]: '''simple docstring''' if isinstance(__a , __a ): snake_case__ : Tuple = 1 elif isinstance(__a , __a ): snake_case__ : List[Any] = len(__a ) else: raise ValueError(F"""`prompt` has to be of type `str` or `list` but is {type(__a )}""" ) snake_case__ : Optional[Any] = batch_size * num_images_per_prompt snake_case__ : Optional[int] = guidance_scale > 1.0 snake_case__ : int = self._encode_prompt(__a , __a , __a ) if (callback_steps is None) or ( callback_steps is not None and (not isinstance(__a , __a ) or callback_steps <= 0) ): raise ValueError( F"""`callback_steps` has to be a positive integer but is {callback_steps} of type""" F""" {type(__a )}.""" ) # get the initial completely masked latents unless the user supplied it snake_case__ : Union[str, Any] = (batch_size, self.transformer.num_latent_pixels) if latents is None: snake_case__ : Any = self.transformer.num_vector_embeds - 1 snake_case__ : List[str] = torch.full(__a , __a ).to(self.device ) else: if latents.shape != latents_shape: raise ValueError(F"""Unexpected latents shape, got {latents.shape}, expected {latents_shape}""" ) if (latents < 0).any() or (latents >= self.transformer.num_vector_embeds).any(): raise ValueError( 'Unexpected latents value(s). All latents be valid embedding indices i.e. in the range 0,' F""" {self.transformer.num_vector_embeds - 1} (inclusive).""" ) snake_case__ : List[Any] = latents.to(self.device ) # set timesteps self.scheduler.set_timesteps(__a , device=self.device ) snake_case__ : Dict = self.scheduler.timesteps.to(self.device ) snake_case__ : Dict = latents for i, t in enumerate(self.progress_bar(__a ) ): # expand the sample if we are doing classifier free guidance snake_case__ : int = torch.cat([sample] * 2 ) if do_classifier_free_guidance else sample # predict the un-noised image # model_output == `log_p_x_0` snake_case__ : str = self.transformer(__a , encoder_hidden_states=__a , timestep=__a ).sample if do_classifier_free_guidance: snake_case__ : Dict = model_output.chunk(2 ) snake_case__ : str = model_output_uncond + guidance_scale * (model_output_text - model_output_uncond) model_output -= torch.logsumexp(__a , dim=1 , keepdim=__a ) snake_case__ : Any = self.truncate(__a , __a ) # remove `log(0)`'s (`-inf`s) snake_case__ : int = model_output.clamp(-70 ) # compute the previous noisy sample x_t -> x_t-1 snake_case__ : Dict = self.scheduler.step(__a , timestep=__a , sample=__a , generator=__a ).prev_sample # call the callback, if provided if callback is not None and i % callback_steps == 0: callback(__a , __a , __a ) snake_case__ : Union[str, Any] = self.vqvae.config.vq_embed_dim snake_case__ : Dict = (batch_size, self.transformer.height, self.transformer.width, embedding_channels) snake_case__ : Tuple = self.vqvae.quantize.get_codebook_entry(__a , shape=__a ) snake_case__ : int = self.vqvae.decode(__a , force_not_quantize=__a ).sample snake_case__ : Tuple = (image / 2 + 0.5).clamp(0 , 1 ) snake_case__ : Optional[Any] = image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": snake_case__ : Dict = self.numpy_to_pil(__a ) if not return_dict: return (image,) return ImagePipelineOutput(images=__a ) def __a ( self , __UpperCamelCase , __UpperCamelCase ) -> torch.FloatTensor: '''simple docstring''' snake_case__ : str = torch.sort(__a , 1 , descending=__a ) snake_case__ : Union[str, Any] = torch.exp(__a ) snake_case__ : Any = sorted_p_x_0.cumsum(dim=1 ) < truncation_rate # Ensure that at least the largest probability is not zeroed out snake_case__ : Optional[int] = torch.full_like(keep_mask[:, 0:1, :] , __a ) snake_case__ : List[Any] = torch.cat((all_true, keep_mask) , dim=1 ) snake_case__ : List[str] = keep_mask[:, :-1, :] snake_case__ : Union[str, Any] = keep_mask.gather(1 , indices.argsort(1 ) ) snake_case__ : List[Any] = log_p_x_0.clone() snake_case__ : Tuple = -torch.inf # -inf = log(0) return rv
707
from collections import namedtuple lowerCAmelCase__ : Union[str, Any] = namedtuple('''from_to''', '''from_ to''') lowerCAmelCase__ : Tuple = { '''cubicmeter''': from_to(1, 1), '''litre''': from_to(0.0_01, 10_00), '''kilolitre''': from_to(1, 1), '''gallon''': from_to(0.0_04_54, 2_64.1_72), '''cubicyard''': from_to(0.7_64_55, 1.3_07_95), '''cubicfoot''': from_to(0.0_28, 35.31_47), '''cup''': from_to(0.0_00_23_65_88, 42_26.75), } def UpperCamelCase__ ( A__ , A__ , A__ ) -> float: if from_type not in METRIC_CONVERSION: raise ValueError( F"""Invalid 'from_type' value: {from_type!r} Supported values are:\n""" + ', '.join(A__ ) ) if to_type not in METRIC_CONVERSION: raise ValueError( F"""Invalid 'to_type' value: {to_type!r}. Supported values are:\n""" + ', '.join(A__ ) ) return value * METRIC_CONVERSION[from_type].from_ * METRIC_CONVERSION[to_type].to if __name__ == "__main__": import doctest doctest.testmod()
699
0
from collections.abc import Callable from math import pi, sqrt from random import uniform from statistics import mean def UpperCamelCase__ ( A__ ) -> Optional[int]: # A local function to see if a dot lands in the circle. def is_in_circle(A__ , A__ ) -> bool: snake_case__ : str = sqrt((x**2) + (y**2) ) # Our circle has a radius of 1, so a distance # greater than 1 would land outside the circle. return distance_from_centre <= 1 # The proportion of guesses that landed in the circle snake_case__ : Tuple = mean( int(is_in_circle(uniform(-1.0 , 1.0 ) , uniform(-1.0 , 1.0 ) ) ) for _ in range(__UpperCamelCase ) ) # The ratio of the area for circle to square is pi/4. snake_case__ : Dict = proportion * 4 print(F"""The estimated value of pi is {pi_estimate}""" ) print(F"""The numpy value of pi is {pi}""" ) print(F"""The total error is {abs(pi - pi_estimate )}""" ) def UpperCamelCase__ ( A__ , A__ , A__ = 0.0 , A__ = 1.0 , ) -> float: return mean( function_to_integrate(uniform(__UpperCamelCase , __UpperCamelCase ) ) for _ in range(__UpperCamelCase ) ) * (max_value - min_value) def UpperCamelCase__ ( A__ , A__ = 0.0 , A__ = 1.0 ) -> None: def identity_function(A__ ) -> float: return x snake_case__ : str = area_under_curve_estimator( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) snake_case__ : Optional[Any] = (max_value * max_value - min_value * min_value) / 2 print('******************' ) print(F"""Estimating area under y=x where x varies from {min_value} to {max_value}""" ) print(F"""Estimated value is {estimated_value}""" ) print(F"""Expected value is {expected_value}""" ) print(F"""Total error is {abs(estimated_value - expected_value )}""" ) print('******************' ) def UpperCamelCase__ ( A__ ) -> None: def function_to_integrate(A__ ) -> float: return sqrt(4.0 - x * x ) snake_case__ : int = area_under_curve_estimator( __UpperCamelCase , __UpperCamelCase , 0.0 , 2.0 ) print('******************' ) print('Estimating pi using area_under_curve_estimator' ) print(F"""Estimated value is {estimated_value}""" ) print(F"""Expected value is {pi}""" ) print(F"""Total error is {abs(estimated_value - pi )}""" ) print('******************' ) if __name__ == "__main__": import doctest doctest.testmod()
708
import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging lowerCAmelCase__ : Tuple = logging.get_logger(__name__) lowerCAmelCase__ : Union[str, Any] = '''▁''' lowerCAmelCase__ : List[Any] = {'''vocab_file''': '''sentencepiece.bpe.model'''} lowerCAmelCase__ : Optional[Any] = { '''vocab_file''': { '''facebook/xglm-564M''': '''https://huggingface.co/facebook/xglm-564M/resolve/main/sentencepiece.bpe.model''', } } lowerCAmelCase__ : str = { '''facebook/xglm-564M''': 20_48, } class __snake_case ( _lowerCamelCase ): __lowerCamelCase = VOCAB_FILES_NAMES __lowerCamelCase = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase = ["""input_ids""", """attention_mask"""] def __init__( self , __UpperCamelCase , __UpperCamelCase="<s>" , __UpperCamelCase="</s>" , __UpperCamelCase="</s>" , __UpperCamelCase="<s>" , __UpperCamelCase="<unk>" , __UpperCamelCase="<pad>" , __UpperCamelCase = None , **__UpperCamelCase , ) -> None: '''simple docstring''' snake_case__ : Any = {} if sp_model_kwargs is None else sp_model_kwargs # Compatibility with the original tokenizer snake_case__ : Tuple = 7 snake_case__ : Dict = [F"""<madeupword{i}>""" for i in range(self.num_madeup_words )] snake_case__ : Union[str, Any] = kwargs.get('additional_special_tokens' , [] ) kwargs["additional_special_tokens"] += [ word for word in madeup_words if word not in kwargs["additional_special_tokens"] ] super().__init__( bos_token=__UpperCamelCase , eos_token=__UpperCamelCase , unk_token=__UpperCamelCase , sep_token=__UpperCamelCase , cls_token=__UpperCamelCase , pad_token=__UpperCamelCase , sp_model_kwargs=self.sp_model_kwargs , **__UpperCamelCase , ) snake_case__ : Optional[int] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(__UpperCamelCase ) ) snake_case__ : Optional[Any] = vocab_file # Original fairseq vocab and spm vocab must be "aligned": # Vocab | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 # -------- | ------- | ------- | ------ | ------- | --- | --- | --- | ----- | ----- | ---- # fairseq | '<s>' | '<pad>' | '</s>' | '<unk>' | ',' | '.' | '▁' | 's' | '▁de' | '-' # spm | '<unk>' | '<s>' | '</s>' | ',' | '.' | '▁' | 's' | '▁de' | '-' | '▁a' # The first "real" token "," has position 4 in the original fairseq vocab and position 3 in the spm vocab snake_case__ : Tuple = 1 # Mimic fairseq token-to-id alignment for the first 4 token snake_case__ : Tuple = {'<s>': 0, '<pad>': 1, '</s>': 2, '<unk>': 3} snake_case__ : List[Any] = len(self.sp_model ) snake_case__ : Optional[Any] = {F"""<madeupword{i}>""": sp_size + i + self.fairseq_offset for i in range(self.num_madeup_words )} self.fairseq_tokens_to_ids.update(__UpperCamelCase ) snake_case__ : Union[str, Any] = {v: k for k, v in self.fairseq_tokens_to_ids.items()} def __getstate__( self ) -> List[Any]: '''simple docstring''' snake_case__ : Union[str, Any] = self.__dict__.copy() snake_case__ : Optional[Any] = None snake_case__ : Tuple = self.sp_model.serialized_model_proto() return state def __setstate__( self , __UpperCamelCase ) -> Union[str, Any]: '''simple docstring''' snake_case__ : Union[str, Any] = d # for backward compatibility if not hasattr(self , 'sp_model_kwargs' ): snake_case__ : Any = {} snake_case__ : Dict = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.LoadFromSerializedProto(self.sp_model_proto ) def __a ( self , __UpperCamelCase , __UpperCamelCase = None ) -> List[int]: '''simple docstring''' if token_ids_a is None: return [self.sep_token_id] + token_ids_a snake_case__ : str = [self.sep_token_id] return sep + token_ids_a + sep + sep + token_ids_a def __a ( self , __UpperCamelCase , __UpperCamelCase = None , __UpperCamelCase = 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 ) if token_ids_a is None: return [1] + ([0] * len(__UpperCamelCase )) return [1] + ([0] * len(__UpperCamelCase )) + [1, 1] + ([0] * len(__UpperCamelCase )) def __a ( self , __UpperCamelCase , __UpperCamelCase = None ) -> List[int]: '''simple docstring''' snake_case__ : int = [self.sep_token_id] if token_ids_a is None: return len(sep + token_ids_a ) * [0] return len(sep + token_ids_a + sep + sep + token_ids_a ) * [0] @property def __a ( self ) -> Tuple: '''simple docstring''' return len(self.sp_model ) + self.fairseq_offset + self.num_madeup_words def __a ( self ) -> Union[str, Any]: '''simple docstring''' snake_case__ : int = {self.convert_ids_to_tokens(__UpperCamelCase ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __a ( self , __UpperCamelCase ) -> List[str]: '''simple docstring''' return self.sp_model.encode(__UpperCamelCase , out_type=__UpperCamelCase ) def __a ( self , __UpperCamelCase ) -> Union[str, Any]: '''simple docstring''' if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] snake_case__ : Optional[Any] = self.sp_model.PieceToId(__UpperCamelCase ) # Need to return unknown token if the SP model returned 0 return spm_id + self.fairseq_offset if spm_id else self.unk_token_id def __a ( self , __UpperCamelCase ) -> Dict: '''simple docstring''' if index in self.fairseq_ids_to_tokens: return self.fairseq_ids_to_tokens[index] return self.sp_model.IdToPiece(index - self.fairseq_offset ) def __a ( self , __UpperCamelCase ) -> int: '''simple docstring''' snake_case__ : int = ''.join(__UpperCamelCase ).replace(__UpperCamelCase , ' ' ).strip() return out_string def __a ( self , __UpperCamelCase , __UpperCamelCase = 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__ : List[str] = 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__ : Any = self.sp_model.serialized_model_proto() fi.write(__UpperCamelCase ) return (out_vocab_file,)
699
0
import argparse import torch from huggingface_hub import hf_hub_download from transformers import AutoTokenizer, RobertaPreLayerNormConfig, RobertaPreLayerNormForMaskedLM from transformers.utils import logging logging.set_verbosity_info() lowerCAmelCase__ : str = logging.get_logger(__name__) def UpperCamelCase__ ( A__ , A__ ) -> Any: snake_case__ : Tuple = RobertaPreLayerNormConfig.from_pretrained( A__ , architectures=['RobertaPreLayerNormForMaskedLM'] ) # convert state_dict snake_case__ : str = torch.load(hf_hub_download(repo_id=A__ , filename='pytorch_model.bin' ) ) snake_case__ : List[Any] = {} for tensor_key, tensor_value in original_state_dict.items(): # The transformer implementation gives the model a unique name, rather than overwiriting 'roberta' if tensor_key.startswith('roberta.' ): snake_case__ : int = 'roberta_prelayernorm.' + tensor_key[len('roberta.' ) :] # The original implementation contains weights which are not used, remove them from the state_dict if tensor_key.endswith('.self.LayerNorm.weight' ) or tensor_key.endswith('.self.LayerNorm.bias' ): continue snake_case__ : List[Any] = tensor_value snake_case__ : Tuple = RobertaPreLayerNormForMaskedLM.from_pretrained( pretrained_model_name_or_path=A__ , config=A__ , state_dict=A__ ) model.save_pretrained(A__ ) # convert tokenizer snake_case__ : Optional[int] = AutoTokenizer.from_pretrained(A__ ) tokenizer.save_pretrained(A__ ) if __name__ == "__main__": lowerCAmelCase__ : List[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--checkpoint-repo''', default=None, type=str, required=True, help='''Path the official PyTorch dump, e.g. \'andreasmadsen/efficient_mlm_m0.40\'.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.''' ) lowerCAmelCase__ : Dict = parser.parse_args() convert_roberta_prelayernorm_checkpoint_to_pytorch(args.checkpoint_repo, args.pytorch_dump_folder_path)
709
import collections from typing import List, Optional, Union from ...tokenization_utils_base import BatchEncoding from ...utils import TensorType, add_end_docstrings, add_start_docstrings, logging from ..bert.tokenization_bert_fast import BertTokenizerFast from .tokenization_dpr import DPRContextEncoderTokenizer, DPRQuestionEncoderTokenizer, DPRReaderTokenizer lowerCAmelCase__ : Any = logging.get_logger(__name__) lowerCAmelCase__ : List[Any] = {'''vocab_file''': '''vocab.txt''', '''tokenizer_file''': '''tokenizer.json'''} lowerCAmelCase__ : Any = { '''vocab_file''': { '''facebook/dpr-ctx_encoder-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/vocab.txt''' ), '''facebook/dpr-ctx_encoder-multiset-base''': ( '''https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/vocab.txt''' ), }, '''tokenizer_file''': { '''facebook/dpr-ctx_encoder-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/tokenizer.json''' ), '''facebook/dpr-ctx_encoder-multiset-base''': ( '''https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/tokenizer.json''' ), }, } lowerCAmelCase__ : Any = { '''vocab_file''': { '''facebook/dpr-question_encoder-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/vocab.txt''' ), '''facebook/dpr-question_encoder-multiset-base''': ( '''https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/vocab.txt''' ), }, '''tokenizer_file''': { '''facebook/dpr-question_encoder-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/tokenizer.json''' ), '''facebook/dpr-question_encoder-multiset-base''': ( '''https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/tokenizer.json''' ), }, } lowerCAmelCase__ : Tuple = { '''vocab_file''': { '''facebook/dpr-reader-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/vocab.txt''' ), '''facebook/dpr-reader-multiset-base''': ( '''https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/vocab.txt''' ), }, '''tokenizer_file''': { '''facebook/dpr-reader-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/tokenizer.json''' ), '''facebook/dpr-reader-multiset-base''': ( '''https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/tokenizer.json''' ), }, } lowerCAmelCase__ : Dict = { '''facebook/dpr-ctx_encoder-single-nq-base''': 5_12, '''facebook/dpr-ctx_encoder-multiset-base''': 5_12, } lowerCAmelCase__ : Union[str, Any] = { '''facebook/dpr-question_encoder-single-nq-base''': 5_12, '''facebook/dpr-question_encoder-multiset-base''': 5_12, } lowerCAmelCase__ : Optional[Any] = { '''facebook/dpr-reader-single-nq-base''': 5_12, '''facebook/dpr-reader-multiset-base''': 5_12, } lowerCAmelCase__ : Tuple = { '''facebook/dpr-ctx_encoder-single-nq-base''': {'''do_lower_case''': True}, '''facebook/dpr-ctx_encoder-multiset-base''': {'''do_lower_case''': True}, } lowerCAmelCase__ : Any = { '''facebook/dpr-question_encoder-single-nq-base''': {'''do_lower_case''': True}, '''facebook/dpr-question_encoder-multiset-base''': {'''do_lower_case''': True}, } lowerCAmelCase__ : List[str] = { '''facebook/dpr-reader-single-nq-base''': {'''do_lower_case''': True}, '''facebook/dpr-reader-multiset-base''': {'''do_lower_case''': True}, } class __snake_case ( _lowerCamelCase ): __lowerCamelCase = VOCAB_FILES_NAMES __lowerCamelCase = CONTEXT_ENCODER_PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase = CONTEXT_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase = CONTEXT_ENCODER_PRETRAINED_INIT_CONFIGURATION __lowerCamelCase = DPRContextEncoderTokenizer class __snake_case ( _lowerCamelCase ): __lowerCamelCase = VOCAB_FILES_NAMES __lowerCamelCase = QUESTION_ENCODER_PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase = QUESTION_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase = QUESTION_ENCODER_PRETRAINED_INIT_CONFIGURATION __lowerCamelCase = DPRQuestionEncoderTokenizer lowerCAmelCase__ : Tuple = collections.namedtuple( '''DPRSpanPrediction''', ['''span_score''', '''relevance_score''', '''doc_id''', '''start_index''', '''end_index''', '''text'''] ) lowerCAmelCase__ : List[Any] = collections.namedtuple('''DPRReaderOutput''', ['''start_logits''', '''end_logits''', '''relevance_logits''']) lowerCAmelCase__ : int = r''' Return a dictionary with the token ids of the input strings and other information to give to `.decode_best_spans`. It converts the strings of a question and different passages (title and text) in a sequence of IDs (integers), using the tokenizer and vocabulary. The resulting `input_ids` is a matrix of size `(n_passages, sequence_length)` with the format: [CLS] <question token ids> [SEP] <titles ids> [SEP] <texts ids> Args: questions (`str` or `List[str]`): The questions to be encoded. You can specify one question for many passages. In this case, the question will be duplicated like `[questions] * n_passages`. Otherwise you have to specify as many questions as in `titles` or `texts`. titles (`str` or `List[str]`): The passages titles to be encoded. This can be a string or a list of strings if there are several passages. texts (`str` or `List[str]`): The passages texts to be encoded. This can be a string or a list of strings if there are several passages. padding (`bool`, `str` or [`~utils.PaddingStrategy`], *optional*, defaults to `False`): Activates and controls padding. Accepts the following values: - `True` or `\'longest\'`: Pad to the longest sequence in the batch (or no padding if only a single sequence if provided). - `\'max_length\'`: Pad to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. - `False` or `\'do_not_pad\'` (default): No padding (i.e., can output a batch with sequences of different lengths). truncation (`bool`, `str` or [`~tokenization_utils_base.TruncationStrategy`], *optional*, defaults to `False`): Activates and controls truncation. Accepts the following values: - `True` or `\'longest_first\'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. This will truncate token by token, removing a token from the longest sequence in the pair if a pair of sequences (or a batch of pairs) is provided. - `\'only_first\'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. This will only truncate the first sequence of a pair if a pair of sequences (or a batch of pairs) is provided. - `\'only_second\'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. This will only truncate the second sequence of a pair if a pair of sequences (or a batch of pairs) is provided. - `False` or `\'do_not_truncate\'` (default): No truncation (i.e., can output batch with sequence lengths greater than the model maximum admissible input size). max_length (`int`, *optional*): Controls the maximum length to use by one of the truncation/padding parameters. If left unset or set to `None`, this will use the predefined model maximum length if a maximum length is required by one of the truncation/padding parameters. If the model has no specific maximum input length (like XLNet) truncation/padding to a maximum length will be deactivated. return_tensors (`str` or [`~utils.TensorType`], *optional*): If set, will return tensors instead of list of python integers. Acceptable values are: - `\'tf\'`: Return TensorFlow `tf.constant` objects. - `\'pt\'`: Return PyTorch `torch.Tensor` objects. - `\'np\'`: Return Numpy `np.ndarray` objects. return_attention_mask (`bool`, *optional*): Whether or not to return the attention mask. If not set, will return the attention mask according to the specific tokenizer\'s default, defined by the `return_outputs` attribute. [What are attention masks?](../glossary#attention-mask) Return: `Dict[str, List[List[int]]]`: A dictionary with the following keys: - `input_ids`: List of token ids to be fed to a model. - `attention_mask`: List of indices specifying which tokens should be attended to by the model. ''' @add_start_docstrings(_lowerCamelCase ) class __snake_case : def __call__( self , __UpperCamelCase , __UpperCamelCase = None , __UpperCamelCase = None , __UpperCamelCase = False , __UpperCamelCase = False , __UpperCamelCase = None , __UpperCamelCase = None , __UpperCamelCase = None , **__UpperCamelCase , ) -> BatchEncoding: '''simple docstring''' if titles is None and texts is None: return super().__call__( __UpperCamelCase , padding=__UpperCamelCase , truncation=__UpperCamelCase , max_length=__UpperCamelCase , return_tensors=__UpperCamelCase , return_attention_mask=__UpperCamelCase , **__UpperCamelCase , ) elif titles is None or texts is None: snake_case__ : Optional[Any] = titles if texts is None else texts return super().__call__( __UpperCamelCase , __UpperCamelCase , padding=__UpperCamelCase , truncation=__UpperCamelCase , max_length=__UpperCamelCase , return_tensors=__UpperCamelCase , return_attention_mask=__UpperCamelCase , **__UpperCamelCase , ) snake_case__ : int = titles if not isinstance(__UpperCamelCase , __UpperCamelCase ) else [titles] snake_case__ : Optional[int] = texts if not isinstance(__UpperCamelCase , __UpperCamelCase ) else [texts] snake_case__ : List[Any] = len(__UpperCamelCase ) snake_case__ : str = questions if not isinstance(__UpperCamelCase , __UpperCamelCase ) else [questions] * n_passages assert len(__UpperCamelCase ) == len( __UpperCamelCase ), F"""There should be as many titles than texts but got {len(__UpperCamelCase )} titles and {len(__UpperCamelCase )} texts.""" snake_case__ : Optional[int] = super().__call__(__UpperCamelCase , __UpperCamelCase , padding=__UpperCamelCase , truncation=__UpperCamelCase )['input_ids'] snake_case__ : Optional[Any] = super().__call__(__UpperCamelCase , add_special_tokens=__UpperCamelCase , padding=__UpperCamelCase , truncation=__UpperCamelCase )['input_ids'] snake_case__ : Union[str, Any] = { 'input_ids': [ (encoded_question_and_title + encoded_text)[:max_length] if max_length is not None and truncation else encoded_question_and_title + encoded_text for encoded_question_and_title, encoded_text in zip(__UpperCamelCase , __UpperCamelCase ) ] } if return_attention_mask is not False: snake_case__ : List[Any] = [] for input_ids in encoded_inputs["input_ids"]: attention_mask.append([int(input_id != self.pad_token_id ) for input_id in input_ids] ) snake_case__ : Union[str, Any] = attention_mask return self.pad(__UpperCamelCase , padding=__UpperCamelCase , max_length=__UpperCamelCase , return_tensors=__UpperCamelCase ) def __a ( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase = 16 , __UpperCamelCase = 64 , __UpperCamelCase = 4 , ) -> List[DPRSpanPrediction]: '''simple docstring''' snake_case__ : Optional[Any] = reader_input['input_ids'] snake_case__ , snake_case__ , snake_case__ : Any = reader_output[:3] snake_case__ : List[str] = len(__UpperCamelCase ) snake_case__ : Tuple = sorted(range(__UpperCamelCase ) , reverse=__UpperCamelCase , key=relevance_logits.__getitem__ ) snake_case__ : List[DPRReaderOutput] = [] for doc_id in sorted_docs: snake_case__ : Tuple = list(input_ids[doc_id] ) # assuming question & title information is at the beginning of the sequence snake_case__ : int = sequence_ids.index(self.sep_token_id , 2 ) + 1 # second sep id if sequence_ids[-1] == self.pad_token_id: snake_case__ : Union[str, Any] = sequence_ids.index(self.pad_token_id ) else: snake_case__ : str = len(__UpperCamelCase ) snake_case__ : Dict = self._get_best_spans( start_logits=start_logits[doc_id][passage_offset:sequence_len] , end_logits=end_logits[doc_id][passage_offset:sequence_len] , max_answer_length=__UpperCamelCase , top_spans=__UpperCamelCase , ) for start_index, end_index in best_spans: start_index += passage_offset end_index += passage_offset nbest_spans_predictions.append( DPRSpanPrediction( span_score=start_logits[doc_id][start_index] + end_logits[doc_id][end_index] , relevance_score=relevance_logits[doc_id] , doc_id=__UpperCamelCase , start_index=__UpperCamelCase , end_index=__UpperCamelCase , text=self.decode(sequence_ids[start_index : end_index + 1] ) , ) ) if len(__UpperCamelCase ) >= num_spans: break return nbest_spans_predictions[:num_spans] def __a ( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , ) -> List[DPRSpanPrediction]: '''simple docstring''' snake_case__ : Any = [] for start_index, start_score in enumerate(__UpperCamelCase ): for answer_length, end_score in enumerate(end_logits[start_index : start_index + max_answer_length] ): scores.append(((start_index, start_index + answer_length), start_score + end_score) ) snake_case__ : str = sorted(__UpperCamelCase , key=lambda __UpperCamelCase : x[1] , reverse=__UpperCamelCase ) snake_case__ : Any = [] for (start_index, end_index), score in scores: assert start_index <= end_index, F"""Wrong span indices: [{start_index}:{end_index}]""" snake_case__ : str = end_index - start_index + 1 assert length <= max_answer_length, F"""Span is too long: {length} > {max_answer_length}""" if any( start_index <= prev_start_index <= prev_end_index <= end_index or prev_start_index <= start_index <= end_index <= prev_end_index for (prev_start_index, prev_end_index) in chosen_span_intervals ): continue chosen_span_intervals.append((start_index, end_index) ) if len(__UpperCamelCase ) == top_spans: break return chosen_span_intervals @add_end_docstrings(_lowerCamelCase ) class __snake_case ( _lowerCamelCase ,_lowerCamelCase ): __lowerCamelCase = VOCAB_FILES_NAMES __lowerCamelCase = READER_PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase = READER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase = READER_PRETRAINED_INIT_CONFIGURATION __lowerCamelCase = ["""input_ids""", """attention_mask"""] __lowerCamelCase = DPRReaderTokenizer
699
0
'''simple docstring''' import itertools import random import unittest import numpy as np from transformers import BatchFeature, SpeechTaFeatureExtractor from transformers.testing_utils import require_torch from transformers.utils.import_utils import is_torch_available from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin if is_torch_available(): import torch lowerCAmelCase__ : int = random.Random() def UpperCamelCase__ ( A__ , A__=1.0 , A__=None , A__=None ) -> str: if rng is None: snake_case__ : List[Any] = global_rng snake_case__ : Union[str, Any] = [] for batch_idx in range(shape[0] ): values.append([] ) for _ in range(shape[1] ): values[-1].append(rng.random() * scale ) return values @require_torch class __snake_case ( unittest.TestCase ): def __init__( self , __UpperCamelCase , __UpperCamelCase=7 , __UpperCamelCase=400 , __UpperCamelCase=2000 , __UpperCamelCase=1 , __UpperCamelCase=0.0 , __UpperCamelCase=16000 , __UpperCamelCase=True , __UpperCamelCase=80 , __UpperCamelCase=16 , __UpperCamelCase=64 , __UpperCamelCase="hann_window" , __UpperCamelCase=80 , __UpperCamelCase=7600 , __UpperCamelCase=1E-10 , __UpperCamelCase=True , ) -> List[str]: '''simple docstring''' snake_case__ : Optional[Any] = parent snake_case__ : Union[str, Any] = batch_size snake_case__ : int = min_seq_length snake_case__ : Tuple = max_seq_length snake_case__ : List[Any] = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) snake_case__ : Any = feature_size snake_case__ : List[Any] = padding_value snake_case__ : Tuple = sampling_rate snake_case__ : int = do_normalize snake_case__ : str = num_mel_bins snake_case__ : List[str] = hop_length snake_case__ : Tuple = win_length snake_case__ : List[str] = win_function snake_case__ : List[str] = fmin snake_case__ : Dict = fmax snake_case__ : Tuple = mel_floor snake_case__ : Tuple = return_attention_mask def __a ( self ) -> Tuple: '''simple docstring''' return { "feature_size": self.feature_size, "padding_value": self.padding_value, "sampling_rate": self.sampling_rate, "do_normalize": self.do_normalize, "num_mel_bins": self.num_mel_bins, "hop_length": self.hop_length, "win_length": self.win_length, "win_function": self.win_function, "fmin": self.fmin, "fmax": self.fmax, "mel_floor": self.mel_floor, "return_attention_mask": self.return_attention_mask, } def __a ( self , __UpperCamelCase=False , __UpperCamelCase=False ) -> Optional[int]: '''simple docstring''' def _flatten(__UpperCamelCase ): return list(itertools.chain(*_a ) ) if equal_length: snake_case__ : List[Any] = floats_list((self.batch_size, self.max_seq_length) ) else: # make sure that inputs increase in size snake_case__ : List[str] = [ _flatten(floats_list((x, self.feature_size) ) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: snake_case__ : int = [np.asarray(_a ) for x in speech_inputs] return speech_inputs def __a ( self , __UpperCamelCase=False , __UpperCamelCase=False ) -> str: '''simple docstring''' if equal_length: snake_case__ : int = [floats_list((self.max_seq_length, self.num_mel_bins) ) for _ in range(self.batch_size )] else: # make sure that inputs increase in size snake_case__ : str = [ floats_list((x, self.num_mel_bins) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: snake_case__ : List[str] = [np.asarray(_a ) for x in speech_inputs] return speech_inputs @require_torch class __snake_case ( UpperCamelCase__ ,unittest.TestCase ): __lowerCamelCase = SpeechTaFeatureExtractor def __a ( self ) -> Union[str, Any]: '''simple docstring''' snake_case__ : Any = SpeechTaFeatureExtractionTester(self ) def __a ( self , __UpperCamelCase ) -> Union[str, Any]: '''simple docstring''' self.assertTrue(np.all(np.mean(_a , axis=0 ) < 1E-3 ) ) self.assertTrue(np.all(np.abs(np.var(_a , axis=0 ) - 1 ) < 1E-3 ) ) def __a ( self ) -> int: '''simple docstring''' snake_case__ : List[Any] = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 snake_case__ : int = [floats_list((1, x) )[0] for x in range(800 , 1400 , 200 )] snake_case__ : int = [np.asarray(_a ) for speech_input in speech_inputs] # Test not batched input snake_case__ : List[Any] = feat_extract(speech_inputs[0] , return_tensors='np' ).input_values snake_case__ : Dict = feat_extract(np_speech_inputs[0] , return_tensors='np' ).input_values self.assertTrue(np.allclose(_a , _a , atol=1E-3 ) ) # Test batched snake_case__ : List[str] = feat_extract(_a , return_tensors='np' ).input_values snake_case__ : List[str] = feat_extract(_a , return_tensors='np' ).input_values for enc_seq_a, enc_seq_a in zip(_a , _a ): self.assertTrue(np.allclose(_a , _a , atol=1E-3 ) ) def __a ( self ) -> Dict: '''simple docstring''' snake_case__ : str = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) snake_case__ : Tuple = [floats_list((1, x) )[0] for x in range(800 , 1400 , 200 )] snake_case__ : Any = ["""longest""", """max_length""", """do_not_pad"""] snake_case__ : Union[str, Any] = [None, 1600, None] for max_length, padding in zip(_a , _a ): snake_case__ : Optional[Any] = feat_extract(_a , padding=_a , max_length=_a , return_tensors='np' ) snake_case__ : List[str] = processed.input_values self._check_zero_mean_unit_variance(input_values[0][:800] ) self.assertTrue(input_values[0][800:].sum() < 1E-6 ) self._check_zero_mean_unit_variance(input_values[1][:1000] ) self.assertTrue(input_values[0][1000:].sum() < 1E-6 ) self._check_zero_mean_unit_variance(input_values[2][:1200] ) def __a ( self ) -> Union[str, Any]: '''simple docstring''' snake_case__ : List[Any] = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) snake_case__ : List[str] = range(800 , 1400 , 200 ) snake_case__ : Any = [floats_list((1, x) )[0] for x in lengths] snake_case__ : List[Any] = ["""longest""", """max_length""", """do_not_pad"""] snake_case__ : Optional[Any] = [None, 1600, None] for max_length, padding in zip(_a , _a ): snake_case__ : Optional[Any] = feat_extract(_a , max_length=_a , padding=_a ) snake_case__ : int = processed.input_values self._check_zero_mean_unit_variance(input_values[0][:800] ) self._check_zero_mean_unit_variance(input_values[1][:1000] ) self._check_zero_mean_unit_variance(input_values[2][:1200] ) def __a ( self ) -> Tuple: '''simple docstring''' snake_case__ : Tuple = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) snake_case__ : str = [floats_list((1, x) )[0] for x in range(800 , 1400 , 200 )] snake_case__ : Any = feat_extract( _a , truncation=_a , max_length=1000 , padding='max_length' , return_tensors='np' ) snake_case__ : Optional[Any] = processed.input_values self._check_zero_mean_unit_variance(input_values[0, :800] ) self._check_zero_mean_unit_variance(input_values[1] ) self._check_zero_mean_unit_variance(input_values[2] ) def __a ( self ) -> Optional[int]: '''simple docstring''' snake_case__ : Union[str, Any] = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) snake_case__ : Dict = [floats_list((1, x) )[0] for x in range(800 , 1400 , 200 )] snake_case__ : str = feat_extract( _a , truncation=_a , max_length=1000 , padding='longest' , return_tensors='np' ) snake_case__ : Any = processed.input_values self._check_zero_mean_unit_variance(input_values[0, :800] ) self._check_zero_mean_unit_variance(input_values[1, :1000] ) self._check_zero_mean_unit_variance(input_values[2] ) # make sure that if max_length < longest -> then pad to max_length self.assertTrue(input_values.shape == (3, 1000) ) snake_case__ : List[Any] = [floats_list((1, x) )[0] for x in range(800 , 1400 , 200 )] snake_case__ : Dict = feat_extract( _a , truncation=_a , max_length=2000 , padding='longest' , return_tensors='np' ) snake_case__ : List[str] = processed.input_values self._check_zero_mean_unit_variance(input_values[0, :800] ) self._check_zero_mean_unit_variance(input_values[1, :1000] ) self._check_zero_mean_unit_variance(input_values[2] ) # make sure that if max_length > longest -> then pad to longest self.assertTrue(input_values.shape == (3, 1200) ) def __a ( self ) -> int: '''simple docstring''' snake_case__ : Dict = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) snake_case__ : Any = np.random.rand(100 ).astype(np.floataa ) snake_case__ : str = np_speech_inputs.tolist() for inputs in [py_speech_inputs, np_speech_inputs]: snake_case__ : List[Any] = feature_extractor.pad([{'input_values': inputs}] , return_tensors='np' ) self.assertTrue(np_processed.input_values.dtype == np.floataa ) snake_case__ : Optional[Any] = feature_extractor.pad([{'input_values': inputs}] , return_tensors='pt' ) self.assertTrue(pt_processed.input_values.dtype == torch.floataa ) def __a ( self ) -> Any: '''simple docstring''' snake_case__ : Any = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 snake_case__ : Dict = [floats_list((1, x) )[0] for x in range(800 , 1400 , 200 )] snake_case__ : Optional[Any] = [np.asarray(_a ) for speech_input in speech_inputs] # Test feature size snake_case__ : Dict = feature_extractor(audio_target=_a , padding=_a , return_tensors='np' ).input_values self.assertTrue(input_values.ndim == 3 ) self.assertTrue(input_values.shape[-1] == feature_extractor.num_mel_bins ) # Test not batched input snake_case__ : Any = feature_extractor(speech_inputs[0] , return_tensors='np' ).input_values snake_case__ : List[str] = feature_extractor(np_speech_inputs[0] , return_tensors='np' ).input_values self.assertTrue(np.allclose(_a , _a , atol=1E-3 ) ) # Test batched snake_case__ : Dict = feature_extractor(_a , return_tensors='np' ).input_values snake_case__ : Optional[int] = feature_extractor(_a , return_tensors='np' ).input_values for enc_seq_a, enc_seq_a in zip(_a , _a ): self.assertTrue(np.allclose(_a , _a , atol=1E-3 ) ) # Test 2-D numpy arrays are batched. snake_case__ : Tuple = [floats_list((1, x) )[0] for x in (800, 800, 800)] snake_case__ : Tuple = np.asarray(_a ) snake_case__ : Union[str, Any] = feature_extractor(_a , return_tensors='np' ).input_values snake_case__ : Optional[int] = feature_extractor(_a , return_tensors='np' ).input_values for enc_seq_a, enc_seq_a in zip(_a , _a ): self.assertTrue(np.allclose(_a , _a , atol=1E-3 ) ) def __a ( self ) -> Tuple: '''simple docstring''' snake_case__ : List[Any] = self.feat_extract_tester.prepare_inputs_for_target() snake_case__ : Optional[Any] = self.feature_extraction_class(**self.feat_extract_dict ) snake_case__ : List[Any] = feat_extract.model_input_names[0] snake_case__ : Union[str, Any] = BatchFeature({input_name: speech_inputs} ) self.assertTrue(all(len(_a ) == len(_a ) for x, y in zip(_a , processed_features[input_name] ) ) ) snake_case__ : Any = self.feat_extract_tester.prepare_inputs_for_target(equal_length=_a ) snake_case__ : Any = BatchFeature({input_name: speech_inputs} , tensor_type='np' ) snake_case__ : Tuple = processed_features[input_name] if len(batch_features_input.shape ) < 3: snake_case__ : str = batch_features_input[:, :, None] self.assertTrue( batch_features_input.shape == (self.feat_extract_tester.batch_size, len(speech_inputs[0] ), self.feat_extract_tester.num_mel_bins) ) @require_torch def __a ( self ) -> List[Any]: '''simple docstring''' snake_case__ : Any = self.feat_extract_tester.prepare_inputs_for_target(equal_length=_a ) snake_case__ : Union[str, Any] = self.feature_extraction_class(**self.feat_extract_dict ) snake_case__ : Any = feat_extract.model_input_names[0] snake_case__ : Union[str, Any] = BatchFeature({input_name: speech_inputs} , tensor_type='pt' ) snake_case__ : int = processed_features[input_name] if len(batch_features_input.shape ) < 3: snake_case__ : int = batch_features_input[:, :, None] self.assertTrue( batch_features_input.shape == (self.feat_extract_tester.batch_size, len(speech_inputs[0] ), self.feat_extract_tester.num_mel_bins) ) @require_torch def __a ( self ) -> Any: '''simple docstring''' snake_case__ : Optional[Any] = self.feature_extraction_class(**self.feat_extract_dict ) snake_case__ : Any = self.feat_extract_tester.prepare_inputs_for_target() snake_case__ : Any = feat_extract.model_input_names[0] snake_case__ : Optional[Any] = BatchFeature({input_name: speech_inputs} ) snake_case__ : Dict = feat_extract.num_mel_bins # hack! snake_case__ : Union[str, Any] = feat_extract.pad(_a , padding='longest' , return_tensors='np' )[input_name] snake_case__ : Union[str, Any] = feat_extract.pad(_a , padding='longest' , return_tensors='pt' )[input_name] self.assertTrue(abs(input_np.astype(np.floataa ).sum() - input_pt.numpy().astype(np.floataa ).sum() ) < 1E-2 ) def __a ( self ) -> List[Any]: '''simple docstring''' snake_case__ : List[str] = self.feat_extract_dict snake_case__ : Any = True snake_case__ : List[Any] = self.feature_extraction_class(**_a ) snake_case__ : List[Any] = self.feat_extract_tester.prepare_inputs_for_target() snake_case__ : str = [len(_a ) for x in speech_inputs] snake_case__ : Dict = feat_extract.model_input_names[0] snake_case__ : str = BatchFeature({input_name: speech_inputs} ) snake_case__ : Tuple = feat_extract.num_mel_bins # hack! snake_case__ : Tuple = feat_extract.pad(_a , padding='longest' , return_tensors='np' ) self.assertIn('attention_mask' , _a ) self.assertListEqual(list(processed.attention_mask.shape ) , list(processed[input_name].shape[:2] ) ) self.assertListEqual(processed.attention_mask.sum(-1 ).tolist() , _a ) def __a ( self ) -> str: '''simple docstring''' snake_case__ : Union[str, Any] = self.feat_extract_dict snake_case__ : int = True snake_case__ : Optional[Any] = self.feature_extraction_class(**_a ) snake_case__ : Any = self.feat_extract_tester.prepare_inputs_for_target() snake_case__ : Dict = [len(_a ) for x in speech_inputs] snake_case__ : Tuple = feat_extract.model_input_names[0] snake_case__ : Optional[Any] = BatchFeature({input_name: speech_inputs} ) snake_case__ : str = min(_a ) snake_case__ : Any = feat_extract.num_mel_bins # hack! snake_case__ : Union[str, Any] = feat_extract.pad( _a , padding='max_length' , max_length=_a , truncation=_a , return_tensors='np' ) self.assertIn('attention_mask' , _a ) self.assertListEqual( list(processed_pad.attention_mask.shape ) , [processed_pad[input_name].shape[0], max_length] ) self.assertListEqual( processed_pad.attention_mask[:, :max_length].sum(-1 ).tolist() , [max_length for x in speech_inputs] ) def __a ( self , __UpperCamelCase ) -> Tuple: '''simple docstring''' from datasets import load_dataset snake_case__ : Optional[Any] = load_dataset('hf-internal-testing/librispeech_asr_dummy' , 'clean' , split='validation' ) # automatic decoding with librispeech snake_case__ : Optional[Any] = ds.sort('id' ).select(range(_a ) )[:num_samples]["""audio"""] return [x["array"] for x in speech_samples] def __a ( self ) -> Tuple: '''simple docstring''' snake_case__ : int = torch.tensor( [2.38_04E-03, 2.07_52E-03, 1.98_36E-03, 2.10_57E-03, 1.61_74E-03, 3.05_18E-04, 9.15_53E-05, 3.35_69E-04, 9.76_56E-04, 1.83_11E-03, 2.01_42E-03, 2.10_57E-03, 1.73_95E-03, 4.57_76E-04, -3.96_73E-04, 4.57_76E-04, 1.00_71E-03, 9.15_53E-05, 4.88_28E-04, 1.15_97E-03, 7.32_42E-04, 9.46_04E-04, 1.80_05E-03, 1.83_11E-03, 8.85_01E-04, 4.27_25E-04, 4.88_28E-04, 7.32_42E-04, 1.09_86E-03, 2.10_57E-03] ) # fmt: on snake_case__ : List[str] = self._load_datasamples(1 ) snake_case__ : Optional[Any] = SpeechTaFeatureExtractor() snake_case__ : Dict = feature_extractor(_a , return_tensors='pt' ).input_values self.assertEquals(input_values.shape , (1, 93680) ) self.assertTrue(torch.allclose(input_values[0, :30] , _a , atol=1E-6 ) ) def __a ( self ) -> Dict: '''simple docstring''' snake_case__ : Tuple = torch.tensor( [-2.6_8_7_0, -3.0_1_0_4, -3.1_3_5_6, -3.5_3_5_2, -3.0_0_4_4, -3.0_3_5_3, -3.4_7_1_9, -3.6_7_7_7, -3.1_5_2_0, -2.9_4_3_5, -2.6_5_5_3, -2.8_7_9_5, -2.9_9_4_4, -2.5_9_2_1, -3.0_2_7_9, -3.0_3_8_6, -3.0_8_6_4, -3.1_2_9_1, -3.2_3_5_3, -2.7_4_4_4, -2.6_8_3_1, -2.7_2_8_7, -3.1_7_6_1, -3.1_5_7_1, -3.2_7_2_6, -3.0_5_8_2, -3.1_0_0_7, -3.4_5_3_3, -3.4_6_9_5, -3.0_9_9_8] ) # fmt: on snake_case__ : Dict = self._load_datasamples(1 ) snake_case__ : Any = SpeechTaFeatureExtractor() snake_case__ : List[str] = feature_extractor(audio_target=_a , return_tensors='pt' ).input_values self.assertEquals(input_values.shape , (1, 366, 80) ) self.assertTrue(torch.allclose(input_values[0, 0, :30] , _a , atol=1E-4 ) )
710
import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, EulerAncestralDiscreteScheduler, LMSDiscreteScheduler, PNDMScheduler, StableDiffusionInstructPixaPixPipeline, UNetaDConditionModel, ) from diffusers.image_processor import VaeImageProcessor from diffusers.utils import floats_tensor, load_image, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..pipeline_params import ( IMAGE_TO_IMAGE_IMAGE_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS, ) from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class __snake_case ( _lowerCamelCase ,_lowerCamelCase ,_lowerCamelCase ,unittest.TestCase ): __lowerCamelCase = StableDiffusionInstructPixaPixPipeline __lowerCamelCase = TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {"""height""", """width""", """cross_attention_kwargs"""} __lowerCamelCase = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS __lowerCamelCase = IMAGE_TO_IMAGE_IMAGE_PARAMS __lowerCamelCase = IMAGE_TO_IMAGE_IMAGE_PARAMS def __a ( self ) -> List[Any]: '''simple docstring''' torch.manual_seed(0 ) snake_case__ : Tuple = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=8 , out_channels=4 , down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D') , up_block_types=('CrossAttnUpBlock2D', 'UpBlock2D') , cross_attention_dim=32 , ) snake_case__ : Any = PNDMScheduler(skip_prk_steps=__UpperCamelCase ) torch.manual_seed(0 ) snake_case__ : Dict = 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 ) snake_case__ : int = 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 , ) snake_case__ : Tuple = CLIPTextModel(__UpperCamelCase ) snake_case__ : Optional[int] = CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip' ) snake_case__ : Optional[int] = { 'unet': unet, 'scheduler': scheduler, 'vae': vae, 'text_encoder': text_encoder, 'tokenizer': tokenizer, 'safety_checker': None, 'feature_extractor': None, } return components def __a ( self , __UpperCamelCase , __UpperCamelCase=0 ) -> Dict: '''simple docstring''' snake_case__ : Optional[Any] = floats_tensor((1, 3, 32, 32) , rng=random.Random(__UpperCamelCase ) ).to(__UpperCamelCase ) snake_case__ : int = image.cpu().permute(0 , 2 , 3 , 1 )[0] snake_case__ : Union[str, Any] = Image.fromarray(np.uinta(__UpperCamelCase ) ).convert('RGB' ) if str(__UpperCamelCase ).startswith('mps' ): snake_case__ : str = torch.manual_seed(__UpperCamelCase ) else: snake_case__ : Dict = torch.Generator(device=__UpperCamelCase ).manual_seed(__UpperCamelCase ) snake_case__ : str = { 'prompt': 'A painting of a squirrel eating a burger', 'image': image, 'generator': generator, 'num_inference_steps': 2, 'guidance_scale': 6.0, 'image_guidance_scale': 1, 'output_type': 'numpy', } return inputs def __a ( self ) -> Tuple: '''simple docstring''' snake_case__ : Dict = 'cpu' # ensure determinism for the device-dependent torch.Generator snake_case__ : Optional[int] = self.get_dummy_components() snake_case__ : str = StableDiffusionInstructPixaPixPipeline(**__UpperCamelCase ) snake_case__ : Optional[int] = sd_pipe.to(__UpperCamelCase ) sd_pipe.set_progress_bar_config(disable=__UpperCamelCase ) snake_case__ : Tuple = self.get_dummy_inputs(__UpperCamelCase ) snake_case__ : List[str] = sd_pipe(**__UpperCamelCase ).images snake_case__ : List[str] = image[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) snake_case__ : str = np.array([0.7_5_2_6, 0.3_7_5_0, 0.4_5_4_7, 0.6_1_1_7, 0.5_8_6_6, 0.5_0_1_6, 0.4_3_2_7, 0.5_6_4_2, 0.4_8_1_5] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 def __a ( self ) -> Any: '''simple docstring''' snake_case__ : Dict = 'cpu' # ensure determinism for the device-dependent torch.Generator snake_case__ : Union[str, Any] = self.get_dummy_components() snake_case__ : Optional[Any] = StableDiffusionInstructPixaPixPipeline(**__UpperCamelCase ) snake_case__ : List[Any] = sd_pipe.to(__UpperCamelCase ) sd_pipe.set_progress_bar_config(disable=__UpperCamelCase ) snake_case__ : Union[str, Any] = self.get_dummy_inputs(__UpperCamelCase ) snake_case__ : List[str] = 'french fries' snake_case__ : Optional[Any] = sd_pipe(**__UpperCamelCase , negative_prompt=__UpperCamelCase ) snake_case__ : Union[str, Any] = output.images snake_case__ : Tuple = image[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) snake_case__ : Any = np.array([0.7_5_1_1, 0.3_6_4_2, 0.4_5_5_3, 0.6_2_3_6, 0.5_7_9_7, 0.5_0_1_3, 0.4_3_4_3, 0.5_6_1_1, 0.4_8_3_1] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 def __a ( self ) -> int: '''simple docstring''' snake_case__ : Optional[int] = 'cpu' # ensure determinism for the device-dependent torch.Generator snake_case__ : List[str] = self.get_dummy_components() snake_case__ : str = StableDiffusionInstructPixaPixPipeline(**__UpperCamelCase ) snake_case__ : str = sd_pipe.to(__UpperCamelCase ) sd_pipe.set_progress_bar_config(disable=__UpperCamelCase ) snake_case__ : Dict = self.get_dummy_inputs(__UpperCamelCase ) snake_case__ : Any = [inputs['prompt']] * 2 snake_case__ : Optional[int] = np.array(inputs['image'] ).astype(np.floataa ) / 2_5_5.0 snake_case__ : Optional[int] = torch.from_numpy(__UpperCamelCase ).unsqueeze(0 ).to(__UpperCamelCase ) snake_case__ : Any = image / 2 + 0.5 snake_case__ : Optional[Any] = image.permute(0 , 3 , 1 , 2 ) snake_case__ : List[Any] = image.repeat(2 , 1 , 1 , 1 ) snake_case__ : Optional[int] = sd_pipe(**__UpperCamelCase ).images snake_case__ : Union[str, Any] = image[-1, -3:, -3:, -1] assert image.shape == (2, 32, 32, 3) snake_case__ : List[Any] = np.array([0.5_8_1_2, 0.5_7_4_8, 0.5_2_2_2, 0.5_9_0_8, 0.5_6_9_5, 0.7_1_7_4, 0.6_8_0_4, 0.5_5_2_3, 0.5_5_7_9] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 def __a ( self ) -> Union[str, Any]: '''simple docstring''' snake_case__ : Optional[int] = 'cpu' # ensure determinism for the device-dependent torch.Generator snake_case__ : Optional[int] = self.get_dummy_components() snake_case__ : Tuple = EulerAncestralDiscreteScheduler( beta_start=0.0_0_0_8_5 , beta_end=0.0_1_2 , beta_schedule='scaled_linear' ) snake_case__ : int = StableDiffusionInstructPixaPixPipeline(**__UpperCamelCase ) snake_case__ : List[str] = sd_pipe.to(__UpperCamelCase ) sd_pipe.set_progress_bar_config(disable=__UpperCamelCase ) snake_case__ : str = self.get_dummy_inputs(__UpperCamelCase ) snake_case__ : Any = sd_pipe(**__UpperCamelCase ).images snake_case__ : int = image[0, -3:, -3:, -1] snake_case__ : Tuple = [round(__UpperCamelCase , 4 ) for x in image_slice.flatten().tolist()] print(','.join([str(__UpperCamelCase ) for x in slice] ) ) assert image.shape == (1, 32, 32, 3) snake_case__ : List[Any] = np.array([0.7_4_1_7, 0.3_8_4_2, 0.4_7_3_2, 0.5_7_7_6, 0.5_8_9_1, 0.5_1_3_9, 0.4_0_5_2, 0.5_6_7_3, 0.4_9_8_6] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 def __a ( self ) -> int: '''simple docstring''' super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) def __a ( self ) -> Any: '''simple docstring''' snake_case__ : Optional[int] = self.get_dummy_components() snake_case__ : int = StableDiffusionInstructPixaPixPipeline(**__UpperCamelCase ) snake_case__ : Union[str, Any] = VaeImageProcessor(do_resize=__UpperCamelCase , do_normalize=__UpperCamelCase ) snake_case__ : Optional[int] = pipe.to(__UpperCamelCase ) pipe.set_progress_bar_config(disable=__UpperCamelCase ) snake_case__ : Optional[Any] = pipe(**self.get_dummy_inputs_by_type(__UpperCamelCase , input_image_type='pt' ) )[0] snake_case__ : Union[str, Any] = components['vae'] snake_case__ : str = self.get_dummy_inputs_by_type(__UpperCamelCase , input_image_type='pt' ) for image_param in self.image_latents_params: if image_param in inputs.keys(): snake_case__ : List[str] = vae.encode(inputs[image_param] ).latent_dist.mode() snake_case__ : Dict = pipe(**__UpperCamelCase )[0] snake_case__ : str = np.abs(out - out_latents_inputs ).max() self.assertLess(__UpperCamelCase , 1E-4 , 'passing latents as image input generate different result from passing image' ) @slow @require_torch_gpu class __snake_case ( unittest.TestCase ): def __a ( self ) -> List[str]: '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def __a ( self , __UpperCamelCase=0 ) -> Dict: '''simple docstring''' snake_case__ : Optional[Any] = torch.manual_seed(__UpperCamelCase ) snake_case__ : List[str] = load_image( 'https://huggingface.co/datasets/diffusers/test-arrays/resolve/main/stable_diffusion_pix2pix/example.jpg' ) snake_case__ : int = { 'prompt': 'turn him into a cyborg', 'image': image, 'generator': generator, 'num_inference_steps': 3, 'guidance_scale': 7.5, 'image_guidance_scale': 1.0, 'output_type': 'numpy', } return inputs def __a ( self ) -> Dict: '''simple docstring''' snake_case__ : Any = StableDiffusionInstructPixaPixPipeline.from_pretrained( 'timbrooks/instruct-pix2pix' , safety_checker=__UpperCamelCase ) pipe.to(__UpperCamelCase ) pipe.set_progress_bar_config(disable=__UpperCamelCase ) pipe.enable_attention_slicing() snake_case__ : Tuple = self.get_inputs() snake_case__ : List[Any] = pipe(**__UpperCamelCase ).images snake_case__ : Union[str, Any] = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 512, 512, 3) snake_case__ : Dict = np.array([0.5_9_0_2, 0.6_0_1_5, 0.6_0_2_7, 0.5_9_8_3, 0.6_0_9_2, 0.6_0_6_1, 0.5_7_6_5, 0.5_7_8_5, 0.5_5_5_5] ) assert np.abs(expected_slice - image_slice ).max() < 1E-3 def __a ( self ) -> str: '''simple docstring''' snake_case__ : Any = StableDiffusionInstructPixaPixPipeline.from_pretrained( 'timbrooks/instruct-pix2pix' , safety_checker=__UpperCamelCase ) snake_case__ : Tuple = LMSDiscreteScheduler.from_config(pipe.scheduler.config ) pipe.to(__UpperCamelCase ) pipe.set_progress_bar_config(disable=__UpperCamelCase ) pipe.enable_attention_slicing() snake_case__ : Dict = self.get_inputs() snake_case__ : Dict = pipe(**__UpperCamelCase ).images snake_case__ : Union[str, Any] = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 512, 512, 3) snake_case__ : List[Any] = np.array([0.6_5_7_8, 0.6_8_1_7, 0.6_9_7_2, 0.6_7_6_1, 0.6_8_5_6, 0.6_9_1_6, 0.6_4_2_8, 0.6_5_1_6, 0.6_3_0_1] ) assert np.abs(expected_slice - image_slice ).max() < 1E-3 def __a ( self ) -> Optional[Any]: '''simple docstring''' snake_case__ : Tuple = StableDiffusionInstructPixaPixPipeline.from_pretrained( 'timbrooks/instruct-pix2pix' , safety_checker=__UpperCamelCase ) snake_case__ : Tuple = DDIMScheduler.from_config(pipe.scheduler.config ) pipe.to(__UpperCamelCase ) pipe.set_progress_bar_config(disable=__UpperCamelCase ) pipe.enable_attention_slicing() snake_case__ : Optional[int] = self.get_inputs() snake_case__ : Optional[int] = pipe(**__UpperCamelCase ).images snake_case__ : Tuple = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 512, 512, 3) snake_case__ : int = np.array([0.3_8_2_8, 0.3_8_3_4, 0.3_8_1_8, 0.3_7_9_2, 0.3_8_6_5, 0.3_7_5_2, 0.3_7_9_2, 0.3_8_4_7, 0.3_7_5_3] ) assert np.abs(expected_slice - image_slice ).max() < 1E-3 def __a ( self ) -> Union[str, Any]: '''simple docstring''' snake_case__ : int = 0 def callback_fn(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> None: snake_case__ : List[Any] = True nonlocal number_of_steps number_of_steps += 1 if step == 1: snake_case__ : Any = latents.detach().cpu().numpy() assert latents.shape == (1, 4, 64, 64) snake_case__ : int = latents[0, -3:, -3:, -1] snake_case__ : List[str] = np.array([-0.2_4_6_3, -0.4_6_4_4, -0.9_7_5_6, 1.5_1_7_6, 1.4_4_1_4, 0.7_8_6_6, 0.9_8_9_7, 0.8_5_2_1, 0.7_9_8_3] ) assert np.abs(latents_slice.flatten() - expected_slice ).max() < 5E-2 elif step == 2: snake_case__ : Dict = latents.detach().cpu().numpy() assert latents.shape == (1, 4, 64, 64) snake_case__ : Dict = latents[0, -3:, -3:, -1] snake_case__ : Optional[Any] = np.array([-0.2_6_4_4, -0.4_6_2_6, -0.9_6_5_3, 1.5_1_7_6, 1.4_5_5_1, 0.7_6_8_6, 0.9_8_0_5, 0.8_4_5_2, 0.8_1_1_5] ) assert np.abs(latents_slice.flatten() - expected_slice ).max() < 5E-2 snake_case__ : str = False snake_case__ : List[Any] = StableDiffusionInstructPixaPixPipeline.from_pretrained( 'timbrooks/instruct-pix2pix' , safety_checker=__UpperCamelCase , torch_dtype=torch.floataa ) snake_case__ : int = pipe.to(__UpperCamelCase ) pipe.set_progress_bar_config(disable=__UpperCamelCase ) pipe.enable_attention_slicing() snake_case__ : int = self.get_inputs() pipe(**__UpperCamelCase , callback=__UpperCamelCase , callback_steps=1 ) assert callback_fn.has_been_called assert number_of_steps == 3 def __a ( self ) -> Any: '''simple docstring''' torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats() snake_case__ : str = StableDiffusionInstructPixaPixPipeline.from_pretrained( 'timbrooks/instruct-pix2pix' , safety_checker=__UpperCamelCase , torch_dtype=torch.floataa ) snake_case__ : Dict = pipe.to(__UpperCamelCase ) pipe.set_progress_bar_config(disable=__UpperCamelCase ) pipe.enable_attention_slicing(1 ) pipe.enable_sequential_cpu_offload() snake_case__ : str = self.get_inputs() snake_case__ : Tuple = pipe(**__UpperCamelCase ) snake_case__ : List[Any] = torch.cuda.max_memory_allocated() # make sure that less than 2.2 GB is allocated assert mem_bytes < 2.2 * 10**9 def __a ( self ) -> int: '''simple docstring''' snake_case__ : int = self.get_inputs() # resize to resolution that is divisible by 8 but not 16 or 32 snake_case__ : Tuple = inputs['image'].resize((504, 504) ) snake_case__ : str = 'timbrooks/instruct-pix2pix' snake_case__ : Tuple = StableDiffusionInstructPixaPixPipeline.from_pretrained( __UpperCamelCase , safety_checker=__UpperCamelCase , ) pipe.to(__UpperCamelCase ) pipe.set_progress_bar_config(disable=__UpperCamelCase ) pipe.enable_attention_slicing() snake_case__ : str = pipe(**__UpperCamelCase ) snake_case__ : List[Any] = output.images[0] snake_case__ : List[Any] = image[255:258, 383:386, -1] assert image.shape == (504, 504, 3) snake_case__ : List[str] = np.array([0.2_7_2_6, 0.2_5_2_9, 0.2_6_6_4, 0.2_6_5_5, 0.2_6_4_1, 0.2_6_4_2, 0.2_5_9_1, 0.2_6_4_9, 0.2_5_9_0] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5E-3
699
0
from pickle import UnpicklingError import jax import jax.numpy as jnp import numpy as np from flax.serialization import from_bytes from flax.traverse_util import flatten_dict from ..utils import logging lowerCAmelCase__ : Optional[Any] = logging.get_logger(__name__) def UpperCamelCase__ ( A__ , A__ ) -> Any: try: with open(__UpperCamelCase , 'rb' ) as flax_state_f: snake_case__ : str = from_bytes(__UpperCamelCase , flax_state_f.read() ) except UnpicklingError as e: try: with open(__UpperCamelCase ) as f: if f.read().startswith('version' ): raise OSError( 'You seem to have cloned a repository without having git-lfs installed. Please' ' install git-lfs and run `git lfs install` followed by `git lfs pull` in the' ' folder you cloned.' ) else: raise ValueError from e except (UnicodeDecodeError, ValueError): raise EnvironmentError(F"""Unable to convert {model_file} to Flax deserializable object. """ ) return load_flax_weights_in_pytorch_model(__UpperCamelCase , __UpperCamelCase ) def UpperCamelCase__ ( A__ , A__ ) -> Optional[Any]: try: import torch # noqa: F401 except ImportError: logger.error( 'Loading Flax weights in PyTorch requires both PyTorch and Flax to be installed. Please see' ' https://pytorch.org/ and https://flax.readthedocs.io/en/latest/installation.html for installation' ' instructions.' ) raise # check if we have bf16 weights snake_case__ : Optional[Any] = flatten_dict(jax.tree_util.tree_map(lambda A__ : x.dtype == jnp.bfloataa , __UpperCamelCase ) ).values() if any(__UpperCamelCase ): # convert all weights to fp32 if they are bf16 since torch.from_numpy can-not handle bf16 # and bf16 is not fully supported in PT yet. logger.warning( 'Found ``bfloat16`` weights in Flax model. Casting all ``bfloat16`` weights to ``float32`` ' 'before loading those in PyTorch model.' ) snake_case__ : List[str] = jax.tree_util.tree_map( lambda A__ : params.astype(np.floataa ) if params.dtype == jnp.bfloataa else params , __UpperCamelCase ) snake_case__ : str = '''''' snake_case__ : Any = flatten_dict(__UpperCamelCase , sep='.' ) snake_case__ : List[Any] = pt_model.state_dict() # keep track of unexpected & missing keys snake_case__ : str = [] snake_case__ : Optional[int] = set(pt_model_dict.keys() ) for flax_key_tuple, flax_tensor in flax_state_dict.items(): snake_case__ : Union[str, Any] = flax_key_tuple.split('.' ) if flax_key_tuple_array[-1] == "kernel" and flax_tensor.ndim == 4: snake_case__ : Optional[int] = flax_key_tuple_array[:-1] + ['''weight'''] snake_case__ : Tuple = jnp.transpose(__UpperCamelCase , (3, 2, 0, 1) ) elif flax_key_tuple_array[-1] == "kernel": snake_case__ : Dict = flax_key_tuple_array[:-1] + ['''weight'''] snake_case__ : str = flax_tensor.T elif flax_key_tuple_array[-1] == "scale": snake_case__ : Optional[int] = flax_key_tuple_array[:-1] + ['''weight'''] if "time_embedding" not in flax_key_tuple_array: for i, flax_key_tuple_string in enumerate(__UpperCamelCase ): snake_case__ : Union[str, Any] = ( flax_key_tuple_string.replace('_0' , '.0' ) .replace('_1' , '.1' ) .replace('_2' , '.2' ) .replace('_3' , '.3' ) .replace('_4' , '.4' ) .replace('_5' , '.5' ) .replace('_6' , '.6' ) .replace('_7' , '.7' ) .replace('_8' , '.8' ) .replace('_9' , '.9' ) ) snake_case__ : str = '''.'''.join(__UpperCamelCase ) if flax_key in pt_model_dict: if flax_tensor.shape != pt_model_dict[flax_key].shape: raise ValueError( F"""Flax checkpoint seems to be incorrect. Weight {flax_key_tuple} was expected """ F"""to be of shape {pt_model_dict[flax_key].shape}, but is {flax_tensor.shape}.""" ) else: # add weight to pytorch dict snake_case__ : str = np.asarray(__UpperCamelCase ) if not isinstance(__UpperCamelCase , np.ndarray ) else flax_tensor snake_case__ : Dict = torch.from_numpy(__UpperCamelCase ) # remove from missing keys missing_keys.remove(__UpperCamelCase ) else: # weight is not expected by PyTorch model unexpected_keys.append(__UpperCamelCase ) pt_model.load_state_dict(__UpperCamelCase ) # re-transform missing_keys to list snake_case__ : str = list(__UpperCamelCase ) if len(__UpperCamelCase ) > 0: logger.warning( 'Some weights of the Flax model were not used when initializing the PyTorch model' F""" {pt_model.__class__.__name__}: {unexpected_keys}\n- This IS expected if you are initializing""" F""" {pt_model.__class__.__name__} from a Flax model trained on another task or with another architecture""" ' (e.g. initializing a BertForSequenceClassification model from a FlaxBertForPreTraining model).\n- This' F""" IS NOT expected if you are initializing {pt_model.__class__.__name__} from a Flax model that you expect""" ' to be exactly identical (e.g. initializing a BertForSequenceClassification model from a' ' FlaxBertForSequenceClassification model).' ) if len(__UpperCamelCase ) > 0: logger.warning( F"""Some weights of {pt_model.__class__.__name__} were not initialized from the Flax model and are newly""" F""" initialized: {missing_keys}\nYou should probably TRAIN this model on a down-stream task to be able to""" ' use it for predictions and inference.' ) return pt_model
711
from .data_collator import ( DataCollatorForLanguageModeling, DataCollatorForPermutationLanguageModeling, DataCollatorForSeqaSeq, DataCollatorForSOP, DataCollatorForTokenClassification, DataCollatorForWholeWordMask, DataCollatorWithPadding, DefaultDataCollator, default_data_collator, ) from .metrics import glue_compute_metrics, xnli_compute_metrics from .processors import ( DataProcessor, InputExample, InputFeatures, SingleSentenceClassificationProcessor, SquadExample, SquadFeatures, SquadVaProcessor, SquadVaProcessor, glue_convert_examples_to_features, glue_output_modes, glue_processors, glue_tasks_num_labels, squad_convert_examples_to_features, xnli_output_modes, xnli_processors, xnli_tasks_num_labels, )
699
0
from collections.abc import Generator from math import sin def UpperCamelCase__ ( A__ ) -> Union[str, Any]: if len(lowerCamelCase_ ) != 32: raise ValueError('Input must be of length 32' ) snake_case__ : List[str] = b'' for i in [3, 2, 1, 0]: little_endian += string_aa[8 * i : 8 * i + 8] return little_endian def UpperCamelCase__ ( A__ ) -> List[str]: if i < 0: raise ValueError('Input must be non-negative' ) snake_case__ : Tuple = format(lowerCamelCase_ , '08x' )[-8:] snake_case__ : List[Any] = b'' for i in [3, 2, 1, 0]: little_endian_hex += hex_rep[2 * i : 2 * i + 2].encode('utf-8' ) return little_endian_hex def UpperCamelCase__ ( A__ ) -> str: snake_case__ : Tuple = b'' for char in message: bit_string += format(lowerCamelCase_ , '08b' ).encode('utf-8' ) snake_case__ : List[Any] = format(len(lowerCamelCase_ ) , '064b' ).encode('utf-8' ) # Pad bit_string to a multiple of 512 chars bit_string += b"1" while len(lowerCamelCase_ ) % 512 != 448: bit_string += b"0" bit_string += to_little_endian(start_len[32:] ) + to_little_endian(start_len[:32] ) return bit_string def UpperCamelCase__ ( A__ ) -> List[str]: if len(lowerCamelCase_ ) % 512 != 0: raise ValueError('Input must have length that\'s a multiple of 512' ) for pos in range(0 , len(lowerCamelCase_ ) , 512 ): snake_case__ : List[Any] = bit_string[pos : pos + 512] snake_case__ : List[str] = [] for i in range(0 , 512 , 32 ): block_words.append(int(to_little_endian(block[i : i + 32] ) , 2 ) ) yield block_words def UpperCamelCase__ ( A__ ) -> Dict: if i < 0: raise ValueError('Input must be non-negative' ) snake_case__ : Any = format(lowerCamelCase_ , '032b' ) snake_case__ : Optional[Any] = '' for c in i_str: new_str += "1" if c == "0" else "0" return int(lowerCamelCase_ , 2 ) def UpperCamelCase__ ( A__ , A__ ) -> Union[str, Any]: return (a + b) % 2**32 def UpperCamelCase__ ( A__ , A__ ) -> Dict: if i < 0: raise ValueError('Input must be non-negative' ) if shift < 0: raise ValueError('Shift must be non-negative' ) return ((i << shift) ^ (i >> (32 - shift))) % 2**32 def UpperCamelCase__ ( A__ ) -> str: snake_case__ : Union[str, Any] = preprocess(lowerCamelCase_ ) snake_case__ : str = [int(2**32 * abs(sin(i + 1 ) ) ) for i in range(64 )] # Starting states snake_case__ : List[str] = 0x67_45_23_01 snake_case__ : Any = 0xEF_CD_AB_89 snake_case__ : int = 0x98_BA_DC_FE snake_case__ : Optional[Any] = 0x10_32_54_76 snake_case__ : int = [ 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, ] # Process bit string in chunks, each with 16 32-char words for block_words in get_block_words(lowerCamelCase_ ): snake_case__ : Optional[int] = aa snake_case__ : Optional[Any] = ba snake_case__ : Any = ca snake_case__ : Optional[Any] = da # Hash current chunk for i in range(64 ): if i <= 15: # f = (b & c) | (not_32(b) & d) # Alternate definition for f snake_case__ : Tuple = d ^ (b & (c ^ d)) snake_case__ : Optional[int] = i elif i <= 31: # f = (d & b) | (not_32(d) & c) # Alternate definition for f snake_case__ : Dict = c ^ (d & (b ^ c)) snake_case__ : Union[str, Any] = (5 * i + 1) % 16 elif i <= 47: snake_case__ : List[Any] = b ^ c ^ d snake_case__ : Dict = (3 * i + 5) % 16 else: snake_case__ : Union[str, Any] = c ^ (b | not_aa(lowerCamelCase_ )) snake_case__ : Any = (7 * i) % 16 snake_case__ : Tuple = (f + a + added_consts[i] + block_words[g]) % 2**32 snake_case__ : Tuple = d snake_case__ : int = c snake_case__ : List[Any] = b snake_case__ : int = sum_aa(lowerCamelCase_ , left_rotate_aa(lowerCamelCase_ , shift_amounts[i] ) ) # Add hashed chunk to running total snake_case__ : str = sum_aa(lowerCamelCase_ , lowerCamelCase_ ) snake_case__ : Dict = sum_aa(lowerCamelCase_ , lowerCamelCase_ ) snake_case__ : Optional[int] = sum_aa(lowerCamelCase_ , lowerCamelCase_ ) snake_case__ : Tuple = sum_aa(lowerCamelCase_ , lowerCamelCase_ ) snake_case__ : Any = reformat_hex(lowerCamelCase_ ) + reformat_hex(lowerCamelCase_ ) + reformat_hex(lowerCamelCase_ ) + reformat_hex(lowerCamelCase_ ) return digest if __name__ == "__main__": import doctest doctest.testmod()
712
from dataclasses import dataclass, field from typing import Optional from transformers import AutoConfig, AutoImageProcessor, AutoTokenizer, FlaxVisionEncoderDecoderModel, HfArgumentParser @dataclass class __snake_case : __lowerCamelCase = field( metadata={"""help""": """The output directory where the model will be written."""} ,) __lowerCamelCase = field( metadata={ """help""": ( """The encoder model checkpoint for weights initialization.""" """Don't set if you want to train an encoder model from scratch.""" ) } ,) __lowerCamelCase = field( metadata={ """help""": ( """The decoder model checkpoint for weights initialization.""" """Don't set if you want to train a decoder model from scratch.""" ) } ,) __lowerCamelCase = field( default=_lowerCamelCase ,metadata={"""help""": """Pretrained encoder config name or path if not the same as encoder_model_name"""} ) __lowerCamelCase = field( default=_lowerCamelCase ,metadata={"""help""": """Pretrained decoder config name or path if not the same as decoder_model_name"""} ) def UpperCamelCase__ ( ) -> Union[str, Any]: snake_case__ : str = HfArgumentParser((ModelArguments,) ) ((snake_case__) , ) : Dict = parser.parse_args_into_dataclasses() # Load pretrained model and tokenizer # Use explicit specified encoder config if model_args.encoder_config_name: snake_case__ : List[str] = AutoConfig.from_pretrained(model_args.encoder_config_name ) # Use pretrained encoder model's config else: snake_case__ : Optional[int] = AutoConfig.from_pretrained(model_args.encoder_model_name_or_path ) # Use explicit specified decoder config if model_args.decoder_config_name: snake_case__ : Optional[Any] = AutoConfig.from_pretrained(model_args.decoder_config_name ) # Use pretrained decoder model's config else: snake_case__ : List[str] = AutoConfig.from_pretrained(model_args.decoder_model_name_or_path ) # necessary for `from_encoder_decoder_pretrained` when `decoder_config` is passed snake_case__ : Any = True snake_case__ : Dict = True snake_case__ : Tuple = FlaxVisionEncoderDecoderModel.from_encoder_decoder_pretrained( encoder_pretrained_model_name_or_path=model_args.encoder_model_name_or_path , decoder_pretrained_model_name_or_path=model_args.decoder_model_name_or_path , encoder_config=A__ , decoder_config=A__ , ) # GPT2 only has bos/eos tokens but not decoder_start/pad tokens snake_case__ : Optional[Any] = decoder_config.decoder_start_token_id snake_case__ : Tuple = decoder_config.pad_token_id if decoder_start_token_id is None: snake_case__ : Optional[Any] = decoder_config.bos_token_id if pad_token_id is None: snake_case__ : int = decoder_config.eos_token_id # This is necessary to make Flax's generate() work snake_case__ : Union[str, Any] = decoder_config.eos_token_id snake_case__ : Optional[int] = decoder_start_token_id snake_case__ : int = pad_token_id snake_case__ : Tuple = AutoImageProcessor.from_pretrained(model_args.encoder_model_name_or_path ) snake_case__ : int = AutoTokenizer.from_pretrained(model_args.decoder_model_name_or_path ) snake_case__ : Tuple = tokenizer.convert_ids_to_tokens(model.config.pad_token_id ) model.save_pretrained(model_args.output_dir ) image_processor.save_pretrained(model_args.output_dir ) tokenizer.save_pretrained(model_args.output_dir ) if __name__ == "__main__": main()
699
0
from collections.abc import Sequence from queue import Queue class __snake_case : def __init__( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase=None , __UpperCamelCase=None ) -> Tuple: '''simple docstring''' snake_case__ : Any = start snake_case__ : int = end snake_case__ : str = val snake_case__ : Union[str, Any] = (start + end) // 2 snake_case__ : List[Any] = left snake_case__ : int = right def __repr__( self ) -> int: '''simple docstring''' return F"""SegmentTreeNode(start={self.start}, end={self.end}, val={self.val})""" class __snake_case : def __init__( self , __UpperCamelCase , __UpperCamelCase ) -> Optional[int]: '''simple docstring''' snake_case__ : List[Any] = collection snake_case__ : str = function if self.collection: snake_case__ : Any = self._build_tree(0 , len(__UpperCamelCase ) - 1 ) def __a ( self , __UpperCamelCase , __UpperCamelCase ) -> List[str]: '''simple docstring''' self._update_tree(self.root , __UpperCamelCase , __UpperCamelCase ) def __a ( self , __UpperCamelCase , __UpperCamelCase ) -> Optional[int]: '''simple docstring''' return self._query_range(self.root , __UpperCamelCase , __UpperCamelCase ) def __a ( self , __UpperCamelCase , __UpperCamelCase ) -> int: '''simple docstring''' if start == end: return SegmentTreeNode(__UpperCamelCase , __UpperCamelCase , self.collection[start] ) snake_case__ : List[Any] = (start + end) // 2 snake_case__ : Dict = self._build_tree(__UpperCamelCase , __UpperCamelCase ) snake_case__ : str = self._build_tree(mid + 1 , __UpperCamelCase ) return SegmentTreeNode(__UpperCamelCase , __UpperCamelCase , self.fn(left.val , right.val ) , __UpperCamelCase , __UpperCamelCase ) def __a ( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> Union[str, Any]: '''simple docstring''' if node.start == i and node.end == i: snake_case__ : Optional[Any] = val return if i <= node.mid: self._update_tree(node.left , __UpperCamelCase , __UpperCamelCase ) else: self._update_tree(node.right , __UpperCamelCase , __UpperCamelCase ) snake_case__ : Tuple = self.fn(node.left.val , node.right.val ) def __a ( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> Union[str, Any]: '''simple docstring''' if node.start == i and node.end == j: return node.val if i <= node.mid: if j <= node.mid: # range in left child tree return self._query_range(node.left , __UpperCamelCase , __UpperCamelCase ) else: # range in left child tree and right child tree return self.fn( self._query_range(node.left , __UpperCamelCase , node.mid ) , self._query_range(node.right , node.mid + 1 , __UpperCamelCase ) , ) else: # range in right child tree return self._query_range(node.right , __UpperCamelCase , __UpperCamelCase ) def __a ( self ) -> Optional[Any]: '''simple docstring''' if self.root is not None: snake_case__ : Optional[int] = Queue() queue.put(self.root ) while not queue.empty(): snake_case__ : Tuple = queue.get() yield node if node.left is not None: queue.put(node.left ) if node.right is not None: queue.put(node.right ) if __name__ == "__main__": import operator for fn in [operator.add, max, min]: print('''*''' * 50) lowerCAmelCase__ : Dict = SegmentTree([2, 1, 5, 3, 4], fn) for node in arr.traverse(): print(node) print() arr.update(1, 5) for node in arr.traverse(): print(node) print() print(arr.query_range(3, 4)) # 7 print(arr.query_range(2, 2)) # 5 print(arr.query_range(1, 3)) # 13 print()
713
import logging import os from dataclasses import dataclass, field from typing import Dict, Optional import datasets import numpy as np import tensorflow as tf from transformers import ( AutoConfig, AutoTokenizer, EvalPrediction, HfArgumentParser, PreTrainedTokenizer, TFAutoModelForSequenceClassification, TFTrainer, TFTrainingArguments, ) from transformers.utils import logging as hf_logging hf_logging.set_verbosity_info() hf_logging.enable_default_handler() hf_logging.enable_explicit_format() def UpperCamelCase__ ( A__ , A__ , A__ , A__ , A__ , A__ = None , ) -> Optional[int]: snake_case__ : List[str] = {} if train_file is not None: snake_case__ : Tuple = [train_file] if eval_file is not None: snake_case__ : Dict = [eval_file] if test_file is not None: snake_case__ : str = [test_file] snake_case__ : Optional[Any] = datasets.load_dataset('csv' , data_files=A__ ) snake_case__ : Any = list(ds[list(files.keys() )[0]].features.keys() ) snake_case__ : Optional[Any] = features_name.pop(A__ ) snake_case__ : Optional[Any] = list(set(ds[list(files.keys() )[0]][label_name] ) ) snake_case__ : str = {label: i for i, label in enumerate(A__ )} snake_case__ : int = tokenizer.model_input_names snake_case__ : int = {} if len(A__ ) == 1: for k in files.keys(): snake_case__ : str = ds[k].map( lambda A__ : tokenizer.batch_encode_plus( example[features_name[0]] , truncation=A__ , max_length=A__ , padding='max_length' ) , batched=A__ , ) elif len(A__ ) == 2: for k in files.keys(): snake_case__ : Optional[int] = ds[k].map( lambda A__ : tokenizer.batch_encode_plus( (example[features_name[0]], example[features_name[1]]) , truncation=A__ , max_length=A__ , padding='max_length' , ) , batched=A__ , ) def gen_train(): for ex in transformed_ds[datasets.Split.TRAIN]: snake_case__ : int = {k: v for k, v in ex.items() if k in input_names} snake_case__ : Any = labelaid[ex[label_name]] yield (d, label) def gen_val(): for ex in transformed_ds[datasets.Split.VALIDATION]: snake_case__ : int = {k: v for k, v in ex.items() if k in input_names} snake_case__ : Union[str, Any] = labelaid[ex[label_name]] yield (d, label) def gen_test(): for ex in transformed_ds[datasets.Split.TEST]: snake_case__ : Dict = {k: v for k, v in ex.items() if k in input_names} snake_case__ : List[str] = labelaid[ex[label_name]] yield (d, label) snake_case__ : Any = ( tf.data.Dataset.from_generator( A__ , ({k: tf.intaa for k in input_names}, tf.intaa) , ({k: tf.TensorShape([None] ) for k in input_names}, tf.TensorShape([] )) , ) if datasets.Split.TRAIN in transformed_ds else None ) if train_ds is not None: snake_case__ : str = train_ds.apply(tf.data.experimental.assert_cardinality(len(ds[datasets.Split.TRAIN] ) ) ) snake_case__ : Optional[int] = ( tf.data.Dataset.from_generator( A__ , ({k: tf.intaa for k in input_names}, tf.intaa) , ({k: tf.TensorShape([None] ) for k in input_names}, tf.TensorShape([] )) , ) if datasets.Split.VALIDATION in transformed_ds else None ) if val_ds is not None: snake_case__ : Optional[int] = val_ds.apply(tf.data.experimental.assert_cardinality(len(ds[datasets.Split.VALIDATION] ) ) ) snake_case__ : List[str] = ( tf.data.Dataset.from_generator( A__ , ({k: tf.intaa for k in input_names}, tf.intaa) , ({k: tf.TensorShape([None] ) for k in input_names}, tf.TensorShape([] )) , ) if datasets.Split.TEST in transformed_ds else None ) if test_ds is not None: snake_case__ : str = test_ds.apply(tf.data.experimental.assert_cardinality(len(ds[datasets.Split.TEST] ) ) ) return train_ds, val_ds, test_ds, labelaid lowerCAmelCase__ : List[str] = logging.getLogger(__name__) @dataclass class __snake_case : __lowerCamelCase = field(metadata={"""help""": """Which column contains the label"""} ) __lowerCamelCase = field(default=_lowerCamelCase ,metadata={"""help""": """The path of the training file"""} ) __lowerCamelCase = field(default=_lowerCamelCase ,metadata={"""help""": """The path of the development file"""} ) __lowerCamelCase = field(default=_lowerCamelCase ,metadata={"""help""": """The path of the test file"""} ) __lowerCamelCase = field( default=128 ,metadata={ """help""": ( """The maximum total input sequence length after tokenization. Sequences longer """ """than this will be truncated, sequences shorter will be padded.""" ) } ,) __lowerCamelCase = field( default=_lowerCamelCase ,metadata={"""help""": """Overwrite the cached training and evaluation sets"""} ) @dataclass class __snake_case : __lowerCamelCase = field( metadata={"""help""": """Path to pretrained model or model identifier from huggingface.co/models"""} ) __lowerCamelCase = field( default=_lowerCamelCase ,metadata={"""help""": """Pretrained config name or path if not the same as model_name"""} ) __lowerCamelCase = field( default=_lowerCamelCase ,metadata={"""help""": """Pretrained tokenizer name or path if not the same as model_name"""} ) __lowerCamelCase = field(default=_lowerCamelCase ,metadata={"""help""": """Set this flag to use fast tokenization."""} ) # If you want to tweak more attributes on your tokenizer, you should do it in a distinct script, # or just modify its tokenizer_config.json. __lowerCamelCase = field( default=_lowerCamelCase ,metadata={"""help""": """Where do you want to store the pretrained models downloaded from huggingface.co"""} ,) def UpperCamelCase__ ( ) -> Union[str, Any]: # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. snake_case__ : int = HfArgumentParser((ModelArguments, DataTrainingArguments, TFTrainingArguments) ) snake_case__ , snake_case__ , snake_case__ : Dict = parser.parse_args_into_dataclasses() if ( os.path.exists(training_args.output_dir ) and os.listdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir ): raise ValueError( F"""Output directory ({training_args.output_dir}) already exists and is not empty. Use""" ' --overwrite_output_dir to overcome.' ) # Setup logging logging.basicConfig( format='%(asctime)s - %(levelname)s - %(name)s - %(message)s' , datefmt='%m/%d/%Y %H:%M:%S' , level=logging.INFO , ) logger.info( F"""n_replicas: {training_args.n_replicas}, distributed training: {bool(training_args.n_replicas > 1 )}, """ F"""16-bits training: {training_args.fpaa}""" ) logger.info(F"""Training/evaluation parameters {training_args}""" ) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. snake_case__ : Dict = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , ) snake_case__ , snake_case__ , snake_case__ , snake_case__ : Dict = get_tfds( train_file=data_args.train_file , eval_file=data_args.dev_file , test_file=data_args.test_file , tokenizer=A__ , label_column_id=data_args.label_column_id , max_seq_length=data_args.max_seq_length , ) snake_case__ : Dict = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , num_labels=len(A__ ) , labelaid=A__ , idalabel={id: label for label, id in labelaid.items()} , finetuning_task='text-classification' , cache_dir=model_args.cache_dir , ) with training_args.strategy.scope(): snake_case__ : Optional[Any] = TFAutoModelForSequenceClassification.from_pretrained( model_args.model_name_or_path , from_pt=bool('.bin' in model_args.model_name_or_path ) , config=A__ , cache_dir=model_args.cache_dir , ) def compute_metrics(A__ ) -> Dict: snake_case__ : Optional[Any] = np.argmax(p.predictions , axis=1 ) return {"acc": (preds == p.label_ids).mean()} # Initialize our Trainer snake_case__ : Any = TFTrainer( model=A__ , args=A__ , train_dataset=A__ , eval_dataset=A__ , compute_metrics=A__ , ) # Training if training_args.do_train: trainer.train() trainer.save_model() tokenizer.save_pretrained(training_args.output_dir ) # Evaluation snake_case__ : Dict = {} if training_args.do_eval: logger.info('*** Evaluate ***' ) snake_case__ : Tuple = trainer.evaluate() snake_case__ : Any = os.path.join(training_args.output_dir , 'eval_results.txt' ) with open(A__ , 'w' ) as writer: logger.info('***** Eval results *****' ) for key, value in result.items(): logger.info(F""" {key} = {value}""" ) writer.write(F"""{key} = {value}\n""" ) results.update(A__ ) return results if __name__ == "__main__": main()
699
0
lowerCAmelCase__ : Tuple = { '''A''': '''.-''', '''B''': '''-...''', '''C''': '''-.-.''', '''D''': '''-..''', '''E''': '''.''', '''F''': '''..-.''', '''G''': '''--.''', '''H''': '''....''', '''I''': '''..''', '''J''': '''.---''', '''K''': '''-.-''', '''L''': '''.-..''', '''M''': '''--''', '''N''': '''-.''', '''O''': '''---''', '''P''': '''.--.''', '''Q''': '''--.-''', '''R''': '''.-.''', '''S''': '''...''', '''T''': '''-''', '''U''': '''..-''', '''V''': '''...-''', '''W''': '''.--''', '''X''': '''-..-''', '''Y''': '''-.--''', '''Z''': '''--..''', '''1''': '''.----''', '''2''': '''..---''', '''3''': '''...--''', '''4''': '''....-''', '''5''': '''.....''', '''6''': '''-....''', '''7''': '''--...''', '''8''': '''---..''', '''9''': '''----.''', '''0''': '''-----''', '''&''': '''.-...''', '''@''': '''.--.-.''', ''':''': '''---...''', ''',''': '''--..--''', '''.''': '''.-.-.-''', '''\'''': '''.----.''', '''"''': '''.-..-.''', '''?''': '''..--..''', '''/''': '''-..-.''', '''=''': '''-...-''', '''+''': '''.-.-.''', '''-''': '''-....-''', '''(''': '''-.--.''', ''')''': '''-.--.-''', '''!''': '''-.-.--''', ''' ''': '''/''' } # Exclamation mark is not in ITU-R recommendation # fmt: on lowerCAmelCase__ : Optional[Any] = {value: key for key, value in MORSE_CODE_DICT.items()} def UpperCamelCase__ ( A__ ) -> Tuple: return " ".join(MORSE_CODE_DICT[char] for char in message.upper() ) def UpperCamelCase__ ( A__ ) -> List[Any]: return "".join(REVERSE_DICT[char] for char in message.split() ) def UpperCamelCase__ ( ) -> Tuple: snake_case__ : Tuple = 'Morse code here!' print(a__ ) snake_case__ : Union[str, Any] = encrypt(a__ ) print(a__ ) snake_case__ : Any = decrypt(a__ ) print(a__ ) if __name__ == "__main__": main()
714
from typing import List import datasets from datasets.tasks import AudioClassification from ..folder_based_builder import folder_based_builder lowerCAmelCase__ : List[Any] = datasets.utils.logging.get_logger(__name__) class __snake_case ( folder_based_builder.FolderBasedBuilderConfig ): __lowerCamelCase = None __lowerCamelCase = None class __snake_case ( folder_based_builder.FolderBasedBuilder ): __lowerCamelCase = datasets.Audio() __lowerCamelCase = """audio""" __lowerCamelCase = AudioFolderConfig __lowerCamelCase = 42 # definition at the bottom of the script __lowerCamelCase = AudioClassification(audio_column="""audio""" ,label_column="""label""" ) lowerCAmelCase__ : Tuple = [ '''.aiff''', '''.au''', '''.avr''', '''.caf''', '''.flac''', '''.htk''', '''.svx''', '''.mat4''', '''.mat5''', '''.mpc2k''', '''.ogg''', '''.paf''', '''.pvf''', '''.raw''', '''.rf64''', '''.sd2''', '''.sds''', '''.ircam''', '''.voc''', '''.w64''', '''.wav''', '''.nist''', '''.wavex''', '''.wve''', '''.xi''', '''.mp3''', '''.opus''', ] lowerCAmelCase__ : List[Any] = AUDIO_EXTENSIONS
699
0
import argparse import json import os import sys import tempfile import unittest from argparse import Namespace from dataclasses import dataclass, field from enum import Enum from pathlib import Path from typing import List, Literal, Optional import yaml from transformers import HfArgumentParser, TrainingArguments from transformers.hf_argparser import make_choice_type_function, string_to_bool # Since Python 3.10, we can use the builtin `|` operator for Union types # See PEP 604: https://peps.python.org/pep-0604 lowerCAmelCase__ : Tuple = sys.version_info >= (3, 10) def UpperCamelCase__ ( A__=None , A__=None ) -> Tuple: return field(default_factory=lambda: default , metadata=UpperCamelCase__ ) @dataclass class __snake_case : __lowerCamelCase = 42 __lowerCamelCase = 42 __lowerCamelCase = 42 __lowerCamelCase = 42 @dataclass class __snake_case : __lowerCamelCase = 42 __lowerCamelCase = field(default="""toto""" ,metadata={"""help""": """help message"""} ) @dataclass class __snake_case : __lowerCamelCase = False __lowerCamelCase = True __lowerCamelCase = None class __snake_case ( UpperCamelCase__ ): __lowerCamelCase = """titi""" __lowerCamelCase = """toto""" class __snake_case ( UpperCamelCase__ ): __lowerCamelCase = """titi""" __lowerCamelCase = """toto""" __lowerCamelCase = 42 @dataclass class __snake_case : __lowerCamelCase = """toto""" def __a ( self ) -> List[Any]: '''simple docstring''' snake_case__ : Union[str, Any] = BasicEnum(self.foo ) @dataclass class __snake_case : __lowerCamelCase = """toto""" def __a ( self ) -> Tuple: '''simple docstring''' snake_case__ : Any = MixedTypeEnum(self.foo ) @dataclass class __snake_case : __lowerCamelCase = None __lowerCamelCase = field(default=UpperCamelCase__ ,metadata={"""help""": """help message"""} ) __lowerCamelCase = None __lowerCamelCase = list_field(default=[] ) __lowerCamelCase = list_field(default=[] ) @dataclass class __snake_case : __lowerCamelCase = list_field(default=[] ) __lowerCamelCase = list_field(default=[1, 2, 3] ) __lowerCamelCase = list_field(default=["""Hallo""", """Bonjour""", """Hello"""] ) __lowerCamelCase = list_field(default=[0.1, 0.2, 0.3] ) @dataclass class __snake_case : __lowerCamelCase = field() __lowerCamelCase = field() __lowerCamelCase = field() def __a ( self ) -> Union[str, Any]: '''simple docstring''' snake_case__ : str = BasicEnum(self.required_enum ) @dataclass class __snake_case : __lowerCamelCase = 42 __lowerCamelCase = field() __lowerCamelCase = None __lowerCamelCase = field(default="""toto""" ,metadata={"""help""": """help message"""} ) __lowerCamelCase = list_field(default=["""Hallo""", """Bonjour""", """Hello"""] ) if is_python_no_less_than_3_10: @dataclass class __snake_case : __lowerCamelCase = False __lowerCamelCase = True __lowerCamelCase = None @dataclass class __snake_case : __lowerCamelCase = None __lowerCamelCase = field(default=UpperCamelCase__ ,metadata={"""help""": """help message"""} ) __lowerCamelCase = None __lowerCamelCase = list_field(default=[] ) __lowerCamelCase = list_field(default=[] ) class __snake_case ( unittest.TestCase ): def __a ( self , __UpperCamelCase , __UpperCamelCase ) -> List[str]: '''simple docstring''' self.assertEqual(len(a._actions ) , len(b._actions ) ) for x, y in zip(a._actions , b._actions ): snake_case__ : int = {k: v for k, v in vars(__A ).items() if k != 'container'} snake_case__ : List[Any] = {k: v for k, v in vars(__A ).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' , __A ) and yy.get('choices' , __A ): for expected_choice in yy["choices"] + xx["choices"]: self.assertEqual(xx['type'](__A ) , yy['type'](__A ) ) del xx["type"], yy["type"] self.assertEqual(__A , __A ) def __a ( self ) -> str: '''simple docstring''' snake_case__ : str = HfArgumentParser(__A ) snake_case__ : Union[str, Any] = argparse.ArgumentParser() expected.add_argument('--foo' , type=__A , required=__A ) expected.add_argument('--bar' , type=__A , required=__A ) expected.add_argument('--baz' , type=__A , required=__A ) expected.add_argument('--flag' , type=__A , default=__A , const=__A , nargs='?' ) self.argparsersEqual(__A , __A ) snake_case__ : Any = ['--foo', '1', '--baz', 'quux', '--bar', '0.5'] ((snake_case__ ) , ) : Dict = parser.parse_args_into_dataclasses(__A , look_for_args_file=__A ) self.assertFalse(example.flag ) def __a ( self ) -> Union[str, Any]: '''simple docstring''' snake_case__ : Union[str, Any] = HfArgumentParser(__A ) snake_case__ : Optional[int] = argparse.ArgumentParser() expected.add_argument('--foo' , default=42 , type=__A ) expected.add_argument('--baz' , default='toto' , type=__A , help='help message' ) self.argparsersEqual(__A , __A ) def __a ( self ) -> Any: '''simple docstring''' snake_case__ : Optional[Any] = argparse.ArgumentParser() expected.add_argument('--foo' , type=__A , default=__A , const=__A , nargs='?' ) expected.add_argument('--baz' , type=__A , default=__A , const=__A , 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=__A , dest='baz' ) expected.add_argument('--opt' , type=__A , default=__A ) snake_case__ : List[Any] = [WithDefaultBoolExample] if is_python_no_less_than_3_10: dataclass_types.append(__A ) for dataclass_type in dataclass_types: snake_case__ : Any = HfArgumentParser(__A ) self.argparsersEqual(__A , __A ) snake_case__ : Union[str, Any] = parser.parse_args([] ) self.assertEqual(__A , Namespace(foo=__A , baz=__A , opt=__A ) ) snake_case__ : Optional[Any] = parser.parse_args(['--foo', '--no_baz'] ) self.assertEqual(__A , Namespace(foo=__A , baz=__A , opt=__A ) ) snake_case__ : Tuple = parser.parse_args(['--foo', '--baz'] ) self.assertEqual(__A , Namespace(foo=__A , baz=__A , opt=__A ) ) snake_case__ : str = parser.parse_args(['--foo', 'True', '--baz', 'True', '--opt', 'True'] ) self.assertEqual(__A , Namespace(foo=__A , baz=__A , opt=__A ) ) snake_case__ : Optional[int] = parser.parse_args(['--foo', 'False', '--baz', 'False', '--opt', 'False'] ) self.assertEqual(__A , Namespace(foo=__A , baz=__A , opt=__A ) ) def __a ( self ) -> Optional[int]: '''simple docstring''' snake_case__ : List[str] = HfArgumentParser(__A ) snake_case__ : Dict = argparse.ArgumentParser() expected.add_argument( '--foo' , default='toto' , choices=['titi', 'toto', 42] , type=make_choice_type_function(['titi', 'toto', 42] ) , ) self.argparsersEqual(__A , __A ) snake_case__ : Optional[Any] = parser.parse_args([] ) self.assertEqual(args.foo , 'toto' ) snake_case__ : Optional[Any] = parser.parse_args_into_dataclasses([] )[0] self.assertEqual(enum_ex.foo , MixedTypeEnum.toto ) snake_case__ : List[Any] = parser.parse_args(['--foo', 'titi'] ) self.assertEqual(args.foo , 'titi' ) snake_case__ : Any = parser.parse_args_into_dataclasses(['--foo', 'titi'] )[0] self.assertEqual(enum_ex.foo , MixedTypeEnum.titi ) snake_case__ : Optional[Any] = parser.parse_args(['--foo', '42'] ) self.assertEqual(args.foo , 42 ) snake_case__ : Optional[Any] = parser.parse_args_into_dataclasses(['--foo', '42'] )[0] self.assertEqual(enum_ex.foo , MixedTypeEnum.fourtytwo ) def __a ( self ) -> Any: '''simple docstring''' @dataclass class __snake_case : __lowerCamelCase = """toto""" snake_case__ : List[str] = HfArgumentParser(__A ) snake_case__ : Any = argparse.ArgumentParser() expected.add_argument( '--foo' , default='toto' , choices=('titi', 'toto', 42) , type=make_choice_type_function(['titi', 'toto', 42] ) , ) self.argparsersEqual(__A , __A ) snake_case__ : Optional[Any] = parser.parse_args([] ) self.assertEqual(args.foo , 'toto' ) snake_case__ : Optional[Any] = parser.parse_args(['--foo', 'titi'] ) self.assertEqual(args.foo , 'titi' ) snake_case__ : Tuple = parser.parse_args(['--foo', '42'] ) self.assertEqual(args.foo , 42 ) def __a ( self ) -> List[Any]: '''simple docstring''' snake_case__ : Any = HfArgumentParser(__A ) snake_case__ : Any = argparse.ArgumentParser() expected.add_argument('--foo_int' , nargs='+' , default=[] , type=__A ) expected.add_argument('--bar_int' , nargs='+' , default=[1, 2, 3] , type=__A ) expected.add_argument('--foo_str' , nargs='+' , default=['Hallo', 'Bonjour', 'Hello'] , type=__A ) expected.add_argument('--foo_float' , nargs='+' , default=[0.1, 0.2, 0.3] , type=__A ) self.argparsersEqual(__A , __A ) snake_case__ : Union[str, Any] = parser.parse_args([] ) self.assertEqual( __A , Namespace(foo_int=[] , bar_int=[1, 2, 3] , foo_str=['Hallo', 'Bonjour', 'Hello'] , foo_float=[0.1, 0.2, 0.3] ) , ) snake_case__ : List[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(__A , Namespace(foo_int=[1] , bar_int=[2, 3] , foo_str=['a', 'b', 'c'] , foo_float=[0.1, 0.7] ) ) def __a ( self ) -> Optional[Any]: '''simple docstring''' snake_case__ : Tuple = argparse.ArgumentParser() expected.add_argument('--foo' , default=__A , type=__A ) expected.add_argument('--bar' , default=__A , type=__A , help='help message' ) expected.add_argument('--baz' , default=__A , type=__A ) expected.add_argument('--ces' , nargs='+' , default=[] , type=__A ) expected.add_argument('--des' , nargs='+' , default=[] , type=__A ) snake_case__ : Any = [OptionalExample] if is_python_no_less_than_3_10: dataclass_types.append(__A ) for dataclass_type in dataclass_types: snake_case__ : Union[str, Any] = HfArgumentParser(__A ) self.argparsersEqual(__A , __A ) snake_case__ : Union[str, Any] = parser.parse_args([] ) self.assertEqual(__A , Namespace(foo=__A , bar=__A , baz=__A , ces=[] , des=[] ) ) snake_case__ : Optional[int] = parser.parse_args('--foo 12 --bar 3.14 --baz 42 --ces a b c --des 1 2 3'.split() ) self.assertEqual(__A , Namespace(foo=12 , bar=3.1_4 , baz='42' , ces=['a', 'b', 'c'] , des=[1, 2, 3] ) ) def __a ( self ) -> Union[str, Any]: '''simple docstring''' snake_case__ : Any = HfArgumentParser(__A ) snake_case__ : Union[str, Any] = argparse.ArgumentParser() expected.add_argument('--required_list' , nargs='+' , type=__A , required=__A ) expected.add_argument('--required_str' , type=__A , required=__A ) expected.add_argument( '--required_enum' , type=make_choice_type_function(['titi', 'toto'] ) , choices=['titi', 'toto'] , required=__A , ) self.argparsersEqual(__A , __A ) def __a ( self ) -> Optional[int]: '''simple docstring''' snake_case__ : str = HfArgumentParser(__A ) snake_case__ : Optional[int] = argparse.ArgumentParser() expected.add_argument('--foo' , type=__A , required=__A ) expected.add_argument( '--required_enum' , type=make_choice_type_function(['titi', 'toto'] ) , choices=['titi', 'toto'] , required=__A , ) expected.add_argument('--opt' , type=__A , default=__A ) expected.add_argument('--baz' , default='toto' , type=__A , help='help message' ) expected.add_argument('--foo_str' , nargs='+' , default=['Hallo', 'Bonjour', 'Hello'] , type=__A ) self.argparsersEqual(__A , __A ) def __a ( self ) -> List[str]: '''simple docstring''' snake_case__ : Tuple = HfArgumentParser(__A ) snake_case__ : Any = { 'foo': 12, 'bar': 3.1_4, 'baz': '42', 'flag': True, } snake_case__ : Tuple = parser.parse_dict(__A )[0] snake_case__ : Union[str, Any] = BasicExample(**__A ) self.assertEqual(__A , __A ) def __a ( self ) -> Union[str, Any]: '''simple docstring''' snake_case__ : Optional[int] = HfArgumentParser(__A ) snake_case__ : List[Any] = { 'foo': 12, 'bar': 3.1_4, 'baz': '42', 'flag': True, 'extra': 42, } self.assertRaises(__A , parser.parse_dict , __A , allow_extra_keys=__A ) def __a ( self ) -> Optional[Any]: '''simple docstring''' snake_case__ : Union[str, Any] = HfArgumentParser(__A ) snake_case__ : List[str] = { 'foo': 12, 'bar': 3.1_4, 'baz': '42', 'flag': True, } with tempfile.TemporaryDirectory() as tmp_dir: snake_case__ : List[Any] = os.path.join(__A , 'temp_json' ) os.mkdir(__A ) with open(temp_local_path + '.json' , 'w+' ) as f: json.dump(__A , __A ) snake_case__ : List[str] = parser.parse_yaml_file(Path(temp_local_path + '.json' ) )[0] snake_case__ : Any = BasicExample(**__A ) self.assertEqual(__A , __A ) def __a ( self ) -> Union[str, Any]: '''simple docstring''' snake_case__ : Tuple = HfArgumentParser(__A ) snake_case__ : Union[str, Any] = { 'foo': 12, 'bar': 3.1_4, 'baz': '42', 'flag': True, } with tempfile.TemporaryDirectory() as tmp_dir: snake_case__ : Dict = os.path.join(__A , 'temp_yaml' ) os.mkdir(__A ) with open(temp_local_path + '.yaml' , 'w+' ) as f: yaml.dump(__A , __A ) snake_case__ : List[str] = parser.parse_yaml_file(Path(temp_local_path + '.yaml' ) )[0] snake_case__ : List[str] = BasicExample(**__A ) self.assertEqual(__A , __A ) def __a ( self ) -> str: '''simple docstring''' snake_case__ : Tuple = HfArgumentParser(__A ) self.assertIsNotNone(__A )
715
import random import unittest import torch from diffusers import IFInpaintingPipeline from diffusers.utils import floats_tensor from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import skip_mps, torch_device from ..pipeline_params import ( TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS, ) from ..test_pipelines_common import PipelineTesterMixin from . import IFPipelineTesterMixin @skip_mps class __snake_case ( _lowerCamelCase ,_lowerCamelCase ,unittest.TestCase ): __lowerCamelCase = IFInpaintingPipeline __lowerCamelCase = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {"""width""", """height"""} __lowerCamelCase = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS __lowerCamelCase = PipelineTesterMixin.required_optional_params - {"""latents"""} def __a ( self ) -> Optional[Any]: '''simple docstring''' return self._get_dummy_components() def __a ( self , __UpperCamelCase , __UpperCamelCase=0 ) -> str: '''simple docstring''' if str(__UpperCamelCase ).startswith('mps' ): snake_case__ : int = torch.manual_seed(__UpperCamelCase ) else: snake_case__ : Union[str, Any] = torch.Generator(device=__UpperCamelCase ).manual_seed(__UpperCamelCase ) snake_case__ : Optional[int] = floats_tensor((1, 3, 32, 32) , rng=random.Random(__UpperCamelCase ) ).to(__UpperCamelCase ) snake_case__ : Dict = floats_tensor((1, 3, 32, 32) , rng=random.Random(__UpperCamelCase ) ).to(__UpperCamelCase ) snake_case__ : Optional[Any] = { 'prompt': 'A painting of a squirrel eating a burger', 'image': image, 'mask_image': mask_image, 'generator': generator, 'num_inference_steps': 2, 'output_type': 'numpy', } return inputs @unittest.skipIf( torch_device != 'cuda' or not is_xformers_available() , reason='XFormers attention is only available with CUDA and `xformers` installed' , ) def __a ( self ) -> List[Any]: '''simple docstring''' self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3 ) def __a ( self ) -> Optional[int]: '''simple docstring''' self._test_save_load_optional_components() @unittest.skipIf(torch_device != 'cuda' , reason='float16 requires CUDA' ) def __a ( self ) -> List[str]: '''simple docstring''' super().test_save_load_floataa(expected_max_diff=1E-1 ) def __a ( self ) -> List[str]: '''simple docstring''' self._test_attention_slicing_forward_pass(expected_max_diff=1E-2 ) def __a ( self ) -> int: '''simple docstring''' self._test_save_load_local() def __a ( self ) -> List[str]: '''simple docstring''' self._test_inference_batch_single_identical( expected_max_diff=1E-2 , )
699
0
# Lint as: python3 import dataclasses import re from dataclasses import dataclass from functools import total_ordering from typing import Optional, Union lowerCAmelCase__ : List[str] = re.compile(r'''^(?P<major>\d+)''' r'''\.(?P<minor>\d+)''' r'''\.(?P<patch>\d+)$''') @total_ordering @dataclass class __snake_case : __lowerCamelCase = 42 __lowerCamelCase = None __lowerCamelCase = None __lowerCamelCase = None __lowerCamelCase = None def __a ( self ) -> str: '''simple docstring''' snake_case__ , snake_case__ , snake_case__ : Optional[Any] = _str_to_version_tuple(self.version_str ) def __repr__( self ) -> Dict: '''simple docstring''' return F"""{self.tuple[0]}.{self.tuple[1]}.{self.tuple[2]}""" @property def __a ( self ) -> Tuple: '''simple docstring''' return self.major, self.minor, self.patch def __a ( self , __UpperCamelCase ) -> Any: '''simple docstring''' if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): return Version(__SCREAMING_SNAKE_CASE ) elif isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): return other raise TypeError(F"""{other} (type {type(__SCREAMING_SNAKE_CASE )}) cannot be compared to version.""" ) def __eq__( self , __UpperCamelCase ) -> Union[str, Any]: '''simple docstring''' try: snake_case__ : List[str] = self._validate_operand(__SCREAMING_SNAKE_CASE ) except (TypeError, ValueError): return False else: return self.tuple == other.tuple def __lt__( self , __UpperCamelCase ) -> Tuple: '''simple docstring''' snake_case__ : Optional[Any] = self._validate_operand(__SCREAMING_SNAKE_CASE ) return self.tuple < other.tuple def __hash__( self ) -> str: '''simple docstring''' return hash(_version_tuple_to_str(self.tuple ) ) @classmethod def __a ( cls , __UpperCamelCase ) -> str: '''simple docstring''' snake_case__ : Any = {f.name for f in dataclasses.fields(cls )} return cls(**{k: v for k, v in dic.items() if k in field_names} ) def __a ( self ) -> List[Any]: '''simple docstring''' return self.version_str def UpperCamelCase__ ( A__ ) -> Optional[Any]: snake_case__ : Optional[int] = _VERSION_REG.match(_UpperCAmelCase ) if not res: raise ValueError(F"""Invalid version \'{version_str}\'. Format should be x.y.z with {{x,y,z}} being digits.""" ) return tuple(int(_UpperCAmelCase ) for v in [res.group('major' ), res.group('minor' ), res.group('patch' )] ) def UpperCamelCase__ ( A__ ) -> Dict: return ".".join(str(_UpperCAmelCase ) for v in version_tuple )
716
import unittest from transformers import BertGenerationTokenizer from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_torch, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin lowerCAmelCase__ : List[Any] = '''▁''' lowerCAmelCase__ : int = get_tests_dir('''fixtures/test_sentencepiece.model''') @require_sentencepiece class __snake_case ( _lowerCamelCase ,unittest.TestCase ): __lowerCamelCase = BertGenerationTokenizer __lowerCamelCase = False __lowerCamelCase = True def __a ( self ) -> Optional[int]: '''simple docstring''' super().setUp() snake_case__ : str = BertGenerationTokenizer(__UpperCamelCase , keep_accents=__UpperCamelCase ) tokenizer.save_pretrained(self.tmpdirname ) def __a ( self ) -> Optional[int]: '''simple docstring''' snake_case__ : List[str] = '<s>' snake_case__ : Dict = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(__UpperCamelCase ) , __UpperCamelCase ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(__UpperCamelCase ) , __UpperCamelCase ) def __a ( self ) -> List[str]: '''simple docstring''' snake_case__ : Tuple = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '<unk>' ) self.assertEqual(vocab_keys[1] , '<s>' ) self.assertEqual(vocab_keys[-1] , '<pad>' ) self.assertEqual(len(__UpperCamelCase ) , 1002 ) def __a ( self ) -> int: '''simple docstring''' self.assertEqual(self.get_tokenizer().vocab_size , 1000 ) def __a ( self ) -> Tuple: '''simple docstring''' snake_case__ : Optional[Any] = BertGenerationTokenizer(__UpperCamelCase , keep_accents=__UpperCamelCase ) snake_case__ : int = tokenizer.tokenize('This is a test' ) self.assertListEqual(__UpperCamelCase , ['▁This', '▁is', '▁a', '▁t', 'est'] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(__UpperCamelCase ) , [285, 46, 10, 170, 382] , ) snake_case__ : Any = tokenizer.tokenize('I was born in 92000, and this is falsé.' ) self.assertListEqual( __UpperCamelCase , [ SPIECE_UNDERLINE + 'I', SPIECE_UNDERLINE + 'was', SPIECE_UNDERLINE + 'b', 'or', 'n', SPIECE_UNDERLINE + 'in', SPIECE_UNDERLINE + '', '9', '2', '0', '0', '0', ',', SPIECE_UNDERLINE + 'and', SPIECE_UNDERLINE + 'this', SPIECE_UNDERLINE + 'is', SPIECE_UNDERLINE + 'f', 'al', 's', 'é', '.', ] , ) snake_case__ : Optional[Any] = tokenizer.convert_tokens_to_ids(__UpperCamelCase ) self.assertListEqual( __UpperCamelCase , [8, 21, 84, 55, 24, 19, 7, 0, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 0, 4] , ) snake_case__ : int = tokenizer.convert_ids_to_tokens(__UpperCamelCase ) self.assertListEqual( __UpperCamelCase , [ SPIECE_UNDERLINE + 'I', SPIECE_UNDERLINE + 'was', SPIECE_UNDERLINE + 'b', 'or', 'n', SPIECE_UNDERLINE + 'in', SPIECE_UNDERLINE + '', '<unk>', '2', '0', '0', '0', ',', SPIECE_UNDERLINE + 'and', SPIECE_UNDERLINE + 'this', SPIECE_UNDERLINE + 'is', SPIECE_UNDERLINE + 'f', 'al', 's', '<unk>', '.', ] , ) @cached_property def __a ( self ) -> Dict: '''simple docstring''' return BertGenerationTokenizer.from_pretrained('google/bert_for_seq_generation_L-24_bbc_encoder' ) @slow def __a ( self ) -> Any: '''simple docstring''' snake_case__ : int = 'Hello World!' snake_case__ : Union[str, Any] = [18536, 2260, 101] self.assertListEqual(__UpperCamelCase , self.big_tokenizer.encode(__UpperCamelCase ) ) @slow def __a ( self ) -> Optional[int]: '''simple docstring''' snake_case__ : str = ( 'This is a very long text with a lot of weird characters, such as: . , ~ ? ( ) " [ ] ! : - . Also we will' ' add words that should not exsist and be tokenized to <unk>, such as saoneuhaoesuth' ) snake_case__ : List[Any] = [ 871, 419, 358, 946, 991, 2521, 452, 358, 1357, 387, 7751, 3536, 112, 985, 456, 126, 865, 938, 5400, 5734, 458, 1368, 467, 786, 2462, 5246, 1159, 633, 865, 4519, 457, 582, 852, 2557, 427, 916, 508, 405, 34324, 497, 391, 408, 11342, 1244, 385, 100, 938, 985, 456, 574, 362, 12597, 3200, 3129, 1172, ] self.assertListEqual(__UpperCamelCase , self.big_tokenizer.encode(__UpperCamelCase ) ) @require_torch @slow def __a ( self ) -> List[str]: '''simple docstring''' import torch from transformers import BertGenerationConfig, BertGenerationEncoder # Build sequence snake_case__ : Optional[int] = list(self.big_tokenizer.get_vocab().keys() )[:10] snake_case__ : Optional[int] = ' '.join(__UpperCamelCase ) snake_case__ : int = self.big_tokenizer.encode_plus(__UpperCamelCase , return_tensors='pt' , return_token_type_ids=__UpperCamelCase ) snake_case__ : Tuple = self.big_tokenizer.batch_encode_plus( [sequence + ' ' + sequence] , return_tensors='pt' , return_token_type_ids=__UpperCamelCase ) snake_case__ : Dict = BertGenerationConfig() snake_case__ : List[str] = BertGenerationEncoder(__UpperCamelCase ) assert model.get_input_embeddings().weight.shape[0] >= self.big_tokenizer.vocab_size with torch.no_grad(): model(**__UpperCamelCase ) model(**__UpperCamelCase ) @slow def __a ( self ) -> Dict: '''simple docstring''' snake_case__ : Optional[int] = {'input_ids': [[39286, 458, 36335, 2001, 456, 13073, 13266, 455, 113, 7746, 1741, 11157, 391, 13073, 13266, 455, 113, 3967, 35412, 113, 4936, 109, 3870, 2377, 113, 30084, 45720, 458, 134, 17496, 112, 503, 11672, 113, 118, 112, 5665, 13347, 38687, 112, 1496, 31389, 112, 3268, 47264, 134, 962, 112, 16377, 8035, 23130, 430, 12169, 15518, 28592, 458, 146, 41697, 109, 391, 12169, 15518, 16689, 458, 146, 41358, 109, 452, 726, 4034, 111, 763, 35412, 5082, 388, 1903, 111, 9051, 391, 2870, 48918, 1900, 1123, 550, 998, 112, 9586, 15985, 455, 391, 410, 22955, 37636, 114], [448, 17496, 419, 3663, 385, 763, 113, 27533, 2870, 3283, 13043, 1639, 24713, 523, 656, 24013, 18550, 2521, 517, 27014, 21244, 420, 1212, 1465, 391, 927, 4833, 388, 578, 11786, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [484, 2169, 7687, 21932, 18146, 726, 363, 17032, 3391, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], 'attention_mask': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=__UpperCamelCase , model_name='google/bert_for_seq_generation_L-24_bbc_encoder' , revision='c817d1fd1be2ffa69431227a1fe320544943d4db' , )
699
0
import unittest from transformers import load_tool from transformers.utils import is_torch_available if is_torch_available(): import torch from transformers.testing_utils import require_torch from .test_tools_common import ToolTesterMixin @require_torch class __snake_case ( unittest.TestCase ,UpperCamelCase_ ): def __a ( self ) -> List[Any]: '''simple docstring''' snake_case__ : List[str] = load_tool('text-to-speech' ) self.tool.setup() def __a ( self ) -> Optional[Any]: '''simple docstring''' torch.manual_seed(0 ) snake_case__ : Dict = self.tool('hey' ) snake_case__ : List[Any] = result.to_raw() self.assertTrue( torch.allclose( resulting_tensor[:3] , torch.tensor([-0.0_0_0_5_9_6_6_6_6_8_8_3_2_1_1_5_8_2_9, -0.0_0_0_3_6_5_7_6_4_0_1_9_0_7_9_5_0_6_4, -0.0_0_0_1_3_4_3_9_5_0_2_7_9_9_8_8_3_4_8_5] ) , ) ) def __a ( self ) -> List[Any]: '''simple docstring''' torch.manual_seed(0 ) snake_case__ : Any = self.tool('hey' ) snake_case__ : Any = result.to_raw() self.assertTrue( torch.allclose( resulting_tensor[:3] , torch.tensor([-0.0_0_0_5_9_6_6_6_6_8_8_3_2_1_1_5_8_2_9, -0.0_0_0_3_6_5_7_6_4_0_1_9_0_7_9_5_0_6_4, -0.0_0_0_1_3_4_3_9_5_0_2_7_9_9_8_8_3_4_8_5] ) , ) )
717
import random import torch from huggingface_hub import HfApi from diffusers import UNetaDModel lowerCAmelCase__ : List[str] = HfApi() lowerCAmelCase__ : str = {} # fmt: off lowerCAmelCase__ : int = torch.tensor([ -0.75_15, -1.68_83, 0.24_20, 0.03_00, 0.63_47, 1.34_33, -1.17_43, -3.74_67, 1.23_42, -2.24_85, 0.46_36, 0.80_76, -0.79_91, 0.39_69, 0.84_98, 0.91_89, -1.88_87, -3.35_22, 0.76_39, 0.20_40, 0.62_71, -2.71_48, -1.63_16, 3.08_39, 0.31_86, 0.27_21, -0.97_59, -1.24_61, 2.62_57, 1.35_57 ]) lowerCAmelCase__ : Dict = torch.tensor([ -2.36_39, -2.53_44, 0.00_54, -0.66_74, 1.59_90, 1.01_58, 0.31_24, -2.14_36, 1.87_95, -2.54_29, -0.15_66, -0.39_73, 1.24_90, 2.64_47, 1.22_83, -0.52_08, -2.81_54, -3.51_19, 2.38_38, 1.20_33, 1.72_01, -2.12_56, -1.45_76, 2.79_48, 2.42_04, -0.97_52, -1.25_46, 0.80_27, 3.27_58, 3.13_65 ]) lowerCAmelCase__ : Dict = torch.tensor([ -0.65_31, -0.68_91, -0.31_72, -0.53_75, -0.91_40, -0.53_67, -0.11_75, -0.78_69, -0.38_08, -0.45_13, -0.20_98, -0.00_83, 0.31_83, 0.51_40, 0.22_47, -0.13_04, -0.13_02, -0.28_02, -0.20_84, -0.20_25, -0.49_67, -0.48_73, -0.08_61, 0.69_25, 0.02_50, 0.12_90, -0.15_43, 0.63_16, 1.04_60, 1.49_43 ]) lowerCAmelCase__ : List[str] = torch.tensor([ 0.09_11, 0.11_07, 0.01_82, 0.04_35, -0.08_05, -0.06_08, 0.03_81, 0.21_72, -0.02_80, 0.13_27, -0.02_99, -0.02_55, -0.00_50, -0.11_70, -0.10_46, 0.03_09, 0.13_67, 0.17_28, -0.05_33, -0.07_48, -0.05_34, 0.16_24, 0.03_84, -0.18_05, -0.07_07, 0.06_42, 0.02_20, -0.01_34, -0.13_33, -0.15_05 ]) lowerCAmelCase__ : Union[str, Any] = torch.tensor([ 0.13_21, 0.13_37, 0.04_40, 0.06_22, -0.05_91, -0.03_70, 0.05_03, 0.21_33, -0.01_77, 0.14_15, -0.01_16, -0.01_12, 0.00_44, -0.09_80, -0.07_89, 0.03_95, 0.15_02, 0.17_85, -0.04_88, -0.05_14, -0.04_04, 0.15_39, 0.04_54, -0.15_59, -0.06_65, 0.06_59, 0.03_83, -0.00_05, -0.12_66, -0.13_86 ]) lowerCAmelCase__ : List[Any] = torch.tensor([ 0.11_54, 0.12_18, 0.03_07, 0.05_26, -0.07_11, -0.05_41, 0.03_66, 0.20_78, -0.02_67, 0.13_17, -0.02_26, -0.01_93, -0.00_14, -0.10_55, -0.09_02, 0.03_30, 0.13_91, 0.17_09, -0.05_62, -0.06_93, -0.05_60, 0.14_82, 0.03_81, -0.16_83, -0.06_81, 0.06_61, 0.03_31, -0.00_46, -0.12_68, -0.14_31 ]) lowerCAmelCase__ : Optional[Any] = torch.tensor([ 0.11_92, 0.12_40, 0.04_14, 0.06_06, -0.05_57, -0.04_12, 0.04_30, 0.20_42, -0.02_00, 0.13_85, -0.01_15, -0.01_32, 0.00_17, -0.09_65, -0.08_02, 0.03_98, 0.14_33, 0.17_47, -0.04_58, -0.05_33, -0.04_07, 0.15_45, 0.04_19, -0.15_74, -0.06_45, 0.06_26, 0.03_41, -0.00_10, -0.11_99, -0.13_90 ]) lowerCAmelCase__ : List[str] = torch.tensor([ 0.10_75, 0.10_74, 0.02_05, 0.04_31, -0.07_74, -0.06_07, 0.02_98, 0.20_42, -0.03_20, 0.12_67, -0.02_81, -0.02_50, -0.00_64, -0.10_91, -0.09_46, 0.02_90, 0.13_28, 0.16_50, -0.05_80, -0.07_38, -0.05_86, 0.14_40, 0.03_37, -0.17_46, -0.07_12, 0.06_05, 0.02_50, -0.00_99, -0.13_16, -0.14_73 ]) lowerCAmelCase__ : List[str] = torch.tensor([ -1.45_72, -2.04_81, -0.04_14, -0.60_05, 1.41_36, 0.58_48, 0.40_28, -2.73_30, 1.22_12, -2.12_28, 0.21_55, 0.40_39, 0.76_62, 2.05_35, 0.74_77, -0.32_43, -2.17_58, -2.76_48, 1.69_47, 0.70_26, 1.23_38, -1.60_78, -0.86_82, 2.28_10, 1.85_74, -0.57_18, -0.55_86, -0.01_86, 2.34_15, 2.12_51]) lowerCAmelCase__ : List[Any] = torch.tensor([ -1.36_90, -1.97_20, -0.40_90, -0.69_66, 1.46_60, 0.99_38, -0.13_85, -2.73_24, 0.77_36, -1.89_17, 0.29_23, 0.42_93, 0.16_93, 1.41_12, 1.18_87, -0.31_81, -2.21_60, -2.63_81, 1.31_70, 0.81_63, 0.92_40, -1.65_44, -0.60_99, 2.52_59, 1.64_30, -0.90_90, -0.93_92, -0.01_26, 2.42_68, 2.32_66 ]) lowerCAmelCase__ : Tuple = torch.tensor([ -1.35_25, -1.96_28, -0.39_56, -0.68_60, 1.46_64, 1.00_14, -0.12_59, -2.72_12, 0.77_72, -1.88_11, 0.29_96, 0.43_88, 0.17_04, 1.40_29, 1.17_01, -0.30_27, -2.20_53, -2.62_87, 1.33_50, 0.81_31, 0.92_74, -1.62_92, -0.60_98, 2.51_31, 1.65_05, -0.89_58, -0.92_98, -0.01_51, 2.42_57, 2.33_55 ]) lowerCAmelCase__ : List[str] = torch.tensor([ -2.05_85, -2.78_97, -0.28_50, -0.89_40, 1.90_52, 0.57_02, 0.63_45, -3.89_59, 1.59_32, -3.23_19, 0.19_74, 0.02_87, 1.75_66, 2.65_43, 0.83_87, -0.53_51, -3.27_36, -4.33_75, 2.90_29, 1.63_90, 1.46_40, -2.17_01, -1.90_13, 2.93_41, 3.49_81, -0.62_55, -1.16_44, -0.15_91, 3.70_97, 3.20_66 ]) lowerCAmelCase__ : Dict = torch.tensor([ -2.31_39, -2.55_94, -0.01_97, -0.67_85, 1.70_01, 1.16_06, 0.30_75, -2.17_40, 1.80_71, -2.56_30, -0.09_26, -0.38_11, 1.21_16, 2.62_46, 1.27_31, -0.53_98, -2.81_53, -3.61_40, 2.38_93, 1.32_62, 1.62_58, -2.18_56, -1.32_67, 2.83_95, 2.37_79, -1.06_23, -1.24_68, 0.89_59, 3.33_67, 3.22_43 ]) lowerCAmelCase__ : Dict = torch.tensor([ -2.06_28, -2.76_67, -0.20_89, -0.82_63, 2.05_39, 0.59_92, 0.64_95, -3.83_36, 1.60_25, -3.28_17, 0.17_21, -0.06_33, 1.75_16, 2.70_39, 0.81_00, -0.59_08, -3.21_13, -4.43_43, 2.92_57, 1.36_32, 1.55_62, -2.14_89, -1.98_94, 3.05_60, 3.33_96, -0.73_28, -1.04_17, 0.03_83, 3.70_93, 3.23_43 ]) lowerCAmelCase__ : Any = torch.tensor([ -1.45_74, -2.05_69, -0.04_73, -0.61_17, 1.40_18, 0.57_69, 0.41_29, -2.73_44, 1.22_41, -2.13_97, 0.20_00, 0.39_37, 0.76_16, 2.04_53, 0.73_24, -0.33_91, -2.17_46, -2.77_44, 1.69_63, 0.69_21, 1.21_87, -1.61_72, -0.88_77, 2.24_39, 1.84_71, -0.58_39, -0.56_05, -0.04_64, 2.32_50, 2.12_19 ]) # fmt: on lowerCAmelCase__ : Any = api.list_models(filter='''diffusers''') for mod in models: if "google" in mod.author or mod.modelId == "CompVis/ldm-celebahq-256": lowerCAmelCase__ : List[str] = '''/home/patrick/google_checkpoints/''' + mod.modelId.split('''/''')[-1] print(F'''Started running {mod.modelId}!!!''') if mod.modelId.startswith('''CompVis'''): lowerCAmelCase__ : int = UNetaDModel.from_pretrained(local_checkpoint, subfolder='''unet''') else: lowerCAmelCase__ : str = UNetaDModel.from_pretrained(local_checkpoint) torch.manual_seed(0) random.seed(0) lowerCAmelCase__ : Any = torch.randn(1, model.config.in_channels, model.config.sample_size, model.config.sample_size) lowerCAmelCase__ : List[str] = torch.tensor([10] * noise.shape[0]) with torch.no_grad(): lowerCAmelCase__ : int = model(noise, time_step).sample assert torch.allclose( logits[0, 0, 0, :30], results['''_'''.join('''_'''.join(mod.modelId.split('''/''')).split('''-'''))], atol=1E-3 ) print(F'''{mod.modelId} has passed successfully!!!''')
699
0
import json import os import shutil import tempfile import unittest from multiprocessing import get_context from pathlib import Path import datasets import numpy as np from datasets import load_dataset from parameterized import parameterized from transformers import AutoProcessor from transformers.models.wavaveca import WavaVecaCTCTokenizer, WavaVecaFeatureExtractor from transformers.models.wavaveca.tokenization_wavaveca import VOCAB_FILES_NAMES from transformers.testing_utils import require_pyctcdecode, require_torch, require_torchaudio, slow from transformers.utils import FEATURE_EXTRACTOR_NAME, is_pyctcdecode_available, is_torch_available from ..wavaveca.test_feature_extraction_wavaveca import floats_list if is_pyctcdecode_available(): from huggingface_hub import snapshot_download from pyctcdecode import BeamSearchDecoderCTC from transformers.models.wavaveca_with_lm import WavaVecaProcessorWithLM from transformers.models.wavaveca_with_lm.processing_wavaveca_with_lm import WavaVecaDecoderWithLMOutput if is_torch_available(): from transformers import WavaVecaForCTC @require_pyctcdecode class __snake_case ( unittest.TestCase ): def __a ( self ) -> Union[str, Any]: '''simple docstring''' snake_case__ : Any = '| <pad> <unk> <s> </s> a b c d e f g h i j k'.split() snake_case__ : int = dict(zip(__A , range(len(__A ) ) ) ) snake_case__ : Any = { 'unk_token': '<unk>', 'bos_token': '<s>', 'eos_token': '</s>', } snake_case__ : Dict = { 'feature_size': 1, 'padding_value': 0.0, 'sampling_rate': 16000, 'return_attention_mask': False, 'do_normalize': True, } snake_case__ : Any = tempfile.mkdtemp() snake_case__ : List[str] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['vocab_file'] ) snake_case__ : str = os.path.join(self.tmpdirname , __A ) with open(self.vocab_file , 'w' , encoding='utf-8' ) as fp: fp.write(json.dumps(__A ) + '\n' ) with open(self.feature_extraction_file , 'w' , encoding='utf-8' ) as fp: fp.write(json.dumps(__A ) + '\n' ) # load decoder from hub snake_case__ : int = 'hf-internal-testing/ngram-beam-search-decoder' def __a ( self , **__UpperCamelCase ) -> Union[str, Any]: '''simple docstring''' snake_case__ : List[str] = self.add_kwargs_tokens_map.copy() kwargs.update(__A ) return WavaVecaCTCTokenizer.from_pretrained(self.tmpdirname , **__A ) def __a ( self , **__UpperCamelCase ) -> Dict: '''simple docstring''' return WavaVecaFeatureExtractor.from_pretrained(self.tmpdirname , **__A ) def __a ( self , **__UpperCamelCase ) -> Optional[Any]: '''simple docstring''' return BeamSearchDecoderCTC.load_from_hf_hub(self.decoder_name , **__A ) def __a ( self ) -> Dict: '''simple docstring''' shutil.rmtree(self.tmpdirname ) def __a ( self ) -> Optional[int]: '''simple docstring''' snake_case__ : List[Any] = self.get_tokenizer() snake_case__ : int = self.get_feature_extractor() snake_case__ : List[str] = self.get_decoder() snake_case__ : Optional[Any] = WavaVecaProcessorWithLM(tokenizer=__A , feature_extractor=__A , decoder=__A ) processor.save_pretrained(self.tmpdirname ) snake_case__ : str = WavaVecaProcessorWithLM.from_pretrained(self.tmpdirname ) # tokenizer self.assertEqual(processor.tokenizer.get_vocab() , tokenizer.get_vocab() ) self.assertIsInstance(processor.tokenizer , __A ) # feature extractor self.assertEqual(processor.feature_extractor.to_json_string() , feature_extractor.to_json_string() ) self.assertIsInstance(processor.feature_extractor , __A ) # decoder self.assertEqual(processor.decoder._alphabet.labels , decoder._alphabet.labels ) self.assertEqual( processor.decoder.model_container[decoder._model_key]._unigram_set , decoder.model_container[decoder._model_key]._unigram_set , ) self.assertIsInstance(processor.decoder , __A ) def __a ( self ) -> List[str]: '''simple docstring''' snake_case__ : List[Any] = WavaVecaProcessorWithLM( tokenizer=self.get_tokenizer() , feature_extractor=self.get_feature_extractor() , decoder=self.get_decoder() ) processor.save_pretrained(self.tmpdirname ) # make sure that error is thrown when decoder alphabet doesn't match snake_case__ : Optional[int] = WavaVecaProcessorWithLM.from_pretrained( self.tmpdirname , alpha=5.0 , beta=3.0 , score_boundary=-7.0 , unk_score_offset=3 ) # decoder self.assertEqual(processor.language_model.alpha , 5.0 ) self.assertEqual(processor.language_model.beta , 3.0 ) self.assertEqual(processor.language_model.score_boundary , -7.0 ) self.assertEqual(processor.language_model.unk_score_offset , 3 ) def __a ( self ) -> int: '''simple docstring''' snake_case__ : int = self.get_tokenizer() # add token to trigger raise tokenizer.add_tokens(['xx'] ) with self.assertRaisesRegex(__A , 'include' ): WavaVecaProcessorWithLM( tokenizer=__A , feature_extractor=self.get_feature_extractor() , decoder=self.get_decoder() ) def __a ( self ) -> Dict: '''simple docstring''' snake_case__ : Dict = self.get_feature_extractor() snake_case__ : Any = self.get_tokenizer() snake_case__ : Dict = self.get_decoder() snake_case__ : Any = WavaVecaProcessorWithLM(tokenizer=__A , feature_extractor=__A , decoder=__A ) snake_case__ : List[str] = floats_list((3, 1000) ) snake_case__ : Tuple = feature_extractor(__A , return_tensors='np' ) snake_case__ : Union[str, Any] = processor(__A , return_tensors='np' ) for key in input_feat_extract.keys(): self.assertAlmostEqual(input_feat_extract[key].sum() , input_processor[key].sum() , delta=1E-2 ) def __a ( self ) -> Optional[int]: '''simple docstring''' snake_case__ : Any = self.get_feature_extractor() snake_case__ : Optional[int] = self.get_tokenizer() snake_case__ : Tuple = self.get_decoder() snake_case__ : Tuple = WavaVecaProcessorWithLM(tokenizer=__A , feature_extractor=__A , decoder=__A ) snake_case__ : Union[str, Any] = 'This is a test string' snake_case__ : List[str] = processor(text=__A ) snake_case__ : Union[str, Any] = tokenizer(__A ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def __a ( self , __UpperCamelCase=(2, 10, 16) , __UpperCamelCase=77 ) -> Dict: '''simple docstring''' np.random.seed(__A ) return np.random.rand(*__A ) def __a ( self ) -> int: '''simple docstring''' snake_case__ : Union[str, Any] = self.get_feature_extractor() snake_case__ : Optional[int] = self.get_tokenizer() snake_case__ : Union[str, Any] = self.get_decoder() snake_case__ : Optional[Any] = WavaVecaProcessorWithLM(tokenizer=__A , feature_extractor=__A , decoder=__A ) snake_case__ : str = self._get_dummy_logits(shape=(10, 16) , seed=13 ) snake_case__ : int = processor.decode(__A ) snake_case__ : Optional[Any] = decoder.decode_beams(__A )[0] self.assertEqual(decoded_decoder[0] , decoded_processor.text ) self.assertEqual('</s> <s> </s>' , decoded_processor.text ) self.assertEqual(decoded_decoder[-2] , decoded_processor.logit_score ) self.assertEqual(decoded_decoder[-1] , decoded_processor.lm_score ) @parameterized.expand([[None], ['fork'], ['spawn']] ) def __a ( self , __UpperCamelCase ) -> Any: '''simple docstring''' snake_case__ : int = self.get_feature_extractor() snake_case__ : Optional[Any] = self.get_tokenizer() snake_case__ : str = self.get_decoder() snake_case__ : Any = WavaVecaProcessorWithLM(tokenizer=__A , feature_extractor=__A , decoder=__A ) snake_case__ : Union[str, Any] = self._get_dummy_logits() # note: pool should be instantiated *after* Wav2Vec2ProcessorWithLM. # otherwise, the LM won't be available to the pool's sub-processes. # manual logic used to allow parameterized test for both pool=None and pool=Pool(...) if pool_context is None: snake_case__ : Union[str, Any] = processor.batch_decode(__A ) else: with get_context(__A ).Pool() as pool: snake_case__ : List[Any] = processor.batch_decode(__A , __A ) snake_case__ : Any = list(__A ) with get_context('fork' ).Pool() as p: snake_case__ : Union[str, Any] = decoder.decode_beams_batch(__A , __A ) snake_case__ , snake_case__ , snake_case__ : Tuple = [], [], [] for beams in decoded_beams: texts_decoder.append(beams[0][0] ) logit_scores_decoder.append(beams[0][-2] ) lm_scores_decoder.append(beams[0][-1] ) self.assertListEqual(__A , decoded_processor.text ) self.assertListEqual(['<s> <s> </s>', '<s> <s> <s>'] , decoded_processor.text ) self.assertListEqual(__A , decoded_processor.logit_score ) self.assertListEqual(__A , decoded_processor.lm_score ) def __a ( self ) -> Union[str, Any]: '''simple docstring''' snake_case__ : Tuple = self.get_feature_extractor() snake_case__ : Union[str, Any] = self.get_tokenizer() snake_case__ : Tuple = self.get_decoder() snake_case__ : Union[str, Any] = WavaVecaProcessorWithLM(tokenizer=__A , feature_extractor=__A , decoder=__A ) snake_case__ : Any = self._get_dummy_logits() snake_case__ : List[Any] = 15 snake_case__ : List[str] = -2_0.0 snake_case__ : str = -4.0 snake_case__ : str = processor.batch_decode( __A , beam_width=__A , beam_prune_logp=__A , token_min_logp=__A , ) snake_case__ : Optional[Any] = decoded_processor_out.text snake_case__ : str = list(__A ) with get_context('fork' ).Pool() as pool: snake_case__ : str = decoder.decode_beams_batch( __A , __A , beam_width=__A , beam_prune_logp=__A , token_min_logp=__A , ) snake_case__ : Optional[int] = [d[0][0] for d in decoded_decoder_out] snake_case__ : int = [d[0][2] for d in decoded_decoder_out] snake_case__ : Dict = [d[0][3] for d in decoded_decoder_out] self.assertListEqual(__A , __A ) self.assertListEqual(['</s> <s> <s>', '<s> <s> <s>'] , __A ) self.assertTrue(np.array_equal(__A , decoded_processor_out.logit_score ) ) self.assertTrue(np.allclose([-2_0.0_5_4, -1_8.4_4_7] , __A , atol=1E-3 ) ) self.assertTrue(np.array_equal(__A , decoded_processor_out.lm_score ) ) self.assertTrue(np.allclose([-1_5.5_5_4, -1_3.9_4_7_4] , __A , atol=1E-3 ) ) def __a ( self ) -> Any: '''simple docstring''' snake_case__ : int = self.get_feature_extractor() snake_case__ : Optional[int] = self.get_tokenizer() snake_case__ : Optional[Any] = self.get_decoder() snake_case__ : Any = WavaVecaProcessorWithLM(tokenizer=__A , feature_extractor=__A , decoder=__A ) snake_case__ : List[Any] = self._get_dummy_logits() snake_case__ : int = 2.0 snake_case__ : Union[str, Any] = 5.0 snake_case__ : List[str] = -2_0.0 snake_case__ : Optional[Any] = True snake_case__ : Union[str, Any] = processor.batch_decode( __A , alpha=__A , beta=__A , unk_score_offset=__A , lm_score_boundary=__A , ) snake_case__ : Optional[Any] = decoded_processor_out.text snake_case__ : Tuple = list(__A ) decoder.reset_params( alpha=__A , beta=__A , unk_score_offset=__A , lm_score_boundary=__A , ) with get_context('fork' ).Pool() as pool: snake_case__ : int = decoder.decode_beams_batch( __A , __A , ) snake_case__ : int = [d[0][0] for d in decoded_decoder_out] self.assertListEqual(__A , __A ) self.assertListEqual(['<s> </s> <s> </s> </s>', '</s> </s> <s> </s> </s>'] , __A ) snake_case__ : List[str] = processor.decoder.model_container[processor.decoder._model_key] self.assertEqual(lm_model.alpha , 2.0 ) self.assertEqual(lm_model.beta , 5.0 ) self.assertEqual(lm_model.unk_score_offset , -2_0.0 ) self.assertEqual(lm_model.score_boundary , __A ) def __a ( self ) -> int: '''simple docstring''' snake_case__ : Union[str, Any] = WavaVecaProcessorWithLM.from_pretrained('hf-internal-testing/processor_with_lm' ) snake_case__ : Optional[int] = processor.decoder.model_container[processor.decoder._model_key] snake_case__ : List[Any] = Path(language_model._kenlm_model.path.decode('utf-8' ) ).parent.parent.absolute() snake_case__ : str = os.listdir(__A ) snake_case__ : List[str] = ['alphabet.json', 'language_model'] downloaded_decoder_files.sort() expected_decoder_files.sort() # test that only decoder relevant files from # https://huggingface.co/hf-internal-testing/processor_with_lm/tree/main # are downloaded and none of the rest (e.g. README.md, ...) self.assertListEqual(__A , __A ) def __a ( self ) -> Dict: '''simple docstring''' snake_case__ : Tuple = snapshot_download('hf-internal-testing/processor_with_lm' ) snake_case__ : Optional[int] = WavaVecaProcessorWithLM.from_pretrained(__A ) snake_case__ : Dict = processor.decoder.model_container[processor.decoder._model_key] snake_case__ : Union[str, Any] = Path(language_model._kenlm_model.path.decode('utf-8' ) ).parent.parent.absolute() snake_case__ : int = os.listdir(__A ) snake_case__ : str = os.listdir(__A ) local_decoder_files.sort() expected_decoder_files.sort() # test that both decoder form hub and local files in cache are the same self.assertListEqual(__A , __A ) def __a ( self ) -> Optional[Any]: '''simple docstring''' snake_case__ : Dict = WavaVecaProcessorWithLM.from_pretrained('hf-internal-testing/processor_with_lm' ) snake_case__ : List[str] = AutoProcessor.from_pretrained('hf-internal-testing/processor_with_lm' ) snake_case__ : Dict = floats_list((3, 1000) ) snake_case__ : Optional[Any] = processor_wavaveca(__A , return_tensors='np' ) snake_case__ : str = processor_auto(__A , return_tensors='np' ) for key in input_wavaveca.keys(): self.assertAlmostEqual(input_wavaveca[key].sum() , input_auto[key].sum() , delta=1E-2 ) snake_case__ : Optional[Any] = self._get_dummy_logits() snake_case__ : Any = processor_wavaveca.batch_decode(__A ) snake_case__ : int = processor_auto.batch_decode(__A ) self.assertListEqual(decoded_wavaveca.text , decoded_auto.text ) def __a ( self ) -> int: '''simple docstring''' snake_case__ : int = self.get_feature_extractor() snake_case__ : List[str] = self.get_tokenizer() snake_case__ : str = self.get_decoder() snake_case__ : Optional[Any] = WavaVecaProcessorWithLM(tokenizer=__A , feature_extractor=__A , decoder=__A ) self.assertListEqual( processor.model_input_names , feature_extractor.model_input_names , msg='`processor` and `feature_extractor` model input names do not match' , ) @staticmethod def __a ( __UpperCamelCase , __UpperCamelCase ) -> int: '''simple docstring''' snake_case__ : Tuple = [d[key] for d in offsets] return retrieved_list def __a ( self ) -> List[str]: '''simple docstring''' snake_case__ : Optional[int] = WavaVecaProcessorWithLM.from_pretrained('hf-internal-testing/processor_with_lm' ) snake_case__ : List[Any] = self._get_dummy_logits()[0] snake_case__ : List[Any] = processor.decode(__A , output_word_offsets=__A ) # check Wav2Vec2CTCTokenizerOutput keys for word self.assertEqual(len(outputs.keys() ) , 4 ) self.assertTrue('text' in outputs ) self.assertTrue('word_offsets' in outputs ) self.assertTrue(isinstance(__A , __A ) ) self.assertEqual(' '.join(self.get_from_offsets(outputs['word_offsets'] , 'word' ) ) , outputs.text ) self.assertListEqual(self.get_from_offsets(outputs['word_offsets'] , 'word' ) , ['<s>', '<s>', '</s>'] ) self.assertListEqual(self.get_from_offsets(outputs['word_offsets'] , 'start_offset' ) , [0, 2, 4] ) self.assertListEqual(self.get_from_offsets(outputs['word_offsets'] , 'end_offset' ) , [1, 3, 5] ) def __a ( self ) -> List[str]: '''simple docstring''' snake_case__ : Dict = WavaVecaProcessorWithLM.from_pretrained('hf-internal-testing/processor_with_lm' ) snake_case__ : Optional[Any] = self._get_dummy_logits() snake_case__ : int = processor.batch_decode(__A , output_word_offsets=__A ) # check Wav2Vec2CTCTokenizerOutput keys for word self.assertEqual(len(outputs.keys() ) , 4 ) self.assertTrue('text' in outputs ) self.assertTrue('word_offsets' in outputs ) self.assertTrue(isinstance(__A , __A ) ) self.assertListEqual( [' '.join(self.get_from_offsets(__A , 'word' ) ) for o in outputs['word_offsets']] , outputs.text ) self.assertListEqual(self.get_from_offsets(outputs['word_offsets'][0] , 'word' ) , ['<s>', '<s>', '</s>'] ) self.assertListEqual(self.get_from_offsets(outputs['word_offsets'][0] , 'start_offset' ) , [0, 2, 4] ) self.assertListEqual(self.get_from_offsets(outputs['word_offsets'][0] , 'end_offset' ) , [1, 3, 5] ) @slow @require_torch @require_torchaudio def __a ( self ) -> Optional[int]: '''simple docstring''' import torch snake_case__ : Dict = load_dataset('common_voice' , 'en' , split='train' , streaming=__A ) snake_case__ : Any = ds.cast_column('audio' , datasets.Audio(sampling_rate=16000 ) ) snake_case__ : Tuple = iter(__A ) snake_case__ : Optional[Any] = next(__A ) snake_case__ : Optional[Any] = AutoProcessor.from_pretrained('patrickvonplaten/wav2vec2-base-100h-with-lm' ) snake_case__ : Union[str, Any] = WavaVecaForCTC.from_pretrained('patrickvonplaten/wav2vec2-base-100h-with-lm' ) # compare to filename `common_voice_en_100038.mp3` of dataset viewer on https://huggingface.co/datasets/common_voice/viewer/en/train snake_case__ : Union[str, Any] = processor(sample['audio']['array'] , return_tensors='pt' ).input_values with torch.no_grad(): snake_case__ : List[Any] = model(__A ).logits.cpu().numpy() snake_case__ : Union[str, Any] = processor.decode(logits[0] , output_word_offsets=__A ) snake_case__ : str = model.config.inputs_to_logits_ratio / processor.feature_extractor.sampling_rate snake_case__ : List[str] = [ { 'start_time': d['start_offset'] * time_offset, 'end_time': d['end_offset'] * time_offset, 'word': d['word'], } for d in output['word_offsets'] ] snake_case__ : Tuple = 'WHY DOES MILISANDRA LOOK LIKE SHE WANTS TO CONSUME JOHN SNOW ON THE RIVER AT THE WALL' # output words self.assertEqual(' '.join(self.get_from_offsets(__A , 'word' ) ) , __A ) self.assertEqual(' '.join(self.get_from_offsets(__A , 'word' ) ) , output.text ) # output times snake_case__ : List[Any] = torch.tensor(self.get_from_offsets(__A , 'start_time' ) ) snake_case__ : Any = torch.tensor(self.get_from_offsets(__A , 'end_time' ) ) # fmt: off snake_case__ : Dict = torch.tensor([1.4_1_9_9, 1.6_5_9_9, 2.2_5_9_9, 3.0, 3.2_4, 3.5_9_9_9, 3.7_9_9_9, 4.0_9_9_9, 4.2_6, 4.9_4, 5.2_8, 5.6_5_9_9, 5.7_8, 5.9_4, 6.3_2, 6.5_3_9_9, 6.6_5_9_9] ) snake_case__ : Tuple = torch.tensor([1.5_3_9_9, 1.8_9_9_9, 2.9, 3.1_6, 3.5_3_9_9, 3.7_2, 4.0_1_9_9, 4.1_7_9_9, 4.7_6, 5.1_5_9_9, 5.5_5_9_9, 5.6_9_9_9, 5.8_6, 6.1_9_9_9, 6.3_8, 6.6_1_9_9, 6.9_4] ) # fmt: on self.assertTrue(torch.allclose(__A , __A , atol=0.0_1 ) ) self.assertTrue(torch.allclose(__A , __A , atol=0.0_1 ) )
718
import warnings from ...utils import logging from .image_processing_perceiver import PerceiverImageProcessor lowerCAmelCase__ : Dict = logging.get_logger(__name__) class __snake_case ( _lowerCamelCase ): def __init__( self , *__UpperCamelCase , **__UpperCamelCase ) -> None: '''simple docstring''' warnings.warn( 'The class PerceiverFeatureExtractor is deprecated and will be removed in version 5 of Transformers.' ' Please use PerceiverImageProcessor instead.' , __UpperCamelCase , ) super().__init__(*__UpperCamelCase , **__UpperCamelCase )
699
0
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 UpperCamelCase__ ( A__ ) -> Any: # picklable for multiprocessing return i + 1 @require_dill_gt_0_3_2 @require_joblibspark @require_not_windows def UpperCamelCase__ ( ) -> Any: with parallel_backend('spark' ): assert ParallelBackendConfig.backend_name == "spark" snake_case__ : Dict = [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 UpperCamelCase__ ( A__ ) -> str: snake_case__ : str = [1, 2] snake_case__ : List[str] = {"""a""": 1, """b""": 2} snake_case__ : int = {"""a""": [1, 2], """b""": [3, 4]} snake_case__ : str = {"""a""": {"""1""": 1}, """b""": 2} snake_case__ : Optional[Any] = {"""a""": 1, """b""": 2, """c""": 3, """d""": 4} snake_case__ : Tuple = [2, 3] snake_case__ : str = {"""a""": 2, """b""": 3} snake_case__ : Optional[int] = {"""a""": [2, 3], """b""": [4, 5]} snake_case__ : int = {"""a""": {"""1""": 2}, """b""": 3} snake_case__ : Union[str, Any] = {"""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
719
import io import itertools import json from dataclasses import dataclass from typing import Optional import pyarrow as pa import pyarrow.json as paj import datasets from datasets.table import table_cast from datasets.utils.file_utils import readline lowerCAmelCase__ : List[Any] = datasets.utils.logging.get_logger(__name__) @dataclass class __snake_case ( datasets.BuilderConfig ): __lowerCamelCase = None __lowerCamelCase = "utf-8" __lowerCamelCase = None __lowerCamelCase = None __lowerCamelCase = True # deprecated __lowerCamelCase = None # deprecated __lowerCamelCase = 10 << 20 # 10MB __lowerCamelCase = None class __snake_case ( datasets.ArrowBasedBuilder ): __lowerCamelCase = JsonConfig def __a ( self ) -> Optional[Any]: '''simple docstring''' if self.config.block_size is not None: logger.warning('The JSON loader parameter `block_size` is deprecated. Please use `chunksize` instead' ) snake_case__ : str = self.config.block_size if self.config.use_threads is not True: logger.warning( 'The JSON loader parameter `use_threads` is deprecated and doesn\'t have any effect anymore.' ) if self.config.newlines_in_values is not None: raise ValueError('The JSON loader parameter `newlines_in_values` is no longer supported' ) return datasets.DatasetInfo(features=self.config.features ) def __a ( self , __UpperCamelCase ) -> Dict: '''simple docstring''' 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}""" ) snake_case__ : int = dl_manager.download_and_extract(self.config.data_files ) if isinstance(__UpperCamelCase , (str, list, tuple) ): snake_case__ : Any = data_files if isinstance(__UpperCamelCase , __UpperCamelCase ): snake_case__ : Optional[Any] = [files] snake_case__ : List[str] = [dl_manager.iter_files(__UpperCamelCase ) for file in files] return [datasets.SplitGenerator(name=datasets.Split.TRAIN , gen_kwargs={'files': files} )] snake_case__ : List[Any] = [] for split_name, files in data_files.items(): if isinstance(__UpperCamelCase , __UpperCamelCase ): snake_case__ : List[Any] = [files] snake_case__ : Any = [dl_manager.iter_files(__UpperCamelCase ) for file in files] splits.append(datasets.SplitGenerator(name=__UpperCamelCase , gen_kwargs={'files': files} ) ) return splits def __a ( self , __UpperCamelCase ) -> pa.Table: '''simple docstring''' if self.config.features is not None: # adding missing columns for column_name in set(self.config.features ) - set(pa_table.column_names ): snake_case__ : List[Any] = self.config.features.arrow_schema.field(__UpperCamelCase ).type snake_case__ : List[str] = pa_table.append_column(__UpperCamelCase , pa.array([None] * len(__UpperCamelCase ) , type=__UpperCamelCase ) ) # more expensive cast to support nested structures with keys in a different order # allows str <-> int/float or str to Audio for example snake_case__ : List[str] = table_cast(__UpperCamelCase , self.config.features.arrow_schema ) return pa_table def __a ( self , __UpperCamelCase ) -> int: '''simple docstring''' for file_idx, file in enumerate(itertools.chain.from_iterable(__UpperCamelCase ) ): # If the file is one json object and if we need to look at the list of items in one specific field if self.config.field is not None: with open(__UpperCamelCase , encoding=self.config.encoding , errors=self.config.encoding_errors ) as f: snake_case__ : Union[str, Any] = json.load(__UpperCamelCase ) # We keep only the field we are interested in snake_case__ : Tuple = dataset[self.config.field] # We accept two format: a list of dicts or a dict of lists if isinstance(__UpperCamelCase , (list, tuple) ): snake_case__ : List[Any] = set().union(*[row.keys() for row in dataset] ) snake_case__ : List[Any] = {col: [row.get(__UpperCamelCase ) for row in dataset] for col in keys} else: snake_case__ : List[Any] = dataset snake_case__ : Dict = pa.Table.from_pydict(__UpperCamelCase ) yield file_idx, self._cast_table(__UpperCamelCase ) # If the file has one json object per line else: with open(__UpperCamelCase , 'rb' ) as f: snake_case__ : Optional[int] = 0 # Use block_size equal to the chunk size divided by 32 to leverage multithreading # Set a default minimum value of 16kB if the chunk size is really small snake_case__ : Tuple = max(self.config.chunksize // 32 , 16 << 10 ) snake_case__ : Optional[Any] = ( self.config.encoding_errors if self.config.encoding_errors is not None else 'strict' ) while True: snake_case__ : Optional[int] = f.read(self.config.chunksize ) if not batch: break # Finish current line try: batch += f.readline() except (AttributeError, io.UnsupportedOperation): batch += readline(__UpperCamelCase ) # PyArrow only accepts utf-8 encoded bytes if self.config.encoding != "utf-8": snake_case__ : int = batch.decode(self.config.encoding , errors=__UpperCamelCase ).encode('utf-8' ) try: while True: try: snake_case__ : List[str] = paj.read_json( io.BytesIO(__UpperCamelCase ) , read_options=paj.ReadOptions(block_size=__UpperCamelCase ) ) break except (pa.ArrowInvalid, pa.ArrowNotImplementedError) as e: if ( isinstance(__UpperCamelCase , pa.ArrowInvalid ) and "straddling" not in str(__UpperCamelCase ) or block_size > len(__UpperCamelCase ) ): raise else: # Increase the block size in case it was too small. # The block size will be reset for the next file. logger.debug( F"""Batch of {len(__UpperCamelCase )} bytes couldn't be parsed with block_size={block_size}. Retrying with block_size={block_size * 2}.""" ) block_size *= 2 except pa.ArrowInvalid as e: try: with open( __UpperCamelCase , encoding=self.config.encoding , errors=self.config.encoding_errors ) as f: snake_case__ : Tuple = json.load(__UpperCamelCase ) except json.JSONDecodeError: logger.error(F"""Failed to read file '{file}' with error {type(__UpperCamelCase )}: {e}""" ) raise e # If possible, parse the file as a list of json objects and exit the loop if isinstance(__UpperCamelCase , __UpperCamelCase ): # list is the only sequence type supported in JSON try: snake_case__ : str = set().union(*[row.keys() for row in dataset] ) snake_case__ : Union[str, Any] = {col: [row.get(__UpperCamelCase ) for row in dataset] for col in keys} snake_case__ : List[str] = pa.Table.from_pydict(__UpperCamelCase ) except (pa.ArrowInvalid, AttributeError) as e: logger.error(F"""Failed to read file '{file}' with error {type(__UpperCamelCase )}: {e}""" ) raise ValueError(F"""Not able to read records in the JSON file at {file}.""" ) from None yield file_idx, self._cast_table(__UpperCamelCase ) break else: logger.error(F"""Failed to read file '{file}' with error {type(__UpperCamelCase )}: {e}""" ) raise ValueError( F"""Not able to read records in the JSON file at {file}. """ F"""You should probably indicate the field of the JSON file containing your records. """ F"""This JSON file contain the following fields: {str(list(dataset.keys() ) )}. """ F"""Select the correct one and provide it as `field='XXX'` to the dataset loading method. """ ) from None # Uncomment for debugging (will print the Arrow table size and elements) # logger.warning(f"pa_table: {pa_table} num rows: {pa_table.num_rows}") # logger.warning('\n'.join(str(pa_table.slice(i, 1).to_pydict()) for i in range(pa_table.num_rows))) yield (file_idx, batch_idx), self._cast_table(__UpperCamelCase ) batch_idx += 1
699
0
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging lowerCAmelCase__ : Union[str, Any] = logging.get_logger(__name__) lowerCAmelCase__ : Optional[Any] = { 'xlm-roberta-base': 'https://huggingface.co/xlm-roberta-base/resolve/main/config.json', 'xlm-roberta-large': 'https://huggingface.co/xlm-roberta-large/resolve/main/config.json', 'xlm-roberta-large-finetuned-conll02-dutch': ( 'https://huggingface.co/xlm-roberta-large-finetuned-conll02-dutch/resolve/main/config.json' ), 'xlm-roberta-large-finetuned-conll02-spanish': ( 'https://huggingface.co/xlm-roberta-large-finetuned-conll02-spanish/resolve/main/config.json' ), 'xlm-roberta-large-finetuned-conll03-english': ( 'https://huggingface.co/xlm-roberta-large-finetuned-conll03-english/resolve/main/config.json' ), 'xlm-roberta-large-finetuned-conll03-german': ( 'https://huggingface.co/xlm-roberta-large-finetuned-conll03-german/resolve/main/config.json' ), } class __snake_case ( _lowerCamelCase ): __lowerCamelCase = '''xlm-roberta''' def __init__( self , __UpperCamelCase=30522 , __UpperCamelCase=768 , __UpperCamelCase=12 , __UpperCamelCase=12 , __UpperCamelCase=3072 , __UpperCamelCase="gelu" , __UpperCamelCase=0.1 , __UpperCamelCase=0.1 , __UpperCamelCase=512 , __UpperCamelCase=2 , __UpperCamelCase=0.0_2 , __UpperCamelCase=1E-12 , __UpperCamelCase=1 , __UpperCamelCase=0 , __UpperCamelCase=2 , __UpperCamelCase="absolute" , __UpperCamelCase=True , __UpperCamelCase=None , **__UpperCamelCase , ) -> Dict: '''simple docstring''' super().__init__(pad_token_id=A__ , bos_token_id=A__ , eos_token_id=A__ , **A__ ) snake_case__ : Optional[int] = vocab_size snake_case__ : Union[str, Any] = hidden_size snake_case__ : str = num_hidden_layers snake_case__ : List[str] = num_attention_heads snake_case__ : Tuple = hidden_act snake_case__ : Optional[int] = intermediate_size snake_case__ : Tuple = hidden_dropout_prob snake_case__ : Optional[int] = attention_probs_dropout_prob snake_case__ : List[Any] = max_position_embeddings snake_case__ : Any = type_vocab_size snake_case__ : Tuple = initializer_range snake_case__ : Dict = layer_norm_eps snake_case__ : List[str] = position_embedding_type snake_case__ : Union[str, Any] = use_cache snake_case__ : Union[str, Any] = classifier_dropout class __snake_case ( _lowerCamelCase ): @property def __a ( self ) -> List[Any]: '''simple docstring''' if self.task == "multiple-choice": snake_case__ : Optional[int] = {0: """batch""", 1: """choice""", 2: """sequence"""} else: snake_case__ : Dict = {0: """batch""", 1: """sequence"""} return OrderedDict( [ ('input_ids', dynamic_axis), ('attention_mask', dynamic_axis), ] )
720
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__ : Any = {'''configuration_xglm''': ['''XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''XGLMConfig''']} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : str = ['''XGLMTokenizer'''] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : Dict = ['''XGLMTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : Optional[int] = [ '''XGLM_PRETRAINED_MODEL_ARCHIVE_LIST''', '''XGLMForCausalLM''', '''XGLMModel''', '''XGLMPreTrainedModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : Dict = [ '''FlaxXGLMForCausalLM''', '''FlaxXGLMModel''', '''FlaxXGLMPreTrainedModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : Dict = [ '''TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFXGLMForCausalLM''', '''TFXGLMModel''', '''TFXGLMPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_xglm import XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XGLMConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xglm import XGLMTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xglm_fast import XGLMTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xglm import XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, XGLMForCausalLM, XGLMModel, XGLMPreTrainedModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_xglm import FlaxXGLMForCausalLM, FlaxXGLMModel, FlaxXGLMPreTrainedModel try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_xglm import ( TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFXGLMForCausalLM, TFXGLMModel, TFXGLMPreTrainedModel, ) else: import sys lowerCAmelCase__ : Tuple = _LazyModule(__name__, globals()['''__file__'''], _import_structure)
699
0
from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase__ : Dict = logging.get_logger(__name__) lowerCAmelCase__ : int = { '''s-JoL/Open-Llama-V1''': '''https://huggingface.co/s-JoL/Open-Llama-V1/blob/main/config.json''', } class __snake_case ( __SCREAMING_SNAKE_CASE ): __lowerCamelCase = 'open-llama' def __init__( self , __UpperCamelCase=100000 , __UpperCamelCase=4096 , __UpperCamelCase=11008 , __UpperCamelCase=32 , __UpperCamelCase=32 , __UpperCamelCase="silu" , __UpperCamelCase=2048 , __UpperCamelCase=0.0_2 , __UpperCamelCase=1E-6 , __UpperCamelCase=True , __UpperCamelCase=0 , __UpperCamelCase=1 , __UpperCamelCase=2 , __UpperCamelCase=False , __UpperCamelCase=True , __UpperCamelCase=0.1 , __UpperCamelCase=0.1 , __UpperCamelCase=True , __UpperCamelCase=True , __UpperCamelCase=None , **__UpperCamelCase , ) -> str: '''simple docstring''' snake_case__ : Optional[int] = vocab_size snake_case__ : Tuple = max_position_embeddings snake_case__ : Any = hidden_size snake_case__ : Optional[int] = intermediate_size snake_case__ : Optional[int] = num_hidden_layers snake_case__ : List[str] = num_attention_heads snake_case__ : Optional[int] = hidden_act snake_case__ : Optional[int] = initializer_range snake_case__ : int = rms_norm_eps snake_case__ : Tuple = use_cache snake_case__ : str = kwargs.pop( 'use_memorry_efficient_attention' , _a ) snake_case__ : Any = hidden_dropout_prob snake_case__ : List[Any] = attention_dropout_prob snake_case__ : Optional[int] = use_stable_embedding snake_case__ : Optional[int] = shared_input_output_embedding snake_case__ : Tuple = rope_scaling self._rope_scaling_validation() super().__init__( pad_token_id=_a , bos_token_id=_a , eos_token_id=_a , tie_word_embeddings=_a , **_a , ) def __a ( self ) -> int: '''simple docstring''' if self.rope_scaling is None: return if not isinstance(self.rope_scaling , _a ) or len(self.rope_scaling ) != 2: raise ValueError( '`rope_scaling` must be a dictionary with with two fields, `name` and `factor`, ' F"""got {self.rope_scaling}""" ) snake_case__ : Any = self.rope_scaling.get('type' , _a ) snake_case__ : int = self.rope_scaling.get('factor' , _a ) if rope_scaling_type is None or rope_scaling_type not in ["linear", "dynamic"]: raise ValueError( F"""`rope_scaling`\'s name field must be one of [\'linear\', \'dynamic\'], got {rope_scaling_type}""" ) if rope_scaling_factor is None or not isinstance(_a , _a ) or rope_scaling_factor <= 1.0: raise ValueError(F"""`rope_scaling`\'s factor field must be an float > 1, got {rope_scaling_factor}""" )
721
from __future__ import annotations import random # Maximum size of the population. Bigger could be faster but is more memory expensive. lowerCAmelCase__ : Dict = 2_00 # Number of elements selected in every generation of evolution. The selection takes # place from best to worst of that generation and must be smaller than N_POPULATION. lowerCAmelCase__ : List[str] = 50 # Probability that an element of a generation can mutate, changing one of its genes. # This will guarantee that all genes will be used during evolution. lowerCAmelCase__ : List[str] = 0.4 # Just a seed to improve randomness required by the algorithm. random.seed(random.randint(0, 10_00)) def UpperCamelCase__ ( A__ , A__ ) -> tuple[str, float]: snake_case__ : Tuple = len([g for position, g in enumerate(A__ ) if g == main_target[position]] ) return (item, float(A__ )) def UpperCamelCase__ ( A__ , A__ ) -> tuple[str, str]: snake_case__ : str = random.randint(0 , len(A__ ) - 1 ) snake_case__ : int = parent_a[:random_slice] + parent_a[random_slice:] snake_case__ : Any = parent_a[:random_slice] + parent_a[random_slice:] return (child_a, child_a) def UpperCamelCase__ ( A__ , A__ ) -> str: snake_case__ : List[Any] = list(A__ ) if random.uniform(0 , 1 ) < MUTATION_PROBABILITY: snake_case__ : Optional[Any] = random.choice(A__ ) return "".join(A__ ) def UpperCamelCase__ ( A__ , A__ , A__ , ) -> list[str]: snake_case__ : Tuple = [] # Generate more children proportionally to the fitness score. snake_case__ : Optional[Any] = int(parent_a[1] * 100 ) + 1 snake_case__ : str = 10 if child_n >= 10 else child_n for _ in range(A__ ): snake_case__ : Any = population_score[random.randint(0 , A__ )][0] snake_case__ , snake_case__ : int = crossover(parent_a[0] , A__ ) # Append new string to the population list. pop.append(mutate(A__ , A__ ) ) pop.append(mutate(A__ , A__ ) ) return pop def UpperCamelCase__ ( A__ , A__ , A__ = True ) -> tuple[int, int, str]: # Verify if N_POPULATION is bigger than N_SELECTED if N_POPULATION < N_SELECTED: snake_case__ : Union[str, Any] = F"""{N_POPULATION} must be bigger than {N_SELECTED}""" raise ValueError(A__ ) # Verify that the target contains no genes besides the ones inside genes variable. snake_case__ : Tuple = sorted({c for c in target if c not in genes} ) if not_in_genes_list: snake_case__ : int = F"""{not_in_genes_list} is not in genes list, evolution cannot converge""" raise ValueError(A__ ) # Generate random starting population. snake_case__ : Union[str, Any] = [] for _ in range(A__ ): population.append(''.join([random.choice(A__ ) for i in range(len(A__ ) )] ) ) # Just some logs to know what the algorithms is doing. snake_case__ , snake_case__ : str = 0, 0 # This loop will end when we find a perfect match for our target. while True: generation += 1 total_population += len(A__ ) # Random population created. Now it's time to evaluate. # Adding a bit of concurrency can make everything faster, # # import concurrent.futures # population_score: list[tuple[str, float]] = [] # with concurrent.futures.ThreadPoolExecutor( # max_workers=NUM_WORKERS) as executor: # futures = {executor.submit(evaluate, item) for item in population} # concurrent.futures.wait(futures) # population_score = [item.result() for item in futures] # # but with a simple algorithm like this, it will probably be slower. # We just need to call evaluate for every item inside the population. snake_case__ : List[Any] = [evaluate(A__ , A__ ) for item in population] # Check if there is a matching evolution. snake_case__ : int = sorted(A__ , key=lambda A__ : x[1] , reverse=A__ ) if population_score[0][0] == target: return (generation, total_population, population_score[0][0]) # Print the best result every 10 generation. # Just to know that the algorithm is working. if debug and generation % 10 == 0: print( F"""\nGeneration: {generation}""" F"""\nTotal Population:{total_population}""" F"""\nBest score: {population_score[0][1]}""" F"""\nBest string: {population_score[0][0]}""" ) # Flush the old population, keeping some of the best evolutions. # Keeping this avoid regression of evolution. snake_case__ : Optional[int] = population[: int(N_POPULATION / 3 )] population.clear() population.extend(A__ ) # Normalize population score to be between 0 and 1. snake_case__ : str = [ (item, score / len(A__ )) for item, score in population_score ] # This is selection for i in range(A__ ): population.extend(select(population_score[int(A__ )] , A__ , A__ ) ) # Check if the population has already reached the maximum value and if so, # break the cycle. If this check is disabled, the algorithm will take # forever to compute large strings, but will also calculate small strings in # a far fewer generations. if len(A__ ) > N_POPULATION: break if __name__ == "__main__": lowerCAmelCase__ : str = ( '''This is a genetic algorithm to evaluate, combine, evolve, and mutate a string!''' ) lowerCAmelCase__ : Optional[Any] = list( ''' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklm''' '''nopqrstuvwxyz.,;!?+-*#@^\'èéòà€ù=)(&%$£/\\''' ) lowerCAmelCase__, lowerCAmelCase__, lowerCAmelCase__ : List[str] = basic(target_str, genes_list) print( F'''\nGeneration: {generation}\nTotal Population: {population}\nTarget: {target}''' )
699
0
from typing import Any def UpperCamelCase__ ( A__ ) -> Union[str, Any]: if not input_list: return [] snake_case__ : Union[str, Any] = [input_list.count(_lowerCamelCase ) for value in input_list] snake_case__ : Optional[Any] = max(_lowerCamelCase ) # Gets the maximum count in the input list. # Gets values of modes return sorted({input_list[i] for i, value in enumerate(_lowerCamelCase ) if value == y} ) if __name__ == "__main__": import doctest doctest.testmod()
700
from __future__ import annotations from collections.abc import Iterator from typing import Generic, TypeVar lowerCAmelCase__ : Optional[int] = TypeVar('''T''') class __snake_case ( Generic[T] ): def __init__( self , __UpperCamelCase ) -> Any: '''simple docstring''' snake_case__ : Optional[int] = data snake_case__ : Node[T] | None = None def __str__( self ) -> str: '''simple docstring''' return F"""{self.data}""" class __snake_case ( Generic[T] ): def __init__( self ) -> None: '''simple docstring''' snake_case__ : Node[T] | None = None def __iter__( self ) -> Iterator[T]: '''simple docstring''' snake_case__ : str = self.top while node: yield node.data snake_case__ : Dict = node.next def __str__( self ) -> str: '''simple docstring''' return "->".join([str(__UpperCamelCase ) for item in self] ) def __len__( self ) -> int: '''simple docstring''' return len(tuple(iter(self ) ) ) def __a ( self ) -> bool: '''simple docstring''' return self.top is None def __a ( self , __UpperCamelCase ) -> None: '''simple docstring''' snake_case__ : str = Node(__UpperCamelCase ) if not self.is_empty(): snake_case__ : List[str] = self.top snake_case__ : Tuple = node def __a ( self ) -> T: '''simple docstring''' if self.is_empty(): raise IndexError('pop from empty stack' ) assert isinstance(self.top , __UpperCamelCase ) snake_case__ : List[str] = self.top snake_case__ : Union[str, Any] = self.top.next return pop_node.data def __a ( self ) -> T: '''simple docstring''' if self.is_empty(): raise IndexError('peek from empty stack' ) assert self.top is not None return self.top.data def __a ( self ) -> None: '''simple docstring''' snake_case__ : Any = None if __name__ == "__main__": from doctest import testmod testmod()
699
0
import sys from collections import defaultdict class __snake_case : def __init__( self ) -> Optional[Any]: '''simple docstring''' snake_case__ : Tuple = [] def __a ( self , __UpperCamelCase ) -> Any: '''simple docstring''' return self.node_position[vertex] def __a ( self , __UpperCamelCase , __UpperCamelCase ) -> Optional[Any]: '''simple docstring''' snake_case__ : Tuple = pos def __a ( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> Tuple: '''simple docstring''' if start > size // 2 - 1: return else: if 2 * start + 2 >= size: snake_case__ : int = 2 * start + 1 else: if heap[2 * start + 1] < heap[2 * start + 2]: snake_case__ : Optional[Any] = 2 * start + 1 else: snake_case__ : Dict = 2 * start + 2 if heap[smallest_child] < heap[start]: snake_case__ , snake_case__ : Dict = heap[smallest_child], positions[smallest_child] snake_case__ , snake_case__ : Union[str, Any] = ( heap[start], positions[start], ) snake_case__ , snake_case__ : Optional[int] = temp, tempa snake_case__ : int = self.get_position(positions[smallest_child] ) self.set_position( positions[smallest_child] , self.get_position(positions[start] ) ) self.set_position(positions[start] , __UpperCamelCase ) self.top_to_bottom(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) def __a ( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> List[str]: '''simple docstring''' snake_case__ : Optional[Any] = position[index] while index != 0: snake_case__ : List[Any] = int((index - 2) / 2 ) if index % 2 == 0 else int((index - 1) / 2 ) if val < heap[parent]: snake_case__ : int = heap[parent] snake_case__ : Tuple = position[parent] self.set_position(position[parent] , __UpperCamelCase ) else: snake_case__ : Optional[int] = val snake_case__ : List[Any] = temp self.set_position(__UpperCamelCase , __UpperCamelCase ) break snake_case__ : Tuple = parent else: snake_case__ : str = val snake_case__ : Any = temp self.set_position(__UpperCamelCase , 0 ) def __a ( self , __UpperCamelCase , __UpperCamelCase ) -> Any: '''simple docstring''' snake_case__ : Optional[Any] = len(__UpperCamelCase ) // 2 - 1 for i in range(__UpperCamelCase , -1 , -1 ): self.top_to_bottom(__UpperCamelCase , __UpperCamelCase , len(__UpperCamelCase ) , __UpperCamelCase ) def __a ( self , __UpperCamelCase , __UpperCamelCase ) -> List[str]: '''simple docstring''' snake_case__ : Dict = positions[0] snake_case__ : Dict = sys.maxsize self.top_to_bottom(__UpperCamelCase , 0 , len(__UpperCamelCase ) , __UpperCamelCase ) return temp def UpperCamelCase__ ( A__ ) -> int: snake_case__ : List[str] = Heap() snake_case__ : Optional[int] = [0] * len(A__ ) snake_case__ : str = [-1] * len(A__ ) # Neighboring Tree Vertex of selected vertex # Minimum Distance of explored vertex with neighboring vertex of partial tree # formed in graph snake_case__ : Any = [] # Heap of Distance of vertices from their neighboring vertex snake_case__ : Tuple = [] for vertex in range(len(A__ ) ): distance_tv.append(sys.maxsize ) positions.append(A__ ) heap.node_position.append(A__ ) snake_case__ : List[str] = [] snake_case__ : int = 1 snake_case__ : str = sys.maxsize for neighbor, distance in adjacency_list[0]: snake_case__ : Tuple = 0 snake_case__ : List[str] = distance heap.heapify(A__ , A__ ) for _ in range(1 , len(A__ ) ): snake_case__ : Dict = heap.delete_minimum(A__ , A__ ) if visited[vertex] == 0: tree_edges.append((nbr_tv[vertex], vertex) ) snake_case__ : Optional[int] = 1 for neighbor, distance in adjacency_list[vertex]: if ( visited[neighbor] == 0 and distance < distance_tv[heap.get_position(A__ )] ): snake_case__ : List[str] = distance heap.bottom_to_top( A__ , heap.get_position(A__ ) , A__ , A__ ) snake_case__ : Optional[int] = vertex return tree_edges if __name__ == "__main__": # pragma: no cover # < --------- Prims Algorithm --------- > lowerCAmelCase__ : str = int(input('''Enter number of edges: ''').strip()) lowerCAmelCase__ : List[str] = defaultdict(list) for _ in range(edges_number): lowerCAmelCase__ : Tuple = [int(x) for x in input().strip().split()] adjacency_list[edge[0]].append([edge[1], edge[2]]) adjacency_list[edge[1]].append([edge[0], edge[2]]) print(prisms_algorithm(adjacency_list))
701
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__ : Dict = logging.get_logger(__name__) lowerCAmelCase__ : int = { '''sail/poolformer_s12''': '''https://huggingface.co/sail/poolformer_s12/resolve/main/config.json''', # See all PoolFormer models at https://huggingface.co/models?filter=poolformer } class __snake_case ( _lowerCamelCase ): __lowerCamelCase = """poolformer""" def __init__( self , __UpperCamelCase=3 , __UpperCamelCase=16 , __UpperCamelCase=16 , __UpperCamelCase=3 , __UpperCamelCase=4.0 , __UpperCamelCase=[2, 2, 6, 2] , __UpperCamelCase=[64, 128, 320, 512] , __UpperCamelCase=[7, 3, 3, 3] , __UpperCamelCase=[4, 2, 2, 2] , __UpperCamelCase=[2, 1, 1, 1] , __UpperCamelCase=4 , __UpperCamelCase=0.0 , __UpperCamelCase="gelu" , __UpperCamelCase=True , __UpperCamelCase=1E-5 , __UpperCamelCase=0.0_2 , **__UpperCamelCase , ) -> Any: '''simple docstring''' snake_case__ : List[str] = num_channels snake_case__ : Dict = patch_size snake_case__ : Optional[int] = stride snake_case__ : str = padding snake_case__ : List[str] = pool_size snake_case__ : List[Any] = hidden_sizes snake_case__ : List[Any] = mlp_ratio snake_case__ : Union[str, Any] = depths snake_case__ : Dict = patch_sizes snake_case__ : Dict = strides snake_case__ : Dict = num_encoder_blocks snake_case__ : Union[str, Any] = drop_path_rate snake_case__ : List[str] = hidden_act snake_case__ : Optional[Any] = use_layer_scale snake_case__ : int = layer_scale_init_value snake_case__ : Dict = initializer_range super().__init__(**__UpperCamelCase ) class __snake_case ( _lowerCamelCase ): __lowerCamelCase = version.parse("""1.11""" ) @property def __a ( self ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' return OrderedDict( [ ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}), ] ) @property def __a ( self ) -> float: '''simple docstring''' return 2E-3
699
0
import argparse import gc import json import os import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType from accelerate.utils.deepspeed import DummyOptim, DummyScheduler lowerCAmelCase__ : Optional[int] = 16 lowerCAmelCase__ : Any = 32 def UpperCamelCase__ ( A__ ) -> str: return int(x / 2**20 ) class __snake_case : def __enter__( self ) -> List[Any]: '''simple docstring''' gc.collect() torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() # reset the peak gauge to zero snake_case__ : int = torch.cuda.memory_allocated() return self def __exit__( self , *__UpperCamelCase ) -> List[Any]: '''simple docstring''' gc.collect() torch.cuda.empty_cache() snake_case__ : Tuple = torch.cuda.memory_allocated() snake_case__ : Any = torch.cuda.max_memory_allocated() snake_case__ : List[str] = bamb(self.end - self.begin ) snake_case__ : Tuple = bamb(self.peak - self.begin ) # print(f"delta used/peak {self.used:4d}/{self.peaked:4d}") def UpperCamelCase__ ( A__ , A__ = 16 , A__ = "bert-base-cased" , A__ = 320 , A__ = 160 , ) -> Any: snake_case__ : Union[str, Any] = AutoTokenizer.from_pretrained(SCREAMING_SNAKE_CASE_ ) snake_case__ : List[str] = load_dataset( 'glue' , 'mrpc' , split={'train': F"""train[:{n_train}]""", 'validation': F"""validation[:{n_val}]"""} ) def tokenize_function(A__ ): # max_length=None => use the model max length (it's actually the default) snake_case__ : List[Any] = tokenizer(examples['sentence1'] , examples['sentence2'] , truncation=SCREAMING_SNAKE_CASE_ , max_length=SCREAMING_SNAKE_CASE_ ) return outputs # Apply the method we just defined to all the examples in all the splits of the dataset snake_case__ : Tuple = datasets.map( SCREAMING_SNAKE_CASE_ , batched=SCREAMING_SNAKE_CASE_ , remove_columns=['idx', 'sentence1', 'sentence2'] , load_from_cache_file=SCREAMING_SNAKE_CASE_ ) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library snake_case__ : Dict = tokenized_datasets.rename_column('label' , 'labels' ) def collate_fn(A__ ): # On TPU it's best to pad everything to the same length or training will be very slow. if accelerator.distributed_type == DistributedType.TPU: return tokenizer.pad(SCREAMING_SNAKE_CASE_ , padding='max_length' , max_length=128 , return_tensors='pt' ) return tokenizer.pad(SCREAMING_SNAKE_CASE_ , padding='longest' , return_tensors='pt' ) # Instantiate dataloaders. snake_case__ : List[str] = DataLoader( tokenized_datasets['train'] , shuffle=SCREAMING_SNAKE_CASE_ , collate_fn=SCREAMING_SNAKE_CASE_ , batch_size=SCREAMING_SNAKE_CASE_ ) snake_case__ : str = DataLoader( tokenized_datasets['validation'] , shuffle=SCREAMING_SNAKE_CASE_ , collate_fn=SCREAMING_SNAKE_CASE_ , batch_size=SCREAMING_SNAKE_CASE_ ) return train_dataloader, eval_dataloader def UpperCamelCase__ ( A__ , A__ ) -> Union[str, Any]: snake_case__ : List[str] = Accelerator() # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs snake_case__ : str = config["lr"] snake_case__ : Optional[int] = int(config['num_epochs'] ) snake_case__ : Dict = int(config['seed'] ) snake_case__ : Tuple = int(config['batch_size'] ) snake_case__ : int = args.model_name_or_path set_seed(SCREAMING_SNAKE_CASE_ ) snake_case__ : Union[str, Any] = get_dataloaders(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , args.n_train , args.n_val ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) snake_case__ : Union[str, Any] = AutoModelForSequenceClassification.from_pretrained(SCREAMING_SNAKE_CASE_ , return_dict=SCREAMING_SNAKE_CASE_ ) # Instantiate optimizer snake_case__ : Union[str, Any] = ( AdamW if accelerator.state.deepspeed_plugin is None or "optimizer" not in accelerator.state.deepspeed_plugin.deepspeed_config else DummyOptim ) snake_case__ : Dict = optimizer_cls(params=model.parameters() , lr=SCREAMING_SNAKE_CASE_ ) if accelerator.state.deepspeed_plugin is not None: snake_case__ : int = accelerator.state.deepspeed_plugin.deepspeed_config[ "gradient_accumulation_steps" ] else: snake_case__ : Optional[Any] = 1 snake_case__ : Union[str, Any] = (len(SCREAMING_SNAKE_CASE_ ) * num_epochs) // gradient_accumulation_steps # Instantiate scheduler if ( accelerator.state.deepspeed_plugin is None or "scheduler" not in accelerator.state.deepspeed_plugin.deepspeed_config ): snake_case__ : str = get_linear_schedule_with_warmup( optimizer=SCREAMING_SNAKE_CASE_ , num_warmup_steps=0 , num_training_steps=SCREAMING_SNAKE_CASE_ , ) else: snake_case__ : List[str] = DummyScheduler(SCREAMING_SNAKE_CASE_ , total_num_steps=SCREAMING_SNAKE_CASE_ , warmup_num_steps=0 ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. snake_case__ : int = accelerator.prepare( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) # We need to keep track of how many total steps we have iterated over snake_case__ : List[str] = 0 # We also need to keep track of the stating epoch so files are named properly snake_case__ : Optional[Any] = 0 # Now we train the model snake_case__ : List[str] = {} for epoch in range(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): with TorchTracemalloc() as tracemalloc: model.train() for step, batch in enumerate(SCREAMING_SNAKE_CASE_ ): snake_case__ : Optional[int] = model(**SCREAMING_SNAKE_CASE_ ) snake_case__ : List[str] = outputs.loss snake_case__ : Union[str, Any] = loss / gradient_accumulation_steps accelerator.backward(SCREAMING_SNAKE_CASE_ ) if step % gradient_accumulation_steps == 0: optimizer.step() lr_scheduler.step() optimizer.zero_grad() overall_step += 1 # Printing the GPU memory usage details such as allocated memory, peak memory, and total memory usage accelerator.print('Memory before entering the train : {}'.format(bamb(tracemalloc.begin ) ) ) accelerator.print('Memory consumed at the end of the train (end-begin): {}'.format(tracemalloc.used ) ) accelerator.print('Peak Memory consumed during the train (max-begin): {}'.format(tracemalloc.peaked ) ) accelerator.print( 'Total Peak Memory consumed during the train (max): {}'.format( tracemalloc.peaked + bamb(tracemalloc.begin ) ) ) snake_case__ : Dict = tracemalloc.peaked + bamb(tracemalloc.begin ) if args.peak_memory_upper_bound is not None: assert ( train_total_peak_memory[F"""epoch-{epoch}"""] <= args.peak_memory_upper_bound ), "Peak memory usage exceeded the upper bound" accelerator.wait_for_everyone() if accelerator.is_main_process: with open(os.path.join(args.output_dir , 'peak_memory_utilization.json' ) , 'w' ) as f: json.dump(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def UpperCamelCase__ ( ) -> Optional[int]: snake_case__ : Optional[int] = argparse.ArgumentParser(description='Simple example of training script tracking peak GPU memory usage.' ) parser.add_argument( '--model_name_or_path' , type=SCREAMING_SNAKE_CASE_ , default='bert-base-cased' , help='Path to pretrained model or model identifier from huggingface.co/models.' , required=SCREAMING_SNAKE_CASE_ , ) parser.add_argument( '--output_dir' , type=SCREAMING_SNAKE_CASE_ , default='.' , help='Optional save directory where all checkpoint folders will be stored. Default is the current working directory.' , ) parser.add_argument( '--peak_memory_upper_bound' , type=SCREAMING_SNAKE_CASE_ , default=SCREAMING_SNAKE_CASE_ , help='The upper bound of peak memory usage in MB. If set, the training will throw an error if the peak memory usage exceeds this value.' , ) parser.add_argument( '--n_train' , type=SCREAMING_SNAKE_CASE_ , default=320 , help='Number of training examples to use.' , ) parser.add_argument( '--n_val' , type=SCREAMING_SNAKE_CASE_ , default=160 , help='Number of validation examples to use.' , ) parser.add_argument( '--num_epochs' , type=SCREAMING_SNAKE_CASE_ , default=1 , help='Number of train epochs.' , ) snake_case__ : List[Any] = parser.parse_args() snake_case__ : int = {"lr": 2e-5, "num_epochs": args.num_epochs, "seed": 42, "batch_size": 16} training_function(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if __name__ == "__main__": main()
702
import numpy as np import qiskit def UpperCamelCase__ ( A__ = 8 , A__ = None ) -> str: snake_case__ : Optional[int] = np.random.default_rng(seed=A__ ) # Roughly 25% of the qubits will contribute to the key. # So we take more than we need. snake_case__ : Tuple = 6 * key_len # Measurement basis for Alice's qubits. snake_case__ : Tuple = rng.integers(2 , size=A__ ) # The set of states Alice will prepare. snake_case__ : List[str] = rng.integers(2 , size=A__ ) # Measurement basis for Bob's qubits. snake_case__ : List[Any] = rng.integers(2 , size=A__ ) # Quantum Circuit to simulate BB84 snake_case__ : Any = qiskit.QuantumCircuit(A__ , name='BB84' ) # Alice prepares her qubits according to rules above. for index, _ in enumerate(A__ ): if alice_state[index] == 1: bbaa_circ.x(A__ ) if alice_basis[index] == 1: bbaa_circ.h(A__ ) bbaa_circ.barrier() # Bob measures the received qubits according to rules above. for index, _ in enumerate(A__ ): if bob_basis[index] == 1: bbaa_circ.h(A__ ) bbaa_circ.barrier() bbaa_circ.measure_all() # Simulate the quantum circuit. snake_case__ : List[str] = qiskit.Aer.get_backend('aer_simulator' ) # We only need to run one shot because the key is unique. # Multiple shots will produce the same key. snake_case__ : Optional[Any] = qiskit.execute(A__ , A__ , shots=1 , seed_simulator=A__ ) # Returns the result of measurement. snake_case__ : Union[str, Any] = job.result().get_counts(A__ ).most_frequent() # Extracting the generated key from the simulation results. # Only keep measurement results where Alice and Bob chose the same basis. snake_case__ : Optional[Any] = ''.join( [ result_bit for alice_basis_bit, bob_basis_bit, result_bit in zip( A__ , A__ , A__ ) if alice_basis_bit == bob_basis_bit ] ) # Get final key. Pad with 0 if too short, otherwise truncate. snake_case__ : Tuple = gen_key[:key_len] if len(A__ ) >= key_len else gen_key.ljust(A__ , '0' ) return key if __name__ == "__main__": print(F'''The generated key is : {bbaa(8, seed=0)}''') from doctest import testmod testmod()
699
0
'''simple docstring''' import logging import os import sys from dataclasses import dataclass, field from itertools import chain from typing import Optional, Union import datasets import numpy as np import torch from datasets import load_dataset import transformers from transformers import ( AutoConfig, AutoModelForMultipleChoice, AutoTokenizer, HfArgumentParser, Trainer, TrainingArguments, default_data_collator, set_seed, ) from transformers.tokenization_utils_base import PreTrainedTokenizerBase from transformers.trainer_utils import get_last_checkpoint from transformers.utils import PaddingStrategy, check_min_version, send_example_telemetry # Will error if the minimal version of Transformers is not installed. Remove at your own risks. check_min_version('''4.31.0''') lowerCAmelCase__ : Optional[int] = logging.getLogger(__name__) @dataclass class __snake_case : __lowerCamelCase = field( metadata={"""help""": """Path to pretrained model or model identifier from huggingface.co/models"""} ) __lowerCamelCase = field( default=_lowerCamelCase ,metadata={"""help""": """Pretrained config name or path if not the same as model_name"""} ) __lowerCamelCase = field( default=_lowerCamelCase ,metadata={"""help""": """Pretrained tokenizer name or path if not the same as model_name"""} ) __lowerCamelCase = field( default=_lowerCamelCase ,metadata={"""help""": """Where do you want to store the pretrained models downloaded from huggingface.co"""} ,) __lowerCamelCase = field( default=_lowerCamelCase ,metadata={"""help""": """Whether to use one of the fast tokenizer (backed by the tokenizers library) or not."""} ,) __lowerCamelCase = field( default="""main""" ,metadata={"""help""": """The specific model version to use (can be a branch name, tag name or commit id)."""} ,) __lowerCamelCase = field( default=_lowerCamelCase ,metadata={ """help""": ( """Will use the token generated when running `huggingface-cli login` (necessary to use this script """ """with private models).""" ) } ,) @dataclass class __snake_case : __lowerCamelCase = field(default=_lowerCamelCase ,metadata={"""help""": """The input training data file (a text file)."""} ) __lowerCamelCase = field( default=_lowerCamelCase ,metadata={"""help""": """An optional input evaluation data file to evaluate the perplexity on (a text file)."""} ,) __lowerCamelCase = field( default=_lowerCamelCase ,metadata={"""help""": """Overwrite the cached training and evaluation sets"""} ) __lowerCamelCase = field( default=_lowerCamelCase ,metadata={"""help""": """The number of processes to use for the preprocessing."""} ,) __lowerCamelCase = field( default=_lowerCamelCase ,metadata={ """help""": ( """The maximum total input sequence length after tokenization. If passed, sequences longer """ """than this will be truncated, sequences shorter will be padded.""" ) } ,) __lowerCamelCase = field( default=_lowerCamelCase ,metadata={ """help""": ( """Whether to pad all samples to the maximum sentence length. """ """If False, will pad the samples dynamically when batching to the maximum length in the batch. More """ """efficient on GPU but very bad for TPU.""" ) } ,) __lowerCamelCase = field( default=_lowerCamelCase ,metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of training examples to this """ """value if set.""" ) } ,) __lowerCamelCase = field( default=_lowerCamelCase ,metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of evaluation examples to this """ """value if set.""" ) } ,) def __a ( self ) -> List[Any]: '''simple docstring''' if self.train_file is not None: snake_case__ : Tuple = self.train_file.split('.' )[-1] assert extension in ["csv", "json"], "`train_file` should be a csv or a json file." if self.validation_file is not None: snake_case__ : List[str] = self.validation_file.split('.' )[-1] assert extension in ["csv", "json"], "`validation_file` should be a csv or a json file." @dataclass class __snake_case : __lowerCamelCase = 42 __lowerCamelCase = True __lowerCamelCase = None __lowerCamelCase = None def __call__( self , __UpperCamelCase ) -> str: '''simple docstring''' snake_case__ : Any = 'label' if 'label' in features[0].keys() else 'labels' snake_case__ : str = [feature.pop(__UpperCamelCase ) for feature in features] snake_case__ : Union[str, Any] = len(__UpperCamelCase ) snake_case__ : Optional[Any] = len(features[0]['input_ids'] ) snake_case__ : str = [ [{k: v[i] for k, v in feature.items()} for i in range(__UpperCamelCase )] for feature in features ] snake_case__ : Optional[int] = list(chain(*__UpperCamelCase ) ) snake_case__ : str = self.tokenizer.pad( __UpperCamelCase , padding=self.padding , max_length=self.max_length , pad_to_multiple_of=self.pad_to_multiple_of , return_tensors='pt' , ) # Un-flatten snake_case__ : Optional[int] = {k: v.view(__UpperCamelCase , __UpperCamelCase , -1 ) for k, v in batch.items()} # Add back labels snake_case__ : int = torch.tensor(__UpperCamelCase , dtype=torch.intaa ) return batch def UpperCamelCase__ ( ) -> int: # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. snake_case__ : Dict = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith('.json' ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. snake_case__ : Any = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: snake_case__ : List[Any] = 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_swag' , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) # Setup logging logging.basicConfig( format='%(asctime)s - %(levelname)s - %(name)s - %(message)s' , datefmt='%m/%d/%Y %H:%M:%S' , handlers=[logging.StreamHandler(sys.stdout )] , ) if training_args.should_log: # The default of training_args.log_level is passive, so we set log level at info here to have that default. transformers.utils.logging.set_verbosity_info() snake_case__ : Dict = training_args.get_process_log_level() logger.setLevel(SCREAMING_SNAKE_CASE_ ) datasets.utils.logging.set_verbosity(SCREAMING_SNAKE_CASE_ ) transformers.utils.logging.set_verbosity(SCREAMING_SNAKE_CASE_ ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Log on each process the small summary: logger.warning( F"""Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}""" + F"""distributed training: {bool(training_args.local_rank != -1 )}, 16-bits training: {training_args.fpaa}""" ) logger.info(F"""Training/evaluation parameters {training_args}""" ) # Detecting last checkpoint. snake_case__ : Tuple = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: snake_case__ : int = get_last_checkpoint(training_args.output_dir ) if last_checkpoint is None and len(os.listdir(training_args.output_dir ) ) > 0: raise ValueError( F"""Output directory ({training_args.output_dir}) already exists and is not empty. """ 'Use --overwrite_output_dir to overcome.' ) elif last_checkpoint is not None and training_args.resume_from_checkpoint is None: logger.info( F"""Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change """ 'the `--output_dir` or add `--overwrite_output_dir` to train from scratch.' ) # Set seed before initializing model. set_seed(training_args.seed ) # Get the datasets: you can either provide your own CSV/JSON/TXT training and evaluation files (see below) # or just provide the name of one of the public datasets available on the hub at https://huggingface.co/datasets/ # (the dataset will be downloaded automatically from the datasets Hub). # For CSV/JSON files, this script will use the column called 'text' or the first column if no column called # 'text' is found. You can easily tweak this behavior (see below). # In distributed training, the load_dataset function guarantee that only one local process can concurrently # download the dataset. if data_args.train_file is not None or data_args.validation_file is not None: snake_case__ : Union[str, Any] = {} if data_args.train_file is not None: snake_case__ : Union[str, Any] = data_args.train_file if data_args.validation_file is not None: snake_case__ : str = data_args.validation_file snake_case__ : Union[str, Any] = data_args.train_file.split('.' )[-1] snake_case__ : List[Any] = load_dataset( SCREAMING_SNAKE_CASE_ , data_files=SCREAMING_SNAKE_CASE_ , cache_dir=model_args.cache_dir , use_auth_token=True if model_args.use_auth_token else None , ) else: # Downloading and loading the swag dataset from the hub. snake_case__ : Union[str, Any] = load_dataset( 'swag' , 'regular' , cache_dir=model_args.cache_dir , use_auth_token=True if model_args.use_auth_token else None , ) # See more about loading any type of standard or custom dataset (from files, python dict, pandas DataFrame, etc) at # https://huggingface.co/docs/datasets/loading_datasets.html. # Load pretrained model and tokenizer # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. snake_case__ : List[str] = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) snake_case__ : Optional[Any] = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , use_fast=model_args.use_fast_tokenizer , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) snake_case__ : Tuple = AutoModelForMultipleChoice.from_pretrained( model_args.model_name_or_path , from_tf=bool('.ckpt' in model_args.model_name_or_path ) , config=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 , ) # When using your own dataset or a different dataset from swag, you will probably need to change this. snake_case__ : Dict = [F"""ending{i}""" for i in range(4 )] snake_case__ : Any = 'sent1' snake_case__ : Dict = 'sent2' if data_args.max_seq_length is None: snake_case__ : List[str] = tokenizer.model_max_length if max_seq_length > 1024: logger.warning( 'The chosen tokenizer supports a `model_max_length` that is longer than the default `block_size` value' ' of 1024. If you would like to use a longer `block_size` up to `tokenizer.model_max_length` you can' ' override this default with `--block_size xxx`.' ) snake_case__ : Tuple = 1024 else: if data_args.max_seq_length > tokenizer.model_max_length: logger.warning( F"""The max_seq_length passed ({data_args.max_seq_length}) is larger than the maximum length for the""" F"""model ({tokenizer.model_max_length}). Using max_seq_length={tokenizer.model_max_length}.""" ) snake_case__ : List[Any] = min(data_args.max_seq_length , tokenizer.model_max_length ) # Preprocessing the datasets. def preprocess_function(A__ ): snake_case__ : List[str] = [[context] * 4 for context in examples[context_name]] snake_case__ : int = examples[question_header_name] snake_case__ : List[str] = [ [F"""{header} {examples[end][i]}""" for end in ending_names] for i, header in enumerate(SCREAMING_SNAKE_CASE_ ) ] # Flatten out snake_case__ : Union[str, Any] = list(chain(*SCREAMING_SNAKE_CASE_ ) ) snake_case__ : Optional[Any] = list(chain(*SCREAMING_SNAKE_CASE_ ) ) # Tokenize snake_case__ : Union[str, Any] = tokenizer( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , truncation=SCREAMING_SNAKE_CASE_ , max_length=SCREAMING_SNAKE_CASE_ , padding='max_length' if data_args.pad_to_max_length else False , ) # Un-flatten return {k: [v[i : i + 4] for i in range(0 , len(SCREAMING_SNAKE_CASE_ ) , 4 )] for k, v in tokenized_examples.items()} if training_args.do_train: if "train" not in raw_datasets: raise ValueError('--do_train requires a train dataset' ) snake_case__ : Dict = raw_datasets['train'] if data_args.max_train_samples is not None: snake_case__ : List[str] = min(len(SCREAMING_SNAKE_CASE_ ) , data_args.max_train_samples ) snake_case__ : str = train_dataset.select(range(SCREAMING_SNAKE_CASE_ ) ) with training_args.main_process_first(desc='train dataset map pre-processing' ): snake_case__ : int = train_dataset.map( SCREAMING_SNAKE_CASE_ , batched=SCREAMING_SNAKE_CASE_ , num_proc=data_args.preprocessing_num_workers , load_from_cache_file=not data_args.overwrite_cache , ) if training_args.do_eval: if "validation" not in raw_datasets: raise ValueError('--do_eval requires a validation dataset' ) snake_case__ : Any = raw_datasets['validation'] if data_args.max_eval_samples is not None: snake_case__ : Optional[int] = min(len(SCREAMING_SNAKE_CASE_ ) , data_args.max_eval_samples ) snake_case__ : Dict = eval_dataset.select(range(SCREAMING_SNAKE_CASE_ ) ) with training_args.main_process_first(desc='validation dataset map pre-processing' ): snake_case__ : str = eval_dataset.map( SCREAMING_SNAKE_CASE_ , batched=SCREAMING_SNAKE_CASE_ , num_proc=data_args.preprocessing_num_workers , load_from_cache_file=not data_args.overwrite_cache , ) # Data collator snake_case__ : List[str] = ( default_data_collator if data_args.pad_to_max_length else DataCollatorForMultipleChoice(tokenizer=SCREAMING_SNAKE_CASE_ , pad_to_multiple_of=8 if training_args.fpaa else None ) ) # Metric def compute_metrics(A__ ): snake_case__ : Optional[Any] = eval_predictions snake_case__ : Tuple = np.argmax(SCREAMING_SNAKE_CASE_ , axis=1 ) return {"accuracy": (preds == label_ids).astype(np.floataa ).mean().item()} # Initialize our Trainer snake_case__ : Optional[int] = Trainer( model=SCREAMING_SNAKE_CASE_ , args=SCREAMING_SNAKE_CASE_ , train_dataset=train_dataset if training_args.do_train else None , eval_dataset=eval_dataset if training_args.do_eval else None , tokenizer=SCREAMING_SNAKE_CASE_ , data_collator=SCREAMING_SNAKE_CASE_ , compute_metrics=SCREAMING_SNAKE_CASE_ , ) # Training if training_args.do_train: snake_case__ : str = None if training_args.resume_from_checkpoint is not None: snake_case__ : str = training_args.resume_from_checkpoint elif last_checkpoint is not None: snake_case__ : Union[str, Any] = last_checkpoint snake_case__ : Optional[Any] = trainer.train(resume_from_checkpoint=SCREAMING_SNAKE_CASE_ ) trainer.save_model() # Saves the tokenizer too for easy upload snake_case__ : Tuple = train_result.metrics snake_case__ : List[str] = ( data_args.max_train_samples if data_args.max_train_samples is not None else len(SCREAMING_SNAKE_CASE_ ) ) snake_case__ : Optional[Any] = min(SCREAMING_SNAKE_CASE_ , len(SCREAMING_SNAKE_CASE_ ) ) trainer.log_metrics('train' , SCREAMING_SNAKE_CASE_ ) trainer.save_metrics('train' , SCREAMING_SNAKE_CASE_ ) trainer.save_state() # Evaluation if training_args.do_eval: logger.info('*** Evaluate ***' ) snake_case__ : List[str] = trainer.evaluate() snake_case__ : Optional[int] = data_args.max_eval_samples if data_args.max_eval_samples is not None else len(SCREAMING_SNAKE_CASE_ ) snake_case__ : Optional[int] = min(SCREAMING_SNAKE_CASE_ , len(SCREAMING_SNAKE_CASE_ ) ) trainer.log_metrics('eval' , SCREAMING_SNAKE_CASE_ ) trainer.save_metrics('eval' , SCREAMING_SNAKE_CASE_ ) snake_case__ : Any = { 'finetuned_from': model_args.model_name_or_path, 'tasks': 'multiple-choice', 'dataset_tags': 'swag', 'dataset_args': 'regular', 'dataset': 'SWAG', 'language': 'en', } if training_args.push_to_hub: trainer.push_to_hub(**SCREAMING_SNAKE_CASE_ ) else: trainer.create_model_card(**SCREAMING_SNAKE_CASE_ ) def UpperCamelCase__ ( A__ ) -> Any: # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
703
def UpperCamelCase__ ( A__ , A__ , A__ ) -> int: if exponent == 1: return base if exponent % 2 == 0: snake_case__ : Dict = _modexpt(A__ , exponent // 2 , A__ ) % modulo_value return (x * x) % modulo_value else: return (base * _modexpt(A__ , exponent - 1 , A__ )) % modulo_value def UpperCamelCase__ ( A__ = 1777 , A__ = 1855 , A__ = 8 ) -> int: snake_case__ : Tuple = base for _ in range(1 , A__ ): snake_case__ : Any = _modexpt(A__ , A__ , 10**digits ) return result if __name__ == "__main__": print(F'''{solution() = }''')
699
0
from math import ceil, sqrt def UpperCamelCase__ ( A__ = 100_0000 ) -> Tuple: '''simple docstring''' snake_case__ : Optional[int] = 0 for outer_width in range(3 , (limit // 4) + 2 ): if outer_width**2 > limit: snake_case__ : int = max(ceil(sqrt(outer_width**2 - limit ) ) , 1 ) else: snake_case__ : Optional[int] = 1 if (outer_width - hole_width_lower_bound) % 2: hole_width_lower_bound += 1 answer += (outer_width - hole_width_lower_bound - 2) // 2 + 1 return answer if __name__ == "__main__": print(F'''{solution() = }''')
704
# tests directory-specific settings - this file is run automatically # by pytest before any tests are run import sys import warnings from os.path import abspath, dirname, join # allow having multiple repository checkouts and not needing to remember to rerun # 'pip install -e .[dev]' when switching between checkouts and running tests. lowerCAmelCase__ : Tuple = abspath(join(dirname(dirname(__file__)), '''src''')) sys.path.insert(1, git_repo_path) # silence FutureWarning warnings in tests since often we can't act on them until # they become normal warnings - i.e. the tests still need to test the current functionality warnings.simplefilter(action='''ignore''', category=FutureWarning) def UpperCamelCase__ ( A__ ) -> Optional[Any]: from diffusers.utils.testing_utils import pytest_addoption_shared pytest_addoption_shared(A__ ) def UpperCamelCase__ ( A__ ) -> Optional[Any]: from diffusers.utils.testing_utils import pytest_terminal_summary_main snake_case__ : Union[str, Any] = terminalreporter.config.getoption('--make-reports' ) if make_reports: pytest_terminal_summary_main(A__ , id=A__ )
699
0
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, ) if is_sentencepiece_available(): from ..ta.tokenization_ta import TaTokenizer else: from ...utils.dummy_sentencepiece_objects import TaTokenizer lowerCAmelCase__ : Union[str, Any] = TaTokenizer if is_tokenizers_available(): from ..ta.tokenization_ta_fast import TaTokenizerFast else: from ...utils.dummy_tokenizers_objects import TaTokenizerFast lowerCAmelCase__ : Any = TaTokenizerFast lowerCAmelCase__ : Dict = {'''configuration_mt5''': ['''MT5Config''', '''MT5OnnxConfig''']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : str = [ '''MT5EncoderModel''', '''MT5ForConditionalGeneration''', '''MT5ForQuestionAnswering''', '''MT5Model''', '''MT5PreTrainedModel''', '''MT5Stack''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : Optional[int] = ['''TFMT5EncoderModel''', '''TFMT5ForConditionalGeneration''', '''TFMT5Model'''] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : List[str] = ['''FlaxMT5EncoderModel''', '''FlaxMT5ForConditionalGeneration''', '''FlaxMT5Model'''] if TYPE_CHECKING: from .configuration_mta import MTaConfig, MTaOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mta import ( MTaEncoderModel, MTaForConditionalGeneration, MTaForQuestionAnswering, MTaModel, MTaPreTrainedModel, MTaStack, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_mta import TFMTaEncoderModel, TFMTaForConditionalGeneration, TFMTaModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_mta import FlaxMTaEncoderModel, FlaxMTaForConditionalGeneration, FlaxMTaModel else: import sys lowerCAmelCase__ : Any = _LazyModule( __name__, globals()['''__file__'''], _import_structure, extra_objects={'''MT5Tokenizer''': MTaTokenizer, '''MT5TokenizerFast''': MTaTokenizerFast}, module_spec=__spec__, )
705
def UpperCamelCase__ ( A__ ) -> list[int]: if length <= 0 or not isinstance(A__ , A__ ): raise ValueError('Length must be a positive integer.' ) return [n * (2 * n - 1) for n in range(A__ )] if __name__ == "__main__": print(hexagonal_numbers(length=5)) print(hexagonal_numbers(length=10))
699
0
import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging lowerCAmelCase__ : int = logging.get_logger(__name__) lowerCAmelCase__ : int = {'''vocab_file''': '''sentencepiece.bpe.model'''} lowerCAmelCase__ : Optional[Any] = { '''vocab_file''': { '''moussaKam/mbarthez''': '''https://huggingface.co/moussaKam/mbarthez/resolve/main/sentencepiece.bpe.model''', '''moussaKam/barthez''': '''https://huggingface.co/moussaKam/barthez/resolve/main/sentencepiece.bpe.model''', '''moussaKam/barthez-orangesum-title''': ( '''https://huggingface.co/moussaKam/barthez-orangesum-title/resolve/main/sentencepiece.bpe.model''' ), }, } lowerCAmelCase__ : Optional[int] = { '''moussaKam/mbarthez''': 10_24, '''moussaKam/barthez''': 10_24, '''moussaKam/barthez-orangesum-title''': 10_24, } lowerCAmelCase__ : Dict = '''▁''' class __snake_case ( __a ): __lowerCamelCase = VOCAB_FILES_NAMES __lowerCamelCase = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase = ['''input_ids''', '''attention_mask'''] def __init__( self , __UpperCamelCase , __UpperCamelCase="<s>" , __UpperCamelCase="</s>" , __UpperCamelCase="</s>" , __UpperCamelCase="<s>" , __UpperCamelCase="<unk>" , __UpperCamelCase="<pad>" , __UpperCamelCase="<mask>" , __UpperCamelCase = None , **__UpperCamelCase , ) -> Dict: '''simple docstring''' snake_case__ : List[str] = AddedToken(snake_case__ , lstrip=snake_case__ , rstrip=snake_case__ ) if isinstance(snake_case__ , snake_case__ ) else mask_token snake_case__ : Tuple = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=snake_case__ , eos_token=snake_case__ , unk_token=snake_case__ , sep_token=snake_case__ , cls_token=snake_case__ , pad_token=snake_case__ , mask_token=snake_case__ , sp_model_kwargs=self.sp_model_kwargs , **snake_case__ , ) snake_case__ : str = vocab_file snake_case__ : int = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(snake_case__ ) ) snake_case__ : List[Any] = {'<s>': 0, '<pad>': 1, '</s>': 2, '<unk>': 3} snake_case__ : str = len(self.sp_model ) - 1 snake_case__ : Optional[int] = {v: k for k, v in self.fairseq_tokens_to_ids.items()} def __a ( self , __UpperCamelCase , __UpperCamelCase = None ) -> List[str]: '''simple docstring''' if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] snake_case__ : str = [self.cls_token_id] snake_case__ : Optional[int] = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def __a ( self , __UpperCamelCase , __UpperCamelCase = None , __UpperCamelCase = False ) -> List[str]: '''simple docstring''' if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=snake_case__ , token_ids_a=snake_case__ , already_has_special_tokens=snake_case__ ) if token_ids_a is None: return [1] + ([0] * len(snake_case__ )) + [1] return [1] + ([0] * len(snake_case__ )) + [1, 1] + ([0] * len(snake_case__ )) + [1] def __a ( self , __UpperCamelCase , __UpperCamelCase = None ) -> Optional[Any]: '''simple docstring''' snake_case__ : int = [self.sep_token_id] snake_case__ : Union[str, Any] = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] @property def __a ( self ) -> int: '''simple docstring''' return len(self.sp_model ) def __a ( self ) -> List[Any]: '''simple docstring''' snake_case__ : Optional[Any] = {self.convert_ids_to_tokens(snake_case__ ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __a ( self , __UpperCamelCase ) -> Any: '''simple docstring''' return self.sp_model.encode(snake_case__ , out_type=snake_case__ ) def __a ( self , __UpperCamelCase ) -> Dict: '''simple docstring''' if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] snake_case__ : List[str] = self.sp_model.PieceToId(snake_case__ ) return spm_id if spm_id else self.unk_token_id def __a ( self , __UpperCamelCase ) -> str: '''simple docstring''' if index in self.fairseq_ids_to_tokens: return self.fairseq_ids_to_tokens[index] return self.sp_model.IdToPiece(snake_case__ ) def __a ( self , __UpperCamelCase ) -> Dict: '''simple docstring''' snake_case__ : Dict = [] snake_case__ : Any = '' snake_case__ : Optional[Any] = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: if not prev_is_special: out_string += " " out_string += self.sp_model.decode(snake_case__ ) + token snake_case__ : Union[str, Any] = True snake_case__ : List[str] = [] else: current_sub_tokens.append(snake_case__ ) snake_case__ : Union[str, Any] = False out_string += self.sp_model.decode(snake_case__ ) return out_string.strip() def __getstate__( self ) -> Optional[int]: '''simple docstring''' snake_case__ : Any = self.__dict__.copy() snake_case__ : Optional[int] = None return state def __setstate__( self , __UpperCamelCase ) -> int: '''simple docstring''' snake_case__ : List[Any] = d # for backward compatibility if not hasattr(self , 'sp_model_kwargs' ): snake_case__ : Any = {} snake_case__ : Any = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def __a ( self , __UpperCamelCase , __UpperCamelCase = None ) -> str: '''simple docstring''' if not os.path.isdir(snake_case__ ): logger.error(F"""Vocabulary path ({save_directory}) should be a directory""" ) return snake_case__ : int = os.path.join( snake_case__ , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(snake_case__ ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , snake_case__ ) elif not os.path.isfile(self.vocab_file ): with open(snake_case__ , 'wb' ) as fi: snake_case__ : str = self.sp_model.serialized_model_proto() fi.write(snake_case__ ) return (out_vocab_file,)
706
import argparse import json from collections import OrderedDict from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( ConditionalDetrConfig, ConditionalDetrForObjectDetection, ConditionalDetrForSegmentation, ConditionalDetrImageProcessor, ) from transformers.utils import logging logging.set_verbosity_info() lowerCAmelCase__ : Dict = logging.get_logger(__name__) # here we list all keys to be renamed (original name on the left, our name on the right) lowerCAmelCase__ : Optional[Any] = [] for i in range(6): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append( (F'''transformer.encoder.layers.{i}.self_attn.out_proj.weight''', F'''encoder.layers.{i}.self_attn.out_proj.weight''') ) rename_keys.append( (F'''transformer.encoder.layers.{i}.self_attn.out_proj.bias''', F'''encoder.layers.{i}.self_attn.out_proj.bias''') ) rename_keys.append((F'''transformer.encoder.layers.{i}.linear1.weight''', F'''encoder.layers.{i}.fc1.weight''')) rename_keys.append((F'''transformer.encoder.layers.{i}.linear1.bias''', F'''encoder.layers.{i}.fc1.bias''')) rename_keys.append((F'''transformer.encoder.layers.{i}.linear2.weight''', F'''encoder.layers.{i}.fc2.weight''')) rename_keys.append((F'''transformer.encoder.layers.{i}.linear2.bias''', F'''encoder.layers.{i}.fc2.bias''')) rename_keys.append( (F'''transformer.encoder.layers.{i}.norm1.weight''', F'''encoder.layers.{i}.self_attn_layer_norm.weight''') ) rename_keys.append((F'''transformer.encoder.layers.{i}.norm1.bias''', F'''encoder.layers.{i}.self_attn_layer_norm.bias''')) rename_keys.append((F'''transformer.encoder.layers.{i}.norm2.weight''', F'''encoder.layers.{i}.final_layer_norm.weight''')) rename_keys.append((F'''transformer.encoder.layers.{i}.norm2.bias''', F'''encoder.layers.{i}.final_layer_norm.bias''')) # decoder layers: 2 times output projection, 2 feedforward neural networks and 3 layernorms rename_keys.append( (F'''transformer.decoder.layers.{i}.self_attn.out_proj.weight''', F'''decoder.layers.{i}.self_attn.out_proj.weight''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.self_attn.out_proj.bias''', F'''decoder.layers.{i}.self_attn.out_proj.bias''') ) rename_keys.append( ( F'''transformer.decoder.layers.{i}.cross_attn.out_proj.weight''', F'''decoder.layers.{i}.encoder_attn.out_proj.weight''', ) ) rename_keys.append( ( F'''transformer.decoder.layers.{i}.cross_attn.out_proj.bias''', F'''decoder.layers.{i}.encoder_attn.out_proj.bias''', ) ) rename_keys.append((F'''transformer.decoder.layers.{i}.linear1.weight''', F'''decoder.layers.{i}.fc1.weight''')) rename_keys.append((F'''transformer.decoder.layers.{i}.linear1.bias''', F'''decoder.layers.{i}.fc1.bias''')) rename_keys.append((F'''transformer.decoder.layers.{i}.linear2.weight''', F'''decoder.layers.{i}.fc2.weight''')) rename_keys.append((F'''transformer.decoder.layers.{i}.linear2.bias''', F'''decoder.layers.{i}.fc2.bias''')) rename_keys.append( (F'''transformer.decoder.layers.{i}.norm1.weight''', F'''decoder.layers.{i}.self_attn_layer_norm.weight''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.norm1.bias''', F'''decoder.layers.{i}.self_attn_layer_norm.bias''')) rename_keys.append( (F'''transformer.decoder.layers.{i}.norm2.weight''', F'''decoder.layers.{i}.encoder_attn_layer_norm.weight''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.norm2.bias''', F'''decoder.layers.{i}.encoder_attn_layer_norm.bias''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.norm3.weight''', F'''decoder.layers.{i}.final_layer_norm.weight''')) rename_keys.append((F'''transformer.decoder.layers.{i}.norm3.bias''', F'''decoder.layers.{i}.final_layer_norm.bias''')) # q, k, v projections in self/cross-attention in decoder for conditional DETR rename_keys.append( (F'''transformer.decoder.layers.{i}.sa_qcontent_proj.weight''', F'''decoder.layers.{i}.sa_qcontent_proj.weight''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.sa_kcontent_proj.weight''', F'''decoder.layers.{i}.sa_kcontent_proj.weight''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.sa_qpos_proj.weight''', F'''decoder.layers.{i}.sa_qpos_proj.weight''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.sa_kpos_proj.weight''', F'''decoder.layers.{i}.sa_kpos_proj.weight''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.sa_v_proj.weight''', F'''decoder.layers.{i}.sa_v_proj.weight''')) rename_keys.append( (F'''transformer.decoder.layers.{i}.ca_qcontent_proj.weight''', F'''decoder.layers.{i}.ca_qcontent_proj.weight''') ) # rename_keys.append((f"transformer.decoder.layers.{i}.ca_qpos_proj.weight", f"decoder.layers.{i}.ca_qpos_proj.weight")) rename_keys.append( (F'''transformer.decoder.layers.{i}.ca_kcontent_proj.weight''', F'''decoder.layers.{i}.ca_kcontent_proj.weight''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.ca_kpos_proj.weight''', F'''decoder.layers.{i}.ca_kpos_proj.weight''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.ca_v_proj.weight''', F'''decoder.layers.{i}.ca_v_proj.weight''')) rename_keys.append( (F'''transformer.decoder.layers.{i}.ca_qpos_sine_proj.weight''', F'''decoder.layers.{i}.ca_qpos_sine_proj.weight''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.sa_qcontent_proj.bias''', F'''decoder.layers.{i}.sa_qcontent_proj.bias''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.sa_kcontent_proj.bias''', F'''decoder.layers.{i}.sa_kcontent_proj.bias''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.sa_qpos_proj.bias''', F'''decoder.layers.{i}.sa_qpos_proj.bias''')) rename_keys.append((F'''transformer.decoder.layers.{i}.sa_kpos_proj.bias''', F'''decoder.layers.{i}.sa_kpos_proj.bias''')) rename_keys.append((F'''transformer.decoder.layers.{i}.sa_v_proj.bias''', F'''decoder.layers.{i}.sa_v_proj.bias''')) rename_keys.append( (F'''transformer.decoder.layers.{i}.ca_qcontent_proj.bias''', F'''decoder.layers.{i}.ca_qcontent_proj.bias''') ) # rename_keys.append((f"transformer.decoder.layers.{i}.ca_qpos_proj.bias", f"decoder.layers.{i}.ca_qpos_proj.bias")) rename_keys.append( (F'''transformer.decoder.layers.{i}.ca_kcontent_proj.bias''', F'''decoder.layers.{i}.ca_kcontent_proj.bias''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.ca_kpos_proj.bias''', F'''decoder.layers.{i}.ca_kpos_proj.bias''')) rename_keys.append((F'''transformer.decoder.layers.{i}.ca_v_proj.bias''', F'''decoder.layers.{i}.ca_v_proj.bias''')) rename_keys.append( (F'''transformer.decoder.layers.{i}.ca_qpos_sine_proj.bias''', F'''decoder.layers.{i}.ca_qpos_sine_proj.bias''') ) # convolutional projection + query embeddings + layernorm of decoder + class and bounding box heads # for conditional DETR, also convert reference point head and query scale MLP rename_keys.extend( [ ('''input_proj.weight''', '''input_projection.weight'''), ('''input_proj.bias''', '''input_projection.bias'''), ('''query_embed.weight''', '''query_position_embeddings.weight'''), ('''transformer.decoder.norm.weight''', '''decoder.layernorm.weight'''), ('''transformer.decoder.norm.bias''', '''decoder.layernorm.bias'''), ('''class_embed.weight''', '''class_labels_classifier.weight'''), ('''class_embed.bias''', '''class_labels_classifier.bias'''), ('''bbox_embed.layers.0.weight''', '''bbox_predictor.layers.0.weight'''), ('''bbox_embed.layers.0.bias''', '''bbox_predictor.layers.0.bias'''), ('''bbox_embed.layers.1.weight''', '''bbox_predictor.layers.1.weight'''), ('''bbox_embed.layers.1.bias''', '''bbox_predictor.layers.1.bias'''), ('''bbox_embed.layers.2.weight''', '''bbox_predictor.layers.2.weight'''), ('''bbox_embed.layers.2.bias''', '''bbox_predictor.layers.2.bias'''), ('''transformer.decoder.ref_point_head.layers.0.weight''', '''decoder.ref_point_head.layers.0.weight'''), ('''transformer.decoder.ref_point_head.layers.0.bias''', '''decoder.ref_point_head.layers.0.bias'''), ('''transformer.decoder.ref_point_head.layers.1.weight''', '''decoder.ref_point_head.layers.1.weight'''), ('''transformer.decoder.ref_point_head.layers.1.bias''', '''decoder.ref_point_head.layers.1.bias'''), ('''transformer.decoder.query_scale.layers.0.weight''', '''decoder.query_scale.layers.0.weight'''), ('''transformer.decoder.query_scale.layers.0.bias''', '''decoder.query_scale.layers.0.bias'''), ('''transformer.decoder.query_scale.layers.1.weight''', '''decoder.query_scale.layers.1.weight'''), ('''transformer.decoder.query_scale.layers.1.bias''', '''decoder.query_scale.layers.1.bias'''), ('''transformer.decoder.layers.0.ca_qpos_proj.weight''', '''decoder.layers.0.ca_qpos_proj.weight'''), ('''transformer.decoder.layers.0.ca_qpos_proj.bias''', '''decoder.layers.0.ca_qpos_proj.bias'''), ] ) def UpperCamelCase__ ( A__ , A__ , A__ ) -> List[str]: snake_case__ : int = state_dict.pop(A__ ) snake_case__ : Union[str, Any] = val def UpperCamelCase__ ( A__ ) -> int: snake_case__ : List[Any] = OrderedDict() for key, value in state_dict.items(): if "backbone.0.body" in key: snake_case__ : Any = key.replace('backbone.0.body' , 'backbone.conv_encoder.model' ) snake_case__ : Optional[int] = value else: snake_case__ : Optional[int] = value return new_state_dict def UpperCamelCase__ ( A__ , A__=False ) -> Optional[int]: snake_case__ : Optional[int] = '' if is_panoptic: snake_case__ : Tuple = 'conditional_detr.' # first: transformer encoder for i in range(6 ): # read in weights + bias of input projection layer (in PyTorch's MultiHeadAttention, this is a single matrix + bias) snake_case__ : int = state_dict.pop(F"""{prefix}transformer.encoder.layers.{i}.self_attn.in_proj_weight""" ) snake_case__ : str = state_dict.pop(F"""{prefix}transformer.encoder.layers.{i}.self_attn.in_proj_bias""" ) # next, add query, keys and values (in that order) to the state dict snake_case__ : Union[str, Any] = in_proj_weight[:256, :] snake_case__ : Union[str, Any] = in_proj_bias[:256] snake_case__ : Union[str, Any] = in_proj_weight[256:512, :] snake_case__ : Optional[Any] = in_proj_bias[256:512] snake_case__ : List[str] = in_proj_weight[-256:, :] snake_case__ : Tuple = in_proj_bias[-256:] def UpperCamelCase__ ( ) -> Tuple: snake_case__ : int = 'http://images.cocodataset.org/val2017/000000039769.jpg' snake_case__ : str = Image.open(requests.get(A__ , stream=A__ ).raw ) return im @torch.no_grad() def UpperCamelCase__ ( A__ , A__ ) -> str: snake_case__ : List[Any] = ConditionalDetrConfig() # set backbone and dilation attributes if "resnet101" in model_name: snake_case__ : Any = 'resnet101' if "dc5" in model_name: snake_case__ : Any = True snake_case__ : int = 'panoptic' in model_name if is_panoptic: snake_case__ : str = 250 else: snake_case__ : Union[str, Any] = 91 snake_case__ : Optional[int] = 'huggingface/label-files' snake_case__ : Optional[Any] = 'coco-detection-id2label.json' snake_case__ : str = json.load(open(hf_hub_download(A__ , A__ , repo_type='dataset' ) , 'r' ) ) snake_case__ : List[Any] = {int(A__ ): v for k, v in idalabel.items()} snake_case__ : Any = idalabel snake_case__ : int = {v: k for k, v in idalabel.items()} # load image processor snake_case__ : List[Any] = 'coco_panoptic' if is_panoptic else 'coco_detection' snake_case__ : List[Any] = ConditionalDetrImageProcessor(format=A__ ) # prepare image snake_case__ : List[str] = prepare_img() snake_case__ : Any = image_processor(images=A__ , return_tensors='pt' ) snake_case__ : Dict = encoding['pixel_values'] logger.info(F"""Converting model {model_name}...""" ) # load original model from torch hub snake_case__ : Any = torch.hub.load('DeppMeng/ConditionalDETR' , A__ , pretrained=A__ ).eval() snake_case__ : Tuple = conditional_detr.state_dict() # rename keys for src, dest in rename_keys: if is_panoptic: snake_case__ : List[Any] = 'conditional_detr.' + src rename_key(A__ , A__ , A__ ) snake_case__ : Dict = rename_backbone_keys(A__ ) # query, key and value matrices need special treatment read_in_q_k_v(A__ , is_panoptic=A__ ) # important: we need to prepend a prefix to each of the base model keys as the head models use different attributes for them snake_case__ : Optional[int] = 'conditional_detr.model.' if is_panoptic else 'model.' for key in state_dict.copy().keys(): if is_panoptic: if ( key.startswith('conditional_detr' ) and not key.startswith('class_labels_classifier' ) and not key.startswith('bbox_predictor' ) ): snake_case__ : List[Any] = state_dict.pop(A__ ) snake_case__ : Optional[int] = val elif "class_labels_classifier" in key or "bbox_predictor" in key: snake_case__ : str = state_dict.pop(A__ ) snake_case__ : List[Any] = val elif key.startswith('bbox_attention' ) or key.startswith('mask_head' ): continue else: snake_case__ : Union[str, Any] = state_dict.pop(A__ ) snake_case__ : Dict = val else: if not key.startswith('class_labels_classifier' ) and not key.startswith('bbox_predictor' ): snake_case__ : List[Any] = state_dict.pop(A__ ) snake_case__ : Optional[int] = val # finally, create HuggingFace model and load state dict snake_case__ : Union[str, Any] = ConditionalDetrForSegmentation(A__ ) if is_panoptic else ConditionalDetrForObjectDetection(A__ ) model.load_state_dict(A__ ) model.eval() model.push_to_hub(repo_id=A__ , organization='DepuMeng' , commit_message='Add model' ) # verify our conversion snake_case__ : Tuple = conditional_detr(A__ ) snake_case__ : str = model(A__ ) assert torch.allclose(outputs.logits , original_outputs['pred_logits'] , atol=1e-4 ) assert torch.allclose(outputs.pred_boxes , original_outputs['pred_boxes'] , atol=1e-4 ) if is_panoptic: assert torch.allclose(outputs.pred_masks , original_outputs['pred_masks'] , atol=1e-4 ) # Save model and image processor logger.info(F"""Saving PyTorch model and image processor to {pytorch_dump_folder_path}...""" ) Path(A__ ).mkdir(exist_ok=A__ ) model.save_pretrained(A__ ) image_processor.save_pretrained(A__ ) if __name__ == "__main__": lowerCAmelCase__ : Any = argparse.ArgumentParser() parser.add_argument( '''--model_name''', default='''conditional_detr_resnet50''', type=str, help='''Name of the CONDITIONAL_DETR model you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the folder to output PyTorch model.''' ) lowerCAmelCase__ : int = parser.parse_args() convert_conditional_detr_checkpoint(args.model_name, args.pytorch_dump_folder_path)
699
0
def UpperCamelCase__ ( A__ , A__ , A__ ) -> str: if n == 0: return 1 elif n % 2 == 1: return (binary_exponentiation(lowerCAmelCase_ , n - 1 , lowerCAmelCase_ ) * a) % mod else: snake_case__ : Tuple = binary_exponentiation(lowerCAmelCase_ , n / 2 , lowerCAmelCase_ ) return (b * b) % mod # a prime number lowerCAmelCase__ : List[str] = 7_01 lowerCAmelCase__ : Union[str, Any] = 10_00_00_00_00 lowerCAmelCase__ : int = 10 # using binary exponentiation function, O(log(p)): print((a / b) % p == (a * binary_exponentiation(b, p - 2, p)) % p) print((a / b) % p == (a * b ** (p - 2)) % p)
707
from collections import namedtuple lowerCAmelCase__ : Union[str, Any] = namedtuple('''from_to''', '''from_ to''') lowerCAmelCase__ : Tuple = { '''cubicmeter''': from_to(1, 1), '''litre''': from_to(0.0_01, 10_00), '''kilolitre''': from_to(1, 1), '''gallon''': from_to(0.0_04_54, 2_64.1_72), '''cubicyard''': from_to(0.7_64_55, 1.3_07_95), '''cubicfoot''': from_to(0.0_28, 35.31_47), '''cup''': from_to(0.0_00_23_65_88, 42_26.75), } def UpperCamelCase__ ( A__ , A__ , A__ ) -> float: if from_type not in METRIC_CONVERSION: raise ValueError( F"""Invalid 'from_type' value: {from_type!r} Supported values are:\n""" + ', '.join(A__ ) ) if to_type not in METRIC_CONVERSION: raise ValueError( F"""Invalid 'to_type' value: {to_type!r}. Supported values are:\n""" + ', '.join(A__ ) ) return value * METRIC_CONVERSION[from_type].from_ * METRIC_CONVERSION[to_type].to if __name__ == "__main__": import doctest doctest.testmod()
699
0
from scipy.stats import spearmanr import datasets lowerCAmelCase__ : int = ''' 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. ''' lowerCAmelCase__ : Tuple = ''' 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 ''' lowerCAmelCase__ : Optional[int] = r'''\ @book{kokoska2000crc, title={CRC standard probability and statistics tables and formulae}, author={Kokoska, Stephen and Zwillinger, Daniel}, year={2000}, publisher={Crc Press} } @article{2020SciPy-NMeth, author = {Virtanen, Pauli and Gommers, Ralf and Oliphant, Travis E. and Haberland, Matt and Reddy, Tyler and Cournapeau, David and Burovski, Evgeni and Peterson, Pearu and Weckesser, Warren and Bright, Jonathan and {van der Walt}, St{\'e}fan J. and Brett, Matthew and Wilson, Joshua and Millman, K. Jarrod and Mayorov, Nikolay and Nelson, Andrew R. J. and Jones, Eric and Kern, Robert and Larson, Eric and Carey, C J and Polat, {\.I}lhan and Feng, Yu and Moore, Eric W. and {VanderPlas}, Jake and Laxalde, Denis and Perktold, Josef and Cimrman, Robert and Henriksen, Ian and Quintero, E. A. and Harris, Charles R. and Archibald, Anne M. and Ribeiro, Ant{\^o}nio H. and Pedregosa, Fabian and {van Mulbregt}, Paul and {SciPy 1.0 Contributors}}, title = {{{SciPy} 1.0: Fundamental Algorithms for Scientific Computing in Python}}, journal = {Nature Methods}, year = {2020}, volume = {17}, pages = {261--272}, adsurl = {https://rdcu.be/b08Wh}, doi = {10.1038/s41592-019-0686-2}, } ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION ,_KWARGS_DESCRIPTION ) class __snake_case ( datasets.Metric ): def __a ( self ) -> Dict: '''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.spearmanr.html'] , ) def __a ( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase=False ) -> Tuple: '''simple docstring''' snake_case__ : Optional[int] = spearmanr(__UpperCamelCase , __UpperCamelCase ) if return_pvalue: return {"spearmanr": results[0], "spearmanr_pvalue": results[1]} else: return {"spearmanr": results[0]}
708
import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging lowerCAmelCase__ : Tuple = logging.get_logger(__name__) lowerCAmelCase__ : Union[str, Any] = '''▁''' lowerCAmelCase__ : List[Any] = {'''vocab_file''': '''sentencepiece.bpe.model'''} lowerCAmelCase__ : Optional[Any] = { '''vocab_file''': { '''facebook/xglm-564M''': '''https://huggingface.co/facebook/xglm-564M/resolve/main/sentencepiece.bpe.model''', } } lowerCAmelCase__ : str = { '''facebook/xglm-564M''': 20_48, } class __snake_case ( _lowerCamelCase ): __lowerCamelCase = VOCAB_FILES_NAMES __lowerCamelCase = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase = ["""input_ids""", """attention_mask"""] def __init__( self , __UpperCamelCase , __UpperCamelCase="<s>" , __UpperCamelCase="</s>" , __UpperCamelCase="</s>" , __UpperCamelCase="<s>" , __UpperCamelCase="<unk>" , __UpperCamelCase="<pad>" , __UpperCamelCase = None , **__UpperCamelCase , ) -> None: '''simple docstring''' snake_case__ : Any = {} if sp_model_kwargs is None else sp_model_kwargs # Compatibility with the original tokenizer snake_case__ : Tuple = 7 snake_case__ : Dict = [F"""<madeupword{i}>""" for i in range(self.num_madeup_words )] snake_case__ : Union[str, Any] = kwargs.get('additional_special_tokens' , [] ) kwargs["additional_special_tokens"] += [ word for word in madeup_words if word not in kwargs["additional_special_tokens"] ] super().__init__( bos_token=__UpperCamelCase , eos_token=__UpperCamelCase , unk_token=__UpperCamelCase , sep_token=__UpperCamelCase , cls_token=__UpperCamelCase , pad_token=__UpperCamelCase , sp_model_kwargs=self.sp_model_kwargs , **__UpperCamelCase , ) snake_case__ : Optional[int] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(__UpperCamelCase ) ) snake_case__ : Optional[Any] = vocab_file # Original fairseq vocab and spm vocab must be "aligned": # Vocab | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 # -------- | ------- | ------- | ------ | ------- | --- | --- | --- | ----- | ----- | ---- # fairseq | '<s>' | '<pad>' | '</s>' | '<unk>' | ',' | '.' | '▁' | 's' | '▁de' | '-' # spm | '<unk>' | '<s>' | '</s>' | ',' | '.' | '▁' | 's' | '▁de' | '-' | '▁a' # The first "real" token "," has position 4 in the original fairseq vocab and position 3 in the spm vocab snake_case__ : Tuple = 1 # Mimic fairseq token-to-id alignment for the first 4 token snake_case__ : Tuple = {'<s>': 0, '<pad>': 1, '</s>': 2, '<unk>': 3} snake_case__ : List[Any] = len(self.sp_model ) snake_case__ : Optional[Any] = {F"""<madeupword{i}>""": sp_size + i + self.fairseq_offset for i in range(self.num_madeup_words )} self.fairseq_tokens_to_ids.update(__UpperCamelCase ) snake_case__ : Union[str, Any] = {v: k for k, v in self.fairseq_tokens_to_ids.items()} def __getstate__( self ) -> List[Any]: '''simple docstring''' snake_case__ : Union[str, Any] = self.__dict__.copy() snake_case__ : Optional[Any] = None snake_case__ : Tuple = self.sp_model.serialized_model_proto() return state def __setstate__( self , __UpperCamelCase ) -> Union[str, Any]: '''simple docstring''' snake_case__ : Union[str, Any] = d # for backward compatibility if not hasattr(self , 'sp_model_kwargs' ): snake_case__ : Any = {} snake_case__ : Dict = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.LoadFromSerializedProto(self.sp_model_proto ) def __a ( self , __UpperCamelCase , __UpperCamelCase = None ) -> List[int]: '''simple docstring''' if token_ids_a is None: return [self.sep_token_id] + token_ids_a snake_case__ : str = [self.sep_token_id] return sep + token_ids_a + sep + sep + token_ids_a def __a ( self , __UpperCamelCase , __UpperCamelCase = None , __UpperCamelCase = 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 ) if token_ids_a is None: return [1] + ([0] * len(__UpperCamelCase )) return [1] + ([0] * len(__UpperCamelCase )) + [1, 1] + ([0] * len(__UpperCamelCase )) def __a ( self , __UpperCamelCase , __UpperCamelCase = None ) -> List[int]: '''simple docstring''' snake_case__ : int = [self.sep_token_id] if token_ids_a is None: return len(sep + token_ids_a ) * [0] return len(sep + token_ids_a + sep + sep + token_ids_a ) * [0] @property def __a ( self ) -> Tuple: '''simple docstring''' return len(self.sp_model ) + self.fairseq_offset + self.num_madeup_words def __a ( self ) -> Union[str, Any]: '''simple docstring''' snake_case__ : int = {self.convert_ids_to_tokens(__UpperCamelCase ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __a ( self , __UpperCamelCase ) -> List[str]: '''simple docstring''' return self.sp_model.encode(__UpperCamelCase , out_type=__UpperCamelCase ) def __a ( self , __UpperCamelCase ) -> Union[str, Any]: '''simple docstring''' if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] snake_case__ : Optional[Any] = self.sp_model.PieceToId(__UpperCamelCase ) # Need to return unknown token if the SP model returned 0 return spm_id + self.fairseq_offset if spm_id else self.unk_token_id def __a ( self , __UpperCamelCase ) -> Dict: '''simple docstring''' if index in self.fairseq_ids_to_tokens: return self.fairseq_ids_to_tokens[index] return self.sp_model.IdToPiece(index - self.fairseq_offset ) def __a ( self , __UpperCamelCase ) -> int: '''simple docstring''' snake_case__ : int = ''.join(__UpperCamelCase ).replace(__UpperCamelCase , ' ' ).strip() return out_string def __a ( self , __UpperCamelCase , __UpperCamelCase = 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__ : List[str] = 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__ : Any = self.sp_model.serialized_model_proto() fi.write(__UpperCamelCase ) return (out_vocab_file,)
699
0
import os from pathlib import Path def UpperCamelCase__ ( ) -> int: from torch.utils.cpp_extension import load snake_case__ : Union[str, Any] = Path(_A ).resolve().parent.parent.parent / 'kernels' / 'deformable_detr' snake_case__ : Union[str, Any] = [ root / filename for filename in [ 'vision.cpp', os.path.join('cpu' , 'ms_deform_attn_cpu.cpp' ), os.path.join('cuda' , 'ms_deform_attn_cuda.cu' ), ] ] load( 'MultiScaleDeformableAttention' , _A , with_cuda=_A , extra_include_paths=[str(_A )] , extra_cflags=['-DWITH_CUDA=1'] , extra_cuda_cflags=[ '-DCUDA_HAS_FP16=1', '-D__CUDA_NO_HALF_OPERATORS__', '-D__CUDA_NO_HALF_CONVERSIONS__', '-D__CUDA_NO_HALF2_OPERATORS__', ] , ) import MultiScaleDeformableAttention as MSDA return MSDA
709
import collections from typing import List, Optional, Union from ...tokenization_utils_base import BatchEncoding from ...utils import TensorType, add_end_docstrings, add_start_docstrings, logging from ..bert.tokenization_bert_fast import BertTokenizerFast from .tokenization_dpr import DPRContextEncoderTokenizer, DPRQuestionEncoderTokenizer, DPRReaderTokenizer lowerCAmelCase__ : Any = logging.get_logger(__name__) lowerCAmelCase__ : List[Any] = {'''vocab_file''': '''vocab.txt''', '''tokenizer_file''': '''tokenizer.json'''} lowerCAmelCase__ : Any = { '''vocab_file''': { '''facebook/dpr-ctx_encoder-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/vocab.txt''' ), '''facebook/dpr-ctx_encoder-multiset-base''': ( '''https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/vocab.txt''' ), }, '''tokenizer_file''': { '''facebook/dpr-ctx_encoder-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/tokenizer.json''' ), '''facebook/dpr-ctx_encoder-multiset-base''': ( '''https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/tokenizer.json''' ), }, } lowerCAmelCase__ : Any = { '''vocab_file''': { '''facebook/dpr-question_encoder-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/vocab.txt''' ), '''facebook/dpr-question_encoder-multiset-base''': ( '''https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/vocab.txt''' ), }, '''tokenizer_file''': { '''facebook/dpr-question_encoder-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/tokenizer.json''' ), '''facebook/dpr-question_encoder-multiset-base''': ( '''https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/tokenizer.json''' ), }, } lowerCAmelCase__ : Tuple = { '''vocab_file''': { '''facebook/dpr-reader-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/vocab.txt''' ), '''facebook/dpr-reader-multiset-base''': ( '''https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/vocab.txt''' ), }, '''tokenizer_file''': { '''facebook/dpr-reader-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/tokenizer.json''' ), '''facebook/dpr-reader-multiset-base''': ( '''https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/tokenizer.json''' ), }, } lowerCAmelCase__ : Dict = { '''facebook/dpr-ctx_encoder-single-nq-base''': 5_12, '''facebook/dpr-ctx_encoder-multiset-base''': 5_12, } lowerCAmelCase__ : Union[str, Any] = { '''facebook/dpr-question_encoder-single-nq-base''': 5_12, '''facebook/dpr-question_encoder-multiset-base''': 5_12, } lowerCAmelCase__ : Optional[Any] = { '''facebook/dpr-reader-single-nq-base''': 5_12, '''facebook/dpr-reader-multiset-base''': 5_12, } lowerCAmelCase__ : Tuple = { '''facebook/dpr-ctx_encoder-single-nq-base''': {'''do_lower_case''': True}, '''facebook/dpr-ctx_encoder-multiset-base''': {'''do_lower_case''': True}, } lowerCAmelCase__ : Any = { '''facebook/dpr-question_encoder-single-nq-base''': {'''do_lower_case''': True}, '''facebook/dpr-question_encoder-multiset-base''': {'''do_lower_case''': True}, } lowerCAmelCase__ : List[str] = { '''facebook/dpr-reader-single-nq-base''': {'''do_lower_case''': True}, '''facebook/dpr-reader-multiset-base''': {'''do_lower_case''': True}, } class __snake_case ( _lowerCamelCase ): __lowerCamelCase = VOCAB_FILES_NAMES __lowerCamelCase = CONTEXT_ENCODER_PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase = CONTEXT_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase = CONTEXT_ENCODER_PRETRAINED_INIT_CONFIGURATION __lowerCamelCase = DPRContextEncoderTokenizer class __snake_case ( _lowerCamelCase ): __lowerCamelCase = VOCAB_FILES_NAMES __lowerCamelCase = QUESTION_ENCODER_PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase = QUESTION_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase = QUESTION_ENCODER_PRETRAINED_INIT_CONFIGURATION __lowerCamelCase = DPRQuestionEncoderTokenizer lowerCAmelCase__ : Tuple = collections.namedtuple( '''DPRSpanPrediction''', ['''span_score''', '''relevance_score''', '''doc_id''', '''start_index''', '''end_index''', '''text'''] ) lowerCAmelCase__ : List[Any] = collections.namedtuple('''DPRReaderOutput''', ['''start_logits''', '''end_logits''', '''relevance_logits''']) lowerCAmelCase__ : int = r''' Return a dictionary with the token ids of the input strings and other information to give to `.decode_best_spans`. It converts the strings of a question and different passages (title and text) in a sequence of IDs (integers), using the tokenizer and vocabulary. The resulting `input_ids` is a matrix of size `(n_passages, sequence_length)` with the format: [CLS] <question token ids> [SEP] <titles ids> [SEP] <texts ids> Args: questions (`str` or `List[str]`): The questions to be encoded. You can specify one question for many passages. In this case, the question will be duplicated like `[questions] * n_passages`. Otherwise you have to specify as many questions as in `titles` or `texts`. titles (`str` or `List[str]`): The passages titles to be encoded. This can be a string or a list of strings if there are several passages. texts (`str` or `List[str]`): The passages texts to be encoded. This can be a string or a list of strings if there are several passages. padding (`bool`, `str` or [`~utils.PaddingStrategy`], *optional*, defaults to `False`): Activates and controls padding. Accepts the following values: - `True` or `\'longest\'`: Pad to the longest sequence in the batch (or no padding if only a single sequence if provided). - `\'max_length\'`: Pad to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. - `False` or `\'do_not_pad\'` (default): No padding (i.e., can output a batch with sequences of different lengths). truncation (`bool`, `str` or [`~tokenization_utils_base.TruncationStrategy`], *optional*, defaults to `False`): Activates and controls truncation. Accepts the following values: - `True` or `\'longest_first\'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. This will truncate token by token, removing a token from the longest sequence in the pair if a pair of sequences (or a batch of pairs) is provided. - `\'only_first\'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. This will only truncate the first sequence of a pair if a pair of sequences (or a batch of pairs) is provided. - `\'only_second\'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. This will only truncate the second sequence of a pair if a pair of sequences (or a batch of pairs) is provided. - `False` or `\'do_not_truncate\'` (default): No truncation (i.e., can output batch with sequence lengths greater than the model maximum admissible input size). max_length (`int`, *optional*): Controls the maximum length to use by one of the truncation/padding parameters. If left unset or set to `None`, this will use the predefined model maximum length if a maximum length is required by one of the truncation/padding parameters. If the model has no specific maximum input length (like XLNet) truncation/padding to a maximum length will be deactivated. return_tensors (`str` or [`~utils.TensorType`], *optional*): If set, will return tensors instead of list of python integers. Acceptable values are: - `\'tf\'`: Return TensorFlow `tf.constant` objects. - `\'pt\'`: Return PyTorch `torch.Tensor` objects. - `\'np\'`: Return Numpy `np.ndarray` objects. return_attention_mask (`bool`, *optional*): Whether or not to return the attention mask. If not set, will return the attention mask according to the specific tokenizer\'s default, defined by the `return_outputs` attribute. [What are attention masks?](../glossary#attention-mask) Return: `Dict[str, List[List[int]]]`: A dictionary with the following keys: - `input_ids`: List of token ids to be fed to a model. - `attention_mask`: List of indices specifying which tokens should be attended to by the model. ''' @add_start_docstrings(_lowerCamelCase ) class __snake_case : def __call__( self , __UpperCamelCase , __UpperCamelCase = None , __UpperCamelCase = None , __UpperCamelCase = False , __UpperCamelCase = False , __UpperCamelCase = None , __UpperCamelCase = None , __UpperCamelCase = None , **__UpperCamelCase , ) -> BatchEncoding: '''simple docstring''' if titles is None and texts is None: return super().__call__( __UpperCamelCase , padding=__UpperCamelCase , truncation=__UpperCamelCase , max_length=__UpperCamelCase , return_tensors=__UpperCamelCase , return_attention_mask=__UpperCamelCase , **__UpperCamelCase , ) elif titles is None or texts is None: snake_case__ : Optional[Any] = titles if texts is None else texts return super().__call__( __UpperCamelCase , __UpperCamelCase , padding=__UpperCamelCase , truncation=__UpperCamelCase , max_length=__UpperCamelCase , return_tensors=__UpperCamelCase , return_attention_mask=__UpperCamelCase , **__UpperCamelCase , ) snake_case__ : int = titles if not isinstance(__UpperCamelCase , __UpperCamelCase ) else [titles] snake_case__ : Optional[int] = texts if not isinstance(__UpperCamelCase , __UpperCamelCase ) else [texts] snake_case__ : List[Any] = len(__UpperCamelCase ) snake_case__ : str = questions if not isinstance(__UpperCamelCase , __UpperCamelCase ) else [questions] * n_passages assert len(__UpperCamelCase ) == len( __UpperCamelCase ), F"""There should be as many titles than texts but got {len(__UpperCamelCase )} titles and {len(__UpperCamelCase )} texts.""" snake_case__ : Optional[int] = super().__call__(__UpperCamelCase , __UpperCamelCase , padding=__UpperCamelCase , truncation=__UpperCamelCase )['input_ids'] snake_case__ : Optional[Any] = super().__call__(__UpperCamelCase , add_special_tokens=__UpperCamelCase , padding=__UpperCamelCase , truncation=__UpperCamelCase )['input_ids'] snake_case__ : Union[str, Any] = { 'input_ids': [ (encoded_question_and_title + encoded_text)[:max_length] if max_length is not None and truncation else encoded_question_and_title + encoded_text for encoded_question_and_title, encoded_text in zip(__UpperCamelCase , __UpperCamelCase ) ] } if return_attention_mask is not False: snake_case__ : List[Any] = [] for input_ids in encoded_inputs["input_ids"]: attention_mask.append([int(input_id != self.pad_token_id ) for input_id in input_ids] ) snake_case__ : Union[str, Any] = attention_mask return self.pad(__UpperCamelCase , padding=__UpperCamelCase , max_length=__UpperCamelCase , return_tensors=__UpperCamelCase ) def __a ( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase = 16 , __UpperCamelCase = 64 , __UpperCamelCase = 4 , ) -> List[DPRSpanPrediction]: '''simple docstring''' snake_case__ : Optional[Any] = reader_input['input_ids'] snake_case__ , snake_case__ , snake_case__ : Any = reader_output[:3] snake_case__ : List[str] = len(__UpperCamelCase ) snake_case__ : Tuple = sorted(range(__UpperCamelCase ) , reverse=__UpperCamelCase , key=relevance_logits.__getitem__ ) snake_case__ : List[DPRReaderOutput] = [] for doc_id in sorted_docs: snake_case__ : Tuple = list(input_ids[doc_id] ) # assuming question & title information is at the beginning of the sequence snake_case__ : int = sequence_ids.index(self.sep_token_id , 2 ) + 1 # second sep id if sequence_ids[-1] == self.pad_token_id: snake_case__ : Union[str, Any] = sequence_ids.index(self.pad_token_id ) else: snake_case__ : str = len(__UpperCamelCase ) snake_case__ : Dict = self._get_best_spans( start_logits=start_logits[doc_id][passage_offset:sequence_len] , end_logits=end_logits[doc_id][passage_offset:sequence_len] , max_answer_length=__UpperCamelCase , top_spans=__UpperCamelCase , ) for start_index, end_index in best_spans: start_index += passage_offset end_index += passage_offset nbest_spans_predictions.append( DPRSpanPrediction( span_score=start_logits[doc_id][start_index] + end_logits[doc_id][end_index] , relevance_score=relevance_logits[doc_id] , doc_id=__UpperCamelCase , start_index=__UpperCamelCase , end_index=__UpperCamelCase , text=self.decode(sequence_ids[start_index : end_index + 1] ) , ) ) if len(__UpperCamelCase ) >= num_spans: break return nbest_spans_predictions[:num_spans] def __a ( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , ) -> List[DPRSpanPrediction]: '''simple docstring''' snake_case__ : Any = [] for start_index, start_score in enumerate(__UpperCamelCase ): for answer_length, end_score in enumerate(end_logits[start_index : start_index + max_answer_length] ): scores.append(((start_index, start_index + answer_length), start_score + end_score) ) snake_case__ : str = sorted(__UpperCamelCase , key=lambda __UpperCamelCase : x[1] , reverse=__UpperCamelCase ) snake_case__ : Any = [] for (start_index, end_index), score in scores: assert start_index <= end_index, F"""Wrong span indices: [{start_index}:{end_index}]""" snake_case__ : str = end_index - start_index + 1 assert length <= max_answer_length, F"""Span is too long: {length} > {max_answer_length}""" if any( start_index <= prev_start_index <= prev_end_index <= end_index or prev_start_index <= start_index <= end_index <= prev_end_index for (prev_start_index, prev_end_index) in chosen_span_intervals ): continue chosen_span_intervals.append((start_index, end_index) ) if len(__UpperCamelCase ) == top_spans: break return chosen_span_intervals @add_end_docstrings(_lowerCamelCase ) class __snake_case ( _lowerCamelCase ,_lowerCamelCase ): __lowerCamelCase = VOCAB_FILES_NAMES __lowerCamelCase = READER_PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase = READER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase = READER_PRETRAINED_INIT_CONFIGURATION __lowerCamelCase = ["""input_ids""", """attention_mask"""] __lowerCamelCase = DPRReaderTokenizer
699
0
'''simple docstring''' from dataclasses import dataclass from typing import Optional, Tuple import torch from torch import nn from transformers import RobertaPreTrainedModel, XLMRobertaConfig, XLMRobertaModel from transformers.utils import ModelOutput @dataclass class __snake_case ( _A ): __lowerCamelCase = None __lowerCamelCase = None __lowerCamelCase = None __lowerCamelCase = None class __snake_case ( _A ): def __init__( self , __UpperCamelCase=1 , __UpperCamelCase=0 , __UpperCamelCase=2 , __UpperCamelCase=512 , __UpperCamelCase="cls" , __UpperCamelCase=False , __UpperCamelCase=True , **__UpperCamelCase , ) -> Optional[Any]: '''simple docstring''' super().__init__(pad_token_id=UpperCamelCase__ , bos_token_id=UpperCamelCase__ , eos_token_id=UpperCamelCase__ , **UpperCamelCase__ ) snake_case__ : Any = project_dim snake_case__ : Optional[Any] = pooler_fn snake_case__ : str = learn_encoder snake_case__ : Tuple = use_attention_mask class __snake_case ( _A ): __lowerCamelCase = [R"""pooler""", R"""logit_scale"""] __lowerCamelCase = [R"""position_ids""", R"""predictions.decoder.bias"""] __lowerCamelCase = """roberta""" __lowerCamelCase = RobertaSeriesConfig def __init__( self , __UpperCamelCase ) -> List[str]: '''simple docstring''' super().__init__(UpperCamelCase__ ) snake_case__ : List[Any] = XLMRobertaModel(UpperCamelCase__ ) snake_case__ : Optional[int] = nn.Linear(config.hidden_size , config.project_dim ) snake_case__ : Optional[int] = getattr(UpperCamelCase__ , 'has_pre_transformation' , UpperCamelCase__ ) if self.has_pre_transformation: snake_case__ : Tuple = nn.Linear(config.hidden_size , config.project_dim ) snake_case__ : Any = nn.LayerNorm(config.hidden_size , eps=config.layer_norm_eps ) self.post_init() def __a ( self , __UpperCamelCase = None , __UpperCamelCase = None , __UpperCamelCase = None , __UpperCamelCase = None , __UpperCamelCase = None , __UpperCamelCase = None , __UpperCamelCase = None , __UpperCamelCase = None , __UpperCamelCase = None , __UpperCamelCase = None , __UpperCamelCase = None , ) -> str: '''simple docstring''' snake_case__ : Optional[Any] = return_dict if return_dict is not None else self.config.use_return_dict snake_case__ : Union[str, Any] = self.base_model( input_ids=UpperCamelCase__ , attention_mask=UpperCamelCase__ , token_type_ids=UpperCamelCase__ , position_ids=UpperCamelCase__ , head_mask=UpperCamelCase__ , inputs_embeds=UpperCamelCase__ , encoder_hidden_states=UpperCamelCase__ , encoder_attention_mask=UpperCamelCase__ , output_attentions=UpperCamelCase__ , output_hidden_states=True if self.has_pre_transformation else output_hidden_states , return_dict=UpperCamelCase__ , ) if self.has_pre_transformation: snake_case__ : Union[str, Any] = outputs['hidden_states'][-2] snake_case__ : str = self.pre_LN(UpperCamelCase__ ) snake_case__ : Dict = self.transformation_pre(UpperCamelCase__ ) return TransformationModelOutput( projection_state=UpperCamelCase__ , last_hidden_state=outputs.last_hidden_state , hidden_states=outputs.hidden_states , attentions=outputs.attentions , ) else: snake_case__ : Union[str, Any] = self.transformation(outputs.last_hidden_state ) return TransformationModelOutput( projection_state=UpperCamelCase__ , last_hidden_state=outputs.last_hidden_state , hidden_states=outputs.hidden_states , attentions=outputs.attentions , )
710
import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, EulerAncestralDiscreteScheduler, LMSDiscreteScheduler, PNDMScheduler, StableDiffusionInstructPixaPixPipeline, UNetaDConditionModel, ) from diffusers.image_processor import VaeImageProcessor from diffusers.utils import floats_tensor, load_image, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..pipeline_params import ( IMAGE_TO_IMAGE_IMAGE_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS, ) from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class __snake_case ( _lowerCamelCase ,_lowerCamelCase ,_lowerCamelCase ,unittest.TestCase ): __lowerCamelCase = StableDiffusionInstructPixaPixPipeline __lowerCamelCase = TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {"""height""", """width""", """cross_attention_kwargs"""} __lowerCamelCase = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS __lowerCamelCase = IMAGE_TO_IMAGE_IMAGE_PARAMS __lowerCamelCase = IMAGE_TO_IMAGE_IMAGE_PARAMS def __a ( self ) -> List[Any]: '''simple docstring''' torch.manual_seed(0 ) snake_case__ : Tuple = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=8 , out_channels=4 , down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D') , up_block_types=('CrossAttnUpBlock2D', 'UpBlock2D') , cross_attention_dim=32 , ) snake_case__ : Any = PNDMScheduler(skip_prk_steps=__UpperCamelCase ) torch.manual_seed(0 ) snake_case__ : Dict = 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 ) snake_case__ : int = 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 , ) snake_case__ : Tuple = CLIPTextModel(__UpperCamelCase ) snake_case__ : Optional[int] = CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip' ) snake_case__ : Optional[int] = { 'unet': unet, 'scheduler': scheduler, 'vae': vae, 'text_encoder': text_encoder, 'tokenizer': tokenizer, 'safety_checker': None, 'feature_extractor': None, } return components def __a ( self , __UpperCamelCase , __UpperCamelCase=0 ) -> Dict: '''simple docstring''' snake_case__ : Optional[Any] = floats_tensor((1, 3, 32, 32) , rng=random.Random(__UpperCamelCase ) ).to(__UpperCamelCase ) snake_case__ : int = image.cpu().permute(0 , 2 , 3 , 1 )[0] snake_case__ : Union[str, Any] = Image.fromarray(np.uinta(__UpperCamelCase ) ).convert('RGB' ) if str(__UpperCamelCase ).startswith('mps' ): snake_case__ : str = torch.manual_seed(__UpperCamelCase ) else: snake_case__ : Dict = torch.Generator(device=__UpperCamelCase ).manual_seed(__UpperCamelCase ) snake_case__ : str = { 'prompt': 'A painting of a squirrel eating a burger', 'image': image, 'generator': generator, 'num_inference_steps': 2, 'guidance_scale': 6.0, 'image_guidance_scale': 1, 'output_type': 'numpy', } return inputs def __a ( self ) -> Tuple: '''simple docstring''' snake_case__ : Dict = 'cpu' # ensure determinism for the device-dependent torch.Generator snake_case__ : Optional[int] = self.get_dummy_components() snake_case__ : str = StableDiffusionInstructPixaPixPipeline(**__UpperCamelCase ) snake_case__ : Optional[int] = sd_pipe.to(__UpperCamelCase ) sd_pipe.set_progress_bar_config(disable=__UpperCamelCase ) snake_case__ : Tuple = self.get_dummy_inputs(__UpperCamelCase ) snake_case__ : List[str] = sd_pipe(**__UpperCamelCase ).images snake_case__ : List[str] = image[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) snake_case__ : str = np.array([0.7_5_2_6, 0.3_7_5_0, 0.4_5_4_7, 0.6_1_1_7, 0.5_8_6_6, 0.5_0_1_6, 0.4_3_2_7, 0.5_6_4_2, 0.4_8_1_5] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 def __a ( self ) -> Any: '''simple docstring''' snake_case__ : Dict = 'cpu' # ensure determinism for the device-dependent torch.Generator snake_case__ : Union[str, Any] = self.get_dummy_components() snake_case__ : Optional[Any] = StableDiffusionInstructPixaPixPipeline(**__UpperCamelCase ) snake_case__ : List[Any] = sd_pipe.to(__UpperCamelCase ) sd_pipe.set_progress_bar_config(disable=__UpperCamelCase ) snake_case__ : Union[str, Any] = self.get_dummy_inputs(__UpperCamelCase ) snake_case__ : List[str] = 'french fries' snake_case__ : Optional[Any] = sd_pipe(**__UpperCamelCase , negative_prompt=__UpperCamelCase ) snake_case__ : Union[str, Any] = output.images snake_case__ : Tuple = image[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) snake_case__ : Any = np.array([0.7_5_1_1, 0.3_6_4_2, 0.4_5_5_3, 0.6_2_3_6, 0.5_7_9_7, 0.5_0_1_3, 0.4_3_4_3, 0.5_6_1_1, 0.4_8_3_1] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 def __a ( self ) -> int: '''simple docstring''' snake_case__ : Optional[int] = 'cpu' # ensure determinism for the device-dependent torch.Generator snake_case__ : List[str] = self.get_dummy_components() snake_case__ : str = StableDiffusionInstructPixaPixPipeline(**__UpperCamelCase ) snake_case__ : str = sd_pipe.to(__UpperCamelCase ) sd_pipe.set_progress_bar_config(disable=__UpperCamelCase ) snake_case__ : Dict = self.get_dummy_inputs(__UpperCamelCase ) snake_case__ : Any = [inputs['prompt']] * 2 snake_case__ : Optional[int] = np.array(inputs['image'] ).astype(np.floataa ) / 2_5_5.0 snake_case__ : Optional[int] = torch.from_numpy(__UpperCamelCase ).unsqueeze(0 ).to(__UpperCamelCase ) snake_case__ : Any = image / 2 + 0.5 snake_case__ : Optional[Any] = image.permute(0 , 3 , 1 , 2 ) snake_case__ : List[Any] = image.repeat(2 , 1 , 1 , 1 ) snake_case__ : Optional[int] = sd_pipe(**__UpperCamelCase ).images snake_case__ : Union[str, Any] = image[-1, -3:, -3:, -1] assert image.shape == (2, 32, 32, 3) snake_case__ : List[Any] = np.array([0.5_8_1_2, 0.5_7_4_8, 0.5_2_2_2, 0.5_9_0_8, 0.5_6_9_5, 0.7_1_7_4, 0.6_8_0_4, 0.5_5_2_3, 0.5_5_7_9] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 def __a ( self ) -> Union[str, Any]: '''simple docstring''' snake_case__ : Optional[int] = 'cpu' # ensure determinism for the device-dependent torch.Generator snake_case__ : Optional[int] = self.get_dummy_components() snake_case__ : Tuple = EulerAncestralDiscreteScheduler( beta_start=0.0_0_0_8_5 , beta_end=0.0_1_2 , beta_schedule='scaled_linear' ) snake_case__ : int = StableDiffusionInstructPixaPixPipeline(**__UpperCamelCase ) snake_case__ : List[str] = sd_pipe.to(__UpperCamelCase ) sd_pipe.set_progress_bar_config(disable=__UpperCamelCase ) snake_case__ : str = self.get_dummy_inputs(__UpperCamelCase ) snake_case__ : Any = sd_pipe(**__UpperCamelCase ).images snake_case__ : int = image[0, -3:, -3:, -1] snake_case__ : Tuple = [round(__UpperCamelCase , 4 ) for x in image_slice.flatten().tolist()] print(','.join([str(__UpperCamelCase ) for x in slice] ) ) assert image.shape == (1, 32, 32, 3) snake_case__ : List[Any] = np.array([0.7_4_1_7, 0.3_8_4_2, 0.4_7_3_2, 0.5_7_7_6, 0.5_8_9_1, 0.5_1_3_9, 0.4_0_5_2, 0.5_6_7_3, 0.4_9_8_6] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 def __a ( self ) -> int: '''simple docstring''' super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) def __a ( self ) -> Any: '''simple docstring''' snake_case__ : Optional[int] = self.get_dummy_components() snake_case__ : int = StableDiffusionInstructPixaPixPipeline(**__UpperCamelCase ) snake_case__ : Union[str, Any] = VaeImageProcessor(do_resize=__UpperCamelCase , do_normalize=__UpperCamelCase ) snake_case__ : Optional[int] = pipe.to(__UpperCamelCase ) pipe.set_progress_bar_config(disable=__UpperCamelCase ) snake_case__ : Optional[Any] = pipe(**self.get_dummy_inputs_by_type(__UpperCamelCase , input_image_type='pt' ) )[0] snake_case__ : Union[str, Any] = components['vae'] snake_case__ : str = self.get_dummy_inputs_by_type(__UpperCamelCase , input_image_type='pt' ) for image_param in self.image_latents_params: if image_param in inputs.keys(): snake_case__ : List[str] = vae.encode(inputs[image_param] ).latent_dist.mode() snake_case__ : Dict = pipe(**__UpperCamelCase )[0] snake_case__ : str = np.abs(out - out_latents_inputs ).max() self.assertLess(__UpperCamelCase , 1E-4 , 'passing latents as image input generate different result from passing image' ) @slow @require_torch_gpu class __snake_case ( unittest.TestCase ): def __a ( self ) -> List[str]: '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def __a ( self , __UpperCamelCase=0 ) -> Dict: '''simple docstring''' snake_case__ : Optional[Any] = torch.manual_seed(__UpperCamelCase ) snake_case__ : List[str] = load_image( 'https://huggingface.co/datasets/diffusers/test-arrays/resolve/main/stable_diffusion_pix2pix/example.jpg' ) snake_case__ : int = { 'prompt': 'turn him into a cyborg', 'image': image, 'generator': generator, 'num_inference_steps': 3, 'guidance_scale': 7.5, 'image_guidance_scale': 1.0, 'output_type': 'numpy', } return inputs def __a ( self ) -> Dict: '''simple docstring''' snake_case__ : Any = StableDiffusionInstructPixaPixPipeline.from_pretrained( 'timbrooks/instruct-pix2pix' , safety_checker=__UpperCamelCase ) pipe.to(__UpperCamelCase ) pipe.set_progress_bar_config(disable=__UpperCamelCase ) pipe.enable_attention_slicing() snake_case__ : Tuple = self.get_inputs() snake_case__ : List[Any] = pipe(**__UpperCamelCase ).images snake_case__ : Union[str, Any] = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 512, 512, 3) snake_case__ : Dict = np.array([0.5_9_0_2, 0.6_0_1_5, 0.6_0_2_7, 0.5_9_8_3, 0.6_0_9_2, 0.6_0_6_1, 0.5_7_6_5, 0.5_7_8_5, 0.5_5_5_5] ) assert np.abs(expected_slice - image_slice ).max() < 1E-3 def __a ( self ) -> str: '''simple docstring''' snake_case__ : Any = StableDiffusionInstructPixaPixPipeline.from_pretrained( 'timbrooks/instruct-pix2pix' , safety_checker=__UpperCamelCase ) snake_case__ : Tuple = LMSDiscreteScheduler.from_config(pipe.scheduler.config ) pipe.to(__UpperCamelCase ) pipe.set_progress_bar_config(disable=__UpperCamelCase ) pipe.enable_attention_slicing() snake_case__ : Dict = self.get_inputs() snake_case__ : Dict = pipe(**__UpperCamelCase ).images snake_case__ : Union[str, Any] = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 512, 512, 3) snake_case__ : List[Any] = np.array([0.6_5_7_8, 0.6_8_1_7, 0.6_9_7_2, 0.6_7_6_1, 0.6_8_5_6, 0.6_9_1_6, 0.6_4_2_8, 0.6_5_1_6, 0.6_3_0_1] ) assert np.abs(expected_slice - image_slice ).max() < 1E-3 def __a ( self ) -> Optional[Any]: '''simple docstring''' snake_case__ : Tuple = StableDiffusionInstructPixaPixPipeline.from_pretrained( 'timbrooks/instruct-pix2pix' , safety_checker=__UpperCamelCase ) snake_case__ : Tuple = DDIMScheduler.from_config(pipe.scheduler.config ) pipe.to(__UpperCamelCase ) pipe.set_progress_bar_config(disable=__UpperCamelCase ) pipe.enable_attention_slicing() snake_case__ : Optional[int] = self.get_inputs() snake_case__ : Optional[int] = pipe(**__UpperCamelCase ).images snake_case__ : Tuple = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 512, 512, 3) snake_case__ : int = np.array([0.3_8_2_8, 0.3_8_3_4, 0.3_8_1_8, 0.3_7_9_2, 0.3_8_6_5, 0.3_7_5_2, 0.3_7_9_2, 0.3_8_4_7, 0.3_7_5_3] ) assert np.abs(expected_slice - image_slice ).max() < 1E-3 def __a ( self ) -> Union[str, Any]: '''simple docstring''' snake_case__ : int = 0 def callback_fn(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> None: snake_case__ : List[Any] = True nonlocal number_of_steps number_of_steps += 1 if step == 1: snake_case__ : Any = latents.detach().cpu().numpy() assert latents.shape == (1, 4, 64, 64) snake_case__ : int = latents[0, -3:, -3:, -1] snake_case__ : List[str] = np.array([-0.2_4_6_3, -0.4_6_4_4, -0.9_7_5_6, 1.5_1_7_6, 1.4_4_1_4, 0.7_8_6_6, 0.9_8_9_7, 0.8_5_2_1, 0.7_9_8_3] ) assert np.abs(latents_slice.flatten() - expected_slice ).max() < 5E-2 elif step == 2: snake_case__ : Dict = latents.detach().cpu().numpy() assert latents.shape == (1, 4, 64, 64) snake_case__ : Dict = latents[0, -3:, -3:, -1] snake_case__ : Optional[Any] = np.array([-0.2_6_4_4, -0.4_6_2_6, -0.9_6_5_3, 1.5_1_7_6, 1.4_5_5_1, 0.7_6_8_6, 0.9_8_0_5, 0.8_4_5_2, 0.8_1_1_5] ) assert np.abs(latents_slice.flatten() - expected_slice ).max() < 5E-2 snake_case__ : str = False snake_case__ : List[Any] = StableDiffusionInstructPixaPixPipeline.from_pretrained( 'timbrooks/instruct-pix2pix' , safety_checker=__UpperCamelCase , torch_dtype=torch.floataa ) snake_case__ : int = pipe.to(__UpperCamelCase ) pipe.set_progress_bar_config(disable=__UpperCamelCase ) pipe.enable_attention_slicing() snake_case__ : int = self.get_inputs() pipe(**__UpperCamelCase , callback=__UpperCamelCase , callback_steps=1 ) assert callback_fn.has_been_called assert number_of_steps == 3 def __a ( self ) -> Any: '''simple docstring''' torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats() snake_case__ : str = StableDiffusionInstructPixaPixPipeline.from_pretrained( 'timbrooks/instruct-pix2pix' , safety_checker=__UpperCamelCase , torch_dtype=torch.floataa ) snake_case__ : Dict = pipe.to(__UpperCamelCase ) pipe.set_progress_bar_config(disable=__UpperCamelCase ) pipe.enable_attention_slicing(1 ) pipe.enable_sequential_cpu_offload() snake_case__ : str = self.get_inputs() snake_case__ : Tuple = pipe(**__UpperCamelCase ) snake_case__ : List[Any] = torch.cuda.max_memory_allocated() # make sure that less than 2.2 GB is allocated assert mem_bytes < 2.2 * 10**9 def __a ( self ) -> int: '''simple docstring''' snake_case__ : int = self.get_inputs() # resize to resolution that is divisible by 8 but not 16 or 32 snake_case__ : Tuple = inputs['image'].resize((504, 504) ) snake_case__ : str = 'timbrooks/instruct-pix2pix' snake_case__ : Tuple = StableDiffusionInstructPixaPixPipeline.from_pretrained( __UpperCamelCase , safety_checker=__UpperCamelCase , ) pipe.to(__UpperCamelCase ) pipe.set_progress_bar_config(disable=__UpperCamelCase ) pipe.enable_attention_slicing() snake_case__ : str = pipe(**__UpperCamelCase ) snake_case__ : List[Any] = output.images[0] snake_case__ : List[Any] = image[255:258, 383:386, -1] assert image.shape == (504, 504, 3) snake_case__ : List[str] = np.array([0.2_7_2_6, 0.2_5_2_9, 0.2_6_6_4, 0.2_6_5_5, 0.2_6_4_1, 0.2_6_4_2, 0.2_5_9_1, 0.2_6_4_9, 0.2_5_9_0] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5E-3
699
0
from __future__ import annotations lowerCAmelCase__ : int = [] def UpperCamelCase__ ( A__ , A__ , A__ ) -> bool: for i in range(len(A__ ) ): if board[row][i] == 1: return False for i in range(len(A__ ) ): if board[i][column] == 1: return False for i, j in zip(range(A__ , -1 , -1 ) , range(A__ , -1 , -1 ) ): if board[i][j] == 1: return False for i, j in zip(range(A__ , -1 , -1 ) , range(A__ , len(A__ ) ) ): if board[i][j] == 1: return False return True def UpperCamelCase__ ( A__ , A__ ) -> bool: if row >= len(A__ ): solution.append(A__ ) printboard(A__ ) print() return True for i in range(len(A__ ) ): if is_safe(A__ , A__ , A__ ): snake_case__ : Dict = 1 solve(A__ , row + 1 ) snake_case__ : List[Any] = 0 return False def UpperCamelCase__ ( A__ ) -> None: for i in range(len(A__ ) ): for j in range(len(A__ ) ): if board[i][j] == 1: print('Q' , end=' ' ) else: print('.' , end=' ' ) print() # n=int(input("The no. of queens")) lowerCAmelCase__ : List[Any] = 8 lowerCAmelCase__ : 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))
711
from .data_collator import ( DataCollatorForLanguageModeling, DataCollatorForPermutationLanguageModeling, DataCollatorForSeqaSeq, DataCollatorForSOP, DataCollatorForTokenClassification, DataCollatorForWholeWordMask, DataCollatorWithPadding, DefaultDataCollator, default_data_collator, ) from .metrics import glue_compute_metrics, xnli_compute_metrics from .processors import ( DataProcessor, InputExample, InputFeatures, SingleSentenceClassificationProcessor, SquadExample, SquadFeatures, SquadVaProcessor, SquadVaProcessor, glue_convert_examples_to_features, glue_output_modes, glue_processors, glue_tasks_num_labels, squad_convert_examples_to_features, xnli_output_modes, xnli_processors, xnli_tasks_num_labels, )
699
0
from __future__ import annotations def UpperCamelCase__ ( A__ , A__ , A__ ) -> int | float: if len(__snake_case ) == 0: raise ValueError('find_max() arg is an empty sequence' ) if ( left >= len(__snake_case ) or left < -len(__snake_case ) or right >= len(__snake_case ) or right < -len(__snake_case ) ): raise IndexError('list index out of range' ) if left == right: return nums[left] snake_case__ : int = (left + right) >> 1 # the middle snake_case__ : Dict = find_max(__snake_case , __snake_case , __snake_case ) # find max in range[left, mid] snake_case__ : Dict = find_max(__snake_case , mid + 1 , __snake_case ) # find max in range[mid + 1, right] return left_max if left_max >= right_max else right_max if __name__ == "__main__": import doctest doctest.testmod(verbose=True)
712
from dataclasses import dataclass, field from typing import Optional from transformers import AutoConfig, AutoImageProcessor, AutoTokenizer, FlaxVisionEncoderDecoderModel, HfArgumentParser @dataclass class __snake_case : __lowerCamelCase = field( metadata={"""help""": """The output directory where the model will be written."""} ,) __lowerCamelCase = field( metadata={ """help""": ( """The encoder model checkpoint for weights initialization.""" """Don't set if you want to train an encoder model from scratch.""" ) } ,) __lowerCamelCase = field( metadata={ """help""": ( """The decoder model checkpoint for weights initialization.""" """Don't set if you want to train a decoder model from scratch.""" ) } ,) __lowerCamelCase = field( default=_lowerCamelCase ,metadata={"""help""": """Pretrained encoder config name or path if not the same as encoder_model_name"""} ) __lowerCamelCase = field( default=_lowerCamelCase ,metadata={"""help""": """Pretrained decoder config name or path if not the same as decoder_model_name"""} ) def UpperCamelCase__ ( ) -> Union[str, Any]: snake_case__ : str = HfArgumentParser((ModelArguments,) ) ((snake_case__) , ) : Dict = parser.parse_args_into_dataclasses() # Load pretrained model and tokenizer # Use explicit specified encoder config if model_args.encoder_config_name: snake_case__ : List[str] = AutoConfig.from_pretrained(model_args.encoder_config_name ) # Use pretrained encoder model's config else: snake_case__ : Optional[int] = AutoConfig.from_pretrained(model_args.encoder_model_name_or_path ) # Use explicit specified decoder config if model_args.decoder_config_name: snake_case__ : Optional[Any] = AutoConfig.from_pretrained(model_args.decoder_config_name ) # Use pretrained decoder model's config else: snake_case__ : List[str] = AutoConfig.from_pretrained(model_args.decoder_model_name_or_path ) # necessary for `from_encoder_decoder_pretrained` when `decoder_config` is passed snake_case__ : Any = True snake_case__ : Dict = True snake_case__ : Tuple = FlaxVisionEncoderDecoderModel.from_encoder_decoder_pretrained( encoder_pretrained_model_name_or_path=model_args.encoder_model_name_or_path , decoder_pretrained_model_name_or_path=model_args.decoder_model_name_or_path , encoder_config=A__ , decoder_config=A__ , ) # GPT2 only has bos/eos tokens but not decoder_start/pad tokens snake_case__ : Optional[Any] = decoder_config.decoder_start_token_id snake_case__ : Tuple = decoder_config.pad_token_id if decoder_start_token_id is None: snake_case__ : Optional[Any] = decoder_config.bos_token_id if pad_token_id is None: snake_case__ : int = decoder_config.eos_token_id # This is necessary to make Flax's generate() work snake_case__ : Union[str, Any] = decoder_config.eos_token_id snake_case__ : Optional[int] = decoder_start_token_id snake_case__ : int = pad_token_id snake_case__ : Tuple = AutoImageProcessor.from_pretrained(model_args.encoder_model_name_or_path ) snake_case__ : int = AutoTokenizer.from_pretrained(model_args.decoder_model_name_or_path ) snake_case__ : Tuple = tokenizer.convert_ids_to_tokens(model.config.pad_token_id ) model.save_pretrained(model_args.output_dir ) image_processor.save_pretrained(model_args.output_dir ) tokenizer.save_pretrained(model_args.output_dir ) if __name__ == "__main__": main()
699
0
import unittest from transformers import AutoTokenizer, is_flax_available from transformers.testing_utils import require_flax, require_sentencepiece, require_tokenizers, slow if is_flax_available(): import jax.numpy as jnp from transformers import FlaxXLMRobertaModel @require_sentencepiece @require_tokenizers @require_flax class __snake_case ( unittest.TestCase ): @slow def __a ( self ) -> Union[str, Any]: '''simple docstring''' snake_case__ : int = FlaxXLMRobertaModel.from_pretrained('xlm-roberta-base' ) snake_case__ : Union[str, Any] = AutoTokenizer.from_pretrained('xlm-roberta-base' ) snake_case__ : Tuple = """The dog is cute and lives in the garden house""" snake_case__ : Optional[Any] = jnp.array([tokenizer.encode(lowerCamelCase_ )] ) snake_case__ : List[str] = (1, 12, 768) # batch_size, sequence_length, embedding_vector_dim snake_case__ : Tuple = jnp.array( [[-0.0_1_0_1, 0.1_2_1_8, -0.0_8_0_3, 0.0_8_0_1, 0.1_3_2_7, 0.0_7_7_6, -0.1_2_1_5, 0.2_3_8_3, 0.3_3_3_8, 0.3_1_0_6, 0.0_3_0_0, 0.0_2_5_2]] ) snake_case__ : Any = model(lowerCamelCase_ )["""last_hidden_state"""] self.assertEqual(output.shape , lowerCamelCase_ ) # compare the actual values for a slice of last dim self.assertTrue(jnp.allclose(output[:, :, -1] , lowerCamelCase_ , atol=1E-3 ) )
713
import logging import os from dataclasses import dataclass, field from typing import Dict, Optional import datasets import numpy as np import tensorflow as tf from transformers import ( AutoConfig, AutoTokenizer, EvalPrediction, HfArgumentParser, PreTrainedTokenizer, TFAutoModelForSequenceClassification, TFTrainer, TFTrainingArguments, ) from transformers.utils import logging as hf_logging hf_logging.set_verbosity_info() hf_logging.enable_default_handler() hf_logging.enable_explicit_format() def UpperCamelCase__ ( A__ , A__ , A__ , A__ , A__ , A__ = None , ) -> Optional[int]: snake_case__ : List[str] = {} if train_file is not None: snake_case__ : Tuple = [train_file] if eval_file is not None: snake_case__ : Dict = [eval_file] if test_file is not None: snake_case__ : str = [test_file] snake_case__ : Optional[Any] = datasets.load_dataset('csv' , data_files=A__ ) snake_case__ : Any = list(ds[list(files.keys() )[0]].features.keys() ) snake_case__ : Optional[Any] = features_name.pop(A__ ) snake_case__ : Optional[Any] = list(set(ds[list(files.keys() )[0]][label_name] ) ) snake_case__ : str = {label: i for i, label in enumerate(A__ )} snake_case__ : int = tokenizer.model_input_names snake_case__ : int = {} if len(A__ ) == 1: for k in files.keys(): snake_case__ : str = ds[k].map( lambda A__ : tokenizer.batch_encode_plus( example[features_name[0]] , truncation=A__ , max_length=A__ , padding='max_length' ) , batched=A__ , ) elif len(A__ ) == 2: for k in files.keys(): snake_case__ : Optional[int] = ds[k].map( lambda A__ : tokenizer.batch_encode_plus( (example[features_name[0]], example[features_name[1]]) , truncation=A__ , max_length=A__ , padding='max_length' , ) , batched=A__ , ) def gen_train(): for ex in transformed_ds[datasets.Split.TRAIN]: snake_case__ : int = {k: v for k, v in ex.items() if k in input_names} snake_case__ : Any = labelaid[ex[label_name]] yield (d, label) def gen_val(): for ex in transformed_ds[datasets.Split.VALIDATION]: snake_case__ : int = {k: v for k, v in ex.items() if k in input_names} snake_case__ : Union[str, Any] = labelaid[ex[label_name]] yield (d, label) def gen_test(): for ex in transformed_ds[datasets.Split.TEST]: snake_case__ : Dict = {k: v for k, v in ex.items() if k in input_names} snake_case__ : List[str] = labelaid[ex[label_name]] yield (d, label) snake_case__ : Any = ( tf.data.Dataset.from_generator( A__ , ({k: tf.intaa for k in input_names}, tf.intaa) , ({k: tf.TensorShape([None] ) for k in input_names}, tf.TensorShape([] )) , ) if datasets.Split.TRAIN in transformed_ds else None ) if train_ds is not None: snake_case__ : str = train_ds.apply(tf.data.experimental.assert_cardinality(len(ds[datasets.Split.TRAIN] ) ) ) snake_case__ : Optional[int] = ( tf.data.Dataset.from_generator( A__ , ({k: tf.intaa for k in input_names}, tf.intaa) , ({k: tf.TensorShape([None] ) for k in input_names}, tf.TensorShape([] )) , ) if datasets.Split.VALIDATION in transformed_ds else None ) if val_ds is not None: snake_case__ : Optional[int] = val_ds.apply(tf.data.experimental.assert_cardinality(len(ds[datasets.Split.VALIDATION] ) ) ) snake_case__ : List[str] = ( tf.data.Dataset.from_generator( A__ , ({k: tf.intaa for k in input_names}, tf.intaa) , ({k: tf.TensorShape([None] ) for k in input_names}, tf.TensorShape([] )) , ) if datasets.Split.TEST in transformed_ds else None ) if test_ds is not None: snake_case__ : str = test_ds.apply(tf.data.experimental.assert_cardinality(len(ds[datasets.Split.TEST] ) ) ) return train_ds, val_ds, test_ds, labelaid lowerCAmelCase__ : List[str] = logging.getLogger(__name__) @dataclass class __snake_case : __lowerCamelCase = field(metadata={"""help""": """Which column contains the label"""} ) __lowerCamelCase = field(default=_lowerCamelCase ,metadata={"""help""": """The path of the training file"""} ) __lowerCamelCase = field(default=_lowerCamelCase ,metadata={"""help""": """The path of the development file"""} ) __lowerCamelCase = field(default=_lowerCamelCase ,metadata={"""help""": """The path of the test file"""} ) __lowerCamelCase = field( default=128 ,metadata={ """help""": ( """The maximum total input sequence length after tokenization. Sequences longer """ """than this will be truncated, sequences shorter will be padded.""" ) } ,) __lowerCamelCase = field( default=_lowerCamelCase ,metadata={"""help""": """Overwrite the cached training and evaluation sets"""} ) @dataclass class __snake_case : __lowerCamelCase = field( metadata={"""help""": """Path to pretrained model or model identifier from huggingface.co/models"""} ) __lowerCamelCase = field( default=_lowerCamelCase ,metadata={"""help""": """Pretrained config name or path if not the same as model_name"""} ) __lowerCamelCase = field( default=_lowerCamelCase ,metadata={"""help""": """Pretrained tokenizer name or path if not the same as model_name"""} ) __lowerCamelCase = field(default=_lowerCamelCase ,metadata={"""help""": """Set this flag to use fast tokenization."""} ) # If you want to tweak more attributes on your tokenizer, you should do it in a distinct script, # or just modify its tokenizer_config.json. __lowerCamelCase = field( default=_lowerCamelCase ,metadata={"""help""": """Where do you want to store the pretrained models downloaded from huggingface.co"""} ,) def UpperCamelCase__ ( ) -> Union[str, Any]: # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. snake_case__ : int = HfArgumentParser((ModelArguments, DataTrainingArguments, TFTrainingArguments) ) snake_case__ , snake_case__ , snake_case__ : Dict = parser.parse_args_into_dataclasses() if ( os.path.exists(training_args.output_dir ) and os.listdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir ): raise ValueError( F"""Output directory ({training_args.output_dir}) already exists and is not empty. Use""" ' --overwrite_output_dir to overcome.' ) # Setup logging logging.basicConfig( format='%(asctime)s - %(levelname)s - %(name)s - %(message)s' , datefmt='%m/%d/%Y %H:%M:%S' , level=logging.INFO , ) logger.info( F"""n_replicas: {training_args.n_replicas}, distributed training: {bool(training_args.n_replicas > 1 )}, """ F"""16-bits training: {training_args.fpaa}""" ) logger.info(F"""Training/evaluation parameters {training_args}""" ) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. snake_case__ : Dict = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , ) snake_case__ , snake_case__ , snake_case__ , snake_case__ : Dict = get_tfds( train_file=data_args.train_file , eval_file=data_args.dev_file , test_file=data_args.test_file , tokenizer=A__ , label_column_id=data_args.label_column_id , max_seq_length=data_args.max_seq_length , ) snake_case__ : Dict = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , num_labels=len(A__ ) , labelaid=A__ , idalabel={id: label for label, id in labelaid.items()} , finetuning_task='text-classification' , cache_dir=model_args.cache_dir , ) with training_args.strategy.scope(): snake_case__ : Optional[Any] = TFAutoModelForSequenceClassification.from_pretrained( model_args.model_name_or_path , from_pt=bool('.bin' in model_args.model_name_or_path ) , config=A__ , cache_dir=model_args.cache_dir , ) def compute_metrics(A__ ) -> Dict: snake_case__ : Optional[Any] = np.argmax(p.predictions , axis=1 ) return {"acc": (preds == p.label_ids).mean()} # Initialize our Trainer snake_case__ : Any = TFTrainer( model=A__ , args=A__ , train_dataset=A__ , eval_dataset=A__ , compute_metrics=A__ , ) # Training if training_args.do_train: trainer.train() trainer.save_model() tokenizer.save_pretrained(training_args.output_dir ) # Evaluation snake_case__ : Dict = {} if training_args.do_eval: logger.info('*** Evaluate ***' ) snake_case__ : Tuple = trainer.evaluate() snake_case__ : Any = os.path.join(training_args.output_dir , 'eval_results.txt' ) with open(A__ , 'w' ) as writer: logger.info('***** Eval results *****' ) for key, value in result.items(): logger.info(F""" {key} = {value}""" ) writer.write(F"""{key} = {value}\n""" ) results.update(A__ ) return results if __name__ == "__main__": main()
699
0
import unittest import numpy as np import torch from .utils_summarization import build_mask, compute_token_type_ids, process_story, truncate_or_pad class __snake_case ( unittest.TestCase ): def __a ( self ) -> List[str]: '''simple docstring''' snake_case__ : List[Any] = 10 def __a ( self ) -> List[Any]: '''simple docstring''' snake_case__ : List[str] = [1, 2, 3, 4] snake_case__ : Optional[int] = [1, 2, 3, 4, 0, 0, 0, 0, 0, 0] self.assertEqual(truncate_or_pad(UpperCamelCase_ , self.block_size , 0 ) , UpperCamelCase_ ) def __a ( self ) -> Dict: '''simple docstring''' snake_case__ : Tuple = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] snake_case__ : Optional[Any] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] self.assertEqual(truncate_or_pad(UpperCamelCase_ , self.block_size , 0 ) , UpperCamelCase_ ) def __a ( self ) -> List[str]: '''simple docstring''' snake_case__ : int = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] snake_case__ : List[str] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] self.assertEqual(truncate_or_pad(UpperCamelCase_ , self.block_size , 0 ) , UpperCamelCase_ ) def __a ( self ) -> List[str]: '''simple docstring''' snake_case__ : List[Any] = 'It was the year of Our Lord one thousand seven hundred and\n seventy-five.\n\nSpiritual revelations were conceded to England at that\n favoured period, as at this.' snake_case__ , snake_case__ : List[Any] = process_story(UpperCamelCase_ ) self.assertEqual(UpperCamelCase_ , [] ) def __a ( self ) -> Tuple: '''simple docstring''' snake_case__ : Dict = '' snake_case__ , snake_case__ : Union[str, Any] = process_story(UpperCamelCase_ ) self.assertEqual(UpperCamelCase_ , [] ) self.assertEqual(UpperCamelCase_ , [] ) def __a ( self ) -> Dict: '''simple docstring''' snake_case__ : str = ( 'It was the year of Our Lord one thousand seven hundred and ' 'seventy-five\n\nSpiritual revelations were conceded to England ' 'at that favoured period, as at this.\n@highlight\n\nIt was the best of times' ) snake_case__ , snake_case__ : Optional[int] = process_story(UpperCamelCase_ ) snake_case__ : int = [ 'It was the year of Our Lord one thousand seven hundred and seventy-five.', 'Spiritual revelations were conceded to England at that favoured period, as at this.', ] self.assertEqual(UpperCamelCase_ , UpperCamelCase_ ) snake_case__ : List[str] = ['It was the best of times.'] self.assertEqual(UpperCamelCase_ , UpperCamelCase_ ) def __a ( self ) -> Dict: '''simple docstring''' snake_case__ : Tuple = torch.tensor([1, 2, 3, 4] ) snake_case__ : Optional[Any] = torch.tensor([1, 1, 1, 1] ) np.testing.assert_array_equal(build_mask(UpperCamelCase_ , 0 ).numpy() , expected.numpy() ) def __a ( self ) -> Optional[int]: '''simple docstring''' snake_case__ : Dict = torch.tensor([1, 2, 3, 4, 23, 23, 23] ) snake_case__ : Any = torch.tensor([1, 1, 1, 1, 0, 0, 0] ) np.testing.assert_array_equal(build_mask(UpperCamelCase_ , 23 ).numpy() , expected.numpy() ) def __a ( self ) -> Tuple: '''simple docstring''' snake_case__ : Dict = torch.tensor([8, 2, 3, 4, 1, 1, 1] ) snake_case__ : int = torch.tensor([1, 1, 1, 1, 0, 0, 0] ) np.testing.assert_array_equal(build_mask(UpperCamelCase_ , 1 ).numpy() , expected.numpy() ) def __a ( self ) -> Tuple: '''simple docstring''' snake_case__ : Optional[int] = 101 snake_case__ : Union[str, Any] = torch.tensor([[1, 2, 3, 4, 5, 6], [1, 2, 3, 101, 5, 6], [1, 101, 3, 4, 101, 6]] ) snake_case__ : Tuple = torch.tensor([[1, 1, 1, 1, 1, 1], [1, 1, 1, 0, 0, 0], [1, 0, 0, 0, 1, 1]] ) snake_case__ : List[str] = compute_token_type_ids(UpperCamelCase_ , UpperCamelCase_ ) np.testing.assert_array_equal(UpperCamelCase_ , UpperCamelCase_ )
714
from typing import List import datasets from datasets.tasks import AudioClassification from ..folder_based_builder import folder_based_builder lowerCAmelCase__ : List[Any] = datasets.utils.logging.get_logger(__name__) class __snake_case ( folder_based_builder.FolderBasedBuilderConfig ): __lowerCamelCase = None __lowerCamelCase = None class __snake_case ( folder_based_builder.FolderBasedBuilder ): __lowerCamelCase = datasets.Audio() __lowerCamelCase = """audio""" __lowerCamelCase = AudioFolderConfig __lowerCamelCase = 42 # definition at the bottom of the script __lowerCamelCase = AudioClassification(audio_column="""audio""" ,label_column="""label""" ) lowerCAmelCase__ : Tuple = [ '''.aiff''', '''.au''', '''.avr''', '''.caf''', '''.flac''', '''.htk''', '''.svx''', '''.mat4''', '''.mat5''', '''.mpc2k''', '''.ogg''', '''.paf''', '''.pvf''', '''.raw''', '''.rf64''', '''.sd2''', '''.sds''', '''.ircam''', '''.voc''', '''.w64''', '''.wav''', '''.nist''', '''.wavex''', '''.wve''', '''.xi''', '''.mp3''', '''.opus''', ] lowerCAmelCase__ : List[Any] = AUDIO_EXTENSIONS
699
0
from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Features, Sequence, Value from .base import TaskTemplate @dataclass(frozen=_UpperCamelCase ) class __snake_case ( _UpperCamelCase ): __lowerCamelCase = field(default="""question-answering-extractive""" ,metadata={"""include_in_asdict_even_if_is_default""": True} ) __lowerCamelCase = Features({"""question""": Value("""string""" ), """context""": Value("""string""" )} ) __lowerCamelCase = Features( { """answers""": Sequence( { """text""": Value("""string""" ), """answer_start""": Value("""int32""" ), } ) } ) __lowerCamelCase = """question""" __lowerCamelCase = """context""" __lowerCamelCase = """answers""" @property def __a ( self ) -> Optional[int]: '''simple docstring''' return {self.question_column: "question", self.context_column: "context", self.answers_column: "answers"}
715
import random import unittest import torch from diffusers import IFInpaintingPipeline from diffusers.utils import floats_tensor from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import skip_mps, torch_device from ..pipeline_params import ( TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS, ) from ..test_pipelines_common import PipelineTesterMixin from . import IFPipelineTesterMixin @skip_mps class __snake_case ( _lowerCamelCase ,_lowerCamelCase ,unittest.TestCase ): __lowerCamelCase = IFInpaintingPipeline __lowerCamelCase = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {"""width""", """height"""} __lowerCamelCase = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS __lowerCamelCase = PipelineTesterMixin.required_optional_params - {"""latents"""} def __a ( self ) -> Optional[Any]: '''simple docstring''' return self._get_dummy_components() def __a ( self , __UpperCamelCase , __UpperCamelCase=0 ) -> str: '''simple docstring''' if str(__UpperCamelCase ).startswith('mps' ): snake_case__ : int = torch.manual_seed(__UpperCamelCase ) else: snake_case__ : Union[str, Any] = torch.Generator(device=__UpperCamelCase ).manual_seed(__UpperCamelCase ) snake_case__ : Optional[int] = floats_tensor((1, 3, 32, 32) , rng=random.Random(__UpperCamelCase ) ).to(__UpperCamelCase ) snake_case__ : Dict = floats_tensor((1, 3, 32, 32) , rng=random.Random(__UpperCamelCase ) ).to(__UpperCamelCase ) snake_case__ : Optional[Any] = { 'prompt': 'A painting of a squirrel eating a burger', 'image': image, 'mask_image': mask_image, 'generator': generator, 'num_inference_steps': 2, 'output_type': 'numpy', } return inputs @unittest.skipIf( torch_device != 'cuda' or not is_xformers_available() , reason='XFormers attention is only available with CUDA and `xformers` installed' , ) def __a ( self ) -> List[Any]: '''simple docstring''' self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3 ) def __a ( self ) -> Optional[int]: '''simple docstring''' self._test_save_load_optional_components() @unittest.skipIf(torch_device != 'cuda' , reason='float16 requires CUDA' ) def __a ( self ) -> List[str]: '''simple docstring''' super().test_save_load_floataa(expected_max_diff=1E-1 ) def __a ( self ) -> List[str]: '''simple docstring''' self._test_attention_slicing_forward_pass(expected_max_diff=1E-2 ) def __a ( self ) -> int: '''simple docstring''' self._test_save_load_local() def __a ( self ) -> List[str]: '''simple docstring''' self._test_inference_batch_single_identical( expected_max_diff=1E-2 , )
699
0
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import DetrConfig, DetrForObjectDetection, DetrForSegmentation, DetrImageProcessor, ResNetConfig from transformers.utils import logging logging.set_verbosity_info() lowerCAmelCase__ : Dict = logging.get_logger(__name__) def UpperCamelCase__ ( A__ ) -> str: if "resnet-50" in model_name: snake_case__ : str = ResNetConfig.from_pretrained('microsoft/resnet-50' ) elif "resnet-101" in model_name: snake_case__ : Any = ResNetConfig.from_pretrained('microsoft/resnet-101' ) else: raise ValueError('Model name should include either resnet50 or resnet101' ) snake_case__ : List[Any] = DetrConfig(use_timm_backbone=a__ , backbone_config=a__ ) # set label attributes snake_case__ : str = 'panoptic' in model_name if is_panoptic: snake_case__ : List[Any] = 250 else: snake_case__ : Any = 91 snake_case__ : Dict = 'huggingface/label-files' snake_case__ : int = 'coco-detection-id2label.json' snake_case__ : Any = json.load(open(hf_hub_download(a__ , a__ , repo_type='dataset' ) , 'r' ) ) snake_case__ : Optional[int] = {int(a__ ): v for k, v in idalabel.items()} snake_case__ : Union[str, Any] = idalabel snake_case__ : str = {v: k for k, v in idalabel.items()} return config, is_panoptic def UpperCamelCase__ ( A__ ) -> Union[str, Any]: snake_case__ : Any = [] # stem # fmt: off rename_keys.append(('backbone.0.body.conv1.weight', 'backbone.conv_encoder.model.embedder.embedder.convolution.weight') ) rename_keys.append(('backbone.0.body.bn1.weight', 'backbone.conv_encoder.model.embedder.embedder.normalization.weight') ) rename_keys.append(('backbone.0.body.bn1.bias', 'backbone.conv_encoder.model.embedder.embedder.normalization.bias') ) rename_keys.append(('backbone.0.body.bn1.running_mean', 'backbone.conv_encoder.model.embedder.embedder.normalization.running_mean') ) rename_keys.append(('backbone.0.body.bn1.running_var', 'backbone.conv_encoder.model.embedder.embedder.normalization.running_var') ) # stages for stage_idx in range(len(config.backbone_config.depths ) ): for layer_idx in range(config.backbone_config.depths[stage_idx] ): # shortcut if layer_idx == 0: rename_keys.append( ( F"""backbone.0.body.layer{stage_idx + 1}.{layer_idx}.downsample.0.weight""", F"""backbone.conv_encoder.model.encoder.stages.{stage_idx}.layers.{layer_idx}.shortcut.convolution.weight""", ) ) rename_keys.append( ( F"""backbone.0.body.layer{stage_idx + 1}.{layer_idx}.downsample.1.weight""", F"""backbone.conv_encoder.model.encoder.stages.{stage_idx}.layers.{layer_idx}.shortcut.normalization.weight""", ) ) rename_keys.append( ( F"""backbone.0.body.layer{stage_idx + 1}.{layer_idx}.downsample.1.bias""", F"""backbone.conv_encoder.model.encoder.stages.{stage_idx}.layers.{layer_idx}.shortcut.normalization.bias""", ) ) rename_keys.append( ( F"""backbone.0.body.layer{stage_idx + 1}.{layer_idx}.downsample.1.running_mean""", F"""backbone.conv_encoder.model.encoder.stages.{stage_idx}.layers.{layer_idx}.shortcut.normalization.running_mean""", ) ) rename_keys.append( ( F"""backbone.0.body.layer{stage_idx + 1}.{layer_idx}.downsample.1.running_var""", F"""backbone.conv_encoder.model.encoder.stages.{stage_idx}.layers.{layer_idx}.shortcut.normalization.running_var""", ) ) # 3 convs for i in range(3 ): rename_keys.append( ( F"""backbone.0.body.layer{stage_idx + 1}.{layer_idx}.conv{i+1}.weight""", F"""backbone.conv_encoder.model.encoder.stages.{stage_idx}.layers.{layer_idx}.layer.{i}.convolution.weight""", ) ) rename_keys.append( ( F"""backbone.0.body.layer{stage_idx + 1}.{layer_idx}.bn{i+1}.weight""", F"""backbone.conv_encoder.model.encoder.stages.{stage_idx}.layers.{layer_idx}.layer.{i}.normalization.weight""", ) ) rename_keys.append( ( F"""backbone.0.body.layer{stage_idx + 1}.{layer_idx}.bn{i+1}.bias""", F"""backbone.conv_encoder.model.encoder.stages.{stage_idx}.layers.{layer_idx}.layer.{i}.normalization.bias""", ) ) rename_keys.append( ( F"""backbone.0.body.layer{stage_idx + 1}.{layer_idx}.bn{i+1}.running_mean""", F"""backbone.conv_encoder.model.encoder.stages.{stage_idx}.layers.{layer_idx}.layer.{i}.normalization.running_mean""", ) ) rename_keys.append( ( F"""backbone.0.body.layer{stage_idx + 1}.{layer_idx}.bn{i+1}.running_var""", F"""backbone.conv_encoder.model.encoder.stages.{stage_idx}.layers.{layer_idx}.layer.{i}.normalization.running_var""", ) ) # fmt: on for i in range(config.encoder_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append( ( F"""transformer.encoder.layers.{i}.self_attn.out_proj.weight""", F"""encoder.layers.{i}.self_attn.out_proj.weight""", ) ) rename_keys.append( (F"""transformer.encoder.layers.{i}.self_attn.out_proj.bias""", F"""encoder.layers.{i}.self_attn.out_proj.bias""") ) rename_keys.append((F"""transformer.encoder.layers.{i}.linear1.weight""", F"""encoder.layers.{i}.fc1.weight""") ) rename_keys.append((F"""transformer.encoder.layers.{i}.linear1.bias""", F"""encoder.layers.{i}.fc1.bias""") ) rename_keys.append((F"""transformer.encoder.layers.{i}.linear2.weight""", F"""encoder.layers.{i}.fc2.weight""") ) rename_keys.append((F"""transformer.encoder.layers.{i}.linear2.bias""", F"""encoder.layers.{i}.fc2.bias""") ) rename_keys.append( (F"""transformer.encoder.layers.{i}.norm1.weight""", F"""encoder.layers.{i}.self_attn_layer_norm.weight""") ) rename_keys.append( (F"""transformer.encoder.layers.{i}.norm1.bias""", F"""encoder.layers.{i}.self_attn_layer_norm.bias""") ) rename_keys.append( (F"""transformer.encoder.layers.{i}.norm2.weight""", F"""encoder.layers.{i}.final_layer_norm.weight""") ) rename_keys.append((F"""transformer.encoder.layers.{i}.norm2.bias""", F"""encoder.layers.{i}.final_layer_norm.bias""") ) # decoder layers: 2 times output projection, 2 feedforward neural networks and 3 layernorms rename_keys.append( ( F"""transformer.decoder.layers.{i}.self_attn.out_proj.weight""", F"""decoder.layers.{i}.self_attn.out_proj.weight""", ) ) rename_keys.append( (F"""transformer.decoder.layers.{i}.self_attn.out_proj.bias""", F"""decoder.layers.{i}.self_attn.out_proj.bias""") ) rename_keys.append( ( F"""transformer.decoder.layers.{i}.multihead_attn.out_proj.weight""", F"""decoder.layers.{i}.encoder_attn.out_proj.weight""", ) ) rename_keys.append( ( F"""transformer.decoder.layers.{i}.multihead_attn.out_proj.bias""", F"""decoder.layers.{i}.encoder_attn.out_proj.bias""", ) ) rename_keys.append((F"""transformer.decoder.layers.{i}.linear1.weight""", F"""decoder.layers.{i}.fc1.weight""") ) rename_keys.append((F"""transformer.decoder.layers.{i}.linear1.bias""", F"""decoder.layers.{i}.fc1.bias""") ) rename_keys.append((F"""transformer.decoder.layers.{i}.linear2.weight""", F"""decoder.layers.{i}.fc2.weight""") ) rename_keys.append((F"""transformer.decoder.layers.{i}.linear2.bias""", F"""decoder.layers.{i}.fc2.bias""") ) rename_keys.append( (F"""transformer.decoder.layers.{i}.norm1.weight""", F"""decoder.layers.{i}.self_attn_layer_norm.weight""") ) rename_keys.append( (F"""transformer.decoder.layers.{i}.norm1.bias""", F"""decoder.layers.{i}.self_attn_layer_norm.bias""") ) rename_keys.append( (F"""transformer.decoder.layers.{i}.norm2.weight""", F"""decoder.layers.{i}.encoder_attn_layer_norm.weight""") ) rename_keys.append( (F"""transformer.decoder.layers.{i}.norm2.bias""", F"""decoder.layers.{i}.encoder_attn_layer_norm.bias""") ) rename_keys.append( (F"""transformer.decoder.layers.{i}.norm3.weight""", F"""decoder.layers.{i}.final_layer_norm.weight""") ) rename_keys.append((F"""transformer.decoder.layers.{i}.norm3.bias""", F"""decoder.layers.{i}.final_layer_norm.bias""") ) # convolutional projection + query embeddings + layernorm of decoder + class and bounding box heads rename_keys.extend( [ ('input_proj.weight', 'input_projection.weight'), ('input_proj.bias', 'input_projection.bias'), ('query_embed.weight', 'query_position_embeddings.weight'), ('transformer.decoder.norm.weight', 'decoder.layernorm.weight'), ('transformer.decoder.norm.bias', 'decoder.layernorm.bias'), ('class_embed.weight', 'class_labels_classifier.weight'), ('class_embed.bias', 'class_labels_classifier.bias'), ('bbox_embed.layers.0.weight', 'bbox_predictor.layers.0.weight'), ('bbox_embed.layers.0.bias', 'bbox_predictor.layers.0.bias'), ('bbox_embed.layers.1.weight', 'bbox_predictor.layers.1.weight'), ('bbox_embed.layers.1.bias', 'bbox_predictor.layers.1.bias'), ('bbox_embed.layers.2.weight', 'bbox_predictor.layers.2.weight'), ('bbox_embed.layers.2.bias', 'bbox_predictor.layers.2.bias'), ] ) return rename_keys def UpperCamelCase__ ( A__ , A__ , A__ ) -> List[Any]: snake_case__ : Optional[int] = state_dict.pop(a__ ) snake_case__ : List[str] = val def UpperCamelCase__ ( A__ , A__=False ) -> List[Any]: snake_case__ : Optional[int] = '' if is_panoptic: snake_case__ : int = 'detr.' # first: transformer encoder for i in range(6 ): # read in weights + bias of input projection layer (in PyTorch's MultiHeadAttention, this is a single matrix + bias) snake_case__ : Any = state_dict.pop(F"""{prefix}transformer.encoder.layers.{i}.self_attn.in_proj_weight""" ) snake_case__ : int = state_dict.pop(F"""{prefix}transformer.encoder.layers.{i}.self_attn.in_proj_bias""" ) # next, add query, keys and values (in that order) to the state dict snake_case__ : Optional[Any] = in_proj_weight[:256, :] snake_case__ : int = in_proj_bias[:256] snake_case__ : Dict = in_proj_weight[256:512, :] snake_case__ : int = in_proj_bias[256:512] snake_case__ : Tuple = in_proj_weight[-256:, :] snake_case__ : Tuple = in_proj_bias[-256:] # next: transformer decoder (which is a bit more complex because it also includes cross-attention) for i in range(6 ): # read in weights + bias of input projection layer of self-attention snake_case__ : List[Any] = state_dict.pop(F"""{prefix}transformer.decoder.layers.{i}.self_attn.in_proj_weight""" ) snake_case__ : int = state_dict.pop(F"""{prefix}transformer.decoder.layers.{i}.self_attn.in_proj_bias""" ) # next, add query, keys and values (in that order) to the state dict snake_case__ : List[str] = in_proj_weight[:256, :] snake_case__ : Optional[int] = in_proj_bias[:256] snake_case__ : Tuple = in_proj_weight[256:512, :] snake_case__ : Optional[int] = in_proj_bias[256:512] snake_case__ : Tuple = in_proj_weight[-256:, :] snake_case__ : Optional[Any] = in_proj_bias[-256:] # read in weights + bias of input projection layer of cross-attention snake_case__ : Optional[Any] = state_dict.pop( F"""{prefix}transformer.decoder.layers.{i}.multihead_attn.in_proj_weight""" ) snake_case__ : List[Any] = state_dict.pop(F"""{prefix}transformer.decoder.layers.{i}.multihead_attn.in_proj_bias""" ) # next, add query, keys and values (in that order) of cross-attention to the state dict snake_case__ : Optional[Any] = in_proj_weight_cross_attn[:256, :] snake_case__ : Optional[Any] = in_proj_bias_cross_attn[:256] snake_case__ : Union[str, Any] = in_proj_weight_cross_attn[256:512, :] snake_case__ : Union[str, Any] = in_proj_bias_cross_attn[256:512] snake_case__ : Tuple = in_proj_weight_cross_attn[-256:, :] snake_case__ : Tuple = in_proj_bias_cross_attn[-256:] def UpperCamelCase__ ( ) -> Tuple: snake_case__ : List[Any] = 'http://images.cocodataset.org/val2017/000000039769.jpg' snake_case__ : List[Any] = Image.open(requests.get(a__ , stream=a__ ).raw ) return im @torch.no_grad() def UpperCamelCase__ ( A__ , A__=None , A__=False ) -> Dict: snake_case__ , snake_case__ : int = get_detr_config(a__ ) # load original model from torch hub snake_case__ : List[str] = { 'detr-resnet-50': 'detr_resnet50', 'detr-resnet-101': 'detr_resnet101', } logger.info(F"""Converting model {model_name}...""" ) snake_case__ : Union[str, Any] = torch.hub.load('facebookresearch/detr' , model_name_to_original_name[model_name] , pretrained=a__ ).eval() snake_case__ : List[str] = detr.state_dict() # rename keys for src, dest in create_rename_keys(a__ ): if is_panoptic: snake_case__ : Any = 'detr.' + src rename_key(a__ , a__ , a__ ) # query, key and value matrices need special treatment read_in_q_k_v(a__ , is_panoptic=a__ ) # important: we need to prepend a prefix to each of the base model keys as the head models use different attributes for them snake_case__ : Tuple = 'detr.model.' if is_panoptic else 'model.' for key in state_dict.copy().keys(): if is_panoptic: if ( key.startswith('detr' ) and not key.startswith('class_labels_classifier' ) and not key.startswith('bbox_predictor' ) ): snake_case__ : Any = state_dict.pop(a__ ) snake_case__ : Dict = val elif "class_labels_classifier" in key or "bbox_predictor" in key: snake_case__ : List[Any] = state_dict.pop(a__ ) snake_case__ : Dict = val elif key.startswith('bbox_attention' ) or key.startswith('mask_head' ): continue else: snake_case__ : Optional[int] = state_dict.pop(a__ ) snake_case__ : List[Any] = val else: if not key.startswith('class_labels_classifier' ) and not key.startswith('bbox_predictor' ): snake_case__ : Tuple = state_dict.pop(a__ ) snake_case__ : Tuple = val # finally, create HuggingFace model and load state dict snake_case__ : List[str] = DetrForSegmentation(a__ ) if is_panoptic else DetrForObjectDetection(a__ ) model.load_state_dict(a__ ) model.eval() # verify our conversion on an image snake_case__ : int = 'coco_panoptic' if is_panoptic else 'coco_detection' snake_case__ : Optional[Any] = DetrImageProcessor(format=a__ ) snake_case__ : Any = processor(images=prepare_img() , return_tensors='pt' ) snake_case__ : List[str] = encoding['pixel_values'] snake_case__ : Optional[int] = detr(a__ ) snake_case__ : Optional[Any] = model(a__ ) assert torch.allclose(outputs.logits , original_outputs['pred_logits'] , atol=1e-3 ) assert torch.allclose(outputs.pred_boxes , original_outputs['pred_boxes'] , atol=1e-3 ) if is_panoptic: assert torch.allclose(outputs.pred_masks , original_outputs['pred_masks'] , atol=1e-4 ) print('Looks ok!' ) if pytorch_dump_folder_path is not None: # Save model and image processor logger.info(F"""Saving PyTorch model and image processor to {pytorch_dump_folder_path}...""" ) Path(a__ ).mkdir(exist_ok=a__ ) model.save_pretrained(a__ ) processor.save_pretrained(a__ ) if push_to_hub: # Upload model and image processor to the hub logger.info('Uploading PyTorch model and image processor to the hub...' ) model.push_to_hub(F"""nielsr/{model_name}""" ) processor.push_to_hub(F"""nielsr/{model_name}""" ) if __name__ == "__main__": lowerCAmelCase__ : Dict = argparse.ArgumentParser() parser.add_argument( '''--model_name''', default='''detr-resnet-50''', type=str, choices=['''detr-resnet-50''', '''detr-resnet-101'''], help='''Name of the DETR model you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the folder to output PyTorch model.''' ) parser.add_argument('''--push_to_hub''', action='''store_true''', help='''Whether to push the model to the hub or not.''') lowerCAmelCase__ : Optional[int] = parser.parse_args() convert_detr_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
716
import unittest from transformers import BertGenerationTokenizer from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_torch, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin lowerCAmelCase__ : List[Any] = '''▁''' lowerCAmelCase__ : int = get_tests_dir('''fixtures/test_sentencepiece.model''') @require_sentencepiece class __snake_case ( _lowerCamelCase ,unittest.TestCase ): __lowerCamelCase = BertGenerationTokenizer __lowerCamelCase = False __lowerCamelCase = True def __a ( self ) -> Optional[int]: '''simple docstring''' super().setUp() snake_case__ : str = BertGenerationTokenizer(__UpperCamelCase , keep_accents=__UpperCamelCase ) tokenizer.save_pretrained(self.tmpdirname ) def __a ( self ) -> Optional[int]: '''simple docstring''' snake_case__ : List[str] = '<s>' snake_case__ : Dict = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(__UpperCamelCase ) , __UpperCamelCase ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(__UpperCamelCase ) , __UpperCamelCase ) def __a ( self ) -> List[str]: '''simple docstring''' snake_case__ : Tuple = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '<unk>' ) self.assertEqual(vocab_keys[1] , '<s>' ) self.assertEqual(vocab_keys[-1] , '<pad>' ) self.assertEqual(len(__UpperCamelCase ) , 1002 ) def __a ( self ) -> int: '''simple docstring''' self.assertEqual(self.get_tokenizer().vocab_size , 1000 ) def __a ( self ) -> Tuple: '''simple docstring''' snake_case__ : Optional[Any] = BertGenerationTokenizer(__UpperCamelCase , keep_accents=__UpperCamelCase ) snake_case__ : int = tokenizer.tokenize('This is a test' ) self.assertListEqual(__UpperCamelCase , ['▁This', '▁is', '▁a', '▁t', 'est'] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(__UpperCamelCase ) , [285, 46, 10, 170, 382] , ) snake_case__ : Any = tokenizer.tokenize('I was born in 92000, and this is falsé.' ) self.assertListEqual( __UpperCamelCase , [ SPIECE_UNDERLINE + 'I', SPIECE_UNDERLINE + 'was', SPIECE_UNDERLINE + 'b', 'or', 'n', SPIECE_UNDERLINE + 'in', SPIECE_UNDERLINE + '', '9', '2', '0', '0', '0', ',', SPIECE_UNDERLINE + 'and', SPIECE_UNDERLINE + 'this', SPIECE_UNDERLINE + 'is', SPIECE_UNDERLINE + 'f', 'al', 's', 'é', '.', ] , ) snake_case__ : Optional[Any] = tokenizer.convert_tokens_to_ids(__UpperCamelCase ) self.assertListEqual( __UpperCamelCase , [8, 21, 84, 55, 24, 19, 7, 0, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 0, 4] , ) snake_case__ : int = tokenizer.convert_ids_to_tokens(__UpperCamelCase ) self.assertListEqual( __UpperCamelCase , [ SPIECE_UNDERLINE + 'I', SPIECE_UNDERLINE + 'was', SPIECE_UNDERLINE + 'b', 'or', 'n', SPIECE_UNDERLINE + 'in', SPIECE_UNDERLINE + '', '<unk>', '2', '0', '0', '0', ',', SPIECE_UNDERLINE + 'and', SPIECE_UNDERLINE + 'this', SPIECE_UNDERLINE + 'is', SPIECE_UNDERLINE + 'f', 'al', 's', '<unk>', '.', ] , ) @cached_property def __a ( self ) -> Dict: '''simple docstring''' return BertGenerationTokenizer.from_pretrained('google/bert_for_seq_generation_L-24_bbc_encoder' ) @slow def __a ( self ) -> Any: '''simple docstring''' snake_case__ : int = 'Hello World!' snake_case__ : Union[str, Any] = [18536, 2260, 101] self.assertListEqual(__UpperCamelCase , self.big_tokenizer.encode(__UpperCamelCase ) ) @slow def __a ( self ) -> Optional[int]: '''simple docstring''' snake_case__ : str = ( 'This is a very long text with a lot of weird characters, such as: . , ~ ? ( ) " [ ] ! : - . Also we will' ' add words that should not exsist and be tokenized to <unk>, such as saoneuhaoesuth' ) snake_case__ : List[Any] = [ 871, 419, 358, 946, 991, 2521, 452, 358, 1357, 387, 7751, 3536, 112, 985, 456, 126, 865, 938, 5400, 5734, 458, 1368, 467, 786, 2462, 5246, 1159, 633, 865, 4519, 457, 582, 852, 2557, 427, 916, 508, 405, 34324, 497, 391, 408, 11342, 1244, 385, 100, 938, 985, 456, 574, 362, 12597, 3200, 3129, 1172, ] self.assertListEqual(__UpperCamelCase , self.big_tokenizer.encode(__UpperCamelCase ) ) @require_torch @slow def __a ( self ) -> List[str]: '''simple docstring''' import torch from transformers import BertGenerationConfig, BertGenerationEncoder # Build sequence snake_case__ : Optional[int] = list(self.big_tokenizer.get_vocab().keys() )[:10] snake_case__ : Optional[int] = ' '.join(__UpperCamelCase ) snake_case__ : int = self.big_tokenizer.encode_plus(__UpperCamelCase , return_tensors='pt' , return_token_type_ids=__UpperCamelCase ) snake_case__ : Tuple = self.big_tokenizer.batch_encode_plus( [sequence + ' ' + sequence] , return_tensors='pt' , return_token_type_ids=__UpperCamelCase ) snake_case__ : Dict = BertGenerationConfig() snake_case__ : List[str] = BertGenerationEncoder(__UpperCamelCase ) assert model.get_input_embeddings().weight.shape[0] >= self.big_tokenizer.vocab_size with torch.no_grad(): model(**__UpperCamelCase ) model(**__UpperCamelCase ) @slow def __a ( self ) -> Dict: '''simple docstring''' snake_case__ : Optional[int] = {'input_ids': [[39286, 458, 36335, 2001, 456, 13073, 13266, 455, 113, 7746, 1741, 11157, 391, 13073, 13266, 455, 113, 3967, 35412, 113, 4936, 109, 3870, 2377, 113, 30084, 45720, 458, 134, 17496, 112, 503, 11672, 113, 118, 112, 5665, 13347, 38687, 112, 1496, 31389, 112, 3268, 47264, 134, 962, 112, 16377, 8035, 23130, 430, 12169, 15518, 28592, 458, 146, 41697, 109, 391, 12169, 15518, 16689, 458, 146, 41358, 109, 452, 726, 4034, 111, 763, 35412, 5082, 388, 1903, 111, 9051, 391, 2870, 48918, 1900, 1123, 550, 998, 112, 9586, 15985, 455, 391, 410, 22955, 37636, 114], [448, 17496, 419, 3663, 385, 763, 113, 27533, 2870, 3283, 13043, 1639, 24713, 523, 656, 24013, 18550, 2521, 517, 27014, 21244, 420, 1212, 1465, 391, 927, 4833, 388, 578, 11786, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [484, 2169, 7687, 21932, 18146, 726, 363, 17032, 3391, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], 'attention_mask': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=__UpperCamelCase , model_name='google/bert_for_seq_generation_L-24_bbc_encoder' , revision='c817d1fd1be2ffa69431227a1fe320544943d4db' , )
699
0
from collections import namedtuple lowerCAmelCase__ : Optional[Any] = namedtuple('''from_to''', '''from_ to''') lowerCAmelCase__ : Optional[Any] = { '''cubicmeter''': from_to(1, 1), '''litre''': from_to(0.0_01, 10_00), '''kilolitre''': from_to(1, 1), '''gallon''': from_to(0.0_04_54, 2_64.1_72), '''cubicyard''': from_to(0.7_64_55, 1.3_07_95), '''cubicfoot''': from_to(0.0_28, 35.31_47), '''cup''': from_to(0.0_00_23_65_88, 42_26.75), } def UpperCamelCase__ ( A__ , A__ , A__ ) -> float: if from_type not in METRIC_CONVERSION: raise ValueError( F"""Invalid \'from_type\' value: {from_type!r} Supported values are:\n""" + ', '.join(snake_case__ ) ) if to_type not in METRIC_CONVERSION: raise ValueError( F"""Invalid \'to_type\' value: {to_type!r}. Supported values are:\n""" + ', '.join(snake_case__ ) ) return value * METRIC_CONVERSION[from_type].from_ * METRIC_CONVERSION[to_type].to if __name__ == "__main__": import doctest doctest.testmod()
717
import random import torch from huggingface_hub import HfApi from diffusers import UNetaDModel lowerCAmelCase__ : List[str] = HfApi() lowerCAmelCase__ : str = {} # fmt: off lowerCAmelCase__ : int = torch.tensor([ -0.75_15, -1.68_83, 0.24_20, 0.03_00, 0.63_47, 1.34_33, -1.17_43, -3.74_67, 1.23_42, -2.24_85, 0.46_36, 0.80_76, -0.79_91, 0.39_69, 0.84_98, 0.91_89, -1.88_87, -3.35_22, 0.76_39, 0.20_40, 0.62_71, -2.71_48, -1.63_16, 3.08_39, 0.31_86, 0.27_21, -0.97_59, -1.24_61, 2.62_57, 1.35_57 ]) lowerCAmelCase__ : Dict = torch.tensor([ -2.36_39, -2.53_44, 0.00_54, -0.66_74, 1.59_90, 1.01_58, 0.31_24, -2.14_36, 1.87_95, -2.54_29, -0.15_66, -0.39_73, 1.24_90, 2.64_47, 1.22_83, -0.52_08, -2.81_54, -3.51_19, 2.38_38, 1.20_33, 1.72_01, -2.12_56, -1.45_76, 2.79_48, 2.42_04, -0.97_52, -1.25_46, 0.80_27, 3.27_58, 3.13_65 ]) lowerCAmelCase__ : Dict = torch.tensor([ -0.65_31, -0.68_91, -0.31_72, -0.53_75, -0.91_40, -0.53_67, -0.11_75, -0.78_69, -0.38_08, -0.45_13, -0.20_98, -0.00_83, 0.31_83, 0.51_40, 0.22_47, -0.13_04, -0.13_02, -0.28_02, -0.20_84, -0.20_25, -0.49_67, -0.48_73, -0.08_61, 0.69_25, 0.02_50, 0.12_90, -0.15_43, 0.63_16, 1.04_60, 1.49_43 ]) lowerCAmelCase__ : List[str] = torch.tensor([ 0.09_11, 0.11_07, 0.01_82, 0.04_35, -0.08_05, -0.06_08, 0.03_81, 0.21_72, -0.02_80, 0.13_27, -0.02_99, -0.02_55, -0.00_50, -0.11_70, -0.10_46, 0.03_09, 0.13_67, 0.17_28, -0.05_33, -0.07_48, -0.05_34, 0.16_24, 0.03_84, -0.18_05, -0.07_07, 0.06_42, 0.02_20, -0.01_34, -0.13_33, -0.15_05 ]) lowerCAmelCase__ : Union[str, Any] = torch.tensor([ 0.13_21, 0.13_37, 0.04_40, 0.06_22, -0.05_91, -0.03_70, 0.05_03, 0.21_33, -0.01_77, 0.14_15, -0.01_16, -0.01_12, 0.00_44, -0.09_80, -0.07_89, 0.03_95, 0.15_02, 0.17_85, -0.04_88, -0.05_14, -0.04_04, 0.15_39, 0.04_54, -0.15_59, -0.06_65, 0.06_59, 0.03_83, -0.00_05, -0.12_66, -0.13_86 ]) lowerCAmelCase__ : List[Any] = torch.tensor([ 0.11_54, 0.12_18, 0.03_07, 0.05_26, -0.07_11, -0.05_41, 0.03_66, 0.20_78, -0.02_67, 0.13_17, -0.02_26, -0.01_93, -0.00_14, -0.10_55, -0.09_02, 0.03_30, 0.13_91, 0.17_09, -0.05_62, -0.06_93, -0.05_60, 0.14_82, 0.03_81, -0.16_83, -0.06_81, 0.06_61, 0.03_31, -0.00_46, -0.12_68, -0.14_31 ]) lowerCAmelCase__ : Optional[Any] = torch.tensor([ 0.11_92, 0.12_40, 0.04_14, 0.06_06, -0.05_57, -0.04_12, 0.04_30, 0.20_42, -0.02_00, 0.13_85, -0.01_15, -0.01_32, 0.00_17, -0.09_65, -0.08_02, 0.03_98, 0.14_33, 0.17_47, -0.04_58, -0.05_33, -0.04_07, 0.15_45, 0.04_19, -0.15_74, -0.06_45, 0.06_26, 0.03_41, -0.00_10, -0.11_99, -0.13_90 ]) lowerCAmelCase__ : List[str] = torch.tensor([ 0.10_75, 0.10_74, 0.02_05, 0.04_31, -0.07_74, -0.06_07, 0.02_98, 0.20_42, -0.03_20, 0.12_67, -0.02_81, -0.02_50, -0.00_64, -0.10_91, -0.09_46, 0.02_90, 0.13_28, 0.16_50, -0.05_80, -0.07_38, -0.05_86, 0.14_40, 0.03_37, -0.17_46, -0.07_12, 0.06_05, 0.02_50, -0.00_99, -0.13_16, -0.14_73 ]) lowerCAmelCase__ : List[str] = torch.tensor([ -1.45_72, -2.04_81, -0.04_14, -0.60_05, 1.41_36, 0.58_48, 0.40_28, -2.73_30, 1.22_12, -2.12_28, 0.21_55, 0.40_39, 0.76_62, 2.05_35, 0.74_77, -0.32_43, -2.17_58, -2.76_48, 1.69_47, 0.70_26, 1.23_38, -1.60_78, -0.86_82, 2.28_10, 1.85_74, -0.57_18, -0.55_86, -0.01_86, 2.34_15, 2.12_51]) lowerCAmelCase__ : List[Any] = torch.tensor([ -1.36_90, -1.97_20, -0.40_90, -0.69_66, 1.46_60, 0.99_38, -0.13_85, -2.73_24, 0.77_36, -1.89_17, 0.29_23, 0.42_93, 0.16_93, 1.41_12, 1.18_87, -0.31_81, -2.21_60, -2.63_81, 1.31_70, 0.81_63, 0.92_40, -1.65_44, -0.60_99, 2.52_59, 1.64_30, -0.90_90, -0.93_92, -0.01_26, 2.42_68, 2.32_66 ]) lowerCAmelCase__ : Tuple = torch.tensor([ -1.35_25, -1.96_28, -0.39_56, -0.68_60, 1.46_64, 1.00_14, -0.12_59, -2.72_12, 0.77_72, -1.88_11, 0.29_96, 0.43_88, 0.17_04, 1.40_29, 1.17_01, -0.30_27, -2.20_53, -2.62_87, 1.33_50, 0.81_31, 0.92_74, -1.62_92, -0.60_98, 2.51_31, 1.65_05, -0.89_58, -0.92_98, -0.01_51, 2.42_57, 2.33_55 ]) lowerCAmelCase__ : List[str] = torch.tensor([ -2.05_85, -2.78_97, -0.28_50, -0.89_40, 1.90_52, 0.57_02, 0.63_45, -3.89_59, 1.59_32, -3.23_19, 0.19_74, 0.02_87, 1.75_66, 2.65_43, 0.83_87, -0.53_51, -3.27_36, -4.33_75, 2.90_29, 1.63_90, 1.46_40, -2.17_01, -1.90_13, 2.93_41, 3.49_81, -0.62_55, -1.16_44, -0.15_91, 3.70_97, 3.20_66 ]) lowerCAmelCase__ : Dict = torch.tensor([ -2.31_39, -2.55_94, -0.01_97, -0.67_85, 1.70_01, 1.16_06, 0.30_75, -2.17_40, 1.80_71, -2.56_30, -0.09_26, -0.38_11, 1.21_16, 2.62_46, 1.27_31, -0.53_98, -2.81_53, -3.61_40, 2.38_93, 1.32_62, 1.62_58, -2.18_56, -1.32_67, 2.83_95, 2.37_79, -1.06_23, -1.24_68, 0.89_59, 3.33_67, 3.22_43 ]) lowerCAmelCase__ : Dict = torch.tensor([ -2.06_28, -2.76_67, -0.20_89, -0.82_63, 2.05_39, 0.59_92, 0.64_95, -3.83_36, 1.60_25, -3.28_17, 0.17_21, -0.06_33, 1.75_16, 2.70_39, 0.81_00, -0.59_08, -3.21_13, -4.43_43, 2.92_57, 1.36_32, 1.55_62, -2.14_89, -1.98_94, 3.05_60, 3.33_96, -0.73_28, -1.04_17, 0.03_83, 3.70_93, 3.23_43 ]) lowerCAmelCase__ : Any = torch.tensor([ -1.45_74, -2.05_69, -0.04_73, -0.61_17, 1.40_18, 0.57_69, 0.41_29, -2.73_44, 1.22_41, -2.13_97, 0.20_00, 0.39_37, 0.76_16, 2.04_53, 0.73_24, -0.33_91, -2.17_46, -2.77_44, 1.69_63, 0.69_21, 1.21_87, -1.61_72, -0.88_77, 2.24_39, 1.84_71, -0.58_39, -0.56_05, -0.04_64, 2.32_50, 2.12_19 ]) # fmt: on lowerCAmelCase__ : Any = api.list_models(filter='''diffusers''') for mod in models: if "google" in mod.author or mod.modelId == "CompVis/ldm-celebahq-256": lowerCAmelCase__ : List[str] = '''/home/patrick/google_checkpoints/''' + mod.modelId.split('''/''')[-1] print(F'''Started running {mod.modelId}!!!''') if mod.modelId.startswith('''CompVis'''): lowerCAmelCase__ : int = UNetaDModel.from_pretrained(local_checkpoint, subfolder='''unet''') else: lowerCAmelCase__ : str = UNetaDModel.from_pretrained(local_checkpoint) torch.manual_seed(0) random.seed(0) lowerCAmelCase__ : Any = torch.randn(1, model.config.in_channels, model.config.sample_size, model.config.sample_size) lowerCAmelCase__ : List[str] = torch.tensor([10] * noise.shape[0]) with torch.no_grad(): lowerCAmelCase__ : int = model(noise, time_step).sample assert torch.allclose( logits[0, 0, 0, :30], results['''_'''.join('''_'''.join(mod.modelId.split('''/''')).split('''-'''))], atol=1E-3 ) print(F'''{mod.modelId} has passed successfully!!!''')
699
0
import tempfile import unittest from transformers import TaConfig, is_torch_available from transformers.testing_utils import ( require_sentencepiece, require_tokenizers, require_torch, slow, torch_device, ) from ...generation.test_utils import GenerationTesterMixin from ...test_modeling_common import ModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import AutoTokenizer, UMTaForConditionalGeneration, UMTaForQuestionAnswering, UMTaModel class __snake_case : def __init__( self , __UpperCamelCase , __UpperCamelCase=99 , __UpperCamelCase=13 , __UpperCamelCase=7 , __UpperCamelCase=9 , __UpperCamelCase=True , __UpperCamelCase=True , __UpperCamelCase=False , __UpperCamelCase=32 , __UpperCamelCase=5 , __UpperCamelCase=4 , __UpperCamelCase=37 , __UpperCamelCase=8 , __UpperCamelCase=0.1 , __UpperCamelCase=0.0_0_2 , __UpperCamelCase=1 , __UpperCamelCase=0 , __UpperCamelCase=0 , __UpperCamelCase=None , __UpperCamelCase=None , ) -> Dict: '''simple docstring''' snake_case__ : Union[str, Any] = parent snake_case__ : Union[str, Any] = batch_size snake_case__ : Any = encoder_seq_length snake_case__ : str = decoder_seq_length # For common tests snake_case__ : Optional[int] = self.decoder_seq_length snake_case__ : Optional[Any] = is_training snake_case__ : List[Any] = use_attention_mask snake_case__ : Union[str, Any] = use_labels snake_case__ : Any = vocab_size snake_case__ : Optional[int] = hidden_size snake_case__ : List[str] = num_hidden_layers snake_case__ : Union[str, Any] = num_attention_heads snake_case__ : Any = d_ff snake_case__ : Any = relative_attention_num_buckets snake_case__ : Optional[Any] = dropout_rate snake_case__ : int = initializer_factor snake_case__ : Optional[Any] = eos_token_id snake_case__ : Dict = pad_token_id snake_case__ : Optional[Any] = decoder_start_token_id snake_case__ : Union[str, Any] = None snake_case__ : List[str] = decoder_layers def __a ( self ) -> str: '''simple docstring''' return TaConfig.from_pretrained('google/umt5-base' ) def __a ( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase=None , __UpperCamelCase=None , __UpperCamelCase=None , __UpperCamelCase=None , __UpperCamelCase=None , ) -> Optional[Any]: '''simple docstring''' if attention_mask is None: snake_case__ : Union[str, Any] = input_ids.ne(config.pad_token_id ) if decoder_attention_mask is None: snake_case__ : Any = decoder_input_ids.ne(config.pad_token_id ) if head_mask is None: snake_case__ : List[Any] = torch.ones(config.num_hidden_layers , config.num_attention_heads , device=UpperCAmelCase__ ) if decoder_head_mask is None: snake_case__ : Tuple = torch.ones(config.num_decoder_layers , config.num_attention_heads , device=UpperCAmelCase__ ) if cross_attn_head_mask is None: snake_case__ : Union[str, Any] = torch.ones( config.num_decoder_layers , config.num_attention_heads , device=UpperCAmelCase__ ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": decoder_attention_mask, "head_mask": head_mask, "decoder_head_mask": decoder_head_mask, "cross_attn_head_mask": cross_attn_head_mask, } def __a ( self ) -> List[Any]: '''simple docstring''' snake_case__ : Union[str, Any] = ids_tensor([self.batch_size, self.encoder_seq_length] , self.vocab_size ) snake_case__ : Union[str, Any] = ids_tensor([self.batch_size, self.decoder_seq_length] , self.vocab_size ) # we need to clamp the input ids here to avoid having pad token in between # this is because for NllbMoe the position_ids are prepared such that # all pad tokens have pos id = 2 and rest are between 2..seq_length # and the seq_length here is seq_length - num_pad_tokens # but when using past, there is no way of knowing if the past input ids had # pad tokens in them, which results in incorrect seq_lenth and which in turn results in # position_ids being off by num_pad_tokens in past input snake_case__ : List[str] = input_ids.clamp(self.pad_token_id + 1 ) snake_case__ : List[str] = decoder_input_ids.clamp(self.pad_token_id + 1 ) snake_case__ : str = self.get_config() snake_case__ : Tuple = config.num_attention_heads snake_case__ : List[Any] = self.prepare_inputs_dict(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ) return config, input_dict def __a ( self ) -> Tuple: '''simple docstring''' snake_case__ : List[str] = self.prepare_config_and_inputs() return config, inputs_dict def __a ( self ) -> Optional[int]: '''simple docstring''' return TaConfig( vocab_size=166 , d_model=self.hidden_size , d_ff=self.d_ff , d_kv=self.hidden_size // self.num_attention_heads , num_layers=self.num_hidden_layers , num_decoder_layers=self.decoder_layers , num_heads=self.num_attention_heads , relative_attention_num_buckets=self.relative_attention_num_buckets , dropout_rate=self.dropout_rate , initializer_factor=self.initializer_factor , eos_token_id=self.eos_token_id , bos_token_id=self.pad_token_id , pad_token_id=self.pad_token_id , decoder_start_token_id=self.decoder_start_token_id , ) def __a ( self ) -> str: '''simple docstring''' return TaConfig( vocab_size=self.vocab_size , d_model=self.hidden_size , d_ff=self.d_ff , d_kv=self.hidden_size // self.num_attention_heads , num_layers=self.num_hidden_layers , num_decoder_layers=self.decoder_layers , num_heads=self.num_attention_heads , relative_attention_num_buckets=self.relative_attention_num_buckets , dropout_rate=self.dropout_rate , initializer_factor=self.initializer_factor , eos_token_id=self.eos_token_id , bos_token_id=self.pad_token_id , pad_token_id=self.pad_token_id , decoder_start_token_id=self.decoder_start_token_id , ) def __a ( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , ) -> List[str]: '''simple docstring''' snake_case__ : str = UMTaModel(config=UpperCAmelCase__ ) model.to(UpperCAmelCase__ ) model.eval() snake_case__ : str = model( input_ids=UpperCAmelCase__ , decoder_input_ids=UpperCAmelCase__ , attention_mask=UpperCAmelCase__ , decoder_attention_mask=UpperCAmelCase__ , ) snake_case__ : int = model(input_ids=UpperCAmelCase__ , decoder_input_ids=UpperCAmelCase__ ) snake_case__ : int = result.last_hidden_state snake_case__ : Dict = result.past_key_values snake_case__ : Dict = result.encoder_last_hidden_state self.parent.assertEqual(encoder_output.size() , (self.batch_size, self.encoder_seq_length, self.hidden_size) ) self.parent.assertEqual(decoder_output.size() , (self.batch_size, self.decoder_seq_length, self.hidden_size) ) # There should be `num_layers` key value embeddings stored in decoder_past self.parent.assertEqual(len(UpperCAmelCase__ ) , config.num_layers ) # There should be a self attn key, a self attn value, a cross attn key and a cross attn value stored in each decoder_past tuple self.parent.assertEqual(len(decoder_past[0] ) , 4 ) def __a ( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , ) -> Dict: '''simple docstring''' snake_case__ : int = UMTaModel(config=UpperCAmelCase__ ).get_decoder().to(UpperCAmelCase__ ).eval() # first forward pass snake_case__ : List[Any] = model(UpperCAmelCase__ , use_cache=UpperCAmelCase__ ) snake_case__ : List[Any] = model(UpperCAmelCase__ ) snake_case__ : Any = model(UpperCAmelCase__ , use_cache=UpperCAmelCase__ ) self.parent.assertTrue(len(UpperCAmelCase__ ) == len(UpperCAmelCase__ ) ) self.parent.assertTrue(len(UpperCAmelCase__ ) == len(UpperCAmelCase__ ) + 1 ) snake_case__ : List[str] = outputs.to_tuple() # create hypothetical next token and extent to next_input_ids snake_case__ : Any = ids_tensor((self.batch_size, 1) , config.vocab_size ) # append to next input_ids and snake_case__ : Union[str, Any] = torch.cat([input_ids, next_tokens] , dim=-1 ) snake_case__ : Any = model(UpperCAmelCase__ )['''last_hidden_state'''] snake_case__ : Optional[Any] = model(UpperCAmelCase__ , past_key_values=UpperCAmelCase__ )['''last_hidden_state'''] # select random slice snake_case__ : Tuple = ids_tensor((1,) , output_from_past.shape[-1] ).item() snake_case__ : Union[str, Any] = output_from_no_past[:, -1, random_slice_idx].detach() snake_case__ : Tuple = output_from_past[:, 0, random_slice_idx].detach() # test that outputs are equal for slice self.parent.assertTrue(torch.allclose(UpperCAmelCase__ , UpperCAmelCase__ , atol=1E-3 ) ) def __a ( self , __UpperCamelCase , __UpperCamelCase , ) -> Tuple: '''simple docstring''' snake_case__ : int = UMTaModel(config=UpperCAmelCase__ ).to(UpperCAmelCase__ ).half().eval() snake_case__ : str = model(**UpperCAmelCase__ )['''last_hidden_state'''] self.parent.assertFalse(torch.isnan(UpperCAmelCase__ ).any().item() ) @require_torch class __snake_case ( _lowerCamelCase ,_lowerCamelCase ,_lowerCamelCase ,unittest.TestCase ): __lowerCamelCase = ( (UMTaModel, UMTaForConditionalGeneration, UMTaForQuestionAnswering) if is_torch_available() else () ) __lowerCamelCase = (UMTaForConditionalGeneration,) if is_torch_available() else () __lowerCamelCase = ( { 'conversational': UMTaForConditionalGeneration, 'feature-extraction': UMTaModel, 'summarization': UMTaForConditionalGeneration, 'text2text-generation': UMTaForConditionalGeneration, 'translation': UMTaForConditionalGeneration, 'question-answering': UMTaForQuestionAnswering, } if is_torch_available() else {} ) __lowerCamelCase = True __lowerCamelCase = False __lowerCamelCase = False __lowerCamelCase = True __lowerCamelCase = True # The small UMT5 model needs higher percentages for CPU/MP tests __lowerCamelCase = [0.8, 0.9] def __a ( self ) -> List[str]: '''simple docstring''' snake_case__ : Union[str, Any] = UMTaModelTester(self ) @unittest.skip('Test has a segmentation fault on torch 1.8.0' ) def __a ( self ) -> Optional[Any]: '''simple docstring''' snake_case__ : Tuple = self.model_tester.prepare_config_and_inputs() snake_case__ : Optional[Any] = UMTaModel(config_and_inputs[0] ).to(UpperCAmelCase__ ) with tempfile.TemporaryDirectory() as tmpdirname: torch.onnx.export( UpperCAmelCase__ , (config_and_inputs[1], config_and_inputs[3], config_and_inputs[2]) , F"""{tmpdirname}/t5_test.onnx""" , export_params=UpperCAmelCase__ , opset_version=9 , input_names=['input_ids', 'decoder_input_ids'] , ) @unittest.skipIf(torch_device == 'cpu' , 'Cant do half precision' ) def __a ( self ) -> List[str]: '''simple docstring''' snake_case__ : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model_fpaa_forward(*UpperCAmelCase__ ) def __a ( self ) -> Optional[int]: '''simple docstring''' snake_case__ : Optional[int] = ['''encoder_attentions''', '''decoder_attentions''', '''cross_attentions'''] snake_case__ : List[Any] = self.model_tester.prepare_config_and_inputs() snake_case__ : int = config_and_inputs[0] snake_case__ : Union[str, Any] = UMTaForConditionalGeneration(UpperCAmelCase__ ).eval() model.to(UpperCAmelCase__ ) snake_case__ : str = { '''head_mask''': torch.zeros(config.num_layers , config.num_heads , device=UpperCAmelCase__ ), '''decoder_head_mask''': torch.zeros(config.num_decoder_layers , config.num_heads , device=UpperCAmelCase__ ), '''cross_attn_head_mask''': torch.zeros(config.num_decoder_layers , config.num_heads , device=UpperCAmelCase__ ), } for attn_name, (name, mask) in zip(UpperCAmelCase__ , head_masking.items() ): snake_case__ : int = {name: mask} # Explicitly pass decoder_head_mask as it is required from T5 model when head_mask specified if name == "head_mask": snake_case__ : List[str] = torch.ones( config.num_decoder_layers , config.num_heads , device=UpperCAmelCase__ ) snake_case__ : Union[str, Any] = model.generate( config_and_inputs[1]['input_ids'] , num_beams=1 , max_length=3 , output_attentions=UpperCAmelCase__ , return_dict_in_generate=UpperCAmelCase__ , **UpperCAmelCase__ , ) # We check the state of decoder_attentions and cross_attentions just from the last step snake_case__ : List[str] = out[attn_name] if attn_name == attention_names[0] else out[attn_name][-1] self.assertEqual(sum([w.sum().item() for w in attn_weights] ) , 0.0 ) @unittest.skip('Does not work on the tiny model as we keep hitting edge cases.' ) def __a ( self ) -> int: '''simple docstring''' pass @require_torch @require_sentencepiece @require_tokenizers class __snake_case ( unittest.TestCase ): @slow @unittest.skip( 'Unless we stop stripping left and right by default for all special tokens, the expected ids obtained here will not match the original ones. Wait for https://github.com/huggingface/transformers/pull/23909 to be merged' ) def __a ( self ) -> Union[str, Any]: '''simple docstring''' snake_case__ : Optional[Any] = UMTaForConditionalGeneration.from_pretrained('google/umt5-small' , return_dict=UpperCAmelCase__ ).to(UpperCAmelCase__ ) snake_case__ : int = AutoTokenizer.from_pretrained('google/umt5-small' , use_fast=UpperCAmelCase__ , legacy=UpperCAmelCase__ ) snake_case__ : List[str] = [ '''Bonjour monsieur <extra_id_0> bien <extra_id_1>.''', '''No se como puedo <extra_id_0>.''', '''This is the reason why we <extra_id_0> them.''', '''The <extra_id_0> walks in <extra_id_1>, seats''', '''A <extra_id_0> walks into a bar and orders a <extra_id_1> with <extra_id_2> pinch of <extra_id_3>.''', ] snake_case__ : Dict = tokenizer(UpperCAmelCase__ , return_tensors='pt' , padding=UpperCAmelCase__ ).input_ids # fmt: off snake_case__ : Optional[Any] = torch.tensor( [ [ 38530, 210703, 256299, 1410, 256298, 274, 1, 0,0, 0, 0, 0, 0, 0, 0, 0,0, 0], [ 826, 321, 671, 25922, 256299, 274, 1, 0,0, 0, 0, 0, 0, 0, 0, 0,0, 0], [ 1460, 339, 312, 19014, 10620, 758, 256299, 2355,274, 1, 0, 0, 0, 0, 0, 0,0, 0], [ 517, 256299, 14869, 281, 301, 256298, 275, 119983,1, 0, 0, 0, 0, 0, 0, 0,0, 0], [ 320, 256299, 14869, 281, 2234, 289, 2275, 333,61391, 289, 256298, 543, 256297, 168714, 329, 256296,274, 1], ] ) # fmt: on torch.testing.assert_allclose(UpperCAmelCase__ , UpperCAmelCase__ ) snake_case__ : List[Any] = model.generate(input_ids.to(UpperCAmelCase__ ) ) snake_case__ : int = [ '''<pad><extra_id_0> et<extra_id_1> [eod] <extra_id_2><extra_id_55>.. [eod] 💐 💐 💐 💐 💐 💐 💐 💐 💐 💐 💐 <extra_id_56>ajšietosto<extra_id_56>lleux<extra_id_19><extra_id_6>ajšie</s>''', '''<pad><extra_id_0>.<extra_id_1>.,<0x0A>...spech <0x0A><extra_id_20> <extra_id_21></s><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad>''', '''<pad><extra_id_0> are not going to be a part of the world. We are not going to be a part of<extra_id_1> and<extra_id_2><0x0A><extra_id_48>.<extra_id_48></s><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad>''', '''<pad><extra_id_0> door<extra_id_1>, the door<extra_id_2> 피해[/</s><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad>''', '''<pad><extra_id_0>nyone who<extra_id_1> drink<extra_id_2> a<extra_id_3> alcohol<extra_id_4> A<extra_id_5> A. This<extra_id_6> I<extra_id_7><extra_id_52><extra_id_53></s><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad>''', ] snake_case__ : Tuple = tokenizer.batch_decode(UpperCAmelCase__ ) self.assertEqual(UpperCAmelCase__ , UpperCAmelCase__ )
718
import warnings from ...utils import logging from .image_processing_perceiver import PerceiverImageProcessor lowerCAmelCase__ : Dict = logging.get_logger(__name__) class __snake_case ( _lowerCamelCase ): def __init__( self , *__UpperCamelCase , **__UpperCamelCase ) -> None: '''simple docstring''' warnings.warn( 'The class PerceiverFeatureExtractor is deprecated and will be removed in version 5 of Transformers.' ' Please use PerceiverImageProcessor instead.' , __UpperCamelCase , ) super().__init__(*__UpperCamelCase , **__UpperCamelCase )
699
0
import math from collections.abc import Callable def UpperCamelCase__ ( A__ , A__ , A__ ) -> float: snake_case__ : float = xa snake_case__ : float = xa while True: if x_n == x_na or function(A__ ) == function(A__ ): raise ZeroDivisionError('float division by zero, could not find root' ) snake_case__ : float = x_na - ( function(A__ ) / ((function(A__ ) - function(A__ )) / (x_na - x_n)) ) if abs(x_na - x_na ) < 10**-5: return x_na snake_case__ : int = x_na snake_case__ : Union[str, Any] = x_na def UpperCamelCase__ ( A__ ) -> float: return math.pow(A__ , 3 ) - (2 * x) - 5 if __name__ == "__main__": print(intersection(f, 3, 3.5))
719
import io import itertools import json from dataclasses import dataclass from typing import Optional import pyarrow as pa import pyarrow.json as paj import datasets from datasets.table import table_cast from datasets.utils.file_utils import readline lowerCAmelCase__ : List[Any] = datasets.utils.logging.get_logger(__name__) @dataclass class __snake_case ( datasets.BuilderConfig ): __lowerCamelCase = None __lowerCamelCase = "utf-8" __lowerCamelCase = None __lowerCamelCase = None __lowerCamelCase = True # deprecated __lowerCamelCase = None # deprecated __lowerCamelCase = 10 << 20 # 10MB __lowerCamelCase = None class __snake_case ( datasets.ArrowBasedBuilder ): __lowerCamelCase = JsonConfig def __a ( self ) -> Optional[Any]: '''simple docstring''' if self.config.block_size is not None: logger.warning('The JSON loader parameter `block_size` is deprecated. Please use `chunksize` instead' ) snake_case__ : str = self.config.block_size if self.config.use_threads is not True: logger.warning( 'The JSON loader parameter `use_threads` is deprecated and doesn\'t have any effect anymore.' ) if self.config.newlines_in_values is not None: raise ValueError('The JSON loader parameter `newlines_in_values` is no longer supported' ) return datasets.DatasetInfo(features=self.config.features ) def __a ( self , __UpperCamelCase ) -> Dict: '''simple docstring''' 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}""" ) snake_case__ : int = dl_manager.download_and_extract(self.config.data_files ) if isinstance(__UpperCamelCase , (str, list, tuple) ): snake_case__ : Any = data_files if isinstance(__UpperCamelCase , __UpperCamelCase ): snake_case__ : Optional[Any] = [files] snake_case__ : List[str] = [dl_manager.iter_files(__UpperCamelCase ) for file in files] return [datasets.SplitGenerator(name=datasets.Split.TRAIN , gen_kwargs={'files': files} )] snake_case__ : List[Any] = [] for split_name, files in data_files.items(): if isinstance(__UpperCamelCase , __UpperCamelCase ): snake_case__ : List[Any] = [files] snake_case__ : Any = [dl_manager.iter_files(__UpperCamelCase ) for file in files] splits.append(datasets.SplitGenerator(name=__UpperCamelCase , gen_kwargs={'files': files} ) ) return splits def __a ( self , __UpperCamelCase ) -> pa.Table: '''simple docstring''' if self.config.features is not None: # adding missing columns for column_name in set(self.config.features ) - set(pa_table.column_names ): snake_case__ : List[Any] = self.config.features.arrow_schema.field(__UpperCamelCase ).type snake_case__ : List[str] = pa_table.append_column(__UpperCamelCase , pa.array([None] * len(__UpperCamelCase ) , type=__UpperCamelCase ) ) # more expensive cast to support nested structures with keys in a different order # allows str <-> int/float or str to Audio for example snake_case__ : List[str] = table_cast(__UpperCamelCase , self.config.features.arrow_schema ) return pa_table def __a ( self , __UpperCamelCase ) -> int: '''simple docstring''' for file_idx, file in enumerate(itertools.chain.from_iterable(__UpperCamelCase ) ): # If the file is one json object and if we need to look at the list of items in one specific field if self.config.field is not None: with open(__UpperCamelCase , encoding=self.config.encoding , errors=self.config.encoding_errors ) as f: snake_case__ : Union[str, Any] = json.load(__UpperCamelCase ) # We keep only the field we are interested in snake_case__ : Tuple = dataset[self.config.field] # We accept two format: a list of dicts or a dict of lists if isinstance(__UpperCamelCase , (list, tuple) ): snake_case__ : List[Any] = set().union(*[row.keys() for row in dataset] ) snake_case__ : List[Any] = {col: [row.get(__UpperCamelCase ) for row in dataset] for col in keys} else: snake_case__ : List[Any] = dataset snake_case__ : Dict = pa.Table.from_pydict(__UpperCamelCase ) yield file_idx, self._cast_table(__UpperCamelCase ) # If the file has one json object per line else: with open(__UpperCamelCase , 'rb' ) as f: snake_case__ : Optional[int] = 0 # Use block_size equal to the chunk size divided by 32 to leverage multithreading # Set a default minimum value of 16kB if the chunk size is really small snake_case__ : Tuple = max(self.config.chunksize // 32 , 16 << 10 ) snake_case__ : Optional[Any] = ( self.config.encoding_errors if self.config.encoding_errors is not None else 'strict' ) while True: snake_case__ : Optional[int] = f.read(self.config.chunksize ) if not batch: break # Finish current line try: batch += f.readline() except (AttributeError, io.UnsupportedOperation): batch += readline(__UpperCamelCase ) # PyArrow only accepts utf-8 encoded bytes if self.config.encoding != "utf-8": snake_case__ : int = batch.decode(self.config.encoding , errors=__UpperCamelCase ).encode('utf-8' ) try: while True: try: snake_case__ : List[str] = paj.read_json( io.BytesIO(__UpperCamelCase ) , read_options=paj.ReadOptions(block_size=__UpperCamelCase ) ) break except (pa.ArrowInvalid, pa.ArrowNotImplementedError) as e: if ( isinstance(__UpperCamelCase , pa.ArrowInvalid ) and "straddling" not in str(__UpperCamelCase ) or block_size > len(__UpperCamelCase ) ): raise else: # Increase the block size in case it was too small. # The block size will be reset for the next file. logger.debug( F"""Batch of {len(__UpperCamelCase )} bytes couldn't be parsed with block_size={block_size}. Retrying with block_size={block_size * 2}.""" ) block_size *= 2 except pa.ArrowInvalid as e: try: with open( __UpperCamelCase , encoding=self.config.encoding , errors=self.config.encoding_errors ) as f: snake_case__ : Tuple = json.load(__UpperCamelCase ) except json.JSONDecodeError: logger.error(F"""Failed to read file '{file}' with error {type(__UpperCamelCase )}: {e}""" ) raise e # If possible, parse the file as a list of json objects and exit the loop if isinstance(__UpperCamelCase , __UpperCamelCase ): # list is the only sequence type supported in JSON try: snake_case__ : str = set().union(*[row.keys() for row in dataset] ) snake_case__ : Union[str, Any] = {col: [row.get(__UpperCamelCase ) for row in dataset] for col in keys} snake_case__ : List[str] = pa.Table.from_pydict(__UpperCamelCase ) except (pa.ArrowInvalid, AttributeError) as e: logger.error(F"""Failed to read file '{file}' with error {type(__UpperCamelCase )}: {e}""" ) raise ValueError(F"""Not able to read records in the JSON file at {file}.""" ) from None yield file_idx, self._cast_table(__UpperCamelCase ) break else: logger.error(F"""Failed to read file '{file}' with error {type(__UpperCamelCase )}: {e}""" ) raise ValueError( F"""Not able to read records in the JSON file at {file}. """ F"""You should probably indicate the field of the JSON file containing your records. """ F"""This JSON file contain the following fields: {str(list(dataset.keys() ) )}. """ F"""Select the correct one and provide it as `field='XXX'` to the dataset loading method. """ ) from None # Uncomment for debugging (will print the Arrow table size and elements) # logger.warning(f"pa_table: {pa_table} num rows: {pa_table.num_rows}") # logger.warning('\n'.join(str(pa_table.slice(i, 1).to_pydict()) for i in range(pa_table.num_rows))) yield (file_idx, batch_idx), self._cast_table(__UpperCamelCase ) batch_idx += 1
699
0
from sklearn.metrics import matthews_corrcoef import datasets lowerCAmelCase__ : Optional[Any] = ''' Compute the Matthews correlation coefficient (MCC) The Matthews correlation coefficient is used in machine learning as a measure of the quality of binary and multiclass classifications. It takes into account true and false positives and negatives and is generally regarded as a balanced measure which can be used even if the classes are of very different sizes. The MCC is in essence a correlation coefficient value between -1 and +1. A coefficient of +1 represents a perfect prediction, 0 an average random prediction and -1 an inverse prediction. The statistic is also known as the phi coefficient. [source: Wikipedia] ''' lowerCAmelCase__ : Tuple = ''' Args: predictions (list of int): Predicted labels, as returned by a model. references (list of int): Ground truth labels. sample_weight (list of int, float, or bool): Sample weights. Defaults to `None`. Returns: matthews_correlation (dict containing float): Matthews correlation. Examples: Example 1, a basic example with only predictions and references as inputs: >>> matthews_metric = datasets.load_metric("matthews_correlation") >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2], ... predictions=[1, 2, 2, 0, 3, 3]) >>> print(round(results[\'matthews_correlation\'], 2)) 0.54 Example 2, the same example as above, but also including sample weights: >>> matthews_metric = datasets.load_metric("matthews_correlation") >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2], ... predictions=[1, 2, 2, 0, 3, 3], ... sample_weight=[0.5, 3, 1, 1, 1, 2]) >>> print(round(results[\'matthews_correlation\'], 2)) 0.1 Example 3, the same example as above, but with sample weights that cause a negative correlation: >>> matthews_metric = datasets.load_metric("matthews_correlation") >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2], ... predictions=[1, 2, 2, 0, 3, 3], ... sample_weight=[0.5, 1, 0, 0, 0, 1]) >>> print(round(results[\'matthews_correlation\'], 2)) -0.25 ''' lowerCAmelCase__ : Dict = '''\ @article{scikit-learn, title={Scikit-learn: Machine Learning in {P}ython}, author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V. and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P. and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.}, journal={Journal of Machine Learning Research}, volume={12}, pages={2825--2830}, year={2011} } ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION ,_KWARGS_DESCRIPTION ) class __snake_case ( datasets.Metric ): def __a ( self ) -> List[Any]: '''simple docstring''' return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { 'predictions': datasets.Value('int32' ), 'references': datasets.Value('int32' ), } ) , reference_urls=[ 'https://scikit-learn.org/stable/modules/generated/sklearn.metrics.matthews_corrcoef.html' ] , ) def __a ( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase=None ) -> Dict: '''simple docstring''' return { "matthews_correlation": float(matthews_corrcoef(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , sample_weight=SCREAMING_SNAKE_CASE_ ) ), }
720
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__ : Any = {'''configuration_xglm''': ['''XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''XGLMConfig''']} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : str = ['''XGLMTokenizer'''] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : Dict = ['''XGLMTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : Optional[int] = [ '''XGLM_PRETRAINED_MODEL_ARCHIVE_LIST''', '''XGLMForCausalLM''', '''XGLMModel''', '''XGLMPreTrainedModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : Dict = [ '''FlaxXGLMForCausalLM''', '''FlaxXGLMModel''', '''FlaxXGLMPreTrainedModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : Dict = [ '''TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFXGLMForCausalLM''', '''TFXGLMModel''', '''TFXGLMPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_xglm import XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XGLMConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xglm import XGLMTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xglm_fast import XGLMTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xglm import XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, XGLMForCausalLM, XGLMModel, XGLMPreTrainedModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_xglm import FlaxXGLMForCausalLM, FlaxXGLMModel, FlaxXGLMPreTrainedModel try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_xglm import ( TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFXGLMForCausalLM, TFXGLMModel, TFXGLMPreTrainedModel, ) else: import sys lowerCAmelCase__ : Tuple = _LazyModule(__name__, globals()['''__file__'''], _import_structure)
699
0
import json import os from functools import lru_cache from typing import List, Optional, Tuple import regex as re from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging lowerCAmelCase__ : Tuple = logging.get_logger(__name__) lowerCAmelCase__ : Union[str, Any] = {"""vocab_file""": """vocab.json""", """merges_file""": """merges.txt"""} lowerCAmelCase__ : List[str] = { """vocab_file""": { """allenai/longformer-base-4096""": """https://huggingface.co/allenai/longformer-base-4096/resolve/main/vocab.json""", """allenai/longformer-large-4096""": ( """https://huggingface.co/allenai/longformer-large-4096/resolve/main/vocab.json""" ), """allenai/longformer-large-4096-finetuned-triviaqa""": ( """https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/vocab.json""" ), """allenai/longformer-base-4096-extra.pos.embd.only""": ( """https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/vocab.json""" ), """allenai/longformer-large-4096-extra.pos.embd.only""": ( """https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/vocab.json""" ), }, """merges_file""": { """allenai/longformer-base-4096""": """https://huggingface.co/allenai/longformer-base-4096/resolve/main/merges.txt""", """allenai/longformer-large-4096""": ( """https://huggingface.co/allenai/longformer-large-4096/resolve/main/merges.txt""" ), """allenai/longformer-large-4096-finetuned-triviaqa""": ( """https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/merges.txt""" ), """allenai/longformer-base-4096-extra.pos.embd.only""": ( """https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/merges.txt""" ), """allenai/longformer-large-4096-extra.pos.embd.only""": ( """https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/merges.txt""" ), }, } lowerCAmelCase__ : Optional[Any] = { """allenai/longformer-base-4096""": 40_96, """allenai/longformer-large-4096""": 40_96, """allenai/longformer-large-4096-finetuned-triviaqa""": 40_96, """allenai/longformer-base-4096-extra.pos.embd.only""": 40_96, """allenai/longformer-large-4096-extra.pos.embd.only""": 40_96, } @lru_cache() # Copied from transformers.models.roberta.tokenization_roberta.bytes_to_unicode def UpperCamelCase__ ( ) -> Tuple: snake_case__ : int = ( list(range(ord('!' ) , ord('~' ) + 1 ) ) + list(range(ord('¡' ) , ord('¬' ) + 1 ) ) + list(range(ord('®' ) , ord('ÿ' ) + 1 ) ) ) snake_case__ : Tuple = bs[:] snake_case__ : Optional[int] = 0 for b in range(2**8 ): if b not in bs: bs.append(A__ ) cs.append(2**8 + n ) n += 1 snake_case__ : Dict = [chr(A__ ) for n in cs] return dict(zip(A__ , A__ ) ) def UpperCamelCase__ ( A__ ) -> Optional[Any]: snake_case__ : Union[str, Any] = set() snake_case__ : Tuple = word[0] for char in word[1:]: pairs.add((prev_char, char) ) snake_case__ : Optional[Any] = char return pairs class __snake_case ( _snake_case ): __lowerCamelCase = VOCAB_FILES_NAMES __lowerCamelCase = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase = ["""input_ids""", """attention_mask"""] def __init__( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase="replace" , __UpperCamelCase="<s>" , __UpperCamelCase="</s>" , __UpperCamelCase="</s>" , __UpperCamelCase="<s>" , __UpperCamelCase="<unk>" , __UpperCamelCase="<pad>" , __UpperCamelCase="<mask>" , __UpperCamelCase=False , **__UpperCamelCase , ) -> Any: '''simple docstring''' snake_case__ : str = AddedToken(snake_case_ , lstrip=snake_case_ , rstrip=snake_case_ ) if isinstance(snake_case_ , snake_case_ ) else bos_token snake_case__ : Dict = AddedToken(snake_case_ , lstrip=snake_case_ , rstrip=snake_case_ ) if isinstance(snake_case_ , snake_case_ ) else eos_token snake_case__ : Dict = AddedToken(snake_case_ , lstrip=snake_case_ , rstrip=snake_case_ ) if isinstance(snake_case_ , snake_case_ ) else sep_token snake_case__ : str = AddedToken(snake_case_ , lstrip=snake_case_ , rstrip=snake_case_ ) if isinstance(snake_case_ , snake_case_ ) else cls_token snake_case__ : Union[str, Any] = AddedToken(snake_case_ , lstrip=snake_case_ , rstrip=snake_case_ ) if isinstance(snake_case_ , snake_case_ ) else unk_token snake_case__ : List[str] = AddedToken(snake_case_ , lstrip=snake_case_ , rstrip=snake_case_ ) if isinstance(snake_case_ , snake_case_ ) else pad_token # Mask token behave like a normal word, i.e. include the space before it snake_case__ : List[Any] = AddedToken(snake_case_ , lstrip=snake_case_ , rstrip=snake_case_ ) if isinstance(snake_case_ , snake_case_ ) else mask_token super().__init__( errors=snake_case_ , bos_token=snake_case_ , eos_token=snake_case_ , unk_token=snake_case_ , sep_token=snake_case_ , cls_token=snake_case_ , pad_token=snake_case_ , mask_token=snake_case_ , add_prefix_space=snake_case_ , **snake_case_ , ) with open(snake_case_ , encoding='utf-8' ) as vocab_handle: snake_case__ : Optional[int] = json.load(snake_case_ ) snake_case__ : str = {v: k for k, v in self.encoder.items()} snake_case__ : Union[str, Any] = errors # how to handle errors in decoding snake_case__ : str = bytes_to_unicode() snake_case__ : Optional[Any] = {v: k for k, v in self.byte_encoder.items()} with open(snake_case_ , encoding='utf-8' ) as merges_handle: snake_case__ : Optional[Any] = merges_handle.read().split('\n' )[1:-1] snake_case__ : Any = [tuple(merge.split() ) for merge in bpe_merges] snake_case__ : Tuple = dict(zip(snake_case_ , range(len(snake_case_ ) ) ) ) snake_case__ : Union[str, Any] = {} snake_case__ : Optional[int] = add_prefix_space # Should have added re.IGNORECASE so BPE merges can happen for capitalized versions of contractions snake_case__ : int = re.compile(r'\'s|\'t|\'re|\'ve|\'m|\'ll|\'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+' ) @property def __a ( self ) -> Optional[int]: '''simple docstring''' return len(self.encoder ) def __a ( self ) -> Optional[Any]: '''simple docstring''' return dict(self.encoder , **self.added_tokens_encoder ) def __a ( self , __UpperCamelCase ) -> List[str]: '''simple docstring''' if token in self.cache: return self.cache[token] snake_case__ : Tuple = tuple(snake_case_ ) snake_case__ : str = get_pairs(snake_case_ ) if not pairs: return token while True: snake_case__ : Dict = min(snake_case_ , key=lambda __UpperCamelCase : self.bpe_ranks.get(snake_case_ , float('inf' ) ) ) if bigram not in self.bpe_ranks: break snake_case__ : Optional[Any] = bigram snake_case__ : Tuple = [] snake_case__ : Tuple = 0 while i < len(snake_case_ ): try: snake_case__ : int = word.index(snake_case_ , snake_case_ ) except ValueError: new_word.extend(word[i:] ) break else: new_word.extend(word[i:j] ) snake_case__ : List[str] = j if word[i] == first and i < len(snake_case_ ) - 1 and word[i + 1] == second: new_word.append(first + second ) i += 2 else: new_word.append(word[i] ) i += 1 snake_case__ : Union[str, Any] = tuple(snake_case_ ) snake_case__ : Optional[Any] = new_word if len(snake_case_ ) == 1: break else: snake_case__ : int = get_pairs(snake_case_ ) snake_case__ : int = " ".join(snake_case_ ) snake_case__ : Optional[int] = word return word def __a ( self , __UpperCamelCase ) -> int: '''simple docstring''' snake_case__ : Optional[Any] = [] for token in re.findall(self.pat , snake_case_ ): snake_case__ : Optional[int] = "".join( self.byte_encoder[b] for b in token.encode('utf-8' ) ) # Maps all our bytes to unicode strings, avoiding control tokens of the BPE (spaces in our case) bpe_tokens.extend(bpe_token for bpe_token in self.bpe(snake_case_ ).split(' ' ) ) return bpe_tokens def __a ( self , __UpperCamelCase ) -> Any: '''simple docstring''' return self.encoder.get(snake_case_ , self.encoder.get(self.unk_token ) ) def __a ( self , __UpperCamelCase ) -> Any: '''simple docstring''' return self.decoder.get(snake_case_ ) def __a ( self , __UpperCamelCase ) -> int: '''simple docstring''' snake_case__ : Optional[Any] = "".join(snake_case_ ) snake_case__ : Tuple = bytearray([self.byte_decoder[c] for c in text] ).decode('utf-8' , errors=self.errors ) return text def __a ( self , __UpperCamelCase , __UpperCamelCase = None ) -> Optional[Any]: '''simple docstring''' if not os.path.isdir(snake_case_ ): logger.error(F"""Vocabulary path ({save_directory}) should be a directory""" ) return snake_case__ : List[Any] = os.path.join( snake_case_ , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) snake_case__ : Any = os.path.join( snake_case_ , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['merges_file'] ) with open(snake_case_ , 'w' , encoding='utf-8' ) as f: f.write(json.dumps(self.encoder , indent=2 , sort_keys=snake_case_ , ensure_ascii=snake_case_ ) + '\n' ) snake_case__ : Any = 0 with open(snake_case_ , 'w' , encoding='utf-8' ) as writer: writer.write('#version: 0.2\n' ) for bpe_tokens, token_index in sorted(self.bpe_ranks.items() , key=lambda __UpperCamelCase : kv[1] ): if index != token_index: logger.warning( F"""Saving vocabulary to {merge_file}: BPE merge indices are not consecutive.""" ' Please check that the tokenizer is not corrupted!' ) snake_case__ : Dict = token_index writer.write(' '.join(snake_case_ ) + '\n' ) index += 1 return vocab_file, merge_file def __a ( self , __UpperCamelCase , __UpperCamelCase = None ) -> Dict: '''simple docstring''' if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] snake_case__ : List[str] = [self.cls_token_id] snake_case__ : Tuple = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def __a ( self , __UpperCamelCase , __UpperCamelCase = None , __UpperCamelCase = False ) -> int: '''simple docstring''' if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=snake_case_ , token_ids_a=snake_case_ , already_has_special_tokens=snake_case_ ) if token_ids_a is None: return [1] + ([0] * len(snake_case_ )) + [1] return [1] + ([0] * len(snake_case_ )) + [1, 1] + ([0] * len(snake_case_ )) + [1] def __a ( self , __UpperCamelCase , __UpperCamelCase = None ) -> int: '''simple docstring''' snake_case__ : Optional[int] = [self.sep_token_id] snake_case__ : Tuple = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def __a ( self , __UpperCamelCase , __UpperCamelCase=False , **__UpperCamelCase ) -> str: '''simple docstring''' snake_case__ : List[str] = kwargs.pop('add_prefix_space' , self.add_prefix_space ) if (is_split_into_words or add_prefix_space) and (len(snake_case_ ) > 0 and not text[0].isspace()): snake_case__ : Union[str, Any] = " " + text return (text, kwargs)
721
from __future__ import annotations import random # Maximum size of the population. Bigger could be faster but is more memory expensive. lowerCAmelCase__ : Dict = 2_00 # Number of elements selected in every generation of evolution. The selection takes # place from best to worst of that generation and must be smaller than N_POPULATION. lowerCAmelCase__ : List[str] = 50 # Probability that an element of a generation can mutate, changing one of its genes. # This will guarantee that all genes will be used during evolution. lowerCAmelCase__ : List[str] = 0.4 # Just a seed to improve randomness required by the algorithm. random.seed(random.randint(0, 10_00)) def UpperCamelCase__ ( A__ , A__ ) -> tuple[str, float]: snake_case__ : Tuple = len([g for position, g in enumerate(A__ ) if g == main_target[position]] ) return (item, float(A__ )) def UpperCamelCase__ ( A__ , A__ ) -> tuple[str, str]: snake_case__ : str = random.randint(0 , len(A__ ) - 1 ) snake_case__ : int = parent_a[:random_slice] + parent_a[random_slice:] snake_case__ : Any = parent_a[:random_slice] + parent_a[random_slice:] return (child_a, child_a) def UpperCamelCase__ ( A__ , A__ ) -> str: snake_case__ : List[Any] = list(A__ ) if random.uniform(0 , 1 ) < MUTATION_PROBABILITY: snake_case__ : Optional[Any] = random.choice(A__ ) return "".join(A__ ) def UpperCamelCase__ ( A__ , A__ , A__ , ) -> list[str]: snake_case__ : Tuple = [] # Generate more children proportionally to the fitness score. snake_case__ : Optional[Any] = int(parent_a[1] * 100 ) + 1 snake_case__ : str = 10 if child_n >= 10 else child_n for _ in range(A__ ): snake_case__ : Any = population_score[random.randint(0 , A__ )][0] snake_case__ , snake_case__ : int = crossover(parent_a[0] , A__ ) # Append new string to the population list. pop.append(mutate(A__ , A__ ) ) pop.append(mutate(A__ , A__ ) ) return pop def UpperCamelCase__ ( A__ , A__ , A__ = True ) -> tuple[int, int, str]: # Verify if N_POPULATION is bigger than N_SELECTED if N_POPULATION < N_SELECTED: snake_case__ : Union[str, Any] = F"""{N_POPULATION} must be bigger than {N_SELECTED}""" raise ValueError(A__ ) # Verify that the target contains no genes besides the ones inside genes variable. snake_case__ : Tuple = sorted({c for c in target if c not in genes} ) if not_in_genes_list: snake_case__ : int = F"""{not_in_genes_list} is not in genes list, evolution cannot converge""" raise ValueError(A__ ) # Generate random starting population. snake_case__ : Union[str, Any] = [] for _ in range(A__ ): population.append(''.join([random.choice(A__ ) for i in range(len(A__ ) )] ) ) # Just some logs to know what the algorithms is doing. snake_case__ , snake_case__ : str = 0, 0 # This loop will end when we find a perfect match for our target. while True: generation += 1 total_population += len(A__ ) # Random population created. Now it's time to evaluate. # Adding a bit of concurrency can make everything faster, # # import concurrent.futures # population_score: list[tuple[str, float]] = [] # with concurrent.futures.ThreadPoolExecutor( # max_workers=NUM_WORKERS) as executor: # futures = {executor.submit(evaluate, item) for item in population} # concurrent.futures.wait(futures) # population_score = [item.result() for item in futures] # # but with a simple algorithm like this, it will probably be slower. # We just need to call evaluate for every item inside the population. snake_case__ : List[Any] = [evaluate(A__ , A__ ) for item in population] # Check if there is a matching evolution. snake_case__ : int = sorted(A__ , key=lambda A__ : x[1] , reverse=A__ ) if population_score[0][0] == target: return (generation, total_population, population_score[0][0]) # Print the best result every 10 generation. # Just to know that the algorithm is working. if debug and generation % 10 == 0: print( F"""\nGeneration: {generation}""" F"""\nTotal Population:{total_population}""" F"""\nBest score: {population_score[0][1]}""" F"""\nBest string: {population_score[0][0]}""" ) # Flush the old population, keeping some of the best evolutions. # Keeping this avoid regression of evolution. snake_case__ : Optional[int] = population[: int(N_POPULATION / 3 )] population.clear() population.extend(A__ ) # Normalize population score to be between 0 and 1. snake_case__ : str = [ (item, score / len(A__ )) for item, score in population_score ] # This is selection for i in range(A__ ): population.extend(select(population_score[int(A__ )] , A__ , A__ ) ) # Check if the population has already reached the maximum value and if so, # break the cycle. If this check is disabled, the algorithm will take # forever to compute large strings, but will also calculate small strings in # a far fewer generations. if len(A__ ) > N_POPULATION: break if __name__ == "__main__": lowerCAmelCase__ : str = ( '''This is a genetic algorithm to evaluate, combine, evolve, and mutate a string!''' ) lowerCAmelCase__ : Optional[Any] = list( ''' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklm''' '''nopqrstuvwxyz.,;!?+-*#@^\'èéòà€ù=)(&%$£/\\''' ) lowerCAmelCase__, lowerCAmelCase__, lowerCAmelCase__ : List[str] = basic(target_str, genes_list) print( F'''\nGeneration: {generation}\nTotal Population: {population}\nTarget: {target}''' )
699
0
import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_xlnet import XLNetTokenizer else: lowerCAmelCase__ : Union[str, Any] = None lowerCAmelCase__ : Optional[Any] = logging.get_logger(__name__) lowerCAmelCase__ : List[Any] = {'''vocab_file''': '''spiece.model''', '''tokenizer_file''': '''tokenizer.json'''} lowerCAmelCase__ : Dict = { '''vocab_file''': { '''xlnet-base-cased''': '''https://huggingface.co/xlnet-base-cased/resolve/main/spiece.model''', '''xlnet-large-cased''': '''https://huggingface.co/xlnet-large-cased/resolve/main/spiece.model''', }, '''tokenizer_file''': { '''xlnet-base-cased''': '''https://huggingface.co/xlnet-base-cased/resolve/main/tokenizer.json''', '''xlnet-large-cased''': '''https://huggingface.co/xlnet-large-cased/resolve/main/tokenizer.json''', }, } lowerCAmelCase__ : Union[str, Any] = { '''xlnet-base-cased''': None, '''xlnet-large-cased''': None, } lowerCAmelCase__ : Tuple = '''▁''' # Segments (not really needed) lowerCAmelCase__ : Any = 0 lowerCAmelCase__ : Tuple = 1 lowerCAmelCase__ : List[Any] = 2 lowerCAmelCase__ : int = 3 lowerCAmelCase__ : Any = 4 class __snake_case ( lowercase__ ): __lowerCamelCase = VOCAB_FILES_NAMES __lowerCamelCase = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase = """left""" __lowerCamelCase = XLNetTokenizer def __init__( self , __UpperCamelCase=None , __UpperCamelCase=None , __UpperCamelCase=False , __UpperCamelCase=True , __UpperCamelCase=False , __UpperCamelCase="<s>" , __UpperCamelCase="</s>" , __UpperCamelCase="<unk>" , __UpperCamelCase="<sep>" , __UpperCamelCase="<pad>" , __UpperCamelCase="<cls>" , __UpperCamelCase="<mask>" , __UpperCamelCase=["<eop>", "<eod>"] , **__UpperCamelCase , ) -> Dict: '''simple docstring''' snake_case__ : List[Any] = AddedToken(UpperCAmelCase__ , lstrip=UpperCAmelCase__ , rstrip=UpperCAmelCase__ ) if isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) else mask_token super().__init__( vocab_file=UpperCAmelCase__ , tokenizer_file=UpperCAmelCase__ , do_lower_case=UpperCAmelCase__ , remove_space=UpperCAmelCase__ , keep_accents=UpperCAmelCase__ , bos_token=UpperCAmelCase__ , eos_token=UpperCAmelCase__ , unk_token=UpperCAmelCase__ , sep_token=UpperCAmelCase__ , pad_token=UpperCAmelCase__ , cls_token=UpperCAmelCase__ , mask_token=UpperCAmelCase__ , additional_special_tokens=UpperCAmelCase__ , **UpperCAmelCase__ , ) snake_case__ : List[Any] = 3 snake_case__ : Tuple = do_lower_case snake_case__ : Tuple = remove_space snake_case__ : Optional[Any] = keep_accents snake_case__ : Any = vocab_file snake_case__ : Optional[Any] = False if not self.vocab_file else True def __a ( self , __UpperCamelCase , __UpperCamelCase = None ) -> Optional[int]: '''simple docstring''' snake_case__ : Optional[Any] = [self.sep_token_id] snake_case__ : Union[str, Any] = [self.cls_token_id] if token_ids_a is None: return token_ids_a + sep + cls return token_ids_a + sep + token_ids_a + sep + cls def __a ( self , __UpperCamelCase , __UpperCamelCase = None ) -> Tuple: '''simple docstring''' snake_case__ : str = [self.sep_token_id] snake_case__ : Any = [2] if token_ids_a is None: return len(token_ids_a + sep ) * [0] + cls_segment_id return len(token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] + cls_segment_id def __a ( self , __UpperCamelCase , __UpperCamelCase = None ) -> Tuple: '''simple docstring''' if not self.can_save_slow_tokenizer: raise ValueError( 'Your fast tokenizer does not have the necessary information to save the vocabulary for a slow ' 'tokenizer.' ) if not os.path.isdir(UpperCAmelCase__ ): logger.error(F"""Vocabulary path ({save_directory}) should be a directory""" ) return snake_case__ : List[Any] = 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__ ): copyfile(self.vocab_file , UpperCAmelCase__ ) return (out_vocab_file,)
700
from __future__ import annotations from collections.abc import Iterator from typing import Generic, TypeVar lowerCAmelCase__ : Optional[int] = TypeVar('''T''') class __snake_case ( Generic[T] ): def __init__( self , __UpperCamelCase ) -> Any: '''simple docstring''' snake_case__ : Optional[int] = data snake_case__ : Node[T] | None = None def __str__( self ) -> str: '''simple docstring''' return F"""{self.data}""" class __snake_case ( Generic[T] ): def __init__( self ) -> None: '''simple docstring''' snake_case__ : Node[T] | None = None def __iter__( self ) -> Iterator[T]: '''simple docstring''' snake_case__ : str = self.top while node: yield node.data snake_case__ : Dict = node.next def __str__( self ) -> str: '''simple docstring''' return "->".join([str(__UpperCamelCase ) for item in self] ) def __len__( self ) -> int: '''simple docstring''' return len(tuple(iter(self ) ) ) def __a ( self ) -> bool: '''simple docstring''' return self.top is None def __a ( self , __UpperCamelCase ) -> None: '''simple docstring''' snake_case__ : str = Node(__UpperCamelCase ) if not self.is_empty(): snake_case__ : List[str] = self.top snake_case__ : Tuple = node def __a ( self ) -> T: '''simple docstring''' if self.is_empty(): raise IndexError('pop from empty stack' ) assert isinstance(self.top , __UpperCamelCase ) snake_case__ : List[str] = self.top snake_case__ : Union[str, Any] = self.top.next return pop_node.data def __a ( self ) -> T: '''simple docstring''' if self.is_empty(): raise IndexError('peek from empty stack' ) assert self.top is not None return self.top.data def __a ( self ) -> None: '''simple docstring''' snake_case__ : Any = None if __name__ == "__main__": from doctest import testmod testmod()
699
0
import argparse import json import requests import timm import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import AutoImageProcessor, SwinConfig, SwinForImageClassification def UpperCamelCase__ ( A__ ) -> Optional[int]: snake_case__ : List[Any] = SwinConfig() snake_case__ : Dict = swin_name.split('_' ) snake_case__ : List[str] = name_split[1] snake_case__ : int = int(name_split[4] ) snake_case__ : Optional[int] = int(name_split[3][-1] ) if model_size == "tiny": snake_case__ : List[str] = 96 snake_case__ : List[str] = (2, 2, 6, 2) snake_case__ : str = (3, 6, 12, 24) elif model_size == "small": snake_case__ : Optional[int] = 96 snake_case__ : List[Any] = (2, 2, 18, 2) snake_case__ : Tuple = (3, 6, 12, 24) elif model_size == "base": snake_case__ : Union[str, Any] = 128 snake_case__ : str = (2, 2, 18, 2) snake_case__ : Optional[Any] = (4, 8, 16, 32) else: snake_case__ : int = 192 snake_case__ : int = (2, 2, 18, 2) snake_case__ : str = (6, 12, 24, 48) if "in22k" in swin_name: snake_case__ : Optional[Any] = 2_1841 else: snake_case__ : Optional[Any] = 1000 snake_case__ : Dict = 'huggingface/label-files' snake_case__ : List[Any] = 'imagenet-1k-id2label.json' snake_case__ : List[str] = json.load(open(hf_hub_download(_lowerCAmelCase , _lowerCAmelCase , repo_type='dataset' ) , 'r' ) ) snake_case__ : Union[str, Any] = {int(_lowerCAmelCase ): v for k, v in idalabel.items()} snake_case__ : Optional[Any] = idalabel snake_case__ : List[Any] = {v: k for k, v in idalabel.items()} snake_case__ : int = img_size snake_case__ : List[Any] = num_classes snake_case__ : List[Any] = embed_dim snake_case__ : str = depths snake_case__ : Union[str, Any] = num_heads snake_case__ : Dict = window_size return config def UpperCamelCase__ ( A__ ) -> Dict: if "patch_embed.proj" in name: snake_case__ : Tuple = name.replace('patch_embed.proj' , 'embeddings.patch_embeddings.projection' ) if "patch_embed.norm" in name: snake_case__ : List[Any] = name.replace('patch_embed.norm' , 'embeddings.norm' ) if "layers" in name: snake_case__ : List[str] = 'encoder.' + name if "attn.proj" in name: snake_case__ : Union[str, Any] = name.replace('attn.proj' , 'attention.output.dense' ) if "attn" in name: snake_case__ : List[Any] = name.replace('attn' , 'attention.self' ) if "norm1" in name: snake_case__ : List[str] = name.replace('norm1' , 'layernorm_before' ) if "norm2" in name: snake_case__ : List[Any] = name.replace('norm2' , 'layernorm_after' ) if "mlp.fc1" in name: snake_case__ : Tuple = name.replace('mlp.fc1' , 'intermediate.dense' ) if "mlp.fc2" in name: snake_case__ : str = name.replace('mlp.fc2' , 'output.dense' ) if name == "norm.weight": snake_case__ : List[Any] = 'layernorm.weight' if name == "norm.bias": snake_case__ : Union[str, Any] = 'layernorm.bias' if "head" in name: snake_case__ : Optional[int] = name.replace('head' , 'classifier' ) else: snake_case__ : str = 'swin.' + name return name def UpperCamelCase__ ( A__ , A__ ) -> Any: for key in orig_state_dict.copy().keys(): snake_case__ : Dict = orig_state_dict.pop(_lowerCAmelCase ) if "mask" in key: continue elif "qkv" in key: snake_case__ : List[str] = key.split('.' ) snake_case__ : Tuple = int(key_split[1] ) snake_case__ : str = int(key_split[3] ) snake_case__ : Tuple = model.swin.encoder.layers[layer_num].blocks[block_num].attention.self.all_head_size if "weight" in key: snake_case__ : Optional[int] = val[:dim, :] snake_case__ : List[str] = val[ dim : dim * 2, : ] snake_case__ : int = val[-dim:, :] else: snake_case__ : List[str] = val[ :dim ] snake_case__ : Dict = val[ dim : dim * 2 ] snake_case__ : List[str] = val[ -dim: ] else: snake_case__ : Dict = val return orig_state_dict def UpperCamelCase__ ( A__ , A__ ) -> Optional[Any]: snake_case__ : Optional[int] = timm.create_model(_lowerCAmelCase , pretrained=_lowerCAmelCase ) timm_model.eval() snake_case__ : Union[str, Any] = get_swin_config(_lowerCAmelCase ) snake_case__ : List[str] = SwinForImageClassification(_lowerCAmelCase ) model.eval() snake_case__ : str = convert_state_dict(timm_model.state_dict() , _lowerCAmelCase ) model.load_state_dict(_lowerCAmelCase ) snake_case__ : Optional[int] = 'http://images.cocodataset.org/val2017/000000039769.jpg' snake_case__ : Optional[int] = AutoImageProcessor.from_pretrained('microsoft/{}'.format(swin_name.replace('_' , '-' ) ) ) snake_case__ : Any = Image.open(requests.get(_lowerCAmelCase , stream=_lowerCAmelCase ).raw ) snake_case__ : Dict = image_processor(images=_lowerCAmelCase , return_tensors='pt' ) snake_case__ : Any = timm_model(inputs['pixel_values'] ) snake_case__ : Any = model(**_lowerCAmelCase ).logits assert torch.allclose(_lowerCAmelCase , _lowerCAmelCase , atol=1e-3 ) print(F"""Saving model {swin_name} to {pytorch_dump_folder_path}""" ) model.save_pretrained(_lowerCAmelCase ) print(F"""Saving image processor to {pytorch_dump_folder_path}""" ) image_processor.save_pretrained(_lowerCAmelCase ) if __name__ == "__main__": lowerCAmelCase__ : int = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--swin_name''', default='''swin_tiny_patch4_window7_224''', type=str, help='''Name of the Swin timm model you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) lowerCAmelCase__ : List[Any] = parser.parse_args() convert_swin_checkpoint(args.swin_name, args.pytorch_dump_folder_path)
701
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__ : Dict = logging.get_logger(__name__) lowerCAmelCase__ : int = { '''sail/poolformer_s12''': '''https://huggingface.co/sail/poolformer_s12/resolve/main/config.json''', # See all PoolFormer models at https://huggingface.co/models?filter=poolformer } class __snake_case ( _lowerCamelCase ): __lowerCamelCase = """poolformer""" def __init__( self , __UpperCamelCase=3 , __UpperCamelCase=16 , __UpperCamelCase=16 , __UpperCamelCase=3 , __UpperCamelCase=4.0 , __UpperCamelCase=[2, 2, 6, 2] , __UpperCamelCase=[64, 128, 320, 512] , __UpperCamelCase=[7, 3, 3, 3] , __UpperCamelCase=[4, 2, 2, 2] , __UpperCamelCase=[2, 1, 1, 1] , __UpperCamelCase=4 , __UpperCamelCase=0.0 , __UpperCamelCase="gelu" , __UpperCamelCase=True , __UpperCamelCase=1E-5 , __UpperCamelCase=0.0_2 , **__UpperCamelCase , ) -> Any: '''simple docstring''' snake_case__ : List[str] = num_channels snake_case__ : Dict = patch_size snake_case__ : Optional[int] = stride snake_case__ : str = padding snake_case__ : List[str] = pool_size snake_case__ : List[Any] = hidden_sizes snake_case__ : List[Any] = mlp_ratio snake_case__ : Union[str, Any] = depths snake_case__ : Dict = patch_sizes snake_case__ : Dict = strides snake_case__ : Dict = num_encoder_blocks snake_case__ : Union[str, Any] = drop_path_rate snake_case__ : List[str] = hidden_act snake_case__ : Optional[Any] = use_layer_scale snake_case__ : int = layer_scale_init_value snake_case__ : Dict = initializer_range super().__init__(**__UpperCamelCase ) class __snake_case ( _lowerCamelCase ): __lowerCamelCase = version.parse("""1.11""" ) @property def __a ( self ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' return OrderedDict( [ ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}), ] ) @property def __a ( self ) -> float: '''simple docstring''' return 2E-3
699
0
import warnings from diffusers import StableDiffusionInpaintPipeline as StableDiffusionInpaintPipeline # noqa F401 warnings.warn( '''The `inpainting.py` script is outdated. Please use directly `from diffusers import''' ''' StableDiffusionInpaintPipeline` instead.''' )
702
import numpy as np import qiskit def UpperCamelCase__ ( A__ = 8 , A__ = None ) -> str: snake_case__ : Optional[int] = np.random.default_rng(seed=A__ ) # Roughly 25% of the qubits will contribute to the key. # So we take more than we need. snake_case__ : Tuple = 6 * key_len # Measurement basis for Alice's qubits. snake_case__ : Tuple = rng.integers(2 , size=A__ ) # The set of states Alice will prepare. snake_case__ : List[str] = rng.integers(2 , size=A__ ) # Measurement basis for Bob's qubits. snake_case__ : List[Any] = rng.integers(2 , size=A__ ) # Quantum Circuit to simulate BB84 snake_case__ : Any = qiskit.QuantumCircuit(A__ , name='BB84' ) # Alice prepares her qubits according to rules above. for index, _ in enumerate(A__ ): if alice_state[index] == 1: bbaa_circ.x(A__ ) if alice_basis[index] == 1: bbaa_circ.h(A__ ) bbaa_circ.barrier() # Bob measures the received qubits according to rules above. for index, _ in enumerate(A__ ): if bob_basis[index] == 1: bbaa_circ.h(A__ ) bbaa_circ.barrier() bbaa_circ.measure_all() # Simulate the quantum circuit. snake_case__ : List[str] = qiskit.Aer.get_backend('aer_simulator' ) # We only need to run one shot because the key is unique. # Multiple shots will produce the same key. snake_case__ : Optional[Any] = qiskit.execute(A__ , A__ , shots=1 , seed_simulator=A__ ) # Returns the result of measurement. snake_case__ : Union[str, Any] = job.result().get_counts(A__ ).most_frequent() # Extracting the generated key from the simulation results. # Only keep measurement results where Alice and Bob chose the same basis. snake_case__ : Optional[Any] = ''.join( [ result_bit for alice_basis_bit, bob_basis_bit, result_bit in zip( A__ , A__ , A__ ) if alice_basis_bit == bob_basis_bit ] ) # Get final key. Pad with 0 if too short, otherwise truncate. snake_case__ : Tuple = gen_key[:key_len] if len(A__ ) >= key_len else gen_key.ljust(A__ , '0' ) return key if __name__ == "__main__": print(F'''The generated key is : {bbaa(8, seed=0)}''') from doctest import testmod testmod()
699
0
'''simple docstring''' def UpperCamelCase__ ( A__ ) -> list[int]: snake_case__ : Tuple = len(UpperCamelCase__ ) for i in range(UpperCamelCase__ ): for j in range(i + 1 , UpperCamelCase__ ): if numbers[j] < numbers[i]: snake_case__ , snake_case__ : str = numbers[j], numbers[i] return numbers if __name__ == "__main__": lowerCAmelCase__ : Optional[int] = input('''Enter numbers separated by a comma:\n''').strip() lowerCAmelCase__ : Union[str, Any] = [int(item) for item in user_input.split(''',''')] print(exchange_sort(unsorted))
703
def UpperCamelCase__ ( A__ , A__ , A__ ) -> int: if exponent == 1: return base if exponent % 2 == 0: snake_case__ : Dict = _modexpt(A__ , exponent // 2 , A__ ) % modulo_value return (x * x) % modulo_value else: return (base * _modexpt(A__ , exponent - 1 , A__ )) % modulo_value def UpperCamelCase__ ( A__ = 1777 , A__ = 1855 , A__ = 8 ) -> int: snake_case__ : Tuple = base for _ in range(1 , A__ ): snake_case__ : Any = _modexpt(A__ , A__ , 10**digits ) return result if __name__ == "__main__": print(F'''{solution() = }''')
699
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCAmelCase__ : List[str] = {'''configuration_wavlm''': ['''WAVLM_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''WavLMConfig''']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : Tuple = [ '''WAVLM_PRETRAINED_MODEL_ARCHIVE_LIST''', '''WavLMForAudioFrameClassification''', '''WavLMForCTC''', '''WavLMForSequenceClassification''', '''WavLMForXVector''', '''WavLMModel''', '''WavLMPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_wavlm import WAVLM_PRETRAINED_CONFIG_ARCHIVE_MAP, WavLMConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_wavlm import ( WAVLM_PRETRAINED_MODEL_ARCHIVE_LIST, WavLMForAudioFrameClassification, WavLMForCTC, WavLMForSequenceClassification, WavLMForXVector, WavLMModel, WavLMPreTrainedModel, ) else: import sys lowerCAmelCase__ : Union[str, Any] = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
704
# tests directory-specific settings - this file is run automatically # by pytest before any tests are run import sys import warnings from os.path import abspath, dirname, join # allow having multiple repository checkouts and not needing to remember to rerun # 'pip install -e .[dev]' when switching between checkouts and running tests. lowerCAmelCase__ : Tuple = abspath(join(dirname(dirname(__file__)), '''src''')) sys.path.insert(1, git_repo_path) # silence FutureWarning warnings in tests since often we can't act on them until # they become normal warnings - i.e. the tests still need to test the current functionality warnings.simplefilter(action='''ignore''', category=FutureWarning) def UpperCamelCase__ ( A__ ) -> Optional[Any]: from diffusers.utils.testing_utils import pytest_addoption_shared pytest_addoption_shared(A__ ) def UpperCamelCase__ ( A__ ) -> Optional[Any]: from diffusers.utils.testing_utils import pytest_terminal_summary_main snake_case__ : Union[str, Any] = terminalreporter.config.getoption('--make-reports' ) if make_reports: pytest_terminal_summary_main(A__ , id=A__ )
699
0
from ..utils import DummyObject, requires_backends class __snake_case ( metaclass=_UpperCAmelCase ): __lowerCamelCase = ['''torch''', '''transformers''', '''onnx'''] def __init__( self , *__UpperCamelCase , **__UpperCamelCase ) -> List[str]: '''simple docstring''' requires_backends(self , ['torch', 'transformers', 'onnx'] ) @classmethod def __a ( cls , *__UpperCamelCase , **__UpperCamelCase ) -> int: '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) @classmethod def __a ( cls , *__UpperCamelCase , **__UpperCamelCase ) -> Tuple: '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) class __snake_case ( metaclass=_UpperCAmelCase ): __lowerCamelCase = ['''torch''', '''transformers''', '''onnx'''] def __init__( self , *__UpperCamelCase , **__UpperCamelCase ) -> str: '''simple docstring''' requires_backends(self , ['torch', 'transformers', 'onnx'] ) @classmethod def __a ( cls , *__UpperCamelCase , **__UpperCamelCase ) -> Any: '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) @classmethod def __a ( cls , *__UpperCamelCase , **__UpperCamelCase ) -> Tuple: '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) class __snake_case ( metaclass=_UpperCAmelCase ): __lowerCamelCase = ['''torch''', '''transformers''', '''onnx'''] def __init__( self , *__UpperCamelCase , **__UpperCamelCase ) -> int: '''simple docstring''' requires_backends(self , ['torch', 'transformers', 'onnx'] ) @classmethod def __a ( cls , *__UpperCamelCase , **__UpperCamelCase ) -> List[str]: '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) @classmethod def __a ( cls , *__UpperCamelCase , **__UpperCamelCase ) -> Optional[Any]: '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) class __snake_case ( metaclass=_UpperCAmelCase ): __lowerCamelCase = ['''torch''', '''transformers''', '''onnx'''] def __init__( self , *__UpperCamelCase , **__UpperCamelCase ) -> Dict: '''simple docstring''' requires_backends(self , ['torch', 'transformers', 'onnx'] ) @classmethod def __a ( cls , *__UpperCamelCase , **__UpperCamelCase ) -> Union[str, Any]: '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) @classmethod def __a ( cls , *__UpperCamelCase , **__UpperCamelCase ) -> str: '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) class __snake_case ( metaclass=_UpperCAmelCase ): __lowerCamelCase = ['''torch''', '''transformers''', '''onnx'''] def __init__( self , *__UpperCamelCase , **__UpperCamelCase ) -> int: '''simple docstring''' requires_backends(self , ['torch', 'transformers', 'onnx'] ) @classmethod def __a ( cls , *__UpperCamelCase , **__UpperCamelCase ) -> int: '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) @classmethod def __a ( cls , *__UpperCamelCase , **__UpperCamelCase ) -> int: '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) class __snake_case ( metaclass=_UpperCAmelCase ): __lowerCamelCase = ['''torch''', '''transformers''', '''onnx'''] def __init__( self , *__UpperCamelCase , **__UpperCamelCase ) -> List[str]: '''simple docstring''' requires_backends(self , ['torch', 'transformers', 'onnx'] ) @classmethod def __a ( cls , *__UpperCamelCase , **__UpperCamelCase ) -> str: '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] ) @classmethod def __a ( cls , *__UpperCamelCase , **__UpperCamelCase ) -> Optional[int]: '''simple docstring''' requires_backends(cls , ['torch', 'transformers', 'onnx'] )
705
def UpperCamelCase__ ( A__ ) -> list[int]: if length <= 0 or not isinstance(A__ , A__ ): raise ValueError('Length must be a positive integer.' ) return [n * (2 * n - 1) for n in range(A__ )] if __name__ == "__main__": print(hexagonal_numbers(length=5)) print(hexagonal_numbers(length=10))
699
0
import argparse import json from pathlib import Path import requests import timm import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import DeiTConfig, DeiTForImageClassificationWithTeacher, DeiTImageProcessor from transformers.utils import logging logging.set_verbosity_info() lowerCAmelCase__ : int = logging.get_logger(__name__) def UpperCamelCase__ ( A__ , A__=False ) -> Optional[int]: snake_case__ : Any = [] for i in range(config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append((F"""blocks.{i}.norm1.weight""", F"""deit.encoder.layer.{i}.layernorm_before.weight""") ) rename_keys.append((F"""blocks.{i}.norm1.bias""", F"""deit.encoder.layer.{i}.layernorm_before.bias""") ) rename_keys.append((F"""blocks.{i}.attn.proj.weight""", F"""deit.encoder.layer.{i}.attention.output.dense.weight""") ) rename_keys.append((F"""blocks.{i}.attn.proj.bias""", F"""deit.encoder.layer.{i}.attention.output.dense.bias""") ) rename_keys.append((F"""blocks.{i}.norm2.weight""", F"""deit.encoder.layer.{i}.layernorm_after.weight""") ) rename_keys.append((F"""blocks.{i}.norm2.bias""", F"""deit.encoder.layer.{i}.layernorm_after.bias""") ) rename_keys.append((F"""blocks.{i}.mlp.fc1.weight""", F"""deit.encoder.layer.{i}.intermediate.dense.weight""") ) rename_keys.append((F"""blocks.{i}.mlp.fc1.bias""", F"""deit.encoder.layer.{i}.intermediate.dense.bias""") ) rename_keys.append((F"""blocks.{i}.mlp.fc2.weight""", F"""deit.encoder.layer.{i}.output.dense.weight""") ) rename_keys.append((F"""blocks.{i}.mlp.fc2.bias""", F"""deit.encoder.layer.{i}.output.dense.bias""") ) # projection layer + position embeddings rename_keys.extend( [ ('cls_token', 'deit.embeddings.cls_token'), ('dist_token', 'deit.embeddings.distillation_token'), ('patch_embed.proj.weight', 'deit.embeddings.patch_embeddings.projection.weight'), ('patch_embed.proj.bias', 'deit.embeddings.patch_embeddings.projection.bias'), ('pos_embed', 'deit.embeddings.position_embeddings'), ] ) if base_model: # layernorm + pooler rename_keys.extend( [ ('norm.weight', 'layernorm.weight'), ('norm.bias', 'layernorm.bias'), ('pre_logits.fc.weight', 'pooler.dense.weight'), ('pre_logits.fc.bias', 'pooler.dense.bias'), ] ) # if just the base model, we should remove "deit" from all keys that start with "deit" snake_case__ : Tuple = [(pair[0], pair[1][4:]) if pair[1].startswith('deit' ) else pair for pair in rename_keys] else: # layernorm + classification heads rename_keys.extend( [ ('norm.weight', 'deit.layernorm.weight'), ('norm.bias', 'deit.layernorm.bias'), ('head.weight', 'cls_classifier.weight'), ('head.bias', 'cls_classifier.bias'), ('head_dist.weight', 'distillation_classifier.weight'), ('head_dist.bias', 'distillation_classifier.bias'), ] ) return rename_keys def UpperCamelCase__ ( A__ , A__ , A__=False ) -> Optional[Any]: for i in range(config.num_hidden_layers ): if base_model: snake_case__ : Optional[int] = "" else: snake_case__ : str = "deit." # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) snake_case__ : int = state_dict.pop(F"""blocks.{i}.attn.qkv.weight""" ) snake_case__ : Union[str, Any] = state_dict.pop(F"""blocks.{i}.attn.qkv.bias""" ) # next, add query, keys and values (in that order) to the state dict snake_case__ : Optional[int] = in_proj_weight[ : config.hidden_size, : ] snake_case__ : List[str] = in_proj_bias[: config.hidden_size] snake_case__ : Union[str, Any] = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] snake_case__ : List[str] = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] snake_case__ : Optional[Any] = in_proj_weight[ -config.hidden_size :, : ] snake_case__ : str = in_proj_bias[-config.hidden_size :] def UpperCamelCase__ ( A__ , A__ , A__ ) -> List[str]: snake_case__ : Union[str, Any] = dct.pop(_lowerCamelCase ) snake_case__ : Tuple = val def UpperCamelCase__ ( ) -> Optional[Any]: snake_case__ : Optional[int] = "http://images.cocodataset.org/val2017/000000039769.jpg" snake_case__ : Any = Image.open(requests.get(_lowerCamelCase , stream=_lowerCamelCase ).raw ) return im @torch.no_grad() def UpperCamelCase__ ( A__ , A__ ) -> str: snake_case__ : Tuple = DeiTConfig() # all deit models have fine-tuned heads snake_case__ : Union[str, Any] = False # dataset (fine-tuned on ImageNet 2012), patch_size and image_size snake_case__ : Dict = 1000 snake_case__ : List[str] = "huggingface/label-files" snake_case__ : Tuple = "imagenet-1k-id2label.json" snake_case__ : int = json.load(open(hf_hub_download(_lowerCamelCase , _lowerCamelCase , repo_type='dataset' ) , 'r' ) ) snake_case__ : List[Any] = {int(_lowerCamelCase ): v for k, v in idalabel.items()} snake_case__ : Tuple = idalabel snake_case__ : Dict = {v: k for k, v in idalabel.items()} snake_case__ : Union[str, Any] = int(deit_name[-6:-4] ) snake_case__ : Optional[int] = int(deit_name[-3:] ) # size of the architecture if deit_name[9:].startswith('tiny' ): snake_case__ : Union[str, Any] = 192 snake_case__ : List[str] = 768 snake_case__ : Optional[Any] = 12 snake_case__ : str = 3 elif deit_name[9:].startswith('small' ): snake_case__ : str = 384 snake_case__ : Union[str, Any] = 1536 snake_case__ : Union[str, Any] = 12 snake_case__ : Optional[Any] = 6 if deit_name[9:].startswith('base' ): pass elif deit_name[4:].startswith('large' ): snake_case__ : Union[str, Any] = 1024 snake_case__ : Tuple = 4096 snake_case__ : Optional[Any] = 24 snake_case__ : Optional[int] = 16 # load original model from timm snake_case__ : Tuple = timm.create_model(_lowerCamelCase , pretrained=_lowerCamelCase ) timm_model.eval() # load state_dict of original model, remove and rename some keys snake_case__ : Optional[Any] = timm_model.state_dict() snake_case__ : List[Any] = create_rename_keys(_lowerCamelCase , _lowerCamelCase ) for src, dest in rename_keys: rename_key(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) read_in_q_k_v(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) # load HuggingFace model snake_case__ : List[Any] = DeiTForImageClassificationWithTeacher(_lowerCamelCase ).eval() model.load_state_dict(_lowerCamelCase ) # Check outputs on an image, prepared by DeiTImageProcessor snake_case__ : Dict = int( (256 / 224) * config.image_size ) # to maintain same ratio w.r.t. 224 images, see https://github.com/facebookresearch/deit/blob/ab5715372db8c6cad5740714b2216d55aeae052e/datasets.py#L103 snake_case__ : str = DeiTImageProcessor(size=_lowerCamelCase , crop_size=config.image_size ) snake_case__ : List[str] = image_processor(images=prepare_img() , return_tensors='pt' ) snake_case__ : List[Any] = encoding["pixel_values"] snake_case__ : Tuple = model(_lowerCamelCase ) snake_case__ : List[Any] = timm_model(_lowerCamelCase ) assert timm_logits.shape == outputs.logits.shape assert torch.allclose(_lowerCamelCase , outputs.logits , atol=1e-3 ) Path(_lowerCamelCase ).mkdir(exist_ok=_lowerCamelCase ) print(F"""Saving model {deit_name} to {pytorch_dump_folder_path}""" ) model.save_pretrained(_lowerCamelCase ) print(F"""Saving image processor to {pytorch_dump_folder_path}""" ) image_processor.save_pretrained(_lowerCamelCase ) if __name__ == "__main__": lowerCAmelCase__ : Tuple = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--deit_name''', default='''vit_deit_base_distilled_patch16_224''', type=str, help='''Name of the DeiT timm model you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) lowerCAmelCase__ : List[str] = parser.parse_args() convert_deit_checkpoint(args.deit_name, args.pytorch_dump_folder_path)
706
import argparse import json from collections import OrderedDict from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( ConditionalDetrConfig, ConditionalDetrForObjectDetection, ConditionalDetrForSegmentation, ConditionalDetrImageProcessor, ) from transformers.utils import logging logging.set_verbosity_info() lowerCAmelCase__ : Dict = logging.get_logger(__name__) # here we list all keys to be renamed (original name on the left, our name on the right) lowerCAmelCase__ : Optional[Any] = [] for i in range(6): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append( (F'''transformer.encoder.layers.{i}.self_attn.out_proj.weight''', F'''encoder.layers.{i}.self_attn.out_proj.weight''') ) rename_keys.append( (F'''transformer.encoder.layers.{i}.self_attn.out_proj.bias''', F'''encoder.layers.{i}.self_attn.out_proj.bias''') ) rename_keys.append((F'''transformer.encoder.layers.{i}.linear1.weight''', F'''encoder.layers.{i}.fc1.weight''')) rename_keys.append((F'''transformer.encoder.layers.{i}.linear1.bias''', F'''encoder.layers.{i}.fc1.bias''')) rename_keys.append((F'''transformer.encoder.layers.{i}.linear2.weight''', F'''encoder.layers.{i}.fc2.weight''')) rename_keys.append((F'''transformer.encoder.layers.{i}.linear2.bias''', F'''encoder.layers.{i}.fc2.bias''')) rename_keys.append( (F'''transformer.encoder.layers.{i}.norm1.weight''', F'''encoder.layers.{i}.self_attn_layer_norm.weight''') ) rename_keys.append((F'''transformer.encoder.layers.{i}.norm1.bias''', F'''encoder.layers.{i}.self_attn_layer_norm.bias''')) rename_keys.append((F'''transformer.encoder.layers.{i}.norm2.weight''', F'''encoder.layers.{i}.final_layer_norm.weight''')) rename_keys.append((F'''transformer.encoder.layers.{i}.norm2.bias''', F'''encoder.layers.{i}.final_layer_norm.bias''')) # decoder layers: 2 times output projection, 2 feedforward neural networks and 3 layernorms rename_keys.append( (F'''transformer.decoder.layers.{i}.self_attn.out_proj.weight''', F'''decoder.layers.{i}.self_attn.out_proj.weight''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.self_attn.out_proj.bias''', F'''decoder.layers.{i}.self_attn.out_proj.bias''') ) rename_keys.append( ( F'''transformer.decoder.layers.{i}.cross_attn.out_proj.weight''', F'''decoder.layers.{i}.encoder_attn.out_proj.weight''', ) ) rename_keys.append( ( F'''transformer.decoder.layers.{i}.cross_attn.out_proj.bias''', F'''decoder.layers.{i}.encoder_attn.out_proj.bias''', ) ) rename_keys.append((F'''transformer.decoder.layers.{i}.linear1.weight''', F'''decoder.layers.{i}.fc1.weight''')) rename_keys.append((F'''transformer.decoder.layers.{i}.linear1.bias''', F'''decoder.layers.{i}.fc1.bias''')) rename_keys.append((F'''transformer.decoder.layers.{i}.linear2.weight''', F'''decoder.layers.{i}.fc2.weight''')) rename_keys.append((F'''transformer.decoder.layers.{i}.linear2.bias''', F'''decoder.layers.{i}.fc2.bias''')) rename_keys.append( (F'''transformer.decoder.layers.{i}.norm1.weight''', F'''decoder.layers.{i}.self_attn_layer_norm.weight''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.norm1.bias''', F'''decoder.layers.{i}.self_attn_layer_norm.bias''')) rename_keys.append( (F'''transformer.decoder.layers.{i}.norm2.weight''', F'''decoder.layers.{i}.encoder_attn_layer_norm.weight''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.norm2.bias''', F'''decoder.layers.{i}.encoder_attn_layer_norm.bias''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.norm3.weight''', F'''decoder.layers.{i}.final_layer_norm.weight''')) rename_keys.append((F'''transformer.decoder.layers.{i}.norm3.bias''', F'''decoder.layers.{i}.final_layer_norm.bias''')) # q, k, v projections in self/cross-attention in decoder for conditional DETR rename_keys.append( (F'''transformer.decoder.layers.{i}.sa_qcontent_proj.weight''', F'''decoder.layers.{i}.sa_qcontent_proj.weight''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.sa_kcontent_proj.weight''', F'''decoder.layers.{i}.sa_kcontent_proj.weight''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.sa_qpos_proj.weight''', F'''decoder.layers.{i}.sa_qpos_proj.weight''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.sa_kpos_proj.weight''', F'''decoder.layers.{i}.sa_kpos_proj.weight''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.sa_v_proj.weight''', F'''decoder.layers.{i}.sa_v_proj.weight''')) rename_keys.append( (F'''transformer.decoder.layers.{i}.ca_qcontent_proj.weight''', F'''decoder.layers.{i}.ca_qcontent_proj.weight''') ) # rename_keys.append((f"transformer.decoder.layers.{i}.ca_qpos_proj.weight", f"decoder.layers.{i}.ca_qpos_proj.weight")) rename_keys.append( (F'''transformer.decoder.layers.{i}.ca_kcontent_proj.weight''', F'''decoder.layers.{i}.ca_kcontent_proj.weight''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.ca_kpos_proj.weight''', F'''decoder.layers.{i}.ca_kpos_proj.weight''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.ca_v_proj.weight''', F'''decoder.layers.{i}.ca_v_proj.weight''')) rename_keys.append( (F'''transformer.decoder.layers.{i}.ca_qpos_sine_proj.weight''', F'''decoder.layers.{i}.ca_qpos_sine_proj.weight''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.sa_qcontent_proj.bias''', F'''decoder.layers.{i}.sa_qcontent_proj.bias''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.sa_kcontent_proj.bias''', F'''decoder.layers.{i}.sa_kcontent_proj.bias''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.sa_qpos_proj.bias''', F'''decoder.layers.{i}.sa_qpos_proj.bias''')) rename_keys.append((F'''transformer.decoder.layers.{i}.sa_kpos_proj.bias''', F'''decoder.layers.{i}.sa_kpos_proj.bias''')) rename_keys.append((F'''transformer.decoder.layers.{i}.sa_v_proj.bias''', F'''decoder.layers.{i}.sa_v_proj.bias''')) rename_keys.append( (F'''transformer.decoder.layers.{i}.ca_qcontent_proj.bias''', F'''decoder.layers.{i}.ca_qcontent_proj.bias''') ) # rename_keys.append((f"transformer.decoder.layers.{i}.ca_qpos_proj.bias", f"decoder.layers.{i}.ca_qpos_proj.bias")) rename_keys.append( (F'''transformer.decoder.layers.{i}.ca_kcontent_proj.bias''', F'''decoder.layers.{i}.ca_kcontent_proj.bias''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.ca_kpos_proj.bias''', F'''decoder.layers.{i}.ca_kpos_proj.bias''')) rename_keys.append((F'''transformer.decoder.layers.{i}.ca_v_proj.bias''', F'''decoder.layers.{i}.ca_v_proj.bias''')) rename_keys.append( (F'''transformer.decoder.layers.{i}.ca_qpos_sine_proj.bias''', F'''decoder.layers.{i}.ca_qpos_sine_proj.bias''') ) # convolutional projection + query embeddings + layernorm of decoder + class and bounding box heads # for conditional DETR, also convert reference point head and query scale MLP rename_keys.extend( [ ('''input_proj.weight''', '''input_projection.weight'''), ('''input_proj.bias''', '''input_projection.bias'''), ('''query_embed.weight''', '''query_position_embeddings.weight'''), ('''transformer.decoder.norm.weight''', '''decoder.layernorm.weight'''), ('''transformer.decoder.norm.bias''', '''decoder.layernorm.bias'''), ('''class_embed.weight''', '''class_labels_classifier.weight'''), ('''class_embed.bias''', '''class_labels_classifier.bias'''), ('''bbox_embed.layers.0.weight''', '''bbox_predictor.layers.0.weight'''), ('''bbox_embed.layers.0.bias''', '''bbox_predictor.layers.0.bias'''), ('''bbox_embed.layers.1.weight''', '''bbox_predictor.layers.1.weight'''), ('''bbox_embed.layers.1.bias''', '''bbox_predictor.layers.1.bias'''), ('''bbox_embed.layers.2.weight''', '''bbox_predictor.layers.2.weight'''), ('''bbox_embed.layers.2.bias''', '''bbox_predictor.layers.2.bias'''), ('''transformer.decoder.ref_point_head.layers.0.weight''', '''decoder.ref_point_head.layers.0.weight'''), ('''transformer.decoder.ref_point_head.layers.0.bias''', '''decoder.ref_point_head.layers.0.bias'''), ('''transformer.decoder.ref_point_head.layers.1.weight''', '''decoder.ref_point_head.layers.1.weight'''), ('''transformer.decoder.ref_point_head.layers.1.bias''', '''decoder.ref_point_head.layers.1.bias'''), ('''transformer.decoder.query_scale.layers.0.weight''', '''decoder.query_scale.layers.0.weight'''), ('''transformer.decoder.query_scale.layers.0.bias''', '''decoder.query_scale.layers.0.bias'''), ('''transformer.decoder.query_scale.layers.1.weight''', '''decoder.query_scale.layers.1.weight'''), ('''transformer.decoder.query_scale.layers.1.bias''', '''decoder.query_scale.layers.1.bias'''), ('''transformer.decoder.layers.0.ca_qpos_proj.weight''', '''decoder.layers.0.ca_qpos_proj.weight'''), ('''transformer.decoder.layers.0.ca_qpos_proj.bias''', '''decoder.layers.0.ca_qpos_proj.bias'''), ] ) def UpperCamelCase__ ( A__ , A__ , A__ ) -> List[str]: snake_case__ : int = state_dict.pop(A__ ) snake_case__ : Union[str, Any] = val def UpperCamelCase__ ( A__ ) -> int: snake_case__ : List[Any] = OrderedDict() for key, value in state_dict.items(): if "backbone.0.body" in key: snake_case__ : Any = key.replace('backbone.0.body' , 'backbone.conv_encoder.model' ) snake_case__ : Optional[int] = value else: snake_case__ : Optional[int] = value return new_state_dict def UpperCamelCase__ ( A__ , A__=False ) -> Optional[int]: snake_case__ : Optional[int] = '' if is_panoptic: snake_case__ : Tuple = 'conditional_detr.' # first: transformer encoder for i in range(6 ): # read in weights + bias of input projection layer (in PyTorch's MultiHeadAttention, this is a single matrix + bias) snake_case__ : int = state_dict.pop(F"""{prefix}transformer.encoder.layers.{i}.self_attn.in_proj_weight""" ) snake_case__ : str = state_dict.pop(F"""{prefix}transformer.encoder.layers.{i}.self_attn.in_proj_bias""" ) # next, add query, keys and values (in that order) to the state dict snake_case__ : Union[str, Any] = in_proj_weight[:256, :] snake_case__ : Union[str, Any] = in_proj_bias[:256] snake_case__ : Union[str, Any] = in_proj_weight[256:512, :] snake_case__ : Optional[Any] = in_proj_bias[256:512] snake_case__ : List[str] = in_proj_weight[-256:, :] snake_case__ : Tuple = in_proj_bias[-256:] def UpperCamelCase__ ( ) -> Tuple: snake_case__ : int = 'http://images.cocodataset.org/val2017/000000039769.jpg' snake_case__ : str = Image.open(requests.get(A__ , stream=A__ ).raw ) return im @torch.no_grad() def UpperCamelCase__ ( A__ , A__ ) -> str: snake_case__ : List[Any] = ConditionalDetrConfig() # set backbone and dilation attributes if "resnet101" in model_name: snake_case__ : Any = 'resnet101' if "dc5" in model_name: snake_case__ : Any = True snake_case__ : int = 'panoptic' in model_name if is_panoptic: snake_case__ : str = 250 else: snake_case__ : Union[str, Any] = 91 snake_case__ : Optional[int] = 'huggingface/label-files' snake_case__ : Optional[Any] = 'coco-detection-id2label.json' snake_case__ : str = json.load(open(hf_hub_download(A__ , A__ , repo_type='dataset' ) , 'r' ) ) snake_case__ : List[Any] = {int(A__ ): v for k, v in idalabel.items()} snake_case__ : Any = idalabel snake_case__ : int = {v: k for k, v in idalabel.items()} # load image processor snake_case__ : List[Any] = 'coco_panoptic' if is_panoptic else 'coco_detection' snake_case__ : List[Any] = ConditionalDetrImageProcessor(format=A__ ) # prepare image snake_case__ : List[str] = prepare_img() snake_case__ : Any = image_processor(images=A__ , return_tensors='pt' ) snake_case__ : Dict = encoding['pixel_values'] logger.info(F"""Converting model {model_name}...""" ) # load original model from torch hub snake_case__ : Any = torch.hub.load('DeppMeng/ConditionalDETR' , A__ , pretrained=A__ ).eval() snake_case__ : Tuple = conditional_detr.state_dict() # rename keys for src, dest in rename_keys: if is_panoptic: snake_case__ : List[Any] = 'conditional_detr.' + src rename_key(A__ , A__ , A__ ) snake_case__ : Dict = rename_backbone_keys(A__ ) # query, key and value matrices need special treatment read_in_q_k_v(A__ , is_panoptic=A__ ) # important: we need to prepend a prefix to each of the base model keys as the head models use different attributes for them snake_case__ : Optional[int] = 'conditional_detr.model.' if is_panoptic else 'model.' for key in state_dict.copy().keys(): if is_panoptic: if ( key.startswith('conditional_detr' ) and not key.startswith('class_labels_classifier' ) and not key.startswith('bbox_predictor' ) ): snake_case__ : List[Any] = state_dict.pop(A__ ) snake_case__ : Optional[int] = val elif "class_labels_classifier" in key or "bbox_predictor" in key: snake_case__ : str = state_dict.pop(A__ ) snake_case__ : List[Any] = val elif key.startswith('bbox_attention' ) or key.startswith('mask_head' ): continue else: snake_case__ : Union[str, Any] = state_dict.pop(A__ ) snake_case__ : Dict = val else: if not key.startswith('class_labels_classifier' ) and not key.startswith('bbox_predictor' ): snake_case__ : List[Any] = state_dict.pop(A__ ) snake_case__ : Optional[int] = val # finally, create HuggingFace model and load state dict snake_case__ : Union[str, Any] = ConditionalDetrForSegmentation(A__ ) if is_panoptic else ConditionalDetrForObjectDetection(A__ ) model.load_state_dict(A__ ) model.eval() model.push_to_hub(repo_id=A__ , organization='DepuMeng' , commit_message='Add model' ) # verify our conversion snake_case__ : Tuple = conditional_detr(A__ ) snake_case__ : str = model(A__ ) assert torch.allclose(outputs.logits , original_outputs['pred_logits'] , atol=1e-4 ) assert torch.allclose(outputs.pred_boxes , original_outputs['pred_boxes'] , atol=1e-4 ) if is_panoptic: assert torch.allclose(outputs.pred_masks , original_outputs['pred_masks'] , atol=1e-4 ) # Save model and image processor logger.info(F"""Saving PyTorch model and image processor to {pytorch_dump_folder_path}...""" ) Path(A__ ).mkdir(exist_ok=A__ ) model.save_pretrained(A__ ) image_processor.save_pretrained(A__ ) if __name__ == "__main__": lowerCAmelCase__ : Any = argparse.ArgumentParser() parser.add_argument( '''--model_name''', default='''conditional_detr_resnet50''', type=str, help='''Name of the CONDITIONAL_DETR model you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the folder to output PyTorch model.''' ) lowerCAmelCase__ : int = parser.parse_args() convert_conditional_detr_checkpoint(args.model_name, args.pytorch_dump_folder_path)
699
0
import numpy # List of input, output pairs lowerCAmelCase__ : int = ( ((5, 2, 3), 15), ((6, 5, 9), 25), ((11, 12, 13), 41), ((1, 1, 1), 8), ((11, 12, 13), 41), ) lowerCAmelCase__ : Tuple = (((5_15, 22, 13), 5_55), ((61, 35, 49), 1_50)) lowerCAmelCase__ : Dict = [2, 4, 1, 5] lowerCAmelCase__ : Optional[Any] = len(train_data) lowerCAmelCase__ : Optional[Any] = 0.0_09 def UpperCamelCase__ ( A__ , A__="train" ) -> Any: return calculate_hypothesis_value(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) - output( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) def UpperCamelCase__ ( A__ ) -> int: snake_case__ : Tuple = 0 for i in range(len(_SCREAMING_SNAKE_CASE ) - 1 ): hyp_val += data_input_tuple[i] * parameter_vector[i + 1] hyp_val += parameter_vector[0] return hyp_val def UpperCamelCase__ ( A__ , A__ ) -> Optional[int]: if data_set == "train": return train_data[example_no][1] elif data_set == "test": return test_data[example_no][1] return None def UpperCamelCase__ ( A__ , A__ ) -> Dict: if data_set == "train": return _hypothesis_value(train_data[example_no][0] ) elif data_set == "test": return _hypothesis_value(test_data[example_no][0] ) return None def UpperCamelCase__ ( A__ , A__=m ) -> List[str]: snake_case__ : List[Any] = 0 for i in range(_SCREAMING_SNAKE_CASE ): if index == -1: summation_value += _error(_SCREAMING_SNAKE_CASE ) else: summation_value += _error(_SCREAMING_SNAKE_CASE ) * train_data[i][0][index] return summation_value def UpperCamelCase__ ( A__ ) -> int: snake_case__ : Any = summation_of_cost_derivative(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) / m return cost_derivative_value def UpperCamelCase__ ( ) -> Optional[Any]: global parameter_vector # Tune these values to set a tolerance value for predicted output snake_case__ : Union[str, Any] = 0.0_0_0_0_0_2 snake_case__ : int = 0 snake_case__ : str = 0 while True: j += 1 snake_case__ : List[Any] = [0, 0, 0, 0] for i in range(0 , len(_SCREAMING_SNAKE_CASE ) ): snake_case__ : Dict = get_cost_derivative(i - 1 ) snake_case__ : str = ( parameter_vector[i] - LEARNING_RATE * cost_derivative ) if numpy.allclose( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , atol=_SCREAMING_SNAKE_CASE , rtol=_SCREAMING_SNAKE_CASE , ): break snake_case__ : Tuple = temp_parameter_vector print(('Number of iterations:', j) ) def UpperCamelCase__ ( ) -> Tuple: for i in range(len(_SCREAMING_SNAKE_CASE ) ): print(('Actual output value:', output(_SCREAMING_SNAKE_CASE , 'test' )) ) print(('Hypothesis output:', calculate_hypothesis_value(_SCREAMING_SNAKE_CASE , 'test' )) ) if __name__ == "__main__": run_gradient_descent() print('''\nTesting gradient descent for a linear hypothesis function.\n''') test_gradient_descent()
707
from collections import namedtuple lowerCAmelCase__ : Union[str, Any] = namedtuple('''from_to''', '''from_ to''') lowerCAmelCase__ : Tuple = { '''cubicmeter''': from_to(1, 1), '''litre''': from_to(0.0_01, 10_00), '''kilolitre''': from_to(1, 1), '''gallon''': from_to(0.0_04_54, 2_64.1_72), '''cubicyard''': from_to(0.7_64_55, 1.3_07_95), '''cubicfoot''': from_to(0.0_28, 35.31_47), '''cup''': from_to(0.0_00_23_65_88, 42_26.75), } def UpperCamelCase__ ( A__ , A__ , A__ ) -> float: if from_type not in METRIC_CONVERSION: raise ValueError( F"""Invalid 'from_type' value: {from_type!r} Supported values are:\n""" + ', '.join(A__ ) ) if to_type not in METRIC_CONVERSION: raise ValueError( F"""Invalid 'to_type' value: {to_type!r}. Supported values are:\n""" + ', '.join(A__ ) ) return value * METRIC_CONVERSION[from_type].from_ * METRIC_CONVERSION[to_type].to if __name__ == "__main__": import doctest doctest.testmod()
699
0
import json import os import re import sys import urllib.request import requests from bsa import BeautifulSoup lowerCAmelCase__ : Union[str, Any] = { '''User-Agent''': '''Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36''' ''' (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.19582''' } def UpperCamelCase__ ( A__ = "dhaka" , A__ = 5 ) -> int: snake_case__ : List[str] = min(A__ , 50 ) # Prevent abuse! snake_case__ : int = { """q""": query, """tbm""": """isch""", """hl""": """en""", """ijn""": """0""", } snake_case__ : Dict = requests.get('https://www.google.com/search' , params=A__ , headers=A__ ) snake_case__ : Dict = BeautifulSoup(html.text , 'html.parser' ) snake_case__ : Union[str, Any] = """""".join( re.findall(r'AF_initDataCallback\(([^<]+)\);' , str(soup.select('script' ) ) ) ) snake_case__ : Dict = json.dumps(A__ ) snake_case__ : Any = json.loads(A__ ) snake_case__ : Any = re.findall( r'\[\"GRID_STATE0\",null,\[\[1,\[0,\".*?\",(.*),\"All\",' , A__ , ) if not matched_google_image_data: return 0 snake_case__ : Optional[int] = re.sub( r'\[\"(https\:\/\/encrypted-tbn0\.gstatic\.com\/images\?.*?)\",\d+,\d+\]' , '' , str(A__ ) , ) snake_case__ : int = re.findall( r'(?:\'|,),\[\"(https:|http.*?)\",\d+,\d+\]' , A__ , ) for index, fixed_full_res_image in enumerate(A__ ): if index >= max_images: return index snake_case__ : List[Any] = bytes(A__ , 'ascii' ).decode( 'unicode-escape' ) snake_case__ : Tuple = bytes(A__ , 'ascii' ).decode( 'unicode-escape' ) snake_case__ : Optional[int] = urllib.request.build_opener() snake_case__ : Any = [ ( """User-Agent""", """Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36""" """ (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.19582""", ) ] urllib.request.install_opener(A__ ) snake_case__ : Dict = F"""query_{query.replace(' ' , '_' )}""" if not os.path.exists(A__ ): os.makedirs(A__ ) urllib.request.urlretrieve( # noqa: S310 A__ , F"""{path_name}/original_size_img_{index}.jpg""" ) return index if __name__ == "__main__": try: lowerCAmelCase__ : List[str] = download_images_from_google_query(sys.argv[1]) print(F'''{image_count} images were downloaded to disk.''') except IndexError: print('''Please provide a search term.''') raise
708
import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging lowerCAmelCase__ : Tuple = logging.get_logger(__name__) lowerCAmelCase__ : Union[str, Any] = '''▁''' lowerCAmelCase__ : List[Any] = {'''vocab_file''': '''sentencepiece.bpe.model'''} lowerCAmelCase__ : Optional[Any] = { '''vocab_file''': { '''facebook/xglm-564M''': '''https://huggingface.co/facebook/xglm-564M/resolve/main/sentencepiece.bpe.model''', } } lowerCAmelCase__ : str = { '''facebook/xglm-564M''': 20_48, } class __snake_case ( _lowerCamelCase ): __lowerCamelCase = VOCAB_FILES_NAMES __lowerCamelCase = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase = ["""input_ids""", """attention_mask"""] def __init__( self , __UpperCamelCase , __UpperCamelCase="<s>" , __UpperCamelCase="</s>" , __UpperCamelCase="</s>" , __UpperCamelCase="<s>" , __UpperCamelCase="<unk>" , __UpperCamelCase="<pad>" , __UpperCamelCase = None , **__UpperCamelCase , ) -> None: '''simple docstring''' snake_case__ : Any = {} if sp_model_kwargs is None else sp_model_kwargs # Compatibility with the original tokenizer snake_case__ : Tuple = 7 snake_case__ : Dict = [F"""<madeupword{i}>""" for i in range(self.num_madeup_words )] snake_case__ : Union[str, Any] = kwargs.get('additional_special_tokens' , [] ) kwargs["additional_special_tokens"] += [ word for word in madeup_words if word not in kwargs["additional_special_tokens"] ] super().__init__( bos_token=__UpperCamelCase , eos_token=__UpperCamelCase , unk_token=__UpperCamelCase , sep_token=__UpperCamelCase , cls_token=__UpperCamelCase , pad_token=__UpperCamelCase , sp_model_kwargs=self.sp_model_kwargs , **__UpperCamelCase , ) snake_case__ : Optional[int] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(__UpperCamelCase ) ) snake_case__ : Optional[Any] = vocab_file # Original fairseq vocab and spm vocab must be "aligned": # Vocab | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 # -------- | ------- | ------- | ------ | ------- | --- | --- | --- | ----- | ----- | ---- # fairseq | '<s>' | '<pad>' | '</s>' | '<unk>' | ',' | '.' | '▁' | 's' | '▁de' | '-' # spm | '<unk>' | '<s>' | '</s>' | ',' | '.' | '▁' | 's' | '▁de' | '-' | '▁a' # The first "real" token "," has position 4 in the original fairseq vocab and position 3 in the spm vocab snake_case__ : Tuple = 1 # Mimic fairseq token-to-id alignment for the first 4 token snake_case__ : Tuple = {'<s>': 0, '<pad>': 1, '</s>': 2, '<unk>': 3} snake_case__ : List[Any] = len(self.sp_model ) snake_case__ : Optional[Any] = {F"""<madeupword{i}>""": sp_size + i + self.fairseq_offset for i in range(self.num_madeup_words )} self.fairseq_tokens_to_ids.update(__UpperCamelCase ) snake_case__ : Union[str, Any] = {v: k for k, v in self.fairseq_tokens_to_ids.items()} def __getstate__( self ) -> List[Any]: '''simple docstring''' snake_case__ : Union[str, Any] = self.__dict__.copy() snake_case__ : Optional[Any] = None snake_case__ : Tuple = self.sp_model.serialized_model_proto() return state def __setstate__( self , __UpperCamelCase ) -> Union[str, Any]: '''simple docstring''' snake_case__ : Union[str, Any] = d # for backward compatibility if not hasattr(self , 'sp_model_kwargs' ): snake_case__ : Any = {} snake_case__ : Dict = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.LoadFromSerializedProto(self.sp_model_proto ) def __a ( self , __UpperCamelCase , __UpperCamelCase = None ) -> List[int]: '''simple docstring''' if token_ids_a is None: return [self.sep_token_id] + token_ids_a snake_case__ : str = [self.sep_token_id] return sep + token_ids_a + sep + sep + token_ids_a def __a ( self , __UpperCamelCase , __UpperCamelCase = None , __UpperCamelCase = 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 ) if token_ids_a is None: return [1] + ([0] * len(__UpperCamelCase )) return [1] + ([0] * len(__UpperCamelCase )) + [1, 1] + ([0] * len(__UpperCamelCase )) def __a ( self , __UpperCamelCase , __UpperCamelCase = None ) -> List[int]: '''simple docstring''' snake_case__ : int = [self.sep_token_id] if token_ids_a is None: return len(sep + token_ids_a ) * [0] return len(sep + token_ids_a + sep + sep + token_ids_a ) * [0] @property def __a ( self ) -> Tuple: '''simple docstring''' return len(self.sp_model ) + self.fairseq_offset + self.num_madeup_words def __a ( self ) -> Union[str, Any]: '''simple docstring''' snake_case__ : int = {self.convert_ids_to_tokens(__UpperCamelCase ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __a ( self , __UpperCamelCase ) -> List[str]: '''simple docstring''' return self.sp_model.encode(__UpperCamelCase , out_type=__UpperCamelCase ) def __a ( self , __UpperCamelCase ) -> Union[str, Any]: '''simple docstring''' if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] snake_case__ : Optional[Any] = self.sp_model.PieceToId(__UpperCamelCase ) # Need to return unknown token if the SP model returned 0 return spm_id + self.fairseq_offset if spm_id else self.unk_token_id def __a ( self , __UpperCamelCase ) -> Dict: '''simple docstring''' if index in self.fairseq_ids_to_tokens: return self.fairseq_ids_to_tokens[index] return self.sp_model.IdToPiece(index - self.fairseq_offset ) def __a ( self , __UpperCamelCase ) -> int: '''simple docstring''' snake_case__ : int = ''.join(__UpperCamelCase ).replace(__UpperCamelCase , ' ' ).strip() return out_string def __a ( self , __UpperCamelCase , __UpperCamelCase = 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__ : List[str] = 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__ : Any = self.sp_model.serialized_model_proto() fi.write(__UpperCamelCase ) return (out_vocab_file,)
699
0
import importlib import os import sys # This is required to make the module import works (when the python process is running from the root of the repo) sys.path.append('''.''') def UpperCamelCase__ ( A__ ) -> Dict: snake_case__ : List[Any] = test_file.split(os.path.sep ) if components[0:2] != ["tests", "models"]: raise ValueError( '`test_file` should start with `tests/models/` (with `/` being the OS specific path separator). Got ' F"""{test_file} instead.""" ) snake_case__ : int = components[-1] if not test_fn.endswith('py' ): raise ValueError(F"""`test_file` should be a python file. Got {test_fn} instead.""" ) if not test_fn.startswith('test_modeling_' ): raise ValueError( F"""`test_file` should point to a file name of the form `test_modeling_*.py`. Got {test_fn} instead.""" ) snake_case__ : str = components[:-1] + [test_fn.replace('.py' , '' )] snake_case__ : Dict = '.'.join(lowerCAmelCase__ ) return test_module_path def UpperCamelCase__ ( A__ ) -> Union[str, Any]: snake_case__ : List[Any] = get_module_path(lowerCAmelCase__ ) snake_case__ : List[Any] = importlib.import_module(lowerCAmelCase__ ) return test_module def UpperCamelCase__ ( A__ ) -> Tuple: snake_case__ : Tuple = [] snake_case__ : Optional[Any] = get_test_module(lowerCAmelCase__ ) for attr in dir(lowerCAmelCase__ ): if attr.endswith('ModelTester' ): tester_classes.append(getattr(lowerCAmelCase__ , lowerCAmelCase__ ) ) # sort with class names return sorted(lowerCAmelCase__ , key=lambda A__ : x.__name__ ) def UpperCamelCase__ ( A__ ) -> Optional[Any]: snake_case__ : str = [] snake_case__ : int = get_test_module(lowerCAmelCase__ ) for attr in dir(lowerCAmelCase__ ): snake_case__ : Tuple = getattr(lowerCAmelCase__ , lowerCAmelCase__ ) # (TF/Flax)ModelTesterMixin is also an attribute in specific model test module. Let's exclude them by checking # `all_model_classes` is not empty (which also excludes other special classes). snake_case__ : str = getattr(lowerCAmelCase__ , 'all_model_classes' , [] ) if len(lowerCAmelCase__ ) > 0: test_classes.append(lowerCAmelCase__ ) # sort with class names return sorted(lowerCAmelCase__ , key=lambda A__ : x.__name__ ) def UpperCamelCase__ ( A__ ) -> Tuple: snake_case__ : Union[str, Any] = get_test_classes(lowerCAmelCase__ ) snake_case__ : Any = set() for test_class in test_classes: model_classes.update(test_class.all_model_classes ) # sort with class names return sorted(lowerCAmelCase__ , key=lambda A__ : x.__name__ ) def UpperCamelCase__ ( A__ ) -> Optional[int]: snake_case__ : Dict = test_class() if hasattr(lowerCAmelCase__ , 'setUp' ): test.setUp() snake_case__ : Any = None if hasattr(lowerCAmelCase__ , 'model_tester' ): # `(TF/Flax)ModelTesterMixin` has this attribute default to `None`. Let's skip this case. if test.model_tester is not None: snake_case__ : int = test.model_tester.__class__ return model_tester def UpperCamelCase__ ( A__ , A__ ) -> List[str]: snake_case__ : str = get_test_classes(lowerCAmelCase__ ) snake_case__ : Dict = [] for test_class in test_classes: if model_class in test_class.all_model_classes: target_test_classes.append(lowerCAmelCase__ ) # sort with class names return sorted(lowerCAmelCase__ , key=lambda A__ : x.__name__ ) def UpperCamelCase__ ( A__ , A__ ) -> str: snake_case__ : Dict = get_test_classes_for_model(lowerCAmelCase__ , lowerCAmelCase__ ) snake_case__ : Any = [] for test_class in test_classes: snake_case__ : List[Any] = get_model_tester_from_test_class(lowerCAmelCase__ ) if tester_class is not None: tester_classes.append(lowerCAmelCase__ ) # sort with class names return sorted(lowerCAmelCase__ , key=lambda A__ : x.__name__ ) def UpperCamelCase__ ( A__ ) -> Union[str, Any]: snake_case__ : int = get_test_classes(lowerCAmelCase__ ) snake_case__ : Optional[Any] = {test_class: get_model_tester_from_test_class(lowerCAmelCase__ ) for test_class in test_classes} return test_tester_mapping def UpperCamelCase__ ( A__ ) -> Tuple: snake_case__ : Union[str, Any] = get_model_classes(lowerCAmelCase__ ) snake_case__ : Optional[int] = { model_class: get_test_classes_for_model(lowerCAmelCase__ , lowerCAmelCase__ ) for model_class in model_classes } return model_test_mapping def UpperCamelCase__ ( A__ ) -> List[str]: snake_case__ : List[Any] = get_model_classes(lowerCAmelCase__ ) snake_case__ : Optional[int] = { model_class: get_tester_classes_for_model(lowerCAmelCase__ , lowerCAmelCase__ ) for model_class in model_classes } return model_to_tester_mapping def UpperCamelCase__ ( A__ ) -> Optional[Any]: if isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): return o elif isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): return o.__name__ elif isinstance(lowerCAmelCase__ , (list, tuple) ): return [to_json(lowerCAmelCase__ ) for x in o] elif isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): return {to_json(lowerCAmelCase__ ): to_json(lowerCAmelCase__ ) for k, v in o.items()} else: return o
709
import collections from typing import List, Optional, Union from ...tokenization_utils_base import BatchEncoding from ...utils import TensorType, add_end_docstrings, add_start_docstrings, logging from ..bert.tokenization_bert_fast import BertTokenizerFast from .tokenization_dpr import DPRContextEncoderTokenizer, DPRQuestionEncoderTokenizer, DPRReaderTokenizer lowerCAmelCase__ : Any = logging.get_logger(__name__) lowerCAmelCase__ : List[Any] = {'''vocab_file''': '''vocab.txt''', '''tokenizer_file''': '''tokenizer.json'''} lowerCAmelCase__ : Any = { '''vocab_file''': { '''facebook/dpr-ctx_encoder-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/vocab.txt''' ), '''facebook/dpr-ctx_encoder-multiset-base''': ( '''https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/vocab.txt''' ), }, '''tokenizer_file''': { '''facebook/dpr-ctx_encoder-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/tokenizer.json''' ), '''facebook/dpr-ctx_encoder-multiset-base''': ( '''https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/tokenizer.json''' ), }, } lowerCAmelCase__ : Any = { '''vocab_file''': { '''facebook/dpr-question_encoder-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/vocab.txt''' ), '''facebook/dpr-question_encoder-multiset-base''': ( '''https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/vocab.txt''' ), }, '''tokenizer_file''': { '''facebook/dpr-question_encoder-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/tokenizer.json''' ), '''facebook/dpr-question_encoder-multiset-base''': ( '''https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/tokenizer.json''' ), }, } lowerCAmelCase__ : Tuple = { '''vocab_file''': { '''facebook/dpr-reader-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/vocab.txt''' ), '''facebook/dpr-reader-multiset-base''': ( '''https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/vocab.txt''' ), }, '''tokenizer_file''': { '''facebook/dpr-reader-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/tokenizer.json''' ), '''facebook/dpr-reader-multiset-base''': ( '''https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/tokenizer.json''' ), }, } lowerCAmelCase__ : Dict = { '''facebook/dpr-ctx_encoder-single-nq-base''': 5_12, '''facebook/dpr-ctx_encoder-multiset-base''': 5_12, } lowerCAmelCase__ : Union[str, Any] = { '''facebook/dpr-question_encoder-single-nq-base''': 5_12, '''facebook/dpr-question_encoder-multiset-base''': 5_12, } lowerCAmelCase__ : Optional[Any] = { '''facebook/dpr-reader-single-nq-base''': 5_12, '''facebook/dpr-reader-multiset-base''': 5_12, } lowerCAmelCase__ : Tuple = { '''facebook/dpr-ctx_encoder-single-nq-base''': {'''do_lower_case''': True}, '''facebook/dpr-ctx_encoder-multiset-base''': {'''do_lower_case''': True}, } lowerCAmelCase__ : Any = { '''facebook/dpr-question_encoder-single-nq-base''': {'''do_lower_case''': True}, '''facebook/dpr-question_encoder-multiset-base''': {'''do_lower_case''': True}, } lowerCAmelCase__ : List[str] = { '''facebook/dpr-reader-single-nq-base''': {'''do_lower_case''': True}, '''facebook/dpr-reader-multiset-base''': {'''do_lower_case''': True}, } class __snake_case ( _lowerCamelCase ): __lowerCamelCase = VOCAB_FILES_NAMES __lowerCamelCase = CONTEXT_ENCODER_PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase = CONTEXT_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase = CONTEXT_ENCODER_PRETRAINED_INIT_CONFIGURATION __lowerCamelCase = DPRContextEncoderTokenizer class __snake_case ( _lowerCamelCase ): __lowerCamelCase = VOCAB_FILES_NAMES __lowerCamelCase = QUESTION_ENCODER_PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase = QUESTION_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase = QUESTION_ENCODER_PRETRAINED_INIT_CONFIGURATION __lowerCamelCase = DPRQuestionEncoderTokenizer lowerCAmelCase__ : Tuple = collections.namedtuple( '''DPRSpanPrediction''', ['''span_score''', '''relevance_score''', '''doc_id''', '''start_index''', '''end_index''', '''text'''] ) lowerCAmelCase__ : List[Any] = collections.namedtuple('''DPRReaderOutput''', ['''start_logits''', '''end_logits''', '''relevance_logits''']) lowerCAmelCase__ : int = r''' Return a dictionary with the token ids of the input strings and other information to give to `.decode_best_spans`. It converts the strings of a question and different passages (title and text) in a sequence of IDs (integers), using the tokenizer and vocabulary. The resulting `input_ids` is a matrix of size `(n_passages, sequence_length)` with the format: [CLS] <question token ids> [SEP] <titles ids> [SEP] <texts ids> Args: questions (`str` or `List[str]`): The questions to be encoded. You can specify one question for many passages. In this case, the question will be duplicated like `[questions] * n_passages`. Otherwise you have to specify as many questions as in `titles` or `texts`. titles (`str` or `List[str]`): The passages titles to be encoded. This can be a string or a list of strings if there are several passages. texts (`str` or `List[str]`): The passages texts to be encoded. This can be a string or a list of strings if there are several passages. padding (`bool`, `str` or [`~utils.PaddingStrategy`], *optional*, defaults to `False`): Activates and controls padding. Accepts the following values: - `True` or `\'longest\'`: Pad to the longest sequence in the batch (or no padding if only a single sequence if provided). - `\'max_length\'`: Pad to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. - `False` or `\'do_not_pad\'` (default): No padding (i.e., can output a batch with sequences of different lengths). truncation (`bool`, `str` or [`~tokenization_utils_base.TruncationStrategy`], *optional*, defaults to `False`): Activates and controls truncation. Accepts the following values: - `True` or `\'longest_first\'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. This will truncate token by token, removing a token from the longest sequence in the pair if a pair of sequences (or a batch of pairs) is provided. - `\'only_first\'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. This will only truncate the first sequence of a pair if a pair of sequences (or a batch of pairs) is provided. - `\'only_second\'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. This will only truncate the second sequence of a pair if a pair of sequences (or a batch of pairs) is provided. - `False` or `\'do_not_truncate\'` (default): No truncation (i.e., can output batch with sequence lengths greater than the model maximum admissible input size). max_length (`int`, *optional*): Controls the maximum length to use by one of the truncation/padding parameters. If left unset or set to `None`, this will use the predefined model maximum length if a maximum length is required by one of the truncation/padding parameters. If the model has no specific maximum input length (like XLNet) truncation/padding to a maximum length will be deactivated. return_tensors (`str` or [`~utils.TensorType`], *optional*): If set, will return tensors instead of list of python integers. Acceptable values are: - `\'tf\'`: Return TensorFlow `tf.constant` objects. - `\'pt\'`: Return PyTorch `torch.Tensor` objects. - `\'np\'`: Return Numpy `np.ndarray` objects. return_attention_mask (`bool`, *optional*): Whether or not to return the attention mask. If not set, will return the attention mask according to the specific tokenizer\'s default, defined by the `return_outputs` attribute. [What are attention masks?](../glossary#attention-mask) Return: `Dict[str, List[List[int]]]`: A dictionary with the following keys: - `input_ids`: List of token ids to be fed to a model. - `attention_mask`: List of indices specifying which tokens should be attended to by the model. ''' @add_start_docstrings(_lowerCamelCase ) class __snake_case : def __call__( self , __UpperCamelCase , __UpperCamelCase = None , __UpperCamelCase = None , __UpperCamelCase = False , __UpperCamelCase = False , __UpperCamelCase = None , __UpperCamelCase = None , __UpperCamelCase = None , **__UpperCamelCase , ) -> BatchEncoding: '''simple docstring''' if titles is None and texts is None: return super().__call__( __UpperCamelCase , padding=__UpperCamelCase , truncation=__UpperCamelCase , max_length=__UpperCamelCase , return_tensors=__UpperCamelCase , return_attention_mask=__UpperCamelCase , **__UpperCamelCase , ) elif titles is None or texts is None: snake_case__ : Optional[Any] = titles if texts is None else texts return super().__call__( __UpperCamelCase , __UpperCamelCase , padding=__UpperCamelCase , truncation=__UpperCamelCase , max_length=__UpperCamelCase , return_tensors=__UpperCamelCase , return_attention_mask=__UpperCamelCase , **__UpperCamelCase , ) snake_case__ : int = titles if not isinstance(__UpperCamelCase , __UpperCamelCase ) else [titles] snake_case__ : Optional[int] = texts if not isinstance(__UpperCamelCase , __UpperCamelCase ) else [texts] snake_case__ : List[Any] = len(__UpperCamelCase ) snake_case__ : str = questions if not isinstance(__UpperCamelCase , __UpperCamelCase ) else [questions] * n_passages assert len(__UpperCamelCase ) == len( __UpperCamelCase ), F"""There should be as many titles than texts but got {len(__UpperCamelCase )} titles and {len(__UpperCamelCase )} texts.""" snake_case__ : Optional[int] = super().__call__(__UpperCamelCase , __UpperCamelCase , padding=__UpperCamelCase , truncation=__UpperCamelCase )['input_ids'] snake_case__ : Optional[Any] = super().__call__(__UpperCamelCase , add_special_tokens=__UpperCamelCase , padding=__UpperCamelCase , truncation=__UpperCamelCase )['input_ids'] snake_case__ : Union[str, Any] = { 'input_ids': [ (encoded_question_and_title + encoded_text)[:max_length] if max_length is not None and truncation else encoded_question_and_title + encoded_text for encoded_question_and_title, encoded_text in zip(__UpperCamelCase , __UpperCamelCase ) ] } if return_attention_mask is not False: snake_case__ : List[Any] = [] for input_ids in encoded_inputs["input_ids"]: attention_mask.append([int(input_id != self.pad_token_id ) for input_id in input_ids] ) snake_case__ : Union[str, Any] = attention_mask return self.pad(__UpperCamelCase , padding=__UpperCamelCase , max_length=__UpperCamelCase , return_tensors=__UpperCamelCase ) def __a ( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase = 16 , __UpperCamelCase = 64 , __UpperCamelCase = 4 , ) -> List[DPRSpanPrediction]: '''simple docstring''' snake_case__ : Optional[Any] = reader_input['input_ids'] snake_case__ , snake_case__ , snake_case__ : Any = reader_output[:3] snake_case__ : List[str] = len(__UpperCamelCase ) snake_case__ : Tuple = sorted(range(__UpperCamelCase ) , reverse=__UpperCamelCase , key=relevance_logits.__getitem__ ) snake_case__ : List[DPRReaderOutput] = [] for doc_id in sorted_docs: snake_case__ : Tuple = list(input_ids[doc_id] ) # assuming question & title information is at the beginning of the sequence snake_case__ : int = sequence_ids.index(self.sep_token_id , 2 ) + 1 # second sep id if sequence_ids[-1] == self.pad_token_id: snake_case__ : Union[str, Any] = sequence_ids.index(self.pad_token_id ) else: snake_case__ : str = len(__UpperCamelCase ) snake_case__ : Dict = self._get_best_spans( start_logits=start_logits[doc_id][passage_offset:sequence_len] , end_logits=end_logits[doc_id][passage_offset:sequence_len] , max_answer_length=__UpperCamelCase , top_spans=__UpperCamelCase , ) for start_index, end_index in best_spans: start_index += passage_offset end_index += passage_offset nbest_spans_predictions.append( DPRSpanPrediction( span_score=start_logits[doc_id][start_index] + end_logits[doc_id][end_index] , relevance_score=relevance_logits[doc_id] , doc_id=__UpperCamelCase , start_index=__UpperCamelCase , end_index=__UpperCamelCase , text=self.decode(sequence_ids[start_index : end_index + 1] ) , ) ) if len(__UpperCamelCase ) >= num_spans: break return nbest_spans_predictions[:num_spans] def __a ( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , ) -> List[DPRSpanPrediction]: '''simple docstring''' snake_case__ : Any = [] for start_index, start_score in enumerate(__UpperCamelCase ): for answer_length, end_score in enumerate(end_logits[start_index : start_index + max_answer_length] ): scores.append(((start_index, start_index + answer_length), start_score + end_score) ) snake_case__ : str = sorted(__UpperCamelCase , key=lambda __UpperCamelCase : x[1] , reverse=__UpperCamelCase ) snake_case__ : Any = [] for (start_index, end_index), score in scores: assert start_index <= end_index, F"""Wrong span indices: [{start_index}:{end_index}]""" snake_case__ : str = end_index - start_index + 1 assert length <= max_answer_length, F"""Span is too long: {length} > {max_answer_length}""" if any( start_index <= prev_start_index <= prev_end_index <= end_index or prev_start_index <= start_index <= end_index <= prev_end_index for (prev_start_index, prev_end_index) in chosen_span_intervals ): continue chosen_span_intervals.append((start_index, end_index) ) if len(__UpperCamelCase ) == top_spans: break return chosen_span_intervals @add_end_docstrings(_lowerCamelCase ) class __snake_case ( _lowerCamelCase ,_lowerCamelCase ): __lowerCamelCase = VOCAB_FILES_NAMES __lowerCamelCase = READER_PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase = READER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase = READER_PRETRAINED_INIT_CONFIGURATION __lowerCamelCase = ["""input_ids""", """attention_mask"""] __lowerCamelCase = DPRReaderTokenizer
699
0
'''simple docstring''' import mpmath # for roots of unity import numpy as np class __snake_case : def __init__( self , __UpperCamelCase=None , __UpperCamelCase=None ) -> Tuple: '''simple docstring''' snake_case__ : Union[str, Any] = list(poly_a or [0] )[:] snake_case__ : Optional[Any] = list(poly_b or [0] )[:] # Remove leading zero coefficients while self.polyA[-1] == 0: self.polyA.pop() snake_case__ : Dict = len(self.polyA ) while self.polyB[-1] == 0: self.polyB.pop() snake_case__ : Dict = len(self.polyB ) # Add 0 to make lengths equal a power of 2 snake_case__ : List[Any] = int( 2 ** np.ceil(np.loga(len(self.polyA ) + len(self.polyB ) - 1 ) ) ) while len(self.polyA ) < self.c_max_length: self.polyA.append(0 ) while len(self.polyB ) < self.c_max_length: self.polyB.append(0 ) # A complex root used for the fourier transform snake_case__ : Tuple = complex(mpmath.root(x=1 , n=self.c_max_length , k=1 ) ) # The product snake_case__ : Tuple = self.__multiply() def __a ( self , __UpperCamelCase ) -> str: '''simple docstring''' snake_case__ : int = [[x] for x in self.polyA] if which == 'A' else [[x] for x in self.polyB] # Corner case if len(lowerCamelCase_ ) <= 1: return dft[0] # snake_case__ : str = self.c_max_length // 2 while next_ncol > 0: snake_case__ : Any = [[] for i in range(lowerCamelCase_ )] snake_case__ : Tuple = self.root**next_ncol # First half of next step snake_case__ : Tuple = 1 for j in range(self.c_max_length // (next_ncol * 2) ): for i in range(lowerCamelCase_ ): new_dft[i].append(dft[i][j] + current_root * dft[i + next_ncol][j] ) current_root *= root # Second half of next step snake_case__ : Optional[Any] = 1 for j in range(self.c_max_length // (next_ncol * 2) ): for i in range(lowerCamelCase_ ): new_dft[i].append(dft[i][j] - current_root * dft[i + next_ncol][j] ) current_root *= root # Update snake_case__ : Tuple = new_dft snake_case__ : List[str] = next_ncol // 2 return dft[0] def __a ( self ) -> str: '''simple docstring''' snake_case__ : Any = self.__dft('A' ) snake_case__ : Tuple = self.__dft('B' ) snake_case__ : Any = [[dft_a[i] * dft_b[i] for i in range(self.c_max_length )]] del dft_a del dft_b # Corner Case if len(inverce_c[0] ) <= 1: return inverce_c[0] # Inverse DFT snake_case__ : Dict = 2 while next_ncol <= self.c_max_length: snake_case__ : Optional[int] = [[] for i in range(lowerCamelCase_ )] snake_case__ : str = self.root ** (next_ncol // 2) snake_case__ : Optional[Any] = 1 # First half of next step for j in range(self.c_max_length // next_ncol ): for i in range(next_ncol // 2 ): # Even positions new_inverse_c[i].append( ( inverce_c[i][j] + inverce_c[i][j + self.c_max_length // next_ncol] ) / 2 ) # Odd positions new_inverse_c[i + next_ncol // 2].append( ( inverce_c[i][j] - inverce_c[i][j + self.c_max_length // next_ncol] ) / (2 * current_root) ) current_root *= root # Update snake_case__ : str = new_inverse_c next_ncol *= 2 # Unpack snake_case__ : Tuple = [round(x[0].real , 8 ) + round(x[0].imag , 8 ) * 1J for x in inverce_c] # Remove leading 0's while inverce_c[-1] == 0: inverce_c.pop() return inverce_c def __str__( self ) -> Optional[int]: '''simple docstring''' snake_case__ : Tuple = 'A = ' + ' + '.join( F"""{coef}*x^{i}""" for coef, i in enumerate(self.polyA[: self.len_A] ) ) snake_case__ : str = 'B = ' + ' + '.join( F"""{coef}*x^{i}""" for coef, i in enumerate(self.polyB[: self.len_B] ) ) snake_case__ : int = 'A*B = ' + ' + '.join( F"""{coef}*x^{i}""" for coef, i in enumerate(self.product ) ) return F"""{a}\n{b}\n{c}""" # Unit tests if __name__ == "__main__": import doctest doctest.testmod()
710
import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, EulerAncestralDiscreteScheduler, LMSDiscreteScheduler, PNDMScheduler, StableDiffusionInstructPixaPixPipeline, UNetaDConditionModel, ) from diffusers.image_processor import VaeImageProcessor from diffusers.utils import floats_tensor, load_image, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..pipeline_params import ( IMAGE_TO_IMAGE_IMAGE_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS, ) from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class __snake_case ( _lowerCamelCase ,_lowerCamelCase ,_lowerCamelCase ,unittest.TestCase ): __lowerCamelCase = StableDiffusionInstructPixaPixPipeline __lowerCamelCase = TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {"""height""", """width""", """cross_attention_kwargs"""} __lowerCamelCase = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS __lowerCamelCase = IMAGE_TO_IMAGE_IMAGE_PARAMS __lowerCamelCase = IMAGE_TO_IMAGE_IMAGE_PARAMS def __a ( self ) -> List[Any]: '''simple docstring''' torch.manual_seed(0 ) snake_case__ : Tuple = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=8 , out_channels=4 , down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D') , up_block_types=('CrossAttnUpBlock2D', 'UpBlock2D') , cross_attention_dim=32 , ) snake_case__ : Any = PNDMScheduler(skip_prk_steps=__UpperCamelCase ) torch.manual_seed(0 ) snake_case__ : Dict = 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 ) snake_case__ : int = 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 , ) snake_case__ : Tuple = CLIPTextModel(__UpperCamelCase ) snake_case__ : Optional[int] = CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip' ) snake_case__ : Optional[int] = { 'unet': unet, 'scheduler': scheduler, 'vae': vae, 'text_encoder': text_encoder, 'tokenizer': tokenizer, 'safety_checker': None, 'feature_extractor': None, } return components def __a ( self , __UpperCamelCase , __UpperCamelCase=0 ) -> Dict: '''simple docstring''' snake_case__ : Optional[Any] = floats_tensor((1, 3, 32, 32) , rng=random.Random(__UpperCamelCase ) ).to(__UpperCamelCase ) snake_case__ : int = image.cpu().permute(0 , 2 , 3 , 1 )[0] snake_case__ : Union[str, Any] = Image.fromarray(np.uinta(__UpperCamelCase ) ).convert('RGB' ) if str(__UpperCamelCase ).startswith('mps' ): snake_case__ : str = torch.manual_seed(__UpperCamelCase ) else: snake_case__ : Dict = torch.Generator(device=__UpperCamelCase ).manual_seed(__UpperCamelCase ) snake_case__ : str = { 'prompt': 'A painting of a squirrel eating a burger', 'image': image, 'generator': generator, 'num_inference_steps': 2, 'guidance_scale': 6.0, 'image_guidance_scale': 1, 'output_type': 'numpy', } return inputs def __a ( self ) -> Tuple: '''simple docstring''' snake_case__ : Dict = 'cpu' # ensure determinism for the device-dependent torch.Generator snake_case__ : Optional[int] = self.get_dummy_components() snake_case__ : str = StableDiffusionInstructPixaPixPipeline(**__UpperCamelCase ) snake_case__ : Optional[int] = sd_pipe.to(__UpperCamelCase ) sd_pipe.set_progress_bar_config(disable=__UpperCamelCase ) snake_case__ : Tuple = self.get_dummy_inputs(__UpperCamelCase ) snake_case__ : List[str] = sd_pipe(**__UpperCamelCase ).images snake_case__ : List[str] = image[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) snake_case__ : str = np.array([0.7_5_2_6, 0.3_7_5_0, 0.4_5_4_7, 0.6_1_1_7, 0.5_8_6_6, 0.5_0_1_6, 0.4_3_2_7, 0.5_6_4_2, 0.4_8_1_5] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 def __a ( self ) -> Any: '''simple docstring''' snake_case__ : Dict = 'cpu' # ensure determinism for the device-dependent torch.Generator snake_case__ : Union[str, Any] = self.get_dummy_components() snake_case__ : Optional[Any] = StableDiffusionInstructPixaPixPipeline(**__UpperCamelCase ) snake_case__ : List[Any] = sd_pipe.to(__UpperCamelCase ) sd_pipe.set_progress_bar_config(disable=__UpperCamelCase ) snake_case__ : Union[str, Any] = self.get_dummy_inputs(__UpperCamelCase ) snake_case__ : List[str] = 'french fries' snake_case__ : Optional[Any] = sd_pipe(**__UpperCamelCase , negative_prompt=__UpperCamelCase ) snake_case__ : Union[str, Any] = output.images snake_case__ : Tuple = image[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) snake_case__ : Any = np.array([0.7_5_1_1, 0.3_6_4_2, 0.4_5_5_3, 0.6_2_3_6, 0.5_7_9_7, 0.5_0_1_3, 0.4_3_4_3, 0.5_6_1_1, 0.4_8_3_1] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 def __a ( self ) -> int: '''simple docstring''' snake_case__ : Optional[int] = 'cpu' # ensure determinism for the device-dependent torch.Generator snake_case__ : List[str] = self.get_dummy_components() snake_case__ : str = StableDiffusionInstructPixaPixPipeline(**__UpperCamelCase ) snake_case__ : str = sd_pipe.to(__UpperCamelCase ) sd_pipe.set_progress_bar_config(disable=__UpperCamelCase ) snake_case__ : Dict = self.get_dummy_inputs(__UpperCamelCase ) snake_case__ : Any = [inputs['prompt']] * 2 snake_case__ : Optional[int] = np.array(inputs['image'] ).astype(np.floataa ) / 2_5_5.0 snake_case__ : Optional[int] = torch.from_numpy(__UpperCamelCase ).unsqueeze(0 ).to(__UpperCamelCase ) snake_case__ : Any = image / 2 + 0.5 snake_case__ : Optional[Any] = image.permute(0 , 3 , 1 , 2 ) snake_case__ : List[Any] = image.repeat(2 , 1 , 1 , 1 ) snake_case__ : Optional[int] = sd_pipe(**__UpperCamelCase ).images snake_case__ : Union[str, Any] = image[-1, -3:, -3:, -1] assert image.shape == (2, 32, 32, 3) snake_case__ : List[Any] = np.array([0.5_8_1_2, 0.5_7_4_8, 0.5_2_2_2, 0.5_9_0_8, 0.5_6_9_5, 0.7_1_7_4, 0.6_8_0_4, 0.5_5_2_3, 0.5_5_7_9] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 def __a ( self ) -> Union[str, Any]: '''simple docstring''' snake_case__ : Optional[int] = 'cpu' # ensure determinism for the device-dependent torch.Generator snake_case__ : Optional[int] = self.get_dummy_components() snake_case__ : Tuple = EulerAncestralDiscreteScheduler( beta_start=0.0_0_0_8_5 , beta_end=0.0_1_2 , beta_schedule='scaled_linear' ) snake_case__ : int = StableDiffusionInstructPixaPixPipeline(**__UpperCamelCase ) snake_case__ : List[str] = sd_pipe.to(__UpperCamelCase ) sd_pipe.set_progress_bar_config(disable=__UpperCamelCase ) snake_case__ : str = self.get_dummy_inputs(__UpperCamelCase ) snake_case__ : Any = sd_pipe(**__UpperCamelCase ).images snake_case__ : int = image[0, -3:, -3:, -1] snake_case__ : Tuple = [round(__UpperCamelCase , 4 ) for x in image_slice.flatten().tolist()] print(','.join([str(__UpperCamelCase ) for x in slice] ) ) assert image.shape == (1, 32, 32, 3) snake_case__ : List[Any] = np.array([0.7_4_1_7, 0.3_8_4_2, 0.4_7_3_2, 0.5_7_7_6, 0.5_8_9_1, 0.5_1_3_9, 0.4_0_5_2, 0.5_6_7_3, 0.4_9_8_6] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 def __a ( self ) -> int: '''simple docstring''' super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) def __a ( self ) -> Any: '''simple docstring''' snake_case__ : Optional[int] = self.get_dummy_components() snake_case__ : int = StableDiffusionInstructPixaPixPipeline(**__UpperCamelCase ) snake_case__ : Union[str, Any] = VaeImageProcessor(do_resize=__UpperCamelCase , do_normalize=__UpperCamelCase ) snake_case__ : Optional[int] = pipe.to(__UpperCamelCase ) pipe.set_progress_bar_config(disable=__UpperCamelCase ) snake_case__ : Optional[Any] = pipe(**self.get_dummy_inputs_by_type(__UpperCamelCase , input_image_type='pt' ) )[0] snake_case__ : Union[str, Any] = components['vae'] snake_case__ : str = self.get_dummy_inputs_by_type(__UpperCamelCase , input_image_type='pt' ) for image_param in self.image_latents_params: if image_param in inputs.keys(): snake_case__ : List[str] = vae.encode(inputs[image_param] ).latent_dist.mode() snake_case__ : Dict = pipe(**__UpperCamelCase )[0] snake_case__ : str = np.abs(out - out_latents_inputs ).max() self.assertLess(__UpperCamelCase , 1E-4 , 'passing latents as image input generate different result from passing image' ) @slow @require_torch_gpu class __snake_case ( unittest.TestCase ): def __a ( self ) -> List[str]: '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def __a ( self , __UpperCamelCase=0 ) -> Dict: '''simple docstring''' snake_case__ : Optional[Any] = torch.manual_seed(__UpperCamelCase ) snake_case__ : List[str] = load_image( 'https://huggingface.co/datasets/diffusers/test-arrays/resolve/main/stable_diffusion_pix2pix/example.jpg' ) snake_case__ : int = { 'prompt': 'turn him into a cyborg', 'image': image, 'generator': generator, 'num_inference_steps': 3, 'guidance_scale': 7.5, 'image_guidance_scale': 1.0, 'output_type': 'numpy', } return inputs def __a ( self ) -> Dict: '''simple docstring''' snake_case__ : Any = StableDiffusionInstructPixaPixPipeline.from_pretrained( 'timbrooks/instruct-pix2pix' , safety_checker=__UpperCamelCase ) pipe.to(__UpperCamelCase ) pipe.set_progress_bar_config(disable=__UpperCamelCase ) pipe.enable_attention_slicing() snake_case__ : Tuple = self.get_inputs() snake_case__ : List[Any] = pipe(**__UpperCamelCase ).images snake_case__ : Union[str, Any] = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 512, 512, 3) snake_case__ : Dict = np.array([0.5_9_0_2, 0.6_0_1_5, 0.6_0_2_7, 0.5_9_8_3, 0.6_0_9_2, 0.6_0_6_1, 0.5_7_6_5, 0.5_7_8_5, 0.5_5_5_5] ) assert np.abs(expected_slice - image_slice ).max() < 1E-3 def __a ( self ) -> str: '''simple docstring''' snake_case__ : Any = StableDiffusionInstructPixaPixPipeline.from_pretrained( 'timbrooks/instruct-pix2pix' , safety_checker=__UpperCamelCase ) snake_case__ : Tuple = LMSDiscreteScheduler.from_config(pipe.scheduler.config ) pipe.to(__UpperCamelCase ) pipe.set_progress_bar_config(disable=__UpperCamelCase ) pipe.enable_attention_slicing() snake_case__ : Dict = self.get_inputs() snake_case__ : Dict = pipe(**__UpperCamelCase ).images snake_case__ : Union[str, Any] = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 512, 512, 3) snake_case__ : List[Any] = np.array([0.6_5_7_8, 0.6_8_1_7, 0.6_9_7_2, 0.6_7_6_1, 0.6_8_5_6, 0.6_9_1_6, 0.6_4_2_8, 0.6_5_1_6, 0.6_3_0_1] ) assert np.abs(expected_slice - image_slice ).max() < 1E-3 def __a ( self ) -> Optional[Any]: '''simple docstring''' snake_case__ : Tuple = StableDiffusionInstructPixaPixPipeline.from_pretrained( 'timbrooks/instruct-pix2pix' , safety_checker=__UpperCamelCase ) snake_case__ : Tuple = DDIMScheduler.from_config(pipe.scheduler.config ) pipe.to(__UpperCamelCase ) pipe.set_progress_bar_config(disable=__UpperCamelCase ) pipe.enable_attention_slicing() snake_case__ : Optional[int] = self.get_inputs() snake_case__ : Optional[int] = pipe(**__UpperCamelCase ).images snake_case__ : Tuple = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 512, 512, 3) snake_case__ : int = np.array([0.3_8_2_8, 0.3_8_3_4, 0.3_8_1_8, 0.3_7_9_2, 0.3_8_6_5, 0.3_7_5_2, 0.3_7_9_2, 0.3_8_4_7, 0.3_7_5_3] ) assert np.abs(expected_slice - image_slice ).max() < 1E-3 def __a ( self ) -> Union[str, Any]: '''simple docstring''' snake_case__ : int = 0 def callback_fn(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> None: snake_case__ : List[Any] = True nonlocal number_of_steps number_of_steps += 1 if step == 1: snake_case__ : Any = latents.detach().cpu().numpy() assert latents.shape == (1, 4, 64, 64) snake_case__ : int = latents[0, -3:, -3:, -1] snake_case__ : List[str] = np.array([-0.2_4_6_3, -0.4_6_4_4, -0.9_7_5_6, 1.5_1_7_6, 1.4_4_1_4, 0.7_8_6_6, 0.9_8_9_7, 0.8_5_2_1, 0.7_9_8_3] ) assert np.abs(latents_slice.flatten() - expected_slice ).max() < 5E-2 elif step == 2: snake_case__ : Dict = latents.detach().cpu().numpy() assert latents.shape == (1, 4, 64, 64) snake_case__ : Dict = latents[0, -3:, -3:, -1] snake_case__ : Optional[Any] = np.array([-0.2_6_4_4, -0.4_6_2_6, -0.9_6_5_3, 1.5_1_7_6, 1.4_5_5_1, 0.7_6_8_6, 0.9_8_0_5, 0.8_4_5_2, 0.8_1_1_5] ) assert np.abs(latents_slice.flatten() - expected_slice ).max() < 5E-2 snake_case__ : str = False snake_case__ : List[Any] = StableDiffusionInstructPixaPixPipeline.from_pretrained( 'timbrooks/instruct-pix2pix' , safety_checker=__UpperCamelCase , torch_dtype=torch.floataa ) snake_case__ : int = pipe.to(__UpperCamelCase ) pipe.set_progress_bar_config(disable=__UpperCamelCase ) pipe.enable_attention_slicing() snake_case__ : int = self.get_inputs() pipe(**__UpperCamelCase , callback=__UpperCamelCase , callback_steps=1 ) assert callback_fn.has_been_called assert number_of_steps == 3 def __a ( self ) -> Any: '''simple docstring''' torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats() snake_case__ : str = StableDiffusionInstructPixaPixPipeline.from_pretrained( 'timbrooks/instruct-pix2pix' , safety_checker=__UpperCamelCase , torch_dtype=torch.floataa ) snake_case__ : Dict = pipe.to(__UpperCamelCase ) pipe.set_progress_bar_config(disable=__UpperCamelCase ) pipe.enable_attention_slicing(1 ) pipe.enable_sequential_cpu_offload() snake_case__ : str = self.get_inputs() snake_case__ : Tuple = pipe(**__UpperCamelCase ) snake_case__ : List[Any] = torch.cuda.max_memory_allocated() # make sure that less than 2.2 GB is allocated assert mem_bytes < 2.2 * 10**9 def __a ( self ) -> int: '''simple docstring''' snake_case__ : int = self.get_inputs() # resize to resolution that is divisible by 8 but not 16 or 32 snake_case__ : Tuple = inputs['image'].resize((504, 504) ) snake_case__ : str = 'timbrooks/instruct-pix2pix' snake_case__ : Tuple = StableDiffusionInstructPixaPixPipeline.from_pretrained( __UpperCamelCase , safety_checker=__UpperCamelCase , ) pipe.to(__UpperCamelCase ) pipe.set_progress_bar_config(disable=__UpperCamelCase ) pipe.enable_attention_slicing() snake_case__ : str = pipe(**__UpperCamelCase ) snake_case__ : List[Any] = output.images[0] snake_case__ : List[Any] = image[255:258, 383:386, -1] assert image.shape == (504, 504, 3) snake_case__ : List[str] = np.array([0.2_7_2_6, 0.2_5_2_9, 0.2_6_6_4, 0.2_6_5_5, 0.2_6_4_1, 0.2_6_4_2, 0.2_5_9_1, 0.2_6_4_9, 0.2_5_9_0] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5E-3
699
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCAmelCase__ : List[str] = { '''configuration_swinv2''': ['''SWINV2_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''Swinv2Config'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : Dict = [ '''SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST''', '''Swinv2ForImageClassification''', '''Swinv2ForMaskedImageModeling''', '''Swinv2Model''', '''Swinv2PreTrainedModel''', ] if TYPE_CHECKING: from .configuration_swinva import SWINV2_PRETRAINED_CONFIG_ARCHIVE_MAP, SwinvaConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_swinva import ( SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST, SwinvaForImageClassification, SwinvaForMaskedImageModeling, SwinvaModel, SwinvaPreTrainedModel, ) else: import sys lowerCAmelCase__ : Dict = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
711
from .data_collator import ( DataCollatorForLanguageModeling, DataCollatorForPermutationLanguageModeling, DataCollatorForSeqaSeq, DataCollatorForSOP, DataCollatorForTokenClassification, DataCollatorForWholeWordMask, DataCollatorWithPadding, DefaultDataCollator, default_data_collator, ) from .metrics import glue_compute_metrics, xnli_compute_metrics from .processors import ( DataProcessor, InputExample, InputFeatures, SingleSentenceClassificationProcessor, SquadExample, SquadFeatures, SquadVaProcessor, SquadVaProcessor, glue_convert_examples_to_features, glue_output_modes, glue_processors, glue_tasks_num_labels, squad_convert_examples_to_features, xnli_output_modes, xnli_processors, xnli_tasks_num_labels, )
699
0
import argparse from collections import defaultdict def UpperCamelCase__ ( A__ , A__ , A__ , A__ , A__ ) -> Tuple: snake_case__ : Optional[int] = F"""{file}_{class_name}_{test_name}""" done_test[_id] += 1 with open(__lowerCAmelCase , 'r' ) as f: snake_case__ : Dict = f.readlines() snake_case__ : Any = F"""class {class_name}(""" snake_case__ : Optional[Any] = F"""{4 * ' '}def {test_name}(""" snake_case__ : Tuple = F"""{8 * ' '}{correct_line.split()[0]}""" snake_case__ : Optional[int] = F"""{16 * ' '}{correct_line.split()[0]}""" snake_case__ : Optional[int] = False snake_case__ : Optional[int] = False snake_case__ : List[Any] = False snake_case__ : Optional[int] = False snake_case__ : Any = 0 snake_case__ : Any = 0 snake_case__ : List[str] = [] for line in lines: if line.startswith(__lowerCAmelCase ): snake_case__ : Union[str, Any] = True elif in_class and line.startswith(__lowerCAmelCase ): snake_case__ : Union[str, Any] = True elif in_class and in_func and (line.startswith(__lowerCAmelCase ) or line.startswith(__lowerCAmelCase )): snake_case__ : Union[str, Any] = len(line.split(correct_line.split()[0] )[0] ) count += 1 if count == done_test[_id]: snake_case__ : Tuple = True if in_class and in_func and in_line: if ")" not in line: continue else: snake_case__ : Optional[Any] = True if in_class and in_func and in_line and insert_line: new_lines.append(F"""{spaces * ' '}{correct_line}""" ) snake_case__ : Union[str, Any] = False else: new_lines.append(__lowerCAmelCase ) with open(__lowerCAmelCase , 'w' ) as f: for line in new_lines: f.write(__lowerCAmelCase ) def UpperCamelCase__ ( A__ , A__=None ) -> Optional[int]: if fail is not None: with open(__lowerCAmelCase , 'r' ) as f: snake_case__ : int = {l.strip() for l in f.readlines()} else: snake_case__ : str = None with open(__lowerCAmelCase , 'r' ) as f: snake_case__ : List[str] = f.readlines() snake_case__ : str = defaultdict(__lowerCAmelCase ) for line in correct_lines: snake_case__ , snake_case__ , snake_case__ , snake_case__ : Optional[int] = line.split(';' ) if test_failures is None or "::".join([file, class_name, test_name] ) in test_failures: overwrite_file(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) if __name__ == "__main__": lowerCAmelCase__ : int = argparse.ArgumentParser() parser.add_argument('''--correct_filename''', help='''filename of tests with expected result''') parser.add_argument('''--fail_filename''', help='''filename of test failures''', type=str, default=None) lowerCAmelCase__ : Optional[int] = parser.parse_args() main(args.correct_filename, args.fail_filename)
712
from dataclasses import dataclass, field from typing import Optional from transformers import AutoConfig, AutoImageProcessor, AutoTokenizer, FlaxVisionEncoderDecoderModel, HfArgumentParser @dataclass class __snake_case : __lowerCamelCase = field( metadata={"""help""": """The output directory where the model will be written."""} ,) __lowerCamelCase = field( metadata={ """help""": ( """The encoder model checkpoint for weights initialization.""" """Don't set if you want to train an encoder model from scratch.""" ) } ,) __lowerCamelCase = field( metadata={ """help""": ( """The decoder model checkpoint for weights initialization.""" """Don't set if you want to train a decoder model from scratch.""" ) } ,) __lowerCamelCase = field( default=_lowerCamelCase ,metadata={"""help""": """Pretrained encoder config name or path if not the same as encoder_model_name"""} ) __lowerCamelCase = field( default=_lowerCamelCase ,metadata={"""help""": """Pretrained decoder config name or path if not the same as decoder_model_name"""} ) def UpperCamelCase__ ( ) -> Union[str, Any]: snake_case__ : str = HfArgumentParser((ModelArguments,) ) ((snake_case__) , ) : Dict = parser.parse_args_into_dataclasses() # Load pretrained model and tokenizer # Use explicit specified encoder config if model_args.encoder_config_name: snake_case__ : List[str] = AutoConfig.from_pretrained(model_args.encoder_config_name ) # Use pretrained encoder model's config else: snake_case__ : Optional[int] = AutoConfig.from_pretrained(model_args.encoder_model_name_or_path ) # Use explicit specified decoder config if model_args.decoder_config_name: snake_case__ : Optional[Any] = AutoConfig.from_pretrained(model_args.decoder_config_name ) # Use pretrained decoder model's config else: snake_case__ : List[str] = AutoConfig.from_pretrained(model_args.decoder_model_name_or_path ) # necessary for `from_encoder_decoder_pretrained` when `decoder_config` is passed snake_case__ : Any = True snake_case__ : Dict = True snake_case__ : Tuple = FlaxVisionEncoderDecoderModel.from_encoder_decoder_pretrained( encoder_pretrained_model_name_or_path=model_args.encoder_model_name_or_path , decoder_pretrained_model_name_or_path=model_args.decoder_model_name_or_path , encoder_config=A__ , decoder_config=A__ , ) # GPT2 only has bos/eos tokens but not decoder_start/pad tokens snake_case__ : Optional[Any] = decoder_config.decoder_start_token_id snake_case__ : Tuple = decoder_config.pad_token_id if decoder_start_token_id is None: snake_case__ : Optional[Any] = decoder_config.bos_token_id if pad_token_id is None: snake_case__ : int = decoder_config.eos_token_id # This is necessary to make Flax's generate() work snake_case__ : Union[str, Any] = decoder_config.eos_token_id snake_case__ : Optional[int] = decoder_start_token_id snake_case__ : int = pad_token_id snake_case__ : Tuple = AutoImageProcessor.from_pretrained(model_args.encoder_model_name_or_path ) snake_case__ : int = AutoTokenizer.from_pretrained(model_args.decoder_model_name_or_path ) snake_case__ : Tuple = tokenizer.convert_ids_to_tokens(model.config.pad_token_id ) model.save_pretrained(model_args.output_dir ) image_processor.save_pretrained(model_args.output_dir ) tokenizer.save_pretrained(model_args.output_dir ) if __name__ == "__main__": main()
699
0
import argparse import json import os import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType from accelerate.utils.deepspeed import DummyOptim, DummyScheduler lowerCAmelCase__ : Optional[int] = 16 lowerCAmelCase__ : Optional[Any] = 32 def UpperCamelCase__ ( A__ , A__ = 16 , A__ = "bert-base-cased" ) -> Any: snake_case__ : Union[str, Any] = AutoTokenizer.from_pretrained(a_ ) snake_case__ : Optional[int] = load_dataset('glue' , 'mrpc' ) def tokenize_function(A__ ): # max_length=None => use the model max length (it's actually the default) snake_case__ : Tuple = tokenizer(examples['sentence1'] , examples['sentence2'] , truncation=a_ , max_length=a_ ) return outputs # Apply the method we just defined to all the examples in all the splits of the dataset snake_case__ : Optional[int] = datasets.map( a_ , batched=a_ , remove_columns=['idx', 'sentence1', 'sentence2'] , load_from_cache_file=a_ ) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library snake_case__ : Any = tokenized_datasets.rename_column('label' , 'labels' ) def collate_fn(A__ ): # On TPU it's best to pad everything to the same length or training will be very slow. if accelerator.distributed_type == DistributedType.TPU: return tokenizer.pad(a_ , padding='max_length' , max_length=128 , return_tensors='pt' ) return tokenizer.pad(a_ , padding='longest' , return_tensors='pt' ) # Instantiate dataloaders. snake_case__ : int = DataLoader( tokenized_datasets['train'] , shuffle=a_ , collate_fn=a_ , batch_size=a_ ) snake_case__ : int = DataLoader( tokenized_datasets['validation'] , shuffle=a_ , collate_fn=a_ , batch_size=a_ ) return train_dataloader, eval_dataloader def UpperCamelCase__ ( A__ , A__ ) -> List[Any]: # Initialize accelerator snake_case__ : str = Accelerator() # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs snake_case__ : Optional[int] = config['''lr'''] snake_case__ : Optional[int] = int(config['num_epochs'] ) snake_case__ : Dict = int(config['seed'] ) snake_case__ : str = int(config['batch_size'] ) snake_case__ : List[str] = args.model_name_or_path set_seed(a_ ) snake_case__ : List[Any] = get_dataloaders(a_ , a_ , a_ ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) snake_case__ : List[str] = AutoModelForSequenceClassification.from_pretrained(a_ , return_dict=a_ ) # Instantiate optimizer snake_case__ : Tuple = ( AdamW if accelerator.state.deepspeed_plugin is None or '''optimizer''' not in accelerator.state.deepspeed_plugin.deepspeed_config else DummyOptim ) snake_case__ : Any = optimizer_cls(params=model.parameters() , lr=a_ ) if accelerator.state.deepspeed_plugin is not None: snake_case__ : int = accelerator.state.deepspeed_plugin.deepspeed_config[ '''gradient_accumulation_steps''' ] else: snake_case__ : Dict = 1 snake_case__ : List[Any] = (len(a_ ) * num_epochs) // gradient_accumulation_steps # Instantiate scheduler if ( accelerator.state.deepspeed_plugin is None or "scheduler" not in accelerator.state.deepspeed_plugin.deepspeed_config ): snake_case__ : Optional[int] = get_linear_schedule_with_warmup( optimizer=a_ , num_warmup_steps=0 , num_training_steps=a_ , ) else: snake_case__ : Optional[int] = DummyScheduler(a_ , total_num_steps=a_ , warmup_num_steps=0 ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. snake_case__ : Tuple = accelerator.prepare( a_ , a_ , a_ , a_ , a_ ) # We need to keep track of how many total steps we have iterated over snake_case__ : Tuple = 0 # We also need to keep track of the stating epoch so files are named properly snake_case__ : Tuple = 0 # Now we train the model snake_case__ : Tuple = evaluate.load('glue' , 'mrpc' ) snake_case__ : Optional[Any] = 0 snake_case__ : Tuple = {} for epoch in range(a_ , a_ ): model.train() for step, batch in enumerate(a_ ): snake_case__ : Any = model(**a_ ) snake_case__ : int = outputs.loss snake_case__ : List[Any] = loss / gradient_accumulation_steps accelerator.backward(a_ ) if step % gradient_accumulation_steps == 0: optimizer.step() lr_scheduler.step() optimizer.zero_grad() overall_step += 1 model.eval() snake_case__ : str = 0 for step, batch in enumerate(a_ ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): snake_case__ : int = model(**a_ ) snake_case__ : List[Any] = outputs.logits.argmax(dim=-1 ) # It is slightly faster to call this once, than multiple times snake_case__ : Optional[Any] = accelerator.gather( (predictions, batch['labels']) ) # If we are in a multiprocess environment, the last batch has duplicates if accelerator.use_distributed: if step == len(a_ ) - 1: snake_case__ : Any = predictions[: len(eval_dataloader.dataset ) - samples_seen] snake_case__ : Any = references[: len(eval_dataloader.dataset ) - samples_seen] else: samples_seen += references.shape[0] metric.add_batch( predictions=a_ , references=a_ , ) snake_case__ : Tuple = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(F"""epoch {epoch}:""" , a_ ) snake_case__ : int = eval_metric['''accuracy'''] if best_performance < eval_metric["accuracy"]: snake_case__ : Dict = eval_metric['''accuracy'''] if args.performance_lower_bound is not None: assert ( args.performance_lower_bound <= best_performance ), F"""Best performance metric {best_performance} is lower than the lower bound {args.performance_lower_bound}""" accelerator.wait_for_everyone() if accelerator.is_main_process: with open(os.path.join(args.output_dir , 'all_results.json' ) , 'w' ) as f: json.dump(a_ , a_ ) def UpperCamelCase__ ( ) -> Union[str, Any]: snake_case__ : str = argparse.ArgumentParser(description='Simple example of training script tracking peak GPU memory usage.' ) parser.add_argument( '--model_name_or_path' , type=a_ , default='bert-base-cased' , help='Path to pretrained model or model identifier from huggingface.co/models.' , required=a_ , ) parser.add_argument( '--output_dir' , type=a_ , default='.' , help='Optional save directory where all checkpoint folders will be stored. Default is the current working directory.' , ) parser.add_argument( '--performance_lower_bound' , type=a_ , default=a_ , help='Optional lower bound for the performance metric. If set, the training will throw error when the performance metric drops below this value.' , ) parser.add_argument( '--num_epochs' , type=a_ , default=3 , help='Number of train epochs.' , ) snake_case__ : Optional[Any] = parser.parse_args() snake_case__ : Optional[int] = {'''lr''': 2e-5, '''num_epochs''': args.num_epochs, '''seed''': 42, '''batch_size''': 16} training_function(a_ , a_ ) if __name__ == "__main__": main()
713
import logging import os from dataclasses import dataclass, field from typing import Dict, Optional import datasets import numpy as np import tensorflow as tf from transformers import ( AutoConfig, AutoTokenizer, EvalPrediction, HfArgumentParser, PreTrainedTokenizer, TFAutoModelForSequenceClassification, TFTrainer, TFTrainingArguments, ) from transformers.utils import logging as hf_logging hf_logging.set_verbosity_info() hf_logging.enable_default_handler() hf_logging.enable_explicit_format() def UpperCamelCase__ ( A__ , A__ , A__ , A__ , A__ , A__ = None , ) -> Optional[int]: snake_case__ : List[str] = {} if train_file is not None: snake_case__ : Tuple = [train_file] if eval_file is not None: snake_case__ : Dict = [eval_file] if test_file is not None: snake_case__ : str = [test_file] snake_case__ : Optional[Any] = datasets.load_dataset('csv' , data_files=A__ ) snake_case__ : Any = list(ds[list(files.keys() )[0]].features.keys() ) snake_case__ : Optional[Any] = features_name.pop(A__ ) snake_case__ : Optional[Any] = list(set(ds[list(files.keys() )[0]][label_name] ) ) snake_case__ : str = {label: i for i, label in enumerate(A__ )} snake_case__ : int = tokenizer.model_input_names snake_case__ : int = {} if len(A__ ) == 1: for k in files.keys(): snake_case__ : str = ds[k].map( lambda A__ : tokenizer.batch_encode_plus( example[features_name[0]] , truncation=A__ , max_length=A__ , padding='max_length' ) , batched=A__ , ) elif len(A__ ) == 2: for k in files.keys(): snake_case__ : Optional[int] = ds[k].map( lambda A__ : tokenizer.batch_encode_plus( (example[features_name[0]], example[features_name[1]]) , truncation=A__ , max_length=A__ , padding='max_length' , ) , batched=A__ , ) def gen_train(): for ex in transformed_ds[datasets.Split.TRAIN]: snake_case__ : int = {k: v for k, v in ex.items() if k in input_names} snake_case__ : Any = labelaid[ex[label_name]] yield (d, label) def gen_val(): for ex in transformed_ds[datasets.Split.VALIDATION]: snake_case__ : int = {k: v for k, v in ex.items() if k in input_names} snake_case__ : Union[str, Any] = labelaid[ex[label_name]] yield (d, label) def gen_test(): for ex in transformed_ds[datasets.Split.TEST]: snake_case__ : Dict = {k: v for k, v in ex.items() if k in input_names} snake_case__ : List[str] = labelaid[ex[label_name]] yield (d, label) snake_case__ : Any = ( tf.data.Dataset.from_generator( A__ , ({k: tf.intaa for k in input_names}, tf.intaa) , ({k: tf.TensorShape([None] ) for k in input_names}, tf.TensorShape([] )) , ) if datasets.Split.TRAIN in transformed_ds else None ) if train_ds is not None: snake_case__ : str = train_ds.apply(tf.data.experimental.assert_cardinality(len(ds[datasets.Split.TRAIN] ) ) ) snake_case__ : Optional[int] = ( tf.data.Dataset.from_generator( A__ , ({k: tf.intaa for k in input_names}, tf.intaa) , ({k: tf.TensorShape([None] ) for k in input_names}, tf.TensorShape([] )) , ) if datasets.Split.VALIDATION in transformed_ds else None ) if val_ds is not None: snake_case__ : Optional[int] = val_ds.apply(tf.data.experimental.assert_cardinality(len(ds[datasets.Split.VALIDATION] ) ) ) snake_case__ : List[str] = ( tf.data.Dataset.from_generator( A__ , ({k: tf.intaa for k in input_names}, tf.intaa) , ({k: tf.TensorShape([None] ) for k in input_names}, tf.TensorShape([] )) , ) if datasets.Split.TEST in transformed_ds else None ) if test_ds is not None: snake_case__ : str = test_ds.apply(tf.data.experimental.assert_cardinality(len(ds[datasets.Split.TEST] ) ) ) return train_ds, val_ds, test_ds, labelaid lowerCAmelCase__ : List[str] = logging.getLogger(__name__) @dataclass class __snake_case : __lowerCamelCase = field(metadata={"""help""": """Which column contains the label"""} ) __lowerCamelCase = field(default=_lowerCamelCase ,metadata={"""help""": """The path of the training file"""} ) __lowerCamelCase = field(default=_lowerCamelCase ,metadata={"""help""": """The path of the development file"""} ) __lowerCamelCase = field(default=_lowerCamelCase ,metadata={"""help""": """The path of the test file"""} ) __lowerCamelCase = field( default=128 ,metadata={ """help""": ( """The maximum total input sequence length after tokenization. Sequences longer """ """than this will be truncated, sequences shorter will be padded.""" ) } ,) __lowerCamelCase = field( default=_lowerCamelCase ,metadata={"""help""": """Overwrite the cached training and evaluation sets"""} ) @dataclass class __snake_case : __lowerCamelCase = field( metadata={"""help""": """Path to pretrained model or model identifier from huggingface.co/models"""} ) __lowerCamelCase = field( default=_lowerCamelCase ,metadata={"""help""": """Pretrained config name or path if not the same as model_name"""} ) __lowerCamelCase = field( default=_lowerCamelCase ,metadata={"""help""": """Pretrained tokenizer name or path if not the same as model_name"""} ) __lowerCamelCase = field(default=_lowerCamelCase ,metadata={"""help""": """Set this flag to use fast tokenization."""} ) # If you want to tweak more attributes on your tokenizer, you should do it in a distinct script, # or just modify its tokenizer_config.json. __lowerCamelCase = field( default=_lowerCamelCase ,metadata={"""help""": """Where do you want to store the pretrained models downloaded from huggingface.co"""} ,) def UpperCamelCase__ ( ) -> Union[str, Any]: # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. snake_case__ : int = HfArgumentParser((ModelArguments, DataTrainingArguments, TFTrainingArguments) ) snake_case__ , snake_case__ , snake_case__ : Dict = parser.parse_args_into_dataclasses() if ( os.path.exists(training_args.output_dir ) and os.listdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir ): raise ValueError( F"""Output directory ({training_args.output_dir}) already exists and is not empty. Use""" ' --overwrite_output_dir to overcome.' ) # Setup logging logging.basicConfig( format='%(asctime)s - %(levelname)s - %(name)s - %(message)s' , datefmt='%m/%d/%Y %H:%M:%S' , level=logging.INFO , ) logger.info( F"""n_replicas: {training_args.n_replicas}, distributed training: {bool(training_args.n_replicas > 1 )}, """ F"""16-bits training: {training_args.fpaa}""" ) logger.info(F"""Training/evaluation parameters {training_args}""" ) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. snake_case__ : Dict = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , ) snake_case__ , snake_case__ , snake_case__ , snake_case__ : Dict = get_tfds( train_file=data_args.train_file , eval_file=data_args.dev_file , test_file=data_args.test_file , tokenizer=A__ , label_column_id=data_args.label_column_id , max_seq_length=data_args.max_seq_length , ) snake_case__ : Dict = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , num_labels=len(A__ ) , labelaid=A__ , idalabel={id: label for label, id in labelaid.items()} , finetuning_task='text-classification' , cache_dir=model_args.cache_dir , ) with training_args.strategy.scope(): snake_case__ : Optional[Any] = TFAutoModelForSequenceClassification.from_pretrained( model_args.model_name_or_path , from_pt=bool('.bin' in model_args.model_name_or_path ) , config=A__ , cache_dir=model_args.cache_dir , ) def compute_metrics(A__ ) -> Dict: snake_case__ : Optional[Any] = np.argmax(p.predictions , axis=1 ) return {"acc": (preds == p.label_ids).mean()} # Initialize our Trainer snake_case__ : Any = TFTrainer( model=A__ , args=A__ , train_dataset=A__ , eval_dataset=A__ , compute_metrics=A__ , ) # Training if training_args.do_train: trainer.train() trainer.save_model() tokenizer.save_pretrained(training_args.output_dir ) # Evaluation snake_case__ : Dict = {} if training_args.do_eval: logger.info('*** Evaluate ***' ) snake_case__ : Tuple = trainer.evaluate() snake_case__ : Any = os.path.join(training_args.output_dir , 'eval_results.txt' ) with open(A__ , 'w' ) as writer: logger.info('***** Eval results *****' ) for key, value in result.items(): logger.info(F""" {key} = {value}""" ) writer.write(F"""{key} = {value}\n""" ) results.update(A__ ) return results if __name__ == "__main__": main()
699
0
import argparse import os import jax as jnp import numpy as onp import torch import torch.nn as nn from music_spectrogram_diffusion import inference from tax import checkpoints from diffusers import DDPMScheduler, OnnxRuntimeModel, SpectrogramDiffusionPipeline from diffusers.pipelines.spectrogram_diffusion import SpectrogramContEncoder, SpectrogramNotesEncoder, TaFilmDecoder lowerCAmelCase__ : Optional[Any] = '''base_with_context''' def UpperCamelCase__ ( A__ , A__ ) -> str: snake_case__ : List[Any] = nn.Parameter(torch.FloatTensor(weights['token_embedder']['embedding'] ) ) snake_case__ : Tuple = nn.Parameter( torch.FloatTensor(weights['Embed_0']['embedding'] ) , requires_grad=UpperCAmelCase__ ) for lyr_num, lyr in enumerate(model.encoders ): snake_case__ : List[Any] = weights[F"""layers_{lyr_num}"""] snake_case__ : Dict = nn.Parameter( torch.FloatTensor(ly_weight['pre_attention_layer_norm']['scale'] ) ) snake_case__ : Optional[Any] = ly_weight['attention'] snake_case__ : List[str] = nn.Parameter(torch.FloatTensor(attention_weights['query']['kernel'].T ) ) snake_case__ : Optional[Any] = nn.Parameter(torch.FloatTensor(attention_weights['key']['kernel'].T ) ) snake_case__ : int = nn.Parameter(torch.FloatTensor(attention_weights['value']['kernel'].T ) ) snake_case__ : Tuple = nn.Parameter(torch.FloatTensor(attention_weights['out']['kernel'].T ) ) snake_case__ : Union[str, Any] = nn.Parameter(torch.FloatTensor(ly_weight['pre_mlp_layer_norm']['scale'] ) ) snake_case__ : int = nn.Parameter(torch.FloatTensor(ly_weight['mlp']['wi_0']['kernel'].T ) ) snake_case__ : Tuple = nn.Parameter(torch.FloatTensor(ly_weight['mlp']['wi_1']['kernel'].T ) ) snake_case__ : Any = nn.Parameter(torch.FloatTensor(ly_weight['mlp']['wo']['kernel'].T ) ) snake_case__ : Any = nn.Parameter(torch.FloatTensor(weights['encoder_norm']['scale'] ) ) return model def UpperCamelCase__ ( A__ , A__ ) -> Optional[int]: snake_case__ : Dict = nn.Parameter(torch.FloatTensor(weights['input_proj']['kernel'].T ) ) snake_case__ : List[Any] = nn.Parameter( torch.FloatTensor(weights['Embed_0']['embedding'] ) , requires_grad=UpperCAmelCase__ ) for lyr_num, lyr in enumerate(model.encoders ): snake_case__ : Any = weights[F"""layers_{lyr_num}"""] snake_case__ : List[Any] = ly_weight['attention'] snake_case__ : List[str] = nn.Parameter(torch.FloatTensor(attention_weights['query']['kernel'].T ) ) snake_case__ : Any = nn.Parameter(torch.FloatTensor(attention_weights['key']['kernel'].T ) ) snake_case__ : str = nn.Parameter(torch.FloatTensor(attention_weights['value']['kernel'].T ) ) snake_case__ : List[Any] = nn.Parameter(torch.FloatTensor(attention_weights['out']['kernel'].T ) ) snake_case__ : Optional[Any] = nn.Parameter( torch.FloatTensor(ly_weight['pre_attention_layer_norm']['scale'] ) ) snake_case__ : Any = nn.Parameter(torch.FloatTensor(ly_weight['mlp']['wi_0']['kernel'].T ) ) snake_case__ : Optional[Any] = nn.Parameter(torch.FloatTensor(ly_weight['mlp']['wi_1']['kernel'].T ) ) snake_case__ : Any = nn.Parameter(torch.FloatTensor(ly_weight['mlp']['wo']['kernel'].T ) ) snake_case__ : Optional[int] = nn.Parameter(torch.FloatTensor(ly_weight['pre_mlp_layer_norm']['scale'] ) ) snake_case__ : Union[str, Any] = nn.Parameter(torch.FloatTensor(weights['encoder_norm']['scale'] ) ) return model def UpperCamelCase__ ( A__ , A__ ) -> Any: snake_case__ : Dict = nn.Parameter(torch.FloatTensor(weights['time_emb_dense0']['kernel'].T ) ) snake_case__ : List[str] = nn.Parameter(torch.FloatTensor(weights['time_emb_dense1']['kernel'].T ) ) snake_case__ : Any = nn.Parameter( torch.FloatTensor(weights['Embed_0']['embedding'] ) , requires_grad=UpperCAmelCase__ ) snake_case__ : Union[str, Any] = nn.Parameter( torch.FloatTensor(weights['continuous_inputs_projection']['kernel'].T ) ) for lyr_num, lyr in enumerate(model.decoders ): snake_case__ : Optional[Any] = weights[F"""layers_{lyr_num}"""] snake_case__ : Tuple = nn.Parameter( torch.FloatTensor(ly_weight['pre_self_attention_layer_norm']['scale'] ) ) snake_case__ : Union[str, Any] = nn.Parameter( torch.FloatTensor(ly_weight['FiLMLayer_0']['DenseGeneral_0']['kernel'].T ) ) snake_case__ : Any = ly_weight['self_attention'] snake_case__ : Tuple = nn.Parameter(torch.FloatTensor(attention_weights['query']['kernel'].T ) ) snake_case__ : Any = nn.Parameter(torch.FloatTensor(attention_weights['key']['kernel'].T ) ) snake_case__ : Union[str, Any] = nn.Parameter(torch.FloatTensor(attention_weights['value']['kernel'].T ) ) snake_case__ : int = nn.Parameter(torch.FloatTensor(attention_weights['out']['kernel'].T ) ) snake_case__ : Tuple = ly_weight['MultiHeadDotProductAttention_0'] snake_case__ : Tuple = nn.Parameter(torch.FloatTensor(attention_weights['query']['kernel'].T ) ) snake_case__ : List[str] = nn.Parameter(torch.FloatTensor(attention_weights['key']['kernel'].T ) ) snake_case__ : Tuple = nn.Parameter(torch.FloatTensor(attention_weights['value']['kernel'].T ) ) snake_case__ : Tuple = nn.Parameter(torch.FloatTensor(attention_weights['out']['kernel'].T ) ) snake_case__ : int = nn.Parameter( torch.FloatTensor(ly_weight['pre_cross_attention_layer_norm']['scale'] ) ) snake_case__ : Dict = nn.Parameter(torch.FloatTensor(ly_weight['pre_mlp_layer_norm']['scale'] ) ) snake_case__ : List[Any] = nn.Parameter( torch.FloatTensor(ly_weight['FiLMLayer_1']['DenseGeneral_0']['kernel'].T ) ) snake_case__ : List[str] = nn.Parameter(torch.FloatTensor(ly_weight['mlp']['wi_0']['kernel'].T ) ) snake_case__ : Optional[int] = nn.Parameter(torch.FloatTensor(ly_weight['mlp']['wi_1']['kernel'].T ) ) snake_case__ : Any = nn.Parameter(torch.FloatTensor(ly_weight['mlp']['wo']['kernel'].T ) ) snake_case__ : Dict = nn.Parameter(torch.FloatTensor(weights['decoder_norm']['scale'] ) ) snake_case__ : Any = nn.Parameter(torch.FloatTensor(weights['spec_out_dense']['kernel'].T ) ) return model def UpperCamelCase__ ( A__ ) -> str: snake_case__ : List[Any] = checkpoints.load_tax_checkpoint(args.checkpoint_path ) snake_case__ : int = jnp.tree_util.tree_map(onp.array , UpperCAmelCase__ ) snake_case__ : List[Any] = [ 'from __gin__ import dynamic_registration', 'from music_spectrogram_diffusion.models.diffusion import diffusion_utils', 'diffusion_utils.ClassifierFreeGuidanceConfig.eval_condition_weight = 2.0', 'diffusion_utils.DiffusionConfig.classifier_free_guidance = @diffusion_utils.ClassifierFreeGuidanceConfig()', ] snake_case__ : Dict = os.path.join(args.checkpoint_path , '..' , 'config.gin' ) snake_case__ : int = inference.parse_training_gin_file(UpperCAmelCase__ , UpperCAmelCase__ ) snake_case__ : Any = inference.InferenceModel(args.checkpoint_path , UpperCAmelCase__ ) snake_case__ : int = DDPMScheduler(beta_schedule='squaredcos_cap_v2' , variance_type='fixed_large' ) snake_case__ : Any = SpectrogramNotesEncoder( max_length=synth_model.sequence_length['inputs'] , vocab_size=synth_model.model.module.config.vocab_size , d_model=synth_model.model.module.config.emb_dim , dropout_rate=synth_model.model.module.config.dropout_rate , num_layers=synth_model.model.module.config.num_encoder_layers , num_heads=synth_model.model.module.config.num_heads , d_kv=synth_model.model.module.config.head_dim , d_ff=synth_model.model.module.config.mlp_dim , feed_forward_proj='gated-gelu' , ) snake_case__ : List[str] = SpectrogramContEncoder( input_dims=synth_model.audio_codec.n_dims , targets_context_length=synth_model.sequence_length['targets_context'] , d_model=synth_model.model.module.config.emb_dim , dropout_rate=synth_model.model.module.config.dropout_rate , num_layers=synth_model.model.module.config.num_encoder_layers , num_heads=synth_model.model.module.config.num_heads , d_kv=synth_model.model.module.config.head_dim , d_ff=synth_model.model.module.config.mlp_dim , feed_forward_proj='gated-gelu' , ) snake_case__ : Any = TaFilmDecoder( input_dims=synth_model.audio_codec.n_dims , targets_length=synth_model.sequence_length['targets_context'] , max_decoder_noise_time=synth_model.model.module.config.max_decoder_noise_time , d_model=synth_model.model.module.config.emb_dim , num_layers=synth_model.model.module.config.num_decoder_layers , num_heads=synth_model.model.module.config.num_heads , d_kv=synth_model.model.module.config.head_dim , d_ff=synth_model.model.module.config.mlp_dim , dropout_rate=synth_model.model.module.config.dropout_rate , ) snake_case__ : Optional[Any] = load_notes_encoder(ta_checkpoint['target']['token_encoder'] , UpperCAmelCase__ ) snake_case__ : Any = load_continuous_encoder(ta_checkpoint['target']['continuous_encoder'] , UpperCAmelCase__ ) snake_case__ : Optional[Any] = load_decoder(ta_checkpoint['target']['decoder'] , UpperCAmelCase__ ) snake_case__ : Tuple = OnnxRuntimeModel.from_pretrained('kashif/soundstream_mel_decoder' ) snake_case__ : List[str] = SpectrogramDiffusionPipeline( notes_encoder=UpperCAmelCase__ , continuous_encoder=UpperCAmelCase__ , decoder=UpperCAmelCase__ , scheduler=UpperCAmelCase__ , melgan=UpperCAmelCase__ , ) if args.save: pipe.save_pretrained(args.output_path ) if __name__ == "__main__": lowerCAmelCase__ : Union[str, Any] = argparse.ArgumentParser() parser.add_argument('''--output_path''', default=None, type=str, required=True, help='''Path to the converted model.''') parser.add_argument( '''--save''', default=True, type=bool, required=False, help='''Whether to save the converted model or not.''' ) parser.add_argument( '''--checkpoint_path''', default=F'''{MODEL}/checkpoint_500000''', type=str, required=False, help='''Path to the original jax model checkpoint.''', ) lowerCAmelCase__ : Any = parser.parse_args() main(args)
714
from typing import List import datasets from datasets.tasks import AudioClassification from ..folder_based_builder import folder_based_builder lowerCAmelCase__ : List[Any] = datasets.utils.logging.get_logger(__name__) class __snake_case ( folder_based_builder.FolderBasedBuilderConfig ): __lowerCamelCase = None __lowerCamelCase = None class __snake_case ( folder_based_builder.FolderBasedBuilder ): __lowerCamelCase = datasets.Audio() __lowerCamelCase = """audio""" __lowerCamelCase = AudioFolderConfig __lowerCamelCase = 42 # definition at the bottom of the script __lowerCamelCase = AudioClassification(audio_column="""audio""" ,label_column="""label""" ) lowerCAmelCase__ : Tuple = [ '''.aiff''', '''.au''', '''.avr''', '''.caf''', '''.flac''', '''.htk''', '''.svx''', '''.mat4''', '''.mat5''', '''.mpc2k''', '''.ogg''', '''.paf''', '''.pvf''', '''.raw''', '''.rf64''', '''.sd2''', '''.sds''', '''.ircam''', '''.voc''', '''.w64''', '''.wav''', '''.nist''', '''.wavex''', '''.wve''', '''.xi''', '''.mp3''', '''.opus''', ] lowerCAmelCase__ : List[Any] = AUDIO_EXTENSIONS
699
0
import argparse import torch from transformers import ( SpeechTaConfig, SpeechTaFeatureExtractor, SpeechTaForSpeechToSpeech, SpeechTaForSpeechToText, SpeechTaForTextToSpeech, SpeechTaProcessor, SpeechTaTokenizer, logging, ) from transformers.tokenization_utils import AddedToken logging.set_verbosity_info() lowerCAmelCase__ : List[Any] = logging.get_logger('''transformers.models.speecht5''') lowerCAmelCase__ : List[str] = { '''speech_encoder_prenet.layer_norm''': '''speecht5.encoder.prenet.feature_projection.layer_norm''', '''speech_encoder_prenet.post_extract_proj''': '''speecht5.encoder.prenet.feature_projection.projection''', '''speech_encoder_prenet.pos_conv.0''': '''speecht5.encoder.prenet.pos_conv_embed.conv''', '''speech_encoder_prenet.mask_emb''': '''speecht5.encoder.prenet.masked_spec_embed''', } lowerCAmelCase__ : Dict = { '''text_encoder_prenet.encoder_prenet.0''': '''speecht5.encoder.prenet.embed_tokens''', '''text_encoder_prenet.encoder_prenet.1.alpha''': '''speecht5.encoder.prenet.encode_positions.alpha''', } lowerCAmelCase__ : str = { '''speech_decoder_prenet.decoder_prenet.0.0.prenet.0.0''': '''speecht5.decoder.prenet.layers.0''', '''speech_decoder_prenet.decoder_prenet.0.0.prenet.1.0''': '''speecht5.decoder.prenet.layers.1''', '''speech_decoder_prenet.decoder_prenet.0.1''': '''speecht5.decoder.prenet.final_layer''', '''speech_decoder_prenet.decoder_prenet.1.alpha''': '''speecht5.decoder.prenet.encode_positions.alpha''', '''speech_decoder_prenet.spkembs_layer.0''': '''speecht5.decoder.prenet.speaker_embeds_layer''', } lowerCAmelCase__ : Optional[int] = { '''speech_decoder_postnet.feat_out''': '''speech_decoder_postnet.feat_out''', '''speech_decoder_postnet.prob_out''': '''speech_decoder_postnet.prob_out''', '''speech_decoder_postnet.postnet.postnet.0.0''': '''speech_decoder_postnet.layers.0.conv''', '''speech_decoder_postnet.postnet.postnet.0.1''': '''speech_decoder_postnet.layers.0.batch_norm''', '''speech_decoder_postnet.postnet.postnet.1.0''': '''speech_decoder_postnet.layers.1.conv''', '''speech_decoder_postnet.postnet.postnet.1.1''': '''speech_decoder_postnet.layers.1.batch_norm''', '''speech_decoder_postnet.postnet.postnet.2.0''': '''speech_decoder_postnet.layers.2.conv''', '''speech_decoder_postnet.postnet.postnet.2.1''': '''speech_decoder_postnet.layers.2.batch_norm''', '''speech_decoder_postnet.postnet.postnet.3.0''': '''speech_decoder_postnet.layers.3.conv''', '''speech_decoder_postnet.postnet.postnet.3.1''': '''speech_decoder_postnet.layers.3.batch_norm''', '''speech_decoder_postnet.postnet.postnet.4.0''': '''speech_decoder_postnet.layers.4.conv''', '''speech_decoder_postnet.postnet.postnet.4.1''': '''speech_decoder_postnet.layers.4.batch_norm''', } lowerCAmelCase__ : Optional[Any] = { '''text_decoder_prenet.embed_tokens''': '''speecht5.decoder.prenet.embed_tokens''', } lowerCAmelCase__ : List[str] = { '''text_decoder_postnet.output_projection''': '''text_decoder_postnet.lm_head''', } lowerCAmelCase__ : Tuple = { '''encoder.layers.*.self_attn.k_proj''': '''speecht5.encoder.wrapped_encoder.layers.*.attention.k_proj''', '''encoder.layers.*.self_attn.v_proj''': '''speecht5.encoder.wrapped_encoder.layers.*.attention.v_proj''', '''encoder.layers.*.self_attn.q_proj''': '''speecht5.encoder.wrapped_encoder.layers.*.attention.q_proj''', '''encoder.layers.*.self_attn.out_proj''': '''speecht5.encoder.wrapped_encoder.layers.*.attention.out_proj''', '''encoder.layers.*.self_attn_layer_norm''': '''speecht5.encoder.wrapped_encoder.layers.*.layer_norm''', '''encoder.layers.*.fc1''': '''speecht5.encoder.wrapped_encoder.layers.*.feed_forward.intermediate_dense''', '''encoder.layers.*.fc2''': '''speecht5.encoder.wrapped_encoder.layers.*.feed_forward.output_dense''', '''encoder.layers.*.final_layer_norm''': '''speecht5.encoder.wrapped_encoder.layers.*.final_layer_norm''', '''encoder.layer_norm''': '''speecht5.encoder.wrapped_encoder.layer_norm''', '''encoder.pos_emb.pe_k''': '''speecht5.encoder.wrapped_encoder.embed_positions.pe_k''', } lowerCAmelCase__ : List[str] = { '''decoder.layers.*.self_attn.k_proj''': '''speecht5.decoder.wrapped_decoder.layers.*.self_attn.k_proj''', '''decoder.layers.*.self_attn.v_proj''': '''speecht5.decoder.wrapped_decoder.layers.*.self_attn.v_proj''', '''decoder.layers.*.self_attn.q_proj''': '''speecht5.decoder.wrapped_decoder.layers.*.self_attn.q_proj''', '''decoder.layers.*.self_attn.out_proj''': '''speecht5.decoder.wrapped_decoder.layers.*.self_attn.out_proj''', '''decoder.layers.*.self_attn_layer_norm''': '''speecht5.decoder.wrapped_decoder.layers.*.self_attn_layer_norm''', '''decoder.layers.*.encoder_attn.k_proj''': '''speecht5.decoder.wrapped_decoder.layers.*.encoder_attn.k_proj''', '''decoder.layers.*.encoder_attn.v_proj''': '''speecht5.decoder.wrapped_decoder.layers.*.encoder_attn.v_proj''', '''decoder.layers.*.encoder_attn.q_proj''': '''speecht5.decoder.wrapped_decoder.layers.*.encoder_attn.q_proj''', '''decoder.layers.*.encoder_attn.out_proj''': '''speecht5.decoder.wrapped_decoder.layers.*.encoder_attn.out_proj''', '''decoder.layers.*.encoder_attn_layer_norm''': '''speecht5.decoder.wrapped_decoder.layers.*.encoder_attn_layer_norm''', '''decoder.layers.*.fc1''': '''speecht5.decoder.wrapped_decoder.layers.*.feed_forward.intermediate_dense''', '''decoder.layers.*.fc2''': '''speecht5.decoder.wrapped_decoder.layers.*.feed_forward.output_dense''', '''decoder.layers.*.final_layer_norm''': '''speecht5.decoder.wrapped_decoder.layers.*.final_layer_norm''', } lowerCAmelCase__ : int = { **MAPPING_SPEECH_ENCODER_PRENET, **MAPPING_ENCODER, **MAPPING_DECODER, **MAPPING_TEXT_DECODER_PRENET, **MAPPING_TEXT_DECODER_POSTNET, } lowerCAmelCase__ : Union[str, Any] = { **MAPPING_TEXT_ENCODER_PRENET, **MAPPING_ENCODER, **MAPPING_DECODER, **MAPPING_SPEECH_DECODER_PRENET, **MAPPING_SPEECH_DECODER_POSTNET, } lowerCAmelCase__ : Tuple = { **MAPPING_SPEECH_ENCODER_PRENET, **MAPPING_ENCODER, **MAPPING_DECODER, **MAPPING_SPEECH_DECODER_PRENET, **MAPPING_SPEECH_DECODER_POSTNET, } lowerCAmelCase__ : Optional[Any] = [] lowerCAmelCase__ : Dict = [ '''encoder.version''', '''encoder.layers.*.norm_k.weight''', '''encoder.layers.*.norm_k.bias''', '''decoder.version''', '''decoder.layers.*.norm_k.weight''', '''decoder.layers.*.norm_k.bias''', '''decoder.pos_emb.pe_k''', '''speech_encoder_prenet.embed_positions._float_tensor''', '''text_decoder_prenet.embed_positions._float_tensor''', ] lowerCAmelCase__ : int = IGNORE_KEYS + [ '''encoder.proj''', '''text_encoder_prenet.*''', '''speech_decoder_prenet.*''', '''speech_decoder_postnet.*''', ] lowerCAmelCase__ : str = IGNORE_KEYS + [ '''encoder.proj''', '''speech_encoder_prenet.*''', '''text_decoder_prenet.*''', '''text_decoder_postnet.*''', ] lowerCAmelCase__ : str = IGNORE_KEYS + [ '''encoder.proj''', '''text_encoder_prenet.*''', '''text_decoder_prenet.*''', '''text_decoder_postnet.*''', ] def UpperCamelCase__ ( A__ , A__ , A__ , A__ , A__ ) -> List[Any]: for attribute in key.split('.' ): snake_case__ : Union[str, Any] = getattr(UpperCAmelCase__ , UpperCAmelCase__ ) if weight_type is not None: snake_case__ : Optional[Any] = getattr(UpperCAmelCase__ , UpperCAmelCase__ ).shape else: snake_case__ : List[str] = hf_pointer.shape if hf_shape != value.shape: raise ValueError( F"""Shape of hf {key + '.' + weight_type if weight_type is not None else ''} is {hf_shape}, but should be""" F""" {value.shape} for {full_name}""" ) if weight_type == "weight": snake_case__ : List[Any] = value elif weight_type == "weight_g": snake_case__ : List[str] = value elif weight_type == "weight_v": snake_case__ : Any = value elif weight_type == "bias": snake_case__ : int = value elif weight_type == "running_mean": snake_case__ : Optional[int] = value elif weight_type == "running_var": snake_case__ : Dict = value elif weight_type == "num_batches_tracked": snake_case__ : Dict = value else: snake_case__ : Tuple = value logger.info(F"""{key + ('.' + weight_type if weight_type is not None else '')} was initialized from {full_name}.""" ) def UpperCamelCase__ ( A__ , A__ ) -> Union[str, Any]: for key in ignore_keys: if key.endswith('.*' ): if name.startswith(key[:-1] ): return True elif ".*." in key: snake_case__ , snake_case__ : Dict = key.split('.*.' ) if prefix in name and suffix in name: return True elif key in name: return True return False def UpperCamelCase__ ( A__ , A__ , A__ ) -> Union[str, Any]: snake_case__ : List[Any] = [] if task == "s2t": snake_case__ : Tuple = hf_model.speechta.encoder.prenet.feature_encoder snake_case__ : Union[str, Any] = MAPPING_S2T snake_case__ : Optional[int] = IGNORE_KEYS_S2T elif task == "t2s": snake_case__ : int = None snake_case__ : Union[str, Any] = MAPPING_T2S snake_case__ : Optional[int] = IGNORE_KEYS_T2S elif task == "s2s": snake_case__ : Union[str, Any] = hf_model.speechta.encoder.prenet.feature_encoder snake_case__ : Union[str, Any] = MAPPING_S2S snake_case__ : Tuple = IGNORE_KEYS_S2S else: raise ValueError(F"""Unsupported task: {task}""" ) for name, value in fairseq_dict.items(): if should_ignore(UpperCAmelCase__ , UpperCAmelCase__ ): logger.info(F"""{name} was ignored""" ) continue snake_case__ : List[Any] = False if "conv_layers" in name: load_conv_layer( UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , hf_model.config.feat_extract_norm == 'group' , ) snake_case__ : List[Any] = True else: for key, mapped_key in MAPPING.items(): # mapped_key = "speecht5." + mapped_key if mapped_key not in TOP_LEVEL_KEYS else mapped_key if "*" in key: snake_case__ , snake_case__ : str = key.split('.*.' ) if prefix in name and suffix in name: snake_case__ : Union[str, Any] = suffix # if key in name or key.split("w2v_model.")[-1] == name.split(".")[0]: if key in name: snake_case__ : Optional[Any] = True if "*" in mapped_key: snake_case__ : Dict = name.split(UpperCAmelCase__ )[0].split('.' )[-2] snake_case__ : str = mapped_key.replace('*' , UpperCAmelCase__ ) if "weight_g" in name: snake_case__ : int = 'weight_g' elif "weight_v" in name: snake_case__ : Dict = 'weight_v' elif "bias" in name: snake_case__ : str = 'bias' elif "weight" in name: snake_case__ : Optional[Any] = 'weight' elif "running_mean" in name: snake_case__ : Tuple = 'running_mean' elif "running_var" in name: snake_case__ : Tuple = 'running_var' elif "num_batches_tracked" in name: snake_case__ : Dict = 'num_batches_tracked' else: snake_case__ : Any = None set_recursively(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ) continue if not is_used: unused_weights.append(UpperCAmelCase__ ) logger.warning(F"""Unused weights: {unused_weights}""" ) def UpperCamelCase__ ( A__ , A__ , A__ , A__ , A__ ) -> Union[str, Any]: snake_case__ : Tuple = full_name.split('conv_layers.' )[-1] snake_case__ : List[str] = name.split('.' ) snake_case__ : List[Any] = int(items[0] ) snake_case__ : Optional[int] = int(items[1] ) if type_id == 0: if "bias" in name: if value.shape != feature_extractor.conv_layers[layer_id].conv.bias.data.shape: raise ValueError( F"""{full_name} has size {value.shape}, but""" F""" {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found.""" ) snake_case__ : Union[str, Any] = value logger.info(F"""Feat extract conv layer {layer_id} was initialized from {full_name}.""" ) elif "weight" in name: if value.shape != feature_extractor.conv_layers[layer_id].conv.weight.data.shape: raise ValueError( F"""{full_name} has size {value.shape}, but""" F""" {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found.""" ) snake_case__ : List[Any] = value logger.info(F"""Feat extract conv layer {layer_id} was initialized from {full_name}.""" ) elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm): if "bias" in name: if value.shape != feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape: raise ValueError( F"""{full_name} has size {value.shape}, but""" F""" {feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape} was found.""" ) snake_case__ : Tuple = value logger.info(F"""Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.""" ) elif "weight" in name: if value.shape != feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape: raise ValueError( F"""{full_name} has size {value.shape}, but""" F""" {feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape} was found.""" ) snake_case__ : Union[str, Any] = value logger.info(F"""Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.""" ) else: unused_weights.append(UpperCAmelCase__ ) @torch.no_grad() def UpperCamelCase__ ( A__ , A__ , A__ , A__=None , A__=None , A__=None , ) -> Dict: if config_path is not None: snake_case__ : Dict = SpeechTaConfig.from_pretrained(UpperCAmelCase__ ) else: snake_case__ : Union[str, Any] = SpeechTaConfig() if task == "s2t": snake_case__ : int = config.max_text_positions snake_case__ : Tuple = SpeechTaForSpeechToText(UpperCAmelCase__ ) elif task == "t2s": snake_case__ : Union[str, Any] = 1876 snake_case__ : int = 600 snake_case__ : Optional[Any] = config.max_speech_positions snake_case__ : List[str] = SpeechTaForTextToSpeech(UpperCAmelCase__ ) elif task == "s2s": snake_case__ : Optional[int] = 1876 snake_case__ : Any = config.max_speech_positions snake_case__ : Optional[int] = SpeechTaForSpeechToSpeech(UpperCAmelCase__ ) else: raise ValueError(F"""Unknown task name: {task}""" ) if vocab_path: snake_case__ : Optional[Any] = SpeechTaTokenizer(UpperCAmelCase__ , model_max_length=config.max_text_positions ) # Mask token behaves like a normal word, i.e. include the space before it snake_case__ : str = AddedToken('<mask>' , lstrip=UpperCAmelCase__ , rstrip=UpperCAmelCase__ ) snake_case__ : Optional[int] = mask_token tokenizer.add_special_tokens({'mask_token': mask_token} ) tokenizer.add_tokens(['<ctc_blank>'] ) snake_case__ : str = SpeechTaFeatureExtractor() snake_case__ : Any = SpeechTaProcessor(tokenizer=UpperCAmelCase__ , feature_extractor=UpperCAmelCase__ ) processor.save_pretrained(UpperCAmelCase__ ) snake_case__ : Union[str, Any] = torch.load(UpperCAmelCase__ ) recursively_load_weights(fairseq_checkpoint['model'] , UpperCAmelCase__ , UpperCAmelCase__ ) model.save_pretrained(UpperCAmelCase__ ) if repo_id: print('Pushing to the hub...' ) processor.push_to_hub(UpperCAmelCase__ ) model.push_to_hub(UpperCAmelCase__ ) if __name__ == "__main__": lowerCAmelCase__ : str = argparse.ArgumentParser() parser.add_argument( '''--task''', default='''s2t''', type=str, help='''Type of the SpeechT5 model you\'d like to convert. Should be one of \'s2t\', \'t2s\', \'s2s\'.''', ) parser.add_argument('''--checkpoint_path''', required=True, default=None, type=str, help='''Path to fairseq checkpoint''') parser.add_argument('''--vocab_path''', default=None, type=str, help='''Path to SentencePiece model''') parser.add_argument('''--config_path''', default=None, type=str, help='''Path to hf config.json of model to convert''') parser.add_argument( '''--pytorch_dump_folder_path''', required=True, default=None, type=str, help='''Path to the output PyTorch model.''' ) parser.add_argument( '''--push_to_hub''', default=None, type=str, help='''Where to upload the converted model on the 🤗 hub.''' ) lowerCAmelCase__ : Union[str, Any] = parser.parse_args() convert_speechta_checkpoint( args.task, args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.vocab_path, args.push_to_hub, )
715
import random import unittest import torch from diffusers import IFInpaintingPipeline from diffusers.utils import floats_tensor from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import skip_mps, torch_device from ..pipeline_params import ( TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS, ) from ..test_pipelines_common import PipelineTesterMixin from . import IFPipelineTesterMixin @skip_mps class __snake_case ( _lowerCamelCase ,_lowerCamelCase ,unittest.TestCase ): __lowerCamelCase = IFInpaintingPipeline __lowerCamelCase = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {"""width""", """height"""} __lowerCamelCase = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS __lowerCamelCase = PipelineTesterMixin.required_optional_params - {"""latents"""} def __a ( self ) -> Optional[Any]: '''simple docstring''' return self._get_dummy_components() def __a ( self , __UpperCamelCase , __UpperCamelCase=0 ) -> str: '''simple docstring''' if str(__UpperCamelCase ).startswith('mps' ): snake_case__ : int = torch.manual_seed(__UpperCamelCase ) else: snake_case__ : Union[str, Any] = torch.Generator(device=__UpperCamelCase ).manual_seed(__UpperCamelCase ) snake_case__ : Optional[int] = floats_tensor((1, 3, 32, 32) , rng=random.Random(__UpperCamelCase ) ).to(__UpperCamelCase ) snake_case__ : Dict = floats_tensor((1, 3, 32, 32) , rng=random.Random(__UpperCamelCase ) ).to(__UpperCamelCase ) snake_case__ : Optional[Any] = { 'prompt': 'A painting of a squirrel eating a burger', 'image': image, 'mask_image': mask_image, 'generator': generator, 'num_inference_steps': 2, 'output_type': 'numpy', } return inputs @unittest.skipIf( torch_device != 'cuda' or not is_xformers_available() , reason='XFormers attention is only available with CUDA and `xformers` installed' , ) def __a ( self ) -> List[Any]: '''simple docstring''' self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3 ) def __a ( self ) -> Optional[int]: '''simple docstring''' self._test_save_load_optional_components() @unittest.skipIf(torch_device != 'cuda' , reason='float16 requires CUDA' ) def __a ( self ) -> List[str]: '''simple docstring''' super().test_save_load_floataa(expected_max_diff=1E-1 ) def __a ( self ) -> List[str]: '''simple docstring''' self._test_attention_slicing_forward_pass(expected_max_diff=1E-2 ) def __a ( self ) -> int: '''simple docstring''' self._test_save_load_local() def __a ( self ) -> List[str]: '''simple docstring''' self._test_inference_batch_single_identical( expected_max_diff=1E-2 , )
699
0
import copy import os from typing import Union from ...configuration_utils import PretrainedConfig from ...models.auto.modeling_auto import MODEL_FOR_CAUSAL_LM_MAPPING_NAMES from ...utils import logging from ..auto import CONFIG_MAPPING lowerCAmelCase__ : Optional[Any] = logging.get_logger(__name__) lowerCAmelCase__ : List[str] = { '''salesforce/blip2-opt-2.7b''': '''https://huggingface.co/salesforce/blip2-opt-2.7b/resolve/main/config.json''', } class __snake_case ( _lowerCamelCase ): __lowerCamelCase = """blip_2_vision_model""" def __init__( self , __UpperCamelCase=1408 , __UpperCamelCase=6144 , __UpperCamelCase=39 , __UpperCamelCase=16 , __UpperCamelCase=224 , __UpperCamelCase=14 , __UpperCamelCase="gelu" , __UpperCamelCase=0.0_0_0_0_1 , __UpperCamelCase=0.0 , __UpperCamelCase=1E-10 , __UpperCamelCase=True , **__UpperCamelCase , ) -> List[Any]: '''simple docstring''' super().__init__(**__UpperCamelCase ) snake_case__ : Dict = hidden_size snake_case__ : str = intermediate_size snake_case__ : List[Any] = num_hidden_layers snake_case__ : Optional[int] = num_attention_heads snake_case__ : Optional[Any] = patch_size snake_case__ : str = image_size snake_case__ : Optional[Any] = initializer_range snake_case__ : Tuple = attention_dropout snake_case__ : Tuple = layer_norm_eps snake_case__ : Optional[Any] = hidden_act snake_case__ : Any = qkv_bias @classmethod def __a ( cls , __UpperCamelCase , **__UpperCamelCase ) -> int: '''simple docstring''' cls._set_token_in_kwargs(__UpperCamelCase ) snake_case__ , snake_case__ : Any = cls.get_config_dict(__UpperCamelCase , **__UpperCamelCase ) # get the vision config dict if we are loading from Blip2Config if config_dict.get('model_type' ) == "blip-2": snake_case__ : 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(__UpperCamelCase , **__UpperCamelCase ) class __snake_case ( _lowerCamelCase ): __lowerCamelCase = """blip_2_qformer""" def __init__( self , __UpperCamelCase=30522 , __UpperCamelCase=768 , __UpperCamelCase=12 , __UpperCamelCase=12 , __UpperCamelCase=3072 , __UpperCamelCase="gelu" , __UpperCamelCase=0.1 , __UpperCamelCase=0.1 , __UpperCamelCase=512 , __UpperCamelCase=0.0_2 , __UpperCamelCase=1E-12 , __UpperCamelCase=0 , __UpperCamelCase="absolute" , __UpperCamelCase=2 , __UpperCamelCase=1408 , **__UpperCamelCase , ) -> Union[str, Any]: '''simple docstring''' super().__init__(pad_token_id=__UpperCamelCase , **__UpperCamelCase ) snake_case__ : Tuple = vocab_size snake_case__ : List[Any] = hidden_size snake_case__ : Optional[int] = num_hidden_layers snake_case__ : Dict = num_attention_heads snake_case__ : List[str] = hidden_act snake_case__ : int = intermediate_size snake_case__ : Any = hidden_dropout_prob snake_case__ : List[str] = attention_probs_dropout_prob snake_case__ : Optional[int] = max_position_embeddings snake_case__ : Union[str, Any] = initializer_range snake_case__ : List[Any] = layer_norm_eps snake_case__ : Any = position_embedding_type snake_case__ : str = cross_attention_frequency snake_case__ : Tuple = encoder_hidden_size @classmethod def __a ( cls , __UpperCamelCase , **__UpperCamelCase ) -> str: '''simple docstring''' cls._set_token_in_kwargs(__UpperCamelCase ) snake_case__ , snake_case__ : Any = cls.get_config_dict(__UpperCamelCase , **__UpperCamelCase ) # get the qformer config dict if we are loading from Blip2Config if config_dict.get('model_type' ) == "blip-2": snake_case__ : Optional[int] = config_dict['qformer_config'] if "model_type" in config_dict and hasattr(cls , 'model_type' ) and config_dict["model_type"] != cls.model_type: logger.warning( F"""You are using a model of type {config_dict['model_type']} to instantiate a model of type """ F"""{cls.model_type}. This is not supported for all configurations of models and can yield errors.""" ) return cls.from_dict(__UpperCamelCase , **__UpperCamelCase ) class __snake_case ( _lowerCamelCase ): __lowerCamelCase = """blip-2""" __lowerCamelCase = True def __init__( self , __UpperCamelCase=None , __UpperCamelCase=None , __UpperCamelCase=None , __UpperCamelCase=32 , **__UpperCamelCase ) -> str: '''simple docstring''' super().__init__(**__UpperCamelCase ) if vision_config is None: snake_case__ : List[Any] = {} logger.info('vision_config is None. initializing the Blip2VisionConfig with default values.' ) if qformer_config is None: snake_case__ : List[str] = {} logger.info('qformer_config is None. Initializing the Blip2QFormerConfig with default values.' ) if text_config is None: snake_case__ : int = {} logger.info('text_config is None. Initializing the text config with default values (`OPTConfig`).' ) snake_case__ : Optional[Any] = BlipaVisionConfig(**__UpperCamelCase ) snake_case__ : List[str] = BlipaQFormerConfig(**__UpperCamelCase ) snake_case__ : Dict = text_config['model_type'] if 'model_type' in text_config else 'opt' snake_case__ : Tuple = CONFIG_MAPPING[text_model_type](**__UpperCamelCase ) snake_case__ : str = self.text_config.tie_word_embeddings snake_case__ : Union[str, Any] = self.text_config.is_encoder_decoder snake_case__ : str = num_query_tokens snake_case__ : Any = self.vision_config.hidden_size snake_case__ : int = self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES snake_case__ : Optional[int] = 1.0 snake_case__ : Union[str, Any] = 0.0_2 @classmethod def __a ( cls , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , **__UpperCamelCase , ) -> int: '''simple docstring''' return cls( vision_config=vision_config.to_dict() , qformer_config=qformer_config.to_dict() , text_config=text_config.to_dict() , **__UpperCamelCase , ) def __a ( self ) -> Optional[Any]: '''simple docstring''' snake_case__ : Union[str, Any] = copy.deepcopy(self.__dict__ ) snake_case__ : Union[str, Any] = self.vision_config.to_dict() snake_case__ : Tuple = self.qformer_config.to_dict() snake_case__ : Optional[Any] = self.text_config.to_dict() snake_case__ : List[str] = self.__class__.model_type return output
716
import unittest from transformers import BertGenerationTokenizer from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_torch, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin lowerCAmelCase__ : List[Any] = '''▁''' lowerCAmelCase__ : int = get_tests_dir('''fixtures/test_sentencepiece.model''') @require_sentencepiece class __snake_case ( _lowerCamelCase ,unittest.TestCase ): __lowerCamelCase = BertGenerationTokenizer __lowerCamelCase = False __lowerCamelCase = True def __a ( self ) -> Optional[int]: '''simple docstring''' super().setUp() snake_case__ : str = BertGenerationTokenizer(__UpperCamelCase , keep_accents=__UpperCamelCase ) tokenizer.save_pretrained(self.tmpdirname ) def __a ( self ) -> Optional[int]: '''simple docstring''' snake_case__ : List[str] = '<s>' snake_case__ : Dict = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(__UpperCamelCase ) , __UpperCamelCase ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(__UpperCamelCase ) , __UpperCamelCase ) def __a ( self ) -> List[str]: '''simple docstring''' snake_case__ : Tuple = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '<unk>' ) self.assertEqual(vocab_keys[1] , '<s>' ) self.assertEqual(vocab_keys[-1] , '<pad>' ) self.assertEqual(len(__UpperCamelCase ) , 1002 ) def __a ( self ) -> int: '''simple docstring''' self.assertEqual(self.get_tokenizer().vocab_size , 1000 ) def __a ( self ) -> Tuple: '''simple docstring''' snake_case__ : Optional[Any] = BertGenerationTokenizer(__UpperCamelCase , keep_accents=__UpperCamelCase ) snake_case__ : int = tokenizer.tokenize('This is a test' ) self.assertListEqual(__UpperCamelCase , ['▁This', '▁is', '▁a', '▁t', 'est'] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(__UpperCamelCase ) , [285, 46, 10, 170, 382] , ) snake_case__ : Any = tokenizer.tokenize('I was born in 92000, and this is falsé.' ) self.assertListEqual( __UpperCamelCase , [ SPIECE_UNDERLINE + 'I', SPIECE_UNDERLINE + 'was', SPIECE_UNDERLINE + 'b', 'or', 'n', SPIECE_UNDERLINE + 'in', SPIECE_UNDERLINE + '', '9', '2', '0', '0', '0', ',', SPIECE_UNDERLINE + 'and', SPIECE_UNDERLINE + 'this', SPIECE_UNDERLINE + 'is', SPIECE_UNDERLINE + 'f', 'al', 's', 'é', '.', ] , ) snake_case__ : Optional[Any] = tokenizer.convert_tokens_to_ids(__UpperCamelCase ) self.assertListEqual( __UpperCamelCase , [8, 21, 84, 55, 24, 19, 7, 0, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 0, 4] , ) snake_case__ : int = tokenizer.convert_ids_to_tokens(__UpperCamelCase ) self.assertListEqual( __UpperCamelCase , [ SPIECE_UNDERLINE + 'I', SPIECE_UNDERLINE + 'was', SPIECE_UNDERLINE + 'b', 'or', 'n', SPIECE_UNDERLINE + 'in', SPIECE_UNDERLINE + '', '<unk>', '2', '0', '0', '0', ',', SPIECE_UNDERLINE + 'and', SPIECE_UNDERLINE + 'this', SPIECE_UNDERLINE + 'is', SPIECE_UNDERLINE + 'f', 'al', 's', '<unk>', '.', ] , ) @cached_property def __a ( self ) -> Dict: '''simple docstring''' return BertGenerationTokenizer.from_pretrained('google/bert_for_seq_generation_L-24_bbc_encoder' ) @slow def __a ( self ) -> Any: '''simple docstring''' snake_case__ : int = 'Hello World!' snake_case__ : Union[str, Any] = [18536, 2260, 101] self.assertListEqual(__UpperCamelCase , self.big_tokenizer.encode(__UpperCamelCase ) ) @slow def __a ( self ) -> Optional[int]: '''simple docstring''' snake_case__ : str = ( 'This is a very long text with a lot of weird characters, such as: . , ~ ? ( ) " [ ] ! : - . Also we will' ' add words that should not exsist and be tokenized to <unk>, such as saoneuhaoesuth' ) snake_case__ : List[Any] = [ 871, 419, 358, 946, 991, 2521, 452, 358, 1357, 387, 7751, 3536, 112, 985, 456, 126, 865, 938, 5400, 5734, 458, 1368, 467, 786, 2462, 5246, 1159, 633, 865, 4519, 457, 582, 852, 2557, 427, 916, 508, 405, 34324, 497, 391, 408, 11342, 1244, 385, 100, 938, 985, 456, 574, 362, 12597, 3200, 3129, 1172, ] self.assertListEqual(__UpperCamelCase , self.big_tokenizer.encode(__UpperCamelCase ) ) @require_torch @slow def __a ( self ) -> List[str]: '''simple docstring''' import torch from transformers import BertGenerationConfig, BertGenerationEncoder # Build sequence snake_case__ : Optional[int] = list(self.big_tokenizer.get_vocab().keys() )[:10] snake_case__ : Optional[int] = ' '.join(__UpperCamelCase ) snake_case__ : int = self.big_tokenizer.encode_plus(__UpperCamelCase , return_tensors='pt' , return_token_type_ids=__UpperCamelCase ) snake_case__ : Tuple = self.big_tokenizer.batch_encode_plus( [sequence + ' ' + sequence] , return_tensors='pt' , return_token_type_ids=__UpperCamelCase ) snake_case__ : Dict = BertGenerationConfig() snake_case__ : List[str] = BertGenerationEncoder(__UpperCamelCase ) assert model.get_input_embeddings().weight.shape[0] >= self.big_tokenizer.vocab_size with torch.no_grad(): model(**__UpperCamelCase ) model(**__UpperCamelCase ) @slow def __a ( self ) -> Dict: '''simple docstring''' snake_case__ : Optional[int] = {'input_ids': [[39286, 458, 36335, 2001, 456, 13073, 13266, 455, 113, 7746, 1741, 11157, 391, 13073, 13266, 455, 113, 3967, 35412, 113, 4936, 109, 3870, 2377, 113, 30084, 45720, 458, 134, 17496, 112, 503, 11672, 113, 118, 112, 5665, 13347, 38687, 112, 1496, 31389, 112, 3268, 47264, 134, 962, 112, 16377, 8035, 23130, 430, 12169, 15518, 28592, 458, 146, 41697, 109, 391, 12169, 15518, 16689, 458, 146, 41358, 109, 452, 726, 4034, 111, 763, 35412, 5082, 388, 1903, 111, 9051, 391, 2870, 48918, 1900, 1123, 550, 998, 112, 9586, 15985, 455, 391, 410, 22955, 37636, 114], [448, 17496, 419, 3663, 385, 763, 113, 27533, 2870, 3283, 13043, 1639, 24713, 523, 656, 24013, 18550, 2521, 517, 27014, 21244, 420, 1212, 1465, 391, 927, 4833, 388, 578, 11786, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [484, 2169, 7687, 21932, 18146, 726, 363, 17032, 3391, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], 'attention_mask': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=__UpperCamelCase , model_name='google/bert_for_seq_generation_L-24_bbc_encoder' , revision='c817d1fd1be2ffa69431227a1fe320544943d4db' , )
699
0
# A Bipartite Graph is a graph whose vertices can be divided into two independent sets, # U and V such that every edge (u, v) either connects a vertex from U to V or a vertex # from V to U. In other words, for every edge (u, v), either u belongs to U and v to V, # or u belongs to V and v to U. We can also say that there is no edge that connects # vertices of same set. def UpperCamelCase__ ( A__ ) -> int: snake_case__ : List[str] = [False] * len(A__ ) snake_case__ : Tuple = [-1] * len(A__ ) def dfs(A__ , A__ ): snake_case__ : List[str] = True snake_case__ : Optional[int] = c for u in graph[v]: if not visited[u]: dfs(A__ , 1 - c ) for i in range(len(A__ ) ): if not visited[i]: dfs(A__ , 0 ) for i in range(len(A__ ) ): for j in graph[i]: if color[i] == color[j]: return False return True # Adjacency list of graph lowerCAmelCase__ : Optional[Any] = {0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 4: []} print(check_bipartite_dfs(graph))
717
import random import torch from huggingface_hub import HfApi from diffusers import UNetaDModel lowerCAmelCase__ : List[str] = HfApi() lowerCAmelCase__ : str = {} # fmt: off lowerCAmelCase__ : int = torch.tensor([ -0.75_15, -1.68_83, 0.24_20, 0.03_00, 0.63_47, 1.34_33, -1.17_43, -3.74_67, 1.23_42, -2.24_85, 0.46_36, 0.80_76, -0.79_91, 0.39_69, 0.84_98, 0.91_89, -1.88_87, -3.35_22, 0.76_39, 0.20_40, 0.62_71, -2.71_48, -1.63_16, 3.08_39, 0.31_86, 0.27_21, -0.97_59, -1.24_61, 2.62_57, 1.35_57 ]) lowerCAmelCase__ : Dict = torch.tensor([ -2.36_39, -2.53_44, 0.00_54, -0.66_74, 1.59_90, 1.01_58, 0.31_24, -2.14_36, 1.87_95, -2.54_29, -0.15_66, -0.39_73, 1.24_90, 2.64_47, 1.22_83, -0.52_08, -2.81_54, -3.51_19, 2.38_38, 1.20_33, 1.72_01, -2.12_56, -1.45_76, 2.79_48, 2.42_04, -0.97_52, -1.25_46, 0.80_27, 3.27_58, 3.13_65 ]) lowerCAmelCase__ : Dict = torch.tensor([ -0.65_31, -0.68_91, -0.31_72, -0.53_75, -0.91_40, -0.53_67, -0.11_75, -0.78_69, -0.38_08, -0.45_13, -0.20_98, -0.00_83, 0.31_83, 0.51_40, 0.22_47, -0.13_04, -0.13_02, -0.28_02, -0.20_84, -0.20_25, -0.49_67, -0.48_73, -0.08_61, 0.69_25, 0.02_50, 0.12_90, -0.15_43, 0.63_16, 1.04_60, 1.49_43 ]) lowerCAmelCase__ : List[str] = torch.tensor([ 0.09_11, 0.11_07, 0.01_82, 0.04_35, -0.08_05, -0.06_08, 0.03_81, 0.21_72, -0.02_80, 0.13_27, -0.02_99, -0.02_55, -0.00_50, -0.11_70, -0.10_46, 0.03_09, 0.13_67, 0.17_28, -0.05_33, -0.07_48, -0.05_34, 0.16_24, 0.03_84, -0.18_05, -0.07_07, 0.06_42, 0.02_20, -0.01_34, -0.13_33, -0.15_05 ]) lowerCAmelCase__ : Union[str, Any] = torch.tensor([ 0.13_21, 0.13_37, 0.04_40, 0.06_22, -0.05_91, -0.03_70, 0.05_03, 0.21_33, -0.01_77, 0.14_15, -0.01_16, -0.01_12, 0.00_44, -0.09_80, -0.07_89, 0.03_95, 0.15_02, 0.17_85, -0.04_88, -0.05_14, -0.04_04, 0.15_39, 0.04_54, -0.15_59, -0.06_65, 0.06_59, 0.03_83, -0.00_05, -0.12_66, -0.13_86 ]) lowerCAmelCase__ : List[Any] = torch.tensor([ 0.11_54, 0.12_18, 0.03_07, 0.05_26, -0.07_11, -0.05_41, 0.03_66, 0.20_78, -0.02_67, 0.13_17, -0.02_26, -0.01_93, -0.00_14, -0.10_55, -0.09_02, 0.03_30, 0.13_91, 0.17_09, -0.05_62, -0.06_93, -0.05_60, 0.14_82, 0.03_81, -0.16_83, -0.06_81, 0.06_61, 0.03_31, -0.00_46, -0.12_68, -0.14_31 ]) lowerCAmelCase__ : Optional[Any] = torch.tensor([ 0.11_92, 0.12_40, 0.04_14, 0.06_06, -0.05_57, -0.04_12, 0.04_30, 0.20_42, -0.02_00, 0.13_85, -0.01_15, -0.01_32, 0.00_17, -0.09_65, -0.08_02, 0.03_98, 0.14_33, 0.17_47, -0.04_58, -0.05_33, -0.04_07, 0.15_45, 0.04_19, -0.15_74, -0.06_45, 0.06_26, 0.03_41, -0.00_10, -0.11_99, -0.13_90 ]) lowerCAmelCase__ : List[str] = torch.tensor([ 0.10_75, 0.10_74, 0.02_05, 0.04_31, -0.07_74, -0.06_07, 0.02_98, 0.20_42, -0.03_20, 0.12_67, -0.02_81, -0.02_50, -0.00_64, -0.10_91, -0.09_46, 0.02_90, 0.13_28, 0.16_50, -0.05_80, -0.07_38, -0.05_86, 0.14_40, 0.03_37, -0.17_46, -0.07_12, 0.06_05, 0.02_50, -0.00_99, -0.13_16, -0.14_73 ]) lowerCAmelCase__ : List[str] = torch.tensor([ -1.45_72, -2.04_81, -0.04_14, -0.60_05, 1.41_36, 0.58_48, 0.40_28, -2.73_30, 1.22_12, -2.12_28, 0.21_55, 0.40_39, 0.76_62, 2.05_35, 0.74_77, -0.32_43, -2.17_58, -2.76_48, 1.69_47, 0.70_26, 1.23_38, -1.60_78, -0.86_82, 2.28_10, 1.85_74, -0.57_18, -0.55_86, -0.01_86, 2.34_15, 2.12_51]) lowerCAmelCase__ : List[Any] = torch.tensor([ -1.36_90, -1.97_20, -0.40_90, -0.69_66, 1.46_60, 0.99_38, -0.13_85, -2.73_24, 0.77_36, -1.89_17, 0.29_23, 0.42_93, 0.16_93, 1.41_12, 1.18_87, -0.31_81, -2.21_60, -2.63_81, 1.31_70, 0.81_63, 0.92_40, -1.65_44, -0.60_99, 2.52_59, 1.64_30, -0.90_90, -0.93_92, -0.01_26, 2.42_68, 2.32_66 ]) lowerCAmelCase__ : Tuple = torch.tensor([ -1.35_25, -1.96_28, -0.39_56, -0.68_60, 1.46_64, 1.00_14, -0.12_59, -2.72_12, 0.77_72, -1.88_11, 0.29_96, 0.43_88, 0.17_04, 1.40_29, 1.17_01, -0.30_27, -2.20_53, -2.62_87, 1.33_50, 0.81_31, 0.92_74, -1.62_92, -0.60_98, 2.51_31, 1.65_05, -0.89_58, -0.92_98, -0.01_51, 2.42_57, 2.33_55 ]) lowerCAmelCase__ : List[str] = torch.tensor([ -2.05_85, -2.78_97, -0.28_50, -0.89_40, 1.90_52, 0.57_02, 0.63_45, -3.89_59, 1.59_32, -3.23_19, 0.19_74, 0.02_87, 1.75_66, 2.65_43, 0.83_87, -0.53_51, -3.27_36, -4.33_75, 2.90_29, 1.63_90, 1.46_40, -2.17_01, -1.90_13, 2.93_41, 3.49_81, -0.62_55, -1.16_44, -0.15_91, 3.70_97, 3.20_66 ]) lowerCAmelCase__ : Dict = torch.tensor([ -2.31_39, -2.55_94, -0.01_97, -0.67_85, 1.70_01, 1.16_06, 0.30_75, -2.17_40, 1.80_71, -2.56_30, -0.09_26, -0.38_11, 1.21_16, 2.62_46, 1.27_31, -0.53_98, -2.81_53, -3.61_40, 2.38_93, 1.32_62, 1.62_58, -2.18_56, -1.32_67, 2.83_95, 2.37_79, -1.06_23, -1.24_68, 0.89_59, 3.33_67, 3.22_43 ]) lowerCAmelCase__ : Dict = torch.tensor([ -2.06_28, -2.76_67, -0.20_89, -0.82_63, 2.05_39, 0.59_92, 0.64_95, -3.83_36, 1.60_25, -3.28_17, 0.17_21, -0.06_33, 1.75_16, 2.70_39, 0.81_00, -0.59_08, -3.21_13, -4.43_43, 2.92_57, 1.36_32, 1.55_62, -2.14_89, -1.98_94, 3.05_60, 3.33_96, -0.73_28, -1.04_17, 0.03_83, 3.70_93, 3.23_43 ]) lowerCAmelCase__ : Any = torch.tensor([ -1.45_74, -2.05_69, -0.04_73, -0.61_17, 1.40_18, 0.57_69, 0.41_29, -2.73_44, 1.22_41, -2.13_97, 0.20_00, 0.39_37, 0.76_16, 2.04_53, 0.73_24, -0.33_91, -2.17_46, -2.77_44, 1.69_63, 0.69_21, 1.21_87, -1.61_72, -0.88_77, 2.24_39, 1.84_71, -0.58_39, -0.56_05, -0.04_64, 2.32_50, 2.12_19 ]) # fmt: on lowerCAmelCase__ : Any = api.list_models(filter='''diffusers''') for mod in models: if "google" in mod.author or mod.modelId == "CompVis/ldm-celebahq-256": lowerCAmelCase__ : List[str] = '''/home/patrick/google_checkpoints/''' + mod.modelId.split('''/''')[-1] print(F'''Started running {mod.modelId}!!!''') if mod.modelId.startswith('''CompVis'''): lowerCAmelCase__ : int = UNetaDModel.from_pretrained(local_checkpoint, subfolder='''unet''') else: lowerCAmelCase__ : str = UNetaDModel.from_pretrained(local_checkpoint) torch.manual_seed(0) random.seed(0) lowerCAmelCase__ : Any = torch.randn(1, model.config.in_channels, model.config.sample_size, model.config.sample_size) lowerCAmelCase__ : List[str] = torch.tensor([10] * noise.shape[0]) with torch.no_grad(): lowerCAmelCase__ : int = model(noise, time_step).sample assert torch.allclose( logits[0, 0, 0, :30], results['''_'''.join('''_'''.join(mod.modelId.split('''/''')).split('''-'''))], atol=1E-3 ) print(F'''{mod.modelId} has passed successfully!!!''')
699
0
import tempfile import unittest from pathlib import Path from shutil import copyfile from transformers import MaMaaaTokenizer, is_torch_available from transformers.testing_utils import ( get_tests_dir, nested_simplify, require_sentencepiece, require_tokenizers, require_torch, slow, ) from transformers.utils import is_sentencepiece_available if is_sentencepiece_available(): from transformers.models.mam_aaa.tokenization_mam_aaa import VOCAB_FILES_NAMES, save_json from ...test_tokenization_common import TokenizerTesterMixin if is_sentencepiece_available(): lowerCAmelCase__ : List[str] = get_tests_dir('''fixtures/test_sentencepiece.model''') if is_torch_available(): from transformers.models.mam_aaa.modeling_mam_aaa import shift_tokens_right lowerCAmelCase__ : Union[str, Any] = 12_80_22 lowerCAmelCase__ : Any = 12_80_28 @require_sentencepiece class __snake_case ( __lowerCAmelCase ,unittest.TestCase ): __lowerCamelCase = MaMaaaTokenizer __lowerCamelCase = False __lowerCamelCase = False __lowerCamelCase = True def __a ( self ) -> Dict: '''simple docstring''' super().setUp() snake_case__ : Optional[Any] = ['''</s>''', '''<unk>''', '''▁This''', '''▁is''', '''▁a''', '''▁t''', '''est''', '''\u0120''', '''<pad>'''] snake_case__ : List[str] = dict(zip(lowerCamelCase__ , range(len(lowerCamelCase__ ) ) ) ) snake_case__ : Any = Path(self.tmpdirname ) save_json(lowerCamelCase__ , save_dir / VOCAB_FILES_NAMES['vocab_file'] ) if not (save_dir / VOCAB_FILES_NAMES["spm_file"]).exists(): copyfile(lowerCamelCase__ , save_dir / VOCAB_FILES_NAMES['spm_file'] ) snake_case__ : Dict = MaMaaaTokenizer.from_pretrained(self.tmpdirname ) tokenizer.save_pretrained(self.tmpdirname ) def __a ( self , **__UpperCamelCase ) -> int: '''simple docstring''' return MaMaaaTokenizer.from_pretrained(self.tmpdirname , **lowerCamelCase__ ) def __a ( self , __UpperCamelCase ) -> Optional[Any]: '''simple docstring''' return ( "This is a test", "This is a test", ) def __a ( self ) -> Optional[Any]: '''simple docstring''' snake_case__ : List[Any] = '''</s>''' snake_case__ : List[Any] = 0 self.assertEqual(self.get_tokenizer()._convert_token_to_id(lowerCamelCase__ ) , lowerCamelCase__ ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(lowerCamelCase__ ) , lowerCamelCase__ ) def __a ( self ) -> List[str]: '''simple docstring''' snake_case__ : Any = self.get_tokenizer() snake_case__ : Optional[int] = list(tokenizer.get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '</s>' ) self.assertEqual(vocab_keys[1] , '<unk>' ) self.assertEqual(vocab_keys[-1] , '<s>' ) self.assertEqual(len(lowerCamelCase__ ) , tokenizer.vocab_size + len(tokenizer.get_added_vocab() ) ) @unittest.skip('Skip this test while all models are still to be uploaded.' ) def __a ( self ) -> str: '''simple docstring''' pass def __a ( self ) -> Union[str, Any]: '''simple docstring''' snake_case__ : int = self.get_tokenizer() snake_case__ : Union[str, Any] = tokenizer.tokenize('This is a test' ) self.assertListEqual(lowerCamelCase__ , ['▁This', '▁is', '▁a', '▁t', 'est'] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(lowerCamelCase__ ) , [2, 3, 4, 5, 6] , ) snake_case__ : List[Any] = tokenizer.convert_ids_to_tokens([2, 3, 4, 5, 6] ) self.assertListEqual(lowerCamelCase__ , ['▁This', '▁is', '▁a', '▁t', 'est'] ) snake_case__ : List[Any] = tokenizer.convert_tokens_to_string(lowerCamelCase__ ) self.assertEqual(lowerCamelCase__ , 'This is a test' ) @slow def __a ( self ) -> Optional[Any]: '''simple docstring''' snake_case__ : Optional[int] = {'''input_ids''': [[128022, 110108, 397, 11, 38272, 2247, 124811, 285, 18105, 1586, 207, 7, 39534, 4428, 397, 1019, 18105, 1586, 207, 7, 41337, 16786, 241, 7, 20214, 17, 125690, 10398, 7, 44378, 58069, 68342, 7798, 7343, 11, 299, 33310, 4, 158, 37350, 94077, 4569, 299, 33310, 90, 4, 52840, 290, 4, 31270, 112, 299, 682, 4, 52840, 39953, 14079, 193, 52519, 90894, 17894, 120697, 11, 40445, 551, 17, 1019, 52519, 90894, 17756, 963, 11, 40445, 480, 17, 9792, 1120, 5173, 1393, 6240, 16786, 241, 120996, 28, 1245, 1393, 118240, 11123, 1019, 93612, 2691, 10618, 98058, 120409, 1928, 279, 4, 40683, 367, 178, 207, 1019, 103, 103121, 506, 65296, 5, 2], [128022, 21217, 367, 117, 125450, 128, 719, 7, 7308, 40, 93612, 12669, 1116, 16704, 71, 17785, 3699, 15592, 35, 144, 9584, 241, 11943, 713, 950, 799, 2247, 88427, 150, 149, 118813, 120706, 1019, 106906, 81518, 28, 1224, 22799, 397, 5, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [128022, 1658, 123311, 5155, 5578, 4722, 279, 14947, 2366, 1120, 1197, 14, 1348, 9232, 5, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], '''attention_mask''': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=lowerCamelCase__ , model_name='facebook/m2m100_418M' , revision='c168bae485c864188cf9aa0e4108b0b6934dc91e' , ) @require_torch @require_sentencepiece @require_tokenizers class __snake_case ( unittest.TestCase ): __lowerCamelCase = "facebook/m2m100_418M" __lowerCamelCase = [ "In my opinion, there are two levels of response from the French government.", "NSA Affair Emphasizes Complete Lack of Debate on Intelligence", ] __lowerCamelCase = [ "Selon moi, il y a deux niveaux de réponse de la part du gouvernement français.", "L'affaire NSA souligne l'absence totale de débat sur le renseignement", ] # fmt: off __lowerCamelCase = [EN_CODE, 593, 1949, 11_5781, 4, 7_1586, 4234, 6_0633, 12_6233, 432, 12_3808, 1_5592, 1197, 11_7132, 12_0618, 5, 2] @classmethod def __a ( cls ) -> Tuple: '''simple docstring''' snake_case__ : MaMaaaTokenizer = MaMaaaTokenizer.from_pretrained( cls.checkpoint_name , src_lang='en' , tgt_lang='fr' ) snake_case__ : int = 1 return cls def __a ( self ) -> Optional[int]: '''simple docstring''' self.assertEqual(self.tokenizer.get_lang_id('ar' ) , 128006 ) self.assertEqual(self.tokenizer.get_lang_id('en' ) , 128022 ) self.assertEqual(self.tokenizer.get_lang_id('ro' ) , 128076 ) self.assertEqual(self.tokenizer.get_lang_id('mr' ) , 128063 ) def __a ( self ) -> Optional[int]: '''simple docstring''' snake_case__ : Any = self.tokenizer.get_vocab() self.assertEqual(len(lowerCamelCase__ ) , self.tokenizer.vocab_size ) self.assertEqual(vocab['<unk>'] , 3 ) self.assertIn(self.tokenizer.get_lang_token('en' ) , lowerCamelCase__ ) def __a ( self ) -> List[Any]: '''simple docstring''' snake_case__ : List[Any] = '''en''' snake_case__ : Any = self.tokenizer.batch_encode_plus(self.src_text ).input_ids[0] self.assertListEqual(self.expected_src_tokens , lowerCamelCase__ ) def __a ( self ) -> Dict: '''simple docstring''' self.assertIn(lowerCamelCase__ , self.tokenizer.all_special_ids ) # fmt: off snake_case__ : List[Any] = [FR_CODE, 5364, 82, 8642, 4, 294, 47, 8, 14028, 136, 3286, 9706, 6, 90797, 6, 144012, 162, 88128, 30061, 5, 2] # fmt: on snake_case__ : Optional[int] = self.tokenizer.decode(lowerCamelCase__ , skip_special_tokens=lowerCamelCase__ ) snake_case__ : Tuple = self.tokenizer.decode(generated_ids[1:] , skip_special_tokens=lowerCamelCase__ ) self.assertEqual(lowerCamelCase__ , lowerCamelCase__ ) self.assertNotIn(self.tokenizer.eos_token , lowerCamelCase__ ) def __a ( self ) -> Optional[Any]: '''simple docstring''' snake_case__ : Dict = tempfile.mkdtemp() snake_case__ : Any = self.tokenizer.lang_token_to_id self.tokenizer.save_pretrained(lowerCamelCase__ ) snake_case__ : List[Any] = MaMaaaTokenizer.from_pretrained(lowerCamelCase__ ) self.assertDictEqual(new_tok.lang_token_to_id , lowerCamelCase__ ) @require_torch def __a ( self ) -> List[str]: '''simple docstring''' snake_case__ : Any = '''en''' snake_case__ : str = '''fr''' snake_case__ : Tuple = self.tokenizer(self.src_text , text_target=self.tgt_text , padding=lowerCamelCase__ , return_tensors='pt' ) snake_case__ : Any = shift_tokens_right( batch['labels'] , self.tokenizer.pad_token_id , self.tokenizer.eos_token_id ) for k in batch: snake_case__ : List[Any] = batch[k].tolist() # batch = {k: v.tolist() for k,v in batch.items()} # fairseq batch: https://gist.github.com/sshleifer/cba08bc2109361a74ac3760a7e30e4f4 # batch.decoder_inputs_ids[0][0] == assert batch.input_ids[1][0] == EN_CODE assert batch.input_ids[1][-1] == 2 assert batch.labels[1][0] == FR_CODE assert batch.labels[1][-1] == 2 assert batch.decoder_input_ids[1][:2] == [2, FR_CODE] @require_torch def __a ( self ) -> Dict: '''simple docstring''' snake_case__ : List[Any] = '''mr''' self.assertListEqual(self.tokenizer.prefix_tokens , [self.tokenizer.get_lang_id('mr' )] ) self.assertListEqual(self.tokenizer.suffix_tokens , [self.tokenizer.eos_token_id] ) snake_case__ : int = '''zh''' self.assertListEqual(self.tokenizer.prefix_tokens , [self.tokenizer.get_lang_id('zh' )] ) self.assertListEqual(self.tokenizer.suffix_tokens , [self.tokenizer.eos_token_id] ) @require_torch def __a ( self ) -> Optional[int]: '''simple docstring''' snake_case__ : int = '''mr''' self.tokenizer._switch_to_target_mode() self.assertListEqual(self.tokenizer.prefix_tokens , [self.tokenizer.get_lang_id('mr' )] ) self.assertListEqual(self.tokenizer.suffix_tokens , [self.tokenizer.eos_token_id] ) self.tokenizer._switch_to_input_mode() self.assertListEqual(self.tokenizer.prefix_tokens , [self.tokenizer.get_lang_id(self.tokenizer.src_lang )] ) snake_case__ : List[str] = '''zh''' self.tokenizer._switch_to_target_mode() self.assertListEqual(self.tokenizer.prefix_tokens , [self.tokenizer.get_lang_id('zh' )] ) self.assertListEqual(self.tokenizer.suffix_tokens , [self.tokenizer.eos_token_id] ) self.tokenizer._switch_to_input_mode() self.assertListEqual(self.tokenizer.prefix_tokens , [self.tokenizer.get_lang_id(self.tokenizer.src_lang )] ) @require_torch def __a ( self ) -> Optional[int]: '''simple docstring''' snake_case__ : Union[str, Any] = self.tokenizer._build_translation_inputs('A test' , return_tensors='pt' , src_lang='en' , tgt_lang='ar' ) self.assertEqual( nested_simplify(lowerCamelCase__ ) , { # en_XX, A, test, EOS 'input_ids': [[128022, 58, 4183, 2]], 'attention_mask': [[1, 1, 1, 1]], # ar_AR 'forced_bos_token_id': 128006, } , )
718
import warnings from ...utils import logging from .image_processing_perceiver import PerceiverImageProcessor lowerCAmelCase__ : Dict = logging.get_logger(__name__) class __snake_case ( _lowerCamelCase ): def __init__( self , *__UpperCamelCase , **__UpperCamelCase ) -> None: '''simple docstring''' warnings.warn( 'The class PerceiverFeatureExtractor is deprecated and will be removed in version 5 of Transformers.' ' Please use PerceiverImageProcessor instead.' , __UpperCamelCase , ) super().__init__(*__UpperCamelCase , **__UpperCamelCase )
699
0
# Algorithm for the pigeonhole sorting def UpperCamelCase__ ( A__ ) -> List[Any]: snake_case__ : Any = min(snake_case_ ) # min() finds the minimum value snake_case__ : Dict = max(snake_case_ ) # max() finds the maximum value snake_case__ : Optional[int] = max_val - min_val + 1 # size is difference of max and min values plus one # list of pigeonholes of size equal to the variable size snake_case__ : Optional[Any] = [0] * size # Populate the pigeonholes. for x in a: assert isinstance(snake_case_ , snake_case_ ), "integers only please" holes[x - min_val] += 1 # Putting the elements back into the array in an order. snake_case__ : Dict = 0 for count in range(snake_case_ ): while holes[count] > 0: holes[count] -= 1 snake_case__ : List[str] = count + min_val i += 1 def UpperCamelCase__ ( ) -> Optional[Any]: snake_case__ : List[Any] = [8, 3, 2, 7, 4, 6, 8] pigeonhole_sort(snake_case_ ) print('Sorted order is:' , ' '.join(snake_case_ ) ) if __name__ == "__main__": main()
719
import io import itertools import json from dataclasses import dataclass from typing import Optional import pyarrow as pa import pyarrow.json as paj import datasets from datasets.table import table_cast from datasets.utils.file_utils import readline lowerCAmelCase__ : List[Any] = datasets.utils.logging.get_logger(__name__) @dataclass class __snake_case ( datasets.BuilderConfig ): __lowerCamelCase = None __lowerCamelCase = "utf-8" __lowerCamelCase = None __lowerCamelCase = None __lowerCamelCase = True # deprecated __lowerCamelCase = None # deprecated __lowerCamelCase = 10 << 20 # 10MB __lowerCamelCase = None class __snake_case ( datasets.ArrowBasedBuilder ): __lowerCamelCase = JsonConfig def __a ( self ) -> Optional[Any]: '''simple docstring''' if self.config.block_size is not None: logger.warning('The JSON loader parameter `block_size` is deprecated. Please use `chunksize` instead' ) snake_case__ : str = self.config.block_size if self.config.use_threads is not True: logger.warning( 'The JSON loader parameter `use_threads` is deprecated and doesn\'t have any effect anymore.' ) if self.config.newlines_in_values is not None: raise ValueError('The JSON loader parameter `newlines_in_values` is no longer supported' ) return datasets.DatasetInfo(features=self.config.features ) def __a ( self , __UpperCamelCase ) -> Dict: '''simple docstring''' 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}""" ) snake_case__ : int = dl_manager.download_and_extract(self.config.data_files ) if isinstance(__UpperCamelCase , (str, list, tuple) ): snake_case__ : Any = data_files if isinstance(__UpperCamelCase , __UpperCamelCase ): snake_case__ : Optional[Any] = [files] snake_case__ : List[str] = [dl_manager.iter_files(__UpperCamelCase ) for file in files] return [datasets.SplitGenerator(name=datasets.Split.TRAIN , gen_kwargs={'files': files} )] snake_case__ : List[Any] = [] for split_name, files in data_files.items(): if isinstance(__UpperCamelCase , __UpperCamelCase ): snake_case__ : List[Any] = [files] snake_case__ : Any = [dl_manager.iter_files(__UpperCamelCase ) for file in files] splits.append(datasets.SplitGenerator(name=__UpperCamelCase , gen_kwargs={'files': files} ) ) return splits def __a ( self , __UpperCamelCase ) -> pa.Table: '''simple docstring''' if self.config.features is not None: # adding missing columns for column_name in set(self.config.features ) - set(pa_table.column_names ): snake_case__ : List[Any] = self.config.features.arrow_schema.field(__UpperCamelCase ).type snake_case__ : List[str] = pa_table.append_column(__UpperCamelCase , pa.array([None] * len(__UpperCamelCase ) , type=__UpperCamelCase ) ) # more expensive cast to support nested structures with keys in a different order # allows str <-> int/float or str to Audio for example snake_case__ : List[str] = table_cast(__UpperCamelCase , self.config.features.arrow_schema ) return pa_table def __a ( self , __UpperCamelCase ) -> int: '''simple docstring''' for file_idx, file in enumerate(itertools.chain.from_iterable(__UpperCamelCase ) ): # If the file is one json object and if we need to look at the list of items in one specific field if self.config.field is not None: with open(__UpperCamelCase , encoding=self.config.encoding , errors=self.config.encoding_errors ) as f: snake_case__ : Union[str, Any] = json.load(__UpperCamelCase ) # We keep only the field we are interested in snake_case__ : Tuple = dataset[self.config.field] # We accept two format: a list of dicts or a dict of lists if isinstance(__UpperCamelCase , (list, tuple) ): snake_case__ : List[Any] = set().union(*[row.keys() for row in dataset] ) snake_case__ : List[Any] = {col: [row.get(__UpperCamelCase ) for row in dataset] for col in keys} else: snake_case__ : List[Any] = dataset snake_case__ : Dict = pa.Table.from_pydict(__UpperCamelCase ) yield file_idx, self._cast_table(__UpperCamelCase ) # If the file has one json object per line else: with open(__UpperCamelCase , 'rb' ) as f: snake_case__ : Optional[int] = 0 # Use block_size equal to the chunk size divided by 32 to leverage multithreading # Set a default minimum value of 16kB if the chunk size is really small snake_case__ : Tuple = max(self.config.chunksize // 32 , 16 << 10 ) snake_case__ : Optional[Any] = ( self.config.encoding_errors if self.config.encoding_errors is not None else 'strict' ) while True: snake_case__ : Optional[int] = f.read(self.config.chunksize ) if not batch: break # Finish current line try: batch += f.readline() except (AttributeError, io.UnsupportedOperation): batch += readline(__UpperCamelCase ) # PyArrow only accepts utf-8 encoded bytes if self.config.encoding != "utf-8": snake_case__ : int = batch.decode(self.config.encoding , errors=__UpperCamelCase ).encode('utf-8' ) try: while True: try: snake_case__ : List[str] = paj.read_json( io.BytesIO(__UpperCamelCase ) , read_options=paj.ReadOptions(block_size=__UpperCamelCase ) ) break except (pa.ArrowInvalid, pa.ArrowNotImplementedError) as e: if ( isinstance(__UpperCamelCase , pa.ArrowInvalid ) and "straddling" not in str(__UpperCamelCase ) or block_size > len(__UpperCamelCase ) ): raise else: # Increase the block size in case it was too small. # The block size will be reset for the next file. logger.debug( F"""Batch of {len(__UpperCamelCase )} bytes couldn't be parsed with block_size={block_size}. Retrying with block_size={block_size * 2}.""" ) block_size *= 2 except pa.ArrowInvalid as e: try: with open( __UpperCamelCase , encoding=self.config.encoding , errors=self.config.encoding_errors ) as f: snake_case__ : Tuple = json.load(__UpperCamelCase ) except json.JSONDecodeError: logger.error(F"""Failed to read file '{file}' with error {type(__UpperCamelCase )}: {e}""" ) raise e # If possible, parse the file as a list of json objects and exit the loop if isinstance(__UpperCamelCase , __UpperCamelCase ): # list is the only sequence type supported in JSON try: snake_case__ : str = set().union(*[row.keys() for row in dataset] ) snake_case__ : Union[str, Any] = {col: [row.get(__UpperCamelCase ) for row in dataset] for col in keys} snake_case__ : List[str] = pa.Table.from_pydict(__UpperCamelCase ) except (pa.ArrowInvalid, AttributeError) as e: logger.error(F"""Failed to read file '{file}' with error {type(__UpperCamelCase )}: {e}""" ) raise ValueError(F"""Not able to read records in the JSON file at {file}.""" ) from None yield file_idx, self._cast_table(__UpperCamelCase ) break else: logger.error(F"""Failed to read file '{file}' with error {type(__UpperCamelCase )}: {e}""" ) raise ValueError( F"""Not able to read records in the JSON file at {file}. """ F"""You should probably indicate the field of the JSON file containing your records. """ F"""This JSON file contain the following fields: {str(list(dataset.keys() ) )}. """ F"""Select the correct one and provide it as `field='XXX'` to the dataset loading method. """ ) from None # Uncomment for debugging (will print the Arrow table size and elements) # logger.warning(f"pa_table: {pa_table} num rows: {pa_table.num_rows}") # logger.warning('\n'.join(str(pa_table.slice(i, 1).to_pydict()) for i in range(pa_table.num_rows))) yield (file_idx, batch_idx), self._cast_table(__UpperCamelCase ) batch_idx += 1
699
0
from __future__ import annotations def UpperCamelCase__ ( A__ ) -> List[Any]: snake_case__ : List[Any] = 0.0_0 snake_case__ : str = 0 for resistor in resistors: if resistor <= 0: snake_case__ : Union[str, Any] = F"""Resistor at index {index} has a negative or zero value!""" raise ValueError(UpperCamelCase__ ) first_sum += 1 / float(UpperCamelCase__ ) index += 1 return 1 / first_sum def UpperCamelCase__ ( A__ ) -> Dict: snake_case__ : Dict = 0.0_0 snake_case__ : str = 0 for resistor in resistors: sum_r += resistor if resistor < 0: snake_case__ : Dict = F"""Resistor at index {index} has a negative value!""" raise ValueError(UpperCamelCase__ ) index += 1 return sum_r if __name__ == "__main__": import doctest doctest.testmod()
720
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__ : Any = {'''configuration_xglm''': ['''XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''XGLMConfig''']} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : str = ['''XGLMTokenizer'''] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : Dict = ['''XGLMTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : Optional[int] = [ '''XGLM_PRETRAINED_MODEL_ARCHIVE_LIST''', '''XGLMForCausalLM''', '''XGLMModel''', '''XGLMPreTrainedModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : Dict = [ '''FlaxXGLMForCausalLM''', '''FlaxXGLMModel''', '''FlaxXGLMPreTrainedModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : Dict = [ '''TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFXGLMForCausalLM''', '''TFXGLMModel''', '''TFXGLMPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_xglm import XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XGLMConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xglm import XGLMTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xglm_fast import XGLMTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xglm import XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, XGLMForCausalLM, XGLMModel, XGLMPreTrainedModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_xglm import FlaxXGLMForCausalLM, FlaxXGLMModel, FlaxXGLMPreTrainedModel try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_xglm import ( TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFXGLMForCausalLM, TFXGLMModel, TFXGLMPreTrainedModel, ) else: import sys lowerCAmelCase__ : Tuple = _LazyModule(__name__, globals()['''__file__'''], _import_structure)
699
0
import sys lowerCAmelCase__ : Union[str, Any] = ( '''73167176531330624919225119674426574742355349194934''' '''96983520312774506326239578318016984801869478851843''' '''85861560789112949495459501737958331952853208805511''' '''12540698747158523863050715693290963295227443043557''' '''66896648950445244523161731856403098711121722383113''' '''62229893423380308135336276614282806444486645238749''' '''30358907296290491560440772390713810515859307960866''' '''70172427121883998797908792274921901699720888093776''' '''65727333001053367881220235421809751254540594752243''' '''52584907711670556013604839586446706324415722155397''' '''53697817977846174064955149290862569321978468622482''' '''83972241375657056057490261407972968652414535100474''' '''82166370484403199890008895243450658541227588666881''' '''16427171479924442928230863465674813919123162824586''' '''17866458359124566529476545682848912883142607690042''' '''24219022671055626321111109370544217506941658960408''' '''07198403850962455444362981230987879927244284909188''' '''84580156166097919133875499200524063689912560717606''' '''05886116467109405077541002256983155200055935729725''' '''71636269561882670428252483600823257530420752963450''' ) def UpperCamelCase__ ( A__ = N ) -> int: snake_case__ : Dict = -sys.maxsize - 1 for i in range(len(lowerCamelCase__ ) - 12 ): snake_case__ : Optional[int] = 1 for j in range(13 ): product *= int(n[i + j] ) if product > largest_product: snake_case__ : List[Any] = product return largest_product if __name__ == "__main__": print(F'''{solution() = }''')
721
from __future__ import annotations import random # Maximum size of the population. Bigger could be faster but is more memory expensive. lowerCAmelCase__ : Dict = 2_00 # Number of elements selected in every generation of evolution. The selection takes # place from best to worst of that generation and must be smaller than N_POPULATION. lowerCAmelCase__ : List[str] = 50 # Probability that an element of a generation can mutate, changing one of its genes. # This will guarantee that all genes will be used during evolution. lowerCAmelCase__ : List[str] = 0.4 # Just a seed to improve randomness required by the algorithm. random.seed(random.randint(0, 10_00)) def UpperCamelCase__ ( A__ , A__ ) -> tuple[str, float]: snake_case__ : Tuple = len([g for position, g in enumerate(A__ ) if g == main_target[position]] ) return (item, float(A__ )) def UpperCamelCase__ ( A__ , A__ ) -> tuple[str, str]: snake_case__ : str = random.randint(0 , len(A__ ) - 1 ) snake_case__ : int = parent_a[:random_slice] + parent_a[random_slice:] snake_case__ : Any = parent_a[:random_slice] + parent_a[random_slice:] return (child_a, child_a) def UpperCamelCase__ ( A__ , A__ ) -> str: snake_case__ : List[Any] = list(A__ ) if random.uniform(0 , 1 ) < MUTATION_PROBABILITY: snake_case__ : Optional[Any] = random.choice(A__ ) return "".join(A__ ) def UpperCamelCase__ ( A__ , A__ , A__ , ) -> list[str]: snake_case__ : Tuple = [] # Generate more children proportionally to the fitness score. snake_case__ : Optional[Any] = int(parent_a[1] * 100 ) + 1 snake_case__ : str = 10 if child_n >= 10 else child_n for _ in range(A__ ): snake_case__ : Any = population_score[random.randint(0 , A__ )][0] snake_case__ , snake_case__ : int = crossover(parent_a[0] , A__ ) # Append new string to the population list. pop.append(mutate(A__ , A__ ) ) pop.append(mutate(A__ , A__ ) ) return pop def UpperCamelCase__ ( A__ , A__ , A__ = True ) -> tuple[int, int, str]: # Verify if N_POPULATION is bigger than N_SELECTED if N_POPULATION < N_SELECTED: snake_case__ : Union[str, Any] = F"""{N_POPULATION} must be bigger than {N_SELECTED}""" raise ValueError(A__ ) # Verify that the target contains no genes besides the ones inside genes variable. snake_case__ : Tuple = sorted({c for c in target if c not in genes} ) if not_in_genes_list: snake_case__ : int = F"""{not_in_genes_list} is not in genes list, evolution cannot converge""" raise ValueError(A__ ) # Generate random starting population. snake_case__ : Union[str, Any] = [] for _ in range(A__ ): population.append(''.join([random.choice(A__ ) for i in range(len(A__ ) )] ) ) # Just some logs to know what the algorithms is doing. snake_case__ , snake_case__ : str = 0, 0 # This loop will end when we find a perfect match for our target. while True: generation += 1 total_population += len(A__ ) # Random population created. Now it's time to evaluate. # Adding a bit of concurrency can make everything faster, # # import concurrent.futures # population_score: list[tuple[str, float]] = [] # with concurrent.futures.ThreadPoolExecutor( # max_workers=NUM_WORKERS) as executor: # futures = {executor.submit(evaluate, item) for item in population} # concurrent.futures.wait(futures) # population_score = [item.result() for item in futures] # # but with a simple algorithm like this, it will probably be slower. # We just need to call evaluate for every item inside the population. snake_case__ : List[Any] = [evaluate(A__ , A__ ) for item in population] # Check if there is a matching evolution. snake_case__ : int = sorted(A__ , key=lambda A__ : x[1] , reverse=A__ ) if population_score[0][0] == target: return (generation, total_population, population_score[0][0]) # Print the best result every 10 generation. # Just to know that the algorithm is working. if debug and generation % 10 == 0: print( F"""\nGeneration: {generation}""" F"""\nTotal Population:{total_population}""" F"""\nBest score: {population_score[0][1]}""" F"""\nBest string: {population_score[0][0]}""" ) # Flush the old population, keeping some of the best evolutions. # Keeping this avoid regression of evolution. snake_case__ : Optional[int] = population[: int(N_POPULATION / 3 )] population.clear() population.extend(A__ ) # Normalize population score to be between 0 and 1. snake_case__ : str = [ (item, score / len(A__ )) for item, score in population_score ] # This is selection for i in range(A__ ): population.extend(select(population_score[int(A__ )] , A__ , A__ ) ) # Check if the population has already reached the maximum value and if so, # break the cycle. If this check is disabled, the algorithm will take # forever to compute large strings, but will also calculate small strings in # a far fewer generations. if len(A__ ) > N_POPULATION: break if __name__ == "__main__": lowerCAmelCase__ : str = ( '''This is a genetic algorithm to evaluate, combine, evolve, and mutate a string!''' ) lowerCAmelCase__ : Optional[Any] = list( ''' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklm''' '''nopqrstuvwxyz.,;!?+-*#@^\'èéòà€ù=)(&%$£/\\''' ) lowerCAmelCase__, lowerCAmelCase__, lowerCAmelCase__ : List[str] = basic(target_str, genes_list) print( F'''\nGeneration: {generation}\nTotal Population: {population}\nTarget: {target}''' )
699
0
import argparse import re import requests import torch # git clone https://github.com/salesforce/BLIP.git from models.blip import blip_decoder from models.blip_itm import blip_itm from models.blip_vqa import blip_vqa from PIL import Image from torchvision import transforms from torchvision.transforms.functional import InterpolationMode from transformers import ( BertTokenizer, BlipConfig, BlipForConditionalGeneration, BlipForImageTextRetrieval, BlipForQuestionAnswering, ) def UpperCamelCase__ ( A__ , A__ ) -> Any: snake_case__ : List[Any] = 'https://storage.googleapis.com/sfr-vision-language-research/BLIP/demo.jpg' snake_case__ : str = Image.open(requests.get(A__ , stream=A__ ).raw ).convert('RGB' ) snake_case__ : Union[str, Any] = transforms.Compose( [ transforms.Resize((image_size, image_size) , interpolation=InterpolationMode.BICUBIC ), transforms.ToTensor(), transforms.Normalize((0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3) , (0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1) ), ] ) snake_case__ : Union[str, Any] = transform(A__ ).unsqueeze(0 ).to(A__ ) return image def UpperCamelCase__ ( A__ ) -> Tuple: if "visual_encoder" in key: snake_case__ : str = re.sub('visual_encoder*' , 'vision_model.encoder' , A__ ) if "blocks" in key: snake_case__ : List[str] = re.sub(r'blocks' , 'layers' , A__ ) if "attn" in key: snake_case__ : Tuple = re.sub(r'attn' , 'self_attn' , A__ ) if "norm1" in key: snake_case__ : Tuple = re.sub(r'norm1' , 'layer_norm1' , A__ ) if "norm2" in key: snake_case__ : Dict = re.sub(r'norm2' , 'layer_norm2' , A__ ) if "encoder.norm" in key: snake_case__ : Tuple = re.sub(r'encoder.norm' , 'post_layernorm' , A__ ) if "encoder.patch_embed.proj" in key: snake_case__ : Dict = re.sub(r'encoder.patch_embed.proj' , 'embeddings.patch_embedding' , A__ ) if "encoder.pos_embed" in key: snake_case__ : List[str] = re.sub(r'encoder.pos_embed' , 'embeddings.position_embedding' , A__ ) if "encoder.cls_token" in key: snake_case__ : Union[str, Any] = re.sub(r'encoder.cls_token' , 'embeddings.class_embedding' , A__ ) if "self_attn" in key: snake_case__ : int = re.sub(r'self_attn.proj' , 'self_attn.projection' , A__ ) return key @torch.no_grad() def UpperCamelCase__ ( A__ , A__=None ) -> int: if config_path is not None: snake_case__ : Optional[Any] = BlipConfig.from_pretrained(A__ ) else: snake_case__ : str = BlipConfig(projection_dim=512 , text_config={} , vision_config={} ) snake_case__ : Dict = BlipForConditionalGeneration(A__ ).eval() snake_case__ : Tuple = 'https://storage.googleapis.com/sfr-vision-language-research/BLIP/models/model_base_capfilt_large.pth' snake_case__ : Tuple = blip_decoder(pretrained=A__ , image_size=384 , vit='base' ) snake_case__ : Any = pt_model.eval() snake_case__ : Optional[int] = pt_model.state_dict() for key in modified_state_dict.copy(): snake_case__ : int = modified_state_dict.pop(A__ ) snake_case__ : Union[str, Any] = rename_key(A__ ) snake_case__ : Any = value hf_model.load_state_dict(A__ ) snake_case__ : Optional[Any] = 384 snake_case__ : List[str] = load_demo_image(image_size=A__ , device='cpu' ) snake_case__ : Any = BertTokenizer.from_pretrained('bert-base-uncased' ) snake_case__ : Optional[Any] = tokenizer(['a picture of'] ).input_ids snake_case__ : Tuple = hf_model.generate(A__ , A__ ) assert out[0].tolist() == [3_0522, 1037, 3861, 1997, 1037, 2450, 3564, 2006, 1996, 3509, 2007, 2014, 3899, 102] snake_case__ : int = hf_model.generate(A__ ) assert out[0].tolist() == [3_0522, 1037, 2450, 3564, 2006, 1996, 3509, 2007, 2014, 3899, 102] if pytorch_dump_folder_path is not None: hf_model.save_pretrained(A__ ) # model_url = 'https://storage.googleapis.com/sfr-vision-language-research/BLIP/models/model_vqa.pth' snake_case__ : Optional[int] = ( 'https://storage.googleapis.com/sfr-vision-language-research/BLIP/models/model_base_vqa_capfilt_large.pth' ) snake_case__ : str = blip_vqa(pretrained=A__ , image_size=A__ , vit='base' ) vqa_model.eval() snake_case__ : Union[str, Any] = vqa_model.state_dict() for key in modified_state_dict.copy(): snake_case__ : Union[str, Any] = modified_state_dict.pop(A__ ) snake_case__ : Union[str, Any] = rename_key(A__ ) snake_case__ : int = value snake_case__ : str = BlipForQuestionAnswering(A__ ) hf_vqa_model.load_state_dict(A__ ) snake_case__ : Dict = ['How many dogs are in this image?'] snake_case__ : List[str] = tokenizer(A__ , return_tensors='pt' ).input_ids snake_case__ : Any = hf_vqa_model.generate(A__ , A__ ) print(tokenizer.decode(answer[0] ) ) assert tokenizer.decode(answer[0] ) == "[UNK] 1 [SEP]" if pytorch_dump_folder_path is not None: hf_vqa_model.save_pretrained(pytorch_dump_folder_path + '_vqa' ) snake_case__ : int = 'https://storage.googleapis.com/sfr-vision-language-research/BLIP/models/model_base_retrieval_coco.pth' snake_case__ : Optional[Any] = blip_itm(pretrained=A__ , image_size=A__ , vit='base' ) itm_model.eval() snake_case__ : Optional[Any] = itm_model.state_dict() for key in modified_state_dict.copy(): snake_case__ : str = modified_state_dict.pop(A__ ) snake_case__ : List[Any] = rename_key(A__ ) snake_case__ : str = value snake_case__ : int = BlipForImageTextRetrieval(A__ ) snake_case__ : Optional[Any] = ['A picture of a woman with a dog sitting in a beach'] snake_case__ : List[Any] = tokenizer( A__ , return_tensors='pt' , padding='max_length' , truncation=A__ , max_length=35 , ).input_ids hf_itm_model.load_state_dict(A__ ) hf_itm_model.eval() snake_case__ : Optional[Any] = hf_itm_model(A__ , A__ , use_itm_head=A__ ) snake_case__ : str = hf_itm_model(A__ , A__ , use_itm_head=A__ ) assert out[0].item() == 0.2_1_1_0_6_8_7_4_9_4_2_7_7_9_5_4 assert torch.nn.functional.softmax(out_itm[0] , dim=1 )[:, 1].item() == 0.4_5_6_9_8_8_4_5_3_8_6_5_0_5_1_2_7 if pytorch_dump_folder_path is not None: hf_itm_model.save_pretrained(pytorch_dump_folder_path + '_itm' ) if __name__ == "__main__": lowerCAmelCase__ : Optional[int] = argparse.ArgumentParser() parser.add_argument('''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''') parser.add_argument('''--config_path''', default=None, type=str, help='''Path to hf config.json of model to convert''') lowerCAmelCase__ : str = parser.parse_args() convert_blip_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path)
700
from __future__ import annotations from collections.abc import Iterator from typing import Generic, TypeVar lowerCAmelCase__ : Optional[int] = TypeVar('''T''') class __snake_case ( Generic[T] ): def __init__( self , __UpperCamelCase ) -> Any: '''simple docstring''' snake_case__ : Optional[int] = data snake_case__ : Node[T] | None = None def __str__( self ) -> str: '''simple docstring''' return F"""{self.data}""" class __snake_case ( Generic[T] ): def __init__( self ) -> None: '''simple docstring''' snake_case__ : Node[T] | None = None def __iter__( self ) -> Iterator[T]: '''simple docstring''' snake_case__ : str = self.top while node: yield node.data snake_case__ : Dict = node.next def __str__( self ) -> str: '''simple docstring''' return "->".join([str(__UpperCamelCase ) for item in self] ) def __len__( self ) -> int: '''simple docstring''' return len(tuple(iter(self ) ) ) def __a ( self ) -> bool: '''simple docstring''' return self.top is None def __a ( self , __UpperCamelCase ) -> None: '''simple docstring''' snake_case__ : str = Node(__UpperCamelCase ) if not self.is_empty(): snake_case__ : List[str] = self.top snake_case__ : Tuple = node def __a ( self ) -> T: '''simple docstring''' if self.is_empty(): raise IndexError('pop from empty stack' ) assert isinstance(self.top , __UpperCamelCase ) snake_case__ : List[str] = self.top snake_case__ : Union[str, Any] = self.top.next return pop_node.data def __a ( self ) -> T: '''simple docstring''' if self.is_empty(): raise IndexError('peek from empty stack' ) assert self.top is not None return self.top.data def __a ( self ) -> None: '''simple docstring''' snake_case__ : Any = None if __name__ == "__main__": from doctest import testmod testmod()
699
0
from ....configuration_utils import PretrainedConfig from ....utils import logging lowerCAmelCase__ : List[Any] = logging.get_logger(__name__) lowerCAmelCase__ : Any = { '''Visual-Attention-Network/van-base''': ( '''https://huggingface.co/Visual-Attention-Network/van-base/blob/main/config.json''' ), } class __snake_case ( _lowerCamelCase ): __lowerCamelCase = """van""" def __init__( self , __UpperCamelCase=224 , __UpperCamelCase=3 , __UpperCamelCase=[7, 3, 3, 3] , __UpperCamelCase=[4, 2, 2, 2] , __UpperCamelCase=[64, 128, 320, 512] , __UpperCamelCase=[3, 3, 12, 3] , __UpperCamelCase=[8, 8, 4, 4] , __UpperCamelCase="gelu" , __UpperCamelCase=0.0_2 , __UpperCamelCase=1E-6 , __UpperCamelCase=1E-2 , __UpperCamelCase=0.0 , __UpperCamelCase=0.0 , **__UpperCamelCase , ) -> Any: '''simple docstring''' super().__init__(**__UpperCamelCase ) snake_case__ : Dict = image_size snake_case__ : Any = num_channels snake_case__ : Tuple = patch_sizes snake_case__ : Dict = strides snake_case__ : Optional[Any] = hidden_sizes snake_case__ : str = depths snake_case__ : str = mlp_ratios snake_case__ : Optional[int] = hidden_act snake_case__ : Dict = initializer_range snake_case__ : List[str] = layer_norm_eps snake_case__ : Optional[Any] = layer_scale_init_value snake_case__ : Union[str, Any] = drop_path_rate snake_case__ : List[Any] = dropout_rate
701
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__ : Dict = logging.get_logger(__name__) lowerCAmelCase__ : int = { '''sail/poolformer_s12''': '''https://huggingface.co/sail/poolformer_s12/resolve/main/config.json''', # See all PoolFormer models at https://huggingface.co/models?filter=poolformer } class __snake_case ( _lowerCamelCase ): __lowerCamelCase = """poolformer""" def __init__( self , __UpperCamelCase=3 , __UpperCamelCase=16 , __UpperCamelCase=16 , __UpperCamelCase=3 , __UpperCamelCase=4.0 , __UpperCamelCase=[2, 2, 6, 2] , __UpperCamelCase=[64, 128, 320, 512] , __UpperCamelCase=[7, 3, 3, 3] , __UpperCamelCase=[4, 2, 2, 2] , __UpperCamelCase=[2, 1, 1, 1] , __UpperCamelCase=4 , __UpperCamelCase=0.0 , __UpperCamelCase="gelu" , __UpperCamelCase=True , __UpperCamelCase=1E-5 , __UpperCamelCase=0.0_2 , **__UpperCamelCase , ) -> Any: '''simple docstring''' snake_case__ : List[str] = num_channels snake_case__ : Dict = patch_size snake_case__ : Optional[int] = stride snake_case__ : str = padding snake_case__ : List[str] = pool_size snake_case__ : List[Any] = hidden_sizes snake_case__ : List[Any] = mlp_ratio snake_case__ : Union[str, Any] = depths snake_case__ : Dict = patch_sizes snake_case__ : Dict = strides snake_case__ : Dict = num_encoder_blocks snake_case__ : Union[str, Any] = drop_path_rate snake_case__ : List[str] = hidden_act snake_case__ : Optional[Any] = use_layer_scale snake_case__ : int = layer_scale_init_value snake_case__ : Dict = initializer_range super().__init__(**__UpperCamelCase ) class __snake_case ( _lowerCamelCase ): __lowerCamelCase = version.parse("""1.11""" ) @property def __a ( self ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' return OrderedDict( [ ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}), ] ) @property def __a ( self ) -> float: '''simple docstring''' return 2E-3
699
0
import time from dataclasses import dataclass from multiprocessing import Pool from unittest import TestCase from unittest.mock import patch import multiprocess import numpy as np import pytest from datasets.utils.py_utils import ( NestedDataStructure, asdict, iflatmap_unordered, map_nested, temp_seed, temporary_assignment, zip_dict, ) from .utils import require_tf, require_torch def UpperCamelCase__ ( A__ ) -> Optional[Any]: # picklable for multiprocessing return x.sum() def UpperCamelCase__ ( A__ ) -> Dict: # picklable for multiprocessing return i + 1 @dataclass class __snake_case : __lowerCamelCase = 42 __lowerCamelCase = 42 class __snake_case ( _lowerCamelCase ): def __a ( self ) -> List[Any]: '''simple docstring''' snake_case__ : List[Any] = {} snake_case__ : Optional[int] = [] snake_case__ : List[str] = 1 snake_case__ : Tuple = [1, 2] snake_case__ : int = {'a': 1, 'b': 2} snake_case__ : str = {'a': [1, 2], 'b': [3, 4]} snake_case__ : List[str] = {'a': {'1': 1}, 'b': 2} snake_case__ : Union[str, Any] = {'a': 1, 'b': 2, 'c': 3, 'd': 4} snake_case__ : Dict = {} snake_case__ : List[str] = [] snake_case__ : List[str] = 2 snake_case__ : Any = [2, 3] snake_case__ : List[Any] = {'a': 2, 'b': 3} snake_case__ : Union[str, Any] = {'a': [2, 3], 'b': [4, 5]} snake_case__ : List[Any] = {'a': {'1': 2}, 'b': 3} snake_case__ : Any = {'a': 2, 'b': 3, 'c': 4, 'd': 5} self.assertEqual(map_nested(__UpperCamelCase , __UpperCamelCase ) , __UpperCamelCase ) self.assertEqual(map_nested(__UpperCamelCase , __UpperCamelCase ) , __UpperCamelCase ) self.assertEqual(map_nested(__UpperCamelCase , __UpperCamelCase ) , __UpperCamelCase ) self.assertEqual(map_nested(__UpperCamelCase , __UpperCamelCase ) , __UpperCamelCase ) self.assertEqual(map_nested(__UpperCamelCase , __UpperCamelCase ) , __UpperCamelCase ) self.assertEqual(map_nested(__UpperCamelCase , __UpperCamelCase ) , __UpperCamelCase ) self.assertEqual(map_nested(__UpperCamelCase , __UpperCamelCase ) , __UpperCamelCase ) self.assertEqual(map_nested(__UpperCamelCase , __UpperCamelCase ) , __UpperCamelCase ) snake_case__ : List[Any] = 2 self.assertEqual(map_nested(__UpperCamelCase , __UpperCamelCase , num_proc=__UpperCamelCase ) , __UpperCamelCase ) self.assertEqual(map_nested(__UpperCamelCase , __UpperCamelCase , num_proc=__UpperCamelCase ) , __UpperCamelCase ) self.assertEqual(map_nested(__UpperCamelCase , __UpperCamelCase , num_proc=__UpperCamelCase ) , __UpperCamelCase ) self.assertEqual(map_nested(__UpperCamelCase , __UpperCamelCase , num_proc=__UpperCamelCase ) , __UpperCamelCase ) self.assertEqual(map_nested(__UpperCamelCase , __UpperCamelCase , num_proc=__UpperCamelCase ) , __UpperCamelCase ) self.assertEqual(map_nested(__UpperCamelCase , __UpperCamelCase , num_proc=__UpperCamelCase ) , __UpperCamelCase ) self.assertEqual(map_nested(__UpperCamelCase , __UpperCamelCase , num_proc=__UpperCamelCase ) , __UpperCamelCase ) self.assertEqual(map_nested(__UpperCamelCase , __UpperCamelCase , num_proc=__UpperCamelCase ) , __UpperCamelCase ) snake_case__ : Optional[Any] = {'a': np.eye(2 ), 'b': np.zeros(3 ), 'c': np.ones(2 )} snake_case__ : List[Any] = {'a': 2, 'b': 0, 'c': 2} snake_case__ : Union[str, Any] = { 'a': np.eye(2 ).astype(__UpperCamelCase ), 'b': np.zeros(3 ).astype(__UpperCamelCase ), 'c': np.ones(2 ).astype(__UpperCamelCase ), } self.assertEqual(map_nested(__UpperCamelCase , __UpperCamelCase , map_numpy=__UpperCamelCase ) , __UpperCamelCase ) self.assertEqual( {k: v.tolist() for k, v in map_nested(__UpperCamelCase , __UpperCamelCase , map_numpy=__UpperCamelCase ).items()} , {k: v.tolist() for k, v in expected_map_nested_sna_int.items()} , ) self.assertEqual(map_nested(__UpperCamelCase , __UpperCamelCase , map_numpy=__UpperCamelCase , num_proc=__UpperCamelCase ) , __UpperCamelCase ) self.assertEqual( {k: v.tolist() for k, v in map_nested(__UpperCamelCase , __UpperCamelCase , map_numpy=__UpperCamelCase , num_proc=__UpperCamelCase ).items()} , {k: v.tolist() for k, v in expected_map_nested_sna_int.items()} , ) with self.assertRaises(__UpperCamelCase ): # can't pickle a local lambda map_nested(lambda __UpperCamelCase : x + 1 , __UpperCamelCase , num_proc=__UpperCamelCase ) def __a ( self ) -> int: '''simple docstring''' snake_case__ : List[Any] = {'a': 1, 'b': 2} snake_case__ : int = {'a': 3, 'b': 4} snake_case__ : Dict = {'a': 5, 'b': 6} snake_case__ : Any = sorted([('a', (1, 3, 5)), ('b', (2, 4, 6))] ) self.assertEqual(sorted(zip_dict(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) ) , __UpperCamelCase ) def __a ( self ) -> int: '''simple docstring''' class __snake_case : __lowerCamelCase = """bar""" snake_case__ : Tuple = Foo() self.assertEqual(foo.my_attr , 'bar' ) with temporary_assignment(__UpperCamelCase , 'my_attr' , 'BAR' ): self.assertEqual(foo.my_attr , 'BAR' ) self.assertEqual(foo.my_attr , 'bar' ) @pytest.mark.parametrize( 'iterable_length, num_proc, expected_num_proc' , [ (1, None, 1), (1, 1, 1), (2, None, 1), (2, 1, 1), (2, 2, 1), (2, 3, 1), (3, 2, 1), (16, 16, 16), (16, 17, 16), (17, 16, 16), ] , ) def UpperCamelCase__ ( A__ , A__ , A__ ) -> List[str]: with patch('datasets.utils.py_utils._single_map_nested' ) as mock_single_map_nested, patch( 'datasets.parallel.parallel.Pool' ) as mock_multiprocessing_pool: snake_case__ : Tuple = {F"""{i}""": i for i in range(A__ )} snake_case__ : List[Any] = map_nested(lambda A__ : x + 10 , A__ , num_proc=A__ , parallel_min_length=16 ) if expected_num_proc == 1: assert mock_single_map_nested.called assert not mock_multiprocessing_pool.called else: assert not mock_single_map_nested.called assert mock_multiprocessing_pool.called assert mock_multiprocessing_pool.call_args[0][0] == expected_num_proc class __snake_case ( _lowerCamelCase ): @require_tf def __a ( self ) -> str: '''simple docstring''' import tensorflow as tf from tensorflow.keras import layers snake_case__ : List[str] = layers.Dense(2 ) def gen_random_output(): snake_case__ : List[str] = tf.random.uniform((1, 3) ) return model(__UpperCamelCase ).numpy() with temp_seed(42 , set_tensorflow=__UpperCamelCase ): snake_case__ : Optional[int] = gen_random_output() with temp_seed(42 , set_tensorflow=__UpperCamelCase ): snake_case__ : Optional[int] = gen_random_output() snake_case__ : Dict = gen_random_output() np.testing.assert_equal(__UpperCamelCase , __UpperCamelCase ) self.assertGreater(np.abs(outa - outa ).sum() , 0 ) @require_torch def __a ( self ) -> Union[str, Any]: '''simple docstring''' import torch def gen_random_output(): snake_case__ : Dict = torch.nn.Linear(3 , 2 ) snake_case__ : List[Any] = torch.rand(1 , 3 ) return model(__UpperCamelCase ).detach().numpy() with temp_seed(42 , set_pytorch=__UpperCamelCase ): snake_case__ : Dict = gen_random_output() with temp_seed(42 , set_pytorch=__UpperCamelCase ): snake_case__ : Any = gen_random_output() snake_case__ : Optional[int] = gen_random_output() np.testing.assert_equal(__UpperCamelCase , __UpperCamelCase ) self.assertGreater(np.abs(outa - outa ).sum() , 0 ) def __a ( self ) -> List[Any]: '''simple docstring''' def gen_random_output(): return np.random.rand(1 , 3 ) with temp_seed(42 ): snake_case__ : List[str] = gen_random_output() with temp_seed(42 ): snake_case__ : Tuple = gen_random_output() snake_case__ : List[str] = gen_random_output() np.testing.assert_equal(__UpperCamelCase , __UpperCamelCase ) self.assertGreater(np.abs(outa - outa ).sum() , 0 ) @pytest.mark.parametrize('input_data' , [{}] ) def UpperCamelCase__ ( A__ ) -> List[Any]: snake_case__ : Union[str, Any] = NestedDataStructure(A__ ).data assert output_data == input_data @pytest.mark.parametrize( 'data, expected_output' , [ ({}, []), ([], []), ('foo', ['foo']), (['foo', 'bar'], ['foo', 'bar']), ([['foo', 'bar']], ['foo', 'bar']), ([[['foo'], ['bar']]], ['foo', 'bar']), ([[['foo'], 'bar']], ['foo', 'bar']), ({'a': 1, 'b': 2}, [1, 2]), ({'a': [1, 2], 'b': [3, 4]}, [1, 2, 3, 4]), ({'a': [[1, 2]], 'b': [[3, 4]]}, [1, 2, 3, 4]), ({'a': [[1, 2]], 'b': [3, 4]}, [1, 2, 3, 4]), ({'a': [[[1], [2]]], 'b': [[[3], [4]]]}, [1, 2, 3, 4]), ({'a': [[[1], [2]]], 'b': [[3, 4]]}, [1, 2, 3, 4]), ({'a': [[[1], [2]]], 'b': [3, 4]}, [1, 2, 3, 4]), ({'a': [[[1], [2]]], 'b': [3, [4]]}, [1, 2, 3, 4]), ({'a': {'1': 1}, 'b': 2}, [1, 2]), ({'a': {'1': [1]}, 'b': 2}, [1, 2]), ({'a': {'1': [1]}, 'b': [2]}, [1, 2]), ] , ) def UpperCamelCase__ ( A__ , A__ ) -> Tuple: snake_case__ : str = NestedDataStructure(A__ ).flatten() assert output == expected_output def UpperCamelCase__ ( ) -> str: snake_case__ : List[str] = A(x=1 , y='foobar' ) snake_case__ : List[Any] = {'x': 1, 'y': 'foobar'} assert asdict(A__ ) == expected_output snake_case__ : Optional[Any] = {'a': {'b': A(x=10 , y='foo' )}, 'c': [A(x=20 , y='bar' )]} snake_case__ : Optional[int] = {'a': {'b': {'x': 10, 'y': 'foo'}}, 'c': [{'x': 20, 'y': 'bar'}]} assert asdict(A__ ) == expected_output with pytest.raises(A__ ): asdict([1, A(x=10 , y='foo' )] ) def UpperCamelCase__ ( A__ ) -> Optional[int]: return text.split() def UpperCamelCase__ ( A__ ) -> List[str]: yield (time.time(), content) time.sleep(2 ) yield (time.time(), content) def UpperCamelCase__ ( ) -> str: with Pool(2 ) as pool: snake_case__ : Tuple = list(iflatmap_unordered(A__ , _split_text , kwargs_iterable=[{'text': 'hello there'}] * 10 ) ) assert out.count('hello' ) == 10 assert out.count('there' ) == 10 assert len(A__ ) == 20 # check multiprocess from pathos (uses dill for pickling) with multiprocess.Pool(2 ) as pool: snake_case__ : Union[str, Any] = list(iflatmap_unordered(A__ , _split_text , kwargs_iterable=[{'text': 'hello there'}] * 10 ) ) assert out.count('hello' ) == 10 assert out.count('there' ) == 10 assert len(A__ ) == 20 # check that we get items as fast as possible with Pool(2 ) as pool: snake_case__ : Any = [] for yield_time, content in iflatmap_unordered( A__ , _aseconds_generator_of_aitems_with_timing , kwargs_iterable=[{'content': 'a'}, {'content': 'b'}] ): assert yield_time < time.time() + 0.1, "we should each item directly after it was yielded" out.append(A__ ) assert out.count('a' ) == 2 assert out.count('b' ) == 2 assert len(A__ ) == 4
702
import numpy as np import qiskit def UpperCamelCase__ ( A__ = 8 , A__ = None ) -> str: snake_case__ : Optional[int] = np.random.default_rng(seed=A__ ) # Roughly 25% of the qubits will contribute to the key. # So we take more than we need. snake_case__ : Tuple = 6 * key_len # Measurement basis for Alice's qubits. snake_case__ : Tuple = rng.integers(2 , size=A__ ) # The set of states Alice will prepare. snake_case__ : List[str] = rng.integers(2 , size=A__ ) # Measurement basis for Bob's qubits. snake_case__ : List[Any] = rng.integers(2 , size=A__ ) # Quantum Circuit to simulate BB84 snake_case__ : Any = qiskit.QuantumCircuit(A__ , name='BB84' ) # Alice prepares her qubits according to rules above. for index, _ in enumerate(A__ ): if alice_state[index] == 1: bbaa_circ.x(A__ ) if alice_basis[index] == 1: bbaa_circ.h(A__ ) bbaa_circ.barrier() # Bob measures the received qubits according to rules above. for index, _ in enumerate(A__ ): if bob_basis[index] == 1: bbaa_circ.h(A__ ) bbaa_circ.barrier() bbaa_circ.measure_all() # Simulate the quantum circuit. snake_case__ : List[str] = qiskit.Aer.get_backend('aer_simulator' ) # We only need to run one shot because the key is unique. # Multiple shots will produce the same key. snake_case__ : Optional[Any] = qiskit.execute(A__ , A__ , shots=1 , seed_simulator=A__ ) # Returns the result of measurement. snake_case__ : Union[str, Any] = job.result().get_counts(A__ ).most_frequent() # Extracting the generated key from the simulation results. # Only keep measurement results where Alice and Bob chose the same basis. snake_case__ : Optional[Any] = ''.join( [ result_bit for alice_basis_bit, bob_basis_bit, result_bit in zip( A__ , A__ , A__ ) if alice_basis_bit == bob_basis_bit ] ) # Get final key. Pad with 0 if too short, otherwise truncate. snake_case__ : Tuple = gen_key[:key_len] if len(A__ ) >= key_len else gen_key.ljust(A__ , '0' ) return key if __name__ == "__main__": print(F'''The generated key is : {bbaa(8, seed=0)}''') from doctest import testmod testmod()
699
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. import torch from accelerate import PartialState from accelerate.utils.operations import broadcast, gather, gather_object, pad_across_processes, reduce def UpperCamelCase__ ( A__ ) -> Tuple: return (torch.arange(state.num_processes ) + 1.0 + (state.num_processes * state.process_index)).to(state.device ) def UpperCamelCase__ ( A__ ) -> Tuple: snake_case__ : Dict = create_tensor(A__ ) snake_case__ : List[Any] = gather(A__ ) assert gathered_tensor.tolist() == list(range(1 , state.num_processes**2 + 1 ) ) def UpperCamelCase__ ( A__ ) -> Dict: snake_case__ : List[str] = [state.process_index] snake_case__ : int = gather_object(A__ ) assert len(A__ ) == state.num_processes, F"""{gathered_obj}, {len(A__ )} != {state.num_processes}""" assert gathered_obj == list(range(state.num_processes ) ), F"""{gathered_obj} != {list(range(state.num_processes ) )}""" def UpperCamelCase__ ( A__ ) -> List[Any]: snake_case__ : Any = create_tensor(A__ ) snake_case__ : List[Any] = broadcast(A__ ) assert broadcasted_tensor.shape == torch.Size([state.num_processes] ) assert broadcasted_tensor.tolist() == list(range(1 , state.num_processes + 1 ) ) def UpperCamelCase__ ( A__ ) -> str: # We need to pad the tensor with one more element if we are the main process # to ensure that we can pad if state.is_main_process: snake_case__ : Any = torch.arange(state.num_processes + 1 ).to(state.device ) else: snake_case__ : List[Any] = torch.arange(state.num_processes ).to(state.device ) snake_case__ : List[str] = pad_across_processes(A__ ) assert padded_tensor.shape == torch.Size([state.num_processes + 1] ) if not state.is_main_process: assert padded_tensor.tolist() == list(range(0 , state.num_processes ) ) + [0] def UpperCamelCase__ ( A__ ) -> str: # For now runs on only two processes if state.num_processes != 2: return snake_case__ : List[Any] = create_tensor(A__ ) snake_case__ : Any = reduce(A__ , 'sum' ) snake_case__ : Any = torch.tensor([4.0, 6] ).to(state.device ) assert torch.allclose(A__ , A__ ), F"""{reduced_tensor} != {truth_tensor}""" def UpperCamelCase__ ( A__ ) -> Optional[int]: # For now runs on only two processes if state.num_processes != 2: return snake_case__ : int = create_tensor(A__ ) snake_case__ : List[Any] = reduce(A__ , 'mean' ) snake_case__ : str = torch.tensor([2.0, 3] ).to(state.device ) assert torch.allclose(A__ , A__ ), F"""{reduced_tensor} != {truth_tensor}""" def UpperCamelCase__ ( A__ ) -> str: # For xla_spawn (TPUs) main() def UpperCamelCase__ ( ) -> str: snake_case__ : Optional[Any] = PartialState() state.print(F"""State: {state}""" ) state.print('testing gather' ) test_gather(A__ ) state.print('testing gather_object' ) test_gather_object(A__ ) state.print('testing broadcast' ) test_broadcast(A__ ) state.print('testing pad_across_processes' ) test_pad_across_processes(A__ ) state.print('testing reduce_sum' ) test_reduce_sum(A__ ) state.print('testing reduce_mean' ) test_reduce_mean(A__ ) if __name__ == "__main__": main()
703
def UpperCamelCase__ ( A__ , A__ , A__ ) -> int: if exponent == 1: return base if exponent % 2 == 0: snake_case__ : Dict = _modexpt(A__ , exponent // 2 , A__ ) % modulo_value return (x * x) % modulo_value else: return (base * _modexpt(A__ , exponent - 1 , A__ )) % modulo_value def UpperCamelCase__ ( A__ = 1777 , A__ = 1855 , A__ = 8 ) -> int: snake_case__ : Tuple = base for _ in range(1 , A__ ): snake_case__ : Any = _modexpt(A__ , A__ , 10**digits ) return result if __name__ == "__main__": print(F'''{solution() = }''')
699
0
import math import os import sys def UpperCamelCase__ ( A__ ) -> str: '''simple docstring''' snake_case__ : str = '' try: with open(A__ , 'rb' ) as binary_file: snake_case__ : Optional[Any] = binary_file.read() for dat in data: snake_case__ : Tuple = F"""{dat:08b}""" result += curr_byte return result except OSError: print('File not accessible' ) sys.exit() def UpperCamelCase__ ( A__ , A__ , A__ , A__ ) -> None: '''simple docstring''' lexicon.pop(A__ ) snake_case__ : Any = last_match_id if math.loga(A__ ).is_integer(): for curr_key in lexicon: snake_case__ : int = '0' + lexicon[curr_key] snake_case__ : Optional[int] = bin(A__ )[2:] def UpperCamelCase__ ( A__ ) -> str: '''simple docstring''' snake_case__ : str = {'0': '0', '1': '1'} snake_case__ : Dict = '', '' snake_case__ : str = len(A__ ) for i in range(len(A__ ) ): curr_string += data_bits[i] if curr_string not in lexicon: continue snake_case__ : List[str] = lexicon[curr_string] result += last_match_id add_key_to_lexicon(A__ , A__ , A__ , A__ ) index += 1 snake_case__ : List[Any] = '' while curr_string != "" and curr_string not in lexicon: curr_string += "0" if curr_string != "": snake_case__ : str = lexicon[curr_string] result += last_match_id return result def UpperCamelCase__ ( A__ , A__ ) -> str: '''simple docstring''' snake_case__ : Tuple = os.path.getsize(A__ ) snake_case__ : Optional[int] = bin(A__ )[2:] snake_case__ : str = len(A__ ) return "0" * (length_length - 1) + file_length_binary + compressed def UpperCamelCase__ ( A__ , A__ ) -> None: '''simple docstring''' snake_case__ : Union[str, Any] = 8 try: with open(A__ , 'wb' ) as opened_file: snake_case__ : Tuple = [ to_write[i : i + byte_length] for i in range(0 , len(A__ ) , A__ ) ] if len(result_byte_array[-1] ) % byte_length == 0: result_byte_array.append('10000000' ) else: result_byte_array[-1] += "1" + "0" * ( byte_length - len(result_byte_array[-1] ) - 1 ) for elem in result_byte_array: opened_file.write(int(A__ , 2 ).to_bytes(1 , byteorder='big' ) ) except OSError: print('File not accessible' ) sys.exit() def UpperCamelCase__ ( A__ , A__ ) -> None: '''simple docstring''' snake_case__ : List[Any] = read_file_binary(A__ ) snake_case__ : Optional[Any] = compress_data(A__ ) snake_case__ : Optional[int] = add_file_length(A__ , A__ ) write_file_binary(A__ , A__ ) if __name__ == "__main__": compress(sys.argv[1], sys.argv[2])
704
# tests directory-specific settings - this file is run automatically # by pytest before any tests are run import sys import warnings from os.path import abspath, dirname, join # allow having multiple repository checkouts and not needing to remember to rerun # 'pip install -e .[dev]' when switching between checkouts and running tests. lowerCAmelCase__ : Tuple = abspath(join(dirname(dirname(__file__)), '''src''')) sys.path.insert(1, git_repo_path) # silence FutureWarning warnings in tests since often we can't act on them until # they become normal warnings - i.e. the tests still need to test the current functionality warnings.simplefilter(action='''ignore''', category=FutureWarning) def UpperCamelCase__ ( A__ ) -> Optional[Any]: from diffusers.utils.testing_utils import pytest_addoption_shared pytest_addoption_shared(A__ ) def UpperCamelCase__ ( A__ ) -> Optional[Any]: from diffusers.utils.testing_utils import pytest_terminal_summary_main snake_case__ : Union[str, Any] = terminalreporter.config.getoption('--make-reports' ) if make_reports: pytest_terminal_summary_main(A__ , id=A__ )
699
0
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, ) if is_sentencepiece_available(): from ..ta.tokenization_ta import TaTokenizer else: from ...utils.dummy_sentencepiece_objects import TaTokenizer lowerCAmelCase__ : Dict = TaTokenizer if is_tokenizers_available(): from ..ta.tokenization_ta_fast import TaTokenizerFast else: from ...utils.dummy_tokenizers_objects import TaTokenizerFast lowerCAmelCase__ : List[Any] = TaTokenizerFast lowerCAmelCase__ : Union[str, Any] = {'''configuration_mt5''': ['''MT5Config''', '''MT5OnnxConfig''']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : Optional[int] = [ '''MT5EncoderModel''', '''MT5ForConditionalGeneration''', '''MT5ForQuestionAnswering''', '''MT5Model''', '''MT5PreTrainedModel''', '''MT5Stack''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : List[Any] = ['''TFMT5EncoderModel''', '''TFMT5ForConditionalGeneration''', '''TFMT5Model'''] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : int = ['''FlaxMT5EncoderModel''', '''FlaxMT5ForConditionalGeneration''', '''FlaxMT5Model'''] if TYPE_CHECKING: from .configuration_mta import MTaConfig, MTaOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mta import ( MTaEncoderModel, MTaForConditionalGeneration, MTaForQuestionAnswering, MTaModel, MTaPreTrainedModel, MTaStack, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_mta import TFMTaEncoderModel, TFMTaForConditionalGeneration, TFMTaModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_mta import FlaxMTaEncoderModel, FlaxMTaForConditionalGeneration, FlaxMTaModel else: import sys lowerCAmelCase__ : Tuple = _LazyModule( __name__, globals()['''__file__'''], _import_structure, extra_objects={'''MT5Tokenizer''': MTaTokenizer, '''MT5TokenizerFast''': MTaTokenizerFast}, module_spec=__spec__, )
705
def UpperCamelCase__ ( A__ ) -> list[int]: if length <= 0 or not isinstance(A__ , A__ ): raise ValueError('Length must be a positive integer.' ) return [n * (2 * n - 1) for n in range(A__ )] if __name__ == "__main__": print(hexagonal_numbers(length=5)) print(hexagonal_numbers(length=10))
699
0
def UpperCamelCase__ ( A__ ) -> str: # noqa: E741 snake_case__ : List[str] = len(A__ ) snake_case__ : List[str] = 0 snake_case__ : Optional[Any] = [0] * n snake_case__ : Optional[int] = [False] * n snake_case__ : Dict = [False] * n def dfs(A__ , A__ , A__ , A__ ): if parent == root: out_edge_count += 1 snake_case__ : int = True snake_case__ : int = at for to in l[at]: if to == parent: pass elif not visited[to]: snake_case__ : Optional[int] = dfs(A__ , A__ , A__ , A__ ) snake_case__ : Tuple = min(low[at] , low[to] ) # AP found via bridge if at < low[to]: snake_case__ : Tuple = True # AP found via cycle if at == low[to]: snake_case__ : List[Any] = True else: snake_case__ : Tuple = min(low[at] , A__ ) return out_edge_count for i in range(A__ ): if not visited[i]: snake_case__ : List[str] = 0 snake_case__ : str = dfs(A__ , A__ , -1 , A__ ) snake_case__ : List[Any] = out_edge_count > 1 for x in range(len(A__ ) ): if is_art[x] is True: print(A__ ) # Adjacency list of graph lowerCAmelCase__ : int = { 0: [1, 2], 1: [0, 2], 2: [0, 1, 3, 5], 3: [2, 4], 4: [3], 5: [2, 6, 8], 6: [5, 7], 7: [6, 8], 8: [5, 7], } compute_ap(data)
706
import argparse import json from collections import OrderedDict from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( ConditionalDetrConfig, ConditionalDetrForObjectDetection, ConditionalDetrForSegmentation, ConditionalDetrImageProcessor, ) from transformers.utils import logging logging.set_verbosity_info() lowerCAmelCase__ : Dict = logging.get_logger(__name__) # here we list all keys to be renamed (original name on the left, our name on the right) lowerCAmelCase__ : Optional[Any] = [] for i in range(6): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append( (F'''transformer.encoder.layers.{i}.self_attn.out_proj.weight''', F'''encoder.layers.{i}.self_attn.out_proj.weight''') ) rename_keys.append( (F'''transformer.encoder.layers.{i}.self_attn.out_proj.bias''', F'''encoder.layers.{i}.self_attn.out_proj.bias''') ) rename_keys.append((F'''transformer.encoder.layers.{i}.linear1.weight''', F'''encoder.layers.{i}.fc1.weight''')) rename_keys.append((F'''transformer.encoder.layers.{i}.linear1.bias''', F'''encoder.layers.{i}.fc1.bias''')) rename_keys.append((F'''transformer.encoder.layers.{i}.linear2.weight''', F'''encoder.layers.{i}.fc2.weight''')) rename_keys.append((F'''transformer.encoder.layers.{i}.linear2.bias''', F'''encoder.layers.{i}.fc2.bias''')) rename_keys.append( (F'''transformer.encoder.layers.{i}.norm1.weight''', F'''encoder.layers.{i}.self_attn_layer_norm.weight''') ) rename_keys.append((F'''transformer.encoder.layers.{i}.norm1.bias''', F'''encoder.layers.{i}.self_attn_layer_norm.bias''')) rename_keys.append((F'''transformer.encoder.layers.{i}.norm2.weight''', F'''encoder.layers.{i}.final_layer_norm.weight''')) rename_keys.append((F'''transformer.encoder.layers.{i}.norm2.bias''', F'''encoder.layers.{i}.final_layer_norm.bias''')) # decoder layers: 2 times output projection, 2 feedforward neural networks and 3 layernorms rename_keys.append( (F'''transformer.decoder.layers.{i}.self_attn.out_proj.weight''', F'''decoder.layers.{i}.self_attn.out_proj.weight''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.self_attn.out_proj.bias''', F'''decoder.layers.{i}.self_attn.out_proj.bias''') ) rename_keys.append( ( F'''transformer.decoder.layers.{i}.cross_attn.out_proj.weight''', F'''decoder.layers.{i}.encoder_attn.out_proj.weight''', ) ) rename_keys.append( ( F'''transformer.decoder.layers.{i}.cross_attn.out_proj.bias''', F'''decoder.layers.{i}.encoder_attn.out_proj.bias''', ) ) rename_keys.append((F'''transformer.decoder.layers.{i}.linear1.weight''', F'''decoder.layers.{i}.fc1.weight''')) rename_keys.append((F'''transformer.decoder.layers.{i}.linear1.bias''', F'''decoder.layers.{i}.fc1.bias''')) rename_keys.append((F'''transformer.decoder.layers.{i}.linear2.weight''', F'''decoder.layers.{i}.fc2.weight''')) rename_keys.append((F'''transformer.decoder.layers.{i}.linear2.bias''', F'''decoder.layers.{i}.fc2.bias''')) rename_keys.append( (F'''transformer.decoder.layers.{i}.norm1.weight''', F'''decoder.layers.{i}.self_attn_layer_norm.weight''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.norm1.bias''', F'''decoder.layers.{i}.self_attn_layer_norm.bias''')) rename_keys.append( (F'''transformer.decoder.layers.{i}.norm2.weight''', F'''decoder.layers.{i}.encoder_attn_layer_norm.weight''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.norm2.bias''', F'''decoder.layers.{i}.encoder_attn_layer_norm.bias''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.norm3.weight''', F'''decoder.layers.{i}.final_layer_norm.weight''')) rename_keys.append((F'''transformer.decoder.layers.{i}.norm3.bias''', F'''decoder.layers.{i}.final_layer_norm.bias''')) # q, k, v projections in self/cross-attention in decoder for conditional DETR rename_keys.append( (F'''transformer.decoder.layers.{i}.sa_qcontent_proj.weight''', F'''decoder.layers.{i}.sa_qcontent_proj.weight''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.sa_kcontent_proj.weight''', F'''decoder.layers.{i}.sa_kcontent_proj.weight''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.sa_qpos_proj.weight''', F'''decoder.layers.{i}.sa_qpos_proj.weight''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.sa_kpos_proj.weight''', F'''decoder.layers.{i}.sa_kpos_proj.weight''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.sa_v_proj.weight''', F'''decoder.layers.{i}.sa_v_proj.weight''')) rename_keys.append( (F'''transformer.decoder.layers.{i}.ca_qcontent_proj.weight''', F'''decoder.layers.{i}.ca_qcontent_proj.weight''') ) # rename_keys.append((f"transformer.decoder.layers.{i}.ca_qpos_proj.weight", f"decoder.layers.{i}.ca_qpos_proj.weight")) rename_keys.append( (F'''transformer.decoder.layers.{i}.ca_kcontent_proj.weight''', F'''decoder.layers.{i}.ca_kcontent_proj.weight''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.ca_kpos_proj.weight''', F'''decoder.layers.{i}.ca_kpos_proj.weight''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.ca_v_proj.weight''', F'''decoder.layers.{i}.ca_v_proj.weight''')) rename_keys.append( (F'''transformer.decoder.layers.{i}.ca_qpos_sine_proj.weight''', F'''decoder.layers.{i}.ca_qpos_sine_proj.weight''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.sa_qcontent_proj.bias''', F'''decoder.layers.{i}.sa_qcontent_proj.bias''') ) rename_keys.append( (F'''transformer.decoder.layers.{i}.sa_kcontent_proj.bias''', F'''decoder.layers.{i}.sa_kcontent_proj.bias''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.sa_qpos_proj.bias''', F'''decoder.layers.{i}.sa_qpos_proj.bias''')) rename_keys.append((F'''transformer.decoder.layers.{i}.sa_kpos_proj.bias''', F'''decoder.layers.{i}.sa_kpos_proj.bias''')) rename_keys.append((F'''transformer.decoder.layers.{i}.sa_v_proj.bias''', F'''decoder.layers.{i}.sa_v_proj.bias''')) rename_keys.append( (F'''transformer.decoder.layers.{i}.ca_qcontent_proj.bias''', F'''decoder.layers.{i}.ca_qcontent_proj.bias''') ) # rename_keys.append((f"transformer.decoder.layers.{i}.ca_qpos_proj.bias", f"decoder.layers.{i}.ca_qpos_proj.bias")) rename_keys.append( (F'''transformer.decoder.layers.{i}.ca_kcontent_proj.bias''', F'''decoder.layers.{i}.ca_kcontent_proj.bias''') ) rename_keys.append((F'''transformer.decoder.layers.{i}.ca_kpos_proj.bias''', F'''decoder.layers.{i}.ca_kpos_proj.bias''')) rename_keys.append((F'''transformer.decoder.layers.{i}.ca_v_proj.bias''', F'''decoder.layers.{i}.ca_v_proj.bias''')) rename_keys.append( (F'''transformer.decoder.layers.{i}.ca_qpos_sine_proj.bias''', F'''decoder.layers.{i}.ca_qpos_sine_proj.bias''') ) # convolutional projection + query embeddings + layernorm of decoder + class and bounding box heads # for conditional DETR, also convert reference point head and query scale MLP rename_keys.extend( [ ('''input_proj.weight''', '''input_projection.weight'''), ('''input_proj.bias''', '''input_projection.bias'''), ('''query_embed.weight''', '''query_position_embeddings.weight'''), ('''transformer.decoder.norm.weight''', '''decoder.layernorm.weight'''), ('''transformer.decoder.norm.bias''', '''decoder.layernorm.bias'''), ('''class_embed.weight''', '''class_labels_classifier.weight'''), ('''class_embed.bias''', '''class_labels_classifier.bias'''), ('''bbox_embed.layers.0.weight''', '''bbox_predictor.layers.0.weight'''), ('''bbox_embed.layers.0.bias''', '''bbox_predictor.layers.0.bias'''), ('''bbox_embed.layers.1.weight''', '''bbox_predictor.layers.1.weight'''), ('''bbox_embed.layers.1.bias''', '''bbox_predictor.layers.1.bias'''), ('''bbox_embed.layers.2.weight''', '''bbox_predictor.layers.2.weight'''), ('''bbox_embed.layers.2.bias''', '''bbox_predictor.layers.2.bias'''), ('''transformer.decoder.ref_point_head.layers.0.weight''', '''decoder.ref_point_head.layers.0.weight'''), ('''transformer.decoder.ref_point_head.layers.0.bias''', '''decoder.ref_point_head.layers.0.bias'''), ('''transformer.decoder.ref_point_head.layers.1.weight''', '''decoder.ref_point_head.layers.1.weight'''), ('''transformer.decoder.ref_point_head.layers.1.bias''', '''decoder.ref_point_head.layers.1.bias'''), ('''transformer.decoder.query_scale.layers.0.weight''', '''decoder.query_scale.layers.0.weight'''), ('''transformer.decoder.query_scale.layers.0.bias''', '''decoder.query_scale.layers.0.bias'''), ('''transformer.decoder.query_scale.layers.1.weight''', '''decoder.query_scale.layers.1.weight'''), ('''transformer.decoder.query_scale.layers.1.bias''', '''decoder.query_scale.layers.1.bias'''), ('''transformer.decoder.layers.0.ca_qpos_proj.weight''', '''decoder.layers.0.ca_qpos_proj.weight'''), ('''transformer.decoder.layers.0.ca_qpos_proj.bias''', '''decoder.layers.0.ca_qpos_proj.bias'''), ] ) def UpperCamelCase__ ( A__ , A__ , A__ ) -> List[str]: snake_case__ : int = state_dict.pop(A__ ) snake_case__ : Union[str, Any] = val def UpperCamelCase__ ( A__ ) -> int: snake_case__ : List[Any] = OrderedDict() for key, value in state_dict.items(): if "backbone.0.body" in key: snake_case__ : Any = key.replace('backbone.0.body' , 'backbone.conv_encoder.model' ) snake_case__ : Optional[int] = value else: snake_case__ : Optional[int] = value return new_state_dict def UpperCamelCase__ ( A__ , A__=False ) -> Optional[int]: snake_case__ : Optional[int] = '' if is_panoptic: snake_case__ : Tuple = 'conditional_detr.' # first: transformer encoder for i in range(6 ): # read in weights + bias of input projection layer (in PyTorch's MultiHeadAttention, this is a single matrix + bias) snake_case__ : int = state_dict.pop(F"""{prefix}transformer.encoder.layers.{i}.self_attn.in_proj_weight""" ) snake_case__ : str = state_dict.pop(F"""{prefix}transformer.encoder.layers.{i}.self_attn.in_proj_bias""" ) # next, add query, keys and values (in that order) to the state dict snake_case__ : Union[str, Any] = in_proj_weight[:256, :] snake_case__ : Union[str, Any] = in_proj_bias[:256] snake_case__ : Union[str, Any] = in_proj_weight[256:512, :] snake_case__ : Optional[Any] = in_proj_bias[256:512] snake_case__ : List[str] = in_proj_weight[-256:, :] snake_case__ : Tuple = in_proj_bias[-256:] def UpperCamelCase__ ( ) -> Tuple: snake_case__ : int = 'http://images.cocodataset.org/val2017/000000039769.jpg' snake_case__ : str = Image.open(requests.get(A__ , stream=A__ ).raw ) return im @torch.no_grad() def UpperCamelCase__ ( A__ , A__ ) -> str: snake_case__ : List[Any] = ConditionalDetrConfig() # set backbone and dilation attributes if "resnet101" in model_name: snake_case__ : Any = 'resnet101' if "dc5" in model_name: snake_case__ : Any = True snake_case__ : int = 'panoptic' in model_name if is_panoptic: snake_case__ : str = 250 else: snake_case__ : Union[str, Any] = 91 snake_case__ : Optional[int] = 'huggingface/label-files' snake_case__ : Optional[Any] = 'coco-detection-id2label.json' snake_case__ : str = json.load(open(hf_hub_download(A__ , A__ , repo_type='dataset' ) , 'r' ) ) snake_case__ : List[Any] = {int(A__ ): v for k, v in idalabel.items()} snake_case__ : Any = idalabel snake_case__ : int = {v: k for k, v in idalabel.items()} # load image processor snake_case__ : List[Any] = 'coco_panoptic' if is_panoptic else 'coco_detection' snake_case__ : List[Any] = ConditionalDetrImageProcessor(format=A__ ) # prepare image snake_case__ : List[str] = prepare_img() snake_case__ : Any = image_processor(images=A__ , return_tensors='pt' ) snake_case__ : Dict = encoding['pixel_values'] logger.info(F"""Converting model {model_name}...""" ) # load original model from torch hub snake_case__ : Any = torch.hub.load('DeppMeng/ConditionalDETR' , A__ , pretrained=A__ ).eval() snake_case__ : Tuple = conditional_detr.state_dict() # rename keys for src, dest in rename_keys: if is_panoptic: snake_case__ : List[Any] = 'conditional_detr.' + src rename_key(A__ , A__ , A__ ) snake_case__ : Dict = rename_backbone_keys(A__ ) # query, key and value matrices need special treatment read_in_q_k_v(A__ , is_panoptic=A__ ) # important: we need to prepend a prefix to each of the base model keys as the head models use different attributes for them snake_case__ : Optional[int] = 'conditional_detr.model.' if is_panoptic else 'model.' for key in state_dict.copy().keys(): if is_panoptic: if ( key.startswith('conditional_detr' ) and not key.startswith('class_labels_classifier' ) and not key.startswith('bbox_predictor' ) ): snake_case__ : List[Any] = state_dict.pop(A__ ) snake_case__ : Optional[int] = val elif "class_labels_classifier" in key or "bbox_predictor" in key: snake_case__ : str = state_dict.pop(A__ ) snake_case__ : List[Any] = val elif key.startswith('bbox_attention' ) or key.startswith('mask_head' ): continue else: snake_case__ : Union[str, Any] = state_dict.pop(A__ ) snake_case__ : Dict = val else: if not key.startswith('class_labels_classifier' ) and not key.startswith('bbox_predictor' ): snake_case__ : List[Any] = state_dict.pop(A__ ) snake_case__ : Optional[int] = val # finally, create HuggingFace model and load state dict snake_case__ : Union[str, Any] = ConditionalDetrForSegmentation(A__ ) if is_panoptic else ConditionalDetrForObjectDetection(A__ ) model.load_state_dict(A__ ) model.eval() model.push_to_hub(repo_id=A__ , organization='DepuMeng' , commit_message='Add model' ) # verify our conversion snake_case__ : Tuple = conditional_detr(A__ ) snake_case__ : str = model(A__ ) assert torch.allclose(outputs.logits , original_outputs['pred_logits'] , atol=1e-4 ) assert torch.allclose(outputs.pred_boxes , original_outputs['pred_boxes'] , atol=1e-4 ) if is_panoptic: assert torch.allclose(outputs.pred_masks , original_outputs['pred_masks'] , atol=1e-4 ) # Save model and image processor logger.info(F"""Saving PyTorch model and image processor to {pytorch_dump_folder_path}...""" ) Path(A__ ).mkdir(exist_ok=A__ ) model.save_pretrained(A__ ) image_processor.save_pretrained(A__ ) if __name__ == "__main__": lowerCAmelCase__ : Any = argparse.ArgumentParser() parser.add_argument( '''--model_name''', default='''conditional_detr_resnet50''', type=str, help='''Name of the CONDITIONAL_DETR model you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the folder to output PyTorch model.''' ) lowerCAmelCase__ : int = parser.parse_args() convert_conditional_detr_checkpoint(args.model_name, args.pytorch_dump_folder_path)
699
0
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__ ( ) -> List[Any]: snake_case__ : Optional[Any] = argparse.ArgumentParser() parser.add_argument( '-m' , '--pretrained_model_name_or_path' , type=A__ , default=A__ , required=A__ , help='Path to pretrained model or model identifier from huggingface.co/models.' , ) parser.add_argument( '-c' , '--caption' , type=A__ , default='robotic cat with wings' , help='Text used to generate images.' , ) parser.add_argument( '-n' , '--images_num' , type=A__ , default=4 , help='How much images to generate.' , ) parser.add_argument( '-s' , '--seed' , type=A__ , default=42 , help='Seed for random process.' , ) parser.add_argument( '-ci' , '--cuda_id' , type=A__ , default=0 , help='cuda_id.' , ) snake_case__ : Any = parser.parse_args() return args def UpperCamelCase__ ( A__ , A__ , A__ ) -> Optional[int]: if not len(A__ ) == rows * cols: raise ValueError('The specified number of rows and columns are not correct.' ) snake_case__ : List[Any] = imgs[0].size snake_case__ : Union[str, Any] = Image.new('RGB' , size=(cols * w, rows * h) ) snake_case__ : Dict = grid.size for i, img in enumerate(A__ ): grid.paste(A__ , box=(i % cols * w, i // cols * h) ) return grid def UpperCamelCase__ ( A__ , A__="robotic cat with wings" , A__=7.5 , A__=50 , A__=1 , A__=42 , ) -> str: snake_case__ : Tuple = torch.Generator(pipeline.device ).manual_seed(A__ ) snake_case__ : int = pipeline( A__ , guidance_scale=A__ , num_inference_steps=A__ , generator=A__ , num_images_per_prompt=A__ , ).images snake_case__ : Dict = int(math.sqrt(A__ ) ) snake_case__ : Optional[Any] = image_grid(A__ , rows=_rows , cols=num_images_per_prompt // _rows ) return grid, images lowerCAmelCase__ : Union[str, Any] = parse_args() # Load models and create wrapper for stable diffusion lowerCAmelCase__ : Any = CLIPTokenizer.from_pretrained(args.pretrained_model_name_or_path, subfolder='''tokenizer''') lowerCAmelCase__ : Optional[Any] = CLIPTextModel.from_pretrained(args.pretrained_model_name_or_path, subfolder='''text_encoder''') lowerCAmelCase__ : Any = AutoencoderKL.from_pretrained(args.pretrained_model_name_or_path, subfolder='''vae''') lowerCAmelCase__ : str = UNetaDConditionModel.from_pretrained(args.pretrained_model_name_or_path, subfolder='''unet''') lowerCAmelCase__ : Tuple = StableDiffusionPipeline.from_pretrained( args.pretrained_model_name_or_path, text_encoder=text_encoder, vae=vae, unet=unet, tokenizer=tokenizer ) lowerCAmelCase__ : Dict = lambda images, clip_input: (images, False) if os.path.exists(os.path.join(args.pretrained_model_name_or_path, '''best_model.pt''')): lowerCAmelCase__ : Union[str, Any] = load(args.pretrained_model_name_or_path, model=unet) unet.eval() setattr(pipeline, '''unet''', unet) else: lowerCAmelCase__ : Any = unet.to(torch.device('''cuda''', args.cuda_id)) lowerCAmelCase__ : List[str] = pipeline.to(unet.device) lowerCAmelCase__ : List[Any] = 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())))) lowerCAmelCase__ : Tuple = 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)))
707
from collections import namedtuple lowerCAmelCase__ : Union[str, Any] = namedtuple('''from_to''', '''from_ to''') lowerCAmelCase__ : Tuple = { '''cubicmeter''': from_to(1, 1), '''litre''': from_to(0.0_01, 10_00), '''kilolitre''': from_to(1, 1), '''gallon''': from_to(0.0_04_54, 2_64.1_72), '''cubicyard''': from_to(0.7_64_55, 1.3_07_95), '''cubicfoot''': from_to(0.0_28, 35.31_47), '''cup''': from_to(0.0_00_23_65_88, 42_26.75), } def UpperCamelCase__ ( A__ , A__ , A__ ) -> float: if from_type not in METRIC_CONVERSION: raise ValueError( F"""Invalid 'from_type' value: {from_type!r} Supported values are:\n""" + ', '.join(A__ ) ) if to_type not in METRIC_CONVERSION: raise ValueError( F"""Invalid 'to_type' value: {to_type!r}. Supported values are:\n""" + ', '.join(A__ ) ) return value * METRIC_CONVERSION[from_type].from_ * METRIC_CONVERSION[to_type].to if __name__ == "__main__": import doctest doctest.testmod()
699
0
from __future__ import annotations def UpperCamelCase__ ( A__ , A__ , A__ , A__ ) -> list: snake_case__ : Dict = [] snake_case__ : str = input_list[low:mid], input_list[mid : high + 1] while left and right: result.append((left if left[0] <= right[0] else right).pop(0 ) ) snake_case__ : Optional[Any] = result + left + right return input_list def UpperCamelCase__ ( A__ ) -> list: if len(A__ ) <= 1: return input_list snake_case__ : Optional[Any] = list(A__ ) # iteration for two-way merging snake_case__ : List[str] = 2 while p <= len(A__ ): # getting low, high and middle value for merge-sort of single list for i in range(0 , len(A__ ) , A__ ): snake_case__ : Optional[int] = i snake_case__ : List[str] = i + p - 1 snake_case__ : Dict = (low + high + 1) // 2 snake_case__ : Union[str, Any] = merge(A__ , A__ , A__ , A__ ) # final merge of last two parts if p * 2 >= len(A__ ): snake_case__ : Tuple = i snake_case__ : List[Any] = merge(A__ , 0 , A__ , len(A__ ) - 1 ) break p *= 2 return input_list if __name__ == "__main__": lowerCAmelCase__ : List[Any] = input('''Enter numbers separated by a comma:\n''').strip() if user_input == "": lowerCAmelCase__ : Any = [] else: lowerCAmelCase__ : int = [int(item.strip()) for item in user_input.split(''',''')] print(iter_merge_sort(unsorted))
708
import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging lowerCAmelCase__ : Tuple = logging.get_logger(__name__) lowerCAmelCase__ : Union[str, Any] = '''▁''' lowerCAmelCase__ : List[Any] = {'''vocab_file''': '''sentencepiece.bpe.model'''} lowerCAmelCase__ : Optional[Any] = { '''vocab_file''': { '''facebook/xglm-564M''': '''https://huggingface.co/facebook/xglm-564M/resolve/main/sentencepiece.bpe.model''', } } lowerCAmelCase__ : str = { '''facebook/xglm-564M''': 20_48, } class __snake_case ( _lowerCamelCase ): __lowerCamelCase = VOCAB_FILES_NAMES __lowerCamelCase = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase = ["""input_ids""", """attention_mask"""] def __init__( self , __UpperCamelCase , __UpperCamelCase="<s>" , __UpperCamelCase="</s>" , __UpperCamelCase="</s>" , __UpperCamelCase="<s>" , __UpperCamelCase="<unk>" , __UpperCamelCase="<pad>" , __UpperCamelCase = None , **__UpperCamelCase , ) -> None: '''simple docstring''' snake_case__ : Any = {} if sp_model_kwargs is None else sp_model_kwargs # Compatibility with the original tokenizer snake_case__ : Tuple = 7 snake_case__ : Dict = [F"""<madeupword{i}>""" for i in range(self.num_madeup_words )] snake_case__ : Union[str, Any] = kwargs.get('additional_special_tokens' , [] ) kwargs["additional_special_tokens"] += [ word for word in madeup_words if word not in kwargs["additional_special_tokens"] ] super().__init__( bos_token=__UpperCamelCase , eos_token=__UpperCamelCase , unk_token=__UpperCamelCase , sep_token=__UpperCamelCase , cls_token=__UpperCamelCase , pad_token=__UpperCamelCase , sp_model_kwargs=self.sp_model_kwargs , **__UpperCamelCase , ) snake_case__ : Optional[int] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(__UpperCamelCase ) ) snake_case__ : Optional[Any] = vocab_file # Original fairseq vocab and spm vocab must be "aligned": # Vocab | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 # -------- | ------- | ------- | ------ | ------- | --- | --- | --- | ----- | ----- | ---- # fairseq | '<s>' | '<pad>' | '</s>' | '<unk>' | ',' | '.' | '▁' | 's' | '▁de' | '-' # spm | '<unk>' | '<s>' | '</s>' | ',' | '.' | '▁' | 's' | '▁de' | '-' | '▁a' # The first "real" token "," has position 4 in the original fairseq vocab and position 3 in the spm vocab snake_case__ : Tuple = 1 # Mimic fairseq token-to-id alignment for the first 4 token snake_case__ : Tuple = {'<s>': 0, '<pad>': 1, '</s>': 2, '<unk>': 3} snake_case__ : List[Any] = len(self.sp_model ) snake_case__ : Optional[Any] = {F"""<madeupword{i}>""": sp_size + i + self.fairseq_offset for i in range(self.num_madeup_words )} self.fairseq_tokens_to_ids.update(__UpperCamelCase ) snake_case__ : Union[str, Any] = {v: k for k, v in self.fairseq_tokens_to_ids.items()} def __getstate__( self ) -> List[Any]: '''simple docstring''' snake_case__ : Union[str, Any] = self.__dict__.copy() snake_case__ : Optional[Any] = None snake_case__ : Tuple = self.sp_model.serialized_model_proto() return state def __setstate__( self , __UpperCamelCase ) -> Union[str, Any]: '''simple docstring''' snake_case__ : Union[str, Any] = d # for backward compatibility if not hasattr(self , 'sp_model_kwargs' ): snake_case__ : Any = {} snake_case__ : Dict = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.LoadFromSerializedProto(self.sp_model_proto ) def __a ( self , __UpperCamelCase , __UpperCamelCase = None ) -> List[int]: '''simple docstring''' if token_ids_a is None: return [self.sep_token_id] + token_ids_a snake_case__ : str = [self.sep_token_id] return sep + token_ids_a + sep + sep + token_ids_a def __a ( self , __UpperCamelCase , __UpperCamelCase = None , __UpperCamelCase = 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 ) if token_ids_a is None: return [1] + ([0] * len(__UpperCamelCase )) return [1] + ([0] * len(__UpperCamelCase )) + [1, 1] + ([0] * len(__UpperCamelCase )) def __a ( self , __UpperCamelCase , __UpperCamelCase = None ) -> List[int]: '''simple docstring''' snake_case__ : int = [self.sep_token_id] if token_ids_a is None: return len(sep + token_ids_a ) * [0] return len(sep + token_ids_a + sep + sep + token_ids_a ) * [0] @property def __a ( self ) -> Tuple: '''simple docstring''' return len(self.sp_model ) + self.fairseq_offset + self.num_madeup_words def __a ( self ) -> Union[str, Any]: '''simple docstring''' snake_case__ : int = {self.convert_ids_to_tokens(__UpperCamelCase ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __a ( self , __UpperCamelCase ) -> List[str]: '''simple docstring''' return self.sp_model.encode(__UpperCamelCase , out_type=__UpperCamelCase ) def __a ( self , __UpperCamelCase ) -> Union[str, Any]: '''simple docstring''' if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] snake_case__ : Optional[Any] = self.sp_model.PieceToId(__UpperCamelCase ) # Need to return unknown token if the SP model returned 0 return spm_id + self.fairseq_offset if spm_id else self.unk_token_id def __a ( self , __UpperCamelCase ) -> Dict: '''simple docstring''' if index in self.fairseq_ids_to_tokens: return self.fairseq_ids_to_tokens[index] return self.sp_model.IdToPiece(index - self.fairseq_offset ) def __a ( self , __UpperCamelCase ) -> int: '''simple docstring''' snake_case__ : int = ''.join(__UpperCamelCase ).replace(__UpperCamelCase , ' ' ).strip() return out_string def __a ( self , __UpperCamelCase , __UpperCamelCase = 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__ : List[str] = 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__ : Any = self.sp_model.serialized_model_proto() fi.write(__UpperCamelCase ) return (out_vocab_file,)
699
0
import time import warnings from abc import ABC from copy import deepcopy from typing import Optional import torch from ..utils import add_start_docstrings, logging lowerCAmelCase__ : Dict = logging.get_logger(__name__) lowerCAmelCase__ : int = r''' Args: input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the vocabulary. Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and [`PreTrainedTokenizer.__call__`] for details. [What are input IDs?](../glossary#input-ids) scores (`torch.FloatTensor` of shape `(batch_size, config.vocab_size)`): Prediction scores of a language modeling head. These can be scores for each vocabulary token before SoftMax or scores for each vocabulary token after SoftMax. kwargs (`Dict[str, Any]`, *optional*): Additional stopping criteria specific kwargs. Return: `bool`. `False` indicates we should continue, `True` indicates we should stop. ''' class __snake_case ( _lowerCamelCase ): @add_start_docstrings(__UpperCamelCase ) def __call__( self , __UpperCamelCase , __UpperCamelCase , **__UpperCamelCase ) -> bool: '''simple docstring''' raise NotImplementedError('StoppingCriteria needs to be subclassed' ) class __snake_case ( _lowerCamelCase ): def __init__( self , __UpperCamelCase , __UpperCamelCase = None ) -> Dict: '''simple docstring''' snake_case__ : Optional[int] = max_length snake_case__ : Union[str, Any] = max_position_embeddings @add_start_docstrings(__UpperCamelCase ) def __call__( self , __UpperCamelCase , __UpperCamelCase , **__UpperCamelCase ) -> bool: '''simple docstring''' snake_case__ : Optional[int] = input_ids.shape[-1] snake_case__ : Tuple = cur_len >= self.max_length if self.max_position_embeddings is not None and not is_done and cur_len >= self.max_position_embeddings: logger.warning_once( 'This is a friendly reminder - the current text generation call will exceed the model\'s predefined ' F"""maximum length ({self.max_position_embeddings}). Depending on the model, you may observe """ 'exceptions, performance degradation, or nothing at all.' ) return is_done class __snake_case ( _lowerCamelCase ): def __init__( self , __UpperCamelCase , __UpperCamelCase ) -> Any: '''simple docstring''' warnings.warn( 'The class `MaxNewTokensCriteria` is deprecated. ' F"""Please use `MaxLengthCriteria(max_length={start_length + max_new_tokens})` """ 'with `max_length = start_length + max_new_tokens` instead.' , __UpperCamelCase , ) snake_case__ : List[Any] = start_length snake_case__ : List[Any] = max_new_tokens snake_case__ : Union[str, Any] = start_length + max_new_tokens @add_start_docstrings(__UpperCamelCase ) def __call__( self , __UpperCamelCase , __UpperCamelCase , **__UpperCamelCase ) -> bool: '''simple docstring''' return input_ids.shape[-1] >= self.max_length class __snake_case ( _lowerCamelCase ): def __init__( self , __UpperCamelCase , __UpperCamelCase = None ) -> Any: '''simple docstring''' snake_case__ : Any = max_time snake_case__ : List[Any] = time.time() if initial_timestamp is None else initial_timestamp @add_start_docstrings(__UpperCamelCase ) def __call__( self , __UpperCamelCase , __UpperCamelCase , **__UpperCamelCase ) -> bool: '''simple docstring''' return time.time() - self.initial_timestamp > self.max_time class __snake_case ( _lowerCamelCase ): @add_start_docstrings(__UpperCamelCase ) def __call__( self , __UpperCamelCase , __UpperCamelCase , **__UpperCamelCase ) -> bool: '''simple docstring''' return any(criteria(__UpperCamelCase , __UpperCamelCase ) for criteria in self ) @property def __a ( self ) -> Optional[int]: '''simple docstring''' for stopping_criterium in self: if isinstance(__UpperCamelCase , __UpperCamelCase ): return stopping_criterium.max_length elif isinstance(__UpperCamelCase , __UpperCamelCase ): return stopping_criterium.max_length return None def UpperCamelCase__ ( A__ , A__ ) -> StoppingCriteriaList: snake_case__ : Dict = stopping_criteria.max_length snake_case__ : str = deepcopy(A__ ) if stopping_max_length is not None and stopping_max_length != max_length: warnings.warn('You set different `max_length` for stopping criteria and `max_length` parameter' , A__ ) elif stopping_max_length is None: new_stopping_criteria.append(MaxLengthCriteria(max_length=A__ ) ) return new_stopping_criteria
709
import collections from typing import List, Optional, Union from ...tokenization_utils_base import BatchEncoding from ...utils import TensorType, add_end_docstrings, add_start_docstrings, logging from ..bert.tokenization_bert_fast import BertTokenizerFast from .tokenization_dpr import DPRContextEncoderTokenizer, DPRQuestionEncoderTokenizer, DPRReaderTokenizer lowerCAmelCase__ : Any = logging.get_logger(__name__) lowerCAmelCase__ : List[Any] = {'''vocab_file''': '''vocab.txt''', '''tokenizer_file''': '''tokenizer.json'''} lowerCAmelCase__ : Any = { '''vocab_file''': { '''facebook/dpr-ctx_encoder-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/vocab.txt''' ), '''facebook/dpr-ctx_encoder-multiset-base''': ( '''https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/vocab.txt''' ), }, '''tokenizer_file''': { '''facebook/dpr-ctx_encoder-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/tokenizer.json''' ), '''facebook/dpr-ctx_encoder-multiset-base''': ( '''https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/tokenizer.json''' ), }, } lowerCAmelCase__ : Any = { '''vocab_file''': { '''facebook/dpr-question_encoder-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/vocab.txt''' ), '''facebook/dpr-question_encoder-multiset-base''': ( '''https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/vocab.txt''' ), }, '''tokenizer_file''': { '''facebook/dpr-question_encoder-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/tokenizer.json''' ), '''facebook/dpr-question_encoder-multiset-base''': ( '''https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/tokenizer.json''' ), }, } lowerCAmelCase__ : Tuple = { '''vocab_file''': { '''facebook/dpr-reader-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/vocab.txt''' ), '''facebook/dpr-reader-multiset-base''': ( '''https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/vocab.txt''' ), }, '''tokenizer_file''': { '''facebook/dpr-reader-single-nq-base''': ( '''https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/tokenizer.json''' ), '''facebook/dpr-reader-multiset-base''': ( '''https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/tokenizer.json''' ), }, } lowerCAmelCase__ : Dict = { '''facebook/dpr-ctx_encoder-single-nq-base''': 5_12, '''facebook/dpr-ctx_encoder-multiset-base''': 5_12, } lowerCAmelCase__ : Union[str, Any] = { '''facebook/dpr-question_encoder-single-nq-base''': 5_12, '''facebook/dpr-question_encoder-multiset-base''': 5_12, } lowerCAmelCase__ : Optional[Any] = { '''facebook/dpr-reader-single-nq-base''': 5_12, '''facebook/dpr-reader-multiset-base''': 5_12, } lowerCAmelCase__ : Tuple = { '''facebook/dpr-ctx_encoder-single-nq-base''': {'''do_lower_case''': True}, '''facebook/dpr-ctx_encoder-multiset-base''': {'''do_lower_case''': True}, } lowerCAmelCase__ : Any = { '''facebook/dpr-question_encoder-single-nq-base''': {'''do_lower_case''': True}, '''facebook/dpr-question_encoder-multiset-base''': {'''do_lower_case''': True}, } lowerCAmelCase__ : List[str] = { '''facebook/dpr-reader-single-nq-base''': {'''do_lower_case''': True}, '''facebook/dpr-reader-multiset-base''': {'''do_lower_case''': True}, } class __snake_case ( _lowerCamelCase ): __lowerCamelCase = VOCAB_FILES_NAMES __lowerCamelCase = CONTEXT_ENCODER_PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase = CONTEXT_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase = CONTEXT_ENCODER_PRETRAINED_INIT_CONFIGURATION __lowerCamelCase = DPRContextEncoderTokenizer class __snake_case ( _lowerCamelCase ): __lowerCamelCase = VOCAB_FILES_NAMES __lowerCamelCase = QUESTION_ENCODER_PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase = QUESTION_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase = QUESTION_ENCODER_PRETRAINED_INIT_CONFIGURATION __lowerCamelCase = DPRQuestionEncoderTokenizer lowerCAmelCase__ : Tuple = collections.namedtuple( '''DPRSpanPrediction''', ['''span_score''', '''relevance_score''', '''doc_id''', '''start_index''', '''end_index''', '''text'''] ) lowerCAmelCase__ : List[Any] = collections.namedtuple('''DPRReaderOutput''', ['''start_logits''', '''end_logits''', '''relevance_logits''']) lowerCAmelCase__ : int = r''' Return a dictionary with the token ids of the input strings and other information to give to `.decode_best_spans`. It converts the strings of a question and different passages (title and text) in a sequence of IDs (integers), using the tokenizer and vocabulary. The resulting `input_ids` is a matrix of size `(n_passages, sequence_length)` with the format: [CLS] <question token ids> [SEP] <titles ids> [SEP] <texts ids> Args: questions (`str` or `List[str]`): The questions to be encoded. You can specify one question for many passages. In this case, the question will be duplicated like `[questions] * n_passages`. Otherwise you have to specify as many questions as in `titles` or `texts`. titles (`str` or `List[str]`): The passages titles to be encoded. This can be a string or a list of strings if there are several passages. texts (`str` or `List[str]`): The passages texts to be encoded. This can be a string or a list of strings if there are several passages. padding (`bool`, `str` or [`~utils.PaddingStrategy`], *optional*, defaults to `False`): Activates and controls padding. Accepts the following values: - `True` or `\'longest\'`: Pad to the longest sequence in the batch (or no padding if only a single sequence if provided). - `\'max_length\'`: Pad to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. - `False` or `\'do_not_pad\'` (default): No padding (i.e., can output a batch with sequences of different lengths). truncation (`bool`, `str` or [`~tokenization_utils_base.TruncationStrategy`], *optional*, defaults to `False`): Activates and controls truncation. Accepts the following values: - `True` or `\'longest_first\'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. This will truncate token by token, removing a token from the longest sequence in the pair if a pair of sequences (or a batch of pairs) is provided. - `\'only_first\'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. This will only truncate the first sequence of a pair if a pair of sequences (or a batch of pairs) is provided. - `\'only_second\'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum acceptable input length for the model if that argument is not provided. This will only truncate the second sequence of a pair if a pair of sequences (or a batch of pairs) is provided. - `False` or `\'do_not_truncate\'` (default): No truncation (i.e., can output batch with sequence lengths greater than the model maximum admissible input size). max_length (`int`, *optional*): Controls the maximum length to use by one of the truncation/padding parameters. If left unset or set to `None`, this will use the predefined model maximum length if a maximum length is required by one of the truncation/padding parameters. If the model has no specific maximum input length (like XLNet) truncation/padding to a maximum length will be deactivated. return_tensors (`str` or [`~utils.TensorType`], *optional*): If set, will return tensors instead of list of python integers. Acceptable values are: - `\'tf\'`: Return TensorFlow `tf.constant` objects. - `\'pt\'`: Return PyTorch `torch.Tensor` objects. - `\'np\'`: Return Numpy `np.ndarray` objects. return_attention_mask (`bool`, *optional*): Whether or not to return the attention mask. If not set, will return the attention mask according to the specific tokenizer\'s default, defined by the `return_outputs` attribute. [What are attention masks?](../glossary#attention-mask) Return: `Dict[str, List[List[int]]]`: A dictionary with the following keys: - `input_ids`: List of token ids to be fed to a model. - `attention_mask`: List of indices specifying which tokens should be attended to by the model. ''' @add_start_docstrings(_lowerCamelCase ) class __snake_case : def __call__( self , __UpperCamelCase , __UpperCamelCase = None , __UpperCamelCase = None , __UpperCamelCase = False , __UpperCamelCase = False , __UpperCamelCase = None , __UpperCamelCase = None , __UpperCamelCase = None , **__UpperCamelCase , ) -> BatchEncoding: '''simple docstring''' if titles is None and texts is None: return super().__call__( __UpperCamelCase , padding=__UpperCamelCase , truncation=__UpperCamelCase , max_length=__UpperCamelCase , return_tensors=__UpperCamelCase , return_attention_mask=__UpperCamelCase , **__UpperCamelCase , ) elif titles is None or texts is None: snake_case__ : Optional[Any] = titles if texts is None else texts return super().__call__( __UpperCamelCase , __UpperCamelCase , padding=__UpperCamelCase , truncation=__UpperCamelCase , max_length=__UpperCamelCase , return_tensors=__UpperCamelCase , return_attention_mask=__UpperCamelCase , **__UpperCamelCase , ) snake_case__ : int = titles if not isinstance(__UpperCamelCase , __UpperCamelCase ) else [titles] snake_case__ : Optional[int] = texts if not isinstance(__UpperCamelCase , __UpperCamelCase ) else [texts] snake_case__ : List[Any] = len(__UpperCamelCase ) snake_case__ : str = questions if not isinstance(__UpperCamelCase , __UpperCamelCase ) else [questions] * n_passages assert len(__UpperCamelCase ) == len( __UpperCamelCase ), F"""There should be as many titles than texts but got {len(__UpperCamelCase )} titles and {len(__UpperCamelCase )} texts.""" snake_case__ : Optional[int] = super().__call__(__UpperCamelCase , __UpperCamelCase , padding=__UpperCamelCase , truncation=__UpperCamelCase )['input_ids'] snake_case__ : Optional[Any] = super().__call__(__UpperCamelCase , add_special_tokens=__UpperCamelCase , padding=__UpperCamelCase , truncation=__UpperCamelCase )['input_ids'] snake_case__ : Union[str, Any] = { 'input_ids': [ (encoded_question_and_title + encoded_text)[:max_length] if max_length is not None and truncation else encoded_question_and_title + encoded_text for encoded_question_and_title, encoded_text in zip(__UpperCamelCase , __UpperCamelCase ) ] } if return_attention_mask is not False: snake_case__ : List[Any] = [] for input_ids in encoded_inputs["input_ids"]: attention_mask.append([int(input_id != self.pad_token_id ) for input_id in input_ids] ) snake_case__ : Union[str, Any] = attention_mask return self.pad(__UpperCamelCase , padding=__UpperCamelCase , max_length=__UpperCamelCase , return_tensors=__UpperCamelCase ) def __a ( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase = 16 , __UpperCamelCase = 64 , __UpperCamelCase = 4 , ) -> List[DPRSpanPrediction]: '''simple docstring''' snake_case__ : Optional[Any] = reader_input['input_ids'] snake_case__ , snake_case__ , snake_case__ : Any = reader_output[:3] snake_case__ : List[str] = len(__UpperCamelCase ) snake_case__ : Tuple = sorted(range(__UpperCamelCase ) , reverse=__UpperCamelCase , key=relevance_logits.__getitem__ ) snake_case__ : List[DPRReaderOutput] = [] for doc_id in sorted_docs: snake_case__ : Tuple = list(input_ids[doc_id] ) # assuming question & title information is at the beginning of the sequence snake_case__ : int = sequence_ids.index(self.sep_token_id , 2 ) + 1 # second sep id if sequence_ids[-1] == self.pad_token_id: snake_case__ : Union[str, Any] = sequence_ids.index(self.pad_token_id ) else: snake_case__ : str = len(__UpperCamelCase ) snake_case__ : Dict = self._get_best_spans( start_logits=start_logits[doc_id][passage_offset:sequence_len] , end_logits=end_logits[doc_id][passage_offset:sequence_len] , max_answer_length=__UpperCamelCase , top_spans=__UpperCamelCase , ) for start_index, end_index in best_spans: start_index += passage_offset end_index += passage_offset nbest_spans_predictions.append( DPRSpanPrediction( span_score=start_logits[doc_id][start_index] + end_logits[doc_id][end_index] , relevance_score=relevance_logits[doc_id] , doc_id=__UpperCamelCase , start_index=__UpperCamelCase , end_index=__UpperCamelCase , text=self.decode(sequence_ids[start_index : end_index + 1] ) , ) ) if len(__UpperCamelCase ) >= num_spans: break return nbest_spans_predictions[:num_spans] def __a ( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , ) -> List[DPRSpanPrediction]: '''simple docstring''' snake_case__ : Any = [] for start_index, start_score in enumerate(__UpperCamelCase ): for answer_length, end_score in enumerate(end_logits[start_index : start_index + max_answer_length] ): scores.append(((start_index, start_index + answer_length), start_score + end_score) ) snake_case__ : str = sorted(__UpperCamelCase , key=lambda __UpperCamelCase : x[1] , reverse=__UpperCamelCase ) snake_case__ : Any = [] for (start_index, end_index), score in scores: assert start_index <= end_index, F"""Wrong span indices: [{start_index}:{end_index}]""" snake_case__ : str = end_index - start_index + 1 assert length <= max_answer_length, F"""Span is too long: {length} > {max_answer_length}""" if any( start_index <= prev_start_index <= prev_end_index <= end_index or prev_start_index <= start_index <= end_index <= prev_end_index for (prev_start_index, prev_end_index) in chosen_span_intervals ): continue chosen_span_intervals.append((start_index, end_index) ) if len(__UpperCamelCase ) == top_spans: break return chosen_span_intervals @add_end_docstrings(_lowerCamelCase ) class __snake_case ( _lowerCamelCase ,_lowerCamelCase ): __lowerCamelCase = VOCAB_FILES_NAMES __lowerCamelCase = READER_PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase = READER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase = READER_PRETRAINED_INIT_CONFIGURATION __lowerCamelCase = ["""input_ids""", """attention_mask"""] __lowerCamelCase = DPRReaderTokenizer
699
0
'''simple docstring''' import random import unittest import torch from diffusers import IFInpaintingPipeline from diffusers.utils import floats_tensor from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import skip_mps, torch_device from ..pipeline_params import ( TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS, ) from ..test_pipelines_common import PipelineTesterMixin from . import IFPipelineTesterMixin @skip_mps class __snake_case ( _lowerCamelCase ,_lowerCamelCase ,unittest.TestCase ): __lowerCamelCase = IFInpaintingPipeline __lowerCamelCase = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {"""width""", """height"""} __lowerCamelCase = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS __lowerCamelCase = PipelineTesterMixin.required_optional_params - {"""latents"""} def __a ( self ) -> Optional[Any]: '''simple docstring''' return self._get_dummy_components() def __a ( self , __UpperCamelCase , __UpperCamelCase=0 ) -> str: '''simple docstring''' if str(__UpperCamelCase ).startswith('mps' ): snake_case__ : int = torch.manual_seed(__UpperCamelCase ) else: snake_case__ : Union[str, Any] = torch.Generator(device=__UpperCamelCase ).manual_seed(__UpperCamelCase ) snake_case__ : Optional[int] = floats_tensor((1, 3, 32, 32) , rng=random.Random(__UpperCamelCase ) ).to(__UpperCamelCase ) snake_case__ : Dict = floats_tensor((1, 3, 32, 32) , rng=random.Random(__UpperCamelCase ) ).to(__UpperCamelCase ) snake_case__ : Optional[Any] = { 'prompt': 'A painting of a squirrel eating a burger', 'image': image, 'mask_image': mask_image, 'generator': generator, 'num_inference_steps': 2, 'output_type': 'numpy', } return inputs @unittest.skipIf( torch_device != 'cuda' or not is_xformers_available() , reason='XFormers attention is only available with CUDA and `xformers` installed' , ) def __a ( self ) -> List[Any]: '''simple docstring''' self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3 ) def __a ( self ) -> Optional[int]: '''simple docstring''' self._test_save_load_optional_components() @unittest.skipIf(torch_device != 'cuda' , reason='float16 requires CUDA' ) def __a ( self ) -> List[str]: '''simple docstring''' super().test_save_load_floataa(expected_max_diff=1E-1 ) def __a ( self ) -> List[str]: '''simple docstring''' self._test_attention_slicing_forward_pass(expected_max_diff=1E-2 ) def __a ( self ) -> int: '''simple docstring''' self._test_save_load_local() def __a ( self ) -> List[str]: '''simple docstring''' self._test_inference_batch_single_identical( expected_max_diff=1E-2 , )
710
import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, EulerAncestralDiscreteScheduler, LMSDiscreteScheduler, PNDMScheduler, StableDiffusionInstructPixaPixPipeline, UNetaDConditionModel, ) from diffusers.image_processor import VaeImageProcessor from diffusers.utils import floats_tensor, load_image, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..pipeline_params import ( IMAGE_TO_IMAGE_IMAGE_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS, ) from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class __snake_case ( _lowerCamelCase ,_lowerCamelCase ,_lowerCamelCase ,unittest.TestCase ): __lowerCamelCase = StableDiffusionInstructPixaPixPipeline __lowerCamelCase = TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {"""height""", """width""", """cross_attention_kwargs"""} __lowerCamelCase = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS __lowerCamelCase = IMAGE_TO_IMAGE_IMAGE_PARAMS __lowerCamelCase = IMAGE_TO_IMAGE_IMAGE_PARAMS def __a ( self ) -> List[Any]: '''simple docstring''' torch.manual_seed(0 ) snake_case__ : Tuple = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=8 , out_channels=4 , down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D') , up_block_types=('CrossAttnUpBlock2D', 'UpBlock2D') , cross_attention_dim=32 , ) snake_case__ : Any = PNDMScheduler(skip_prk_steps=__UpperCamelCase ) torch.manual_seed(0 ) snake_case__ : Dict = 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 ) snake_case__ : int = 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 , ) snake_case__ : Tuple = CLIPTextModel(__UpperCamelCase ) snake_case__ : Optional[int] = CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip' ) snake_case__ : Optional[int] = { 'unet': unet, 'scheduler': scheduler, 'vae': vae, 'text_encoder': text_encoder, 'tokenizer': tokenizer, 'safety_checker': None, 'feature_extractor': None, } return components def __a ( self , __UpperCamelCase , __UpperCamelCase=0 ) -> Dict: '''simple docstring''' snake_case__ : Optional[Any] = floats_tensor((1, 3, 32, 32) , rng=random.Random(__UpperCamelCase ) ).to(__UpperCamelCase ) snake_case__ : int = image.cpu().permute(0 , 2 , 3 , 1 )[0] snake_case__ : Union[str, Any] = Image.fromarray(np.uinta(__UpperCamelCase ) ).convert('RGB' ) if str(__UpperCamelCase ).startswith('mps' ): snake_case__ : str = torch.manual_seed(__UpperCamelCase ) else: snake_case__ : Dict = torch.Generator(device=__UpperCamelCase ).manual_seed(__UpperCamelCase ) snake_case__ : str = { 'prompt': 'A painting of a squirrel eating a burger', 'image': image, 'generator': generator, 'num_inference_steps': 2, 'guidance_scale': 6.0, 'image_guidance_scale': 1, 'output_type': 'numpy', } return inputs def __a ( self ) -> Tuple: '''simple docstring''' snake_case__ : Dict = 'cpu' # ensure determinism for the device-dependent torch.Generator snake_case__ : Optional[int] = self.get_dummy_components() snake_case__ : str = StableDiffusionInstructPixaPixPipeline(**__UpperCamelCase ) snake_case__ : Optional[int] = sd_pipe.to(__UpperCamelCase ) sd_pipe.set_progress_bar_config(disable=__UpperCamelCase ) snake_case__ : Tuple = self.get_dummy_inputs(__UpperCamelCase ) snake_case__ : List[str] = sd_pipe(**__UpperCamelCase ).images snake_case__ : List[str] = image[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) snake_case__ : str = np.array([0.7_5_2_6, 0.3_7_5_0, 0.4_5_4_7, 0.6_1_1_7, 0.5_8_6_6, 0.5_0_1_6, 0.4_3_2_7, 0.5_6_4_2, 0.4_8_1_5] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 def __a ( self ) -> Any: '''simple docstring''' snake_case__ : Dict = 'cpu' # ensure determinism for the device-dependent torch.Generator snake_case__ : Union[str, Any] = self.get_dummy_components() snake_case__ : Optional[Any] = StableDiffusionInstructPixaPixPipeline(**__UpperCamelCase ) snake_case__ : List[Any] = sd_pipe.to(__UpperCamelCase ) sd_pipe.set_progress_bar_config(disable=__UpperCamelCase ) snake_case__ : Union[str, Any] = self.get_dummy_inputs(__UpperCamelCase ) snake_case__ : List[str] = 'french fries' snake_case__ : Optional[Any] = sd_pipe(**__UpperCamelCase , negative_prompt=__UpperCamelCase ) snake_case__ : Union[str, Any] = output.images snake_case__ : Tuple = image[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) snake_case__ : Any = np.array([0.7_5_1_1, 0.3_6_4_2, 0.4_5_5_3, 0.6_2_3_6, 0.5_7_9_7, 0.5_0_1_3, 0.4_3_4_3, 0.5_6_1_1, 0.4_8_3_1] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 def __a ( self ) -> int: '''simple docstring''' snake_case__ : Optional[int] = 'cpu' # ensure determinism for the device-dependent torch.Generator snake_case__ : List[str] = self.get_dummy_components() snake_case__ : str = StableDiffusionInstructPixaPixPipeline(**__UpperCamelCase ) snake_case__ : str = sd_pipe.to(__UpperCamelCase ) sd_pipe.set_progress_bar_config(disable=__UpperCamelCase ) snake_case__ : Dict = self.get_dummy_inputs(__UpperCamelCase ) snake_case__ : Any = [inputs['prompt']] * 2 snake_case__ : Optional[int] = np.array(inputs['image'] ).astype(np.floataa ) / 2_5_5.0 snake_case__ : Optional[int] = torch.from_numpy(__UpperCamelCase ).unsqueeze(0 ).to(__UpperCamelCase ) snake_case__ : Any = image / 2 + 0.5 snake_case__ : Optional[Any] = image.permute(0 , 3 , 1 , 2 ) snake_case__ : List[Any] = image.repeat(2 , 1 , 1 , 1 ) snake_case__ : Optional[int] = sd_pipe(**__UpperCamelCase ).images snake_case__ : Union[str, Any] = image[-1, -3:, -3:, -1] assert image.shape == (2, 32, 32, 3) snake_case__ : List[Any] = np.array([0.5_8_1_2, 0.5_7_4_8, 0.5_2_2_2, 0.5_9_0_8, 0.5_6_9_5, 0.7_1_7_4, 0.6_8_0_4, 0.5_5_2_3, 0.5_5_7_9] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 def __a ( self ) -> Union[str, Any]: '''simple docstring''' snake_case__ : Optional[int] = 'cpu' # ensure determinism for the device-dependent torch.Generator snake_case__ : Optional[int] = self.get_dummy_components() snake_case__ : Tuple = EulerAncestralDiscreteScheduler( beta_start=0.0_0_0_8_5 , beta_end=0.0_1_2 , beta_schedule='scaled_linear' ) snake_case__ : int = StableDiffusionInstructPixaPixPipeline(**__UpperCamelCase ) snake_case__ : List[str] = sd_pipe.to(__UpperCamelCase ) sd_pipe.set_progress_bar_config(disable=__UpperCamelCase ) snake_case__ : str = self.get_dummy_inputs(__UpperCamelCase ) snake_case__ : Any = sd_pipe(**__UpperCamelCase ).images snake_case__ : int = image[0, -3:, -3:, -1] snake_case__ : Tuple = [round(__UpperCamelCase , 4 ) for x in image_slice.flatten().tolist()] print(','.join([str(__UpperCamelCase ) for x in slice] ) ) assert image.shape == (1, 32, 32, 3) snake_case__ : List[Any] = np.array([0.7_4_1_7, 0.3_8_4_2, 0.4_7_3_2, 0.5_7_7_6, 0.5_8_9_1, 0.5_1_3_9, 0.4_0_5_2, 0.5_6_7_3, 0.4_9_8_6] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-3 def __a ( self ) -> int: '''simple docstring''' super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) def __a ( self ) -> Any: '''simple docstring''' snake_case__ : Optional[int] = self.get_dummy_components() snake_case__ : int = StableDiffusionInstructPixaPixPipeline(**__UpperCamelCase ) snake_case__ : Union[str, Any] = VaeImageProcessor(do_resize=__UpperCamelCase , do_normalize=__UpperCamelCase ) snake_case__ : Optional[int] = pipe.to(__UpperCamelCase ) pipe.set_progress_bar_config(disable=__UpperCamelCase ) snake_case__ : Optional[Any] = pipe(**self.get_dummy_inputs_by_type(__UpperCamelCase , input_image_type='pt' ) )[0] snake_case__ : Union[str, Any] = components['vae'] snake_case__ : str = self.get_dummy_inputs_by_type(__UpperCamelCase , input_image_type='pt' ) for image_param in self.image_latents_params: if image_param in inputs.keys(): snake_case__ : List[str] = vae.encode(inputs[image_param] ).latent_dist.mode() snake_case__ : Dict = pipe(**__UpperCamelCase )[0] snake_case__ : str = np.abs(out - out_latents_inputs ).max() self.assertLess(__UpperCamelCase , 1E-4 , 'passing latents as image input generate different result from passing image' ) @slow @require_torch_gpu class __snake_case ( unittest.TestCase ): def __a ( self ) -> List[str]: '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def __a ( self , __UpperCamelCase=0 ) -> Dict: '''simple docstring''' snake_case__ : Optional[Any] = torch.manual_seed(__UpperCamelCase ) snake_case__ : List[str] = load_image( 'https://huggingface.co/datasets/diffusers/test-arrays/resolve/main/stable_diffusion_pix2pix/example.jpg' ) snake_case__ : int = { 'prompt': 'turn him into a cyborg', 'image': image, 'generator': generator, 'num_inference_steps': 3, 'guidance_scale': 7.5, 'image_guidance_scale': 1.0, 'output_type': 'numpy', } return inputs def __a ( self ) -> Dict: '''simple docstring''' snake_case__ : Any = StableDiffusionInstructPixaPixPipeline.from_pretrained( 'timbrooks/instruct-pix2pix' , safety_checker=__UpperCamelCase ) pipe.to(__UpperCamelCase ) pipe.set_progress_bar_config(disable=__UpperCamelCase ) pipe.enable_attention_slicing() snake_case__ : Tuple = self.get_inputs() snake_case__ : List[Any] = pipe(**__UpperCamelCase ).images snake_case__ : Union[str, Any] = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 512, 512, 3) snake_case__ : Dict = np.array([0.5_9_0_2, 0.6_0_1_5, 0.6_0_2_7, 0.5_9_8_3, 0.6_0_9_2, 0.6_0_6_1, 0.5_7_6_5, 0.5_7_8_5, 0.5_5_5_5] ) assert np.abs(expected_slice - image_slice ).max() < 1E-3 def __a ( self ) -> str: '''simple docstring''' snake_case__ : Any = StableDiffusionInstructPixaPixPipeline.from_pretrained( 'timbrooks/instruct-pix2pix' , safety_checker=__UpperCamelCase ) snake_case__ : Tuple = LMSDiscreteScheduler.from_config(pipe.scheduler.config ) pipe.to(__UpperCamelCase ) pipe.set_progress_bar_config(disable=__UpperCamelCase ) pipe.enable_attention_slicing() snake_case__ : Dict = self.get_inputs() snake_case__ : Dict = pipe(**__UpperCamelCase ).images snake_case__ : Union[str, Any] = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 512, 512, 3) snake_case__ : List[Any] = np.array([0.6_5_7_8, 0.6_8_1_7, 0.6_9_7_2, 0.6_7_6_1, 0.6_8_5_6, 0.6_9_1_6, 0.6_4_2_8, 0.6_5_1_6, 0.6_3_0_1] ) assert np.abs(expected_slice - image_slice ).max() < 1E-3 def __a ( self ) -> Optional[Any]: '''simple docstring''' snake_case__ : Tuple = StableDiffusionInstructPixaPixPipeline.from_pretrained( 'timbrooks/instruct-pix2pix' , safety_checker=__UpperCamelCase ) snake_case__ : Tuple = DDIMScheduler.from_config(pipe.scheduler.config ) pipe.to(__UpperCamelCase ) pipe.set_progress_bar_config(disable=__UpperCamelCase ) pipe.enable_attention_slicing() snake_case__ : Optional[int] = self.get_inputs() snake_case__ : Optional[int] = pipe(**__UpperCamelCase ).images snake_case__ : Tuple = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 512, 512, 3) snake_case__ : int = np.array([0.3_8_2_8, 0.3_8_3_4, 0.3_8_1_8, 0.3_7_9_2, 0.3_8_6_5, 0.3_7_5_2, 0.3_7_9_2, 0.3_8_4_7, 0.3_7_5_3] ) assert np.abs(expected_slice - image_slice ).max() < 1E-3 def __a ( self ) -> Union[str, Any]: '''simple docstring''' snake_case__ : int = 0 def callback_fn(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) -> None: snake_case__ : List[Any] = True nonlocal number_of_steps number_of_steps += 1 if step == 1: snake_case__ : Any = latents.detach().cpu().numpy() assert latents.shape == (1, 4, 64, 64) snake_case__ : int = latents[0, -3:, -3:, -1] snake_case__ : List[str] = np.array([-0.2_4_6_3, -0.4_6_4_4, -0.9_7_5_6, 1.5_1_7_6, 1.4_4_1_4, 0.7_8_6_6, 0.9_8_9_7, 0.8_5_2_1, 0.7_9_8_3] ) assert np.abs(latents_slice.flatten() - expected_slice ).max() < 5E-2 elif step == 2: snake_case__ : Dict = latents.detach().cpu().numpy() assert latents.shape == (1, 4, 64, 64) snake_case__ : Dict = latents[0, -3:, -3:, -1] snake_case__ : Optional[Any] = np.array([-0.2_6_4_4, -0.4_6_2_6, -0.9_6_5_3, 1.5_1_7_6, 1.4_5_5_1, 0.7_6_8_6, 0.9_8_0_5, 0.8_4_5_2, 0.8_1_1_5] ) assert np.abs(latents_slice.flatten() - expected_slice ).max() < 5E-2 snake_case__ : str = False snake_case__ : List[Any] = StableDiffusionInstructPixaPixPipeline.from_pretrained( 'timbrooks/instruct-pix2pix' , safety_checker=__UpperCamelCase , torch_dtype=torch.floataa ) snake_case__ : int = pipe.to(__UpperCamelCase ) pipe.set_progress_bar_config(disable=__UpperCamelCase ) pipe.enable_attention_slicing() snake_case__ : int = self.get_inputs() pipe(**__UpperCamelCase , callback=__UpperCamelCase , callback_steps=1 ) assert callback_fn.has_been_called assert number_of_steps == 3 def __a ( self ) -> Any: '''simple docstring''' torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats() snake_case__ : str = StableDiffusionInstructPixaPixPipeline.from_pretrained( 'timbrooks/instruct-pix2pix' , safety_checker=__UpperCamelCase , torch_dtype=torch.floataa ) snake_case__ : Dict = pipe.to(__UpperCamelCase ) pipe.set_progress_bar_config(disable=__UpperCamelCase ) pipe.enable_attention_slicing(1 ) pipe.enable_sequential_cpu_offload() snake_case__ : str = self.get_inputs() snake_case__ : Tuple = pipe(**__UpperCamelCase ) snake_case__ : List[Any] = torch.cuda.max_memory_allocated() # make sure that less than 2.2 GB is allocated assert mem_bytes < 2.2 * 10**9 def __a ( self ) -> int: '''simple docstring''' snake_case__ : int = self.get_inputs() # resize to resolution that is divisible by 8 but not 16 or 32 snake_case__ : Tuple = inputs['image'].resize((504, 504) ) snake_case__ : str = 'timbrooks/instruct-pix2pix' snake_case__ : Tuple = StableDiffusionInstructPixaPixPipeline.from_pretrained( __UpperCamelCase , safety_checker=__UpperCamelCase , ) pipe.to(__UpperCamelCase ) pipe.set_progress_bar_config(disable=__UpperCamelCase ) pipe.enable_attention_slicing() snake_case__ : str = pipe(**__UpperCamelCase ) snake_case__ : List[Any] = output.images[0] snake_case__ : List[Any] = image[255:258, 383:386, -1] assert image.shape == (504, 504, 3) snake_case__ : List[str] = np.array([0.2_7_2_6, 0.2_5_2_9, 0.2_6_6_4, 0.2_6_5_5, 0.2_6_4_1, 0.2_6_4_2, 0.2_5_9_1, 0.2_6_4_9, 0.2_5_9_0] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5E-3
699
0
import argparse import json from pathlib import Path import requests import timm import torch from huggingface_hub import hf_hub_download from PIL import Image from timm.data import resolve_data_config from timm.data.transforms_factory import create_transform from transformers import ( BitConfig, ViTHybridConfig, ViTHybridForImageClassification, ViTHybridImageProcessor, ViTHybridModel, ) from transformers.image_utils import PILImageResampling from transformers.utils import logging logging.set_verbosity_info() lowerCAmelCase__ : str = logging.get_logger(__name__) def UpperCamelCase__ ( A__ , A__=False ) -> Union[str, Any]: snake_case__ : str = [] # fmt: off # stem: rename_keys.append(('cls_token', 'vit.embeddings.cls_token') ) rename_keys.append(('pos_embed', 'vit.embeddings.position_embeddings') ) rename_keys.append(('patch_embed.proj.weight', 'vit.embeddings.patch_embeddings.projection.weight') ) rename_keys.append(('patch_embed.proj.bias', 'vit.embeddings.patch_embeddings.projection.bias') ) # backbone rename_keys.append(('patch_embed.backbone.stem.conv.weight', 'vit.embeddings.patch_embeddings.backbone.bit.embedder.convolution.weight') ) rename_keys.append(('patch_embed.backbone.stem.norm.weight', 'vit.embeddings.patch_embeddings.backbone.bit.embedder.norm.weight') ) rename_keys.append(('patch_embed.backbone.stem.norm.bias', 'vit.embeddings.patch_embeddings.backbone.bit.embedder.norm.bias') ) for stage_idx in range(len(config.backbone_config.depths ) ): for layer_idx in range(config.backbone_config.depths[stage_idx] ): rename_keys.append((F"""patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.conv1.weight""", F"""vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.conv1.weight""") ) rename_keys.append((F"""patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm1.weight""", F"""vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm1.weight""") ) rename_keys.append((F"""patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm1.bias""", F"""vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm1.bias""") ) rename_keys.append((F"""patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.conv2.weight""", F"""vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.conv2.weight""") ) rename_keys.append((F"""patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm2.weight""", F"""vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm2.weight""") ) rename_keys.append((F"""patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm2.bias""", F"""vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm2.bias""") ) rename_keys.append((F"""patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.conv3.weight""", F"""vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.conv3.weight""") ) rename_keys.append((F"""patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm3.weight""", F"""vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm3.weight""") ) rename_keys.append((F"""patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm3.bias""", F"""vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm3.bias""") ) rename_keys.append((F"""patch_embed.backbone.stages.{stage_idx}.blocks.0.downsample.conv.weight""", F"""vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.0.downsample.conv.weight""") ) rename_keys.append((F"""patch_embed.backbone.stages.{stage_idx}.blocks.0.downsample.norm.weight""", F"""vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.0.downsample.norm.weight""") ) rename_keys.append((F"""patch_embed.backbone.stages.{stage_idx}.blocks.0.downsample.norm.bias""", F"""vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.0.downsample.norm.bias""") ) # transformer encoder for i in range(config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append((F"""blocks.{i}.norm1.weight""", F"""vit.encoder.layer.{i}.layernorm_before.weight""") ) rename_keys.append((F"""blocks.{i}.norm1.bias""", F"""vit.encoder.layer.{i}.layernorm_before.bias""") ) rename_keys.append((F"""blocks.{i}.attn.proj.weight""", F"""vit.encoder.layer.{i}.attention.output.dense.weight""") ) rename_keys.append((F"""blocks.{i}.attn.proj.bias""", F"""vit.encoder.layer.{i}.attention.output.dense.bias""") ) rename_keys.append((F"""blocks.{i}.norm2.weight""", F"""vit.encoder.layer.{i}.layernorm_after.weight""") ) rename_keys.append((F"""blocks.{i}.norm2.bias""", F"""vit.encoder.layer.{i}.layernorm_after.bias""") ) rename_keys.append((F"""blocks.{i}.mlp.fc1.weight""", F"""vit.encoder.layer.{i}.intermediate.dense.weight""") ) rename_keys.append((F"""blocks.{i}.mlp.fc1.bias""", F"""vit.encoder.layer.{i}.intermediate.dense.bias""") ) rename_keys.append((F"""blocks.{i}.mlp.fc2.weight""", F"""vit.encoder.layer.{i}.output.dense.weight""") ) rename_keys.append((F"""blocks.{i}.mlp.fc2.bias""", F"""vit.encoder.layer.{i}.output.dense.bias""") ) if base_model: # layernorm + pooler rename_keys.extend( [ ('norm.weight', 'layernorm.weight'), ('norm.bias', 'layernorm.bias'), ('pre_logits.fc.weight', 'pooler.dense.weight'), ('pre_logits.fc.bias', 'pooler.dense.bias'), ] ) # if just the base model, we should remove "vit" from all keys that start with "vit" snake_case__ : List[str] = [(pair[0], pair[1][4:]) if pair[1].startswith('vit' ) else pair for pair in rename_keys] else: # layernorm + classification head rename_keys.extend( [ ('norm.weight', 'vit.layernorm.weight'), ('norm.bias', 'vit.layernorm.bias'), ('head.weight', 'classifier.weight'), ('head.bias', 'classifier.bias'), ] ) # fmt: on return rename_keys def UpperCamelCase__ ( A__ , A__ , A__=False ) -> Dict: for i in range(config.num_hidden_layers ): if base_model: snake_case__ : str = '' else: snake_case__ : str = 'vit.' # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) snake_case__ : Tuple = state_dict.pop(F"""blocks.{i}.attn.qkv.weight""" ) snake_case__ : Tuple = state_dict.pop(F"""blocks.{i}.attn.qkv.bias""" ) # next, add query, keys and values (in that order) to the state dict snake_case__ : Any = in_proj_weight[ : config.hidden_size, : ] snake_case__ : Union[str, Any] = in_proj_bias[: config.hidden_size] snake_case__ : int = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] snake_case__ : Tuple = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] snake_case__ : List[Any] = in_proj_weight[ -config.hidden_size :, : ] snake_case__ : Optional[Any] = in_proj_bias[-config.hidden_size :] def UpperCamelCase__ ( A__ ) -> Optional[Any]: snake_case__ : Dict = ['head.weight', 'head.bias'] for k in ignore_keys: state_dict.pop(A__ , A__ ) def UpperCamelCase__ ( A__ , A__ , A__ ) -> Optional[Any]: snake_case__ : str = dct.pop(A__ ) snake_case__ : Dict = val def UpperCamelCase__ ( ) -> Tuple: snake_case__ : List[str] = 'http://images.cocodataset.org/val2017/000000039769.jpg' snake_case__ : Union[str, Any] = Image.open(requests.get(A__ , stream=A__ ).raw ) return im @torch.no_grad() def UpperCamelCase__ ( A__ , A__ , A__=False ) -> Optional[Any]: snake_case__ : Optional[int] = BitConfig( global_padding='same' , layer_type='bottleneck' , depths=(3, 4, 9) , out_features=['stage3'] , embedding_dynamic_padding=A__ , ) snake_case__ : Any = ViTHybridConfig(backbone_config=A__ , image_size=384 , num_labels=1000 ) snake_case__ : List[Any] = False # load original model from timm snake_case__ : Any = timm.create_model(A__ , pretrained=A__ ) timm_model.eval() # load state_dict of original model, remove and rename some keys snake_case__ : Dict = timm_model.state_dict() if base_model: remove_classification_head_(A__ ) snake_case__ : Tuple = create_rename_keys(A__ , A__ ) for src, dest in rename_keys: rename_key(A__ , A__ , A__ ) read_in_q_k_v(A__ , A__ , A__ ) snake_case__ : Any = 'huggingface/label-files' snake_case__ : Union[str, Any] = 'imagenet-1k-id2label.json' snake_case__ : Dict = json.load(open(hf_hub_download(A__ , A__ , repo_type='dataset' ) , 'r' ) ) snake_case__ : Union[str, Any] = {int(A__ ): v for k, v in idalabel.items()} snake_case__ : Union[str, Any] = idalabel snake_case__ : Tuple = {v: k for k, v in idalabel.items()} # load HuggingFace model if vit_name[-5:] == "in21k": snake_case__ : Tuple = ViTHybridModel(A__ ).eval() else: snake_case__ : List[Any] = ViTHybridForImageClassification(A__ ).eval() model.load_state_dict(A__ ) # create image processor snake_case__ : int = create_transform(**resolve_data_config({} , model=A__ ) ) snake_case__ : List[Any] = transform.transforms snake_case__ : List[Any] = { 'bilinear': PILImageResampling.BILINEAR, 'bicubic': PILImageResampling.BICUBIC, 'nearest': PILImageResampling.NEAREST, } snake_case__ : Union[str, Any] = ViTHybridImageProcessor( do_resize=A__ , size={'shortest_edge': timm_transforms[0].size} , resample=pillow_resamplings[timm_transforms[0].interpolation.value] , do_center_crop=A__ , crop_size={'height': timm_transforms[1].size[0], 'width': timm_transforms[1].size[1]} , do_normalize=A__ , image_mean=timm_transforms[-1].mean.tolist() , image_std=timm_transforms[-1].std.tolist() , ) snake_case__ : Union[str, Any] = prepare_img() snake_case__ : List[Any] = transform(A__ ).unsqueeze(0 ) snake_case__ : List[str] = processor(A__ , return_tensors='pt' ).pixel_values # verify pixel values assert torch.allclose(A__ , A__ ) # verify logits with torch.no_grad(): snake_case__ : str = model(A__ ) snake_case__ : List[str] = outputs.logits print('Predicted class:' , logits.argmax(-1 ).item() ) if base_model: snake_case__ : Optional[int] = timm_model.forward_features(A__ ) assert timm_pooled_output.shape == outputs.pooler_output.shape assert torch.allclose(A__ , outputs.pooler_output , atol=1e-3 ) else: snake_case__ : Optional[int] = timm_model(A__ ) assert timm_logits.shape == outputs.logits.shape assert torch.allclose(A__ , outputs.logits , atol=1e-3 ) print('Looks ok!' ) if pytorch_dump_folder_path is not None: Path(A__ ).mkdir(exist_ok=A__ ) print(F"""Saving model {vit_name} to {pytorch_dump_folder_path}""" ) model.save_pretrained(A__ ) print(F"""Saving processor to {pytorch_dump_folder_path}""" ) processor.save_pretrained(A__ ) if push_to_hub: print(F"""Pushing model and processor to the hub {vit_name}""" ) model.push_to_hub(F"""ybelkada/{vit_name}""" ) processor.push_to_hub(F"""ybelkada/{vit_name}""" ) if __name__ == "__main__": lowerCAmelCase__ : str = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--vit_name''', default='''vit_base_r50_s16_384''', type=str, help='''Name of the hybrid ViT timm model you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) parser.add_argument( '''--push_to_hub''', action='''store_true''', help='''Whether to upload the model to the HuggingFace hub.''' ) lowerCAmelCase__ : Optional[Any] = parser.parse_args() convert_vit_checkpoint(args.vit_name, args.pytorch_dump_folder_path, args.push_to_hub)
711
from .data_collator import ( DataCollatorForLanguageModeling, DataCollatorForPermutationLanguageModeling, DataCollatorForSeqaSeq, DataCollatorForSOP, DataCollatorForTokenClassification, DataCollatorForWholeWordMask, DataCollatorWithPadding, DefaultDataCollator, default_data_collator, ) from .metrics import glue_compute_metrics, xnli_compute_metrics from .processors import ( DataProcessor, InputExample, InputFeatures, SingleSentenceClassificationProcessor, SquadExample, SquadFeatures, SquadVaProcessor, SquadVaProcessor, glue_convert_examples_to_features, glue_output_modes, glue_processors, glue_tasks_num_labels, squad_convert_examples_to_features, xnli_output_modes, xnli_processors, xnli_tasks_num_labels, )
699
0
from pathlib import Path import numpy as np from PIL import Image def UpperCamelCase__ ( A__ ) -> np.ndarray: snake_case__ : Any = rgb[:, :, 0], rgb[:, :, 1], rgb[:, :, 2] return 0.2_9_8_9 * r + 0.5_8_7_0 * g + 0.1_1_4_0 * b def UpperCamelCase__ ( A__ ) -> np.ndarray: return (gray > 127) & (gray <= 255) def UpperCamelCase__ ( A__ , A__ ) -> np.ndarray: snake_case__ : Optional[Any] = np.zeros_like(A__ ) snake_case__ : Optional[Any] = np.zeros( (image.shape[0] + kernel.shape[0] - 1, image.shape[1] + kernel.shape[1] - 1) ) # Copy image to padded image snake_case__ : List[Any] = image # Iterate over image & apply kernel for x in range(image.shape[1] ): for y in range(image.shape[0] ): snake_case__ : Tuple = ( kernel * image_padded[y : y + kernel.shape[0], x : x + kernel.shape[1]] ).sum() snake_case__ : List[str] = int(summation > 0 ) return output if __name__ == "__main__": # read original image lowerCAmelCase__ : Optional[Any] = Path(__file__).resolve().parent / '''image_data''' / '''lena.jpg''' lowerCAmelCase__ : Optional[Any] = np.array(Image.open(lena_path)) # kernel to be applied lowerCAmelCase__ : Dict = np.array([[0, 1, 0], [1, 1, 1], [0, 1, 0]]) lowerCAmelCase__ : Any = dilation(gray_to_binary(rgb_to_gray(lena)), structuring_element) # Save the output image lowerCAmelCase__ : int = Image.fromarray(output).convert('''RGB''') pil_img.save('''result_dilation.png''')
712
from dataclasses import dataclass, field from typing import Optional from transformers import AutoConfig, AutoImageProcessor, AutoTokenizer, FlaxVisionEncoderDecoderModel, HfArgumentParser @dataclass class __snake_case : __lowerCamelCase = field( metadata={"""help""": """The output directory where the model will be written."""} ,) __lowerCamelCase = field( metadata={ """help""": ( """The encoder model checkpoint for weights initialization.""" """Don't set if you want to train an encoder model from scratch.""" ) } ,) __lowerCamelCase = field( metadata={ """help""": ( """The decoder model checkpoint for weights initialization.""" """Don't set if you want to train a decoder model from scratch.""" ) } ,) __lowerCamelCase = field( default=_lowerCamelCase ,metadata={"""help""": """Pretrained encoder config name or path if not the same as encoder_model_name"""} ) __lowerCamelCase = field( default=_lowerCamelCase ,metadata={"""help""": """Pretrained decoder config name or path if not the same as decoder_model_name"""} ) def UpperCamelCase__ ( ) -> Union[str, Any]: snake_case__ : str = HfArgumentParser((ModelArguments,) ) ((snake_case__) , ) : Dict = parser.parse_args_into_dataclasses() # Load pretrained model and tokenizer # Use explicit specified encoder config if model_args.encoder_config_name: snake_case__ : List[str] = AutoConfig.from_pretrained(model_args.encoder_config_name ) # Use pretrained encoder model's config else: snake_case__ : Optional[int] = AutoConfig.from_pretrained(model_args.encoder_model_name_or_path ) # Use explicit specified decoder config if model_args.decoder_config_name: snake_case__ : Optional[Any] = AutoConfig.from_pretrained(model_args.decoder_config_name ) # Use pretrained decoder model's config else: snake_case__ : List[str] = AutoConfig.from_pretrained(model_args.decoder_model_name_or_path ) # necessary for `from_encoder_decoder_pretrained` when `decoder_config` is passed snake_case__ : Any = True snake_case__ : Dict = True snake_case__ : Tuple = FlaxVisionEncoderDecoderModel.from_encoder_decoder_pretrained( encoder_pretrained_model_name_or_path=model_args.encoder_model_name_or_path , decoder_pretrained_model_name_or_path=model_args.decoder_model_name_or_path , encoder_config=A__ , decoder_config=A__ , ) # GPT2 only has bos/eos tokens but not decoder_start/pad tokens snake_case__ : Optional[Any] = decoder_config.decoder_start_token_id snake_case__ : Tuple = decoder_config.pad_token_id if decoder_start_token_id is None: snake_case__ : Optional[Any] = decoder_config.bos_token_id if pad_token_id is None: snake_case__ : int = decoder_config.eos_token_id # This is necessary to make Flax's generate() work snake_case__ : Union[str, Any] = decoder_config.eos_token_id snake_case__ : Optional[int] = decoder_start_token_id snake_case__ : int = pad_token_id snake_case__ : Tuple = AutoImageProcessor.from_pretrained(model_args.encoder_model_name_or_path ) snake_case__ : int = AutoTokenizer.from_pretrained(model_args.decoder_model_name_or_path ) snake_case__ : Tuple = tokenizer.convert_ids_to_tokens(model.config.pad_token_id ) model.save_pretrained(model_args.output_dir ) image_processor.save_pretrained(model_args.output_dir ) tokenizer.save_pretrained(model_args.output_dir ) if __name__ == "__main__": main()
699
0
import os def UpperCamelCase__ ( ) -> int: snake_case__ : Optional[Any] = os.path.join(os.path.dirname(A__ ) , 'num.txt' ) with open(A__ ) as file_hand: return str(sum(int(A__ ) for line in file_hand ) )[:10] if __name__ == "__main__": print(solution())
713
import logging import os from dataclasses import dataclass, field from typing import Dict, Optional import datasets import numpy as np import tensorflow as tf from transformers import ( AutoConfig, AutoTokenizer, EvalPrediction, HfArgumentParser, PreTrainedTokenizer, TFAutoModelForSequenceClassification, TFTrainer, TFTrainingArguments, ) from transformers.utils import logging as hf_logging hf_logging.set_verbosity_info() hf_logging.enable_default_handler() hf_logging.enable_explicit_format() def UpperCamelCase__ ( A__ , A__ , A__ , A__ , A__ , A__ = None , ) -> Optional[int]: snake_case__ : List[str] = {} if train_file is not None: snake_case__ : Tuple = [train_file] if eval_file is not None: snake_case__ : Dict = [eval_file] if test_file is not None: snake_case__ : str = [test_file] snake_case__ : Optional[Any] = datasets.load_dataset('csv' , data_files=A__ ) snake_case__ : Any = list(ds[list(files.keys() )[0]].features.keys() ) snake_case__ : Optional[Any] = features_name.pop(A__ ) snake_case__ : Optional[Any] = list(set(ds[list(files.keys() )[0]][label_name] ) ) snake_case__ : str = {label: i for i, label in enumerate(A__ )} snake_case__ : int = tokenizer.model_input_names snake_case__ : int = {} if len(A__ ) == 1: for k in files.keys(): snake_case__ : str = ds[k].map( lambda A__ : tokenizer.batch_encode_plus( example[features_name[0]] , truncation=A__ , max_length=A__ , padding='max_length' ) , batched=A__ , ) elif len(A__ ) == 2: for k in files.keys(): snake_case__ : Optional[int] = ds[k].map( lambda A__ : tokenizer.batch_encode_plus( (example[features_name[0]], example[features_name[1]]) , truncation=A__ , max_length=A__ , padding='max_length' , ) , batched=A__ , ) def gen_train(): for ex in transformed_ds[datasets.Split.TRAIN]: snake_case__ : int = {k: v for k, v in ex.items() if k in input_names} snake_case__ : Any = labelaid[ex[label_name]] yield (d, label) def gen_val(): for ex in transformed_ds[datasets.Split.VALIDATION]: snake_case__ : int = {k: v for k, v in ex.items() if k in input_names} snake_case__ : Union[str, Any] = labelaid[ex[label_name]] yield (d, label) def gen_test(): for ex in transformed_ds[datasets.Split.TEST]: snake_case__ : Dict = {k: v for k, v in ex.items() if k in input_names} snake_case__ : List[str] = labelaid[ex[label_name]] yield (d, label) snake_case__ : Any = ( tf.data.Dataset.from_generator( A__ , ({k: tf.intaa for k in input_names}, tf.intaa) , ({k: tf.TensorShape([None] ) for k in input_names}, tf.TensorShape([] )) , ) if datasets.Split.TRAIN in transformed_ds else None ) if train_ds is not None: snake_case__ : str = train_ds.apply(tf.data.experimental.assert_cardinality(len(ds[datasets.Split.TRAIN] ) ) ) snake_case__ : Optional[int] = ( tf.data.Dataset.from_generator( A__ , ({k: tf.intaa for k in input_names}, tf.intaa) , ({k: tf.TensorShape([None] ) for k in input_names}, tf.TensorShape([] )) , ) if datasets.Split.VALIDATION in transformed_ds else None ) if val_ds is not None: snake_case__ : Optional[int] = val_ds.apply(tf.data.experimental.assert_cardinality(len(ds[datasets.Split.VALIDATION] ) ) ) snake_case__ : List[str] = ( tf.data.Dataset.from_generator( A__ , ({k: tf.intaa for k in input_names}, tf.intaa) , ({k: tf.TensorShape([None] ) for k in input_names}, tf.TensorShape([] )) , ) if datasets.Split.TEST in transformed_ds else None ) if test_ds is not None: snake_case__ : str = test_ds.apply(tf.data.experimental.assert_cardinality(len(ds[datasets.Split.TEST] ) ) ) return train_ds, val_ds, test_ds, labelaid lowerCAmelCase__ : List[str] = logging.getLogger(__name__) @dataclass class __snake_case : __lowerCamelCase = field(metadata={"""help""": """Which column contains the label"""} ) __lowerCamelCase = field(default=_lowerCamelCase ,metadata={"""help""": """The path of the training file"""} ) __lowerCamelCase = field(default=_lowerCamelCase ,metadata={"""help""": """The path of the development file"""} ) __lowerCamelCase = field(default=_lowerCamelCase ,metadata={"""help""": """The path of the test file"""} ) __lowerCamelCase = field( default=128 ,metadata={ """help""": ( """The maximum total input sequence length after tokenization. Sequences longer """ """than this will be truncated, sequences shorter will be padded.""" ) } ,) __lowerCamelCase = field( default=_lowerCamelCase ,metadata={"""help""": """Overwrite the cached training and evaluation sets"""} ) @dataclass class __snake_case : __lowerCamelCase = field( metadata={"""help""": """Path to pretrained model or model identifier from huggingface.co/models"""} ) __lowerCamelCase = field( default=_lowerCamelCase ,metadata={"""help""": """Pretrained config name or path if not the same as model_name"""} ) __lowerCamelCase = field( default=_lowerCamelCase ,metadata={"""help""": """Pretrained tokenizer name or path if not the same as model_name"""} ) __lowerCamelCase = field(default=_lowerCamelCase ,metadata={"""help""": """Set this flag to use fast tokenization."""} ) # If you want to tweak more attributes on your tokenizer, you should do it in a distinct script, # or just modify its tokenizer_config.json. __lowerCamelCase = field( default=_lowerCamelCase ,metadata={"""help""": """Where do you want to store the pretrained models downloaded from huggingface.co"""} ,) def UpperCamelCase__ ( ) -> Union[str, Any]: # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. snake_case__ : int = HfArgumentParser((ModelArguments, DataTrainingArguments, TFTrainingArguments) ) snake_case__ , snake_case__ , snake_case__ : Dict = parser.parse_args_into_dataclasses() if ( os.path.exists(training_args.output_dir ) and os.listdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir ): raise ValueError( F"""Output directory ({training_args.output_dir}) already exists and is not empty. Use""" ' --overwrite_output_dir to overcome.' ) # Setup logging logging.basicConfig( format='%(asctime)s - %(levelname)s - %(name)s - %(message)s' , datefmt='%m/%d/%Y %H:%M:%S' , level=logging.INFO , ) logger.info( F"""n_replicas: {training_args.n_replicas}, distributed training: {bool(training_args.n_replicas > 1 )}, """ F"""16-bits training: {training_args.fpaa}""" ) logger.info(F"""Training/evaluation parameters {training_args}""" ) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. snake_case__ : Dict = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , ) snake_case__ , snake_case__ , snake_case__ , snake_case__ : Dict = get_tfds( train_file=data_args.train_file , eval_file=data_args.dev_file , test_file=data_args.test_file , tokenizer=A__ , label_column_id=data_args.label_column_id , max_seq_length=data_args.max_seq_length , ) snake_case__ : Dict = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , num_labels=len(A__ ) , labelaid=A__ , idalabel={id: label for label, id in labelaid.items()} , finetuning_task='text-classification' , cache_dir=model_args.cache_dir , ) with training_args.strategy.scope(): snake_case__ : Optional[Any] = TFAutoModelForSequenceClassification.from_pretrained( model_args.model_name_or_path , from_pt=bool('.bin' in model_args.model_name_or_path ) , config=A__ , cache_dir=model_args.cache_dir , ) def compute_metrics(A__ ) -> Dict: snake_case__ : Optional[Any] = np.argmax(p.predictions , axis=1 ) return {"acc": (preds == p.label_ids).mean()} # Initialize our Trainer snake_case__ : Any = TFTrainer( model=A__ , args=A__ , train_dataset=A__ , eval_dataset=A__ , compute_metrics=A__ , ) # Training if training_args.do_train: trainer.train() trainer.save_model() tokenizer.save_pretrained(training_args.output_dir ) # Evaluation snake_case__ : Dict = {} if training_args.do_eval: logger.info('*** Evaluate ***' ) snake_case__ : Tuple = trainer.evaluate() snake_case__ : Any = os.path.join(training_args.output_dir , 'eval_results.txt' ) with open(A__ , 'w' ) as writer: logger.info('***** Eval results *****' ) for key, value in result.items(): logger.info(F""" {key} = {value}""" ) writer.write(F"""{key} = {value}\n""" ) results.update(A__ ) return results if __name__ == "__main__": main()
699
0
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available, is_vision_available, ) lowerCAmelCase__ : Any = { '''configuration_blip''': [ '''BLIP_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''BlipConfig''', '''BlipTextConfig''', '''BlipVisionConfig''', ], '''processing_blip''': ['''BlipProcessor'''], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : Optional[int] = ['''BlipImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : Tuple = [ '''BLIP_PRETRAINED_MODEL_ARCHIVE_LIST''', '''BlipModel''', '''BlipPreTrainedModel''', '''BlipForConditionalGeneration''', '''BlipForQuestionAnswering''', '''BlipVisionModel''', '''BlipTextModel''', '''BlipForImageTextRetrieval''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : Union[str, Any] = [ '''TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFBlipModel''', '''TFBlipPreTrainedModel''', '''TFBlipForConditionalGeneration''', '''TFBlipForQuestionAnswering''', '''TFBlipVisionModel''', '''TFBlipTextModel''', '''TFBlipForImageTextRetrieval''', ] if TYPE_CHECKING: from .configuration_blip import BLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, BlipConfig, BlipTextConfig, BlipVisionConfig from .processing_blip import BlipProcessor try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_blip import BlipImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_blip import ( BLIP_PRETRAINED_MODEL_ARCHIVE_LIST, BlipForConditionalGeneration, BlipForImageTextRetrieval, BlipForQuestionAnswering, BlipModel, BlipPreTrainedModel, BlipTextModel, BlipVisionModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_blip import ( TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST, TFBlipForConditionalGeneration, TFBlipForImageTextRetrieval, TFBlipForQuestionAnswering, TFBlipModel, TFBlipPreTrainedModel, TFBlipTextModel, TFBlipVisionModel, ) else: import sys lowerCAmelCase__ : int = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
714
from typing import List import datasets from datasets.tasks import AudioClassification from ..folder_based_builder import folder_based_builder lowerCAmelCase__ : List[Any] = datasets.utils.logging.get_logger(__name__) class __snake_case ( folder_based_builder.FolderBasedBuilderConfig ): __lowerCamelCase = None __lowerCamelCase = None class __snake_case ( folder_based_builder.FolderBasedBuilder ): __lowerCamelCase = datasets.Audio() __lowerCamelCase = """audio""" __lowerCamelCase = AudioFolderConfig __lowerCamelCase = 42 # definition at the bottom of the script __lowerCamelCase = AudioClassification(audio_column="""audio""" ,label_column="""label""" ) lowerCAmelCase__ : Tuple = [ '''.aiff''', '''.au''', '''.avr''', '''.caf''', '''.flac''', '''.htk''', '''.svx''', '''.mat4''', '''.mat5''', '''.mpc2k''', '''.ogg''', '''.paf''', '''.pvf''', '''.raw''', '''.rf64''', '''.sd2''', '''.sds''', '''.ircam''', '''.voc''', '''.w64''', '''.wav''', '''.nist''', '''.wavex''', '''.wve''', '''.xi''', '''.mp3''', '''.opus''', ] lowerCAmelCase__ : List[Any] = AUDIO_EXTENSIONS
699
0
import json import sys def UpperCamelCase__ ( A__ , A__ ) -> Union[str, Any]: with open(A__ , encoding='utf-8' ) as f: snake_case__ : Optional[int] = json.load(A__ ) snake_case__ : Optional[int] = ['<details>', '<summary>Show updated benchmarks!</summary>', ' '] for benchmark_name in sorted(A__ ): snake_case__ : Union[str, Any] = results[benchmark_name] snake_case__ : List[Any] = benchmark_name.split('/' )[-1] output_md.append(F"""### Benchmark: {benchmark_file_name}""" ) snake_case__ : List[str] = '| metric |' snake_case__ : Any = '|--------|' snake_case__ : List[Any] = '| new / old (diff) |' for metric_name in sorted(A__ ): snake_case__ : Union[str, Any] = benchmark_res[metric_name] snake_case__ : int = metric_vals['new'] snake_case__ : Optional[Any] = metric_vals.get('old' , A__ ) snake_case__ : List[Any] = metric_vals.get('diff' , A__ ) snake_case__ : List[str] = F""" {new_val:f}""" if isinstance(A__ , (int, float) ) else 'None' if old_val is not None: val_str += F""" / {old_val:f}""" if isinstance(A__ , (int, float) ) else "None" if dif_val is not None: val_str += F""" ({dif_val:f})""" if isinstance(A__ , (int, float) ) else "None" title += " " + metric_name + " |" lines += "---|" value += val_str + " |" output_md += [title, lines, value, " "] output_md.append('</details>' ) with open(A__ , 'w' , encoding='utf-8' ) as f: f.writelines('\n'.join(A__ ) ) if __name__ == "__main__": lowerCAmelCase__ : Tuple = sys.argv[1] lowerCAmelCase__ : List[str] = sys.argv[2] format_json_to_md(input_json_file, output_md_file)
715
import random import unittest import torch from diffusers import IFInpaintingPipeline from diffusers.utils import floats_tensor from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import skip_mps, torch_device from ..pipeline_params import ( TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS, ) from ..test_pipelines_common import PipelineTesterMixin from . import IFPipelineTesterMixin @skip_mps class __snake_case ( _lowerCamelCase ,_lowerCamelCase ,unittest.TestCase ): __lowerCamelCase = IFInpaintingPipeline __lowerCamelCase = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {"""width""", """height"""} __lowerCamelCase = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS __lowerCamelCase = PipelineTesterMixin.required_optional_params - {"""latents"""} def __a ( self ) -> Optional[Any]: '''simple docstring''' return self._get_dummy_components() def __a ( self , __UpperCamelCase , __UpperCamelCase=0 ) -> str: '''simple docstring''' if str(__UpperCamelCase ).startswith('mps' ): snake_case__ : int = torch.manual_seed(__UpperCamelCase ) else: snake_case__ : Union[str, Any] = torch.Generator(device=__UpperCamelCase ).manual_seed(__UpperCamelCase ) snake_case__ : Optional[int] = floats_tensor((1, 3, 32, 32) , rng=random.Random(__UpperCamelCase ) ).to(__UpperCamelCase ) snake_case__ : Dict = floats_tensor((1, 3, 32, 32) , rng=random.Random(__UpperCamelCase ) ).to(__UpperCamelCase ) snake_case__ : Optional[Any] = { 'prompt': 'A painting of a squirrel eating a burger', 'image': image, 'mask_image': mask_image, 'generator': generator, 'num_inference_steps': 2, 'output_type': 'numpy', } return inputs @unittest.skipIf( torch_device != 'cuda' or not is_xformers_available() , reason='XFormers attention is only available with CUDA and `xformers` installed' , ) def __a ( self ) -> List[Any]: '''simple docstring''' self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3 ) def __a ( self ) -> Optional[int]: '''simple docstring''' self._test_save_load_optional_components() @unittest.skipIf(torch_device != 'cuda' , reason='float16 requires CUDA' ) def __a ( self ) -> List[str]: '''simple docstring''' super().test_save_load_floataa(expected_max_diff=1E-1 ) def __a ( self ) -> List[str]: '''simple docstring''' self._test_attention_slicing_forward_pass(expected_max_diff=1E-2 ) def __a ( self ) -> int: '''simple docstring''' self._test_save_load_local() def __a ( self ) -> List[str]: '''simple docstring''' self._test_inference_batch_single_identical( expected_max_diff=1E-2 , )
699
0
# Copyright 2022 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import argparse import os import platform import numpy as np import psutil import torch from accelerate import __version__ as version from accelerate.commands.config import default_config_file, load_config_from_file from ..utils import is_npu_available, is_xpu_available def UpperCamelCase__ ( A__=None ) -> int: if subparsers is not None: snake_case__ : int = subparsers.add_parser('env' ) else: snake_case__ : Optional[int] = argparse.ArgumentParser('Accelerate env command' ) parser.add_argument( '--config_file' , default=A__ , help='The config file to use for the default values in the launching script.' ) if subparsers is not None: parser.set_defaults(func=A__ ) return parser def UpperCamelCase__ ( A__ ) -> Union[str, Any]: snake_case__ : int = torch.__version__ snake_case__ : Dict = torch.cuda.is_available() snake_case__ : Tuple = is_xpu_available() snake_case__ : Optional[Any] = is_npu_available() snake_case__ : Union[str, Any] = 'Not found' # Get the default from the config file. if args.config_file is not None or os.path.isfile(A__ ): snake_case__ : Tuple = load_config_from_file(args.config_file ).to_dict() snake_case__ : Union[str, Any] = { '`Accelerate` version': version, 'Platform': platform.platform(), 'Python version': platform.python_version(), 'Numpy version': np.__version__, 'PyTorch version (GPU?)': F"""{pt_version} ({pt_cuda_available})""", 'PyTorch XPU available': str(A__ ), 'PyTorch NPU available': str(A__ ), 'System RAM': F"""{psutil.virtual_memory().total / 1024 ** 3:.2f} GB""", } if pt_cuda_available: snake_case__ : int = torch.cuda.get_device_name() print('\nCopy-and-paste the text below in your GitHub issue\n' ) print('\n'.join([F"""- {prop}: {val}""" for prop, val in info.items()] ) ) print('- `Accelerate` default config:' if args.config_file is None else '- `Accelerate` config passed:' ) snake_case__ : Dict = ( '\n'.join([F"""\t- {prop}: {val}""" for prop, val in accelerate_config.items()] ) if isinstance(A__ , A__ ) else F"""\t{accelerate_config}""" ) print(A__ ) snake_case__ : List[str] = accelerate_config return info def UpperCamelCase__ ( ) -> int: snake_case__ : List[Any] = env_command_parser() snake_case__ : List[Any] = parser.parse_args() env_command(A__ ) return 0 if __name__ == "__main__": raise SystemExit(main())
716
import unittest from transformers import BertGenerationTokenizer from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_torch, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin lowerCAmelCase__ : List[Any] = '''▁''' lowerCAmelCase__ : int = get_tests_dir('''fixtures/test_sentencepiece.model''') @require_sentencepiece class __snake_case ( _lowerCamelCase ,unittest.TestCase ): __lowerCamelCase = BertGenerationTokenizer __lowerCamelCase = False __lowerCamelCase = True def __a ( self ) -> Optional[int]: '''simple docstring''' super().setUp() snake_case__ : str = BertGenerationTokenizer(__UpperCamelCase , keep_accents=__UpperCamelCase ) tokenizer.save_pretrained(self.tmpdirname ) def __a ( self ) -> Optional[int]: '''simple docstring''' snake_case__ : List[str] = '<s>' snake_case__ : Dict = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(__UpperCamelCase ) , __UpperCamelCase ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(__UpperCamelCase ) , __UpperCamelCase ) def __a ( self ) -> List[str]: '''simple docstring''' snake_case__ : Tuple = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '<unk>' ) self.assertEqual(vocab_keys[1] , '<s>' ) self.assertEqual(vocab_keys[-1] , '<pad>' ) self.assertEqual(len(__UpperCamelCase ) , 1002 ) def __a ( self ) -> int: '''simple docstring''' self.assertEqual(self.get_tokenizer().vocab_size , 1000 ) def __a ( self ) -> Tuple: '''simple docstring''' snake_case__ : Optional[Any] = BertGenerationTokenizer(__UpperCamelCase , keep_accents=__UpperCamelCase ) snake_case__ : int = tokenizer.tokenize('This is a test' ) self.assertListEqual(__UpperCamelCase , ['▁This', '▁is', '▁a', '▁t', 'est'] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(__UpperCamelCase ) , [285, 46, 10, 170, 382] , ) snake_case__ : Any = tokenizer.tokenize('I was born in 92000, and this is falsé.' ) self.assertListEqual( __UpperCamelCase , [ SPIECE_UNDERLINE + 'I', SPIECE_UNDERLINE + 'was', SPIECE_UNDERLINE + 'b', 'or', 'n', SPIECE_UNDERLINE + 'in', SPIECE_UNDERLINE + '', '9', '2', '0', '0', '0', ',', SPIECE_UNDERLINE + 'and', SPIECE_UNDERLINE + 'this', SPIECE_UNDERLINE + 'is', SPIECE_UNDERLINE + 'f', 'al', 's', 'é', '.', ] , ) snake_case__ : Optional[Any] = tokenizer.convert_tokens_to_ids(__UpperCamelCase ) self.assertListEqual( __UpperCamelCase , [8, 21, 84, 55, 24, 19, 7, 0, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 0, 4] , ) snake_case__ : int = tokenizer.convert_ids_to_tokens(__UpperCamelCase ) self.assertListEqual( __UpperCamelCase , [ SPIECE_UNDERLINE + 'I', SPIECE_UNDERLINE + 'was', SPIECE_UNDERLINE + 'b', 'or', 'n', SPIECE_UNDERLINE + 'in', SPIECE_UNDERLINE + '', '<unk>', '2', '0', '0', '0', ',', SPIECE_UNDERLINE + 'and', SPIECE_UNDERLINE + 'this', SPIECE_UNDERLINE + 'is', SPIECE_UNDERLINE + 'f', 'al', 's', '<unk>', '.', ] , ) @cached_property def __a ( self ) -> Dict: '''simple docstring''' return BertGenerationTokenizer.from_pretrained('google/bert_for_seq_generation_L-24_bbc_encoder' ) @slow def __a ( self ) -> Any: '''simple docstring''' snake_case__ : int = 'Hello World!' snake_case__ : Union[str, Any] = [18536, 2260, 101] self.assertListEqual(__UpperCamelCase , self.big_tokenizer.encode(__UpperCamelCase ) ) @slow def __a ( self ) -> Optional[int]: '''simple docstring''' snake_case__ : str = ( 'This is a very long text with a lot of weird characters, such as: . , ~ ? ( ) " [ ] ! : - . Also we will' ' add words that should not exsist and be tokenized to <unk>, such as saoneuhaoesuth' ) snake_case__ : List[Any] = [ 871, 419, 358, 946, 991, 2521, 452, 358, 1357, 387, 7751, 3536, 112, 985, 456, 126, 865, 938, 5400, 5734, 458, 1368, 467, 786, 2462, 5246, 1159, 633, 865, 4519, 457, 582, 852, 2557, 427, 916, 508, 405, 34324, 497, 391, 408, 11342, 1244, 385, 100, 938, 985, 456, 574, 362, 12597, 3200, 3129, 1172, ] self.assertListEqual(__UpperCamelCase , self.big_tokenizer.encode(__UpperCamelCase ) ) @require_torch @slow def __a ( self ) -> List[str]: '''simple docstring''' import torch from transformers import BertGenerationConfig, BertGenerationEncoder # Build sequence snake_case__ : Optional[int] = list(self.big_tokenizer.get_vocab().keys() )[:10] snake_case__ : Optional[int] = ' '.join(__UpperCamelCase ) snake_case__ : int = self.big_tokenizer.encode_plus(__UpperCamelCase , return_tensors='pt' , return_token_type_ids=__UpperCamelCase ) snake_case__ : Tuple = self.big_tokenizer.batch_encode_plus( [sequence + ' ' + sequence] , return_tensors='pt' , return_token_type_ids=__UpperCamelCase ) snake_case__ : Dict = BertGenerationConfig() snake_case__ : List[str] = BertGenerationEncoder(__UpperCamelCase ) assert model.get_input_embeddings().weight.shape[0] >= self.big_tokenizer.vocab_size with torch.no_grad(): model(**__UpperCamelCase ) model(**__UpperCamelCase ) @slow def __a ( self ) -> Dict: '''simple docstring''' snake_case__ : Optional[int] = {'input_ids': [[39286, 458, 36335, 2001, 456, 13073, 13266, 455, 113, 7746, 1741, 11157, 391, 13073, 13266, 455, 113, 3967, 35412, 113, 4936, 109, 3870, 2377, 113, 30084, 45720, 458, 134, 17496, 112, 503, 11672, 113, 118, 112, 5665, 13347, 38687, 112, 1496, 31389, 112, 3268, 47264, 134, 962, 112, 16377, 8035, 23130, 430, 12169, 15518, 28592, 458, 146, 41697, 109, 391, 12169, 15518, 16689, 458, 146, 41358, 109, 452, 726, 4034, 111, 763, 35412, 5082, 388, 1903, 111, 9051, 391, 2870, 48918, 1900, 1123, 550, 998, 112, 9586, 15985, 455, 391, 410, 22955, 37636, 114], [448, 17496, 419, 3663, 385, 763, 113, 27533, 2870, 3283, 13043, 1639, 24713, 523, 656, 24013, 18550, 2521, 517, 27014, 21244, 420, 1212, 1465, 391, 927, 4833, 388, 578, 11786, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [484, 2169, 7687, 21932, 18146, 726, 363, 17032, 3391, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], 'attention_mask': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=__UpperCamelCase , model_name='google/bert_for_seq_generation_L-24_bbc_encoder' , revision='c817d1fd1be2ffa69431227a1fe320544943d4db' , )
699
0
from itertools import product def UpperCamelCase__ ( A__ , A__ ) -> list[int]: snake_case__ : Optional[Any] = sides_number snake_case__ : Any = max_face_number * dice_number snake_case__ : Optional[int] = [0] * (max_total + 1) snake_case__ : Optional[int] = 1 snake_case__ : str = range(A__ , max_face_number + 1 ) for dice_numbers in product(A__ , repeat=A__ ): snake_case__ : Optional[int] = sum(A__ ) totals_frequencies[total] += 1 return totals_frequencies def UpperCamelCase__ ( ) -> float: snake_case__ : int = total_frequency_distribution( sides_number=4 , dice_number=9 ) snake_case__ : Any = total_frequency_distribution( sides_number=6 , dice_number=6 ) snake_case__ : Tuple = 0 snake_case__ : List[str] = 9 snake_case__ : Any = 4 * 9 snake_case__ : Any = 6 for peter_total in range(A__ , max_peter_total + 1 ): peter_wins_count += peter_totals_frequencies[peter_total] * sum( colin_totals_frequencies[min_colin_total:peter_total] ) snake_case__ : str = (4**9) * (6**6) snake_case__ : Any = peter_wins_count / total_games_number snake_case__ : List[str] = round(A__ , ndigits=7 ) return rounded_peter_win_probability if __name__ == "__main__": print(F'''{solution() = }''')
717
import random import torch from huggingface_hub import HfApi from diffusers import UNetaDModel lowerCAmelCase__ : List[str] = HfApi() lowerCAmelCase__ : str = {} # fmt: off lowerCAmelCase__ : int = torch.tensor([ -0.75_15, -1.68_83, 0.24_20, 0.03_00, 0.63_47, 1.34_33, -1.17_43, -3.74_67, 1.23_42, -2.24_85, 0.46_36, 0.80_76, -0.79_91, 0.39_69, 0.84_98, 0.91_89, -1.88_87, -3.35_22, 0.76_39, 0.20_40, 0.62_71, -2.71_48, -1.63_16, 3.08_39, 0.31_86, 0.27_21, -0.97_59, -1.24_61, 2.62_57, 1.35_57 ]) lowerCAmelCase__ : Dict = torch.tensor([ -2.36_39, -2.53_44, 0.00_54, -0.66_74, 1.59_90, 1.01_58, 0.31_24, -2.14_36, 1.87_95, -2.54_29, -0.15_66, -0.39_73, 1.24_90, 2.64_47, 1.22_83, -0.52_08, -2.81_54, -3.51_19, 2.38_38, 1.20_33, 1.72_01, -2.12_56, -1.45_76, 2.79_48, 2.42_04, -0.97_52, -1.25_46, 0.80_27, 3.27_58, 3.13_65 ]) lowerCAmelCase__ : Dict = torch.tensor([ -0.65_31, -0.68_91, -0.31_72, -0.53_75, -0.91_40, -0.53_67, -0.11_75, -0.78_69, -0.38_08, -0.45_13, -0.20_98, -0.00_83, 0.31_83, 0.51_40, 0.22_47, -0.13_04, -0.13_02, -0.28_02, -0.20_84, -0.20_25, -0.49_67, -0.48_73, -0.08_61, 0.69_25, 0.02_50, 0.12_90, -0.15_43, 0.63_16, 1.04_60, 1.49_43 ]) lowerCAmelCase__ : List[str] = torch.tensor([ 0.09_11, 0.11_07, 0.01_82, 0.04_35, -0.08_05, -0.06_08, 0.03_81, 0.21_72, -0.02_80, 0.13_27, -0.02_99, -0.02_55, -0.00_50, -0.11_70, -0.10_46, 0.03_09, 0.13_67, 0.17_28, -0.05_33, -0.07_48, -0.05_34, 0.16_24, 0.03_84, -0.18_05, -0.07_07, 0.06_42, 0.02_20, -0.01_34, -0.13_33, -0.15_05 ]) lowerCAmelCase__ : Union[str, Any] = torch.tensor([ 0.13_21, 0.13_37, 0.04_40, 0.06_22, -0.05_91, -0.03_70, 0.05_03, 0.21_33, -0.01_77, 0.14_15, -0.01_16, -0.01_12, 0.00_44, -0.09_80, -0.07_89, 0.03_95, 0.15_02, 0.17_85, -0.04_88, -0.05_14, -0.04_04, 0.15_39, 0.04_54, -0.15_59, -0.06_65, 0.06_59, 0.03_83, -0.00_05, -0.12_66, -0.13_86 ]) lowerCAmelCase__ : List[Any] = torch.tensor([ 0.11_54, 0.12_18, 0.03_07, 0.05_26, -0.07_11, -0.05_41, 0.03_66, 0.20_78, -0.02_67, 0.13_17, -0.02_26, -0.01_93, -0.00_14, -0.10_55, -0.09_02, 0.03_30, 0.13_91, 0.17_09, -0.05_62, -0.06_93, -0.05_60, 0.14_82, 0.03_81, -0.16_83, -0.06_81, 0.06_61, 0.03_31, -0.00_46, -0.12_68, -0.14_31 ]) lowerCAmelCase__ : Optional[Any] = torch.tensor([ 0.11_92, 0.12_40, 0.04_14, 0.06_06, -0.05_57, -0.04_12, 0.04_30, 0.20_42, -0.02_00, 0.13_85, -0.01_15, -0.01_32, 0.00_17, -0.09_65, -0.08_02, 0.03_98, 0.14_33, 0.17_47, -0.04_58, -0.05_33, -0.04_07, 0.15_45, 0.04_19, -0.15_74, -0.06_45, 0.06_26, 0.03_41, -0.00_10, -0.11_99, -0.13_90 ]) lowerCAmelCase__ : List[str] = torch.tensor([ 0.10_75, 0.10_74, 0.02_05, 0.04_31, -0.07_74, -0.06_07, 0.02_98, 0.20_42, -0.03_20, 0.12_67, -0.02_81, -0.02_50, -0.00_64, -0.10_91, -0.09_46, 0.02_90, 0.13_28, 0.16_50, -0.05_80, -0.07_38, -0.05_86, 0.14_40, 0.03_37, -0.17_46, -0.07_12, 0.06_05, 0.02_50, -0.00_99, -0.13_16, -0.14_73 ]) lowerCAmelCase__ : List[str] = torch.tensor([ -1.45_72, -2.04_81, -0.04_14, -0.60_05, 1.41_36, 0.58_48, 0.40_28, -2.73_30, 1.22_12, -2.12_28, 0.21_55, 0.40_39, 0.76_62, 2.05_35, 0.74_77, -0.32_43, -2.17_58, -2.76_48, 1.69_47, 0.70_26, 1.23_38, -1.60_78, -0.86_82, 2.28_10, 1.85_74, -0.57_18, -0.55_86, -0.01_86, 2.34_15, 2.12_51]) lowerCAmelCase__ : List[Any] = torch.tensor([ -1.36_90, -1.97_20, -0.40_90, -0.69_66, 1.46_60, 0.99_38, -0.13_85, -2.73_24, 0.77_36, -1.89_17, 0.29_23, 0.42_93, 0.16_93, 1.41_12, 1.18_87, -0.31_81, -2.21_60, -2.63_81, 1.31_70, 0.81_63, 0.92_40, -1.65_44, -0.60_99, 2.52_59, 1.64_30, -0.90_90, -0.93_92, -0.01_26, 2.42_68, 2.32_66 ]) lowerCAmelCase__ : Tuple = torch.tensor([ -1.35_25, -1.96_28, -0.39_56, -0.68_60, 1.46_64, 1.00_14, -0.12_59, -2.72_12, 0.77_72, -1.88_11, 0.29_96, 0.43_88, 0.17_04, 1.40_29, 1.17_01, -0.30_27, -2.20_53, -2.62_87, 1.33_50, 0.81_31, 0.92_74, -1.62_92, -0.60_98, 2.51_31, 1.65_05, -0.89_58, -0.92_98, -0.01_51, 2.42_57, 2.33_55 ]) lowerCAmelCase__ : List[str] = torch.tensor([ -2.05_85, -2.78_97, -0.28_50, -0.89_40, 1.90_52, 0.57_02, 0.63_45, -3.89_59, 1.59_32, -3.23_19, 0.19_74, 0.02_87, 1.75_66, 2.65_43, 0.83_87, -0.53_51, -3.27_36, -4.33_75, 2.90_29, 1.63_90, 1.46_40, -2.17_01, -1.90_13, 2.93_41, 3.49_81, -0.62_55, -1.16_44, -0.15_91, 3.70_97, 3.20_66 ]) lowerCAmelCase__ : Dict = torch.tensor([ -2.31_39, -2.55_94, -0.01_97, -0.67_85, 1.70_01, 1.16_06, 0.30_75, -2.17_40, 1.80_71, -2.56_30, -0.09_26, -0.38_11, 1.21_16, 2.62_46, 1.27_31, -0.53_98, -2.81_53, -3.61_40, 2.38_93, 1.32_62, 1.62_58, -2.18_56, -1.32_67, 2.83_95, 2.37_79, -1.06_23, -1.24_68, 0.89_59, 3.33_67, 3.22_43 ]) lowerCAmelCase__ : Dict = torch.tensor([ -2.06_28, -2.76_67, -0.20_89, -0.82_63, 2.05_39, 0.59_92, 0.64_95, -3.83_36, 1.60_25, -3.28_17, 0.17_21, -0.06_33, 1.75_16, 2.70_39, 0.81_00, -0.59_08, -3.21_13, -4.43_43, 2.92_57, 1.36_32, 1.55_62, -2.14_89, -1.98_94, 3.05_60, 3.33_96, -0.73_28, -1.04_17, 0.03_83, 3.70_93, 3.23_43 ]) lowerCAmelCase__ : Any = torch.tensor([ -1.45_74, -2.05_69, -0.04_73, -0.61_17, 1.40_18, 0.57_69, 0.41_29, -2.73_44, 1.22_41, -2.13_97, 0.20_00, 0.39_37, 0.76_16, 2.04_53, 0.73_24, -0.33_91, -2.17_46, -2.77_44, 1.69_63, 0.69_21, 1.21_87, -1.61_72, -0.88_77, 2.24_39, 1.84_71, -0.58_39, -0.56_05, -0.04_64, 2.32_50, 2.12_19 ]) # fmt: on lowerCAmelCase__ : Any = api.list_models(filter='''diffusers''') for mod in models: if "google" in mod.author or mod.modelId == "CompVis/ldm-celebahq-256": lowerCAmelCase__ : List[str] = '''/home/patrick/google_checkpoints/''' + mod.modelId.split('''/''')[-1] print(F'''Started running {mod.modelId}!!!''') if mod.modelId.startswith('''CompVis'''): lowerCAmelCase__ : int = UNetaDModel.from_pretrained(local_checkpoint, subfolder='''unet''') else: lowerCAmelCase__ : str = UNetaDModel.from_pretrained(local_checkpoint) torch.manual_seed(0) random.seed(0) lowerCAmelCase__ : Any = torch.randn(1, model.config.in_channels, model.config.sample_size, model.config.sample_size) lowerCAmelCase__ : List[str] = torch.tensor([10] * noise.shape[0]) with torch.no_grad(): lowerCAmelCase__ : int = model(noise, time_step).sample assert torch.allclose( logits[0, 0, 0, :30], results['''_'''.join('''_'''.join(mod.modelId.split('''/''')).split('''-'''))], atol=1E-3 ) print(F'''{mod.modelId} has passed successfully!!!''')
699
0
import warnings from ...utils import logging from .image_processing_segformer import SegformerImageProcessor lowerCAmelCase__ : int = logging.get_logger(__name__) class __snake_case ( _lowerCamelCase ): def __init__( self , *__UpperCamelCase , **__UpperCamelCase ) -> None: '''simple docstring''' warnings.warn( 'The class SegformerFeatureExtractor is deprecated and will be removed in version 5 of Transformers.' ' Please use SegformerImageProcessor instead.' , __UpperCamelCase , ) super().__init__(*__UpperCamelCase , **__UpperCamelCase )
718
import warnings from ...utils import logging from .image_processing_perceiver import PerceiverImageProcessor lowerCAmelCase__ : Dict = logging.get_logger(__name__) class __snake_case ( _lowerCamelCase ): def __init__( self , *__UpperCamelCase , **__UpperCamelCase ) -> None: '''simple docstring''' warnings.warn( 'The class PerceiverFeatureExtractor is deprecated and will be removed in version 5 of Transformers.' ' Please use PerceiverImageProcessor instead.' , __UpperCamelCase , ) super().__init__(*__UpperCamelCase , **__UpperCamelCase )
699
0
import json import os from typing import Dict, List, Optional, Tuple from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging lowerCAmelCase__ : int = logging.get_logger(__name__) lowerCAmelCase__ : Optional[int] = { '''vocab_file''': '''vocab.json''', '''tokenizer_config_file''': '''tokenizer_config.json''', '''merges_file''': '''merges.txt''', } lowerCAmelCase__ : Dict = { '''vocab_file''': { '''facebook/s2t-wav2vec2-large-en-de''': ( '''https://huggingface.co/facebook/s2t-wav2vec2-large-en-de/resolve/main/vocab.json''' ), }, '''tokenizer_config_file''': { '''facebook/s2t-wav2vec2-large-en-de''': ( '''https://huggingface.co/facebook/s2t-wav2vec2-large-en-de/resolve/main/tokenizer_config.json''' ), }, '''merges_file''': { '''facebook/s2t-wav2vec2-large-en-de''': ( '''https://huggingface.co/facebook/s2t-wav2vec2-large-en-de/resolve/main/merges.txt''' ), }, } lowerCAmelCase__ : Tuple = '''</w>''' lowerCAmelCase__ : int = '''@@ ''' def UpperCamelCase__ ( A__ ) -> str: snake_case__ : List[str] = set() snake_case__ : Optional[int] = word[0] for char in word[1:]: pairs.add((prev_char, char) ) snake_case__ : Optional[int] = char return pairs # Speech2Text2 has no max input length lowerCAmelCase__ : Tuple = {'''facebook/s2t-wav2vec2-large-en-de''': 10_24} class __snake_case ( _lowerCamelCase ): __lowerCamelCase = VOCAB_FILES_NAMES __lowerCamelCase = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase = ["""input_ids""", """attention_mask"""] def __init__( self , __UpperCamelCase , __UpperCamelCase="<s>" , __UpperCamelCase="<pad>" , __UpperCamelCase="</s>" , __UpperCamelCase="<unk>" , __UpperCamelCase=False , __UpperCamelCase=None , **__UpperCamelCase , ) -> List[str]: '''simple docstring''' super().__init__( unk_token=__UpperCamelCase , bos_token=__UpperCamelCase , eos_token=__UpperCamelCase , pad_token=__UpperCamelCase , do_lower_case=__UpperCamelCase , **__UpperCamelCase , ) snake_case__ : int = do_lower_case with open(__UpperCamelCase , encoding='utf-8' ) as vocab_handle: snake_case__ : Union[str, Any] = json.load(__UpperCamelCase ) snake_case__ : int = {v: k for k, v in self.encoder.items()} if merges_file is None: logger.info(F"""No merges files provided. {self.__class__.__name__} can only be used for decoding.""" ) snake_case__ : Optional[int] = None snake_case__ : Optional[Any] = None else: with open(__UpperCamelCase , encoding='utf-8' ) as merges_handle: snake_case__ : List[Any] = merges_handle.read().split('\n' )[:-1] snake_case__ : Tuple = [tuple(merge.split()[:2] ) for merge in merges] snake_case__ : int = dict(zip(__UpperCamelCase , range(len(__UpperCamelCase ) ) ) ) snake_case__ : Tuple = {} @property def __a ( self ) -> int: '''simple docstring''' return len(self.decoder ) def __a ( self ) -> Dict: '''simple docstring''' return dict(self.encoder , **self.added_tokens_encoder ) def __a ( self , __UpperCamelCase ) -> Union[str, Any]: '''simple docstring''' snake_case__ : Dict = tuple(token[:-1] ) + (token[-1] + BPE_TOKEN_MERGES,) if token in self.cache: return self.cache[token] snake_case__ : Dict = get_pairs(__UpperCamelCase ) if not pairs: return token while True: snake_case__ : List[str] = min(__UpperCamelCase , key=lambda __UpperCamelCase : self.bpe_ranks.get(__UpperCamelCase , float('inf' ) ) ) if bigram not in self.bpe_ranks: break snake_case__ : Optional[int] = bigram snake_case__ : str = [] snake_case__ : str = 0 while i < len(__UpperCamelCase ): try: snake_case__ : Union[str, Any] = word.index(__UpperCamelCase , __UpperCamelCase ) except ValueError: new_word.extend(word[i:] ) break else: new_word.extend(word[i:j] ) snake_case__ : List[str] = j if word[i] == first and i < len(__UpperCamelCase ) - 1 and word[i + 1] == second: new_word.append(first + second ) i += 2 else: new_word.append(word[i] ) i += 1 snake_case__ : List[Any] = tuple(__UpperCamelCase ) snake_case__ : Union[str, Any] = new_word if len(__UpperCamelCase ) == 1: break else: snake_case__ : Optional[Any] = get_pairs(__UpperCamelCase ) snake_case__ : Dict = ' '.join(__UpperCamelCase ) if word == "\n " + BPE_TOKEN_MERGES: snake_case__ : str = '\n' + BPE_TOKEN_MERGES if word.endswith(__UpperCamelCase ): snake_case__ : Tuple = word.replace(__UpperCamelCase , '' ) snake_case__ : int = word.replace(' ' , __UpperCamelCase ) snake_case__ : int = word return word def __a ( self , __UpperCamelCase ) -> int: '''simple docstring''' if self.bpe_ranks is None: raise ValueError( 'This tokenizer was instantiated without a `merges.txt` file, so' ' that it can only be used for decoding, not for encoding.' 'Make sure to provide `merges.txt` file at instantiation to enable ' 'encoding.' ) if self.do_lower_case: snake_case__ : Any = text.lower() snake_case__ : Union[str, Any] = text.split() snake_case__ : Union[str, Any] = [] for token in text: if token: split_tokens.extend(list(self.bpe(__UpperCamelCase ).split(' ' ) ) ) return split_tokens def __a ( self , __UpperCamelCase ) -> int: '''simple docstring''' return self.encoder.get(__UpperCamelCase , self.encoder.get(self.unk_token ) ) def __a ( self , __UpperCamelCase ) -> str: '''simple docstring''' snake_case__ : Union[str, Any] = self.decoder.get(__UpperCamelCase , self.unk_token ) return result def __a ( self , __UpperCamelCase ) -> str: '''simple docstring''' snake_case__ : str = ' '.join(__UpperCamelCase ) # make sure @@ tokens are concatenated snake_case__ : Dict = ''.join(string.split(__UpperCamelCase ) ) return string def __a ( self , __UpperCamelCase , __UpperCamelCase = 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__ : Any = os.path.join( __UpperCamelCase , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) snake_case__ : Any = os.path.join( __UpperCamelCase , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['merges_file'] ) with open(__UpperCamelCase , 'w' , encoding='utf-8' ) as f: f.write(json.dumps(self.encoder , indent=2 , sort_keys=__UpperCamelCase , ensure_ascii=__UpperCamelCase ) + '\n' ) snake_case__ : Dict = 0 if self.bpe_ranks is None: return (vocab_file,) with open(__UpperCamelCase , 'w' , encoding='utf-8' ) as writer: for bpe_tokens, token_index in sorted(self.bpe_ranks.items() , key=lambda __UpperCamelCase : kv[1] ): if index != token_index: logger.warning( F"""Saving vocabulary to {merges_file}: BPE merge indices are not consecutive.""" ' Please check that the tokenizer is not corrupted!' ) snake_case__ : Optional[Any] = token_index writer.write(' '.join(__UpperCamelCase ) + '\n' ) index += 1 return (vocab_file, merges_file)
719
import io import itertools import json from dataclasses import dataclass from typing import Optional import pyarrow as pa import pyarrow.json as paj import datasets from datasets.table import table_cast from datasets.utils.file_utils import readline lowerCAmelCase__ : List[Any] = datasets.utils.logging.get_logger(__name__) @dataclass class __snake_case ( datasets.BuilderConfig ): __lowerCamelCase = None __lowerCamelCase = "utf-8" __lowerCamelCase = None __lowerCamelCase = None __lowerCamelCase = True # deprecated __lowerCamelCase = None # deprecated __lowerCamelCase = 10 << 20 # 10MB __lowerCamelCase = None class __snake_case ( datasets.ArrowBasedBuilder ): __lowerCamelCase = JsonConfig def __a ( self ) -> Optional[Any]: '''simple docstring''' if self.config.block_size is not None: logger.warning('The JSON loader parameter `block_size` is deprecated. Please use `chunksize` instead' ) snake_case__ : str = self.config.block_size if self.config.use_threads is not True: logger.warning( 'The JSON loader parameter `use_threads` is deprecated and doesn\'t have any effect anymore.' ) if self.config.newlines_in_values is not None: raise ValueError('The JSON loader parameter `newlines_in_values` is no longer supported' ) return datasets.DatasetInfo(features=self.config.features ) def __a ( self , __UpperCamelCase ) -> Dict: '''simple docstring''' 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}""" ) snake_case__ : int = dl_manager.download_and_extract(self.config.data_files ) if isinstance(__UpperCamelCase , (str, list, tuple) ): snake_case__ : Any = data_files if isinstance(__UpperCamelCase , __UpperCamelCase ): snake_case__ : Optional[Any] = [files] snake_case__ : List[str] = [dl_manager.iter_files(__UpperCamelCase ) for file in files] return [datasets.SplitGenerator(name=datasets.Split.TRAIN , gen_kwargs={'files': files} )] snake_case__ : List[Any] = [] for split_name, files in data_files.items(): if isinstance(__UpperCamelCase , __UpperCamelCase ): snake_case__ : List[Any] = [files] snake_case__ : Any = [dl_manager.iter_files(__UpperCamelCase ) for file in files] splits.append(datasets.SplitGenerator(name=__UpperCamelCase , gen_kwargs={'files': files} ) ) return splits def __a ( self , __UpperCamelCase ) -> pa.Table: '''simple docstring''' if self.config.features is not None: # adding missing columns for column_name in set(self.config.features ) - set(pa_table.column_names ): snake_case__ : List[Any] = self.config.features.arrow_schema.field(__UpperCamelCase ).type snake_case__ : List[str] = pa_table.append_column(__UpperCamelCase , pa.array([None] * len(__UpperCamelCase ) , type=__UpperCamelCase ) ) # more expensive cast to support nested structures with keys in a different order # allows str <-> int/float or str to Audio for example snake_case__ : List[str] = table_cast(__UpperCamelCase , self.config.features.arrow_schema ) return pa_table def __a ( self , __UpperCamelCase ) -> int: '''simple docstring''' for file_idx, file in enumerate(itertools.chain.from_iterable(__UpperCamelCase ) ): # If the file is one json object and if we need to look at the list of items in one specific field if self.config.field is not None: with open(__UpperCamelCase , encoding=self.config.encoding , errors=self.config.encoding_errors ) as f: snake_case__ : Union[str, Any] = json.load(__UpperCamelCase ) # We keep only the field we are interested in snake_case__ : Tuple = dataset[self.config.field] # We accept two format: a list of dicts or a dict of lists if isinstance(__UpperCamelCase , (list, tuple) ): snake_case__ : List[Any] = set().union(*[row.keys() for row in dataset] ) snake_case__ : List[Any] = {col: [row.get(__UpperCamelCase ) for row in dataset] for col in keys} else: snake_case__ : List[Any] = dataset snake_case__ : Dict = pa.Table.from_pydict(__UpperCamelCase ) yield file_idx, self._cast_table(__UpperCamelCase ) # If the file has one json object per line else: with open(__UpperCamelCase , 'rb' ) as f: snake_case__ : Optional[int] = 0 # Use block_size equal to the chunk size divided by 32 to leverage multithreading # Set a default minimum value of 16kB if the chunk size is really small snake_case__ : Tuple = max(self.config.chunksize // 32 , 16 << 10 ) snake_case__ : Optional[Any] = ( self.config.encoding_errors if self.config.encoding_errors is not None else 'strict' ) while True: snake_case__ : Optional[int] = f.read(self.config.chunksize ) if not batch: break # Finish current line try: batch += f.readline() except (AttributeError, io.UnsupportedOperation): batch += readline(__UpperCamelCase ) # PyArrow only accepts utf-8 encoded bytes if self.config.encoding != "utf-8": snake_case__ : int = batch.decode(self.config.encoding , errors=__UpperCamelCase ).encode('utf-8' ) try: while True: try: snake_case__ : List[str] = paj.read_json( io.BytesIO(__UpperCamelCase ) , read_options=paj.ReadOptions(block_size=__UpperCamelCase ) ) break except (pa.ArrowInvalid, pa.ArrowNotImplementedError) as e: if ( isinstance(__UpperCamelCase , pa.ArrowInvalid ) and "straddling" not in str(__UpperCamelCase ) or block_size > len(__UpperCamelCase ) ): raise else: # Increase the block size in case it was too small. # The block size will be reset for the next file. logger.debug( F"""Batch of {len(__UpperCamelCase )} bytes couldn't be parsed with block_size={block_size}. Retrying with block_size={block_size * 2}.""" ) block_size *= 2 except pa.ArrowInvalid as e: try: with open( __UpperCamelCase , encoding=self.config.encoding , errors=self.config.encoding_errors ) as f: snake_case__ : Tuple = json.load(__UpperCamelCase ) except json.JSONDecodeError: logger.error(F"""Failed to read file '{file}' with error {type(__UpperCamelCase )}: {e}""" ) raise e # If possible, parse the file as a list of json objects and exit the loop if isinstance(__UpperCamelCase , __UpperCamelCase ): # list is the only sequence type supported in JSON try: snake_case__ : str = set().union(*[row.keys() for row in dataset] ) snake_case__ : Union[str, Any] = {col: [row.get(__UpperCamelCase ) for row in dataset] for col in keys} snake_case__ : List[str] = pa.Table.from_pydict(__UpperCamelCase ) except (pa.ArrowInvalid, AttributeError) as e: logger.error(F"""Failed to read file '{file}' with error {type(__UpperCamelCase )}: {e}""" ) raise ValueError(F"""Not able to read records in the JSON file at {file}.""" ) from None yield file_idx, self._cast_table(__UpperCamelCase ) break else: logger.error(F"""Failed to read file '{file}' with error {type(__UpperCamelCase )}: {e}""" ) raise ValueError( F"""Not able to read records in the JSON file at {file}. """ F"""You should probably indicate the field of the JSON file containing your records. """ F"""This JSON file contain the following fields: {str(list(dataset.keys() ) )}. """ F"""Select the correct one and provide it as `field='XXX'` to the dataset loading method. """ ) from None # Uncomment for debugging (will print the Arrow table size and elements) # logger.warning(f"pa_table: {pa_table} num rows: {pa_table.num_rows}") # logger.warning('\n'.join(str(pa_table.slice(i, 1).to_pydict()) for i in range(pa_table.num_rows))) yield (file_idx, batch_idx), self._cast_table(__UpperCamelCase ) batch_idx += 1
699
0
from typing import List, Optional, Union from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy from ...utils import TensorType class __snake_case ( _lowerCamelCase ): __lowerCamelCase = ["""image_processor""", """tokenizer"""] __lowerCamelCase = """BridgeTowerImageProcessor""" __lowerCamelCase = ("""RobertaTokenizer""", """RobertaTokenizerFast""") def __init__( self , __UpperCamelCase , __UpperCamelCase ) -> Dict: '''simple docstring''' super().__init__(__UpperCamelCase , __UpperCamelCase ) def __call__( self , __UpperCamelCase , __UpperCamelCase = None , __UpperCamelCase = True , __UpperCamelCase = False , __UpperCamelCase = None , __UpperCamelCase = None , __UpperCamelCase = 0 , __UpperCamelCase = None , __UpperCamelCase = None , __UpperCamelCase = None , __UpperCamelCase = False , __UpperCamelCase = False , __UpperCamelCase = False , __UpperCamelCase = False , __UpperCamelCase = True , __UpperCamelCase = None , **__UpperCamelCase , ) -> BatchEncoding: '''simple docstring''' snake_case__ : str = self.tokenizer( text=__UpperCamelCase , add_special_tokens=__UpperCamelCase , padding=__UpperCamelCase , truncation=__UpperCamelCase , max_length=__UpperCamelCase , stride=__UpperCamelCase , pad_to_multiple_of=__UpperCamelCase , return_token_type_ids=__UpperCamelCase , return_attention_mask=__UpperCamelCase , return_overflowing_tokens=__UpperCamelCase , return_special_tokens_mask=__UpperCamelCase , return_offsets_mapping=__UpperCamelCase , return_length=__UpperCamelCase , verbose=__UpperCamelCase , return_tensors=__UpperCamelCase , **__UpperCamelCase , ) # add pixel_values + pixel_mask snake_case__ : Any = self.image_processor( __UpperCamelCase , return_tensors=__UpperCamelCase , do_normalize=__UpperCamelCase , do_center_crop=__UpperCamelCase , **__UpperCamelCase ) encoding.update(__UpperCamelCase ) return encoding def __a ( self , *__UpperCamelCase , **__UpperCamelCase ) -> Tuple: '''simple docstring''' return self.tokenizer.batch_decode(*__UpperCamelCase , **__UpperCamelCase ) def __a ( self , *__UpperCamelCase , **__UpperCamelCase ) -> Tuple: '''simple docstring''' return self.tokenizer.decode(*__UpperCamelCase , **__UpperCamelCase ) @property def __a ( self ) -> Any: '''simple docstring''' snake_case__ : Optional[int] = self.tokenizer.model_input_names snake_case__ : List[Any] = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) )
720
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__ : Any = {'''configuration_xglm''': ['''XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''XGLMConfig''']} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : str = ['''XGLMTokenizer'''] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : Dict = ['''XGLMTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : Optional[int] = [ '''XGLM_PRETRAINED_MODEL_ARCHIVE_LIST''', '''XGLMForCausalLM''', '''XGLMModel''', '''XGLMPreTrainedModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : Dict = [ '''FlaxXGLMForCausalLM''', '''FlaxXGLMModel''', '''FlaxXGLMPreTrainedModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : Dict = [ '''TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFXGLMForCausalLM''', '''TFXGLMModel''', '''TFXGLMPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_xglm import XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XGLMConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xglm import XGLMTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xglm_fast import XGLMTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xglm import XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, XGLMForCausalLM, XGLMModel, XGLMPreTrainedModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_xglm import FlaxXGLMForCausalLM, FlaxXGLMModel, FlaxXGLMPreTrainedModel try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_xglm import ( TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFXGLMForCausalLM, TFXGLMModel, TFXGLMPreTrainedModel, ) else: import sys lowerCAmelCase__ : Tuple = _LazyModule(__name__, globals()['''__file__'''], _import_structure)
699
0
from collections.abc import Generator from math import sin def UpperCamelCase__ ( A__ ) -> bytes: if len(A__ ) != 32: raise ValueError('Input must be of length 32' ) snake_case__ : int = b'' for i in [3, 2, 1, 0]: little_endian += string_aa[8 * i : 8 * i + 8] return little_endian def UpperCamelCase__ ( A__ ) -> bytes: if i < 0: raise ValueError('Input must be non-negative' ) snake_case__ : Union[str, Any] = format(A__ , '08x' )[-8:] snake_case__ : Union[str, Any] = b'' for i in [3, 2, 1, 0]: little_endian_hex += hex_rep[2 * i : 2 * i + 2].encode('utf-8' ) return little_endian_hex def UpperCamelCase__ ( A__ ) -> bytes: snake_case__ : Optional[int] = b'' for char in message: bit_string += format(A__ , '08b' ).encode('utf-8' ) snake_case__ : Optional[int] = format(len(A__ ) , '064b' ).encode('utf-8' ) # Pad bit_string to a multiple of 512 chars bit_string += b"1" while len(A__ ) % 512 != 448: bit_string += b"0" bit_string += to_little_endian(start_len[32:] ) + to_little_endian(start_len[:32] ) return bit_string def UpperCamelCase__ ( A__ ) -> Generator[list[int], None, None]: if len(A__ ) % 512 != 0: raise ValueError('Input must have length that\'s a multiple of 512' ) for pos in range(0 , len(A__ ) , 512 ): snake_case__ : int = bit_string[pos : pos + 512] snake_case__ : List[str] = [] for i in range(0 , 512 , 32 ): block_words.append(int(to_little_endian(block[i : i + 32] ) , 2 ) ) yield block_words def UpperCamelCase__ ( A__ ) -> int: if i < 0: raise ValueError('Input must be non-negative' ) snake_case__ : Any = format(A__ , '032b' ) snake_case__ : Tuple = '' for c in i_str: new_str += "1" if c == "0" else "0" return int(A__ , 2 ) def UpperCamelCase__ ( A__ , A__ ) -> int: return (a + b) % 2**32 def UpperCamelCase__ ( A__ , A__ ) -> int: if i < 0: raise ValueError('Input must be non-negative' ) if shift < 0: raise ValueError('Shift must be non-negative' ) return ((i << shift) ^ (i >> (32 - shift))) % 2**32 def UpperCamelCase__ ( A__ ) -> bytes: snake_case__ : Any = preprocess(A__ ) snake_case__ : Union[str, Any] = [int(2**32 * abs(sin(i + 1 ) ) ) for i in range(64 )] # Starting states snake_case__ : Dict = 0x67_45_23_01 snake_case__ : List[str] = 0xEF_CD_AB_89 snake_case__ : List[str] = 0x98_BA_DC_FE snake_case__ : int = 0x10_32_54_76 snake_case__ : Union[str, Any] = [ 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, ] # Process bit string in chunks, each with 16 32-char words for block_words in get_block_words(A__ ): snake_case__ : List[str] = aa snake_case__ : Optional[int] = ba snake_case__ : Union[str, Any] = ca snake_case__ : List[Any] = da # Hash current chunk for i in range(64 ): if i <= 15: # f = (b & c) | (not_32(b) & d) # Alternate definition for f snake_case__ : Optional[Any] = d ^ (b & (c ^ d)) snake_case__ : int = i elif i <= 31: # f = (d & b) | (not_32(d) & c) # Alternate definition for f snake_case__ : str = c ^ (d & (b ^ c)) snake_case__ : Optional[Any] = (5 * i + 1) % 16 elif i <= 47: snake_case__ : List[str] = b ^ c ^ d snake_case__ : Optional[int] = (3 * i + 5) % 16 else: snake_case__ : Any = c ^ (b | not_aa(A__ )) snake_case__ : Optional[int] = (7 * i) % 16 snake_case__ : Union[str, Any] = (f + a + added_consts[i] + block_words[g]) % 2**32 snake_case__ : List[Any] = d snake_case__ : Optional[Any] = c snake_case__ : Optional[int] = b snake_case__ : int = sum_aa(A__ , left_rotate_aa(A__ , shift_amounts[i] ) ) # Add hashed chunk to running total snake_case__ : List[Any] = sum_aa(A__ , A__ ) snake_case__ : int = sum_aa(A__ , A__ ) snake_case__ : List[Any] = sum_aa(A__ , A__ ) snake_case__ : Optional[Any] = sum_aa(A__ , A__ ) snake_case__ : Dict = reformat_hex(A__ ) + reformat_hex(A__ ) + reformat_hex(A__ ) + reformat_hex(A__ ) return digest if __name__ == "__main__": import doctest doctest.testmod()
721
from __future__ import annotations import random # Maximum size of the population. Bigger could be faster but is more memory expensive. lowerCAmelCase__ : Dict = 2_00 # Number of elements selected in every generation of evolution. The selection takes # place from best to worst of that generation and must be smaller than N_POPULATION. lowerCAmelCase__ : List[str] = 50 # Probability that an element of a generation can mutate, changing one of its genes. # This will guarantee that all genes will be used during evolution. lowerCAmelCase__ : List[str] = 0.4 # Just a seed to improve randomness required by the algorithm. random.seed(random.randint(0, 10_00)) def UpperCamelCase__ ( A__ , A__ ) -> tuple[str, float]: snake_case__ : Tuple = len([g for position, g in enumerate(A__ ) if g == main_target[position]] ) return (item, float(A__ )) def UpperCamelCase__ ( A__ , A__ ) -> tuple[str, str]: snake_case__ : str = random.randint(0 , len(A__ ) - 1 ) snake_case__ : int = parent_a[:random_slice] + parent_a[random_slice:] snake_case__ : Any = parent_a[:random_slice] + parent_a[random_slice:] return (child_a, child_a) def UpperCamelCase__ ( A__ , A__ ) -> str: snake_case__ : List[Any] = list(A__ ) if random.uniform(0 , 1 ) < MUTATION_PROBABILITY: snake_case__ : Optional[Any] = random.choice(A__ ) return "".join(A__ ) def UpperCamelCase__ ( A__ , A__ , A__ , ) -> list[str]: snake_case__ : Tuple = [] # Generate more children proportionally to the fitness score. snake_case__ : Optional[Any] = int(parent_a[1] * 100 ) + 1 snake_case__ : str = 10 if child_n >= 10 else child_n for _ in range(A__ ): snake_case__ : Any = population_score[random.randint(0 , A__ )][0] snake_case__ , snake_case__ : int = crossover(parent_a[0] , A__ ) # Append new string to the population list. pop.append(mutate(A__ , A__ ) ) pop.append(mutate(A__ , A__ ) ) return pop def UpperCamelCase__ ( A__ , A__ , A__ = True ) -> tuple[int, int, str]: # Verify if N_POPULATION is bigger than N_SELECTED if N_POPULATION < N_SELECTED: snake_case__ : Union[str, Any] = F"""{N_POPULATION} must be bigger than {N_SELECTED}""" raise ValueError(A__ ) # Verify that the target contains no genes besides the ones inside genes variable. snake_case__ : Tuple = sorted({c for c in target if c not in genes} ) if not_in_genes_list: snake_case__ : int = F"""{not_in_genes_list} is not in genes list, evolution cannot converge""" raise ValueError(A__ ) # Generate random starting population. snake_case__ : Union[str, Any] = [] for _ in range(A__ ): population.append(''.join([random.choice(A__ ) for i in range(len(A__ ) )] ) ) # Just some logs to know what the algorithms is doing. snake_case__ , snake_case__ : str = 0, 0 # This loop will end when we find a perfect match for our target. while True: generation += 1 total_population += len(A__ ) # Random population created. Now it's time to evaluate. # Adding a bit of concurrency can make everything faster, # # import concurrent.futures # population_score: list[tuple[str, float]] = [] # with concurrent.futures.ThreadPoolExecutor( # max_workers=NUM_WORKERS) as executor: # futures = {executor.submit(evaluate, item) for item in population} # concurrent.futures.wait(futures) # population_score = [item.result() for item in futures] # # but with a simple algorithm like this, it will probably be slower. # We just need to call evaluate for every item inside the population. snake_case__ : List[Any] = [evaluate(A__ , A__ ) for item in population] # Check if there is a matching evolution. snake_case__ : int = sorted(A__ , key=lambda A__ : x[1] , reverse=A__ ) if population_score[0][0] == target: return (generation, total_population, population_score[0][0]) # Print the best result every 10 generation. # Just to know that the algorithm is working. if debug and generation % 10 == 0: print( F"""\nGeneration: {generation}""" F"""\nTotal Population:{total_population}""" F"""\nBest score: {population_score[0][1]}""" F"""\nBest string: {population_score[0][0]}""" ) # Flush the old population, keeping some of the best evolutions. # Keeping this avoid regression of evolution. snake_case__ : Optional[int] = population[: int(N_POPULATION / 3 )] population.clear() population.extend(A__ ) # Normalize population score to be between 0 and 1. snake_case__ : str = [ (item, score / len(A__ )) for item, score in population_score ] # This is selection for i in range(A__ ): population.extend(select(population_score[int(A__ )] , A__ , A__ ) ) # Check if the population has already reached the maximum value and if so, # break the cycle. If this check is disabled, the algorithm will take # forever to compute large strings, but will also calculate small strings in # a far fewer generations. if len(A__ ) > N_POPULATION: break if __name__ == "__main__": lowerCAmelCase__ : str = ( '''This is a genetic algorithm to evaluate, combine, evolve, and mutate a string!''' ) lowerCAmelCase__ : Optional[Any] = list( ''' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklm''' '''nopqrstuvwxyz.,;!?+-*#@^\'èéòà€ù=)(&%$£/\\''' ) lowerCAmelCase__, lowerCAmelCase__, lowerCAmelCase__ : List[str] = basic(target_str, genes_list) print( F'''\nGeneration: {generation}\nTotal Population: {population}\nTarget: {target}''' )
699
0
import requests from bsa import BeautifulSoup def UpperCamelCase__ ( A__ , A__ ) -> str: snake_case__ : Dict = BeautifulSoup(requests.get(A__ , params=A__ ).content , 'html.parser' ) snake_case__ : Any = soup.find('div' , attrs={'class': 'gs_ri'} ) snake_case__ : Optional[Any] = div.find('div' , attrs={'class': 'gs_fl'} ).find_all('a' ) return anchors[2].get_text() if __name__ == "__main__": lowerCAmelCase__ : int = { '''title''': ( '''Precisely geometry controlled microsupercapacitors for ultrahigh areal ''' '''capacitance, volumetric capacitance, and energy density''' ), '''journal''': '''Chem. Mater.''', '''volume''': 30, '''pages''': '''3979-3990''', '''year''': 20_18, '''hl''': '''en''', } print(get_citation('''https://scholar.google.com/scholar_lookup''', params=params))
700
from __future__ import annotations from collections.abc import Iterator from typing import Generic, TypeVar lowerCAmelCase__ : Optional[int] = TypeVar('''T''') class __snake_case ( Generic[T] ): def __init__( self , __UpperCamelCase ) -> Any: '''simple docstring''' snake_case__ : Optional[int] = data snake_case__ : Node[T] | None = None def __str__( self ) -> str: '''simple docstring''' return F"""{self.data}""" class __snake_case ( Generic[T] ): def __init__( self ) -> None: '''simple docstring''' snake_case__ : Node[T] | None = None def __iter__( self ) -> Iterator[T]: '''simple docstring''' snake_case__ : str = self.top while node: yield node.data snake_case__ : Dict = node.next def __str__( self ) -> str: '''simple docstring''' return "->".join([str(__UpperCamelCase ) for item in self] ) def __len__( self ) -> int: '''simple docstring''' return len(tuple(iter(self ) ) ) def __a ( self ) -> bool: '''simple docstring''' return self.top is None def __a ( self , __UpperCamelCase ) -> None: '''simple docstring''' snake_case__ : str = Node(__UpperCamelCase ) if not self.is_empty(): snake_case__ : List[str] = self.top snake_case__ : Tuple = node def __a ( self ) -> T: '''simple docstring''' if self.is_empty(): raise IndexError('pop from empty stack' ) assert isinstance(self.top , __UpperCamelCase ) snake_case__ : List[str] = self.top snake_case__ : Union[str, Any] = self.top.next return pop_node.data def __a ( self ) -> T: '''simple docstring''' if self.is_empty(): raise IndexError('peek from empty stack' ) assert self.top is not None return self.top.data def __a ( self ) -> None: '''simple docstring''' snake_case__ : Any = None if __name__ == "__main__": from doctest import testmod testmod()
699
0