code
stringlengths
81
54k
code_codestyle
int64
0
721
style_context
stringlengths
91
41.9k
style_context_codestyle
int64
0
699
label
int64
0
1
"""simple docstring""" import argparse import glob import importlib.util import os import re import black from doc_builder.style_doc import style_docstrings_in_code # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/check_copies.py _snake_case = "src/diffusers" _snake_case = "." # This is to make sure the diffusers module imported is the one in the repo. _snake_case = importlib.util.spec_from_file_location( "diffusers", os.path.join(DIFFUSERS_PATH, "__init__.py"), submodule_search_locations=[DIFFUSERS_PATH], ) _snake_case = spec.loader.load_module() def snake_case ( _a: str , _a: List[Any] )-> Optional[int]: '''simple docstring''' return line.startswith(_a ) or len(_a ) <= 1 or re.search(R'^\s*\)(\s*->.*:|:)\s*$' , _a ) is not None def snake_case ( _a: List[Any] )-> Optional[Any]: '''simple docstring''' lowerCamelCase__ = object_name.split('.' ) lowerCamelCase__ = 0 # First let's find the module where our object lives. lowerCamelCase__ = parts[i] while i < len(_a ) and not os.path.isfile(os.path.join(_a , F'{module}.py' ) ): i += 1 if i < len(_a ): lowerCamelCase__ = os.path.join(_a , parts[i] ) if i >= len(_a ): raise ValueError(F'`object_name` should begin with the name of a module of diffusers but got {object_name}.' ) with open(os.path.join(_a , F'{module}.py' ) , 'r' , encoding='utf-8' , newline='\n' ) as f: lowerCamelCase__ = f.readlines() # Now let's find the class / func in the code! lowerCamelCase__ = '' lowerCamelCase__ = 0 for name in parts[i + 1 :]: while ( line_index < len(_a ) and re.search(RF'^{indent}(class|def)\s+{name}(\(|\:)' , lines[line_index] ) is None ): line_index += 1 indent += " " line_index += 1 if line_index >= len(_a ): raise ValueError(F' {object_name} does not match any function or class in {module}.' ) # We found the beginning of the class / func, now let's find the end (when the indent diminishes). lowerCamelCase__ = line_index while line_index < len(_a ) and _should_continue(lines[line_index] , _a ): line_index += 1 # Clean up empty lines at the end (if any). while len(lines[line_index - 1] ) <= 1: line_index -= 1 lowerCamelCase__ = lines[start_index:line_index] return "".join(_a ) _snake_case = re.compile(R"^(\s*)#\s*Copied from\s+diffusers\.(\S+\.\S+)\s*($|\S.*$)") _snake_case = re.compile(R"^\s*(\S+)->(\S+)(\s+.*|$)") _snake_case = re.compile(R"<FILL\s+[^>]*>") def snake_case ( _a: Tuple )-> Dict: '''simple docstring''' lowerCamelCase__ = code.split('\n' ) lowerCamelCase__ = 0 while idx < len(_a ) and len(lines[idx] ) == 0: idx += 1 if idx < len(_a ): return re.search(R'^(\s*)\S' , lines[idx] ).groups()[0] return "" def snake_case ( _a: int )-> List[Any]: '''simple docstring''' lowerCamelCase__ = len(get_indent(_a ) ) > 0 if has_indent: lowerCamelCase__ = F'class Bla:\n{code}' lowerCamelCase__ = black.Mode(target_versions={black.TargetVersion.PYaa} , line_length=119 , preview=_a ) lowerCamelCase__ = black.format_str(_a , mode=_a ) lowerCamelCase__ , lowerCamelCase__ = style_docstrings_in_code(_a ) return result[len('class Bla:\n' ) :] if has_indent else result def snake_case ( _a: Union[str, Any] , _a: int=False )-> Any: '''simple docstring''' with open(_a , 'r' , encoding='utf-8' , newline='\n' ) as f: lowerCamelCase__ = f.readlines() lowerCamelCase__ = [] lowerCamelCase__ = 0 # Not a for loop cause `lines` is going to change (if `overwrite=True`). while line_index < len(_a ): lowerCamelCase__ = _re_copy_warning.search(lines[line_index] ) if search is None: line_index += 1 continue # There is some copied code here, let's retrieve the original. lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ = search.groups() lowerCamelCase__ = find_code_in_diffusers(_a ) lowerCamelCase__ = get_indent(_a ) lowerCamelCase__ = line_index + 1 if indent == theoretical_indent else line_index + 2 lowerCamelCase__ = theoretical_indent lowerCamelCase__ = start_index # Loop to check the observed code, stop when indentation diminishes or if we see a End copy comment. lowerCamelCase__ = True while line_index < len(_a ) and should_continue: line_index += 1 if line_index >= len(_a ): break lowerCamelCase__ = lines[line_index] lowerCamelCase__ = _should_continue(_a , _a ) and re.search(F'^{indent}# End copy' , _a ) is None # Clean up empty lines at the end (if any). while len(lines[line_index - 1] ) <= 1: line_index -= 1 lowerCamelCase__ = lines[start_index:line_index] lowerCamelCase__ = ''.join(_a ) # Remove any nested `Copied from` comments to avoid circular copies lowerCamelCase__ = [line for line in theoretical_code.split('\n' ) if _re_copy_warning.search(_a ) is None] lowerCamelCase__ = '\n'.join(_a ) # Before comparing, use the `replace_pattern` on the original code. if len(_a ) > 0: lowerCamelCase__ = replace_pattern.replace('with' , '' ).split(',' ) lowerCamelCase__ = [_re_replace_pattern.search(_a ) for p in patterns] for pattern in patterns: if pattern is None: continue lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ = pattern.groups() lowerCamelCase__ = re.sub(_a , _a , _a ) if option.strip() == "all-casing": lowerCamelCase__ = re.sub(obja.lower() , obja.lower() , _a ) lowerCamelCase__ = re.sub(obja.upper() , obja.upper() , _a ) # Blackify after replacement. To be able to do that, we need the header (class or function definition) # from the previous line lowerCamelCase__ = blackify(lines[start_index - 1] + theoretical_code ) lowerCamelCase__ = theoretical_code[len(lines[start_index - 1] ) :] # Test for a diff and act accordingly. if observed_code != theoretical_code: diffs.append([object_name, start_index] ) if overwrite: lowerCamelCase__ = lines[:start_index] + [theoretical_code] + lines[line_index:] lowerCamelCase__ = start_index + 1 if overwrite and len(_a ) > 0: # Warn the user a file has been modified. print(F'Detected changes, rewriting {filename}.' ) with open(_a , 'w' , encoding='utf-8' , newline='\n' ) as f: f.writelines(_a ) return diffs def snake_case ( _a: bool = False )-> Union[str, Any]: '''simple docstring''' lowerCamelCase__ = glob.glob(os.path.join(_a , '**/*.py' ) , recursive=_a ) lowerCamelCase__ = [] for filename in all_files: lowerCamelCase__ = is_copy_consistent(_a , _a ) diffs += [F'- {filename}: copy does not match {d[0]} at line {d[1]}' for d in new_diffs] if not overwrite and len(_a ) > 0: lowerCamelCase__ = '\n'.join(_a ) raise Exception( 'Found the following copy inconsistencies:\n' + diff + '\nRun `make fix-copies` or `python utils/check_copies.py --fix_and_overwrite` to fix them.' ) if __name__ == "__main__": _snake_case = argparse.ArgumentParser() parser.add_argument("--fix_and_overwrite", action="store_true", help="Whether to fix inconsistencies.") _snake_case = parser.parse_args() check_copies(args.fix_and_overwrite)
659
"""simple docstring""" import json import multiprocessing import os import re from collections import defaultdict import torch from accelerate import Accelerator from accelerate.utils import set_seed from arguments import HumanEvalArguments from datasets import load_dataset, load_metric from torch.utils.data import IterableDataset from torch.utils.data.dataloader import DataLoader from tqdm import tqdm import transformers from transformers import AutoModelForCausalLM, AutoTokenizer, HfArgumentParser, StoppingCriteria, StoppingCriteriaList _snake_case = ["\nclass", "\ndef", "\n#", "\n@", "\nprint", "\nif"] class _a ( SCREAMING_SNAKE_CASE_ ): def __init__( self : List[str] , SCREAMING_SNAKE_CASE__ : Any , SCREAMING_SNAKE_CASE__ : List[Any] , SCREAMING_SNAKE_CASE__ : int=None , SCREAMING_SNAKE_CASE__ : Optional[int]=1 ): lowerCamelCase__ = tokenizer lowerCamelCase__ = dataset lowerCamelCase__ = len(SCREAMING_SNAKE_CASE__ ) if n_tasks is None else n_tasks lowerCamelCase__ = n_copies def __iter__( self : Any ): lowerCamelCase__ = [] for task in range(self.n_tasks ): # without strip, the model generate commented codes ... prompts.append(self.tokenizer.eos_token + self.dataset[task]['prompt'].strip() ) lowerCamelCase__ = self.tokenizer(SCREAMING_SNAKE_CASE__ , padding=SCREAMING_SNAKE_CASE__ , return_tensors='pt' ) for task in range(self.n_tasks ): for _ in range(self.n_copies ): yield { "ids": outputs.input_ids[task], "task_id": task, "input_len": outputs.attention_mask[task].sum(), } class _a ( SCREAMING_SNAKE_CASE_ ): def __init__( self : Dict , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : List[Any] , SCREAMING_SNAKE_CASE__ : Dict ): lowerCamelCase__ = start_length lowerCamelCase__ = eof_strings lowerCamelCase__ = tokenizer def __call__( self : Any , SCREAMING_SNAKE_CASE__ : Any , SCREAMING_SNAKE_CASE__ : List[str] , **SCREAMING_SNAKE_CASE__ : Dict ): lowerCamelCase__ = self.tokenizer.batch_decode(input_ids[:, self.start_length :] ) lowerCamelCase__ = [] for decoded_generation in decoded_generations: done.append(any(stop_string in decoded_generation for stop_string in self.eof_strings ) ) return all(SCREAMING_SNAKE_CASE__ ) def snake_case ( _a: List[Any] )-> Dict: '''simple docstring''' lowerCamelCase__ = re.split('(%s)' % '|'.join(_a ) , _a ) # last string should be "" return "".join(string_list[:-2] ) def snake_case ( _a: List[Any] , _a: Optional[int] , _a: str , _a: Union[str, Any] , _a: Dict , _a: Optional[int]=20 , **_a: Optional[int] )-> List[str]: '''simple docstring''' lowerCamelCase__ = defaultdict(_a ) # dict of list of generated tokens for step, batch in tqdm(enumerate(_a ) ): with torch.no_grad(): lowerCamelCase__ = batch['ids'].shape[-1] lowerCamelCase__ = accelerator.unwrap_model(_a ).generate( input_ids=batch['ids'][:, : batch['input_len']] , num_return_sequences=_a , **_a ) # each task is generated batch_size times lowerCamelCase__ = batch['task_id'].repeat(_a ) lowerCamelCase__ = accelerator.pad_across_processes( _a , dim=1 , pad_index=tokenizer.pad_token_id ) lowerCamelCase__ , lowerCamelCase__ = accelerator.gather((generated_tokens, generated_tasks) ) lowerCamelCase__ = generated_tokens.cpu().numpy() lowerCamelCase__ = generated_tasks.cpu().numpy() for task, generated_tokens in zip(_a , _a ): gen_token_dict[task].append(_a ) lowerCamelCase__ = [[] for _ in range(_a )] for task, generated_tokens in gen_token_dict.items(): for s in generated_tokens: lowerCamelCase__ = tokenizer.decode(_a , skip_special_tokens=_a , clean_up_tokenization_spaces=_a ) code_gens[task].append(remove_last_block(_a ) ) return code_gens def snake_case ( )-> Union[str, Any]: '''simple docstring''' lowerCamelCase__ = HfArgumentParser(_a ) lowerCamelCase__ = parser.parse_args() transformers.logging.set_verbosity_error() # enables code execution in code_eval metric lowerCamelCase__ = args.HF_ALLOW_CODE_EVAL # make sure tokenizer plays nice with multiprocessing lowerCamelCase__ = 'false' if args.num_workers is None: lowerCamelCase__ = multiprocessing.cpu_count() # Use dataset load to feed to accelerate lowerCamelCase__ = Accelerator() set_seed(args.seed , device_specific=_a ) # Load model and tokenizer lowerCamelCase__ = AutoTokenizer.from_pretrained(args.model_ckpt ) lowerCamelCase__ = tokenizer.eos_token lowerCamelCase__ = AutoModelForCausalLM.from_pretrained(args.model_ckpt ) # Generation settings lowerCamelCase__ = { 'do_sample': args.do_sample, 'temperature': args.temperature, 'max_new_tokens': args.max_new_tokens, 'top_p': args.top_p, 'top_k': args.top_k, 'stopping_criteria': StoppingCriteriaList([EndOfFunctionCriteria(0 , _a , _a )] ), } # Load evaluation dataset and metric lowerCamelCase__ = load_dataset('openai_humaneval' ) lowerCamelCase__ = load_metric('code_eval' ) lowerCamelCase__ = args.num_tasks if args.num_tasks is not None else len(human_eval['test'] ) lowerCamelCase__ = args.n_samples // args.batch_size lowerCamelCase__ = TokenizedDataset(_a , human_eval['test'] , n_copies=_a , n_tasks=_a ) # do not confuse args.batch_size, which is actually the num_return_sequences lowerCamelCase__ = DataLoader(_a , batch_size=1 ) # Run a quick test to see if code evaluation is enabled try: lowerCamelCase__ = code_eval_metric.compute(references=[''] , predictions=[['']] ) except ValueError as exception: print( 'Code evaluation not enabled. Read the warning below carefully and then use `--HF_ALLOW_CODE_EVAL="1"`' ' flag to enable code evaluation.' ) raise exception lowerCamelCase__ , lowerCamelCase__ = accelerator.prepare(_a , _a ) lowerCamelCase__ = complete_code( _a , _a , _a , _a , n_tasks=_a , batch_size=args.batch_size , **_a , ) if accelerator.is_main_process: lowerCamelCase__ = [] for task in tqdm(range(_a ) ): lowerCamelCase__ = human_eval['test'][task]['test'] lowerCamelCase__ = F'check({human_eval["test"][task]["entry_point"]})' references.append('\n' + test_func + '\n' + entry_point ) # Evaluate completions with "code_eval" metric lowerCamelCase__ , lowerCamelCase__ = code_eval_metric.compute( references=_a , predictions=_a , num_workers=args.num_workers ) print(F'Results: {pass_at_k}' ) # Save results to json file with open(args.output_file , 'w' ) as fp: json.dump(_a , _a ) # For some reason the folliwng seems to be necessary sometimes for code_eval to work nice with multiprocessing # https://stackoverflow.com/questions/60804599/python-multiprocessing-keeps-spawning-the-whole-script if __name__ == "__main__": main()
659
1
"""simple docstring""" import numpy # List of input, output pairs _snake_case = ( ((5, 2, 3), 15), ((6, 5, 9), 25), ((11, 12, 13), 41), ((1, 1, 1), 8), ((11, 12, 13), 41), ) _snake_case = (((515, 22, 13), 555), ((61, 35, 49), 150)) _snake_case = [2, 4, 1, 5] _snake_case = len(train_data) _snake_case = 0.0_09 def snake_case ( _a: Dict , _a: str="train" )-> int: '''simple docstring''' return calculate_hypothesis_value(_a , _a ) - output( _a , _a ) def snake_case ( _a: str )-> Union[str, Any]: '''simple docstring''' lowerCamelCase__ = 0 for i in range(len(_a ) - 1 ): hyp_val += data_input_tuple[i] * parameter_vector[i + 1] hyp_val += parameter_vector[0] return hyp_val def snake_case ( _a: str , _a: Union[str, Any] )-> Any: '''simple docstring''' if data_set == "train": return train_data[example_no][1] elif data_set == "test": return test_data[example_no][1] return None def snake_case ( _a: Any , _a: Optional[int] )-> Optional[Any]: '''simple docstring''' if data_set == "train": return _hypothesis_value(train_data[example_no][0] ) elif data_set == "test": return _hypothesis_value(test_data[example_no][0] ) return None def snake_case ( _a: int , _a: str=m )-> Any: '''simple docstring''' lowerCamelCase__ = 0 for i in range(_a ): if index == -1: summation_value += _error(_a ) else: summation_value += _error(_a ) * train_data[i][0][index] return summation_value def snake_case ( _a: str )-> Dict: '''simple docstring''' lowerCamelCase__ = summation_of_cost_derivative(_a , _a ) / m return cost_derivative_value def snake_case ( )-> Dict: '''simple docstring''' global parameter_vector # Tune these values to set a tolerance value for predicted output lowerCamelCase__ = 0.00_0002 lowerCamelCase__ = 0 lowerCamelCase__ = 0 while True: j += 1 lowerCamelCase__ = [0, 0, 0, 0] for i in range(0 , len(_a ) ): lowerCamelCase__ = get_cost_derivative(i - 1 ) lowerCamelCase__ = ( parameter_vector[i] - LEARNING_RATE * cost_derivative ) if numpy.allclose( _a , _a , atol=_a , rtol=_a , ): break lowerCamelCase__ = temp_parameter_vector print(('Number of iterations:', j) ) def snake_case ( )-> Optional[Any]: '''simple docstring''' for i in range(len(_a ) ): print(('Actual output value:', output(_a , 'test' )) ) print(('Hypothesis output:', calculate_hypothesis_value(_a , 'test' )) ) if __name__ == "__main__": run_gradient_descent() print("\nTesting gradient descent for a linear hypothesis function.\n") test_gradient_descent()
659
"""simple docstring""" import argparse import json from tqdm import tqdm def snake_case ( )-> List[Any]: '''simple docstring''' lowerCamelCase__ = argparse.ArgumentParser() # Required parameters parser.add_argument( '--src_path' , type=_a , default='biencoder-nq-dev.json' , help='Path to raw DPR training data' , ) parser.add_argument( '--evaluation_set' , type=_a , help='where to store parsed evaluation_set file' , ) parser.add_argument( '--gold_data_path' , type=_a , help='where to store parsed gold_data_path file' , ) lowerCamelCase__ = parser.parse_args() with open(args.src_path , 'r' ) as src_file, open(args.evaluation_set , 'w' ) as eval_file, open( args.gold_data_path , 'w' ) as gold_file: lowerCamelCase__ = json.load(_a ) for dpr_record in tqdm(_a ): lowerCamelCase__ = dpr_record['question'] lowerCamelCase__ = [context['title'] for context in dpr_record['positive_ctxs']] eval_file.write(question + '\n' ) gold_file.write('\t'.join(_a ) + '\n' ) if __name__ == "__main__": main()
659
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available, is_vision_available, ) _snake_case = { "configuration_layoutlmv2": ["LAYOUTLMV2_PRETRAINED_CONFIG_ARCHIVE_MAP", "LayoutLMv2Config"], "processing_layoutlmv2": ["LayoutLMv2Processor"], "tokenization_layoutlmv2": ["LayoutLMv2Tokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = ["LayoutLMv2TokenizerFast"] try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = ["LayoutLMv2FeatureExtractor"] _snake_case = ["LayoutLMv2ImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = [ "LAYOUTLMV2_PRETRAINED_MODEL_ARCHIVE_LIST", "LayoutLMv2ForQuestionAnswering", "LayoutLMv2ForSequenceClassification", "LayoutLMv2ForTokenClassification", "LayoutLMv2Layer", "LayoutLMv2Model", "LayoutLMv2PreTrainedModel", ] if TYPE_CHECKING: from .configuration_layoutlmva import LAYOUTLMV2_PRETRAINED_CONFIG_ARCHIVE_MAP, LayoutLMvaConfig from .processing_layoutlmva import LayoutLMvaProcessor from .tokenization_layoutlmva import LayoutLMvaTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_layoutlmva_fast import LayoutLMvaTokenizerFast try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_layoutlmva import LayoutLMvaFeatureExtractor, LayoutLMvaImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_layoutlmva import ( LAYOUTLMV2_PRETRAINED_MODEL_ARCHIVE_LIST, LayoutLMvaForQuestionAnswering, LayoutLMvaForSequenceClassification, LayoutLMvaForTokenClassification, LayoutLMvaLayer, LayoutLMvaModel, LayoutLMvaPreTrainedModel, ) else: import sys _snake_case = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
659
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) _snake_case = { "configuration_blenderbot": [ "BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP", "BlenderbotConfig", "BlenderbotOnnxConfig", ], "tokenization_blenderbot": ["BlenderbotTokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = ["BlenderbotTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = [ "BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST", "BlenderbotForCausalLM", "BlenderbotForConditionalGeneration", "BlenderbotModel", "BlenderbotPreTrainedModel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = [ "TFBlenderbotForConditionalGeneration", "TFBlenderbotModel", "TFBlenderbotPreTrainedModel", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = [ "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 _snake_case = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
659
1
"""simple docstring""" import json import os import unittest from transformers.models.roc_bert.tokenization_roc_bert import ( VOCAB_FILES_NAMES, RoCBertBasicTokenizer, RoCBertTokenizer, RoCBertWordpieceTokenizer, _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 ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): a_ : str = RoCBertTokenizer a_ : Optional[int] = None a_ : Any = False a_ : Union[str, Any] = True a_ : str = filter_non_english def _UpperCamelCase ( self : List[Any] ): super().setUp() lowerCamelCase__ = ['[UNK]', '[CLS]', '[SEP]', '[PAD]', '[MASK]', '你', '好', '是', '谁', 'a', 'b', 'c', 'd'] lowerCamelCase__ = {} lowerCamelCase__ = {} for i, value in enumerate(SCREAMING_SNAKE_CASE__ ): lowerCamelCase__ = i lowerCamelCase__ = i lowerCamelCase__ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['vocab_file'] ) lowerCamelCase__ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['word_shape_file'] ) lowerCamelCase__ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['word_pronunciation_file'] ) with open(self.vocab_file , 'w' , encoding='utf-8' ) as vocab_writer: vocab_writer.write(''.join([x + '\n' for x in vocab_tokens] ) ) with open(self.word_shape_file , 'w' , encoding='utf-8' ) as word_shape_writer: json.dump(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , ensure_ascii=SCREAMING_SNAKE_CASE__ ) with open(self.word_pronunciation_file , 'w' , encoding='utf-8' ) as word_pronunciation_writer: json.dump(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , ensure_ascii=SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : Dict ): lowerCamelCase__ = self.tokenizer_class(self.vocab_file , self.word_shape_file , self.word_pronunciation_file ) lowerCamelCase__ = tokenizer.tokenize('你好[SEP]你是谁' ) self.assertListEqual(SCREAMING_SNAKE_CASE__ , ['你', '好', '[SEP]', '你', '是', '谁'] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(SCREAMING_SNAKE_CASE__ ) , [5, 6, 2, 5, 7, 8] ) self.assertListEqual(tokenizer.convert_tokens_to_shape_ids(SCREAMING_SNAKE_CASE__ ) , [5, 6, 2, 5, 7, 8] ) self.assertListEqual(tokenizer.convert_tokens_to_pronunciation_ids(SCREAMING_SNAKE_CASE__ ) , [5, 6, 2, 5, 7, 8] ) def _UpperCamelCase ( self : List[Any] ): lowerCamelCase__ = RoCBertBasicTokenizer() self.assertListEqual(tokenizer.tokenize('ah\u535A\u63A8zz' ) , ['ah', '\u535A', '\u63A8', 'zz'] ) def _UpperCamelCase ( self : str ): lowerCamelCase__ = RoCBertBasicTokenizer(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 : Union[str, Any] ): lowerCamelCase__ = RoCBertBasicTokenizer(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 : Optional[int] ): lowerCamelCase__ = RoCBertBasicTokenizer(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 : Dict ): lowerCamelCase__ = RoCBertBasicTokenizer(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 : Any ): lowerCamelCase__ = RoCBertBasicTokenizer(do_lower_case=SCREAMING_SNAKE_CASE__ ) self.assertListEqual( tokenizer.tokenize(' \tHeLLo!how \n Are yoU? ' ) , ['HeLLo', '!', 'how', 'Are', 'yoU', '?'] ) def _UpperCamelCase ( self : Dict ): lowerCamelCase__ = RoCBertBasicTokenizer(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 : Dict ): lowerCamelCase__ = RoCBertBasicTokenizer(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 ): lowerCamelCase__ = RoCBertBasicTokenizer(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 : Dict ): lowerCamelCase__ = ['[UNK]', '[CLS]', '[SEP]', 'want', '##want', '##ed', 'wa', 'un', 'runn', '##ing'] lowerCamelCase__ = {} for i, token in enumerate(SCREAMING_SNAKE_CASE__ ): lowerCamelCase__ = i lowerCamelCase__ = RoCBertWordpieceTokenizer(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] ): 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 : Optional[Any] ): 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 ): 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 : Dict ): lowerCamelCase__ = self.get_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]']] ) if self.test_rust_tokenizer: lowerCamelCase__ = self.get_rust_tokenizer() self.assertListEqual( [rust_tokenizer.tokenize(SCREAMING_SNAKE_CASE__ ) for t in ['Test', '\xad', 'test']] , [['[UNK]'], [], ['[UNK]']] ) def _UpperCamelCase ( self : 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 ): 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__ ) @slow def _UpperCamelCase ( self : Optional[int] ): lowerCamelCase__ = self.tokenizer_class(self.vocab_file , self.word_shape_file , self.word_pronunciation_file ) lowerCamelCase__ = tokenizer.encode('你好' , add_special_tokens=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = tokenizer.encode('你是谁' , 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 == [1] + text + [2] assert encoded_pair == [1] + text + [2] + text_a + [2] def _UpperCamelCase ( self : Dict ): lowerCamelCase__ = self.get_tokenizers(do_lower_case=SCREAMING_SNAKE_CASE__ ) for tokenizer in tokenizers: with self.subTest(F'{tokenizer.__class__.__name__}' ): lowerCamelCase__ = '你好,你是谁' lowerCamelCase__ = tokenizer.tokenize(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = tokenizer.convert_tokens_to_ids(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = tokenizer.convert_tokens_to_shape_ids(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = tokenizer.convert_tokens_to_pronunciation_ids(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = tokenizer.prepare_for_model( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , add_special_tokens=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = tokenizer.encode_plus(SCREAMING_SNAKE_CASE__ , add_special_tokens=SCREAMING_SNAKE_CASE__ ) self.assertEqual(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ )
659
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging _snake_case = logging.get_logger(__name__) _snake_case = { "microsoft/swinv2-tiny-patch4-window8-256": ( "https://huggingface.co/microsoft/swinv2-tiny-patch4-window8-256/resolve/main/config.json" ), } class _a ( SCREAMING_SNAKE_CASE_ ): a_ : Union[str, Any] = 'swinv2' a_ : Optional[int] = { 'num_attention_heads': 'num_heads', 'num_hidden_layers': 'num_layers', } def __init__( self : List[str] , SCREAMING_SNAKE_CASE__ : int=2_24 , SCREAMING_SNAKE_CASE__ : str=4 , SCREAMING_SNAKE_CASE__ : List[Any]=3 , SCREAMING_SNAKE_CASE__ : str=96 , SCREAMING_SNAKE_CASE__ : Dict=[2, 2, 6, 2] , SCREAMING_SNAKE_CASE__ : Union[str, Any]=[3, 6, 12, 24] , SCREAMING_SNAKE_CASE__ : Tuple=7 , SCREAMING_SNAKE_CASE__ : Union[str, Any]=4.0 , SCREAMING_SNAKE_CASE__ : List[str]=True , SCREAMING_SNAKE_CASE__ : List[Any]=0.0 , SCREAMING_SNAKE_CASE__ : Optional[int]=0.0 , SCREAMING_SNAKE_CASE__ : Dict=0.1 , SCREAMING_SNAKE_CASE__ : Tuple="gelu" , SCREAMING_SNAKE_CASE__ : Tuple=False , SCREAMING_SNAKE_CASE__ : Any=0.02 , SCREAMING_SNAKE_CASE__ : List[Any]=1e-5 , SCREAMING_SNAKE_CASE__ : int=32 , **SCREAMING_SNAKE_CASE__ : List[str] , ): super().__init__(**SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = image_size lowerCamelCase__ = patch_size lowerCamelCase__ = num_channels lowerCamelCase__ = embed_dim lowerCamelCase__ = depths lowerCamelCase__ = len(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = num_heads lowerCamelCase__ = window_size lowerCamelCase__ = mlp_ratio lowerCamelCase__ = qkv_bias lowerCamelCase__ = hidden_dropout_prob lowerCamelCase__ = attention_probs_dropout_prob lowerCamelCase__ = drop_path_rate lowerCamelCase__ = hidden_act lowerCamelCase__ = use_absolute_embeddings lowerCamelCase__ = layer_norm_eps lowerCamelCase__ = initializer_range lowerCamelCase__ = encoder_stride # we set the hidden_size attribute in order to make Swinv2 work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model lowerCamelCase__ = int(embed_dim * 2 ** (len(SCREAMING_SNAKE_CASE__ ) - 1) ) lowerCamelCase__ = (0, 0, 0, 0)
659
1
"""simple docstring""" from argparse import ArgumentParser from .env import EnvironmentCommand def snake_case ( )-> List[Any]: '''simple docstring''' lowerCamelCase__ = ArgumentParser('Diffusers CLI tool' , usage='diffusers-cli <command> [<args>]' ) lowerCamelCase__ = parser.add_subparsers(help='diffusers-cli command helpers' ) # Register commands EnvironmentCommand.register_subcommand(_a ) # Let's go lowerCamelCase__ = parser.parse_args() if not hasattr(_a , 'func' ): parser.print_help() exit(1 ) # Run lowerCamelCase__ = args.func(_a ) service.run() if __name__ == "__main__": main()
659
"""simple docstring""" def snake_case ( _a: int )-> int: '''simple docstring''' lowerCamelCase__ = 1 for i in range(1 , num + 1 ): fact *= i return fact def snake_case ( _a: int )-> int: '''simple docstring''' lowerCamelCase__ = 0 while number > 0: lowerCamelCase__ = number % 10 sum_of_digits += last_digit lowerCamelCase__ = number // 10 # Removing the last_digit from the given number return sum_of_digits def snake_case ( _a: int = 100 )-> int: '''simple docstring''' lowerCamelCase__ = factorial(_a ) lowerCamelCase__ = split_and_add(_a ) return result if __name__ == "__main__": print(solution(int(input("Enter the Number: ").strip())))
659
1
"""simple docstring""" def snake_case ( _a: Optional[int] , _a: Any , _a: List[Any] , _a: int )-> Optional[int]: '''simple docstring''' global f # a global dp table for knapsack if f[i][j] < 0: if j < wt[i - 1]: lowerCamelCase__ = mf_knapsack(i - 1 , _a , _a , _a ) else: lowerCamelCase__ = max( mf_knapsack(i - 1 , _a , _a , _a ) , mf_knapsack(i - 1 , _a , _a , j - wt[i - 1] ) + val[i - 1] , ) lowerCamelCase__ = val return f[i][j] def snake_case ( _a: str , _a: List[str] , _a: int , _a: Tuple )-> Optional[int]: '''simple docstring''' lowerCamelCase__ = [[0] * (w + 1) for _ in range(n + 1 )] for i in range(1 , n + 1 ): for w_ in range(1 , w + 1 ): if wt[i - 1] <= w_: lowerCamelCase__ = max(val[i - 1] + dp[i - 1][w_ - wt[i - 1]] , dp[i - 1][w_] ) else: lowerCamelCase__ = dp[i - 1][w_] return dp[n][w_], dp def snake_case ( _a: int , _a: list , _a: list )-> str: '''simple docstring''' if not (isinstance(_a , (list, tuple) ) and isinstance(_a , (list, tuple) )): raise ValueError( 'Both the weights and values vectors must be either lists or tuples' ) lowerCamelCase__ = len(_a ) if num_items != len(_a ): lowerCamelCase__ = ( 'The number of weights must be the same as the number of values.\n' F'But got {num_items} weights and {len(_a )} values' ) raise ValueError(_a ) for i in range(_a ): if not isinstance(wt[i] , _a ): lowerCamelCase__ = ( 'All weights must be integers but got weight of ' F'type {type(wt[i] )} at index {i}' ) raise TypeError(_a ) lowerCamelCase__ , lowerCamelCase__ = knapsack(_a , _a , _a , _a ) lowerCamelCase__ = set() _construct_solution(_a , _a , _a , _a , _a ) return optimal_val, example_optional_set def snake_case ( _a: list , _a: list , _a: int , _a: int , _a: set )-> str: '''simple docstring''' if i > 0 and j > 0: if dp[i - 1][j] == dp[i][j]: _construct_solution(_a , _a , i - 1 , _a , _a ) else: optimal_set.add(_a ) _construct_solution(_a , _a , i - 1 , j - wt[i - 1] , _a ) if __name__ == "__main__": _snake_case = [3, 2, 4, 4] _snake_case = [4, 3, 2, 3] _snake_case = 4 _snake_case = 6 _snake_case = [[0] * (w + 1)] + [[0] + [-1] * (w + 1) for _ in range(n + 1)] _snake_case , _snake_case = knapsack(w, wt, val, n) print(optimal_solution) print(mf_knapsack(n, wt, val, w)) # switched the n and w # testing the dynamic programming problem with example # the optimal subset for the above example are items 3 and 4 _snake_case , _snake_case = knapsack_with_example_solution(w, wt, val) assert optimal_solution == 8 assert optimal_subset == {3, 4} print("optimal_value = ", optimal_solution) print("An optimal subset corresponding to the optimal value", optimal_subset)
659
"""simple docstring""" 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 _snake_case = re.compile("[^A-Za-z_0-9]") # parameters used in DuplicationIndex _snake_case = 10 _snake_case = 256 def snake_case ( _a: List[str] )-> Optional[MinHash]: '''simple docstring''' if len(_a ) < MIN_NUM_TOKENS: return None lowerCamelCase__ = MinHash(num_perm=_a ) for token in set(_a ): min_hash.update(token.encode() ) return min_hash def snake_case ( _a: str )-> Set[str]: '''simple docstring''' return {t for t in NON_ALPHA.split(_a ) if len(t.strip() ) > 0} class _a : def __init__( self : List[Any] , *, SCREAMING_SNAKE_CASE__ : float = 0.85 , ): lowerCamelCase__ = duplication_jaccard_threshold lowerCamelCase__ = NUM_PERM lowerCamelCase__ = MinHashLSH(threshold=self._duplication_jaccard_threshold , num_perm=self._num_perm ) lowerCamelCase__ = defaultdict(SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : List[Any] , SCREAMING_SNAKE_CASE__ : Tuple , SCREAMING_SNAKE_CASE__ : MinHash ): lowerCamelCase__ = self._index.query(SCREAMING_SNAKE_CASE__ ) if code_key in self._index.keys: print(F'Duplicate key {code_key}' ) return self._index.insert(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) if len(SCREAMING_SNAKE_CASE__ ) > 0: for base_duplicate in close_duplicates: if base_duplicate in self._duplicate_clusters: self._duplicate_clusters[base_duplicate].add(SCREAMING_SNAKE_CASE__ ) break else: self._duplicate_clusters[close_duplicates[0]].add(SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : str ): lowerCamelCase__ = [] for base, duplicates in self._duplicate_clusters.items(): lowerCamelCase__ = [base] + list(SCREAMING_SNAKE_CASE__ ) # reformat the cluster to be a list of dict lowerCamelCase__ = [{'base_index': el[0], 'repo_name': el[1], 'path': el[2]} for el in cluster] duplicate_clusters.append(SCREAMING_SNAKE_CASE__ ) return duplicate_clusters def _UpperCamelCase ( self : List[str] , SCREAMING_SNAKE_CASE__ : Union[str, Any] ): lowerCamelCase__ = self.get_duplicate_clusters() with open(SCREAMING_SNAKE_CASE__ , 'w' ) as f: json.dump(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) def snake_case ( _a: Union[str, Any] )-> Optional[int]: '''simple docstring''' lowerCamelCase__ , lowerCamelCase__ = element lowerCamelCase__ = 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 snake_case ( _a: Type[Dataset] )-> Tuple: '''simple docstring''' with mp.Pool() as pool: for data in pool.imap_unordered( _compute_min_hash , ThreadedIterator(_a , max_queue_size=10000 ) , chunksize=100 , ): if data is not None: yield data def snake_case ( _a: Type[Dataset] , _a: float )-> Optional[int]: '''simple docstring''' lowerCamelCase__ = 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 snake_case ( _a: str , _a: str )-> float: '''simple docstring''' lowerCamelCase__ = get_tokens(_a ) lowerCamelCase__ = get_tokens(_a ) return len(tokensa & tokensa ) / len(tokensa | tokensa ) _snake_case = None def snake_case ( _a: Dict , _a: Union[str, Any] )-> List[str]: '''simple docstring''' lowerCamelCase__ = [] for elementa in cluster: lowerCamelCase__ = _shared_dataset[elementa['base_index']]['content'] for elementa in extremes: lowerCamelCase__ = _shared_dataset[elementa['base_index']]['content'] if jaccard_similarity(_a , _a ) >= jaccard_threshold: elementa["copies"] += 1 break else: lowerCamelCase__ = 1 extremes.append(_a ) return extremes def snake_case ( _a: Any , _a: Tuple , _a: Dict )-> Union[str, Any]: '''simple docstring''' global _shared_dataset lowerCamelCase__ = dataset lowerCamelCase__ = [] lowerCamelCase__ = 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 snake_case ( _a: Type[Dataset] , _a: float = 0.85 )-> Tuple[Type[Dataset], List[List[Dict]]]: '''simple docstring''' lowerCamelCase__ = make_duplicate_clusters(_a , _a ) lowerCamelCase__ = {x['base_index'] for cluster in duplicate_clusters for x in cluster} lowerCamelCase__ = {} lowerCamelCase__ = find_extremes(_a , _a , _a ) for extremes in extremes_clusters: for element in extremes: lowerCamelCase__ = element lowerCamelCase__ = duplicate_indices - set(extreme_dict.keys() ) lowerCamelCase__ = 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: lowerCamelCase__ = element['base_index'] in extreme_dict if element["is_extreme"]: lowerCamelCase__ = 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
659
1
"""simple docstring""" import logging import os from logging import ( CRITICAL, # NOQA DEBUG, # NOQA ERROR, # NOQA FATAL, # NOQA INFO, # NOQA NOTSET, # NOQA WARN, # NOQA WARNING, # NOQA ) from typing import Optional from tqdm import auto as tqdm_lib _snake_case = { "debug": logging.DEBUG, "info": logging.INFO, "warning": logging.WARNING, "error": logging.ERROR, "critical": logging.CRITICAL, } _snake_case = logging.WARNING def snake_case ( )-> Optional[int]: '''simple docstring''' lowerCamelCase__ = os.getenv('DATASETS_VERBOSITY' , _a ) if env_level_str: if env_level_str in log_levels: return log_levels[env_level_str] else: logging.getLogger().warning( F'Unknown option DATASETS_VERBOSITY={env_level_str}, ' F'has to be one of: { ", ".join(log_levels.keys() ) }' ) return _default_log_level def snake_case ( )-> str: '''simple docstring''' return __name__.split('.' )[0] def snake_case ( )-> logging.Logger: '''simple docstring''' return logging.getLogger(_get_library_name() ) def snake_case ( )-> None: '''simple docstring''' lowerCamelCase__ = _get_library_root_logger() library_root_logger.setLevel(_get_default_logging_level() ) def snake_case ( )-> None: '''simple docstring''' lowerCamelCase__ = _get_library_root_logger() library_root_logger.setLevel(logging.NOTSET ) def snake_case ( _a: Optional[str] = None )-> logging.Logger: '''simple docstring''' if name is None: lowerCamelCase__ = _get_library_name() return logging.getLogger(_a ) def snake_case ( )-> int: '''simple docstring''' return _get_library_root_logger().getEffectiveLevel() def snake_case ( _a: int )-> None: '''simple docstring''' _get_library_root_logger().setLevel(_a ) def snake_case ( )-> Tuple: '''simple docstring''' return set_verbosity(_a ) def snake_case ( )-> Union[str, Any]: '''simple docstring''' return set_verbosity(_a ) def snake_case ( )-> Any: '''simple docstring''' return set_verbosity(_a ) def snake_case ( )-> Optional[int]: '''simple docstring''' return set_verbosity(_a ) def snake_case ( )-> None: '''simple docstring''' lowerCamelCase__ = False def snake_case ( )-> None: '''simple docstring''' lowerCamelCase__ = True # Configure the library root logger at the module level (singleton-like) _configure_library_root_logger() class _a : def __init__( self : int , *SCREAMING_SNAKE_CASE__ : Optional[Any] , **SCREAMING_SNAKE_CASE__ : Union[str, Any] ): # pylint: disable=unused-argument lowerCamelCase__ = args[0] if args else None def __iter__( self : List[str] ): return iter(self._iterator ) def __getattr__( self : Optional[Any] , SCREAMING_SNAKE_CASE__ : Tuple ): def empty_fn(*SCREAMING_SNAKE_CASE__ : Union[str, Any] , **SCREAMING_SNAKE_CASE__ : Any ): # pylint: disable=unused-argument return return empty_fn def __enter__( self : Optional[Any] ): return self def __exit__( self : Any , SCREAMING_SNAKE_CASE__ : Any , SCREAMING_SNAKE_CASE__ : Dict , SCREAMING_SNAKE_CASE__ : Union[str, Any] ): return _snake_case = True class _a : def __call__( self : List[str] , *SCREAMING_SNAKE_CASE__ : Dict , SCREAMING_SNAKE_CASE__ : Tuple=False , **SCREAMING_SNAKE_CASE__ : Optional[Any] ): if _tqdm_active and not disable: return tqdm_lib.tqdm(*SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) else: return EmptyTqdm(*SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : int , *SCREAMING_SNAKE_CASE__ : Optional[int] , **SCREAMING_SNAKE_CASE__ : List[str] ): lowerCamelCase__ = None if _tqdm_active: return tqdm_lib.tqdm.set_lock(*SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : int ): if _tqdm_active: return tqdm_lib.tqdm.get_lock() _snake_case = _tqdm_cls() def snake_case ( )-> bool: '''simple docstring''' global _tqdm_active return bool(_tqdm_active ) def snake_case ( )-> Optional[Any]: '''simple docstring''' global _tqdm_active lowerCamelCase__ = True def snake_case ( )-> str: '''simple docstring''' global _tqdm_active lowerCamelCase__ = False
659
"""simple docstring""" import collections import json import math import os import re import time from fnmatch import fnmatch from typing import Dict import requests from slack_sdk import WebClient _snake_case = WebClient(token=os.environ["CI_SLACK_BOT_TOKEN"]) def snake_case ( _a: Any )-> Optional[Any]: '''simple docstring''' lowerCamelCase__ = test_results.split(' ' ) lowerCamelCase__ = 0 lowerCamelCase__ = 0 # When the output is short enough, the output is surrounded by = signs: "== OUTPUT ==" # When it is too long, those signs are not present. lowerCamelCase__ = expressions[-2] if '=' in expressions[-1] else expressions[-1] for i, expression in enumerate(_a ): if "failed" in expression: failed += int(expressions[i - 1] ) if "passed" in expression: success += int(expressions[i - 1] ) return failed, success, time_spent def snake_case ( _a: Optional[int] )-> Optional[int]: '''simple docstring''' lowerCamelCase__ = {} lowerCamelCase__ = None lowerCamelCase__ = False for line in failures_short_lines.split('\n' ): if re.search(R'_ \[doctest\]' , _a ): lowerCamelCase__ = True lowerCamelCase__ = line.split(' ' )[2] elif in_error and not line.split(' ' )[0].isdigit(): lowerCamelCase__ = line lowerCamelCase__ = False return failures class _a : def __init__( self : Optional[Any] , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : Dict ): lowerCamelCase__ = title lowerCamelCase__ = doc_test_results['time_spent'].split(',' )[0] lowerCamelCase__ = doc_test_results['success'] lowerCamelCase__ = doc_test_results['failures'] lowerCamelCase__ = self.n_success + self.n_failures # Failures and success of the modeling tests lowerCamelCase__ = doc_test_results @property def _UpperCamelCase ( self : List[str] ): lowerCamelCase__ = [self._time_spent] lowerCamelCase__ = 0 for time in time_spent: lowerCamelCase__ = time.split(':' ) # Time can be formatted as xx:xx:xx, as .xx, or as x.xx if the time spent was less than a minute. if len(SCREAMING_SNAKE_CASE__ ) == 1: lowerCamelCase__ = [0, 0, time_parts[0]] lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ = int(time_parts[0] ), int(time_parts[1] ), float(time_parts[2] ) total_secs += hours * 36_00 + minutes * 60 + seconds lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ = total_secs // 36_00, (total_secs % 36_00) // 60, total_secs % 60 return F'{int(SCREAMING_SNAKE_CASE__ )}h{int(SCREAMING_SNAKE_CASE__ )}m{int(SCREAMING_SNAKE_CASE__ )}s' @property def _UpperCamelCase ( self : Dict ): return {"type": "header", "text": {"type": "plain_text", "text": self.title}} @property def _UpperCamelCase ( self : Dict ): return { "type": "section", "text": { "type": "plain_text", "text": F'🌞 There were no failures: all {self.n_tests} tests passed. The suite ran in {self.time}.', "emoji": True, }, "accessory": { "type": "button", "text": {"type": "plain_text", "text": "Check Action results", "emoji": True}, "url": F'https://github.com/huggingface/transformers/actions/runs/{os.environ["GITHUB_RUN_ID"]}', }, } @property def _UpperCamelCase ( self : Any ): return { "type": "section", "text": { "type": "plain_text", "text": ( F'There were {self.n_failures} failures, out of {self.n_tests} tests.\nThe suite ran in' F' {self.time}.' ), "emoji": True, }, "accessory": { "type": "button", "text": {"type": "plain_text", "text": "Check Action results", "emoji": True}, "url": F'https://github.com/huggingface/transformers/actions/runs/{os.environ["GITHUB_RUN_ID"]}', }, } @property def _UpperCamelCase ( self : Union[str, Any] ): lowerCamelCase__ = 40 lowerCamelCase__ = {k: v['failed'] for k, v in doc_test_results.items() if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ )} lowerCamelCase__ = '' for category, failures in category_failures.items(): if len(SCREAMING_SNAKE_CASE__ ) == 0: continue if report != "": report += "\n\n" report += F'*{category} failures*:'.ljust(line_length // 2 ).rjust(line_length // 2 ) + "\n" report += "`" report += "`\n`".join(SCREAMING_SNAKE_CASE__ ) report += "`" return { "type": "section", "text": { "type": "mrkdwn", "text": F'The following examples had failures:\n\n\n{report}\n', }, } @property def _UpperCamelCase ( self : str ): lowerCamelCase__ = [self.header] if self.n_failures > 0: blocks.append(self.failures ) if self.n_failures > 0: blocks.extend([self.category_failures] ) if self.n_failures == 0: blocks.append(self.no_failures ) return json.dumps(SCREAMING_SNAKE_CASE__ ) @staticmethod def _UpperCamelCase ( ): lowerCamelCase__ = [ { 'type': 'section', 'text': { 'type': 'plain_text', 'text': 'There was an issue running the tests.', }, 'accessory': { 'type': 'button', 'text': {'type': 'plain_text', 'text': 'Check Action results', 'emoji': True}, 'url': F'https://github.com/huggingface/transformers/actions/runs/{os.environ["GITHUB_RUN_ID"]}', }, } ] print('Sending the following payload' ) print(json.dumps({'blocks': json.loads(SCREAMING_SNAKE_CASE__ )} ) ) client.chat_postMessage( channel=os.environ['CI_SLACK_CHANNEL_ID_DAILY'] , text='There was an issue running the tests.' , blocks=SCREAMING_SNAKE_CASE__ , ) def _UpperCamelCase ( self : Optional[int] ): print('Sending the following payload' ) print(json.dumps({'blocks': json.loads(self.payload )} ) ) lowerCamelCase__ = F'{self.n_failures} failures out of {self.n_tests} tests,' if self.n_failures else 'All tests passed.' lowerCamelCase__ = client.chat_postMessage( channel=os.environ['CI_SLACK_CHANNEL_ID_DAILY'] , blocks=self.payload , text=SCREAMING_SNAKE_CASE__ , ) def _UpperCamelCase ( self : Optional[Any] , SCREAMING_SNAKE_CASE__ : Dict , SCREAMING_SNAKE_CASE__ : List[Any] , SCREAMING_SNAKE_CASE__ : Tuple , SCREAMING_SNAKE_CASE__ : Tuple ): lowerCamelCase__ = '' for key, value in failures.items(): lowerCamelCase__ = value[:2_00] + ' [Truncated]' if len(SCREAMING_SNAKE_CASE__ ) > 2_50 else value failures_text += F'*{key}*\n_{value}_\n\n' lowerCamelCase__ = job_name lowerCamelCase__ = {'type': 'section', 'text': {'type': 'mrkdwn', 'text': text}} if job_link is not None: lowerCamelCase__ = { 'type': 'button', 'text': {'type': 'plain_text', 'text': 'GitHub Action job', 'emoji': True}, 'url': job_link, } return [ {"type": "header", "text": {"type": "plain_text", "text": title.upper(), "emoji": True}}, content, {"type": "section", "text": {"type": "mrkdwn", "text": failures_text}}, ] def _UpperCamelCase ( self : Optional[int] ): if self.thread_ts is None: raise ValueError('Can only post reply if a post has been made.' ) lowerCamelCase__ = self.doc_test_results.pop('job_link' ) self.doc_test_results.pop('failures' ) self.doc_test_results.pop('success' ) self.doc_test_results.pop('time_spent' ) lowerCamelCase__ = sorted(self.doc_test_results.items() , key=lambda SCREAMING_SNAKE_CASE__ : t[0] ) for job, job_result in sorted_dict: if len(job_result['failures'] ): lowerCamelCase__ = F'*Num failures* :{len(job_result["failed"] )} \n' lowerCamelCase__ = job_result['failures'] lowerCamelCase__ = self.get_reply_blocks(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , text=SCREAMING_SNAKE_CASE__ ) print('Sending the following reply' ) print(json.dumps({'blocks': blocks} ) ) client.chat_postMessage( channel=os.environ['CI_SLACK_CHANNEL_ID_DAILY'] , text=F'Results for {job}' , blocks=SCREAMING_SNAKE_CASE__ , thread_ts=self.thread_ts['ts'] , ) time.sleep(1 ) def snake_case ( )-> Optional[Any]: '''simple docstring''' lowerCamelCase__ = os.environ['GITHUB_RUN_ID'] lowerCamelCase__ = F'https://api.github.com/repos/huggingface/transformers/actions/runs/{run_id}/jobs?per_page=100' lowerCamelCase__ = requests.get(_a ).json() lowerCamelCase__ = {} try: jobs.update({job['name']: job['html_url'] for job in result['jobs']} ) lowerCamelCase__ = math.ceil((result['total_count'] - 100) / 100 ) for i in range(_a ): lowerCamelCase__ = requests.get(url + F'&page={i + 2}' ).json() jobs.update({job['name']: job['html_url'] for job in result['jobs']} ) return jobs except Exception as e: print('Unknown error, could not fetch links.' , _a ) return {} def snake_case ( _a: str )-> Dict: '''simple docstring''' lowerCamelCase__ = {} if os.path.exists(_a ): lowerCamelCase__ = os.listdir(_a ) for file in files: try: with open(os.path.join(_a , _a ) , encoding='utf-8' ) as f: lowerCamelCase__ = f.read() except UnicodeDecodeError as e: raise ValueError(F'Could not open {os.path.join(_a , _a )}.' ) from e return _artifact def snake_case ( )-> Optional[int]: '''simple docstring''' class _a : def __init__( self : Tuple , SCREAMING_SNAKE_CASE__ : str ): lowerCamelCase__ = name lowerCamelCase__ = [] def __str__( self : Dict ): return self.name def _UpperCamelCase ( self : Union[str, Any] , SCREAMING_SNAKE_CASE__ : str ): self.paths.append({'name': self.name, 'path': path} ) lowerCamelCase__ = {} lowerCamelCase__ = filter(os.path.isdir , os.listdir() ) for directory in directories: lowerCamelCase__ = directory if artifact_name not in _available_artifacts: lowerCamelCase__ = Artifact(_a ) _available_artifacts[artifact_name].add_path(_a ) return _available_artifacts if __name__ == "__main__": _snake_case = get_job_links() _snake_case = retrieve_available_artifacts() _snake_case = collections.OrderedDict( [ ("*.py", "API Examples"), ("*.md", "MD Examples"), ] ) # This dict will contain all the information relative to each doc test category: # - failed: list of failed tests # - failures: dict in the format 'test': 'error_message' _snake_case = { v: { "failed": [], "failures": {}, } for v in docs.values() } # Link to the GitHub Action job _snake_case = github_actions_job_links.get("run_doctests") _snake_case = available_artifacts["doc_tests_gpu_test_reports"].paths[0] _snake_case = retrieve_artifact(artifact_path["name"]) if "stats" in artifact: _snake_case , _snake_case , _snake_case = handle_test_results(artifact["stats"]) _snake_case = failed _snake_case = success _snake_case = time_spent[1:-1] + ", " _snake_case = extract_first_line_failure(artifact["failures_short"]) for line in artifact["summary_short"].split("\n"): if re.search("FAILED", line): _snake_case = line.replace("FAILED ", "") _snake_case = line.split()[0].replace("\n", "") if "::" in line: _snake_case , _snake_case = line.split("::") else: _snake_case , _snake_case = line, line for file_regex in docs.keys(): if fnmatch(file_path, file_regex): _snake_case = docs[file_regex] doc_test_results[category]["failed"].append(test) _snake_case = all_failures[test] if test in all_failures else "N/A" _snake_case = failure break _snake_case = Message("🤗 Results of the doc tests.", doc_test_results) message.post() message.post_reply()
659
1
"""simple docstring""" from __future__ import annotations def snake_case ( _a: list[int] , _a: list[int] , _a: int )-> tuple[float, list[float]]: '''simple docstring''' lowerCamelCase__ = list(range(len(_a ) ) ) lowerCamelCase__ = [v / w for v, w in zip(_a , _a )] index.sort(key=lambda _a : ratio[i] , reverse=_a ) lowerCamelCase__ = 0 lowerCamelCase__ = [0] * len(_a ) for i in index: if weight[i] <= capacity: lowerCamelCase__ = 1 max_value += value[i] capacity -= weight[i] else: lowerCamelCase__ = capacity / weight[i] max_value += value[i] * capacity / weight[i] break return max_value, fractions if __name__ == "__main__": import doctest doctest.testmod()
659
"""simple docstring""" 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 _snake_case = logging.get_logger(__name__) # pylint: disable=invalid-name _snake_case = "\n Examples:\n ```py\n >>> from PIL import Image\n >>> import torch\n >>> from diffusers import DiffusionPipeline\n >>> from diffusers.utils import export_to_gif, load_image\n\n >>> device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n\n >>> repo = \"openai/shap-e-img2img\"\n >>> pipe = DiffusionPipeline.from_pretrained(repo, torch_dtype=torch.float16)\n >>> pipe = pipe.to(device)\n\n >>> guidance_scale = 3.0\n >>> image_url = \"https://hf.co/datasets/diffusers/docs-images/resolve/main/shap-e/corgi.png\"\n >>> image = load_image(image_url).convert(\"RGB\")\n\n >>> images = pipe(\n ... image,\n ... guidance_scale=guidance_scale,\n ... num_inference_steps=64,\n ... frame_size=256,\n ... ).images\n\n >>> gif_path = export_to_gif(images[0], \"corgi_3d.gif\")\n ```\n" @dataclass class _a ( SCREAMING_SNAKE_CASE_ ): a_ : Union[PIL.Image.Image, np.ndarray] class _a ( SCREAMING_SNAKE_CASE_ ): def __init__( self : Tuple , SCREAMING_SNAKE_CASE__ : PriorTransformer , SCREAMING_SNAKE_CASE__ : CLIPVisionModel , SCREAMING_SNAKE_CASE__ : CLIPImageProcessor , SCREAMING_SNAKE_CASE__ : HeunDiscreteScheduler , SCREAMING_SNAKE_CASE__ : ShapERenderer , ): super().__init__() self.register_modules( prior=SCREAMING_SNAKE_CASE__ , image_encoder=SCREAMING_SNAKE_CASE__ , image_processor=SCREAMING_SNAKE_CASE__ , scheduler=SCREAMING_SNAKE_CASE__ , renderer=SCREAMING_SNAKE_CASE__ , ) def _UpperCamelCase ( self : str , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : Optional[int] , SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : List[str] ): if latents is None: lowerCamelCase__ = randn_tensor(SCREAMING_SNAKE_CASE__ , generator=SCREAMING_SNAKE_CASE__ , device=SCREAMING_SNAKE_CASE__ , dtype=SCREAMING_SNAKE_CASE__ ) else: if latents.shape != shape: raise ValueError(F'Unexpected latents shape, got {latents.shape}, expected {shape}' ) lowerCamelCase__ = latents.to(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = latents * scheduler.init_noise_sigma return latents def _UpperCamelCase ( self : List[Any] , SCREAMING_SNAKE_CASE__ : List[str]=0 ): if is_accelerate_available(): from accelerate import cpu_offload else: raise ImportError('Please install accelerate via `pip install accelerate`' ) lowerCamelCase__ = torch.device(F'cuda:{gpu_id}' ) lowerCamelCase__ = [self.image_encoder, self.prior] for cpu_offloaded_model in models: if cpu_offloaded_model is not None: cpu_offload(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) @property def _UpperCamelCase ( self : Dict ): 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(SCREAMING_SNAKE_CASE__ , '_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 _UpperCamelCase ( self : Optional[int] , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : Dict , SCREAMING_SNAKE_CASE__ : Optional[Any] , SCREAMING_SNAKE_CASE__ : int , ): if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and isinstance(image[0] , torch.Tensor ): lowerCamelCase__ = torch.cat(SCREAMING_SNAKE_CASE__ , axis=0 ) if image[0].ndim == 4 else torch.stack(SCREAMING_SNAKE_CASE__ , axis=0 ) if not isinstance(SCREAMING_SNAKE_CASE__ , torch.Tensor ): lowerCamelCase__ = self.image_processor(SCREAMING_SNAKE_CASE__ , return_tensors='pt' ).pixel_values[0].unsqueeze(0 ) lowerCamelCase__ = image.to(dtype=self.image_encoder.dtype , device=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = self.image_encoder(SCREAMING_SNAKE_CASE__ )['last_hidden_state'] lowerCamelCase__ = image_embeds[:, 1:, :].contiguous() # batch_size, dim, 256 lowerCamelCase__ = image_embeds.repeat_interleave(SCREAMING_SNAKE_CASE__ , dim=0 ) if do_classifier_free_guidance: lowerCamelCase__ = torch.zeros_like(SCREAMING_SNAKE_CASE__ ) # 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 lowerCamelCase__ = torch.cat([negative_image_embeds, image_embeds] ) return image_embeds @torch.no_grad() @replace_example_docstring(SCREAMING_SNAKE_CASE__ ) def __call__( self : List[Any] , SCREAMING_SNAKE_CASE__ : Union[PIL.Image.Image, List[PIL.Image.Image]] , SCREAMING_SNAKE_CASE__ : int = 1 , SCREAMING_SNAKE_CASE__ : int = 25 , SCREAMING_SNAKE_CASE__ : Optional[Union[torch.Generator, List[torch.Generator]]] = None , SCREAMING_SNAKE_CASE__ : Optional[torch.FloatTensor] = None , SCREAMING_SNAKE_CASE__ : float = 4.0 , SCREAMING_SNAKE_CASE__ : int = 64 , SCREAMING_SNAKE_CASE__ : Optional[str] = "pil" , SCREAMING_SNAKE_CASE__ : bool = True , ): if isinstance(SCREAMING_SNAKE_CASE__ , PIL.Image.Image ): lowerCamelCase__ = 1 elif isinstance(SCREAMING_SNAKE_CASE__ , torch.Tensor ): lowerCamelCase__ = image.shape[0] elif isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and isinstance(image[0] , (torch.Tensor, PIL.Image.Image) ): lowerCamelCase__ = len(SCREAMING_SNAKE_CASE__ ) 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(SCREAMING_SNAKE_CASE__ )}' ) lowerCamelCase__ = self._execution_device lowerCamelCase__ = batch_size * num_images_per_prompt lowerCamelCase__ = guidance_scale > 1.0 lowerCamelCase__ = self._encode_image(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) # prior self.scheduler.set_timesteps(SCREAMING_SNAKE_CASE__ , device=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = self.scheduler.timesteps lowerCamelCase__ = self.prior.config.num_embeddings lowerCamelCase__ = self.prior.config.embedding_dim lowerCamelCase__ = self.prepare_latents( (batch_size, num_embeddings * embedding_dim) , image_embeds.dtype , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , 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 lowerCamelCase__ = latents.reshape(latents.shape[0] , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) for i, t in enumerate(self.progress_bar(SCREAMING_SNAKE_CASE__ ) ): # expand the latents if we are doing classifier free guidance lowerCamelCase__ = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents lowerCamelCase__ = self.scheduler.scale_model_input(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = self.prior( SCREAMING_SNAKE_CASE__ , timestep=SCREAMING_SNAKE_CASE__ , proj_embedding=SCREAMING_SNAKE_CASE__ , ).predicted_image_embedding # remove the variance lowerCamelCase__ , lowerCamelCase__ = noise_pred.split( scaled_model_input.shape[2] , dim=2 ) # batch_size, num_embeddings, embedding_dim if do_classifier_free_guidance is not None: lowerCamelCase__ , lowerCamelCase__ = noise_pred.chunk(2 ) lowerCamelCase__ = noise_pred_uncond + guidance_scale * (noise_pred - noise_pred_uncond) lowerCamelCase__ = self.scheduler.step( SCREAMING_SNAKE_CASE__ , timestep=SCREAMING_SNAKE_CASE__ , sample=SCREAMING_SNAKE_CASE__ , ).prev_sample if output_type == "latent": return ShapEPipelineOutput(images=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = [] for i, latent in enumerate(SCREAMING_SNAKE_CASE__ ): print() lowerCamelCase__ = self.renderer.decode( latent[None, :] , SCREAMING_SNAKE_CASE__ , size=SCREAMING_SNAKE_CASE__ , ray_batch_size=40_96 , n_coarse_samples=64 , n_fine_samples=1_28 , ) images.append(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = torch.stack(SCREAMING_SNAKE_CASE__ ) if output_type not in ["np", "pil"]: raise ValueError(F'Only the output types `pil` and `np` are supported not output_type={output_type}' ) lowerCamelCase__ = images.cpu().numpy() if output_type == "pil": lowerCamelCase__ = [self.numpy_to_pil(SCREAMING_SNAKE_CASE__ ) 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=SCREAMING_SNAKE_CASE__ )
659
1
"""simple docstring""" def snake_case ( _a: List[Any] )-> Optional[int]: '''simple docstring''' lowerCamelCase__ = len(_a ) while cur > 1: # Find the maximum number in arr lowerCamelCase__ = arr.index(max(arr[0:cur] ) ) # Reverse from 0 to mi lowerCamelCase__ = arr[mi::-1] + arr[mi + 1 : len(_a )] # Reverse whole list lowerCamelCase__ = arr[cur - 1 :: -1] + arr[cur : len(_a )] cur -= 1 return arr if __name__ == "__main__": _snake_case = input("Enter numbers separated by a comma:\n").strip() _snake_case = [int(item) for item in user_input.split(",")] print(pancake_sort(unsorted))
659
"""simple docstring""" import os from shutil import copyfile from typing import List, Optional, Tuple from tokenizers import processors from ...tokenization_utils import AddedToken, BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_nllb import NllbTokenizer else: _snake_case = None _snake_case = logging.get_logger(__name__) _snake_case = {"vocab_file": "sentencepiece.bpe.model", "tokenizer_file": "tokenizer.json"} _snake_case = { "vocab_file": { "facebook/nllb-200-distilled-600M": ( "https://huggingface.co/facebook/nllb-200-distilled-600M/resolve/main/sentencepiece.bpe.model" ), }, "tokenizer_file": { "facebook/nllb-200-distilled-600M": ( "https://huggingface.co/facebook/nllb-200-distilled-600M/resolve/main/tokenizer.json" ), }, } _snake_case = { "facebook/nllb-large-en-ro": 1024, "facebook/nllb-200-distilled-600M": 1024, } # fmt: off _snake_case = ["ace_Arab", "ace_Latn", "acm_Arab", "acq_Arab", "aeb_Arab", "afr_Latn", "ajp_Arab", "aka_Latn", "amh_Ethi", "apc_Arab", "arb_Arab", "ars_Arab", "ary_Arab", "arz_Arab", "asm_Beng", "ast_Latn", "awa_Deva", "ayr_Latn", "azb_Arab", "azj_Latn", "bak_Cyrl", "bam_Latn", "ban_Latn", "bel_Cyrl", "bem_Latn", "ben_Beng", "bho_Deva", "bjn_Arab", "bjn_Latn", "bod_Tibt", "bos_Latn", "bug_Latn", "bul_Cyrl", "cat_Latn", "ceb_Latn", "ces_Latn", "cjk_Latn", "ckb_Arab", "crh_Latn", "cym_Latn", "dan_Latn", "deu_Latn", "dik_Latn", "dyu_Latn", "dzo_Tibt", "ell_Grek", "eng_Latn", "epo_Latn", "est_Latn", "eus_Latn", "ewe_Latn", "fao_Latn", "pes_Arab", "fij_Latn", "fin_Latn", "fon_Latn", "fra_Latn", "fur_Latn", "fuv_Latn", "gla_Latn", "gle_Latn", "glg_Latn", "grn_Latn", "guj_Gujr", "hat_Latn", "hau_Latn", "heb_Hebr", "hin_Deva", "hne_Deva", "hrv_Latn", "hun_Latn", "hye_Armn", "ibo_Latn", "ilo_Latn", "ind_Latn", "isl_Latn", "ita_Latn", "jav_Latn", "jpn_Jpan", "kab_Latn", "kac_Latn", "kam_Latn", "kan_Knda", "kas_Arab", "kas_Deva", "kat_Geor", "knc_Arab", "knc_Latn", "kaz_Cyrl", "kbp_Latn", "kea_Latn", "khm_Khmr", "kik_Latn", "kin_Latn", "kir_Cyrl", "kmb_Latn", "kon_Latn", "kor_Hang", "kmr_Latn", "lao_Laoo", "lvs_Latn", "lij_Latn", "lim_Latn", "lin_Latn", "lit_Latn", "lmo_Latn", "ltg_Latn", "ltz_Latn", "lua_Latn", "lug_Latn", "luo_Latn", "lus_Latn", "mag_Deva", "mai_Deva", "mal_Mlym", "mar_Deva", "min_Latn", "mkd_Cyrl", "plt_Latn", "mlt_Latn", "mni_Beng", "khk_Cyrl", "mos_Latn", "mri_Latn", "zsm_Latn", "mya_Mymr", "nld_Latn", "nno_Latn", "nob_Latn", "npi_Deva", "nso_Latn", "nus_Latn", "nya_Latn", "oci_Latn", "gaz_Latn", "ory_Orya", "pag_Latn", "pan_Guru", "pap_Latn", "pol_Latn", "por_Latn", "prs_Arab", "pbt_Arab", "quy_Latn", "ron_Latn", "run_Latn", "rus_Cyrl", "sag_Latn", "san_Deva", "sat_Beng", "scn_Latn", "shn_Mymr", "sin_Sinh", "slk_Latn", "slv_Latn", "smo_Latn", "sna_Latn", "snd_Arab", "som_Latn", "sot_Latn", "spa_Latn", "als_Latn", "srd_Latn", "srp_Cyrl", "ssw_Latn", "sun_Latn", "swe_Latn", "swh_Latn", "szl_Latn", "tam_Taml", "tat_Cyrl", "tel_Telu", "tgk_Cyrl", "tgl_Latn", "tha_Thai", "tir_Ethi", "taq_Latn", "taq_Tfng", "tpi_Latn", "tsn_Latn", "tso_Latn", "tuk_Latn", "tum_Latn", "tur_Latn", "twi_Latn", "tzm_Tfng", "uig_Arab", "ukr_Cyrl", "umb_Latn", "urd_Arab", "uzn_Latn", "vec_Latn", "vie_Latn", "war_Latn", "wol_Latn", "xho_Latn", "ydd_Hebr", "yor_Latn", "yue_Hant", "zho_Hans", "zho_Hant", "zul_Latn"] class _a ( SCREAMING_SNAKE_CASE_ ): a_ : Any = VOCAB_FILES_NAMES a_ : Optional[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES a_ : Optional[Any] = PRETRAINED_VOCAB_FILES_MAP a_ : List[str] = ['input_ids', 'attention_mask'] a_ : Union[str, Any] = NllbTokenizer a_ : List[int] = [] a_ : List[int] = [] def __init__( self : int , SCREAMING_SNAKE_CASE__ : List[Any]=None , SCREAMING_SNAKE_CASE__ : Optional[int]=None , SCREAMING_SNAKE_CASE__ : List[Any]="<s>" , SCREAMING_SNAKE_CASE__ : List[str]="</s>" , SCREAMING_SNAKE_CASE__ : Any="</s>" , SCREAMING_SNAKE_CASE__ : List[str]="<s>" , SCREAMING_SNAKE_CASE__ : Tuple="<unk>" , SCREAMING_SNAKE_CASE__ : Optional[int]="<pad>" , SCREAMING_SNAKE_CASE__ : Any="<mask>" , SCREAMING_SNAKE_CASE__ : Union[str, Any]=None , SCREAMING_SNAKE_CASE__ : int=None , SCREAMING_SNAKE_CASE__ : Dict=None , SCREAMING_SNAKE_CASE__ : Tuple=False , **SCREAMING_SNAKE_CASE__ : str , ): # Mask token behave like a normal word, i.e. include the space before it lowerCamelCase__ = AddedToken(SCREAMING_SNAKE_CASE__ , lstrip=SCREAMING_SNAKE_CASE__ , rstrip=SCREAMING_SNAKE_CASE__ ) if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) else mask_token lowerCamelCase__ = legacy_behaviour super().__init__( vocab_file=SCREAMING_SNAKE_CASE__ , tokenizer_file=SCREAMING_SNAKE_CASE__ , bos_token=SCREAMING_SNAKE_CASE__ , eos_token=SCREAMING_SNAKE_CASE__ , sep_token=SCREAMING_SNAKE_CASE__ , cls_token=SCREAMING_SNAKE_CASE__ , unk_token=SCREAMING_SNAKE_CASE__ , pad_token=SCREAMING_SNAKE_CASE__ , mask_token=SCREAMING_SNAKE_CASE__ , src_lang=SCREAMING_SNAKE_CASE__ , tgt_lang=SCREAMING_SNAKE_CASE__ , additional_special_tokens=SCREAMING_SNAKE_CASE__ , legacy_behaviour=SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ , ) lowerCamelCase__ = vocab_file lowerCamelCase__ = False if not self.vocab_file else True lowerCamelCase__ = FAIRSEQ_LANGUAGE_CODES.copy() if additional_special_tokens is not None: # Only add those special tokens if they are not already there. _additional_special_tokens.extend( [t for t in additional_special_tokens if t not in _additional_special_tokens] ) self.add_special_tokens({'additional_special_tokens': _additional_special_tokens} ) lowerCamelCase__ = { lang_code: self.convert_tokens_to_ids(SCREAMING_SNAKE_CASE__ ) for lang_code in FAIRSEQ_LANGUAGE_CODES } lowerCamelCase__ = src_lang if src_lang is not None else 'eng_Latn' lowerCamelCase__ = self.convert_tokens_to_ids(self._src_lang ) lowerCamelCase__ = tgt_lang self.set_src_lang_special_tokens(self._src_lang ) @property def _UpperCamelCase ( self : str ): return self._src_lang @src_lang.setter def _UpperCamelCase ( self : int , SCREAMING_SNAKE_CASE__ : str ): lowerCamelCase__ = new_src_lang self.set_src_lang_special_tokens(self._src_lang ) def _UpperCamelCase ( self : Tuple , SCREAMING_SNAKE_CASE__ : List[int] , SCREAMING_SNAKE_CASE__ : Optional[List[int]] = None ): if token_ids_a is None: return self.prefix_tokens + token_ids_a + self.suffix_tokens # We don't expect to process pairs, but leave the pair logic for API consistency return self.prefix_tokens + token_ids_a + token_ids_a + self.suffix_tokens def _UpperCamelCase ( self : Optional[Any] , SCREAMING_SNAKE_CASE__ : List[int] , SCREAMING_SNAKE_CASE__ : Optional[List[int]] = None ): lowerCamelCase__ = [self.sep_token_id] lowerCamelCase__ = [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 _UpperCamelCase ( self : str , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : Optional[str] , SCREAMING_SNAKE_CASE__ : Optional[str] , **SCREAMING_SNAKE_CASE__ : Optional[int] ): if src_lang is None or tgt_lang is None: raise ValueError('Translation requires a `src_lang` and a `tgt_lang` for this model' ) lowerCamelCase__ = src_lang lowerCamelCase__ = self(SCREAMING_SNAKE_CASE__ , add_special_tokens=SCREAMING_SNAKE_CASE__ , return_tensors=SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = self.convert_tokens_to_ids(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = tgt_lang_id return inputs def _UpperCamelCase ( self : str , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : str = "eng_Latn" , SCREAMING_SNAKE_CASE__ : Optional[List[str]] = None , SCREAMING_SNAKE_CASE__ : str = "fra_Latn" , **SCREAMING_SNAKE_CASE__ : Dict , ): lowerCamelCase__ = src_lang lowerCamelCase__ = tgt_lang return super().prepare_seqaseq_batch(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : List[str] ): return self.set_src_lang_special_tokens(self.src_lang ) def _UpperCamelCase ( self : List[Any] ): return self.set_tgt_lang_special_tokens(self.tgt_lang ) def _UpperCamelCase ( self : str , SCREAMING_SNAKE_CASE__ : Union[str, Any] ): lowerCamelCase__ = self.convert_tokens_to_ids(SCREAMING_SNAKE_CASE__ ) if self.legacy_behaviour: lowerCamelCase__ = [] lowerCamelCase__ = [self.eos_token_id, self.cur_lang_code] else: lowerCamelCase__ = [self.cur_lang_code] lowerCamelCase__ = [self.eos_token_id] lowerCamelCase__ = self.convert_ids_to_tokens(self.prefix_tokens ) lowerCamelCase__ = self.convert_ids_to_tokens(self.suffix_tokens ) lowerCamelCase__ = processors.TemplateProcessing( single=prefix_tokens_str + ['$A'] + suffix_tokens_str , pair=prefix_tokens_str + ['$A', '$B'] + suffix_tokens_str , special_tokens=list(zip(prefix_tokens_str + suffix_tokens_str , self.prefix_tokens + self.suffix_tokens ) ) , ) def _UpperCamelCase ( self : Union[str, Any] , SCREAMING_SNAKE_CASE__ : str ): lowerCamelCase__ = self.convert_tokens_to_ids(SCREAMING_SNAKE_CASE__ ) if self.legacy_behaviour: lowerCamelCase__ = [] lowerCamelCase__ = [self.eos_token_id, self.cur_lang_code] else: lowerCamelCase__ = [self.cur_lang_code] lowerCamelCase__ = [self.eos_token_id] lowerCamelCase__ = self.convert_ids_to_tokens(self.prefix_tokens ) lowerCamelCase__ = self.convert_ids_to_tokens(self.suffix_tokens ) lowerCamelCase__ = processors.TemplateProcessing( single=prefix_tokens_str + ['$A'] + suffix_tokens_str , pair=prefix_tokens_str + ['$A', '$B'] + suffix_tokens_str , special_tokens=list(zip(prefix_tokens_str + suffix_tokens_str , self.prefix_tokens + self.suffix_tokens ) ) , ) def _UpperCamelCase ( self : Dict , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : Optional[str] = None ): if not self.can_save_slow_tokenizer: raise ValueError( 'Your fast tokenizer does not have the necessary information to save the vocabulary for a slow ' 'tokenizer.' ) if not os.path.isdir(SCREAMING_SNAKE_CASE__ ): logger.error(F'Vocabulary path ({save_directory}) should be a directory.' ) return lowerCamelCase__ = os.path.join( SCREAMING_SNAKE_CASE__ , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(SCREAMING_SNAKE_CASE__ ): copyfile(self.vocab_file , SCREAMING_SNAKE_CASE__ ) return (out_vocab_file,)
659
1
"""simple docstring""" import warnings from diffusers import StableDiffusionImgaImgPipeline # noqa F401 warnings.warn( "The `image_to_image.py` script is outdated. Please use directly `from diffusers import" " StableDiffusionImg2ImgPipeline` instead." )
659
"""simple docstring""" from __future__ import annotations import unittest import numpy as np from transformers import BlipTextConfig from transformers.testing_utils import require_tf, slow from transformers.utils import is_tf_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask if is_tf_available(): import tensorflow as tf from transformers import TFBlipTextModel from transformers.models.blip.modeling_tf_blip import TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST class _a : def __init__( self : List[str] , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : Union[str, Any]=12 , SCREAMING_SNAKE_CASE__ : Union[str, Any]=7 , SCREAMING_SNAKE_CASE__ : Union[str, Any]=True , SCREAMING_SNAKE_CASE__ : Optional[Any]=True , SCREAMING_SNAKE_CASE__ : str=True , SCREAMING_SNAKE_CASE__ : Any=99 , SCREAMING_SNAKE_CASE__ : Optional[Any]=32 , SCREAMING_SNAKE_CASE__ : str=32 , SCREAMING_SNAKE_CASE__ : Tuple=2 , SCREAMING_SNAKE_CASE__ : str=4 , SCREAMING_SNAKE_CASE__ : Optional[Any]=37 , SCREAMING_SNAKE_CASE__ : Union[str, Any]=0.1 , SCREAMING_SNAKE_CASE__ : Optional[Any]=0.1 , SCREAMING_SNAKE_CASE__ : str=5_12 , SCREAMING_SNAKE_CASE__ : str=0.02 , SCREAMING_SNAKE_CASE__ : Union[str, Any]=0 , SCREAMING_SNAKE_CASE__ : Any=None , ): lowerCamelCase__ = parent lowerCamelCase__ = batch_size lowerCamelCase__ = seq_length lowerCamelCase__ = is_training lowerCamelCase__ = use_input_mask lowerCamelCase__ = use_labels lowerCamelCase__ = vocab_size lowerCamelCase__ = hidden_size lowerCamelCase__ = projection_dim lowerCamelCase__ = num_hidden_layers lowerCamelCase__ = num_attention_heads lowerCamelCase__ = intermediate_size lowerCamelCase__ = dropout lowerCamelCase__ = attention_dropout lowerCamelCase__ = max_position_embeddings lowerCamelCase__ = initializer_range lowerCamelCase__ = scope lowerCamelCase__ = bos_token_id def _UpperCamelCase ( self : int ): lowerCamelCase__ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) lowerCamelCase__ = None if self.use_input_mask: lowerCamelCase__ = random_attention_mask([self.batch_size, self.seq_length] ) if input_mask is not None: lowerCamelCase__ = input_mask.numpy() lowerCamelCase__ , lowerCamelCase__ = input_mask.shape lowerCamelCase__ = np.random.randint(1 , seq_length - 1 , size=(batch_size,) ) for batch_idx, start_index in enumerate(SCREAMING_SNAKE_CASE__ ): lowerCamelCase__ = 1 lowerCamelCase__ = 0 lowerCamelCase__ = self.get_config() return config, input_ids, tf.convert_to_tensor(SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : Any ): return BlipTextConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , projection_dim=self.projection_dim , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , dropout=self.dropout , attention_dropout=self.attention_dropout , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , bos_token_id=self.bos_token_id , ) def _UpperCamelCase ( self : Any , SCREAMING_SNAKE_CASE__ : Any , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : List[str] ): lowerCamelCase__ = TFBlipTextModel(config=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = model(SCREAMING_SNAKE_CASE__ , attention_mask=SCREAMING_SNAKE_CASE__ , training=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = model(SCREAMING_SNAKE_CASE__ , training=SCREAMING_SNAKE_CASE__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size) ) def _UpperCamelCase ( self : Dict ): lowerCamelCase__ = self.prepare_config_and_inputs() lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ = config_and_inputs lowerCamelCase__ = {'input_ids': input_ids, 'attention_mask': input_mask} return config, inputs_dict @require_tf class _a ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): a_ : str = (TFBlipTextModel,) if is_tf_available() else () a_ : List[str] = False a_ : Optional[Any] = False a_ : Union[str, Any] = False def _UpperCamelCase ( self : str ): lowerCamelCase__ = BlipTextModelTester(self ) lowerCamelCase__ = ConfigTester(self , config_class=SCREAMING_SNAKE_CASE__ , hidden_size=37 ) def _UpperCamelCase ( self : Tuple ): self.config_tester.run_common_tests() def _UpperCamelCase ( self : str ): lowerCamelCase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : Tuple ): pass def _UpperCamelCase ( self : Tuple ): pass @unittest.skip(reason='Blip does not use inputs_embeds' ) def _UpperCamelCase ( self : List[str] ): pass @unittest.skip(reason='BlipTextModel has no base class and is not available in MODEL_MAPPING' ) def _UpperCamelCase ( self : Dict ): pass @unittest.skip(reason='BlipTextModel has no base class and is not available in MODEL_MAPPING' ) def _UpperCamelCase ( self : List[Any] ): pass @slow def _UpperCamelCase ( self : str ): for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowerCamelCase__ = TFBlipTextModel.from_pretrained(SCREAMING_SNAKE_CASE__ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : Optional[int] , SCREAMING_SNAKE_CASE__ : List[Any]=True ): super().test_pt_tf_model_equivalence(allow_missing_keys=SCREAMING_SNAKE_CASE__ )
659
1
"""simple docstring""" import unittest from transformers import PegasusConfig, PegasusTokenizer, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_configuration_common import ConfigTester from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor if is_flax_available(): import os # The slow tests are often failing with OOM error on GPU # This makes JAX allocate exactly what is needed on demand, and deallocate memory that is no longer needed # but will be slower as stated here https://jax.readthedocs.io/en/latest/gpu_memory_allocation.html _snake_case = "platform" import jax import jax.numpy as jnp import numpy as np from transformers import FlaxPegasusForConditionalGeneration, FlaxPegasusModel @require_flax class _a : a_ : Tuple = PegasusConfig a_ : Dict = {} a_ : Optional[int] = 'gelu' def __init__( self : Union[str, Any] , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : Optional[int]=13 , SCREAMING_SNAKE_CASE__ : Optional[Any]=7 , SCREAMING_SNAKE_CASE__ : Any=True , SCREAMING_SNAKE_CASE__ : List[str]=False , SCREAMING_SNAKE_CASE__ : Optional[int]=99 , SCREAMING_SNAKE_CASE__ : Dict=32 , SCREAMING_SNAKE_CASE__ : Tuple=5 , SCREAMING_SNAKE_CASE__ : List[str]=4 , SCREAMING_SNAKE_CASE__ : int=37 , SCREAMING_SNAKE_CASE__ : str=0.1 , SCREAMING_SNAKE_CASE__ : Optional[int]=0.1 , SCREAMING_SNAKE_CASE__ : List[str]=20 , SCREAMING_SNAKE_CASE__ : Optional[Any]=2 , SCREAMING_SNAKE_CASE__ : List[str]=1 , SCREAMING_SNAKE_CASE__ : Union[str, Any]=0 , ): lowerCamelCase__ = parent lowerCamelCase__ = batch_size lowerCamelCase__ = seq_length lowerCamelCase__ = is_training lowerCamelCase__ = use_labels lowerCamelCase__ = vocab_size lowerCamelCase__ = hidden_size lowerCamelCase__ = num_hidden_layers lowerCamelCase__ = num_attention_heads lowerCamelCase__ = intermediate_size lowerCamelCase__ = hidden_dropout_prob lowerCamelCase__ = attention_probs_dropout_prob lowerCamelCase__ = max_position_embeddings lowerCamelCase__ = eos_token_id lowerCamelCase__ = pad_token_id lowerCamelCase__ = bos_token_id def _UpperCamelCase ( self : Union[str, Any] ): lowerCamelCase__ = ids_tensor([self.batch_size, self.seq_length - 1] , self.vocab_size ).clip(3 , self.vocab_size ) lowerCamelCase__ = np.expand_dims(np.array([self.eos_token_id] * self.batch_size ) , 1 ) lowerCamelCase__ = np.concatenate([input_ids, eos_tensor] , axis=1 ) lowerCamelCase__ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) lowerCamelCase__ = self.config_cls( vocab_size=self.vocab_size , d_model=self.hidden_size , encoder_layers=self.num_hidden_layers , decoder_layers=self.num_hidden_layers , encoder_attention_heads=self.num_attention_heads , decoder_attention_heads=self.num_attention_heads , encoder_ffn_dim=self.intermediate_size , decoder_ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , eos_token_ids=[2] , bos_token_id=self.bos_token_id , pad_token_id=self.pad_token_id , decoder_start_token_id=self.pad_token_id , **self.config_updates , ) lowerCamelCase__ = prepare_pegasus_inputs_dict(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) return config, inputs_dict def _UpperCamelCase ( self : List[Any] , SCREAMING_SNAKE_CASE__ : Union[str, Any] , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : int ): lowerCamelCase__ = 20 lowerCamelCase__ = model_class_name(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = model.encode(inputs_dict['input_ids'] ) lowerCamelCase__ , lowerCamelCase__ = ( inputs_dict['decoder_input_ids'], inputs_dict['decoder_attention_mask'], ) lowerCamelCase__ = model.init_cache(decoder_input_ids.shape[0] , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = jnp.ones((decoder_input_ids.shape[0], max_decoder_length) , dtype='i4' ) lowerCamelCase__ = jnp.broadcast_to( jnp.arange(decoder_input_ids.shape[-1] - 1 )[None, :] , (decoder_input_ids.shape[0], decoder_input_ids.shape[-1] - 1) , ) lowerCamelCase__ = model.decode( decoder_input_ids[:, :-1] , SCREAMING_SNAKE_CASE__ , decoder_attention_mask=SCREAMING_SNAKE_CASE__ , past_key_values=SCREAMING_SNAKE_CASE__ , decoder_position_ids=SCREAMING_SNAKE_CASE__ , ) lowerCamelCase__ = jnp.array(decoder_input_ids.shape[0] * [[decoder_input_ids.shape[-1] - 1]] , dtype='i4' ) lowerCamelCase__ = model.decode( decoder_input_ids[:, -1:] , SCREAMING_SNAKE_CASE__ , decoder_attention_mask=SCREAMING_SNAKE_CASE__ , past_key_values=outputs_cache.past_key_values , decoder_position_ids=SCREAMING_SNAKE_CASE__ , ) lowerCamelCase__ = model.decode(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = 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 _UpperCamelCase ( self : Dict , SCREAMING_SNAKE_CASE__ : Any , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : str ): lowerCamelCase__ = 20 lowerCamelCase__ = model_class_name(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = model.encode(inputs_dict['input_ids'] ) lowerCamelCase__ , lowerCamelCase__ = ( inputs_dict['decoder_input_ids'], inputs_dict['decoder_attention_mask'], ) lowerCamelCase__ = jnp.concatenate( [ decoder_attention_mask, jnp.zeros((decoder_attention_mask.shape[0], max_decoder_length - decoder_attention_mask.shape[1]) ), ] , axis=-1 , ) lowerCamelCase__ = model.init_cache(decoder_input_ids.shape[0] , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = jnp.broadcast_to( jnp.arange(decoder_input_ids.shape[-1] - 1 )[None, :] , (decoder_input_ids.shape[0], decoder_input_ids.shape[-1] - 1) , ) lowerCamelCase__ = model.decode( decoder_input_ids[:, :-1] , SCREAMING_SNAKE_CASE__ , decoder_attention_mask=SCREAMING_SNAKE_CASE__ , past_key_values=SCREAMING_SNAKE_CASE__ , decoder_position_ids=SCREAMING_SNAKE_CASE__ , ) lowerCamelCase__ = jnp.array(decoder_input_ids.shape[0] * [[decoder_input_ids.shape[-1] - 1]] , dtype='i4' ) lowerCamelCase__ = model.decode( decoder_input_ids[:, -1:] , SCREAMING_SNAKE_CASE__ , past_key_values=outputs_cache.past_key_values , decoder_attention_mask=SCREAMING_SNAKE_CASE__ , decoder_position_ids=SCREAMING_SNAKE_CASE__ , ) lowerCamelCase__ = model.decode(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , decoder_attention_mask=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = 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 snake_case ( _a: Optional[int] , _a: int , _a: Optional[Any] , _a: Union[str, Any]=None , _a: List[Any]=None , )-> List[str]: '''simple docstring''' if attention_mask is None: lowerCamelCase__ = np.not_equal(_a , config.pad_token_id ).astype(np.inta ) if decoder_attention_mask is None: lowerCamelCase__ = np.concatenate( [ np.ones(decoder_input_ids[:, :1].shape , dtype=np.inta ), np.not_equal(decoder_input_ids[:, 1:] , config.pad_token_id ).astype(np.inta ), ] , axis=-1 , ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": decoder_attention_mask, } @require_flax class _a ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): a_ : int = ( ( FlaxPegasusForConditionalGeneration, FlaxPegasusModel, ) if is_flax_available() else () ) a_ : List[str] = (FlaxPegasusForConditionalGeneration,) if is_flax_available() else () a_ : Union[str, Any] = True a_ : List[Any] = False a_ : List[Any] = False a_ : int = False def _UpperCamelCase ( self : List[str] ): lowerCamelCase__ = FlaxPegasusModelTester(self ) lowerCamelCase__ = ConfigTester(self , config_class=SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : Union[str, Any] ): self.config_tester.run_common_tests() def _UpperCamelCase ( self : int ): lowerCamelCase__ , lowerCamelCase__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: self.model_tester.check_use_cache_forward(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : int ): lowerCamelCase__ , lowerCamelCase__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: self.model_tester.check_use_cache_forward_with_attn_mask(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : List[Any] ): lowerCamelCase__ , lowerCamelCase__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): lowerCamelCase__ = self._prepare_for_class(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = model_class(SCREAMING_SNAKE_CASE__ ) @jax.jit def encode_jitted(SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : Optional[int]=None , **SCREAMING_SNAKE_CASE__ : Optional[Any] ): return model.encode(input_ids=SCREAMING_SNAKE_CASE__ , attention_mask=SCREAMING_SNAKE_CASE__ ) with self.subTest('JIT Enabled' ): lowerCamelCase__ = encode_jitted(**SCREAMING_SNAKE_CASE__ ).to_tuple() with self.subTest('JIT Disabled' ): with jax.disable_jit(): lowerCamelCase__ = encode_jitted(**SCREAMING_SNAKE_CASE__ ).to_tuple() self.assertEqual(len(SCREAMING_SNAKE_CASE__ ) , len(SCREAMING_SNAKE_CASE__ ) ) for jitted_output, output in zip(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): self.assertEqual(jitted_output.shape , output.shape ) def _UpperCamelCase ( self : Tuple ): lowerCamelCase__ , lowerCamelCase__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): lowerCamelCase__ = model_class(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = model.encode(inputs_dict['input_ids'] , inputs_dict['attention_mask'] ) lowerCamelCase__ = { 'decoder_input_ids': inputs_dict['decoder_input_ids'], 'decoder_attention_mask': inputs_dict['decoder_attention_mask'], 'encoder_outputs': encoder_outputs, } @jax.jit def decode_jitted(SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : List[str] ): return model.decode( decoder_input_ids=SCREAMING_SNAKE_CASE__ , decoder_attention_mask=SCREAMING_SNAKE_CASE__ , encoder_outputs=SCREAMING_SNAKE_CASE__ , ) with self.subTest('JIT Enabled' ): lowerCamelCase__ = decode_jitted(**SCREAMING_SNAKE_CASE__ ).to_tuple() with self.subTest('JIT Disabled' ): with jax.disable_jit(): lowerCamelCase__ = decode_jitted(**SCREAMING_SNAKE_CASE__ ).to_tuple() self.assertEqual(len(SCREAMING_SNAKE_CASE__ ) , len(SCREAMING_SNAKE_CASE__ ) ) for jitted_output, output in zip(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): self.assertEqual(jitted_output.shape , output.shape ) @slow def _UpperCamelCase ( self : Optional[Any] ): for model_class_name in self.all_model_classes: lowerCamelCase__ = model_class_name.from_pretrained('google/pegasus-large' , from_pt=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = np.ones((1, 1) ) lowerCamelCase__ = model(SCREAMING_SNAKE_CASE__ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE__ ) @slow def _UpperCamelCase ( self : Optional[Any] ): lowerCamelCase__ = FlaxPegasusForConditionalGeneration.from_pretrained('google/pegasus-xsum' ) lowerCamelCase__ = PegasusTokenizer.from_pretrained('google/pegasus-xsum' ) lowerCamelCase__ = [ ' PG&E stated it scheduled the blackouts in response to forecasts for high winds amid dry conditions. The aim is to reduce the risk of wildfires. Nearly 800 thousand customers were scheduled to be affected by the shutoffs which were expected to last through at least midday tomorrow.', ' The London trio are up for best UK act and best album, as well as getting two nominations in the best song category."We got told like this morning \'Oh I think you\'re nominated\'", said Dappy."And I was like \'Oh yeah, which one?\' And now we\'ve got nominated for four awards. I mean, wow!"Bandmate Fazer added: "We thought it\'s best of us to come down and mingle with everyone and say hello to the cameras. And now we find we\'ve got four nominations."The band have two shots at the best song prize, getting the nod for their Tynchy Stryder collaboration Number One, and single Strong Again.Their album Uncle B will also go up against records by the likes of Beyonce and Kanye West.N-Dubz picked up the best newcomer Mobo in 2007, but female member Tulisa said they wouldn\'t be too disappointed if they didn\'t win this time around."At the end of the day we\'re grateful to be where we are in our careers."If it don\'t happen then it don\'t happen - live to fight another day and keep on making albums and hits for the fans."Dappy also revealed they could be performing live several times on the night.The group will be doing Number One and also a possible rendition of the War Child single, I Got Soul.The charity song is a re-working of The Killers\' All These Things That I\'ve Done and is set to feature artists like Chipmunk, Ironik and Pixie Lott.This year\'s Mobos will be held outside of London for the first time, in Glasgow on 30 September.N-Dubz said they were looking forward to performing for their Scottish fans and boasted about their recent shows north of the border."We just done Edinburgh the other day," said Dappy."We smashed up an N-Dubz show over there. We done Aberdeen about three or four months ago - we smashed up that show over there! Everywhere we go we smash it up!" ', ] lowerCamelCase__ = [ 'California\'s largest electricity provider has turned off power to hundreds of thousands of customers.', 'Pop group N-Dubz have revealed they were surprised to get four nominations for this year\'s Mobo Awards.', ] lowerCamelCase__ = tokenizer(SCREAMING_SNAKE_CASE__ , return_tensors='np' , truncation=SCREAMING_SNAKE_CASE__ , max_length=5_12 , padding=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = model.generate(**SCREAMING_SNAKE_CASE__ , num_beams=2 ).sequences lowerCamelCase__ = tokenizer.batch_decode(SCREAMING_SNAKE_CASE__ , skip_special_tokens=SCREAMING_SNAKE_CASE__ ) assert tgt_text == decoded
659
"""simple docstring""" import warnings from diffusers import StableDiffusionImgaImgPipeline # noqa F401 warnings.warn( "The `image_to_image.py` script is outdated. Please use directly `from diffusers import" " StableDiffusionImg2ImgPipeline` instead." )
659
1
"""simple docstring""" from functools import reduce _snake_case = ( "73167176531330624919225119674426574742355349194934" "96983520312774506326239578318016984801869478851843" "85861560789112949495459501737958331952853208805511" "12540698747158523863050715693290963295227443043557" "66896648950445244523161731856403098711121722383113" "62229893423380308135336276614282806444486645238749" "30358907296290491560440772390713810515859307960866" "70172427121883998797908792274921901699720888093776" "65727333001053367881220235421809751254540594752243" "52584907711670556013604839586446706324415722155397" "53697817977846174064955149290862569321978468622482" "83972241375657056057490261407972968652414535100474" "82166370484403199890008895243450658541227588666881" "16427171479924442928230863465674813919123162824586" "17866458359124566529476545682848912883142607690042" "24219022671055626321111109370544217506941658960408" "07198403850962455444362981230987879927244284909188" "84580156166097919133875499200524063689912560717606" "05886116467109405077541002256983155200055935729725" "71636269561882670428252483600823257530420752963450" ) def snake_case ( _a: str = N )-> int: '''simple docstring''' return max( # mypy cannot properly interpret reduce int(reduce(lambda _a , _a : str(int(_a ) * int(_a ) ) , n[i : i + 13] ) ) for i in range(len(_a ) - 12 ) ) if __name__ == "__main__": print(f"""{solution() = }""")
659
"""simple docstring""" import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_funnel import FunnelTokenizer _snake_case = logging.get_logger(__name__) _snake_case = {"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"} _snake_case = [ "small", "small-base", "medium", "medium-base", "intermediate", "intermediate-base", "large", "large-base", "xlarge", "xlarge-base", ] _snake_case = { "vocab_file": { "funnel-transformer/small": "https://huggingface.co/funnel-transformer/small/resolve/main/vocab.txt", "funnel-transformer/small-base": "https://huggingface.co/funnel-transformer/small-base/resolve/main/vocab.txt", "funnel-transformer/medium": "https://huggingface.co/funnel-transformer/medium/resolve/main/vocab.txt", "funnel-transformer/medium-base": ( "https://huggingface.co/funnel-transformer/medium-base/resolve/main/vocab.txt" ), "funnel-transformer/intermediate": ( "https://huggingface.co/funnel-transformer/intermediate/resolve/main/vocab.txt" ), "funnel-transformer/intermediate-base": ( "https://huggingface.co/funnel-transformer/intermediate-base/resolve/main/vocab.txt" ), "funnel-transformer/large": "https://huggingface.co/funnel-transformer/large/resolve/main/vocab.txt", "funnel-transformer/large-base": "https://huggingface.co/funnel-transformer/large-base/resolve/main/vocab.txt", "funnel-transformer/xlarge": "https://huggingface.co/funnel-transformer/xlarge/resolve/main/vocab.txt", "funnel-transformer/xlarge-base": ( "https://huggingface.co/funnel-transformer/xlarge-base/resolve/main/vocab.txt" ), }, "tokenizer_file": { "funnel-transformer/small": "https://huggingface.co/funnel-transformer/small/resolve/main/tokenizer.json", "funnel-transformer/small-base": ( "https://huggingface.co/funnel-transformer/small-base/resolve/main/tokenizer.json" ), "funnel-transformer/medium": "https://huggingface.co/funnel-transformer/medium/resolve/main/tokenizer.json", "funnel-transformer/medium-base": ( "https://huggingface.co/funnel-transformer/medium-base/resolve/main/tokenizer.json" ), "funnel-transformer/intermediate": ( "https://huggingface.co/funnel-transformer/intermediate/resolve/main/tokenizer.json" ), "funnel-transformer/intermediate-base": ( "https://huggingface.co/funnel-transformer/intermediate-base/resolve/main/tokenizer.json" ), "funnel-transformer/large": "https://huggingface.co/funnel-transformer/large/resolve/main/tokenizer.json", "funnel-transformer/large-base": ( "https://huggingface.co/funnel-transformer/large-base/resolve/main/tokenizer.json" ), "funnel-transformer/xlarge": "https://huggingface.co/funnel-transformer/xlarge/resolve/main/tokenizer.json", "funnel-transformer/xlarge-base": ( "https://huggingface.co/funnel-transformer/xlarge-base/resolve/main/tokenizer.json" ), }, } _snake_case = {f"""funnel-transformer/{name}""": 512 for name in _model_names} _snake_case = {f"""funnel-transformer/{name}""": {"do_lower_case": True} for name in _model_names} class _a ( SCREAMING_SNAKE_CASE_ ): a_ : List[str] = VOCAB_FILES_NAMES a_ : List[str] = PRETRAINED_VOCAB_FILES_MAP a_ : Optional[int] = PRETRAINED_INIT_CONFIGURATION a_ : List[str] = FunnelTokenizer a_ : Dict = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES a_ : int = 2 def __init__( self : int , SCREAMING_SNAKE_CASE__ : Optional[int]=None , SCREAMING_SNAKE_CASE__ : Union[str, Any]=None , SCREAMING_SNAKE_CASE__ : int=True , SCREAMING_SNAKE_CASE__ : Any="<unk>" , SCREAMING_SNAKE_CASE__ : List[Any]="<sep>" , SCREAMING_SNAKE_CASE__ : int="<pad>" , SCREAMING_SNAKE_CASE__ : Tuple="<cls>" , SCREAMING_SNAKE_CASE__ : Tuple="<mask>" , SCREAMING_SNAKE_CASE__ : Any="<s>" , SCREAMING_SNAKE_CASE__ : Tuple="</s>" , SCREAMING_SNAKE_CASE__ : str=True , SCREAMING_SNAKE_CASE__ : Any=True , SCREAMING_SNAKE_CASE__ : str=None , SCREAMING_SNAKE_CASE__ : int="##" , **SCREAMING_SNAKE_CASE__ : Any , ): super().__init__( SCREAMING_SNAKE_CASE__ , tokenizer_file=SCREAMING_SNAKE_CASE__ , do_lower_case=SCREAMING_SNAKE_CASE__ , unk_token=SCREAMING_SNAKE_CASE__ , sep_token=SCREAMING_SNAKE_CASE__ , pad_token=SCREAMING_SNAKE_CASE__ , cls_token=SCREAMING_SNAKE_CASE__ , mask_token=SCREAMING_SNAKE_CASE__ , bos_token=SCREAMING_SNAKE_CASE__ , eos_token=SCREAMING_SNAKE_CASE__ , clean_text=SCREAMING_SNAKE_CASE__ , tokenize_chinese_chars=SCREAMING_SNAKE_CASE__ , strip_accents=SCREAMING_SNAKE_CASE__ , wordpieces_prefix=SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ , ) lowerCamelCase__ = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get('lowercase' , SCREAMING_SNAKE_CASE__ ) != do_lower_case or normalizer_state.get('strip_accents' , SCREAMING_SNAKE_CASE__ ) != strip_accents or normalizer_state.get('handle_chinese_chars' , SCREAMING_SNAKE_CASE__ ) != tokenize_chinese_chars ): lowerCamelCase__ = getattr(SCREAMING_SNAKE_CASE__ , normalizer_state.pop('type' ) ) lowerCamelCase__ = do_lower_case lowerCamelCase__ = strip_accents lowerCamelCase__ = tokenize_chinese_chars lowerCamelCase__ = normalizer_class(**SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = do_lower_case def _UpperCamelCase ( self : Dict , SCREAMING_SNAKE_CASE__ : Tuple , SCREAMING_SNAKE_CASE__ : Optional[Any]=None ): lowerCamelCase__ = [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 _UpperCamelCase ( self : Dict , SCREAMING_SNAKE_CASE__ : List[int] , SCREAMING_SNAKE_CASE__ : Optional[List[int]] = None ): lowerCamelCase__ = [self.sep_token_id] lowerCamelCase__ = [self.cls_token_id] if token_ids_a is None: return len(cls ) * [self.cls_token_type_id] + len(token_ids_a + sep ) * [0] return len(cls ) * [self.cls_token_type_id] + len(token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def _UpperCamelCase ( self : Optional[int] , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : Optional[str] = None ): lowerCamelCase__ = self._tokenizer.model.save(SCREAMING_SNAKE_CASE__ , name=SCREAMING_SNAKE_CASE__ ) return tuple(SCREAMING_SNAKE_CASE__ )
659
1
"""simple docstring""" import argparse import torch from transformers import GPTaConfig, GPTaModel, load_tf_weights_in_gpta from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging logging.set_verbosity_info() def snake_case ( _a: Optional[int] , _a: Optional[Any] , _a: int )-> Tuple: '''simple docstring''' if gpta_config_file == "": lowerCamelCase__ = GPTaConfig() else: lowerCamelCase__ = GPTaConfig.from_json_file(_a ) lowerCamelCase__ = GPTaModel(_a ) # Load weights from numpy load_tf_weights_in_gpta(_a , _a , _a ) # Save pytorch-model lowerCamelCase__ = pytorch_dump_folder_path + '/' + WEIGHTS_NAME lowerCamelCase__ = pytorch_dump_folder_path + '/' + CONFIG_NAME print(F'Save PyTorch model to {pytorch_weights_dump_path}' ) torch.save(model.state_dict() , _a ) print(F'Save configuration file to {pytorch_config_dump_path}' ) with open(_a , 'w' , encoding='utf-8' ) as f: f.write(config.to_json_string() ) if __name__ == "__main__": _snake_case = argparse.ArgumentParser() # Required parameters parser.add_argument( "--gpt2_checkpoint_path", default=None, type=str, required=True, help="Path to the TensorFlow checkpoint path." ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, required=True, help="Path to the output PyTorch model." ) parser.add_argument( "--gpt2_config_file", default="", type=str, help=( "An optional config json file corresponding to the pre-trained OpenAI model. \n" "This specifies the model architecture." ), ) _snake_case = parser.parse_args() convert_gpta_checkpoint_to_pytorch(args.gpta_checkpoint_path, args.gpta_config_file, args.pytorch_dump_folder_path)
659
"""simple docstring""" # A Bipartite Graph is a graph whose vertices can be divided into two independent sets, # U and V such that every edge (u, v) either connects a vertex from U to V or a vertex # from V to U. In other words, for every edge (u, v), either u belongs to U and v to V, # or u belongs to V and v to U. We can also say that there is no edge that connects # vertices of same set. def snake_case ( _a: Optional[Any] )-> Union[str, Any]: '''simple docstring''' lowerCamelCase__ = [False] * len(_a ) lowerCamelCase__ = [-1] * len(_a ) def dfs(_a: Any , _a: Optional[int] ): lowerCamelCase__ = True lowerCamelCase__ = c for u in graph[v]: if not visited[u]: dfs(_a , 1 - c ) for i in range(len(_a ) ): if not visited[i]: dfs(_a , 0 ) for i in range(len(_a ) ): for j in graph[i]: if color[i] == color[j]: return False return True # Adjacency list of graph _snake_case = {0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 4: []} print(check_bipartite_dfs(graph))
659
1
"""simple docstring""" from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Features, Value from .base import TaskTemplate @dataclass(frozen=SCREAMING_SNAKE_CASE_ ) class _a ( SCREAMING_SNAKE_CASE_ ): a_ : str = field(default='language-modeling' , metadata={'include_in_asdict_even_if_is_default': True} ) a_ : ClassVar[Features] = Features({'text': Value('string' )} ) a_ : ClassVar[Features] = Features({} ) a_ : str = "text" @property def _UpperCamelCase ( self : Union[str, Any] ): return {self.text_column: "text"}
659
"""simple docstring""" from collections.abc import Iterator, MutableMapping from dataclasses import dataclass from typing import Generic, TypeVar _snake_case = TypeVar("KEY") _snake_case = TypeVar("VAL") @dataclass(frozen=SCREAMING_SNAKE_CASE_ , slots=SCREAMING_SNAKE_CASE_ ) class _a ( Generic[KEY, VAL] ): a_ : KEY a_ : VAL class _a ( _Item ): def __init__( self : List[str] ): super().__init__(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) def __bool__( self : str ): return False _snake_case = _DeletedItem() class _a ( MutableMapping[KEY, VAL] ): def __init__( self : Optional[Any] , SCREAMING_SNAKE_CASE__ : int = 8 , SCREAMING_SNAKE_CASE__ : float = 0.75 ): lowerCamelCase__ = initial_block_size lowerCamelCase__ = [None] * initial_block_size assert 0.0 < capacity_factor < 1.0 lowerCamelCase__ = capacity_factor lowerCamelCase__ = 0 def _UpperCamelCase ( self : Optional[int] , SCREAMING_SNAKE_CASE__ : KEY ): return hash(SCREAMING_SNAKE_CASE__ ) % len(self._buckets ) def _UpperCamelCase ( self : str , SCREAMING_SNAKE_CASE__ : int ): return (ind + 1) % len(self._buckets ) def _UpperCamelCase ( self : Union[str, Any] , SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : KEY , SCREAMING_SNAKE_CASE__ : VAL ): lowerCamelCase__ = self._buckets[ind] if not stored: lowerCamelCase__ = _Item(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) self._len += 1 return True elif stored.key == key: lowerCamelCase__ = _Item(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) return True else: return False def _UpperCamelCase ( self : Dict ): lowerCamelCase__ = len(self._buckets ) * self._capacity_factor return len(self ) >= int(SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : int ): if len(self._buckets ) <= self._initial_block_size: return False lowerCamelCase__ = len(self._buckets ) * self._capacity_factor / 2 return len(self ) < limit def _UpperCamelCase ( self : List[str] , SCREAMING_SNAKE_CASE__ : int ): lowerCamelCase__ = self._buckets lowerCamelCase__ = [None] * new_size lowerCamelCase__ = 0 for item in old_buckets: if item: self._add_item(item.key , item.val ) def _UpperCamelCase ( self : List[str] ): self._resize(len(self._buckets ) * 2 ) def _UpperCamelCase ( self : Optional[int] ): self._resize(len(self._buckets ) // 2 ) def _UpperCamelCase ( self : Any , SCREAMING_SNAKE_CASE__ : KEY ): lowerCamelCase__ = self._get_bucket_index(SCREAMING_SNAKE_CASE__ ) for _ in range(len(self._buckets ) ): yield ind lowerCamelCase__ = self._get_next_ind(SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : Optional[Any] , SCREAMING_SNAKE_CASE__ : KEY , SCREAMING_SNAKE_CASE__ : VAL ): for ind in self._iterate_buckets(SCREAMING_SNAKE_CASE__ ): if self._try_set(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): break def __setitem__( self : Dict , SCREAMING_SNAKE_CASE__ : KEY , SCREAMING_SNAKE_CASE__ : VAL ): if self._is_full(): self._size_up() self._add_item(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) def __delitem__( self : Dict , SCREAMING_SNAKE_CASE__ : KEY ): for ind in self._iterate_buckets(SCREAMING_SNAKE_CASE__ ): lowerCamelCase__ = self._buckets[ind] if item is None: raise KeyError(SCREAMING_SNAKE_CASE__ ) if item is _deleted: continue if item.key == key: lowerCamelCase__ = _deleted self._len -= 1 break if self._is_sparse(): self._size_down() def __getitem__( self : str , SCREAMING_SNAKE_CASE__ : KEY ): for ind in self._iterate_buckets(SCREAMING_SNAKE_CASE__ ): lowerCamelCase__ = self._buckets[ind] if item is None: break if item is _deleted: continue if item.key == key: return item.val raise KeyError(SCREAMING_SNAKE_CASE__ ) def __len__( self : List[Any] ): return self._len def __iter__( self : Optional[int] ): yield from (item.key for item in self._buckets if item) def __repr__( self : str ): lowerCamelCase__ = ' ,'.join( F'{item.key}: {item.val}' for item in self._buckets if item ) return F'HashMap({val_string})'
659
1
"""simple docstring""" from string import ascii_lowercase, ascii_uppercase def snake_case ( _a: str )-> str: '''simple docstring''' if not sentence: return "" lowerCamelCase__ = dict(zip(_a , _a ) ) return lower_to_upper.get(sentence[0] , sentence[0] ) + sentence[1:] if __name__ == "__main__": from doctest import testmod testmod()
659
"""simple docstring""" def snake_case ( _a: int , _a: list[int] , _a: int )-> int: '''simple docstring''' def count_of_possible_combinations(_a: int ) -> int: if target < 0: return 0 if target == 0: return 1 return sum(count_of_possible_combinations(target - item ) for item in array ) return count_of_possible_combinations(_a ) def snake_case ( _a: int , _a: list[int] , _a: int )-> int: '''simple docstring''' def count_of_possible_combinations_with_dp_array( _a: int , _a: list[int] ) -> int: if target < 0: return 0 if target == 0: return 1 if dp_array[target] != -1: return dp_array[target] lowerCamelCase__ = sum( count_of_possible_combinations_with_dp_array(target - item , _a ) for item in array ) lowerCamelCase__ = answer return answer lowerCamelCase__ = [-1] * (target + 1) return count_of_possible_combinations_with_dp_array(_a , _a ) def snake_case ( _a: int , _a: list[int] , _a: int )-> int: '''simple docstring''' lowerCamelCase__ = [0] * (target + 1) lowerCamelCase__ = 1 for i in range(1 , target + 1 ): for j in range(_a ): if i - array[j] >= 0: dp_array[i] += dp_array[i - array[j]] return dp_array[target] if __name__ == "__main__": import doctest doctest.testmod() _snake_case = 3 _snake_case = 5 _snake_case = [1, 2, 5] print(combination_sum_iv(n, array, target))
659
1
"""simple docstring""" import json import os import unittest from transformers import OpenAIGPTTokenizer, OpenAIGPTTokenizerFast from transformers.models.openai.tokenization_openai import VOCAB_FILES_NAMES from transformers.testing_utils import require_ftfy, require_spacy, require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class _a ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): a_ : Dict = OpenAIGPTTokenizer a_ : str = OpenAIGPTTokenizerFast a_ : Tuple = True a_ : Any = False def _UpperCamelCase ( self : List[Any] ): super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt lowerCamelCase__ = [ 'l', 'o', 'w', 'e', 'r', 's', 't', 'i', 'd', 'n', 'w</w>', 'r</w>', 't</w>', 'lo', 'low', 'er</w>', 'low</w>', 'lowest</w>', 'newer</w>', 'wider</w>', '<unk>', ] lowerCamelCase__ = dict(zip(SCREAMING_SNAKE_CASE__ , range(len(SCREAMING_SNAKE_CASE__ ) ) ) ) lowerCamelCase__ = ['#version: 0.2', 'l o', 'lo w', 'e r</w>', ''] lowerCamelCase__ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['vocab_file'] ) lowerCamelCase__ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['merges_file'] ) with open(self.vocab_file , 'w' ) as fp: fp.write(json.dumps(SCREAMING_SNAKE_CASE__ ) ) with open(self.merges_file , 'w' ) as fp: fp.write('\n'.join(SCREAMING_SNAKE_CASE__ ) ) def _UpperCamelCase ( self : Tuple , SCREAMING_SNAKE_CASE__ : Dict ): return "lower newer", "lower newer" def _UpperCamelCase ( self : str ): lowerCamelCase__ = OpenAIGPTTokenizer(self.vocab_file , self.merges_file ) lowerCamelCase__ = 'lower' lowerCamelCase__ = ['low', 'er</w>'] lowerCamelCase__ = tokenizer.tokenize(SCREAMING_SNAKE_CASE__ ) self.assertListEqual(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = tokens + ['<unk>'] lowerCamelCase__ = [14, 15, 20] self.assertListEqual(tokenizer.convert_tokens_to_ids(SCREAMING_SNAKE_CASE__ ) , SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : int , SCREAMING_SNAKE_CASE__ : List[Any]=15 ): 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__ ) # Simple input lowerCamelCase__ = 'This is a simple input' lowerCamelCase__ = ['This is a simple input 1', 'This is a simple input 2'] lowerCamelCase__ = ('This is a simple input', 'This is a pair') lowerCamelCase__ = [ ('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 self.assertRaises(SCREAMING_SNAKE_CASE__ , tokenizer_r.encode , SCREAMING_SNAKE_CASE__ , max_length=SCREAMING_SNAKE_CASE__ , padding='max_length' ) # Simple input self.assertRaises(SCREAMING_SNAKE_CASE__ , tokenizer_r.encode_plus , SCREAMING_SNAKE_CASE__ , max_length=SCREAMING_SNAKE_CASE__ , padding='max_length' ) # Simple input self.assertRaises( SCREAMING_SNAKE_CASE__ , tokenizer_r.batch_encode_plus , SCREAMING_SNAKE_CASE__ , max_length=SCREAMING_SNAKE_CASE__ , padding='max_length' , ) # Pair input self.assertRaises(SCREAMING_SNAKE_CASE__ , tokenizer_r.encode , SCREAMING_SNAKE_CASE__ , max_length=SCREAMING_SNAKE_CASE__ , padding='max_length' ) # Pair input self.assertRaises(SCREAMING_SNAKE_CASE__ , tokenizer_r.encode_plus , SCREAMING_SNAKE_CASE__ , max_length=SCREAMING_SNAKE_CASE__ , padding='max_length' ) # Pair input self.assertRaises( SCREAMING_SNAKE_CASE__ , tokenizer_r.batch_encode_plus , SCREAMING_SNAKE_CASE__ , max_length=SCREAMING_SNAKE_CASE__ , padding='max_length' , ) def _UpperCamelCase ( self : str ): pass @require_ftfy @require_spacy @require_tokenizers class _a ( SCREAMING_SNAKE_CASE_ ): pass
659
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available _snake_case = { "configuration_m2m_100": ["M2M_100_PRETRAINED_CONFIG_ARCHIVE_MAP", "M2M100Config", "M2M100OnnxConfig"], "tokenization_m2m_100": ["M2M100Tokenizer"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = [ "M2M_100_PRETRAINED_MODEL_ARCHIVE_LIST", "M2M100ForConditionalGeneration", "M2M100Model", "M2M100PreTrainedModel", ] if TYPE_CHECKING: from .configuration_mam_aaa import M2M_100_PRETRAINED_CONFIG_ARCHIVE_MAP, MaMaaaConfig, MaMaaaOnnxConfig from .tokenization_mam_aaa import MaMaaaTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mam_aaa import ( M2M_100_PRETRAINED_MODEL_ARCHIVE_LIST, MaMaaaForConditionalGeneration, MaMaaaModel, MaMaaaPreTrainedModel, ) else: import sys _snake_case = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
659
1
"""simple docstring""" from collections import Counter import numpy as np from sklearn import datasets from sklearn.model_selection import train_test_split _snake_case = datasets.load_iris() _snake_case = np.array(data["data"]) _snake_case = np.array(data["target"]) _snake_case = data["target_names"] _snake_case , _snake_case , _snake_case , _snake_case = train_test_split(X, y) def snake_case ( _a: Optional[Any] , _a: List[Any] )-> Union[str, Any]: '''simple docstring''' return np.linalg.norm(np.array(_a ) - np.array(_a ) ) def snake_case ( _a: List[Any] , _a: List[Any] , _a: Optional[Any] , _a: List[str] , _a: List[Any]=5 )-> Optional[Any]: '''simple docstring''' lowerCamelCase__ = zip(_a , _a ) # List of distances of all points from the point to be classified lowerCamelCase__ = [] for data_point in data: lowerCamelCase__ = euclidean_distance(data_point[0] , _a ) distances.append((distance, data_point[1]) ) # Choosing 'k' points with the least distances. lowerCamelCase__ = [i[1] for i in sorted(_a )[:k]] # Most commonly occurring class among them # is the class into which the point is classified lowerCamelCase__ = Counter(_a ).most_common(1 )[0][0] return classes[result] if __name__ == "__main__": print(classifier(X_train, y_train, classes, [4.4, 3.1, 1.3, 1.4]))
659
"""simple docstring""" def snake_case ( _a: list[list[float]] )-> list[list[float]]: '''simple docstring''' lowerCamelCase__ = [] for data in source_data: for i, el in enumerate(_a ): if len(_a ) < i + 1: data_lists.append([] ) data_lists[i].append(float(_a ) ) return data_lists def snake_case ( _a: list[list[float]] , _a: list[int] )-> list[list[float]]: '''simple docstring''' lowerCamelCase__ = [] for dlist, weight in zip(_a , _a ): lowerCamelCase__ = min(_a ) lowerCamelCase__ = max(_a ) lowerCamelCase__ = [] # for weight 0 score is 1 - actual score if weight == 0: for item in dlist: try: score.append(1 - ((item - mind) / (maxd - mind)) ) except ZeroDivisionError: score.append(1 ) elif weight == 1: for item in dlist: try: score.append((item - mind) / (maxd - mind) ) except ZeroDivisionError: score.append(0 ) # weight not 0 or 1 else: lowerCamelCase__ = F'Invalid weight of {weight:f} provided' raise ValueError(_a ) score_lists.append(_a ) return score_lists def snake_case ( _a: list[list[float]] )-> list[float]: '''simple docstring''' lowerCamelCase__ = [0 for i in range(len(score_lists[0] ) )] for slist in score_lists: for j, ele in enumerate(_a ): lowerCamelCase__ = final_scores[j] + ele return final_scores def snake_case ( _a: list[list[float]] , _a: list[int] )-> list[list[float]]: '''simple docstring''' lowerCamelCase__ = get_data(_a ) lowerCamelCase__ = calculate_each_score(_a , _a ) lowerCamelCase__ = generate_final_scores(_a ) # append scores to source data for i, ele in enumerate(_a ): source_data[i].append(_a ) return source_data
659
1
"""simple docstring""" import warnings from ...utils import logging from .image_processing_chinese_clip import ChineseCLIPImageProcessor _snake_case = logging.get_logger(__name__) class _a ( SCREAMING_SNAKE_CASE_ ): def __init__( self : List[Any] , *SCREAMING_SNAKE_CASE__ : Any , **SCREAMING_SNAKE_CASE__ : Tuple ): warnings.warn( 'The class ChineseCLIPFeatureExtractor is deprecated and will be removed in version 5 of Transformers.' ' Please use ChineseCLIPImageProcessor instead.' , SCREAMING_SNAKE_CASE__ , ) super().__init__(*SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ )
659
"""simple docstring""" from __future__ import annotations from math import gcd def snake_case ( _a: int , _a: int = 2 , _a: int = 1 , _a: int = 3 , )-> int | None: '''simple docstring''' if num < 2: raise ValueError('The input value cannot be less than 2' ) # Because of the relationship between ``f(f(x))`` and ``f(x)``, this # algorithm struggles to find factors that are divisible by two. # As a workaround, we specifically check for two and even inputs. # See: https://math.stackexchange.com/a/2856214/165820 if num > 2 and num % 2 == 0: return 2 # Pollard's Rho algorithm requires a function that returns pseudorandom # values between 0 <= X < ``num``. It doesn't need to be random in the # sense that the output value is cryptographically secure or difficult # to calculate, it only needs to be random in the sense that all output # values should be equally likely to appear. # For this reason, Pollard suggested using ``f(x) = (x**2 - 1) % num`` # However, the success of Pollard's algorithm isn't guaranteed and is # determined in part by the initial seed and the chosen random function. # To make retries easier, we will instead use ``f(x) = (x**2 + C) % num`` # where ``C`` is a value that we can modify between each attempt. def rand_fn(_a: int , _a: int , _a: int ) -> int: return (pow(_a , 2 ) + step) % modulus for _ in range(_a ): # These track the position within the cycle detection logic. lowerCamelCase__ = seed lowerCamelCase__ = seed while True: # At each iteration, the tortoise moves one step and the hare moves two. lowerCamelCase__ = rand_fn(_a , _a , _a ) lowerCamelCase__ = rand_fn(_a , _a , _a ) lowerCamelCase__ = rand_fn(_a , _a , _a ) # At some point both the tortoise and the hare will enter a cycle whose # length ``p`` is a divisor of ``num``. Once in that cycle, at some point # the tortoise and hare will end up on the same value modulo ``p``. # We can detect when this happens because the position difference between # the tortoise and the hare will share a common divisor with ``num``. lowerCamelCase__ = gcd(hare - tortoise , _a ) if divisor == 1: # No common divisor yet, just keep searching. continue else: # We found a common divisor! if divisor == num: # Unfortunately, the divisor is ``num`` itself and is useless. break else: # The divisor is a nontrivial factor of ``num``! return divisor # If we made it here, then this attempt failed. # We need to pick a new starting seed for the tortoise and hare # in addition to a new step value for the random function. # To keep this example implementation deterministic, the # new values will be generated based on currently available # values instead of using something like ``random.randint``. # We can use the hare's position as the new seed. # This is actually what Richard Brent's the "optimized" variant does. lowerCamelCase__ = hare # The new step value for the random function can just be incremented. # At first the results will be similar to what the old function would # have produced, but the value will quickly diverge after a bit. step += 1 # We haven't found a divisor within the requested number of attempts. # We were unlucky or ``num`` itself is actually prime. return None if __name__ == "__main__": import argparse _snake_case = argparse.ArgumentParser() parser.add_argument( "num", type=int, help="The value to find a divisor of", ) parser.add_argument( "--attempts", type=int, default=3, help="The number of attempts before giving up", ) _snake_case = parser.parse_args() _snake_case = pollard_rho(args.num, attempts=args.attempts) if divisor is None: print(f"""{args.num} is probably prime""") else: _snake_case = args.num // divisor print(f"""{args.num} = {divisor} * {quotient}""")
659
1
"""simple docstring""" import json import os import pickle import shutil import tempfile from unittest import TestCase from unittest.mock import patch import numpy as np from datasets import Dataset from transformers import is_faiss_available from transformers.models.bart.configuration_bart import BartConfig from transformers.models.bart.tokenization_bart import BartTokenizer from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES as DPR_VOCAB_FILES_NAMES from transformers.models.dpr.configuration_dpr import DPRConfig from transformers.models.dpr.tokenization_dpr import DPRContextEncoderTokenizer, DPRQuestionEncoderTokenizer from transformers.models.rag.configuration_rag import RagConfig from transformers.models.rag.retrieval_rag import CustomHFIndex, RagRetriever from transformers.models.roberta.tokenization_roberta import VOCAB_FILES_NAMES as BART_VOCAB_FILES_NAMES from transformers.testing_utils import require_faiss, require_sentencepiece, require_tokenizers, require_torch if is_faiss_available(): import faiss @require_faiss class _a ( SCREAMING_SNAKE_CASE_ ): def _UpperCamelCase ( self : Union[str, Any] ): lowerCamelCase__ = tempfile.mkdtemp() lowerCamelCase__ = 8 # DPR tok lowerCamelCase__ = [ '[UNK]', '[CLS]', '[SEP]', '[PAD]', '[MASK]', 'want', '##want', '##ed', 'wa', 'un', 'runn', '##ing', ',', 'low', 'lowest', ] lowerCamelCase__ = os.path.join(self.tmpdirname , 'dpr_tokenizer' ) os.makedirs(SCREAMING_SNAKE_CASE__ , exist_ok=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = os.path.join(SCREAMING_SNAKE_CASE__ , DPR_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] ) ) # BART tok lowerCamelCase__ = [ 'l', 'o', 'w', 'e', 'r', 's', 't', 'i', 'd', 'n', '\u0120', '\u0120l', '\u0120n', '\u0120lo', '\u0120low', 'er', '\u0120lowest', '\u0120newer', '\u0120wider', '<unk>', ] lowerCamelCase__ = dict(zip(SCREAMING_SNAKE_CASE__ , range(len(SCREAMING_SNAKE_CASE__ ) ) ) ) lowerCamelCase__ = ['#version: 0.2', '\u0120 l', '\u0120l o', '\u0120lo w', 'e r', ''] lowerCamelCase__ = {'unk_token': '<unk>'} lowerCamelCase__ = os.path.join(self.tmpdirname , 'bart_tokenizer' ) os.makedirs(SCREAMING_SNAKE_CASE__ , exist_ok=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = os.path.join(SCREAMING_SNAKE_CASE__ , BART_VOCAB_FILES_NAMES['vocab_file'] ) lowerCamelCase__ = os.path.join(SCREAMING_SNAKE_CASE__ , BART_VOCAB_FILES_NAMES['merges_file'] ) with open(self.vocab_file , 'w' , encoding='utf-8' ) as fp: fp.write(json.dumps(SCREAMING_SNAKE_CASE__ ) + '\n' ) with open(self.merges_file , 'w' , encoding='utf-8' ) as fp: fp.write('\n'.join(SCREAMING_SNAKE_CASE__ ) ) def _UpperCamelCase ( self : Dict ): return DPRQuestionEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname , 'dpr_tokenizer' ) ) def _UpperCamelCase ( self : str ): return DPRContextEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname , 'dpr_tokenizer' ) ) def _UpperCamelCase ( self : Any ): return BartTokenizer.from_pretrained(os.path.join(self.tmpdirname , 'bart_tokenizer' ) ) def _UpperCamelCase ( self : int ): shutil.rmtree(self.tmpdirname ) def _UpperCamelCase ( self : Union[str, Any] ): lowerCamelCase__ = Dataset.from_dict( { 'id': ['0', '1'], 'text': ['foo', 'bar'], 'title': ['Foo', 'Bar'], 'embeddings': [np.ones(self.retrieval_vector_size ), 2 * np.ones(self.retrieval_vector_size )], } ) dataset.add_faiss_index('embeddings' , string_factory='Flat' , metric_type=faiss.METRIC_INNER_PRODUCT ) return dataset def _UpperCamelCase ( self : Tuple ): lowerCamelCase__ = self.get_dummy_dataset() lowerCamelCase__ = RagConfig( retrieval_vector_size=self.retrieval_vector_size , question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() , ) with patch('transformers.models.rag.retrieval_rag.load_dataset' ) as mock_load_dataset: lowerCamelCase__ = dataset lowerCamelCase__ = RagRetriever( SCREAMING_SNAKE_CASE__ , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , ) return retriever def _UpperCamelCase ( self : Tuple , SCREAMING_SNAKE_CASE__ : bool ): lowerCamelCase__ = self.get_dummy_dataset() lowerCamelCase__ = RagConfig( retrieval_vector_size=self.retrieval_vector_size , question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() , index_name='custom' , ) if from_disk: lowerCamelCase__ = os.path.join(self.tmpdirname , 'dataset' ) lowerCamelCase__ = os.path.join(self.tmpdirname , 'index.faiss' ) dataset.get_index('embeddings' ).save(os.path.join(self.tmpdirname , 'index.faiss' ) ) dataset.drop_index('embeddings' ) dataset.save_to_disk(os.path.join(self.tmpdirname , 'dataset' ) ) del dataset lowerCamelCase__ = RagRetriever( SCREAMING_SNAKE_CASE__ , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , ) else: lowerCamelCase__ = RagRetriever( SCREAMING_SNAKE_CASE__ , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , index=CustomHFIndex(config.retrieval_vector_size , SCREAMING_SNAKE_CASE__ ) , ) return retriever def _UpperCamelCase ( self : Tuple ): lowerCamelCase__ = Dataset.from_dict( { 'id': ['0', '1'], 'text': ['foo', 'bar'], 'title': ['Foo', 'Bar'], 'embeddings': [np.ones(self.retrieval_vector_size + 1 ), 2 * np.ones(self.retrieval_vector_size + 1 )], } ) dataset.add_faiss_index('embeddings' , string_factory='Flat' , metric_type=faiss.METRIC_INNER_PRODUCT ) lowerCamelCase__ = os.path.join(self.tmpdirname , 'hf_bert_base.hnswSQ8_correct_phi_128.c_index' ) dataset.save_faiss_index('embeddings' , index_file_name + '.index.dpr' ) pickle.dump(dataset['id'] , open(index_file_name + '.index_meta.dpr' , 'wb' ) ) lowerCamelCase__ = os.path.join(self.tmpdirname , 'psgs_w100.tsv.pkl' ) lowerCamelCase__ = {sample['id']: [sample['text'], sample['title']] for sample in dataset} pickle.dump(SCREAMING_SNAKE_CASE__ , open(SCREAMING_SNAKE_CASE__ , 'wb' ) ) lowerCamelCase__ = RagConfig( retrieval_vector_size=self.retrieval_vector_size , question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() , index_name='legacy' , index_path=self.tmpdirname , ) lowerCamelCase__ = RagRetriever( SCREAMING_SNAKE_CASE__ , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() ) return retriever def _UpperCamelCase ( self : Optional[int] ): lowerCamelCase__ = 1 lowerCamelCase__ = self.get_dummy_canonical_hf_index_retriever() lowerCamelCase__ = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ = retriever.retrieve(SCREAMING_SNAKE_CASE__ , n_docs=SCREAMING_SNAKE_CASE__ ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(SCREAMING_SNAKE_CASE__ ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['embeddings', 'id', 'text', 'title'] ) self.assertEqual(len(doc_dicts[0]['id'] ) , SCREAMING_SNAKE_CASE__ ) self.assertEqual(doc_dicts[0]['id'][0] , '1' ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]['id'][0] , '0' ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def _UpperCamelCase ( self : Optional[int] ): lowerCamelCase__ = self.get_dummy_canonical_hf_index_retriever() with tempfile.TemporaryDirectory() as tmp_dirname: with patch('transformers.models.rag.retrieval_rag.load_dataset' ) as mock_load_dataset: lowerCamelCase__ = self.get_dummy_dataset() retriever.save_pretrained(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = RagRetriever.from_pretrained(SCREAMING_SNAKE_CASE__ ) self.assertIsInstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) lowerCamelCase__ = retriever.retrieve(SCREAMING_SNAKE_CASE__ , n_docs=1 ) self.assertTrue(out is not None ) def _UpperCamelCase ( self : List[Any] ): lowerCamelCase__ = 1 lowerCamelCase__ = self.get_dummy_custom_hf_index_retriever(from_disk=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ = retriever.retrieve(SCREAMING_SNAKE_CASE__ , n_docs=SCREAMING_SNAKE_CASE__ ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(SCREAMING_SNAKE_CASE__ ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['embeddings', 'id', 'text', 'title'] ) self.assertEqual(len(doc_dicts[0]['id'] ) , SCREAMING_SNAKE_CASE__ ) self.assertEqual(doc_dicts[0]['id'][0] , '1' ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]['id'][0] , '0' ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def _UpperCamelCase ( self : Union[str, Any] ): lowerCamelCase__ = self.get_dummy_custom_hf_index_retriever(from_disk=SCREAMING_SNAKE_CASE__ ) with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = RagRetriever.from_pretrained(SCREAMING_SNAKE_CASE__ ) self.assertIsInstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) lowerCamelCase__ = retriever.retrieve(SCREAMING_SNAKE_CASE__ , n_docs=1 ) self.assertTrue(out is not None ) def _UpperCamelCase ( self : str ): lowerCamelCase__ = 1 lowerCamelCase__ = self.get_dummy_custom_hf_index_retriever(from_disk=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ = retriever.retrieve(SCREAMING_SNAKE_CASE__ , n_docs=SCREAMING_SNAKE_CASE__ ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(SCREAMING_SNAKE_CASE__ ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['embeddings', 'id', 'text', 'title'] ) self.assertEqual(len(doc_dicts[0]['id'] ) , SCREAMING_SNAKE_CASE__ ) self.assertEqual(doc_dicts[0]['id'][0] , '1' ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]['id'][0] , '0' ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def _UpperCamelCase ( self : Tuple ): lowerCamelCase__ = self.get_dummy_custom_hf_index_retriever(from_disk=SCREAMING_SNAKE_CASE__ ) with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = RagRetriever.from_pretrained(SCREAMING_SNAKE_CASE__ ) self.assertIsInstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) lowerCamelCase__ = retriever.retrieve(SCREAMING_SNAKE_CASE__ , n_docs=1 ) self.assertTrue(out is not None ) def _UpperCamelCase ( self : str ): lowerCamelCase__ = 1 lowerCamelCase__ = self.get_dummy_legacy_index_retriever() lowerCamelCase__ = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ = retriever.retrieve(SCREAMING_SNAKE_CASE__ , n_docs=SCREAMING_SNAKE_CASE__ ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(SCREAMING_SNAKE_CASE__ ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['text', 'title'] ) self.assertEqual(len(doc_dicts[0]['text'] ) , SCREAMING_SNAKE_CASE__ ) self.assertEqual(doc_dicts[0]['text'][0] , 'bar' ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]['text'][0] , 'foo' ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def _UpperCamelCase ( self : List[str] ): lowerCamelCase__ = self.get_dummy_legacy_index_retriever() with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = RagRetriever.from_pretrained(SCREAMING_SNAKE_CASE__ ) self.assertIsInstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) lowerCamelCase__ = retriever.retrieve(SCREAMING_SNAKE_CASE__ , n_docs=1 ) self.assertTrue(out is not None ) @require_torch @require_tokenizers @require_sentencepiece def _UpperCamelCase ( self : Dict ): import torch lowerCamelCase__ = 1 lowerCamelCase__ = self.get_dummy_canonical_hf_index_retriever() lowerCamelCase__ = [[5, 7], [10, 11]] lowerCamelCase__ = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) lowerCamelCase__ = retriever(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , prefix=retriever.config.generator.prefix , n_docs=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ = ( out['context_input_ids'], out['context_attention_mask'], out['retrieved_doc_embeds'], ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertIsInstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) self.assertIsInstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) self.assertIsInstance(SCREAMING_SNAKE_CASE__ , np.ndarray ) lowerCamelCase__ = retriever( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , prefix=retriever.config.generator.prefix , n_docs=SCREAMING_SNAKE_CASE__ , return_tensors='pt' , ) lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ = ( # noqa: F841 out['context_input_ids'], out['context_attention_mask'], out['retrieved_doc_embeds'], out['doc_ids'], ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertIsInstance(SCREAMING_SNAKE_CASE__ , torch.Tensor ) self.assertIsInstance(SCREAMING_SNAKE_CASE__ , torch.Tensor ) self.assertIsInstance(SCREAMING_SNAKE_CASE__ , torch.Tensor ) @require_torch @require_tokenizers @require_sentencepiece def _UpperCamelCase ( self : Tuple ): lowerCamelCase__ = self.get_dpr_ctx_encoder_tokenizer() lowerCamelCase__ = 1 lowerCamelCase__ = self.get_dummy_custom_hf_index_retriever(from_disk=SCREAMING_SNAKE_CASE__ ) retriever.set_ctx_encoder_tokenizer(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = [[5, 7], [10, 11]] lowerCamelCase__ = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) lowerCamelCase__ = retriever(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , prefix=retriever.config.generator.prefix , n_docs=SCREAMING_SNAKE_CASE__ ) self.assertEqual( len(SCREAMING_SNAKE_CASE__ ) , 6 ) # check whether the retriever output consist of 6 attributes including tokenized docs self.assertEqual( all(k in out for k in ('tokenized_doc_ids', 'tokenized_doc_attention_mask') ) , SCREAMING_SNAKE_CASE__ ) # check for doc token related keys in dictionary.
659
"""simple docstring""" from ...utils import ( OptionalDependencyNotAvailable, is_torch_available, is_transformers_available, is_transformers_version, ) try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import ShapEPipeline else: from .camera import create_pan_cameras from .pipeline_shap_e import ShapEPipeline from .pipeline_shap_e_img2img import ShapEImgaImgPipeline from .renderer import ( BoundingBoxVolume, ImportanceRaySampler, MLPNeRFModelOutput, MLPNeRSTFModel, ShapEParamsProjModel, ShapERenderer, StratifiedRaySampler, VoidNeRFModel, )
659
1
"""simple docstring""" import random import unittest from torch.utils.data import BatchSampler, DataLoader, IterableDataset from accelerate import Accelerator from accelerate.data_loader import ( BatchSamplerShard, DataLoaderDispatcher, DataLoaderShard, IterableDatasetShard, SkipBatchSampler, SkipDataLoader, skip_first_batches, ) class _a ( SCREAMING_SNAKE_CASE_ ): def __init__( self : List[str] , SCREAMING_SNAKE_CASE__ : Optional[Any]=0.01 , SCREAMING_SNAKE_CASE__ : str=10_00 ): lowerCamelCase__ = p_stop lowerCamelCase__ = max_length def __iter__( self : Union[str, Any] ): lowerCamelCase__ = 0 lowerCamelCase__ = False while not stop and count < self.max_length: yield count count += 1 lowerCamelCase__ = random.random() < self.p_stop class _a ( unittest.TestCase ): def _UpperCamelCase ( self : List[Any] , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : Any=False , SCREAMING_SNAKE_CASE__ : Dict=True ): lowerCamelCase__ = [ BatchSamplerShard(SCREAMING_SNAKE_CASE__ , 2 , SCREAMING_SNAKE_CASE__ , split_batches=SCREAMING_SNAKE_CASE__ , even_batches=SCREAMING_SNAKE_CASE__ ) for i in range(2 ) ] lowerCamelCase__ = [list(SCREAMING_SNAKE_CASE__ ) for batch_sampler_shard in batch_sampler_shards] if not split_batches: self.assertListEqual([len(SCREAMING_SNAKE_CASE__ ) for shard in batch_sampler_shards] , [len(SCREAMING_SNAKE_CASE__ ) for e in expected] ) self.assertListEqual(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : Dict ): # Check the shards when the dataset is a round multiple of total batch size. lowerCamelCase__ = BatchSampler(range(24 ) , batch_size=3 , drop_last=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = [ [[0, 1, 2], [6, 7, 8], [12, 13, 14], [18, 19, 20]], [[3, 4, 5], [9, 10, 11], [15, 16, 17], [21, 22, 23]], ] self.check_batch_sampler_shards(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = BatchSampler(range(24 ) , batch_size=3 , drop_last=SCREAMING_SNAKE_CASE__ ) # Expected shouldn't change self.check_batch_sampler_shards(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) # Check the shards when the dataset is a round multiple of batch size but not total batch size. lowerCamelCase__ = BatchSampler(range(21 ) , batch_size=3 , drop_last=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = [ [[0, 1, 2], [6, 7, 8], [12, 13, 14], [18, 19, 20]], [[3, 4, 5], [9, 10, 11], [15, 16, 17], [0, 1, 2]], ] self.check_batch_sampler_shards(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = BatchSampler(range(21 ) , batch_size=3 , drop_last=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = [ [[0, 1, 2], [6, 7, 8], [12, 13, 14]], [[3, 4, 5], [9, 10, 11], [15, 16, 17]], ] self.check_batch_sampler_shards(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) # Check the shards when the dataset is not a round multiple of batch size but has a multiple of # num_processes batch. lowerCamelCase__ = BatchSampler(range(22 ) , batch_size=3 , drop_last=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = [ [[0, 1, 2], [6, 7, 8], [12, 13, 14], [18, 19, 20]], [[3, 4, 5], [9, 10, 11], [15, 16, 17], [21, 0, 1]], ] self.check_batch_sampler_shards(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = BatchSampler(range(22 ) , batch_size=3 , drop_last=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = [ [[0, 1, 2], [6, 7, 8], [12, 13, 14]], [[3, 4, 5], [9, 10, 11], [15, 16, 17]], ] self.check_batch_sampler_shards(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) # Check the shards when the dataset is not a round multiple of batch size but and has not a multiple of # num_processes batch. lowerCamelCase__ = BatchSampler(range(20 ) , batch_size=3 , drop_last=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = [ [[0, 1, 2], [6, 7, 8], [12, 13, 14], [18, 19, 0]], [[3, 4, 5], [9, 10, 11], [15, 16, 17], [1, 2, 3]], ] self.check_batch_sampler_shards(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = BatchSampler(range(20 ) , batch_size=3 , drop_last=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = [ [[0, 1, 2], [6, 7, 8], [12, 13, 14]], [[3, 4, 5], [9, 10, 11], [15, 16, 17]], ] self.check_batch_sampler_shards(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) # Check the shards when the dataset is very small. lowerCamelCase__ = BatchSampler(range(2 ) , batch_size=3 , drop_last=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = [[[0, 1, 0]], [[1, 0, 1]]] self.check_batch_sampler_shards(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = BatchSampler(range(2 ) , batch_size=3 , drop_last=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = [[], []] self.check_batch_sampler_shards(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : Dict ): # Check the shards when the dataset is a round multiple of batch size. lowerCamelCase__ = BatchSampler(range(24 ) , batch_size=4 , drop_last=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = [ [[0, 1], [4, 5], [8, 9], [12, 13], [16, 17], [20, 21]], [[2, 3], [6, 7], [10, 11], [14, 15], [18, 19], [22, 23]], ] self.check_batch_sampler_shards(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , split_batches=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = BatchSampler(range(24 ) , batch_size=4 , drop_last=SCREAMING_SNAKE_CASE__ ) # Expected shouldn't change self.check_batch_sampler_shards(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , split_batches=SCREAMING_SNAKE_CASE__ ) # Check the shards when the dataset is not a round multiple of batch size. lowerCamelCase__ = BatchSampler(range(22 ) , batch_size=4 , drop_last=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = [ [[0, 1], [4, 5], [8, 9], [12, 13], [16, 17], [20, 21]], [[2, 3], [6, 7], [10, 11], [14, 15], [18, 19], [0, 1]], ] self.check_batch_sampler_shards(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , split_batches=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = BatchSampler(range(22 ) , batch_size=4 , drop_last=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = [ [[0, 1], [4, 5], [8, 9], [12, 13], [16, 17]], [[2, 3], [6, 7], [10, 11], [14, 15], [18, 19]], ] self.check_batch_sampler_shards(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , split_batches=SCREAMING_SNAKE_CASE__ ) # Check the shards when the dataset is not a round multiple of batch size or num_processes. lowerCamelCase__ = BatchSampler(range(21 ) , batch_size=4 , drop_last=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = [ [[0, 1], [4, 5], [8, 9], [12, 13], [16, 17], [20, 0]], [[2, 3], [6, 7], [10, 11], [14, 15], [18, 19], [1, 2]], ] self.check_batch_sampler_shards(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , split_batches=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = BatchSampler(range(21 ) , batch_size=4 , drop_last=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = [ [[0, 1], [4, 5], [8, 9], [12, 13], [16, 17]], [[2, 3], [6, 7], [10, 11], [14, 15], [18, 19]], ] self.check_batch_sampler_shards(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , split_batches=SCREAMING_SNAKE_CASE__ ) # Check the shards when the dataset is very small. lowerCamelCase__ = BatchSampler(range(2 ) , batch_size=4 , drop_last=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = [[[0, 1]], [[0, 1]]] self.check_batch_sampler_shards(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , split_batches=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = BatchSampler(range(2 ) , batch_size=4 , drop_last=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = [[], []] self.check_batch_sampler_shards(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , split_batches=SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : str ): # Check the shards when the dataset is a round multiple of total batch size. lowerCamelCase__ = BatchSampler(range(24 ) , batch_size=3 , drop_last=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = [ [[0, 1, 2], [6, 7, 8], [12, 13, 14], [18, 19, 20]], [[3, 4, 5], [9, 10, 11], [15, 16, 17], [21, 22, 23]], ] self.check_batch_sampler_shards(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , even_batches=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = BatchSampler(range(24 ) , batch_size=3 , drop_last=SCREAMING_SNAKE_CASE__ ) # Expected shouldn't change self.check_batch_sampler_shards(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , even_batches=SCREAMING_SNAKE_CASE__ ) # Check the shards when the dataset is a round multiple of batch size but not total batch size. lowerCamelCase__ = BatchSampler(range(21 ) , batch_size=3 , drop_last=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = [ [[0, 1, 2], [6, 7, 8], [12, 13, 14], [18, 19, 20]], [[3, 4, 5], [9, 10, 11], [15, 16, 17]], ] self.check_batch_sampler_shards(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , even_batches=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = BatchSampler(range(21 ) , batch_size=3 , drop_last=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = [ [[0, 1, 2], [6, 7, 8], [12, 13, 14]], [[3, 4, 5], [9, 10, 11], [15, 16, 17]], ] self.check_batch_sampler_shards(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , even_batches=SCREAMING_SNAKE_CASE__ ) # Check the shards when the dataset is not a round multiple of batch size but has a multiple of # num_processes batch. lowerCamelCase__ = BatchSampler(range(22 ) , batch_size=3 , drop_last=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = [ [[0, 1, 2], [6, 7, 8], [12, 13, 14], [18, 19, 20]], [[3, 4, 5], [9, 10, 11], [15, 16, 17], [21]], ] self.check_batch_sampler_shards(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , even_batches=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = BatchSampler(range(22 ) , batch_size=3 , drop_last=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = [ [[0, 1, 2], [6, 7, 8], [12, 13, 14]], [[3, 4, 5], [9, 10, 11], [15, 16, 17]], ] self.check_batch_sampler_shards(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , even_batches=SCREAMING_SNAKE_CASE__ ) # Check the shards when the dataset is not a round multiple of batch size but and has not a multiple of # num_processes batch. lowerCamelCase__ = BatchSampler(range(20 ) , batch_size=3 , drop_last=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = [ [[0, 1, 2], [6, 7, 8], [12, 13, 14], [18, 19]], [[3, 4, 5], [9, 10, 11], [15, 16, 17]], ] self.check_batch_sampler_shards(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , even_batches=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = BatchSampler(range(20 ) , batch_size=3 , drop_last=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = [ [[0, 1, 2], [6, 7, 8], [12, 13, 14]], [[3, 4, 5], [9, 10, 11], [15, 16, 17]], ] self.check_batch_sampler_shards(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , even_batches=SCREAMING_SNAKE_CASE__ ) # Check the shards when the dataset is very small. lowerCamelCase__ = BatchSampler(range(2 ) , batch_size=3 , drop_last=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = [[[0, 1]], []] self.check_batch_sampler_shards(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , even_batches=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = BatchSampler(range(2 ) , batch_size=3 , drop_last=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = [[], []] self.check_batch_sampler_shards(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , even_batches=SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : List[Any] ): # Check the shards when the dataset is a round multiple of batch size. lowerCamelCase__ = BatchSampler(range(24 ) , batch_size=4 , drop_last=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = [ [[0, 1], [4, 5], [8, 9], [12, 13], [16, 17], [20, 21]], [[2, 3], [6, 7], [10, 11], [14, 15], [18, 19], [22, 23]], ] self.check_batch_sampler_shards(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , split_batches=SCREAMING_SNAKE_CASE__ , even_batches=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = BatchSampler(range(24 ) , batch_size=4 , drop_last=SCREAMING_SNAKE_CASE__ ) # Expected shouldn't change self.check_batch_sampler_shards(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , split_batches=SCREAMING_SNAKE_CASE__ , even_batches=SCREAMING_SNAKE_CASE__ ) # Check the shards when the dataset is not a round multiple of batch size. lowerCamelCase__ = BatchSampler(range(22 ) , batch_size=4 , drop_last=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = [ [[0, 1], [4, 5], [8, 9], [12, 13], [16, 17], [20, 21]], [[2, 3], [6, 7], [10, 11], [14, 15], [18, 19]], ] self.check_batch_sampler_shards(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , split_batches=SCREAMING_SNAKE_CASE__ , even_batches=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = BatchSampler(range(22 ) , batch_size=4 , drop_last=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = [ [[0, 1], [4, 5], [8, 9], [12, 13], [16, 17]], [[2, 3], [6, 7], [10, 11], [14, 15], [18, 19]], ] self.check_batch_sampler_shards(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , split_batches=SCREAMING_SNAKE_CASE__ , even_batches=SCREAMING_SNAKE_CASE__ ) # Check the shards when the dataset is not a round multiple of batch size or num_processes. lowerCamelCase__ = BatchSampler(range(21 ) , batch_size=4 , drop_last=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = [ [[0, 1], [4, 5], [8, 9], [12, 13], [16, 17], [20]], [[2, 3], [6, 7], [10, 11], [14, 15], [18, 19]], ] self.check_batch_sampler_shards(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , split_batches=SCREAMING_SNAKE_CASE__ , even_batches=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = BatchSampler(range(21 ) , batch_size=4 , drop_last=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = [ [[0, 1], [4, 5], [8, 9], [12, 13], [16, 17]], [[2, 3], [6, 7], [10, 11], [14, 15], [18, 19]], ] self.check_batch_sampler_shards(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , split_batches=SCREAMING_SNAKE_CASE__ , even_batches=SCREAMING_SNAKE_CASE__ ) # Check the shards when the dataset is very small. lowerCamelCase__ = BatchSampler(range(2 ) , batch_size=4 , drop_last=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = [[[0, 1]], []] self.check_batch_sampler_shards(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , split_batches=SCREAMING_SNAKE_CASE__ , even_batches=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = BatchSampler(range(2 ) , batch_size=4 , drop_last=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = [[], []] self.check_batch_sampler_shards(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , split_batches=SCREAMING_SNAKE_CASE__ , even_batches=SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : int ): lowerCamelCase__ = [[0, 1, 2], [3, 4], [5, 6, 7, 8], [9, 10, 11], [12, 13]] lowerCamelCase__ = [BatchSamplerShard(SCREAMING_SNAKE_CASE__ , 2 , SCREAMING_SNAKE_CASE__ , even_batches=SCREAMING_SNAKE_CASE__ ) for i in range(2 )] self.assertEqual(len(batch_sampler_shards[0] ) , 3 ) self.assertEqual(len(batch_sampler_shards[1] ) , 2 ) self.assertListEqual(list(batch_sampler_shards[0] ) , [[0, 1, 2], [5, 6, 7, 8], [12, 13]] ) self.assertListEqual(list(batch_sampler_shards[1] ) , [[3, 4], [9, 10, 11]] ) def _UpperCamelCase ( self : Optional[int] , SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : List[Any] , SCREAMING_SNAKE_CASE__ : str=False , SCREAMING_SNAKE_CASE__ : int=2 , SCREAMING_SNAKE_CASE__ : List[Any]=False ): random.seed(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = list(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = [ IterableDatasetShard( SCREAMING_SNAKE_CASE__ , batch_size=SCREAMING_SNAKE_CASE__ , drop_last=SCREAMING_SNAKE_CASE__ , num_processes=SCREAMING_SNAKE_CASE__ , process_index=SCREAMING_SNAKE_CASE__ , split_batches=SCREAMING_SNAKE_CASE__ , ) for i in range(SCREAMING_SNAKE_CASE__ ) ] lowerCamelCase__ = [] for iterable_dataset_shard in iterable_dataset_shards: # Since our random iterable dataset will be... random... we need to use a seed to get reproducible results. random.seed(SCREAMING_SNAKE_CASE__ ) iterable_dataset_lists.append(list(SCREAMING_SNAKE_CASE__ ) ) lowerCamelCase__ = batch_size // num_processes if split_batches else batch_size # All iterable dataset shard should have the same length, a round multiple of shard_batch_size lowerCamelCase__ = iterable_dataset_lists[0] for l in iterable_dataset_lists[1:]: self.assertEqual(len(SCREAMING_SNAKE_CASE__ ) , len(SCREAMING_SNAKE_CASE__ ) ) self.assertTrue(len(SCREAMING_SNAKE_CASE__ ) % shard_batch_size == 0 ) lowerCamelCase__ = [] for idx in range(0 , len(SCREAMING_SNAKE_CASE__ ) , SCREAMING_SNAKE_CASE__ ): for l in iterable_dataset_lists: observed += l[idx : idx + shard_batch_size] if not drop_last: while len(SCREAMING_SNAKE_CASE__ ) < len(SCREAMING_SNAKE_CASE__ ): reference += reference self.assertListEqual(SCREAMING_SNAKE_CASE__ , reference[: len(SCREAMING_SNAKE_CASE__ )] ) def _UpperCamelCase ( self : List[str] ): lowerCamelCase__ = 42 lowerCamelCase__ = RandomIterableDataset() self.check_iterable_dataset_shards(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , batch_size=4 , drop_last=SCREAMING_SNAKE_CASE__ , split_batches=SCREAMING_SNAKE_CASE__ ) self.check_iterable_dataset_shards(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , batch_size=4 , drop_last=SCREAMING_SNAKE_CASE__ , split_batches=SCREAMING_SNAKE_CASE__ ) self.check_iterable_dataset_shards(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , batch_size=4 , drop_last=SCREAMING_SNAKE_CASE__ , split_batches=SCREAMING_SNAKE_CASE__ ) self.check_iterable_dataset_shards(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , batch_size=4 , drop_last=SCREAMING_SNAKE_CASE__ , split_batches=SCREAMING_SNAKE_CASE__ ) # Edge case with a very small dataset lowerCamelCase__ = RandomIterableDataset(max_length=2 ) self.check_iterable_dataset_shards(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , batch_size=4 , drop_last=SCREAMING_SNAKE_CASE__ , split_batches=SCREAMING_SNAKE_CASE__ ) self.check_iterable_dataset_shards(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , batch_size=4 , drop_last=SCREAMING_SNAKE_CASE__ , split_batches=SCREAMING_SNAKE_CASE__ ) self.check_iterable_dataset_shards(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , batch_size=4 , drop_last=SCREAMING_SNAKE_CASE__ , split_batches=SCREAMING_SNAKE_CASE__ ) self.check_iterable_dataset_shards(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , batch_size=4 , drop_last=SCREAMING_SNAKE_CASE__ , split_batches=SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : Optional[int] ): lowerCamelCase__ = BatchSampler(range(16 ) , batch_size=4 , drop_last=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = SkipBatchSampler(SCREAMING_SNAKE_CASE__ , 2 ) self.assertListEqual(list(SCREAMING_SNAKE_CASE__ ) , [[8, 9, 10, 11], [12, 13, 14, 15]] ) def _UpperCamelCase ( self : List[Any] ): lowerCamelCase__ = SkipDataLoader(list(range(16 ) ) , batch_size=4 , skip_batches=2 ) self.assertListEqual([t.tolist() for t in dataloader] , [[8, 9, 10, 11], [12, 13, 14, 15]] ) def _UpperCamelCase ( self : Optional[Any] ): lowerCamelCase__ = DataLoader(list(range(16 ) ) , batch_size=4 ) lowerCamelCase__ = skip_first_batches(SCREAMING_SNAKE_CASE__ , num_batches=2 ) self.assertListEqual([t.tolist() for t in new_dataloader] , [[8, 9, 10, 11], [12, 13, 14, 15]] ) def _UpperCamelCase ( self : Union[str, Any] ): lowerCamelCase__ = DataLoaderShard(list(range(16 ) ) , batch_size=4 ) for idx, _ in enumerate(SCREAMING_SNAKE_CASE__ ): self.assertEqual(dataloader.end_of_dataloader , idx == 3 ) # Test it also works on the second iteration for idx, _ in enumerate(SCREAMING_SNAKE_CASE__ ): self.assertEqual(dataloader.end_of_dataloader , idx == 3 ) def _UpperCamelCase ( self : List[Any] ): Accelerator() lowerCamelCase__ = DataLoaderDispatcher(range(16 ) , batch_size=4 ) for idx, _ in enumerate(SCREAMING_SNAKE_CASE__ ): self.assertEqual(dataloader.end_of_dataloader , idx == 3 ) # Test it also works on the second iteration for idx, _ in enumerate(SCREAMING_SNAKE_CASE__ ): self.assertEqual(dataloader.end_of_dataloader , idx == 3 )
659
"""simple docstring""" from __future__ import annotations _snake_case = [ [-1, 0], # left [0, -1], # down [1, 0], # right [0, 1], # up ] def snake_case ( _a: list[list[int]] , _a: list[int] , _a: list[int] , _a: int , _a: list[list[int]] , )-> tuple[list[list[int]], list[list[int]]]: '''simple docstring''' lowerCamelCase__ = [ [0 for col in range(len(grid[0] ) )] for row in range(len(_a ) ) ] # the reference grid lowerCamelCase__ = 1 lowerCamelCase__ = [ [0 for col in range(len(grid[0] ) )] for row in range(len(_a ) ) ] # the action grid lowerCamelCase__ = init[0] lowerCamelCase__ = init[1] lowerCamelCase__ = 0 lowerCamelCase__ = g + heuristic[x][y] # cost from starting cell to destination cell lowerCamelCase__ = [[f, g, x, y]] lowerCamelCase__ = False # flag that is set when search is complete lowerCamelCase__ = False # flag set if we can't find expand while not found and not resign: if len(_a ) == 0: raise ValueError('Algorithm is unable to find solution' ) else: # to choose the least costliest action so as to move closer to the goal cell.sort() cell.reverse() lowerCamelCase__ = cell.pop() lowerCamelCase__ = next_cell[2] lowerCamelCase__ = next_cell[3] lowerCamelCase__ = next_cell[1] if x == goal[0] and y == goal[1]: lowerCamelCase__ = True else: for i in range(len(_a ) ): # to try out different valid actions lowerCamelCase__ = x + DIRECTIONS[i][0] lowerCamelCase__ = y + DIRECTIONS[i][1] if xa >= 0 and xa < len(_a ) and ya >= 0 and ya < len(grid[0] ): if closed[xa][ya] == 0 and grid[xa][ya] == 0: lowerCamelCase__ = g + cost lowerCamelCase__ = ga + heuristic[xa][ya] cell.append([fa, ga, xa, ya] ) lowerCamelCase__ = 1 lowerCamelCase__ = i lowerCamelCase__ = [] lowerCamelCase__ = goal[0] lowerCamelCase__ = goal[1] invpath.append([x, y] ) # we get the reverse path from here while x != init[0] or y != init[1]: lowerCamelCase__ = x - DIRECTIONS[action[x][y]][0] lowerCamelCase__ = y - DIRECTIONS[action[x][y]][1] lowerCamelCase__ = xa lowerCamelCase__ = ya invpath.append([x, y] ) lowerCamelCase__ = [] for i in range(len(_a ) ): path.append(invpath[len(_a ) - 1 - i] ) return path, action if __name__ == "__main__": _snake_case = [ [0, 1, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles [0, 1, 0, 0, 0, 0], [0, 1, 0, 0, 1, 0], [0, 0, 0, 0, 1, 0], ] _snake_case = [0, 0] # all coordinates are given in format [y,x] _snake_case = [len(grid) - 1, len(grid[0]) - 1] _snake_case = 1 # the cost map which pushes the path closer to the goal _snake_case = [[0 for row in range(len(grid[0]))] for col in range(len(grid))] for i in range(len(grid)): for j in range(len(grid[0])): _snake_case = abs(i - goal[0]) + abs(j - goal[1]) if grid[i][j] == 1: # added extra penalty in the heuristic map _snake_case = 99 _snake_case , _snake_case = search(grid, init, goal, cost, heuristic) print("ACTION MAP") for i in range(len(action)): print(action[i]) for i in range(len(path)): print(path[i])
659
1
"""simple docstring""" import json import multiprocessing import os import re from collections import defaultdict import torch from accelerate import Accelerator from accelerate.utils import set_seed from arguments import HumanEvalArguments from datasets import load_dataset, load_metric from torch.utils.data import IterableDataset from torch.utils.data.dataloader import DataLoader from tqdm import tqdm import transformers from transformers import AutoModelForCausalLM, AutoTokenizer, HfArgumentParser, StoppingCriteria, StoppingCriteriaList _snake_case = ["\nclass", "\ndef", "\n#", "\n@", "\nprint", "\nif"] class _a ( SCREAMING_SNAKE_CASE_ ): def __init__( self : List[str] , SCREAMING_SNAKE_CASE__ : Any , SCREAMING_SNAKE_CASE__ : List[Any] , SCREAMING_SNAKE_CASE__ : int=None , SCREAMING_SNAKE_CASE__ : Optional[int]=1 ): lowerCamelCase__ = tokenizer lowerCamelCase__ = dataset lowerCamelCase__ = len(SCREAMING_SNAKE_CASE__ ) if n_tasks is None else n_tasks lowerCamelCase__ = n_copies def __iter__( self : Any ): lowerCamelCase__ = [] for task in range(self.n_tasks ): # without strip, the model generate commented codes ... prompts.append(self.tokenizer.eos_token + self.dataset[task]['prompt'].strip() ) lowerCamelCase__ = self.tokenizer(SCREAMING_SNAKE_CASE__ , padding=SCREAMING_SNAKE_CASE__ , return_tensors='pt' ) for task in range(self.n_tasks ): for _ in range(self.n_copies ): yield { "ids": outputs.input_ids[task], "task_id": task, "input_len": outputs.attention_mask[task].sum(), } class _a ( SCREAMING_SNAKE_CASE_ ): def __init__( self : Dict , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : List[Any] , SCREAMING_SNAKE_CASE__ : Dict ): lowerCamelCase__ = start_length lowerCamelCase__ = eof_strings lowerCamelCase__ = tokenizer def __call__( self : Any , SCREAMING_SNAKE_CASE__ : Any , SCREAMING_SNAKE_CASE__ : List[str] , **SCREAMING_SNAKE_CASE__ : Dict ): lowerCamelCase__ = self.tokenizer.batch_decode(input_ids[:, self.start_length :] ) lowerCamelCase__ = [] for decoded_generation in decoded_generations: done.append(any(stop_string in decoded_generation for stop_string in self.eof_strings ) ) return all(SCREAMING_SNAKE_CASE__ ) def snake_case ( _a: List[Any] )-> Dict: '''simple docstring''' lowerCamelCase__ = re.split('(%s)' % '|'.join(_a ) , _a ) # last string should be "" return "".join(string_list[:-2] ) def snake_case ( _a: List[Any] , _a: Optional[int] , _a: str , _a: Union[str, Any] , _a: Dict , _a: Optional[int]=20 , **_a: Optional[int] )-> List[str]: '''simple docstring''' lowerCamelCase__ = defaultdict(_a ) # dict of list of generated tokens for step, batch in tqdm(enumerate(_a ) ): with torch.no_grad(): lowerCamelCase__ = batch['ids'].shape[-1] lowerCamelCase__ = accelerator.unwrap_model(_a ).generate( input_ids=batch['ids'][:, : batch['input_len']] , num_return_sequences=_a , **_a ) # each task is generated batch_size times lowerCamelCase__ = batch['task_id'].repeat(_a ) lowerCamelCase__ = accelerator.pad_across_processes( _a , dim=1 , pad_index=tokenizer.pad_token_id ) lowerCamelCase__ , lowerCamelCase__ = accelerator.gather((generated_tokens, generated_tasks) ) lowerCamelCase__ = generated_tokens.cpu().numpy() lowerCamelCase__ = generated_tasks.cpu().numpy() for task, generated_tokens in zip(_a , _a ): gen_token_dict[task].append(_a ) lowerCamelCase__ = [[] for _ in range(_a )] for task, generated_tokens in gen_token_dict.items(): for s in generated_tokens: lowerCamelCase__ = tokenizer.decode(_a , skip_special_tokens=_a , clean_up_tokenization_spaces=_a ) code_gens[task].append(remove_last_block(_a ) ) return code_gens def snake_case ( )-> Union[str, Any]: '''simple docstring''' lowerCamelCase__ = HfArgumentParser(_a ) lowerCamelCase__ = parser.parse_args() transformers.logging.set_verbosity_error() # enables code execution in code_eval metric lowerCamelCase__ = args.HF_ALLOW_CODE_EVAL # make sure tokenizer plays nice with multiprocessing lowerCamelCase__ = 'false' if args.num_workers is None: lowerCamelCase__ = multiprocessing.cpu_count() # Use dataset load to feed to accelerate lowerCamelCase__ = Accelerator() set_seed(args.seed , device_specific=_a ) # Load model and tokenizer lowerCamelCase__ = AutoTokenizer.from_pretrained(args.model_ckpt ) lowerCamelCase__ = tokenizer.eos_token lowerCamelCase__ = AutoModelForCausalLM.from_pretrained(args.model_ckpt ) # Generation settings lowerCamelCase__ = { 'do_sample': args.do_sample, 'temperature': args.temperature, 'max_new_tokens': args.max_new_tokens, 'top_p': args.top_p, 'top_k': args.top_k, 'stopping_criteria': StoppingCriteriaList([EndOfFunctionCriteria(0 , _a , _a )] ), } # Load evaluation dataset and metric lowerCamelCase__ = load_dataset('openai_humaneval' ) lowerCamelCase__ = load_metric('code_eval' ) lowerCamelCase__ = args.num_tasks if args.num_tasks is not None else len(human_eval['test'] ) lowerCamelCase__ = args.n_samples // args.batch_size lowerCamelCase__ = TokenizedDataset(_a , human_eval['test'] , n_copies=_a , n_tasks=_a ) # do not confuse args.batch_size, which is actually the num_return_sequences lowerCamelCase__ = DataLoader(_a , batch_size=1 ) # Run a quick test to see if code evaluation is enabled try: lowerCamelCase__ = code_eval_metric.compute(references=[''] , predictions=[['']] ) except ValueError as exception: print( 'Code evaluation not enabled. Read the warning below carefully and then use `--HF_ALLOW_CODE_EVAL="1"`' ' flag to enable code evaluation.' ) raise exception lowerCamelCase__ , lowerCamelCase__ = accelerator.prepare(_a , _a ) lowerCamelCase__ = complete_code( _a , _a , _a , _a , n_tasks=_a , batch_size=args.batch_size , **_a , ) if accelerator.is_main_process: lowerCamelCase__ = [] for task in tqdm(range(_a ) ): lowerCamelCase__ = human_eval['test'][task]['test'] lowerCamelCase__ = F'check({human_eval["test"][task]["entry_point"]})' references.append('\n' + test_func + '\n' + entry_point ) # Evaluate completions with "code_eval" metric lowerCamelCase__ , lowerCamelCase__ = code_eval_metric.compute( references=_a , predictions=_a , num_workers=args.num_workers ) print(F'Results: {pass_at_k}' ) # Save results to json file with open(args.output_file , 'w' ) as fp: json.dump(_a , _a ) # For some reason the folliwng seems to be necessary sometimes for code_eval to work nice with multiprocessing # https://stackoverflow.com/questions/60804599/python-multiprocessing-keeps-spawning-the-whole-script if __name__ == "__main__": main()
659
"""simple docstring""" def snake_case ( _a: int = 4000000 )-> int: '''simple docstring''' lowerCamelCase__ = [0, 1] lowerCamelCase__ = 0 while fib[i] <= n: fib.append(fib[i] + fib[i + 1] ) if fib[i + 2] > n: break i += 1 lowerCamelCase__ = 0 for j in range(len(_a ) - 1 ): if fib[j] % 2 == 0: total += fib[j] return total if __name__ == "__main__": print(f"""{solution() = }""")
659
1
"""simple docstring""" def snake_case ( _a: int )-> list: '''simple docstring''' lowerCamelCase__ = int(_a ) if n_element < 1: lowerCamelCase__ = ValueError('a should be a positive number' ) raise my_error lowerCamelCase__ = [1] lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ = (0, 0, 0) lowerCamelCase__ = 1 while index < n_element: while hamming_list[i] * 2 <= hamming_list[-1]: i += 1 while hamming_list[j] * 3 <= hamming_list[-1]: j += 1 while hamming_list[k] * 5 <= hamming_list[-1]: k += 1 hamming_list.append( min(hamming_list[i] * 2 , hamming_list[j] * 3 , hamming_list[k] * 5 ) ) index += 1 return hamming_list if __name__ == "__main__": _snake_case = input("Enter the last number (nth term) of the Hamming Number Series: ") print("Formula of Hamming Number Series => 2^i * 3^j * 5^k") _snake_case = hamming(int(n)) print("-----------------------------------------------------") print(f"""The list with nth numbers is: {hamming_numbers}""") print("-----------------------------------------------------")
659
"""simple docstring""" def snake_case ( _a: List[Any] , _a: Any , _a: str , _a: List[Any] )-> List[Any]: '''simple docstring''' lowerCamelCase__ = [False] * len(_a ) lowerCamelCase__ = [] queue.append(_a ) lowerCamelCase__ = True while queue: lowerCamelCase__ = queue.pop(0 ) for ind in range(len(graph[u] ) ): if visited[ind] is False and graph[u][ind] > 0: queue.append(_a ) lowerCamelCase__ = True lowerCamelCase__ = u return visited[t] def snake_case ( _a: List[Any] , _a: str , _a: List[str] )-> Optional[int]: '''simple docstring''' lowerCamelCase__ = [-1] * (len(_a )) lowerCamelCase__ = 0 while bfs(_a , _a , _a , _a ): lowerCamelCase__ = float('Inf' ) lowerCamelCase__ = sink while s != source: # Find the minimum value in select path lowerCamelCase__ = min(_a , graph[parent[s]][s] ) lowerCamelCase__ = parent[s] max_flow += path_flow lowerCamelCase__ = sink while v != source: lowerCamelCase__ = parent[v] graph[u][v] -= path_flow graph[v][u] += path_flow lowerCamelCase__ = parent[v] return max_flow _snake_case = [ [0, 16, 13, 0, 0, 0], [0, 0, 10, 12, 0, 0], [0, 4, 0, 0, 14, 0], [0, 0, 9, 0, 0, 20], [0, 0, 0, 7, 0, 4], [0, 0, 0, 0, 0, 0], ] _snake_case , _snake_case = 0, 5 print(ford_fulkerson(graph, source, sink))
659
1
"""simple docstring""" import gc import unittest from diffusers import FlaxControlNetModel, FlaxStableDiffusionControlNetPipeline from diffusers.utils import is_flax_available, load_image, slow from diffusers.utils.testing_utils import require_flax if is_flax_available(): import jax import jax.numpy as jnp from flax.jax_utils import replicate from flax.training.common_utils import shard @slow @require_flax class _a ( unittest.TestCase ): def _UpperCamelCase ( self : str ): # clean up the VRAM after each test super().tearDown() gc.collect() def _UpperCamelCase ( self : Tuple ): lowerCamelCase__ , lowerCamelCase__ = FlaxControlNetModel.from_pretrained( 'lllyasviel/sd-controlnet-canny' , from_pt=SCREAMING_SNAKE_CASE__ , dtype=jnp.bfloataa ) lowerCamelCase__ , lowerCamelCase__ = FlaxStableDiffusionControlNetPipeline.from_pretrained( 'runwayml/stable-diffusion-v1-5' , controlnet=SCREAMING_SNAKE_CASE__ , from_pt=SCREAMING_SNAKE_CASE__ , dtype=jnp.bfloataa ) lowerCamelCase__ = controlnet_params lowerCamelCase__ = 'bird' lowerCamelCase__ = jax.device_count() lowerCamelCase__ = pipe.prepare_text_inputs([prompts] * num_samples ) lowerCamelCase__ = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/bird_canny.png' ) lowerCamelCase__ = pipe.prepare_image_inputs([canny_image] * num_samples ) lowerCamelCase__ = jax.random.PRNGKey(0 ) lowerCamelCase__ = jax.random.split(SCREAMING_SNAKE_CASE__ , jax.device_count() ) lowerCamelCase__ = replicate(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = shard(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = shard(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = pipe( prompt_ids=SCREAMING_SNAKE_CASE__ , image=SCREAMING_SNAKE_CASE__ , params=SCREAMING_SNAKE_CASE__ , prng_seed=SCREAMING_SNAKE_CASE__ , num_inference_steps=50 , jit=SCREAMING_SNAKE_CASE__ , ).images assert images.shape == (jax.device_count(), 1, 7_68, 5_12, 3) lowerCamelCase__ = images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:] ) lowerCamelCase__ = images[0, 2_53:2_56, 2_53:2_56, -1] lowerCamelCase__ = jnp.asarray(jax.device_get(image_slice.flatten() ) ) lowerCamelCase__ = jnp.array( [0.16_79_69, 0.11_66_99, 0.08_15_43, 0.15_42_97, 0.13_28_12, 0.10_88_87, 0.16_99_22, 0.16_99_22, 0.20_50_78] ) print(F'output_slice: {output_slice}' ) assert jnp.abs(output_slice - expected_slice ).max() < 1e-2 def _UpperCamelCase ( self : str ): lowerCamelCase__ , lowerCamelCase__ = FlaxControlNetModel.from_pretrained( 'lllyasviel/sd-controlnet-openpose' , from_pt=SCREAMING_SNAKE_CASE__ , dtype=jnp.bfloataa ) lowerCamelCase__ , lowerCamelCase__ = FlaxStableDiffusionControlNetPipeline.from_pretrained( 'runwayml/stable-diffusion-v1-5' , controlnet=SCREAMING_SNAKE_CASE__ , from_pt=SCREAMING_SNAKE_CASE__ , dtype=jnp.bfloataa ) lowerCamelCase__ = controlnet_params lowerCamelCase__ = 'Chef in the kitchen' lowerCamelCase__ = jax.device_count() lowerCamelCase__ = pipe.prepare_text_inputs([prompts] * num_samples ) lowerCamelCase__ = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/pose.png' ) lowerCamelCase__ = pipe.prepare_image_inputs([pose_image] * num_samples ) lowerCamelCase__ = jax.random.PRNGKey(0 ) lowerCamelCase__ = jax.random.split(SCREAMING_SNAKE_CASE__ , jax.device_count() ) lowerCamelCase__ = replicate(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = shard(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = shard(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = pipe( prompt_ids=SCREAMING_SNAKE_CASE__ , image=SCREAMING_SNAKE_CASE__ , params=SCREAMING_SNAKE_CASE__ , prng_seed=SCREAMING_SNAKE_CASE__ , num_inference_steps=50 , jit=SCREAMING_SNAKE_CASE__ , ).images assert images.shape == (jax.device_count(), 1, 7_68, 5_12, 3) lowerCamelCase__ = images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:] ) lowerCamelCase__ = images[0, 2_53:2_56, 2_53:2_56, -1] lowerCamelCase__ = jnp.asarray(jax.device_get(image_slice.flatten() ) ) lowerCamelCase__ = jnp.array( [[0.27_14_84, 0.26_17_19, 0.27_53_91, 0.27_73_44, 0.27_92_97, 0.29_10_16, 0.29_49_22, 0.30_27_34, 0.30_27_34]] ) print(F'output_slice: {output_slice}' ) assert jnp.abs(output_slice - expected_slice ).max() < 1e-2
659
"""simple docstring""" import json import multiprocessing import os import re from collections import defaultdict import torch from accelerate import Accelerator from accelerate.utils import set_seed from arguments import HumanEvalArguments from datasets import load_dataset, load_metric from torch.utils.data import IterableDataset from torch.utils.data.dataloader import DataLoader from tqdm import tqdm import transformers from transformers import AutoModelForCausalLM, AutoTokenizer, HfArgumentParser, StoppingCriteria, StoppingCriteriaList _snake_case = ["\nclass", "\ndef", "\n#", "\n@", "\nprint", "\nif"] class _a ( SCREAMING_SNAKE_CASE_ ): def __init__( self : List[str] , SCREAMING_SNAKE_CASE__ : Any , SCREAMING_SNAKE_CASE__ : List[Any] , SCREAMING_SNAKE_CASE__ : int=None , SCREAMING_SNAKE_CASE__ : Optional[int]=1 ): lowerCamelCase__ = tokenizer lowerCamelCase__ = dataset lowerCamelCase__ = len(SCREAMING_SNAKE_CASE__ ) if n_tasks is None else n_tasks lowerCamelCase__ = n_copies def __iter__( self : Any ): lowerCamelCase__ = [] for task in range(self.n_tasks ): # without strip, the model generate commented codes ... prompts.append(self.tokenizer.eos_token + self.dataset[task]['prompt'].strip() ) lowerCamelCase__ = self.tokenizer(SCREAMING_SNAKE_CASE__ , padding=SCREAMING_SNAKE_CASE__ , return_tensors='pt' ) for task in range(self.n_tasks ): for _ in range(self.n_copies ): yield { "ids": outputs.input_ids[task], "task_id": task, "input_len": outputs.attention_mask[task].sum(), } class _a ( SCREAMING_SNAKE_CASE_ ): def __init__( self : Dict , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : List[Any] , SCREAMING_SNAKE_CASE__ : Dict ): lowerCamelCase__ = start_length lowerCamelCase__ = eof_strings lowerCamelCase__ = tokenizer def __call__( self : Any , SCREAMING_SNAKE_CASE__ : Any , SCREAMING_SNAKE_CASE__ : List[str] , **SCREAMING_SNAKE_CASE__ : Dict ): lowerCamelCase__ = self.tokenizer.batch_decode(input_ids[:, self.start_length :] ) lowerCamelCase__ = [] for decoded_generation in decoded_generations: done.append(any(stop_string in decoded_generation for stop_string in self.eof_strings ) ) return all(SCREAMING_SNAKE_CASE__ ) def snake_case ( _a: List[Any] )-> Dict: '''simple docstring''' lowerCamelCase__ = re.split('(%s)' % '|'.join(_a ) , _a ) # last string should be "" return "".join(string_list[:-2] ) def snake_case ( _a: List[Any] , _a: Optional[int] , _a: str , _a: Union[str, Any] , _a: Dict , _a: Optional[int]=20 , **_a: Optional[int] )-> List[str]: '''simple docstring''' lowerCamelCase__ = defaultdict(_a ) # dict of list of generated tokens for step, batch in tqdm(enumerate(_a ) ): with torch.no_grad(): lowerCamelCase__ = batch['ids'].shape[-1] lowerCamelCase__ = accelerator.unwrap_model(_a ).generate( input_ids=batch['ids'][:, : batch['input_len']] , num_return_sequences=_a , **_a ) # each task is generated batch_size times lowerCamelCase__ = batch['task_id'].repeat(_a ) lowerCamelCase__ = accelerator.pad_across_processes( _a , dim=1 , pad_index=tokenizer.pad_token_id ) lowerCamelCase__ , lowerCamelCase__ = accelerator.gather((generated_tokens, generated_tasks) ) lowerCamelCase__ = generated_tokens.cpu().numpy() lowerCamelCase__ = generated_tasks.cpu().numpy() for task, generated_tokens in zip(_a , _a ): gen_token_dict[task].append(_a ) lowerCamelCase__ = [[] for _ in range(_a )] for task, generated_tokens in gen_token_dict.items(): for s in generated_tokens: lowerCamelCase__ = tokenizer.decode(_a , skip_special_tokens=_a , clean_up_tokenization_spaces=_a ) code_gens[task].append(remove_last_block(_a ) ) return code_gens def snake_case ( )-> Union[str, Any]: '''simple docstring''' lowerCamelCase__ = HfArgumentParser(_a ) lowerCamelCase__ = parser.parse_args() transformers.logging.set_verbosity_error() # enables code execution in code_eval metric lowerCamelCase__ = args.HF_ALLOW_CODE_EVAL # make sure tokenizer plays nice with multiprocessing lowerCamelCase__ = 'false' if args.num_workers is None: lowerCamelCase__ = multiprocessing.cpu_count() # Use dataset load to feed to accelerate lowerCamelCase__ = Accelerator() set_seed(args.seed , device_specific=_a ) # Load model and tokenizer lowerCamelCase__ = AutoTokenizer.from_pretrained(args.model_ckpt ) lowerCamelCase__ = tokenizer.eos_token lowerCamelCase__ = AutoModelForCausalLM.from_pretrained(args.model_ckpt ) # Generation settings lowerCamelCase__ = { 'do_sample': args.do_sample, 'temperature': args.temperature, 'max_new_tokens': args.max_new_tokens, 'top_p': args.top_p, 'top_k': args.top_k, 'stopping_criteria': StoppingCriteriaList([EndOfFunctionCriteria(0 , _a , _a )] ), } # Load evaluation dataset and metric lowerCamelCase__ = load_dataset('openai_humaneval' ) lowerCamelCase__ = load_metric('code_eval' ) lowerCamelCase__ = args.num_tasks if args.num_tasks is not None else len(human_eval['test'] ) lowerCamelCase__ = args.n_samples // args.batch_size lowerCamelCase__ = TokenizedDataset(_a , human_eval['test'] , n_copies=_a , n_tasks=_a ) # do not confuse args.batch_size, which is actually the num_return_sequences lowerCamelCase__ = DataLoader(_a , batch_size=1 ) # Run a quick test to see if code evaluation is enabled try: lowerCamelCase__ = code_eval_metric.compute(references=[''] , predictions=[['']] ) except ValueError as exception: print( 'Code evaluation not enabled. Read the warning below carefully and then use `--HF_ALLOW_CODE_EVAL="1"`' ' flag to enable code evaluation.' ) raise exception lowerCamelCase__ , lowerCamelCase__ = accelerator.prepare(_a , _a ) lowerCamelCase__ = complete_code( _a , _a , _a , _a , n_tasks=_a , batch_size=args.batch_size , **_a , ) if accelerator.is_main_process: lowerCamelCase__ = [] for task in tqdm(range(_a ) ): lowerCamelCase__ = human_eval['test'][task]['test'] lowerCamelCase__ = F'check({human_eval["test"][task]["entry_point"]})' references.append('\n' + test_func + '\n' + entry_point ) # Evaluate completions with "code_eval" metric lowerCamelCase__ , lowerCamelCase__ = code_eval_metric.compute( references=_a , predictions=_a , num_workers=args.num_workers ) print(F'Results: {pass_at_k}' ) # Save results to json file with open(args.output_file , 'w' ) as fp: json.dump(_a , _a ) # For some reason the folliwng seems to be necessary sometimes for code_eval to work nice with multiprocessing # https://stackoverflow.com/questions/60804599/python-multiprocessing-keeps-spawning-the-whole-script if __name__ == "__main__": main()
659
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_torch_available _snake_case = { "configuration_gpt_neo": ["GPT_NEO_PRETRAINED_CONFIG_ARCHIVE_MAP", "GPTNeoConfig", "GPTNeoOnnxConfig"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = [ "GPT_NEO_PRETRAINED_MODEL_ARCHIVE_LIST", "GPTNeoForCausalLM", "GPTNeoForQuestionAnswering", "GPTNeoForSequenceClassification", "GPTNeoForTokenClassification", "GPTNeoModel", "GPTNeoPreTrainedModel", "load_tf_weights_in_gpt_neo", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = [ "FlaxGPTNeoForCausalLM", "FlaxGPTNeoModel", "FlaxGPTNeoPreTrainedModel", ] if TYPE_CHECKING: from .configuration_gpt_neo import GPT_NEO_PRETRAINED_CONFIG_ARCHIVE_MAP, GPTNeoConfig, GPTNeoOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_gpt_neo import ( GPT_NEO_PRETRAINED_MODEL_ARCHIVE_LIST, GPTNeoForCausalLM, GPTNeoForQuestionAnswering, GPTNeoForSequenceClassification, GPTNeoForTokenClassification, GPTNeoModel, GPTNeoPreTrainedModel, load_tf_weights_in_gpt_neo, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_gpt_neo import FlaxGPTNeoForCausalLM, FlaxGPTNeoModel, FlaxGPTNeoPreTrainedModel else: import sys _snake_case = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
659
"""simple docstring""" import argparse import json from tqdm import tqdm def snake_case ( )-> List[Any]: '''simple docstring''' lowerCamelCase__ = argparse.ArgumentParser() # Required parameters parser.add_argument( '--src_path' , type=_a , default='biencoder-nq-dev.json' , help='Path to raw DPR training data' , ) parser.add_argument( '--evaluation_set' , type=_a , help='where to store parsed evaluation_set file' , ) parser.add_argument( '--gold_data_path' , type=_a , help='where to store parsed gold_data_path file' , ) lowerCamelCase__ = parser.parse_args() with open(args.src_path , 'r' ) as src_file, open(args.evaluation_set , 'w' ) as eval_file, open( args.gold_data_path , 'w' ) as gold_file: lowerCamelCase__ = json.load(_a ) for dpr_record in tqdm(_a ): lowerCamelCase__ = dpr_record['question'] lowerCamelCase__ = [context['title'] for context in dpr_record['positive_ctxs']] eval_file.write(question + '\n' ) gold_file.write('\t'.join(_a ) + '\n' ) if __name__ == "__main__": main()
659
1
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging _snake_case = logging.get_logger(__name__) _snake_case = { "bigcode/gpt_bigcode-santacoder": "https://huggingface.co/bigcode/gpt_bigcode-santacoder/resolve/main/config.json", } class _a ( SCREAMING_SNAKE_CASE_ ): a_ : Optional[int] = 'gpt_bigcode' a_ : str = ['past_key_values'] a_ : Union[str, Any] = { 'hidden_size': 'n_embd', 'max_position_embeddings': 'n_positions', 'num_attention_heads': 'n_head', 'num_hidden_layers': 'n_layer', } def __init__( self : Tuple , SCREAMING_SNAKE_CASE__ : Union[str, Any]=5_02_57 , SCREAMING_SNAKE_CASE__ : List[Any]=10_24 , SCREAMING_SNAKE_CASE__ : int=7_68 , SCREAMING_SNAKE_CASE__ : Optional[Any]=12 , SCREAMING_SNAKE_CASE__ : int=12 , SCREAMING_SNAKE_CASE__ : List[str]=None , SCREAMING_SNAKE_CASE__ : Tuple="gelu_pytorch_tanh" , SCREAMING_SNAKE_CASE__ : Optional[int]=0.1 , SCREAMING_SNAKE_CASE__ : Tuple=0.1 , SCREAMING_SNAKE_CASE__ : List[str]=0.1 , SCREAMING_SNAKE_CASE__ : Optional[Any]=1e-5 , SCREAMING_SNAKE_CASE__ : Optional[Any]=0.02 , SCREAMING_SNAKE_CASE__ : Tuple=True , SCREAMING_SNAKE_CASE__ : Dict=True , SCREAMING_SNAKE_CASE__ : List[Any]=5_02_56 , SCREAMING_SNAKE_CASE__ : Optional[Any]=5_02_56 , SCREAMING_SNAKE_CASE__ : Union[str, Any]=True , SCREAMING_SNAKE_CASE__ : Any=True , SCREAMING_SNAKE_CASE__ : List[Any]=True , **SCREAMING_SNAKE_CASE__ : Dict , ): lowerCamelCase__ = vocab_size lowerCamelCase__ = n_positions lowerCamelCase__ = n_embd lowerCamelCase__ = n_layer lowerCamelCase__ = n_head lowerCamelCase__ = n_inner lowerCamelCase__ = activation_function lowerCamelCase__ = resid_pdrop lowerCamelCase__ = embd_pdrop lowerCamelCase__ = attn_pdrop lowerCamelCase__ = layer_norm_epsilon lowerCamelCase__ = initializer_range lowerCamelCase__ = scale_attn_weights lowerCamelCase__ = use_cache lowerCamelCase__ = attention_softmax_in_fpaa lowerCamelCase__ = scale_attention_softmax_in_fpaa lowerCamelCase__ = multi_query lowerCamelCase__ = bos_token_id lowerCamelCase__ = eos_token_id super().__init__(bos_token_id=SCREAMING_SNAKE_CASE__ , eos_token_id=SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ )
659
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) _snake_case = { "configuration_blenderbot": [ "BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP", "BlenderbotConfig", "BlenderbotOnnxConfig", ], "tokenization_blenderbot": ["BlenderbotTokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = ["BlenderbotTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = [ "BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST", "BlenderbotForCausalLM", "BlenderbotForConditionalGeneration", "BlenderbotModel", "BlenderbotPreTrainedModel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = [ "TFBlenderbotForConditionalGeneration", "TFBlenderbotModel", "TFBlenderbotPreTrainedModel", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = [ "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 _snake_case = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
659
1
"""simple docstring""" from typing import List, Optional, Union from ...image_utils import ImageInput from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy from ...utils import TensorType class _a ( SCREAMING_SNAKE_CASE_ ): a_ : Any = ['image_processor', 'tokenizer'] a_ : List[str] = 'BlipImageProcessor' a_ : int = 'AutoTokenizer' def __init__( self : Optional[Any] , SCREAMING_SNAKE_CASE__ : List[Any] , SCREAMING_SNAKE_CASE__ : List[str] ): lowerCamelCase__ = False super().__init__(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = self.image_processor def __call__( self : Tuple , SCREAMING_SNAKE_CASE__ : ImageInput = None , SCREAMING_SNAKE_CASE__ : Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None , SCREAMING_SNAKE_CASE__ : bool = True , SCREAMING_SNAKE_CASE__ : Union[bool, str, PaddingStrategy] = False , SCREAMING_SNAKE_CASE__ : Union[bool, str, TruncationStrategy] = None , SCREAMING_SNAKE_CASE__ : Optional[int] = None , SCREAMING_SNAKE_CASE__ : int = 0 , SCREAMING_SNAKE_CASE__ : Optional[int] = None , SCREAMING_SNAKE_CASE__ : Optional[bool] = None , SCREAMING_SNAKE_CASE__ : bool = False , SCREAMING_SNAKE_CASE__ : bool = False , SCREAMING_SNAKE_CASE__ : bool = False , SCREAMING_SNAKE_CASE__ : bool = False , SCREAMING_SNAKE_CASE__ : bool = False , SCREAMING_SNAKE_CASE__ : bool = True , SCREAMING_SNAKE_CASE__ : Optional[Union[str, TensorType]] = None , **SCREAMING_SNAKE_CASE__ : List[Any] , ): if images is None and text is None: raise ValueError('You have to specify either images or text.' ) # Get only text if images is None: lowerCamelCase__ = self.tokenizer lowerCamelCase__ = self.tokenizer( text=SCREAMING_SNAKE_CASE__ , add_special_tokens=SCREAMING_SNAKE_CASE__ , padding=SCREAMING_SNAKE_CASE__ , truncation=SCREAMING_SNAKE_CASE__ , max_length=SCREAMING_SNAKE_CASE__ , stride=SCREAMING_SNAKE_CASE__ , pad_to_multiple_of=SCREAMING_SNAKE_CASE__ , return_attention_mask=SCREAMING_SNAKE_CASE__ , return_overflowing_tokens=SCREAMING_SNAKE_CASE__ , return_special_tokens_mask=SCREAMING_SNAKE_CASE__ , return_offsets_mapping=SCREAMING_SNAKE_CASE__ , return_token_type_ids=SCREAMING_SNAKE_CASE__ , return_length=SCREAMING_SNAKE_CASE__ , verbose=SCREAMING_SNAKE_CASE__ , return_tensors=SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ , ) return text_encoding # add pixel_values lowerCamelCase__ = self.image_processor(SCREAMING_SNAKE_CASE__ , return_tensors=SCREAMING_SNAKE_CASE__ ) if text is not None: lowerCamelCase__ = self.tokenizer( text=SCREAMING_SNAKE_CASE__ , add_special_tokens=SCREAMING_SNAKE_CASE__ , padding=SCREAMING_SNAKE_CASE__ , truncation=SCREAMING_SNAKE_CASE__ , max_length=SCREAMING_SNAKE_CASE__ , stride=SCREAMING_SNAKE_CASE__ , pad_to_multiple_of=SCREAMING_SNAKE_CASE__ , return_attention_mask=SCREAMING_SNAKE_CASE__ , return_overflowing_tokens=SCREAMING_SNAKE_CASE__ , return_special_tokens_mask=SCREAMING_SNAKE_CASE__ , return_offsets_mapping=SCREAMING_SNAKE_CASE__ , return_token_type_ids=SCREAMING_SNAKE_CASE__ , return_length=SCREAMING_SNAKE_CASE__ , verbose=SCREAMING_SNAKE_CASE__ , return_tensors=SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ , ) else: lowerCamelCase__ = None if text_encoding is not None: encoding_image_processor.update(SCREAMING_SNAKE_CASE__ ) return encoding_image_processor def _UpperCamelCase ( self : List[str] , *SCREAMING_SNAKE_CASE__ : List[str] , **SCREAMING_SNAKE_CASE__ : str ): return self.tokenizer.batch_decode(*SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : str , *SCREAMING_SNAKE_CASE__ : Dict , **SCREAMING_SNAKE_CASE__ : Optional[int] ): return self.tokenizer.decode(*SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) @property # Copied from transformers.models.blip.processing_blip.BlipProcessor.model_input_names def _UpperCamelCase ( self : Tuple ): lowerCamelCase__ = self.tokenizer.model_input_names lowerCamelCase__ = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) )
659
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging _snake_case = logging.get_logger(__name__) _snake_case = { "microsoft/swinv2-tiny-patch4-window8-256": ( "https://huggingface.co/microsoft/swinv2-tiny-patch4-window8-256/resolve/main/config.json" ), } class _a ( SCREAMING_SNAKE_CASE_ ): a_ : Union[str, Any] = 'swinv2' a_ : Optional[int] = { 'num_attention_heads': 'num_heads', 'num_hidden_layers': 'num_layers', } def __init__( self : List[str] , SCREAMING_SNAKE_CASE__ : int=2_24 , SCREAMING_SNAKE_CASE__ : str=4 , SCREAMING_SNAKE_CASE__ : List[Any]=3 , SCREAMING_SNAKE_CASE__ : str=96 , SCREAMING_SNAKE_CASE__ : Dict=[2, 2, 6, 2] , SCREAMING_SNAKE_CASE__ : Union[str, Any]=[3, 6, 12, 24] , SCREAMING_SNAKE_CASE__ : Tuple=7 , SCREAMING_SNAKE_CASE__ : Union[str, Any]=4.0 , SCREAMING_SNAKE_CASE__ : List[str]=True , SCREAMING_SNAKE_CASE__ : List[Any]=0.0 , SCREAMING_SNAKE_CASE__ : Optional[int]=0.0 , SCREAMING_SNAKE_CASE__ : Dict=0.1 , SCREAMING_SNAKE_CASE__ : Tuple="gelu" , SCREAMING_SNAKE_CASE__ : Tuple=False , SCREAMING_SNAKE_CASE__ : Any=0.02 , SCREAMING_SNAKE_CASE__ : List[Any]=1e-5 , SCREAMING_SNAKE_CASE__ : int=32 , **SCREAMING_SNAKE_CASE__ : List[str] , ): super().__init__(**SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = image_size lowerCamelCase__ = patch_size lowerCamelCase__ = num_channels lowerCamelCase__ = embed_dim lowerCamelCase__ = depths lowerCamelCase__ = len(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = num_heads lowerCamelCase__ = window_size lowerCamelCase__ = mlp_ratio lowerCamelCase__ = qkv_bias lowerCamelCase__ = hidden_dropout_prob lowerCamelCase__ = attention_probs_dropout_prob lowerCamelCase__ = drop_path_rate lowerCamelCase__ = hidden_act lowerCamelCase__ = use_absolute_embeddings lowerCamelCase__ = layer_norm_eps lowerCamelCase__ = initializer_range lowerCamelCase__ = encoder_stride # we set the hidden_size attribute in order to make Swinv2 work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model lowerCamelCase__ = int(embed_dim * 2 ** (len(SCREAMING_SNAKE_CASE__ ) - 1) ) lowerCamelCase__ = (0, 0, 0, 0)
659
1
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging _snake_case = logging.get_logger(__name__) _snake_case = { "uclanlp/visualbert-vqa": "https://huggingface.co/uclanlp/visualbert-vqa/resolve/main/config.json", "uclanlp/visualbert-vqa-pre": "https://huggingface.co/uclanlp/visualbert-vqa-pre/resolve/main/config.json", "uclanlp/visualbert-vqa-coco-pre": ( "https://huggingface.co/uclanlp/visualbert-vqa-coco-pre/resolve/main/config.json" ), "uclanlp/visualbert-vcr": "https://huggingface.co/uclanlp/visualbert-vcr/resolve/main/config.json", "uclanlp/visualbert-vcr-pre": "https://huggingface.co/uclanlp/visualbert-vcr-pre/resolve/main/config.json", "uclanlp/visualbert-vcr-coco-pre": ( "https://huggingface.co/uclanlp/visualbert-vcr-coco-pre/resolve/main/config.json" ), "uclanlp/visualbert-nlvr2": "https://huggingface.co/uclanlp/visualbert-nlvr2/resolve/main/config.json", "uclanlp/visualbert-nlvr2-pre": "https://huggingface.co/uclanlp/visualbert-nlvr2-pre/resolve/main/config.json", "uclanlp/visualbert-nlvr2-coco-pre": ( "https://huggingface.co/uclanlp/visualbert-nlvr2-coco-pre/resolve/main/config.json" ) # See all VisualBERT models at https://huggingface.co/models?filter=visual_bert } class _a ( SCREAMING_SNAKE_CASE_ ): a_ : str = 'visual_bert' def __init__( self : Tuple , SCREAMING_SNAKE_CASE__ : Optional[Any]=3_05_22 , SCREAMING_SNAKE_CASE__ : Union[str, Any]=7_68 , SCREAMING_SNAKE_CASE__ : Any=5_12 , SCREAMING_SNAKE_CASE__ : Any=12 , SCREAMING_SNAKE_CASE__ : Optional[Any]=12 , SCREAMING_SNAKE_CASE__ : Union[str, Any]=30_72 , SCREAMING_SNAKE_CASE__ : str="gelu" , SCREAMING_SNAKE_CASE__ : Optional[int]=0.1 , SCREAMING_SNAKE_CASE__ : Union[str, Any]=0.1 , SCREAMING_SNAKE_CASE__ : Any=5_12 , SCREAMING_SNAKE_CASE__ : Tuple=2 , SCREAMING_SNAKE_CASE__ : List[Any]=0.02 , SCREAMING_SNAKE_CASE__ : Any=1e-12 , SCREAMING_SNAKE_CASE__ : int=False , SCREAMING_SNAKE_CASE__ : Any=True , SCREAMING_SNAKE_CASE__ : Union[str, Any]=1 , SCREAMING_SNAKE_CASE__ : str=0 , SCREAMING_SNAKE_CASE__ : List[str]=2 , **SCREAMING_SNAKE_CASE__ : int , ): super().__init__(pad_token_id=SCREAMING_SNAKE_CASE__ , bos_token_id=SCREAMING_SNAKE_CASE__ , eos_token_id=SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = vocab_size lowerCamelCase__ = max_position_embeddings lowerCamelCase__ = hidden_size lowerCamelCase__ = visual_embedding_dim lowerCamelCase__ = num_hidden_layers lowerCamelCase__ = num_attention_heads lowerCamelCase__ = intermediate_size lowerCamelCase__ = hidden_act lowerCamelCase__ = hidden_dropout_prob lowerCamelCase__ = attention_probs_dropout_prob lowerCamelCase__ = initializer_range lowerCamelCase__ = type_vocab_size lowerCamelCase__ = layer_norm_eps lowerCamelCase__ = bypass_transformer lowerCamelCase__ = special_visual_initialize
659
"""simple docstring""" def snake_case ( _a: int )-> int: '''simple docstring''' lowerCamelCase__ = 1 for i in range(1 , num + 1 ): fact *= i return fact def snake_case ( _a: int )-> int: '''simple docstring''' lowerCamelCase__ = 0 while number > 0: lowerCamelCase__ = number % 10 sum_of_digits += last_digit lowerCamelCase__ = number // 10 # Removing the last_digit from the given number return sum_of_digits def snake_case ( _a: int = 100 )-> int: '''simple docstring''' lowerCamelCase__ = factorial(_a ) lowerCamelCase__ = split_and_add(_a ) return result if __name__ == "__main__": print(solution(int(input("Enter the Number: ").strip())))
659
1
"""simple docstring""" import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import XLMRobertaTokenizerFast from diffusers import DDIMScheduler, KandinskyInpaintPipeline, KandinskyPriorPipeline, UNetaDConditionModel, VQModel from diffusers.pipelines.kandinsky.text_encoder import MCLIPConfig, MultilingualCLIP from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference enable_full_determinism() class _a ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): a_ : Any = KandinskyInpaintPipeline a_ : List[str] = ['prompt', 'image_embeds', 'negative_image_embeds', 'image', 'mask_image'] a_ : Dict = [ 'prompt', 'negative_prompt', 'image_embeds', 'negative_image_embeds', 'image', 'mask_image', ] a_ : Optional[int] = [ 'generator', 'height', 'width', 'latents', 'guidance_scale', 'negative_prompt', 'num_inference_steps', 'return_dict', 'guidance_scale', 'num_images_per_prompt', 'output_type', 'return_dict', ] a_ : Tuple = False @property def _UpperCamelCase ( self : Optional[int] ): return 32 @property def _UpperCamelCase ( self : Dict ): return 32 @property def _UpperCamelCase ( self : int ): return self.time_input_dim @property def _UpperCamelCase ( self : str ): return self.time_input_dim * 4 @property def _UpperCamelCase ( self : Dict ): return 1_00 @property def _UpperCamelCase ( self : Tuple ): lowerCamelCase__ = XLMRobertaTokenizerFast.from_pretrained('YiYiXu/tiny-random-mclip-base' ) return tokenizer @property def _UpperCamelCase ( self : List[Any] ): torch.manual_seed(0 ) lowerCamelCase__ = MCLIPConfig( numDims=self.cross_attention_dim , transformerDimensions=self.text_embedder_hidden_size , hidden_size=self.text_embedder_hidden_size , intermediate_size=37 , num_attention_heads=4 , num_hidden_layers=5 , vocab_size=10_05 , ) lowerCamelCase__ = MultilingualCLIP(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = text_encoder.eval() return text_encoder @property def _UpperCamelCase ( self : Any ): torch.manual_seed(0 ) lowerCamelCase__ = { 'in_channels': 9, # Out channels is double in channels because predicts mean and variance 'out_channels': 8, 'addition_embed_type': 'text_image', 'down_block_types': ('ResnetDownsampleBlock2D', 'SimpleCrossAttnDownBlock2D'), 'up_block_types': ('SimpleCrossAttnUpBlock2D', 'ResnetUpsampleBlock2D'), 'mid_block_type': 'UNetMidBlock2DSimpleCrossAttn', 'block_out_channels': (self.block_out_channels_a, self.block_out_channels_a * 2), 'layers_per_block': 1, 'encoder_hid_dim': self.text_embedder_hidden_size, 'encoder_hid_dim_type': 'text_image_proj', 'cross_attention_dim': self.cross_attention_dim, 'attention_head_dim': 4, 'resnet_time_scale_shift': 'scale_shift', 'class_embed_type': None, } lowerCamelCase__ = UNetaDConditionModel(**SCREAMING_SNAKE_CASE__ ) return model @property def _UpperCamelCase ( self : Dict ): return { "block_out_channels": [32, 64], "down_block_types": ["DownEncoderBlock2D", "AttnDownEncoderBlock2D"], "in_channels": 3, "latent_channels": 4, "layers_per_block": 1, "norm_num_groups": 8, "norm_type": "spatial", "num_vq_embeddings": 12, "out_channels": 3, "up_block_types": [ "AttnUpDecoderBlock2D", "UpDecoderBlock2D", ], "vq_embed_dim": 4, } @property def _UpperCamelCase ( self : Tuple ): torch.manual_seed(0 ) lowerCamelCase__ = VQModel(**self.dummy_movq_kwargs ) return model def _UpperCamelCase ( self : Union[str, Any] ): lowerCamelCase__ = self.dummy_text_encoder lowerCamelCase__ = self.dummy_tokenizer lowerCamelCase__ = self.dummy_unet lowerCamelCase__ = self.dummy_movq lowerCamelCase__ = DDIMScheduler( num_train_timesteps=10_00 , beta_schedule='linear' , beta_start=0.0_00_85 , beta_end=0.0_12 , clip_sample=SCREAMING_SNAKE_CASE__ , set_alpha_to_one=SCREAMING_SNAKE_CASE__ , steps_offset=1 , prediction_type='epsilon' , thresholding=SCREAMING_SNAKE_CASE__ , ) lowerCamelCase__ = { 'text_encoder': text_encoder, 'tokenizer': tokenizer, 'unet': unet, 'scheduler': scheduler, 'movq': movq, } return components def _UpperCamelCase ( self : int , SCREAMING_SNAKE_CASE__ : Dict , SCREAMING_SNAKE_CASE__ : Optional[int]=0 ): lowerCamelCase__ = floats_tensor((1, self.cross_attention_dim) , rng=random.Random(SCREAMING_SNAKE_CASE__ ) ).to(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = floats_tensor((1, self.cross_attention_dim) , rng=random.Random(seed + 1 ) ).to(SCREAMING_SNAKE_CASE__ ) # create init_image lowerCamelCase__ = floats_tensor((1, 3, 64, 64) , rng=random.Random(SCREAMING_SNAKE_CASE__ ) ).to(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = image.cpu().permute(0 , 2 , 3 , 1 )[0] lowerCamelCase__ = Image.fromarray(np.uinta(SCREAMING_SNAKE_CASE__ ) ).convert('RGB' ).resize((2_56, 2_56) ) # create mask lowerCamelCase__ = np.ones((64, 64) , dtype=np.floataa ) lowerCamelCase__ = 0 if str(SCREAMING_SNAKE_CASE__ ).startswith('mps' ): lowerCamelCase__ = torch.manual_seed(SCREAMING_SNAKE_CASE__ ) else: lowerCamelCase__ = torch.Generator(device=SCREAMING_SNAKE_CASE__ ).manual_seed(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = { 'prompt': 'horse', 'image': init_image, 'mask_image': mask, 'image_embeds': image_embeds, 'negative_image_embeds': negative_image_embeds, 'generator': generator, 'height': 64, 'width': 64, 'num_inference_steps': 2, 'guidance_scale': 4.0, 'output_type': 'np', } return inputs def _UpperCamelCase ( self : int ): lowerCamelCase__ = 'cpu' lowerCamelCase__ = self.get_dummy_components() lowerCamelCase__ = self.pipeline_class(**SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = pipe.to(SCREAMING_SNAKE_CASE__ ) pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = pipe(**self.get_dummy_inputs(SCREAMING_SNAKE_CASE__ ) ) lowerCamelCase__ = output.images lowerCamelCase__ = pipe( **self.get_dummy_inputs(SCREAMING_SNAKE_CASE__ ) , return_dict=SCREAMING_SNAKE_CASE__ , )[0] lowerCamelCase__ = image[0, -3:, -3:, -1] lowerCamelCase__ = image_from_tuple[0, -3:, -3:, -1] print(F'image.shape {image.shape}' ) assert image.shape == (1, 64, 64, 3) lowerCamelCase__ = np.array( [0.8_32_69_19, 0.73_79_04_67, 0.20_91_85_81, 0.9_30_96_12, 0.5_51_17_91, 0.43_71_33_28, 0.5_51_33_21, 0.49_92_29_34, 0.59_49_77_86] ) assert ( np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 ), F' expected_slice {expected_slice}, but got {image_slice.flatten()}' assert ( np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 ), F' expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}' def _UpperCamelCase ( self : Dict ): super().test_inference_batch_single_identical(expected_max_diff=3e-3 ) @slow @require_torch_gpu class _a ( unittest.TestCase ): def _UpperCamelCase ( self : str ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def _UpperCamelCase ( self : Optional[int] ): lowerCamelCase__ = load_numpy( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/kandinsky/kandinsky_inpaint_cat_with_hat_fp16.npy' ) lowerCamelCase__ = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/kandinsky/cat.png' ) lowerCamelCase__ = np.ones((7_68, 7_68) , dtype=np.floataa ) lowerCamelCase__ = 0 lowerCamelCase__ = 'a hat' lowerCamelCase__ = KandinskyPriorPipeline.from_pretrained( 'kandinsky-community/kandinsky-2-1-prior' , torch_dtype=torch.floataa ) pipe_prior.to(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = KandinskyInpaintPipeline.from_pretrained( 'kandinsky-community/kandinsky-2-1-inpaint' , torch_dtype=torch.floataa ) lowerCamelCase__ = pipeline.to(SCREAMING_SNAKE_CASE__ ) pipeline.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = torch.Generator(device='cpu' ).manual_seed(0 ) lowerCamelCase__ , lowerCamelCase__ = pipe_prior( SCREAMING_SNAKE_CASE__ , generator=SCREAMING_SNAKE_CASE__ , num_inference_steps=5 , negative_prompt='' , ).to_tuple() lowerCamelCase__ = pipeline( SCREAMING_SNAKE_CASE__ , image=SCREAMING_SNAKE_CASE__ , mask_image=SCREAMING_SNAKE_CASE__ , image_embeds=SCREAMING_SNAKE_CASE__ , negative_image_embeds=SCREAMING_SNAKE_CASE__ , generator=SCREAMING_SNAKE_CASE__ , num_inference_steps=1_00 , height=7_68 , width=7_68 , output_type='np' , ) lowerCamelCase__ = output.images[0] assert image.shape == (7_68, 7_68, 3) assert_mean_pixel_difference(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ )
659
"""simple docstring""" 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 _snake_case = re.compile("[^A-Za-z_0-9]") # parameters used in DuplicationIndex _snake_case = 10 _snake_case = 256 def snake_case ( _a: List[str] )-> Optional[MinHash]: '''simple docstring''' if len(_a ) < MIN_NUM_TOKENS: return None lowerCamelCase__ = MinHash(num_perm=_a ) for token in set(_a ): min_hash.update(token.encode() ) return min_hash def snake_case ( _a: str )-> Set[str]: '''simple docstring''' return {t for t in NON_ALPHA.split(_a ) if len(t.strip() ) > 0} class _a : def __init__( self : List[Any] , *, SCREAMING_SNAKE_CASE__ : float = 0.85 , ): lowerCamelCase__ = duplication_jaccard_threshold lowerCamelCase__ = NUM_PERM lowerCamelCase__ = MinHashLSH(threshold=self._duplication_jaccard_threshold , num_perm=self._num_perm ) lowerCamelCase__ = defaultdict(SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : List[Any] , SCREAMING_SNAKE_CASE__ : Tuple , SCREAMING_SNAKE_CASE__ : MinHash ): lowerCamelCase__ = self._index.query(SCREAMING_SNAKE_CASE__ ) if code_key in self._index.keys: print(F'Duplicate key {code_key}' ) return self._index.insert(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) if len(SCREAMING_SNAKE_CASE__ ) > 0: for base_duplicate in close_duplicates: if base_duplicate in self._duplicate_clusters: self._duplicate_clusters[base_duplicate].add(SCREAMING_SNAKE_CASE__ ) break else: self._duplicate_clusters[close_duplicates[0]].add(SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : str ): lowerCamelCase__ = [] for base, duplicates in self._duplicate_clusters.items(): lowerCamelCase__ = [base] + list(SCREAMING_SNAKE_CASE__ ) # reformat the cluster to be a list of dict lowerCamelCase__ = [{'base_index': el[0], 'repo_name': el[1], 'path': el[2]} for el in cluster] duplicate_clusters.append(SCREAMING_SNAKE_CASE__ ) return duplicate_clusters def _UpperCamelCase ( self : List[str] , SCREAMING_SNAKE_CASE__ : Union[str, Any] ): lowerCamelCase__ = self.get_duplicate_clusters() with open(SCREAMING_SNAKE_CASE__ , 'w' ) as f: json.dump(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) def snake_case ( _a: Union[str, Any] )-> Optional[int]: '''simple docstring''' lowerCamelCase__ , lowerCamelCase__ = element lowerCamelCase__ = 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 snake_case ( _a: Type[Dataset] )-> Tuple: '''simple docstring''' with mp.Pool() as pool: for data in pool.imap_unordered( _compute_min_hash , ThreadedIterator(_a , max_queue_size=10000 ) , chunksize=100 , ): if data is not None: yield data def snake_case ( _a: Type[Dataset] , _a: float )-> Optional[int]: '''simple docstring''' lowerCamelCase__ = 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 snake_case ( _a: str , _a: str )-> float: '''simple docstring''' lowerCamelCase__ = get_tokens(_a ) lowerCamelCase__ = get_tokens(_a ) return len(tokensa & tokensa ) / len(tokensa | tokensa ) _snake_case = None def snake_case ( _a: Dict , _a: Union[str, Any] )-> List[str]: '''simple docstring''' lowerCamelCase__ = [] for elementa in cluster: lowerCamelCase__ = _shared_dataset[elementa['base_index']]['content'] for elementa in extremes: lowerCamelCase__ = _shared_dataset[elementa['base_index']]['content'] if jaccard_similarity(_a , _a ) >= jaccard_threshold: elementa["copies"] += 1 break else: lowerCamelCase__ = 1 extremes.append(_a ) return extremes def snake_case ( _a: Any , _a: Tuple , _a: Dict )-> Union[str, Any]: '''simple docstring''' global _shared_dataset lowerCamelCase__ = dataset lowerCamelCase__ = [] lowerCamelCase__ = 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 snake_case ( _a: Type[Dataset] , _a: float = 0.85 )-> Tuple[Type[Dataset], List[List[Dict]]]: '''simple docstring''' lowerCamelCase__ = make_duplicate_clusters(_a , _a ) lowerCamelCase__ = {x['base_index'] for cluster in duplicate_clusters for x in cluster} lowerCamelCase__ = {} lowerCamelCase__ = find_extremes(_a , _a , _a ) for extremes in extremes_clusters: for element in extremes: lowerCamelCase__ = element lowerCamelCase__ = duplicate_indices - set(extreme_dict.keys() ) lowerCamelCase__ = 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: lowerCamelCase__ = element['base_index'] in extreme_dict if element["is_extreme"]: lowerCamelCase__ = 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
659
1
"""simple docstring""" import numpy as np import qiskit def snake_case ( _a: int = 8 , _a: int | None = None )-> str: '''simple docstring''' lowerCamelCase__ = np.random.default_rng(seed=_a ) # Roughly 25% of the qubits will contribute to the key. # So we take more than we need. lowerCamelCase__ = 6 * key_len # Measurement basis for Alice's qubits. lowerCamelCase__ = rng.integers(2 , size=_a ) # The set of states Alice will prepare. lowerCamelCase__ = rng.integers(2 , size=_a ) # Measurement basis for Bob's qubits. lowerCamelCase__ = rng.integers(2 , size=_a ) # Quantum Circuit to simulate BB84 lowerCamelCase__ = qiskit.QuantumCircuit(_a , name='BB84' ) # Alice prepares her qubits according to rules above. for index, _ in enumerate(_a ): if alice_state[index] == 1: bbaa_circ.x(_a ) if alice_basis[index] == 1: bbaa_circ.h(_a ) bbaa_circ.barrier() # Bob measures the received qubits according to rules above. for index, _ in enumerate(_a ): if bob_basis[index] == 1: bbaa_circ.h(_a ) bbaa_circ.barrier() bbaa_circ.measure_all() # Simulate the quantum circuit. lowerCamelCase__ = 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. lowerCamelCase__ = qiskit.execute(_a , _a , shots=1 , seed_simulator=_a ) # Returns the result of measurement. lowerCamelCase__ = job.result().get_counts(_a ).most_frequent() # Extracting the generated key from the simulation results. # Only keep measurement results where Alice and Bob chose the same basis. lowerCamelCase__ = ''.join( [ result_bit for alice_basis_bit, bob_basis_bit, result_bit in zip( _a , _a , _a ) if alice_basis_bit == bob_basis_bit ] ) # Get final key. Pad with 0 if too short, otherwise truncate. lowerCamelCase__ = gen_key[:key_len] if len(_a ) >= key_len else gen_key.ljust(_a , '0' ) return key if __name__ == "__main__": print(f"""The generated key is : {bbaa(8, seed=0)}""") from doctest import testmod testmod()
659
"""simple docstring""" import collections import json import math import os import re import time from fnmatch import fnmatch from typing import Dict import requests from slack_sdk import WebClient _snake_case = WebClient(token=os.environ["CI_SLACK_BOT_TOKEN"]) def snake_case ( _a: Any )-> Optional[Any]: '''simple docstring''' lowerCamelCase__ = test_results.split(' ' ) lowerCamelCase__ = 0 lowerCamelCase__ = 0 # When the output is short enough, the output is surrounded by = signs: "== OUTPUT ==" # When it is too long, those signs are not present. lowerCamelCase__ = expressions[-2] if '=' in expressions[-1] else expressions[-1] for i, expression in enumerate(_a ): if "failed" in expression: failed += int(expressions[i - 1] ) if "passed" in expression: success += int(expressions[i - 1] ) return failed, success, time_spent def snake_case ( _a: Optional[int] )-> Optional[int]: '''simple docstring''' lowerCamelCase__ = {} lowerCamelCase__ = None lowerCamelCase__ = False for line in failures_short_lines.split('\n' ): if re.search(R'_ \[doctest\]' , _a ): lowerCamelCase__ = True lowerCamelCase__ = line.split(' ' )[2] elif in_error and not line.split(' ' )[0].isdigit(): lowerCamelCase__ = line lowerCamelCase__ = False return failures class _a : def __init__( self : Optional[Any] , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : Dict ): lowerCamelCase__ = title lowerCamelCase__ = doc_test_results['time_spent'].split(',' )[0] lowerCamelCase__ = doc_test_results['success'] lowerCamelCase__ = doc_test_results['failures'] lowerCamelCase__ = self.n_success + self.n_failures # Failures and success of the modeling tests lowerCamelCase__ = doc_test_results @property def _UpperCamelCase ( self : List[str] ): lowerCamelCase__ = [self._time_spent] lowerCamelCase__ = 0 for time in time_spent: lowerCamelCase__ = time.split(':' ) # Time can be formatted as xx:xx:xx, as .xx, or as x.xx if the time spent was less than a minute. if len(SCREAMING_SNAKE_CASE__ ) == 1: lowerCamelCase__ = [0, 0, time_parts[0]] lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ = int(time_parts[0] ), int(time_parts[1] ), float(time_parts[2] ) total_secs += hours * 36_00 + minutes * 60 + seconds lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ = total_secs // 36_00, (total_secs % 36_00) // 60, total_secs % 60 return F'{int(SCREAMING_SNAKE_CASE__ )}h{int(SCREAMING_SNAKE_CASE__ )}m{int(SCREAMING_SNAKE_CASE__ )}s' @property def _UpperCamelCase ( self : Dict ): return {"type": "header", "text": {"type": "plain_text", "text": self.title}} @property def _UpperCamelCase ( self : Dict ): return { "type": "section", "text": { "type": "plain_text", "text": F'🌞 There were no failures: all {self.n_tests} tests passed. The suite ran in {self.time}.', "emoji": True, }, "accessory": { "type": "button", "text": {"type": "plain_text", "text": "Check Action results", "emoji": True}, "url": F'https://github.com/huggingface/transformers/actions/runs/{os.environ["GITHUB_RUN_ID"]}', }, } @property def _UpperCamelCase ( self : Any ): return { "type": "section", "text": { "type": "plain_text", "text": ( F'There were {self.n_failures} failures, out of {self.n_tests} tests.\nThe suite ran in' F' {self.time}.' ), "emoji": True, }, "accessory": { "type": "button", "text": {"type": "plain_text", "text": "Check Action results", "emoji": True}, "url": F'https://github.com/huggingface/transformers/actions/runs/{os.environ["GITHUB_RUN_ID"]}', }, } @property def _UpperCamelCase ( self : Union[str, Any] ): lowerCamelCase__ = 40 lowerCamelCase__ = {k: v['failed'] for k, v in doc_test_results.items() if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ )} lowerCamelCase__ = '' for category, failures in category_failures.items(): if len(SCREAMING_SNAKE_CASE__ ) == 0: continue if report != "": report += "\n\n" report += F'*{category} failures*:'.ljust(line_length // 2 ).rjust(line_length // 2 ) + "\n" report += "`" report += "`\n`".join(SCREAMING_SNAKE_CASE__ ) report += "`" return { "type": "section", "text": { "type": "mrkdwn", "text": F'The following examples had failures:\n\n\n{report}\n', }, } @property def _UpperCamelCase ( self : str ): lowerCamelCase__ = [self.header] if self.n_failures > 0: blocks.append(self.failures ) if self.n_failures > 0: blocks.extend([self.category_failures] ) if self.n_failures == 0: blocks.append(self.no_failures ) return json.dumps(SCREAMING_SNAKE_CASE__ ) @staticmethod def _UpperCamelCase ( ): lowerCamelCase__ = [ { 'type': 'section', 'text': { 'type': 'plain_text', 'text': 'There was an issue running the tests.', }, 'accessory': { 'type': 'button', 'text': {'type': 'plain_text', 'text': 'Check Action results', 'emoji': True}, 'url': F'https://github.com/huggingface/transformers/actions/runs/{os.environ["GITHUB_RUN_ID"]}', }, } ] print('Sending the following payload' ) print(json.dumps({'blocks': json.loads(SCREAMING_SNAKE_CASE__ )} ) ) client.chat_postMessage( channel=os.environ['CI_SLACK_CHANNEL_ID_DAILY'] , text='There was an issue running the tests.' , blocks=SCREAMING_SNAKE_CASE__ , ) def _UpperCamelCase ( self : Optional[int] ): print('Sending the following payload' ) print(json.dumps({'blocks': json.loads(self.payload )} ) ) lowerCamelCase__ = F'{self.n_failures} failures out of {self.n_tests} tests,' if self.n_failures else 'All tests passed.' lowerCamelCase__ = client.chat_postMessage( channel=os.environ['CI_SLACK_CHANNEL_ID_DAILY'] , blocks=self.payload , text=SCREAMING_SNAKE_CASE__ , ) def _UpperCamelCase ( self : Optional[Any] , SCREAMING_SNAKE_CASE__ : Dict , SCREAMING_SNAKE_CASE__ : List[Any] , SCREAMING_SNAKE_CASE__ : Tuple , SCREAMING_SNAKE_CASE__ : Tuple ): lowerCamelCase__ = '' for key, value in failures.items(): lowerCamelCase__ = value[:2_00] + ' [Truncated]' if len(SCREAMING_SNAKE_CASE__ ) > 2_50 else value failures_text += F'*{key}*\n_{value}_\n\n' lowerCamelCase__ = job_name lowerCamelCase__ = {'type': 'section', 'text': {'type': 'mrkdwn', 'text': text}} if job_link is not None: lowerCamelCase__ = { 'type': 'button', 'text': {'type': 'plain_text', 'text': 'GitHub Action job', 'emoji': True}, 'url': job_link, } return [ {"type": "header", "text": {"type": "plain_text", "text": title.upper(), "emoji": True}}, content, {"type": "section", "text": {"type": "mrkdwn", "text": failures_text}}, ] def _UpperCamelCase ( self : Optional[int] ): if self.thread_ts is None: raise ValueError('Can only post reply if a post has been made.' ) lowerCamelCase__ = self.doc_test_results.pop('job_link' ) self.doc_test_results.pop('failures' ) self.doc_test_results.pop('success' ) self.doc_test_results.pop('time_spent' ) lowerCamelCase__ = sorted(self.doc_test_results.items() , key=lambda SCREAMING_SNAKE_CASE__ : t[0] ) for job, job_result in sorted_dict: if len(job_result['failures'] ): lowerCamelCase__ = F'*Num failures* :{len(job_result["failed"] )} \n' lowerCamelCase__ = job_result['failures'] lowerCamelCase__ = self.get_reply_blocks(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , text=SCREAMING_SNAKE_CASE__ ) print('Sending the following reply' ) print(json.dumps({'blocks': blocks} ) ) client.chat_postMessage( channel=os.environ['CI_SLACK_CHANNEL_ID_DAILY'] , text=F'Results for {job}' , blocks=SCREAMING_SNAKE_CASE__ , thread_ts=self.thread_ts['ts'] , ) time.sleep(1 ) def snake_case ( )-> Optional[Any]: '''simple docstring''' lowerCamelCase__ = os.environ['GITHUB_RUN_ID'] lowerCamelCase__ = F'https://api.github.com/repos/huggingface/transformers/actions/runs/{run_id}/jobs?per_page=100' lowerCamelCase__ = requests.get(_a ).json() lowerCamelCase__ = {} try: jobs.update({job['name']: job['html_url'] for job in result['jobs']} ) lowerCamelCase__ = math.ceil((result['total_count'] - 100) / 100 ) for i in range(_a ): lowerCamelCase__ = requests.get(url + F'&page={i + 2}' ).json() jobs.update({job['name']: job['html_url'] for job in result['jobs']} ) return jobs except Exception as e: print('Unknown error, could not fetch links.' , _a ) return {} def snake_case ( _a: str )-> Dict: '''simple docstring''' lowerCamelCase__ = {} if os.path.exists(_a ): lowerCamelCase__ = os.listdir(_a ) for file in files: try: with open(os.path.join(_a , _a ) , encoding='utf-8' ) as f: lowerCamelCase__ = f.read() except UnicodeDecodeError as e: raise ValueError(F'Could not open {os.path.join(_a , _a )}.' ) from e return _artifact def snake_case ( )-> Optional[int]: '''simple docstring''' class _a : def __init__( self : Tuple , SCREAMING_SNAKE_CASE__ : str ): lowerCamelCase__ = name lowerCamelCase__ = [] def __str__( self : Dict ): return self.name def _UpperCamelCase ( self : Union[str, Any] , SCREAMING_SNAKE_CASE__ : str ): self.paths.append({'name': self.name, 'path': path} ) lowerCamelCase__ = {} lowerCamelCase__ = filter(os.path.isdir , os.listdir() ) for directory in directories: lowerCamelCase__ = directory if artifact_name not in _available_artifacts: lowerCamelCase__ = Artifact(_a ) _available_artifacts[artifact_name].add_path(_a ) return _available_artifacts if __name__ == "__main__": _snake_case = get_job_links() _snake_case = retrieve_available_artifacts() _snake_case = collections.OrderedDict( [ ("*.py", "API Examples"), ("*.md", "MD Examples"), ] ) # This dict will contain all the information relative to each doc test category: # - failed: list of failed tests # - failures: dict in the format 'test': 'error_message' _snake_case = { v: { "failed": [], "failures": {}, } for v in docs.values() } # Link to the GitHub Action job _snake_case = github_actions_job_links.get("run_doctests") _snake_case = available_artifacts["doc_tests_gpu_test_reports"].paths[0] _snake_case = retrieve_artifact(artifact_path["name"]) if "stats" in artifact: _snake_case , _snake_case , _snake_case = handle_test_results(artifact["stats"]) _snake_case = failed _snake_case = success _snake_case = time_spent[1:-1] + ", " _snake_case = extract_first_line_failure(artifact["failures_short"]) for line in artifact["summary_short"].split("\n"): if re.search("FAILED", line): _snake_case = line.replace("FAILED ", "") _snake_case = line.split()[0].replace("\n", "") if "::" in line: _snake_case , _snake_case = line.split("::") else: _snake_case , _snake_case = line, line for file_regex in docs.keys(): if fnmatch(file_path, file_regex): _snake_case = docs[file_regex] doc_test_results[category]["failed"].append(test) _snake_case = all_failures[test] if test in all_failures else "N/A" _snake_case = failure break _snake_case = Message("🤗 Results of the doc tests.", doc_test_results) message.post() message.post_reply()
659
1
"""simple docstring""" from typing import Dict, List, Optional, Tuple, Union import torch from ...models import AutoencoderKL, TransformeraDModel from ...schedulers import KarrasDiffusionSchedulers from ...utils import randn_tensor from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput class _a ( SCREAMING_SNAKE_CASE_ ): def __init__( self : int , SCREAMING_SNAKE_CASE__ : TransformeraDModel , SCREAMING_SNAKE_CASE__ : AutoencoderKL , SCREAMING_SNAKE_CASE__ : KarrasDiffusionSchedulers , SCREAMING_SNAKE_CASE__ : Optional[Dict[int, str]] = None , ): super().__init__() self.register_modules(transformer=SCREAMING_SNAKE_CASE__ , vae=SCREAMING_SNAKE_CASE__ , scheduler=SCREAMING_SNAKE_CASE__ ) # create a imagenet -> id dictionary for easier use lowerCamelCase__ = {} if idalabel is not None: for key, value in idalabel.items(): for label in value.split(',' ): lowerCamelCase__ = int(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = dict(sorted(self.labels.items() ) ) def _UpperCamelCase ( self : Optional[Any] , SCREAMING_SNAKE_CASE__ : Union[str, List[str]] ): if not isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): lowerCamelCase__ = list(SCREAMING_SNAKE_CASE__ ) for l in label: if l not in self.labels: raise ValueError( F'{l} does not exist. Please make sure to select one of the following labels: \n {self.labels}.' ) return [self.labels[l] for l in label] @torch.no_grad() def __call__( self : List[Any] , SCREAMING_SNAKE_CASE__ : List[int] , SCREAMING_SNAKE_CASE__ : float = 4.0 , SCREAMING_SNAKE_CASE__ : Optional[Union[torch.Generator, List[torch.Generator]]] = None , SCREAMING_SNAKE_CASE__ : int = 50 , SCREAMING_SNAKE_CASE__ : Optional[str] = "pil" , SCREAMING_SNAKE_CASE__ : bool = True , ): lowerCamelCase__ = len(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = self.transformer.config.sample_size lowerCamelCase__ = self.transformer.config.in_channels lowerCamelCase__ = randn_tensor( shape=(batch_size, latent_channels, latent_size, latent_size) , generator=SCREAMING_SNAKE_CASE__ , device=self.device , dtype=self.transformer.dtype , ) lowerCamelCase__ = torch.cat([latents] * 2 ) if guidance_scale > 1 else latents lowerCamelCase__ = torch.tensor(SCREAMING_SNAKE_CASE__ , device=self.device ).reshape(-1 ) lowerCamelCase__ = torch.tensor([10_00] * batch_size , device=self.device ) lowerCamelCase__ = torch.cat([class_labels, class_null] , 0 ) if guidance_scale > 1 else class_labels # set step values self.scheduler.set_timesteps(SCREAMING_SNAKE_CASE__ ) for t in self.progress_bar(self.scheduler.timesteps ): if guidance_scale > 1: lowerCamelCase__ = latent_model_input[: len(SCREAMING_SNAKE_CASE__ ) // 2] lowerCamelCase__ = torch.cat([half, half] , dim=0 ) lowerCamelCase__ = self.scheduler.scale_model_input(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = t if not torch.is_tensor(SCREAMING_SNAKE_CASE__ ): # TODO: this requires sync between CPU and GPU. So try to pass timesteps as tensors if you can # This would be a good case for the `match` statement (Python 3.10+) lowerCamelCase__ = latent_model_input.device.type == 'mps' if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): lowerCamelCase__ = torch.floataa if is_mps else torch.floataa else: lowerCamelCase__ = torch.intaa if is_mps else torch.intaa lowerCamelCase__ = torch.tensor([timesteps] , dtype=SCREAMING_SNAKE_CASE__ , device=latent_model_input.device ) elif len(timesteps.shape ) == 0: lowerCamelCase__ = timesteps[None].to(latent_model_input.device ) # broadcast to batch dimension in a way that's compatible with ONNX/Core ML lowerCamelCase__ = timesteps.expand(latent_model_input.shape[0] ) # predict noise model_output lowerCamelCase__ = self.transformer( SCREAMING_SNAKE_CASE__ , timestep=SCREAMING_SNAKE_CASE__ , class_labels=SCREAMING_SNAKE_CASE__ ).sample # perform guidance if guidance_scale > 1: lowerCamelCase__ , lowerCamelCase__ = noise_pred[:, :latent_channels], noise_pred[:, latent_channels:] lowerCamelCase__ , lowerCamelCase__ = torch.split(SCREAMING_SNAKE_CASE__ , len(SCREAMING_SNAKE_CASE__ ) // 2 , dim=0 ) lowerCamelCase__ = uncond_eps + guidance_scale * (cond_eps - uncond_eps) lowerCamelCase__ = torch.cat([half_eps, half_eps] , dim=0 ) lowerCamelCase__ = torch.cat([eps, rest] , dim=1 ) # learned sigma if self.transformer.config.out_channels // 2 == latent_channels: lowerCamelCase__ , lowerCamelCase__ = torch.split(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , dim=1 ) else: lowerCamelCase__ = noise_pred # compute previous image: x_t -> x_t-1 lowerCamelCase__ = self.scheduler.step(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ).prev_sample if guidance_scale > 1: lowerCamelCase__ , lowerCamelCase__ = latent_model_input.chunk(2 , dim=0 ) else: lowerCamelCase__ = latent_model_input lowerCamelCase__ = 1 / self.vae.config.scaling_factor * latents lowerCamelCase__ = self.vae.decode(SCREAMING_SNAKE_CASE__ ).sample lowerCamelCase__ = (samples / 2 + 0.5).clamp(0 , 1 ) # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16 lowerCamelCase__ = samples.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() if output_type == "pil": lowerCamelCase__ = self.numpy_to_pil(SCREAMING_SNAKE_CASE__ ) if not return_dict: return (samples,) return ImagePipelineOutput(images=SCREAMING_SNAKE_CASE__ )
659
"""simple docstring""" 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 _snake_case = logging.get_logger(__name__) # pylint: disable=invalid-name _snake_case = "\n Examples:\n ```py\n >>> from PIL import Image\n >>> import torch\n >>> from diffusers import DiffusionPipeline\n >>> from diffusers.utils import export_to_gif, load_image\n\n >>> device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n\n >>> repo = \"openai/shap-e-img2img\"\n >>> pipe = DiffusionPipeline.from_pretrained(repo, torch_dtype=torch.float16)\n >>> pipe = pipe.to(device)\n\n >>> guidance_scale = 3.0\n >>> image_url = \"https://hf.co/datasets/diffusers/docs-images/resolve/main/shap-e/corgi.png\"\n >>> image = load_image(image_url).convert(\"RGB\")\n\n >>> images = pipe(\n ... image,\n ... guidance_scale=guidance_scale,\n ... num_inference_steps=64,\n ... frame_size=256,\n ... ).images\n\n >>> gif_path = export_to_gif(images[0], \"corgi_3d.gif\")\n ```\n" @dataclass class _a ( SCREAMING_SNAKE_CASE_ ): a_ : Union[PIL.Image.Image, np.ndarray] class _a ( SCREAMING_SNAKE_CASE_ ): def __init__( self : Tuple , SCREAMING_SNAKE_CASE__ : PriorTransformer , SCREAMING_SNAKE_CASE__ : CLIPVisionModel , SCREAMING_SNAKE_CASE__ : CLIPImageProcessor , SCREAMING_SNAKE_CASE__ : HeunDiscreteScheduler , SCREAMING_SNAKE_CASE__ : ShapERenderer , ): super().__init__() self.register_modules( prior=SCREAMING_SNAKE_CASE__ , image_encoder=SCREAMING_SNAKE_CASE__ , image_processor=SCREAMING_SNAKE_CASE__ , scheduler=SCREAMING_SNAKE_CASE__ , renderer=SCREAMING_SNAKE_CASE__ , ) def _UpperCamelCase ( self : str , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : Optional[int] , SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : List[str] ): if latents is None: lowerCamelCase__ = randn_tensor(SCREAMING_SNAKE_CASE__ , generator=SCREAMING_SNAKE_CASE__ , device=SCREAMING_SNAKE_CASE__ , dtype=SCREAMING_SNAKE_CASE__ ) else: if latents.shape != shape: raise ValueError(F'Unexpected latents shape, got {latents.shape}, expected {shape}' ) lowerCamelCase__ = latents.to(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = latents * scheduler.init_noise_sigma return latents def _UpperCamelCase ( self : List[Any] , SCREAMING_SNAKE_CASE__ : List[str]=0 ): if is_accelerate_available(): from accelerate import cpu_offload else: raise ImportError('Please install accelerate via `pip install accelerate`' ) lowerCamelCase__ = torch.device(F'cuda:{gpu_id}' ) lowerCamelCase__ = [self.image_encoder, self.prior] for cpu_offloaded_model in models: if cpu_offloaded_model is not None: cpu_offload(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) @property def _UpperCamelCase ( self : Dict ): 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(SCREAMING_SNAKE_CASE__ , '_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 _UpperCamelCase ( self : Optional[int] , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : Dict , SCREAMING_SNAKE_CASE__ : Optional[Any] , SCREAMING_SNAKE_CASE__ : int , ): if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and isinstance(image[0] , torch.Tensor ): lowerCamelCase__ = torch.cat(SCREAMING_SNAKE_CASE__ , axis=0 ) if image[0].ndim == 4 else torch.stack(SCREAMING_SNAKE_CASE__ , axis=0 ) if not isinstance(SCREAMING_SNAKE_CASE__ , torch.Tensor ): lowerCamelCase__ = self.image_processor(SCREAMING_SNAKE_CASE__ , return_tensors='pt' ).pixel_values[0].unsqueeze(0 ) lowerCamelCase__ = image.to(dtype=self.image_encoder.dtype , device=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = self.image_encoder(SCREAMING_SNAKE_CASE__ )['last_hidden_state'] lowerCamelCase__ = image_embeds[:, 1:, :].contiguous() # batch_size, dim, 256 lowerCamelCase__ = image_embeds.repeat_interleave(SCREAMING_SNAKE_CASE__ , dim=0 ) if do_classifier_free_guidance: lowerCamelCase__ = torch.zeros_like(SCREAMING_SNAKE_CASE__ ) # 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 lowerCamelCase__ = torch.cat([negative_image_embeds, image_embeds] ) return image_embeds @torch.no_grad() @replace_example_docstring(SCREAMING_SNAKE_CASE__ ) def __call__( self : List[Any] , SCREAMING_SNAKE_CASE__ : Union[PIL.Image.Image, List[PIL.Image.Image]] , SCREAMING_SNAKE_CASE__ : int = 1 , SCREAMING_SNAKE_CASE__ : int = 25 , SCREAMING_SNAKE_CASE__ : Optional[Union[torch.Generator, List[torch.Generator]]] = None , SCREAMING_SNAKE_CASE__ : Optional[torch.FloatTensor] = None , SCREAMING_SNAKE_CASE__ : float = 4.0 , SCREAMING_SNAKE_CASE__ : int = 64 , SCREAMING_SNAKE_CASE__ : Optional[str] = "pil" , SCREAMING_SNAKE_CASE__ : bool = True , ): if isinstance(SCREAMING_SNAKE_CASE__ , PIL.Image.Image ): lowerCamelCase__ = 1 elif isinstance(SCREAMING_SNAKE_CASE__ , torch.Tensor ): lowerCamelCase__ = image.shape[0] elif isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and isinstance(image[0] , (torch.Tensor, PIL.Image.Image) ): lowerCamelCase__ = len(SCREAMING_SNAKE_CASE__ ) 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(SCREAMING_SNAKE_CASE__ )}' ) lowerCamelCase__ = self._execution_device lowerCamelCase__ = batch_size * num_images_per_prompt lowerCamelCase__ = guidance_scale > 1.0 lowerCamelCase__ = self._encode_image(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) # prior self.scheduler.set_timesteps(SCREAMING_SNAKE_CASE__ , device=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = self.scheduler.timesteps lowerCamelCase__ = self.prior.config.num_embeddings lowerCamelCase__ = self.prior.config.embedding_dim lowerCamelCase__ = self.prepare_latents( (batch_size, num_embeddings * embedding_dim) , image_embeds.dtype , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , 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 lowerCamelCase__ = latents.reshape(latents.shape[0] , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) for i, t in enumerate(self.progress_bar(SCREAMING_SNAKE_CASE__ ) ): # expand the latents if we are doing classifier free guidance lowerCamelCase__ = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents lowerCamelCase__ = self.scheduler.scale_model_input(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = self.prior( SCREAMING_SNAKE_CASE__ , timestep=SCREAMING_SNAKE_CASE__ , proj_embedding=SCREAMING_SNAKE_CASE__ , ).predicted_image_embedding # remove the variance lowerCamelCase__ , lowerCamelCase__ = noise_pred.split( scaled_model_input.shape[2] , dim=2 ) # batch_size, num_embeddings, embedding_dim if do_classifier_free_guidance is not None: lowerCamelCase__ , lowerCamelCase__ = noise_pred.chunk(2 ) lowerCamelCase__ = noise_pred_uncond + guidance_scale * (noise_pred - noise_pred_uncond) lowerCamelCase__ = self.scheduler.step( SCREAMING_SNAKE_CASE__ , timestep=SCREAMING_SNAKE_CASE__ , sample=SCREAMING_SNAKE_CASE__ , ).prev_sample if output_type == "latent": return ShapEPipelineOutput(images=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = [] for i, latent in enumerate(SCREAMING_SNAKE_CASE__ ): print() lowerCamelCase__ = self.renderer.decode( latent[None, :] , SCREAMING_SNAKE_CASE__ , size=SCREAMING_SNAKE_CASE__ , ray_batch_size=40_96 , n_coarse_samples=64 , n_fine_samples=1_28 , ) images.append(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = torch.stack(SCREAMING_SNAKE_CASE__ ) if output_type not in ["np", "pil"]: raise ValueError(F'Only the output types `pil` and `np` are supported not output_type={output_type}' ) lowerCamelCase__ = images.cpu().numpy() if output_type == "pil": lowerCamelCase__ = [self.numpy_to_pil(SCREAMING_SNAKE_CASE__ ) 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=SCREAMING_SNAKE_CASE__ )
659
1
"""simple docstring""" from ..utils import DummyObject, requires_backends class _a ( metaclass=SCREAMING_SNAKE_CASE_ ): a_ : int = ['keras_nlp'] def __init__( self : str , *SCREAMING_SNAKE_CASE__ : Any , **SCREAMING_SNAKE_CASE__ : Optional[int] ): requires_backends(self , ['keras_nlp'] )
659
"""simple docstring""" import os from shutil import copyfile from typing import List, Optional, Tuple from tokenizers import processors from ...tokenization_utils import AddedToken, BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_nllb import NllbTokenizer else: _snake_case = None _snake_case = logging.get_logger(__name__) _snake_case = {"vocab_file": "sentencepiece.bpe.model", "tokenizer_file": "tokenizer.json"} _snake_case = { "vocab_file": { "facebook/nllb-200-distilled-600M": ( "https://huggingface.co/facebook/nllb-200-distilled-600M/resolve/main/sentencepiece.bpe.model" ), }, "tokenizer_file": { "facebook/nllb-200-distilled-600M": ( "https://huggingface.co/facebook/nllb-200-distilled-600M/resolve/main/tokenizer.json" ), }, } _snake_case = { "facebook/nllb-large-en-ro": 1024, "facebook/nllb-200-distilled-600M": 1024, } # fmt: off _snake_case = ["ace_Arab", "ace_Latn", "acm_Arab", "acq_Arab", "aeb_Arab", "afr_Latn", "ajp_Arab", "aka_Latn", "amh_Ethi", "apc_Arab", "arb_Arab", "ars_Arab", "ary_Arab", "arz_Arab", "asm_Beng", "ast_Latn", "awa_Deva", "ayr_Latn", "azb_Arab", "azj_Latn", "bak_Cyrl", "bam_Latn", "ban_Latn", "bel_Cyrl", "bem_Latn", "ben_Beng", "bho_Deva", "bjn_Arab", "bjn_Latn", "bod_Tibt", "bos_Latn", "bug_Latn", "bul_Cyrl", "cat_Latn", "ceb_Latn", "ces_Latn", "cjk_Latn", "ckb_Arab", "crh_Latn", "cym_Latn", "dan_Latn", "deu_Latn", "dik_Latn", "dyu_Latn", "dzo_Tibt", "ell_Grek", "eng_Latn", "epo_Latn", "est_Latn", "eus_Latn", "ewe_Latn", "fao_Latn", "pes_Arab", "fij_Latn", "fin_Latn", "fon_Latn", "fra_Latn", "fur_Latn", "fuv_Latn", "gla_Latn", "gle_Latn", "glg_Latn", "grn_Latn", "guj_Gujr", "hat_Latn", "hau_Latn", "heb_Hebr", "hin_Deva", "hne_Deva", "hrv_Latn", "hun_Latn", "hye_Armn", "ibo_Latn", "ilo_Latn", "ind_Latn", "isl_Latn", "ita_Latn", "jav_Latn", "jpn_Jpan", "kab_Latn", "kac_Latn", "kam_Latn", "kan_Knda", "kas_Arab", "kas_Deva", "kat_Geor", "knc_Arab", "knc_Latn", "kaz_Cyrl", "kbp_Latn", "kea_Latn", "khm_Khmr", "kik_Latn", "kin_Latn", "kir_Cyrl", "kmb_Latn", "kon_Latn", "kor_Hang", "kmr_Latn", "lao_Laoo", "lvs_Latn", "lij_Latn", "lim_Latn", "lin_Latn", "lit_Latn", "lmo_Latn", "ltg_Latn", "ltz_Latn", "lua_Latn", "lug_Latn", "luo_Latn", "lus_Latn", "mag_Deva", "mai_Deva", "mal_Mlym", "mar_Deva", "min_Latn", "mkd_Cyrl", "plt_Latn", "mlt_Latn", "mni_Beng", "khk_Cyrl", "mos_Latn", "mri_Latn", "zsm_Latn", "mya_Mymr", "nld_Latn", "nno_Latn", "nob_Latn", "npi_Deva", "nso_Latn", "nus_Latn", "nya_Latn", "oci_Latn", "gaz_Latn", "ory_Orya", "pag_Latn", "pan_Guru", "pap_Latn", "pol_Latn", "por_Latn", "prs_Arab", "pbt_Arab", "quy_Latn", "ron_Latn", "run_Latn", "rus_Cyrl", "sag_Latn", "san_Deva", "sat_Beng", "scn_Latn", "shn_Mymr", "sin_Sinh", "slk_Latn", "slv_Latn", "smo_Latn", "sna_Latn", "snd_Arab", "som_Latn", "sot_Latn", "spa_Latn", "als_Latn", "srd_Latn", "srp_Cyrl", "ssw_Latn", "sun_Latn", "swe_Latn", "swh_Latn", "szl_Latn", "tam_Taml", "tat_Cyrl", "tel_Telu", "tgk_Cyrl", "tgl_Latn", "tha_Thai", "tir_Ethi", "taq_Latn", "taq_Tfng", "tpi_Latn", "tsn_Latn", "tso_Latn", "tuk_Latn", "tum_Latn", "tur_Latn", "twi_Latn", "tzm_Tfng", "uig_Arab", "ukr_Cyrl", "umb_Latn", "urd_Arab", "uzn_Latn", "vec_Latn", "vie_Latn", "war_Latn", "wol_Latn", "xho_Latn", "ydd_Hebr", "yor_Latn", "yue_Hant", "zho_Hans", "zho_Hant", "zul_Latn"] class _a ( SCREAMING_SNAKE_CASE_ ): a_ : Any = VOCAB_FILES_NAMES a_ : Optional[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES a_ : Optional[Any] = PRETRAINED_VOCAB_FILES_MAP a_ : List[str] = ['input_ids', 'attention_mask'] a_ : Union[str, Any] = NllbTokenizer a_ : List[int] = [] a_ : List[int] = [] def __init__( self : int , SCREAMING_SNAKE_CASE__ : List[Any]=None , SCREAMING_SNAKE_CASE__ : Optional[int]=None , SCREAMING_SNAKE_CASE__ : List[Any]="<s>" , SCREAMING_SNAKE_CASE__ : List[str]="</s>" , SCREAMING_SNAKE_CASE__ : Any="</s>" , SCREAMING_SNAKE_CASE__ : List[str]="<s>" , SCREAMING_SNAKE_CASE__ : Tuple="<unk>" , SCREAMING_SNAKE_CASE__ : Optional[int]="<pad>" , SCREAMING_SNAKE_CASE__ : Any="<mask>" , SCREAMING_SNAKE_CASE__ : Union[str, Any]=None , SCREAMING_SNAKE_CASE__ : int=None , SCREAMING_SNAKE_CASE__ : Dict=None , SCREAMING_SNAKE_CASE__ : Tuple=False , **SCREAMING_SNAKE_CASE__ : str , ): # Mask token behave like a normal word, i.e. include the space before it lowerCamelCase__ = AddedToken(SCREAMING_SNAKE_CASE__ , lstrip=SCREAMING_SNAKE_CASE__ , rstrip=SCREAMING_SNAKE_CASE__ ) if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) else mask_token lowerCamelCase__ = legacy_behaviour super().__init__( vocab_file=SCREAMING_SNAKE_CASE__ , tokenizer_file=SCREAMING_SNAKE_CASE__ , bos_token=SCREAMING_SNAKE_CASE__ , eos_token=SCREAMING_SNAKE_CASE__ , sep_token=SCREAMING_SNAKE_CASE__ , cls_token=SCREAMING_SNAKE_CASE__ , unk_token=SCREAMING_SNAKE_CASE__ , pad_token=SCREAMING_SNAKE_CASE__ , mask_token=SCREAMING_SNAKE_CASE__ , src_lang=SCREAMING_SNAKE_CASE__ , tgt_lang=SCREAMING_SNAKE_CASE__ , additional_special_tokens=SCREAMING_SNAKE_CASE__ , legacy_behaviour=SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ , ) lowerCamelCase__ = vocab_file lowerCamelCase__ = False if not self.vocab_file else True lowerCamelCase__ = FAIRSEQ_LANGUAGE_CODES.copy() if additional_special_tokens is not None: # Only add those special tokens if they are not already there. _additional_special_tokens.extend( [t for t in additional_special_tokens if t not in _additional_special_tokens] ) self.add_special_tokens({'additional_special_tokens': _additional_special_tokens} ) lowerCamelCase__ = { lang_code: self.convert_tokens_to_ids(SCREAMING_SNAKE_CASE__ ) for lang_code in FAIRSEQ_LANGUAGE_CODES } lowerCamelCase__ = src_lang if src_lang is not None else 'eng_Latn' lowerCamelCase__ = self.convert_tokens_to_ids(self._src_lang ) lowerCamelCase__ = tgt_lang self.set_src_lang_special_tokens(self._src_lang ) @property def _UpperCamelCase ( self : str ): return self._src_lang @src_lang.setter def _UpperCamelCase ( self : int , SCREAMING_SNAKE_CASE__ : str ): lowerCamelCase__ = new_src_lang self.set_src_lang_special_tokens(self._src_lang ) def _UpperCamelCase ( self : Tuple , SCREAMING_SNAKE_CASE__ : List[int] , SCREAMING_SNAKE_CASE__ : Optional[List[int]] = None ): if token_ids_a is None: return self.prefix_tokens + token_ids_a + self.suffix_tokens # We don't expect to process pairs, but leave the pair logic for API consistency return self.prefix_tokens + token_ids_a + token_ids_a + self.suffix_tokens def _UpperCamelCase ( self : Optional[Any] , SCREAMING_SNAKE_CASE__ : List[int] , SCREAMING_SNAKE_CASE__ : Optional[List[int]] = None ): lowerCamelCase__ = [self.sep_token_id] lowerCamelCase__ = [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 _UpperCamelCase ( self : str , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : Optional[str] , SCREAMING_SNAKE_CASE__ : Optional[str] , **SCREAMING_SNAKE_CASE__ : Optional[int] ): if src_lang is None or tgt_lang is None: raise ValueError('Translation requires a `src_lang` and a `tgt_lang` for this model' ) lowerCamelCase__ = src_lang lowerCamelCase__ = self(SCREAMING_SNAKE_CASE__ , add_special_tokens=SCREAMING_SNAKE_CASE__ , return_tensors=SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = self.convert_tokens_to_ids(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = tgt_lang_id return inputs def _UpperCamelCase ( self : str , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : str = "eng_Latn" , SCREAMING_SNAKE_CASE__ : Optional[List[str]] = None , SCREAMING_SNAKE_CASE__ : str = "fra_Latn" , **SCREAMING_SNAKE_CASE__ : Dict , ): lowerCamelCase__ = src_lang lowerCamelCase__ = tgt_lang return super().prepare_seqaseq_batch(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : List[str] ): return self.set_src_lang_special_tokens(self.src_lang ) def _UpperCamelCase ( self : List[Any] ): return self.set_tgt_lang_special_tokens(self.tgt_lang ) def _UpperCamelCase ( self : str , SCREAMING_SNAKE_CASE__ : Union[str, Any] ): lowerCamelCase__ = self.convert_tokens_to_ids(SCREAMING_SNAKE_CASE__ ) if self.legacy_behaviour: lowerCamelCase__ = [] lowerCamelCase__ = [self.eos_token_id, self.cur_lang_code] else: lowerCamelCase__ = [self.cur_lang_code] lowerCamelCase__ = [self.eos_token_id] lowerCamelCase__ = self.convert_ids_to_tokens(self.prefix_tokens ) lowerCamelCase__ = self.convert_ids_to_tokens(self.suffix_tokens ) lowerCamelCase__ = processors.TemplateProcessing( single=prefix_tokens_str + ['$A'] + suffix_tokens_str , pair=prefix_tokens_str + ['$A', '$B'] + suffix_tokens_str , special_tokens=list(zip(prefix_tokens_str + suffix_tokens_str , self.prefix_tokens + self.suffix_tokens ) ) , ) def _UpperCamelCase ( self : Union[str, Any] , SCREAMING_SNAKE_CASE__ : str ): lowerCamelCase__ = self.convert_tokens_to_ids(SCREAMING_SNAKE_CASE__ ) if self.legacy_behaviour: lowerCamelCase__ = [] lowerCamelCase__ = [self.eos_token_id, self.cur_lang_code] else: lowerCamelCase__ = [self.cur_lang_code] lowerCamelCase__ = [self.eos_token_id] lowerCamelCase__ = self.convert_ids_to_tokens(self.prefix_tokens ) lowerCamelCase__ = self.convert_ids_to_tokens(self.suffix_tokens ) lowerCamelCase__ = processors.TemplateProcessing( single=prefix_tokens_str + ['$A'] + suffix_tokens_str , pair=prefix_tokens_str + ['$A', '$B'] + suffix_tokens_str , special_tokens=list(zip(prefix_tokens_str + suffix_tokens_str , self.prefix_tokens + self.suffix_tokens ) ) , ) def _UpperCamelCase ( self : Dict , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : Optional[str] = None ): if not self.can_save_slow_tokenizer: raise ValueError( 'Your fast tokenizer does not have the necessary information to save the vocabulary for a slow ' 'tokenizer.' ) if not os.path.isdir(SCREAMING_SNAKE_CASE__ ): logger.error(F'Vocabulary path ({save_directory}) should be a directory.' ) return lowerCamelCase__ = os.path.join( SCREAMING_SNAKE_CASE__ , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(SCREAMING_SNAKE_CASE__ ): copyfile(self.vocab_file , SCREAMING_SNAKE_CASE__ ) return (out_vocab_file,)
659
1
"""simple docstring""" import importlib import os from dataclasses import dataclass from enum import Enum from typing import Any, Dict, Optional, Union import torch from ..utils import BaseOutput _snake_case = "scheduler_config.json" class _a ( SCREAMING_SNAKE_CASE_ ): a_ : int = 1 a_ : Tuple = 2 a_ : Dict = 3 a_ : Any = 4 a_ : List[Any] = 5 a_ : int = 6 a_ : Optional[int] = 7 a_ : Union[str, Any] = 8 a_ : Optional[int] = 9 a_ : Tuple = 10 a_ : Optional[int] = 11 a_ : str = 12 a_ : List[Any] = 13 a_ : List[str] = 14 @dataclass class _a ( SCREAMING_SNAKE_CASE_ ): a_ : torch.FloatTensor class _a : a_ : Dict = SCHEDULER_CONFIG_NAME a_ : Union[str, Any] = [] a_ : Union[str, Any] = True @classmethod def _UpperCamelCase ( cls : str , SCREAMING_SNAKE_CASE__ : Dict[str, Any] = None , SCREAMING_SNAKE_CASE__ : Optional[str] = None , SCREAMING_SNAKE_CASE__ : Union[str, Any]=False , **SCREAMING_SNAKE_CASE__ : str , ): lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ = cls.load_config( pretrained_model_name_or_path=SCREAMING_SNAKE_CASE__ , subfolder=SCREAMING_SNAKE_CASE__ , return_unused_kwargs=SCREAMING_SNAKE_CASE__ , return_commit_hash=SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ , ) return cls.from_config(SCREAMING_SNAKE_CASE__ , return_unused_kwargs=SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : List[str] , SCREAMING_SNAKE_CASE__ : Union[str, os.PathLike] , SCREAMING_SNAKE_CASE__ : bool = False , **SCREAMING_SNAKE_CASE__ : str ): self.save_config(save_directory=SCREAMING_SNAKE_CASE__ , push_to_hub=SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) @property def _UpperCamelCase ( self : Union[str, Any] ): return self._get_compatibles() @classmethod def _UpperCamelCase ( cls : Union[str, Any] ): lowerCamelCase__ = list(set([cls.__name__] + cls._compatibles ) ) lowerCamelCase__ = importlib.import_module(__name__.split('.' )[0] ) lowerCamelCase__ = [ getattr(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) for c in compatible_classes_str if hasattr(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) ] return compatible_classes
659
"""simple docstring""" from __future__ import annotations import unittest import numpy as np from transformers import BlipTextConfig from transformers.testing_utils import require_tf, slow from transformers.utils import is_tf_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask if is_tf_available(): import tensorflow as tf from transformers import TFBlipTextModel from transformers.models.blip.modeling_tf_blip import TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST class _a : def __init__( self : List[str] , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : Union[str, Any]=12 , SCREAMING_SNAKE_CASE__ : Union[str, Any]=7 , SCREAMING_SNAKE_CASE__ : Union[str, Any]=True , SCREAMING_SNAKE_CASE__ : Optional[Any]=True , SCREAMING_SNAKE_CASE__ : str=True , SCREAMING_SNAKE_CASE__ : Any=99 , SCREAMING_SNAKE_CASE__ : Optional[Any]=32 , SCREAMING_SNAKE_CASE__ : str=32 , SCREAMING_SNAKE_CASE__ : Tuple=2 , SCREAMING_SNAKE_CASE__ : str=4 , SCREAMING_SNAKE_CASE__ : Optional[Any]=37 , SCREAMING_SNAKE_CASE__ : Union[str, Any]=0.1 , SCREAMING_SNAKE_CASE__ : Optional[Any]=0.1 , SCREAMING_SNAKE_CASE__ : str=5_12 , SCREAMING_SNAKE_CASE__ : str=0.02 , SCREAMING_SNAKE_CASE__ : Union[str, Any]=0 , SCREAMING_SNAKE_CASE__ : Any=None , ): lowerCamelCase__ = parent lowerCamelCase__ = batch_size lowerCamelCase__ = seq_length lowerCamelCase__ = is_training lowerCamelCase__ = use_input_mask lowerCamelCase__ = use_labels lowerCamelCase__ = vocab_size lowerCamelCase__ = hidden_size lowerCamelCase__ = projection_dim lowerCamelCase__ = num_hidden_layers lowerCamelCase__ = num_attention_heads lowerCamelCase__ = intermediate_size lowerCamelCase__ = dropout lowerCamelCase__ = attention_dropout lowerCamelCase__ = max_position_embeddings lowerCamelCase__ = initializer_range lowerCamelCase__ = scope lowerCamelCase__ = bos_token_id def _UpperCamelCase ( self : int ): lowerCamelCase__ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) lowerCamelCase__ = None if self.use_input_mask: lowerCamelCase__ = random_attention_mask([self.batch_size, self.seq_length] ) if input_mask is not None: lowerCamelCase__ = input_mask.numpy() lowerCamelCase__ , lowerCamelCase__ = input_mask.shape lowerCamelCase__ = np.random.randint(1 , seq_length - 1 , size=(batch_size,) ) for batch_idx, start_index in enumerate(SCREAMING_SNAKE_CASE__ ): lowerCamelCase__ = 1 lowerCamelCase__ = 0 lowerCamelCase__ = self.get_config() return config, input_ids, tf.convert_to_tensor(SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : Any ): return BlipTextConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , projection_dim=self.projection_dim , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , dropout=self.dropout , attention_dropout=self.attention_dropout , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , bos_token_id=self.bos_token_id , ) def _UpperCamelCase ( self : Any , SCREAMING_SNAKE_CASE__ : Any , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : List[str] ): lowerCamelCase__ = TFBlipTextModel(config=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = model(SCREAMING_SNAKE_CASE__ , attention_mask=SCREAMING_SNAKE_CASE__ , training=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = model(SCREAMING_SNAKE_CASE__ , training=SCREAMING_SNAKE_CASE__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size) ) def _UpperCamelCase ( self : Dict ): lowerCamelCase__ = self.prepare_config_and_inputs() lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ = config_and_inputs lowerCamelCase__ = {'input_ids': input_ids, 'attention_mask': input_mask} return config, inputs_dict @require_tf class _a ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): a_ : str = (TFBlipTextModel,) if is_tf_available() else () a_ : List[str] = False a_ : Optional[Any] = False a_ : Union[str, Any] = False def _UpperCamelCase ( self : str ): lowerCamelCase__ = BlipTextModelTester(self ) lowerCamelCase__ = ConfigTester(self , config_class=SCREAMING_SNAKE_CASE__ , hidden_size=37 ) def _UpperCamelCase ( self : Tuple ): self.config_tester.run_common_tests() def _UpperCamelCase ( self : str ): lowerCamelCase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : Tuple ): pass def _UpperCamelCase ( self : Tuple ): pass @unittest.skip(reason='Blip does not use inputs_embeds' ) def _UpperCamelCase ( self : List[str] ): pass @unittest.skip(reason='BlipTextModel has no base class and is not available in MODEL_MAPPING' ) def _UpperCamelCase ( self : Dict ): pass @unittest.skip(reason='BlipTextModel has no base class and is not available in MODEL_MAPPING' ) def _UpperCamelCase ( self : List[Any] ): pass @slow def _UpperCamelCase ( self : str ): for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowerCamelCase__ = TFBlipTextModel.from_pretrained(SCREAMING_SNAKE_CASE__ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : Optional[int] , SCREAMING_SNAKE_CASE__ : List[Any]=True ): super().test_pt_tf_model_equivalence(allow_missing_keys=SCREAMING_SNAKE_CASE__ )
659
1
"""simple docstring""" import argparse import OmegaConf import torch from diffusers import DDIMScheduler, LDMPipeline, UNetLDMModel, VQModel def snake_case ( _a: Optional[Any] , _a: Dict , _a: Optional[Any] )-> List[Any]: '''simple docstring''' lowerCamelCase__ = OmegaConf.load(_a ) lowerCamelCase__ = torch.load(_a , map_location='cpu' )['model'] lowerCamelCase__ = list(state_dict.keys() ) # extract state_dict for VQVAE lowerCamelCase__ = {} lowerCamelCase__ = 'first_stage_model.' for key in keys: if key.startswith(_a ): lowerCamelCase__ = state_dict[key] # extract state_dict for UNetLDM lowerCamelCase__ = {} lowerCamelCase__ = 'model.diffusion_model.' for key in keys: if key.startswith(_a ): lowerCamelCase__ = state_dict[key] lowerCamelCase__ = config.model.params.first_stage_config.params lowerCamelCase__ = config.model.params.unet_config.params lowerCamelCase__ = VQModel(**_a ).eval() vqvae.load_state_dict(_a ) lowerCamelCase__ = UNetLDMModel(**_a ).eval() unet.load_state_dict(_a ) lowerCamelCase__ = DDIMScheduler( timesteps=config.model.params.timesteps , beta_schedule='scaled_linear' , beta_start=config.model.params.linear_start , beta_end=config.model.params.linear_end , clip_sample=_a , ) lowerCamelCase__ = LDMPipeline(_a , _a , _a ) pipeline.save_pretrained(_a ) if __name__ == "__main__": _snake_case = argparse.ArgumentParser() parser.add_argument("--checkpoint_path", type=str, required=True) parser.add_argument("--config_path", type=str, required=True) parser.add_argument("--output_path", type=str, required=True) _snake_case = parser.parse_args() convert_ldm_original(args.checkpoint_path, args.config_path, args.output_path)
659
"""simple docstring""" import warnings from diffusers import StableDiffusionImgaImgPipeline # noqa F401 warnings.warn( "The `image_to_image.py` script is outdated. Please use directly `from diffusers import" " StableDiffusionImg2ImgPipeline` instead." )
659
1
"""simple docstring""" from ...utils import ( OptionalDependencyNotAvailable, is_torch_available, is_transformers_available, is_transformers_version, ) try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import ShapEPipeline else: from .camera import create_pan_cameras from .pipeline_shap_e import ShapEPipeline from .pipeline_shap_e_img2img import ShapEImgaImgPipeline from .renderer import ( BoundingBoxVolume, ImportanceRaySampler, MLPNeRFModelOutput, MLPNeRSTFModel, ShapEParamsProjModel, ShapERenderer, StratifiedRaySampler, VoidNeRFModel, )
659
"""simple docstring""" import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_funnel import FunnelTokenizer _snake_case = logging.get_logger(__name__) _snake_case = {"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"} _snake_case = [ "small", "small-base", "medium", "medium-base", "intermediate", "intermediate-base", "large", "large-base", "xlarge", "xlarge-base", ] _snake_case = { "vocab_file": { "funnel-transformer/small": "https://huggingface.co/funnel-transformer/small/resolve/main/vocab.txt", "funnel-transformer/small-base": "https://huggingface.co/funnel-transformer/small-base/resolve/main/vocab.txt", "funnel-transformer/medium": "https://huggingface.co/funnel-transformer/medium/resolve/main/vocab.txt", "funnel-transformer/medium-base": ( "https://huggingface.co/funnel-transformer/medium-base/resolve/main/vocab.txt" ), "funnel-transformer/intermediate": ( "https://huggingface.co/funnel-transformer/intermediate/resolve/main/vocab.txt" ), "funnel-transformer/intermediate-base": ( "https://huggingface.co/funnel-transformer/intermediate-base/resolve/main/vocab.txt" ), "funnel-transformer/large": "https://huggingface.co/funnel-transformer/large/resolve/main/vocab.txt", "funnel-transformer/large-base": "https://huggingface.co/funnel-transformer/large-base/resolve/main/vocab.txt", "funnel-transformer/xlarge": "https://huggingface.co/funnel-transformer/xlarge/resolve/main/vocab.txt", "funnel-transformer/xlarge-base": ( "https://huggingface.co/funnel-transformer/xlarge-base/resolve/main/vocab.txt" ), }, "tokenizer_file": { "funnel-transformer/small": "https://huggingface.co/funnel-transformer/small/resolve/main/tokenizer.json", "funnel-transformer/small-base": ( "https://huggingface.co/funnel-transformer/small-base/resolve/main/tokenizer.json" ), "funnel-transformer/medium": "https://huggingface.co/funnel-transformer/medium/resolve/main/tokenizer.json", "funnel-transformer/medium-base": ( "https://huggingface.co/funnel-transformer/medium-base/resolve/main/tokenizer.json" ), "funnel-transformer/intermediate": ( "https://huggingface.co/funnel-transformer/intermediate/resolve/main/tokenizer.json" ), "funnel-transformer/intermediate-base": ( "https://huggingface.co/funnel-transformer/intermediate-base/resolve/main/tokenizer.json" ), "funnel-transformer/large": "https://huggingface.co/funnel-transformer/large/resolve/main/tokenizer.json", "funnel-transformer/large-base": ( "https://huggingface.co/funnel-transformer/large-base/resolve/main/tokenizer.json" ), "funnel-transformer/xlarge": "https://huggingface.co/funnel-transformer/xlarge/resolve/main/tokenizer.json", "funnel-transformer/xlarge-base": ( "https://huggingface.co/funnel-transformer/xlarge-base/resolve/main/tokenizer.json" ), }, } _snake_case = {f"""funnel-transformer/{name}""": 512 for name in _model_names} _snake_case = {f"""funnel-transformer/{name}""": {"do_lower_case": True} for name in _model_names} class _a ( SCREAMING_SNAKE_CASE_ ): a_ : List[str] = VOCAB_FILES_NAMES a_ : List[str] = PRETRAINED_VOCAB_FILES_MAP a_ : Optional[int] = PRETRAINED_INIT_CONFIGURATION a_ : List[str] = FunnelTokenizer a_ : Dict = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES a_ : int = 2 def __init__( self : int , SCREAMING_SNAKE_CASE__ : Optional[int]=None , SCREAMING_SNAKE_CASE__ : Union[str, Any]=None , SCREAMING_SNAKE_CASE__ : int=True , SCREAMING_SNAKE_CASE__ : Any="<unk>" , SCREAMING_SNAKE_CASE__ : List[Any]="<sep>" , SCREAMING_SNAKE_CASE__ : int="<pad>" , SCREAMING_SNAKE_CASE__ : Tuple="<cls>" , SCREAMING_SNAKE_CASE__ : Tuple="<mask>" , SCREAMING_SNAKE_CASE__ : Any="<s>" , SCREAMING_SNAKE_CASE__ : Tuple="</s>" , SCREAMING_SNAKE_CASE__ : str=True , SCREAMING_SNAKE_CASE__ : Any=True , SCREAMING_SNAKE_CASE__ : str=None , SCREAMING_SNAKE_CASE__ : int="##" , **SCREAMING_SNAKE_CASE__ : Any , ): super().__init__( SCREAMING_SNAKE_CASE__ , tokenizer_file=SCREAMING_SNAKE_CASE__ , do_lower_case=SCREAMING_SNAKE_CASE__ , unk_token=SCREAMING_SNAKE_CASE__ , sep_token=SCREAMING_SNAKE_CASE__ , pad_token=SCREAMING_SNAKE_CASE__ , cls_token=SCREAMING_SNAKE_CASE__ , mask_token=SCREAMING_SNAKE_CASE__ , bos_token=SCREAMING_SNAKE_CASE__ , eos_token=SCREAMING_SNAKE_CASE__ , clean_text=SCREAMING_SNAKE_CASE__ , tokenize_chinese_chars=SCREAMING_SNAKE_CASE__ , strip_accents=SCREAMING_SNAKE_CASE__ , wordpieces_prefix=SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ , ) lowerCamelCase__ = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get('lowercase' , SCREAMING_SNAKE_CASE__ ) != do_lower_case or normalizer_state.get('strip_accents' , SCREAMING_SNAKE_CASE__ ) != strip_accents or normalizer_state.get('handle_chinese_chars' , SCREAMING_SNAKE_CASE__ ) != tokenize_chinese_chars ): lowerCamelCase__ = getattr(SCREAMING_SNAKE_CASE__ , normalizer_state.pop('type' ) ) lowerCamelCase__ = do_lower_case lowerCamelCase__ = strip_accents lowerCamelCase__ = tokenize_chinese_chars lowerCamelCase__ = normalizer_class(**SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = do_lower_case def _UpperCamelCase ( self : Dict , SCREAMING_SNAKE_CASE__ : Tuple , SCREAMING_SNAKE_CASE__ : Optional[Any]=None ): lowerCamelCase__ = [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 _UpperCamelCase ( self : Dict , SCREAMING_SNAKE_CASE__ : List[int] , SCREAMING_SNAKE_CASE__ : Optional[List[int]] = None ): lowerCamelCase__ = [self.sep_token_id] lowerCamelCase__ = [self.cls_token_id] if token_ids_a is None: return len(cls ) * [self.cls_token_type_id] + len(token_ids_a + sep ) * [0] return len(cls ) * [self.cls_token_type_id] + len(token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def _UpperCamelCase ( self : Optional[int] , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : Optional[str] = None ): lowerCamelCase__ = self._tokenizer.model.save(SCREAMING_SNAKE_CASE__ , name=SCREAMING_SNAKE_CASE__ ) return tuple(SCREAMING_SNAKE_CASE__ )
659
1
"""simple docstring""" from collections.abc import Iterable from typing import Generic, TypeVar _snake_case = TypeVar("_T") class _a ( Generic[_T] ): def __init__( self : int , SCREAMING_SNAKE_CASE__ : Iterable[_T] | None = None ): lowerCamelCase__ = list(iterable or [] ) lowerCamelCase__ = [] def __len__( self : Any ): return len(self._stacka ) + len(self._stacka ) def __repr__( self : str ): return F'Queue({tuple(self._stacka[::-1] + self._stacka )})' def _UpperCamelCase ( self : Dict , SCREAMING_SNAKE_CASE__ : _T ): self._stacka.append(SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : int ): lowerCamelCase__ = self._stacka.pop lowerCamelCase__ = self._stacka.append if not self._stacka: while self._stacka: stacka_append(stacka_pop() ) if not self._stacka: raise IndexError('Queue is empty' ) return self._stacka.pop() if __name__ == "__main__": from doctest import testmod testmod()
659
"""simple docstring""" # A Bipartite Graph is a graph whose vertices can be divided into two independent sets, # U and V such that every edge (u, v) either connects a vertex from U to V or a vertex # from V to U. In other words, for every edge (u, v), either u belongs to U and v to V, # or u belongs to V and v to U. We can also say that there is no edge that connects # vertices of same set. def snake_case ( _a: Optional[Any] )-> Union[str, Any]: '''simple docstring''' lowerCamelCase__ = [False] * len(_a ) lowerCamelCase__ = [-1] * len(_a ) def dfs(_a: Any , _a: Optional[int] ): lowerCamelCase__ = True lowerCamelCase__ = c for u in graph[v]: if not visited[u]: dfs(_a , 1 - c ) for i in range(len(_a ) ): if not visited[i]: dfs(_a , 0 ) for i in range(len(_a ) ): for j in graph[i]: if color[i] == color[j]: return False return True # Adjacency list of graph _snake_case = {0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 4: []} print(check_bipartite_dfs(graph))
659
1
"""simple docstring""" import json import os import re import unittest from transformers import CodeGenTokenizer, CodeGenTokenizerFast from transformers.models.codegen.tokenization_codegen import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class _a ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): a_ : Tuple = CodeGenTokenizer a_ : List[str] = CodeGenTokenizerFast a_ : Optional[int] = True a_ : Union[str, Any] = {'add_prefix_space': True} a_ : List[str] = False def _UpperCamelCase ( self : int ): super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt lowerCamelCase__ = [ 'l', 'o', 'w', 'e', 'r', 's', 't', 'i', 'd', 'n', '\u0120', '\u0120l', '\u0120n', '\u0120lo', '\u0120low', 'er', '\u0120lowest', '\u0120newer', '\u0120wider', '<unk>', '<|endoftext|>', ] lowerCamelCase__ = dict(zip(SCREAMING_SNAKE_CASE__ , range(len(SCREAMING_SNAKE_CASE__ ) ) ) ) lowerCamelCase__ = ['#version: 0.2', '\u0120 l', '\u0120l o', '\u0120lo w', 'e r', ''] lowerCamelCase__ = {'unk_token': '<unk>'} lowerCamelCase__ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['vocab_file'] ) lowerCamelCase__ = 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(SCREAMING_SNAKE_CASE__ ) + '\n' ) with open(self.merges_file , 'w' , encoding='utf-8' ) as fp: fp.write('\n'.join(SCREAMING_SNAKE_CASE__ ) ) def _UpperCamelCase ( self : Union[str, Any] , **SCREAMING_SNAKE_CASE__ : Union[str, Any] ): kwargs.update(self.special_tokens_map ) return CodeGenTokenizer.from_pretrained(self.tmpdirname , **SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : List[str] , **SCREAMING_SNAKE_CASE__ : str ): kwargs.update(self.special_tokens_map ) return CodeGenTokenizerFast.from_pretrained(self.tmpdirname , **SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : int , SCREAMING_SNAKE_CASE__ : List[str] ): lowerCamelCase__ = 'lower newer' lowerCamelCase__ = 'lower newer' return input_text, output_text def _UpperCamelCase ( self : Union[str, Any] ): lowerCamelCase__ = CodeGenTokenizer(self.vocab_file , self.merges_file , **self.special_tokens_map ) lowerCamelCase__ = 'lower newer' lowerCamelCase__ = ['\u0120low', 'er', '\u0120', 'n', 'e', 'w', 'er'] lowerCamelCase__ = tokenizer.tokenize(SCREAMING_SNAKE_CASE__ , add_prefix_space=SCREAMING_SNAKE_CASE__ ) self.assertListEqual(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = tokens + [tokenizer.unk_token] lowerCamelCase__ = [14, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(tokenizer.convert_tokens_to_ids(SCREAMING_SNAKE_CASE__ ) , SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : str ): if not self.test_rust_tokenizer: return lowerCamelCase__ = self.get_tokenizer() lowerCamelCase__ = self.get_rust_tokenizer(add_prefix_space=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = 'lower newer' # Testing tokenization lowerCamelCase__ = tokenizer.tokenize(SCREAMING_SNAKE_CASE__ , add_prefix_space=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = rust_tokenizer.tokenize(SCREAMING_SNAKE_CASE__ ) self.assertListEqual(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) # Testing conversion to ids without special tokens lowerCamelCase__ = tokenizer.encode(SCREAMING_SNAKE_CASE__ , add_special_tokens=SCREAMING_SNAKE_CASE__ , add_prefix_space=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = rust_tokenizer.encode(SCREAMING_SNAKE_CASE__ , add_special_tokens=SCREAMING_SNAKE_CASE__ ) self.assertListEqual(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) # Testing conversion to ids with special tokens lowerCamelCase__ = self.get_rust_tokenizer(add_prefix_space=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = tokenizer.encode(SCREAMING_SNAKE_CASE__ , add_prefix_space=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = rust_tokenizer.encode(SCREAMING_SNAKE_CASE__ ) self.assertListEqual(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) # Testing the unknown token lowerCamelCase__ = tokens + [rust_tokenizer.unk_token] lowerCamelCase__ = [14, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(rust_tokenizer.convert_tokens_to_ids(SCREAMING_SNAKE_CASE__ ) , SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : List[Any] , *SCREAMING_SNAKE_CASE__ : Dict , **SCREAMING_SNAKE_CASE__ : str ): # It's very difficult to mix/test pretokenization with byte-level # And get both CodeGen and Roberta to work at the same time (mostly an issue of adding a space before the string) pass def _UpperCamelCase ( self : Dict , SCREAMING_SNAKE_CASE__ : Union[str, Any]=15 ): 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__ ) # Simple input lowerCamelCase__ = 'This is a simple input' lowerCamelCase__ = ['This is a simple input 1', 'This is a simple input 2'] lowerCamelCase__ = ('This is a simple input', 'This is a pair') lowerCamelCase__ = [ ('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 self.assertRaises(SCREAMING_SNAKE_CASE__ , tokenizer_r.encode , SCREAMING_SNAKE_CASE__ , max_length=SCREAMING_SNAKE_CASE__ , padding='max_length' ) # Simple input self.assertRaises(SCREAMING_SNAKE_CASE__ , tokenizer_r.encode_plus , SCREAMING_SNAKE_CASE__ , max_length=SCREAMING_SNAKE_CASE__ , padding='max_length' ) # Simple input self.assertRaises( SCREAMING_SNAKE_CASE__ , tokenizer_r.batch_encode_plus , SCREAMING_SNAKE_CASE__ , max_length=SCREAMING_SNAKE_CASE__ , padding='max_length' , ) # Pair input self.assertRaises(SCREAMING_SNAKE_CASE__ , tokenizer_r.encode , SCREAMING_SNAKE_CASE__ , max_length=SCREAMING_SNAKE_CASE__ , padding='max_length' ) # Pair input self.assertRaises(SCREAMING_SNAKE_CASE__ , tokenizer_r.encode_plus , SCREAMING_SNAKE_CASE__ , max_length=SCREAMING_SNAKE_CASE__ , padding='max_length' ) # Pair input self.assertRaises( SCREAMING_SNAKE_CASE__ , tokenizer_r.batch_encode_plus , SCREAMING_SNAKE_CASE__ , max_length=SCREAMING_SNAKE_CASE__ , padding='max_length' , ) def _UpperCamelCase ( self : List[Any] ): lowerCamelCase__ = CodeGenTokenizer.from_pretrained(self.tmpdirname , pad_token='<pad>' ) # Simple input lowerCamelCase__ = 'This is a simple input' lowerCamelCase__ = ['This is a simple input looooooooong', 'This is a simple input'] lowerCamelCase__ = ('This is a simple input', 'This is a pair') lowerCamelCase__ = [ ('This is a simple input loooooong', 'This is a simple input'), ('This is a simple pair loooooong', 'This is a simple pair'), ] lowerCamelCase__ = tokenizer.pad_token_id lowerCamelCase__ = tokenizer(SCREAMING_SNAKE_CASE__ , padding='max_length' , max_length=30 , return_tensors='np' ) lowerCamelCase__ = tokenizer(SCREAMING_SNAKE_CASE__ , padding=SCREAMING_SNAKE_CASE__ , truncate=SCREAMING_SNAKE_CASE__ , return_tensors='np' ) lowerCamelCase__ = tokenizer(*SCREAMING_SNAKE_CASE__ , padding='max_length' , max_length=60 , return_tensors='np' ) lowerCamelCase__ = tokenizer(SCREAMING_SNAKE_CASE__ , padding=SCREAMING_SNAKE_CASE__ , truncate=SCREAMING_SNAKE_CASE__ , return_tensors='np' ) # s # test single string max_length padding self.assertEqual(out_s['input_ids'].shape[-1] , 30 ) self.assertTrue(pad_token_id in out_s['input_ids'] ) self.assertTrue(0 in out_s['attention_mask'] ) # s2 # test automatic padding self.assertEqual(out_sa['input_ids'].shape[-1] , 33 ) # long slice doesn't have padding self.assertFalse(pad_token_id in out_sa['input_ids'][0] ) self.assertFalse(0 in out_sa['attention_mask'][0] ) # short slice does have padding self.assertTrue(pad_token_id in out_sa['input_ids'][1] ) self.assertTrue(0 in out_sa['attention_mask'][1] ) # p # test single pair max_length padding self.assertEqual(out_p['input_ids'].shape[-1] , 60 ) self.assertTrue(pad_token_id in out_p['input_ids'] ) self.assertTrue(0 in out_p['attention_mask'] ) # p2 # test automatic padding pair self.assertEqual(out_pa['input_ids'].shape[-1] , 52 ) # long slice pair doesn't have padding self.assertFalse(pad_token_id in out_pa['input_ids'][0] ) self.assertFalse(0 in out_pa['attention_mask'][0] ) # short slice pair does have padding self.assertTrue(pad_token_id in out_pa['input_ids'][1] ) self.assertTrue(0 in out_pa['attention_mask'][1] ) def _UpperCamelCase ( self : int ): lowerCamelCase__ = '$$$' lowerCamelCase__ = CodeGenTokenizer.from_pretrained(self.tmpdirname , bos_token=SCREAMING_SNAKE_CASE__ , add_bos_token=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = 'This is a simple input' lowerCamelCase__ = ['This is a simple input 1', 'This is a simple input 2'] lowerCamelCase__ = tokenizer.bos_token_id lowerCamelCase__ = tokenizer(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = tokenizer(SCREAMING_SNAKE_CASE__ ) self.assertEqual(out_s.input_ids[0] , SCREAMING_SNAKE_CASE__ ) self.assertTrue(all(o[0] == bos_token_id for o in out_sa.input_ids ) ) lowerCamelCase__ = tokenizer.decode(out_s.input_ids ) lowerCamelCase__ = tokenizer.batch_decode(out_sa.input_ids ) self.assertEqual(decode_s.split()[0] , SCREAMING_SNAKE_CASE__ ) self.assertTrue(all(d.split()[0] == bos_token for d in decode_sa ) ) @slow def _UpperCamelCase ( self : Union[str, Any] ): lowerCamelCase__ = CodeGenTokenizer.from_pretrained('Salesforce/codegen-350M-mono' ) lowerCamelCase__ = '\nif len_a > len_b:\n result = a\nelse:\n result = b\n\n\n\n#' lowerCamelCase__ = '\nif len_a > len_b: result = a\nelse: result = b' lowerCamelCase__ = tokenizer.encode(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = ['^#', re.escape('<|endoftext|>' ), '^\'\'\'', '^"""', '\n\n\n'] lowerCamelCase__ = tokenizer.decode(SCREAMING_SNAKE_CASE__ , truncate_before_pattern=SCREAMING_SNAKE_CASE__ ) self.assertEqual(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : Optional[int] ): pass
659
"""simple docstring""" from collections.abc import Iterator, MutableMapping from dataclasses import dataclass from typing import Generic, TypeVar _snake_case = TypeVar("KEY") _snake_case = TypeVar("VAL") @dataclass(frozen=SCREAMING_SNAKE_CASE_ , slots=SCREAMING_SNAKE_CASE_ ) class _a ( Generic[KEY, VAL] ): a_ : KEY a_ : VAL class _a ( _Item ): def __init__( self : List[str] ): super().__init__(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) def __bool__( self : str ): return False _snake_case = _DeletedItem() class _a ( MutableMapping[KEY, VAL] ): def __init__( self : Optional[Any] , SCREAMING_SNAKE_CASE__ : int = 8 , SCREAMING_SNAKE_CASE__ : float = 0.75 ): lowerCamelCase__ = initial_block_size lowerCamelCase__ = [None] * initial_block_size assert 0.0 < capacity_factor < 1.0 lowerCamelCase__ = capacity_factor lowerCamelCase__ = 0 def _UpperCamelCase ( self : Optional[int] , SCREAMING_SNAKE_CASE__ : KEY ): return hash(SCREAMING_SNAKE_CASE__ ) % len(self._buckets ) def _UpperCamelCase ( self : str , SCREAMING_SNAKE_CASE__ : int ): return (ind + 1) % len(self._buckets ) def _UpperCamelCase ( self : Union[str, Any] , SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : KEY , SCREAMING_SNAKE_CASE__ : VAL ): lowerCamelCase__ = self._buckets[ind] if not stored: lowerCamelCase__ = _Item(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) self._len += 1 return True elif stored.key == key: lowerCamelCase__ = _Item(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) return True else: return False def _UpperCamelCase ( self : Dict ): lowerCamelCase__ = len(self._buckets ) * self._capacity_factor return len(self ) >= int(SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : int ): if len(self._buckets ) <= self._initial_block_size: return False lowerCamelCase__ = len(self._buckets ) * self._capacity_factor / 2 return len(self ) < limit def _UpperCamelCase ( self : List[str] , SCREAMING_SNAKE_CASE__ : int ): lowerCamelCase__ = self._buckets lowerCamelCase__ = [None] * new_size lowerCamelCase__ = 0 for item in old_buckets: if item: self._add_item(item.key , item.val ) def _UpperCamelCase ( self : List[str] ): self._resize(len(self._buckets ) * 2 ) def _UpperCamelCase ( self : Optional[int] ): self._resize(len(self._buckets ) // 2 ) def _UpperCamelCase ( self : Any , SCREAMING_SNAKE_CASE__ : KEY ): lowerCamelCase__ = self._get_bucket_index(SCREAMING_SNAKE_CASE__ ) for _ in range(len(self._buckets ) ): yield ind lowerCamelCase__ = self._get_next_ind(SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : Optional[Any] , SCREAMING_SNAKE_CASE__ : KEY , SCREAMING_SNAKE_CASE__ : VAL ): for ind in self._iterate_buckets(SCREAMING_SNAKE_CASE__ ): if self._try_set(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): break def __setitem__( self : Dict , SCREAMING_SNAKE_CASE__ : KEY , SCREAMING_SNAKE_CASE__ : VAL ): if self._is_full(): self._size_up() self._add_item(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) def __delitem__( self : Dict , SCREAMING_SNAKE_CASE__ : KEY ): for ind in self._iterate_buckets(SCREAMING_SNAKE_CASE__ ): lowerCamelCase__ = self._buckets[ind] if item is None: raise KeyError(SCREAMING_SNAKE_CASE__ ) if item is _deleted: continue if item.key == key: lowerCamelCase__ = _deleted self._len -= 1 break if self._is_sparse(): self._size_down() def __getitem__( self : str , SCREAMING_SNAKE_CASE__ : KEY ): for ind in self._iterate_buckets(SCREAMING_SNAKE_CASE__ ): lowerCamelCase__ = self._buckets[ind] if item is None: break if item is _deleted: continue if item.key == key: return item.val raise KeyError(SCREAMING_SNAKE_CASE__ ) def __len__( self : List[Any] ): return self._len def __iter__( self : Optional[int] ): yield from (item.key for item in self._buckets if item) def __repr__( self : str ): lowerCamelCase__ = ' ,'.join( F'{item.key}: {item.val}' for item in self._buckets if item ) return F'HashMap({val_string})'
659
1
"""simple docstring""" import os import warnings from typing import List, Optional from ...tokenization_utils_base import BatchEncoding from ...utils import logging from .configuration_rag import RagConfig _snake_case = logging.get_logger(__name__) class _a : def __init__( self : Union[str, Any] , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : Any ): lowerCamelCase__ = question_encoder lowerCamelCase__ = generator lowerCamelCase__ = self.question_encoder def _UpperCamelCase ( self : Union[str, Any] , SCREAMING_SNAKE_CASE__ : Optional[int] ): if os.path.isfile(SCREAMING_SNAKE_CASE__ ): raise ValueError(F'Provided path ({save_directory}) should be a directory, not a file' ) os.makedirs(SCREAMING_SNAKE_CASE__ , exist_ok=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = os.path.join(SCREAMING_SNAKE_CASE__ , 'question_encoder_tokenizer' ) lowerCamelCase__ = os.path.join(SCREAMING_SNAKE_CASE__ , 'generator_tokenizer' ) self.question_encoder.save_pretrained(SCREAMING_SNAKE_CASE__ ) self.generator.save_pretrained(SCREAMING_SNAKE_CASE__ ) @classmethod def _UpperCamelCase ( cls : Dict , SCREAMING_SNAKE_CASE__ : Dict , **SCREAMING_SNAKE_CASE__ : Optional[Any] ): # dynamically import AutoTokenizer from ..auto.tokenization_auto import AutoTokenizer lowerCamelCase__ = kwargs.pop('config' , SCREAMING_SNAKE_CASE__ ) if config is None: lowerCamelCase__ = RagConfig.from_pretrained(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = AutoTokenizer.from_pretrained( SCREAMING_SNAKE_CASE__ , config=config.question_encoder , subfolder='question_encoder_tokenizer' ) lowerCamelCase__ = AutoTokenizer.from_pretrained( SCREAMING_SNAKE_CASE__ , config=config.generator , subfolder='generator_tokenizer' ) return cls(question_encoder=SCREAMING_SNAKE_CASE__ , generator=SCREAMING_SNAKE_CASE__ ) def __call__( self : List[Any] , *SCREAMING_SNAKE_CASE__ : List[str] , **SCREAMING_SNAKE_CASE__ : Optional[Any] ): return self.current_tokenizer(*SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : List[str] , *SCREAMING_SNAKE_CASE__ : Tuple , **SCREAMING_SNAKE_CASE__ : int ): return self.generator.batch_decode(*SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : List[str] , *SCREAMING_SNAKE_CASE__ : List[str] , **SCREAMING_SNAKE_CASE__ : Tuple ): return self.generator.decode(*SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : Dict ): lowerCamelCase__ = self.question_encoder def _UpperCamelCase ( self : Optional[Any] ): lowerCamelCase__ = self.generator def _UpperCamelCase ( self : str , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : Optional[List[str]] = None , SCREAMING_SNAKE_CASE__ : Optional[int] = None , SCREAMING_SNAKE_CASE__ : Optional[int] = None , SCREAMING_SNAKE_CASE__ : str = "longest" , SCREAMING_SNAKE_CASE__ : str = None , SCREAMING_SNAKE_CASE__ : bool = True , **SCREAMING_SNAKE_CASE__ : Optional[int] , ): warnings.warn( '`prepare_seq2seq_batch` is deprecated and will be removed in version 5 of 🤗 Transformers. Use the ' 'regular `__call__` method to prepare your inputs and the tokenizer under the `with_target_tokenizer` ' 'context manager to prepare your targets. See the documentation of your specific tokenizer for more ' 'details' , SCREAMING_SNAKE_CASE__ , ) if max_length is None: lowerCamelCase__ = self.current_tokenizer.model_max_length lowerCamelCase__ = self( SCREAMING_SNAKE_CASE__ , add_special_tokens=SCREAMING_SNAKE_CASE__ , return_tensors=SCREAMING_SNAKE_CASE__ , max_length=SCREAMING_SNAKE_CASE__ , padding=SCREAMING_SNAKE_CASE__ , truncation=SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ , ) if tgt_texts is None: return model_inputs # Process tgt_texts if max_target_length is None: lowerCamelCase__ = self.current_tokenizer.model_max_length lowerCamelCase__ = self( text_target=SCREAMING_SNAKE_CASE__ , add_special_tokens=SCREAMING_SNAKE_CASE__ , return_tensors=SCREAMING_SNAKE_CASE__ , padding=SCREAMING_SNAKE_CASE__ , max_length=SCREAMING_SNAKE_CASE__ , truncation=SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ , ) lowerCamelCase__ = labels['input_ids'] return model_inputs
659
"""simple docstring""" def snake_case ( _a: int , _a: list[int] , _a: int )-> int: '''simple docstring''' def count_of_possible_combinations(_a: int ) -> int: if target < 0: return 0 if target == 0: return 1 return sum(count_of_possible_combinations(target - item ) for item in array ) return count_of_possible_combinations(_a ) def snake_case ( _a: int , _a: list[int] , _a: int )-> int: '''simple docstring''' def count_of_possible_combinations_with_dp_array( _a: int , _a: list[int] ) -> int: if target < 0: return 0 if target == 0: return 1 if dp_array[target] != -1: return dp_array[target] lowerCamelCase__ = sum( count_of_possible_combinations_with_dp_array(target - item , _a ) for item in array ) lowerCamelCase__ = answer return answer lowerCamelCase__ = [-1] * (target + 1) return count_of_possible_combinations_with_dp_array(_a , _a ) def snake_case ( _a: int , _a: list[int] , _a: int )-> int: '''simple docstring''' lowerCamelCase__ = [0] * (target + 1) lowerCamelCase__ = 1 for i in range(1 , target + 1 ): for j in range(_a ): if i - array[j] >= 0: dp_array[i] += dp_array[i - array[j]] return dp_array[target] if __name__ == "__main__": import doctest doctest.testmod() _snake_case = 3 _snake_case = 5 _snake_case = [1, 2, 5] print(combination_sum_iv(n, array, target))
659
1
"""simple docstring""" import argparse import os import re _snake_case = "src/diffusers" # Pattern that looks at the indentation in a line. _snake_case = re.compile(R"^(\s*)\S") # Pattern that matches `"key":" and puts `key` in group 0. _snake_case = re.compile(R"^\s*\"([^\"]+)\":") # Pattern that matches `_import_structure["key"]` and puts `key` in group 0. _snake_case = re.compile(R"^\s*_import_structure\[\"([^\"]+)\"\]") # Pattern that matches `"key",` and puts `key` in group 0. _snake_case = re.compile(R"^\s*\"([^\"]+)\",\s*$") # Pattern that matches any `[stuff]` and puts `stuff` in group 0. _snake_case = re.compile(R"\[([^\]]+)\]") def snake_case ( _a: str )-> Tuple: '''simple docstring''' lowerCamelCase__ = _re_indent.search(_a ) return "" if search is None else search.groups()[0] def snake_case ( _a: Tuple , _a: List[str]="" , _a: str=None , _a: Optional[Any]=None )-> int: '''simple docstring''' lowerCamelCase__ = 0 lowerCamelCase__ = code.split('\n' ) if start_prompt is not None: while not lines[index].startswith(_a ): index += 1 lowerCamelCase__ = ['\n'.join(lines[:index] )] else: lowerCamelCase__ = [] # We split into blocks until we get to the `end_prompt` (or the end of the block). lowerCamelCase__ = [lines[index]] index += 1 while index < len(_a ) and (end_prompt is None or not lines[index].startswith(_a )): if len(lines[index] ) > 0 and get_indent(lines[index] ) == indent_level: if len(_a ) > 0 and get_indent(current_block[-1] ).startswith(indent_level + ' ' ): current_block.append(lines[index] ) blocks.append('\n'.join(_a ) ) if index < len(_a ) - 1: lowerCamelCase__ = [lines[index + 1]] index += 1 else: lowerCamelCase__ = [] else: blocks.append('\n'.join(_a ) ) lowerCamelCase__ = [lines[index]] else: current_block.append(lines[index] ) index += 1 # Adds current block if it's nonempty. if len(_a ) > 0: blocks.append('\n'.join(_a ) ) # Add final block after end_prompt if provided. if end_prompt is not None and index < len(_a ): blocks.append('\n'.join(lines[index:] ) ) return blocks def snake_case ( _a: Union[str, Any] )-> int: '''simple docstring''' def _inner(_a: Union[str, Any] ): return key(_a ).lower().replace('_' , '' ) return _inner def snake_case ( _a: Dict , _a: int=None )-> Dict: '''simple docstring''' def noop(_a: List[Any] ): return x if key is None: lowerCamelCase__ = noop # Constants are all uppercase, they go first. lowerCamelCase__ = [obj for obj in objects if key(_a ).isupper()] # Classes are not all uppercase but start with a capital, they go second. lowerCamelCase__ = [obj for obj in objects if key(_a )[0].isupper() and not key(_a ).isupper()] # Functions begin with a lowercase, they go last. lowerCamelCase__ = [obj for obj in objects if not key(_a )[0].isupper()] lowerCamelCase__ = ignore_underscore(_a ) return sorted(_a , key=_a ) + sorted(_a , key=_a ) + sorted(_a , key=_a ) def snake_case ( _a: str )-> List[str]: '''simple docstring''' def _replace(_a: List[Any] ): lowerCamelCase__ = match.groups()[0] if "," not in imports: return F'[{imports}]' lowerCamelCase__ = [part.strip().replace('"' , '' ) for part in imports.split(',' )] # We will have a final empty element if the line finished with a comma. if len(keys[-1] ) == 0: lowerCamelCase__ = keys[:-1] return "[" + ", ".join([F'"{k}"' for k in sort_objects(_a )] ) + "]" lowerCamelCase__ = import_statement.split('\n' ) if len(_a ) > 3: # Here we have to sort internal imports that are on several lines (one per name): # key: [ # "object1", # "object2", # ... # ] # We may have to ignore one or two lines on each side. lowerCamelCase__ = 2 if lines[1].strip() == '[' else 1 lowerCamelCase__ = [(i, _re_strip_line.search(_a ).groups()[0]) for i, line in enumerate(lines[idx:-idx] )] lowerCamelCase__ = sort_objects(_a , key=lambda _a : x[1] ) lowerCamelCase__ = [lines[x[0] + idx] for x in sorted_indices] return "\n".join(lines[:idx] + sorted_lines + lines[-idx:] ) elif len(_a ) == 3: # Here we have to sort internal imports that are on one separate line: # key: [ # "object1", "object2", ... # ] if _re_bracket_content.search(lines[1] ) is not None: lowerCamelCase__ = _re_bracket_content.sub(_replace , lines[1] ) else: lowerCamelCase__ = [part.strip().replace('"' , '' ) for part in lines[1].split(',' )] # We will have a final empty element if the line finished with a comma. if len(keys[-1] ) == 0: lowerCamelCase__ = keys[:-1] lowerCamelCase__ = get_indent(lines[1] ) + ', '.join([F'"{k}"' for k in sort_objects(_a )] ) return "\n".join(_a ) else: # Finally we have to deal with imports fitting on one line lowerCamelCase__ = _re_bracket_content.sub(_replace , _a ) return import_statement def snake_case ( _a: List[Any] , _a: Any=True )-> List[str]: '''simple docstring''' with open(_a , 'r' ) as f: lowerCamelCase__ = f.read() if "_import_structure" not in code: return # Blocks of indent level 0 lowerCamelCase__ = split_code_in_indented_blocks( _a , start_prompt='_import_structure = {' , end_prompt='if TYPE_CHECKING:' ) # We ignore block 0 (everything until start_prompt) and the last block (everything after end_prompt). for block_idx in range(1 , len(_a ) - 1 ): # Check if the block contains some `_import_structure`s thingy to sort. lowerCamelCase__ = main_blocks[block_idx] lowerCamelCase__ = block.split('\n' ) # Get to the start of the imports. lowerCamelCase__ = 0 while line_idx < len(_a ) and "_import_structure" not in block_lines[line_idx]: # Skip dummy import blocks if "import dummy" in block_lines[line_idx]: lowerCamelCase__ = len(_a ) else: line_idx += 1 if line_idx >= len(_a ): continue # Ignore beginning and last line: they don't contain anything. lowerCamelCase__ = '\n'.join(block_lines[line_idx:-1] ) lowerCamelCase__ = get_indent(block_lines[1] ) # Slit the internal block into blocks of indent level 1. lowerCamelCase__ = split_code_in_indented_blocks(_a , indent_level=_a ) # We have two categories of import key: list or _import_structure[key].append/extend lowerCamelCase__ = _re_direct_key if '_import_structure' in block_lines[0] else _re_indirect_key # Grab the keys, but there is a trap: some lines are empty or just comments. lowerCamelCase__ = [(pattern.search(_a ).groups()[0] if pattern.search(_a ) is not None else None) for b in internal_blocks] # We only sort the lines with a key. lowerCamelCase__ = [(i, key) for i, key in enumerate(_a ) if key is not None] lowerCamelCase__ = [x[0] for x in sorted(_a , key=lambda _a : x[1] )] # We reorder the blocks by leaving empty lines/comments as they were and reorder the rest. lowerCamelCase__ = 0 lowerCamelCase__ = [] for i in range(len(_a ) ): if keys[i] is None: reordered_blocks.append(internal_blocks[i] ) else: lowerCamelCase__ = sort_objects_in_import(internal_blocks[sorted_indices[count]] ) reordered_blocks.append(_a ) count += 1 # And we put our main block back together with its first and last line. lowerCamelCase__ = '\n'.join(block_lines[:line_idx] + reordered_blocks + [block_lines[-1]] ) if code != "\n".join(_a ): if check_only: return True else: print(F'Overwriting {file}.' ) with open(_a , 'w' ) as f: f.write('\n'.join(_a ) ) def snake_case ( _a: List[str]=True )-> Optional[int]: '''simple docstring''' lowerCamelCase__ = [] for root, _, files in os.walk(_a ): if "__init__.py" in files: lowerCamelCase__ = sort_imports(os.path.join(_a , '__init__.py' ) , check_only=_a ) if result: lowerCamelCase__ = [os.path.join(_a , '__init__.py' )] if len(_a ) > 0: raise ValueError(F'Would overwrite {len(_a )} files, run `make style`.' ) if __name__ == "__main__": _snake_case = argparse.ArgumentParser() parser.add_argument("--check_only", action="store_true", help="Whether to only check or fix style.") _snake_case = parser.parse_args() sort_imports_in_all_inits(check_only=args.check_only)
659
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available _snake_case = { "configuration_m2m_100": ["M2M_100_PRETRAINED_CONFIG_ARCHIVE_MAP", "M2M100Config", "M2M100OnnxConfig"], "tokenization_m2m_100": ["M2M100Tokenizer"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = [ "M2M_100_PRETRAINED_MODEL_ARCHIVE_LIST", "M2M100ForConditionalGeneration", "M2M100Model", "M2M100PreTrainedModel", ] if TYPE_CHECKING: from .configuration_mam_aaa import M2M_100_PRETRAINED_CONFIG_ARCHIVE_MAP, MaMaaaConfig, MaMaaaOnnxConfig from .tokenization_mam_aaa import MaMaaaTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mam_aaa import ( M2M_100_PRETRAINED_MODEL_ARCHIVE_LIST, MaMaaaForConditionalGeneration, MaMaaaModel, MaMaaaPreTrainedModel, ) else: import sys _snake_case = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
659
1
"""simple docstring""" from __future__ import annotations import bisect def snake_case ( _a: list[int] , _a: int , _a: int = 0 , _a: int = -1 )-> int: '''simple docstring''' if hi < 0: lowerCamelCase__ = len(_a ) while lo < hi: lowerCamelCase__ = lo + (hi - lo) // 2 if sorted_collection[mid] < item: lowerCamelCase__ = mid + 1 else: lowerCamelCase__ = mid return lo def snake_case ( _a: list[int] , _a: int , _a: int = 0 , _a: int = -1 )-> int: '''simple docstring''' if hi < 0: lowerCamelCase__ = len(_a ) while lo < hi: lowerCamelCase__ = lo + (hi - lo) // 2 if sorted_collection[mid] <= item: lowerCamelCase__ = mid + 1 else: lowerCamelCase__ = mid return lo def snake_case ( _a: list[int] , _a: int , _a: int = 0 , _a: int = -1 )-> None: '''simple docstring''' sorted_collection.insert(bisect_left(_a , _a , _a , _a ) , _a ) def snake_case ( _a: list[int] , _a: int , _a: int = 0 , _a: int = -1 )-> None: '''simple docstring''' sorted_collection.insert(bisect_right(_a , _a , _a , _a ) , _a ) def snake_case ( _a: list[int] , _a: int )-> int | None: '''simple docstring''' lowerCamelCase__ = 0 lowerCamelCase__ = len(_a ) - 1 while left <= right: lowerCamelCase__ = left + (right - left) // 2 lowerCamelCase__ = sorted_collection[midpoint] if current_item == item: return midpoint elif item < current_item: lowerCamelCase__ = midpoint - 1 else: lowerCamelCase__ = midpoint + 1 return None def snake_case ( _a: list[int] , _a: int )-> int | None: '''simple docstring''' lowerCamelCase__ = bisect.bisect_left(_a , _a ) if index != len(_a ) and sorted_collection[index] == item: return index return None def snake_case ( _a: list[int] , _a: int , _a: int , _a: int )-> int | None: '''simple docstring''' if right < left: return None lowerCamelCase__ = left + (right - left) // 2 if sorted_collection[midpoint] == item: return midpoint elif sorted_collection[midpoint] > item: return binary_search_by_recursion(_a , _a , _a , midpoint - 1 ) else: return binary_search_by_recursion(_a , _a , midpoint + 1 , _a ) if __name__ == "__main__": _snake_case = input("Enter numbers separated by comma:\n").strip() _snake_case = sorted(int(item) for item in user_input.split(",")) _snake_case = int(input("Enter a single number to be found in the list:\n")) _snake_case = binary_search(collection, target) if result is None: print(f"""{target} was not found in {collection}.""") else: print(f"""{target} was found at position {result} in {collection}.""")
659
"""simple docstring""" def snake_case ( _a: list[list[float]] )-> list[list[float]]: '''simple docstring''' lowerCamelCase__ = [] for data in source_data: for i, el in enumerate(_a ): if len(_a ) < i + 1: data_lists.append([] ) data_lists[i].append(float(_a ) ) return data_lists def snake_case ( _a: list[list[float]] , _a: list[int] )-> list[list[float]]: '''simple docstring''' lowerCamelCase__ = [] for dlist, weight in zip(_a , _a ): lowerCamelCase__ = min(_a ) lowerCamelCase__ = max(_a ) lowerCamelCase__ = [] # for weight 0 score is 1 - actual score if weight == 0: for item in dlist: try: score.append(1 - ((item - mind) / (maxd - mind)) ) except ZeroDivisionError: score.append(1 ) elif weight == 1: for item in dlist: try: score.append((item - mind) / (maxd - mind) ) except ZeroDivisionError: score.append(0 ) # weight not 0 or 1 else: lowerCamelCase__ = F'Invalid weight of {weight:f} provided' raise ValueError(_a ) score_lists.append(_a ) return score_lists def snake_case ( _a: list[list[float]] )-> list[float]: '''simple docstring''' lowerCamelCase__ = [0 for i in range(len(score_lists[0] ) )] for slist in score_lists: for j, ele in enumerate(_a ): lowerCamelCase__ = final_scores[j] + ele return final_scores def snake_case ( _a: list[list[float]] , _a: list[int] )-> list[list[float]]: '''simple docstring''' lowerCamelCase__ = get_data(_a ) lowerCamelCase__ = calculate_each_score(_a , _a ) lowerCamelCase__ = generate_final_scores(_a ) # append scores to source data for i, ele in enumerate(_a ): source_data[i].append(_a ) return source_data
659
1
"""simple docstring""" from __future__ import annotations _snake_case = [ [-1, 0], # left [0, -1], # down [1, 0], # right [0, 1], # up ] def snake_case ( _a: list[list[int]] , _a: list[int] , _a: list[int] , _a: int , _a: list[list[int]] , )-> tuple[list[list[int]], list[list[int]]]: '''simple docstring''' lowerCamelCase__ = [ [0 for col in range(len(grid[0] ) )] for row in range(len(_a ) ) ] # the reference grid lowerCamelCase__ = 1 lowerCamelCase__ = [ [0 for col in range(len(grid[0] ) )] for row in range(len(_a ) ) ] # the action grid lowerCamelCase__ = init[0] lowerCamelCase__ = init[1] lowerCamelCase__ = 0 lowerCamelCase__ = g + heuristic[x][y] # cost from starting cell to destination cell lowerCamelCase__ = [[f, g, x, y]] lowerCamelCase__ = False # flag that is set when search is complete lowerCamelCase__ = False # flag set if we can't find expand while not found and not resign: if len(_a ) == 0: raise ValueError('Algorithm is unable to find solution' ) else: # to choose the least costliest action so as to move closer to the goal cell.sort() cell.reverse() lowerCamelCase__ = cell.pop() lowerCamelCase__ = next_cell[2] lowerCamelCase__ = next_cell[3] lowerCamelCase__ = next_cell[1] if x == goal[0] and y == goal[1]: lowerCamelCase__ = True else: for i in range(len(_a ) ): # to try out different valid actions lowerCamelCase__ = x + DIRECTIONS[i][0] lowerCamelCase__ = y + DIRECTIONS[i][1] if xa >= 0 and xa < len(_a ) and ya >= 0 and ya < len(grid[0] ): if closed[xa][ya] == 0 and grid[xa][ya] == 0: lowerCamelCase__ = g + cost lowerCamelCase__ = ga + heuristic[xa][ya] cell.append([fa, ga, xa, ya] ) lowerCamelCase__ = 1 lowerCamelCase__ = i lowerCamelCase__ = [] lowerCamelCase__ = goal[0] lowerCamelCase__ = goal[1] invpath.append([x, y] ) # we get the reverse path from here while x != init[0] or y != init[1]: lowerCamelCase__ = x - DIRECTIONS[action[x][y]][0] lowerCamelCase__ = y - DIRECTIONS[action[x][y]][1] lowerCamelCase__ = xa lowerCamelCase__ = ya invpath.append([x, y] ) lowerCamelCase__ = [] for i in range(len(_a ) ): path.append(invpath[len(_a ) - 1 - i] ) return path, action if __name__ == "__main__": _snake_case = [ [0, 1, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles [0, 1, 0, 0, 0, 0], [0, 1, 0, 0, 1, 0], [0, 0, 0, 0, 1, 0], ] _snake_case = [0, 0] # all coordinates are given in format [y,x] _snake_case = [len(grid) - 1, len(grid[0]) - 1] _snake_case = 1 # the cost map which pushes the path closer to the goal _snake_case = [[0 for row in range(len(grid[0]))] for col in range(len(grid))] for i in range(len(grid)): for j in range(len(grid[0])): _snake_case = abs(i - goal[0]) + abs(j - goal[1]) if grid[i][j] == 1: # added extra penalty in the heuristic map _snake_case = 99 _snake_case , _snake_case = search(grid, init, goal, cost, heuristic) print("ACTION MAP") for i in range(len(action)): print(action[i]) for i in range(len(path)): print(path[i])
659
"""simple docstring""" from __future__ import annotations from math import gcd def snake_case ( _a: int , _a: int = 2 , _a: int = 1 , _a: int = 3 , )-> int | None: '''simple docstring''' if num < 2: raise ValueError('The input value cannot be less than 2' ) # Because of the relationship between ``f(f(x))`` and ``f(x)``, this # algorithm struggles to find factors that are divisible by two. # As a workaround, we specifically check for two and even inputs. # See: https://math.stackexchange.com/a/2856214/165820 if num > 2 and num % 2 == 0: return 2 # Pollard's Rho algorithm requires a function that returns pseudorandom # values between 0 <= X < ``num``. It doesn't need to be random in the # sense that the output value is cryptographically secure or difficult # to calculate, it only needs to be random in the sense that all output # values should be equally likely to appear. # For this reason, Pollard suggested using ``f(x) = (x**2 - 1) % num`` # However, the success of Pollard's algorithm isn't guaranteed and is # determined in part by the initial seed and the chosen random function. # To make retries easier, we will instead use ``f(x) = (x**2 + C) % num`` # where ``C`` is a value that we can modify between each attempt. def rand_fn(_a: int , _a: int , _a: int ) -> int: return (pow(_a , 2 ) + step) % modulus for _ in range(_a ): # These track the position within the cycle detection logic. lowerCamelCase__ = seed lowerCamelCase__ = seed while True: # At each iteration, the tortoise moves one step and the hare moves two. lowerCamelCase__ = rand_fn(_a , _a , _a ) lowerCamelCase__ = rand_fn(_a , _a , _a ) lowerCamelCase__ = rand_fn(_a , _a , _a ) # At some point both the tortoise and the hare will enter a cycle whose # length ``p`` is a divisor of ``num``. Once in that cycle, at some point # the tortoise and hare will end up on the same value modulo ``p``. # We can detect when this happens because the position difference between # the tortoise and the hare will share a common divisor with ``num``. lowerCamelCase__ = gcd(hare - tortoise , _a ) if divisor == 1: # No common divisor yet, just keep searching. continue else: # We found a common divisor! if divisor == num: # Unfortunately, the divisor is ``num`` itself and is useless. break else: # The divisor is a nontrivial factor of ``num``! return divisor # If we made it here, then this attempt failed. # We need to pick a new starting seed for the tortoise and hare # in addition to a new step value for the random function. # To keep this example implementation deterministic, the # new values will be generated based on currently available # values instead of using something like ``random.randint``. # We can use the hare's position as the new seed. # This is actually what Richard Brent's the "optimized" variant does. lowerCamelCase__ = hare # The new step value for the random function can just be incremented. # At first the results will be similar to what the old function would # have produced, but the value will quickly diverge after a bit. step += 1 # We haven't found a divisor within the requested number of attempts. # We were unlucky or ``num`` itself is actually prime. return None if __name__ == "__main__": import argparse _snake_case = argparse.ArgumentParser() parser.add_argument( "num", type=int, help="The value to find a divisor of", ) parser.add_argument( "--attempts", type=int, default=3, help="The number of attempts before giving up", ) _snake_case = parser.parse_args() _snake_case = pollard_rho(args.num, attempts=args.attempts) if divisor is None: print(f"""{args.num} is probably prime""") else: _snake_case = args.num // divisor print(f"""{args.num} = {divisor} * {quotient}""")
659
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_tokenizers_available, is_torch_available, ) _snake_case = { "configuration_convbert": ["CONVBERT_PRETRAINED_CONFIG_ARCHIVE_MAP", "ConvBertConfig", "ConvBertOnnxConfig"], "tokenization_convbert": ["ConvBertTokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = ["ConvBertTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = [ "CONVBERT_PRETRAINED_MODEL_ARCHIVE_LIST", "ConvBertForMaskedLM", "ConvBertForMultipleChoice", "ConvBertForQuestionAnswering", "ConvBertForSequenceClassification", "ConvBertForTokenClassification", "ConvBertLayer", "ConvBertModel", "ConvBertPreTrainedModel", "load_tf_weights_in_convbert", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = [ "TF_CONVBERT_PRETRAINED_MODEL_ARCHIVE_LIST", "TFConvBertForMaskedLM", "TFConvBertForMultipleChoice", "TFConvBertForQuestionAnswering", "TFConvBertForSequenceClassification", "TFConvBertForTokenClassification", "TFConvBertLayer", "TFConvBertModel", "TFConvBertPreTrainedModel", ] if TYPE_CHECKING: from .configuration_convbert import CONVBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ConvBertConfig, ConvBertOnnxConfig from .tokenization_convbert import ConvBertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_convbert_fast import ConvBertTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_convbert import ( CONVBERT_PRETRAINED_MODEL_ARCHIVE_LIST, ConvBertForMaskedLM, ConvBertForMultipleChoice, ConvBertForQuestionAnswering, ConvBertForSequenceClassification, ConvBertForTokenClassification, ConvBertLayer, ConvBertModel, ConvBertPreTrainedModel, load_tf_weights_in_convbert, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_convbert import ( TF_CONVBERT_PRETRAINED_MODEL_ARCHIVE_LIST, TFConvBertForMaskedLM, TFConvBertForMultipleChoice, TFConvBertForQuestionAnswering, TFConvBertForSequenceClassification, TFConvBertForTokenClassification, TFConvBertLayer, TFConvBertModel, TFConvBertPreTrainedModel, ) else: import sys _snake_case = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
659
"""simple docstring""" from ...utils import ( OptionalDependencyNotAvailable, is_torch_available, is_transformers_available, is_transformers_version, ) try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import ShapEPipeline else: from .camera import create_pan_cameras from .pipeline_shap_e import ShapEPipeline from .pipeline_shap_e_img2img import ShapEImgaImgPipeline from .renderer import ( BoundingBoxVolume, ImportanceRaySampler, MLPNeRFModelOutput, MLPNeRSTFModel, ShapEParamsProjModel, ShapERenderer, StratifiedRaySampler, VoidNeRFModel, )
659
1
"""simple docstring""" import shutil import tempfile import unittest from transformers import SPIECE_UNDERLINE, BatchEncoding, MBartaaTokenizer, MBartaaTokenizerFast, is_torch_available from transformers.testing_utils import ( get_tests_dir, nested_simplify, require_sentencepiece, require_tokenizers, require_torch, slow, ) from ...test_tokenization_common import TokenizerTesterMixin _snake_case = get_tests_dir("fixtures/test_sentencepiece.model") if is_torch_available(): from transformers.models.mbart.modeling_mbart import shift_tokens_right _snake_case = 25_0004 _snake_case = 25_0020 @require_sentencepiece @require_tokenizers class _a ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): a_ : Dict = MBartaaTokenizer a_ : Union[str, Any] = MBartaaTokenizerFast a_ : List[Any] = True a_ : List[Any] = True def _UpperCamelCase ( self : Optional[int] ): super().setUp() # We have a SentencePiece fixture for testing lowerCamelCase__ = MBartaaTokenizer(SCREAMING_SNAKE_CASE__ , src_lang='en_XX' , tgt_lang='ro_RO' , keep_accents=SCREAMING_SNAKE_CASE__ ) tokenizer.save_pretrained(self.tmpdirname ) def _UpperCamelCase ( self : Tuple ): lowerCamelCase__ = '<s>' lowerCamelCase__ = 0 self.assertEqual(self.get_tokenizer()._convert_token_to_id(SCREAMING_SNAKE_CASE__ ) , SCREAMING_SNAKE_CASE__ ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(SCREAMING_SNAKE_CASE__ ) , SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : Dict ): 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] , '<mask>' ) self.assertEqual(len(SCREAMING_SNAKE_CASE__ ) , 10_54 ) def _UpperCamelCase ( self : Optional[int] ): self.assertEqual(self.get_tokenizer().vocab_size , 10_54 ) def _UpperCamelCase ( self : Optional[Any] ): lowerCamelCase__ = MBartaaTokenizer(SCREAMING_SNAKE_CASE__ , src_lang='en_XX' , tgt_lang='ro_RO' , keep_accents=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = tokenizer.tokenize('This is a test' ) self.assertListEqual(SCREAMING_SNAKE_CASE__ , ['▁This', '▁is', '▁a', '▁t', 'est'] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(SCREAMING_SNAKE_CASE__ ) , [value + tokenizer.fairseq_offset for value in [2_85, 46, 10, 1_70, 3_82]] , ) lowerCamelCase__ = tokenizer.tokenize('I was born in 92000, and this is falsé.' ) self.assertListEqual( SCREAMING_SNAKE_CASE__ , [SPIECE_UNDERLINE + 'I', SPIECE_UNDERLINE + 'was', SPIECE_UNDERLINE + 'b', 'or', 'n', SPIECE_UNDERLINE + 'in', SPIECE_UNDERLINE + '', '9', '2', '0', '0', '0', ',', SPIECE_UNDERLINE + 'and', SPIECE_UNDERLINE + 'this', SPIECE_UNDERLINE + 'is', SPIECE_UNDERLINE + 'f', 'al', 's', 'é', '.'] , ) lowerCamelCase__ = tokenizer.convert_tokens_to_ids(SCREAMING_SNAKE_CASE__ ) self.assertListEqual( SCREAMING_SNAKE_CASE__ , [ value + tokenizer.fairseq_offset for value in [8, 21, 84, 55, 24, 19, 7, 2, 6_02, 3_47, 3_47, 3_47, 3, 12, 66, 46, 72, 80, 6, 2, 4] ] , ) lowerCamelCase__ = tokenizer.convert_ids_to_tokens(SCREAMING_SNAKE_CASE__ ) self.assertListEqual( SCREAMING_SNAKE_CASE__ , [SPIECE_UNDERLINE + 'I', SPIECE_UNDERLINE + 'was', SPIECE_UNDERLINE + 'b', 'or', 'n', SPIECE_UNDERLINE + 'in', SPIECE_UNDERLINE + '', '<unk>', '2', '0', '0', '0', ',', SPIECE_UNDERLINE + 'and', SPIECE_UNDERLINE + 'this', SPIECE_UNDERLINE + 'is', SPIECE_UNDERLINE + 'f', 'al', 's', '<unk>', '.'] , ) @slow def _UpperCamelCase ( self : List[Any] ): # fmt: off lowerCamelCase__ = {'input_ids': [[25_00_04, 1_10_62, 8_27_72, 7, 15, 8_27_72, 5_38, 5_15_29, 2_37, 1_71_98, 12_90, 2_06, 9, 21_51_75, 13_14, 1_36, 1_71_98, 12_90, 2_06, 9, 5_63_59, 42, 12_20_09, 9, 1_64_66, 16, 8_73_44, 45_37, 9, 47_17, 7_83_81, 6, 15_99_58, 7, 15, 2_44_80, 6_18, 4, 5_27, 2_26_93, 54_28, 4, 27_77, 2_44_80, 98_74, 4, 4_35_23, 5_94, 4, 8_03, 1_83_92, 3_31_89, 18, 4, 4_35_23, 2_44_47, 1_23_99, 1_00, 2_49_55, 8_36_58, 96_26, 14_40_57, 15, 8_39, 2_23_35, 16, 1_36, 2_49_55, 8_36_58, 8_34_79, 15, 3_91_02, 7_24, 16, 6_78, 6_45, 27_89, 13_28, 45_89, 42, 12_20_09, 11_57_74, 23, 8_05, 13_28, 4_68_76, 7, 1_36, 5_38_94, 19_40, 4_22_27, 4_11_59, 1_77_21, 8_23, 4_25, 4, 2_75_12, 9_87_22, 2_06, 1_36, 55_31, 49_70, 9_19, 1_73_36, 5, 2], [25_00_04, 2_00_80, 6_18, 83, 8_27_75, 47, 4_79, 9, 15_17, 73, 5_38_94, 3_33, 8_05_81, 11_01_17, 1_88_11, 52_56, 12_95, 51, 15_25_26, 2_97, 79_86, 3_90, 12_44_16, 5_38, 3_54_31, 2_14, 98, 1_50_44, 2_57_37, 1_36, 71_08, 4_37_01, 23, 7_56, 13_53_55, 7, 5, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [25_00_04, 5_81, 6_37_73, 11_94_55, 6, 14_77_97, 8_82_03, 7, 6_45, 70, 21, 32_85, 1_02_69, 5, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], 'attention_mask': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=SCREAMING_SNAKE_CASE__ , model_name='facebook/mbart-large-50' , revision='d3913889c59cd5c9e456b269c376325eabad57e2' , ) def _UpperCamelCase ( self : Union[str, Any] ): if not self.test_slow_tokenizer: # as we don't have a slow version, we can't compare the outputs between slow and fast versions return lowerCamelCase__ = (self.rust_tokenizer_class, 'hf-internal-testing/tiny-random-mbart50', {}) 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__ = self.tokenizer_class.from_pretrained(SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = tempfile.mkdtemp() lowerCamelCase__ = tokenizer_r.save_pretrained(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = tokenizer_p.save_pretrained(SCREAMING_SNAKE_CASE__ ) # Checks it save with the same files + the tokenizer.json file for the fast one self.assertTrue(any('tokenizer.json' in f for f in tokenizer_r_files ) ) lowerCamelCase__ = tuple(f for f in tokenizer_r_files if 'tokenizer.json' not in f ) self.assertSequenceEqual(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) # Checks everything loads correctly in the same way lowerCamelCase__ = tokenizer_r.from_pretrained(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = tokenizer_p.from_pretrained(SCREAMING_SNAKE_CASE__ ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) ) # self.assertEqual(getattr(tokenizer_rp, key), getattr(tokenizer_pp, key)) # self.assertEqual(getattr(tokenizer_rp, key + "_id"), getattr(tokenizer_pp, key + "_id")) shutil.rmtree(SCREAMING_SNAKE_CASE__ ) # Save tokenizer rust, legacy_format=True lowerCamelCase__ = tempfile.mkdtemp() lowerCamelCase__ = tokenizer_r.save_pretrained(SCREAMING_SNAKE_CASE__ , legacy_format=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = tokenizer_p.save_pretrained(SCREAMING_SNAKE_CASE__ ) # Checks it save with the same files self.assertSequenceEqual(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) # Checks everything loads correctly in the same way lowerCamelCase__ = tokenizer_r.from_pretrained(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = tokenizer_p.from_pretrained(SCREAMING_SNAKE_CASE__ ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) ) shutil.rmtree(SCREAMING_SNAKE_CASE__ ) # Save tokenizer rust, legacy_format=False lowerCamelCase__ = tempfile.mkdtemp() lowerCamelCase__ = tokenizer_r.save_pretrained(SCREAMING_SNAKE_CASE__ , legacy_format=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = tokenizer_p.save_pretrained(SCREAMING_SNAKE_CASE__ ) # Checks it saved the tokenizer.json file self.assertTrue(any('tokenizer.json' in f for f in tokenizer_r_files ) ) # Checks everything loads correctly in the same way lowerCamelCase__ = tokenizer_r.from_pretrained(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = tokenizer_p.from_pretrained(SCREAMING_SNAKE_CASE__ ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) ) shutil.rmtree(SCREAMING_SNAKE_CASE__ ) @require_torch @require_sentencepiece @require_tokenizers class _a ( unittest.TestCase ): a_ : Dict = 'facebook/mbart-large-50-one-to-many-mmt' a_ : Optional[int] = [ ' UN Chief Says There Is No Military Solution in Syria', ' Secretary-General Ban Ki-moon says his response to Russia\'s stepped up military support for Syria is that "there is no military solution" to the nearly five-year conflict and more weapons will only worsen the violence and misery for millions of people.', ] a_ : Tuple = [ 'Şeful ONU declară că nu există o soluţie militară în Siria', 'Secretarul General Ban Ki-moon declară că răspunsul său la intensificarea sprijinului militar al Rusiei' ' pentru Siria este că "nu există o soluţie militară" la conflictul de aproape cinci ani şi că noi arme nu vor' ' face decât să înrăutăţească violenţele şi mizeria pentru milioane de oameni.', ] a_ : str = [EN_CODE, 8274, 127873, 25916, 7, 8622, 2071, 438, 67485, 53, 187895, 23, 51712, 2] @classmethod def _UpperCamelCase ( cls : List[Any] ): lowerCamelCase__ = MBartaaTokenizer.from_pretrained( cls.checkpoint_name , src_lang='en_XX' , tgt_lang='ro_RO' ) lowerCamelCase__ = 1 return cls def _UpperCamelCase ( self : Dict ): self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['ar_AR'] , 25_00_01 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['en_EN'] , 25_00_04 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['ro_RO'] , 25_00_20 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['mr_IN'] , 25_00_38 ) def _UpperCamelCase ( self : Optional[Any] ): lowerCamelCase__ = self.tokenizer.batch_encode_plus(self.src_text ).input_ids[0] self.assertListEqual(self.expected_src_tokens , SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : List[str] ): self.assertIn(SCREAMING_SNAKE_CASE__ , self.tokenizer.all_special_ids ) lowerCamelCase__ = [RO_CODE, 8_84, 90_19, 96, 9, 9_16, 8_67_92, 36, 1_87_43, 1_55_96, 5, 2] lowerCamelCase__ = self.tokenizer.decode(SCREAMING_SNAKE_CASE__ , skip_special_tokens=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = self.tokenizer.decode(generated_ids[1:] , skip_special_tokens=SCREAMING_SNAKE_CASE__ ) self.assertEqual(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) self.assertNotIn(self.tokenizer.eos_token , SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : Optional[Any] ): lowerCamelCase__ = ['this is gunna be a long sentence ' * 20] assert isinstance(src_text[0] , SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = 10 lowerCamelCase__ = self.tokenizer(SCREAMING_SNAKE_CASE__ , max_length=SCREAMING_SNAKE_CASE__ , truncation=SCREAMING_SNAKE_CASE__ ).input_ids[0] self.assertEqual(ids[0] , SCREAMING_SNAKE_CASE__ ) self.assertEqual(ids[-1] , 2 ) self.assertEqual(len(SCREAMING_SNAKE_CASE__ ) , SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : List[str] ): self.assertListEqual(self.tokenizer.convert_tokens_to_ids(['<mask>', 'ar_AR'] ) , [25_00_53, 25_00_01] ) def _UpperCamelCase ( self : Dict ): lowerCamelCase__ = tempfile.mkdtemp() lowerCamelCase__ = self.tokenizer.fairseq_tokens_to_ids self.tokenizer.save_pretrained(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = MBartaaTokenizer.from_pretrained(SCREAMING_SNAKE_CASE__ ) self.assertDictEqual(new_tok.fairseq_tokens_to_ids , SCREAMING_SNAKE_CASE__ ) @require_torch def _UpperCamelCase ( self : List[Any] ): lowerCamelCase__ = self.tokenizer(self.src_text , text_target=self.tgt_text , padding=SCREAMING_SNAKE_CASE__ , return_tensors='pt' ) lowerCamelCase__ = shift_tokens_right(batch['labels'] , self.tokenizer.pad_token_id ) # fairseq batch: https://gist.github.com/sshleifer/cba08bc2109361a74ac3760a7e30e4f4 assert batch.input_ids[1][0] == EN_CODE assert batch.input_ids[1][-1] == 2 assert batch.labels[1][0] == RO_CODE assert batch.labels[1][-1] == 2 assert batch.decoder_input_ids[1][:2].tolist() == [2, RO_CODE] @require_torch def _UpperCamelCase ( self : List[str] ): lowerCamelCase__ = self.tokenizer( self.src_text , text_target=self.tgt_text , padding=SCREAMING_SNAKE_CASE__ , truncation=SCREAMING_SNAKE_CASE__ , max_length=len(self.expected_src_tokens ) , return_tensors='pt' , ) lowerCamelCase__ = shift_tokens_right(batch['labels'] , self.tokenizer.pad_token_id ) self.assertIsInstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) self.assertEqual((2, 14) , batch.input_ids.shape ) self.assertEqual((2, 14) , batch.attention_mask.shape ) lowerCamelCase__ = batch.input_ids.tolist()[0] self.assertListEqual(self.expected_src_tokens , SCREAMING_SNAKE_CASE__ ) self.assertEqual(2 , batch.decoder_input_ids[0, 0] ) # decoder_start_token_id # Test that special tokens are reset self.assertEqual(self.tokenizer.prefix_tokens , [EN_CODE] ) self.assertEqual(self.tokenizer.suffix_tokens , [self.tokenizer.eos_token_id] ) def _UpperCamelCase ( self : Dict ): lowerCamelCase__ = self.tokenizer(self.src_text , padding=SCREAMING_SNAKE_CASE__ , truncation=SCREAMING_SNAKE_CASE__ , max_length=3 , return_tensors='pt' ) lowerCamelCase__ = self.tokenizer( text_target=self.tgt_text , padding=SCREAMING_SNAKE_CASE__ , truncation=SCREAMING_SNAKE_CASE__ , max_length=10 , return_tensors='pt' ) lowerCamelCase__ = targets['input_ids'] lowerCamelCase__ = shift_tokens_right(SCREAMING_SNAKE_CASE__ , self.tokenizer.pad_token_id ) self.assertEqual(batch.input_ids.shape[1] , 3 ) self.assertEqual(batch.decoder_input_ids.shape[1] , 10 ) @require_torch def _UpperCamelCase ( self : Dict ): lowerCamelCase__ = self.tokenizer._build_translation_inputs( 'A test' , return_tensors='pt' , src_lang='en_XX' , tgt_lang='ar_AR' ) self.assertEqual( nested_simplify(SCREAMING_SNAKE_CASE__ ) , { # en_XX, A, test, EOS 'input_ids': [[25_00_04, 62, 30_34, 2]], 'attention_mask': [[1, 1, 1, 1]], # ar_AR 'forced_bos_token_id': 25_00_01, } , )
659
"""simple docstring""" from __future__ import annotations _snake_case = [ [-1, 0], # left [0, -1], # down [1, 0], # right [0, 1], # up ] def snake_case ( _a: list[list[int]] , _a: list[int] , _a: list[int] , _a: int , _a: list[list[int]] , )-> tuple[list[list[int]], list[list[int]]]: '''simple docstring''' lowerCamelCase__ = [ [0 for col in range(len(grid[0] ) )] for row in range(len(_a ) ) ] # the reference grid lowerCamelCase__ = 1 lowerCamelCase__ = [ [0 for col in range(len(grid[0] ) )] for row in range(len(_a ) ) ] # the action grid lowerCamelCase__ = init[0] lowerCamelCase__ = init[1] lowerCamelCase__ = 0 lowerCamelCase__ = g + heuristic[x][y] # cost from starting cell to destination cell lowerCamelCase__ = [[f, g, x, y]] lowerCamelCase__ = False # flag that is set when search is complete lowerCamelCase__ = False # flag set if we can't find expand while not found and not resign: if len(_a ) == 0: raise ValueError('Algorithm is unable to find solution' ) else: # to choose the least costliest action so as to move closer to the goal cell.sort() cell.reverse() lowerCamelCase__ = cell.pop() lowerCamelCase__ = next_cell[2] lowerCamelCase__ = next_cell[3] lowerCamelCase__ = next_cell[1] if x == goal[0] and y == goal[1]: lowerCamelCase__ = True else: for i in range(len(_a ) ): # to try out different valid actions lowerCamelCase__ = x + DIRECTIONS[i][0] lowerCamelCase__ = y + DIRECTIONS[i][1] if xa >= 0 and xa < len(_a ) and ya >= 0 and ya < len(grid[0] ): if closed[xa][ya] == 0 and grid[xa][ya] == 0: lowerCamelCase__ = g + cost lowerCamelCase__ = ga + heuristic[xa][ya] cell.append([fa, ga, xa, ya] ) lowerCamelCase__ = 1 lowerCamelCase__ = i lowerCamelCase__ = [] lowerCamelCase__ = goal[0] lowerCamelCase__ = goal[1] invpath.append([x, y] ) # we get the reverse path from here while x != init[0] or y != init[1]: lowerCamelCase__ = x - DIRECTIONS[action[x][y]][0] lowerCamelCase__ = y - DIRECTIONS[action[x][y]][1] lowerCamelCase__ = xa lowerCamelCase__ = ya invpath.append([x, y] ) lowerCamelCase__ = [] for i in range(len(_a ) ): path.append(invpath[len(_a ) - 1 - i] ) return path, action if __name__ == "__main__": _snake_case = [ [0, 1, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles [0, 1, 0, 0, 0, 0], [0, 1, 0, 0, 1, 0], [0, 0, 0, 0, 1, 0], ] _snake_case = [0, 0] # all coordinates are given in format [y,x] _snake_case = [len(grid) - 1, len(grid[0]) - 1] _snake_case = 1 # the cost map which pushes the path closer to the goal _snake_case = [[0 for row in range(len(grid[0]))] for col in range(len(grid))] for i in range(len(grid)): for j in range(len(grid[0])): _snake_case = abs(i - goal[0]) + abs(j - goal[1]) if grid[i][j] == 1: # added extra penalty in the heuristic map _snake_case = 99 _snake_case , _snake_case = search(grid, init, goal, cost, heuristic) print("ACTION MAP") for i in range(len(action)): print(action[i]) for i in range(len(path)): print(path[i])
659
1
"""simple docstring""" from math import pow, sqrt def snake_case ( *_a: float )-> bool: '''simple docstring''' lowerCamelCase__ = len(_a ) > 0 and all(value > 0.0 for value in values ) return result def snake_case ( _a: float , _a: float )-> float | ValueError: '''simple docstring''' return ( round(sqrt(molar_mass_a / molar_mass_a ) , 6 ) if validate(_a , _a ) else ValueError('Input Error: Molar mass values must greater than 0.' ) ) def snake_case ( _a: float , _a: float , _a: float )-> float | ValueError: '''simple docstring''' return ( round(effusion_rate * sqrt(molar_mass_a / molar_mass_a ) , 6 ) if validate(_a , _a , _a ) else ValueError( 'Input Error: Molar mass and effusion rate values must greater than 0.' ) ) def snake_case ( _a: float , _a: float , _a: float )-> float | ValueError: '''simple docstring''' return ( round(effusion_rate / sqrt(molar_mass_a / molar_mass_a ) , 6 ) if validate(_a , _a , _a ) else ValueError( 'Input Error: Molar mass and effusion rate values must greater than 0.' ) ) def snake_case ( _a: float , _a: float , _a: float )-> float | ValueError: '''simple docstring''' return ( round(molar_mass / pow(effusion_rate_a / effusion_rate_a , 2 ) , 6 ) if validate(_a , _a , _a ) else ValueError( 'Input Error: Molar mass and effusion rate values must greater than 0.' ) ) def snake_case ( _a: float , _a: float , _a: float )-> float | ValueError: '''simple docstring''' return ( round(pow(effusion_rate_a / effusion_rate_a , 2 ) / molar_mass , 6 ) if validate(_a , _a , _a ) else ValueError( 'Input Error: Molar mass and effusion rate values must greater than 0.' ) )
659
"""simple docstring""" def snake_case ( _a: int = 4000000 )-> int: '''simple docstring''' lowerCamelCase__ = [0, 1] lowerCamelCase__ = 0 while fib[i] <= n: fib.append(fib[i] + fib[i + 1] ) if fib[i + 2] > n: break i += 1 lowerCamelCase__ = 0 for j in range(len(_a ) - 1 ): if fib[j] % 2 == 0: total += fib[j] return total if __name__ == "__main__": print(f"""{solution() = }""")
659
1
"""simple docstring""" from __future__ import annotations _snake_case = tuple[int, int, int] _snake_case = tuple[str, str, str] # used alphabet -------------------------- # from string.ascii_uppercase _snake_case = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" # -------------------------- default selection -------------------------- # rotors -------------------------- _snake_case = "EGZWVONAHDCLFQMSIPJBYUKXTR" _snake_case = "FOBHMDKEXQNRAULPGSJVTYICZW" _snake_case = "ZJXESIUQLHAVRMDOYGTNFWPBKC" # reflector -------------------------- _snake_case = { "A": "N", "N": "A", "B": "O", "O": "B", "C": "P", "P": "C", "D": "Q", "Q": "D", "E": "R", "R": "E", "F": "S", "S": "F", "G": "T", "T": "G", "H": "U", "U": "H", "I": "V", "V": "I", "J": "W", "W": "J", "K": "X", "X": "K", "L": "Y", "Y": "L", "M": "Z", "Z": "M", } # -------------------------- extra rotors -------------------------- _snake_case = "RMDJXFUWGISLHVTCQNKYPBEZOA" _snake_case = "SGLCPQWZHKXAREONTFBVIYJUDM" _snake_case = "HVSICLTYKQUBXDWAJZOMFGPREN" _snake_case = "RZWQHFMVDBKICJLNTUXAGYPSOE" _snake_case = "LFKIJODBEGAMQPXVUHYSTCZRWN" _snake_case = "KOAEGVDHXPQZMLFTYWJNBRCIUS" def snake_case ( _a: RotorPositionT , _a: RotorSelectionT , _a: str )-> tuple[RotorPositionT, RotorSelectionT, dict[str, str]]: '''simple docstring''' if (unique_rotsel := len(set(_a ) )) < 3: lowerCamelCase__ = F'Please use 3 unique rotors (not {unique_rotsel})' raise Exception(_a ) # Checks if rotor positions are valid lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ = rotpos if not 0 < rotorposa <= len(_a ): lowerCamelCase__ = F'First rotor position is not within range of 1..26 ({rotorposa}' raise ValueError(_a ) if not 0 < rotorposa <= len(_a ): lowerCamelCase__ = F'Second rotor position is not within range of 1..26 ({rotorposa})' raise ValueError(_a ) if not 0 < rotorposa <= len(_a ): lowerCamelCase__ = F'Third rotor position is not within range of 1..26 ({rotorposa})' raise ValueError(_a ) # Validates string and returns dict lowerCamelCase__ = _plugboard(_a ) return rotpos, rotsel, pbdict def snake_case ( _a: str )-> dict[str, str]: '''simple docstring''' if not isinstance(_a , _a ): lowerCamelCase__ = F'Plugboard setting isn\'t type string ({type(_a )})' raise TypeError(_a ) elif len(_a ) % 2 != 0: lowerCamelCase__ = F'Odd number of symbols ({len(_a )})' raise Exception(_a ) elif pbstring == "": return {} pbstring.replace(' ' , '' ) # Checks if all characters are unique lowerCamelCase__ = set() for i in pbstring: if i not in abc: lowerCamelCase__ = F'\'{i}\' not in list of symbols' raise Exception(_a ) elif i in tmppbl: lowerCamelCase__ = F'Duplicate symbol ({i})' raise Exception(_a ) else: tmppbl.add(_a ) del tmppbl # Created the dictionary lowerCamelCase__ = {} for j in range(0 , len(_a ) - 1 , 2 ): lowerCamelCase__ = pbstring[j + 1] lowerCamelCase__ = pbstring[j] return pb def snake_case ( _a: str , _a: RotorPositionT , _a: RotorSelectionT = (rotora, rotora, rotora) , _a: str = "" , )-> str: '''simple docstring''' lowerCamelCase__ = text.upper() lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ = _validator( _a , _a , plugb.upper() ) lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ = rotor_position lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ = rotor_selection rotorposa -= 1 rotorposa -= 1 rotorposa -= 1 lowerCamelCase__ = [] # encryption/decryption process -------------------------- for symbol in text: if symbol in abc: # 1st plugboard -------------------------- if symbol in plugboard: lowerCamelCase__ = plugboard[symbol] # rotor ra -------------------------- lowerCamelCase__ = abc.index(_a ) + rotorposa lowerCamelCase__ = rotora[index % len(_a )] # rotor rb -------------------------- lowerCamelCase__ = abc.index(_a ) + rotorposa lowerCamelCase__ = rotora[index % len(_a )] # rotor rc -------------------------- lowerCamelCase__ = abc.index(_a ) + rotorposa lowerCamelCase__ = rotora[index % len(_a )] # reflector -------------------------- # this is the reason you don't need another machine to decipher lowerCamelCase__ = reflector[symbol] # 2nd rotors lowerCamelCase__ = abc[rotora.index(_a ) - rotorposa] lowerCamelCase__ = abc[rotora.index(_a ) - rotorposa] lowerCamelCase__ = abc[rotora.index(_a ) - rotorposa] # 2nd plugboard if symbol in plugboard: lowerCamelCase__ = plugboard[symbol] # moves/resets rotor positions rotorposa += 1 if rotorposa >= len(_a ): lowerCamelCase__ = 0 rotorposa += 1 if rotorposa >= len(_a ): lowerCamelCase__ = 0 rotorposa += 1 if rotorposa >= len(_a ): lowerCamelCase__ = 0 # else: # pass # Error could be also raised # raise ValueError( # 'Invalid symbol('+repr(symbol)+')') result.append(_a ) return "".join(_a ) if __name__ == "__main__": _snake_case = "This is my Python script that emulates the Enigma machine from WWII." _snake_case = (1, 1, 1) _snake_case = "pictures" _snake_case = (rotora, rotora, rotora) _snake_case = enigma(message, rotor_pos, rotor_sel, pb) print("Encrypted message:", en) print("Decrypted message:", enigma(en, rotor_pos, rotor_sel, pb))
659
"""simple docstring""" def snake_case ( _a: List[Any] , _a: Any , _a: str , _a: List[Any] )-> List[Any]: '''simple docstring''' lowerCamelCase__ = [False] * len(_a ) lowerCamelCase__ = [] queue.append(_a ) lowerCamelCase__ = True while queue: lowerCamelCase__ = queue.pop(0 ) for ind in range(len(graph[u] ) ): if visited[ind] is False and graph[u][ind] > 0: queue.append(_a ) lowerCamelCase__ = True lowerCamelCase__ = u return visited[t] def snake_case ( _a: List[Any] , _a: str , _a: List[str] )-> Optional[int]: '''simple docstring''' lowerCamelCase__ = [-1] * (len(_a )) lowerCamelCase__ = 0 while bfs(_a , _a , _a , _a ): lowerCamelCase__ = float('Inf' ) lowerCamelCase__ = sink while s != source: # Find the minimum value in select path lowerCamelCase__ = min(_a , graph[parent[s]][s] ) lowerCamelCase__ = parent[s] max_flow += path_flow lowerCamelCase__ = sink while v != source: lowerCamelCase__ = parent[v] graph[u][v] -= path_flow graph[v][u] += path_flow lowerCamelCase__ = parent[v] return max_flow _snake_case = [ [0, 16, 13, 0, 0, 0], [0, 0, 10, 12, 0, 0], [0, 4, 0, 0, 14, 0], [0, 0, 9, 0, 0, 20], [0, 0, 0, 7, 0, 4], [0, 0, 0, 0, 0, 0], ] _snake_case , _snake_case = 0, 5 print(ford_fulkerson(graph, source, sink))
659
1
"""simple docstring""" _snake_case = { "Pillow": "Pillow", "accelerate": "accelerate>=0.11.0", "compel": "compel==0.1.8", "black": "black~=23.1", "datasets": "datasets", "filelock": "filelock", "flax": "flax>=0.4.1", "hf-doc-builder": "hf-doc-builder>=0.3.0", "huggingface-hub": "huggingface-hub>=0.13.2", "requests-mock": "requests-mock==1.10.0", "importlib_metadata": "importlib_metadata", "invisible-watermark": "invisible-watermark", "isort": "isort>=5.5.4", "jax": "jax>=0.2.8,!=0.3.2", "jaxlib": "jaxlib>=0.1.65", "Jinja2": "Jinja2", "k-diffusion": "k-diffusion>=0.0.12", "torchsde": "torchsde", "note_seq": "note_seq", "librosa": "librosa", "numpy": "numpy", "omegaconf": "omegaconf", "parameterized": "parameterized", "protobuf": "protobuf>=3.20.3,<4", "pytest": "pytest", "pytest-timeout": "pytest-timeout", "pytest-xdist": "pytest-xdist", "ruff": "ruff>=0.0.241", "safetensors": "safetensors", "sentencepiece": "sentencepiece>=0.1.91,!=0.1.92", "scipy": "scipy", "onnx": "onnx", "regex": "regex!=2019.12.17", "requests": "requests", "tensorboard": "tensorboard", "torch": "torch>=1.4", "torchvision": "torchvision", "transformers": "transformers>=4.25.1", "urllib3": "urllib3<=2.0.0", }
659
"""simple docstring""" import json import multiprocessing import os import re from collections import defaultdict import torch from accelerate import Accelerator from accelerate.utils import set_seed from arguments import HumanEvalArguments from datasets import load_dataset, load_metric from torch.utils.data import IterableDataset from torch.utils.data.dataloader import DataLoader from tqdm import tqdm import transformers from transformers import AutoModelForCausalLM, AutoTokenizer, HfArgumentParser, StoppingCriteria, StoppingCriteriaList _snake_case = ["\nclass", "\ndef", "\n#", "\n@", "\nprint", "\nif"] class _a ( SCREAMING_SNAKE_CASE_ ): def __init__( self : List[str] , SCREAMING_SNAKE_CASE__ : Any , SCREAMING_SNAKE_CASE__ : List[Any] , SCREAMING_SNAKE_CASE__ : int=None , SCREAMING_SNAKE_CASE__ : Optional[int]=1 ): lowerCamelCase__ = tokenizer lowerCamelCase__ = dataset lowerCamelCase__ = len(SCREAMING_SNAKE_CASE__ ) if n_tasks is None else n_tasks lowerCamelCase__ = n_copies def __iter__( self : Any ): lowerCamelCase__ = [] for task in range(self.n_tasks ): # without strip, the model generate commented codes ... prompts.append(self.tokenizer.eos_token + self.dataset[task]['prompt'].strip() ) lowerCamelCase__ = self.tokenizer(SCREAMING_SNAKE_CASE__ , padding=SCREAMING_SNAKE_CASE__ , return_tensors='pt' ) for task in range(self.n_tasks ): for _ in range(self.n_copies ): yield { "ids": outputs.input_ids[task], "task_id": task, "input_len": outputs.attention_mask[task].sum(), } class _a ( SCREAMING_SNAKE_CASE_ ): def __init__( self : Dict , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : List[Any] , SCREAMING_SNAKE_CASE__ : Dict ): lowerCamelCase__ = start_length lowerCamelCase__ = eof_strings lowerCamelCase__ = tokenizer def __call__( self : Any , SCREAMING_SNAKE_CASE__ : Any , SCREAMING_SNAKE_CASE__ : List[str] , **SCREAMING_SNAKE_CASE__ : Dict ): lowerCamelCase__ = self.tokenizer.batch_decode(input_ids[:, self.start_length :] ) lowerCamelCase__ = [] for decoded_generation in decoded_generations: done.append(any(stop_string in decoded_generation for stop_string in self.eof_strings ) ) return all(SCREAMING_SNAKE_CASE__ ) def snake_case ( _a: List[Any] )-> Dict: '''simple docstring''' lowerCamelCase__ = re.split('(%s)' % '|'.join(_a ) , _a ) # last string should be "" return "".join(string_list[:-2] ) def snake_case ( _a: List[Any] , _a: Optional[int] , _a: str , _a: Union[str, Any] , _a: Dict , _a: Optional[int]=20 , **_a: Optional[int] )-> List[str]: '''simple docstring''' lowerCamelCase__ = defaultdict(_a ) # dict of list of generated tokens for step, batch in tqdm(enumerate(_a ) ): with torch.no_grad(): lowerCamelCase__ = batch['ids'].shape[-1] lowerCamelCase__ = accelerator.unwrap_model(_a ).generate( input_ids=batch['ids'][:, : batch['input_len']] , num_return_sequences=_a , **_a ) # each task is generated batch_size times lowerCamelCase__ = batch['task_id'].repeat(_a ) lowerCamelCase__ = accelerator.pad_across_processes( _a , dim=1 , pad_index=tokenizer.pad_token_id ) lowerCamelCase__ , lowerCamelCase__ = accelerator.gather((generated_tokens, generated_tasks) ) lowerCamelCase__ = generated_tokens.cpu().numpy() lowerCamelCase__ = generated_tasks.cpu().numpy() for task, generated_tokens in zip(_a , _a ): gen_token_dict[task].append(_a ) lowerCamelCase__ = [[] for _ in range(_a )] for task, generated_tokens in gen_token_dict.items(): for s in generated_tokens: lowerCamelCase__ = tokenizer.decode(_a , skip_special_tokens=_a , clean_up_tokenization_spaces=_a ) code_gens[task].append(remove_last_block(_a ) ) return code_gens def snake_case ( )-> Union[str, Any]: '''simple docstring''' lowerCamelCase__ = HfArgumentParser(_a ) lowerCamelCase__ = parser.parse_args() transformers.logging.set_verbosity_error() # enables code execution in code_eval metric lowerCamelCase__ = args.HF_ALLOW_CODE_EVAL # make sure tokenizer plays nice with multiprocessing lowerCamelCase__ = 'false' if args.num_workers is None: lowerCamelCase__ = multiprocessing.cpu_count() # Use dataset load to feed to accelerate lowerCamelCase__ = Accelerator() set_seed(args.seed , device_specific=_a ) # Load model and tokenizer lowerCamelCase__ = AutoTokenizer.from_pretrained(args.model_ckpt ) lowerCamelCase__ = tokenizer.eos_token lowerCamelCase__ = AutoModelForCausalLM.from_pretrained(args.model_ckpt ) # Generation settings lowerCamelCase__ = { 'do_sample': args.do_sample, 'temperature': args.temperature, 'max_new_tokens': args.max_new_tokens, 'top_p': args.top_p, 'top_k': args.top_k, 'stopping_criteria': StoppingCriteriaList([EndOfFunctionCriteria(0 , _a , _a )] ), } # Load evaluation dataset and metric lowerCamelCase__ = load_dataset('openai_humaneval' ) lowerCamelCase__ = load_metric('code_eval' ) lowerCamelCase__ = args.num_tasks if args.num_tasks is not None else len(human_eval['test'] ) lowerCamelCase__ = args.n_samples // args.batch_size lowerCamelCase__ = TokenizedDataset(_a , human_eval['test'] , n_copies=_a , n_tasks=_a ) # do not confuse args.batch_size, which is actually the num_return_sequences lowerCamelCase__ = DataLoader(_a , batch_size=1 ) # Run a quick test to see if code evaluation is enabled try: lowerCamelCase__ = code_eval_metric.compute(references=[''] , predictions=[['']] ) except ValueError as exception: print( 'Code evaluation not enabled. Read the warning below carefully and then use `--HF_ALLOW_CODE_EVAL="1"`' ' flag to enable code evaluation.' ) raise exception lowerCamelCase__ , lowerCamelCase__ = accelerator.prepare(_a , _a ) lowerCamelCase__ = complete_code( _a , _a , _a , _a , n_tasks=_a , batch_size=args.batch_size , **_a , ) if accelerator.is_main_process: lowerCamelCase__ = [] for task in tqdm(range(_a ) ): lowerCamelCase__ = human_eval['test'][task]['test'] lowerCamelCase__ = F'check({human_eval["test"][task]["entry_point"]})' references.append('\n' + test_func + '\n' + entry_point ) # Evaluate completions with "code_eval" metric lowerCamelCase__ , lowerCamelCase__ = code_eval_metric.compute( references=_a , predictions=_a , num_workers=args.num_workers ) print(F'Results: {pass_at_k}' ) # Save results to json file with open(args.output_file , 'w' ) as fp: json.dump(_a , _a ) # For some reason the folliwng seems to be necessary sometimes for code_eval to work nice with multiprocessing # https://stackoverflow.com/questions/60804599/python-multiprocessing-keeps-spawning-the-whole-script if __name__ == "__main__": main()
659
1
"""simple docstring""" import json import os from pathlib import Path from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple, Union import sentencepiece from ...tokenization_utils import BatchEncoding, PreTrainedTokenizer from ...utils import logging _snake_case = logging.get_logger(__name__) _snake_case = "▁" _snake_case = { "vocab_file": "vocab.json", "spm_file": "sentencepiece.bpe.model", "tokenizer_config_file": "tokenizer_config.json", } _snake_case = { "vocab_file": { "facebook/m2m100_418M": "https://huggingface.co/facebook/m2m100_418M/resolve/main/vocab.json", "facebook/m2m100_1.2B": "https://huggingface.co/facebook/m2m100_1.2B/resolve/main/vocab.json", }, "spm_file": { "facebook/m2m100_418M": "https://huggingface.co/facebook/m2m100_418M/resolve/main/sentencepiece.bpe.model", "facebook/m2m100_1.2B": "https://huggingface.co/facebook/m2m100_1.2B/resolve/main/sentencepiece.bpe.model", }, "tokenizer_config_file": { "facebook/m2m100_418M": "https://huggingface.co/facebook/m2m100_418M/resolve/main/tokenizer_config.json", "facebook/m2m100_1.2B": "https://huggingface.co/facebook/m2m100_1.2B/resolve/main/tokenizer_config.json", }, } _snake_case = { "facebook/m2m100_418M": 1024, } # fmt: off _snake_case = { "m2m100": ["af", "am", "ar", "ast", "az", "ba", "be", "bg", "bn", "br", "bs", "ca", "ceb", "cs", "cy", "da", "de", "el", "en", "es", "et", "fa", "ff", "fi", "fr", "fy", "ga", "gd", "gl", "gu", "ha", "he", "hi", "hr", "ht", "hu", "hy", "id", "ig", "ilo", "is", "it", "ja", "jv", "ka", "kk", "km", "kn", "ko", "lb", "lg", "ln", "lo", "lt", "lv", "mg", "mk", "ml", "mn", "mr", "ms", "my", "ne", "nl", "no", "ns", "oc", "or", "pa", "pl", "ps", "pt", "ro", "ru", "sd", "si", "sk", "sl", "so", "sq", "sr", "ss", "su", "sv", "sw", "ta", "th", "tl", "tn", "tr", "uk", "ur", "uz", "vi", "wo", "xh", "yi", "yo", "zh", "zu"], "wmt21": ["en", "ha", "is", "ja", "cs", "ru", "zh", "de"] } class _a ( SCREAMING_SNAKE_CASE_ ): a_ : Optional[int] = VOCAB_FILES_NAMES a_ : List[str] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES a_ : List[str] = PRETRAINED_VOCAB_FILES_MAP a_ : Optional[Any] = ['input_ids', 'attention_mask'] a_ : List[int] = [] a_ : List[int] = [] def __init__( self : str , SCREAMING_SNAKE_CASE__ : List[Any] , SCREAMING_SNAKE_CASE__ : Dict , SCREAMING_SNAKE_CASE__ : List[Any]=None , SCREAMING_SNAKE_CASE__ : List[str]=None , SCREAMING_SNAKE_CASE__ : Tuple="<s>" , SCREAMING_SNAKE_CASE__ : Any="</s>" , SCREAMING_SNAKE_CASE__ : List[str]="</s>" , SCREAMING_SNAKE_CASE__ : str="<pad>" , SCREAMING_SNAKE_CASE__ : List[str]="<unk>" , SCREAMING_SNAKE_CASE__ : str="m2m100" , SCREAMING_SNAKE_CASE__ : Optional[Dict[str, Any]] = None , SCREAMING_SNAKE_CASE__ : int=8 , **SCREAMING_SNAKE_CASE__ : Optional[int] , ): lowerCamelCase__ = {} if sp_model_kwargs is None else sp_model_kwargs lowerCamelCase__ = language_codes lowerCamelCase__ = FAIRSEQ_LANGUAGE_CODES[language_codes] lowerCamelCase__ = {lang_code: F'__{lang_code}__' for lang_code in fairseq_language_code} lowerCamelCase__ = kwargs.get('additional_special_tokens' , [] ) kwargs["additional_special_tokens"] += [ self.get_lang_token(SCREAMING_SNAKE_CASE__ ) for lang_code in fairseq_language_code if self.get_lang_token(SCREAMING_SNAKE_CASE__ ) not in kwargs["additional_special_tokens"] ] super().__init__( src_lang=SCREAMING_SNAKE_CASE__ , tgt_lang=SCREAMING_SNAKE_CASE__ , bos_token=SCREAMING_SNAKE_CASE__ , eos_token=SCREAMING_SNAKE_CASE__ , sep_token=SCREAMING_SNAKE_CASE__ , unk_token=SCREAMING_SNAKE_CASE__ , pad_token=SCREAMING_SNAKE_CASE__ , language_codes=SCREAMING_SNAKE_CASE__ , sp_model_kwargs=self.sp_model_kwargs , num_madeup_words=SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ , ) lowerCamelCase__ = vocab_file lowerCamelCase__ = load_json(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = {v: k for k, v in self.encoder.items()} lowerCamelCase__ = spm_file lowerCamelCase__ = load_spm(SCREAMING_SNAKE_CASE__ , self.sp_model_kwargs ) lowerCamelCase__ = len(self.encoder ) lowerCamelCase__ = { self.get_lang_token(SCREAMING_SNAKE_CASE__ ): self.encoder_size + i for i, lang_code in enumerate(SCREAMING_SNAKE_CASE__ ) } lowerCamelCase__ = {lang_code: self.encoder_size + i for i, lang_code in enumerate(SCREAMING_SNAKE_CASE__ )} lowerCamelCase__ = {v: k for k, v in self.lang_token_to_id.items()} lowerCamelCase__ = src_lang if src_lang is not None else 'en' lowerCamelCase__ = tgt_lang lowerCamelCase__ = self.get_lang_id(self._src_lang ) self.set_src_lang_special_tokens(self._src_lang ) lowerCamelCase__ = num_madeup_words @property def _UpperCamelCase ( self : Union[str, Any] ): return len(self.encoder ) + len(self.lang_token_to_id ) @property def _UpperCamelCase ( self : List[str] ): return self._src_lang @src_lang.setter def _UpperCamelCase ( self : Dict , SCREAMING_SNAKE_CASE__ : str ): lowerCamelCase__ = new_src_lang self.set_src_lang_special_tokens(self._src_lang ) def _UpperCamelCase ( self : List[Any] , SCREAMING_SNAKE_CASE__ : str ): return self.sp_model.encode(SCREAMING_SNAKE_CASE__ , out_type=SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : List[str] , SCREAMING_SNAKE_CASE__ : List[Any] ): if token in self.lang_token_to_id: return self.lang_token_to_id[token] return self.encoder.get(SCREAMING_SNAKE_CASE__ , self.encoder[self.unk_token] ) def _UpperCamelCase ( self : Optional[int] , SCREAMING_SNAKE_CASE__ : int ): if index in self.id_to_lang_token: return self.id_to_lang_token[index] return self.decoder.get(SCREAMING_SNAKE_CASE__ , self.unk_token ) def _UpperCamelCase ( self : Optional[int] , SCREAMING_SNAKE_CASE__ : List[str] ): lowerCamelCase__ = [] lowerCamelCase__ = '' for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: out_string += self.sp_model.decode(SCREAMING_SNAKE_CASE__ ) + token lowerCamelCase__ = [] else: current_sub_tokens.append(SCREAMING_SNAKE_CASE__ ) out_string += self.sp_model.decode(SCREAMING_SNAKE_CASE__ ) return out_string.strip() def _UpperCamelCase ( self : List[Any] , SCREAMING_SNAKE_CASE__ : List[int] , SCREAMING_SNAKE_CASE__ : Optional[List[int]] = None , SCREAMING_SNAKE_CASE__ : bool = False ): if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=SCREAMING_SNAKE_CASE__ , token_ids_a=SCREAMING_SNAKE_CASE__ , already_has_special_tokens=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = [1] * len(self.prefix_tokens ) lowerCamelCase__ = [1] * len(self.suffix_tokens ) if token_ids_a is None: return prefix_ones + ([0] * len(SCREAMING_SNAKE_CASE__ )) + suffix_ones return prefix_ones + ([0] * len(SCREAMING_SNAKE_CASE__ )) + ([0] * len(SCREAMING_SNAKE_CASE__ )) + suffix_ones def _UpperCamelCase ( self : List[str] , SCREAMING_SNAKE_CASE__ : List[int] , SCREAMING_SNAKE_CASE__ : Optional[List[int]] = None ): if token_ids_a is None: return self.prefix_tokens + token_ids_a + self.suffix_tokens # We don't expect to process pairs, but leave the pair logic for API consistency return self.prefix_tokens + token_ids_a + token_ids_a + self.suffix_tokens def _UpperCamelCase ( self : str ): lowerCamelCase__ = {self.convert_ids_to_tokens(SCREAMING_SNAKE_CASE__ ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self : int ): lowerCamelCase__ = self.__dict__.copy() lowerCamelCase__ = None return state def __setstate__( self : Dict , SCREAMING_SNAKE_CASE__ : Dict ): lowerCamelCase__ = d # for backward compatibility if not hasattr(self , 'sp_model_kwargs' ): lowerCamelCase__ = {} lowerCamelCase__ = load_spm(self.spm_file , self.sp_model_kwargs ) def _UpperCamelCase ( self : Union[str, Any] , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : Optional[str] = None ): lowerCamelCase__ = Path(SCREAMING_SNAKE_CASE__ ) if not save_dir.is_dir(): raise OSError(F'{save_directory} should be a directory' ) lowerCamelCase__ = save_dir / ( (filename_prefix + '-' if filename_prefix else '') + self.vocab_files_names['vocab_file'] ) lowerCamelCase__ = save_dir / ( (filename_prefix + '-' if filename_prefix else '') + self.vocab_files_names['spm_file'] ) save_json(self.encoder , SCREAMING_SNAKE_CASE__ ) if os.path.abspath(self.spm_file ) != os.path.abspath(SCREAMING_SNAKE_CASE__ ) and os.path.isfile(self.spm_file ): copyfile(self.spm_file , SCREAMING_SNAKE_CASE__ ) elif not os.path.isfile(self.spm_file ): with open(SCREAMING_SNAKE_CASE__ , 'wb' ) as fi: lowerCamelCase__ = self.sp_model.serialized_model_proto() fi.write(SCREAMING_SNAKE_CASE__ ) return (str(SCREAMING_SNAKE_CASE__ ), str(SCREAMING_SNAKE_CASE__ )) def _UpperCamelCase ( self : List[Any] , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : str = "en" , SCREAMING_SNAKE_CASE__ : Optional[List[str]] = None , SCREAMING_SNAKE_CASE__ : str = "ro" , **SCREAMING_SNAKE_CASE__ : str , ): lowerCamelCase__ = src_lang lowerCamelCase__ = tgt_lang self.set_src_lang_special_tokens(self.src_lang ) return super().prepare_seqaseq_batch(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : str , SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : Optional[str] , SCREAMING_SNAKE_CASE__ : Optional[str] , **SCREAMING_SNAKE_CASE__ : List[str] ): if src_lang is None or tgt_lang is None: raise ValueError('Translation requires a `src_lang` and a `tgt_lang` for this model' ) lowerCamelCase__ = src_lang lowerCamelCase__ = self(SCREAMING_SNAKE_CASE__ , add_special_tokens=SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = self.get_lang_id(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = tgt_lang_id return inputs def _UpperCamelCase ( self : Optional[int] ): self.set_src_lang_special_tokens(self.src_lang ) def _UpperCamelCase ( self : Tuple ): self.set_tgt_lang_special_tokens(self.tgt_lang ) def _UpperCamelCase ( self : Dict , SCREAMING_SNAKE_CASE__ : str ): lowerCamelCase__ = self.get_lang_token(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = self.lang_token_to_id[lang_token] lowerCamelCase__ = [self.cur_lang_id] lowerCamelCase__ = [self.eos_token_id] def _UpperCamelCase ( self : List[Any] , SCREAMING_SNAKE_CASE__ : str ): lowerCamelCase__ = self.get_lang_token(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = self.lang_token_to_id[lang_token] lowerCamelCase__ = [self.cur_lang_id] lowerCamelCase__ = [self.eos_token_id] def _UpperCamelCase ( self : Union[str, Any] , SCREAMING_SNAKE_CASE__ : str ): return self.lang_code_to_token[lang] def _UpperCamelCase ( self : List[Any] , SCREAMING_SNAKE_CASE__ : str ): lowerCamelCase__ = self.get_lang_token(SCREAMING_SNAKE_CASE__ ) return self.lang_token_to_id[lang_token] def snake_case ( _a: str , _a: Dict[str, Any] )-> sentencepiece.SentencePieceProcessor: '''simple docstring''' lowerCamelCase__ = sentencepiece.SentencePieceProcessor(**_a ) spm.Load(str(_a ) ) return spm def snake_case ( _a: str )-> Union[Dict, List]: '''simple docstring''' with open(_a , 'r' ) as f: return json.load(_a ) def snake_case ( _a: str , _a: str )-> None: '''simple docstring''' with open(_a , 'w' ) as f: json.dump(_a , _a , indent=2 )
659
"""simple docstring""" import argparse import json from tqdm import tqdm def snake_case ( )-> List[Any]: '''simple docstring''' lowerCamelCase__ = argparse.ArgumentParser() # Required parameters parser.add_argument( '--src_path' , type=_a , default='biencoder-nq-dev.json' , help='Path to raw DPR training data' , ) parser.add_argument( '--evaluation_set' , type=_a , help='where to store parsed evaluation_set file' , ) parser.add_argument( '--gold_data_path' , type=_a , help='where to store parsed gold_data_path file' , ) lowerCamelCase__ = parser.parse_args() with open(args.src_path , 'r' ) as src_file, open(args.evaluation_set , 'w' ) as eval_file, open( args.gold_data_path , 'w' ) as gold_file: lowerCamelCase__ = json.load(_a ) for dpr_record in tqdm(_a ): lowerCamelCase__ = dpr_record['question'] lowerCamelCase__ = [context['title'] for context in dpr_record['positive_ctxs']] eval_file.write(question + '\n' ) gold_file.write('\t'.join(_a ) + '\n' ) if __name__ == "__main__": main()
659
1
"""simple docstring""" from __future__ import annotations def snake_case ( _a: str , _a: str )-> bool: '''simple docstring''' lowerCamelCase__ = get_failure_array(_a ) # 2) Step through text searching for pattern lowerCamelCase__ , lowerCamelCase__ = 0, 0 # index into text, pattern while i < len(_a ): if pattern[j] == text[i]: if j == (len(_a ) - 1): return True j += 1 # if this is a prefix in our pattern # just go back far enough to continue elif j > 0: lowerCamelCase__ = failure[j - 1] continue i += 1 return False def snake_case ( _a: str )-> list[int]: '''simple docstring''' lowerCamelCase__ = [0] lowerCamelCase__ = 0 lowerCamelCase__ = 1 while j < len(_a ): if pattern[i] == pattern[j]: i += 1 elif i > 0: lowerCamelCase__ = failure[i - 1] continue j += 1 failure.append(_a ) return failure if __name__ == "__main__": # Test 1) _snake_case = "abc1abc12" _snake_case = "alskfjaldsabc1abc1abc12k23adsfabcabc" _snake_case = "alskfjaldsk23adsfabcabc" assert kmp(pattern, texta) and not kmp(pattern, texta) # Test 2) _snake_case = "ABABX" _snake_case = "ABABZABABYABABX" assert kmp(pattern, text) # Test 3) _snake_case = "AAAB" _snake_case = "ABAAAAAB" assert kmp(pattern, text) # Test 4) _snake_case = "abcdabcy" _snake_case = "abcxabcdabxabcdabcdabcy" assert kmp(pattern, text) # Test 5) _snake_case = "aabaabaaa" assert get_failure_array(pattern) == [0, 1, 0, 1, 2, 3, 4, 5, 2]
659
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) _snake_case = { "configuration_blenderbot": [ "BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP", "BlenderbotConfig", "BlenderbotOnnxConfig", ], "tokenization_blenderbot": ["BlenderbotTokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = ["BlenderbotTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = [ "BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST", "BlenderbotForCausalLM", "BlenderbotForConditionalGeneration", "BlenderbotModel", "BlenderbotPreTrainedModel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = [ "TFBlenderbotForConditionalGeneration", "TFBlenderbotModel", "TFBlenderbotPreTrainedModel", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = [ "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 _snake_case = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
659
1
"""simple docstring""" from unittest.mock import patch import pyspark from datasets.packaged_modules.spark.spark import ( Spark, SparkExamplesIterable, _generate_iterable_examples, ) from ..utils import ( require_dill_gt_0_3_2, require_not_windows, ) def snake_case ( _a: Dict , _a: int )-> List[str]: '''simple docstring''' lowerCamelCase__ = [] for part_id in partition_order: lowerCamelCase__ = df.where(F'SPARK_PARTITION_ID() = {part_id}' ).collect() for row_idx, row in enumerate(_a ): expected_row_ids_and_row_dicts.append((F'{part_id}_{row_idx}', row.asDict()) ) return expected_row_ids_and_row_dicts @require_not_windows @require_dill_gt_0_3_2 def snake_case ( )-> List[str]: '''simple docstring''' lowerCamelCase__ = pyspark.sql.SparkSession.builder.master('local[*]' ).appName('pyspark' ).getOrCreate() lowerCamelCase__ = spark.range(100 ).repartition(1 ) lowerCamelCase__ = Spark(_a ) # The id ints will be converted to Pyarrow int64s, so each row will be 8 bytes. Setting a max_shard_size of 16 means # that each partition can hold 2 rows. spark_builder._repartition_df_if_needed(max_shard_size=16 ) # Given that the dataframe has 100 rows and each partition has 2 rows, we expect 50 partitions. assert spark_builder.df.rdd.getNumPartitions() == 50 @require_not_windows @require_dill_gt_0_3_2 def snake_case ( )-> List[Any]: '''simple docstring''' lowerCamelCase__ = pyspark.sql.SparkSession.builder.master('local[*]' ).appName('pyspark' ).getOrCreate() lowerCamelCase__ = spark.range(10 ).repartition(2 ) lowerCamelCase__ = [1, 0] lowerCamelCase__ = _generate_iterable_examples(_a , _a ) # Reverse the partitions. lowerCamelCase__ = _get_expected_row_ids_and_row_dicts_for_partition_order(_a , _a ) for i, (row_id, row_dict) in enumerate(generate_fn() ): lowerCamelCase__ , lowerCamelCase__ = expected_row_ids_and_row_dicts[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def snake_case ( )-> Optional[int]: '''simple docstring''' lowerCamelCase__ = pyspark.sql.SparkSession.builder.master('local[*]' ).appName('pyspark' ).getOrCreate() lowerCamelCase__ = spark.range(10 ).repartition(1 ) lowerCamelCase__ = SparkExamplesIterable(_a ) assert it.n_shards == 1 for i, (row_id, row_dict) in enumerate(_a ): assert row_id == F'0_{i}' assert row_dict == {"id": i} @require_not_windows @require_dill_gt_0_3_2 def snake_case ( )-> Tuple: '''simple docstring''' lowerCamelCase__ = pyspark.sql.SparkSession.builder.master('local[*]' ).appName('pyspark' ).getOrCreate() lowerCamelCase__ = spark.range(30 ).repartition(3 ) # Mock the generator so that shuffle reverses the partition indices. with patch('numpy.random.Generator' ) as generator_mock: lowerCamelCase__ = lambda _a : x.reverse() lowerCamelCase__ = _get_expected_row_ids_and_row_dicts_for_partition_order(_a , [2, 1, 0] ) lowerCamelCase__ = SparkExamplesIterable(_a ).shuffle_data_sources(_a ) assert shuffled_it.n_shards == 3 for i, (row_id, row_dict) in enumerate(_a ): lowerCamelCase__ , lowerCamelCase__ = expected_row_ids_and_row_dicts[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def snake_case ( )-> Optional[int]: '''simple docstring''' lowerCamelCase__ = pyspark.sql.SparkSession.builder.master('local[*]' ).appName('pyspark' ).getOrCreate() lowerCamelCase__ = spark.range(20 ).repartition(4 ) # Partitions 0 and 2 lowerCamelCase__ = SparkExamplesIterable(_a ).shard_data_sources(worker_id=0 , num_workers=2 ) assert shard_it_a.n_shards == 2 lowerCamelCase__ = _get_expected_row_ids_and_row_dicts_for_partition_order(_a , [0, 2] ) for i, (row_id, row_dict) in enumerate(_a ): lowerCamelCase__ , lowerCamelCase__ = expected_row_ids_and_row_dicts_a[i] assert row_id == expected_row_id assert row_dict == expected_row_dict # Partitions 1 and 3 lowerCamelCase__ = SparkExamplesIterable(_a ).shard_data_sources(worker_id=1 , num_workers=2 ) assert shard_it_a.n_shards == 2 lowerCamelCase__ = _get_expected_row_ids_and_row_dicts_for_partition_order(_a , [1, 3] ) for i, (row_id, row_dict) in enumerate(_a ): lowerCamelCase__ , lowerCamelCase__ = expected_row_ids_and_row_dicts_a[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def snake_case ( )-> Optional[Any]: '''simple docstring''' lowerCamelCase__ = pyspark.sql.SparkSession.builder.master('local[*]' ).appName('pyspark' ).getOrCreate() lowerCamelCase__ = spark.range(100 ).repartition(1 ) lowerCamelCase__ = Spark(_a ) # Choose a small max_shard_size for maximum partitioning. spark_builder._repartition_df_if_needed(max_shard_size=1 ) # The new number of partitions should not be greater than the number of rows. assert spark_builder.df.rdd.getNumPartitions() == 100
659
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging _snake_case = logging.get_logger(__name__) _snake_case = { "microsoft/swinv2-tiny-patch4-window8-256": ( "https://huggingface.co/microsoft/swinv2-tiny-patch4-window8-256/resolve/main/config.json" ), } class _a ( SCREAMING_SNAKE_CASE_ ): a_ : Union[str, Any] = 'swinv2' a_ : Optional[int] = { 'num_attention_heads': 'num_heads', 'num_hidden_layers': 'num_layers', } def __init__( self : List[str] , SCREAMING_SNAKE_CASE__ : int=2_24 , SCREAMING_SNAKE_CASE__ : str=4 , SCREAMING_SNAKE_CASE__ : List[Any]=3 , SCREAMING_SNAKE_CASE__ : str=96 , SCREAMING_SNAKE_CASE__ : Dict=[2, 2, 6, 2] , SCREAMING_SNAKE_CASE__ : Union[str, Any]=[3, 6, 12, 24] , SCREAMING_SNAKE_CASE__ : Tuple=7 , SCREAMING_SNAKE_CASE__ : Union[str, Any]=4.0 , SCREAMING_SNAKE_CASE__ : List[str]=True , SCREAMING_SNAKE_CASE__ : List[Any]=0.0 , SCREAMING_SNAKE_CASE__ : Optional[int]=0.0 , SCREAMING_SNAKE_CASE__ : Dict=0.1 , SCREAMING_SNAKE_CASE__ : Tuple="gelu" , SCREAMING_SNAKE_CASE__ : Tuple=False , SCREAMING_SNAKE_CASE__ : Any=0.02 , SCREAMING_SNAKE_CASE__ : List[Any]=1e-5 , SCREAMING_SNAKE_CASE__ : int=32 , **SCREAMING_SNAKE_CASE__ : List[str] , ): super().__init__(**SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = image_size lowerCamelCase__ = patch_size lowerCamelCase__ = num_channels lowerCamelCase__ = embed_dim lowerCamelCase__ = depths lowerCamelCase__ = len(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = num_heads lowerCamelCase__ = window_size lowerCamelCase__ = mlp_ratio lowerCamelCase__ = qkv_bias lowerCamelCase__ = hidden_dropout_prob lowerCamelCase__ = attention_probs_dropout_prob lowerCamelCase__ = drop_path_rate lowerCamelCase__ = hidden_act lowerCamelCase__ = use_absolute_embeddings lowerCamelCase__ = layer_norm_eps lowerCamelCase__ = initializer_range lowerCamelCase__ = encoder_stride # we set the hidden_size attribute in order to make Swinv2 work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model lowerCamelCase__ = int(embed_dim * 2 ** (len(SCREAMING_SNAKE_CASE__ ) - 1) ) lowerCamelCase__ = (0, 0, 0, 0)
659
1
"""simple docstring""" import math from dataclasses import dataclass from typing import Optional, Tuple, Union import numpy as np import torch from ..configuration_utils import ConfigMixin, register_to_config from ..utils import BaseOutput, randn_tensor from .scheduling_utils import SchedulerMixin @dataclass # Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->UnCLIP class _a ( SCREAMING_SNAKE_CASE_ ): a_ : torch.FloatTensor a_ : Optional[torch.FloatTensor] = None def snake_case ( _a: List[str] , _a: Dict=0.999 , _a: Union[str, Any]="cosine" , )-> Any: '''simple docstring''' if alpha_transform_type == "cosine": def alpha_bar_fn(_a: str ): return math.cos((t + 0.008) / 1.008 * math.pi / 2 ) ** 2 elif alpha_transform_type == "exp": def alpha_bar_fn(_a: Union[str, Any] ): return math.exp(t * -12.0 ) else: raise ValueError(F'Unsupported alpha_tranform_type: {alpha_transform_type}' ) lowerCamelCase__ = [] for i in range(_a ): lowerCamelCase__ = i / num_diffusion_timesteps lowerCamelCase__ = (i + 1) / num_diffusion_timesteps betas.append(min(1 - alpha_bar_fn(_a ) / alpha_bar_fn(_a ) , _a ) ) return torch.tensor(_a , dtype=torch.floataa ) class _a ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): @register_to_config def __init__( self : Union[str, Any] , SCREAMING_SNAKE_CASE__ : int = 10_00 , SCREAMING_SNAKE_CASE__ : str = "fixed_small_log" , SCREAMING_SNAKE_CASE__ : bool = True , SCREAMING_SNAKE_CASE__ : Optional[float] = 1.0 , SCREAMING_SNAKE_CASE__ : str = "epsilon" , SCREAMING_SNAKE_CASE__ : str = "squaredcos_cap_v2" , ): if beta_schedule != "squaredcos_cap_v2": raise ValueError('UnCLIPScheduler only supports `beta_schedule`: \'squaredcos_cap_v2\'' ) lowerCamelCase__ = betas_for_alpha_bar(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = 1.0 - self.betas lowerCamelCase__ = torch.cumprod(self.alphas , dim=0 ) lowerCamelCase__ = torch.tensor(1.0 ) # standard deviation of the initial noise distribution lowerCamelCase__ = 1.0 # setable values lowerCamelCase__ = None lowerCamelCase__ = torch.from_numpy(np.arange(0 , SCREAMING_SNAKE_CASE__ )[::-1].copy() ) lowerCamelCase__ = variance_type def _UpperCamelCase ( self : Dict , SCREAMING_SNAKE_CASE__ : torch.FloatTensor , SCREAMING_SNAKE_CASE__ : Optional[int] = None ): return sample def _UpperCamelCase ( self : int , SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : Union[str, torch.device] = None ): lowerCamelCase__ = num_inference_steps lowerCamelCase__ = (self.config.num_train_timesteps - 1) / (self.num_inference_steps - 1) lowerCamelCase__ = (np.arange(0 , SCREAMING_SNAKE_CASE__ ) * step_ratio).round()[::-1].copy().astype(np.intaa ) lowerCamelCase__ = torch.from_numpy(SCREAMING_SNAKE_CASE__ ).to(SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : int , SCREAMING_SNAKE_CASE__ : Optional[Any] , SCREAMING_SNAKE_CASE__ : Optional[int]=None , SCREAMING_SNAKE_CASE__ : Union[str, Any]=None , SCREAMING_SNAKE_CASE__ : Optional[int]=None ): if prev_timestep is None: lowerCamelCase__ = t - 1 lowerCamelCase__ = self.alphas_cumprod[t] lowerCamelCase__ = self.alphas_cumprod[prev_timestep] if prev_timestep >= 0 else self.one lowerCamelCase__ = 1 - alpha_prod_t lowerCamelCase__ = 1 - alpha_prod_t_prev if prev_timestep == t - 1: lowerCamelCase__ = self.betas[t] else: lowerCamelCase__ = 1 - alpha_prod_t / alpha_prod_t_prev # For t > 0, compute predicted variance βt (see formula (6) and (7) from https://arxiv.org/pdf/2006.11239.pdf) # and sample from it to get previous sample # x_{t-1} ~ N(pred_prev_sample, variance) == add variance to pred_sample lowerCamelCase__ = beta_prod_t_prev / beta_prod_t * beta if variance_type is None: lowerCamelCase__ = self.config.variance_type # hacks - were probably added for training stability if variance_type == "fixed_small_log": lowerCamelCase__ = torch.log(torch.clamp(SCREAMING_SNAKE_CASE__ , min=1e-20 ) ) lowerCamelCase__ = torch.exp(0.5 * variance ) elif variance_type == "learned_range": # NOTE difference with DDPM scheduler lowerCamelCase__ = variance.log() lowerCamelCase__ = beta.log() lowerCamelCase__ = (predicted_variance + 1) / 2 lowerCamelCase__ = frac * max_log + (1 - frac) * min_log return variance def _UpperCamelCase ( self : Tuple , SCREAMING_SNAKE_CASE__ : torch.FloatTensor , SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : torch.FloatTensor , SCREAMING_SNAKE_CASE__ : Optional[int] = None , SCREAMING_SNAKE_CASE__ : Tuple=None , SCREAMING_SNAKE_CASE__ : bool = True , ): lowerCamelCase__ = timestep if model_output.shape[1] == sample.shape[1] * 2 and self.variance_type == "learned_range": lowerCamelCase__ , lowerCamelCase__ = torch.split(SCREAMING_SNAKE_CASE__ , sample.shape[1] , dim=1 ) else: lowerCamelCase__ = None # 1. compute alphas, betas if prev_timestep is None: lowerCamelCase__ = t - 1 lowerCamelCase__ = self.alphas_cumprod[t] lowerCamelCase__ = self.alphas_cumprod[prev_timestep] if prev_timestep >= 0 else self.one lowerCamelCase__ = 1 - alpha_prod_t lowerCamelCase__ = 1 - alpha_prod_t_prev if prev_timestep == t - 1: lowerCamelCase__ = self.betas[t] lowerCamelCase__ = self.alphas[t] else: lowerCamelCase__ = 1 - alpha_prod_t / alpha_prod_t_prev lowerCamelCase__ = 1 - beta # 2. compute predicted original sample from predicted noise also called # "predicted x_0" of formula (15) from https://arxiv.org/pdf/2006.11239.pdf if self.config.prediction_type == "epsilon": lowerCamelCase__ = (sample - beta_prod_t ** 0.5 * model_output) / alpha_prod_t ** 0.5 elif self.config.prediction_type == "sample": lowerCamelCase__ = model_output else: raise ValueError( F'prediction_type given as {self.config.prediction_type} must be one of `epsilon` or `sample`' ' for the UnCLIPScheduler.' ) # 3. Clip "predicted x_0" if self.config.clip_sample: lowerCamelCase__ = torch.clamp( SCREAMING_SNAKE_CASE__ , -self.config.clip_sample_range , self.config.clip_sample_range ) # 4. Compute coefficients for pred_original_sample x_0 and current sample x_t # See formula (7) from https://arxiv.org/pdf/2006.11239.pdf lowerCamelCase__ = (alpha_prod_t_prev ** 0.5 * beta) / beta_prod_t lowerCamelCase__ = alpha ** 0.5 * beta_prod_t_prev / beta_prod_t # 5. Compute predicted previous sample µ_t # See formula (7) from https://arxiv.org/pdf/2006.11239.pdf lowerCamelCase__ = pred_original_sample_coeff * pred_original_sample + current_sample_coeff * sample # 6. Add noise lowerCamelCase__ = 0 if t > 0: lowerCamelCase__ = randn_tensor( model_output.shape , dtype=model_output.dtype , generator=SCREAMING_SNAKE_CASE__ , device=model_output.device ) lowerCamelCase__ = self._get_variance( SCREAMING_SNAKE_CASE__ , predicted_variance=SCREAMING_SNAKE_CASE__ , prev_timestep=SCREAMING_SNAKE_CASE__ , ) if self.variance_type == "fixed_small_log": lowerCamelCase__ = variance elif self.variance_type == "learned_range": lowerCamelCase__ = (0.5 * variance).exp() else: raise ValueError( F'variance_type given as {self.variance_type} must be one of `fixed_small_log` or `learned_range`' ' for the UnCLIPScheduler.' ) lowerCamelCase__ = variance * variance_noise lowerCamelCase__ = pred_prev_sample + variance if not return_dict: return (pred_prev_sample,) return UnCLIPSchedulerOutput(prev_sample=SCREAMING_SNAKE_CASE__ , pred_original_sample=SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : Optional[Any] , SCREAMING_SNAKE_CASE__ : torch.FloatTensor , SCREAMING_SNAKE_CASE__ : torch.FloatTensor , SCREAMING_SNAKE_CASE__ : torch.IntTensor , ): # Make sure alphas_cumprod and timestep have same device and dtype as original_samples lowerCamelCase__ = self.alphas_cumprod.to(device=original_samples.device , dtype=original_samples.dtype ) lowerCamelCase__ = timesteps.to(original_samples.device ) lowerCamelCase__ = alphas_cumprod[timesteps] ** 0.5 lowerCamelCase__ = sqrt_alpha_prod.flatten() while len(sqrt_alpha_prod.shape ) < len(original_samples.shape ): lowerCamelCase__ = sqrt_alpha_prod.unsqueeze(-1 ) lowerCamelCase__ = (1 - alphas_cumprod[timesteps]) ** 0.5 lowerCamelCase__ = sqrt_one_minus_alpha_prod.flatten() while len(sqrt_one_minus_alpha_prod.shape ) < len(original_samples.shape ): lowerCamelCase__ = sqrt_one_minus_alpha_prod.unsqueeze(-1 ) lowerCamelCase__ = sqrt_alpha_prod * original_samples + sqrt_one_minus_alpha_prod * noise return noisy_samples
659
"""simple docstring""" def snake_case ( _a: int )-> int: '''simple docstring''' lowerCamelCase__ = 1 for i in range(1 , num + 1 ): fact *= i return fact def snake_case ( _a: int )-> int: '''simple docstring''' lowerCamelCase__ = 0 while number > 0: lowerCamelCase__ = number % 10 sum_of_digits += last_digit lowerCamelCase__ = number // 10 # Removing the last_digit from the given number return sum_of_digits def snake_case ( _a: int = 100 )-> int: '''simple docstring''' lowerCamelCase__ = factorial(_a ) lowerCamelCase__ = split_and_add(_a ) return result if __name__ == "__main__": print(solution(int(input("Enter the Number: ").strip())))
659
1
"""simple docstring""" 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 _snake_case = re.compile("[^A-Za-z_0-9]") # parameters used in DuplicationIndex _snake_case = 10 _snake_case = 256 def snake_case ( _a: List[str] )-> Optional[MinHash]: '''simple docstring''' if len(_a ) < MIN_NUM_TOKENS: return None lowerCamelCase__ = MinHash(num_perm=_a ) for token in set(_a ): min_hash.update(token.encode() ) return min_hash def snake_case ( _a: str )-> Set[str]: '''simple docstring''' return {t for t in NON_ALPHA.split(_a ) if len(t.strip() ) > 0} class _a : def __init__( self : List[Any] , *, SCREAMING_SNAKE_CASE__ : float = 0.85 , ): lowerCamelCase__ = duplication_jaccard_threshold lowerCamelCase__ = NUM_PERM lowerCamelCase__ = MinHashLSH(threshold=self._duplication_jaccard_threshold , num_perm=self._num_perm ) lowerCamelCase__ = defaultdict(SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : List[Any] , SCREAMING_SNAKE_CASE__ : Tuple , SCREAMING_SNAKE_CASE__ : MinHash ): lowerCamelCase__ = self._index.query(SCREAMING_SNAKE_CASE__ ) if code_key in self._index.keys: print(F'Duplicate key {code_key}' ) return self._index.insert(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) if len(SCREAMING_SNAKE_CASE__ ) > 0: for base_duplicate in close_duplicates: if base_duplicate in self._duplicate_clusters: self._duplicate_clusters[base_duplicate].add(SCREAMING_SNAKE_CASE__ ) break else: self._duplicate_clusters[close_duplicates[0]].add(SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : str ): lowerCamelCase__ = [] for base, duplicates in self._duplicate_clusters.items(): lowerCamelCase__ = [base] + list(SCREAMING_SNAKE_CASE__ ) # reformat the cluster to be a list of dict lowerCamelCase__ = [{'base_index': el[0], 'repo_name': el[1], 'path': el[2]} for el in cluster] duplicate_clusters.append(SCREAMING_SNAKE_CASE__ ) return duplicate_clusters def _UpperCamelCase ( self : List[str] , SCREAMING_SNAKE_CASE__ : Union[str, Any] ): lowerCamelCase__ = self.get_duplicate_clusters() with open(SCREAMING_SNAKE_CASE__ , 'w' ) as f: json.dump(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) def snake_case ( _a: Union[str, Any] )-> Optional[int]: '''simple docstring''' lowerCamelCase__ , lowerCamelCase__ = element lowerCamelCase__ = 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 snake_case ( _a: Type[Dataset] )-> Tuple: '''simple docstring''' with mp.Pool() as pool: for data in pool.imap_unordered( _compute_min_hash , ThreadedIterator(_a , max_queue_size=10000 ) , chunksize=100 , ): if data is not None: yield data def snake_case ( _a: Type[Dataset] , _a: float )-> Optional[int]: '''simple docstring''' lowerCamelCase__ = 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 snake_case ( _a: str , _a: str )-> float: '''simple docstring''' lowerCamelCase__ = get_tokens(_a ) lowerCamelCase__ = get_tokens(_a ) return len(tokensa & tokensa ) / len(tokensa | tokensa ) _snake_case = None def snake_case ( _a: Dict , _a: Union[str, Any] )-> List[str]: '''simple docstring''' lowerCamelCase__ = [] for elementa in cluster: lowerCamelCase__ = _shared_dataset[elementa['base_index']]['content'] for elementa in extremes: lowerCamelCase__ = _shared_dataset[elementa['base_index']]['content'] if jaccard_similarity(_a , _a ) >= jaccard_threshold: elementa["copies"] += 1 break else: lowerCamelCase__ = 1 extremes.append(_a ) return extremes def snake_case ( _a: Any , _a: Tuple , _a: Dict )-> Union[str, Any]: '''simple docstring''' global _shared_dataset lowerCamelCase__ = dataset lowerCamelCase__ = [] lowerCamelCase__ = 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 snake_case ( _a: Type[Dataset] , _a: float = 0.85 )-> Tuple[Type[Dataset], List[List[Dict]]]: '''simple docstring''' lowerCamelCase__ = make_duplicate_clusters(_a , _a ) lowerCamelCase__ = {x['base_index'] for cluster in duplicate_clusters for x in cluster} lowerCamelCase__ = {} lowerCamelCase__ = find_extremes(_a , _a , _a ) for extremes in extremes_clusters: for element in extremes: lowerCamelCase__ = element lowerCamelCase__ = duplicate_indices - set(extreme_dict.keys() ) lowerCamelCase__ = 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: lowerCamelCase__ = element['base_index'] in extreme_dict if element["is_extreme"]: lowerCamelCase__ = 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
659
"""simple docstring""" 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 _snake_case = re.compile("[^A-Za-z_0-9]") # parameters used in DuplicationIndex _snake_case = 10 _snake_case = 256 def snake_case ( _a: List[str] )-> Optional[MinHash]: '''simple docstring''' if len(_a ) < MIN_NUM_TOKENS: return None lowerCamelCase__ = MinHash(num_perm=_a ) for token in set(_a ): min_hash.update(token.encode() ) return min_hash def snake_case ( _a: str )-> Set[str]: '''simple docstring''' return {t for t in NON_ALPHA.split(_a ) if len(t.strip() ) > 0} class _a : def __init__( self : List[Any] , *, SCREAMING_SNAKE_CASE__ : float = 0.85 , ): lowerCamelCase__ = duplication_jaccard_threshold lowerCamelCase__ = NUM_PERM lowerCamelCase__ = MinHashLSH(threshold=self._duplication_jaccard_threshold , num_perm=self._num_perm ) lowerCamelCase__ = defaultdict(SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : List[Any] , SCREAMING_SNAKE_CASE__ : Tuple , SCREAMING_SNAKE_CASE__ : MinHash ): lowerCamelCase__ = self._index.query(SCREAMING_SNAKE_CASE__ ) if code_key in self._index.keys: print(F'Duplicate key {code_key}' ) return self._index.insert(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) if len(SCREAMING_SNAKE_CASE__ ) > 0: for base_duplicate in close_duplicates: if base_duplicate in self._duplicate_clusters: self._duplicate_clusters[base_duplicate].add(SCREAMING_SNAKE_CASE__ ) break else: self._duplicate_clusters[close_duplicates[0]].add(SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : str ): lowerCamelCase__ = [] for base, duplicates in self._duplicate_clusters.items(): lowerCamelCase__ = [base] + list(SCREAMING_SNAKE_CASE__ ) # reformat the cluster to be a list of dict lowerCamelCase__ = [{'base_index': el[0], 'repo_name': el[1], 'path': el[2]} for el in cluster] duplicate_clusters.append(SCREAMING_SNAKE_CASE__ ) return duplicate_clusters def _UpperCamelCase ( self : List[str] , SCREAMING_SNAKE_CASE__ : Union[str, Any] ): lowerCamelCase__ = self.get_duplicate_clusters() with open(SCREAMING_SNAKE_CASE__ , 'w' ) as f: json.dump(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) def snake_case ( _a: Union[str, Any] )-> Optional[int]: '''simple docstring''' lowerCamelCase__ , lowerCamelCase__ = element lowerCamelCase__ = 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 snake_case ( _a: Type[Dataset] )-> Tuple: '''simple docstring''' with mp.Pool() as pool: for data in pool.imap_unordered( _compute_min_hash , ThreadedIterator(_a , max_queue_size=10000 ) , chunksize=100 , ): if data is not None: yield data def snake_case ( _a: Type[Dataset] , _a: float )-> Optional[int]: '''simple docstring''' lowerCamelCase__ = 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 snake_case ( _a: str , _a: str )-> float: '''simple docstring''' lowerCamelCase__ = get_tokens(_a ) lowerCamelCase__ = get_tokens(_a ) return len(tokensa & tokensa ) / len(tokensa | tokensa ) _snake_case = None def snake_case ( _a: Dict , _a: Union[str, Any] )-> List[str]: '''simple docstring''' lowerCamelCase__ = [] for elementa in cluster: lowerCamelCase__ = _shared_dataset[elementa['base_index']]['content'] for elementa in extremes: lowerCamelCase__ = _shared_dataset[elementa['base_index']]['content'] if jaccard_similarity(_a , _a ) >= jaccard_threshold: elementa["copies"] += 1 break else: lowerCamelCase__ = 1 extremes.append(_a ) return extremes def snake_case ( _a: Any , _a: Tuple , _a: Dict )-> Union[str, Any]: '''simple docstring''' global _shared_dataset lowerCamelCase__ = dataset lowerCamelCase__ = [] lowerCamelCase__ = 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 snake_case ( _a: Type[Dataset] , _a: float = 0.85 )-> Tuple[Type[Dataset], List[List[Dict]]]: '''simple docstring''' lowerCamelCase__ = make_duplicate_clusters(_a , _a ) lowerCamelCase__ = {x['base_index'] for cluster in duplicate_clusters for x in cluster} lowerCamelCase__ = {} lowerCamelCase__ = find_extremes(_a , _a , _a ) for extremes in extremes_clusters: for element in extremes: lowerCamelCase__ = element lowerCamelCase__ = duplicate_indices - set(extreme_dict.keys() ) lowerCamelCase__ = 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: lowerCamelCase__ = element['base_index'] in extreme_dict if element["is_extreme"]: lowerCamelCase__ = 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
659
1
"""simple docstring""" import html from ...feature_extraction_utils import BatchFeature, FeatureExtractionMixin from ...utils import is_bsa_available, logging, requires_backends if is_bsa_available(): import bsa from bsa import BeautifulSoup _snake_case = logging.get_logger(__name__) class _a ( SCREAMING_SNAKE_CASE_ ): def __init__( self : Optional[Any] , **SCREAMING_SNAKE_CASE__ : Dict ): requires_backends(self , ['bs4'] ) super().__init__(**SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : Optional[Any] , SCREAMING_SNAKE_CASE__ : List[Any] ): lowerCamelCase__ = [] lowerCamelCase__ = [] lowerCamelCase__ = element if element.name else element.parent for parent in child.parents: # type: bs4.element.Tag lowerCamelCase__ = parent.find_all(child.name , recursive=SCREAMING_SNAKE_CASE__ ) xpath_tags.append(child.name ) xpath_subscripts.append( 0 if 1 == len(SCREAMING_SNAKE_CASE__ ) else next(i for i, s in enumerate(SCREAMING_SNAKE_CASE__ , 1 ) if s is child ) ) lowerCamelCase__ = parent xpath_tags.reverse() xpath_subscripts.reverse() return xpath_tags, xpath_subscripts def _UpperCamelCase ( self : Any , SCREAMING_SNAKE_CASE__ : Optional[Any] ): lowerCamelCase__ = BeautifulSoup(SCREAMING_SNAKE_CASE__ , 'html.parser' ) lowerCamelCase__ = [] lowerCamelCase__ = [] lowerCamelCase__ = [] for element in html_code.descendants: if type(SCREAMING_SNAKE_CASE__ ) == bsa.element.NavigableString: if type(element.parent ) != bsa.element.Tag: continue lowerCamelCase__ = html.unescape(SCREAMING_SNAKE_CASE__ ).strip() if not text_in_this_tag: continue all_doc_strings.append(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ , lowerCamelCase__ = self.xpath_soup(SCREAMING_SNAKE_CASE__ ) stringaxtag_seq.append(SCREAMING_SNAKE_CASE__ ) stringaxsubs_seq.append(SCREAMING_SNAKE_CASE__ ) if len(SCREAMING_SNAKE_CASE__ ) != len(SCREAMING_SNAKE_CASE__ ): raise ValueError('Number of doc strings and xtags does not correspond' ) if len(SCREAMING_SNAKE_CASE__ ) != len(SCREAMING_SNAKE_CASE__ ): raise ValueError('Number of doc strings and xsubs does not correspond' ) return all_doc_strings, stringaxtag_seq, stringaxsubs_seq def _UpperCamelCase ( self : Dict , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : Any ): lowerCamelCase__ = '' for tagname, subs in zip(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): xpath += F'/{tagname}' if subs != 0: xpath += F'[{subs}]' return xpath def __call__( self : Tuple , SCREAMING_SNAKE_CASE__ : List[Any] ): lowerCamelCase__ = False # Check that strings has a valid type if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): lowerCamelCase__ = True elif isinstance(SCREAMING_SNAKE_CASE__ , (list, tuple) ): if len(SCREAMING_SNAKE_CASE__ ) == 0 or isinstance(html_strings[0] , SCREAMING_SNAKE_CASE__ ): lowerCamelCase__ = True if not valid_strings: raise ValueError( 'HTML strings must of type `str`, `List[str]` (batch of examples), ' F'but is of type {type(SCREAMING_SNAKE_CASE__ )}.' ) lowerCamelCase__ = bool(isinstance(SCREAMING_SNAKE_CASE__ , (list, tuple) ) and (isinstance(html_strings[0] , SCREAMING_SNAKE_CASE__ )) ) if not is_batched: lowerCamelCase__ = [html_strings] # Get nodes + xpaths lowerCamelCase__ = [] lowerCamelCase__ = [] for html_string in html_strings: lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ = self.get_three_from_single(SCREAMING_SNAKE_CASE__ ) nodes.append(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = [] for node, tag_list, sub_list in zip(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): lowerCamelCase__ = self.construct_xpath(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) xpath_strings.append(SCREAMING_SNAKE_CASE__ ) xpaths.append(SCREAMING_SNAKE_CASE__ ) # return as Dict lowerCamelCase__ = {'nodes': nodes, 'xpaths': xpaths} lowerCamelCase__ = BatchFeature(data=SCREAMING_SNAKE_CASE__ , tensor_type=SCREAMING_SNAKE_CASE__ ) return encoded_inputs
659
"""simple docstring""" import collections import json import math import os import re import time from fnmatch import fnmatch from typing import Dict import requests from slack_sdk import WebClient _snake_case = WebClient(token=os.environ["CI_SLACK_BOT_TOKEN"]) def snake_case ( _a: Any )-> Optional[Any]: '''simple docstring''' lowerCamelCase__ = test_results.split(' ' ) lowerCamelCase__ = 0 lowerCamelCase__ = 0 # When the output is short enough, the output is surrounded by = signs: "== OUTPUT ==" # When it is too long, those signs are not present. lowerCamelCase__ = expressions[-2] if '=' in expressions[-1] else expressions[-1] for i, expression in enumerate(_a ): if "failed" in expression: failed += int(expressions[i - 1] ) if "passed" in expression: success += int(expressions[i - 1] ) return failed, success, time_spent def snake_case ( _a: Optional[int] )-> Optional[int]: '''simple docstring''' lowerCamelCase__ = {} lowerCamelCase__ = None lowerCamelCase__ = False for line in failures_short_lines.split('\n' ): if re.search(R'_ \[doctest\]' , _a ): lowerCamelCase__ = True lowerCamelCase__ = line.split(' ' )[2] elif in_error and not line.split(' ' )[0].isdigit(): lowerCamelCase__ = line lowerCamelCase__ = False return failures class _a : def __init__( self : Optional[Any] , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : Dict ): lowerCamelCase__ = title lowerCamelCase__ = doc_test_results['time_spent'].split(',' )[0] lowerCamelCase__ = doc_test_results['success'] lowerCamelCase__ = doc_test_results['failures'] lowerCamelCase__ = self.n_success + self.n_failures # Failures and success of the modeling tests lowerCamelCase__ = doc_test_results @property def _UpperCamelCase ( self : List[str] ): lowerCamelCase__ = [self._time_spent] lowerCamelCase__ = 0 for time in time_spent: lowerCamelCase__ = time.split(':' ) # Time can be formatted as xx:xx:xx, as .xx, or as x.xx if the time spent was less than a minute. if len(SCREAMING_SNAKE_CASE__ ) == 1: lowerCamelCase__ = [0, 0, time_parts[0]] lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ = int(time_parts[0] ), int(time_parts[1] ), float(time_parts[2] ) total_secs += hours * 36_00 + minutes * 60 + seconds lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ = total_secs // 36_00, (total_secs % 36_00) // 60, total_secs % 60 return F'{int(SCREAMING_SNAKE_CASE__ )}h{int(SCREAMING_SNAKE_CASE__ )}m{int(SCREAMING_SNAKE_CASE__ )}s' @property def _UpperCamelCase ( self : Dict ): return {"type": "header", "text": {"type": "plain_text", "text": self.title}} @property def _UpperCamelCase ( self : Dict ): return { "type": "section", "text": { "type": "plain_text", "text": F'🌞 There were no failures: all {self.n_tests} tests passed. The suite ran in {self.time}.', "emoji": True, }, "accessory": { "type": "button", "text": {"type": "plain_text", "text": "Check Action results", "emoji": True}, "url": F'https://github.com/huggingface/transformers/actions/runs/{os.environ["GITHUB_RUN_ID"]}', }, } @property def _UpperCamelCase ( self : Any ): return { "type": "section", "text": { "type": "plain_text", "text": ( F'There were {self.n_failures} failures, out of {self.n_tests} tests.\nThe suite ran in' F' {self.time}.' ), "emoji": True, }, "accessory": { "type": "button", "text": {"type": "plain_text", "text": "Check Action results", "emoji": True}, "url": F'https://github.com/huggingface/transformers/actions/runs/{os.environ["GITHUB_RUN_ID"]}', }, } @property def _UpperCamelCase ( self : Union[str, Any] ): lowerCamelCase__ = 40 lowerCamelCase__ = {k: v['failed'] for k, v in doc_test_results.items() if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ )} lowerCamelCase__ = '' for category, failures in category_failures.items(): if len(SCREAMING_SNAKE_CASE__ ) == 0: continue if report != "": report += "\n\n" report += F'*{category} failures*:'.ljust(line_length // 2 ).rjust(line_length // 2 ) + "\n" report += "`" report += "`\n`".join(SCREAMING_SNAKE_CASE__ ) report += "`" return { "type": "section", "text": { "type": "mrkdwn", "text": F'The following examples had failures:\n\n\n{report}\n', }, } @property def _UpperCamelCase ( self : str ): lowerCamelCase__ = [self.header] if self.n_failures > 0: blocks.append(self.failures ) if self.n_failures > 0: blocks.extend([self.category_failures] ) if self.n_failures == 0: blocks.append(self.no_failures ) return json.dumps(SCREAMING_SNAKE_CASE__ ) @staticmethod def _UpperCamelCase ( ): lowerCamelCase__ = [ { 'type': 'section', 'text': { 'type': 'plain_text', 'text': 'There was an issue running the tests.', }, 'accessory': { 'type': 'button', 'text': {'type': 'plain_text', 'text': 'Check Action results', 'emoji': True}, 'url': F'https://github.com/huggingface/transformers/actions/runs/{os.environ["GITHUB_RUN_ID"]}', }, } ] print('Sending the following payload' ) print(json.dumps({'blocks': json.loads(SCREAMING_SNAKE_CASE__ )} ) ) client.chat_postMessage( channel=os.environ['CI_SLACK_CHANNEL_ID_DAILY'] , text='There was an issue running the tests.' , blocks=SCREAMING_SNAKE_CASE__ , ) def _UpperCamelCase ( self : Optional[int] ): print('Sending the following payload' ) print(json.dumps({'blocks': json.loads(self.payload )} ) ) lowerCamelCase__ = F'{self.n_failures} failures out of {self.n_tests} tests,' if self.n_failures else 'All tests passed.' lowerCamelCase__ = client.chat_postMessage( channel=os.environ['CI_SLACK_CHANNEL_ID_DAILY'] , blocks=self.payload , text=SCREAMING_SNAKE_CASE__ , ) def _UpperCamelCase ( self : Optional[Any] , SCREAMING_SNAKE_CASE__ : Dict , SCREAMING_SNAKE_CASE__ : List[Any] , SCREAMING_SNAKE_CASE__ : Tuple , SCREAMING_SNAKE_CASE__ : Tuple ): lowerCamelCase__ = '' for key, value in failures.items(): lowerCamelCase__ = value[:2_00] + ' [Truncated]' if len(SCREAMING_SNAKE_CASE__ ) > 2_50 else value failures_text += F'*{key}*\n_{value}_\n\n' lowerCamelCase__ = job_name lowerCamelCase__ = {'type': 'section', 'text': {'type': 'mrkdwn', 'text': text}} if job_link is not None: lowerCamelCase__ = { 'type': 'button', 'text': {'type': 'plain_text', 'text': 'GitHub Action job', 'emoji': True}, 'url': job_link, } return [ {"type": "header", "text": {"type": "plain_text", "text": title.upper(), "emoji": True}}, content, {"type": "section", "text": {"type": "mrkdwn", "text": failures_text}}, ] def _UpperCamelCase ( self : Optional[int] ): if self.thread_ts is None: raise ValueError('Can only post reply if a post has been made.' ) lowerCamelCase__ = self.doc_test_results.pop('job_link' ) self.doc_test_results.pop('failures' ) self.doc_test_results.pop('success' ) self.doc_test_results.pop('time_spent' ) lowerCamelCase__ = sorted(self.doc_test_results.items() , key=lambda SCREAMING_SNAKE_CASE__ : t[0] ) for job, job_result in sorted_dict: if len(job_result['failures'] ): lowerCamelCase__ = F'*Num failures* :{len(job_result["failed"] )} \n' lowerCamelCase__ = job_result['failures'] lowerCamelCase__ = self.get_reply_blocks(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , text=SCREAMING_SNAKE_CASE__ ) print('Sending the following reply' ) print(json.dumps({'blocks': blocks} ) ) client.chat_postMessage( channel=os.environ['CI_SLACK_CHANNEL_ID_DAILY'] , text=F'Results for {job}' , blocks=SCREAMING_SNAKE_CASE__ , thread_ts=self.thread_ts['ts'] , ) time.sleep(1 ) def snake_case ( )-> Optional[Any]: '''simple docstring''' lowerCamelCase__ = os.environ['GITHUB_RUN_ID'] lowerCamelCase__ = F'https://api.github.com/repos/huggingface/transformers/actions/runs/{run_id}/jobs?per_page=100' lowerCamelCase__ = requests.get(_a ).json() lowerCamelCase__ = {} try: jobs.update({job['name']: job['html_url'] for job in result['jobs']} ) lowerCamelCase__ = math.ceil((result['total_count'] - 100) / 100 ) for i in range(_a ): lowerCamelCase__ = requests.get(url + F'&page={i + 2}' ).json() jobs.update({job['name']: job['html_url'] for job in result['jobs']} ) return jobs except Exception as e: print('Unknown error, could not fetch links.' , _a ) return {} def snake_case ( _a: str )-> Dict: '''simple docstring''' lowerCamelCase__ = {} if os.path.exists(_a ): lowerCamelCase__ = os.listdir(_a ) for file in files: try: with open(os.path.join(_a , _a ) , encoding='utf-8' ) as f: lowerCamelCase__ = f.read() except UnicodeDecodeError as e: raise ValueError(F'Could not open {os.path.join(_a , _a )}.' ) from e return _artifact def snake_case ( )-> Optional[int]: '''simple docstring''' class _a : def __init__( self : Tuple , SCREAMING_SNAKE_CASE__ : str ): lowerCamelCase__ = name lowerCamelCase__ = [] def __str__( self : Dict ): return self.name def _UpperCamelCase ( self : Union[str, Any] , SCREAMING_SNAKE_CASE__ : str ): self.paths.append({'name': self.name, 'path': path} ) lowerCamelCase__ = {} lowerCamelCase__ = filter(os.path.isdir , os.listdir() ) for directory in directories: lowerCamelCase__ = directory if artifact_name not in _available_artifacts: lowerCamelCase__ = Artifact(_a ) _available_artifacts[artifact_name].add_path(_a ) return _available_artifacts if __name__ == "__main__": _snake_case = get_job_links() _snake_case = retrieve_available_artifacts() _snake_case = collections.OrderedDict( [ ("*.py", "API Examples"), ("*.md", "MD Examples"), ] ) # This dict will contain all the information relative to each doc test category: # - failed: list of failed tests # - failures: dict in the format 'test': 'error_message' _snake_case = { v: { "failed": [], "failures": {}, } for v in docs.values() } # Link to the GitHub Action job _snake_case = github_actions_job_links.get("run_doctests") _snake_case = available_artifacts["doc_tests_gpu_test_reports"].paths[0] _snake_case = retrieve_artifact(artifact_path["name"]) if "stats" in artifact: _snake_case , _snake_case , _snake_case = handle_test_results(artifact["stats"]) _snake_case = failed _snake_case = success _snake_case = time_spent[1:-1] + ", " _snake_case = extract_first_line_failure(artifact["failures_short"]) for line in artifact["summary_short"].split("\n"): if re.search("FAILED", line): _snake_case = line.replace("FAILED ", "") _snake_case = line.split()[0].replace("\n", "") if "::" in line: _snake_case , _snake_case = line.split("::") else: _snake_case , _snake_case = line, line for file_regex in docs.keys(): if fnmatch(file_path, file_regex): _snake_case = docs[file_regex] doc_test_results[category]["failed"].append(test) _snake_case = all_failures[test] if test in all_failures else "N/A" _snake_case = failure break _snake_case = Message("🤗 Results of the doc tests.", doc_test_results) message.post() message.post_reply()
659
1
"""simple docstring""" from __future__ import annotations import math _snake_case = "2020.9.26" _snake_case = "xcodz-dot, cclaus, dhruvmanila" def snake_case ( _a: float , _a: float , _a: float , _a: float , _a: float )-> tuple[float, float]: '''simple docstring''' if not all(isinstance(_a , (float, int) ) for val in locals().values() ): lowerCamelCase__ = F'Input values must either be float or int: {list(locals().values() )}' raise TypeError(_a ) lowerCamelCase__ = ((x * distance) / (z + distance)) * scale lowerCamelCase__ = ((y * distance) / (z + distance)) * scale return projected_x, projected_y def snake_case ( _a: float , _a: float , _a: float , _a: str , _a: float )-> tuple[float, float, float]: '''simple docstring''' if not isinstance(_a , _a ): raise TypeError('Axis must be a str' ) lowerCamelCase__ = locals() del input_variables["axis"] if not all(isinstance(_a , (float, int) ) for val in input_variables.values() ): lowerCamelCase__ = ( 'Input values except axis must either be float or int: ' F'{list(input_variables.values() )}' ) raise TypeError(_a ) lowerCamelCase__ = (angle % 360) / 450 * 180 / math.pi if axis == "z": lowerCamelCase__ = x * math.cos(_a ) - y * math.sin(_a ) lowerCamelCase__ = y * math.cos(_a ) + x * math.sin(_a ) lowerCamelCase__ = z elif axis == "x": lowerCamelCase__ = y * math.cos(_a ) - z * math.sin(_a ) lowerCamelCase__ = z * math.cos(_a ) + y * math.sin(_a ) lowerCamelCase__ = x elif axis == "y": lowerCamelCase__ = x * math.cos(_a ) - z * math.sin(_a ) lowerCamelCase__ = z * math.cos(_a ) + x * math.sin(_a ) lowerCamelCase__ = y else: raise ValueError('not a valid axis, choose one of \'x\', \'y\', \'z\'' ) return new_x, new_y, new_z if __name__ == "__main__": import doctest doctest.testmod() print(f"""{convert_to_ad(1.0, 2.0, 3.0, 10.0, 10.0) = }""") print(f"""{rotate(1.0, 2.0, 3.0, "y", 90.0) = }""")
659
"""simple docstring""" 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 _snake_case = logging.get_logger(__name__) # pylint: disable=invalid-name _snake_case = "\n Examples:\n ```py\n >>> from PIL import Image\n >>> import torch\n >>> from diffusers import DiffusionPipeline\n >>> from diffusers.utils import export_to_gif, load_image\n\n >>> device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n\n >>> repo = \"openai/shap-e-img2img\"\n >>> pipe = DiffusionPipeline.from_pretrained(repo, torch_dtype=torch.float16)\n >>> pipe = pipe.to(device)\n\n >>> guidance_scale = 3.0\n >>> image_url = \"https://hf.co/datasets/diffusers/docs-images/resolve/main/shap-e/corgi.png\"\n >>> image = load_image(image_url).convert(\"RGB\")\n\n >>> images = pipe(\n ... image,\n ... guidance_scale=guidance_scale,\n ... num_inference_steps=64,\n ... frame_size=256,\n ... ).images\n\n >>> gif_path = export_to_gif(images[0], \"corgi_3d.gif\")\n ```\n" @dataclass class _a ( SCREAMING_SNAKE_CASE_ ): a_ : Union[PIL.Image.Image, np.ndarray] class _a ( SCREAMING_SNAKE_CASE_ ): def __init__( self : Tuple , SCREAMING_SNAKE_CASE__ : PriorTransformer , SCREAMING_SNAKE_CASE__ : CLIPVisionModel , SCREAMING_SNAKE_CASE__ : CLIPImageProcessor , SCREAMING_SNAKE_CASE__ : HeunDiscreteScheduler , SCREAMING_SNAKE_CASE__ : ShapERenderer , ): super().__init__() self.register_modules( prior=SCREAMING_SNAKE_CASE__ , image_encoder=SCREAMING_SNAKE_CASE__ , image_processor=SCREAMING_SNAKE_CASE__ , scheduler=SCREAMING_SNAKE_CASE__ , renderer=SCREAMING_SNAKE_CASE__ , ) def _UpperCamelCase ( self : str , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : Optional[int] , SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : List[str] ): if latents is None: lowerCamelCase__ = randn_tensor(SCREAMING_SNAKE_CASE__ , generator=SCREAMING_SNAKE_CASE__ , device=SCREAMING_SNAKE_CASE__ , dtype=SCREAMING_SNAKE_CASE__ ) else: if latents.shape != shape: raise ValueError(F'Unexpected latents shape, got {latents.shape}, expected {shape}' ) lowerCamelCase__ = latents.to(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = latents * scheduler.init_noise_sigma return latents def _UpperCamelCase ( self : List[Any] , SCREAMING_SNAKE_CASE__ : List[str]=0 ): if is_accelerate_available(): from accelerate import cpu_offload else: raise ImportError('Please install accelerate via `pip install accelerate`' ) lowerCamelCase__ = torch.device(F'cuda:{gpu_id}' ) lowerCamelCase__ = [self.image_encoder, self.prior] for cpu_offloaded_model in models: if cpu_offloaded_model is not None: cpu_offload(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) @property def _UpperCamelCase ( self : Dict ): 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(SCREAMING_SNAKE_CASE__ , '_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 _UpperCamelCase ( self : Optional[int] , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : Dict , SCREAMING_SNAKE_CASE__ : Optional[Any] , SCREAMING_SNAKE_CASE__ : int , ): if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and isinstance(image[0] , torch.Tensor ): lowerCamelCase__ = torch.cat(SCREAMING_SNAKE_CASE__ , axis=0 ) if image[0].ndim == 4 else torch.stack(SCREAMING_SNAKE_CASE__ , axis=0 ) if not isinstance(SCREAMING_SNAKE_CASE__ , torch.Tensor ): lowerCamelCase__ = self.image_processor(SCREAMING_SNAKE_CASE__ , return_tensors='pt' ).pixel_values[0].unsqueeze(0 ) lowerCamelCase__ = image.to(dtype=self.image_encoder.dtype , device=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = self.image_encoder(SCREAMING_SNAKE_CASE__ )['last_hidden_state'] lowerCamelCase__ = image_embeds[:, 1:, :].contiguous() # batch_size, dim, 256 lowerCamelCase__ = image_embeds.repeat_interleave(SCREAMING_SNAKE_CASE__ , dim=0 ) if do_classifier_free_guidance: lowerCamelCase__ = torch.zeros_like(SCREAMING_SNAKE_CASE__ ) # 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 lowerCamelCase__ = torch.cat([negative_image_embeds, image_embeds] ) return image_embeds @torch.no_grad() @replace_example_docstring(SCREAMING_SNAKE_CASE__ ) def __call__( self : List[Any] , SCREAMING_SNAKE_CASE__ : Union[PIL.Image.Image, List[PIL.Image.Image]] , SCREAMING_SNAKE_CASE__ : int = 1 , SCREAMING_SNAKE_CASE__ : int = 25 , SCREAMING_SNAKE_CASE__ : Optional[Union[torch.Generator, List[torch.Generator]]] = None , SCREAMING_SNAKE_CASE__ : Optional[torch.FloatTensor] = None , SCREAMING_SNAKE_CASE__ : float = 4.0 , SCREAMING_SNAKE_CASE__ : int = 64 , SCREAMING_SNAKE_CASE__ : Optional[str] = "pil" , SCREAMING_SNAKE_CASE__ : bool = True , ): if isinstance(SCREAMING_SNAKE_CASE__ , PIL.Image.Image ): lowerCamelCase__ = 1 elif isinstance(SCREAMING_SNAKE_CASE__ , torch.Tensor ): lowerCamelCase__ = image.shape[0] elif isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and isinstance(image[0] , (torch.Tensor, PIL.Image.Image) ): lowerCamelCase__ = len(SCREAMING_SNAKE_CASE__ ) 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(SCREAMING_SNAKE_CASE__ )}' ) lowerCamelCase__ = self._execution_device lowerCamelCase__ = batch_size * num_images_per_prompt lowerCamelCase__ = guidance_scale > 1.0 lowerCamelCase__ = self._encode_image(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) # prior self.scheduler.set_timesteps(SCREAMING_SNAKE_CASE__ , device=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = self.scheduler.timesteps lowerCamelCase__ = self.prior.config.num_embeddings lowerCamelCase__ = self.prior.config.embedding_dim lowerCamelCase__ = self.prepare_latents( (batch_size, num_embeddings * embedding_dim) , image_embeds.dtype , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , 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 lowerCamelCase__ = latents.reshape(latents.shape[0] , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) for i, t in enumerate(self.progress_bar(SCREAMING_SNAKE_CASE__ ) ): # expand the latents if we are doing classifier free guidance lowerCamelCase__ = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents lowerCamelCase__ = self.scheduler.scale_model_input(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = self.prior( SCREAMING_SNAKE_CASE__ , timestep=SCREAMING_SNAKE_CASE__ , proj_embedding=SCREAMING_SNAKE_CASE__ , ).predicted_image_embedding # remove the variance lowerCamelCase__ , lowerCamelCase__ = noise_pred.split( scaled_model_input.shape[2] , dim=2 ) # batch_size, num_embeddings, embedding_dim if do_classifier_free_guidance is not None: lowerCamelCase__ , lowerCamelCase__ = noise_pred.chunk(2 ) lowerCamelCase__ = noise_pred_uncond + guidance_scale * (noise_pred - noise_pred_uncond) lowerCamelCase__ = self.scheduler.step( SCREAMING_SNAKE_CASE__ , timestep=SCREAMING_SNAKE_CASE__ , sample=SCREAMING_SNAKE_CASE__ , ).prev_sample if output_type == "latent": return ShapEPipelineOutput(images=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = [] for i, latent in enumerate(SCREAMING_SNAKE_CASE__ ): print() lowerCamelCase__ = self.renderer.decode( latent[None, :] , SCREAMING_SNAKE_CASE__ , size=SCREAMING_SNAKE_CASE__ , ray_batch_size=40_96 , n_coarse_samples=64 , n_fine_samples=1_28 , ) images.append(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = torch.stack(SCREAMING_SNAKE_CASE__ ) if output_type not in ["np", "pil"]: raise ValueError(F'Only the output types `pil` and `np` are supported not output_type={output_type}' ) lowerCamelCase__ = images.cpu().numpy() if output_type == "pil": lowerCamelCase__ = [self.numpy_to_pil(SCREAMING_SNAKE_CASE__ ) 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=SCREAMING_SNAKE_CASE__ )
659
1
"""simple docstring""" import os _snake_case = {"I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000} def snake_case ( _a: str )-> int: '''simple docstring''' lowerCamelCase__ = 0 lowerCamelCase__ = 0 while index < len(_a ) - 1: lowerCamelCase__ = SYMBOLS[numerals[index]] lowerCamelCase__ = SYMBOLS[numerals[index + 1]] if current_value < next_value: total_value -= current_value else: total_value += current_value index += 1 total_value += SYMBOLS[numerals[index]] return total_value def snake_case ( _a: int )-> str: '''simple docstring''' lowerCamelCase__ = '' lowerCamelCase__ = num // 1000 numerals += m_count * "M" num %= 1000 lowerCamelCase__ = num // 100 if c_count == 9: numerals += "CM" c_count -= 9 elif c_count == 4: numerals += "CD" c_count -= 4 if c_count >= 5: numerals += "D" c_count -= 5 numerals += c_count * "C" num %= 100 lowerCamelCase__ = num // 10 if x_count == 9: numerals += "XC" x_count -= 9 elif x_count == 4: numerals += "XL" x_count -= 4 if x_count >= 5: numerals += "L" x_count -= 5 numerals += x_count * "X" num %= 10 if num == 9: numerals += "IX" num -= 9 elif num == 4: numerals += "IV" num -= 4 if num >= 5: numerals += "V" num -= 5 numerals += num * "I" return numerals def snake_case ( _a: str = "/p089_roman.txt" )-> int: '''simple docstring''' lowerCamelCase__ = 0 with open(os.path.dirname(_a ) + roman_numerals_filename ) as filea: lowerCamelCase__ = filea.readlines() for line in lines: lowerCamelCase__ = line.strip() lowerCamelCase__ = parse_roman_numerals(_a ) lowerCamelCase__ = generate_roman_numerals(_a ) savings += len(_a ) - len(_a ) return savings if __name__ == "__main__": print(f"""{solution() = }""")
659
"""simple docstring""" import os from shutil import copyfile from typing import List, Optional, Tuple from tokenizers import processors from ...tokenization_utils import AddedToken, BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_nllb import NllbTokenizer else: _snake_case = None _snake_case = logging.get_logger(__name__) _snake_case = {"vocab_file": "sentencepiece.bpe.model", "tokenizer_file": "tokenizer.json"} _snake_case = { "vocab_file": { "facebook/nllb-200-distilled-600M": ( "https://huggingface.co/facebook/nllb-200-distilled-600M/resolve/main/sentencepiece.bpe.model" ), }, "tokenizer_file": { "facebook/nllb-200-distilled-600M": ( "https://huggingface.co/facebook/nllb-200-distilled-600M/resolve/main/tokenizer.json" ), }, } _snake_case = { "facebook/nllb-large-en-ro": 1024, "facebook/nllb-200-distilled-600M": 1024, } # fmt: off _snake_case = ["ace_Arab", "ace_Latn", "acm_Arab", "acq_Arab", "aeb_Arab", "afr_Latn", "ajp_Arab", "aka_Latn", "amh_Ethi", "apc_Arab", "arb_Arab", "ars_Arab", "ary_Arab", "arz_Arab", "asm_Beng", "ast_Latn", "awa_Deva", "ayr_Latn", "azb_Arab", "azj_Latn", "bak_Cyrl", "bam_Latn", "ban_Latn", "bel_Cyrl", "bem_Latn", "ben_Beng", "bho_Deva", "bjn_Arab", "bjn_Latn", "bod_Tibt", "bos_Latn", "bug_Latn", "bul_Cyrl", "cat_Latn", "ceb_Latn", "ces_Latn", "cjk_Latn", "ckb_Arab", "crh_Latn", "cym_Latn", "dan_Latn", "deu_Latn", "dik_Latn", "dyu_Latn", "dzo_Tibt", "ell_Grek", "eng_Latn", "epo_Latn", "est_Latn", "eus_Latn", "ewe_Latn", "fao_Latn", "pes_Arab", "fij_Latn", "fin_Latn", "fon_Latn", "fra_Latn", "fur_Latn", "fuv_Latn", "gla_Latn", "gle_Latn", "glg_Latn", "grn_Latn", "guj_Gujr", "hat_Latn", "hau_Latn", "heb_Hebr", "hin_Deva", "hne_Deva", "hrv_Latn", "hun_Latn", "hye_Armn", "ibo_Latn", "ilo_Latn", "ind_Latn", "isl_Latn", "ita_Latn", "jav_Latn", "jpn_Jpan", "kab_Latn", "kac_Latn", "kam_Latn", "kan_Knda", "kas_Arab", "kas_Deva", "kat_Geor", "knc_Arab", "knc_Latn", "kaz_Cyrl", "kbp_Latn", "kea_Latn", "khm_Khmr", "kik_Latn", "kin_Latn", "kir_Cyrl", "kmb_Latn", "kon_Latn", "kor_Hang", "kmr_Latn", "lao_Laoo", "lvs_Latn", "lij_Latn", "lim_Latn", "lin_Latn", "lit_Latn", "lmo_Latn", "ltg_Latn", "ltz_Latn", "lua_Latn", "lug_Latn", "luo_Latn", "lus_Latn", "mag_Deva", "mai_Deva", "mal_Mlym", "mar_Deva", "min_Latn", "mkd_Cyrl", "plt_Latn", "mlt_Latn", "mni_Beng", "khk_Cyrl", "mos_Latn", "mri_Latn", "zsm_Latn", "mya_Mymr", "nld_Latn", "nno_Latn", "nob_Latn", "npi_Deva", "nso_Latn", "nus_Latn", "nya_Latn", "oci_Latn", "gaz_Latn", "ory_Orya", "pag_Latn", "pan_Guru", "pap_Latn", "pol_Latn", "por_Latn", "prs_Arab", "pbt_Arab", "quy_Latn", "ron_Latn", "run_Latn", "rus_Cyrl", "sag_Latn", "san_Deva", "sat_Beng", "scn_Latn", "shn_Mymr", "sin_Sinh", "slk_Latn", "slv_Latn", "smo_Latn", "sna_Latn", "snd_Arab", "som_Latn", "sot_Latn", "spa_Latn", "als_Latn", "srd_Latn", "srp_Cyrl", "ssw_Latn", "sun_Latn", "swe_Latn", "swh_Latn", "szl_Latn", "tam_Taml", "tat_Cyrl", "tel_Telu", "tgk_Cyrl", "tgl_Latn", "tha_Thai", "tir_Ethi", "taq_Latn", "taq_Tfng", "tpi_Latn", "tsn_Latn", "tso_Latn", "tuk_Latn", "tum_Latn", "tur_Latn", "twi_Latn", "tzm_Tfng", "uig_Arab", "ukr_Cyrl", "umb_Latn", "urd_Arab", "uzn_Latn", "vec_Latn", "vie_Latn", "war_Latn", "wol_Latn", "xho_Latn", "ydd_Hebr", "yor_Latn", "yue_Hant", "zho_Hans", "zho_Hant", "zul_Latn"] class _a ( SCREAMING_SNAKE_CASE_ ): a_ : Any = VOCAB_FILES_NAMES a_ : Optional[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES a_ : Optional[Any] = PRETRAINED_VOCAB_FILES_MAP a_ : List[str] = ['input_ids', 'attention_mask'] a_ : Union[str, Any] = NllbTokenizer a_ : List[int] = [] a_ : List[int] = [] def __init__( self : int , SCREAMING_SNAKE_CASE__ : List[Any]=None , SCREAMING_SNAKE_CASE__ : Optional[int]=None , SCREAMING_SNAKE_CASE__ : List[Any]="<s>" , SCREAMING_SNAKE_CASE__ : List[str]="</s>" , SCREAMING_SNAKE_CASE__ : Any="</s>" , SCREAMING_SNAKE_CASE__ : List[str]="<s>" , SCREAMING_SNAKE_CASE__ : Tuple="<unk>" , SCREAMING_SNAKE_CASE__ : Optional[int]="<pad>" , SCREAMING_SNAKE_CASE__ : Any="<mask>" , SCREAMING_SNAKE_CASE__ : Union[str, Any]=None , SCREAMING_SNAKE_CASE__ : int=None , SCREAMING_SNAKE_CASE__ : Dict=None , SCREAMING_SNAKE_CASE__ : Tuple=False , **SCREAMING_SNAKE_CASE__ : str , ): # Mask token behave like a normal word, i.e. include the space before it lowerCamelCase__ = AddedToken(SCREAMING_SNAKE_CASE__ , lstrip=SCREAMING_SNAKE_CASE__ , rstrip=SCREAMING_SNAKE_CASE__ ) if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) else mask_token lowerCamelCase__ = legacy_behaviour super().__init__( vocab_file=SCREAMING_SNAKE_CASE__ , tokenizer_file=SCREAMING_SNAKE_CASE__ , bos_token=SCREAMING_SNAKE_CASE__ , eos_token=SCREAMING_SNAKE_CASE__ , sep_token=SCREAMING_SNAKE_CASE__ , cls_token=SCREAMING_SNAKE_CASE__ , unk_token=SCREAMING_SNAKE_CASE__ , pad_token=SCREAMING_SNAKE_CASE__ , mask_token=SCREAMING_SNAKE_CASE__ , src_lang=SCREAMING_SNAKE_CASE__ , tgt_lang=SCREAMING_SNAKE_CASE__ , additional_special_tokens=SCREAMING_SNAKE_CASE__ , legacy_behaviour=SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ , ) lowerCamelCase__ = vocab_file lowerCamelCase__ = False if not self.vocab_file else True lowerCamelCase__ = FAIRSEQ_LANGUAGE_CODES.copy() if additional_special_tokens is not None: # Only add those special tokens if they are not already there. _additional_special_tokens.extend( [t for t in additional_special_tokens if t not in _additional_special_tokens] ) self.add_special_tokens({'additional_special_tokens': _additional_special_tokens} ) lowerCamelCase__ = { lang_code: self.convert_tokens_to_ids(SCREAMING_SNAKE_CASE__ ) for lang_code in FAIRSEQ_LANGUAGE_CODES } lowerCamelCase__ = src_lang if src_lang is not None else 'eng_Latn' lowerCamelCase__ = self.convert_tokens_to_ids(self._src_lang ) lowerCamelCase__ = tgt_lang self.set_src_lang_special_tokens(self._src_lang ) @property def _UpperCamelCase ( self : str ): return self._src_lang @src_lang.setter def _UpperCamelCase ( self : int , SCREAMING_SNAKE_CASE__ : str ): lowerCamelCase__ = new_src_lang self.set_src_lang_special_tokens(self._src_lang ) def _UpperCamelCase ( self : Tuple , SCREAMING_SNAKE_CASE__ : List[int] , SCREAMING_SNAKE_CASE__ : Optional[List[int]] = None ): if token_ids_a is None: return self.prefix_tokens + token_ids_a + self.suffix_tokens # We don't expect to process pairs, but leave the pair logic for API consistency return self.prefix_tokens + token_ids_a + token_ids_a + self.suffix_tokens def _UpperCamelCase ( self : Optional[Any] , SCREAMING_SNAKE_CASE__ : List[int] , SCREAMING_SNAKE_CASE__ : Optional[List[int]] = None ): lowerCamelCase__ = [self.sep_token_id] lowerCamelCase__ = [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 _UpperCamelCase ( self : str , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : Optional[str] , SCREAMING_SNAKE_CASE__ : Optional[str] , **SCREAMING_SNAKE_CASE__ : Optional[int] ): if src_lang is None or tgt_lang is None: raise ValueError('Translation requires a `src_lang` and a `tgt_lang` for this model' ) lowerCamelCase__ = src_lang lowerCamelCase__ = self(SCREAMING_SNAKE_CASE__ , add_special_tokens=SCREAMING_SNAKE_CASE__ , return_tensors=SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = self.convert_tokens_to_ids(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = tgt_lang_id return inputs def _UpperCamelCase ( self : str , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : str = "eng_Latn" , SCREAMING_SNAKE_CASE__ : Optional[List[str]] = None , SCREAMING_SNAKE_CASE__ : str = "fra_Latn" , **SCREAMING_SNAKE_CASE__ : Dict , ): lowerCamelCase__ = src_lang lowerCamelCase__ = tgt_lang return super().prepare_seqaseq_batch(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : List[str] ): return self.set_src_lang_special_tokens(self.src_lang ) def _UpperCamelCase ( self : List[Any] ): return self.set_tgt_lang_special_tokens(self.tgt_lang ) def _UpperCamelCase ( self : str , SCREAMING_SNAKE_CASE__ : Union[str, Any] ): lowerCamelCase__ = self.convert_tokens_to_ids(SCREAMING_SNAKE_CASE__ ) if self.legacy_behaviour: lowerCamelCase__ = [] lowerCamelCase__ = [self.eos_token_id, self.cur_lang_code] else: lowerCamelCase__ = [self.cur_lang_code] lowerCamelCase__ = [self.eos_token_id] lowerCamelCase__ = self.convert_ids_to_tokens(self.prefix_tokens ) lowerCamelCase__ = self.convert_ids_to_tokens(self.suffix_tokens ) lowerCamelCase__ = processors.TemplateProcessing( single=prefix_tokens_str + ['$A'] + suffix_tokens_str , pair=prefix_tokens_str + ['$A', '$B'] + suffix_tokens_str , special_tokens=list(zip(prefix_tokens_str + suffix_tokens_str , self.prefix_tokens + self.suffix_tokens ) ) , ) def _UpperCamelCase ( self : Union[str, Any] , SCREAMING_SNAKE_CASE__ : str ): lowerCamelCase__ = self.convert_tokens_to_ids(SCREAMING_SNAKE_CASE__ ) if self.legacy_behaviour: lowerCamelCase__ = [] lowerCamelCase__ = [self.eos_token_id, self.cur_lang_code] else: lowerCamelCase__ = [self.cur_lang_code] lowerCamelCase__ = [self.eos_token_id] lowerCamelCase__ = self.convert_ids_to_tokens(self.prefix_tokens ) lowerCamelCase__ = self.convert_ids_to_tokens(self.suffix_tokens ) lowerCamelCase__ = processors.TemplateProcessing( single=prefix_tokens_str + ['$A'] + suffix_tokens_str , pair=prefix_tokens_str + ['$A', '$B'] + suffix_tokens_str , special_tokens=list(zip(prefix_tokens_str + suffix_tokens_str , self.prefix_tokens + self.suffix_tokens ) ) , ) def _UpperCamelCase ( self : Dict , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : Optional[str] = None ): if not self.can_save_slow_tokenizer: raise ValueError( 'Your fast tokenizer does not have the necessary information to save the vocabulary for a slow ' 'tokenizer.' ) if not os.path.isdir(SCREAMING_SNAKE_CASE__ ): logger.error(F'Vocabulary path ({save_directory}) should be a directory.' ) return lowerCamelCase__ = os.path.join( SCREAMING_SNAKE_CASE__ , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(SCREAMING_SNAKE_CASE__ ): copyfile(self.vocab_file , SCREAMING_SNAKE_CASE__ ) return (out_vocab_file,)
659
1
"""simple docstring""" def snake_case ( _a: str , _a: str )-> float: '''simple docstring''' def get_matched_characters(_a: str , _a: str ) -> str: lowerCamelCase__ = [] lowerCamelCase__ = min(len(_stra ) , len(_stra ) ) // 2 for i, l in enumerate(_stra ): lowerCamelCase__ = int(max(0 , i - limit ) ) lowerCamelCase__ = int(min(i + limit + 1 , len(_stra ) ) ) if l in _stra[left:right]: matched.append(_a ) lowerCamelCase__ = F'{_stra[0:_stra.index(_a )]} {_stra[_stra.index(_a ) + 1:]}' return "".join(_a ) # matching characters lowerCamelCase__ = get_matched_characters(_a , _a ) lowerCamelCase__ = get_matched_characters(_a , _a ) lowerCamelCase__ = len(_a ) # transposition lowerCamelCase__ = ( len([(ca, ca) for ca, ca in zip(_a , _a ) if ca != ca] ) // 2 ) if not match_count: lowerCamelCase__ = 0.0 else: lowerCamelCase__ = ( 1 / 3 * ( match_count / len(_a ) + match_count / len(_a ) + (match_count - transpositions) / match_count ) ) # common prefix up to 4 characters lowerCamelCase__ = 0 for ca, ca in zip(stra[:4] , stra[:4] ): if ca == ca: prefix_len += 1 else: break return jaro + 0.1 * prefix_len * (1 - jaro) if __name__ == "__main__": import doctest doctest.testmod() print(jaro_winkler("hello", "world"))
659
"""simple docstring""" from __future__ import annotations import unittest import numpy as np from transformers import BlipTextConfig from transformers.testing_utils import require_tf, slow from transformers.utils import is_tf_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask if is_tf_available(): import tensorflow as tf from transformers import TFBlipTextModel from transformers.models.blip.modeling_tf_blip import TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST class _a : def __init__( self : List[str] , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : Union[str, Any]=12 , SCREAMING_SNAKE_CASE__ : Union[str, Any]=7 , SCREAMING_SNAKE_CASE__ : Union[str, Any]=True , SCREAMING_SNAKE_CASE__ : Optional[Any]=True , SCREAMING_SNAKE_CASE__ : str=True , SCREAMING_SNAKE_CASE__ : Any=99 , SCREAMING_SNAKE_CASE__ : Optional[Any]=32 , SCREAMING_SNAKE_CASE__ : str=32 , SCREAMING_SNAKE_CASE__ : Tuple=2 , SCREAMING_SNAKE_CASE__ : str=4 , SCREAMING_SNAKE_CASE__ : Optional[Any]=37 , SCREAMING_SNAKE_CASE__ : Union[str, Any]=0.1 , SCREAMING_SNAKE_CASE__ : Optional[Any]=0.1 , SCREAMING_SNAKE_CASE__ : str=5_12 , SCREAMING_SNAKE_CASE__ : str=0.02 , SCREAMING_SNAKE_CASE__ : Union[str, Any]=0 , SCREAMING_SNAKE_CASE__ : Any=None , ): lowerCamelCase__ = parent lowerCamelCase__ = batch_size lowerCamelCase__ = seq_length lowerCamelCase__ = is_training lowerCamelCase__ = use_input_mask lowerCamelCase__ = use_labels lowerCamelCase__ = vocab_size lowerCamelCase__ = hidden_size lowerCamelCase__ = projection_dim lowerCamelCase__ = num_hidden_layers lowerCamelCase__ = num_attention_heads lowerCamelCase__ = intermediate_size lowerCamelCase__ = dropout lowerCamelCase__ = attention_dropout lowerCamelCase__ = max_position_embeddings lowerCamelCase__ = initializer_range lowerCamelCase__ = scope lowerCamelCase__ = bos_token_id def _UpperCamelCase ( self : int ): lowerCamelCase__ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) lowerCamelCase__ = None if self.use_input_mask: lowerCamelCase__ = random_attention_mask([self.batch_size, self.seq_length] ) if input_mask is not None: lowerCamelCase__ = input_mask.numpy() lowerCamelCase__ , lowerCamelCase__ = input_mask.shape lowerCamelCase__ = np.random.randint(1 , seq_length - 1 , size=(batch_size,) ) for batch_idx, start_index in enumerate(SCREAMING_SNAKE_CASE__ ): lowerCamelCase__ = 1 lowerCamelCase__ = 0 lowerCamelCase__ = self.get_config() return config, input_ids, tf.convert_to_tensor(SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : Any ): return BlipTextConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , projection_dim=self.projection_dim , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , dropout=self.dropout , attention_dropout=self.attention_dropout , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , bos_token_id=self.bos_token_id , ) def _UpperCamelCase ( self : Any , SCREAMING_SNAKE_CASE__ : Any , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : List[str] ): lowerCamelCase__ = TFBlipTextModel(config=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = model(SCREAMING_SNAKE_CASE__ , attention_mask=SCREAMING_SNAKE_CASE__ , training=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = model(SCREAMING_SNAKE_CASE__ , training=SCREAMING_SNAKE_CASE__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size) ) def _UpperCamelCase ( self : Dict ): lowerCamelCase__ = self.prepare_config_and_inputs() lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ = config_and_inputs lowerCamelCase__ = {'input_ids': input_ids, 'attention_mask': input_mask} return config, inputs_dict @require_tf class _a ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): a_ : str = (TFBlipTextModel,) if is_tf_available() else () a_ : List[str] = False a_ : Optional[Any] = False a_ : Union[str, Any] = False def _UpperCamelCase ( self : str ): lowerCamelCase__ = BlipTextModelTester(self ) lowerCamelCase__ = ConfigTester(self , config_class=SCREAMING_SNAKE_CASE__ , hidden_size=37 ) def _UpperCamelCase ( self : Tuple ): self.config_tester.run_common_tests() def _UpperCamelCase ( self : str ): lowerCamelCase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : Tuple ): pass def _UpperCamelCase ( self : Tuple ): pass @unittest.skip(reason='Blip does not use inputs_embeds' ) def _UpperCamelCase ( self : List[str] ): pass @unittest.skip(reason='BlipTextModel has no base class and is not available in MODEL_MAPPING' ) def _UpperCamelCase ( self : Dict ): pass @unittest.skip(reason='BlipTextModel has no base class and is not available in MODEL_MAPPING' ) def _UpperCamelCase ( self : List[Any] ): pass @slow def _UpperCamelCase ( self : str ): for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowerCamelCase__ = TFBlipTextModel.from_pretrained(SCREAMING_SNAKE_CASE__ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : Optional[int] , SCREAMING_SNAKE_CASE__ : List[Any]=True ): super().test_pt_tf_model_equivalence(allow_missing_keys=SCREAMING_SNAKE_CASE__ )
659
1
"""simple docstring""" import gc import random import unittest import numpy as np import torch from transformers import ( CLIPImageProcessor, CLIPTextConfig, CLIPTextModelWithProjection, CLIPTokenizer, CLIPVisionConfig, CLIPVisionModelWithProjection, ) from diffusers import ( DiffusionPipeline, UnCLIPImageVariationPipeline, UnCLIPScheduler, UNetaDConditionModel, UNetaDModel, ) from diffusers.pipelines.unclip.text_proj import UnCLIPTextProjModel from diffusers.utils import floats_tensor, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, load_image, require_torch_gpu, skip_mps from ..pipeline_params import IMAGE_VARIATION_BATCH_PARAMS, IMAGE_VARIATION_PARAMS from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference enable_full_determinism() class _a ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): a_ : List[Any] = UnCLIPImageVariationPipeline a_ : List[str] = IMAGE_VARIATION_PARAMS - {'height', 'width', 'guidance_scale'} a_ : Optional[int] = IMAGE_VARIATION_BATCH_PARAMS a_ : List[str] = [ 'generator', 'return_dict', 'decoder_num_inference_steps', 'super_res_num_inference_steps', ] a_ : Tuple = False @property def _UpperCamelCase ( self : Optional[Any] ): return 32 @property def _UpperCamelCase ( self : str ): return 32 @property def _UpperCamelCase ( self : str ): return self.time_input_dim @property def _UpperCamelCase ( self : List[str] ): return self.time_input_dim * 4 @property def _UpperCamelCase ( self : Any ): return 1_00 @property def _UpperCamelCase ( self : Dict ): lowerCamelCase__ = CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip' ) return tokenizer @property def _UpperCamelCase ( self : Tuple ): torch.manual_seed(0 ) lowerCamelCase__ = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=self.text_embedder_hidden_size , projection_dim=self.text_embedder_hidden_size , intermediate_size=37 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=10_00 , ) return CLIPTextModelWithProjection(SCREAMING_SNAKE_CASE__ ) @property def _UpperCamelCase ( self : List[Any] ): torch.manual_seed(0 ) lowerCamelCase__ = CLIPVisionConfig( hidden_size=self.text_embedder_hidden_size , projection_dim=self.text_embedder_hidden_size , num_hidden_layers=5 , num_attention_heads=4 , image_size=32 , intermediate_size=37 , patch_size=1 , ) return CLIPVisionModelWithProjection(SCREAMING_SNAKE_CASE__ ) @property def _UpperCamelCase ( self : int ): torch.manual_seed(0 ) lowerCamelCase__ = { 'clip_embeddings_dim': self.text_embedder_hidden_size, 'time_embed_dim': self.time_embed_dim, 'cross_attention_dim': self.cross_attention_dim, } lowerCamelCase__ = UnCLIPTextProjModel(**SCREAMING_SNAKE_CASE__ ) return model @property def _UpperCamelCase ( self : str ): torch.manual_seed(0 ) lowerCamelCase__ = { 'sample_size': 32, # RGB in channels 'in_channels': 3, # Out channels is double in channels because predicts mean and variance 'out_channels': 6, '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, 'cross_attention_dim': self.cross_attention_dim, 'attention_head_dim': 4, 'resnet_time_scale_shift': 'scale_shift', 'class_embed_type': 'identity', } lowerCamelCase__ = UNetaDConditionModel(**SCREAMING_SNAKE_CASE__ ) return model @property def _UpperCamelCase ( self : Any ): return { "sample_size": 64, "layers_per_block": 1, "down_block_types": ("ResnetDownsampleBlock2D", "ResnetDownsampleBlock2D"), "up_block_types": ("ResnetUpsampleBlock2D", "ResnetUpsampleBlock2D"), "block_out_channels": (self.block_out_channels_a, self.block_out_channels_a * 2), "in_channels": 6, "out_channels": 3, } @property def _UpperCamelCase ( self : str ): torch.manual_seed(0 ) lowerCamelCase__ = UNetaDModel(**self.dummy_super_res_kwargs ) return model @property def _UpperCamelCase ( self : List[Any] ): # seeded differently to get different unet than `self.dummy_super_res_first` torch.manual_seed(1 ) lowerCamelCase__ = UNetaDModel(**self.dummy_super_res_kwargs ) return model def _UpperCamelCase ( self : str ): lowerCamelCase__ = self.dummy_decoder lowerCamelCase__ = self.dummy_text_proj lowerCamelCase__ = self.dummy_text_encoder lowerCamelCase__ = self.dummy_tokenizer lowerCamelCase__ = self.dummy_super_res_first lowerCamelCase__ = self.dummy_super_res_last lowerCamelCase__ = UnCLIPScheduler( variance_type='learned_range' , prediction_type='epsilon' , num_train_timesteps=10_00 , ) lowerCamelCase__ = UnCLIPScheduler( variance_type='fixed_small_log' , prediction_type='epsilon' , num_train_timesteps=10_00 , ) lowerCamelCase__ = CLIPImageProcessor(crop_size=32 , size=32 ) lowerCamelCase__ = self.dummy_image_encoder return { "decoder": decoder, "text_encoder": text_encoder, "tokenizer": tokenizer, "text_proj": text_proj, "feature_extractor": feature_extractor, "image_encoder": image_encoder, "super_res_first": super_res_first, "super_res_last": super_res_last, "decoder_scheduler": decoder_scheduler, "super_res_scheduler": super_res_scheduler, } def _UpperCamelCase ( self : str , SCREAMING_SNAKE_CASE__ : Union[str, Any] , SCREAMING_SNAKE_CASE__ : List[Any]=0 , SCREAMING_SNAKE_CASE__ : Tuple=True ): lowerCamelCase__ = floats_tensor((1, 3, 32, 32) , rng=random.Random(SCREAMING_SNAKE_CASE__ ) ).to(SCREAMING_SNAKE_CASE__ ) if str(SCREAMING_SNAKE_CASE__ ).startswith('mps' ): lowerCamelCase__ = torch.manual_seed(SCREAMING_SNAKE_CASE__ ) else: lowerCamelCase__ = torch.Generator(device=SCREAMING_SNAKE_CASE__ ).manual_seed(SCREAMING_SNAKE_CASE__ ) if pil_image: lowerCamelCase__ = input_image * 0.5 + 0.5 lowerCamelCase__ = input_image.clamp(0 , 1 ) lowerCamelCase__ = input_image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() lowerCamelCase__ = DiffusionPipeline.numpy_to_pil(SCREAMING_SNAKE_CASE__ )[0] return { "image": input_image, "generator": generator, "decoder_num_inference_steps": 2, "super_res_num_inference_steps": 2, "output_type": "np", } def _UpperCamelCase ( self : Any ): lowerCamelCase__ = 'cpu' lowerCamelCase__ = self.get_dummy_components() lowerCamelCase__ = self.pipeline_class(**SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = pipe.to(SCREAMING_SNAKE_CASE__ ) pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = self.get_dummy_inputs(SCREAMING_SNAKE_CASE__ , pil_image=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = pipe(**SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = output.images lowerCamelCase__ = self.get_dummy_inputs(SCREAMING_SNAKE_CASE__ , pil_image=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = pipe( **SCREAMING_SNAKE_CASE__ , return_dict=SCREAMING_SNAKE_CASE__ , )[0] lowerCamelCase__ = image[0, -3:, -3:, -1] lowerCamelCase__ = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) lowerCamelCase__ = np.array( [ 0.99_97, 0.00_02, 0.99_97, 0.99_97, 0.99_69, 0.00_23, 0.99_97, 0.99_69, 0.99_70, ] ) 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 _UpperCamelCase ( self : List[str] ): lowerCamelCase__ = 'cpu' lowerCamelCase__ = self.get_dummy_components() lowerCamelCase__ = self.pipeline_class(**SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = pipe.to(SCREAMING_SNAKE_CASE__ ) pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = self.get_dummy_inputs(SCREAMING_SNAKE_CASE__ , pil_image=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = pipe(**SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = output.images lowerCamelCase__ = self.get_dummy_inputs(SCREAMING_SNAKE_CASE__ , pil_image=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = pipe( **SCREAMING_SNAKE_CASE__ , return_dict=SCREAMING_SNAKE_CASE__ , )[0] lowerCamelCase__ = image[0, -3:, -3:, -1] lowerCamelCase__ = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) lowerCamelCase__ = np.array([0.99_97, 0.00_03, 0.99_97, 0.99_97, 0.99_70, 0.00_24, 0.99_97, 0.99_71, 0.99_71] ) 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 _UpperCamelCase ( self : Optional[int] ): lowerCamelCase__ = 'cpu' lowerCamelCase__ = self.get_dummy_components() lowerCamelCase__ = self.pipeline_class(**SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = pipe.to(SCREAMING_SNAKE_CASE__ ) pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = self.get_dummy_inputs(SCREAMING_SNAKE_CASE__ , pil_image=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = [ pipeline_inputs['image'], pipeline_inputs['image'], ] lowerCamelCase__ = pipe(**SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = output.images lowerCamelCase__ = self.get_dummy_inputs(SCREAMING_SNAKE_CASE__ , pil_image=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = [ tuple_pipeline_inputs['image'], tuple_pipeline_inputs['image'], ] lowerCamelCase__ = pipe( **SCREAMING_SNAKE_CASE__ , return_dict=SCREAMING_SNAKE_CASE__ , )[0] lowerCamelCase__ = image[0, -3:, -3:, -1] lowerCamelCase__ = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (2, 64, 64, 3) lowerCamelCase__ = np.array( [ 0.99_97, 0.99_89, 0.00_08, 0.00_21, 0.99_60, 0.00_18, 0.00_14, 0.00_02, 0.99_33, ] ) 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 _UpperCamelCase ( self : Tuple ): lowerCamelCase__ = torch.device('cpu' ) class _a : a_ : Dict = 1 lowerCamelCase__ = self.get_dummy_components() lowerCamelCase__ = self.pipeline_class(**SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = pipe.to(SCREAMING_SNAKE_CASE__ ) pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = torch.Generator(device=SCREAMING_SNAKE_CASE__ ).manual_seed(0 ) lowerCamelCase__ = pipe.decoder.dtype lowerCamelCase__ = 1 lowerCamelCase__ = ( batch_size, pipe.decoder.config.in_channels, pipe.decoder.config.sample_size, pipe.decoder.config.sample_size, ) lowerCamelCase__ = pipe.prepare_latents( SCREAMING_SNAKE_CASE__ , dtype=SCREAMING_SNAKE_CASE__ , device=SCREAMING_SNAKE_CASE__ , generator=SCREAMING_SNAKE_CASE__ , latents=SCREAMING_SNAKE_CASE__ , scheduler=DummyScheduler() ) lowerCamelCase__ = ( batch_size, pipe.super_res_first.config.in_channels // 2, pipe.super_res_first.config.sample_size, pipe.super_res_first.config.sample_size, ) lowerCamelCase__ = pipe.prepare_latents( SCREAMING_SNAKE_CASE__ , dtype=SCREAMING_SNAKE_CASE__ , device=SCREAMING_SNAKE_CASE__ , generator=SCREAMING_SNAKE_CASE__ , latents=SCREAMING_SNAKE_CASE__ , scheduler=DummyScheduler() ) lowerCamelCase__ = self.get_dummy_inputs(SCREAMING_SNAKE_CASE__ , pil_image=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = pipe( **SCREAMING_SNAKE_CASE__ , decoder_latents=SCREAMING_SNAKE_CASE__ , super_res_latents=SCREAMING_SNAKE_CASE__ ).images lowerCamelCase__ = self.get_dummy_inputs(SCREAMING_SNAKE_CASE__ , pil_image=SCREAMING_SNAKE_CASE__ ) # Don't pass image, instead pass embedding lowerCamelCase__ = pipeline_inputs.pop('image' ) lowerCamelCase__ = pipe.image_encoder(SCREAMING_SNAKE_CASE__ ).image_embeds lowerCamelCase__ = pipe( **SCREAMING_SNAKE_CASE__ , decoder_latents=SCREAMING_SNAKE_CASE__ , super_res_latents=SCREAMING_SNAKE_CASE__ , image_embeddings=SCREAMING_SNAKE_CASE__ , ).images # make sure passing text embeddings manually is identical assert np.abs(img_out_a - img_out_a ).max() < 1e-4 @skip_mps def _UpperCamelCase ( self : Tuple ): lowerCamelCase__ = torch_device == 'cpu' # Check is relaxed because there is not a torch 2.0 sliced attention added kv processor lowerCamelCase__ = 1e-2 self._test_attention_slicing_forward_pass( test_max_difference=SCREAMING_SNAKE_CASE__ , expected_max_diff=SCREAMING_SNAKE_CASE__ ) @skip_mps def _UpperCamelCase ( self : Optional[Any] ): lowerCamelCase__ = torch_device == 'cpu' lowerCamelCase__ = True lowerCamelCase__ = [ 'decoder_num_inference_steps', 'super_res_num_inference_steps', ] self._test_inference_batch_single_identical( test_max_difference=SCREAMING_SNAKE_CASE__ , relax_max_difference=SCREAMING_SNAKE_CASE__ , additional_params_copy_to_batched_inputs=SCREAMING_SNAKE_CASE__ , ) def _UpperCamelCase ( self : List[str] ): lowerCamelCase__ = [ 'decoder_num_inference_steps', 'super_res_num_inference_steps', ] if torch_device == "mps": # TODO: MPS errors with larger batch sizes lowerCamelCase__ = [2, 3] self._test_inference_batch_consistent( batch_sizes=SCREAMING_SNAKE_CASE__ , additional_params_copy_to_batched_inputs=SCREAMING_SNAKE_CASE__ , ) else: self._test_inference_batch_consistent( additional_params_copy_to_batched_inputs=SCREAMING_SNAKE_CASE__ ) @skip_mps def _UpperCamelCase ( self : List[str] ): return super().test_dict_tuple_outputs_equivalent() @skip_mps def _UpperCamelCase ( self : Optional[int] ): return super().test_save_load_local() @skip_mps def _UpperCamelCase ( self : List[Any] ): return super().test_save_load_optional_components() @slow @require_torch_gpu class _a ( unittest.TestCase ): def _UpperCamelCase ( self : List[str] ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def _UpperCamelCase ( self : Any ): lowerCamelCase__ = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/unclip/cat.png' ) lowerCamelCase__ = load_numpy( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/unclip/karlo_v1_alpha_cat_variation_fp16.npy' ) lowerCamelCase__ = UnCLIPImageVariationPipeline.from_pretrained( 'kakaobrain/karlo-v1-alpha-image-variations' , torch_dtype=torch.floataa ) lowerCamelCase__ = pipeline.to(SCREAMING_SNAKE_CASE__ ) pipeline.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = torch.Generator(device='cpu' ).manual_seed(0 ) lowerCamelCase__ = pipeline( SCREAMING_SNAKE_CASE__ , generator=SCREAMING_SNAKE_CASE__ , output_type='np' , ) lowerCamelCase__ = output.images[0] assert image.shape == (2_56, 2_56, 3) assert_mean_pixel_difference(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , 15 )
659
"""simple docstring""" import warnings from diffusers import StableDiffusionImgaImgPipeline # noqa F401 warnings.warn( "The `image_to_image.py` script is outdated. Please use directly `from diffusers import" " StableDiffusionImg2ImgPipeline` instead." )
659
1
"""simple docstring""" def snake_case ( _a: List[str] )-> Any: '''simple docstring''' lowerCamelCase__ , lowerCamelCase__ = [], [] while len(_a ) > 1: lowerCamelCase__ , lowerCamelCase__ = min(_a ), max(_a ) start.append(_a ) end.append(_a ) collection.remove(_a ) collection.remove(_a ) end.reverse() return start + collection + end if __name__ == "__main__": _snake_case = input("Enter numbers separated by a comma:\n").strip() _snake_case = [int(item) for item in user_input.split(",")] print(*merge_sort(unsorted), sep=",")
659
"""simple docstring""" import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_funnel import FunnelTokenizer _snake_case = logging.get_logger(__name__) _snake_case = {"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"} _snake_case = [ "small", "small-base", "medium", "medium-base", "intermediate", "intermediate-base", "large", "large-base", "xlarge", "xlarge-base", ] _snake_case = { "vocab_file": { "funnel-transformer/small": "https://huggingface.co/funnel-transformer/small/resolve/main/vocab.txt", "funnel-transformer/small-base": "https://huggingface.co/funnel-transformer/small-base/resolve/main/vocab.txt", "funnel-transformer/medium": "https://huggingface.co/funnel-transformer/medium/resolve/main/vocab.txt", "funnel-transformer/medium-base": ( "https://huggingface.co/funnel-transformer/medium-base/resolve/main/vocab.txt" ), "funnel-transformer/intermediate": ( "https://huggingface.co/funnel-transformer/intermediate/resolve/main/vocab.txt" ), "funnel-transformer/intermediate-base": ( "https://huggingface.co/funnel-transformer/intermediate-base/resolve/main/vocab.txt" ), "funnel-transformer/large": "https://huggingface.co/funnel-transformer/large/resolve/main/vocab.txt", "funnel-transformer/large-base": "https://huggingface.co/funnel-transformer/large-base/resolve/main/vocab.txt", "funnel-transformer/xlarge": "https://huggingface.co/funnel-transformer/xlarge/resolve/main/vocab.txt", "funnel-transformer/xlarge-base": ( "https://huggingface.co/funnel-transformer/xlarge-base/resolve/main/vocab.txt" ), }, "tokenizer_file": { "funnel-transformer/small": "https://huggingface.co/funnel-transformer/small/resolve/main/tokenizer.json", "funnel-transformer/small-base": ( "https://huggingface.co/funnel-transformer/small-base/resolve/main/tokenizer.json" ), "funnel-transformer/medium": "https://huggingface.co/funnel-transformer/medium/resolve/main/tokenizer.json", "funnel-transformer/medium-base": ( "https://huggingface.co/funnel-transformer/medium-base/resolve/main/tokenizer.json" ), "funnel-transformer/intermediate": ( "https://huggingface.co/funnel-transformer/intermediate/resolve/main/tokenizer.json" ), "funnel-transformer/intermediate-base": ( "https://huggingface.co/funnel-transformer/intermediate-base/resolve/main/tokenizer.json" ), "funnel-transformer/large": "https://huggingface.co/funnel-transformer/large/resolve/main/tokenizer.json", "funnel-transformer/large-base": ( "https://huggingface.co/funnel-transformer/large-base/resolve/main/tokenizer.json" ), "funnel-transformer/xlarge": "https://huggingface.co/funnel-transformer/xlarge/resolve/main/tokenizer.json", "funnel-transformer/xlarge-base": ( "https://huggingface.co/funnel-transformer/xlarge-base/resolve/main/tokenizer.json" ), }, } _snake_case = {f"""funnel-transformer/{name}""": 512 for name in _model_names} _snake_case = {f"""funnel-transformer/{name}""": {"do_lower_case": True} for name in _model_names} class _a ( SCREAMING_SNAKE_CASE_ ): a_ : List[str] = VOCAB_FILES_NAMES a_ : List[str] = PRETRAINED_VOCAB_FILES_MAP a_ : Optional[int] = PRETRAINED_INIT_CONFIGURATION a_ : List[str] = FunnelTokenizer a_ : Dict = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES a_ : int = 2 def __init__( self : int , SCREAMING_SNAKE_CASE__ : Optional[int]=None , SCREAMING_SNAKE_CASE__ : Union[str, Any]=None , SCREAMING_SNAKE_CASE__ : int=True , SCREAMING_SNAKE_CASE__ : Any="<unk>" , SCREAMING_SNAKE_CASE__ : List[Any]="<sep>" , SCREAMING_SNAKE_CASE__ : int="<pad>" , SCREAMING_SNAKE_CASE__ : Tuple="<cls>" , SCREAMING_SNAKE_CASE__ : Tuple="<mask>" , SCREAMING_SNAKE_CASE__ : Any="<s>" , SCREAMING_SNAKE_CASE__ : Tuple="</s>" , SCREAMING_SNAKE_CASE__ : str=True , SCREAMING_SNAKE_CASE__ : Any=True , SCREAMING_SNAKE_CASE__ : str=None , SCREAMING_SNAKE_CASE__ : int="##" , **SCREAMING_SNAKE_CASE__ : Any , ): super().__init__( SCREAMING_SNAKE_CASE__ , tokenizer_file=SCREAMING_SNAKE_CASE__ , do_lower_case=SCREAMING_SNAKE_CASE__ , unk_token=SCREAMING_SNAKE_CASE__ , sep_token=SCREAMING_SNAKE_CASE__ , pad_token=SCREAMING_SNAKE_CASE__ , cls_token=SCREAMING_SNAKE_CASE__ , mask_token=SCREAMING_SNAKE_CASE__ , bos_token=SCREAMING_SNAKE_CASE__ , eos_token=SCREAMING_SNAKE_CASE__ , clean_text=SCREAMING_SNAKE_CASE__ , tokenize_chinese_chars=SCREAMING_SNAKE_CASE__ , strip_accents=SCREAMING_SNAKE_CASE__ , wordpieces_prefix=SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ , ) lowerCamelCase__ = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get('lowercase' , SCREAMING_SNAKE_CASE__ ) != do_lower_case or normalizer_state.get('strip_accents' , SCREAMING_SNAKE_CASE__ ) != strip_accents or normalizer_state.get('handle_chinese_chars' , SCREAMING_SNAKE_CASE__ ) != tokenize_chinese_chars ): lowerCamelCase__ = getattr(SCREAMING_SNAKE_CASE__ , normalizer_state.pop('type' ) ) lowerCamelCase__ = do_lower_case lowerCamelCase__ = strip_accents lowerCamelCase__ = tokenize_chinese_chars lowerCamelCase__ = normalizer_class(**SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = do_lower_case def _UpperCamelCase ( self : Dict , SCREAMING_SNAKE_CASE__ : Tuple , SCREAMING_SNAKE_CASE__ : Optional[Any]=None ): lowerCamelCase__ = [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 _UpperCamelCase ( self : Dict , SCREAMING_SNAKE_CASE__ : List[int] , SCREAMING_SNAKE_CASE__ : Optional[List[int]] = None ): lowerCamelCase__ = [self.sep_token_id] lowerCamelCase__ = [self.cls_token_id] if token_ids_a is None: return len(cls ) * [self.cls_token_type_id] + len(token_ids_a + sep ) * [0] return len(cls ) * [self.cls_token_type_id] + len(token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def _UpperCamelCase ( self : Optional[int] , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : Optional[str] = None ): lowerCamelCase__ = self._tokenizer.model.save(SCREAMING_SNAKE_CASE__ , name=SCREAMING_SNAKE_CASE__ ) return tuple(SCREAMING_SNAKE_CASE__ )
659
1
"""simple docstring""" from ..utils import DummyObject, requires_backends class _a ( metaclass=SCREAMING_SNAKE_CASE_ ): a_ : List[Any] = ['transformers', 'torch', 'note_seq'] def __init__( self : List[Any] , *SCREAMING_SNAKE_CASE__ : Tuple , **SCREAMING_SNAKE_CASE__ : List[Any] ): requires_backends(self , ['transformers', 'torch', 'note_seq'] ) @classmethod def _UpperCamelCase ( cls : str , *SCREAMING_SNAKE_CASE__ : Union[str, Any] , **SCREAMING_SNAKE_CASE__ : Tuple ): requires_backends(cls , ['transformers', 'torch', 'note_seq'] ) @classmethod def _UpperCamelCase ( cls : Optional[int] , *SCREAMING_SNAKE_CASE__ : int , **SCREAMING_SNAKE_CASE__ : int ): requires_backends(cls , ['transformers', 'torch', 'note_seq'] )
659
"""simple docstring""" # A Bipartite Graph is a graph whose vertices can be divided into two independent sets, # U and V such that every edge (u, v) either connects a vertex from U to V or a vertex # from V to U. In other words, for every edge (u, v), either u belongs to U and v to V, # or u belongs to V and v to U. We can also say that there is no edge that connects # vertices of same set. def snake_case ( _a: Optional[Any] )-> Union[str, Any]: '''simple docstring''' lowerCamelCase__ = [False] * len(_a ) lowerCamelCase__ = [-1] * len(_a ) def dfs(_a: Any , _a: Optional[int] ): lowerCamelCase__ = True lowerCamelCase__ = c for u in graph[v]: if not visited[u]: dfs(_a , 1 - c ) for i in range(len(_a ) ): if not visited[i]: dfs(_a , 0 ) for i in range(len(_a ) ): for j in graph[i]: if color[i] == color[j]: return False return True # Adjacency list of graph _snake_case = {0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 4: []} print(check_bipartite_dfs(graph))
659
1
"""simple docstring""" def snake_case ( _a: List[Any] , _a: Any , _a: str , _a: List[Any] )-> List[Any]: '''simple docstring''' lowerCamelCase__ = [False] * len(_a ) lowerCamelCase__ = [] queue.append(_a ) lowerCamelCase__ = True while queue: lowerCamelCase__ = queue.pop(0 ) for ind in range(len(graph[u] ) ): if visited[ind] is False and graph[u][ind] > 0: queue.append(_a ) lowerCamelCase__ = True lowerCamelCase__ = u return visited[t] def snake_case ( _a: List[Any] , _a: str , _a: List[str] )-> Optional[int]: '''simple docstring''' lowerCamelCase__ = [-1] * (len(_a )) lowerCamelCase__ = 0 while bfs(_a , _a , _a , _a ): lowerCamelCase__ = float('Inf' ) lowerCamelCase__ = sink while s != source: # Find the minimum value in select path lowerCamelCase__ = min(_a , graph[parent[s]][s] ) lowerCamelCase__ = parent[s] max_flow += path_flow lowerCamelCase__ = sink while v != source: lowerCamelCase__ = parent[v] graph[u][v] -= path_flow graph[v][u] += path_flow lowerCamelCase__ = parent[v] return max_flow _snake_case = [ [0, 16, 13, 0, 0, 0], [0, 0, 10, 12, 0, 0], [0, 4, 0, 0, 14, 0], [0, 0, 9, 0, 0, 20], [0, 0, 0, 7, 0, 4], [0, 0, 0, 0, 0, 0], ] _snake_case , _snake_case = 0, 5 print(ford_fulkerson(graph, source, sink))
659
"""simple docstring""" from collections.abc import Iterator, MutableMapping from dataclasses import dataclass from typing import Generic, TypeVar _snake_case = TypeVar("KEY") _snake_case = TypeVar("VAL") @dataclass(frozen=SCREAMING_SNAKE_CASE_ , slots=SCREAMING_SNAKE_CASE_ ) class _a ( Generic[KEY, VAL] ): a_ : KEY a_ : VAL class _a ( _Item ): def __init__( self : List[str] ): super().__init__(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) def __bool__( self : str ): return False _snake_case = _DeletedItem() class _a ( MutableMapping[KEY, VAL] ): def __init__( self : Optional[Any] , SCREAMING_SNAKE_CASE__ : int = 8 , SCREAMING_SNAKE_CASE__ : float = 0.75 ): lowerCamelCase__ = initial_block_size lowerCamelCase__ = [None] * initial_block_size assert 0.0 < capacity_factor < 1.0 lowerCamelCase__ = capacity_factor lowerCamelCase__ = 0 def _UpperCamelCase ( self : Optional[int] , SCREAMING_SNAKE_CASE__ : KEY ): return hash(SCREAMING_SNAKE_CASE__ ) % len(self._buckets ) def _UpperCamelCase ( self : str , SCREAMING_SNAKE_CASE__ : int ): return (ind + 1) % len(self._buckets ) def _UpperCamelCase ( self : Union[str, Any] , SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : KEY , SCREAMING_SNAKE_CASE__ : VAL ): lowerCamelCase__ = self._buckets[ind] if not stored: lowerCamelCase__ = _Item(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) self._len += 1 return True elif stored.key == key: lowerCamelCase__ = _Item(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) return True else: return False def _UpperCamelCase ( self : Dict ): lowerCamelCase__ = len(self._buckets ) * self._capacity_factor return len(self ) >= int(SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : int ): if len(self._buckets ) <= self._initial_block_size: return False lowerCamelCase__ = len(self._buckets ) * self._capacity_factor / 2 return len(self ) < limit def _UpperCamelCase ( self : List[str] , SCREAMING_SNAKE_CASE__ : int ): lowerCamelCase__ = self._buckets lowerCamelCase__ = [None] * new_size lowerCamelCase__ = 0 for item in old_buckets: if item: self._add_item(item.key , item.val ) def _UpperCamelCase ( self : List[str] ): self._resize(len(self._buckets ) * 2 ) def _UpperCamelCase ( self : Optional[int] ): self._resize(len(self._buckets ) // 2 ) def _UpperCamelCase ( self : Any , SCREAMING_SNAKE_CASE__ : KEY ): lowerCamelCase__ = self._get_bucket_index(SCREAMING_SNAKE_CASE__ ) for _ in range(len(self._buckets ) ): yield ind lowerCamelCase__ = self._get_next_ind(SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : Optional[Any] , SCREAMING_SNAKE_CASE__ : KEY , SCREAMING_SNAKE_CASE__ : VAL ): for ind in self._iterate_buckets(SCREAMING_SNAKE_CASE__ ): if self._try_set(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): break def __setitem__( self : Dict , SCREAMING_SNAKE_CASE__ : KEY , SCREAMING_SNAKE_CASE__ : VAL ): if self._is_full(): self._size_up() self._add_item(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) def __delitem__( self : Dict , SCREAMING_SNAKE_CASE__ : KEY ): for ind in self._iterate_buckets(SCREAMING_SNAKE_CASE__ ): lowerCamelCase__ = self._buckets[ind] if item is None: raise KeyError(SCREAMING_SNAKE_CASE__ ) if item is _deleted: continue if item.key == key: lowerCamelCase__ = _deleted self._len -= 1 break if self._is_sparse(): self._size_down() def __getitem__( self : str , SCREAMING_SNAKE_CASE__ : KEY ): for ind in self._iterate_buckets(SCREAMING_SNAKE_CASE__ ): lowerCamelCase__ = self._buckets[ind] if item is None: break if item is _deleted: continue if item.key == key: return item.val raise KeyError(SCREAMING_SNAKE_CASE__ ) def __len__( self : List[Any] ): return self._len def __iter__( self : Optional[int] ): yield from (item.key for item in self._buckets if item) def __repr__( self : str ): lowerCamelCase__ = ' ,'.join( F'{item.key}: {item.val}' for item in self._buckets if item ) return F'HashMap({val_string})'
659
1
"""simple docstring""" import os def snake_case ( _a: Tuple )-> List[str]: '''simple docstring''' lowerCamelCase__ = len(grid[0] ) lowerCamelCase__ = len(_a ) lowerCamelCase__ = 0 lowerCamelCase__ = 0 lowerCamelCase__ = 0 # Check vertically, horizontally, diagonally at the same time (only works # for nxn grid) for i in range(_a ): for j in range(n_rows - 3 ): lowerCamelCase__ = grid[j][i] * grid[j + 1][i] * grid[j + 2][i] * grid[j + 3][i] lowerCamelCase__ = grid[i][j] * grid[i][j + 1] * grid[i][j + 2] * grid[i][j + 3] # Left-to-right diagonal (\) product if i < n_columns - 3: lowerCamelCase__ = ( grid[i][j] * grid[i + 1][j + 1] * grid[i + 2][j + 2] * grid[i + 3][j + 3] ) # Right-to-left diagonal(/) product if i > 2: lowerCamelCase__ = ( grid[i][j] * grid[i - 1][j + 1] * grid[i - 2][j + 2] * grid[i - 3][j + 3] ) lowerCamelCase__ = max( _a , _a , _a , _a ) if max_product > largest: lowerCamelCase__ = max_product return largest def snake_case ( )-> Optional[int]: '''simple docstring''' lowerCamelCase__ = [] with open(os.path.dirname(_a ) + '/grid.txt' ) as file: for line in file: grid.append(line.strip('\n' ).split(' ' ) ) lowerCamelCase__ = [[int(_a ) for i in grid[j]] for j in range(len(_a ) )] return largest_product(_a ) if __name__ == "__main__": print(solution())
659
"""simple docstring""" def snake_case ( _a: int , _a: list[int] , _a: int )-> int: '''simple docstring''' def count_of_possible_combinations(_a: int ) -> int: if target < 0: return 0 if target == 0: return 1 return sum(count_of_possible_combinations(target - item ) for item in array ) return count_of_possible_combinations(_a ) def snake_case ( _a: int , _a: list[int] , _a: int )-> int: '''simple docstring''' def count_of_possible_combinations_with_dp_array( _a: int , _a: list[int] ) -> int: if target < 0: return 0 if target == 0: return 1 if dp_array[target] != -1: return dp_array[target] lowerCamelCase__ = sum( count_of_possible_combinations_with_dp_array(target - item , _a ) for item in array ) lowerCamelCase__ = answer return answer lowerCamelCase__ = [-1] * (target + 1) return count_of_possible_combinations_with_dp_array(_a , _a ) def snake_case ( _a: int , _a: list[int] , _a: int )-> int: '''simple docstring''' lowerCamelCase__ = [0] * (target + 1) lowerCamelCase__ = 1 for i in range(1 , target + 1 ): for j in range(_a ): if i - array[j] >= 0: dp_array[i] += dp_array[i - array[j]] return dp_array[target] if __name__ == "__main__": import doctest doctest.testmod() _snake_case = 3 _snake_case = 5 _snake_case = [1, 2, 5] print(combination_sum_iv(n, array, target))
659
1
"""simple docstring""" _snake_case = "0.21.0" from .accelerator import Accelerator from .big_modeling import ( cpu_offload, cpu_offload_with_hook, disk_offload, dispatch_model, init_empty_weights, init_on_device, load_checkpoint_and_dispatch, ) from .data_loader import skip_first_batches from .launchers import debug_launcher, notebook_launcher from .state import PartialState from .utils import ( DeepSpeedPlugin, DistributedDataParallelKwargs, DistributedType, FullyShardedDataParallelPlugin, GradScalerKwargs, InitProcessGroupKwargs, find_executable_batch_size, infer_auto_device_map, is_rich_available, load_checkpoint_in_model, synchronize_rng_states, ) if is_rich_available(): from .utils import rich
659
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available _snake_case = { "configuration_m2m_100": ["M2M_100_PRETRAINED_CONFIG_ARCHIVE_MAP", "M2M100Config", "M2M100OnnxConfig"], "tokenization_m2m_100": ["M2M100Tokenizer"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = [ "M2M_100_PRETRAINED_MODEL_ARCHIVE_LIST", "M2M100ForConditionalGeneration", "M2M100Model", "M2M100PreTrainedModel", ] if TYPE_CHECKING: from .configuration_mam_aaa import M2M_100_PRETRAINED_CONFIG_ARCHIVE_MAP, MaMaaaConfig, MaMaaaOnnxConfig from .tokenization_mam_aaa import MaMaaaTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mam_aaa import ( M2M_100_PRETRAINED_MODEL_ARCHIVE_LIST, MaMaaaForConditionalGeneration, MaMaaaModel, MaMaaaPreTrainedModel, ) else: import sys _snake_case = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
659
1
"""simple docstring""" _snake_case = "\n# Transformers installation\n! pip install transformers datasets\n# To install from source instead of the last release, comment the command above and uncomment the following one.\n# ! pip install git+https://github.com/huggingface/transformers.git\n" _snake_case = [{"type": "code", "content": INSTALL_CONTENT}] _snake_case = { "{processor_class}": "FakeProcessorClass", "{model_class}": "FakeModelClass", "{object_class}": "FakeObjectClass", }
659
"""simple docstring""" def snake_case ( _a: list[list[float]] )-> list[list[float]]: '''simple docstring''' lowerCamelCase__ = [] for data in source_data: for i, el in enumerate(_a ): if len(_a ) < i + 1: data_lists.append([] ) data_lists[i].append(float(_a ) ) return data_lists def snake_case ( _a: list[list[float]] , _a: list[int] )-> list[list[float]]: '''simple docstring''' lowerCamelCase__ = [] for dlist, weight in zip(_a , _a ): lowerCamelCase__ = min(_a ) lowerCamelCase__ = max(_a ) lowerCamelCase__ = [] # for weight 0 score is 1 - actual score if weight == 0: for item in dlist: try: score.append(1 - ((item - mind) / (maxd - mind)) ) except ZeroDivisionError: score.append(1 ) elif weight == 1: for item in dlist: try: score.append((item - mind) / (maxd - mind) ) except ZeroDivisionError: score.append(0 ) # weight not 0 or 1 else: lowerCamelCase__ = F'Invalid weight of {weight:f} provided' raise ValueError(_a ) score_lists.append(_a ) return score_lists def snake_case ( _a: list[list[float]] )-> list[float]: '''simple docstring''' lowerCamelCase__ = [0 for i in range(len(score_lists[0] ) )] for slist in score_lists: for j, ele in enumerate(_a ): lowerCamelCase__ = final_scores[j] + ele return final_scores def snake_case ( _a: list[list[float]] , _a: list[int] )-> list[list[float]]: '''simple docstring''' lowerCamelCase__ = get_data(_a ) lowerCamelCase__ = calculate_each_score(_a , _a ) lowerCamelCase__ = generate_final_scores(_a ) # append scores to source data for i, ele in enumerate(_a ): source_data[i].append(_a ) return source_data
659
1
"""simple docstring""" import math import time from transformers import Trainer, is_torch_tpu_available from transformers.trainer_utils import PredictionOutput, speed_metrics if is_torch_tpu_available(check_device=False): import torch_xla.core.xla_model as xm import torch_xla.debug.metrics as met class _a ( SCREAMING_SNAKE_CASE_ ): def __init__( self : List[str] , *SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : str=None , SCREAMING_SNAKE_CASE__ : Optional[int]=None , **SCREAMING_SNAKE_CASE__ : Dict ): super().__init__(*SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = eval_examples lowerCamelCase__ = post_process_function def _UpperCamelCase ( self : str , SCREAMING_SNAKE_CASE__ : List[str]=None , SCREAMING_SNAKE_CASE__ : Tuple=None , SCREAMING_SNAKE_CASE__ : Optional[int]=None , SCREAMING_SNAKE_CASE__ : str = "eval" ): lowerCamelCase__ = self.eval_dataset if eval_dataset is None else eval_dataset lowerCamelCase__ = self.get_eval_dataloader(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = self.eval_examples if eval_examples is None else eval_examples # Temporarily disable metric computation, we will do it in the loop here. lowerCamelCase__ = self.compute_metrics lowerCamelCase__ = None lowerCamelCase__ = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop lowerCamelCase__ = time.time() try: lowerCamelCase__ = eval_loop( SCREAMING_SNAKE_CASE__ , description='Evaluation' , prediction_loss_only=True if compute_metrics is None else None , ignore_keys=SCREAMING_SNAKE_CASE__ , metric_key_prefix=SCREAMING_SNAKE_CASE__ , ) finally: lowerCamelCase__ = compute_metrics lowerCamelCase__ = self.args.eval_batch_size * self.args.world_size if F'{metric_key_prefix}_jit_compilation_time' in output.metrics: start_time += output.metrics[F'{metric_key_prefix}_jit_compilation_time'] output.metrics.update( speed_metrics( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , num_samples=output.num_samples , num_steps=math.ceil(output.num_samples / total_batch_size ) , ) ) if self.post_process_function is not None and self.compute_metrics is not None and self.args.should_save: # Only the main node write the results by default lowerCamelCase__ = self.post_process_function(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , output.predictions ) lowerCamelCase__ = self.compute_metrics(SCREAMING_SNAKE_CASE__ ) # Prefix all keys with metric_key_prefix + '_' for key in list(metrics.keys() ): if not key.startswith(F'{metric_key_prefix}_' ): lowerCamelCase__ = metrics.pop(SCREAMING_SNAKE_CASE__ ) metrics.update(output.metrics ) else: lowerCamelCase__ = output.metrics if self.args.should_log: # Only the main node log the results by default self.log(SCREAMING_SNAKE_CASE__ ) if self.args.tpu_metrics_debug or self.args.debug: # tpu-comment: Logging debug metrics for PyTorch/XLA (compile, execute times, ops, etc.) xm.master_print(met.metrics_report() ) lowerCamelCase__ = self.callback_handler.on_evaluate(self.args , self.state , self.control , SCREAMING_SNAKE_CASE__ ) return metrics def _UpperCamelCase ( self : List[Any] , SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : Optional[int]=None , SCREAMING_SNAKE_CASE__ : str = "test" ): lowerCamelCase__ = self.get_test_dataloader(SCREAMING_SNAKE_CASE__ ) # Temporarily disable metric computation, we will do it in the loop here. lowerCamelCase__ = self.compute_metrics lowerCamelCase__ = None lowerCamelCase__ = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop lowerCamelCase__ = time.time() try: lowerCamelCase__ = eval_loop( SCREAMING_SNAKE_CASE__ , description='Prediction' , prediction_loss_only=True if compute_metrics is None else None , ignore_keys=SCREAMING_SNAKE_CASE__ , metric_key_prefix=SCREAMING_SNAKE_CASE__ , ) finally: lowerCamelCase__ = compute_metrics lowerCamelCase__ = self.args.eval_batch_size * self.args.world_size if F'{metric_key_prefix}_jit_compilation_time' in output.metrics: start_time += output.metrics[F'{metric_key_prefix}_jit_compilation_time'] output.metrics.update( speed_metrics( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , num_samples=output.num_samples , num_steps=math.ceil(output.num_samples / total_batch_size ) , ) ) if self.post_process_function is None or self.compute_metrics is None: return output lowerCamelCase__ = self.post_process_function(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , output.predictions , 'predict' ) lowerCamelCase__ = self.compute_metrics(SCREAMING_SNAKE_CASE__ ) # Prefix all keys with metric_key_prefix + '_' for key in list(metrics.keys() ): if not key.startswith(F'{metric_key_prefix}_' ): lowerCamelCase__ = metrics.pop(SCREAMING_SNAKE_CASE__ ) metrics.update(output.metrics ) return PredictionOutput(predictions=predictions.predictions , label_ids=predictions.label_ids , metrics=SCREAMING_SNAKE_CASE__ )
659
"""simple docstring""" from __future__ import annotations from math import gcd def snake_case ( _a: int , _a: int = 2 , _a: int = 1 , _a: int = 3 , )-> int | None: '''simple docstring''' if num < 2: raise ValueError('The input value cannot be less than 2' ) # Because of the relationship between ``f(f(x))`` and ``f(x)``, this # algorithm struggles to find factors that are divisible by two. # As a workaround, we specifically check for two and even inputs. # See: https://math.stackexchange.com/a/2856214/165820 if num > 2 and num % 2 == 0: return 2 # Pollard's Rho algorithm requires a function that returns pseudorandom # values between 0 <= X < ``num``. It doesn't need to be random in the # sense that the output value is cryptographically secure or difficult # to calculate, it only needs to be random in the sense that all output # values should be equally likely to appear. # For this reason, Pollard suggested using ``f(x) = (x**2 - 1) % num`` # However, the success of Pollard's algorithm isn't guaranteed and is # determined in part by the initial seed and the chosen random function. # To make retries easier, we will instead use ``f(x) = (x**2 + C) % num`` # where ``C`` is a value that we can modify between each attempt. def rand_fn(_a: int , _a: int , _a: int ) -> int: return (pow(_a , 2 ) + step) % modulus for _ in range(_a ): # These track the position within the cycle detection logic. lowerCamelCase__ = seed lowerCamelCase__ = seed while True: # At each iteration, the tortoise moves one step and the hare moves two. lowerCamelCase__ = rand_fn(_a , _a , _a ) lowerCamelCase__ = rand_fn(_a , _a , _a ) lowerCamelCase__ = rand_fn(_a , _a , _a ) # At some point both the tortoise and the hare will enter a cycle whose # length ``p`` is a divisor of ``num``. Once in that cycle, at some point # the tortoise and hare will end up on the same value modulo ``p``. # We can detect when this happens because the position difference between # the tortoise and the hare will share a common divisor with ``num``. lowerCamelCase__ = gcd(hare - tortoise , _a ) if divisor == 1: # No common divisor yet, just keep searching. continue else: # We found a common divisor! if divisor == num: # Unfortunately, the divisor is ``num`` itself and is useless. break else: # The divisor is a nontrivial factor of ``num``! return divisor # If we made it here, then this attempt failed. # We need to pick a new starting seed for the tortoise and hare # in addition to a new step value for the random function. # To keep this example implementation deterministic, the # new values will be generated based on currently available # values instead of using something like ``random.randint``. # We can use the hare's position as the new seed. # This is actually what Richard Brent's the "optimized" variant does. lowerCamelCase__ = hare # The new step value for the random function can just be incremented. # At first the results will be similar to what the old function would # have produced, but the value will quickly diverge after a bit. step += 1 # We haven't found a divisor within the requested number of attempts. # We were unlucky or ``num`` itself is actually prime. return None if __name__ == "__main__": import argparse _snake_case = argparse.ArgumentParser() parser.add_argument( "num", type=int, help="The value to find a divisor of", ) parser.add_argument( "--attempts", type=int, default=3, help="The number of attempts before giving up", ) _snake_case = parser.parse_args() _snake_case = pollard_rho(args.num, attempts=args.attempts) if divisor is None: print(f"""{args.num} is probably prime""") else: _snake_case = args.num // divisor print(f"""{args.num} = {divisor} * {quotient}""")
659
1
"""simple docstring""" # Copyright 2023 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available _snake_case = {"configuration_timm_backbone": ["TimmBackboneConfig"]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = ["TimmBackbone"] if TYPE_CHECKING: from .configuration_timm_backbone import TimmBackboneConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_timm_backbone import TimmBackbone else: import sys _snake_case = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
659
"""simple docstring""" from ...utils import ( OptionalDependencyNotAvailable, is_torch_available, is_transformers_available, is_transformers_version, ) try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import ShapEPipeline else: from .camera import create_pan_cameras from .pipeline_shap_e import ShapEPipeline from .pipeline_shap_e_img2img import ShapEImgaImgPipeline from .renderer import ( BoundingBoxVolume, ImportanceRaySampler, MLPNeRFModelOutput, MLPNeRSTFModel, ShapEParamsProjModel, ShapERenderer, StratifiedRaySampler, VoidNeRFModel, )
659
1
"""simple docstring""" def snake_case ( _a: Union[str, Any] )-> Optional[int]: # noqa: E741 '''simple docstring''' lowerCamelCase__ = len(_a ) lowerCamelCase__ = 0 lowerCamelCase__ = [0] * n lowerCamelCase__ = [False] * n lowerCamelCase__ = [False] * n def dfs(_a: List[Any] , _a: Optional[int] , _a: Optional[Any] , _a: List[Any] ): if parent == root: out_edge_count += 1 lowerCamelCase__ = True lowerCamelCase__ = at for to in l[at]: if to == parent: pass elif not visited[to]: lowerCamelCase__ = dfs(_a , _a , _a , _a ) lowerCamelCase__ = min(low[at] , low[to] ) # AP found via bridge if at < low[to]: lowerCamelCase__ = True # AP found via cycle if at == low[to]: lowerCamelCase__ = True else: lowerCamelCase__ = min(low[at] , _a ) return out_edge_count for i in range(_a ): if not visited[i]: lowerCamelCase__ = 0 lowerCamelCase__ = dfs(_a , _a , -1 , _a ) lowerCamelCase__ = out_edge_count > 1 for x in range(len(_a ) ): if is_art[x] is True: print(_a ) # Adjacency list of graph _snake_case = { 0: [1, 2], 1: [0, 2], 2: [0, 1, 3, 5], 3: [2, 4], 4: [3], 5: [2, 6, 8], 6: [5, 7], 7: [6, 8], 8: [5, 7], } compute_ap(data)
659
"""simple docstring""" from __future__ import annotations _snake_case = [ [-1, 0], # left [0, -1], # down [1, 0], # right [0, 1], # up ] def snake_case ( _a: list[list[int]] , _a: list[int] , _a: list[int] , _a: int , _a: list[list[int]] , )-> tuple[list[list[int]], list[list[int]]]: '''simple docstring''' lowerCamelCase__ = [ [0 for col in range(len(grid[0] ) )] for row in range(len(_a ) ) ] # the reference grid lowerCamelCase__ = 1 lowerCamelCase__ = [ [0 for col in range(len(grid[0] ) )] for row in range(len(_a ) ) ] # the action grid lowerCamelCase__ = init[0] lowerCamelCase__ = init[1] lowerCamelCase__ = 0 lowerCamelCase__ = g + heuristic[x][y] # cost from starting cell to destination cell lowerCamelCase__ = [[f, g, x, y]] lowerCamelCase__ = False # flag that is set when search is complete lowerCamelCase__ = False # flag set if we can't find expand while not found and not resign: if len(_a ) == 0: raise ValueError('Algorithm is unable to find solution' ) else: # to choose the least costliest action so as to move closer to the goal cell.sort() cell.reverse() lowerCamelCase__ = cell.pop() lowerCamelCase__ = next_cell[2] lowerCamelCase__ = next_cell[3] lowerCamelCase__ = next_cell[1] if x == goal[0] and y == goal[1]: lowerCamelCase__ = True else: for i in range(len(_a ) ): # to try out different valid actions lowerCamelCase__ = x + DIRECTIONS[i][0] lowerCamelCase__ = y + DIRECTIONS[i][1] if xa >= 0 and xa < len(_a ) and ya >= 0 and ya < len(grid[0] ): if closed[xa][ya] == 0 and grid[xa][ya] == 0: lowerCamelCase__ = g + cost lowerCamelCase__ = ga + heuristic[xa][ya] cell.append([fa, ga, xa, ya] ) lowerCamelCase__ = 1 lowerCamelCase__ = i lowerCamelCase__ = [] lowerCamelCase__ = goal[0] lowerCamelCase__ = goal[1] invpath.append([x, y] ) # we get the reverse path from here while x != init[0] or y != init[1]: lowerCamelCase__ = x - DIRECTIONS[action[x][y]][0] lowerCamelCase__ = y - DIRECTIONS[action[x][y]][1] lowerCamelCase__ = xa lowerCamelCase__ = ya invpath.append([x, y] ) lowerCamelCase__ = [] for i in range(len(_a ) ): path.append(invpath[len(_a ) - 1 - i] ) return path, action if __name__ == "__main__": _snake_case = [ [0, 1, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles [0, 1, 0, 0, 0, 0], [0, 1, 0, 0, 1, 0], [0, 0, 0, 0, 1, 0], ] _snake_case = [0, 0] # all coordinates are given in format [y,x] _snake_case = [len(grid) - 1, len(grid[0]) - 1] _snake_case = 1 # the cost map which pushes the path closer to the goal _snake_case = [[0 for row in range(len(grid[0]))] for col in range(len(grid))] for i in range(len(grid)): for j in range(len(grid[0])): _snake_case = abs(i - goal[0]) + abs(j - goal[1]) if grid[i][j] == 1: # added extra penalty in the heuristic map _snake_case = 99 _snake_case , _snake_case = search(grid, init, goal, cost, heuristic) print("ACTION MAP") for i in range(len(action)): print(action[i]) for i in range(len(path)): print(path[i])
659
1
"""simple docstring""" from typing import Any, Dict, List, Optional, Tuple, Union import torch from torch import nn from torch.utils.data import DistributedSampler, RandomSampler from transformers import PreTrainedModel, Trainer, logging from transformers.integrations import is_fairscale_available from transformers.models.fsmt.configuration_fsmt import FSMTConfig from transformers.optimization 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_linear_schedule_with_warmup, get_polynomial_decay_schedule_with_warmup, ) from transformers.trainer_pt_utils import get_tpu_sampler from transformers.training_args import ParallelMode from transformers.utils import is_torch_tpu_available if is_fairscale_available(): from fairscale.optim import OSS _snake_case = logging.get_logger(__name__) _snake_case = { "linear": get_linear_schedule_with_warmup, "cosine": get_cosine_schedule_with_warmup, "cosine_w_restarts": get_cosine_with_hard_restarts_schedule_with_warmup, "polynomial": get_polynomial_decay_schedule_with_warmup, "constant": get_constant_schedule, "constant_w_warmup": get_constant_schedule_with_warmup, } class _a ( SCREAMING_SNAKE_CASE_ ): def __init__( self : str , SCREAMING_SNAKE_CASE__ : Dict=None , SCREAMING_SNAKE_CASE__ : Optional[Any]=None , *SCREAMING_SNAKE_CASE__ : Dict , **SCREAMING_SNAKE_CASE__ : List[str] ): super().__init__(*SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) if config is None: assert isinstance(self.model , SCREAMING_SNAKE_CASE__ ), ( "If no `config` is passed the model to be trained has to be of type `PreTrainedModel`, but is" F' {self.model.__class__}' ) lowerCamelCase__ = self.model.config else: lowerCamelCase__ = config lowerCamelCase__ = data_args lowerCamelCase__ = self.config.tgt_vocab_size if isinstance(self.config , SCREAMING_SNAKE_CASE__ ) else self.config.vocab_size if self.args.label_smoothing != 0 or (self.data_args is not None and self.data_args.ignore_pad_token_for_loss): assert self.config.pad_token_id is not None, ( "Make sure that `config.pad_token_id` is correcly defined when ignoring `pad_token` for loss" " calculation or doing label smoothing." ) if self.config.pad_token_id is None and self.config.eos_token_id is not None: logger.warning( F'The `config.pad_token_id` is `None`. Using `config.eos_token_id` = {self.config.eos_token_id} for' ' padding..' ) if self.args.label_smoothing == 0: lowerCamelCase__ = torch.nn.CrossEntropyLoss(ignore_index=self.config.pad_token_id ) else: # dynamically import label_smoothed_nll_loss from utils import label_smoothed_nll_loss lowerCamelCase__ = label_smoothed_nll_loss def _UpperCamelCase ( self : str , SCREAMING_SNAKE_CASE__ : int ): if self.optimizer is None: lowerCamelCase__ = ['bias', 'LayerNorm.weight'] lowerCamelCase__ = [ { 'params': [p for n, p in self.model.named_parameters() if not any(nd in n for nd in no_decay )], 'weight_decay': self.args.weight_decay, }, { 'params': [p for n, p in self.model.named_parameters() if any(nd in n for nd in no_decay )], 'weight_decay': 0.0, }, ] lowerCamelCase__ = Adafactor if self.args.adafactor else AdamW if self.args.adafactor: lowerCamelCase__ = Adafactor lowerCamelCase__ = {'scale_parameter': False, 'relative_step': False} else: lowerCamelCase__ = AdamW lowerCamelCase__ = { 'betas': (self.args.adam_betaa, self.args.adam_betaa), 'eps': self.args.adam_epsilon, } lowerCamelCase__ = self.args.learning_rate if self.sharded_ddp: lowerCamelCase__ = OSS( params=SCREAMING_SNAKE_CASE__ , optim=SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ , ) else: lowerCamelCase__ = optimizer_cls(SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) if self.lr_scheduler is None: lowerCamelCase__ = self._get_lr_scheduler(SCREAMING_SNAKE_CASE__ ) else: # ignoring --lr_scheduler logger.warning('scheduler is passed to `Seq2SeqTrainer`, `--lr_scheduler` arg is ignored.' ) def _UpperCamelCase ( self : Any , SCREAMING_SNAKE_CASE__ : Union[str, Any] ): lowerCamelCase__ = arg_to_scheduler[self.args.lr_scheduler] if self.args.lr_scheduler == "constant": lowerCamelCase__ = schedule_func(self.optimizer ) elif self.args.lr_scheduler == "constant_w_warmup": lowerCamelCase__ = schedule_func(self.optimizer , num_warmup_steps=self.args.warmup_steps ) else: lowerCamelCase__ = schedule_func( self.optimizer , num_warmup_steps=self.args.warmup_steps , num_training_steps=SCREAMING_SNAKE_CASE__ ) return scheduler def _UpperCamelCase ( self : Any ): if isinstance(self.train_dataset , torch.utils.data.IterableDataset ): return None elif is_torch_tpu_available(): return get_tpu_sampler(self.train_dataset ) else: if self.args.sortish_sampler: self.train_dataset.make_sortish_sampler( self.args.per_device_train_batch_size , distributed=(self.args.parallel_mode == ParallelMode.DISTRIBUTED) , ) return ( RandomSampler(self.train_dataset ) if self.args.local_rank == -1 else DistributedSampler(self.train_dataset ) ) def _UpperCamelCase ( self : str , SCREAMING_SNAKE_CASE__ : Tuple , SCREAMING_SNAKE_CASE__ : List[Any] , SCREAMING_SNAKE_CASE__ : Tuple ): if self.args.label_smoothing == 0: if self.data_args is not None and self.data_args.ignore_pad_token_for_loss: # force training to ignore pad token lowerCamelCase__ = model(**SCREAMING_SNAKE_CASE__ , use_cache=SCREAMING_SNAKE_CASE__ )[0] lowerCamelCase__ = self.loss_fn(logits.view(-1 , logits.shape[-1] ) , labels.view(-1 ) ) else: # compute usual loss via models lowerCamelCase__ , lowerCamelCase__ = model(**SCREAMING_SNAKE_CASE__ , labels=SCREAMING_SNAKE_CASE__ , use_cache=SCREAMING_SNAKE_CASE__ )[:2] else: # compute label smoothed loss lowerCamelCase__ = model(**SCREAMING_SNAKE_CASE__ , use_cache=SCREAMING_SNAKE_CASE__ )[0] lowerCamelCase__ = torch.nn.functional.log_softmax(SCREAMING_SNAKE_CASE__ , dim=-1 ) lowerCamelCase__ , lowerCamelCase__ = self.loss_fn(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , self.args.label_smoothing , ignore_index=self.config.pad_token_id ) return loss, logits def _UpperCamelCase ( self : Tuple , SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : int ): lowerCamelCase__ = inputs.pop('labels' ) lowerCamelCase__ , lowerCamelCase__ = self._compute_loss(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) return loss def _UpperCamelCase ( self : str , SCREAMING_SNAKE_CASE__ : nn.Module , SCREAMING_SNAKE_CASE__ : Dict[str, Union[torch.Tensor, Any]] , SCREAMING_SNAKE_CASE__ : bool , SCREAMING_SNAKE_CASE__ : Optional[List[str]] = None , ): lowerCamelCase__ = self._prepare_inputs(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = { 'max_length': self.data_args.val_max_target_length if self.data_args is not None else self.config.max_length, 'num_beams': self.data_args.eval_beams if self.data_args is not None else self.config.num_beams, } if self.args.predict_with_generate and not self.args.prediction_loss_only: lowerCamelCase__ = self.model.generate( inputs['input_ids'] , attention_mask=inputs['attention_mask'] , **SCREAMING_SNAKE_CASE__ , ) # in case the batch is shorter than max length, the output should be padded if generated_tokens.shape[-1] < gen_kwargs["max_length"]: lowerCamelCase__ = self._pad_tensors_to_max_len(SCREAMING_SNAKE_CASE__ , gen_kwargs['max_length'] ) lowerCamelCase__ = inputs.pop('labels' ) with torch.no_grad(): # compute loss on predict data lowerCamelCase__ , lowerCamelCase__ = self._compute_loss(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = loss.mean().detach() if self.args.prediction_loss_only: return (loss, None, None) lowerCamelCase__ = generated_tokens if self.args.predict_with_generate else logits if labels.shape[-1] < gen_kwargs["max_length"]: lowerCamelCase__ = self._pad_tensors_to_max_len(SCREAMING_SNAKE_CASE__ , gen_kwargs['max_length'] ) return (loss, logits, labels) def _UpperCamelCase ( self : Optional[Any] , SCREAMING_SNAKE_CASE__ : List[Any] , SCREAMING_SNAKE_CASE__ : Tuple ): # If PAD token is not defined at least EOS token has to be defined lowerCamelCase__ = self.config.pad_token_id if self.config.pad_token_id is not None else self.config.eos_token_id if pad_token_id is None: raise ValueError( 'Make sure that either `config.pad_token_id` or `config.eos_token_id` is defined if tensor has to be' F' padded to `max_length`={max_length}' ) lowerCamelCase__ = pad_token_id * torch.ones( (tensor.shape[0], max_length) , dtype=tensor.dtype , device=tensor.device ) lowerCamelCase__ = tensor return padded_tensor
659
"""simple docstring""" def snake_case ( _a: int = 4000000 )-> int: '''simple docstring''' lowerCamelCase__ = [0, 1] lowerCamelCase__ = 0 while fib[i] <= n: fib.append(fib[i] + fib[i + 1] ) if fib[i + 2] > n: break i += 1 lowerCamelCase__ = 0 for j in range(len(_a ) - 1 ): if fib[j] % 2 == 0: total += fib[j] return total if __name__ == "__main__": print(f"""{solution() = }""")
659
1
"""simple docstring""" import math def snake_case ( _a: list , _a: int = 0 , _a: int = 0 )-> list: '''simple docstring''' lowerCamelCase__ = end or len(_a ) for i in range(_a , _a ): lowerCamelCase__ = i lowerCamelCase__ = array[i] while temp_index != start and temp_index_value < array[temp_index - 1]: lowerCamelCase__ = array[temp_index - 1] temp_index -= 1 lowerCamelCase__ = temp_index_value return array def snake_case ( _a: list , _a: int , _a: int )-> None: # Max Heap '''simple docstring''' lowerCamelCase__ = index lowerCamelCase__ = 2 * index + 1 # Left Node lowerCamelCase__ = 2 * index + 2 # Right Node if left_index < heap_size and array[largest] < array[left_index]: lowerCamelCase__ = left_index if right_index < heap_size and array[largest] < array[right_index]: lowerCamelCase__ = right_index if largest != index: lowerCamelCase__ , lowerCamelCase__ = array[largest], array[index] heapify(_a , _a , _a ) def snake_case ( _a: list )-> list: '''simple docstring''' lowerCamelCase__ = len(_a ) for i in range(n // 2 , -1 , -1 ): heapify(_a , _a , _a ) for i in range(n - 1 , 0 , -1 ): lowerCamelCase__ , lowerCamelCase__ = array[0], array[i] heapify(_a , 0 , _a ) return array def snake_case ( _a: list , _a: int , _a: int , _a: int )-> int: '''simple docstring''' if (array[first_index] > array[middle_index]) != ( array[first_index] > array[last_index] ): return array[first_index] elif (array[middle_index] > array[first_index]) != ( array[middle_index] > array[last_index] ): return array[middle_index] else: return array[last_index] def snake_case ( _a: list , _a: int , _a: int , _a: int )-> int: '''simple docstring''' lowerCamelCase__ = low lowerCamelCase__ = high while True: while array[i] < pivot: i += 1 j -= 1 while pivot < array[j]: j -= 1 if i >= j: return i lowerCamelCase__ , lowerCamelCase__ = array[j], array[i] i += 1 def snake_case ( _a: list )-> list: '''simple docstring''' if len(_a ) == 0: return array lowerCamelCase__ = 2 * math.ceil(math.loga(len(_a ) ) ) lowerCamelCase__ = 16 return intro_sort(_a , 0 , len(_a ) , _a , _a ) def snake_case ( _a: list , _a: int , _a: int , _a: int , _a: int )-> list: '''simple docstring''' while end - start > size_threshold: if max_depth == 0: return heap_sort(_a ) max_depth -= 1 lowerCamelCase__ = median_of_a(_a , _a , start + ((end - start) // 2) + 1 , end - 1 ) lowerCamelCase__ = partition(_a , _a , _a , _a ) intro_sort(_a , _a , _a , _a , _a ) lowerCamelCase__ = p return insertion_sort(_a , _a , _a ) if __name__ == "__main__": import doctest doctest.testmod() _snake_case = input("Enter numbers separated by a comma : ").strip() _snake_case = [float(item) for item in user_input.split(",")] print(sort(unsorted))
659
"""simple docstring""" def snake_case ( _a: List[Any] , _a: Any , _a: str , _a: List[Any] )-> List[Any]: '''simple docstring''' lowerCamelCase__ = [False] * len(_a ) lowerCamelCase__ = [] queue.append(_a ) lowerCamelCase__ = True while queue: lowerCamelCase__ = queue.pop(0 ) for ind in range(len(graph[u] ) ): if visited[ind] is False and graph[u][ind] > 0: queue.append(_a ) lowerCamelCase__ = True lowerCamelCase__ = u return visited[t] def snake_case ( _a: List[Any] , _a: str , _a: List[str] )-> Optional[int]: '''simple docstring''' lowerCamelCase__ = [-1] * (len(_a )) lowerCamelCase__ = 0 while bfs(_a , _a , _a , _a ): lowerCamelCase__ = float('Inf' ) lowerCamelCase__ = sink while s != source: # Find the minimum value in select path lowerCamelCase__ = min(_a , graph[parent[s]][s] ) lowerCamelCase__ = parent[s] max_flow += path_flow lowerCamelCase__ = sink while v != source: lowerCamelCase__ = parent[v] graph[u][v] -= path_flow graph[v][u] += path_flow lowerCamelCase__ = parent[v] return max_flow _snake_case = [ [0, 16, 13, 0, 0, 0], [0, 0, 10, 12, 0, 0], [0, 4, 0, 0, 14, 0], [0, 0, 9, 0, 0, 20], [0, 0, 0, 7, 0, 4], [0, 0, 0, 0, 0, 0], ] _snake_case , _snake_case = 0, 5 print(ford_fulkerson(graph, source, sink))
659
1
"""simple docstring""" 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 snake_case ( _a: int )-> int: '''simple docstring''' lowerCamelCase__ = filter(lambda _a : p.requires_grad , model.parameters() ) lowerCamelCase__ = sum([np.prod(p.size() ) for p in model_parameters] ) return params _snake_case = logging.getLogger(__name__) def snake_case ( _a: str , _a: Union[str, Any] )-> str: '''simple docstring''' if metric == "rouge2": lowerCamelCase__ = '{val_avg_rouge2:.4f}-{step_count}' elif metric == "bleu": lowerCamelCase__ = '{val_avg_bleu:.4f}-{step_count}' elif metric == "em": lowerCamelCase__ = '{val_avg_em:.4f}-{step_count}' elif metric == "loss": lowerCamelCase__ = '{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.' ) lowerCamelCase__ = ModelCheckpoint( dirpath=_a , filename=_a , monitor=F'val_{metric}' , mode='max' , save_top_k=1 , every_n_epochs=1 , ) return checkpoint_callback def snake_case ( _a: int , _a: Dict )-> List[Any]: '''simple docstring''' return EarlyStopping( monitor=F'val_{metric}' , mode='min' if 'loss' in metric else 'max' , patience=_a , verbose=_a , ) class _a ( pl.Callback ): def _UpperCamelCase ( self : List[Any] , SCREAMING_SNAKE_CASE__ : Optional[Any] , SCREAMING_SNAKE_CASE__ : Dict ): lowerCamelCase__ = {F'lr_group_{i}': param['lr'] for i, param in enumerate(pl_module.trainer.optimizers[0].param_groups )} pl_module.logger.log_metrics(SCREAMING_SNAKE_CASE__ ) @rank_zero_only def _UpperCamelCase ( self : List[str] , SCREAMING_SNAKE_CASE__ : pl.Trainer , SCREAMING_SNAKE_CASE__ : pl.LightningModule , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : int=True ): logger.info(F'***** {type_path} results at step {trainer.global_step:05d} *****' ) lowerCamelCase__ = 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 lowerCamelCase__ = Path(pl_module.hparams.output_dir ) if type_path == "test": lowerCamelCase__ = od / 'test_results.txt' lowerCamelCase__ = 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. lowerCamelCase__ = od / F'{type_path}_results/{trainer.global_step:05d}.txt' lowerCamelCase__ = od / F'{type_path}_generations/{trainer.global_step:05d}.txt' results_file.parent.mkdir(exist_ok=SCREAMING_SNAKE_CASE__ ) generations_file.parent.mkdir(exist_ok=SCREAMING_SNAKE_CASE__ ) with open(SCREAMING_SNAKE_CASE__ , 'a+' ) as writer: for key in sorted(SCREAMING_SNAKE_CASE__ ): if key in ["log", "progress_bar", "preds"]: continue lowerCamelCase__ = metrics[key] if isinstance(SCREAMING_SNAKE_CASE__ , torch.Tensor ): lowerCamelCase__ = val.item() lowerCamelCase__ = F'{key}: {val:.6f}\n' writer.write(SCREAMING_SNAKE_CASE__ ) if not save_generations: return if "preds" in metrics: lowerCamelCase__ = '\n'.join(metrics['preds'] ) generations_file.open('w+' ).write(SCREAMING_SNAKE_CASE__ ) @rank_zero_only def _UpperCamelCase ( self : List[str] , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : str ): try: lowerCamelCase__ = pl_module.model.model.num_parameters() except AttributeError: lowerCamelCase__ = pl_module.model.num_parameters() lowerCamelCase__ = count_trainable_parameters(SCREAMING_SNAKE_CASE__ ) # 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 _UpperCamelCase ( self : int , SCREAMING_SNAKE_CASE__ : pl.Trainer , SCREAMING_SNAKE_CASE__ : pl.LightningModule ): save_json(pl_module.metrics , pl_module.metrics_save_path ) return self._write_logs(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , 'test' ) @rank_zero_only def _UpperCamelCase ( self : Optional[int] , SCREAMING_SNAKE_CASE__ : pl.Trainer , SCREAMING_SNAKE_CASE__ : Optional[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")
659
"""simple docstring""" import json import multiprocessing import os import re from collections import defaultdict import torch from accelerate import Accelerator from accelerate.utils import set_seed from arguments import HumanEvalArguments from datasets import load_dataset, load_metric from torch.utils.data import IterableDataset from torch.utils.data.dataloader import DataLoader from tqdm import tqdm import transformers from transformers import AutoModelForCausalLM, AutoTokenizer, HfArgumentParser, StoppingCriteria, StoppingCriteriaList _snake_case = ["\nclass", "\ndef", "\n#", "\n@", "\nprint", "\nif"] class _a ( SCREAMING_SNAKE_CASE_ ): def __init__( self : List[str] , SCREAMING_SNAKE_CASE__ : Any , SCREAMING_SNAKE_CASE__ : List[Any] , SCREAMING_SNAKE_CASE__ : int=None , SCREAMING_SNAKE_CASE__ : Optional[int]=1 ): lowerCamelCase__ = tokenizer lowerCamelCase__ = dataset lowerCamelCase__ = len(SCREAMING_SNAKE_CASE__ ) if n_tasks is None else n_tasks lowerCamelCase__ = n_copies def __iter__( self : Any ): lowerCamelCase__ = [] for task in range(self.n_tasks ): # without strip, the model generate commented codes ... prompts.append(self.tokenizer.eos_token + self.dataset[task]['prompt'].strip() ) lowerCamelCase__ = self.tokenizer(SCREAMING_SNAKE_CASE__ , padding=SCREAMING_SNAKE_CASE__ , return_tensors='pt' ) for task in range(self.n_tasks ): for _ in range(self.n_copies ): yield { "ids": outputs.input_ids[task], "task_id": task, "input_len": outputs.attention_mask[task].sum(), } class _a ( SCREAMING_SNAKE_CASE_ ): def __init__( self : Dict , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : List[Any] , SCREAMING_SNAKE_CASE__ : Dict ): lowerCamelCase__ = start_length lowerCamelCase__ = eof_strings lowerCamelCase__ = tokenizer def __call__( self : Any , SCREAMING_SNAKE_CASE__ : Any , SCREAMING_SNAKE_CASE__ : List[str] , **SCREAMING_SNAKE_CASE__ : Dict ): lowerCamelCase__ = self.tokenizer.batch_decode(input_ids[:, self.start_length :] ) lowerCamelCase__ = [] for decoded_generation in decoded_generations: done.append(any(stop_string in decoded_generation for stop_string in self.eof_strings ) ) return all(SCREAMING_SNAKE_CASE__ ) def snake_case ( _a: List[Any] )-> Dict: '''simple docstring''' lowerCamelCase__ = re.split('(%s)' % '|'.join(_a ) , _a ) # last string should be "" return "".join(string_list[:-2] ) def snake_case ( _a: List[Any] , _a: Optional[int] , _a: str , _a: Union[str, Any] , _a: Dict , _a: Optional[int]=20 , **_a: Optional[int] )-> List[str]: '''simple docstring''' lowerCamelCase__ = defaultdict(_a ) # dict of list of generated tokens for step, batch in tqdm(enumerate(_a ) ): with torch.no_grad(): lowerCamelCase__ = batch['ids'].shape[-1] lowerCamelCase__ = accelerator.unwrap_model(_a ).generate( input_ids=batch['ids'][:, : batch['input_len']] , num_return_sequences=_a , **_a ) # each task is generated batch_size times lowerCamelCase__ = batch['task_id'].repeat(_a ) lowerCamelCase__ = accelerator.pad_across_processes( _a , dim=1 , pad_index=tokenizer.pad_token_id ) lowerCamelCase__ , lowerCamelCase__ = accelerator.gather((generated_tokens, generated_tasks) ) lowerCamelCase__ = generated_tokens.cpu().numpy() lowerCamelCase__ = generated_tasks.cpu().numpy() for task, generated_tokens in zip(_a , _a ): gen_token_dict[task].append(_a ) lowerCamelCase__ = [[] for _ in range(_a )] for task, generated_tokens in gen_token_dict.items(): for s in generated_tokens: lowerCamelCase__ = tokenizer.decode(_a , skip_special_tokens=_a , clean_up_tokenization_spaces=_a ) code_gens[task].append(remove_last_block(_a ) ) return code_gens def snake_case ( )-> Union[str, Any]: '''simple docstring''' lowerCamelCase__ = HfArgumentParser(_a ) lowerCamelCase__ = parser.parse_args() transformers.logging.set_verbosity_error() # enables code execution in code_eval metric lowerCamelCase__ = args.HF_ALLOW_CODE_EVAL # make sure tokenizer plays nice with multiprocessing lowerCamelCase__ = 'false' if args.num_workers is None: lowerCamelCase__ = multiprocessing.cpu_count() # Use dataset load to feed to accelerate lowerCamelCase__ = Accelerator() set_seed(args.seed , device_specific=_a ) # Load model and tokenizer lowerCamelCase__ = AutoTokenizer.from_pretrained(args.model_ckpt ) lowerCamelCase__ = tokenizer.eos_token lowerCamelCase__ = AutoModelForCausalLM.from_pretrained(args.model_ckpt ) # Generation settings lowerCamelCase__ = { 'do_sample': args.do_sample, 'temperature': args.temperature, 'max_new_tokens': args.max_new_tokens, 'top_p': args.top_p, 'top_k': args.top_k, 'stopping_criteria': StoppingCriteriaList([EndOfFunctionCriteria(0 , _a , _a )] ), } # Load evaluation dataset and metric lowerCamelCase__ = load_dataset('openai_humaneval' ) lowerCamelCase__ = load_metric('code_eval' ) lowerCamelCase__ = args.num_tasks if args.num_tasks is not None else len(human_eval['test'] ) lowerCamelCase__ = args.n_samples // args.batch_size lowerCamelCase__ = TokenizedDataset(_a , human_eval['test'] , n_copies=_a , n_tasks=_a ) # do not confuse args.batch_size, which is actually the num_return_sequences lowerCamelCase__ = DataLoader(_a , batch_size=1 ) # Run a quick test to see if code evaluation is enabled try: lowerCamelCase__ = code_eval_metric.compute(references=[''] , predictions=[['']] ) except ValueError as exception: print( 'Code evaluation not enabled. Read the warning below carefully and then use `--HF_ALLOW_CODE_EVAL="1"`' ' flag to enable code evaluation.' ) raise exception lowerCamelCase__ , lowerCamelCase__ = accelerator.prepare(_a , _a ) lowerCamelCase__ = complete_code( _a , _a , _a , _a , n_tasks=_a , batch_size=args.batch_size , **_a , ) if accelerator.is_main_process: lowerCamelCase__ = [] for task in tqdm(range(_a ) ): lowerCamelCase__ = human_eval['test'][task]['test'] lowerCamelCase__ = F'check({human_eval["test"][task]["entry_point"]})' references.append('\n' + test_func + '\n' + entry_point ) # Evaluate completions with "code_eval" metric lowerCamelCase__ , lowerCamelCase__ = code_eval_metric.compute( references=_a , predictions=_a , num_workers=args.num_workers ) print(F'Results: {pass_at_k}' ) # Save results to json file with open(args.output_file , 'w' ) as fp: json.dump(_a , _a ) # For some reason the folliwng seems to be necessary sometimes for code_eval to work nice with multiprocessing # https://stackoverflow.com/questions/60804599/python-multiprocessing-keeps-spawning-the-whole-script if __name__ == "__main__": main()
659
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, ) _snake_case = { "configuration_blenderbot": [ "BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP", "BlenderbotConfig", "BlenderbotOnnxConfig", ], "tokenization_blenderbot": ["BlenderbotTokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = ["BlenderbotTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = [ "BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST", "BlenderbotForCausalLM", "BlenderbotForConditionalGeneration", "BlenderbotModel", "BlenderbotPreTrainedModel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = [ "TFBlenderbotForConditionalGeneration", "TFBlenderbotModel", "TFBlenderbotPreTrainedModel", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = [ "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 _snake_case = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
659
"""simple docstring""" import argparse import json from tqdm import tqdm def snake_case ( )-> List[Any]: '''simple docstring''' lowerCamelCase__ = argparse.ArgumentParser() # Required parameters parser.add_argument( '--src_path' , type=_a , default='biencoder-nq-dev.json' , help='Path to raw DPR training data' , ) parser.add_argument( '--evaluation_set' , type=_a , help='where to store parsed evaluation_set file' , ) parser.add_argument( '--gold_data_path' , type=_a , help='where to store parsed gold_data_path file' , ) lowerCamelCase__ = parser.parse_args() with open(args.src_path , 'r' ) as src_file, open(args.evaluation_set , 'w' ) as eval_file, open( args.gold_data_path , 'w' ) as gold_file: lowerCamelCase__ = json.load(_a ) for dpr_record in tqdm(_a ): lowerCamelCase__ = dpr_record['question'] lowerCamelCase__ = [context['title'] for context in dpr_record['positive_ctxs']] eval_file.write(question + '\n' ) gold_file.write('\t'.join(_a ) + '\n' ) if __name__ == "__main__": main()
659
1
"""simple docstring""" import unittest from transformers import load_tool from .test_tools_common import ToolTesterMixin _snake_case = "\nHugging 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.\n\nIn March 2021, Hugging Face raised $40 million in a Series B funding round.[3]\n\nOn 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]\n" class _a ( unittest.TestCase , SCREAMING_SNAKE_CASE_ ): def _UpperCamelCase ( self : Any ): lowerCamelCase__ = load_tool('text-question-answering' ) self.tool.setup() lowerCamelCase__ = load_tool('text-question-answering' , remote=SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : List[str] ): lowerCamelCase__ = self.tool(SCREAMING_SNAKE_CASE__ , 'What did Hugging Face do in April 2021?' ) self.assertEqual(SCREAMING_SNAKE_CASE__ , 'launched the BigScience Research Workshop' ) def _UpperCamelCase ( self : Optional[int] ): lowerCamelCase__ = self.remote_tool(SCREAMING_SNAKE_CASE__ , 'What did Hugging Face do in April 2021?' ) self.assertEqual(SCREAMING_SNAKE_CASE__ , 'launched the BigScience Research Workshop' ) def _UpperCamelCase ( self : List[str] ): lowerCamelCase__ = self.tool(text=SCREAMING_SNAKE_CASE__ , question='What did Hugging Face do in April 2021?' ) self.assertEqual(SCREAMING_SNAKE_CASE__ , 'launched the BigScience Research Workshop' ) def _UpperCamelCase ( self : Any ): lowerCamelCase__ = self.remote_tool(text=SCREAMING_SNAKE_CASE__ , question='What did Hugging Face do in April 2021?' ) self.assertEqual(SCREAMING_SNAKE_CASE__ , 'launched the BigScience Research Workshop' )
659
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) _snake_case = { "configuration_blenderbot": [ "BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP", "BlenderbotConfig", "BlenderbotOnnxConfig", ], "tokenization_blenderbot": ["BlenderbotTokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = ["BlenderbotTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = [ "BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST", "BlenderbotForCausalLM", "BlenderbotForConditionalGeneration", "BlenderbotModel", "BlenderbotPreTrainedModel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = [ "TFBlenderbotForConditionalGeneration", "TFBlenderbotModel", "TFBlenderbotPreTrainedModel", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = [ "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 _snake_case = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
659
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_tokenizers_available, is_torch_available, is_vision_available, ) _snake_case = { "configuration_layoutlmv3": [ "LAYOUTLMV3_PRETRAINED_CONFIG_ARCHIVE_MAP", "LayoutLMv3Config", "LayoutLMv3OnnxConfig", ], "processing_layoutlmv3": ["LayoutLMv3Processor"], "tokenization_layoutlmv3": ["LayoutLMv3Tokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = ["LayoutLMv3TokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = [ "LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST", "LayoutLMv3ForQuestionAnswering", "LayoutLMv3ForSequenceClassification", "LayoutLMv3ForTokenClassification", "LayoutLMv3Model", "LayoutLMv3PreTrainedModel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = [ "TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST", "TFLayoutLMv3ForQuestionAnswering", "TFLayoutLMv3ForSequenceClassification", "TFLayoutLMv3ForTokenClassification", "TFLayoutLMv3Model", "TFLayoutLMv3PreTrainedModel", ] try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = ["LayoutLMv3FeatureExtractor"] _snake_case = ["LayoutLMv3ImageProcessor"] if TYPE_CHECKING: from .configuration_layoutlmva import ( LAYOUTLMV3_PRETRAINED_CONFIG_ARCHIVE_MAP, LayoutLMvaConfig, LayoutLMvaOnnxConfig, ) from .processing_layoutlmva import LayoutLMvaProcessor from .tokenization_layoutlmva import LayoutLMvaTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_layoutlmva_fast import LayoutLMvaTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_layoutlmva import ( LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST, LayoutLMvaForQuestionAnswering, LayoutLMvaForSequenceClassification, LayoutLMvaForTokenClassification, LayoutLMvaModel, LayoutLMvaPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_layoutlmva import ( TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST, TFLayoutLMvaForQuestionAnswering, TFLayoutLMvaForSequenceClassification, TFLayoutLMvaForTokenClassification, TFLayoutLMvaModel, TFLayoutLMvaPreTrainedModel, ) try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_layoutlmva import LayoutLMvaFeatureExtractor from .image_processing_layoutlmva import LayoutLMvaImageProcessor else: import sys _snake_case = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
659
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging _snake_case = logging.get_logger(__name__) _snake_case = { "microsoft/swinv2-tiny-patch4-window8-256": ( "https://huggingface.co/microsoft/swinv2-tiny-patch4-window8-256/resolve/main/config.json" ), } class _a ( SCREAMING_SNAKE_CASE_ ): a_ : Union[str, Any] = 'swinv2' a_ : Optional[int] = { 'num_attention_heads': 'num_heads', 'num_hidden_layers': 'num_layers', } def __init__( self : List[str] , SCREAMING_SNAKE_CASE__ : int=2_24 , SCREAMING_SNAKE_CASE__ : str=4 , SCREAMING_SNAKE_CASE__ : List[Any]=3 , SCREAMING_SNAKE_CASE__ : str=96 , SCREAMING_SNAKE_CASE__ : Dict=[2, 2, 6, 2] , SCREAMING_SNAKE_CASE__ : Union[str, Any]=[3, 6, 12, 24] , SCREAMING_SNAKE_CASE__ : Tuple=7 , SCREAMING_SNAKE_CASE__ : Union[str, Any]=4.0 , SCREAMING_SNAKE_CASE__ : List[str]=True , SCREAMING_SNAKE_CASE__ : List[Any]=0.0 , SCREAMING_SNAKE_CASE__ : Optional[int]=0.0 , SCREAMING_SNAKE_CASE__ : Dict=0.1 , SCREAMING_SNAKE_CASE__ : Tuple="gelu" , SCREAMING_SNAKE_CASE__ : Tuple=False , SCREAMING_SNAKE_CASE__ : Any=0.02 , SCREAMING_SNAKE_CASE__ : List[Any]=1e-5 , SCREAMING_SNAKE_CASE__ : int=32 , **SCREAMING_SNAKE_CASE__ : List[str] , ): super().__init__(**SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = image_size lowerCamelCase__ = patch_size lowerCamelCase__ = num_channels lowerCamelCase__ = embed_dim lowerCamelCase__ = depths lowerCamelCase__ = len(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = num_heads lowerCamelCase__ = window_size lowerCamelCase__ = mlp_ratio lowerCamelCase__ = qkv_bias lowerCamelCase__ = hidden_dropout_prob lowerCamelCase__ = attention_probs_dropout_prob lowerCamelCase__ = drop_path_rate lowerCamelCase__ = hidden_act lowerCamelCase__ = use_absolute_embeddings lowerCamelCase__ = layer_norm_eps lowerCamelCase__ = initializer_range lowerCamelCase__ = encoder_stride # we set the hidden_size attribute in order to make Swinv2 work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model lowerCamelCase__ = int(embed_dim * 2 ** (len(SCREAMING_SNAKE_CASE__ ) - 1) ) lowerCamelCase__ = (0, 0, 0, 0)
659
1
"""simple docstring""" import os from collections import deque import torch from torch.utils.data import Dataset class _a ( SCREAMING_SNAKE_CASE_ ): def __init__( self : Any , SCREAMING_SNAKE_CASE__ : Union[str, Any]="" , SCREAMING_SNAKE_CASE__ : Dict="train" ): assert os.path.isdir(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = [] lowerCamelCase__ = os.listdir(SCREAMING_SNAKE_CASE__ ) for story_filename in story_filenames_list: if "summary" in story_filename: continue lowerCamelCase__ = os.path.join(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) if not os.path.isfile(SCREAMING_SNAKE_CASE__ ): continue self.documents.append(SCREAMING_SNAKE_CASE__ ) def __len__( self : Union[str, Any] ): return len(self.documents ) def __getitem__( self : Tuple , SCREAMING_SNAKE_CASE__ : Any ): lowerCamelCase__ = self.documents[idx] lowerCamelCase__ = document_path.split('/' )[-1] with open(SCREAMING_SNAKE_CASE__ , encoding='utf-8' ) as source: lowerCamelCase__ = source.read() lowerCamelCase__ , lowerCamelCase__ = process_story(SCREAMING_SNAKE_CASE__ ) return document_name, story_lines, summary_lines def snake_case ( _a: List[Any] )-> int: '''simple docstring''' lowerCamelCase__ = list(filter(lambda _a : len(_a ) != 0 , [line.strip() for line in raw_story.split('\n' )] ) ) # for some unknown reason some lines miss a period, add it lowerCamelCase__ = [_add_missing_period(_a ) for line in nonempty_lines] # gather article lines lowerCamelCase__ = [] lowerCamelCase__ = deque(_a ) while True: try: lowerCamelCase__ = lines.popleft() if element.startswith('@highlight' ): break story_lines.append(_a ) except IndexError: # if "@highlight" is absent from the file we pop # all elements until there is None, raising an exception. return story_lines, [] # gather summary lines lowerCamelCase__ = list(filter(lambda _a : not t.startswith('@highlight' ) , _a ) ) return story_lines, summary_lines def snake_case ( _a: Tuple )-> List[Any]: '''simple docstring''' lowerCamelCase__ = ['.', '!', '?', '...', '\'', '`', '"', '\u2019', '\u2019', ')'] if line.startswith('@highlight' ): return line if line[-1] in END_TOKENS: return line return line + "." def snake_case ( _a: Optional[int] , _a: int , _a: Optional[Any] )-> str: '''simple docstring''' if len(_a ) > block_size: return sequence[:block_size] else: sequence.extend([pad_token_id] * (block_size - len(_a )) ) return sequence def snake_case ( _a: str , _a: List[Any] )-> int: '''simple docstring''' lowerCamelCase__ = torch.ones_like(_a ) lowerCamelCase__ = sequence == pad_token_id lowerCamelCase__ = 0 return mask def snake_case ( _a: Any , _a: Tuple , _a: Optional[int] )-> int: '''simple docstring''' lowerCamelCase__ = [tokenizer.encode(_a ) for line in story_lines] lowerCamelCase__ = [token for sentence in story_lines_token_ids for token in sentence] lowerCamelCase__ = [tokenizer.encode(_a ) for line in summary_lines] lowerCamelCase__ = [token for sentence in summary_lines_token_ids for token in sentence] return story_token_ids, summary_token_ids def snake_case ( _a: int , _a: str )-> List[str]: '''simple docstring''' lowerCamelCase__ = [] for sequence in batch: lowerCamelCase__ = -1 lowerCamelCase__ = [] for s in sequence: if s == separator_token_id: sentence_num += 1 embeddings.append(sentence_num % 2 ) batch_embeddings.append(_a ) return torch.tensor(_a )
659
"""simple docstring""" def snake_case ( _a: int )-> int: '''simple docstring''' lowerCamelCase__ = 1 for i in range(1 , num + 1 ): fact *= i return fact def snake_case ( _a: int )-> int: '''simple docstring''' lowerCamelCase__ = 0 while number > 0: lowerCamelCase__ = number % 10 sum_of_digits += last_digit lowerCamelCase__ = number // 10 # Removing the last_digit from the given number return sum_of_digits def snake_case ( _a: int = 100 )-> int: '''simple docstring''' lowerCamelCase__ = factorial(_a ) lowerCamelCase__ = split_and_add(_a ) return result if __name__ == "__main__": print(solution(int(input("Enter the Number: ").strip())))
659
1
"""simple docstring""" from __future__ import annotations _snake_case = "#" class _a : def __init__( self : Union[str, Any] ): lowerCamelCase__ = {} def _UpperCamelCase ( self : List[str] , SCREAMING_SNAKE_CASE__ : str ): lowerCamelCase__ = self._trie for char in text: if char not in trie: lowerCamelCase__ = {} lowerCamelCase__ = trie[char] lowerCamelCase__ = True def _UpperCamelCase ( self : Optional[Any] , SCREAMING_SNAKE_CASE__ : str ): lowerCamelCase__ = self._trie for char in prefix: if char in trie: lowerCamelCase__ = trie[char] else: return [] return self._elements(SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : List[str] , SCREAMING_SNAKE_CASE__ : dict ): lowerCamelCase__ = [] for c, v in d.items(): lowerCamelCase__ = [' '] if c == END else [(c + s) for s in self._elements(SCREAMING_SNAKE_CASE__ )] result.extend(SCREAMING_SNAKE_CASE__ ) return tuple(SCREAMING_SNAKE_CASE__ ) _snake_case = Trie() _snake_case = ("depart", "detergent", "daring", "dog", "deer", "deal") for word in words: trie.insert_word(word) def snake_case ( _a: str )-> tuple: '''simple docstring''' lowerCamelCase__ = trie.find_word(_a ) return tuple(string + word for word in suffixes ) def snake_case ( )-> None: '''simple docstring''' print(autocomplete_using_trie('de' ) ) if __name__ == "__main__": import doctest doctest.testmod() main()
659
"""simple docstring""" 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 _snake_case = re.compile("[^A-Za-z_0-9]") # parameters used in DuplicationIndex _snake_case = 10 _snake_case = 256 def snake_case ( _a: List[str] )-> Optional[MinHash]: '''simple docstring''' if len(_a ) < MIN_NUM_TOKENS: return None lowerCamelCase__ = MinHash(num_perm=_a ) for token in set(_a ): min_hash.update(token.encode() ) return min_hash def snake_case ( _a: str )-> Set[str]: '''simple docstring''' return {t for t in NON_ALPHA.split(_a ) if len(t.strip() ) > 0} class _a : def __init__( self : List[Any] , *, SCREAMING_SNAKE_CASE__ : float = 0.85 , ): lowerCamelCase__ = duplication_jaccard_threshold lowerCamelCase__ = NUM_PERM lowerCamelCase__ = MinHashLSH(threshold=self._duplication_jaccard_threshold , num_perm=self._num_perm ) lowerCamelCase__ = defaultdict(SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : List[Any] , SCREAMING_SNAKE_CASE__ : Tuple , SCREAMING_SNAKE_CASE__ : MinHash ): lowerCamelCase__ = self._index.query(SCREAMING_SNAKE_CASE__ ) if code_key in self._index.keys: print(F'Duplicate key {code_key}' ) return self._index.insert(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) if len(SCREAMING_SNAKE_CASE__ ) > 0: for base_duplicate in close_duplicates: if base_duplicate in self._duplicate_clusters: self._duplicate_clusters[base_duplicate].add(SCREAMING_SNAKE_CASE__ ) break else: self._duplicate_clusters[close_duplicates[0]].add(SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : str ): lowerCamelCase__ = [] for base, duplicates in self._duplicate_clusters.items(): lowerCamelCase__ = [base] + list(SCREAMING_SNAKE_CASE__ ) # reformat the cluster to be a list of dict lowerCamelCase__ = [{'base_index': el[0], 'repo_name': el[1], 'path': el[2]} for el in cluster] duplicate_clusters.append(SCREAMING_SNAKE_CASE__ ) return duplicate_clusters def _UpperCamelCase ( self : List[str] , SCREAMING_SNAKE_CASE__ : Union[str, Any] ): lowerCamelCase__ = self.get_duplicate_clusters() with open(SCREAMING_SNAKE_CASE__ , 'w' ) as f: json.dump(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) def snake_case ( _a: Union[str, Any] )-> Optional[int]: '''simple docstring''' lowerCamelCase__ , lowerCamelCase__ = element lowerCamelCase__ = 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 snake_case ( _a: Type[Dataset] )-> Tuple: '''simple docstring''' with mp.Pool() as pool: for data in pool.imap_unordered( _compute_min_hash , ThreadedIterator(_a , max_queue_size=10000 ) , chunksize=100 , ): if data is not None: yield data def snake_case ( _a: Type[Dataset] , _a: float )-> Optional[int]: '''simple docstring''' lowerCamelCase__ = 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 snake_case ( _a: str , _a: str )-> float: '''simple docstring''' lowerCamelCase__ = get_tokens(_a ) lowerCamelCase__ = get_tokens(_a ) return len(tokensa & tokensa ) / len(tokensa | tokensa ) _snake_case = None def snake_case ( _a: Dict , _a: Union[str, Any] )-> List[str]: '''simple docstring''' lowerCamelCase__ = [] for elementa in cluster: lowerCamelCase__ = _shared_dataset[elementa['base_index']]['content'] for elementa in extremes: lowerCamelCase__ = _shared_dataset[elementa['base_index']]['content'] if jaccard_similarity(_a , _a ) >= jaccard_threshold: elementa["copies"] += 1 break else: lowerCamelCase__ = 1 extremes.append(_a ) return extremes def snake_case ( _a: Any , _a: Tuple , _a: Dict )-> Union[str, Any]: '''simple docstring''' global _shared_dataset lowerCamelCase__ = dataset lowerCamelCase__ = [] lowerCamelCase__ = 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 snake_case ( _a: Type[Dataset] , _a: float = 0.85 )-> Tuple[Type[Dataset], List[List[Dict]]]: '''simple docstring''' lowerCamelCase__ = make_duplicate_clusters(_a , _a ) lowerCamelCase__ = {x['base_index'] for cluster in duplicate_clusters for x in cluster} lowerCamelCase__ = {} lowerCamelCase__ = find_extremes(_a , _a , _a ) for extremes in extremes_clusters: for element in extremes: lowerCamelCase__ = element lowerCamelCase__ = duplicate_indices - set(extreme_dict.keys() ) lowerCamelCase__ = 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: lowerCamelCase__ = element['base_index'] in extreme_dict if element["is_extreme"]: lowerCamelCase__ = 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
659
1
"""simple docstring""" 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 _snake_case = logging.get_logger(__name__) # pylint: disable=invalid-name _snake_case = "\n Examples:\n ```py\n >>> from PIL import Image\n >>> import torch\n >>> from diffusers import DiffusionPipeline\n >>> from diffusers.utils import export_to_gif, load_image\n\n >>> device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n\n >>> repo = \"openai/shap-e-img2img\"\n >>> pipe = DiffusionPipeline.from_pretrained(repo, torch_dtype=torch.float16)\n >>> pipe = pipe.to(device)\n\n >>> guidance_scale = 3.0\n >>> image_url = \"https://hf.co/datasets/diffusers/docs-images/resolve/main/shap-e/corgi.png\"\n >>> image = load_image(image_url).convert(\"RGB\")\n\n >>> images = pipe(\n ... image,\n ... guidance_scale=guidance_scale,\n ... num_inference_steps=64,\n ... frame_size=256,\n ... ).images\n\n >>> gif_path = export_to_gif(images[0], \"corgi_3d.gif\")\n ```\n" @dataclass class _a ( SCREAMING_SNAKE_CASE_ ): a_ : Union[PIL.Image.Image, np.ndarray] class _a ( SCREAMING_SNAKE_CASE_ ): def __init__( self : Tuple , SCREAMING_SNAKE_CASE__ : PriorTransformer , SCREAMING_SNAKE_CASE__ : CLIPVisionModel , SCREAMING_SNAKE_CASE__ : CLIPImageProcessor , SCREAMING_SNAKE_CASE__ : HeunDiscreteScheduler , SCREAMING_SNAKE_CASE__ : ShapERenderer , ): super().__init__() self.register_modules( prior=SCREAMING_SNAKE_CASE__ , image_encoder=SCREAMING_SNAKE_CASE__ , image_processor=SCREAMING_SNAKE_CASE__ , scheduler=SCREAMING_SNAKE_CASE__ , renderer=SCREAMING_SNAKE_CASE__ , ) def _UpperCamelCase ( self : str , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : Optional[int] , SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : List[str] ): if latents is None: lowerCamelCase__ = randn_tensor(SCREAMING_SNAKE_CASE__ , generator=SCREAMING_SNAKE_CASE__ , device=SCREAMING_SNAKE_CASE__ , dtype=SCREAMING_SNAKE_CASE__ ) else: if latents.shape != shape: raise ValueError(F'Unexpected latents shape, got {latents.shape}, expected {shape}' ) lowerCamelCase__ = latents.to(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = latents * scheduler.init_noise_sigma return latents def _UpperCamelCase ( self : List[Any] , SCREAMING_SNAKE_CASE__ : List[str]=0 ): if is_accelerate_available(): from accelerate import cpu_offload else: raise ImportError('Please install accelerate via `pip install accelerate`' ) lowerCamelCase__ = torch.device(F'cuda:{gpu_id}' ) lowerCamelCase__ = [self.image_encoder, self.prior] for cpu_offloaded_model in models: if cpu_offloaded_model is not None: cpu_offload(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) @property def _UpperCamelCase ( self : Dict ): 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(SCREAMING_SNAKE_CASE__ , '_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 _UpperCamelCase ( self : Optional[int] , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : Dict , SCREAMING_SNAKE_CASE__ : Optional[Any] , SCREAMING_SNAKE_CASE__ : int , ): if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and isinstance(image[0] , torch.Tensor ): lowerCamelCase__ = torch.cat(SCREAMING_SNAKE_CASE__ , axis=0 ) if image[0].ndim == 4 else torch.stack(SCREAMING_SNAKE_CASE__ , axis=0 ) if not isinstance(SCREAMING_SNAKE_CASE__ , torch.Tensor ): lowerCamelCase__ = self.image_processor(SCREAMING_SNAKE_CASE__ , return_tensors='pt' ).pixel_values[0].unsqueeze(0 ) lowerCamelCase__ = image.to(dtype=self.image_encoder.dtype , device=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = self.image_encoder(SCREAMING_SNAKE_CASE__ )['last_hidden_state'] lowerCamelCase__ = image_embeds[:, 1:, :].contiguous() # batch_size, dim, 256 lowerCamelCase__ = image_embeds.repeat_interleave(SCREAMING_SNAKE_CASE__ , dim=0 ) if do_classifier_free_guidance: lowerCamelCase__ = torch.zeros_like(SCREAMING_SNAKE_CASE__ ) # 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 lowerCamelCase__ = torch.cat([negative_image_embeds, image_embeds] ) return image_embeds @torch.no_grad() @replace_example_docstring(SCREAMING_SNAKE_CASE__ ) def __call__( self : List[Any] , SCREAMING_SNAKE_CASE__ : Union[PIL.Image.Image, List[PIL.Image.Image]] , SCREAMING_SNAKE_CASE__ : int = 1 , SCREAMING_SNAKE_CASE__ : int = 25 , SCREAMING_SNAKE_CASE__ : Optional[Union[torch.Generator, List[torch.Generator]]] = None , SCREAMING_SNAKE_CASE__ : Optional[torch.FloatTensor] = None , SCREAMING_SNAKE_CASE__ : float = 4.0 , SCREAMING_SNAKE_CASE__ : int = 64 , SCREAMING_SNAKE_CASE__ : Optional[str] = "pil" , SCREAMING_SNAKE_CASE__ : bool = True , ): if isinstance(SCREAMING_SNAKE_CASE__ , PIL.Image.Image ): lowerCamelCase__ = 1 elif isinstance(SCREAMING_SNAKE_CASE__ , torch.Tensor ): lowerCamelCase__ = image.shape[0] elif isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and isinstance(image[0] , (torch.Tensor, PIL.Image.Image) ): lowerCamelCase__ = len(SCREAMING_SNAKE_CASE__ ) 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(SCREAMING_SNAKE_CASE__ )}' ) lowerCamelCase__ = self._execution_device lowerCamelCase__ = batch_size * num_images_per_prompt lowerCamelCase__ = guidance_scale > 1.0 lowerCamelCase__ = self._encode_image(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) # prior self.scheduler.set_timesteps(SCREAMING_SNAKE_CASE__ , device=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = self.scheduler.timesteps lowerCamelCase__ = self.prior.config.num_embeddings lowerCamelCase__ = self.prior.config.embedding_dim lowerCamelCase__ = self.prepare_latents( (batch_size, num_embeddings * embedding_dim) , image_embeds.dtype , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , 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 lowerCamelCase__ = latents.reshape(latents.shape[0] , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) for i, t in enumerate(self.progress_bar(SCREAMING_SNAKE_CASE__ ) ): # expand the latents if we are doing classifier free guidance lowerCamelCase__ = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents lowerCamelCase__ = self.scheduler.scale_model_input(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = self.prior( SCREAMING_SNAKE_CASE__ , timestep=SCREAMING_SNAKE_CASE__ , proj_embedding=SCREAMING_SNAKE_CASE__ , ).predicted_image_embedding # remove the variance lowerCamelCase__ , lowerCamelCase__ = noise_pred.split( scaled_model_input.shape[2] , dim=2 ) # batch_size, num_embeddings, embedding_dim if do_classifier_free_guidance is not None: lowerCamelCase__ , lowerCamelCase__ = noise_pred.chunk(2 ) lowerCamelCase__ = noise_pred_uncond + guidance_scale * (noise_pred - noise_pred_uncond) lowerCamelCase__ = self.scheduler.step( SCREAMING_SNAKE_CASE__ , timestep=SCREAMING_SNAKE_CASE__ , sample=SCREAMING_SNAKE_CASE__ , ).prev_sample if output_type == "latent": return ShapEPipelineOutput(images=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = [] for i, latent in enumerate(SCREAMING_SNAKE_CASE__ ): print() lowerCamelCase__ = self.renderer.decode( latent[None, :] , SCREAMING_SNAKE_CASE__ , size=SCREAMING_SNAKE_CASE__ , ray_batch_size=40_96 , n_coarse_samples=64 , n_fine_samples=1_28 , ) images.append(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = torch.stack(SCREAMING_SNAKE_CASE__ ) if output_type not in ["np", "pil"]: raise ValueError(F'Only the output types `pil` and `np` are supported not output_type={output_type}' ) lowerCamelCase__ = images.cpu().numpy() if output_type == "pil": lowerCamelCase__ = [self.numpy_to_pil(SCREAMING_SNAKE_CASE__ ) 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=SCREAMING_SNAKE_CASE__ )
659
"""simple docstring""" import collections import json import math import os import re import time from fnmatch import fnmatch from typing import Dict import requests from slack_sdk import WebClient _snake_case = WebClient(token=os.environ["CI_SLACK_BOT_TOKEN"]) def snake_case ( _a: Any )-> Optional[Any]: '''simple docstring''' lowerCamelCase__ = test_results.split(' ' ) lowerCamelCase__ = 0 lowerCamelCase__ = 0 # When the output is short enough, the output is surrounded by = signs: "== OUTPUT ==" # When it is too long, those signs are not present. lowerCamelCase__ = expressions[-2] if '=' in expressions[-1] else expressions[-1] for i, expression in enumerate(_a ): if "failed" in expression: failed += int(expressions[i - 1] ) if "passed" in expression: success += int(expressions[i - 1] ) return failed, success, time_spent def snake_case ( _a: Optional[int] )-> Optional[int]: '''simple docstring''' lowerCamelCase__ = {} lowerCamelCase__ = None lowerCamelCase__ = False for line in failures_short_lines.split('\n' ): if re.search(R'_ \[doctest\]' , _a ): lowerCamelCase__ = True lowerCamelCase__ = line.split(' ' )[2] elif in_error and not line.split(' ' )[0].isdigit(): lowerCamelCase__ = line lowerCamelCase__ = False return failures class _a : def __init__( self : Optional[Any] , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : Dict ): lowerCamelCase__ = title lowerCamelCase__ = doc_test_results['time_spent'].split(',' )[0] lowerCamelCase__ = doc_test_results['success'] lowerCamelCase__ = doc_test_results['failures'] lowerCamelCase__ = self.n_success + self.n_failures # Failures and success of the modeling tests lowerCamelCase__ = doc_test_results @property def _UpperCamelCase ( self : List[str] ): lowerCamelCase__ = [self._time_spent] lowerCamelCase__ = 0 for time in time_spent: lowerCamelCase__ = time.split(':' ) # Time can be formatted as xx:xx:xx, as .xx, or as x.xx if the time spent was less than a minute. if len(SCREAMING_SNAKE_CASE__ ) == 1: lowerCamelCase__ = [0, 0, time_parts[0]] lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ = int(time_parts[0] ), int(time_parts[1] ), float(time_parts[2] ) total_secs += hours * 36_00 + minutes * 60 + seconds lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ = total_secs // 36_00, (total_secs % 36_00) // 60, total_secs % 60 return F'{int(SCREAMING_SNAKE_CASE__ )}h{int(SCREAMING_SNAKE_CASE__ )}m{int(SCREAMING_SNAKE_CASE__ )}s' @property def _UpperCamelCase ( self : Dict ): return {"type": "header", "text": {"type": "plain_text", "text": self.title}} @property def _UpperCamelCase ( self : Dict ): return { "type": "section", "text": { "type": "plain_text", "text": F'🌞 There were no failures: all {self.n_tests} tests passed. The suite ran in {self.time}.', "emoji": True, }, "accessory": { "type": "button", "text": {"type": "plain_text", "text": "Check Action results", "emoji": True}, "url": F'https://github.com/huggingface/transformers/actions/runs/{os.environ["GITHUB_RUN_ID"]}', }, } @property def _UpperCamelCase ( self : Any ): return { "type": "section", "text": { "type": "plain_text", "text": ( F'There were {self.n_failures} failures, out of {self.n_tests} tests.\nThe suite ran in' F' {self.time}.' ), "emoji": True, }, "accessory": { "type": "button", "text": {"type": "plain_text", "text": "Check Action results", "emoji": True}, "url": F'https://github.com/huggingface/transformers/actions/runs/{os.environ["GITHUB_RUN_ID"]}', }, } @property def _UpperCamelCase ( self : Union[str, Any] ): lowerCamelCase__ = 40 lowerCamelCase__ = {k: v['failed'] for k, v in doc_test_results.items() if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ )} lowerCamelCase__ = '' for category, failures in category_failures.items(): if len(SCREAMING_SNAKE_CASE__ ) == 0: continue if report != "": report += "\n\n" report += F'*{category} failures*:'.ljust(line_length // 2 ).rjust(line_length // 2 ) + "\n" report += "`" report += "`\n`".join(SCREAMING_SNAKE_CASE__ ) report += "`" return { "type": "section", "text": { "type": "mrkdwn", "text": F'The following examples had failures:\n\n\n{report}\n', }, } @property def _UpperCamelCase ( self : str ): lowerCamelCase__ = [self.header] if self.n_failures > 0: blocks.append(self.failures ) if self.n_failures > 0: blocks.extend([self.category_failures] ) if self.n_failures == 0: blocks.append(self.no_failures ) return json.dumps(SCREAMING_SNAKE_CASE__ ) @staticmethod def _UpperCamelCase ( ): lowerCamelCase__ = [ { 'type': 'section', 'text': { 'type': 'plain_text', 'text': 'There was an issue running the tests.', }, 'accessory': { 'type': 'button', 'text': {'type': 'plain_text', 'text': 'Check Action results', 'emoji': True}, 'url': F'https://github.com/huggingface/transformers/actions/runs/{os.environ["GITHUB_RUN_ID"]}', }, } ] print('Sending the following payload' ) print(json.dumps({'blocks': json.loads(SCREAMING_SNAKE_CASE__ )} ) ) client.chat_postMessage( channel=os.environ['CI_SLACK_CHANNEL_ID_DAILY'] , text='There was an issue running the tests.' , blocks=SCREAMING_SNAKE_CASE__ , ) def _UpperCamelCase ( self : Optional[int] ): print('Sending the following payload' ) print(json.dumps({'blocks': json.loads(self.payload )} ) ) lowerCamelCase__ = F'{self.n_failures} failures out of {self.n_tests} tests,' if self.n_failures else 'All tests passed.' lowerCamelCase__ = client.chat_postMessage( channel=os.environ['CI_SLACK_CHANNEL_ID_DAILY'] , blocks=self.payload , text=SCREAMING_SNAKE_CASE__ , ) def _UpperCamelCase ( self : Optional[Any] , SCREAMING_SNAKE_CASE__ : Dict , SCREAMING_SNAKE_CASE__ : List[Any] , SCREAMING_SNAKE_CASE__ : Tuple , SCREAMING_SNAKE_CASE__ : Tuple ): lowerCamelCase__ = '' for key, value in failures.items(): lowerCamelCase__ = value[:2_00] + ' [Truncated]' if len(SCREAMING_SNAKE_CASE__ ) > 2_50 else value failures_text += F'*{key}*\n_{value}_\n\n' lowerCamelCase__ = job_name lowerCamelCase__ = {'type': 'section', 'text': {'type': 'mrkdwn', 'text': text}} if job_link is not None: lowerCamelCase__ = { 'type': 'button', 'text': {'type': 'plain_text', 'text': 'GitHub Action job', 'emoji': True}, 'url': job_link, } return [ {"type": "header", "text": {"type": "plain_text", "text": title.upper(), "emoji": True}}, content, {"type": "section", "text": {"type": "mrkdwn", "text": failures_text}}, ] def _UpperCamelCase ( self : Optional[int] ): if self.thread_ts is None: raise ValueError('Can only post reply if a post has been made.' ) lowerCamelCase__ = self.doc_test_results.pop('job_link' ) self.doc_test_results.pop('failures' ) self.doc_test_results.pop('success' ) self.doc_test_results.pop('time_spent' ) lowerCamelCase__ = sorted(self.doc_test_results.items() , key=lambda SCREAMING_SNAKE_CASE__ : t[0] ) for job, job_result in sorted_dict: if len(job_result['failures'] ): lowerCamelCase__ = F'*Num failures* :{len(job_result["failed"] )} \n' lowerCamelCase__ = job_result['failures'] lowerCamelCase__ = self.get_reply_blocks(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , text=SCREAMING_SNAKE_CASE__ ) print('Sending the following reply' ) print(json.dumps({'blocks': blocks} ) ) client.chat_postMessage( channel=os.environ['CI_SLACK_CHANNEL_ID_DAILY'] , text=F'Results for {job}' , blocks=SCREAMING_SNAKE_CASE__ , thread_ts=self.thread_ts['ts'] , ) time.sleep(1 ) def snake_case ( )-> Optional[Any]: '''simple docstring''' lowerCamelCase__ = os.environ['GITHUB_RUN_ID'] lowerCamelCase__ = F'https://api.github.com/repos/huggingface/transformers/actions/runs/{run_id}/jobs?per_page=100' lowerCamelCase__ = requests.get(_a ).json() lowerCamelCase__ = {} try: jobs.update({job['name']: job['html_url'] for job in result['jobs']} ) lowerCamelCase__ = math.ceil((result['total_count'] - 100) / 100 ) for i in range(_a ): lowerCamelCase__ = requests.get(url + F'&page={i + 2}' ).json() jobs.update({job['name']: job['html_url'] for job in result['jobs']} ) return jobs except Exception as e: print('Unknown error, could not fetch links.' , _a ) return {} def snake_case ( _a: str )-> Dict: '''simple docstring''' lowerCamelCase__ = {} if os.path.exists(_a ): lowerCamelCase__ = os.listdir(_a ) for file in files: try: with open(os.path.join(_a , _a ) , encoding='utf-8' ) as f: lowerCamelCase__ = f.read() except UnicodeDecodeError as e: raise ValueError(F'Could not open {os.path.join(_a , _a )}.' ) from e return _artifact def snake_case ( )-> Optional[int]: '''simple docstring''' class _a : def __init__( self : Tuple , SCREAMING_SNAKE_CASE__ : str ): lowerCamelCase__ = name lowerCamelCase__ = [] def __str__( self : Dict ): return self.name def _UpperCamelCase ( self : Union[str, Any] , SCREAMING_SNAKE_CASE__ : str ): self.paths.append({'name': self.name, 'path': path} ) lowerCamelCase__ = {} lowerCamelCase__ = filter(os.path.isdir , os.listdir() ) for directory in directories: lowerCamelCase__ = directory if artifact_name not in _available_artifacts: lowerCamelCase__ = Artifact(_a ) _available_artifacts[artifact_name].add_path(_a ) return _available_artifacts if __name__ == "__main__": _snake_case = get_job_links() _snake_case = retrieve_available_artifacts() _snake_case = collections.OrderedDict( [ ("*.py", "API Examples"), ("*.md", "MD Examples"), ] ) # This dict will contain all the information relative to each doc test category: # - failed: list of failed tests # - failures: dict in the format 'test': 'error_message' _snake_case = { v: { "failed": [], "failures": {}, } for v in docs.values() } # Link to the GitHub Action job _snake_case = github_actions_job_links.get("run_doctests") _snake_case = available_artifacts["doc_tests_gpu_test_reports"].paths[0] _snake_case = retrieve_artifact(artifact_path["name"]) if "stats" in artifact: _snake_case , _snake_case , _snake_case = handle_test_results(artifact["stats"]) _snake_case = failed _snake_case = success _snake_case = time_spent[1:-1] + ", " _snake_case = extract_first_line_failure(artifact["failures_short"]) for line in artifact["summary_short"].split("\n"): if re.search("FAILED", line): _snake_case = line.replace("FAILED ", "") _snake_case = line.split()[0].replace("\n", "") if "::" in line: _snake_case , _snake_case = line.split("::") else: _snake_case , _snake_case = line, line for file_regex in docs.keys(): if fnmatch(file_path, file_regex): _snake_case = docs[file_regex] doc_test_results[category]["failed"].append(test) _snake_case = all_failures[test] if test in all_failures else "N/A" _snake_case = failure break _snake_case = Message("🤗 Results of the doc tests.", doc_test_results) message.post() message.post_reply()
659
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available _snake_case = { "configuration_time_series_transformer": [ "TIME_SERIES_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", "TimeSeriesTransformerConfig", ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = [ "TIME_SERIES_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "TimeSeriesTransformerForPrediction", "TimeSeriesTransformerModel", "TimeSeriesTransformerPreTrainedModel", ] if TYPE_CHECKING: from .configuration_time_series_transformer import ( TIME_SERIES_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TimeSeriesTransformerConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_time_series_transformer import ( TIME_SERIES_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TimeSeriesTransformerForPrediction, TimeSeriesTransformerModel, TimeSeriesTransformerPreTrainedModel, ) else: import sys _snake_case = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
659
"""simple docstring""" 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 _snake_case = logging.get_logger(__name__) # pylint: disable=invalid-name _snake_case = "\n Examples:\n ```py\n >>> from PIL import Image\n >>> import torch\n >>> from diffusers import DiffusionPipeline\n >>> from diffusers.utils import export_to_gif, load_image\n\n >>> device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n\n >>> repo = \"openai/shap-e-img2img\"\n >>> pipe = DiffusionPipeline.from_pretrained(repo, torch_dtype=torch.float16)\n >>> pipe = pipe.to(device)\n\n >>> guidance_scale = 3.0\n >>> image_url = \"https://hf.co/datasets/diffusers/docs-images/resolve/main/shap-e/corgi.png\"\n >>> image = load_image(image_url).convert(\"RGB\")\n\n >>> images = pipe(\n ... image,\n ... guidance_scale=guidance_scale,\n ... num_inference_steps=64,\n ... frame_size=256,\n ... ).images\n\n >>> gif_path = export_to_gif(images[0], \"corgi_3d.gif\")\n ```\n" @dataclass class _a ( SCREAMING_SNAKE_CASE_ ): a_ : Union[PIL.Image.Image, np.ndarray] class _a ( SCREAMING_SNAKE_CASE_ ): def __init__( self : Tuple , SCREAMING_SNAKE_CASE__ : PriorTransformer , SCREAMING_SNAKE_CASE__ : CLIPVisionModel , SCREAMING_SNAKE_CASE__ : CLIPImageProcessor , SCREAMING_SNAKE_CASE__ : HeunDiscreteScheduler , SCREAMING_SNAKE_CASE__ : ShapERenderer , ): super().__init__() self.register_modules( prior=SCREAMING_SNAKE_CASE__ , image_encoder=SCREAMING_SNAKE_CASE__ , image_processor=SCREAMING_SNAKE_CASE__ , scheduler=SCREAMING_SNAKE_CASE__ , renderer=SCREAMING_SNAKE_CASE__ , ) def _UpperCamelCase ( self : str , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : Optional[int] , SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : List[str] ): if latents is None: lowerCamelCase__ = randn_tensor(SCREAMING_SNAKE_CASE__ , generator=SCREAMING_SNAKE_CASE__ , device=SCREAMING_SNAKE_CASE__ , dtype=SCREAMING_SNAKE_CASE__ ) else: if latents.shape != shape: raise ValueError(F'Unexpected latents shape, got {latents.shape}, expected {shape}' ) lowerCamelCase__ = latents.to(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = latents * scheduler.init_noise_sigma return latents def _UpperCamelCase ( self : List[Any] , SCREAMING_SNAKE_CASE__ : List[str]=0 ): if is_accelerate_available(): from accelerate import cpu_offload else: raise ImportError('Please install accelerate via `pip install accelerate`' ) lowerCamelCase__ = torch.device(F'cuda:{gpu_id}' ) lowerCamelCase__ = [self.image_encoder, self.prior] for cpu_offloaded_model in models: if cpu_offloaded_model is not None: cpu_offload(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) @property def _UpperCamelCase ( self : Dict ): 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(SCREAMING_SNAKE_CASE__ , '_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 _UpperCamelCase ( self : Optional[int] , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : Dict , SCREAMING_SNAKE_CASE__ : Optional[Any] , SCREAMING_SNAKE_CASE__ : int , ): if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and isinstance(image[0] , torch.Tensor ): lowerCamelCase__ = torch.cat(SCREAMING_SNAKE_CASE__ , axis=0 ) if image[0].ndim == 4 else torch.stack(SCREAMING_SNAKE_CASE__ , axis=0 ) if not isinstance(SCREAMING_SNAKE_CASE__ , torch.Tensor ): lowerCamelCase__ = self.image_processor(SCREAMING_SNAKE_CASE__ , return_tensors='pt' ).pixel_values[0].unsqueeze(0 ) lowerCamelCase__ = image.to(dtype=self.image_encoder.dtype , device=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = self.image_encoder(SCREAMING_SNAKE_CASE__ )['last_hidden_state'] lowerCamelCase__ = image_embeds[:, 1:, :].contiguous() # batch_size, dim, 256 lowerCamelCase__ = image_embeds.repeat_interleave(SCREAMING_SNAKE_CASE__ , dim=0 ) if do_classifier_free_guidance: lowerCamelCase__ = torch.zeros_like(SCREAMING_SNAKE_CASE__ ) # 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 lowerCamelCase__ = torch.cat([negative_image_embeds, image_embeds] ) return image_embeds @torch.no_grad() @replace_example_docstring(SCREAMING_SNAKE_CASE__ ) def __call__( self : List[Any] , SCREAMING_SNAKE_CASE__ : Union[PIL.Image.Image, List[PIL.Image.Image]] , SCREAMING_SNAKE_CASE__ : int = 1 , SCREAMING_SNAKE_CASE__ : int = 25 , SCREAMING_SNAKE_CASE__ : Optional[Union[torch.Generator, List[torch.Generator]]] = None , SCREAMING_SNAKE_CASE__ : Optional[torch.FloatTensor] = None , SCREAMING_SNAKE_CASE__ : float = 4.0 , SCREAMING_SNAKE_CASE__ : int = 64 , SCREAMING_SNAKE_CASE__ : Optional[str] = "pil" , SCREAMING_SNAKE_CASE__ : bool = True , ): if isinstance(SCREAMING_SNAKE_CASE__ , PIL.Image.Image ): lowerCamelCase__ = 1 elif isinstance(SCREAMING_SNAKE_CASE__ , torch.Tensor ): lowerCamelCase__ = image.shape[0] elif isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and isinstance(image[0] , (torch.Tensor, PIL.Image.Image) ): lowerCamelCase__ = len(SCREAMING_SNAKE_CASE__ ) 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(SCREAMING_SNAKE_CASE__ )}' ) lowerCamelCase__ = self._execution_device lowerCamelCase__ = batch_size * num_images_per_prompt lowerCamelCase__ = guidance_scale > 1.0 lowerCamelCase__ = self._encode_image(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) # prior self.scheduler.set_timesteps(SCREAMING_SNAKE_CASE__ , device=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = self.scheduler.timesteps lowerCamelCase__ = self.prior.config.num_embeddings lowerCamelCase__ = self.prior.config.embedding_dim lowerCamelCase__ = self.prepare_latents( (batch_size, num_embeddings * embedding_dim) , image_embeds.dtype , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , 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 lowerCamelCase__ = latents.reshape(latents.shape[0] , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) for i, t in enumerate(self.progress_bar(SCREAMING_SNAKE_CASE__ ) ): # expand the latents if we are doing classifier free guidance lowerCamelCase__ = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents lowerCamelCase__ = self.scheduler.scale_model_input(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = self.prior( SCREAMING_SNAKE_CASE__ , timestep=SCREAMING_SNAKE_CASE__ , proj_embedding=SCREAMING_SNAKE_CASE__ , ).predicted_image_embedding # remove the variance lowerCamelCase__ , lowerCamelCase__ = noise_pred.split( scaled_model_input.shape[2] , dim=2 ) # batch_size, num_embeddings, embedding_dim if do_classifier_free_guidance is not None: lowerCamelCase__ , lowerCamelCase__ = noise_pred.chunk(2 ) lowerCamelCase__ = noise_pred_uncond + guidance_scale * (noise_pred - noise_pred_uncond) lowerCamelCase__ = self.scheduler.step( SCREAMING_SNAKE_CASE__ , timestep=SCREAMING_SNAKE_CASE__ , sample=SCREAMING_SNAKE_CASE__ , ).prev_sample if output_type == "latent": return ShapEPipelineOutput(images=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = [] for i, latent in enumerate(SCREAMING_SNAKE_CASE__ ): print() lowerCamelCase__ = self.renderer.decode( latent[None, :] , SCREAMING_SNAKE_CASE__ , size=SCREAMING_SNAKE_CASE__ , ray_batch_size=40_96 , n_coarse_samples=64 , n_fine_samples=1_28 , ) images.append(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = torch.stack(SCREAMING_SNAKE_CASE__ ) if output_type not in ["np", "pil"]: raise ValueError(F'Only the output types `pil` and `np` are supported not output_type={output_type}' ) lowerCamelCase__ = images.cpu().numpy() if output_type == "pil": lowerCamelCase__ = [self.numpy_to_pil(SCREAMING_SNAKE_CASE__ ) 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=SCREAMING_SNAKE_CASE__ )
659
1
"""simple docstring""" def snake_case ( _a: int )-> str: '''simple docstring''' lowerCamelCase__ = int(_a ) if decimal in (0, 1): # Exit cases for the recursion return str(_a ) lowerCamelCase__ , lowerCamelCase__ = divmod(_a , 2 ) return binary_recursive(_a ) + str(_a ) def snake_case ( _a: str )-> str: '''simple docstring''' lowerCamelCase__ = str(_a ).strip() if not number: raise ValueError('No input value was provided' ) lowerCamelCase__ = '-' if number.startswith('-' ) else '' lowerCamelCase__ = number.lstrip('-' ) if not number.isnumeric(): raise ValueError('Input value is not an integer' ) return F'{negative}0b{binary_recursive(int(_a ) )}' if __name__ == "__main__": from doctest import testmod testmod()
659
"""simple docstring""" import os from shutil import copyfile from typing import List, Optional, Tuple from tokenizers import processors from ...tokenization_utils import AddedToken, BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_nllb import NllbTokenizer else: _snake_case = None _snake_case = logging.get_logger(__name__) _snake_case = {"vocab_file": "sentencepiece.bpe.model", "tokenizer_file": "tokenizer.json"} _snake_case = { "vocab_file": { "facebook/nllb-200-distilled-600M": ( "https://huggingface.co/facebook/nllb-200-distilled-600M/resolve/main/sentencepiece.bpe.model" ), }, "tokenizer_file": { "facebook/nllb-200-distilled-600M": ( "https://huggingface.co/facebook/nllb-200-distilled-600M/resolve/main/tokenizer.json" ), }, } _snake_case = { "facebook/nllb-large-en-ro": 1024, "facebook/nllb-200-distilled-600M": 1024, } # fmt: off _snake_case = ["ace_Arab", "ace_Latn", "acm_Arab", "acq_Arab", "aeb_Arab", "afr_Latn", "ajp_Arab", "aka_Latn", "amh_Ethi", "apc_Arab", "arb_Arab", "ars_Arab", "ary_Arab", "arz_Arab", "asm_Beng", "ast_Latn", "awa_Deva", "ayr_Latn", "azb_Arab", "azj_Latn", "bak_Cyrl", "bam_Latn", "ban_Latn", "bel_Cyrl", "bem_Latn", "ben_Beng", "bho_Deva", "bjn_Arab", "bjn_Latn", "bod_Tibt", "bos_Latn", "bug_Latn", "bul_Cyrl", "cat_Latn", "ceb_Latn", "ces_Latn", "cjk_Latn", "ckb_Arab", "crh_Latn", "cym_Latn", "dan_Latn", "deu_Latn", "dik_Latn", "dyu_Latn", "dzo_Tibt", "ell_Grek", "eng_Latn", "epo_Latn", "est_Latn", "eus_Latn", "ewe_Latn", "fao_Latn", "pes_Arab", "fij_Latn", "fin_Latn", "fon_Latn", "fra_Latn", "fur_Latn", "fuv_Latn", "gla_Latn", "gle_Latn", "glg_Latn", "grn_Latn", "guj_Gujr", "hat_Latn", "hau_Latn", "heb_Hebr", "hin_Deva", "hne_Deva", "hrv_Latn", "hun_Latn", "hye_Armn", "ibo_Latn", "ilo_Latn", "ind_Latn", "isl_Latn", "ita_Latn", "jav_Latn", "jpn_Jpan", "kab_Latn", "kac_Latn", "kam_Latn", "kan_Knda", "kas_Arab", "kas_Deva", "kat_Geor", "knc_Arab", "knc_Latn", "kaz_Cyrl", "kbp_Latn", "kea_Latn", "khm_Khmr", "kik_Latn", "kin_Latn", "kir_Cyrl", "kmb_Latn", "kon_Latn", "kor_Hang", "kmr_Latn", "lao_Laoo", "lvs_Latn", "lij_Latn", "lim_Latn", "lin_Latn", "lit_Latn", "lmo_Latn", "ltg_Latn", "ltz_Latn", "lua_Latn", "lug_Latn", "luo_Latn", "lus_Latn", "mag_Deva", "mai_Deva", "mal_Mlym", "mar_Deva", "min_Latn", "mkd_Cyrl", "plt_Latn", "mlt_Latn", "mni_Beng", "khk_Cyrl", "mos_Latn", "mri_Latn", "zsm_Latn", "mya_Mymr", "nld_Latn", "nno_Latn", "nob_Latn", "npi_Deva", "nso_Latn", "nus_Latn", "nya_Latn", "oci_Latn", "gaz_Latn", "ory_Orya", "pag_Latn", "pan_Guru", "pap_Latn", "pol_Latn", "por_Latn", "prs_Arab", "pbt_Arab", "quy_Latn", "ron_Latn", "run_Latn", "rus_Cyrl", "sag_Latn", "san_Deva", "sat_Beng", "scn_Latn", "shn_Mymr", "sin_Sinh", "slk_Latn", "slv_Latn", "smo_Latn", "sna_Latn", "snd_Arab", "som_Latn", "sot_Latn", "spa_Latn", "als_Latn", "srd_Latn", "srp_Cyrl", "ssw_Latn", "sun_Latn", "swe_Latn", "swh_Latn", "szl_Latn", "tam_Taml", "tat_Cyrl", "tel_Telu", "tgk_Cyrl", "tgl_Latn", "tha_Thai", "tir_Ethi", "taq_Latn", "taq_Tfng", "tpi_Latn", "tsn_Latn", "tso_Latn", "tuk_Latn", "tum_Latn", "tur_Latn", "twi_Latn", "tzm_Tfng", "uig_Arab", "ukr_Cyrl", "umb_Latn", "urd_Arab", "uzn_Latn", "vec_Latn", "vie_Latn", "war_Latn", "wol_Latn", "xho_Latn", "ydd_Hebr", "yor_Latn", "yue_Hant", "zho_Hans", "zho_Hant", "zul_Latn"] class _a ( SCREAMING_SNAKE_CASE_ ): a_ : Any = VOCAB_FILES_NAMES a_ : Optional[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES a_ : Optional[Any] = PRETRAINED_VOCAB_FILES_MAP a_ : List[str] = ['input_ids', 'attention_mask'] a_ : Union[str, Any] = NllbTokenizer a_ : List[int] = [] a_ : List[int] = [] def __init__( self : int , SCREAMING_SNAKE_CASE__ : List[Any]=None , SCREAMING_SNAKE_CASE__ : Optional[int]=None , SCREAMING_SNAKE_CASE__ : List[Any]="<s>" , SCREAMING_SNAKE_CASE__ : List[str]="</s>" , SCREAMING_SNAKE_CASE__ : Any="</s>" , SCREAMING_SNAKE_CASE__ : List[str]="<s>" , SCREAMING_SNAKE_CASE__ : Tuple="<unk>" , SCREAMING_SNAKE_CASE__ : Optional[int]="<pad>" , SCREAMING_SNAKE_CASE__ : Any="<mask>" , SCREAMING_SNAKE_CASE__ : Union[str, Any]=None , SCREAMING_SNAKE_CASE__ : int=None , SCREAMING_SNAKE_CASE__ : Dict=None , SCREAMING_SNAKE_CASE__ : Tuple=False , **SCREAMING_SNAKE_CASE__ : str , ): # Mask token behave like a normal word, i.e. include the space before it lowerCamelCase__ = AddedToken(SCREAMING_SNAKE_CASE__ , lstrip=SCREAMING_SNAKE_CASE__ , rstrip=SCREAMING_SNAKE_CASE__ ) if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) else mask_token lowerCamelCase__ = legacy_behaviour super().__init__( vocab_file=SCREAMING_SNAKE_CASE__ , tokenizer_file=SCREAMING_SNAKE_CASE__ , bos_token=SCREAMING_SNAKE_CASE__ , eos_token=SCREAMING_SNAKE_CASE__ , sep_token=SCREAMING_SNAKE_CASE__ , cls_token=SCREAMING_SNAKE_CASE__ , unk_token=SCREAMING_SNAKE_CASE__ , pad_token=SCREAMING_SNAKE_CASE__ , mask_token=SCREAMING_SNAKE_CASE__ , src_lang=SCREAMING_SNAKE_CASE__ , tgt_lang=SCREAMING_SNAKE_CASE__ , additional_special_tokens=SCREAMING_SNAKE_CASE__ , legacy_behaviour=SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ , ) lowerCamelCase__ = vocab_file lowerCamelCase__ = False if not self.vocab_file else True lowerCamelCase__ = FAIRSEQ_LANGUAGE_CODES.copy() if additional_special_tokens is not None: # Only add those special tokens if they are not already there. _additional_special_tokens.extend( [t for t in additional_special_tokens if t not in _additional_special_tokens] ) self.add_special_tokens({'additional_special_tokens': _additional_special_tokens} ) lowerCamelCase__ = { lang_code: self.convert_tokens_to_ids(SCREAMING_SNAKE_CASE__ ) for lang_code in FAIRSEQ_LANGUAGE_CODES } lowerCamelCase__ = src_lang if src_lang is not None else 'eng_Latn' lowerCamelCase__ = self.convert_tokens_to_ids(self._src_lang ) lowerCamelCase__ = tgt_lang self.set_src_lang_special_tokens(self._src_lang ) @property def _UpperCamelCase ( self : str ): return self._src_lang @src_lang.setter def _UpperCamelCase ( self : int , SCREAMING_SNAKE_CASE__ : str ): lowerCamelCase__ = new_src_lang self.set_src_lang_special_tokens(self._src_lang ) def _UpperCamelCase ( self : Tuple , SCREAMING_SNAKE_CASE__ : List[int] , SCREAMING_SNAKE_CASE__ : Optional[List[int]] = None ): if token_ids_a is None: return self.prefix_tokens + token_ids_a + self.suffix_tokens # We don't expect to process pairs, but leave the pair logic for API consistency return self.prefix_tokens + token_ids_a + token_ids_a + self.suffix_tokens def _UpperCamelCase ( self : Optional[Any] , SCREAMING_SNAKE_CASE__ : List[int] , SCREAMING_SNAKE_CASE__ : Optional[List[int]] = None ): lowerCamelCase__ = [self.sep_token_id] lowerCamelCase__ = [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 _UpperCamelCase ( self : str , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : Optional[str] , SCREAMING_SNAKE_CASE__ : Optional[str] , **SCREAMING_SNAKE_CASE__ : Optional[int] ): if src_lang is None or tgt_lang is None: raise ValueError('Translation requires a `src_lang` and a `tgt_lang` for this model' ) lowerCamelCase__ = src_lang lowerCamelCase__ = self(SCREAMING_SNAKE_CASE__ , add_special_tokens=SCREAMING_SNAKE_CASE__ , return_tensors=SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = self.convert_tokens_to_ids(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = tgt_lang_id return inputs def _UpperCamelCase ( self : str , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : str = "eng_Latn" , SCREAMING_SNAKE_CASE__ : Optional[List[str]] = None , SCREAMING_SNAKE_CASE__ : str = "fra_Latn" , **SCREAMING_SNAKE_CASE__ : Dict , ): lowerCamelCase__ = src_lang lowerCamelCase__ = tgt_lang return super().prepare_seqaseq_batch(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : List[str] ): return self.set_src_lang_special_tokens(self.src_lang ) def _UpperCamelCase ( self : List[Any] ): return self.set_tgt_lang_special_tokens(self.tgt_lang ) def _UpperCamelCase ( self : str , SCREAMING_SNAKE_CASE__ : Union[str, Any] ): lowerCamelCase__ = self.convert_tokens_to_ids(SCREAMING_SNAKE_CASE__ ) if self.legacy_behaviour: lowerCamelCase__ = [] lowerCamelCase__ = [self.eos_token_id, self.cur_lang_code] else: lowerCamelCase__ = [self.cur_lang_code] lowerCamelCase__ = [self.eos_token_id] lowerCamelCase__ = self.convert_ids_to_tokens(self.prefix_tokens ) lowerCamelCase__ = self.convert_ids_to_tokens(self.suffix_tokens ) lowerCamelCase__ = processors.TemplateProcessing( single=prefix_tokens_str + ['$A'] + suffix_tokens_str , pair=prefix_tokens_str + ['$A', '$B'] + suffix_tokens_str , special_tokens=list(zip(prefix_tokens_str + suffix_tokens_str , self.prefix_tokens + self.suffix_tokens ) ) , ) def _UpperCamelCase ( self : Union[str, Any] , SCREAMING_SNAKE_CASE__ : str ): lowerCamelCase__ = self.convert_tokens_to_ids(SCREAMING_SNAKE_CASE__ ) if self.legacy_behaviour: lowerCamelCase__ = [] lowerCamelCase__ = [self.eos_token_id, self.cur_lang_code] else: lowerCamelCase__ = [self.cur_lang_code] lowerCamelCase__ = [self.eos_token_id] lowerCamelCase__ = self.convert_ids_to_tokens(self.prefix_tokens ) lowerCamelCase__ = self.convert_ids_to_tokens(self.suffix_tokens ) lowerCamelCase__ = processors.TemplateProcessing( single=prefix_tokens_str + ['$A'] + suffix_tokens_str , pair=prefix_tokens_str + ['$A', '$B'] + suffix_tokens_str , special_tokens=list(zip(prefix_tokens_str + suffix_tokens_str , self.prefix_tokens + self.suffix_tokens ) ) , ) def _UpperCamelCase ( self : Dict , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : Optional[str] = None ): if not self.can_save_slow_tokenizer: raise ValueError( 'Your fast tokenizer does not have the necessary information to save the vocabulary for a slow ' 'tokenizer.' ) if not os.path.isdir(SCREAMING_SNAKE_CASE__ ): logger.error(F'Vocabulary path ({save_directory}) should be a directory.' ) return lowerCamelCase__ = os.path.join( SCREAMING_SNAKE_CASE__ , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(SCREAMING_SNAKE_CASE__ ): copyfile(self.vocab_file , SCREAMING_SNAKE_CASE__ ) return (out_vocab_file,)
659
1
"""simple docstring""" import unittest from transformers import AutoTokenizer, NystromformerConfig, is_torch_available from transformers.testing_utils import require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( NystromformerForMaskedLM, NystromformerForMultipleChoice, NystromformerForQuestionAnswering, NystromformerForSequenceClassification, NystromformerForTokenClassification, NystromformerModel, ) from transformers.models.nystromformer.modeling_nystromformer import NYSTROMFORMER_PRETRAINED_MODEL_ARCHIVE_LIST class _a : def __init__( self : Dict , SCREAMING_SNAKE_CASE__ : Union[str, Any] , SCREAMING_SNAKE_CASE__ : Tuple=13 , SCREAMING_SNAKE_CASE__ : List[str]=7 , SCREAMING_SNAKE_CASE__ : Any=True , SCREAMING_SNAKE_CASE__ : Tuple=True , SCREAMING_SNAKE_CASE__ : Any=True , SCREAMING_SNAKE_CASE__ : Union[str, Any]=True , SCREAMING_SNAKE_CASE__ : str=99 , SCREAMING_SNAKE_CASE__ : Optional[Any]=32 , SCREAMING_SNAKE_CASE__ : List[str]=5 , SCREAMING_SNAKE_CASE__ : int=4 , SCREAMING_SNAKE_CASE__ : Any=37 , SCREAMING_SNAKE_CASE__ : str="gelu" , SCREAMING_SNAKE_CASE__ : Dict=0.1 , SCREAMING_SNAKE_CASE__ : str=0.1 , SCREAMING_SNAKE_CASE__ : Optional[Any]=5_12 , SCREAMING_SNAKE_CASE__ : Any=16 , SCREAMING_SNAKE_CASE__ : Dict=2 , SCREAMING_SNAKE_CASE__ : Any=0.02 , SCREAMING_SNAKE_CASE__ : int=3 , SCREAMING_SNAKE_CASE__ : Dict=4 , SCREAMING_SNAKE_CASE__ : List[Any]=None , ): lowerCamelCase__ = parent lowerCamelCase__ = batch_size lowerCamelCase__ = seq_length lowerCamelCase__ = is_training lowerCamelCase__ = use_input_mask lowerCamelCase__ = use_token_type_ids lowerCamelCase__ = use_labels lowerCamelCase__ = vocab_size lowerCamelCase__ = hidden_size lowerCamelCase__ = num_hidden_layers lowerCamelCase__ = num_attention_heads lowerCamelCase__ = intermediate_size lowerCamelCase__ = hidden_act lowerCamelCase__ = hidden_dropout_prob lowerCamelCase__ = attention_probs_dropout_prob lowerCamelCase__ = max_position_embeddings lowerCamelCase__ = type_vocab_size lowerCamelCase__ = type_sequence_label_size lowerCamelCase__ = initializer_range lowerCamelCase__ = num_labels lowerCamelCase__ = num_choices lowerCamelCase__ = scope def _UpperCamelCase ( self : Optional[Any] ): lowerCamelCase__ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) lowerCamelCase__ = None if self.use_input_mask: lowerCamelCase__ = random_attention_mask([self.batch_size, self.seq_length] ) lowerCamelCase__ = None if self.use_token_type_ids: lowerCamelCase__ = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) lowerCamelCase__ = None lowerCamelCase__ = None lowerCamelCase__ = None if self.use_labels: lowerCamelCase__ = ids_tensor([self.batch_size] , self.type_sequence_label_size ) lowerCamelCase__ = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) lowerCamelCase__ = ids_tensor([self.batch_size] , self.num_choices ) lowerCamelCase__ = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def _UpperCamelCase ( self : Dict ): return NystromformerConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=SCREAMING_SNAKE_CASE__ , initializer_range=self.initializer_range , ) def _UpperCamelCase ( self : int , SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : Any , SCREAMING_SNAKE_CASE__ : Optional[int] , SCREAMING_SNAKE_CASE__ : Any , SCREAMING_SNAKE_CASE__ : Any , SCREAMING_SNAKE_CASE__ : Tuple , SCREAMING_SNAKE_CASE__ : int ): lowerCamelCase__ = NystromformerModel(config=SCREAMING_SNAKE_CASE__ ) model.to(SCREAMING_SNAKE_CASE__ ) model.eval() lowerCamelCase__ = model(SCREAMING_SNAKE_CASE__ , attention_mask=SCREAMING_SNAKE_CASE__ , token_type_ids=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = model(SCREAMING_SNAKE_CASE__ , token_type_ids=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = model(SCREAMING_SNAKE_CASE__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _UpperCamelCase ( self : str , SCREAMING_SNAKE_CASE__ : Tuple , SCREAMING_SNAKE_CASE__ : List[Any] , SCREAMING_SNAKE_CASE__ : Union[str, Any] , SCREAMING_SNAKE_CASE__ : Optional[int] , SCREAMING_SNAKE_CASE__ : Tuple , SCREAMING_SNAKE_CASE__ : Optional[int] , SCREAMING_SNAKE_CASE__ : Optional[int] ): lowerCamelCase__ = NystromformerForMaskedLM(config=SCREAMING_SNAKE_CASE__ ) model.to(SCREAMING_SNAKE_CASE__ ) model.eval() lowerCamelCase__ = 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 _UpperCamelCase ( self : Any , SCREAMING_SNAKE_CASE__ : Dict , SCREAMING_SNAKE_CASE__ : Optional[int] , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : Dict , SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : Tuple ): lowerCamelCase__ = NystromformerForQuestionAnswering(config=SCREAMING_SNAKE_CASE__ ) model.to(SCREAMING_SNAKE_CASE__ ) model.eval() lowerCamelCase__ = 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 _UpperCamelCase ( self : Optional[int] , SCREAMING_SNAKE_CASE__ : Dict , SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : Optional[Any] , SCREAMING_SNAKE_CASE__ : Dict , SCREAMING_SNAKE_CASE__ : List[Any] , SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : Optional[Any] ): lowerCamelCase__ = self.num_labels lowerCamelCase__ = NystromformerForSequenceClassification(SCREAMING_SNAKE_CASE__ ) model.to(SCREAMING_SNAKE_CASE__ ) model.eval() lowerCamelCase__ = model(SCREAMING_SNAKE_CASE__ , attention_mask=SCREAMING_SNAKE_CASE__ , token_type_ids=SCREAMING_SNAKE_CASE__ , labels=SCREAMING_SNAKE_CASE__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def _UpperCamelCase ( self : int , SCREAMING_SNAKE_CASE__ : Dict , SCREAMING_SNAKE_CASE__ : List[Any] , SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : Any , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : Union[str, Any] , SCREAMING_SNAKE_CASE__ : Tuple ): lowerCamelCase__ = self.num_labels lowerCamelCase__ = NystromformerForTokenClassification(config=SCREAMING_SNAKE_CASE__ ) model.to(SCREAMING_SNAKE_CASE__ ) model.eval() lowerCamelCase__ = 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 _UpperCamelCase ( self : Optional[int] , SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : Tuple , SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : Optional[int] , SCREAMING_SNAKE_CASE__ : List[Any] , SCREAMING_SNAKE_CASE__ : Optional[int] , SCREAMING_SNAKE_CASE__ : Optional[Any] ): lowerCamelCase__ = self.num_choices lowerCamelCase__ = NystromformerForMultipleChoice(config=SCREAMING_SNAKE_CASE__ ) model.to(SCREAMING_SNAKE_CASE__ ) model.eval() lowerCamelCase__ = input_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() lowerCamelCase__ = token_type_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() lowerCamelCase__ = input_mask.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() lowerCamelCase__ = model( SCREAMING_SNAKE_CASE__ , attention_mask=SCREAMING_SNAKE_CASE__ , token_type_ids=SCREAMING_SNAKE_CASE__ , labels=SCREAMING_SNAKE_CASE__ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def _UpperCamelCase ( self : Dict ): lowerCamelCase__ = self.prepare_config_and_inputs() ( ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ) = config_and_inputs lowerCamelCase__ = {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask} return config, inputs_dict @require_torch class _a ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , unittest.TestCase ): a_ : Optional[Any] = ( ( NystromformerModel, NystromformerForMaskedLM, NystromformerForMultipleChoice, NystromformerForQuestionAnswering, NystromformerForSequenceClassification, NystromformerForTokenClassification, ) if is_torch_available() else () ) a_ : str = ( { 'feature-extraction': NystromformerModel, 'fill-mask': NystromformerForMaskedLM, 'question-answering': NystromformerForQuestionAnswering, 'text-classification': NystromformerForSequenceClassification, 'token-classification': NystromformerForTokenClassification, 'zero-shot': NystromformerForSequenceClassification, } if is_torch_available() else {} ) a_ : Any = False a_ : Union[str, Any] = False def _UpperCamelCase ( self : List[Any] ): lowerCamelCase__ = NystromformerModelTester(self ) lowerCamelCase__ = ConfigTester(self , config_class=SCREAMING_SNAKE_CASE__ , hidden_size=37 ) def _UpperCamelCase ( self : Tuple ): self.config_tester.run_common_tests() def _UpperCamelCase ( self : Any ): lowerCamelCase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : Dict ): lowerCamelCase__ = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: lowerCamelCase__ = type self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : Optional[int] ): lowerCamelCase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : Any ): lowerCamelCase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_multiple_choice(*SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : List[str] ): lowerCamelCase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : int ): lowerCamelCase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : int ): lowerCamelCase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*SCREAMING_SNAKE_CASE__ ) @slow def _UpperCamelCase ( self : Union[str, Any] ): for model_name in NYSTROMFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowerCamelCase__ = NystromformerModel.from_pretrained(SCREAMING_SNAKE_CASE__ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE__ ) @require_torch class _a ( unittest.TestCase ): @slow def _UpperCamelCase ( self : List[Any] ): lowerCamelCase__ = NystromformerModel.from_pretrained('uw-madison/nystromformer-512' ) lowerCamelCase__ = torch.tensor([[0, 1, 2, 3, 4, 5]] ) with torch.no_grad(): lowerCamelCase__ = model(SCREAMING_SNAKE_CASE__ )[0] lowerCamelCase__ = torch.Size((1, 6, 7_68) ) self.assertEqual(output.shape , SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = torch.tensor( [[[-0.45_32, -0.09_36, 0.51_37], [-0.26_76, 0.06_28, 0.61_86], [-0.36_29, -0.17_26, 0.47_16]]] ) self.assertTrue(torch.allclose(output[:, :3, :3] , SCREAMING_SNAKE_CASE__ , atol=1e-4 ) ) @slow def _UpperCamelCase ( self : str ): lowerCamelCase__ = 'the [MASK] of Belgium is Brussels' lowerCamelCase__ = AutoTokenizer.from_pretrained('uw-madison/nystromformer-512' ) lowerCamelCase__ = NystromformerForMaskedLM.from_pretrained('uw-madison/nystromformer-512' ) lowerCamelCase__ = tokenizer(SCREAMING_SNAKE_CASE__ , return_tensors='pt' ) with torch.no_grad(): lowerCamelCase__ = model(encoding.input_ids ).logits lowerCamelCase__ = token_logits[:, 2, :].argmax(-1 )[0] self.assertEqual(tokenizer.decode(SCREAMING_SNAKE_CASE__ ) , 'capital' )
659
"""simple docstring""" from __future__ import annotations import unittest import numpy as np from transformers import BlipTextConfig from transformers.testing_utils import require_tf, slow from transformers.utils import is_tf_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask if is_tf_available(): import tensorflow as tf from transformers import TFBlipTextModel from transformers.models.blip.modeling_tf_blip import TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST class _a : def __init__( self : List[str] , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : Union[str, Any]=12 , SCREAMING_SNAKE_CASE__ : Union[str, Any]=7 , SCREAMING_SNAKE_CASE__ : Union[str, Any]=True , SCREAMING_SNAKE_CASE__ : Optional[Any]=True , SCREAMING_SNAKE_CASE__ : str=True , SCREAMING_SNAKE_CASE__ : Any=99 , SCREAMING_SNAKE_CASE__ : Optional[Any]=32 , SCREAMING_SNAKE_CASE__ : str=32 , SCREAMING_SNAKE_CASE__ : Tuple=2 , SCREAMING_SNAKE_CASE__ : str=4 , SCREAMING_SNAKE_CASE__ : Optional[Any]=37 , SCREAMING_SNAKE_CASE__ : Union[str, Any]=0.1 , SCREAMING_SNAKE_CASE__ : Optional[Any]=0.1 , SCREAMING_SNAKE_CASE__ : str=5_12 , SCREAMING_SNAKE_CASE__ : str=0.02 , SCREAMING_SNAKE_CASE__ : Union[str, Any]=0 , SCREAMING_SNAKE_CASE__ : Any=None , ): lowerCamelCase__ = parent lowerCamelCase__ = batch_size lowerCamelCase__ = seq_length lowerCamelCase__ = is_training lowerCamelCase__ = use_input_mask lowerCamelCase__ = use_labels lowerCamelCase__ = vocab_size lowerCamelCase__ = hidden_size lowerCamelCase__ = projection_dim lowerCamelCase__ = num_hidden_layers lowerCamelCase__ = num_attention_heads lowerCamelCase__ = intermediate_size lowerCamelCase__ = dropout lowerCamelCase__ = attention_dropout lowerCamelCase__ = max_position_embeddings lowerCamelCase__ = initializer_range lowerCamelCase__ = scope lowerCamelCase__ = bos_token_id def _UpperCamelCase ( self : int ): lowerCamelCase__ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) lowerCamelCase__ = None if self.use_input_mask: lowerCamelCase__ = random_attention_mask([self.batch_size, self.seq_length] ) if input_mask is not None: lowerCamelCase__ = input_mask.numpy() lowerCamelCase__ , lowerCamelCase__ = input_mask.shape lowerCamelCase__ = np.random.randint(1 , seq_length - 1 , size=(batch_size,) ) for batch_idx, start_index in enumerate(SCREAMING_SNAKE_CASE__ ): lowerCamelCase__ = 1 lowerCamelCase__ = 0 lowerCamelCase__ = self.get_config() return config, input_ids, tf.convert_to_tensor(SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : Any ): return BlipTextConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , projection_dim=self.projection_dim , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , dropout=self.dropout , attention_dropout=self.attention_dropout , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , bos_token_id=self.bos_token_id , ) def _UpperCamelCase ( self : Any , SCREAMING_SNAKE_CASE__ : Any , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : List[str] ): lowerCamelCase__ = TFBlipTextModel(config=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = model(SCREAMING_SNAKE_CASE__ , attention_mask=SCREAMING_SNAKE_CASE__ , training=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = model(SCREAMING_SNAKE_CASE__ , training=SCREAMING_SNAKE_CASE__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size) ) def _UpperCamelCase ( self : Dict ): lowerCamelCase__ = self.prepare_config_and_inputs() lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ = config_and_inputs lowerCamelCase__ = {'input_ids': input_ids, 'attention_mask': input_mask} return config, inputs_dict @require_tf class _a ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): a_ : str = (TFBlipTextModel,) if is_tf_available() else () a_ : List[str] = False a_ : Optional[Any] = False a_ : Union[str, Any] = False def _UpperCamelCase ( self : str ): lowerCamelCase__ = BlipTextModelTester(self ) lowerCamelCase__ = ConfigTester(self , config_class=SCREAMING_SNAKE_CASE__ , hidden_size=37 ) def _UpperCamelCase ( self : Tuple ): self.config_tester.run_common_tests() def _UpperCamelCase ( self : str ): lowerCamelCase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : Tuple ): pass def _UpperCamelCase ( self : Tuple ): pass @unittest.skip(reason='Blip does not use inputs_embeds' ) def _UpperCamelCase ( self : List[str] ): pass @unittest.skip(reason='BlipTextModel has no base class and is not available in MODEL_MAPPING' ) def _UpperCamelCase ( self : Dict ): pass @unittest.skip(reason='BlipTextModel has no base class and is not available in MODEL_MAPPING' ) def _UpperCamelCase ( self : List[Any] ): pass @slow def _UpperCamelCase ( self : str ): for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowerCamelCase__ = TFBlipTextModel.from_pretrained(SCREAMING_SNAKE_CASE__ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : Optional[int] , SCREAMING_SNAKE_CASE__ : List[Any]=True ): super().test_pt_tf_model_equivalence(allow_missing_keys=SCREAMING_SNAKE_CASE__ )
659
1
"""simple docstring""" import unittest import torch from torch import nn from diffusers.models.activations import get_activation class _a ( unittest.TestCase ): def _UpperCamelCase ( self : str ): lowerCamelCase__ = get_activation('swish' ) self.assertIsInstance(SCREAMING_SNAKE_CASE__ , nn.SiLU ) self.assertEqual(act(torch.tensor(-1_00 , 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 : Union[str, Any] ): lowerCamelCase__ = get_activation('silu' ) self.assertIsInstance(SCREAMING_SNAKE_CASE__ , nn.SiLU ) self.assertEqual(act(torch.tensor(-1_00 , 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 : Optional[Any] ): lowerCamelCase__ = get_activation('mish' ) self.assertIsInstance(SCREAMING_SNAKE_CASE__ , nn.Mish ) self.assertEqual(act(torch.tensor(-2_00 , 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 : Optional[Any] ): lowerCamelCase__ = get_activation('gelu' ) self.assertIsInstance(SCREAMING_SNAKE_CASE__ , nn.GELU ) self.assertEqual(act(torch.tensor(-1_00 , 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 )
659
"""simple docstring""" import warnings from diffusers import StableDiffusionImgaImgPipeline # noqa F401 warnings.warn( "The `image_to_image.py` script is outdated. Please use directly `from diffusers import" " StableDiffusionImg2ImgPipeline` instead." )
659
1
"""simple docstring""" from scipy.stats import spearmanr import datasets _snake_case = "\nThe Spearman rank-order correlation coefficient is a measure of the\nrelationship between two datasets. Like other correlation coefficients,\nthis one varies between -1 and +1 with 0 implying no correlation.\nPositive correlations imply that as data in dataset x increases, so\ndoes data in dataset y. Negative correlations imply that as x increases,\ny decreases. Correlations of -1 or +1 imply an exact monotonic relationship.\n\nUnlike the Pearson correlation, the Spearman correlation does not\nassume that both datasets are normally distributed.\n\nThe p-value roughly indicates the probability of an uncorrelated system\nproducing datasets that have a Spearman correlation at least as extreme\nas the one computed from these datasets. The p-values are not entirely\nreliable but are probably reasonable for datasets larger than 500 or so.\n" _snake_case = "\nArgs:\n predictions (`List[float]`): Predicted labels, as returned by a model.\n references (`List[float]`): Ground truth labels.\n return_pvalue (`bool`): If `True`, returns the p-value. If `False`, returns\n only the spearmanr score. Defaults to `False`.\nReturns:\n spearmanr (`float`): Spearman correlation coefficient.\n p-value (`float`): p-value. **Note**: is only returned if `return_pvalue=True` is input.\nExamples:\n Example 1:\n >>> spearmanr_metric = datasets.load_metric(\"spearmanr\")\n >>> results = spearmanr_metric.compute(references=[1, 2, 3, 4, 5], predictions=[10, 9, 2.5, 6, 4])\n >>> print(results)\n {'spearmanr': -0.7}\n\n Example 2:\n >>> spearmanr_metric = datasets.load_metric(\"spearmanr\")\n >>> results = spearmanr_metric.compute(references=[1, 2, 3, 4, 5],\n ... predictions=[10, 9, 2.5, 6, 4],\n ... return_pvalue=True)\n >>> print(results['spearmanr'])\n -0.7\n >>> print(round(results['spearmanr_pvalue'], 2))\n 0.19\n" _snake_case = R"\\n@book{kokoska2000crc,\n title={CRC standard probability and statistics tables and formulae},\n author={Kokoska, Stephen and Zwillinger, Daniel},\n year={2000},\n publisher={Crc Press}\n}\n@article{2020SciPy-NMeth,\n author = {Virtanen, Pauli and Gommers, Ralf and Oliphant, Travis E. and\n Haberland, Matt and Reddy, Tyler and Cournapeau, David and\n Burovski, Evgeni and Peterson, Pearu and Weckesser, Warren and\n Bright, Jonathan and {van der Walt}, St{\'e}fan J. and\n Brett, Matthew and Wilson, Joshua and Millman, K. Jarrod and\n Mayorov, Nikolay and Nelson, Andrew R. J. and Jones, Eric and\n Kern, Robert and Larson, Eric and Carey, C J and\n Polat, {\.I}lhan and Feng, Yu and Moore, Eric W. and\n {VanderPlas}, Jake and Laxalde, Denis and Perktold, Josef and\n Cimrman, Robert and Henriksen, Ian and Quintero, E. A. and\n Harris, Charles R. and Archibald, Anne M. and\n Ribeiro, Ant{\^o}nio H. and Pedregosa, Fabian and\n {van Mulbregt}, Paul and {SciPy 1.0 Contributors}},\n title = {{{SciPy} 1.0: Fundamental Algorithms for Scientific\n Computing in Python}},\n journal = {Nature Methods},\n year = {2020},\n volume = {17},\n pages = {261--272},\n adsurl = {https://rdcu.be/b08Wh},\n doi = {10.1038/s41592-019-0686-2},\n}\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class _a ( datasets.Metric ): def _UpperCamelCase ( self : List[str] ): return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { 'predictions': datasets.Value('float' ), 'references': datasets.Value('float' ), } ) , reference_urls=['https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.spearmanr.html'] , ) def _UpperCamelCase ( self : List[Any] , SCREAMING_SNAKE_CASE__ : Dict , SCREAMING_SNAKE_CASE__ : Optional[Any] , SCREAMING_SNAKE_CASE__ : Any=False ): lowerCamelCase__ = spearmanr(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) if return_pvalue: return {"spearmanr": results[0], "spearmanr_pvalue": results[1]} else: return {"spearmanr": results[0]}
659
"""simple docstring""" import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_funnel import FunnelTokenizer _snake_case = logging.get_logger(__name__) _snake_case = {"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"} _snake_case = [ "small", "small-base", "medium", "medium-base", "intermediate", "intermediate-base", "large", "large-base", "xlarge", "xlarge-base", ] _snake_case = { "vocab_file": { "funnel-transformer/small": "https://huggingface.co/funnel-transformer/small/resolve/main/vocab.txt", "funnel-transformer/small-base": "https://huggingface.co/funnel-transformer/small-base/resolve/main/vocab.txt", "funnel-transformer/medium": "https://huggingface.co/funnel-transformer/medium/resolve/main/vocab.txt", "funnel-transformer/medium-base": ( "https://huggingface.co/funnel-transformer/medium-base/resolve/main/vocab.txt" ), "funnel-transformer/intermediate": ( "https://huggingface.co/funnel-transformer/intermediate/resolve/main/vocab.txt" ), "funnel-transformer/intermediate-base": ( "https://huggingface.co/funnel-transformer/intermediate-base/resolve/main/vocab.txt" ), "funnel-transformer/large": "https://huggingface.co/funnel-transformer/large/resolve/main/vocab.txt", "funnel-transformer/large-base": "https://huggingface.co/funnel-transformer/large-base/resolve/main/vocab.txt", "funnel-transformer/xlarge": "https://huggingface.co/funnel-transformer/xlarge/resolve/main/vocab.txt", "funnel-transformer/xlarge-base": ( "https://huggingface.co/funnel-transformer/xlarge-base/resolve/main/vocab.txt" ), }, "tokenizer_file": { "funnel-transformer/small": "https://huggingface.co/funnel-transformer/small/resolve/main/tokenizer.json", "funnel-transformer/small-base": ( "https://huggingface.co/funnel-transformer/small-base/resolve/main/tokenizer.json" ), "funnel-transformer/medium": "https://huggingface.co/funnel-transformer/medium/resolve/main/tokenizer.json", "funnel-transformer/medium-base": ( "https://huggingface.co/funnel-transformer/medium-base/resolve/main/tokenizer.json" ), "funnel-transformer/intermediate": ( "https://huggingface.co/funnel-transformer/intermediate/resolve/main/tokenizer.json" ), "funnel-transformer/intermediate-base": ( "https://huggingface.co/funnel-transformer/intermediate-base/resolve/main/tokenizer.json" ), "funnel-transformer/large": "https://huggingface.co/funnel-transformer/large/resolve/main/tokenizer.json", "funnel-transformer/large-base": ( "https://huggingface.co/funnel-transformer/large-base/resolve/main/tokenizer.json" ), "funnel-transformer/xlarge": "https://huggingface.co/funnel-transformer/xlarge/resolve/main/tokenizer.json", "funnel-transformer/xlarge-base": ( "https://huggingface.co/funnel-transformer/xlarge-base/resolve/main/tokenizer.json" ), }, } _snake_case = {f"""funnel-transformer/{name}""": 512 for name in _model_names} _snake_case = {f"""funnel-transformer/{name}""": {"do_lower_case": True} for name in _model_names} class _a ( SCREAMING_SNAKE_CASE_ ): a_ : List[str] = VOCAB_FILES_NAMES a_ : List[str] = PRETRAINED_VOCAB_FILES_MAP a_ : Optional[int] = PRETRAINED_INIT_CONFIGURATION a_ : List[str] = FunnelTokenizer a_ : Dict = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES a_ : int = 2 def __init__( self : int , SCREAMING_SNAKE_CASE__ : Optional[int]=None , SCREAMING_SNAKE_CASE__ : Union[str, Any]=None , SCREAMING_SNAKE_CASE__ : int=True , SCREAMING_SNAKE_CASE__ : Any="<unk>" , SCREAMING_SNAKE_CASE__ : List[Any]="<sep>" , SCREAMING_SNAKE_CASE__ : int="<pad>" , SCREAMING_SNAKE_CASE__ : Tuple="<cls>" , SCREAMING_SNAKE_CASE__ : Tuple="<mask>" , SCREAMING_SNAKE_CASE__ : Any="<s>" , SCREAMING_SNAKE_CASE__ : Tuple="</s>" , SCREAMING_SNAKE_CASE__ : str=True , SCREAMING_SNAKE_CASE__ : Any=True , SCREAMING_SNAKE_CASE__ : str=None , SCREAMING_SNAKE_CASE__ : int="##" , **SCREAMING_SNAKE_CASE__ : Any , ): super().__init__( SCREAMING_SNAKE_CASE__ , tokenizer_file=SCREAMING_SNAKE_CASE__ , do_lower_case=SCREAMING_SNAKE_CASE__ , unk_token=SCREAMING_SNAKE_CASE__ , sep_token=SCREAMING_SNAKE_CASE__ , pad_token=SCREAMING_SNAKE_CASE__ , cls_token=SCREAMING_SNAKE_CASE__ , mask_token=SCREAMING_SNAKE_CASE__ , bos_token=SCREAMING_SNAKE_CASE__ , eos_token=SCREAMING_SNAKE_CASE__ , clean_text=SCREAMING_SNAKE_CASE__ , tokenize_chinese_chars=SCREAMING_SNAKE_CASE__ , strip_accents=SCREAMING_SNAKE_CASE__ , wordpieces_prefix=SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ , ) lowerCamelCase__ = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get('lowercase' , SCREAMING_SNAKE_CASE__ ) != do_lower_case or normalizer_state.get('strip_accents' , SCREAMING_SNAKE_CASE__ ) != strip_accents or normalizer_state.get('handle_chinese_chars' , SCREAMING_SNAKE_CASE__ ) != tokenize_chinese_chars ): lowerCamelCase__ = getattr(SCREAMING_SNAKE_CASE__ , normalizer_state.pop('type' ) ) lowerCamelCase__ = do_lower_case lowerCamelCase__ = strip_accents lowerCamelCase__ = tokenize_chinese_chars lowerCamelCase__ = normalizer_class(**SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = do_lower_case def _UpperCamelCase ( self : Dict , SCREAMING_SNAKE_CASE__ : Tuple , SCREAMING_SNAKE_CASE__ : Optional[Any]=None ): lowerCamelCase__ = [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 _UpperCamelCase ( self : Dict , SCREAMING_SNAKE_CASE__ : List[int] , SCREAMING_SNAKE_CASE__ : Optional[List[int]] = None ): lowerCamelCase__ = [self.sep_token_id] lowerCamelCase__ = [self.cls_token_id] if token_ids_a is None: return len(cls ) * [self.cls_token_type_id] + len(token_ids_a + sep ) * [0] return len(cls ) * [self.cls_token_type_id] + len(token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def _UpperCamelCase ( self : Optional[int] , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : Optional[str] = None ): lowerCamelCase__ = self._tokenizer.model.save(SCREAMING_SNAKE_CASE__ , name=SCREAMING_SNAKE_CASE__ ) return tuple(SCREAMING_SNAKE_CASE__ )
659
1
"""simple docstring""" from __future__ import annotations from math import gcd def snake_case ( _a: int , _a: int = 2 , _a: int = 1 , _a: int = 3 , )-> int | None: '''simple docstring''' if num < 2: raise ValueError('The input value cannot be less than 2' ) # Because of the relationship between ``f(f(x))`` and ``f(x)``, this # algorithm struggles to find factors that are divisible by two. # As a workaround, we specifically check for two and even inputs. # See: https://math.stackexchange.com/a/2856214/165820 if num > 2 and num % 2 == 0: return 2 # Pollard's Rho algorithm requires a function that returns pseudorandom # values between 0 <= X < ``num``. It doesn't need to be random in the # sense that the output value is cryptographically secure or difficult # to calculate, it only needs to be random in the sense that all output # values should be equally likely to appear. # For this reason, Pollard suggested using ``f(x) = (x**2 - 1) % num`` # However, the success of Pollard's algorithm isn't guaranteed and is # determined in part by the initial seed and the chosen random function. # To make retries easier, we will instead use ``f(x) = (x**2 + C) % num`` # where ``C`` is a value that we can modify between each attempt. def rand_fn(_a: int , _a: int , _a: int ) -> int: return (pow(_a , 2 ) + step) % modulus for _ in range(_a ): # These track the position within the cycle detection logic. lowerCamelCase__ = seed lowerCamelCase__ = seed while True: # At each iteration, the tortoise moves one step and the hare moves two. lowerCamelCase__ = rand_fn(_a , _a , _a ) lowerCamelCase__ = rand_fn(_a , _a , _a ) lowerCamelCase__ = rand_fn(_a , _a , _a ) # At some point both the tortoise and the hare will enter a cycle whose # length ``p`` is a divisor of ``num``. Once in that cycle, at some point # the tortoise and hare will end up on the same value modulo ``p``. # We can detect when this happens because the position difference between # the tortoise and the hare will share a common divisor with ``num``. lowerCamelCase__ = gcd(hare - tortoise , _a ) if divisor == 1: # No common divisor yet, just keep searching. continue else: # We found a common divisor! if divisor == num: # Unfortunately, the divisor is ``num`` itself and is useless. break else: # The divisor is a nontrivial factor of ``num``! return divisor # If we made it here, then this attempt failed. # We need to pick a new starting seed for the tortoise and hare # in addition to a new step value for the random function. # To keep this example implementation deterministic, the # new values will be generated based on currently available # values instead of using something like ``random.randint``. # We can use the hare's position as the new seed. # This is actually what Richard Brent's the "optimized" variant does. lowerCamelCase__ = hare # The new step value for the random function can just be incremented. # At first the results will be similar to what the old function would # have produced, but the value will quickly diverge after a bit. step += 1 # We haven't found a divisor within the requested number of attempts. # We were unlucky or ``num`` itself is actually prime. return None if __name__ == "__main__": import argparse _snake_case = argparse.ArgumentParser() parser.add_argument( "num", type=int, help="The value to find a divisor of", ) parser.add_argument( "--attempts", type=int, default=3, help="The number of attempts before giving up", ) _snake_case = parser.parse_args() _snake_case = pollard_rho(args.num, attempts=args.attempts) if divisor is None: print(f"""{args.num} is probably prime""") else: _snake_case = args.num // divisor print(f"""{args.num} = {divisor} * {quotient}""")
659
"""simple docstring""" # A Bipartite Graph is a graph whose vertices can be divided into two independent sets, # U and V such that every edge (u, v) either connects a vertex from U to V or a vertex # from V to U. In other words, for every edge (u, v), either u belongs to U and v to V, # or u belongs to V and v to U. We can also say that there is no edge that connects # vertices of same set. def snake_case ( _a: Optional[Any] )-> Union[str, Any]: '''simple docstring''' lowerCamelCase__ = [False] * len(_a ) lowerCamelCase__ = [-1] * len(_a ) def dfs(_a: Any , _a: Optional[int] ): lowerCamelCase__ = True lowerCamelCase__ = c for u in graph[v]: if not visited[u]: dfs(_a , 1 - c ) for i in range(len(_a ) ): if not visited[i]: dfs(_a , 0 ) for i in range(len(_a ) ): for j in graph[i]: if color[i] == color[j]: return False return True # Adjacency list of graph _snake_case = {0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 4: []} print(check_bipartite_dfs(graph))
659
1
"""simple docstring""" _snake_case = 0 # The first color of the flag. _snake_case = 1 # The second color of the flag. _snake_case = 2 # The third color of the flag. _snake_case = (red, white, blue) def snake_case ( _a: list )-> list: '''simple docstring''' if not sequence: return [] if len(_a ) == 1: return list(_a ) lowerCamelCase__ = 0 lowerCamelCase__ = len(_a ) - 1 lowerCamelCase__ = 0 while mid <= high: if sequence[mid] == colors[0]: lowerCamelCase__ , lowerCamelCase__ = sequence[mid], sequence[low] low += 1 mid += 1 elif sequence[mid] == colors[1]: mid += 1 elif sequence[mid] == colors[2]: lowerCamelCase__ , lowerCamelCase__ = sequence[high], sequence[mid] high -= 1 else: lowerCamelCase__ = F'The elements inside the sequence must contains only {colors} values' raise ValueError(_a ) return sequence if __name__ == "__main__": import doctest doctest.testmod() _snake_case = input("Enter numbers separated by commas:\n").strip() _snake_case = [int(item.strip()) for item in user_input.split(",")] print(f"""{dutch_national_flag_sort(unsorted)}""")
659
"""simple docstring""" from collections.abc import Iterator, MutableMapping from dataclasses import dataclass from typing import Generic, TypeVar _snake_case = TypeVar("KEY") _snake_case = TypeVar("VAL") @dataclass(frozen=SCREAMING_SNAKE_CASE_ , slots=SCREAMING_SNAKE_CASE_ ) class _a ( Generic[KEY, VAL] ): a_ : KEY a_ : VAL class _a ( _Item ): def __init__( self : List[str] ): super().__init__(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) def __bool__( self : str ): return False _snake_case = _DeletedItem() class _a ( MutableMapping[KEY, VAL] ): def __init__( self : Optional[Any] , SCREAMING_SNAKE_CASE__ : int = 8 , SCREAMING_SNAKE_CASE__ : float = 0.75 ): lowerCamelCase__ = initial_block_size lowerCamelCase__ = [None] * initial_block_size assert 0.0 < capacity_factor < 1.0 lowerCamelCase__ = capacity_factor lowerCamelCase__ = 0 def _UpperCamelCase ( self : Optional[int] , SCREAMING_SNAKE_CASE__ : KEY ): return hash(SCREAMING_SNAKE_CASE__ ) % len(self._buckets ) def _UpperCamelCase ( self : str , SCREAMING_SNAKE_CASE__ : int ): return (ind + 1) % len(self._buckets ) def _UpperCamelCase ( self : Union[str, Any] , SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : KEY , SCREAMING_SNAKE_CASE__ : VAL ): lowerCamelCase__ = self._buckets[ind] if not stored: lowerCamelCase__ = _Item(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) self._len += 1 return True elif stored.key == key: lowerCamelCase__ = _Item(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) return True else: return False def _UpperCamelCase ( self : Dict ): lowerCamelCase__ = len(self._buckets ) * self._capacity_factor return len(self ) >= int(SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : int ): if len(self._buckets ) <= self._initial_block_size: return False lowerCamelCase__ = len(self._buckets ) * self._capacity_factor / 2 return len(self ) < limit def _UpperCamelCase ( self : List[str] , SCREAMING_SNAKE_CASE__ : int ): lowerCamelCase__ = self._buckets lowerCamelCase__ = [None] * new_size lowerCamelCase__ = 0 for item in old_buckets: if item: self._add_item(item.key , item.val ) def _UpperCamelCase ( self : List[str] ): self._resize(len(self._buckets ) * 2 ) def _UpperCamelCase ( self : Optional[int] ): self._resize(len(self._buckets ) // 2 ) def _UpperCamelCase ( self : Any , SCREAMING_SNAKE_CASE__ : KEY ): lowerCamelCase__ = self._get_bucket_index(SCREAMING_SNAKE_CASE__ ) for _ in range(len(self._buckets ) ): yield ind lowerCamelCase__ = self._get_next_ind(SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : Optional[Any] , SCREAMING_SNAKE_CASE__ : KEY , SCREAMING_SNAKE_CASE__ : VAL ): for ind in self._iterate_buckets(SCREAMING_SNAKE_CASE__ ): if self._try_set(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): break def __setitem__( self : Dict , SCREAMING_SNAKE_CASE__ : KEY , SCREAMING_SNAKE_CASE__ : VAL ): if self._is_full(): self._size_up() self._add_item(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) def __delitem__( self : Dict , SCREAMING_SNAKE_CASE__ : KEY ): for ind in self._iterate_buckets(SCREAMING_SNAKE_CASE__ ): lowerCamelCase__ = self._buckets[ind] if item is None: raise KeyError(SCREAMING_SNAKE_CASE__ ) if item is _deleted: continue if item.key == key: lowerCamelCase__ = _deleted self._len -= 1 break if self._is_sparse(): self._size_down() def __getitem__( self : str , SCREAMING_SNAKE_CASE__ : KEY ): for ind in self._iterate_buckets(SCREAMING_SNAKE_CASE__ ): lowerCamelCase__ = self._buckets[ind] if item is None: break if item is _deleted: continue if item.key == key: return item.val raise KeyError(SCREAMING_SNAKE_CASE__ ) def __len__( self : List[Any] ): return self._len def __iter__( self : Optional[int] ): yield from (item.key for item in self._buckets if item) def __repr__( self : str ): lowerCamelCase__ = ' ,'.join( F'{item.key}: {item.val}' for item in self._buckets if item ) return F'HashMap({val_string})'
659
1
"""simple docstring""" def snake_case ( _a: int )-> int: '''simple docstring''' lowerCamelCase__ = 1 for i in range(1 , num + 1 ): fact *= i return fact def snake_case ( _a: int )-> int: '''simple docstring''' lowerCamelCase__ = 0 while number > 0: lowerCamelCase__ = number % 10 sum_of_digits += last_digit lowerCamelCase__ = number // 10 # Removing the last_digit from the given number return sum_of_digits def snake_case ( _a: int = 100 )-> int: '''simple docstring''' lowerCamelCase__ = factorial(_a ) lowerCamelCase__ = split_and_add(_a ) return result if __name__ == "__main__": print(solution(int(input("Enter the Number: ").strip())))
659
"""simple docstring""" def snake_case ( _a: int , _a: list[int] , _a: int )-> int: '''simple docstring''' def count_of_possible_combinations(_a: int ) -> int: if target < 0: return 0 if target == 0: return 1 return sum(count_of_possible_combinations(target - item ) for item in array ) return count_of_possible_combinations(_a ) def snake_case ( _a: int , _a: list[int] , _a: int )-> int: '''simple docstring''' def count_of_possible_combinations_with_dp_array( _a: int , _a: list[int] ) -> int: if target < 0: return 0 if target == 0: return 1 if dp_array[target] != -1: return dp_array[target] lowerCamelCase__ = sum( count_of_possible_combinations_with_dp_array(target - item , _a ) for item in array ) lowerCamelCase__ = answer return answer lowerCamelCase__ = [-1] * (target + 1) return count_of_possible_combinations_with_dp_array(_a , _a ) def snake_case ( _a: int , _a: list[int] , _a: int )-> int: '''simple docstring''' lowerCamelCase__ = [0] * (target + 1) lowerCamelCase__ = 1 for i in range(1 , target + 1 ): for j in range(_a ): if i - array[j] >= 0: dp_array[i] += dp_array[i - array[j]] return dp_array[target] if __name__ == "__main__": import doctest doctest.testmod() _snake_case = 3 _snake_case = 5 _snake_case = [1, 2, 5] print(combination_sum_iv(n, array, target))
659
1
"""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 _snake_case = logging.get_logger(__name__) _snake_case = { "google/vit-base-patch16-224": "https://huggingface.co/vit-base-patch16-224/resolve/main/config.json", # See all ViT models at https://huggingface.co/models?filter=vit } class _a ( SCREAMING_SNAKE_CASE_ ): a_ : List[Any] = 'vit' def __init__( self : int , SCREAMING_SNAKE_CASE__ : str=7_68 , SCREAMING_SNAKE_CASE__ : Optional[Any]=12 , SCREAMING_SNAKE_CASE__ : Tuple=12 , SCREAMING_SNAKE_CASE__ : str=30_72 , SCREAMING_SNAKE_CASE__ : str="gelu" , SCREAMING_SNAKE_CASE__ : List[str]=0.0 , SCREAMING_SNAKE_CASE__ : str=0.0 , SCREAMING_SNAKE_CASE__ : List[Any]=0.02 , SCREAMING_SNAKE_CASE__ : Optional[Any]=1e-12 , SCREAMING_SNAKE_CASE__ : Optional[Any]=2_24 , SCREAMING_SNAKE_CASE__ : str=16 , SCREAMING_SNAKE_CASE__ : Optional[int]=3 , SCREAMING_SNAKE_CASE__ : Any=True , SCREAMING_SNAKE_CASE__ : Union[str, Any]=16 , **SCREAMING_SNAKE_CASE__ : str , ): super().__init__(**SCREAMING_SNAKE_CASE__ ) 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__ = initializer_range lowerCamelCase__ = layer_norm_eps lowerCamelCase__ = image_size lowerCamelCase__ = patch_size lowerCamelCase__ = num_channels lowerCamelCase__ = qkv_bias lowerCamelCase__ = encoder_stride class _a ( SCREAMING_SNAKE_CASE_ ): a_ : Dict = version.parse('1.11' ) @property def _UpperCamelCase ( self : Optional[Any] ): return OrderedDict( [ ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}), ] ) @property def _UpperCamelCase ( self : List[Any] ): return 1e-4
659
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available _snake_case = { "configuration_m2m_100": ["M2M_100_PRETRAINED_CONFIG_ARCHIVE_MAP", "M2M100Config", "M2M100OnnxConfig"], "tokenization_m2m_100": ["M2M100Tokenizer"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = [ "M2M_100_PRETRAINED_MODEL_ARCHIVE_LIST", "M2M100ForConditionalGeneration", "M2M100Model", "M2M100PreTrainedModel", ] if TYPE_CHECKING: from .configuration_mam_aaa import M2M_100_PRETRAINED_CONFIG_ARCHIVE_MAP, MaMaaaConfig, MaMaaaOnnxConfig from .tokenization_mam_aaa import MaMaaaTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mam_aaa import ( M2M_100_PRETRAINED_MODEL_ARCHIVE_LIST, MaMaaaForConditionalGeneration, MaMaaaModel, MaMaaaPreTrainedModel, ) else: import sys _snake_case = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
659
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available _snake_case = { "configuration_mobilenet_v2": [ "MOBILENET_V2_PRETRAINED_CONFIG_ARCHIVE_MAP", "MobileNetV2Config", "MobileNetV2OnnxConfig", ], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = ["MobileNetV2FeatureExtractor"] _snake_case = ["MobileNetV2ImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = [ "MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST", "MobileNetV2ForImageClassification", "MobileNetV2ForSemanticSegmentation", "MobileNetV2Model", "MobileNetV2PreTrainedModel", "load_tf_weights_in_mobilenet_v2", ] if TYPE_CHECKING: from .configuration_mobilenet_va import ( MOBILENET_V2_PRETRAINED_CONFIG_ARCHIVE_MAP, MobileNetVaConfig, MobileNetVaOnnxConfig, ) try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_mobilenet_va import MobileNetVaFeatureExtractor from .image_processing_mobilenet_va import MobileNetVaImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mobilenet_va import ( MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST, MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation, MobileNetVaModel, MobileNetVaPreTrainedModel, load_tf_weights_in_mobilenet_va, ) else: import sys _snake_case = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
659
"""simple docstring""" def snake_case ( _a: list[list[float]] )-> list[list[float]]: '''simple docstring''' lowerCamelCase__ = [] for data in source_data: for i, el in enumerate(_a ): if len(_a ) < i + 1: data_lists.append([] ) data_lists[i].append(float(_a ) ) return data_lists def snake_case ( _a: list[list[float]] , _a: list[int] )-> list[list[float]]: '''simple docstring''' lowerCamelCase__ = [] for dlist, weight in zip(_a , _a ): lowerCamelCase__ = min(_a ) lowerCamelCase__ = max(_a ) lowerCamelCase__ = [] # for weight 0 score is 1 - actual score if weight == 0: for item in dlist: try: score.append(1 - ((item - mind) / (maxd - mind)) ) except ZeroDivisionError: score.append(1 ) elif weight == 1: for item in dlist: try: score.append((item - mind) / (maxd - mind) ) except ZeroDivisionError: score.append(0 ) # weight not 0 or 1 else: lowerCamelCase__ = F'Invalid weight of {weight:f} provided' raise ValueError(_a ) score_lists.append(_a ) return score_lists def snake_case ( _a: list[list[float]] )-> list[float]: '''simple docstring''' lowerCamelCase__ = [0 for i in range(len(score_lists[0] ) )] for slist in score_lists: for j, ele in enumerate(_a ): lowerCamelCase__ = final_scores[j] + ele return final_scores def snake_case ( _a: list[list[float]] , _a: list[int] )-> list[list[float]]: '''simple docstring''' lowerCamelCase__ = get_data(_a ) lowerCamelCase__ = calculate_each_score(_a , _a ) lowerCamelCase__ = generate_final_scores(_a ) # append scores to source data for i, ele in enumerate(_a ): source_data[i].append(_a ) return source_data
659
1
"""simple docstring""" _snake_case = "0.18.2" from .configuration_utils import ConfigMixin from .utils import ( OptionalDependencyNotAvailable, is_flax_available, is_inflect_available, is_invisible_watermark_available, is_k_diffusion_available, is_k_diffusion_version, is_librosa_available, is_note_seq_available, is_onnx_available, is_scipy_available, is_torch_available, is_torchsde_available, is_transformers_available, is_transformers_version, is_unidecode_available, logging, ) try: if not is_onnx_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_onnx_objects import * # noqa F403 else: from .pipelines import OnnxRuntimeModel try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_pt_objects import * # noqa F403 else: from .models import ( AutoencoderKL, ControlNetModel, ModelMixin, PriorTransformer, TaFilmDecoder, TransformeraDModel, UNetaDModel, UNetaDConditionModel, UNetaDModel, UNetaDConditionModel, VQModel, ) from .optimization import ( get_constant_schedule, get_constant_schedule_with_warmup, get_cosine_schedule_with_warmup, get_cosine_with_hard_restarts_schedule_with_warmup, get_linear_schedule_with_warmup, get_polynomial_decay_schedule_with_warmup, get_scheduler, ) from .pipelines import ( AudioPipelineOutput, ConsistencyModelPipeline, DanceDiffusionPipeline, DDIMPipeline, DDPMPipeline, DiffusionPipeline, DiTPipeline, ImagePipelineOutput, KarrasVePipeline, LDMPipeline, LDMSuperResolutionPipeline, PNDMPipeline, RePaintPipeline, ScoreSdeVePipeline, ) from .schedulers import ( CMStochasticIterativeScheduler, DDIMInverseScheduler, DDIMParallelScheduler, DDIMScheduler, DDPMParallelScheduler, DDPMScheduler, DEISMultistepScheduler, DPMSolverMultistepInverseScheduler, DPMSolverMultistepScheduler, DPMSolverSinglestepScheduler, EulerAncestralDiscreteScheduler, EulerDiscreteScheduler, HeunDiscreteScheduler, IPNDMScheduler, KarrasVeScheduler, KDPMaAncestralDiscreteScheduler, KDPMaDiscreteScheduler, PNDMScheduler, RePaintScheduler, SchedulerMixin, ScoreSdeVeScheduler, UnCLIPScheduler, UniPCMultistepScheduler, VQDiffusionScheduler, ) from .training_utils import EMAModel try: if not (is_torch_available() and is_scipy_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_scipy_objects import * # noqa F403 else: from .schedulers import LMSDiscreteScheduler try: if not (is_torch_available() and is_torchsde_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_torchsde_objects import * # noqa F403 else: from .schedulers import DPMSolverSDEScheduler try: if not (is_torch_available() and is_transformers_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_transformers_objects import * # noqa F403 else: from .pipelines import ( AltDiffusionImgaImgPipeline, AltDiffusionPipeline, AudioLDMPipeline, CycleDiffusionPipeline, IFImgaImgPipeline, IFImgaImgSuperResolutionPipeline, IFInpaintingPipeline, IFInpaintingSuperResolutionPipeline, IFPipeline, IFSuperResolutionPipeline, ImageTextPipelineOutput, KandinskyImgaImgPipeline, KandinskyInpaintPipeline, KandinskyPipeline, KandinskyPriorPipeline, KandinskyVaaControlnetImgaImgPipeline, KandinskyVaaControlnetPipeline, KandinskyVaaImgaImgPipeline, KandinskyVaaInpaintPipeline, KandinskyVaaPipeline, KandinskyVaaPriorEmbaEmbPipeline, KandinskyVaaPriorPipeline, LDMTextToImagePipeline, PaintByExamplePipeline, SemanticStableDiffusionPipeline, ShapEImgaImgPipeline, ShapEPipeline, StableDiffusionAttendAndExcitePipeline, StableDiffusionControlNetImgaImgPipeline, StableDiffusionControlNetInpaintPipeline, StableDiffusionControlNetPipeline, StableDiffusionDepthaImgPipeline, StableDiffusionDiffEditPipeline, StableDiffusionImageVariationPipeline, StableDiffusionImgaImgPipeline, StableDiffusionInpaintPipeline, StableDiffusionInpaintPipelineLegacy, StableDiffusionInstructPixaPixPipeline, StableDiffusionLatentUpscalePipeline, StableDiffusionLDMaDPipeline, StableDiffusionModelEditingPipeline, StableDiffusionPanoramaPipeline, StableDiffusionParadigmsPipeline, StableDiffusionPipeline, StableDiffusionPipelineSafe, StableDiffusionPixaPixZeroPipeline, StableDiffusionSAGPipeline, StableDiffusionUpscalePipeline, StableUnCLIPImgaImgPipeline, StableUnCLIPPipeline, TextToVideoSDPipeline, TextToVideoZeroPipeline, UnCLIPImageVariationPipeline, UnCLIPPipeline, UniDiffuserModel, UniDiffuserPipeline, UniDiffuserTextDecoder, VersatileDiffusionDualGuidedPipeline, VersatileDiffusionImageVariationPipeline, VersatileDiffusionPipeline, VersatileDiffusionTextToImagePipeline, VideoToVideoSDPipeline, VQDiffusionPipeline, ) try: if not (is_torch_available() and is_transformers_available() and is_invisible_watermark_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_transformers_and_invisible_watermark_objects import * # noqa F403 else: from .pipelines import StableDiffusionXLImgaImgPipeline, StableDiffusionXLPipeline try: if not (is_torch_available() and is_transformers_available() and is_k_diffusion_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_transformers_and_k_diffusion_objects import * # noqa F403 else: from .pipelines import StableDiffusionKDiffusionPipeline try: if not (is_torch_available() and is_transformers_available() and is_onnx_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_transformers_and_onnx_objects import * # noqa F403 else: from .pipelines import ( OnnxStableDiffusionImgaImgPipeline, OnnxStableDiffusionInpaintPipeline, OnnxStableDiffusionInpaintPipelineLegacy, OnnxStableDiffusionPipeline, OnnxStableDiffusionUpscalePipeline, StableDiffusionOnnxPipeline, ) try: if not (is_torch_available() and is_librosa_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_librosa_objects import * # noqa F403 else: from .pipelines import AudioDiffusionPipeline, Mel try: if not (is_transformers_available() and is_torch_available() and is_note_seq_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_transformers_and_torch_and_note_seq_objects import * # noqa F403 else: from .pipelines import SpectrogramDiffusionPipeline try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_flax_objects import * # noqa F403 else: from .models.controlnet_flax import FlaxControlNetModel from .models.modeling_flax_utils import FlaxModelMixin from .models.unet_ad_condition_flax import FlaxUNetaDConditionModel from .models.vae_flax import FlaxAutoencoderKL from .pipelines import FlaxDiffusionPipeline from .schedulers import ( FlaxDDIMScheduler, FlaxDDPMScheduler, FlaxDPMSolverMultistepScheduler, FlaxKarrasVeScheduler, FlaxLMSDiscreteScheduler, FlaxPNDMScheduler, FlaxSchedulerMixin, FlaxScoreSdeVeScheduler, ) try: if not (is_flax_available() and is_transformers_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_flax_and_transformers_objects import * # noqa F403 else: from .pipelines import ( FlaxStableDiffusionControlNetPipeline, FlaxStableDiffusionImgaImgPipeline, FlaxStableDiffusionInpaintPipeline, FlaxStableDiffusionPipeline, ) try: if not (is_note_seq_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_note_seq_objects import * # noqa F403 else: from .pipelines import MidiProcessor
659
"""simple docstring""" from __future__ import annotations from math import gcd def snake_case ( _a: int , _a: int = 2 , _a: int = 1 , _a: int = 3 , )-> int | None: '''simple docstring''' if num < 2: raise ValueError('The input value cannot be less than 2' ) # Because of the relationship between ``f(f(x))`` and ``f(x)``, this # algorithm struggles to find factors that are divisible by two. # As a workaround, we specifically check for two and even inputs. # See: https://math.stackexchange.com/a/2856214/165820 if num > 2 and num % 2 == 0: return 2 # Pollard's Rho algorithm requires a function that returns pseudorandom # values between 0 <= X < ``num``. It doesn't need to be random in the # sense that the output value is cryptographically secure or difficult # to calculate, it only needs to be random in the sense that all output # values should be equally likely to appear. # For this reason, Pollard suggested using ``f(x) = (x**2 - 1) % num`` # However, the success of Pollard's algorithm isn't guaranteed and is # determined in part by the initial seed and the chosen random function. # To make retries easier, we will instead use ``f(x) = (x**2 + C) % num`` # where ``C`` is a value that we can modify between each attempt. def rand_fn(_a: int , _a: int , _a: int ) -> int: return (pow(_a , 2 ) + step) % modulus for _ in range(_a ): # These track the position within the cycle detection logic. lowerCamelCase__ = seed lowerCamelCase__ = seed while True: # At each iteration, the tortoise moves one step and the hare moves two. lowerCamelCase__ = rand_fn(_a , _a , _a ) lowerCamelCase__ = rand_fn(_a , _a , _a ) lowerCamelCase__ = rand_fn(_a , _a , _a ) # At some point both the tortoise and the hare will enter a cycle whose # length ``p`` is a divisor of ``num``. Once in that cycle, at some point # the tortoise and hare will end up on the same value modulo ``p``. # We can detect when this happens because the position difference between # the tortoise and the hare will share a common divisor with ``num``. lowerCamelCase__ = gcd(hare - tortoise , _a ) if divisor == 1: # No common divisor yet, just keep searching. continue else: # We found a common divisor! if divisor == num: # Unfortunately, the divisor is ``num`` itself and is useless. break else: # The divisor is a nontrivial factor of ``num``! return divisor # If we made it here, then this attempt failed. # We need to pick a new starting seed for the tortoise and hare # in addition to a new step value for the random function. # To keep this example implementation deterministic, the # new values will be generated based on currently available # values instead of using something like ``random.randint``. # We can use the hare's position as the new seed. # This is actually what Richard Brent's the "optimized" variant does. lowerCamelCase__ = hare # The new step value for the random function can just be incremented. # At first the results will be similar to what the old function would # have produced, but the value will quickly diverge after a bit. step += 1 # We haven't found a divisor within the requested number of attempts. # We were unlucky or ``num`` itself is actually prime. return None if __name__ == "__main__": import argparse _snake_case = argparse.ArgumentParser() parser.add_argument( "num", type=int, help="The value to find a divisor of", ) parser.add_argument( "--attempts", type=int, default=3, help="The number of attempts before giving up", ) _snake_case = parser.parse_args() _snake_case = pollard_rho(args.num, attempts=args.attempts) if divisor is None: print(f"""{args.num} is probably prime""") else: _snake_case = args.num // divisor print(f"""{args.num} = {divisor} * {quotient}""")
659
1
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging _snake_case = logging.get_logger(__name__) _snake_case = { "unc-nlp/lxmert-base-uncased": "https://huggingface.co/unc-nlp/lxmert-base-uncased/resolve/main/config.json", } class _a ( SCREAMING_SNAKE_CASE_ ): a_ : List[Any] = 'lxmert' a_ : Optional[int] = {} def __init__( self : List[str] , SCREAMING_SNAKE_CASE__ : Optional[int]=3_05_22 , SCREAMING_SNAKE_CASE__ : Tuple=7_68 , SCREAMING_SNAKE_CASE__ : str=12 , SCREAMING_SNAKE_CASE__ : Dict=95_00 , SCREAMING_SNAKE_CASE__ : str=16_00 , SCREAMING_SNAKE_CASE__ : str=4_00 , SCREAMING_SNAKE_CASE__ : Union[str, Any]=30_72 , SCREAMING_SNAKE_CASE__ : Union[str, Any]="gelu" , SCREAMING_SNAKE_CASE__ : List[str]=0.1 , SCREAMING_SNAKE_CASE__ : str=0.1 , SCREAMING_SNAKE_CASE__ : int=5_12 , SCREAMING_SNAKE_CASE__ : Optional[Any]=2 , SCREAMING_SNAKE_CASE__ : List[Any]=0.02 , SCREAMING_SNAKE_CASE__ : List[str]=1e-12 , SCREAMING_SNAKE_CASE__ : Tuple=9 , SCREAMING_SNAKE_CASE__ : Tuple=5 , SCREAMING_SNAKE_CASE__ : Optional[int]=5 , SCREAMING_SNAKE_CASE__ : str=20_48 , SCREAMING_SNAKE_CASE__ : Optional[int]=4 , SCREAMING_SNAKE_CASE__ : Dict=6.67 , SCREAMING_SNAKE_CASE__ : List[Any]=True , SCREAMING_SNAKE_CASE__ : Optional[int]=True , SCREAMING_SNAKE_CASE__ : Union[str, Any]=True , SCREAMING_SNAKE_CASE__ : Union[str, Any]=True , SCREAMING_SNAKE_CASE__ : Optional[int]=True , SCREAMING_SNAKE_CASE__ : Optional[int]=True , SCREAMING_SNAKE_CASE__ : str=True , **SCREAMING_SNAKE_CASE__ : Optional[Any] , ): lowerCamelCase__ = vocab_size lowerCamelCase__ = hidden_size lowerCamelCase__ = num_attention_heads lowerCamelCase__ = hidden_act lowerCamelCase__ = intermediate_size 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__ = num_qa_labels lowerCamelCase__ = num_object_labels lowerCamelCase__ = num_attr_labels lowerCamelCase__ = l_layers lowerCamelCase__ = x_layers lowerCamelCase__ = r_layers lowerCamelCase__ = visual_feat_dim lowerCamelCase__ = visual_pos_dim lowerCamelCase__ = visual_loss_normalizer lowerCamelCase__ = task_matched lowerCamelCase__ = task_mask_lm lowerCamelCase__ = task_obj_predict lowerCamelCase__ = task_qa lowerCamelCase__ = visual_obj_loss lowerCamelCase__ = visual_attr_loss lowerCamelCase__ = visual_feat_loss lowerCamelCase__ = {'vision': r_layers, 'cross_encoder': x_layers, 'language': l_layers} super().__init__(**SCREAMING_SNAKE_CASE__ )
659
"""simple docstring""" from ...utils import ( OptionalDependencyNotAvailable, is_torch_available, is_transformers_available, is_transformers_version, ) try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import ShapEPipeline else: from .camera import create_pan_cameras from .pipeline_shap_e import ShapEPipeline from .pipeline_shap_e_img2img import ShapEImgaImgPipeline from .renderer import ( BoundingBoxVolume, ImportanceRaySampler, MLPNeRFModelOutput, MLPNeRSTFModel, ShapEParamsProjModel, ShapERenderer, StratifiedRaySampler, VoidNeRFModel, )
659
1
"""simple docstring""" import pytest import datasets.config from datasets.utils.info_utils import is_small_dataset @pytest.mark.parametrize('dataset_size' , [None, 400 * 2**20, 600 * 2**20] ) @pytest.mark.parametrize('input_in_memory_max_size' , ['default', 0, 100 * 2**20, 900 * 2**20] ) def snake_case ( _a: str , _a: Union[str, Any] , _a: List[str] )-> List[str]: '''simple docstring''' if input_in_memory_max_size != "default": monkeypatch.setattr(datasets.config , 'IN_MEMORY_MAX_SIZE' , _a ) lowerCamelCase__ = datasets.config.IN_MEMORY_MAX_SIZE if input_in_memory_max_size == "default": assert in_memory_max_size == 0 else: assert in_memory_max_size == input_in_memory_max_size if dataset_size and in_memory_max_size: lowerCamelCase__ = dataset_size < in_memory_max_size else: lowerCamelCase__ = False lowerCamelCase__ = is_small_dataset(_a ) assert result == expected
659
"""simple docstring""" from __future__ import annotations _snake_case = [ [-1, 0], # left [0, -1], # down [1, 0], # right [0, 1], # up ] def snake_case ( _a: list[list[int]] , _a: list[int] , _a: list[int] , _a: int , _a: list[list[int]] , )-> tuple[list[list[int]], list[list[int]]]: '''simple docstring''' lowerCamelCase__ = [ [0 for col in range(len(grid[0] ) )] for row in range(len(_a ) ) ] # the reference grid lowerCamelCase__ = 1 lowerCamelCase__ = [ [0 for col in range(len(grid[0] ) )] for row in range(len(_a ) ) ] # the action grid lowerCamelCase__ = init[0] lowerCamelCase__ = init[1] lowerCamelCase__ = 0 lowerCamelCase__ = g + heuristic[x][y] # cost from starting cell to destination cell lowerCamelCase__ = [[f, g, x, y]] lowerCamelCase__ = False # flag that is set when search is complete lowerCamelCase__ = False # flag set if we can't find expand while not found and not resign: if len(_a ) == 0: raise ValueError('Algorithm is unable to find solution' ) else: # to choose the least costliest action so as to move closer to the goal cell.sort() cell.reverse() lowerCamelCase__ = cell.pop() lowerCamelCase__ = next_cell[2] lowerCamelCase__ = next_cell[3] lowerCamelCase__ = next_cell[1] if x == goal[0] and y == goal[1]: lowerCamelCase__ = True else: for i in range(len(_a ) ): # to try out different valid actions lowerCamelCase__ = x + DIRECTIONS[i][0] lowerCamelCase__ = y + DIRECTIONS[i][1] if xa >= 0 and xa < len(_a ) and ya >= 0 and ya < len(grid[0] ): if closed[xa][ya] == 0 and grid[xa][ya] == 0: lowerCamelCase__ = g + cost lowerCamelCase__ = ga + heuristic[xa][ya] cell.append([fa, ga, xa, ya] ) lowerCamelCase__ = 1 lowerCamelCase__ = i lowerCamelCase__ = [] lowerCamelCase__ = goal[0] lowerCamelCase__ = goal[1] invpath.append([x, y] ) # we get the reverse path from here while x != init[0] or y != init[1]: lowerCamelCase__ = x - DIRECTIONS[action[x][y]][0] lowerCamelCase__ = y - DIRECTIONS[action[x][y]][1] lowerCamelCase__ = xa lowerCamelCase__ = ya invpath.append([x, y] ) lowerCamelCase__ = [] for i in range(len(_a ) ): path.append(invpath[len(_a ) - 1 - i] ) return path, action if __name__ == "__main__": _snake_case = [ [0, 1, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles [0, 1, 0, 0, 0, 0], [0, 1, 0, 0, 1, 0], [0, 0, 0, 0, 1, 0], ] _snake_case = [0, 0] # all coordinates are given in format [y,x] _snake_case = [len(grid) - 1, len(grid[0]) - 1] _snake_case = 1 # the cost map which pushes the path closer to the goal _snake_case = [[0 for row in range(len(grid[0]))] for col in range(len(grid))] for i in range(len(grid)): for j in range(len(grid[0])): _snake_case = abs(i - goal[0]) + abs(j - goal[1]) if grid[i][j] == 1: # added extra penalty in the heuristic map _snake_case = 99 _snake_case , _snake_case = search(grid, init, goal, cost, heuristic) print("ACTION MAP") for i in range(len(action)): print(action[i]) for i in range(len(path)): print(path[i])
659
1
"""simple docstring""" from __future__ import annotations import os import tempfile import unittest from transformers import ConvBertConfig, is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TFConvBertForMaskedLM, TFConvBertForMultipleChoice, TFConvBertForQuestionAnswering, TFConvBertForSequenceClassification, TFConvBertForTokenClassification, TFConvBertModel, ) class _a : def __init__( self : str , SCREAMING_SNAKE_CASE__ : List[Any] , SCREAMING_SNAKE_CASE__ : Tuple=13 , SCREAMING_SNAKE_CASE__ : int=7 , SCREAMING_SNAKE_CASE__ : Union[str, Any]=True , SCREAMING_SNAKE_CASE__ : int=True , SCREAMING_SNAKE_CASE__ : Optional[int]=True , SCREAMING_SNAKE_CASE__ : Tuple=True , SCREAMING_SNAKE_CASE__ : Optional[int]=99 , SCREAMING_SNAKE_CASE__ : Any=32 , SCREAMING_SNAKE_CASE__ : Any=2 , SCREAMING_SNAKE_CASE__ : str=4 , SCREAMING_SNAKE_CASE__ : Dict=37 , SCREAMING_SNAKE_CASE__ : Tuple="gelu" , SCREAMING_SNAKE_CASE__ : int=0.1 , SCREAMING_SNAKE_CASE__ : Optional[int]=0.1 , SCREAMING_SNAKE_CASE__ : Dict=5_12 , SCREAMING_SNAKE_CASE__ : int=16 , SCREAMING_SNAKE_CASE__ : Tuple=2 , SCREAMING_SNAKE_CASE__ : Any=0.02 , SCREAMING_SNAKE_CASE__ : Optional[Any]=3 , SCREAMING_SNAKE_CASE__ : Optional[int]=4 , SCREAMING_SNAKE_CASE__ : List[Any]=None , ): lowerCamelCase__ = parent lowerCamelCase__ = 13 lowerCamelCase__ = 7 lowerCamelCase__ = True lowerCamelCase__ = True lowerCamelCase__ = True lowerCamelCase__ = True lowerCamelCase__ = 99 lowerCamelCase__ = 3_84 lowerCamelCase__ = 2 lowerCamelCase__ = 4 lowerCamelCase__ = 37 lowerCamelCase__ = 'gelu' lowerCamelCase__ = 0.1 lowerCamelCase__ = 0.1 lowerCamelCase__ = 5_12 lowerCamelCase__ = 16 lowerCamelCase__ = 2 lowerCamelCase__ = 0.02 lowerCamelCase__ = 3 lowerCamelCase__ = 4 lowerCamelCase__ = 1_28 lowerCamelCase__ = 2 lowerCamelCase__ = 9 lowerCamelCase__ = 1 lowerCamelCase__ = None def _UpperCamelCase ( self : List[Any] ): lowerCamelCase__ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) lowerCamelCase__ = None if self.use_input_mask: lowerCamelCase__ = random_attention_mask([self.batch_size, self.seq_length] ) lowerCamelCase__ = None if self.use_token_type_ids: lowerCamelCase__ = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) lowerCamelCase__ = None lowerCamelCase__ = None lowerCamelCase__ = None if self.use_labels: lowerCamelCase__ = ids_tensor([self.batch_size] , self.type_sequence_label_size ) lowerCamelCase__ = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) lowerCamelCase__ = ids_tensor([self.batch_size] , self.num_choices ) lowerCamelCase__ = ConvBertConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , return_dict=SCREAMING_SNAKE_CASE__ , ) return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def _UpperCamelCase ( self : Tuple , SCREAMING_SNAKE_CASE__ : Optional[int] , SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : Optional[Any] , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : Any , SCREAMING_SNAKE_CASE__ : List[str] ): lowerCamelCase__ = TFConvBertModel(config=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids} lowerCamelCase__ = [input_ids, input_mask] lowerCamelCase__ = model(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = model(SCREAMING_SNAKE_CASE__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _UpperCamelCase ( self : int , SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : Union[str, Any] , SCREAMING_SNAKE_CASE__ : Dict , SCREAMING_SNAKE_CASE__ : Tuple , SCREAMING_SNAKE_CASE__ : List[Any] , SCREAMING_SNAKE_CASE__ : Any , SCREAMING_SNAKE_CASE__ : Any ): lowerCamelCase__ = TFConvBertForMaskedLM(config=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = { 'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids, } lowerCamelCase__ = model(SCREAMING_SNAKE_CASE__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def _UpperCamelCase ( self : int , SCREAMING_SNAKE_CASE__ : Tuple , SCREAMING_SNAKE_CASE__ : Tuple , SCREAMING_SNAKE_CASE__ : Any , SCREAMING_SNAKE_CASE__ : List[Any] , SCREAMING_SNAKE_CASE__ : Dict , SCREAMING_SNAKE_CASE__ : List[Any] , SCREAMING_SNAKE_CASE__ : Optional[Any] ): lowerCamelCase__ = self.num_labels lowerCamelCase__ = TFConvBertForSequenceClassification(config=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = { 'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids, } lowerCamelCase__ = model(SCREAMING_SNAKE_CASE__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def _UpperCamelCase ( self : str , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : Optional[int] , SCREAMING_SNAKE_CASE__ : Tuple , SCREAMING_SNAKE_CASE__ : Any ): lowerCamelCase__ = self.num_choices lowerCamelCase__ = TFConvBertForMultipleChoice(config=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = tf.tile(tf.expand_dims(SCREAMING_SNAKE_CASE__ , 1 ) , (1, self.num_choices, 1) ) lowerCamelCase__ = tf.tile(tf.expand_dims(SCREAMING_SNAKE_CASE__ , 1 ) , (1, self.num_choices, 1) ) lowerCamelCase__ = tf.tile(tf.expand_dims(SCREAMING_SNAKE_CASE__ , 1 ) , (1, self.num_choices, 1) ) lowerCamelCase__ = { 'input_ids': multiple_choice_inputs_ids, 'attention_mask': multiple_choice_input_mask, 'token_type_ids': multiple_choice_token_type_ids, } lowerCamelCase__ = model(SCREAMING_SNAKE_CASE__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def _UpperCamelCase ( self : List[Any] , SCREAMING_SNAKE_CASE__ : Any , SCREAMING_SNAKE_CASE__ : Union[str, Any] , SCREAMING_SNAKE_CASE__ : Any , SCREAMING_SNAKE_CASE__ : Union[str, Any] , SCREAMING_SNAKE_CASE__ : Tuple , SCREAMING_SNAKE_CASE__ : Optional[Any] , SCREAMING_SNAKE_CASE__ : Any ): lowerCamelCase__ = self.num_labels lowerCamelCase__ = TFConvBertForTokenClassification(config=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = { 'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids, } lowerCamelCase__ = model(SCREAMING_SNAKE_CASE__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def _UpperCamelCase ( self : Any , SCREAMING_SNAKE_CASE__ : Any , SCREAMING_SNAKE_CASE__ : Dict , SCREAMING_SNAKE_CASE__ : List[Any] , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : Optional[Any] , SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : List[str] ): lowerCamelCase__ = TFConvBertForQuestionAnswering(config=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = { 'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids, } lowerCamelCase__ = model(SCREAMING_SNAKE_CASE__ ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def _UpperCamelCase ( self : Union[str, Any] ): lowerCamelCase__ = self.prepare_config_and_inputs() ( ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ) = config_and_inputs lowerCamelCase__ = {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask} return config, inputs_dict @require_tf class _a ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , unittest.TestCase ): a_ : Optional[int] = ( ( TFConvBertModel, TFConvBertForMaskedLM, TFConvBertForQuestionAnswering, TFConvBertForSequenceClassification, TFConvBertForTokenClassification, TFConvBertForMultipleChoice, ) if is_tf_available() else () ) a_ : Tuple = ( { 'feature-extraction': TFConvBertModel, 'fill-mask': TFConvBertForMaskedLM, 'question-answering': TFConvBertForQuestionAnswering, 'text-classification': TFConvBertForSequenceClassification, 'token-classification': TFConvBertForTokenClassification, 'zero-shot': TFConvBertForSequenceClassification, } if is_tf_available() else {} ) a_ : Union[str, Any] = False a_ : Dict = False a_ : str = False def _UpperCamelCase ( self : Optional[int] ): lowerCamelCase__ = TFConvBertModelTester(self ) lowerCamelCase__ = ConfigTester(self , config_class=SCREAMING_SNAKE_CASE__ , hidden_size=37 ) def _UpperCamelCase ( self : int ): self.config_tester.run_common_tests() def _UpperCamelCase ( self : Any ): lowerCamelCase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : Optional[Any] ): lowerCamelCase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : str ): lowerCamelCase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_multiple_choice(*SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : List[str] ): lowerCamelCase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : List[str] ): lowerCamelCase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : str ): lowerCamelCase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*SCREAMING_SNAKE_CASE__ ) @slow def _UpperCamelCase ( self : List[str] ): lowerCamelCase__ , lowerCamelCase__ = self.model_tester.prepare_config_and_inputs_for_common() lowerCamelCase__ = True lowerCamelCase__ = True if hasattr(SCREAMING_SNAKE_CASE__ , 'use_cache' ): lowerCamelCase__ = True lowerCamelCase__ = getattr(self.model_tester , 'encoder_seq_length' , self.model_tester.seq_length ) lowerCamelCase__ = getattr(self.model_tester , 'key_length' , SCREAMING_SNAKE_CASE__ ) for model_class in self.all_model_classes: lowerCamelCase__ = self._prepare_for_class(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = model_class(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = len(model(SCREAMING_SNAKE_CASE__ ) ) with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(SCREAMING_SNAKE_CASE__ , saved_model=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = os.path.join(SCREAMING_SNAKE_CASE__ , 'saved_model' , '1' ) lowerCamelCase__ = tf.keras.models.load_model(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = model(SCREAMING_SNAKE_CASE__ ) if self.is_encoder_decoder: lowerCamelCase__ = outputs['encoder_hidden_states'] lowerCamelCase__ = outputs['encoder_attentions'] else: lowerCamelCase__ = outputs['hidden_states'] lowerCamelCase__ = outputs['attentions'] self.assertEqual(len(SCREAMING_SNAKE_CASE__ ) , SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = getattr( self.model_tester , 'expected_num_hidden_layers' , self.model_tester.num_hidden_layers + 1 ) self.assertEqual(len(SCREAMING_SNAKE_CASE__ ) , SCREAMING_SNAKE_CASE__ ) self.assertListEqual( list(output_hidden_states[0].shape[-2:] ) , [self.model_tester.seq_length, self.model_tester.hidden_size] , ) self.assertEqual(len(SCREAMING_SNAKE_CASE__ ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(output_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length] , ) @slow def _UpperCamelCase ( self : List[Any] ): lowerCamelCase__ = TFConvBertModel.from_pretrained('YituTech/conv-bert-base' ) self.assertIsNotNone(SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : List[Any] ): lowerCamelCase__ , lowerCamelCase__ = self.model_tester.prepare_config_and_inputs_for_common() lowerCamelCase__ = True lowerCamelCase__ = getattr(self.model_tester , 'decoder_seq_length' , self.model_tester.seq_length ) lowerCamelCase__ = getattr(self.model_tester , 'encoder_seq_length' , self.model_tester.seq_length ) lowerCamelCase__ = getattr(self.model_tester , 'key_length' , SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = getattr(self.model_tester , 'key_length' , SCREAMING_SNAKE_CASE__ ) def check_decoder_attentions_output(SCREAMING_SNAKE_CASE__ : str ): lowerCamelCase__ = len(SCREAMING_SNAKE_CASE__ ) self.assertEqual(out_len % 2 , 0 ) lowerCamelCase__ = outputs.decoder_attentions self.assertEqual(len(SCREAMING_SNAKE_CASE__ ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(decoder_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads / 2, decoder_seq_length, decoder_key_length] , ) def check_encoder_attentions_output(SCREAMING_SNAKE_CASE__ : Any ): lowerCamelCase__ = [ t.numpy() for t in (outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions) ] self.assertEqual(len(SCREAMING_SNAKE_CASE__ ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length] , ) for model_class in self.all_model_classes: lowerCamelCase__ = True lowerCamelCase__ = False lowerCamelCase__ = model_class(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = model(self._prepare_for_class(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) ) lowerCamelCase__ = len(SCREAMING_SNAKE_CASE__ ) self.assertEqual(config.output_hidden_states , SCREAMING_SNAKE_CASE__ ) check_encoder_attentions_output(SCREAMING_SNAKE_CASE__ ) if self.is_encoder_decoder: lowerCamelCase__ = model_class(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = model(self._prepare_for_class(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) ) self.assertEqual(config.output_hidden_states , SCREAMING_SNAKE_CASE__ ) check_decoder_attentions_output(SCREAMING_SNAKE_CASE__ ) # Check that output attentions can also be changed via the config del inputs_dict["output_attentions"] lowerCamelCase__ = True lowerCamelCase__ = model_class(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = model(self._prepare_for_class(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) ) self.assertEqual(config.output_hidden_states , SCREAMING_SNAKE_CASE__ ) check_encoder_attentions_output(SCREAMING_SNAKE_CASE__ ) # Check attention is always last and order is fine lowerCamelCase__ = True lowerCamelCase__ = True lowerCamelCase__ = model_class(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = model(self._prepare_for_class(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) ) self.assertEqual(out_len + (2 if self.is_encoder_decoder else 1) , len(SCREAMING_SNAKE_CASE__ ) ) self.assertEqual(model.config.output_hidden_states , SCREAMING_SNAKE_CASE__ ) check_encoder_attentions_output(SCREAMING_SNAKE_CASE__ ) @require_tf class _a ( unittest.TestCase ): @slow def _UpperCamelCase ( self : Any ): lowerCamelCase__ = TFConvBertModel.from_pretrained('YituTech/conv-bert-base' ) lowerCamelCase__ = tf.constant([[0, 1, 2, 3, 4, 5]] ) lowerCamelCase__ = model(SCREAMING_SNAKE_CASE__ )[0] lowerCamelCase__ = [1, 6, 7_68] self.assertEqual(output.shape , SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = tf.constant( [ [ [-0.03_47_54_93, -0.4_68_60_34, -0.30_63_88_32], [0.22_63_72_48, -0.26_98_86_46, -0.7_42_34_24], [0.10_32_48_68, -0.45_01_35_08, -0.58_28_07_84], ] ] ) tf.debugging.assert_near(output[:, :3, :3] , SCREAMING_SNAKE_CASE__ , atol=1e-4 )
659
"""simple docstring""" def snake_case ( _a: int = 4000000 )-> int: '''simple docstring''' lowerCamelCase__ = [0, 1] lowerCamelCase__ = 0 while fib[i] <= n: fib.append(fib[i] + fib[i + 1] ) if fib[i + 2] > n: break i += 1 lowerCamelCase__ = 0 for j in range(len(_a ) - 1 ): if fib[j] % 2 == 0: total += fib[j] return total if __name__ == "__main__": print(f"""{solution() = }""")
659
1
"""simple docstring""" import json import os from functools import lru_cache from typing import Dict, List, Optional, Tuple, Union import regex as re from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...tokenization_utils_base import BatchEncoding, EncodedInput from ...utils import PaddingStrategy, logging _snake_case = logging.get_logger(__name__) _snake_case = {"vocab_file": "vocab.json", "merges_file": "merges.txt"} # See all LED models at https://huggingface.co/models?filter=LED _snake_case = { "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 = { "allenai/led-base-16384": 1_6384, } @lru_cache() # Copied from transformers.models.bart.tokenization_bart.bytes_to_unicode def snake_case ( )-> Tuple: '''simple docstring''' lowerCamelCase__ = ( list(range(ord('!' ) , ord('~' ) + 1 ) ) + list(range(ord('¡' ) , ord('¬' ) + 1 ) ) + list(range(ord('®' ) , ord('ÿ' ) + 1 ) ) ) lowerCamelCase__ = bs[:] lowerCamelCase__ = 0 for b in range(2**8 ): if b not in bs: bs.append(_a ) cs.append(2**8 + n ) n += 1 lowerCamelCase__ = [chr(_a ) for n in cs] return dict(zip(_a , _a ) ) def snake_case ( _a: Optional[Any] )-> Optional[int]: '''simple docstring''' lowerCamelCase__ = set() lowerCamelCase__ = word[0] for char in word[1:]: pairs.add((prev_char, char) ) lowerCamelCase__ = char return pairs class _a ( SCREAMING_SNAKE_CASE_ ): a_ : Any = VOCAB_FILES_NAMES a_ : Optional[int] = PRETRAINED_VOCAB_FILES_MAP a_ : Union[str, Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES a_ : Tuple = ['input_ids', 'attention_mask'] def __init__( self : Dict , SCREAMING_SNAKE_CASE__ : Dict , SCREAMING_SNAKE_CASE__ : List[Any] , SCREAMING_SNAKE_CASE__ : int="replace" , SCREAMING_SNAKE_CASE__ : Optional[Any]="<s>" , SCREAMING_SNAKE_CASE__ : Union[str, Any]="</s>" , SCREAMING_SNAKE_CASE__ : List[str]="</s>" , SCREAMING_SNAKE_CASE__ : Union[str, Any]="<s>" , SCREAMING_SNAKE_CASE__ : Dict="<unk>" , SCREAMING_SNAKE_CASE__ : str="<pad>" , SCREAMING_SNAKE_CASE__ : int="<mask>" , SCREAMING_SNAKE_CASE__ : Any=False , **SCREAMING_SNAKE_CASE__ : List[str] , ): lowerCamelCase__ = AddedToken(SCREAMING_SNAKE_CASE__ , lstrip=SCREAMING_SNAKE_CASE__ , rstrip=SCREAMING_SNAKE_CASE__ ) if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) else bos_token lowerCamelCase__ = AddedToken(SCREAMING_SNAKE_CASE__ , lstrip=SCREAMING_SNAKE_CASE__ , rstrip=SCREAMING_SNAKE_CASE__ ) if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) else eos_token lowerCamelCase__ = AddedToken(SCREAMING_SNAKE_CASE__ , lstrip=SCREAMING_SNAKE_CASE__ , rstrip=SCREAMING_SNAKE_CASE__ ) if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) else sep_token lowerCamelCase__ = AddedToken(SCREAMING_SNAKE_CASE__ , lstrip=SCREAMING_SNAKE_CASE__ , rstrip=SCREAMING_SNAKE_CASE__ ) if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) else cls_token lowerCamelCase__ = AddedToken(SCREAMING_SNAKE_CASE__ , lstrip=SCREAMING_SNAKE_CASE__ , rstrip=SCREAMING_SNAKE_CASE__ ) if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) else unk_token lowerCamelCase__ = AddedToken(SCREAMING_SNAKE_CASE__ , lstrip=SCREAMING_SNAKE_CASE__ , rstrip=SCREAMING_SNAKE_CASE__ ) if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) else pad_token # Mask token behave like a normal word, i.e. include the space before it lowerCamelCase__ = AddedToken(SCREAMING_SNAKE_CASE__ , lstrip=SCREAMING_SNAKE_CASE__ , rstrip=SCREAMING_SNAKE_CASE__ ) if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) else mask_token super().__init__( errors=SCREAMING_SNAKE_CASE__ , bos_token=SCREAMING_SNAKE_CASE__ , eos_token=SCREAMING_SNAKE_CASE__ , unk_token=SCREAMING_SNAKE_CASE__ , sep_token=SCREAMING_SNAKE_CASE__ , cls_token=SCREAMING_SNAKE_CASE__ , pad_token=SCREAMING_SNAKE_CASE__ , mask_token=SCREAMING_SNAKE_CASE__ , add_prefix_space=SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ , ) with open(SCREAMING_SNAKE_CASE__ , encoding='utf-8' ) as vocab_handle: lowerCamelCase__ = json.load(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = {v: k for k, v in self.encoder.items()} lowerCamelCase__ = errors # how to handle errors in decoding lowerCamelCase__ = bytes_to_unicode() lowerCamelCase__ = {v: k for k, v in self.byte_encoder.items()} with open(SCREAMING_SNAKE_CASE__ , encoding='utf-8' ) as merges_handle: lowerCamelCase__ = merges_handle.read().split('\n' )[1:-1] lowerCamelCase__ = [tuple(merge.split() ) for merge in bpe_merges] lowerCamelCase__ = dict(zip(SCREAMING_SNAKE_CASE__ , range(len(SCREAMING_SNAKE_CASE__ ) ) ) ) lowerCamelCase__ = {} lowerCamelCase__ = add_prefix_space # Should have added re.IGNORECASE so BPE merges can happen for capitalized versions of contractions lowerCamelCase__ = re.compile(R'\'s|\'t|\'re|\'ve|\'m|\'ll|\'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+' ) @property # Copied from transformers.models.bart.tokenization_bart.BartTokenizer.vocab_size def _UpperCamelCase ( self : Dict ): return len(self.encoder ) def _UpperCamelCase ( self : Dict ): return dict(self.encoder , **self.added_tokens_encoder ) def _UpperCamelCase ( self : Any , SCREAMING_SNAKE_CASE__ : int ): if token in self.cache: return self.cache[token] lowerCamelCase__ = tuple(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = get_pairs(SCREAMING_SNAKE_CASE__ ) if not pairs: return token while True: lowerCamelCase__ = min(SCREAMING_SNAKE_CASE__ , key=lambda SCREAMING_SNAKE_CASE__ : self.bpe_ranks.get(SCREAMING_SNAKE_CASE__ , float('inf' ) ) ) if bigram not in self.bpe_ranks: break lowerCamelCase__ , lowerCamelCase__ = bigram lowerCamelCase__ = [] lowerCamelCase__ = 0 while i < len(SCREAMING_SNAKE_CASE__ ): try: lowerCamelCase__ = word.index(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) except ValueError: new_word.extend(word[i:] ) break else: new_word.extend(word[i:j] ) lowerCamelCase__ = j if word[i] == first and i < len(SCREAMING_SNAKE_CASE__ ) - 1 and word[i + 1] == second: new_word.append(first + second ) i += 2 else: new_word.append(word[i] ) i += 1 lowerCamelCase__ = tuple(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = new_word if len(SCREAMING_SNAKE_CASE__ ) == 1: break else: lowerCamelCase__ = get_pairs(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = ' '.join(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = word return word def _UpperCamelCase ( self : Tuple , SCREAMING_SNAKE_CASE__ : List[str] ): lowerCamelCase__ = [] for token in re.findall(self.pat , SCREAMING_SNAKE_CASE__ ): lowerCamelCase__ = ''.join( self.byte_encoder[b] for b in token.encode('utf-8' ) ) # Maps all our bytes to unicode strings, avoiding control tokens of the BPE (spaces in our case) bpe_tokens.extend(bpe_token for bpe_token in self.bpe(SCREAMING_SNAKE_CASE__ ).split(' ' ) ) return bpe_tokens def _UpperCamelCase ( self : List[Any] , SCREAMING_SNAKE_CASE__ : Optional[int] ): return self.encoder.get(SCREAMING_SNAKE_CASE__ , self.encoder.get(self.unk_token ) ) def _UpperCamelCase ( self : str , SCREAMING_SNAKE_CASE__ : List[str] ): return self.decoder.get(SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : int , SCREAMING_SNAKE_CASE__ : Union[str, Any] ): lowerCamelCase__ = ''.join(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = bytearray([self.byte_decoder[c] for c in text] ).decode('utf-8' , errors=self.errors ) return text def _UpperCamelCase ( self : List[Any] , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : Optional[str] = None ): if not os.path.isdir(SCREAMING_SNAKE_CASE__ ): logger.error(F'Vocabulary path ({save_directory}) should be a directory' ) return lowerCamelCase__ = os.path.join( SCREAMING_SNAKE_CASE__ , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) lowerCamelCase__ = os.path.join( SCREAMING_SNAKE_CASE__ , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['merges_file'] ) with open(SCREAMING_SNAKE_CASE__ , 'w' , encoding='utf-8' ) as f: f.write(json.dumps(self.encoder , indent=2 , sort_keys=SCREAMING_SNAKE_CASE__ , ensure_ascii=SCREAMING_SNAKE_CASE__ ) + '\n' ) lowerCamelCase__ = 0 with open(SCREAMING_SNAKE_CASE__ , 'w' , encoding='utf-8' ) as writer: writer.write('#version: 0.2\n' ) for bpe_tokens, token_index in sorted(self.bpe_ranks.items() , key=lambda SCREAMING_SNAKE_CASE__ : kv[1] ): if index != token_index: logger.warning( F'Saving vocabulary to {merge_file}: BPE merge indices are not consecutive.' ' Please check that the tokenizer is not corrupted!' ) lowerCamelCase__ = token_index writer.write(' '.join(SCREAMING_SNAKE_CASE__ ) + '\n' ) index += 1 return vocab_file, merge_file def _UpperCamelCase ( self : Any , SCREAMING_SNAKE_CASE__ : List[int] , SCREAMING_SNAKE_CASE__ : Optional[List[int]] = None ): if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] lowerCamelCase__ = [self.cls_token_id] lowerCamelCase__ = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def _UpperCamelCase ( self : Optional[int] , SCREAMING_SNAKE_CASE__ : List[int] , SCREAMING_SNAKE_CASE__ : Optional[List[int]] = None , SCREAMING_SNAKE_CASE__ : bool = False ): if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=SCREAMING_SNAKE_CASE__ , token_ids_a=SCREAMING_SNAKE_CASE__ , already_has_special_tokens=SCREAMING_SNAKE_CASE__ ) if token_ids_a is None: return [1] + ([0] * len(SCREAMING_SNAKE_CASE__ )) + [1] return [1] + ([0] * len(SCREAMING_SNAKE_CASE__ )) + [1, 1] + ([0] * len(SCREAMING_SNAKE_CASE__ )) + [1] def _UpperCamelCase ( self : Dict , SCREAMING_SNAKE_CASE__ : List[int] , SCREAMING_SNAKE_CASE__ : Optional[List[int]] = None ): lowerCamelCase__ = [self.sep_token_id] lowerCamelCase__ = [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 _UpperCamelCase ( self : List[str] , SCREAMING_SNAKE_CASE__ : Optional[int] , SCREAMING_SNAKE_CASE__ : List[str]=False , **SCREAMING_SNAKE_CASE__ : Any ): lowerCamelCase__ = kwargs.pop('add_prefix_space' , self.add_prefix_space ) if (is_split_into_words or add_prefix_space) and (len(SCREAMING_SNAKE_CASE__ ) > 0 and not text[0].isspace()): lowerCamelCase__ = ' ' + text return (text, kwargs) def _UpperCamelCase ( self : int , SCREAMING_SNAKE_CASE__ : Union[Dict[str, EncodedInput], BatchEncoding] , SCREAMING_SNAKE_CASE__ : Optional[int] = None , SCREAMING_SNAKE_CASE__ : PaddingStrategy = PaddingStrategy.DO_NOT_PAD , SCREAMING_SNAKE_CASE__ : Optional[int] = None , SCREAMING_SNAKE_CASE__ : Optional[bool] = None , ): lowerCamelCase__ = super()._pad( encoded_inputs=SCREAMING_SNAKE_CASE__ , max_length=SCREAMING_SNAKE_CASE__ , padding_strategy=SCREAMING_SNAKE_CASE__ , pad_to_multiple_of=SCREAMING_SNAKE_CASE__ , return_attention_mask=SCREAMING_SNAKE_CASE__ , ) # Load from model defaults if return_attention_mask is None: lowerCamelCase__ = 'attention_mask' in self.model_input_names if return_attention_mask and "global_attention_mask" in encoded_inputs: lowerCamelCase__ = encoded_inputs[self.model_input_names[0]] # `global_attention_mask` need to have the same length as other (sequential) inputs. lowerCamelCase__ = len(encoded_inputs['global_attention_mask'] ) != len(SCREAMING_SNAKE_CASE__ ) if needs_to_be_padded: lowerCamelCase__ = len(SCREAMING_SNAKE_CASE__ ) - 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` lowerCamelCase__ = ( encoded_inputs['global_attention_mask'] + [-1] * difference ) elif self.padding_side == "left": lowerCamelCase__ = [-1] * difference + encoded_inputs[ 'global_attention_mask' ] else: raise ValueError('Invalid padding strategy:' + str(self.padding_side ) ) return encoded_inputs
659
"""simple docstring""" def snake_case ( _a: List[Any] , _a: Any , _a: str , _a: List[Any] )-> List[Any]: '''simple docstring''' lowerCamelCase__ = [False] * len(_a ) lowerCamelCase__ = [] queue.append(_a ) lowerCamelCase__ = True while queue: lowerCamelCase__ = queue.pop(0 ) for ind in range(len(graph[u] ) ): if visited[ind] is False and graph[u][ind] > 0: queue.append(_a ) lowerCamelCase__ = True lowerCamelCase__ = u return visited[t] def snake_case ( _a: List[Any] , _a: str , _a: List[str] )-> Optional[int]: '''simple docstring''' lowerCamelCase__ = [-1] * (len(_a )) lowerCamelCase__ = 0 while bfs(_a , _a , _a , _a ): lowerCamelCase__ = float('Inf' ) lowerCamelCase__ = sink while s != source: # Find the minimum value in select path lowerCamelCase__ = min(_a , graph[parent[s]][s] ) lowerCamelCase__ = parent[s] max_flow += path_flow lowerCamelCase__ = sink while v != source: lowerCamelCase__ = parent[v] graph[u][v] -= path_flow graph[v][u] += path_flow lowerCamelCase__ = parent[v] return max_flow _snake_case = [ [0, 16, 13, 0, 0, 0], [0, 0, 10, 12, 0, 0], [0, 4, 0, 0, 14, 0], [0, 0, 9, 0, 0, 20], [0, 0, 0, 7, 0, 4], [0, 0, 0, 0, 0, 0], ] _snake_case , _snake_case = 0, 5 print(ford_fulkerson(graph, source, sink))
659
1
"""simple docstring""" from __future__ import annotations import random # Maximum size of the population. Bigger could be faster but is more memory expensive. _snake_case = 200 # Number of elements selected in every generation of evolution. The selection takes # place from best to worst of that generation and must be smaller than N_POPULATION. _snake_case = 50 # Probability that an element of a generation can mutate, changing one of its genes. # This will guarantee that all genes will be used during evolution. _snake_case = 0.4 # Just a seed to improve randomness required by the algorithm. random.seed(random.randint(0, 1000)) def snake_case ( _a: str , _a: str )-> tuple[str, float]: '''simple docstring''' lowerCamelCase__ = len([g for position, g in enumerate(_a ) if g == main_target[position]] ) return (item, float(_a )) def snake_case ( _a: str , _a: str )-> tuple[str, str]: '''simple docstring''' lowerCamelCase__ = random.randint(0 , len(_a ) - 1 ) lowerCamelCase__ = parent_a[:random_slice] + parent_a[random_slice:] lowerCamelCase__ = parent_a[:random_slice] + parent_a[random_slice:] return (child_a, child_a) def snake_case ( _a: str , _a: list[str] )-> str: '''simple docstring''' lowerCamelCase__ = list(_a ) if random.uniform(0 , 1 ) < MUTATION_PROBABILITY: lowerCamelCase__ = random.choice(_a ) return "".join(_a ) def snake_case ( _a: tuple[str, float] , _a: list[tuple[str, float]] , _a: list[str] , )-> list[str]: '''simple docstring''' lowerCamelCase__ = [] # Generate more children proportionally to the fitness score. lowerCamelCase__ = int(parent_a[1] * 100 ) + 1 lowerCamelCase__ = 10 if child_n >= 10 else child_n for _ in range(_a ): lowerCamelCase__ = population_score[random.randint(0 , _a )][0] lowerCamelCase__ , lowerCamelCase__ = crossover(parent_a[0] , _a ) # Append new string to the population list. pop.append(mutate(_a , _a ) ) pop.append(mutate(_a , _a ) ) return pop def snake_case ( _a: str , _a: list[str] , _a: bool = True )-> tuple[int, int, str]: '''simple docstring''' if N_POPULATION < N_SELECTED: lowerCamelCase__ = F'{N_POPULATION} must be bigger than {N_SELECTED}' raise ValueError(_a ) # Verify that the target contains no genes besides the ones inside genes variable. lowerCamelCase__ = sorted({c for c in target if c not in genes} ) if not_in_genes_list: lowerCamelCase__ = F'{not_in_genes_list} is not in genes list, evolution cannot converge' raise ValueError(_a ) # Generate random starting population. lowerCamelCase__ = [] for _ in range(_a ): population.append(''.join([random.choice(_a ) for i in range(len(_a ) )] ) ) # Just some logs to know what the algorithms is doing. lowerCamelCase__ , lowerCamelCase__ = 0, 0 # This loop will end when we find a perfect match for our target. while True: generation += 1 total_population += len(_a ) # Random population created. Now it's time to evaluate. # Adding a bit of concurrency can make everything faster, # # import concurrent.futures # population_score: list[tuple[str, float]] = [] # with concurrent.futures.ThreadPoolExecutor( # max_workers=NUM_WORKERS) as executor: # futures = {executor.submit(evaluate, item) for item in population} # concurrent.futures.wait(futures) # population_score = [item.result() for item in futures] # # but with a simple algorithm like this, it will probably be slower. # We just need to call evaluate for every item inside the population. lowerCamelCase__ = [evaluate(_a , _a ) for item in population] # Check if there is a matching evolution. lowerCamelCase__ = sorted(_a , key=lambda _a : x[1] , reverse=_a ) if population_score[0][0] == target: return (generation, total_population, population_score[0][0]) # Print the best result every 10 generation. # Just to know that the algorithm is working. if debug and generation % 10 == 0: print( F'\nGeneration: {generation}' F'\nTotal Population:{total_population}' F'\nBest score: {population_score[0][1]}' F'\nBest string: {population_score[0][0]}' ) # Flush the old population, keeping some of the best evolutions. # Keeping this avoid regression of evolution. lowerCamelCase__ = population[: int(N_POPULATION / 3 )] population.clear() population.extend(_a ) # Normalize population score to be between 0 and 1. lowerCamelCase__ = [ (item, score / len(_a )) for item, score in population_score ] # This is selection for i in range(_a ): population.extend(select(population_score[int(_a )] , _a , _a ) ) # Check if the population has already reached the maximum value and if so, # break the cycle. If this check is disabled, the algorithm will take # forever to compute large strings, but will also calculate small strings in # a far fewer generations. if len(_a ) > N_POPULATION: break if __name__ == "__main__": _snake_case = ( "This is a genetic algorithm to evaluate, combine, evolve, and mutate a string!" ) _snake_case = list( " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklm" "nopqrstuvwxyz.,;!?+-*#@^'èéòà€ù=)(&%$£/\\" ) _snake_case , _snake_case , _snake_case = basic(target_str, genes_list) print( f"""\nGeneration: {generation}\nTotal Population: {population}\nTarget: {target}""" )
659
"""simple docstring""" import json import multiprocessing import os import re from collections import defaultdict import torch from accelerate import Accelerator from accelerate.utils import set_seed from arguments import HumanEvalArguments from datasets import load_dataset, load_metric from torch.utils.data import IterableDataset from torch.utils.data.dataloader import DataLoader from tqdm import tqdm import transformers from transformers import AutoModelForCausalLM, AutoTokenizer, HfArgumentParser, StoppingCriteria, StoppingCriteriaList _snake_case = ["\nclass", "\ndef", "\n#", "\n@", "\nprint", "\nif"] class _a ( SCREAMING_SNAKE_CASE_ ): def __init__( self : List[str] , SCREAMING_SNAKE_CASE__ : Any , SCREAMING_SNAKE_CASE__ : List[Any] , SCREAMING_SNAKE_CASE__ : int=None , SCREAMING_SNAKE_CASE__ : Optional[int]=1 ): lowerCamelCase__ = tokenizer lowerCamelCase__ = dataset lowerCamelCase__ = len(SCREAMING_SNAKE_CASE__ ) if n_tasks is None else n_tasks lowerCamelCase__ = n_copies def __iter__( self : Any ): lowerCamelCase__ = [] for task in range(self.n_tasks ): # without strip, the model generate commented codes ... prompts.append(self.tokenizer.eos_token + self.dataset[task]['prompt'].strip() ) lowerCamelCase__ = self.tokenizer(SCREAMING_SNAKE_CASE__ , padding=SCREAMING_SNAKE_CASE__ , return_tensors='pt' ) for task in range(self.n_tasks ): for _ in range(self.n_copies ): yield { "ids": outputs.input_ids[task], "task_id": task, "input_len": outputs.attention_mask[task].sum(), } class _a ( SCREAMING_SNAKE_CASE_ ): def __init__( self : Dict , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : List[Any] , SCREAMING_SNAKE_CASE__ : Dict ): lowerCamelCase__ = start_length lowerCamelCase__ = eof_strings lowerCamelCase__ = tokenizer def __call__( self : Any , SCREAMING_SNAKE_CASE__ : Any , SCREAMING_SNAKE_CASE__ : List[str] , **SCREAMING_SNAKE_CASE__ : Dict ): lowerCamelCase__ = self.tokenizer.batch_decode(input_ids[:, self.start_length :] ) lowerCamelCase__ = [] for decoded_generation in decoded_generations: done.append(any(stop_string in decoded_generation for stop_string in self.eof_strings ) ) return all(SCREAMING_SNAKE_CASE__ ) def snake_case ( _a: List[Any] )-> Dict: '''simple docstring''' lowerCamelCase__ = re.split('(%s)' % '|'.join(_a ) , _a ) # last string should be "" return "".join(string_list[:-2] ) def snake_case ( _a: List[Any] , _a: Optional[int] , _a: str , _a: Union[str, Any] , _a: Dict , _a: Optional[int]=20 , **_a: Optional[int] )-> List[str]: '''simple docstring''' lowerCamelCase__ = defaultdict(_a ) # dict of list of generated tokens for step, batch in tqdm(enumerate(_a ) ): with torch.no_grad(): lowerCamelCase__ = batch['ids'].shape[-1] lowerCamelCase__ = accelerator.unwrap_model(_a ).generate( input_ids=batch['ids'][:, : batch['input_len']] , num_return_sequences=_a , **_a ) # each task is generated batch_size times lowerCamelCase__ = batch['task_id'].repeat(_a ) lowerCamelCase__ = accelerator.pad_across_processes( _a , dim=1 , pad_index=tokenizer.pad_token_id ) lowerCamelCase__ , lowerCamelCase__ = accelerator.gather((generated_tokens, generated_tasks) ) lowerCamelCase__ = generated_tokens.cpu().numpy() lowerCamelCase__ = generated_tasks.cpu().numpy() for task, generated_tokens in zip(_a , _a ): gen_token_dict[task].append(_a ) lowerCamelCase__ = [[] for _ in range(_a )] for task, generated_tokens in gen_token_dict.items(): for s in generated_tokens: lowerCamelCase__ = tokenizer.decode(_a , skip_special_tokens=_a , clean_up_tokenization_spaces=_a ) code_gens[task].append(remove_last_block(_a ) ) return code_gens def snake_case ( )-> Union[str, Any]: '''simple docstring''' lowerCamelCase__ = HfArgumentParser(_a ) lowerCamelCase__ = parser.parse_args() transformers.logging.set_verbosity_error() # enables code execution in code_eval metric lowerCamelCase__ = args.HF_ALLOW_CODE_EVAL # make sure tokenizer plays nice with multiprocessing lowerCamelCase__ = 'false' if args.num_workers is None: lowerCamelCase__ = multiprocessing.cpu_count() # Use dataset load to feed to accelerate lowerCamelCase__ = Accelerator() set_seed(args.seed , device_specific=_a ) # Load model and tokenizer lowerCamelCase__ = AutoTokenizer.from_pretrained(args.model_ckpt ) lowerCamelCase__ = tokenizer.eos_token lowerCamelCase__ = AutoModelForCausalLM.from_pretrained(args.model_ckpt ) # Generation settings lowerCamelCase__ = { 'do_sample': args.do_sample, 'temperature': args.temperature, 'max_new_tokens': args.max_new_tokens, 'top_p': args.top_p, 'top_k': args.top_k, 'stopping_criteria': StoppingCriteriaList([EndOfFunctionCriteria(0 , _a , _a )] ), } # Load evaluation dataset and metric lowerCamelCase__ = load_dataset('openai_humaneval' ) lowerCamelCase__ = load_metric('code_eval' ) lowerCamelCase__ = args.num_tasks if args.num_tasks is not None else len(human_eval['test'] ) lowerCamelCase__ = args.n_samples // args.batch_size lowerCamelCase__ = TokenizedDataset(_a , human_eval['test'] , n_copies=_a , n_tasks=_a ) # do not confuse args.batch_size, which is actually the num_return_sequences lowerCamelCase__ = DataLoader(_a , batch_size=1 ) # Run a quick test to see if code evaluation is enabled try: lowerCamelCase__ = code_eval_metric.compute(references=[''] , predictions=[['']] ) except ValueError as exception: print( 'Code evaluation not enabled. Read the warning below carefully and then use `--HF_ALLOW_CODE_EVAL="1"`' ' flag to enable code evaluation.' ) raise exception lowerCamelCase__ , lowerCamelCase__ = accelerator.prepare(_a , _a ) lowerCamelCase__ = complete_code( _a , _a , _a , _a , n_tasks=_a , batch_size=args.batch_size , **_a , ) if accelerator.is_main_process: lowerCamelCase__ = [] for task in tqdm(range(_a ) ): lowerCamelCase__ = human_eval['test'][task]['test'] lowerCamelCase__ = F'check({human_eval["test"][task]["entry_point"]})' references.append('\n' + test_func + '\n' + entry_point ) # Evaluate completions with "code_eval" metric lowerCamelCase__ , lowerCamelCase__ = code_eval_metric.compute( references=_a , predictions=_a , num_workers=args.num_workers ) print(F'Results: {pass_at_k}' ) # Save results to json file with open(args.output_file , 'w' ) as fp: json.dump(_a , _a ) # For some reason the folliwng seems to be necessary sometimes for code_eval to work nice with multiprocessing # https://stackoverflow.com/questions/60804599/python-multiprocessing-keeps-spawning-the-whole-script if __name__ == "__main__": main()
659
1
"""simple docstring""" from collections import defaultdict from pathlib import Path import pandas as pd from rouge_cli import calculate_rouge_path from utils import calculate_rouge _snake_case = [ "Prosecutor: \"No videos were used in the crash investigation\" German papers say they saw a cell phone video of the" " final seconds on board Flight 9525. The Germanwings co-pilot says he had a \"previous episode of severe" " depression\" German airline confirms it knew of Andreas Lubitz's depression years before he took control.", "The Palestinian Authority officially becomes the 123rd member of the International Criminal Court. The formal" " accession was marked with a ceremony at The Hague, in the Netherlands. The Palestinians signed the ICC's" " founding Rome Statute in January. Israel and the United States opposed the Palestinians' efforts to join the" " body.", "Amnesty International releases its annual report on the death penalty. The report catalogs the use of" " state-sanctioned killing as a punitive measure across the globe. At least 607 people were executed around the" " world in 2014, compared to 778 in 2013. The U.S. remains one of the worst offenders for imposing capital" " punishment.", ] _snake_case = [ "Marseille prosecutor says \"so far no videos were used in the crash investigation\" despite media reports ." " Journalists at Bild and Paris Match are \"very confident\" the video clip is real, an editor says . Andreas Lubitz" " had informed his Lufthansa training school of an episode of severe depression, airline says .", "Membership gives the ICC jurisdiction over alleged crimes committed in Palestinian territories since last June ." " Israel and the United States opposed the move, which could open the door to war crimes investigations against" " Israelis .", "Amnesty's annual death penalty report catalogs encouraging signs, but setbacks in numbers of those sentenced to" " death . Organization claims that governments around the world are using the threat of terrorism to advance" " executions . The number of executions worldwide has gone down by almost 22% compared with 2013, but death" " sentences up by 28% .", ] def snake_case ( )-> List[Any]: '''simple docstring''' lowerCamelCase__ = calculate_rouge(_a , _a , bootstrap_aggregation=_a , rouge_keys=['rouge2', 'rougeL'] ) assert isinstance(_a , _a ) lowerCamelCase__ = calculate_rouge(_a , _a , bootstrap_aggregation=_a , rouge_keys=['rouge2'] ) assert ( pd.DataFrame(no_aggregation['rouge2'] ).fmeasure.mean() == pd.DataFrame(no_aggregation_just_ra['rouge2'] ).fmeasure.mean() ) def snake_case ( )-> Union[str, Any]: '''simple docstring''' lowerCamelCase__ = 'rougeLsum' lowerCamelCase__ = calculate_rouge(_a , _a , newline_sep=_a , rouge_keys=[k] )[k] lowerCamelCase__ = calculate_rouge(_a , _a , newline_sep=_a , rouge_keys=[k] )[k] assert score > score_no_sep def snake_case ( )-> Optional[int]: '''simple docstring''' lowerCamelCase__ = ['rouge1', 'rouge2', 'rougeL'] lowerCamelCase__ = calculate_rouge(_a , _a , newline_sep=_a , rouge_keys=_a ) lowerCamelCase__ = calculate_rouge(_a , _a , newline_sep=_a , rouge_keys=_a ) assert score_sep == score_no_sep def snake_case ( )-> Dict: '''simple docstring''' lowerCamelCase__ = [ 'Her older sister, Margot Frank, died in 1945, a month earlier than previously thought.', 'Marseille prosecutor says "so far no videos were used in the crash investigation" despite media reports .', ] lowerCamelCase__ = [ 'Margot Frank, died in 1945, a month earlier than previously thought.', 'Prosecutor: "No videos were used in the crash investigation" German papers say they saw a cell phone video of' ' the final seconds on board Flight 9525.', ] assert calculate_rouge(_a , _a , newline_sep=_a ) == calculate_rouge(_a , _a , newline_sep=_a ) def snake_case ( )-> Optional[int]: '''simple docstring''' lowerCamelCase__ = [ '" "a person who has such a video needs to immediately give it to the investigators," prosecutor says .<n> "it is a very disturbing scene," editor-in-chief of bild online tells "erin burnett: outfront" ' ] lowerCamelCase__ = [ ' Marseille prosecutor says "so far no videos were used in the crash investigation" despite media reports . Journalists at Bild and Paris Match are "very confident" the video clip is real, an editor says . Andreas Lubitz had informed his Lufthansa training school of an episode of severe depression, airline says .' ] lowerCamelCase__ = calculate_rouge(_a , _a , rouge_keys=['rougeLsum'] , newline_sep=_a )['rougeLsum'] lowerCamelCase__ = calculate_rouge(_a , _a , rouge_keys=['rougeLsum'] )['rougeLsum'] assert new_score > prev_score def snake_case ( )-> str: '''simple docstring''' lowerCamelCase__ = Path('examples/seq2seq/test_data/wmt_en_ro' ) lowerCamelCase__ = calculate_rouge_path(data_dir.joinpath('test.source' ) , data_dir.joinpath('test.target' ) ) assert isinstance(_a , _a ) lowerCamelCase__ = calculate_rouge_path( data_dir.joinpath('test.source' ) , data_dir.joinpath('test.target' ) , bootstrap_aggregation=_a ) assert isinstance(_a , _a )
659
"""simple docstring""" import argparse import json from tqdm import tqdm def snake_case ( )-> List[Any]: '''simple docstring''' lowerCamelCase__ = argparse.ArgumentParser() # Required parameters parser.add_argument( '--src_path' , type=_a , default='biencoder-nq-dev.json' , help='Path to raw DPR training data' , ) parser.add_argument( '--evaluation_set' , type=_a , help='where to store parsed evaluation_set file' , ) parser.add_argument( '--gold_data_path' , type=_a , help='where to store parsed gold_data_path file' , ) lowerCamelCase__ = parser.parse_args() with open(args.src_path , 'r' ) as src_file, open(args.evaluation_set , 'w' ) as eval_file, open( args.gold_data_path , 'w' ) as gold_file: lowerCamelCase__ = json.load(_a ) for dpr_record in tqdm(_a ): lowerCamelCase__ = dpr_record['question'] lowerCamelCase__ = [context['title'] for context in dpr_record['positive_ctxs']] eval_file.write(question + '\n' ) gold_file.write('\t'.join(_a ) + '\n' ) if __name__ == "__main__": main()
659
1
"""simple docstring""" def snake_case ( _a: int )-> Optional[Any]: '''simple docstring''' lowerCamelCase__ = 1 lowerCamelCase__ = 2 while i * i <= n: lowerCamelCase__ = 0 while n % i == 0: n //= i multiplicity += 1 n_divisors *= multiplicity + 1 i += 1 if n > 1: n_divisors *= 2 return n_divisors def snake_case ( )-> int: '''simple docstring''' lowerCamelCase__ = 1 lowerCamelCase__ = 1 while True: i += 1 t_num += i if count_divisors(_a ) > 500: break return t_num if __name__ == "__main__": print(solution())
659
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) _snake_case = { "configuration_blenderbot": [ "BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP", "BlenderbotConfig", "BlenderbotOnnxConfig", ], "tokenization_blenderbot": ["BlenderbotTokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = ["BlenderbotTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = [ "BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST", "BlenderbotForCausalLM", "BlenderbotForConditionalGeneration", "BlenderbotModel", "BlenderbotPreTrainedModel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = [ "TFBlenderbotForConditionalGeneration", "TFBlenderbotModel", "TFBlenderbotPreTrainedModel", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = [ "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 _snake_case = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
659
1
"""simple docstring""" _snake_case = [ 999, 800, 799, 600, 599, 500, 400, 399, 377, 355, 333, 311, 288, 266, 244, 222, 200, 199, 177, 155, 133, 111, 88, 66, 44, 22, 0, ] _snake_case = [ 999, 976, 952, 928, 905, 882, 858, 857, 810, 762, 715, 714, 572, 429, 428, 286, 285, 238, 190, 143, 142, 118, 95, 71, 47, 24, 0, ] _snake_case = [ 999, 988, 977, 966, 955, 944, 933, 922, 911, 900, 899, 879, 859, 840, 820, 800, 799, 766, 733, 700, 699, 650, 600, 599, 500, 499, 400, 399, 350, 300, 299, 266, 233, 200, 199, 179, 159, 140, 120, 100, 99, 88, 77, 66, 55, 44, 33, 22, 11, 0, ] _snake_case = [ 999, 995, 992, 989, 985, 981, 978, 975, 971, 967, 964, 961, 957, 956, 951, 947, 942, 937, 933, 928, 923, 919, 914, 913, 908, 903, 897, 892, 887, 881, 876, 871, 870, 864, 858, 852, 846, 840, 834, 828, 827, 820, 813, 806, 799, 792, 785, 784, 777, 770, 763, 756, 749, 742, 741, 733, 724, 716, 707, 699, 698, 688, 677, 666, 656, 655, 645, 634, 623, 613, 612, 598, 584, 570, 569, 555, 541, 527, 526, 505, 484, 483, 462, 440, 439, 396, 395, 352, 351, 308, 307, 264, 263, 220, 219, 176, 132, 88, 44, 0, ] _snake_case = [ 999, 997, 995, 992, 990, 988, 986, 984, 981, 979, 977, 975, 972, 970, 968, 966, 964, 961, 959, 957, 956, 954, 951, 949, 946, 944, 941, 939, 936, 934, 931, 929, 926, 924, 921, 919, 916, 914, 913, 910, 907, 905, 902, 899, 896, 893, 891, 888, 885, 882, 879, 877, 874, 871, 870, 867, 864, 861, 858, 855, 852, 849, 846, 843, 840, 837, 834, 831, 828, 827, 824, 821, 817, 814, 811, 808, 804, 801, 798, 795, 791, 788, 785, 784, 780, 777, 774, 770, 766, 763, 760, 756, 752, 749, 746, 742, 741, 737, 733, 730, 726, 722, 718, 714, 710, 707, 703, 699, 698, 694, 690, 685, 681, 677, 673, 669, 664, 660, 656, 655, 650, 646, 641, 636, 632, 627, 622, 618, 613, 612, 607, 602, 596, 591, 586, 580, 575, 570, 569, 563, 557, 551, 545, 539, 533, 527, 526, 519, 512, 505, 498, 491, 484, 483, 474, 466, 457, 449, 440, 439, 428, 418, 407, 396, 395, 381, 366, 352, 351, 330, 308, 307, 286, 264, 263, 242, 220, 219, 176, 175, 132, 131, 88, 44, 0, ] _snake_case = [ 999, 991, 982, 974, 966, 958, 950, 941, 933, 925, 916, 908, 900, 899, 874, 850, 825, 800, 799, 700, 600, 500, 400, 300, 200, 100, 0, ] _snake_case = [ 999, 992, 985, 978, 971, 964, 957, 949, 942, 935, 928, 921, 914, 907, 900, 899, 879, 859, 840, 820, 800, 799, 766, 733, 700, 699, 650, 600, 599, 500, 499, 400, 399, 300, 299, 200, 199, 100, 99, 0, ] _snake_case = [ 999, 996, 992, 989, 985, 982, 979, 975, 972, 968, 965, 961, 958, 955, 951, 948, 944, 941, 938, 934, 931, 927, 924, 920, 917, 914, 910, 907, 903, 900, 899, 891, 884, 876, 869, 861, 853, 846, 838, 830, 823, 815, 808, 800, 799, 788, 777, 766, 755, 744, 733, 722, 711, 700, 699, 688, 677, 666, 655, 644, 633, 622, 611, 600, 599, 585, 571, 557, 542, 528, 514, 500, 499, 485, 471, 457, 442, 428, 414, 400, 399, 379, 359, 340, 320, 300, 299, 279, 259, 240, 220, 200, 199, 166, 133, 100, 99, 66, 33, 0, ]
659
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging _snake_case = logging.get_logger(__name__) _snake_case = { "microsoft/swinv2-tiny-patch4-window8-256": ( "https://huggingface.co/microsoft/swinv2-tiny-patch4-window8-256/resolve/main/config.json" ), } class _a ( SCREAMING_SNAKE_CASE_ ): a_ : Union[str, Any] = 'swinv2' a_ : Optional[int] = { 'num_attention_heads': 'num_heads', 'num_hidden_layers': 'num_layers', } def __init__( self : List[str] , SCREAMING_SNAKE_CASE__ : int=2_24 , SCREAMING_SNAKE_CASE__ : str=4 , SCREAMING_SNAKE_CASE__ : List[Any]=3 , SCREAMING_SNAKE_CASE__ : str=96 , SCREAMING_SNAKE_CASE__ : Dict=[2, 2, 6, 2] , SCREAMING_SNAKE_CASE__ : Union[str, Any]=[3, 6, 12, 24] , SCREAMING_SNAKE_CASE__ : Tuple=7 , SCREAMING_SNAKE_CASE__ : Union[str, Any]=4.0 , SCREAMING_SNAKE_CASE__ : List[str]=True , SCREAMING_SNAKE_CASE__ : List[Any]=0.0 , SCREAMING_SNAKE_CASE__ : Optional[int]=0.0 , SCREAMING_SNAKE_CASE__ : Dict=0.1 , SCREAMING_SNAKE_CASE__ : Tuple="gelu" , SCREAMING_SNAKE_CASE__ : Tuple=False , SCREAMING_SNAKE_CASE__ : Any=0.02 , SCREAMING_SNAKE_CASE__ : List[Any]=1e-5 , SCREAMING_SNAKE_CASE__ : int=32 , **SCREAMING_SNAKE_CASE__ : List[str] , ): super().__init__(**SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = image_size lowerCamelCase__ = patch_size lowerCamelCase__ = num_channels lowerCamelCase__ = embed_dim lowerCamelCase__ = depths lowerCamelCase__ = len(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = num_heads lowerCamelCase__ = window_size lowerCamelCase__ = mlp_ratio lowerCamelCase__ = qkv_bias lowerCamelCase__ = hidden_dropout_prob lowerCamelCase__ = attention_probs_dropout_prob lowerCamelCase__ = drop_path_rate lowerCamelCase__ = hidden_act lowerCamelCase__ = use_absolute_embeddings lowerCamelCase__ = layer_norm_eps lowerCamelCase__ = initializer_range lowerCamelCase__ = encoder_stride # we set the hidden_size attribute in order to make Swinv2 work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model lowerCamelCase__ = int(embed_dim * 2 ** (len(SCREAMING_SNAKE_CASE__ ) - 1) ) lowerCamelCase__ = (0, 0, 0, 0)
659
1
"""simple docstring""" import warnings from ...utils import logging from .image_processing_dpt import DPTImageProcessor _snake_case = logging.get_logger(__name__) class _a ( SCREAMING_SNAKE_CASE_ ): def __init__( self : Tuple , *SCREAMING_SNAKE_CASE__ : str , **SCREAMING_SNAKE_CASE__ : Any ): warnings.warn( 'The class DPTFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please' ' use DPTImageProcessor instead.' , SCREAMING_SNAKE_CASE__ , ) super().__init__(*SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ )
659
"""simple docstring""" def snake_case ( _a: int )-> int: '''simple docstring''' lowerCamelCase__ = 1 for i in range(1 , num + 1 ): fact *= i return fact def snake_case ( _a: int )-> int: '''simple docstring''' lowerCamelCase__ = 0 while number > 0: lowerCamelCase__ = number % 10 sum_of_digits += last_digit lowerCamelCase__ = number // 10 # Removing the last_digit from the given number return sum_of_digits def snake_case ( _a: int = 100 )-> int: '''simple docstring''' lowerCamelCase__ = factorial(_a ) lowerCamelCase__ = split_and_add(_a ) return result if __name__ == "__main__": print(solution(int(input("Enter the Number: ").strip())))
659
1
"""simple docstring""" from typing import List, Optional, Union import numpy as np from ....audio_utils import mel_filter_bank, optimal_fft_length, spectrogram, window_function from ....feature_extraction_sequence_utils import SequenceFeatureExtractor from ....feature_extraction_utils import BatchFeature from ....file_utils import PaddingStrategy, TensorType from ....utils import logging _snake_case = logging.get_logger(__name__) class _a ( SCREAMING_SNAKE_CASE_ ): a_ : Tuple = ['input_features', 'attention_mask'] def __init__( self : str , SCREAMING_SNAKE_CASE__ : Tuple=80 , SCREAMING_SNAKE_CASE__ : Optional[int]=1_60_00 , SCREAMING_SNAKE_CASE__ : List[str]=0.0 , SCREAMING_SNAKE_CASE__ : Tuple=10 , SCREAMING_SNAKE_CASE__ : int=25 , SCREAMING_SNAKE_CASE__ : str="hamming_window" , SCREAMING_SNAKE_CASE__ : List[str]=3_27_68.0 , SCREAMING_SNAKE_CASE__ : List[str]=0.97 , SCREAMING_SNAKE_CASE__ : int=1.0 , SCREAMING_SNAKE_CASE__ : Optional[int]=True , SCREAMING_SNAKE_CASE__ : List[Any]=True , SCREAMING_SNAKE_CASE__ : Optional[int]=False , **SCREAMING_SNAKE_CASE__ : List[str] , ): super().__init__(feature_size=SCREAMING_SNAKE_CASE__ , sampling_rate=SCREAMING_SNAKE_CASE__ , padding_value=SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = feature_size lowerCamelCase__ = sampling_rate lowerCamelCase__ = padding_value lowerCamelCase__ = hop_length lowerCamelCase__ = win_length lowerCamelCase__ = frame_signal_scale lowerCamelCase__ = preemphasis_coeff lowerCamelCase__ = mel_floor lowerCamelCase__ = normalize_means lowerCamelCase__ = normalize_vars lowerCamelCase__ = win_function lowerCamelCase__ = return_attention_mask lowerCamelCase__ = win_length * sampling_rate // 10_00 lowerCamelCase__ = hop_length * sampling_rate // 10_00 lowerCamelCase__ = optimal_fft_length(self.sample_size ) lowerCamelCase__ = (self.n_fft // 2) + 1 def _UpperCamelCase ( self : List[str] , SCREAMING_SNAKE_CASE__ : np.array ): if self.win_function == "hamming_window": lowerCamelCase__ = window_function(window_length=self.sample_size , name=self.win_function , periodic=SCREAMING_SNAKE_CASE__ ) else: lowerCamelCase__ = window_function(window_length=self.sample_size , name=self.win_function ) lowerCamelCase__ = mel_filter_bank( num_frequency_bins=self.n_freqs , num_mel_filters=self.feature_size , min_frequency=0.0 , max_frequency=self.sampling_rate / 2.0 , sampling_rate=self.sampling_rate , ) lowerCamelCase__ = spectrogram( one_waveform * self.frame_signal_scale , window=SCREAMING_SNAKE_CASE__ , frame_length=self.sample_size , hop_length=self.sample_stride , fft_length=self.n_fft , center=SCREAMING_SNAKE_CASE__ , preemphasis=self.preemphasis_coeff , mel_filters=SCREAMING_SNAKE_CASE__ , mel_floor=self.mel_floor , log_mel='log' , ) return msfc_features.T def _UpperCamelCase ( self : Dict , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : Optional[int] , SCREAMING_SNAKE_CASE__ : Any ): # make sure we normalize float32 arrays if self.normalize_means: lowerCamelCase__ = x[:input_length].mean(axis=0 ) lowerCamelCase__ = np.subtract(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) if self.normalize_vars: lowerCamelCase__ = x[:input_length].std(axis=0 ) lowerCamelCase__ = np.divide(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) if input_length < x.shape[0]: lowerCamelCase__ = padding_value # make sure array is in float32 lowerCamelCase__ = x.astype(np.floataa ) return x def _UpperCamelCase ( self : Any , SCREAMING_SNAKE_CASE__ : List[np.ndarray] , SCREAMING_SNAKE_CASE__ : Optional[np.ndarray] = None ): lowerCamelCase__ = attention_mask.sum(-1 ) if attention_mask is not None else [x.shape[0] for x in input_features] return [self._normalize_one(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , self.padding_value ) for x, n in zip(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ )] def __call__( self : List[str] , SCREAMING_SNAKE_CASE__ : Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]] , SCREAMING_SNAKE_CASE__ : Union[bool, str, PaddingStrategy] = False , SCREAMING_SNAKE_CASE__ : Optional[int] = None , SCREAMING_SNAKE_CASE__ : bool = False , SCREAMING_SNAKE_CASE__ : Optional[int] = None , SCREAMING_SNAKE_CASE__ : Optional[bool] = None , SCREAMING_SNAKE_CASE__ : Optional[Union[str, TensorType]] = None , SCREAMING_SNAKE_CASE__ : Optional[int] = None , **SCREAMING_SNAKE_CASE__ : Any , ): if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( F'The model corresponding to this feature extractor: {self} was trained using a sampling rate of' F' {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled with' F' {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.' ) lowerCamelCase__ = isinstance(SCREAMING_SNAKE_CASE__ , 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}' ) lowerCamelCase__ = is_batched_numpy or ( isinstance(SCREAMING_SNAKE_CASE__ , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: lowerCamelCase__ = [np.asarray(SCREAMING_SNAKE_CASE__ , dtype=np.floataa ) for speech in raw_speech] elif not is_batched and not isinstance(SCREAMING_SNAKE_CASE__ , np.ndarray ): lowerCamelCase__ = np.asarray(SCREAMING_SNAKE_CASE__ , dtype=np.floataa ) elif isinstance(SCREAMING_SNAKE_CASE__ , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): lowerCamelCase__ = raw_speech.astype(np.floataa ) # always return batch if not is_batched: lowerCamelCase__ = [raw_speech] # extract fbank features lowerCamelCase__ = [self._extract_mfsc_features(SCREAMING_SNAKE_CASE__ ) for one_waveform in raw_speech] # convert into correct format for padding lowerCamelCase__ = BatchFeature({'input_features': features} ) lowerCamelCase__ = self.pad( SCREAMING_SNAKE_CASE__ , padding=SCREAMING_SNAKE_CASE__ , max_length=SCREAMING_SNAKE_CASE__ , truncation=SCREAMING_SNAKE_CASE__ , pad_to_multiple_of=SCREAMING_SNAKE_CASE__ , return_attention_mask=SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ , ) # make sure list is in array format lowerCamelCase__ = padded_inputs.get('input_features' ) if isinstance(input_features[0] , SCREAMING_SNAKE_CASE__ ): lowerCamelCase__ = [np.asarray(SCREAMING_SNAKE_CASE__ , dtype=np.floataa ) for feature in input_features] lowerCamelCase__ = padded_inputs.get('attention_mask' ) if attention_mask is not None: lowerCamelCase__ = [np.asarray(SCREAMING_SNAKE_CASE__ , dtype=np.intaa ) for array in attention_mask] if self.normalize_means or self.normalize_vars: lowerCamelCase__ = ( np.array(SCREAMING_SNAKE_CASE__ , dtype=np.intaa ) if self._get_padding_strategies(SCREAMING_SNAKE_CASE__ , max_length=SCREAMING_SNAKE_CASE__ ) is not PaddingStrategy.DO_NOT_PAD and padding else None ) lowerCamelCase__ = self.normalize( padded_inputs['input_features'] , attention_mask=SCREAMING_SNAKE_CASE__ ) if return_tensors is not None: lowerCamelCase__ = padded_inputs.convert_to_tensors(SCREAMING_SNAKE_CASE__ ) return padded_inputs
659
"""simple docstring""" 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 _snake_case = re.compile("[^A-Za-z_0-9]") # parameters used in DuplicationIndex _snake_case = 10 _snake_case = 256 def snake_case ( _a: List[str] )-> Optional[MinHash]: '''simple docstring''' if len(_a ) < MIN_NUM_TOKENS: return None lowerCamelCase__ = MinHash(num_perm=_a ) for token in set(_a ): min_hash.update(token.encode() ) return min_hash def snake_case ( _a: str )-> Set[str]: '''simple docstring''' return {t for t in NON_ALPHA.split(_a ) if len(t.strip() ) > 0} class _a : def __init__( self : List[Any] , *, SCREAMING_SNAKE_CASE__ : float = 0.85 , ): lowerCamelCase__ = duplication_jaccard_threshold lowerCamelCase__ = NUM_PERM lowerCamelCase__ = MinHashLSH(threshold=self._duplication_jaccard_threshold , num_perm=self._num_perm ) lowerCamelCase__ = defaultdict(SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : List[Any] , SCREAMING_SNAKE_CASE__ : Tuple , SCREAMING_SNAKE_CASE__ : MinHash ): lowerCamelCase__ = self._index.query(SCREAMING_SNAKE_CASE__ ) if code_key in self._index.keys: print(F'Duplicate key {code_key}' ) return self._index.insert(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) if len(SCREAMING_SNAKE_CASE__ ) > 0: for base_duplicate in close_duplicates: if base_duplicate in self._duplicate_clusters: self._duplicate_clusters[base_duplicate].add(SCREAMING_SNAKE_CASE__ ) break else: self._duplicate_clusters[close_duplicates[0]].add(SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : str ): lowerCamelCase__ = [] for base, duplicates in self._duplicate_clusters.items(): lowerCamelCase__ = [base] + list(SCREAMING_SNAKE_CASE__ ) # reformat the cluster to be a list of dict lowerCamelCase__ = [{'base_index': el[0], 'repo_name': el[1], 'path': el[2]} for el in cluster] duplicate_clusters.append(SCREAMING_SNAKE_CASE__ ) return duplicate_clusters def _UpperCamelCase ( self : List[str] , SCREAMING_SNAKE_CASE__ : Union[str, Any] ): lowerCamelCase__ = self.get_duplicate_clusters() with open(SCREAMING_SNAKE_CASE__ , 'w' ) as f: json.dump(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) def snake_case ( _a: Union[str, Any] )-> Optional[int]: '''simple docstring''' lowerCamelCase__ , lowerCamelCase__ = element lowerCamelCase__ = 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 snake_case ( _a: Type[Dataset] )-> Tuple: '''simple docstring''' with mp.Pool() as pool: for data in pool.imap_unordered( _compute_min_hash , ThreadedIterator(_a , max_queue_size=10000 ) , chunksize=100 , ): if data is not None: yield data def snake_case ( _a: Type[Dataset] , _a: float )-> Optional[int]: '''simple docstring''' lowerCamelCase__ = 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 snake_case ( _a: str , _a: str )-> float: '''simple docstring''' lowerCamelCase__ = get_tokens(_a ) lowerCamelCase__ = get_tokens(_a ) return len(tokensa & tokensa ) / len(tokensa | tokensa ) _snake_case = None def snake_case ( _a: Dict , _a: Union[str, Any] )-> List[str]: '''simple docstring''' lowerCamelCase__ = [] for elementa in cluster: lowerCamelCase__ = _shared_dataset[elementa['base_index']]['content'] for elementa in extremes: lowerCamelCase__ = _shared_dataset[elementa['base_index']]['content'] if jaccard_similarity(_a , _a ) >= jaccard_threshold: elementa["copies"] += 1 break else: lowerCamelCase__ = 1 extremes.append(_a ) return extremes def snake_case ( _a: Any , _a: Tuple , _a: Dict )-> Union[str, Any]: '''simple docstring''' global _shared_dataset lowerCamelCase__ = dataset lowerCamelCase__ = [] lowerCamelCase__ = 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 snake_case ( _a: Type[Dataset] , _a: float = 0.85 )-> Tuple[Type[Dataset], List[List[Dict]]]: '''simple docstring''' lowerCamelCase__ = make_duplicate_clusters(_a , _a ) lowerCamelCase__ = {x['base_index'] for cluster in duplicate_clusters for x in cluster} lowerCamelCase__ = {} lowerCamelCase__ = find_extremes(_a , _a , _a ) for extremes in extremes_clusters: for element in extremes: lowerCamelCase__ = element lowerCamelCase__ = duplicate_indices - set(extreme_dict.keys() ) lowerCamelCase__ = 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: lowerCamelCase__ = element['base_index'] in extreme_dict if element["is_extreme"]: lowerCamelCase__ = 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
659
1
"""simple docstring""" from __future__ import annotations import unittest from transformers import is_tf_available, is_torch_available from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, SMALL_MODEL_IDENTIFIER, is_pt_tf_cross_test, slow if is_tf_available(): from transformers import ( AutoConfig, BertConfig, GPTaConfig, TaConfig, TFAutoModel, TFAutoModelForCausalLM, TFAutoModelForMaskedLM, TFAutoModelForPreTraining, TFAutoModelForQuestionAnswering, TFAutoModelForSeqaSeqLM, TFAutoModelForSequenceClassification, TFAutoModelWithLMHead, TFBertForMaskedLM, TFBertForPreTraining, TFBertForQuestionAnswering, TFBertForSequenceClassification, TFBertModel, TFGPTaLMHeadModel, TFRobertaForMaskedLM, TFTaForConditionalGeneration, ) from transformers.models.bert.modeling_tf_bert import TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST from transformers.models.gpta.modeling_tf_gpta import TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST from transformers.models.ta.modeling_tf_ta import TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST if is_torch_available(): from transformers import ( AutoModel, AutoModelForCausalLM, AutoModelForMaskedLM, AutoModelForPreTraining, AutoModelForQuestionAnswering, AutoModelForSeqaSeqLM, AutoModelForSequenceClassification, AutoModelWithLMHead, BertForMaskedLM, BertForPreTraining, BertForQuestionAnswering, BertForSequenceClassification, BertModel, GPTaLMHeadModel, RobertaForMaskedLM, TaForConditionalGeneration, ) @is_pt_tf_cross_test class _a ( unittest.TestCase ): @slow def _UpperCamelCase ( self : Tuple ): # for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: for model_name in ["bert-base-uncased"]: lowerCamelCase__ = AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE__ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE__ ) self.assertIsInstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = TFAutoModel.from_pretrained(SCREAMING_SNAKE_CASE__ , from_pt=SCREAMING_SNAKE_CASE__ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE__ ) self.assertIsInstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = AutoModel.from_pretrained(SCREAMING_SNAKE_CASE__ , from_tf=SCREAMING_SNAKE_CASE__ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE__ ) self.assertIsInstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) @slow def _UpperCamelCase ( self : Dict ): # for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: for model_name in ["bert-base-uncased"]: lowerCamelCase__ = AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE__ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE__ ) self.assertIsInstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = TFAutoModelForPreTraining.from_pretrained(SCREAMING_SNAKE_CASE__ , from_pt=SCREAMING_SNAKE_CASE__ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE__ ) self.assertIsInstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = AutoModelForPreTraining.from_pretrained(SCREAMING_SNAKE_CASE__ , from_tf=SCREAMING_SNAKE_CASE__ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE__ ) self.assertIsInstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) @slow def _UpperCamelCase ( self : Tuple ): for model_name in TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowerCamelCase__ = AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE__ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE__ ) self.assertIsInstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = TFAutoModelForCausalLM.from_pretrained(SCREAMING_SNAKE_CASE__ , from_pt=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ , lowerCamelCase__ = TFAutoModelForCausalLM.from_pretrained( SCREAMING_SNAKE_CASE__ , output_loading_info=SCREAMING_SNAKE_CASE__ , from_pt=SCREAMING_SNAKE_CASE__ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE__ ) self.assertIsInstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = AutoModelForCausalLM.from_pretrained(SCREAMING_SNAKE_CASE__ , from_tf=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ , lowerCamelCase__ = AutoModelForCausalLM.from_pretrained( SCREAMING_SNAKE_CASE__ , output_loading_info=SCREAMING_SNAKE_CASE__ , from_tf=SCREAMING_SNAKE_CASE__ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE__ ) self.assertIsInstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) @slow def _UpperCamelCase ( self : Optional[int] ): for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowerCamelCase__ = AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE__ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE__ ) self.assertIsInstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = TFAutoModelWithLMHead.from_pretrained(SCREAMING_SNAKE_CASE__ , from_pt=SCREAMING_SNAKE_CASE__ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE__ ) self.assertIsInstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = AutoModelWithLMHead.from_pretrained(SCREAMING_SNAKE_CASE__ , from_tf=SCREAMING_SNAKE_CASE__ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE__ ) self.assertIsInstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) @slow def _UpperCamelCase ( self : Tuple ): for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowerCamelCase__ = AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE__ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE__ ) self.assertIsInstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = TFAutoModelForMaskedLM.from_pretrained(SCREAMING_SNAKE_CASE__ , from_pt=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ , lowerCamelCase__ = TFAutoModelForMaskedLM.from_pretrained( SCREAMING_SNAKE_CASE__ , output_loading_info=SCREAMING_SNAKE_CASE__ , from_pt=SCREAMING_SNAKE_CASE__ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE__ ) self.assertIsInstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = AutoModelForMaskedLM.from_pretrained(SCREAMING_SNAKE_CASE__ , from_tf=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ , lowerCamelCase__ = AutoModelForMaskedLM.from_pretrained( SCREAMING_SNAKE_CASE__ , output_loading_info=SCREAMING_SNAKE_CASE__ , from_tf=SCREAMING_SNAKE_CASE__ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE__ ) self.assertIsInstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) @slow def _UpperCamelCase ( self : List[Any] ): for model_name in TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowerCamelCase__ = AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE__ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE__ ) self.assertIsInstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = TFAutoModelForSeqaSeqLM.from_pretrained(SCREAMING_SNAKE_CASE__ , from_pt=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ , lowerCamelCase__ = TFAutoModelForSeqaSeqLM.from_pretrained( SCREAMING_SNAKE_CASE__ , output_loading_info=SCREAMING_SNAKE_CASE__ , from_pt=SCREAMING_SNAKE_CASE__ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE__ ) self.assertIsInstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = AutoModelForSeqaSeqLM.from_pretrained(SCREAMING_SNAKE_CASE__ , from_tf=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ , lowerCamelCase__ = AutoModelForSeqaSeqLM.from_pretrained( SCREAMING_SNAKE_CASE__ , output_loading_info=SCREAMING_SNAKE_CASE__ , from_tf=SCREAMING_SNAKE_CASE__ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE__ ) self.assertIsInstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) @slow def _UpperCamelCase ( self : Tuple ): # for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: for model_name in ["bert-base-uncased"]: lowerCamelCase__ = AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE__ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE__ ) self.assertIsInstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = TFAutoModelForSequenceClassification.from_pretrained(SCREAMING_SNAKE_CASE__ , from_pt=SCREAMING_SNAKE_CASE__ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE__ ) self.assertIsInstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = AutoModelForSequenceClassification.from_pretrained(SCREAMING_SNAKE_CASE__ , from_tf=SCREAMING_SNAKE_CASE__ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE__ ) self.assertIsInstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) @slow def _UpperCamelCase ( self : Any ): # for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: for model_name in ["bert-base-uncased"]: lowerCamelCase__ = AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE__ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE__ ) self.assertIsInstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = TFAutoModelForQuestionAnswering.from_pretrained(SCREAMING_SNAKE_CASE__ , from_pt=SCREAMING_SNAKE_CASE__ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE__ ) self.assertIsInstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = AutoModelForQuestionAnswering.from_pretrained(SCREAMING_SNAKE_CASE__ , from_tf=SCREAMING_SNAKE_CASE__ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE__ ) self.assertIsInstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : List[Any] ): lowerCamelCase__ = TFAutoModelWithLMHead.from_pretrained(SCREAMING_SNAKE_CASE__ , from_pt=SCREAMING_SNAKE_CASE__ ) self.assertIsInstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) self.assertEqual(model.num_parameters() , 1_44_10 ) self.assertEqual(model.num_parameters(only_trainable=SCREAMING_SNAKE_CASE__ ) , 1_44_10 ) lowerCamelCase__ = AutoModelWithLMHead.from_pretrained(SCREAMING_SNAKE_CASE__ , from_tf=SCREAMING_SNAKE_CASE__ ) self.assertIsInstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) self.assertEqual(model.num_parameters() , 1_44_10 ) self.assertEqual(model.num_parameters(only_trainable=SCREAMING_SNAKE_CASE__ ) , 1_44_10 ) def _UpperCamelCase ( self : Optional[Any] ): lowerCamelCase__ = TFAutoModelWithLMHead.from_pretrained(SCREAMING_SNAKE_CASE__ , from_pt=SCREAMING_SNAKE_CASE__ ) self.assertIsInstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) self.assertEqual(model.num_parameters() , 1_44_10 ) self.assertEqual(model.num_parameters(only_trainable=SCREAMING_SNAKE_CASE__ ) , 1_44_10 ) lowerCamelCase__ = AutoModelWithLMHead.from_pretrained(SCREAMING_SNAKE_CASE__ , from_tf=SCREAMING_SNAKE_CASE__ ) self.assertIsInstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) self.assertEqual(model.num_parameters() , 1_44_10 ) self.assertEqual(model.num_parameters(only_trainable=SCREAMING_SNAKE_CASE__ ) , 1_44_10 )
659
"""simple docstring""" import collections import json import math import os import re import time from fnmatch import fnmatch from typing import Dict import requests from slack_sdk import WebClient _snake_case = WebClient(token=os.environ["CI_SLACK_BOT_TOKEN"]) def snake_case ( _a: Any )-> Optional[Any]: '''simple docstring''' lowerCamelCase__ = test_results.split(' ' ) lowerCamelCase__ = 0 lowerCamelCase__ = 0 # When the output is short enough, the output is surrounded by = signs: "== OUTPUT ==" # When it is too long, those signs are not present. lowerCamelCase__ = expressions[-2] if '=' in expressions[-1] else expressions[-1] for i, expression in enumerate(_a ): if "failed" in expression: failed += int(expressions[i - 1] ) if "passed" in expression: success += int(expressions[i - 1] ) return failed, success, time_spent def snake_case ( _a: Optional[int] )-> Optional[int]: '''simple docstring''' lowerCamelCase__ = {} lowerCamelCase__ = None lowerCamelCase__ = False for line in failures_short_lines.split('\n' ): if re.search(R'_ \[doctest\]' , _a ): lowerCamelCase__ = True lowerCamelCase__ = line.split(' ' )[2] elif in_error and not line.split(' ' )[0].isdigit(): lowerCamelCase__ = line lowerCamelCase__ = False return failures class _a : def __init__( self : Optional[Any] , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : Dict ): lowerCamelCase__ = title lowerCamelCase__ = doc_test_results['time_spent'].split(',' )[0] lowerCamelCase__ = doc_test_results['success'] lowerCamelCase__ = doc_test_results['failures'] lowerCamelCase__ = self.n_success + self.n_failures # Failures and success of the modeling tests lowerCamelCase__ = doc_test_results @property def _UpperCamelCase ( self : List[str] ): lowerCamelCase__ = [self._time_spent] lowerCamelCase__ = 0 for time in time_spent: lowerCamelCase__ = time.split(':' ) # Time can be formatted as xx:xx:xx, as .xx, or as x.xx if the time spent was less than a minute. if len(SCREAMING_SNAKE_CASE__ ) == 1: lowerCamelCase__ = [0, 0, time_parts[0]] lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ = int(time_parts[0] ), int(time_parts[1] ), float(time_parts[2] ) total_secs += hours * 36_00 + minutes * 60 + seconds lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ = total_secs // 36_00, (total_secs % 36_00) // 60, total_secs % 60 return F'{int(SCREAMING_SNAKE_CASE__ )}h{int(SCREAMING_SNAKE_CASE__ )}m{int(SCREAMING_SNAKE_CASE__ )}s' @property def _UpperCamelCase ( self : Dict ): return {"type": "header", "text": {"type": "plain_text", "text": self.title}} @property def _UpperCamelCase ( self : Dict ): return { "type": "section", "text": { "type": "plain_text", "text": F'🌞 There were no failures: all {self.n_tests} tests passed. The suite ran in {self.time}.', "emoji": True, }, "accessory": { "type": "button", "text": {"type": "plain_text", "text": "Check Action results", "emoji": True}, "url": F'https://github.com/huggingface/transformers/actions/runs/{os.environ["GITHUB_RUN_ID"]}', }, } @property def _UpperCamelCase ( self : Any ): return { "type": "section", "text": { "type": "plain_text", "text": ( F'There were {self.n_failures} failures, out of {self.n_tests} tests.\nThe suite ran in' F' {self.time}.' ), "emoji": True, }, "accessory": { "type": "button", "text": {"type": "plain_text", "text": "Check Action results", "emoji": True}, "url": F'https://github.com/huggingface/transformers/actions/runs/{os.environ["GITHUB_RUN_ID"]}', }, } @property def _UpperCamelCase ( self : Union[str, Any] ): lowerCamelCase__ = 40 lowerCamelCase__ = {k: v['failed'] for k, v in doc_test_results.items() if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ )} lowerCamelCase__ = '' for category, failures in category_failures.items(): if len(SCREAMING_SNAKE_CASE__ ) == 0: continue if report != "": report += "\n\n" report += F'*{category} failures*:'.ljust(line_length // 2 ).rjust(line_length // 2 ) + "\n" report += "`" report += "`\n`".join(SCREAMING_SNAKE_CASE__ ) report += "`" return { "type": "section", "text": { "type": "mrkdwn", "text": F'The following examples had failures:\n\n\n{report}\n', }, } @property def _UpperCamelCase ( self : str ): lowerCamelCase__ = [self.header] if self.n_failures > 0: blocks.append(self.failures ) if self.n_failures > 0: blocks.extend([self.category_failures] ) if self.n_failures == 0: blocks.append(self.no_failures ) return json.dumps(SCREAMING_SNAKE_CASE__ ) @staticmethod def _UpperCamelCase ( ): lowerCamelCase__ = [ { 'type': 'section', 'text': { 'type': 'plain_text', 'text': 'There was an issue running the tests.', }, 'accessory': { 'type': 'button', 'text': {'type': 'plain_text', 'text': 'Check Action results', 'emoji': True}, 'url': F'https://github.com/huggingface/transformers/actions/runs/{os.environ["GITHUB_RUN_ID"]}', }, } ] print('Sending the following payload' ) print(json.dumps({'blocks': json.loads(SCREAMING_SNAKE_CASE__ )} ) ) client.chat_postMessage( channel=os.environ['CI_SLACK_CHANNEL_ID_DAILY'] , text='There was an issue running the tests.' , blocks=SCREAMING_SNAKE_CASE__ , ) def _UpperCamelCase ( self : Optional[int] ): print('Sending the following payload' ) print(json.dumps({'blocks': json.loads(self.payload )} ) ) lowerCamelCase__ = F'{self.n_failures} failures out of {self.n_tests} tests,' if self.n_failures else 'All tests passed.' lowerCamelCase__ = client.chat_postMessage( channel=os.environ['CI_SLACK_CHANNEL_ID_DAILY'] , blocks=self.payload , text=SCREAMING_SNAKE_CASE__ , ) def _UpperCamelCase ( self : Optional[Any] , SCREAMING_SNAKE_CASE__ : Dict , SCREAMING_SNAKE_CASE__ : List[Any] , SCREAMING_SNAKE_CASE__ : Tuple , SCREAMING_SNAKE_CASE__ : Tuple ): lowerCamelCase__ = '' for key, value in failures.items(): lowerCamelCase__ = value[:2_00] + ' [Truncated]' if len(SCREAMING_SNAKE_CASE__ ) > 2_50 else value failures_text += F'*{key}*\n_{value}_\n\n' lowerCamelCase__ = job_name lowerCamelCase__ = {'type': 'section', 'text': {'type': 'mrkdwn', 'text': text}} if job_link is not None: lowerCamelCase__ = { 'type': 'button', 'text': {'type': 'plain_text', 'text': 'GitHub Action job', 'emoji': True}, 'url': job_link, } return [ {"type": "header", "text": {"type": "plain_text", "text": title.upper(), "emoji": True}}, content, {"type": "section", "text": {"type": "mrkdwn", "text": failures_text}}, ] def _UpperCamelCase ( self : Optional[int] ): if self.thread_ts is None: raise ValueError('Can only post reply if a post has been made.' ) lowerCamelCase__ = self.doc_test_results.pop('job_link' ) self.doc_test_results.pop('failures' ) self.doc_test_results.pop('success' ) self.doc_test_results.pop('time_spent' ) lowerCamelCase__ = sorted(self.doc_test_results.items() , key=lambda SCREAMING_SNAKE_CASE__ : t[0] ) for job, job_result in sorted_dict: if len(job_result['failures'] ): lowerCamelCase__ = F'*Num failures* :{len(job_result["failed"] )} \n' lowerCamelCase__ = job_result['failures'] lowerCamelCase__ = self.get_reply_blocks(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , text=SCREAMING_SNAKE_CASE__ ) print('Sending the following reply' ) print(json.dumps({'blocks': blocks} ) ) client.chat_postMessage( channel=os.environ['CI_SLACK_CHANNEL_ID_DAILY'] , text=F'Results for {job}' , blocks=SCREAMING_SNAKE_CASE__ , thread_ts=self.thread_ts['ts'] , ) time.sleep(1 ) def snake_case ( )-> Optional[Any]: '''simple docstring''' lowerCamelCase__ = os.environ['GITHUB_RUN_ID'] lowerCamelCase__ = F'https://api.github.com/repos/huggingface/transformers/actions/runs/{run_id}/jobs?per_page=100' lowerCamelCase__ = requests.get(_a ).json() lowerCamelCase__ = {} try: jobs.update({job['name']: job['html_url'] for job in result['jobs']} ) lowerCamelCase__ = math.ceil((result['total_count'] - 100) / 100 ) for i in range(_a ): lowerCamelCase__ = requests.get(url + F'&page={i + 2}' ).json() jobs.update({job['name']: job['html_url'] for job in result['jobs']} ) return jobs except Exception as e: print('Unknown error, could not fetch links.' , _a ) return {} def snake_case ( _a: str )-> Dict: '''simple docstring''' lowerCamelCase__ = {} if os.path.exists(_a ): lowerCamelCase__ = os.listdir(_a ) for file in files: try: with open(os.path.join(_a , _a ) , encoding='utf-8' ) as f: lowerCamelCase__ = f.read() except UnicodeDecodeError as e: raise ValueError(F'Could not open {os.path.join(_a , _a )}.' ) from e return _artifact def snake_case ( )-> Optional[int]: '''simple docstring''' class _a : def __init__( self : Tuple , SCREAMING_SNAKE_CASE__ : str ): lowerCamelCase__ = name lowerCamelCase__ = [] def __str__( self : Dict ): return self.name def _UpperCamelCase ( self : Union[str, Any] , SCREAMING_SNAKE_CASE__ : str ): self.paths.append({'name': self.name, 'path': path} ) lowerCamelCase__ = {} lowerCamelCase__ = filter(os.path.isdir , os.listdir() ) for directory in directories: lowerCamelCase__ = directory if artifact_name not in _available_artifacts: lowerCamelCase__ = Artifact(_a ) _available_artifacts[artifact_name].add_path(_a ) return _available_artifacts if __name__ == "__main__": _snake_case = get_job_links() _snake_case = retrieve_available_artifacts() _snake_case = collections.OrderedDict( [ ("*.py", "API Examples"), ("*.md", "MD Examples"), ] ) # This dict will contain all the information relative to each doc test category: # - failed: list of failed tests # - failures: dict in the format 'test': 'error_message' _snake_case = { v: { "failed": [], "failures": {}, } for v in docs.values() } # Link to the GitHub Action job _snake_case = github_actions_job_links.get("run_doctests") _snake_case = available_artifacts["doc_tests_gpu_test_reports"].paths[0] _snake_case = retrieve_artifact(artifact_path["name"]) if "stats" in artifact: _snake_case , _snake_case , _snake_case = handle_test_results(artifact["stats"]) _snake_case = failed _snake_case = success _snake_case = time_spent[1:-1] + ", " _snake_case = extract_first_line_failure(artifact["failures_short"]) for line in artifact["summary_short"].split("\n"): if re.search("FAILED", line): _snake_case = line.replace("FAILED ", "") _snake_case = line.split()[0].replace("\n", "") if "::" in line: _snake_case , _snake_case = line.split("::") else: _snake_case , _snake_case = line, line for file_regex in docs.keys(): if fnmatch(file_path, file_regex): _snake_case = docs[file_regex] doc_test_results[category]["failed"].append(test) _snake_case = all_failures[test] if test in all_failures else "N/A" _snake_case = failure break _snake_case = Message("🤗 Results of the doc tests.", doc_test_results) message.post() message.post_reply()
659
1
"""simple docstring""" import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_funnel import FunnelTokenizer _snake_case = logging.get_logger(__name__) _snake_case = {"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"} _snake_case = [ "small", "small-base", "medium", "medium-base", "intermediate", "intermediate-base", "large", "large-base", "xlarge", "xlarge-base", ] _snake_case = { "vocab_file": { "funnel-transformer/small": "https://huggingface.co/funnel-transformer/small/resolve/main/vocab.txt", "funnel-transformer/small-base": "https://huggingface.co/funnel-transformer/small-base/resolve/main/vocab.txt", "funnel-transformer/medium": "https://huggingface.co/funnel-transformer/medium/resolve/main/vocab.txt", "funnel-transformer/medium-base": ( "https://huggingface.co/funnel-transformer/medium-base/resolve/main/vocab.txt" ), "funnel-transformer/intermediate": ( "https://huggingface.co/funnel-transformer/intermediate/resolve/main/vocab.txt" ), "funnel-transformer/intermediate-base": ( "https://huggingface.co/funnel-transformer/intermediate-base/resolve/main/vocab.txt" ), "funnel-transformer/large": "https://huggingface.co/funnel-transformer/large/resolve/main/vocab.txt", "funnel-transformer/large-base": "https://huggingface.co/funnel-transformer/large-base/resolve/main/vocab.txt", "funnel-transformer/xlarge": "https://huggingface.co/funnel-transformer/xlarge/resolve/main/vocab.txt", "funnel-transformer/xlarge-base": ( "https://huggingface.co/funnel-transformer/xlarge-base/resolve/main/vocab.txt" ), }, "tokenizer_file": { "funnel-transformer/small": "https://huggingface.co/funnel-transformer/small/resolve/main/tokenizer.json", "funnel-transformer/small-base": ( "https://huggingface.co/funnel-transformer/small-base/resolve/main/tokenizer.json" ), "funnel-transformer/medium": "https://huggingface.co/funnel-transformer/medium/resolve/main/tokenizer.json", "funnel-transformer/medium-base": ( "https://huggingface.co/funnel-transformer/medium-base/resolve/main/tokenizer.json" ), "funnel-transformer/intermediate": ( "https://huggingface.co/funnel-transformer/intermediate/resolve/main/tokenizer.json" ), "funnel-transformer/intermediate-base": ( "https://huggingface.co/funnel-transformer/intermediate-base/resolve/main/tokenizer.json" ), "funnel-transformer/large": "https://huggingface.co/funnel-transformer/large/resolve/main/tokenizer.json", "funnel-transformer/large-base": ( "https://huggingface.co/funnel-transformer/large-base/resolve/main/tokenizer.json" ), "funnel-transformer/xlarge": "https://huggingface.co/funnel-transformer/xlarge/resolve/main/tokenizer.json", "funnel-transformer/xlarge-base": ( "https://huggingface.co/funnel-transformer/xlarge-base/resolve/main/tokenizer.json" ), }, } _snake_case = {f"""funnel-transformer/{name}""": 512 for name in _model_names} _snake_case = {f"""funnel-transformer/{name}""": {"do_lower_case": True} for name in _model_names} class _a ( SCREAMING_SNAKE_CASE_ ): a_ : List[str] = VOCAB_FILES_NAMES a_ : List[str] = PRETRAINED_VOCAB_FILES_MAP a_ : Optional[int] = PRETRAINED_INIT_CONFIGURATION a_ : List[str] = FunnelTokenizer a_ : Dict = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES a_ : int = 2 def __init__( self : int , SCREAMING_SNAKE_CASE__ : Optional[int]=None , SCREAMING_SNAKE_CASE__ : Union[str, Any]=None , SCREAMING_SNAKE_CASE__ : int=True , SCREAMING_SNAKE_CASE__ : Any="<unk>" , SCREAMING_SNAKE_CASE__ : List[Any]="<sep>" , SCREAMING_SNAKE_CASE__ : int="<pad>" , SCREAMING_SNAKE_CASE__ : Tuple="<cls>" , SCREAMING_SNAKE_CASE__ : Tuple="<mask>" , SCREAMING_SNAKE_CASE__ : Any="<s>" , SCREAMING_SNAKE_CASE__ : Tuple="</s>" , SCREAMING_SNAKE_CASE__ : str=True , SCREAMING_SNAKE_CASE__ : Any=True , SCREAMING_SNAKE_CASE__ : str=None , SCREAMING_SNAKE_CASE__ : int="##" , **SCREAMING_SNAKE_CASE__ : Any , ): super().__init__( SCREAMING_SNAKE_CASE__ , tokenizer_file=SCREAMING_SNAKE_CASE__ , do_lower_case=SCREAMING_SNAKE_CASE__ , unk_token=SCREAMING_SNAKE_CASE__ , sep_token=SCREAMING_SNAKE_CASE__ , pad_token=SCREAMING_SNAKE_CASE__ , cls_token=SCREAMING_SNAKE_CASE__ , mask_token=SCREAMING_SNAKE_CASE__ , bos_token=SCREAMING_SNAKE_CASE__ , eos_token=SCREAMING_SNAKE_CASE__ , clean_text=SCREAMING_SNAKE_CASE__ , tokenize_chinese_chars=SCREAMING_SNAKE_CASE__ , strip_accents=SCREAMING_SNAKE_CASE__ , wordpieces_prefix=SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ , ) lowerCamelCase__ = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get('lowercase' , SCREAMING_SNAKE_CASE__ ) != do_lower_case or normalizer_state.get('strip_accents' , SCREAMING_SNAKE_CASE__ ) != strip_accents or normalizer_state.get('handle_chinese_chars' , SCREAMING_SNAKE_CASE__ ) != tokenize_chinese_chars ): lowerCamelCase__ = getattr(SCREAMING_SNAKE_CASE__ , normalizer_state.pop('type' ) ) lowerCamelCase__ = do_lower_case lowerCamelCase__ = strip_accents lowerCamelCase__ = tokenize_chinese_chars lowerCamelCase__ = normalizer_class(**SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = do_lower_case def _UpperCamelCase ( self : Dict , SCREAMING_SNAKE_CASE__ : Tuple , SCREAMING_SNAKE_CASE__ : Optional[Any]=None ): lowerCamelCase__ = [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 _UpperCamelCase ( self : Dict , SCREAMING_SNAKE_CASE__ : List[int] , SCREAMING_SNAKE_CASE__ : Optional[List[int]] = None ): lowerCamelCase__ = [self.sep_token_id] lowerCamelCase__ = [self.cls_token_id] if token_ids_a is None: return len(cls ) * [self.cls_token_type_id] + len(token_ids_a + sep ) * [0] return len(cls ) * [self.cls_token_type_id] + len(token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def _UpperCamelCase ( self : Optional[int] , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : Optional[str] = None ): lowerCamelCase__ = self._tokenizer.model.save(SCREAMING_SNAKE_CASE__ , name=SCREAMING_SNAKE_CASE__ ) return tuple(SCREAMING_SNAKE_CASE__ )
659
"""simple docstring""" 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 _snake_case = logging.get_logger(__name__) # pylint: disable=invalid-name _snake_case = "\n Examples:\n ```py\n >>> from PIL import Image\n >>> import torch\n >>> from diffusers import DiffusionPipeline\n >>> from diffusers.utils import export_to_gif, load_image\n\n >>> device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n\n >>> repo = \"openai/shap-e-img2img\"\n >>> pipe = DiffusionPipeline.from_pretrained(repo, torch_dtype=torch.float16)\n >>> pipe = pipe.to(device)\n\n >>> guidance_scale = 3.0\n >>> image_url = \"https://hf.co/datasets/diffusers/docs-images/resolve/main/shap-e/corgi.png\"\n >>> image = load_image(image_url).convert(\"RGB\")\n\n >>> images = pipe(\n ... image,\n ... guidance_scale=guidance_scale,\n ... num_inference_steps=64,\n ... frame_size=256,\n ... ).images\n\n >>> gif_path = export_to_gif(images[0], \"corgi_3d.gif\")\n ```\n" @dataclass class _a ( SCREAMING_SNAKE_CASE_ ): a_ : Union[PIL.Image.Image, np.ndarray] class _a ( SCREAMING_SNAKE_CASE_ ): def __init__( self : Tuple , SCREAMING_SNAKE_CASE__ : PriorTransformer , SCREAMING_SNAKE_CASE__ : CLIPVisionModel , SCREAMING_SNAKE_CASE__ : CLIPImageProcessor , SCREAMING_SNAKE_CASE__ : HeunDiscreteScheduler , SCREAMING_SNAKE_CASE__ : ShapERenderer , ): super().__init__() self.register_modules( prior=SCREAMING_SNAKE_CASE__ , image_encoder=SCREAMING_SNAKE_CASE__ , image_processor=SCREAMING_SNAKE_CASE__ , scheduler=SCREAMING_SNAKE_CASE__ , renderer=SCREAMING_SNAKE_CASE__ , ) def _UpperCamelCase ( self : str , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : Optional[int] , SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : List[str] ): if latents is None: lowerCamelCase__ = randn_tensor(SCREAMING_SNAKE_CASE__ , generator=SCREAMING_SNAKE_CASE__ , device=SCREAMING_SNAKE_CASE__ , dtype=SCREAMING_SNAKE_CASE__ ) else: if latents.shape != shape: raise ValueError(F'Unexpected latents shape, got {latents.shape}, expected {shape}' ) lowerCamelCase__ = latents.to(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = latents * scheduler.init_noise_sigma return latents def _UpperCamelCase ( self : List[Any] , SCREAMING_SNAKE_CASE__ : List[str]=0 ): if is_accelerate_available(): from accelerate import cpu_offload else: raise ImportError('Please install accelerate via `pip install accelerate`' ) lowerCamelCase__ = torch.device(F'cuda:{gpu_id}' ) lowerCamelCase__ = [self.image_encoder, self.prior] for cpu_offloaded_model in models: if cpu_offloaded_model is not None: cpu_offload(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) @property def _UpperCamelCase ( self : Dict ): 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(SCREAMING_SNAKE_CASE__ , '_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 _UpperCamelCase ( self : Optional[int] , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : Dict , SCREAMING_SNAKE_CASE__ : Optional[Any] , SCREAMING_SNAKE_CASE__ : int , ): if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and isinstance(image[0] , torch.Tensor ): lowerCamelCase__ = torch.cat(SCREAMING_SNAKE_CASE__ , axis=0 ) if image[0].ndim == 4 else torch.stack(SCREAMING_SNAKE_CASE__ , axis=0 ) if not isinstance(SCREAMING_SNAKE_CASE__ , torch.Tensor ): lowerCamelCase__ = self.image_processor(SCREAMING_SNAKE_CASE__ , return_tensors='pt' ).pixel_values[0].unsqueeze(0 ) lowerCamelCase__ = image.to(dtype=self.image_encoder.dtype , device=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = self.image_encoder(SCREAMING_SNAKE_CASE__ )['last_hidden_state'] lowerCamelCase__ = image_embeds[:, 1:, :].contiguous() # batch_size, dim, 256 lowerCamelCase__ = image_embeds.repeat_interleave(SCREAMING_SNAKE_CASE__ , dim=0 ) if do_classifier_free_guidance: lowerCamelCase__ = torch.zeros_like(SCREAMING_SNAKE_CASE__ ) # 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 lowerCamelCase__ = torch.cat([negative_image_embeds, image_embeds] ) return image_embeds @torch.no_grad() @replace_example_docstring(SCREAMING_SNAKE_CASE__ ) def __call__( self : List[Any] , SCREAMING_SNAKE_CASE__ : Union[PIL.Image.Image, List[PIL.Image.Image]] , SCREAMING_SNAKE_CASE__ : int = 1 , SCREAMING_SNAKE_CASE__ : int = 25 , SCREAMING_SNAKE_CASE__ : Optional[Union[torch.Generator, List[torch.Generator]]] = None , SCREAMING_SNAKE_CASE__ : Optional[torch.FloatTensor] = None , SCREAMING_SNAKE_CASE__ : float = 4.0 , SCREAMING_SNAKE_CASE__ : int = 64 , SCREAMING_SNAKE_CASE__ : Optional[str] = "pil" , SCREAMING_SNAKE_CASE__ : bool = True , ): if isinstance(SCREAMING_SNAKE_CASE__ , PIL.Image.Image ): lowerCamelCase__ = 1 elif isinstance(SCREAMING_SNAKE_CASE__ , torch.Tensor ): lowerCamelCase__ = image.shape[0] elif isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) and isinstance(image[0] , (torch.Tensor, PIL.Image.Image) ): lowerCamelCase__ = len(SCREAMING_SNAKE_CASE__ ) 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(SCREAMING_SNAKE_CASE__ )}' ) lowerCamelCase__ = self._execution_device lowerCamelCase__ = batch_size * num_images_per_prompt lowerCamelCase__ = guidance_scale > 1.0 lowerCamelCase__ = self._encode_image(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) # prior self.scheduler.set_timesteps(SCREAMING_SNAKE_CASE__ , device=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = self.scheduler.timesteps lowerCamelCase__ = self.prior.config.num_embeddings lowerCamelCase__ = self.prior.config.embedding_dim lowerCamelCase__ = self.prepare_latents( (batch_size, num_embeddings * embedding_dim) , image_embeds.dtype , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , 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 lowerCamelCase__ = latents.reshape(latents.shape[0] , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) for i, t in enumerate(self.progress_bar(SCREAMING_SNAKE_CASE__ ) ): # expand the latents if we are doing classifier free guidance lowerCamelCase__ = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents lowerCamelCase__ = self.scheduler.scale_model_input(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = self.prior( SCREAMING_SNAKE_CASE__ , timestep=SCREAMING_SNAKE_CASE__ , proj_embedding=SCREAMING_SNAKE_CASE__ , ).predicted_image_embedding # remove the variance lowerCamelCase__ , lowerCamelCase__ = noise_pred.split( scaled_model_input.shape[2] , dim=2 ) # batch_size, num_embeddings, embedding_dim if do_classifier_free_guidance is not None: lowerCamelCase__ , lowerCamelCase__ = noise_pred.chunk(2 ) lowerCamelCase__ = noise_pred_uncond + guidance_scale * (noise_pred - noise_pred_uncond) lowerCamelCase__ = self.scheduler.step( SCREAMING_SNAKE_CASE__ , timestep=SCREAMING_SNAKE_CASE__ , sample=SCREAMING_SNAKE_CASE__ , ).prev_sample if output_type == "latent": return ShapEPipelineOutput(images=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = [] for i, latent in enumerate(SCREAMING_SNAKE_CASE__ ): print() lowerCamelCase__ = self.renderer.decode( latent[None, :] , SCREAMING_SNAKE_CASE__ , size=SCREAMING_SNAKE_CASE__ , ray_batch_size=40_96 , n_coarse_samples=64 , n_fine_samples=1_28 , ) images.append(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = torch.stack(SCREAMING_SNAKE_CASE__ ) if output_type not in ["np", "pil"]: raise ValueError(F'Only the output types `pil` and `np` are supported not output_type={output_type}' ) lowerCamelCase__ = images.cpu().numpy() if output_type == "pil": lowerCamelCase__ = [self.numpy_to_pil(SCREAMING_SNAKE_CASE__ ) 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=SCREAMING_SNAKE_CASE__ )
659
1
"""simple docstring""" # Copyright 2021 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import argparse import os from accelerate.test_utils import execute_subprocess_async def snake_case ( _a: Optional[Any]=None )-> Optional[Any]: '''simple docstring''' if subparsers is not None: lowerCamelCase__ = subparsers.add_parser('test' ) else: lowerCamelCase__ = argparse.ArgumentParser('Accelerate test command' ) parser.add_argument( '--config_file' , default=_a , help=( 'The path to use to store the config file. Will default to a file named default_config.yaml in the cache ' 'location, which is the content of the environment `HF_HOME` suffixed with \'accelerate\', or if you don\'t have ' 'such an environment variable, your cache directory (\'~/.cache\' or the content of `XDG_CACHE_HOME`) suffixed ' 'with \'huggingface\'.' ) , ) if subparsers is not None: parser.set_defaults(func=_a ) return parser def snake_case ( _a: Optional[Any] )-> Union[str, Any]: '''simple docstring''' lowerCamelCase__ = os.path.sep.join(__file__.split(os.path.sep )[:-2] + ['test_utils', 'scripts', 'test_script.py'] ) if args.config_file is None: lowerCamelCase__ = script_name else: lowerCamelCase__ = F'--config_file={args.config_file} {script_name}' lowerCamelCase__ = ['accelerate-launch'] + test_args.split() lowerCamelCase__ = execute_subprocess_async(_a , env=os.environ.copy() ) if result.returncode == 0: print('Test is a success! You are ready for your distributed training!' ) def snake_case ( )-> Optional[int]: '''simple docstring''' lowerCamelCase__ = test_command_parser() lowerCamelCase__ = parser.parse_args() test_command(_a ) if __name__ == "__main__": main()
659
"""simple docstring""" import os from shutil import copyfile from typing import List, Optional, Tuple from tokenizers import processors from ...tokenization_utils import AddedToken, BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_nllb import NllbTokenizer else: _snake_case = None _snake_case = logging.get_logger(__name__) _snake_case = {"vocab_file": "sentencepiece.bpe.model", "tokenizer_file": "tokenizer.json"} _snake_case = { "vocab_file": { "facebook/nllb-200-distilled-600M": ( "https://huggingface.co/facebook/nllb-200-distilled-600M/resolve/main/sentencepiece.bpe.model" ), }, "tokenizer_file": { "facebook/nllb-200-distilled-600M": ( "https://huggingface.co/facebook/nllb-200-distilled-600M/resolve/main/tokenizer.json" ), }, } _snake_case = { "facebook/nllb-large-en-ro": 1024, "facebook/nllb-200-distilled-600M": 1024, } # fmt: off _snake_case = ["ace_Arab", "ace_Latn", "acm_Arab", "acq_Arab", "aeb_Arab", "afr_Latn", "ajp_Arab", "aka_Latn", "amh_Ethi", "apc_Arab", "arb_Arab", "ars_Arab", "ary_Arab", "arz_Arab", "asm_Beng", "ast_Latn", "awa_Deva", "ayr_Latn", "azb_Arab", "azj_Latn", "bak_Cyrl", "bam_Latn", "ban_Latn", "bel_Cyrl", "bem_Latn", "ben_Beng", "bho_Deva", "bjn_Arab", "bjn_Latn", "bod_Tibt", "bos_Latn", "bug_Latn", "bul_Cyrl", "cat_Latn", "ceb_Latn", "ces_Latn", "cjk_Latn", "ckb_Arab", "crh_Latn", "cym_Latn", "dan_Latn", "deu_Latn", "dik_Latn", "dyu_Latn", "dzo_Tibt", "ell_Grek", "eng_Latn", "epo_Latn", "est_Latn", "eus_Latn", "ewe_Latn", "fao_Latn", "pes_Arab", "fij_Latn", "fin_Latn", "fon_Latn", "fra_Latn", "fur_Latn", "fuv_Latn", "gla_Latn", "gle_Latn", "glg_Latn", "grn_Latn", "guj_Gujr", "hat_Latn", "hau_Latn", "heb_Hebr", "hin_Deva", "hne_Deva", "hrv_Latn", "hun_Latn", "hye_Armn", "ibo_Latn", "ilo_Latn", "ind_Latn", "isl_Latn", "ita_Latn", "jav_Latn", "jpn_Jpan", "kab_Latn", "kac_Latn", "kam_Latn", "kan_Knda", "kas_Arab", "kas_Deva", "kat_Geor", "knc_Arab", "knc_Latn", "kaz_Cyrl", "kbp_Latn", "kea_Latn", "khm_Khmr", "kik_Latn", "kin_Latn", "kir_Cyrl", "kmb_Latn", "kon_Latn", "kor_Hang", "kmr_Latn", "lao_Laoo", "lvs_Latn", "lij_Latn", "lim_Latn", "lin_Latn", "lit_Latn", "lmo_Latn", "ltg_Latn", "ltz_Latn", "lua_Latn", "lug_Latn", "luo_Latn", "lus_Latn", "mag_Deva", "mai_Deva", "mal_Mlym", "mar_Deva", "min_Latn", "mkd_Cyrl", "plt_Latn", "mlt_Latn", "mni_Beng", "khk_Cyrl", "mos_Latn", "mri_Latn", "zsm_Latn", "mya_Mymr", "nld_Latn", "nno_Latn", "nob_Latn", "npi_Deva", "nso_Latn", "nus_Latn", "nya_Latn", "oci_Latn", "gaz_Latn", "ory_Orya", "pag_Latn", "pan_Guru", "pap_Latn", "pol_Latn", "por_Latn", "prs_Arab", "pbt_Arab", "quy_Latn", "ron_Latn", "run_Latn", "rus_Cyrl", "sag_Latn", "san_Deva", "sat_Beng", "scn_Latn", "shn_Mymr", "sin_Sinh", "slk_Latn", "slv_Latn", "smo_Latn", "sna_Latn", "snd_Arab", "som_Latn", "sot_Latn", "spa_Latn", "als_Latn", "srd_Latn", "srp_Cyrl", "ssw_Latn", "sun_Latn", "swe_Latn", "swh_Latn", "szl_Latn", "tam_Taml", "tat_Cyrl", "tel_Telu", "tgk_Cyrl", "tgl_Latn", "tha_Thai", "tir_Ethi", "taq_Latn", "taq_Tfng", "tpi_Latn", "tsn_Latn", "tso_Latn", "tuk_Latn", "tum_Latn", "tur_Latn", "twi_Latn", "tzm_Tfng", "uig_Arab", "ukr_Cyrl", "umb_Latn", "urd_Arab", "uzn_Latn", "vec_Latn", "vie_Latn", "war_Latn", "wol_Latn", "xho_Latn", "ydd_Hebr", "yor_Latn", "yue_Hant", "zho_Hans", "zho_Hant", "zul_Latn"] class _a ( SCREAMING_SNAKE_CASE_ ): a_ : Any = VOCAB_FILES_NAMES a_ : Optional[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES a_ : Optional[Any] = PRETRAINED_VOCAB_FILES_MAP a_ : List[str] = ['input_ids', 'attention_mask'] a_ : Union[str, Any] = NllbTokenizer a_ : List[int] = [] a_ : List[int] = [] def __init__( self : int , SCREAMING_SNAKE_CASE__ : List[Any]=None , SCREAMING_SNAKE_CASE__ : Optional[int]=None , SCREAMING_SNAKE_CASE__ : List[Any]="<s>" , SCREAMING_SNAKE_CASE__ : List[str]="</s>" , SCREAMING_SNAKE_CASE__ : Any="</s>" , SCREAMING_SNAKE_CASE__ : List[str]="<s>" , SCREAMING_SNAKE_CASE__ : Tuple="<unk>" , SCREAMING_SNAKE_CASE__ : Optional[int]="<pad>" , SCREAMING_SNAKE_CASE__ : Any="<mask>" , SCREAMING_SNAKE_CASE__ : Union[str, Any]=None , SCREAMING_SNAKE_CASE__ : int=None , SCREAMING_SNAKE_CASE__ : Dict=None , SCREAMING_SNAKE_CASE__ : Tuple=False , **SCREAMING_SNAKE_CASE__ : str , ): # Mask token behave like a normal word, i.e. include the space before it lowerCamelCase__ = AddedToken(SCREAMING_SNAKE_CASE__ , lstrip=SCREAMING_SNAKE_CASE__ , rstrip=SCREAMING_SNAKE_CASE__ ) if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) else mask_token lowerCamelCase__ = legacy_behaviour super().__init__( vocab_file=SCREAMING_SNAKE_CASE__ , tokenizer_file=SCREAMING_SNAKE_CASE__ , bos_token=SCREAMING_SNAKE_CASE__ , eos_token=SCREAMING_SNAKE_CASE__ , sep_token=SCREAMING_SNAKE_CASE__ , cls_token=SCREAMING_SNAKE_CASE__ , unk_token=SCREAMING_SNAKE_CASE__ , pad_token=SCREAMING_SNAKE_CASE__ , mask_token=SCREAMING_SNAKE_CASE__ , src_lang=SCREAMING_SNAKE_CASE__ , tgt_lang=SCREAMING_SNAKE_CASE__ , additional_special_tokens=SCREAMING_SNAKE_CASE__ , legacy_behaviour=SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ , ) lowerCamelCase__ = vocab_file lowerCamelCase__ = False if not self.vocab_file else True lowerCamelCase__ = FAIRSEQ_LANGUAGE_CODES.copy() if additional_special_tokens is not None: # Only add those special tokens if they are not already there. _additional_special_tokens.extend( [t for t in additional_special_tokens if t not in _additional_special_tokens] ) self.add_special_tokens({'additional_special_tokens': _additional_special_tokens} ) lowerCamelCase__ = { lang_code: self.convert_tokens_to_ids(SCREAMING_SNAKE_CASE__ ) for lang_code in FAIRSEQ_LANGUAGE_CODES } lowerCamelCase__ = src_lang if src_lang is not None else 'eng_Latn' lowerCamelCase__ = self.convert_tokens_to_ids(self._src_lang ) lowerCamelCase__ = tgt_lang self.set_src_lang_special_tokens(self._src_lang ) @property def _UpperCamelCase ( self : str ): return self._src_lang @src_lang.setter def _UpperCamelCase ( self : int , SCREAMING_SNAKE_CASE__ : str ): lowerCamelCase__ = new_src_lang self.set_src_lang_special_tokens(self._src_lang ) def _UpperCamelCase ( self : Tuple , SCREAMING_SNAKE_CASE__ : List[int] , SCREAMING_SNAKE_CASE__ : Optional[List[int]] = None ): if token_ids_a is None: return self.prefix_tokens + token_ids_a + self.suffix_tokens # We don't expect to process pairs, but leave the pair logic for API consistency return self.prefix_tokens + token_ids_a + token_ids_a + self.suffix_tokens def _UpperCamelCase ( self : Optional[Any] , SCREAMING_SNAKE_CASE__ : List[int] , SCREAMING_SNAKE_CASE__ : Optional[List[int]] = None ): lowerCamelCase__ = [self.sep_token_id] lowerCamelCase__ = [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 _UpperCamelCase ( self : str , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : Optional[str] , SCREAMING_SNAKE_CASE__ : Optional[str] , **SCREAMING_SNAKE_CASE__ : Optional[int] ): if src_lang is None or tgt_lang is None: raise ValueError('Translation requires a `src_lang` and a `tgt_lang` for this model' ) lowerCamelCase__ = src_lang lowerCamelCase__ = self(SCREAMING_SNAKE_CASE__ , add_special_tokens=SCREAMING_SNAKE_CASE__ , return_tensors=SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = self.convert_tokens_to_ids(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = tgt_lang_id return inputs def _UpperCamelCase ( self : str , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : str = "eng_Latn" , SCREAMING_SNAKE_CASE__ : Optional[List[str]] = None , SCREAMING_SNAKE_CASE__ : str = "fra_Latn" , **SCREAMING_SNAKE_CASE__ : Dict , ): lowerCamelCase__ = src_lang lowerCamelCase__ = tgt_lang return super().prepare_seqaseq_batch(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : List[str] ): return self.set_src_lang_special_tokens(self.src_lang ) def _UpperCamelCase ( self : List[Any] ): return self.set_tgt_lang_special_tokens(self.tgt_lang ) def _UpperCamelCase ( self : str , SCREAMING_SNAKE_CASE__ : Union[str, Any] ): lowerCamelCase__ = self.convert_tokens_to_ids(SCREAMING_SNAKE_CASE__ ) if self.legacy_behaviour: lowerCamelCase__ = [] lowerCamelCase__ = [self.eos_token_id, self.cur_lang_code] else: lowerCamelCase__ = [self.cur_lang_code] lowerCamelCase__ = [self.eos_token_id] lowerCamelCase__ = self.convert_ids_to_tokens(self.prefix_tokens ) lowerCamelCase__ = self.convert_ids_to_tokens(self.suffix_tokens ) lowerCamelCase__ = processors.TemplateProcessing( single=prefix_tokens_str + ['$A'] + suffix_tokens_str , pair=prefix_tokens_str + ['$A', '$B'] + suffix_tokens_str , special_tokens=list(zip(prefix_tokens_str + suffix_tokens_str , self.prefix_tokens + self.suffix_tokens ) ) , ) def _UpperCamelCase ( self : Union[str, Any] , SCREAMING_SNAKE_CASE__ : str ): lowerCamelCase__ = self.convert_tokens_to_ids(SCREAMING_SNAKE_CASE__ ) if self.legacy_behaviour: lowerCamelCase__ = [] lowerCamelCase__ = [self.eos_token_id, self.cur_lang_code] else: lowerCamelCase__ = [self.cur_lang_code] lowerCamelCase__ = [self.eos_token_id] lowerCamelCase__ = self.convert_ids_to_tokens(self.prefix_tokens ) lowerCamelCase__ = self.convert_ids_to_tokens(self.suffix_tokens ) lowerCamelCase__ = processors.TemplateProcessing( single=prefix_tokens_str + ['$A'] + suffix_tokens_str , pair=prefix_tokens_str + ['$A', '$B'] + suffix_tokens_str , special_tokens=list(zip(prefix_tokens_str + suffix_tokens_str , self.prefix_tokens + self.suffix_tokens ) ) , ) def _UpperCamelCase ( self : Dict , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : Optional[str] = None ): if not self.can_save_slow_tokenizer: raise ValueError( 'Your fast tokenizer does not have the necessary information to save the vocabulary for a slow ' 'tokenizer.' ) if not os.path.isdir(SCREAMING_SNAKE_CASE__ ): logger.error(F'Vocabulary path ({save_directory}) should be a directory.' ) return lowerCamelCase__ = os.path.join( SCREAMING_SNAKE_CASE__ , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(SCREAMING_SNAKE_CASE__ ): copyfile(self.vocab_file , SCREAMING_SNAKE_CASE__ ) return (out_vocab_file,)
659
1
"""simple docstring""" import random def snake_case ( _a: int , _a: float , _a: bool = False )-> dict: '''simple docstring''' lowerCamelCase__ = {i: [] for i in range(_a )} # if probability is greater or equal than 1, then generate a complete graph if probability >= 1: return complete_graph(_a ) # if probability is lower or equal than 0, then return a graph without edges if probability <= 0: return graph # for each couple of nodes, add an edge from u to v # if the number randomly generated is greater than probability probability for i in range(_a ): for j in range(i + 1 , _a ): if random.random() < probability: graph[i].append(_a ) if not directed: # if the graph is undirected, add an edge in from j to i, either graph[j].append(_a ) return graph def snake_case ( _a: int )-> dict: '''simple docstring''' return { i: [j for j in range(_a ) if i != j] for i in range(_a ) } if __name__ == "__main__": import doctest doctest.testmod()
659
"""simple docstring""" from __future__ import annotations import unittest import numpy as np from transformers import BlipTextConfig from transformers.testing_utils import require_tf, slow from transformers.utils import is_tf_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask if is_tf_available(): import tensorflow as tf from transformers import TFBlipTextModel from transformers.models.blip.modeling_tf_blip import TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST class _a : def __init__( self : List[str] , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : Union[str, Any]=12 , SCREAMING_SNAKE_CASE__ : Union[str, Any]=7 , SCREAMING_SNAKE_CASE__ : Union[str, Any]=True , SCREAMING_SNAKE_CASE__ : Optional[Any]=True , SCREAMING_SNAKE_CASE__ : str=True , SCREAMING_SNAKE_CASE__ : Any=99 , SCREAMING_SNAKE_CASE__ : Optional[Any]=32 , SCREAMING_SNAKE_CASE__ : str=32 , SCREAMING_SNAKE_CASE__ : Tuple=2 , SCREAMING_SNAKE_CASE__ : str=4 , SCREAMING_SNAKE_CASE__ : Optional[Any]=37 , SCREAMING_SNAKE_CASE__ : Union[str, Any]=0.1 , SCREAMING_SNAKE_CASE__ : Optional[Any]=0.1 , SCREAMING_SNAKE_CASE__ : str=5_12 , SCREAMING_SNAKE_CASE__ : str=0.02 , SCREAMING_SNAKE_CASE__ : Union[str, Any]=0 , SCREAMING_SNAKE_CASE__ : Any=None , ): lowerCamelCase__ = parent lowerCamelCase__ = batch_size lowerCamelCase__ = seq_length lowerCamelCase__ = is_training lowerCamelCase__ = use_input_mask lowerCamelCase__ = use_labels lowerCamelCase__ = vocab_size lowerCamelCase__ = hidden_size lowerCamelCase__ = projection_dim lowerCamelCase__ = num_hidden_layers lowerCamelCase__ = num_attention_heads lowerCamelCase__ = intermediate_size lowerCamelCase__ = dropout lowerCamelCase__ = attention_dropout lowerCamelCase__ = max_position_embeddings lowerCamelCase__ = initializer_range lowerCamelCase__ = scope lowerCamelCase__ = bos_token_id def _UpperCamelCase ( self : int ): lowerCamelCase__ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) lowerCamelCase__ = None if self.use_input_mask: lowerCamelCase__ = random_attention_mask([self.batch_size, self.seq_length] ) if input_mask is not None: lowerCamelCase__ = input_mask.numpy() lowerCamelCase__ , lowerCamelCase__ = input_mask.shape lowerCamelCase__ = np.random.randint(1 , seq_length - 1 , size=(batch_size,) ) for batch_idx, start_index in enumerate(SCREAMING_SNAKE_CASE__ ): lowerCamelCase__ = 1 lowerCamelCase__ = 0 lowerCamelCase__ = self.get_config() return config, input_ids, tf.convert_to_tensor(SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : Any ): return BlipTextConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , projection_dim=self.projection_dim , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , dropout=self.dropout , attention_dropout=self.attention_dropout , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , bos_token_id=self.bos_token_id , ) def _UpperCamelCase ( self : Any , SCREAMING_SNAKE_CASE__ : Any , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : List[str] ): lowerCamelCase__ = TFBlipTextModel(config=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = model(SCREAMING_SNAKE_CASE__ , attention_mask=SCREAMING_SNAKE_CASE__ , training=SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = model(SCREAMING_SNAKE_CASE__ , training=SCREAMING_SNAKE_CASE__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size) ) def _UpperCamelCase ( self : Dict ): lowerCamelCase__ = self.prepare_config_and_inputs() lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ = config_and_inputs lowerCamelCase__ = {'input_ids': input_ids, 'attention_mask': input_mask} return config, inputs_dict @require_tf class _a ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): a_ : str = (TFBlipTextModel,) if is_tf_available() else () a_ : List[str] = False a_ : Optional[Any] = False a_ : Union[str, Any] = False def _UpperCamelCase ( self : str ): lowerCamelCase__ = BlipTextModelTester(self ) lowerCamelCase__ = ConfigTester(self , config_class=SCREAMING_SNAKE_CASE__ , hidden_size=37 ) def _UpperCamelCase ( self : Tuple ): self.config_tester.run_common_tests() def _UpperCamelCase ( self : str ): lowerCamelCase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : Tuple ): pass def _UpperCamelCase ( self : Tuple ): pass @unittest.skip(reason='Blip does not use inputs_embeds' ) def _UpperCamelCase ( self : List[str] ): pass @unittest.skip(reason='BlipTextModel has no base class and is not available in MODEL_MAPPING' ) def _UpperCamelCase ( self : Dict ): pass @unittest.skip(reason='BlipTextModel has no base class and is not available in MODEL_MAPPING' ) def _UpperCamelCase ( self : List[Any] ): pass @slow def _UpperCamelCase ( self : str ): for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowerCamelCase__ = TFBlipTextModel.from_pretrained(SCREAMING_SNAKE_CASE__ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE__ ) def _UpperCamelCase ( self : Optional[int] , SCREAMING_SNAKE_CASE__ : List[Any]=True ): super().test_pt_tf_model_equivalence(allow_missing_keys=SCREAMING_SNAKE_CASE__ )
659
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available _snake_case = { "configuration_mvp": ["MVP_PRETRAINED_CONFIG_ARCHIVE_MAP", "MvpConfig", "MvpOnnxConfig"], "tokenization_mvp": ["MvpTokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = ["MvpTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = [ "MVP_PRETRAINED_MODEL_ARCHIVE_LIST", "MvpForCausalLM", "MvpForConditionalGeneration", "MvpForQuestionAnswering", "MvpForSequenceClassification", "MvpModel", "MvpPreTrainedModel", ] if TYPE_CHECKING: from .configuration_mvp import MVP_PRETRAINED_CONFIG_ARCHIVE_MAP, MvpConfig, MvpOnnxConfig from .tokenization_mvp import MvpTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_mvp_fast import MvpTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mvp import ( MVP_PRETRAINED_MODEL_ARCHIVE_LIST, MvpForCausalLM, MvpForConditionalGeneration, MvpForQuestionAnswering, MvpForSequenceClassification, MvpModel, MvpPreTrainedModel, ) else: import sys _snake_case = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
659
"""simple docstring""" import warnings from diffusers import StableDiffusionImgaImgPipeline # noqa F401 warnings.warn( "The `image_to_image.py` script is outdated. Please use directly `from diffusers import" " StableDiffusionImg2ImgPipeline` instead." )
659
1
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging _snake_case = logging.get_logger(__name__) _snake_case = { "microsoft/swinv2-tiny-patch4-window8-256": ( "https://huggingface.co/microsoft/swinv2-tiny-patch4-window8-256/resolve/main/config.json" ), } class _a ( SCREAMING_SNAKE_CASE_ ): a_ : Union[str, Any] = 'swinv2' a_ : Optional[int] = { 'num_attention_heads': 'num_heads', 'num_hidden_layers': 'num_layers', } def __init__( self : List[str] , SCREAMING_SNAKE_CASE__ : int=2_24 , SCREAMING_SNAKE_CASE__ : str=4 , SCREAMING_SNAKE_CASE__ : List[Any]=3 , SCREAMING_SNAKE_CASE__ : str=96 , SCREAMING_SNAKE_CASE__ : Dict=[2, 2, 6, 2] , SCREAMING_SNAKE_CASE__ : Union[str, Any]=[3, 6, 12, 24] , SCREAMING_SNAKE_CASE__ : Tuple=7 , SCREAMING_SNAKE_CASE__ : Union[str, Any]=4.0 , SCREAMING_SNAKE_CASE__ : List[str]=True , SCREAMING_SNAKE_CASE__ : List[Any]=0.0 , SCREAMING_SNAKE_CASE__ : Optional[int]=0.0 , SCREAMING_SNAKE_CASE__ : Dict=0.1 , SCREAMING_SNAKE_CASE__ : Tuple="gelu" , SCREAMING_SNAKE_CASE__ : Tuple=False , SCREAMING_SNAKE_CASE__ : Any=0.02 , SCREAMING_SNAKE_CASE__ : List[Any]=1e-5 , SCREAMING_SNAKE_CASE__ : int=32 , **SCREAMING_SNAKE_CASE__ : List[str] , ): super().__init__(**SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = image_size lowerCamelCase__ = patch_size lowerCamelCase__ = num_channels lowerCamelCase__ = embed_dim lowerCamelCase__ = depths lowerCamelCase__ = len(SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = num_heads lowerCamelCase__ = window_size lowerCamelCase__ = mlp_ratio lowerCamelCase__ = qkv_bias lowerCamelCase__ = hidden_dropout_prob lowerCamelCase__ = attention_probs_dropout_prob lowerCamelCase__ = drop_path_rate lowerCamelCase__ = hidden_act lowerCamelCase__ = use_absolute_embeddings lowerCamelCase__ = layer_norm_eps lowerCamelCase__ = initializer_range lowerCamelCase__ = encoder_stride # we set the hidden_size attribute in order to make Swinv2 work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model lowerCamelCase__ = int(embed_dim * 2 ** (len(SCREAMING_SNAKE_CASE__ ) - 1) ) lowerCamelCase__ = (0, 0, 0, 0)
659
"""simple docstring""" import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_funnel import FunnelTokenizer _snake_case = logging.get_logger(__name__) _snake_case = {"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"} _snake_case = [ "small", "small-base", "medium", "medium-base", "intermediate", "intermediate-base", "large", "large-base", "xlarge", "xlarge-base", ] _snake_case = { "vocab_file": { "funnel-transformer/small": "https://huggingface.co/funnel-transformer/small/resolve/main/vocab.txt", "funnel-transformer/small-base": "https://huggingface.co/funnel-transformer/small-base/resolve/main/vocab.txt", "funnel-transformer/medium": "https://huggingface.co/funnel-transformer/medium/resolve/main/vocab.txt", "funnel-transformer/medium-base": ( "https://huggingface.co/funnel-transformer/medium-base/resolve/main/vocab.txt" ), "funnel-transformer/intermediate": ( "https://huggingface.co/funnel-transformer/intermediate/resolve/main/vocab.txt" ), "funnel-transformer/intermediate-base": ( "https://huggingface.co/funnel-transformer/intermediate-base/resolve/main/vocab.txt" ), "funnel-transformer/large": "https://huggingface.co/funnel-transformer/large/resolve/main/vocab.txt", "funnel-transformer/large-base": "https://huggingface.co/funnel-transformer/large-base/resolve/main/vocab.txt", "funnel-transformer/xlarge": "https://huggingface.co/funnel-transformer/xlarge/resolve/main/vocab.txt", "funnel-transformer/xlarge-base": ( "https://huggingface.co/funnel-transformer/xlarge-base/resolve/main/vocab.txt" ), }, "tokenizer_file": { "funnel-transformer/small": "https://huggingface.co/funnel-transformer/small/resolve/main/tokenizer.json", "funnel-transformer/small-base": ( "https://huggingface.co/funnel-transformer/small-base/resolve/main/tokenizer.json" ), "funnel-transformer/medium": "https://huggingface.co/funnel-transformer/medium/resolve/main/tokenizer.json", "funnel-transformer/medium-base": ( "https://huggingface.co/funnel-transformer/medium-base/resolve/main/tokenizer.json" ), "funnel-transformer/intermediate": ( "https://huggingface.co/funnel-transformer/intermediate/resolve/main/tokenizer.json" ), "funnel-transformer/intermediate-base": ( "https://huggingface.co/funnel-transformer/intermediate-base/resolve/main/tokenizer.json" ), "funnel-transformer/large": "https://huggingface.co/funnel-transformer/large/resolve/main/tokenizer.json", "funnel-transformer/large-base": ( "https://huggingface.co/funnel-transformer/large-base/resolve/main/tokenizer.json" ), "funnel-transformer/xlarge": "https://huggingface.co/funnel-transformer/xlarge/resolve/main/tokenizer.json", "funnel-transformer/xlarge-base": ( "https://huggingface.co/funnel-transformer/xlarge-base/resolve/main/tokenizer.json" ), }, } _snake_case = {f"""funnel-transformer/{name}""": 512 for name in _model_names} _snake_case = {f"""funnel-transformer/{name}""": {"do_lower_case": True} for name in _model_names} class _a ( SCREAMING_SNAKE_CASE_ ): a_ : List[str] = VOCAB_FILES_NAMES a_ : List[str] = PRETRAINED_VOCAB_FILES_MAP a_ : Optional[int] = PRETRAINED_INIT_CONFIGURATION a_ : List[str] = FunnelTokenizer a_ : Dict = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES a_ : int = 2 def __init__( self : int , SCREAMING_SNAKE_CASE__ : Optional[int]=None , SCREAMING_SNAKE_CASE__ : Union[str, Any]=None , SCREAMING_SNAKE_CASE__ : int=True , SCREAMING_SNAKE_CASE__ : Any="<unk>" , SCREAMING_SNAKE_CASE__ : List[Any]="<sep>" , SCREAMING_SNAKE_CASE__ : int="<pad>" , SCREAMING_SNAKE_CASE__ : Tuple="<cls>" , SCREAMING_SNAKE_CASE__ : Tuple="<mask>" , SCREAMING_SNAKE_CASE__ : Any="<s>" , SCREAMING_SNAKE_CASE__ : Tuple="</s>" , SCREAMING_SNAKE_CASE__ : str=True , SCREAMING_SNAKE_CASE__ : Any=True , SCREAMING_SNAKE_CASE__ : str=None , SCREAMING_SNAKE_CASE__ : int="##" , **SCREAMING_SNAKE_CASE__ : Any , ): super().__init__( SCREAMING_SNAKE_CASE__ , tokenizer_file=SCREAMING_SNAKE_CASE__ , do_lower_case=SCREAMING_SNAKE_CASE__ , unk_token=SCREAMING_SNAKE_CASE__ , sep_token=SCREAMING_SNAKE_CASE__ , pad_token=SCREAMING_SNAKE_CASE__ , cls_token=SCREAMING_SNAKE_CASE__ , mask_token=SCREAMING_SNAKE_CASE__ , bos_token=SCREAMING_SNAKE_CASE__ , eos_token=SCREAMING_SNAKE_CASE__ , clean_text=SCREAMING_SNAKE_CASE__ , tokenize_chinese_chars=SCREAMING_SNAKE_CASE__ , strip_accents=SCREAMING_SNAKE_CASE__ , wordpieces_prefix=SCREAMING_SNAKE_CASE__ , **SCREAMING_SNAKE_CASE__ , ) lowerCamelCase__ = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get('lowercase' , SCREAMING_SNAKE_CASE__ ) != do_lower_case or normalizer_state.get('strip_accents' , SCREAMING_SNAKE_CASE__ ) != strip_accents or normalizer_state.get('handle_chinese_chars' , SCREAMING_SNAKE_CASE__ ) != tokenize_chinese_chars ): lowerCamelCase__ = getattr(SCREAMING_SNAKE_CASE__ , normalizer_state.pop('type' ) ) lowerCamelCase__ = do_lower_case lowerCamelCase__ = strip_accents lowerCamelCase__ = tokenize_chinese_chars lowerCamelCase__ = normalizer_class(**SCREAMING_SNAKE_CASE__ ) lowerCamelCase__ = do_lower_case def _UpperCamelCase ( self : Dict , SCREAMING_SNAKE_CASE__ : Tuple , SCREAMING_SNAKE_CASE__ : Optional[Any]=None ): lowerCamelCase__ = [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 _UpperCamelCase ( self : Dict , SCREAMING_SNAKE_CASE__ : List[int] , SCREAMING_SNAKE_CASE__ : Optional[List[int]] = None ): lowerCamelCase__ = [self.sep_token_id] lowerCamelCase__ = [self.cls_token_id] if token_ids_a is None: return len(cls ) * [self.cls_token_type_id] + len(token_ids_a + sep ) * [0] return len(cls ) * [self.cls_token_type_id] + len(token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def _UpperCamelCase ( self : Optional[int] , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : Optional[str] = None ): lowerCamelCase__ = self._tokenizer.model.save(SCREAMING_SNAKE_CASE__ , name=SCREAMING_SNAKE_CASE__ ) return tuple(SCREAMING_SNAKE_CASE__ )
659
1