code
stringlengths
86
54.5k
code_codestyle
int64
0
371
style_context
stringlengths
87
49.2k
style_context_codestyle
int64
0
349
label
int64
0
1
'''simple docstring''' import numpy as np import torch from torch.nn import CrossEntropyLoss from transformers import AutoModelForCausalLM, AutoTokenizer import datasets from datasets import logging __A : str = '\\n\n' __A : str = '\nPerplexity (PPL) is one of the most common metrics for evaluating language models.\nIt is defined as the exponentiated average negative log-likelihood of a sequence.\n\nFor more information, see https://huggingface.co/docs/transformers/perplexity\n' __A : List[str] = '\nArgs:\n model_id (str): model used for calculating Perplexity\n NOTE: Perplexity can only be calculated for causal language models.\n This includes models such as gpt2, causal variations of bert,\n causal versions of t5, and more (the full list can be found\n in the AutoModelForCausalLM documentation here:\n https://huggingface.co/docs/transformers/master/en/model_doc/auto#transformers.AutoModelForCausalLM )\n\n input_texts (list of str): input text, each separate text snippet\n is one list entry.\n batch_size (int): the batch size to run texts through the model. Defaults to 16.\n add_start_token (bool): whether to add the start token to the texts,\n so the perplexity can include the probability of the first word. Defaults to True.\n device (str): device to run on, defaults to \'cuda\' when available\nReturns:\n perplexity: dictionary containing the perplexity scores for the texts\n in the input list, as well as the mean perplexity. If one of the input texts is\n longer than the max input length of the model, then it is truncated to the\n max length for the perplexity computation.\nExamples:\n Example 1:\n >>> perplexity = datasets.load_metric("perplexity")\n >>> input_texts = ["lorem ipsum", "Happy Birthday!", "Bienvenue"]\n >>> results = perplexity.compute(model_id=\'gpt2\',\n ... add_start_token=False,\n ... input_texts=input_texts) # doctest:+ELLIPSIS\n >>> print(list(results.keys()))\n [\'perplexities\', \'mean_perplexity\']\n >>> print(round(results["mean_perplexity"], 2))\n 78.22\n >>> print(round(results["perplexities"][0], 2))\n 11.11\n\n Example 2:\n >>> perplexity = datasets.load_metric("perplexity")\n >>> input_texts = datasets.load_dataset("wikitext",\n ... "wikitext-2-raw-v1",\n ... split="test")["text"][:50] # doctest:+ELLIPSIS\n [...]\n >>> input_texts = [s for s in input_texts if s!=\'\']\n >>> results = perplexity.compute(model_id=\'gpt2\',\n ... input_texts=input_texts) # doctest:+ELLIPSIS\n >>> print(list(results.keys()))\n [\'perplexities\', \'mean_perplexity\']\n >>> print(round(results["mean_perplexity"], 2))\n 60.35\n >>> print(round(results["perplexities"][0], 2))\n 81.12\n' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION ,_KWARGS_DESCRIPTION) class __snake_case ( datasets.Metric): """simple docstring""" def __lowercase ( self : Union[str, Any] ) -> int: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """input_texts""": datasets.Value("""string""" ), } ) , reference_urls=["""https://huggingface.co/docs/transformers/perplexity"""] , ) def __lowercase ( self : int , lowerCamelCase : Tuple , lowerCamelCase : int , lowerCamelCase : int = 16 , lowerCamelCase : bool = True , lowerCamelCase : int=None ) -> Optional[Any]: if device is not None: assert device in ["gpu", "cpu", "cuda"], "device should be either gpu or cpu." if device == "gpu": lowerCAmelCase_ : int = 'cuda' else: lowerCAmelCase_ : Optional[Any] = 'cuda' if torch.cuda.is_available() else 'cpu' lowerCAmelCase_ : Dict = AutoModelForCausalLM.from_pretrained(lowerCamelCase__ ) lowerCAmelCase_ : Dict = model.to(lowerCamelCase__ ) lowerCAmelCase_ : int = AutoTokenizer.from_pretrained(lowerCamelCase__ ) # if batch_size > 1 (which generally leads to padding being required), and # if there is not an already assigned pad_token, assign an existing # special token to also be the padding token if tokenizer.pad_token is None and batch_size > 1: lowerCAmelCase_ : List[Any] = list(tokenizer.special_tokens_map_extended.values() ) # check that the model already has at least one special token defined assert ( len(lowerCamelCase__ ) > 0 ), "If batch_size > 1, model must have at least one special token to use for padding. Please use a different model or set batch_size=1." # assign one of the special tokens to also be the pad token tokenizer.add_special_tokens({"""pad_token""": existing_special_tokens[0]} ) if add_start_token: # leave room for <BOS> token to be added: assert ( tokenizer.bos_token is not None ), "Input model must already have a BOS token if using add_start_token=True. Please use a different model, or set add_start_token=False" lowerCAmelCase_ : List[Any] = model.config.max_length - 1 else: lowerCAmelCase_ : Tuple = model.config.max_length lowerCAmelCase_ : Tuple = tokenizer( lowerCamelCase__ , add_special_tokens=lowerCamelCase__ , padding=lowerCamelCase__ , truncation=lowerCamelCase__ , max_length=lowerCamelCase__ , return_tensors="""pt""" , return_attention_mask=lowerCamelCase__ , ).to(lowerCamelCase__ ) lowerCAmelCase_ : str = encodings['input_ids'] lowerCAmelCase_ : int = encodings['attention_mask'] # check that each input is long enough: if add_start_token: assert torch.all(torch.ge(attn_masks.sum(1 ) , 1 ) ), "Each input text must be at least one token long." else: assert torch.all( torch.ge(attn_masks.sum(1 ) , 2 ) ), "When add_start_token=False, each input text must be at least two tokens long. Run with add_start_token=True if inputting strings of only one token, and remove all empty input strings." lowerCAmelCase_ : List[Any] = [] lowerCAmelCase_ : Dict = CrossEntropyLoss(reduction="""none""" ) for start_index in logging.tqdm(range(0 , len(lowerCamelCase__ ) , lowerCamelCase__ ) ): lowerCAmelCase_ : str = min(start_index + batch_size , len(lowerCamelCase__ ) ) lowerCAmelCase_ : Tuple = encoded_texts[start_index:end_index] lowerCAmelCase_ : List[str] = attn_masks[start_index:end_index] if add_start_token: lowerCAmelCase_ : Dict = torch.tensor([[tokenizer.bos_token_id]] * encoded_batch.size(dim=0 ) ).to(lowerCamelCase__ ) lowerCAmelCase_ : List[Any] = torch.cat([bos_tokens_tensor, encoded_batch] , dim=1 ) lowerCAmelCase_ : Tuple = torch.cat( [torch.ones(bos_tokens_tensor.size() , dtype=torch.intaa ).to(lowerCamelCase__ ), attn_mask] , dim=1 ) lowerCAmelCase_ : Tuple = encoded_batch with torch.no_grad(): lowerCAmelCase_ : Any = model(lowerCamelCase__ , attention_mask=lowerCamelCase__ ).logits lowerCAmelCase_ : Dict = out_logits[..., :-1, :].contiguous() lowerCAmelCase_ : List[str] = labels[..., 1:].contiguous() lowerCAmelCase_ : Optional[int] = attn_mask[..., 1:].contiguous() lowerCAmelCase_ : Union[str, Any] = torch.expa( (loss_fct(shift_logits.transpose(1 , 2 ) , lowerCamelCase__ ) * shift_attention_mask_batch).sum(1 ) / shift_attention_mask_batch.sum(1 ) ) ppls += perplexity_batch.tolist() return {"perplexities": ppls, "mean_perplexity": np.mean(lowerCamelCase__ )}
120
'''simple docstring''' import argparse import logging import pickle import random import time import numpy as np from transformers import BertTokenizer, GPTaTokenizer, RobertaTokenizer logging.basicConfig( format='%(asctime)s - %(levelname)s - %(name)s - %(message)s', datefmt='%m/%d/%Y %H:%M:%S', level=logging.INFO ) snake_case_ : str = logging.getLogger(__name__) def A__ ( ): _UpperCamelCase : List[Any] = argparse.ArgumentParser( description='Preprocess the data to avoid re-doing it several times by (tokenization + token_to_ids).' ) parser.add_argument('--file_path' , type=UpperCAmelCase_ , default='data/dump.txt' , help='The path to the data.' ) parser.add_argument('--tokenizer_type' , type=UpperCAmelCase_ , default='bert' , choices=['bert', 'roberta', 'gpt2'] ) parser.add_argument('--tokenizer_name' , type=UpperCAmelCase_ , default='bert-base-uncased' , help='The tokenizer to use.' ) parser.add_argument('--dump_file' , type=UpperCAmelCase_ , default='data/dump' , help='The dump file prefix.' ) _UpperCamelCase : Any = parser.parse_args() logger.info(f'Loading Tokenizer ({args.tokenizer_name})' ) if args.tokenizer_type == "bert": _UpperCamelCase : Optional[int] = BertTokenizer.from_pretrained(args.tokenizer_name ) _UpperCamelCase : Optional[int] = tokenizer.special_tokens_map['cls_token'] # `[CLS]` _UpperCamelCase : Dict = tokenizer.special_tokens_map['sep_token'] # `[SEP]` elif args.tokenizer_type == "roberta": _UpperCamelCase : List[Any] = RobertaTokenizer.from_pretrained(args.tokenizer_name ) _UpperCamelCase : Any = tokenizer.special_tokens_map['cls_token'] # `<s>` _UpperCamelCase : int = tokenizer.special_tokens_map['sep_token'] # `</s>` elif args.tokenizer_type == "gpt2": _UpperCamelCase : Optional[int] = GPTaTokenizer.from_pretrained(args.tokenizer_name ) _UpperCamelCase : Optional[Any] = tokenizer.special_tokens_map['bos_token'] # `<|endoftext|>` _UpperCamelCase : Any = tokenizer.special_tokens_map['eos_token'] # `<|endoftext|>` logger.info(f'Loading text from {args.file_path}' ) with open(args.file_path , 'r' , encoding='utf8' ) as fp: _UpperCamelCase : List[Any] = fp.readlines() logger.info('Start encoding' ) logger.info(f'{len(UpperCAmelCase_ )} examples to process.' ) _UpperCamelCase : int = [] _UpperCamelCase : Any = 0 _UpperCamelCase : Any = 1_0_0_0_0 _UpperCamelCase : Optional[Any] = time.time() for text in data: _UpperCamelCase : List[Any] = f'{bos} {text.strip()} {sep}' _UpperCamelCase : Any = tokenizer.encode(UpperCAmelCase_ , add_special_tokens=UpperCAmelCase_ ) rslt.append(UpperCAmelCase_ ) iter += 1 if iter % interval == 0: _UpperCamelCase : Union[str, Any] = time.time() logger.info(f'{iter} examples processed. - {(end-start):.2f}s/{interval}expl' ) _UpperCamelCase : Tuple = time.time() logger.info('Finished binarization' ) logger.info(f'{len(UpperCAmelCase_ )} examples processed.' ) _UpperCamelCase : Optional[int] = f'{args.dump_file}.{args.tokenizer_name}.pickle' _UpperCamelCase : List[str] = tokenizer.vocab_size if vocab_size < (1 << 1_6): _UpperCamelCase : List[Any] = [np.uintaa(UpperCAmelCase_ ) for d in rslt] else: _UpperCamelCase : Any = [np.intaa(UpperCAmelCase_ ) for d in rslt] random.shuffle(rslt_ ) logger.info(f'Dump to {dp_file}' ) with open(UpperCAmelCase_ , 'wb' ) as handle: pickle.dump(rslt_ , UpperCAmelCase_ , protocol=pickle.HIGHEST_PROTOCOL ) if __name__ == "__main__": main()
83
0
'''simple docstring''' import logging import os from typing import Dict, List, Optional, Union import torch import torch.nn as nn from accelerate.utils.imports import ( is_abit_bnb_available, is_abit_bnb_available, is_bnb_available, ) from ..big_modeling import dispatch_model, init_empty_weights from .dataclasses import BnbQuantizationConfig from .modeling import ( find_tied_parameters, get_balanced_memory, infer_auto_device_map, load_checkpoint_in_model, offload_weight, set_module_tensor_to_device, ) if is_bnb_available(): import bitsandbytes as bnb from copy import deepcopy __lowerCamelCase = logging.getLogger(__name__) def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__ = None, UpperCAmelCase__ = None, UpperCAmelCase__ = None, UpperCAmelCase__ = None, UpperCAmelCase__ = None, UpperCAmelCase__ = False, ) -> List[str]: A_ = bnb_quantization_config.load_in_abit A_ = bnb_quantization_config.load_in_abit if load_in_abit and not is_abit_bnb_available(): raise ImportError( """You have a version of `bitsandbytes` that is not compatible with 8bit quantization,""" """ make sure you have the latest version of `bitsandbytes` installed.""" ) if load_in_abit and not is_abit_bnb_available(): raise ValueError( """You have a version of `bitsandbytes` that is not compatible with 4bit quantization,""" """make sure you have the latest version of `bitsandbytes` installed.""" ) A_ = [] # custom device map if isinstance(UpperCAmelCase__, UpperCAmelCase__ ) and len(device_map.keys() ) > 1: A_ = [key for key, value in device_map.items() if value in ["""disk""", """cpu"""]] # We keep some modules such as the lm_head in their original dtype for numerical stability reasons if bnb_quantization_config.skip_modules is None: A_ = get_keys_to_not_convert(UpperCAmelCase__ ) # add cpu modules to skip modules only for 4-bit modules if load_in_abit: bnb_quantization_config.skip_modules.extend(UpperCAmelCase__ ) A_ = bnb_quantization_config.skip_modules # We add the modules we want to keep in full precision if bnb_quantization_config.keep_in_fpaa_modules is None: A_ = [] A_ = bnb_quantization_config.keep_in_fpaa_modules modules_to_not_convert.extend(UpperCAmelCase__ ) # compatibility with peft A_ = load_in_abit A_ = load_in_abit A_ = get_parameter_device(UpperCAmelCase__ ) if model_device.type != "meta": # quantization of an already loaded model logger.warning( """It is not recommended to quantize a loaded model. """ """The model should be instantiated under the `init_empty_weights` context manager.""" ) A_ = replace_with_bnb_layers(UpperCAmelCase__, UpperCAmelCase__, modules_to_not_convert=UpperCAmelCase__ ) # convert param to the right dtype A_ = bnb_quantization_config.torch_dtype for name, param in model.state_dict().items(): if any(module_to_keep_in_fpaa in name for module_to_keep_in_fpaa in keep_in_fpaa_modules ): param.to(torch.floataa ) if param.dtype != torch.floataa: A_ = name.replace(""".weight""", """""" ).replace(""".bias""", """""" ) A_ = getattr(UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__ ) if param is not None: param.to(torch.floataa ) elif torch.is_floating_point(UpperCAmelCase__ ): param.to(UpperCAmelCase__ ) if model_device.type == "cuda": # move everything to cpu in the first place because we can't do quantization if the weights are already on cuda model.cuda(torch.cuda.current_device() ) torch.cuda.empty_cache() elif torch.cuda.is_available(): model.to(torch.cuda.current_device() ) else: raise RuntimeError("""No GPU found. A GPU is needed for quantization.""" ) logger.info( F'''The model device type is {model_device.type}. However, cuda is needed for quantization.''' """We move the model to cuda.""" ) return model elif weights_location is None: raise RuntimeError( F'''`weights_location` needs to be the folder path containing the weights of the model, but we found {weights_location} ''' ) else: with init_empty_weights(): A_ = replace_with_bnb_layers( UpperCAmelCase__, UpperCAmelCase__, modules_to_not_convert=UpperCAmelCase__ ) A_ = get_quantized_model_device_map( UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, max_memory=UpperCAmelCase__, no_split_module_classes=UpperCAmelCase__, ) if offload_state_dict is None and device_map is not None and "disk" in device_map.values(): A_ = True A_ = any(x in list(device_map.values() ) for x in ["""cpu""", """disk"""] ) load_checkpoint_in_model( UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, dtype=bnb_quantization_config.torch_dtype, offload_folder=UpperCAmelCase__, offload_state_dict=UpperCAmelCase__, keep_in_fpaa_modules=bnb_quantization_config.keep_in_fpaa_modules, offload_abit_bnb=load_in_abit and offload, ) return dispatch_model(UpperCAmelCase__, device_map=UpperCAmelCase__, offload_dir=UpperCAmelCase__ ) def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__=None, UpperCAmelCase__=None, UpperCAmelCase__=None ) -> List[Any]: if device_map is None: if torch.cuda.is_available(): A_ = {"""""": torch.cuda.current_device()} else: raise RuntimeError("""No GPU found. A GPU is needed for quantization.""" ) logger.info("""The device_map was not initialized.""" """Setting device_map to `{'':torch.cuda.current_device()}`.""" ) if isinstance(UpperCAmelCase__, UpperCAmelCase__ ): if device_map not in ["auto", "balanced", "balanced_low_0", "sequential"]: raise ValueError( """If passing a string for `device_map`, please choose 'auto', 'balanced', 'balanced_low_0' or """ """'sequential'.""" ) A_ = {} special_dtypes.update( { name: bnb_quantization_config.torch_dtype for name, _ in model.named_parameters() if any(m in name for m in bnb_quantization_config.skip_modules ) } ) special_dtypes.update( { name: torch.floataa for name, _ in model.named_parameters() if any(m in name for m in bnb_quantization_config.keep_in_fpaa_modules ) } ) A_ = {} A_ = special_dtypes A_ = no_split_module_classes A_ = bnb_quantization_config.target_dtype # get max_memory for each device. if device_map != "sequential": A_ = get_balanced_memory( UpperCAmelCase__, low_zero=(device_map == """balanced_low_0"""), max_memory=UpperCAmelCase__, **UpperCAmelCase__, ) A_ = max_memory A_ = infer_auto_device_map(UpperCAmelCase__, **UpperCAmelCase__ ) if isinstance(UpperCAmelCase__, UpperCAmelCase__ ): # check if don't have any quantized module on the cpu A_ = bnb_quantization_config.skip_modules + bnb_quantization_config.keep_in_fpaa_modules A_ = { key: device_map[key] for key in device_map.keys() if key not in modules_not_to_convert } for device in ["cpu", "disk"]: if device in device_map_without_some_modules.values(): if bnb_quantization_config.load_in_abit: raise ValueError( """ Some modules are dispatched on the CPU or the disk. Make sure you have enough GPU RAM to fit the quantized model. If you want to dispatch the model on the CPU or the disk while keeping these modules in `torch_dtype`, you need to pass a custom `device_map` to `load_and_quantize_model`. Check https://huggingface.co/docs/accelerate/main/en/usage_guides/quantization#offload-modules-to-cpu-and-disk for more details. """ ) else: logger.info( """Some modules are are offloaded to the CPU or the disk. Note that these modules will be converted to 8-bit""" ) del device_map_without_some_modules return device_map def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__=None, UpperCAmelCase__=None ) -> str: if modules_to_not_convert is None: A_ = [] A_ , A_ = _replace_with_bnb_layers( UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__ ) 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.""" """ this can happen for some architectures such as gpt2 that uses Conv1D instead of Linear layers.""" """ Please double check your model architecture, or submit an issue on github if you think this is""" """ a bug.""" ) return model def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__=None, UpperCAmelCase__=None, ) -> str: A_ = False for name, module in model.named_children(): if current_key_name is None: A_ = [] current_key_name.append(UpperCAmelCase__ ) if isinstance(UpperCAmelCase__, nn.Linear ) and name not in modules_to_not_convert: # Check if the current key is not in the `modules_to_not_convert` A_ = """.""".join(UpperCAmelCase__ ) A_ = True for key in modules_to_not_convert: if ( (key in current_key_name_str) and (key + "." in current_key_name_str) ) or key == current_key_name_str: A_ = False break if proceed: # Load bnb module with empty weight and replace ``nn.Linear` module if bnb_quantization_config.load_in_abit: A_ = bnb.nn.LinearabitLt( module.in_features, module.out_features, module.bias is not None, has_fpaa_weights=UpperCAmelCase__, threshold=bnb_quantization_config.llm_inta_threshold, ) elif bnb_quantization_config.load_in_abit: A_ = bnb.nn.Linearabit( module.in_features, module.out_features, module.bias is not None, bnb_quantization_config.bnb_abit_compute_dtype, compress_statistics=bnb_quantization_config.bnb_abit_use_double_quant, quant_type=bnb_quantization_config.bnb_abit_quant_type, ) else: raise ValueError("""load_in_8bit and load_in_4bit can't be both False""" ) A_ = module.weight.data if module.bias is not None: A_ = module.bias.data bnb_module.requires_grad_(UpperCAmelCase__ ) setattr(UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__ ) A_ = True if len(list(module.children() ) ) > 0: A_ , A_ = _replace_with_bnb_layers( UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__ ) A_ = has_been_replaced | _has_been_replaced # Remove the last key for recursion current_key_name.pop(-1 ) return model, has_been_replaced def UpperCAmelCase__ ( UpperCAmelCase__ ) -> Optional[int]: # Create a copy of the model with init_empty_weights(): A_ = deepcopy(UpperCAmelCase__ ) # this has 0 cost since it is done inside `init_empty_weights` context manager` A_ = find_tied_parameters(UpperCAmelCase__ ) # For compatibility with Accelerate < 0.18 if isinstance(UpperCAmelCase__, UpperCAmelCase__ ): A_ = sum(list(tied_params.values() ), [] ) + list(tied_params.keys() ) else: A_ = sum(UpperCAmelCase__, [] ) A_ = len(UpperCAmelCase__ ) > 0 # Check if it is a base model A_ = False if hasattr(UpperCAmelCase__, """base_model_prefix""" ): A_ = not hasattr(UpperCAmelCase__, 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 A_ = list(model.named_children() ) A_ = [list_modules[-1][0]] # add last module together with tied weights A_ = set(UpperCAmelCase__ ) - set(UpperCAmelCase__ ) A_ = list(set(UpperCAmelCase__ ) ) + list(UpperCAmelCase__ ) # remove ".weight" from the keys A_ = [""".weight""", """.bias"""] A_ = [] for name in list_untouched: for name_to_remove in names_to_remove: if name_to_remove in name: A_ = name.replace(UpperCAmelCase__, """""" ) filtered_module_names.append(UpperCAmelCase__ ) return filtered_module_names def UpperCAmelCase__ ( UpperCAmelCase__ ) -> Optional[Any]: for m in model.modules(): if isinstance(UpperCAmelCase__, bnb.nn.Linearabit ): return True return False def UpperCAmelCase__ ( UpperCAmelCase__ ) -> Optional[Any]: return next(parameter.parameters() ).device def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__ ) -> Union[str, Any]: # if it is not quantized, we quantize and offload the quantized weights and the SCB stats if fpaa_statistics is None: set_module_tensor_to_device(UpperCAmelCase__, UpperCAmelCase__, 0, dtype=UpperCAmelCase__, value=UpperCAmelCase__ ) A_ = param_name A_ = model if "." in tensor_name: A_ = tensor_name.split(""".""" ) for split in splits[:-1]: A_ = getattr(UpperCAmelCase__, UpperCAmelCase__ ) if new_module is None: raise ValueError(F'''{module} has no attribute {split}.''' ) A_ = new_module A_ = splits[-1] # offload weights A_ = False offload_weight(module._parameters[tensor_name], UpperCAmelCase__, UpperCAmelCase__, index=UpperCAmelCase__ ) if hasattr(module._parameters[tensor_name], """SCB""" ): offload_weight( module._parameters[tensor_name].SCB, param_name.replace("""weight""", """SCB""" ), UpperCAmelCase__, index=UpperCAmelCase__, ) else: offload_weight(UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, index=UpperCAmelCase__ ) offload_weight(UpperCAmelCase__, param_name.replace("""weight""", """SCB""" ), UpperCAmelCase__, index=UpperCAmelCase__ ) set_module_tensor_to_device(UpperCAmelCase__, UpperCAmelCase__, """meta""", dtype=UpperCAmelCase__, value=torch.empty(*param.size() ) )
101
'''simple docstring''' from __future__ import annotations from typing import Any def UpperCAmelCase__ ( UpperCAmelCase__ ) -> int: if not postfix_notation: return 0 A_ = {"""+""", """-""", """*""", """/"""} A_ = [] for token in postfix_notation: if token in operations: A_ , A_ = stack.pop(), stack.pop() if token == "+": stack.append(a + b ) elif token == "-": stack.append(a - b ) elif token == "*": stack.append(a * b ) else: if a * b < 0 and a % b != 0: stack.append(a // b + 1 ) else: stack.append(a // b ) else: stack.append(int(UpperCAmelCase__ ) ) return stack.pop() if __name__ == "__main__": import doctest doctest.testmod()
101
1
'''simple docstring''' import numpy as np import qiskit def lowercase__( __UpperCamelCase: int = 8 ,__UpperCamelCase: int | None = None ): """simple docstring""" SCREAMING_SNAKE_CASE : Any = np.random.default_rng(seed=__UpperCamelCase ) # Roughly 25% of the qubits will contribute to the key. # So we take more than we need. SCREAMING_SNAKE_CASE : str = 6 * key_len # Measurement basis for Alice's qubits. SCREAMING_SNAKE_CASE : Tuple = rng.integers(2 ,size=__UpperCamelCase ) # The set of states Alice will prepare. SCREAMING_SNAKE_CASE : Optional[int] = rng.integers(2 ,size=__UpperCamelCase ) # Measurement basis for Bob's qubits. SCREAMING_SNAKE_CASE : Optional[Any] = rng.integers(2 ,size=__UpperCamelCase ) # Quantum Circuit to simulate BB84 SCREAMING_SNAKE_CASE : int = qiskit.QuantumCircuit(__UpperCamelCase ,name='BB84' ) # Alice prepares her qubits according to rules above. for index, _ in enumerate(__UpperCamelCase ): if alice_state[index] == 1: bbaa_circ.x(__UpperCamelCase ) if alice_basis[index] == 1: bbaa_circ.h(__UpperCamelCase ) bbaa_circ.barrier() # Bob measures the received qubits according to rules above. for index, _ in enumerate(__UpperCamelCase ): if bob_basis[index] == 1: bbaa_circ.h(__UpperCamelCase ) bbaa_circ.barrier() bbaa_circ.measure_all() # Simulate the quantum circuit. SCREAMING_SNAKE_CASE : Tuple = 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. SCREAMING_SNAKE_CASE : List[Any] = qiskit.execute(__UpperCamelCase ,__UpperCamelCase ,shots=1 ,seed_simulator=__UpperCamelCase ) # Returns the result of measurement. SCREAMING_SNAKE_CASE : int = job.result().get_counts(__UpperCamelCase ).most_frequent() # Extracting the generated key from the simulation results. # Only keep measurement results where Alice and Bob chose the same basis. SCREAMING_SNAKE_CASE : Optional[Any] = ''.join( [ result_bit for alice_basis_bit, bob_basis_bit, result_bit in zip( __UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase ) if alice_basis_bit == bob_basis_bit ] ) # Get final key. Pad with 0 if too short, otherwise truncate. SCREAMING_SNAKE_CASE : Union[str, Any] = gen_key[:key_len] if len(__UpperCamelCase ) >= key_len else gen_key.ljust(__UpperCamelCase ,'0' ) return key if __name__ == "__main__": print(F"""The generated key is : {bbaa(8, seed=0)}""") from doctest import testmod testmod()
251
'''simple docstring''' from __future__ import annotations from scipy.special import comb # type: ignore class _a : '''simple docstring''' def __init__( self, A ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = list_of_points # Degree determines the flexibility of the curve. # Degree = 1 will produce a straight line. SCREAMING_SNAKE_CASE : Optional[int] = len(A ) - 1 def UpperCamelCase_ ( self, A ): '''simple docstring''' assert 0 <= t <= 1, "Time t must be between 0 and 1." SCREAMING_SNAKE_CASE : list[float] = [] for i in range(len(self.list_of_points ) ): # basis function for each i output_values.append( comb(self.degree, A ) * ((1 - t) ** (self.degree - i)) * (t**i) ) # the basis must sum up to 1 for it to produce a valid Bezier curve. assert round(sum(A ), 5 ) == 1 return output_values def UpperCamelCase_ ( self, A ): '''simple docstring''' assert 0 <= t <= 1, "Time t must be between 0 and 1." SCREAMING_SNAKE_CASE : str = self.basis_function(A ) SCREAMING_SNAKE_CASE : str = 0.0 SCREAMING_SNAKE_CASE : List[Any] = 0.0 for i in range(len(self.list_of_points ) ): # For all points, sum up the product of i-th basis function and i-th point. x += basis_function[i] * self.list_of_points[i][0] y += basis_function[i] * self.list_of_points[i][1] return (x, y) def UpperCamelCase_ ( self, A = 0.01 ): '''simple docstring''' from matplotlib import pyplot as plt # type: ignore SCREAMING_SNAKE_CASE : list[float] = [] # x coordinates of points to plot SCREAMING_SNAKE_CASE : list[float] = [] # y coordinates of points to plot SCREAMING_SNAKE_CASE : List[str] = 0.0 while t <= 1: SCREAMING_SNAKE_CASE : Optional[int] = self.bezier_curve_function(A ) to_plot_x.append(value[0] ) to_plot_y.append(value[1] ) t += step_size SCREAMING_SNAKE_CASE : List[Any] = [i[0] for i in self.list_of_points] SCREAMING_SNAKE_CASE : Union[str, Any] = [i[1] for i in self.list_of_points] plt.plot( A, A, color='blue', label='Curve of Degree ' + str(self.degree ), ) plt.scatter(A, A, color='red', label='Control Points' ) plt.legend() plt.show() if __name__ == "__main__": import doctest doctest.testmod() BezierCurve([(1, 2), (3, 5)]).plot_curve() # degree 1 BezierCurve([(0, 0), (5, 5), (5, 0)]).plot_curve() # degree 2 BezierCurve([(0, 0), (5, 5), (5, 0), (2.5, -2.5)]).plot_curve() # degree 3
251
1
import warnings from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = ["image_processor", "tokenizer"] lowercase_ = "ChineseCLIPImageProcessor" lowercase_ = ("BertTokenizer", "BertTokenizerFast") def __init__( self : Optional[Any] , _lowerCAmelCase : Any=None , _lowerCAmelCase : Dict=None , **_lowerCAmelCase : Dict ): SCREAMING_SNAKE_CASE_ = None if "feature_extractor" in kwargs: warnings.warn( 'The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`' ' instead.' , _lowerCAmelCase , ) SCREAMING_SNAKE_CASE_ = kwargs.pop('feature_extractor' ) SCREAMING_SNAKE_CASE_ = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError('You need to specify an `image_processor`.' ) if tokenizer is None: raise ValueError('You need to specify a `tokenizer`.' ) super().__init__(_lowerCAmelCase , _lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.image_processor def __call__( self : Optional[int] , _lowerCAmelCase : Tuple=None , _lowerCAmelCase : str=None , _lowerCAmelCase : str=None , **_lowerCAmelCase : Tuple ): if text is None and images is None: raise ValueError('You have to specify either text or images. Both cannot be none.' ) if text is not None: SCREAMING_SNAKE_CASE_ = self.tokenizer(_lowerCAmelCase , return_tensors=_lowerCAmelCase , **_lowerCAmelCase ) if images is not None: SCREAMING_SNAKE_CASE_ = self.image_processor(_lowerCAmelCase , return_tensors=_lowerCAmelCase , **_lowerCAmelCase ) if text is not None and images is not None: SCREAMING_SNAKE_CASE_ = image_features.pixel_values return encoding elif text is not None: return encoding else: return BatchEncoding(data=dict(**_lowerCAmelCase ) , tensor_type=_lowerCAmelCase ) def lowerCAmelCase_ ( self : Optional[int] , *_lowerCAmelCase : int , **_lowerCAmelCase : Union[str, Any] ): return self.tokenizer.batch_decode(*_lowerCAmelCase , **_lowerCAmelCase ) def lowerCAmelCase_ ( self : str , *_lowerCAmelCase : Dict , **_lowerCAmelCase : List[Any] ): return self.tokenizer.decode(*_lowerCAmelCase , **_lowerCAmelCase ) @property def lowerCAmelCase_ ( self : int ): SCREAMING_SNAKE_CASE_ = self.tokenizer.model_input_names SCREAMING_SNAKE_CASE_ = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) ) @property def lowerCAmelCase_ ( self : List[str] ): warnings.warn( '`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.' , _lowerCAmelCase , ) return self.image_processor_class
361
import importlib import json import os import sys import tempfile import unittest from pathlib import Path import transformers import transformers.models.auto from transformers.models.auto.configuration_auto import CONFIG_MAPPING, AutoConfig from transformers.models.bert.configuration_bert import BertConfig from transformers.models.roberta.configuration_roberta import RobertaConfig from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, get_tests_dir sys.path.append(str(Path(__file__).parent.parent.parent.parent / 'utils')) from test_module.custom_configuration import CustomConfig # noqa E402 lowerCamelCase__ : str = get_tests_dir('fixtures/dummy-config.json') class lowerCamelCase_ ( unittest.TestCase ): '''simple docstring''' def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = 0 def lowerCAmelCase_ ( self : Optional[int] ): self.assertIsNotNone(transformers.models.auto.__spec__ ) self.assertIsNotNone(importlib.util.find_spec('transformers.models.auto' ) ) def lowerCAmelCase_ ( self : Any ): SCREAMING_SNAKE_CASE_ = AutoConfig.from_pretrained('bert-base-uncased' ) self.assertIsInstance(_lowerCAmelCase , _lowerCAmelCase ) def lowerCAmelCase_ ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE_ = AutoConfig.from_pretrained(_lowerCAmelCase ) self.assertIsInstance(_lowerCAmelCase , _lowerCAmelCase ) def lowerCAmelCase_ ( self : Optional[Any] ): SCREAMING_SNAKE_CASE_ = AutoConfig.from_pretrained(_lowerCAmelCase ) self.assertIsInstance(_lowerCAmelCase , _lowerCAmelCase ) def lowerCAmelCase_ ( self : Dict ): SCREAMING_SNAKE_CASE_ = AutoConfig.for_model('roberta' ) self.assertIsInstance(_lowerCAmelCase , _lowerCAmelCase ) def lowerCAmelCase_ ( self : Dict ): with tempfile.TemporaryDirectory() as tmp_dir: # This model name contains bert and roberta, but roberta ends up being picked. SCREAMING_SNAKE_CASE_ = os.path.join(_lowerCAmelCase , 'fake-roberta' ) os.makedirs(_lowerCAmelCase , exist_ok=_lowerCAmelCase ) with open(os.path.join(_lowerCAmelCase , 'config.json' ) , 'w' ) as f: f.write(json.dumps({} ) ) SCREAMING_SNAKE_CASE_ = AutoConfig.from_pretrained(_lowerCAmelCase ) self.assertEqual(type(_lowerCAmelCase ) , _lowerCAmelCase ) def lowerCAmelCase_ ( self : Optional[Any] ): try: AutoConfig.register('custom' , _lowerCAmelCase ) # Wrong model type will raise an error with self.assertRaises(_lowerCAmelCase ): AutoConfig.register('model' , _lowerCAmelCase ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(_lowerCAmelCase ): AutoConfig.register('bert' , _lowerCAmelCase ) # Now that the config is registered, it can be used as any other config with the auto-API SCREAMING_SNAKE_CASE_ = CustomConfig() with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = AutoConfig.from_pretrained(_lowerCAmelCase ) self.assertIsInstance(_lowerCAmelCase , _lowerCAmelCase ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] def lowerCAmelCase_ ( self : Optional[int] ): with self.assertRaisesRegex( _lowerCAmelCase , 'bert-base is not a local folder and is not a valid model identifier' ): SCREAMING_SNAKE_CASE_ = AutoConfig.from_pretrained('bert-base' ) def lowerCAmelCase_ ( self : int ): with self.assertRaisesRegex( _lowerCAmelCase , R'aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)' ): SCREAMING_SNAKE_CASE_ = AutoConfig.from_pretrained(_lowerCAmelCase , revision='aaaaaa' ) def lowerCAmelCase_ ( self : Tuple ): with self.assertRaisesRegex( _lowerCAmelCase , 'hf-internal-testing/no-config-test-repo does not appear to have a file named config.json.' , ): SCREAMING_SNAKE_CASE_ = AutoConfig.from_pretrained('hf-internal-testing/no-config-test-repo' ) def lowerCAmelCase_ ( self : Union[str, Any] ): # If remote code is not set, we will time out when asking whether to load the model. with self.assertRaises(_lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = AutoConfig.from_pretrained('hf-internal-testing/test_dynamic_model' ) # If remote code is disabled, we can't load this config. with self.assertRaises(_lowerCAmelCase ): SCREAMING_SNAKE_CASE_ = AutoConfig.from_pretrained('hf-internal-testing/test_dynamic_model' , trust_remote_code=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = AutoConfig.from_pretrained('hf-internal-testing/test_dynamic_model' , trust_remote_code=_lowerCAmelCase ) self.assertEqual(config.__class__.__name__ , 'NewModelConfig' ) # Test config can be reloaded. with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = AutoConfig.from_pretrained(_lowerCAmelCase , trust_remote_code=_lowerCAmelCase ) self.assertEqual(reloaded_config.__class__.__name__ , 'NewModelConfig' ) def lowerCAmelCase_ ( self : Any ): class lowerCamelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' lowercase_ = "new-model" try: AutoConfig.register('new-model' , _lowerCAmelCase ) # If remote code is not set, the default is to use local SCREAMING_SNAKE_CASE_ = AutoConfig.from_pretrained('hf-internal-testing/test_dynamic_model' ) self.assertEqual(config.__class__.__name__ , 'NewModelConfigLocal' ) # If remote code is disabled, we load the local one. SCREAMING_SNAKE_CASE_ = AutoConfig.from_pretrained('hf-internal-testing/test_dynamic_model' , trust_remote_code=_lowerCAmelCase ) self.assertEqual(config.__class__.__name__ , 'NewModelConfigLocal' ) # If remote is enabled, we load from the Hub SCREAMING_SNAKE_CASE_ = AutoConfig.from_pretrained('hf-internal-testing/test_dynamic_model' , trust_remote_code=_lowerCAmelCase ) self.assertEqual(config.__class__.__name__ , 'NewModelConfig' ) finally: if "new-model" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["new-model"]
210
0
"""simple docstring""" import subprocess import sys from transformers import BertConfig, BertModel, BertTokenizer, pipeline from transformers.testing_utils import TestCasePlus, require_torch class UpperCamelCase ( lowerCAmelCase__ ): @require_torch def a_ ( self) -> Tuple: # this test is a bit tricky since TRANSFORMERS_OFFLINE can only be changed before # `transformers` is loaded, and it's too late for inside pytest - so we are changing it # while running an external program # python one-liner segments # this must be loaded before socket.socket is monkey-patched snake_case_ = '\nfrom transformers import BertConfig, BertModel, BertTokenizer, pipeline\n ' snake_case_ = '\nmname = "hf-internal-testing/tiny-random-bert"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nBertTokenizer.from_pretrained(mname)\npipe = pipeline(task="fill-mask", model=mname)\nprint("success")\n ' snake_case_ = '\nimport socket\ndef offline_socket(*args, **kwargs): raise RuntimeError("Offline mode is enabled, we shouldn\'t access internet")\nsocket.socket = offline_socket\n ' # Force fetching the files so that we can use the cache snake_case_ = 'hf-internal-testing/tiny-random-bert' BertConfig.from_pretrained(lowerCAmelCase__) BertModel.from_pretrained(lowerCAmelCase__) BertTokenizer.from_pretrained(lowerCAmelCase__) pipeline(task='fill-mask', model=lowerCAmelCase__) # baseline - just load from_pretrained with normal network snake_case_ = [sys.executable, '-c', '\n'.join([load, run, mock])] # should succeed snake_case_ = self.get_env() # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files snake_case_ = '1' snake_case_ = subprocess.run(lowerCAmelCase__, env=lowerCAmelCase__, check=lowerCAmelCase__, capture_output=lowerCAmelCase__) self.assertEqual(result.returncode, 0, result.stderr) self.assertIn('success', result.stdout.decode()) @require_torch def a_ ( self) -> Tuple: # python one-liner segments # this must be loaded before socket.socket is monkey-patched snake_case_ = '\nfrom transformers import BertConfig, BertModel, BertTokenizer, pipeline\n ' snake_case_ = '\nmname = "hf-internal-testing/tiny-random-bert"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nBertTokenizer.from_pretrained(mname)\npipe = pipeline(task="fill-mask", model=mname)\nprint("success")\n ' snake_case_ = '\nimport socket\ndef offline_socket(*args, **kwargs): raise socket.error("Faking flaky internet")\nsocket.socket = offline_socket\n ' # Force fetching the files so that we can use the cache snake_case_ = 'hf-internal-testing/tiny-random-bert' BertConfig.from_pretrained(lowerCAmelCase__) BertModel.from_pretrained(lowerCAmelCase__) BertTokenizer.from_pretrained(lowerCAmelCase__) pipeline(task='fill-mask', model=lowerCAmelCase__) # baseline - just load from_pretrained with normal network snake_case_ = [sys.executable, '-c', '\n'.join([load, run, mock])] # should succeed snake_case_ = self.get_env() snake_case_ = subprocess.run(lowerCAmelCase__, env=lowerCAmelCase__, check=lowerCAmelCase__, capture_output=lowerCAmelCase__) self.assertEqual(result.returncode, 0, result.stderr) self.assertIn('success', result.stdout.decode()) @require_torch def a_ ( self) -> Union[str, Any]: # this test is a bit tricky since TRANSFORMERS_OFFLINE can only be changed before # `transformers` is loaded, and it's too late for inside pytest - so we are changing it # while running an external program # python one-liner segments # this must be loaded before socket.socket is monkey-patched snake_case_ = '\nfrom transformers import BertConfig, BertModel, BertTokenizer\n ' snake_case_ = '\nmname = "hf-internal-testing/tiny-random-bert-sharded"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nprint("success")\n ' snake_case_ = '\nimport socket\ndef offline_socket(*args, **kwargs): raise ValueError("Offline mode is enabled")\nsocket.socket = offline_socket\n ' # baseline - just load from_pretrained with normal network snake_case_ = [sys.executable, '-c', '\n'.join([load, run])] # should succeed snake_case_ = self.get_env() snake_case_ = subprocess.run(lowerCAmelCase__, env=lowerCAmelCase__, check=lowerCAmelCase__, capture_output=lowerCAmelCase__) self.assertEqual(result.returncode, 0, result.stderr) self.assertIn('success', result.stdout.decode()) # next emulate no network snake_case_ = [sys.executable, '-c', '\n'.join([load, mock, run])] # Doesn't fail anymore since the model is in the cache due to other tests, so commenting this. # env["TRANSFORMERS_OFFLINE"] = "0" # result = subprocess.run(cmd, env=env, check=False, capture_output=True) # self.assertEqual(result.returncode, 1, result.stderr) # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files snake_case_ = '1' snake_case_ = subprocess.run(lowerCAmelCase__, env=lowerCAmelCase__, check=lowerCAmelCase__, capture_output=lowerCAmelCase__) self.assertEqual(result.returncode, 0, result.stderr) self.assertIn('success', result.stdout.decode()) @require_torch def a_ ( self) -> Optional[int]: snake_case_ = '\nfrom transformers import pipeline\n ' snake_case_ = '\nmname = "hf-internal-testing/tiny-random-bert"\npipe = pipeline(model=mname)\n ' snake_case_ = '\nimport socket\ndef offline_socket(*args, **kwargs): raise socket.error("Offline mode is enabled")\nsocket.socket = offline_socket\n ' snake_case_ = self.get_env() snake_case_ = '1' snake_case_ = [sys.executable, '-c', '\n'.join([load, mock, run])] snake_case_ = subprocess.run(lowerCAmelCase__, env=lowerCAmelCase__, check=lowerCAmelCase__, capture_output=lowerCAmelCase__) self.assertEqual(result.returncode, 1, result.stderr) self.assertIn( 'You cannot infer task automatically within `pipeline` when using offline mode', result.stderr.decode().replace('\n', ''), ) @require_torch def a_ ( self) -> List[str]: snake_case_ = '\nfrom transformers import AutoModel\n ' snake_case_ = '\nmname = "hf-internal-testing/test_dynamic_model"\nAutoModel.from_pretrained(mname, trust_remote_code=True)\nprint("success")\n ' # baseline - just load from_pretrained with normal network snake_case_ = [sys.executable, '-c', '\n'.join([load, run])] # should succeed snake_case_ = self.get_env() snake_case_ = subprocess.run(lowerCAmelCase__, env=lowerCAmelCase__, check=lowerCAmelCase__, capture_output=lowerCAmelCase__) self.assertEqual(result.returncode, 0, result.stderr) self.assertIn('success', result.stdout.decode()) # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files snake_case_ = '1' snake_case_ = subprocess.run(lowerCAmelCase__, env=lowerCAmelCase__, check=lowerCAmelCase__, capture_output=lowerCAmelCase__) self.assertEqual(result.returncode, 0, result.stderr) self.assertIn('success', result.stdout.decode())
69
"""simple docstring""" from unittest import TestCase from datasets import Sequence, Value from datasets.arrow_dataset import Dataset class _lowerCamelCase ( a_ ): def _lowerCAmelCase ( self : Any ) -> str: """simple docstring""" return [ {"col_1": 3, "col_2": "a"}, {"col_1": 2, "col_2": "b"}, {"col_1": 1, "col_2": "c"}, {"col_1": 0, "col_2": "d"}, ] def _lowerCAmelCase ( self : Any ) -> Tuple: """simple docstring""" lowerCAmelCase__ : List[Any] = {"""col_1""": [3, 2, 1, 0], """col_2""": ["""a""", """b""", """c""", """d"""]} return Dataset.from_dict(UpperCamelCase ) def _lowerCAmelCase ( self : List[str] ) -> Union[str, Any]: """simple docstring""" lowerCAmelCase__ : Optional[int] = self._create_example_records() lowerCAmelCase__ : Tuple = Dataset.from_list(UpperCamelCase ) self.assertListEqual(dset.column_names , ["""col_1""", """col_2"""] ) for i, r in enumerate(UpperCamelCase ): self.assertDictEqual(UpperCamelCase , example_records[i] ) def _lowerCAmelCase ( self : Any ) -> Optional[Any]: """simple docstring""" lowerCAmelCase__ : Any = self._create_example_records() lowerCAmelCase__ : Optional[Any] = Dataset.from_list(UpperCamelCase ) lowerCAmelCase__ : int = Dataset.from_dict({k: [r[k] for r in example_records] for k in example_records[0]} ) self.assertEqual(dset.info , dset_from_dict.info ) def _lowerCAmelCase ( self : Tuple ) -> List[Any]: # checks what happens with missing columns """simple docstring""" lowerCAmelCase__ : str = [{"""col_1""": 1}, {"""col_2""": """x"""}] lowerCAmelCase__ : int = Dataset.from_list(UpperCamelCase ) self.assertDictEqual(dset[0] , {"""col_1""": 1} ) self.assertDictEqual(dset[1] , {"""col_1""": None} ) # NB: first record is used for columns def _lowerCAmelCase ( self : str ) -> Dict: # checks if the type can be inferred from the second record """simple docstring""" lowerCAmelCase__ : Union[str, Any] = [{"""col_1""": []}, {"""col_1""": [1, 2]}] lowerCAmelCase__ : Optional[int] = Dataset.from_list(UpperCamelCase ) self.assertEqual(dset.info.features["""col_1"""] , Sequence(Value("""int64""" ) ) ) def _lowerCAmelCase ( self : Any ) -> Dict: """simple docstring""" lowerCAmelCase__ : Optional[int] = Dataset.from_list([] ) self.assertEqual(len(UpperCamelCase ) , 0 ) self.assertListEqual(dset.column_names , [] )
242
0
from __future__ import annotations from collections.abc import Iterator from typing import Any class A__ : """simple docstring""" def __init__( self , __snake_case ): snake_case = data snake_case = None class A__ : """simple docstring""" def __init__( self ): snake_case = None snake_case = None def __iter__( self ): snake_case = self.head while self.head: yield node.data snake_case = node.next if node == self.head: break def __len__( self ): return sum(1 for _ in self ) def __repr__( self ): return "->".join(str(__snake_case ) for item in iter(self ) ) def a_ ( self , __snake_case ): self.insert_nth(len(self ) , __snake_case ) def a_ ( self , __snake_case ): self.insert_nth(0 , __snake_case ) def a_ ( self , __snake_case , __snake_case ): if index < 0 or index > len(self ): raise IndexError('''list index out of range.''' ) snake_case = Node(__snake_case ) if self.head is None: snake_case = new_node # first node points itself snake_case = snake_case = new_node elif index == 0: # insert at head snake_case = self.head snake_case = snake_case = new_node else: snake_case = self.head for _ in range(index - 1 ): snake_case = temp.next snake_case = temp.next snake_case = new_node if index == len(self ) - 1: # insert at tail snake_case = new_node def a_ ( self ): return self.delete_nth(0 ) def a_ ( self ): return self.delete_nth(len(self ) - 1 ) def a_ ( self , __snake_case = 0 ): if not 0 <= index < len(self ): raise IndexError('''list index out of range.''' ) snake_case = self.head if self.head == self.tail: # just one node snake_case = snake_case = None elif index == 0: # delete head node snake_case = self.tail.next.next snake_case = self.head.next else: snake_case = self.head for _ in range(index - 1 ): snake_case = temp.next snake_case = temp.next snake_case = temp.next.next if index == len(self ) - 1: # delete at tail snake_case = temp return delete_node.data def a_ ( self ): return len(self ) == 0 def UpperCAmelCase__ (): """simple docstring""" snake_case = CircularLinkedList() assert len(UpperCamelCase_ ) == 0 assert circular_linked_list.is_empty() is True assert str(UpperCamelCase_ ) == "" try: circular_linked_list.delete_front() raise AssertionError # This should not happen except IndexError: assert True # This should happen try: circular_linked_list.delete_tail() raise AssertionError # This should not happen except IndexError: assert True # This should happen try: circular_linked_list.delete_nth(-1 ) raise AssertionError except IndexError: assert True try: circular_linked_list.delete_nth(0 ) raise AssertionError except IndexError: assert True assert circular_linked_list.is_empty() is True for i in range(5 ): assert len(UpperCamelCase_ ) == i circular_linked_list.insert_nth(UpperCamelCase_ ,i + 1 ) assert str(UpperCamelCase_ ) == "->".join(str(UpperCamelCase_ ) for i in range(1 ,6 ) ) circular_linked_list.insert_tail(6 ) assert str(UpperCamelCase_ ) == "->".join(str(UpperCamelCase_ ) for i in range(1 ,7 ) ) circular_linked_list.insert_head(0 ) assert str(UpperCamelCase_ ) == "->".join(str(UpperCamelCase_ ) for i in range(0 ,7 ) ) assert circular_linked_list.delete_front() == 0 assert circular_linked_list.delete_tail() == 6 assert str(UpperCamelCase_ ) == "->".join(str(UpperCamelCase_ ) for i in range(1 ,6 ) ) assert circular_linked_list.delete_nth(2 ) == 3 circular_linked_list.insert_nth(2 ,3 ) assert str(UpperCamelCase_ ) == "->".join(str(UpperCamelCase_ ) for i in range(1 ,6 ) ) assert circular_linked_list.is_empty() is False if __name__ == "__main__": import doctest doctest.testmod()
363
import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import XLMRobertaTokenizerFast from diffusers import DDIMScheduler, KandinskyImgaImgPipeline, KandinskyPriorPipeline, UNetaDConditionModel, VQModel from diffusers.pipelines.kandinsky.text_encoder import MCLIPConfig, MultilingualCLIP from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference enable_full_determinism() class A__ ( snake_case__ , unittest.TestCase ): """simple docstring""" __magic_name__ = KandinskyImgaImgPipeline __magic_name__ = ['prompt', 'image_embeds', 'negative_image_embeds', 'image'] __magic_name__ = [ 'prompt', 'negative_prompt', 'image_embeds', 'negative_image_embeds', 'image', ] __magic_name__ = [ 'generator', 'height', 'width', 'strength', 'guidance_scale', 'negative_prompt', 'num_inference_steps', 'return_dict', 'guidance_scale', 'num_images_per_prompt', 'output_type', 'return_dict', ] __magic_name__ = False @property def a_ ( self ): return 3_2 @property def a_ ( self ): return 3_2 @property def a_ ( self ): return self.time_input_dim @property def a_ ( self ): return self.time_input_dim * 4 @property def a_ ( self ): return 1_0_0 @property def a_ ( self ): snake_case = XLMRobertaTokenizerFast.from_pretrained('''YiYiXu/tiny-random-mclip-base''' ) return tokenizer @property def a_ ( self ): torch.manual_seed(0 ) snake_case = MCLIPConfig( numDims=self.cross_attention_dim , transformerDimensions=self.text_embedder_hidden_size , hidden_size=self.text_embedder_hidden_size , intermediate_size=3_7 , num_attention_heads=4 , num_hidden_layers=5 , vocab_size=1_0_0_5 , ) snake_case = MultilingualCLIP(__snake_case ) snake_case = text_encoder.eval() return text_encoder @property def a_ ( self ): torch.manual_seed(0 ) snake_case = { '''in_channels''': 4, # Out channels is double in channels because predicts mean and variance '''out_channels''': 8, '''addition_embed_type''': '''text_image''', '''down_block_types''': ('''ResnetDownsampleBlock2D''', '''SimpleCrossAttnDownBlock2D'''), '''up_block_types''': ('''SimpleCrossAttnUpBlock2D''', '''ResnetUpsampleBlock2D'''), '''mid_block_type''': '''UNetMidBlock2DSimpleCrossAttn''', '''block_out_channels''': (self.block_out_channels_a, self.block_out_channels_a * 2), '''layers_per_block''': 1, '''encoder_hid_dim''': self.text_embedder_hidden_size, '''encoder_hid_dim_type''': '''text_image_proj''', '''cross_attention_dim''': self.cross_attention_dim, '''attention_head_dim''': 4, '''resnet_time_scale_shift''': '''scale_shift''', '''class_embed_type''': None, } snake_case = UNetaDConditionModel(**__snake_case ) return model @property def a_ ( self ): return { "block_out_channels": [3_2, 6_4], "down_block_types": ["DownEncoderBlock2D", "AttnDownEncoderBlock2D"], "in_channels": 3, "latent_channels": 4, "layers_per_block": 1, "norm_num_groups": 8, "norm_type": "spatial", "num_vq_embeddings": 1_2, "out_channels": 3, "up_block_types": [ "AttnUpDecoderBlock2D", "UpDecoderBlock2D", ], "vq_embed_dim": 4, } @property def a_ ( self ): torch.manual_seed(0 ) snake_case = VQModel(**self.dummy_movq_kwargs ) return model def a_ ( self ): snake_case = self.dummy_text_encoder snake_case = self.dummy_tokenizer snake_case = self.dummy_unet snake_case = self.dummy_movq snake_case = { '''num_train_timesteps''': 1_0_0_0, '''beta_schedule''': '''linear''', '''beta_start''': 0.0_0085, '''beta_end''': 0.012, '''clip_sample''': False, '''set_alpha_to_one''': False, '''steps_offset''': 0, '''prediction_type''': '''epsilon''', '''thresholding''': False, } snake_case = DDIMScheduler(**__snake_case ) snake_case = { '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''unet''': unet, '''scheduler''': scheduler, '''movq''': movq, } return components def a_ ( self , __snake_case , __snake_case=0 ): snake_case = floats_tensor((1, self.cross_attention_dim) , rng=random.Random(__snake_case ) ).to(__snake_case ) snake_case = floats_tensor((1, self.cross_attention_dim) , rng=random.Random(seed + 1 ) ).to(__snake_case ) # create init_image snake_case = floats_tensor((1, 3, 6_4, 6_4) , rng=random.Random(__snake_case ) ).to(__snake_case ) snake_case = image.cpu().permute(0 , 2 , 3 , 1 )[0] snake_case = Image.fromarray(np.uinta(__snake_case ) ).convert('''RGB''' ).resize((2_5_6, 2_5_6) ) if str(__snake_case ).startswith('''mps''' ): snake_case = torch.manual_seed(__snake_case ) else: snake_case = torch.Generator(device=__snake_case ).manual_seed(__snake_case ) snake_case = { '''prompt''': '''horse''', '''image''': init_image, '''image_embeds''': image_embeds, '''negative_image_embeds''': negative_image_embeds, '''generator''': generator, '''height''': 6_4, '''width''': 6_4, '''num_inference_steps''': 1_0, '''guidance_scale''': 7.0, '''strength''': 0.2, '''output_type''': '''np''', } return inputs def a_ ( self ): snake_case = '''cpu''' snake_case = self.get_dummy_components() snake_case = self.pipeline_class(**__snake_case ) snake_case = pipe.to(__snake_case ) pipe.set_progress_bar_config(disable=__snake_case ) snake_case = pipe(**self.get_dummy_inputs(__snake_case ) ) snake_case = output.images snake_case = pipe( **self.get_dummy_inputs(__snake_case ) , return_dict=__snake_case , )[0] snake_case = image[0, -3:, -3:, -1] snake_case = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 6_4, 6_4, 3) snake_case = np.array( [0.6147_4943, 0.607_3539, 0.4330_8544, 0.592_8269, 0.4749_3595, 0.4675_5973, 0.461_3838, 0.4536_8797, 0.5011_9233] ) assert ( np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 ), F''' expected_slice {expected_slice}, but got {image_slice.flatten()}''' assert ( np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1E-2 ), F''' expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}''' @slow @require_torch_gpu class A__ ( unittest.TestCase ): """simple docstring""" def a_ ( self ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def a_ ( self ): snake_case = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/kandinsky/kandinsky_img2img_frog.npy''' ) snake_case = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/kandinsky/cat.png''' ) snake_case = '''A red cartoon frog, 4k''' snake_case = KandinskyPriorPipeline.from_pretrained( '''kandinsky-community/kandinsky-2-1-prior''' , torch_dtype=torch.floataa ) pipe_prior.to(__snake_case ) snake_case = KandinskyImgaImgPipeline.from_pretrained( '''kandinsky-community/kandinsky-2-1''' , torch_dtype=torch.floataa ) snake_case = pipeline.to(__snake_case ) pipeline.set_progress_bar_config(disable=__snake_case ) snake_case = torch.Generator(device='''cpu''' ).manual_seed(0 ) snake_case , snake_case = pipe_prior( __snake_case , generator=__snake_case , num_inference_steps=5 , negative_prompt='''''' , ).to_tuple() snake_case = pipeline( __snake_case , image=__snake_case , image_embeds=__snake_case , negative_image_embeds=__snake_case , generator=__snake_case , num_inference_steps=1_0_0 , height=7_6_8 , width=7_6_8 , strength=0.2 , output_type='''np''' , ) snake_case = output.images[0] assert image.shape == (7_6_8, 7_6_8, 3) assert_mean_pixel_difference(__snake_case , __snake_case )
213
0
"""simple docstring""" def _A ( lowercase ): """simple docstring""" if not head: return True # split the list to two parts a , a =head.next, head while fast and fast.next: a =fast.next.next a =slow.next a =slow.next a =None # Don't forget here! But forget still works! # reverse the second part a =None while second: a =second.next a =node a =second a =nxt # compare two parts # second part has the same or one less node while node: if node.val != head.val: return False a =node.next a =head.next return True def _A ( lowercase ): """simple docstring""" if not head or not head.next: return True # 1. Get the midpoint (slow) a =a =a =head while fast and fast.next: a , a =fast.next.next, slow.next # 2. Push the second half into the stack a =[slow.val] while slow.next: a =slow.next stack.append(slow.val ) # 3. Comparison while stack: if stack.pop() != cur.val: return False a =cur.next return True def _A ( lowercase ): """simple docstring""" if not head or not head.next: return True a ={} a =0 while head: if head.val in d: d[head.val].append(lowercase ) else: a =[pos] a =head.next pos += 1 a =pos - 1 a =0 for v in d.values(): if len(lowercase ) % 2 != 0: middle += 1 else: a =0 for i in range(0 , len(lowercase ) ): if v[i] + v[len(lowercase ) - 1 - step] != checksum: return False step += 1 if middle > 1: return False return True
81
import absl # noqa: F401 # Here to have a nice missing dependency error message early on import nltk # noqa: F401 # Here to have a nice missing dependency error message early on import numpy # noqa: F401 # Here to have a nice missing dependency error message early on import six # noqa: F401 # Here to have a nice missing dependency error message early on from rouge_score import rouge_scorer, scoring import datasets snake_case : List[Any] = "\\n@inproceedings{lin-2004-rouge,\n title = \"{ROUGE}: A Package for Automatic Evaluation of Summaries\",\n author = \"Lin, Chin-Yew\",\n booktitle = \"Text Summarization Branches Out\",\n month = jul,\n year = \"2004\",\n address = \"Barcelona, Spain\",\n publisher = \"Association for Computational Linguistics\",\n url = \"https://www.aclweb.org/anthology/W04-1013\",\n pages = \"74--81\",\n}\n" snake_case : Any = "\\nROUGE, or Recall-Oriented Understudy for Gisting Evaluation, is a set of metrics and a software package used for\nevaluating automatic summarization and machine translation software in natural language processing.\nThe metrics compare an automatically produced summary or translation against a reference or a set of references (human-produced) summary or translation.\n\nNote that ROUGE is case insensitive, meaning that upper case letters are treated the same way as lower case letters.\n\nThis metrics is a wrapper around Google Research reimplementation of ROUGE:\nhttps://github.com/google-research/google-research/tree/master/rouge\n" snake_case : str = "\nCalculates average rouge scores for a list of hypotheses and references\nArgs:\n predictions: list of predictions to score. Each prediction\n should be a string with tokens separated by spaces.\n references: list of reference for each prediction. Each\n reference should be a string with tokens separated by spaces.\n rouge_types: A list of rouge types to calculate.\n Valid names:\n `\"rouge{n}\"` (e.g. `\"rouge1\"`, `\"rouge2\"`) where: {n} is the n-gram based scoring,\n `\"rougeL\"`: Longest common subsequence based scoring.\n `\"rougeLSum\"`: rougeLsum splits text using `\"\n\"`.\n See details in https://github.com/huggingface/datasets/issues/617\n use_stemmer: Bool indicating whether Porter stemmer should be used to strip word suffixes.\n use_aggregator: Return aggregates if this is set to True\nReturns:\n rouge1: rouge_1 (precision, recall, f1),\n rouge2: rouge_2 (precision, recall, f1),\n rougeL: rouge_l (precision, recall, f1),\n rougeLsum: rouge_lsum (precision, recall, f1)\nExamples:\n\n >>> rouge = datasets.load_metric('rouge')\n >>> predictions = [\"hello there\", \"general kenobi\"]\n >>> references = [\"hello there\", \"general kenobi\"]\n >>> results = rouge.compute(predictions=predictions, references=references)\n >>> print(list(results.keys()))\n ['rouge1', 'rouge2', 'rougeL', 'rougeLsum']\n >>> print(results[\"rouge1\"])\n AggregateScore(low=Score(precision=1.0, recall=1.0, fmeasure=1.0), mid=Score(precision=1.0, recall=1.0, fmeasure=1.0), high=Score(precision=1.0, recall=1.0, fmeasure=1.0))\n >>> print(results[\"rouge1\"].mid.fmeasure)\n 1.0\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class _snake_case ( datasets.Metric ): def SCREAMING_SNAKE_CASE ( self ): return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { "predictions": datasets.Value("string" , id="sequence" ), "references": datasets.Value("string" , id="sequence" ), } ) , codebase_urls=["https://github.com/google-research/google-research/tree/master/rouge"] , reference_urls=[ "https://en.wikipedia.org/wiki/ROUGE_(metric)", "https://github.com/google-research/google-research/tree/master/rouge", ] , ) def SCREAMING_SNAKE_CASE ( self , _a , _a , _a=None , _a=True , _a=False ): if rouge_types is None: __magic_name__ : str = ["rouge1", "rouge2", "rougeL", "rougeLsum"] __magic_name__ : List[str] = rouge_scorer.RougeScorer(rouge_types=_a , use_stemmer=_a ) if use_aggregator: __magic_name__ : Dict = scoring.BootstrapAggregator() else: __magic_name__ : str = [] for ref, pred in zip(_a , _a ): __magic_name__ : Union[str, Any] = scorer.score(_a , _a ) if use_aggregator: aggregator.add_scores(_a ) else: scores.append(_a ) if use_aggregator: __magic_name__ : Any = aggregator.aggregate() else: __magic_name__ : List[Any] = {} for key in scores[0]: __magic_name__ : str = [score[key] for score in scores] return result
281
0
from itertools import zip_longest import requests from bsa import BeautifulSoup from pandas import DataFrame def __SCREAMING_SNAKE_CASE ( __UpperCamelCase : str = "laptop" ) -> DataFrame: """simple docstring""" SCREAMING_SNAKE_CASE__ = f"""https://www.amazon.in/laptop/s?k={product}""" SCREAMING_SNAKE_CASE__ = { """User-Agent""": """Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)Chrome/44.0.2403.157 Safari/537.36""", """Accept-Language""": """en-US, en;q=0.5""", } SCREAMING_SNAKE_CASE__ = BeautifulSoup(requests.get(__UpperCamelCase , headers=__UpperCamelCase ).text ) # Initialize a Pandas dataframe with the column titles SCREAMING_SNAKE_CASE__ = DataFrame( columns=[ """Product Title""", """Product Link""", """Current Price of the product""", """Product Rating""", """MRP of the product""", """Discount""", ] ) # Loop through each entry and store them in the dataframe for item, _ in zip_longest( soup.find_all( """div""" , attrs={"""class""": """s-result-item""", """data-component-type""": """s-search-result"""} , ) , soup.find_all("""div""" , attrs={"""class""": """a-row a-size-base a-color-base"""} ) , ): try: SCREAMING_SNAKE_CASE__ = item.ha.text SCREAMING_SNAKE_CASE__ = """https://www.amazon.in/""" + item.ha.a["""href"""] SCREAMING_SNAKE_CASE__ = item.find("""span""" , attrs={"""class""": """a-offscreen"""} ).text try: SCREAMING_SNAKE_CASE__ = item.find("""span""" , attrs={"""class""": """a-icon-alt"""} ).text except AttributeError: SCREAMING_SNAKE_CASE__ = """Not available""" try: SCREAMING_SNAKE_CASE__ = ( """₹""" + item.find( """span""" , attrs={"""class""": """a-price a-text-price"""} ).text.split("""₹""" )[1] ) except AttributeError: SCREAMING_SNAKE_CASE__ = """""" try: SCREAMING_SNAKE_CASE__ = float( ( ( float(product_mrp.strip("""₹""" ).replace(""",""" , """""" ) ) - float(product_price.strip("""₹""" ).replace(""",""" , """""" ) ) ) / float(product_mrp.strip("""₹""" ).replace(""",""" , """""" ) ) ) * 1_00 ) except ValueError: SCREAMING_SNAKE_CASE__ = float("""nan""" ) except AttributeError: pass SCREAMING_SNAKE_CASE__ = [ product_title, product_link, product_price, product_rating, product_mrp, discount, ] SCREAMING_SNAKE_CASE__ = """ """ SCREAMING_SNAKE_CASE__ = """ """ data_frame.index += 1 return data_frame if __name__ == "__main__": __lowerCamelCase : int = '''headphones''' get_amazon_product_data(product).to_csv(F"""Amazon Product Data for {product}.csv""")
204
from __future__ import annotations import unittest from transformers import LEDConfig, is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFLEDForConditionalGeneration, TFLEDModel @require_tf class __snake_case : lowerCAmelCase_ = LEDConfig lowerCAmelCase_ = {} lowerCAmelCase_ = "gelu" def __init__( self : Any , _lowercase : Tuple , _lowercase : str=13 , _lowercase : Optional[int]=7 , _lowercase : Optional[Any]=True , _lowercase : Dict=False , _lowercase : Union[str, Any]=99 , _lowercase : Any=32 , _lowercase : int=2 , _lowercase : List[str]=4 , _lowercase : Optional[int]=37 , _lowercase : Union[str, Any]=0.1 , _lowercase : str=0.1 , _lowercase : Union[str, Any]=20 , _lowercase : List[str]=2 , _lowercase : Optional[int]=1 , _lowercase : Dict=0 , _lowercase : List[str]=4 , ): """simple docstring""" SCREAMING_SNAKE_CASE__ = parent SCREAMING_SNAKE_CASE__ = batch_size SCREAMING_SNAKE_CASE__ = seq_length SCREAMING_SNAKE_CASE__ = is_training SCREAMING_SNAKE_CASE__ = use_labels SCREAMING_SNAKE_CASE__ = vocab_size SCREAMING_SNAKE_CASE__ = hidden_size SCREAMING_SNAKE_CASE__ = num_hidden_layers SCREAMING_SNAKE_CASE__ = num_attention_heads SCREAMING_SNAKE_CASE__ = intermediate_size SCREAMING_SNAKE_CASE__ = hidden_dropout_prob SCREAMING_SNAKE_CASE__ = attention_probs_dropout_prob SCREAMING_SNAKE_CASE__ = max_position_embeddings SCREAMING_SNAKE_CASE__ = eos_token_id SCREAMING_SNAKE_CASE__ = pad_token_id SCREAMING_SNAKE_CASE__ = bos_token_id SCREAMING_SNAKE_CASE__ = attention_window # `ModelTesterMixin.test_attention_outputs` is expecting attention tensors to be of size # [num_attention_heads, encoder_seq_length, encoder_key_length], but TFLongformerSelfAttention # returns attention of shape [num_attention_heads, encoder_seq_length, self.attention_window + 1] # because its local attention only attends to `self.attention_window` and one before and one after SCREAMING_SNAKE_CASE__ = self.attention_window + 2 # because of padding `encoder_seq_length`, is different from `seq_length`. Relevant for # the `test_attention_outputs` and `test_hidden_states_output` tests SCREAMING_SNAKE_CASE__ = ( self.seq_length + (self.attention_window - self.seq_length % self.attention_window) % self.attention_window ) def __a ( self : Optional[int] ): """simple docstring""" SCREAMING_SNAKE_CASE__ = ids_tensor([self.batch_size, self.seq_length - 1] , self.vocab_size ) SCREAMING_SNAKE_CASE__ = tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size ) , 1 ) SCREAMING_SNAKE_CASE__ = tf.concat([input_ids, eos_tensor] , axis=1 ) SCREAMING_SNAKE_CASE__ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) SCREAMING_SNAKE_CASE__ = self.config_cls( vocab_size=self.vocab_size , d_model=self.hidden_size , encoder_layers=self.num_hidden_layers , decoder_layers=self.num_hidden_layers , encoder_attention_heads=self.num_attention_heads , decoder_attention_heads=self.num_attention_heads , encoder_ffn_dim=self.intermediate_size , decoder_ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , eos_token_ids=[2] , bos_token_id=self.bos_token_id , pad_token_id=self.pad_token_id , decoder_start_token_id=self.pad_token_id , attention_window=self.attention_window , **self.config_updates , ) SCREAMING_SNAKE_CASE__ = prepare_led_inputs_dict(_lowercase , _lowercase , _lowercase ) SCREAMING_SNAKE_CASE__ = tf.concat( [tf.zeros_like(_lowercase )[:, :-1], tf.ones_like(_lowercase )[:, -1:]] , axis=-1 , ) SCREAMING_SNAKE_CASE__ = global_attention_mask return config, inputs_dict def __a ( self : Tuple , _lowercase : int , _lowercase : int ): """simple docstring""" SCREAMING_SNAKE_CASE__ = TFLEDModel(config=_lowercase ).get_decoder() SCREAMING_SNAKE_CASE__ = inputs_dict["""input_ids"""] SCREAMING_SNAKE_CASE__ = input_ids[:1, :] SCREAMING_SNAKE_CASE__ = inputs_dict["""attention_mask"""][:1, :] SCREAMING_SNAKE_CASE__ = 1 # first forward pass SCREAMING_SNAKE_CASE__ = model(_lowercase , attention_mask=_lowercase , use_cache=_lowercase ) SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ = outputs.to_tuple() # create hypothetical next token and extent to next_input_ids SCREAMING_SNAKE_CASE__ = ids_tensor((self.batch_size, 3) , config.vocab_size ) SCREAMING_SNAKE_CASE__ = tf.cast(ids_tensor((self.batch_size, 3) , 2 ) , tf.inta ) # append to next input_ids and SCREAMING_SNAKE_CASE__ = tf.concat([input_ids, next_tokens] , axis=-1 ) SCREAMING_SNAKE_CASE__ = tf.concat([attention_mask, next_attn_mask] , axis=-1 ) SCREAMING_SNAKE_CASE__ = model(_lowercase , attention_mask=_lowercase )[0] SCREAMING_SNAKE_CASE__ = model(_lowercase , attention_mask=_lowercase , past_key_values=_lowercase )[0] self.parent.assertEqual(next_tokens.shape[1] , output_from_past.shape[1] ) # select random slice SCREAMING_SNAKE_CASE__ = int(ids_tensor((1,) , output_from_past.shape[-1] ) ) SCREAMING_SNAKE_CASE__ = output_from_no_past[:, -3:, random_slice_idx] SCREAMING_SNAKE_CASE__ = output_from_past[:, :, random_slice_idx] # test that outputs are equal for slice tf.debugging.assert_near(_lowercase , _lowercase , rtol=1E-3 ) def __SCREAMING_SNAKE_CASE ( __UpperCamelCase : Optional[Any] , __UpperCamelCase : Dict , __UpperCamelCase : int , __UpperCamelCase : List[str]=None , __UpperCamelCase : Union[str, Any]=None , __UpperCamelCase : Dict=None , __UpperCamelCase : Optional[Any]=None , ) -> int: """simple docstring""" if attention_mask is None: SCREAMING_SNAKE_CASE__ = tf.cast(tf.math.not_equal(__UpperCamelCase , config.pad_token_id ) , tf.inta ) if decoder_attention_mask is None: SCREAMING_SNAKE_CASE__ = tf.concat( [ tf.ones(decoder_input_ids[:, :1].shape , dtype=tf.inta ), tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:] , config.pad_token_id ) , tf.inta ), ] , axis=-1 , ) if head_mask is None: SCREAMING_SNAKE_CASE__ = tf.ones((config.encoder_layers, config.encoder_attention_heads) ) if decoder_head_mask is None: SCREAMING_SNAKE_CASE__ = tf.ones((config.decoder_layers, config.decoder_attention_heads) ) return { "input_ids": input_ids, "attention_mask": attention_mask, "decoder_input_ids": decoder_input_ids, "decoder_attention_mask": decoder_attention_mask, "head_mask": head_mask, "decoder_head_mask": decoder_head_mask, } @require_tf class __snake_case ( lowerCamelCase_ , lowerCamelCase_ , unittest.TestCase ): lowerCAmelCase_ = (TFLEDForConditionalGeneration, TFLEDModel) if is_tf_available() else () lowerCAmelCase_ = (TFLEDForConditionalGeneration,) if is_tf_available() else () lowerCAmelCase_ = ( { "conversational": TFLEDForConditionalGeneration, "feature-extraction": TFLEDModel, "summarization": TFLEDForConditionalGeneration, "text2text-generation": TFLEDForConditionalGeneration, "translation": TFLEDForConditionalGeneration, } if is_tf_available() else {} ) lowerCAmelCase_ = True lowerCAmelCase_ = False lowerCAmelCase_ = False lowerCAmelCase_ = False def __a ( self : Dict ): """simple docstring""" SCREAMING_SNAKE_CASE__ = TFLEDModelTester(self ) SCREAMING_SNAKE_CASE__ = ConfigTester(self , config_class=_lowercase ) def __a ( self : str ): """simple docstring""" self.config_tester.run_common_tests() def __a ( self : Optional[Any] ): """simple docstring""" SCREAMING_SNAKE_CASE__ = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_decoder_model_past_large_inputs(*_lowercase ) def __a ( self : Tuple ): """simple docstring""" SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE__ = tf.zeros_like(inputs_dict["""attention_mask"""] ) SCREAMING_SNAKE_CASE__ = 2 SCREAMING_SNAKE_CASE__ = tf.where( tf.range(self.model_tester.seq_length )[None, :] < num_global_attn_indices , 1 , inputs_dict["""global_attention_mask"""] , ) SCREAMING_SNAKE_CASE__ = True SCREAMING_SNAKE_CASE__ = self.model_tester.seq_length SCREAMING_SNAKE_CASE__ = self.model_tester.encoder_seq_length def check_decoder_attentions_output(_lowercase : List[str] ): SCREAMING_SNAKE_CASE__ = outputs.decoder_attentions self.assertEqual(len(_lowercase ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(decoder_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, seq_length, seq_length] , ) def check_encoder_attentions_output(_lowercase : List[str] ): SCREAMING_SNAKE_CASE__ = [t.numpy() for t in outputs.encoder_attentions] SCREAMING_SNAKE_CASE__ = [t.numpy() for t in outputs.encoder_global_attentions] self.assertEqual(len(_lowercase ) , self.model_tester.num_hidden_layers ) self.assertEqual(len(_lowercase ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, seq_length, seq_length] , ) self.assertListEqual( list(global_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, encoder_seq_length, num_global_attn_indices] , ) for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE__ = True SCREAMING_SNAKE_CASE__ = False SCREAMING_SNAKE_CASE__ = False SCREAMING_SNAKE_CASE__ = model_class(_lowercase ) SCREAMING_SNAKE_CASE__ = model(self._prepare_for_class(_lowercase , _lowercase ) ) SCREAMING_SNAKE_CASE__ = len(_lowercase ) self.assertEqual(config.output_hidden_states , _lowercase ) check_encoder_attentions_output(_lowercase ) if self.is_encoder_decoder: SCREAMING_SNAKE_CASE__ = model_class(_lowercase ) SCREAMING_SNAKE_CASE__ = model(self._prepare_for_class(_lowercase , _lowercase ) ) self.assertEqual(config.output_hidden_states , _lowercase ) check_decoder_attentions_output(_lowercase ) # Check that output attentions can also be changed via the config del inputs_dict["output_attentions"] SCREAMING_SNAKE_CASE__ = True SCREAMING_SNAKE_CASE__ = model_class(_lowercase ) SCREAMING_SNAKE_CASE__ = model(self._prepare_for_class(_lowercase , _lowercase ) ) self.assertEqual(config.output_hidden_states , _lowercase ) check_encoder_attentions_output(_lowercase ) # Check attention is always last and order is fine SCREAMING_SNAKE_CASE__ = True SCREAMING_SNAKE_CASE__ = True SCREAMING_SNAKE_CASE__ = model_class(_lowercase ) SCREAMING_SNAKE_CASE__ = model(self._prepare_for_class(_lowercase , _lowercase ) ) self.assertEqual(out_len + (2 if self.is_encoder_decoder else 1) , len(_lowercase ) ) self.assertEqual(model.config.output_hidden_states , _lowercase ) check_encoder_attentions_output(_lowercase ) @unittest.skip("""LED keeps using potentially symbolic tensors in conditionals and breaks tracing.""" ) def __a ( self : List[Any] ): """simple docstring""" pass def __a ( self : List[str] ): """simple docstring""" pass def __SCREAMING_SNAKE_CASE ( __UpperCamelCase : List[str] ) -> Dict: """simple docstring""" return tf.constant(__UpperCamelCase , dtype=tf.intaa ) __lowerCamelCase : List[str] = 1e-4 @slow @require_tf class __snake_case ( unittest.TestCase ): def __a ( self : Tuple ): """simple docstring""" SCREAMING_SNAKE_CASE__ = TFLEDForConditionalGeneration.from_pretrained("""allenai/led-base-16384""" ).led # change to intended input here SCREAMING_SNAKE_CASE__ = _long_tensor([5_12 * [0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69]] ) SCREAMING_SNAKE_CASE__ = _long_tensor([1_28 * [0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69]] ) SCREAMING_SNAKE_CASE__ = prepare_led_inputs_dict(model.config , _lowercase , _lowercase ) SCREAMING_SNAKE_CASE__ = model(**_lowercase )[0] SCREAMING_SNAKE_CASE__ = (1, 10_24, 7_68) self.assertEqual(output.shape , _lowercase ) # change to expected output here SCREAMING_SNAKE_CASE__ = tf.convert_to_tensor( [[2.30_50, 2.82_79, 0.65_31], [-1.84_57, -0.14_55, -3.56_61], [-1.01_86, 0.45_86, -2.20_43]] , ) tf.debugging.assert_near(output[:, :3, :3] , _lowercase , atol=1E-3 ) def __a ( self : Optional[Any] ): """simple docstring""" SCREAMING_SNAKE_CASE__ = TFLEDForConditionalGeneration.from_pretrained("""allenai/led-base-16384""" ) # change to intended input here SCREAMING_SNAKE_CASE__ = _long_tensor([5_12 * [0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69]] ) SCREAMING_SNAKE_CASE__ = _long_tensor([1_28 * [0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69]] ) SCREAMING_SNAKE_CASE__ = prepare_led_inputs_dict(model.config , _lowercase , _lowercase ) SCREAMING_SNAKE_CASE__ = model(**_lowercase )[0] SCREAMING_SNAKE_CASE__ = (1, 10_24, model.config.vocab_size) self.assertEqual(output.shape , _lowercase ) # change to expected output here SCREAMING_SNAKE_CASE__ = tf.convert_to_tensor( [[33.65_07, 6.45_72, 16.80_89], [5.87_39, -2.42_38, 11.29_02], [-3.21_39, -4.31_49, 4.27_83]] , ) tf.debugging.assert_near(output[:, :3, :3] , _lowercase , atol=1E-3 , rtol=1E-3 )
204
1
from ..utils import DummyObject, requires_backends class a ( metaclass=__snake_case ): __lowerCAmelCase : List[str] = ["torch"] def __init__( self :Optional[int] ,*__lowercase :Tuple ,**__lowercase :Tuple ): requires_backends(self ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Dict ,*__lowercase :Optional[Any] ,**__lowercase :Union[str, Any] ): requires_backends(cls ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :List[str] ,*__lowercase :List[Any] ,**__lowercase :Optional[int] ): requires_backends(cls ,['''torch'''] ) class a ( metaclass=__snake_case ): __lowerCAmelCase : Union[str, Any] = ["torch"] def __init__( self :List[Any] ,*__lowercase :Any ,**__lowercase :Optional[Any] ): requires_backends(self ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Tuple ,*__lowercase :int ,**__lowercase :int ): requires_backends(cls ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Dict ,*__lowercase :Union[str, Any] ,**__lowercase :Tuple ): requires_backends(cls ,['''torch'''] ) class a ( metaclass=__snake_case ): __lowerCAmelCase : Union[str, Any] = ["torch"] def __init__( self :Any ,*__lowercase :Dict ,**__lowercase :int ): requires_backends(self ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Any ,*__lowercase :Any ,**__lowercase :int ): requires_backends(cls ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :List[Any] ,*__lowercase :Dict ,**__lowercase :str ): requires_backends(cls ,['''torch'''] ) class a ( metaclass=__snake_case ): __lowerCAmelCase : Optional[int] = ["torch"] def __init__( self :List[Any] ,*__lowercase :Dict ,**__lowercase :Dict ): requires_backends(self ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :str ,*__lowercase :Union[str, Any] ,**__lowercase :int ): requires_backends(cls ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Any ,*__lowercase :int ,**__lowercase :int ): requires_backends(cls ,['''torch'''] ) class a ( metaclass=__snake_case ): __lowerCAmelCase : Tuple = ["torch"] def __init__( self :Union[str, Any] ,*__lowercase :Optional[int] ,**__lowercase :int ): requires_backends(self ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Optional[int] ,*__lowercase :List[Any] ,**__lowercase :Optional[int] ): requires_backends(cls ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Any ,*__lowercase :Union[str, Any] ,**__lowercase :List[str] ): requires_backends(cls ,['''torch'''] ) class a ( metaclass=__snake_case ): __lowerCAmelCase : Optional[int] = ["torch"] def __init__( self :Dict ,*__lowercase :Optional[Any] ,**__lowercase :str ): requires_backends(self ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Optional[Any] ,*__lowercase :Optional[Any] ,**__lowercase :Optional[int] ): requires_backends(cls ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Tuple ,*__lowercase :List[str] ,**__lowercase :Optional[int] ): requires_backends(cls ,['''torch'''] ) class a ( metaclass=__snake_case ): __lowerCAmelCase : Union[str, Any] = ["torch"] def __init__( self :int ,*__lowercase :Union[str, Any] ,**__lowercase :Dict ): requires_backends(self ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Union[str, Any] ,*__lowercase :Tuple ,**__lowercase :Tuple ): requires_backends(cls ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :List[Any] ,*__lowercase :str ,**__lowercase :Any ): requires_backends(cls ,['''torch'''] ) class a ( metaclass=__snake_case ): __lowerCAmelCase : List[Any] = ["torch"] def __init__( self :Union[str, Any] ,*__lowercase :Any ,**__lowercase :Tuple ): requires_backends(self ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Optional[Any] ,*__lowercase :int ,**__lowercase :Any ): requires_backends(cls ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :str ,*__lowercase :str ,**__lowercase :List[Any] ): requires_backends(cls ,['''torch'''] ) class a ( metaclass=__snake_case ): __lowerCAmelCase : Dict = ["torch"] def __init__( self :Optional[Any] ,*__lowercase :Tuple ,**__lowercase :Dict ): requires_backends(self ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Optional[int] ,*__lowercase :str ,**__lowercase :Dict ): requires_backends(cls ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Dict ,*__lowercase :Dict ,**__lowercase :Union[str, Any] ): requires_backends(cls ,['''torch'''] ) class a ( metaclass=__snake_case ): __lowerCAmelCase : Any = ["torch"] def __init__( self :Optional[int] ,*__lowercase :Union[str, Any] ,**__lowercase :Dict ): requires_backends(self ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Dict ,*__lowercase :str ,**__lowercase :List[str] ): requires_backends(cls ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Optional[Any] ,*__lowercase :Dict ,**__lowercase :Optional[int] ): requires_backends(cls ,['''torch'''] ) class a ( metaclass=__snake_case ): __lowerCAmelCase : Optional[Any] = ["torch"] def __init__( self :Tuple ,*__lowercase :List[Any] ,**__lowercase :str ): requires_backends(self ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :str ,*__lowercase :List[str] ,**__lowercase :int ): requires_backends(cls ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Optional[Any] ,*__lowercase :Tuple ,**__lowercase :Any ): requires_backends(cls ,['''torch'''] ) def _lowerCAmelCase ( *__lowerCAmelCase , **__lowerCAmelCase ) -> Union[str, Any]: """simple docstring""" requires_backends(_a , ['''torch'''] ) def _lowerCAmelCase ( *__lowerCAmelCase , **__lowerCAmelCase ) -> Optional[Any]: """simple docstring""" requires_backends(_a , ['''torch'''] ) def _lowerCAmelCase ( *__lowerCAmelCase , **__lowerCAmelCase ) -> Union[str, Any]: """simple docstring""" requires_backends(_a , ['''torch'''] ) def _lowerCAmelCase ( *__lowerCAmelCase , **__lowerCAmelCase ) -> Dict: """simple docstring""" requires_backends(_a , ['''torch'''] ) def _lowerCAmelCase ( *__lowerCAmelCase , **__lowerCAmelCase ) -> Dict: """simple docstring""" requires_backends(_a , ['''torch'''] ) def _lowerCAmelCase ( *__lowerCAmelCase , **__lowerCAmelCase ) -> List[Any]: """simple docstring""" requires_backends(_a , ['''torch'''] ) def _lowerCAmelCase ( *__lowerCAmelCase , **__lowerCAmelCase ) -> List[Any]: """simple docstring""" requires_backends(_a , ['''torch'''] ) class a ( metaclass=__snake_case ): __lowerCAmelCase : List[Any] = ["torch"] def __init__( self :Optional[Any] ,*__lowercase :List[Any] ,**__lowercase :Optional[int] ): requires_backends(self ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Optional[int] ,*__lowercase :Tuple ,**__lowercase :Any ): requires_backends(cls ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :int ,*__lowercase :str ,**__lowercase :Optional[int] ): requires_backends(cls ,['''torch'''] ) class a ( metaclass=__snake_case ): __lowerCAmelCase : List[Any] = ["torch"] def __init__( self :List[Any] ,*__lowercase :Any ,**__lowercase :Optional[int] ): requires_backends(self ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Union[str, Any] ,*__lowercase :Optional[int] ,**__lowercase :Optional[Any] ): requires_backends(cls ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Tuple ,*__lowercase :Tuple ,**__lowercase :int ): requires_backends(cls ,['''torch'''] ) class a ( metaclass=__snake_case ): __lowerCAmelCase : Optional[Any] = ["torch"] def __init__( self :Tuple ,*__lowercase :List[Any] ,**__lowercase :List[str] ): requires_backends(self ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Tuple ,*__lowercase :Dict ,**__lowercase :Optional[int] ): requires_backends(cls ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :str ,*__lowercase :Dict ,**__lowercase :List[Any] ): requires_backends(cls ,['''torch'''] ) class a ( metaclass=__snake_case ): __lowerCAmelCase : Optional[Any] = ["torch"] def __init__( self :Union[str, Any] ,*__lowercase :Optional[Any] ,**__lowercase :Optional[Any] ): requires_backends(self ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Dict ,*__lowercase :List[str] ,**__lowercase :Tuple ): requires_backends(cls ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Optional[Any] ,*__lowercase :List[str] ,**__lowercase :Dict ): requires_backends(cls ,['''torch'''] ) class a ( metaclass=__snake_case ): __lowerCAmelCase : int = ["torch"] def __init__( self :Optional[Any] ,*__lowercase :int ,**__lowercase :int ): requires_backends(self ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :str ,*__lowercase :int ,**__lowercase :List[Any] ): requires_backends(cls ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Dict ,*__lowercase :Optional[int] ,**__lowercase :Tuple ): requires_backends(cls ,['''torch'''] ) class a ( metaclass=__snake_case ): __lowerCAmelCase : List[str] = ["torch"] def __init__( self :List[str] ,*__lowercase :Any ,**__lowercase :str ): requires_backends(self ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :List[str] ,*__lowercase :str ,**__lowercase :Any ): requires_backends(cls ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Dict ,*__lowercase :List[Any] ,**__lowercase :Dict ): requires_backends(cls ,['''torch'''] ) class a ( metaclass=__snake_case ): __lowerCAmelCase : Union[str, Any] = ["torch"] def __init__( self :List[Any] ,*__lowercase :str ,**__lowercase :Tuple ): requires_backends(self ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :List[str] ,*__lowercase :Union[str, Any] ,**__lowercase :str ): requires_backends(cls ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :str ,*__lowercase :Dict ,**__lowercase :List[str] ): requires_backends(cls ,['''torch'''] ) class a ( metaclass=__snake_case ): __lowerCAmelCase : Union[str, Any] = ["torch"] def __init__( self :Optional[int] ,*__lowercase :Tuple ,**__lowercase :Dict ): requires_backends(self ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :int ,*__lowercase :Tuple ,**__lowercase :List[str] ): requires_backends(cls ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :List[str] ,*__lowercase :Union[str, Any] ,**__lowercase :List[str] ): requires_backends(cls ,['''torch'''] ) class a ( metaclass=__snake_case ): __lowerCAmelCase : Tuple = ["torch"] def __init__( self :Any ,*__lowercase :Dict ,**__lowercase :Any ): requires_backends(self ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :str ,*__lowercase :Dict ,**__lowercase :int ): requires_backends(cls ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :str ,*__lowercase :Union[str, Any] ,**__lowercase :Optional[Any] ): requires_backends(cls ,['''torch'''] ) class a ( metaclass=__snake_case ): __lowerCAmelCase : Union[str, Any] = ["torch"] def __init__( self :int ,*__lowercase :List[str] ,**__lowercase :Tuple ): requires_backends(self ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Optional[int] ,*__lowercase :Any ,**__lowercase :Dict ): requires_backends(cls ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Optional[int] ,*__lowercase :Any ,**__lowercase :Optional[Any] ): requires_backends(cls ,['''torch'''] ) class a ( metaclass=__snake_case ): __lowerCAmelCase : List[str] = ["torch"] def __init__( self :str ,*__lowercase :Tuple ,**__lowercase :Union[str, Any] ): requires_backends(self ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Any ,*__lowercase :List[str] ,**__lowercase :List[str] ): requires_backends(cls ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Any ,*__lowercase :Any ,**__lowercase :List[str] ): requires_backends(cls ,['''torch'''] ) class a ( metaclass=__snake_case ): __lowerCAmelCase : List[str] = ["torch"] def __init__( self :Dict ,*__lowercase :List[str] ,**__lowercase :Dict ): requires_backends(self ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Dict ,*__lowercase :List[Any] ,**__lowercase :Dict ): requires_backends(cls ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :int ,*__lowercase :Union[str, Any] ,**__lowercase :Optional[Any] ): requires_backends(cls ,['''torch'''] ) class a ( metaclass=__snake_case ): __lowerCAmelCase : Dict = ["torch"] def __init__( self :str ,*__lowercase :int ,**__lowercase :Dict ): requires_backends(self ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Dict ,*__lowercase :Union[str, Any] ,**__lowercase :List[Any] ): requires_backends(cls ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Any ,*__lowercase :Union[str, Any] ,**__lowercase :List[Any] ): requires_backends(cls ,['''torch'''] ) class a ( metaclass=__snake_case ): __lowerCAmelCase : Optional[int] = ["torch"] def __init__( self :Optional[Any] ,*__lowercase :int ,**__lowercase :int ): requires_backends(self ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :List[Any] ,*__lowercase :Dict ,**__lowercase :Any ): requires_backends(cls ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Tuple ,*__lowercase :List[str] ,**__lowercase :int ): requires_backends(cls ,['''torch'''] ) class a ( metaclass=__snake_case ): __lowerCAmelCase : Tuple = ["torch"] def __init__( self :Tuple ,*__lowercase :int ,**__lowercase :Tuple ): requires_backends(self ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Dict ,*__lowercase :Tuple ,**__lowercase :Tuple ): requires_backends(cls ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :List[str] ,*__lowercase :Tuple ,**__lowercase :Any ): requires_backends(cls ,['''torch'''] ) class a ( metaclass=__snake_case ): __lowerCAmelCase : List[Any] = ["torch"] def __init__( self :List[Any] ,*__lowercase :List[Any] ,**__lowercase :Optional[int] ): requires_backends(self ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :int ,*__lowercase :Optional[Any] ,**__lowercase :Union[str, Any] ): requires_backends(cls ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Tuple ,*__lowercase :Optional[Any] ,**__lowercase :str ): requires_backends(cls ,['''torch'''] ) class a ( metaclass=__snake_case ): __lowerCAmelCase : Any = ["torch"] def __init__( self :List[Any] ,*__lowercase :str ,**__lowercase :Optional[Any] ): requires_backends(self ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Any ,*__lowercase :str ,**__lowercase :List[Any] ): requires_backends(cls ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :int ,*__lowercase :Tuple ,**__lowercase :Optional[int] ): requires_backends(cls ,['''torch'''] ) class a ( metaclass=__snake_case ): __lowerCAmelCase : Union[str, Any] = ["torch"] def __init__( self :Dict ,*__lowercase :Any ,**__lowercase :Optional[int] ): requires_backends(self ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Dict ,*__lowercase :str ,**__lowercase :Dict ): requires_backends(cls ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :List[Any] ,*__lowercase :int ,**__lowercase :int ): requires_backends(cls ,['''torch'''] ) class a ( metaclass=__snake_case ): __lowerCAmelCase : Tuple = ["torch"] def __init__( self :Dict ,*__lowercase :Optional[int] ,**__lowercase :Dict ): requires_backends(self ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Optional[int] ,*__lowercase :Dict ,**__lowercase :Union[str, Any] ): requires_backends(cls ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Any ,*__lowercase :List[Any] ,**__lowercase :Dict ): requires_backends(cls ,['''torch'''] ) class a ( metaclass=__snake_case ): __lowerCAmelCase : Union[str, Any] = ["torch"] def __init__( self :str ,*__lowercase :str ,**__lowercase :List[Any] ): requires_backends(self ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :List[str] ,*__lowercase :List[Any] ,**__lowercase :Dict ): requires_backends(cls ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :List[Any] ,*__lowercase :List[Any] ,**__lowercase :str ): requires_backends(cls ,['''torch'''] ) class a ( metaclass=__snake_case ): __lowerCAmelCase : int = ["torch"] def __init__( self :Optional[Any] ,*__lowercase :List[str] ,**__lowercase :int ): requires_backends(self ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :List[str] ,*__lowercase :Any ,**__lowercase :Optional[Any] ): requires_backends(cls ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Any ,*__lowercase :Optional[int] ,**__lowercase :int ): requires_backends(cls ,['''torch'''] ) class a ( metaclass=__snake_case ): __lowerCAmelCase : Union[str, Any] = ["torch"] def __init__( self :Tuple ,*__lowercase :List[str] ,**__lowercase :List[Any] ): requires_backends(self ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :List[str] ,*__lowercase :Union[str, Any] ,**__lowercase :Union[str, Any] ): requires_backends(cls ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :List[Any] ,*__lowercase :int ,**__lowercase :Optional[Any] ): requires_backends(cls ,['''torch'''] ) class a ( metaclass=__snake_case ): __lowerCAmelCase : str = ["torch"] def __init__( self :Optional[int] ,*__lowercase :Optional[Any] ,**__lowercase :Any ): requires_backends(self ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Dict ,*__lowercase :List[Any] ,**__lowercase :Any ): requires_backends(cls ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :List[Any] ,*__lowercase :Optional[int] ,**__lowercase :str ): requires_backends(cls ,['''torch'''] ) class a ( metaclass=__snake_case ): __lowerCAmelCase : List[str] = ["torch"] def __init__( self :Optional[Any] ,*__lowercase :List[Any] ,**__lowercase :str ): requires_backends(self ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Optional[Any] ,*__lowercase :Tuple ,**__lowercase :List[str] ): requires_backends(cls ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :List[str] ,*__lowercase :List[Any] ,**__lowercase :Optional[int] ): requires_backends(cls ,['''torch'''] ) class a ( metaclass=__snake_case ): __lowerCAmelCase : str = ["torch"] def __init__( self :List[str] ,*__lowercase :str ,**__lowercase :int ): requires_backends(self ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :int ,*__lowercase :int ,**__lowercase :Any ): requires_backends(cls ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :str ,*__lowercase :Union[str, Any] ,**__lowercase :Dict ): requires_backends(cls ,['''torch'''] ) class a ( metaclass=__snake_case ): __lowerCAmelCase : Tuple = ["torch"] def __init__( self :Union[str, Any] ,*__lowercase :Optional[Any] ,**__lowercase :Dict ): requires_backends(self ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :int ,*__lowercase :Tuple ,**__lowercase :List[str] ): requires_backends(cls ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Union[str, Any] ,*__lowercase :Union[str, Any] ,**__lowercase :Optional[int] ): requires_backends(cls ,['''torch'''] ) class a ( metaclass=__snake_case ): __lowerCAmelCase : Tuple = ["torch"] def __init__( self :Optional[int] ,*__lowercase :int ,**__lowercase :int ): requires_backends(self ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Optional[Any] ,*__lowercase :List[str] ,**__lowercase :Dict ): requires_backends(cls ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Any ,*__lowercase :Dict ,**__lowercase :int ): requires_backends(cls ,['''torch'''] ) class a ( metaclass=__snake_case ): __lowerCAmelCase : Dict = ["torch"] def __init__( self :Optional[Any] ,*__lowercase :List[str] ,**__lowercase :List[Any] ): requires_backends(self ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Union[str, Any] ,*__lowercase :int ,**__lowercase :Any ): requires_backends(cls ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Tuple ,*__lowercase :Dict ,**__lowercase :Optional[Any] ): requires_backends(cls ,['''torch'''] ) class a ( metaclass=__snake_case ): __lowerCAmelCase : str = ["torch"] def __init__( self :Optional[Any] ,*__lowercase :Union[str, Any] ,**__lowercase :Union[str, Any] ): requires_backends(self ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Union[str, Any] ,*__lowercase :Optional[int] ,**__lowercase :Optional[Any] ): requires_backends(cls ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :List[str] ,*__lowercase :List[str] ,**__lowercase :Dict ): requires_backends(cls ,['''torch'''] ) class a ( metaclass=__snake_case ): __lowerCAmelCase : str = ["torch"] def __init__( self :Union[str, Any] ,*__lowercase :Optional[Any] ,**__lowercase :int ): requires_backends(self ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :List[str] ,*__lowercase :str ,**__lowercase :Union[str, Any] ): requires_backends(cls ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Union[str, Any] ,*__lowercase :Any ,**__lowercase :Optional[Any] ): requires_backends(cls ,['''torch'''] ) class a ( metaclass=__snake_case ): __lowerCAmelCase : Dict = ["torch"] def __init__( self :Optional[int] ,*__lowercase :Tuple ,**__lowercase :Tuple ): requires_backends(self ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Tuple ,*__lowercase :Union[str, Any] ,**__lowercase :Dict ): requires_backends(cls ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Optional[int] ,*__lowercase :Any ,**__lowercase :Optional[int] ): requires_backends(cls ,['''torch'''] ) class a ( metaclass=__snake_case ): __lowerCAmelCase : Tuple = ["torch"] def __init__( self :Union[str, Any] ,*__lowercase :str ,**__lowercase :List[Any] ): requires_backends(self ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Any ,*__lowercase :Dict ,**__lowercase :Any ): requires_backends(cls ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :int ,*__lowercase :int ,**__lowercase :Any ): requires_backends(cls ,['''torch'''] ) class a ( metaclass=__snake_case ): __lowerCAmelCase : Tuple = ["torch"] def __init__( self :Union[str, Any] ,*__lowercase :Optional[Any] ,**__lowercase :Optional[Any] ): requires_backends(self ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Any ,*__lowercase :List[Any] ,**__lowercase :List[Any] ): requires_backends(cls ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :List[Any] ,*__lowercase :str ,**__lowercase :List[str] ): requires_backends(cls ,['''torch'''] ) class a ( metaclass=__snake_case ): __lowerCAmelCase : Any = ["torch"] def __init__( self :Tuple ,*__lowercase :Union[str, Any] ,**__lowercase :Dict ): requires_backends(self ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :List[str] ,*__lowercase :Tuple ,**__lowercase :Any ): requires_backends(cls ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Optional[Any] ,*__lowercase :Any ,**__lowercase :str ): requires_backends(cls ,['''torch'''] ) class a ( metaclass=__snake_case ): __lowerCAmelCase : List[str] = ["torch"] def __init__( self :List[str] ,*__lowercase :Optional[int] ,**__lowercase :Any ): requires_backends(self ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :str ,*__lowercase :List[str] ,**__lowercase :Tuple ): requires_backends(cls ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :List[Any] ,*__lowercase :Optional[Any] ,**__lowercase :Union[str, Any] ): requires_backends(cls ,['''torch'''] ) class a ( metaclass=__snake_case ): __lowerCAmelCase : Dict = ["torch"] def __init__( self :str ,*__lowercase :Optional[Any] ,**__lowercase :Union[str, Any] ): requires_backends(self ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :str ,*__lowercase :List[Any] ,**__lowercase :Any ): requires_backends(cls ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :str ,*__lowercase :List[Any] ,**__lowercase :int ): requires_backends(cls ,['''torch'''] ) class a ( metaclass=__snake_case ): __lowerCAmelCase : Optional[int] = ["torch"] def __init__( self :Optional[Any] ,*__lowercase :Any ,**__lowercase :Optional[Any] ): requires_backends(self ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :List[Any] ,*__lowercase :Any ,**__lowercase :Tuple ): requires_backends(cls ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :str ,*__lowercase :List[Any] ,**__lowercase :Any ): requires_backends(cls ,['''torch'''] ) class a ( metaclass=__snake_case ): __lowerCAmelCase : Optional[Any] = ["torch"] def __init__( self :Tuple ,*__lowercase :List[str] ,**__lowercase :Tuple ): requires_backends(self ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Any ,*__lowercase :Dict ,**__lowercase :int ): requires_backends(cls ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Any ,*__lowercase :Tuple ,**__lowercase :List[Any] ): requires_backends(cls ,['''torch'''] ) class a ( metaclass=__snake_case ): __lowerCAmelCase : List[str] = ["torch"] def __init__( self :Union[str, Any] ,*__lowercase :int ,**__lowercase :str ): requires_backends(self ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Optional[Any] ,*__lowercase :int ,**__lowercase :Optional[Any] ): requires_backends(cls ,['''torch'''] ) @classmethod def __lowerCamelCase ( cls :Optional[Any] ,*__lowercase :Optional[Any] ,**__lowercase :Dict ): requires_backends(cls ,['''torch'''] )
230
from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...onnx.utils import compute_effective_axis_dimension from ...utils import logging if TYPE_CHECKING: from ...processing_utils import ProcessorMixin from ...utils import TensorType UpperCamelCase_ = logging.get_logger(__name__) UpperCamelCase_ = { '''microsoft/layoutlmv3-base''': '''https://huggingface.co/microsoft/layoutlmv3-base/resolve/main/config.json''', } class _snake_case ( __snake_case ): '''simple docstring''' A__ : Optional[Any] = "layoutlmv3" def __init__( self: str ,lowerCamelCase_: Any=50265 ,lowerCamelCase_: int=768 ,lowerCamelCase_: Any=12 ,lowerCamelCase_: Any=12 ,lowerCamelCase_: List[Any]=3072 ,lowerCamelCase_: str="gelu" ,lowerCamelCase_: List[str]=0.1 ,lowerCamelCase_: Any=0.1 ,lowerCamelCase_: Tuple=512 ,lowerCamelCase_: Union[str, Any]=2 ,lowerCamelCase_: Dict=0.0_2 ,lowerCamelCase_: List[str]=1e-5 ,lowerCamelCase_: int=1 ,lowerCamelCase_: int=0 ,lowerCamelCase_: List[str]=2 ,lowerCamelCase_: Dict=1024 ,lowerCamelCase_: Tuple=128 ,lowerCamelCase_: Tuple=128 ,lowerCamelCase_: Dict=True ,lowerCamelCase_: Union[str, Any]=32 ,lowerCamelCase_: Union[str, Any]=128 ,lowerCamelCase_: Tuple=64 ,lowerCamelCase_: Tuple=256 ,lowerCamelCase_: List[str]=True ,lowerCamelCase_: Optional[int]=True ,lowerCamelCase_: Any=True ,lowerCamelCase_: Dict=224 ,lowerCamelCase_: Optional[int]=3 ,lowerCamelCase_: Optional[int]=16 ,lowerCamelCase_: Dict=None ,**lowerCamelCase_: str ,) -> List[Any]: super().__init__( vocab_size=lowerCamelCase_ ,hidden_size=lowerCamelCase_ ,num_hidden_layers=lowerCamelCase_ ,num_attention_heads=lowerCamelCase_ ,intermediate_size=lowerCamelCase_ ,hidden_act=lowerCamelCase_ ,hidden_dropout_prob=lowerCamelCase_ ,attention_probs_dropout_prob=lowerCamelCase_ ,max_position_embeddings=lowerCamelCase_ ,type_vocab_size=lowerCamelCase_ ,initializer_range=lowerCamelCase_ ,layer_norm_eps=lowerCamelCase_ ,pad_token_id=lowerCamelCase_ ,bos_token_id=lowerCamelCase_ ,eos_token_id=lowerCamelCase_ ,**lowerCamelCase_ ,) UpperCAmelCase_ : List[Any] = max_ad_position_embeddings UpperCAmelCase_ : Optional[int] = coordinate_size UpperCAmelCase_ : Optional[int] = shape_size UpperCAmelCase_ : Optional[Any] = has_relative_attention_bias UpperCAmelCase_ : Optional[int] = rel_pos_bins UpperCAmelCase_ : Union[str, Any] = max_rel_pos UpperCAmelCase_ : Dict = has_spatial_attention_bias UpperCAmelCase_ : Optional[int] = rel_ad_pos_bins UpperCAmelCase_ : Tuple = max_rel_ad_pos UpperCAmelCase_ : Union[str, Any] = text_embed UpperCAmelCase_ : Optional[Any] = visual_embed UpperCAmelCase_ : List[str] = input_size UpperCAmelCase_ : str = num_channels UpperCAmelCase_ : Optional[int] = patch_size UpperCAmelCase_ : Tuple = classifier_dropout class _snake_case ( __snake_case ): '''simple docstring''' A__ : Optional[Any] = version.parse("1.12" ) @property def A__ ( self: Dict ) -> Mapping[str, Mapping[int, str]]: # The order of inputs is different for question answering and sequence classification if self.task in ["question-answering", "sequence-classification"]: return OrderedDict( [ ("""input_ids""", {0: """batch""", 1: """sequence"""}), ("""attention_mask""", {0: """batch""", 1: """sequence"""}), ("""bbox""", {0: """batch""", 1: """sequence"""}), ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}), ] ) else: return OrderedDict( [ ("""input_ids""", {0: """batch""", 1: """sequence"""}), ("""bbox""", {0: """batch""", 1: """sequence"""}), ("""attention_mask""", {0: """batch""", 1: """sequence"""}), ("""pixel_values""", {0: """batch""", 1: """num_channels"""}), ] ) @property def A__ ( self: Any ) -> float: return 1e-5 @property def A__ ( self: int ) -> int: return 12 def A__ ( self: List[str] ,lowerCamelCase_: "ProcessorMixin" ,lowerCamelCase_: int = -1 ,lowerCamelCase_: int = -1 ,lowerCamelCase_: bool = False ,lowerCamelCase_: Optional["TensorType"] = None ,lowerCamelCase_: int = 3 ,lowerCamelCase_: int = 40 ,lowerCamelCase_: int = 40 ,) -> Mapping[str, Any]: setattr(processor.image_processor ,"""apply_ocr""" ,lowerCamelCase_ ) # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX UpperCAmelCase_ : List[str] = compute_effective_axis_dimension( lowerCamelCase_ ,fixed_dimension=OnnxConfig.default_fixed_batch ,num_token_to_add=0 ) # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX UpperCAmelCase_ : int = processor.tokenizer.num_special_tokens_to_add(lowerCamelCase_ ) UpperCAmelCase_ : int = compute_effective_axis_dimension( lowerCamelCase_ ,fixed_dimension=OnnxConfig.default_fixed_sequence ,num_token_to_add=lowerCamelCase_ ) # Generate dummy inputs according to compute batch and sequence UpperCAmelCase_ : Optional[int] = [[""" """.join([processor.tokenizer.unk_token] ) * seq_length]] * batch_size # Generate dummy bounding boxes UpperCAmelCase_ : List[Any] = [[[48, 84, 73, 128]]] * batch_size # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX # batch_size = compute_effective_axis_dimension(batch_size, fixed_dimension=OnnxConfig.default_fixed_batch) UpperCAmelCase_ : Any = self._generate_dummy_images(lowerCamelCase_ ,lowerCamelCase_ ,lowerCamelCase_ ,lowerCamelCase_ ) UpperCAmelCase_ : Optional[Any] = dict( processor( lowerCamelCase_ ,text=lowerCamelCase_ ,boxes=lowerCamelCase_ ,return_tensors=lowerCamelCase_ ,) ) return inputs
345
0
"""simple docstring""" from __future__ import annotations from random import random from typing import Generic, TypeVar _lowerCAmelCase : Union[str, Any] = TypeVar('''KT''') _lowerCAmelCase : Union[str, Any] = TypeVar('''VT''') class A_ ( Generic[KT, VT] ): def __init__( self: int ,__lowerCAmelCase: KT | str = "root" ,__lowerCAmelCase: VT | None = None ): '''simple docstring''' _lowerCamelCase : Optional[int] = key _lowerCamelCase : int = value _lowerCamelCase : list[Node[KT, VT]] = [] def __repr__( self: Dict ): '''simple docstring''' return F"""Node({self.key}: {self.value})""" @property def _lowercase ( self: Optional[Any] ): '''simple docstring''' return len(self.forward ) class A_ ( Generic[KT, VT] ): def __init__( self: str ,__lowerCAmelCase: float = 0.5 ,__lowerCAmelCase: int = 16 ): '''simple docstring''' _lowerCamelCase : Node[KT, VT] = Node[KT, VT]() _lowerCamelCase : List[str] = 0 _lowerCamelCase : Tuple = p _lowerCamelCase : Optional[Any] = max_level def __str__( self: str ): '''simple docstring''' _lowerCamelCase : Tuple = list(self ) if len(__lowerCAmelCase ) == 0: return F"""SkipList(level={self.level})""" _lowerCamelCase : Optional[int] = max((len(str(__lowerCAmelCase ) ) for item in items) ,default=4 ) _lowerCamelCase : Any = max(__lowerCAmelCase ,4 ) + 4 _lowerCamelCase : int = self.head _lowerCamelCase : Any = [] _lowerCamelCase : str = node.forward.copy() lines.append(F"""[{node.key}]""".ljust(__lowerCAmelCase ,"-" ) + "* " * len(__lowerCAmelCase ) ) lines.append(" " * label_size + "| " * len(__lowerCAmelCase ) ) while len(node.forward ) != 0: _lowerCamelCase : List[Any] = node.forward[0] lines.append( F"""[{node.key}]""".ljust(__lowerCAmelCase ,"-" ) + " ".join(str(n.key ) if n.key == node.key else "|" for n in forwards ) ) lines.append(" " * label_size + "| " * len(__lowerCAmelCase ) ) _lowerCamelCase : int = node.forward lines.append("None".ljust(__lowerCAmelCase ) + "* " * len(__lowerCAmelCase ) ) return F"""SkipList(level={self.level})\n""" + "\n".join(__lowerCAmelCase ) def __iter__( self: Optional[int] ): '''simple docstring''' _lowerCamelCase : List[str] = self.head while len(node.forward ) != 0: yield node.forward[0].key _lowerCamelCase : Optional[int] = node.forward[0] def _lowercase ( self: List[Any] ): '''simple docstring''' _lowerCamelCase : Union[str, Any] = 1 while random() < self.p and level < self.max_level: level += 1 return level def _lowercase ( self: List[Any] ,__lowerCAmelCase: Dict ): '''simple docstring''' _lowerCamelCase : List[str] = [] _lowerCamelCase : int = self.head for i in reversed(range(self.level ) ): # i < node.level - When node level is lesser than `i` decrement `i`. # node.forward[i].key < key - Jumping to node with key value higher # or equal to searched key would result # in skipping searched key. while i < node.level and node.forward[i].key < key: _lowerCamelCase : Optional[int] = node.forward[i] # Each leftmost node (relative to searched node) will potentially have to # be updated. update_vector.append(__lowerCAmelCase ) update_vector.reverse() # Note that we were inserting values in reverse order. # len(node.forward) != 0 - If current node doesn't contain any further # references then searched key is not present. # node.forward[0].key == key - Next node key should be equal to search key # if key is present. if len(node.forward ) != 0 and node.forward[0].key == key: return node.forward[0], update_vector else: return None, update_vector def _lowercase ( self: Union[str, Any] ,__lowerCAmelCase: KT ): '''simple docstring''' _lowerCamelCase, _lowerCamelCase : List[str] = self._locate_node(__lowerCAmelCase ) if node is not None: for i, update_node in enumerate(__lowerCAmelCase ): # Remove or replace all references to removed node. if update_node.level > i and update_node.forward[i].key == key: if node.level > i: _lowerCamelCase : int = node.forward[i] else: _lowerCamelCase : Optional[int] = update_node.forward[:i] def _lowercase ( self: Tuple ,__lowerCAmelCase: KT ,__lowerCAmelCase: VT ): '''simple docstring''' _lowerCamelCase, _lowerCamelCase : int = self._locate_node(__lowerCAmelCase ) if node is not None: _lowerCamelCase : int = value else: _lowerCamelCase : Optional[int] = self.random_level() if level > self.level: # After level increase we have to add additional nodes to head. for _ in range(self.level - 1 ,__lowerCAmelCase ): update_vector.append(self.head ) _lowerCamelCase : Any = level _lowerCamelCase : List[str] = Node(__lowerCAmelCase ,__lowerCAmelCase ) for i, update_node in enumerate(update_vector[:level] ): # Change references to pass through new node. if update_node.level > i: new_node.forward.append(update_node.forward[i] ) if update_node.level < i + 1: update_node.forward.append(__lowerCAmelCase ) else: _lowerCamelCase : Optional[int] = new_node def _lowercase ( self: Tuple ,__lowerCAmelCase: VT ): '''simple docstring''' _lowerCamelCase, _lowerCamelCase : Optional[Any] = self._locate_node(__lowerCAmelCase ) if node is not None: return node.value return None def lowerCamelCase_( ) -> int: '''simple docstring''' _lowerCamelCase : Union[str, Any] = SkipList() skip_list.insert("Key1" , 3 ) skip_list.insert("Key2" , 12 ) skip_list.insert("Key3" , 41 ) skip_list.insert("Key4" , -19 ) _lowerCamelCase : Union[str, Any] = skip_list.head _lowerCamelCase : List[Any] = {} while node.level != 0: _lowerCamelCase : Tuple = node.forward[0] _lowerCamelCase : Optional[Any] = node.value assert len(_lowerCamelCase ) == 4 assert all_values["Key1"] == 3 assert all_values["Key2"] == 12 assert all_values["Key3"] == 41 assert all_values["Key4"] == -19 def lowerCamelCase_( ) -> List[str]: '''simple docstring''' _lowerCamelCase : Optional[int] = SkipList() skip_list.insert("Key1" , 10 ) skip_list.insert("Key1" , 12 ) skip_list.insert("Key5" , 7 ) skip_list.insert("Key7" , 10 ) skip_list.insert("Key10" , 5 ) skip_list.insert("Key7" , 7 ) skip_list.insert("Key5" , 5 ) skip_list.insert("Key10" , 10 ) _lowerCamelCase : List[str] = skip_list.head _lowerCamelCase : Dict = {} while node.level != 0: _lowerCamelCase : Union[str, Any] = node.forward[0] _lowerCamelCase : Tuple = node.value if len(_lowerCamelCase ) != 4: print() assert len(_lowerCamelCase ) == 4 assert all_values["Key1"] == 12 assert all_values["Key7"] == 7 assert all_values["Key5"] == 5 assert all_values["Key10"] == 10 def lowerCamelCase_( ) -> List[Any]: '''simple docstring''' _lowerCamelCase : Dict = SkipList() assert skip_list.find("Some key" ) is None def lowerCamelCase_( ) -> str: '''simple docstring''' _lowerCamelCase : List[str] = SkipList() skip_list.insert("Key2" , 20 ) assert skip_list.find("Key2" ) == 20 skip_list.insert("Some Key" , 10 ) skip_list.insert("Key2" , 8 ) skip_list.insert("V" , 13 ) assert skip_list.find("Y" ) is None assert skip_list.find("Key2" ) == 8 assert skip_list.find("Some Key" ) == 10 assert skip_list.find("V" ) == 13 def lowerCamelCase_( ) -> List[str]: '''simple docstring''' _lowerCamelCase : Union[str, Any] = SkipList() skip_list.delete("Some key" ) assert len(skip_list.head.forward ) == 0 def lowerCamelCase_( ) -> List[str]: '''simple docstring''' _lowerCamelCase : Any = SkipList() skip_list.insert("Key1" , 12 ) skip_list.insert("V" , 13 ) skip_list.insert("X" , 14 ) skip_list.insert("Key2" , 15 ) skip_list.delete("V" ) skip_list.delete("Key2" ) assert skip_list.find("V" ) is None assert skip_list.find("Key2" ) is None def lowerCamelCase_( ) -> Optional[Any]: '''simple docstring''' _lowerCamelCase : Any = SkipList() skip_list.insert("Key1" , 12 ) skip_list.insert("V" , 13 ) skip_list.insert("X" , 14 ) skip_list.insert("Key2" , 15 ) skip_list.delete("V" ) assert skip_list.find("V" ) is None assert skip_list.find("X" ) == 14 assert skip_list.find("Key1" ) == 12 assert skip_list.find("Key2" ) == 15 skip_list.delete("X" ) assert skip_list.find("V" ) is None assert skip_list.find("X" ) is None assert skip_list.find("Key1" ) == 12 assert skip_list.find("Key2" ) == 15 skip_list.delete("Key1" ) assert skip_list.find("V" ) is None assert skip_list.find("X" ) is None assert skip_list.find("Key1" ) is None assert skip_list.find("Key2" ) == 15 skip_list.delete("Key2" ) assert skip_list.find("V" ) is None assert skip_list.find("X" ) is None assert skip_list.find("Key1" ) is None assert skip_list.find("Key2" ) is None def lowerCamelCase_( ) -> Tuple: '''simple docstring''' _lowerCamelCase : Dict = SkipList() skip_list.insert("Key1" , 12 ) skip_list.insert("V" , 13 ) skip_list.insert("X" , 142 ) skip_list.insert("Key2" , 15 ) skip_list.delete("X" ) def traverse_keys(_lowerCamelCase ): yield node.key for forward_node in node.forward: yield from traverse_keys(_lowerCamelCase ) assert len(set(traverse_keys(skip_list.head ) ) ) == 4 def lowerCamelCase_( ) -> Any: '''simple docstring''' def is_sorted(_lowerCamelCase ): return all(next_item >= item for item, next_item in zip(_lowerCamelCase , lst[1:] ) ) _lowerCamelCase : Tuple = SkipList() for i in range(10 ): skip_list.insert(_lowerCamelCase , _lowerCamelCase ) assert is_sorted(list(_lowerCamelCase ) ) skip_list.delete(5 ) skip_list.delete(8 ) skip_list.delete(2 ) assert is_sorted(list(_lowerCamelCase ) ) skip_list.insert(-12 , -12 ) skip_list.insert(77 , 77 ) assert is_sorted(list(_lowerCamelCase ) ) def lowerCamelCase_( ) -> List[Any]: '''simple docstring''' for _ in range(100 ): # Repeat test 100 times due to the probabilistic nature of skip list # random values == random bugs test_insert() test_insert_overrides_existing_value() test_searching_empty_list_returns_none() test_search() test_deleting_item_from_empty_list_do_nothing() test_deleted_items_are_not_founded_by_find_method() test_delete_removes_only_given_key() test_delete_doesnt_leave_dead_nodes() test_iter_always_yields_sorted_values() def lowerCamelCase_( ) -> Tuple: '''simple docstring''' _lowerCamelCase : Tuple = SkipList() skip_list.insert(2 , "2" ) skip_list.insert(4 , "4" ) skip_list.insert(6 , "4" ) skip_list.insert(4 , "5" ) skip_list.insert(8 , "4" ) skip_list.insert(9 , "4" ) skip_list.delete(4 ) print(_lowerCamelCase ) if __name__ == "__main__": import doctest doctest.testmod() main()
340
"""simple docstring""" def lowerCamelCase_( _lowerCamelCase , _lowerCamelCase ) -> str: '''simple docstring''' _lowerCamelCase : int = len(_lowerCamelCase ) _lowerCamelCase : int = len(_lowerCamelCase ) _lowerCamelCase : int = ( first_str_length if first_str_length > second_str_length else second_str_length ) _lowerCamelCase : list = [] for char_count in range(_lowerCamelCase ): if char_count < first_str_length: output_list.append(first_str[char_count] ) if char_count < second_str_length: output_list.append(second_str[char_count] ) return "".join(_lowerCamelCase ) if __name__ == "__main__": print(alternative_string_arrange('''AB''', '''XYZ'''), end=''' ''')
340
1
import unittest from transformers import DebertaConfig, is_torch_available from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( DebertaForMaskedLM, DebertaForQuestionAnswering, DebertaForSequenceClassification, DebertaForTokenClassification, DebertaModel, ) from transformers.models.deberta.modeling_deberta import DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST class _A ( __UpperCAmelCase ): def __init__( self : List[str] , __SCREAMING_SNAKE_CASE : Union[str, Any] , __SCREAMING_SNAKE_CASE : Optional[Any]=13 , __SCREAMING_SNAKE_CASE : Tuple=7 , __SCREAMING_SNAKE_CASE : List[str]=True , __SCREAMING_SNAKE_CASE : List[Any]=True , __SCREAMING_SNAKE_CASE : Dict=True , __SCREAMING_SNAKE_CASE : List[Any]=True , __SCREAMING_SNAKE_CASE : str=99 , __SCREAMING_SNAKE_CASE : List[str]=32 , __SCREAMING_SNAKE_CASE : Tuple=5 , __SCREAMING_SNAKE_CASE : List[Any]=4 , __SCREAMING_SNAKE_CASE : int=37 , __SCREAMING_SNAKE_CASE : List[Any]="gelu" , __SCREAMING_SNAKE_CASE : List[Any]=0.1 , __SCREAMING_SNAKE_CASE : List[str]=0.1 , __SCREAMING_SNAKE_CASE : Optional[int]=512 , __SCREAMING_SNAKE_CASE : str=16 , __SCREAMING_SNAKE_CASE : List[Any]=2 , __SCREAMING_SNAKE_CASE : Optional[int]=0.02 , __SCREAMING_SNAKE_CASE : str=False , __SCREAMING_SNAKE_CASE : str=True , __SCREAMING_SNAKE_CASE : Tuple="None" , __SCREAMING_SNAKE_CASE : Union[str, Any]=3 , __SCREAMING_SNAKE_CASE : Any=4 , __SCREAMING_SNAKE_CASE : Optional[Any]=None , ): '''simple docstring''' __a = parent __a = batch_size __a = seq_length __a = is_training __a = use_input_mask __a = use_token_type_ids __a = use_labels __a = vocab_size __a = hidden_size __a = num_hidden_layers __a = num_attention_heads __a = intermediate_size __a = hidden_act __a = hidden_dropout_prob __a = attention_probs_dropout_prob __a = max_position_embeddings __a = type_vocab_size __a = type_sequence_label_size __a = initializer_range __a = num_labels __a = num_choices __a = relative_attention __a = position_biased_input __a = pos_att_type __a = scope def _lowerCamelCase ( self : List[str]): '''simple docstring''' __a = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size) __a = None if self.use_input_mask: __a = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2) __a = None if self.use_token_type_ids: __a = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size) __a = None __a = None __a = None if self.use_labels: __a = ids_tensor([self.batch_size] , self.type_sequence_label_size) __a = ids_tensor([self.batch_size, self.seq_length] , self.num_labels) __a = ids_tensor([self.batch_size] , self.num_choices) __a = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def _lowerCamelCase ( self : Union[str, Any]): '''simple docstring''' return DebertaConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , relative_attention=self.relative_attention , position_biased_input=self.position_biased_input , pos_att_type=self.pos_att_type , ) def _lowerCamelCase ( self : Union[str, Any]): '''simple docstring''' __a = self.get_config() __a = 300 return config def _lowerCamelCase ( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : Tuple): '''simple docstring''' self.parent.assertListEqual(list(result.loss.size()) , []) def _lowerCamelCase ( self : Optional[int] , __SCREAMING_SNAKE_CASE : Tuple , __SCREAMING_SNAKE_CASE : List[Any] , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : Union[str, Any] , __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : List[str] , __SCREAMING_SNAKE_CASE : Union[str, Any]): '''simple docstring''' __a = DebertaModel(config=__SCREAMING_SNAKE_CASE) model.to(__SCREAMING_SNAKE_CASE) model.eval() __a = model(__SCREAMING_SNAKE_CASE , attention_mask=__SCREAMING_SNAKE_CASE , token_type_ids=__SCREAMING_SNAKE_CASE)[0] __a = model(__SCREAMING_SNAKE_CASE , token_type_ids=__SCREAMING_SNAKE_CASE)[0] __a = model(__SCREAMING_SNAKE_CASE)[0] self.parent.assertListEqual(list(sequence_output.size()) , [self.batch_size, self.seq_length, self.hidden_size]) def _lowerCamelCase ( self : Optional[int] , __SCREAMING_SNAKE_CASE : Dict , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : Tuple , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : Tuple , __SCREAMING_SNAKE_CASE : Any , __SCREAMING_SNAKE_CASE : List[Any]): '''simple docstring''' __a = DebertaForMaskedLM(config=__SCREAMING_SNAKE_CASE) model.to(__SCREAMING_SNAKE_CASE) model.eval() __a = model(__SCREAMING_SNAKE_CASE , attention_mask=__SCREAMING_SNAKE_CASE , token_type_ids=__SCREAMING_SNAKE_CASE , labels=__SCREAMING_SNAKE_CASE) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size)) def _lowerCamelCase ( self : Any , __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : Optional[int] , __SCREAMING_SNAKE_CASE : Optional[int] , __SCREAMING_SNAKE_CASE : List[Any] , __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : Any): '''simple docstring''' __a = self.num_labels __a = DebertaForSequenceClassification(__SCREAMING_SNAKE_CASE) model.to(__SCREAMING_SNAKE_CASE) model.eval() __a = model(__SCREAMING_SNAKE_CASE , attention_mask=__SCREAMING_SNAKE_CASE , token_type_ids=__SCREAMING_SNAKE_CASE , labels=__SCREAMING_SNAKE_CASE) self.parent.assertListEqual(list(result.logits.size()) , [self.batch_size, self.num_labels]) self.check_loss_output(__SCREAMING_SNAKE_CASE) def _lowerCamelCase ( self : Optional[int] , __SCREAMING_SNAKE_CASE : Dict , __SCREAMING_SNAKE_CASE : Any , __SCREAMING_SNAKE_CASE : Optional[int] , __SCREAMING_SNAKE_CASE : List[Any] , __SCREAMING_SNAKE_CASE : Union[str, Any] , __SCREAMING_SNAKE_CASE : List[str] , __SCREAMING_SNAKE_CASE : Optional[int]): '''simple docstring''' __a = self.num_labels __a = DebertaForTokenClassification(config=__SCREAMING_SNAKE_CASE) model.to(__SCREAMING_SNAKE_CASE) model.eval() __a = model(__SCREAMING_SNAKE_CASE , attention_mask=__SCREAMING_SNAKE_CASE , token_type_ids=__SCREAMING_SNAKE_CASE , labels=__SCREAMING_SNAKE_CASE) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels)) def _lowerCamelCase ( self : str , __SCREAMING_SNAKE_CASE : Dict , __SCREAMING_SNAKE_CASE : List[str] , __SCREAMING_SNAKE_CASE : Optional[int] , __SCREAMING_SNAKE_CASE : Any , __SCREAMING_SNAKE_CASE : Optional[int] , __SCREAMING_SNAKE_CASE : Dict , __SCREAMING_SNAKE_CASE : Optional[int]): '''simple docstring''' __a = DebertaForQuestionAnswering(config=__SCREAMING_SNAKE_CASE) model.to(__SCREAMING_SNAKE_CASE) model.eval() __a = model( __SCREAMING_SNAKE_CASE , attention_mask=__SCREAMING_SNAKE_CASE , token_type_ids=__SCREAMING_SNAKE_CASE , start_positions=__SCREAMING_SNAKE_CASE , end_positions=__SCREAMING_SNAKE_CASE , ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length)) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length)) def _lowerCamelCase ( self : Tuple): '''simple docstring''' __a = self.prepare_config_and_inputs() ( ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ( __a ) , ) = config_and_inputs __a = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_torch class _A ( __UpperCAmelCase ,__UpperCAmelCase ,unittest.TestCase ): UpperCamelCase__ : int = ( ( DebertaModel, DebertaForMaskedLM, DebertaForSequenceClassification, DebertaForTokenClassification, DebertaForQuestionAnswering, ) if is_torch_available() else () ) UpperCamelCase__ : List[str] = ( { '''feature-extraction''': DebertaModel, '''fill-mask''': DebertaForMaskedLM, '''question-answering''': DebertaForQuestionAnswering, '''text-classification''': DebertaForSequenceClassification, '''token-classification''': DebertaForTokenClassification, '''zero-shot''': DebertaForSequenceClassification, } if is_torch_available() else {} ) UpperCamelCase__ : Union[str, Any] = True UpperCamelCase__ : Any = False UpperCamelCase__ : Any = False UpperCamelCase__ : Dict = False UpperCamelCase__ : Tuple = False def _lowerCamelCase ( self : Tuple): '''simple docstring''' __a = DebertaModelTester(self) __a = ConfigTester(self , config_class=__SCREAMING_SNAKE_CASE , hidden_size=37) def _lowerCamelCase ( self : List[str]): '''simple docstring''' self.config_tester.run_common_tests() def _lowerCamelCase ( self : Tuple): '''simple docstring''' __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_deberta_model(*__SCREAMING_SNAKE_CASE) def _lowerCamelCase ( self : Tuple): '''simple docstring''' __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_deberta_for_sequence_classification(*__SCREAMING_SNAKE_CASE) def _lowerCamelCase ( self : Any): '''simple docstring''' __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_deberta_for_masked_lm(*__SCREAMING_SNAKE_CASE) def _lowerCamelCase ( self : Dict): '''simple docstring''' __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_deberta_for_question_answering(*__SCREAMING_SNAKE_CASE) def _lowerCamelCase ( self : Any): '''simple docstring''' __a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_deberta_for_token_classification(*__SCREAMING_SNAKE_CASE) @slow def _lowerCamelCase ( self : Tuple): '''simple docstring''' for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __a = DebertaModel.from_pretrained(__SCREAMING_SNAKE_CASE) self.assertIsNotNone(__SCREAMING_SNAKE_CASE) @require_torch @require_sentencepiece @require_tokenizers class _A ( unittest.TestCase ): @unittest.skip(reason='''Model not available yet''') def _lowerCamelCase ( self : int): '''simple docstring''' pass @slow def _lowerCamelCase ( self : int): '''simple docstring''' __a = DebertaModel.from_pretrained('''microsoft/deberta-base''') __a = torch.tensor([[0, 31_414, 232, 328, 740, 1_140, 12_695, 69, 46_078, 1_588, 2]]) __a = torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]) with torch.no_grad(): __a = model(__SCREAMING_SNAKE_CASE , attention_mask=__SCREAMING_SNAKE_CASE)[0] # compare the actual values for a slice. __a = torch.tensor( [[[-0.59_86, -0.80_55, -0.84_62], [1.44_84, -0.93_48, -0.80_59], [0.31_23, 0.00_32, -1.41_31]]]) self.assertTrue(torch.allclose(output[:, 1:4, 1:4] , __SCREAMING_SNAKE_CASE , atol=1E-4) , F'{output[:, 1:4, 1:4]}')
49
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import cached_download, hf_hub_url from PIL import Image from transformers import DPTConfig, DPTForDepthEstimation, DPTForSemanticSegmentation, DPTImageProcessor from transformers.utils import logging logging.set_verbosity_info() _snake_case : int = logging.get_logger(__name__) def lowerCAmelCase_ ( __lowerCamelCase ): __snake_case : Any = DPTConfig(embedding_type="hybrid" ) if "large" in checkpoint_url: __snake_case : Optional[int] = 1_0_2_4 __snake_case : List[Any] = 4_0_9_6 __snake_case : List[Any] = 2_4 __snake_case : Optional[Any] = 1_6 __snake_case : str = [5, 1_1, 1_7, 2_3] __snake_case : List[str] = [2_5_6, 5_1_2, 1_0_2_4, 1_0_2_4] __snake_case : Union[str, Any] = (1, 3_8_4, 3_8_4) if "nyu" or "midas" in checkpoint_url: __snake_case : Tuple = 7_6_8 __snake_case : Any = [1, 1, 1, 0.5] __snake_case : Any = [2_5_6, 5_1_2, 7_6_8, 7_6_8] __snake_case : Any = 1_5_0 __snake_case : Optional[Any] = 1_6 __snake_case : List[str] = (1, 3_8_4, 3_8_4) __snake_case : Tuple = False __snake_case : Optional[Any] = "project" if "ade" in checkpoint_url: __snake_case : Optional[int] = True __snake_case : List[str] = 7_6_8 __snake_case : int = [1, 1, 1, 0.5] __snake_case : Any = 1_5_0 __snake_case : Tuple = 1_6 __snake_case : List[str] = "huggingface/label-files" __snake_case : Union[str, Any] = "ade20k-id2label.json" __snake_case : List[str] = json.load(open(cached_download(hf_hub_url(__lowerCamelCase , __lowerCamelCase , repo_type="dataset" ) ) , "r" ) ) __snake_case : int = {int(__lowerCamelCase ): v for k, v in idalabel.items()} __snake_case : Optional[Any] = idalabel __snake_case : str = {v: k for k, v in idalabel.items()} __snake_case : Tuple = [1, 1_5_0, 4_8_0, 4_8_0] return config, expected_shape def lowerCAmelCase_ ( __lowerCamelCase ): __snake_case : Tuple = ["pretrained.model.head.weight", "pretrained.model.head.bias"] for k in ignore_keys: state_dict.pop(__lowerCamelCase , __lowerCamelCase ) def lowerCAmelCase_ ( __lowerCamelCase ): if ( "pretrained.model" in name and "cls_token" not in name and "pos_embed" not in name and "patch_embed" not in name ): __snake_case : Tuple = name.replace("pretrained.model" , "dpt.encoder" ) if "pretrained.model" in name: __snake_case : Tuple = name.replace("pretrained.model" , "dpt.embeddings" ) if "patch_embed" in name: __snake_case : Optional[Any] = name.replace("patch_embed" , "" ) if "pos_embed" in name: __snake_case : Optional[int] = name.replace("pos_embed" , "position_embeddings" ) if "attn.proj" in name: __snake_case : List[str] = name.replace("attn.proj" , "attention.output.dense" ) if "proj" in name and "project" not in name: __snake_case : Union[str, Any] = name.replace("proj" , "projection" ) if "blocks" in name: __snake_case : int = name.replace("blocks" , "layer" ) if "mlp.fc1" in name: __snake_case : Tuple = name.replace("mlp.fc1" , "intermediate.dense" ) if "mlp.fc2" in name: __snake_case : Any = name.replace("mlp.fc2" , "output.dense" ) if "norm1" in name and "backbone" not in name: __snake_case : Optional[Any] = name.replace("norm1" , "layernorm_before" ) if "norm2" in name and "backbone" not in name: __snake_case : Any = name.replace("norm2" , "layernorm_after" ) if "scratch.output_conv" in name: __snake_case : Dict = name.replace("scratch.output_conv" , "head" ) if "scratch" in name: __snake_case : Union[str, Any] = name.replace("scratch" , "neck" ) if "layer1_rn" in name: __snake_case : List[Any] = name.replace("layer1_rn" , "convs.0" ) if "layer2_rn" in name: __snake_case : str = name.replace("layer2_rn" , "convs.1" ) if "layer3_rn" in name: __snake_case : List[str] = name.replace("layer3_rn" , "convs.2" ) if "layer4_rn" in name: __snake_case : Optional[int] = name.replace("layer4_rn" , "convs.3" ) if "refinenet" in name: __snake_case : Optional[int] = int(name[len("neck.refinenet" ) : len("neck.refinenet" ) + 1] ) # tricky here: we need to map 4 to 0, 3 to 1, 2 to 2 and 1 to 3 __snake_case : int = name.replace(F'refinenet{layer_idx}' , F'fusion_stage.layers.{abs(layer_idx-4 )}' ) if "out_conv" in name: __snake_case : Any = name.replace("out_conv" , "projection" ) if "resConfUnit1" in name: __snake_case : List[Any] = name.replace("resConfUnit1" , "residual_layer1" ) if "resConfUnit2" in name: __snake_case : Tuple = name.replace("resConfUnit2" , "residual_layer2" ) if "conv1" in name: __snake_case : List[str] = name.replace("conv1" , "convolution1" ) if "conv2" in name: __snake_case : str = name.replace("conv2" , "convolution2" ) # readout blocks if "pretrained.act_postprocess1.0.project.0" in name: __snake_case : Union[str, Any] = name.replace("pretrained.act_postprocess1.0.project.0" , "neck.reassemble_stage.readout_projects.0.0" ) if "pretrained.act_postprocess2.0.project.0" in name: __snake_case : Optional[int] = name.replace("pretrained.act_postprocess2.0.project.0" , "neck.reassemble_stage.readout_projects.1.0" ) if "pretrained.act_postprocess3.0.project.0" in name: __snake_case : List[str] = name.replace("pretrained.act_postprocess3.0.project.0" , "neck.reassemble_stage.readout_projects.2.0" ) if "pretrained.act_postprocess4.0.project.0" in name: __snake_case : Dict = name.replace("pretrained.act_postprocess4.0.project.0" , "neck.reassemble_stage.readout_projects.3.0" ) # resize blocks if "pretrained.act_postprocess1.3" in name: __snake_case : Tuple = name.replace("pretrained.act_postprocess1.3" , "neck.reassemble_stage.layers.0.projection" ) if "pretrained.act_postprocess1.4" in name: __snake_case : int = name.replace("pretrained.act_postprocess1.4" , "neck.reassemble_stage.layers.0.resize" ) if "pretrained.act_postprocess2.3" in name: __snake_case : Union[str, Any] = name.replace("pretrained.act_postprocess2.3" , "neck.reassemble_stage.layers.1.projection" ) if "pretrained.act_postprocess2.4" in name: __snake_case : Optional[Any] = name.replace("pretrained.act_postprocess2.4" , "neck.reassemble_stage.layers.1.resize" ) if "pretrained.act_postprocess3.3" in name: __snake_case : Optional[int] = name.replace("pretrained.act_postprocess3.3" , "neck.reassemble_stage.layers.2.projection" ) if "pretrained.act_postprocess4.3" in name: __snake_case : Dict = name.replace("pretrained.act_postprocess4.3" , "neck.reassemble_stage.layers.3.projection" ) if "pretrained.act_postprocess4.4" in name: __snake_case : Union[str, Any] = name.replace("pretrained.act_postprocess4.4" , "neck.reassemble_stage.layers.3.resize" ) if "pretrained" in name: __snake_case : Union[str, Any] = name.replace("pretrained" , "dpt" ) if "bn" in name: __snake_case : Tuple = name.replace("bn" , "batch_norm" ) if "head" in name: __snake_case : Dict = name.replace("head" , "head.head" ) if "encoder.norm" in name: __snake_case : Optional[int] = name.replace("encoder.norm" , "layernorm" ) if "auxlayer" in name: __snake_case : Tuple = name.replace("auxlayer" , "auxiliary_head.head" ) if "backbone" in name: __snake_case : str = name.replace("backbone" , "backbone.bit.encoder" ) if ".." in name: __snake_case : Tuple = name.replace(".." , "." ) if "stem.conv" in name: __snake_case : int = name.replace("stem.conv" , "bit.embedder.convolution" ) if "blocks" in name: __snake_case : Any = name.replace("blocks" , "layers" ) if "convolution" in name and "backbone" in name: __snake_case : Optional[int] = name.replace("convolution" , "conv" ) if "layer" in name and "backbone" in name: __snake_case : List[Any] = name.replace("layer" , "layers" ) if "backbone.bit.encoder.bit" in name: __snake_case : Optional[int] = name.replace("backbone.bit.encoder.bit" , "backbone.bit" ) if "embedder.conv" in name: __snake_case : int = name.replace("embedder.conv" , "embedder.convolution" ) if "backbone.bit.encoder.stem.norm" in name: __snake_case : Optional[Any] = name.replace("backbone.bit.encoder.stem.norm" , "backbone.bit.embedder.norm" ) return name def lowerCAmelCase_ ( __lowerCamelCase , __lowerCamelCase ): for i in range(config.num_hidden_layers ): # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) __snake_case : int = state_dict.pop(F'dpt.encoder.layer.{i}.attn.qkv.weight' ) __snake_case : Any = state_dict.pop(F'dpt.encoder.layer.{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 : List[Any] = in_proj_bias[: config.hidden_size] __snake_case : str = 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 : Optional[Any] = in_proj_weight[ -config.hidden_size :, : ] __snake_case : int = in_proj_bias[-config.hidden_size :] def lowerCAmelCase_ ( ): __snake_case : List[str] = "http://images.cocodataset.org/val2017/000000039769.jpg" __snake_case : int = Image.open(requests.get(__lowerCamelCase , stream=__lowerCamelCase ).raw ) return im @torch.no_grad() def lowerCAmelCase_ ( __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase ): __snake_case , __snake_case : Optional[int] = get_dpt_config(__lowerCamelCase ) # load original state_dict from URL # state_dict = torch.hub.load_state_dict_from_url(checkpoint_url, map_location="cpu") __snake_case : Optional[int] = torch.load(__lowerCamelCase , map_location="cpu" ) # remove certain keys remove_ignore_keys_(__lowerCamelCase ) # rename keys for key in state_dict.copy().keys(): __snake_case : Optional[int] = state_dict.pop(__lowerCamelCase ) __snake_case : Optional[Any] = val # read in qkv matrices read_in_q_k_v(__lowerCamelCase , __lowerCamelCase ) # load HuggingFace model __snake_case : Dict = DPTForSemanticSegmentation(__lowerCamelCase ) if "ade" in checkpoint_url else DPTForDepthEstimation(__lowerCamelCase ) model.load_state_dict(__lowerCamelCase ) model.eval() # Check outputs on an image __snake_case : str = 4_8_0 if "ade" in checkpoint_url else 3_8_4 __snake_case : Any = DPTImageProcessor(size=__lowerCamelCase ) __snake_case : int = prepare_img() __snake_case : Union[str, Any] = image_processor(__lowerCamelCase , return_tensors="pt" ) # forward pass __snake_case : Dict = model(**__lowerCamelCase ).logits if "ade" in checkpoint_url else model(**__lowerCamelCase ).predicted_depth if show_prediction: __snake_case : int = ( torch.nn.functional.interpolate( outputs.unsqueeze(1 ) , size=(image.size[1], image.size[0]) , mode="bicubic" , align_corners=__lowerCamelCase , ) .squeeze() .cpu() .numpy() ) Image.fromarray((prediction / prediction.max()) * 2_5_5 ).show() if pytorch_dump_folder_path is not None: Path(__lowerCamelCase ).mkdir(exist_ok=__lowerCamelCase ) print(F'Saving model to {pytorch_dump_folder_path}' ) model.save_pretrained(__lowerCamelCase ) print(F'Saving image processor to {pytorch_dump_folder_path}' ) image_processor.save_pretrained(__lowerCamelCase ) if push_to_hub: model.push_to_hub("ybelkada/dpt-hybrid-midas" ) image_processor.push_to_hub("ybelkada/dpt-hybrid-midas" ) if __name__ == "__main__": _snake_case : int = argparse.ArgumentParser() # Required parameters parser.add_argument( "--checkpoint_url", default="https://github.com/intel-isl/DPT/releases/download/1_0/dpt_large-midas-2f21e586.pt", type=str, help="URL of the original DPT checkpoint you'd like to convert.", ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, required=False, help="Path to the output PyTorch model directory.", ) parser.add_argument( "--push_to_hub", action="store_true", ) parser.add_argument( "--model_name", default="dpt-large", type=str, help="Name of the model, in case you're pushing to the hub.", ) parser.add_argument( "--show_prediction", action="store_true", ) _snake_case : str = parser.parse_args() convert_dpt_checkpoint( args.checkpoint_url, args.pytorch_dump_folder_path, args.push_to_hub, args.model_name, args.show_prediction )
123
0
'''simple docstring''' _lowercase = [0, 2, 4, 6, 8] _lowercase = [1, 3, 5, 7, 9] def A (__lowerCamelCase :int , __lowerCamelCase :int , __lowerCamelCase :list[int] , __lowerCamelCase :int ): if remaining_length == 0: if digits[0] == 0 or digits[-1] == 0: return 0 for i in range(length // 2 - 1 , -1 , -1 ): remainder += digits[i] + digits[length - i - 1] if remainder % 2 == 0: return 0 remainder //= 10 return 1 if remaining_length == 1: if remainder % 2 == 0: return 0 _lowerCAmelCase = 0 for digit in range(10 ): _lowerCAmelCase = digit result += reversible_numbers( 0 , (remainder + 2 * digit) // 10 , __lowerCamelCase , __lowerCamelCase ) return result _lowerCAmelCase = 0 for digita in range(10 ): _lowerCAmelCase = digita if (remainder + digita) % 2 == 0: _lowerCAmelCase = ODD_DIGITS else: _lowerCAmelCase = EVEN_DIGITS for digita in other_parity_digits: _lowerCAmelCase = digita result += reversible_numbers( remaining_length - 2 , (remainder + digita + digita) // 10 , __lowerCamelCase , __lowerCamelCase , ) return result def A (__lowerCamelCase :int = 9 ): _lowerCAmelCase = 0 for length in range(1 , max_power + 1 ): result += reversible_numbers(__lowerCamelCase , 0 , [0] * length , __lowerCamelCase ) return result if __name__ == "__main__": print(F"""{solution() = }""")
229
'''simple docstring''' from __future__ import annotations # This is the precision for this function which can be altered. # It is recommended for users to keep this number greater than or equal to 10. _lowercase = 10 def A (__lowerCamelCase :int , __lowerCamelCase :int , __lowerCamelCase :list[int] , __lowerCamelCase :int ): for i in range(__lowerCamelCase , __lowerCamelCase ): if array[i] == target: return i return -1 def A (__lowerCamelCase :list[int] , __lowerCamelCase :int ): _lowerCAmelCase = 0 _lowerCAmelCase = len(__lowerCamelCase ) while left <= right: if right - left < precision: return lin_search(__lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase ) _lowerCAmelCase = (left + right) // 3 + 1 _lowerCAmelCase = 2 * (left + right) // 3 + 1 if array[one_third] == target: return one_third elif array[two_third] == target: return two_third elif target < array[one_third]: _lowerCAmelCase = one_third - 1 elif array[two_third] < target: _lowerCAmelCase = two_third + 1 else: _lowerCAmelCase = one_third + 1 _lowerCAmelCase = two_third - 1 else: return -1 def A (__lowerCamelCase :int , __lowerCamelCase :int , __lowerCamelCase :list[int] , __lowerCamelCase :int ): if left < right: if right - left < precision: return lin_search(__lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase ) _lowerCAmelCase = (left + right) // 3 + 1 _lowerCAmelCase = 2 * (left + right) // 3 + 1 if array[one_third] == target: return one_third elif array[two_third] == target: return two_third elif target < array[one_third]: return rec_ternary_search(__lowerCamelCase , one_third - 1 , __lowerCamelCase , __lowerCamelCase ) elif array[two_third] < target: return rec_ternary_search(two_third + 1 , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase ) else: return rec_ternary_search(one_third + 1 , two_third - 1 , __lowerCamelCase , __lowerCamelCase ) else: return -1 if __name__ == "__main__": import doctest doctest.testmod() _lowercase = input("""Enter numbers separated by comma:\n""").strip() _lowercase = [int(item.strip()) for item in user_input.split(""",""")] assert collection == sorted(collection), F"List must be ordered.\n{collection}." _lowercase = int(input("""Enter the number to be found in the list:\n""").strip()) _lowercase = ite_ternary_search(collection, target) _lowercase = rec_ternary_search(0, len(collection) - 1, collection, target) if resulta != -1: print(F"""Iterative search: {target} found at positions: {resulta}""") print(F"""Recursive search: {target} found at positions: {resulta}""") else: print("""Not found""")
229
1
'''simple docstring''' import os from huggingface_hub.constants import HUGGINGFACE_HUB_CACHE, hf_cache_home __lowerCAmelCase = HUGGINGFACE_HUB_CACHE __lowerCAmelCase = """config.json""" __lowerCAmelCase = """diffusion_pytorch_model.bin""" __lowerCAmelCase = """diffusion_flax_model.msgpack""" __lowerCAmelCase = """model.onnx""" __lowerCAmelCase = """diffusion_pytorch_model.safetensors""" __lowerCAmelCase = """weights.pb""" __lowerCAmelCase = """https://huggingface.co""" __lowerCAmelCase = default_cache_path __lowerCAmelCase = """diffusers_modules""" __lowerCAmelCase = os.getenv("""HF_MODULES_CACHE""", os.path.join(hf_cache_home, """modules""")) __lowerCAmelCase = ["""fp16""", """non-ema"""] __lowerCAmelCase = """.self_attn"""
271
'''simple docstring''' import inspect import os import unittest from pathlib import Path import torch import accelerate from accelerate.test_utils import execute_subprocess_async from accelerate.test_utils.testing import run_command class UpperCAmelCase__ ( unittest.TestCase ): """simple docstring""" __UpperCAmelCase : Optional[Any] = inspect.getfile(accelerate.test_utils ) __UpperCAmelCase : List[str] = os.path.sep.join(mod_file.split(os.path.sep )[:-1] + ['''scripts''', '''test_cli.py'''] ) __UpperCAmelCase : Dict = ['''accelerate''', '''launch'''] __UpperCAmelCase : Dict = Path.home() / '''.cache/huggingface/accelerate''' __UpperCAmelCase : Dict = '''default_config.yaml''' __UpperCAmelCase : Optional[Any] = config_folder / config_file __UpperCAmelCase : Dict = config_folder / '''_default_config.yaml''' __UpperCAmelCase : Any = Path('''tests/test_configs''' ) @classmethod def __lowercase ( cls : int ): '''simple docstring''' if cls.config_path.is_file(): cls.config_path.rename(cls.changed_path ) @classmethod def __lowercase ( cls : List[Any] ): '''simple docstring''' if cls.changed_path.is_file(): cls.changed_path.rename(cls.config_path ) def __lowercase ( self : Optional[int] ): '''simple docstring''' _a : Dict = self.base_cmd if torch.cuda.is_available() and (torch.cuda.device_count() > 1): cmd += ["--multi_gpu"] execute_subprocess_async(cmd + [self.test_file_path] ,env=os.environ.copy() ) def __lowercase ( self : Optional[Any] ): '''simple docstring''' for config in sorted(self.test_config_path.glob('**/*.yaml' ) ): with self.subTest(config_file=_a ): execute_subprocess_async( self.base_cmd + ['--config_file', str(_a ), self.test_file_path] ,env=os.environ.copy() ) def __lowercase ( self : Optional[int] ): '''simple docstring''' execute_subprocess_async(['accelerate', 'test'] ,env=os.environ.copy() ) class UpperCAmelCase__ ( unittest.TestCase ): """simple docstring""" __UpperCAmelCase : Optional[Any] = '''test-tpu''' __UpperCAmelCase : Any = '''us-central1-a''' __UpperCAmelCase : List[Any] = '''ls''' __UpperCAmelCase : Any = ['''accelerate''', '''tpu-config'''] __UpperCAmelCase : Dict = '''cd /usr/share''' __UpperCAmelCase : Any = '''tests/test_samples/test_command_file.sh''' __UpperCAmelCase : List[Any] = '''Running gcloud compute tpus tpu-vm ssh''' def __lowercase ( self : Dict ): '''simple docstring''' _a : Optional[Any] = run_command( self.cmd + ['--command', self.command, '--tpu_zone', self.tpu_zone, '--tpu_name', self.tpu_name, '--debug'] ,return_stdout=_a ,) self.assertIn( F"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls --worker all""" ,_a ,) def __lowercase ( self : List[str] ): '''simple docstring''' _a : Any = run_command( self.cmd + [ '--config_file', 'tests/test_configs/0_12_0.yaml', '--command', self.command, '--tpu_zone', self.tpu_zone, '--tpu_name', self.tpu_name, '--debug', ] ,return_stdout=_a ,) self.assertIn( F"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls --worker all""" ,_a ,) def __lowercase ( self : List[str] ): '''simple docstring''' _a : Optional[int] = run_command( self.cmd + ['--config_file', 'tests/test_configs/latest.yaml', '--debug'] ,return_stdout=_a ) self.assertIn( F"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; echo \"hello world\"; echo \"this is a second command\" --worker all""" ,_a ,) def __lowercase ( self : int ): '''simple docstring''' _a : Optional[Any] = run_command( self.cmd + ['--config_file', 'tests/test_configs/latest.yaml', '--command', self.command, '--debug'] ,return_stdout=_a ,) self.assertIn( F"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls --worker all""" ,_a ,) def __lowercase ( self : str ): '''simple docstring''' _a : List[str] = run_command( self.cmd + [ '--config_file', 'tests/test_configs/latest.yaml', '--command', self.command, '--command', 'echo "Hello World"', '--debug', ] ,return_stdout=_a ,) self.assertIn( F"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls; echo \"Hello World\" --worker all""" ,_a ,) def __lowercase ( self : Union[str, Any] ): '''simple docstring''' _a : Any = run_command( self.cmd + ['--config_file', 'tests/test_configs/latest.yaml', '--command_file', self.command_file, '--debug'] ,return_stdout=_a ,) self.assertIn( F"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; echo \"hello world\"; echo \"this is a second command\" --worker all""" ,_a ,) def __lowercase ( self : Union[str, Any] ): '''simple docstring''' _a : Union[str, Any] = run_command( self.cmd + [ '--config_file', 'tests/test_configs/0_12_0.yaml', '--command_file', self.command_file, '--tpu_zone', self.tpu_zone, '--tpu_name', self.tpu_name, '--debug', ] ,return_stdout=_a ,) self.assertIn( F"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; echo \"hello world\"; echo \"this is a second command\" --worker all""" ,_a ,) def __lowercase ( self : Any ): '''simple docstring''' _a : Optional[int] = run_command( self.cmd + ['--config_file', 'tests/test_configs/latest.yaml', '--install_accelerate', '--debug'] ,return_stdout=_a ,) self.assertIn( F"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; pip install accelerate -U; echo \"hello world\"; echo \"this is a second command\" --worker all""" ,_a ,) def __lowercase ( self : List[str] ): '''simple docstring''' _a : Optional[int] = run_command( self.cmd + [ '--config_file', 'tests/test_configs/latest.yaml', '--install_accelerate', '--accelerate_version', '12.0.0', '--debug', ] ,return_stdout=_a ,) self.assertIn( F"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; pip install accelerate==12.0.0; echo \"hello world\"; echo \"this is a second command\" --worker all""" ,_a ,)
271
1
def __lowerCamelCase ( __magic_name__ : Optional[Any] , __magic_name__ : List[str] = False ): if not isinstance(lowercase_ , lowercase_ ): a__: List[str] =F"Expected string as input, found {type(lowercase_ )}" raise ValueError(lowercase_ ) if not isinstance(lowercase_ , lowercase_ ): a__: Any =F"Expected boolean as use_pascal parameter, found {type(lowercase_ )}" raise ValueError(lowercase_ ) a__: List[Any] =input_str.split("_" ) a__: Optional[Any] =0 if use_pascal else 1 a__: Union[str, Any] =words[start_index:] a__: List[Any] =[word[0].upper() + word[1:] for word in words_to_capitalize] a__: Any ="" if use_pascal else words[0] return "".join([initial_word, *capitalized_words] ) if __name__ == "__main__": from doctest import testmod testmod()
362
from dataclasses import dataclass from typing import List, Optional, Union import numpy as np import PIL import torch from transformers import CLIPImageProcessor, CLIPVisionModel from ...models import PriorTransformer from ...pipelines import DiffusionPipeline from ...schedulers import HeunDiscreteScheduler from ...utils import ( BaseOutput, is_accelerate_available, logging, randn_tensor, replace_example_docstring, ) from .renderer import ShapERenderer __UpperCAmelCase = logging.get_logger(__name__) # pylint: disable=invalid-name __UpperCAmelCase = ''' Examples: ```py >>> from PIL import Image >>> import torch >>> from diffusers import DiffusionPipeline >>> from diffusers.utils import export_to_gif, load_image >>> device = torch.device("cuda" if torch.cuda.is_available() else "cpu") >>> repo = "openai/shap-e-img2img" >>> pipe = DiffusionPipeline.from_pretrained(repo, torch_dtype=torch.float16) >>> pipe = pipe.to(device) >>> guidance_scale = 3.0 >>> image_url = "https://hf.co/datasets/diffusers/docs-images/resolve/main/shap-e/corgi.png" >>> image = load_image(image_url).convert("RGB") >>> images = pipe( ... image, ... guidance_scale=guidance_scale, ... num_inference_steps=64, ... frame_size=256, ... ).images >>> gif_path = export_to_gif(images[0], "corgi_3d.gif") ``` ''' @dataclass class lowerCamelCase__ ( _a ): _lowerCAmelCase = 42 class lowerCamelCase__ ( _a ): def __init__( self : Optional[Any] , _a : PriorTransformer , _a : CLIPVisionModel , _a : CLIPImageProcessor , _a : HeunDiscreteScheduler , _a : ShapERenderer , ): super().__init__() self.register_modules( prior=_a , image_encoder=_a , image_processor=_a , scheduler=_a , renderer=_a , ) def _lowerCamelCase ( self : Tuple , _a : Tuple , _a : Tuple , _a : Any , _a : Any , _a : List[str] , _a : Any ): if latents is None: a__: Any =randn_tensor(_a , generator=_a , device=_a , dtype=_a ) else: if latents.shape != shape: raise ValueError(F"Unexpected latents shape, got {latents.shape}, expected {shape}" ) a__: List[str] =latents.to(_a ) a__: int =latents * scheduler.init_noise_sigma return latents def _lowerCamelCase ( self : Dict , _a : str=0 ): if is_accelerate_available(): from accelerate import cpu_offload else: raise ImportError("Please install accelerate via `pip install accelerate`" ) a__: List[Any] =torch.device(F"cuda:{gpu_id}" ) a__: List[Any] =[self.image_encoder, self.prior] for cpu_offloaded_model in models: if cpu_offloaded_model is not None: cpu_offload(_a , _a ) @property def _lowerCamelCase ( self : Optional[Any] ): if self.device != torch.device("meta" ) or not hasattr(self.image_encoder , "_hf_hook" ): return self.device for module in self.image_encoder.modules(): if ( hasattr(_a , "_hf_hook" ) and hasattr(module._hf_hook , "execution_device" ) and module._hf_hook.execution_device is not None ): return torch.device(module._hf_hook.execution_device ) return self.device def _lowerCamelCase ( self : Any , _a : List[str] , _a : Optional[int] , _a : Union[str, Any] , _a : List[str] , ): if isinstance(_a , _a ) and isinstance(image[0] , torch.Tensor ): a__: Optional[int] =torch.cat(_a , axis=0 ) if image[0].ndim == 4 else torch.stack(_a , axis=0 ) if not isinstance(_a , torch.Tensor ): a__: Optional[Any] =self.image_processor(_a , return_tensors="pt" ).pixel_values[0].unsqueeze(0 ) a__: int =image.to(dtype=self.image_encoder.dtype , device=_a ) a__: str =self.image_encoder(_a )["last_hidden_state"] a__: Tuple =image_embeds[:, 1:, :].contiguous() # batch_size, dim, 256 a__: Optional[Any] =image_embeds.repeat_interleave(_a , dim=0 ) if do_classifier_free_guidance: a__: Union[str, Any] =torch.zeros_like(_a ) # 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 a__: int =torch.cat([negative_image_embeds, image_embeds] ) return image_embeds @torch.no_grad() @replace_example_docstring(_a ) def __call__( self : List[Any] , _a : Union[PIL.Image.Image, List[PIL.Image.Image]] , _a : int = 1 , _a : int = 2_5 , _a : Optional[Union[torch.Generator, List[torch.Generator]]] = None , _a : Optional[torch.FloatTensor] = None , _a : float = 4.0 , _a : int = 6_4 , _a : Optional[str] = "pil" , _a : bool = True , ): if isinstance(_a , PIL.Image.Image ): a__: List[str] =1 elif isinstance(_a , torch.Tensor ): a__: List[Any] =image.shape[0] elif isinstance(_a , _a ) and isinstance(image[0] , (torch.Tensor, PIL.Image.Image) ): a__: int =len(_a ) else: raise ValueError( F"`image` has to be of type `PIL.Image.Image`, `torch.Tensor`, `List[PIL.Image.Image]` or `List[torch.Tensor]` but is {type(_a )}" ) a__: Optional[int] =self._execution_device a__: Optional[int] =batch_size * num_images_per_prompt a__: Any =guidance_scale > 1.0 a__: int =self._encode_image(_a , _a , _a , _a ) # prior self.scheduler.set_timesteps(_a , device=_a ) a__: int =self.scheduler.timesteps a__: str =self.prior.config.num_embeddings a__: Dict =self.prior.config.embedding_dim a__: Dict =self.prepare_latents( (batch_size, num_embeddings * embedding_dim) , image_embeds.dtype , _a , _a , _a , self.scheduler , ) # YiYi notes: for testing only to match ldm, we can directly create a latents with desired shape: batch_size, num_embeddings, embedding_dim a__: int =latents.reshape(latents.shape[0] , _a , _a ) for i, t in enumerate(self.progress_bar(_a ) ): # expand the latents if we are doing classifier free guidance a__: Union[str, Any] =torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents a__: Tuple =self.scheduler.scale_model_input(_a , _a ) a__: Tuple =self.prior( _a , timestep=_a , proj_embedding=_a , ).predicted_image_embedding # remove the variance a__ , a__: List[str] =noise_pred.split( scaled_model_input.shape[2] , dim=2 ) # batch_size, num_embeddings, embedding_dim if do_classifier_free_guidance is not None: a__ , a__: Union[str, Any] =noise_pred.chunk(2 ) a__: Union[str, Any] =noise_pred_uncond + guidance_scale * (noise_pred - noise_pred_uncond) a__: Any =self.scheduler.step( _a , timestep=_a , sample=_a , ).prev_sample if output_type == "latent": return ShapEPipelineOutput(images=_a ) a__: List[str] =[] for i, latent in enumerate(_a ): print() a__: Any =self.renderer.decode( latent[None, :] , _a , size=_a , ray_batch_size=4_0_9_6 , n_coarse_samples=6_4 , n_fine_samples=1_2_8 , ) images.append(_a ) a__: Tuple =torch.stack(_a ) if output_type not in ["np", "pil"]: raise ValueError(F"Only the output types `pil` and `np` are supported not output_type={output_type}" ) a__: Dict =images.cpu().numpy() if output_type == "pil": a__: Optional[int] =[self.numpy_to_pil(_a ) for image in images] # Offload last model to CPU if hasattr(self , "final_offload_hook" ) and self.final_offload_hook is not None: self.final_offload_hook.offload() if not return_dict: return (images,) return ShapEPipelineOutput(images=_a )
42
0
from dataclasses import dataclass, field from typing import Optional from transformers import AutoConfig, AutoImageProcessor, AutoTokenizer, FlaxVisionEncoderDecoderModel, HfArgumentParser @dataclass class __A : """simple docstring""" UpperCamelCase__ : str =field( metadata={"""help""": """The output directory where the model will be written."""} , ) UpperCamelCase__ : str =field( metadata={ """help""": ( """The encoder model checkpoint for weights initialization.""" """Don\'t set if you want to train an encoder model from scratch.""" ) } , ) UpperCamelCase__ : str =field( metadata={ """help""": ( """The decoder model checkpoint for weights initialization.""" """Don\'t set if you want to train a decoder model from scratch.""" ) } , ) UpperCamelCase__ : Optional[str] =field( default=_UpperCAmelCase , metadata={"""help""": """Pretrained encoder config name or path if not the same as encoder_model_name"""} ) UpperCamelCase__ : Optional[str] =field( default=_UpperCAmelCase , metadata={"""help""": """Pretrained decoder config name or path if not the same as decoder_model_name"""} ) def A ( ) -> Any: __UpperCamelCase : Tuple =HfArgumentParser((ModelArguments,) ) ((__UpperCamelCase ) , ) : List[str] =parser.parse_args_into_dataclasses() # Load pretrained model and tokenizer # Use explicit specified encoder config if model_args.encoder_config_name: __UpperCamelCase : Optional[int] =AutoConfig.from_pretrained(model_args.encoder_config_name ) # Use pretrained encoder model's config else: __UpperCamelCase : int =AutoConfig.from_pretrained(model_args.encoder_model_name_or_path ) # Use explicit specified decoder config if model_args.decoder_config_name: __UpperCamelCase : int =AutoConfig.from_pretrained(model_args.decoder_config_name ) # Use pretrained decoder model's config else: __UpperCamelCase : str =AutoConfig.from_pretrained(model_args.decoder_model_name_or_path ) # necessary for `from_encoder_decoder_pretrained` when `decoder_config` is passed __UpperCamelCase : List[str] =True __UpperCamelCase : List[str] =True __UpperCamelCase : Dict =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 __UpperCamelCase : Union[str, Any] =decoder_config.decoder_start_token_id __UpperCamelCase : Any =decoder_config.pad_token_id if decoder_start_token_id is None: __UpperCamelCase : Any =decoder_config.bos_token_id if pad_token_id is None: __UpperCamelCase : List[str] =decoder_config.eos_token_id # This is necessary to make Flax's generate() work __UpperCamelCase : Optional[Any] =decoder_config.eos_token_id __UpperCamelCase : Tuple =decoder_start_token_id __UpperCamelCase : Optional[Any] =pad_token_id __UpperCamelCase : List[Any] =AutoImageProcessor.from_pretrained(model_args.encoder_model_name_or_path ) __UpperCamelCase : Optional[int] =AutoTokenizer.from_pretrained(model_args.decoder_model_name_or_path ) __UpperCamelCase : Union[str, Any] =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()
71
import csv import tweepy # Twitter API credentials __a : Union[str, Any] = """""" __a : Union[str, Any] = """""" __a : Union[str, Any] = """""" __a : List[Any] = """""" def UpperCAmelCase ( lowercase ): """simple docstring""" __lowercase = tweepy.OAuthHandler(lowercase , lowercase ) auth.set_access_token(lowercase , lowercase ) __lowercase = tweepy.API(lowercase ) # initialize a list to hold all the tweepy Tweets __lowercase = [] # make initial request for most recent tweets (200 is the maximum allowed count) __lowercase = api.user_timeline(screen_name=lowercase , count=200 ) # save most recent tweets alltweets.extend(lowercase ) # save the id of the oldest tweet less one __lowercase = alltweets[-1].id - 1 # keep grabbing tweets until there are no tweets left to grab while len(lowercase ) > 0: print(F"getting tweets before {oldest}" ) # all subsequent requests use the max_id param to prevent duplicates __lowercase = api.user_timeline( screen_name=lowercase , count=200 , max_id=lowercase ) # save most recent tweets alltweets.extend(lowercase ) # update the id of the oldest tweet less one __lowercase = alltweets[-1].id - 1 print(F"...{len(lowercase )} tweets downloaded so far" ) # transform the tweepy tweets into a 2D array that will populate the csv __lowercase = [[tweet.id_str, tweet.created_at, tweet.text] for tweet in alltweets] # write the csv with open(F"new_{screen_name}_tweets.csv" , '''w''' ) as f: __lowercase = csv.writer(lowercase ) writer.writerow(['''id''', '''created_at''', '''text'''] ) writer.writerows(lowercase ) if __name__ == "__main__": # pass in the username of the account you want to download get_all_tweets("""FirePing32""")
210
0
"""simple docstring""" import warnings from ...utils import logging from .image_processing_dpt import DPTImageProcessor UpperCAmelCase =logging.get_logger(__name__) class lowerCamelCase__ ( SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self ,*lowerCamelCase_ ,**lowerCamelCase_ ) -> None: warnings.warn( """The class DPTFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please""" """ use DPTImageProcessor instead.""" ,lowerCamelCase_ ,) super().__init__(*lowerCamelCase_ ,**lowerCamelCase_ )
77
"""simple docstring""" import warnings from typing import List import numpy as np from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding from ...utils import is_flax_available, is_tf_available, is_torch_available class lowerCamelCase__ ( SCREAMING_SNAKE_CASE ): '''simple docstring''' _lowerCamelCase = ['''image_processor''', '''tokenizer'''] _lowerCamelCase = '''OwlViTImageProcessor''' _lowerCamelCase = ('''CLIPTokenizer''', '''CLIPTokenizerFast''') def __init__( self ,lowerCamelCase_=None ,lowerCamelCase_=None ,**lowerCamelCase_ ) -> Tuple: A = None if "feature_extractor" in kwargs: warnings.warn( """The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`""" """ instead.""" ,lowerCamelCase_ ,) A = kwargs.pop("""feature_extractor""" ) A = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError("""You need to specify an `image_processor`.""" ) if tokenizer is None: raise ValueError("""You need to specify a `tokenizer`.""" ) super().__init__(lowerCamelCase_ ,lowerCamelCase_ ) def __call__( self ,lowerCamelCase_=None ,lowerCamelCase_=None ,lowerCamelCase_=None ,lowerCamelCase_="max_length" ,lowerCamelCase_="np" ,**lowerCamelCase_ ) -> Optional[Any]: if text is None and query_images is None and images is None: raise ValueError( """You have to specify at least one text or query image or image. All three cannot be none.""" ) if text is not None: if isinstance(lowerCamelCase_ ,lowerCamelCase_ ) or (isinstance(lowerCamelCase_ ,lowerCamelCase_ ) and not isinstance(text[0] ,lowerCamelCase_ )): A = [self.tokenizer(lowerCamelCase_ ,padding=lowerCamelCase_ ,return_tensors=lowerCamelCase_ ,**lowerCamelCase_ )] elif isinstance(lowerCamelCase_ ,lowerCamelCase_ ) and isinstance(text[0] ,lowerCamelCase_ ): A = [] # Maximum number of queries across batch A = max([len(lowerCamelCase_ ) for t in text] ) # Pad all batch samples to max number of text queries for t in text: if len(lowerCamelCase_ ) != max_num_queries: A = t + [""" """] * (max_num_queries - len(lowerCamelCase_ )) A = self.tokenizer(lowerCamelCase_ ,padding=lowerCamelCase_ ,return_tensors=lowerCamelCase_ ,**lowerCamelCase_ ) encodings.append(lowerCamelCase_ ) else: raise TypeError("""Input text should be a string, a list of strings or a nested list of strings""" ) if return_tensors == "np": A = np.concatenate([encoding["""input_ids"""] for encoding in encodings] ,axis=0 ) A = np.concatenate([encoding["""attention_mask"""] for encoding in encodings] ,axis=0 ) elif return_tensors == "jax" and is_flax_available(): import jax.numpy as jnp A = jnp.concatenate([encoding["""input_ids"""] for encoding in encodings] ,axis=0 ) A = jnp.concatenate([encoding["""attention_mask"""] for encoding in encodings] ,axis=0 ) elif return_tensors == "pt" and is_torch_available(): import torch A = torch.cat([encoding["""input_ids"""] for encoding in encodings] ,dim=0 ) A = torch.cat([encoding["""attention_mask"""] for encoding in encodings] ,dim=0 ) elif return_tensors == "tf" and is_tf_available(): import tensorflow as tf A = tf.stack([encoding["""input_ids"""] for encoding in encodings] ,axis=0 ) A = tf.stack([encoding["""attention_mask"""] for encoding in encodings] ,axis=0 ) else: raise ValueError("""Target return tensor type could not be returned""" ) A = BatchEncoding() A = input_ids A = attention_mask if query_images is not None: A = BatchEncoding() A = self.image_processor( lowerCamelCase_ ,return_tensors=lowerCamelCase_ ,**lowerCamelCase_ ).pixel_values A = query_pixel_values if images is not None: A = self.image_processor(lowerCamelCase_ ,return_tensors=lowerCamelCase_ ,**lowerCamelCase_ ) if text is not None and images is not None: A = image_features.pixel_values return encoding elif query_images is not None and images is not None: A = image_features.pixel_values return encoding elif text is not None or query_images is not None: return encoding else: return BatchEncoding(data=dict(**lowerCamelCase_ ) ,tensor_type=lowerCamelCase_ ) def UpperCamelCase__ ( self ,*lowerCamelCase_ ,**lowerCamelCase_ ) -> int: return self.image_processor.post_process(*lowerCamelCase_ ,**lowerCamelCase_ ) def UpperCamelCase__ ( self ,*lowerCamelCase_ ,**lowerCamelCase_ ) -> Optional[Any]: return self.image_processor.post_process_object_detection(*lowerCamelCase_ ,**lowerCamelCase_ ) def UpperCamelCase__ ( self ,*lowerCamelCase_ ,**lowerCamelCase_ ) -> Optional[Any]: return self.image_processor.post_process_image_guided_detection(*lowerCamelCase_ ,**lowerCamelCase_ ) def UpperCamelCase__ ( self ,*lowerCamelCase_ ,**lowerCamelCase_ ) -> List[str]: return self.tokenizer.batch_decode(*lowerCamelCase_ ,**lowerCamelCase_ ) def UpperCamelCase__ ( self ,*lowerCamelCase_ ,**lowerCamelCase_ ) -> List[str]: return self.tokenizer.decode(*lowerCamelCase_ ,**lowerCamelCase_ ) @property def UpperCamelCase__ ( self ) -> Union[str, Any]: warnings.warn( """`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.""" ,lowerCamelCase_ ,) return self.image_processor_class @property def UpperCamelCase__ ( self ) -> Union[str, Any]: warnings.warn( """`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.""" ,lowerCamelCase_ ,) return self.image_processor
77
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) UpperCAmelCase : Any = { 'configuration_blenderbot': [ 'BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'BlenderbotConfig', 'BlenderbotOnnxConfig', ], 'tokenization_blenderbot': ['BlenderbotTokenizer'], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase : Optional[int] = ['BlenderbotTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase : Optional[Any] = [ 'BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST', 'BlenderbotForCausalLM', 'BlenderbotForConditionalGeneration', 'BlenderbotModel', 'BlenderbotPreTrainedModel', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase : Optional[int] = [ 'TFBlenderbotForConditionalGeneration', 'TFBlenderbotModel', 'TFBlenderbotPreTrainedModel', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase : Optional[int] = [ 'FlaxBlenderbotForConditionalGeneration', 'FlaxBlenderbotModel', 'FlaxBlenderbotPreTrainedModel', ] if TYPE_CHECKING: from .configuration_blenderbot import ( BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP, BlenderbotConfig, BlenderbotOnnxConfig, ) from .tokenization_blenderbot import BlenderbotTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_blenderbot_fast import BlenderbotTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_blenderbot import ( BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST, BlenderbotForCausalLM, BlenderbotForConditionalGeneration, BlenderbotModel, BlenderbotPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_blenderbot import ( TFBlenderbotForConditionalGeneration, TFBlenderbotModel, TFBlenderbotPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_blenderbot import ( FlaxBlenderbotForConditionalGeneration, FlaxBlenderbotModel, FlaxBlenderbotPreTrainedModel, ) else: import sys UpperCAmelCase : Any = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
115
"""simple docstring""" from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import rescale, resize, to_channel_dimension_format from ...image_utils import ( ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging if is_vision_available(): import PIL UpperCAmelCase : Optional[Any] = logging.get_logger(__name__) def lowerCamelCase ( _UpperCamelCase : Any , _UpperCamelCase : Optional[Any] ) -> int: '''simple docstring''' __UpperCAmelCase : int = b.T __UpperCAmelCase : Union[str, Any] = np.sum(np.square(_UpperCamelCase ) , axis=1 ) __UpperCAmelCase : List[Any] = np.sum(np.square(_UpperCamelCase ) , axis=0 ) __UpperCAmelCase : int = np.matmul(_UpperCamelCase , _UpperCamelCase ) __UpperCAmelCase : Tuple = aa[:, None] - 2 * ab + ba[None, :] return d def lowerCamelCase ( _UpperCamelCase : Tuple , _UpperCamelCase : Optional[Any] ) -> Optional[int]: '''simple docstring''' __UpperCAmelCase : Tuple = x.reshape(-1 , 3 ) __UpperCAmelCase : Optional[Any] = squared_euclidean_distance(_UpperCamelCase , _UpperCamelCase ) return np.argmin(_UpperCamelCase , axis=1 ) class lowerCamelCase__ ( A ): """simple docstring""" __a = ["""pixel_values"""] def __init__( self : List[str] , UpperCamelCase : Optional[Union[List[List[int]], np.ndarray]] = None , UpperCamelCase : bool = True , UpperCamelCase : Dict[str, int] = None , UpperCamelCase : PILImageResampling = PILImageResampling.BILINEAR , UpperCamelCase : bool = True , UpperCamelCase : bool = True , **UpperCamelCase : int , ): '''simple docstring''' super().__init__(**UpperCamelCase ) __UpperCAmelCase : Optional[Any] = size if size is not None else {"""height""": 256, """width""": 256} __UpperCAmelCase : Tuple = get_size_dict(UpperCamelCase ) __UpperCAmelCase : Union[str, Any] = np.array(UpperCamelCase ) if clusters is not None else None __UpperCAmelCase : int = do_resize __UpperCAmelCase : List[str] = size __UpperCAmelCase : Dict = resample __UpperCAmelCase : int = do_normalize __UpperCAmelCase : List[Any] = do_color_quantize def lowerCamelCase__ ( self : List[str] , UpperCamelCase : np.ndarray , UpperCamelCase : Dict[str, int] , UpperCamelCase : PILImageResampling = PILImageResampling.BILINEAR , UpperCamelCase : Optional[Union[str, ChannelDimension]] = None , **UpperCamelCase : str , ): '''simple docstring''' __UpperCAmelCase : Any = get_size_dict(UpperCamelCase ) if "height" not in size or "width" not in size: raise ValueError(f'''Size dictionary must contain both height and width keys. Got {size.keys()}''' ) return resize( UpperCamelCase , size=(size["""height"""], size["""width"""]) , resample=UpperCamelCase , data_format=UpperCamelCase , **UpperCamelCase ) def lowerCamelCase__ ( self : Dict , UpperCamelCase : np.ndarray , UpperCamelCase : Optional[Union[str, ChannelDimension]] = None , ): '''simple docstring''' __UpperCAmelCase : Optional[Any] = rescale(image=UpperCamelCase , scale=1 / 127.5 , data_format=UpperCamelCase ) __UpperCAmelCase : Optional[Any] = image - 1 return image def lowerCamelCase__ ( self : Dict , UpperCamelCase : ImageInput , UpperCamelCase : bool = None , UpperCamelCase : Dict[str, int] = None , UpperCamelCase : PILImageResampling = None , UpperCamelCase : bool = None , UpperCamelCase : Optional[bool] = None , UpperCamelCase : Optional[Union[List[List[int]], np.ndarray]] = None , UpperCamelCase : Optional[Union[str, TensorType]] = None , UpperCamelCase : Optional[Union[str, ChannelDimension]] = ChannelDimension.FIRST , **UpperCamelCase : Optional[int] , ): '''simple docstring''' __UpperCAmelCase : List[str] = do_resize if do_resize is not None else self.do_resize __UpperCAmelCase : Any = size if size is not None else self.size __UpperCAmelCase : Any = get_size_dict(UpperCamelCase ) __UpperCAmelCase : Optional[Any] = resample if resample is not None else self.resample __UpperCAmelCase : Any = do_normalize if do_normalize is not None else self.do_normalize __UpperCAmelCase : Any = do_color_quantize if do_color_quantize is not None else self.do_color_quantize __UpperCAmelCase : Tuple = clusters if clusters is not None else self.clusters __UpperCAmelCase : Tuple = np.array(UpperCamelCase ) __UpperCAmelCase : int = make_list_of_images(UpperCamelCase ) if not valid_images(UpperCamelCase ): raise ValueError( """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """ """torch.Tensor, tf.Tensor or jax.ndarray.""" ) if do_resize and size is None or resample is None: raise ValueError("""Size and resample must be specified if do_resize is True.""" ) if do_color_quantize and clusters is None: raise ValueError("""Clusters must be specified if do_color_quantize is True.""" ) # All transformations expect numpy arrays. __UpperCAmelCase : Tuple = [to_numpy_array(UpperCamelCase ) for image in images] if do_resize: __UpperCAmelCase : Dict = [self.resize(image=UpperCamelCase , size=UpperCamelCase , resample=UpperCamelCase ) for image in images] if do_normalize: __UpperCAmelCase : Any = [self.normalize(image=UpperCamelCase ) for image in images] if do_color_quantize: __UpperCAmelCase : str = [to_channel_dimension_format(UpperCamelCase , ChannelDimension.LAST ) for image in images] # color quantize from (batch_size, height, width, 3) to (batch_size, height, width) __UpperCAmelCase : Optional[int] = np.array(UpperCamelCase ) __UpperCAmelCase : Any = color_quantize(UpperCamelCase , UpperCamelCase ).reshape(images.shape[:-1] ) # flatten to (batch_size, height*width) __UpperCAmelCase : Dict = images.shape[0] __UpperCAmelCase : Optional[Any] = images.reshape(UpperCamelCase , -1 ) # We need to convert back to a list of images to keep consistent behaviour across processors. __UpperCAmelCase : int = list(UpperCamelCase ) else: __UpperCAmelCase : List[Any] = [to_channel_dimension_format(UpperCamelCase , UpperCamelCase ) for image in images] __UpperCAmelCase : List[str] = {"""input_ids""": images} return BatchFeature(data=UpperCamelCase , tensor_type=UpperCamelCase )
115
1
'''simple docstring''' import math import sys def _a ( _lowerCamelCase ) -> int: """simple docstring""" if number != int(_lowerCamelCase ): raise ValueError("""the value of input must be a natural number""" ) if number < 0: raise ValueError("""the value of input must not be a negative number""" ) if number == 0: return 1 __snake_case : Union[str, Any] = [-1] * (number + 1) __snake_case : List[Any] = 0 for i in range(1 , number + 1 ): __snake_case : Union[str, Any] = sys.maxsize __snake_case : str = int(math.sqrt(_lowerCamelCase ) ) for j in range(1 , root + 1 ): __snake_case : int = 1 + answers[i - (j**2)] __snake_case : Dict = min(_lowerCamelCase , _lowerCamelCase ) __snake_case : Optional[Any] = answer return answers[number] if __name__ == "__main__": import doctest doctest.testmod()
13
'''simple docstring''' import json import os import shutil import tempfile import unittest from transformers import BatchEncoding, CanineTokenizer from transformers.testing_utils import require_tokenizers, require_torch from transformers.tokenization_utils import AddedToken from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin class _A ( __lowercase , unittest.TestCase ): lowercase__: List[Any] = CanineTokenizer lowercase__: Optional[int] = False def lowercase__ ( self : Any ) -> Any: """simple docstring""" super().setUp() __snake_case : Dict = CanineTokenizer() tokenizer.save_pretrained(self.tmpdirname ) @cached_property def lowercase__ ( self : Dict ) -> Optional[Any]: """simple docstring""" return CanineTokenizer.from_pretrained("""google/canine-s""" ) def lowercase__ ( self : str , **__magic_name__ : List[Any] ) -> CanineTokenizer: """simple docstring""" __snake_case : Optional[int] = self.tokenizer_class.from_pretrained(self.tmpdirname , **__magic_name__ ) __snake_case : Optional[Any] = 10_24 return tokenizer @require_torch def lowercase__ ( self : Tuple ) -> int: """simple docstring""" __snake_case : Optional[Any] = self.canine_tokenizer __snake_case : List[str] = ["""Life is like a box of chocolates.""", """You never know what you're gonna get."""] # fmt: off __snake_case : Dict = [5_73_44, 76, 1_05, 1_02, 1_01, 32, 1_05, 1_15, 32, 1_08, 1_05, 1_07, 1_01, 32, 97, 32, 98, 1_11, 1_20, 32, 1_11, 1_02, 32, 99, 1_04, 1_11, 99, 1_11, 1_08, 97, 1_16, 1_01, 1_15, 46, 5_73_45, 0, 0, 0, 0] # fmt: on __snake_case : str = tokenizer(__magic_name__ , padding=__magic_name__ , return_tensors="""pt""" ) self.assertIsInstance(__magic_name__ , __magic_name__ ) __snake_case : Union[str, Any] = list(batch.input_ids.numpy()[0] ) self.assertListEqual(__magic_name__ , __magic_name__ ) self.assertEqual((2, 39) , batch.input_ids.shape ) self.assertEqual((2, 39) , batch.attention_mask.shape ) @require_torch def lowercase__ ( self : Union[str, Any] ) -> Dict: """simple docstring""" __snake_case : Any = self.canine_tokenizer __snake_case : List[Any] = ["""Once there was a man.""", """He wrote a test in HuggingFace Tranformers."""] __snake_case : Tuple = tokenizer(__magic_name__ , padding=__magic_name__ , return_tensors="""pt""" ) # check if input_ids, attention_mask and token_type_ids are returned self.assertIn("""input_ids""" , __magic_name__ ) self.assertIn("""attention_mask""" , __magic_name__ ) self.assertIn("""token_type_ids""" , __magic_name__ ) @require_torch def lowercase__ ( self : int ) -> List[str]: """simple docstring""" __snake_case : Dict = self.canine_tokenizer __snake_case : Optional[Any] = [ """What's the weater?""", """It's about 25 degrees.""", ] __snake_case : Any = tokenizer( text_target=__magic_name__ , max_length=32 , padding="""max_length""" , truncation=__magic_name__ , return_tensors="""pt""" ) self.assertEqual(32 , targets["""input_ids"""].shape[1] ) def lowercase__ ( self : Tuple ) -> int: """simple docstring""" __snake_case : List[Any] = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(f'''{tokenizer.__class__.__name__}''' ): self.assertNotEqual(tokenizer.model_max_length , 42 ) # Now let's start the test __snake_case : str = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(f'''{tokenizer.__class__.__name__}''' ): # Isolate this from the other tests because we save additional tokens/etc __snake_case : Dict = tempfile.mkdtemp() __snake_case : str = """ He is very happy, UNwant\u00E9d,running""" __snake_case : Dict = tokenizer.encode(__magic_name__ , add_special_tokens=__magic_name__ ) tokenizer.save_pretrained(__magic_name__ ) __snake_case : str = tokenizer.__class__.from_pretrained(__magic_name__ ) __snake_case : Dict = after_tokenizer.encode(__magic_name__ , add_special_tokens=__magic_name__ ) self.assertListEqual(__magic_name__ , __magic_name__ ) shutil.rmtree(__magic_name__ ) __snake_case : Tuple = self.get_tokenizers(model_max_length=42 ) for tokenizer in tokenizers: with self.subTest(f'''{tokenizer.__class__.__name__}''' ): # Isolate this from the other tests because we save additional tokens/etc __snake_case : Optional[Any] = tempfile.mkdtemp() __snake_case : List[str] = """ He is very happy, UNwant\u00E9d,running""" __snake_case : Optional[int] = tokenizer.additional_special_tokens # We can add a new special token for Canine as follows: __snake_case : List[Any] = chr(0xE007 ) additional_special_tokens.append(__magic_name__ ) tokenizer.add_special_tokens({"""additional_special_tokens""": additional_special_tokens} ) __snake_case : List[str] = tokenizer.encode(__magic_name__ , add_special_tokens=__magic_name__ ) tokenizer.save_pretrained(__magic_name__ ) __snake_case : Union[str, Any] = tokenizer.__class__.from_pretrained(__magic_name__ ) __snake_case : int = after_tokenizer.encode(__magic_name__ , add_special_tokens=__magic_name__ ) self.assertListEqual(__magic_name__ , __magic_name__ ) self.assertIn(__magic_name__ , after_tokenizer.additional_special_tokens ) self.assertEqual(after_tokenizer.model_max_length , 42 ) __snake_case : str = tokenizer.__class__.from_pretrained(__magic_name__ , model_max_length=43 ) self.assertEqual(tokenizer.model_max_length , 43 ) shutil.rmtree(__magic_name__ ) def lowercase__ ( self : Optional[Any] ) -> Union[str, Any]: """simple docstring""" __snake_case : Tuple = self.get_tokenizers(do_lower_case=__magic_name__ ) for tokenizer in tokenizers: with self.subTest(f'''{tokenizer.__class__.__name__}''' ): __snake_case , __snake_case : Any = self.get_clean_sequence(__magic_name__ ) # a special token for Canine can be defined as follows: __snake_case : Tuple = 0xE005 __snake_case : Tuple = chr(__magic_name__ ) tokenizer.add_special_tokens({"""cls_token""": special_token} ) __snake_case : Optional[Any] = tokenizer.encode(__magic_name__ , add_special_tokens=__magic_name__ ) self.assertEqual(len(__magic_name__ ) , 1 ) __snake_case : Any = tokenizer.decode(ids + encoded_special_token , clean_up_tokenization_spaces=__magic_name__ ) __snake_case : Tuple = tokenizer.encode(__magic_name__ , add_special_tokens=__magic_name__ ) __snake_case : Dict = tokenizer.encode(__magic_name__ , add_special_tokens=__magic_name__ ) __snake_case : int = tokenizer.encode(__magic_name__ , add_special_tokens=__magic_name__ ) self.assertEqual(__magic_name__ , input_encoded + special_token_id ) __snake_case : Tuple = tokenizer.decode(__magic_name__ , skip_special_tokens=__magic_name__ ) self.assertTrue(special_token not in decoded ) def lowercase__ ( self : List[str] ) -> Tuple: """simple docstring""" __snake_case : Any = self.get_tokenizers(do_lower_case=__magic_name__ ) for tokenizer in tokenizers: with self.subTest(f'''{tokenizer.__class__.__name__}''' ): __snake_case : Dict = chr(0xE005 ) __snake_case : str = chr(0xE006 ) # `add_tokens` method stores special tokens only in `tokenizer.unique_no_split_tokens`. (in tokenization_utils.py) tokenizer.add_tokens([SPECIAL_TOKEN_1] , special_tokens=__magic_name__ ) # `add_special_tokens` method stores special tokens in `tokenizer.additional_special_tokens`, # which also occur in `tokenizer.all_special_tokens`. (in tokenization_utils_base.py) tokenizer.add_special_tokens({"""additional_special_tokens""": [SPECIAL_TOKEN_2]} ) __snake_case : Tuple = tokenizer.tokenize(__magic_name__ ) __snake_case : Any = tokenizer.tokenize(__magic_name__ ) self.assertEqual(len(__magic_name__ ) , 1 ) self.assertEqual(len(__magic_name__ ) , 1 ) self.assertEqual(token_a[0] , __magic_name__ ) self.assertEqual(token_a[0] , __magic_name__ ) @require_tokenizers def lowercase__ ( self : Optional[int] ) -> Tuple: """simple docstring""" __snake_case : str = self.get_tokenizers(do_lower_case=__magic_name__ ) for tokenizer in tokenizers: with self.subTest(f'''{tokenizer.__class__.__name__}''' ): # a special token for Canine can be defined as follows: __snake_case : Optional[Any] = 0xE006 __snake_case : List[str] = chr(__magic_name__ ) __snake_case : Optional[Any] = AddedToken(__magic_name__ , lstrip=__magic_name__ ) tokenizer.add_special_tokens({"""additional_special_tokens""": [new_token]} ) with tempfile.TemporaryDirectory() as tmp_dir_name: tokenizer.save_pretrained(__magic_name__ ) tokenizer.from_pretrained(__magic_name__ ) def lowercase__ ( self : Any ) -> int: """simple docstring""" __snake_case : Union[str, Any] = [] if self.test_slow_tokenizer: tokenizer_list.append((self.tokenizer_class, self.get_tokenizer()) ) if self.test_rust_tokenizer: tokenizer_list.append((self.rust_tokenizer_class, self.get_rust_tokenizer()) ) for tokenizer_class, tokenizer_utils in tokenizer_list: with tempfile.TemporaryDirectory() as tmp_dir: tokenizer_utils.save_pretrained(__magic_name__ ) with open(os.path.join(__magic_name__ , """special_tokens_map.json""" ) , encoding="""utf-8""" ) as json_file: __snake_case : Any = json.load(__magic_name__ ) with open(os.path.join(__magic_name__ , """tokenizer_config.json""" ) , encoding="""utf-8""" ) as json_file: __snake_case : Tuple = json.load(__magic_name__ ) # a special token for Canine can be defined as follows: __snake_case : Tuple = 0xE006 __snake_case : int = chr(__magic_name__ ) __snake_case : List[Any] = [new_token_a] __snake_case : Union[str, Any] = [new_token_a] with open(os.path.join(__magic_name__ , """special_tokens_map.json""" ) , """w""" , encoding="""utf-8""" ) as outfile: json.dump(__magic_name__ , __magic_name__ ) with open(os.path.join(__magic_name__ , """tokenizer_config.json""" ) , """w""" , encoding="""utf-8""" ) as outfile: json.dump(__magic_name__ , __magic_name__ ) # the following checks allow us to verify that our test works as expected, i.e. that the tokenizer takes # into account the new value of additional_special_tokens given in the "tokenizer_config.json" and # "special_tokens_map.json" files __snake_case : Tuple = tokenizer_class.from_pretrained(__magic_name__ , extra_ids=0 ) self.assertIn(__magic_name__ , tokenizer_without_change_in_init.additional_special_tokens ) # self.assertIn("an_additional_special_token",tokenizer_without_change_in_init.get_vocab()) # ByT5Tokenization no vocab self.assertEqual( [new_token_a] , tokenizer_without_change_in_init.convert_ids_to_tokens( tokenizer_without_change_in_init.convert_tokens_to_ids([new_token_a] ) ) , ) __snake_case : Any = 0xE007 __snake_case : Any = chr(__magic_name__ ) # Now we test that we can change the value of additional_special_tokens in the from_pretrained __snake_case : Dict = [AddedToken(__magic_name__ , lstrip=__magic_name__ )] __snake_case : Union[str, Any] = tokenizer_class.from_pretrained( __magic_name__ , additional_special_tokens=__magic_name__ , extra_ids=0 ) self.assertIn(__magic_name__ , tokenizer.additional_special_tokens ) # self.assertIn(new_token_2,tokenizer.get_vocab()) # ByT5Tokenization no vocab self.assertEqual( [new_token_a] , tokenizer.convert_ids_to_tokens(tokenizer.convert_tokens_to_ids([new_token_a] ) ) ) @require_tokenizers def lowercase__ ( self : Optional[int] ) -> List[str]: """simple docstring""" __snake_case : int = self.get_tokenizers(do_lower_case=__magic_name__ ) for tokenizer in tokenizers: with self.subTest(f'''{tokenizer.__class__.__name__}''' ): __snake_case : List[str] = """hello world""" if self.space_between_special_tokens: __snake_case : Union[str, Any] = """[CLS] hello world [SEP]""" else: __snake_case : List[Any] = input __snake_case : int = tokenizer.encode(__magic_name__ , add_special_tokens=__magic_name__ ) __snake_case : Any = tokenizer.decode(__magic_name__ , spaces_between_special_tokens=self.space_between_special_tokens ) self.assertIn(__magic_name__ , [output, output.lower()] ) def lowercase__ ( self : Tuple ) -> Tuple: """simple docstring""" __snake_case : Optional[Any] = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(f'''{tokenizer.__class__.__name__}''' ): __snake_case : str = [ """bos_token""", """eos_token""", """unk_token""", """sep_token""", """pad_token""", """cls_token""", """mask_token""", ] __snake_case : Dict = """a""" __snake_case : Tuple = ord(__magic_name__ ) for attr in attributes_list: setattr(__magic_name__ , attr + """_id""" , __magic_name__ ) self.assertEqual(getattr(__magic_name__ , __magic_name__ ) , __magic_name__ ) self.assertEqual(getattr(__magic_name__ , attr + """_id""" ) , __magic_name__ ) setattr(__magic_name__ , attr + """_id""" , __magic_name__ ) self.assertEqual(getattr(__magic_name__ , __magic_name__ ) , __magic_name__ ) self.assertEqual(getattr(__magic_name__ , attr + """_id""" ) , __magic_name__ ) setattr(__magic_name__ , """additional_special_tokens_ids""" , [] ) self.assertListEqual(getattr(__magic_name__ , """additional_special_tokens""" ) , [] ) self.assertListEqual(getattr(__magic_name__ , """additional_special_tokens_ids""" ) , [] ) __snake_case : Dict = 0xE006 __snake_case : str = chr(__magic_name__ ) setattr(__magic_name__ , """additional_special_tokens_ids""" , [additional_special_token_id] ) self.assertListEqual(getattr(__magic_name__ , """additional_special_tokens""" ) , [additional_special_token] ) self.assertListEqual(getattr(__magic_name__ , """additional_special_tokens_ids""" ) , [additional_special_token_id] ) def lowercase__ ( self : Dict ) -> int: """simple docstring""" pass def lowercase__ ( self : str ) -> Tuple: """simple docstring""" pass def lowercase__ ( self : Tuple ) -> List[str]: """simple docstring""" pass def lowercase__ ( self : Optional[int] ) -> Any: """simple docstring""" pass def lowercase__ ( self : Union[str, Any] ) -> List[Any]: """simple docstring""" pass def lowercase__ ( self : List[Any] ) -> List[Any]: """simple docstring""" pass def lowercase__ ( self : List[Any] ) -> Any: """simple docstring""" pass def lowercase__ ( self : Dict ) -> List[str]: """simple docstring""" pass
13
1
"""simple docstring""" from __future__ import annotations import math from collections import Counter from string import ascii_lowercase def _a ( _SCREAMING_SNAKE_CASE ) -> None: snake_case_ , snake_case_ = analyze_text(_SCREAMING_SNAKE_CASE ) snake_case_ = list(""" """ + ascii_lowercase ) # what is our total sum of probabilities. snake_case_ = sum(single_char_strings.values() ) # one length string snake_case_ = 0 # for each alpha we go in our dict and if it is in it we calculate entropy for ch in my_alphas: if ch in single_char_strings: snake_case_ = single_char_strings[ch] snake_case_ = my_str / all_sum my_fir_sum += prob * math.loga(_SCREAMING_SNAKE_CASE ) # entropy formula. # print entropy print(f"""{round(-1 * my_fir_sum ):.1f}""" ) # two len string snake_case_ = sum(two_char_strings.values() ) snake_case_ = 0 # for each alpha (two in size) calculate entropy. for cha in my_alphas: for cha in my_alphas: snake_case_ = cha + cha if sequence in two_char_strings: snake_case_ = two_char_strings[sequence] snake_case_ = int(_SCREAMING_SNAKE_CASE ) / all_sum my_sec_sum += prob * math.loga(_SCREAMING_SNAKE_CASE ) # print second entropy print(f"""{round(-1 * my_sec_sum ):.1f}""" ) # print the difference between them print(f"""{round((-1 * my_sec_sum) - (-1 * my_fir_sum) ):.1f}""" ) def _a ( _SCREAMING_SNAKE_CASE ) -> tuple[dict, dict]: snake_case_ = Counter() # type: ignore snake_case_ = Counter() # type: ignore single_char_strings[text[-1]] += 1 # first case when we have space at start. two_char_strings[" " + text[0]] += 1 for i in range(0 , len(_SCREAMING_SNAKE_CASE ) - 1 ): single_char_strings[text[i]] += 1 two_char_strings[text[i : i + 2]] += 1 return single_char_strings, two_char_strings def _a ( ) -> Optional[int]: import doctest doctest.testmod() # text = ( # "Had repulsive dashwoods suspicion sincerity but advantage now him. Remark " # "easily garret nor nay. Civil those mrs enjoy shy fat merry. You greatest " # "jointure saw horrible. He private he on be imagine suppose. Fertile " # "beloved evident through no service elderly is. Blind there if every no so " # "at. Own neglected you preferred way sincerity delivered his attempted. To " # "of message cottage windows do besides against uncivil. Delightful " # "unreserved impossible few estimating men favourable see entreaties. She " # "propriety immediate was improving. He or entrance humoured likewise " # "moderate. Much nor game son say feel. Fat make met can must form into " # "gate. Me we offending prevailed discovery. " # ) # calculate_prob(text) if __name__ == "__main__": main()
347
"""simple docstring""" from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging __SCREAMING_SNAKE_CASE : str = logging.get_logger(__name__) __SCREAMING_SNAKE_CASE : Tuple = { 'microsoft/beit-base-patch16-224-pt22k': ( 'https://huggingface.co/microsoft/beit-base-patch16-224-pt22k/resolve/main/config.json' ), # See all BEiT models at https://huggingface.co/models?filter=beit } class __A (snake_case__): '''simple docstring''' __lowercase: Optional[int] = """beit""" def __init__( self : List[str] , UpperCAmelCase_ : List[Any]=8_192 , UpperCAmelCase_ : Dict=768 , UpperCAmelCase_ : int=12 , UpperCAmelCase_ : Tuple=12 , UpperCAmelCase_ : List[Any]=3_072 , UpperCAmelCase_ : Tuple="gelu" , UpperCAmelCase_ : Dict=0.0 , UpperCAmelCase_ : List[str]=0.0 , UpperCAmelCase_ : Any=0.02 , UpperCAmelCase_ : Optional[Any]=1E-12 , UpperCAmelCase_ : int=224 , UpperCAmelCase_ : Tuple=16 , UpperCAmelCase_ : List[str]=3 , UpperCAmelCase_ : int=False , UpperCAmelCase_ : List[str]=False , UpperCAmelCase_ : Tuple=False , UpperCAmelCase_ : int=False , UpperCAmelCase_ : List[Any]=0.1 , UpperCAmelCase_ : List[str]=0.1 , UpperCAmelCase_ : Any=True , UpperCAmelCase_ : Dict=[3, 5, 7, 11] , UpperCAmelCase_ : Tuple=[1, 2, 3, 6] , UpperCAmelCase_ : Optional[int]=True , UpperCAmelCase_ : List[Any]=0.4 , UpperCAmelCase_ : Optional[Any]=256 , UpperCAmelCase_ : Optional[Any]=1 , UpperCAmelCase_ : int=False , UpperCAmelCase_ : Tuple=255 , **UpperCAmelCase_ : List[str] , ) ->Optional[Any]: """simple docstring""" super().__init__(**UpperCAmelCase_ ) snake_case_ = vocab_size snake_case_ = hidden_size snake_case_ = num_hidden_layers snake_case_ = num_attention_heads snake_case_ = intermediate_size snake_case_ = hidden_act snake_case_ = hidden_dropout_prob snake_case_ = attention_probs_dropout_prob snake_case_ = initializer_range snake_case_ = layer_norm_eps snake_case_ = image_size snake_case_ = patch_size snake_case_ = num_channels snake_case_ = use_mask_token snake_case_ = use_absolute_position_embeddings snake_case_ = use_relative_position_bias snake_case_ = use_shared_relative_position_bias snake_case_ = layer_scale_init_value snake_case_ = drop_path_rate snake_case_ = use_mean_pooling # decode head attributes (semantic segmentation) snake_case_ = out_indices snake_case_ = pool_scales # auxiliary head attributes (semantic segmentation) snake_case_ = use_auxiliary_head snake_case_ = auxiliary_loss_weight snake_case_ = auxiliary_channels snake_case_ = auxiliary_num_convs snake_case_ = auxiliary_concat_input snake_case_ = semantic_loss_ignore_index class __A (snake_case__): '''simple docstring''' __lowercase: List[Any] = version.parse("""1.11""") @property def lowerCAmelCase ( self : Dict ) ->Mapping[str, Mapping[int, str]]: """simple docstring""" return OrderedDict( [ ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}), ] ) @property def lowerCAmelCase ( self : Any ) ->float: """simple docstring""" return 1E-4
347
1
'''simple docstring''' from typing import List import datasets from datasets.tasks import AudioClassification from ..folder_based_builder import folder_based_builder SCREAMING_SNAKE_CASE_: Tuple =datasets.utils.logging.get_logger(__name__) class __A ( folder_based_builder.FolderBasedBuilderConfig ): a__ : str = None a__ : Optional[int] = None class __A ( folder_based_builder.FolderBasedBuilder ): a__ : Optional[Any] = datasets.Audio() a__ : Optional[Any] = """audio""" a__ : Optional[Any] = AudioFolderConfig a__ : List[str] = 42 # definition at the bottom of the script a__ : Optional[Any] = AudioClassification(audio_column="""audio""" , label_column="""label""" ) SCREAMING_SNAKE_CASE_: Dict =[ '''.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''', ] SCREAMING_SNAKE_CASE_: List[str] =AUDIO_EXTENSIONS
358
'''simple docstring''' import argparse import json import os import fairseq import torch from torch import nn from transformers import ( SpeechaTextaConfig, SpeechaTextaForCausalLM, SpeechaTextaTokenizer, SpeechEncoderDecoderConfig, SpeechEncoderDecoderModel, WavaVecaConfig, WavaVecaFeatureExtractor, WavaVecaModel, logging, ) logging.set_verbosity_info() SCREAMING_SNAKE_CASE_: int =logging.get_logger(__name__) SCREAMING_SNAKE_CASE_: Dict ={ 'post_extract_proj': 'feature_projection.projection', 'encoder.pos_conv.0': 'encoder.pos_conv_embed.conv', 'self_attn.k_proj': 'encoder.layers.*.attention.k_proj', 'self_attn.v_proj': 'encoder.layers.*.attention.v_proj', 'self_attn.q_proj': 'encoder.layers.*.attention.q_proj', 'self_attn.out_proj': 'encoder.layers.*.attention.out_proj', 'self_attn_layer_norm': 'encoder.layers.*.layer_norm', 'fc1': 'encoder.layers.*.feed_forward.intermediate_dense', 'fc2': 'encoder.layers.*.feed_forward.output_dense', 'final_layer_norm': 'encoder.layers.*.final_layer_norm', 'encoder.layer_norm': 'encoder.layer_norm', 'w2v_model.layer_norm': 'feature_projection.layer_norm', 'quantizer.weight_proj': 'quantizer.weight_proj', 'quantizer.vars': 'quantizer.codevectors', 'project_q': 'project_q', 'final_proj': 'project_hid', 'w2v_encoder.proj': 'lm_head', 'mask_emb': 'masked_spec_embed', } SCREAMING_SNAKE_CASE_: List[str] =[ 'lm_head', 'quantizer.weight_proj', 'quantizer.codevectors', 'project_q', 'project_hid', ] def lowerCAmelCase_ ( snake_case_ : str , snake_case_ : List[Any] , snake_case_ : Union[str, Any] , snake_case_ : int , snake_case_ : Any ) -> Optional[int]: '''simple docstring''' for attribute in key.split("." ): UpperCAmelCase_ = getattr(snake_case_ , snake_case_ ) if weight_type is not None: UpperCAmelCase_ = getattr(snake_case_ , snake_case_ ).shape else: UpperCAmelCase_ = hf_pointer.shape assert hf_shape == value.shape, ( f"""Shape of hf {key + "." + weight_type if weight_type is not None else ""} is {hf_shape}, but should be""" f""" {value.shape} for {full_name}""" ) if weight_type == "weight": UpperCAmelCase_ = value elif weight_type == "weight_g": UpperCAmelCase_ = value elif weight_type == "weight_v": UpperCAmelCase_ = value elif weight_type == "bias": UpperCAmelCase_ = value else: UpperCAmelCase_ = value logger.info(f"""{key + "." + weight_type if weight_type is not None else ""} was initialized from {full_name}.""" ) def lowerCAmelCase_ ( snake_case_ : List[Any] , snake_case_ : List[str] ) -> int: '''simple docstring''' UpperCAmelCase_ = [] UpperCAmelCase_ = fairseq_model.state_dict() UpperCAmelCase_ = hf_model.feature_extractor # if encoder has different dim to decoder -> use proj_weight UpperCAmelCase_ = None for name, value in fairseq_dict.items(): UpperCAmelCase_ = False if "conv_layers" in name: load_conv_layer( snake_case_ , snake_case_ , snake_case_ , snake_case_ , hf_model.config.feat_extract_norm == "group" , ) UpperCAmelCase_ = True elif name.split("." )[0] == "proj": UpperCAmelCase_ = fairseq_model.proj UpperCAmelCase_ = True else: for key, mapped_key in MAPPING.items(): if key in name or key.split("w2v_model." )[-1] == name.split("." )[0]: UpperCAmelCase_ = True if "*" in mapped_key: UpperCAmelCase_ = name.split(snake_case_ )[0].split("." )[-2] UpperCAmelCase_ = mapped_key.replace("*" , snake_case_ ) if "weight_g" in name: UpperCAmelCase_ = "weight_g" elif "weight_v" in name: UpperCAmelCase_ = "weight_v" elif "bias" in name: UpperCAmelCase_ = "bias" elif "weight" in name: UpperCAmelCase_ = "weight" else: UpperCAmelCase_ = None set_recursively(snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ ) continue if not is_used: unused_weights.append(snake_case_ ) logger.warning(f"""Unused weights: {unused_weights}""" ) return proj_weight def lowerCAmelCase_ ( snake_case_ : Dict , snake_case_ : List[Any] , snake_case_ : str , snake_case_ : Any , snake_case_ : int ) -> Dict: '''simple docstring''' UpperCAmelCase_ = full_name.split("conv_layers." )[-1] UpperCAmelCase_ = name.split("." ) UpperCAmelCase_ = int(items[0] ) UpperCAmelCase_ = int(items[1] ) if type_id == 0: if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.bias.data.shape, ( f"""{full_name} has size {value.shape}, but""" f""" {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found.""" ) UpperCAmelCase_ = value logger.info(f"""Feat extract conv layer {layer_id} was initialized from {full_name}.""" ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.weight.data.shape, ( f"""{full_name} has size {value.shape}, but""" f""" {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found.""" ) UpperCAmelCase_ = value logger.info(f"""Feat extract conv layer {layer_id} was initialized from {full_name}.""" ) elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm): if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape, ( f"""{full_name} has size {value.shape}, but {feature_extractor[layer_id].layer_norm.bias.data.shape} was""" " found." ) UpperCAmelCase_ = value logger.info(f"""Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.""" ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape, ( f"""{full_name} has size {value.shape}, but""" f""" {feature_extractor[layer_id].layer_norm.weight.data.shape} was found.""" ) UpperCAmelCase_ = value logger.info(f"""Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.""" ) else: unused_weights.append(snake_case_ ) def lowerCAmelCase_ ( snake_case_ : Any ) -> Any: '''simple docstring''' UpperCAmelCase_ , UpperCAmelCase_ = emb.weight.shape UpperCAmelCase_ = nn.Linear(snake_case_ , snake_case_ , bias=snake_case_ ) UpperCAmelCase_ = emb.weight.data return lin_layer def lowerCAmelCase_ ( snake_case_ : int ) -> List[str]: '''simple docstring''' with open(snake_case_ , "r" , encoding="utf-8" ) as f: UpperCAmelCase_ = f.readlines() UpperCAmelCase_ = [line.split(" " )[0] for line in lines] UpperCAmelCase_ = len(snake_case_ ) UpperCAmelCase_ = { "<s>": 0, "<pad>": 1, "</s>": 2, "<unk>": 3, } vocab_dict.update(dict(zip(snake_case_ , range(4 , num_words + 4 ) ) ) ) return vocab_dict @torch.no_grad() def lowerCAmelCase_ ( snake_case_ : Dict , snake_case_ : Tuple , snake_case_ : List[str] , snake_case_ : Dict , snake_case_ : Union[str, Any] , snake_case_ : Tuple , snake_case_ : Any , ) -> Dict: '''simple docstring''' UpperCAmelCase_ = WavaVecaConfig.from_pretrained(snake_case_ ) UpperCAmelCase_ = SpeechaTextaConfig.from_pretrained( snake_case_ , vocab_size=snake_case_ , decoder_layers=snake_case_ , do_stable_layer_norm=snake_case_ ) UpperCAmelCase_ = WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=1_60_00 , padding_value=0 , do_normalize=snake_case_ , return_attention_mask=snake_case_ , ) UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ = fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] , arg_overrides={"data": "/".join(dict_path.split("/" )[:-1] )} ) UpperCAmelCase_ = model[0].eval() # set weights for wav2vec2 encoder UpperCAmelCase_ = WavaVecaModel(snake_case_ ) UpperCAmelCase_ = recursively_load_weights_wavaveca(model.encoder , snake_case_ ) UpperCAmelCase_ = SpeechaTextaForCausalLM(snake_case_ ) UpperCAmelCase_ , UpperCAmelCase_ = hf_decoder.model.decoder.load_state_dict(model.decoder.state_dict() , strict=snake_case_ ) # set output linear layer unexpected_keys.remove("embed_out" ) UpperCAmelCase_ = nn.Parameter(model.decoder.embed_out.detach() ) # layer norm is init to identity matrix so leaving it is fine logger.warning(f"""The following keys are missing when loading the decoder weights: {missing_keys}""" ) logger.warning(f"""The following keys are unexpected when loading the decoder weights: {unexpected_keys}""" ) UpperCAmelCase_ = SpeechEncoderDecoderModel(encoder=snake_case_ , decoder=snake_case_ ) UpperCAmelCase_ = False # add projection layer UpperCAmelCase_ = nn.Parameter(projection_layer.weight ) UpperCAmelCase_ = nn.Parameter(projection_layer.bias ) UpperCAmelCase_ = create_vocab_dict(snake_case_ ) with open(os.path.join(snake_case_ , "vocab.json" ) , "w" ) as fp: json.dump(snake_case_ , snake_case_ ) UpperCAmelCase_ = SpeechaTextaTokenizer(os.path.join(snake_case_ , "vocab.json" ) ) tokenizer.save_pretrained(snake_case_ ) UpperCAmelCase_ = hf_wavavec.config.to_dict() UpperCAmelCase_ = tokenizer.pad_token_id UpperCAmelCase_ = tokenizer.bos_token_id UpperCAmelCase_ = tokenizer.eos_token_id UpperCAmelCase_ = "speech_to_text_2" UpperCAmelCase_ = "wav2vec2" UpperCAmelCase_ = SpeechEncoderDecoderConfig.from_dict(snake_case_ ) hf_wavavec.save_pretrained(snake_case_ ) feature_extractor.save_pretrained(snake_case_ ) if __name__ == "__main__": SCREAMING_SNAKE_CASE_: 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('--checkpoint_path', default=None, type=str, help='Path to fairseq checkpoint') parser.add_argument('--dict_path', default=None, type=str, help='Path to dict of fine-tuned model') parser.add_argument( '--encoder_config_path', default='facebook/wav2vec2-large-lv60', type=str, help='Path to hf encoder wav2vec2 checkpoint config', ) parser.add_argument( '--decoder_config_path', default='facebook/s2t-small-mustc-en-fr-st', type=str, help='Path to hf decoder s2t checkpoint config', ) parser.add_argument('--vocab_size', default=1_02_24, type=int, help='Vocab size of decoder') parser.add_argument('--num_decoder_layers', default=7, type=int, help='Number of decoder layers') SCREAMING_SNAKE_CASE_: Dict =parser.parse_args() convert_wavaveca_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.dict_path, encoder_config_path=args.encoder_config_path, decoder_config_path=args.decoder_config_path, vocab_size=args.vocab_size, num_decoder_layers=args.num_decoder_layers, )
106
0
"""simple docstring""" from math import sqrt import numpy as np from sympy import symbols # Coefficient # Speed of light (m/s) __UpperCAmelCase = 2_99_79_24_58 # Symbols __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase = symbols('ct x y z') def _snake_case ( lowercase__ : float ) -> float: '''simple docstring''' if velocity > c: raise ValueError("""Speed must not exceed light speed 299,792,458 [m/s]!""" ) elif velocity < 1: # Usually the speed should be much higher than 1 (c order of magnitude) raise ValueError("""Speed must be greater than or equal to 1!""" ) return velocity / c def _snake_case ( lowercase__ : float ) -> float: '''simple docstring''' return 1 / sqrt(1 - beta(lowercase__ ) ** 2 ) def _snake_case ( lowercase__ : float ) -> np.ndarray: '''simple docstring''' return np.array( [ [gamma(lowercase__ ), -gamma(lowercase__ ) * beta(lowercase__ ), 0, 0], [-gamma(lowercase__ ) * beta(lowercase__ ), gamma(lowercase__ ), 0, 0], [0, 0, 1, 0], [0, 0, 0, 1], ] ) def _snake_case ( lowercase__ : float , lowercase__ : np.ndarray | None = None ) -> np.ndarray: '''simple docstring''' if event is None: lowerCAmelCase_ :Tuple = np.array([ct, x, y, z] ) # Symbolic four vector else: event[0] *= c # x0 is ct (speed of light * time) return transformation_matrix(lowercase__ ) @ event if __name__ == "__main__": import doctest doctest.testmod() # Example of symbolic vector: __UpperCAmelCase = transform(29_97_92_45) print('Example of four vector: ') print(F"""ct' = {four_vector[0]}""") print(F"""x' = {four_vector[1]}""") print(F"""y' = {four_vector[2]}""") print(F"""z' = {four_vector[3]}""") # Substitute symbols with numerical values __UpperCAmelCase = {ct: c, x: 1, y: 1, z: 1} __UpperCAmelCase = [four_vector[i].subs(sub_dict) for i in range(4)] print(F"""\n{numerical_vector}""")
84
"""simple docstring""" import os import unittest from transformers import MobileBertTokenizer, MobileBertTokenizerFast from transformers.models.bert.tokenization_bert import ( VOCAB_FILES_NAMES, BasicTokenizer, WordpieceTokenizer, _is_control, _is_punctuation, _is_whitespace, ) from transformers.testing_utils import require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin, filter_non_english @require_tokenizers class a ( __snake_case , unittest.TestCase ): SCREAMING_SNAKE_CASE : List[str] = MobileBertTokenizer SCREAMING_SNAKE_CASE : int = MobileBertTokenizerFast SCREAMING_SNAKE_CASE : Union[str, Any] = True SCREAMING_SNAKE_CASE : Optional[Any] = True SCREAMING_SNAKE_CASE : Dict = filter_non_english SCREAMING_SNAKE_CASE : str = """google/mobilebert-uncased""" def UpperCamelCase ( self : List[str] ) -> Dict: super().setUp() lowerCamelCase_ = [ '[UNK]', '[CLS]', '[SEP]', '[PAD]', '[MASK]', 'want', '##want', '##ed', 'wa', 'un', 'runn', '##ing', ',', 'low', 'lowest', ] lowerCamelCase_ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['vocab_file'] ) with open(self.vocab_file , 'w' , encoding='utf-8' ) as vocab_writer: vocab_writer.write(''.join([x + '\n' for x in vocab_tokens] ) ) lowerCamelCase_ = [ (tokenizer_def[0], self.pre_trained_model_path, tokenizer_def[2]) # else the 'google/' prefix is stripped for tokenizer_def in self.tokenizers_list ] def UpperCamelCase ( self : List[Any] , __SCREAMING_SNAKE_CASE : Union[str, Any] ) -> int: lowerCamelCase_ = 'UNwant\u00E9d,running' lowerCamelCase_ = 'unwanted, running' return input_text, output_text def UpperCamelCase ( self : Dict ) -> Any: lowerCamelCase_ = self.tokenizer_class(self.vocab_file ) lowerCamelCase_ = tokenizer.tokenize('UNwant\u00E9d,running' ) self.assertListEqual(__SCREAMING_SNAKE_CASE , ['un', '##want', '##ed', ',', 'runn', '##ing'] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) , [9, 6, 7, 12, 10, 11] ) def UpperCamelCase ( self : Dict ) -> Dict: if not self.test_rust_tokenizer: return lowerCamelCase_ = self.get_tokenizer() lowerCamelCase_ = self.get_rust_tokenizer() lowerCamelCase_ = 'UNwant\u00E9d,running' lowerCamelCase_ = tokenizer.tokenize(__SCREAMING_SNAKE_CASE ) lowerCamelCase_ = rust_tokenizer.tokenize(__SCREAMING_SNAKE_CASE ) self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) lowerCamelCase_ = tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) lowerCamelCase_ = rust_tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) lowerCamelCase_ = self.get_rust_tokenizer() lowerCamelCase_ = tokenizer.encode(__SCREAMING_SNAKE_CASE ) lowerCamelCase_ = rust_tokenizer.encode(__SCREAMING_SNAKE_CASE ) self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # With lower casing lowerCamelCase_ = self.get_tokenizer(do_lower_case=__SCREAMING_SNAKE_CASE ) lowerCamelCase_ = self.get_rust_tokenizer(do_lower_case=__SCREAMING_SNAKE_CASE ) lowerCamelCase_ = 'UNwant\u00E9d,running' lowerCamelCase_ = tokenizer.tokenize(__SCREAMING_SNAKE_CASE ) lowerCamelCase_ = rust_tokenizer.tokenize(__SCREAMING_SNAKE_CASE ) self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) lowerCamelCase_ = tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) lowerCamelCase_ = rust_tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) lowerCamelCase_ = self.get_rust_tokenizer() lowerCamelCase_ = tokenizer.encode(__SCREAMING_SNAKE_CASE ) lowerCamelCase_ = rust_tokenizer.encode(__SCREAMING_SNAKE_CASE ) self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def UpperCamelCase ( self : List[str] ) -> str: lowerCamelCase_ = BasicTokenizer() self.assertListEqual(tokenizer.tokenize('ah\u535A\u63A8zz' ) , ['ah', '\u535A', '\u63A8', 'zz'] ) def UpperCamelCase ( self : List[Any] ) -> Optional[int]: lowerCamelCase_ = BasicTokenizer(do_lower_case=__SCREAMING_SNAKE_CASE ) self.assertListEqual( tokenizer.tokenize(' \tHeLLo!how \n Are yoU? ' ) , ['hello', '!', 'how', 'are', 'you', '?'] ) self.assertListEqual(tokenizer.tokenize('H\u00E9llo' ) , ['hello'] ) def UpperCamelCase ( self : Any ) -> Any: lowerCamelCase_ = BasicTokenizer(do_lower_case=__SCREAMING_SNAKE_CASE , strip_accents=__SCREAMING_SNAKE_CASE ) self.assertListEqual( tokenizer.tokenize(' \tHäLLo!how \n Are yoU? ' ) , ['hällo', '!', 'how', 'are', 'you', '?'] ) self.assertListEqual(tokenizer.tokenize('H\u00E9llo' ) , ['h\u00E9llo'] ) def UpperCamelCase ( self : Tuple ) -> Union[str, Any]: lowerCamelCase_ = BasicTokenizer(do_lower_case=__SCREAMING_SNAKE_CASE , strip_accents=__SCREAMING_SNAKE_CASE ) self.assertListEqual( tokenizer.tokenize(' \tHäLLo!how \n Are yoU? ' ) , ['hallo', '!', 'how', 'are', 'you', '?'] ) self.assertListEqual(tokenizer.tokenize('H\u00E9llo' ) , ['hello'] ) def UpperCamelCase ( self : Tuple ) -> List[str]: lowerCamelCase_ = BasicTokenizer(do_lower_case=__SCREAMING_SNAKE_CASE ) self.assertListEqual( tokenizer.tokenize(' \tHäLLo!how \n Are yoU? ' ) , ['hallo', '!', 'how', 'are', 'you', '?'] ) self.assertListEqual(tokenizer.tokenize('H\u00E9llo' ) , ['hello'] ) def UpperCamelCase ( self : Optional[int] ) -> Any: lowerCamelCase_ = BasicTokenizer(do_lower_case=__SCREAMING_SNAKE_CASE ) self.assertListEqual( tokenizer.tokenize(' \tHeLLo!how \n Are yoU? ' ) , ['HeLLo', '!', 'how', 'Are', 'yoU', '?'] ) def UpperCamelCase ( self : Tuple ) -> Tuple: lowerCamelCase_ = BasicTokenizer(do_lower_case=__SCREAMING_SNAKE_CASE , strip_accents=__SCREAMING_SNAKE_CASE ) self.assertListEqual( tokenizer.tokenize(' \tHäLLo!how \n Are yoU? ' ) , ['HäLLo', '!', 'how', 'Are', 'yoU', '?'] ) def UpperCamelCase ( self : List[str] ) -> List[Any]: lowerCamelCase_ = BasicTokenizer(do_lower_case=__SCREAMING_SNAKE_CASE , strip_accents=__SCREAMING_SNAKE_CASE ) self.assertListEqual( tokenizer.tokenize(' \tHäLLo!how \n Are yoU? ' ) , ['HaLLo', '!', 'how', 'Are', 'yoU', '?'] ) def UpperCamelCase ( self : Tuple ) -> str: lowerCamelCase_ = BasicTokenizer(do_lower_case=__SCREAMING_SNAKE_CASE , never_split=['[UNK]'] ) self.assertListEqual( tokenizer.tokenize(' \tHeLLo!how \n Are yoU? [UNK]' ) , ['HeLLo', '!', 'how', 'Are', 'yoU', '?', '[UNK]'] ) def UpperCamelCase ( self : List[str] ) -> Any: lowerCamelCase_ = ['[UNK]', '[CLS]', '[SEP]', 'want', '##want', '##ed', 'wa', 'un', 'runn', '##ing'] lowerCamelCase_ = {} for i, token in enumerate(__SCREAMING_SNAKE_CASE ): lowerCamelCase_ = i lowerCamelCase_ = WordpieceTokenizer(vocab=__SCREAMING_SNAKE_CASE , unk_token='[UNK]' ) self.assertListEqual(tokenizer.tokenize('' ) , [] ) self.assertListEqual(tokenizer.tokenize('unwanted running' ) , ['un', '##want', '##ed', 'runn', '##ing'] ) self.assertListEqual(tokenizer.tokenize('unwantedX running' ) , ['[UNK]', 'runn', '##ing'] ) def UpperCamelCase ( self : List[Any] ) -> Any: self.assertTrue(_is_whitespace(' ' ) ) self.assertTrue(_is_whitespace('\t' ) ) self.assertTrue(_is_whitespace('\r' ) ) self.assertTrue(_is_whitespace('\n' ) ) self.assertTrue(_is_whitespace('\u00A0' ) ) self.assertFalse(_is_whitespace('A' ) ) self.assertFalse(_is_whitespace('-' ) ) def UpperCamelCase ( self : Union[str, Any] ) -> int: self.assertTrue(_is_control('\u0005' ) ) self.assertFalse(_is_control('A' ) ) self.assertFalse(_is_control(' ' ) ) self.assertFalse(_is_control('\t' ) ) self.assertFalse(_is_control('\r' ) ) def UpperCamelCase ( self : str ) -> Optional[Any]: self.assertTrue(_is_punctuation('-' ) ) self.assertTrue(_is_punctuation('$' ) ) self.assertTrue(_is_punctuation('`' ) ) self.assertTrue(_is_punctuation('.' ) ) self.assertFalse(_is_punctuation('A' ) ) self.assertFalse(_is_punctuation(' ' ) ) def UpperCamelCase ( self : int ) -> List[Any]: lowerCamelCase_ = self.get_tokenizer() lowerCamelCase_ = self.get_rust_tokenizer() # Example taken from the issue https://github.com/huggingface/tokenizers/issues/340 self.assertListEqual([tokenizer.tokenize(__SCREAMING_SNAKE_CASE ) for t in ['Test', '\xad', 'test']] , [['[UNK]'], [], ['[UNK]']] ) self.assertListEqual( [rust_tokenizer.tokenize(__SCREAMING_SNAKE_CASE ) for t in ['Test', '\xad', 'test']] , [['[UNK]'], [], ['[UNK]']] ) @slow def UpperCamelCase ( self : Dict ) -> List[str]: lowerCamelCase_ = self.tokenizer_class.from_pretrained('google/mobilebert-uncased' ) lowerCamelCase_ = tokenizer.encode('sequence builders' , add_special_tokens=__SCREAMING_SNAKE_CASE ) lowerCamelCase_ = tokenizer.encode('multi-sequence build' , add_special_tokens=__SCREAMING_SNAKE_CASE ) lowerCamelCase_ = tokenizer.build_inputs_with_special_tokens(__SCREAMING_SNAKE_CASE ) lowerCamelCase_ = tokenizer.build_inputs_with_special_tokens(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) assert encoded_sentence == [101] + text + [102] assert encoded_pair == [101] + text + [102] + text_a + [102] def UpperCamelCase ( self : Tuple ) -> Union[str, Any]: for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): lowerCamelCase_ = self.rust_tokenizer_class.from_pretrained(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) lowerCamelCase_ = F'''A, naïve {tokenizer_r.mask_token} AllenNLP sentence.''' lowerCamelCase_ = tokenizer_r.encode_plus( __SCREAMING_SNAKE_CASE , return_attention_mask=__SCREAMING_SNAKE_CASE , return_token_type_ids=__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , ) lowerCamelCase_ = tokenizer_r.do_lower_case if hasattr(__SCREAMING_SNAKE_CASE , 'do_lower_case' ) else False lowerCamelCase_ = ( [ ((0, 0), tokenizer_r.cls_token), ((0, 1), 'A'), ((1, 2), ','), ((3, 5), 'na'), ((5, 6), '##ï'), ((6, 8), '##ve'), ((9, 15), tokenizer_r.mask_token), ((16, 21), 'Allen'), ((21, 23), '##NL'), ((23, 24), '##P'), ((25, 33), 'sentence'), ((33, 34), '.'), ((0, 0), tokenizer_r.sep_token), ] if not do_lower_case else [ ((0, 0), tokenizer_r.cls_token), ((0, 1), 'a'), ((1, 2), ','), ((3, 8), 'naive'), ((9, 15), tokenizer_r.mask_token), ((16, 21), 'allen'), ((21, 23), '##nl'), ((23, 24), '##p'), ((25, 33), 'sentence'), ((33, 34), '.'), ((0, 0), tokenizer_r.sep_token), ] ) self.assertEqual( [e[1] for e in expected_results] , tokenizer_r.convert_ids_to_tokens(tokens['input_ids'] ) ) self.assertEqual([e[0] for e in expected_results] , tokens['offset_mapping'] ) def UpperCamelCase ( self : Any ) -> List[Any]: lowerCamelCase_ = ['的', '人', '有'] lowerCamelCase_ = ''.join(__SCREAMING_SNAKE_CASE ) for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): lowerCamelCase_ = True lowerCamelCase_ = self.tokenizer_class.from_pretrained(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) lowerCamelCase_ = self.rust_tokenizer_class.from_pretrained(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) lowerCamelCase_ = tokenizer_p.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) lowerCamelCase_ = tokenizer_r.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) lowerCamelCase_ = tokenizer_r.convert_ids_to_tokens(__SCREAMING_SNAKE_CASE ) lowerCamelCase_ = tokenizer_p.convert_ids_to_tokens(__SCREAMING_SNAKE_CASE ) # it is expected that each Chinese character is not preceded by "##" self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) lowerCamelCase_ = False lowerCamelCase_ = self.rust_tokenizer_class.from_pretrained(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) lowerCamelCase_ = self.tokenizer_class.from_pretrained(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) lowerCamelCase_ = tokenizer_r.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) lowerCamelCase_ = tokenizer_p.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) lowerCamelCase_ = tokenizer_r.convert_ids_to_tokens(__SCREAMING_SNAKE_CASE ) lowerCamelCase_ = tokenizer_p.convert_ids_to_tokens(__SCREAMING_SNAKE_CASE ) # it is expected that only the first Chinese character is not preceded by "##". lowerCamelCase_ = [ F'''##{token}''' if idx != 0 else token for idx, token in enumerate(__SCREAMING_SNAKE_CASE ) ] self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE )
183
0
import inspect import tempfile import unittest from huggingface_hub import hf_hub_download from transformers import is_torch_available from transformers.testing_utils import is_flaky, require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin __UpperCamelCase : int = 1E-4 if is_torch_available(): import torch from transformers import AutoformerConfig, AutoformerForPrediction, AutoformerModel from transformers.models.autoformer.modeling_autoformer import AutoformerDecoder, AutoformerEncoder @require_torch class __magic_name__ : def __init__( self : int , lowerCamelCase__ : str , lowerCamelCase__ : Any=16 , lowerCamelCase__ : List[str]=13 , lowerCamelCase__ : str=7 , lowerCamelCase__ : Tuple=14 , lowerCamelCase__ : Optional[Any]=10 , lowerCamelCase__ : int=19 , lowerCamelCase__ : List[Any]=5 , lowerCamelCase__ : Optional[int]=4 , lowerCamelCase__ : List[Any]=True , lowerCamelCase__ : Optional[int]=16 , lowerCamelCase__ : List[Any]=2 , lowerCamelCase__ : Any=4 , lowerCamelCase__ : Optional[int]=4 , lowerCamelCase__ : str="gelu" , lowerCamelCase__ : Optional[Any]=0.1 , lowerCamelCase__ : Any=0.1 , lowerCamelCase__ : List[str]=[1, 2, 3, 4, 5] , lowerCamelCase__ : Optional[Any]=25 , lowerCamelCase__ : Optional[Any]=5 , ) -> List[str]: '''simple docstring''' UpperCamelCase__ : Tuple = d_model UpperCamelCase__ : Dict = parent UpperCamelCase__ : str = batch_size UpperCamelCase__ : Any = prediction_length UpperCamelCase__ : Union[str, Any] = context_length UpperCamelCase__ : Tuple = cardinality UpperCamelCase__ : Optional[int] = num_time_features UpperCamelCase__ : List[str] = lags_sequence UpperCamelCase__ : int = embedding_dimension UpperCamelCase__ : Dict = is_training UpperCamelCase__ : List[Any] = hidden_size UpperCamelCase__ : List[str] = num_hidden_layers UpperCamelCase__ : Union[str, Any] = num_attention_heads UpperCamelCase__ : Optional[Any] = intermediate_size UpperCamelCase__ : Tuple = hidden_act UpperCamelCase__ : List[Any] = hidden_dropout_prob UpperCamelCase__ : Tuple = attention_probs_dropout_prob UpperCamelCase__ : Optional[int] = context_length UpperCamelCase__ : List[str] = prediction_length + label_length UpperCamelCase__ : List[str] = label_length UpperCamelCase__ : Dict = moving_average UpperCamelCase__ : Any = autocorrelation_factor def UpperCAmelCase__ ( self : Union[str, Any] ) -> Optional[int]: '''simple docstring''' return AutoformerConfig( d_model=self.d_model , encoder_layers=self.num_hidden_layers , decoder_layers=self.num_hidden_layers , encoder_attention_heads=self.num_attention_heads , decoder_attention_heads=self.num_attention_heads , encoder_ffn_dim=self.intermediate_size , decoder_ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , prediction_length=self.prediction_length , context_length=self.context_length , label_length=self.label_length , lags_sequence=self.lags_sequence , num_time_features=self.num_time_features , num_static_categorical_features=1 , cardinality=[self.cardinality] , embedding_dimension=[self.embedding_dimension] , moving_average=self.moving_average , ) def UpperCAmelCase__ ( self : List[Any] , lowerCamelCase__ : Tuple ) -> Tuple: '''simple docstring''' UpperCamelCase__ : Optional[int] = config.context_length + max(config.lags_sequence ) UpperCamelCase__ : Optional[Any] = ids_tensor([self.batch_size, 1] , config.cardinality[0] ) UpperCamelCase__ : Optional[int] = floats_tensor([self.batch_size, _past_length, config.num_time_features] ) UpperCamelCase__ : Union[str, Any] = floats_tensor([self.batch_size, _past_length] ) UpperCamelCase__ : Tuple = floats_tensor([self.batch_size, _past_length] ) > 0.5 # decoder inputs UpperCamelCase__ : Dict = floats_tensor([self.batch_size, config.prediction_length, config.num_time_features] ) UpperCamelCase__ : Union[str, Any] = floats_tensor([self.batch_size, config.prediction_length] ) UpperCamelCase__ : List[Any] = { '''past_values''': past_values, '''static_categorical_features''': static_categorical_features, '''past_time_features''': past_time_features, '''past_observed_mask''': past_observed_mask, '''future_time_features''': future_time_features, '''future_values''': future_values, } return inputs_dict def UpperCAmelCase__ ( self : Optional[int] ) -> Dict: '''simple docstring''' UpperCamelCase__ : Optional[Any] = self.get_config() UpperCamelCase__ : Tuple = self.prepare_autoformer_inputs_dict(lowerCamelCase__ ) return config, inputs_dict def UpperCAmelCase__ ( self : str ) -> Dict: '''simple docstring''' UpperCamelCase__ , UpperCamelCase__ : Optional[Any] = self.prepare_config_and_inputs() return config, inputs_dict def UpperCAmelCase__ ( self : str , lowerCamelCase__ : Union[str, Any] , lowerCamelCase__ : int ) -> int: '''simple docstring''' UpperCamelCase__ : Tuple = AutoformerModel(config=lowerCamelCase__ ).to(lowerCamelCase__ ).eval() UpperCamelCase__ : Any = model(**lowerCamelCase__ ) UpperCamelCase__ : Dict = outputs.encoder_last_hidden_state UpperCamelCase__ : Dict = outputs.last_hidden_state with tempfile.TemporaryDirectory() as tmpdirname: UpperCamelCase__ : str = model.get_encoder() encoder.save_pretrained(lowerCamelCase__ ) UpperCamelCase__ : int = AutoformerEncoder.from_pretrained(lowerCamelCase__ ).to(lowerCamelCase__ ) UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ : Union[str, Any] = model.create_network_inputs(**lowerCamelCase__ ) UpperCamelCase__ , UpperCamelCase__ : int = model.decomposition_layer(transformer_inputs[:, : config.context_length, ...] ) UpperCamelCase__ : Union[str, Any] = torch.cat( (transformer_inputs[:, : config.context_length, ...], feature[:, : config.context_length, ...]) , dim=-1 , ) UpperCamelCase__ : Optional[Any] = encoder(inputs_embeds=lowerCamelCase__ )[0] self.parent.assertTrue((encoder_last_hidden_state_a - encoder_last_hidden_state).abs().max().item() < 1E-3 ) UpperCamelCase__ : List[Any] = ( torch.mean(transformer_inputs[:, : config.context_length, ...] , dim=1 ) .unsqueeze(1 ) .repeat(1 , config.prediction_length , 1 ) ) UpperCamelCase__ : List[str] = torch.zeros( [transformer_inputs.shape[0], config.prediction_length, transformer_inputs.shape[2]] , device=enc_input.device , ) UpperCamelCase__ : int = torch.cat( ( torch.cat((seasonal_input[:, -config.label_length :, ...], zeros) , dim=1 ), feature[:, config.context_length - config.label_length :, ...], ) , dim=-1 , ) UpperCamelCase__ : Tuple = torch.cat( ( torch.cat((trend_input[:, -config.label_length :, ...], mean) , dim=1 ), feature[:, config.context_length - config.label_length :, ...], ) , dim=-1 , ) with tempfile.TemporaryDirectory() as tmpdirname: UpperCamelCase__ : List[Any] = model.get_decoder() decoder.save_pretrained(lowerCamelCase__ ) UpperCamelCase__ : Dict = AutoformerDecoder.from_pretrained(lowerCamelCase__ ).to(lowerCamelCase__ ) UpperCamelCase__ : str = decoder( trend=lowerCamelCase__ , inputs_embeds=lowerCamelCase__ , encoder_hidden_states=lowerCamelCase__ , )[0] self.parent.assertTrue((last_hidden_state_a - last_hidden_state).abs().max().item() < 1E-3 ) @require_torch class __magic_name__ ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , unittest.TestCase): A: Any = (AutoformerModel, AutoformerForPrediction) if is_torch_available() else () A: str = (AutoformerForPrediction,) if is_torch_available() else () A: int = {"feature-extraction": AutoformerModel} if is_torch_available() else {} A: List[str] = False A: Any = False A: Optional[int] = False A: List[Any] = False A: List[Any] = False A: Dict = False def UpperCAmelCase__ ( self : Any ) -> str: '''simple docstring''' UpperCamelCase__ : Union[str, Any] = AutoformerModelTester(self ) UpperCamelCase__ : Dict = ConfigTester(self , config_class=lowerCamelCase__ , has_text_modality=lowerCamelCase__ ) def UpperCAmelCase__ ( self : List[Any] ) -> Union[str, Any]: '''simple docstring''' self.config_tester.run_common_tests() def UpperCAmelCase__ ( self : Optional[int] ) -> Optional[Any]: '''simple docstring''' UpperCamelCase__ , UpperCamelCase__ : Union[str, Any] = self.model_tester.prepare_config_and_inputs() for model_class in self.all_model_classes: UpperCamelCase__ : Optional[int] = model_class(lowerCamelCase__ ) with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(lowerCamelCase__ ) UpperCamelCase__ , UpperCamelCase__ : str = model_class.from_pretrained(lowerCamelCase__ , output_loading_info=lowerCamelCase__ ) self.assertEqual(info['''missing_keys'''] , [] ) def UpperCAmelCase__ ( self : Dict ) -> List[Any]: '''simple docstring''' UpperCamelCase__ : Dict = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_encoder_decoder_model_standalone(*lowerCamelCase__ ) @unittest.skip(reason='''Model has no tokens embeddings''' ) def UpperCAmelCase__ ( self : Tuple ) -> Dict: '''simple docstring''' pass def UpperCAmelCase__ ( self : int ) -> Optional[Any]: '''simple docstring''' UpperCamelCase__ : int = inspect.signature(getattr(lowerCamelCase__ , '''forward''' ) ) # The main input is the name of the argument after `self` UpperCamelCase__ : str = list(model_signature.parameters.keys() )[1] self.assertEqual(AutoformerModel.main_input_name , lowerCamelCase__ ) def UpperCAmelCase__ ( self : Tuple ) -> Dict: '''simple docstring''' UpperCamelCase__ , UpperCamelCase__ : Dict = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCamelCase__ : Optional[Any] = model_class(lowerCamelCase__ ) UpperCamelCase__ : Any = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic UpperCamelCase__ : Dict = [*signature.parameters.keys()] UpperCamelCase__ : Optional[int] = [ '''past_values''', '''past_time_features''', '''past_observed_mask''', '''static_categorical_features''', '''static_real_features''', '''future_values''', '''future_time_features''', ] if model.__class__.__name__ in ["AutoformerForPrediction"]: expected_arg_names.append('''future_observed_mask''' ) expected_arg_names.extend( [ '''decoder_attention_mask''', '''head_mask''', '''decoder_head_mask''', '''cross_attn_head_mask''', '''encoder_outputs''', '''past_key_values''', '''output_hidden_states''', '''output_attentions''', '''use_cache''', '''return_dict''', ] ) self.assertListEqual(arg_names[: len(lowerCamelCase__ )] , lowerCamelCase__ ) def UpperCAmelCase__ ( self : Tuple ) -> Dict: '''simple docstring''' UpperCamelCase__ , UpperCamelCase__ : Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() UpperCamelCase__ : List[str] = True UpperCamelCase__ : Any = getattr(self.model_tester , '''seq_length''' , lowerCamelCase__ ) UpperCamelCase__ : int = getattr(self.model_tester , '''decoder_seq_length''' , lowerCamelCase__ ) UpperCamelCase__ : str = getattr(self.model_tester , '''encoder_seq_length''' , lowerCamelCase__ ) UpperCamelCase__ : int = getattr(self.model_tester , '''d_model''' , lowerCamelCase__ ) UpperCamelCase__ : List[str] = getattr(self.model_tester , '''num_attention_heads''' , lowerCamelCase__ ) UpperCamelCase__ : Dict = d_model // num_attention_heads for model_class in self.all_model_classes: UpperCamelCase__ : str = True UpperCamelCase__ : Any = False UpperCamelCase__ : str = True UpperCamelCase__ : Union[str, Any] = model_class(lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() with torch.no_grad(): UpperCamelCase__ : Any = model(**self._prepare_for_class(lowerCamelCase__ , lowerCamelCase__ ) ) UpperCamelCase__ : Tuple = outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions self.assertEqual(len(lowerCamelCase__ ) , self.model_tester.num_hidden_layers ) # check that output_attentions also work using config del inputs_dict["output_attentions"] UpperCamelCase__ : Dict = True UpperCamelCase__ : int = model_class(lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() with torch.no_grad(): UpperCamelCase__ : Any = model(**self._prepare_for_class(lowerCamelCase__ , lowerCamelCase__ ) ) UpperCamelCase__ : List[str] = outputs.encoder_attentions self.assertEqual(len(lowerCamelCase__ ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, encoder_seq_length, dim] , ) UpperCamelCase__ : Optional[Any] = len(lowerCamelCase__ ) UpperCamelCase__ : List[Any] = 7 if "last_hidden_state" in outputs: correct_outlen += 1 if "trend" in outputs: correct_outlen += 1 if "past_key_values" in outputs: correct_outlen += 1 # past_key_values have been returned if "loss" in outputs: correct_outlen += 1 if "params" in outputs: correct_outlen += 1 self.assertEqual(lowerCamelCase__ , lowerCamelCase__ ) # decoder attentions UpperCamelCase__ : Tuple = outputs.decoder_attentions self.assertIsInstance(lowerCamelCase__ , (list, tuple) ) self.assertEqual(len(lowerCamelCase__ ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(decoder_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, decoder_seq_length, dim] , ) # cross attentions UpperCamelCase__ : Any = outputs.cross_attentions self.assertIsInstance(lowerCamelCase__ , (list, tuple) ) self.assertEqual(len(lowerCamelCase__ ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(cross_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, decoder_seq_length, dim] , ) # Check attention is always last and order is fine UpperCamelCase__ : List[str] = True UpperCamelCase__ : int = True UpperCamelCase__ : Optional[int] = model_class(lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() with torch.no_grad(): UpperCamelCase__ : Optional[Any] = model(**self._prepare_for_class(lowerCamelCase__ , lowerCamelCase__ ) ) self.assertEqual(out_len + 2 , len(lowerCamelCase__ ) ) UpperCamelCase__ : Dict = outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions self.assertEqual(len(lowerCamelCase__ ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(self_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, encoder_seq_length, dim] , ) @is_flaky() def UpperCAmelCase__ ( self : List[str] ) -> Union[str, Any]: '''simple docstring''' super().test_retain_grad_hidden_states_attentions() def _a ( SCREAMING_SNAKE_CASE : Optional[Any]="train-batch.pt" ): """simple docstring""" UpperCamelCase__ : Union[str, Any] = hf_hub_download(repo_id='''hf-internal-testing/tourism-monthly-batch''' , filename=lowerCAmelCase__ , repo_type='''dataset''' ) UpperCamelCase__ : Tuple = torch.load(lowerCAmelCase__ , map_location=lowerCAmelCase__ ) return batch @require_torch @slow class __magic_name__ ( unittest.TestCase): def UpperCAmelCase__ ( self : Tuple ) -> Dict: '''simple docstring''' UpperCamelCase__ : Tuple = AutoformerModel.from_pretrained('''huggingface/autoformer-tourism-monthly''' ).to(lowerCamelCase__ ) UpperCamelCase__ : Optional[int] = prepare_batch() with torch.no_grad(): UpperCamelCase__ : Optional[Any] = model( past_values=batch['''past_values'''] , past_time_features=batch['''past_time_features'''] , past_observed_mask=batch['''past_observed_mask'''] , static_categorical_features=batch['''static_categorical_features'''] , future_values=batch['''future_values'''] , future_time_features=batch['''future_time_features'''] , )[0] UpperCamelCase__ : Dict = torch.Size( (64, model.config.prediction_length + model.config.label_length, model.config.feature_size) ) self.assertEqual(output.shape , lowerCamelCase__ ) UpperCamelCase__ : Optional[Any] = torch.tensor( [[0.3593, -1.3398, 0.6330], [0.2279, 1.5396, -0.1792], [0.0450, 1.3225, -0.2335]] , device=lowerCamelCase__ ) self.assertTrue(torch.allclose(output[0, :3, :3] , lowerCamelCase__ , atol=lowerCamelCase__ ) ) def UpperCAmelCase__ ( self : str ) -> List[str]: '''simple docstring''' UpperCamelCase__ : Optional[int] = AutoformerForPrediction.from_pretrained('''huggingface/autoformer-tourism-monthly''' ).to(lowerCamelCase__ ) UpperCamelCase__ : Dict = prepare_batch('''val-batch.pt''' ) with torch.no_grad(): UpperCamelCase__ : Optional[Any] = model( past_values=batch['''past_values'''] , past_time_features=batch['''past_time_features'''] , past_observed_mask=batch['''past_observed_mask'''] , static_categorical_features=batch['''static_categorical_features'''] , ).encoder_last_hidden_state UpperCamelCase__ : Dict = torch.Size((64, model.config.context_length, model.config.d_model) ) self.assertEqual(output.shape , lowerCamelCase__ ) UpperCamelCase__ : int = torch.tensor( [[-0.0734, -0.9036, 0.8358], [4.7186, 2.4113, 1.9581], [1.7953, 2.3558, 1.2970]] , device=lowerCamelCase__ ) self.assertTrue(torch.allclose(output[0, :3, :3] , lowerCamelCase__ , atol=lowerCamelCase__ ) ) def UpperCAmelCase__ ( self : int ) -> str: '''simple docstring''' UpperCamelCase__ : Tuple = AutoformerForPrediction.from_pretrained('''huggingface/autoformer-tourism-monthly''' ).to(lowerCamelCase__ ) UpperCamelCase__ : List[str] = prepare_batch('''val-batch.pt''' ) with torch.no_grad(): UpperCamelCase__ : List[str] = model.generate( static_categorical_features=batch['''static_categorical_features'''] , past_time_features=batch['''past_time_features'''] , past_values=batch['''past_values'''] , future_time_features=batch['''future_time_features'''] , past_observed_mask=batch['''past_observed_mask'''] , ) UpperCamelCase__ : Tuple = torch.Size((64, model.config.num_parallel_samples, model.config.prediction_length) ) self.assertEqual(outputs.sequences.shape , lowerCamelCase__ ) UpperCamelCase__ : Optional[Any] = torch.tensor([3130.6763, 4056.5293, 7053.0786] , device=lowerCamelCase__ ) UpperCamelCase__ : Dict = outputs.sequences.mean(dim=1 ) self.assertTrue(torch.allclose(mean_prediction[0, -3:] , lowerCamelCase__ , rtol=1E-1 ) )
365
import argparse import intel_extension_for_pytorch as ipex import torch from diffusers import DPMSolverMultistepScheduler, StableDiffusionPipeline __UpperCamelCase : Optional[int] = argparse.ArgumentParser("Stable Diffusion script with intel optimization", add_help=False) parser.add_argument("--dpm", action="store_true", help="Enable DPMSolver or not") parser.add_argument("--steps", default=None, type=int, help="Num inference steps") __UpperCamelCase : Optional[int] = parser.parse_args() __UpperCamelCase : Union[str, Any] = "cpu" __UpperCamelCase : Dict = "a lovely <dicoo> in red dress and hat, in the snowly and brightly night, with many brighly buildings" __UpperCamelCase : int = "path-to-your-trained-model" __UpperCamelCase : List[str] = StableDiffusionPipeline.from_pretrained(model_id) if args.dpm: __UpperCamelCase : Optional[Any] = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config) __UpperCamelCase : Optional[Any] = pipe.to(device) # to channels last __UpperCamelCase : Tuple = pipe.unet.to(memory_format=torch.channels_last) __UpperCamelCase : Optional[int] = pipe.vae.to(memory_format=torch.channels_last) __UpperCamelCase : int = pipe.text_encoder.to(memory_format=torch.channels_last) if pipe.requires_safety_checker: __UpperCamelCase : Tuple = pipe.safety_checker.to(memory_format=torch.channels_last) # optimize with ipex __UpperCamelCase : Tuple = torch.randn(2, 4, 64, 64) __UpperCamelCase : Any = torch.rand(1) * 999 __UpperCamelCase : Any = torch.randn(2, 77, 768) __UpperCamelCase : List[Any] = (sample, timestep, encoder_hidden_status) try: __UpperCamelCase : Union[str, Any] = ipex.optimize(pipe.unet.eval(), dtype=torch.bfloataa, inplace=True, sample_input=input_example) except Exception: __UpperCamelCase : str = ipex.optimize(pipe.unet.eval(), dtype=torch.bfloataa, inplace=True) __UpperCamelCase : Tuple = ipex.optimize(pipe.vae.eval(), dtype=torch.bfloataa, inplace=True) __UpperCamelCase : str = ipex.optimize(pipe.text_encoder.eval(), dtype=torch.bfloataa, inplace=True) if pipe.requires_safety_checker: __UpperCamelCase : List[Any] = ipex.optimize(pipe.safety_checker.eval(), dtype=torch.bfloataa, inplace=True) # compute __UpperCamelCase : Optional[Any] = 666 __UpperCamelCase : int = torch.Generator(device).manual_seed(seed) __UpperCamelCase : int = {"generator": generator} if args.steps is not None: __UpperCamelCase : str = args.steps with torch.cpu.amp.autocast(enabled=True, dtype=torch.bfloataa): __UpperCamelCase : str = pipe(prompt, **generate_kwargs).images[0] # save image image.save("generated.png")
51
0
'''simple docstring''' import argparse import json import os from collections import OrderedDict import torch from transformers import LukeConfig, LukeForMaskedLM, MLukeTokenizer, XLMRobertaTokenizer from transformers.tokenization_utils_base import AddedToken @torch.no_grad() def _UpperCamelCase ( __A , __A , __A , __A , __A ) -> List[str]: '''simple docstring''' with open(__A ) as metadata_file: UpperCamelCase__ = json.load(__A ) UpperCamelCase__ = LukeConfig(use_entity_aware_attention=__A , **metadata["model_config"] ) # Load in the weights from the checkpoint_path UpperCamelCase__ = torch.load(__A , map_location="cpu" )["module"] # Load the entity vocab file UpperCamelCase__ = load_original_entity_vocab(__A ) # add an entry for [MASK2] UpperCamelCase__ = max(entity_vocab.values() ) + 1 config.entity_vocab_size += 1 UpperCamelCase__ = XLMRobertaTokenizer.from_pretrained(metadata["model_config"]["bert_model_name"] ) # Add special tokens to the token vocabulary for downstream tasks UpperCamelCase__ = AddedToken("<ent>" , lstrip=__A , rstrip=__A ) UpperCamelCase__ = AddedToken("<ent2>" , lstrip=__A , rstrip=__A ) tokenizer.add_special_tokens({"additional_special_tokens": [entity_token_a, entity_token_a]} ) config.vocab_size += 2 print(F'''Saving tokenizer to {pytorch_dump_folder_path}''' ) tokenizer.save_pretrained(__A ) with open(os.path.join(__A , "tokenizer_config.json" ) , "r" ) as f: UpperCamelCase__ = json.load(__A ) UpperCamelCase__ = "MLukeTokenizer" with open(os.path.join(__A , "tokenizer_config.json" ) , "w" ) as f: json.dump(__A , __A ) with open(os.path.join(__A , MLukeTokenizer.vocab_files_names["entity_vocab_file"] ) , "w" ) as f: json.dump(__A , __A ) UpperCamelCase__ = MLukeTokenizer.from_pretrained(__A ) # Initialize the embeddings of the special tokens UpperCamelCase__ = tokenizer.convert_tokens_to_ids(["@"] )[0] UpperCamelCase__ = tokenizer.convert_tokens_to_ids(["#"] )[0] UpperCamelCase__ = state_dict["embeddings.word_embeddings.weight"] UpperCamelCase__ = word_emb[ent_init_index].unsqueeze(0 ) UpperCamelCase__ = word_emb[enta_init_index].unsqueeze(0 ) UpperCamelCase__ = torch.cat([word_emb, ent_emb, enta_emb] ) # add special tokens for 'entity_predictions.bias' for bias_name in ["lm_head.decoder.bias", "lm_head.bias"]: UpperCamelCase__ = state_dict[bias_name] UpperCamelCase__ = decoder_bias[ent_init_index].unsqueeze(0 ) UpperCamelCase__ = decoder_bias[enta_init_index].unsqueeze(0 ) UpperCamelCase__ = torch.cat([decoder_bias, ent_decoder_bias, enta_decoder_bias] ) # Initialize the query layers of the entity-aware self-attention mechanism for layer_index in range(config.num_hidden_layers ): for matrix_name in ["query.weight", "query.bias"]: UpperCamelCase__ = F'''encoder.layer.{layer_index}.attention.self.''' UpperCamelCase__ = state_dict[prefix + matrix_name] UpperCamelCase__ = state_dict[prefix + matrix_name] UpperCamelCase__ = state_dict[prefix + matrix_name] # Initialize the embedding of the [MASK2] entity using that of the [MASK] entity for downstream tasks UpperCamelCase__ = state_dict["entity_embeddings.entity_embeddings.weight"] UpperCamelCase__ = entity_emb[entity_vocab["[MASK]"]].unsqueeze(0 ) UpperCamelCase__ = torch.cat([entity_emb, entity_mask_emb] ) # add [MASK2] for 'entity_predictions.bias' UpperCamelCase__ = state_dict["entity_predictions.bias"] UpperCamelCase__ = entity_prediction_bias[entity_vocab["[MASK]"]].unsqueeze(0 ) UpperCamelCase__ = torch.cat([entity_prediction_bias, entity_mask_bias] ) UpperCamelCase__ = LukeForMaskedLM(config=__A ).eval() state_dict.pop("entity_predictions.decoder.weight" ) state_dict.pop("lm_head.decoder.weight" ) state_dict.pop("lm_head.decoder.bias" ) UpperCamelCase__ = OrderedDict() for key, value in state_dict.items(): if not (key.startswith("lm_head" ) or key.startswith("entity_predictions" )): UpperCamelCase__ = state_dict[key] else: UpperCamelCase__ = state_dict[key] UpperCamelCase__ , UpperCamelCase__ = model.load_state_dict(__A , strict=__A ) if set(__A ) != {"luke.embeddings.position_ids"}: raise ValueError(F'''Unexpected unexpected_keys: {unexpected_keys}''' ) if set(__A ) != { "lm_head.decoder.weight", "lm_head.decoder.bias", "entity_predictions.decoder.weight", }: raise ValueError(F'''Unexpected missing_keys: {missing_keys}''' ) model.tie_weights() assert (model.luke.embeddings.word_embeddings.weight == model.lm_head.decoder.weight).all() assert (model.luke.entity_embeddings.entity_embeddings.weight == model.entity_predictions.decoder.weight).all() # Check outputs UpperCamelCase__ = MLukeTokenizer.from_pretrained(__A , task="entity_classification" ) UpperCamelCase__ = "ISO 639-3 uses the code fas for the dialects spoken across Iran and アフガニスタン (Afghanistan)." UpperCamelCase__ = (0, 9) UpperCamelCase__ = tokenizer(__A , entity_spans=[span] , return_tensors="pt" ) UpperCamelCase__ = model(**__A ) # Verify word hidden states if model_size == "large": raise NotImplementedError else: # base UpperCamelCase__ = torch.Size((1, 33, 768) ) UpperCamelCase__ = torch.tensor([[0.0892, 0.0596, -0.2819], [0.0134, 0.1199, 0.0573], [-0.0169, 0.0927, 0.0644]] ) if not (outputs.last_hidden_state.shape == expected_shape): raise ValueError( F'''Outputs.last_hidden_state.shape is {outputs.last_hidden_state.shape}, Expected shape is {expected_shape}''' ) if not torch.allclose(outputs.last_hidden_state[0, :3, :3] , __A , atol=1E-4 ): raise ValueError # Verify entity hidden states if model_size == "large": raise NotImplementedError else: # base UpperCamelCase__ = torch.Size((1, 1, 768) ) UpperCamelCase__ = torch.tensor([[-0.1482, 0.0609, 0.0322]] ) if not (outputs.entity_last_hidden_state.shape == expected_shape): raise ValueError( F'''Outputs.entity_last_hidden_state.shape is {outputs.entity_last_hidden_state.shape}, Expected shape is''' F''' {expected_shape}''' ) if not torch.allclose(outputs.entity_last_hidden_state[0, :3, :3] , __A , atol=1E-4 ): raise ValueError # Verify masked word/entity prediction UpperCamelCase__ = MLukeTokenizer.from_pretrained(__A ) UpperCamelCase__ = "Tokyo is the capital of <mask>." UpperCamelCase__ = (24, 30) UpperCamelCase__ = tokenizer(__A , entity_spans=[span] , return_tensors="pt" ) UpperCamelCase__ = model(**__A ) UpperCamelCase__ = encoding["input_ids"][0].tolist() UpperCamelCase__ = input_ids.index(tokenizer.convert_tokens_to_ids("<mask>" ) ) UpperCamelCase__ = outputs.logits[0][mask_position_id].argmax(dim=-1 ) assert "Japan" == tokenizer.decode(__A ) UpperCamelCase__ = outputs.entity_logits[0][0].argmax().item() UpperCamelCase__ = [ entity for entity, entity_id in tokenizer.entity_vocab.items() if entity_id == predicted_entity_id ] assert [e for e in multilingual_predicted_entities if e.startswith("en:" )][0] == "en:Japan" # Finally, save our PyTorch model and tokenizer print("Saving PyTorch model to {}".format(__A ) ) model.save_pretrained(__A ) def _UpperCamelCase ( __A ) -> Optional[int]: '''simple docstring''' UpperCamelCase__ = ["[MASK]", "[PAD]", "[UNK]"] UpperCamelCase__ = [json.loads(__A ) for line in open(__A )] UpperCamelCase__ = {} for entry in data: UpperCamelCase__ = entry["id"] for entity_name, language in entry["entities"]: if entity_name in SPECIAL_TOKENS: UpperCamelCase__ = entity_id break UpperCamelCase__ = F'''{language}:{entity_name}''' UpperCamelCase__ = entity_id return new_mapping if __name__ == "__main__": a__ : Tuple = argparse.ArgumentParser() # Required parameters parser.add_argument('--checkpoint_path', type=str, help='Path to a pytorch_model.bin file.') parser.add_argument( '--metadata_path', default=None, type=str, help='Path to a metadata.json file, defining the configuration.' ) parser.add_argument( '--entity_vocab_path', default=None, type=str, help='Path to an entity_vocab.tsv file, containing the entity vocabulary.', ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, help='Path to where to dump the output PyTorch model.' ) parser.add_argument( '--model_size', default='base', type=str, choices=['base', 'large'], help='Size of the model to be converted.' ) a__ : Any = parser.parse_args() convert_luke_checkpoint( args.checkpoint_path, args.metadata_path, args.entity_vocab_path, args.pytorch_dump_folder_path, args.model_size, )
80
import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_mobilebert import MobileBertTokenizer SCREAMING_SNAKE_CASE :Any = logging.get_logger(__name__) SCREAMING_SNAKE_CASE :Union[str, Any] = {'''vocab_file''': '''vocab.txt''', '''tokenizer_file''': '''tokenizer.json'''} SCREAMING_SNAKE_CASE :str = { '''vocab_file''': {'''mobilebert-uncased''': '''https://huggingface.co/google/mobilebert-uncased/resolve/main/vocab.txt'''}, '''tokenizer_file''': { '''mobilebert-uncased''': '''https://huggingface.co/google/mobilebert-uncased/resolve/main/tokenizer.json''' }, } SCREAMING_SNAKE_CASE :Optional[int] = {'''mobilebert-uncased''': 5_12} SCREAMING_SNAKE_CASE :Dict = {} class __lowerCAmelCase ( a ): """simple docstring""" _SCREAMING_SNAKE_CASE = VOCAB_FILES_NAMES _SCREAMING_SNAKE_CASE = PRETRAINED_VOCAB_FILES_MAP _SCREAMING_SNAKE_CASE = PRETRAINED_INIT_CONFIGURATION _SCREAMING_SNAKE_CASE = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _SCREAMING_SNAKE_CASE = MobileBertTokenizer def __init__( self : Dict , _lowerCAmelCase : Any=None , _lowerCAmelCase : List[Any]=None , _lowerCAmelCase : Tuple=True , _lowerCAmelCase : List[Any]="[UNK]" , _lowerCAmelCase : Union[str, Any]="[SEP]" , _lowerCAmelCase : List[str]="[PAD]" , _lowerCAmelCase : Tuple="[CLS]" , _lowerCAmelCase : Union[str, Any]="[MASK]" , _lowerCAmelCase : List[str]=True , _lowerCAmelCase : List[str]=None , **_lowerCAmelCase : Union[str, Any] , ) -> Optional[int]: """simple docstring""" super().__init__( _lowerCAmelCase , tokenizer_file=_lowerCAmelCase , do_lower_case=_lowerCAmelCase , unk_token=_lowerCAmelCase , sep_token=_lowerCAmelCase , pad_token=_lowerCAmelCase , cls_token=_lowerCAmelCase , mask_token=_lowerCAmelCase , tokenize_chinese_chars=_lowerCAmelCase , strip_accents=_lowerCAmelCase , **_lowerCAmelCase , ) snake_case_ = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get("lowercase" , _lowerCAmelCase ) != do_lower_case or normalizer_state.get("strip_accents" , _lowerCAmelCase ) != strip_accents or normalizer_state.get("handle_chinese_chars" , _lowerCAmelCase ) != tokenize_chinese_chars ): snake_case_ = getattr(_lowerCAmelCase , normalizer_state.pop("type" ) ) snake_case_ = do_lower_case snake_case_ = strip_accents snake_case_ = tokenize_chinese_chars snake_case_ = normalizer_class(**_lowerCAmelCase ) snake_case_ = do_lower_case def lowerCAmelCase__ ( self : str , _lowerCAmelCase : Any , _lowerCAmelCase : List[Any]=None ) -> Optional[int]: """simple docstring""" snake_case_ = [self.cls_token_id] + token_ids_a + [self.sep_token_id] if token_ids_a: output += token_ids_a + [self.sep_token_id] return output def lowerCAmelCase__ ( self : List[Any] , _lowerCAmelCase : List[int] , _lowerCAmelCase : Optional[List[int]] = None ) -> List[int]: """simple docstring""" snake_case_ = [self.sep_token_id] snake_case_ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def lowerCAmelCase__ ( self : Optional[Any] , _lowerCAmelCase : str , _lowerCAmelCase : Optional[str] = None ) -> Tuple[str]: """simple docstring""" snake_case_ = self._tokenizer.model.save(_lowerCAmelCase , name=_lowerCAmelCase ) return tuple(_lowerCAmelCase )
159
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_torch_available, ) SCREAMING_SNAKE_CASE__ = { 'configuration_mega': ['MEGA_PRETRAINED_CONFIG_ARCHIVE_MAP', 'MegaConfig', 'MegaOnnxConfig'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ 'MEGA_PRETRAINED_MODEL_ARCHIVE_LIST', 'MegaForCausalLM', 'MegaForMaskedLM', 'MegaForMultipleChoice', 'MegaForQuestionAnswering', 'MegaForSequenceClassification', 'MegaForTokenClassification', 'MegaModel', 'MegaPreTrainedModel', ] if TYPE_CHECKING: from .configuration_mega import MEGA_PRETRAINED_CONFIG_ARCHIVE_MAP, MegaConfig, MegaOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mega import ( MEGA_PRETRAINED_MODEL_ARCHIVE_LIST, MegaForCausalLM, MegaForMaskedLM, MegaForMultipleChoice, MegaForQuestionAnswering, MegaForSequenceClassification, MegaForTokenClassification, MegaModel, MegaPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE__ = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
183
'''simple docstring''' import os from typing import List, Optional, Union from ...tokenization_utils import PreTrainedTokenizer from ...tokenization_utils_base import AddedToken from ...utils import logging SCREAMING_SNAKE_CASE__ = logging.get_logger(__name__) SCREAMING_SNAKE_CASE__ = {'vocab_file': 'vocab.txt'} SCREAMING_SNAKE_CASE__ = { 'vocab_file': { 'facebook/esm2_t6_8M_UR50D': 'https://huggingface.co/facebook/esm2_t6_8M_UR50D/resolve/main/vocab.txt', 'facebook/esm2_t12_35M_UR50D': 'https://huggingface.co/facebook/esm2_t12_35M_UR50D/resolve/main/vocab.txt', }, } SCREAMING_SNAKE_CASE__ = { 'facebook/esm2_t6_8M_UR50D': 1_0_2_4, 'facebook/esm2_t12_35M_UR50D': 1_0_2_4, } def lowercase__ ( __UpperCamelCase )-> Any: with open(__UpperCamelCase , """r""" ) as f: UpperCamelCase = f.read().splitlines() return [l.strip() for l in lines] class a_ ( lowerCamelCase ): lowercase = VOCAB_FILES_NAMES lowercase = PRETRAINED_VOCAB_FILES_MAP lowercase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase = ["""input_ids""", """attention_mask"""] def __init__( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE="<unk>" , _SCREAMING_SNAKE_CASE="<cls>" , _SCREAMING_SNAKE_CASE="<pad>" , _SCREAMING_SNAKE_CASE="<mask>" , _SCREAMING_SNAKE_CASE="<eos>" , **_SCREAMING_SNAKE_CASE , ) -> Any: """simple docstring""" super().__init__(**_SCREAMING_SNAKE_CASE ) UpperCamelCase = load_vocab_file(_SCREAMING_SNAKE_CASE ) UpperCamelCase = dict(enumerate(self.all_tokens ) ) UpperCamelCase = {tok: ind for ind, tok in enumerate(self.all_tokens )} UpperCamelCase = unk_token UpperCamelCase = cls_token UpperCamelCase = pad_token UpperCamelCase = mask_token UpperCamelCase = eos_token UpperCamelCase = self.all_tokens self._create_trie(self.unique_no_split_tokens ) def A__ ( self , _SCREAMING_SNAKE_CASE ) -> str: """simple docstring""" return self._id_to_token.get(_SCREAMING_SNAKE_CASE , self.unk_token ) def A__ ( self , _SCREAMING_SNAKE_CASE ) -> int: """simple docstring""" return self._token_to_id.get(_SCREAMING_SNAKE_CASE , self._token_to_id.get(self.unk_token ) ) def A__ ( self , _SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) -> List[str]: """simple docstring""" return text.split() def A__ ( self , _SCREAMING_SNAKE_CASE=False ) -> Dict: """simple docstring""" return len(self._id_to_token ) def A__ ( self ) -> Tuple: """simple docstring""" return {token: i for i, token in enumerate(self.all_tokens )} def A__ ( self , _SCREAMING_SNAKE_CASE ) -> int: """simple docstring""" return self._token_to_id.get(_SCREAMING_SNAKE_CASE , self._token_to_id.get(self.unk_token ) ) def A__ ( self , _SCREAMING_SNAKE_CASE ) -> str: """simple docstring""" return self._id_to_token.get(_SCREAMING_SNAKE_CASE , self.unk_token ) def A__ ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None ) -> List[int]: """simple docstring""" UpperCamelCase = [self.cls_token_id] UpperCamelCase = [self.eos_token_id] # No sep token in ESM vocabulary if token_ids_a is None: if self.eos_token_id is None: return cls + token_ids_a else: return cls + token_ids_a + sep elif self.eos_token_id is None: raise ValueError("""Cannot tokenize multiple sequences when EOS token is not set!""" ) return cls + token_ids_a + sep + token_ids_a + sep # Multiple inputs always have an EOS token def A__ ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None , _SCREAMING_SNAKE_CASE = False ) -> List[int]: """simple docstring""" if already_has_special_tokens: if token_ids_a is not None: raise ValueError( """You should not supply a second sequence if the provided sequence of """ """ids is already formatted with special tokens for the model.""" ) return [1 if token in self.all_special_ids else 0 for token in token_ids_a] UpperCamelCase = [1] + ([0] * len(_SCREAMING_SNAKE_CASE )) + [1] if token_ids_a is not None: mask += [0] * len(_SCREAMING_SNAKE_CASE ) + [1] return mask def A__ ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Optional[int]: """simple docstring""" UpperCamelCase = os.path.join(_SCREAMING_SNAKE_CASE , (filename_prefix + """-""" if filename_prefix else """""") + """vocab.txt""" ) with open(_SCREAMING_SNAKE_CASE , """w""" ) as f: f.write("""\n""".join(self.all_tokens ) ) return (vocab_file,) @property def A__ ( self ) -> int: """simple docstring""" return self.get_vocab_size(with_added_tokens=_SCREAMING_SNAKE_CASE ) def A__ ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = False ) -> int: """simple docstring""" return super()._add_tokens(_SCREAMING_SNAKE_CASE , special_tokens=_SCREAMING_SNAKE_CASE )
183
1
"""simple docstring""" from math import acos, sin from typing import List, Tuple, Union import numpy as np import torch from PIL import Image from ...models import AutoencoderKL, UNetaDConditionModel from ...schedulers import DDIMScheduler, DDPMScheduler from ...utils import randn_tensor from ..pipeline_utils import AudioPipelineOutput, BaseOutput, DiffusionPipeline, ImagePipelineOutput from .mel import Mel class _UpperCAmelCase ( _A ): SCREAMING_SNAKE_CASE_ : Optional[int] = ["vqvae"] def __init__( self : Dict , A : AutoencoderKL , A : UNetaDConditionModel , A : Mel , A : Union[DDIMScheduler, DDPMScheduler] , ) -> List[str]: super().__init__() self.register_modules(unet=A , scheduler=A , mel=A , vqvae=A ) def A ( self : List[str] ) -> int: return 50 if isinstance(self.scheduler , A ) else 10_00 @torch.no_grad() def __call__( self : List[str] , A : int = 1 , A : str = None , A : np.ndarray = None , A : int = 0 , A : int = 0 , A : int = None , A : torch.Generator = None , A : float = 0 , A : float = 0 , A : torch.Generator = None , A : float = 0 , A : torch.Tensor = None , A : torch.Tensor = None , A : Tuple=True , ) -> Union[ Union[AudioPipelineOutput, ImagePipelineOutput], Tuple[List[Image.Image], Tuple[int, List[np.ndarray]]], ]: lowercase_ : Dict = steps or self.get_default_steps() self.scheduler.set_timesteps(A ) lowercase_ : List[Any] = step_generator or generator # For backwards compatibility if type(self.unet.config.sample_size ) == int: lowercase_ : List[Any] = (self.unet.config.sample_size, self.unet.config.sample_size) if noise is None: lowercase_ : int = randn_tensor( ( batch_size, self.unet.config.in_channels, self.unet.config.sample_size[0], self.unet.config.sample_size[1], ) , generator=A , device=self.device , ) lowercase_ : Dict = noise lowercase_ : List[Any] = None if audio_file is not None or raw_audio is not None: self.mel.load_audio(A , A ) lowercase_ : int = self.mel.audio_slice_to_image(A ) lowercase_ : Tuple = np.frombuffer(input_image.tobytes() , dtype='''uint8''' ).reshape( (input_image.height, input_image.width) ) lowercase_ : Any = (input_image / 2_55) * 2 - 1 lowercase_ : int = torch.tensor(input_image[np.newaxis, :, :] , dtype=torch.float ).to(self.device ) if self.vqvae is not None: lowercase_ : str = self.vqvae.encode(torch.unsqueeze(A , 0 ) ).latent_dist.sample( generator=A )[0] lowercase_ : Optional[Any] = self.vqvae.config.scaling_factor * input_images if start_step > 0: lowercase_ : Any = self.scheduler.add_noise(A , A , self.scheduler.timesteps[start_step - 1] ) lowercase_ : Optional[Any] = ( self.unet.config.sample_size[1] * self.mel.get_sample_rate() / self.mel.x_res / self.mel.hop_length ) lowercase_ : Optional[Any] = int(mask_start_secs * pixels_per_second ) lowercase_ : int = int(mask_end_secs * pixels_per_second ) lowercase_ : Union[str, Any] = self.scheduler.add_noise(A , A , torch.tensor(self.scheduler.timesteps[start_step:] ) ) for step, t in enumerate(self.progress_bar(self.scheduler.timesteps[start_step:] ) ): if isinstance(self.unet , A ): lowercase_ : Optional[int] = self.unet(A , A , A )['''sample'''] else: lowercase_ : int = self.unet(A , A )['''sample'''] if isinstance(self.scheduler , A ): lowercase_ : List[Any] = self.scheduler.step( model_output=A , timestep=A , sample=A , eta=A , generator=A , )['''prev_sample'''] else: lowercase_ : int = self.scheduler.step( model_output=A , timestep=A , sample=A , generator=A , )['''prev_sample'''] if mask is not None: if mask_start > 0: lowercase_ : List[Any] = mask[:, step, :, :mask_start] if mask_end > 0: lowercase_ : List[str] = mask[:, step, :, -mask_end:] if self.vqvae is not None: # 0.18215 was scaling factor used in training to ensure unit variance lowercase_ : Optional[Any] = 1 / self.vqvae.config.scaling_factor * images lowercase_ : str = self.vqvae.decode(A )['''sample'''] lowercase_ : Tuple = (images / 2 + 0.5).clamp(0 , 1 ) lowercase_ : Optional[Any] = images.cpu().permute(0 , 2 , 3 , 1 ).numpy() lowercase_ : List[str] = (images * 2_55).round().astype('''uint8''' ) lowercase_ : Optional[int] = list( (Image.fromarray(_[:, :, 0] ) for _ in images) if images.shape[3] == 1 else (Image.fromarray(A , mode='''RGB''' ).convert('''L''' ) for _ in images) ) lowercase_ : Any = [self.mel.image_to_audio(A ) for _ in images] if not return_dict: return images, (self.mel.get_sample_rate(), audios) return BaseOutput(**AudioPipelineOutput(np.array(A )[:, np.newaxis, :] ) , **ImagePipelineOutput(A ) ) @torch.no_grad() def A ( self : List[str] , A : List[Image.Image] , A : int = 50 ) -> np.ndarray: assert isinstance(self.scheduler , A ) self.scheduler.set_timesteps(A ) lowercase_ : List[str] = np.array( [np.frombuffer(image.tobytes() , dtype='''uint8''' ).reshape((1, image.height, image.width) ) for image in images] ) lowercase_ : Union[str, Any] = (sample / 2_55) * 2 - 1 lowercase_ : Union[str, Any] = torch.Tensor(A ).to(self.device ) for t in self.progress_bar(torch.flip(self.scheduler.timesteps , (0,) ) ): lowercase_ : Union[str, Any] = t - self.scheduler.config.num_train_timesteps // self.scheduler.num_inference_steps lowercase_ : Dict = self.scheduler.alphas_cumprod[t] lowercase_ : int = ( self.scheduler.alphas_cumprod[prev_timestep] if prev_timestep >= 0 else self.scheduler.final_alpha_cumprod ) lowercase_ : str = 1 - alpha_prod_t lowercase_ : Dict = self.unet(A , A )['''sample'''] lowercase_ : List[Any] = (1 - alpha_prod_t_prev) ** 0.5 * model_output lowercase_ : List[str] = (sample - pred_sample_direction) * alpha_prod_t_prev ** (-0.5) lowercase_ : int = sample * alpha_prod_t ** 0.5 + beta_prod_t ** 0.5 * model_output return sample @staticmethod def A ( A : torch.Tensor , A : torch.Tensor , A : float ) -> torch.Tensor: lowercase_ : List[str] = acos(torch.dot(torch.flatten(A ) , torch.flatten(A ) ) / torch.norm(A ) / torch.norm(A ) ) return sin((1 - alpha) * theta ) * xa / sin(A ) + sin(alpha * theta ) * xa / sin(A )
33
'''simple docstring''' import argparse import numpy as np import torch from transformers import SpeechTaHifiGan, SpeechTaHifiGanConfig, logging logging.set_verbosity_info() lowercase : List[str] = logging.get_logger("transformers.models.speecht5") def SCREAMING_SNAKE_CASE__ ( __A , __A , __A ) -> Dict: hf_model.apply_weight_norm() _snake_case = checkpoint['input_conv.weight_g'] _snake_case = checkpoint['input_conv.weight_v'] _snake_case = checkpoint['input_conv.bias'] for i in range(len(config.upsample_rates ) ): _snake_case = checkpoint[F'upsamples.{i}.1.weight_g'] _snake_case = checkpoint[F'upsamples.{i}.1.weight_v'] _snake_case = checkpoint[F'upsamples.{i}.1.bias'] for i in range(len(config.upsample_rates ) * len(config.resblock_kernel_sizes ) ): for j in range(len(config.resblock_dilation_sizes ) ): _snake_case = checkpoint[F'blocks.{i}.convs1.{j}.1.weight_g'] _snake_case = checkpoint[F'blocks.{i}.convs1.{j}.1.weight_v'] _snake_case = checkpoint[F'blocks.{i}.convs1.{j}.1.bias'] _snake_case = checkpoint[F'blocks.{i}.convs2.{j}.1.weight_g'] _snake_case = checkpoint[F'blocks.{i}.convs2.{j}.1.weight_v'] _snake_case = checkpoint[F'blocks.{i}.convs2.{j}.1.bias'] _snake_case = checkpoint['output_conv.1.weight_g'] _snake_case = checkpoint['output_conv.1.weight_v'] _snake_case = checkpoint['output_conv.1.bias'] hf_model.remove_weight_norm() @torch.no_grad() def SCREAMING_SNAKE_CASE__ ( __A , __A , __A , __A=None , __A=None , ) -> List[Any]: if config_path is not None: _snake_case = SpeechTaHifiGanConfig.from_pretrained(__A ) else: _snake_case = SpeechTaHifiGanConfig() _snake_case = SpeechTaHifiGan(__A ) _snake_case = torch.load(__A ) load_weights(orig_checkpoint['model']['generator'] , __A , __A ) _snake_case = np.load(__A ) _snake_case = stats[0].reshape(-1 ) _snake_case = stats[1].reshape(-1 ) _snake_case = torch.from_numpy(__A ).float() _snake_case = torch.from_numpy(__A ).float() model.save_pretrained(__A ) if repo_id: print('Pushing to the hub...' ) model.push_to_hub(__A ) if __name__ == "__main__": lowercase : Dict = argparse.ArgumentParser() parser.add_argument("--checkpoint_path", required=True, default=None, type=str, help="Path to original checkpoint") parser.add_argument("--stats_path", required=True, default=None, type=str, help="Path to stats.npy file") 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." ) lowercase : List[Any] = parser.parse_args() convert_hifigan_checkpoint( args.checkpoint_path, args.stats_path, args.pytorch_dump_folder_path, args.config_path, args.push_to_hub, )
42
0
'''simple docstring''' import math import random from typing import Any from .hill_climbing import SearchProblem def _UpperCAmelCase ( _UpperCamelCase : str, _UpperCamelCase : bool = True, _UpperCamelCase : float = math.inf, _UpperCamelCase : float = -math.inf, _UpperCamelCase : float = math.inf, _UpperCamelCase : float = -math.inf, _UpperCamelCase : bool = False, _UpperCamelCase : float = 1_00, _UpperCamelCase : float = 0.0_1, _UpperCamelCase : float = 1, ) -> Any: A_ = False A_ = search_prob A_ = start_temperate A_ = [] A_ = 0 A_ = None while not search_end: A_ = current_state.score() if best_state is None or current_score > best_state.score(): A_ = current_state scores.append(_UpperCamelCase ) iterations += 1 A_ = None A_ = current_state.get_neighbors() while ( next_state is None and neighbors ): # till we do not find a neighbor that we can move to A_ = random.randint(0, len(_UpperCamelCase ) - 1 ) # picking a random neighbor A_ = neighbors.pop(_UpperCamelCase ) A_ = picked_neighbor.score() - current_score if ( picked_neighbor.x > max_x or picked_neighbor.x < min_x or picked_neighbor.y > max_y or picked_neighbor.y < min_y ): continue # neighbor outside our bounds if not find_max: A_ = change * -1 # in case we are finding minimum if change > 0: # improves the solution A_ = picked_neighbor else: A_ = (math.e) ** ( change / current_temp ) # probability generation function if random.random() < probability: # random number within probability A_ = picked_neighbor A_ = current_temp - (current_temp * rate_of_decrease) if current_temp < threshold_temp or next_state is None: # temperature below threshold, or could not find a suitable neighbor A_ = True else: A_ = next_state if visualization: from matplotlib import pyplot as plt plt.plot(range(_UpperCamelCase ), _UpperCamelCase ) plt.xlabel('''Iterations''' ) plt.ylabel('''Function values''' ) plt.show() return best_state if __name__ == "__main__": def _UpperCAmelCase ( _UpperCamelCase : Dict, _UpperCamelCase : Any ) -> int: return (x**2) + (y**2) # starting the problem with initial coordinates (12, 47) __snake_case : Union[str, Any] = SearchProblem(x=12, y=47, step_size=1, function_to_optimize=test_fa) __snake_case : Union[str, Any] = simulated_annealing( prob, find_max=False, max_x=100, min_x=5, max_y=50, min_y=-5, visualization=True ) print( 'The minimum score for f(x, y) = x^2 + y^2 with the domain 100 > x > 5 ' F"""and 50 > y > - 5 found via hill climbing: {local_min.score()}""" ) # starting the problem with initial coordinates (12, 47) __snake_case : int = SearchProblem(x=12, y=47, step_size=1, function_to_optimize=test_fa) __snake_case : Optional[int] = simulated_annealing( prob, find_max=True, max_x=100, min_x=5, max_y=50, min_y=-5, visualization=True ) print( 'The maximum score for f(x, y) = x^2 + y^2 with the domain 100 > x > 5 ' F"""and 50 > y > - 5 found via hill climbing: {local_min.score()}""" ) def _UpperCAmelCase ( _UpperCamelCase : Optional[int], _UpperCamelCase : List[Any] ) -> int: return (3 * x**2) - (6 * y) __snake_case : str = SearchProblem(x=3, y=4, step_size=1, function_to_optimize=test_fa) __snake_case : Dict = simulated_annealing(prob, find_max=False, visualization=True) print( 'The minimum score for f(x, y) = 3*x^2 - 6*y found via hill climbing: ' F"""{local_min.score()}""" ) __snake_case : int = SearchProblem(x=3, y=4, step_size=1, function_to_optimize=test_fa) __snake_case : Any = simulated_annealing(prob, find_max=True, visualization=True) print( 'The maximum score for f(x, y) = 3*x^2 - 6*y found via hill climbing: ' F"""{local_min.score()}""" )
18
'''simple docstring''' import logging import os import sys from dataclasses import dataclass, field from typing import Optional import evaluate import numpy as np import torch from datasets import load_dataset from PIL import Image from torchvision.transforms import ( CenterCrop, Compose, Normalize, RandomHorizontalFlip, RandomResizedCrop, Resize, ToTensor, ) import transformers from transformers import ( MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING, AutoConfig, AutoImageProcessor, AutoModelForImageClassification, HfArgumentParser, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import get_last_checkpoint from transformers.utils import check_min_version, send_example_telemetry from transformers.utils.versions import require_version __snake_case : Optional[int] = logging.getLogger(__name__) # Will error if the minimal version of Transformers is not installed. Remove at your own risks. check_min_version('4.31.0') require_version('datasets>=1.8.0', 'To fix: pip install -r examples/pytorch/image-classification/requirements.txt') __snake_case : str = list(MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING.keys()) __snake_case : int = tuple(conf.model_type for conf in MODEL_CONFIG_CLASSES) def _UpperCAmelCase ( _UpperCamelCase : str ) -> int: with open(_UpperCamelCase, '''rb''' ) as f: A_ = Image.open(_UpperCamelCase ) return im.convert('''RGB''' ) @dataclass class __UpperCAmelCase : '''simple docstring''' __lowercase : Optional[str] = field( default=_UpperCamelCase , metadata={ 'help': 'Name of a dataset from the hub (could be your own, possibly private dataset hosted on the hub).' } , ) __lowercase : Optional[str] = field( default=_UpperCamelCase , metadata={'help': 'The configuration name of the dataset to use (via the datasets library).'} ) __lowercase : Optional[str] = field(default=_UpperCamelCase , metadata={'help': 'A folder containing the training data.'} ) __lowercase : Optional[str] = field(default=_UpperCamelCase , metadata={'help': 'A folder containing the validation data.'} ) __lowercase : Optional[float] = field( default=0.15 , metadata={'help': 'Percent to split off of train for validation.'} ) __lowercase : Optional[int] = field( default=_UpperCamelCase , metadata={ 'help': ( 'For debugging purposes or quicker training, truncate the number of training examples to this ' 'value if set.' ) } , ) __lowercase : Optional[int] = field( default=_UpperCamelCase , metadata={ 'help': ( 'For debugging purposes or quicker training, truncate the number of evaluation examples to this ' 'value if set.' ) } , ) def __A ( self ) -> int: if self.dataset_name is None and (self.train_dir is None and self.validation_dir is None): raise ValueError( '''You must specify either a dataset name from the hub or a train and/or validation directory.''' ) @dataclass class __UpperCAmelCase : '''simple docstring''' __lowercase : str = field( default='google/vit-base-patch16-224-in21k' , metadata={'help': 'Path to pretrained model or model identifier from huggingface.co/models'} , ) __lowercase : Optional[str] = field( default=_UpperCamelCase , metadata={'help': 'If training from scratch, pass a model type from the list: ' + ', '.join(_UpperCamelCase )} , ) __lowercase : Optional[str] = field( default=_UpperCamelCase , metadata={'help': 'Pretrained config name or path if not the same as model_name'} ) __lowercase : Optional[str] = field( default=_UpperCamelCase , metadata={'help': 'Where do you want to store the pretrained models downloaded from s3'} ) __lowercase : str = field( default='main' , metadata={'help': 'The specific model version to use (can be a branch name, tag name or commit id).'} , ) __lowercase : str = field(default=_UpperCamelCase , metadata={'help': 'Name or path of preprocessor config.'} ) __lowercase : bool = field( default=_UpperCamelCase , metadata={ 'help': ( 'Will use the token generated when running `huggingface-cli login` (necessary to use this script ' 'with private models).' ) } , ) __lowercase : bool = field( default=_UpperCamelCase , metadata={'help': 'Will enable to load a pretrained model whose head dimensions are different.'} , ) def _UpperCAmelCase ( _UpperCamelCase : str ) -> Dict: A_ = torch.stack([example['''pixel_values'''] for example in examples] ) A_ = torch.tensor([example['''labels'''] for example in examples] ) return {"pixel_values": pixel_values, "labels": labels} def _UpperCAmelCase ( ) -> Tuple: # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. A_ = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith('''.json''' ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. A_ ,A_ ,A_ = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: A_ ,A_ ,A_ = parser.parse_args_into_dataclasses() # Sending telemetry. Tracking the example usage helps us better allocate resources to maintain them. The # information sent is the one passed as arguments along with your Python/PyTorch versions. send_example_telemetry('''run_image_classification''', _UpperCamelCase, _UpperCamelCase ) # Setup logging logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''', datefmt='''%m/%d/%Y %H:%M:%S''', handlers=[logging.StreamHandler(sys.stdout )], ) if training_args.should_log: # The default of training_args.log_level is passive, so we set log level at info here to have that default. transformers.utils.logging.set_verbosity_info() A_ = training_args.get_process_log_level() logger.setLevel(_UpperCamelCase ) transformers.utils.logging.set_verbosity(_UpperCamelCase ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Log on each process the small summary: logger.warning( F'''Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}''' + F'''distributed training: {bool(training_args.local_rank != -1 )}, 16-bits training: {training_args.fpaa}''' ) logger.info(F'''Training/evaluation parameters {training_args}''' ) # Detecting last checkpoint. A_ = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: A_ = get_last_checkpoint(training_args.output_dir ) if last_checkpoint is None and len(os.listdir(training_args.output_dir ) ) > 0: raise ValueError( F'''Output directory ({training_args.output_dir}) already exists and is not empty. ''' '''Use --overwrite_output_dir to overcome.''' ) elif last_checkpoint is not None and training_args.resume_from_checkpoint is None: logger.info( F'''Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change ''' '''the `--output_dir` or add `--overwrite_output_dir` to train from scratch.''' ) # Set seed before initializing model. set_seed(training_args.seed ) # Initialize our dataset and prepare it for the 'image-classification' task. if data_args.dataset_name is not None: A_ = load_dataset( data_args.dataset_name, data_args.dataset_config_name, cache_dir=model_args.cache_dir, task='''image-classification''', use_auth_token=True if model_args.use_auth_token else None, ) else: A_ = {} if data_args.train_dir is not None: A_ = os.path.join(data_args.train_dir, '''**''' ) if data_args.validation_dir is not None: A_ = os.path.join(data_args.validation_dir, '''**''' ) A_ = load_dataset( '''imagefolder''', data_files=_UpperCamelCase, cache_dir=model_args.cache_dir, task='''image-classification''', ) # If we don't have a validation split, split off a percentage of train as validation. A_ = None if '''validation''' in dataset.keys() else data_args.train_val_split if isinstance(data_args.train_val_split, _UpperCamelCase ) and data_args.train_val_split > 0.0: A_ = dataset['''train'''].train_test_split(data_args.train_val_split ) A_ = split['''train'''] A_ = split['''test'''] # Prepare label mappings. # We'll include these in the model's config to get human readable labels in the Inference API. A_ = dataset['''train'''].features['''labels'''].names A_ ,A_ = {}, {} for i, label in enumerate(_UpperCamelCase ): A_ = str(_UpperCamelCase ) A_ = label # Load the accuracy metric from the datasets package A_ = evaluate.load('''accuracy''' ) # Define our compute_metrics function. It takes an `EvalPrediction` object (a namedtuple with a # predictions and label_ids field) and has to return a dictionary string to float. def compute_metrics(_UpperCamelCase : Optional[Any] ): return metric.compute(predictions=np.argmax(p.predictions, axis=1 ), references=p.label_ids ) A_ = AutoConfig.from_pretrained( model_args.config_name or model_args.model_name_or_path, num_labels=len(_UpperCamelCase ), labelaid=_UpperCamelCase, idalabel=_UpperCamelCase, finetuning_task='''image-classification''', cache_dir=model_args.cache_dir, revision=model_args.model_revision, use_auth_token=True if model_args.use_auth_token else None, ) A_ = AutoModelForImageClassification.from_pretrained( model_args.model_name_or_path, from_tf=bool('''.ckpt''' in model_args.model_name_or_path ), config=_UpperCamelCase, cache_dir=model_args.cache_dir, revision=model_args.model_revision, use_auth_token=True if model_args.use_auth_token else None, ignore_mismatched_sizes=model_args.ignore_mismatched_sizes, ) A_ = AutoImageProcessor.from_pretrained( model_args.image_processor_name or model_args.model_name_or_path, cache_dir=model_args.cache_dir, revision=model_args.model_revision, use_auth_token=True if model_args.use_auth_token else None, ) # Define torchvision transforms to be applied to each image. if "shortest_edge" in image_processor.size: A_ = image_processor.size['''shortest_edge'''] else: A_ = (image_processor.size['''height'''], image_processor.size['''width''']) A_ = Normalize(mean=image_processor.image_mean, std=image_processor.image_std ) A_ = Compose( [ RandomResizedCrop(_UpperCamelCase ), RandomHorizontalFlip(), ToTensor(), normalize, ] ) A_ = Compose( [ Resize(_UpperCamelCase ), CenterCrop(_UpperCamelCase ), ToTensor(), normalize, ] ) def train_transforms(_UpperCamelCase : Dict ): A_ = [ _train_transforms(pil_img.convert('''RGB''' ) ) for pil_img in example_batch['''image'''] ] return example_batch def val_transforms(_UpperCamelCase : Any ): A_ = [_val_transforms(pil_img.convert('''RGB''' ) ) for pil_img in example_batch['''image''']] return example_batch if training_args.do_train: if "train" not in dataset: raise ValueError('''--do_train requires a train dataset''' ) if data_args.max_train_samples is not None: A_ = ( dataset['''train'''].shuffle(seed=training_args.seed ).select(range(data_args.max_train_samples ) ) ) # Set the training transforms dataset["train"].set_transform(_UpperCamelCase ) if training_args.do_eval: if "validation" not in dataset: raise ValueError('''--do_eval requires a validation dataset''' ) if data_args.max_eval_samples is not None: A_ = ( dataset['''validation'''].shuffle(seed=training_args.seed ).select(range(data_args.max_eval_samples ) ) ) # Set the validation transforms dataset["validation"].set_transform(_UpperCamelCase ) # Initalize our trainer A_ = Trainer( model=_UpperCamelCase, args=_UpperCamelCase, train_dataset=dataset['''train'''] if training_args.do_train else None, eval_dataset=dataset['''validation'''] if training_args.do_eval else None, compute_metrics=_UpperCamelCase, tokenizer=_UpperCamelCase, data_collator=_UpperCamelCase, ) # Training if training_args.do_train: A_ = None if training_args.resume_from_checkpoint is not None: A_ = training_args.resume_from_checkpoint elif last_checkpoint is not None: A_ = last_checkpoint A_ = trainer.train(resume_from_checkpoint=_UpperCamelCase ) trainer.save_model() trainer.log_metrics('''train''', train_result.metrics ) trainer.save_metrics('''train''', train_result.metrics ) trainer.save_state() # Evaluation if training_args.do_eval: A_ = trainer.evaluate() trainer.log_metrics('''eval''', _UpperCamelCase ) trainer.save_metrics('''eval''', _UpperCamelCase ) # Write model card and (optionally) push to hub A_ = { '''finetuned_from''': model_args.model_name_or_path, '''tasks''': '''image-classification''', '''dataset''': data_args.dataset_name, '''tags''': ['''image-classification''', '''vision'''], } if training_args.push_to_hub: trainer.push_to_hub(**_UpperCamelCase ) else: trainer.create_model_card(**_UpperCamelCase ) if __name__ == "__main__": main()
18
1
from typing import Any import numpy as np def _lowerCAmelCase ( __lowerCAmelCase ) -> bool: """simple docstring""" return np.array_equal(__lowerCAmelCase , matrix.conjugate().T ) def _lowerCAmelCase ( __lowerCAmelCase , __lowerCAmelCase ) -> Any: """simple docstring""" snake_case__ : str = v.conjugate().T snake_case__ : Optional[Any] = v_star.dot(__lowerCAmelCase ) assert isinstance(__lowerCAmelCase , np.ndarray ) return (v_star_dot.dot(__lowerCAmelCase )) / (v_star.dot(__lowerCAmelCase )) def _lowerCAmelCase ( ) -> None: """simple docstring""" snake_case__ : Union[str, Any] = np.array([[2, 2 + 1J, 4], [2 - 1J, 3, 1J], [4, -1J, 1]] ) snake_case__ : Optional[Any] = np.array([[1], [2], [3]] ) assert is_hermitian(__lowerCAmelCase ), f"""{a} is not hermitian.""" print(rayleigh_quotient(__lowerCAmelCase , __lowerCAmelCase ) ) snake_case__ : Optional[Any] = np.array([[1, 2, 4], [2, 3, -1], [4, -1, 1]] ) assert is_hermitian(__lowerCAmelCase ), f"""{a} is not hermitian.""" assert rayleigh_quotient(__lowerCAmelCase , __lowerCAmelCase ) == float(3 ) if __name__ == "__main__": import doctest doctest.testmod() tests()
230
import copy from typing import Any, Dict, List, Optional, Union import numpy as np import torch from ...audio_utils import mel_filter_bank, spectrogram, window_function from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import TensorType, logging A__ = logging.get_logger(__name__) class a ( __lowerCamelCase ): __lowerCAmelCase : str = ["""input_features""", """is_longer"""] def __init__( self :Union[str, Any] ,__lowercase :str=6_4 ,__lowercase :Any=4_8_0_0_0 ,__lowercase :List[Any]=4_8_0 ,__lowercase :Optional[int]=1_0 ,__lowercase :Optional[int]=1_0_2_4 ,__lowercase :int=0.0 ,__lowercase :List[Any]=False ,__lowercase :float = 0 ,__lowercase :float = 1_4_0_0_0 ,__lowercase :int = None ,__lowercase :str = "fusion" ,__lowercase :str = "repeatpad" ,**__lowercase :List[Any] ,): super().__init__( feature_size=__lowercase ,sampling_rate=__lowercase ,padding_value=__lowercase ,return_attention_mask=__lowercase ,**__lowercase ,) snake_case__ : Optional[Any] = top_db snake_case__ : Tuple = truncation snake_case__ : Tuple = padding snake_case__ : List[Any] = fft_window_size snake_case__ : List[Any] = (fft_window_size >> 1) + 1 snake_case__ : str = hop_length snake_case__ : Dict = max_length_s snake_case__ : List[str] = max_length_s * sampling_rate snake_case__ : List[Any] = sampling_rate snake_case__ : Any = frequency_min snake_case__ : Dict = frequency_max snake_case__ : Tuple = mel_filter_bank( num_frequency_bins=self.nb_frequency_bins ,num_mel_filters=__lowercase ,min_frequency=__lowercase ,max_frequency=__lowercase ,sampling_rate=__lowercase ,norm=__lowercase ,mel_scale='''htk''' ,) snake_case__ : Dict = mel_filter_bank( num_frequency_bins=self.nb_frequency_bins ,num_mel_filters=__lowercase ,min_frequency=__lowercase ,max_frequency=__lowercase ,sampling_rate=__lowercase ,norm='''slaney''' ,mel_scale='''slaney''' ,) def __lowerCamelCase ( self :int ): snake_case__ : Dict = copy.deepcopy(self.__dict__ ) snake_case__ : Optional[int] = self.__class__.__name__ if "mel_filters" in output: del output["mel_filters"] if "mel_filters_slaney" in output: del output["mel_filters_slaney"] return output def __lowerCamelCase ( self :Union[str, Any] ,__lowercase :np.array ,__lowercase :Optional[np.array] = None ): snake_case__ : List[Any] = spectrogram( __lowercase ,window_function(self.fft_window_size ,'''hann''' ) ,frame_length=self.fft_window_size ,hop_length=self.hop_length ,power=2.0 ,mel_filters=__lowercase ,log_mel='''dB''' ,) return log_mel_spectrogram.T def __lowerCamelCase ( self :Optional[Any] ,__lowercase :List[str] ,__lowercase :Tuple ,__lowercase :List[str] ): snake_case__ : Dict = np.array_split(list(range(0 ,total_frames - chunk_frames + 1 ) ) ,3 ) if len(ranges[1] ) == 0: # if the audio is too short, we just use the first chunk snake_case__ : Tuple = [0] if len(ranges[2] ) == 0: # if the audio is too short, we just use the first chunk snake_case__ : str = [0] # randomly choose index for each part snake_case__ : Dict = np.random.choice(ranges[0] ) snake_case__ : Any = np.random.choice(ranges[1] ) snake_case__ : Dict = np.random.choice(ranges[2] ) snake_case__ : Optional[int] = mel[idx_front : idx_front + chunk_frames, :] snake_case__ : Optional[Any] = mel[idx_middle : idx_middle + chunk_frames, :] snake_case__ : List[str] = mel[idx_back : idx_back + chunk_frames, :] snake_case__ : Optional[Any] = torch.tensor(mel[None, None, :] ) snake_case__ : Any = torch.nn.functional.interpolate( __lowercase ,size=[chunk_frames, 6_4] ,mode='''bilinear''' ,align_corners=__lowercase ) snake_case__ : List[Any] = mel_shrink[0][0].numpy() snake_case__ : Union[str, Any] = np.stack([mel_shrink, mel_chunk_front, mel_chunk_middle, mel_chunk_back] ,axis=0 ) return mel_fusion def __lowerCamelCase ( self :Any ,__lowercase :np.array ,__lowercase :str ,__lowercase :int ,__lowercase :List[str] ): if waveform.shape[0] > max_length: if truncation == "rand_trunc": snake_case__ : Tuple = True # random crop to max_length (for compatibility) -> this should be handled by self.pad snake_case__ : List[str] = len(__lowercase ) - max_length snake_case__ : Any = np.random.randint(0 ,overflow + 1 ) snake_case__ : Tuple = waveform[idx : idx + max_length] snake_case__ : Tuple = self._np_extract_fbank_features(__lowercase ,self.mel_filters_slaney )[None, :] elif truncation == "fusion": snake_case__ : str = self._np_extract_fbank_features(__lowercase ,self.mel_filters ) snake_case__ : Union[str, Any] = max_length // self.hop_length + 1 # the +1 related to how the spectrogram is computed snake_case__ : Union[str, Any] = mel.shape[0] if chunk_frames == total_frames: # there is a corner case where the audio length is larger than max_length but smaller than max_length+hop_length. # In this case, we just use the whole audio. snake_case__ : Tuple = np.stack([mel, mel, mel, mel] ,axis=0 ) snake_case__ : List[Any] = False else: snake_case__ : List[Any] = self._random_mel_fusion(__lowercase ,__lowercase ,__lowercase ) snake_case__ : Dict = True else: raise NotImplementedError(F"""data_truncating {truncation} not implemented""" ) else: snake_case__ : Optional[int] = False # only use repeat as a new possible value for padding. you repeat the audio before applying the usual max_length padding if waveform.shape[0] < max_length: if padding == "repeat": snake_case__ : List[str] = int(max_length / len(__lowercase ) ) snake_case__ : List[str] = np.stack(np.tile(__lowercase ,n_repeat + 1 ) )[:max_length] if padding == "repeatpad": snake_case__ : Union[str, Any] = int(max_length / len(__lowercase ) ) snake_case__ : List[str] = np.stack(np.tile(__lowercase ,__lowercase ) ) snake_case__ : int = np.pad(__lowercase ,(0, max_length - waveform.shape[0]) ,mode='''constant''' ,constant_values=0 ) if truncation == "fusion": snake_case__ : Tuple = self._np_extract_fbank_features(__lowercase ,self.mel_filters ) snake_case__ : Optional[int] = np.stack([input_mel, input_mel, input_mel, input_mel] ,axis=0 ) else: snake_case__ : List[Any] = self._np_extract_fbank_features(__lowercase ,self.mel_filters_slaney )[None, :] return input_mel, longer def __call__( self :Dict ,__lowercase :Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]] ,__lowercase :str = None ,__lowercase :Optional[str] = None ,__lowercase :Optional[int] = None ,__lowercase :Optional[int] = None ,__lowercase :Optional[Union[str, TensorType]] = None ,**__lowercase :Optional[int] ,): snake_case__ : Optional[int] = truncation if truncation is not None else self.truncation snake_case__ : Optional[int] = padding if padding else self.padding if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( F"""The model corresponding to this feature extractor: {self.__class__.__name__} was trained using a""" F""" sampling rate of {self.sampling_rate}. Please make sure that the provided `raw_speech` input""" F""" was sampled with {self.sampling_rate} and not {sampling_rate}.""" ) else: logger.warning( '''It is strongly recommended to pass the `sampling_rate` argument to this function. ''' '''Failing to do so can result in silent errors that might be hard to debug.''' ) snake_case__ : List[str] = isinstance(__lowercase ,np.ndarray ) and len(raw_speech.shape ) > 1 if is_batched_numpy and len(raw_speech.shape ) > 2: raise ValueError(F"""Only mono-channel audio is supported for input to {self}""" ) snake_case__ : Optional[int] = is_batched_numpy or ( isinstance(__lowercase ,(list, tuple) ) and (isinstance(raw_speech[0] ,(np.ndarray, tuple, list) )) ) if is_batched: snake_case__ : Optional[Any] = [np.asarray(__lowercase ,dtype=np.floataa ) for speech in raw_speech] elif not is_batched and not isinstance(__lowercase ,np.ndarray ): snake_case__ : Tuple = np.asarray(__lowercase ,dtype=np.floataa ) elif isinstance(__lowercase ,np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): snake_case__ : Dict = raw_speech.astype(np.floataa ) # always return batch if not is_batched: snake_case__ : Dict = [np.asarray(__lowercase )] # convert to mel spectrogram, truncate and pad if needed. snake_case__ : Any = [ self._get_input_mel(__lowercase ,max_length if max_length else self.nb_max_samples ,__lowercase ,__lowercase ) for waveform in raw_speech ] snake_case__ : Any = [] snake_case__ : Tuple = [] for mel, longer in padded_inputs: input_mel.append(__lowercase ) is_longer.append(__lowercase ) if truncation == "fusion" and sum(__lowercase ) == 0: # if no audio is longer than 10s, then randomly select one audio to be longer snake_case__ : Optional[int] = np.random.randint(0 ,len(__lowercase ) ) snake_case__ : List[str] = True if isinstance(input_mel[0] ,__lowercase ): snake_case__ : Optional[int] = [np.asarray(__lowercase ,dtype=np.floataa ) for feature in input_mel] # is_longer is a list of bool snake_case__ : Dict = [[longer] for longer in is_longer] snake_case__ : Dict = {'''input_features''': input_mel, '''is_longer''': is_longer} snake_case__ : str = BatchFeature(__lowercase ) if return_tensors is not None: snake_case__ : List[str] = input_features.convert_to_tensors(__lowercase ) return input_features
230
1
"""simple docstring""" import unittest import numpy as np from transformers.testing_utils import require_flax, require_tf, require_torch from transformers.utils import ( expand_dims, flatten_dict, is_flax_available, is_tf_available, is_torch_available, reshape, squeeze, transpose, ) if is_flax_available(): import jax.numpy as jnp if is_tf_available(): import tensorflow as tf if is_torch_available(): import torch class __a ( unittest.TestCase ): def snake_case_ ( self ): _lowerCamelCase = { 'task_specific_params': { 'summarization': {'length_penalty': 1.0, 'max_length': 1_28, 'min_length': 12, 'num_beams': 4}, 'summarization_cnn': {'length_penalty': 2.0, 'max_length': 1_42, 'min_length': 56, 'num_beams': 4}, 'summarization_xsum': {'length_penalty': 1.0, 'max_length': 62, 'min_length': 11, 'num_beams': 6}, } } _lowerCamelCase = { 'task_specific_params.summarization.length_penalty': 1.0, 'task_specific_params.summarization.max_length': 1_28, 'task_specific_params.summarization.min_length': 12, 'task_specific_params.summarization.num_beams': 4, 'task_specific_params.summarization_cnn.length_penalty': 2.0, 'task_specific_params.summarization_cnn.max_length': 1_42, 'task_specific_params.summarization_cnn.min_length': 56, 'task_specific_params.summarization_cnn.num_beams': 4, 'task_specific_params.summarization_xsum.length_penalty': 1.0, 'task_specific_params.summarization_xsum.max_length': 62, 'task_specific_params.summarization_xsum.min_length': 11, 'task_specific_params.summarization_xsum.num_beams': 6, } self.assertEqual(flatten_dict(a__ ) , a__ ) def snake_case_ ( self ): _lowerCamelCase = np.random.randn(3 , 4 ) self.assertTrue(np.allclose(transpose(a__ ) , x.transpose() ) ) _lowerCamelCase = np.random.randn(3 , 4 , 5 ) self.assertTrue(np.allclose(transpose(a__ , axes=(1, 2, 0) ) , x.transpose((1, 2, 0) ) ) ) @require_torch def snake_case_ ( self ): _lowerCamelCase = np.random.randn(3 , 4 ) _lowerCamelCase = torch.tensor(a__ ) self.assertTrue(np.allclose(transpose(a__ ) , transpose(a__ ).numpy() ) ) _lowerCamelCase = np.random.randn(3 , 4 , 5 ) _lowerCamelCase = torch.tensor(a__ ) self.assertTrue(np.allclose(transpose(a__ , axes=(1, 2, 0) ) , transpose(a__ , axes=(1, 2, 0) ).numpy() ) ) @require_tf def snake_case_ ( self ): _lowerCamelCase = np.random.randn(3 , 4 ) _lowerCamelCase = tf.constant(a__ ) self.assertTrue(np.allclose(transpose(a__ ) , transpose(a__ ).numpy() ) ) _lowerCamelCase = np.random.randn(3 , 4 , 5 ) _lowerCamelCase = tf.constant(a__ ) self.assertTrue(np.allclose(transpose(a__ , axes=(1, 2, 0) ) , transpose(a__ , axes=(1, 2, 0) ).numpy() ) ) @require_flax def snake_case_ ( self ): _lowerCamelCase = np.random.randn(3 , 4 ) _lowerCamelCase = jnp.array(a__ ) self.assertTrue(np.allclose(transpose(a__ ) , np.asarray(transpose(a__ ) ) ) ) _lowerCamelCase = np.random.randn(3 , 4 , 5 ) _lowerCamelCase = jnp.array(a__ ) self.assertTrue(np.allclose(transpose(a__ , axes=(1, 2, 0) ) , np.asarray(transpose(a__ , axes=(1, 2, 0) ) ) ) ) def snake_case_ ( self ): _lowerCamelCase = np.random.randn(3 , 4 ) self.assertTrue(np.allclose(reshape(a__ , (4, 3) ) , np.reshape(a__ , (4, 3) ) ) ) _lowerCamelCase = np.random.randn(3 , 4 , 5 ) self.assertTrue(np.allclose(reshape(a__ , (12, 5) ) , np.reshape(a__ , (12, 5) ) ) ) @require_torch def snake_case_ ( self ): _lowerCamelCase = np.random.randn(3 , 4 ) _lowerCamelCase = torch.tensor(a__ ) self.assertTrue(np.allclose(reshape(a__ , (4, 3) ) , reshape(a__ , (4, 3) ).numpy() ) ) _lowerCamelCase = np.random.randn(3 , 4 , 5 ) _lowerCamelCase = torch.tensor(a__ ) self.assertTrue(np.allclose(reshape(a__ , (12, 5) ) , reshape(a__ , (12, 5) ).numpy() ) ) @require_tf def snake_case_ ( self ): _lowerCamelCase = np.random.randn(3 , 4 ) _lowerCamelCase = tf.constant(a__ ) self.assertTrue(np.allclose(reshape(a__ , (4, 3) ) , reshape(a__ , (4, 3) ).numpy() ) ) _lowerCamelCase = np.random.randn(3 , 4 , 5 ) _lowerCamelCase = tf.constant(a__ ) self.assertTrue(np.allclose(reshape(a__ , (12, 5) ) , reshape(a__ , (12, 5) ).numpy() ) ) @require_flax def snake_case_ ( self ): _lowerCamelCase = np.random.randn(3 , 4 ) _lowerCamelCase = jnp.array(a__ ) self.assertTrue(np.allclose(reshape(a__ , (4, 3) ) , np.asarray(reshape(a__ , (4, 3) ) ) ) ) _lowerCamelCase = np.random.randn(3 , 4 , 5 ) _lowerCamelCase = jnp.array(a__ ) self.assertTrue(np.allclose(reshape(a__ , (12, 5) ) , np.asarray(reshape(a__ , (12, 5) ) ) ) ) def snake_case_ ( self ): _lowerCamelCase = np.random.randn(1 , 3 , 4 ) self.assertTrue(np.allclose(squeeze(a__ ) , np.squeeze(a__ ) ) ) _lowerCamelCase = np.random.randn(1 , 4 , 1 , 5 ) self.assertTrue(np.allclose(squeeze(a__ , axis=2 ) , np.squeeze(a__ , axis=2 ) ) ) @require_torch def snake_case_ ( self ): _lowerCamelCase = np.random.randn(1 , 3 , 4 ) _lowerCamelCase = torch.tensor(a__ ) self.assertTrue(np.allclose(squeeze(a__ ) , squeeze(a__ ).numpy() ) ) _lowerCamelCase = np.random.randn(1 , 4 , 1 , 5 ) _lowerCamelCase = torch.tensor(a__ ) self.assertTrue(np.allclose(squeeze(a__ , axis=2 ) , squeeze(a__ , axis=2 ).numpy() ) ) @require_tf def snake_case_ ( self ): _lowerCamelCase = np.random.randn(1 , 3 , 4 ) _lowerCamelCase = tf.constant(a__ ) self.assertTrue(np.allclose(squeeze(a__ ) , squeeze(a__ ).numpy() ) ) _lowerCamelCase = np.random.randn(1 , 4 , 1 , 5 ) _lowerCamelCase = tf.constant(a__ ) self.assertTrue(np.allclose(squeeze(a__ , axis=2 ) , squeeze(a__ , axis=2 ).numpy() ) ) @require_flax def snake_case_ ( self ): _lowerCamelCase = np.random.randn(1 , 3 , 4 ) _lowerCamelCase = jnp.array(a__ ) self.assertTrue(np.allclose(squeeze(a__ ) , np.asarray(squeeze(a__ ) ) ) ) _lowerCamelCase = np.random.randn(1 , 4 , 1 , 5 ) _lowerCamelCase = jnp.array(a__ ) self.assertTrue(np.allclose(squeeze(a__ , axis=2 ) , np.asarray(squeeze(a__ , axis=2 ) ) ) ) def snake_case_ ( self ): _lowerCamelCase = np.random.randn(3 , 4 ) self.assertTrue(np.allclose(expand_dims(a__ , axis=1 ) , np.expand_dims(a__ , axis=1 ) ) ) @require_torch def snake_case_ ( self ): _lowerCamelCase = np.random.randn(3 , 4 ) _lowerCamelCase = torch.tensor(a__ ) self.assertTrue(np.allclose(expand_dims(a__ , axis=1 ) , expand_dims(a__ , axis=1 ).numpy() ) ) @require_tf def snake_case_ ( self ): _lowerCamelCase = np.random.randn(3 , 4 ) _lowerCamelCase = tf.constant(a__ ) self.assertTrue(np.allclose(expand_dims(a__ , axis=1 ) , expand_dims(a__ , axis=1 ).numpy() ) ) @require_flax def snake_case_ ( self ): _lowerCamelCase = np.random.randn(3 , 4 ) _lowerCamelCase = jnp.array(a__ ) self.assertTrue(np.allclose(expand_dims(a__ , axis=1 ) , np.asarray(expand_dims(a__ , axis=1 ) ) ) )
80
"""simple docstring""" from typing import Any, Dict, List, Union from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging, requires_backends from .base import PIPELINE_INIT_ARGS, Pipeline if is_vision_available(): from ..image_utils import load_image if is_torch_available(): import torch from ..models.auto.modeling_auto import MODEL_FOR_OBJECT_DETECTION_MAPPING, MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING A_ : Any =logging.get_logger(__name__) A_ : Dict =Dict[str, Any] A_ : Dict =List[Prediction] @add_end_docstrings(lowerCAmelCase__ ) class __a ( lowerCAmelCase__ ): def __init__( self , *a__ , **a__ ): super().__init__(*a__ , **a__ ) if self.framework == "tf": raise ValueError(F'The {self.__class__} is only available in PyTorch.' ) requires_backends(self , 'vision' ) self.check_model_type( dict(MODEL_FOR_OBJECT_DETECTION_MAPPING.items() + MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING.items() ) ) def snake_case_ ( self , **a__ ): _lowerCamelCase = {} if "threshold" in kwargs: _lowerCamelCase = kwargs['threshold'] return {}, {}, postprocess_kwargs def __call__( self , *a__ , **a__ ): return super().__call__(*a__ , **a__ ) def snake_case_ ( self , a__ ): _lowerCamelCase = load_image(a__ ) _lowerCamelCase = torch.IntTensor([[image.height, image.width]] ) _lowerCamelCase = self.image_processor(images=[image] , return_tensors='pt' ) if self.tokenizer is not None: _lowerCamelCase = self.tokenizer(text=inputs['words'] , boxes=inputs['boxes'] , return_tensors='pt' ) _lowerCamelCase = target_size return inputs def snake_case_ ( self , a__ ): _lowerCamelCase = model_inputs.pop('target_size' ) _lowerCamelCase = self.model(**a__ ) _lowerCamelCase = outputs.__class__({'target_size': target_size, **outputs} ) if self.tokenizer is not None: _lowerCamelCase = model_inputs['bbox'] return model_outputs def snake_case_ ( self , a__ , a__=0.9 ): _lowerCamelCase = model_outputs['target_size'] if self.tokenizer is not None: # This is a LayoutLMForTokenClassification variant. # The OCR got the boxes and the model classified the words. _lowerCamelCase , _lowerCamelCase = target_size[0].tolist() def unnormalize(a__ ): return self._get_bounding_box( torch.Tensor( [ (width * bbox[0] / 10_00), (height * bbox[1] / 10_00), (width * bbox[2] / 10_00), (height * bbox[3] / 10_00), ] ) ) _lowerCamelCase , _lowerCamelCase = model_outputs['logits'].squeeze(0 ).softmax(dim=-1 ).max(dim=-1 ) _lowerCamelCase = [self.model.config.idalabel[prediction] for prediction in classes.tolist()] _lowerCamelCase = [unnormalize(a__ ) for bbox in model_outputs['bbox'].squeeze(0 )] _lowerCamelCase = ['score', 'label', 'box'] _lowerCamelCase = [dict(zip(a__ , a__ ) ) for vals in zip(scores.tolist() , a__ , a__ ) if vals[0] > threshold] else: # This is a regular ForObjectDetectionModel _lowerCamelCase = self.image_processor.post_process_object_detection(a__ , a__ , a__ ) _lowerCamelCase = raw_annotations[0] _lowerCamelCase = raw_annotation['scores'] _lowerCamelCase = raw_annotation['labels'] _lowerCamelCase = raw_annotation['boxes'] _lowerCamelCase = scores.tolist() _lowerCamelCase = [self.model.config.idalabel[label.item()] for label in labels] _lowerCamelCase = [self._get_bounding_box(a__ ) for box in boxes] # {"scores": [...], ...} --> [{"score":x, ...}, ...] _lowerCamelCase = ['score', 'label', 'box'] _lowerCamelCase = [ dict(zip(a__ , a__ ) ) for vals in zip(raw_annotation['scores'] , raw_annotation['labels'] , raw_annotation['boxes'] ) ] return annotation def snake_case_ ( self , a__ ): if self.framework != "pt": raise ValueError('The ObjectDetectionPipeline is only available in PyTorch.' ) _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase = box.int().tolist() _lowerCamelCase = { 'xmin': xmin, 'ymin': ymin, 'xmax': xmax, 'ymax': ymax, } return bbox
80
1
import math import sys def A_ ( _UpperCAmelCase ): if number != int(_UpperCAmelCase ): raise ValueError("the value of input must be a natural number" ) if number < 0: raise ValueError("the value of input must not be a negative number" ) if number == 0: return 1 SCREAMING_SNAKE_CASE_: List[str] = [-1] * (number + 1) SCREAMING_SNAKE_CASE_: str = 0 for i in range(1 , number + 1 ): SCREAMING_SNAKE_CASE_: str = sys.maxsize SCREAMING_SNAKE_CASE_: List[Any] = int(math.sqrt(_UpperCAmelCase ) ) for j in range(1 , root + 1 ): SCREAMING_SNAKE_CASE_: List[str] = 1 + answers[i - (j**2)] SCREAMING_SNAKE_CASE_: Optional[Any] = min(_UpperCAmelCase , _UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Dict = answer return answers[number] if __name__ == "__main__": import doctest doctest.testmod()
13
import unittest from transformers import is_torch_available from transformers.testing_utils import require_torch if is_torch_available(): import torch from transformers.generation import DisjunctiveConstraint @require_torch class __lowercase ( unittest.TestCase ): """simple docstring""" def _SCREAMING_SNAKE_CASE ( self : List[str]): # For consistency across different places the DisjunctiveConstraint is called, # dc.token_ids is a list of integers. It is also initialized only by integers. SCREAMING_SNAKE_CASE_: Optional[Any] = [[1, 2, 4], [1, 2, 3, 4]] SCREAMING_SNAKE_CASE_: Any = DisjunctiveConstraint(lowerCAmelCase__) self.assertTrue(isinstance(dc.token_ids , lowerCAmelCase__)) with self.assertRaises(lowerCAmelCase__): DisjunctiveConstraint(torch.LongTensor([[1, 2, 4], [1, 2, 3]])) with self.assertRaises(lowerCAmelCase__): DisjunctiveConstraint([torch.LongTensor([1, 2, 4]), torch.LongTensor([1, 2, 3, 4, 5])]) def _SCREAMING_SNAKE_CASE ( self : Optional[int]): # We can't have constraints that are complete subsets of another. This leads to a preverse # interpretation of "constraint fulfillment": does generating [1,2,3] fulfill the constraint? # It would mean that it generated [1,2] which fulfills it, but it's in the middle of potentially # fulfilling [1,2,3,4]. If we believe that [1,2,3] does fulfill the constraint, then the algorithm # will necessarily never reach [1,2,3,4], giving users a false sense of control (better to just not allow it). SCREAMING_SNAKE_CASE_: Union[str, Any] = [[1, 2], [1, 2, 3, 4]] with self.assertRaises(lowerCAmelCase__): DisjunctiveConstraint(lowerCAmelCase__) # fails here def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_: List[str] = [[1, 2, 3], [1, 2, 4]] SCREAMING_SNAKE_CASE_: Tuple = DisjunctiveConstraint(lowerCAmelCase__) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: int = dc.update(1) SCREAMING_SNAKE_CASE_: Dict = stepped is True and completed is False and reset is False self.assertTrue(lowerCAmelCase__) self.assertTrue(not dc.completed) self.assertTrue(dc.current_seq == [1]) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: str = dc.update(2) SCREAMING_SNAKE_CASE_: Optional[Any] = stepped is True and completed is False and reset is False self.assertTrue(lowerCAmelCase__) self.assertTrue(not dc.completed) self.assertTrue(dc.current_seq == [1, 2]) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = dc.update(3) SCREAMING_SNAKE_CASE_: Tuple = stepped is True and completed is True and reset is False self.assertTrue(lowerCAmelCase__) self.assertTrue(dc.completed) # Completed! self.assertTrue(dc.current_seq == [1, 2, 3]) def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): SCREAMING_SNAKE_CASE_: Union[str, Any] = [[1, 2, 3], [1, 2, 4, 5], [1, 2, 5]] SCREAMING_SNAKE_CASE_: List[Any] = DisjunctiveConstraint(lowerCAmelCase__) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[Any] = dc.update(1) self.assertTrue(not dc.completed) self.assertTrue(dc.current_seq == [1]) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Dict = dc.update(2) self.assertTrue(not dc.completed) self.assertTrue(dc.current_seq == [1, 2]) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = dc.update(4) self.assertTrue(not dc.completed) self.assertTrue(dc.current_seq == [1, 2, 4]) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Optional[Any] = dc.update(5) self.assertTrue(dc.completed) # Completed! self.assertTrue(dc.current_seq == [1, 2, 4, 5]) dc.reset() SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: List[Any] = dc.update(1) self.assertTrue(not dc.completed) self.assertTrue(dc.remaining() == 3) self.assertTrue(dc.current_seq == [1]) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Tuple = dc.update(2) self.assertTrue(not dc.completed) self.assertTrue(dc.remaining() == 2) self.assertTrue(dc.current_seq == [1, 2]) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_: Union[str, Any] = dc.update(5) self.assertTrue(dc.completed) # Completed! self.assertTrue(dc.remaining() == 0) self.assertTrue(dc.current_seq == [1, 2, 5])
13
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available lowercase__ = { """configuration_data2vec_audio""": ["""DATA2VEC_AUDIO_PRETRAINED_CONFIG_ARCHIVE_MAP""", """Data2VecAudioConfig"""], """configuration_data2vec_text""": [ """DATA2VEC_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """Data2VecTextConfig""", """Data2VecTextOnnxConfig""", ], """configuration_data2vec_vision""": [ """DATA2VEC_VISION_PRETRAINED_CONFIG_ARCHIVE_MAP""", """Data2VecVisionConfig""", """Data2VecVisionOnnxConfig""", ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase__ = [ """DATA2VEC_AUDIO_PRETRAINED_MODEL_ARCHIVE_LIST""", """Data2VecAudioForAudioFrameClassification""", """Data2VecAudioForCTC""", """Data2VecAudioForSequenceClassification""", """Data2VecAudioForXVector""", """Data2VecAudioModel""", """Data2VecAudioPreTrainedModel""", ] lowercase__ = [ """DATA2VEC_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST""", """Data2VecTextForCausalLM""", """Data2VecTextForMaskedLM""", """Data2VecTextForMultipleChoice""", """Data2VecTextForQuestionAnswering""", """Data2VecTextForSequenceClassification""", """Data2VecTextForTokenClassification""", """Data2VecTextModel""", """Data2VecTextPreTrainedModel""", ] lowercase__ = [ """DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST""", """Data2VecVisionForImageClassification""", """Data2VecVisionForMaskedImageModeling""", """Data2VecVisionForSemanticSegmentation""", """Data2VecVisionModel""", """Data2VecVisionPreTrainedModel""", ] if is_tf_available(): lowercase__ = [ """TFData2VecVisionForImageClassification""", """TFData2VecVisionForSemanticSegmentation""", """TFData2VecVisionModel""", """TFData2VecVisionPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_dataavec_audio import DATA2VEC_AUDIO_PRETRAINED_CONFIG_ARCHIVE_MAP, DataaVecAudioConfig from .configuration_dataavec_text import ( DATA2VEC_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP, DataaVecTextConfig, DataaVecTextOnnxConfig, ) from .configuration_dataavec_vision import ( DATA2VEC_VISION_PRETRAINED_CONFIG_ARCHIVE_MAP, DataaVecVisionConfig, DataaVecVisionOnnxConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_dataavec_audio import ( DATA2VEC_AUDIO_PRETRAINED_MODEL_ARCHIVE_LIST, DataaVecAudioForAudioFrameClassification, DataaVecAudioForCTC, DataaVecAudioForSequenceClassification, DataaVecAudioForXVector, DataaVecAudioModel, DataaVecAudioPreTrainedModel, ) from .modeling_dataavec_text import ( DATA2VEC_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST, DataaVecTextForCausalLM, DataaVecTextForMaskedLM, DataaVecTextForMultipleChoice, DataaVecTextForQuestionAnswering, DataaVecTextForSequenceClassification, DataaVecTextForTokenClassification, DataaVecTextModel, DataaVecTextPreTrainedModel, ) from .modeling_dataavec_vision import ( DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST, DataaVecVisionForImageClassification, DataaVecVisionForMaskedImageModeling, DataaVecVisionForSemanticSegmentation, DataaVecVisionModel, DataaVecVisionPreTrainedModel, ) if is_tf_available(): from .modeling_tf_dataavec_vision import ( TFDataaVecVisionForImageClassification, TFDataaVecVisionForSemanticSegmentation, TFDataaVecVisionModel, TFDataaVecVisionPreTrainedModel, ) else: import sys lowercase__ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
12
"""simple docstring""" import unittest import numpy as np import torch from diffusers import VersatileDiffusionImageVariationPipeline from diffusers.utils.testing_utils import load_image, require_torch_gpu, slow, torch_device lowercase__ = False class lowerCAmelCase__ ( unittest.TestCase ): '''simple docstring''' pass @slow @require_torch_gpu class lowerCAmelCase__ ( unittest.TestCase ): '''simple docstring''' def A_ ( self ): _lowerCamelCase : Tuple = VersatileDiffusionImageVariationPipeline.from_pretrained('shi-labs/versatile-diffusion' ) pipe.to(lowercase ) pipe.set_progress_bar_config(disable=lowercase ) _lowerCamelCase : Tuple = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg' ) _lowerCamelCase : Dict = torch.manual_seed(0 ) _lowerCamelCase : Dict = pipe( image=lowercase , generator=lowercase , guidance_scale=7.5 , num_inference_steps=50 , output_type='numpy' , ).images _lowerCamelCase : str = image[0, 253:256, 253:256, -1] assert image.shape == (1, 512, 512, 3) _lowerCamelCase : Any = np.array([0.04_41, 0.04_69, 0.05_07, 0.05_75, 0.06_32, 0.06_50, 0.08_65, 0.09_09, 0.09_45] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2
12
1
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_ ( __lowerCAmelCase ) -> int: __lowercase : List[Any] = 384 if "tiny" in model_name: __lowercase : Tuple = [3, 3, 9, 3] __lowercase : str = [96, 192, 384, 768] if "small" in model_name: __lowercase : Optional[Any] = [3, 3, 27, 3] __lowercase : Tuple = [96, 192, 384, 768] if "base" in model_name: __lowercase : Optional[Any] = [3, 3, 27, 3] __lowercase : Tuple = [128, 256, 512, 1_024] __lowercase : List[Any] = 512 if "large" in model_name: __lowercase : List[str] = [3, 3, 27, 3] __lowercase : Union[str, Any] = [192, 384, 768, 1_536] __lowercase : Tuple = 768 if "xlarge" in model_name: __lowercase : Any = [3, 3, 27, 3] __lowercase : Dict = [256, 512, 1_024, 2_048] __lowercase : Any = 1_024 # set label information __lowercase : Optional[int] = 150 __lowercase : str = '''huggingface/label-files''' __lowercase : Union[str, Any] = '''ade20k-id2label.json''' __lowercase : Union[str, Any] = json.load(open(hf_hub_download(__lowerCAmelCase , __lowerCAmelCase , repo_type='''dataset''' ) , '''r''' ) ) __lowercase : List[Any] = {int(__lowerCAmelCase ): v for k, v in idalabel.items()} __lowercase : Any = {v: k for k, v in idalabel.items()} __lowercase : List[str] = ConvNextConfig( depths=__lowerCAmelCase , hidden_sizes=__lowerCAmelCase , out_features=['''stage1''', '''stage2''', '''stage3''', '''stage4'''] ) __lowercase : Optional[Any] = UperNetConfig( backbone_config=__lowerCAmelCase , auxiliary_in_channels=__lowerCAmelCase , num_labels=__lowerCAmelCase , idalabel=__lowerCAmelCase , labelaid=__lowerCAmelCase , ) return config def UpperCAmelCase_ ( __lowerCAmelCase ) -> str: __lowercase : Union[str, Any] = [] # 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_ ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) -> Dict: __lowercase : Tuple = dct.pop(__lowerCAmelCase ) __lowercase : List[str] = val def UpperCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) -> Union[str, Any]: __lowercase : List[Any] = { '''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''', } __lowercase : Optional[Any] = model_name_to_url[model_name] __lowercase : Any = torch.hub.load_state_dict_from_url(__lowerCAmelCase , map_location='''cpu''' )['''state_dict'''] __lowercase : List[Any] = get_upernet_config(__lowerCAmelCase ) __lowercase : Any = UperNetForSemanticSegmentation(__lowerCAmelCase ) model.eval() # replace "bn" => "batch_norm" for key in state_dict.copy().keys(): __lowercase : Optional[Any] = state_dict.pop(__lowerCAmelCase ) if "bn" in key: __lowercase : Tuple = key.replace('''bn''' , '''batch_norm''' ) __lowercase : str = val # rename keys __lowercase : Optional[Any] = create_rename_keys(__lowerCAmelCase ) for src, dest in rename_keys: rename_key(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) model.load_state_dict(__lowerCAmelCase ) # verify on image __lowercase : int = '''https://huggingface.co/datasets/hf-internal-testing/fixtures_ade20k/resolve/main/ADE_val_00000001.jpg''' __lowercase : Optional[int] = Image.open(requests.get(__lowerCAmelCase , stream=__lowerCAmelCase ).raw ).convert('''RGB''' ) __lowercase : Tuple = SegformerImageProcessor() __lowercase : Union[str, Any] = processor(__lowerCAmelCase , return_tensors='''pt''' ).pixel_values with torch.no_grad(): __lowercase : Optional[Any] = model(__lowerCAmelCase ) if model_name == "upernet-convnext-tiny": __lowercase : Optional[Any] = torch.tensor( [[-8.8110, -8.8110, -8.6521], [-8.8110, -8.8110, -8.6521], [-8.7746, -8.7746, -8.6130]] ) elif model_name == "upernet-convnext-small": __lowercase : Union[str, Any] = torch.tensor( [[-8.8236, -8.8236, -8.6771], [-8.8236, -8.8236, -8.6771], [-8.7638, -8.7638, -8.6240]] ) elif model_name == "upernet-convnext-base": __lowercase : Tuple = torch.tensor( [[-8.8558, -8.8558, -8.6905], [-8.8558, -8.8558, -8.6905], [-8.7669, -8.7669, -8.6021]] ) elif model_name == "upernet-convnext-large": __lowercase : Dict = torch.tensor( [[-8.6660, -8.6660, -8.6210], [-8.6660, -8.6660, -8.6210], [-8.6310, -8.6310, -8.5964]] ) elif model_name == "upernet-convnext-xlarge": __lowercase : int = torch.tensor( [[-8.4980, -8.4980, -8.3977], [-8.4980, -8.4980, -8.3977], [-8.4379, -8.4379, -8.3412]] ) print('''Logits:''' , outputs.logits[0, 0, :3, :3] ) assert torch.allclose(outputs.logits[0, 0, :3, :3] , __lowerCAmelCase , 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(__lowerCAmelCase ) print(F'Saving processor to {pytorch_dump_folder_path}' ) processor.save_pretrained(__lowerCAmelCase ) 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 : Optional[Any] = 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 : List[Any] = parser.parse_args() convert_upernet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
156
from __future__ import annotations from collections.abc import Iterable, Iterator from dataclasses import dataclass __lowerCAmelCase : Any = (3, 9, -11, 0, 7, 5, 1, -1) __lowerCAmelCase : Tuple = (4, 6, 2, 0, 8, 10, 3, -2) @dataclass class __lowerCAmelCase : """simple docstring""" A__ : int A__ : Node | None class __lowerCAmelCase : """simple docstring""" def __init__( self : Optional[Any] , _snake_case : Iterable[int] ): __lowercase : Node | None = None for i in sorted(_snake_case , reverse=_snake_case ): __lowercase : List[Any] = Node(_snake_case , self.head ) def __iter__( self : str ): __lowercase : Union[str, Any] = self.head while node: yield node.data __lowercase : List[Any] = node.next_node def __len__( self : str ): return sum(1 for _ in self ) def __str__( self : List[str] ): return " -> ".join([str(_snake_case ) for node in self] ) def UpperCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase ) -> SortedLinkedList: return SortedLinkedList(list(__lowerCAmelCase ) + list(__lowerCAmelCase ) ) if __name__ == "__main__": import doctest doctest.testmod() __lowerCAmelCase : Dict = SortedLinkedList print(merge_lists(SSL(test_data_odd), SSL(test_data_even)))
156
1
import numpy as np from sklearn.datasets import fetch_california_housing from sklearn.metrics import mean_absolute_error, mean_squared_error from sklearn.model_selection import train_test_split from xgboost import XGBRegressor def lowerCamelCase__ (_UpperCAmelCase): return (data["data"], data["target"]) def lowerCamelCase__ (_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase): SCREAMING_SNAKE_CASE = XGBRegressor(verbosity=0 , random_state=42) xgb.fit(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE) # Predict target for test data SCREAMING_SNAKE_CASE = xgb.predict(__SCREAMING_SNAKE_CASE) SCREAMING_SNAKE_CASE = predictions.reshape(len(__SCREAMING_SNAKE_CASE) , 1) return predictions def lowerCamelCase__ (): SCREAMING_SNAKE_CASE = fetch_california_housing() SCREAMING_SNAKE_CASE = data_handling(__SCREAMING_SNAKE_CASE) SCREAMING_SNAKE_CASE = train_test_split( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , test_size=0.25 , random_state=1) SCREAMING_SNAKE_CASE = xgboost(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE) # Error printing print(F'''Mean Absolute Error : {mean_absolute_error(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE)}''') print(F'''Mean Square Error : {mean_squared_error(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE)}''') if __name__ == "__main__": import doctest doctest.testmod(verbose=True) main()
370
from math import isqrt def lowerCamelCase__ (_UpperCAmelCase): SCREAMING_SNAKE_CASE = [True] * max_number for i in range(2 , isqrt(max_number - 1) + 1): if is_prime[i]: for j in range(i**2 , _UpperCAmelCase , _UpperCAmelCase): SCREAMING_SNAKE_CASE = False return [i for i in range(2 , _UpperCAmelCase) if is_prime[i]] def lowerCamelCase__ (_UpperCAmelCase = 10**8): SCREAMING_SNAKE_CASE = calculate_prime_numbers(max_number // 2) SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = len(_UpperCAmelCase) - 1 while left <= right: while prime_numbers[left] * prime_numbers[right] >= max_number: right -= 1 semiprimes_count += right - left + 1 left += 1 return semiprimes_count if __name__ == "__main__": print(f"""{solution() = }""")
327
0
"""simple docstring""" import argparse import os from io import BytesIO from pathlib import Path import requests from clip_retrieval.clip_client import ClipClient from PIL import Image from tqdm import tqdm def lowercase ( _snake_case : Optional[Any] , _snake_case : List[Any] , _snake_case : Union[str, Any] ) ->List[str]: """simple docstring""" __snake_case : List[Any] = 1.5 __snake_case : List[str] = int(factor * num_class_images ) __snake_case : Tuple = ClipClient( url='''https://knn.laion.ai/knn-service''' , indice_name='''laion_400m''' , num_images=_snake_case , aesthetic_weight=0.1 ) os.makedirs(f"""{class_data_dir}/images""" , exist_ok=_snake_case ) if len(list(Path(f"""{class_data_dir}/images""" ).iterdir() ) ) >= num_class_images: return while True: __snake_case : Dict = client.query(text=_snake_case ) if len(_snake_case ) >= factor * num_class_images or num_images > 1e4: break else: __snake_case : str = int(factor * num_images ) __snake_case : Union[str, Any] = ClipClient( url='''https://knn.laion.ai/knn-service''' , indice_name='''laion_400m''' , num_images=_snake_case , aesthetic_weight=0.1 , ) __snake_case : Any = 0 __snake_case : List[str] = 0 __snake_case : Union[str, Any] = tqdm(desc='''downloading real regularization images''' , total=_snake_case ) with open(f"""{class_data_dir}/caption.txt""" , '''w''' ) as fa, open(f"""{class_data_dir}/urls.txt""" , '''w''' ) as fa, open( f"""{class_data_dir}/images.txt""" , '''w''' ) as fa: while total < num_class_images: __snake_case : Tuple = class_images[count] count += 1 try: __snake_case : List[str] = requests.get(images['''url'''] ) if img.status_code == 200: __snake_case : Optional[int] = Image.open(BytesIO(img.content ) ) with open(f"""{class_data_dir}/images/{total}.jpg""" , '''wb''' ) as f: f.write(img.content ) fa.write(images['''caption'''] + '''\n''' ) fa.write(images['''url'''] + '''\n''' ) fa.write(f"""{class_data_dir}/images/{total}.jpg""" + '''\n''' ) total += 1 pbar.update(1 ) else: continue except Exception: continue return def lowercase ( ) ->List[str]: """simple docstring""" __snake_case : Any = argparse.ArgumentParser('''''' , add_help=_snake_case ) parser.add_argument('''--class_prompt''' , help='''text prompt to retrieve images''' , required=_snake_case , type=_snake_case ) parser.add_argument('''--class_data_dir''' , help='''path to save images''' , required=_snake_case , type=_snake_case ) parser.add_argument('''--num_class_images''' , help='''number of images to download''' , default=200 , type=_snake_case ) return parser.parse_args() if __name__ == "__main__": SCREAMING_SNAKE_CASE : List[Any] = parse_args() retrieve(args.class_prompt, args.class_data_dir, args.num_class_images)
102
import random import timeit from functools import wraps from typing import Callable, Optional from ..configuration_utils import PretrainedConfig from ..models.auto.modeling_tf_auto import TF_MODEL_MAPPING, TF_MODEL_WITH_LM_HEAD_MAPPING from ..utils import is_pyanvml_available, is_tf_available, logging from .benchmark_utils import ( Benchmark, Memory, MemorySummary, measure_peak_memory_cpu, start_memory_tracing, stop_memory_tracing, ) if is_tf_available(): import tensorflow as tf from tensorflow.python.framework.errors_impl import ResourceExhaustedError from .benchmark_args_tf import TensorFlowBenchmarkArguments if is_pyanvml_available(): import pyanvml.pyanvml as nvml snake_case_ : Tuple = logging.get_logger(__name__) def A (__A : bool , __A : bool ) -> Optional[Any]: """simple docstring""" def run_func(__A : Optional[Any] ): @wraps(__A ) def run_in_eager_mode(*__A : Dict , **__A : List[Any] ): return func(*__A , **__A ) @wraps(__A ) @tf.function(experimental_compile=__A ) def run_in_graph_mode(*__A : Optional[Any] , **__A : Any ): return func(*__A , **__A ) if do_eager_mode is True: if use_xla is not False: raise ValueError( '''Cannot run model in XLA, if `args.eager_mode` is set to `True`. Please set `args.eager_mode=False`.''' ) return run_in_eager_mode else: return run_in_graph_mode return run_func def A (__A : int , __A : int , __A : int ) -> ["tf.Tensor"]: """simple docstring""" UpperCAmelCase_ = random.Random() UpperCAmelCase_ = [rng.randint(0 , vocab_size - 1 ) for i in range(batch_size * sequence_length )] return tf.constant(__A , shape=(batch_size, sequence_length) , dtype=tf.intaa ) class __snake_case ( a ): UpperCAmelCase__ : TensorFlowBenchmarkArguments UpperCAmelCase__ : PretrainedConfig UpperCAmelCase__ : str = "TensorFlow" @property def lowerCamelCase ( self : List[str]): """simple docstring""" return tf.__version__ def lowerCamelCase ( self : Dict , _snake_case : str , _snake_case : int , _snake_case : int): """simple docstring""" UpperCAmelCase_ = self.args.strategy if strategy is None: raise ValueError('''A device strategy has to be initialized before using TensorFlow.''') UpperCAmelCase_ = self._prepare_inference_func(_snake_case , _snake_case , _snake_case) return self._measure_speed(_inference) def lowerCamelCase ( self : Any , _snake_case : str , _snake_case : int , _snake_case : int): """simple docstring""" UpperCAmelCase_ = self.args.strategy if strategy is None: raise ValueError('''A device strategy has to be initialized before using TensorFlow.''') UpperCAmelCase_ = self._prepare_train_func(_snake_case , _snake_case , _snake_case) return self._measure_speed(_train) def lowerCamelCase ( self : Any , _snake_case : str , _snake_case : int , _snake_case : int): """simple docstring""" if self.args.is_gpu: tf.config.experimental.set_memory_growth(self.args.gpu_list[self.args.device_idx] , _snake_case) UpperCAmelCase_ = self.args.strategy if strategy is None: raise ValueError('''A device strategy has to be initialized before using TensorFlow.''') UpperCAmelCase_ = self._prepare_inference_func(_snake_case , _snake_case , _snake_case) return self._measure_memory(_inference) def lowerCamelCase ( self : Optional[Any] , _snake_case : str , _snake_case : int , _snake_case : int): """simple docstring""" if self.args.is_gpu: tf.config.experimental.set_memory_growth(self.args.gpu_list[self.args.device_idx] , _snake_case) UpperCAmelCase_ = self.args.strategy if strategy is None: raise ValueError('''A device strategy has to be initialized before using TensorFlow.''') UpperCAmelCase_ = self._prepare_train_func(_snake_case , _snake_case , _snake_case) return self._measure_memory(_train) def lowerCamelCase ( self : Optional[int] , _snake_case : str , _snake_case : int , _snake_case : int): """simple docstring""" UpperCAmelCase_ = self.config_dict[model_name] if self.args.fpaa: raise NotImplementedError('''Mixed precision is currently not supported.''') UpperCAmelCase_ = ( hasattr(_snake_case , '''architectures''') and isinstance(config.architectures , _snake_case) and len(config.architectures) > 0 ) if not self.args.only_pretrain_model and has_model_class_in_config: try: UpperCAmelCase_ = '''TF''' + config.architectures[0] # prepend 'TF' for tensorflow model UpperCAmelCase_ = __import__('''transformers''' , fromlist=[model_class]) UpperCAmelCase_ = getattr(_snake_case , _snake_case) UpperCAmelCase_ = model_cls(_snake_case) except ImportError: raise ImportError( F"""{model_class} does not exist. If you just want to test the pretrained model, you might want to""" ''' set `--only_pretrain_model` or `args.only_pretrain_model=True`.''') else: UpperCAmelCase_ = TF_MODEL_MAPPING[config.__class__](_snake_case) # encoder-decoder has vocab size saved differently UpperCAmelCase_ = config.vocab_size if hasattr(_snake_case , '''vocab_size''') else config.encoder.vocab_size UpperCAmelCase_ = random_input_ids(_snake_case , _snake_case , _snake_case) @run_with_tf_optimizations(self.args.eager_mode , self.args.use_xla) def encoder_decoder_forward(): return model(_snake_case , decoder_input_ids=_snake_case , training=_snake_case) @run_with_tf_optimizations(self.args.eager_mode , self.args.use_xla) def encoder_forward(): return model(_snake_case , training=_snake_case) UpperCAmelCase_ = encoder_decoder_forward if config.is_encoder_decoder else encoder_forward return _inference def lowerCamelCase ( self : Optional[Any] , _snake_case : str , _snake_case : int , _snake_case : int): """simple docstring""" UpperCAmelCase_ = self.config_dict[model_name] if self.args.eager_mode is not False: raise ValueError('''Training cannot be done in eager mode. Please make sure that `args.eager_mode = False`.''') if self.args.fpaa: raise NotImplementedError('''Mixed precision is currently not supported.''') UpperCAmelCase_ = ( hasattr(_snake_case , '''architectures''') and isinstance(config.architectures , _snake_case) and len(config.architectures) > 0 ) if not self.args.only_pretrain_model and has_model_class_in_config: try: UpperCAmelCase_ = '''TF''' + config.architectures[0] # prepend 'TF' for tensorflow model UpperCAmelCase_ = __import__('''transformers''' , fromlist=[model_class]) UpperCAmelCase_ = getattr(_snake_case , _snake_case) UpperCAmelCase_ = model_cls(_snake_case) except ImportError: raise ImportError( F"""{model_class} does not exist. If you just want to test the pretrained model, you might want to""" ''' set `--only_pretrain_model` or `args.only_pretrain_model=True`.''') else: UpperCAmelCase_ = TF_MODEL_WITH_LM_HEAD_MAPPING[config.__class__](_snake_case) # encoder-decoder has vocab size saved differently UpperCAmelCase_ = config.vocab_size if hasattr(_snake_case , '''vocab_size''') else config.encoder.vocab_size UpperCAmelCase_ = random_input_ids(_snake_case , _snake_case , _snake_case) @run_with_tf_optimizations(self.args.eager_mode , self.args.use_xla) def encoder_decoder_train(): UpperCAmelCase_ = model(_snake_case , decoder_input_ids=_snake_case , labels=_snake_case , training=_snake_case)[0] UpperCAmelCase_ = tf.gradients(_snake_case , model.trainable_variables) return gradients @run_with_tf_optimizations(self.args.eager_mode , self.args.use_xla) def encoder_train(): UpperCAmelCase_ = model(_snake_case , labels=_snake_case , training=_snake_case)[0] UpperCAmelCase_ = tf.gradients(_snake_case , model.trainable_variables) return gradients UpperCAmelCase_ = encoder_decoder_train if config.is_encoder_decoder else encoder_train return _train def lowerCamelCase ( self : Any , _snake_case : Optional[Any]): """simple docstring""" with self.args.strategy.scope(): try: if self.args.is_tpu or self.args.use_xla: # run additional 10 times to stabilize compilation for tpu logger.info('''Do inference on TPU. Running model 5 times to stabilize compilation''') timeit.repeat(_snake_case , repeat=1 , number=5) # as written in https://docs.python.org/2/library/timeit.html#timeit.Timer.repeat, min should be taken rather than the average UpperCAmelCase_ = timeit.repeat( _snake_case , repeat=self.args.repeat , number=10 , ) return min(_snake_case) / 1_0.0 except ResourceExhaustedError as e: self.print_fn(F"""Doesn't fit on GPU. {e}""") def lowerCamelCase ( self : Dict , _snake_case : Callable[[], None]): """simple docstring""" logger.info( '''Note that TensorFlow allocates more memory than ''' '''it might need to speed up computation. ''' '''The memory reported here corresponds to the memory ''' '''reported by `nvidia-smi`, which can vary depending ''' '''on total available memory on the GPU that is used.''') with self.args.strategy.scope(): try: if self.args.trace_memory_line_by_line: if not self.args.eager_mode: raise ValueError( '''`args.eager_mode` is set to `False`. Make sure to run model in eager mode to measure memory''' ''' consumption line by line.''') UpperCAmelCase_ = start_memory_tracing('''transformers''') if self.args.is_tpu: # tpu raise NotImplementedError( '''Memory Benchmarking is currently not implemented for TPU. Please disable memory benchmarking''' ''' with `args.memory=False`''') elif self.args.is_gpu: # gpu if not is_pyanvml_available(): logger.warning( '''py3nvml not installed, we won\'t log GPU memory usage. ''' '''Install py3nvml (pip install py3nvml) to log information about GPU.''') UpperCAmelCase_ = '''N/A''' else: logger.info( '''Measuring total GPU usage on GPU device. Make sure to not have additional processes''' ''' running on the same GPU.''') # init nvml nvml.nvmlInit() func() UpperCAmelCase_ = nvml.nvmlDeviceGetHandleByIndex(self.args.device_idx) UpperCAmelCase_ = nvml.nvmlDeviceGetMemoryInfo(_snake_case) UpperCAmelCase_ = meminfo.used UpperCAmelCase_ = Memory(_snake_case) # shutdown nvml nvml.nvmlShutdown() else: # cpu if self.args.trace_memory_line_by_line: logger.info( '''When enabling line by line tracing, the max peak memory for CPU is inaccurate in''' ''' TensorFlow.''') UpperCAmelCase_ = None else: UpperCAmelCase_ = measure_peak_memory_cpu(_snake_case) UpperCAmelCase_ = Memory(_snake_case) if isinstance(_snake_case , _snake_case) else memory_bytes if self.args.trace_memory_line_by_line: UpperCAmelCase_ = stop_memory_tracing(_snake_case) if memory is None: UpperCAmelCase_ = summary.total else: UpperCAmelCase_ = None return memory, summary except ResourceExhaustedError as e: self.print_fn(F"""Doesn't fit on GPU. {e}""") return "N/A", None
51
0
from __future__ import annotations import unittest from transformers import is_tf_available from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import numpy as np import tensorflow as tf from transformers import ( TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST, FlaubertConfig, TFFlaubertForMultipleChoice, TFFlaubertForQuestionAnsweringSimple, TFFlaubertForSequenceClassification, TFFlaubertForTokenClassification, TFFlaubertModel, TFFlaubertWithLMHeadModel, ) class __magic_name__ : def __init__( self : List[str] , lowerCamelCase__ : Any , ) -> Optional[Any]: '''simple docstring''' UpperCamelCase__ : Optional[int] = parent UpperCamelCase__ : Optional[int] = 13 UpperCamelCase__ : Dict = 7 UpperCamelCase__ : Optional[int] = True UpperCamelCase__ : List[str] = True UpperCamelCase__ : List[Any] = True UpperCamelCase__ : Union[str, Any] = True UpperCamelCase__ : Any = True UpperCamelCase__ : Optional[int] = False UpperCamelCase__ : Any = False UpperCamelCase__ : str = False UpperCamelCase__ : Optional[Any] = 2 UpperCamelCase__ : Optional[int] = 99 UpperCamelCase__ : Any = 0 UpperCamelCase__ : List[Any] = 32 UpperCamelCase__ : int = 2 UpperCamelCase__ : Optional[Any] = 4 UpperCamelCase__ : Dict = 0.1 UpperCamelCase__ : Optional[Any] = 0.1 UpperCamelCase__ : str = 512 UpperCamelCase__ : Any = 16 UpperCamelCase__ : str = 2 UpperCamelCase__ : Dict = 0.02 UpperCamelCase__ : List[Any] = 3 UpperCamelCase__ : Optional[int] = 4 UpperCamelCase__ : Tuple = '''last''' UpperCamelCase__ : Any = True UpperCamelCase__ : Union[str, Any] = None UpperCamelCase__ : Tuple = 0 def UpperCAmelCase__ ( self : List[str] ) -> Dict: '''simple docstring''' UpperCamelCase__ : List[str] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCamelCase__ : Any = random_attention_mask([self.batch_size, self.seq_length] , dtype=tf.floataa ) UpperCamelCase__ : Tuple = None if self.use_input_lengths: UpperCamelCase__ : List[str] = ( ids_tensor([self.batch_size] , vocab_size=2 ) + self.seq_length - 2 ) # small variation of seq_length UpperCamelCase__ : Any = None if self.use_token_type_ids: UpperCamelCase__ : int = ids_tensor([self.batch_size, self.seq_length] , self.n_langs ) UpperCamelCase__ : Dict = None UpperCamelCase__ : List[str] = None UpperCamelCase__ : Union[str, Any] = None if self.use_labels: UpperCamelCase__ : Dict = ids_tensor([self.batch_size] , self.type_sequence_label_size ) UpperCamelCase__ : int = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) UpperCamelCase__ : List[str] = ids_tensor([self.batch_size] , 2 , dtype=tf.floataa ) UpperCamelCase__ : Optional[Any] = ids_tensor([self.batch_size] , self.num_choices ) UpperCamelCase__ : Optional[int] = FlaubertConfig( vocab_size=self.vocab_size , n_special=self.n_special , emb_dim=self.hidden_size , n_layers=self.num_hidden_layers , n_heads=self.num_attention_heads , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , gelu_activation=self.gelu_activation , sinusoidal_embeddings=self.sinusoidal_embeddings , asm=self.asm , causal=self.causal , n_langs=self.n_langs , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , summary_type=self.summary_type , use_proj=self.use_proj , bos_token_id=self.bos_token_id , ) return ( config, input_ids, token_type_ids, input_lengths, sequence_labels, token_labels, is_impossible_labels, choice_labels, input_mask, ) def UpperCAmelCase__ ( self : Any , lowerCamelCase__ : Union[str, Any] , lowerCamelCase__ : Tuple , lowerCamelCase__ : Union[str, Any] , lowerCamelCase__ : str , lowerCamelCase__ : Any , lowerCamelCase__ : List[Any] , lowerCamelCase__ : List[str] , lowerCamelCase__ : Any , lowerCamelCase__ : int , ) -> Optional[Any]: '''simple docstring''' UpperCamelCase__ : str = TFFlaubertModel(config=__lowercase ) UpperCamelCase__ : List[str] = {'''input_ids''': input_ids, '''lengths''': input_lengths, '''langs''': token_type_ids} UpperCamelCase__ : Optional[int] = model(__lowercase ) UpperCamelCase__ : Optional[int] = [input_ids, input_mask] UpperCamelCase__ : Union[str, Any] = model(__lowercase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def UpperCAmelCase__ ( self : Optional[int] , lowerCamelCase__ : Optional[int] , lowerCamelCase__ : Optional[int] , lowerCamelCase__ : List[Any] , lowerCamelCase__ : Optional[Any] , lowerCamelCase__ : List[Any] , lowerCamelCase__ : str , lowerCamelCase__ : Union[str, Any] , lowerCamelCase__ : Any , lowerCamelCase__ : int , ) -> int: '''simple docstring''' UpperCamelCase__ : str = TFFlaubertWithLMHeadModel(__lowercase ) UpperCamelCase__ : List[str] = {'''input_ids''': input_ids, '''lengths''': input_lengths, '''langs''': token_type_ids} UpperCamelCase__ : Optional[int] = model(__lowercase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def UpperCAmelCase__ ( self : List[str] , lowerCamelCase__ : Any , lowerCamelCase__ : Optional[Any] , lowerCamelCase__ : List[str] , lowerCamelCase__ : List[str] , lowerCamelCase__ : List[str] , lowerCamelCase__ : Dict , lowerCamelCase__ : Optional[int] , lowerCamelCase__ : int , lowerCamelCase__ : Optional[Any] , ) -> Any: '''simple docstring''' UpperCamelCase__ : Any = TFFlaubertForQuestionAnsweringSimple(__lowercase ) UpperCamelCase__ : str = {'''input_ids''': input_ids, '''lengths''': input_lengths} UpperCamelCase__ : int = model(__lowercase ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def UpperCAmelCase__ ( self : Optional[int] , lowerCamelCase__ : List[str] , lowerCamelCase__ : int , lowerCamelCase__ : Tuple , lowerCamelCase__ : int , lowerCamelCase__ : Any , lowerCamelCase__ : str , lowerCamelCase__ : str , lowerCamelCase__ : str , lowerCamelCase__ : int , ) -> Any: '''simple docstring''' UpperCamelCase__ : int = TFFlaubertForSequenceClassification(__lowercase ) UpperCamelCase__ : List[Any] = {'''input_ids''': input_ids, '''lengths''': input_lengths} UpperCamelCase__ : Any = model(__lowercase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def UpperCAmelCase__ ( self : Optional[int] , lowerCamelCase__ : str , lowerCamelCase__ : List[Any] , lowerCamelCase__ : List[str] , lowerCamelCase__ : int , lowerCamelCase__ : str , lowerCamelCase__ : Dict , lowerCamelCase__ : int , lowerCamelCase__ : Optional[int] , lowerCamelCase__ : Union[str, Any] , ) -> Tuple: '''simple docstring''' UpperCamelCase__ : Optional[int] = self.num_labels UpperCamelCase__ : int = TFFlaubertForTokenClassification(config=__lowercase ) UpperCamelCase__ : Union[str, Any] = {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids} UpperCamelCase__ : Optional[Any] = model(__lowercase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def UpperCAmelCase__ ( self : Any , lowerCamelCase__ : Union[str, Any] , lowerCamelCase__ : Dict , lowerCamelCase__ : int , lowerCamelCase__ : Optional[Any] , lowerCamelCase__ : Optional[Any] , lowerCamelCase__ : List[str] , lowerCamelCase__ : Any , lowerCamelCase__ : str , lowerCamelCase__ : Union[str, Any] , ) -> List[Any]: '''simple docstring''' UpperCamelCase__ : Any = self.num_choices UpperCamelCase__ : Any = TFFlaubertForMultipleChoice(config=__lowercase ) UpperCamelCase__ : List[str] = tf.tile(tf.expand_dims(__lowercase , 1 ) , (1, self.num_choices, 1) ) UpperCamelCase__ : Optional[Any] = tf.tile(tf.expand_dims(__lowercase , 1 ) , (1, self.num_choices, 1) ) UpperCamelCase__ : Tuple = tf.tile(tf.expand_dims(__lowercase , 1 ) , (1, self.num_choices, 1) ) UpperCamelCase__ : Any = { '''input_ids''': multiple_choice_inputs_ids, '''attention_mask''': multiple_choice_input_mask, '''token_type_ids''': multiple_choice_token_type_ids, } UpperCamelCase__ : List[Any] = model(__lowercase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def UpperCAmelCase__ ( self : Any ) -> Dict: '''simple docstring''' UpperCamelCase__ : Optional[int] = self.prepare_config_and_inputs() ( UpperCamelCase__ ) : Any = config_and_inputs UpperCamelCase__ : int = { '''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''langs''': token_type_ids, '''lengths''': input_lengths, } return config, inputs_dict @require_tf class __magic_name__ ( UpperCAmelCase_ , UpperCAmelCase_ , unittest.TestCase): A: List[Any] = ( ( TFFlaubertModel, TFFlaubertWithLMHeadModel, TFFlaubertForSequenceClassification, TFFlaubertForQuestionAnsweringSimple, TFFlaubertForTokenClassification, TFFlaubertForMultipleChoice, ) if is_tf_available() else () ) A: Tuple = ( (TFFlaubertWithLMHeadModel,) if is_tf_available() else () ) # TODO (PVP): Check other models whether language generation is also applicable A: Tuple = ( { """feature-extraction""": TFFlaubertModel, """fill-mask""": TFFlaubertWithLMHeadModel, """question-answering""": TFFlaubertForQuestionAnsweringSimple, """text-classification""": TFFlaubertForSequenceClassification, """token-classification""": TFFlaubertForTokenClassification, """zero-shot""": TFFlaubertForSequenceClassification, } if is_tf_available() else {} ) A: Tuple = False A: Any = False def UpperCAmelCase__ ( self : List[str] , lowerCamelCase__ : Dict , lowerCamelCase__ : Union[str, Any] , lowerCamelCase__ : Dict , lowerCamelCase__ : Tuple , lowerCamelCase__ : Any ) -> Any: '''simple docstring''' if ( pipeline_test_casse_name == "QAPipelineTests" and tokenizer_name is not None and not tokenizer_name.endswith('''Fast''' ) ): # `QAPipelineTests` fails for a few models when the slower tokenizer are used. # (The slower tokenizers were never used for pipeline tests before the pipeline testing rework) # TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer return True return False def UpperCAmelCase__ ( self : Tuple ) -> Union[str, Any]: '''simple docstring''' UpperCamelCase__ : int = TFFlaubertModelTester(self ) UpperCamelCase__ : Dict = ConfigTester(self , config_class=__lowercase , emb_dim=37 ) def UpperCAmelCase__ ( self : Optional[int] ) -> str: '''simple docstring''' self.config_tester.run_common_tests() def UpperCAmelCase__ ( self : int ) -> Any: '''simple docstring''' UpperCamelCase__ : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_model(*__lowercase ) def UpperCAmelCase__ ( self : Optional[Any] ) -> Union[str, Any]: '''simple docstring''' UpperCamelCase__ : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_lm_head(*__lowercase ) def UpperCAmelCase__ ( self : Tuple ) -> Dict: '''simple docstring''' UpperCamelCase__ : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_qa(*__lowercase ) def UpperCAmelCase__ ( self : List[str] ) -> Tuple: '''simple docstring''' UpperCamelCase__ : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_sequence_classif(*__lowercase ) def UpperCAmelCase__ ( self : List[Any] ) -> Optional[Any]: '''simple docstring''' UpperCamelCase__ : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_for_token_classification(*__lowercase ) def UpperCAmelCase__ ( self : List[Any] ) -> str: '''simple docstring''' UpperCamelCase__ : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_for_multiple_choice(*__lowercase ) @slow def UpperCAmelCase__ ( self : str ) -> Union[str, Any]: '''simple docstring''' for model_name in TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCamelCase__ : Any = TFFlaubertModel.from_pretrained(__lowercase ) self.assertIsNotNone(__lowercase ) @require_tf @require_sentencepiece @require_tokenizers class __magic_name__ ( unittest.TestCase): @slow def UpperCAmelCase__ ( self : Optional[Any] ) -> Optional[Any]: '''simple docstring''' UpperCamelCase__ : List[Any] = TFFlaubertModel.from_pretrained('''jplu/tf-flaubert-small-cased''' ) UpperCamelCase__ : Tuple = tf.convert_to_tensor( [[0, 158, 735, 2592, 1424, 6727, 82, 1]] , dtype=tf.intaa , ) # "J'aime flaubert !" UpperCamelCase__ : Union[str, Any] = model(__lowercase )[0] UpperCamelCase__ : str = tf.TensorShape((1, 8, 512) ) self.assertEqual(output.shape , __lowercase ) # compare the actual values for a slice. UpperCamelCase__ : Optional[int] = tf.convert_to_tensor( [ [ [-1.876_8773, -1.56_6555, 0.2707_2418], [-1.692_0038, -0.587_3505, 1.932_9599], [-2.956_3985, -1.699_3835, 1.797_2052], ] ] , dtype=tf.floataa , ) self.assertTrue(np.allclose(output[:, :3, :3].numpy() , expected_slice.numpy() , atol=1E-4 ) )
366
import json import os import unittest from transformers.models.ctrl.tokenization_ctrl import VOCAB_FILES_NAMES, CTRLTokenizer from ...test_tokenization_common import TokenizerTesterMixin class __magic_name__ ( __lowerCAmelCase , unittest.TestCase): A: int = CTRLTokenizer A: List[Any] = False A: Dict = False def UpperCAmelCase__ ( self : Optional[Any] ) -> str: '''simple docstring''' super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt UpperCamelCase__ : Dict = ['''adapt''', '''re@@''', '''a@@''', '''apt''', '''c@@''', '''t''', '''<unk>'''] UpperCamelCase__ : List[str] = dict(zip(lowerCamelCase__ , range(len(lowerCamelCase__ ) ) ) ) UpperCamelCase__ : Tuple = ['''#version: 0.2''', '''a p''', '''ap t</w>''', '''r e''', '''a d''', '''ad apt</w>''', ''''''] UpperCamelCase__ : int = {'''unk_token''': '''<unk>'''} UpperCamelCase__ : int = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) UpperCamelCase__ : Optional[int] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''merges_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(lowerCamelCase__ ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(lowerCamelCase__ ) ) def UpperCAmelCase__ ( self : Tuple , **lowerCamelCase__ : str ) -> Dict: '''simple docstring''' kwargs.update(self.special_tokens_map ) return CTRLTokenizer.from_pretrained(self.tmpdirname , **lowerCamelCase__ ) def UpperCAmelCase__ ( self : List[Any] , lowerCamelCase__ : Any ) -> Optional[Any]: '''simple docstring''' UpperCamelCase__ : Tuple = '''adapt react readapt apt''' UpperCamelCase__ : Optional[Any] = '''adapt react readapt apt''' return input_text, output_text def UpperCAmelCase__ ( self : Optional[Any] ) -> int: '''simple docstring''' UpperCamelCase__ : int = CTRLTokenizer(self.vocab_file , self.merges_file , **self.special_tokens_map ) UpperCamelCase__ : Optional[Any] = '''adapt react readapt apt''' UpperCamelCase__ : List[Any] = '''adapt re@@ a@@ c@@ t re@@ adapt apt'''.split() UpperCamelCase__ : Tuple = tokenizer.tokenize(lowerCamelCase__ ) self.assertListEqual(lowerCamelCase__ , lowerCamelCase__ ) UpperCamelCase__ : Dict = tokens + [tokenizer.unk_token] UpperCamelCase__ : List[str] = [0, 1, 2, 4, 5, 1, 0, 3, 6] self.assertListEqual(tokenizer.convert_tokens_to_ids(lowerCamelCase__ ) , lowerCamelCase__ )
51
0
# Copyright 2021 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import argparse from .config import config_command_parser from .config_args import default_config_file, load_config_from_file # noqa: F401 from .default import default_command_parser from .update import update_command_parser def __UpperCamelCase ( _A=None ): lowerCAmelCase_ = argparse.ArgumentParser(add_help=_A , allow_abbrev=_A ) # The main config parser lowerCAmelCase_ = config_command_parser(_A ) # The subparser to add commands to lowerCAmelCase_ = config_parser.add_subparsers(title='''subcommands''' , dest='''subcommand''' ) # Then add other parsers with the parent parser default_command_parser(_A , parents=[parent_parser] ) update_command_parser(_A , parents=[parent_parser] ) return config_parser def __UpperCamelCase ( ): lowerCAmelCase_ = get_config_parser() lowerCAmelCase_ = config_parser.parse_args() if not hasattr(_A , '''func''' ): config_parser.print_help() exit(1 ) # Run args.func(_A ) if __name__ == "__main__": main()
278
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available lowerCamelCase : Optional[int] = { 'configuration_rag': ['RagConfig'], 'retrieval_rag': ['RagRetriever'], 'tokenization_rag': ['RagTokenizer'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase : List[Any] = [ 'RagModel', 'RagPreTrainedModel', 'RagSequenceForGeneration', 'RagTokenForGeneration', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase : int = [ 'TFRagModel', 'TFRagPreTrainedModel', 'TFRagSequenceForGeneration', 'TFRagTokenForGeneration', ] if TYPE_CHECKING: from .configuration_rag import RagConfig from .retrieval_rag import RagRetriever from .tokenization_rag import RagTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_rag import RagModel, RagPreTrainedModel, RagSequenceForGeneration, RagTokenForGeneration try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_rag import ( TFRagModel, TFRagPreTrainedModel, TFRagSequenceForGeneration, TFRagTokenForGeneration, ) else: import sys lowerCamelCase : str = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
124
0
"""simple docstring""" import numpy as np from cva import COLOR_BGR2GRAY, cvtColor, imread from numpy import array, uinta from PIL import Image from digital_image_processing import change_contrast as cc from digital_image_processing import convert_to_negative as cn from digital_image_processing import sepia as sp from digital_image_processing.dithering import burkes as bs from digital_image_processing.edge_detection import canny from digital_image_processing.filters import convolve as conv from digital_image_processing.filters import gaussian_filter as gg from digital_image_processing.filters import local_binary_pattern as lbp from digital_image_processing.filters import median_filter as med from digital_image_processing.filters import sobel_filter as sob from digital_image_processing.resize import resize as rs lowerCamelCase__ = imread(R"digital_image_processing/image_data/lena_small.jpg") lowerCamelCase__ = cvtColor(img, COLOR_BGR2GRAY) def lowercase__ ( ) -> Optional[Any]: """simple docstring""" _UpperCamelCase : Union[str, Any] = cn.convert_to_negative(a__ ) # assert negative_img array for at least one True assert negative_img.any() def lowercase__ ( ) -> Union[str, Any]: """simple docstring""" with Image.open("digital_image_processing/image_data/lena_small.jpg" ) as img: # Work around assertion for response assert str(cc.change_contrast(a__ ,110 ) ).startswith( "<PIL.Image.Image image mode=RGB size=100x100 at" ) def lowercase__ ( ) -> Union[str, Any]: """simple docstring""" _UpperCamelCase : List[Any] = canny.gen_gaussian_kernel(9 ,sigma=1.4 ) # Assert ambiguous array assert resp.all() def lowercase__ ( ) -> str: """simple docstring""" _UpperCamelCase : Tuple = imread("digital_image_processing/image_data/lena_small.jpg" ,0 ) # assert ambiguous array for all == True assert canny_img.all() _UpperCamelCase : List[Any] = canny.canny(a__ ) # assert canny array for at least one True assert canny_array.any() def lowercase__ ( ) -> List[str]: """simple docstring""" assert gg.gaussian_filter(a__ ,5 ,sigma=0.9 ).all() def lowercase__ ( ) -> Optional[int]: """simple docstring""" _UpperCamelCase : int = array([[0.25, 0.5, 0.25], [0.5, -3, 0.5], [0.25, 0.5, 0.25]] ) _UpperCamelCase : Union[str, Any] = conv.img_convolve(a__ ,a__ ).astype(a__ ) assert res.any() def lowercase__ ( ) -> Optional[Any]: """simple docstring""" assert med.median_filter(a__ ,3 ).any() def lowercase__ ( ) -> Optional[Any]: """simple docstring""" _UpperCamelCase, _UpperCamelCase : Any = sob.sobel_filter(a__ ) assert grad.any() and theta.any() def lowercase__ ( ) -> int: """simple docstring""" _UpperCamelCase : Union[str, Any] = sp.make_sepia(a__ ,20 ) assert sepia.all() def lowercase__ ( lowercase_ = "digital_image_processing/image_data/lena_small.jpg" ) -> Any: """simple docstring""" _UpperCamelCase : Optional[int] = bs.Burkes(imread(a__ ,1 ) ,120 ) burkes.process() assert burkes.output_img.any() def lowercase__ ( lowercase_ = "digital_image_processing/image_data/lena_small.jpg" ,) -> List[str]: """simple docstring""" _UpperCamelCase : Dict = rs.NearestNeighbour(imread(a__ ,1 ) ,400 ,200 ) nn.process() assert nn.output.any() def lowercase__ ( ) -> List[str]: """simple docstring""" _UpperCamelCase : List[str] = "digital_image_processing/image_data/lena.jpg" # Reading the image and converting it to grayscale. _UpperCamelCase : Union[str, Any] = imread(a__ ,0 ) # Test for get_neighbors_pixel function() return not None _UpperCamelCase : Dict = 0 _UpperCamelCase : Union[str, Any] = 0 _UpperCamelCase : List[str] = image[x_coordinate][y_coordinate] _UpperCamelCase : List[str] = lbp.get_neighbors_pixel( a__ ,a__ ,a__ ,a__ ) assert neighbors_pixels is not None # Test for local_binary_pattern function() # Create a numpy array as the same height and width of read image _UpperCamelCase : Optional[Any] = np.zeros((image.shape[0], image.shape[1]) ) # Iterating through the image and calculating the local binary pattern value # for each pixel. for i in range(0 ,image.shape[0] ): for j in range(0 ,image.shape[1] ): _UpperCamelCase : Tuple = lbp.local_binary_value(a__ ,a__ ,a__ ) assert lbp_image.any()
371
"""simple docstring""" import copy from ...configuration_utils import PretrainedConfig from ...utils import add_start_docstrings lowerCamelCase__ = R"\n [`RagConfig`] stores the configuration of a *RagModel*. Configuration objects inherit from [`PretrainedConfig`] and\n can be used to control the model outputs. Read the documentation from [`PretrainedConfig`] for more information.\n\n Args:\n title_sep (`str`, *optional*, defaults to `\" / \"`):\n Separator inserted between the title and the text of the retrieved document when calling [`RagRetriever`].\n doc_sep (`str`, *optional*, defaults to `\" // \"`):\n Separator inserted between the text of the retrieved document and the original input when calling\n [`RagRetriever`].\n n_docs (`int`, *optional*, defaults to 5):\n Number of documents to retrieve.\n max_combined_length (`int`, *optional*, defaults to 300):\n Max length of contextualized input returned by [`~RagRetriever.__call__`].\n retrieval_vector_size (`int`, *optional*, defaults to 768):\n Dimensionality of the document embeddings indexed by [`RagRetriever`].\n retrieval_batch_size (`int`, *optional*, defaults to 8):\n Retrieval batch size, defined as the number of queries issues concurrently to the faiss index encapsulated\n [`RagRetriever`].\n dataset (`str`, *optional*, defaults to `\"wiki_dpr\"`):\n A dataset identifier of the indexed dataset in HuggingFace Datasets (list all available datasets and ids\n using `datasets.list_datasets()`).\n dataset_split (`str`, *optional*, defaults to `\"train\"`)\n Which split of the `dataset` to load.\n index_name (`str`, *optional*, defaults to `\"compressed\"`)\n The index name of the index associated with the `dataset`. One can choose between `\"legacy\"`, `\"exact\"` and\n `\"compressed\"`.\n index_path (`str`, *optional*)\n The path to the serialized faiss index on disk.\n passages_path (`str`, *optional*):\n A path to text passages compatible with the faiss index. Required if using\n [`~models.rag.retrieval_rag.LegacyIndex`]\n use_dummy_dataset (`bool`, *optional*, defaults to `False`)\n Whether to load a \"dummy\" variant of the dataset specified by `dataset`.\n label_smoothing (`float`, *optional*, defaults to 0.0):\n Only relevant if `return_loss` is set to `True`. Controls the `epsilon` parameter value for label smoothing\n in the loss calculation. If set to 0, no label smoothing is performed.\n do_marginalize (`bool`, *optional*, defaults to `False`):\n If `True`, the logits are marginalized over all documents by making use of\n `torch.nn.functional.log_softmax`.\n reduce_loss (`bool`, *optional*, defaults to `False`):\n Whether or not to reduce the NLL loss using the `torch.Tensor.sum` operation.\n do_deduplication (`bool`, *optional*, defaults to `True`):\n Whether or not to deduplicate the generations from different context documents for a given input. Has to be\n set to `False` if used while training with distributed backend.\n exclude_bos_score (`bool`, *optional*, defaults to `False`):\n Whether or not to disregard the BOS token when computing the loss.\n output_retrieved(`bool`, *optional*, defaults to `False`):\n If set to `True`, `retrieved_doc_embeds`, `retrieved_doc_ids`, `context_input_ids` and\n `context_attention_mask` are returned. See returned tensors for more detail.\n use_cache (`bool`, *optional*, defaults to `True`):\n Whether or not the model should return the last key/values attentions (not used by all models).\n forced_eos_token_id (`int`, *optional*):\n The id of the token to force as the last generated token when `max_length` is reached. Usually set to\n `eos_token_id`.\n" @add_start_docstrings(_UpperCamelCase ) class __SCREAMING_SNAKE_CASE ( _UpperCamelCase ): '''simple docstring''' SCREAMING_SNAKE_CASE__ :int = "rag" SCREAMING_SNAKE_CASE__ :List[str] = True def __init__( self : List[Any] , __a : Optional[Any]=None , __a : str=True , __a : Tuple=None , __a : Dict=None , __a : Optional[int]=None , __a : Optional[int]=None , __a : List[Any]=None , __a : Dict=" / " , __a : int=" // " , __a : Optional[Any]=5 , __a : Dict=300 , __a : Optional[int]=768 , __a : Tuple=8 , __a : Union[str, Any]="wiki_dpr" , __a : Dict="train" , __a : List[Any]="compressed" , __a : str=None , __a : Tuple=None , __a : int=False , __a : str=False , __a : Optional[int]=0.0 , __a : Dict=True , __a : Tuple=False , __a : Dict=False , __a : str=False , __a : str=True , __a : Optional[Any]=None , **__a : Tuple , ) -> Any: super().__init__( bos_token_id=__a , pad_token_id=__a , eos_token_id=__a , decoder_start_token_id=__a , forced_eos_token_id=__a , is_encoder_decoder=__a , prefix=__a , vocab_size=__a , **__a , ) assert ( "question_encoder" in kwargs and "generator" in kwargs ), "Config has to be initialized with question_encoder and generator config" _UpperCamelCase : Optional[int] = kwargs.pop("question_encoder" ) _UpperCamelCase : str = question_encoder_config.pop("model_type" ) _UpperCamelCase : Tuple = kwargs.pop("generator" ) _UpperCamelCase : str = decoder_config.pop("model_type" ) from ..auto.configuration_auto import AutoConfig _UpperCamelCase : Union[str, Any] = AutoConfig.for_model(__a , **__a ) _UpperCamelCase : str = AutoConfig.for_model(__a , **__a ) _UpperCamelCase : Optional[int] = reduce_loss _UpperCamelCase : str = label_smoothing _UpperCamelCase : int = exclude_bos_score _UpperCamelCase : List[str] = do_marginalize _UpperCamelCase : Optional[int] = title_sep _UpperCamelCase : Optional[int] = doc_sep _UpperCamelCase : Union[str, Any] = n_docs _UpperCamelCase : Tuple = max_combined_length _UpperCamelCase : Union[str, Any] = dataset _UpperCamelCase : Any = dataset_split _UpperCamelCase : List[str] = index_name _UpperCamelCase : int = retrieval_vector_size _UpperCamelCase : str = retrieval_batch_size _UpperCamelCase : Dict = passages_path _UpperCamelCase : str = index_path _UpperCamelCase : Tuple = use_dummy_dataset _UpperCamelCase : Union[str, Any] = output_retrieved _UpperCamelCase : Optional[Any] = do_deduplication _UpperCamelCase : str = use_cache if self.forced_eos_token_id is None: _UpperCamelCase : List[str] = getattr(self.generator , "forced_eos_token_id" , __a ) @classmethod def __SCREAMING_SNAKE_CASE ( cls : Union[str, Any] , __a : PretrainedConfig , __a : PretrainedConfig , **__a : Optional[int] ) -> PretrainedConfig: return cls(question_encoder=question_encoder_config.to_dict() , generator=generator_config.to_dict() , **__a ) def __SCREAMING_SNAKE_CASE ( self : Dict ) -> int: _UpperCamelCase : Dict = copy.deepcopy(self.__dict__ ) _UpperCamelCase : List[Any] = self.question_encoder.to_dict() _UpperCamelCase : Tuple = self.generator.to_dict() _UpperCamelCase : Any = self.__class__.model_type return output
310
0
from argparse import ArgumentParser from .add_new_model import AddNewModelCommand from .add_new_model_like import AddNewModelLikeCommand from .convert import ConvertCommand from .download import DownloadCommand from .env import EnvironmentCommand from .lfs import LfsCommands from .pt_to_tf import PTtoTFCommand from .run import RunCommand from .serving import ServeCommand from .user import UserCommands def _snake_case ( ): """simple docstring""" SCREAMING_SNAKE_CASE_ : Union[str, Any] = ArgumentParser("Transformers CLI tool" , usage="transformers-cli <command> [<args>]" ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = parser.add_subparsers(help="transformers-cli command helpers" ) # Register commands ConvertCommand.register_subcommand(lowerCAmelCase ) DownloadCommand.register_subcommand(lowerCAmelCase ) EnvironmentCommand.register_subcommand(lowerCAmelCase ) RunCommand.register_subcommand(lowerCAmelCase ) ServeCommand.register_subcommand(lowerCAmelCase ) UserCommands.register_subcommand(lowerCAmelCase ) AddNewModelCommand.register_subcommand(lowerCAmelCase ) AddNewModelLikeCommand.register_subcommand(lowerCAmelCase ) LfsCommands.register_subcommand(lowerCAmelCase ) PTtoTFCommand.register_subcommand(lowerCAmelCase ) # Let's go SCREAMING_SNAKE_CASE_ : Any = parser.parse_args() if not hasattr(lowerCAmelCase , "func" ): parser.print_help() exit(1 ) # Run SCREAMING_SNAKE_CASE_ : int = args.func(lowerCAmelCase ) service.run() if __name__ == "__main__": main()
18
def _snake_case ( lowerCAmelCase : int ): """simple docstring""" SCREAMING_SNAKE_CASE_ : Tuple = int(lowerCAmelCase ) if decimal in (0, 1): # Exit cases for the recursion return str(lowerCAmelCase ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Dict = divmod(lowerCAmelCase , 2 ) return binary_recursive(lowerCAmelCase ) + str(lowerCAmelCase ) def _snake_case ( lowerCAmelCase : str ): """simple docstring""" SCREAMING_SNAKE_CASE_ : str = str(lowerCAmelCase ).strip() if not number: raise ValueError("No input value was provided" ) SCREAMING_SNAKE_CASE_ : List[str] = "-" if number.startswith("-" ) else "" SCREAMING_SNAKE_CASE_ : Optional[Any] = number.lstrip("-" ) if not number.isnumeric(): raise ValueError("Input value is not an integer" ) return f'{negative}0b{binary_recursive(int(lowerCAmelCase ) )}' if __name__ == "__main__": from doctest import testmod testmod()
18
1
from typing import Dict, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import flip_channel_order, resize, to_channel_dimension_format, to_pil_image from ...image_utils import ( ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_pytesseract_available, is_vision_available, logging, requires_backends if is_vision_available(): import PIL # soft dependency if is_pytesseract_available(): import pytesseract __UpperCAmelCase = logging.get_logger(__name__) def __lowerCamelCase ( __magic_name__ : Optional[Any] , __magic_name__ : str , __magic_name__ : Any ): return [ int(1_000 * (box[0] / width) ), int(1_000 * (box[1] / height) ), int(1_000 * (box[2] / width) ), int(1_000 * (box[3] / height) ), ] def __lowerCamelCase ( __magic_name__ : np.ndarray , __magic_name__ : Optional[str] , __magic_name__ : Optional[str] = None ): a__: Optional[Any] =tesseract_config if tesseract_config is not None else "" # apply OCR a__: str =to_pil_image(__magic_name__ ) a__ , a__: Any =pil_image.size a__: Optional[Any] =pytesseract.image_to_data(__magic_name__ , lang=__magic_name__ , output_type="dict" , config=__magic_name__ ) a__ , a__ , a__ , a__ , a__: Dict =data["text"], data["left"], data["top"], data["width"], data["height"] # filter empty words and corresponding coordinates a__: Optional[int] =[idx for idx, word in enumerate(__magic_name__ ) if not word.strip()] a__: Optional[int] =[word for idx, word in enumerate(__magic_name__ ) if idx not in irrelevant_indices] a__: Tuple =[coord for idx, coord in enumerate(__magic_name__ ) if idx not in irrelevant_indices] a__: Any =[coord for idx, coord in enumerate(__magic_name__ ) if idx not in irrelevant_indices] a__: List[Any] =[coord for idx, coord in enumerate(__magic_name__ ) if idx not in irrelevant_indices] a__: str =[coord for idx, coord in enumerate(__magic_name__ ) if idx not in irrelevant_indices] # turn coordinates into (left, top, left+width, top+height) format a__: Optional[int] =[] for x, y, w, h in zip(__magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ ): a__: Optional[int] =[x, y, x + w, y + h] actual_boxes.append(__magic_name__ ) # finally, normalize the bounding boxes a__: Tuple =[] for box in actual_boxes: normalized_boxes.append(normalize_box(__magic_name__ , __magic_name__ , __magic_name__ ) ) assert len(__magic_name__ ) == len(__magic_name__ ), "Not as many words as there are bounding boxes" return words, normalized_boxes class lowerCamelCase__ ( _a ): _lowerCAmelCase = ['''pixel_values'''] def __init__( self : int , _a : bool = True , _a : Dict[str, int] = None , _a : PILImageResampling = PILImageResampling.BILINEAR , _a : bool = True , _a : Optional[str] = None , _a : Optional[str] = "" , **_a : List[Any] , ): super().__init__(**_a ) a__: Optional[Any] =size if size is not None else {"height": 2_2_4, "width": 2_2_4} a__: List[str] =get_size_dict(_a ) a__: List[str] =do_resize a__: Any =size a__: List[Any] =resample a__: Union[str, Any] =apply_ocr a__: Any =ocr_lang a__: Dict =tesseract_config def _lowerCamelCase ( self : Tuple , _a : np.ndarray , _a : Dict[str, int] , _a : PILImageResampling = PILImageResampling.BILINEAR , _a : Optional[Union[str, ChannelDimension]] = None , **_a : Tuple , ): a__: Optional[int] =get_size_dict(_a ) if "height" not in size or "width" not in size: raise ValueError(F"The size dictionary must contain the keys 'height' and 'width'. Got {size.keys()}" ) a__: str =(size["height"], size["width"]) return resize(_a , size=_a , resample=_a , data_format=_a , **_a ) def _lowerCamelCase ( self : Optional[Any] , _a : ImageInput , _a : bool = None , _a : Dict[str, int] = None , _a : PILImageResampling = None , _a : bool = None , _a : Optional[str] = None , _a : Optional[str] = None , _a : Optional[Union[str, TensorType]] = None , _a : ChannelDimension = ChannelDimension.FIRST , **_a : Any , ): a__: Any =do_resize if do_resize is not None else self.do_resize a__: List[Any] =size if size is not None else self.size a__: List[str] =get_size_dict(_a ) a__: List[str] =resample if resample is not None else self.resample a__: Optional[Any] =apply_ocr if apply_ocr is not None else self.apply_ocr a__: int =ocr_lang if ocr_lang is not None else self.ocr_lang a__: Any =tesseract_config if tesseract_config is not None else self.tesseract_config a__: Any =make_list_of_images(_a ) if not valid_images(_a ): raise ValueError( "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, " "torch.Tensor, tf.Tensor or jax.ndarray." ) if do_resize and size is None: raise ValueError("Size must be specified if do_resize is True." ) # All transformations expect numpy arrays. a__: Optional[int] =[to_numpy_array(_a ) for image in images] if apply_ocr: requires_backends(self , "pytesseract" ) a__: Optional[int] =[] a__: Optional[int] =[] for image in images: a__ , a__: List[Any] =apply_tesseract(_a , _a , _a ) words_batch.append(_a ) boxes_batch.append(_a ) if do_resize: a__: List[Any] =[self.resize(image=_a , size=_a , resample=_a ) for image in images] # flip color channels from RGB to BGR (as Detectron2 requires this) a__: Optional[Any] =[flip_channel_order(_a ) for image in images] a__: Dict =[to_channel_dimension_format(_a , _a ) for image in images] a__: Union[str, Any] =BatchFeature(data={"pixel_values": images} , tensor_type=_a ) if apply_ocr: a__: str =words_batch a__: Optional[int] =boxes_batch return data
42
import argparse import logging import os from datetime import datetime import numpy as np import torch from torch import nn from torch.utils.data import DataLoader, RandomSampler, TensorDataset from tqdm import tqdm from transformers import GPTaLMHeadModel __UpperCAmelCase = logging.getLogger(__name__) def __lowerCamelCase ( __magic_name__ : Tuple , __magic_name__ : List[Any] ): # save results if os.path.exists(__magic_name__ ): if os.path.exists(os.path.join(__magic_name__ , "config.json" ) ) and os.path.isfile( os.path.join(__magic_name__ , "config.json" ) ): os.remove(os.path.join(__magic_name__ , "config.json" ) ) if os.path.exists(os.path.join(__magic_name__ , "pytorch_model.bin" ) ) and os.path.isfile( os.path.join(__magic_name__ , "pytorch_model.bin" ) ): os.remove(os.path.join(__magic_name__ , "pytorch_model.bin" ) ) else: os.makedirs(__magic_name__ ) model.save_pretrained(__magic_name__ ) def __lowerCamelCase ( __magic_name__ : Optional[Any] , __magic_name__ : Optional[Any]=False ): a__: int =2 if unlogit: a__: Union[str, Any] =torch.pow(__magic_name__ , __magic_name__ ) a__: str =p * torch.log(__magic_name__ ) a__: Dict =0 return -plogp.sum(dim=-1 ) def __lowerCamelCase ( __magic_name__ : Optional[int] ): logger.info("lv, h >\t" + "\t".join(F"{x + 1}" for x in range(len(__magic_name__ ) ) ) ) for row in range(len(__magic_name__ ) ): if tensor.dtype != torch.long: logger.info(F"layer {row + 1}:\t" + "\t".join(F"{x:.5f}" for x in tensor[row].cpu().data ) ) else: logger.info(F"layer {row + 1}:\t" + "\t".join(F"{x:d}" for x in tensor[row].cpu().data ) ) def __lowerCamelCase ( __magic_name__ : Optional[Any] , __magic_name__ : Optional[Any] , __magic_name__ : Dict , __magic_name__ : Union[str, Any]=True , __magic_name__ : int=True , __magic_name__ : Dict=None , __magic_name__ : Union[str, Any]=False ): a__ , a__: int =model.config.num_hidden_layers, model.config.num_attention_heads a__: List[str] =torch.zeros(__magic_name__ , __magic_name__ ).to(args.device ) a__: List[Any] =torch.zeros(__magic_name__ , __magic_name__ ).to(args.device ) if head_mask is None: a__: Any =torch.ones(__magic_name__ , __magic_name__ ).to(args.device ) head_mask.requires_grad_(requires_grad=__magic_name__ ) # If actually pruned attention multi-head, set head mask to None to avoid shape mismatch if actually_pruned: a__: int =None a__: Optional[int] =0.0 a__: Optional[Any] =0.0 for step, inputs in enumerate(tqdm(__magic_name__ , desc="Iteration" , disable=args.local_rank not in [-1, 0] ) ): a__: Tuple =tuple(t.to(args.device ) for t in inputs ) ((a__) , ): List[Any] =inputs # Do a forward pass (not with torch.no_grad() since we need gradients for importance score - see below) a__: List[Any] =model(__magic_name__ , labels=__magic_name__ , head_mask=__magic_name__ ) # (loss), lm_logits, presents, (all hidden_states), (attentions) a__ , a__ , a__: Optional[Any] =( outputs[0], outputs[1], outputs[-1], ) # Loss and logits are the first, attention the last loss.backward() # Backpropagate to populate the gradients in the head mask total_loss += loss.detach().cpu().numpy() if compute_entropy: for layer, attn in enumerate(__magic_name__ ): a__: int =entropy(attn.detach() , __magic_name__ ) attn_entropy[layer] += masked_entropy.sum(-1 ).sum(0 ).sum(0 ).detach() if compute_importance: head_importance += head_mask.grad.abs().detach() tot_tokens += torch.ones_like(__magic_name__ ).float().detach().sum().data # Normalize attn_entropy /= tot_tokens head_importance /= tot_tokens # Layerwise importance normalization if not args.dont_normalize_importance_by_layer: a__: Any =2 a__: Any =torch.pow(torch.pow(__magic_name__ , __magic_name__ ).sum(-1 ) , 1 / exponent ) head_importance /= norm_by_layer.unsqueeze(-1 ) + 1e-20 if not args.dont_normalize_global_importance: a__: int =(head_importance - head_importance.min()) / (head_importance.max() - head_importance.min()) # Print matrices if compute_entropy: logger.info("Attention entropies" ) print_ad_tensor(__magic_name__ ) if compute_importance: logger.info("Head importance scores" ) print_ad_tensor(__magic_name__ ) logger.info("Head ranked by importance scores" ) a__: Any =torch.zeros(head_importance.numel() , dtype=torch.long , device=args.device ) a__: List[Any] =torch.arange( head_importance.numel() , device=args.device ) a__: int =head_ranks.view_as(__magic_name__ ) print_ad_tensor(__magic_name__ ) return attn_entropy, head_importance, total_loss def __lowerCamelCase ( __magic_name__ : Any , __magic_name__ : List[str] , __magic_name__ : Tuple ): a__ , a__ , a__: List[Any] =compute_heads_importance(__magic_name__ , __magic_name__ , __magic_name__ , compute_entropy=__magic_name__ ) a__: List[str] =1 / loss # instead of downsteam score use the LM loss logger.info("Pruning: original score: %f, threshold: %f" , __magic_name__ , original_score * args.masking_threshold ) a__: Union[str, Any] =torch.ones_like(__magic_name__ ) a__: Optional[Any] =max(1 , int(new_head_mask.numel() * args.masking_amount ) ) a__: Union[str, Any] =original_score while current_score >= original_score * args.masking_threshold: a__: Dict =new_head_mask.clone().detach() # save current head mask # heads from least important to most - keep only not-masked heads a__: List[Any] =float("Inf" ) a__: List[str] =head_importance.view(-1 ).sort()[1] if len(__magic_name__ ) <= num_to_mask: print("BREAK BY num_to_mask" ) break # mask heads a__: Union[str, Any] =current_heads_to_mask[:num_to_mask] logger.info("Heads to mask: %s" , str(current_heads_to_mask.tolist() ) ) a__: Any =new_head_mask.view(-1 ) a__: Optional[int] =0.0 a__: Optional[int] =new_head_mask.view_as(__magic_name__ ) a__: str =new_head_mask.clone().detach() print_ad_tensor(__magic_name__ ) # Compute metric and head importance again a__ , a__ , a__: Optional[Any] =compute_heads_importance( __magic_name__ , __magic_name__ , __magic_name__ , compute_entropy=__magic_name__ , head_mask=__magic_name__ ) a__: Optional[int] =1 / loss logger.info( "Masking: current score: %f, remaining heads %d (%.1f percents)" , __magic_name__ , new_head_mask.sum() , new_head_mask.sum() / new_head_mask.numel() * 100 , ) logger.info("Final head mask" ) print_ad_tensor(__magic_name__ ) np.save(os.path.join(args.output_dir , "head_mask.npy" ) , head_mask.detach().cpu().numpy() ) return head_mask def __lowerCamelCase ( __magic_name__ : Tuple , __magic_name__ : Optional[int] , __magic_name__ : Union[str, Any] , __magic_name__ : Any ): a__: Any =datetime.now() a__ , a__ , a__: int =compute_heads_importance( __magic_name__ , __magic_name__ , __magic_name__ , compute_entropy=__magic_name__ , compute_importance=__magic_name__ , head_mask=__magic_name__ ) a__: Optional[int] =1 / loss a__: Optional[Any] =datetime.now() - before_time a__: str =sum(p.numel() for p in model.parameters() ) a__: Optional[Any] ={ layer: (1 - head_mask[layer].long()).nonzero().squeeze().tolist() for layer in range(len(__magic_name__ ) ) } for k, v in heads_to_prune.items(): if isinstance(__magic_name__ , __magic_name__ ): a__: List[Any] =[ v, ] assert sum(len(__magic_name__ ) for h in heads_to_prune.values() ) == (1 - head_mask.long()).sum().item() model.prune_heads(__magic_name__ ) a__: Dict =sum(p.numel() for p in model.parameters() ) a__: Any =datetime.now() a__ , a__ , a__: Union[str, Any] =compute_heads_importance( __magic_name__ , __magic_name__ , __magic_name__ , compute_entropy=__magic_name__ , compute_importance=__magic_name__ , head_mask=__magic_name__ , actually_pruned=__magic_name__ , ) a__: Dict =1 / loss a__: Dict =datetime.now() - before_time logger.info( "Pruning: original num of params: %.2e, after pruning %.2e (%.1f percents)" , __magic_name__ , __magic_name__ , pruned_num_params / original_num_params * 100 , ) logger.info("Pruning: score with masking: %f score with pruning: %f" , __magic_name__ , __magic_name__ ) logger.info("Pruning: speed ratio (original timing / new timing): %f percents" , original_time / new_time * 100 ) save_model(__magic_name__ , args.output_dir ) def __lowerCamelCase ( ): a__: int =argparse.ArgumentParser() # Required parameters parser.add_argument( "--data_dir" , default=__magic_name__ , type=__magic_name__ , required=__magic_name__ , help="The input data dir. Should contain the .tsv files (or other data files) for the task." , ) parser.add_argument( "--model_name_or_path" , default=__magic_name__ , type=__magic_name__ , required=__magic_name__ , help="Path to pretrained model or model identifier from huggingface.co/models" , ) parser.add_argument( "--output_dir" , default=__magic_name__ , type=__magic_name__ , required=__magic_name__ , help="The output directory where the model predictions and checkpoints will be written." , ) # Other parameters parser.add_argument( "--config_name" , default="" , type=__magic_name__ , help="Pretrained config name or path if not the same as model_name_or_path" , ) parser.add_argument( "--tokenizer_name" , default="" , type=__magic_name__ , help="Pretrained tokenizer name or path if not the same as model_name_or_path" , ) parser.add_argument( "--cache_dir" , default=__magic_name__ , type=__magic_name__ , help="Where do you want to store the pre-trained models downloaded from s3" , ) parser.add_argument( "--data_subset" , type=__magic_name__ , default=-1 , help="If > 0: limit the data to a subset of data_subset instances." ) parser.add_argument( "--overwrite_output_dir" , action="store_true" , help="Whether to overwrite data in output directory" ) parser.add_argument( "--overwrite_cache" , action="store_true" , help="Overwrite the cached training and evaluation sets" ) parser.add_argument( "--dont_normalize_importance_by_layer" , action="store_true" , help="Don't normalize importance score by layers" ) parser.add_argument( "--dont_normalize_global_importance" , action="store_true" , help="Don't normalize all importance scores between 0 and 1" , ) parser.add_argument( "--try_masking" , action="store_true" , help="Whether to try to mask head until a threshold of accuracy." ) parser.add_argument( "--masking_threshold" , default=0.9 , type=__magic_name__ , help="masking threshold in term of metrics (stop masking when metric < threshold * original metric value)." , ) parser.add_argument( "--masking_amount" , default=0.1 , type=__magic_name__ , help="Amount to heads to masking at each masking step." ) parser.add_argument("--metric_name" , default="acc" , type=__magic_name__ , help="Metric to use for head masking." ) parser.add_argument( "--max_seq_length" , default=128 , type=__magic_name__ , help=( "The maximum total input sequence length after WordPiece tokenization. \n" "Sequences longer than this will be truncated, sequences shorter padded." ) , ) parser.add_argument("--batch_size" , default=1 , type=__magic_name__ , help="Batch size." ) parser.add_argument("--seed" , type=__magic_name__ , default=42 ) parser.add_argument("--local_rank" , type=__magic_name__ , default=-1 , help="local_rank for distributed training on gpus" ) parser.add_argument("--no_cuda" , action="store_true" , help="Whether not to use CUDA when available" ) parser.add_argument("--server_ip" , type=__magic_name__ , default="" , help="Can be used for distant debugging." ) parser.add_argument("--server_port" , type=__magic_name__ , default="" , help="Can be used for distant debugging." ) a__: Union[str, Any] =parser.parse_args() if args.server_ip and args.server_port: # Distant debugging - see https://code.visualstudio.com/docs/python/debugging#_attach-to-a-local-script import ptvsd print("Waiting for debugger attach" ) ptvsd.enable_attach(address=(args.server_ip, args.server_port) , redirect_output=__magic_name__ ) ptvsd.wait_for_attach() # Setup devices and distributed training if args.local_rank == -1 or args.no_cuda: a__: Tuple =torch.device("cuda" if torch.cuda.is_available() and not args.no_cuda else "cpu" ) a__: Optional[Any] =0 if args.no_cuda else torch.cuda.device_count() else: torch.cuda.set_device(args.local_rank ) a__: Optional[Any] =torch.device("cuda" , args.local_rank ) a__: Dict =1 torch.distributed.init_process_group(backend="nccl" ) # Initializes the distributed backend # Setup logging logging.basicConfig(level=logging.INFO if args.local_rank in [-1, 0] else logging.WARN ) logger.info("device: {} n_gpu: {}, distributed: {}".format(args.device , args.n_gpu , bool(args.local_rank != -1 ) ) ) a__: Dict =GPTaLMHeadModel.from_pretrained(args.model_name_or_path ) # Distributed and parallel training model.to(args.device ) if args.local_rank != -1: a__: List[str] =nn.parallel.DistributedDataParallel( __magic_name__ , device_ids=[args.local_rank] , output_device=args.local_rank , find_unused_parameters=__magic_name__ ) elif args.n_gpu > 1: a__: List[str] =nn.DataParallel(__magic_name__ ) # Print/save training arguments os.makedirs(args.output_dir , exist_ok=__magic_name__ ) torch.save(__magic_name__ , os.path.join(args.output_dir , "run_args.bin" ) ) logger.info("Training/evaluation parameters %s" , __magic_name__ ) # Prepare dataset a__: int =np.concatenate( [ np.loadtxt(args.data_dir , dtype=np.intaa ), ] ) a__: Any =(torch.from_numpy(__magic_name__ ),) a__: List[str] =TensorDataset(*__magic_name__ ) a__: Optional[int] =RandomSampler(__magic_name__ ) a__: Union[str, Any] =DataLoader(__magic_name__ , sampler=__magic_name__ , batch_size=args.batch_size ) # Compute head entropy and importance score compute_heads_importance(__magic_name__ , __magic_name__ , __magic_name__ ) # Try head masking (set heads to zero until the score goes under a threshole) # and head pruning (remove masked heads and see the effect on the network) if args.try_masking and args.masking_threshold > 0.0 and args.masking_threshold < 1.0: a__: Optional[int] =mask_heads(__magic_name__ , __magic_name__ , __magic_name__ ) prune_heads(__magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ ) if __name__ == "__main__": main()
42
1
"""simple docstring""" import unittest import torch from torch import nn from diffusers.models.activations import get_activation class __lowerCAmelCase ( unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self ): __a = get_activation('''swish''' ) self.assertIsInstance(_a , nn.SiLU ) self.assertEqual(act(torch.tensor(-100 , dtype=torch.floataa ) ).item() , 0 ) self.assertNotEqual(act(torch.tensor(-1 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(0 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(20 , dtype=torch.floataa ) ).item() , 20 ) def __UpperCAmelCase ( self ): __a = get_activation('''silu''' ) self.assertIsInstance(_a , nn.SiLU ) self.assertEqual(act(torch.tensor(-100 , dtype=torch.floataa ) ).item() , 0 ) self.assertNotEqual(act(torch.tensor(-1 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(0 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(20 , dtype=torch.floataa ) ).item() , 20 ) def __UpperCAmelCase ( self ): __a = get_activation('''mish''' ) self.assertIsInstance(_a , nn.Mish ) self.assertEqual(act(torch.tensor(-200 , dtype=torch.floataa ) ).item() , 0 ) self.assertNotEqual(act(torch.tensor(-1 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(0 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(20 , dtype=torch.floataa ) ).item() , 20 ) def __UpperCAmelCase ( self ): __a = get_activation('''gelu''' ) self.assertIsInstance(_a , nn.GELU ) self.assertEqual(act(torch.tensor(-100 , dtype=torch.floataa ) ).item() , 0 ) self.assertNotEqual(act(torch.tensor(-1 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(0 , dtype=torch.floataa ) ).item() , 0 ) self.assertEqual(act(torch.tensor(20 , dtype=torch.floataa ) ).item() , 20 )
45
import os import tempfile from functools import partial from unittest import TestCase from unittest.mock import patch import numpy as np import pytest from datasets.arrow_dataset import Dataset from datasets.search import ElasticSearchIndex, FaissIndex, MissingIndex from .utils import require_elasticsearch, require_faiss A_ : Any = pytest.mark.integration @require_faiss class _a (__magic_name__ ): '''simple docstring''' def __A ( self ): A__ : Tuple = Dataset.from_dict({"""filename""": ["""my_name-train""" + """_""" + str(A__ ) for x in np.arange(30 ).tolist()]} ) return dset def __A ( self ): import faiss A__ : Dataset = self._create_dummy_dataset() A__ : Union[str, Any] = dset.map( lambda A__ , A__ : {"vecs": i * np.ones(5 , dtype=np.floataa )} , with_indices=A__ , keep_in_memory=A__ ) A__ : int = dset.add_faiss_index("""vecs""" , batch_size=100 , metric_type=faiss.METRIC_INNER_PRODUCT ) A__ , A__ : Optional[Any] = dset.get_nearest_examples("""vecs""" , np.ones(5 , dtype=np.floataa ) ) self.assertEqual(examples["""filename"""][0] , """my_name-train_29""" ) dset.drop_index("""vecs""" ) def __A ( self ): import faiss A__ : Dataset = self._create_dummy_dataset() dset.add_faiss_index_from_external_arrays( external_arrays=np.ones((30, 5) ) * np.arange(30 ).reshape(-1 , 1 ) , index_name="""vecs""" , batch_size=100 , metric_type=faiss.METRIC_INNER_PRODUCT , ) A__ , A__ : str = dset.get_nearest_examples("""vecs""" , np.ones(5 , dtype=np.floataa ) ) self.assertEqual(examples["""filename"""][0] , """my_name-train_29""" ) def __A ( self ): import faiss A__ : Dataset = self._create_dummy_dataset() dset.add_faiss_index_from_external_arrays( external_arrays=np.ones((30, 5) ) * np.arange(30 ).reshape(-1 , 1 ) , index_name="""vecs""" , metric_type=faiss.METRIC_INNER_PRODUCT , ) # Setting delete=False and unlinking manually is not pretty... but it is required on Windows to # ensure somewhat stable behaviour. If we don't, we get PermissionErrors. This is an age-old issue. # see https://bugs.python.org/issue14243 and # https://stackoverflow.com/questions/23212435/permission-denied-to-write-to-my-temporary-file/23212515 with tempfile.NamedTemporaryFile(delete=A__ ) as tmp_file: dset.save_faiss_index("""vecs""" , tmp_file.name ) dset.load_faiss_index("""vecs2""" , tmp_file.name ) os.unlink(tmp_file.name ) A__ , A__ : Tuple = dset.get_nearest_examples("""vecs2""" , np.ones(5 , dtype=np.floataa ) ) self.assertEqual(examples["""filename"""][0] , """my_name-train_29""" ) def __A ( self ): A__ : Dataset = self._create_dummy_dataset() dset.add_faiss_index_from_external_arrays( external_arrays=np.ones((30, 5) ) * np.arange(30 ).reshape(-1 , 1 ) , index_name="""vecs""" ) dset.drop_index("""vecs""" ) self.assertRaises(A__ , partial(dset.get_nearest_examples , """vecs2""" , np.ones(5 , dtype=np.floataa ) ) ) def __A ( self ): from elasticsearch import Elasticsearch A__ : Dataset = self._create_dummy_dataset() with patch("""elasticsearch.Elasticsearch.search""" ) as mocked_search, patch( """elasticsearch.client.IndicesClient.create""" ) as mocked_index_create, patch("""elasticsearch.helpers.streaming_bulk""" ) as mocked_bulk: A__ : List[Any] = {"""acknowledged""": True} mocked_bulk.return_value([(True, None)] * 30 ) A__ : List[Any] = {"""hits""": {"""hits""": [{"""_score""": 1, """_id""": 29}]}} A__ : Any = Elasticsearch() dset.add_elasticsearch_index("""filename""" , es_client=A__ ) A__ , A__ : Any = dset.get_nearest_examples("""filename""" , """my_name-train_29""" ) self.assertEqual(examples["""filename"""][0] , """my_name-train_29""" ) @require_faiss class _a (__magic_name__ ): '''simple docstring''' def __A ( self ): import faiss A__ : Optional[int] = FaissIndex(metric_type=faiss.METRIC_INNER_PRODUCT ) # add vectors index.add_vectors(np.eye(5 , dtype=np.floataa ) ) self.assertIsNotNone(index.faiss_index ) self.assertEqual(index.faiss_index.ntotal , 5 ) index.add_vectors(np.zeros((5, 5) , dtype=np.floataa ) ) self.assertEqual(index.faiss_index.ntotal , 10 ) # single query A__ : Any = np.zeros(5 , dtype=np.floataa ) A__ : str = 1 A__ , A__ : Optional[Any] = index.search(A__ ) self.assertRaises(A__ , index.search , query.reshape(-1 , 1 ) ) self.assertGreater(scores[0] , 0 ) self.assertEqual(indices[0] , 1 ) # batched queries A__ : Optional[int] = np.eye(5 , dtype=np.floataa )[::-1] A__ , A__ : str = index.search_batch(A__ ) self.assertRaises(A__ , index.search_batch , queries[0] ) A__ : str = [scores[0] for scores in total_scores] A__ : Tuple = [indices[0] for indices in total_indices] self.assertGreater(np.min(A__ ) , 0 ) self.assertListEqual([4, 3, 2, 1, 0] , A__ ) def __A ( self ): import faiss A__ : Dict = FaissIndex(string_factory="""Flat""" ) index.add_vectors(np.eye(5 , dtype=np.floataa ) ) self.assertIsInstance(index.faiss_index , faiss.IndexFlat ) A__ : Dict = FaissIndex(string_factory="""LSH""" ) index.add_vectors(np.eye(5 , dtype=np.floataa ) ) self.assertIsInstance(index.faiss_index , faiss.IndexLSH ) with self.assertRaises(A__ ): A__ : List[Any] = FaissIndex(string_factory="""Flat""" , custom_index=faiss.IndexFlat(5 ) ) def __A ( self ): import faiss A__ : List[Any] = faiss.IndexFlat(5 ) A__ : Union[str, Any] = FaissIndex(custom_index=A__ ) index.add_vectors(np.eye(5 , dtype=np.floataa ) ) self.assertIsInstance(index.faiss_index , faiss.IndexFlat ) def __A ( self ): import faiss A__ : Any = FaissIndex(metric_type=faiss.METRIC_INNER_PRODUCT ) index.add_vectors(np.eye(5 , dtype=np.floataa ) ) # Setting delete=False and unlinking manually is not pretty... but it is required on Windows to # ensure somewhat stable behaviour. If we don't, we get PermissionErrors. This is an age-old issue. # see https://bugs.python.org/issue14243 and # https://stackoverflow.com/questions/23212435/permission-denied-to-write-to-my-temporary-file/23212515 with tempfile.NamedTemporaryFile(delete=A__ ) as tmp_file: index.save(tmp_file.name ) A__ : int = FaissIndex.load(tmp_file.name ) os.unlink(tmp_file.name ) A__ : Optional[Any] = np.zeros(5 , dtype=np.floataa ) A__ : Optional[int] = 1 A__ , A__ : List[Any] = index.search(A__ ) self.assertGreater(scores[0] , 0 ) self.assertEqual(indices[0] , 1 ) @require_faiss def UpperCamelCase (lowercase_: Dict ) -> Optional[Any]: import faiss A__ : Dict = FaissIndex(metric_type=faiss.METRIC_INNER_PRODUCT ) index.add_vectors(np.eye(5 , dtype=np.floataa ) ) A__ : Optional[Any] = """index.faiss""" A__ : Any = f"""mock://{index_name}""" index.save(lowercase_ , storage_options=mockfs.storage_options ) A__ : str = FaissIndex.load(lowercase_ , storage_options=mockfs.storage_options ) A__ : int = np.zeros(5 , dtype=np.floataa ) A__ : Union[str, Any] = 1 A__ , A__ : Any = index.search(lowercase_ ) assert scores[0] > 0 assert indices[0] == 1 @require_elasticsearch class _a (__magic_name__ ): '''simple docstring''' def __A ( self ): from elasticsearch import Elasticsearch with patch("""elasticsearch.Elasticsearch.search""" ) as mocked_search, patch( """elasticsearch.client.IndicesClient.create""" ) as mocked_index_create, patch("""elasticsearch.helpers.streaming_bulk""" ) as mocked_bulk: A__ : List[str] = Elasticsearch() A__ : List[Any] = {"""acknowledged""": True} A__ : int = ElasticSearchIndex(es_client=A__ ) mocked_bulk.return_value([(True, None)] * 3 ) index.add_documents(["""foo""", """bar""", """foobar"""] ) # single query A__ : Dict = """foo""" A__ : Tuple = {"""hits""": {"""hits""": [{"""_score""": 1, """_id""": 0}]}} A__ , A__ : Tuple = index.search(A__ ) self.assertEqual(scores[0] , 1 ) self.assertEqual(indices[0] , 0 ) # single query with timeout A__ : List[str] = """foo""" A__ : str = {"""hits""": {"""hits""": [{"""_score""": 1, """_id""": 0}]}} A__ , A__ : Dict = index.search(A__ , request_timeout=30 ) self.assertEqual(scores[0] , 1 ) self.assertEqual(indices[0] , 0 ) # batched queries A__ : Union[str, Any] = ["""foo""", """bar""", """foobar"""] A__ : Tuple = {"""hits""": {"""hits""": [{"""_score""": 1, """_id""": 1}]}} A__ , A__ : Dict = index.search_batch(A__ ) A__ : Tuple = [scores[0] for scores in total_scores] A__ : Optional[int] = [indices[0] for indices in total_indices] self.assertGreater(np.min(A__ ) , 0 ) self.assertListEqual([1, 1, 1] , A__ ) # batched queries with timeout A__ : int = ["""foo""", """bar""", """foobar"""] A__ : List[Any] = {"""hits""": {"""hits""": [{"""_score""": 1, """_id""": 1}]}} A__ , A__ : Tuple = index.search_batch(A__ , request_timeout=30 ) A__ : List[Any] = [scores[0] for scores in total_scores] A__ : Optional[int] = [indices[0] for indices in total_indices] self.assertGreater(np.min(A__ ) , 0 ) self.assertListEqual([1, 1, 1] , A__ )
192
0
"""simple docstring""" from math import factorial UpperCAmelCase : Tuple = {str(d): factorial(d) for d in range(10)} def lowerCamelCase ( _UpperCamelCase : int ) -> int: '''simple docstring''' return sum(DIGIT_FACTORIAL[d] for d in str(_UpperCamelCase ) ) def lowerCamelCase ( ) -> int: '''simple docstring''' __UpperCAmelCase : Optional[int] = 7 * factorial(9 ) + 1 return sum(i for i in range(3 , _UpperCamelCase ) if sum_of_digit_factorial(_UpperCamelCase ) == i ) if __name__ == "__main__": print(F"{solution() = }")
320
"""simple docstring""" import tempfile import unittest from transformers import AutoModelForSeqaSeqLM, AutoTokenizer from transformers.testing_utils import ( is_torch_available, require_optimum, require_torch, slow, ) if is_torch_available(): import torch @require_torch @require_optimum @slow class lowerCamelCase__ ( unittest.TestCase ): """simple docstring""" def lowerCamelCase__ ( self : int ): '''simple docstring''' __UpperCAmelCase : List[Any] = """hf-internal-testing/tiny-random-t5""" __UpperCAmelCase : Dict = AutoTokenizer.from_pretrained(UpperCamelCase ) __UpperCAmelCase : Any = AutoModelForSeqaSeqLM.from_pretrained(UpperCamelCase ) __UpperCAmelCase : Optional[int] = tokenizer("""This is me""" , return_tensors="""pt""" ) __UpperCAmelCase : int = model.to_bettertransformer() self.assertTrue(any("""BetterTransformer""" in mod.__class__.__name__ for _, mod in model.named_modules() ) ) __UpperCAmelCase : Tuple = model.generate(**UpperCamelCase ) __UpperCAmelCase : Tuple = model.reverse_bettertransformer() self.assertFalse(any("""BetterTransformer""" in mod.__class__.__name__ for _, mod in model.named_modules() ) ) with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(UpperCamelCase ) __UpperCAmelCase : Any = AutoModelForSeqaSeqLM.from_pretrained(UpperCamelCase ) self.assertFalse( any("""BetterTransformer""" in mod.__class__.__name__ for _, mod in model_reloaded.named_modules() ) ) __UpperCAmelCase : Tuple = model_reloaded.generate(**UpperCamelCase ) self.assertTrue(torch.allclose(UpperCamelCase , UpperCamelCase ) ) def lowerCamelCase__ ( self : Any ): '''simple docstring''' __UpperCAmelCase : Any = """hf-internal-testing/tiny-random-t5""" __UpperCAmelCase : List[Any] = AutoModelForSeqaSeqLM.from_pretrained(UpperCamelCase ) __UpperCAmelCase : Tuple = model.to_bettertransformer() with tempfile.TemporaryDirectory() as tmpdirname: with self.assertRaises(UpperCamelCase ): model.save_pretrained(UpperCamelCase ) __UpperCAmelCase : Tuple = model.reverse_bettertransformer() model.save_pretrained(UpperCamelCase )
320
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available UpperCAmelCase_ = { 'configuration_data2vec_audio': ['DATA2VEC_AUDIO_PRETRAINED_CONFIG_ARCHIVE_MAP', 'Data2VecAudioConfig'], 'configuration_data2vec_text': [ 'DATA2VEC_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'Data2VecTextConfig', 'Data2VecTextOnnxConfig', ], 'configuration_data2vec_vision': [ 'DATA2VEC_VISION_PRETRAINED_CONFIG_ARCHIVE_MAP', 'Data2VecVisionConfig', 'Data2VecVisionOnnxConfig', ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase_ = [ 'DATA2VEC_AUDIO_PRETRAINED_MODEL_ARCHIVE_LIST', 'Data2VecAudioForAudioFrameClassification', 'Data2VecAudioForCTC', 'Data2VecAudioForSequenceClassification', 'Data2VecAudioForXVector', 'Data2VecAudioModel', 'Data2VecAudioPreTrainedModel', ] UpperCAmelCase_ = [ 'DATA2VEC_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST', 'Data2VecTextForCausalLM', 'Data2VecTextForMaskedLM', 'Data2VecTextForMultipleChoice', 'Data2VecTextForQuestionAnswering', 'Data2VecTextForSequenceClassification', 'Data2VecTextForTokenClassification', 'Data2VecTextModel', 'Data2VecTextPreTrainedModel', ] UpperCAmelCase_ = [ 'DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST', 'Data2VecVisionForImageClassification', 'Data2VecVisionForMaskedImageModeling', 'Data2VecVisionForSemanticSegmentation', 'Data2VecVisionModel', 'Data2VecVisionPreTrainedModel', ] if is_tf_available(): UpperCAmelCase_ = [ 'TFData2VecVisionForImageClassification', 'TFData2VecVisionForSemanticSegmentation', 'TFData2VecVisionModel', 'TFData2VecVisionPreTrainedModel', ] if TYPE_CHECKING: from .configuration_dataavec_audio import DATA2VEC_AUDIO_PRETRAINED_CONFIG_ARCHIVE_MAP, DataaVecAudioConfig from .configuration_dataavec_text import ( DATA2VEC_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP, DataaVecTextConfig, DataaVecTextOnnxConfig, ) from .configuration_dataavec_vision import ( DATA2VEC_VISION_PRETRAINED_CONFIG_ARCHIVE_MAP, DataaVecVisionConfig, DataaVecVisionOnnxConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_dataavec_audio import ( DATA2VEC_AUDIO_PRETRAINED_MODEL_ARCHIVE_LIST, DataaVecAudioForAudioFrameClassification, DataaVecAudioForCTC, DataaVecAudioForSequenceClassification, DataaVecAudioForXVector, DataaVecAudioModel, DataaVecAudioPreTrainedModel, ) from .modeling_dataavec_text import ( DATA2VEC_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST, DataaVecTextForCausalLM, DataaVecTextForMaskedLM, DataaVecTextForMultipleChoice, DataaVecTextForQuestionAnswering, DataaVecTextForSequenceClassification, DataaVecTextForTokenClassification, DataaVecTextModel, DataaVecTextPreTrainedModel, ) from .modeling_dataavec_vision import ( DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST, DataaVecVisionForImageClassification, DataaVecVisionForMaskedImageModeling, DataaVecVisionForSemanticSegmentation, DataaVecVisionModel, DataaVecVisionPreTrainedModel, ) if is_tf_available(): from .modeling_tf_dataavec_vision import ( TFDataaVecVisionForImageClassification, TFDataaVecVisionForSemanticSegmentation, TFDataaVecVisionModel, TFDataaVecVisionPreTrainedModel, ) else: import sys UpperCAmelCase_ = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
12
from __future__ import annotations def lowerCamelCase__ ( A__ : list[int] , A__ : list[int] , A__ : list[int] , A__ : list[list[str]] , A__ : int , ): '''simple docstring''' __lowerCamelCase = len(A__ ) # If row is equal to the size of the board it means there are a queen in each row in # the current board (possible_board) if row == n: # We convert the variable possible_board that looks like this: [1, 3, 0, 2] to # this: ['. Q . . ', '. . . Q ', 'Q . . . ', '. . Q . '] boards.append([""". """ * i + """Q """ + """. """ * (n - 1 - i) for i in possible_board] ) return # We iterate each column in the row to find all possible results in each row for col in range(A__ ): # We apply that we learned previously. First we check that in the current board # (possible_board) there are not other same value because if there is it means # that there are a collision in vertical. Then we apply the two formulas we # learned before: # # 45º: y - x = b or 45: row - col = b # 135º: y + x = b or row + col = b. # # And we verify if the results of this two formulas not exist in their variables # respectively. (diagonal_right_collisions, diagonal_left_collisions) # # If any or these are True it means there is a collision so we continue to the # next value in the for loop. if ( col in possible_board or row - col in diagonal_right_collisions or row + col in diagonal_left_collisions ): continue # If it is False we call dfs function again and we update the inputs depth_first_search( [*possible_board, col] , [*diagonal_right_collisions, row - col] , [*diagonal_left_collisions, row + col] , A__ , A__ , ) def lowerCamelCase__ ( A__ : int ): '''simple docstring''' __lowerCamelCase = [] depth_first_search([] , [] , [] , A__ , A__ ) # Print all the boards for board in boards: for column in board: print(A__ ) print("""""" ) print(len(A__ ) , """solutions were found.""" ) if __name__ == "__main__": import doctest doctest.testmod() n_queens_solution(4)
12
1
import json from typing import TYPE_CHECKING, List, Optional, Tuple from tokenizers import pre_tokenizers, processors from ...tokenization_utils_base import AddedToken, BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_blenderbot import BlenderbotTokenizer if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation a__ : Optional[Any] = logging.get_logger(__name__) a__ : List[str] = { '''vocab_file''': '''vocab.json''', '''merges_file''': '''merges.txt''', '''tokenizer_config_file''': '''tokenizer_config.json''', } a__ : Any = { '''vocab_file''': {'''facebook/blenderbot-3B''': '''https://huggingface.co/facebook/blenderbot-3B/resolve/main/vocab.json'''}, '''merges_file''': {'''facebook/blenderbot-3B''': '''https://huggingface.co/facebook/blenderbot-3B/resolve/main/merges.txt'''}, '''tokenizer_config_file''': { '''facebook/blenderbot-3B''': '''https://huggingface.co/facebook/blenderbot-3B/resolve/main/tokenizer_config.json''' }, } a__ : str = {'''facebook/blenderbot-3B''': 128} class a_ ( a__ ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[Any] = VOCAB_FILES_NAMES __SCREAMING_SNAKE_CASE : Optional[int] = PRETRAINED_VOCAB_FILES_MAP __SCREAMING_SNAKE_CASE : Optional[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __SCREAMING_SNAKE_CASE : Optional[int] = ['input_ids', 'attention_mask'] __SCREAMING_SNAKE_CASE : Union[str, Any] = BlenderbotTokenizer def __init__( self , _lowerCamelCase=None , _lowerCamelCase=None , _lowerCamelCase=None , _lowerCamelCase="replace" , _lowerCamelCase="<s>" , _lowerCamelCase="</s>" , _lowerCamelCase="</s>" , _lowerCamelCase="<s>" , _lowerCamelCase="<unk>" , _lowerCamelCase="<pad>" , _lowerCamelCase="<mask>" , _lowerCamelCase=False , _lowerCamelCase=True , **_lowerCamelCase , ) ->Any: super().__init__( _lowerCamelCase , _lowerCamelCase , tokenizer_file=_lowerCamelCase , errors=_lowerCamelCase , bos_token=_lowerCamelCase , eos_token=_lowerCamelCase , sep_token=_lowerCamelCase , cls_token=_lowerCamelCase , unk_token=_lowerCamelCase , pad_token=_lowerCamelCase , mask_token=_lowerCamelCase , add_prefix_space=_lowerCamelCase , trim_offsets=_lowerCamelCase , **_lowerCamelCase , ) SCREAMING_SNAKE_CASE : Union[str, Any] = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get('''add_prefix_space''' , _lowerCamelCase ) != add_prefix_space: SCREAMING_SNAKE_CASE : Optional[Any] = getattr(_lowerCamelCase , pre_tok_state.pop('''type''' ) ) SCREAMING_SNAKE_CASE : Dict = add_prefix_space SCREAMING_SNAKE_CASE : Optional[int] = pre_tok_class(**_lowerCamelCase ) SCREAMING_SNAKE_CASE : Optional[Any] = add_prefix_space SCREAMING_SNAKE_CASE : Union[str, Any] = '''post_processor''' SCREAMING_SNAKE_CASE : Tuple = getattr(self.backend_tokenizer , _lowerCamelCase , _lowerCamelCase ) if tokenizer_component_instance: SCREAMING_SNAKE_CASE : List[Any] = json.loads(tokenizer_component_instance.__getstate__() ) # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class` if "sep" in state: SCREAMING_SNAKE_CASE : List[str] = tuple(state['''sep'''] ) if "cls" in state: SCREAMING_SNAKE_CASE : List[str] = tuple(state['''cls'''] ) SCREAMING_SNAKE_CASE : Union[str, Any] = False if state.get('''add_prefix_space''' , _lowerCamelCase ) != add_prefix_space: SCREAMING_SNAKE_CASE : int = add_prefix_space SCREAMING_SNAKE_CASE : Union[str, Any] = True if state.get('''trim_offsets''' , _lowerCamelCase ) != trim_offsets: SCREAMING_SNAKE_CASE : str = trim_offsets SCREAMING_SNAKE_CASE : int = True if changes_to_apply: SCREAMING_SNAKE_CASE : str = getattr(_lowerCamelCase , state.pop('''type''' ) ) SCREAMING_SNAKE_CASE : str = component_class(**_lowerCamelCase ) setattr(self.backend_tokenizer , _lowerCamelCase , _lowerCamelCase ) @property # Copied from transformers.models.roberta.tokenization_roberta_fast.RobertaTokenizerFast.mask_token with Roberta->Blenderbot, RoBERTa->Blenderbot def __lowerCAmelCase ( self ) ->str: if self._mask_token is None: if self.verbose: logger.error('''Using mask_token, but it is not set yet.''' ) return None return str(self._mask_token ) @mask_token.setter def __lowerCAmelCase ( self , _lowerCamelCase ) ->List[str]: SCREAMING_SNAKE_CASE : str = AddedToken(_lowerCamelCase , lstrip=_lowerCamelCase , rstrip=_lowerCamelCase ) if isinstance(_lowerCamelCase , _lowerCamelCase ) else value SCREAMING_SNAKE_CASE : Tuple = value def __lowerCAmelCase ( self , *_lowerCamelCase , **_lowerCamelCase ) ->BatchEncoding: SCREAMING_SNAKE_CASE : int = kwargs.get('''is_split_into_words''' , _lowerCamelCase ) assert self.add_prefix_space or not is_split_into_words, ( F"""You need to instantiate {self.__class__.__name__} with add_prefix_space=True """ "to use it with pretokenized inputs." ) return super()._batch_encode_plus(*_lowerCamelCase , **_lowerCamelCase ) def __lowerCAmelCase ( self , *_lowerCamelCase , **_lowerCamelCase ) ->BatchEncoding: SCREAMING_SNAKE_CASE : List[str] = kwargs.get('''is_split_into_words''' , _lowerCamelCase ) assert self.add_prefix_space or not is_split_into_words, ( F"""You need to instantiate {self.__class__.__name__} with add_prefix_space=True """ "to use it with pretokenized inputs." ) return super()._encode_plus(*_lowerCamelCase , **_lowerCamelCase ) def __lowerCAmelCase ( self , _lowerCamelCase , _lowerCamelCase = None ) ->Tuple[str]: SCREAMING_SNAKE_CASE : int = self._tokenizer.model.save(_lowerCamelCase , name=_lowerCamelCase ) return tuple(_lowerCamelCase ) def __lowerCAmelCase ( self , _lowerCamelCase , _lowerCamelCase = None ) ->List[int]: SCREAMING_SNAKE_CASE : Optional[Any] = [self.sep_token_id] SCREAMING_SNAKE_CASE : Dict = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def __lowerCAmelCase ( self , _lowerCamelCase , _lowerCamelCase = None ) ->Optional[Any]: return token_ids_a + [self.eos_token_id] def __lowerCAmelCase ( self , _lowerCamelCase ) ->List[int]: SCREAMING_SNAKE_CASE : Optional[int] = [] for is_user, text in conversation.iter_texts(): if is_user: # We need to space prefix as it's being done within blenderbot inputs.append(''' ''' + text ) else: # Generated responses should contain them already. inputs.append(_lowerCamelCase ) SCREAMING_SNAKE_CASE : int = ''' '''.join(_lowerCamelCase ) SCREAMING_SNAKE_CASE : List[Any] = self.encode(_lowerCamelCase ) if len(_lowerCamelCase ) > self.model_max_length: SCREAMING_SNAKE_CASE : Tuple = input_ids[-self.model_max_length :] logger.warning(F"""Trimmed input from conversation as it was longer than {self.model_max_length} tokens.""" ) return input_ids
19
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, ) a__ : Any = {'''configuration_xglm''': ['''XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''XGLMConfig''']} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a__ : Dict = ['''XGLMTokenizer'''] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a__ : List[Any] = ['''XGLMTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a__ : int = [ '''XGLM_PRETRAINED_MODEL_ARCHIVE_LIST''', '''XGLMForCausalLM''', '''XGLMModel''', '''XGLMPreTrainedModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a__ : Tuple = [ '''FlaxXGLMForCausalLM''', '''FlaxXGLMModel''', '''FlaxXGLMPreTrainedModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a__ : Tuple = [ '''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 a__ : Dict = _LazyModule(__name__, globals()['''__file__'''], _import_structure)
19
1
from transformers import BertTokenizer, EncoderDecoderModel, SeqaSeqTrainer, SeqaSeqTrainingArguments from transformers.testing_utils import TestCasePlus, require_torch, slow from transformers.utils import is_datasets_available if is_datasets_available(): import datasets class lowerCAmelCase ( __UpperCamelCase ): @slow @require_torch def A_ ( self : Tuple ) -> str: lowerCamelCase__ : Optional[int] = EncoderDecoderModel.from_encoder_decoder_pretrained('prajjwal1/bert-tiny' , 'prajjwal1/bert-tiny' ) lowerCamelCase__ : List[Any] = BertTokenizer.from_pretrained('bert-base-uncased' ) lowerCamelCase__ : Tuple = bertabert.config.encoder.vocab_size lowerCamelCase__ : Optional[int] = tokenizer.sep_token_id lowerCamelCase__ : List[Any] = tokenizer.cls_token_id lowerCamelCase__ : str = 128 lowerCamelCase__ : Optional[Any] = datasets.load_dataset('cnn_dailymail' , '3.0.0' , split='train[:1%]' ) lowerCamelCase__ : Any = datasets.load_dataset('cnn_dailymail' , '3.0.0' , split='validation[:1%]' ) lowerCamelCase__ : List[Any] = train_dataset.select(range(32 ) ) lowerCamelCase__ : Dict = val_dataset.select(range(16 ) ) lowerCamelCase__ : str = 4 def _map_to_encoder_decoder_inputs(UpperCAmelCase : int ): # Tokenizer will automatically set [BOS] <text> [EOS] lowerCamelCase__ : List[str] = tokenizer(batch['article'] , padding='max_length' , truncation=UpperCAmelCase , max_length=512 ) lowerCamelCase__ : Optional[Any] = tokenizer(batch['highlights'] , padding='max_length' , truncation=UpperCAmelCase , max_length=128 ) lowerCamelCase__ : int = inputs.input_ids lowerCamelCase__ : List[str] = inputs.attention_mask lowerCamelCase__ : Optional[int] = outputs.input_ids lowerCamelCase__ : Dict = outputs.input_ids.copy() lowerCamelCase__ : Union[str, Any] = [ [-100 if token == tokenizer.pad_token_id else token for token in labels] for labels in batch['labels'] ] lowerCamelCase__ : Union[str, Any] = outputs.attention_mask assert all(len(UpperCAmelCase ) == 512 for x in inputs.input_ids ) assert all(len(UpperCAmelCase ) == 128 for x in outputs.input_ids ) return batch def _compute_metrics(UpperCAmelCase : Optional[int] ): lowerCamelCase__ : int = pred.label_ids lowerCamelCase__ : Optional[int] = pred.predictions # all unnecessary tokens are removed lowerCamelCase__ : Optional[int] = tokenizer.batch_decode(UpperCAmelCase , skip_special_tokens=UpperCAmelCase ) lowerCamelCase__ : List[str] = tokenizer.batch_decode(UpperCAmelCase , skip_special_tokens=UpperCAmelCase ) lowerCamelCase__ : Any = sum([int(pred_str[i] == label_str[i] ) for i in range(len(UpperCAmelCase ) )] ) / len(UpperCAmelCase ) return {"accuracy": accuracy} # map train dataset lowerCamelCase__ : int = train_dataset.map( _map_to_encoder_decoder_inputs , batched=UpperCAmelCase , batch_size=UpperCAmelCase , remove_columns=['article', 'highlights'] , ) train_dataset.set_format( type='torch' , columns=['input_ids', 'attention_mask', 'decoder_input_ids', 'decoder_attention_mask', 'labels'] , ) # same for validation dataset lowerCamelCase__ : Optional[int] = val_dataset.map( _map_to_encoder_decoder_inputs , batched=UpperCAmelCase , batch_size=UpperCAmelCase , remove_columns=['article', 'highlights'] , ) val_dataset.set_format( type='torch' , columns=['input_ids', 'attention_mask', 'decoder_input_ids', 'decoder_attention_mask', 'labels'] , ) lowerCamelCase__ : Dict = self.get_auto_remove_tmp_dir() lowerCamelCase__ : List[str] = SeqaSeqTrainingArguments( output_dir=UpperCAmelCase , per_device_train_batch_size=UpperCAmelCase , per_device_eval_batch_size=UpperCAmelCase , predict_with_generate=UpperCAmelCase , evaluation_strategy='steps' , do_train=UpperCAmelCase , do_eval=UpperCAmelCase , warmup_steps=0 , eval_steps=2 , logging_steps=2 , ) # instantiate trainer lowerCamelCase__ : List[str] = SeqaSeqTrainer( model=UpperCAmelCase , args=UpperCAmelCase , compute_metrics=_compute_metrics , train_dataset=UpperCAmelCase , eval_dataset=UpperCAmelCase , tokenizer=UpperCAmelCase , ) # start training trainer.train()
50
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available, is_vision_available, ) UpperCamelCase = { """configuration_mobilevit""": ["""MOBILEVIT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """MobileViTConfig""", """MobileViTOnnxConfig"""], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ["""MobileViTFeatureExtractor"""] UpperCamelCase = ["""MobileViTImageProcessor"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ """MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST""", """MobileViTForImageClassification""", """MobileViTForSemanticSegmentation""", """MobileViTModel""", """MobileViTPreTrainedModel""", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ """TF_MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST""", """TFMobileViTForImageClassification""", """TFMobileViTForSemanticSegmentation""", """TFMobileViTModel""", """TFMobileViTPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_mobilevit import MOBILEVIT_PRETRAINED_CONFIG_ARCHIVE_MAP, MobileViTConfig, MobileViTOnnxConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_mobilevit import MobileViTFeatureExtractor from .image_processing_mobilevit import MobileViTImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mobilevit import ( MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST, MobileViTForImageClassification, MobileViTForSemanticSegmentation, MobileViTModel, MobileViTPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_mobilevit import ( TF_MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST, TFMobileViTForImageClassification, TFMobileViTForSemanticSegmentation, TFMobileViTModel, TFMobileViTPreTrainedModel, ) else: import sys UpperCamelCase = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
186
0
import unittest import numpy as np from transformers import RobertaConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask if is_flax_available(): from transformers.models.roberta.modeling_flax_roberta import ( FlaxRobertaForCausalLM, FlaxRobertaForMaskedLM, FlaxRobertaForMultipleChoice, FlaxRobertaForQuestionAnswering, FlaxRobertaForSequenceClassification, FlaxRobertaForTokenClassification, FlaxRobertaModel, ) class SCREAMING_SNAKE_CASE__ ( unittest.TestCase ): """simple docstring""" 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=5_12 , __UpperCamelCase=16 , __UpperCamelCase=2 , __UpperCamelCase=0.02 , __UpperCamelCase=4 , ) -> Union[str, Any]: '''simple docstring''' __UpperCamelCase : List[str] = parent __UpperCamelCase : Tuple = batch_size __UpperCamelCase : str = seq_length __UpperCamelCase : List[str] = is_training __UpperCamelCase : Optional[int] = use_attention_mask __UpperCamelCase : Optional[Any] = use_token_type_ids __UpperCamelCase : List[Any] = use_labels __UpperCamelCase : int = vocab_size __UpperCamelCase : Union[str, Any] = hidden_size __UpperCamelCase : Any = num_hidden_layers __UpperCamelCase : List[Any] = num_attention_heads __UpperCamelCase : List[str] = intermediate_size __UpperCamelCase : Any = hidden_act __UpperCamelCase : List[Any] = hidden_dropout_prob __UpperCamelCase : List[Any] = attention_probs_dropout_prob __UpperCamelCase : Dict = max_position_embeddings __UpperCamelCase : List[Any] = type_vocab_size __UpperCamelCase : Optional[int] = type_sequence_label_size __UpperCamelCase : Any = initializer_range __UpperCamelCase : Dict = num_choices def __lowerCamelCase ( self ) -> List[str]: '''simple docstring''' __UpperCamelCase : List[str] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __UpperCamelCase : Tuple = None if self.use_attention_mask: __UpperCamelCase : Union[str, Any] = random_attention_mask([self.batch_size, self.seq_length] ) __UpperCamelCase : Optional[Any] = None if self.use_token_type_ids: __UpperCamelCase : int = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) __UpperCamelCase : Dict = RobertaConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=__UpperCamelCase , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def __lowerCamelCase ( self ) -> Any: '''simple docstring''' __UpperCamelCase : Dict = self.prepare_config_and_inputs() __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase : List[str] = config_and_inputs __UpperCamelCase : str = {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": attention_mask} return config, inputs_dict def __lowerCamelCase ( self ) -> Tuple: '''simple docstring''' __UpperCamelCase : Tuple = self.prepare_config_and_inputs() __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase : Any = config_and_inputs __UpperCamelCase : Optional[int] = True __UpperCamelCase : Tuple = floats_tensor([self.batch_size, self.seq_length, self.hidden_size] ) __UpperCamelCase : List[str] = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2 ) return ( config, input_ids, token_type_ids, encoder_hidden_states, encoder_attention_mask, ) @require_flax class SCREAMING_SNAKE_CASE__ ( lowerCamelCase__ , unittest.TestCase ): """simple docstring""" lowercase : int = True lowercase : int = ( ( FlaxRobertaModel, FlaxRobertaForCausalLM, FlaxRobertaForMaskedLM, FlaxRobertaForSequenceClassification, FlaxRobertaForTokenClassification, FlaxRobertaForMultipleChoice, FlaxRobertaForQuestionAnswering, ) if is_flax_available() else () ) def __lowerCamelCase ( self ) -> str: '''simple docstring''' __UpperCamelCase : int = FlaxRobertaModelTester(self ) @slow def __lowerCamelCase ( self ) -> Dict: '''simple docstring''' for model_class_name in self.all_model_classes: __UpperCamelCase : Optional[int] = model_class_name.from_pretrained("roberta-base" , from_pt=__UpperCamelCase ) __UpperCamelCase : List[Any] = model(np.ones((1, 1) ) ) self.assertIsNotNone(__UpperCamelCase )
171
import copy from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import ClassLabel, Features, Value from .base import TaskTemplate @dataclass(frozen=lowerCamelCase__ ) class SCREAMING_SNAKE_CASE__ ( lowerCamelCase__ ): """simple docstring""" lowercase : str = field(default='text-classification' , metadata={'include_in_asdict_even_if_is_default': True} ) lowercase : ClassVar[Features] = Features({'text': Value('string' )} ) lowercase : ClassVar[Features] = Features({'labels': ClassLabel} ) lowercase : str = "text" lowercase : str = "labels" def __lowerCamelCase ( self , __UpperCamelCase ) -> List[str]: '''simple docstring''' if self.label_column not in features: raise ValueError(f'''Column {self.label_column} is not present in features.''' ) if not isinstance(features[self.label_column] , __UpperCamelCase ): raise ValueError(f'''Column {self.label_column} is not a ClassLabel.''' ) __UpperCamelCase : int = copy.deepcopy(self ) __UpperCamelCase : List[Any] = self.label_schema.copy() __UpperCamelCase : Union[str, Any] = features[self.label_column] __UpperCamelCase : Optional[Any] = label_schema return task_template @property def __lowerCamelCase ( self ) -> Dict[str, str]: '''simple docstring''' return { self.text_column: "text", self.label_column: "labels", }
171
1
from bisect import bisect from itertools import accumulate def UpperCamelCase( __UpperCamelCase : Optional[int] ,__UpperCamelCase : Optional[Any] ,__UpperCamelCase : Dict ,__UpperCamelCase : str ): lowerCAmelCase_ : Union[str, Any] = sorted(zip(__UpperCamelCase ,__UpperCamelCase ) ,key=lambda __UpperCamelCase : x[0] / x[1] ,reverse=__UpperCamelCase ) lowerCAmelCase_ , lowerCAmelCase_ : Any = [i[0] for i in r], [i[1] for i in r] lowerCAmelCase_ : Union[str, Any] = list(accumulate(__UpperCamelCase ) ) lowerCAmelCase_ : List[Any] = bisect(__UpperCamelCase ,__UpperCamelCase ) return ( 0 if k == 0 else sum(vl[:k] ) + (w - acc[k - 1]) * (vl[k]) / (wt[k]) if k != n else sum(vl[:k] ) ) if __name__ == "__main__": import doctest doctest.testmod()
103
import hashlib import unittest from transformers import MODEL_FOR_DEPTH_ESTIMATION_MAPPING, is_torch_available, is_vision_available from transformers.pipelines import DepthEstimationPipeline, pipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_timm, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_torch_available(): import torch if is_vision_available(): from PIL import Image else: class __snake_case : @staticmethod def lowerCamelCase ( *_snake_case : Optional[int] , **_snake_case : int): """simple docstring""" pass def A (__A : Image ) -> str: """simple docstring""" UpperCAmelCase_ = hashlib.mda(image.tobytes() ) return m.hexdigest() @is_pipeline_test @require_vision @require_timm @require_torch class __snake_case ( unittest.TestCase ): UpperCAmelCase__ : Tuple = MODEL_FOR_DEPTH_ESTIMATION_MAPPING def lowerCamelCase ( self : Tuple , _snake_case : Optional[Any] , _snake_case : Optional[Any] , _snake_case : List[str]): """simple docstring""" UpperCAmelCase_ = DepthEstimationPipeline(model=_snake_case , image_processor=_snake_case) return depth_estimator, [ "./tests/fixtures/tests_samples/COCO/000000039769.png", "./tests/fixtures/tests_samples/COCO/000000039769.png", ] def lowerCamelCase ( self : str , _snake_case : Optional[int] , _snake_case : List[str]): """simple docstring""" UpperCAmelCase_ = depth_estimator('''./tests/fixtures/tests_samples/COCO/000000039769.png''') self.assertEqual({'''predicted_depth''': ANY(torch.Tensor), '''depth''': ANY(Image.Image)} , _snake_case) import datasets UpperCAmelCase_ = datasets.load_dataset('''hf-internal-testing/fixtures_image_utils''' , '''image''' , split='''test''') UpperCAmelCase_ = depth_estimator( [ Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''), '''http://images.cocodataset.org/val2017/000000039769.jpg''', # RGBA dataset[0]['''file'''], # LA dataset[1]['''file'''], # L dataset[2]['''file'''], ]) self.assertEqual( [ {'''predicted_depth''': ANY(torch.Tensor), '''depth''': ANY(Image.Image)}, {'''predicted_depth''': ANY(torch.Tensor), '''depth''': ANY(Image.Image)}, {'''predicted_depth''': ANY(torch.Tensor), '''depth''': ANY(Image.Image)}, {'''predicted_depth''': ANY(torch.Tensor), '''depth''': ANY(Image.Image)}, {'''predicted_depth''': ANY(torch.Tensor), '''depth''': ANY(Image.Image)}, ] , _snake_case , ) @require_tf @unittest.skip('''Depth estimation is not implemented in TF''') def lowerCamelCase ( self : Union[str, Any]): """simple docstring""" pass @slow @require_torch def lowerCamelCase ( self : List[str]): """simple docstring""" UpperCAmelCase_ = '''Intel/dpt-large''' UpperCAmelCase_ = pipeline('''depth-estimation''' , model=_snake_case) UpperCAmelCase_ = depth_estimator('''http://images.cocodataset.org/val2017/000000039769.jpg''') UpperCAmelCase_ = hashimage(outputs['''depth''']) # This seems flaky. # self.assertEqual(outputs["depth"], "1a39394e282e9f3b0741a90b9f108977") self.assertEqual(nested_simplify(outputs['''predicted_depth'''].max().item()) , 2_9.3_0_4) self.assertEqual(nested_simplify(outputs['''predicted_depth'''].min().item()) , 2.6_6_2) @require_torch def lowerCamelCase ( self : Optional[Any]): """simple docstring""" self.skipTest('''There is not hf-internal-testing tiny model for either GLPN nor DPT''')
51
0
"""simple docstring""" import math def _snake_case ( ) -> None: '''simple docstring''' lowerCAmelCase_ :Tuple = input("""Enter message: """ ) lowerCAmelCase_ :Optional[Any] = int(input(f"""Enter key [2-{len(lowercase__ ) - 1}]: """ ) ) lowerCAmelCase_ :Tuple = input("""Encryption/Decryption [e/d]: """ ) if mode.lower().startswith("""e""" ): lowerCAmelCase_ :List[Any] = encrypt_message(lowercase__ , lowercase__ ) elif mode.lower().startswith("""d""" ): lowerCAmelCase_ :Tuple = decrypt_message(lowercase__ , lowercase__ ) # Append pipe symbol (vertical bar) to identify spaces at the end. print(f"""Output:\n{text + "|"}""" ) def _snake_case ( lowercase__ : int , lowercase__ : str ) -> str: '''simple docstring''' lowerCAmelCase_ :Optional[int] = [""""""] * key for col in range(lowercase__ ): lowerCAmelCase_ :str = col while pointer < len(lowercase__ ): cipher_text[col] += message[pointer] pointer += key return "".join(lowercase__ ) def _snake_case ( lowercase__ : int , lowercase__ : str ) -> str: '''simple docstring''' lowerCAmelCase_ :Dict = math.ceil(len(lowercase__ ) / key ) lowerCAmelCase_ :int = key lowerCAmelCase_ :List[str] = (num_cols * num_rows) - len(lowercase__ ) lowerCAmelCase_ :Dict = [""""""] * num_cols lowerCAmelCase_ :int = 0 lowerCAmelCase_ :Tuple = 0 for symbol in message: plain_text[col] += symbol col += 1 if ( (col == num_cols) or (col == num_cols - 1) and (row >= num_rows - num_shaded_boxes) ): lowerCAmelCase_ :Any = 0 row += 1 return "".join(lowercase__ ) if __name__ == "__main__": import doctest doctest.testmod() main()
368
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available, is_vision_available, ) __UpperCAmelCase = {'configuration_deit': ['DEIT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'DeiTConfig', 'DeiTOnnxConfig']} try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase = ['DeiTFeatureExtractor'] __UpperCAmelCase = ['DeiTImageProcessor'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase = [ 'DEIT_PRETRAINED_MODEL_ARCHIVE_LIST', 'DeiTForImageClassification', 'DeiTForImageClassificationWithTeacher', 'DeiTForMaskedImageModeling', 'DeiTModel', 'DeiTPreTrainedModel', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCAmelCase = [ 'TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFDeiTForImageClassification', 'TFDeiTForImageClassificationWithTeacher', 'TFDeiTForMaskedImageModeling', 'TFDeiTModel', 'TFDeiTPreTrainedModel', ] if TYPE_CHECKING: from .configuration_deit import DEIT_PRETRAINED_CONFIG_ARCHIVE_MAP, DeiTConfig, DeiTOnnxConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_deit import DeiTFeatureExtractor from .image_processing_deit import DeiTImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_deit import ( DEIT_PRETRAINED_MODEL_ARCHIVE_LIST, DeiTForImageClassification, DeiTForImageClassificationWithTeacher, DeiTForMaskedImageModeling, DeiTModel, DeiTPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_deit import ( TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST, TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher, TFDeiTForMaskedImageModeling, TFDeiTModel, TFDeiTPreTrainedModel, ) else: import sys __UpperCAmelCase = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
1
0
from typing import TYPE_CHECKING from ...file_utils import _LazyModule, is_tokenizers_available, is_torch_available, is_vision_available from ...utils import OptionalDependencyNotAvailable UpperCamelCase = {'''configuration_dpt''': ['''DPT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''DPTConfig''']} try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = ['''DPTFeatureExtractor'''] UpperCamelCase = ['''DPTImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase = [ '''DPT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''DPTForDepthEstimation''', '''DPTForSemanticSegmentation''', '''DPTModel''', '''DPTPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_dpt import DPT_PRETRAINED_CONFIG_ARCHIVE_MAP, DPTConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_dpt import DPTFeatureExtractor from .image_processing_dpt import DPTImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_dpt import ( DPT_PRETRAINED_MODEL_ARCHIVE_LIST, DPTForDepthEstimation, DPTForSemanticSegmentation, DPTModel, DPTPreTrainedModel, ) else: import sys UpperCamelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
87
import os def _A ( ) -> Tuple: """simple docstring""" with open(os.path.dirname(_lowercase ) + '/p022_names.txt' ) as file: __UpperCamelCase = str(file.readlines()[0] ) __UpperCamelCase = names.replace('"' , '' ).split(',' ) names.sort() __UpperCamelCase = 0 __UpperCamelCase = 0 for i, name in enumerate(_lowercase ): for letter in name: name_score += ord(_lowercase ) - 64 total_score += (i + 1) * name_score __UpperCamelCase = 0 return total_score if __name__ == "__main__": print(solution())
310
0
"""simple docstring""" from math import factorial, radians def __SCREAMING_SNAKE_CASE ( __UpperCAmelCase , __UpperCAmelCase = 18 , __UpperCAmelCase = 10 ): _lowercase : int = angle_in_degrees - ((angle_in_degrees // 360.0) * 360.0) # Converting from degrees to radians _lowercase : List[Any] = radians(__UpperCAmelCase ) _lowercase : List[Any] = angle_in_radians _lowercase : Union[str, Any] = 3 _lowercase : Union[str, Any] = -1 for _ in range(__UpperCAmelCase ): result += (b * (angle_in_radians**a)) / factorial(__UpperCAmelCase ) _lowercase : Tuple = -b # One positive term and the next will be negative and so on... a += 2 # Increased by 2 for every term. return round(__UpperCAmelCase , __UpperCAmelCase ) if __name__ == "__main__": __import__("""doctest""").testmod()
370
"""simple docstring""" import inspect import re from hashlib import shaaaa from typing import Dict, List from .arrow import arrow from .audiofolder import audiofolder from .csv import csv from .imagefolder import imagefolder from .json import json from .pandas import pandas from .parquet import parquet from .sql import sql # noqa F401 from .text import text def __SCREAMING_SNAKE_CASE ( __UpperCAmelCase ): _lowercase : int = [] for line in lines: _lowercase : Dict = re.sub(R"""#.*""" , """""" , __UpperCAmelCase ) # remove comments if line: filtered_lines.append(__UpperCAmelCase ) _lowercase : Tuple = """\n""".join(__UpperCAmelCase ) # Make a hash from all this code _lowercase : Tuple = full_str.encode("""utf-8""" ) return shaaaa(__UpperCAmelCase ).hexdigest() # get importable module names and hash for caching UpperCAmelCase: Tuple = { """csv""": (csv.__name__, _hash_python_lines(inspect.getsource(csv).splitlines())), """json""": (json.__name__, _hash_python_lines(inspect.getsource(json).splitlines())), """pandas""": (pandas.__name__, _hash_python_lines(inspect.getsource(pandas).splitlines())), """parquet""": (parquet.__name__, _hash_python_lines(inspect.getsource(parquet).splitlines())), """arrow""": (arrow.__name__, _hash_python_lines(inspect.getsource(arrow).splitlines())), """text""": (text.__name__, _hash_python_lines(inspect.getsource(text).splitlines())), """imagefolder""": (imagefolder.__name__, _hash_python_lines(inspect.getsource(imagefolder).splitlines())), """audiofolder""": (audiofolder.__name__, _hash_python_lines(inspect.getsource(audiofolder).splitlines())), } # Used to infer the module to use based on the data files extensions UpperCAmelCase: List[str] = { """.csv""": ("""csv""", {}), """.tsv""": ("""csv""", {"""sep""": """\t"""}), """.json""": ("""json""", {}), """.jsonl""": ("""json""", {}), """.parquet""": ("""parquet""", {}), """.arrow""": ("""arrow""", {}), """.txt""": ("""text""", {}), } _EXTENSION_TO_MODULE.update({ext: ("""imagefolder""", {}) for ext in imagefolder.ImageFolder.EXTENSIONS}) _EXTENSION_TO_MODULE.update({ext.upper(): ("""imagefolder""", {}) for ext in imagefolder.ImageFolder.EXTENSIONS}) _EXTENSION_TO_MODULE.update({ext: ("""audiofolder""", {}) for ext in audiofolder.AudioFolder.EXTENSIONS}) _EXTENSION_TO_MODULE.update({ext.upper(): ("""audiofolder""", {}) for ext in audiofolder.AudioFolder.EXTENSIONS}) UpperCAmelCase: Any = {"""imagefolder""", """audiofolder"""} # Used to filter data files based on extensions given a module name UpperCAmelCase: Dict[str, List[str]] = {} for _ext, (_module, _) in _EXTENSION_TO_MODULE.items(): _MODULE_TO_EXTENSIONS.setdefault(_module, []).append(_ext) _MODULE_TO_EXTENSIONS["imagefolder"].append(""".zip""") _MODULE_TO_EXTENSIONS["audiofolder"].append(""".zip""")
336
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available lowercase : Tuple = { "configuration_ctrl": ["CTRL_PRETRAINED_CONFIG_ARCHIVE_MAP", "CTRLConfig"], "tokenization_ctrl": ["CTRLTokenizer"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase : List[Any] = [ "CTRL_PRETRAINED_MODEL_ARCHIVE_LIST", "CTRLForSequenceClassification", "CTRLLMHeadModel", "CTRLModel", "CTRLPreTrainedModel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase : int = [ "TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST", "TFCTRLForSequenceClassification", "TFCTRLLMHeadModel", "TFCTRLModel", "TFCTRLPreTrainedModel", ] if TYPE_CHECKING: from .configuration_ctrl import CTRL_PRETRAINED_CONFIG_ARCHIVE_MAP, CTRLConfig from .tokenization_ctrl import CTRLTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_ctrl import ( CTRL_PRETRAINED_MODEL_ARCHIVE_LIST, CTRLForSequenceClassification, CTRLLMHeadModel, CTRLModel, CTRLPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_ctrl import ( TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST, TFCTRLForSequenceClassification, TFCTRLLMHeadModel, TFCTRLModel, TFCTRLPreTrainedModel, ) else: import sys lowercase : Dict = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
42
'''simple docstring''' import gc import tempfile import unittest import numpy as np import torch from diffusers import VersatileDiffusionTextToImagePipeline from diffusers.utils.testing_utils import nightly, require_torch_gpu, torch_device lowercase : Optional[Any] = False class __UpperCAmelCase ( unittest.TestCase ): pass @nightly @require_torch_gpu class __UpperCAmelCase ( unittest.TestCase ): def lowerCamelCase ( self ): """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def lowerCamelCase ( self ): """simple docstring""" _snake_case = VersatileDiffusionTextToImagePipeline.from_pretrained('shi-labs/versatile-diffusion' ) # remove text_unet pipe.remove_unused_weights() pipe.to(lowerCAmelCase_ ) pipe.set_progress_bar_config(disable=lowerCAmelCase_ ) _snake_case = 'A painting of a squirrel eating a burger ' _snake_case = torch.manual_seed(0 ) _snake_case = pipe( prompt=lowerCAmelCase_ , generator=lowerCAmelCase_ , guidance_scale=7.5 , num_inference_steps=2 , output_type='numpy' ).images with tempfile.TemporaryDirectory() as tmpdirname: pipe.save_pretrained(lowerCAmelCase_ ) _snake_case = VersatileDiffusionTextToImagePipeline.from_pretrained(lowerCAmelCase_ ) pipe.to(lowerCAmelCase_ ) pipe.set_progress_bar_config(disable=lowerCAmelCase_ ) _snake_case = generator.manual_seed(0 ) _snake_case = pipe( prompt=lowerCAmelCase_ , generator=lowerCAmelCase_ , guidance_scale=7.5 , num_inference_steps=2 , output_type='numpy' ).images assert np.abs(image - new_image ).sum() < 1E-5, "Models don't have the same forward pass" def lowerCamelCase ( self ): """simple docstring""" _snake_case = VersatileDiffusionTextToImagePipeline.from_pretrained( 'shi-labs/versatile-diffusion' , torch_dtype=torch.floataa ) pipe.to(lowerCAmelCase_ ) pipe.set_progress_bar_config(disable=lowerCAmelCase_ ) _snake_case = 'A painting of a squirrel eating a burger ' _snake_case = torch.manual_seed(0 ) _snake_case = pipe( prompt=lowerCAmelCase_ , generator=lowerCAmelCase_ , guidance_scale=7.5 , num_inference_steps=50 , output_type='numpy' ).images _snake_case = image[0, 2_53:2_56, 2_53:2_56, -1] assert image.shape == (1, 5_12, 5_12, 3) _snake_case = np.array([0.3367, 0.3169, 0.2656, 0.3870, 0.4790, 0.3796, 0.4009, 0.4878, 0.4778] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2
42
1
from typing import List, Optional, Union import numpy as np import tensorflow as tf from .utils import logging _snake_case = logging.get_logger(__name__) def A ( _lowerCamelCase ): '''simple docstring''' if isinstance(_lowerCamelCase , np.ndarray ): return list(tensor.shape ) _lowerCAmelCase : Optional[Any] = tf.shape(_lowerCamelCase ) if tensor.shape == tf.TensorShape(_lowerCamelCase ): return dynamic _lowerCAmelCase : Optional[int] = tensor.shape.as_list() return [dynamic[i] if s is None else s for i, s in enumerate(_lowerCamelCase )] def A ( _lowerCamelCase , _lowerCamelCase = None , _lowerCamelCase = None ): '''simple docstring''' return tf.nn.softmax(logits=logits + 1e-9 , axis=_lowerCamelCase , name=_lowerCamelCase ) def A ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase=1e-5 , _lowerCamelCase=-1 ): '''simple docstring''' if weight.shape.rank != 1 or bias.shape.rank != 1 or not isinstance(_lowerCamelCase , _lowerCamelCase ): raise NotImplementedError("Only 1D weight and bias tensors are supported for now, with only a single axis." ) # Get mean and variance on the axis to be normalized _lowerCAmelCase : Tuple = tf.nn.moments(_lowerCamelCase , axes=[axis] , keepdims=_lowerCamelCase ) if axis != -1: # Reshape scale and weight to have the same rank as inputs, but with 1 dimensions # on every dimension except axis _lowerCAmelCase : Union[str, Any] = [1] * inputs.shape.rank _lowerCAmelCase : Optional[Any] = shape_list(_lowerCamelCase )[axis] _lowerCAmelCase : int = tf.reshape(_lowerCamelCase , _lowerCamelCase ) _lowerCAmelCase : Any = tf.reshape(_lowerCamelCase , _lowerCamelCase ) # Compute layer normalization using the batch_normalization # function. _lowerCAmelCase : Dict = tf.nn.batch_normalization( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , offset=_lowerCamelCase , scale=_lowerCamelCase , variance_epsilon=_lowerCamelCase , ) return outputs def A ( _lowerCamelCase , _lowerCamelCase=0 , _lowerCamelCase=-1 ): '''simple docstring''' if end_dim < 0: end_dim += input.shape.rank if start_dim < 0: start_dim += input.shape.rank if start_dim == end_dim: return input _lowerCAmelCase : Dict = tf.shape(_lowerCamelCase ) _lowerCAmelCase : Any = tf.math.reduce_prod(in_shape[start_dim : end_dim + 1] ) _lowerCAmelCase : Tuple = tf.concat([in_shape[:start_dim], [flattened_dim], in_shape[end_dim + 1 :]] , axis=0 ) return tf.reshape(_lowerCamelCase , _lowerCamelCase ) def A ( _lowerCamelCase ): '''simple docstring''' if not isinstance(_lowerCamelCase , tf.Tensor ): _lowerCAmelCase : Dict = tf.convert_to_tensor(_lowerCamelCase ) # Catches stray NumPy inputs if encoder_attention_mask.shape.rank == 3: _lowerCAmelCase : Any = encoder_attention_mask[:, None, :, :] if encoder_attention_mask.shape.rank == 2: _lowerCAmelCase : Optional[int] = encoder_attention_mask[:, None, None, :] # T5 has a mask that can compare sequence ids, we can simulate this here with this transposition # Cf. https://github.com/tensorflow/mesh/blob/8d2465e9bc93129b913b5ccc6a59aa97abd96ec6/mesh_tensorflow # /transformer/transformer_layers.py#L270 # encoder_extended_attention_mask = (encoder_extended_attention_mask == # encoder_extended_attention_mask.transpose(-1, -2)) _lowerCAmelCase : List[Any] = ( tf.cast(1 , encoder_attention_mask.dtype ) - encoder_extended_attention_mask ) * encoder_extended_attention_mask.dtype.min return encoder_extended_attention_mask def A ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase = "input_ids" ): '''simple docstring''' tf.debugging.assert_less( _lowerCamelCase , tf.cast(_lowerCamelCase , dtype=tensor.dtype ) , message=( F"The maximum value of {tensor_name} ({tf.math.reduce_max(_lowerCamelCase )}) must be smaller than the embedding " F"layer's input dimension ({embed_dim}). The likely cause is some problem at tokenization time." ) , ) def A ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ): '''simple docstring''' _lowerCAmelCase : str = 64_512 # Check that no item in `data` is larger than `HDF5_OBJECT_HEADER_LIMIT` # because in that case even chunking the array would not make the saving # possible. _lowerCAmelCase : Union[str, Any] = [x for x in data if len(_lowerCamelCase ) > HDF5_OBJECT_HEADER_LIMIT] # Expecting this to never be true. if bad_attributes: raise RuntimeError( "The following attributes cannot be saved to HDF5 file because " F"they are larger than {HDF5_OBJECT_HEADER_LIMIT} " F"bytes: {bad_attributes}" ) _lowerCAmelCase : Union[str, Any] = np.asarray(_lowerCamelCase ) _lowerCAmelCase : Optional[int] = 1 _lowerCAmelCase : Optional[int] = np.array_split(_lowerCamelCase , _lowerCamelCase ) # This will never loop forever thanks to the test above. while any(x.nbytes > HDF5_OBJECT_HEADER_LIMIT for x in chunked_data ): num_chunks += 1 _lowerCAmelCase : str = np.array_split(_lowerCamelCase , _lowerCamelCase ) if num_chunks > 1: for chunk_id, chunk_data in enumerate(_lowerCamelCase ): _lowerCAmelCase : Any = chunk_data else: _lowerCAmelCase : Dict = data def A ( _lowerCamelCase , _lowerCamelCase ): '''simple docstring''' if name in group.attrs: _lowerCAmelCase : Dict = [n.decode("utf8" ) if hasattr(_lowerCamelCase , "decode" ) else n for n in group.attrs[name]] else: _lowerCAmelCase : Optional[Any] = [] _lowerCAmelCase : List[Any] = 0 while "%s%d" % (name, chunk_id) in group.attrs: data.extend( [n.decode("utf8" ) if hasattr(_lowerCamelCase , "decode" ) else n for n in group.attrs["%s%d" % (name, chunk_id)]] ) chunk_id += 1 return data def A ( _lowerCamelCase ): '''simple docstring''' def _expand_single_ad_tensor(_lowerCamelCase ): if isinstance(_lowerCamelCase , tf.Tensor ) and t.shape.rank == 1: return tf.expand_dims(_lowerCamelCase , axis=-1 ) return t return tf.nest.map_structure(_expand_single_ad_tensor , _lowerCamelCase )
358
import logging import sys from dataclasses import dataclass, field from typing import Any, Dict, List, Optional, Union import librosa import torch from datasets import DatasetDict, load_dataset from packaging import version from torch import nn from transformers import ( HfArgumentParser, Trainer, TrainingArguments, WavaVecaConfig, WavaVecaFeatureExtractor, WavaVecaForPreTraining, is_apex_available, trainer_utils, ) from transformers.models.wavaveca.modeling_wavaveca import _compute_mask_indices if is_apex_available(): from apex import amp if version.parse(version.parse(torch.__version__).base_version) >= version.parse("1.6"): _snake_case = True from torch.cuda.amp import autocast _snake_case = logging.getLogger(__name__) @dataclass class UpperCAmelCase_ : lowerCamelCase__ = field( metadata={'help': 'Path to pretrained model or model identifier from huggingface.co/models'}) lowerCamelCase__ = field( default=a , metadata={'help': 'Where do you want to store the pretrained models downloaded from huggingface.co'} , ) lowerCamelCase__ = field( default=a , metadata={'help': 'Whether to freeze the feature extractor layers of the model.'}) lowerCamelCase__ = field( default=a , metadata={'help': 'Whether to log verbose messages or not.'} , ) lowerCamelCase__ = field( default=2.0 , metadata={'help': 'Maximum temperature for gumbel softmax.'}) lowerCamelCase__ = field( default=0.5 , metadata={'help': 'Minimum temperature for gumbel softmax.'}) lowerCamelCase__ = field( default=0.9_9_9_9_9_5 , metadata={'help': 'Decay of gumbel temperature during training.'}) def A ( _lowerCamelCase , _lowerCamelCase ): '''simple docstring''' logging.basicConfig( format="%(asctime)s - %(levelname)s - %(name)s - %(message)s" , datefmt="%m/%d/%Y %H:%M:%S" , handlers=[logging.StreamHandler(sys.stdout )] , ) _lowerCAmelCase : Optional[Any] = logging.WARNING if model_args.verbose_logging: _lowerCAmelCase : Dict = logging.DEBUG elif trainer_utils.is_main_process(training_args.local_rank ): _lowerCAmelCase : str = logging.INFO logger.setLevel(_lowerCamelCase ) @dataclass class UpperCAmelCase_ : lowerCamelCase__ = field( default=a , metadata={'help': 'The name of the dataset to use (via the datasets library).'}) lowerCamelCase__ = field( default=a , metadata={'help': 'The configuration name of the dataset to use (via the datasets library).'}) lowerCamelCase__ = field( default='train' , metadata={ 'help': 'The name of the training data set split to use (via the datasets library). Defaults to \'train\'' } , ) lowerCamelCase__ = field( default='validation' , metadata={ 'help': ( 'The name of the validation data set split to use (via the datasets library). Defaults to \'validation\'' ) } , ) lowerCamelCase__ = field( default='file' , metadata={'help': 'Column in the dataset that contains speech file path. Defaults to \'file\''} , ) lowerCamelCase__ = field( default=a , metadata={'help': 'Overwrite the cached preprocessed datasets or not.'}) lowerCamelCase__ = field( default=1 , metadata={ 'help': 'The percentage of the train set used as validation set in case there\'s no validation split' } , ) lowerCamelCase__ = field( default=a , metadata={'help': 'The number of processes to use for the preprocessing.'} , ) lowerCamelCase__ = field( default=2_0.0 , metadata={'help': 'Filter audio files that are longer than `max_duration_in_seconds` seconds'}) @dataclass class UpperCAmelCase_ : lowerCamelCase__ = 42 lowerCamelCase__ = 42 lowerCamelCase__ = "longest" lowerCamelCase__ = None lowerCamelCase__ = None def __call__( self, __a): '''simple docstring''' _lowerCAmelCase : Any = self.feature_extractor.pad( __a, max_length=self.max_length, padding=self.padding, pad_to_multiple_of=self.pad_to_multiple_of, return_tensors="pt", ) _lowerCAmelCase : Tuple = self.model._get_feat_extract_output_lengths(batch["input_values"].shape[-1]) _lowerCAmelCase : Optional[Any] = batch["input_values"].shape[0] # make sure that no loss is computed on padded inputs if batch["attention_mask"] is not None: # compute real output lengths according to convolution formula _lowerCAmelCase : List[str] = self.model._get_feat_extract_output_lengths(batch["attention_mask"].sum(-1)).to( torch.long) _lowerCAmelCase : Dict = torch.zeros( (batch_size, mask_indices_seq_length), dtype=torch.long, device=batch["input_values"].device) # these two operations makes sure that all values # before the output lengths indices are attended to _lowerCAmelCase : List[str] = 1 _lowerCAmelCase : Union[str, Any] = attention_mask.flip([-1]).cumsum(-1).flip([-1]).bool() # sample randomly masked indices _lowerCAmelCase : Optional[Any] = _compute_mask_indices( (batch_size, mask_indices_seq_length), self.model.config.mask_time_prob, self.model.config.mask_time_length, attention_mask=__a, min_masks=2, ) return batch class UpperCAmelCase_ ( a): def __init__( self, *__a, __a=1, __a=0, __a=1.0, **__a): '''simple docstring''' super().__init__(*__a, **__a) _lowerCAmelCase : Dict = 0 _lowerCAmelCase : List[str] = max_gumbel_temp _lowerCAmelCase : List[Any] = min_gumbel_temp _lowerCAmelCase : int = gumbel_temp_decay def snake_case__ ( self, __a, __a): '''simple docstring''' model.train() _lowerCAmelCase : str = self._prepare_inputs(__a) if self.use_amp: with autocast(): _lowerCAmelCase : Any = self.compute_loss(__a, __a) else: _lowerCAmelCase : Dict = self.compute_loss(__a, __a) if self.args.n_gpu > 1 or self.deepspeed: if model.module.config.ctc_loss_reduction == "mean": _lowerCAmelCase : List[str] = loss.mean() elif model.module.config.ctc_loss_reduction == "sum": _lowerCAmelCase : Union[str, Any] = loss.sum() / (inputs["mask_time_indices"]).sum() else: raise ValueError(f"{model.config.ctc_loss_reduction} is not valid. Choose one of ['mean', 'sum']") if self.args.gradient_accumulation_steps > 1: _lowerCAmelCase : List[str] = loss / self.args.gradient_accumulation_steps if self.use_amp: self.scaler.scale(__a).backward() elif self.use_apex: with amp.scale_loss(__a, self.optimizer) as scaled_loss: scaled_loss.backward() elif self.deepspeed: self.deepspeed.backward(__a) else: loss.backward() self.num_update_step += 1 # make sure gumbel softmax temperature is decayed if self.args.n_gpu > 1 or self.deepspeed: model.module.set_gumbel_temperature( max(self.max_gumbel_temp * self.gumbel_temp_decay**self.num_update_step, self.min_gumbel_temp)) else: model.set_gumbel_temperature( max(self.max_gumbel_temp * self.gumbel_temp_decay**self.num_update_step, self.min_gumbel_temp)) return loss.detach() def A ( ): '''simple docstring''' _lowerCAmelCase : Any = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase : Union[str, Any] = parser.parse_args_into_dataclasses() configure_logger(_lowerCamelCase , _lowerCamelCase ) # Downloading and loading a dataset from the hub. _lowerCAmelCase : List[Any] = load_dataset(data_args.dataset_name , data_args.dataset_config_name , cache_dir=model_args.cache_dir ) if "validation" not in datasets.keys(): # make sure only "validation" and "train" keys remain" _lowerCAmelCase : int = DatasetDict() _lowerCAmelCase : Optional[int] = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split=F"{data_args.train_split_name}[:{data_args.validation_split_percentage}%]" , cache_dir=model_args.cache_dir , ) _lowerCAmelCase : List[str] = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split=F"{data_args.train_split_name}[{data_args.validation_split_percentage}%:]" , cache_dir=model_args.cache_dir , ) else: # make sure only "validation" and "train" keys remain" _lowerCAmelCase : List[str] = DatasetDict() _lowerCAmelCase : List[Any] = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split="validation" , cache_dir=model_args.cache_dir , ) _lowerCAmelCase : Union[str, Any] = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split=F"{data_args.train_split_name}" , cache_dir=model_args.cache_dir , ) # only normalized-inputs-training is supported _lowerCAmelCase : List[Any] = WavaVecaFeatureExtractor.from_pretrained( model_args.model_name_or_path , cache_dir=model_args.cache_dir , do_normalize=_lowerCamelCase ) def prepare_dataset(_lowerCamelCase ): # check that all files have the correct sampling rate _lowerCAmelCase , _lowerCAmelCase : Any = librosa.load(batch[data_args.speech_file_column] , sr=feature_extractor.sampling_rate ) return batch # load audio files into numpy arrays _lowerCAmelCase : Dict = datasets.map( _lowerCamelCase , num_proc=data_args.preprocessing_num_workers , remove_columns=datasets["train"].column_names ) # filter audio files that are too long _lowerCAmelCase : Tuple = vectorized_datasets.filter( lambda _lowerCamelCase : len(data["speech"] ) < int(data_args.max_duration_in_seconds * feature_extractor.sampling_rate ) ) def normalize(_lowerCamelCase ): return feature_extractor(batch["speech"] , sampling_rate=feature_extractor.sampling_rate ) # normalize and transform to `BatchFeatures` _lowerCAmelCase : Dict = vectorized_datasets.map( _lowerCamelCase , batched=_lowerCamelCase , num_proc=data_args.preprocessing_num_workers , load_from_cache_file=not data_args.overwrite_cache , remove_columns=vectorized_datasets["train"].column_names , ) # pretraining is only supported for "newer" stable layer norm architecture # apply_spec_augment has to be True, mask_feature_prob has to be 0.0 _lowerCAmelCase : Tuple = WavaVecaConfig.from_pretrained( model_args.model_name_or_path , cache_dir=model_args.cache_dir , gradient_checkpointing=training_args.gradient_checkpointing , ) if not config.do_stable_layer_norm or config.feat_extract_norm != "layer": raise ValueError( "PreTraining is only supported for ``config.do_stable_layer_norm=True`` and" " ``config.feat_extract_norm='layer'" ) _lowerCAmelCase : Union[str, Any] = WavaVecaForPreTraining(_lowerCamelCase ) _lowerCAmelCase : int = DataCollatorForWavaVecaPretraining(model=_lowerCamelCase , feature_extractor=_lowerCamelCase ) _lowerCAmelCase : Optional[Any] = WavaVecaPreTrainer( model=_lowerCamelCase , data_collator=_lowerCamelCase , args=_lowerCamelCase , train_dataset=vectorized_datasets["train"] , eval_dataset=vectorized_datasets["validation"] , tokenizer=_lowerCamelCase , max_gumbel_temp=model_args.max_gumbel_temperature , min_gumbel_temp=model_args.min_gumbel_temperature , gumbel_temp_decay=model_args.gumbel_temperature_decay , ) trainer.train() if __name__ == "__main__": main()
300
0
from math import ceil, sqrt def __A ( __lowerCamelCase = 100_0000 ) -> int: a = 0 for outer_width in range(3 , (limit // 4) + 2 ): if outer_width**2 > limit: a = max(ceil(sqrt(outer_width**2 - limit ) ) , 1 ) else: a = 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() = }')
228
from math import pi, sqrt def __A ( __lowerCamelCase ) -> float: if num <= 0: raise ValueError("""math domain error""" ) if num > 171.5: raise OverflowError("""math range error""" ) elif num - int(__lowerCamelCase ) not in (0, 0.5): raise NotImplementedError("""num must be an integer or a half-integer""" ) elif num == 0.5: return sqrt(__lowerCamelCase ) else: return 1.0 if num == 1 else (num - 1) * gamma(num - 1 ) def __A ( ) -> None: assert gamma(0.5 ) == sqrt(__lowerCamelCase ) assert gamma(1 ) == 1.0 assert gamma(2 ) == 1.0 if __name__ == "__main__": from doctest import testmod testmod() __UpperCamelCase : str = 1.0 while num: __UpperCamelCase : Dict = float(input("Gamma of: ")) print(F'gamma({num}) = {gamma(num)}') print("\nEnter 0 to exit...")
228
1
"""simple docstring""" import heapq import sys import numpy as np _lowerCAmelCase : Any = tuple[int, int] class UpperCAmelCase_ : def __init__( self : str ): _UpperCAmelCase : List[str] = [] _UpperCAmelCase : str = set() def snake_case_ ( self : Optional[int] ): if not self.empty(): return self.elements[0][0] else: return float("inf" ) def snake_case_ ( self : List[Any] ): return len(self.elements ) == 0 def snake_case_ ( self : Dict , A : Any , A : str ): if item not in self.set: heapq.heappush(self.elements , (priority, item) ) self.set.add(A ) else: # update # print("update", item) _UpperCAmelCase : Optional[int] = [] (_UpperCAmelCase) : Any = heapq.heappop(self.elements ) while x != item: temp.append((pri, x) ) (_UpperCAmelCase) : str = heapq.heappop(self.elements ) temp.append((priority, item) ) for pro, xxx in temp: heapq.heappush(self.elements , (pro, xxx) ) def snake_case_ ( self : List[Any] , A : str ): if item in self.set: self.set.remove(A ) _UpperCAmelCase : int = [] (_UpperCAmelCase) : Dict = heapq.heappop(self.elements ) while x != item: temp.append((pro, x) ) (_UpperCAmelCase) : Union[str, Any] = heapq.heappop(self.elements ) for prito, yyy in temp: heapq.heappush(self.elements , (prito, yyy) ) def snake_case_ ( self : int ): return self.elements[0][1] def snake_case_ ( self : Any ): (_UpperCAmelCase) : Any = heapq.heappop(self.elements ) self.set.remove(A ) return (priority, item) def __snake_case ( SCREAMING_SNAKE_CASE__ : TPos , SCREAMING_SNAKE_CASE__ : TPos ) -> Optional[Any]: '''simple docstring''' _UpperCAmelCase : Optional[Any] = np.array(SCREAMING_SNAKE_CASE__ ) _UpperCAmelCase : int = np.array(SCREAMING_SNAKE_CASE__ ) return np.linalg.norm(a - b ) def __snake_case ( SCREAMING_SNAKE_CASE__ : TPos , SCREAMING_SNAKE_CASE__ : TPos ) -> Optional[Any]: '''simple docstring''' return consistent_heuristic(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) // t def __snake_case ( SCREAMING_SNAKE_CASE__ : TPos , SCREAMING_SNAKE_CASE__ : TPos ) -> Dict: '''simple docstring''' return abs(p[0] - goal[0] ) + abs(p[1] - goal[1] ) def __snake_case ( SCREAMING_SNAKE_CASE__ : TPos , SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : TPos , SCREAMING_SNAKE_CASE__ : dict[TPos, float] ) -> int: '''simple docstring''' _UpperCAmelCase : Tuple = g_function[start] + Wa * heuristics[i](SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) return ans def __snake_case ( SCREAMING_SNAKE_CASE__ : Any , SCREAMING_SNAKE_CASE__ : List[Any] , SCREAMING_SNAKE_CASE__ : str ) -> Dict: '''simple docstring''' _UpperCAmelCase : Union[str, Any] = np.chararray((n, n) ) for i in range(SCREAMING_SNAKE_CASE__ ): for j in range(SCREAMING_SNAKE_CASE__ ): _UpperCAmelCase : Optional[int] = "*" for i in range(SCREAMING_SNAKE_CASE__ ): for j in range(SCREAMING_SNAKE_CASE__ ): if (j, (n - 1) - i) in blocks: _UpperCAmelCase : Any = "#" _UpperCAmelCase : Optional[Any] = "-" _UpperCAmelCase : List[Any] = back_pointer[goal] while x != start: (_UpperCAmelCase) : List[Any] = x # print(x) _UpperCAmelCase : Optional[Any] = "-" _UpperCAmelCase : int = back_pointer[x] _UpperCAmelCase : Union[str, Any] = "-" for i in range(SCREAMING_SNAKE_CASE__ ): for j in range(SCREAMING_SNAKE_CASE__ ): if (i, j) == (0, n - 1): print(grid[i][j] , end=" " ) print("<-- End position" , end=" " ) else: print(grid[i][j] , end=" " ) print() print("^" ) print("Start position" ) print() print("# is an obstacle" ) print("- is the path taken by algorithm" ) print("PATH TAKEN BY THE ALGORITHM IS:-" ) _UpperCAmelCase : int = back_pointer[goal] while x != start: print(SCREAMING_SNAKE_CASE__ , end=" " ) _UpperCAmelCase : Optional[Any] = back_pointer[x] print(SCREAMING_SNAKE_CASE__ ) sys.exit() def __snake_case ( SCREAMING_SNAKE_CASE__ : TPos ) -> List[Any]: '''simple docstring''' if p[0] < 0 or p[0] > n - 1: return False if p[1] < 0 or p[1] > n - 1: return False return True def __snake_case ( SCREAMING_SNAKE_CASE__ : Union[str, Any] , SCREAMING_SNAKE_CASE__ : Union[str, Any] , SCREAMING_SNAKE_CASE__ : Tuple , SCREAMING_SNAKE_CASE__ : List[Any] , SCREAMING_SNAKE_CASE__ : Any , SCREAMING_SNAKE_CASE__ : Any , SCREAMING_SNAKE_CASE__ : Union[str, Any] , SCREAMING_SNAKE_CASE__ : Tuple , ) -> Optional[int]: '''simple docstring''' for itera in range(SCREAMING_SNAKE_CASE__ ): open_list[itera].remove_element(SCREAMING_SNAKE_CASE__ ) # print("s", s) # print("j", j) (_UpperCAmelCase) : int = s _UpperCAmelCase : List[Any] = (x - 1, y) _UpperCAmelCase : Any = (x + 1, y) _UpperCAmelCase : Tuple = (x, y + 1) _UpperCAmelCase : int = (x, y - 1) for neighbours in [left, right, up, down]: if neighbours not in blocks: if valid(SCREAMING_SNAKE_CASE__ ) and neighbours not in visited: # print("neighbour", neighbours) visited.add(SCREAMING_SNAKE_CASE__ ) _UpperCAmelCase : int = -1 _UpperCAmelCase : int = float("inf" ) if valid(SCREAMING_SNAKE_CASE__ ) and g_function[neighbours] > g_function[s] + 1: _UpperCAmelCase : Union[str, Any] = g_function[s] + 1 _UpperCAmelCase : Union[str, Any] = s if neighbours not in close_list_anchor: open_list[0].put(SCREAMING_SNAKE_CASE__ , key(SCREAMING_SNAKE_CASE__ , 0 , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) ) if neighbours not in close_list_inad: for var in range(1 , SCREAMING_SNAKE_CASE__ ): if key(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) <= Wa * key( SCREAMING_SNAKE_CASE__ , 0 , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): open_list[j].put( SCREAMING_SNAKE_CASE__ , key(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) ) def __snake_case ( ) -> List[str]: '''simple docstring''' _UpperCAmelCase : Optional[Any] = [] for x in range(1 , 5 ): for y in range(1 , 6 ): some_list.append((x, y) ) for x in range(15 , 20 ): some_list.append((x, 17) ) for x in range(10 , 19 ): for y in range(1 , 15 ): some_list.append((x, y) ) # L block for x in range(1 , 4 ): for y in range(12 , 19 ): some_list.append((x, y) ) for x in range(3 , 13 ): for y in range(16 , 19 ): some_list.append((x, y) ) return some_list _lowerCAmelCase : Optional[int] = {0: consistent_heuristic, 1: heuristic_a, 2: heuristic_a} _lowerCAmelCase : Any = [ (0, 1), (1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (7, 1), (8, 1), (9, 1), (10, 1), (11, 1), (12, 1), (13, 1), (14, 1), (15, 1), (16, 1), (17, 1), (18, 1), (19, 1), ] _lowerCAmelCase : Optional[int] = make_common_ground() _lowerCAmelCase : Union[str, Any] = blocks_blk # hyper parameters _lowerCAmelCase : Dict = 1 _lowerCAmelCase : Any = 1 _lowerCAmelCase : int = 20 _lowerCAmelCase : int = 3 # one consistent and two other inconsistent # start and end destination _lowerCAmelCase : Union[str, Any] = (0, 0) _lowerCAmelCase : Tuple = (n - 1, n - 1) _lowerCAmelCase : Union[str, Any] = 1 def __snake_case ( SCREAMING_SNAKE_CASE__ : TPos , SCREAMING_SNAKE_CASE__ : TPos , SCREAMING_SNAKE_CASE__ : int ) -> Tuple: '''simple docstring''' _UpperCAmelCase : Any = {start: 0, goal: float("inf" )} _UpperCAmelCase : Dict = {start: -1, goal: -1} _UpperCAmelCase : Any = [] _UpperCAmelCase : Tuple = set() for i in range(SCREAMING_SNAKE_CASE__ ): open_list.append(PriorityQueue() ) open_list[i].put(SCREAMING_SNAKE_CASE__ , key(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) ) _UpperCAmelCase : list[int] = [] _UpperCAmelCase : list[int] = [] while open_list[0].minkey() < float("inf" ): for i in range(1 , SCREAMING_SNAKE_CASE__ ): # print(open_list[0].minkey(), open_list[i].minkey()) if open_list[i].minkey() <= Wa * open_list[0].minkey(): global t t += 1 if g_function[goal] <= open_list[i].minkey(): if g_function[goal] < float("inf" ): do_something(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) else: _UpperCAmelCase : int = open_list[i].top_show() visited.add(SCREAMING_SNAKE_CASE__ ) expand_state( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , ) close_list_inad.append(SCREAMING_SNAKE_CASE__ ) else: if g_function[goal] <= open_list[0].minkey(): if g_function[goal] < float("inf" ): do_something(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) else: _UpperCAmelCase : Optional[int] = open_list[0].top_show() visited.add(SCREAMING_SNAKE_CASE__ ) expand_state( SCREAMING_SNAKE_CASE__ , 0 , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , ) close_list_anchor.append(SCREAMING_SNAKE_CASE__ ) print("No path found to goal" ) print() for i in range(n - 1 , -1 , -1 ): for j in range(SCREAMING_SNAKE_CASE__ ): if (j, i) in blocks: print("#" , end=" " ) elif (j, i) in back_pointer: if (j, i) == (n - 1, n - 1): print("*" , end=" " ) else: print("-" , end=" " ) else: print("*" , end=" " ) if (j, i) == (n - 1, n - 1): print("<-- End position" , end=" " ) print() print("^" ) print("Start position" ) print() print("# is an obstacle" ) print("- is the path taken by algorithm" ) if __name__ == "__main__": multi_a_star(start, goal, n_heuristic)
360
"""simple docstring""" import json import os from functools import lru_cache from typing import List, Optional, Tuple import regex as re from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging _lowerCAmelCase : Optional[int] = logging.get_logger(__name__) _lowerCAmelCase : Dict = {"vocab_file": "vocab.json", "merges_file": "merges.txt"} _lowerCAmelCase : Tuple = { "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 : str = { "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 __snake_case ( ) -> List[Any]: '''simple docstring''' _UpperCAmelCase : str = ( list(range(ord("!" ) , ord("~" ) + 1 ) ) + list(range(ord("¡" ) , ord("¬" ) + 1 ) ) + list(range(ord("®" ) , ord("ÿ" ) + 1 ) ) ) _UpperCAmelCase : Any = bs[:] _UpperCAmelCase : Tuple = 0 for b in range(2**8 ): if b not in bs: bs.append(SCREAMING_SNAKE_CASE__ ) cs.append(2**8 + n ) n += 1 _UpperCAmelCase : Union[str, Any] = [chr(SCREAMING_SNAKE_CASE__ ) for n in cs] return dict(zip(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) ) def __snake_case ( SCREAMING_SNAKE_CASE__ : Optional[Any] ) -> int: '''simple docstring''' _UpperCAmelCase : int = set() _UpperCAmelCase : int = word[0] for char in word[1:]: pairs.add((prev_char, char) ) _UpperCAmelCase : Optional[int] = char return pairs class UpperCAmelCase_ ( _UpperCamelCase ): __SCREAMING_SNAKE_CASE : List[str] = VOCAB_FILES_NAMES __SCREAMING_SNAKE_CASE : List[str] = PRETRAINED_VOCAB_FILES_MAP __SCREAMING_SNAKE_CASE : str = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __SCREAMING_SNAKE_CASE : Union[str, Any] = ['input_ids', 'attention_mask'] def __init__( self : Optional[Any] , A : int , A : Any , A : List[str]="replace" , A : List[Any]="<s>" , A : int="</s>" , A : Union[str, Any]="</s>" , A : Tuple="<s>" , A : str="<unk>" , A : Dict="<pad>" , A : Optional[Any]="<mask>" , A : Tuple=False , **A : Dict , ): _UpperCAmelCase : Tuple = AddedToken(A , lstrip=A , rstrip=A ) if isinstance(A , A ) else bos_token _UpperCAmelCase : int = AddedToken(A , lstrip=A , rstrip=A ) if isinstance(A , A ) else eos_token _UpperCAmelCase : int = AddedToken(A , lstrip=A , rstrip=A ) if isinstance(A , A ) else sep_token _UpperCAmelCase : Dict = AddedToken(A , lstrip=A , rstrip=A ) if isinstance(A , A ) else cls_token _UpperCAmelCase : Optional[int] = AddedToken(A , lstrip=A , rstrip=A ) if isinstance(A , A ) else unk_token _UpperCAmelCase : Dict = AddedToken(A , lstrip=A , rstrip=A ) if isinstance(A , A ) else pad_token # Mask token behave like a normal word, i.e. include the space before it _UpperCAmelCase : Dict = AddedToken(A , lstrip=A , rstrip=A ) if isinstance(A , A ) else mask_token super().__init__( errors=A , bos_token=A , eos_token=A , unk_token=A , sep_token=A , cls_token=A , pad_token=A , mask_token=A , add_prefix_space=A , **A , ) with open(A , encoding="utf-8" ) as vocab_handle: _UpperCAmelCase : Union[str, Any] = json.load(A ) _UpperCAmelCase : List[str] = {v: k for k, v in self.encoder.items()} _UpperCAmelCase : Dict = errors # how to handle errors in decoding _UpperCAmelCase : List[str] = bytes_to_unicode() _UpperCAmelCase : Dict = {v: k for k, v in self.byte_encoder.items()} with open(A , encoding="utf-8" ) as merges_handle: _UpperCAmelCase : Union[str, Any] = merges_handle.read().split("\n" )[1:-1] _UpperCAmelCase : Union[str, Any] = [tuple(merge.split() ) for merge in bpe_merges] _UpperCAmelCase : List[str] = dict(zip(A , range(len(A ) ) ) ) _UpperCAmelCase : Tuple = {} _UpperCAmelCase : Optional[Any] = add_prefix_space # Should have added re.IGNORECASE so BPE merges can happen for capitalized versions of contractions _UpperCAmelCase : Optional[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 snake_case_ ( self : Optional[Any] ): return len(self.encoder ) def snake_case_ ( self : List[str] ): return dict(self.encoder , **self.added_tokens_encoder ) def snake_case_ ( self : Tuple , A : Union[str, Any] ): if token in self.cache: return self.cache[token] _UpperCAmelCase : Optional[int] = tuple(A ) _UpperCAmelCase : Optional[Any] = get_pairs(A ) if not pairs: return token while True: _UpperCAmelCase : Optional[int] = min(A , key=lambda A : self.bpe_ranks.get(A , float("inf" ) ) ) if bigram not in self.bpe_ranks: break _UpperCAmelCase , _UpperCAmelCase : str = bigram _UpperCAmelCase : Dict = [] _UpperCAmelCase : Union[str, Any] = 0 while i < len(A ): try: _UpperCAmelCase : int = word.index(A , A ) except ValueError: new_word.extend(word[i:] ) break else: new_word.extend(word[i:j] ) _UpperCAmelCase : Dict = j if word[i] == first and i < len(A ) - 1 and word[i + 1] == second: new_word.append(first + second ) i += 2 else: new_word.append(word[i] ) i += 1 _UpperCAmelCase : Optional[Any] = tuple(A ) _UpperCAmelCase : Dict = new_word if len(A ) == 1: break else: _UpperCAmelCase : Optional[int] = get_pairs(A ) _UpperCAmelCase : Any = " ".join(A ) _UpperCAmelCase : int = word return word def snake_case_ ( self : Optional[int] , A : List[str] ): _UpperCAmelCase : str = [] for token in re.findall(self.pat , A ): _UpperCAmelCase : Optional[Any] = "".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(A ).split(" " ) ) return bpe_tokens def snake_case_ ( self : Optional[int] , A : Union[str, Any] ): return self.encoder.get(A , self.encoder.get(self.unk_token ) ) def snake_case_ ( self : Union[str, Any] , A : List[str] ): return self.decoder.get(A ) def snake_case_ ( self : Dict , A : int ): _UpperCAmelCase : Tuple = "".join(A ) _UpperCAmelCase : Dict = bytearray([self.byte_decoder[c] for c in text] ).decode("utf-8" , errors=self.errors ) return text def snake_case_ ( self : int , A : str , A : Optional[str] = None ): if not os.path.isdir(A ): logger.error(f'Vocabulary path ({save_directory}) should be a directory' ) return _UpperCAmelCase : List[Any] = os.path.join( A , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) _UpperCAmelCase : Any = os.path.join( A , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["merges_file"] ) with open(A , "w" , encoding="utf-8" ) as f: f.write(json.dumps(self.encoder , indent=2 , sort_keys=A , ensure_ascii=A ) + "\n" ) _UpperCAmelCase : Optional[Any] = 0 with open(A , "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 A : 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!" ) _UpperCAmelCase : Any = token_index writer.write(" ".join(A ) + "\n" ) index += 1 return vocab_file, merge_file def snake_case_ ( self : int , A : List[int] , A : Optional[List[int]] = None ): if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] _UpperCAmelCase : List[Any] = [self.cls_token_id] _UpperCAmelCase : List[Any] = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def snake_case_ ( self : Optional[Any] , A : List[int] , A : Optional[List[int]] = None , A : bool = False ): if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=A , token_ids_a=A , already_has_special_tokens=A ) if token_ids_a is None: return [1] + ([0] * len(A )) + [1] return [1] + ([0] * len(A )) + [1, 1] + ([0] * len(A )) + [1] def snake_case_ ( self : str , A : List[int] , A : Optional[List[int]] = None ): _UpperCAmelCase : Optional[Any] = [self.sep_token_id] _UpperCAmelCase : Any = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def snake_case_ ( self : str , A : List[str] , A : List[str]=False , **A : List[str] ): _UpperCAmelCase : Optional[Any] = kwargs.pop("add_prefix_space" , self.add_prefix_space ) if (is_split_into_words or add_prefix_space) and (len(A ) > 0 and not text[0].isspace()): _UpperCAmelCase : List[Any] = " " + text return (text, kwargs)
202
0
import logging import random import ray from transformers import RagConfig, RagRetriever, RagTokenizer from transformers.models.rag.retrieval_rag import CustomHFIndex __A =logging.getLogger(__name__) class _SCREAMING_SNAKE_CASE : def __init__( self ) -> Optional[int]: lowerCamelCase_ = False def SCREAMING_SNAKE_CASE_( self , lowercase , lowercase , lowercase , lowercase ) -> Optional[int]: if not self.initialized: lowerCamelCase_ = RagRetriever( lowercase , question_encoder_tokenizer=lowercase , generator_tokenizer=lowercase , index=lowercase , init_retrieval=lowercase , ) lowerCamelCase_ = True def SCREAMING_SNAKE_CASE_( self ) -> List[Any]: self.retriever.index.init_index() def SCREAMING_SNAKE_CASE_( self , lowercase , lowercase ) -> Any: lowerCamelCase_ , lowerCamelCase_ = self.retriever._main_retrieve(lowercase , lowercase ) return doc_ids, retrieved_doc_embeds class _SCREAMING_SNAKE_CASE ( snake_case_ ): def __init__( self , lowercase , lowercase , lowercase , lowercase , lowercase=None ) -> Dict: if index is not None and index.is_initialized() and len(lowercase ) > 0: raise ValueError( "When using Ray for distributed fine-tuning, " "you'll need to provide the paths instead, " "as the dataset and the index are loaded " "separately. More info in examples/rag/use_own_knowledge_dataset.py " ) super().__init__( lowercase , question_encoder_tokenizer=lowercase , generator_tokenizer=lowercase , index=lowercase , init_retrieval=lowercase , ) lowerCamelCase_ = retrieval_workers if len(self.retrieval_workers ) > 0: ray.get( [ worker.create_rag_retriever.remote(lowercase , lowercase , lowercase , lowercase ) for worker in self.retrieval_workers ] ) def SCREAMING_SNAKE_CASE_( self ) -> Optional[int]: logger.info("initializing retrieval" ) if len(self.retrieval_workers ) > 0: ray.get([worker.init_retrieval.remote() for worker in self.retrieval_workers] ) else: # Non-distributed training. Load index into this same process. self.index.init_index() def SCREAMING_SNAKE_CASE_( self , lowercase , lowercase ) -> Dict: if len(self.retrieval_workers ) > 0: # Select a random retrieval actor. lowerCamelCase_ = self.retrieval_workers[random.randint(0 , len(self.retrieval_workers ) - 1 )] lowerCamelCase_ , lowerCamelCase_ = ray.get(random_worker.retrieve.remote(lowercase , lowercase ) ) else: lowerCamelCase_ , lowerCamelCase_ = self._main_retrieve(lowercase , lowercase ) return retrieved_doc_embeds, doc_ids, self.index.get_doc_dicts(lowercase ) @classmethod def SCREAMING_SNAKE_CASE_( cls , lowercase , lowercase=None , **lowercase ) -> Optional[int]: return super(lowercase , cls ).get_tokenizers(lowercase , lowercase , **lowercase ) @classmethod def SCREAMING_SNAKE_CASE_( cls , lowercase , lowercase , lowercase=None , **lowercase ) -> Optional[Any]: lowerCamelCase_ = kwargs.pop("config" , lowercase ) or RagConfig.from_pretrained(lowercase , **lowercase ) lowerCamelCase_ = RagTokenizer.from_pretrained(lowercase , config=lowercase ) lowerCamelCase_ = rag_tokenizer.question_encoder lowerCamelCase_ = rag_tokenizer.generator if indexed_dataset is not None: lowerCamelCase_ = "custom" lowerCamelCase_ = CustomHFIndex(config.retrieval_vector_size , lowercase ) else: lowerCamelCase_ = cls._build_index(lowercase ) return cls( lowercase , question_encoder_tokenizer=lowercase , generator_tokenizer=lowercase , retrieval_workers=lowercase , index=lowercase , )
19
from abc import ABC, abstractmethod from argparse import ArgumentParser class _SCREAMING_SNAKE_CASE ( snake_case_ ): @staticmethod @abstractmethod def SCREAMING_SNAKE_CASE_( lowercase ) -> int: raise NotImplementedError() @abstractmethod def SCREAMING_SNAKE_CASE_( self ) -> Union[str, Any]: raise NotImplementedError()
19
1
import argparse import logging import pickle from collections import Counter logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''', datefmt='''%m/%d/%Y %H:%M:%S''', level=logging.INFO ) __UpperCAmelCase = logging.getLogger(__name__) if __name__ == "__main__": __UpperCAmelCase = argparse.ArgumentParser( description='''Token Counts for smoothing the masking probabilities in MLM (cf XLM/word2vec)''' ) parser.add_argument( '''--data_file''', type=str, default='''data/dump.bert-base-uncased.pickle''', help='''The binarized dataset.''' ) parser.add_argument( '''--token_counts_dump''', type=str, default='''data/token_counts.bert-base-uncased.pickle''', help='''The dump file.''' ) parser.add_argument('''--vocab_size''', default=3_05_22, type=int) __UpperCAmelCase = parser.parse_args() logger.info(f"""Loading data from {args.data_file}""") with open(args.data_file, '''rb''') as fp: __UpperCAmelCase = pickle.load(fp) logger.info('''Counting occurrences for MLM.''') __UpperCAmelCase = Counter() for tk_ids in data: counter.update(tk_ids) __UpperCAmelCase = [0] * args.vocab_size for k, v in counter.items(): __UpperCAmelCase = v logger.info(f"""Dump to {args.token_counts_dump}""") with open(args.token_counts_dump, '''wb''') as handle: pickle.dump(counts, handle, protocol=pickle.HIGHEST_PROTOCOL)
350
import os def __lowerCamelCase ( __magic_name__ : str = "input.txt" ): with open(os.path.join(os.path.dirname(__magic_name__ ) , __magic_name__ ) ) as input_file: a__: str =[ [int(__magic_name__ ) for element in line.split("," )] for line in input_file.readlines() ] a__: int =len(__magic_name__ ) a__: int =len(matrix[0] ) a__: Optional[Any] =[[-1 for _ in range(__magic_name__ )] for _ in range(__magic_name__ )] for i in range(__magic_name__ ): a__: Dict =matrix[i][0] for j in range(1 , __magic_name__ ): for i in range(__magic_name__ ): a__: int =minimal_path_sums[i][j - 1] + matrix[i][j] for i in range(1 , __magic_name__ ): a__: Tuple =min( minimal_path_sums[i][j] , minimal_path_sums[i - 1][j] + matrix[i][j] ) for i in range(rows - 2 , -1 , -1 ): a__: Tuple =min( minimal_path_sums[i][j] , minimal_path_sums[i + 1][j] + matrix[i][j] ) return min(minimal_path_sums_row[-1] for minimal_path_sums_row in minimal_path_sums ) if __name__ == "__main__": print(f"""{solution() = }""")
42
0
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tokenizers_available, is_torch_available, ) A_ :Any = {'''configuration_fnet''': ['''FNET_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''FNetConfig''']} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ :int = ['''FNetTokenizer'''] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ :Dict = ['''FNetTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A_ :Optional[Any] = [ '''FNET_PRETRAINED_MODEL_ARCHIVE_LIST''', '''FNetForMaskedLM''', '''FNetForMultipleChoice''', '''FNetForNextSentencePrediction''', '''FNetForPreTraining''', '''FNetForQuestionAnswering''', '''FNetForSequenceClassification''', '''FNetForTokenClassification''', '''FNetLayer''', '''FNetModel''', '''FNetPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_fnet import FNET_PRETRAINED_CONFIG_ARCHIVE_MAP, FNetConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_fnet import FNetTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_fnet_fast import FNetTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_fnet import ( FNET_PRETRAINED_MODEL_ARCHIVE_LIST, FNetForMaskedLM, FNetForMultipleChoice, FNetForNextSentencePrediction, FNetForPreTraining, FNetForQuestionAnswering, FNetForSequenceClassification, FNetForTokenClassification, FNetLayer, FNetModel, FNetPreTrainedModel, ) else: import sys A_ :List[Any] = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
71
'''simple docstring''' def UpperCAmelCase_ ( __lowerCamelCase : int = 1_00 ): lowercase_ :Tuple = n * (n + 1) * (2 * n + 1) / 6 lowercase_ :List[str] = (n * (n + 1) / 2) ** 2 return int(square_of_sum - sum_of_squares ) if __name__ == "__main__": print(F'''{solution() = }''')
223
0
"""simple docstring""" import os import shutil import sys import tempfile import unittest from pathlib import Path import pytest import transformers from transformers import ( BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, GPT2_PRETRAINED_CONFIG_ARCHIVE_MAP, AutoTokenizer, BertConfig, BertTokenizer, BertTokenizerFast, CTRLTokenizer, GPTaTokenizer, GPTaTokenizerFast, PreTrainedTokenizerFast, RobertaTokenizer, RobertaTokenizerFast, is_tokenizers_available, ) from transformers.models.auto.configuration_auto import CONFIG_MAPPING, AutoConfig from transformers.models.auto.tokenization_auto import ( TOKENIZER_MAPPING, get_tokenizer_config, tokenizer_class_from_name, ) from transformers.models.roberta.configuration_roberta import RobertaConfig from transformers.testing_utils import ( DUMMY_DIFF_TOKENIZER_IDENTIFIER, DUMMY_UNKNOWN_IDENTIFIER, SMALL_MODEL_IDENTIFIER, RequestCounter, require_tokenizers, slow, ) sys.path.append(str(Path(__file__).parent.parent.parent.parent / 'utils')) from test_module.custom_configuration import CustomConfig # noqa E402 from test_module.custom_tokenization import CustomTokenizer # noqa E402 if is_tokenizers_available(): from test_module.custom_tokenization_fast import CustomTokenizerFast class __A ( unittest.TestCase ): '''simple docstring''' def UpperCAmelCase ( self : Tuple ) -> Any: """simple docstring""" lowercase__ : Dict = 0 @slow def UpperCAmelCase ( self : List[str] ) -> Dict: """simple docstring""" for model_name in (x for x in BERT_PRETRAINED_CONFIG_ARCHIVE_MAP.keys() if "japanese" not in x): lowercase__ : Optional[Any] = AutoTokenizer.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,(BertTokenizer, BertTokenizerFast) ) self.assertGreater(len(_snake_case ) ,0 ) for model_name in GPT2_PRETRAINED_CONFIG_ARCHIVE_MAP.keys(): lowercase__ : Tuple = AutoTokenizer.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) self.assertIsInstance(_snake_case ,(GPTaTokenizer, GPTaTokenizerFast) ) self.assertGreater(len(_snake_case ) ,0 ) def UpperCAmelCase ( self : Optional[Any] ) -> str: """simple docstring""" lowercase__ : int = AutoTokenizer.from_pretrained(_snake_case ) self.assertIsInstance(_snake_case ,(BertTokenizer, BertTokenizerFast) ) self.assertEqual(tokenizer.vocab_size ,12 ) def UpperCAmelCase ( self : str ) -> Dict: """simple docstring""" lowercase__ : Union[str, Any] = AutoTokenizer.from_pretrained(_snake_case ) self.assertIsInstance(_snake_case ,(RobertaTokenizer, RobertaTokenizerFast) ) self.assertEqual(tokenizer.vocab_size ,20 ) def UpperCAmelCase ( self : int ) -> Dict: """simple docstring""" lowercase__ : List[Any] = AutoConfig.from_pretrained(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) # Check that tokenizer_type ≠ model_type lowercase__ : str = AutoTokenizer.from_pretrained(_snake_case ,config=_snake_case ) self.assertIsInstance(_snake_case ,(BertTokenizer, BertTokenizerFast) ) self.assertEqual(tokenizer.vocab_size ,12 ) def UpperCAmelCase ( self : int ) -> Any: """simple docstring""" with tempfile.TemporaryDirectory() as tmp_dir: shutil.copy('''./tests/fixtures/vocab.txt''' ,os.path.join(_snake_case ,'''vocab.txt''' ) ) lowercase__ : Optional[int] = AutoTokenizer.from_pretrained(_snake_case ,tokenizer_type='''bert''' ,use_fast=_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) with tempfile.TemporaryDirectory() as tmp_dir: shutil.copy('''./tests/fixtures/vocab.json''' ,os.path.join(_snake_case ,'''vocab.json''' ) ) shutil.copy('''./tests/fixtures/merges.txt''' ,os.path.join(_snake_case ,'''merges.txt''' ) ) lowercase__ : Optional[Any] = AutoTokenizer.from_pretrained(_snake_case ,tokenizer_type='''gpt2''' ,use_fast=_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) @require_tokenizers def UpperCAmelCase ( self : Any ) -> Tuple: """simple docstring""" with tempfile.TemporaryDirectory() as tmp_dir: shutil.copy('''./tests/fixtures/vocab.txt''' ,os.path.join(_snake_case ,'''vocab.txt''' ) ) lowercase__ : Union[str, Any] = AutoTokenizer.from_pretrained(_snake_case ,tokenizer_type='''bert''' ) self.assertIsInstance(_snake_case ,_snake_case ) with tempfile.TemporaryDirectory() as tmp_dir: shutil.copy('''./tests/fixtures/vocab.json''' ,os.path.join(_snake_case ,'''vocab.json''' ) ) shutil.copy('''./tests/fixtures/merges.txt''' ,os.path.join(_snake_case ,'''merges.txt''' ) ) lowercase__ : str = AutoTokenizer.from_pretrained(_snake_case ,tokenizer_type='''gpt2''' ) self.assertIsInstance(_snake_case ,_snake_case ) def UpperCAmelCase ( self : Optional[Any] ) -> List[Any]: """simple docstring""" with pytest.raises(_snake_case ): AutoTokenizer.from_pretrained('''./''' ,tokenizer_type='''xxx''' ) @require_tokenizers def UpperCAmelCase ( self : str ) -> str: """simple docstring""" for tokenizer_class in [BertTokenizer, BertTokenizerFast, AutoTokenizer]: lowercase__ : Tuple = tokenizer_class.from_pretrained('''wietsedv/bert-base-dutch-cased''' ) self.assertIsInstance(_snake_case ,(BertTokenizer, BertTokenizerFast) ) if isinstance(_snake_case ,_snake_case ): self.assertEqual(tokenizer.basic_tokenizer.do_lower_case ,_snake_case ) else: self.assertEqual(tokenizer.do_lower_case ,_snake_case ) self.assertEqual(tokenizer.model_max_length ,512 ) @require_tokenizers def UpperCAmelCase ( self : Any ) -> Union[str, Any]: """simple docstring""" for tokenizer_class in [BertTokenizer, BertTokenizerFast, AutoTokenizer]: with self.assertRaisesRegex( _snake_case ,'''julien-c/herlolip-not-exists is not a local folder and is not a valid model identifier''' ,): lowercase__ : Tuple = tokenizer_class.from_pretrained('''julien-c/herlolip-not-exists''' ) def UpperCAmelCase ( self : int ) -> Union[str, Any]: """simple docstring""" lowercase__ : Any = TOKENIZER_MAPPING.values() lowercase__ : Union[str, Any] = [] for slow_tok, fast_tok in tokenizers: if slow_tok is not None: tokenizer_names.append(slow_tok.__name__ ) if fast_tok is not None: tokenizer_names.append(fast_tok.__name__ ) for tokenizer_name in tokenizer_names: # must find the right class tokenizer_class_from_name(_snake_case ) @require_tokenizers def UpperCAmelCase ( self : Tuple ) -> List[Any]: """simple docstring""" self.assertIsInstance(AutoTokenizer.from_pretrained('''bert-base-cased''' ,use_fast=_snake_case ) ,_snake_case ) self.assertIsInstance(AutoTokenizer.from_pretrained('''bert-base-cased''' ) ,_snake_case ) @require_tokenizers def UpperCAmelCase ( self : List[Any] ) -> List[Any]: """simple docstring""" lowercase__ : Optional[int] = AutoTokenizer.from_pretrained('''distilbert-base-uncased''' ,do_lower_case=_snake_case ) lowercase__ : str = '''Hello, world. How are you?''' lowercase__ : Union[str, Any] = tokenizer.tokenize(_snake_case ) self.assertEqual('''[UNK]''' ,tokens[0] ) lowercase__ : Optional[Any] = AutoTokenizer.from_pretrained('''microsoft/mpnet-base''' ,do_lower_case=_snake_case ) lowercase__ : Union[str, Any] = tokenizer.tokenize(_snake_case ) self.assertEqual('''[UNK]''' ,tokens[0] ) @require_tokenizers def UpperCAmelCase ( self : Optional[int] ) -> Union[str, Any]: """simple docstring""" lowercase__ : Optional[Any] = AutoTokenizer.from_pretrained('''robot-test/dummy-tokenizer-fast-with-model-config''' ) self.assertEqual(type(_snake_case ) ,_snake_case ) self.assertEqual(tokenizer.model_max_length ,512 ) self.assertEqual(tokenizer.vocab_size ,30_000 ) self.assertEqual(tokenizer.unk_token ,'''[UNK]''' ) self.assertEqual(tokenizer.padding_side ,'''right''' ) self.assertEqual(tokenizer.truncation_side ,'''right''' ) def UpperCAmelCase ( self : List[Any] ) -> str: """simple docstring""" lowercase__ : Tuple = AutoTokenizer.from_pretrained(_snake_case ) self.assertIsInstance(_snake_case ,(BertTokenizer, BertTokenizerFast) ) with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(_snake_case ) lowercase__ : int = AutoTokenizer.from_pretrained(_snake_case ) self.assertIsInstance(_snake_case ,tokenizer.__class__ ) self.assertEqual(tokenizera.vocab_size ,12 ) def UpperCAmelCase ( self : str ) -> Union[str, Any]: """simple docstring""" lowercase__ : Optional[Any] = AutoTokenizer.from_pretrained('''ctrl''' ) # There is no fast CTRL so this always gives us a slow tokenizer. self.assertIsInstance(_snake_case ,_snake_case ) def UpperCAmelCase ( self : str ) -> Tuple: """simple docstring""" lowercase__ : Optional[Any] = get_tokenizer_config('''bert-base-cased''' ) lowercase__ : Union[str, Any] = config.pop('''_commit_hash''' ,_snake_case ) # If we ever update bert-base-cased tokenizer config, this dict here will need to be updated. self.assertEqual(_snake_case ,{'''do_lower_case''': False} ) # This model does not have a tokenizer_config so we get back an empty dict. lowercase__ : List[Any] = get_tokenizer_config(_snake_case ) self.assertDictEqual(_snake_case ,{} ) # A tokenizer saved with `save_pretrained` always creates a tokenizer config. lowercase__ : Optional[int] = AutoTokenizer.from_pretrained(_snake_case ) with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(_snake_case ) lowercase__ : str = get_tokenizer_config(_snake_case ) # Check the class of the tokenizer was properly saved (note that it always saves the slow class). self.assertEqual(config['''tokenizer_class'''] ,'''BertTokenizer''' ) def UpperCAmelCase ( self : Tuple ) -> Optional[int]: """simple docstring""" try: AutoConfig.register('''custom''' ,_snake_case ) AutoTokenizer.register(_snake_case ,slow_tokenizer_class=_snake_case ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(_snake_case ): AutoTokenizer.register(_snake_case ,slow_tokenizer_class=_snake_case ) lowercase__ : Optional[Any] = CustomTokenizer.from_pretrained(_snake_case ) with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(_snake_case ) lowercase__ : str = AutoTokenizer.from_pretrained(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in TOKENIZER_MAPPING._extra_content: del TOKENIZER_MAPPING._extra_content[CustomConfig] @require_tokenizers def UpperCAmelCase ( self : Optional[Any] ) -> int: """simple docstring""" try: AutoConfig.register('''custom''' ,_snake_case ) # Can register in two steps AutoTokenizer.register(_snake_case ,slow_tokenizer_class=_snake_case ) self.assertEqual(TOKENIZER_MAPPING[CustomConfig] ,(CustomTokenizer, None) ) AutoTokenizer.register(_snake_case ,fast_tokenizer_class=_snake_case ) self.assertEqual(TOKENIZER_MAPPING[CustomConfig] ,(CustomTokenizer, CustomTokenizerFast) ) del TOKENIZER_MAPPING._extra_content[CustomConfig] # Can register in one step AutoTokenizer.register( _snake_case ,slow_tokenizer_class=_snake_case ,fast_tokenizer_class=_snake_case ) self.assertEqual(TOKENIZER_MAPPING[CustomConfig] ,(CustomTokenizer, CustomTokenizerFast) ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(_snake_case ): AutoTokenizer.register(_snake_case ,fast_tokenizer_class=_snake_case ) # We pass through a bert tokenizer fast cause there is no converter slow to fast for our new toknizer # and that model does not have a tokenizer.json with tempfile.TemporaryDirectory() as tmp_dir: lowercase__ : str = BertTokenizerFast.from_pretrained(_snake_case ) bert_tokenizer.save_pretrained(_snake_case ) lowercase__ : Optional[Any] = CustomTokenizerFast.from_pretrained(_snake_case ) with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(_snake_case ) lowercase__ : Union[str, Any] = AutoTokenizer.from_pretrained(_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) lowercase__ : Optional[Any] = AutoTokenizer.from_pretrained(_snake_case ,use_fast=_snake_case ) self.assertIsInstance(_snake_case ,_snake_case ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in TOKENIZER_MAPPING._extra_content: del TOKENIZER_MAPPING._extra_content[CustomConfig] def UpperCAmelCase ( self : Union[str, Any] ) -> Any: """simple docstring""" with self.assertRaises(_snake_case ): lowercase__ : Dict = AutoTokenizer.from_pretrained('''hf-internal-testing/test_dynamic_tokenizer''' ) # If remote code is disabled, we can't load this config. with self.assertRaises(_snake_case ): lowercase__ : Optional[int] = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer''' ,trust_remote_code=_snake_case ) lowercase__ : str = AutoTokenizer.from_pretrained('''hf-internal-testing/test_dynamic_tokenizer''' ,trust_remote_code=_snake_case ) self.assertTrue(tokenizer.special_attribute_present ) # Test tokenizer can be reloaded. with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(_snake_case ) lowercase__ : Any = AutoTokenizer.from_pretrained(_snake_case ,trust_remote_code=_snake_case ) self.assertTrue(reloaded_tokenizer.special_attribute_present ) if is_tokenizers_available(): self.assertEqual(tokenizer.__class__.__name__ ,'''NewTokenizerFast''' ) self.assertEqual(reloaded_tokenizer.__class__.__name__ ,'''NewTokenizerFast''' ) # Test we can also load the slow version lowercase__ : Any = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer''' ,trust_remote_code=_snake_case ,use_fast=_snake_case ) self.assertTrue(tokenizer.special_attribute_present ) self.assertEqual(tokenizer.__class__.__name__ ,'''NewTokenizer''' ) # Test tokenizer can be reloaded. with tempfile.TemporaryDirectory() as tmp_dir: tokenizer.save_pretrained(_snake_case ) lowercase__ : Dict = AutoTokenizer.from_pretrained(_snake_case ,trust_remote_code=_snake_case ,use_fast=_snake_case ) self.assertEqual(reloaded_tokenizer.__class__.__name__ ,'''NewTokenizer''' ) self.assertTrue(reloaded_tokenizer.special_attribute_present ) else: self.assertEqual(tokenizer.__class__.__name__ ,'''NewTokenizer''' ) self.assertEqual(reloaded_tokenizer.__class__.__name__ ,'''NewTokenizer''' ) @require_tokenizers def UpperCAmelCase ( self : Any ) -> List[Any]: """simple docstring""" class __A ( A_ ): '''simple docstring''' lowerCAmelCase : Tuple = False class __A ( A_ ): '''simple docstring''' lowerCAmelCase : Optional[int] = NewTokenizer lowerCAmelCase : Optional[Any] = False try: AutoConfig.register('''custom''' ,_snake_case ) AutoTokenizer.register(_snake_case ,slow_tokenizer_class=_snake_case ) AutoTokenizer.register(_snake_case ,fast_tokenizer_class=_snake_case ) # If remote code is not set, the default is to use local lowercase__ : int = AutoTokenizer.from_pretrained('''hf-internal-testing/test_dynamic_tokenizer''' ) self.assertEqual(tokenizer.__class__.__name__ ,'''NewTokenizerFast''' ) self.assertFalse(tokenizer.special_attribute_present ) lowercase__ : List[Any] = AutoTokenizer.from_pretrained('''hf-internal-testing/test_dynamic_tokenizer''' ,use_fast=_snake_case ) self.assertEqual(tokenizer.__class__.__name__ ,'''NewTokenizer''' ) self.assertFalse(tokenizer.special_attribute_present ) # If remote code is disabled, we load the local one. lowercase__ : Optional[int] = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer''' ,trust_remote_code=_snake_case ) self.assertEqual(tokenizer.__class__.__name__ ,'''NewTokenizerFast''' ) self.assertFalse(tokenizer.special_attribute_present ) lowercase__ : str = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer''' ,trust_remote_code=_snake_case ,use_fast=_snake_case ) self.assertEqual(tokenizer.__class__.__name__ ,'''NewTokenizer''' ) self.assertFalse(tokenizer.special_attribute_present ) # If remote is enabled, we load from the Hub lowercase__ : List[str] = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer''' ,trust_remote_code=_snake_case ) self.assertEqual(tokenizer.__class__.__name__ ,'''NewTokenizerFast''' ) self.assertTrue(tokenizer.special_attribute_present ) lowercase__ : List[Any] = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer''' ,trust_remote_code=_snake_case ,use_fast=_snake_case ) self.assertEqual(tokenizer.__class__.__name__ ,'''NewTokenizer''' ) self.assertTrue(tokenizer.special_attribute_present ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in TOKENIZER_MAPPING._extra_content: del TOKENIZER_MAPPING._extra_content[CustomConfig] def UpperCAmelCase ( self : Optional[Any] ) -> Tuple: """simple docstring""" lowercase__ : List[str] = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer_legacy''' ,trust_remote_code=_snake_case ) self.assertTrue(tokenizer.special_attribute_present ) if is_tokenizers_available(): self.assertEqual(tokenizer.__class__.__name__ ,'''NewTokenizerFast''' ) # Test we can also load the slow version lowercase__ : Tuple = AutoTokenizer.from_pretrained( '''hf-internal-testing/test_dynamic_tokenizer_legacy''' ,trust_remote_code=_snake_case ,use_fast=_snake_case ) self.assertTrue(tokenizer.special_attribute_present ) self.assertEqual(tokenizer.__class__.__name__ ,'''NewTokenizer''' ) else: self.assertEqual(tokenizer.__class__.__name__ ,'''NewTokenizer''' ) def UpperCAmelCase ( self : int ) -> Dict: """simple docstring""" with self.assertRaisesRegex( _snake_case ,'''bert-base is not a local folder and is not a valid model identifier''' ): lowercase__ : Tuple = AutoTokenizer.from_pretrained('''bert-base''' ) def UpperCAmelCase ( self : Union[str, Any] ) -> Dict: """simple docstring""" with self.assertRaisesRegex( _snake_case ,r'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)''' ): lowercase__ : List[Any] = AutoTokenizer.from_pretrained(_snake_case ,revision='''aaaaaa''' ) def UpperCAmelCase ( self : Union[str, Any] ) -> str: """simple docstring""" lowercase__ : Union[str, Any] = AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-bert''' ) with RequestCounter() as counter: lowercase__ : Dict = AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-bert''' ) self.assertEqual(counter.get_request_count ,0 ) self.assertEqual(counter.head_request_count ,1 ) self.assertEqual(counter.other_request_count ,0 )
371
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) lowerCAmelCase_ = { 'configuration_roberta': ['ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP', 'RobertaConfig', 'RobertaOnnxConfig'], 'tokenization_roberta': ['RobertaTokenizer'], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ = ['RobertaTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ = [ 'ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST', 'RobertaForCausalLM', 'RobertaForMaskedLM', 'RobertaForMultipleChoice', 'RobertaForQuestionAnswering', 'RobertaForSequenceClassification', 'RobertaForTokenClassification', 'RobertaModel', 'RobertaPreTrainedModel', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ = [ 'TF_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFRobertaForCausalLM', 'TFRobertaForMaskedLM', 'TFRobertaForMultipleChoice', 'TFRobertaForQuestionAnswering', 'TFRobertaForSequenceClassification', 'TFRobertaForTokenClassification', 'TFRobertaMainLayer', 'TFRobertaModel', 'TFRobertaPreTrainedModel', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ = [ 'FlaxRobertaForCausalLM', 'FlaxRobertaForMaskedLM', 'FlaxRobertaForMultipleChoice', 'FlaxRobertaForQuestionAnswering', 'FlaxRobertaForSequenceClassification', 'FlaxRobertaForTokenClassification', 'FlaxRobertaModel', 'FlaxRobertaPreTrainedModel', ] if TYPE_CHECKING: from .configuration_roberta import ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, RobertaConfig, RobertaOnnxConfig from .tokenization_roberta import RobertaTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_roberta_fast import RobertaTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_roberta import ( ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, RobertaForCausalLM, RobertaForMaskedLM, RobertaForMultipleChoice, RobertaForQuestionAnswering, RobertaForSequenceClassification, RobertaForTokenClassification, RobertaModel, RobertaPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_roberta import ( TF_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, TFRobertaForCausalLM, TFRobertaForMaskedLM, TFRobertaForMultipleChoice, TFRobertaForQuestionAnswering, TFRobertaForSequenceClassification, TFRobertaForTokenClassification, TFRobertaMainLayer, TFRobertaModel, TFRobertaPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_roberta import ( FlaxRobertaForCausalLM, FlaxRobertaForMaskedLM, FlaxRobertaForMultipleChoice, FlaxRobertaForQuestionAnswering, FlaxRobertaForSequenceClassification, FlaxRobertaForTokenClassification, FlaxRobertaModel, FlaxRobertaPreTrainedModel, ) else: import sys lowerCAmelCase_ = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
302
0
'''simple docstring''' import argparse import torch from transformers import ( WavaVecaConfig, WavaVecaFeatureExtractor, WavaVecaForAudioFrameClassification, WavaVecaForSequenceClassification, WavaVecaForXVector, logging, ) logging.set_verbosity_info() lowerCamelCase : Optional[int] = logging.get_logger(__name__) def _lowerCAmelCase ( _UpperCamelCase : List[Any] , _UpperCamelCase : Tuple , _UpperCamelCase : int ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE =WavaVecaForSequenceClassification.from_pretrained(snake_case_ , config=snake_case_ ) _SCREAMING_SNAKE_CASE =downstream_dict['projector.weight'] _SCREAMING_SNAKE_CASE =downstream_dict['projector.bias'] _SCREAMING_SNAKE_CASE =downstream_dict['model.post_net.linear.weight'] _SCREAMING_SNAKE_CASE =downstream_dict['model.post_net.linear.bias'] return model def _lowerCAmelCase ( _UpperCamelCase : Union[str, Any] , _UpperCamelCase : Dict , _UpperCamelCase : List[str] ) -> Optional[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =WavaVecaForAudioFrameClassification.from_pretrained(snake_case_ , config=snake_case_ ) _SCREAMING_SNAKE_CASE =downstream_dict['model.linear.weight'] _SCREAMING_SNAKE_CASE =downstream_dict['model.linear.bias'] return model def _lowerCAmelCase ( _UpperCamelCase : int , _UpperCamelCase : List[str] , _UpperCamelCase : Union[str, Any] ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE =WavaVecaForXVector.from_pretrained(snake_case_ , config=snake_case_ ) _SCREAMING_SNAKE_CASE =downstream_dict['connector.weight'] _SCREAMING_SNAKE_CASE =downstream_dict['connector.bias'] for i, kernel_size in enumerate(hf_config.tdnn_kernel ): _SCREAMING_SNAKE_CASE =downstream_dict[ f"model.framelevel_feature_extractor.module.{i}.kernel.weight" ] _SCREAMING_SNAKE_CASE =downstream_dict[f"model.framelevel_feature_extractor.module.{i}.kernel.bias"] _SCREAMING_SNAKE_CASE =downstream_dict['model.utterancelevel_feature_extractor.linear1.weight'] _SCREAMING_SNAKE_CASE =downstream_dict['model.utterancelevel_feature_extractor.linear1.bias'] _SCREAMING_SNAKE_CASE =downstream_dict['model.utterancelevel_feature_extractor.linear2.weight'] _SCREAMING_SNAKE_CASE =downstream_dict['model.utterancelevel_feature_extractor.linear2.bias'] _SCREAMING_SNAKE_CASE =downstream_dict['objective.W'] return model @torch.no_grad() def _lowerCAmelCase ( _UpperCamelCase : Dict , _UpperCamelCase : Tuple , _UpperCamelCase : List[Any] , _UpperCamelCase : Dict ) -> List[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =torch.load(snake_case_ , map_location='cpu' ) _SCREAMING_SNAKE_CASE =checkpoint['Downstream'] _SCREAMING_SNAKE_CASE =WavaVecaConfig.from_pretrained(snake_case_ ) _SCREAMING_SNAKE_CASE =WavaVecaFeatureExtractor.from_pretrained( snake_case_ , return_attention_mask=snake_case_ , do_normalize=snake_case_ ) _SCREAMING_SNAKE_CASE =hf_config.architectures[0] if arch.endswith('ForSequenceClassification' ): _SCREAMING_SNAKE_CASE =convert_classification(snake_case_ , snake_case_ , snake_case_ ) elif arch.endswith('ForAudioFrameClassification' ): _SCREAMING_SNAKE_CASE =convert_diarization(snake_case_ , snake_case_ , snake_case_ ) elif arch.endswith('ForXVector' ): _SCREAMING_SNAKE_CASE =convert_xvector(snake_case_ , snake_case_ , snake_case_ ) else: raise NotImplementedError(f"S3PRL weights conversion is not supported for {arch}" ) if hf_config.use_weighted_layer_sum: _SCREAMING_SNAKE_CASE =checkpoint['Featurizer']['weights'] hf_feature_extractor.save_pretrained(snake_case_ ) hf_model.save_pretrained(snake_case_ ) if __name__ == "__main__": lowerCamelCase : int = argparse.ArgumentParser() parser.add_argument( "--base_model_name", default=None, type=str, help="Name of the huggingface pretrained base model." ) parser.add_argument("--config_path", default=None, type=str, help="Path to the huggingface classifier config.") parser.add_argument("--checkpoint_path", default=None, type=str, help="Path to the s3prl checkpoint.") parser.add_argument("--model_dump_path", default=None, type=str, help="Path to the final converted model.") lowerCamelCase : List[str] = parser.parse_args() convert_saprl_checkpoint(args.base_model_name, args.config_path, args.checkpoint_path, args.model_dump_path)
47
'''simple docstring''' 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, DDPMScheduler, StableDiffusionUpscalePipeline, UNetaDConditionModel from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu enable_full_determinism() class __A ( unittest.TestCase ): def _lowercase (self : Tuple ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() @property def _lowercase (self : str ): UpperCAmelCase_ = 1 UpperCAmelCase_ = 3 UpperCAmelCase_ = (32, 32) UpperCAmelCase_ = floats_tensor((batch_size, num_channels) + sizes , rng=random.Random(0 ) ).to(__a ) return image @property def _lowercase (self : int ): torch.manual_seed(0 ) UpperCAmelCase_ = UNetaDConditionModel( block_out_channels=(32, 32, 64) , layers_per_block=2 , sample_size=32 , in_channels=7 , out_channels=4 , down_block_types=("DownBlock2D", "CrossAttnDownBlock2D", "CrossAttnDownBlock2D") , up_block_types=("CrossAttnUpBlock2D", "CrossAttnUpBlock2D", "UpBlock2D") , cross_attention_dim=32 , attention_head_dim=8 , use_linear_projection=__a , only_cross_attention=(True, True, False) , num_class_embeds=100 , ) return model @property def _lowercase (self : Any ): torch.manual_seed(0 ) UpperCAmelCase_ = AutoencoderKL( block_out_channels=[32, 32, 64] , in_channels=3 , out_channels=3 , down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D", "DownEncoderBlock2D"] , up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D", "UpDecoderBlock2D"] , latent_channels=4 , ) return model @property def _lowercase (self : Optional[Any] ): torch.manual_seed(0 ) UpperCAmelCase_ = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , hidden_act="gelu" , projection_dim=512 , ) return CLIPTextModel(__a ) def _lowercase (self : Any ): UpperCAmelCase_ = "cpu" # ensure determinism for the device-dependent torch.Generator UpperCAmelCase_ = self.dummy_cond_unet_upscale UpperCAmelCase_ = DDPMScheduler() UpperCAmelCase_ = DDIMScheduler(prediction_type="v_prediction" ) UpperCAmelCase_ = self.dummy_vae UpperCAmelCase_ = self.dummy_text_encoder UpperCAmelCase_ = CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip" ) UpperCAmelCase_ = self.dummy_image.cpu().permute(0 , 2 , 3 , 1 )[0] UpperCAmelCase_ = Image.fromarray(np.uinta(__a ) ).convert("RGB" ).resize((64, 64) ) # make sure here that pndm scheduler skips prk UpperCAmelCase_ = StableDiffusionUpscalePipeline( unet=__a , low_res_scheduler=__a , scheduler=__a , vae=__a , text_encoder=__a , tokenizer=__a , max_noise_level=350 , ) UpperCAmelCase_ = sd_pipe.to(__a ) sd_pipe.set_progress_bar_config(disable=__a ) UpperCAmelCase_ = "A painting of a squirrel eating a burger" UpperCAmelCase_ = torch.Generator(device=__a ).manual_seed(0 ) UpperCAmelCase_ = sd_pipe( [prompt] , image=__a , generator=__a , guidance_scale=6.0 , noise_level=20 , num_inference_steps=2 , output_type="np" , ) UpperCAmelCase_ = output.images UpperCAmelCase_ = torch.Generator(device=__a ).manual_seed(0 ) UpperCAmelCase_ = sd_pipe( [prompt] , image=__a , generator=__a , guidance_scale=6.0 , noise_level=20 , num_inference_steps=2 , output_type="np" , return_dict=__a , )[0] UpperCAmelCase_ = image[0, -3:, -3:, -1] UpperCAmelCase_ = image_from_tuple[0, -3:, -3:, -1] UpperCAmelCase_ = low_res_image.size[0] * 4 assert image.shape == (1, expected_height_width, expected_height_width, 3) UpperCAmelCase_ = np.array([0.31_13, 0.39_10, 0.42_72, 0.48_59, 0.50_61, 0.46_52, 0.53_62, 0.57_15, 0.56_61] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1E-2 def _lowercase (self : Optional[int] ): UpperCAmelCase_ = "cpu" # ensure determinism for the device-dependent torch.Generator UpperCAmelCase_ = self.dummy_cond_unet_upscale UpperCAmelCase_ = DDPMScheduler() UpperCAmelCase_ = DDIMScheduler(prediction_type="v_prediction" ) UpperCAmelCase_ = self.dummy_vae UpperCAmelCase_ = self.dummy_text_encoder UpperCAmelCase_ = CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip" ) UpperCAmelCase_ = self.dummy_image.cpu().permute(0 , 2 , 3 , 1 )[0] UpperCAmelCase_ = Image.fromarray(np.uinta(__a ) ).convert("RGB" ).resize((64, 64) ) # make sure here that pndm scheduler skips prk UpperCAmelCase_ = StableDiffusionUpscalePipeline( unet=__a , low_res_scheduler=__a , scheduler=__a , vae=__a , text_encoder=__a , tokenizer=__a , max_noise_level=350 , ) UpperCAmelCase_ = sd_pipe.to(__a ) sd_pipe.set_progress_bar_config(disable=__a ) UpperCAmelCase_ = "A painting of a squirrel eating a burger" UpperCAmelCase_ = sd_pipe( 2 * [prompt] , image=2 * [low_res_image] , guidance_scale=6.0 , noise_level=20 , num_inference_steps=2 , output_type="np" , ) UpperCAmelCase_ = output.images assert image.shape[0] == 2 UpperCAmelCase_ = torch.Generator(device=__a ).manual_seed(0 ) UpperCAmelCase_ = sd_pipe( [prompt] , image=__a , generator=__a , num_images_per_prompt=2 , guidance_scale=6.0 , noise_level=20 , num_inference_steps=2 , output_type="np" , ) UpperCAmelCase_ = output.images assert image.shape[0] == 2 @unittest.skipIf(torch_device != "cuda" , "This test requires a GPU" ) def _lowercase (self : str ): UpperCAmelCase_ = self.dummy_cond_unet_upscale UpperCAmelCase_ = DDPMScheduler() UpperCAmelCase_ = DDIMScheduler(prediction_type="v_prediction" ) UpperCAmelCase_ = self.dummy_vae UpperCAmelCase_ = self.dummy_text_encoder UpperCAmelCase_ = CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip" ) UpperCAmelCase_ = self.dummy_image.cpu().permute(0 , 2 , 3 , 1 )[0] UpperCAmelCase_ = Image.fromarray(np.uinta(__a ) ).convert("RGB" ).resize((64, 64) ) # put models in fp16, except vae as it overflows in fp16 UpperCAmelCase_ = unet.half() UpperCAmelCase_ = text_encoder.half() # make sure here that pndm scheduler skips prk UpperCAmelCase_ = StableDiffusionUpscalePipeline( unet=__a , low_res_scheduler=__a , scheduler=__a , vae=__a , text_encoder=__a , tokenizer=__a , max_noise_level=350 , ) UpperCAmelCase_ = sd_pipe.to(__a ) sd_pipe.set_progress_bar_config(disable=__a ) UpperCAmelCase_ = "A painting of a squirrel eating a burger" UpperCAmelCase_ = torch.manual_seed(0 ) UpperCAmelCase_ = sd_pipe( [prompt] , image=__a , generator=__a , num_inference_steps=2 , output_type="np" , ).images UpperCAmelCase_ = low_res_image.size[0] * 4 assert image.shape == (1, expected_height_width, expected_height_width, 3) @slow @require_torch_gpu class __A ( unittest.TestCase ): def _lowercase (self : List[str] ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def _lowercase (self : List[Any] ): UpperCAmelCase_ = load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/sd2-upscale/low_res_cat.png" ) UpperCAmelCase_ = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-upscale" "/upsampled_cat.npy" ) UpperCAmelCase_ = "stabilityai/stable-diffusion-x4-upscaler" UpperCAmelCase_ = StableDiffusionUpscalePipeline.from_pretrained(__a ) pipe.to(__a ) pipe.set_progress_bar_config(disable=__a ) pipe.enable_attention_slicing() UpperCAmelCase_ = "a cat sitting on a park bench" UpperCAmelCase_ = torch.manual_seed(0 ) UpperCAmelCase_ = pipe( prompt=__a , image=__a , generator=__a , output_type="np" , ) UpperCAmelCase_ = output.images[0] assert image.shape == (512, 512, 3) assert np.abs(expected_image - image ).max() < 1E-3 def _lowercase (self : Tuple ): UpperCAmelCase_ = load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/sd2-upscale/low_res_cat.png" ) UpperCAmelCase_ = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-upscale" "/upsampled_cat_fp16.npy" ) UpperCAmelCase_ = "stabilityai/stable-diffusion-x4-upscaler" UpperCAmelCase_ = StableDiffusionUpscalePipeline.from_pretrained( __a , torch_dtype=torch.floataa , ) pipe.to(__a ) pipe.set_progress_bar_config(disable=__a ) pipe.enable_attention_slicing() UpperCAmelCase_ = "a cat sitting on a park bench" UpperCAmelCase_ = torch.manual_seed(0 ) UpperCAmelCase_ = pipe( prompt=__a , image=__a , generator=__a , output_type="np" , ) UpperCAmelCase_ = output.images[0] assert image.shape == (512, 512, 3) assert np.abs(expected_image - image ).max() < 5E-1 def _lowercase (self : List[Any] ): torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats() UpperCAmelCase_ = load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/sd2-upscale/low_res_cat.png" ) UpperCAmelCase_ = "stabilityai/stable-diffusion-x4-upscaler" UpperCAmelCase_ = StableDiffusionUpscalePipeline.from_pretrained( __a , torch_dtype=torch.floataa , ) pipe.to(__a ) pipe.set_progress_bar_config(disable=__a ) pipe.enable_attention_slicing(1 ) pipe.enable_sequential_cpu_offload() UpperCAmelCase_ = "a cat sitting on a park bench" UpperCAmelCase_ = torch.manual_seed(0 ) UpperCAmelCase_ = pipe( prompt=__a , image=__a , generator=__a , num_inference_steps=5 , output_type="np" , ) UpperCAmelCase_ = torch.cuda.max_memory_allocated() # make sure that less than 2.9 GB is allocated assert mem_bytes < 2.9 * 10**9
1
0
"""simple docstring""" from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import rescale, resize, to_channel_dimension_format from ...image_utils import ( ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging if is_vision_available(): import PIL lowerCAmelCase__ = logging.get_logger(__name__) def a__ ( SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : Optional[Any] ): '''simple docstring''' lowerCAmelCase : Tuple = b.T lowerCAmelCase : Union[str, Any] = np.sum(np.square(SCREAMING_SNAKE_CASE ) , axis=1 ) lowerCAmelCase : Optional[int] = np.sum(np.square(SCREAMING_SNAKE_CASE ) , axis=0 ) lowerCAmelCase : Dict = np.matmul(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) lowerCAmelCase : Optional[int] = aa[:, None] - 2 * ab + ba[None, :] return d def a__ ( SCREAMING_SNAKE_CASE : Optional[Any] , SCREAMING_SNAKE_CASE : Optional[Any] ): '''simple docstring''' lowerCAmelCase : str = x.reshape(-1 , 3 ) lowerCAmelCase : Optional[Any] = squared_euclidean_distance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) return np.argmin(SCREAMING_SNAKE_CASE , axis=1 ) class SCREAMING_SNAKE_CASE__ ( lowercase ): """simple docstring""" a : str =["pixel_values"] def __init__( self , snake_case__ = None , snake_case__ = True , snake_case__ = None , snake_case__ = PILImageResampling.BILINEAR , snake_case__ = True , snake_case__ = True , **snake_case__ , ): """simple docstring""" super().__init__(**snake_case__ ) lowerCAmelCase : List[Any] = size if size is not None else {"height": 256, "width": 256} lowerCAmelCase : Any = get_size_dict(snake_case__ ) lowerCAmelCase : Tuple = np.array(snake_case__ ) if clusters is not None else None lowerCAmelCase : Union[str, Any] = do_resize lowerCAmelCase : Optional[Any] = size lowerCAmelCase : Tuple = resample lowerCAmelCase : Dict = do_normalize lowerCAmelCase : Any = do_color_quantize def lowercase__ ( self , snake_case__ , snake_case__ , snake_case__ = PILImageResampling.BILINEAR , snake_case__ = None , **snake_case__ , ): """simple docstring""" lowerCAmelCase : str = get_size_dict(snake_case__ ) if "height" not in size or "width" not in size: raise ValueError(f"""Size dictionary must contain both height and width keys. Got {size.keys()}""" ) return resize( snake_case__ , size=(size["height"], size["width"]) , resample=snake_case__ , data_format=snake_case__ , **snake_case__ ) def lowercase__ ( self , snake_case__ , snake_case__ = None , ): """simple docstring""" lowerCAmelCase : List[Any] = rescale(image=snake_case__ , scale=1 / 127.5 , data_format=snake_case__ ) lowerCAmelCase : Union[str, Any] = image - 1 return image def lowercase__ ( self , snake_case__ , snake_case__ = None , snake_case__ = None , snake_case__ = None , snake_case__ = None , snake_case__ = None , snake_case__ = None , snake_case__ = None , snake_case__ = ChannelDimension.FIRST , **snake_case__ , ): """simple docstring""" lowerCAmelCase : Any = do_resize if do_resize is not None else self.do_resize lowerCAmelCase : int = size if size is not None else self.size lowerCAmelCase : Tuple = get_size_dict(snake_case__ ) lowerCAmelCase : int = resample if resample is not None else self.resample lowerCAmelCase : Dict = do_normalize if do_normalize is not None else self.do_normalize lowerCAmelCase : Tuple = do_color_quantize if do_color_quantize is not None else self.do_color_quantize lowerCAmelCase : Optional[Any] = clusters if clusters is not None else self.clusters lowerCAmelCase : Any = np.array(snake_case__ ) lowerCAmelCase : str = make_list_of_images(snake_case__ ) if not valid_images(snake_case__ ): raise ValueError( "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, " "torch.Tensor, tf.Tensor or jax.ndarray." ) if do_resize and size is None or resample is None: raise ValueError("Size and resample must be specified if do_resize is True." ) if do_color_quantize and clusters is None: raise ValueError("Clusters must be specified if do_color_quantize is True." ) # All transformations expect numpy arrays. lowerCAmelCase : Tuple = [to_numpy_array(snake_case__ ) for image in images] if do_resize: lowerCAmelCase : str = [self.resize(image=snake_case__ , size=snake_case__ , resample=snake_case__ ) for image in images] if do_normalize: lowerCAmelCase : Any = [self.normalize(image=snake_case__ ) for image in images] if do_color_quantize: lowerCAmelCase : Dict = [to_channel_dimension_format(snake_case__ , ChannelDimension.LAST ) for image in images] # color quantize from (batch_size, height, width, 3) to (batch_size, height, width) lowerCAmelCase : Union[str, Any] = np.array(snake_case__ ) lowerCAmelCase : Optional[int] = color_quantize(snake_case__ , snake_case__ ).reshape(images.shape[:-1] ) # flatten to (batch_size, height*width) lowerCAmelCase : int = images.shape[0] lowerCAmelCase : Dict = images.reshape(snake_case__ , -1 ) # We need to convert back to a list of images to keep consistent behaviour across processors. lowerCAmelCase : Optional[int] = list(snake_case__ ) else: lowerCAmelCase : Union[str, Any] = [to_channel_dimension_format(snake_case__ , snake_case__ ) for image in images] lowerCAmelCase : Optional[Any] = {"input_ids": images} return BatchFeature(data=snake_case__ , tensor_type=snake_case__ )
365
"""simple docstring""" import unittest from datasets import load_dataset from transformers import BloomTokenizerFast from transformers.testing_utils import require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class SCREAMING_SNAKE_CASE__ ( lowercase , unittest.TestCase ): """simple docstring""" a : List[str] =None a : List[Any] =BloomTokenizerFast a : Optional[int] =BloomTokenizerFast a : Optional[Any] =True a : Dict =False a : Optional[Any] ="tokenizer_file" a : Optional[int] ={"bos_token": "<s>", "eos_token": "</s>", "unk_token": "<unk>", "pad_token": "<pad>"} def lowercase__ ( self ): """simple docstring""" super().setUp() lowerCAmelCase : Tuple = BloomTokenizerFast.from_pretrained("bigscience/tokenizer" ) tokenizer.save_pretrained(self.tmpdirname ) def lowercase__ ( self , **snake_case__ ): """simple docstring""" kwargs.update(self.special_tokens_map ) return BloomTokenizerFast.from_pretrained(self.tmpdirname , **snake_case__ ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : int = self.get_rust_tokenizer() lowerCAmelCase : List[Any] = ["The quick brown fox</s>", "jumps over the lazy dog</s>"] lowerCAmelCase : str = [[2_175, 23_714, 73_173, 144_252, 2], [77, 132_619, 3_478, 368, 109_586, 35_433, 2]] lowerCAmelCase : Optional[int] = tokenizer.batch_encode_plus(snake_case__ )["input_ids"] self.assertListEqual(snake_case__ , snake_case__ ) lowerCAmelCase : Optional[int] = tokenizer.batch_decode(snake_case__ ) self.assertListEqual(snake_case__ , snake_case__ ) def lowercase__ ( self , snake_case__=6 ): """simple docstring""" for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f"""{tokenizer.__class__.__name__} ({pretrained_name})""" ): lowerCAmelCase : Tuple = self.rust_tokenizer_class.from_pretrained(snake_case__ , **snake_case__ ) # tokenizer_r.pad_token = None # Hotfixing padding = None # Simple input lowerCAmelCase : str = "This is a simple input" lowerCAmelCase : Tuple = ["This is a simple input 1", "This is a simple input 2"] lowerCAmelCase : Any = ("This is a simple input", "This is a pair") lowerCAmelCase : Tuple = [ ("This is a simple input 1", "This is a simple input 2"), ("This is a simple pair 1", "This is a simple pair 2"), ] # Simple input tests try: tokenizer_r.encode(snake_case__ , max_length=snake_case__ ) tokenizer_r.encode_plus(snake_case__ , max_length=snake_case__ ) tokenizer_r.batch_encode_plus(snake_case__ , max_length=snake_case__ ) tokenizer_r.encode(snake_case__ , max_length=snake_case__ ) tokenizer_r.batch_encode_plus(snake_case__ , max_length=snake_case__ ) except ValueError: self.fail("Bloom Tokenizer should be able to deal with padding" ) lowerCAmelCase : Tuple = None # Hotfixing padding = None self.assertRaises(snake_case__ , tokenizer_r.encode , snake_case__ , max_length=snake_case__ , padding="max_length" ) # Simple input self.assertRaises(snake_case__ , tokenizer_r.encode_plus , snake_case__ , max_length=snake_case__ , padding="max_length" ) # Simple input self.assertRaises( snake_case__ , tokenizer_r.batch_encode_plus , snake_case__ , max_length=snake_case__ , padding="max_length" , ) # Pair input self.assertRaises(snake_case__ , tokenizer_r.encode , snake_case__ , max_length=snake_case__ , padding="max_length" ) # Pair input self.assertRaises(snake_case__ , tokenizer_r.encode_plus , snake_case__ , max_length=snake_case__ , padding="max_length" ) # Pair input self.assertRaises( snake_case__ , tokenizer_r.batch_encode_plus , snake_case__ , max_length=snake_case__ , padding="max_length" , ) def lowercase__ ( self ): """simple docstring""" lowerCAmelCase : int = self.get_rust_tokenizer() lowerCAmelCase : int = load_dataset("xnli" , "all_languages" , split="test" , streaming=snake_case__ ) lowerCAmelCase : Tuple = next(iter(snake_case__ ) )["premise"] # pick up one data lowerCAmelCase : Optional[Any] = list(sample_data.values() ) lowerCAmelCase : int = list(map(tokenizer.encode , snake_case__ ) ) lowerCAmelCase : List[Any] = [tokenizer.decode(snake_case__ , clean_up_tokenization_spaces=snake_case__ ) for x in output_tokens] self.assertListEqual(snake_case__ , snake_case__ ) def lowercase__ ( self ): """simple docstring""" self.assertGreaterEqual(len(self.tokenizer_class.pretrained_vocab_files_map ) , 1 ) self.assertGreaterEqual(len(list(self.tokenizer_class.pretrained_vocab_files_map.values() )[0] ) , 1 )
133
0
import argparse import torch from transformers import ( WavaVecaConfig, WavaVecaFeatureExtractor, WavaVecaForAudioFrameClassification, WavaVecaForSequenceClassification, WavaVecaForXVector, logging, ) logging.set_verbosity_info() lowerCAmelCase__ = logging.get_logger(__name__) def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_: Dict , SCREAMING_SNAKE_CASE_: List[Any] , SCREAMING_SNAKE_CASE_: Tuple ) -> List[Any]: '''simple docstring''' A__ = WavaVecaForSequenceClassification.from_pretrained(SCREAMING_SNAKE_CASE_ , config=SCREAMING_SNAKE_CASE_ ) A__ = downstream_dict["projector.weight"] A__ = downstream_dict["projector.bias"] A__ = downstream_dict["model.post_net.linear.weight"] A__ = downstream_dict["model.post_net.linear.bias"] return model def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_: int , SCREAMING_SNAKE_CASE_: Dict , SCREAMING_SNAKE_CASE_: Optional[Any] ) -> str: '''simple docstring''' A__ = WavaVecaForAudioFrameClassification.from_pretrained(SCREAMING_SNAKE_CASE_ , config=SCREAMING_SNAKE_CASE_ ) A__ = downstream_dict["model.linear.weight"] A__ = downstream_dict["model.linear.bias"] return model def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_: Optional[Any] , SCREAMING_SNAKE_CASE_: List[Any] , SCREAMING_SNAKE_CASE_: Any ) -> Union[str, Any]: '''simple docstring''' A__ = WavaVecaForXVector.from_pretrained(SCREAMING_SNAKE_CASE_ , config=SCREAMING_SNAKE_CASE_ ) A__ = downstream_dict["connector.weight"] A__ = downstream_dict["connector.bias"] for i, kernel_size in enumerate(hf_config.tdnn_kernel ): A__ = downstream_dict[ F'model.framelevel_feature_extractor.module.{i}.kernel.weight' ] A__ = downstream_dict[F'model.framelevel_feature_extractor.module.{i}.kernel.bias'] A__ = downstream_dict["model.utterancelevel_feature_extractor.linear1.weight"] A__ = downstream_dict["model.utterancelevel_feature_extractor.linear1.bias"] A__ = downstream_dict["model.utterancelevel_feature_extractor.linear2.weight"] A__ = downstream_dict["model.utterancelevel_feature_extractor.linear2.bias"] A__ = downstream_dict["objective.W"] return model @torch.no_grad() def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_: Dict , SCREAMING_SNAKE_CASE_: Tuple , SCREAMING_SNAKE_CASE_: Optional[Any] , SCREAMING_SNAKE_CASE_: List[str] ) -> Any: '''simple docstring''' A__ = torch.load(SCREAMING_SNAKE_CASE_ , map_location="cpu" ) A__ = checkpoint["Downstream"] A__ = WavaVecaConfig.from_pretrained(SCREAMING_SNAKE_CASE_ ) A__ = WavaVecaFeatureExtractor.from_pretrained( SCREAMING_SNAKE_CASE_ , return_attention_mask=SCREAMING_SNAKE_CASE_ , do_normalize=SCREAMING_SNAKE_CASE_ ) A__ = hf_config.architectures[0] if arch.endswith("ForSequenceClassification" ): A__ = convert_classification(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) elif arch.endswith("ForAudioFrameClassification" ): A__ = convert_diarization(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) elif arch.endswith("ForXVector" ): A__ = convert_xvector(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) else: raise NotImplementedError(F'S3PRL weights conversion is not supported for {arch}' ) if hf_config.use_weighted_layer_sum: A__ = checkpoint["Featurizer"]["weights"] hf_feature_extractor.save_pretrained(SCREAMING_SNAKE_CASE_ ) hf_model.save_pretrained(SCREAMING_SNAKE_CASE_ ) if __name__ == "__main__": lowerCAmelCase__ = argparse.ArgumentParser() parser.add_argument( """--base_model_name""", default=None, type=str, help="""Name of the huggingface pretrained base model.""" ) parser.add_argument("""--config_path""", default=None, type=str, help="""Path to the huggingface classifier config.""") parser.add_argument("""--checkpoint_path""", default=None, type=str, help="""Path to the s3prl checkpoint.""") parser.add_argument("""--model_dump_path""", default=None, type=str, help="""Path to the final converted model.""") lowerCAmelCase__ = parser.parse_args() convert_saprl_checkpoint(args.base_model_name, args.config_path, args.checkpoint_path, args.model_dump_path)
68
'''simple docstring''' import os import tempfile import unittest import numpy as np from diffusers.utils import is_flax_available from diffusers.utils.testing_utils import require_flax, slow if is_flax_available(): import jax import jax.numpy as jnp from flax.jax_utils import replicate from flax.training.common_utils import shard from diffusers import FlaxDDIMScheduler, FlaxDiffusionPipeline, FlaxStableDiffusionPipeline @require_flax class SCREAMING_SNAKE_CASE (unittest.TestCase ): def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' with tempfile.TemporaryDirectory() as tmpdirname: # pipeline has Flax weights __A : Union[str, Any] = FlaxDiffusionPipeline.from_pretrained( 'hf-internal-testing/tiny-stable-diffusion-pipe' , safety_checker=_UpperCAmelCase , cache_dir=_UpperCAmelCase) __A : Optional[Any] = [t[-1] for t in os.walk(os.path.join(_UpperCAmelCase , os.listdir(_UpperCAmelCase)[0] , 'snapshots'))] __A : int = [item for sublist in all_root_files for item in sublist] # None of the downloaded files should be a PyTorch file even if we have some here: # https://huggingface.co/hf-internal-testing/tiny-stable-diffusion-pipe/blob/main/unet/diffusion_pytorch_model.bin assert not any(f.endswith('.bin') for f in files) @slow @require_flax class SCREAMING_SNAKE_CASE (unittest.TestCase ): def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' __A ,__A : Optional[int] = FlaxStableDiffusionPipeline.from_pretrained( 'hf-internal-testing/tiny-stable-diffusion-pipe' , safety_checker=_UpperCAmelCase) __A : Dict = ( 'A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of' ' field, close up, split lighting, cinematic' ) __A : Optional[Any] = jax.random.PRNGKey(0) __A : int = 4 __A : Tuple = jax.device_count() __A : Union[str, Any] = num_samples * [prompt] __A : Tuple = pipeline.prepare_inputs(_UpperCAmelCase) # shard inputs and rng __A : str = replicate(_UpperCAmelCase) __A : Tuple = jax.random.split(_UpperCAmelCase , _UpperCAmelCase) __A : Union[str, Any] = shard(_UpperCAmelCase) __A : Union[str, Any] = pipeline(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , jit=_UpperCAmelCase).images assert images.shape == (num_samples, 1, 64, 64, 3) if jax.device_count() == 8: assert np.abs(np.abs(images[0, 0, :2, :2, -2:] , dtype=np.floataa).sum() - 4.1514745) < 1e-3 assert np.abs(np.abs(_UpperCAmelCase , dtype=np.floataa).sum() - 49947.875) < 5e-1 __A : List[str] = pipeline.numpy_to_pil(np.asarray(images.reshape((num_samples,) + images.shape[-3:]))) assert len(_UpperCAmelCase) == num_samples def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' __A ,__A : str = FlaxStableDiffusionPipeline.from_pretrained( 'CompVis/stable-diffusion-v1-4' , revision='flax' , safety_checker=_UpperCAmelCase) __A : List[Any] = ( 'A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of' ' field, close up, split lighting, cinematic' ) __A : Tuple = jax.random.PRNGKey(0) __A : Any = 50 __A : str = jax.device_count() __A : Union[str, Any] = num_samples * [prompt] __A : List[str] = pipeline.prepare_inputs(_UpperCAmelCase) # shard inputs and rng __A : Dict = replicate(_UpperCAmelCase) __A : Optional[Any] = jax.random.split(_UpperCAmelCase , _UpperCAmelCase) __A : int = shard(_UpperCAmelCase) __A : Tuple = pipeline(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , jit=_UpperCAmelCase).images assert images.shape == (num_samples, 1, 512, 512, 3) if jax.device_count() == 8: assert np.abs((np.abs(images[0, 0, :2, :2, -2:] , dtype=np.floataa).sum() - 0.05652401)) < 1e-3 assert np.abs((np.abs(_UpperCAmelCase , dtype=np.floataa).sum() - 2383808.2)) < 5e-1 def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' __A ,__A : List[str] = FlaxStableDiffusionPipeline.from_pretrained( 'CompVis/stable-diffusion-v1-4' , revision='bf16' , dtype=jnp.bfloataa , safety_checker=_UpperCAmelCase) __A : List[Any] = ( 'A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of' ' field, close up, split lighting, cinematic' ) __A : str = jax.random.PRNGKey(0) __A : Any = 50 __A : Optional[int] = jax.device_count() __A : int = num_samples * [prompt] __A : Optional[int] = pipeline.prepare_inputs(_UpperCAmelCase) # shard inputs and rng __A : Optional[int] = replicate(_UpperCAmelCase) __A : List[str] = jax.random.split(_UpperCAmelCase , _UpperCAmelCase) __A : Dict = shard(_UpperCAmelCase) __A : str = pipeline(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , jit=_UpperCAmelCase).images assert images.shape == (num_samples, 1, 512, 512, 3) if jax.device_count() == 8: assert np.abs((np.abs(images[0, 0, :2, :2, -2:] , dtype=np.floataa).sum() - 0.04003906)) < 1e-3 assert np.abs((np.abs(_UpperCAmelCase , dtype=np.floataa).sum() - 2373516.75)) < 5e-1 def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' __A ,__A : Optional[Any] = FlaxStableDiffusionPipeline.from_pretrained( 'CompVis/stable-diffusion-v1-4' , revision='bf16' , dtype=jnp.bfloataa) __A : Union[str, Any] = ( 'A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of' ' field, close up, split lighting, cinematic' ) __A : Any = jax.random.PRNGKey(0) __A : List[str] = 50 __A : Optional[int] = jax.device_count() __A : List[Any] = num_samples * [prompt] __A : List[Any] = pipeline.prepare_inputs(_UpperCAmelCase) # shard inputs and rng __A : Union[str, Any] = replicate(_UpperCAmelCase) __A : Optional[Any] = jax.random.split(_UpperCAmelCase , _UpperCAmelCase) __A : List[str] = shard(_UpperCAmelCase) __A : int = pipeline(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , jit=_UpperCAmelCase).images assert images.shape == (num_samples, 1, 512, 512, 3) if jax.device_count() == 8: assert np.abs((np.abs(images[0, 0, :2, :2, -2:] , dtype=np.floataa).sum() - 0.04003906)) < 1e-3 assert np.abs((np.abs(_UpperCAmelCase , dtype=np.floataa).sum() - 2373516.75)) < 5e-1 def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' __A : List[Any] = FlaxDDIMScheduler( beta_start=0.00085 , beta_end=0.012 , beta_schedule='scaled_linear' , set_alpha_to_one=_UpperCAmelCase , steps_offset=1 , ) __A ,__A : Any = FlaxStableDiffusionPipeline.from_pretrained( 'CompVis/stable-diffusion-v1-4' , revision='bf16' , dtype=jnp.bfloataa , scheduler=_UpperCAmelCase , safety_checker=_UpperCAmelCase , ) __A : Optional[Any] = scheduler.create_state() __A : Any = scheduler_state __A : List[str] = ( 'A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of' ' field, close up, split lighting, cinematic' ) __A : Union[str, Any] = jax.random.PRNGKey(0) __A : Optional[int] = 50 __A : Optional[Any] = jax.device_count() __A : Any = num_samples * [prompt] __A : Optional[Any] = pipeline.prepare_inputs(_UpperCAmelCase) # shard inputs and rng __A : int = replicate(_UpperCAmelCase) __A : Any = jax.random.split(_UpperCAmelCase , _UpperCAmelCase) __A : Tuple = shard(_UpperCAmelCase) __A : Union[str, Any] = pipeline(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , jit=_UpperCAmelCase).images assert images.shape == (num_samples, 1, 512, 512, 3) if jax.device_count() == 8: assert np.abs((np.abs(images[0, 0, :2, :2, -2:] , dtype=np.floataa).sum() - 0.045043945)) < 1e-3 assert np.abs((np.abs(_UpperCAmelCase , dtype=np.floataa).sum() - 2347693.5)) < 5e-1 def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' __A : Dict = ( 'A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of' ' field, close up, split lighting, cinematic' ) __A : int = jax.device_count() __A : List[Any] = num_samples * [prompt] __A : List[Any] = jax.random.split(jax.random.PRNGKey(0) , _UpperCAmelCase) __A ,__A : Tuple = FlaxStableDiffusionPipeline.from_pretrained( 'CompVis/stable-diffusion-v1-4' , revision='bf16' , dtype=jnp.bfloataa , safety_checker=_UpperCAmelCase , ) __A : str = replicate(_UpperCAmelCase) __A : str = pipeline.prepare_inputs(_UpperCAmelCase) __A : str = shard(_UpperCAmelCase) __A : int = pipeline(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , jit=_UpperCAmelCase).images assert images.shape == (num_samples, 1, 512, 512, 3) __A : Any = images[2, 0, 256, 10:17, 1] # With memory efficient attention __A ,__A : str = FlaxStableDiffusionPipeline.from_pretrained( 'CompVis/stable-diffusion-v1-4' , revision='bf16' , dtype=jnp.bfloataa , safety_checker=_UpperCAmelCase , use_memory_efficient_attention=_UpperCAmelCase , ) __A : Any = replicate(_UpperCAmelCase) __A : List[Any] = pipeline.prepare_inputs(_UpperCAmelCase) __A : Optional[Any] = shard(_UpperCAmelCase) __A : List[Any] = pipeline(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , jit=_UpperCAmelCase).images assert images_eff.shape == (num_samples, 1, 512, 512, 3) __A : List[Any] = images[2, 0, 256, 10:17, 1] # I checked the results visually and they are very similar. However, I saw that the max diff is `1` and the `sum` # over the 8 images is exactly `256`, which is very suspicious. Testing a random slice for now. assert abs(slice_eff - slice).max() < 1e-2
190
0
# this script reports modified .py files under the desired list of top-level sub-dirs passed as a list of arguments, e.g.: # python ./utils/get_modified_files.py utils src tests examples # # it uses git to find the forking point and which files were modified - i.e. files not under git won't be considered # since the output of this script is fed into Makefile commands it doesn't print a newline after the results import re import subprocess import sys UpperCAmelCase__ : Union[str, Any] =subprocess.check_output('''git merge-base main HEAD'''.split()).decode('''utf-8''') UpperCAmelCase__ : List[str] =( subprocess.check_output(F"git diff --diff-filter=d --name-only {fork_point_sha}".split()).decode('''utf-8''').split() ) UpperCAmelCase__ : Tuple ='''|'''.join(sys.argv[1:]) UpperCAmelCase__ : List[str] =re.compile(rF"^({joined_dirs}).*?\.py$") UpperCAmelCase__ : Any =[x for x in modified_files if regex.match(x)] print(''' '''.join(relevant_modified_files), end='''''')
262
from __future__ import annotations from typing import Dict from ...configuration_utils import PretrainedConfig UpperCAmelCase__ : Optional[Any] ={ '''susnato/ernie-m-base_pytorch''': '''https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/config.json''', '''susnato/ernie-m-large_pytorch''': '''https://huggingface.co/susnato/ernie-m-large_pytorch/blob/main/config.json''', } class __A ( a ): __A = """ernie_m""" __A = {"dropout": "classifier_dropout", "num_classes": "num_labels"} def __init__( self , UpperCAmelCase_ = 250002 , UpperCAmelCase_ = 768 , UpperCAmelCase_ = 12 , UpperCAmelCase_ = 12 , UpperCAmelCase_ = 3072 , UpperCAmelCase_ = "gelu" , UpperCAmelCase_ = 0.1 , UpperCAmelCase_ = 0.1 , UpperCAmelCase_ = 514 , UpperCAmelCase_ = 0.0_2 , UpperCAmelCase_ = 1 , UpperCAmelCase_ = 1E-05 , UpperCAmelCase_=None , UpperCAmelCase_=False , UpperCAmelCase_=0.0 , **UpperCAmelCase_ , ): super().__init__(pad_token_id=UpperCAmelCase_ , **UpperCAmelCase_ ) lowerCamelCase =vocab_size lowerCamelCase =hidden_size lowerCamelCase =num_hidden_layers lowerCamelCase =num_attention_heads lowerCamelCase =intermediate_size lowerCamelCase =hidden_act lowerCamelCase =hidden_dropout_prob lowerCamelCase =attention_probs_dropout_prob lowerCamelCase =max_position_embeddings lowerCamelCase =initializer_range lowerCamelCase =layer_norm_eps lowerCamelCase =classifier_dropout lowerCamelCase =is_decoder lowerCamelCase =act_dropout
262
1
"""simple docstring""" import os from datetime import datetime as dt from github import Github __UpperCamelCase = [ '''good first issue''', '''feature request''', '''wip''', ] def lowercase () -> int: SCREAMING_SNAKE_CASE = Github(os.environ['GITHUB_TOKEN'] ) SCREAMING_SNAKE_CASE = g.get_repo('huggingface/accelerate' ) SCREAMING_SNAKE_CASE = repo.get_issues(state='open' ) for issue in open_issues: SCREAMING_SNAKE_CASE = sorted([comment for comment in issue.get_comments()] , key=lambda SCREAMING_SNAKE_CASE_ : i.created_at , reverse=_lowerCAmelCase ) SCREAMING_SNAKE_CASE = comments[0] if len(_lowerCAmelCase ) > 0 else None SCREAMING_SNAKE_CASE = dt.utcnow() SCREAMING_SNAKE_CASE = (current_time - issue.updated_at).days SCREAMING_SNAKE_CASE = (current_time - issue.created_at).days if ( last_comment is not None and last_comment.user.login == "github-actions[bot]" and days_since_updated > 7 and days_since_creation >= 30 and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels() ) ): # Close issue since it has been 7 days of inactivity since bot mention. issue.edit(state='closed' ) elif ( days_since_updated > 23 and days_since_creation >= 30 and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels() ) ): # Add stale comment issue.create_comment( 'This issue has been automatically marked as stale because it has not had ' 'recent activity. If you think this still needs to be addressed ' 'please comment on this thread.\n\nPlease note that issues that do not follow the ' '[contributing guidelines](https://github.com/huggingface/accelerate/blob/main/CONTRIBUTING.md) ' 'are likely to be ignored.' ) if __name__ == "__main__": main()
113
def __snake_case ( _lowerCAmelCase : list ) -> list: if len(_lowerCAmelCase ) <= 1: return [tuple(_lowerCAmelCase )] A_ : Tuple = [] def generate(_lowerCAmelCase : int , _lowerCAmelCase : list ): A_ : List[str] = [0] * n res.append(tuple(_lowerCAmelCase ) ) A_ : int = 0 while i < n: if c[i] < i: if i % 2 == 0: A_ , A_ : str = arr[i], arr[0] else: A_ , A_ : List[str] = arr[i], arr[c[i]] res.append(tuple(_lowerCAmelCase ) ) c[i] += 1 A_ : Tuple = 0 else: A_ : Dict = 0 i += 1 generate(len(_lowerCAmelCase ) , _lowerCAmelCase ) return res if __name__ == "__main__": _lowerCAmelCase : str = input('''Enter numbers separated by a comma:\n''').strip() _lowerCAmelCase : str = [int(item) for item in user_input.split(''',''')] print(heaps(arr))
300
0
from math import isclose, sqrt def _a ( a :float , a :float , a :float ) -> tuple[float, float, float]: a = point_y / 4 / point_x a = 2 * normal_gradient / (1 + normal_gradient * normal_gradient) a = (1 - normal_gradient * normal_gradient) / ( 1 + normal_gradient * normal_gradient ) a = (sa - ca * incoming_gradient) / (ca + sa * incoming_gradient) # to find the next point, solve the simultaeneous equations: # y^2 + 4x^2 = 100 # y - b = m * (x - a) # ==> A x^2 + B x + C = 0 a = outgoing_gradient**2 + 4 a = 2 * outgoing_gradient * (point_y - outgoing_gradient * point_x) a = (point_y - outgoing_gradient * point_x) ** 2 - 100 a = ( -linear_term - sqrt(linear_term**2 - 4 * quadratic_term * constant_term ) ) / (2 * quadratic_term) a = ( -linear_term + sqrt(linear_term**2 - 4 * quadratic_term * constant_term ) ) / (2 * quadratic_term) # two solutions, one of which is our input point a = x_minus if isclose(a , a ) else x_plus a = point_y + outgoing_gradient * (next_x - point_x) return next_x, next_y, outgoing_gradient def _a ( a :float = 1.4 , a :float = -9.6 ) -> int: a = 0 a = first_x_coord a = first_y_coord a = (10.1 - point_y) / (0.0 - point_x) while not (-0.01 <= point_x <= 0.01 and point_y > 0): a , a , a = next_point(a , a , a ) num_reflections += 1 return num_reflections if __name__ == "__main__": print(f"""{solution() = }""")
26
import json import multiprocessing as mp import re from collections import defaultdict from functools import partial from typing import Dict, List, Optional, Set, Tuple, Type from datasets import Dataset from datasketch import MinHash, MinHashLSH from dpu_utils.utils.iterators import ThreadedIterator from tqdm import tqdm UpperCAmelCase__ = re.compile("[^A-Za-z_0-9]") # parameters used in DuplicationIndex UpperCAmelCase__ = 10 UpperCAmelCase__ = 256 def _a ( a :List[str] ) -> Optional[MinHash]: if len(a ) < MIN_NUM_TOKENS: return None a = MinHash(num_perm=a ) for token in set(a ): min_hash.update(token.encode() ) return min_hash def _a ( a :str ) -> Set[str]: return {t for t in NON_ALPHA.split(a ) if len(t.strip() ) > 0} class lowercase_ : '''simple docstring''' def __init__( self : Any , *, __UpperCAmelCase : float = 0.85 , ) ->Dict: """simple docstring""" a = duplication_jaccard_threshold a = NUM_PERM a = MinHashLSH(threshold=self._duplication_jaccard_threshold , num_perm=self._num_perm ) a = defaultdict(__UpperCAmelCase ) def __lowerCAmelCase ( self : List[str] , __UpperCAmelCase : Tuple , __UpperCAmelCase : MinHash ) ->None: """simple docstring""" a = self._index.query(__UpperCAmelCase ) if code_key in self._index.keys: print(F"""Duplicate key {code_key}""" ) return self._index.insert(__UpperCAmelCase , __UpperCAmelCase ) if len(__UpperCAmelCase ) > 0: for base_duplicate in close_duplicates: if base_duplicate in self._duplicate_clusters: self._duplicate_clusters[base_duplicate].add(__UpperCAmelCase ) break else: self._duplicate_clusters[close_duplicates[0]].add(__UpperCAmelCase ) def __lowerCAmelCase ( self : Dict ) ->List[List[Dict]]: """simple docstring""" a = [] for base, duplicates in self._duplicate_clusters.items(): a = [base] + list(__UpperCAmelCase ) # reformat the cluster to be a list of dict a = [{'''base_index''': el[0], '''repo_name''': el[1], '''path''': el[2]} for el in cluster] duplicate_clusters.append(__UpperCAmelCase ) return duplicate_clusters def __lowerCAmelCase ( self : Any , __UpperCAmelCase : Dict ) ->None: """simple docstring""" a = self.get_duplicate_clusters() with open(__UpperCAmelCase , '''w''' ) as f: json.dump(__UpperCAmelCase , __UpperCAmelCase ) def _a ( a :List[Any] ) -> List[Any]: a , a = element a = get_min_hash([t for t in NON_ALPHA.split(data['''content'''] ) if len(t.strip() ) > 0] ) if min_hash is not None: return (index, data["repo_name"], data["path"]), min_hash def _a ( a :Type[Dataset] ) -> List[Any]: with mp.Pool() as pool: for data in pool.imap_unordered( _compute_min_hash , ThreadedIterator(a , max_queue_size=10_000 ) , chunksize=100 , ): if data is not None: yield data def _a ( a :Type[Dataset] , a :float ) -> str: a = DuplicationIndex(duplication_jaccard_threshold=a ) for filename, min_hash in tqdm(ThreadedIterator(minhash_iter(enumerate(a ) ) , max_queue_size=100 ) ): di.add(a , a ) # Returns a List[Cluster] where Cluster is List[str] with the filenames. return di.get_duplicate_clusters() def _a ( a :str , a :str ) -> float: a = get_tokens(a ) a = get_tokens(a ) return len(tokensa & tokensa ) / len(tokensa | tokensa ) UpperCAmelCase__ = None def _a ( a :Tuple , a :Tuple ) -> Any: a = [] for elementa in cluster: a = _shared_dataset[elementa['''base_index''']]['''content'''] for elementa in extremes: a = _shared_dataset[elementa['''base_index''']]['''content'''] if jaccard_similarity(a , a ) >= jaccard_threshold: elementa["copies"] += 1 break else: a = 1 extremes.append(a ) return extremes def _a ( a :List[Any] , a :Optional[Any] , a :Union[str, Any] ) -> Optional[int]: global _shared_dataset a = dataset a = [] a = partial(_find_cluster_extremes_shared , jaccard_threshold=a ) with mp.Pool() as pool: for extremes in tqdm( pool.imap_unordered( a , a , ) , total=len(a ) , ): extremes_list.append(a ) return extremes_list def _a ( a :Type[Dataset] , a :float = 0.85 ) -> Tuple[Type[Dataset], List[List[Dict]]]: a = make_duplicate_clusters(a , a ) a = {x['''base_index'''] for cluster in duplicate_clusters for x in cluster} a = {} a = find_extremes(a , a , a ) for extremes in extremes_clusters: for element in extremes: a = element a = duplicate_indices - set(extreme_dict.keys() ) a = dataset.filter(lambda a , a : idx not in remove_indices , with_indices=a ) # update duplicate_clusters for cluster in duplicate_clusters: for element in cluster: a = element['''base_index'''] in extreme_dict if element["is_extreme"]: a = extreme_dict[element['''base_index''']]['''copies'''] print(F"""Original dataset size: {len(a )}""" ) print(F"""Number of duplicate clusters: {len(a )}""" ) print(F"""Files in duplicate cluster: {len(a )}""" ) print(F"""Unique files in duplicate cluster: {len(a )}""" ) print(F"""Filtered dataset size: {len(a )}""" ) return ds_filter, duplicate_clusters
26
1
import unittest from typing import Dict, List, Optional, Union import numpy as np from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import BridgeTowerImageProcessor class __SCREAMING_SNAKE_CASE( unittest.TestCase ): def __init__( self: Any , UpperCamelCase: List[Any] , UpperCamelCase: Dict = True , UpperCamelCase: Optional[Any] = None , UpperCamelCase: List[Any] = 32 , UpperCamelCase: Optional[Any] = True , UpperCamelCase: Any = 1 / 2_55 , UpperCamelCase: Dict = True , UpperCamelCase: Union[str, Any] = True , UpperCamelCase: int = [0.48_145_466, 0.4_578_275, 0.40_821_073] , UpperCamelCase: Optional[Any] = [0.26_862_954, 0.26_130_258, 0.27_577_711] , UpperCamelCase: int = True , UpperCamelCase: Optional[int]=7 , UpperCamelCase: str=30 , UpperCamelCase: Tuple=4_00 , UpperCamelCase: Tuple=3 , ) -> Tuple: snake_case__ = parent snake_case__ = do_resize snake_case__ = size if size is not None else {"shortest_edge": 2_88} snake_case__ = size_divisor snake_case__ = do_rescale snake_case__ = rescale_factor snake_case__ = do_normalize snake_case__ = do_center_crop snake_case__ = image_mean snake_case__ = image_std snake_case__ = do_pad snake_case__ = batch_size snake_case__ = num_channels snake_case__ = min_resolution snake_case__ = max_resolution def lowerCAmelCase_ ( self: int ) -> Optional[int]: return { "image_mean": self.image_mean, "image_std": self.image_std, "do_normalize": self.do_normalize, "do_resize": self.do_resize, "size": self.size, "size_divisor": self.size_divisor, } def lowerCAmelCase_ ( self: Dict , UpperCamelCase: Any , UpperCamelCase: Optional[Any]=False ) -> int: if not batched: snake_case__ = self.size["shortest_edge"] snake_case__ = image_inputs[0] if isinstance(_a , Image.Image ): snake_case__ = image.size else: snake_case__ = image.shape[1], image.shape[2] snake_case__ = size / min(_a , _a ) if h < w: snake_case__ = size, scale * w else: snake_case__ = scale * h, size snake_case__ = int((13_33 / 8_00) * size ) if max(_a , _a ) > max_size: snake_case__ = max_size / max(_a , _a ) snake_case__ = newh * scale snake_case__ = neww * scale snake_case__ = int(newh + 0.5 ), int(neww + 0.5 ) snake_case__ = ( newh // self.size_divisor * self.size_divisor, neww // self.size_divisor * self.size_divisor, ) else: snake_case__ = [] for image in image_inputs: snake_case__ = self.get_expected_values([image] ) expected_values.append((expected_height, expected_width) ) snake_case__ = max(_a , key=lambda UpperCamelCase : item[0] )[0] snake_case__ = max(_a , key=lambda UpperCamelCase : item[1] )[1] return expected_height, expected_width @require_torch @require_vision class __SCREAMING_SNAKE_CASE( a_ , unittest.TestCase ): _UpperCAmelCase = BridgeTowerImageProcessor if is_vision_available() else None def lowerCAmelCase_ ( self: int ) -> List[Any]: snake_case__ = BridgeTowerImageProcessingTester(self ) @property def lowerCAmelCase_ ( self: List[Any] ) -> Union[str, Any]: return self.image_processor_tester.prepare_image_processor_dict() def lowerCAmelCase_ ( self: List[Any] ) -> List[str]: snake_case__ = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_a , 'image_mean' ) ) self.assertTrue(hasattr(_a , 'image_std' ) ) self.assertTrue(hasattr(_a , 'do_normalize' ) ) self.assertTrue(hasattr(_a , 'do_resize' ) ) self.assertTrue(hasattr(_a , 'size' ) ) self.assertTrue(hasattr(_a , 'size_divisor' ) ) def lowerCAmelCase_ ( self: List[Any] ) -> Any: pass def lowerCAmelCase_ ( self: Optional[Any] ) -> Optional[int]: # Initialize image processor snake_case__ = self.image_processing_class(**self.image_processor_dict ) # create random PIL images snake_case__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=_a ) for image in image_inputs: self.assertIsInstance(_a , Image.Image ) # Test not batched input snake_case__ = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values snake_case__ = self.image_processor_tester.get_expected_values(_a ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched snake_case__ = image_processing(_a , return_tensors='pt' ).pixel_values snake_case__ = self.image_processor_tester.get_expected_values(_a , batched=_a ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def lowerCAmelCase_ ( self: Optional[int] ) -> List[Any]: # Initialize image processor snake_case__ = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors snake_case__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=_a , numpify=_a ) for image in image_inputs: self.assertIsInstance(_a , np.ndarray ) # Test not batched input snake_case__ = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values snake_case__ = self.image_processor_tester.get_expected_values(_a ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched snake_case__ = image_processing(_a , return_tensors='pt' ).pixel_values snake_case__ = self.image_processor_tester.get_expected_values(_a , batched=_a ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def lowerCAmelCase_ ( self: List[str] ) -> Tuple: # Initialize image processor snake_case__ = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors snake_case__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=_a , torchify=_a ) for image in image_inputs: self.assertIsInstance(_a , torch.Tensor ) # Test not batched input snake_case__ = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values snake_case__ = self.image_processor_tester.get_expected_values(_a ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched snake_case__ = image_processing(_a , return_tensors='pt' ).pixel_values snake_case__ = self.image_processor_tester.get_expected_values(_a , batched=_a ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , )
307
"""simple docstring""" import unittest from transformers import load_tool from .test_tools_common import ToolTesterMixin _A : int = """ Hugging Face was founded in 2016 by French entrepreneurs Clément Delangue, Julien Chaumond, and Thomas Wolf originally as a company that developed a chatbot app targeted at teenagers.[2] After open-sourcing the model behind the chatbot, the company pivoted to focus on being a platform for machine learning. In March 2021, Hugging Face raised $40 million in a Series B funding round.[3] On April 28, 2021, the company launched the BigScience Research Workshop in collaboration with several other research groups to release an open large language model.[4] In 2022, the workshop concluded with the announcement of BLOOM, a multilingual large language model with 176 billion parameters.[5] """ class a__ ( unittest.TestCase, a_ ): def __magic_name__ ( self ): lowercase : Tuple = load_tool("text-question-answering" ) self.tool.setup() lowercase : Dict = load_tool("text-question-answering" , remote=_a ) def __magic_name__ ( self ): lowercase : str = self.tool(_a , "What did Hugging Face do in April 2021?" ) self.assertEqual(_a , "launched the BigScience Research Workshop" ) def __magic_name__ ( self ): lowercase : Union[str, Any] = self.remote_tool(_a , "What did Hugging Face do in April 2021?" ) self.assertEqual(_a , "launched the BigScience Research Workshop" ) def __magic_name__ ( self ): lowercase : int = self.tool(text=_a , question="What did Hugging Face do in April 2021?" ) self.assertEqual(_a , "launched the BigScience Research Workshop" ) def __magic_name__ ( self ): lowercase : Optional[Any] = self.remote_tool(text=_a , question="What did Hugging Face do in April 2021?" ) self.assertEqual(_a , "launched the BigScience Research Workshop" )
202
0
import argparse import os from transformers.utils import direct_transformers_import # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/check_task_guides.py lowerCamelCase__ = '''src/transformers''' lowerCamelCase__ = '''docs/source/en/tasks''' def A(__a: Union[str, Any] , __a: List[str] , __a: List[str] ): with open(__a , "r" , encoding="utf-8" , newline="\n" ) as f: lowerCAmelCase_ = f.readlines() # Find the start prompt. lowerCAmelCase_ = 0 while not lines[start_index].startswith(__a ): start_index += 1 start_index += 1 lowerCAmelCase_ = start_index while not lines[end_index].startswith(__a ): end_index += 1 end_index -= 1 while len(lines[start_index] ) <= 1: start_index += 1 while len(lines[end_index] ) <= 1: end_index -= 1 end_index += 1 return "".join(lines[start_index:end_index] ), start_index, end_index, lines # This is to make sure the transformers module imported is the one in the repo. lowerCamelCase__ = direct_transformers_import(TRANSFORMERS_PATH) lowerCamelCase__ = { '''asr.md''': transformers_module.models.auto.modeling_auto.MODEL_FOR_CTC_MAPPING_NAMES, '''audio_classification.md''': transformers_module.models.auto.modeling_auto.MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES, '''language_modeling.md''': transformers_module.models.auto.modeling_auto.MODEL_FOR_CAUSAL_LM_MAPPING_NAMES, '''image_classification.md''': transformers_module.models.auto.modeling_auto.MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES, '''masked_language_modeling.md''': transformers_module.models.auto.modeling_auto.MODEL_FOR_MASKED_LM_MAPPING_NAMES, '''multiple_choice.md''': transformers_module.models.auto.modeling_auto.MODEL_FOR_MULTIPLE_CHOICE_MAPPING_NAMES, '''object_detection.md''': transformers_module.models.auto.modeling_auto.MODEL_FOR_OBJECT_DETECTION_MAPPING_NAMES, '''question_answering.md''': transformers_module.models.auto.modeling_auto.MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES, '''semantic_segmentation.md''': transformers_module.models.auto.modeling_auto.MODEL_FOR_SEMANTIC_SEGMENTATION_MAPPING_NAMES, '''sequence_classification.md''': transformers_module.models.auto.modeling_auto.MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES, '''summarization.md''': transformers_module.models.auto.modeling_auto.MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES, '''token_classification.md''': transformers_module.models.auto.modeling_auto.MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES, '''translation.md''': transformers_module.models.auto.modeling_auto.MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES, '''video_classification.md''': transformers_module.models.auto.modeling_auto.MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING_NAMES, '''document_question_answering.md''': transformers_module.models.auto.modeling_auto.MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING_NAMES, '''monocular_depth_estimation.md''': transformers_module.models.auto.modeling_auto.MODEL_FOR_DEPTH_ESTIMATION_MAPPING_NAMES, } # This list contains model types used in some task guides that are not in `CONFIG_MAPPING_NAMES` (therefore not in any # `MODEL_MAPPING_NAMES` or any `MODEL_FOR_XXX_MAPPING_NAMES`). lowerCamelCase__ = { '''summarization.md''': ('''nllb''',), '''translation.md''': ('''nllb''',), } def A(__a: str ): lowerCAmelCase_ = TASK_GUIDE_TO_MODELS[task_guide] lowerCAmelCase_ = SPECIAL_TASK_GUIDE_TO_MODEL_TYPES.get(__a , set() ) lowerCAmelCase_ = { code: name for code, name in transformers_module.MODEL_NAMES_MAPPING.items() if (code in model_maping_names or code in special_model_types) } return ", ".join([F"[{name}](../model_doc/{code})" for code, name in model_names.items()] ) + "\n" def A(__a: List[str] , __a: Dict=False ): lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ = _find_text_in_file( filename=os.path.join(__a , __a ) , start_prompt="<!--This tip is automatically generated by `make fix-copies`, do not fill manually!-->" , end_prompt="<!--End of the generated tip-->" , ) lowerCAmelCase_ = get_model_list_for_task(__a ) if current_list != new_list: if overwrite: with open(os.path.join(__a , __a ) , "w" , encoding="utf-8" , newline="\n" ) as f: f.writelines(lines[:start_index] + [new_list] + lines[end_index:] ) else: raise ValueError( F"The list of models that can be used in the {task_guide} guide needs an update. Run `make fix-copies`" " to fix this." ) if __name__ == "__main__": lowerCamelCase__ = argparse.ArgumentParser() parser.add_argument('''--fix_and_overwrite''', action='''store_true''', help='''Whether to fix inconsistencies.''') lowerCamelCase__ = parser.parse_args() for task_guide in TASK_GUIDE_TO_MODELS.keys(): check_model_list_for_task(task_guide, args.fix_and_overwrite)
22
import re import jax.numpy as jnp from flax.traverse_util import flatten_dict, unflatten_dict from jax.random import PRNGKey from ..utils import logging lowerCamelCase__ = logging.get_logger(__name__) def A(__a: Dict ): lowerCAmelCase_ = r"\w+[.]\d+" lowerCAmelCase_ = re.findall(__a , __a ) for pat in pats: lowerCAmelCase_ = key.replace(__a , "_".join(pat.split("." ) ) ) return key def A(__a: str , __a: Tuple , __a: List[Any] ): lowerCAmelCase_ = pt_tuple_key[:-1] + ("scale",) if ( any("norm" in str_ for str_ in pt_tuple_key ) and (pt_tuple_key[-1] == "bias") and (pt_tuple_key[:-1] + ("bias",) not in random_flax_state_dict) and (pt_tuple_key[:-1] + ("scale",) in random_flax_state_dict) ): lowerCAmelCase_ = pt_tuple_key[:-1] + ("scale",) return renamed_pt_tuple_key, pt_tensor elif pt_tuple_key[-1] in ["weight", "gamma"] and pt_tuple_key[:-1] + ("scale",) in random_flax_state_dict: lowerCAmelCase_ = pt_tuple_key[:-1] + ("scale",) return renamed_pt_tuple_key, pt_tensor # embedding if pt_tuple_key[-1] == "weight" and pt_tuple_key[:-1] + ("embedding",) in random_flax_state_dict: lowerCAmelCase_ = pt_tuple_key[:-1] + ("embedding",) return renamed_pt_tuple_key, pt_tensor # conv layer lowerCAmelCase_ = pt_tuple_key[:-1] + ("kernel",) if pt_tuple_key[-1] == "weight" and pt_tensor.ndim == 4: lowerCAmelCase_ = pt_tensor.transpose(2 , 3 , 1 , 0 ) return renamed_pt_tuple_key, pt_tensor # linear layer lowerCAmelCase_ = pt_tuple_key[:-1] + ("kernel",) if pt_tuple_key[-1] == "weight": lowerCAmelCase_ = pt_tensor.T return renamed_pt_tuple_key, pt_tensor # old PyTorch layer norm weight lowerCAmelCase_ = pt_tuple_key[:-1] + ("weight",) if pt_tuple_key[-1] == "gamma": return renamed_pt_tuple_key, pt_tensor # old PyTorch layer norm bias lowerCAmelCase_ = pt_tuple_key[:-1] + ("bias",) if pt_tuple_key[-1] == "beta": return renamed_pt_tuple_key, pt_tensor return pt_tuple_key, pt_tensor def A(__a: Dict , __a: Any , __a: List[Any]=42 ): # Step 1: Convert pytorch tensor to numpy lowerCAmelCase_ = {k: v.numpy() for k, v in pt_state_dict.items()} # Step 2: Since the model is stateless, get random Flax params lowerCAmelCase_ = flax_model.init_weights(PRNGKey(__a ) ) lowerCAmelCase_ = flatten_dict(__a ) lowerCAmelCase_ = {} # Need to change some parameters name to match Flax names for pt_key, pt_tensor in pt_state_dict.items(): lowerCAmelCase_ = rename_key(__a ) lowerCAmelCase_ = tuple(renamed_pt_key.split("." ) ) # Correctly rename weight parameters lowerCAmelCase_ , lowerCAmelCase_ = rename_key_and_reshape_tensor(__a , __a , __a ) if flax_key in random_flax_state_dict: if flax_tensor.shape != random_flax_state_dict[flax_key].shape: raise ValueError( F"PyTorch checkpoint seems to be incorrect. Weight {pt_key} was expected to be of shape " F"{random_flax_state_dict[flax_key].shape}, but is {flax_tensor.shape}." ) # also add unexpected weight so that warning is thrown lowerCAmelCase_ = jnp.asarray(__a ) return unflatten_dict(__a )
22
1
import gc import unittest import numpy as np import torch from diffusers import ( AudioDiffusionPipeline, AutoencoderKL, DDIMScheduler, DDPMScheduler, DiffusionPipeline, Mel, UNetaDConditionModel, UNetaDModel, ) from diffusers.utils import slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu enable_full_determinism() class __lowerCamelCase (unittest.TestCase ): def snake_case_ ( self: List[Any] ): '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() @property def snake_case_ ( self: Optional[Any] ): '''simple docstring''' torch.manual_seed(0 ) __UpperCamelCase = UNetaDModel( sample_size=(32, 64),in_channels=1,out_channels=1,layers_per_block=2,block_out_channels=(128, 128),down_block_types=('AttnDownBlock2D', 'DownBlock2D'),up_block_types=('UpBlock2D', 'AttnUpBlock2D'),) return model @property def snake_case_ ( self: List[Any] ): '''simple docstring''' torch.manual_seed(0 ) __UpperCamelCase = UNetaDConditionModel( sample_size=(64, 32),in_channels=1,out_channels=1,layers_per_block=2,block_out_channels=(128, 128),down_block_types=('CrossAttnDownBlock2D', 'DownBlock2D'),up_block_types=('UpBlock2D', 'CrossAttnUpBlock2D'),cross_attention_dim=10,) return model @property def snake_case_ ( self: str ): '''simple docstring''' torch.manual_seed(0 ) __UpperCamelCase = AutoencoderKL( sample_size=(128, 64),in_channels=1,out_channels=1,latent_channels=1,layers_per_block=2,block_out_channels=(128, 128),down_block_types=('DownEncoderBlock2D', 'DownEncoderBlock2D'),up_block_types=('UpDecoderBlock2D', 'UpDecoderBlock2D'),) __UpperCamelCase = UNetaDModel( sample_size=(64, 32),in_channels=1,out_channels=1,layers_per_block=2,block_out_channels=(128, 128),down_block_types=('AttnDownBlock2D', 'DownBlock2D'),up_block_types=('UpBlock2D', 'AttnUpBlock2D'),) return vqvae, unet @slow def snake_case_ ( self: Dict ): '''simple docstring''' __UpperCamelCase = 'cpu' # ensure determinism for the device-dependent torch.Generator __UpperCamelCase = Mel( x_res=self.dummy_unet.config.sample_size[1],y_res=self.dummy_unet.config.sample_size[0],) __UpperCamelCase = DDPMScheduler() __UpperCamelCase = AudioDiffusionPipeline(vqvae=lowerCAmelCase_,unet=self.dummy_unet,mel=lowerCAmelCase_,scheduler=lowerCAmelCase_ ) __UpperCamelCase = pipe.to(lowerCAmelCase_ ) pipe.set_progress_bar_config(disable=lowerCAmelCase_ ) __UpperCamelCase = torch.Generator(device=lowerCAmelCase_ ).manual_seed(42 ) __UpperCamelCase = pipe(generator=lowerCAmelCase_,steps=4 ) __UpperCamelCase = output.audios[0] __UpperCamelCase = output.images[0] __UpperCamelCase = torch.Generator(device=lowerCAmelCase_ ).manual_seed(42 ) __UpperCamelCase = pipe(generator=lowerCAmelCase_,steps=4,return_dict=lowerCAmelCase_ ) __UpperCamelCase = output[0][0] assert audio.shape == (1, (self.dummy_unet.config.sample_size[1] - 1) * mel.hop_length) assert ( image.height == self.dummy_unet.config.sample_size[0] and image.width == self.dummy_unet.config.sample_size[1] ) __UpperCamelCase = np.frombuffer(image.tobytes(),dtype='uint8' )[:10] __UpperCamelCase = np.frombuffer(image_from_tuple.tobytes(),dtype='uint8' )[:10] __UpperCamelCase = np.array([69, 255, 255, 255, 0, 0, 77, 181, 12, 127] ) assert np.abs(image_slice.flatten() - expected_slice ).max() == 0 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() == 0 __UpperCamelCase = Mel( x_res=self.dummy_vqvae_and_unet[0].config.sample_size[1],y_res=self.dummy_vqvae_and_unet[0].config.sample_size[0],) __UpperCamelCase = DDIMScheduler() __UpperCamelCase = self.dummy_vqvae_and_unet __UpperCamelCase = AudioDiffusionPipeline( vqvae=self.dummy_vqvae_and_unet[0],unet=dummy_vqvae_and_unet[1],mel=lowerCAmelCase_,scheduler=lowerCAmelCase_ ) __UpperCamelCase = pipe.to(lowerCAmelCase_ ) pipe.set_progress_bar_config(disable=lowerCAmelCase_ ) np.random.seed(0 ) __UpperCamelCase = np.random.uniform(-1,1,((dummy_vqvae_and_unet[0].config.sample_size[1] - 1) * mel.hop_length,) ) __UpperCamelCase = torch.Generator(device=lowerCAmelCase_ ).manual_seed(42 ) __UpperCamelCase = pipe(raw_audio=lowerCAmelCase_,generator=lowerCAmelCase_,start_step=5,steps=10 ) __UpperCamelCase = output.images[0] assert ( image.height == self.dummy_vqvae_and_unet[0].config.sample_size[0] and image.width == self.dummy_vqvae_and_unet[0].config.sample_size[1] ) __UpperCamelCase = np.frombuffer(image.tobytes(),dtype='uint8' )[:10] __UpperCamelCase = np.array([120, 117, 110, 109, 138, 167, 138, 148, 132, 121] ) assert np.abs(image_slice.flatten() - expected_slice ).max() == 0 __UpperCamelCase = self.dummy_unet_condition __UpperCamelCase = AudioDiffusionPipeline( vqvae=self.dummy_vqvae_and_unet[0],unet=lowerCAmelCase_,mel=lowerCAmelCase_,scheduler=lowerCAmelCase_ ) __UpperCamelCase = pipe.to(lowerCAmelCase_ ) pipe.set_progress_bar_config(disable=lowerCAmelCase_ ) np.random.seed(0 ) __UpperCamelCase = torch.rand((1, 1, 10) ) __UpperCamelCase = pipe(generator=lowerCAmelCase_,encoding=lowerCAmelCase_ ) __UpperCamelCase = output.images[0] __UpperCamelCase = np.frombuffer(image.tobytes(),dtype='uint8' )[:10] __UpperCamelCase = np.array([107, 103, 120, 127, 142, 122, 113, 122, 97, 111] ) assert np.abs(image_slice.flatten() - expected_slice ).max() == 0 @slow @require_torch_gpu class __lowerCamelCase (unittest.TestCase ): def snake_case_ ( self: List[str] ): '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def snake_case_ ( self: List[str] ): '''simple docstring''' __UpperCamelCase = torch_device __UpperCamelCase = DiffusionPipeline.from_pretrained('teticio/audio-diffusion-ddim-256' ) __UpperCamelCase = pipe.to(lowerCAmelCase_ ) pipe.set_progress_bar_config(disable=lowerCAmelCase_ ) __UpperCamelCase = torch.Generator(device=lowerCAmelCase_ ).manual_seed(42 ) __UpperCamelCase = pipe(generator=lowerCAmelCase_ ) __UpperCamelCase = output.audios[0] __UpperCamelCase = output.images[0] assert audio.shape == (1, (pipe.unet.config.sample_size[1] - 1) * pipe.mel.hop_length) assert image.height == pipe.unet.config.sample_size[0] and image.width == pipe.unet.config.sample_size[1] __UpperCamelCase = np.frombuffer(image.tobytes(),dtype='uint8' )[:10] __UpperCamelCase = np.array([151, 167, 154, 144, 122, 134, 121, 105, 70, 26] ) assert np.abs(image_slice.flatten() - expected_slice ).max() == 0
310
'''simple docstring''' import unittest from transformers import is_torch_available from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device if is_torch_available(): from transformers import AutoModelForSeqaSeqLM, AutoTokenizer @require_torch @require_sentencepiece @require_tokenizers class __UpperCAmelCase ( unittest.TestCase ): @slow def lowerCamelCase ( self ): """simple docstring""" _snake_case = AutoModelForSeqaSeqLM.from_pretrained('google/mt5-small' , return_dict=lowerCAmelCase_ ).to(lowerCAmelCase_ ) _snake_case = AutoTokenizer.from_pretrained('google/mt5-small' ) _snake_case = tokenizer('Hello there' , return_tensors='pt' ).input_ids _snake_case = tokenizer('Hi I am' , return_tensors='pt' ).input_ids _snake_case = model(input_ids.to(lowerCAmelCase_ ) , labels=labels.to(lowerCAmelCase_ ) ).loss _snake_case = -(labels.shape[-1] * loss.item()) _snake_case = -84.9127 self.assertTrue(abs(mtf_score - EXPECTED_SCORE ) < 1E-4 )
42
0
# Lint as: python3 import dataclasses import re from dataclasses import dataclass from functools import total_ordering from typing import Optional, Union lowerCamelCase_ : Union[str, Any] = re.compile(r"""^(?P<major>\d+)""" r"""\.(?P<minor>\d+)""" r"""\.(?P<patch>\d+)$""") @total_ordering @dataclass class a__ : A__ : str A__ : Optional[str] = None A__ : Optional[Union[str, int]] = None A__ : Optional[Union[str, int]] = None A__ : Optional[Union[str, int]] = None def __SCREAMING_SNAKE_CASE ( self ) -> List[Any]: __a , __a , __a = _str_to_version_tuple(self.version_str ) def __repr__( self ) -> Dict: return f'''{self.tuple[0]}.{self.tuple[1]}.{self.tuple[2]}''' @property def __SCREAMING_SNAKE_CASE ( self ) -> Union[str, Any]: return self.major, self.minor, self.patch def __SCREAMING_SNAKE_CASE ( self , UpperCAmelCase ) -> List[Any]: if isinstance(UpperCAmelCase , UpperCAmelCase ): return Version(UpperCAmelCase ) elif isinstance(UpperCAmelCase , UpperCAmelCase ): return other raise TypeError(f'''{other} (type {type(UpperCAmelCase )}) cannot be compared to version.''' ) def __eq__( self , UpperCAmelCase ) -> Union[str, Any]: try: __a = self._validate_operand(UpperCAmelCase ) except (TypeError, ValueError): return False else: return self.tuple == other.tuple def __lt__( self , UpperCAmelCase ) -> int: __a = self._validate_operand(UpperCAmelCase ) return self.tuple < other.tuple def __hash__( self ) -> List[Any]: return hash(_version_tuple_to_str(self.tuple ) ) @classmethod def __SCREAMING_SNAKE_CASE ( cls , UpperCAmelCase ) -> Dict: __a = {f.name for f in dataclasses.fields(cls )} return cls(**{k: v for k, v in dic.items() if k in field_names} ) def __SCREAMING_SNAKE_CASE ( self ) -> str: return self.version_str def lowerCAmelCase( __lowerCamelCase ): __a = _VERSION_REG.match(__lowerCamelCase ) 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(__lowerCamelCase ) for v in [res.group('major' ), res.group('minor' ), res.group('patch' )] ) def lowerCAmelCase( __lowerCamelCase ): return ".".join(str(__lowerCamelCase ) for v in version_tuple )
356
import warnings from ..trainer import Trainer from ..utils import logging lowerCamelCase_ : Union[str, Any] = logging.get_logger(__name__) class a__ ( __snake_case ): def __init__( self , UpperCAmelCase=None , **UpperCAmelCase ) -> Any: warnings.warn( '`SageMakerTrainer` is deprecated and will be removed in v5 of Transformers. You can use `Trainer` ' 'instead.' , UpperCAmelCase , ) super().__init__(args=UpperCAmelCase , **UpperCAmelCase )
197
0
from __future__ import annotations _a = { 'A': ['B', 'C', 'E'], 'B': ['A', 'D', 'E'], 'C': ['A', 'F', 'G'], 'D': ['B'], 'E': ['A', 'B', 'D'], 'F': ['C'], 'G': ['C'], } class A_ : def __init__( self : Any , UpperCAmelCase : dict[str, list[str]] , UpperCAmelCase : str ) -> str: __lowerCAmelCase: int = graph # mapping node to its parent in resulting breadth first tree __lowerCAmelCase: str = {} __lowerCAmelCase: Dict = source_vertex def UpperCAmelCase ( self : Optional[Any] ) -> List[str]: __lowerCAmelCase: int = {self.source_vertex} __lowerCAmelCase: Any = None __lowerCAmelCase: List[str] = [self.source_vertex] # first in first out queue while queue: __lowerCAmelCase: str = queue.pop(0 ) for adjacent_vertex in self.graph[vertex]: if adjacent_vertex not in visited: visited.add(UpperCAmelCase ) __lowerCAmelCase: List[str] = vertex queue.append(UpperCAmelCase ) def UpperCAmelCase ( self : str , UpperCAmelCase : str ) -> str: if target_vertex == self.source_vertex: return self.source_vertex __lowerCAmelCase: Optional[Any] = self.parent.get(UpperCAmelCase ) if target_vertex_parent is None: __lowerCAmelCase: Optional[Any] = ( F'''No path from vertex: {self.source_vertex} to vertex: {target_vertex}''' ) raise ValueError(UpperCAmelCase ) return self.shortest_path(UpperCAmelCase ) + F'''->{target_vertex}''' if __name__ == "__main__": _a = Graph(graph, '''G''') g.breath_first_search() print(g.shortest_path('''D''')) print(g.shortest_path('''G''')) print(g.shortest_path('''Foo'''))
322
import functools from typing import Any def __snake_case ( __UpperCamelCase : str ,__UpperCamelCase : list[str] ): """simple docstring""" if not isinstance(__UpperCamelCase ,__UpperCamelCase ) or len(__UpperCamelCase ) == 0: raise ValueError("the string should be not empty string" ) if not isinstance(__UpperCamelCase ,__UpperCamelCase ) or not all( isinstance(__UpperCamelCase ,__UpperCamelCase ) and len(__UpperCamelCase ) > 0 for item in words ): raise ValueError("the words should be a list of non-empty strings" ) # Build trie A_ = {} A_ = "WORD_KEEPER" for word in words: A_ = trie for c in word: if c not in trie_node: A_ = {} A_ = trie_node[c] A_ = True A_ = len(__UpperCamelCase ) # Dynamic programming method @functools.cache def is_breakable(__UpperCamelCase : int ) -> bool: if index == len_string: return True A_ = trie for i in range(__UpperCamelCase ,__UpperCamelCase ): A_ = trie_node.get(string[i] ,__UpperCamelCase ) if trie_node is None: return False if trie_node.get(__UpperCamelCase ,__UpperCamelCase ) and is_breakable(i + 1 ): return True return False return is_breakable(0 ) if __name__ == "__main__": import doctest doctest.testmod()
312
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 : Tuple = logging.get_logger(__name__) def SCREAMING_SNAKE_CASE ( lowercase_ , lowercase_=False ) -> Optional[Any]: """simple docstring""" A__ = [] 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" A__ = [(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 SCREAMING_SNAKE_CASE ( lowercase_ , lowercase_ , lowercase_=False ) -> int: """simple docstring""" for i in range(config.num_hidden_layers ): if base_model: A__ = '''''' else: A__ = '''deit.''' # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) A__ = state_dict.pop(f"""blocks.{i}.attn.qkv.weight""" ) A__ = state_dict.pop(f"""blocks.{i}.attn.qkv.bias""" ) # next, add query, keys and values (in that order) to the state dict A__ = in_proj_weight[ : config.hidden_size, : ] A__ = in_proj_bias[: config.hidden_size] A__ = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] A__ = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] A__ = in_proj_weight[ -config.hidden_size :, : ] A__ = in_proj_bias[-config.hidden_size :] def SCREAMING_SNAKE_CASE ( lowercase_ , lowercase_ , lowercase_ ) -> int: """simple docstring""" A__ = dct.pop(lowercase_ ) A__ = val def SCREAMING_SNAKE_CASE ( ) -> int: """simple docstring""" A__ = '''http://images.cocodataset.org/val2017/000000039769.jpg''' A__ = Image.open(requests.get(lowercase_ , stream=lowercase_ ).raw ) return im @torch.no_grad() def SCREAMING_SNAKE_CASE ( lowercase_ , lowercase_ ) -> Optional[int]: """simple docstring""" A__ = DeiTConfig() # all deit models have fine-tuned heads A__ = False # dataset (fine-tuned on ImageNet 2012), patch_size and image_size A__ = 1_000 A__ = '''huggingface/label-files''' A__ = '''imagenet-1k-id2label.json''' A__ = json.load(open(hf_hub_download(lowercase_ , lowercase_ , repo_type='''dataset''' ) , '''r''' ) ) A__ = {int(lowercase_ ): v for k, v in idalabel.items()} A__ = idalabel A__ = {v: k for k, v in idalabel.items()} A__ = int(deit_name[-6:-4] ) A__ = int(deit_name[-3:] ) # size of the architecture if deit_name[9:].startswith('''tiny''' ): A__ = 192 A__ = 768 A__ = 12 A__ = 3 elif deit_name[9:].startswith('''small''' ): A__ = 384 A__ = 1_536 A__ = 12 A__ = 6 if deit_name[9:].startswith('''base''' ): pass elif deit_name[4:].startswith('''large''' ): A__ = 1_024 A__ = 4_096 A__ = 24 A__ = 16 # load original model from timm A__ = timm.create_model(lowercase_ , pretrained=lowercase_ ) timm_model.eval() # load state_dict of original model, remove and rename some keys A__ = timm_model.state_dict() A__ = create_rename_keys(lowercase_ , lowercase_ ) for src, dest in rename_keys: rename_key(lowercase_ , lowercase_ , lowercase_ ) read_in_q_k_v(lowercase_ , lowercase_ , lowercase_ ) # load HuggingFace model A__ = DeiTForImageClassificationWithTeacher(lowercase_ ).eval() model.load_state_dict(lowercase_ ) # Check outputs on an image, prepared by DeiTImageProcessor A__ = 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 A__ = DeiTImageProcessor(size=lowercase_ , crop_size=config.image_size ) A__ = image_processor(images=prepare_img() , return_tensors='''pt''' ) A__ = encoding['''pixel_values'''] A__ = model(lowercase_ ) A__ = timm_model(lowercase_ ) assert timm_logits.shape == outputs.logits.shape assert torch.allclose(lowercase_ , outputs.logits , atol=1E-3 ) Path(lowercase_ ).mkdir(exist_ok=lowercase_ ) print(f"""Saving model {deit_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 : Dict = 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 : Optional[int] = parser.parse_args() convert_deit_checkpoint(args.deit_name, args.pytorch_dump_folder_path)
231
import argparse from tax import checkpoints from transformers import AutoConfig, FlaxAutoModelForSeqaSeqLM def SCREAMING_SNAKE_CASE ( lowercase_ , lowercase_ , lowercase_ ) -> Tuple: """simple docstring""" A__ = AutoConfig.from_pretrained(lowercase_ ) A__ = FlaxAutoModelForSeqaSeqLM.from_config(config=lowercase_ ) A__ = checkpoints.load_tax_checkpoint(lowercase_ ) A__ = '''wi_0''' in tax_model['''target''']['''encoder''']['''layers_0''']['''mlp'''] if config.model_type == "t5": A__ = '''SelfAttention''' if config.model_type == "longt5" and config.encoder_attention_type == "local": A__ = '''LocalSelfAttention''' elif config.model_type == "longt5" and config.encoder_attention_type == "transient-global": A__ = '''TransientGlobalSelfAttention''' else: raise ValueError( '''Given config is expected to have `model_type=\'t5\'`, or `model_type=\'longt5` with `encoder_attention_type`''' ''' attribute with a value from [\'local\', \'transient-global].''' ) # Encoder for layer_index in range(config.num_layers ): A__ = f"""layers_{str(lowercase_ )}""" # Self-Attention A__ = tax_model['''target''']['''encoder'''][layer_name]['''attention''']['''key''']['''kernel'''] A__ = tax_model['''target''']['''encoder'''][layer_name]['''attention''']['''out''']['''kernel'''] A__ = tax_model['''target''']['''encoder'''][layer_name]['''attention''']['''query''']['''kernel'''] A__ = tax_model['''target''']['''encoder'''][layer_name]['''attention''']['''value''']['''kernel'''] # Global input layer norm if config.model_type == "longt5" and config.encoder_attention_type == "transient-global": A__ = tax_model['''target''']['''encoder'''][layer_name]['''attention''']['''T5LayerNorm_0''']['''scale'''] # Layer Normalization A__ = tax_model['''target''']['''encoder'''][layer_name]['''pre_attention_layer_norm''']['''scale'''] if split_mlp_wi: A__ = tax_model['''target''']['''encoder'''][layer_name]['''mlp''']['''wi_0''']['''kernel'''] A__ = tax_model['''target''']['''encoder'''][layer_name]['''mlp''']['''wi_1''']['''kernel'''] else: A__ = tax_model['''target''']['''encoder'''][layer_name]['''mlp''']['''wi''']['''kernel'''] A__ = tax_model['''target''']['''encoder'''][layer_name]['''mlp''']['''wo''']['''kernel'''] # Layer Normalization A__ = tax_model['''target''']['''encoder'''][layer_name]['''pre_mlp_layer_norm''']['''scale'''] # Assigning A__ = flax_model.params['''encoder''']['''block'''][str(lowercase_ )]['''layer'''] A__ = tax_attention_key A__ = tax_attention_out A__ = tax_attention_query A__ = tax_attention_value A__ = tax_attention_layer_norm # Global input layer norm if config.model_type == "longt5" and config.encoder_attention_type == "transient-global": A__ = tax_global_layer_norm if split_mlp_wi: A__ = tax_mlp_wi_a A__ = tax_mlp_wi_a else: A__ = tax_mlp_wi A__ = tax_mlp_wo A__ = tax_mlp_layer_norm A__ = flax_model_encoder_layer_block # Only for layer 0: A__ = tax_model['''target''']['''encoder''']['''relpos_bias''']['''rel_embedding'''].T A__ = tax_encoder_rel_embedding # Side/global relative position_bias + layer norm if config.model_type == "longt5" and config.encoder_attention_type == "transient-global": A__ = tax_model['''target''']['''encoder''']['''side_relpos_bias''']['''rel_embedding'''].T A__ = tax_encoder_global_rel_embedding # Assigning A__ = tax_model['''target''']['''encoder''']['''encoder_norm''']['''scale'''] A__ = tax_encoder_norm # Decoder for layer_index in range(config.num_layers ): A__ = f"""layers_{str(lowercase_ )}""" # Self-Attention A__ = tax_model['''target''']['''decoder'''][layer_name]['''self_attention''']['''key''']['''kernel'''] A__ = tax_model['''target''']['''decoder'''][layer_name]['''self_attention''']['''out''']['''kernel'''] A__ = tax_model['''target''']['''decoder'''][layer_name]['''self_attention''']['''query''']['''kernel'''] A__ = tax_model['''target''']['''decoder'''][layer_name]['''self_attention''']['''value''']['''kernel'''] # Layer Normalization A__ = tax_model['''target''']['''decoder'''][layer_name]['''pre_self_attention_layer_norm'''][ '''scale''' ] # Encoder-Decoder-Attention A__ = tax_model['''target''']['''decoder'''][layer_name]['''encoder_decoder_attention'''] A__ = tax_enc_dec_attention_module['''key''']['''kernel'''] A__ = tax_enc_dec_attention_module['''out''']['''kernel'''] A__ = tax_enc_dec_attention_module['''query''']['''kernel'''] A__ = tax_enc_dec_attention_module['''value''']['''kernel'''] # Layer Normalization A__ = tax_model['''target''']['''decoder'''][layer_name]['''pre_cross_attention_layer_norm''']['''scale'''] # MLP if split_mlp_wi: A__ = tax_model['''target''']['''decoder'''][layer_name]['''mlp''']['''wi_0''']['''kernel'''] A__ = tax_model['''target''']['''decoder'''][layer_name]['''mlp''']['''wi_1''']['''kernel'''] else: A__ = tax_model['''target''']['''decoder'''][layer_name]['''mlp''']['''wi''']['''kernel'''] A__ = tax_model['''target''']['''decoder'''][layer_name]['''mlp''']['''wo''']['''kernel'''] # Layer Normalization A__ = tax_model['''target''']['''decoder'''][layer_name]['''pre_mlp_layer_norm''']['''scale'''] # Assigning A__ = flax_model.params['''decoder''']['''block'''][str(lowercase_ )]['''layer'''] A__ = tax_attention_key A__ = tax_attention_out A__ = tax_attention_query A__ = tax_attention_value A__ = tax_pre_attention_layer_norm A__ = tax_enc_dec_attention_key A__ = tax_enc_dec_attention_out A__ = tax_enc_dec_attention_query A__ = tax_enc_dec_attention_value A__ = tax_cross_layer_norm if split_mlp_wi: A__ = tax_mlp_wi_a A__ = tax_mlp_wi_a else: A__ = tax_mlp_wi A__ = tax_mlp_wo A__ = txa_mlp_layer_norm A__ = flax_model_decoder_layer_block # Decoder Normalization A__ = tax_model['''target''']['''decoder''']['''decoder_norm''']['''scale'''] A__ = txa_decoder_norm # Only for layer 0: A__ = tax_model['''target''']['''decoder''']['''relpos_bias''']['''rel_embedding'''].T A__ = tax_decoder_rel_embedding # Token Embeddings A__ = tax_model['''target''']['''token_embedder''']['''embedding'''] A__ = txa_token_embeddings # LM Head (only in v1.1 and LongT5 checkpoints) if "logits_dense" in tax_model["target"]["decoder"]: A__ = tax_model['''target''']['''decoder''']['''logits_dense''']['''kernel'''] flax_model.save_pretrained(lowercase_ ) print('''T5X Model was sucessfully converted!''' ) if __name__ == "__main__": _lowerCamelCase : Any = argparse.ArgumentParser() # Required parameters parser.add_argument( """--t5x_checkpoint_path""", default=None, type=str, required=True, help="""Path the T5X checkpoint.""" ) parser.add_argument("""--config_name""", default=None, type=str, required=True, help="""Config name of LongT5/T5 model.""") parser.add_argument( """--flax_dump_folder_path""", default=None, type=str, required=True, help="""Path to the output FLAX model.""" ) _lowerCamelCase : Tuple = parser.parse_args() convert_tax_checkpoint_to_flax(args.tax_checkpoint_path, args.config_name, args.flax_dump_folder_path)
231
1
"""simple docstring""" import random def UpperCAmelCase__ ( _UpperCAmelCase ): """simple docstring""" A_ : int = num - 1 A_ : List[str] = 0 while s % 2 == 0: A_ : Any = s // 2 t += 1 for _ in range(5 ): A_ : Optional[Any] = random.randrange(2 , num - 1 ) A_ : Dict = pow(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) if v != 1: A_ : Dict = 0 while v != (num - 1): if i == t - 1: return False else: A_ : Tuple = i + 1 A_ : List[str] = (v**2) % num return True def UpperCAmelCase__ ( _UpperCAmelCase ): """simple docstring""" if num < 2: return False A_ : str = [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, ] if num in low_primes: return True for prime in low_primes: if (num % prime) == 0: return False return rabin_miller(_UpperCAmelCase ) def UpperCAmelCase__ ( _UpperCAmelCase = 1024 ): """simple docstring""" while True: A_ : Union[str, Any] = random.randrange(2 ** (keysize - 1) , 2 ** (keysize) ) if is_prime_low_num(_UpperCAmelCase ): return num if __name__ == "__main__": lowerCamelCase_ : Any = generate_large_prime() print(('Prime number:', num)) print(('is_prime_low_num:', is_prime_low_num(num)))
286
"""simple docstring""" def UpperCAmelCase__ ( _UpperCAmelCase ): """simple docstring""" A_ : List[str] = hex_num.strip() if not hex_num: raise ValueError('No value was passed to the function' ) A_ : Any = hex_num[0] == '-' if is_negative: A_ : Optional[Any] = hex_num[1:] try: A_ : Tuple = int(_UpperCAmelCase , 16 ) except ValueError: raise ValueError('Invalid value was passed to the function' ) A_ : Union[str, Any] = '' while int_num > 0: A_ : Optional[Any] = str(int_num % 2 ) + bin_str int_num >>= 1 return int(('-' + bin_str) if is_negative else bin_str ) if __name__ == "__main__": import doctest doctest.testmod()
286
1
import os import tempfile import unittest from transformers import is_torch_available from transformers.testing_utils import require_torch if is_torch_available(): import torch from torch import nn from transformers import ( Adafactor, AdamW, get_constant_schedule, get_constant_schedule_with_warmup, get_cosine_schedule_with_warmup, get_cosine_with_hard_restarts_schedule_with_warmup, get_inverse_sqrt_schedule, get_linear_schedule_with_warmup, get_polynomial_decay_schedule_with_warmup, ) def lowerCAmelCase( __lowerCamelCase , __lowerCamelCase=10 ): __a = [] for _ in range(__lowerCamelCase ): lrs.append(scheduler.get_lr()[0] ) scheduler.step() return lrs def lowerCAmelCase( __lowerCamelCase , __lowerCamelCase=10 ): __a = [] for step in range(__lowerCamelCase ): lrs.append(scheduler.get_lr()[0] ) scheduler.step() if step == num_steps // 2: with tempfile.TemporaryDirectory() as tmpdirname: __a = os.path.join(__lowerCamelCase , 'schedule.bin' ) torch.save(scheduler.state_dict() , __lowerCamelCase ) __a = torch.load(__lowerCamelCase ) scheduler.load_state_dict(__lowerCamelCase ) return lrs @require_torch class a__ ( unittest.TestCase ): def __SCREAMING_SNAKE_CASE ( self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) -> Tuple: self.assertEqual(len(UpperCAmelCase ) , len(UpperCAmelCase ) ) for a, b in zip(UpperCAmelCase , UpperCAmelCase ): self.assertAlmostEqual(UpperCAmelCase , UpperCAmelCase , delta=UpperCAmelCase ) def __SCREAMING_SNAKE_CASE ( self ) -> Union[str, Any]: __a = torch.tensor([0.1, -0.2, -0.1] , requires_grad=UpperCAmelCase ) __a = torch.tensor([0.4, 0.2, -0.5] ) __a = nn.MSELoss() # No warmup, constant schedule, no gradient clipping __a = AdamW(params=[w] , lr=2e-1 , weight_decay=0.0 ) for _ in range(1_0_0 ): __a = criterion(UpperCAmelCase , UpperCAmelCase ) loss.backward() optimizer.step() w.grad.detach_() # No zero_grad() function on simple tensors. we do it ourselves. w.grad.zero_() self.assertListAlmostEqual(w.tolist() , [0.4, 0.2, -0.5] , tol=1e-2 ) def __SCREAMING_SNAKE_CASE ( self ) -> Optional[int]: __a = torch.tensor([0.1, -0.2, -0.1] , requires_grad=UpperCAmelCase ) __a = torch.tensor([0.4, 0.2, -0.5] ) __a = nn.MSELoss() # No warmup, constant schedule, no gradient clipping __a = Adafactor( params=[w] , lr=1e-2 , eps=(1e-30, 1e-3) , clip_threshold=1.0 , decay_rate=-0.8 , betaa=UpperCAmelCase , weight_decay=0.0 , relative_step=UpperCAmelCase , scale_parameter=UpperCAmelCase , warmup_init=UpperCAmelCase , ) for _ in range(1_0_0_0 ): __a = criterion(UpperCAmelCase , UpperCAmelCase ) loss.backward() optimizer.step() w.grad.detach_() # No zero_grad() function on simple tensors. we do it ourselves. w.grad.zero_() self.assertListAlmostEqual(w.tolist() , [0.4, 0.2, -0.5] , tol=1e-2 ) @require_torch class a__ ( unittest.TestCase ): A__ : Union[str, Any] = nn.Linear(50 , 50 ) if is_torch_available() else None A__ : Tuple = AdamW(m.parameters() , lr=10.0 ) if is_torch_available() else None A__ : int = 10 def __SCREAMING_SNAKE_CASE ( self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase=None ) -> Optional[int]: self.assertEqual(len(UpperCAmelCase ) , len(UpperCAmelCase ) ) for a, b in zip(UpperCAmelCase , UpperCAmelCase ): self.assertAlmostEqual(UpperCAmelCase , UpperCAmelCase , delta=UpperCAmelCase , msg=UpperCAmelCase ) def __SCREAMING_SNAKE_CASE ( self ) -> Tuple: __a = {'num_warmup_steps': 2, 'num_training_steps': 1_0} # schedulers doct format # function: (sched_args_dict, expected_learning_rates) __a = { get_constant_schedule: ({}, [10.0] * self.num_steps), get_constant_schedule_with_warmup: ( {'num_warmup_steps': 4}, [0.0, 2.5, 5.0, 7.5, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0], ), get_linear_schedule_with_warmup: ( {**common_kwargs}, [0.0, 5.0, 10.0, 8.75, 7.5, 6.25, 5.0, 3.75, 2.5, 1.25], ), get_cosine_schedule_with_warmup: ( {**common_kwargs}, [0.0, 5.0, 10.0, 9.61, 8.53, 6.91, 5.0, 3.08, 1.46, 0.38], ), get_cosine_with_hard_restarts_schedule_with_warmup: ( {**common_kwargs, 'num_cycles': 2}, [0.0, 5.0, 10.0, 8.53, 5.0, 1.46, 10.0, 8.53, 5.0, 1.46], ), get_polynomial_decay_schedule_with_warmup: ( {**common_kwargs, 'power': 2.0, 'lr_end': 1e-7}, [0.0, 5.0, 10.0, 7.656, 5.625, 3.906, 2.5, 1.406, 0.625, 0.156], ), get_inverse_sqrt_schedule: ( {'num_warmup_steps': 2}, [0.0, 5.0, 10.0, 8.165, 7.071, 6.325, 5.774, 5.345, 5.0, 4.714], ), } for scheduler_func, data in scheds.items(): __a , __a = data __a = scheduler_func(self.optimizer , **UpperCAmelCase ) self.assertEqual(len([scheduler.get_lr()[0]] ) , 1 ) __a = unwrap_schedule(UpperCAmelCase , self.num_steps ) self.assertListAlmostEqual( UpperCAmelCase , UpperCAmelCase , tol=1e-2 , msg=f'''failed for {scheduler_func} in normal scheduler''' , ) __a = scheduler_func(self.optimizer , **UpperCAmelCase ) if scheduler_func.__name__ != "get_constant_schedule": LambdaScheduleWrapper.wrap_scheduler(UpperCAmelCase ) # wrap to test picklability of the schedule __a = unwrap_and_save_reload_schedule(UpperCAmelCase , self.num_steps ) self.assertListEqual(UpperCAmelCase , UpperCAmelCase , msg=f'''failed for {scheduler_func} in save and reload''' ) class a__ : def __init__( self , UpperCAmelCase ) -> Optional[int]: __a = fn def __call__( self , *UpperCAmelCase , **UpperCAmelCase ) -> Tuple: return self.fn(*UpperCAmelCase , **UpperCAmelCase ) @classmethod def __SCREAMING_SNAKE_CASE ( self , UpperCAmelCase ) -> str: __a = list(map(self , scheduler.lr_lambdas ) )
365
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available lowerCamelCase_ : Dict = {"""configuration_swin""": ["""SWIN_PRETRAINED_CONFIG_ARCHIVE_MAP""", """SwinConfig""", """SwinOnnxConfig"""]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase_ : int = [ """SWIN_PRETRAINED_MODEL_ARCHIVE_LIST""", """SwinForImageClassification""", """SwinForMaskedImageModeling""", """SwinModel""", """SwinPreTrainedModel""", """SwinBackbone""", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase_ : Any = [ """TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST""", """TFSwinForImageClassification""", """TFSwinForMaskedImageModeling""", """TFSwinModel""", """TFSwinPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_swin import SWIN_PRETRAINED_CONFIG_ARCHIVE_MAP, SwinConfig, SwinOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_swin import ( SWIN_PRETRAINED_MODEL_ARCHIVE_LIST, SwinBackbone, SwinForImageClassification, SwinForMaskedImageModeling, SwinModel, SwinPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_swin import ( TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST, TFSwinForImageClassification, TFSwinForMaskedImageModeling, TFSwinModel, TFSwinPreTrainedModel, ) else: import sys lowerCamelCase_ : List[Any] = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
197
0
'''simple docstring''' from __future__ import annotations lowerCAmelCase :Any = '''#''' class _lowerCamelCase : '''simple docstring''' def __init__( self : int ) -> None: __magic_name__ : dict = {} def __lowerCAmelCase ( self : Any , _A : str ) -> None: __magic_name__ : Tuple = self._trie for char in text: if char not in trie: __magic_name__ : List[Any] = {} __magic_name__ : Any = trie[char] __magic_name__ : Tuple = True def __lowerCAmelCase ( self : Any , _A : str ) -> tuple | list: __magic_name__ : str = self._trie for char in prefix: if char in trie: __magic_name__ : Optional[int] = trie[char] else: return [] return self._elements(_A ) def __lowerCAmelCase ( self : Any , _A : dict ) -> tuple: __magic_name__ : Optional[int] = [] for c, v in d.items(): __magic_name__ : Any = [' '] if c == END else [(c + s) for s in self._elements(_A )] result.extend(_A ) return tuple(_A ) lowerCAmelCase :Dict = Trie() lowerCAmelCase :Optional[int] = ('''depart''', '''detergent''', '''daring''', '''dog''', '''deer''', '''deal''') for word in words: trie.insert_word(word) def lowerCamelCase ( lowerCAmelCase : str ): """simple docstring""" __magic_name__ : List[str] = trie.find_word(lowerCAmelCase ) return tuple(string + word for word in suffixes ) def lowerCamelCase ( ): """simple docstring""" print(autocomplete_using_trie('de' ) ) if __name__ == "__main__": import doctest doctest.testmod() main()
331
'''simple docstring''' from __future__ import annotations import os import tempfile import unittest from transformers import ConvBertConfig, is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TFConvBertForMaskedLM, TFConvBertForMultipleChoice, TFConvBertForQuestionAnswering, TFConvBertForSequenceClassification, TFConvBertForTokenClassification, TFConvBertModel, ) class _lowerCamelCase : '''simple docstring''' def __init__( self : Optional[Any] , _A : Optional[int] , _A : Union[str, Any]=13 , _A : Optional[int]=7 , _A : int=True , _A : Union[str, Any]=True , _A : Tuple=True , _A : Dict=True , _A : int=99 , _A : str=32 , _A : List[Any]=2 , _A : Any=4 , _A : List[str]=37 , _A : List[str]="gelu" , _A : Any=0.1 , _A : List[str]=0.1 , _A : Optional[Any]=512 , _A : str=16 , _A : Union[str, Any]=2 , _A : List[Any]=0.02 , _A : Any=3 , _A : str=4 , _A : int=None , ) -> int: __magic_name__ : str = parent __magic_name__ : List[Any] = 13 __magic_name__ : Union[str, Any] = 7 __magic_name__ : Tuple = True __magic_name__ : Dict = True __magic_name__ : Union[str, Any] = True __magic_name__ : Tuple = True __magic_name__ : int = 99 __magic_name__ : List[str] = 384 __magic_name__ : Optional[int] = 2 __magic_name__ : List[Any] = 4 __magic_name__ : int = 37 __magic_name__ : Union[str, Any] = 'gelu' __magic_name__ : Optional[int] = 0.1 __magic_name__ : str = 0.1 __magic_name__ : Optional[Any] = 512 __magic_name__ : Any = 16 __magic_name__ : Union[str, Any] = 2 __magic_name__ : Any = 0.02 __magic_name__ : List[str] = 3 __magic_name__ : Tuple = 4 __magic_name__ : List[Any] = 128 __magic_name__ : Optional[Any] = 2 __magic_name__ : List[str] = 9 __magic_name__ : str = 1 __magic_name__ : List[str] = None def __lowerCAmelCase ( self : List[str] ) -> List[str]: __magic_name__ : Tuple = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __magic_name__ : Optional[Any] = None if self.use_input_mask: __magic_name__ : str = random_attention_mask([self.batch_size, self.seq_length] ) __magic_name__ : List[str] = None if self.use_token_type_ids: __magic_name__ : int = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) __magic_name__ : Tuple = None __magic_name__ : Union[str, Any] = None __magic_name__ : int = None if self.use_labels: __magic_name__ : int = ids_tensor([self.batch_size] , self.type_sequence_label_size ) __magic_name__ : Any = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) __magic_name__ : int = ids_tensor([self.batch_size] , self.num_choices ) __magic_name__ : Optional[Any] = ConvBertConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , return_dict=_A , ) return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def __lowerCAmelCase ( self : int , _A : int , _A : str , _A : Union[str, Any] , _A : List[str] , _A : Tuple , _A : int , _A : Union[str, Any] ) -> Any: __magic_name__ : Dict = TFConvBertModel(config=_A ) __magic_name__ : int = {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids} __magic_name__ : Any = [input_ids, input_mask] __magic_name__ : Tuple = model(_A ) __magic_name__ : List[Any] = model(_A ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __lowerCAmelCase ( self : int , _A : str , _A : Dict , _A : Dict , _A : Dict , _A : Any , _A : Optional[int] , _A : int ) -> Optional[Any]: __magic_name__ : Dict = TFConvBertForMaskedLM(config=_A ) __magic_name__ : Union[str, Any] = { 'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids, } __magic_name__ : Dict = model(_A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def __lowerCAmelCase ( self : Optional[int] , _A : str , _A : Union[str, Any] , _A : Tuple , _A : Dict , _A : Dict , _A : Union[str, Any] , _A : Dict ) -> Tuple: __magic_name__ : Any = self.num_labels __magic_name__ : str = TFConvBertForSequenceClassification(config=_A ) __magic_name__ : List[Any] = { 'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids, } __magic_name__ : Any = model(_A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __lowerCAmelCase ( self : int , _A : Dict , _A : Tuple , _A : str , _A : str , _A : int , _A : List[Any] , _A : Optional[int] ) -> Union[str, Any]: __magic_name__ : Optional[Any] = self.num_choices __magic_name__ : Optional[int] = TFConvBertForMultipleChoice(config=_A ) __magic_name__ : Union[str, Any] = tf.tile(tf.expand_dims(_A , 1 ) , (1, self.num_choices, 1) ) __magic_name__ : str = tf.tile(tf.expand_dims(_A , 1 ) , (1, self.num_choices, 1) ) __magic_name__ : Tuple = tf.tile(tf.expand_dims(_A , 1 ) , (1, self.num_choices, 1) ) __magic_name__ : Optional[int] = { 'input_ids': multiple_choice_inputs_ids, 'attention_mask': multiple_choice_input_mask, 'token_type_ids': multiple_choice_token_type_ids, } __magic_name__ : Union[str, Any] = model(_A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def __lowerCAmelCase ( self : List[Any] , _A : int , _A : List[str] , _A : int , _A : Tuple , _A : List[str] , _A : Any , _A : Optional[int] ) -> List[Any]: __magic_name__ : List[Any] = self.num_labels __magic_name__ : Union[str, Any] = TFConvBertForTokenClassification(config=_A ) __magic_name__ : Dict = { 'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids, } __magic_name__ : Any = model(_A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def __lowerCAmelCase ( self : Optional[int] , _A : List[Any] , _A : Tuple , _A : List[Any] , _A : Optional[int] , _A : Tuple , _A : str , _A : List[str] ) -> int: __magic_name__ : Dict = TFConvBertForQuestionAnswering(config=_A ) __magic_name__ : int = { 'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids, } __magic_name__ : Union[str, Any] = model(_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 __lowerCAmelCase ( self : Optional[Any] ) -> Optional[int]: __magic_name__ : List[str] = self.prepare_config_and_inputs() ( ( __magic_name__ ) , ( __magic_name__ ) , ( __magic_name__ ) , ( __magic_name__ ) , ( __magic_name__ ) , ( __magic_name__ ) , ( __magic_name__ ) , ) : str = config_and_inputs __magic_name__ : Dict = {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask} return config, inputs_dict @require_tf class _lowerCamelCase ( lowercase__ , lowercase__ , unittest.TestCase ): '''simple docstring''' A_ : Optional[int] = ( ( TFConvBertModel, TFConvBertForMaskedLM, TFConvBertForQuestionAnswering, TFConvBertForSequenceClassification, TFConvBertForTokenClassification, TFConvBertForMultipleChoice, ) if is_tf_available() else () ) A_ : List[str] = ( { """feature-extraction""": TFConvBertModel, """fill-mask""": TFConvBertForMaskedLM, """question-answering""": TFConvBertForQuestionAnswering, """text-classification""": TFConvBertForSequenceClassification, """token-classification""": TFConvBertForTokenClassification, """zero-shot""": TFConvBertForSequenceClassification, } if is_tf_available() else {} ) A_ : Tuple = False A_ : Any = False A_ : List[Any] = False def __lowerCAmelCase ( self : List[Any] ) -> int: __magic_name__ : Optional[Any] = TFConvBertModelTester(self ) __magic_name__ : List[Any] = ConfigTester(self , config_class=_A , hidden_size=37 ) def __lowerCAmelCase ( self : str ) -> Dict: self.config_tester.run_common_tests() def __lowerCAmelCase ( self : Optional[Any] ) -> Union[str, Any]: __magic_name__ : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_A ) def __lowerCAmelCase ( self : Optional[int] ) -> int: __magic_name__ : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*_A ) def __lowerCAmelCase ( self : List[Any] ) -> Dict: __magic_name__ : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_multiple_choice(*_A ) def __lowerCAmelCase ( self : List[str] ) -> Optional[int]: __magic_name__ : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*_A ) def __lowerCAmelCase ( self : Union[str, Any] ) -> Union[str, Any]: __magic_name__ : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*_A ) def __lowerCAmelCase ( self : int ) -> Any: __magic_name__ : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*_A ) @slow def __lowerCAmelCase ( self : Dict ) -> List[str]: __magic_name__ , __magic_name__ : int = self.model_tester.prepare_config_and_inputs_for_common() __magic_name__ : Optional[int] = True __magic_name__ : Any = True if hasattr(_A , 'use_cache' ): __magic_name__ : List[Any] = True __magic_name__ : str = getattr(self.model_tester , 'encoder_seq_length' , self.model_tester.seq_length ) __magic_name__ : Optional[Any] = getattr(self.model_tester , 'key_length' , _A ) for model_class in self.all_model_classes: __magic_name__ : List[str] = self._prepare_for_class(_A , _A ) __magic_name__ : Optional[int] = model_class(_A ) __magic_name__ : Tuple = len(model(_A ) ) with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(_A , saved_model=_A ) __magic_name__ : Union[str, Any] = os.path.join(_A , 'saved_model' , '1' ) __magic_name__ : Optional[int] = tf.keras.models.load_model(_A ) __magic_name__ : Optional[Any] = model(_A ) if self.is_encoder_decoder: __magic_name__ : Optional[int] = outputs['encoder_hidden_states'] __magic_name__ : Tuple = outputs['encoder_attentions'] else: __magic_name__ : Union[str, Any] = outputs['hidden_states'] __magic_name__ : Optional[Any] = outputs['attentions'] self.assertEqual(len(_A ) , _A ) __magic_name__ : Optional[Any] = getattr( self.model_tester , 'expected_num_hidden_layers' , self.model_tester.num_hidden_layers + 1 ) self.assertEqual(len(_A ) , _A ) self.assertListEqual( list(output_hidden_states[0].shape[-2:] ) , [self.model_tester.seq_length, self.model_tester.hidden_size] , ) self.assertEqual(len(_A ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(output_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length] , ) @slow def __lowerCAmelCase ( self : Union[str, Any] ) -> Any: __magic_name__ : Optional[Any] = TFConvBertModel.from_pretrained('YituTech/conv-bert-base' ) self.assertIsNotNone(_A ) def __lowerCAmelCase ( self : List[str] ) -> Any: __magic_name__ , __magic_name__ : Tuple = self.model_tester.prepare_config_and_inputs_for_common() __magic_name__ : str = True __magic_name__ : Optional[int] = getattr(self.model_tester , 'decoder_seq_length' , self.model_tester.seq_length ) __magic_name__ : List[Any] = getattr(self.model_tester , 'encoder_seq_length' , self.model_tester.seq_length ) __magic_name__ : List[Any] = getattr(self.model_tester , 'key_length' , _A ) __magic_name__ : Optional[int] = getattr(self.model_tester , 'key_length' , _A ) def check_decoder_attentions_output(_A : List[Any] ): __magic_name__ : Tuple = len(_A ) self.assertEqual(out_len % 2 , 0 ) __magic_name__ : Any = outputs.decoder_attentions self.assertEqual(len(_A ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(decoder_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads / 2, decoder_seq_length, decoder_key_length] , ) def check_encoder_attentions_output(_A : int ): __magic_name__ : Dict = [ t.numpy() for t in (outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions) ] self.assertEqual(len(_A ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length] , ) for model_class in self.all_model_classes: __magic_name__ : Union[str, Any] = True __magic_name__ : Tuple = False __magic_name__ : List[str] = model_class(_A ) __magic_name__ : Any = model(self._prepare_for_class(_A , _A ) ) __magic_name__ : Tuple = len(_A ) self.assertEqual(config.output_hidden_states , _A ) check_encoder_attentions_output(_A ) if self.is_encoder_decoder: __magic_name__ : Any = model_class(_A ) __magic_name__ : Any = model(self._prepare_for_class(_A , _A ) ) self.assertEqual(config.output_hidden_states , _A ) check_decoder_attentions_output(_A ) # Check that output attentions can also be changed via the config del inputs_dict["output_attentions"] __magic_name__ : Optional[int] = True __magic_name__ : Optional[int] = model_class(_A ) __magic_name__ : Optional[int] = model(self._prepare_for_class(_A , _A ) ) self.assertEqual(config.output_hidden_states , _A ) check_encoder_attentions_output(_A ) # Check attention is always last and order is fine __magic_name__ : str = True __magic_name__ : str = True __magic_name__ : Optional[int] = model_class(_A ) __magic_name__ : str = model(self._prepare_for_class(_A , _A ) ) self.assertEqual(out_len + (2 if self.is_encoder_decoder else 1) , len(_A ) ) self.assertEqual(model.config.output_hidden_states , _A ) check_encoder_attentions_output(_A ) @require_tf class _lowerCamelCase ( unittest.TestCase ): '''simple docstring''' @slow def __lowerCAmelCase ( self : int ) -> int: __magic_name__ : List[Any] = TFConvBertModel.from_pretrained('YituTech/conv-bert-base' ) __magic_name__ : Union[str, Any] = tf.constant([[0, 1, 2, 3, 4, 5]] ) __magic_name__ : Tuple = model(_A )[0] __magic_name__ : str = [1, 6, 768] self.assertEqual(output.shape , _A ) __magic_name__ : Tuple = tf.constant( [ [ [-0.0347_5493, -0.468_6034, -0.3063_8832], [0.2263_7248, -0.2698_8646, -0.742_3424], [0.1032_4868, -0.4501_3508, -0.5828_0784], ] ] ) tf.debugging.assert_near(output[:, :3, :3] , _A , atol=1E-4 )
331
1
'''simple docstring''' from __future__ import annotations from typing import Any class _lowerCAmelCase : """simple docstring""" def __init__( self , _lowerCamelCase ) -> None: A_ : int = num_of_nodes A_ : list[list[int]] = [] A_ : dict[int, int] = {} def UpperCAmelCase_ ( self , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) -> None: self.m_edges.append([u_node, v_node, weight] ) def UpperCAmelCase_ ( self , _lowerCamelCase ) -> int: if self.m_component[u_node] == u_node: return u_node return self.find_component(self.m_component[u_node] ) def UpperCAmelCase_ ( self , _lowerCamelCase ) -> None: if self.m_component[u_node] != u_node: for k in self.m_component: A_ : Any = self.find_component(_lowerCamelCase ) def UpperCAmelCase_ ( self , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) -> None: if component_size[u_node] <= component_size[v_node]: A_ : int = v_node component_size[v_node] += component_size[u_node] self.set_component(_lowerCamelCase ) elif component_size[u_node] >= component_size[v_node]: A_ : Optional[int] = self.find_component(_lowerCamelCase ) component_size[u_node] += component_size[v_node] self.set_component(_lowerCamelCase ) def UpperCAmelCase_ ( self ) -> None: A_ : Optional[Any] = [] A_ : str = 0 A_ : list[Any] = [-1] * self.m_num_of_nodes # A list of components (initialized to all of the nodes) for node in range(self.m_num_of_nodes ): self.m_component.update({node: node} ) component_size.append(1 ) A_ : int = self.m_num_of_nodes while num_of_components > 1: for edge in self.m_edges: A_ , A_ , A_ : List[str] = edge A_ : Dict = self.m_component[u] A_ : Dict = self.m_component[v] if u_component != v_component: for component in (u_component, v_component): if ( minimum_weight_edge[component] == -1 or minimum_weight_edge[component][2] > w ): A_ : Dict = [u, v, w] for edge in minimum_weight_edge: if isinstance(_lowerCamelCase , _lowerCamelCase ): A_ , A_ , A_ : List[Any] = edge A_ : Optional[int] = self.m_component[u] A_ : Optional[Any] = self.m_component[v] if u_component != v_component: mst_weight += w self.union(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) print(F"Added edge [{u} - {v}]\nAdded weight: {w}\n" ) num_of_components -= 1 A_ : Optional[Any] = [-1] * self.m_num_of_nodes print(F"The total weight of the minimal spanning tree is: {mst_weight}" ) def UpperCAmelCase ( ) -> None: """simple docstring""" if __name__ == "__main__": import doctest doctest.testmod()
164
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_torch_available UpperCamelCase__ : Any = {'configuration_speech_encoder_decoder': ['SpeechEncoderDecoderConfig']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : Union[str, Any] = ['SpeechEncoderDecoderModel'] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ : int = ['FlaxSpeechEncoderDecoderModel'] if TYPE_CHECKING: from .configuration_speech_encoder_decoder import SpeechEncoderDecoderConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_speech_encoder_decoder import SpeechEncoderDecoderModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_speech_encoder_decoder import FlaxSpeechEncoderDecoderModel else: import sys UpperCamelCase__ : Optional[Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
164
1
import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import LevitImageProcessor class lowercase ( unittest.TestCase ): def __init__( self , _a , _a=7 , _a=3 , _a=18 , _a=30 , _a=400 , _a=True , _a=None , _a=True , _a=None , _a=True , _a=[0.5, 0.5, 0.5] , _a=[0.5, 0.5, 0.5] , ) -> Optional[int]: _A : Optional[int] = size if size is not None else {"""shortest_edge""": 18} _A : Optional[Any] = crop_size if crop_size is not None else {"""height""": 18, """width""": 18} _A : Tuple = parent _A : int = batch_size _A : int = num_channels _A : int = image_size _A : Optional[Any] = min_resolution _A : Any = max_resolution _A : Any = do_resize _A : Any = size _A : Optional[Any] = do_center_crop _A : str = crop_size _A : Union[str, Any] = do_normalize _A : Any = image_mean _A : Dict = image_std def a__ ( self ) -> List[Any]: return { "image_mean": self.image_mean, "image_std": self.image_std, "do_normalize": self.do_normalize, "do_resize": self.do_resize, "do_center_crop": self.do_center_crop, "size": self.size, "crop_size": self.crop_size, } @require_torch @require_vision class lowercase ( UpperCamelCase__,unittest.TestCase ): _a = LevitImageProcessor if is_vision_available() else None def a__ ( self ) -> List[Any]: _A : Optional[Any] = LevitImageProcessingTester(self ) @property def a__ ( self ) -> Optional[Any]: return self.image_processor_tester.prepare_image_processor_dict() def a__ ( self ) -> Tuple: _A : Dict = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_a , """image_mean""" ) ) self.assertTrue(hasattr(_a , """image_std""" ) ) self.assertTrue(hasattr(_a , """do_normalize""" ) ) self.assertTrue(hasattr(_a , """do_resize""" ) ) self.assertTrue(hasattr(_a , """do_center_crop""" ) ) self.assertTrue(hasattr(_a , """size""" ) ) def a__ ( self ) -> Optional[Any]: _A : Dict = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"""shortest_edge""": 18} ) self.assertEqual(image_processor.crop_size , {"""height""": 18, """width""": 18} ) _A : Dict = self.image_processing_class.from_dict(self.image_processor_dict , size=42 , crop_size=84 ) self.assertEqual(image_processor.size , {"""shortest_edge""": 42} ) self.assertEqual(image_processor.crop_size , {"""height""": 84, """width""": 84} ) def a__ ( self ) -> Optional[int]: pass def a__ ( self ) -> Tuple: # Initialize image_processing _A : int = self.image_processing_class(**self.image_processor_dict ) # create random PIL images _A : Any = prepare_image_inputs(self.image_processor_tester , equal_resolution=_a ) for image in image_inputs: self.assertIsInstance(_a , Image.Image ) # Test not batched input _A : Dict = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , ) # Test batched _A : Union[str, Any] = image_processing(_a , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , ) def a__ ( self ) -> str: # Initialize image_processing _A : Any = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors _A : List[Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=_a , numpify=_a ) for image in image_inputs: self.assertIsInstance(_a , np.ndarray ) # Test not batched input _A : Any = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , ) # Test batched _A : Tuple = image_processing(_a , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , ) def a__ ( self ) -> Optional[int]: # Initialize image_processing _A : Tuple = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors _A : List[str] = prepare_image_inputs(self.image_processor_tester , equal_resolution=_a , torchify=_a ) for image in image_inputs: self.assertIsInstance(_a , torch.Tensor ) # Test not batched input _A : Tuple = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , ) # Test batched _A : Any = image_processing(_a , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , )
26
import importlib import json import os from collections import OrderedDict from typing import Dict, Optional, Union # Build the list of all feature extractors from ...configuration_utils import PretrainedConfig from ...dynamic_module_utils import get_class_from_dynamic_module, resolve_trust_remote_code from ...feature_extraction_utils import FeatureExtractionMixin from ...utils import CONFIG_NAME, FEATURE_EXTRACTOR_NAME, get_file_from_repo, logging from .auto_factory import _LazyAutoMapping from .configuration_auto import ( CONFIG_MAPPING_NAMES, AutoConfig, model_type_to_module_name, replace_list_option_in_docstrings, ) _snake_case = logging.get_logger(__name__) _snake_case = OrderedDict( [ ("audio-spectrogram-transformer", "ASTFeatureExtractor"), ("beit", "BeitFeatureExtractor"), ("chinese_clip", "ChineseCLIPFeatureExtractor"), ("clap", "ClapFeatureExtractor"), ("clip", "CLIPFeatureExtractor"), ("clipseg", "ViTFeatureExtractor"), ("conditional_detr", "ConditionalDetrFeatureExtractor"), ("convnext", "ConvNextFeatureExtractor"), ("cvt", "ConvNextFeatureExtractor"), ("data2vec-audio", "Wav2Vec2FeatureExtractor"), ("data2vec-vision", "BeitFeatureExtractor"), ("deformable_detr", "DeformableDetrFeatureExtractor"), ("deit", "DeiTFeatureExtractor"), ("detr", "DetrFeatureExtractor"), ("dinat", "ViTFeatureExtractor"), ("donut-swin", "DonutFeatureExtractor"), ("dpt", "DPTFeatureExtractor"), ("encodec", "EncodecFeatureExtractor"), ("flava", "FlavaFeatureExtractor"), ("glpn", "GLPNFeatureExtractor"), ("groupvit", "CLIPFeatureExtractor"), ("hubert", "Wav2Vec2FeatureExtractor"), ("imagegpt", "ImageGPTFeatureExtractor"), ("layoutlmv2", "LayoutLMv2FeatureExtractor"), ("layoutlmv3", "LayoutLMv3FeatureExtractor"), ("levit", "LevitFeatureExtractor"), ("maskformer", "MaskFormerFeatureExtractor"), ("mctct", "MCTCTFeatureExtractor"), ("mobilenet_v1", "MobileNetV1FeatureExtractor"), ("mobilenet_v2", "MobileNetV2FeatureExtractor"), ("mobilevit", "MobileViTFeatureExtractor"), ("nat", "ViTFeatureExtractor"), ("owlvit", "OwlViTFeatureExtractor"), ("perceiver", "PerceiverFeatureExtractor"), ("poolformer", "PoolFormerFeatureExtractor"), ("regnet", "ConvNextFeatureExtractor"), ("resnet", "ConvNextFeatureExtractor"), ("segformer", "SegformerFeatureExtractor"), ("sew", "Wav2Vec2FeatureExtractor"), ("sew-d", "Wav2Vec2FeatureExtractor"), ("speech_to_text", "Speech2TextFeatureExtractor"), ("speecht5", "SpeechT5FeatureExtractor"), ("swiftformer", "ViTFeatureExtractor"), ("swin", "ViTFeatureExtractor"), ("swinv2", "ViTFeatureExtractor"), ("table-transformer", "DetrFeatureExtractor"), ("timesformer", "VideoMAEFeatureExtractor"), ("tvlt", "TvltFeatureExtractor"), ("unispeech", "Wav2Vec2FeatureExtractor"), ("unispeech-sat", "Wav2Vec2FeatureExtractor"), ("van", "ConvNextFeatureExtractor"), ("videomae", "VideoMAEFeatureExtractor"), ("vilt", "ViltFeatureExtractor"), ("vit", "ViTFeatureExtractor"), ("vit_mae", "ViTFeatureExtractor"), ("vit_msn", "ViTFeatureExtractor"), ("wav2vec2", "Wav2Vec2FeatureExtractor"), ("wav2vec2-conformer", "Wav2Vec2FeatureExtractor"), ("wavlm", "Wav2Vec2FeatureExtractor"), ("whisper", "WhisperFeatureExtractor"), ("xclip", "CLIPFeatureExtractor"), ("yolos", "YolosFeatureExtractor"), ] ) _snake_case = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FEATURE_EXTRACTOR_MAPPING_NAMES) def lowerCAmelCase_ ( snake_case_ ): for module_name, extractors in FEATURE_EXTRACTOR_MAPPING_NAMES.items(): if class_name in extractors: _A : List[str] = model_type_to_module_name(snake_case_ ) _A : List[Any] = importlib.import_module(f'''.{module_name}''',"""transformers.models""" ) try: return getattr(snake_case_,snake_case_ ) except AttributeError: continue for _, extractor in FEATURE_EXTRACTOR_MAPPING._extra_content.items(): if getattr(snake_case_,"""__name__""",snake_case_ ) == class_name: return extractor # We did not fine the class, but maybe it's because a dep is missing. In that case, the class will be in the main # init and we return the proper dummy to get an appropriate error message. _A : List[Any] = importlib.import_module("""transformers""" ) if hasattr(snake_case_,snake_case_ ): return getattr(snake_case_,snake_case_ ) return None def lowerCAmelCase_ ( snake_case_,snake_case_ = None,snake_case_ = False,snake_case_ = False,snake_case_ = None,snake_case_ = None,snake_case_ = None,snake_case_ = False,**snake_case_,): _A : Optional[int] = get_file_from_repo( snake_case_,snake_case_,cache_dir=snake_case_,force_download=snake_case_,resume_download=snake_case_,proxies=snake_case_,use_auth_token=snake_case_,revision=snake_case_,local_files_only=snake_case_,) if resolved_config_file is None: logger.info( """Could not locate the feature extractor configuration file, will try to use the model config instead.""" ) return {} with open(snake_case_,encoding="""utf-8""" ) as reader: return json.load(snake_case_ ) class lowercase : def __init__( self ) -> List[Any]: raise EnvironmentError( """AutoFeatureExtractor is designed to be instantiated """ """using the `AutoFeatureExtractor.from_pretrained(pretrained_model_name_or_path)` method.""" ) @classmethod @replace_list_option_in_docstrings(_a ) def a__ ( cls , _a , **_a ) -> Any: _A : Tuple = kwargs.pop("""config""" , _a ) _A : Tuple = kwargs.pop("""trust_remote_code""" , _a ) _A : List[Any] = True _A , _A : Tuple = FeatureExtractionMixin.get_feature_extractor_dict(_a , **_a ) _A : Tuple = config_dict.get("""feature_extractor_type""" , _a ) _A : int = None if "AutoFeatureExtractor" in config_dict.get("""auto_map""" , {} ): _A : Optional[int] = config_dict["""auto_map"""]["""AutoFeatureExtractor"""] # If we don't find the feature extractor class in the feature extractor config, let's try the model config. if feature_extractor_class is None and feature_extractor_auto_map is None: if not isinstance(_a , _a ): _A : int = AutoConfig.from_pretrained(_a , **_a ) # It could be in `config.feature_extractor_type`` _A : Optional[int] = getattr(_a , """feature_extractor_type""" , _a ) if hasattr(_a , """auto_map""" ) and "AutoFeatureExtractor" in config.auto_map: _A : Tuple = config.auto_map["""AutoFeatureExtractor"""] if feature_extractor_class is not None: _A : Optional[Any] = feature_extractor_class_from_name(_a ) _A : List[Any] = feature_extractor_auto_map is not None _A : Union[str, Any] = feature_extractor_class is not None or type(_a ) in FEATURE_EXTRACTOR_MAPPING _A : Optional[int] = resolve_trust_remote_code( _a , _a , _a , _a ) if has_remote_code and trust_remote_code: _A : Dict = get_class_from_dynamic_module( _a , _a , **_a ) _A : str = kwargs.pop("""code_revision""" , _a ) if os.path.isdir(_a ): feature_extractor_class.register_for_auto_class() return feature_extractor_class.from_dict(_a , **_a ) elif feature_extractor_class is not None: return feature_extractor_class.from_dict(_a , **_a ) # Last try: we use the FEATURE_EXTRACTOR_MAPPING. elif type(_a ) in FEATURE_EXTRACTOR_MAPPING: _A : Dict = FEATURE_EXTRACTOR_MAPPING[type(_a )] return feature_extractor_class.from_dict(_a , **_a ) raise ValueError( F'''Unrecognized feature extractor in {pretrained_model_name_or_path}. Should have a ''' F'''`feature_extractor_type` key in its {FEATURE_EXTRACTOR_NAME} of {CONFIG_NAME}, or one of the following ''' F'''`model_type` keys in its {CONFIG_NAME}: {", ".join(c for c in FEATURE_EXTRACTOR_MAPPING_NAMES.keys() )}''' ) @staticmethod def a__ ( _a , _a ) -> Optional[int]: FEATURE_EXTRACTOR_MAPPING.register(_a , _a )
26
1
# Usage: # ./gen-card-allenai-wmt16.py import os from pathlib import Path def __A ( _lowercase , _lowercase , _lowercase , _lowercase ): '''simple docstring''' _A = { '''en''': '''Machine learning is great, isn\'t it?''', '''ru''': '''Машинное обучение - это здорово, не так ли?''', '''de''': '''Maschinelles Lernen ist großartig, nicht wahr?''', } # BLUE scores as follows: # "pair": [fairseq, transformers] _A = { '''wmt16-en-de-dist-12-1''': [28.3, 27.52], '''wmt16-en-de-dist-6-1''': [27.4, 27.11], '''wmt16-en-de-12-1''': [26.9, 25.75], } _A = f"""{src_lang}-{tgt_lang}""" _A = f""" --- language: - {src_lang} - {tgt_lang} thumbnail: tags: - translation - wmt16 - allenai license: apache-2.0 datasets: - wmt16 metrics: - bleu --- # FSMT ## Model description This is a ported version of fairseq-based [wmt16 transformer](https://github.com/jungokasai/deep-shallow/) for {src_lang}-{tgt_lang}. For more details, please, see [Deep Encoder, Shallow Decoder: Reevaluating the Speed-Quality Tradeoff in Machine Translation](https://arxiv.org/abs/2006.10369). All 3 models are available: * [wmt16-en-de-dist-12-1](https://huggingface.co/allenai/wmt16-en-de-dist-12-1) * [wmt16-en-de-dist-6-1](https://huggingface.co/allenai/wmt16-en-de-dist-6-1) * [wmt16-en-de-12-1](https://huggingface.co/allenai/wmt16-en-de-12-1) ## Intended uses & limitations #### How to use ```python from transformers import FSMTForConditionalGeneration, FSMTTokenizer mname = \"allenai/{model_name}\" tokenizer = FSMTTokenizer.from_pretrained(mname) model = FSMTForConditionalGeneration.from_pretrained(mname) input = \"{texts[src_lang]}\" input_ids = tokenizer.encode(input, return_tensors=\"pt\") outputs = model.generate(input_ids) decoded = tokenizer.decode(outputs[0], skip_special_tokens=True) print(decoded) # {texts[tgt_lang]} ``` #### Limitations and bias ## Training data Pretrained weights were left identical to the original model released by allenai. For more details, please, see the [paper](https://arxiv.org/abs/2006.10369). ## Eval results Here are the BLEU scores: model | fairseq | transformers -------|---------|---------- {model_name} | {scores[model_name][0]} | {scores[model_name][1]} The score is slightly below the score reported in the paper, as the researchers don't use `sacrebleu` and measure the score on tokenized outputs. `transformers` score was measured using `sacrebleu` on detokenized outputs. The score was calculated using this code: ```bash git clone https://github.com/huggingface/transformers cd transformers export PAIR={pair} export DATA_DIR=data/$PAIR export SAVE_DIR=data/$PAIR export BS=8 export NUM_BEAMS=5 mkdir -p $DATA_DIR sacrebleu -t wmt16 -l $PAIR --echo src > $DATA_DIR/val.source sacrebleu -t wmt16 -l $PAIR --echo ref > $DATA_DIR/val.target echo $PAIR PYTHONPATH=\"src:examples/seq2seq\" python examples/seq2seq/run_eval.py allenai/{model_name} $DATA_DIR/val.source $SAVE_DIR/test_translations.txt --reference_path $DATA_DIR/val.target --score_path $SAVE_DIR/test_bleu.json --bs $BS --task translation --num_beams $NUM_BEAMS ``` ## Data Sources - [training, etc.](http://www.statmt.org/wmt16/) - [test set](http://matrix.statmt.org/test_sets/newstest2016.tgz?1504722372) ### BibTeX entry and citation info ``` @misc{{kasai2020deep, title={{Deep Encoder, Shallow Decoder: Reevaluating the Speed-Quality Tradeoff in Machine Translation}}, author={{Jungo Kasai and Nikolaos Pappas and Hao Peng and James Cross and Noah A. Smith}}, year={{2020}}, eprint={{2006.10369}}, archivePrefix={{arXiv}}, primaryClass={{cs.CL}} }} ``` """ model_card_dir.mkdir(parents=_lowercase , exist_ok=_lowercase ) _A = os.path.join(_lowercase , '''README.md''' ) print(f"""Generating {path}""" ) with open(_lowercase , '''w''' , encoding='''utf-8''' ) as f: f.write(_lowercase ) # make sure we are under the root of the project __A = Path(__file__).resolve().parent.parent.parent __A = repo_dir / 'model_cards' for model_name in ["wmt16-en-de-dist-12-1", "wmt16-en-de-dist-6-1", "wmt16-en-de-12-1"]: __A = model_cards_dir / 'allenai' / model_name write_model_card(model_card_dir, src_lang='en', tgt_lang='de', model_name=model_name)
75
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, is_vision_available, ) __A = { 'configuration_owlvit': [ 'OWLVIT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'OwlViTConfig', 'OwlViTOnnxConfig', 'OwlViTTextConfig', 'OwlViTVisionConfig', ], 'processing_owlvit': ['OwlViTProcessor'], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A = ['OwlViTFeatureExtractor'] __A = ['OwlViTImageProcessor'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A = [ 'OWLVIT_PRETRAINED_MODEL_ARCHIVE_LIST', 'OwlViTModel', 'OwlViTPreTrainedModel', 'OwlViTTextModel', 'OwlViTVisionModel', 'OwlViTForObjectDetection', ] if TYPE_CHECKING: from .configuration_owlvit import ( OWLVIT_PRETRAINED_CONFIG_ARCHIVE_MAP, OwlViTConfig, OwlViTOnnxConfig, OwlViTTextConfig, OwlViTVisionConfig, ) from .processing_owlvit import OwlViTProcessor try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_owlvit import OwlViTFeatureExtractor from .image_processing_owlvit import OwlViTImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_owlvit import ( OWLVIT_PRETRAINED_MODEL_ARCHIVE_LIST, OwlViTForObjectDetection, OwlViTModel, OwlViTPreTrainedModel, OwlViTTextModel, OwlViTVisionModel, ) else: import sys __A = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
75
1
'''simple docstring''' import argparse from transformers import ( TapasConfig, TapasForMaskedLM, TapasForQuestionAnswering, TapasForSequenceClassification, TapasModel, TapasTokenizer, load_tf_weights_in_tapas, ) from transformers.utils import logging logging.set_verbosity_info() def UpperCAmelCase_ ( __lowercase : int , __lowercase : Dict , __lowercase : str , __lowercase : Optional[Any] , __lowercase : str ) -> List[str]: '''simple docstring''' _UpperCAmelCase = TapasConfig.from_json_file(__lowercase ) # set absolute/relative position embeddings parameter _UpperCAmelCase = reset_position_index_per_cell # set remaining parameters of TapasConfig as well as the model based on the task if task == "SQA": _UpperCAmelCase = TapasForQuestionAnswering(config=__lowercase ) elif task == "WTQ": # run_task_main.py hparams _UpperCAmelCase = 4 _UpperCAmelCase = True # hparam_utils.py hparams _UpperCAmelCase = 0.66_4694 _UpperCAmelCase = 0.20_7951 _UpperCAmelCase = 0.12_1194 _UpperCAmelCase = True _UpperCAmelCase = True _UpperCAmelCase = False _UpperCAmelCase = 0.035_2513 _UpperCAmelCase = TapasForQuestionAnswering(config=__lowercase ) elif task == "WIKISQL_SUPERVISED": # run_task_main.py hparams _UpperCAmelCase = 4 _UpperCAmelCase = False # hparam_utils.py hparams _UpperCAmelCase = 36.4519 _UpperCAmelCase = 0.90_3421 _UpperCAmelCase = 222.088 _UpperCAmelCase = True _UpperCAmelCase = True _UpperCAmelCase = True _UpperCAmelCase = 0.76_3141 _UpperCAmelCase = TapasForQuestionAnswering(config=__lowercase ) elif task == "TABFACT": _UpperCAmelCase = TapasForSequenceClassification(config=__lowercase ) elif task == "MLM": _UpperCAmelCase = TapasForMaskedLM(config=__lowercase ) elif task == "INTERMEDIATE_PRETRAINING": _UpperCAmelCase = TapasModel(config=__lowercase ) else: raise ValueError(f'Task {task} not supported.' ) print(f'Building PyTorch model from configuration: {config}' ) # Load weights from tf checkpoint load_tf_weights_in_tapas(__lowercase , __lowercase , __lowercase ) # Save pytorch-model (weights and configuration) print(f'Save PyTorch model to {pytorch_dump_path}' ) model.save_pretrained(__lowercase ) # Save tokenizer files print(f'Save tokenizer files to {pytorch_dump_path}' ) _UpperCAmelCase = TapasTokenizer(vocab_file=tf_checkpoint_path[:-10] + "vocab.txt" , model_max_length=512 ) tokenizer.save_pretrained(__lowercase ) print("Used relative position embeddings:" , model.config.reset_position_index_per_cell ) if __name__ == "__main__": __SCREAMING_SNAKE_CASE :List[str] = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--task''', default='''SQA''', type=str, help='''Model task for which to convert a checkpoint. Defaults to SQA.''' ) parser.add_argument( '''--reset_position_index_per_cell''', default=False, action='''store_true''', help='''Whether to use relative position embeddings or not. Defaults to True.''', ) parser.add_argument( '''--tf_checkpoint_path''', default=None, type=str, required=True, help='''Path to the TensorFlow checkpoint path.''' ) parser.add_argument( '''--tapas_config_file''', default=None, type=str, required=True, help=( '''The config json file corresponding to the pre-trained TAPAS model. \n''' '''This specifies the model architecture.''' ), ) parser.add_argument( '''--pytorch_dump_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.''' ) __SCREAMING_SNAKE_CASE :List[str] = parser.parse_args() convert_tf_checkpoint_to_pytorch( args.task, args.reset_position_index_per_cell, args.tf_checkpoint_path, args.tapas_config_file, args.pytorch_dump_path, )
22
'''simple docstring''' from __future__ import annotations from collections.abc import Callable from typing import Any, Generic, TypeVar __SCREAMING_SNAKE_CASE :Optional[int] = TypeVar('''T''') class A_ ( Generic[T] ): def __init__( self : List[Any] , snake_case_ : list[T] , snake_case_ : Callable[[T, T], T] ): _UpperCAmelCase = None _UpperCAmelCase = len(snake_case_ ) _UpperCAmelCase = [any_type for _ in range(self.N )] + arr _UpperCAmelCase = fnc self.build() def lowercase ( self : List[Any] ): for p in range(self.N - 1 , 0 , -1 ): _UpperCAmelCase = self.fn(self.st[p * 2] , self.st[p * 2 + 1] ) def lowercase ( self : Optional[Any] , snake_case_ : int , snake_case_ : T ): p += self.N _UpperCAmelCase = v while p > 1: _UpperCAmelCase = p // 2 _UpperCAmelCase = self.fn(self.st[p * 2] , self.st[p * 2 + 1] ) def lowercase ( self : Any , snake_case_ : int , snake_case_ : int ): # noqa: E741 _UpperCAmelCase , _UpperCAmelCase = l + self.N, r + self.N _UpperCAmelCase = None while l <= r: if l % 2 == 1: _UpperCAmelCase = self.st[l] if res is None else self.fn(snake_case_ , self.st[l] ) if r % 2 == 0: _UpperCAmelCase = self.st[r] if res is None else self.fn(snake_case_ , self.st[r] ) _UpperCAmelCase , _UpperCAmelCase = (l + 1) // 2, (r - 1) // 2 return res if __name__ == "__main__": from functools import reduce __SCREAMING_SNAKE_CASE :Union[str, Any] = [1, 10, -2, 9, -3, 8, 4, -7, 5, 6, 11, -12] __SCREAMING_SNAKE_CASE :List[str] = { 0: 7, 1: 2, 2: 6, 3: -14, 4: 5, 5: 4, 6: 7, 7: -10, 8: 9, 9: 10, 10: 12, 11: 1, } __SCREAMING_SNAKE_CASE :Any = SegmentTree(test_array, min) __SCREAMING_SNAKE_CASE :Any = SegmentTree(test_array, max) __SCREAMING_SNAKE_CASE :Any = SegmentTree(test_array, lambda a, b: a + b) def UpperCAmelCase_ ( ) -> None: '''simple docstring''' for i in range(len(__lowercase ) ): for j in range(__lowercase , len(__lowercase ) ): _UpperCAmelCase = reduce(__lowercase , test_array[i : j + 1] ) _UpperCAmelCase = reduce(__lowercase , test_array[i : j + 1] ) _UpperCAmelCase = reduce(lambda __lowercase , __lowercase : a + b , test_array[i : j + 1] ) assert min_range == min_segment_tree.query(__lowercase , __lowercase ) assert max_range == max_segment_tree.query(__lowercase , __lowercase ) assert sum_range == sum_segment_tree.query(__lowercase , __lowercase ) test_all_segments() for index, value in test_updates.items(): __SCREAMING_SNAKE_CASE :str = value min_segment_tree.update(index, value) max_segment_tree.update(index, value) sum_segment_tree.update(index, value) test_all_segments()
22
1
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 UpperCAmelCase_ = logging.get_logger(__name__) UpperCAmelCase_ = {"""vocab_file""": """vocab.json""", """merges_file""": """merges.txt"""} # See all BART models at https://huggingface.co/models?filter=bart UpperCAmelCase_ = { """vocab_file""": { """facebook/bart-base""": """https://huggingface.co/facebook/bart-base/resolve/main/vocab.json""", """facebook/bart-large""": """https://huggingface.co/facebook/bart-large/resolve/main/vocab.json""", """facebook/bart-large-mnli""": """https://huggingface.co/facebook/bart-large-mnli/resolve/main/vocab.json""", """facebook/bart-large-cnn""": """https://huggingface.co/facebook/bart-large-cnn/resolve/main/vocab.json""", """facebook/bart-large-xsum""": """https://huggingface.co/facebook/bart-large-xsum/resolve/main/vocab.json""", """yjernite/bart_eli5""": """https://huggingface.co/yjernite/bart_eli5/resolve/main/vocab.json""", }, """merges_file""": { """facebook/bart-base""": """https://huggingface.co/facebook/bart-base/resolve/main/merges.txt""", """facebook/bart-large""": """https://huggingface.co/facebook/bart-large/resolve/main/merges.txt""", """facebook/bart-large-mnli""": """https://huggingface.co/facebook/bart-large-mnli/resolve/main/merges.txt""", """facebook/bart-large-cnn""": """https://huggingface.co/facebook/bart-large-cnn/resolve/main/merges.txt""", """facebook/bart-large-xsum""": """https://huggingface.co/facebook/bart-large-xsum/resolve/main/merges.txt""", """yjernite/bart_eli5""": """https://huggingface.co/yjernite/bart_eli5/resolve/main/merges.txt""", }, } UpperCAmelCase_ = { """facebook/bart-base""": 1024, """facebook/bart-large""": 1024, """facebook/bart-large-mnli""": 1024, """facebook/bart-large-cnn""": 1024, """facebook/bart-large-xsum""": 1024, """yjernite/bart_eli5""": 1024, } @lru_cache() def lowerCamelCase__ ( ) -> Tuple: '''simple docstring''' _snake_case = ( list(range(ord('!' ) , ord('~' ) + 1 ) ) + list(range(ord('¡' ) , ord('¬' ) + 1 ) ) + list(range(ord('®' ) , ord('ÿ' ) + 1 ) ) ) _snake_case = bs[:] _snake_case = 0 for b in range(2**8 ): if b not in bs: bs.append(UpperCamelCase__ ) cs.append(2**8 + n ) n += 1 _snake_case = [chr(UpperCamelCase__ ) for n in cs] return dict(zip(UpperCamelCase__ , UpperCamelCase__ ) ) def lowerCamelCase__ ( UpperCamelCase__ : Tuple ) -> int: '''simple docstring''' _snake_case = set() _snake_case = word[0] for char in word[1:]: pairs.add((prev_char, char) ) _snake_case = char return pairs class UpperCamelCase_ ( _lowerCamelCase ): lowerCAmelCase_ = VOCAB_FILES_NAMES lowerCAmelCase_ = PRETRAINED_VOCAB_FILES_MAP lowerCAmelCase_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCAmelCase_ = ['''input_ids''', '''attention_mask'''] def __init__( self , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_="replace" , lowerCAmelCase_="<s>" , lowerCAmelCase_="</s>" , lowerCAmelCase_="</s>" , lowerCAmelCase_="<s>" , lowerCAmelCase_="<unk>" , lowerCAmelCase_="<pad>" , lowerCAmelCase_="<mask>" , lowerCAmelCase_=False , **lowerCAmelCase_ , ) -> Tuple: _snake_case = AddedToken(lowerCAmelCase_ , lstrip=lowerCAmelCase_ , rstrip=lowerCAmelCase_ ) if isinstance(lowerCAmelCase_ , lowerCAmelCase_ ) else bos_token _snake_case = AddedToken(lowerCAmelCase_ , lstrip=lowerCAmelCase_ , rstrip=lowerCAmelCase_ ) if isinstance(lowerCAmelCase_ , lowerCAmelCase_ ) else eos_token _snake_case = AddedToken(lowerCAmelCase_ , lstrip=lowerCAmelCase_ , rstrip=lowerCAmelCase_ ) if isinstance(lowerCAmelCase_ , lowerCAmelCase_ ) else sep_token _snake_case = AddedToken(lowerCAmelCase_ , lstrip=lowerCAmelCase_ , rstrip=lowerCAmelCase_ ) if isinstance(lowerCAmelCase_ , lowerCAmelCase_ ) else cls_token _snake_case = AddedToken(lowerCAmelCase_ , lstrip=lowerCAmelCase_ , rstrip=lowerCAmelCase_ ) if isinstance(lowerCAmelCase_ , lowerCAmelCase_ ) else unk_token _snake_case = AddedToken(lowerCAmelCase_ , lstrip=lowerCAmelCase_ , rstrip=lowerCAmelCase_ ) if isinstance(lowerCAmelCase_ , lowerCAmelCase_ ) else pad_token # Mask token behave like a normal word, i.e. include the space before it _snake_case = AddedToken(lowerCAmelCase_ , lstrip=lowerCAmelCase_ , rstrip=lowerCAmelCase_ ) if isinstance(lowerCAmelCase_ , lowerCAmelCase_ ) else mask_token super().__init__( errors=lowerCAmelCase_ , bos_token=lowerCAmelCase_ , eos_token=lowerCAmelCase_ , unk_token=lowerCAmelCase_ , sep_token=lowerCAmelCase_ , cls_token=lowerCAmelCase_ , pad_token=lowerCAmelCase_ , mask_token=lowerCAmelCase_ , add_prefix_space=lowerCAmelCase_ , **lowerCAmelCase_ , ) with open(lowerCAmelCase_ , encoding='utf-8' ) as vocab_handle: _snake_case = json.load(lowerCAmelCase_ ) _snake_case = {v: k for k, v in self.encoder.items()} _snake_case = errors # how to handle errors in decoding _snake_case = bytes_to_unicode() _snake_case = {v: k for k, v in self.byte_encoder.items()} with open(lowerCAmelCase_ , encoding='utf-8' ) as merges_handle: _snake_case = merges_handle.read().split('\n' )[1:-1] _snake_case = [tuple(merge.split() ) for merge in bpe_merges] _snake_case = dict(zip(lowerCAmelCase_ , range(len(lowerCAmelCase_ ) ) ) ) _snake_case = {} _snake_case = add_prefix_space # Should have added re.IGNORECASE so BPE merges can happen for capitalized versions of contractions _snake_case = re.compile(r'\'s|\'t|\'re|\'ve|\'m|\'ll|\'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+' ) @property def lowerCAmelCase ( self ) -> Any: return len(self.encoder ) def lowerCAmelCase ( self ) -> Dict: return dict(self.encoder , **self.added_tokens_encoder ) def lowerCAmelCase ( self , lowerCAmelCase_ ) -> Dict: if token in self.cache: return self.cache[token] _snake_case = tuple(lowerCAmelCase_ ) _snake_case = get_pairs(lowerCAmelCase_ ) if not pairs: return token while True: _snake_case = min(lowerCAmelCase_ , key=lambda lowerCAmelCase_ : self.bpe_ranks.get(lowerCAmelCase_ , float('inf' ) ) ) if bigram not in self.bpe_ranks: break _snake_case , _snake_case = bigram _snake_case = [] _snake_case = 0 while i < len(lowerCAmelCase_ ): try: _snake_case = word.index(lowerCAmelCase_ , lowerCAmelCase_ ) except ValueError: new_word.extend(word[i:] ) break else: new_word.extend(word[i:j] ) _snake_case = j if word[i] == first and i < len(lowerCAmelCase_ ) - 1 and word[i + 1] == second: new_word.append(first + second ) i += 2 else: new_word.append(word[i] ) i += 1 _snake_case = tuple(lowerCAmelCase_ ) _snake_case = new_word if len(lowerCAmelCase_ ) == 1: break else: _snake_case = get_pairs(lowerCAmelCase_ ) _snake_case = ' '.join(lowerCAmelCase_ ) _snake_case = word return word def lowerCAmelCase ( self , lowerCAmelCase_ ) -> List[Any]: _snake_case = [] for token in re.findall(self.pat , lowerCAmelCase_ ): _snake_case = ''.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(lowerCAmelCase_ ).split(' ' ) ) return bpe_tokens def lowerCAmelCase ( self , lowerCAmelCase_ ) -> str: return self.encoder.get(lowerCAmelCase_ , self.encoder.get(self.unk_token ) ) def lowerCAmelCase ( self , lowerCAmelCase_ ) -> Optional[int]: return self.decoder.get(lowerCAmelCase_ ) def lowerCAmelCase ( self , lowerCAmelCase_ ) -> Union[str, Any]: _snake_case = ''.join(lowerCAmelCase_ ) _snake_case = bytearray([self.byte_decoder[c] for c in text] ).decode('utf-8' , errors=self.errors ) return text def lowerCAmelCase ( self , lowerCAmelCase_ , lowerCAmelCase_ = None ) -> Tuple[str]: if not os.path.isdir(lowerCAmelCase_ ): logger.error(F'''Vocabulary path ({save_directory}) should be a directory''' ) return _snake_case = os.path.join( lowerCAmelCase_ , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) _snake_case = os.path.join( lowerCAmelCase_ , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['merges_file'] ) with open(lowerCAmelCase_ , 'w' , encoding='utf-8' ) as f: f.write(json.dumps(self.encoder , indent=2 , sort_keys=lowerCAmelCase_ , ensure_ascii=lowerCAmelCase_ ) + '\n' ) _snake_case = 0 with open(lowerCAmelCase_ , '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 lowerCAmelCase_ : 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 = token_index writer.write(' '.join(lowerCAmelCase_ ) + '\n' ) index += 1 return vocab_file, merge_file def lowerCAmelCase ( self , lowerCAmelCase_ , lowerCAmelCase_ = None ) -> List[int]: if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] _snake_case = [self.cls_token_id] _snake_case = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def lowerCAmelCase ( self , lowerCAmelCase_ , lowerCAmelCase_ = None , lowerCAmelCase_ = False ) -> List[int]: if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=lowerCAmelCase_ , token_ids_a=lowerCAmelCase_ , already_has_special_tokens=lowerCAmelCase_ ) if token_ids_a is None: return [1] + ([0] * len(lowerCAmelCase_ )) + [1] return [1] + ([0] * len(lowerCAmelCase_ )) + [1, 1] + ([0] * len(lowerCAmelCase_ )) + [1] def lowerCAmelCase ( self , lowerCAmelCase_ , lowerCAmelCase_ = None ) -> List[int]: _snake_case = [self.sep_token_id] _snake_case = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def lowerCAmelCase ( self , lowerCAmelCase_ , lowerCAmelCase_=False , **lowerCAmelCase_ ) -> str: _snake_case = kwargs.pop('add_prefix_space' , self.add_prefix_space ) if (is_split_into_words or add_prefix_space) and (len(lowerCAmelCase_ ) > 0 and not text[0].isspace()): _snake_case = ' ' + text return (text, kwargs)
295
import math import torch from torch import nn from ..configuration_utils import ConfigMixin, register_to_config from .attention_processor import Attention from .embeddings import get_timestep_embedding from .modeling_utils import ModelMixin class UpperCamelCase_ ( _lowerCamelCase , _lowerCamelCase ): @register_to_config def __init__( self , lowerCAmelCase_ = 128 , lowerCAmelCase_ = 256 , lowerCAmelCase_ = 20_00.0 , lowerCAmelCase_ = 768 , lowerCAmelCase_ = 12 , lowerCAmelCase_ = 12 , lowerCAmelCase_ = 64 , lowerCAmelCase_ = 2048 , lowerCAmelCase_ = 0.1 , ) -> Union[str, Any]: super().__init__() _snake_case = nn.Sequential( nn.Linear(lowerCAmelCase_ , d_model * 4 , bias=lowerCAmelCase_ ) , nn.SiLU() , nn.Linear(d_model * 4 , d_model * 4 , bias=lowerCAmelCase_ ) , nn.SiLU() , ) _snake_case = nn.Embedding(lowerCAmelCase_ , lowerCAmelCase_ ) _snake_case = False _snake_case = nn.Linear(lowerCAmelCase_ , lowerCAmelCase_ , bias=lowerCAmelCase_ ) _snake_case = nn.Dropout(p=lowerCAmelCase_ ) _snake_case = nn.ModuleList() for lyr_num in range(lowerCAmelCase_ ): # FiLM conditional T5 decoder _snake_case = DecoderLayer(d_model=lowerCAmelCase_ , d_kv=lowerCAmelCase_ , num_heads=lowerCAmelCase_ , d_ff=lowerCAmelCase_ , dropout_rate=lowerCAmelCase_ ) self.decoders.append(lowerCAmelCase_ ) _snake_case = TaLayerNorm(lowerCAmelCase_ ) _snake_case = nn.Dropout(p=lowerCAmelCase_ ) _snake_case = nn.Linear(lowerCAmelCase_ , lowerCAmelCase_ , bias=lowerCAmelCase_ ) def lowerCAmelCase ( self , lowerCAmelCase_ , lowerCAmelCase_ ) -> Tuple: _snake_case = torch.mul(query_input.unsqueeze(-1 ) , key_input.unsqueeze(-2 ) ) return mask.unsqueeze(-3 ) def lowerCAmelCase ( self , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ) -> Optional[Any]: _snake_case , _snake_case , _snake_case = decoder_input_tokens.shape assert decoder_noise_time.shape == (batch,) # decoder_noise_time is in [0, 1), so rescale to expected timing range. _snake_case = get_timestep_embedding( decoder_noise_time * self.config.max_decoder_noise_time , embedding_dim=self.config.d_model , max_period=self.config.max_decoder_noise_time , ).to(dtype=self.dtype ) _snake_case = self.conditioning_emb(lowerCAmelCase_ ).unsqueeze(1 ) assert conditioning_emb.shape == (batch, 1, self.config.d_model * 4) _snake_case = decoder_input_tokens.shape[1] # If we want to use relative positions for audio context, we can just offset # this sequence by the length of encodings_and_masks. _snake_case = torch.broadcast_to( torch.arange(lowerCAmelCase_ , device=decoder_input_tokens.device ) , (batch, seq_length) , ) _snake_case = self.position_encoding(lowerCAmelCase_ ) _snake_case = self.continuous_inputs_projection(lowerCAmelCase_ ) inputs += position_encodings _snake_case = self.dropout(lowerCAmelCase_ ) # decoder: No padding present. _snake_case = torch.ones( decoder_input_tokens.shape[:2] , device=decoder_input_tokens.device , dtype=inputs.dtype ) # Translate encoding masks to encoder-decoder masks. _snake_case = [(x, self.encoder_decoder_mask(lowerCAmelCase_ , lowerCAmelCase_ )) for x, y in encodings_and_masks] # cross attend style: concat encodings _snake_case = torch.cat([x[0] for x in encodings_and_encdec_masks] , dim=1 ) _snake_case = torch.cat([x[1] for x in encodings_and_encdec_masks] , dim=-1 ) for lyr in self.decoders: _snake_case = lyr( lowerCAmelCase_ , conditioning_emb=lowerCAmelCase_ , encoder_hidden_states=lowerCAmelCase_ , encoder_attention_mask=lowerCAmelCase_ , )[0] _snake_case = self.decoder_norm(lowerCAmelCase_ ) _snake_case = self.post_dropout(lowerCAmelCase_ ) _snake_case = self.spec_out(lowerCAmelCase_ ) return spec_out class UpperCamelCase_ ( nn.Module ): def __init__( self , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_=1E-6 ) -> Tuple: super().__init__() _snake_case = nn.ModuleList() # cond self attention: layer 0 self.layer.append( TaLayerSelfAttentionCond(d_model=lowerCAmelCase_ , d_kv=lowerCAmelCase_ , num_heads=lowerCAmelCase_ , dropout_rate=lowerCAmelCase_ ) ) # cross attention: layer 1 self.layer.append( TaLayerCrossAttention( d_model=lowerCAmelCase_ , d_kv=lowerCAmelCase_ , num_heads=lowerCAmelCase_ , dropout_rate=lowerCAmelCase_ , layer_norm_epsilon=lowerCAmelCase_ , ) ) # Film Cond MLP + dropout: last layer self.layer.append( TaLayerFFCond(d_model=lowerCAmelCase_ , d_ff=lowerCAmelCase_ , dropout_rate=lowerCAmelCase_ , layer_norm_epsilon=lowerCAmelCase_ ) ) def lowerCAmelCase ( self , lowerCAmelCase_ , lowerCAmelCase_=None , lowerCAmelCase_=None , lowerCAmelCase_=None , lowerCAmelCase_=None , lowerCAmelCase_=None , ) -> Tuple: _snake_case = self.layer[0]( lowerCAmelCase_ , conditioning_emb=lowerCAmelCase_ , attention_mask=lowerCAmelCase_ , ) if encoder_hidden_states is not None: _snake_case = torch.where(encoder_attention_mask > 0 , 0 , -1E10 ).to( encoder_hidden_states.dtype ) _snake_case = self.layer[1]( lowerCAmelCase_ , key_value_states=lowerCAmelCase_ , attention_mask=lowerCAmelCase_ , ) # Apply Film Conditional Feed Forward layer _snake_case = self.layer[-1](lowerCAmelCase_ , lowerCAmelCase_ ) return (hidden_states,) class UpperCamelCase_ ( nn.Module ): def __init__( self , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ) -> Tuple: super().__init__() _snake_case = TaLayerNorm(lowerCAmelCase_ ) _snake_case = TaFiLMLayer(in_features=d_model * 4 , out_features=lowerCAmelCase_ ) _snake_case = Attention(query_dim=lowerCAmelCase_ , heads=lowerCAmelCase_ , dim_head=lowerCAmelCase_ , out_bias=lowerCAmelCase_ , scale_qk=lowerCAmelCase_ ) _snake_case = nn.Dropout(lowerCAmelCase_ ) def lowerCAmelCase ( self , lowerCAmelCase_ , lowerCAmelCase_=None , lowerCAmelCase_=None , ) -> str: # pre_self_attention_layer_norm _snake_case = self.layer_norm(lowerCAmelCase_ ) if conditioning_emb is not None: _snake_case = self.FiLMLayer(lowerCAmelCase_ , lowerCAmelCase_ ) # Self-attention block _snake_case = self.attention(lowerCAmelCase_ ) _snake_case = hidden_states + self.dropout(lowerCAmelCase_ ) return hidden_states class UpperCamelCase_ ( nn.Module ): def __init__( self , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ) -> str: super().__init__() _snake_case = Attention(query_dim=lowerCAmelCase_ , heads=lowerCAmelCase_ , dim_head=lowerCAmelCase_ , out_bias=lowerCAmelCase_ , scale_qk=lowerCAmelCase_ ) _snake_case = TaLayerNorm(lowerCAmelCase_ , eps=lowerCAmelCase_ ) _snake_case = nn.Dropout(lowerCAmelCase_ ) def lowerCAmelCase ( self , lowerCAmelCase_ , lowerCAmelCase_=None , lowerCAmelCase_=None , ) -> Dict: _snake_case = self.layer_norm(lowerCAmelCase_ ) _snake_case = self.attention( lowerCAmelCase_ , encoder_hidden_states=lowerCAmelCase_ , attention_mask=attention_mask.squeeze(1 ) , ) _snake_case = hidden_states + self.dropout(lowerCAmelCase_ ) return layer_output class UpperCamelCase_ ( nn.Module ): def __init__( self , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ) -> List[str]: super().__init__() _snake_case = TaDenseGatedActDense(d_model=lowerCAmelCase_ , d_ff=lowerCAmelCase_ , dropout_rate=lowerCAmelCase_ ) _snake_case = TaFiLMLayer(in_features=d_model * 4 , out_features=lowerCAmelCase_ ) _snake_case = TaLayerNorm(lowerCAmelCase_ , eps=lowerCAmelCase_ ) _snake_case = nn.Dropout(lowerCAmelCase_ ) def lowerCAmelCase ( self , lowerCAmelCase_ , lowerCAmelCase_=None ) -> Union[str, Any]: _snake_case = self.layer_norm(lowerCAmelCase_ ) if conditioning_emb is not None: _snake_case = self.film(lowerCAmelCase_ , lowerCAmelCase_ ) _snake_case = self.DenseReluDense(lowerCAmelCase_ ) _snake_case = hidden_states + self.dropout(lowerCAmelCase_ ) return hidden_states class UpperCamelCase_ ( nn.Module ): def __init__( self , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ) -> Union[str, Any]: super().__init__() _snake_case = nn.Linear(lowerCAmelCase_ , lowerCAmelCase_ , bias=lowerCAmelCase_ ) _snake_case = nn.Linear(lowerCAmelCase_ , lowerCAmelCase_ , bias=lowerCAmelCase_ ) _snake_case = nn.Linear(lowerCAmelCase_ , lowerCAmelCase_ , bias=lowerCAmelCase_ ) _snake_case = nn.Dropout(lowerCAmelCase_ ) _snake_case = NewGELUActivation() def lowerCAmelCase ( self , lowerCAmelCase_ ) -> Any: _snake_case = self.act(self.wi_a(lowerCAmelCase_ ) ) _snake_case = self.wi_a(lowerCAmelCase_ ) _snake_case = hidden_gelu * hidden_linear _snake_case = self.dropout(lowerCAmelCase_ ) _snake_case = self.wo(lowerCAmelCase_ ) return hidden_states class UpperCamelCase_ ( nn.Module ): def __init__( self , lowerCAmelCase_ , lowerCAmelCase_=1E-6 ) -> str: super().__init__() _snake_case = nn.Parameter(torch.ones(lowerCAmelCase_ ) ) _snake_case = eps def lowerCAmelCase ( self , lowerCAmelCase_ ) -> int: # T5 uses a layer_norm which only scales and doesn't shift, which is also known as Root Mean # Square Layer Normalization https://arxiv.org/abs/1910.07467 thus variance is calculated # w/o mean and there is no bias. Additionally we want to make sure that the accumulation for # half-precision inputs is done in fp32 _snake_case = hidden_states.to(torch.floataa ).pow(2 ).mean(-1 , keepdim=lowerCAmelCase_ ) _snake_case = hidden_states * torch.rsqrt(variance + self.variance_epsilon ) # convert into half-precision if necessary if self.weight.dtype in [torch.floataa, torch.bfloataa]: _snake_case = hidden_states.to(self.weight.dtype ) return self.weight * hidden_states class UpperCamelCase_ ( nn.Module ): def lowerCAmelCase ( self , lowerCAmelCase_ ) -> torch.Tensor: return 0.5 * input * (1.0 + torch.tanh(math.sqrt(2.0 / math.pi ) * (input + 0.04_47_15 * torch.pow(lowerCAmelCase_ , 3.0 )) )) class UpperCamelCase_ ( nn.Module ): def __init__( self , lowerCAmelCase_ , lowerCAmelCase_ ) -> Any: super().__init__() _snake_case = nn.Linear(lowerCAmelCase_ , out_features * 2 , bias=lowerCAmelCase_ ) def lowerCAmelCase ( self , lowerCAmelCase_ , lowerCAmelCase_ ) -> Optional[Any]: _snake_case = self.scale_bias(lowerCAmelCase_ ) _snake_case , _snake_case = torch.chunk(lowerCAmelCase_ , 2 , -1 ) _snake_case = x * (1 + scale) + shift return x
295
1
import argparse import requests import torch from PIL import Image from transformers import SwinConfig, SwinForMaskedImageModeling, ViTImageProcessor def SCREAMING_SNAKE_CASE_ ( __A : List[str] ) -> Union[str, Any]: """simple docstring""" a_ : Dict = SwinConfig(image_size=1_92 ) if "base" in model_name: a_ : List[str] = 6 a_ : int = 1_28 a_ : Tuple = (2, 2, 18, 2) a_ : Optional[int] = (4, 8, 16, 32) elif "large" in model_name: a_ : List[str] = 12 a_ : Union[str, Any] = 1_92 a_ : Union[str, Any] = (2, 2, 18, 2) a_ : str = (6, 12, 24, 48) else: raise ValueError('Model not supported, only supports base and large variants' ) a_ : List[str] = window_size a_ : Any = embed_dim a_ : Optional[int] = depths a_ : List[Any] = num_heads return config def SCREAMING_SNAKE_CASE_ ( __A : List[str] ) -> Optional[int]: """simple docstring""" if "encoder.mask_token" in name: a_ : Dict = name.replace('encoder.mask_token' , 'embeddings.mask_token' ) if "encoder.patch_embed.proj" in name: a_ : int = name.replace('encoder.patch_embed.proj' , 'embeddings.patch_embeddings.projection' ) if "encoder.patch_embed.norm" in name: a_ : Tuple = name.replace('encoder.patch_embed.norm' , 'embeddings.norm' ) if "attn.proj" in name: a_ : int = name.replace('attn.proj' , 'attention.output.dense' ) if "attn" in name: a_ : Union[str, Any] = name.replace('attn' , 'attention.self' ) if "norm1" in name: a_ : Dict = name.replace('norm1' , 'layernorm_before' ) if "norm2" in name: a_ : int = name.replace('norm2' , 'layernorm_after' ) if "mlp.fc1" in name: a_ : Tuple = name.replace('mlp.fc1' , 'intermediate.dense' ) if "mlp.fc2" in name: a_ : Dict = name.replace('mlp.fc2' , 'output.dense' ) if name == "encoder.norm.weight": a_ : Optional[int] = 'layernorm.weight' if name == "encoder.norm.bias": a_ : Union[str, Any] = 'layernorm.bias' if "decoder" in name: pass else: a_ : List[str] = 'swin.' + name return name def SCREAMING_SNAKE_CASE_ ( __A : Optional[Any] , __A : Tuple ) -> List[str]: """simple docstring""" for key in orig_state_dict.copy().keys(): a_ : List[str] = orig_state_dict.pop(__A ) if "attn_mask" in key: pass elif "qkv" in key: a_ : int = key.split('.' ) a_ : Dict = int(key_split[2] ) a_ : Union[str, Any] = int(key_split[4] ) a_ : Dict = model.swin.encoder.layers[layer_num].blocks[block_num].attention.self.all_head_size if "weight" in key: a_ : int = val[:dim, :] a_ : Optional[Any] = val[ dim : dim * 2, : ] a_ : List[str] = val[-dim:, :] else: a_ : Union[str, Any] = val[ :dim ] a_ : Dict = val[ dim : dim * 2 ] a_ : Tuple = val[ -dim: ] else: a_ : int = val return orig_state_dict def SCREAMING_SNAKE_CASE_ ( __A : str , __A : Any , __A : str , __A : Optional[int] ) -> Union[str, Any]: """simple docstring""" a_ : str = torch.load(__A , map_location='cpu' )['model'] a_ : Union[str, Any] = get_swin_config(__A ) a_ : List[str] = SwinForMaskedImageModeling(__A ) model.eval() a_ : Dict = convert_state_dict(__A , __A ) model.load_state_dict(__A ) a_ : List[Any] = 'http://images.cocodataset.org/val2017/000000039769.jpg' a_ : List[str] = ViTImageProcessor(size={'height': 1_92, 'width': 1_92} ) a_ : Optional[Any] = Image.open(requests.get(__A , stream=__A ).raw ) a_ : List[str] = image_processor(images=__A , return_tensors='pt' ) with torch.no_grad(): a_ : str = model(**__A ).logits print(outputs.keys() ) print('Looks ok!' ) if pytorch_dump_folder_path is not None: print(F"""Saving model {model_name} to {pytorch_dump_folder_path}""" ) model.save_pretrained(__A ) print(F"""Saving image processor to {pytorch_dump_folder_path}""" ) image_processor.save_pretrained(__A ) if push_to_hub: print(F"""Pushing model and image processor for {model_name} to hub""" ) model.push_to_hub(F"""microsoft/{model_name}""" ) image_processor.push_to_hub(F"""microsoft/{model_name}""" ) if __name__ == "__main__": UpperCAmelCase_ : List[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '--model_name', default='swin-base-simmim-window6-192', type=str, choices=['swin-base-simmim-window6-192', 'swin-large-simmim-window12-192'], help='Name of the Swin SimMIM model you\'d like to convert.', ) parser.add_argument( '--checkpoint_path', default='/Users/nielsrogge/Documents/SwinSimMIM/simmim_pretrain__swin_base__img192_window6__100ep.pth', type=str, help='Path to the original PyTorch checkpoint (.pth file).', ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model directory.' ) parser.add_argument( '--push_to_hub', action='store_true', help='Whether or not to push the converted model to the 🤗 hub.' ) UpperCAmelCase_ : List[Any] = parser.parse_args() convert_swin_checkpoint(args.model_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub)
32
'''simple docstring''' import warnings from ..trainer import Trainer from ..utils import logging __a = logging.get_logger(__name__) class A__ ( UpperCamelCase ): """simple docstring""" def __init__( self : Optional[int] , lowerCAmelCase__ : List[str]=None , **lowerCAmelCase__ : List[str] ) -> Union[str, Any]: """simple docstring""" warnings.warn( "`SageMakerTrainer` is deprecated and will be removed in v5 of Transformers. You can use `Trainer` " "instead." , lowerCAmelCase__ , ) super().__init__(args=lowerCAmelCase__ , **lowerCAmelCase__ )
145
0
'''simple docstring''' import argparse import torch from datasets import load_dataset from donut import DonutModel from transformers import ( DonutImageProcessor, DonutProcessor, DonutSwinConfig, DonutSwinModel, MBartConfig, MBartForCausalLM, VisionEncoderDecoderModel, XLMRobertaTokenizerFast, ) def a_ ( __snake_case : Dict ) -> Any: """simple docstring""" lowerCamelCase_ =model.config lowerCamelCase_ =DonutSwinConfig( image_size=original_config.input_size , patch_size=4 , depths=original_config.encoder_layer , num_heads=[4, 8, 16, 32] , window_size=original_config.window_size , embed_dim=128 , ) lowerCamelCase_ =MBartConfig( is_decoder=SCREAMING_SNAKE_CASE_ , is_encoder_decoder=SCREAMING_SNAKE_CASE_ , add_cross_attention=SCREAMING_SNAKE_CASE_ , decoder_layers=original_config.decoder_layer , max_position_embeddings=original_config.max_position_embeddings , vocab_size=len( model.decoder.tokenizer ) , scale_embedding=SCREAMING_SNAKE_CASE_ , add_final_layer_norm=SCREAMING_SNAKE_CASE_ , ) return encoder_config, decoder_config def a_ ( __snake_case : List[str] ) -> Union[str, Any]: """simple docstring""" if "encoder.model" in name: lowerCamelCase_ =name.replace('''encoder.model''' , '''encoder''' ) if "decoder.model" in name: lowerCamelCase_ =name.replace('''decoder.model''' , '''decoder''' ) if "patch_embed.proj" in name: lowerCamelCase_ =name.replace('''patch_embed.proj''' , '''embeddings.patch_embeddings.projection''' ) if "patch_embed.norm" in name: lowerCamelCase_ =name.replace('''patch_embed.norm''' , '''embeddings.norm''' ) if name.startswith('''encoder''' ): if "layers" in name: lowerCamelCase_ ='''encoder.''' + name if "attn.proj" in name: lowerCamelCase_ =name.replace('''attn.proj''' , '''attention.output.dense''' ) if "attn" in name and "mask" not in name: lowerCamelCase_ =name.replace('''attn''' , '''attention.self''' ) if "norm1" in name: lowerCamelCase_ =name.replace('''norm1''' , '''layernorm_before''' ) if "norm2" in name: lowerCamelCase_ =name.replace('''norm2''' , '''layernorm_after''' ) if "mlp.fc1" in name: lowerCamelCase_ =name.replace('''mlp.fc1''' , '''intermediate.dense''' ) if "mlp.fc2" in name: lowerCamelCase_ =name.replace('''mlp.fc2''' , '''output.dense''' ) if name == "encoder.norm.weight": lowerCamelCase_ ='''encoder.layernorm.weight''' if name == "encoder.norm.bias": lowerCamelCase_ ='''encoder.layernorm.bias''' return name def a_ ( __snake_case : List[str] , __snake_case : Dict ) -> Union[str, Any]: """simple docstring""" for key in orig_state_dict.copy().keys(): lowerCamelCase_ =orig_state_dict.pop(SCREAMING_SNAKE_CASE_ ) if "qkv" in key: lowerCamelCase_ =key.split('''.''' ) lowerCamelCase_ =int(key_split[3] ) lowerCamelCase_ =int(key_split[5] ) lowerCamelCase_ =model.encoder.encoder.layers[layer_num].blocks[block_num].attention.self.all_head_size if "weight" in key: lowerCamelCase_ =val[:dim, :] lowerCamelCase_ =val[dim : dim * 2, :] lowerCamelCase_ =val[-dim:, :] else: lowerCamelCase_ =val[:dim] lowerCamelCase_ =val[dim : dim * 2] lowerCamelCase_ =val[-dim:] elif "attn_mask" in key or key in ["encoder.model.norm.weight", "encoder.model.norm.bias"]: # HuggingFace implementation doesn't use attn_mask buffer # and model doesn't use final LayerNorms for the encoder pass else: lowerCamelCase_ =val return orig_state_dict def a_ ( __snake_case : List[Any] , __snake_case : List[str]=None , __snake_case : Union[str, Any]=False ) -> Any: """simple docstring""" # load original model lowerCamelCase_ =DonutModel.from_pretrained(SCREAMING_SNAKE_CASE_ ).eval() # load HuggingFace model lowerCamelCase_, lowerCamelCase_ =get_configs(SCREAMING_SNAKE_CASE_ ) lowerCamelCase_ =DonutSwinModel(SCREAMING_SNAKE_CASE_ ) lowerCamelCase_ =MBartForCausalLM(SCREAMING_SNAKE_CASE_ ) lowerCamelCase_ =VisionEncoderDecoderModel(encoder=SCREAMING_SNAKE_CASE_ , decoder=SCREAMING_SNAKE_CASE_ ) model.eval() lowerCamelCase_ =original_model.state_dict() lowerCamelCase_ =convert_state_dict(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) model.load_state_dict(SCREAMING_SNAKE_CASE_ ) # verify results on scanned document lowerCamelCase_ =load_dataset('''hf-internal-testing/example-documents''' ) lowerCamelCase_ =dataset['''test'''][0]['''image'''].convert('''RGB''' ) lowerCamelCase_ =XLMRobertaTokenizerFast.from_pretrained(SCREAMING_SNAKE_CASE_ , from_slow=SCREAMING_SNAKE_CASE_ ) lowerCamelCase_ =DonutImageProcessor( do_align_long_axis=original_model.config.align_long_axis , size=original_model.config.input_size[::-1] ) lowerCamelCase_ =DonutProcessor(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) lowerCamelCase_ =processor(SCREAMING_SNAKE_CASE_ , return_tensors='''pt''' ).pixel_values if model_name == "naver-clova-ix/donut-base-finetuned-docvqa": lowerCamelCase_ ='''<s_docvqa><s_question>{user_input}</s_question><s_answer>''' lowerCamelCase_ ='''When is the coffee break?''' lowerCamelCase_ =task_prompt.replace('''{user_input}''' , SCREAMING_SNAKE_CASE_ ) elif model_name == "naver-clova-ix/donut-base-finetuned-rvlcdip": lowerCamelCase_ ='''<s_rvlcdip>''' elif model_name in [ "naver-clova-ix/donut-base-finetuned-cord-v1", "naver-clova-ix/donut-base-finetuned-cord-v1-2560", ]: lowerCamelCase_ ='''<s_cord>''' elif model_name == "naver-clova-ix/donut-base-finetuned-cord-v2": lowerCamelCase_ ='''s_cord-v2>''' elif model_name == "naver-clova-ix/donut-base-finetuned-zhtrainticket": lowerCamelCase_ ='''<s_zhtrainticket>''' elif model_name in ["naver-clova-ix/donut-proto", "naver-clova-ix/donut-base"]: # use a random prompt lowerCamelCase_ ='''hello world''' else: raise ValueError('''Model name not supported''' ) lowerCamelCase_ =original_model.decoder.tokenizer(SCREAMING_SNAKE_CASE_ , add_special_tokens=SCREAMING_SNAKE_CASE_ , return_tensors='''pt''' )[ '''input_ids''' ] lowerCamelCase_ =original_model.encoder.model.patch_embed(SCREAMING_SNAKE_CASE_ ) lowerCamelCase_, lowerCamelCase_ =model.encoder.embeddings(SCREAMING_SNAKE_CASE_ ) assert torch.allclose(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , atol=1e-3 ) # verify encoder hidden states lowerCamelCase_ =original_model.encoder(SCREAMING_SNAKE_CASE_ ) lowerCamelCase_ =model.encoder(SCREAMING_SNAKE_CASE_ ).last_hidden_state assert torch.allclose(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , atol=1e-2 ) # verify decoder hidden states lowerCamelCase_ =original_model(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ).logits lowerCamelCase_ =model(SCREAMING_SNAKE_CASE_ , decoder_input_ids=SCREAMING_SNAKE_CASE_ ).logits assert torch.allclose(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , atol=1e-3 ) print('''Looks ok!''' ) if pytorch_dump_folder_path is not None: print(F'''Saving model and processor to {pytorch_dump_folder_path}''' ) model.save_pretrained(SCREAMING_SNAKE_CASE_ ) processor.save_pretrained(SCREAMING_SNAKE_CASE_ ) if push_to_hub: model.push_to_hub('''nielsr/''' + model_name.split('''/''' )[-1] , commit_message='''Update model''' ) processor.push_to_hub('''nielsr/''' + model_name.split('''/''' )[-1] , commit_message='''Update model''' ) if __name__ == "__main__": a_ : List[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( """--model_name""", default="""naver-clova-ix/donut-base-finetuned-docvqa""", required=False, type=str, help="""Name of the original model you\'d like to convert.""", ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, required=False, 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 and processor to the 🤗 hub.""", ) a_ : Any = parser.parse_args() convert_donut_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
365
'''simple docstring''' import gc import random import unittest import numpy as np import torch from transformers import CLIPImageProcessor, CLIPVisionConfig, CLIPVisionModel from diffusers import HeunDiscreteScheduler, PriorTransformer, ShapEImgaImgPipeline from diffusers.pipelines.shap_e import ShapERenderer from diffusers.utils import floats_tensor, load_image, load_numpy, slow from diffusers.utils.testing_utils import require_torch_gpu, torch_device from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference class __UpperCamelCase ( lowerCamelCase__ , unittest.TestCase ): lowercase : Union[str, Any] =ShapEImgaImgPipeline lowercase : Dict =['image'] lowercase : str =['image'] lowercase : int =[ 'num_images_per_prompt', 'num_inference_steps', 'generator', 'latents', 'guidance_scale', 'frame_size', 'output_type', 'return_dict', ] lowercase : int =False @property def lowercase__ ( self ): """simple docstring""" return 32 @property def lowercase__ ( self ): """simple docstring""" return 32 @property def lowercase__ ( self ): """simple docstring""" return self.time_input_dim * 4 @property def lowercase__ ( self ): """simple docstring""" return 8 @property def lowercase__ ( self ): """simple docstring""" torch.manual_seed(0 ) lowerCamelCase_ =CLIPVisionConfig( hidden_size=self.text_embedder_hidden_size, image_size=64, projection_dim=self.text_embedder_hidden_size, intermediate_size=37, num_attention_heads=4, num_channels=3, num_hidden_layers=5, patch_size=1, ) lowerCamelCase_ =CLIPVisionModel(lowerCAmelCase ) return model @property def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =CLIPImageProcessor( crop_size=224, do_center_crop=lowerCAmelCase, do_normalize=lowerCAmelCase, do_resize=lowerCAmelCase, image_mean=[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], image_std=[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], resample=3, size=224, ) return image_processor @property def lowercase__ ( self ): """simple docstring""" torch.manual_seed(0 ) lowerCamelCase_ ={ '''num_attention_heads''': 2, '''attention_head_dim''': 16, '''embedding_dim''': self.time_input_dim, '''num_embeddings''': 32, '''embedding_proj_dim''': self.text_embedder_hidden_size, '''time_embed_dim''': self.time_embed_dim, '''num_layers''': 1, '''clip_embed_dim''': self.time_input_dim * 2, '''additional_embeddings''': 0, '''time_embed_act_fn''': '''gelu''', '''norm_in_type''': '''layer''', '''embedding_proj_norm_type''': '''layer''', '''encoder_hid_proj_type''': None, '''added_emb_type''': None, } lowerCamelCase_ =PriorTransformer(**lowerCAmelCase ) return model @property def lowercase__ ( self ): """simple docstring""" torch.manual_seed(0 ) lowerCamelCase_ ={ '''param_shapes''': ( (self.renderer_dim, 93), (self.renderer_dim, 8), (self.renderer_dim, 8), (self.renderer_dim, 8), ), '''d_latent''': self.time_input_dim, '''d_hidden''': self.renderer_dim, '''n_output''': 12, '''background''': ( 0.1, 0.1, 0.1, ), } lowerCamelCase_ =ShapERenderer(**lowerCAmelCase ) return model def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =self.dummy_prior lowerCamelCase_ =self.dummy_image_encoder lowerCamelCase_ =self.dummy_image_processor lowerCamelCase_ =self.dummy_renderer lowerCamelCase_ =HeunDiscreteScheduler( beta_schedule='''exp''', num_train_timesteps=1_024, prediction_type='''sample''', use_karras_sigmas=lowerCAmelCase, clip_sample=lowerCAmelCase, clip_sample_range=1.0, ) lowerCamelCase_ ={ '''prior''': prior, '''image_encoder''': image_encoder, '''image_processor''': image_processor, '''renderer''': renderer, '''scheduler''': scheduler, } return components def lowercase__ ( self, lowerCAmelCase, lowerCAmelCase=0 ): """simple docstring""" lowerCamelCase_ =floats_tensor((1, 3, 64, 64), rng=random.Random(lowerCAmelCase ) ).to(lowerCAmelCase ) if str(lowerCAmelCase ).startswith('''mps''' ): lowerCamelCase_ =torch.manual_seed(lowerCAmelCase ) else: lowerCamelCase_ =torch.Generator(device=lowerCAmelCase ).manual_seed(lowerCAmelCase ) lowerCamelCase_ ={ '''image''': input_image, '''generator''': generator, '''num_inference_steps''': 1, '''frame_size''': 32, '''output_type''': '''np''', } return inputs def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ ='''cpu''' lowerCamelCase_ =self.get_dummy_components() lowerCamelCase_ =self.pipeline_class(**lowerCAmelCase ) lowerCamelCase_ =pipe.to(lowerCAmelCase ) pipe.set_progress_bar_config(disable=lowerCAmelCase ) lowerCamelCase_ =pipe(**self.get_dummy_inputs(lowerCAmelCase ) ) lowerCamelCase_ =output.images[0] lowerCamelCase_ =image[0, -3:, -3:, -1] assert image.shape == (20, 32, 32, 3) lowerCamelCase_ =np.array( [ 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, 0.0_0_0_3_9_2_1_6, ] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def lowercase__ ( self ): """simple docstring""" self._test_inference_batch_consistent(batch_sizes=[1, 2] ) def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =torch_device == '''cpu''' lowerCamelCase_ =True self._test_inference_batch_single_identical( batch_size=2, test_max_difference=lowerCAmelCase, relax_max_difference=lowerCAmelCase, ) def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =self.get_dummy_components() lowerCamelCase_ =self.pipeline_class(**lowerCAmelCase ) lowerCamelCase_ =pipe.to(lowerCAmelCase ) pipe.set_progress_bar_config(disable=lowerCAmelCase ) lowerCamelCase_ =1 lowerCamelCase_ =2 lowerCamelCase_ =self.get_dummy_inputs(lowerCAmelCase ) for key in inputs.keys(): if key in self.batch_params: lowerCamelCase_ =batch_size * [inputs[key]] lowerCamelCase_ =pipe(**lowerCAmelCase, num_images_per_prompt=lowerCAmelCase )[0] assert images.shape[0] == batch_size * num_images_per_prompt @slow @require_torch_gpu class __UpperCamelCase ( unittest.TestCase ): def lowercase__ ( self ): """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/shap_e/corgi.png''' ) lowerCamelCase_ =load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/shap_e/test_shap_e_img2img_out.npy''' ) lowerCamelCase_ =ShapEImgaImgPipeline.from_pretrained('''openai/shap-e-img2img''' ) lowerCamelCase_ =pipe.to(lowerCAmelCase ) pipe.set_progress_bar_config(disable=lowerCAmelCase ) lowerCamelCase_ =torch.Generator(device=lowerCAmelCase ).manual_seed(0 ) lowerCamelCase_ =pipe( lowerCAmelCase, generator=lowerCAmelCase, guidance_scale=3.0, num_inference_steps=64, frame_size=64, output_type='''np''', ).images[0] assert images.shape == (20, 64, 64, 3) assert_mean_pixel_difference(lowerCAmelCase, lowerCAmelCase )
6
0
"""simple docstring""" # Lint as: python3 import itertools import os import re _A = re.compile(r"""([A-Z]+)([A-Z][a-z])""") _A = re.compile(r"""([a-z\d])([A-Z])""") _A = re.compile(r"""(?<!_)_(?!_)""") _A = re.compile(r"""(_{2,})""") _A = r"""^\w+(\.\w+)*$""" _A = r"""<>:/\|?*""" def lowercase_ ( __UpperCAmelCase ) -> Any: lowerCAmelCase__ : Dict = _uppercase_uppercase_re.sub(R"""\1_\2""" , __UpperCAmelCase ) lowerCAmelCase__ : Optional[Any] = _lowercase_uppercase_re.sub(R"""\1_\2""" , __UpperCAmelCase ) return name.lower() def lowercase_ ( __UpperCAmelCase ) -> Optional[int]: lowerCAmelCase__ : Optional[Any] = _single_underscore_re.split(__UpperCAmelCase ) lowerCAmelCase__ : str = [_multiple_underscores_re.split(__UpperCAmelCase ) for n in name] return "".join(n.capitalize() for n in itertools.chain.from_iterable(__UpperCAmelCase ) if n != """""" ) def lowercase_ ( __UpperCAmelCase ) -> Optional[Any]: if os.path.basename(__UpperCAmelCase ) != name: raise ValueError(f"""Should be a dataset name, not a path: {name}""" ) return camelcase_to_snakecase(__UpperCAmelCase ) def lowercase_ ( __UpperCAmelCase , __UpperCAmelCase ) -> List[Any]: if os.path.basename(__UpperCAmelCase ) != name: raise ValueError(f"""Should be a dataset name, not a path: {name}""" ) if not re.match(_split_re , __UpperCAmelCase ): raise ValueError(f"""Split name should match '{_split_re}'' but got '{split}'.""" ) return f"""{filename_prefix_for_name(__UpperCAmelCase )}-{split}""" def lowercase_ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase=None ) -> Any: lowerCAmelCase__ : Dict = filename_prefix_for_split(__UpperCAmelCase , __UpperCAmelCase ) if filetype_suffix: prefix += f""".{filetype_suffix}""" lowerCAmelCase__ : List[Any] = os.path.join(__UpperCAmelCase , __UpperCAmelCase ) return f"""{filepath}*""" def lowercase_ ( __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase=None , __UpperCAmelCase=None ) -> Optional[Any]: lowerCAmelCase__ : Optional[int] = filename_prefix_for_split(__UpperCAmelCase , __UpperCAmelCase ) lowerCAmelCase__ : int = os.path.join(__UpperCAmelCase , __UpperCAmelCase ) if shard_lengths: lowerCAmelCase__ : Union[str, Any] = len(__UpperCAmelCase ) lowerCAmelCase__ : Tuple = [f"""{prefix}-{shard_id:05d}-of-{num_shards:05d}""" for shard_id in range(__UpperCAmelCase )] if filetype_suffix: lowerCAmelCase__ : Union[str, Any] = [filename + f""".{filetype_suffix}""" for filename in filenames] return filenames else: lowerCAmelCase__ : Tuple = prefix if filetype_suffix: filename += f""".{filetype_suffix}""" return [filename]
242
"""simple docstring""" # Copyright 2023 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import TYPE_CHECKING from ..models.auto import AutoModelForVisionaSeq from ..utils import requires_backends from .base import PipelineTool if TYPE_CHECKING: from PIL import Image class _lowerCamelCase ( a_ ): _lowerCamelCase :Any = "Salesforce/blip-image-captioning-base" _lowerCamelCase :int = ( "This is a tool that generates a description of an image. It takes an input named `image` which should be the " "image to caption, and returns a text that contains the description in English." ) _lowerCamelCase :List[Any] = "image_captioner" _lowerCamelCase :Tuple = AutoModelForVisionaSeq _lowerCamelCase :Dict = ["image"] _lowerCamelCase :str = ["text"] def __init__( self : Dict , *UpperCamelCase : Any , **UpperCamelCase : Any ) -> Any: """simple docstring""" requires_backends(self , ["""vision"""] ) super().__init__(*UpperCamelCase , **UpperCamelCase ) def _lowerCAmelCase ( self : Any , UpperCamelCase : "Image" ) -> Union[str, Any]: """simple docstring""" return self.pre_processor(images=UpperCamelCase , return_tensors="""pt""" ) def _lowerCAmelCase ( self : Optional[Any] , UpperCamelCase : str ) -> Tuple: """simple docstring""" return self.model.generate(**UpperCamelCase ) def _lowerCAmelCase ( self : Tuple , UpperCamelCase : int ) -> Tuple: """simple docstring""" return self.pre_processor.batch_decode(UpperCamelCase , skip_special_tokens=UpperCamelCase )[0].strip()
242
1
import glob import os import random from string import ascii_lowercase, digits import cva import numpy as np # Parrameters __UpperCAmelCase = (7_20, 12_80) # Height, Width __UpperCAmelCase = (0.4, 0.6) # if height or width lower than this scale, drop it. __UpperCAmelCase = 1 / 1_00 __UpperCAmelCase = '' __UpperCAmelCase = '' __UpperCAmelCase = '' __UpperCAmelCase = 2_50 def __UpperCamelCase ( ) -> None: '''simple docstring''' lowerCAmelCase_ : List[Any] = get_dataset(lowercase__ , lowercase__ ) for index in range(lowercase__ ): lowerCAmelCase_ : str = random.sample(range(len(lowercase__ ) ) , 4 ) lowerCAmelCase_ : List[str] = update_image_and_anno( lowercase__ , lowercase__ , lowercase__ , lowercase__ , lowercase__ , filter_scale=lowercase__ , ) # Get random string code: '7b7ad245cdff75241935e4dd860f3bad' lowerCAmelCase_ : Tuple = random_chars(32 ) lowerCAmelCase_ : Tuple = path.split(os.sep )[-1].rsplit(""".""" , 1 )[0] lowerCAmelCase_ : Dict = f'{OUTPUT_DIR}/{file_name}_MOSAIC_{letter_code}' cva.imwrite(f'{file_root}.jpg' , lowercase__ , [cva.IMWRITE_JPEG_QUALITY, 85] ) print(f'Succeeded {index+1}/{NUMBER_IMAGES} with {file_name}' ) lowerCAmelCase_ : List[Any] = [] for anno in new_annos: lowerCAmelCase_ : Union[str, Any] = anno[3] - anno[1] lowerCAmelCase_ : List[Any] = anno[4] - anno[2] lowerCAmelCase_ : Tuple = anno[1] + width / 2 lowerCAmelCase_ : Union[str, Any] = anno[2] + height / 2 lowerCAmelCase_ : Dict = f'{anno[0]} {x_center} {y_center} {width} {height}' annos_list.append(lowercase__ ) with open(f'{file_root}.txt' , """w""" ) as outfile: outfile.write("""\n""".join(line for line in annos_list ) ) def __UpperCamelCase ( lowercase__ : str , lowercase__ : str ) -> tuple[list, list]: '''simple docstring''' lowerCAmelCase_ : Dict = [] lowerCAmelCase_ : List[str] = [] for label_file in glob.glob(os.path.join(lowercase__ , """*.txt""" ) ): lowerCAmelCase_ : int = label_file.split(os.sep )[-1].rsplit(""".""" , 1 )[0] with open(lowercase__ ) as in_file: lowerCAmelCase_ : List[Any] = in_file.readlines() lowerCAmelCase_ : Dict = os.path.join(lowercase__ , f'{label_name}.jpg' ) lowerCAmelCase_ : Optional[int] = [] for obj_list in obj_lists: lowerCAmelCase_ : Tuple = obj_list.rstrip("""\n""" ).split(""" """ ) lowerCAmelCase_ : Any = float(obj[1] ) - float(obj[3] ) / 2 lowerCAmelCase_ : int = float(obj[2] ) - float(obj[4] ) / 2 lowerCAmelCase_ : List[Any] = float(obj[1] ) + float(obj[3] ) / 2 lowerCAmelCase_ : Any = float(obj[2] ) + float(obj[4] ) / 2 boxes.append([int(obj[0] ), xmin, ymin, xmax, ymax] ) if not boxes: continue img_paths.append(lowercase__ ) labels.append(lowercase__ ) return img_paths, labels def __UpperCamelCase ( lowercase__ : list , lowercase__ : list , lowercase__ : list[int] , lowercase__ : tuple[int, int] , lowercase__ : tuple[float, float] , lowercase__ : float = 0.0 , ) -> tuple[list, list, str]: '''simple docstring''' lowerCAmelCase_ : Optional[int] = np.zeros([output_size[0], output_size[1], 3] , dtype=np.uinta ) lowerCAmelCase_ : Dict = scale_range[0] + random.random() * (scale_range[1] - scale_range[0]) lowerCAmelCase_ : int = scale_range[0] + random.random() * (scale_range[1] - scale_range[0]) lowerCAmelCase_ : Union[str, Any] = int(scale_x * output_size[1] ) lowerCAmelCase_ : Optional[int] = int(scale_y * output_size[0] ) lowerCAmelCase_ : List[str] = [] lowerCAmelCase_ : Union[str, Any] = [] for i, index in enumerate(lowercase__ ): lowerCAmelCase_ : Any = all_img_list[index] path_list.append(lowercase__ ) lowerCAmelCase_ : Union[str, Any] = all_annos[index] lowerCAmelCase_ : List[str] = cva.imread(lowercase__ ) if i == 0: # top-left lowerCAmelCase_ : int = cva.resize(lowercase__ , (divid_point_x, divid_point_y) ) lowerCAmelCase_ : Union[str, Any] = img for bbox in img_annos: lowerCAmelCase_ : Optional[int] = bbox[1] * scale_x lowerCAmelCase_ : Optional[int] = bbox[2] * scale_y lowerCAmelCase_ : int = bbox[3] * scale_x lowerCAmelCase_ : Union[str, Any] = bbox[4] * scale_y new_anno.append([bbox[0], xmin, ymin, xmax, ymax] ) elif i == 1: # top-right lowerCAmelCase_ : Union[str, Any] = cva.resize(lowercase__ , (output_size[1] - divid_point_x, divid_point_y) ) lowerCAmelCase_ : str = img for bbox in img_annos: lowerCAmelCase_ : str = scale_x + bbox[1] * (1 - scale_x) lowerCAmelCase_ : Tuple = bbox[2] * scale_y lowerCAmelCase_ : List[Any] = scale_x + bbox[3] * (1 - scale_x) lowerCAmelCase_ : List[str] = bbox[4] * scale_y new_anno.append([bbox[0], xmin, ymin, xmax, ymax] ) elif i == 2: # bottom-left lowerCAmelCase_ : str = cva.resize(lowercase__ , (divid_point_x, output_size[0] - divid_point_y) ) lowerCAmelCase_ : List[str] = img for bbox in img_annos: lowerCAmelCase_ : Any = bbox[1] * scale_x lowerCAmelCase_ : Union[str, Any] = scale_y + bbox[2] * (1 - scale_y) lowerCAmelCase_ : List[str] = bbox[3] * scale_x lowerCAmelCase_ : int = scale_y + bbox[4] * (1 - scale_y) new_anno.append([bbox[0], xmin, ymin, xmax, ymax] ) else: # bottom-right lowerCAmelCase_ : Dict = cva.resize( lowercase__ , (output_size[1] - divid_point_x, output_size[0] - divid_point_y) ) lowerCAmelCase_ : List[str] = img for bbox in img_annos: lowerCAmelCase_ : Any = scale_x + bbox[1] * (1 - scale_x) lowerCAmelCase_ : Optional[int] = scale_y + bbox[2] * (1 - scale_y) lowerCAmelCase_ : Optional[Any] = scale_x + bbox[3] * (1 - scale_x) lowerCAmelCase_ : int = scale_y + bbox[4] * (1 - scale_y) new_anno.append([bbox[0], xmin, ymin, xmax, ymax] ) # Remove bounding box small than scale of filter if filter_scale > 0: lowerCAmelCase_ : Any = [ anno for anno in new_anno if filter_scale < (anno[3] - anno[1]) and filter_scale < (anno[4] - anno[2]) ] return output_img, new_anno, path_list[0] def __UpperCamelCase ( lowercase__ : int ) -> str: '''simple docstring''' assert number_char > 1, "The number of character should greater than 1" lowerCAmelCase_ : int = ascii_lowercase + digits return "".join(random.choice(lowercase__ ) for _ in range(lowercase__ ) ) if __name__ == "__main__": main() print('DONE ✅')
352
import tempfile import unittest import numpy as np import transformers from transformers import GPTaTokenizer, GPTJConfig, is_flax_available, is_torch_available from transformers.testing_utils import is_pt_flax_cross_test, require_flax, tooslow from ...generation.test_flax_utils import FlaxGenerationTesterMixin from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax import jax.numpy as jnp from transformers.modeling_flax_pytorch_utils import ( convert_pytorch_state_dict_to_flax, load_flax_weights_in_pytorch_model, ) from transformers.models.gptj.modeling_flax_gptj import FlaxGPTJForCausalLM, FlaxGPTJModel if is_torch_available(): import torch class __a : def __init__( self : Union[str, Any] , UpperCAmelCase : int , UpperCAmelCase : List[Any]=14 , UpperCAmelCase : str=7 , UpperCAmelCase : str=True , UpperCAmelCase : int=True , UpperCAmelCase : List[Any]=False , UpperCAmelCase : Any=True , UpperCAmelCase : Any=99 , UpperCAmelCase : Any=32 , UpperCAmelCase : Any=4 , UpperCAmelCase : int=4 , UpperCAmelCase : str=4 , UpperCAmelCase : Tuple=37 , UpperCAmelCase : Dict="gelu" , UpperCAmelCase : Optional[int]=0.1 , UpperCAmelCase : Union[str, Any]=0.1 , UpperCAmelCase : Optional[Any]=5_12 , UpperCAmelCase : List[str]=0.02 , ): lowerCAmelCase_ : List[Any] = parent lowerCAmelCase_ : Union[str, Any] = batch_size lowerCAmelCase_ : Dict = seq_length lowerCAmelCase_ : Optional[Any] = is_training lowerCAmelCase_ : Optional[int] = use_input_mask lowerCAmelCase_ : Optional[Any] = use_token_type_ids lowerCAmelCase_ : Optional[Any] = use_labels lowerCAmelCase_ : Any = vocab_size lowerCAmelCase_ : Tuple = hidden_size lowerCAmelCase_ : Any = rotary_dim lowerCAmelCase_ : str = num_hidden_layers lowerCAmelCase_ : int = num_attention_heads lowerCAmelCase_ : Any = intermediate_size lowerCAmelCase_ : Dict = hidden_act lowerCAmelCase_ : Optional[Any] = hidden_dropout_prob lowerCAmelCase_ : Optional[int] = attention_probs_dropout_prob lowerCAmelCase_ : Optional[Any] = max_position_embeddings lowerCAmelCase_ : Union[str, Any] = initializer_range lowerCAmelCase_ : int = None lowerCAmelCase_ : Union[str, Any] = vocab_size - 1 lowerCAmelCase_ : str = vocab_size - 1 lowerCAmelCase_ : Optional[int] = vocab_size - 1 def A ( self : List[Any] ): lowerCAmelCase_ : str = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) lowerCAmelCase_ : Optional[int] = None if self.use_input_mask: lowerCAmelCase_ : Union[str, Any] = random_attention_mask([self.batch_size, self.seq_length] ) lowerCAmelCase_ : Optional[int] = GPTJConfig( vocab_size=self.vocab_size , n_embd=self.hidden_size , n_layer=self.num_hidden_layers , n_head=self.num_attention_heads , n_positions=self.max_position_embeddings , use_cache=UpperCAmelCase , bos_token_id=self.bos_token_id , eos_token_id=self.eos_token_id , pad_token_id=self.pad_token_id , rotary_dim=self.rotary_dim , ) return (config, input_ids, input_mask) def A ( self : str ): lowerCAmelCase_ : Optional[int] = self.prepare_config_and_inputs() lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ : List[str] = config_and_inputs lowerCAmelCase_ : int = {"""input_ids""": input_ids, """attention_mask""": attention_mask} return config, inputs_dict def A ( self : Dict , UpperCAmelCase : Union[str, Any] , UpperCAmelCase : Optional[int] , UpperCAmelCase : int , UpperCAmelCase : Tuple ): lowerCAmelCase_ : str = 20 lowerCAmelCase_ : Dict = model_class_name(UpperCAmelCase ) lowerCAmelCase_ : Optional[int] = model.init_cache(input_ids.shape[0] , UpperCAmelCase ) lowerCAmelCase_ : Dict = jnp.ones((input_ids.shape[0], max_decoder_length) , dtype="""i4""" ) lowerCAmelCase_ : Tuple = jnp.broadcast_to( jnp.arange(input_ids.shape[-1] - 1 )[None, :] , (input_ids.shape[0], input_ids.shape[-1] - 1) ) lowerCAmelCase_ : Dict = model( input_ids[:, :-1] , attention_mask=UpperCAmelCase , past_key_values=UpperCAmelCase , position_ids=UpperCAmelCase , ) lowerCAmelCase_ : Union[str, Any] = jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]] , dtype="""i4""" ) lowerCAmelCase_ : List[str] = model( input_ids[:, -1:] , attention_mask=UpperCAmelCase , past_key_values=outputs_cache.past_key_values , position_ids=UpperCAmelCase , ) lowerCAmelCase_ : Any = model(UpperCAmelCase ) lowerCAmelCase_ : Tuple = np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) ) self.parent.assertTrue(diff < 1e-3 , msg=F'Max diff is {diff}' ) def A ( self : Optional[Any] , UpperCAmelCase : int , UpperCAmelCase : Union[str, Any] , UpperCAmelCase : Dict , UpperCAmelCase : Any ): lowerCAmelCase_ : int = 20 lowerCAmelCase_ : List[Any] = model_class_name(UpperCAmelCase ) lowerCAmelCase_ : Tuple = jnp.concatenate( [attention_mask, jnp.zeros((attention_mask.shape[0], max_decoder_length - attention_mask.shape[1]) )] , axis=-1 , ) lowerCAmelCase_ : Optional[int] = model.init_cache(input_ids.shape[0] , UpperCAmelCase ) lowerCAmelCase_ : Dict = jnp.broadcast_to( jnp.arange(input_ids.shape[-1] - 1 )[None, :] , (input_ids.shape[0], input_ids.shape[-1] - 1) ) lowerCAmelCase_ : Tuple = model( input_ids[:, :-1] , attention_mask=UpperCAmelCase , past_key_values=UpperCAmelCase , position_ids=UpperCAmelCase , ) lowerCAmelCase_ : List[str] = jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]] , dtype="""i4""" ) lowerCAmelCase_ : Tuple = model( input_ids[:, -1:] , past_key_values=outputs_cache.past_key_values , attention_mask=UpperCAmelCase , position_ids=UpperCAmelCase , ) lowerCAmelCase_ : Union[str, Any] = model(UpperCAmelCase , attention_mask=UpperCAmelCase ) lowerCAmelCase_ : str = np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) ) self.parent.assertTrue(diff < 1e-3 , msg=F'Max diff is {diff}' ) @require_flax class __a ( __UpperCamelCase ,__UpperCamelCase ,unittest.TestCase ): __snake_case : Union[str, Any] = (FlaxGPTJModel, FlaxGPTJForCausalLM) if is_flax_available() else () __snake_case : Any = (FlaxGPTJForCausalLM,) if is_flax_available() else () def A ( self : Any ): lowerCAmelCase_ : List[str] = FlaxGPTJModelTester(self ) def A ( self : Union[str, Any] ): for model_class_name in self.all_model_classes: lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ : str = self.model_tester.prepare_config_and_inputs() self.model_tester.check_use_cache_forward(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) def A ( self : Tuple ): for model_class_name in self.all_model_classes: lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.check_use_cache_forward_with_attn_mask( UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) @tooslow def A ( self : int ): lowerCAmelCase_ : Optional[int] = GPTaTokenizer.from_pretrained("""gpt2""" , pad_token="""<|endoftext|>""" , padding_side="""left""" ) lowerCAmelCase_ : Tuple = tokenizer(["""Hello this is a long string""", """Hey"""] , return_tensors="""np""" , padding=UpperCAmelCase , truncation=UpperCAmelCase ) lowerCAmelCase_ : Optional[Any] = FlaxGPTJForCausalLM.from_pretrained("""EleutherAI/gpt-j-6B""" ) lowerCAmelCase_ : List[str] = False lowerCAmelCase_ : Optional[Any] = model.config.eos_token_id lowerCAmelCase_ : List[Any] = jax.jit(model.generate ) lowerCAmelCase_ : Any = jit_generate( inputs["""input_ids"""] , attention_mask=inputs["""attention_mask"""] , pad_token_id=tokenizer.pad_token_id ).sequences lowerCAmelCase_ : str = tokenizer.batch_decode(UpperCAmelCase , skip_special_tokens=UpperCAmelCase ) lowerCAmelCase_ : Optional[int] = [ """Hello this is a long string of text.\n\nI'm trying to get the text of the""", """Hey, I'm a little late to the party. I'm going to""", ] self.assertListEqual(UpperCAmelCase , UpperCAmelCase ) @is_pt_flax_cross_test def A ( self : Optional[Any] ): lowerCAmelCase_ , lowerCAmelCase_ : int = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): # prepare inputs lowerCAmelCase_ : int = self._prepare_for_class(UpperCAmelCase , UpperCAmelCase ) lowerCAmelCase_ : List[Any] = {k: torch.tensor(v.tolist() ) for k, v in prepared_inputs_dict.items()} # load corresponding PyTorch class lowerCAmelCase_ : List[str] = model_class.__name__[4:] # Skip the "Flax" at the beginning lowerCAmelCase_ : Dict = getattr(UpperCAmelCase , UpperCAmelCase ) lowerCAmelCase_ , lowerCAmelCase_ : Optional[Any] = pt_inputs["""input_ids"""].shape lowerCAmelCase_ : str = np.random.randint(0 , seq_length - 1 , size=(batch_size,) ) for batch_idx, start_index in enumerate(UpperCAmelCase ): lowerCAmelCase_ : Optional[Any] = 0 lowerCAmelCase_ : Any = 1 lowerCAmelCase_ : Tuple = 0 lowerCAmelCase_ : List[Any] = 1 lowerCAmelCase_ : Tuple = pt_model_class(UpperCAmelCase ).eval() lowerCAmelCase_ : List[str] = model_class(UpperCAmelCase , dtype=jnp.floataa ) lowerCAmelCase_ : List[str] = convert_pytorch_state_dict_to_flax(pt_model.state_dict() , UpperCAmelCase ) lowerCAmelCase_ : List[str] = fx_state with torch.no_grad(): lowerCAmelCase_ : List[str] = pt_model(**UpperCAmelCase ).to_tuple() lowerCAmelCase_ : int = fx_model(**UpperCAmelCase ).to_tuple() self.assertEqual(len(UpperCAmelCase ) , len(UpperCAmelCase ) , """Output lengths differ between Flax and PyTorch""" ) for fx_output, pt_output in zip(UpperCAmelCase , UpperCAmelCase ): self.assert_almost_equals(fx_output[:, -1] , pt_output[:, -1].numpy() , 4e-2 ) with tempfile.TemporaryDirectory() as tmpdirname: pt_model.save_pretrained(UpperCAmelCase ) lowerCAmelCase_ : Optional[int] = model_class.from_pretrained(UpperCAmelCase , from_pt=UpperCAmelCase ) lowerCAmelCase_ : Union[str, Any] = fx_model_loaded(**UpperCAmelCase ).to_tuple() self.assertEqual( len(UpperCAmelCase ) , len(UpperCAmelCase ) , """Output lengths differ between Flax and PyTorch""" ) for fx_output_loaded, pt_output in zip(UpperCAmelCase , UpperCAmelCase ): self.assert_almost_equals(fx_output_loaded[:, -1] , pt_output[:, -1].numpy() , 4e-2 ) @is_pt_flax_cross_test def A ( self : Optional[Any] ): lowerCAmelCase_ , lowerCAmelCase_ : Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): # prepare inputs lowerCAmelCase_ : str = self._prepare_for_class(UpperCAmelCase , UpperCAmelCase ) lowerCAmelCase_ : int = {k: torch.tensor(v.tolist() ) for k, v in prepared_inputs_dict.items()} # load corresponding PyTorch class lowerCAmelCase_ : Optional[int] = model_class.__name__[4:] # Skip the "Flax" at the beginning lowerCAmelCase_ : Any = getattr(UpperCAmelCase , UpperCAmelCase ) lowerCAmelCase_ : str = pt_model_class(UpperCAmelCase ).eval() lowerCAmelCase_ : Any = model_class(UpperCAmelCase , dtype=jnp.floataa ) lowerCAmelCase_ : Union[str, Any] = load_flax_weights_in_pytorch_model(UpperCAmelCase , fx_model.params ) lowerCAmelCase_ , lowerCAmelCase_ : List[Any] = pt_inputs["""input_ids"""].shape lowerCAmelCase_ : str = np.random.randint(0 , seq_length - 1 , size=(batch_size,) ) for batch_idx, start_index in enumerate(UpperCAmelCase ): lowerCAmelCase_ : Any = 0 lowerCAmelCase_ : Optional[int] = 1 lowerCAmelCase_ : Tuple = 0 lowerCAmelCase_ : str = 1 # make sure weights are tied in PyTorch pt_model.tie_weights() with torch.no_grad(): lowerCAmelCase_ : List[str] = pt_model(**UpperCAmelCase ).to_tuple() lowerCAmelCase_ : Tuple = fx_model(**UpperCAmelCase ).to_tuple() self.assertEqual(len(UpperCAmelCase ) , len(UpperCAmelCase ) , """Output lengths differ between Flax and PyTorch""" ) for fx_output, pt_output in zip(UpperCAmelCase , UpperCAmelCase ): self.assert_almost_equals(fx_output[:, -1] , pt_output[:, -1].numpy() , 4e-2 ) with tempfile.TemporaryDirectory() as tmpdirname: fx_model.save_pretrained(UpperCAmelCase ) lowerCAmelCase_ : Optional[Any] = pt_model_class.from_pretrained(UpperCAmelCase , from_flax=UpperCAmelCase ) with torch.no_grad(): lowerCAmelCase_ : Dict = pt_model_loaded(**UpperCAmelCase ).to_tuple() self.assertEqual( len(UpperCAmelCase ) , len(UpperCAmelCase ) , """Output lengths differ between Flax and PyTorch""" ) for fx_output, pt_output in zip(UpperCAmelCase , UpperCAmelCase ): self.assert_almost_equals(fx_output[:, -1] , pt_output[:, -1].numpy() , 4e-2 ) @tooslow def A ( self : str ): for model_class_name in self.all_model_classes: lowerCAmelCase_ : Optional[Any] = model_class_name.from_pretrained("""EleutherAI/gpt-j-6B""" ) lowerCAmelCase_ : Optional[Any] = model(np.ones((1, 1) ) ) self.assertIsNotNone(UpperCAmelCase )
28
0
"""simple docstring""" from __future__ import annotations from typing import Generic, TypeVar lowercase_ = TypeVar('T') class snake_case ( Generic[T] ): '''simple docstring''' def __init__( self : Optional[int], _lowerCamelCase : T ): '''simple docstring''' __A = data __A = self __A = 0 class snake_case ( Generic[T] ): '''simple docstring''' def __init__( self : Dict ): '''simple docstring''' # map from node name to the node object __A = {} def _SCREAMING_SNAKE_CASE ( self : Dict, _lowerCamelCase : T ): '''simple docstring''' # create a new set with x as its member __A = DisjointSetTreeNode(_lowerCamelCase ) def _SCREAMING_SNAKE_CASE ( self : Optional[int], _lowerCamelCase : T ): '''simple docstring''' # find the set x belongs to (with path-compression) __A = self.map[data] if elem_ref != elem_ref.parent: __A = self.find_set(elem_ref.parent.data ) return elem_ref.parent def _SCREAMING_SNAKE_CASE ( self : Any, _lowerCamelCase : DisjointSetTreeNode[T], _lowerCamelCase : DisjointSetTreeNode[T] ): '''simple docstring''' # helper function for union operation if nodea.rank > nodea.rank: __A = nodea else: __A = nodea if nodea.rank == nodea.rank: nodea.rank += 1 def _SCREAMING_SNAKE_CASE ( self : Optional[int], _lowerCamelCase : T, _lowerCamelCase : T ): '''simple docstring''' # merge 2 disjoint sets self.link(self.find_set(_lowerCamelCase ), self.find_set(_lowerCamelCase ) ) class snake_case ( Generic[T] ): '''simple docstring''' def __init__( self : Dict ): '''simple docstring''' # connections: map from the node to the neighbouring nodes (with weights) __A = {} def _SCREAMING_SNAKE_CASE ( self : str, _lowerCamelCase : T ): '''simple docstring''' # add a node ONLY if its not present in the graph if node not in self.connections: __A = {} def _SCREAMING_SNAKE_CASE ( self : int, _lowerCamelCase : T, _lowerCamelCase : T, _lowerCamelCase : int ): '''simple docstring''' # add an edge with the given weight self.add_node(_lowerCamelCase ) self.add_node(_lowerCamelCase ) __A = weight __A = weight def _SCREAMING_SNAKE_CASE ( self : Any ): '''simple docstring''' __A = [] __A = set() for start in self.connections: for end in self.connections[start]: if (start, end) not in seen: seen.add((end, start) ) edges.append((start, end, self.connections[start][end]) ) edges.sort(key=lambda _lowerCamelCase : x[2] ) # creating the disjoint set __A = DisjointSetTree[T]() for node in self.connections: disjoint_set.make_set(_lowerCamelCase ) # MST generation __A = 0 __A = 0 __A = GraphUndirectedWeighted[T]() while num_edges < len(self.connections ) - 1: __A , __A , __A = edges[index] index += 1 __A = disjoint_set.find_set(_lowerCamelCase ) __A = disjoint_set.find_set(_lowerCamelCase ) if parent_u != parent_v: num_edges += 1 graph.add_edge(_lowerCamelCase, _lowerCamelCase, _lowerCamelCase ) disjoint_set.union(_lowerCamelCase, _lowerCamelCase ) return graph
266
"""simple docstring""" import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class snake_case ( unittest.TestCase ): '''simple docstring''' def __init__( self : List[str], _lowerCamelCase : Optional[Any], _lowerCamelCase : Union[str, Any]=13, _lowerCamelCase : Any=3, _lowerCamelCase : Optional[int]=2_24, _lowerCamelCase : str=30, _lowerCamelCase : Dict=4_00, _lowerCamelCase : Union[str, Any]=True, _lowerCamelCase : Any=None, _lowerCamelCase : Optional[Any]=True, _lowerCamelCase : Any=[0.5, 0.5, 0.5], _lowerCamelCase : List[str]=[0.5, 0.5, 0.5], ): '''simple docstring''' __A = size if size is not None else {'''height''': 18, '''width''': 18} __A = parent __A = batch_size __A = num_channels __A = image_size __A = min_resolution __A = max_resolution __A = do_resize __A = size __A = do_normalize __A = image_mean __A = image_std def _SCREAMING_SNAKE_CASE ( self : int ): '''simple docstring''' return { "image_mean": self.image_mean, "image_std": self.image_std, "do_normalize": self.do_normalize, "do_resize": self.do_resize, "size": self.size, } @require_torch @require_vision class snake_case ( _lowerCAmelCase , unittest.TestCase ): '''simple docstring''' A_ : str = ViTImageProcessor if is_vision_available() else None def _SCREAMING_SNAKE_CASE ( self : Optional[int] ): '''simple docstring''' __A = EfficientFormerImageProcessorTester(self ) @property def _SCREAMING_SNAKE_CASE ( self : Dict ): '''simple docstring''' return self.image_proc_tester.prepare_image_processor_dict() def _SCREAMING_SNAKE_CASE ( self : Optional[Any] ): '''simple docstring''' __A = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_lowerCamelCase, '''image_mean''' ) ) self.assertTrue(hasattr(_lowerCamelCase, '''image_std''' ) ) self.assertTrue(hasattr(_lowerCamelCase, '''do_normalize''' ) ) self.assertTrue(hasattr(_lowerCamelCase, '''do_resize''' ) ) self.assertTrue(hasattr(_lowerCamelCase, '''size''' ) ) def _SCREAMING_SNAKE_CASE ( self : Union[str, Any] ): '''simple docstring''' pass def _SCREAMING_SNAKE_CASE ( self : List[Any] ): '''simple docstring''' # Initialize image_processor __A = self.image_processing_class(**self.image_processor_dict ) # create random PIL images __A = prepare_image_inputs(self.image_proc_tester, equal_resolution=_lowerCamelCase ) for image in image_inputs: self.assertIsInstance(_lowerCamelCase, Image.Image ) # Test not batched input __A = image_processor(image_inputs[0], return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape, ( 1, self.image_proc_tester.num_channels, self.image_proc_tester.size['''height'''], self.image_proc_tester.size['''width'''], ), ) # Test batched __A = image_processor(_lowerCamelCase, return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape, ( self.image_proc_tester.batch_size, self.image_proc_tester.num_channels, self.image_proc_tester.size['''height'''], self.image_proc_tester.size['''width'''], ), ) def _SCREAMING_SNAKE_CASE ( self : Tuple ): '''simple docstring''' # Initialize image_processor __A = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors __A = prepare_image_inputs(self.image_proc_tester, equal_resolution=_lowerCamelCase, numpify=_lowerCamelCase ) for image in image_inputs: self.assertIsInstance(_lowerCamelCase, np.ndarray ) # Test not batched input __A = image_processor(image_inputs[0], return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape, ( 1, self.image_proc_tester.num_channels, self.image_proc_tester.size['''height'''], self.image_proc_tester.size['''width'''], ), ) # Test batched __A = image_processor(_lowerCamelCase, return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape, ( self.image_proc_tester.batch_size, self.image_proc_tester.num_channels, self.image_proc_tester.size['''height'''], self.image_proc_tester.size['''width'''], ), ) def _SCREAMING_SNAKE_CASE ( self : str ): '''simple docstring''' # Initialize image_processor __A = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors __A = prepare_image_inputs(self.image_proc_tester, equal_resolution=_lowerCamelCase, torchify=_lowerCamelCase ) for image in image_inputs: self.assertIsInstance(_lowerCamelCase, torch.Tensor ) # Test not batched input __A = image_processor(image_inputs[0], return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape, ( 1, self.image_proc_tester.num_channels, self.image_proc_tester.size['''height'''], self.image_proc_tester.size['''width'''], ), ) # Test batched __A = image_processor(_lowerCamelCase, return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape, ( self.image_proc_tester.batch_size, self.image_proc_tester.num_channels, self.image_proc_tester.size['''height'''], self.image_proc_tester.size['''width'''], ), )
266
1
"""simple docstring""" 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 _A = logging.get_logger(__name__) _A = '▁' _A = {'vocab_file': 'sentencepiece.bpe.model'} _A = { 'vocab_file': { 'xlm-roberta-base': 'https://huggingface.co/xlm-roberta-base/resolve/main/sentencepiece.bpe.model', 'xlm-roberta-large': 'https://huggingface.co/xlm-roberta-large/resolve/main/sentencepiece.bpe.model', 'xlm-roberta-large-finetuned-conll02-dutch': ( 'https://huggingface.co/xlm-roberta-large-finetuned-conll02-dutch/resolve/main/sentencepiece.bpe.model' ), 'xlm-roberta-large-finetuned-conll02-spanish': ( 'https://huggingface.co/xlm-roberta-large-finetuned-conll02-spanish/resolve/main/sentencepiece.bpe.model' ), 'xlm-roberta-large-finetuned-conll03-english': ( 'https://huggingface.co/xlm-roberta-large-finetuned-conll03-english/resolve/main/sentencepiece.bpe.model' ), 'xlm-roberta-large-finetuned-conll03-german': ( 'https://huggingface.co/xlm-roberta-large-finetuned-conll03-german/resolve/main/sentencepiece.bpe.model' ), } } _A = { 'xlm-roberta-base': 5_12, 'xlm-roberta-large': 5_12, 'xlm-roberta-large-finetuned-conll02-dutch': 5_12, 'xlm-roberta-large-finetuned-conll02-spanish': 5_12, 'xlm-roberta-large-finetuned-conll03-english': 5_12, 'xlm-roberta-large-finetuned-conll03-german': 5_12, } class lowerCamelCase ( lowerCAmelCase__ ): '''simple docstring''' SCREAMING_SNAKE_CASE = VOCAB_FILES_NAMES SCREAMING_SNAKE_CASE = PRETRAINED_VOCAB_FILES_MAP SCREAMING_SNAKE_CASE = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES SCREAMING_SNAKE_CASE = ['input_ids', 'attention_mask'] def __init__(self , _lowerCamelCase , _lowerCamelCase="<s>" , _lowerCamelCase="</s>" , _lowerCamelCase="</s>" , _lowerCamelCase="<s>" , _lowerCamelCase="<unk>" , _lowerCamelCase="<pad>" , _lowerCamelCase="<mask>" , _lowerCamelCase = None , **_lowerCamelCase , ): """simple docstring""" UpperCAmelCase__ : Any = AddedToken(_snake_case , lstrip=_snake_case , rstrip=_snake_case ) if isinstance(_snake_case , _snake_case ) else mask_token UpperCAmelCase__ : Dict = {} 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 , ) UpperCAmelCase__ : Union[str, Any] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(_snake_case ) ) UpperCAmelCase__ : List[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' # Mimic fairseq token-to-id alignment for the first 4 token UpperCAmelCase__ : Optional[int] = {"""<s>""": 0, """<pad>""": 1, """</s>""": 2, """<unk>""": 3} # The first "real" token "," has position 4 in the original fairseq vocab and position 3 in the spm vocab UpperCAmelCase__ : Dict = 1 UpperCAmelCase__ : List[Any] = len(self.sp_model ) + self.fairseq_offset UpperCAmelCase__ : Union[str, Any] = {v: k for k, v in self.fairseq_tokens_to_ids.items()} def __getstate__(self ): """simple docstring""" UpperCAmelCase__ : int = self.__dict__.copy() UpperCAmelCase__ : Dict = None UpperCAmelCase__ : Any = self.sp_model.serialized_model_proto() return state def __setstate__(self , _lowerCamelCase ): """simple docstring""" UpperCAmelCase__ : str = d # for backward compatibility if not hasattr(self , """sp_model_kwargs""" ): UpperCAmelCase__ : List[str] = {} UpperCAmelCase__ : List[Any] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.LoadFromSerializedProto(self.sp_model_proto ) def _a (self , _lowerCamelCase , _lowerCamelCase = None ): """simple docstring""" if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] UpperCAmelCase__ : Any = [self.cls_token_id] UpperCAmelCase__ : Tuple = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def _a (self , _lowerCamelCase , _lowerCamelCase = None , _lowerCamelCase = False ): """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 , _lowerCamelCase , _lowerCamelCase = None ): """simple docstring""" UpperCAmelCase__ : Optional[int] = [self.sep_token_id] UpperCAmelCase__ : 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 ): """simple docstring""" return len(self.sp_model ) + self.fairseq_offset + 1 # Add the <mask> token def _a (self ): """simple docstring""" UpperCAmelCase__ : str = {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 , _lowerCamelCase ): """simple docstring""" return self.sp_model.encode(_snake_case , out_type=_snake_case ) def _a (self , _lowerCamelCase ): """simple docstring""" if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] UpperCAmelCase__ : List[Any] = self.sp_model.PieceToId(_snake_case ) # 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 , _lowerCamelCase ): """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 , _lowerCamelCase ): """simple docstring""" UpperCAmelCase__ : str = """""".join(_snake_case ).replace(_snake_case , """ """ ).strip() return out_string def _a (self , _lowerCamelCase , _lowerCamelCase = None ): """simple docstring""" if not os.path.isdir(_snake_case ): logger.error(F"""Vocabulary path ({save_directory}) should be a directory""" ) return UpperCAmelCase__ : Union[str, Any] = 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: UpperCAmelCase__ : str = self.sp_model.serialized_model_proto() fi.write(_snake_case ) return (out_vocab_file,)
361
"""simple docstring""" import argparse from collections import OrderedDict from pathlib import Path import requests import torch from PIL import Image from transformers import GLPNConfig, GLPNForDepthEstimation, GLPNImageProcessor from transformers.utils import logging logging.set_verbosity_info() _A = logging.get_logger(__name__) def a__ ( lowerCAmelCase ) -> Tuple: UpperCAmelCase__ : Optional[int] = OrderedDict() for key, value in state_dict.items(): if key.startswith("""module.encoder""" ): UpperCAmelCase__ : Dict = key.replace("""module.encoder""" , """glpn.encoder""" ) if key.startswith("""module.decoder""" ): UpperCAmelCase__ : int = key.replace("""module.decoder""" , """decoder.stages""" ) if "patch_embed" in key: # replace for example patch_embed1 by patch_embeddings.0 UpperCAmelCase__ : Optional[int] = key[key.find("""patch_embed""" ) + len("""patch_embed""" )] UpperCAmelCase__ : Union[str, Any] = key.replace(F"""patch_embed{idx}""" , F"""patch_embeddings.{int(lowerCAmelCase )-1}""" ) if "norm" in key: UpperCAmelCase__ : Optional[Any] = key.replace("""norm""" , """layer_norm""" ) if "glpn.encoder.layer_norm" in key: # replace for example layer_norm1 by layer_norm.0 UpperCAmelCase__ : int = key[key.find("""glpn.encoder.layer_norm""" ) + len("""glpn.encoder.layer_norm""" )] UpperCAmelCase__ : Union[str, Any] = key.replace(F"""layer_norm{idx}""" , F"""layer_norm.{int(lowerCAmelCase )-1}""" ) if "layer_norm1" in key: UpperCAmelCase__ : Any = key.replace("""layer_norm1""" , """layer_norm_1""" ) if "layer_norm2" in key: UpperCAmelCase__ : Union[str, Any] = key.replace("""layer_norm2""" , """layer_norm_2""" ) if "block" in key: # replace for example block1 by block.0 UpperCAmelCase__ : int = key[key.find("""block""" ) + len("""block""" )] UpperCAmelCase__ : List[Any] = key.replace(F"""block{idx}""" , F"""block.{int(lowerCAmelCase )-1}""" ) if "attn.q" in key: UpperCAmelCase__ : List[Any] = key.replace("""attn.q""" , """attention.self.query""" ) if "attn.proj" in key: UpperCAmelCase__ : Tuple = key.replace("""attn.proj""" , """attention.output.dense""" ) if "attn" in key: UpperCAmelCase__ : Union[str, Any] = key.replace("""attn""" , """attention.self""" ) if "fc1" in key: UpperCAmelCase__ : int = key.replace("""fc1""" , """dense1""" ) if "fc2" in key: UpperCAmelCase__ : List[Any] = key.replace("""fc2""" , """dense2""" ) if "linear_pred" in key: UpperCAmelCase__ : Optional[Any] = key.replace("""linear_pred""" , """classifier""" ) if "linear_fuse" in key: UpperCAmelCase__ : Optional[Any] = key.replace("""linear_fuse.conv""" , """linear_fuse""" ) UpperCAmelCase__ : Optional[Any] = key.replace("""linear_fuse.bn""" , """batch_norm""" ) if "linear_c" in key: # replace for example linear_c4 by linear_c.3 UpperCAmelCase__ : List[Any] = key[key.find("""linear_c""" ) + len("""linear_c""" )] UpperCAmelCase__ : int = key.replace(F"""linear_c{idx}""" , F"""linear_c.{int(lowerCAmelCase )-1}""" ) if "bot_conv" in key: UpperCAmelCase__ : int = key.replace("""bot_conv""" , """0.convolution""" ) if "skip_conv1" in key: UpperCAmelCase__ : List[Any] = key.replace("""skip_conv1""" , """1.convolution""" ) if "skip_conv2" in key: UpperCAmelCase__ : List[Any] = key.replace("""skip_conv2""" , """2.convolution""" ) if "fusion1" in key: UpperCAmelCase__ : Optional[Any] = key.replace("""fusion1""" , """1.fusion""" ) if "fusion2" in key: UpperCAmelCase__ : List[str] = key.replace("""fusion2""" , """2.fusion""" ) if "fusion3" in key: UpperCAmelCase__ : int = key.replace("""fusion3""" , """3.fusion""" ) if "fusion" in key and "conv" in key: UpperCAmelCase__ : Union[str, Any] = key.replace("""conv""" , """convolutional_layer""" ) if key.startswith("""module.last_layer_depth""" ): UpperCAmelCase__ : Optional[int] = key.replace("""module.last_layer_depth""" , """head.head""" ) UpperCAmelCase__ : Optional[Any] = value return new_state_dict def a__ ( lowerCAmelCase , lowerCAmelCase ) -> Dict: # for each of the encoder blocks: for i in range(config.num_encoder_blocks ): for j in range(config.depths[i] ): # read in weights + bias of keys and values (which is a single matrix in the original implementation) UpperCAmelCase__ : Dict = state_dict.pop(F"""glpn.encoder.block.{i}.{j}.attention.self.kv.weight""" ) UpperCAmelCase__ : int = state_dict.pop(F"""glpn.encoder.block.{i}.{j}.attention.self.kv.bias""" ) # next, add keys and values (in that order) to the state dict UpperCAmelCase__ : Optional[int] = kv_weight[ : config.hidden_sizes[i], : ] UpperCAmelCase__ : int = kv_bias[: config.hidden_sizes[i]] UpperCAmelCase__ : int = kv_weight[ config.hidden_sizes[i] :, : ] UpperCAmelCase__ : List[Any] = kv_bias[config.hidden_sizes[i] :] def a__ ( ) -> int: UpperCAmelCase__ : Optional[Any] = """http://images.cocodataset.org/val2017/000000039769.jpg""" UpperCAmelCase__ : int = Image.open(requests.get(lowerCAmelCase , stream=lowerCAmelCase ).raw ) return image @torch.no_grad() def a__ ( lowerCAmelCase , lowerCAmelCase , lowerCAmelCase=False , lowerCAmelCase=None ) -> Union[str, Any]: UpperCAmelCase__ : Any = GLPNConfig(hidden_sizes=[64, 1_28, 3_20, 5_12] , decoder_hidden_size=64 , depths=[3, 8, 27, 3] ) # load image processor (only resize + rescale) UpperCAmelCase__ : Any = GLPNImageProcessor() # prepare image UpperCAmelCase__ : List[str] = prepare_img() UpperCAmelCase__ : Tuple = image_processor(images=lowerCAmelCase , return_tensors="""pt""" ).pixel_values logger.info("""Converting model...""" ) # load original state dict UpperCAmelCase__ : Tuple = torch.load(lowerCAmelCase , map_location=torch.device("""cpu""" ) ) # rename keys UpperCAmelCase__ : Optional[Any] = rename_keys(lowerCAmelCase ) # key and value matrices need special treatment read_in_k_v(lowerCAmelCase , lowerCAmelCase ) # create HuggingFace model and load state dict UpperCAmelCase__ : Union[str, Any] = GLPNForDepthEstimation(lowerCAmelCase ) model.load_state_dict(lowerCAmelCase ) model.eval() # forward pass UpperCAmelCase__ : Any = model(lowerCAmelCase ) UpperCAmelCase__ : Tuple = outputs.predicted_depth # verify output if model_name is not None: if "nyu" in model_name: UpperCAmelCase__ : int = torch.tensor( [[4.4147, 4.0873, 4.0673], [3.7890, 3.2881, 3.1525], [3.7674, 3.5423, 3.4913]] ) elif "kitti" in model_name: UpperCAmelCase__ : Union[str, Any] = torch.tensor( [[3.4291, 2.7865, 2.5151], [3.2841, 2.7021, 2.3502], [3.1147, 2.4625, 2.2481]] ) else: raise ValueError(F"""Unknown model name: {model_name}""" ) UpperCAmelCase__ : Any = torch.Size([1, 4_80, 6_40] ) assert predicted_depth.shape == expected_shape assert torch.allclose(predicted_depth[0, :3, :3] , lowerCAmelCase , atol=1E-4 ) print("""Looks ok!""" ) # finally, push to hub if required if push_to_hub: logger.info("""Pushing model and image processor to the hub...""" ) model.push_to_hub( repo_path_or_name=Path(lowerCAmelCase , lowerCAmelCase ) , organization="""nielsr""" , commit_message="""Add model""" , use_temp_dir=lowerCAmelCase , ) image_processor.push_to_hub( repo_path_or_name=Path(lowerCAmelCase , lowerCAmelCase ) , organization="""nielsr""" , commit_message="""Add image processor""" , use_temp_dir=lowerCAmelCase , ) if __name__ == "__main__": _A = argparse.ArgumentParser() parser.add_argument( """--checkpoint_path""", default=None, type=str, help="""Path to the original PyTorch checkpoint (.pth file).""", ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the folder to output PyTorch model.""" ) parser.add_argument( """--push_to_hub""", action="""store_true""", help="""Whether to upload the model to the HuggingFace hub.""" ) parser.add_argument( """--model_name""", default="""glpn-kitti""", type=str, help="""Name of the model in case you're pushing to the hub.""", ) _A = parser.parse_args() convert_glpn_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub, args.model_name)
166
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 AutoImageProcessor, SwinvaConfig, SwinvaForImageClassification def SCREAMING_SNAKE_CASE__ ( lowerCamelCase__ ) -> int: __lowerCamelCase : Optional[int] = SwinvaConfig() __lowerCamelCase : Any = swinva_name.split('_' ) __lowerCamelCase : Optional[int] = name_split[1] if "to" in name_split[3]: __lowerCamelCase : List[Any] = int(name_split[3][-3:] ) else: __lowerCamelCase : List[Any] = int(name_split[3] ) if "to" in name_split[2]: __lowerCamelCase : Optional[Any] = int(name_split[2][-2:] ) else: __lowerCamelCase : Tuple = int(name_split[2][6:] ) if model_size == "tiny": __lowerCamelCase : List[str] = 9_6 __lowerCamelCase : int = (2, 2, 6, 2) __lowerCamelCase : str = (3, 6, 1_2, 2_4) elif model_size == "small": __lowerCamelCase : Union[str, Any] = 9_6 __lowerCamelCase : List[Any] = (2, 2, 1_8, 2) __lowerCamelCase : str = (3, 6, 1_2, 2_4) elif model_size == "base": __lowerCamelCase : Any = 1_2_8 __lowerCamelCase : Union[str, Any] = (2, 2, 1_8, 2) __lowerCamelCase : Optional[Any] = (4, 8, 1_6, 3_2) else: __lowerCamelCase : Union[str, Any] = 1_9_2 __lowerCamelCase : List[Any] = (2, 2, 1_8, 2) __lowerCamelCase : List[Any] = (6, 1_2, 2_4, 4_8) if "to" in swinva_name: __lowerCamelCase : Optional[Any] = (1_2, 1_2, 1_2, 6) if ("22k" in swinva_name) and ("to" not in swinva_name): __lowerCamelCase : Any = 2_1_8_4_1 __lowerCamelCase : Tuple = 'huggingface/label-files' __lowerCamelCase : Optional[int] = 'imagenet-22k-id2label.json' __lowerCamelCase : List[Any] = json.load(open(hf_hub_download(_A , _A , repo_type='dataset' ) , 'r' ) ) __lowerCamelCase : Optional[int] = {int(_A ): v for k, v in idalabel.items()} __lowerCamelCase : Union[str, Any] = idalabel __lowerCamelCase : Optional[Any] = {v: k for k, v in idalabel.items()} else: __lowerCamelCase : Tuple = 1_0_0_0 __lowerCamelCase : Optional[Any] = 'huggingface/label-files' __lowerCamelCase : Optional[Any] = 'imagenet-1k-id2label.json' __lowerCamelCase : List[Any] = json.load(open(hf_hub_download(_A , _A , repo_type='dataset' ) , 'r' ) ) __lowerCamelCase : Tuple = {int(_A ): v for k, v in idalabel.items()} __lowerCamelCase : List[str] = idalabel __lowerCamelCase : Optional[int] = {v: k for k, v in idalabel.items()} __lowerCamelCase : Tuple = img_size __lowerCamelCase : Tuple = num_classes __lowerCamelCase : Optional[Any] = embed_dim __lowerCamelCase : Optional[Any] = depths __lowerCamelCase : int = num_heads __lowerCamelCase : Union[str, Any] = window_size return config def SCREAMING_SNAKE_CASE__ ( lowerCamelCase__ ) -> str: if "patch_embed.proj" in name: __lowerCamelCase : Optional[Any] = name.replace('patch_embed.proj' , 'embeddings.patch_embeddings.projection' ) if "patch_embed.norm" in name: __lowerCamelCase : str = name.replace('patch_embed.norm' , 'embeddings.norm' ) if "layers" in name: __lowerCamelCase : List[Any] = 'encoder.' + name if "attn.proj" in name: __lowerCamelCase : str = name.replace('attn.proj' , 'attention.output.dense' ) if "attn" in name: __lowerCamelCase : List[str] = name.replace('attn' , 'attention.self' ) if "norm1" in name: __lowerCamelCase : Union[str, Any] = name.replace('norm1' , 'layernorm_before' ) if "norm2" in name: __lowerCamelCase : Dict = name.replace('norm2' , 'layernorm_after' ) if "mlp.fc1" in name: __lowerCamelCase : Optional[Any] = name.replace('mlp.fc1' , 'intermediate.dense' ) if "mlp.fc2" in name: __lowerCamelCase : List[str] = name.replace('mlp.fc2' , 'output.dense' ) if "q_bias" in name: __lowerCamelCase : List[str] = name.replace('q_bias' , 'query.bias' ) if "k_bias" in name: __lowerCamelCase : Optional[int] = name.replace('k_bias' , 'key.bias' ) if "v_bias" in name: __lowerCamelCase : Tuple = name.replace('v_bias' , 'value.bias' ) if "cpb_mlp" in name: __lowerCamelCase : Optional[Any] = name.replace('cpb_mlp' , 'continuous_position_bias_mlp' ) if name == "norm.weight": __lowerCamelCase : Tuple = 'layernorm.weight' if name == "norm.bias": __lowerCamelCase : Dict = 'layernorm.bias' if "head" in name: __lowerCamelCase : str = name.replace('head' , 'classifier' ) else: __lowerCamelCase : List[str] = 'swinv2.' + name return name def SCREAMING_SNAKE_CASE__ ( lowerCamelCase__ , lowerCamelCase__ ) -> List[Any]: for key in orig_state_dict.copy().keys(): __lowerCamelCase : int = orig_state_dict.pop(_A ) if "mask" in key: continue elif "qkv" in key: __lowerCamelCase : str = key.split('.' ) __lowerCamelCase : Any = int(key_split[1] ) __lowerCamelCase : Any = int(key_split[3] ) __lowerCamelCase : Optional[Any] = model.swinva.encoder.layers[layer_num].blocks[block_num].attention.self.all_head_size if "weight" in key: __lowerCamelCase : List[str] = val[:dim, :] __lowerCamelCase : str = val[dim : dim * 2, :] __lowerCamelCase : str = val[-dim:, :] else: __lowerCamelCase : str = val[:dim] __lowerCamelCase : int = val[ dim : dim * 2 ] __lowerCamelCase : List[str] = val[-dim:] else: __lowerCamelCase : Union[str, Any] = val return orig_state_dict def SCREAMING_SNAKE_CASE__ ( lowerCamelCase__ , lowerCamelCase__ ) -> str: __lowerCamelCase : Any = timm.create_model(_A , pretrained=_A ) timm_model.eval() __lowerCamelCase : str = get_swinva_config(_A ) __lowerCamelCase : Optional[Any] = SwinvaForImageClassification(_A ) model.eval() __lowerCamelCase : Any = convert_state_dict(timm_model.state_dict() , _A ) model.load_state_dict(_A ) __lowerCamelCase : Union[str, Any] = 'http://images.cocodataset.org/val2017/000000039769.jpg' __lowerCamelCase : List[str] = AutoImageProcessor.from_pretrained('microsoft/{}'.format(swinva_name.replace('_' , '-' ) ) ) __lowerCamelCase : Any = Image.open(requests.get(_A , stream=_A ).raw ) __lowerCamelCase : Any = image_processor(images=_A , return_tensors='pt' ) __lowerCamelCase : Optional[Any] = timm_model(inputs['pixel_values'] ) __lowerCamelCase : int = model(**_A ).logits assert torch.allclose(_A , _A , atol=1e-3 ) print(F"Saving model {swinva_name} to {pytorch_dump_folder_path}" ) model.save_pretrained(_A ) print(F"Saving image processor to {pytorch_dump_folder_path}" ) image_processor.save_pretrained(_A ) model.push_to_hub( repo_path_or_name=Path(_A , _A ) , organization='nandwalritik' , commit_message='Add model' , ) if __name__ == "__main__": a =argparse.ArgumentParser() # Required parameters parser.add_argument( """--swinv2_name""", default="""swinv2_tiny_patch4_window8_256""", type=str, help="""Name of the Swinv2 timm model you\'d like to convert.""", ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model directory.""" ) a =parser.parse_args() convert_swinva_checkpoint(args.swinva_name, args.pytorch_dump_folder_path)
73
'''simple docstring''' import unittest from transformers import EsmConfig, is_torch_available from transformers.testing_utils import TestCasePlus, 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.models.esm.modeling_esmfold import EsmForProteinFolding class a__: def __init__( self : str , __snake_case : Union[str, Any] , __snake_case : List[str]=13 , __snake_case : Tuple=7 , __snake_case : Optional[Any]=False , __snake_case : Dict=True , __snake_case : List[Any]=False , __snake_case : Optional[int]=False , __snake_case : Optional[Any]=19 , __snake_case : Any=32 , __snake_case : Union[str, Any]=5 , __snake_case : Union[str, Any]=4 , __snake_case : int=37 , __snake_case : Union[str, Any]="gelu" , __snake_case : Optional[Any]=0.1 , __snake_case : List[str]=0.1 , __snake_case : int=5_12 , __snake_case : int=16 , __snake_case : Tuple=2 , __snake_case : str=0.02 , __snake_case : str=3 , __snake_case : Dict=4 , __snake_case : List[Any]=None , ): a : Tuple = parent a : List[str] = batch_size a : Optional[Any] = seq_length a : Tuple = is_training a : Optional[Any] = use_input_mask a : List[Any] = use_token_type_ids a : List[Any] = use_labels a : int = vocab_size a : Union[str, Any] = hidden_size a : Any = num_hidden_layers a : List[str] = num_attention_heads a : int = intermediate_size a : str = hidden_act a : Tuple = hidden_dropout_prob a : Union[str, Any] = attention_probs_dropout_prob a : List[str] = max_position_embeddings a : Any = type_vocab_size a : List[str] = type_sequence_label_size a : Union[str, Any] = initializer_range a : Optional[int] = num_labels a : Optional[Any] = num_choices a : Optional[int] = scope def lowercase_ ( self : List[Any] ): a : List[Any] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) a : Dict = None if self.use_input_mask: a : Optional[Any] = random_attention_mask([self.batch_size, self.seq_length] ) a : Optional[Any] = None a : Optional[int] = None a : Dict = None if self.use_labels: a : Dict = ids_tensor([self.batch_size] , self.type_sequence_label_size ) a : List[Any] = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) a : List[str] = ids_tensor([self.batch_size] , self.num_choices ) a : Dict = self.get_config() return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels def lowercase_ ( self : List[Any] ): a : Any = EsmConfig( vocab_size=33 , hidden_size=self.hidden_size , pad_token_id=1 , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , is_folding_model=__snake_case , esmfold_config={'trunk': {'num_blocks': 2}, 'fp16_esm': False} , ) return config def lowercase_ ( self : Optional[Any] , __snake_case : int , __snake_case : str , __snake_case : Optional[Any] , __snake_case : List[Any] , __snake_case : str , __snake_case : Any ): a : Tuple = EsmForProteinFolding(config=__snake_case ).float() model.to(__snake_case ) model.eval() a : Dict = model(__snake_case , attention_mask=__snake_case ) a : Union[str, Any] = model(__snake_case ) a : List[Any] = model(__snake_case ) self.parent.assertEqual(result.positions.shape , (8, self.batch_size, self.seq_length, 14, 3) ) self.parent.assertEqual(result.angles.shape , (8, self.batch_size, self.seq_length, 7, 2) ) def lowercase_ ( self : Optional[Any] ): a : Tuple = self.prepare_config_and_inputs() ( ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ( a ) , ) : Optional[Any] = config_and_inputs a : Optional[int] = {'input_ids': input_ids, 'attention_mask': input_mask} return config, inputs_dict @require_torch class a__( lowerCamelCase__ , lowerCamelCase__ , unittest.TestCase ): lowercase__ = False lowercase__ = (EsmForProteinFolding,) if is_torch_available() else () lowercase__ = () lowercase__ = {} if is_torch_available() else {} lowercase__ = False def lowercase_ ( self : int ): a : Tuple = EsmFoldModelTester(self ) a : Any = ConfigTester(self , config_class=__snake_case , hidden_size=37 ) def lowercase_ ( self : List[str] ): self.config_tester.run_common_tests() def lowercase_ ( self : Union[str, Any] ): a : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__snake_case ) @unittest.skip('Does not support attention outputs' ) def lowercase_ ( self : str ): pass @unittest.skip def lowercase_ ( self : Optional[int] ): pass @unittest.skip('Esm does not support embedding resizing' ) def lowercase_ ( self : Optional[int] ): pass @unittest.skip('Esm does not support embedding resizing' ) def lowercase_ ( self : Any ): pass @unittest.skip('ESMFold does not support passing input embeds!' ) def lowercase_ ( self : Any ): pass @unittest.skip('ESMFold does not support head pruning.' ) def lowercase_ ( self : Union[str, Any] ): pass @unittest.skip('ESMFold does not support head pruning.' ) def lowercase_ ( self : List[Any] ): pass @unittest.skip('ESMFold does not support head pruning.' ) def lowercase_ ( self : List[Any] ): pass @unittest.skip('ESMFold does not support head pruning.' ) def lowercase_ ( self : int ): pass @unittest.skip('ESMFold does not support head pruning.' ) def lowercase_ ( self : List[Any] ): pass @unittest.skip('ESMFold does not output hidden states in the normal way.' ) def lowercase_ ( self : int ): pass @unittest.skip('ESMfold does not output hidden states in the normal way.' ) def lowercase_ ( self : int ): pass @unittest.skip('ESMFold only has one output format.' ) def lowercase_ ( self : Dict ): pass @unittest.skip('This test doesn\'t work for ESMFold and doesn\'t test core functionality' ) def lowercase_ ( self : Tuple ): pass @unittest.skip('ESMFold does not support input chunking.' ) def lowercase_ ( self : List[str] ): pass @unittest.skip('ESMFold doesn\'t respect you and it certainly doesn\'t respect your initialization arguments.' ) def lowercase_ ( self : List[Any] ): pass @unittest.skip('ESMFold doesn\'t support torchscript compilation.' ) def lowercase_ ( self : Union[str, Any] ): pass @unittest.skip('ESMFold doesn\'t support torchscript compilation.' ) def lowercase_ ( self : Any ): pass @unittest.skip('ESMFold doesn\'t support torchscript compilation.' ) def lowercase_ ( self : List[str] ): pass @unittest.skip('ESMFold doesn\'t support data parallel.' ) def lowercase_ ( self : Dict ): pass @unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.' ) def lowercase_ ( self : Union[str, Any] ): pass @require_torch class a__( lowerCamelCase__ ): @slow def lowercase_ ( self : Optional[int] ): a : Optional[Any] = EsmForProteinFolding.from_pretrained('facebook/esmfold_v1' ).float() model.eval() a : int = torch.tensor([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]] ) a : Any = model(__snake_case )['positions'] a : Dict = torch.tensor([2.5828, 0.7993, -10.9334] , dtype=torch.floataa ) self.assertTrue(torch.allclose(position_outputs[0, 0, 0, 0] , __snake_case , atol=1e-4 ) )
297
0
from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import center_crop, normalize, rescale, resize, to_channel_dimension_format from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging if is_vision_available(): import PIL __snake_case : List[Any] =logging.get_logger(__name__) class lowerCamelCase__ ( lowerCamelCase__): '''simple docstring''' snake_case_ =["""pixel_values"""] def __init__(self ,__lowerCamelCase = True ,__lowerCamelCase = None ,__lowerCamelCase = PIL.Image.BICUBIC ,__lowerCamelCase = True ,__lowerCamelCase = None ,__lowerCamelCase = 1 / 2_55 ,__lowerCamelCase = True ,__lowerCamelCase = True ,__lowerCamelCase = None ,__lowerCamelCase = None ,**__lowerCamelCase ,) -> None: """simple docstring""" super().__init__(**__lowerCamelCase ) lowerCAmelCase__ : Union[str, Any] = size if size is not None else {'''height''': 2_56, '''width''': 2_56} lowerCAmelCase__ : Dict = get_size_dict(__lowerCamelCase ) lowerCAmelCase__ : Dict = crop_size if crop_size is not None else {'''height''': 2_24, '''width''': 2_24} lowerCAmelCase__ : Any = get_size_dict(__lowerCamelCase ,param_name='''crop_size''' ) lowerCAmelCase__ : str = do_resize lowerCAmelCase__ : Any = size lowerCAmelCase__ : Optional[Any] = resample lowerCAmelCase__ : Dict = do_center_crop lowerCAmelCase__ : int = crop_size lowerCAmelCase__ : List[Any] = do_rescale lowerCAmelCase__ : Optional[Any] = rescale_factor lowerCAmelCase__ : Union[str, Any] = do_normalize lowerCAmelCase__ : Dict = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN lowerCAmelCase__ : Tuple = image_std if image_std is not None else IMAGENET_STANDARD_STD def lowerCAmelCase__ (self ,__lowerCamelCase ,__lowerCamelCase ,__lowerCamelCase = PIL.Image.BICUBIC ,__lowerCamelCase = None ,**__lowerCamelCase ,) -> np.ndarray: """simple docstring""" lowerCAmelCase__ : int = get_size_dict(__lowerCamelCase ) if "height" not in size or "width" not in size: raise ValueError(f"""The size dictionary must have keys 'height' and 'width'. Got {size.keys()}""" ) return resize( __lowerCamelCase ,size=(size['''height'''], size['''width''']) ,resample=__lowerCamelCase ,data_format=__lowerCamelCase ,**__lowerCamelCase ) def lowerCAmelCase__ (self ,__lowerCamelCase ,__lowerCamelCase ,__lowerCamelCase = None ,**__lowerCamelCase ,) -> np.ndarray: """simple docstring""" lowerCAmelCase__ : List[Any] = get_size_dict(__lowerCamelCase ) if "height" not in size or "width" not in size: raise ValueError(f"""The size dictionary must have keys 'height' and 'width'. Got {size.keys()}""" ) return center_crop(__lowerCamelCase ,size=(size['''height'''], size['''width''']) ,data_format=__lowerCamelCase ,**__lowerCamelCase ) def lowerCAmelCase__ (self ,__lowerCamelCase ,__lowerCamelCase ,__lowerCamelCase = None ,**__lowerCamelCase ,) -> Optional[int]: """simple docstring""" return rescale(__lowerCamelCase ,scale=__lowerCamelCase ,data_format=__lowerCamelCase ,**__lowerCamelCase ) def lowerCAmelCase__ (self ,__lowerCamelCase ,__lowerCamelCase ,__lowerCamelCase ,__lowerCamelCase = None ,**__lowerCamelCase ,) -> np.ndarray: """simple docstring""" return normalize(__lowerCamelCase ,mean=__lowerCamelCase ,std=__lowerCamelCase ,data_format=__lowerCamelCase ,**__lowerCamelCase ) def lowerCAmelCase__ (self ,__lowerCamelCase ,__lowerCamelCase = None ,__lowerCamelCase = None ,__lowerCamelCase=None ,__lowerCamelCase = None ,__lowerCamelCase = None ,__lowerCamelCase = None ,__lowerCamelCase = None ,__lowerCamelCase = None ,__lowerCamelCase = None ,__lowerCamelCase = None ,__lowerCamelCase = None ,__lowerCamelCase = ChannelDimension.FIRST ,**__lowerCamelCase ,) -> PIL.Image.Image: """simple docstring""" lowerCAmelCase__ : Union[str, Any] = do_resize if do_resize is not None else self.do_resize lowerCAmelCase__ : List[Any] = resample if resample is not None else self.resample lowerCAmelCase__ : List[str] = do_center_crop if do_center_crop is not None else self.do_center_crop lowerCAmelCase__ : Dict = do_rescale if do_rescale is not None else self.do_rescale lowerCAmelCase__ : Tuple = rescale_factor if rescale_factor is not None else self.rescale_factor lowerCAmelCase__ : Dict = do_normalize if do_normalize is not None else self.do_normalize lowerCAmelCase__ : List[str] = image_mean if image_mean is not None else self.image_mean lowerCAmelCase__ : Optional[Any] = image_std if image_std is not None else self.image_std lowerCAmelCase__ : str = size if size is not None else self.size lowerCAmelCase__ : Optional[Any] = get_size_dict(__lowerCamelCase ) lowerCAmelCase__ : Optional[int] = crop_size if crop_size is not None else self.crop_size lowerCAmelCase__ : Union[str, Any] = get_size_dict(__lowerCamelCase ,param_name='''crop_size''' ) lowerCAmelCase__ : Optional[Any] = make_list_of_images(__lowerCamelCase ) if not valid_images(__lowerCamelCase ): raise ValueError( '''Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ''' '''torch.Tensor, tf.Tensor or jax.ndarray.''' ) if do_resize and size is None or resample is None: raise ValueError('''Size and resample must be specified if do_resize is True.''' ) if do_center_crop and crop_size is None: raise ValueError('''Crop size must be specified if do_center_crop is True.''' ) if do_rescale and rescale_factor is None: raise ValueError('''Rescale factor must be specified if do_rescale is True.''' ) if do_normalize and (image_mean is None or image_std is None): raise ValueError('''Image mean and std must be specified if do_normalize is True.''' ) # All transformations expect numpy arrays. lowerCAmelCase__ : List[str] = [to_numpy_array(__lowerCamelCase ) for image in images] if do_resize: lowerCAmelCase__ : List[str] = [self.resize(image=__lowerCamelCase ,size=__lowerCamelCase ,resample=__lowerCamelCase ) for image in images] if do_center_crop: lowerCAmelCase__ : str = [self.center_crop(image=__lowerCamelCase ,size=__lowerCamelCase ) for image in images] if do_rescale: lowerCAmelCase__ : Tuple = [self.rescale(image=__lowerCamelCase ,scale=__lowerCamelCase ) for image in images] if do_normalize: lowerCAmelCase__ : List[Any] = [self.normalize(image=__lowerCamelCase ,mean=__lowerCamelCase ,std=__lowerCamelCase ) for image in images] lowerCAmelCase__ : Optional[int] = [to_channel_dimension_format(__lowerCamelCase ,__lowerCamelCase ) for image in images] lowerCAmelCase__ : Optional[Any] = {'''pixel_values''': images} return BatchFeature(data=__lowerCamelCase ,tensor_type=__lowerCamelCase )
352
from ...configuration_utils import PretrainedConfig from ...utils import logging __snake_case : Any =logging.get_logger(__name__) __snake_case : Tuple ={ 'sayakpaul/vit-msn-base': 'https://huggingface.co/sayakpaul/vit-msn-base/resolve/main/config.json', # See all ViT MSN models at https://huggingface.co/models?filter=vit_msn } class lowerCamelCase__ ( lowerCamelCase__): '''simple docstring''' snake_case_ ="""vit_msn""" def __init__(self ,__lowerCamelCase=7_68 ,__lowerCamelCase=12 ,__lowerCamelCase=12 ,__lowerCamelCase=30_72 ,__lowerCamelCase="gelu" ,__lowerCamelCase=0.0 ,__lowerCamelCase=0.0 ,__lowerCamelCase=0.02 ,__lowerCamelCase=1e-06 ,__lowerCamelCase=2_24 ,__lowerCamelCase=16 ,__lowerCamelCase=3 ,__lowerCamelCase=True ,**__lowerCamelCase ,) -> Any: """simple docstring""" super().__init__(**__lowerCamelCase ) lowerCAmelCase__ : List[Any] = hidden_size lowerCAmelCase__ : str = num_hidden_layers lowerCAmelCase__ : List[str] = num_attention_heads lowerCAmelCase__ : Optional[int] = intermediate_size lowerCAmelCase__ : List[Any] = hidden_act lowerCAmelCase__ : Tuple = hidden_dropout_prob lowerCAmelCase__ : List[Any] = attention_probs_dropout_prob lowerCAmelCase__ : int = initializer_range lowerCAmelCase__ : Union[str, Any] = layer_norm_eps lowerCAmelCase__ : List[str] = image_size lowerCAmelCase__ : str = patch_size lowerCAmelCase__ : Optional[int] = num_channels lowerCAmelCase__ : int = qkv_bias
94
0
'''simple docstring''' import inspect import warnings from typing import Any, Dict, Optional, Union from packaging import version def a_ ( *__snake_case : Tuple , __snake_case : Optional[Union[Dict, Any]] = None , __snake_case : List[str]=True , __snake_case : Tuple=2 ) -> Any: """simple docstring""" from .. import __version__ lowerCamelCase_ =take_from lowerCamelCase_ =() if not isinstance(args[0] , __snake_case ): lowerCamelCase_ =(args,) for attribute, version_name, message in args: if version.parse(version.parse(__snake_case ).base_version ) >= version.parse(__snake_case ): raise ValueError( F'''The deprecation tuple {(attribute, version_name, message)} should be removed since diffusers\'''' F''' version {__version__} is >= {version_name}''' ) lowerCamelCase_ =None if isinstance(__snake_case , __snake_case ) and attribute in deprecated_kwargs: values += (deprecated_kwargs.pop(__snake_case ),) lowerCamelCase_ =F'''The `{attribute}` argument is deprecated and will be removed in version {version_name}.''' elif hasattr(__snake_case , __snake_case ): values += (getattr(__snake_case , __snake_case ),) lowerCamelCase_ =F'''The `{attribute}` attribute is deprecated and will be removed in version {version_name}.''' elif deprecated_kwargs is None: lowerCamelCase_ =F'''`{attribute}` is deprecated and will be removed in version {version_name}.''' if warning is not None: lowerCamelCase_ =warning + ''' ''' if standard_warn else '''''' warnings.warn(warning + message , __snake_case , stacklevel=__snake_case ) if isinstance(__snake_case , __snake_case ) and len(__snake_case ) > 0: lowerCamelCase_ =inspect.getouterframes(inspect.currentframe() )[1] lowerCamelCase_ =call_frame.filename lowerCamelCase_ =call_frame.lineno lowerCamelCase_ =call_frame.function lowerCamelCase_, lowerCamelCase_ =next(iter(deprecated_kwargs.items() ) ) raise TypeError(F'''{function} in {filename} line {line_number-1} got an unexpected keyword argument `{key}`''' ) if len(__snake_case ) == 0: return elif len(__snake_case ) == 1: return values[0] return values
75
'''simple docstring''' import unittest from pathlib import Path from shutil import copyfile from transformers import SPIECE_UNDERLINE, is_sentencepiece_available from transformers.models.speech_to_text import SpeechaTextTokenizer from transformers.models.speech_to_text.tokenization_speech_to_text import VOCAB_FILES_NAMES, save_json from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin a_ : str = get_tests_dir("""fixtures/test_sentencepiece.model""") if is_sentencepiece_available(): import sentencepiece as sp a_ : Optional[Any] = 5 a_ : str = 10 @require_sentencepiece @require_tokenizers class __UpperCamelCase ( lowerCamelCase__ , unittest.TestCase ): lowercase : int =SpeechaTextTokenizer lowercase : int =False lowercase : List[str] =True def lowercase__ ( self ): """simple docstring""" super().setUp() lowerCamelCase_ =sp.SentencePieceProcessor() spm_model.Load(lowerCAmelCase ) lowerCamelCase_ =['''<s>''', '''<pad>''', '''</s>''', '''<unk>'''] vocab += [spm_model.IdToPiece(id_ ) for id_ in range(len(lowerCAmelCase ) )] lowerCamelCase_ =dict(zip(lowerCAmelCase, range(len(lowerCAmelCase ) ) ) ) lowerCamelCase_ =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'''] ) lowerCamelCase_ =SpeechaTextTokenizer.from_pretrained(self.tmpdirname ) tokenizer.save_pretrained(self.tmpdirname ) def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ ='''<pad>''' lowerCamelCase_ =1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(lowerCAmelCase ), lowerCAmelCase ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(lowerCAmelCase ), lowerCAmelCase ) def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0], '''<s>''' ) self.assertEqual(vocab_keys[1], '''<pad>''' ) self.assertEqual(vocab_keys[-1], '''j''' ) self.assertEqual(len(lowerCAmelCase ), 1_001 ) def lowercase__ ( self ): """simple docstring""" self.assertEqual(self.get_tokenizer().vocab_size, 1_001 ) def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ =SpeechaTextTokenizer.from_pretrained(self.tmpdirname ) lowerCamelCase_ =tokenizer.tokenize('''This is a test''' ) self.assertListEqual(lowerCAmelCase, ['''▁This''', '''▁is''', '''▁a''', '''▁t''', '''est'''] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(lowerCAmelCase ), [289, 50, 14, 174, 386], ) lowerCamelCase_ =tokenizer.tokenize('''I was born in 92000, and this is falsé.''' ) self.assertListEqual( lowerCAmelCase, [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''', '''é''', '''.'''], ) lowerCamelCase_ =tokenizer.convert_tokens_to_ids(lowerCAmelCase ) self.assertListEqual(lowerCAmelCase, [12, 25, 88, 59, 28, 23, 11, 4, 606, 351, 351, 351, 7, 16, 70, 50, 76, 84, 10, 4, 8] ) lowerCamelCase_ =tokenizer.convert_ids_to_tokens(lowerCAmelCase ) self.assertListEqual( lowerCAmelCase, [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>''', '''.'''], ) @slow def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ ={'''input_ids''': [[3_791, 797, 31, 11, 64, 797, 31, 2_429, 433, 12, 1_176, 12, 20, 786, 915, 142, 2_413, 240, 37, 3_238, 797, 31, 11, 35, 93, 915, 142, 2_413, 240, 37, 5_540, 567, 1_276, 93, 37, 610, 40, 62, 455, 657, 1_042, 123, 780, 177, 37, 309, 241, 1_298, 514, 20, 292, 2_737, 114, 2_469, 241, 85, 64, 302, 548, 528, 423, 4, 509, 406, 423, 37, 601, 4, 777, 302, 548, 528, 423, 284, 4, 3_388, 511, 459, 4, 3_555, 40, 321, 302, 705, 4, 3_388, 511, 583, 326, 5, 5, 5, 62, 3_310, 560, 177, 2_680, 217, 1_508, 32, 31, 853, 418, 64, 583, 511, 1_605, 62, 35, 93, 560, 177, 2_680, 217, 1_508, 1_521, 64, 583, 511, 519, 62, 20, 1_515, 764, 20, 149, 261, 5_625, 7_972, 20, 5_540, 567, 1_276, 93, 3_925, 1_675, 11, 15, 802, 7_972, 576, 217, 1_508, 11, 35, 93, 1_253, 2_441, 15, 289, 652, 31, 416, 321, 3_842, 115, 40, 911, 8, 476, 619, 4, 380, 142, 423, 335, 240, 35, 93, 264, 8, 11, 335, 569, 420, 163, 5, 2], [260, 548, 528, 423, 20, 451, 20, 2_681, 1_153, 3_434, 20, 5_540, 37, 567, 126, 1_253, 2_441, 3_376, 449, 210, 431, 1_563, 177, 767, 5_540, 11, 1_203, 472, 11, 2_953, 685, 285, 364, 706, 1_153, 20, 6_799, 20, 2_869, 20, 4_464, 126, 40, 2_429, 20, 1_040, 866, 2_664, 418, 20, 318, 20, 1_726, 186, 20, 265, 522, 35, 93, 2_191, 4_634, 20, 1_040, 12, 6_799, 15, 228, 2_356, 142, 31, 11, 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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [2_575, 2_666, 684, 1_582, 1_176, 12, 627, 149, 619, 20, 4_902, 563, 11, 20, 149, 261, 3_420, 2_356, 174, 142, 4_714, 131, 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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], '''attention_mask''': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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/s2t-small-mustc-en-de-st''', revision='''a14f04cf0776c02f62a8cb800cf7909e15ea23ad''', ) @require_sentencepiece class __UpperCamelCase ( unittest.TestCase ): lowercase : Tuple ='valhalla/s2t_mustc_multilinguial_medium' lowercase : Dict ='C\'est trop cool' lowercase : str ='Esto es genial' @classmethod def lowercase__ ( cls ): """simple docstring""" lowerCamelCase_ =SpeechaTextTokenizer.from_pretrained(cls.checkpoint_name ) return cls def lowercase__ ( self ): """simple docstring""" self.assertEqual(self.tokenizer.lang_code_to_id['''pt'''], 4 ) self.assertEqual(self.tokenizer.lang_code_to_id['''ru'''], 6 ) self.assertEqual(self.tokenizer.lang_code_to_id['''it'''], 9 ) self.assertEqual(self.tokenizer.lang_code_to_id['''de'''], 11 ) def lowercase__ ( self ): """simple docstring""" self.assertEqual(self.tokenizer.vocab_size, 10_000 ) def lowercase__ ( self ): """simple docstring""" self.assertIn(lowerCAmelCase, self.tokenizer.all_special_ids ) lowerCamelCase_ =[ES_CODE, 4, 1_601, 47, 7_647, 2] lowerCamelCase_ =self.tokenizer.decode(lowerCAmelCase, skip_special_tokens=lowerCAmelCase ) lowerCamelCase_ =self.tokenizer.decode(generated_ids[1:], skip_special_tokens=lowerCAmelCase ) self.assertEqual(lowerCAmelCase, lowerCAmelCase ) self.assertNotIn(self.tokenizer.eos_token, lowerCAmelCase ) def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ ='''fr''' lowerCamelCase_ =self.tokenizer(self.french_text ).input_ids self.assertEqual(encoded[0], lowerCAmelCase ) self.assertEqual(encoded[-1], self.tokenizer.eos_token_id ) def lowercase__ ( self ): """simple docstring""" lowerCamelCase_ ='''fr''' self.assertListEqual(self.tokenizer.prefix_tokens, [FR_CODE] ) lowerCamelCase_ ='''es''' self.assertListEqual(self.tokenizer.prefix_tokens, [ES_CODE] )
75
1
import os import time import warnings from dataclasses import dataclass, field from enum import Enum from typing import List, Optional, Union import torch from filelock import FileLock from torch.utils.data import Dataset from ...tokenization_utils_base import PreTrainedTokenizerBase from ...utils import logging from ..processors.glue import glue_convert_examples_to_features, glue_output_modes, glue_processors from ..processors.utils import InputFeatures a__ = logging.get_logger(__name__) @dataclass class UpperCAmelCase_ : """simple docstring""" UpperCAmelCase__ : str = field(metadata={"help": "The name of the task to train on: " + ", ".join(glue_processors.keys() )} ) UpperCAmelCase__ : str = field( metadata={"help": "The input data dir. Should contain the .tsv files (or other data files) for the task."} ) UpperCAmelCase__ : int = field( default=128 , metadata={ "help": ( "The maximum total input sequence length after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) } , ) UpperCAmelCase__ : bool = field( default=__lowercase , metadata={"help": "Overwrite the cached training and evaluation sets"} ) def __lowercase ( self ) -> List[Any]: _a : Union[str, Any] = self.task_name.lower() class UpperCAmelCase_ ( __lowercase ): """simple docstring""" UpperCAmelCase__ : str = "train" UpperCAmelCase__ : Optional[Any] = "dev" UpperCAmelCase__ : int = "test" class UpperCAmelCase_ ( __lowercase ): """simple docstring""" UpperCAmelCase__ : GlueDataTrainingArguments UpperCAmelCase__ : str UpperCAmelCase__ : List[InputFeatures] def __init__( self , _a , _a , _a = None , _a = Split.train , _a = None , ) -> int: warnings.warn( '''This dataset will be removed from the library soon, preprocessing should be handled with the 🤗 Datasets ''' '''library. You can have a look at this example script for pointers: ''' '''https://github.com/huggingface/transformers/blob/main/examples/pytorch/text-classification/run_glue.py''' , _a , ) _a : List[Any] = args _a : List[Any] = glue_processors[args.task_name]() _a : Optional[Any] = glue_output_modes[args.task_name] if isinstance(_a , _a ): try: _a : Optional[Any] = Split[mode] except KeyError: raise KeyError('''mode is not a valid split name''' ) # Load data features from cache or dataset file _a : int = os.path.join( cache_dir if cache_dir is not None else args.data_dir , F"""cached_{mode.value}_{tokenizer.__class__.__name__}_{args.max_seq_length}_{args.task_name}""" , ) _a : Tuple = self.processor.get_labels() if args.task_name in ["mnli", "mnli-mm"] and tokenizer.__class__.__name__ in ( "RobertaTokenizer", "RobertaTokenizerFast", "XLMRobertaTokenizer", "BartTokenizer", "BartTokenizerFast", ): # HACK(label indices are swapped in RoBERTa pretrained model) _a , _a : Union[str, Any] = label_list[2], label_list[1] _a : List[Any] = label_list # Make sure only the first process in distributed training processes the dataset, # and the others will use the cache. _a : Optional[Any] = cached_features_file + '''.lock''' with FileLock(_a ): if os.path.exists(_a ) and not args.overwrite_cache: _a : str = time.time() _a : Union[str, Any] = torch.load(_a ) logger.info( F"""Loading features from cached file {cached_features_file} [took %.3f s]""" , time.time() - start ) else: logger.info(F"""Creating features from dataset file at {args.data_dir}""" ) if mode == Split.dev: _a : Union[str, Any] = self.processor.get_dev_examples(args.data_dir ) elif mode == Split.test: _a : Optional[int] = self.processor.get_test_examples(args.data_dir ) else: _a : Tuple = self.processor.get_train_examples(args.data_dir ) if limit_length is not None: _a : List[Any] = examples[:limit_length] _a : Optional[Any] = glue_convert_examples_to_features( _a , _a , max_length=args.max_seq_length , label_list=_a , output_mode=self.output_mode , ) _a : List[str] = time.time() torch.save(self.features , _a ) # ^ This seems to take a lot of time so I want to investigate why and how we can improve. logger.info( F"""Saving features into cached file {cached_features_file} [took {time.time() - start:.3f} s]""" ) def __len__( self ) -> int: return len(self.features ) def __getitem__( self , _a ) -> InputFeatures: return self.features[i] def __lowercase ( self ) -> Optional[Any]: return self.label_list
15
import enum import warnings from .. import MODEL_FOR_CAUSAL_LM_MAPPING, TF_MODEL_FOR_CAUSAL_LM_MAPPING from ..utils import add_end_docstrings, is_tf_available from .base import PIPELINE_INIT_ARGS, Pipeline if is_tf_available(): import tensorflow as tf class UpperCAmelCase_ ( enum.Enum ): """simple docstring""" UpperCAmelCase__ : int = 0 UpperCAmelCase__ : Union[str, Any] = 1 UpperCAmelCase__ : Optional[Any] = 2 @add_end_docstrings(__lowercase ) class UpperCAmelCase_ ( __lowercase ): """simple docstring""" UpperCAmelCase__ : Optional[Any] = "\n In 1991, the remains of Russian Tsar Nicholas II and his family (except for Alexei and Maria) are discovered. The\n voice of Nicholas's young son, Tsarevich Alexei Nikolaevich, narrates the remainder of the story. 1883 Western\n Siberia, a young Grigori Rasputin is asked by his father and a group of men to perform magic. Rasputin has a vision\n and denounces one of the men as a horse thief. Although his father initially slaps him for making such an\n accusation, Rasputin watches as the man is chased outside and beaten. Twenty years later, Rasputin sees a vision of\n the Virgin Mary, prompting him to become a priest. Rasputin quickly becomes famous, with people, even a bishop,\n begging for his blessing. <eod> </s> <eos>\n " def __init__( self , *_a , **_a ) -> List[str]: super().__init__(*_a , **_a ) self.check_model_type( TF_MODEL_FOR_CAUSAL_LM_MAPPING if self.framework == '''tf''' else MODEL_FOR_CAUSAL_LM_MAPPING ) if "prefix" not in self._preprocess_params: # This is very specific. The logic is quite complex and needs to be done # as a "default". # It also defines both some preprocess_kwargs and generate_kwargs # which is why we cannot put them in their respective methods. _a : Dict = None if self.model.config.prefix is not None: _a : List[Any] = self.model.config.prefix if prefix is None and self.model.__class__.__name__ in [ "XLNetLMHeadModel", "TransfoXLLMHeadModel", "TFXLNetLMHeadModel", "TFTransfoXLLMHeadModel", ]: # For XLNet and TransformerXL we add an article to the prompt to give more state to the model. _a : Optional[Any] = self.XL_PREFIX if prefix is not None: # Recalculate some generate_kwargs linked to prefix. _a , _a , _a : str = self._sanitize_parameters(prefix=_a , **self._forward_params ) _a : Optional[Any] = {**self._preprocess_params, **preprocess_params} _a : List[Any] = {**self._forward_params, **forward_params} def __lowercase ( self , _a=None , _a=None , _a=None , _a=None , _a=None , _a=None , _a=None , _a=None , **_a , ) -> Optional[int]: _a : List[Any] = {} if prefix is not None: _a : Optional[Any] = prefix if prefix: _a : Dict = self.tokenizer( _a , padding=_a , add_special_tokens=_a , return_tensors=self.framework ) _a : Tuple = prefix_inputs['''input_ids'''].shape[-1] if handle_long_generation is not None: if handle_long_generation not in {"hole"}: raise ValueError( F"""{handle_long_generation} is not a valid value for `handle_long_generation` parameter expected""" ''' [None, \'hole\']''' ) _a : Dict = handle_long_generation preprocess_params.update(_a ) _a : Tuple = generate_kwargs _a : Any = {} if return_full_text is not None and return_type is None: if return_text is not None: raise ValueError('''`return_text` is mutually exclusive with `return_full_text`''' ) if return_tensors is not None: raise ValueError('''`return_full_text` is mutually exclusive with `return_tensors`''' ) _a : List[str] = ReturnType.FULL_TEXT if return_full_text else ReturnType.NEW_TEXT if return_tensors is not None and return_type is None: if return_text is not None: raise ValueError('''`return_text` is mutually exclusive with `return_tensors`''' ) _a : Any = ReturnType.TENSORS if return_type is not None: _a : Any = return_type if clean_up_tokenization_spaces is not None: _a : List[Any] = clean_up_tokenization_spaces if stop_sequence is not None: _a : Tuple = self.tokenizer.encode(_a , add_special_tokens=_a ) if len(_a ) > 1: warnings.warn( '''Stopping on a multiple token sequence is not yet supported on transformers. The first token of''' ''' the stop sequence will be used as the stop sequence string in the interim.''' ) _a : List[Any] = stop_sequence_ids[0] return preprocess_params, forward_params, postprocess_params def __lowercase ( self , *_a , **_a ) -> Union[str, Any]: # Parse arguments if self.model.__class__.__name__ in ["TransfoXLLMHeadModel"]: kwargs.update({'''add_space_before_punct_symbol''': True} ) return super()._parse_and_tokenize(*_a , **_a ) def __call__( self , _a , **_a ) -> List[str]: return super().__call__(_a , **_a ) def __lowercase ( self , _a , _a="" , _a=None , **_a ) -> List[Any]: _a : Optional[int] = self.tokenizer( prefix + prompt_text , padding=_a , add_special_tokens=_a , return_tensors=self.framework ) _a : Union[str, Any] = prompt_text if handle_long_generation == "hole": _a : List[str] = inputs['''input_ids'''].shape[-1] if "max_new_tokens" in generate_kwargs: _a : int = generate_kwargs['''max_new_tokens'''] else: _a : List[Any] = generate_kwargs.get('''max_length''' , self.model.config.max_length ) - cur_len if new_tokens < 0: raise ValueError('''We cannot infer how many new tokens are expected''' ) if cur_len + new_tokens > self.tokenizer.model_max_length: _a : List[str] = self.tokenizer.model_max_length - new_tokens if keep_length <= 0: raise ValueError( '''We cannot use `hole` to handle this generation the number of desired tokens exceeds the''' ''' models max length''' ) _a : List[Any] = inputs['''input_ids'''][:, -keep_length:] if "attention_mask" in inputs: _a : List[str] = inputs['''attention_mask'''][:, -keep_length:] return inputs def __lowercase ( self , _a , **_a ) -> Optional[int]: _a : Any = model_inputs['''input_ids'''] _a : Optional[Any] = model_inputs.get('''attention_mask''' , _a ) # Allow empty prompts if input_ids.shape[1] == 0: _a : int = None _a : int = None _a : List[str] = 1 else: _a : List[Any] = input_ids.shape[0] _a : Union[str, Any] = model_inputs.pop('''prompt_text''' ) # If there is a prefix, we may need to adjust the generation length. Do so without permanently modifying # generate_kwargs, as some of the parameterization may come from the initialization of the pipeline. _a : int = generate_kwargs.pop('''prefix_length''' , 0 ) if prefix_length > 0: _a : Tuple = '''max_new_tokens''' in generate_kwargs or ( '''generation_config''' in generate_kwargs and generate_kwargs['''generation_config'''].max_new_tokens is not None ) if not has_max_new_tokens: _a : int = generate_kwargs.get('''max_length''' ) or self.model.config.max_length generate_kwargs["max_length"] += prefix_length _a : Dict = '''min_new_tokens''' in generate_kwargs or ( '''generation_config''' in generate_kwargs and generate_kwargs['''generation_config'''].min_new_tokens is not None ) if not has_min_new_tokens and "min_length" in generate_kwargs: generate_kwargs["min_length"] += prefix_length # BS x SL _a : Optional[Any] = self.model.generate(input_ids=_a , attention_mask=_a , **_a ) _a : int = generated_sequence.shape[0] if self.framework == "pt": _a : Tuple = generated_sequence.reshape(_a , out_b // in_b , *generated_sequence.shape[1:] ) elif self.framework == "tf": _a : List[Any] = tf.reshape(_a , (in_b, out_b // in_b, *generated_sequence.shape[1:]) ) return {"generated_sequence": generated_sequence, "input_ids": input_ids, "prompt_text": prompt_text} def __lowercase ( self , _a , _a=ReturnType.FULL_TEXT , _a=True ) -> int: _a : Tuple = model_outputs['''generated_sequence'''][0] _a : int = model_outputs['''input_ids'''] _a : Any = model_outputs['''prompt_text'''] _a : Any = generated_sequence.numpy().tolist() _a : Any = [] for sequence in generated_sequence: if return_type == ReturnType.TENSORS: _a : Optional[int] = {'''generated_token_ids''': sequence} elif return_type in {ReturnType.NEW_TEXT, ReturnType.FULL_TEXT}: # Decode text _a : str = self.tokenizer.decode( _a , skip_special_tokens=_a , clean_up_tokenization_spaces=_a , ) # Remove PADDING prompt of the sequence if XLNet or Transfo-XL model is used if input_ids is None: _a : Union[str, Any] = 0 else: _a : str = len( self.tokenizer.decode( input_ids[0] , skip_special_tokens=_a , clean_up_tokenization_spaces=_a , ) ) if return_type == ReturnType.FULL_TEXT: _a : str = prompt_text + text[prompt_length:] else: _a : List[str] = text[prompt_length:] _a : Union[str, Any] = {'''generated_text''': all_text} records.append(_a ) return records
15
1
def _lowerCamelCase( lowercase__ ) -> list: '''simple docstring''' __lowercase= [0] * len(lowercase__ ) for i in range(1 , len(lowercase__ ) ): # use last results for better performance - dynamic programming __lowercase= prefix_result[i - 1] while j > 0 and input_string[i] != input_string[j]: __lowercase= prefix_result[j - 1] if input_string[i] == input_string[j]: j += 1 __lowercase= j return prefix_result def _lowerCamelCase( lowercase__ ) -> int: '''simple docstring''' return max(prefix_function(lowercase__ ) ) if __name__ == "__main__": import doctest doctest.testmod()
295
from dataclasses import dataclass from typing import Optional, Tuple, Union import torch import torch.nn as nn from ..configuration_utils import ConfigMixin, register_to_config from ..utils import BaseOutput, apply_forward_hook from .modeling_utils import ModelMixin from .vae import Decoder, DecoderOutput, Encoder, VectorQuantizer @dataclass class A ( A_ ): UpperCamelCase_ : torch.FloatTensor class A ( A_ , A_ ): @register_to_config def __init__(self , lowerCAmelCase = 3 , lowerCAmelCase = 3 , lowerCAmelCase = ("DownEncoderBlock2D",) , lowerCAmelCase = ("UpDecoderBlock2D",) , lowerCAmelCase = (6_4,) , lowerCAmelCase = 1 , lowerCAmelCase = "silu" , lowerCAmelCase = 3 , lowerCAmelCase = 3_2 , lowerCAmelCase = 2_5_6 , lowerCAmelCase = 3_2 , lowerCAmelCase = None , lowerCAmelCase = 0.1_82_15 , lowerCAmelCase = "group" , ): super().__init__() # pass init params to Encoder __lowercase= Encoder( in_channels=lowerCAmelCase , out_channels=lowerCAmelCase , down_block_types=lowerCAmelCase , block_out_channels=lowerCAmelCase , layers_per_block=lowerCAmelCase , act_fn=lowerCAmelCase , norm_num_groups=lowerCAmelCase , double_z=lowerCAmelCase , ) __lowercase= vq_embed_dim if vq_embed_dim is not None else latent_channels __lowercase= nn.Convad(lowerCAmelCase , lowerCAmelCase , 1 ) __lowercase= VectorQuantizer(lowerCAmelCase , lowerCAmelCase , beta=0.25 , remap=lowerCAmelCase , sane_index_shape=lowerCAmelCase ) __lowercase= nn.Convad(lowerCAmelCase , lowerCAmelCase , 1 ) # pass init params to Decoder __lowercase= Decoder( in_channels=lowerCAmelCase , out_channels=lowerCAmelCase , up_block_types=lowerCAmelCase , block_out_channels=lowerCAmelCase , layers_per_block=lowerCAmelCase , act_fn=lowerCAmelCase , norm_num_groups=lowerCAmelCase , norm_type=lowerCAmelCase , ) @apply_forward_hook def _A (self , lowerCAmelCase , lowerCAmelCase = True ): __lowercase= self.encoder(lowerCAmelCase ) __lowercase= self.quant_conv(lowerCAmelCase ) if not return_dict: return (h,) return VQEncoderOutput(latents=lowerCAmelCase ) @apply_forward_hook def _A (self , lowerCAmelCase , lowerCAmelCase = False , lowerCAmelCase = True ): # also go through quantization layer if not force_not_quantize: __lowercase, __lowercase, __lowercase= self.quantize(lowerCAmelCase ) else: __lowercase= h __lowercase= self.post_quant_conv(lowerCAmelCase ) __lowercase= self.decoder(lowerCAmelCase , quant if self.config.norm_type == 'spatial' else None ) if not return_dict: return (dec,) return DecoderOutput(sample=lowerCAmelCase ) def _A (self , lowerCAmelCase , lowerCAmelCase = True ): __lowercase= sample __lowercase= self.encode(lowerCAmelCase ).latents __lowercase= self.decode(lowerCAmelCase ).sample if not return_dict: return (dec,) return DecoderOutput(sample=lowerCAmelCase )
295
1
"""simple docstring""" import math def lowercase__ ( lowercase_ ,lowercase_ ) -> float: """simple docstring""" if ( not isinstance(lowercase_ ,(int, float) ) or power_factor < -1 or power_factor > 1 ): raise ValueError("power_factor must be a valid float value between -1 and 1." ) return apparent_power * power_factor def lowercase__ ( lowercase_ ,lowercase_ ) -> float: """simple docstring""" if ( not isinstance(lowercase_ ,(int, float) ) or power_factor < -1 or power_factor > 1 ): raise ValueError("power_factor must be a valid float value between -1 and 1." ) return apparent_power * math.sqrt(1 - power_factor**2 ) if __name__ == "__main__": import doctest doctest.testmod()
350
"""simple docstring""" import inspect import unittest from transformers import ViTConfig from transformers.testing_utils import ( require_accelerate, require_torch, require_torch_gpu, require_vision, slow, torch_device, ) from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTForImageClassification, ViTForMaskedImageModeling, ViTModel from transformers.models.vit.modeling_vit import VIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class __SCREAMING_SNAKE_CASE : '''simple docstring''' def __init__( self : Dict , __a : List[Any] , __a : str=13 , __a : Any=30 , __a : List[str]=2 , __a : Dict=3 , __a : Union[str, Any]=True , __a : Dict=True , __a : List[str]=32 , __a : Tuple=5 , __a : str=4 , __a : List[str]=37 , __a : Tuple="gelu" , __a : str=0.1 , __a : Optional[int]=0.1 , __a : Union[str, Any]=10 , __a : Optional[Any]=0.02 , __a : List[Any]=None , __a : str=2 , ) -> int: _UpperCamelCase : Tuple = parent _UpperCamelCase : str = batch_size _UpperCamelCase : Tuple = image_size _UpperCamelCase : List[str] = patch_size _UpperCamelCase : Dict = num_channels _UpperCamelCase : List[str] = is_training _UpperCamelCase : Any = use_labels _UpperCamelCase : int = hidden_size _UpperCamelCase : List[Any] = num_hidden_layers _UpperCamelCase : Union[str, Any] = num_attention_heads _UpperCamelCase : Optional[int] = intermediate_size _UpperCamelCase : Any = hidden_act _UpperCamelCase : Dict = hidden_dropout_prob _UpperCamelCase : Dict = attention_probs_dropout_prob _UpperCamelCase : Optional[int] = type_sequence_label_size _UpperCamelCase : int = initializer_range _UpperCamelCase : Optional[int] = scope _UpperCamelCase : Any = encoder_stride # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) _UpperCamelCase : Optional[int] = (image_size // patch_size) ** 2 _UpperCamelCase : Optional[int] = num_patches + 1 def __SCREAMING_SNAKE_CASE ( self : int ) -> Optional[Any]: _UpperCamelCase : Tuple = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) _UpperCamelCase : Union[str, Any] = None if self.use_labels: _UpperCamelCase : Optional[Any] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _UpperCamelCase : Any = self.get_config() return config, pixel_values, labels def __SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> List[str]: return ViTConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=__a , initializer_range=self.initializer_range , encoder_stride=self.encoder_stride , ) def __SCREAMING_SNAKE_CASE ( self : Tuple , __a : Optional[int] , __a : Union[str, Any] , __a : Tuple ) -> Union[str, Any]: _UpperCamelCase : Optional[Any] = ViTModel(config=__a ) model.to(__a ) model.eval() _UpperCamelCase : Tuple = model(__a ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __SCREAMING_SNAKE_CASE ( self : Dict , __a : str , __a : Optional[int] , __a : int ) -> Optional[int]: _UpperCamelCase : Tuple = ViTForMaskedImageModeling(config=__a ) model.to(__a ) model.eval() _UpperCamelCase : Any = model(__a ) self.parent.assertEqual( result.reconstruction.shape , (self.batch_size, self.num_channels, self.image_size, self.image_size) ) # test greyscale images _UpperCamelCase : Union[str, Any] = 1 _UpperCamelCase : Union[str, Any] = ViTForMaskedImageModeling(__a ) model.to(__a ) model.eval() _UpperCamelCase : List[Any] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) _UpperCamelCase : Dict = model(__a ) self.parent.assertEqual(result.reconstruction.shape , (self.batch_size, 1, self.image_size, self.image_size) ) def __SCREAMING_SNAKE_CASE ( self : List[Any] , __a : Tuple , __a : int , __a : Dict ) -> int: _UpperCamelCase : Any = self.type_sequence_label_size _UpperCamelCase : Optional[Any] = ViTForImageClassification(__a ) model.to(__a ) model.eval() _UpperCamelCase : int = model(__a , labels=__a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images _UpperCamelCase : Tuple = 1 _UpperCamelCase : Union[str, Any] = ViTForImageClassification(__a ) model.to(__a ) model.eval() _UpperCamelCase : Optional[int] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) _UpperCamelCase : List[Any] = model(__a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def __SCREAMING_SNAKE_CASE ( self : str ) -> Tuple: _UpperCamelCase : Dict = self.prepare_config_and_inputs() ( ( _UpperCamelCase ), ( _UpperCamelCase ), ( _UpperCamelCase ), ) : Union[str, Any] = config_and_inputs _UpperCamelCase : Union[str, Any] = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class __SCREAMING_SNAKE_CASE ( _UpperCamelCase , _UpperCamelCase , unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE__ :Optional[Any] = ( ( ViTModel, ViTForImageClassification, ViTForMaskedImageModeling, ) if is_torch_available() else () ) SCREAMING_SNAKE_CASE__ :Any = ( {"feature-extraction": ViTModel, "image-classification": ViTForImageClassification} if is_torch_available() else {} ) SCREAMING_SNAKE_CASE__ :str = True SCREAMING_SNAKE_CASE__ :List[Any] = False SCREAMING_SNAKE_CASE__ :int = False SCREAMING_SNAKE_CASE__ :int = False def __SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> List[Any]: _UpperCamelCase : Dict = ViTModelTester(self ) _UpperCamelCase : Any = ConfigTester(self , config_class=__a , has_text_modality=__a , hidden_size=37 ) def __SCREAMING_SNAKE_CASE ( self : str ) -> Optional[Any]: self.config_tester.run_common_tests() @unittest.skip(reason="ViT does not use inputs_embeds" ) def __SCREAMING_SNAKE_CASE ( self : int ) -> List[str]: pass def __SCREAMING_SNAKE_CASE ( self : List[Any] ) -> Union[str, Any]: _UpperCamelCase, _UpperCamelCase : Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _UpperCamelCase : List[Any] = model_class(__a ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) _UpperCamelCase : Any = model.get_output_embeddings() self.assertTrue(x is None or isinstance(__a , nn.Linear ) ) def __SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> Optional[Any]: _UpperCamelCase, _UpperCamelCase : Dict = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _UpperCamelCase : Any = model_class(__a ) _UpperCamelCase : Any = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _UpperCamelCase : List[str] = [*signature.parameters.keys()] _UpperCamelCase : Optional[Any] = ["pixel_values"] self.assertListEqual(arg_names[:1] , __a ) def __SCREAMING_SNAKE_CASE ( self : Any ) -> int: _UpperCamelCase : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__a ) def __SCREAMING_SNAKE_CASE ( self : str ) -> List[str]: _UpperCamelCase : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_image_modeling(*__a ) def __SCREAMING_SNAKE_CASE ( self : Dict ) -> Union[str, Any]: _UpperCamelCase : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*__a ) @slow def __SCREAMING_SNAKE_CASE ( self : str ) -> List[str]: for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _UpperCamelCase : List[str] = ViTModel.from_pretrained(__a ) self.assertIsNotNone(__a ) def lowercase__ ( ) -> str: """simple docstring""" _UpperCamelCase : Tuple = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_torch @require_vision class __SCREAMING_SNAKE_CASE ( unittest.TestCase ): '''simple docstring''' @cached_property def __SCREAMING_SNAKE_CASE ( self : List[Any] ) -> Optional[int]: return ViTImageProcessor.from_pretrained("google/vit-base-patch16-224" ) if is_vision_available() else None @slow def __SCREAMING_SNAKE_CASE ( self : Tuple ) -> Dict: _UpperCamelCase : List[Any] = ViTForImageClassification.from_pretrained("google/vit-base-patch16-224" ).to(__a ) _UpperCamelCase : str = self.default_image_processor _UpperCamelCase : List[Any] = prepare_img() _UpperCamelCase : Any = image_processor(images=__a , return_tensors="pt" ).to(__a ) # forward pass with torch.no_grad(): _UpperCamelCase : Dict = model(**__a ) # verify the logits _UpperCamelCase : Tuple = torch.Size((1, 1000) ) self.assertEqual(outputs.logits.shape , __a ) _UpperCamelCase : str = torch.tensor([-0.27_44, 0.82_15, -0.08_36] ).to(__a ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , __a , atol=1e-4 ) ) @slow def __SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> str: # ViT models have an `interpolate_pos_encoding` argument in their forward method, # allowing to interpolate the pre-trained position embeddings in order to use # the model on higher resolutions. The DINO model by Facebook AI leverages this # to visualize self-attention on higher resolution images. _UpperCamelCase : List[str] = ViTModel.from_pretrained("facebook/dino-vits8" ).to(__a ) _UpperCamelCase : Union[str, Any] = ViTImageProcessor.from_pretrained("facebook/dino-vits8" , size=480 ) _UpperCamelCase : List[str] = prepare_img() _UpperCamelCase : int = image_processor(images=__a , return_tensors="pt" ) _UpperCamelCase : Any = inputs.pixel_values.to(__a ) # forward pass with torch.no_grad(): _UpperCamelCase : str = model(__a , interpolate_pos_encoding=__a ) # verify the logits _UpperCamelCase : int = torch.Size((1, 3601, 384) ) self.assertEqual(outputs.last_hidden_state.shape , __a ) _UpperCamelCase : int = torch.tensor( [[4.23_40, 4.39_06, -6.66_92], [4.54_63, 1.89_28, -6.72_57], [4.44_29, 0.84_96, -5.85_85]] ).to(__a ) self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3] , __a , atol=1e-4 ) ) @slow @require_accelerate @require_torch_gpu def __SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Any: _UpperCamelCase : Tuple = ViTModel.from_pretrained("facebook/dino-vits8" , torch_dtype=torch.floataa , device_map="auto" ) _UpperCamelCase : int = self.default_image_processor _UpperCamelCase : Dict = prepare_img() _UpperCamelCase : Union[str, Any] = image_processor(images=__a , return_tensors="pt" ) _UpperCamelCase : Any = inputs.pixel_values.to(__a ) # forward pass to make sure inference works in fp16 with torch.no_grad(): _UpperCamelCase : int = model(__a )
310
0
import logging from pathlib import Path import numpy as np import pytorch_lightning as pl import torch from pytorch_lightning.callbacks import EarlyStopping, ModelCheckpoint from pytorch_lightning.utilities import rank_zero_only from utils_rag import save_json def SCREAMING_SNAKE_CASE_ ( __magic_name__ : Any ) -> int: """simple docstring""" UpperCamelCase :int = filter(lambda __magic_name__ : p.requires_grad , model.parameters() ) UpperCamelCase :List[str] = sum([np.prod(p.size() ) for p in model_parameters] ) return params UpperCAmelCase_ : str = logging.getLogger(__name__) def SCREAMING_SNAKE_CASE_ ( __magic_name__ : Union[str, Any] , __magic_name__ : Dict ) -> Dict: """simple docstring""" if metric == "rouge2": UpperCamelCase :Tuple = """{val_avg_rouge2:.4f}-{step_count}""" elif metric == "bleu": UpperCamelCase :List[Any] = """{val_avg_bleu:.4f}-{step_count}""" elif metric == "em": UpperCamelCase :Tuple = """{val_avg_em:.4f}-{step_count}""" elif metric == "loss": UpperCamelCase :Dict = """{val_avg_loss:.4f}-{step_count}""" else: raise NotImplementedError( f"""seq2seq callbacks only support rouge2 and bleu, got {metric}, You can make your own by adding to this""" """ function.""" ) UpperCamelCase :List[str] = ModelCheckpoint( dirpath=__magic_name__ , filename=__magic_name__ , monitor=f"""val_{metric}""" , mode="""max""" , save_top_k=1 , every_n_epochs=1 , ) return checkpoint_callback def SCREAMING_SNAKE_CASE_ ( __magic_name__ : Any , __magic_name__ : str ) -> Optional[int]: """simple docstring""" return EarlyStopping( monitor=f"""val_{metric}""" , mode="""min""" if """loss""" in metric else """max""" , patience=__magic_name__ , verbose=__magic_name__ , ) class _SCREAMING_SNAKE_CASE ( pl.Callback ): def _A ( self : str , __lowerCamelCase : List[Any] , __lowerCamelCase : Any ): UpperCamelCase :Optional[int] = {F"""lr_group_{i}""": param["""lr"""] for i, param in enumerate(pl_module.trainer.optimizers[0].param_groups )} pl_module.logger.log_metrics(__lowerCamelCase ) @rank_zero_only def _A ( self : List[str] , __lowerCamelCase : pl.Trainer , __lowerCamelCase : pl.LightningModule , __lowerCamelCase : str , __lowerCamelCase : List[str]=True ): logger.info(F"""***** {type_path} results at step {trainer.global_step:05d} *****""" ) UpperCamelCase :str = trainer.callback_metrics trainer.logger.log_metrics({k: v for k, v in metrics.items() if k not in ["""log""", """progress_bar""", """preds"""]} ) # Log results UpperCamelCase :Union[str, Any] = Path(pl_module.hparams.output_dir ) if type_path == "test": UpperCamelCase :Dict = od / """test_results.txt""" UpperCamelCase :str = od / """test_generations.txt""" else: # this never gets hit. I prefer not to save intermediate generations, and results are in metrics.json # If people want this it will be easy enough to add back. UpperCamelCase :Any = od / F"""{type_path}_results/{trainer.global_step:05d}.txt""" UpperCamelCase :List[str] = od / F"""{type_path}_generations/{trainer.global_step:05d}.txt""" results_file.parent.mkdir(exist_ok=__lowerCamelCase ) generations_file.parent.mkdir(exist_ok=__lowerCamelCase ) with open(__lowerCamelCase , """a+""" ) as writer: for key in sorted(__lowerCamelCase ): if key in ["log", "progress_bar", "preds"]: continue UpperCamelCase :int = metrics[key] if isinstance(__lowerCamelCase , torch.Tensor ): UpperCamelCase :Any = val.item() UpperCamelCase :Union[str, Any] = F"""{key}: {val:.6f}\n""" writer.write(__lowerCamelCase ) if not save_generations: return if "preds" in metrics: UpperCamelCase :Any = """\n""".join(metrics["""preds"""] ) generations_file.open("""w+""" ).write(__lowerCamelCase ) @rank_zero_only def _A ( self : str , __lowerCamelCase : Optional[Any] , __lowerCamelCase : str ): try: UpperCamelCase :Union[str, Any] = pl_module.model.model.num_parameters() except AttributeError: UpperCamelCase :int = pl_module.model.num_parameters() UpperCamelCase :Union[str, Any] = count_trainable_parameters(__lowerCamelCase ) # mp stands for million parameters trainer.logger.log_metrics({"""n_params""": npars, """mp""": npars / 1E6, """grad_mp""": n_trainable_pars / 1E6} ) @rank_zero_only def _A ( self : List[str] , __lowerCamelCase : pl.Trainer , __lowerCamelCase : pl.LightningModule ): save_json(pl_module.metrics , pl_module.metrics_save_path ) return self._write_logs(__lowerCamelCase , __lowerCamelCase , """test""" ) @rank_zero_only def _A ( self : Optional[int] , __lowerCamelCase : pl.Trainer , __lowerCamelCase : Union[str, Any] ): save_json(pl_module.metrics , pl_module.metrics_save_path ) # Uncommenting this will save val generations # return self._write_logs(trainer, pl_module, "valid")
38
from typing import Dict from .base import GenericTensor, Pipeline class __A( a ): def SCREAMING_SNAKE_CASE_ ( self , _snake_case=None , _snake_case=None , _snake_case=None , **_snake_case ) -> Optional[Any]: '''simple docstring''' if tokenize_kwargs is None: __a = {} if truncation is not None: if "truncation" in tokenize_kwargs: raise ValueError( '''truncation parameter defined twice (given as keyword argument as well as in tokenize_kwargs)''' ) __a = truncation __a = tokenize_kwargs __a = {} if return_tensors is not None: __a = return_tensors return preprocess_params, {}, postprocess_params def SCREAMING_SNAKE_CASE_ ( self , _snake_case , **_snake_case ) -> Dict[str, GenericTensor]: '''simple docstring''' __a = self.framework __a = self.tokenizer(_snake_case , return_tensors=_snake_case , **_snake_case ) return model_inputs def SCREAMING_SNAKE_CASE_ ( self , _snake_case ) -> Optional[Any]: '''simple docstring''' __a = self.model(**_snake_case ) return model_outputs def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case=False ) -> Optional[int]: '''simple docstring''' if return_tensors: return model_outputs[0] if self.framework == "pt": return model_outputs[0].tolist() elif self.framework == "tf": return model_outputs[0].numpy().tolist() def __call__( self , *_snake_case , **_snake_case ) -> Any: '''simple docstring''' return super().__call__(*_snake_case , **_snake_case )
6
0
'''simple docstring''' import cmath import math def _snake_case ( _SCREAMING_SNAKE_CASE : float , _SCREAMING_SNAKE_CASE : float , _SCREAMING_SNAKE_CASE : float , _SCREAMING_SNAKE_CASE : float ) -> complex: """simple docstring""" lowerCAmelCase = math.radians(_SCREAMING_SNAKE_CASE ) lowerCAmelCase = math.radians(_SCREAMING_SNAKE_CASE ) # Convert voltage and current to rectangular form lowerCAmelCase = cmath.rect(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) lowerCAmelCase = cmath.rect(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # Calculate apparent power return voltage_rect * current_rect if __name__ == "__main__": import doctest doctest.testmod()
187
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCAmelCase = logging.get_logger(__name__) class __snake_case( _lowerCAmelCase ): '''simple docstring''' UpperCAmelCase : Optional[int] = "timm_backbone" def __init__( self , A_=None , A_=3 , A_=True , A_=True , A_=None , **A_ , ) -> int: super().__init__(**A_ ) lowerCAmelCase = backbone lowerCAmelCase = num_channels lowerCAmelCase = features_only lowerCAmelCase = use_pretrained_backbone lowerCAmelCase = True lowerCAmelCase = out_indices if out_indices is not None else (-1,)
187
1
'''simple docstring''' import argparse import numpy as np import torch from transformers import SpeechTaHifiGan, SpeechTaHifiGanConfig, logging logging.set_verbosity_info() lowerCAmelCase: Union[str, Any] = logging.get_logger('transformers.models.speecht5') def lowerCamelCase__ ( _A , _A , _A ): hf_model.apply_weight_norm() a : str = checkpoint['input_conv.weight_g'] a : Union[str, Any] = checkpoint['input_conv.weight_v'] a : Tuple = checkpoint['input_conv.bias'] for i in range(len(config.upsample_rates ) ): a : List[str] = checkpoint[f"""upsamples.{i}.1.weight_g"""] a : Union[str, Any] = checkpoint[f"""upsamples.{i}.1.weight_v"""] a : List[Any] = checkpoint[f"""upsamples.{i}.1.bias"""] for i in range(len(config.upsample_rates ) * len(config.resblock_kernel_sizes ) ): for j in range(len(config.resblock_dilation_sizes ) ): a : Optional[Any] = checkpoint[f"""blocks.{i}.convs1.{j}.1.weight_g"""] a : Dict = checkpoint[f"""blocks.{i}.convs1.{j}.1.weight_v"""] a : List[Any] = checkpoint[f"""blocks.{i}.convs1.{j}.1.bias"""] a : int = checkpoint[f"""blocks.{i}.convs2.{j}.1.weight_g"""] a : Tuple = checkpoint[f"""blocks.{i}.convs2.{j}.1.weight_v"""] a : Dict = checkpoint[f"""blocks.{i}.convs2.{j}.1.bias"""] a : List[Any] = checkpoint['output_conv.1.weight_g'] a : Optional[Any] = checkpoint['output_conv.1.weight_v'] a : Union[str, Any] = checkpoint['output_conv.1.bias'] hf_model.remove_weight_norm() @torch.no_grad() def lowerCamelCase__ ( _A , _A , _A , _A=None , _A=None , ): if config_path is not None: a : Any = SpeechTaHifiGanConfig.from_pretrained(A__ ) else: a : Any = SpeechTaHifiGanConfig() a : List[str] = SpeechTaHifiGan(A__ ) a : List[str] = torch.load(A__ ) load_weights(orig_checkpoint['model']['generator'] , A__ , A__ ) a : Optional[int] = np.load(A__ ) a : List[str] = stats[0].reshape(-1 ) a : Union[str, Any] = stats[1].reshape(-1 ) a : Optional[Any] = torch.from_numpy(A__ ).float() a : int = torch.from_numpy(A__ ).float() model.save_pretrained(A__ ) if repo_id: print('Pushing to the hub...' ) model.push_to_hub(A__ ) if __name__ == "__main__": lowerCAmelCase: Optional[Any] = argparse.ArgumentParser() parser.add_argument('--checkpoint_path', required=True, default=None, type=str, help='Path to original checkpoint') parser.add_argument('--stats_path', required=True, default=None, type=str, help='Path to stats.npy file') 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_hifigan_checkpoint( args.checkpoint_path, args.stats_path, args.pytorch_dump_folder_path, args.config_path, args.push_to_hub, )
297
'''simple docstring''' def __lowerCamelCase ( A__ ) -> list: """simple docstring""" UpperCamelCase = len(A__ ) for i in range(1 , A__ ): UpperCamelCase = collection[i] UpperCamelCase = 0 UpperCamelCase = i - 1 while low <= high: UpperCamelCase = (low + high) // 2 if val < collection[mid]: UpperCamelCase = mid - 1 else: UpperCamelCase = mid + 1 for j in range(A__ , A__ , -1 ): UpperCamelCase = collection[j - 1] UpperCamelCase = val return collection if __name__ == "__main__": _lowerCamelCase : int = input("Enter numbers separated by a comma:\n").strip() _lowerCamelCase : Union[str, Any] = [int(item) for item in user_input.split(",")] print(binary_insertion_sort(unsorted))
28
0
import gc import random import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer import diffusers from diffusers import ( AutoencoderKL, EulerDiscreteScheduler, StableDiffusionLatentUpscalePipeline, StableDiffusionPipeline, UNetaDConditionModel, ) from diffusers.schedulers import KarrasDiffusionSchedulers from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..pipeline_params import TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() def _a ( lowerCamelCase: Union[str, Any] ) -> Any: '''simple docstring''' __A = [tensor.shape for tensor in tensor_list] return all(shape == shapes[0] for shape in shapes[1:] ) class A_ ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , unittest.TestCase ): lowerCAmelCase__ = StableDiffusionLatentUpscalePipeline lowerCAmelCase__ = TEXT_GUIDED_IMAGE_VARIATION_PARAMS - { """height""", """width""", """cross_attention_kwargs""", """negative_prompt_embeds""", """prompt_embeds""", } lowerCAmelCase__ = PipelineTesterMixin.required_optional_params - {"""num_images_per_prompt"""} lowerCAmelCase__ = TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS lowerCAmelCase__ = frozenset( [] ) # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess lowerCAmelCase__ = frozenset([] ) lowerCAmelCase__ = True @property def _lowerCAmelCase (self :Tuple )-> Tuple: __A = 1 __A = 4 __A = (16, 16) __A = floats_tensor((batch_size, num_channels) + sizes , rng=random.Random(0 ) ).to(_UpperCamelCase ) return image def _lowerCAmelCase (self :int )-> str: torch.manual_seed(0 ) __A = UNetaDConditionModel( act_fn='''gelu''' , attention_head_dim=8 , norm_num_groups=_UpperCamelCase , block_out_channels=[32, 32, 64, 64] , time_cond_proj_dim=160 , conv_in_kernel=1 , conv_out_kernel=1 , cross_attention_dim=32 , down_block_types=( '''KDownBlock2D''', '''KCrossAttnDownBlock2D''', '''KCrossAttnDownBlock2D''', '''KCrossAttnDownBlock2D''', ) , in_channels=8 , mid_block_type=_UpperCamelCase , only_cross_attention=_UpperCamelCase , out_channels=5 , resnet_time_scale_shift='''scale_shift''' , time_embedding_type='''fourier''' , timestep_post_act='''gelu''' , up_block_types=('''KCrossAttnUpBlock2D''', '''KCrossAttnUpBlock2D''', '''KCrossAttnUpBlock2D''', '''KUpBlock2D''') , ) __A = AutoencoderKL( block_out_channels=[32, 32, 64, 64] , in_channels=3 , out_channels=3 , down_block_types=[ '''DownEncoderBlock2D''', '''DownEncoderBlock2D''', '''DownEncoderBlock2D''', '''DownEncoderBlock2D''', ] , up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D''', '''UpDecoderBlock2D''', '''UpDecoderBlock2D'''] , latent_channels=4 , ) __A = EulerDiscreteScheduler(prediction_type='''sample''' ) __A = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , hidden_act='''quick_gelu''' , projection_dim=512 , ) __A = CLIPTextModel(_UpperCamelCase ) __A = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) __A = { '''unet''': model.eval(), '''vae''': vae.eval(), '''scheduler''': scheduler, '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, } return components def _lowerCAmelCase (self :Optional[Any] , _UpperCamelCase :List[str] , _UpperCamelCase :Any=0 )-> Any: if str(_UpperCamelCase ).startswith('''mps''' ): __A = torch.manual_seed(_UpperCamelCase ) else: __A = torch.Generator(device=_UpperCamelCase ).manual_seed(_UpperCamelCase ) __A = { '''prompt''': '''A painting of a squirrel eating a burger''', '''image''': self.dummy_image.cpu(), '''generator''': generator, '''num_inference_steps''': 2, '''output_type''': '''numpy''', } return inputs def _lowerCAmelCase (self :Dict )-> Dict: __A = '''cpu''' __A = self.get_dummy_components() __A = self.pipeline_class(**_UpperCamelCase ) pipe.to(_UpperCamelCase ) pipe.set_progress_bar_config(disable=_UpperCamelCase ) __A = self.get_dummy_inputs(_UpperCamelCase ) __A = pipe(**_UpperCamelCase ).images __A = image[0, -3:, -3:, -1] self.assertEqual(image.shape , (1, 256, 256, 3) ) __A = np.array( [0.4_7_2_2_2_4_1_2, 0.4_1_9_2_1_6_3_3, 0.4_4_7_1_7_4_3_4, 0.4_6_8_7_4_1_9_2, 0.4_2_5_8_8_2_5_8, 0.4_6_1_5_0_7_2_6, 0.4_6_7_7_5_3_4, 0.4_5_5_8_3_8_3_2, 0.4_8_5_7_9_0_5_5] ) __A = np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(_UpperCamelCase , 1e-3 ) def _lowerCAmelCase (self :str )-> Tuple: super().test_attention_slicing_forward_pass(expected_max_diff=7e-3 ) def _lowerCAmelCase (self :str )-> Union[str, Any]: super().test_cpu_offload_forward_pass(expected_max_diff=3e-3 ) def _lowerCAmelCase (self :Any )-> Any: super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-3 ) def _lowerCAmelCase (self :int )-> Union[str, Any]: super().test_inference_batch_single_identical(expected_max_diff=7e-3 ) def _lowerCAmelCase (self :Tuple )-> Any: super().test_pt_np_pil_outputs_equivalent(expected_max_diff=3e-3 ) def _lowerCAmelCase (self :Dict )-> Union[str, Any]: super().test_save_load_local(expected_max_difference=3e-3 ) def _lowerCAmelCase (self :int )-> Any: super().test_save_load_optional_components(expected_max_difference=3e-3 ) def _lowerCAmelCase (self :int )-> int: __A = [ '''DDIMScheduler''', '''DDPMScheduler''', '''PNDMScheduler''', '''HeunDiscreteScheduler''', '''EulerAncestralDiscreteScheduler''', '''KDPM2DiscreteScheduler''', '''KDPM2AncestralDiscreteScheduler''', '''DPMSolverSDEScheduler''', ] __A = self.get_dummy_components() __A = self.pipeline_class(**_UpperCamelCase ) # make sure that PNDM does not need warm-up pipe.scheduler.register_to_config(skip_prk_steps=_UpperCamelCase ) pipe.to(_UpperCamelCase ) pipe.set_progress_bar_config(disable=_UpperCamelCase ) __A = self.get_dummy_inputs(_UpperCamelCase ) __A = 2 __A = [] for scheduler_enum in KarrasDiffusionSchedulers: if scheduler_enum.name in skip_schedulers: # no sigma schedulers are not supported # no schedulers continue __A = getattr(_UpperCamelCase , scheduler_enum.name ) __A = scheduler_cls.from_config(pipe.scheduler.config ) __A = pipe(**_UpperCamelCase )[0] outputs.append(_UpperCamelCase ) assert check_same_shape(_UpperCamelCase ) @require_torch_gpu @slow class A_ ( unittest.TestCase ): def _lowerCAmelCase (self :List[str] )-> str: super().tearDown() gc.collect() torch.cuda.empty_cache() def _lowerCAmelCase (self :Optional[Any] )-> Any: __A = torch.manual_seed(33 ) __A = StableDiffusionPipeline.from_pretrained('''CompVis/stable-diffusion-v1-4''' , torch_dtype=torch.floataa ) pipe.to('''cuda''' ) __A = StableDiffusionLatentUpscalePipeline.from_pretrained( '''stabilityai/sd-x2-latent-upscaler''' , torch_dtype=torch.floataa ) upscaler.to('''cuda''' ) __A = '''a photo of an astronaut high resolution, unreal engine, ultra realistic''' __A = pipe(_UpperCamelCase , generator=_UpperCamelCase , output_type='''latent''' ).images __A = upscaler( prompt=_UpperCamelCase , image=_UpperCamelCase , num_inference_steps=20 , guidance_scale=0 , generator=_UpperCamelCase , output_type='''np''' , ).images[0] __A = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/latent-upscaler/astronaut_1024.npy''' ) assert np.abs((expected_image - image).mean() ) < 5e-2 def _lowerCAmelCase (self :Tuple )-> Dict: __A = torch.manual_seed(33 ) __A = StableDiffusionLatentUpscalePipeline.from_pretrained( '''stabilityai/sd-x2-latent-upscaler''' , torch_dtype=torch.floataa ) upscaler.to('''cuda''' ) __A = '''the temple of fire by Ross Tran and Gerardo Dottori, oil on canvas''' __A = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/latent-upscaler/fire_temple_512.png''' ) __A = upscaler( prompt=_UpperCamelCase , image=_UpperCamelCase , num_inference_steps=20 , guidance_scale=0 , generator=_UpperCamelCase , output_type='''np''' , ).images[0] __A = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/latent-upscaler/fire_temple_1024.npy''' ) assert np.abs((expected_image - image).max() ) < 5e-2
250
import argparse import requests import torch from PIL import Image from torchvision.transforms import Compose, Normalize, Resize, ToTensor from transformers import SwinaSRConfig, SwinaSRForImageSuperResolution, SwinaSRImageProcessor def _a ( lowerCamelCase: List[str] ) -> Tuple: '''simple docstring''' __A = SwinaSRConfig() if "Swin2SR_ClassicalSR_X4_64" in checkpoint_url: __A = 4 elif "Swin2SR_CompressedSR_X4_48" in checkpoint_url: __A = 4 __A = 48 __A = '''pixelshuffle_aux''' elif "Swin2SR_Lightweight_X2_64" in checkpoint_url: __A = [6, 6, 6, 6] __A = 60 __A = [6, 6, 6, 6] __A = '''pixelshuffledirect''' elif "Swin2SR_RealworldSR_X4_64_BSRGAN_PSNR" in checkpoint_url: __A = 4 __A = '''nearest+conv''' elif "Swin2SR_Jpeg_dynamic" in checkpoint_url: __A = 1 __A = 1 __A = 1_26 __A = 7 __A = 255.0 __A = '''''' return config def _a ( lowerCamelCase: List[Any] , lowerCamelCase: Optional[int] ) -> Optional[int]: '''simple docstring''' if "patch_embed.proj" in name and "layers" not in name: __A = name.replace('''patch_embed.proj''' , '''embeddings.patch_embeddings.projection''' ) if "patch_embed.norm" in name: __A = name.replace('''patch_embed.norm''' , '''embeddings.patch_embeddings.layernorm''' ) if "layers" in name: __A = name.replace('''layers''' , '''encoder.stages''' ) if "residual_group.blocks" in name: __A = name.replace('''residual_group.blocks''' , '''layers''' ) if "attn.proj" in name: __A = name.replace('''attn.proj''' , '''attention.output.dense''' ) if "attn" in name: __A = name.replace('''attn''' , '''attention.self''' ) if "norm1" in name: __A = name.replace('''norm1''' , '''layernorm_before''' ) if "norm2" in name: __A = name.replace('''norm2''' , '''layernorm_after''' ) if "mlp.fc1" in name: __A = name.replace('''mlp.fc1''' , '''intermediate.dense''' ) if "mlp.fc2" in name: __A = name.replace('''mlp.fc2''' , '''output.dense''' ) if "q_bias" in name: __A = name.replace('''q_bias''' , '''query.bias''' ) if "k_bias" in name: __A = name.replace('''k_bias''' , '''key.bias''' ) if "v_bias" in name: __A = name.replace('''v_bias''' , '''value.bias''' ) if "cpb_mlp" in name: __A = name.replace('''cpb_mlp''' , '''continuous_position_bias_mlp''' ) if "patch_embed.proj" in name: __A = name.replace('''patch_embed.proj''' , '''patch_embed.projection''' ) if name == "norm.weight": __A = '''layernorm.weight''' if name == "norm.bias": __A = '''layernorm.bias''' if "conv_first" in name: __A = name.replace('''conv_first''' , '''first_convolution''' ) if ( "upsample" in name or "conv_before_upsample" in name or "conv_bicubic" in name or "conv_up" in name or "conv_hr" in name or "conv_last" in name or "aux" in name ): # heads if "conv_last" in name: __A = name.replace('''conv_last''' , '''final_convolution''' ) if config.upsampler in ["pixelshuffle", "pixelshuffle_aux", "nearest+conv"]: if "conv_before_upsample.0" in name: __A = name.replace('''conv_before_upsample.0''' , '''conv_before_upsample''' ) if "upsample.0" in name: __A = name.replace('''upsample.0''' , '''upsample.convolution_0''' ) if "upsample.2" in name: __A = name.replace('''upsample.2''' , '''upsample.convolution_1''' ) __A = '''upsample.''' + name elif config.upsampler == "pixelshuffledirect": __A = name.replace('''upsample.0.weight''' , '''upsample.conv.weight''' ) __A = name.replace('''upsample.0.bias''' , '''upsample.conv.bias''' ) else: pass else: __A = '''swin2sr.''' + name return name def _a ( lowerCamelCase: int , lowerCamelCase: Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' for key in orig_state_dict.copy().keys(): __A = orig_state_dict.pop(lowerCamelCase ) if "qkv" in key: __A = key.split('''.''' ) __A = int(key_split[1] ) __A = int(key_split[4] ) __A = config.embed_dim if "weight" in key: __A = val[:dim, :] __A = val[dim : dim * 2, :] __A = val[-dim:, :] else: __A = val[:dim] __A = val[dim : dim * 2] __A = val[-dim:] pass else: __A = val return orig_state_dict def _a ( lowerCamelCase: List[Any] , lowerCamelCase: int , lowerCamelCase: Optional[int] ) -> List[Any]: '''simple docstring''' __A = get_config(lowerCamelCase ) __A = SwinaSRForImageSuperResolution(lowerCamelCase ) model.eval() __A = torch.hub.load_state_dict_from_url(lowerCamelCase , map_location='''cpu''' ) __A = convert_state_dict(lowerCamelCase , lowerCamelCase ) __A , __A = model.load_state_dict(lowerCamelCase , strict=lowerCamelCase ) if len(lowerCamelCase ) > 0: raise ValueError('''Missing keys when converting: {}'''.format(lowerCamelCase ) ) for key in unexpected_keys: if not ("relative_position_index" in key or "relative_coords_table" in key or "self_mask" in key): raise ValueError(F"""Unexpected key {key} in state_dict""" ) # verify values __A = '''https://github.com/mv-lab/swin2sr/blob/main/testsets/real-inputs/shanghai.jpg?raw=true''' __A = Image.open(requests.get(lowerCamelCase , stream=lowerCamelCase ).raw ).convert('''RGB''' ) __A = SwinaSRImageProcessor() # pixel_values = processor(image, return_tensors="pt").pixel_values __A = 1_26 if '''Jpeg''' in checkpoint_url else 2_56 __A = Compose( [ Resize((image_size, image_size) ), ToTensor(), Normalize(mean=[0.485, 0.456, 0.406] , std=[0.229, 0.224, 0.225] ), ] ) __A = transforms(lowerCamelCase ).unsqueeze(0 ) if config.num_channels == 1: __A = pixel_values[:, 0, :, :].unsqueeze(1 ) __A = model(lowerCamelCase ) # assert values if "Swin2SR_ClassicalSR_X2_64" in checkpoint_url: __A = torch.Size([1, 3, 5_12, 5_12] ) __A = torch.tensor( [[-0.7_087, -0.7_138, -0.6_721], [-0.8_340, -0.8_095, -0.7_298], [-0.9_149, -0.8_414, -0.7_940]] ) elif "Swin2SR_ClassicalSR_X4_64" in checkpoint_url: __A = torch.Size([1, 3, 10_24, 10_24] ) __A = torch.tensor( [[-0.7_775, -0.8_105, -0.8_933], [-0.7_764, -0.8_356, -0.9_225], [-0.7_976, -0.8_686, -0.9_579]] ) elif "Swin2SR_CompressedSR_X4_48" in checkpoint_url: # TODO values didn't match exactly here __A = torch.Size([1, 3, 10_24, 10_24] ) __A = torch.tensor( [[-0.8_035, -0.7_504, -0.7_491], [-0.8_538, -0.8_124, -0.7_782], [-0.8_804, -0.8_651, -0.8_493]] ) elif "Swin2SR_Lightweight_X2_64" in checkpoint_url: __A = torch.Size([1, 3, 5_12, 5_12] ) __A = torch.tensor( [[-0.7_669, -0.8_662, -0.8_767], [-0.8_810, -0.9_962, -0.9_820], [-0.9_340, -1.0_322, -1.1_149]] ) elif "Swin2SR_RealworldSR_X4_64_BSRGAN_PSNR" in checkpoint_url: __A = torch.Size([1, 3, 10_24, 10_24] ) __A = torch.tensor( [[-0.5_238, -0.5_557, -0.6_321], [-0.6_016, -0.5_903, -0.6_391], [-0.6_244, -0.6_334, -0.6_889]] ) assert ( outputs.reconstruction.shape == expected_shape ), F"""Shape of reconstruction should be {expected_shape}, but is {outputs.reconstruction.shape}""" assert torch.allclose(outputs.reconstruction[0, 0, :3, :3] , lowerCamelCase , atol=1e-3 ) print('''Looks ok!''' ) __A = { '''https://github.com/mv-lab/swin2sr/releases/download/v0.0.1/Swin2SR_ClassicalSR_X2_64.pth''': ( '''swin2SR-classical-sr-x2-64''' ), '''https://github.com/mv-lab/swin2sr/releases/download/v0.0.1/Swin2SR_ClassicalSR_X4_64.pth''': ( '''swin2SR-classical-sr-x4-64''' ), '''https://github.com/mv-lab/swin2sr/releases/download/v0.0.1/Swin2SR_CompressedSR_X4_48.pth''': ( '''swin2SR-compressed-sr-x4-48''' ), '''https://github.com/mv-lab/swin2sr/releases/download/v0.0.1/Swin2SR_Lightweight_X2_64.pth''': ( '''swin2SR-lightweight-x2-64''' ), '''https://github.com/mv-lab/swin2sr/releases/download/v0.0.1/Swin2SR_RealworldSR_X4_64_BSRGAN_PSNR.pth''': ( '''swin2SR-realworld-sr-x4-64-bsrgan-psnr''' ), } __A = url_to_name[checkpoint_url] if pytorch_dump_folder_path is not None: print(F"""Saving model {model_name} to {pytorch_dump_folder_path}""" ) model.save_pretrained(lowerCamelCase ) print(F"""Saving image processor to {pytorch_dump_folder_path}""" ) processor.save_pretrained(lowerCamelCase ) if push_to_hub: model.push_to_hub(F"""caidas/{model_name}""" ) processor.push_to_hub(F"""caidas/{model_name}""" ) if __name__ == "__main__": snake_case__ : str = argparse.ArgumentParser() # Required parameters parser.add_argument( '--checkpoint_url', default='https://github.com/mv-lab/swin2sr/releases/download/v0.0.1/Swin2SR_ClassicalSR_X2_64.pth', type=str, help='URL of the original Swin2SR checkpoint you\'d like to convert.', ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model directory.' ) parser.add_argument('--push_to_hub', action='store_true', help='Whether to push the converted model to the hub.') snake_case__ : str = parser.parse_args() convert_swinasr_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path, args.push_to_hub)
250
1
import json from typing import Dict, List, Optional, Tuple, Union from tokenizers import pre_tokenizers, processors from ...tokenization_utils_base import AddedToken, BatchEncoding, EncodedInput from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import PaddingStrategy, logging from .tokenization_led import LEDTokenizer snake_case : Union[str, Any] = logging.get_logger(__name__) snake_case : Union[str, Any] = {"vocab_file": "vocab.json", "merges_file": "merges.txt", "tokenizer_file": "tokenizer.json"} snake_case : Any = { "vocab_file": { "allenai/led-base-16384": "https://huggingface.co/allenai/led-base-16384/resolve/main/vocab.json", }, "merges_file": { "allenai/led-base-16384": "https://huggingface.co/allenai/led-base-16384/resolve/main/merges.txt", }, "tokenizer_file": { "allenai/led-base-16384": "https://huggingface.co/allenai/led-base-16384/resolve/main/tokenizer.json", }, } snake_case : int = { "allenai/led-base-16384": 1_63_84, } class snake_case_ (a_ ): UpperCAmelCase__ : Any = VOCAB_FILES_NAMES UpperCAmelCase__ : Tuple = PRETRAINED_VOCAB_FILES_MAP UpperCAmelCase__ : int = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCAmelCase__ : Any = LEDTokenizer UpperCAmelCase__ : Any = ['''input_ids''', '''attention_mask'''] def __init__( self :Optional[int] ,__snake_case :str=None ,__snake_case :str=None ,__snake_case :Dict=None ,__snake_case :Optional[Any]="replace" ,__snake_case :Dict="<s>" ,__snake_case :Dict="</s>" ,__snake_case :Dict="</s>" ,__snake_case :Optional[int]="<s>" ,__snake_case :List[str]="<unk>" ,__snake_case :Union[str, Any]="<pad>" ,__snake_case :int="<mask>" ,__snake_case :List[str]=False ,__snake_case :List[str]=True ,**__snake_case :Any ,) -> Optional[int]: super().__init__( _A ,_A ,tokenizer_file=_A ,errors=_A ,bos_token=_A ,eos_token=_A ,sep_token=_A ,cls_token=_A ,unk_token=_A ,pad_token=_A ,mask_token=_A ,add_prefix_space=_A ,trim_offsets=_A ,**_A ,) a__ = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get('add_prefix_space' ,_A ) != add_prefix_space: a__ = getattr(_A ,pre_tok_state.pop('type' ) ) a__ = add_prefix_space a__ = pre_tok_class(**_A ) a__ = add_prefix_space # the pre_tokenizer is already updated in the GPT2TokenizerFast `__init__` a__ = 'post_processor' a__ = getattr(self.backend_tokenizer ,_A ,_A ) if tokenizer_component_instance: a__ = json.loads(tokenizer_component_instance.__getstate__() ) # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class` if "sep" in state: a__ = tuple(state['sep'] ) if "cls" in state: a__ = tuple(state['cls'] ) a__ = False if state.get('add_prefix_space' ,_A ) != add_prefix_space: a__ = add_prefix_space a__ = True if state.get('trim_offsets' ,_A ) != trim_offsets: a__ = trim_offsets a__ = True if changes_to_apply: a__ = getattr(_A ,state.pop('type' ) ) a__ = component_class(**_A ) setattr(self.backend_tokenizer ,_A ,_A ) @property # Copied from transformers.models.bart.tokenization_bart_fast.BartTokenizerFast.mask_token with BART->LED def lowerCamelCase__( self :List[str] ) -> List[Any]: if self._mask_token is None: if self.verbose: logger.error('Using mask_token, but it is not set yet.' ) return None return str(self._mask_token ) @mask_token.setter def lowerCamelCase__( self :Dict ,__snake_case :List[Any] ) -> str: a__ = AddedToken(_A ,lstrip=_A ,rstrip=_A ) if isinstance(_A ,_A ) else value a__ = value def lowerCamelCase__( self :Tuple ,*__snake_case :List[str] ,**__snake_case :List[Any] ) -> Tuple: a__ = kwargs.get('is_split_into_words' ,_A ) if is_split_into_words and not self.add_prefix_space: raise ValueError( F'You need to instantiate {self.__class__.__name__} with add_prefix_space=True ' 'to use it with pretokenized inputs.' ) return super()._batch_encode_plus(*_A ,**_A ) def lowerCamelCase__( self :str ,*__snake_case :Dict ,**__snake_case :Tuple ) -> str: a__ = kwargs.get('is_split_into_words' ,_A ) if is_split_into_words and not self.add_prefix_space: raise ValueError( F'You need to instantiate {self.__class__.__name__} with add_prefix_space=True ' 'to use it with pretokenized inputs.' ) return super()._encode_plus(*_A ,**_A ) def lowerCamelCase__( self :List[Any] ,__snake_case :Optional[Any] ,__snake_case :Optional[Any] = None ) -> Dict: a__ = self._tokenizer.model.save(_A ,name=_A ) return tuple(_A ) def lowerCamelCase__( self :Union[str, Any] ,__snake_case :Tuple ,__snake_case :int=None ) -> Any: a__ = [self.bos_token_id] + token_ids_a + [self.eos_token_id] if token_ids_a is None: return output return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id] def lowerCamelCase__( self :int ,__snake_case :Tuple ,__snake_case :Tuple = None ) -> Optional[int]: a__ = [self.sep_token_id] a__ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def lowerCamelCase__( self :Optional[Any] ,__snake_case :List[str] ,__snake_case :List[str] = None ,__snake_case :Any = PaddingStrategy.DO_NOT_PAD ,__snake_case :Optional[int] = None ,__snake_case :Optional[int] = None ,) -> List[Any]: a__ = super()._pad( encoded_inputs=_A ,max_length=_A ,padding_strategy=_A ,pad_to_multiple_of=_A ,return_attention_mask=_A ,) # Load from model defaults if return_attention_mask is None: a__ = 'attention_mask' in self.model_input_names if return_attention_mask and "global_attention_mask" in encoded_inputs: a__ = encoded_inputs[self.model_input_names[0]] # `global_attention_mask` need to have the same length as other (sequential) inputs. a__ = len(encoded_inputs['global_attention_mask'] ) != len(_A ) if needs_to_be_padded: a__ = len(_A ) - len(encoded_inputs['global_attention_mask'] ) if self.padding_side == "right": # Use `-1` since `0` in `global_attention_mask` means `local attention` instead of `not to attend` a__ = ( encoded_inputs['global_attention_mask'] + [-1] * difference ) elif self.padding_side == "left": a__ = [-1] * difference + encoded_inputs[ 'global_attention_mask' ] else: raise ValueError('Invalid padding strategy:' + str(self.padding_side ) ) return encoded_inputs
240
from typing import List, Optional, Tuple, Union import torch from ...models import UNetaDModel from ...schedulers import KarrasVeScheduler from ...utils import randn_tensor from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput class A_ (a_ ): UpperCAmelCase__ = 42 UpperCAmelCase__ = 42 def __init__( self , _A , _A ): '''simple docstring''' super().__init__() self.register_modules(unet=_A , scheduler=_A ) @torch.no_grad() def __call__( self , _A = 1 , _A = 5_0 , _A = None , _A = "pil" , _A = True , **_A , ): '''simple docstring''' UpperCAmelCase = self.unet.config.sample_size UpperCAmelCase = (batch_size, 3, img_size, img_size) UpperCAmelCase = self.unet # sample x_0 ~ N(0, sigma_0^2 * I) UpperCAmelCase = randn_tensor(_A , generator=_A , device=self.device ) * self.scheduler.init_noise_sigma self.scheduler.set_timesteps(_A ) for t in self.progress_bar(self.scheduler.timesteps ): # here sigma_t == t_i from the paper UpperCAmelCase = self.scheduler.schedule[t] UpperCAmelCase = self.scheduler.schedule[t - 1] if t > 0 else 0 # 1. Select temporarily increased noise level sigma_hat # 2. Add new noise to move from sample_i to sample_hat UpperCAmelCase , UpperCAmelCase = self.scheduler.add_noise_to_input(_A , _A , generator=_A ) # 3. Predict the noise residual given the noise magnitude `sigma_hat` # The model inputs and output are adjusted by following eq. (213) in [1]. UpperCAmelCase = (sigma_hat / 2) * model((sample_hat + 1) / 2 , sigma_hat / 2 ).sample # 4. Evaluate dx/dt at sigma_hat # 5. Take Euler step from sigma to sigma_prev UpperCAmelCase = self.scheduler.step(_A , _A , _A , _A ) if sigma_prev != 0: # 6. Apply 2nd order correction # The model inputs and output are adjusted by following eq. (213) in [1]. UpperCAmelCase = (sigma_prev / 2) * model((step_output.prev_sample + 1) / 2 , sigma_prev / 2 ).sample UpperCAmelCase = self.scheduler.step_correct( _A , _A , _A , _A , step_output.prev_sample , step_output['''derivative'''] , ) UpperCAmelCase = step_output.prev_sample UpperCAmelCase = (sample / 2 + 0.5).clamp(0 , 1 ) UpperCAmelCase = sample.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": UpperCAmelCase = self.numpy_to_pil(_A ) if not return_dict: return (image,) return ImagePipelineOutput(images=_A )
273
0
import numpy as np def A__ ( __lowerCamelCase, __lowerCamelCase, __lowerCamelCase = 1E-12, __lowerCamelCase = 1_00, ): assert np.shape(__lowerCamelCase )[0] == np.shape(__lowerCamelCase )[1] # Ensure proper dimensionality. assert np.shape(__lowerCamelCase )[0] == np.shape(__lowerCamelCase )[0] # Ensure inputs are either both complex or both real assert np.iscomplexobj(__lowerCamelCase ) == np.iscomplexobj(__lowerCamelCase ) SCREAMING_SNAKE_CASE_ = np.iscomplexobj(__lowerCamelCase ) if is_complex: # Ensure complex input_matrix is Hermitian assert np.array_equal(__lowerCamelCase, input_matrix.conj().T ) # Set convergence to False. Will define convergence when we exceed max_iterations # or when we have small changes from one iteration to next. SCREAMING_SNAKE_CASE_ = False SCREAMING_SNAKE_CASE_ = 0 SCREAMING_SNAKE_CASE_ = 0 SCREAMING_SNAKE_CASE_ = 1E12 while not convergence: # Multiple matrix by the vector. SCREAMING_SNAKE_CASE_ = np.dot(__lowerCamelCase, __lowerCamelCase ) # Normalize the resulting output vector. SCREAMING_SNAKE_CASE_ = w / np.linalg.norm(__lowerCamelCase ) # Find rayleigh quotient # (faster than usual b/c we know vector is normalized already) SCREAMING_SNAKE_CASE_ = vector.conj().T if is_complex else vector.T SCREAMING_SNAKE_CASE_ = np.dot(__lowerCamelCase, np.dot(__lowerCamelCase, __lowerCamelCase ) ) # Check convergence. SCREAMING_SNAKE_CASE_ = np.abs(lambda_ - lambda_previous ) / lambda_ iterations += 1 if error <= error_tol or iterations >= max_iterations: SCREAMING_SNAKE_CASE_ = True SCREAMING_SNAKE_CASE_ = lambda_ if is_complex: SCREAMING_SNAKE_CASE_ = np.real(lambda_ ) return lambda_, vector def A__ ( ): SCREAMING_SNAKE_CASE_ = np.array([[41, 4, 20], [4, 26, 30], [20, 30, 50]] ) SCREAMING_SNAKE_CASE_ = np.array([41, 4, 20] ) SCREAMING_SNAKE_CASE_ = real_input_matrix.astype(np.complexaaa ) SCREAMING_SNAKE_CASE_ = np.triu(1j * complex_input_matrix, 1 ) complex_input_matrix += imag_matrix complex_input_matrix += -1 * imag_matrix.T SCREAMING_SNAKE_CASE_ = np.array([41, 4, 20] ).astype(np.complexaaa ) for problem_type in ["real", "complex"]: if problem_type == "real": SCREAMING_SNAKE_CASE_ = real_input_matrix SCREAMING_SNAKE_CASE_ = real_vector elif problem_type == "complex": SCREAMING_SNAKE_CASE_ = complex_input_matrix SCREAMING_SNAKE_CASE_ = complex_vector # Our implementation. SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = power_iteration(__lowerCamelCase, __lowerCamelCase ) # Numpy implementation. # Get eigenvalues and eigenvectors using built-in numpy # eigh (eigh used for symmetric or hermetian matrices). SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = np.linalg.eigh(__lowerCamelCase ) # Last eigenvalue is the maximum one. SCREAMING_SNAKE_CASE_ = eigen_values[-1] # Last column in this matrix is eigenvector corresponding to largest eigenvalue. SCREAMING_SNAKE_CASE_ = eigen_vectors[:, -1] # Check our implementation and numpy gives close answers. assert np.abs(eigen_value - eigen_value_max ) <= 1E-6 # Take absolute values element wise of each eigenvector. # as they are only unique to a minus sign. assert np.linalg.norm(np.abs(__lowerCamelCase ) - np.abs(__lowerCamelCase ) ) <= 1E-6 if __name__ == "__main__": import doctest doctest.testmod() test_power_iteration()
257
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from timm import create_model from timm.data import resolve_data_config from timm.data.transforms_factory import create_transform from transformers import BitConfig, BitForImageClassification, BitImageProcessor from transformers.image_utils import PILImageResampling from transformers.utils import logging logging.set_verbosity_info() __UpperCAmelCase = logging.get_logger(__name__) def A__ ( __lowerCamelCase ): SCREAMING_SNAKE_CASE_ = '''huggingface/label-files''' SCREAMING_SNAKE_CASE_ = '''imagenet-1k-id2label.json''' SCREAMING_SNAKE_CASE_ = json.load(open(hf_hub_download(__lowerCamelCase, __lowerCamelCase, repo_type='''dataset''' ), '''r''' ) ) SCREAMING_SNAKE_CASE_ = {int(__lowerCamelCase ): v for k, v in idalabel.items()} SCREAMING_SNAKE_CASE_ = {v: k for k, v in idalabel.items()} SCREAMING_SNAKE_CASE_ = '''std_conv''' if '''bit''' in model_name else False # note that when using BiT as backbone for ViT-hybrid checkpoints, # one needs to additionally set config.layer_type = "bottleneck", config.stem_type = "same", # config.conv_layer = "std_conv_same" SCREAMING_SNAKE_CASE_ = BitConfig( conv_layer=__lowerCamelCase, num_labels=10_00, idalabel=__lowerCamelCase, labelaid=__lowerCamelCase, ) return config def A__ ( __lowerCamelCase ): if "stem.conv" in name: SCREAMING_SNAKE_CASE_ = name.replace('''stem.conv''', '''bit.embedder.convolution''' ) if "blocks" in name: SCREAMING_SNAKE_CASE_ = name.replace('''blocks''', '''layers''' ) if "head.fc" in name: SCREAMING_SNAKE_CASE_ = name.replace('''head.fc''', '''classifier.1''' ) if name.startswith('''norm''' ): SCREAMING_SNAKE_CASE_ = '''bit.''' + name if "bit" not in name and "classifier" not in name: SCREAMING_SNAKE_CASE_ = '''bit.encoder.''' + name return name def A__ ( ): SCREAMING_SNAKE_CASE_ = '''http://images.cocodataset.org/val2017/000000039769.jpg''' SCREAMING_SNAKE_CASE_ = Image.open(requests.get(__lowerCamelCase, stream=__lowerCamelCase ).raw ) return im @torch.no_grad() def A__ ( __lowerCamelCase, __lowerCamelCase, __lowerCamelCase=False ): SCREAMING_SNAKE_CASE_ = get_config(__lowerCamelCase ) # load original model from timm SCREAMING_SNAKE_CASE_ = create_model(__lowerCamelCase, pretrained=__lowerCamelCase ) timm_model.eval() # load state_dict of original model SCREAMING_SNAKE_CASE_ = timm_model.state_dict() for key in state_dict.copy().keys(): SCREAMING_SNAKE_CASE_ = state_dict.pop(__lowerCamelCase ) SCREAMING_SNAKE_CASE_ = val.squeeze() if '''head''' in key else val # load HuggingFace model SCREAMING_SNAKE_CASE_ = BitForImageClassification(__lowerCamelCase ) model.eval() model.load_state_dict(__lowerCamelCase ) # create image processor SCREAMING_SNAKE_CASE_ = create_transform(**resolve_data_config({}, model=__lowerCamelCase ) ) SCREAMING_SNAKE_CASE_ = transform.transforms SCREAMING_SNAKE_CASE_ = { '''bilinear''': PILImageResampling.BILINEAR, '''bicubic''': PILImageResampling.BICUBIC, '''nearest''': PILImageResampling.NEAREST, } SCREAMING_SNAKE_CASE_ = BitImageProcessor( do_resize=__lowerCamelCase, size={'''shortest_edge''': timm_transforms[0].size}, resample=pillow_resamplings[timm_transforms[0].interpolation.value], do_center_crop=__lowerCamelCase, crop_size={'''height''': timm_transforms[1].size[0], '''width''': timm_transforms[1].size[1]}, do_normalize=__lowerCamelCase, image_mean=timm_transforms[-1].mean.tolist(), image_std=timm_transforms[-1].std.tolist(), ) SCREAMING_SNAKE_CASE_ = prepare_img() SCREAMING_SNAKE_CASE_ = transform(__lowerCamelCase ).unsqueeze(0 ) SCREAMING_SNAKE_CASE_ = processor(__lowerCamelCase, return_tensors='''pt''' ).pixel_values # verify pixel values assert torch.allclose(__lowerCamelCase, __lowerCamelCase ) # verify logits with torch.no_grad(): SCREAMING_SNAKE_CASE_ = model(__lowerCamelCase ) SCREAMING_SNAKE_CASE_ = outputs.logits print('''Logits:''', logits[0, :3] ) print('''Predicted class:''', model.config.idalabel[logits.argmax(-1 ).item()] ) SCREAMING_SNAKE_CASE_ = timm_model(__lowerCamelCase ) assert timm_logits.shape == outputs.logits.shape assert torch.allclose(__lowerCamelCase, outputs.logits, atol=1E-3 ) print('''Looks ok!''' ) if pytorch_dump_folder_path is not None: Path(__lowerCamelCase ).mkdir(exist_ok=__lowerCamelCase ) print(F'''Saving model {model_name} and processor to {pytorch_dump_folder_path}''' ) model.save_pretrained(__lowerCamelCase ) processor.save_pretrained(__lowerCamelCase ) if push_to_hub: print(F'''Pushing model {model_name} and processor to the hub''' ) model.push_to_hub(F'''ybelkada/{model_name}''' ) processor.push_to_hub(F'''ybelkada/{model_name}''' ) if __name__ == "__main__": __UpperCAmelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( "--model_name", default="resnetv2_50x1_bitm", type=str, help="Name of the BiT 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 push the model to the hub.", ) __UpperCAmelCase = parser.parse_args() convert_bit_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
257
1
"""simple docstring""" from typing import Any, Dict, List, Union from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging, requires_backends from .base import PIPELINE_INIT_ARGS, ChunkPipeline if is_vision_available(): from PIL import Image from ..image_utils import load_image if is_torch_available(): import torch from transformers.modeling_outputs import BaseModelOutput from ..models.auto.modeling_auto import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING __A = logging.get_logger(__name__) @add_end_docstrings(_snake_case ) class snake_case ( _snake_case ): def __init__( self : Any , **UpperCamelCase__ : Union[str, Any])-> Dict: '''simple docstring''' super().__init__(**_lowerCamelCase) if self.framework == "tf": raise ValueError(f"The {self.__class__} is only available in PyTorch.") requires_backends(self , "vision") self.check_model_type(_lowerCamelCase) def __call__( self : Union[str, Any] , UpperCamelCase__ : str , UpperCamelCase__ : Optional[int] = None , **UpperCamelCase__ : Tuple , )-> List[str]: '''simple docstring''' if "text_queries" in kwargs: __lowerCAmelCase: Tuple = kwargs.pop("text_queries") if isinstance(_lowerCamelCase , (str, Image.Image)): __lowerCAmelCase: Any = {'''image''': image, '''candidate_labels''': candidate_labels} else: __lowerCAmelCase: Optional[int] = image __lowerCAmelCase: Optional[Any] = super().__call__(_lowerCamelCase , **_lowerCamelCase) return results def lowercase_ ( self : int , **UpperCamelCase__ : Optional[Any])-> Tuple: '''simple docstring''' __lowerCAmelCase: Any = {} if "threshold" in kwargs: __lowerCAmelCase: List[str] = kwargs['''threshold'''] if "top_k" in kwargs: __lowerCAmelCase: Optional[int] = kwargs['''top_k'''] return {}, {}, postprocess_params def lowercase_ ( self : int , UpperCamelCase__ : Dict)-> Union[str, Any]: '''simple docstring''' __lowerCAmelCase: Dict = load_image(inputs["image"]) __lowerCAmelCase: List[Any] = inputs['''candidate_labels'''] if isinstance(_lowerCamelCase , _lowerCamelCase): __lowerCAmelCase: Tuple = candidate_labels.split(",") __lowerCAmelCase: Optional[int] = torch.tensor([[image.height, image.width]] , dtype=torch.intaa) for i, candidate_label in enumerate(_lowerCamelCase): __lowerCAmelCase: Optional[Any] = self.tokenizer(_lowerCamelCase , return_tensors=self.framework) __lowerCAmelCase: Optional[int] = self.image_processor(_lowerCamelCase , return_tensors=self.framework) yield { "is_last": i == len(_lowerCamelCase) - 1, "target_size": target_size, "candidate_label": candidate_label, **text_inputs, **image_features, } def lowercase_ ( self : Dict , UpperCamelCase__ : int)-> Tuple: '''simple docstring''' __lowerCAmelCase: int = model_inputs.pop("target_size") __lowerCAmelCase: int = model_inputs.pop("candidate_label") __lowerCAmelCase: Optional[Any] = model_inputs.pop("is_last") __lowerCAmelCase: Dict = self.model(**_lowerCamelCase) __lowerCAmelCase: str = {'''target_size''': target_size, '''candidate_label''': candidate_label, '''is_last''': is_last, **outputs} return model_outputs def lowercase_ ( self : List[Any] , UpperCamelCase__ : List[Any] , UpperCamelCase__ : Tuple=0.1 , UpperCamelCase__ : Tuple=None)-> List[str]: '''simple docstring''' __lowerCAmelCase: int = [] for model_output in model_outputs: __lowerCAmelCase: List[Any] = model_output['''candidate_label'''] __lowerCAmelCase: Union[str, Any] = BaseModelOutput(_lowerCamelCase) __lowerCAmelCase: Union[str, Any] = self.image_processor.post_process_object_detection( outputs=_lowerCamelCase , threshold=_lowerCamelCase , target_sizes=model_output["target_size"])[0] for index in outputs["scores"].nonzero(): __lowerCAmelCase: str = outputs['''scores'''][index].item() __lowerCAmelCase: Dict = self._get_bounding_box(outputs["boxes"][index][0]) __lowerCAmelCase: Tuple = {'''score''': score, '''label''': label, '''box''': box} results.append(_lowerCamelCase) __lowerCAmelCase: Optional[int] = sorted(_lowerCamelCase , key=lambda UpperCamelCase__: x["score"] , reverse=_lowerCamelCase) if top_k: __lowerCAmelCase: Optional[int] = results[:top_k] return results def lowercase_ ( self : Any , UpperCamelCase__ : Tuple)-> Optional[int]: '''simple docstring''' if self.framework != "pt": raise ValueError("The ZeroShotObjectDetectionPipeline is only available in PyTorch.") __lowerCAmelCase: int = box.int().tolist() __lowerCAmelCase: List[Any] = { '''xmin''': xmin, '''ymin''': ymin, '''xmax''': xmax, '''ymax''': ymax, } return bbox
217
def __lowerCamelCase ( UpperCAmelCase_ : int = 1000 ): """simple docstring""" a , a :int = 1, 1 a :Any = 2 while True: a :Optional[int] = 0 a :str = fa + fa a , a :List[Any] = fa, f index += 1 for _ in str(UpperCAmelCase_ ): i += 1 if i == n: break return index if __name__ == "__main__": print(solution(int(str(input()).strip())))
94
0
"""simple docstring""" from math import acos, sin from typing import List, Tuple, Union import numpy as np import torch from PIL import Image from ...models import AutoencoderKL, UNetaDConditionModel from ...schedulers import DDIMScheduler, DDPMScheduler from ...utils import randn_tensor from ..pipeline_utils import AudioPipelineOutput, BaseOutput, DiffusionPipeline, ImagePipelineOutput from .mel import Mel class a ( _lowerCamelCase ): """simple docstring""" UpperCAmelCase = ["vqvae"] def __init__( self: List[str] , UpperCamelCase: AutoencoderKL , UpperCamelCase: UNetaDConditionModel , UpperCamelCase: Mel , UpperCamelCase: Union[DDIMScheduler, DDPMScheduler] , ): """simple docstring""" super().__init__() self.register_modules(unet=UpperCamelCase , scheduler=UpperCamelCase , mel=UpperCamelCase , vqvae=UpperCamelCase ) def UpperCamelCase ( self: Any ): """simple docstring""" return 50 if isinstance(self.scheduler , UpperCamelCase ) else 10_00 @torch.no_grad() def __call__( self: List[str] , UpperCamelCase: int = 1 , UpperCamelCase: str = None , UpperCamelCase: np.ndarray = None , UpperCamelCase: int = 0 , UpperCamelCase: int = 0 , UpperCamelCase: int = None , UpperCamelCase: torch.Generator = None , UpperCamelCase: float = 0 , UpperCamelCase: float = 0 , UpperCamelCase: torch.Generator = None , UpperCamelCase: float = 0 , UpperCamelCase: torch.Tensor = None , UpperCamelCase: torch.Tensor = None , UpperCamelCase: str=True , ): """simple docstring""" A__ = steps or self.get_default_steps() self.scheduler.set_timesteps(UpperCamelCase ) A__ = step_generator or generator # For backwards compatibility if type(self.unet.config.sample_size ) == int: A__ = (self.unet.config.sample_size, self.unet.config.sample_size) if noise is None: A__ = randn_tensor( ( batch_size, self.unet.config.in_channels, self.unet.config.sample_size[0], self.unet.config.sample_size[1], ) , generator=UpperCamelCase , device=self.device , ) A__ = noise A__ = None if audio_file is not None or raw_audio is not None: self.mel.load_audio(UpperCamelCase , UpperCamelCase ) A__ = self.mel.audio_slice_to_image(UpperCamelCase ) A__ = np.frombuffer(input_image.tobytes() , dtype="""uint8""" ).reshape( (input_image.height, input_image.width) ) A__ = (input_image / 2_55) * 2 - 1 A__ = torch.tensor(input_image[np.newaxis, :, :] , dtype=torch.float ).to(self.device ) if self.vqvae is not None: A__ = self.vqvae.encode(torch.unsqueeze(UpperCamelCase , 0 ) ).latent_dist.sample( generator=UpperCamelCase )[0] A__ = self.vqvae.config.scaling_factor * input_images if start_step > 0: A__ = self.scheduler.add_noise(UpperCamelCase , UpperCamelCase , self.scheduler.timesteps[start_step - 1] ) A__ = ( self.unet.config.sample_size[1] * self.mel.get_sample_rate() / self.mel.x_res / self.mel.hop_length ) A__ = int(mask_start_secs * pixels_per_second ) A__ = int(mask_end_secs * pixels_per_second ) A__ = self.scheduler.add_noise(UpperCamelCase , UpperCamelCase , torch.tensor(self.scheduler.timesteps[start_step:] ) ) for step, t in enumerate(self.progress_bar(self.scheduler.timesteps[start_step:] ) ): if isinstance(self.unet , UpperCamelCase ): A__ = self.unet(UpperCamelCase , UpperCamelCase , UpperCamelCase )["""sample"""] else: A__ = self.unet(UpperCamelCase , UpperCamelCase )["""sample"""] if isinstance(self.scheduler , UpperCamelCase ): A__ = self.scheduler.step( model_output=UpperCamelCase , timestep=UpperCamelCase , sample=UpperCamelCase , eta=UpperCamelCase , generator=UpperCamelCase , )["""prev_sample"""] else: A__ = self.scheduler.step( model_output=UpperCamelCase , timestep=UpperCamelCase , sample=UpperCamelCase , generator=UpperCamelCase , )["""prev_sample"""] if mask is not None: if mask_start > 0: A__ = mask[:, step, :, :mask_start] if mask_end > 0: A__ = mask[:, step, :, -mask_end:] if self.vqvae is not None: # 0.18215 was scaling factor used in training to ensure unit variance A__ = 1 / self.vqvae.config.scaling_factor * images A__ = self.vqvae.decode(UpperCamelCase )["""sample"""] A__ = (images / 2 + 0.5).clamp(0 , 1 ) A__ = images.cpu().permute(0 , 2 , 3 , 1 ).numpy() A__ = (images * 2_55).round().astype("""uint8""" ) A__ = list( (Image.fromarray(_[:, :, 0] ) for _ in images) if images.shape[3] == 1 else (Image.fromarray(UpperCamelCase , mode="""RGB""" ).convert("""L""" ) for _ in images) ) A__ = [self.mel.image_to_audio(UpperCamelCase ) for _ in images] if not return_dict: return images, (self.mel.get_sample_rate(), audios) return BaseOutput(**AudioPipelineOutput(np.array(UpperCamelCase )[:, np.newaxis, :] ) , **ImagePipelineOutput(UpperCamelCase ) ) @torch.no_grad() def UpperCamelCase ( self: Union[str, Any] , UpperCamelCase: List[Image.Image] , UpperCamelCase: int = 50 ): """simple docstring""" assert isinstance(self.scheduler , UpperCamelCase ) self.scheduler.set_timesteps(UpperCamelCase ) A__ = np.array( [np.frombuffer(image.tobytes() , dtype="""uint8""" ).reshape((1, image.height, image.width) ) for image in images] ) A__ = (sample / 2_55) * 2 - 1 A__ = torch.Tensor(UpperCamelCase ).to(self.device ) for t in self.progress_bar(torch.flip(self.scheduler.timesteps , (0,) ) ): A__ = t - self.scheduler.config.num_train_timesteps // self.scheduler.num_inference_steps A__ = self.scheduler.alphas_cumprod[t] A__ = ( self.scheduler.alphas_cumprod[prev_timestep] if prev_timestep >= 0 else self.scheduler.final_alpha_cumprod ) A__ = 1 - alpha_prod_t A__ = self.unet(UpperCamelCase , UpperCamelCase )["""sample"""] A__ = (1 - alpha_prod_t_prev) ** 0.5 * model_output A__ = (sample - pred_sample_direction) * alpha_prod_t_prev ** (-0.5) A__ = sample * alpha_prod_t ** 0.5 + beta_prod_t ** 0.5 * model_output return sample @staticmethod def UpperCamelCase ( UpperCamelCase: torch.Tensor , UpperCamelCase: torch.Tensor , UpperCamelCase: float ): """simple docstring""" A__ = acos(torch.dot(torch.flatten(UpperCamelCase ) , torch.flatten(UpperCamelCase ) ) / torch.norm(UpperCamelCase ) / torch.norm(UpperCamelCase ) ) return sin((1 - alpha) * theta ) * xa / sin(UpperCamelCase ) + sin(alpha * theta ) * xa / sin(UpperCamelCase )
69
"""simple docstring""" import sys from collections import defaultdict class a : """simple docstring""" def __init__( self: Union[str, Any] ): """simple docstring""" A__ = [] def UpperCamelCase ( self: List[str] , UpperCamelCase: int ): """simple docstring""" return self.node_position[vertex] def UpperCamelCase ( self: Union[str, Any] , UpperCamelCase: Optional[Any] , UpperCamelCase: str ): """simple docstring""" A__ = pos def UpperCamelCase ( self: Dict , UpperCamelCase: List[Any] , UpperCamelCase: Any , UpperCamelCase: str , UpperCamelCase: List[str] ): """simple docstring""" if start > size // 2 - 1: return else: if 2 * start + 2 >= size: A__ = 2 * start + 1 else: if heap[2 * start + 1] < heap[2 * start + 2]: A__ = 2 * start + 1 else: A__ = 2 * start + 2 if heap[smallest_child] < heap[start]: A__ , A__ = heap[smallest_child], positions[smallest_child] A__ , A__ = ( heap[start], positions[start], ) A__ , A__ = temp, tempa A__ = 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 UpperCamelCase ( self: Tuple , UpperCamelCase: Tuple , UpperCamelCase: Any , UpperCamelCase: Optional[Any] , UpperCamelCase: Any ): """simple docstring""" A__ = position[index] while index != 0: A__ = int((index - 2) / 2 ) if index % 2 == 0 else int((index - 1) / 2 ) if val < heap[parent]: A__ = heap[parent] A__ = position[parent] self.set_position(position[parent] , UpperCamelCase ) else: A__ = val A__ = temp self.set_position(UpperCamelCase , UpperCamelCase ) break A__ = parent else: A__ = val A__ = temp self.set_position(UpperCamelCase , 0 ) def UpperCamelCase ( self: Union[str, Any] , UpperCamelCase: List[str] , UpperCamelCase: Optional[int] ): """simple docstring""" A__ = len(UpperCamelCase ) // 2 - 1 for i in range(UpperCamelCase , -1 , -1 ): self.top_to_bottom(UpperCamelCase , UpperCamelCase , len(UpperCamelCase ) , UpperCamelCase ) def UpperCamelCase ( self: Optional[int] , UpperCamelCase: str , UpperCamelCase: List[str] ): """simple docstring""" A__ = positions[0] A__ = sys.maxsize self.top_to_bottom(UpperCamelCase , 0 , len(UpperCamelCase ) , UpperCamelCase ) return temp def _snake_case ( UpperCAmelCase_ : Union[str, Any] ): A__ = Heap() A__ = [0] * len(UpperCAmelCase_ ) A__ = [-1] * len(UpperCAmelCase_ ) # Neighboring Tree Vertex of selected vertex # Minimum Distance of explored vertex with neighboring vertex of partial tree # formed in graph A__ = [] # Heap of Distance of vertices from their neighboring vertex A__ = [] for vertex in range(len(UpperCAmelCase_ ) ): distance_tv.append(sys.maxsize ) positions.append(UpperCAmelCase_ ) heap.node_position.append(UpperCAmelCase_ ) A__ = [] A__ = 1 A__ = sys.maxsize for neighbor, distance in adjacency_list[0]: A__ = 0 A__ = distance heap.heapify(UpperCAmelCase_ , UpperCAmelCase_ ) for _ in range(1 , len(UpperCAmelCase_ ) ): A__ = heap.delete_minimum(UpperCAmelCase_ , UpperCAmelCase_ ) if visited[vertex] == 0: tree_edges.append((nbr_tv[vertex], vertex) ) A__ = 1 for neighbor, distance in adjacency_list[vertex]: if ( visited[neighbor] == 0 and distance < distance_tv[heap.get_position(UpperCAmelCase_ )] ): A__ = distance heap.bottom_to_top( UpperCAmelCase_ , heap.get_position(UpperCAmelCase_ ) , UpperCAmelCase_ , UpperCAmelCase_ ) A__ = vertex return tree_edges if __name__ == "__main__": # pragma: no cover # < --------- Prims Algorithm --------- > SCREAMING_SNAKE_CASE_ : int = int(input('Enter number of edges: ').strip()) SCREAMING_SNAKE_CASE_ : str = defaultdict(list) for _ in range(edges_number): SCREAMING_SNAKE_CASE_ : Optional[int] = [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))
69
1
import os import time import warnings from dataclasses import dataclass, field from enum import Enum from typing import List, Optional, Union import torch from filelock import FileLock from torch.utils.data import Dataset from ...tokenization_utils_base import PreTrainedTokenizerBase from ...utils import logging from ..processors.glue import glue_convert_examples_to_features, glue_output_modes, glue_processors from ..processors.utils import InputFeatures SCREAMING_SNAKE_CASE :Optional[Any] = logging.get_logger(__name__) @dataclass class UpperCAmelCase : '''simple docstring''' snake_case_ = field(metadata={"help": "The name of the task to train on: " + ", ".join(glue_processors.keys() )} ) snake_case_ = field( metadata={"help": "The input data dir. Should contain the .tsv files (or other data files) for the task."} ) snake_case_ = 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." ) } , ) snake_case_ = field( default=__SCREAMING_SNAKE_CASE , metadata={"help": "Overwrite the cached training and evaluation sets"} ) def UpperCamelCase_ ( self : Union[str, Any] ): __A = self.task_name.lower() class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = "train" snake_case_ = "dev" snake_case_ = "test" class UpperCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' snake_case_ = 42 snake_case_ = 42 snake_case_ = 42 def __init__( self : Union[str, Any] ,A : GlueDataTrainingArguments ,A : PreTrainedTokenizerBase ,A : Optional[int] = None ,A : Union[str, Split] = Split.train ,A : Optional[str] = None ,): warnings.warn( "This dataset will be removed from the library soon, preprocessing should be handled with the 🤗 Datasets " "library. You can have a look at this example script for pointers: " "https://github.com/huggingface/transformers/blob/main/examples/pytorch/text-classification/run_glue.py" ,A ,) __A = args __A = glue_processors[args.task_name]() __A = glue_output_modes[args.task_name] if isinstance(A ,A ): try: __A = Split[mode] except KeyError: raise KeyError("mode is not a valid split name" ) # Load data features from cache or dataset file __A = os.path.join( cache_dir if cache_dir is not None else args.data_dir ,f'''cached_{mode.value}_{tokenizer.__class__.__name__}_{args.max_seq_length}_{args.task_name}''' ,) __A = self.processor.get_labels() if args.task_name in ["mnli", "mnli-mm"] and tokenizer.__class__.__name__ in ( "RobertaTokenizer", "RobertaTokenizerFast", "XLMRobertaTokenizer", "BartTokenizer", "BartTokenizerFast", ): # HACK(label indices are swapped in RoBERTa pretrained model) __A , __A = label_list[2], label_list[1] __A = label_list # Make sure only the first process in distributed training processes the dataset, # and the others will use the cache. __A = cached_features_file + ".lock" with FileLock(A ): if os.path.exists(A ) and not args.overwrite_cache: __A = time.time() __A = torch.load(A ) logger.info( f'''Loading features from cached file {cached_features_file} [took %.3f s]''' ,time.time() - start ) else: logger.info(f'''Creating features from dataset file at {args.data_dir}''' ) if mode == Split.dev: __A = self.processor.get_dev_examples(args.data_dir ) elif mode == Split.test: __A = self.processor.get_test_examples(args.data_dir ) else: __A = self.processor.get_train_examples(args.data_dir ) if limit_length is not None: __A = examples[:limit_length] __A = glue_convert_examples_to_features( A ,A ,max_length=args.max_seq_length ,label_list=A ,output_mode=self.output_mode ,) __A = time.time() torch.save(self.features ,A ) # ^ This seems to take a lot of time so I want to investigate why and how we can improve. logger.info( f'''Saving features into cached file {cached_features_file} [took {time.time() - start:.3f} s]''' ) def __len__( self : int ): return len(self.features ) def __getitem__( self : Tuple ,A : List[Any] ): return self.features[i] def UpperCamelCase_ ( self : List[Any] ): return self.label_list
15
import argparse import math import traceback import dateutil.parser as date_parser import requests def UpperCAmelCase ( a_ ) -> str: """simple docstring""" __A = {} __A = job["started_at"] __A = job["completed_at"] __A = date_parser.parse(a_ ) __A = date_parser.parse(a_ ) __A = round((end_datetime - start_datetime).total_seconds() / 60.0 ) __A = start __A = end __A = duration_in_min return job_info def UpperCAmelCase ( a_ , a_=None ) -> str: """simple docstring""" __A = None if token is not None: __A = {"Accept": "application/vnd.github+json", "Authorization": F'''Bearer {token}'''} __A = F'''https://api.github.com/repos/huggingface/transformers/actions/runs/{workflow_run_id}/jobs?per_page=100''' __A = requests.get(a_ , headers=a_ ).json() __A = {} try: job_time.update({job["name"]: extract_time_from_single_job(a_ ) for job in result["jobs"]} ) __A = math.ceil((result["total_count"] - 1_0_0) / 1_0_0 ) for i in range(a_ ): __A = requests.get(url + F'''&page={i + 2}''' , headers=a_ ).json() job_time.update({job["name"]: extract_time_from_single_job(a_ ) for job in result["jobs"]} ) return job_time except Exception: print(F'''Unknown error, could not fetch links:\n{traceback.format_exc()}''' ) return {} if __name__ == "__main__": SCREAMING_SNAKE_CASE :Union[str, Any] = argparse.ArgumentParser() # Required parameters parser.add_argument('--workflow_run_id', type=str, required=True, help='A GitHub Actions workflow run id.') SCREAMING_SNAKE_CASE :Optional[int] = parser.parse_args() SCREAMING_SNAKE_CASE :Union[str, Any] = get_job_time(args.workflow_run_id) SCREAMING_SNAKE_CASE :Optional[int] = dict(sorted(job_time.items(), key=lambda item: item[1]["duration"], reverse=True)) for k, v in job_time.items(): print(f'''{k}: {v["duration"]}''')
15
1
"""simple docstring""" import jax.numpy as jnp from ...utils import logging from ..ta.modeling_flax_ta import FlaxTaEncoderModel, FlaxTaForConditionalGeneration, FlaxTaModel from .configuration_mta import MTaConfig UpperCAmelCase_ : Dict = logging.get_logger(__name__) UpperCAmelCase_ : List[Any] = """T5Config""" def _A (__a , __a , __a ) -> jnp.ndarray: """simple docstring""" SCREAMING_SNAKE_CASE_ : str = jnp.zeros_like(__a ) SCREAMING_SNAKE_CASE_ : Tuple = shifted_input_ids.at[:, 1:].set(input_ids[:, :-1] ) SCREAMING_SNAKE_CASE_ : Optional[Any] = shifted_input_ids.at[:, 0].set(__a ) SCREAMING_SNAKE_CASE_ : List[Any] = jnp.where(shifted_input_ids == -1_00 , __a , __a ) return shifted_input_ids class lowerCAmelCase__ ( UpperCAmelCase__ ): '''simple docstring''' __UpperCamelCase = "mt5" __UpperCamelCase = MTaConfig class lowerCAmelCase__ ( UpperCAmelCase__ ): '''simple docstring''' __UpperCamelCase = "mt5" __UpperCamelCase = MTaConfig class lowerCAmelCase__ ( UpperCAmelCase__ ): '''simple docstring''' __UpperCamelCase = "mt5" __UpperCamelCase = MTaConfig
318
"""simple docstring""" import argparse from pathlib import Path from typing import Dict, OrderedDict, Tuple import torch from audiocraft.models import MusicGen from transformers import ( AutoFeatureExtractor, AutoTokenizer, EncodecModel, MusicgenDecoderConfig, MusicgenForConditionalGeneration, MusicgenProcessor, TaEncoderModel, ) from transformers.models.musicgen.modeling_musicgen import MusicgenForCausalLM from transformers.utils import logging logging.set_verbosity_info() UpperCAmelCase_ : str = logging.get_logger(__name__) UpperCAmelCase_ : Optional[Any] = ["""model.decoder.embed_positions.weights"""] def _A (__a ) -> Dict: """simple docstring""" if "emb" in name: SCREAMING_SNAKE_CASE_ : Optional[int] = name.replace('''emb''' , '''model.decoder.embed_tokens''' ) if "transformer" in name: SCREAMING_SNAKE_CASE_ : Tuple = name.replace('''transformer''' , '''model.decoder''' ) if "cross_attention" in name: SCREAMING_SNAKE_CASE_ : str = name.replace('''cross_attention''' , '''encoder_attn''' ) if "linear1" in name: SCREAMING_SNAKE_CASE_ : Optional[int] = name.replace('''linear1''' , '''fc1''' ) if "linear2" in name: SCREAMING_SNAKE_CASE_ : str = name.replace('''linear2''' , '''fc2''' ) if "norm1" in name: SCREAMING_SNAKE_CASE_ : Any = name.replace('''norm1''' , '''self_attn_layer_norm''' ) if "norm_cross" in name: SCREAMING_SNAKE_CASE_ : List[str] = name.replace('''norm_cross''' , '''encoder_attn_layer_norm''' ) if "norm2" in name: SCREAMING_SNAKE_CASE_ : Tuple = name.replace('''norm2''' , '''final_layer_norm''' ) if "out_norm" in name: SCREAMING_SNAKE_CASE_ : List[str] = name.replace('''out_norm''' , '''model.decoder.layer_norm''' ) if "linears" in name: SCREAMING_SNAKE_CASE_ : Dict = name.replace('''linears''' , '''lm_heads''' ) if "condition_provider.conditioners.description.output_proj" in name: SCREAMING_SNAKE_CASE_ : List[str] = name.replace('''condition_provider.conditioners.description.output_proj''' , '''enc_to_dec_proj''' ) return name def _A (__a , __a ) -> Tuple[Dict, Dict]: """simple docstring""" SCREAMING_SNAKE_CASE_ : Union[str, Any] = list(state_dict.keys() ) SCREAMING_SNAKE_CASE_ : int = {} for key in keys: SCREAMING_SNAKE_CASE_ : int = state_dict.pop(__a ) SCREAMING_SNAKE_CASE_ : int = rename_keys(__a ) if "in_proj_weight" in key: # split fused qkv proj SCREAMING_SNAKE_CASE_ : List[str] = val[:hidden_size, :] SCREAMING_SNAKE_CASE_ : List[str] = val[hidden_size : 2 * hidden_size, :] SCREAMING_SNAKE_CASE_ : Optional[Any] = val[-hidden_size:, :] elif "enc_to_dec_proj" in key: SCREAMING_SNAKE_CASE_ : int = val else: SCREAMING_SNAKE_CASE_ : Any = val return state_dict, enc_dec_proj_state_dict def _A (__a ) -> MusicgenDecoderConfig: """simple docstring""" if checkpoint == "small": # default config values SCREAMING_SNAKE_CASE_ : Optional[int] = 10_24 SCREAMING_SNAKE_CASE_ : Tuple = 24 SCREAMING_SNAKE_CASE_ : Optional[Any] = 16 elif checkpoint == "medium": SCREAMING_SNAKE_CASE_ : List[str] = 15_36 SCREAMING_SNAKE_CASE_ : Optional[int] = 48 SCREAMING_SNAKE_CASE_ : Optional[int] = 24 elif checkpoint == "large": SCREAMING_SNAKE_CASE_ : Optional[Any] = 20_48 SCREAMING_SNAKE_CASE_ : Optional[int] = 48 SCREAMING_SNAKE_CASE_ : int = 32 else: raise ValueError(f'Checkpoint should be one of `[\'small\', \'medium\', \'large\']`, got {checkpoint}.' ) SCREAMING_SNAKE_CASE_ : List[Any] = MusicgenDecoderConfig( hidden_size=__a , ffn_dim=hidden_size * 4 , num_hidden_layers=__a , num_attention_heads=__a , ) return config @torch.no_grad() def _A (__a , __a=None , __a=None , __a="cpu" ) -> List[str]: """simple docstring""" SCREAMING_SNAKE_CASE_ : Any = MusicGen.get_pretrained(__a , device=__a ) SCREAMING_SNAKE_CASE_ : Dict = decoder_config_from_checkpoint(__a ) SCREAMING_SNAKE_CASE_ : Optional[Any] = fairseq_model.lm.state_dict() SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : int = rename_state_dict( __a , hidden_size=decoder_config.hidden_size ) SCREAMING_SNAKE_CASE_ : Optional[Any] = TaEncoderModel.from_pretrained('''t5-base''' ) SCREAMING_SNAKE_CASE_ : List[str] = EncodecModel.from_pretrained('''facebook/encodec_32khz''' ) SCREAMING_SNAKE_CASE_ : int = MusicgenForCausalLM(__a ).eval() # load all decoder weights - expect that we'll be missing embeddings and enc-dec projection SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : List[str] = decoder.load_state_dict(__a , strict=__a ) for key in missing_keys.copy(): if key.startswith(('''text_encoder''', '''audio_encoder''') ) or key in EXPECTED_MISSING_KEYS: missing_keys.remove(__a ) if len(__a ) > 0: raise ValueError(f'Missing key(s) in state_dict: {missing_keys}' ) if len(__a ) > 0: raise ValueError(f'Unexpected key(s) in state_dict: {unexpected_keys}' ) # init the composite model SCREAMING_SNAKE_CASE_ : str = MusicgenForConditionalGeneration(text_encoder=__a , audio_encoder=__a , decoder=__a ) # load the pre-trained enc-dec projection (from the decoder state dict) model.enc_to_dec_proj.load_state_dict(__a ) # check we can do a forward pass SCREAMING_SNAKE_CASE_ : Dict = torch.arange(0 , 8 , dtype=torch.long ).reshape(2 , -1 ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = input_ids.reshape(2 * 4 , -1 ) with torch.no_grad(): SCREAMING_SNAKE_CASE_ : List[Any] = model(input_ids=__a , decoder_input_ids=__a ).logits if logits.shape != (8, 1, 20_48): raise ValueError('''Incorrect shape for logits''' ) # now construct the processor SCREAMING_SNAKE_CASE_ : str = AutoTokenizer.from_pretrained('''t5-base''' ) SCREAMING_SNAKE_CASE_ : str = AutoFeatureExtractor.from_pretrained('''facebook/encodec_32khz''' , padding_side='''left''' ) SCREAMING_SNAKE_CASE_ : Tuple = MusicgenProcessor(feature_extractor=__a , tokenizer=__a ) # set the appropriate bos/pad token ids SCREAMING_SNAKE_CASE_ : str = 20_48 SCREAMING_SNAKE_CASE_ : List[Any] = 20_48 # set other default generation config params SCREAMING_SNAKE_CASE_ : int = int(30 * audio_encoder.config.frame_rate ) SCREAMING_SNAKE_CASE_ : str = True SCREAMING_SNAKE_CASE_ : Optional[Any] = 3.0 if pytorch_dump_folder is not None: Path(__a ).mkdir(exist_ok=__a ) logger.info(f'Saving model {checkpoint} to {pytorch_dump_folder}' ) model.save_pretrained(__a ) processor.save_pretrained(__a ) if repo_id: logger.info(f'Pushing model {checkpoint} to {repo_id}' ) model.push_to_hub(__a ) processor.push_to_hub(__a ) if __name__ == "__main__": UpperCAmelCase_ : Dict = argparse.ArgumentParser() # Required parameters parser.add_argument( """--checkpoint""", default="""small""", type=str, help="""Checkpoint size of the MusicGen model you'd like to convert. Can be one of: `['small', 'medium', 'large']`.""", ) parser.add_argument( """--pytorch_dump_folder""", required=True, default=None, type=str, help="""Path to the output PyTorch model directory.""", ) parser.add_argument( """--push_to_hub""", default=None, type=str, help="""Where to upload the converted model on the 🤗 hub.""" ) parser.add_argument( """--device""", default="""cpu""", type=str, help="""Torch device to run the conversion, either cpu or cuda.""" ) UpperCAmelCase_ : Dict = parser.parse_args() convert_musicgen_checkpoint(args.checkpoint, args.pytorch_dump_folder, args.push_to_hub)
318
1
def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : float , SCREAMING_SNAKE_CASE__ : int ): if digit_amount > 0: return round(number - int(SCREAMING_SNAKE_CASE__ ) , SCREAMING_SNAKE_CASE__ ) return number - int(SCREAMING_SNAKE_CASE__ ) if __name__ == "__main__": print(decimal_isolate(1.53, 0)) print(decimal_isolate(35.345, 1)) print(decimal_isolate(35.345, 2)) print(decimal_isolate(35.345, 3)) print(decimal_isolate(-14.789, 3)) print(decimal_isolate(0, 2)) print(decimal_isolate(-14.123, 1)) print(decimal_isolate(-14.123, 2)) print(decimal_isolate(-14.123, 3))
62
"""simple docstring""" # Copyright 2023 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from ..models.auto import AutoModelForSeqaSeqLM, AutoTokenizer from .base import PipelineTool UpperCAmelCase : List[Any] = { 'Acehnese Arabic': 'ace_Arab', 'Acehnese Latin': 'ace_Latn', 'Mesopotamian Arabic': 'acm_Arab', 'Ta\'izzi-Adeni Arabic': 'acq_Arab', 'Tunisian Arabic': 'aeb_Arab', 'Afrikaans': 'afr_Latn', 'South Levantine Arabic': 'ajp_Arab', 'Akan': 'aka_Latn', 'Amharic': 'amh_Ethi', 'North Levantine Arabic': 'apc_Arab', 'Modern Standard Arabic': 'arb_Arab', 'Modern Standard Arabic Romanized': 'arb_Latn', 'Najdi Arabic': 'ars_Arab', 'Moroccan Arabic': 'ary_Arab', 'Egyptian Arabic': 'arz_Arab', 'Assamese': 'asm_Beng', 'Asturian': 'ast_Latn', 'Awadhi': 'awa_Deva', 'Central Aymara': 'ayr_Latn', 'South Azerbaijani': 'azb_Arab', 'North Azerbaijani': 'azj_Latn', 'Bashkir': 'bak_Cyrl', 'Bambara': 'bam_Latn', 'Balinese': 'ban_Latn', 'Belarusian': 'bel_Cyrl', 'Bemba': 'bem_Latn', 'Bengali': 'ben_Beng', 'Bhojpuri': 'bho_Deva', 'Banjar Arabic': 'bjn_Arab', 'Banjar Latin': 'bjn_Latn', 'Standard Tibetan': 'bod_Tibt', 'Bosnian': 'bos_Latn', 'Buginese': 'bug_Latn', 'Bulgarian': 'bul_Cyrl', 'Catalan': 'cat_Latn', 'Cebuano': 'ceb_Latn', 'Czech': 'ces_Latn', 'Chokwe': 'cjk_Latn', 'Central Kurdish': 'ckb_Arab', 'Crimean Tatar': 'crh_Latn', 'Welsh': 'cym_Latn', 'Danish': 'dan_Latn', 'German': 'deu_Latn', 'Southwestern Dinka': 'dik_Latn', 'Dyula': 'dyu_Latn', 'Dzongkha': 'dzo_Tibt', 'Greek': 'ell_Grek', 'English': 'eng_Latn', 'Esperanto': 'epo_Latn', 'Estonian': 'est_Latn', 'Basque': 'eus_Latn', 'Ewe': 'ewe_Latn', 'Faroese': 'fao_Latn', 'Fijian': 'fij_Latn', 'Finnish': 'fin_Latn', 'Fon': 'fon_Latn', 'French': 'fra_Latn', 'Friulian': 'fur_Latn', 'Nigerian Fulfulde': 'fuv_Latn', 'Scottish Gaelic': 'gla_Latn', 'Irish': 'gle_Latn', 'Galician': 'glg_Latn', 'Guarani': 'grn_Latn', 'Gujarati': 'guj_Gujr', 'Haitian Creole': 'hat_Latn', 'Hausa': 'hau_Latn', 'Hebrew': 'heb_Hebr', 'Hindi': 'hin_Deva', 'Chhattisgarhi': 'hne_Deva', 'Croatian': 'hrv_Latn', 'Hungarian': 'hun_Latn', 'Armenian': 'hye_Armn', 'Igbo': 'ibo_Latn', 'Ilocano': 'ilo_Latn', 'Indonesian': 'ind_Latn', 'Icelandic': 'isl_Latn', 'Italian': 'ita_Latn', 'Javanese': 'jav_Latn', 'Japanese': 'jpn_Jpan', 'Kabyle': 'kab_Latn', 'Jingpho': 'kac_Latn', 'Kamba': 'kam_Latn', 'Kannada': 'kan_Knda', 'Kashmiri Arabic': 'kas_Arab', 'Kashmiri Devanagari': 'kas_Deva', 'Georgian': 'kat_Geor', 'Central Kanuri Arabic': 'knc_Arab', 'Central Kanuri Latin': 'knc_Latn', 'Kazakh': 'kaz_Cyrl', 'Kabiyè': 'kbp_Latn', 'Kabuverdianu': 'kea_Latn', 'Khmer': 'khm_Khmr', 'Kikuyu': 'kik_Latn', 'Kinyarwanda': 'kin_Latn', 'Kyrgyz': 'kir_Cyrl', 'Kimbundu': 'kmb_Latn', 'Northern Kurdish': 'kmr_Latn', 'Kikongo': 'kon_Latn', 'Korean': 'kor_Hang', 'Lao': 'lao_Laoo', 'Ligurian': 'lij_Latn', 'Limburgish': 'lim_Latn', 'Lingala': 'lin_Latn', 'Lithuanian': 'lit_Latn', 'Lombard': 'lmo_Latn', 'Latgalian': 'ltg_Latn', 'Luxembourgish': 'ltz_Latn', 'Luba-Kasai': 'lua_Latn', 'Ganda': 'lug_Latn', 'Luo': 'luo_Latn', 'Mizo': 'lus_Latn', 'Standard Latvian': 'lvs_Latn', 'Magahi': 'mag_Deva', 'Maithili': 'mai_Deva', 'Malayalam': 'mal_Mlym', 'Marathi': 'mar_Deva', 'Minangkabau Arabic ': 'min_Arab', 'Minangkabau Latin': 'min_Latn', 'Macedonian': 'mkd_Cyrl', 'Plateau Malagasy': 'plt_Latn', 'Maltese': 'mlt_Latn', 'Meitei Bengali': 'mni_Beng', 'Halh Mongolian': 'khk_Cyrl', 'Mossi': 'mos_Latn', 'Maori': 'mri_Latn', 'Burmese': 'mya_Mymr', 'Dutch': 'nld_Latn', 'Norwegian Nynorsk': 'nno_Latn', 'Norwegian Bokmål': 'nob_Latn', 'Nepali': 'npi_Deva', 'Northern Sotho': 'nso_Latn', 'Nuer': 'nus_Latn', 'Nyanja': 'nya_Latn', 'Occitan': 'oci_Latn', 'West Central Oromo': 'gaz_Latn', 'Odia': 'ory_Orya', 'Pangasinan': 'pag_Latn', 'Eastern Panjabi': 'pan_Guru', 'Papiamento': 'pap_Latn', 'Western Persian': 'pes_Arab', 'Polish': 'pol_Latn', 'Portuguese': 'por_Latn', 'Dari': 'prs_Arab', 'Southern Pashto': 'pbt_Arab', 'Ayacucho Quechua': 'quy_Latn', 'Romanian': 'ron_Latn', 'Rundi': 'run_Latn', 'Russian': 'rus_Cyrl', 'Sango': 'sag_Latn', 'Sanskrit': 'san_Deva', 'Santali': 'sat_Olck', 'Sicilian': 'scn_Latn', 'Shan': 'shn_Mymr', 'Sinhala': 'sin_Sinh', 'Slovak': 'slk_Latn', 'Slovenian': 'slv_Latn', 'Samoan': 'smo_Latn', 'Shona': 'sna_Latn', 'Sindhi': 'snd_Arab', 'Somali': 'som_Latn', 'Southern Sotho': 'sot_Latn', 'Spanish': 'spa_Latn', 'Tosk Albanian': 'als_Latn', 'Sardinian': 'srd_Latn', 'Serbian': 'srp_Cyrl', 'Swati': 'ssw_Latn', 'Sundanese': 'sun_Latn', 'Swedish': 'swe_Latn', 'Swahili': 'swh_Latn', 'Silesian': 'szl_Latn', 'Tamil': 'tam_Taml', 'Tatar': 'tat_Cyrl', 'Telugu': 'tel_Telu', 'Tajik': 'tgk_Cyrl', 'Tagalog': 'tgl_Latn', 'Thai': 'tha_Thai', 'Tigrinya': 'tir_Ethi', 'Tamasheq Latin': 'taq_Latn', 'Tamasheq Tifinagh': 'taq_Tfng', 'Tok Pisin': 'tpi_Latn', 'Tswana': 'tsn_Latn', 'Tsonga': 'tso_Latn', 'Turkmen': 'tuk_Latn', 'Tumbuka': 'tum_Latn', 'Turkish': 'tur_Latn', 'Twi': 'twi_Latn', 'Central Atlas Tamazight': 'tzm_Tfng', 'Uyghur': 'uig_Arab', 'Ukrainian': 'ukr_Cyrl', 'Umbundu': 'umb_Latn', 'Urdu': 'urd_Arab', 'Northern Uzbek': 'uzn_Latn', 'Venetian': 'vec_Latn', 'Vietnamese': 'vie_Latn', 'Waray': 'war_Latn', 'Wolof': 'wol_Latn', 'Xhosa': 'xho_Latn', 'Eastern Yiddish': 'ydd_Hebr', 'Yoruba': 'yor_Latn', 'Yue Chinese': 'yue_Hant', 'Chinese Simplified': 'zho_Hans', 'Chinese Traditional': 'zho_Hant', 'Standard Malay': 'zsm_Latn', 'Zulu': 'zul_Latn', } class lowerCamelCase__ ( A ): """simple docstring""" __a = """facebook/nllb-200-distilled-600M""" __a = ( """This is a tool that translates text from a language to another. It takes three inputs: `text`, which should """ """be the text to translate, `src_lang`, which should be the language of the text to translate and `tgt_lang`, """ """which should be the language for the desired ouput language. Both `src_lang` and `tgt_lang` are written in """ """plain English, such as 'Romanian', or 'Albanian'. It returns the text translated in `tgt_lang`.""" ) __a = """translator""" __a = AutoTokenizer __a = AutoModelForSeqaSeqLM __a = LANGUAGE_CODES __a = ["""text""", """text""", """text"""] __a = ["""text"""] def lowerCamelCase__ ( self : Optional[Any] , UpperCamelCase : Tuple , UpperCamelCase : int , UpperCamelCase : List[Any] ): '''simple docstring''' if src_lang not in self.lang_to_code: raise ValueError(f'''{src_lang} is not a supported language.''' ) if tgt_lang not in self.lang_to_code: raise ValueError(f'''{tgt_lang} is not a supported language.''' ) __UpperCAmelCase : Union[str, Any] = self.lang_to_code[src_lang] __UpperCAmelCase : Dict = self.lang_to_code[tgt_lang] return self.pre_processor._build_translation_inputs( UpperCamelCase , return_tensors="""pt""" , src_lang=UpperCamelCase , tgt_lang=UpperCamelCase ) def lowerCamelCase__ ( self : Tuple , UpperCamelCase : List[str] ): '''simple docstring''' return self.model.generate(**UpperCamelCase ) def lowerCamelCase__ ( self : Dict , UpperCamelCase : Optional[Any] ): '''simple docstring''' return self.post_processor.decode(outputs[0].tolist() , skip_special_tokens=UpperCamelCase )
115
0
'''simple docstring''' from torch import nn def UpperCAmelCase_ (__a : Optional[Any] ): """simple docstring""" if act_fn in ["swish", "silu"]: return nn.SiLU() elif act_fn == "mish": return nn.Mish() elif act_fn == "gelu": return nn.GELU() else: raise ValueError(f"""Unsupported activation function: {act_fn}""" )
364
'''simple docstring''' from collections.abc import Generator from math import sin def UpperCAmelCase_ (__a : bytes ): """simple docstring""" if len(__a ) != 3_2: raise ValueError('Input must be of length 32' ) _a : Any = b'' for i in [3, 2, 1, 0]: little_endian += string_aa[8 * i : 8 * i + 8] return little_endian def UpperCAmelCase_ (__a : int ): """simple docstring""" if i < 0: raise ValueError('Input must be non-negative' ) _a : List[str] = format(__a , '08x' )[-8:] _a : str = 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 ): """simple docstring""" _a : List[Any] = b'' for char in message: bit_string += format(__a , '08b' ).encode('utf-8' ) _a : int = format(len(__a ) , '064b' ).encode('utf-8' ) # Pad bit_string to a multiple of 512 chars bit_string += b"1" while len(__a ) % 5_1_2 != 4_4_8: bit_string += b"0" bit_string += to_little_endian(start_len[3_2:] ) + to_little_endian(start_len[:3_2] ) return bit_string def UpperCAmelCase_ (__a : bytes ): """simple docstring""" if len(__a ) % 5_1_2 != 0: raise ValueError('Input must have length that\'s a multiple of 512' ) for pos in range(0 , len(__a ) , 5_1_2 ): _a : List[Any] = bit_string[pos : pos + 5_1_2] _a : str = [] for i in range(0 , 5_1_2 , 3_2 ): block_words.append(int(to_little_endian(block[i : i + 3_2] ) , 2 ) ) yield block_words def UpperCAmelCase_ (__a : int ): """simple docstring""" if i < 0: raise ValueError('Input must be non-negative' ) _a : List[str] = format(__a , '032b' ) _a : int = '' for c in i_str: new_str += "1" if c == "0" else "0" return int(__a , 2 ) def UpperCAmelCase_ (__a : int , __a : int ): """simple docstring""" return (a + b) % 2**3_2 def UpperCAmelCase_ (__a : int , __a : int ): """simple docstring""" if i < 0: raise ValueError('Input must be non-negative' ) if shift < 0: raise ValueError('Shift must be non-negative' ) return ((i << shift) ^ (i >> (3_2 - shift))) % 2**3_2 def UpperCAmelCase_ (__a : bytes ): """simple docstring""" _a : str = preprocess(__a ) _a : Optional[int] = [int(2**3_2 * abs(sin(i + 1 ) ) ) for i in range(6_4 )] # Starting states _a : int = 0x67_45_23_01 _a : Union[str, Any] = 0xEF_CD_AB_89 _a : str = 0x98_BA_DC_FE _a : List[Any] = 0x10_32_54_76 _a : Optional[int] = [ 7, 1_2, 1_7, 2_2, 7, 1_2, 1_7, 2_2, 7, 1_2, 1_7, 2_2, 7, 1_2, 1_7, 2_2, 5, 9, 1_4, 2_0, 5, 9, 1_4, 2_0, 5, 9, 1_4, 2_0, 5, 9, 1_4, 2_0, 4, 1_1, 1_6, 2_3, 4, 1_1, 1_6, 2_3, 4, 1_1, 1_6, 2_3, 4, 1_1, 1_6, 2_3, 6, 1_0, 1_5, 2_1, 6, 1_0, 1_5, 2_1, 6, 1_0, 1_5, 2_1, 6, 1_0, 1_5, 2_1, ] # Process bit string in chunks, each with 16 32-char words for block_words in get_block_words(__a ): _a : Union[str, Any] = aa _a : List[Any] = ba _a : List[Any] = ca _a : Dict = da # Hash current chunk for i in range(6_4 ): if i <= 1_5: # f = (b & c) | (not_32(b) & d) # Alternate definition for f _a : Optional[int] = d ^ (b & (c ^ d)) _a : Optional[Any] = i elif i <= 3_1: # f = (d & b) | (not_32(d) & c) # Alternate definition for f _a : Optional[Any] = c ^ (d & (b ^ c)) _a : Dict = (5 * i + 1) % 1_6 elif i <= 4_7: _a : Optional[Any] = b ^ c ^ d _a : Dict = (3 * i + 5) % 1_6 else: _a : int = c ^ (b | not_aa(__a )) _a : List[str] = (7 * i) % 1_6 _a : Optional[int] = (f + a + added_consts[i] + block_words[g]) % 2**3_2 _a : Union[str, Any] = d _a : Tuple = c _a : Optional[int] = b _a : Union[str, Any] = sum_aa(__a , left_rotate_aa(__a , shift_amounts[i] ) ) # Add hashed chunk to running total _a : Any = sum_aa(__a , __a ) _a : Dict = sum_aa(__a , __a ) _a : Union[str, Any] = sum_aa(__a , __a ) _a : str = sum_aa(__a , __a ) _a : Optional[Any] = reformat_hex(__a ) + reformat_hex(__a ) + reformat_hex(__a ) + reformat_hex(__a ) return digest if __name__ == "__main__": import doctest doctest.testmod()
5
0