code
stringlengths
87
55.2k
code_codestyle
int64
0
349
style_context
stringlengths
135
49.1k
style_context_codestyle
int64
0
349
label
int64
0
1
def SCREAMING_SNAKE_CASE_ ( snake_case__ ) -> int: lowerCAmelCase = 0 while num > 0: digit_sum += num % 1_0 num //= 1_0 return digit_sum def SCREAMING_SNAKE_CASE_ ( snake_case__ = 1_0_0 ) -> int: lowerCAmelCase = 1 lowerCAmelCase = 2 for i in range(2 , max_n + 1 ): lowerCAmelCase = pre_numerator lowerCAmelCase = 2 * i // 3 if i % 3 == 0 else 1 lowerCAmelCase = cur_numerator lowerCAmelCase = e_cont * pre_numerator + temp return sum_digits(snake_case__ ) if __name__ == "__main__": print(f'{solution() = }')
338
def SCREAMING_SNAKE_CASE_ ( snake_case__ ) -> List[str]: lowerCAmelCase = len(snake_case__ ) for i in range(length - 1 ): lowerCAmelCase = i for k in range(i + 1 , snake_case__ ): if collection[k] < collection[least]: lowerCAmelCase = k if least != i: lowerCAmelCase , lowerCAmelCase = (collection[i], collection[least]) return collection if __name__ == "__main__": lowercase__ : Optional[int] = input('''Enter numbers separated by a comma:\n''').strip() lowercase__ : str = [int(item) for item in user_input.split(''',''')] print(selection_sort(unsorted))
338
1
import math import sys import cva import numpy as np def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ ) -> np.ndarray: # For applying gaussian function for each element in matrix. lowerCAmelCase = math.sqrt(snake_case__ ) lowerCAmelCase = 1 / (sigma * math.sqrt(2 * math.pi )) return cons * np.exp(-((img / sigma) ** 2) * 0.5 ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ ) -> np.ndarray: lowerCAmelCase = kernel_size // 2 return img[x - half : x + half + 1, y - half : y + half + 1] def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ ) -> np.ndarray: # Creates a gaussian kernel of given dimension. lowerCAmelCase = np.zeros((kernel_size, kernel_size) ) for i in range(0 , snake_case__ ): for j in range(0 , snake_case__ ): lowerCAmelCase = math.sqrt( abs(i - kernel_size // 2 ) ** 2 + abs(j - kernel_size // 2 ) ** 2 ) return vec_gaussian(snake_case__ , snake_case__ ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ , ) -> np.ndarray: lowerCAmelCase = np.zeros(img.shape ) lowerCAmelCase = get_gauss_kernel(snake_case__ , snake_case__ ) lowerCAmelCase , lowerCAmelCase = img.shape for i in range(kernel_size // 2 , size_x - kernel_size // 2 ): for j in range(kernel_size // 2 , size_y - kernel_size // 2 ): lowerCAmelCase = get_slice(snake_case__ , snake_case__ , snake_case__ , snake_case__ ) lowerCAmelCase = img_s - img_s[kernel_size // 2, kernel_size // 2] lowerCAmelCase = vec_gaussian(snake_case__ , snake_case__ ) lowerCAmelCase = np.multiply(snake_case__ , snake_case__ ) lowerCAmelCase = np.multiply(snake_case__ , snake_case__ ) lowerCAmelCase = np.sum(snake_case__ ) / np.sum(snake_case__ ) lowerCAmelCase = val return imga def SCREAMING_SNAKE_CASE_ ( snake_case__ ) -> tuple: lowerCAmelCase = args[1] if args[1:] else '''../image_data/lena.jpg''' lowerCAmelCase = float(args[2] ) if args[2:] else 1.0 lowerCAmelCase = float(args[3] ) if args[3:] else 1.0 if args[4:]: lowerCAmelCase = int(args[4] ) lowerCAmelCase = kernel_size + abs(kernel_size % 2 - 1 ) else: lowerCAmelCase = 5 return filename, spatial_variance, intensity_variance, kernel_size if __name__ == "__main__": lowercase__ , lowercase__ , lowercase__ , lowercase__ : Optional[int] = parse_args(sys.argv) lowercase__ : Optional[int] = cva.imread(filename, 0) cva.imshow('''input image''', img) lowercase__ : List[str] = img / 2_5_5 lowercase__ : int = out.astype('''float32''') lowercase__ : int = bilateral_filter(out, spatial_variance, intensity_variance, kernel_size) lowercase__ : Optional[int] = out * 2_5_5 lowercase__ : Union[str, Any] = np.uinta(out) cva.imshow('''output image''', out) cva.waitKey(0) cva.destroyAllWindows()
338
import unittest from transformers import EsmConfig, is_torch_available from transformers.testing_utils import TestCasePlus, require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers.models.esm.modeling_esmfold import EsmForProteinFolding class lowercase_ : """simple docstring""" def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=13 , __SCREAMING_SNAKE_CASE=7 , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=19 , __SCREAMING_SNAKE_CASE=32 , __SCREAMING_SNAKE_CASE=5 , __SCREAMING_SNAKE_CASE=4 , __SCREAMING_SNAKE_CASE=37 , __SCREAMING_SNAKE_CASE="gelu" , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=512 , __SCREAMING_SNAKE_CASE=16 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=0.0_2 , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=4 , __SCREAMING_SNAKE_CASE=None , ) ->Union[str, Any]: 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 SCREAMING_SNAKE_CASE_ ( self ) ->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 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, input_mask, sequence_labels, token_labels, choice_labels def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[int]: lowerCAmelCase = EsmConfig( vocab_size=33 , hidden_size=self.hidden_size , pad_token_id=1 , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , is_folding_model=__SCREAMING_SNAKE_CASE , esmfold_config={'''trunk''': {'''num_blocks''': 2}, '''fp16_esm''': False} , ) return config def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ->Tuple: lowerCAmelCase = EsmForProteinFolding(config=__SCREAMING_SNAKE_CASE ).float() model.to(__SCREAMING_SNAKE_CASE ) model.eval() lowerCAmelCase = model(__SCREAMING_SNAKE_CASE , attention_mask=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = model(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = model(__SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.positions.shape , (8, self.batch_size, self.seq_length, 14, 3) ) self.parent.assertEqual(result.angles.shape , (8, self.batch_size, self.seq_length, 7, 2) ) def SCREAMING_SNAKE_CASE_ ( self ) ->int: lowerCAmelCase = self.prepare_config_and_inputs() ( ( lowerCAmelCase ) , ( lowerCAmelCase ) , ( lowerCAmelCase ) , ( lowerCAmelCase ) , ( lowerCAmelCase ) , ( lowerCAmelCase ) , ) = config_and_inputs lowerCAmelCase = {'''input_ids''': input_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_torch class lowercase_ ( UpperCamelCase_ , UpperCamelCase_ , unittest.TestCase ): """simple docstring""" UpperCAmelCase_ : Optional[Any] = False UpperCAmelCase_ : Dict = (EsmForProteinFolding,) if is_torch_available() else () UpperCAmelCase_ : List[Any] = () UpperCAmelCase_ : Tuple = {} if is_torch_available() else {} UpperCAmelCase_ : List[str] = False def SCREAMING_SNAKE_CASE_ ( self ) ->Dict: lowerCAmelCase = EsmFoldModelTester(self ) lowerCAmelCase = ConfigTester(self , config_class=__SCREAMING_SNAKE_CASE , hidden_size=37 ) def SCREAMING_SNAKE_CASE_ ( self ) ->Any: self.config_tester.run_common_tests() def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[int]: lowerCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE ) @unittest.skip('''Does not support attention outputs''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->Tuple: pass @unittest.skip def SCREAMING_SNAKE_CASE_ ( self ) ->Any: pass @unittest.skip('''Esm does not support embedding resizing''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->List[str]: pass @unittest.skip('''Esm does not support embedding resizing''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->str: pass @unittest.skip('''ESMFold does not support passing input embeds!''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->List[Any]: pass @unittest.skip('''ESMFold does not support head pruning.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->str: pass @unittest.skip('''ESMFold does not support head pruning.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->Dict: pass @unittest.skip('''ESMFold does not support head pruning.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->List[str]: pass @unittest.skip('''ESMFold does not support head pruning.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->List[Any]: pass @unittest.skip('''ESMFold does not support head pruning.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[int]: pass @unittest.skip('''ESMFold does not output hidden states in the normal way.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->Tuple: pass @unittest.skip('''ESMfold does not output hidden states in the normal way.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->Dict: pass @unittest.skip('''ESMFold only has one output format.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->List[str]: pass @unittest.skip('''This test doesn\'t work for ESMFold and doesn\'t test core functionality''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->Dict: pass @unittest.skip('''ESMFold does not support input chunking.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->Union[str, Any]: pass @unittest.skip('''ESMFold doesn\'t respect you and it certainly doesn\'t respect your initialization arguments.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[Any]: pass @unittest.skip('''ESMFold doesn\'t support torchscript compilation.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->str: pass @unittest.skip('''ESMFold doesn\'t support torchscript compilation.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->Any: pass @unittest.skip('''ESMFold doesn\'t support torchscript compilation.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->str: pass @unittest.skip('''ESMFold doesn\'t support data parallel.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->str: pass @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[int]: pass @require_torch class lowercase_ ( UpperCamelCase_ ): """simple docstring""" @slow def SCREAMING_SNAKE_CASE_ ( self ) ->str: lowerCAmelCase = EsmForProteinFolding.from_pretrained('''facebook/esmfold_v1''' ).float() model.eval() lowerCAmelCase = torch.tensor([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]] ) lowerCAmelCase = model(__SCREAMING_SNAKE_CASE )['''positions'''] lowerCAmelCase = torch.tensor([2.5_8_2_8, 0.7_9_9_3, -1_0.9_3_3_4] , dtype=torch.floataa ) self.assertTrue(torch.allclose(position_outputs[0, 0, 0, 0] , __SCREAMING_SNAKE_CASE , atol=1e-4 ) )
338
1
import argparse import collections import json import os import re import string import sys import numpy as np lowercase__ : Tuple = re.compile(R'''\b(a|an|the)\b''', re.UNICODE) lowercase__ : str = None def SCREAMING_SNAKE_CASE_ ( ) -> Optional[int]: lowerCAmelCase = argparse.ArgumentParser('''Official evaluation script for SQuAD version 2.0.''' ) parser.add_argument('''data_file''' , metavar='''data.json''' , help='''Input data JSON file.''' ) parser.add_argument('''pred_file''' , metavar='''pred.json''' , help='''Model predictions.''' ) parser.add_argument( '''--out-file''' , '''-o''' , metavar='''eval.json''' , help='''Write accuracy metrics to file (default is stdout).''' ) parser.add_argument( '''--na-prob-file''' , '''-n''' , metavar='''na_prob.json''' , help='''Model estimates of probability of no answer.''' ) parser.add_argument( '''--na-prob-thresh''' , '''-t''' , type=snake_case__ , default=1.0 , help='''Predict "" if no-answer probability exceeds this (default = 1.0).''' , ) parser.add_argument( '''--out-image-dir''' , '''-p''' , metavar='''out_images''' , default=snake_case__ , help='''Save precision-recall curves to directory.''' ) parser.add_argument('''--verbose''' , '''-v''' , action='''store_true''' ) if len(sys.argv ) == 1: parser.print_help() sys.exit(1 ) return parser.parse_args() def SCREAMING_SNAKE_CASE_ ( snake_case__ ) -> List[Any]: lowerCAmelCase = {} for article in dataset: for p in article["paragraphs"]: for qa in p["qas"]: lowerCAmelCase = bool(qa['''answers''']['''text'''] ) return qid_to_has_ans def SCREAMING_SNAKE_CASE_ ( snake_case__ ) -> Tuple: def remove_articles(snake_case__ ): return ARTICLES_REGEX.sub(''' ''' , snake_case__ ) def white_space_fix(snake_case__ ): return " ".join(text.split() ) def remove_punc(snake_case__ ): lowerCAmelCase = set(string.punctuation ) return "".join(ch for ch in text if ch not in exclude ) def lower(snake_case__ ): return text.lower() return white_space_fix(remove_articles(remove_punc(lower(snake_case__ ) ) ) ) def SCREAMING_SNAKE_CASE_ ( snake_case__ ) -> Union[str, Any]: if not s: return [] return normalize_answer(snake_case__ ).split() def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ ) -> int: return int(normalize_answer(snake_case__ ) == normalize_answer(snake_case__ ) ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ ) -> Optional[Any]: lowerCAmelCase = get_tokens(snake_case__ ) lowerCAmelCase = get_tokens(snake_case__ ) lowerCAmelCase = collections.Counter(snake_case__ ) & collections.Counter(snake_case__ ) lowerCAmelCase = sum(common.values() ) if len(snake_case__ ) == 0 or len(snake_case__ ) == 0: # If either is no-answer, then F1 is 1 if they agree, 0 otherwise return int(gold_toks == pred_toks ) if num_same == 0: return 0 lowerCAmelCase = 1.0 * num_same / len(snake_case__ ) lowerCAmelCase = 1.0 * num_same / len(snake_case__ ) lowerCAmelCase = (2 * precision * recall) / (precision + recall) return fa def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ ) -> Union[str, Any]: lowerCAmelCase = {} lowerCAmelCase = {} for article in dataset: for p in article["paragraphs"]: for qa in p["qas"]: lowerCAmelCase = qa['''id'''] lowerCAmelCase = [t for t in qa['''answers''']['''text'''] if normalize_answer(snake_case__ )] if not gold_answers: # For unanswerable questions, only correct answer is empty string lowerCAmelCase = [''''''] if qid not in preds: print(f"Missing prediction for {qid}" ) continue lowerCAmelCase = preds[qid] # Take max over all gold answers lowerCAmelCase = max(compute_exact(snake_case__ , snake_case__ ) for a in gold_answers ) lowerCAmelCase = max(compute_fa(snake_case__ , snake_case__ ) for a in gold_answers ) return exact_scores, fa_scores def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ ) -> Tuple: lowerCAmelCase = {} for qid, s in scores.items(): lowerCAmelCase = na_probs[qid] > na_prob_thresh if pred_na: lowerCAmelCase = float(not qid_to_has_ans[qid] ) else: lowerCAmelCase = s return new_scores def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__=None ) -> Union[str, Any]: if not qid_list: lowerCAmelCase = len(snake_case__ ) return collections.OrderedDict( [ ('''exact''', 1_00.0 * sum(exact_scores.values() ) / total), ('''f1''', 1_00.0 * sum(fa_scores.values() ) / total), ('''total''', total), ] ) else: lowerCAmelCase = len(snake_case__ ) return collections.OrderedDict( [ ('''exact''', 1_00.0 * sum(exact_scores[k] for k in qid_list ) / total), ('''f1''', 1_00.0 * sum(fa_scores[k] for k in qid_list ) / total), ('''total''', total), ] ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> Optional[int]: for k in new_eval: lowerCAmelCase = new_eval[k] def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ ) -> Dict: plt.step(snake_case__ , snake_case__ , color='''b''' , alpha=0.2 , where='''post''' ) plt.fill_between(snake_case__ , snake_case__ , step='''post''' , alpha=0.2 , color='''b''' ) plt.xlabel('''Recall''' ) plt.ylabel('''Precision''' ) plt.xlim([0.0, 1.05] ) plt.ylim([0.0, 1.05] ) plt.title(snake_case__ ) plt.savefig(snake_case__ ) plt.clf() def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__=None , snake_case__=None ) -> Optional[Any]: lowerCAmelCase = sorted(snake_case__ , key=lambda snake_case__ : na_probs[k] ) lowerCAmelCase = 0.0 lowerCAmelCase = 1.0 lowerCAmelCase = 0.0 lowerCAmelCase = [1.0] lowerCAmelCase = [0.0] lowerCAmelCase = 0.0 for i, qid in enumerate(snake_case__ ): if qid_to_has_ans[qid]: true_pos += scores[qid] lowerCAmelCase = true_pos / float(i + 1 ) lowerCAmelCase = true_pos / float(snake_case__ ) if i == len(snake_case__ ) - 1 or na_probs[qid] != na_probs[qid_list[i + 1]]: # i.e., if we can put a threshold after this point avg_prec += cur_p * (cur_r - recalls[-1]) precisions.append(snake_case__ ) recalls.append(snake_case__ ) if out_image: plot_pr_curve(snake_case__ , snake_case__ , snake_case__ , snake_case__ ) return {"ap": 1_00.0 * avg_prec} def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ ) -> Any: if out_image_dir and not os.path.exists(snake_case__ ): os.makedirs(snake_case__ ) lowerCAmelCase = sum(1 for v in qid_to_has_ans.values() if v ) if num_true_pos == 0: return lowerCAmelCase = make_precision_recall_eval( snake_case__ , snake_case__ , snake_case__ , snake_case__ , out_image=os.path.join(snake_case__ , '''pr_exact.png''' ) , title='''Precision-Recall curve for Exact Match score''' , ) lowerCAmelCase = make_precision_recall_eval( snake_case__ , snake_case__ , snake_case__ , snake_case__ , out_image=os.path.join(snake_case__ , '''pr_f1.png''' ) , title='''Precision-Recall curve for F1 score''' , ) lowerCAmelCase = {k: float(snake_case__ ) for k, v in qid_to_has_ans.items()} lowerCAmelCase = make_precision_recall_eval( snake_case__ , snake_case__ , snake_case__ , snake_case__ , out_image=os.path.join(snake_case__ , '''pr_oracle.png''' ) , title='''Oracle Precision-Recall curve (binary task of HasAns vs. NoAns)''' , ) merge_eval(snake_case__ , snake_case__ , '''pr_exact''' ) merge_eval(snake_case__ , snake_case__ , '''pr_f1''' ) merge_eval(snake_case__ , snake_case__ , '''pr_oracle''' ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ ) -> Union[str, Any]: if not qid_list: return lowerCAmelCase = [na_probs[k] for k in qid_list] lowerCAmelCase = np.ones_like(snake_case__ ) / float(len(snake_case__ ) ) plt.hist(snake_case__ , weights=snake_case__ , bins=2_0 , range=(0.0, 1.0) ) plt.xlabel('''Model probability of no-answer''' ) plt.ylabel('''Proportion of dataset''' ) plt.title(f"Histogram of no-answer probability: {name}" ) plt.savefig(os.path.join(snake_case__ , f"na_prob_hist_{name}.png" ) ) plt.clf() def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ ) -> Dict: lowerCAmelCase = sum(1 for k in qid_to_has_ans if not qid_to_has_ans[k] ) lowerCAmelCase = num_no_ans lowerCAmelCase = cur_score lowerCAmelCase = 0.0 lowerCAmelCase = sorted(snake_case__ , key=lambda snake_case__ : na_probs[k] ) for i, qid in enumerate(snake_case__ ): if qid not in scores: continue if qid_to_has_ans[qid]: lowerCAmelCase = scores[qid] else: if preds[qid]: lowerCAmelCase = -1 else: lowerCAmelCase = 0 cur_score += diff if cur_score > best_score: lowerCAmelCase = cur_score lowerCAmelCase = na_probs[qid] return 1_00.0 * best_score / len(snake_case__ ), best_thresh def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ ) -> List[Any]: lowerCAmelCase , lowerCAmelCase = find_best_thresh(snake_case__ , snake_case__ , snake_case__ , snake_case__ ) lowerCAmelCase , lowerCAmelCase = find_best_thresh(snake_case__ , snake_case__ , snake_case__ , snake_case__ ) lowerCAmelCase = best_exact lowerCAmelCase = exact_thresh lowerCAmelCase = best_fa lowerCAmelCase = fa_thresh def SCREAMING_SNAKE_CASE_ ( ) -> List[Any]: with open(OPTS.data_file ) as f: lowerCAmelCase = json.load(snake_case__ ) lowerCAmelCase = dataset_json['''data'''] with open(OPTS.pred_file ) as f: lowerCAmelCase = json.load(snake_case__ ) if OPTS.na_prob_file: with open(OPTS.na_prob_file ) as f: lowerCAmelCase = json.load(snake_case__ ) else: lowerCAmelCase = {k: 0.0 for k in preds} lowerCAmelCase = make_qid_to_has_ans(snake_case__ ) # maps qid to True/False lowerCAmelCase = [k for k, v in qid_to_has_ans.items() if v] lowerCAmelCase = [k for k, v in qid_to_has_ans.items() if not v] lowerCAmelCase , lowerCAmelCase = get_raw_scores(snake_case__ , snake_case__ ) lowerCAmelCase = apply_no_ans_threshold(snake_case__ , snake_case__ , snake_case__ , OPTS.na_prob_thresh ) lowerCAmelCase = apply_no_ans_threshold(snake_case__ , snake_case__ , snake_case__ , OPTS.na_prob_thresh ) lowerCAmelCase = make_eval_dict(snake_case__ , snake_case__ ) if has_ans_qids: lowerCAmelCase = make_eval_dict(snake_case__ , snake_case__ , qid_list=snake_case__ ) merge_eval(snake_case__ , snake_case__ , '''HasAns''' ) if no_ans_qids: lowerCAmelCase = make_eval_dict(snake_case__ , snake_case__ , qid_list=snake_case__ ) merge_eval(snake_case__ , snake_case__ , '''NoAns''' ) if OPTS.na_prob_file: find_all_best_thresh(snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ ) if OPTS.na_prob_file and OPTS.out_image_dir: run_precision_recall_analysis(snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , OPTS.out_image_dir ) histogram_na_prob(snake_case__ , snake_case__ , OPTS.out_image_dir , '''hasAns''' ) histogram_na_prob(snake_case__ , snake_case__ , OPTS.out_image_dir , '''noAns''' ) if OPTS.out_file: with open(OPTS.out_file , '''w''' ) as f: json.dump(snake_case__ , snake_case__ ) else: print(json.dumps(snake_case__ , indent=2 ) ) if __name__ == "__main__": lowercase__ : int = parse_args() if OPTS.out_image_dir: import matplotlib matplotlib.use('''Agg''') import matplotlib.pyplot as plt main()
338
import warnings from typing import List import numpy as np from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding from ...utils import is_flax_available, is_tf_available, is_torch_available class lowercase_ ( UpperCamelCase_ ): """simple docstring""" UpperCAmelCase_ : List[str] = ["""image_processor""", """tokenizer"""] UpperCAmelCase_ : int = """OwlViTImageProcessor""" UpperCAmelCase_ : Any = ("""CLIPTokenizer""", """CLIPTokenizerFast""") def __init__( self , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE ) ->Any: lowerCAmelCase = None if "feature_extractor" in kwargs: warnings.warn( '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`''' ''' instead.''' , __SCREAMING_SNAKE_CASE , ) lowerCAmelCase = kwargs.pop('''feature_extractor''' ) lowerCAmelCase = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError('''You need to specify an `image_processor`.''' ) if tokenizer is None: raise ValueError('''You need to specify a `tokenizer`.''' ) super().__init__(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def __call__( self , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE="max_length" , __SCREAMING_SNAKE_CASE="np" , **__SCREAMING_SNAKE_CASE ) ->int: if text is None and query_images is None and images is None: raise ValueError( '''You have to specify at least one text or query image or image. All three cannot be none.''' ) if text is not None: if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) or (isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) and not isinstance(text[0] , __SCREAMING_SNAKE_CASE )): lowerCAmelCase = [self.tokenizer(__SCREAMING_SNAKE_CASE , padding=__SCREAMING_SNAKE_CASE , return_tensors=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE )] elif isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) and isinstance(text[0] , __SCREAMING_SNAKE_CASE ): lowerCAmelCase = [] # Maximum number of queries across batch lowerCAmelCase = max([len(__SCREAMING_SNAKE_CASE ) for t in text] ) # Pad all batch samples to max number of text queries for t in text: if len(__SCREAMING_SNAKE_CASE ) != max_num_queries: lowerCAmelCase = t + [''' '''] * (max_num_queries - len(__SCREAMING_SNAKE_CASE )) lowerCAmelCase = self.tokenizer(__SCREAMING_SNAKE_CASE , padding=__SCREAMING_SNAKE_CASE , return_tensors=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) encodings.append(__SCREAMING_SNAKE_CASE ) else: raise TypeError('''Input text should be a string, a list of strings or a nested list of strings''' ) if return_tensors == "np": lowerCAmelCase = np.concatenate([encoding['''input_ids'''] for encoding in encodings] , axis=0 ) lowerCAmelCase = np.concatenate([encoding['''attention_mask'''] for encoding in encodings] , axis=0 ) elif return_tensors == "jax" and is_flax_available(): import jax.numpy as jnp lowerCAmelCase = jnp.concatenate([encoding['''input_ids'''] for encoding in encodings] , axis=0 ) lowerCAmelCase = jnp.concatenate([encoding['''attention_mask'''] for encoding in encodings] , axis=0 ) elif return_tensors == "pt" and is_torch_available(): import torch lowerCAmelCase = torch.cat([encoding['''input_ids'''] for encoding in encodings] , dim=0 ) lowerCAmelCase = torch.cat([encoding['''attention_mask'''] for encoding in encodings] , dim=0 ) elif return_tensors == "tf" and is_tf_available(): import tensorflow as tf lowerCAmelCase = tf.stack([encoding['''input_ids'''] for encoding in encodings] , axis=0 ) lowerCAmelCase = tf.stack([encoding['''attention_mask'''] for encoding in encodings] , axis=0 ) else: raise ValueError('''Target return tensor type could not be returned''' ) lowerCAmelCase = BatchEncoding() lowerCAmelCase = input_ids lowerCAmelCase = attention_mask if query_images is not None: lowerCAmelCase = BatchEncoding() lowerCAmelCase = self.image_processor( __SCREAMING_SNAKE_CASE , return_tensors=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).pixel_values lowerCAmelCase = query_pixel_values if images is not None: lowerCAmelCase = self.image_processor(__SCREAMING_SNAKE_CASE , return_tensors=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) if text is not None and images is not None: lowerCAmelCase = image_features.pixel_values return encoding elif query_images is not None and images is not None: lowerCAmelCase = image_features.pixel_values return encoding elif text is not None or query_images is not None: return encoding else: return BatchEncoding(data=dict(**__SCREAMING_SNAKE_CASE ) , tensor_type=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) ->Optional[int]: return self.image_processor.post_process(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) ->Any: return self.image_processor.post_process_object_detection(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) ->Tuple: return self.image_processor.post_process_image_guided_detection(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) ->str: return self.tokenizer.batch_decode(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) ->List[Any]: return self.tokenizer.decode(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) @property def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[int]: warnings.warn( '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.''' , __SCREAMING_SNAKE_CASE , ) return self.image_processor_class @property def SCREAMING_SNAKE_CASE_ ( self ) ->int: warnings.warn( '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.''' , __SCREAMING_SNAKE_CASE , ) return self.image_processor
338
1
def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> float: return round(float(moles / volume ) * nfactor ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> float: return round(float((moles * 0.08_21 * temperature) / (volume) ) ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> float: return round(float((moles * 0.08_21 * temperature) / (pressure) ) ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> float: return round(float((pressure * volume) / (0.08_21 * moles) ) ) if __name__ == "__main__": import doctest doctest.testmod()
338
import os import unicodedata from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging lowercase__ : List[Any] = logging.get_logger(__name__) lowercase__ : Optional[Any] = {'''vocab_file''': '''spiece.model'''} lowercase__ : Optional[int] = { '''vocab_file''': { '''albert-base-v1''': '''https://huggingface.co/albert-base-v1/resolve/main/spiece.model''', '''albert-large-v1''': '''https://huggingface.co/albert-large-v1/resolve/main/spiece.model''', '''albert-xlarge-v1''': '''https://huggingface.co/albert-xlarge-v1/resolve/main/spiece.model''', '''albert-xxlarge-v1''': '''https://huggingface.co/albert-xxlarge-v1/resolve/main/spiece.model''', '''albert-base-v2''': '''https://huggingface.co/albert-base-v2/resolve/main/spiece.model''', '''albert-large-v2''': '''https://huggingface.co/albert-large-v2/resolve/main/spiece.model''', '''albert-xlarge-v2''': '''https://huggingface.co/albert-xlarge-v2/resolve/main/spiece.model''', '''albert-xxlarge-v2''': '''https://huggingface.co/albert-xxlarge-v2/resolve/main/spiece.model''', } } lowercase__ : Any = { '''albert-base-v1''': 5_1_2, '''albert-large-v1''': 5_1_2, '''albert-xlarge-v1''': 5_1_2, '''albert-xxlarge-v1''': 5_1_2, '''albert-base-v2''': 5_1_2, '''albert-large-v2''': 5_1_2, '''albert-xlarge-v2''': 5_1_2, '''albert-xxlarge-v2''': 5_1_2, } lowercase__ : Tuple = '''▁''' class lowercase_ ( UpperCamelCase_ ): """simple docstring""" UpperCAmelCase_ : Dict = VOCAB_FILES_NAMES UpperCAmelCase_ : Tuple = PRETRAINED_VOCAB_FILES_MAP UpperCAmelCase_ : Optional[int] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE="[CLS]" , __SCREAMING_SNAKE_CASE="[SEP]" , __SCREAMING_SNAKE_CASE="<unk>" , __SCREAMING_SNAKE_CASE="[SEP]" , __SCREAMING_SNAKE_CASE="<pad>" , __SCREAMING_SNAKE_CASE="[CLS]" , __SCREAMING_SNAKE_CASE="[MASK]" , __SCREAMING_SNAKE_CASE = None , **__SCREAMING_SNAKE_CASE , ) ->None: # Mask token behave like a normal word, i.e. include the space before it and # is included in the raw text, there should be a match in a non-normalized sentence. lowerCAmelCase = ( AddedToken(__SCREAMING_SNAKE_CASE , lstrip=__SCREAMING_SNAKE_CASE , rstrip=__SCREAMING_SNAKE_CASE , normalized=__SCREAMING_SNAKE_CASE ) if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else mask_token ) lowerCAmelCase = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( do_lower_case=__SCREAMING_SNAKE_CASE , remove_space=__SCREAMING_SNAKE_CASE , keep_accents=__SCREAMING_SNAKE_CASE , bos_token=__SCREAMING_SNAKE_CASE , eos_token=__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 , sp_model_kwargs=self.sp_model_kwargs , **__SCREAMING_SNAKE_CASE , ) lowerCAmelCase = do_lower_case lowerCAmelCase = remove_space lowerCAmelCase = keep_accents lowerCAmelCase = vocab_file lowerCAmelCase = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(__SCREAMING_SNAKE_CASE ) @property def SCREAMING_SNAKE_CASE_ ( self ) ->Any: return len(self.sp_model ) def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[Any]: 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 , __SCREAMING_SNAKE_CASE ) ->Tuple: lowerCAmelCase = d # for backward compatibility if not hasattr(self , '''sp_model_kwargs''' ): lowerCAmelCase = {} lowerCAmelCase = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE ) ->Any: if self.remove_space: lowerCAmelCase = ''' '''.join(inputs.strip().split() ) else: lowerCAmelCase = inputs lowerCAmelCase = outputs.replace('''``''' , '''"''' ).replace('''\'\'''' , '''"''' ) if not self.keep_accents: lowerCAmelCase = unicodedata.normalize('''NFKD''' , __SCREAMING_SNAKE_CASE ) lowerCAmelCase = ''''''.join([c for c in outputs if not unicodedata.combining(__SCREAMING_SNAKE_CASE )] ) if self.do_lower_case: lowerCAmelCase = outputs.lower() return outputs def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE ) ->List[str]: lowerCAmelCase = self.preprocess_text(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = self.sp_model.encode(__SCREAMING_SNAKE_CASE , out_type=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = [] for piece in pieces: if len(__SCREAMING_SNAKE_CASE ) > 1 and piece[-1] == str(''',''' ) and piece[-2].isdigit(): lowerCAmelCase = self.sp_model.EncodeAsPieces(piece[:-1].replace(__SCREAMING_SNAKE_CASE , '''''' ) ) if piece[0] != SPIECE_UNDERLINE and cur_pieces[0][0] == SPIECE_UNDERLINE: if len(cur_pieces[0] ) == 1: lowerCAmelCase = cur_pieces[1:] else: lowerCAmelCase = cur_pieces[0][1:] cur_pieces.append(piece[-1] ) new_pieces.extend(__SCREAMING_SNAKE_CASE ) else: new_pieces.append(__SCREAMING_SNAKE_CASE ) return new_pieces def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE ) ->int: return self.sp_model.PieceToId(__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE ) ->int: return self.sp_model.IdToPiece(__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE ) ->Optional[int]: lowerCAmelCase = [] lowerCAmelCase = '''''' lowerCAmelCase = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: if not prev_is_special: out_string += " " out_string += self.sp_model.decode(__SCREAMING_SNAKE_CASE ) + token lowerCAmelCase = True lowerCAmelCase = [] else: current_sub_tokens.append(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = False out_string += self.sp_model.decode(__SCREAMING_SNAKE_CASE ) return out_string.strip() def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ) ->List[int]: lowerCAmelCase = [self.sep_token_id] lowerCAmelCase = [self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = False ) ->List[int]: 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 not None: return [1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1] return [1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1] def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ) ->List[int]: 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 ) * [0] + len(token_ids_a + sep ) * [1] def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ) ->Tuple[str]: 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 ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , __SCREAMING_SNAKE_CASE ) elif not os.path.isfile(self.vocab_file ): with open(__SCREAMING_SNAKE_CASE , '''wb''' ) as fi: lowerCAmelCase = self.sp_model.serialized_model_proto() fi.write(__SCREAMING_SNAKE_CASE ) return (out_vocab_file,)
338
1
def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ ) -> Any: global f # a global dp table for knapsack if f[i][j] < 0: if j < wt[i - 1]: lowerCAmelCase = mf_knapsack(i - 1 , snake_case__ , snake_case__ , snake_case__ ) else: lowerCAmelCase = max( mf_knapsack(i - 1 , snake_case__ , snake_case__ , snake_case__ ) , mf_knapsack(i - 1 , snake_case__ , snake_case__ , j - wt[i - 1] ) + val[i - 1] , ) lowerCAmelCase = val return f[i][j] def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ ) -> Tuple: 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 SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> List[str]: if not (isinstance(snake_case__ , (list, tuple) ) and isinstance(snake_case__ , (list, tuple) )): raise ValueError( '''Both the weights and values vectors must be either lists or tuples''' ) lowerCAmelCase = len(snake_case__ ) if num_items != len(snake_case__ ): lowerCAmelCase = ( '''The number of weights must be the same as the number of values.\n''' f"But got {num_items} weights and {len(snake_case__ )} values" ) raise ValueError(snake_case__ ) for i in range(snake_case__ ): if not isinstance(wt[i] , snake_case__ ): lowerCAmelCase = ( '''All weights must be integers but got weight of ''' f"type {type(wt[i] )} at index {i}" ) raise TypeError(snake_case__ ) lowerCAmelCase , lowerCAmelCase = knapsack(snake_case__ , snake_case__ , snake_case__ , snake_case__ ) lowerCAmelCase = set() _construct_solution(snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ ) return optimal_val, example_optional_set def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ ) -> str: # for the current item i at a maximum weight j to be part of an optimal subset, # the optimal value at (i, j) must be greater than the optimal value at (i-1, j). # where i - 1 means considering only the previous items at the given maximum weight if i > 0 and j > 0: if dp[i - 1][j] == dp[i][j]: _construct_solution(snake_case__ , snake_case__ , i - 1 , snake_case__ , snake_case__ ) else: optimal_set.add(snake_case__ ) _construct_solution(snake_case__ , snake_case__ , i - 1 , j - wt[i - 1] , snake_case__ ) if __name__ == "__main__": lowercase__ : Optional[Any] = [3, 2, 4, 4] lowercase__ : Tuple = [4, 3, 2, 3] lowercase__ : Optional[int] = 4 lowercase__ : Optional[Any] = 6 lowercase__ : Tuple = [[0] * (w + 1)] + [[0] + [-1] * (w + 1) for _ in range(n + 1)] lowercase__ , lowercase__ : List[str] = knapsack(w, wt, val, n) print(optimal_solution) print(mf_knapsack(n, wt, val, w)) # switched the n and w # testing the dynamic programming problem with example # the optimal subset for the above example are items 3 and 4 lowercase__ , lowercase__ : str = 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)
338
import tempfile import torch from diffusers import ( DEISMultistepScheduler, DPMSolverMultistepScheduler, DPMSolverSinglestepScheduler, UniPCMultistepScheduler, ) from .test_schedulers import SchedulerCommonTest class lowercase_ ( UpperCamelCase_ ): """simple docstring""" UpperCAmelCase_ : List[Any] = (DEISMultistepScheduler,) UpperCAmelCase_ : int = (("""num_inference_steps""", 25),) def SCREAMING_SNAKE_CASE_ ( self , **__SCREAMING_SNAKE_CASE ) ->str: lowerCAmelCase = { '''num_train_timesteps''': 1000, '''beta_start''': 0.0_0_0_1, '''beta_end''': 0.0_2, '''beta_schedule''': '''linear''', '''solver_order''': 2, } config.update(**__SCREAMING_SNAKE_CASE ) return config def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE=0 , **__SCREAMING_SNAKE_CASE ) ->Tuple: lowerCAmelCase = dict(self.forward_default_kwargs ) lowerCAmelCase = kwargs.pop('''num_inference_steps''' , __SCREAMING_SNAKE_CASE ) lowerCAmelCase = self.dummy_sample lowerCAmelCase = 0.1 * sample lowerCAmelCase = [residual + 0.2, residual + 0.1_5, residual + 0.1_0] for scheduler_class in self.scheduler_classes: lowerCAmelCase = self.get_scheduler_config(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) # copy over dummy past residuals lowerCAmelCase = dummy_past_residuals[: scheduler.config.solver_order] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = scheduler_class.from_pretrained(__SCREAMING_SNAKE_CASE ) new_scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) # copy over dummy past residuals lowerCAmelCase = dummy_past_residuals[: new_scheduler.config.solver_order] lowerCAmelCase , lowerCAmelCase = sample, sample for t in range(__SCREAMING_SNAKE_CASE , time_step + scheduler.config.solver_order + 1 ): lowerCAmelCase = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).prev_sample lowerCAmelCase = new_scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1e-5, "Scheduler outputs are not identical" def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[int]: pass def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE=0 , **__SCREAMING_SNAKE_CASE ) ->List[Any]: lowerCAmelCase = dict(self.forward_default_kwargs ) lowerCAmelCase = kwargs.pop('''num_inference_steps''' , __SCREAMING_SNAKE_CASE ) lowerCAmelCase = self.dummy_sample lowerCAmelCase = 0.1 * sample lowerCAmelCase = [residual + 0.2, residual + 0.1_5, residual + 0.1_0] for scheduler_class in self.scheduler_classes: lowerCAmelCase = self.get_scheduler_config() lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) # copy over dummy past residuals (must be after setting timesteps) lowerCAmelCase = dummy_past_residuals[: scheduler.config.solver_order] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = scheduler_class.from_pretrained(__SCREAMING_SNAKE_CASE ) # copy over dummy past residuals new_scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) # copy over dummy past residual (must be after setting timesteps) lowerCAmelCase = dummy_past_residuals[: new_scheduler.config.solver_order] lowerCAmelCase = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).prev_sample lowerCAmelCase = new_scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1e-5, "Scheduler outputs are not identical" def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE ) ->List[Any]: if scheduler is None: lowerCAmelCase = self.scheduler_classes[0] lowerCAmelCase = self.get_scheduler_config(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = self.scheduler_classes[0] lowerCAmelCase = self.get_scheduler_config(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = 10 lowerCAmelCase = self.dummy_model() lowerCAmelCase = self.dummy_sample_deter scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) for i, t in enumerate(scheduler.timesteps ): lowerCAmelCase = model(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) lowerCAmelCase = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ).prev_sample return sample def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[int]: lowerCAmelCase = dict(self.forward_default_kwargs ) lowerCAmelCase = kwargs.pop('''num_inference_steps''' , __SCREAMING_SNAKE_CASE ) for scheduler_class in self.scheduler_classes: lowerCAmelCase = self.get_scheduler_config() lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = self.dummy_sample lowerCAmelCase = 0.1 * sample if num_inference_steps is not None and hasattr(__SCREAMING_SNAKE_CASE , '''set_timesteps''' ): scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) elif num_inference_steps is not None and not hasattr(__SCREAMING_SNAKE_CASE , '''set_timesteps''' ): lowerCAmelCase = num_inference_steps # copy over dummy past residuals (must be done after set_timesteps) lowerCAmelCase = [residual + 0.2, residual + 0.1_5, residual + 0.1_0] lowerCAmelCase = dummy_past_residuals[: scheduler.config.solver_order] lowerCAmelCase = scheduler.timesteps[5] lowerCAmelCase = scheduler.timesteps[6] lowerCAmelCase = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).prev_sample lowerCAmelCase = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).prev_sample self.assertEqual(output_a.shape , sample.shape ) self.assertEqual(output_a.shape , output_a.shape ) def SCREAMING_SNAKE_CASE_ ( self ) ->int: # make sure that iterating over schedulers with same config names gives same results # for defaults lowerCAmelCase = DEISMultistepScheduler(**self.get_scheduler_config() ) lowerCAmelCase = self.full_loop(scheduler=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = torch.mean(torch.abs(__SCREAMING_SNAKE_CASE ) ) assert abs(result_mean.item() - 0.2_3_9_1_6 ) < 1e-3 lowerCAmelCase = DPMSolverSinglestepScheduler.from_config(scheduler.config ) lowerCAmelCase = DPMSolverMultistepScheduler.from_config(scheduler.config ) lowerCAmelCase = UniPCMultistepScheduler.from_config(scheduler.config ) lowerCAmelCase = DEISMultistepScheduler.from_config(scheduler.config ) lowerCAmelCase = self.full_loop(scheduler=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = torch.mean(torch.abs(__SCREAMING_SNAKE_CASE ) ) assert abs(result_mean.item() - 0.2_3_9_1_6 ) < 1e-3 def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[int]: for timesteps in [25, 50, 100, 999, 1000]: self.check_over_configs(num_train_timesteps=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->int: self.check_over_configs(thresholding=__SCREAMING_SNAKE_CASE ) for order in [1, 2, 3]: for solver_type in ["logrho"]: for threshold in [0.5, 1.0, 2.0]: for prediction_type in ["epsilon", "sample"]: self.check_over_configs( thresholding=__SCREAMING_SNAKE_CASE , prediction_type=__SCREAMING_SNAKE_CASE , sample_max_value=__SCREAMING_SNAKE_CASE , algorithm_type='''deis''' , solver_order=__SCREAMING_SNAKE_CASE , solver_type=__SCREAMING_SNAKE_CASE , ) def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[Any]: for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->Union[str, Any]: for algorithm_type in ["deis"]: for solver_type in ["logrho"]: for order in [1, 2, 3]: for prediction_type in ["epsilon", "sample"]: self.check_over_configs( solver_order=__SCREAMING_SNAKE_CASE , solver_type=__SCREAMING_SNAKE_CASE , prediction_type=__SCREAMING_SNAKE_CASE , algorithm_type=__SCREAMING_SNAKE_CASE , ) lowerCAmelCase = self.full_loop( solver_order=__SCREAMING_SNAKE_CASE , solver_type=__SCREAMING_SNAKE_CASE , prediction_type=__SCREAMING_SNAKE_CASE , algorithm_type=__SCREAMING_SNAKE_CASE , ) assert not torch.isnan(__SCREAMING_SNAKE_CASE ).any(), "Samples have nan numbers" def SCREAMING_SNAKE_CASE_ ( self ) ->List[Any]: self.check_over_configs(lower_order_final=__SCREAMING_SNAKE_CASE ) self.check_over_configs(lower_order_final=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->List[Any]: for num_inference_steps in [1, 2, 3, 5, 10, 50, 100, 999, 1000]: self.check_over_forward(num_inference_steps=__SCREAMING_SNAKE_CASE , time_step=0 ) def SCREAMING_SNAKE_CASE_ ( self ) ->Dict: lowerCAmelCase = self.full_loop() lowerCAmelCase = torch.mean(torch.abs(__SCREAMING_SNAKE_CASE ) ) assert abs(result_mean.item() - 0.2_3_9_1_6 ) < 1e-3 def SCREAMING_SNAKE_CASE_ ( self ) ->List[str]: lowerCAmelCase = self.full_loop(prediction_type='''v_prediction''' ) lowerCAmelCase = torch.mean(torch.abs(__SCREAMING_SNAKE_CASE ) ) assert abs(result_mean.item() - 0.0_9_1 ) < 1e-3 def SCREAMING_SNAKE_CASE_ ( self ) ->Dict: lowerCAmelCase = self.scheduler_classes[0] lowerCAmelCase = self.get_scheduler_config(thresholding=__SCREAMING_SNAKE_CASE , dynamic_thresholding_ratio=0 ) lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = 10 lowerCAmelCase = self.dummy_model() lowerCAmelCase = self.dummy_sample_deter.half() scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) for i, t in enumerate(scheduler.timesteps ): lowerCAmelCase = model(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) lowerCAmelCase = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ).prev_sample assert sample.dtype == torch.floataa
338
1
def SCREAMING_SNAKE_CASE_ ( snake_case__ ) -> int: if n == 1 or not isinstance(snake_case__ , snake_case__ ): return 0 elif n == 2: return 1 else: lowerCAmelCase = [0, 1] for i in range(2 , n + 1 ): sequence.append(sequence[i - 1] + sequence[i - 2] ) return sequence[n] def SCREAMING_SNAKE_CASE_ ( snake_case__ ) -> int: lowerCAmelCase = 0 lowerCAmelCase = 2 while digits < n: index += 1 lowerCAmelCase = len(str(fibonacci(snake_case__ ) ) ) return index def SCREAMING_SNAKE_CASE_ ( snake_case__ = 1_0_0_0 ) -> int: return fibonacci_digits_index(snake_case__ ) if __name__ == "__main__": print(solution(int(str(input()).strip())))
338
import unittest import numpy as np import torch from diffusers import KarrasVePipeline, KarrasVeScheduler, UNetaDModel from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device enable_full_determinism() class lowercase_ ( unittest.TestCase ): """simple docstring""" @property def SCREAMING_SNAKE_CASE_ ( self ) ->List[str]: torch.manual_seed(0 ) lowerCAmelCase = UNetaDModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=3 , out_channels=3 , down_block_types=('''DownBlock2D''', '''AttnDownBlock2D''') , up_block_types=('''AttnUpBlock2D''', '''UpBlock2D''') , ) return model def SCREAMING_SNAKE_CASE_ ( self ) ->int: lowerCAmelCase = self.dummy_uncond_unet lowerCAmelCase = KarrasVeScheduler() lowerCAmelCase = KarrasVePipeline(unet=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = torch.manual_seed(0 ) lowerCAmelCase = pipe(num_inference_steps=2 , generator=__SCREAMING_SNAKE_CASE , output_type='''numpy''' ).images lowerCAmelCase = torch.manual_seed(0 ) lowerCAmelCase = pipe(num_inference_steps=2 , generator=__SCREAMING_SNAKE_CASE , output_type='''numpy''' , return_dict=__SCREAMING_SNAKE_CASE )[0] lowerCAmelCase = image[0, -3:, -3:, -1] lowerCAmelCase = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) lowerCAmelCase = np.array([0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 @slow @require_torch class lowercase_ ( unittest.TestCase ): """simple docstring""" def SCREAMING_SNAKE_CASE_ ( self ) ->List[Any]: lowerCAmelCase = '''google/ncsnpp-celebahq-256''' lowerCAmelCase = UNetaDModel.from_pretrained(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = KarrasVeScheduler() lowerCAmelCase = KarrasVePipeline(unet=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = torch.manual_seed(0 ) lowerCAmelCase = pipe(num_inference_steps=20 , generator=__SCREAMING_SNAKE_CASE , output_type='''numpy''' ).images lowerCAmelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 256, 256, 3) lowerCAmelCase = np.array([0.5_7_8, 0.5_8_1_1, 0.5_9_2_4, 0.5_8_0_9, 0.5_8_7, 0.5_8_8_6, 0.5_8_6_1, 0.5_8_0_2, 0.5_8_6] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
338
1
from __future__ import annotations # This is the precision for this function which can be altered. # It is recommended for users to keep this number greater than or equal to 10. lowercase__ : List[Any] = 1_0 def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ ) -> int: for i in range(snake_case__ , snake_case__ ): if array[i] == target: return i return -1 def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ ) -> int: lowerCAmelCase = 0 lowerCAmelCase = len(snake_case__ ) while left <= right: if right - left < precision: return lin_search(snake_case__ , snake_case__ , snake_case__ , snake_case__ ) lowerCAmelCase = (left + right) // 3 + 1 lowerCAmelCase = 2 * (left + right) // 3 + 1 if array[one_third] == target: return one_third elif array[two_third] == target: return two_third elif target < array[one_third]: lowerCAmelCase = one_third - 1 elif array[two_third] < target: lowerCAmelCase = two_third + 1 else: lowerCAmelCase = one_third + 1 lowerCAmelCase = two_third - 1 else: return -1 def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ ) -> int: if left < right: if right - left < precision: return lin_search(snake_case__ , snake_case__ , snake_case__ , snake_case__ ) lowerCAmelCase = (left + right) // 3 + 1 lowerCAmelCase = 2 * (left + right) // 3 + 1 if array[one_third] == target: return one_third elif array[two_third] == target: return two_third elif target < array[one_third]: return rec_ternary_search(snake_case__ , one_third - 1 , snake_case__ , snake_case__ ) elif array[two_third] < target: return rec_ternary_search(two_third + 1 , snake_case__ , snake_case__ , snake_case__ ) else: return rec_ternary_search(one_third + 1 , two_third - 1 , snake_case__ , snake_case__ ) else: return -1 if __name__ == "__main__": import doctest doctest.testmod() lowercase__ : Tuple = input('''Enter numbers separated by comma:\n''').strip() lowercase__ : Optional[int] = [int(item.strip()) for item in user_input.split(''',''')] assert collection == sorted(collection), f"List must be ordered.\n{collection}." lowercase__ : Any = int(input('''Enter the number to be found in the list:\n''').strip()) lowercase__ : str = ite_ternary_search(collection, target) lowercase__ : Optional[Any] = rec_ternary_search(0, len(collection) - 1, collection, target) if resulta != -1: print(f'Iterative search: {target} found at positions: {resulta}') print(f'Recursive search: {target} found at positions: {resulta}') else: print('''Not found''')
338
from typing import Dict import numpy as np from ..utils import add_end_docstrings, is_tf_available, is_torch_available, logging from .base import PIPELINE_INIT_ARGS, GenericTensor, Pipeline, PipelineException if is_tf_available(): import tensorflow as tf from ..tf_utils import stable_softmax if is_torch_available(): import torch lowercase__ : Dict = logging.get_logger(__name__) @add_end_docstrings( UpperCamelCase_ , r""" top_k (`int`, defaults to 5): The number of predictions to return. targets (`str` or `List[str]`, *optional*): When passed, the model will limit the scores to the passed targets instead of looking up in the whole vocab. If the provided targets are not in the model vocab, they will be tokenized and the first resulting token will be used (with a warning, and that might be slower). """ , ) class lowercase_ ( UpperCamelCase_ ): """simple docstring""" def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE ) ->np.ndarray: if self.framework == "tf": lowerCAmelCase = tf.where(input_ids == self.tokenizer.mask_token_id ).numpy() elif self.framework == "pt": lowerCAmelCase = torch.nonzero(input_ids == self.tokenizer.mask_token_id , as_tuple=__SCREAMING_SNAKE_CASE ) else: raise ValueError('''Unsupported framework''' ) return masked_index def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE ) ->np.ndarray: lowerCAmelCase = self.get_masked_index(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = np.prod(masked_index.shape ) if numel < 1: raise PipelineException( '''fill-mask''' , self.model.base_model_prefix , F"No mask_token ({self.tokenizer.mask_token}) found on the input" , ) def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE ) ->str: if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): for model_input in model_inputs: self._ensure_exactly_one_mask_token(model_input['''input_ids'''][0] ) else: for input_ids in model_inputs["input_ids"]: self._ensure_exactly_one_mask_token(__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE ) ->Dict[str, GenericTensor]: if return_tensors is None: lowerCAmelCase = self.framework lowerCAmelCase = self.tokenizer(__SCREAMING_SNAKE_CASE , return_tensors=__SCREAMING_SNAKE_CASE ) self.ensure_exactly_one_mask_token(__SCREAMING_SNAKE_CASE ) return model_inputs def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE ) ->Tuple: lowerCAmelCase = self.model(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = model_inputs['''input_ids'''] return model_outputs def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=5 , __SCREAMING_SNAKE_CASE=None ) ->str: # Cap top_k if there are targets if target_ids is not None and target_ids.shape[0] < top_k: lowerCAmelCase = target_ids.shape[0] lowerCAmelCase = model_outputs['''input_ids'''][0] lowerCAmelCase = model_outputs['''logits'''] if self.framework == "tf": lowerCAmelCase = tf.where(input_ids == self.tokenizer.mask_token_id ).numpy()[:, 0] lowerCAmelCase = outputs.numpy() lowerCAmelCase = outputs[0, masked_index, :] lowerCAmelCase = stable_softmax(__SCREAMING_SNAKE_CASE , axis=-1 ) if target_ids is not None: lowerCAmelCase = tf.gather_nd(tf.squeeze(__SCREAMING_SNAKE_CASE , 0 ) , target_ids.reshape(-1 , 1 ) ) lowerCAmelCase = tf.expand_dims(__SCREAMING_SNAKE_CASE , 0 ) lowerCAmelCase = tf.math.top_k(__SCREAMING_SNAKE_CASE , k=__SCREAMING_SNAKE_CASE ) lowerCAmelCase , lowerCAmelCase = topk.values.numpy(), topk.indices.numpy() else: lowerCAmelCase = torch.nonzero(input_ids == self.tokenizer.mask_token_id , as_tuple=__SCREAMING_SNAKE_CASE ).squeeze(-1 ) # Fill mask pipeline supports only one ${mask_token} per sample lowerCAmelCase = outputs[0, masked_index, :] lowerCAmelCase = logits.softmax(dim=-1 ) if target_ids is not None: lowerCAmelCase = probs[..., target_ids] lowerCAmelCase , lowerCAmelCase = probs.topk(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = [] lowerCAmelCase = values.shape[0] == 1 for i, (_values, _predictions) in enumerate(zip(values.tolist() , predictions.tolist() ) ): lowerCAmelCase = [] for v, p in zip(_values , _predictions ): # Copy is important since we're going to modify this array in place lowerCAmelCase = input_ids.numpy().copy() if target_ids is not None: lowerCAmelCase = target_ids[p].tolist() lowerCAmelCase = p # Filter padding out: lowerCAmelCase = tokens[np.where(tokens != self.tokenizer.pad_token_id )] # Originally we skip special tokens to give readable output. # For multi masks though, the other [MASK] would be removed otherwise # making the output look odd, so we add them back lowerCAmelCase = self.tokenizer.decode(__SCREAMING_SNAKE_CASE , skip_special_tokens=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = {'''score''': v, '''token''': p, '''token_str''': self.tokenizer.decode([p] ), '''sequence''': sequence} row.append(__SCREAMING_SNAKE_CASE ) result.append(__SCREAMING_SNAKE_CASE ) if single_mask: return result[0] return result def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None ) ->Optional[Any]: if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): lowerCAmelCase = [targets] try: lowerCAmelCase = self.tokenizer.get_vocab() except Exception: lowerCAmelCase = {} lowerCAmelCase = [] for target in targets: lowerCAmelCase = vocab.get(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) if id_ is None: lowerCAmelCase = self.tokenizer( __SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , return_attention_mask=__SCREAMING_SNAKE_CASE , return_token_type_ids=__SCREAMING_SNAKE_CASE , max_length=1 , truncation=__SCREAMING_SNAKE_CASE , )['''input_ids'''] if len(__SCREAMING_SNAKE_CASE ) == 0: logger.warning( F"The specified target token `{target}` does not exist in the model vocabulary. " '''We cannot replace it with anything meaningful, ignoring it''' ) continue lowerCAmelCase = input_ids[0] # XXX: If users encounter this pass # it becomes pretty slow, so let's make sure # The warning enables them to fix the input to # get faster performance. logger.warning( F"The specified target token `{target}` does not exist in the model vocabulary. " F"Replacing with `{self.tokenizer.convert_ids_to_tokens(id_ )}`." ) target_ids.append(id_ ) lowerCAmelCase = list(set(__SCREAMING_SNAKE_CASE ) ) if len(__SCREAMING_SNAKE_CASE ) == 0: raise ValueError('''At least one target must be provided when passed.''' ) lowerCAmelCase = np.array(__SCREAMING_SNAKE_CASE ) return target_ids def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None ) ->Dict: lowerCAmelCase = {} if targets is not None: lowerCAmelCase = self.get_target_ids(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) lowerCAmelCase = target_ids if top_k is not None: lowerCAmelCase = top_k if self.tokenizer.mask_token_id is None: raise PipelineException( '''fill-mask''' , self.model.base_model_prefix , '''The tokenizer does not define a `mask_token`.''' ) return {}, {}, postprocess_params def __call__( self , __SCREAMING_SNAKE_CASE , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) ->List[Any]: lowerCAmelCase = super().__call__(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) and len(__SCREAMING_SNAKE_CASE ) == 1: return outputs[0] return outputs
338
1
from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, logging lowercase__ : Optional[int] = logging.get_logger(__name__) class lowercase_ ( UpperCamelCase_ ): """simple docstring""" UpperCAmelCase_ : Any = ["""pixel_values"""] def __init__( self , __SCREAMING_SNAKE_CASE = True , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = PILImageResampling.BILINEAR , __SCREAMING_SNAKE_CASE = True , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = True , __SCREAMING_SNAKE_CASE = 1 / 255 , __SCREAMING_SNAKE_CASE = True , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , **__SCREAMING_SNAKE_CASE , ) ->None: super().__init__(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = size if size is not None else {'''shortest_edge''': 256} lowerCAmelCase = get_size_dict(__SCREAMING_SNAKE_CASE , default_to_square=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = crop_size if crop_size is not None else {'''height''': 224, '''width''': 224} lowerCAmelCase = get_size_dict(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = do_resize lowerCAmelCase = size lowerCAmelCase = resample lowerCAmelCase = do_center_crop lowerCAmelCase = crop_size lowerCAmelCase = do_rescale lowerCAmelCase = rescale_factor lowerCAmelCase = do_normalize lowerCAmelCase = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN lowerCAmelCase = image_std if image_std is not None else IMAGENET_STANDARD_STD def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = PILImageResampling.BICUBIC , __SCREAMING_SNAKE_CASE = None , **__SCREAMING_SNAKE_CASE , ) ->np.ndarray: lowerCAmelCase = get_size_dict(__SCREAMING_SNAKE_CASE , default_to_square=__SCREAMING_SNAKE_CASE ) if "shortest_edge" not in size: raise ValueError(F"The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}" ) lowerCAmelCase = get_resize_output_image_size(__SCREAMING_SNAKE_CASE , size=size['''shortest_edge'''] , default_to_square=__SCREAMING_SNAKE_CASE ) return resize(__SCREAMING_SNAKE_CASE , size=__SCREAMING_SNAKE_CASE , resample=__SCREAMING_SNAKE_CASE , data_format=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None , **__SCREAMING_SNAKE_CASE , ) ->np.ndarray: lowerCAmelCase = get_size_dict(__SCREAMING_SNAKE_CASE ) return center_crop(__SCREAMING_SNAKE_CASE , size=(size['''height'''], size['''width''']) , data_format=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None , **__SCREAMING_SNAKE_CASE ) ->np.ndarray: return rescale(__SCREAMING_SNAKE_CASE , scale=__SCREAMING_SNAKE_CASE , data_format=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None , **__SCREAMING_SNAKE_CASE , ) ->np.ndarray: return normalize(__SCREAMING_SNAKE_CASE , mean=__SCREAMING_SNAKE_CASE , std=__SCREAMING_SNAKE_CASE , data_format=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = ChannelDimension.FIRST , **__SCREAMING_SNAKE_CASE , ) ->int: lowerCAmelCase = do_resize if do_resize is not None else self.do_resize lowerCAmelCase = size if size is not None else self.size lowerCAmelCase = get_size_dict(__SCREAMING_SNAKE_CASE , default_to_square=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = resample if resample is not None else self.resample lowerCAmelCase = do_center_crop if do_center_crop is not None else self.do_center_crop lowerCAmelCase = crop_size if crop_size is not None else self.crop_size lowerCAmelCase = get_size_dict(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = do_rescale if do_rescale is not None else self.do_rescale lowerCAmelCase = rescale_factor if rescale_factor is not None else self.rescale_factor lowerCAmelCase = do_normalize if do_normalize is not None else self.do_normalize lowerCAmelCase = image_mean if image_mean is not None else self.image_mean lowerCAmelCase = image_std if image_std is not None else self.image_std lowerCAmelCase = make_list_of_images(__SCREAMING_SNAKE_CASE ) if not valid_images(__SCREAMING_SNAKE_CASE ): raise ValueError( '''Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ''' '''torch.Tensor, tf.Tensor or jax.ndarray.''' ) if do_resize and size is None: raise ValueError('''Size must be specified if do_resize is True.''' ) if do_center_crop and crop_size is None: raise ValueError('''Crop size must be specified if do_center_crop is True.''' ) if do_rescale and rescale_factor is None: raise ValueError('''Rescale factor must be specified if do_rescale is True.''' ) if do_normalize and (image_mean is None or image_std is None): raise ValueError('''Image mean and std must be specified if do_normalize is True.''' ) # All transformations expect numpy arrays. lowerCAmelCase = [to_numpy_array(__SCREAMING_SNAKE_CASE ) for image in images] if do_resize: lowerCAmelCase = [self.resize(image=__SCREAMING_SNAKE_CASE , size=__SCREAMING_SNAKE_CASE , resample=__SCREAMING_SNAKE_CASE ) for image in images] if do_center_crop: lowerCAmelCase = [self.center_crop(image=__SCREAMING_SNAKE_CASE , size=__SCREAMING_SNAKE_CASE ) for image in images] if do_rescale: lowerCAmelCase = [self.rescale(image=__SCREAMING_SNAKE_CASE , scale=__SCREAMING_SNAKE_CASE ) for image in images] if do_normalize: lowerCAmelCase = [self.normalize(image=__SCREAMING_SNAKE_CASE , mean=__SCREAMING_SNAKE_CASE , std=__SCREAMING_SNAKE_CASE ) for image in images] lowerCAmelCase = [to_channel_dimension_format(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) for image in images] lowerCAmelCase = {'''pixel_values''': images} return BatchFeature(data=__SCREAMING_SNAKE_CASE , tensor_type=__SCREAMING_SNAKE_CASE )
338
from typing import TYPE_CHECKING from ...utils import _LazyModule lowercase__ : int = {'''tokenization_wav2vec2_phoneme''': ['''Wav2Vec2PhonemeCTCTokenizer''']} if TYPE_CHECKING: from .tokenization_wavaveca_phoneme import WavaVecaPhonemeCTCTokenizer else: import sys lowercase__ : Optional[int] = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
338
1
from datetime import datetime as dt import os from github import Github lowercase__ : str = [ '''good first issue''', '''good second issue''', '''good difficult issue''', '''feature request''', '''new model''', '''wip''', ] def SCREAMING_SNAKE_CASE_ ( ) -> int: lowerCAmelCase = Github(os.environ['''GITHUB_TOKEN'''] ) lowerCAmelCase = g.get_repo('''huggingface/transformers''' ) lowerCAmelCase = repo.get_issues(state='''open''' ) for issue in open_issues: lowerCAmelCase = sorted([comment for comment in issue.get_comments()] , key=lambda snake_case__ : i.created_at , reverse=snake_case__ ) lowerCAmelCase = comments[0] if len(snake_case__ ) > 0 else None if ( last_comment is not None and last_comment.user.login == "github-actions[bot]" and (dt.utcnow() - issue.updated_at).days > 7 and (dt.utcnow() - issue.created_at).days >= 3_0 and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels() ) ): # print(f"Would close issue {issue.number} since it has been 7 days of inactivity since bot mention.") issue.edit(state='''closed''' ) elif ( (dt.utcnow() - issue.updated_at).days > 2_3 and (dt.utcnow() - issue.created_at).days >= 3_0 and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels() ) ): # print(f"Would add stale comment to {issue.number}") issue.create_comment( '''This issue has been automatically marked as stale because it has not had ''' '''recent activity. If you think this still needs to be addressed ''' '''please comment on this thread.\n\nPlease note that issues that do not follow the ''' '''[contributing guidelines](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) ''' '''are likely to be ignored.''' ) if __name__ == "__main__": main()
338
lowercase__ : Optional[int] = '''ABCDEFGHIJKLMNOPQRSTUVWXYZ''' def SCREAMING_SNAKE_CASE_ ( ) -> None: lowerCAmelCase = input('''Enter message: ''' ) lowerCAmelCase = input('''Enter key [alphanumeric]: ''' ) lowerCAmelCase = input('''Encrypt/Decrypt [e/d]: ''' ) if mode.lower().startswith('''e''' ): lowerCAmelCase = '''encrypt''' lowerCAmelCase = encrypt_message(snake_case__ , snake_case__ ) elif mode.lower().startswith('''d''' ): lowerCAmelCase = '''decrypt''' lowerCAmelCase = decrypt_message(snake_case__ , snake_case__ ) print(f"\n{mode.title()}ed message:" ) print(snake_case__ ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ ) -> str: return translate_message(snake_case__ , snake_case__ , '''encrypt''' ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ ) -> str: return translate_message(snake_case__ , snake_case__ , '''decrypt''' ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> str: lowerCAmelCase = [] lowerCAmelCase = 0 lowerCAmelCase = key.upper() for symbol in message: lowerCAmelCase = LETTERS.find(symbol.upper() ) if num != -1: if mode == "encrypt": num += LETTERS.find(key[key_index] ) elif mode == "decrypt": num -= LETTERS.find(key[key_index] ) num %= len(snake_case__ ) if symbol.isupper(): translated.append(LETTERS[num] ) elif symbol.islower(): translated.append(LETTERS[num].lower() ) key_index += 1 if key_index == len(snake_case__ ): lowerCAmelCase = 0 else: translated.append(snake_case__ ) return "".join(snake_case__ ) if __name__ == "__main__": main()
338
1
from __future__ import annotations from functools import lru_cache from math import ceil lowercase__ : Union[str, Any] = 1_0_0 lowercase__ : Tuple = set(range(3, NUM_PRIMES, 2)) primes.add(2) lowercase__ : int for prime in range(3, ceil(NUM_PRIMES**0.5), 2): if prime not in primes: continue primes.difference_update(set(range(prime * prime, NUM_PRIMES, prime))) @lru_cache(maxsize=1_0_0 ) def SCREAMING_SNAKE_CASE_ ( snake_case__ ) -> set[int]: if number_to_partition < 0: return set() elif number_to_partition == 0: return {1} lowerCAmelCase = set() lowerCAmelCase = 42 lowerCAmelCase = 42 for prime in primes: if prime > number_to_partition: continue for sub in partition(number_to_partition - prime ): ret.add(sub * prime ) return ret def SCREAMING_SNAKE_CASE_ ( snake_case__ = 5_0_0_0 ) -> int | None: for number_to_partition in range(1 , snake_case__ ): if len(partition(snake_case__ ) ) > number_unique_partitions: return number_to_partition return None if __name__ == "__main__": print(f'{solution() = }')
338
from collections import defaultdict from math import ceil, sqrt def SCREAMING_SNAKE_CASE_ ( snake_case__ = 1_0_0_0_0_0_0 , snake_case__ = 1_0 ) -> int: lowerCAmelCase = defaultdict(snake_case__ ) for outer_width in range(3 , (t_limit // 4) + 2 ): if outer_width * outer_width > t_limit: lowerCAmelCase = max( ceil(sqrt(outer_width * outer_width - t_limit ) ) , 1 ) else: lowerCAmelCase = 1 hole_width_lower_bound += (outer_width - hole_width_lower_bound) % 2 for hole_width in range(snake_case__ , outer_width - 1 , 2 ): count[outer_width * outer_width - hole_width * hole_width] += 1 return sum(1 for n in count.values() if 1 <= n <= 1_0 ) if __name__ == "__main__": print(f'{solution() = }')
338
1
import unittest import torch from torch import nn from accelerate.test_utils import require_cuda from accelerate.utils.memory import find_executable_batch_size, release_memory def SCREAMING_SNAKE_CASE_ ( ) -> int: raise RuntimeError('''CUDA out of memory.''' ) class lowercase_ ( nn.Module ): """simple docstring""" def __init__( self ) ->Any: super().__init__() lowerCAmelCase = nn.Linear(3 , 4 ) lowerCAmelCase = nn.BatchNormad(4 ) lowerCAmelCase = nn.Linear(4 , 5 ) def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE ) ->str: return self.lineara(self.batchnorm(self.lineara(__SCREAMING_SNAKE_CASE ) ) ) class lowercase_ ( unittest.TestCase ): """simple docstring""" def SCREAMING_SNAKE_CASE_ ( self ) ->int: lowerCAmelCase = [] @find_executable_batch_size(starting_batch_size=128 ) def mock_training_loop_function(__SCREAMING_SNAKE_CASE ): nonlocal batch_sizes batch_sizes.append(__SCREAMING_SNAKE_CASE ) if batch_size != 8: raise_fake_out_of_memory() mock_training_loop_function() self.assertListEqual(__SCREAMING_SNAKE_CASE , [128, 64, 32, 16, 8] ) def SCREAMING_SNAKE_CASE_ ( self ) ->Union[str, Any]: lowerCAmelCase = [] @find_executable_batch_size(starting_batch_size=128 ) def mock_training_loop_function(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): nonlocal batch_sizes batch_sizes.append(__SCREAMING_SNAKE_CASE ) if batch_size != 8: raise_fake_out_of_memory() return batch_size, arga lowerCAmelCase , lowerCAmelCase = mock_training_loop_function('''hello''' ) self.assertListEqual(__SCREAMING_SNAKE_CASE , [128, 64, 32, 16, 8] ) self.assertListEqual([bs, arga] , [8, '''hello'''] ) def SCREAMING_SNAKE_CASE_ ( self ) ->int: @find_executable_batch_size(starting_batch_size=0 ) def mock_training_loop_function(__SCREAMING_SNAKE_CASE ): pass with self.assertRaises(__SCREAMING_SNAKE_CASE ) as cm: mock_training_loop_function() self.assertIn('''No executable batch size found, reached zero.''' , cm.exception.args[0] ) def SCREAMING_SNAKE_CASE_ ( self ) ->Union[str, Any]: @find_executable_batch_size(starting_batch_size=16 ) def mock_training_loop_function(__SCREAMING_SNAKE_CASE ): if batch_size > 0: raise_fake_out_of_memory() pass with self.assertRaises(__SCREAMING_SNAKE_CASE ) as cm: mock_training_loop_function() self.assertIn('''No executable batch size found, reached zero.''' , cm.exception.args[0] ) def SCREAMING_SNAKE_CASE_ ( self ) ->Any: @find_executable_batch_size(starting_batch_size=128 ) def mock_training_loop_function(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): if batch_size != 8: raise raise_fake_out_of_memory() with self.assertRaises(__SCREAMING_SNAKE_CASE ) as cm: mock_training_loop_function(128 , '''hello''' , '''world''' ) self.assertIn('''Batch size was passed into `f`''' , cm.exception.args[0] ) self.assertIn('''`f(arg1=\'hello\', arg2=\'world\')''' , cm.exception.args[0] ) def SCREAMING_SNAKE_CASE_ ( self ) ->Any: @find_executable_batch_size(starting_batch_size=16 ) def mock_training_loop_function(__SCREAMING_SNAKE_CASE ): raise ValueError('''Oops, we had an error!''' ) with self.assertRaises(__SCREAMING_SNAKE_CASE ) as cm: mock_training_loop_function() self.assertIn('''Oops, we had an error!''' , cm.exception.args[0] ) @require_cuda def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[int]: lowerCAmelCase = torch.cuda.memory_allocated() lowerCAmelCase = ModelForTest() model.cuda() self.assertGreater(torch.cuda.memory_allocated() , __SCREAMING_SNAKE_CASE ) lowerCAmelCase = release_memory(__SCREAMING_SNAKE_CASE ) self.assertEqual(torch.cuda.memory_allocated() , __SCREAMING_SNAKE_CASE )
338
import pytest from datasets import Dataset, DatasetDict, Features, NamedSplit, Value from datasets.io.text import TextDatasetReader from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ ) -> Union[str, Any]: assert isinstance(snake_case__ , snake_case__ ) assert dataset.num_rows == 4 assert dataset.num_columns == 1 assert dataset.column_names == ["text"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize('''keep_in_memory''' , [False, True] ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> Union[str, Any]: lowerCAmelCase = tmp_path / '''cache''' lowerCAmelCase = {'''text''': '''string'''} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): lowerCAmelCase = TextDatasetReader(snake_case__ , cache_dir=snake_case__ , keep_in_memory=snake_case__ ).read() _check_text_dataset(snake_case__ , snake_case__ ) @pytest.mark.parametrize( '''features''' , [ None, {'''text''': '''string'''}, {'''text''': '''int32'''}, {'''text''': '''float32'''}, ] , ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> Optional[Any]: lowerCAmelCase = tmp_path / '''cache''' lowerCAmelCase = {'''text''': '''string'''} lowerCAmelCase = features.copy() if features else default_expected_features lowerCAmelCase = ( Features({feature: Value(snake_case__ ) for feature, dtype in features.items()} ) if features is not None else None ) lowerCAmelCase = TextDatasetReader(snake_case__ , features=snake_case__ , cache_dir=snake_case__ ).read() _check_text_dataset(snake_case__ , snake_case__ ) @pytest.mark.parametrize('''split''' , [None, NamedSplit('''train''' ), '''train''', '''test'''] ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> List[str]: lowerCAmelCase = tmp_path / '''cache''' lowerCAmelCase = {'''text''': '''string'''} lowerCAmelCase = TextDatasetReader(snake_case__ , cache_dir=snake_case__ , split=snake_case__ ).read() _check_text_dataset(snake_case__ , snake_case__ ) assert dataset.split == split if split else "train" @pytest.mark.parametrize('''path_type''' , [str, list] ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> Optional[int]: if issubclass(snake_case__ , snake_case__ ): lowerCAmelCase = text_path elif issubclass(snake_case__ , snake_case__ ): lowerCAmelCase = [text_path] lowerCAmelCase = tmp_path / '''cache''' lowerCAmelCase = {'''text''': '''string'''} lowerCAmelCase = TextDatasetReader(snake_case__ , cache_dir=snake_case__ ).read() _check_text_dataset(snake_case__ , snake_case__ ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__=("train",) ) -> Optional[Any]: assert isinstance(snake_case__ , snake_case__ ) for split in splits: lowerCAmelCase = dataset_dict[split] assert dataset.num_rows == 4 assert dataset.num_columns == 1 assert dataset.column_names == ["text"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize('''keep_in_memory''' , [False, True] ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> Optional[Any]: lowerCAmelCase = tmp_path / '''cache''' lowerCAmelCase = {'''text''': '''string'''} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): lowerCAmelCase = TextDatasetReader({'''train''': text_path} , cache_dir=snake_case__ , keep_in_memory=snake_case__ ).read() _check_text_datasetdict(snake_case__ , snake_case__ ) @pytest.mark.parametrize( '''features''' , [ None, {'''text''': '''string'''}, {'''text''': '''int32'''}, {'''text''': '''float32'''}, ] , ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> List[Any]: lowerCAmelCase = tmp_path / '''cache''' # CSV file loses col_1 string dtype information: default now is "int64" instead of "string" lowerCAmelCase = {'''text''': '''string'''} lowerCAmelCase = features.copy() if features else default_expected_features lowerCAmelCase = ( Features({feature: Value(snake_case__ ) for feature, dtype in features.items()} ) if features is not None else None ) lowerCAmelCase = TextDatasetReader({'''train''': text_path} , features=snake_case__ , cache_dir=snake_case__ ).read() _check_text_datasetdict(snake_case__ , snake_case__ ) @pytest.mark.parametrize('''split''' , [None, NamedSplit('''train''' ), '''train''', '''test'''] ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> Any: if split: lowerCAmelCase = {split: text_path} else: lowerCAmelCase = '''train''' lowerCAmelCase = {'''train''': text_path, '''test''': text_path} lowerCAmelCase = tmp_path / '''cache''' lowerCAmelCase = {'''text''': '''string'''} lowerCAmelCase = TextDatasetReader(snake_case__ , cache_dir=snake_case__ ).read() _check_text_datasetdict(snake_case__ , snake_case__ , splits=list(path.keys() ) ) assert all(dataset[split].split == split for split in path.keys() )
338
1
import os import time import pytest from datasets.utils.filelock import FileLock, Timeout def SCREAMING_SNAKE_CASE_ ( snake_case__ ) -> Any: lowerCAmelCase = FileLock(str(tmpdir / '''foo.lock''' ) ) lowerCAmelCase = FileLock(str(tmpdir / '''foo.lock''' ) ) lowerCAmelCase = 0.01 with locka.acquire(): with pytest.raises(snake_case__ ): lowerCAmelCase = time.time() locka.acquire(snake_case__ ) assert time.time() - _start > timeout def SCREAMING_SNAKE_CASE_ ( snake_case__ ) -> List[str]: lowerCAmelCase = '''a''' * 1_0_0_0 + '''.lock''' lowerCAmelCase = FileLock(str(tmpdir / filename ) ) assert locka._lock_file.endswith('''.lock''' ) assert not locka._lock_file.endswith(snake_case__ ) assert len(os.path.basename(locka._lock_file ) ) <= 2_5_5 lowerCAmelCase = FileLock(tmpdir / filename ) with locka.acquire(): with pytest.raises(snake_case__ ): locka.acquire(0 )
338
def SCREAMING_SNAKE_CASE_ ( snake_case__ ) -> str: if isinstance(snake_case__ , snake_case__ ): raise TypeError('''\'float\' object cannot be interpreted as an integer''' ) if isinstance(snake_case__ , snake_case__ ): raise TypeError('''\'str\' object cannot be interpreted as an integer''' ) if num == 0: return "0b0" lowerCAmelCase = False if num < 0: lowerCAmelCase = True lowerCAmelCase = -num lowerCAmelCase = [] while num > 0: binary.insert(0 , num % 2 ) num >>= 1 if negative: return "-0b" + "".join(str(snake_case__ ) for e in binary ) return "0b" + "".join(str(snake_case__ ) for e in binary ) if __name__ == "__main__": import doctest doctest.testmod()
338
1
from __future__ import annotations import time import numpy as np lowercase__ : Optional[int] = [8, 5, 9, 7] lowercase__ : Union[str, Any] = [ [2, 0, 1, 1], [0, 1, 2, 1], [4, 0, 0, 3], [0, 2, 1, 0], [1, 0, 3, 0], ] lowercase__ : Dict = [ [3, 2, 1, 4], [0, 2, 5, 2], [5, 1, 0, 5], [1, 5, 3, 0], [3, 0, 3, 3], ] class lowercase_ : """simple docstring""" def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , ) ->None: lowerCAmelCase = claim_vector lowerCAmelCase = allocated_resources_table lowerCAmelCase = maximum_claim_table def SCREAMING_SNAKE_CASE_ ( self ) ->list[int]: return [ sum(p_item[i] for p_item in self.__allocated_resources_table ) for i in range(len(self.__allocated_resources_table[0] ) ) ] def SCREAMING_SNAKE_CASE_ ( self ) ->list[int]: return np.array(self.__claim_vector ) - np.array( self.__processes_resource_summation() ) def SCREAMING_SNAKE_CASE_ ( self ) ->list[list[int]]: return [ list(np.array(self.__maximum_claim_table[i] ) - np.array(__SCREAMING_SNAKE_CASE ) ) for i, allocated_resource in enumerate(self.__allocated_resources_table ) ] def SCREAMING_SNAKE_CASE_ ( self ) ->dict[int, list[int]]: return {self.__need().index(__SCREAMING_SNAKE_CASE ): i for i in self.__need()} def SCREAMING_SNAKE_CASE_ ( self , **__SCREAMING_SNAKE_CASE ) ->None: lowerCAmelCase = self.__need() lowerCAmelCase = self.__allocated_resources_table lowerCAmelCase = self.__available_resources() lowerCAmelCase = self.__need_index_manager() for kw, val in kwargs.items(): if kw and val is True: self.__pretty_data() print('''_''' * 50 + '''\n''' ) while need_list: lowerCAmelCase = False for each_need in need_list: lowerCAmelCase = True for index, need in enumerate(__SCREAMING_SNAKE_CASE ): if need > available_resources[index]: lowerCAmelCase = False break if execution: lowerCAmelCase = True # get the original index of the process from ind_ctrl db for original_need_index, need_clone in need_index_manager.items(): if each_need == need_clone: lowerCAmelCase = original_need_index print(F"Process {process_number + 1} is executing." ) # remove the process run from stack need_list.remove(__SCREAMING_SNAKE_CASE ) # update available/freed resources stack lowerCAmelCase = np.array(__SCREAMING_SNAKE_CASE ) + np.array( alloc_resources_table[process_number] ) print( '''Updated available resource stack for processes: ''' + ''' '''.join([str(__SCREAMING_SNAKE_CASE ) for x in available_resources] ) ) break if safe: print('''The process is in a safe state.\n''' ) else: print('''System in unsafe state. Aborting...\n''' ) break def SCREAMING_SNAKE_CASE_ ( self ) ->Dict: print(''' ''' * 9 + '''Allocated Resource Table''' ) for item in self.__allocated_resources_table: print( F"P{self.__allocated_resources_table.index(__SCREAMING_SNAKE_CASE ) + 1}" + ''' '''.join(F"{it:>8}" for it in item ) + '''\n''' ) print(''' ''' * 9 + '''System Resource Table''' ) for item in self.__maximum_claim_table: print( F"P{self.__maximum_claim_table.index(__SCREAMING_SNAKE_CASE ) + 1}" + ''' '''.join(F"{it:>8}" for it in item ) + '''\n''' ) print( '''Current Usage by Active Processes: ''' + ''' '''.join(str(__SCREAMING_SNAKE_CASE ) for x in self.__claim_vector ) ) print( '''Initial Available Resources: ''' + ''' '''.join(str(__SCREAMING_SNAKE_CASE ) for x in self.__available_resources() ) ) time.sleep(1 ) if __name__ == "__main__": import doctest doctest.testmod()
338
class lowercase_ : """simple docstring""" def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ->Any: lowerCAmelCase = name lowerCAmelCase = value lowerCAmelCase = weight def __repr__( self ) ->str: return F"{self.__class__.__name__}({self.name}, {self.value}, {self.weight})" def SCREAMING_SNAKE_CASE_ ( self ) ->List[Any]: return self.value def SCREAMING_SNAKE_CASE_ ( self ) ->int: return self.name def SCREAMING_SNAKE_CASE_ ( self ) ->Union[str, Any]: return self.weight def SCREAMING_SNAKE_CASE_ ( self ) ->Tuple: return self.value / self.weight def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> int: lowerCAmelCase = [] for i in range(len(snake_case__ ) ): menu.append(Things(name[i] , value[i] , weight[i] ) ) return menu def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> Optional[int]: lowerCAmelCase = sorted(snake_case__ , key=snake_case__ , reverse=snake_case__ ) lowerCAmelCase = [] lowerCAmelCase , lowerCAmelCase = 0.0, 0.0 for i in range(len(snake_case__ ) ): if (total_cost + items_copy[i].get_weight()) <= max_cost: result.append(items_copy[i] ) total_cost += items_copy[i].get_weight() total_value += items_copy[i].get_value() return (result, total_value) def SCREAMING_SNAKE_CASE_ ( ) -> Optional[int]: pass if __name__ == "__main__": import doctest doctest.testmod()
338
1
import inspect import re from hashlib import shaaaa from typing import Dict, List from .arrow import arrow from .audiofolder import audiofolder from .csv import csv from .imagefolder import imagefolder from .json import json from .pandas import pandas from .parquet import parquet from .sql import sql # noqa F401 from .text import text def SCREAMING_SNAKE_CASE_ ( snake_case__ ) -> str: lowerCAmelCase = [] for line in lines: lowerCAmelCase = re.sub(R'''#.*''' , '''''' , snake_case__ ) # remove comments if line: filtered_lines.append(snake_case__ ) lowerCAmelCase = '''\n'''.join(snake_case__ ) # Make a hash from all this code lowerCAmelCase = full_str.encode('''utf-8''' ) return shaaaa(snake_case__ ).hexdigest() # get importable module names and hash for caching lowercase__ : str = { '''csv''': (csv.__name__, _hash_python_lines(inspect.getsource(csv).splitlines())), '''json''': (json.__name__, _hash_python_lines(inspect.getsource(json).splitlines())), '''pandas''': (pandas.__name__, _hash_python_lines(inspect.getsource(pandas).splitlines())), '''parquet''': (parquet.__name__, _hash_python_lines(inspect.getsource(parquet).splitlines())), '''arrow''': (arrow.__name__, _hash_python_lines(inspect.getsource(arrow).splitlines())), '''text''': (text.__name__, _hash_python_lines(inspect.getsource(text).splitlines())), '''imagefolder''': (imagefolder.__name__, _hash_python_lines(inspect.getsource(imagefolder).splitlines())), '''audiofolder''': (audiofolder.__name__, _hash_python_lines(inspect.getsource(audiofolder).splitlines())), } # Used to infer the module to use based on the data files extensions lowercase__ : Optional[Any] = { '''.csv''': ('''csv''', {}), '''.tsv''': ('''csv''', {'''sep''': '''\t'''}), '''.json''': ('''json''', {}), '''.jsonl''': ('''json''', {}), '''.parquet''': ('''parquet''', {}), '''.arrow''': ('''arrow''', {}), '''.txt''': ('''text''', {}), } _EXTENSION_TO_MODULE.update({ext: ('''imagefolder''', {}) for ext in imagefolder.ImageFolder.EXTENSIONS}) _EXTENSION_TO_MODULE.update({ext.upper(): ('''imagefolder''', {}) for ext in imagefolder.ImageFolder.EXTENSIONS}) _EXTENSION_TO_MODULE.update({ext: ('''audiofolder''', {}) for ext in audiofolder.AudioFolder.EXTENSIONS}) _EXTENSION_TO_MODULE.update({ext.upper(): ('''audiofolder''', {}) for ext in audiofolder.AudioFolder.EXTENSIONS}) lowercase__ : Any = {'''imagefolder''', '''audiofolder'''} # Used to filter data files based on extensions given a module name lowercase__ : Dict[str, List[str]] = {} for _ext, (_module, _) in _EXTENSION_TO_MODULE.items(): _MODULE_TO_EXTENSIONS.setdefault(_module, []).append(_ext) _MODULE_TO_EXTENSIONS["imagefolder"].append('''.zip''') _MODULE_TO_EXTENSIONS["audiofolder"].append('''.zip''')
338
import numpy as np import skfuzzy as fuzz if __name__ == "__main__": # Create universe of discourse in Python using linspace () lowercase__ : Dict = np.linspace(start=0, stop=7_5, num=7_5, endpoint=True, retstep=False) # Create two fuzzy sets by defining any membership function # (trapmf(), gbellmf(), gaussmf(), etc). lowercase__ : Optional[int] = [0, 2_5, 5_0] lowercase__ : Union[str, Any] = [2_5, 5_0, 7_5] lowercase__ : int = fuzz.membership.trimf(X, abca) lowercase__ : Tuple = fuzz.membership.trimf(X, abca) # Compute the different operations using inbuilt functions. lowercase__ : List[str] = np.ones(7_5) lowercase__ : Any = np.zeros((7_5,)) # 1. Union = max(µA(x), µB(x)) lowercase__ : Union[str, Any] = fuzz.fuzzy_or(X, young, X, middle_aged)[1] # 2. Intersection = min(µA(x), µB(x)) lowercase__ : int = fuzz.fuzzy_and(X, young, X, middle_aged)[1] # 3. Complement (A) = (1- min(µA(x)) lowercase__ : Union[str, Any] = fuzz.fuzzy_not(young) # 4. Difference (A/B) = min(µA(x),(1- µB(x))) lowercase__ : Optional[int] = fuzz.fuzzy_and(X, young, X, fuzz.fuzzy_not(middle_aged)[1])[1] # 5. Algebraic Sum = [µA(x) + µB(x) – (µA(x) * µB(x))] lowercase__ : Any = young + middle_aged - (young * middle_aged) # 6. Algebraic Product = (µA(x) * µB(x)) lowercase__ : str = young * middle_aged # 7. Bounded Sum = min[1,(µA(x), µB(x))] lowercase__ : Tuple = fuzz.fuzzy_and(X, one, X, young + middle_aged)[1] # 8. Bounded difference = min[0,(µA(x), µB(x))] lowercase__ : Tuple = fuzz.fuzzy_or(X, zero, X, young - middle_aged)[1] # max-min composition # max-product composition # Plot each set A, set B and each operation result using plot() and subplot(). from matplotlib import pyplot as plt plt.figure() plt.subplot(4, 3, 1) plt.plot(X, young) plt.title('''Young''') plt.grid(True) plt.subplot(4, 3, 2) plt.plot(X, middle_aged) plt.title('''Middle aged''') plt.grid(True) plt.subplot(4, 3, 3) plt.plot(X, union) plt.title('''union''') plt.grid(True) plt.subplot(4, 3, 4) plt.plot(X, intersection) plt.title('''intersection''') plt.grid(True) plt.subplot(4, 3, 5) plt.plot(X, complement_a) plt.title('''complement_a''') plt.grid(True) plt.subplot(4, 3, 6) plt.plot(X, difference) plt.title('''difference a/b''') plt.grid(True) plt.subplot(4, 3, 7) plt.plot(X, alg_sum) plt.title('''alg_sum''') plt.grid(True) plt.subplot(4, 3, 8) plt.plot(X, alg_product) plt.title('''alg_product''') plt.grid(True) plt.subplot(4, 3, 9) plt.plot(X, bdd_sum) plt.title('''bdd_sum''') plt.grid(True) plt.subplot(4, 3, 1_0) plt.plot(X, bdd_difference) plt.title('''bdd_difference''') plt.grid(True) plt.subplots_adjust(hspace=0.5) plt.show()
338
1
from __future__ import annotations def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ ) -> list[int]: lowerCAmelCase = 0 lowerCAmelCase = len(snake_case__ ) - 1 while i < j: if nums[i] + nums[j] == target: return [i, j] elif nums[i] + nums[j] < target: lowerCAmelCase = i + 1 else: lowerCAmelCase = j - 1 return [] if __name__ == "__main__": import doctest doctest.testmod() print(f'{two_pointer([2, 7, 1_1, 1_5], 9) = }')
338
import torch from diffusers import DDPMScheduler from .test_schedulers import SchedulerCommonTest class lowercase_ ( UpperCamelCase_ ): """simple docstring""" UpperCAmelCase_ : str = (DDPMScheduler,) def SCREAMING_SNAKE_CASE_ ( self , **__SCREAMING_SNAKE_CASE ) ->Optional[Any]: lowerCAmelCase = { '''num_train_timesteps''': 1000, '''beta_start''': 0.0_0_0_1, '''beta_end''': 0.0_2, '''beta_schedule''': '''linear''', '''variance_type''': '''fixed_small''', '''clip_sample''': True, } config.update(**__SCREAMING_SNAKE_CASE ) return config def SCREAMING_SNAKE_CASE_ ( self ) ->Tuple: for timesteps in [1, 5, 100, 1000]: self.check_over_configs(num_train_timesteps=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->List[Any]: for beta_start, beta_end in zip([0.0_0_0_1, 0.0_0_1, 0.0_1, 0.1] , [0.0_0_2, 0.0_2, 0.2, 2] ): self.check_over_configs(beta_start=__SCREAMING_SNAKE_CASE , beta_end=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[Any]: for schedule in ["linear", "squaredcos_cap_v2"]: self.check_over_configs(beta_schedule=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->Dict: for variance in ["fixed_small", "fixed_large", "other"]: self.check_over_configs(variance_type=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->List[str]: for clip_sample in [True, False]: self.check_over_configs(clip_sample=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->Any: self.check_over_configs(thresholding=__SCREAMING_SNAKE_CASE ) for threshold in [0.5, 1.0, 2.0]: for prediction_type in ["epsilon", "sample", "v_prediction"]: self.check_over_configs( thresholding=__SCREAMING_SNAKE_CASE , prediction_type=__SCREAMING_SNAKE_CASE , sample_max_value=__SCREAMING_SNAKE_CASE , ) def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[int]: for prediction_type in ["epsilon", "sample", "v_prediction"]: self.check_over_configs(prediction_type=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[Any]: for t in [0, 500, 999]: self.check_over_forward(time_step=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->Tuple: lowerCAmelCase = self.scheduler_classes[0] lowerCAmelCase = self.get_scheduler_config() lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) assert torch.sum(torch.abs(scheduler._get_variance(0 ) - 0.0 ) ) < 1e-5 assert torch.sum(torch.abs(scheduler._get_variance(487 ) - 0.0_0_9_7_9 ) ) < 1e-5 assert torch.sum(torch.abs(scheduler._get_variance(999 ) - 0.0_2 ) ) < 1e-5 def SCREAMING_SNAKE_CASE_ ( self ) ->str: lowerCAmelCase = self.scheduler_classes[0] lowerCAmelCase = self.get_scheduler_config() lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = len(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = self.dummy_model() lowerCAmelCase = self.dummy_sample_deter lowerCAmelCase = torch.manual_seed(0 ) for t in reversed(range(__SCREAMING_SNAKE_CASE ) ): # 1. predict noise residual lowerCAmelCase = model(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # 2. predict previous mean of sample x_t-1 lowerCAmelCase = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , generator=__SCREAMING_SNAKE_CASE ).prev_sample # if t > 0: # noise = self.dummy_sample_deter # variance = scheduler.get_variance(t) ** (0.5) * noise # # sample = pred_prev_sample + variance lowerCAmelCase = pred_prev_sample lowerCAmelCase = torch.sum(torch.abs(__SCREAMING_SNAKE_CASE ) ) lowerCAmelCase = torch.mean(torch.abs(__SCREAMING_SNAKE_CASE ) ) assert abs(result_sum.item() - 2_5_8.9_6_0_6 ) < 1e-2 assert abs(result_mean.item() - 0.3_3_7_2 ) < 1e-3 def SCREAMING_SNAKE_CASE_ ( self ) ->str: lowerCAmelCase = self.scheduler_classes[0] lowerCAmelCase = self.get_scheduler_config(prediction_type='''v_prediction''' ) lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = len(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = self.dummy_model() lowerCAmelCase = self.dummy_sample_deter lowerCAmelCase = torch.manual_seed(0 ) for t in reversed(range(__SCREAMING_SNAKE_CASE ) ): # 1. predict noise residual lowerCAmelCase = model(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # 2. predict previous mean of sample x_t-1 lowerCAmelCase = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , generator=__SCREAMING_SNAKE_CASE ).prev_sample # if t > 0: # noise = self.dummy_sample_deter # variance = scheduler.get_variance(t) ** (0.5) * noise # # sample = pred_prev_sample + variance lowerCAmelCase = pred_prev_sample lowerCAmelCase = torch.sum(torch.abs(__SCREAMING_SNAKE_CASE ) ) lowerCAmelCase = torch.mean(torch.abs(__SCREAMING_SNAKE_CASE ) ) assert abs(result_sum.item() - 2_0_2.0_2_9_6 ) < 1e-2 assert abs(result_mean.item() - 0.2_6_3_1 ) < 1e-3 def SCREAMING_SNAKE_CASE_ ( self ) ->Any: lowerCAmelCase = self.scheduler_classes[0] lowerCAmelCase = self.get_scheduler_config() lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = [100, 87, 50, 1, 0] scheduler.set_timesteps(timesteps=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = scheduler.timesteps for i, timestep in enumerate(__SCREAMING_SNAKE_CASE ): if i == len(__SCREAMING_SNAKE_CASE ) - 1: lowerCAmelCase = -1 else: lowerCAmelCase = timesteps[i + 1] lowerCAmelCase = scheduler.previous_timestep(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = prev_t.item() self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[Any]: lowerCAmelCase = self.scheduler_classes[0] lowerCAmelCase = self.get_scheduler_config() lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = [100, 87, 50, 51, 0] with self.assertRaises(__SCREAMING_SNAKE_CASE , msg='''`custom_timesteps` must be in descending order.''' ): scheduler.set_timesteps(timesteps=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->str: lowerCAmelCase = self.scheduler_classes[0] lowerCAmelCase = self.get_scheduler_config() lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = [100, 87, 50, 1, 0] lowerCAmelCase = len(__SCREAMING_SNAKE_CASE ) with self.assertRaises(__SCREAMING_SNAKE_CASE , msg='''Can only pass one of `num_inference_steps` or `custom_timesteps`.''' ): scheduler.set_timesteps(num_inference_steps=__SCREAMING_SNAKE_CASE , timesteps=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->Any: lowerCAmelCase = self.scheduler_classes[0] lowerCAmelCase = self.get_scheduler_config() lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = [scheduler.config.num_train_timesteps] with self.assertRaises( __SCREAMING_SNAKE_CASE , msg='''`timesteps` must start before `self.config.train_timesteps`: {scheduler.config.num_train_timesteps}}''' , ): scheduler.set_timesteps(timesteps=__SCREAMING_SNAKE_CASE )
338
1
from math import factorial def SCREAMING_SNAKE_CASE_ ( snake_case__ = 2_0 ) -> int: lowerCAmelCase = 2 * n # middle entry of odd rows starting at row 3 is the solution for n = 1, # 2, 3,... lowerCAmelCase = n // 2 return int(factorial(snake_case__ ) / (factorial(snake_case__ ) * factorial(n - k )) ) if __name__ == "__main__": import sys if len(sys.argv) == 1: print(solution(2_0)) else: try: lowercase__ : Any = int(sys.argv[1]) print(solution(n)) except ValueError: print('''Invalid entry - please enter a number.''')
338
import json import os from typing import Optional import numpy as np from ...feature_extraction_utils import BatchFeature from ...processing_utils import ProcessorMixin from ...utils import logging from ...utils.hub import get_file_from_repo from ..auto import AutoTokenizer lowercase__ : str = logging.get_logger(__name__) class lowercase_ ( UpperCamelCase_ ): """simple docstring""" UpperCAmelCase_ : Any = """AutoTokenizer""" UpperCAmelCase_ : Optional[int] = ["""tokenizer"""] UpperCAmelCase_ : str = { """semantic_prompt""": 1, """coarse_prompt""": 2, """fine_prompt""": 2, } def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None ) ->Optional[Any]: super().__init__(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = speaker_embeddings @classmethod def SCREAMING_SNAKE_CASE_ ( cls , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE="speaker_embeddings_path.json" , **__SCREAMING_SNAKE_CASE ) ->Tuple: if speaker_embeddings_dict_path is not None: lowerCAmelCase = get_file_from_repo( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , subfolder=kwargs.pop('''subfolder''' , __SCREAMING_SNAKE_CASE ) , cache_dir=kwargs.pop('''cache_dir''' , __SCREAMING_SNAKE_CASE ) , force_download=kwargs.pop('''force_download''' , __SCREAMING_SNAKE_CASE ) , proxies=kwargs.pop('''proxies''' , __SCREAMING_SNAKE_CASE ) , resume_download=kwargs.pop('''resume_download''' , __SCREAMING_SNAKE_CASE ) , local_files_only=kwargs.pop('''local_files_only''' , __SCREAMING_SNAKE_CASE ) , use_auth_token=kwargs.pop('''use_auth_token''' , __SCREAMING_SNAKE_CASE ) , revision=kwargs.pop('''revision''' , __SCREAMING_SNAKE_CASE ) , ) if speaker_embeddings_path is None: logger.warning( F"`{os.path.join(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE )}` does not exists\n , no preloaded speaker embeddings will be used - Make sure to provide a correct path to the json\n dictionnary if wanted, otherwise set `speaker_embeddings_dict_path=None`." ) lowerCAmelCase = None else: with open(__SCREAMING_SNAKE_CASE ) as speaker_embeddings_json: lowerCAmelCase = json.load(__SCREAMING_SNAKE_CASE ) else: lowerCAmelCase = None lowerCAmelCase = AutoTokenizer.from_pretrained(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) return cls(tokenizer=__SCREAMING_SNAKE_CASE , speaker_embeddings=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE="speaker_embeddings_path.json" , __SCREAMING_SNAKE_CASE="speaker_embeddings" , __SCREAMING_SNAKE_CASE = False , **__SCREAMING_SNAKE_CASE , ) ->int: if self.speaker_embeddings is not None: os.makedirs(os.path.join(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , '''v2''' ) , exist_ok=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = {} lowerCAmelCase = save_directory for prompt_key in self.speaker_embeddings: if prompt_key != "repo_or_path": lowerCAmelCase = self._load_voice_preset(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = {} for key in self.speaker_embeddings[prompt_key]: np.save( os.path.join( embeddings_dict['''repo_or_path'''] , __SCREAMING_SNAKE_CASE , F"{prompt_key}_{key}" ) , voice_preset[key] , allow_pickle=__SCREAMING_SNAKE_CASE , ) lowerCAmelCase = os.path.join(__SCREAMING_SNAKE_CASE , F"{prompt_key}_{key}.npy" ) lowerCAmelCase = tmp_dict with open(os.path.join(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) , '''w''' ) as fp: json.dump(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) super().save_pretrained(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE = None , **__SCREAMING_SNAKE_CASE ) ->List[str]: lowerCAmelCase = self.speaker_embeddings[voice_preset] lowerCAmelCase = {} for key in ["semantic_prompt", "coarse_prompt", "fine_prompt"]: if key not in voice_preset_paths: raise ValueError( F"Voice preset unrecognized, missing {key} as a key in self.speaker_embeddings[{voice_preset}]." ) lowerCAmelCase = get_file_from_repo( self.speaker_embeddings.get('''repo_or_path''' , '''/''' ) , voice_preset_paths[key] , subfolder=kwargs.pop('''subfolder''' , __SCREAMING_SNAKE_CASE ) , cache_dir=kwargs.pop('''cache_dir''' , __SCREAMING_SNAKE_CASE ) , force_download=kwargs.pop('''force_download''' , __SCREAMING_SNAKE_CASE ) , proxies=kwargs.pop('''proxies''' , __SCREAMING_SNAKE_CASE ) , resume_download=kwargs.pop('''resume_download''' , __SCREAMING_SNAKE_CASE ) , local_files_only=kwargs.pop('''local_files_only''' , __SCREAMING_SNAKE_CASE ) , use_auth_token=kwargs.pop('''use_auth_token''' , __SCREAMING_SNAKE_CASE ) , revision=kwargs.pop('''revision''' , __SCREAMING_SNAKE_CASE ) , ) if path is None: raise ValueError( F"`{os.path.join(self.speaker_embeddings.get('repo_or_path' , '/' ) , voice_preset_paths[key] )}` does not exists\n , no preloaded voice preset will be used - Make sure to provide correct paths to the {voice_preset}\n embeddings." ) lowerCAmelCase = np.load(__SCREAMING_SNAKE_CASE ) return voice_preset_dict def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE = None ) ->Tuple: for key in ["semantic_prompt", "coarse_prompt", "fine_prompt"]: if key not in voice_preset: raise ValueError(F"Voice preset unrecognized, missing {key} as a key." ) if not isinstance(voice_preset[key] , np.ndarray ): raise ValueError(F"{key} voice preset must be a {str(self.preset_shape[key] )}D ndarray." ) if len(voice_preset[key].shape ) != self.preset_shape[key]: raise ValueError(F"{key} voice preset must be a {str(self.preset_shape[key] )}D ndarray." ) def __call__( self , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE="pt" , __SCREAMING_SNAKE_CASE=256 , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=False , **__SCREAMING_SNAKE_CASE , ) ->int: if voice_preset is not None and not isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): if ( isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) and self.speaker_embeddings is not None and voice_preset in self.speaker_embeddings ): lowerCAmelCase = self._load_voice_preset(__SCREAMING_SNAKE_CASE ) else: if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) and not voice_preset.endswith('''.npz''' ): lowerCAmelCase = voice_preset + '''.npz''' lowerCAmelCase = np.load(__SCREAMING_SNAKE_CASE ) if voice_preset is not None: self._validate_voice_preset_dict(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) lowerCAmelCase = BatchFeature(data=__SCREAMING_SNAKE_CASE , tensor_type=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = self.tokenizer( __SCREAMING_SNAKE_CASE , return_tensors=__SCREAMING_SNAKE_CASE , padding='''max_length''' , max_length=__SCREAMING_SNAKE_CASE , return_attention_mask=__SCREAMING_SNAKE_CASE , return_token_type_ids=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) if voice_preset is not None: lowerCAmelCase = voice_preset return encoded_text
338
1
import unittest from transformers import SPIECE_UNDERLINE from transformers.models.speechta import SpeechTaTokenizer from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow from transformers.tokenization_utils import AddedToken from ...test_tokenization_common import TokenizerTesterMixin lowercase__ : List[Any] = get_tests_dir('''fixtures/test_sentencepiece_bpe_char.model''') @require_sentencepiece @require_tokenizers class lowercase_ ( UpperCamelCase_ , unittest.TestCase ): """simple docstring""" UpperCAmelCase_ : Dict = SpeechTaTokenizer UpperCAmelCase_ : Dict = False UpperCAmelCase_ : List[Any] = True def SCREAMING_SNAKE_CASE_ ( self ) ->List[Any]: super().setUp() # We have a SentencePiece fixture for testing lowerCAmelCase = SpeechTaTokenizer(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = AddedToken('''<mask>''' , lstrip=__SCREAMING_SNAKE_CASE , rstrip=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = mask_token tokenizer.add_special_tokens({'''mask_token''': mask_token} ) tokenizer.add_tokens(['''<ctc_blank>'''] ) tokenizer.save_pretrained(self.tmpdirname ) def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE ) ->List[Any]: lowerCAmelCase = '''this is a test''' lowerCAmelCase = '''this is a test''' return input_text, output_text def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=20 , __SCREAMING_SNAKE_CASE=5 ) ->str: lowerCAmelCase , lowerCAmelCase = self.get_input_output_texts(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = tokenizer.decode(__SCREAMING_SNAKE_CASE , clean_up_tokenization_spaces=__SCREAMING_SNAKE_CASE ) return text, ids def SCREAMING_SNAKE_CASE_ ( self ) ->int: lowerCAmelCase = '''<pad>''' lowerCAmelCase = 1 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 SCREAMING_SNAKE_CASE_ ( self ) ->Optional[Any]: lowerCAmelCase = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '''<s>''' ) self.assertEqual(vocab_keys[1] , '''<pad>''' ) self.assertEqual(vocab_keys[-4] , '''œ''' ) self.assertEqual(vocab_keys[-2] , '''<mask>''' ) self.assertEqual(vocab_keys[-1] , '''<ctc_blank>''' ) self.assertEqual(len(__SCREAMING_SNAKE_CASE ) , 81 ) def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[Any]: self.assertEqual(self.get_tokenizer().vocab_size , 79 ) def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[Any]: lowerCAmelCase = self.get_tokenizers(do_lower_case=__SCREAMING_SNAKE_CASE ) for tokenizer in tokenizers: with self.subTest(F"{tokenizer.__class__.__name__}" ): lowerCAmelCase = tokenizer.vocab_size lowerCAmelCase = len(__SCREAMING_SNAKE_CASE ) self.assertNotEqual(__SCREAMING_SNAKE_CASE , 0 ) # We usually have added tokens from the start in tests because our vocab fixtures are # smaller than the original vocabs - let's not assert this # self.assertEqual(vocab_size, all_size) lowerCAmelCase = ['''aaaaa bbbbbb''', '''cccccccccdddddddd'''] lowerCAmelCase = tokenizer.add_tokens(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = tokenizer.vocab_size lowerCAmelCase = len(__SCREAMING_SNAKE_CASE ) self.assertNotEqual(__SCREAMING_SNAKE_CASE , 0 ) self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) self.assertEqual(__SCREAMING_SNAKE_CASE , len(__SCREAMING_SNAKE_CASE ) ) self.assertEqual(__SCREAMING_SNAKE_CASE , all_size + len(__SCREAMING_SNAKE_CASE ) ) lowerCAmelCase = tokenizer.encode('''aaaaa bbbbbb low cccccccccdddddddd l''' , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertGreaterEqual(len(__SCREAMING_SNAKE_CASE ) , 4 ) self.assertGreater(tokens[0] , tokenizer.vocab_size - 1 ) self.assertGreater(tokens[-3] , tokenizer.vocab_size - 1 ) lowerCAmelCase = {'''eos_token''': '''>>>>|||<||<<|<<''', '''pad_token''': '''<<<<<|||>|>>>>|>'''} lowerCAmelCase = tokenizer.add_special_tokens(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = tokenizer.vocab_size lowerCAmelCase = len(__SCREAMING_SNAKE_CASE ) self.assertNotEqual(__SCREAMING_SNAKE_CASE , 0 ) self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) self.assertEqual(__SCREAMING_SNAKE_CASE , len(__SCREAMING_SNAKE_CASE ) ) self.assertEqual(__SCREAMING_SNAKE_CASE , all_size_a + len(__SCREAMING_SNAKE_CASE ) ) lowerCAmelCase = tokenizer.encode( '''>>>>|||<||<<|<< aaaaabbbbbb low cccccccccdddddddd <<<<<|||>|>>>>|> l''' , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertGreaterEqual(len(__SCREAMING_SNAKE_CASE ) , 6 ) self.assertGreater(tokens[0] , tokenizer.vocab_size - 1 ) self.assertGreater(tokens[0] , tokens[1] ) self.assertGreater(tokens[-3] , tokenizer.vocab_size - 1 ) self.assertGreater(tokens[-3] , tokens[-4] ) self.assertEqual(tokens[0] , tokenizer.eos_token_id ) self.assertEqual(tokens[-3] , tokenizer.pad_token_id ) def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[int]: pass def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[int]: pass def SCREAMING_SNAKE_CASE_ ( self ) ->List[str]: lowerCAmelCase = self.get_tokenizer() lowerCAmelCase = tokenizer.tokenize('''This is a test''' ) # fmt: off self.assertListEqual(__SCREAMING_SNAKE_CASE , [SPIECE_UNDERLINE, '''T''', '''h''', '''i''', '''s''', SPIECE_UNDERLINE, '''i''', '''s''', SPIECE_UNDERLINE, '''a''', SPIECE_UNDERLINE, '''t''', '''e''', '''s''', '''t'''] ) # fmt: on self.assertListEqual( tokenizer.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) , [4, 32, 11, 10, 12, 4, 10, 12, 4, 7, 4, 6, 5, 12, 6] , ) lowerCAmelCase = tokenizer.tokenize('''I was born in 92000, and this is falsé.''' ) self.assertListEqual( __SCREAMING_SNAKE_CASE , [SPIECE_UNDERLINE, '''I''', SPIECE_UNDERLINE, '''w''', '''a''', '''s''', SPIECE_UNDERLINE, '''b''', '''o''', '''r''', '''n''', SPIECE_UNDERLINE, '''i''', '''n''', SPIECE_UNDERLINE, '''92000''', ''',''', SPIECE_UNDERLINE, '''a''', '''n''', '''d''', SPIECE_UNDERLINE, '''t''', '''h''', '''i''', '''s''', SPIECE_UNDERLINE, '''i''', '''s''', SPIECE_UNDERLINE, '''f''', '''a''', '''l''', '''s''', '''é''', '''.'''] ) lowerCAmelCase = tokenizer.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) # fmt: off self.assertListEqual(__SCREAMING_SNAKE_CASE , [4, 30, 4, 20, 7, 12, 4, 25, 8, 13, 9, 4, 10, 9, 4, 3, 23, 4, 7, 9, 14, 4, 6, 11, 10, 12, 4, 10, 12, 4, 19, 7, 15, 12, 73, 26] ) # fmt: on lowerCAmelCase = tokenizer.convert_ids_to_tokens(__SCREAMING_SNAKE_CASE ) self.assertListEqual( __SCREAMING_SNAKE_CASE , [SPIECE_UNDERLINE, '''I''', SPIECE_UNDERLINE, '''w''', '''a''', '''s''', SPIECE_UNDERLINE, '''b''', '''o''', '''r''', '''n''', SPIECE_UNDERLINE, '''i''', '''n''', SPIECE_UNDERLINE, '''<unk>''', ''',''', SPIECE_UNDERLINE, '''a''', '''n''', '''d''', SPIECE_UNDERLINE, '''t''', '''h''', '''i''', '''s''', SPIECE_UNDERLINE, '''i''', '''s''', SPIECE_UNDERLINE, '''f''', '''a''', '''l''', '''s''', '''é''', '''.'''] ) @slow def SCREAMING_SNAKE_CASE_ ( self ) ->Dict: # Use custom sequence because this tokenizer does not handle numbers. lowerCAmelCase = [ '''Transformers (formerly known as pytorch-transformers and pytorch-pretrained-bert) provides ''' '''general-purpose architectures (BERT, GPT, RoBERTa, XLM, DistilBert, XLNet...) for Natural ''' '''Language Understanding (NLU) and Natural Language Generation (NLG) with over thirty-two pretrained ''' '''models in one hundred plus languages and deep interoperability between Jax, PyTorch and TensorFlow.''', '''BERT is designed to pre-train deep bidirectional representations from unlabeled text by jointly ''' '''conditioning on both left and right context in all layers.''', '''The quick brown fox jumps over the lazy dog.''', ] # fmt: off lowerCAmelCase = { '''input_ids''': [ [4, 32, 13, 7, 9, 12, 19, 8, 13, 18, 5, 13, 12, 4, 64, 19, 8, 13, 18, 5, 13, 15, 22, 4, 28, 9, 8, 20, 9, 4, 7, 12, 4, 24, 22, 6, 8, 13, 17, 11, 39, 6, 13, 7, 9, 12, 19, 8, 13, 18, 5, 13, 12, 4, 7, 9, 14, 4, 24, 22, 6, 8, 13, 17, 11, 39, 24, 13, 5, 6, 13, 7, 10, 9, 5, 14, 39, 25, 5, 13, 6, 63, 4, 24, 13, 8, 27, 10, 14, 5, 12, 4, 21, 5, 9, 5, 13, 7, 15, 39, 24, 16, 13, 24, 8, 12, 5, 4, 7, 13, 17, 11, 10, 6, 5, 17, 6, 16, 13, 5, 12, 4, 64, 40, 47, 54, 32, 23, 4, 53, 49, 32, 23, 4, 54, 8, 40, 47, 54, 32, 7, 23, 4, 69, 52, 43, 23, 4, 51, 10, 12, 6, 10, 15, 40, 5, 13, 6, 23, 4, 69, 52, 48, 5, 6, 26, 26, 26, 63, 4, 19, 8, 13, 4, 48, 7, 6, 16, 13, 7, 15, 4, 52, 7, 9, 21, 16, 7, 21, 5, 4, 61, 9, 14, 5, 13, 12, 6, 7, 9, 14, 10, 9, 21, 4, 64, 48, 52, 61, 63, 4, 7, 9, 14, 4, 48, 7, 6, 16, 13, 7, 15, 4, 52, 7, 9, 21, 16, 7, 21, 5, 4, 53, 5, 9, 5, 13, 7, 6, 10, 8, 9, 4, 64, 48, 52, 53, 63, 4, 20, 10, 6, 11, 4, 8, 27, 5, 13, 4, 6, 11, 10, 13, 6, 22, 39, 6, 20, 8, 4, 24, 13, 5, 6, 13, 7, 10, 9, 5, 14, 4, 18, 8, 14, 5, 15, 12, 4, 10, 9, 4, 8, 9, 5, 4, 11, 16, 9, 14, 13, 5, 14, 4, 24, 15, 16, 12, 4, 15, 7, 9, 21, 16, 7, 21, 5, 12, 4, 7, 9, 14, 4, 14, 5, 5, 24, 4, 10, 9, 6, 5, 13, 8, 24, 5, 13, 7, 25, 10, 15, 10, 6, 22, 4, 25, 5, 6, 20, 5, 5, 9, 4, 58, 7, 37, 23, 4, 49, 22, 32, 8, 13, 17, 11, 4, 7, 9, 14, 4, 32, 5, 9, 12, 8, 13, 55, 15, 8, 20, 26, 2], [4, 40, 47, 54, 32, 4, 10, 12, 4, 14, 5, 12, 10, 21, 9, 5, 14, 4, 6, 8, 4, 24, 13, 5, 39, 6, 13, 7, 10, 9, 4, 14, 5, 5, 24, 4, 25, 10, 14, 10, 13, 5, 17, 6, 10, 8, 9, 7, 15, 4, 13, 5, 24, 13, 5, 12, 5, 9, 6, 7, 6, 10, 8, 9, 12, 4, 19, 13, 8, 18, 4, 16, 9, 15, 7, 25, 5, 15, 5, 14, 4, 6, 5, 37, 6, 4, 25, 22, 4, 46, 8, 10, 9, 6, 15, 22, 4, 17, 8, 9, 14, 10, 6, 10, 8, 9, 10, 9, 21, 4, 8, 9, 4, 25, 8, 6, 11, 4, 15, 5, 19, 6, 4, 7, 9, 14, 4, 13, 10, 21, 11, 6, 4, 17, 8, 9, 6, 5, 37, 6, 4, 10, 9, 4, 7, 15, 15, 4, 15, 7, 22, 5, 13, 12, 26, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [4, 32, 11, 5, 4, 45, 16, 10, 17, 28, 4, 25, 13, 8, 20, 9, 4, 19, 8, 37, 4, 46, 16, 18, 24, 12, 4, 8, 27, 5, 13, 4, 6, 11, 5, 4, 15, 7, 57, 22, 4, 14, 8, 21, 26, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], ], '''attention_mask''': [ [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], ] } # fmt: on self.tokenizer_integration_test_util( expected_encoding=__SCREAMING_SNAKE_CASE , model_name='''microsoft/speecht5_asr''' , revision='''c5ef64c71905caeccde0e4462ef3f9077224c524''' , sequences=__SCREAMING_SNAKE_CASE , )
338
import warnings from diffusers import StableDiffusionInpaintPipeline as StableDiffusionInpaintPipeline # noqa F401 warnings.warn( '''The `inpainting.py` script is outdated. Please use directly `from diffusers import''' ''' StableDiffusionInpaintPipeline` instead.''' )
338
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowercase__ : Optional[int] = { '''configuration_blip_2''': [ '''BLIP_2_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''Blip2Config''', '''Blip2QFormerConfig''', '''Blip2VisionConfig''', ], '''processing_blip_2''': ['''Blip2Processor'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase__ : Tuple = [ '''BLIP_2_PRETRAINED_MODEL_ARCHIVE_LIST''', '''Blip2Model''', '''Blip2QFormerModel''', '''Blip2PreTrainedModel''', '''Blip2ForConditionalGeneration''', '''Blip2VisionModel''', ] if TYPE_CHECKING: from .configuration_blip_a import ( BLIP_2_PRETRAINED_CONFIG_ARCHIVE_MAP, BlipaConfig, BlipaQFormerConfig, BlipaVisionConfig, ) from .processing_blip_a import BlipaProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_blip_a import ( BLIP_2_PRETRAINED_MODEL_ARCHIVE_LIST, BlipaForConditionalGeneration, BlipaModel, BlipaPreTrainedModel, BlipaQFormerModel, BlipaVisionModel, ) else: import sys lowercase__ : int = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
338
import os import re import shutil import sys import tempfile import unittest import black lowercase__ : List[str] = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) sys.path.append(os.path.join(git_repo_path, '''utils''')) import check_copies # noqa: E402 # This is the reference code that will be used in the tests. # If BertLMPredictionHead is changed in modeling_bert.py, this code needs to be manually updated. lowercase__ : Dict = ''' def __init__(self, config): super().__init__() self.transform = BertPredictionHeadTransform(config) # The output weights are the same as the input embeddings, but there is # an output-only bias for each token. self.decoder = nn.Linear(config.hidden_size, config.vocab_size, bias=False) self.bias = nn.Parameter(torch.zeros(config.vocab_size)) # Need a link between the two variables so that the bias is correctly resized with `resize_token_embeddings` self.decoder.bias = self.bias def forward(self, hidden_states): hidden_states = self.transform(hidden_states) hidden_states = self.decoder(hidden_states) return hidden_states ''' class lowercase_ ( unittest.TestCase ): """simple docstring""" def SCREAMING_SNAKE_CASE_ ( self ) ->Dict: lowerCAmelCase = tempfile.mkdtemp() os.makedirs(os.path.join(self.transformer_dir , '''models/bert/''' ) ) lowerCAmelCase = self.transformer_dir shutil.copy( os.path.join(__SCREAMING_SNAKE_CASE , '''src/transformers/models/bert/modeling_bert.py''' ) , os.path.join(self.transformer_dir , '''models/bert/modeling_bert.py''' ) , ) def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[Any]: lowerCAmelCase = '''src/transformers''' shutil.rmtree(self.transformer_dir ) def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None ) ->Union[str, Any]: lowerCAmelCase = comment + F"\nclass {class_name}(nn.Module):\n" + class_code if overwrite_result is not None: lowerCAmelCase = comment + F"\nclass {class_name}(nn.Module):\n" + overwrite_result lowerCAmelCase = black.Mode(target_versions={black.TargetVersion.PYaa} , line_length=119 ) lowerCAmelCase = black.format_str(__SCREAMING_SNAKE_CASE , mode=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = os.path.join(self.transformer_dir , '''new_code.py''' ) with open(__SCREAMING_SNAKE_CASE , '''w''' , newline='''\n''' ) as f: f.write(__SCREAMING_SNAKE_CASE ) if overwrite_result is None: self.assertTrue(len(check_copies.is_copy_consistent(__SCREAMING_SNAKE_CASE ) ) == 0 ) else: check_copies.is_copy_consistent(f.name , overwrite=__SCREAMING_SNAKE_CASE ) with open(__SCREAMING_SNAKE_CASE , '''r''' ) as f: self.assertTrue(f.read() , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->int: lowerCAmelCase = check_copies.find_code_in_transformers('''models.bert.modeling_bert.BertLMPredictionHead''' ) self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->Tuple: # Base copy consistency self.check_copy_consistency( '''# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead''' , '''BertLMPredictionHead''' , REFERENCE_CODE + '''\n''' , ) # With no empty line at the end self.check_copy_consistency( '''# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead''' , '''BertLMPredictionHead''' , __SCREAMING_SNAKE_CASE , ) # Copy consistency with rename self.check_copy_consistency( '''# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->TestModel''' , '''TestModelLMPredictionHead''' , re.sub('''Bert''' , '''TestModel''' , __SCREAMING_SNAKE_CASE ) , ) # Copy consistency with a really long name lowerCAmelCase = '''TestModelWithAReallyLongNameBecauseSomePeopleLikeThatForSomeReason''' self.check_copy_consistency( F"# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->{long_class_name}" , F"{long_class_name}LMPredictionHead" , re.sub('''Bert''' , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) , ) # Copy consistency with overwrite self.check_copy_consistency( '''# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->TestModel''' , '''TestModelLMPredictionHead''' , __SCREAMING_SNAKE_CASE , overwrite_result=re.sub('''Bert''' , '''TestModel''' , __SCREAMING_SNAKE_CASE ) , ) def SCREAMING_SNAKE_CASE_ ( self ) ->Tuple: lowerCAmelCase = check_copies.LOCALIZED_READMES['''README_zh-hans.md'''] lowerCAmelCase = ( '''1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (from Google Research and the''' ''' Toyota Technological Institute at Chicago) released with the paper [ALBERT: A Lite BERT for''' ''' Self-supervised Learning of Language Representations](https://arxiv.org/abs/1909.11942), by Zhenzhong''' ''' Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut.\n1.''' ''' **[DistilBERT](https://huggingface.co/transformers/model_doc/distilbert.html)** (from HuggingFace),''' ''' released together with the paper [DistilBERT, a distilled version of BERT: smaller, faster, cheaper and''' ''' lighter](https://arxiv.org/abs/1910.01108) by Victor Sanh, Lysandre Debut and Thomas Wolf. The same''' ''' method has been applied to compress GPT2 into''' ''' [DistilGPT2](https://github.com/huggingface/transformers/tree/main/examples/distillation), RoBERTa into''' ''' [DistilRoBERTa](https://github.com/huggingface/transformers/tree/main/examples/distillation),''' ''' Multilingual BERT into''' ''' [DistilmBERT](https://github.com/huggingface/transformers/tree/main/examples/distillation) and a German''' ''' version of DistilBERT.\n1. **[ELECTRA](https://huggingface.co/transformers/model_doc/electra.html)**''' ''' (from Google Research/Stanford University) released with the paper [ELECTRA: Pre-training text encoders''' ''' as discriminators rather than generators](https://arxiv.org/abs/2003.10555) by Kevin Clark, Minh-Thang''' ''' Luong, Quoc V. Le, Christopher D. Manning.''' ) lowerCAmelCase = ( '''1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the''' ''' Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of''' ''' Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian''' ''' Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n''' ) lowerCAmelCase = ( '''1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the''' ''' Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of''' ''' Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian''' ''' Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n1.''' ''' **[DistilBERT](https://huggingface.co/transformers/model_doc/distilbert.html)** (来自 HuggingFace) 伴随论文''' ''' [DistilBERT, a distilled version of BERT: smaller, faster, cheaper and''' ''' lighter](https://arxiv.org/abs/1910.01108) 由 Victor Sanh, Lysandre Debut and Thomas Wolf 发布。 The same''' ''' method has been applied to compress GPT2 into''' ''' [DistilGPT2](https://github.com/huggingface/transformers/tree/main/examples/distillation), RoBERTa into''' ''' [DistilRoBERTa](https://github.com/huggingface/transformers/tree/main/examples/distillation),''' ''' Multilingual BERT into''' ''' [DistilmBERT](https://github.com/huggingface/transformers/tree/main/examples/distillation) and a German''' ''' version of DistilBERT.\n1. **[ELECTRA](https://huggingface.co/transformers/model_doc/electra.html)** (来自''' ''' Google Research/Stanford University) 伴随论文 [ELECTRA: Pre-training text encoders as discriminators rather''' ''' than generators](https://arxiv.org/abs/2003.10555) 由 Kevin Clark, Minh-Thang Luong, Quoc V. Le,''' ''' Christopher D. Manning 发布。\n''' ) lowerCAmelCase , lowerCAmelCase = check_copies.convert_to_localized_md( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , localized_readme['''format_model_list'''] ) self.assertFalse(__SCREAMING_SNAKE_CASE ) self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) lowerCAmelCase , lowerCAmelCase = check_copies.convert_to_localized_md( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , localized_readme['''format_model_list'''] ) # Check whether the number of models is equal to README.md after conversion. self.assertTrue(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = ( '''1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (from Google Research and the''' ''' Toyota Technological Institute at Chicago) released with the paper [ALBERT: A Lite BERT for''' ''' Self-supervised Learning of Language Representations](https://arxiv.org/abs/1909.11942), by Zhenzhong''' ''' Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut.''' ) lowerCAmelCase = ( '''1. **[ALBERT](https://huggingface.co/transformers/main/model_doc/albert.html)** (来自 Google Research and''' ''' the Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of''' ''' Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian''' ''' Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n''' ) lowerCAmelCase = ( '''1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the''' ''' Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of''' ''' Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian''' ''' Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n''' ) lowerCAmelCase , lowerCAmelCase = check_copies.convert_to_localized_md( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , localized_readme['''format_model_list'''] ) # Check if the model link is synchronized. self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE )
338
1
# Copyright 2021 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import argparse from ...utils.dataclasses import ( ComputeEnvironment, DistributedType, DynamoBackend, PrecisionType, SageMakerDistributedType, ) from ..menu import BulletMenu lowercase__ : Union[str, Any] = [ '''EAGER''', '''AOT_EAGER''', '''INDUCTOR''', '''NVFUSER''', '''AOT_NVFUSER''', '''AOT_CUDAGRAPHS''', '''OFI''', '''FX2TRT''', '''ONNXRT''', '''IPEX''', ] def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__=None , snake_case__=None , snake_case__=None ) -> Optional[int]: lowerCAmelCase = True while ask_again: lowerCAmelCase = input(snake_case__ ) try: if default is not None and len(snake_case__ ) == 0: return default return convert_value(snake_case__ ) if convert_value is not None else result except Exception: if error_message is not None: print(snake_case__ ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__=[] , snake_case__=None , snake_case__=0 ) -> Any: lowerCAmelCase = BulletMenu(snake_case__ , snake_case__ ) lowerCAmelCase = menu.run(default_choice=snake_case__ ) return convert_value(snake_case__ ) if convert_value is not None else result def SCREAMING_SNAKE_CASE_ ( snake_case__ ) -> Optional[int]: lowerCAmelCase = int(snake_case__ ) return ComputeEnvironment(['''LOCAL_MACHINE''', '''AMAZON_SAGEMAKER'''][value] ) def SCREAMING_SNAKE_CASE_ ( snake_case__ ) -> Tuple: lowerCAmelCase = int(snake_case__ ) return DistributedType(['''NO''', '''MULTI_CPU''', '''MULTI_XPU''', '''MULTI_GPU''', '''MULTI_NPU''', '''TPU'''][value] ) def SCREAMING_SNAKE_CASE_ ( snake_case__ ) -> Optional[Any]: lowerCAmelCase = int(snake_case__ ) return DynamoBackend(DYNAMO_BACKENDS[value] ).value def SCREAMING_SNAKE_CASE_ ( snake_case__ ) -> Dict: lowerCAmelCase = int(snake_case__ ) return PrecisionType(['''no''', '''fp16''', '''bf16''', '''fp8'''][value] ) def SCREAMING_SNAKE_CASE_ ( snake_case__ ) -> Optional[int]: lowerCAmelCase = int(snake_case__ ) return SageMakerDistributedType(['''NO''', '''DATA_PARALLEL''', '''MODEL_PARALLEL'''][value] ) def SCREAMING_SNAKE_CASE_ ( snake_case__ ) -> Optional[int]: return {"yes": True, "no": False}[value.lower()] class lowercase_ ( argparse.RawDescriptionHelpFormatter ): """simple docstring""" def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ->List[str]: lowerCAmelCase = super()._format_usage(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) lowerCAmelCase = usage.replace('''<command> [<args>] ''' , '''''' ) return usage
338
import pytest from datasets.splits import SplitDict, SplitInfo from datasets.utils.py_utils import asdict @pytest.mark.parametrize( '''split_dict''' , [ SplitDict(), SplitDict({'''train''': SplitInfo(name='''train''' , num_bytes=1_3_3_7 , num_examples=4_2 , dataset_name='''my_dataset''' )} ), SplitDict({'''train''': SplitInfo(name='''train''' , num_bytes=1_3_3_7 , num_examples=4_2 )} ), SplitDict({'''train''': SplitInfo()} ), ] , ) def SCREAMING_SNAKE_CASE_ ( snake_case__ ) -> Union[str, Any]: lowerCAmelCase = split_dict._to_yaml_list() assert len(snake_case__ ) == len(snake_case__ ) lowerCAmelCase = SplitDict._from_yaml_list(snake_case__ ) for split_name, split_info in split_dict.items(): # dataset_name field is deprecated, and is therefore not part of the YAML dump lowerCAmelCase = None # the split name of split_dict takes over the name of the split info object lowerCAmelCase = split_name assert split_dict == reloaded @pytest.mark.parametrize( '''split_info''' , [SplitInfo(), SplitInfo(dataset_name=snake_case__ ), SplitInfo(dataset_name='''my_dataset''' )] ) def SCREAMING_SNAKE_CASE_ ( snake_case__ ) -> Optional[int]: # For backward compatibility, we need asdict(split_dict) to return split info dictrionaries with the "dataset_name" # field even if it's deprecated. This way old versionso of `datasets` can still reload dataset_infos.json files lowerCAmelCase = asdict(SplitDict({'''train''': split_info} ) ) assert "dataset_name" in split_dict_asdict["train"] assert split_dict_asdict["train"]["dataset_name"] == split_info.dataset_name
338
1
import warnings from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class lowercase_ ( UpperCamelCase_ ): """simple docstring""" UpperCAmelCase_ : Dict = ["""image_processor""", """tokenizer"""] UpperCAmelCase_ : List[str] = """CLIPImageProcessor""" UpperCAmelCase_ : List[str] = ("""CLIPTokenizer""", """CLIPTokenizerFast""") def __init__( self , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE ) ->Optional[int]: lowerCAmelCase = None if "feature_extractor" in kwargs: warnings.warn( '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`''' ''' instead.''' , __SCREAMING_SNAKE_CASE , ) lowerCAmelCase = kwargs.pop('''feature_extractor''' ) lowerCAmelCase = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError('''You need to specify an `image_processor`.''' ) if tokenizer is None: raise ValueError('''You need to specify a `tokenizer`.''' ) super().__init__(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def __call__( self , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE ) ->Union[str, Any]: if text is None and images is None: raise ValueError('''You have to specify either text or images. Both cannot be none.''' ) if text is not None: lowerCAmelCase = self.tokenizer(__SCREAMING_SNAKE_CASE , return_tensors=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) if images is not None: lowerCAmelCase = self.image_processor(__SCREAMING_SNAKE_CASE , return_tensors=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) if text is not None and images is not None: lowerCAmelCase = image_features.pixel_values return encoding elif text is not None: return encoding else: return BatchEncoding(data=dict(**__SCREAMING_SNAKE_CASE ) , tensor_type=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) ->str: return self.tokenizer.batch_decode(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) ->List[str]: return self.tokenizer.decode(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) @property def SCREAMING_SNAKE_CASE_ ( self ) ->str: lowerCAmelCase = self.tokenizer.model_input_names lowerCAmelCase = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) ) @property def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[Any]: warnings.warn( '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.''' , __SCREAMING_SNAKE_CASE , ) return self.image_processor_class @property def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[int]: warnings.warn( '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.''' , __SCREAMING_SNAKE_CASE , ) return self.image_processor
338
import unittest import numpy as np def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ = None , ) -> np.ndarray: lowerCAmelCase = np.shape(snake_case__ ) lowerCAmelCase = np.shape(snake_case__ ) lowerCAmelCase = np.shape(snake_case__ ) if shape_a[0] != shape_b[0]: lowerCAmelCase = ( '''Expected the same number of rows for A and B. ''' f"Instead found A of size {shape_a} and B of size {shape_b}" ) raise ValueError(snake_case__ ) if shape_b[1] != shape_c[1]: lowerCAmelCase = ( '''Expected the same number of columns for B and C. ''' f"Instead found B of size {shape_b} and C of size {shape_c}" ) raise ValueError(snake_case__ ) lowerCAmelCase = pseudo_inv if a_inv is None: try: lowerCAmelCase = np.linalg.inv(snake_case__ ) except np.linalg.LinAlgError: raise ValueError( '''Input matrix A is not invertible. Cannot compute Schur complement.''' ) return mat_c - mat_b.T @ a_inv @ mat_b class lowercase_ ( unittest.TestCase ): """simple docstring""" def SCREAMING_SNAKE_CASE_ ( self ) ->None: lowerCAmelCase = np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]] ) lowerCAmelCase = np.array([[0, 3], [3, 0], [2, 3]] ) lowerCAmelCase = np.array([[2, 1], [6, 3]] ) lowerCAmelCase = schur_complement(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) lowerCAmelCase = np.block([[a, b], [b.T, c]] ) lowerCAmelCase = np.linalg.det(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = np.linalg.det(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = np.linalg.det(__SCREAMING_SNAKE_CASE ) self.assertAlmostEqual(__SCREAMING_SNAKE_CASE , det_a * det_s ) def SCREAMING_SNAKE_CASE_ ( self ) ->None: lowerCAmelCase = np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]] ) lowerCAmelCase = np.array([[0, 3], [3, 0], [2, 3]] ) lowerCAmelCase = np.array([[2, 1], [6, 3]] ) with self.assertRaises(__SCREAMING_SNAKE_CASE ): schur_complement(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->None: lowerCAmelCase = np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]] ) lowerCAmelCase = np.array([[0, 3], [3, 0], [2, 3]] ) lowerCAmelCase = np.array([[2, 1, 3], [6, 3, 5]] ) with self.assertRaises(__SCREAMING_SNAKE_CASE ): schur_complement(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) if __name__ == "__main__": import doctest doctest.testmod() unittest.main()
338
1
from typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowercase__ : str = {'''configuration_focalnet''': ['''FOCALNET_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''FocalNetConfig''']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase__ : List[Any] = [ '''FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST''', '''FocalNetForImageClassification''', '''FocalNetForMaskedImageModeling''', '''FocalNetBackbone''', '''FocalNetModel''', '''FocalNetPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_focalnet import FOCALNET_PRETRAINED_CONFIG_ARCHIVE_MAP, FocalNetConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_focalnet import ( FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST, FocalNetBackbone, FocalNetForImageClassification, FocalNetForMaskedImageModeling, FocalNetModel, FocalNetPreTrainedModel, ) else: import sys lowercase__ : Union[str, Any] = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
338
import argparse import hashlib import os import urllib import warnings import torch from torch import nn from tqdm import tqdm from transformers import WhisperConfig, WhisperForConditionalGeneration lowercase__ : Any = { '''tiny.en''': '''https://openaipublic.azureedge.net/main/whisper/models/d3dd57d32accea0b295c96e26691aa14d8822fac7d9d27d5dc00b4ca2826dd03/tiny.en.pt''', '''tiny''': '''https://openaipublic.azureedge.net/main/whisper/models/65147644a518d12f04e32d6f3b26facc3f8dd46e5390956a9424a650c0ce22b9/tiny.pt''', '''base.en''': '''https://openaipublic.azureedge.net/main/whisper/models/25a8566e1d0c1e2231d1c762132cd20e0f96a85d16145c3a00adf5d1ac670ead/base.en.pt''', '''base''': '''https://openaipublic.azureedge.net/main/whisper/models/ed3a0b6b1c0edf879ad9b11b1af5a0e6ab5db9205f891f668f8b0e6c6326e34e/base.pt''', '''small.en''': '''https://openaipublic.azureedge.net/main/whisper/models/f953ad0fd29cacd07d5a9eda5624af0f6bcf2258be67c92b79389873d91e0872/small.en.pt''', '''small''': '''https://openaipublic.azureedge.net/main/whisper/models/9ecf779972d90ba49c06d968637d720dd632c55bbf19d441fb42bf17a411e794/small.pt''', '''medium.en''': '''https://openaipublic.azureedge.net/main/whisper/models/d7440d1dc186f76616474e0ff0b3b6b879abc9d1a4926b7adfa41db2d497ab4f/medium.en.pt''', '''medium''': '''https://openaipublic.azureedge.net/main/whisper/models/345ae4da62f9b3d59415adc60127b97c714f32e89e936602e85993674d08dcb1/medium.pt''', '''large''': '''https://openaipublic.azureedge.net/main/whisper/models/e4b87e7e0bf463eb8e6956e646f1e277e901512310def2c24bf0e11bd3c28e9a/large.pt''', '''large-v2''': '''https://openaipublic.azureedge.net/main/whisper/models/81f7c96c852ee8fc832187b0132e569d6c3065a3252ed18e56effd0b6a73e524/large-v2.pt''', } def SCREAMING_SNAKE_CASE_ ( snake_case__ ) -> str: lowerCAmelCase = ['''layers''', '''blocks'''] for k in ignore_keys: state_dict.pop(snake_case__ , snake_case__ ) lowercase__ : List[Any] = { '''blocks''': '''layers''', '''mlp.0''': '''fc1''', '''mlp.2''': '''fc2''', '''mlp_ln''': '''final_layer_norm''', '''.attn.query''': '''.self_attn.q_proj''', '''.attn.key''': '''.self_attn.k_proj''', '''.attn.value''': '''.self_attn.v_proj''', '''.attn_ln''': '''.self_attn_layer_norm''', '''.attn.out''': '''.self_attn.out_proj''', '''.cross_attn.query''': '''.encoder_attn.q_proj''', '''.cross_attn.key''': '''.encoder_attn.k_proj''', '''.cross_attn.value''': '''.encoder_attn.v_proj''', '''.cross_attn_ln''': '''.encoder_attn_layer_norm''', '''.cross_attn.out''': '''.encoder_attn.out_proj''', '''decoder.ln.''': '''decoder.layer_norm.''', '''encoder.ln.''': '''encoder.layer_norm.''', '''token_embedding''': '''embed_tokens''', '''encoder.positional_embedding''': '''encoder.embed_positions.weight''', '''decoder.positional_embedding''': '''decoder.embed_positions.weight''', '''ln_post''': '''layer_norm''', } def SCREAMING_SNAKE_CASE_ ( snake_case__ ) -> Union[str, Any]: lowerCAmelCase = list(s_dict.keys() ) for key in keys: lowerCAmelCase = key for k, v in WHISPER_MAPPING.items(): if k in key: lowerCAmelCase = new_key.replace(snake_case__ , snake_case__ ) print(f"{key} -> {new_key}" ) lowerCAmelCase = s_dict.pop(snake_case__ ) return s_dict def SCREAMING_SNAKE_CASE_ ( snake_case__ ) -> Union[str, Any]: lowerCAmelCase , lowerCAmelCase = emb.weight.shape lowerCAmelCase = nn.Linear(snake_case__ , snake_case__ , bias=snake_case__ ) lowerCAmelCase = emb.weight.data return lin_layer def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ ) -> bytes: os.makedirs(snake_case__ , exist_ok=snake_case__ ) lowerCAmelCase = os.path.basename(snake_case__ ) lowerCAmelCase = url.split('''/''' )[-2] lowerCAmelCase = os.path.join(snake_case__ , snake_case__ ) if os.path.exists(snake_case__ ) and not os.path.isfile(snake_case__ ): raise RuntimeError(f"{download_target} exists and is not a regular file" ) if os.path.isfile(snake_case__ ): lowerCAmelCase = open(snake_case__ , '''rb''' ).read() if hashlib.shaaaa(snake_case__ ).hexdigest() == expected_shaaaa: return model_bytes else: warnings.warn(f"{download_target} exists, but the SHA256 checksum does not match; re-downloading the file" ) with urllib.request.urlopen(snake_case__ ) as source, open(snake_case__ , '''wb''' ) as output: with tqdm( total=int(source.info().get('''Content-Length''' ) ) , ncols=8_0 , unit='''iB''' , unit_scale=snake_case__ , unit_divisor=1_0_2_4 ) as loop: while True: lowerCAmelCase = source.read(8_1_9_2 ) if not buffer: break output.write(snake_case__ ) loop.update(len(snake_case__ ) ) lowerCAmelCase = open(snake_case__ , '''rb''' ).read() if hashlib.shaaaa(snake_case__ ).hexdigest() != expected_shaaaa: raise RuntimeError( '''Model has been downloaded but the SHA256 checksum does not not match. Please retry loading the model.''' ) return model_bytes def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ ) -> str: if ".pt" not in checkpoint_path: lowerCAmelCase = _download(_MODELS[checkpoint_path] ) else: lowerCAmelCase = torch.load(snake_case__ , map_location='''cpu''' ) lowerCAmelCase = original_checkpoint['''dims'''] lowerCAmelCase = original_checkpoint['''model_state_dict'''] lowerCAmelCase = state_dict['''decoder.token_embedding.weight'''] remove_ignore_keys_(snake_case__ ) rename_keys(snake_case__ ) lowerCAmelCase = True lowerCAmelCase = state_dict['''decoder.layers.0.fc1.weight'''].shape[0] lowerCAmelCase = WhisperConfig( vocab_size=dimensions['''n_vocab'''] , encoder_ffn_dim=snake_case__ , decoder_ffn_dim=snake_case__ , num_mel_bins=dimensions['''n_mels'''] , d_model=dimensions['''n_audio_state'''] , max_target_positions=dimensions['''n_text_ctx'''] , encoder_layers=dimensions['''n_audio_layer'''] , encoder_attention_heads=dimensions['''n_audio_head'''] , decoder_layers=dimensions['''n_text_layer'''] , decoder_attention_heads=dimensions['''n_text_state'''] , max_source_positions=dimensions['''n_audio_ctx'''] , ) lowerCAmelCase = WhisperForConditionalGeneration(snake_case__ ) lowerCAmelCase , lowerCAmelCase = model.model.load_state_dict(snake_case__ , strict=snake_case__ ) if len(snake_case__ ) > 0 and not set(snake_case__ ) <= { "encoder.embed_positions.weights", "decoder.embed_positions.weights", }: raise ValueError( '''Only `encoder.embed_positions.weights` and `decoder.embed_positions.weights` are allowed to be missing,''' f" but all the following weights are missing {missing}" ) if tie_embeds: lowerCAmelCase = make_linear_from_emb(model.model.decoder.embed_tokens ) else: lowerCAmelCase = proj_out_weights model.save_pretrained(snake_case__ ) if __name__ == "__main__": lowercase__ : List[str] = argparse.ArgumentParser() # # Required parameters parser.add_argument('''--checkpoint_path''', type=str, help='''Patht to the downloaded checkpoints''') parser.add_argument('''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''') lowercase__ : int = parser.parse_args() convert_openai_whisper_to_tfms(args.checkpoint_path, args.pytorch_dump_folder_path)
338
1
def SCREAMING_SNAKE_CASE_ ( snake_case__ ) -> bool: if not isinstance(snake_case__ , snake_case__ ): lowerCAmelCase = f"Input value of [number={number}] must be an integer" raise TypeError(snake_case__ ) if number < 0: return False lowerCAmelCase = number * number while number > 0: if number % 1_0 != number_square % 1_0: return False number //= 1_0 number_square //= 1_0 return True if __name__ == "__main__": import doctest doctest.testmod()
338
from ...processing_utils import ProcessorMixin class lowercase_ ( UpperCamelCase_ ): """simple docstring""" UpperCAmelCase_ : List[Any] = ["""image_processor""", """feature_extractor"""] UpperCAmelCase_ : Optional[int] = """TvltImageProcessor""" UpperCAmelCase_ : Optional[int] = """TvltFeatureExtractor""" def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ->Optional[int]: super().__init__(image_processor=__SCREAMING_SNAKE_CASE , feature_extractor=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = image_processor lowerCAmelCase = feature_extractor def __call__( self , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=False , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) ->List[Any]: if images is None and audio is None: raise ValueError('''You need to specify either an `images` or `audio` input to process.''' ) lowerCAmelCase = None if images is not None: lowerCAmelCase = self.image_processor(__SCREAMING_SNAKE_CASE , mask_pixel=__SCREAMING_SNAKE_CASE , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) if images_mixed is not None: lowerCAmelCase = self.image_processor(__SCREAMING_SNAKE_CASE , is_mixed=__SCREAMING_SNAKE_CASE , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) if audio is not None: lowerCAmelCase = self.feature_extractor( __SCREAMING_SNAKE_CASE , *__SCREAMING_SNAKE_CASE , sampling_rate=__SCREAMING_SNAKE_CASE , mask_audio=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) lowerCAmelCase = {} if audio is not None: output_dict.update(__SCREAMING_SNAKE_CASE ) if images is not None: output_dict.update(__SCREAMING_SNAKE_CASE ) if images_mixed_dict is not None: output_dict.update(__SCREAMING_SNAKE_CASE ) return output_dict @property def SCREAMING_SNAKE_CASE_ ( self ) ->Any: lowerCAmelCase = self.image_processor.model_input_names lowerCAmelCase = self.feature_extractor.model_input_names return list(dict.fromkeys(image_processor_input_names + feature_extractor_input_names ) )
338
1
from typing import Dict from transformers import EvalPrediction, HfArgumentParser, TrainingArguments, is_torch_available from transformers.testing_utils import ( TestCasePlus, execute_subprocess_async, get_torch_dist_unique_port, require_torch_multi_gpu, require_torch_neuroncore, ) from transformers.training_args import ParallelMode from transformers.utils import logging lowercase__ : Any = logging.get_logger(__name__) if is_torch_available(): import torch from torch import nn from torch.utils.data import Dataset from transformers import Trainer class lowercase_ ( UpperCamelCase_ ): """simple docstring""" def __init__( self , __SCREAMING_SNAKE_CASE = 101 ) ->Any: lowerCAmelCase = length def __len__( self ) ->int: return self.length def __getitem__( self , __SCREAMING_SNAKE_CASE ) ->int: return i class lowercase_ : """simple docstring""" def __call__( self , __SCREAMING_SNAKE_CASE ) ->Union[str, Any]: return {"input_ids": torch.tensor(__SCREAMING_SNAKE_CASE ), "labels": torch.tensor(__SCREAMING_SNAKE_CASE )} class lowercase_ ( nn.Module ): """simple docstring""" def __init__( self ) ->str: super().__init__() # Add some (unused) params otherwise DDP will complain. lowerCAmelCase = nn.Linear(120 , 80 ) def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None ) ->List[str]: if labels is not None: return torch.tensor(0.0 , device=input_ids.device ), input_ids else: return input_ids class lowercase_ ( UpperCamelCase_ ): """simple docstring""" @require_torch_neuroncore def SCREAMING_SNAKE_CASE_ ( self ) ->List[str]: lowerCAmelCase = F"--nproc_per_node=2\n --master_port={get_torch_dist_unique_port()}\n {self.test_file_dir}/test_trainer_distributed.py\n ".split() lowerCAmelCase = self.get_auto_remove_tmp_dir() lowerCAmelCase = F"--output_dir {output_dir}".split() lowerCAmelCase = ['''torchrun'''] + distributed_args + args execute_subprocess_async(__SCREAMING_SNAKE_CASE , env=self.get_env() ) # successful return here == success - any errors would have caused an error in the sub-call class lowercase_ ( UpperCamelCase_ ): """simple docstring""" @require_torch_multi_gpu def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[Any]: lowerCAmelCase = F"--nproc_per_node={torch.cuda.device_count()}\n --master_port={get_torch_dist_unique_port()}\n {self.test_file_dir}/test_trainer_distributed.py\n ".split() lowerCAmelCase = self.get_auto_remove_tmp_dir() lowerCAmelCase = F"--output_dir {output_dir}".split() lowerCAmelCase = ['''torchrun'''] + distributed_args + args execute_subprocess_async(__SCREAMING_SNAKE_CASE , env=self.get_env() ) # successful return here == success - any errors would have caused an error in the sub-call if __name__ == "__main__": # The script below is meant to be run under torch.distributed, on a machine with multiple GPUs: # # PYTHONPATH="src" python -m torch.distributed.run --nproc_per_node 2 --output_dir output_dir ./tests/test_trainer_distributed.py lowercase__ : List[Any] = HfArgumentParser((TrainingArguments,)) lowercase__ : Tuple = parser.parse_args_into_dataclasses()[0] logger.warning( f'Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}, ' f'distributed training: {training_args.parallel_mode != ParallelMode.NOT_DISTRIBUTED}' ) # Essentially, what we want to verify in the distributed case is that we get all samples back, # in the right order. (this is crucial for prediction for instance) for dataset_length in [1_0_1, 4_0, 7]: lowercase__ : List[Any] = DummyDataset(dataset_length) def SCREAMING_SNAKE_CASE_ ( snake_case__ ) -> Dict: lowerCAmelCase = list(range(len(snake_case__ ) ) ) lowerCAmelCase = p.predictions.tolist() == sequential and p.label_ids.tolist() == sequential if not success and training_args.local_rank == 0: logger.warning( '''Predictions and/or labels do not match expected results:\n - predictions: ''' f"{p.predictions.tolist()}\n - labels: {p.label_ids.tolist()}\n - expected: {sequential}" ) return {"success": success} lowercase__ : List[str] = Trainer( model=DummyModel(), args=training_args, data_collator=DummyDataCollator(), eval_dataset=dataset, compute_metrics=compute_metrics, ) lowercase__ : List[str] = trainer.evaluate() logger.info(metrics) if metrics["eval_success"] is not True: logger.error(metrics) exit(1) lowercase__ : Tuple = trainer.predict(dataset) logger.info(p.metrics) if p.metrics["test_success"] is not True: logger.error(p.metrics) exit(1) lowercase__ : Union[str, Any] = 2 lowercase__ : int = trainer.evaluate() logger.info(metrics) if metrics["eval_success"] is not True: logger.error(metrics) exit(1) lowercase__ : int = trainer.predict(dataset) logger.info(p.metrics) if p.metrics["test_success"] is not True: logger.error(p.metrics) exit(1) lowercase__ : Union[str, Any] = None
338
def SCREAMING_SNAKE_CASE_ ( snake_case__ ) -> List[str]: lowerCAmelCase = len(snake_case__ ) for i in range(length - 1 ): lowerCAmelCase = i for k in range(i + 1 , snake_case__ ): if collection[k] < collection[least]: lowerCAmelCase = k if least != i: lowerCAmelCase , lowerCAmelCase = (collection[i], collection[least]) return collection if __name__ == "__main__": lowercase__ : Optional[int] = input('''Enter numbers separated by a comma:\n''').strip() lowercase__ : str = [int(item) for item in user_input.split(''',''')] print(selection_sort(unsorted))
338
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available lowercase__ : List[Any] = { '''configuration_altclip''': [ '''ALTCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''AltCLIPConfig''', '''AltCLIPTextConfig''', '''AltCLIPVisionConfig''', ], '''processing_altclip''': ['''AltCLIPProcessor'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase__ : Dict = [ '''ALTCLIP_PRETRAINED_MODEL_ARCHIVE_LIST''', '''AltCLIPPreTrainedModel''', '''AltCLIPModel''', '''AltCLIPTextModel''', '''AltCLIPVisionModel''', ] if TYPE_CHECKING: from .configuration_altclip import ( ALTCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, AltCLIPConfig, AltCLIPTextConfig, AltCLIPVisionConfig, ) from .processing_altclip import AltCLIPProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_altclip import ( ALTCLIP_PRETRAINED_MODEL_ARCHIVE_LIST, AltCLIPModel, AltCLIPPreTrainedModel, AltCLIPTextModel, AltCLIPVisionModel, ) else: import sys lowercase__ : str = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
338
import unittest from transformers import EsmConfig, is_torch_available from transformers.testing_utils import TestCasePlus, require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers.models.esm.modeling_esmfold import EsmForProteinFolding class lowercase_ : """simple docstring""" def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=13 , __SCREAMING_SNAKE_CASE=7 , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=19 , __SCREAMING_SNAKE_CASE=32 , __SCREAMING_SNAKE_CASE=5 , __SCREAMING_SNAKE_CASE=4 , __SCREAMING_SNAKE_CASE=37 , __SCREAMING_SNAKE_CASE="gelu" , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=512 , __SCREAMING_SNAKE_CASE=16 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=0.0_2 , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=4 , __SCREAMING_SNAKE_CASE=None , ) ->Union[str, Any]: 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 SCREAMING_SNAKE_CASE_ ( self ) ->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 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, input_mask, sequence_labels, token_labels, choice_labels def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[int]: lowerCAmelCase = EsmConfig( vocab_size=33 , hidden_size=self.hidden_size , pad_token_id=1 , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , is_folding_model=__SCREAMING_SNAKE_CASE , esmfold_config={'''trunk''': {'''num_blocks''': 2}, '''fp16_esm''': False} , ) return config def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ->Tuple: lowerCAmelCase = EsmForProteinFolding(config=__SCREAMING_SNAKE_CASE ).float() model.to(__SCREAMING_SNAKE_CASE ) model.eval() lowerCAmelCase = model(__SCREAMING_SNAKE_CASE , attention_mask=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = model(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = model(__SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.positions.shape , (8, self.batch_size, self.seq_length, 14, 3) ) self.parent.assertEqual(result.angles.shape , (8, self.batch_size, self.seq_length, 7, 2) ) def SCREAMING_SNAKE_CASE_ ( self ) ->int: lowerCAmelCase = self.prepare_config_and_inputs() ( ( lowerCAmelCase ) , ( lowerCAmelCase ) , ( lowerCAmelCase ) , ( lowerCAmelCase ) , ( lowerCAmelCase ) , ( lowerCAmelCase ) , ) = config_and_inputs lowerCAmelCase = {'''input_ids''': input_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_torch class lowercase_ ( UpperCamelCase_ , UpperCamelCase_ , unittest.TestCase ): """simple docstring""" UpperCAmelCase_ : Optional[Any] = False UpperCAmelCase_ : Dict = (EsmForProteinFolding,) if is_torch_available() else () UpperCAmelCase_ : List[Any] = () UpperCAmelCase_ : Tuple = {} if is_torch_available() else {} UpperCAmelCase_ : List[str] = False def SCREAMING_SNAKE_CASE_ ( self ) ->Dict: lowerCAmelCase = EsmFoldModelTester(self ) lowerCAmelCase = ConfigTester(self , config_class=__SCREAMING_SNAKE_CASE , hidden_size=37 ) def SCREAMING_SNAKE_CASE_ ( self ) ->Any: self.config_tester.run_common_tests() def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[int]: lowerCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE ) @unittest.skip('''Does not support attention outputs''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->Tuple: pass @unittest.skip def SCREAMING_SNAKE_CASE_ ( self ) ->Any: pass @unittest.skip('''Esm does not support embedding resizing''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->List[str]: pass @unittest.skip('''Esm does not support embedding resizing''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->str: pass @unittest.skip('''ESMFold does not support passing input embeds!''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->List[Any]: pass @unittest.skip('''ESMFold does not support head pruning.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->str: pass @unittest.skip('''ESMFold does not support head pruning.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->Dict: pass @unittest.skip('''ESMFold does not support head pruning.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->List[str]: pass @unittest.skip('''ESMFold does not support head pruning.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->List[Any]: pass @unittest.skip('''ESMFold does not support head pruning.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[int]: pass @unittest.skip('''ESMFold does not output hidden states in the normal way.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->Tuple: pass @unittest.skip('''ESMfold does not output hidden states in the normal way.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->Dict: pass @unittest.skip('''ESMFold only has one output format.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->List[str]: pass @unittest.skip('''This test doesn\'t work for ESMFold and doesn\'t test core functionality''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->Dict: pass @unittest.skip('''ESMFold does not support input chunking.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->Union[str, Any]: pass @unittest.skip('''ESMFold doesn\'t respect you and it certainly doesn\'t respect your initialization arguments.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[Any]: pass @unittest.skip('''ESMFold doesn\'t support torchscript compilation.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->str: pass @unittest.skip('''ESMFold doesn\'t support torchscript compilation.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->Any: pass @unittest.skip('''ESMFold doesn\'t support torchscript compilation.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->str: pass @unittest.skip('''ESMFold doesn\'t support data parallel.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->str: pass @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[int]: pass @require_torch class lowercase_ ( UpperCamelCase_ ): """simple docstring""" @slow def SCREAMING_SNAKE_CASE_ ( self ) ->str: lowerCAmelCase = EsmForProteinFolding.from_pretrained('''facebook/esmfold_v1''' ).float() model.eval() lowerCAmelCase = torch.tensor([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]] ) lowerCAmelCase = model(__SCREAMING_SNAKE_CASE )['''positions'''] lowerCAmelCase = torch.tensor([2.5_8_2_8, 0.7_9_9_3, -1_0.9_3_3_4] , dtype=torch.floataa ) self.assertTrue(torch.allclose(position_outputs[0, 0, 0, 0] , __SCREAMING_SNAKE_CASE , atol=1e-4 ) )
338
1
import argparse import os import torch from diffusers import ( CMStochasticIterativeScheduler, ConsistencyModelPipeline, UNetaDModel, ) lowercase__ : Dict = { '''sample_size''': 3_2, '''in_channels''': 3, '''out_channels''': 3, '''layers_per_block''': 2, '''num_class_embeds''': 1_0_0_0, '''block_out_channels''': [3_2, 6_4], '''attention_head_dim''': 8, '''down_block_types''': [ '''ResnetDownsampleBlock2D''', '''AttnDownBlock2D''', ], '''up_block_types''': [ '''AttnUpBlock2D''', '''ResnetUpsampleBlock2D''', ], '''resnet_time_scale_shift''': '''scale_shift''', '''upsample_type''': '''resnet''', '''downsample_type''': '''resnet''', } lowercase__ : int = { '''sample_size''': 6_4, '''in_channels''': 3, '''out_channels''': 3, '''layers_per_block''': 3, '''num_class_embeds''': 1_0_0_0, '''block_out_channels''': [1_9_2, 1_9_2 * 2, 1_9_2 * 3, 1_9_2 * 4], '''attention_head_dim''': 6_4, '''down_block_types''': [ '''ResnetDownsampleBlock2D''', '''AttnDownBlock2D''', '''AttnDownBlock2D''', '''AttnDownBlock2D''', ], '''up_block_types''': [ '''AttnUpBlock2D''', '''AttnUpBlock2D''', '''AttnUpBlock2D''', '''ResnetUpsampleBlock2D''', ], '''resnet_time_scale_shift''': '''scale_shift''', '''upsample_type''': '''resnet''', '''downsample_type''': '''resnet''', } lowercase__ : Dict = { '''sample_size''': 2_5_6, '''in_channels''': 3, '''out_channels''': 3, '''layers_per_block''': 2, '''num_class_embeds''': None, '''block_out_channels''': [2_5_6, 2_5_6, 2_5_6 * 2, 2_5_6 * 2, 2_5_6 * 4, 2_5_6 * 4], '''attention_head_dim''': 6_4, '''down_block_types''': [ '''ResnetDownsampleBlock2D''', '''ResnetDownsampleBlock2D''', '''ResnetDownsampleBlock2D''', '''AttnDownBlock2D''', '''AttnDownBlock2D''', '''AttnDownBlock2D''', ], '''up_block_types''': [ '''AttnUpBlock2D''', '''AttnUpBlock2D''', '''AttnUpBlock2D''', '''ResnetUpsampleBlock2D''', '''ResnetUpsampleBlock2D''', '''ResnetUpsampleBlock2D''', ], '''resnet_time_scale_shift''': '''default''', '''upsample_type''': '''resnet''', '''downsample_type''': '''resnet''', } lowercase__ : Optional[Any] = { '''num_train_timesteps''': 4_0, '''sigma_min''': 0.0_0_2, '''sigma_max''': 8_0.0, } lowercase__ : List[Any] = { '''num_train_timesteps''': 2_0_1, '''sigma_min''': 0.0_0_2, '''sigma_max''': 8_0.0, } lowercase__ : Dict = { '''num_train_timesteps''': 1_5_1, '''sigma_min''': 0.0_0_2, '''sigma_max''': 8_0.0, } def SCREAMING_SNAKE_CASE_ ( snake_case__ ) -> Any: if isinstance(snake_case__ , snake_case__ ): return v if v.lower() in ("yes", "true", "t", "y", "1"): return True elif v.lower() in ("no", "false", "f", "n", "0"): return False else: raise argparse.ArgumentTypeError('''boolean value expected''' ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__=False ) -> Any: lowerCAmelCase = checkpoint[f"{old_prefix}.in_layers.0.weight"] lowerCAmelCase = checkpoint[f"{old_prefix}.in_layers.0.bias"] lowerCAmelCase = checkpoint[f"{old_prefix}.in_layers.2.weight"] lowerCAmelCase = checkpoint[f"{old_prefix}.in_layers.2.bias"] lowerCAmelCase = checkpoint[f"{old_prefix}.emb_layers.1.weight"] lowerCAmelCase = checkpoint[f"{old_prefix}.emb_layers.1.bias"] lowerCAmelCase = checkpoint[f"{old_prefix}.out_layers.0.weight"] lowerCAmelCase = checkpoint[f"{old_prefix}.out_layers.0.bias"] lowerCAmelCase = checkpoint[f"{old_prefix}.out_layers.3.weight"] lowerCAmelCase = checkpoint[f"{old_prefix}.out_layers.3.bias"] if has_skip: lowerCAmelCase = checkpoint[f"{old_prefix}.skip_connection.weight"] lowerCAmelCase = checkpoint[f"{old_prefix}.skip_connection.bias"] return new_checkpoint def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__=None ) -> Tuple: lowerCAmelCase , lowerCAmelCase , lowerCAmelCase = checkpoint[f"{old_prefix}.qkv.weight"].chunk(3 , dim=0 ) lowerCAmelCase , lowerCAmelCase , lowerCAmelCase = checkpoint[f"{old_prefix}.qkv.bias"].chunk(3 , dim=0 ) lowerCAmelCase = checkpoint[f"{old_prefix}.norm.weight"] lowerCAmelCase = checkpoint[f"{old_prefix}.norm.bias"] lowerCAmelCase = weight_q.squeeze(-1 ).squeeze(-1 ) lowerCAmelCase = bias_q.squeeze(-1 ).squeeze(-1 ) lowerCAmelCase = weight_k.squeeze(-1 ).squeeze(-1 ) lowerCAmelCase = bias_k.squeeze(-1 ).squeeze(-1 ) lowerCAmelCase = weight_v.squeeze(-1 ).squeeze(-1 ) lowerCAmelCase = bias_v.squeeze(-1 ).squeeze(-1 ) lowerCAmelCase = ( checkpoint[f"{old_prefix}.proj_out.weight"].squeeze(-1 ).squeeze(-1 ) ) lowerCAmelCase = checkpoint[f"{old_prefix}.proj_out.bias"].squeeze(-1 ).squeeze(-1 ) return new_checkpoint def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ ) -> int: lowerCAmelCase = torch.load(snake_case__ , map_location='''cpu''' ) lowerCAmelCase = {} lowerCAmelCase = checkpoint['''time_embed.0.weight'''] lowerCAmelCase = checkpoint['''time_embed.0.bias'''] lowerCAmelCase = checkpoint['''time_embed.2.weight'''] lowerCAmelCase = checkpoint['''time_embed.2.bias'''] if unet_config["num_class_embeds"] is not None: lowerCAmelCase = checkpoint['''label_emb.weight'''] lowerCAmelCase = checkpoint['''input_blocks.0.0.weight'''] lowerCAmelCase = checkpoint['''input_blocks.0.0.bias'''] lowerCAmelCase = unet_config['''down_block_types'''] lowerCAmelCase = unet_config['''layers_per_block'''] lowerCAmelCase = unet_config['''attention_head_dim'''] lowerCAmelCase = unet_config['''block_out_channels'''] lowerCAmelCase = 1 lowerCAmelCase = channels_list[0] for i, layer_type in enumerate(snake_case__ ): lowerCAmelCase = channels_list[i] lowerCAmelCase = current_channels != prev_channels if layer_type == "ResnetDownsampleBlock2D": for j in range(snake_case__ ): lowerCAmelCase = f"down_blocks.{i}.resnets.{j}" lowerCAmelCase = f"input_blocks.{current_layer}.0" lowerCAmelCase = True if j == 0 and downsample_block_has_skip else False lowerCAmelCase = convert_resnet(snake_case__ , snake_case__ , snake_case__ , snake_case__ , has_skip=snake_case__ ) current_layer += 1 elif layer_type == "AttnDownBlock2D": for j in range(snake_case__ ): lowerCAmelCase = f"down_blocks.{i}.resnets.{j}" lowerCAmelCase = f"input_blocks.{current_layer}.0" lowerCAmelCase = True if j == 0 and downsample_block_has_skip else False lowerCAmelCase = convert_resnet(snake_case__ , snake_case__ , snake_case__ , snake_case__ , has_skip=snake_case__ ) lowerCAmelCase = f"down_blocks.{i}.attentions.{j}" lowerCAmelCase = f"input_blocks.{current_layer}.1" lowerCAmelCase = convert_attention( snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ ) current_layer += 1 if i != len(snake_case__ ) - 1: lowerCAmelCase = f"down_blocks.{i}.downsamplers.0" lowerCAmelCase = f"input_blocks.{current_layer}.0" lowerCAmelCase = convert_resnet(snake_case__ , snake_case__ , snake_case__ , snake_case__ ) current_layer += 1 lowerCAmelCase = current_channels # hardcoded the mid-block for now lowerCAmelCase = '''mid_block.resnets.0''' lowerCAmelCase = '''middle_block.0''' lowerCAmelCase = convert_resnet(snake_case__ , snake_case__ , snake_case__ , snake_case__ ) lowerCAmelCase = '''mid_block.attentions.0''' lowerCAmelCase = '''middle_block.1''' lowerCAmelCase = convert_attention(snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ ) lowerCAmelCase = '''mid_block.resnets.1''' lowerCAmelCase = '''middle_block.2''' lowerCAmelCase = convert_resnet(snake_case__ , snake_case__ , snake_case__ , snake_case__ ) lowerCAmelCase = 0 lowerCAmelCase = unet_config['''up_block_types'''] for i, layer_type in enumerate(snake_case__ ): if layer_type == "ResnetUpsampleBlock2D": for j in range(layers_per_block + 1 ): lowerCAmelCase = f"up_blocks.{i}.resnets.{j}" lowerCAmelCase = f"output_blocks.{current_layer}.0" lowerCAmelCase = convert_resnet(snake_case__ , snake_case__ , snake_case__ , snake_case__ , has_skip=snake_case__ ) current_layer += 1 if i != len(snake_case__ ) - 1: lowerCAmelCase = f"up_blocks.{i}.upsamplers.0" lowerCAmelCase = f"output_blocks.{current_layer-1}.1" lowerCAmelCase = convert_resnet(snake_case__ , snake_case__ , snake_case__ , snake_case__ ) elif layer_type == "AttnUpBlock2D": for j in range(layers_per_block + 1 ): lowerCAmelCase = f"up_blocks.{i}.resnets.{j}" lowerCAmelCase = f"output_blocks.{current_layer}.0" lowerCAmelCase = convert_resnet(snake_case__ , snake_case__ , snake_case__ , snake_case__ , has_skip=snake_case__ ) lowerCAmelCase = f"up_blocks.{i}.attentions.{j}" lowerCAmelCase = f"output_blocks.{current_layer}.1" lowerCAmelCase = convert_attention( snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ ) current_layer += 1 if i != len(snake_case__ ) - 1: lowerCAmelCase = f"up_blocks.{i}.upsamplers.0" lowerCAmelCase = f"output_blocks.{current_layer-1}.2" lowerCAmelCase = convert_resnet(snake_case__ , snake_case__ , snake_case__ , snake_case__ ) lowerCAmelCase = checkpoint['''out.0.weight'''] lowerCAmelCase = checkpoint['''out.0.bias'''] lowerCAmelCase = checkpoint['''out.2.weight'''] lowerCAmelCase = checkpoint['''out.2.bias'''] return new_checkpoint if __name__ == "__main__": lowercase__ : Any = argparse.ArgumentParser() parser.add_argument('''--unet_path''', default=None, type=str, required=True, help='''Path to the unet.pt to convert.''') parser.add_argument( '''--dump_path''', default=None, type=str, required=True, help='''Path to output the converted UNet model.''' ) parser.add_argument('''--class_cond''', default=True, type=str, help='''Whether the model is class-conditional.''') lowercase__ : List[str] = parser.parse_args() lowercase__ : List[Any] = strabool(args.class_cond) lowercase__ : Union[str, Any] = os.path.basename(args.unet_path) print(f'Checkpoint: {ckpt_name}') # Get U-Net config if "imagenet64" in ckpt_name: lowercase__ : int = IMAGENET_64_UNET_CONFIG elif "256" in ckpt_name and (("bedroom" in ckpt_name) or ("cat" in ckpt_name)): lowercase__ : Dict = LSUN_256_UNET_CONFIG elif "test" in ckpt_name: lowercase__ : List[str] = TEST_UNET_CONFIG else: raise ValueError(f'Checkpoint type {ckpt_name} is not currently supported.') if not args.class_cond: lowercase__ : Optional[Any] = None lowercase__ : List[Any] = con_pt_to_diffuser(args.unet_path, unet_config) lowercase__ : str = UNetaDModel(**unet_config) image_unet.load_state_dict(converted_unet_ckpt) # Get scheduler config if "cd" in ckpt_name or "test" in ckpt_name: lowercase__ : int = CD_SCHEDULER_CONFIG elif "ct" in ckpt_name and "imagenet64" in ckpt_name: lowercase__ : List[str] = CT_IMAGENET_64_SCHEDULER_CONFIG elif "ct" in ckpt_name and "256" in ckpt_name and (("bedroom" in ckpt_name) or ("cat" in ckpt_name)): lowercase__ : Optional[int] = CT_LSUN_256_SCHEDULER_CONFIG else: raise ValueError(f'Checkpoint type {ckpt_name} is not currently supported.') lowercase__ : Any = CMStochasticIterativeScheduler(**scheduler_config) lowercase__ : str = ConsistencyModelPipeline(unet=image_unet, scheduler=cm_scheduler) consistency_model.save_pretrained(args.dump_path)
338
import warnings from typing import List import numpy as np from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding from ...utils import is_flax_available, is_tf_available, is_torch_available class lowercase_ ( UpperCamelCase_ ): """simple docstring""" UpperCAmelCase_ : List[str] = ["""image_processor""", """tokenizer"""] UpperCAmelCase_ : int = """OwlViTImageProcessor""" UpperCAmelCase_ : Any = ("""CLIPTokenizer""", """CLIPTokenizerFast""") def __init__( self , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE ) ->Any: lowerCAmelCase = None if "feature_extractor" in kwargs: warnings.warn( '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`''' ''' instead.''' , __SCREAMING_SNAKE_CASE , ) lowerCAmelCase = kwargs.pop('''feature_extractor''' ) lowerCAmelCase = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError('''You need to specify an `image_processor`.''' ) if tokenizer is None: raise ValueError('''You need to specify a `tokenizer`.''' ) super().__init__(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def __call__( self , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE="max_length" , __SCREAMING_SNAKE_CASE="np" , **__SCREAMING_SNAKE_CASE ) ->int: if text is None and query_images is None and images is None: raise ValueError( '''You have to specify at least one text or query image or image. All three cannot be none.''' ) if text is not None: if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) or (isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) and not isinstance(text[0] , __SCREAMING_SNAKE_CASE )): lowerCAmelCase = [self.tokenizer(__SCREAMING_SNAKE_CASE , padding=__SCREAMING_SNAKE_CASE , return_tensors=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE )] elif isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) and isinstance(text[0] , __SCREAMING_SNAKE_CASE ): lowerCAmelCase = [] # Maximum number of queries across batch lowerCAmelCase = max([len(__SCREAMING_SNAKE_CASE ) for t in text] ) # Pad all batch samples to max number of text queries for t in text: if len(__SCREAMING_SNAKE_CASE ) != max_num_queries: lowerCAmelCase = t + [''' '''] * (max_num_queries - len(__SCREAMING_SNAKE_CASE )) lowerCAmelCase = self.tokenizer(__SCREAMING_SNAKE_CASE , padding=__SCREAMING_SNAKE_CASE , return_tensors=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) encodings.append(__SCREAMING_SNAKE_CASE ) else: raise TypeError('''Input text should be a string, a list of strings or a nested list of strings''' ) if return_tensors == "np": lowerCAmelCase = np.concatenate([encoding['''input_ids'''] for encoding in encodings] , axis=0 ) lowerCAmelCase = np.concatenate([encoding['''attention_mask'''] for encoding in encodings] , axis=0 ) elif return_tensors == "jax" and is_flax_available(): import jax.numpy as jnp lowerCAmelCase = jnp.concatenate([encoding['''input_ids'''] for encoding in encodings] , axis=0 ) lowerCAmelCase = jnp.concatenate([encoding['''attention_mask'''] for encoding in encodings] , axis=0 ) elif return_tensors == "pt" and is_torch_available(): import torch lowerCAmelCase = torch.cat([encoding['''input_ids'''] for encoding in encodings] , dim=0 ) lowerCAmelCase = torch.cat([encoding['''attention_mask'''] for encoding in encodings] , dim=0 ) elif return_tensors == "tf" and is_tf_available(): import tensorflow as tf lowerCAmelCase = tf.stack([encoding['''input_ids'''] for encoding in encodings] , axis=0 ) lowerCAmelCase = tf.stack([encoding['''attention_mask'''] for encoding in encodings] , axis=0 ) else: raise ValueError('''Target return tensor type could not be returned''' ) lowerCAmelCase = BatchEncoding() lowerCAmelCase = input_ids lowerCAmelCase = attention_mask if query_images is not None: lowerCAmelCase = BatchEncoding() lowerCAmelCase = self.image_processor( __SCREAMING_SNAKE_CASE , return_tensors=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).pixel_values lowerCAmelCase = query_pixel_values if images is not None: lowerCAmelCase = self.image_processor(__SCREAMING_SNAKE_CASE , return_tensors=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) if text is not None and images is not None: lowerCAmelCase = image_features.pixel_values return encoding elif query_images is not None and images is not None: lowerCAmelCase = image_features.pixel_values return encoding elif text is not None or query_images is not None: return encoding else: return BatchEncoding(data=dict(**__SCREAMING_SNAKE_CASE ) , tensor_type=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) ->Optional[int]: return self.image_processor.post_process(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) ->Any: return self.image_processor.post_process_object_detection(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) ->Tuple: return self.image_processor.post_process_image_guided_detection(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) ->str: return self.tokenizer.batch_decode(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) ->List[Any]: return self.tokenizer.decode(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) @property def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[int]: warnings.warn( '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.''' , __SCREAMING_SNAKE_CASE , ) return self.image_processor_class @property def SCREAMING_SNAKE_CASE_ ( self ) ->int: warnings.warn( '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.''' , __SCREAMING_SNAKE_CASE , ) return self.image_processor
338
1
import argparse lowercase__ : Optional[Any] = '''docs/source/_static/js/custom.js''' def SCREAMING_SNAKE_CASE_ ( snake_case__ ) -> Optional[int]: with open(snake_case__ , encoding='''utf-8''' , newline='''\n''' ) as f: lowerCAmelCase = f.readlines() lowerCAmelCase = 0 # First let's put the right version while not lines[index].startswith('''const stableVersion =''' ): index += 1 lowerCAmelCase = f"const stableVersion = \"v{version}\"\n" # Then update the dictionary while not lines[index].startswith('''const versionMapping = {''' ): index += 1 # We go until the end while not lines[index].startswith('''}''' ): index += 1 # We add the new version at the end lines[index - 1] += f" \"v{version}\": \"v{version}\",\n" with open(snake_case__ , '''w''' , encoding='''utf-8''' , newline='''\n''' ) as f: f.writelines(snake_case__ ) if __name__ == "__main__": lowercase__ : List[str] = argparse.ArgumentParser() parser.add_argument('''--version''', help='''Release version.''') lowercase__ : Dict = parser.parse_args() update_custom_js(args.version)
338
import os import unicodedata from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging lowercase__ : List[Any] = logging.get_logger(__name__) lowercase__ : Optional[Any] = {'''vocab_file''': '''spiece.model'''} lowercase__ : Optional[int] = { '''vocab_file''': { '''albert-base-v1''': '''https://huggingface.co/albert-base-v1/resolve/main/spiece.model''', '''albert-large-v1''': '''https://huggingface.co/albert-large-v1/resolve/main/spiece.model''', '''albert-xlarge-v1''': '''https://huggingface.co/albert-xlarge-v1/resolve/main/spiece.model''', '''albert-xxlarge-v1''': '''https://huggingface.co/albert-xxlarge-v1/resolve/main/spiece.model''', '''albert-base-v2''': '''https://huggingface.co/albert-base-v2/resolve/main/spiece.model''', '''albert-large-v2''': '''https://huggingface.co/albert-large-v2/resolve/main/spiece.model''', '''albert-xlarge-v2''': '''https://huggingface.co/albert-xlarge-v2/resolve/main/spiece.model''', '''albert-xxlarge-v2''': '''https://huggingface.co/albert-xxlarge-v2/resolve/main/spiece.model''', } } lowercase__ : Any = { '''albert-base-v1''': 5_1_2, '''albert-large-v1''': 5_1_2, '''albert-xlarge-v1''': 5_1_2, '''albert-xxlarge-v1''': 5_1_2, '''albert-base-v2''': 5_1_2, '''albert-large-v2''': 5_1_2, '''albert-xlarge-v2''': 5_1_2, '''albert-xxlarge-v2''': 5_1_2, } lowercase__ : Tuple = '''▁''' class lowercase_ ( UpperCamelCase_ ): """simple docstring""" UpperCAmelCase_ : Dict = VOCAB_FILES_NAMES UpperCAmelCase_ : Tuple = PRETRAINED_VOCAB_FILES_MAP UpperCAmelCase_ : Optional[int] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE="[CLS]" , __SCREAMING_SNAKE_CASE="[SEP]" , __SCREAMING_SNAKE_CASE="<unk>" , __SCREAMING_SNAKE_CASE="[SEP]" , __SCREAMING_SNAKE_CASE="<pad>" , __SCREAMING_SNAKE_CASE="[CLS]" , __SCREAMING_SNAKE_CASE="[MASK]" , __SCREAMING_SNAKE_CASE = None , **__SCREAMING_SNAKE_CASE , ) ->None: # Mask token behave like a normal word, i.e. include the space before it and # is included in the raw text, there should be a match in a non-normalized sentence. lowerCAmelCase = ( AddedToken(__SCREAMING_SNAKE_CASE , lstrip=__SCREAMING_SNAKE_CASE , rstrip=__SCREAMING_SNAKE_CASE , normalized=__SCREAMING_SNAKE_CASE ) if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else mask_token ) lowerCAmelCase = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( do_lower_case=__SCREAMING_SNAKE_CASE , remove_space=__SCREAMING_SNAKE_CASE , keep_accents=__SCREAMING_SNAKE_CASE , bos_token=__SCREAMING_SNAKE_CASE , eos_token=__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 , sp_model_kwargs=self.sp_model_kwargs , **__SCREAMING_SNAKE_CASE , ) lowerCAmelCase = do_lower_case lowerCAmelCase = remove_space lowerCAmelCase = keep_accents lowerCAmelCase = vocab_file lowerCAmelCase = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(__SCREAMING_SNAKE_CASE ) @property def SCREAMING_SNAKE_CASE_ ( self ) ->Any: return len(self.sp_model ) def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[Any]: 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 , __SCREAMING_SNAKE_CASE ) ->Tuple: lowerCAmelCase = d # for backward compatibility if not hasattr(self , '''sp_model_kwargs''' ): lowerCAmelCase = {} lowerCAmelCase = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE ) ->Any: if self.remove_space: lowerCAmelCase = ''' '''.join(inputs.strip().split() ) else: lowerCAmelCase = inputs lowerCAmelCase = outputs.replace('''``''' , '''"''' ).replace('''\'\'''' , '''"''' ) if not self.keep_accents: lowerCAmelCase = unicodedata.normalize('''NFKD''' , __SCREAMING_SNAKE_CASE ) lowerCAmelCase = ''''''.join([c for c in outputs if not unicodedata.combining(__SCREAMING_SNAKE_CASE )] ) if self.do_lower_case: lowerCAmelCase = outputs.lower() return outputs def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE ) ->List[str]: lowerCAmelCase = self.preprocess_text(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = self.sp_model.encode(__SCREAMING_SNAKE_CASE , out_type=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = [] for piece in pieces: if len(__SCREAMING_SNAKE_CASE ) > 1 and piece[-1] == str(''',''' ) and piece[-2].isdigit(): lowerCAmelCase = self.sp_model.EncodeAsPieces(piece[:-1].replace(__SCREAMING_SNAKE_CASE , '''''' ) ) if piece[0] != SPIECE_UNDERLINE and cur_pieces[0][0] == SPIECE_UNDERLINE: if len(cur_pieces[0] ) == 1: lowerCAmelCase = cur_pieces[1:] else: lowerCAmelCase = cur_pieces[0][1:] cur_pieces.append(piece[-1] ) new_pieces.extend(__SCREAMING_SNAKE_CASE ) else: new_pieces.append(__SCREAMING_SNAKE_CASE ) return new_pieces def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE ) ->int: return self.sp_model.PieceToId(__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE ) ->int: return self.sp_model.IdToPiece(__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE ) ->Optional[int]: lowerCAmelCase = [] lowerCAmelCase = '''''' lowerCAmelCase = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: if not prev_is_special: out_string += " " out_string += self.sp_model.decode(__SCREAMING_SNAKE_CASE ) + token lowerCAmelCase = True lowerCAmelCase = [] else: current_sub_tokens.append(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = False out_string += self.sp_model.decode(__SCREAMING_SNAKE_CASE ) return out_string.strip() def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ) ->List[int]: lowerCAmelCase = [self.sep_token_id] lowerCAmelCase = [self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = False ) ->List[int]: 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 not None: return [1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1] return [1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1] def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ) ->List[int]: 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 ) * [0] + len(token_ids_a + sep ) * [1] def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ) ->Tuple[str]: 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 ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , __SCREAMING_SNAKE_CASE ) elif not os.path.isfile(self.vocab_file ): with open(__SCREAMING_SNAKE_CASE , '''wb''' ) as fi: lowerCAmelCase = self.sp_model.serialized_model_proto() fi.write(__SCREAMING_SNAKE_CASE ) return (out_vocab_file,)
338
1
def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ ) -> list[int]: lowerCAmelCase = int(snake_case__ ) # Initialize Result lowerCAmelCase = [] # Traverse through all denomination for denomination in reversed(snake_case__ ): # Find denominations while int(snake_case__ ) >= int(snake_case__ ): total_value -= int(snake_case__ ) answer.append(snake_case__ ) # Append the "answers" array return answer # Driver Code if __name__ == "__main__": lowercase__ : Dict = [] lowercase__ : List[str] = '''0''' if ( input('''Do you want to enter your denominations ? (yY/n): ''').strip().lower() == "y" ): lowercase__ : str = int(input('''Enter the number of denominations you want to add: ''').strip()) for i in range(0, n): denominations.append(int(input(f'Denomination {i}: ').strip())) lowercase__ : str = input('''Enter the change you want to make in Indian Currency: ''').strip() else: # All denominations of Indian Currency if user does not enter lowercase__ : int = [1, 2, 5, 1_0, 2_0, 5_0, 1_0_0, 5_0_0, 2_0_0_0] lowercase__ : List[Any] = input('''Enter the change you want to make: ''').strip() if int(value) == 0 or int(value) < 0: print('''The total value cannot be zero or negative.''') else: print(f'Following is minimal change for {value}: ') lowercase__ : Optional[int] = find_minimum_change(denominations, value) # Print result for i in range(len(answer)): print(answer[i], end=''' ''')
338
import tempfile import torch from diffusers import ( DEISMultistepScheduler, DPMSolverMultistepScheduler, DPMSolverSinglestepScheduler, UniPCMultistepScheduler, ) from .test_schedulers import SchedulerCommonTest class lowercase_ ( UpperCamelCase_ ): """simple docstring""" UpperCAmelCase_ : List[Any] = (DEISMultistepScheduler,) UpperCAmelCase_ : int = (("""num_inference_steps""", 25),) def SCREAMING_SNAKE_CASE_ ( self , **__SCREAMING_SNAKE_CASE ) ->str: lowerCAmelCase = { '''num_train_timesteps''': 1000, '''beta_start''': 0.0_0_0_1, '''beta_end''': 0.0_2, '''beta_schedule''': '''linear''', '''solver_order''': 2, } config.update(**__SCREAMING_SNAKE_CASE ) return config def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE=0 , **__SCREAMING_SNAKE_CASE ) ->Tuple: lowerCAmelCase = dict(self.forward_default_kwargs ) lowerCAmelCase = kwargs.pop('''num_inference_steps''' , __SCREAMING_SNAKE_CASE ) lowerCAmelCase = self.dummy_sample lowerCAmelCase = 0.1 * sample lowerCAmelCase = [residual + 0.2, residual + 0.1_5, residual + 0.1_0] for scheduler_class in self.scheduler_classes: lowerCAmelCase = self.get_scheduler_config(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) # copy over dummy past residuals lowerCAmelCase = dummy_past_residuals[: scheduler.config.solver_order] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = scheduler_class.from_pretrained(__SCREAMING_SNAKE_CASE ) new_scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) # copy over dummy past residuals lowerCAmelCase = dummy_past_residuals[: new_scheduler.config.solver_order] lowerCAmelCase , lowerCAmelCase = sample, sample for t in range(__SCREAMING_SNAKE_CASE , time_step + scheduler.config.solver_order + 1 ): lowerCAmelCase = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).prev_sample lowerCAmelCase = new_scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1e-5, "Scheduler outputs are not identical" def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[int]: pass def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE=0 , **__SCREAMING_SNAKE_CASE ) ->List[Any]: lowerCAmelCase = dict(self.forward_default_kwargs ) lowerCAmelCase = kwargs.pop('''num_inference_steps''' , __SCREAMING_SNAKE_CASE ) lowerCAmelCase = self.dummy_sample lowerCAmelCase = 0.1 * sample lowerCAmelCase = [residual + 0.2, residual + 0.1_5, residual + 0.1_0] for scheduler_class in self.scheduler_classes: lowerCAmelCase = self.get_scheduler_config() lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) # copy over dummy past residuals (must be after setting timesteps) lowerCAmelCase = dummy_past_residuals[: scheduler.config.solver_order] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = scheduler_class.from_pretrained(__SCREAMING_SNAKE_CASE ) # copy over dummy past residuals new_scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) # copy over dummy past residual (must be after setting timesteps) lowerCAmelCase = dummy_past_residuals[: new_scheduler.config.solver_order] lowerCAmelCase = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).prev_sample lowerCAmelCase = new_scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1e-5, "Scheduler outputs are not identical" def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE ) ->List[Any]: if scheduler is None: lowerCAmelCase = self.scheduler_classes[0] lowerCAmelCase = self.get_scheduler_config(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = self.scheduler_classes[0] lowerCAmelCase = self.get_scheduler_config(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = 10 lowerCAmelCase = self.dummy_model() lowerCAmelCase = self.dummy_sample_deter scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) for i, t in enumerate(scheduler.timesteps ): lowerCAmelCase = model(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) lowerCAmelCase = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ).prev_sample return sample def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[int]: lowerCAmelCase = dict(self.forward_default_kwargs ) lowerCAmelCase = kwargs.pop('''num_inference_steps''' , __SCREAMING_SNAKE_CASE ) for scheduler_class in self.scheduler_classes: lowerCAmelCase = self.get_scheduler_config() lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = self.dummy_sample lowerCAmelCase = 0.1 * sample if num_inference_steps is not None and hasattr(__SCREAMING_SNAKE_CASE , '''set_timesteps''' ): scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) elif num_inference_steps is not None and not hasattr(__SCREAMING_SNAKE_CASE , '''set_timesteps''' ): lowerCAmelCase = num_inference_steps # copy over dummy past residuals (must be done after set_timesteps) lowerCAmelCase = [residual + 0.2, residual + 0.1_5, residual + 0.1_0] lowerCAmelCase = dummy_past_residuals[: scheduler.config.solver_order] lowerCAmelCase = scheduler.timesteps[5] lowerCAmelCase = scheduler.timesteps[6] lowerCAmelCase = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).prev_sample lowerCAmelCase = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).prev_sample self.assertEqual(output_a.shape , sample.shape ) self.assertEqual(output_a.shape , output_a.shape ) def SCREAMING_SNAKE_CASE_ ( self ) ->int: # make sure that iterating over schedulers with same config names gives same results # for defaults lowerCAmelCase = DEISMultistepScheduler(**self.get_scheduler_config() ) lowerCAmelCase = self.full_loop(scheduler=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = torch.mean(torch.abs(__SCREAMING_SNAKE_CASE ) ) assert abs(result_mean.item() - 0.2_3_9_1_6 ) < 1e-3 lowerCAmelCase = DPMSolverSinglestepScheduler.from_config(scheduler.config ) lowerCAmelCase = DPMSolverMultistepScheduler.from_config(scheduler.config ) lowerCAmelCase = UniPCMultistepScheduler.from_config(scheduler.config ) lowerCAmelCase = DEISMultistepScheduler.from_config(scheduler.config ) lowerCAmelCase = self.full_loop(scheduler=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = torch.mean(torch.abs(__SCREAMING_SNAKE_CASE ) ) assert abs(result_mean.item() - 0.2_3_9_1_6 ) < 1e-3 def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[int]: for timesteps in [25, 50, 100, 999, 1000]: self.check_over_configs(num_train_timesteps=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->int: self.check_over_configs(thresholding=__SCREAMING_SNAKE_CASE ) for order in [1, 2, 3]: for solver_type in ["logrho"]: for threshold in [0.5, 1.0, 2.0]: for prediction_type in ["epsilon", "sample"]: self.check_over_configs( thresholding=__SCREAMING_SNAKE_CASE , prediction_type=__SCREAMING_SNAKE_CASE , sample_max_value=__SCREAMING_SNAKE_CASE , algorithm_type='''deis''' , solver_order=__SCREAMING_SNAKE_CASE , solver_type=__SCREAMING_SNAKE_CASE , ) def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[Any]: for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->Union[str, Any]: for algorithm_type in ["deis"]: for solver_type in ["logrho"]: for order in [1, 2, 3]: for prediction_type in ["epsilon", "sample"]: self.check_over_configs( solver_order=__SCREAMING_SNAKE_CASE , solver_type=__SCREAMING_SNAKE_CASE , prediction_type=__SCREAMING_SNAKE_CASE , algorithm_type=__SCREAMING_SNAKE_CASE , ) lowerCAmelCase = self.full_loop( solver_order=__SCREAMING_SNAKE_CASE , solver_type=__SCREAMING_SNAKE_CASE , prediction_type=__SCREAMING_SNAKE_CASE , algorithm_type=__SCREAMING_SNAKE_CASE , ) assert not torch.isnan(__SCREAMING_SNAKE_CASE ).any(), "Samples have nan numbers" def SCREAMING_SNAKE_CASE_ ( self ) ->List[Any]: self.check_over_configs(lower_order_final=__SCREAMING_SNAKE_CASE ) self.check_over_configs(lower_order_final=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->List[Any]: for num_inference_steps in [1, 2, 3, 5, 10, 50, 100, 999, 1000]: self.check_over_forward(num_inference_steps=__SCREAMING_SNAKE_CASE , time_step=0 ) def SCREAMING_SNAKE_CASE_ ( self ) ->Dict: lowerCAmelCase = self.full_loop() lowerCAmelCase = torch.mean(torch.abs(__SCREAMING_SNAKE_CASE ) ) assert abs(result_mean.item() - 0.2_3_9_1_6 ) < 1e-3 def SCREAMING_SNAKE_CASE_ ( self ) ->List[str]: lowerCAmelCase = self.full_loop(prediction_type='''v_prediction''' ) lowerCAmelCase = torch.mean(torch.abs(__SCREAMING_SNAKE_CASE ) ) assert abs(result_mean.item() - 0.0_9_1 ) < 1e-3 def SCREAMING_SNAKE_CASE_ ( self ) ->Dict: lowerCAmelCase = self.scheduler_classes[0] lowerCAmelCase = self.get_scheduler_config(thresholding=__SCREAMING_SNAKE_CASE , dynamic_thresholding_ratio=0 ) lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = 10 lowerCAmelCase = self.dummy_model() lowerCAmelCase = self.dummy_sample_deter.half() scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) for i, t in enumerate(scheduler.timesteps ): lowerCAmelCase = model(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) lowerCAmelCase = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ).prev_sample assert sample.dtype == torch.floataa
338
1
import warnings from ...utils import logging from .image_processing_segformer import SegformerImageProcessor lowercase__ : str = logging.get_logger(__name__) class lowercase_ ( UpperCamelCase_ ): """simple docstring""" def __init__( self , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) ->None: warnings.warn( '''The class SegformerFeatureExtractor is deprecated and will be removed in version 5 of Transformers.''' ''' Please use SegformerImageProcessor instead.''' , __SCREAMING_SNAKE_CASE , ) super().__init__(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE )
338
import unittest import numpy as np import torch from diffusers import KarrasVePipeline, KarrasVeScheduler, UNetaDModel from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device enable_full_determinism() class lowercase_ ( unittest.TestCase ): """simple docstring""" @property def SCREAMING_SNAKE_CASE_ ( self ) ->List[str]: torch.manual_seed(0 ) lowerCAmelCase = UNetaDModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=3 , out_channels=3 , down_block_types=('''DownBlock2D''', '''AttnDownBlock2D''') , up_block_types=('''AttnUpBlock2D''', '''UpBlock2D''') , ) return model def SCREAMING_SNAKE_CASE_ ( self ) ->int: lowerCAmelCase = self.dummy_uncond_unet lowerCAmelCase = KarrasVeScheduler() lowerCAmelCase = KarrasVePipeline(unet=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = torch.manual_seed(0 ) lowerCAmelCase = pipe(num_inference_steps=2 , generator=__SCREAMING_SNAKE_CASE , output_type='''numpy''' ).images lowerCAmelCase = torch.manual_seed(0 ) lowerCAmelCase = pipe(num_inference_steps=2 , generator=__SCREAMING_SNAKE_CASE , output_type='''numpy''' , return_dict=__SCREAMING_SNAKE_CASE )[0] lowerCAmelCase = image[0, -3:, -3:, -1] lowerCAmelCase = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) lowerCAmelCase = np.array([0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 @slow @require_torch class lowercase_ ( unittest.TestCase ): """simple docstring""" def SCREAMING_SNAKE_CASE_ ( self ) ->List[Any]: lowerCAmelCase = '''google/ncsnpp-celebahq-256''' lowerCAmelCase = UNetaDModel.from_pretrained(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = KarrasVeScheduler() lowerCAmelCase = KarrasVePipeline(unet=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = torch.manual_seed(0 ) lowerCAmelCase = pipe(num_inference_steps=20 , generator=__SCREAMING_SNAKE_CASE , output_type='''numpy''' ).images lowerCAmelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 256, 256, 3) lowerCAmelCase = np.array([0.5_7_8, 0.5_8_1_1, 0.5_9_2_4, 0.5_8_0_9, 0.5_8_7, 0.5_8_8_6, 0.5_8_6_1, 0.5_8_0_2, 0.5_8_6] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
338
1
import multiprocessing from typing import TYPE_CHECKING, Optional, Union from .. import Dataset, Features, config from ..formatting import query_table from ..packaged_modules.sql.sql import Sql from ..utils import logging from .abc import AbstractDatasetInputStream if TYPE_CHECKING: import sqlitea import sqlalchemy class lowercase_ ( UpperCamelCase_ ): """simple docstring""" def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = False , **__SCREAMING_SNAKE_CASE , ) ->Union[str, Any]: super().__init__(features=__SCREAMING_SNAKE_CASE , cache_dir=__SCREAMING_SNAKE_CASE , keep_in_memory=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) lowerCAmelCase = Sql( cache_dir=__SCREAMING_SNAKE_CASE , features=__SCREAMING_SNAKE_CASE , sql=__SCREAMING_SNAKE_CASE , con=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) def SCREAMING_SNAKE_CASE_ ( self ) ->List[str]: lowerCAmelCase = None lowerCAmelCase = None lowerCAmelCase = None lowerCAmelCase = None self.builder.download_and_prepare( download_config=__SCREAMING_SNAKE_CASE , download_mode=__SCREAMING_SNAKE_CASE , verification_mode=__SCREAMING_SNAKE_CASE , base_path=__SCREAMING_SNAKE_CASE , ) # Build dataset for splits lowerCAmelCase = self.builder.as_dataset( split='''train''' , verification_mode=__SCREAMING_SNAKE_CASE , in_memory=self.keep_in_memory ) return dataset class lowercase_ : """simple docstring""" def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = None , **__SCREAMING_SNAKE_CASE , ) ->Optional[Any]: if num_proc is not None and num_proc <= 0: raise ValueError(F"num_proc {num_proc} must be an integer > 0." ) lowerCAmelCase = dataset lowerCAmelCase = name lowerCAmelCase = con lowerCAmelCase = batch_size if batch_size else config.DEFAULT_MAX_BATCH_SIZE lowerCAmelCase = num_proc lowerCAmelCase = to_sql_kwargs def SCREAMING_SNAKE_CASE_ ( self ) ->int: lowerCAmelCase = self.to_sql_kwargs.pop('''sql''' , __SCREAMING_SNAKE_CASE ) lowerCAmelCase = self.to_sql_kwargs.pop('''con''' , __SCREAMING_SNAKE_CASE ) lowerCAmelCase = self.to_sql_kwargs.pop('''index''' , __SCREAMING_SNAKE_CASE ) lowerCAmelCase = self._write(index=__SCREAMING_SNAKE_CASE , **self.to_sql_kwargs ) return written def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE ) ->Tuple: lowerCAmelCase , lowerCAmelCase , lowerCAmelCase = args lowerCAmelCase = {**to_sql_kwargs, '''if_exists''': '''append'''} if offset > 0 else to_sql_kwargs lowerCAmelCase = query_table( table=self.dataset.data , key=slice(__SCREAMING_SNAKE_CASE , offset + self.batch_size ) , indices=self.dataset._indices , ) lowerCAmelCase = batch.to_pandas() lowerCAmelCase = df.to_sql(self.name , self.con , index=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) return num_rows or len(__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) ->int: lowerCAmelCase = 0 if self.num_proc is None or self.num_proc == 1: for offset in logging.tqdm( range(0 , len(self.dataset ) , self.batch_size ) , unit='''ba''' , disable=not logging.is_progress_bar_enabled() , desc='''Creating SQL from Arrow format''' , ): written += self._batch_sql((offset, index, to_sql_kwargs) ) else: lowerCAmelCase , lowerCAmelCase = len(self.dataset ), self.batch_size with multiprocessing.Pool(self.num_proc ) as pool: for num_rows in logging.tqdm( pool.imap( self._batch_sql , [(offset, index, to_sql_kwargs) for offset in range(0 , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE )] , ) , total=(num_rows // batch_size) + 1 if num_rows % batch_size else num_rows // batch_size , unit='''ba''' , disable=not logging.is_progress_bar_enabled() , desc='''Creating SQL from Arrow format''' , ): written += num_rows return written
338
from typing import Dict import numpy as np from ..utils import add_end_docstrings, is_tf_available, is_torch_available, logging from .base import PIPELINE_INIT_ARGS, GenericTensor, Pipeline, PipelineException if is_tf_available(): import tensorflow as tf from ..tf_utils import stable_softmax if is_torch_available(): import torch lowercase__ : Dict = logging.get_logger(__name__) @add_end_docstrings( UpperCamelCase_ , r""" top_k (`int`, defaults to 5): The number of predictions to return. targets (`str` or `List[str]`, *optional*): When passed, the model will limit the scores to the passed targets instead of looking up in the whole vocab. If the provided targets are not in the model vocab, they will be tokenized and the first resulting token will be used (with a warning, and that might be slower). """ , ) class lowercase_ ( UpperCamelCase_ ): """simple docstring""" def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE ) ->np.ndarray: if self.framework == "tf": lowerCAmelCase = tf.where(input_ids == self.tokenizer.mask_token_id ).numpy() elif self.framework == "pt": lowerCAmelCase = torch.nonzero(input_ids == self.tokenizer.mask_token_id , as_tuple=__SCREAMING_SNAKE_CASE ) else: raise ValueError('''Unsupported framework''' ) return masked_index def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE ) ->np.ndarray: lowerCAmelCase = self.get_masked_index(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = np.prod(masked_index.shape ) if numel < 1: raise PipelineException( '''fill-mask''' , self.model.base_model_prefix , F"No mask_token ({self.tokenizer.mask_token}) found on the input" , ) def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE ) ->str: if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): for model_input in model_inputs: self._ensure_exactly_one_mask_token(model_input['''input_ids'''][0] ) else: for input_ids in model_inputs["input_ids"]: self._ensure_exactly_one_mask_token(__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE ) ->Dict[str, GenericTensor]: if return_tensors is None: lowerCAmelCase = self.framework lowerCAmelCase = self.tokenizer(__SCREAMING_SNAKE_CASE , return_tensors=__SCREAMING_SNAKE_CASE ) self.ensure_exactly_one_mask_token(__SCREAMING_SNAKE_CASE ) return model_inputs def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE ) ->Tuple: lowerCAmelCase = self.model(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = model_inputs['''input_ids'''] return model_outputs def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=5 , __SCREAMING_SNAKE_CASE=None ) ->str: # Cap top_k if there are targets if target_ids is not None and target_ids.shape[0] < top_k: lowerCAmelCase = target_ids.shape[0] lowerCAmelCase = model_outputs['''input_ids'''][0] lowerCAmelCase = model_outputs['''logits'''] if self.framework == "tf": lowerCAmelCase = tf.where(input_ids == self.tokenizer.mask_token_id ).numpy()[:, 0] lowerCAmelCase = outputs.numpy() lowerCAmelCase = outputs[0, masked_index, :] lowerCAmelCase = stable_softmax(__SCREAMING_SNAKE_CASE , axis=-1 ) if target_ids is not None: lowerCAmelCase = tf.gather_nd(tf.squeeze(__SCREAMING_SNAKE_CASE , 0 ) , target_ids.reshape(-1 , 1 ) ) lowerCAmelCase = tf.expand_dims(__SCREAMING_SNAKE_CASE , 0 ) lowerCAmelCase = tf.math.top_k(__SCREAMING_SNAKE_CASE , k=__SCREAMING_SNAKE_CASE ) lowerCAmelCase , lowerCAmelCase = topk.values.numpy(), topk.indices.numpy() else: lowerCAmelCase = torch.nonzero(input_ids == self.tokenizer.mask_token_id , as_tuple=__SCREAMING_SNAKE_CASE ).squeeze(-1 ) # Fill mask pipeline supports only one ${mask_token} per sample lowerCAmelCase = outputs[0, masked_index, :] lowerCAmelCase = logits.softmax(dim=-1 ) if target_ids is not None: lowerCAmelCase = probs[..., target_ids] lowerCAmelCase , lowerCAmelCase = probs.topk(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = [] lowerCAmelCase = values.shape[0] == 1 for i, (_values, _predictions) in enumerate(zip(values.tolist() , predictions.tolist() ) ): lowerCAmelCase = [] for v, p in zip(_values , _predictions ): # Copy is important since we're going to modify this array in place lowerCAmelCase = input_ids.numpy().copy() if target_ids is not None: lowerCAmelCase = target_ids[p].tolist() lowerCAmelCase = p # Filter padding out: lowerCAmelCase = tokens[np.where(tokens != self.tokenizer.pad_token_id )] # Originally we skip special tokens to give readable output. # For multi masks though, the other [MASK] would be removed otherwise # making the output look odd, so we add them back lowerCAmelCase = self.tokenizer.decode(__SCREAMING_SNAKE_CASE , skip_special_tokens=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = {'''score''': v, '''token''': p, '''token_str''': self.tokenizer.decode([p] ), '''sequence''': sequence} row.append(__SCREAMING_SNAKE_CASE ) result.append(__SCREAMING_SNAKE_CASE ) if single_mask: return result[0] return result def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None ) ->Optional[Any]: if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): lowerCAmelCase = [targets] try: lowerCAmelCase = self.tokenizer.get_vocab() except Exception: lowerCAmelCase = {} lowerCAmelCase = [] for target in targets: lowerCAmelCase = vocab.get(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) if id_ is None: lowerCAmelCase = self.tokenizer( __SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , return_attention_mask=__SCREAMING_SNAKE_CASE , return_token_type_ids=__SCREAMING_SNAKE_CASE , max_length=1 , truncation=__SCREAMING_SNAKE_CASE , )['''input_ids'''] if len(__SCREAMING_SNAKE_CASE ) == 0: logger.warning( F"The specified target token `{target}` does not exist in the model vocabulary. " '''We cannot replace it with anything meaningful, ignoring it''' ) continue lowerCAmelCase = input_ids[0] # XXX: If users encounter this pass # it becomes pretty slow, so let's make sure # The warning enables them to fix the input to # get faster performance. logger.warning( F"The specified target token `{target}` does not exist in the model vocabulary. " F"Replacing with `{self.tokenizer.convert_ids_to_tokens(id_ )}`." ) target_ids.append(id_ ) lowerCAmelCase = list(set(__SCREAMING_SNAKE_CASE ) ) if len(__SCREAMING_SNAKE_CASE ) == 0: raise ValueError('''At least one target must be provided when passed.''' ) lowerCAmelCase = np.array(__SCREAMING_SNAKE_CASE ) return target_ids def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None ) ->Dict: lowerCAmelCase = {} if targets is not None: lowerCAmelCase = self.get_target_ids(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) lowerCAmelCase = target_ids if top_k is not None: lowerCAmelCase = top_k if self.tokenizer.mask_token_id is None: raise PipelineException( '''fill-mask''' , self.model.base_model_prefix , '''The tokenizer does not define a `mask_token`.''' ) return {}, {}, postprocess_params def __call__( self , __SCREAMING_SNAKE_CASE , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) ->List[Any]: lowerCAmelCase = super().__call__(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) and len(__SCREAMING_SNAKE_CASE ) == 1: return outputs[0] return outputs
338
1
class lowercase_ : """simple docstring""" def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ->Any: lowerCAmelCase = name lowerCAmelCase = value lowerCAmelCase = weight def __repr__( self ) ->str: return F"{self.__class__.__name__}({self.name}, {self.value}, {self.weight})" def SCREAMING_SNAKE_CASE_ ( self ) ->List[Any]: return self.value def SCREAMING_SNAKE_CASE_ ( self ) ->int: return self.name def SCREAMING_SNAKE_CASE_ ( self ) ->Union[str, Any]: return self.weight def SCREAMING_SNAKE_CASE_ ( self ) ->Tuple: return self.value / self.weight def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> int: lowerCAmelCase = [] for i in range(len(snake_case__ ) ): menu.append(Things(name[i] , value[i] , weight[i] ) ) return menu def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> Optional[int]: lowerCAmelCase = sorted(snake_case__ , key=snake_case__ , reverse=snake_case__ ) lowerCAmelCase = [] lowerCAmelCase , lowerCAmelCase = 0.0, 0.0 for i in range(len(snake_case__ ) ): if (total_cost + items_copy[i].get_weight()) <= max_cost: result.append(items_copy[i] ) total_cost += items_copy[i].get_weight() total_value += items_copy[i].get_value() return (result, total_value) def SCREAMING_SNAKE_CASE_ ( ) -> Optional[int]: pass if __name__ == "__main__": import doctest doctest.testmod()
338
from typing import TYPE_CHECKING from ...utils import _LazyModule lowercase__ : int = {'''tokenization_wav2vec2_phoneme''': ['''Wav2Vec2PhonemeCTCTokenizer''']} if TYPE_CHECKING: from .tokenization_wavaveca_phoneme import WavaVecaPhonemeCTCTokenizer else: import sys lowercase__ : Optional[int] = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
338
1
from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices lowercase__ : Any = logging.get_logger(__name__) lowercase__ : Any = { '''microsoft/focalnet-tiny''': '''https://huggingface.co/microsoft/focalnet-tiny/resolve/main/config.json''', } class lowercase_ ( UpperCamelCase_ , UpperCamelCase_ ): """simple docstring""" UpperCAmelCase_ : List[Any] = """focalnet""" def __init__( self , __SCREAMING_SNAKE_CASE=224 , __SCREAMING_SNAKE_CASE=4 , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=96 , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=[192, 384, 768, 768] , __SCREAMING_SNAKE_CASE=[2, 2, 6, 2] , __SCREAMING_SNAKE_CASE=[2, 2, 2, 2] , __SCREAMING_SNAKE_CASE=[3, 3, 3, 3] , __SCREAMING_SNAKE_CASE="gelu" , __SCREAMING_SNAKE_CASE=4.0 , __SCREAMING_SNAKE_CASE=0.0 , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=1e-4 , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=0.0_2 , __SCREAMING_SNAKE_CASE=1e-5 , __SCREAMING_SNAKE_CASE=32 , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE , ) ->int: super().__init__(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = image_size lowerCAmelCase = patch_size lowerCAmelCase = num_channels lowerCAmelCase = embed_dim lowerCAmelCase = use_conv_embed lowerCAmelCase = hidden_sizes lowerCAmelCase = depths lowerCAmelCase = focal_levels lowerCAmelCase = focal_windows lowerCAmelCase = hidden_act lowerCAmelCase = mlp_ratio lowerCAmelCase = hidden_dropout_prob lowerCAmelCase = drop_path_rate lowerCAmelCase = use_layerscale lowerCAmelCase = layerscale_value lowerCAmelCase = use_post_layernorm lowerCAmelCase = use_post_layernorm_in_modulation lowerCAmelCase = normalize_modulator lowerCAmelCase = initializer_range lowerCAmelCase = layer_norm_eps lowerCAmelCase = encoder_stride lowerCAmelCase = ['''stem'''] + [F"stage{idx}" for idx in range(1 , len(self.depths ) + 1 )] lowerCAmelCase , lowerCAmelCase = get_aligned_output_features_output_indices( out_features=__SCREAMING_SNAKE_CASE , out_indices=__SCREAMING_SNAKE_CASE , stage_names=self.stage_names )
338
lowercase__ : Optional[int] = '''ABCDEFGHIJKLMNOPQRSTUVWXYZ''' def SCREAMING_SNAKE_CASE_ ( ) -> None: lowerCAmelCase = input('''Enter message: ''' ) lowerCAmelCase = input('''Enter key [alphanumeric]: ''' ) lowerCAmelCase = input('''Encrypt/Decrypt [e/d]: ''' ) if mode.lower().startswith('''e''' ): lowerCAmelCase = '''encrypt''' lowerCAmelCase = encrypt_message(snake_case__ , snake_case__ ) elif mode.lower().startswith('''d''' ): lowerCAmelCase = '''decrypt''' lowerCAmelCase = decrypt_message(snake_case__ , snake_case__ ) print(f"\n{mode.title()}ed message:" ) print(snake_case__ ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ ) -> str: return translate_message(snake_case__ , snake_case__ , '''encrypt''' ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ ) -> str: return translate_message(snake_case__ , snake_case__ , '''decrypt''' ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> str: lowerCAmelCase = [] lowerCAmelCase = 0 lowerCAmelCase = key.upper() for symbol in message: lowerCAmelCase = LETTERS.find(symbol.upper() ) if num != -1: if mode == "encrypt": num += LETTERS.find(key[key_index] ) elif mode == "decrypt": num -= LETTERS.find(key[key_index] ) num %= len(snake_case__ ) if symbol.isupper(): translated.append(LETTERS[num] ) elif symbol.islower(): translated.append(LETTERS[num].lower() ) key_index += 1 if key_index == len(snake_case__ ): lowerCAmelCase = 0 else: translated.append(snake_case__ ) return "".join(snake_case__ ) if __name__ == "__main__": main()
338
1
import warnings from typing import List, Optional, Tuple, Union import numpy as np import PIL import torch from ...models import UNetaDModel from ...schedulers import RePaintScheduler from ...utils import PIL_INTERPOLATION, logging, randn_tensor from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput lowercase__ : Dict = logging.get_logger(__name__) # pylint: disable=invalid-name def SCREAMING_SNAKE_CASE_ ( snake_case__ ) -> List[str]: warnings.warn( '''The preprocess method is deprecated and will be removed in a future version. Please''' ''' use VaeImageProcessor.preprocess instead''' , snake_case__ , ) if isinstance(snake_case__ , torch.Tensor ): return image elif isinstance(snake_case__ , PIL.Image.Image ): lowerCAmelCase = [image] if isinstance(image[0] , PIL.Image.Image ): lowerCAmelCase , lowerCAmelCase = image[0].size lowerCAmelCase , lowerCAmelCase = (x - x % 8 for x in (w, h)) # resize to integer multiple of 8 lowerCAmelCase = [np.array(i.resize((w, h) , resample=PIL_INTERPOLATION['''lanczos'''] ) )[None, :] for i in image] lowerCAmelCase = np.concatenate(snake_case__ , axis=0 ) lowerCAmelCase = np.array(snake_case__ ).astype(np.floataa ) / 2_55.0 lowerCAmelCase = image.transpose(0 , 3 , 1 , 2 ) lowerCAmelCase = 2.0 * image - 1.0 lowerCAmelCase = torch.from_numpy(snake_case__ ) elif isinstance(image[0] , torch.Tensor ): lowerCAmelCase = torch.cat(snake_case__ , dim=0 ) return image def SCREAMING_SNAKE_CASE_ ( snake_case__ ) -> Any: if isinstance(snake_case__ , torch.Tensor ): return mask elif isinstance(snake_case__ , PIL.Image.Image ): lowerCAmelCase = [mask] if isinstance(mask[0] , PIL.Image.Image ): lowerCAmelCase , lowerCAmelCase = mask[0].size lowerCAmelCase , lowerCAmelCase = (x - x % 3_2 for x in (w, h)) # resize to integer multiple of 32 lowerCAmelCase = [np.array(m.convert('''L''' ).resize((w, h) , resample=PIL_INTERPOLATION['''nearest'''] ) )[None, :] for m in mask] lowerCAmelCase = np.concatenate(snake_case__ , axis=0 ) lowerCAmelCase = mask.astype(np.floataa ) / 2_55.0 lowerCAmelCase = 0 lowerCAmelCase = 1 lowerCAmelCase = torch.from_numpy(snake_case__ ) elif isinstance(mask[0] , torch.Tensor ): lowerCAmelCase = torch.cat(snake_case__ , dim=0 ) return mask class lowercase_ ( UpperCamelCase_ ): """simple docstring""" UpperCAmelCase_ : UNetaDModel UpperCAmelCase_ : RePaintScheduler def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ->int: super().__init__() self.register_modules(unet=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) @torch.no_grad() def __call__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = 250 , __SCREAMING_SNAKE_CASE = 0.0 , __SCREAMING_SNAKE_CASE = 10 , __SCREAMING_SNAKE_CASE = 10 , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = "pil" , __SCREAMING_SNAKE_CASE = True , ) ->Union[ImagePipelineOutput, Tuple]: lowerCAmelCase = image lowerCAmelCase = _preprocess_image(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = original_image.to(device=self.device , dtype=self.unet.dtype ) lowerCAmelCase = _preprocess_mask(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = mask_image.to(device=self.device , dtype=self.unet.dtype ) lowerCAmelCase = original_image.shape[0] # sample gaussian noise to begin the loop if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) and len(__SCREAMING_SNAKE_CASE ) != batch_size: raise ValueError( F"You have passed a list of generators of length {len(__SCREAMING_SNAKE_CASE )}, but requested an effective batch" F" size of {batch_size}. Make sure the batch size matches the length of the generators." ) lowerCAmelCase = original_image.shape lowerCAmelCase = randn_tensor(__SCREAMING_SNAKE_CASE , generator=__SCREAMING_SNAKE_CASE , device=self.device , dtype=self.unet.dtype ) # set step values self.scheduler.set_timesteps(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , self.device ) lowerCAmelCase = eta lowerCAmelCase = self.scheduler.timesteps[0] + 1 lowerCAmelCase = generator[0] if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else generator for i, t in enumerate(self.progress_bar(self.scheduler.timesteps ) ): if t < t_last: # predict the noise residual lowerCAmelCase = self.unet(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ).sample # compute previous image: x_t -> x_t-1 lowerCAmelCase = self.scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ).prev_sample else: # compute the reverse: x_t-1 -> x_t lowerCAmelCase = self.scheduler.undo_step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) lowerCAmelCase = t lowerCAmelCase = (image / 2 + 0.5).clamp(0 , 1 ) lowerCAmelCase = image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": lowerCAmelCase = self.numpy_to_pil(__SCREAMING_SNAKE_CASE ) if not return_dict: return (image,) return ImagePipelineOutput(images=__SCREAMING_SNAKE_CASE )
338
from collections import defaultdict from math import ceil, sqrt def SCREAMING_SNAKE_CASE_ ( snake_case__ = 1_0_0_0_0_0_0 , snake_case__ = 1_0 ) -> int: lowerCAmelCase = defaultdict(snake_case__ ) for outer_width in range(3 , (t_limit // 4) + 2 ): if outer_width * outer_width > t_limit: lowerCAmelCase = max( ceil(sqrt(outer_width * outer_width - t_limit ) ) , 1 ) else: lowerCAmelCase = 1 hole_width_lower_bound += (outer_width - hole_width_lower_bound) % 2 for hole_width in range(snake_case__ , outer_width - 1 , 2 ): count[outer_width * outer_width - hole_width * hole_width] += 1 return sum(1 for n in count.values() if 1 <= n <= 1_0 ) if __name__ == "__main__": print(f'{solution() = }')
338
1
import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_xlnet import XLNetTokenizer else: lowercase__ : Any = None lowercase__ : List[Any] = logging.get_logger(__name__) lowercase__ : Dict = {'''vocab_file''': '''spiece.model''', '''tokenizer_file''': '''tokenizer.json'''} lowercase__ : List[Any] = { '''vocab_file''': { '''xlnet-base-cased''': '''https://huggingface.co/xlnet-base-cased/resolve/main/spiece.model''', '''xlnet-large-cased''': '''https://huggingface.co/xlnet-large-cased/resolve/main/spiece.model''', }, '''tokenizer_file''': { '''xlnet-base-cased''': '''https://huggingface.co/xlnet-base-cased/resolve/main/tokenizer.json''', '''xlnet-large-cased''': '''https://huggingface.co/xlnet-large-cased/resolve/main/tokenizer.json''', }, } lowercase__ : int = { '''xlnet-base-cased''': None, '''xlnet-large-cased''': None, } lowercase__ : List[Any] = '''▁''' # Segments (not really needed) lowercase__ : Any = 0 lowercase__ : Any = 1 lowercase__ : Optional[Any] = 2 lowercase__ : Union[str, Any] = 3 lowercase__ : Any = 4 class lowercase_ ( UpperCamelCase_ ): """simple docstring""" UpperCAmelCase_ : Any = VOCAB_FILES_NAMES UpperCAmelCase_ : Optional[int] = PRETRAINED_VOCAB_FILES_MAP UpperCAmelCase_ : str = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCAmelCase_ : str = """left""" UpperCAmelCase_ : Optional[Any] = XLNetTokenizer def __init__( self , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE="<s>" , __SCREAMING_SNAKE_CASE="</s>" , __SCREAMING_SNAKE_CASE="<unk>" , __SCREAMING_SNAKE_CASE="<sep>" , __SCREAMING_SNAKE_CASE="<pad>" , __SCREAMING_SNAKE_CASE="<cls>" , __SCREAMING_SNAKE_CASE="<mask>" , __SCREAMING_SNAKE_CASE=["<eop>", "<eod>"] , **__SCREAMING_SNAKE_CASE , ) ->Tuple: # 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__( vocab_file=__SCREAMING_SNAKE_CASE , tokenizer_file=__SCREAMING_SNAKE_CASE , do_lower_case=__SCREAMING_SNAKE_CASE , remove_space=__SCREAMING_SNAKE_CASE , keep_accents=__SCREAMING_SNAKE_CASE , bos_token=__SCREAMING_SNAKE_CASE , eos_token=__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 , additional_special_tokens=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) lowerCAmelCase = 3 lowerCAmelCase = do_lower_case lowerCAmelCase = remove_space lowerCAmelCase = keep_accents lowerCAmelCase = vocab_file lowerCAmelCase = False if not self.vocab_file else True def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ) ->List[int]: lowerCAmelCase = [self.sep_token_id] lowerCAmelCase = [self.cls_token_id] if token_ids_a is None: return token_ids_a + sep + cls return token_ids_a + sep + token_ids_a + sep + cls def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ) ->List[int]: lowerCAmelCase = [self.sep_token_id] lowerCAmelCase = [2] if token_ids_a is None: return len(token_ids_a + sep ) * [0] + cls_segment_id return len(token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] + cls_segment_id def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ) ->Tuple[str]: if not self.can_save_slow_tokenizer: raise ValueError( '''Your fast tokenizer does not have the necessary information to save the vocabulary for a slow ''' '''tokenizer.''' ) if not os.path.isdir(__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,)
338
import pytest from datasets import Dataset, DatasetDict, Features, NamedSplit, Value from datasets.io.text import TextDatasetReader from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ ) -> Union[str, Any]: assert isinstance(snake_case__ , snake_case__ ) assert dataset.num_rows == 4 assert dataset.num_columns == 1 assert dataset.column_names == ["text"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize('''keep_in_memory''' , [False, True] ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> Union[str, Any]: lowerCAmelCase = tmp_path / '''cache''' lowerCAmelCase = {'''text''': '''string'''} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): lowerCAmelCase = TextDatasetReader(snake_case__ , cache_dir=snake_case__ , keep_in_memory=snake_case__ ).read() _check_text_dataset(snake_case__ , snake_case__ ) @pytest.mark.parametrize( '''features''' , [ None, {'''text''': '''string'''}, {'''text''': '''int32'''}, {'''text''': '''float32'''}, ] , ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> Optional[Any]: lowerCAmelCase = tmp_path / '''cache''' lowerCAmelCase = {'''text''': '''string'''} lowerCAmelCase = features.copy() if features else default_expected_features lowerCAmelCase = ( Features({feature: Value(snake_case__ ) for feature, dtype in features.items()} ) if features is not None else None ) lowerCAmelCase = TextDatasetReader(snake_case__ , features=snake_case__ , cache_dir=snake_case__ ).read() _check_text_dataset(snake_case__ , snake_case__ ) @pytest.mark.parametrize('''split''' , [None, NamedSplit('''train''' ), '''train''', '''test'''] ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> List[str]: lowerCAmelCase = tmp_path / '''cache''' lowerCAmelCase = {'''text''': '''string'''} lowerCAmelCase = TextDatasetReader(snake_case__ , cache_dir=snake_case__ , split=snake_case__ ).read() _check_text_dataset(snake_case__ , snake_case__ ) assert dataset.split == split if split else "train" @pytest.mark.parametrize('''path_type''' , [str, list] ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> Optional[int]: if issubclass(snake_case__ , snake_case__ ): lowerCAmelCase = text_path elif issubclass(snake_case__ , snake_case__ ): lowerCAmelCase = [text_path] lowerCAmelCase = tmp_path / '''cache''' lowerCAmelCase = {'''text''': '''string'''} lowerCAmelCase = TextDatasetReader(snake_case__ , cache_dir=snake_case__ ).read() _check_text_dataset(snake_case__ , snake_case__ ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__=("train",) ) -> Optional[Any]: assert isinstance(snake_case__ , snake_case__ ) for split in splits: lowerCAmelCase = dataset_dict[split] assert dataset.num_rows == 4 assert dataset.num_columns == 1 assert dataset.column_names == ["text"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize('''keep_in_memory''' , [False, True] ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> Optional[Any]: lowerCAmelCase = tmp_path / '''cache''' lowerCAmelCase = {'''text''': '''string'''} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): lowerCAmelCase = TextDatasetReader({'''train''': text_path} , cache_dir=snake_case__ , keep_in_memory=snake_case__ ).read() _check_text_datasetdict(snake_case__ , snake_case__ ) @pytest.mark.parametrize( '''features''' , [ None, {'''text''': '''string'''}, {'''text''': '''int32'''}, {'''text''': '''float32'''}, ] , ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> List[Any]: lowerCAmelCase = tmp_path / '''cache''' # CSV file loses col_1 string dtype information: default now is "int64" instead of "string" lowerCAmelCase = {'''text''': '''string'''} lowerCAmelCase = features.copy() if features else default_expected_features lowerCAmelCase = ( Features({feature: Value(snake_case__ ) for feature, dtype in features.items()} ) if features is not None else None ) lowerCAmelCase = TextDatasetReader({'''train''': text_path} , features=snake_case__ , cache_dir=snake_case__ ).read() _check_text_datasetdict(snake_case__ , snake_case__ ) @pytest.mark.parametrize('''split''' , [None, NamedSplit('''train''' ), '''train''', '''test'''] ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> Any: if split: lowerCAmelCase = {split: text_path} else: lowerCAmelCase = '''train''' lowerCAmelCase = {'''train''': text_path, '''test''': text_path} lowerCAmelCase = tmp_path / '''cache''' lowerCAmelCase = {'''text''': '''string'''} lowerCAmelCase = TextDatasetReader(snake_case__ , cache_dir=snake_case__ ).read() _check_text_datasetdict(snake_case__ , snake_case__ , splits=list(path.keys() ) ) assert all(dataset[split].split == split for split in path.keys() )
338
1
import json import pathlib import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision, slow from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import ConditionalDetrImageProcessor class lowercase_ ( unittest.TestCase ): """simple docstring""" def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=7 , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=30 , __SCREAMING_SNAKE_CASE=400 , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=[0.5, 0.5, 0.5] , __SCREAMING_SNAKE_CASE=[0.5, 0.5, 0.5] , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=1 / 255 , __SCREAMING_SNAKE_CASE=True , ) ->Union[str, Any]: # by setting size["longest_edge"] > max_resolution we're effectively not testing this :p lowerCAmelCase = size if size is not None else {'''shortest_edge''': 18, '''longest_edge''': 1333} lowerCAmelCase = parent lowerCAmelCase = batch_size lowerCAmelCase = num_channels lowerCAmelCase = min_resolution lowerCAmelCase = max_resolution lowerCAmelCase = do_resize lowerCAmelCase = size lowerCAmelCase = do_normalize lowerCAmelCase = image_mean lowerCAmelCase = image_std lowerCAmelCase = do_rescale lowerCAmelCase = rescale_factor lowerCAmelCase = do_pad def SCREAMING_SNAKE_CASE_ ( self ) ->List[str]: return { "do_resize": self.do_resize, "size": self.size, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, "do_rescale": self.do_rescale, "rescale_factor": self.rescale_factor, "do_pad": self.do_pad, } def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=False ) ->Dict: if not batched: lowerCAmelCase = image_inputs[0] if isinstance(__SCREAMING_SNAKE_CASE , Image.Image ): lowerCAmelCase , lowerCAmelCase = image.size else: lowerCAmelCase , lowerCAmelCase = image.shape[1], image.shape[2] if w < h: lowerCAmelCase = int(self.size['''shortest_edge'''] * h / w ) lowerCAmelCase = self.size['''shortest_edge'''] elif w > h: lowerCAmelCase = self.size['''shortest_edge'''] lowerCAmelCase = int(self.size['''shortest_edge'''] * w / h ) else: lowerCAmelCase = self.size['''shortest_edge'''] lowerCAmelCase = self.size['''shortest_edge'''] else: lowerCAmelCase = [] for image in image_inputs: lowerCAmelCase , lowerCAmelCase = self.get_expected_values([image] ) expected_values.append((expected_height, expected_width) ) lowerCAmelCase = max(__SCREAMING_SNAKE_CASE , key=lambda __SCREAMING_SNAKE_CASE : item[0] )[0] lowerCAmelCase = max(__SCREAMING_SNAKE_CASE , key=lambda __SCREAMING_SNAKE_CASE : item[1] )[1] return expected_height, expected_width @require_torch @require_vision class lowercase_ ( UpperCamelCase_ , unittest.TestCase ): """simple docstring""" UpperCAmelCase_ : Any = ConditionalDetrImageProcessor if is_vision_available() else None def SCREAMING_SNAKE_CASE_ ( self ) ->Tuple: lowerCAmelCase = ConditionalDetrImageProcessingTester(self ) @property def SCREAMING_SNAKE_CASE_ ( self ) ->str: return self.image_processor_tester.prepare_image_processor_dict() def SCREAMING_SNAKE_CASE_ ( self ) ->int: lowerCAmelCase = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''image_mean''' ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''image_std''' ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''do_normalize''' ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''do_resize''' ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''size''' ) ) def SCREAMING_SNAKE_CASE_ ( self ) ->Union[str, Any]: lowerCAmelCase = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'''shortest_edge''': 18, '''longest_edge''': 1333} ) self.assertEqual(image_processor.do_pad , __SCREAMING_SNAKE_CASE ) lowerCAmelCase = self.image_processing_class.from_dict( self.image_processor_dict , size=42 , max_size=84 , pad_and_return_pixel_mask=__SCREAMING_SNAKE_CASE ) self.assertEqual(image_processor.size , {'''shortest_edge''': 42, '''longest_edge''': 84} ) self.assertEqual(image_processor.do_pad , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[Any]: pass def SCREAMING_SNAKE_CASE_ ( self ) ->Tuple: # Initialize image_processing lowerCAmelCase = self.image_processing_class(**self.image_processor_dict ) # create random PIL images lowerCAmelCase = prepare_image_inputs(self.image_processor_tester , equal_resolution=__SCREAMING_SNAKE_CASE ) for image in image_inputs: self.assertIsInstance(__SCREAMING_SNAKE_CASE , Image.Image ) # Test not batched input lowerCAmelCase = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values lowerCAmelCase , lowerCAmelCase = self.image_processor_tester.get_expected_values(__SCREAMING_SNAKE_CASE ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched lowerCAmelCase , lowerCAmelCase = self.image_processor_tester.get_expected_values(__SCREAMING_SNAKE_CASE , batched=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = image_processing(__SCREAMING_SNAKE_CASE , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def SCREAMING_SNAKE_CASE_ ( self ) ->Tuple: # Initialize image_processing lowerCAmelCase = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors lowerCAmelCase = prepare_image_inputs(self.image_processor_tester , equal_resolution=__SCREAMING_SNAKE_CASE , numpify=__SCREAMING_SNAKE_CASE ) for image in image_inputs: self.assertIsInstance(__SCREAMING_SNAKE_CASE , np.ndarray ) # Test not batched input lowerCAmelCase = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values lowerCAmelCase , lowerCAmelCase = self.image_processor_tester.get_expected_values(__SCREAMING_SNAKE_CASE ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched lowerCAmelCase = image_processing(__SCREAMING_SNAKE_CASE , return_tensors='''pt''' ).pixel_values lowerCAmelCase , lowerCAmelCase = self.image_processor_tester.get_expected_values(__SCREAMING_SNAKE_CASE , batched=__SCREAMING_SNAKE_CASE ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def SCREAMING_SNAKE_CASE_ ( self ) ->str: # Initialize image_processing lowerCAmelCase = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors lowerCAmelCase = prepare_image_inputs(self.image_processor_tester , equal_resolution=__SCREAMING_SNAKE_CASE , torchify=__SCREAMING_SNAKE_CASE ) for image in image_inputs: self.assertIsInstance(__SCREAMING_SNAKE_CASE , torch.Tensor ) # Test not batched input lowerCAmelCase = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values lowerCAmelCase , lowerCAmelCase = self.image_processor_tester.get_expected_values(__SCREAMING_SNAKE_CASE ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched lowerCAmelCase = image_processing(__SCREAMING_SNAKE_CASE , return_tensors='''pt''' ).pixel_values lowerCAmelCase , lowerCAmelCase = self.image_processor_tester.get_expected_values(__SCREAMING_SNAKE_CASE , batched=__SCREAMING_SNAKE_CASE ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) @slow def SCREAMING_SNAKE_CASE_ ( self ) ->Union[str, Any]: # prepare image and target lowerCAmelCase = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) with open('''./tests/fixtures/tests_samples/COCO/coco_annotations.txt''' , '''r''' ) as f: lowerCAmelCase = json.loads(f.read() ) lowerCAmelCase = {'''image_id''': 39769, '''annotations''': target} # encode them lowerCAmelCase = ConditionalDetrImageProcessor.from_pretrained('''microsoft/conditional-detr-resnet-50''' ) lowerCAmelCase = image_processing(images=__SCREAMING_SNAKE_CASE , annotations=__SCREAMING_SNAKE_CASE , return_tensors='''pt''' ) # verify pixel values lowerCAmelCase = torch.Size([1, 3, 800, 1066] ) self.assertEqual(encoding['''pixel_values'''].shape , __SCREAMING_SNAKE_CASE ) lowerCAmelCase = torch.tensor([0.2_7_9_6, 0.3_1_3_8, 0.3_4_8_1] ) self.assertTrue(torch.allclose(encoding['''pixel_values'''][0, 0, 0, :3] , __SCREAMING_SNAKE_CASE , atol=1e-4 ) ) # verify area lowerCAmelCase = torch.tensor([5_8_8_7.9_6_0_0, 1_1_2_5_0.2_0_6_1, 4_8_9_3_5_3.8_4_3_8, 8_3_7_1_2_2.7_5_0_0, 1_4_7_9_6_7.5_1_5_6, 1_6_5_7_3_2.3_4_3_8] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''area'''] , __SCREAMING_SNAKE_CASE ) ) # verify boxes lowerCAmelCase = torch.Size([6, 4] ) self.assertEqual(encoding['''labels'''][0]['''boxes'''].shape , __SCREAMING_SNAKE_CASE ) lowerCAmelCase = torch.tensor([0.5_5_0_3, 0.2_7_6_5, 0.0_6_0_4, 0.2_2_1_5] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''boxes'''][0] , __SCREAMING_SNAKE_CASE , atol=1e-3 ) ) # verify image_id lowerCAmelCase = torch.tensor([39769] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''image_id'''] , __SCREAMING_SNAKE_CASE ) ) # verify is_crowd lowerCAmelCase = torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''iscrowd'''] , __SCREAMING_SNAKE_CASE ) ) # verify class_labels lowerCAmelCase = torch.tensor([75, 75, 63, 65, 17, 17] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''class_labels'''] , __SCREAMING_SNAKE_CASE ) ) # verify orig_size lowerCAmelCase = torch.tensor([480, 640] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''orig_size'''] , __SCREAMING_SNAKE_CASE ) ) # verify size lowerCAmelCase = torch.tensor([800, 1066] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''size'''] , __SCREAMING_SNAKE_CASE ) ) @slow def SCREAMING_SNAKE_CASE_ ( self ) ->Tuple: # prepare image, target and masks_path lowerCAmelCase = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) with open('''./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt''' , '''r''' ) as f: lowerCAmelCase = json.loads(f.read() ) lowerCAmelCase = {'''file_name''': '''000000039769.png''', '''image_id''': 39769, '''segments_info''': target} lowerCAmelCase = pathlib.Path('''./tests/fixtures/tests_samples/COCO/coco_panoptic''' ) # encode them lowerCAmelCase = ConditionalDetrImageProcessor(format='''coco_panoptic''' ) lowerCAmelCase = image_processing(images=__SCREAMING_SNAKE_CASE , annotations=__SCREAMING_SNAKE_CASE , masks_path=__SCREAMING_SNAKE_CASE , return_tensors='''pt''' ) # verify pixel values lowerCAmelCase = torch.Size([1, 3, 800, 1066] ) self.assertEqual(encoding['''pixel_values'''].shape , __SCREAMING_SNAKE_CASE ) lowerCAmelCase = torch.tensor([0.2_7_9_6, 0.3_1_3_8, 0.3_4_8_1] ) self.assertTrue(torch.allclose(encoding['''pixel_values'''][0, 0, 0, :3] , __SCREAMING_SNAKE_CASE , atol=1e-4 ) ) # verify area lowerCAmelCase = torch.tensor([1_4_7_9_7_9.6_8_7_5, 1_6_5_5_2_7.0_4_6_9, 4_8_4_6_3_8.5_9_3_8, 1_1_2_9_2.9_3_7_5, 5_8_7_9.6_5_6_2, 7_6_3_4.1_1_4_7] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''area'''] , __SCREAMING_SNAKE_CASE ) ) # verify boxes lowerCAmelCase = torch.Size([6, 4] ) self.assertEqual(encoding['''labels'''][0]['''boxes'''].shape , __SCREAMING_SNAKE_CASE ) lowerCAmelCase = torch.tensor([0.2_6_2_5, 0.5_4_3_7, 0.4_6_8_8, 0.8_6_2_5] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''boxes'''][0] , __SCREAMING_SNAKE_CASE , atol=1e-3 ) ) # verify image_id lowerCAmelCase = torch.tensor([39769] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''image_id'''] , __SCREAMING_SNAKE_CASE ) ) # verify is_crowd lowerCAmelCase = torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''iscrowd'''] , __SCREAMING_SNAKE_CASE ) ) # verify class_labels lowerCAmelCase = torch.tensor([17, 17, 63, 75, 75, 93] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''class_labels'''] , __SCREAMING_SNAKE_CASE ) ) # verify masks lowerCAmelCase = 822873 self.assertEqual(encoding['''labels'''][0]['''masks'''].sum().item() , __SCREAMING_SNAKE_CASE ) # verify orig_size lowerCAmelCase = torch.tensor([480, 640] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''orig_size'''] , __SCREAMING_SNAKE_CASE ) ) # verify size lowerCAmelCase = torch.tensor([800, 1066] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''size'''] , __SCREAMING_SNAKE_CASE ) )
338
def SCREAMING_SNAKE_CASE_ ( snake_case__ ) -> str: if isinstance(snake_case__ , snake_case__ ): raise TypeError('''\'float\' object cannot be interpreted as an integer''' ) if isinstance(snake_case__ , snake_case__ ): raise TypeError('''\'str\' object cannot be interpreted as an integer''' ) if num == 0: return "0b0" lowerCAmelCase = False if num < 0: lowerCAmelCase = True lowerCAmelCase = -num lowerCAmelCase = [] while num > 0: binary.insert(0 , num % 2 ) num >>= 1 if negative: return "-0b" + "".join(str(snake_case__ ) for e in binary ) return "0b" + "".join(str(snake_case__ ) for e in binary ) if __name__ == "__main__": import doctest doctest.testmod()
338
1
import tempfile import torch from diffusers import ( DEISMultistepScheduler, DPMSolverMultistepScheduler, DPMSolverSinglestepScheduler, UniPCMultistepScheduler, ) from .test_schedulers import SchedulerCommonTest class lowercase_ ( UpperCamelCase_ ): """simple docstring""" UpperCAmelCase_ : List[Any] = (DEISMultistepScheduler,) UpperCAmelCase_ : int = (("""num_inference_steps""", 25),) def SCREAMING_SNAKE_CASE_ ( self , **__SCREAMING_SNAKE_CASE ) ->str: lowerCAmelCase = { '''num_train_timesteps''': 1000, '''beta_start''': 0.0_0_0_1, '''beta_end''': 0.0_2, '''beta_schedule''': '''linear''', '''solver_order''': 2, } config.update(**__SCREAMING_SNAKE_CASE ) return config def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE=0 , **__SCREAMING_SNAKE_CASE ) ->Tuple: lowerCAmelCase = dict(self.forward_default_kwargs ) lowerCAmelCase = kwargs.pop('''num_inference_steps''' , __SCREAMING_SNAKE_CASE ) lowerCAmelCase = self.dummy_sample lowerCAmelCase = 0.1 * sample lowerCAmelCase = [residual + 0.2, residual + 0.1_5, residual + 0.1_0] for scheduler_class in self.scheduler_classes: lowerCAmelCase = self.get_scheduler_config(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) # copy over dummy past residuals lowerCAmelCase = dummy_past_residuals[: scheduler.config.solver_order] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = scheduler_class.from_pretrained(__SCREAMING_SNAKE_CASE ) new_scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) # copy over dummy past residuals lowerCAmelCase = dummy_past_residuals[: new_scheduler.config.solver_order] lowerCAmelCase , lowerCAmelCase = sample, sample for t in range(__SCREAMING_SNAKE_CASE , time_step + scheduler.config.solver_order + 1 ): lowerCAmelCase = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).prev_sample lowerCAmelCase = new_scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1e-5, "Scheduler outputs are not identical" def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[int]: pass def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE=0 , **__SCREAMING_SNAKE_CASE ) ->List[Any]: lowerCAmelCase = dict(self.forward_default_kwargs ) lowerCAmelCase = kwargs.pop('''num_inference_steps''' , __SCREAMING_SNAKE_CASE ) lowerCAmelCase = self.dummy_sample lowerCAmelCase = 0.1 * sample lowerCAmelCase = [residual + 0.2, residual + 0.1_5, residual + 0.1_0] for scheduler_class in self.scheduler_classes: lowerCAmelCase = self.get_scheduler_config() lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) # copy over dummy past residuals (must be after setting timesteps) lowerCAmelCase = dummy_past_residuals[: scheduler.config.solver_order] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = scheduler_class.from_pretrained(__SCREAMING_SNAKE_CASE ) # copy over dummy past residuals new_scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) # copy over dummy past residual (must be after setting timesteps) lowerCAmelCase = dummy_past_residuals[: new_scheduler.config.solver_order] lowerCAmelCase = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).prev_sample lowerCAmelCase = new_scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1e-5, "Scheduler outputs are not identical" def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE ) ->List[Any]: if scheduler is None: lowerCAmelCase = self.scheduler_classes[0] lowerCAmelCase = self.get_scheduler_config(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = self.scheduler_classes[0] lowerCAmelCase = self.get_scheduler_config(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = 10 lowerCAmelCase = self.dummy_model() lowerCAmelCase = self.dummy_sample_deter scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) for i, t in enumerate(scheduler.timesteps ): lowerCAmelCase = model(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) lowerCAmelCase = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ).prev_sample return sample def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[int]: lowerCAmelCase = dict(self.forward_default_kwargs ) lowerCAmelCase = kwargs.pop('''num_inference_steps''' , __SCREAMING_SNAKE_CASE ) for scheduler_class in self.scheduler_classes: lowerCAmelCase = self.get_scheduler_config() lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = self.dummy_sample lowerCAmelCase = 0.1 * sample if num_inference_steps is not None and hasattr(__SCREAMING_SNAKE_CASE , '''set_timesteps''' ): scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) elif num_inference_steps is not None and not hasattr(__SCREAMING_SNAKE_CASE , '''set_timesteps''' ): lowerCAmelCase = num_inference_steps # copy over dummy past residuals (must be done after set_timesteps) lowerCAmelCase = [residual + 0.2, residual + 0.1_5, residual + 0.1_0] lowerCAmelCase = dummy_past_residuals[: scheduler.config.solver_order] lowerCAmelCase = scheduler.timesteps[5] lowerCAmelCase = scheduler.timesteps[6] lowerCAmelCase = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).prev_sample lowerCAmelCase = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).prev_sample self.assertEqual(output_a.shape , sample.shape ) self.assertEqual(output_a.shape , output_a.shape ) def SCREAMING_SNAKE_CASE_ ( self ) ->int: # make sure that iterating over schedulers with same config names gives same results # for defaults lowerCAmelCase = DEISMultistepScheduler(**self.get_scheduler_config() ) lowerCAmelCase = self.full_loop(scheduler=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = torch.mean(torch.abs(__SCREAMING_SNAKE_CASE ) ) assert abs(result_mean.item() - 0.2_3_9_1_6 ) < 1e-3 lowerCAmelCase = DPMSolverSinglestepScheduler.from_config(scheduler.config ) lowerCAmelCase = DPMSolverMultistepScheduler.from_config(scheduler.config ) lowerCAmelCase = UniPCMultistepScheduler.from_config(scheduler.config ) lowerCAmelCase = DEISMultistepScheduler.from_config(scheduler.config ) lowerCAmelCase = self.full_loop(scheduler=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = torch.mean(torch.abs(__SCREAMING_SNAKE_CASE ) ) assert abs(result_mean.item() - 0.2_3_9_1_6 ) < 1e-3 def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[int]: for timesteps in [25, 50, 100, 999, 1000]: self.check_over_configs(num_train_timesteps=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->int: self.check_over_configs(thresholding=__SCREAMING_SNAKE_CASE ) for order in [1, 2, 3]: for solver_type in ["logrho"]: for threshold in [0.5, 1.0, 2.0]: for prediction_type in ["epsilon", "sample"]: self.check_over_configs( thresholding=__SCREAMING_SNAKE_CASE , prediction_type=__SCREAMING_SNAKE_CASE , sample_max_value=__SCREAMING_SNAKE_CASE , algorithm_type='''deis''' , solver_order=__SCREAMING_SNAKE_CASE , solver_type=__SCREAMING_SNAKE_CASE , ) def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[Any]: for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->Union[str, Any]: for algorithm_type in ["deis"]: for solver_type in ["logrho"]: for order in [1, 2, 3]: for prediction_type in ["epsilon", "sample"]: self.check_over_configs( solver_order=__SCREAMING_SNAKE_CASE , solver_type=__SCREAMING_SNAKE_CASE , prediction_type=__SCREAMING_SNAKE_CASE , algorithm_type=__SCREAMING_SNAKE_CASE , ) lowerCAmelCase = self.full_loop( solver_order=__SCREAMING_SNAKE_CASE , solver_type=__SCREAMING_SNAKE_CASE , prediction_type=__SCREAMING_SNAKE_CASE , algorithm_type=__SCREAMING_SNAKE_CASE , ) assert not torch.isnan(__SCREAMING_SNAKE_CASE ).any(), "Samples have nan numbers" def SCREAMING_SNAKE_CASE_ ( self ) ->List[Any]: self.check_over_configs(lower_order_final=__SCREAMING_SNAKE_CASE ) self.check_over_configs(lower_order_final=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->List[Any]: for num_inference_steps in [1, 2, 3, 5, 10, 50, 100, 999, 1000]: self.check_over_forward(num_inference_steps=__SCREAMING_SNAKE_CASE , time_step=0 ) def SCREAMING_SNAKE_CASE_ ( self ) ->Dict: lowerCAmelCase = self.full_loop() lowerCAmelCase = torch.mean(torch.abs(__SCREAMING_SNAKE_CASE ) ) assert abs(result_mean.item() - 0.2_3_9_1_6 ) < 1e-3 def SCREAMING_SNAKE_CASE_ ( self ) ->List[str]: lowerCAmelCase = self.full_loop(prediction_type='''v_prediction''' ) lowerCAmelCase = torch.mean(torch.abs(__SCREAMING_SNAKE_CASE ) ) assert abs(result_mean.item() - 0.0_9_1 ) < 1e-3 def SCREAMING_SNAKE_CASE_ ( self ) ->Dict: lowerCAmelCase = self.scheduler_classes[0] lowerCAmelCase = self.get_scheduler_config(thresholding=__SCREAMING_SNAKE_CASE , dynamic_thresholding_ratio=0 ) lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = 10 lowerCAmelCase = self.dummy_model() lowerCAmelCase = self.dummy_sample_deter.half() scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) for i, t in enumerate(scheduler.timesteps ): lowerCAmelCase = model(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) lowerCAmelCase = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ).prev_sample assert sample.dtype == torch.floataa
338
class lowercase_ : """simple docstring""" def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ->Any: lowerCAmelCase = name lowerCAmelCase = value lowerCAmelCase = weight def __repr__( self ) ->str: return F"{self.__class__.__name__}({self.name}, {self.value}, {self.weight})" def SCREAMING_SNAKE_CASE_ ( self ) ->List[Any]: return self.value def SCREAMING_SNAKE_CASE_ ( self ) ->int: return self.name def SCREAMING_SNAKE_CASE_ ( self ) ->Union[str, Any]: return self.weight def SCREAMING_SNAKE_CASE_ ( self ) ->Tuple: return self.value / self.weight def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> int: lowerCAmelCase = [] for i in range(len(snake_case__ ) ): menu.append(Things(name[i] , value[i] , weight[i] ) ) return menu def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> Optional[int]: lowerCAmelCase = sorted(snake_case__ , key=snake_case__ , reverse=snake_case__ ) lowerCAmelCase = [] lowerCAmelCase , lowerCAmelCase = 0.0, 0.0 for i in range(len(snake_case__ ) ): if (total_cost + items_copy[i].get_weight()) <= max_cost: result.append(items_copy[i] ) total_cost += items_copy[i].get_weight() total_value += items_copy[i].get_value() return (result, total_value) def SCREAMING_SNAKE_CASE_ ( ) -> Optional[int]: pass if __name__ == "__main__": import doctest doctest.testmod()
338
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available lowercase__ : List[Any] = { '''configuration_xlm''': ['''XLM_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''XLMConfig''', '''XLMOnnxConfig'''], '''tokenization_xlm''': ['''XLMTokenizer'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase__ : Dict = [ '''XLM_PRETRAINED_MODEL_ARCHIVE_LIST''', '''XLMForMultipleChoice''', '''XLMForQuestionAnswering''', '''XLMForQuestionAnsweringSimple''', '''XLMForSequenceClassification''', '''XLMForTokenClassification''', '''XLMModel''', '''XLMPreTrainedModel''', '''XLMWithLMHeadModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase__ : int = [ '''TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFXLMForMultipleChoice''', '''TFXLMForQuestionAnsweringSimple''', '''TFXLMForSequenceClassification''', '''TFXLMForTokenClassification''', '''TFXLMMainLayer''', '''TFXLMModel''', '''TFXLMPreTrainedModel''', '''TFXLMWithLMHeadModel''', ] if TYPE_CHECKING: from .configuration_xlm import XLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XLMConfig, XLMOnnxConfig from .tokenization_xlm import XLMTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xlm import ( XLM_PRETRAINED_MODEL_ARCHIVE_LIST, XLMForMultipleChoice, XLMForQuestionAnswering, XLMForQuestionAnsweringSimple, XLMForSequenceClassification, XLMForTokenClassification, XLMModel, XLMPreTrainedModel, XLMWithLMHeadModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_xlm import ( TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFXLMForMultipleChoice, TFXLMForQuestionAnsweringSimple, TFXLMForSequenceClassification, TFXLMForTokenClassification, TFXLMMainLayer, TFXLMModel, TFXLMPreTrainedModel, TFXLMWithLMHeadModel, ) else: import sys lowercase__ : Optional[Any] = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
338
import numpy as np import skfuzzy as fuzz if __name__ == "__main__": # Create universe of discourse in Python using linspace () lowercase__ : Dict = np.linspace(start=0, stop=7_5, num=7_5, endpoint=True, retstep=False) # Create two fuzzy sets by defining any membership function # (trapmf(), gbellmf(), gaussmf(), etc). lowercase__ : Optional[int] = [0, 2_5, 5_0] lowercase__ : Union[str, Any] = [2_5, 5_0, 7_5] lowercase__ : int = fuzz.membership.trimf(X, abca) lowercase__ : Tuple = fuzz.membership.trimf(X, abca) # Compute the different operations using inbuilt functions. lowercase__ : List[str] = np.ones(7_5) lowercase__ : Any = np.zeros((7_5,)) # 1. Union = max(µA(x), µB(x)) lowercase__ : Union[str, Any] = fuzz.fuzzy_or(X, young, X, middle_aged)[1] # 2. Intersection = min(µA(x), µB(x)) lowercase__ : int = fuzz.fuzzy_and(X, young, X, middle_aged)[1] # 3. Complement (A) = (1- min(µA(x)) lowercase__ : Union[str, Any] = fuzz.fuzzy_not(young) # 4. Difference (A/B) = min(µA(x),(1- µB(x))) lowercase__ : Optional[int] = fuzz.fuzzy_and(X, young, X, fuzz.fuzzy_not(middle_aged)[1])[1] # 5. Algebraic Sum = [µA(x) + µB(x) – (µA(x) * µB(x))] lowercase__ : Any = young + middle_aged - (young * middle_aged) # 6. Algebraic Product = (µA(x) * µB(x)) lowercase__ : str = young * middle_aged # 7. Bounded Sum = min[1,(µA(x), µB(x))] lowercase__ : Tuple = fuzz.fuzzy_and(X, one, X, young + middle_aged)[1] # 8. Bounded difference = min[0,(µA(x), µB(x))] lowercase__ : Tuple = fuzz.fuzzy_or(X, zero, X, young - middle_aged)[1] # max-min composition # max-product composition # Plot each set A, set B and each operation result using plot() and subplot(). from matplotlib import pyplot as plt plt.figure() plt.subplot(4, 3, 1) plt.plot(X, young) plt.title('''Young''') plt.grid(True) plt.subplot(4, 3, 2) plt.plot(X, middle_aged) plt.title('''Middle aged''') plt.grid(True) plt.subplot(4, 3, 3) plt.plot(X, union) plt.title('''union''') plt.grid(True) plt.subplot(4, 3, 4) plt.plot(X, intersection) plt.title('''intersection''') plt.grid(True) plt.subplot(4, 3, 5) plt.plot(X, complement_a) plt.title('''complement_a''') plt.grid(True) plt.subplot(4, 3, 6) plt.plot(X, difference) plt.title('''difference a/b''') plt.grid(True) plt.subplot(4, 3, 7) plt.plot(X, alg_sum) plt.title('''alg_sum''') plt.grid(True) plt.subplot(4, 3, 8) plt.plot(X, alg_product) plt.title('''alg_product''') plt.grid(True) plt.subplot(4, 3, 9) plt.plot(X, bdd_sum) plt.title('''bdd_sum''') plt.grid(True) plt.subplot(4, 3, 1_0) plt.plot(X, bdd_difference) plt.title('''bdd_difference''') plt.grid(True) plt.subplots_adjust(hspace=0.5) plt.show()
338
1
import json from typing import TYPE_CHECKING, List, Optional, Tuple from tokenizers import pre_tokenizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation lowercase__ : List[str] = logging.get_logger(__name__) lowercase__ : Optional[int] = {'''vocab_file''': '''vocab.json''', '''merges_file''': '''merges.txt''', '''tokenizer_file''': '''tokenizer.json'''} lowercase__ : List[Any] = { '''tokenizer_file''': { '''EleutherAI/gpt-neox-20b''': '''https://huggingface.co/EleutherAI/gpt-neox-20b/resolve/main/tokenizer.json''', }, } lowercase__ : Any = { '''gpt-neox-20b''': 2_0_4_8, } class lowercase_ ( UpperCamelCase_ ): """simple docstring""" UpperCAmelCase_ : Dict = VOCAB_FILES_NAMES UpperCAmelCase_ : Optional[int] = PRETRAINED_VOCAB_FILES_MAP UpperCAmelCase_ : List[str] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCAmelCase_ : Dict = ["""input_ids""", """attention_mask"""] def __init__( self , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE="<|endoftext|>" , __SCREAMING_SNAKE_CASE="<|endoftext|>" , __SCREAMING_SNAKE_CASE="<|endoftext|>" , __SCREAMING_SNAKE_CASE=False , **__SCREAMING_SNAKE_CASE , ) ->Tuple: super().__init__( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , tokenizer_file=__SCREAMING_SNAKE_CASE , unk_token=__SCREAMING_SNAKE_CASE , bos_token=__SCREAMING_SNAKE_CASE , eos_token=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) lowerCAmelCase = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get('''add_prefix_space''' , __SCREAMING_SNAKE_CASE ) != add_prefix_space: lowerCAmelCase = getattr(__SCREAMING_SNAKE_CASE , pre_tok_state.pop('''type''' ) ) lowerCAmelCase = add_prefix_space lowerCAmelCase = pre_tok_class(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = add_prefix_space def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ) ->Tuple[str]: lowerCAmelCase = self._tokenizer.model.save(__SCREAMING_SNAKE_CASE , name=__SCREAMING_SNAKE_CASE ) return tuple(__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE ) ->List[int]: lowerCAmelCase = [] for is_user, text in conversation.iter_texts(): input_ids.extend(self.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) + [self.eos_token_id] ) if len(__SCREAMING_SNAKE_CASE ) > self.model_max_length: lowerCAmelCase = input_ids[-self.model_max_length :] return input_ids
338
import torch from diffusers import DDPMScheduler from .test_schedulers import SchedulerCommonTest class lowercase_ ( UpperCamelCase_ ): """simple docstring""" UpperCAmelCase_ : str = (DDPMScheduler,) def SCREAMING_SNAKE_CASE_ ( self , **__SCREAMING_SNAKE_CASE ) ->Optional[Any]: lowerCAmelCase = { '''num_train_timesteps''': 1000, '''beta_start''': 0.0_0_0_1, '''beta_end''': 0.0_2, '''beta_schedule''': '''linear''', '''variance_type''': '''fixed_small''', '''clip_sample''': True, } config.update(**__SCREAMING_SNAKE_CASE ) return config def SCREAMING_SNAKE_CASE_ ( self ) ->Tuple: for timesteps in [1, 5, 100, 1000]: self.check_over_configs(num_train_timesteps=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->List[Any]: for beta_start, beta_end in zip([0.0_0_0_1, 0.0_0_1, 0.0_1, 0.1] , [0.0_0_2, 0.0_2, 0.2, 2] ): self.check_over_configs(beta_start=__SCREAMING_SNAKE_CASE , beta_end=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[Any]: for schedule in ["linear", "squaredcos_cap_v2"]: self.check_over_configs(beta_schedule=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->Dict: for variance in ["fixed_small", "fixed_large", "other"]: self.check_over_configs(variance_type=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->List[str]: for clip_sample in [True, False]: self.check_over_configs(clip_sample=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->Any: self.check_over_configs(thresholding=__SCREAMING_SNAKE_CASE ) for threshold in [0.5, 1.0, 2.0]: for prediction_type in ["epsilon", "sample", "v_prediction"]: self.check_over_configs( thresholding=__SCREAMING_SNAKE_CASE , prediction_type=__SCREAMING_SNAKE_CASE , sample_max_value=__SCREAMING_SNAKE_CASE , ) def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[int]: for prediction_type in ["epsilon", "sample", "v_prediction"]: self.check_over_configs(prediction_type=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[Any]: for t in [0, 500, 999]: self.check_over_forward(time_step=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->Tuple: lowerCAmelCase = self.scheduler_classes[0] lowerCAmelCase = self.get_scheduler_config() lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) assert torch.sum(torch.abs(scheduler._get_variance(0 ) - 0.0 ) ) < 1e-5 assert torch.sum(torch.abs(scheduler._get_variance(487 ) - 0.0_0_9_7_9 ) ) < 1e-5 assert torch.sum(torch.abs(scheduler._get_variance(999 ) - 0.0_2 ) ) < 1e-5 def SCREAMING_SNAKE_CASE_ ( self ) ->str: lowerCAmelCase = self.scheduler_classes[0] lowerCAmelCase = self.get_scheduler_config() lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = len(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = self.dummy_model() lowerCAmelCase = self.dummy_sample_deter lowerCAmelCase = torch.manual_seed(0 ) for t in reversed(range(__SCREAMING_SNAKE_CASE ) ): # 1. predict noise residual lowerCAmelCase = model(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # 2. predict previous mean of sample x_t-1 lowerCAmelCase = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , generator=__SCREAMING_SNAKE_CASE ).prev_sample # if t > 0: # noise = self.dummy_sample_deter # variance = scheduler.get_variance(t) ** (0.5) * noise # # sample = pred_prev_sample + variance lowerCAmelCase = pred_prev_sample lowerCAmelCase = torch.sum(torch.abs(__SCREAMING_SNAKE_CASE ) ) lowerCAmelCase = torch.mean(torch.abs(__SCREAMING_SNAKE_CASE ) ) assert abs(result_sum.item() - 2_5_8.9_6_0_6 ) < 1e-2 assert abs(result_mean.item() - 0.3_3_7_2 ) < 1e-3 def SCREAMING_SNAKE_CASE_ ( self ) ->str: lowerCAmelCase = self.scheduler_classes[0] lowerCAmelCase = self.get_scheduler_config(prediction_type='''v_prediction''' ) lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = len(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = self.dummy_model() lowerCAmelCase = self.dummy_sample_deter lowerCAmelCase = torch.manual_seed(0 ) for t in reversed(range(__SCREAMING_SNAKE_CASE ) ): # 1. predict noise residual lowerCAmelCase = model(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # 2. predict previous mean of sample x_t-1 lowerCAmelCase = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , generator=__SCREAMING_SNAKE_CASE ).prev_sample # if t > 0: # noise = self.dummy_sample_deter # variance = scheduler.get_variance(t) ** (0.5) * noise # # sample = pred_prev_sample + variance lowerCAmelCase = pred_prev_sample lowerCAmelCase = torch.sum(torch.abs(__SCREAMING_SNAKE_CASE ) ) lowerCAmelCase = torch.mean(torch.abs(__SCREAMING_SNAKE_CASE ) ) assert abs(result_sum.item() - 2_0_2.0_2_9_6 ) < 1e-2 assert abs(result_mean.item() - 0.2_6_3_1 ) < 1e-3 def SCREAMING_SNAKE_CASE_ ( self ) ->Any: lowerCAmelCase = self.scheduler_classes[0] lowerCAmelCase = self.get_scheduler_config() lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = [100, 87, 50, 1, 0] scheduler.set_timesteps(timesteps=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = scheduler.timesteps for i, timestep in enumerate(__SCREAMING_SNAKE_CASE ): if i == len(__SCREAMING_SNAKE_CASE ) - 1: lowerCAmelCase = -1 else: lowerCAmelCase = timesteps[i + 1] lowerCAmelCase = scheduler.previous_timestep(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = prev_t.item() self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[Any]: lowerCAmelCase = self.scheduler_classes[0] lowerCAmelCase = self.get_scheduler_config() lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = [100, 87, 50, 51, 0] with self.assertRaises(__SCREAMING_SNAKE_CASE , msg='''`custom_timesteps` must be in descending order.''' ): scheduler.set_timesteps(timesteps=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->str: lowerCAmelCase = self.scheduler_classes[0] lowerCAmelCase = self.get_scheduler_config() lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = [100, 87, 50, 1, 0] lowerCAmelCase = len(__SCREAMING_SNAKE_CASE ) with self.assertRaises(__SCREAMING_SNAKE_CASE , msg='''Can only pass one of `num_inference_steps` or `custom_timesteps`.''' ): scheduler.set_timesteps(num_inference_steps=__SCREAMING_SNAKE_CASE , timesteps=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->Any: lowerCAmelCase = self.scheduler_classes[0] lowerCAmelCase = self.get_scheduler_config() lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = [scheduler.config.num_train_timesteps] with self.assertRaises( __SCREAMING_SNAKE_CASE , msg='''`timesteps` must start before `self.config.train_timesteps`: {scheduler.config.num_train_timesteps}}''' , ): scheduler.set_timesteps(timesteps=__SCREAMING_SNAKE_CASE )
338
1
from collections import defaultdict from math import ceil, sqrt def SCREAMING_SNAKE_CASE_ ( snake_case__ = 1_0_0_0_0_0_0 , snake_case__ = 1_0 ) -> int: lowerCAmelCase = defaultdict(snake_case__ ) for outer_width in range(3 , (t_limit // 4) + 2 ): if outer_width * outer_width > t_limit: lowerCAmelCase = max( ceil(sqrt(outer_width * outer_width - t_limit ) ) , 1 ) else: lowerCAmelCase = 1 hole_width_lower_bound += (outer_width - hole_width_lower_bound) % 2 for hole_width in range(snake_case__ , outer_width - 1 , 2 ): count[outer_width * outer_width - hole_width * hole_width] += 1 return sum(1 for n in count.values() if 1 <= n <= 1_0 ) if __name__ == "__main__": print(f'{solution() = }')
338
import json import os from typing import Optional import numpy as np from ...feature_extraction_utils import BatchFeature from ...processing_utils import ProcessorMixin from ...utils import logging from ...utils.hub import get_file_from_repo from ..auto import AutoTokenizer lowercase__ : str = logging.get_logger(__name__) class lowercase_ ( UpperCamelCase_ ): """simple docstring""" UpperCAmelCase_ : Any = """AutoTokenizer""" UpperCAmelCase_ : Optional[int] = ["""tokenizer"""] UpperCAmelCase_ : str = { """semantic_prompt""": 1, """coarse_prompt""": 2, """fine_prompt""": 2, } def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None ) ->Optional[Any]: super().__init__(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = speaker_embeddings @classmethod def SCREAMING_SNAKE_CASE_ ( cls , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE="speaker_embeddings_path.json" , **__SCREAMING_SNAKE_CASE ) ->Tuple: if speaker_embeddings_dict_path is not None: lowerCAmelCase = get_file_from_repo( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , subfolder=kwargs.pop('''subfolder''' , __SCREAMING_SNAKE_CASE ) , cache_dir=kwargs.pop('''cache_dir''' , __SCREAMING_SNAKE_CASE ) , force_download=kwargs.pop('''force_download''' , __SCREAMING_SNAKE_CASE ) , proxies=kwargs.pop('''proxies''' , __SCREAMING_SNAKE_CASE ) , resume_download=kwargs.pop('''resume_download''' , __SCREAMING_SNAKE_CASE ) , local_files_only=kwargs.pop('''local_files_only''' , __SCREAMING_SNAKE_CASE ) , use_auth_token=kwargs.pop('''use_auth_token''' , __SCREAMING_SNAKE_CASE ) , revision=kwargs.pop('''revision''' , __SCREAMING_SNAKE_CASE ) , ) if speaker_embeddings_path is None: logger.warning( F"`{os.path.join(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE )}` does not exists\n , no preloaded speaker embeddings will be used - Make sure to provide a correct path to the json\n dictionnary if wanted, otherwise set `speaker_embeddings_dict_path=None`." ) lowerCAmelCase = None else: with open(__SCREAMING_SNAKE_CASE ) as speaker_embeddings_json: lowerCAmelCase = json.load(__SCREAMING_SNAKE_CASE ) else: lowerCAmelCase = None lowerCAmelCase = AutoTokenizer.from_pretrained(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) return cls(tokenizer=__SCREAMING_SNAKE_CASE , speaker_embeddings=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE="speaker_embeddings_path.json" , __SCREAMING_SNAKE_CASE="speaker_embeddings" , __SCREAMING_SNAKE_CASE = False , **__SCREAMING_SNAKE_CASE , ) ->int: if self.speaker_embeddings is not None: os.makedirs(os.path.join(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , '''v2''' ) , exist_ok=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = {} lowerCAmelCase = save_directory for prompt_key in self.speaker_embeddings: if prompt_key != "repo_or_path": lowerCAmelCase = self._load_voice_preset(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = {} for key in self.speaker_embeddings[prompt_key]: np.save( os.path.join( embeddings_dict['''repo_or_path'''] , __SCREAMING_SNAKE_CASE , F"{prompt_key}_{key}" ) , voice_preset[key] , allow_pickle=__SCREAMING_SNAKE_CASE , ) lowerCAmelCase = os.path.join(__SCREAMING_SNAKE_CASE , F"{prompt_key}_{key}.npy" ) lowerCAmelCase = tmp_dict with open(os.path.join(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) , '''w''' ) as fp: json.dump(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) super().save_pretrained(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE = None , **__SCREAMING_SNAKE_CASE ) ->List[str]: lowerCAmelCase = self.speaker_embeddings[voice_preset] lowerCAmelCase = {} for key in ["semantic_prompt", "coarse_prompt", "fine_prompt"]: if key not in voice_preset_paths: raise ValueError( F"Voice preset unrecognized, missing {key} as a key in self.speaker_embeddings[{voice_preset}]." ) lowerCAmelCase = get_file_from_repo( self.speaker_embeddings.get('''repo_or_path''' , '''/''' ) , voice_preset_paths[key] , subfolder=kwargs.pop('''subfolder''' , __SCREAMING_SNAKE_CASE ) , cache_dir=kwargs.pop('''cache_dir''' , __SCREAMING_SNAKE_CASE ) , force_download=kwargs.pop('''force_download''' , __SCREAMING_SNAKE_CASE ) , proxies=kwargs.pop('''proxies''' , __SCREAMING_SNAKE_CASE ) , resume_download=kwargs.pop('''resume_download''' , __SCREAMING_SNAKE_CASE ) , local_files_only=kwargs.pop('''local_files_only''' , __SCREAMING_SNAKE_CASE ) , use_auth_token=kwargs.pop('''use_auth_token''' , __SCREAMING_SNAKE_CASE ) , revision=kwargs.pop('''revision''' , __SCREAMING_SNAKE_CASE ) , ) if path is None: raise ValueError( F"`{os.path.join(self.speaker_embeddings.get('repo_or_path' , '/' ) , voice_preset_paths[key] )}` does not exists\n , no preloaded voice preset will be used - Make sure to provide correct paths to the {voice_preset}\n embeddings." ) lowerCAmelCase = np.load(__SCREAMING_SNAKE_CASE ) return voice_preset_dict def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE = None ) ->Tuple: for key in ["semantic_prompt", "coarse_prompt", "fine_prompt"]: if key not in voice_preset: raise ValueError(F"Voice preset unrecognized, missing {key} as a key." ) if not isinstance(voice_preset[key] , np.ndarray ): raise ValueError(F"{key} voice preset must be a {str(self.preset_shape[key] )}D ndarray." ) if len(voice_preset[key].shape ) != self.preset_shape[key]: raise ValueError(F"{key} voice preset must be a {str(self.preset_shape[key] )}D ndarray." ) def __call__( self , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE="pt" , __SCREAMING_SNAKE_CASE=256 , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=False , **__SCREAMING_SNAKE_CASE , ) ->int: if voice_preset is not None and not isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): if ( isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) and self.speaker_embeddings is not None and voice_preset in self.speaker_embeddings ): lowerCAmelCase = self._load_voice_preset(__SCREAMING_SNAKE_CASE ) else: if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) and not voice_preset.endswith('''.npz''' ): lowerCAmelCase = voice_preset + '''.npz''' lowerCAmelCase = np.load(__SCREAMING_SNAKE_CASE ) if voice_preset is not None: self._validate_voice_preset_dict(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) lowerCAmelCase = BatchFeature(data=__SCREAMING_SNAKE_CASE , tensor_type=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = self.tokenizer( __SCREAMING_SNAKE_CASE , return_tensors=__SCREAMING_SNAKE_CASE , padding='''max_length''' , max_length=__SCREAMING_SNAKE_CASE , return_attention_mask=__SCREAMING_SNAKE_CASE , return_token_type_ids=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) if voice_preset is not None: lowerCAmelCase = voice_preset return encoded_text
338
1
import numpy as np class lowercase_ : """simple docstring""" def __init__( self ) ->List[Any]: lowerCAmelCase = (0, 0) lowerCAmelCase = None lowerCAmelCase = 0 lowerCAmelCase = 0 lowerCAmelCase = 0 def __eq__( self , __SCREAMING_SNAKE_CASE ) ->Optional[Any]: return self.position == cell.position def SCREAMING_SNAKE_CASE_ ( self ) ->Tuple: print(self.position ) class lowercase_ : """simple docstring""" def __init__( self , __SCREAMING_SNAKE_CASE=(5, 5) ) ->Any: lowerCAmelCase = np.zeros(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = world_size[0] lowerCAmelCase = world_size[1] def SCREAMING_SNAKE_CASE_ ( self ) ->List[str]: print(self.w ) def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE ) ->Optional[int]: lowerCAmelCase = [ (-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1), ] lowerCAmelCase = cell.position[0] lowerCAmelCase = cell.position[1] lowerCAmelCase = [] for n in neughbour_cord: lowerCAmelCase = current_x + n[0] lowerCAmelCase = current_y + n[1] if 0 <= x < self.world_x_limit and 0 <= y < self.world_y_limit: lowerCAmelCase = Cell() lowerCAmelCase = (x, y) lowerCAmelCase = cell neighbours.append(__SCREAMING_SNAKE_CASE ) return neighbours def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> int: lowerCAmelCase = [] lowerCAmelCase = [] _open.append(snake_case__ ) while _open: lowerCAmelCase = np.argmin([n.f for n in _open] ) lowerCAmelCase = _open[min_f] _closed.append(_open.pop(snake_case__ ) ) if current == goal: break for n in world.get_neigbours(snake_case__ ): for c in _closed: if c == n: continue lowerCAmelCase = current.g + 1 lowerCAmelCase , lowerCAmelCase = n.position lowerCAmelCase , lowerCAmelCase = goal.position lowerCAmelCase = (ya - ya) ** 2 + (xa - xa) ** 2 lowerCAmelCase = n.h + n.g for c in _open: if c == n and c.f < n.f: continue _open.append(snake_case__ ) lowerCAmelCase = [] while current.parent is not None: path.append(current.position ) lowerCAmelCase = current.parent path.append(current.position ) return path[::-1] if __name__ == "__main__": lowercase__ : str = Gridworld() # Start position and goal lowercase__ : str = Cell() lowercase__ : str = (0, 0) lowercase__ : Optional[Any] = Cell() lowercase__ : Optional[int] = (4, 4) print(f'path from {start.position} to {goal.position}') lowercase__ : List[Any] = astar(world, start, goal) # Just for visual reasons. for i in s: lowercase__ : List[str] = 1 print(world.w)
338
import warnings from diffusers import StableDiffusionInpaintPipeline as StableDiffusionInpaintPipeline # noqa F401 warnings.warn( '''The `inpainting.py` script is outdated. Please use directly `from diffusers import''' ''' StableDiffusionInpaintPipeline` instead.''' )
338
1
from typing import Any, Callable, Dict, List, Optional, Union import torch from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, DiffusionPipeline, LMSDiscreteScheduler, PNDMScheduler, StableDiffusionPipeline, UNetaDConditionModel, ) from diffusers.pipelines.stable_diffusion import StableDiffusionPipelineOutput from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker UpperCAmelCase__ = "CompVis/stable-diffusion-v1-1" UpperCAmelCase__ = "CompVis/stable-diffusion-v1-2" UpperCAmelCase__ = "CompVis/stable-diffusion-v1-3" UpperCAmelCase__ = "CompVis/stable-diffusion-v1-4" class lowercase_ ( lowercase ): '''simple docstring''' def __init__( self : Tuple , __UpperCAmelCase : AutoencoderKL , __UpperCAmelCase : CLIPTextModel , __UpperCAmelCase : CLIPTokenizer , __UpperCAmelCase : UNetaDConditionModel , __UpperCAmelCase : Union[DDIMScheduler, PNDMScheduler, LMSDiscreteScheduler] , __UpperCAmelCase : StableDiffusionSafetyChecker , __UpperCAmelCase : CLIPImageProcessor , __UpperCAmelCase : bool = True , ) ->List[str]: """simple docstring""" super()._init_() a = StableDiffusionPipeline.from_pretrained(__UpperCAmelCase ) a = StableDiffusionPipeline.from_pretrained(__UpperCAmelCase ) a = StableDiffusionPipeline.from_pretrained(__UpperCAmelCase ) a = StableDiffusionPipeline( vae=__UpperCAmelCase , text_encoder=__UpperCAmelCase , tokenizer=__UpperCAmelCase , unet=__UpperCAmelCase , scheduler=__UpperCAmelCase , safety_checker=__UpperCAmelCase , feature_extractor=__UpperCAmelCase , requires_safety_checker=__UpperCAmelCase , ) self.register_modules(pipelinea=self.pipea , pipelinea=self.pipea , pipelinea=self.pipea , pipelinea=self.pipea ) @property def __lowerCAmelCase ( self : str ) ->Dict[str, Any]: """simple docstring""" return {k: getattr(self , __UpperCAmelCase ) for k in self.config.keys() if not k.startswith('''_''' )} def __lowerCAmelCase ( self : str , __UpperCAmelCase : Optional[Union[str, int]] = "auto" ) ->int: """simple docstring""" if slice_size == "auto": # half the attention head size is usually a good trade-off between # speed and memory a = self.unet.config.attention_head_dim // 2 self.unet.set_attention_slice(__UpperCAmelCase ) def __lowerCAmelCase ( self : Optional[int] ) ->Tuple: """simple docstring""" self.enable_attention_slicing(__UpperCAmelCase ) @torch.no_grad() def __lowerCAmelCase ( self : Dict , __UpperCAmelCase : Union[str, List[str]] , __UpperCAmelCase : int = 512 , __UpperCAmelCase : int = 512 , __UpperCAmelCase : int = 50 , __UpperCAmelCase : float = 7.5 , __UpperCAmelCase : Optional[Union[str, List[str]]] = None , __UpperCAmelCase : Optional[int] = 1 , __UpperCAmelCase : float = 0.0 , __UpperCAmelCase : Optional[torch.Generator] = None , __UpperCAmelCase : Optional[torch.FloatTensor] = None , __UpperCAmelCase : Optional[str] = "pil" , __UpperCAmelCase : bool = True , __UpperCAmelCase : Optional[Callable[[int, int, torch.FloatTensor], None]] = None , __UpperCAmelCase : int = 1 , **__UpperCAmelCase : List[str] , ) ->Any: """simple docstring""" return self.pipea( prompt=__UpperCAmelCase , height=__UpperCAmelCase , width=__UpperCAmelCase , num_inference_steps=__UpperCAmelCase , guidance_scale=__UpperCAmelCase , negative_prompt=__UpperCAmelCase , num_images_per_prompt=__UpperCAmelCase , eta=__UpperCAmelCase , generator=__UpperCAmelCase , latents=__UpperCAmelCase , output_type=__UpperCAmelCase , return_dict=__UpperCAmelCase , callback=__UpperCAmelCase , callback_steps=__UpperCAmelCase , **__UpperCAmelCase , ) @torch.no_grad() def __lowerCAmelCase ( self : Optional[int] , __UpperCAmelCase : Union[str, List[str]] , __UpperCAmelCase : int = 512 , __UpperCAmelCase : int = 512 , __UpperCAmelCase : int = 50 , __UpperCAmelCase : float = 7.5 , __UpperCAmelCase : Optional[Union[str, List[str]]] = None , __UpperCAmelCase : Optional[int] = 1 , __UpperCAmelCase : float = 0.0 , __UpperCAmelCase : Optional[torch.Generator] = None , __UpperCAmelCase : Optional[torch.FloatTensor] = None , __UpperCAmelCase : Optional[str] = "pil" , __UpperCAmelCase : bool = True , __UpperCAmelCase : Optional[Callable[[int, int, torch.FloatTensor], None]] = None , __UpperCAmelCase : int = 1 , **__UpperCAmelCase : Any , ) ->str: """simple docstring""" return self.pipea( prompt=__UpperCAmelCase , height=__UpperCAmelCase , width=__UpperCAmelCase , num_inference_steps=__UpperCAmelCase , guidance_scale=__UpperCAmelCase , negative_prompt=__UpperCAmelCase , num_images_per_prompt=__UpperCAmelCase , eta=__UpperCAmelCase , generator=__UpperCAmelCase , latents=__UpperCAmelCase , output_type=__UpperCAmelCase , return_dict=__UpperCAmelCase , callback=__UpperCAmelCase , callback_steps=__UpperCAmelCase , **__UpperCAmelCase , ) @torch.no_grad() def __lowerCAmelCase ( self : List[str] , __UpperCAmelCase : Union[str, List[str]] , __UpperCAmelCase : int = 512 , __UpperCAmelCase : int = 512 , __UpperCAmelCase : int = 50 , __UpperCAmelCase : float = 7.5 , __UpperCAmelCase : Optional[Union[str, List[str]]] = None , __UpperCAmelCase : Optional[int] = 1 , __UpperCAmelCase : float = 0.0 , __UpperCAmelCase : Optional[torch.Generator] = None , __UpperCAmelCase : Optional[torch.FloatTensor] = None , __UpperCAmelCase : Optional[str] = "pil" , __UpperCAmelCase : bool = True , __UpperCAmelCase : Optional[Callable[[int, int, torch.FloatTensor], None]] = None , __UpperCAmelCase : int = 1 , **__UpperCAmelCase : Tuple , ) ->Tuple: """simple docstring""" return self.pipea( prompt=__UpperCAmelCase , height=__UpperCAmelCase , width=__UpperCAmelCase , num_inference_steps=__UpperCAmelCase , guidance_scale=__UpperCAmelCase , negative_prompt=__UpperCAmelCase , num_images_per_prompt=__UpperCAmelCase , eta=__UpperCAmelCase , generator=__UpperCAmelCase , latents=__UpperCAmelCase , output_type=__UpperCAmelCase , return_dict=__UpperCAmelCase , callback=__UpperCAmelCase , callback_steps=__UpperCAmelCase , **__UpperCAmelCase , ) @torch.no_grad() def __lowerCAmelCase ( self : Tuple , __UpperCAmelCase : Union[str, List[str]] , __UpperCAmelCase : int = 512 , __UpperCAmelCase : int = 512 , __UpperCAmelCase : int = 50 , __UpperCAmelCase : float = 7.5 , __UpperCAmelCase : Optional[Union[str, List[str]]] = None , __UpperCAmelCase : Optional[int] = 1 , __UpperCAmelCase : float = 0.0 , __UpperCAmelCase : Optional[torch.Generator] = None , __UpperCAmelCase : Optional[torch.FloatTensor] = None , __UpperCAmelCase : Optional[str] = "pil" , __UpperCAmelCase : bool = True , __UpperCAmelCase : Optional[Callable[[int, int, torch.FloatTensor], None]] = None , __UpperCAmelCase : int = 1 , **__UpperCAmelCase : List[str] , ) ->Tuple: """simple docstring""" return self.pipea( prompt=__UpperCAmelCase , height=__UpperCAmelCase , width=__UpperCAmelCase , num_inference_steps=__UpperCAmelCase , guidance_scale=__UpperCAmelCase , negative_prompt=__UpperCAmelCase , num_images_per_prompt=__UpperCAmelCase , eta=__UpperCAmelCase , generator=__UpperCAmelCase , latents=__UpperCAmelCase , output_type=__UpperCAmelCase , return_dict=__UpperCAmelCase , callback=__UpperCAmelCase , callback_steps=__UpperCAmelCase , **__UpperCAmelCase , ) @torch.no_grad() def __lowerCAmelCase ( self : List[str] , __UpperCAmelCase : Union[str, List[str]] , __UpperCAmelCase : int = 512 , __UpperCAmelCase : int = 512 , __UpperCAmelCase : int = 50 , __UpperCAmelCase : float = 7.5 , __UpperCAmelCase : Optional[Union[str, List[str]]] = None , __UpperCAmelCase : Optional[int] = 1 , __UpperCAmelCase : float = 0.0 , __UpperCAmelCase : Optional[torch.Generator] = None , __UpperCAmelCase : Optional[torch.FloatTensor] = None , __UpperCAmelCase : Optional[str] = "pil" , __UpperCAmelCase : bool = True , __UpperCAmelCase : Optional[Callable[[int, int, torch.FloatTensor], None]] = None , __UpperCAmelCase : int = 1 , **__UpperCAmelCase : int , ) ->Optional[int]: """simple docstring""" a = '''cuda''' if torch.cuda.is_available() else '''cpu''' self.to(__UpperCAmelCase ) # Checks if the height and width are divisible by 8 or not if height % 8 != 0 or width % 8 != 0: raise ValueError(F"""`height` and `width` must be divisible by 8 but are {height} and {width}.""" ) # Get first result from Stable Diffusion Checkpoint v1.1 a = self.textaimg_sda_a( prompt=__UpperCAmelCase , height=__UpperCAmelCase , width=__UpperCAmelCase , num_inference_steps=__UpperCAmelCase , guidance_scale=__UpperCAmelCase , negative_prompt=__UpperCAmelCase , num_images_per_prompt=__UpperCAmelCase , eta=__UpperCAmelCase , generator=__UpperCAmelCase , latents=__UpperCAmelCase , output_type=__UpperCAmelCase , return_dict=__UpperCAmelCase , callback=__UpperCAmelCase , callback_steps=__UpperCAmelCase , **__UpperCAmelCase , ) # Get first result from Stable Diffusion Checkpoint v1.2 a = self.textaimg_sda_a( prompt=__UpperCAmelCase , height=__UpperCAmelCase , width=__UpperCAmelCase , num_inference_steps=__UpperCAmelCase , guidance_scale=__UpperCAmelCase , negative_prompt=__UpperCAmelCase , num_images_per_prompt=__UpperCAmelCase , eta=__UpperCAmelCase , generator=__UpperCAmelCase , latents=__UpperCAmelCase , output_type=__UpperCAmelCase , return_dict=__UpperCAmelCase , callback=__UpperCAmelCase , callback_steps=__UpperCAmelCase , **__UpperCAmelCase , ) # Get first result from Stable Diffusion Checkpoint v1.3 a = self.textaimg_sda_a( prompt=__UpperCAmelCase , height=__UpperCAmelCase , width=__UpperCAmelCase , num_inference_steps=__UpperCAmelCase , guidance_scale=__UpperCAmelCase , negative_prompt=__UpperCAmelCase , num_images_per_prompt=__UpperCAmelCase , eta=__UpperCAmelCase , generator=__UpperCAmelCase , latents=__UpperCAmelCase , output_type=__UpperCAmelCase , return_dict=__UpperCAmelCase , callback=__UpperCAmelCase , callback_steps=__UpperCAmelCase , **__UpperCAmelCase , ) # Get first result from Stable Diffusion Checkpoint v1.4 a = self.textaimg_sda_a( prompt=__UpperCAmelCase , height=__UpperCAmelCase , width=__UpperCAmelCase , num_inference_steps=__UpperCAmelCase , guidance_scale=__UpperCAmelCase , negative_prompt=__UpperCAmelCase , num_images_per_prompt=__UpperCAmelCase , eta=__UpperCAmelCase , generator=__UpperCAmelCase , latents=__UpperCAmelCase , output_type=__UpperCAmelCase , return_dict=__UpperCAmelCase , callback=__UpperCAmelCase , callback_steps=__UpperCAmelCase , **__UpperCAmelCase , ) # Get all result images into a single list and pass it via StableDiffusionPipelineOutput for final result return StableDiffusionPipelineOutput([resa[0], resa[0], resa[0], resa[0]] )
0
import os import re import shutil import sys import tempfile import unittest import black lowercase__ : List[str] = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) sys.path.append(os.path.join(git_repo_path, '''utils''')) import check_copies # noqa: E402 # This is the reference code that will be used in the tests. # If BertLMPredictionHead is changed in modeling_bert.py, this code needs to be manually updated. lowercase__ : Dict = ''' def __init__(self, config): super().__init__() self.transform = BertPredictionHeadTransform(config) # The output weights are the same as the input embeddings, but there is # an output-only bias for each token. self.decoder = nn.Linear(config.hidden_size, config.vocab_size, bias=False) self.bias = nn.Parameter(torch.zeros(config.vocab_size)) # Need a link between the two variables so that the bias is correctly resized with `resize_token_embeddings` self.decoder.bias = self.bias def forward(self, hidden_states): hidden_states = self.transform(hidden_states) hidden_states = self.decoder(hidden_states) return hidden_states ''' class lowercase_ ( unittest.TestCase ): """simple docstring""" def SCREAMING_SNAKE_CASE_ ( self ) ->Dict: lowerCAmelCase = tempfile.mkdtemp() os.makedirs(os.path.join(self.transformer_dir , '''models/bert/''' ) ) lowerCAmelCase = self.transformer_dir shutil.copy( os.path.join(__SCREAMING_SNAKE_CASE , '''src/transformers/models/bert/modeling_bert.py''' ) , os.path.join(self.transformer_dir , '''models/bert/modeling_bert.py''' ) , ) def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[Any]: lowerCAmelCase = '''src/transformers''' shutil.rmtree(self.transformer_dir ) def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None ) ->Union[str, Any]: lowerCAmelCase = comment + F"\nclass {class_name}(nn.Module):\n" + class_code if overwrite_result is not None: lowerCAmelCase = comment + F"\nclass {class_name}(nn.Module):\n" + overwrite_result lowerCAmelCase = black.Mode(target_versions={black.TargetVersion.PYaa} , line_length=119 ) lowerCAmelCase = black.format_str(__SCREAMING_SNAKE_CASE , mode=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = os.path.join(self.transformer_dir , '''new_code.py''' ) with open(__SCREAMING_SNAKE_CASE , '''w''' , newline='''\n''' ) as f: f.write(__SCREAMING_SNAKE_CASE ) if overwrite_result is None: self.assertTrue(len(check_copies.is_copy_consistent(__SCREAMING_SNAKE_CASE ) ) == 0 ) else: check_copies.is_copy_consistent(f.name , overwrite=__SCREAMING_SNAKE_CASE ) with open(__SCREAMING_SNAKE_CASE , '''r''' ) as f: self.assertTrue(f.read() , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->int: lowerCAmelCase = check_copies.find_code_in_transformers('''models.bert.modeling_bert.BertLMPredictionHead''' ) self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->Tuple: # Base copy consistency self.check_copy_consistency( '''# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead''' , '''BertLMPredictionHead''' , REFERENCE_CODE + '''\n''' , ) # With no empty line at the end self.check_copy_consistency( '''# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead''' , '''BertLMPredictionHead''' , __SCREAMING_SNAKE_CASE , ) # Copy consistency with rename self.check_copy_consistency( '''# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->TestModel''' , '''TestModelLMPredictionHead''' , re.sub('''Bert''' , '''TestModel''' , __SCREAMING_SNAKE_CASE ) , ) # Copy consistency with a really long name lowerCAmelCase = '''TestModelWithAReallyLongNameBecauseSomePeopleLikeThatForSomeReason''' self.check_copy_consistency( F"# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->{long_class_name}" , F"{long_class_name}LMPredictionHead" , re.sub('''Bert''' , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) , ) # Copy consistency with overwrite self.check_copy_consistency( '''# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->TestModel''' , '''TestModelLMPredictionHead''' , __SCREAMING_SNAKE_CASE , overwrite_result=re.sub('''Bert''' , '''TestModel''' , __SCREAMING_SNAKE_CASE ) , ) def SCREAMING_SNAKE_CASE_ ( self ) ->Tuple: lowerCAmelCase = check_copies.LOCALIZED_READMES['''README_zh-hans.md'''] lowerCAmelCase = ( '''1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (from Google Research and the''' ''' Toyota Technological Institute at Chicago) released with the paper [ALBERT: A Lite BERT for''' ''' Self-supervised Learning of Language Representations](https://arxiv.org/abs/1909.11942), by Zhenzhong''' ''' Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut.\n1.''' ''' **[DistilBERT](https://huggingface.co/transformers/model_doc/distilbert.html)** (from HuggingFace),''' ''' released together with the paper [DistilBERT, a distilled version of BERT: smaller, faster, cheaper and''' ''' lighter](https://arxiv.org/abs/1910.01108) by Victor Sanh, Lysandre Debut and Thomas Wolf. The same''' ''' method has been applied to compress GPT2 into''' ''' [DistilGPT2](https://github.com/huggingface/transformers/tree/main/examples/distillation), RoBERTa into''' ''' [DistilRoBERTa](https://github.com/huggingface/transformers/tree/main/examples/distillation),''' ''' Multilingual BERT into''' ''' [DistilmBERT](https://github.com/huggingface/transformers/tree/main/examples/distillation) and a German''' ''' version of DistilBERT.\n1. **[ELECTRA](https://huggingface.co/transformers/model_doc/electra.html)**''' ''' (from Google Research/Stanford University) released with the paper [ELECTRA: Pre-training text encoders''' ''' as discriminators rather than generators](https://arxiv.org/abs/2003.10555) by Kevin Clark, Minh-Thang''' ''' Luong, Quoc V. Le, Christopher D. Manning.''' ) lowerCAmelCase = ( '''1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the''' ''' Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of''' ''' Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian''' ''' Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n''' ) lowerCAmelCase = ( '''1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the''' ''' Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of''' ''' Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian''' ''' Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n1.''' ''' **[DistilBERT](https://huggingface.co/transformers/model_doc/distilbert.html)** (来自 HuggingFace) 伴随论文''' ''' [DistilBERT, a distilled version of BERT: smaller, faster, cheaper and''' ''' lighter](https://arxiv.org/abs/1910.01108) 由 Victor Sanh, Lysandre Debut and Thomas Wolf 发布。 The same''' ''' method has been applied to compress GPT2 into''' ''' [DistilGPT2](https://github.com/huggingface/transformers/tree/main/examples/distillation), RoBERTa into''' ''' [DistilRoBERTa](https://github.com/huggingface/transformers/tree/main/examples/distillation),''' ''' Multilingual BERT into''' ''' [DistilmBERT](https://github.com/huggingface/transformers/tree/main/examples/distillation) and a German''' ''' version of DistilBERT.\n1. **[ELECTRA](https://huggingface.co/transformers/model_doc/electra.html)** (来自''' ''' Google Research/Stanford University) 伴随论文 [ELECTRA: Pre-training text encoders as discriminators rather''' ''' than generators](https://arxiv.org/abs/2003.10555) 由 Kevin Clark, Minh-Thang Luong, Quoc V. Le,''' ''' Christopher D. Manning 发布。\n''' ) lowerCAmelCase , lowerCAmelCase = check_copies.convert_to_localized_md( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , localized_readme['''format_model_list'''] ) self.assertFalse(__SCREAMING_SNAKE_CASE ) self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) lowerCAmelCase , lowerCAmelCase = check_copies.convert_to_localized_md( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , localized_readme['''format_model_list'''] ) # Check whether the number of models is equal to README.md after conversion. self.assertTrue(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = ( '''1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (from Google Research and the''' ''' Toyota Technological Institute at Chicago) released with the paper [ALBERT: A Lite BERT for''' ''' Self-supervised Learning of Language Representations](https://arxiv.org/abs/1909.11942), by Zhenzhong''' ''' Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut.''' ) lowerCAmelCase = ( '''1. **[ALBERT](https://huggingface.co/transformers/main/model_doc/albert.html)** (来自 Google Research and''' ''' the Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of''' ''' Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian''' ''' Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n''' ) lowerCAmelCase = ( '''1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the''' ''' Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of''' ''' Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian''' ''' Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n''' ) lowerCAmelCase , lowerCAmelCase = check_copies.convert_to_localized_md( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , localized_readme['''format_model_list'''] ) # Check if the model link is synchronized. self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE )
338
0
'''simple docstring''' from math import log from scipy.constants import Boltzmann, physical_constants SCREAMING_SNAKE_CASE_: Optional[int] =3_00 # TEMPERATURE (unit = K) def lowerCAmelCase_ ( snake_case_ : float , snake_case_ : float , snake_case_ : float , ) -> float: '''simple docstring''' if donor_conc <= 0: raise ValueError("Donor concentration should be positive" ) elif acceptor_conc <= 0: raise ValueError("Acceptor concentration should be positive" ) elif intrinsic_conc <= 0: raise ValueError("Intrinsic concentration should be positive" ) elif donor_conc <= intrinsic_conc: raise ValueError( "Donor concentration should be greater than intrinsic concentration" ) elif acceptor_conc <= intrinsic_conc: raise ValueError( "Acceptor concentration should be greater than intrinsic concentration" ) else: return ( Boltzmann * T * log((donor_conc * acceptor_conc) / intrinsic_conc**2 ) / physical_constants["electron volt"][0] ) if __name__ == "__main__": import doctest doctest.testmod()
1
import pytest from datasets.splits import SplitDict, SplitInfo from datasets.utils.py_utils import asdict @pytest.mark.parametrize( '''split_dict''' , [ SplitDict(), SplitDict({'''train''': SplitInfo(name='''train''' , num_bytes=1_3_3_7 , num_examples=4_2 , dataset_name='''my_dataset''' )} ), SplitDict({'''train''': SplitInfo(name='''train''' , num_bytes=1_3_3_7 , num_examples=4_2 )} ), SplitDict({'''train''': SplitInfo()} ), ] , ) def SCREAMING_SNAKE_CASE_ ( snake_case__ ) -> Union[str, Any]: lowerCAmelCase = split_dict._to_yaml_list() assert len(snake_case__ ) == len(snake_case__ ) lowerCAmelCase = SplitDict._from_yaml_list(snake_case__ ) for split_name, split_info in split_dict.items(): # dataset_name field is deprecated, and is therefore not part of the YAML dump lowerCAmelCase = None # the split name of split_dict takes over the name of the split info object lowerCAmelCase = split_name assert split_dict == reloaded @pytest.mark.parametrize( '''split_info''' , [SplitInfo(), SplitInfo(dataset_name=snake_case__ ), SplitInfo(dataset_name='''my_dataset''' )] ) def SCREAMING_SNAKE_CASE_ ( snake_case__ ) -> Optional[int]: # For backward compatibility, we need asdict(split_dict) to return split info dictrionaries with the "dataset_name" # field even if it's deprecated. This way old versionso of `datasets` can still reload dataset_infos.json files lowerCAmelCase = asdict(SplitDict({'''train''': split_info} ) ) assert "dataset_name" in split_dict_asdict["train"] assert split_dict_asdict["train"]["dataset_name"] == split_info.dataset_name
338
0
'''simple docstring''' import logging from dataclasses import dataclass, field from pathlib import Path from typing import Optional, Union from .generation.configuration_utils import GenerationConfig from .training_args import TrainingArguments from .utils import add_start_docstrings lowerCamelCase : Dict = logging.getLogger(__name__) @dataclass @add_start_docstrings(TrainingArguments.__doc__ ) class __lowerCAmelCase (lowercase_ ): '''simple docstring''' lowerCAmelCase__ : bool = field(default=lowercase_ , metadata={"""help""": """Whether to use SortishSampler or not."""} ) lowerCAmelCase__ : bool = field( default=lowercase_ , metadata={"""help""": """Whether to use generate to calculate generative metrics (ROUGE, BLEU)."""} ) lowerCAmelCase__ : Optional[int] = field( default=lowercase_ , metadata={ """help""": ( """The `max_length` to use on each evaluation loop when `predict_with_generate=True`. Will default """ """to the `max_length` value of the model configuration.""" ) } , ) lowerCAmelCase__ : Optional[int] = field( default=lowercase_ , metadata={ """help""": ( """The `num_beams` to use on each evaluation loop when `predict_with_generate=True`. Will default """ """to the `num_beams` value of the model configuration.""" ) } , ) lowerCAmelCase__ : Optional[Union[str, Path, GenerationConfig]] = field( default=lowercase_ , metadata={ """help""": """Model id, file path or url pointing to a GenerationConfig json file, to use during prediction.""" } , ) def UpperCamelCase__ (self : Dict ): '''simple docstring''' lowercase__ = super().to_dict() for k, v in d.items(): if isinstance(UpperCamelCase , UpperCamelCase ): lowercase__ = v.to_dict() return d
2
import unittest import numpy as np def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ = None , ) -> np.ndarray: lowerCAmelCase = np.shape(snake_case__ ) lowerCAmelCase = np.shape(snake_case__ ) lowerCAmelCase = np.shape(snake_case__ ) if shape_a[0] != shape_b[0]: lowerCAmelCase = ( '''Expected the same number of rows for A and B. ''' f"Instead found A of size {shape_a} and B of size {shape_b}" ) raise ValueError(snake_case__ ) if shape_b[1] != shape_c[1]: lowerCAmelCase = ( '''Expected the same number of columns for B and C. ''' f"Instead found B of size {shape_b} and C of size {shape_c}" ) raise ValueError(snake_case__ ) lowerCAmelCase = pseudo_inv if a_inv is None: try: lowerCAmelCase = np.linalg.inv(snake_case__ ) except np.linalg.LinAlgError: raise ValueError( '''Input matrix A is not invertible. Cannot compute Schur complement.''' ) return mat_c - mat_b.T @ a_inv @ mat_b class lowercase_ ( unittest.TestCase ): """simple docstring""" def SCREAMING_SNAKE_CASE_ ( self ) ->None: lowerCAmelCase = np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]] ) lowerCAmelCase = np.array([[0, 3], [3, 0], [2, 3]] ) lowerCAmelCase = np.array([[2, 1], [6, 3]] ) lowerCAmelCase = schur_complement(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) lowerCAmelCase = np.block([[a, b], [b.T, c]] ) lowerCAmelCase = np.linalg.det(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = np.linalg.det(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = np.linalg.det(__SCREAMING_SNAKE_CASE ) self.assertAlmostEqual(__SCREAMING_SNAKE_CASE , det_a * det_s ) def SCREAMING_SNAKE_CASE_ ( self ) ->None: lowerCAmelCase = np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]] ) lowerCAmelCase = np.array([[0, 3], [3, 0], [2, 3]] ) lowerCAmelCase = np.array([[2, 1], [6, 3]] ) with self.assertRaises(__SCREAMING_SNAKE_CASE ): schur_complement(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->None: lowerCAmelCase = np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]] ) lowerCAmelCase = np.array([[0, 3], [3, 0], [2, 3]] ) lowerCAmelCase = np.array([[2, 1, 3], [6, 3, 5]] ) with self.assertRaises(__SCREAMING_SNAKE_CASE ): schur_complement(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) if __name__ == "__main__": import doctest doctest.testmod() unittest.main()
338
0
'''simple docstring''' from math import factorial def lowerCAmelCase_ ( snake_case__ , snake_case__ , snake_case__ ): '''simple docstring''' if successes > trials: raise ValueError('''successes must be lower or equal to trials''' ) if trials < 0 or successes < 0: raise ValueError('''the function is defined for non-negative integers''' ) if not isinstance(snake_case__ , snake_case__ ) or not isinstance(snake_case__ , snake_case__ ): raise ValueError('''the function is defined for non-negative integers''' ) if not 0 < prob < 1: raise ValueError('''prob has to be in range of 1 - 0''' ) A : Any = (prob**successes) * ((1 - prob) ** (trials - successes)) # Calculate the binomial coefficient: n! / k!(n-k)! A : Tuple = float(factorial(snake_case__ ) ) coefficient /= factorial(snake_case__ ) * factorial(trials - successes ) return probability * coefficient if __name__ == "__main__": from doctest import testmod testmod() print('Probability of 2 successes out of 4 trails') print('with probability of 0.75 is:', end=' ') print(binomial_distribution(2, 4, 0.75))
3
import argparse import hashlib import os import urllib import warnings import torch from torch import nn from tqdm import tqdm from transformers import WhisperConfig, WhisperForConditionalGeneration lowercase__ : Any = { '''tiny.en''': '''https://openaipublic.azureedge.net/main/whisper/models/d3dd57d32accea0b295c96e26691aa14d8822fac7d9d27d5dc00b4ca2826dd03/tiny.en.pt''', '''tiny''': '''https://openaipublic.azureedge.net/main/whisper/models/65147644a518d12f04e32d6f3b26facc3f8dd46e5390956a9424a650c0ce22b9/tiny.pt''', '''base.en''': '''https://openaipublic.azureedge.net/main/whisper/models/25a8566e1d0c1e2231d1c762132cd20e0f96a85d16145c3a00adf5d1ac670ead/base.en.pt''', '''base''': '''https://openaipublic.azureedge.net/main/whisper/models/ed3a0b6b1c0edf879ad9b11b1af5a0e6ab5db9205f891f668f8b0e6c6326e34e/base.pt''', '''small.en''': '''https://openaipublic.azureedge.net/main/whisper/models/f953ad0fd29cacd07d5a9eda5624af0f6bcf2258be67c92b79389873d91e0872/small.en.pt''', '''small''': '''https://openaipublic.azureedge.net/main/whisper/models/9ecf779972d90ba49c06d968637d720dd632c55bbf19d441fb42bf17a411e794/small.pt''', '''medium.en''': '''https://openaipublic.azureedge.net/main/whisper/models/d7440d1dc186f76616474e0ff0b3b6b879abc9d1a4926b7adfa41db2d497ab4f/medium.en.pt''', '''medium''': '''https://openaipublic.azureedge.net/main/whisper/models/345ae4da62f9b3d59415adc60127b97c714f32e89e936602e85993674d08dcb1/medium.pt''', '''large''': '''https://openaipublic.azureedge.net/main/whisper/models/e4b87e7e0bf463eb8e6956e646f1e277e901512310def2c24bf0e11bd3c28e9a/large.pt''', '''large-v2''': '''https://openaipublic.azureedge.net/main/whisper/models/81f7c96c852ee8fc832187b0132e569d6c3065a3252ed18e56effd0b6a73e524/large-v2.pt''', } def SCREAMING_SNAKE_CASE_ ( snake_case__ ) -> str: lowerCAmelCase = ['''layers''', '''blocks'''] for k in ignore_keys: state_dict.pop(snake_case__ , snake_case__ ) lowercase__ : List[Any] = { '''blocks''': '''layers''', '''mlp.0''': '''fc1''', '''mlp.2''': '''fc2''', '''mlp_ln''': '''final_layer_norm''', '''.attn.query''': '''.self_attn.q_proj''', '''.attn.key''': '''.self_attn.k_proj''', '''.attn.value''': '''.self_attn.v_proj''', '''.attn_ln''': '''.self_attn_layer_norm''', '''.attn.out''': '''.self_attn.out_proj''', '''.cross_attn.query''': '''.encoder_attn.q_proj''', '''.cross_attn.key''': '''.encoder_attn.k_proj''', '''.cross_attn.value''': '''.encoder_attn.v_proj''', '''.cross_attn_ln''': '''.encoder_attn_layer_norm''', '''.cross_attn.out''': '''.encoder_attn.out_proj''', '''decoder.ln.''': '''decoder.layer_norm.''', '''encoder.ln.''': '''encoder.layer_norm.''', '''token_embedding''': '''embed_tokens''', '''encoder.positional_embedding''': '''encoder.embed_positions.weight''', '''decoder.positional_embedding''': '''decoder.embed_positions.weight''', '''ln_post''': '''layer_norm''', } def SCREAMING_SNAKE_CASE_ ( snake_case__ ) -> Union[str, Any]: lowerCAmelCase = list(s_dict.keys() ) for key in keys: lowerCAmelCase = key for k, v in WHISPER_MAPPING.items(): if k in key: lowerCAmelCase = new_key.replace(snake_case__ , snake_case__ ) print(f"{key} -> {new_key}" ) lowerCAmelCase = s_dict.pop(snake_case__ ) return s_dict def SCREAMING_SNAKE_CASE_ ( snake_case__ ) -> Union[str, Any]: lowerCAmelCase , lowerCAmelCase = emb.weight.shape lowerCAmelCase = nn.Linear(snake_case__ , snake_case__ , bias=snake_case__ ) lowerCAmelCase = emb.weight.data return lin_layer def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ ) -> bytes: os.makedirs(snake_case__ , exist_ok=snake_case__ ) lowerCAmelCase = os.path.basename(snake_case__ ) lowerCAmelCase = url.split('''/''' )[-2] lowerCAmelCase = os.path.join(snake_case__ , snake_case__ ) if os.path.exists(snake_case__ ) and not os.path.isfile(snake_case__ ): raise RuntimeError(f"{download_target} exists and is not a regular file" ) if os.path.isfile(snake_case__ ): lowerCAmelCase = open(snake_case__ , '''rb''' ).read() if hashlib.shaaaa(snake_case__ ).hexdigest() == expected_shaaaa: return model_bytes else: warnings.warn(f"{download_target} exists, but the SHA256 checksum does not match; re-downloading the file" ) with urllib.request.urlopen(snake_case__ ) as source, open(snake_case__ , '''wb''' ) as output: with tqdm( total=int(source.info().get('''Content-Length''' ) ) , ncols=8_0 , unit='''iB''' , unit_scale=snake_case__ , unit_divisor=1_0_2_4 ) as loop: while True: lowerCAmelCase = source.read(8_1_9_2 ) if not buffer: break output.write(snake_case__ ) loop.update(len(snake_case__ ) ) lowerCAmelCase = open(snake_case__ , '''rb''' ).read() if hashlib.shaaaa(snake_case__ ).hexdigest() != expected_shaaaa: raise RuntimeError( '''Model has been downloaded but the SHA256 checksum does not not match. Please retry loading the model.''' ) return model_bytes def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ ) -> str: if ".pt" not in checkpoint_path: lowerCAmelCase = _download(_MODELS[checkpoint_path] ) else: lowerCAmelCase = torch.load(snake_case__ , map_location='''cpu''' ) lowerCAmelCase = original_checkpoint['''dims'''] lowerCAmelCase = original_checkpoint['''model_state_dict'''] lowerCAmelCase = state_dict['''decoder.token_embedding.weight'''] remove_ignore_keys_(snake_case__ ) rename_keys(snake_case__ ) lowerCAmelCase = True lowerCAmelCase = state_dict['''decoder.layers.0.fc1.weight'''].shape[0] lowerCAmelCase = WhisperConfig( vocab_size=dimensions['''n_vocab'''] , encoder_ffn_dim=snake_case__ , decoder_ffn_dim=snake_case__ , num_mel_bins=dimensions['''n_mels'''] , d_model=dimensions['''n_audio_state'''] , max_target_positions=dimensions['''n_text_ctx'''] , encoder_layers=dimensions['''n_audio_layer'''] , encoder_attention_heads=dimensions['''n_audio_head'''] , decoder_layers=dimensions['''n_text_layer'''] , decoder_attention_heads=dimensions['''n_text_state'''] , max_source_positions=dimensions['''n_audio_ctx'''] , ) lowerCAmelCase = WhisperForConditionalGeneration(snake_case__ ) lowerCAmelCase , lowerCAmelCase = model.model.load_state_dict(snake_case__ , strict=snake_case__ ) if len(snake_case__ ) > 0 and not set(snake_case__ ) <= { "encoder.embed_positions.weights", "decoder.embed_positions.weights", }: raise ValueError( '''Only `encoder.embed_positions.weights` and `decoder.embed_positions.weights` are allowed to be missing,''' f" but all the following weights are missing {missing}" ) if tie_embeds: lowerCAmelCase = make_linear_from_emb(model.model.decoder.embed_tokens ) else: lowerCAmelCase = proj_out_weights model.save_pretrained(snake_case__ ) if __name__ == "__main__": lowercase__ : List[str] = argparse.ArgumentParser() # # Required parameters parser.add_argument('''--checkpoint_path''', type=str, help='''Patht to the downloaded checkpoints''') parser.add_argument('''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''') lowercase__ : int = parser.parse_args() convert_openai_whisper_to_tfms(args.checkpoint_path, args.pytorch_dump_folder_path)
338
0
'''simple docstring''' import logging import os from dataclasses import dataclass, field from typing import Dict, Optional import numpy as np from utils_multiple_choice import MultipleChoiceDataset, Split, processors import transformers from transformers import ( AutoConfig, AutoModelForMultipleChoice, AutoTokenizer, DataCollatorWithPadding, EvalPrediction, HfArgumentParser, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import is_main_process __snake_case =logging.getLogger(__name__) def a_ ( lowerCamelCase : List[Any] , lowerCamelCase : Dict ): return (preds == labels).mean() @dataclass class UpperCAmelCase_ : lowerCamelCase : str = field( metadata={'''help''': '''Path to pretrained model or model identifier from huggingface.co/models'''} ) lowerCamelCase : Optional[str] = field( default=__lowercase , metadata={'''help''': '''Pretrained config name or path if not the same as model_name'''} ) lowerCamelCase : Optional[str] = field( default=__lowercase , metadata={'''help''': '''Pretrained tokenizer name or path if not the same as model_name'''} ) lowerCamelCase : Optional[str] = field( default=__lowercase , metadata={'''help''': '''Where do you want to store the pretrained models downloaded from huggingface.co'''} , ) @dataclass class UpperCAmelCase_ : lowerCamelCase : str = field(metadata={'''help''': '''The name of the task to train on: ''' + ''', '''.join(processors.keys() )} ) lowerCamelCase : str = field(metadata={'''help''': '''Should contain the data files for the task.'''} ) lowerCamelCase : int = field( default=128 , metadata={ '''help''': ( '''The maximum total input sequence length after tokenization. Sequences longer ''' '''than this will be truncated, sequences shorter will be padded.''' ) } , ) lowerCamelCase : bool = field( default=__lowercase , metadata={'''help''': '''Overwrite the cached training and evaluation sets'''} ) def a_ ( ): # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. lowerCAmelCase = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) lowerCAmelCase , lowerCAmelCase , lowerCAmelCase = parser.parse_args_into_dataclasses() if ( os.path.exists(training_args.output_dir ) and os.listdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir ): raise ValueError( f'''Output directory ({training_args.output_dir}) already exists and is not empty. Use''' ' --overwrite_output_dir to overcome.' ) # Setup logging logging.basicConfig( format='%(asctime)s - %(levelname)s - %(name)s - %(message)s' , datefmt='%m/%d/%Y %H:%M:%S' , level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN , ) logger.warning( 'Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s' , training_args.local_rank , training_args.device , training_args.n_gpu , bool(training_args.local_rank != -1 ) , training_args.fpaa , ) # Set the verbosity to info of the Transformers logger (on main process only): if is_main_process(training_args.local_rank ): transformers.utils.logging.set_verbosity_info() transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() logger.info('Training/evaluation parameters %s' , lowerCamelCase ) # Set seed set_seed(training_args.seed ) try: lowerCAmelCase = processors[data_args.task_name]() lowerCAmelCase = processor.get_labels() lowerCAmelCase = len(lowerCamelCase ) except KeyError: raise ValueError('Task not found: %s' % (data_args.task_name) ) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. lowerCAmelCase = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , num_labels=lowerCamelCase , finetuning_task=data_args.task_name , cache_dir=model_args.cache_dir , ) lowerCAmelCase = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , ) lowerCAmelCase = AutoModelForMultipleChoice.from_pretrained( model_args.model_name_or_path , from_tf=bool('.ckpt' in model_args.model_name_or_path ) , config=lowerCamelCase , cache_dir=model_args.cache_dir , ) # Get datasets lowerCAmelCase = ( MultipleChoiceDataset( data_dir=data_args.data_dir , tokenizer=lowerCamelCase , task=data_args.task_name , max_seq_length=data_args.max_seq_length , overwrite_cache=data_args.overwrite_cache , mode=Split.train , ) if training_args.do_train else None ) lowerCAmelCase = ( MultipleChoiceDataset( data_dir=data_args.data_dir , tokenizer=lowerCamelCase , task=data_args.task_name , max_seq_length=data_args.max_seq_length , overwrite_cache=data_args.overwrite_cache , mode=Split.dev , ) if training_args.do_eval else None ) def compute_metrics(lowerCamelCase : EvalPrediction ) -> Dict: lowerCAmelCase = np.argmax(p.predictions , axis=1 ) return {"acc": simple_accuracy(lowerCamelCase , p.label_ids )} # Data collator lowerCAmelCase = DataCollatorWithPadding(lowerCamelCase , pad_to_multiple_of=8 ) if training_args.fpaa else None # Initialize our Trainer lowerCAmelCase = Trainer( model=lowerCamelCase , args=lowerCamelCase , train_dataset=lowerCamelCase , eval_dataset=lowerCamelCase , compute_metrics=lowerCamelCase , data_collator=lowerCamelCase , ) # Training if training_args.do_train: trainer.train( model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path ) else None ) trainer.save_model() # For convenience, we also re-save the tokenizer to the same directory, # so that you can share your model easily on huggingface.co/models =) if trainer.is_world_master(): tokenizer.save_pretrained(training_args.output_dir ) # Evaluation lowerCAmelCase = {} if training_args.do_eval: logger.info('*** Evaluate ***' ) lowerCAmelCase = trainer.evaluate() lowerCAmelCase = os.path.join(training_args.output_dir , 'eval_results.txt' ) if trainer.is_world_master(): with open(lowerCamelCase , 'w' ) as writer: logger.info('***** Eval results *****' ) for key, value in result.items(): logger.info(' %s = %s' , lowerCamelCase , lowerCamelCase ) writer.write('%s = %s\n' % (key, value) ) results.update(lowerCamelCase ) return results def a_ ( lowerCamelCase : Dict ): # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
4
from ...processing_utils import ProcessorMixin class lowercase_ ( UpperCamelCase_ ): """simple docstring""" UpperCAmelCase_ : List[Any] = ["""image_processor""", """feature_extractor"""] UpperCAmelCase_ : Optional[int] = """TvltImageProcessor""" UpperCAmelCase_ : Optional[int] = """TvltFeatureExtractor""" def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ->Optional[int]: super().__init__(image_processor=__SCREAMING_SNAKE_CASE , feature_extractor=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = image_processor lowerCAmelCase = feature_extractor def __call__( self , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=False , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) ->List[Any]: if images is None and audio is None: raise ValueError('''You need to specify either an `images` or `audio` input to process.''' ) lowerCAmelCase = None if images is not None: lowerCAmelCase = self.image_processor(__SCREAMING_SNAKE_CASE , mask_pixel=__SCREAMING_SNAKE_CASE , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) if images_mixed is not None: lowerCAmelCase = self.image_processor(__SCREAMING_SNAKE_CASE , is_mixed=__SCREAMING_SNAKE_CASE , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) if audio is not None: lowerCAmelCase = self.feature_extractor( __SCREAMING_SNAKE_CASE , *__SCREAMING_SNAKE_CASE , sampling_rate=__SCREAMING_SNAKE_CASE , mask_audio=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) lowerCAmelCase = {} if audio is not None: output_dict.update(__SCREAMING_SNAKE_CASE ) if images is not None: output_dict.update(__SCREAMING_SNAKE_CASE ) if images_mixed_dict is not None: output_dict.update(__SCREAMING_SNAKE_CASE ) return output_dict @property def SCREAMING_SNAKE_CASE_ ( self ) ->Any: lowerCAmelCase = self.image_processor.model_input_names lowerCAmelCase = self.feature_extractor.model_input_names return list(dict.fromkeys(image_processor_input_names + feature_extractor_input_names ) )
338
0
import logging from transformers import PretrainedConfig UpperCAmelCase__ = logging.getLogger(__name__) UpperCAmelCase__ = { '''bertabs-finetuned-cnndm''': '''https://huggingface.co/remi/bertabs-finetuned-cnndm-extractive-abstractive-summarization/resolve/main/config.json''', } class lowerCamelCase__ ( lowerCAmelCase): SCREAMING_SNAKE_CASE__ = '''bertabs''' def __init__(self , UpperCAmelCase=3_0_5_2_2 , UpperCAmelCase=5_1_2 , UpperCAmelCase=6 , UpperCAmelCase=5_1_2 , UpperCAmelCase=8 , UpperCAmelCase=5_1_2 , UpperCAmelCase=0.2 , UpperCAmelCase=6 , UpperCAmelCase=7_6_8 , UpperCAmelCase=8 , UpperCAmelCase=2_0_4_8 , UpperCAmelCase=0.2 , **UpperCAmelCase , ) -> str: super().__init__(**UpperCAmelCase ) _lowercase =vocab_size _lowercase =max_pos _lowercase =enc_layers _lowercase =enc_hidden_size _lowercase =enc_heads _lowercase =enc_ff_size _lowercase =enc_dropout _lowercase =dec_layers _lowercase =dec_hidden_size _lowercase =dec_heads _lowercase =dec_ff_size _lowercase =dec_dropout
5
def SCREAMING_SNAKE_CASE_ ( snake_case__ ) -> List[str]: lowerCAmelCase = len(snake_case__ ) for i in range(length - 1 ): lowerCAmelCase = i for k in range(i + 1 , snake_case__ ): if collection[k] < collection[least]: lowerCAmelCase = k if least != i: lowerCAmelCase , lowerCAmelCase = (collection[i], collection[least]) return collection if __name__ == "__main__": lowercase__ : Optional[int] = input('''Enter numbers separated by a comma:\n''').strip() lowercase__ : str = [int(item) for item in user_input.split(''',''')] print(selection_sort(unsorted))
338
0
import dataclasses import re import string from typing import Any, Dict, Iterator, List, Mapping, Optional, Sequence, Tuple import numpy as np from . import residue_constants A : Any = Mapping[str, np.ndarray] A : Any = Mapping[str, Any] # Is a nested dict. A : Union[str, Any] = 0.01 @dataclasses.dataclass(frozen=a ) class __A: snake_case_ = 42 # [num_res, num_atom_type, 3] # Amino-acid type for each residue represented as an integer between 0 and # 20, where 20 is 'X'. snake_case_ = 42 # [num_res] # Binary float mask to indicate presence of a particular atom. 1.0 if an atom # is present and 0.0 if not. This should be used for loss masking. snake_case_ = 42 # [num_res, num_atom_type] # Residue index as used in PDB. It is not necessarily continuous or 0-indexed. snake_case_ = 42 # [num_res] # B-factors, or temperature factors, of each residue (in sq. angstroms units), # representing the displacement of the residue from its ground truth mean # value. snake_case_ = 42 # [num_res, num_atom_type] # Chain indices for multi-chain predictions snake_case_ = None # Optional remark about the protein. Included as a comment in output PDB # files snake_case_ = None # Templates used to generate this protein (prediction-only) snake_case_ = None # Chain corresponding to each parent snake_case_ = None def __lowerCAmelCase ( a__ ) -> Protein: __a = R'''(\[[A-Z]+\]\n)''' __a = [tag.strip() for tag in re.split(a__ , a__ ) if len(a__ ) > 0] __a = zip(tags[0::2] , [l.split('''\n''' ) for l in tags[1::2]] ) __a = ["N", "CA", "C"] __a = None __a = None __a = None for g in groups: if "[PRIMARY]" == g[0]: __a = g[1][0].strip() for i in range(len(a__ ) ): if seq[i] not in residue_constants.restypes: __a = '''X''' # FIXME: strings are immutable __a = np.array( [residue_constants.restype_order.get(a__ , residue_constants.restype_num ) for res_symbol in seq] ) elif "[TERTIARY]" == g[0]: __a = [] for axis in range(3 ): tertiary.append(list(map(a__ , g[1][axis].split() ) ) ) __a = np.array(a__ ) __a = np.zeros((len(tertiary[0] ) // 3, residue_constants.atom_type_num, 3) ).astype(np.floataa ) for i, atom in enumerate(a__ ): __a = np.transpose(tertiary_np[:, i::3] ) atom_positions *= PICO_TO_ANGSTROM elif "[MASK]" == g[0]: __a = np.array(list(map({'''-''': 0, '''+''': 1}.get , g[1][0].strip() ) ) ) __a = np.zeros( ( len(a__ ), residue_constants.atom_type_num, ) ).astype(np.floataa ) for i, atom in enumerate(a__ ): __a = 1 atom_mask *= mask[..., None] assert aatype is not None return Protein( atom_positions=a__ , atom_mask=a__ , aatype=a__ , residue_index=np.arange(len(a__ ) ) , b_factors=a__ , ) def __lowerCAmelCase ( a__ , a__ = 0 ) -> List[str]: __a = [] __a = prot.remark if remark is not None: pdb_headers.append(F"""REMARK {remark}""" ) __a = prot.parents __a = prot.parents_chain_index if parents is not None and parents_chain_index is not None: __a = [p for i, p in zip(a__ , a__ ) if i == chain_id] if parents is None or len(a__ ) == 0: __a = ['''N/A'''] pdb_headers.append(F"""PARENT {' '.join(a__ )}""" ) return pdb_headers def __lowerCAmelCase ( a__ , a__ ) -> str: __a = [] __a = pdb_str.split('''\n''' ) __a = prot.remark if remark is not None: out_pdb_lines.append(F"""REMARK {remark}""" ) __a = 42 if prot.parents is not None and len(prot.parents ) > 0: __a = [] if prot.parents_chain_index is not None: __a = {} for p, i in zip(prot.parents , prot.parents_chain_index ): parent_dict.setdefault(str(a__ ) , [] ) parent_dict[str(a__ )].append(a__ ) __a = max([int(a__ ) for chain_idx in parent_dict] ) for i in range(max_idx + 1 ): __a = parent_dict.get(str(a__ ) , ['''N/A'''] ) parents_per_chain.append(a__ ) else: parents_per_chain.append(list(prot.parents ) ) else: __a = [['''N/A''']] def make_parent_line(a__ ) -> str: return F"""PARENT {' '.join(a__ )}""" out_pdb_lines.append(make_parent_line(parents_per_chain[0] ) ) __a = 0 for i, l in enumerate(a__ ): if "PARENT" not in l and "REMARK" not in l: out_pdb_lines.append(a__ ) if "TER" in l and "END" not in lines[i + 1]: chain_counter += 1 if not chain_counter >= len(a__ ): __a = parents_per_chain[chain_counter] else: __a = ['''N/A'''] out_pdb_lines.append(make_parent_line(a__ ) ) return "\n".join(a__ ) def __lowerCAmelCase ( a__ ) -> str: __a = residue_constants.restypes + ['''X'''] def res_atoa(a__ ) -> str: return residue_constants.restype_atoa.get(restypes[r] , '''UNK''' ) __a = residue_constants.atom_types __a = [] __a = prot.atom_mask __a = prot.aatype __a = prot.atom_positions __a = prot.residue_index.astype(np.intaa ) __a = prot.b_factors __a = prot.chain_index if np.any(aatype > residue_constants.restype_num ): raise ValueError('''Invalid aatypes.''' ) __a = get_pdb_headers(a__ ) if len(a__ ) > 0: pdb_lines.extend(a__ ) __a = aatype.shape[0] __a = 1 __a = 0 __a = string.ascii_uppercase __a = None # Add all atom sites. for i in range(a__ ): __a = res_atoa(aatype[i] ) for atom_name, pos, mask, b_factor in zip(a__ , atom_positions[i] , atom_mask[i] , b_factors[i] ): if mask < 0.5: continue __a = '''ATOM''' __a = atom_name if len(a__ ) == 4 else F""" {atom_name}""" __a = '''''' __a = '''''' __a = 1.00 __a = atom_name[0] # Protein supports only C, N, O, S, this works. __a = '''''' __a = '''A''' if chain_index is not None: __a = chain_tags[chain_index[i]] # PDB is a columnar format, every space matters here! __a = ( F"""{record_type:<6}{atom_index:>5} {name:<4}{alt_loc:>1}""" F"""{res_name_a:>3} {chain_tag:>1}""" F"""{residue_index[i]:>4}{insertion_code:>1} """ F"""{pos[0]:>8.3f}{pos[1]:>8.3f}{pos[2]:>8.3f}""" F"""{occupancy:>6.2f}{b_factor:>6.2f} """ F"""{element:>2}{charge:>2}""" ) pdb_lines.append(a__ ) atom_index += 1 __a = i == n - 1 if chain_index is not None: if i != n - 1 and chain_index[i + 1] != prev_chain_index: __a = True __a = chain_index[i + 1] if should_terminate: # Close the chain. __a = '''TER''' __a = ( F"""{chain_end:<6}{atom_index:>5} {res_atoa(aatype[i] ):>3} {chain_tag:>1}{residue_index[i]:>4}""" ) pdb_lines.append(a__ ) atom_index += 1 if i != n - 1: # "prev" is a misnomer here. This happens at the beginning of # each new chain. pdb_lines.extend(get_pdb_headers(a__ , a__ ) ) pdb_lines.append('''END''' ) pdb_lines.append('''''' ) return "\n".join(a__ ) def __lowerCAmelCase ( a__ ) -> np.ndarray: return residue_constants.STANDARD_ATOM_MASK[prot.aatype] def __lowerCAmelCase ( a__ , a__ , a__ = None , a__ = None , a__ = None , a__ = None , a__ = None , ) -> Protein: return Protein( aatype=features['''aatype'''] , atom_positions=result['''final_atom_positions'''] , atom_mask=result['''final_atom_mask'''] , residue_index=features['''residue_index'''] + 1 , b_factors=b_factors if b_factors is not None else np.zeros_like(result['''final_atom_mask'''] ) , chain_index=a__ , remark=a__ , parents=a__ , parents_chain_index=a__ , )
6
import unittest from transformers import EsmConfig, is_torch_available from transformers.testing_utils import TestCasePlus, require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers.models.esm.modeling_esmfold import EsmForProteinFolding class lowercase_ : """simple docstring""" def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=13 , __SCREAMING_SNAKE_CASE=7 , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=19 , __SCREAMING_SNAKE_CASE=32 , __SCREAMING_SNAKE_CASE=5 , __SCREAMING_SNAKE_CASE=4 , __SCREAMING_SNAKE_CASE=37 , __SCREAMING_SNAKE_CASE="gelu" , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=512 , __SCREAMING_SNAKE_CASE=16 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=0.0_2 , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=4 , __SCREAMING_SNAKE_CASE=None , ) ->Union[str, Any]: 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 SCREAMING_SNAKE_CASE_ ( self ) ->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 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, input_mask, sequence_labels, token_labels, choice_labels def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[int]: lowerCAmelCase = EsmConfig( vocab_size=33 , hidden_size=self.hidden_size , pad_token_id=1 , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , is_folding_model=__SCREAMING_SNAKE_CASE , esmfold_config={'''trunk''': {'''num_blocks''': 2}, '''fp16_esm''': False} , ) return config def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ->Tuple: lowerCAmelCase = EsmForProteinFolding(config=__SCREAMING_SNAKE_CASE ).float() model.to(__SCREAMING_SNAKE_CASE ) model.eval() lowerCAmelCase = model(__SCREAMING_SNAKE_CASE , attention_mask=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = model(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = model(__SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.positions.shape , (8, self.batch_size, self.seq_length, 14, 3) ) self.parent.assertEqual(result.angles.shape , (8, self.batch_size, self.seq_length, 7, 2) ) def SCREAMING_SNAKE_CASE_ ( self ) ->int: lowerCAmelCase = self.prepare_config_and_inputs() ( ( lowerCAmelCase ) , ( lowerCAmelCase ) , ( lowerCAmelCase ) , ( lowerCAmelCase ) , ( lowerCAmelCase ) , ( lowerCAmelCase ) , ) = config_and_inputs lowerCAmelCase = {'''input_ids''': input_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_torch class lowercase_ ( UpperCamelCase_ , UpperCamelCase_ , unittest.TestCase ): """simple docstring""" UpperCAmelCase_ : Optional[Any] = False UpperCAmelCase_ : Dict = (EsmForProteinFolding,) if is_torch_available() else () UpperCAmelCase_ : List[Any] = () UpperCAmelCase_ : Tuple = {} if is_torch_available() else {} UpperCAmelCase_ : List[str] = False def SCREAMING_SNAKE_CASE_ ( self ) ->Dict: lowerCAmelCase = EsmFoldModelTester(self ) lowerCAmelCase = ConfigTester(self , config_class=__SCREAMING_SNAKE_CASE , hidden_size=37 ) def SCREAMING_SNAKE_CASE_ ( self ) ->Any: self.config_tester.run_common_tests() def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[int]: lowerCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE ) @unittest.skip('''Does not support attention outputs''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->Tuple: pass @unittest.skip def SCREAMING_SNAKE_CASE_ ( self ) ->Any: pass @unittest.skip('''Esm does not support embedding resizing''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->List[str]: pass @unittest.skip('''Esm does not support embedding resizing''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->str: pass @unittest.skip('''ESMFold does not support passing input embeds!''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->List[Any]: pass @unittest.skip('''ESMFold does not support head pruning.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->str: pass @unittest.skip('''ESMFold does not support head pruning.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->Dict: pass @unittest.skip('''ESMFold does not support head pruning.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->List[str]: pass @unittest.skip('''ESMFold does not support head pruning.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->List[Any]: pass @unittest.skip('''ESMFold does not support head pruning.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[int]: pass @unittest.skip('''ESMFold does not output hidden states in the normal way.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->Tuple: pass @unittest.skip('''ESMfold does not output hidden states in the normal way.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->Dict: pass @unittest.skip('''ESMFold only has one output format.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->List[str]: pass @unittest.skip('''This test doesn\'t work for ESMFold and doesn\'t test core functionality''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->Dict: pass @unittest.skip('''ESMFold does not support input chunking.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->Union[str, Any]: pass @unittest.skip('''ESMFold doesn\'t respect you and it certainly doesn\'t respect your initialization arguments.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[Any]: pass @unittest.skip('''ESMFold doesn\'t support torchscript compilation.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->str: pass @unittest.skip('''ESMFold doesn\'t support torchscript compilation.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->Any: pass @unittest.skip('''ESMFold doesn\'t support torchscript compilation.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->str: pass @unittest.skip('''ESMFold doesn\'t support data parallel.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->str: pass @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[int]: pass @require_torch class lowercase_ ( UpperCamelCase_ ): """simple docstring""" @slow def SCREAMING_SNAKE_CASE_ ( self ) ->str: lowerCAmelCase = EsmForProteinFolding.from_pretrained('''facebook/esmfold_v1''' ).float() model.eval() lowerCAmelCase = torch.tensor([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]] ) lowerCAmelCase = model(__SCREAMING_SNAKE_CASE )['''positions'''] lowerCAmelCase = torch.tensor([2.5_8_2_8, 0.7_9_9_3, -1_0.9_3_3_4] , dtype=torch.floataa ) self.assertTrue(torch.allclose(position_outputs[0, 0, 0, 0] , __SCREAMING_SNAKE_CASE , atol=1e-4 ) )
338
0
import os import warnings from typing import List, Optional from ...tokenization_utils_base import BatchEncoding from ...utils import logging from .configuration_rag import RagConfig lowercase_ = logging.get_logger(__name__) class A : """simple docstring""" def __init__( self : Optional[int],lowercase_ : List[Any],lowercase_ : int )-> Tuple: '''simple docstring''' A__ = question_encoder A__ = generator A__ = self.question_encoder def snake_case__ ( self : Any,lowercase_ : Optional[int] )-> Dict: '''simple docstring''' if os.path.isfile(lowercase_ ): raise ValueError(F'Provided path ({save_directory}) should be a directory, not a file' ) os.makedirs(lowercase_,exist_ok=lowercase_ ) A__ = os.path.join(lowercase_,'question_encoder_tokenizer' ) A__ = os.path.join(lowercase_,'generator_tokenizer' ) self.question_encoder.save_pretrained(lowercase_ ) self.generator.save_pretrained(lowercase_ ) @classmethod def snake_case__ ( cls : Any,lowercase_ : Optional[Any],**lowercase_ : Union[str, Any] )-> Optional[Any]: '''simple docstring''' from ..auto.tokenization_auto import AutoTokenizer A__ = kwargs.pop('config',lowercase_ ) if config is None: A__ = RagConfig.from_pretrained(lowercase_ ) A__ = AutoTokenizer.from_pretrained( lowercase_,config=config.question_encoder,subfolder='question_encoder_tokenizer' ) A__ = AutoTokenizer.from_pretrained( lowercase_,config=config.generator,subfolder='generator_tokenizer' ) return cls(question_encoder=lowercase_,generator=lowercase_ ) def __call__( self : Any,*lowercase_ : Any,**lowercase_ : Optional[Any] )-> Dict: '''simple docstring''' return self.current_tokenizer(*lowercase_,**lowercase_ ) def snake_case__ ( self : str,*lowercase_ : Optional[int],**lowercase_ : Union[str, Any] )-> Any: '''simple docstring''' return self.generator.batch_decode(*lowercase_,**lowercase_ ) def snake_case__ ( self : Dict,*lowercase_ : Optional[Any],**lowercase_ : Dict )-> Union[str, Any]: '''simple docstring''' return self.generator.decode(*lowercase_,**lowercase_ ) def snake_case__ ( self : int )-> Optional[Any]: '''simple docstring''' A__ = self.question_encoder def snake_case__ ( self : Optional[Any] )-> str: '''simple docstring''' A__ = self.generator def snake_case__ ( self : int,lowercase_ : List[str],lowercase_ : Optional[List[str]] = None,lowercase_ : Optional[int] = None,lowercase_ : Optional[int] = None,lowercase_ : str = "longest",lowercase_ : str = None,lowercase_ : bool = True,**lowercase_ : Tuple,)-> BatchEncoding: '''simple docstring''' 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',lowercase_,) if max_length is None: A__ = self.current_tokenizer.model_max_length A__ = self( lowercase_,add_special_tokens=lowercase_,return_tensors=lowercase_,max_length=lowercase_,padding=lowercase_,truncation=lowercase_,**lowercase_,) if tgt_texts is None: return model_inputs # Process tgt_texts if max_target_length is None: A__ = self.current_tokenizer.model_max_length A__ = self( text_target=lowercase_,add_special_tokens=lowercase_,return_tensors=lowercase_,padding=lowercase_,max_length=lowercase_,truncation=lowercase_,**lowercase_,) A__ = labels['input_ids'] return model_inputs
7
import warnings from typing import List import numpy as np from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding from ...utils import is_flax_available, is_tf_available, is_torch_available class lowercase_ ( UpperCamelCase_ ): """simple docstring""" UpperCAmelCase_ : List[str] = ["""image_processor""", """tokenizer"""] UpperCAmelCase_ : int = """OwlViTImageProcessor""" UpperCAmelCase_ : Any = ("""CLIPTokenizer""", """CLIPTokenizerFast""") def __init__( self , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE ) ->Any: lowerCAmelCase = None if "feature_extractor" in kwargs: warnings.warn( '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`''' ''' instead.''' , __SCREAMING_SNAKE_CASE , ) lowerCAmelCase = kwargs.pop('''feature_extractor''' ) lowerCAmelCase = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError('''You need to specify an `image_processor`.''' ) if tokenizer is None: raise ValueError('''You need to specify a `tokenizer`.''' ) super().__init__(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def __call__( self , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE="max_length" , __SCREAMING_SNAKE_CASE="np" , **__SCREAMING_SNAKE_CASE ) ->int: if text is None and query_images is None and images is None: raise ValueError( '''You have to specify at least one text or query image or image. All three cannot be none.''' ) if text is not None: if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) or (isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) and not isinstance(text[0] , __SCREAMING_SNAKE_CASE )): lowerCAmelCase = [self.tokenizer(__SCREAMING_SNAKE_CASE , padding=__SCREAMING_SNAKE_CASE , return_tensors=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE )] elif isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) and isinstance(text[0] , __SCREAMING_SNAKE_CASE ): lowerCAmelCase = [] # Maximum number of queries across batch lowerCAmelCase = max([len(__SCREAMING_SNAKE_CASE ) for t in text] ) # Pad all batch samples to max number of text queries for t in text: if len(__SCREAMING_SNAKE_CASE ) != max_num_queries: lowerCAmelCase = t + [''' '''] * (max_num_queries - len(__SCREAMING_SNAKE_CASE )) lowerCAmelCase = self.tokenizer(__SCREAMING_SNAKE_CASE , padding=__SCREAMING_SNAKE_CASE , return_tensors=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) encodings.append(__SCREAMING_SNAKE_CASE ) else: raise TypeError('''Input text should be a string, a list of strings or a nested list of strings''' ) if return_tensors == "np": lowerCAmelCase = np.concatenate([encoding['''input_ids'''] for encoding in encodings] , axis=0 ) lowerCAmelCase = np.concatenate([encoding['''attention_mask'''] for encoding in encodings] , axis=0 ) elif return_tensors == "jax" and is_flax_available(): import jax.numpy as jnp lowerCAmelCase = jnp.concatenate([encoding['''input_ids'''] for encoding in encodings] , axis=0 ) lowerCAmelCase = jnp.concatenate([encoding['''attention_mask'''] for encoding in encodings] , axis=0 ) elif return_tensors == "pt" and is_torch_available(): import torch lowerCAmelCase = torch.cat([encoding['''input_ids'''] for encoding in encodings] , dim=0 ) lowerCAmelCase = torch.cat([encoding['''attention_mask'''] for encoding in encodings] , dim=0 ) elif return_tensors == "tf" and is_tf_available(): import tensorflow as tf lowerCAmelCase = tf.stack([encoding['''input_ids'''] for encoding in encodings] , axis=0 ) lowerCAmelCase = tf.stack([encoding['''attention_mask'''] for encoding in encodings] , axis=0 ) else: raise ValueError('''Target return tensor type could not be returned''' ) lowerCAmelCase = BatchEncoding() lowerCAmelCase = input_ids lowerCAmelCase = attention_mask if query_images is not None: lowerCAmelCase = BatchEncoding() lowerCAmelCase = self.image_processor( __SCREAMING_SNAKE_CASE , return_tensors=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).pixel_values lowerCAmelCase = query_pixel_values if images is not None: lowerCAmelCase = self.image_processor(__SCREAMING_SNAKE_CASE , return_tensors=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) if text is not None and images is not None: lowerCAmelCase = image_features.pixel_values return encoding elif query_images is not None and images is not None: lowerCAmelCase = image_features.pixel_values return encoding elif text is not None or query_images is not None: return encoding else: return BatchEncoding(data=dict(**__SCREAMING_SNAKE_CASE ) , tensor_type=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) ->Optional[int]: return self.image_processor.post_process(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) ->Any: return self.image_processor.post_process_object_detection(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) ->Tuple: return self.image_processor.post_process_image_guided_detection(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) ->str: return self.tokenizer.batch_decode(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) ->List[Any]: return self.tokenizer.decode(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) @property def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[int]: warnings.warn( '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.''' , __SCREAMING_SNAKE_CASE , ) return self.image_processor_class @property def SCREAMING_SNAKE_CASE_ ( self ) ->int: warnings.warn( '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.''' , __SCREAMING_SNAKE_CASE , ) return self.image_processor
338
0
import argparse import json import os import numpy as np import PIL import requests import tensorflow.keras.applications.efficientnet as efficientnet import torch from huggingface_hub import hf_hub_download from PIL import Image from tensorflow.keras.preprocessing import image from transformers import ( EfficientNetConfig, EfficientNetForImageClassification, EfficientNetImageProcessor, ) from transformers.utils import logging logging.set_verbosity_info() lowerCAmelCase_ = logging.get_logger(__name__) lowerCAmelCase_ = { '''b0''': efficientnet.EfficientNetBa, '''b1''': efficientnet.EfficientNetBa, '''b2''': efficientnet.EfficientNetBa, '''b3''': efficientnet.EfficientNetBa, '''b4''': efficientnet.EfficientNetBa, '''b5''': efficientnet.EfficientNetBa, '''b6''': efficientnet.EfficientNetBa, '''b7''': efficientnet.EfficientNetBa, } lowerCAmelCase_ = { '''b0''': { '''hidden_dim''': 12_80, '''width_coef''': 1.0, '''depth_coef''': 1.0, '''image_size''': 2_24, '''dropout_rate''': 0.2, '''dw_padding''': [], }, '''b1''': { '''hidden_dim''': 12_80, '''width_coef''': 1.0, '''depth_coef''': 1.1, '''image_size''': 2_40, '''dropout_rate''': 0.2, '''dw_padding''': [16], }, '''b2''': { '''hidden_dim''': 14_08, '''width_coef''': 1.1, '''depth_coef''': 1.2, '''image_size''': 2_60, '''dropout_rate''': 0.3, '''dw_padding''': [5, 8, 16], }, '''b3''': { '''hidden_dim''': 15_36, '''width_coef''': 1.2, '''depth_coef''': 1.4, '''image_size''': 3_00, '''dropout_rate''': 0.3, '''dw_padding''': [5, 18], }, '''b4''': { '''hidden_dim''': 17_92, '''width_coef''': 1.4, '''depth_coef''': 1.8, '''image_size''': 3_80, '''dropout_rate''': 0.4, '''dw_padding''': [6], }, '''b5''': { '''hidden_dim''': 20_48, '''width_coef''': 1.6, '''depth_coef''': 2.2, '''image_size''': 4_56, '''dropout_rate''': 0.4, '''dw_padding''': [13, 27], }, '''b6''': { '''hidden_dim''': 23_04, '''width_coef''': 1.8, '''depth_coef''': 2.6, '''image_size''': 5_28, '''dropout_rate''': 0.5, '''dw_padding''': [31], }, '''b7''': { '''hidden_dim''': 25_60, '''width_coef''': 2.0, '''depth_coef''': 3.1, '''image_size''': 6_00, '''dropout_rate''': 0.5, '''dw_padding''': [18], }, } def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): snake_case_ = EfficientNetConfig() snake_case_ = CONFIG_MAP[model_name]['''hidden_dim'''] snake_case_ = CONFIG_MAP[model_name]['''width_coef'''] snake_case_ = CONFIG_MAP[model_name]['''depth_coef'''] snake_case_ = CONFIG_MAP[model_name]['''image_size'''] snake_case_ = CONFIG_MAP[model_name]['''dropout_rate'''] snake_case_ = CONFIG_MAP[model_name]['''dw_padding'''] snake_case_ = '''huggingface/label-files''' snake_case_ = '''imagenet-1k-id2label.json''' snake_case_ = 1000 snake_case_ = json.load(open(hf_hub_download(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , repo_type='''dataset''' ) , '''r''' ) ) snake_case_ = {int(SCREAMING_SNAKE_CASE__ ): v for k, v in idalabel.items()} snake_case_ = idalabel snake_case_ = {v: k for k, v in idalabel.items()} return config def __SCREAMING_SNAKE_CASE (): snake_case_ = '''http://images.cocodataset.org/val2017/000000039769.jpg''' snake_case_ = Image.open(requests.get(SCREAMING_SNAKE_CASE__ , stream=SCREAMING_SNAKE_CASE__ ).raw ) return im def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): snake_case_ = CONFIG_MAP[model_name]['''image_size'''] snake_case_ = EfficientNetImageProcessor( size={'''height''': size, '''width''': size} , image_mean=[0.485, 0.456, 0.406] , image_std=[0.4785_3944, 0.473_2864, 0.4743_4163] , do_center_crop=SCREAMING_SNAKE_CASE__ , ) return preprocessor def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): snake_case_ = [v.split('''_''' )[0].split('''block''' )[1] for v in original_param_names if v.startswith('''block''' )] snake_case_ = sorted(set(SCREAMING_SNAKE_CASE__ ) ) snake_case_ = len(SCREAMING_SNAKE_CASE__ ) snake_case_ = {b: str(SCREAMING_SNAKE_CASE__ ) for b, i in zip(SCREAMING_SNAKE_CASE__ , range(SCREAMING_SNAKE_CASE__ ) )} snake_case_ = [] rename_keys.append(('''stem_conv/kernel:0''', '''embeddings.convolution.weight''') ) rename_keys.append(('''stem_bn/gamma:0''', '''embeddings.batchnorm.weight''') ) rename_keys.append(('''stem_bn/beta:0''', '''embeddings.batchnorm.bias''') ) rename_keys.append(('''stem_bn/moving_mean:0''', '''embeddings.batchnorm.running_mean''') ) rename_keys.append(('''stem_bn/moving_variance:0''', '''embeddings.batchnorm.running_var''') ) for b in block_names: snake_case_ = block_name_mapping[b] rename_keys.append((F'''block{b}_expand_conv/kernel:0''', F'''encoder.blocks.{hf_b}.expansion.expand_conv.weight''') ) rename_keys.append((F'''block{b}_expand_bn/gamma:0''', F'''encoder.blocks.{hf_b}.expansion.expand_bn.weight''') ) rename_keys.append((F'''block{b}_expand_bn/beta:0''', F'''encoder.blocks.{hf_b}.expansion.expand_bn.bias''') ) rename_keys.append( (F'''block{b}_expand_bn/moving_mean:0''', F'''encoder.blocks.{hf_b}.expansion.expand_bn.running_mean''') ) rename_keys.append( (F'''block{b}_expand_bn/moving_variance:0''', F'''encoder.blocks.{hf_b}.expansion.expand_bn.running_var''') ) rename_keys.append( (F'''block{b}_dwconv/depthwise_kernel:0''', F'''encoder.blocks.{hf_b}.depthwise_conv.depthwise_conv.weight''') ) rename_keys.append((F'''block{b}_bn/gamma:0''', F'''encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.weight''') ) rename_keys.append((F'''block{b}_bn/beta:0''', F'''encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.bias''') ) rename_keys.append( (F'''block{b}_bn/moving_mean:0''', F'''encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.running_mean''') ) rename_keys.append( (F'''block{b}_bn/moving_variance:0''', F'''encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.running_var''') ) rename_keys.append((F'''block{b}_se_reduce/kernel:0''', F'''encoder.blocks.{hf_b}.squeeze_excite.reduce.weight''') ) rename_keys.append((F'''block{b}_se_reduce/bias:0''', F'''encoder.blocks.{hf_b}.squeeze_excite.reduce.bias''') ) rename_keys.append((F'''block{b}_se_expand/kernel:0''', F'''encoder.blocks.{hf_b}.squeeze_excite.expand.weight''') ) rename_keys.append((F'''block{b}_se_expand/bias:0''', F'''encoder.blocks.{hf_b}.squeeze_excite.expand.bias''') ) rename_keys.append( (F'''block{b}_project_conv/kernel:0''', F'''encoder.blocks.{hf_b}.projection.project_conv.weight''') ) rename_keys.append((F'''block{b}_project_bn/gamma:0''', F'''encoder.blocks.{hf_b}.projection.project_bn.weight''') ) rename_keys.append((F'''block{b}_project_bn/beta:0''', F'''encoder.blocks.{hf_b}.projection.project_bn.bias''') ) rename_keys.append( (F'''block{b}_project_bn/moving_mean:0''', F'''encoder.blocks.{hf_b}.projection.project_bn.running_mean''') ) rename_keys.append( (F'''block{b}_project_bn/moving_variance:0''', F'''encoder.blocks.{hf_b}.projection.project_bn.running_var''') ) rename_keys.append(('''top_conv/kernel:0''', '''encoder.top_conv.weight''') ) rename_keys.append(('''top_bn/gamma:0''', '''encoder.top_bn.weight''') ) rename_keys.append(('''top_bn/beta:0''', '''encoder.top_bn.bias''') ) rename_keys.append(('''top_bn/moving_mean:0''', '''encoder.top_bn.running_mean''') ) rename_keys.append(('''top_bn/moving_variance:0''', '''encoder.top_bn.running_var''') ) snake_case_ = {} for item in rename_keys: if item[0] in original_param_names: snake_case_ = '''efficientnet.''' + item[1] snake_case_ = '''classifier.weight''' snake_case_ = '''classifier.bias''' return key_mapping def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): for key, value in tf_params.items(): if "normalization" in key: continue snake_case_ = key_mapping[key] if "_conv" in key and "kernel" in key: snake_case_ = torch.from_numpy(SCREAMING_SNAKE_CASE__ ).permute(3 , 2 , 0 , 1 ) elif "depthwise_kernel" in key: snake_case_ = torch.from_numpy(SCREAMING_SNAKE_CASE__ ).permute(2 , 3 , 0 , 1 ) elif "kernel" in key: snake_case_ = torch.from_numpy(np.transpose(SCREAMING_SNAKE_CASE__ ) ) else: snake_case_ = torch.from_numpy(SCREAMING_SNAKE_CASE__ ) # Replace HF parameters with original TF model parameters assert hf_params[hf_key].shape == new_hf_value.shape hf_params[hf_key].copy_(SCREAMING_SNAKE_CASE__ ) @torch.no_grad() def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): snake_case_ = model_classes[model_name]( include_top=SCREAMING_SNAKE_CASE__ , weights='''imagenet''' , input_tensor=SCREAMING_SNAKE_CASE__ , input_shape=SCREAMING_SNAKE_CASE__ , pooling=SCREAMING_SNAKE_CASE__ , classes=1000 , classifier_activation='''softmax''' , ) snake_case_ = original_model.trainable_variables snake_case_ = original_model.non_trainable_variables snake_case_ = {param.name: param.numpy() for param in tf_params} for param in tf_non_train_params: snake_case_ = param.numpy() snake_case_ = list(tf_params.keys() ) # Load HuggingFace model snake_case_ = get_efficientnet_config(SCREAMING_SNAKE_CASE__ ) snake_case_ = EfficientNetForImageClassification(SCREAMING_SNAKE_CASE__ ).eval() snake_case_ = hf_model.state_dict() # Create src-to-dst parameter name mapping dictionary print('''Converting parameters...''' ) snake_case_ = rename_keys(SCREAMING_SNAKE_CASE__ ) replace_params(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) # Initialize preprocessor and preprocess input image snake_case_ = convert_image_processor(SCREAMING_SNAKE_CASE__ ) snake_case_ = preprocessor(images=prepare_img() , return_tensors='''pt''' ) # HF model inference hf_model.eval() with torch.no_grad(): snake_case_ = hf_model(**SCREAMING_SNAKE_CASE__ ) snake_case_ = outputs.logits.detach().numpy() # Original model inference snake_case_ = False snake_case_ = CONFIG_MAP[model_name]['''image_size'''] snake_case_ = prepare_img().resize((image_size, image_size) , resample=PIL.Image.NEAREST ) snake_case_ = image.img_to_array(SCREAMING_SNAKE_CASE__ ) snake_case_ = np.expand_dims(SCREAMING_SNAKE_CASE__ , axis=0 ) snake_case_ = original_model.predict(SCREAMING_SNAKE_CASE__ ) # Check whether original and HF model outputs match -> np.allclose assert np.allclose(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , atol=1E-3 ), "The predicted logits are not the same." print('''Model outputs match!''' ) if save_model: # Create folder to save model if not os.path.isdir(SCREAMING_SNAKE_CASE__ ): os.mkdir(SCREAMING_SNAKE_CASE__ ) # Save converted model and image processor hf_model.save_pretrained(SCREAMING_SNAKE_CASE__ ) preprocessor.save_pretrained(SCREAMING_SNAKE_CASE__ ) if push_to_hub: # Push model and image processor to hub print(F'''Pushing converted {model_name} to the hub...''' ) snake_case_ = F'''efficientnet-{model_name}''' preprocessor.push_to_hub(SCREAMING_SNAKE_CASE__ ) hf_model.push_to_hub(SCREAMING_SNAKE_CASE__ ) if __name__ == "__main__": lowerCAmelCase_ = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default='''b0''', type=str, help='''Version name of the EfficientNet model you want to convert, select from [b0, b1, b2, b3, b4, b5, b6, b7].''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default='''hf_model''', type=str, help='''Path to the output PyTorch model directory.''', ) parser.add_argument('''--save_model''', action='''store_true''', help='''Save model to local''') parser.add_argument('''--push_to_hub''', action='''store_true''', help='''Push model and image processor to the hub''') lowerCAmelCase_ = parser.parse_args() convert_efficientnet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.save_model, args.push_to_hub)
8
import os import unicodedata from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging lowercase__ : List[Any] = logging.get_logger(__name__) lowercase__ : Optional[Any] = {'''vocab_file''': '''spiece.model'''} lowercase__ : Optional[int] = { '''vocab_file''': { '''albert-base-v1''': '''https://huggingface.co/albert-base-v1/resolve/main/spiece.model''', '''albert-large-v1''': '''https://huggingface.co/albert-large-v1/resolve/main/spiece.model''', '''albert-xlarge-v1''': '''https://huggingface.co/albert-xlarge-v1/resolve/main/spiece.model''', '''albert-xxlarge-v1''': '''https://huggingface.co/albert-xxlarge-v1/resolve/main/spiece.model''', '''albert-base-v2''': '''https://huggingface.co/albert-base-v2/resolve/main/spiece.model''', '''albert-large-v2''': '''https://huggingface.co/albert-large-v2/resolve/main/spiece.model''', '''albert-xlarge-v2''': '''https://huggingface.co/albert-xlarge-v2/resolve/main/spiece.model''', '''albert-xxlarge-v2''': '''https://huggingface.co/albert-xxlarge-v2/resolve/main/spiece.model''', } } lowercase__ : Any = { '''albert-base-v1''': 5_1_2, '''albert-large-v1''': 5_1_2, '''albert-xlarge-v1''': 5_1_2, '''albert-xxlarge-v1''': 5_1_2, '''albert-base-v2''': 5_1_2, '''albert-large-v2''': 5_1_2, '''albert-xlarge-v2''': 5_1_2, '''albert-xxlarge-v2''': 5_1_2, } lowercase__ : Tuple = '''▁''' class lowercase_ ( UpperCamelCase_ ): """simple docstring""" UpperCAmelCase_ : Dict = VOCAB_FILES_NAMES UpperCAmelCase_ : Tuple = PRETRAINED_VOCAB_FILES_MAP UpperCAmelCase_ : Optional[int] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE="[CLS]" , __SCREAMING_SNAKE_CASE="[SEP]" , __SCREAMING_SNAKE_CASE="<unk>" , __SCREAMING_SNAKE_CASE="[SEP]" , __SCREAMING_SNAKE_CASE="<pad>" , __SCREAMING_SNAKE_CASE="[CLS]" , __SCREAMING_SNAKE_CASE="[MASK]" , __SCREAMING_SNAKE_CASE = None , **__SCREAMING_SNAKE_CASE , ) ->None: # Mask token behave like a normal word, i.e. include the space before it and # is included in the raw text, there should be a match in a non-normalized sentence. lowerCAmelCase = ( AddedToken(__SCREAMING_SNAKE_CASE , lstrip=__SCREAMING_SNAKE_CASE , rstrip=__SCREAMING_SNAKE_CASE , normalized=__SCREAMING_SNAKE_CASE ) if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else mask_token ) lowerCAmelCase = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( do_lower_case=__SCREAMING_SNAKE_CASE , remove_space=__SCREAMING_SNAKE_CASE , keep_accents=__SCREAMING_SNAKE_CASE , bos_token=__SCREAMING_SNAKE_CASE , eos_token=__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 , sp_model_kwargs=self.sp_model_kwargs , **__SCREAMING_SNAKE_CASE , ) lowerCAmelCase = do_lower_case lowerCAmelCase = remove_space lowerCAmelCase = keep_accents lowerCAmelCase = vocab_file lowerCAmelCase = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(__SCREAMING_SNAKE_CASE ) @property def SCREAMING_SNAKE_CASE_ ( self ) ->Any: return len(self.sp_model ) def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[Any]: 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 , __SCREAMING_SNAKE_CASE ) ->Tuple: lowerCAmelCase = d # for backward compatibility if not hasattr(self , '''sp_model_kwargs''' ): lowerCAmelCase = {} lowerCAmelCase = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE ) ->Any: if self.remove_space: lowerCAmelCase = ''' '''.join(inputs.strip().split() ) else: lowerCAmelCase = inputs lowerCAmelCase = outputs.replace('''``''' , '''"''' ).replace('''\'\'''' , '''"''' ) if not self.keep_accents: lowerCAmelCase = unicodedata.normalize('''NFKD''' , __SCREAMING_SNAKE_CASE ) lowerCAmelCase = ''''''.join([c for c in outputs if not unicodedata.combining(__SCREAMING_SNAKE_CASE )] ) if self.do_lower_case: lowerCAmelCase = outputs.lower() return outputs def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE ) ->List[str]: lowerCAmelCase = self.preprocess_text(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = self.sp_model.encode(__SCREAMING_SNAKE_CASE , out_type=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = [] for piece in pieces: if len(__SCREAMING_SNAKE_CASE ) > 1 and piece[-1] == str(''',''' ) and piece[-2].isdigit(): lowerCAmelCase = self.sp_model.EncodeAsPieces(piece[:-1].replace(__SCREAMING_SNAKE_CASE , '''''' ) ) if piece[0] != SPIECE_UNDERLINE and cur_pieces[0][0] == SPIECE_UNDERLINE: if len(cur_pieces[0] ) == 1: lowerCAmelCase = cur_pieces[1:] else: lowerCAmelCase = cur_pieces[0][1:] cur_pieces.append(piece[-1] ) new_pieces.extend(__SCREAMING_SNAKE_CASE ) else: new_pieces.append(__SCREAMING_SNAKE_CASE ) return new_pieces def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE ) ->int: return self.sp_model.PieceToId(__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE ) ->int: return self.sp_model.IdToPiece(__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE ) ->Optional[int]: lowerCAmelCase = [] lowerCAmelCase = '''''' lowerCAmelCase = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: if not prev_is_special: out_string += " " out_string += self.sp_model.decode(__SCREAMING_SNAKE_CASE ) + token lowerCAmelCase = True lowerCAmelCase = [] else: current_sub_tokens.append(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = False out_string += self.sp_model.decode(__SCREAMING_SNAKE_CASE ) return out_string.strip() def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ) ->List[int]: lowerCAmelCase = [self.sep_token_id] lowerCAmelCase = [self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = False ) ->List[int]: 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 not None: return [1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1] return [1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1] def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ) ->List[int]: 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 ) * [0] + len(token_ids_a + sep ) * [1] def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ) ->Tuple[str]: 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 ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , __SCREAMING_SNAKE_CASE ) elif not os.path.isfile(self.vocab_file ): with open(__SCREAMING_SNAKE_CASE , '''wb''' ) as fi: lowerCAmelCase = self.sp_model.serialized_model_proto() fi.write(__SCREAMING_SNAKE_CASE ) return (out_vocab_file,)
338
0
import numpy as np from cva import COLOR_BGR2GRAY, cvtColor, imread from numpy import array, uinta from PIL import Image from digital_image_processing import change_contrast as cc from digital_image_processing import convert_to_negative as cn from digital_image_processing import sepia as sp from digital_image_processing.dithering import burkes as bs from digital_image_processing.edge_detection import canny from digital_image_processing.filters import convolve as conv from digital_image_processing.filters import gaussian_filter as gg from digital_image_processing.filters import local_binary_pattern as lbp from digital_image_processing.filters import median_filter as med from digital_image_processing.filters import sobel_filter as sob from digital_image_processing.resize import resize as rs __lowerCAmelCase : Union[str, Any] =imread(r'digital_image_processing/image_data/lena_small.jpg') __lowerCAmelCase : List[Any] =cvtColor(img, COLOR_BGR2GRAY) def _UpperCamelCase ( ): __SCREAMING_SNAKE_CASE : Optional[Any] = cn.convert_to_negative(lowercase__ ) # assert negative_img array for at least one True assert negative_img.any() def _UpperCamelCase ( ): with Image.open('''digital_image_processing/image_data/lena_small.jpg''' ) as img: # Work around assertion for response assert str(cc.change_contrast(lowercase__ , 110 ) ).startswith( '''<PIL.Image.Image image mode=RGB size=100x100 at''' ) def _UpperCamelCase ( ): __SCREAMING_SNAKE_CASE : List[Any] = canny.gen_gaussian_kernel(9 , sigma=1.4 ) # Assert ambiguous array assert resp.all() def _UpperCamelCase ( ): __SCREAMING_SNAKE_CASE : str = imread('''digital_image_processing/image_data/lena_small.jpg''' , 0 ) # assert ambiguous array for all == True assert canny_img.all() __SCREAMING_SNAKE_CASE : Dict = canny.canny(lowercase__ ) # assert canny array for at least one True assert canny_array.any() def _UpperCamelCase ( ): assert gg.gaussian_filter(lowercase__ , 5 , sigma=0.9 ).all() def _UpperCamelCase ( ): # laplace diagonals __SCREAMING_SNAKE_CASE : List[Any] = array([[0.25, 0.5, 0.25], [0.5, -3, 0.5], [0.25, 0.5, 0.25]] ) __SCREAMING_SNAKE_CASE : Tuple = conv.img_convolve(lowercase__ , lowercase__ ).astype(lowercase__ ) assert res.any() def _UpperCamelCase ( ): assert med.median_filter(lowercase__ , 3 ).any() def _UpperCamelCase ( ): __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : str = sob.sobel_filter(lowercase__ ) assert grad.any() and theta.any() def _UpperCamelCase ( ): __SCREAMING_SNAKE_CASE : List[str] = sp.make_sepia(lowercase__ , 20 ) assert sepia.all() def _UpperCamelCase ( lowercase__ = "digital_image_processing/image_data/lena_small.jpg" ): __SCREAMING_SNAKE_CASE : List[str] = bs.Burkes(imread(lowercase__ , 1 ) , 120 ) burkes.process() assert burkes.output_img.any() def _UpperCamelCase ( lowercase__ = "digital_image_processing/image_data/lena_small.jpg" , ): __SCREAMING_SNAKE_CASE : Union[str, Any] = rs.NearestNeighbour(imread(lowercase__ , 1 ) , 400 , 200 ) nn.process() assert nn.output.any() def _UpperCamelCase ( ): __SCREAMING_SNAKE_CASE : Union[str, Any] = '''digital_image_processing/image_data/lena.jpg''' # Reading the image and converting it to grayscale. __SCREAMING_SNAKE_CASE : Dict = imread(lowercase__ , 0 ) # Test for get_neighbors_pixel function() return not None __SCREAMING_SNAKE_CASE : List[str] = 0 __SCREAMING_SNAKE_CASE : Optional[int] = 0 __SCREAMING_SNAKE_CASE : str = image[x_coordinate][y_coordinate] __SCREAMING_SNAKE_CASE : Any = lbp.get_neighbors_pixel( lowercase__ , lowercase__ , lowercase__ , lowercase__ ) assert neighbors_pixels is not None # Test for local_binary_pattern function() # Create a numpy array as the same height and width of read image __SCREAMING_SNAKE_CASE : Dict = np.zeros((image.shape[0], image.shape[1]) ) # Iterating through the image and calculating the local binary pattern value # for each pixel. for i in range(0 , image.shape[0] ): for j in range(0 , image.shape[1] ): __SCREAMING_SNAKE_CASE : Optional[Any] = lbp.local_binary_value(lowercase__ , lowercase__ , lowercase__ ) assert lbp_image.any()
9
import tempfile import torch from diffusers import ( DEISMultistepScheduler, DPMSolverMultistepScheduler, DPMSolverSinglestepScheduler, UniPCMultistepScheduler, ) from .test_schedulers import SchedulerCommonTest class lowercase_ ( UpperCamelCase_ ): """simple docstring""" UpperCAmelCase_ : List[Any] = (DEISMultistepScheduler,) UpperCAmelCase_ : int = (("""num_inference_steps""", 25),) def SCREAMING_SNAKE_CASE_ ( self , **__SCREAMING_SNAKE_CASE ) ->str: lowerCAmelCase = { '''num_train_timesteps''': 1000, '''beta_start''': 0.0_0_0_1, '''beta_end''': 0.0_2, '''beta_schedule''': '''linear''', '''solver_order''': 2, } config.update(**__SCREAMING_SNAKE_CASE ) return config def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE=0 , **__SCREAMING_SNAKE_CASE ) ->Tuple: lowerCAmelCase = dict(self.forward_default_kwargs ) lowerCAmelCase = kwargs.pop('''num_inference_steps''' , __SCREAMING_SNAKE_CASE ) lowerCAmelCase = self.dummy_sample lowerCAmelCase = 0.1 * sample lowerCAmelCase = [residual + 0.2, residual + 0.1_5, residual + 0.1_0] for scheduler_class in self.scheduler_classes: lowerCAmelCase = self.get_scheduler_config(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) # copy over dummy past residuals lowerCAmelCase = dummy_past_residuals[: scheduler.config.solver_order] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = scheduler_class.from_pretrained(__SCREAMING_SNAKE_CASE ) new_scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) # copy over dummy past residuals lowerCAmelCase = dummy_past_residuals[: new_scheduler.config.solver_order] lowerCAmelCase , lowerCAmelCase = sample, sample for t in range(__SCREAMING_SNAKE_CASE , time_step + scheduler.config.solver_order + 1 ): lowerCAmelCase = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).prev_sample lowerCAmelCase = new_scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1e-5, "Scheduler outputs are not identical" def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[int]: pass def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE=0 , **__SCREAMING_SNAKE_CASE ) ->List[Any]: lowerCAmelCase = dict(self.forward_default_kwargs ) lowerCAmelCase = kwargs.pop('''num_inference_steps''' , __SCREAMING_SNAKE_CASE ) lowerCAmelCase = self.dummy_sample lowerCAmelCase = 0.1 * sample lowerCAmelCase = [residual + 0.2, residual + 0.1_5, residual + 0.1_0] for scheduler_class in self.scheduler_classes: lowerCAmelCase = self.get_scheduler_config() lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) # copy over dummy past residuals (must be after setting timesteps) lowerCAmelCase = dummy_past_residuals[: scheduler.config.solver_order] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = scheduler_class.from_pretrained(__SCREAMING_SNAKE_CASE ) # copy over dummy past residuals new_scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) # copy over dummy past residual (must be after setting timesteps) lowerCAmelCase = dummy_past_residuals[: new_scheduler.config.solver_order] lowerCAmelCase = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).prev_sample lowerCAmelCase = new_scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1e-5, "Scheduler outputs are not identical" def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE ) ->List[Any]: if scheduler is None: lowerCAmelCase = self.scheduler_classes[0] lowerCAmelCase = self.get_scheduler_config(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = self.scheduler_classes[0] lowerCAmelCase = self.get_scheduler_config(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = 10 lowerCAmelCase = self.dummy_model() lowerCAmelCase = self.dummy_sample_deter scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) for i, t in enumerate(scheduler.timesteps ): lowerCAmelCase = model(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) lowerCAmelCase = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ).prev_sample return sample def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[int]: lowerCAmelCase = dict(self.forward_default_kwargs ) lowerCAmelCase = kwargs.pop('''num_inference_steps''' , __SCREAMING_SNAKE_CASE ) for scheduler_class in self.scheduler_classes: lowerCAmelCase = self.get_scheduler_config() lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = self.dummy_sample lowerCAmelCase = 0.1 * sample if num_inference_steps is not None and hasattr(__SCREAMING_SNAKE_CASE , '''set_timesteps''' ): scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) elif num_inference_steps is not None and not hasattr(__SCREAMING_SNAKE_CASE , '''set_timesteps''' ): lowerCAmelCase = num_inference_steps # copy over dummy past residuals (must be done after set_timesteps) lowerCAmelCase = [residual + 0.2, residual + 0.1_5, residual + 0.1_0] lowerCAmelCase = dummy_past_residuals[: scheduler.config.solver_order] lowerCAmelCase = scheduler.timesteps[5] lowerCAmelCase = scheduler.timesteps[6] lowerCAmelCase = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).prev_sample lowerCAmelCase = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).prev_sample self.assertEqual(output_a.shape , sample.shape ) self.assertEqual(output_a.shape , output_a.shape ) def SCREAMING_SNAKE_CASE_ ( self ) ->int: # make sure that iterating over schedulers with same config names gives same results # for defaults lowerCAmelCase = DEISMultistepScheduler(**self.get_scheduler_config() ) lowerCAmelCase = self.full_loop(scheduler=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = torch.mean(torch.abs(__SCREAMING_SNAKE_CASE ) ) assert abs(result_mean.item() - 0.2_3_9_1_6 ) < 1e-3 lowerCAmelCase = DPMSolverSinglestepScheduler.from_config(scheduler.config ) lowerCAmelCase = DPMSolverMultistepScheduler.from_config(scheduler.config ) lowerCAmelCase = UniPCMultistepScheduler.from_config(scheduler.config ) lowerCAmelCase = DEISMultistepScheduler.from_config(scheduler.config ) lowerCAmelCase = self.full_loop(scheduler=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = torch.mean(torch.abs(__SCREAMING_SNAKE_CASE ) ) assert abs(result_mean.item() - 0.2_3_9_1_6 ) < 1e-3 def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[int]: for timesteps in [25, 50, 100, 999, 1000]: self.check_over_configs(num_train_timesteps=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->int: self.check_over_configs(thresholding=__SCREAMING_SNAKE_CASE ) for order in [1, 2, 3]: for solver_type in ["logrho"]: for threshold in [0.5, 1.0, 2.0]: for prediction_type in ["epsilon", "sample"]: self.check_over_configs( thresholding=__SCREAMING_SNAKE_CASE , prediction_type=__SCREAMING_SNAKE_CASE , sample_max_value=__SCREAMING_SNAKE_CASE , algorithm_type='''deis''' , solver_order=__SCREAMING_SNAKE_CASE , solver_type=__SCREAMING_SNAKE_CASE , ) def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[Any]: for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->Union[str, Any]: for algorithm_type in ["deis"]: for solver_type in ["logrho"]: for order in [1, 2, 3]: for prediction_type in ["epsilon", "sample"]: self.check_over_configs( solver_order=__SCREAMING_SNAKE_CASE , solver_type=__SCREAMING_SNAKE_CASE , prediction_type=__SCREAMING_SNAKE_CASE , algorithm_type=__SCREAMING_SNAKE_CASE , ) lowerCAmelCase = self.full_loop( solver_order=__SCREAMING_SNAKE_CASE , solver_type=__SCREAMING_SNAKE_CASE , prediction_type=__SCREAMING_SNAKE_CASE , algorithm_type=__SCREAMING_SNAKE_CASE , ) assert not torch.isnan(__SCREAMING_SNAKE_CASE ).any(), "Samples have nan numbers" def SCREAMING_SNAKE_CASE_ ( self ) ->List[Any]: self.check_over_configs(lower_order_final=__SCREAMING_SNAKE_CASE ) self.check_over_configs(lower_order_final=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->List[Any]: for num_inference_steps in [1, 2, 3, 5, 10, 50, 100, 999, 1000]: self.check_over_forward(num_inference_steps=__SCREAMING_SNAKE_CASE , time_step=0 ) def SCREAMING_SNAKE_CASE_ ( self ) ->Dict: lowerCAmelCase = self.full_loop() lowerCAmelCase = torch.mean(torch.abs(__SCREAMING_SNAKE_CASE ) ) assert abs(result_mean.item() - 0.2_3_9_1_6 ) < 1e-3 def SCREAMING_SNAKE_CASE_ ( self ) ->List[str]: lowerCAmelCase = self.full_loop(prediction_type='''v_prediction''' ) lowerCAmelCase = torch.mean(torch.abs(__SCREAMING_SNAKE_CASE ) ) assert abs(result_mean.item() - 0.0_9_1 ) < 1e-3 def SCREAMING_SNAKE_CASE_ ( self ) ->Dict: lowerCAmelCase = self.scheduler_classes[0] lowerCAmelCase = self.get_scheduler_config(thresholding=__SCREAMING_SNAKE_CASE , dynamic_thresholding_ratio=0 ) lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = 10 lowerCAmelCase = self.dummy_model() lowerCAmelCase = self.dummy_sample_deter.half() scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) for i, t in enumerate(scheduler.timesteps ): lowerCAmelCase = model(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) lowerCAmelCase = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ).prev_sample assert sample.dtype == torch.floataa
338
0
from __future__ import annotations import unittest from transformers import RoFormerConfig, 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 ( TFRoFormerForCausalLM, TFRoFormerForMaskedLM, TFRoFormerForMultipleChoice, TFRoFormerForQuestionAnswering, TFRoFormerForSequenceClassification, TFRoFormerForTokenClassification, TFRoFormerModel, ) from transformers.models.roformer.modeling_tf_roformer import ( TFRoFormerSelfAttention, TFRoFormerSinusoidalPositionalEmbedding, ) class _SCREAMING_SNAKE_CASE : '''simple docstring''' def __init__(self : Dict , UpperCAmelCase_ : str , UpperCAmelCase_ : Dict=13 , UpperCAmelCase_ : Optional[int]=7 , UpperCAmelCase_ : List[Any]=True , UpperCAmelCase_ : List[str]=True , UpperCAmelCase_ : Tuple=True , UpperCAmelCase_ : Optional[Any]=True , UpperCAmelCase_ : Any=99 , UpperCAmelCase_ : List[Any]=32 , UpperCAmelCase_ : Optional[Any]=2 , UpperCAmelCase_ : Tuple=4 , UpperCAmelCase_ : Any=37 , UpperCAmelCase_ : str="gelu" , UpperCAmelCase_ : Union[str, Any]=0.1 , UpperCAmelCase_ : Optional[int]=0.1 , UpperCAmelCase_ : Optional[int]=512 , UpperCAmelCase_ : Any=16 , UpperCAmelCase_ : str=2 , UpperCAmelCase_ : Tuple=0.02 , UpperCAmelCase_ : Optional[int]=3 , UpperCAmelCase_ : Dict=4 , UpperCAmelCase_ : Tuple=None , ) ->Any: '''simple docstring''' lowerCamelCase__: Any =parent lowerCamelCase__: int =13 lowerCamelCase__: List[str] =7 lowerCamelCase__: int =True lowerCamelCase__: int =True lowerCamelCase__: Tuple =True lowerCamelCase__: List[str] =True lowerCamelCase__: Any =99 lowerCamelCase__: List[Any] =32 lowerCamelCase__: Union[str, Any] =2 lowerCamelCase__: List[str] =4 lowerCamelCase__: Optional[Any] =37 lowerCamelCase__: Any ="gelu" lowerCamelCase__: int =0.1 lowerCamelCase__: List[str] =0.1 lowerCamelCase__: str =512 lowerCamelCase__: Tuple =16 lowerCamelCase__: Optional[int] =2 lowerCamelCase__: List[Any] =0.02 lowerCamelCase__: str =3 lowerCamelCase__: int =4 lowerCamelCase__: int =None def SCREAMING_SNAKE_CASE_ (self : Any) ->int: '''simple docstring''' lowerCamelCase__: Dict =ids_tensor([self.batch_size, self.seq_length] , self.vocab_size) lowerCamelCase__: Optional[Any] =None if self.use_input_mask: lowerCamelCase__: Tuple =random_attention_mask([self.batch_size, self.seq_length]) lowerCamelCase__: Optional[Any] =None if self.use_token_type_ids: lowerCamelCase__: Dict =ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size) lowerCamelCase__: Dict =None lowerCamelCase__: Optional[Any] =None lowerCamelCase__: str =None if self.use_labels: lowerCamelCase__: Tuple =ids_tensor([self.batch_size] , self.type_sequence_label_size) lowerCamelCase__: Optional[Any] =ids_tensor([self.batch_size, self.seq_length] , self.num_labels) lowerCamelCase__: str =ids_tensor([self.batch_size] , self.num_choices) lowerCamelCase__: List[Any] =RoFormerConfig( 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=UpperCAmelCase_ , ) return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def SCREAMING_SNAKE_CASE_ (self : List[str] , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Dict , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : str , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : List[Any]) ->int: '''simple docstring''' lowerCamelCase__: List[Any] =TFRoFormerModel(config=UpperCAmelCase_) lowerCamelCase__: Union[str, Any] ={"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids} lowerCamelCase__: List[Any] =[input_ids, input_mask] lowerCamelCase__: Dict =model(UpperCAmelCase_) lowerCamelCase__: str =model(UpperCAmelCase_) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size)) def SCREAMING_SNAKE_CASE_ (self : Optional[Any] , UpperCAmelCase_ : Any , UpperCAmelCase_ : Dict , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : int , UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : Dict , UpperCAmelCase_ : Union[str, Any]) ->Dict: '''simple docstring''' lowerCamelCase__: Optional[Any] =True lowerCamelCase__: int =TFRoFormerForCausalLM(config=UpperCAmelCase_) lowerCamelCase__: Optional[int] ={ "input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids, } lowerCamelCase__: Any =model(UpperCAmelCase_)["logits"] self.parent.assertListEqual( list(prediction_scores.numpy().shape) , [self.batch_size, self.seq_length, self.vocab_size]) def SCREAMING_SNAKE_CASE_ (self : int , UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Dict , UpperCAmelCase_ : str , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Dict , UpperCAmelCase_ : List[str]) ->int: '''simple docstring''' lowerCamelCase__: Union[str, Any] =TFRoFormerForMaskedLM(config=UpperCAmelCase_) lowerCamelCase__: Tuple ={ "input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids, } lowerCamelCase__: Union[str, Any] =model(UpperCAmelCase_) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size)) def SCREAMING_SNAKE_CASE_ (self : Any , UpperCAmelCase_ : int , UpperCAmelCase_ : str , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Any , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : Tuple) ->List[Any]: '''simple docstring''' lowerCamelCase__: List[Any] =self.num_labels lowerCamelCase__: Tuple =TFRoFormerForSequenceClassification(config=UpperCAmelCase_) lowerCamelCase__: List[Any] ={ "input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids, } lowerCamelCase__: Tuple =model(UpperCAmelCase_) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels)) def SCREAMING_SNAKE_CASE_ (self : Any , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : int , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : int) ->List[str]: '''simple docstring''' lowerCamelCase__: Dict =self.num_choices lowerCamelCase__: List[Any] =TFRoFormerForMultipleChoice(config=UpperCAmelCase_) lowerCamelCase__: List[Any] =tf.tile(tf.expand_dims(UpperCAmelCase_ , 1) , (1, self.num_choices, 1)) lowerCamelCase__: List[Any] =tf.tile(tf.expand_dims(UpperCAmelCase_ , 1) , (1, self.num_choices, 1)) lowerCamelCase__: int =tf.tile(tf.expand_dims(UpperCAmelCase_ , 1) , (1, self.num_choices, 1)) lowerCamelCase__: List[Any] ={ "input_ids": multiple_choice_inputs_ids, "attention_mask": multiple_choice_input_mask, "token_type_ids": multiple_choice_token_type_ids, } lowerCamelCase__: Tuple =model(UpperCAmelCase_) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices)) def SCREAMING_SNAKE_CASE_ (self : Tuple , UpperCAmelCase_ : Any , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : str , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : str , UpperCAmelCase_ : str , UpperCAmelCase_ : List[Any]) ->List[Any]: '''simple docstring''' lowerCamelCase__: List[str] =self.num_labels lowerCamelCase__: Optional[int] =TFRoFormerForTokenClassification(config=UpperCAmelCase_) lowerCamelCase__: Dict ={ "input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids, } lowerCamelCase__: Union[str, Any] =model(UpperCAmelCase_) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels)) def SCREAMING_SNAKE_CASE_ (self : List[str] , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : int , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : str , UpperCAmelCase_ : int , UpperCAmelCase_ : int) ->str: '''simple docstring''' lowerCamelCase__: Dict =TFRoFormerForQuestionAnswering(config=UpperCAmelCase_) lowerCamelCase__: Optional[int] ={ "input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids, } lowerCamelCase__: Optional[Any] =model(UpperCAmelCase_) 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 SCREAMING_SNAKE_CASE_ (self : int) ->Tuple: '''simple docstring''' lowerCamelCase__: Optional[Any] =self.prepare_config_and_inputs() ( ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ): int =config_and_inputs lowerCamelCase__: Optional[Any] ={"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask} return config, inputs_dict @require_tf class _SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' lowercase_ = ( ( TFRoFormerModel, TFRoFormerForCausalLM, TFRoFormerForMaskedLM, TFRoFormerForQuestionAnswering, TFRoFormerForSequenceClassification, TFRoFormerForTokenClassification, TFRoFormerForMultipleChoice, ) if is_tf_available() else () ) lowercase_ = ( { "feature-extraction": TFRoFormerModel, "fill-mask": TFRoFormerForMaskedLM, "question-answering": TFRoFormerForQuestionAnswering, "text-classification": TFRoFormerForSequenceClassification, "text-generation": TFRoFormerForCausalLM, "token-classification": TFRoFormerForTokenClassification, "zero-shot": TFRoFormerForSequenceClassification, } if is_tf_available() else {} ) lowercase_ = False lowercase_ = False def SCREAMING_SNAKE_CASE_ (self : List[Any] , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : str , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : int) ->str: '''simple docstring''' if pipeline_test_casse_name == "TextGenerationPipelineTests": return True return False def SCREAMING_SNAKE_CASE_ (self : Optional[int]) ->int: '''simple docstring''' lowerCamelCase__: int =TFRoFormerModelTester(self) lowerCamelCase__: Any =ConfigTester(self , config_class=UpperCAmelCase_ , hidden_size=37) def SCREAMING_SNAKE_CASE_ (self : Optional[Any]) ->Any: '''simple docstring''' self.config_tester.run_common_tests() def SCREAMING_SNAKE_CASE_ (self : List[str]) ->Dict: '''simple docstring''' lowerCamelCase__: Dict =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ (self : int) ->Optional[int]: '''simple docstring''' lowerCamelCase__: int =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ (self : str) ->List[str]: '''simple docstring''' lowerCamelCase__: List[str] =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_lm_head(*UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ (self : List[str]) ->str: '''simple docstring''' lowerCamelCase__: Optional[int] =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_multiple_choice(*UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ (self : Any) ->str: '''simple docstring''' lowerCamelCase__: int =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ (self : Tuple) ->Dict: '''simple docstring''' lowerCamelCase__: List[str] =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*UpperCAmelCase_) def SCREAMING_SNAKE_CASE_ (self : List[str]) ->Dict: '''simple docstring''' lowerCamelCase__: List[str] =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*UpperCAmelCase_) @slow def SCREAMING_SNAKE_CASE_ (self : Union[str, Any]) ->Union[str, Any]: '''simple docstring''' lowerCamelCase__: Dict =TFRoFormerModel.from_pretrained("junnyu/roformer_chinese_base") self.assertIsNotNone(UpperCAmelCase_) @require_tf class _SCREAMING_SNAKE_CASE ( unittest.TestCase ): '''simple docstring''' @slow def SCREAMING_SNAKE_CASE_ (self : Tuple) ->Union[str, Any]: '''simple docstring''' lowerCamelCase__: List[str] =TFRoFormerForMaskedLM.from_pretrained("junnyu/roformer_chinese_base") lowerCamelCase__: Union[str, Any] =tf.constant([[0, 1, 2, 3, 4, 5]]) lowerCamelCase__: int =model(UpperCAmelCase_)[0] # TODO Replace vocab size lowerCamelCase__: str =50_000 lowerCamelCase__: int =[1, 6, vocab_size] self.assertEqual(output.shape , UpperCAmelCase_) print(output[:, :3, :3]) # TODO Replace values below with what was printed above. lowerCamelCase__: List[Any] =tf.constant( [ [ [-0.1205_3341, -1.026_4901, 0.2922_1946], [-1.513_3783, 0.19_7433, 0.1519_0607], [-5.013_5403, -3.90_0256, -0.8403_8764], ] ]) tf.debugging.assert_near(output[:, :3, :3] , UpperCAmelCase_ , atol=1E-4) @require_tf class _SCREAMING_SNAKE_CASE ( unittest.TestCase ): '''simple docstring''' lowercase_ = 1E-4 def SCREAMING_SNAKE_CASE_ (self : List[str]) ->str: '''simple docstring''' lowerCamelCase__: Optional[Any] =tf.constant([[4, 10]]) lowerCamelCase__: Any =TFRoFormerSinusoidalPositionalEmbedding(num_positions=6 , embedding_dim=6) lowerCamelCase__: Dict =emba(input_ids.shape) lowerCamelCase__: Dict =tf.constant( [[0.0000, 0.0000, 0.0000, 1.0000, 1.0000, 1.0000], [0.8415, 0.0464, 0.0022, 0.5403, 0.9989, 1.0000]]) tf.debugging.assert_near(UpperCAmelCase_ , UpperCAmelCase_ , atol=self.tolerance) def SCREAMING_SNAKE_CASE_ (self : str) ->int: '''simple docstring''' lowerCamelCase__: Dict =tf.constant( [ [0.0000, 0.0000, 0.0000, 0.0000, 0.0000], [0.8415, 0.8219, 0.8020, 0.7819, 0.7617], [0.9093, 0.9364, 0.9581, 0.9749, 0.9870], ]) lowerCamelCase__: List[str] =TFRoFormerSinusoidalPositionalEmbedding(num_positions=512 , embedding_dim=512) emba([2, 16, 512]) lowerCamelCase__: str =emba.weight[:3, :5] tf.debugging.assert_near(UpperCAmelCase_ , UpperCAmelCase_ , atol=self.tolerance) @require_tf class _SCREAMING_SNAKE_CASE ( unittest.TestCase ): '''simple docstring''' lowercase_ = 1E-4 def SCREAMING_SNAKE_CASE_ (self : Any) ->Optional[int]: '''simple docstring''' lowerCamelCase__: int =tf.reshape(tf.range(2 * 12 * 16 * 64 , dtype=tf.floataa) , shape=(2, 12, 16, 64)) / 100 lowerCamelCase__: Tuple =-tf.reshape(tf.range(2 * 12 * 16 * 64 , dtype=tf.floataa) , shape=(2, 12, 16, 64)) / 100 lowerCamelCase__: Any =TFRoFormerSinusoidalPositionalEmbedding(num_positions=32 , embedding_dim=64) lowerCamelCase__: int =embed_positions([2, 16, 768])[None, None, :, :] lowerCamelCase__ , lowerCamelCase__: int =TFRoFormerSelfAttention.apply_rotary_position_embeddings( UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_) lowerCamelCase__: Optional[int] =tf.constant( [ [0.0000, 0.0100, 0.0200, 0.0300, 0.0400, 0.0500, 0.0600, 0.0700], [-0.2012, 0.8897, 0.0263, 0.9401, 0.2074, 0.9463, 0.3481, 0.9343], [-1.7057, 0.6271, -1.2145, 1.3897, -0.6303, 1.7647, -0.1173, 1.8985], [-2.1731, -1.6397, -2.7358, 0.2854, -2.1840, 1.7183, -1.3018, 2.4871], [0.2717, -3.6173, -2.9206, -2.1988, -3.6638, 0.3858, -2.9155, 2.2980], [3.9859, -2.1580, -0.7984, -4.4904, -4.1181, -2.0252, -4.4782, 1.1253], ]) lowerCamelCase__: Union[str, Any] =tf.constant( [ [0.0000, -0.0100, -0.0200, -0.0300, -0.0400, -0.0500, -0.0600, -0.0700], [0.2012, -0.8897, -0.0263, -0.9401, -0.2074, -0.9463, -0.3481, -0.9343], [1.7057, -0.6271, 1.2145, -1.3897, 0.6303, -1.7647, 0.1173, -1.8985], [2.1731, 1.6397, 2.7358, -0.2854, 2.1840, -1.7183, 1.3018, -2.4871], [-0.2717, 3.6173, 2.9206, 2.1988, 3.6638, -0.3858, 2.9155, -2.2980], [-3.9859, 2.1580, 0.7984, 4.4904, 4.1181, 2.0252, 4.4782, -1.1253], ]) tf.debugging.assert_near(query_layer[0, 0, :6, :8] , UpperCAmelCase_ , atol=self.tolerance) tf.debugging.assert_near(key_layer[0, 0, :6, :8] , UpperCAmelCase_ , atol=self.tolerance)
10
import unittest import numpy as np import torch from diffusers import KarrasVePipeline, KarrasVeScheduler, UNetaDModel from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device enable_full_determinism() class lowercase_ ( unittest.TestCase ): """simple docstring""" @property def SCREAMING_SNAKE_CASE_ ( self ) ->List[str]: torch.manual_seed(0 ) lowerCAmelCase = UNetaDModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=3 , out_channels=3 , down_block_types=('''DownBlock2D''', '''AttnDownBlock2D''') , up_block_types=('''AttnUpBlock2D''', '''UpBlock2D''') , ) return model def SCREAMING_SNAKE_CASE_ ( self ) ->int: lowerCAmelCase = self.dummy_uncond_unet lowerCAmelCase = KarrasVeScheduler() lowerCAmelCase = KarrasVePipeline(unet=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = torch.manual_seed(0 ) lowerCAmelCase = pipe(num_inference_steps=2 , generator=__SCREAMING_SNAKE_CASE , output_type='''numpy''' ).images lowerCAmelCase = torch.manual_seed(0 ) lowerCAmelCase = pipe(num_inference_steps=2 , generator=__SCREAMING_SNAKE_CASE , output_type='''numpy''' , return_dict=__SCREAMING_SNAKE_CASE )[0] lowerCAmelCase = image[0, -3:, -3:, -1] lowerCAmelCase = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) lowerCAmelCase = np.array([0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 @slow @require_torch class lowercase_ ( unittest.TestCase ): """simple docstring""" def SCREAMING_SNAKE_CASE_ ( self ) ->List[Any]: lowerCAmelCase = '''google/ncsnpp-celebahq-256''' lowerCAmelCase = UNetaDModel.from_pretrained(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = KarrasVeScheduler() lowerCAmelCase = KarrasVePipeline(unet=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = torch.manual_seed(0 ) lowerCAmelCase = pipe(num_inference_steps=20 , generator=__SCREAMING_SNAKE_CASE , output_type='''numpy''' ).images lowerCAmelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 256, 256, 3) lowerCAmelCase = np.array([0.5_7_8, 0.5_8_1_1, 0.5_9_2_4, 0.5_8_0_9, 0.5_8_7, 0.5_8_8_6, 0.5_8_6_1, 0.5_8_0_2, 0.5_8_6] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
338
0
from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = { 'facebook/timesformer': 'https://huggingface.co/facebook/timesformer/resolve/main/config.json', } class lowerCAmelCase__ ( a): '''simple docstring''' __SCREAMING_SNAKE_CASE = "timesformer" def __init__( self , __lowerCamelCase=2_2_4 , __lowerCamelCase=1_6 , __lowerCamelCase=3 , __lowerCamelCase=8 , __lowerCamelCase=7_6_8 , __lowerCamelCase=1_2 , __lowerCamelCase=1_2 , __lowerCamelCase=3_0_7_2 , __lowerCamelCase="gelu" , __lowerCamelCase=0.0 , __lowerCamelCase=0.0 , __lowerCamelCase=0.0_2 , __lowerCamelCase=1e-6 , __lowerCamelCase=True , __lowerCamelCase="divided_space_time" , __lowerCamelCase=0 , **__lowerCamelCase , ) -> Tuple: super().__init__(**__lowerCamelCase) _A : Any = image_size _A : Union[str, Any] = patch_size _A : Tuple = num_channels _A : Dict = num_frames _A : int = hidden_size _A : Union[str, Any] = num_hidden_layers _A : List[Any] = num_attention_heads _A : Optional[int] = intermediate_size _A : Union[str, Any] = hidden_act _A : List[Any] = hidden_dropout_prob _A : Any = attention_probs_dropout_prob _A : Tuple = initializer_range _A : Tuple = layer_norm_eps _A : str = qkv_bias _A : str = attention_type _A : Dict = drop_path_rate
11
from typing import Dict import numpy as np from ..utils import add_end_docstrings, is_tf_available, is_torch_available, logging from .base import PIPELINE_INIT_ARGS, GenericTensor, Pipeline, PipelineException if is_tf_available(): import tensorflow as tf from ..tf_utils import stable_softmax if is_torch_available(): import torch lowercase__ : Dict = logging.get_logger(__name__) @add_end_docstrings( UpperCamelCase_ , r""" top_k (`int`, defaults to 5): The number of predictions to return. targets (`str` or `List[str]`, *optional*): When passed, the model will limit the scores to the passed targets instead of looking up in the whole vocab. If the provided targets are not in the model vocab, they will be tokenized and the first resulting token will be used (with a warning, and that might be slower). """ , ) class lowercase_ ( UpperCamelCase_ ): """simple docstring""" def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE ) ->np.ndarray: if self.framework == "tf": lowerCAmelCase = tf.where(input_ids == self.tokenizer.mask_token_id ).numpy() elif self.framework == "pt": lowerCAmelCase = torch.nonzero(input_ids == self.tokenizer.mask_token_id , as_tuple=__SCREAMING_SNAKE_CASE ) else: raise ValueError('''Unsupported framework''' ) return masked_index def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE ) ->np.ndarray: lowerCAmelCase = self.get_masked_index(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = np.prod(masked_index.shape ) if numel < 1: raise PipelineException( '''fill-mask''' , self.model.base_model_prefix , F"No mask_token ({self.tokenizer.mask_token}) found on the input" , ) def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE ) ->str: if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): for model_input in model_inputs: self._ensure_exactly_one_mask_token(model_input['''input_ids'''][0] ) else: for input_ids in model_inputs["input_ids"]: self._ensure_exactly_one_mask_token(__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE ) ->Dict[str, GenericTensor]: if return_tensors is None: lowerCAmelCase = self.framework lowerCAmelCase = self.tokenizer(__SCREAMING_SNAKE_CASE , return_tensors=__SCREAMING_SNAKE_CASE ) self.ensure_exactly_one_mask_token(__SCREAMING_SNAKE_CASE ) return model_inputs def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE ) ->Tuple: lowerCAmelCase = self.model(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = model_inputs['''input_ids'''] return model_outputs def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=5 , __SCREAMING_SNAKE_CASE=None ) ->str: # Cap top_k if there are targets if target_ids is not None and target_ids.shape[0] < top_k: lowerCAmelCase = target_ids.shape[0] lowerCAmelCase = model_outputs['''input_ids'''][0] lowerCAmelCase = model_outputs['''logits'''] if self.framework == "tf": lowerCAmelCase = tf.where(input_ids == self.tokenizer.mask_token_id ).numpy()[:, 0] lowerCAmelCase = outputs.numpy() lowerCAmelCase = outputs[0, masked_index, :] lowerCAmelCase = stable_softmax(__SCREAMING_SNAKE_CASE , axis=-1 ) if target_ids is not None: lowerCAmelCase = tf.gather_nd(tf.squeeze(__SCREAMING_SNAKE_CASE , 0 ) , target_ids.reshape(-1 , 1 ) ) lowerCAmelCase = tf.expand_dims(__SCREAMING_SNAKE_CASE , 0 ) lowerCAmelCase = tf.math.top_k(__SCREAMING_SNAKE_CASE , k=__SCREAMING_SNAKE_CASE ) lowerCAmelCase , lowerCAmelCase = topk.values.numpy(), topk.indices.numpy() else: lowerCAmelCase = torch.nonzero(input_ids == self.tokenizer.mask_token_id , as_tuple=__SCREAMING_SNAKE_CASE ).squeeze(-1 ) # Fill mask pipeline supports only one ${mask_token} per sample lowerCAmelCase = outputs[0, masked_index, :] lowerCAmelCase = logits.softmax(dim=-1 ) if target_ids is not None: lowerCAmelCase = probs[..., target_ids] lowerCAmelCase , lowerCAmelCase = probs.topk(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = [] lowerCAmelCase = values.shape[0] == 1 for i, (_values, _predictions) in enumerate(zip(values.tolist() , predictions.tolist() ) ): lowerCAmelCase = [] for v, p in zip(_values , _predictions ): # Copy is important since we're going to modify this array in place lowerCAmelCase = input_ids.numpy().copy() if target_ids is not None: lowerCAmelCase = target_ids[p].tolist() lowerCAmelCase = p # Filter padding out: lowerCAmelCase = tokens[np.where(tokens != self.tokenizer.pad_token_id )] # Originally we skip special tokens to give readable output. # For multi masks though, the other [MASK] would be removed otherwise # making the output look odd, so we add them back lowerCAmelCase = self.tokenizer.decode(__SCREAMING_SNAKE_CASE , skip_special_tokens=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = {'''score''': v, '''token''': p, '''token_str''': self.tokenizer.decode([p] ), '''sequence''': sequence} row.append(__SCREAMING_SNAKE_CASE ) result.append(__SCREAMING_SNAKE_CASE ) if single_mask: return result[0] return result def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None ) ->Optional[Any]: if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): lowerCAmelCase = [targets] try: lowerCAmelCase = self.tokenizer.get_vocab() except Exception: lowerCAmelCase = {} lowerCAmelCase = [] for target in targets: lowerCAmelCase = vocab.get(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) if id_ is None: lowerCAmelCase = self.tokenizer( __SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , return_attention_mask=__SCREAMING_SNAKE_CASE , return_token_type_ids=__SCREAMING_SNAKE_CASE , max_length=1 , truncation=__SCREAMING_SNAKE_CASE , )['''input_ids'''] if len(__SCREAMING_SNAKE_CASE ) == 0: logger.warning( F"The specified target token `{target}` does not exist in the model vocabulary. " '''We cannot replace it with anything meaningful, ignoring it''' ) continue lowerCAmelCase = input_ids[0] # XXX: If users encounter this pass # it becomes pretty slow, so let's make sure # The warning enables them to fix the input to # get faster performance. logger.warning( F"The specified target token `{target}` does not exist in the model vocabulary. " F"Replacing with `{self.tokenizer.convert_ids_to_tokens(id_ )}`." ) target_ids.append(id_ ) lowerCAmelCase = list(set(__SCREAMING_SNAKE_CASE ) ) if len(__SCREAMING_SNAKE_CASE ) == 0: raise ValueError('''At least one target must be provided when passed.''' ) lowerCAmelCase = np.array(__SCREAMING_SNAKE_CASE ) return target_ids def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None ) ->Dict: lowerCAmelCase = {} if targets is not None: lowerCAmelCase = self.get_target_ids(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) lowerCAmelCase = target_ids if top_k is not None: lowerCAmelCase = top_k if self.tokenizer.mask_token_id is None: raise PipelineException( '''fill-mask''' , self.model.base_model_prefix , '''The tokenizer does not define a `mask_token`.''' ) return {}, {}, postprocess_params def __call__( self , __SCREAMING_SNAKE_CASE , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) ->List[Any]: lowerCAmelCase = super().__call__(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) and len(__SCREAMING_SNAKE_CASE ) == 1: return outputs[0] return outputs
338
0
import functools def lowerCamelCase__ ( A__ : str , A__ : str ): '''simple docstring''' __lowerCamelCase = len(A__ ) __lowerCamelCase = len(A__ ) @functools.cache def min_distance(A__ : int , A__ : int ) -> int: # if first word index is overflow - delete all from the second word if indexa >= len_worda: return len_worda - indexa # if second word index is overflow - delete all from the first word if indexa >= len_worda: return len_worda - indexa __lowerCamelCase = int(worda[indexa] != worda[indexa] ) # current letters not identical return min( 1 + min_distance(indexa + 1 , A__ ) , 1 + min_distance(A__ , indexa + 1 ) , diff + min_distance(indexa + 1 , indexa + 1 ) , ) return min_distance(0 , 0 ) if __name__ == "__main__": import doctest doctest.testmod()
12
from typing import TYPE_CHECKING from ...utils import _LazyModule lowercase__ : int = {'''tokenization_wav2vec2_phoneme''': ['''Wav2Vec2PhonemeCTCTokenizer''']} if TYPE_CHECKING: from .tokenization_wavaveca_phoneme import WavaVecaPhonemeCTCTokenizer else: import sys lowercase__ : Optional[int] = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
338
0
def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ): # 1. Validate that path exists between current and next vertices if graph[path[curr_ind - 1]][next_ver] == 0: return False # 2. Validate that next vertex is not already in path return not any(vertex == next_ver for vertex in path ) def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ): # Base Case if curr_ind == len(_UpperCAmelCase ): # return whether path exists between current and starting vertices return graph[path[curr_ind - 1]][path[0]] == 1 # Recursive Step for next_ver in range(0 , len(_UpperCAmelCase ) ): if valid_connection(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ): # Insert current vertex into path as next transition SCREAMING_SNAKE_CASE_: Optional[int] = next_ver # Validate created path if util_hamilton_cycle(_UpperCAmelCase , _UpperCAmelCase , curr_ind + 1 ): return True # Backtrack SCREAMING_SNAKE_CASE_: Optional[int] = -1 return False def A_ ( _UpperCAmelCase , _UpperCAmelCase = 0 ): SCREAMING_SNAKE_CASE_: Dict = [-1] * (len(_UpperCAmelCase ) + 1) # initialize start and end of path with starting index SCREAMING_SNAKE_CASE_: int = start_index # evaluate and if we find answer return path either return empty array return path if util_hamilton_cycle(_UpperCAmelCase , _UpperCAmelCase , 1 ) else []
13
lowercase__ : Optional[int] = '''ABCDEFGHIJKLMNOPQRSTUVWXYZ''' def SCREAMING_SNAKE_CASE_ ( ) -> None: lowerCAmelCase = input('''Enter message: ''' ) lowerCAmelCase = input('''Enter key [alphanumeric]: ''' ) lowerCAmelCase = input('''Encrypt/Decrypt [e/d]: ''' ) if mode.lower().startswith('''e''' ): lowerCAmelCase = '''encrypt''' lowerCAmelCase = encrypt_message(snake_case__ , snake_case__ ) elif mode.lower().startswith('''d''' ): lowerCAmelCase = '''decrypt''' lowerCAmelCase = decrypt_message(snake_case__ , snake_case__ ) print(f"\n{mode.title()}ed message:" ) print(snake_case__ ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ ) -> str: return translate_message(snake_case__ , snake_case__ , '''encrypt''' ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ ) -> str: return translate_message(snake_case__ , snake_case__ , '''decrypt''' ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> str: lowerCAmelCase = [] lowerCAmelCase = 0 lowerCAmelCase = key.upper() for symbol in message: lowerCAmelCase = LETTERS.find(symbol.upper() ) if num != -1: if mode == "encrypt": num += LETTERS.find(key[key_index] ) elif mode == "decrypt": num -= LETTERS.find(key[key_index] ) num %= len(snake_case__ ) if symbol.isupper(): translated.append(LETTERS[num] ) elif symbol.islower(): translated.append(LETTERS[num].lower() ) key_index += 1 if key_index == len(snake_case__ ): lowerCAmelCase = 0 else: translated.append(snake_case__ ) return "".join(snake_case__ ) if __name__ == "__main__": main()
338
0
from __future__ import annotations from collections import deque from collections.abc import Iterator from dataclasses import dataclass @dataclass class UpperCamelCase_ : '''simple docstring''' UpperCAmelCase__ = 42 UpperCAmelCase__ = 42 class UpperCamelCase_ : '''simple docstring''' def __init__( self : str , UpperCAmelCase__ : int) ->str: '''simple docstring''' A__ = [[] for _ in range(UpperCAmelCase__)] A__ = size def __getitem__( self : List[Any] , UpperCAmelCase__ : int) ->Iterator[Edge]: '''simple docstring''' return iter(self._graph[vertex]) @property def SCREAMING_SNAKE_CASE ( self : str) ->Union[str, Any]: '''simple docstring''' return self._size def SCREAMING_SNAKE_CASE ( self : Tuple , UpperCAmelCase__ : int , UpperCAmelCase__ : int , UpperCAmelCase__ : int) ->Optional[Any]: '''simple docstring''' if weight not in (0, 1): raise ValueError('''Edge weight must be either 0 or 1.''') if to_vertex < 0 or to_vertex >= self.size: raise ValueError('''Vertex indexes must be in [0; size).''') self._graph[from_vertex].append(Edge(UpperCAmelCase__ , UpperCAmelCase__)) def SCREAMING_SNAKE_CASE ( self : List[Any] , UpperCAmelCase__ : int , UpperCAmelCase__ : int) ->int | None: '''simple docstring''' A__ = deque([start_vertex]) A__ = [None] * self.size A__ = 0 while queue: A__ = queue.popleft() A__ = distances[current_vertex] if current_distance is None: continue for edge in self[current_vertex]: A__ = current_distance + edge.weight A__ = distances[edge.destination_vertex] if ( isinstance(UpperCAmelCase__ , UpperCAmelCase__) and new_distance >= dest_vertex_distance ): continue A__ = new_distance if edge.weight == 0: queue.appendleft(edge.destination_vertex) else: queue.append(edge.destination_vertex) if distances[finish_vertex] is None: raise ValueError('''No path from start_vertex to finish_vertex.''') return distances[finish_vertex] if __name__ == "__main__": import doctest doctest.testmod()
14
from collections import defaultdict from math import ceil, sqrt def SCREAMING_SNAKE_CASE_ ( snake_case__ = 1_0_0_0_0_0_0 , snake_case__ = 1_0 ) -> int: lowerCAmelCase = defaultdict(snake_case__ ) for outer_width in range(3 , (t_limit // 4) + 2 ): if outer_width * outer_width > t_limit: lowerCAmelCase = max( ceil(sqrt(outer_width * outer_width - t_limit ) ) , 1 ) else: lowerCAmelCase = 1 hole_width_lower_bound += (outer_width - hole_width_lower_bound) % 2 for hole_width in range(snake_case__ , outer_width - 1 , 2 ): count[outer_width * outer_width - hole_width * hole_width] += 1 return sum(1 for n in count.values() if 1 <= n <= 1_0 ) if __name__ == "__main__": print(f'{solution() = }')
338
0
def UpperCAmelCase ( a_ = 1_0_0_0 ) -> int: """simple docstring""" return sum(2 * a * ((a - 1) // 2) for a in range(3 , n + 1 ) ) if __name__ == "__main__": print(solution())
15
import pytest from datasets import Dataset, DatasetDict, Features, NamedSplit, Value from datasets.io.text import TextDatasetReader from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ ) -> Union[str, Any]: assert isinstance(snake_case__ , snake_case__ ) assert dataset.num_rows == 4 assert dataset.num_columns == 1 assert dataset.column_names == ["text"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize('''keep_in_memory''' , [False, True] ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> Union[str, Any]: lowerCAmelCase = tmp_path / '''cache''' lowerCAmelCase = {'''text''': '''string'''} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): lowerCAmelCase = TextDatasetReader(snake_case__ , cache_dir=snake_case__ , keep_in_memory=snake_case__ ).read() _check_text_dataset(snake_case__ , snake_case__ ) @pytest.mark.parametrize( '''features''' , [ None, {'''text''': '''string'''}, {'''text''': '''int32'''}, {'''text''': '''float32'''}, ] , ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> Optional[Any]: lowerCAmelCase = tmp_path / '''cache''' lowerCAmelCase = {'''text''': '''string'''} lowerCAmelCase = features.copy() if features else default_expected_features lowerCAmelCase = ( Features({feature: Value(snake_case__ ) for feature, dtype in features.items()} ) if features is not None else None ) lowerCAmelCase = TextDatasetReader(snake_case__ , features=snake_case__ , cache_dir=snake_case__ ).read() _check_text_dataset(snake_case__ , snake_case__ ) @pytest.mark.parametrize('''split''' , [None, NamedSplit('''train''' ), '''train''', '''test'''] ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> List[str]: lowerCAmelCase = tmp_path / '''cache''' lowerCAmelCase = {'''text''': '''string'''} lowerCAmelCase = TextDatasetReader(snake_case__ , cache_dir=snake_case__ , split=snake_case__ ).read() _check_text_dataset(snake_case__ , snake_case__ ) assert dataset.split == split if split else "train" @pytest.mark.parametrize('''path_type''' , [str, list] ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> Optional[int]: if issubclass(snake_case__ , snake_case__ ): lowerCAmelCase = text_path elif issubclass(snake_case__ , snake_case__ ): lowerCAmelCase = [text_path] lowerCAmelCase = tmp_path / '''cache''' lowerCAmelCase = {'''text''': '''string'''} lowerCAmelCase = TextDatasetReader(snake_case__ , cache_dir=snake_case__ ).read() _check_text_dataset(snake_case__ , snake_case__ ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__=("train",) ) -> Optional[Any]: assert isinstance(snake_case__ , snake_case__ ) for split in splits: lowerCAmelCase = dataset_dict[split] assert dataset.num_rows == 4 assert dataset.num_columns == 1 assert dataset.column_names == ["text"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize('''keep_in_memory''' , [False, True] ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> Optional[Any]: lowerCAmelCase = tmp_path / '''cache''' lowerCAmelCase = {'''text''': '''string'''} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): lowerCAmelCase = TextDatasetReader({'''train''': text_path} , cache_dir=snake_case__ , keep_in_memory=snake_case__ ).read() _check_text_datasetdict(snake_case__ , snake_case__ ) @pytest.mark.parametrize( '''features''' , [ None, {'''text''': '''string'''}, {'''text''': '''int32'''}, {'''text''': '''float32'''}, ] , ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> List[Any]: lowerCAmelCase = tmp_path / '''cache''' # CSV file loses col_1 string dtype information: default now is "int64" instead of "string" lowerCAmelCase = {'''text''': '''string'''} lowerCAmelCase = features.copy() if features else default_expected_features lowerCAmelCase = ( Features({feature: Value(snake_case__ ) for feature, dtype in features.items()} ) if features is not None else None ) lowerCAmelCase = TextDatasetReader({'''train''': text_path} , features=snake_case__ , cache_dir=snake_case__ ).read() _check_text_datasetdict(snake_case__ , snake_case__ ) @pytest.mark.parametrize('''split''' , [None, NamedSplit('''train''' ), '''train''', '''test'''] ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> Any: if split: lowerCAmelCase = {split: text_path} else: lowerCAmelCase = '''train''' lowerCAmelCase = {'''train''': text_path, '''test''': text_path} lowerCAmelCase = tmp_path / '''cache''' lowerCAmelCase = {'''text''': '''string'''} lowerCAmelCase = TextDatasetReader(snake_case__ , cache_dir=snake_case__ ).read() _check_text_datasetdict(snake_case__ , snake_case__ , splits=list(path.keys() ) ) assert all(dataset[split].split == split for split in path.keys() )
338
0
"""simple docstring""" from math import pi, sqrt def __UpperCAmelCase ( __lowerCamelCase ) -> float: if num <= 0: raise ValueError('''math domain error''' ) if num > 1_7_1.5: raise OverflowError('''math range error''' ) elif num - int(__lowerCamelCase ) not in (0, 0.5): raise NotImplementedError('''num must be an integer or a half-integer''' ) elif num == 0.5: return sqrt(__lowerCamelCase ) else: return 1.0 if num == 1 else (num - 1) * gamma(num - 1 ) def __UpperCAmelCase ( ) -> None: assert gamma(0.5 ) == sqrt(__lowerCamelCase ) assert gamma(1 ) == 1.0 assert gamma(2 ) == 1.0 if __name__ == "__main__": from doctest import testmod testmod() lowerCAmelCase_ = 1.0 while num: lowerCAmelCase_ = float(input('Gamma of: ')) print(F'''gamma({num}) = {gamma(num)}''') print('\nEnter 0 to exit...')
16
def SCREAMING_SNAKE_CASE_ ( snake_case__ ) -> str: if isinstance(snake_case__ , snake_case__ ): raise TypeError('''\'float\' object cannot be interpreted as an integer''' ) if isinstance(snake_case__ , snake_case__ ): raise TypeError('''\'str\' object cannot be interpreted as an integer''' ) if num == 0: return "0b0" lowerCAmelCase = False if num < 0: lowerCAmelCase = True lowerCAmelCase = -num lowerCAmelCase = [] while num > 0: binary.insert(0 , num % 2 ) num >>= 1 if negative: return "-0b" + "".join(str(snake_case__ ) for e in binary ) return "0b" + "".join(str(snake_case__ ) for e in binary ) if __name__ == "__main__": import doctest doctest.testmod()
338
0
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available _a = { 'configuration_groupvit': [ 'GROUPVIT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'GroupViTConfig', 'GroupViTOnnxConfig', 'GroupViTTextConfig', 'GroupViTVisionConfig', ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _a = [ 'GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST', 'GroupViTModel', 'GroupViTPreTrainedModel', 'GroupViTTextModel', 'GroupViTVisionModel', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _a = [ 'TF_GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFGroupViTModel', 'TFGroupViTPreTrainedModel', 'TFGroupViTTextModel', 'TFGroupViTVisionModel', ] if TYPE_CHECKING: from .configuration_groupvit import ( GROUPVIT_PRETRAINED_CONFIG_ARCHIVE_MAP, GroupViTConfig, GroupViTOnnxConfig, GroupViTTextConfig, GroupViTVisionConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_groupvit import ( GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST, GroupViTModel, GroupViTPreTrainedModel, GroupViTTextModel, GroupViTVisionModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_groupvit import ( TF_GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST, TFGroupViTModel, TFGroupViTPreTrainedModel, TFGroupViTTextModel, TFGroupViTVisionModel, ) else: import sys _a = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
17
class lowercase_ : """simple docstring""" def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ->Any: lowerCAmelCase = name lowerCAmelCase = value lowerCAmelCase = weight def __repr__( self ) ->str: return F"{self.__class__.__name__}({self.name}, {self.value}, {self.weight})" def SCREAMING_SNAKE_CASE_ ( self ) ->List[Any]: return self.value def SCREAMING_SNAKE_CASE_ ( self ) ->int: return self.name def SCREAMING_SNAKE_CASE_ ( self ) ->Union[str, Any]: return self.weight def SCREAMING_SNAKE_CASE_ ( self ) ->Tuple: return self.value / self.weight def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> int: lowerCAmelCase = [] for i in range(len(snake_case__ ) ): menu.append(Things(name[i] , value[i] , weight[i] ) ) return menu def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> Optional[int]: lowerCAmelCase = sorted(snake_case__ , key=snake_case__ , reverse=snake_case__ ) lowerCAmelCase = [] lowerCAmelCase , lowerCAmelCase = 0.0, 0.0 for i in range(len(snake_case__ ) ): if (total_cost + items_copy[i].get_weight()) <= max_cost: result.append(items_copy[i] ) total_cost += items_copy[i].get_weight() total_value += items_copy[i].get_value() return (result, total_value) def SCREAMING_SNAKE_CASE_ ( ) -> Optional[int]: pass if __name__ == "__main__": import doctest doctest.testmod()
338
0
import sys import webbrowser import requests from bsa import BeautifulSoup from fake_useragent import UserAgent if __name__ == "__main__": print('''Googling.....''') __lowerCamelCase : Optional[Any] = '''https://www.google.com/search?q=''' + ''' '''.join(sys.argv[1:]) __lowerCamelCase : Optional[int] = requests.get(url, headers={'''UserAgent''': UserAgent().random}) # res.raise_for_status() with open('''project1a.html''', '''wb''') as out_file: # only for knowing the class for data in res.iter_content(1_00_00): out_file.write(data) __lowerCamelCase : int = BeautifulSoup(res.text, '''html.parser''') __lowerCamelCase : Any = list(soup.select('''.eZt8xd'''))[:5] print(len(links)) for link in links: if link.text == "Maps": webbrowser.open(link.get('''href''')) else: webbrowser.open(f'''https://google.com{link.get('href')}''')
18
import numpy as np import skfuzzy as fuzz if __name__ == "__main__": # Create universe of discourse in Python using linspace () lowercase__ : Dict = np.linspace(start=0, stop=7_5, num=7_5, endpoint=True, retstep=False) # Create two fuzzy sets by defining any membership function # (trapmf(), gbellmf(), gaussmf(), etc). lowercase__ : Optional[int] = [0, 2_5, 5_0] lowercase__ : Union[str, Any] = [2_5, 5_0, 7_5] lowercase__ : int = fuzz.membership.trimf(X, abca) lowercase__ : Tuple = fuzz.membership.trimf(X, abca) # Compute the different operations using inbuilt functions. lowercase__ : List[str] = np.ones(7_5) lowercase__ : Any = np.zeros((7_5,)) # 1. Union = max(µA(x), µB(x)) lowercase__ : Union[str, Any] = fuzz.fuzzy_or(X, young, X, middle_aged)[1] # 2. Intersection = min(µA(x), µB(x)) lowercase__ : int = fuzz.fuzzy_and(X, young, X, middle_aged)[1] # 3. Complement (A) = (1- min(µA(x)) lowercase__ : Union[str, Any] = fuzz.fuzzy_not(young) # 4. Difference (A/B) = min(µA(x),(1- µB(x))) lowercase__ : Optional[int] = fuzz.fuzzy_and(X, young, X, fuzz.fuzzy_not(middle_aged)[1])[1] # 5. Algebraic Sum = [µA(x) + µB(x) – (µA(x) * µB(x))] lowercase__ : Any = young + middle_aged - (young * middle_aged) # 6. Algebraic Product = (µA(x) * µB(x)) lowercase__ : str = young * middle_aged # 7. Bounded Sum = min[1,(µA(x), µB(x))] lowercase__ : Tuple = fuzz.fuzzy_and(X, one, X, young + middle_aged)[1] # 8. Bounded difference = min[0,(µA(x), µB(x))] lowercase__ : Tuple = fuzz.fuzzy_or(X, zero, X, young - middle_aged)[1] # max-min composition # max-product composition # Plot each set A, set B and each operation result using plot() and subplot(). from matplotlib import pyplot as plt plt.figure() plt.subplot(4, 3, 1) plt.plot(X, young) plt.title('''Young''') plt.grid(True) plt.subplot(4, 3, 2) plt.plot(X, middle_aged) plt.title('''Middle aged''') plt.grid(True) plt.subplot(4, 3, 3) plt.plot(X, union) plt.title('''union''') plt.grid(True) plt.subplot(4, 3, 4) plt.plot(X, intersection) plt.title('''intersection''') plt.grid(True) plt.subplot(4, 3, 5) plt.plot(X, complement_a) plt.title('''complement_a''') plt.grid(True) plt.subplot(4, 3, 6) plt.plot(X, difference) plt.title('''difference a/b''') plt.grid(True) plt.subplot(4, 3, 7) plt.plot(X, alg_sum) plt.title('''alg_sum''') plt.grid(True) plt.subplot(4, 3, 8) plt.plot(X, alg_product) plt.title('''alg_product''') plt.grid(True) plt.subplot(4, 3, 9) plt.plot(X, bdd_sum) plt.title('''bdd_sum''') plt.grid(True) plt.subplot(4, 3, 1_0) plt.plot(X, bdd_difference) plt.title('''bdd_difference''') plt.grid(True) plt.subplots_adjust(hspace=0.5) plt.show()
338
0
import unittest import numpy as np import torch from diffusers import PNDMPipeline, PNDMScheduler, UNetaDModel from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device enable_full_determinism() class _SCREAMING_SNAKE_CASE ( unittest.TestCase ): @property def SCREAMING_SNAKE_CASE_( self ) -> Optional[Any]: torch.manual_seed(0 ) lowerCamelCase_ = UNetaDModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=3 , out_channels=3 , down_block_types=("DownBlock2D", "AttnDownBlock2D") , up_block_types=("AttnUpBlock2D", "UpBlock2D") , ) return model def SCREAMING_SNAKE_CASE_( self ) -> Optional[Any]: lowerCamelCase_ = self.dummy_uncond_unet lowerCamelCase_ = PNDMScheduler() lowerCamelCase_ = PNDMPipeline(unet=lowercase , scheduler=lowercase ) pndm.to(lowercase ) pndm.set_progress_bar_config(disable=lowercase ) lowerCamelCase_ = torch.manual_seed(0 ) lowerCamelCase_ = pndm(generator=lowercase , num_inference_steps=20 , output_type="numpy" ).images lowerCamelCase_ = torch.manual_seed(0 ) lowerCamelCase_ = pndm(generator=lowercase , num_inference_steps=20 , output_type="numpy" , return_dict=lowercase )[0] lowerCamelCase_ = image[0, -3:, -3:, -1] lowerCamelCase_ = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) lowerCamelCase_ = np.array([1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 @slow @require_torch class _SCREAMING_SNAKE_CASE ( unittest.TestCase ): def SCREAMING_SNAKE_CASE_( self ) -> int: lowerCamelCase_ = "google/ddpm-cifar10-32" lowerCamelCase_ = UNetaDModel.from_pretrained(lowercase ) lowerCamelCase_ = PNDMScheduler() lowerCamelCase_ = PNDMPipeline(unet=lowercase , scheduler=lowercase ) pndm.to(lowercase ) pndm.set_progress_bar_config(disable=lowercase ) lowerCamelCase_ = torch.manual_seed(0 ) lowerCamelCase_ = pndm(generator=lowercase , output_type="numpy" ).images lowerCamelCase_ = image[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) lowerCamelCase_ = np.array([0.1_5_6_4, 0.1_4_6_4_5, 0.1_4_0_6, 0.1_4_7_1_5, 0.1_2_4_2_5, 0.1_4_0_4_5, 0.1_3_1_1_5, 0.1_2_1_7_5, 0.1_2_5] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
19
import torch from diffusers import DDPMScheduler from .test_schedulers import SchedulerCommonTest class lowercase_ ( UpperCamelCase_ ): """simple docstring""" UpperCAmelCase_ : str = (DDPMScheduler,) def SCREAMING_SNAKE_CASE_ ( self , **__SCREAMING_SNAKE_CASE ) ->Optional[Any]: lowerCAmelCase = { '''num_train_timesteps''': 1000, '''beta_start''': 0.0_0_0_1, '''beta_end''': 0.0_2, '''beta_schedule''': '''linear''', '''variance_type''': '''fixed_small''', '''clip_sample''': True, } config.update(**__SCREAMING_SNAKE_CASE ) return config def SCREAMING_SNAKE_CASE_ ( self ) ->Tuple: for timesteps in [1, 5, 100, 1000]: self.check_over_configs(num_train_timesteps=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->List[Any]: for beta_start, beta_end in zip([0.0_0_0_1, 0.0_0_1, 0.0_1, 0.1] , [0.0_0_2, 0.0_2, 0.2, 2] ): self.check_over_configs(beta_start=__SCREAMING_SNAKE_CASE , beta_end=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[Any]: for schedule in ["linear", "squaredcos_cap_v2"]: self.check_over_configs(beta_schedule=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->Dict: for variance in ["fixed_small", "fixed_large", "other"]: self.check_over_configs(variance_type=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->List[str]: for clip_sample in [True, False]: self.check_over_configs(clip_sample=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->Any: self.check_over_configs(thresholding=__SCREAMING_SNAKE_CASE ) for threshold in [0.5, 1.0, 2.0]: for prediction_type in ["epsilon", "sample", "v_prediction"]: self.check_over_configs( thresholding=__SCREAMING_SNAKE_CASE , prediction_type=__SCREAMING_SNAKE_CASE , sample_max_value=__SCREAMING_SNAKE_CASE , ) def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[int]: for prediction_type in ["epsilon", "sample", "v_prediction"]: self.check_over_configs(prediction_type=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[Any]: for t in [0, 500, 999]: self.check_over_forward(time_step=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->Tuple: lowerCAmelCase = self.scheduler_classes[0] lowerCAmelCase = self.get_scheduler_config() lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) assert torch.sum(torch.abs(scheduler._get_variance(0 ) - 0.0 ) ) < 1e-5 assert torch.sum(torch.abs(scheduler._get_variance(487 ) - 0.0_0_9_7_9 ) ) < 1e-5 assert torch.sum(torch.abs(scheduler._get_variance(999 ) - 0.0_2 ) ) < 1e-5 def SCREAMING_SNAKE_CASE_ ( self ) ->str: lowerCAmelCase = self.scheduler_classes[0] lowerCAmelCase = self.get_scheduler_config() lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = len(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = self.dummy_model() lowerCAmelCase = self.dummy_sample_deter lowerCAmelCase = torch.manual_seed(0 ) for t in reversed(range(__SCREAMING_SNAKE_CASE ) ): # 1. predict noise residual lowerCAmelCase = model(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # 2. predict previous mean of sample x_t-1 lowerCAmelCase = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , generator=__SCREAMING_SNAKE_CASE ).prev_sample # if t > 0: # noise = self.dummy_sample_deter # variance = scheduler.get_variance(t) ** (0.5) * noise # # sample = pred_prev_sample + variance lowerCAmelCase = pred_prev_sample lowerCAmelCase = torch.sum(torch.abs(__SCREAMING_SNAKE_CASE ) ) lowerCAmelCase = torch.mean(torch.abs(__SCREAMING_SNAKE_CASE ) ) assert abs(result_sum.item() - 2_5_8.9_6_0_6 ) < 1e-2 assert abs(result_mean.item() - 0.3_3_7_2 ) < 1e-3 def SCREAMING_SNAKE_CASE_ ( self ) ->str: lowerCAmelCase = self.scheduler_classes[0] lowerCAmelCase = self.get_scheduler_config(prediction_type='''v_prediction''' ) lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = len(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = self.dummy_model() lowerCAmelCase = self.dummy_sample_deter lowerCAmelCase = torch.manual_seed(0 ) for t in reversed(range(__SCREAMING_SNAKE_CASE ) ): # 1. predict noise residual lowerCAmelCase = model(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # 2. predict previous mean of sample x_t-1 lowerCAmelCase = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , generator=__SCREAMING_SNAKE_CASE ).prev_sample # if t > 0: # noise = self.dummy_sample_deter # variance = scheduler.get_variance(t) ** (0.5) * noise # # sample = pred_prev_sample + variance lowerCAmelCase = pred_prev_sample lowerCAmelCase = torch.sum(torch.abs(__SCREAMING_SNAKE_CASE ) ) lowerCAmelCase = torch.mean(torch.abs(__SCREAMING_SNAKE_CASE ) ) assert abs(result_sum.item() - 2_0_2.0_2_9_6 ) < 1e-2 assert abs(result_mean.item() - 0.2_6_3_1 ) < 1e-3 def SCREAMING_SNAKE_CASE_ ( self ) ->Any: lowerCAmelCase = self.scheduler_classes[0] lowerCAmelCase = self.get_scheduler_config() lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = [100, 87, 50, 1, 0] scheduler.set_timesteps(timesteps=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = scheduler.timesteps for i, timestep in enumerate(__SCREAMING_SNAKE_CASE ): if i == len(__SCREAMING_SNAKE_CASE ) - 1: lowerCAmelCase = -1 else: lowerCAmelCase = timesteps[i + 1] lowerCAmelCase = scheduler.previous_timestep(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = prev_t.item() self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[Any]: lowerCAmelCase = self.scheduler_classes[0] lowerCAmelCase = self.get_scheduler_config() lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = [100, 87, 50, 51, 0] with self.assertRaises(__SCREAMING_SNAKE_CASE , msg='''`custom_timesteps` must be in descending order.''' ): scheduler.set_timesteps(timesteps=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->str: lowerCAmelCase = self.scheduler_classes[0] lowerCAmelCase = self.get_scheduler_config() lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = [100, 87, 50, 1, 0] lowerCAmelCase = len(__SCREAMING_SNAKE_CASE ) with self.assertRaises(__SCREAMING_SNAKE_CASE , msg='''Can only pass one of `num_inference_steps` or `custom_timesteps`.''' ): scheduler.set_timesteps(num_inference_steps=__SCREAMING_SNAKE_CASE , timesteps=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->Any: lowerCAmelCase = self.scheduler_classes[0] lowerCAmelCase = self.get_scheduler_config() lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = [scheduler.config.num_train_timesteps] with self.assertRaises( __SCREAMING_SNAKE_CASE , msg='''`timesteps` must start before `self.config.train_timesteps`: {scheduler.config.num_train_timesteps}}''' , ): scheduler.set_timesteps(timesteps=__SCREAMING_SNAKE_CASE )
338
0
def _snake_case( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) -> int: lowercase , lowercase : Union[str, Any] = len(SCREAMING_SNAKE_CASE__ ), len(grid[0] ) if ( min(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) < 0 or row == row_length or col == col_length or (row, col) in visit or grid[row][col] == 1 ): return 0 if row == row_length - 1 and col == col_length - 1: return 1 visit.add((row, col) ) lowercase : str = 0 count += depth_first_search(SCREAMING_SNAKE_CASE__ , row + 1 , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) count += depth_first_search(SCREAMING_SNAKE_CASE__ , row - 1 , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) count += depth_first_search(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , col + 1 , SCREAMING_SNAKE_CASE__ ) count += depth_first_search(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , col - 1 , SCREAMING_SNAKE_CASE__ ) visit.remove((row, col) ) return count if __name__ == "__main__": import doctest doctest.testmod()
20
import json import os from typing import Optional import numpy as np from ...feature_extraction_utils import BatchFeature from ...processing_utils import ProcessorMixin from ...utils import logging from ...utils.hub import get_file_from_repo from ..auto import AutoTokenizer lowercase__ : str = logging.get_logger(__name__) class lowercase_ ( UpperCamelCase_ ): """simple docstring""" UpperCAmelCase_ : Any = """AutoTokenizer""" UpperCAmelCase_ : Optional[int] = ["""tokenizer"""] UpperCAmelCase_ : str = { """semantic_prompt""": 1, """coarse_prompt""": 2, """fine_prompt""": 2, } def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None ) ->Optional[Any]: super().__init__(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = speaker_embeddings @classmethod def SCREAMING_SNAKE_CASE_ ( cls , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE="speaker_embeddings_path.json" , **__SCREAMING_SNAKE_CASE ) ->Tuple: if speaker_embeddings_dict_path is not None: lowerCAmelCase = get_file_from_repo( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , subfolder=kwargs.pop('''subfolder''' , __SCREAMING_SNAKE_CASE ) , cache_dir=kwargs.pop('''cache_dir''' , __SCREAMING_SNAKE_CASE ) , force_download=kwargs.pop('''force_download''' , __SCREAMING_SNAKE_CASE ) , proxies=kwargs.pop('''proxies''' , __SCREAMING_SNAKE_CASE ) , resume_download=kwargs.pop('''resume_download''' , __SCREAMING_SNAKE_CASE ) , local_files_only=kwargs.pop('''local_files_only''' , __SCREAMING_SNAKE_CASE ) , use_auth_token=kwargs.pop('''use_auth_token''' , __SCREAMING_SNAKE_CASE ) , revision=kwargs.pop('''revision''' , __SCREAMING_SNAKE_CASE ) , ) if speaker_embeddings_path is None: logger.warning( F"`{os.path.join(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE )}` does not exists\n , no preloaded speaker embeddings will be used - Make sure to provide a correct path to the json\n dictionnary if wanted, otherwise set `speaker_embeddings_dict_path=None`." ) lowerCAmelCase = None else: with open(__SCREAMING_SNAKE_CASE ) as speaker_embeddings_json: lowerCAmelCase = json.load(__SCREAMING_SNAKE_CASE ) else: lowerCAmelCase = None lowerCAmelCase = AutoTokenizer.from_pretrained(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) return cls(tokenizer=__SCREAMING_SNAKE_CASE , speaker_embeddings=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE="speaker_embeddings_path.json" , __SCREAMING_SNAKE_CASE="speaker_embeddings" , __SCREAMING_SNAKE_CASE = False , **__SCREAMING_SNAKE_CASE , ) ->int: if self.speaker_embeddings is not None: os.makedirs(os.path.join(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , '''v2''' ) , exist_ok=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = {} lowerCAmelCase = save_directory for prompt_key in self.speaker_embeddings: if prompt_key != "repo_or_path": lowerCAmelCase = self._load_voice_preset(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = {} for key in self.speaker_embeddings[prompt_key]: np.save( os.path.join( embeddings_dict['''repo_or_path'''] , __SCREAMING_SNAKE_CASE , F"{prompt_key}_{key}" ) , voice_preset[key] , allow_pickle=__SCREAMING_SNAKE_CASE , ) lowerCAmelCase = os.path.join(__SCREAMING_SNAKE_CASE , F"{prompt_key}_{key}.npy" ) lowerCAmelCase = tmp_dict with open(os.path.join(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) , '''w''' ) as fp: json.dump(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) super().save_pretrained(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE = None , **__SCREAMING_SNAKE_CASE ) ->List[str]: lowerCAmelCase = self.speaker_embeddings[voice_preset] lowerCAmelCase = {} for key in ["semantic_prompt", "coarse_prompt", "fine_prompt"]: if key not in voice_preset_paths: raise ValueError( F"Voice preset unrecognized, missing {key} as a key in self.speaker_embeddings[{voice_preset}]." ) lowerCAmelCase = get_file_from_repo( self.speaker_embeddings.get('''repo_or_path''' , '''/''' ) , voice_preset_paths[key] , subfolder=kwargs.pop('''subfolder''' , __SCREAMING_SNAKE_CASE ) , cache_dir=kwargs.pop('''cache_dir''' , __SCREAMING_SNAKE_CASE ) , force_download=kwargs.pop('''force_download''' , __SCREAMING_SNAKE_CASE ) , proxies=kwargs.pop('''proxies''' , __SCREAMING_SNAKE_CASE ) , resume_download=kwargs.pop('''resume_download''' , __SCREAMING_SNAKE_CASE ) , local_files_only=kwargs.pop('''local_files_only''' , __SCREAMING_SNAKE_CASE ) , use_auth_token=kwargs.pop('''use_auth_token''' , __SCREAMING_SNAKE_CASE ) , revision=kwargs.pop('''revision''' , __SCREAMING_SNAKE_CASE ) , ) if path is None: raise ValueError( F"`{os.path.join(self.speaker_embeddings.get('repo_or_path' , '/' ) , voice_preset_paths[key] )}` does not exists\n , no preloaded voice preset will be used - Make sure to provide correct paths to the {voice_preset}\n embeddings." ) lowerCAmelCase = np.load(__SCREAMING_SNAKE_CASE ) return voice_preset_dict def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE = None ) ->Tuple: for key in ["semantic_prompt", "coarse_prompt", "fine_prompt"]: if key not in voice_preset: raise ValueError(F"Voice preset unrecognized, missing {key} as a key." ) if not isinstance(voice_preset[key] , np.ndarray ): raise ValueError(F"{key} voice preset must be a {str(self.preset_shape[key] )}D ndarray." ) if len(voice_preset[key].shape ) != self.preset_shape[key]: raise ValueError(F"{key} voice preset must be a {str(self.preset_shape[key] )}D ndarray." ) def __call__( self , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE="pt" , __SCREAMING_SNAKE_CASE=256 , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=False , **__SCREAMING_SNAKE_CASE , ) ->int: if voice_preset is not None and not isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): if ( isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) and self.speaker_embeddings is not None and voice_preset in self.speaker_embeddings ): lowerCAmelCase = self._load_voice_preset(__SCREAMING_SNAKE_CASE ) else: if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) and not voice_preset.endswith('''.npz''' ): lowerCAmelCase = voice_preset + '''.npz''' lowerCAmelCase = np.load(__SCREAMING_SNAKE_CASE ) if voice_preset is not None: self._validate_voice_preset_dict(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) lowerCAmelCase = BatchFeature(data=__SCREAMING_SNAKE_CASE , tensor_type=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = self.tokenizer( __SCREAMING_SNAKE_CASE , return_tensors=__SCREAMING_SNAKE_CASE , padding='''max_length''' , max_length=__SCREAMING_SNAKE_CASE , return_attention_mask=__SCREAMING_SNAKE_CASE , return_token_type_ids=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) if voice_preset is not None: lowerCAmelCase = voice_preset return encoded_text
338
0
import flax.linen as nn import jax.numpy as jnp from .attention_flax import FlaxTransformeraDModel from .resnet_flax import FlaxDownsampleaD, FlaxResnetBlockaD, FlaxUpsampleaD class _lowerCamelCase( nn.Module ): lowercase_ : int lowercase_ : int lowercase_ : float = 0.0 lowercase_ : int = 1 lowercase_ : int = 1 lowercase_ : bool = True lowercase_ : bool = False lowercase_ : bool = False lowercase_ : bool = False lowercase_ : jnp.dtype = jnp.floataa def UpperCamelCase ( self) -> Tuple: """simple docstring""" _lowercase : List[Any] = [] _lowercase : int = [] for i in range(self.num_layers): _lowercase : Dict = self.in_channels if i == 0 else self.out_channels _lowercase : Optional[Any] = FlaxResnetBlockaD( in_channels=lowerCamelCase, out_channels=self.out_channels, dropout_prob=self.dropout, dtype=self.dtype, ) resnets.append(lowerCamelCase) _lowercase : str = FlaxTransformeraDModel( in_channels=self.out_channels, n_heads=self.num_attention_heads, d_head=self.out_channels // self.num_attention_heads, depth=1, use_linear_projection=self.use_linear_projection, only_cross_attention=self.only_cross_attention, use_memory_efficient_attention=self.use_memory_efficient_attention, dtype=self.dtype, ) attentions.append(lowerCamelCase) _lowercase : Dict = resnets _lowercase : Tuple = attentions if self.add_downsample: _lowercase : Dict = FlaxDownsampleaD(self.out_channels, dtype=self.dtype) def __call__( self, lowerCamelCase, lowerCamelCase, lowerCamelCase, lowerCamelCase=True) -> Dict: """simple docstring""" _lowercase : int = () for resnet, attn in zip(self.resnets, self.attentions): _lowercase : int = resnet(lowerCamelCase, lowerCamelCase, deterministic=lowerCamelCase) _lowercase : Optional[Any] = attn(lowerCamelCase, lowerCamelCase, deterministic=lowerCamelCase) output_states += (hidden_states,) if self.add_downsample: _lowercase : str = self.downsamplers_a(lowerCamelCase) output_states += (hidden_states,) return hidden_states, output_states class _lowerCamelCase( nn.Module ): lowercase_ : int lowercase_ : int lowercase_ : float = 0.0 lowercase_ : int = 1 lowercase_ : bool = True lowercase_ : jnp.dtype = jnp.floataa def UpperCamelCase ( self) -> Any: """simple docstring""" _lowercase : Optional[Any] = [] for i in range(self.num_layers): _lowercase : Dict = self.in_channels if i == 0 else self.out_channels _lowercase : List[Any] = FlaxResnetBlockaD( in_channels=lowerCamelCase, out_channels=self.out_channels, dropout_prob=self.dropout, dtype=self.dtype, ) resnets.append(lowerCamelCase) _lowercase : str = resnets if self.add_downsample: _lowercase : int = FlaxDownsampleaD(self.out_channels, dtype=self.dtype) def __call__( self, lowerCamelCase, lowerCamelCase, lowerCamelCase=True) -> List[Any]: """simple docstring""" _lowercase : Tuple = () for resnet in self.resnets: _lowercase : List[str] = resnet(lowerCamelCase, lowerCamelCase, deterministic=lowerCamelCase) output_states += (hidden_states,) if self.add_downsample: _lowercase : Any = self.downsamplers_a(lowerCamelCase) output_states += (hidden_states,) return hidden_states, output_states class _lowerCamelCase( nn.Module ): lowercase_ : int lowercase_ : int lowercase_ : int lowercase_ : float = 0.0 lowercase_ : int = 1 lowercase_ : int = 1 lowercase_ : bool = True lowercase_ : bool = False lowercase_ : bool = False lowercase_ : bool = False lowercase_ : jnp.dtype = jnp.floataa def UpperCamelCase ( self) -> Tuple: """simple docstring""" _lowercase : Union[str, Any] = [] _lowercase : Optional[Any] = [] for i in range(self.num_layers): _lowercase : Optional[int] = self.in_channels if (i == self.num_layers - 1) else self.out_channels _lowercase : Any = self.prev_output_channel if i == 0 else self.out_channels _lowercase : Union[str, Any] = FlaxResnetBlockaD( in_channels=resnet_in_channels + res_skip_channels, out_channels=self.out_channels, dropout_prob=self.dropout, dtype=self.dtype, ) resnets.append(lowerCamelCase) _lowercase : List[str] = FlaxTransformeraDModel( in_channels=self.out_channels, n_heads=self.num_attention_heads, d_head=self.out_channels // self.num_attention_heads, depth=1, use_linear_projection=self.use_linear_projection, only_cross_attention=self.only_cross_attention, use_memory_efficient_attention=self.use_memory_efficient_attention, dtype=self.dtype, ) attentions.append(lowerCamelCase) _lowercase : Tuple = resnets _lowercase : List[Any] = attentions if self.add_upsample: _lowercase : int = FlaxUpsampleaD(self.out_channels, dtype=self.dtype) def __call__( self, lowerCamelCase, lowerCamelCase, lowerCamelCase, lowerCamelCase, lowerCamelCase=True) -> Tuple: """simple docstring""" for resnet, attn in zip(self.resnets, self.attentions): # pop res hidden states _lowercase : Dict = res_hidden_states_tuple[-1] _lowercase : Dict = res_hidden_states_tuple[:-1] _lowercase : Union[str, Any] = jnp.concatenate((hidden_states, res_hidden_states), axis=-1) _lowercase : Any = resnet(lowerCamelCase, lowerCamelCase, deterministic=lowerCamelCase) _lowercase : Dict = attn(lowerCamelCase, lowerCamelCase, deterministic=lowerCamelCase) if self.add_upsample: _lowercase : int = self.upsamplers_a(lowerCamelCase) return hidden_states class _lowerCamelCase( nn.Module ): lowercase_ : int lowercase_ : int lowercase_ : int lowercase_ : float = 0.0 lowercase_ : int = 1 lowercase_ : bool = True lowercase_ : jnp.dtype = jnp.floataa def UpperCamelCase ( self) -> List[Any]: """simple docstring""" _lowercase : Dict = [] for i in range(self.num_layers): _lowercase : Optional[int] = self.in_channels if (i == self.num_layers - 1) else self.out_channels _lowercase : List[Any] = self.prev_output_channel if i == 0 else self.out_channels _lowercase : Tuple = FlaxResnetBlockaD( in_channels=resnet_in_channels + res_skip_channels, out_channels=self.out_channels, dropout_prob=self.dropout, dtype=self.dtype, ) resnets.append(lowerCamelCase) _lowercase : Optional[int] = resnets if self.add_upsample: _lowercase : Dict = FlaxUpsampleaD(self.out_channels, dtype=self.dtype) def __call__( self, lowerCamelCase, lowerCamelCase, lowerCamelCase, lowerCamelCase=True) -> Any: """simple docstring""" for resnet in self.resnets: # pop res hidden states _lowercase : int = res_hidden_states_tuple[-1] _lowercase : Union[str, Any] = res_hidden_states_tuple[:-1] _lowercase : Any = jnp.concatenate((hidden_states, res_hidden_states), axis=-1) _lowercase : str = resnet(lowerCamelCase, lowerCamelCase, deterministic=lowerCamelCase) if self.add_upsample: _lowercase : List[Any] = self.upsamplers_a(lowerCamelCase) return hidden_states class _lowerCamelCase( nn.Module ): lowercase_ : int lowercase_ : float = 0.0 lowercase_ : int = 1 lowercase_ : int = 1 lowercase_ : bool = False lowercase_ : bool = False lowercase_ : jnp.dtype = jnp.floataa def UpperCamelCase ( self) -> Optional[Any]: """simple docstring""" _lowercase : int = [ FlaxResnetBlockaD( in_channels=self.in_channels, out_channels=self.in_channels, dropout_prob=self.dropout, dtype=self.dtype, ) ] _lowercase : Union[str, Any] = [] for _ in range(self.num_layers): _lowercase : Dict = FlaxTransformeraDModel( in_channels=self.in_channels, n_heads=self.num_attention_heads, d_head=self.in_channels // self.num_attention_heads, depth=1, use_linear_projection=self.use_linear_projection, use_memory_efficient_attention=self.use_memory_efficient_attention, dtype=self.dtype, ) attentions.append(lowerCamelCase) _lowercase : Dict = FlaxResnetBlockaD( in_channels=self.in_channels, out_channels=self.in_channels, dropout_prob=self.dropout, dtype=self.dtype, ) resnets.append(lowerCamelCase) _lowercase : Optional[Any] = resnets _lowercase : str = attentions def __call__( self, lowerCamelCase, lowerCamelCase, lowerCamelCase, lowerCamelCase=True) -> int: """simple docstring""" _lowercase : str = self.resnets[0](lowerCamelCase, lowerCamelCase) for attn, resnet in zip(self.attentions, self.resnets[1:]): _lowercase : List[str] = attn(lowerCamelCase, lowerCamelCase, deterministic=lowerCamelCase) _lowercase : Tuple = resnet(lowerCamelCase, lowerCamelCase, deterministic=lowerCamelCase) return hidden_states
21
import warnings from diffusers import StableDiffusionInpaintPipeline as StableDiffusionInpaintPipeline # noqa F401 warnings.warn( '''The `inpainting.py` script is outdated. Please use directly `from diffusers import''' ''' StableDiffusionInpaintPipeline` instead.''' )
338
0
'''simple docstring''' from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...onnx.utils import compute_effective_axis_dimension from ...utils import logging if TYPE_CHECKING: from ...processing_utils import ProcessorMixin from ...utils import TensorType __SCREAMING_SNAKE_CASE :Optional[int] = logging.get_logger(__name__) __SCREAMING_SNAKE_CASE :Dict = { '''microsoft/layoutlmv3-base''': '''https://huggingface.co/microsoft/layoutlmv3-base/resolve/main/config.json''', } class A_ ( lowerCAmelCase_ ): _lowerCamelCase : Optional[int] = """layoutlmv3""" def __init__( self : int , snake_case_ : List[Any]=5_0_2_6_5 , snake_case_ : int=7_6_8 , snake_case_ : Optional[int]=1_2 , snake_case_ : Dict=1_2 , snake_case_ : Union[str, Any]=3_0_7_2 , snake_case_ : Optional[Any]="gelu" , snake_case_ : Union[str, Any]=0.1 , snake_case_ : Optional[Any]=0.1 , snake_case_ : List[Any]=5_1_2 , snake_case_ : Any=2 , snake_case_ : Any=0.0_2 , snake_case_ : Union[str, Any]=1e-5 , snake_case_ : Tuple=1 , snake_case_ : Any=0 , snake_case_ : Optional[int]=2 , snake_case_ : List[str]=1_0_2_4 , snake_case_ : int=1_2_8 , snake_case_ : List[Any]=1_2_8 , snake_case_ : List[str]=True , snake_case_ : int=3_2 , snake_case_ : Union[str, Any]=1_2_8 , snake_case_ : Dict=6_4 , snake_case_ : int=2_5_6 , snake_case_ : Tuple=True , snake_case_ : str=True , snake_case_ : Tuple=True , snake_case_ : Optional[Any]=2_2_4 , snake_case_ : List[str]=3 , snake_case_ : Tuple=1_6 , snake_case_ : Any=None , **snake_case_ : Union[str, Any] , ): super().__init__( vocab_size=snake_case_ , hidden_size=snake_case_ , num_hidden_layers=snake_case_ , num_attention_heads=snake_case_ , intermediate_size=snake_case_ , hidden_act=snake_case_ , hidden_dropout_prob=snake_case_ , attention_probs_dropout_prob=snake_case_ , max_position_embeddings=snake_case_ , type_vocab_size=snake_case_ , initializer_range=snake_case_ , layer_norm_eps=snake_case_ , pad_token_id=snake_case_ , bos_token_id=snake_case_ , eos_token_id=snake_case_ , **snake_case_ , ) _UpperCAmelCase = max_ad_position_embeddings _UpperCAmelCase = coordinate_size _UpperCAmelCase = shape_size _UpperCAmelCase = has_relative_attention_bias _UpperCAmelCase = rel_pos_bins _UpperCAmelCase = max_rel_pos _UpperCAmelCase = has_spatial_attention_bias _UpperCAmelCase = rel_ad_pos_bins _UpperCAmelCase = max_rel_ad_pos _UpperCAmelCase = text_embed _UpperCAmelCase = visual_embed _UpperCAmelCase = input_size _UpperCAmelCase = num_channels _UpperCAmelCase = patch_size _UpperCAmelCase = classifier_dropout class A_ ( lowerCAmelCase_ ): _lowerCamelCase : Tuple = version.parse("""1.12""" ) @property def lowercase ( self : List[str] ): # The order of inputs is different for question answering and sequence classification if self.task in ["question-answering", "sequence-classification"]: return OrderedDict( [ ("input_ids", {0: "batch", 1: "sequence"}), ("attention_mask", {0: "batch", 1: "sequence"}), ("bbox", {0: "batch", 1: "sequence"}), ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}), ] ) else: return OrderedDict( [ ("input_ids", {0: "batch", 1: "sequence"}), ("bbox", {0: "batch", 1: "sequence"}), ("attention_mask", {0: "batch", 1: "sequence"}), ("pixel_values", {0: "batch", 1: "num_channels"}), ] ) @property def lowercase ( self : Union[str, Any] ): return 1e-5 @property def lowercase ( self : Dict ): return 1_2 def lowercase ( self : Optional[Any] , snake_case_ : "ProcessorMixin" , snake_case_ : int = -1 , snake_case_ : int = -1 , snake_case_ : bool = False , snake_case_ : Optional["TensorType"] = None , snake_case_ : int = 3 , snake_case_ : int = 4_0 , snake_case_ : int = 4_0 , ): setattr(processor.image_processor , "apply_ocr" , snake_case_ ) # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX _UpperCAmelCase = compute_effective_axis_dimension( snake_case_ , fixed_dimension=OnnxConfig.default_fixed_batch , num_token_to_add=0 ) # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX _UpperCAmelCase = processor.tokenizer.num_special_tokens_to_add(snake_case_ ) _UpperCAmelCase = compute_effective_axis_dimension( snake_case_ , fixed_dimension=OnnxConfig.default_fixed_sequence , num_token_to_add=snake_case_ ) # Generate dummy inputs according to compute batch and sequence _UpperCAmelCase = [[" ".join([processor.tokenizer.unk_token] ) * seq_length]] * batch_size # Generate dummy bounding boxes _UpperCAmelCase = [[[4_8, 8_4, 7_3, 1_2_8]]] * batch_size # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX # batch_size = compute_effective_axis_dimension(batch_size, fixed_dimension=OnnxConfig.default_fixed_batch) _UpperCAmelCase = self._generate_dummy_images(snake_case_ , snake_case_ , snake_case_ , snake_case_ ) _UpperCAmelCase = dict( processor( snake_case_ , text=snake_case_ , boxes=snake_case_ , return_tensors=snake_case_ , ) ) return inputs
22
import os import re import shutil import sys import tempfile import unittest import black lowercase__ : List[str] = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) sys.path.append(os.path.join(git_repo_path, '''utils''')) import check_copies # noqa: E402 # This is the reference code that will be used in the tests. # If BertLMPredictionHead is changed in modeling_bert.py, this code needs to be manually updated. lowercase__ : Dict = ''' def __init__(self, config): super().__init__() self.transform = BertPredictionHeadTransform(config) # The output weights are the same as the input embeddings, but there is # an output-only bias for each token. self.decoder = nn.Linear(config.hidden_size, config.vocab_size, bias=False) self.bias = nn.Parameter(torch.zeros(config.vocab_size)) # Need a link between the two variables so that the bias is correctly resized with `resize_token_embeddings` self.decoder.bias = self.bias def forward(self, hidden_states): hidden_states = self.transform(hidden_states) hidden_states = self.decoder(hidden_states) return hidden_states ''' class lowercase_ ( unittest.TestCase ): """simple docstring""" def SCREAMING_SNAKE_CASE_ ( self ) ->Dict: lowerCAmelCase = tempfile.mkdtemp() os.makedirs(os.path.join(self.transformer_dir , '''models/bert/''' ) ) lowerCAmelCase = self.transformer_dir shutil.copy( os.path.join(__SCREAMING_SNAKE_CASE , '''src/transformers/models/bert/modeling_bert.py''' ) , os.path.join(self.transformer_dir , '''models/bert/modeling_bert.py''' ) , ) def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[Any]: lowerCAmelCase = '''src/transformers''' shutil.rmtree(self.transformer_dir ) def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None ) ->Union[str, Any]: lowerCAmelCase = comment + F"\nclass {class_name}(nn.Module):\n" + class_code if overwrite_result is not None: lowerCAmelCase = comment + F"\nclass {class_name}(nn.Module):\n" + overwrite_result lowerCAmelCase = black.Mode(target_versions={black.TargetVersion.PYaa} , line_length=119 ) lowerCAmelCase = black.format_str(__SCREAMING_SNAKE_CASE , mode=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = os.path.join(self.transformer_dir , '''new_code.py''' ) with open(__SCREAMING_SNAKE_CASE , '''w''' , newline='''\n''' ) as f: f.write(__SCREAMING_SNAKE_CASE ) if overwrite_result is None: self.assertTrue(len(check_copies.is_copy_consistent(__SCREAMING_SNAKE_CASE ) ) == 0 ) else: check_copies.is_copy_consistent(f.name , overwrite=__SCREAMING_SNAKE_CASE ) with open(__SCREAMING_SNAKE_CASE , '''r''' ) as f: self.assertTrue(f.read() , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->int: lowerCAmelCase = check_copies.find_code_in_transformers('''models.bert.modeling_bert.BertLMPredictionHead''' ) self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->Tuple: # Base copy consistency self.check_copy_consistency( '''# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead''' , '''BertLMPredictionHead''' , REFERENCE_CODE + '''\n''' , ) # With no empty line at the end self.check_copy_consistency( '''# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead''' , '''BertLMPredictionHead''' , __SCREAMING_SNAKE_CASE , ) # Copy consistency with rename self.check_copy_consistency( '''# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->TestModel''' , '''TestModelLMPredictionHead''' , re.sub('''Bert''' , '''TestModel''' , __SCREAMING_SNAKE_CASE ) , ) # Copy consistency with a really long name lowerCAmelCase = '''TestModelWithAReallyLongNameBecauseSomePeopleLikeThatForSomeReason''' self.check_copy_consistency( F"# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->{long_class_name}" , F"{long_class_name}LMPredictionHead" , re.sub('''Bert''' , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) , ) # Copy consistency with overwrite self.check_copy_consistency( '''# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->TestModel''' , '''TestModelLMPredictionHead''' , __SCREAMING_SNAKE_CASE , overwrite_result=re.sub('''Bert''' , '''TestModel''' , __SCREAMING_SNAKE_CASE ) , ) def SCREAMING_SNAKE_CASE_ ( self ) ->Tuple: lowerCAmelCase = check_copies.LOCALIZED_READMES['''README_zh-hans.md'''] lowerCAmelCase = ( '''1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (from Google Research and the''' ''' Toyota Technological Institute at Chicago) released with the paper [ALBERT: A Lite BERT for''' ''' Self-supervised Learning of Language Representations](https://arxiv.org/abs/1909.11942), by Zhenzhong''' ''' Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut.\n1.''' ''' **[DistilBERT](https://huggingface.co/transformers/model_doc/distilbert.html)** (from HuggingFace),''' ''' released together with the paper [DistilBERT, a distilled version of BERT: smaller, faster, cheaper and''' ''' lighter](https://arxiv.org/abs/1910.01108) by Victor Sanh, Lysandre Debut and Thomas Wolf. The same''' ''' method has been applied to compress GPT2 into''' ''' [DistilGPT2](https://github.com/huggingface/transformers/tree/main/examples/distillation), RoBERTa into''' ''' [DistilRoBERTa](https://github.com/huggingface/transformers/tree/main/examples/distillation),''' ''' Multilingual BERT into''' ''' [DistilmBERT](https://github.com/huggingface/transformers/tree/main/examples/distillation) and a German''' ''' version of DistilBERT.\n1. **[ELECTRA](https://huggingface.co/transformers/model_doc/electra.html)**''' ''' (from Google Research/Stanford University) released with the paper [ELECTRA: Pre-training text encoders''' ''' as discriminators rather than generators](https://arxiv.org/abs/2003.10555) by Kevin Clark, Minh-Thang''' ''' Luong, Quoc V. Le, Christopher D. Manning.''' ) lowerCAmelCase = ( '''1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the''' ''' Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of''' ''' Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian''' ''' Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n''' ) lowerCAmelCase = ( '''1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the''' ''' Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of''' ''' Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian''' ''' Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n1.''' ''' **[DistilBERT](https://huggingface.co/transformers/model_doc/distilbert.html)** (来自 HuggingFace) 伴随论文''' ''' [DistilBERT, a distilled version of BERT: smaller, faster, cheaper and''' ''' lighter](https://arxiv.org/abs/1910.01108) 由 Victor Sanh, Lysandre Debut and Thomas Wolf 发布。 The same''' ''' method has been applied to compress GPT2 into''' ''' [DistilGPT2](https://github.com/huggingface/transformers/tree/main/examples/distillation), RoBERTa into''' ''' [DistilRoBERTa](https://github.com/huggingface/transformers/tree/main/examples/distillation),''' ''' Multilingual BERT into''' ''' [DistilmBERT](https://github.com/huggingface/transformers/tree/main/examples/distillation) and a German''' ''' version of DistilBERT.\n1. **[ELECTRA](https://huggingface.co/transformers/model_doc/electra.html)** (来自''' ''' Google Research/Stanford University) 伴随论文 [ELECTRA: Pre-training text encoders as discriminators rather''' ''' than generators](https://arxiv.org/abs/2003.10555) 由 Kevin Clark, Minh-Thang Luong, Quoc V. Le,''' ''' Christopher D. Manning 发布。\n''' ) lowerCAmelCase , lowerCAmelCase = check_copies.convert_to_localized_md( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , localized_readme['''format_model_list'''] ) self.assertFalse(__SCREAMING_SNAKE_CASE ) self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) lowerCAmelCase , lowerCAmelCase = check_copies.convert_to_localized_md( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , localized_readme['''format_model_list'''] ) # Check whether the number of models is equal to README.md after conversion. self.assertTrue(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = ( '''1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (from Google Research and the''' ''' Toyota Technological Institute at Chicago) released with the paper [ALBERT: A Lite BERT for''' ''' Self-supervised Learning of Language Representations](https://arxiv.org/abs/1909.11942), by Zhenzhong''' ''' Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut.''' ) lowerCAmelCase = ( '''1. **[ALBERT](https://huggingface.co/transformers/main/model_doc/albert.html)** (来自 Google Research and''' ''' the Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of''' ''' Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian''' ''' Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n''' ) lowerCAmelCase = ( '''1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the''' ''' Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of''' ''' Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian''' ''' Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n''' ) lowerCAmelCase , lowerCAmelCase = check_copies.convert_to_localized_md( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , localized_readme['''format_model_list'''] ) # Check if the model link is synchronized. self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE )
338
0
'''simple docstring''' import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCamelCase__: str = logging.get_logger(__name__) UpperCamelCase__: Tuple = { "microsoft/unispeech-sat-base-100h-libri-ft": ( "https://huggingface.co/microsoft/unispeech-sat-base-100h-libri-ft/resolve/main/config.json" ), # See all UniSpeechSat models at https://huggingface.co/models?filter=unispeech_sat } class SCREAMING_SNAKE_CASE( A__ ): """simple docstring""" lowerCamelCase__ = """unispeech-sat""" def __init__( self : str , __snake_case : List[str]=32 , __snake_case : List[str]=768 , __snake_case : str=12 , __snake_case : int=12 , __snake_case : Union[str, Any]=3072 , __snake_case : List[Any]="gelu" , __snake_case : Union[str, Any]=0.1 , __snake_case : Union[str, Any]=0.1 , __snake_case : Dict=0.1 , __snake_case : List[Any]=0.0 , __snake_case : Any=0.0 , __snake_case : Any=0.1 , __snake_case : List[str]=0.1 , __snake_case : Optional[Any]=0.02 , __snake_case : Dict=1E-5 , __snake_case : int="group" , __snake_case : Any="gelu" , __snake_case : Dict=(512, 512, 512, 512, 512, 512, 512) , __snake_case : str=(5, 2, 2, 2, 2, 2, 2) , __snake_case : Tuple=(10, 3, 3, 3, 3, 2, 2) , __snake_case : Optional[int]=False , __snake_case : List[str]=128 , __snake_case : Any=16 , __snake_case : List[str]=False , __snake_case : List[str]=True , __snake_case : Union[str, Any]=0.05 , __snake_case : Tuple=10 , __snake_case : Optional[Any]=2 , __snake_case : Dict=0.0 , __snake_case : int=10 , __snake_case : Tuple=0 , __snake_case : Dict=320 , __snake_case : List[Any]=2 , __snake_case : int=0.1 , __snake_case : str=100 , __snake_case : List[Any]=256 , __snake_case : str=256 , __snake_case : Tuple=0.1 , __snake_case : int="mean" , __snake_case : List[str]=False , __snake_case : Optional[Any]=False , __snake_case : str=256 , __snake_case : Tuple=(512, 512, 512, 512, 1500) , __snake_case : Union[str, Any]=(5, 3, 3, 1, 1) , __snake_case : Dict=(1, 2, 3, 1, 1) , __snake_case : int=512 , __snake_case : Dict=0 , __snake_case : Optional[int]=1 , __snake_case : int=2 , __snake_case : List[Any]=504 , **__snake_case : Dict , ) -> Optional[int]: super().__init__(**__snake_case , pad_token_id=__snake_case , bos_token_id=__snake_case , eos_token_id=__snake_case ) UpperCAmelCase : Tuple = hidden_size UpperCAmelCase : Optional[Any] = feat_extract_norm UpperCAmelCase : int = feat_extract_activation UpperCAmelCase : Optional[int] = list(__snake_case ) UpperCAmelCase : List[str] = list(__snake_case ) UpperCAmelCase : List[str] = list(__snake_case ) UpperCAmelCase : Tuple = conv_bias UpperCAmelCase : Union[str, Any] = num_conv_pos_embeddings UpperCAmelCase : Optional[Any] = num_conv_pos_embedding_groups UpperCAmelCase : Tuple = len(self.conv_dim ) UpperCAmelCase : Optional[Any] = num_hidden_layers UpperCAmelCase : Optional[Any] = intermediate_size UpperCAmelCase : Tuple = hidden_act UpperCAmelCase : Union[str, Any] = num_attention_heads UpperCAmelCase : Dict = hidden_dropout UpperCAmelCase : Optional[Any] = attention_dropout UpperCAmelCase : Optional[int] = activation_dropout UpperCAmelCase : Any = feat_proj_dropout UpperCAmelCase : Optional[Any] = final_dropout UpperCAmelCase : int = layerdrop UpperCAmelCase : str = layer_norm_eps UpperCAmelCase : Dict = initializer_range UpperCAmelCase : Any = vocab_size UpperCAmelCase : str = num_clusters UpperCAmelCase : Tuple = do_stable_layer_norm UpperCAmelCase : Any = use_weighted_layer_sum if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( '''Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` ==''' ''' `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) =''' F""" {len(self.conv_dim )}`, `len(config.conv_stride) = {len(self.conv_stride )}`,""" F""" `len(config.conv_kernel) = {len(self.conv_kernel )}`.""" ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 UpperCAmelCase : Dict = apply_spec_augment UpperCAmelCase : Optional[int] = mask_time_prob UpperCAmelCase : Union[str, Any] = mask_time_length UpperCAmelCase : Tuple = mask_time_min_masks UpperCAmelCase : Tuple = mask_feature_prob UpperCAmelCase : Optional[Any] = mask_feature_length UpperCAmelCase : List[str] = mask_feature_min_masks # parameters for pretraining with codevector quantized representations UpperCAmelCase : List[str] = num_codevectors_per_group UpperCAmelCase : List[Any] = num_codevector_groups UpperCAmelCase : Tuple = contrastive_logits_temperature UpperCAmelCase : Union[str, Any] = feat_quantizer_dropout UpperCAmelCase : Any = num_negatives UpperCAmelCase : int = codevector_dim UpperCAmelCase : Union[str, Any] = proj_codevector_dim UpperCAmelCase : Union[str, Any] = diversity_loss_weight # ctc loss UpperCAmelCase : Tuple = ctc_loss_reduction UpperCAmelCase : List[Any] = ctc_zero_infinity # SequenceClassification-specific parameter. Feel free to ignore for other classes. UpperCAmelCase : str = classifier_proj_size # XVector-specific parameters. Feel free to ignore for other classes. UpperCAmelCase : Tuple = list(__snake_case ) UpperCAmelCase : List[str] = list(__snake_case ) UpperCAmelCase : List[Any] = list(__snake_case ) UpperCAmelCase : Optional[Any] = xvector_output_dim @property def A ( self : Dict ) -> int: return functools.reduce(operator.mul , self.conv_stride , 1 )
23
import pytest from datasets.splits import SplitDict, SplitInfo from datasets.utils.py_utils import asdict @pytest.mark.parametrize( '''split_dict''' , [ SplitDict(), SplitDict({'''train''': SplitInfo(name='''train''' , num_bytes=1_3_3_7 , num_examples=4_2 , dataset_name='''my_dataset''' )} ), SplitDict({'''train''': SplitInfo(name='''train''' , num_bytes=1_3_3_7 , num_examples=4_2 )} ), SplitDict({'''train''': SplitInfo()} ), ] , ) def SCREAMING_SNAKE_CASE_ ( snake_case__ ) -> Union[str, Any]: lowerCAmelCase = split_dict._to_yaml_list() assert len(snake_case__ ) == len(snake_case__ ) lowerCAmelCase = SplitDict._from_yaml_list(snake_case__ ) for split_name, split_info in split_dict.items(): # dataset_name field is deprecated, and is therefore not part of the YAML dump lowerCAmelCase = None # the split name of split_dict takes over the name of the split info object lowerCAmelCase = split_name assert split_dict == reloaded @pytest.mark.parametrize( '''split_info''' , [SplitInfo(), SplitInfo(dataset_name=snake_case__ ), SplitInfo(dataset_name='''my_dataset''' )] ) def SCREAMING_SNAKE_CASE_ ( snake_case__ ) -> Optional[int]: # For backward compatibility, we need asdict(split_dict) to return split info dictrionaries with the "dataset_name" # field even if it's deprecated. This way old versionso of `datasets` can still reload dataset_infos.json files lowerCAmelCase = asdict(SplitDict({'''train''': split_info} ) ) assert "dataset_name" in split_dict_asdict["train"] assert split_dict_asdict["train"]["dataset_name"] == split_info.dataset_name
338
0
def lowerCamelCase__ ( snake_case_ : Optional[Any] ) -> Dict: __snake_case = len(snake_case_ ) __snake_case = sum(snake_case_ ) __snake_case = [[False for x in range(s + 1 )] for y in range(n + 1 )] for i in range(1 , n + 1 ): __snake_case = True for i in range(1 , s + 1 ): __snake_case = False for i in range(1 , n + 1 ): for j in range(1 , s + 1 ): __snake_case = dp[i][j - 1] if arr[i - 1] <= j: __snake_case = dp[i][j] or dp[i - 1][j - arr[i - 1]] for j in range(int(s / 2 ) , -1 , -1 ): if dp[n][j] is True: __snake_case = s - 2 * j break return diff
24
import unittest import numpy as np def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ = None , ) -> np.ndarray: lowerCAmelCase = np.shape(snake_case__ ) lowerCAmelCase = np.shape(snake_case__ ) lowerCAmelCase = np.shape(snake_case__ ) if shape_a[0] != shape_b[0]: lowerCAmelCase = ( '''Expected the same number of rows for A and B. ''' f"Instead found A of size {shape_a} and B of size {shape_b}" ) raise ValueError(snake_case__ ) if shape_b[1] != shape_c[1]: lowerCAmelCase = ( '''Expected the same number of columns for B and C. ''' f"Instead found B of size {shape_b} and C of size {shape_c}" ) raise ValueError(snake_case__ ) lowerCAmelCase = pseudo_inv if a_inv is None: try: lowerCAmelCase = np.linalg.inv(snake_case__ ) except np.linalg.LinAlgError: raise ValueError( '''Input matrix A is not invertible. Cannot compute Schur complement.''' ) return mat_c - mat_b.T @ a_inv @ mat_b class lowercase_ ( unittest.TestCase ): """simple docstring""" def SCREAMING_SNAKE_CASE_ ( self ) ->None: lowerCAmelCase = np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]] ) lowerCAmelCase = np.array([[0, 3], [3, 0], [2, 3]] ) lowerCAmelCase = np.array([[2, 1], [6, 3]] ) lowerCAmelCase = schur_complement(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) lowerCAmelCase = np.block([[a, b], [b.T, c]] ) lowerCAmelCase = np.linalg.det(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = np.linalg.det(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = np.linalg.det(__SCREAMING_SNAKE_CASE ) self.assertAlmostEqual(__SCREAMING_SNAKE_CASE , det_a * det_s ) def SCREAMING_SNAKE_CASE_ ( self ) ->None: lowerCAmelCase = np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]] ) lowerCAmelCase = np.array([[0, 3], [3, 0], [2, 3]] ) lowerCAmelCase = np.array([[2, 1], [6, 3]] ) with self.assertRaises(__SCREAMING_SNAKE_CASE ): schur_complement(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->None: lowerCAmelCase = np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]] ) lowerCAmelCase = np.array([[0, 3], [3, 0], [2, 3]] ) lowerCAmelCase = np.array([[2, 1, 3], [6, 3, 5]] ) with self.assertRaises(__SCREAMING_SNAKE_CASE ): schur_complement(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) if __name__ == "__main__": import doctest doctest.testmod() unittest.main()
338
0
"""simple docstring""" from string import ascii_lowercase, ascii_uppercase def lowercase_ ( _snake_case ): if not sentence: return "" SCREAMING_SNAKE_CASE__ : int = dict(zip(_snake_case ,_snake_case ) ) return lower_to_upper.get(sentence[0] ,sentence[0] ) + sentence[1:] if __name__ == "__main__": from doctest import testmod testmod()
25
import argparse import hashlib import os import urllib import warnings import torch from torch import nn from tqdm import tqdm from transformers import WhisperConfig, WhisperForConditionalGeneration lowercase__ : Any = { '''tiny.en''': '''https://openaipublic.azureedge.net/main/whisper/models/d3dd57d32accea0b295c96e26691aa14d8822fac7d9d27d5dc00b4ca2826dd03/tiny.en.pt''', '''tiny''': '''https://openaipublic.azureedge.net/main/whisper/models/65147644a518d12f04e32d6f3b26facc3f8dd46e5390956a9424a650c0ce22b9/tiny.pt''', '''base.en''': '''https://openaipublic.azureedge.net/main/whisper/models/25a8566e1d0c1e2231d1c762132cd20e0f96a85d16145c3a00adf5d1ac670ead/base.en.pt''', '''base''': '''https://openaipublic.azureedge.net/main/whisper/models/ed3a0b6b1c0edf879ad9b11b1af5a0e6ab5db9205f891f668f8b0e6c6326e34e/base.pt''', '''small.en''': '''https://openaipublic.azureedge.net/main/whisper/models/f953ad0fd29cacd07d5a9eda5624af0f6bcf2258be67c92b79389873d91e0872/small.en.pt''', '''small''': '''https://openaipublic.azureedge.net/main/whisper/models/9ecf779972d90ba49c06d968637d720dd632c55bbf19d441fb42bf17a411e794/small.pt''', '''medium.en''': '''https://openaipublic.azureedge.net/main/whisper/models/d7440d1dc186f76616474e0ff0b3b6b879abc9d1a4926b7adfa41db2d497ab4f/medium.en.pt''', '''medium''': '''https://openaipublic.azureedge.net/main/whisper/models/345ae4da62f9b3d59415adc60127b97c714f32e89e936602e85993674d08dcb1/medium.pt''', '''large''': '''https://openaipublic.azureedge.net/main/whisper/models/e4b87e7e0bf463eb8e6956e646f1e277e901512310def2c24bf0e11bd3c28e9a/large.pt''', '''large-v2''': '''https://openaipublic.azureedge.net/main/whisper/models/81f7c96c852ee8fc832187b0132e569d6c3065a3252ed18e56effd0b6a73e524/large-v2.pt''', } def SCREAMING_SNAKE_CASE_ ( snake_case__ ) -> str: lowerCAmelCase = ['''layers''', '''blocks'''] for k in ignore_keys: state_dict.pop(snake_case__ , snake_case__ ) lowercase__ : List[Any] = { '''blocks''': '''layers''', '''mlp.0''': '''fc1''', '''mlp.2''': '''fc2''', '''mlp_ln''': '''final_layer_norm''', '''.attn.query''': '''.self_attn.q_proj''', '''.attn.key''': '''.self_attn.k_proj''', '''.attn.value''': '''.self_attn.v_proj''', '''.attn_ln''': '''.self_attn_layer_norm''', '''.attn.out''': '''.self_attn.out_proj''', '''.cross_attn.query''': '''.encoder_attn.q_proj''', '''.cross_attn.key''': '''.encoder_attn.k_proj''', '''.cross_attn.value''': '''.encoder_attn.v_proj''', '''.cross_attn_ln''': '''.encoder_attn_layer_norm''', '''.cross_attn.out''': '''.encoder_attn.out_proj''', '''decoder.ln.''': '''decoder.layer_norm.''', '''encoder.ln.''': '''encoder.layer_norm.''', '''token_embedding''': '''embed_tokens''', '''encoder.positional_embedding''': '''encoder.embed_positions.weight''', '''decoder.positional_embedding''': '''decoder.embed_positions.weight''', '''ln_post''': '''layer_norm''', } def SCREAMING_SNAKE_CASE_ ( snake_case__ ) -> Union[str, Any]: lowerCAmelCase = list(s_dict.keys() ) for key in keys: lowerCAmelCase = key for k, v in WHISPER_MAPPING.items(): if k in key: lowerCAmelCase = new_key.replace(snake_case__ , snake_case__ ) print(f"{key} -> {new_key}" ) lowerCAmelCase = s_dict.pop(snake_case__ ) return s_dict def SCREAMING_SNAKE_CASE_ ( snake_case__ ) -> Union[str, Any]: lowerCAmelCase , lowerCAmelCase = emb.weight.shape lowerCAmelCase = nn.Linear(snake_case__ , snake_case__ , bias=snake_case__ ) lowerCAmelCase = emb.weight.data return lin_layer def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ ) -> bytes: os.makedirs(snake_case__ , exist_ok=snake_case__ ) lowerCAmelCase = os.path.basename(snake_case__ ) lowerCAmelCase = url.split('''/''' )[-2] lowerCAmelCase = os.path.join(snake_case__ , snake_case__ ) if os.path.exists(snake_case__ ) and not os.path.isfile(snake_case__ ): raise RuntimeError(f"{download_target} exists and is not a regular file" ) if os.path.isfile(snake_case__ ): lowerCAmelCase = open(snake_case__ , '''rb''' ).read() if hashlib.shaaaa(snake_case__ ).hexdigest() == expected_shaaaa: return model_bytes else: warnings.warn(f"{download_target} exists, but the SHA256 checksum does not match; re-downloading the file" ) with urllib.request.urlopen(snake_case__ ) as source, open(snake_case__ , '''wb''' ) as output: with tqdm( total=int(source.info().get('''Content-Length''' ) ) , ncols=8_0 , unit='''iB''' , unit_scale=snake_case__ , unit_divisor=1_0_2_4 ) as loop: while True: lowerCAmelCase = source.read(8_1_9_2 ) if not buffer: break output.write(snake_case__ ) loop.update(len(snake_case__ ) ) lowerCAmelCase = open(snake_case__ , '''rb''' ).read() if hashlib.shaaaa(snake_case__ ).hexdigest() != expected_shaaaa: raise RuntimeError( '''Model has been downloaded but the SHA256 checksum does not not match. Please retry loading the model.''' ) return model_bytes def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ ) -> str: if ".pt" not in checkpoint_path: lowerCAmelCase = _download(_MODELS[checkpoint_path] ) else: lowerCAmelCase = torch.load(snake_case__ , map_location='''cpu''' ) lowerCAmelCase = original_checkpoint['''dims'''] lowerCAmelCase = original_checkpoint['''model_state_dict'''] lowerCAmelCase = state_dict['''decoder.token_embedding.weight'''] remove_ignore_keys_(snake_case__ ) rename_keys(snake_case__ ) lowerCAmelCase = True lowerCAmelCase = state_dict['''decoder.layers.0.fc1.weight'''].shape[0] lowerCAmelCase = WhisperConfig( vocab_size=dimensions['''n_vocab'''] , encoder_ffn_dim=snake_case__ , decoder_ffn_dim=snake_case__ , num_mel_bins=dimensions['''n_mels'''] , d_model=dimensions['''n_audio_state'''] , max_target_positions=dimensions['''n_text_ctx'''] , encoder_layers=dimensions['''n_audio_layer'''] , encoder_attention_heads=dimensions['''n_audio_head'''] , decoder_layers=dimensions['''n_text_layer'''] , decoder_attention_heads=dimensions['''n_text_state'''] , max_source_positions=dimensions['''n_audio_ctx'''] , ) lowerCAmelCase = WhisperForConditionalGeneration(snake_case__ ) lowerCAmelCase , lowerCAmelCase = model.model.load_state_dict(snake_case__ , strict=snake_case__ ) if len(snake_case__ ) > 0 and not set(snake_case__ ) <= { "encoder.embed_positions.weights", "decoder.embed_positions.weights", }: raise ValueError( '''Only `encoder.embed_positions.weights` and `decoder.embed_positions.weights` are allowed to be missing,''' f" but all the following weights are missing {missing}" ) if tie_embeds: lowerCAmelCase = make_linear_from_emb(model.model.decoder.embed_tokens ) else: lowerCAmelCase = proj_out_weights model.save_pretrained(snake_case__ ) if __name__ == "__main__": lowercase__ : List[str] = argparse.ArgumentParser() # # Required parameters parser.add_argument('''--checkpoint_path''', type=str, help='''Patht to the downloaded checkpoints''') parser.add_argument('''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''') lowercase__ : int = parser.parse_args() convert_openai_whisper_to_tfms(args.checkpoint_path, args.pytorch_dump_folder_path)
338
0
def lowerCAmelCase_ ( snake_case_,snake_case_,snake_case_ = 0,snake_case_ = 0 ): _A : Optional[int] = right or len(snake_case_ ) - 1 if left > right: return -1 elif list_data[left] == key: return left elif list_data[right] == key: return right else: return search(snake_case_,snake_case_,left + 1,right - 1 ) if __name__ == "__main__": import doctest doctest.testmod()
26
from ...processing_utils import ProcessorMixin class lowercase_ ( UpperCamelCase_ ): """simple docstring""" UpperCAmelCase_ : List[Any] = ["""image_processor""", """feature_extractor"""] UpperCAmelCase_ : Optional[int] = """TvltImageProcessor""" UpperCAmelCase_ : Optional[int] = """TvltFeatureExtractor""" def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ->Optional[int]: super().__init__(image_processor=__SCREAMING_SNAKE_CASE , feature_extractor=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = image_processor lowerCAmelCase = feature_extractor def __call__( self , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=False , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) ->List[Any]: if images is None and audio is None: raise ValueError('''You need to specify either an `images` or `audio` input to process.''' ) lowerCAmelCase = None if images is not None: lowerCAmelCase = self.image_processor(__SCREAMING_SNAKE_CASE , mask_pixel=__SCREAMING_SNAKE_CASE , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) if images_mixed is not None: lowerCAmelCase = self.image_processor(__SCREAMING_SNAKE_CASE , is_mixed=__SCREAMING_SNAKE_CASE , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) if audio is not None: lowerCAmelCase = self.feature_extractor( __SCREAMING_SNAKE_CASE , *__SCREAMING_SNAKE_CASE , sampling_rate=__SCREAMING_SNAKE_CASE , mask_audio=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) lowerCAmelCase = {} if audio is not None: output_dict.update(__SCREAMING_SNAKE_CASE ) if images is not None: output_dict.update(__SCREAMING_SNAKE_CASE ) if images_mixed_dict is not None: output_dict.update(__SCREAMING_SNAKE_CASE ) return output_dict @property def SCREAMING_SNAKE_CASE_ ( self ) ->Any: lowerCAmelCase = self.image_processor.model_input_names lowerCAmelCase = self.feature_extractor.model_input_names return list(dict.fromkeys(image_processor_input_names + feature_extractor_input_names ) )
338
0
'''simple docstring''' import json import os from typing import Optional, Tuple from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging __lowercase : int = logging.get_logger(__name__) __lowercase : List[str] = {'vocab_file': 'vocab.json'} __lowercase : List[Any] = { 'vocab_file': { 'mgp-str': 'https://huggingface.co/alibaba-damo/mgp-str-base/blob/main/vocab.json', } } __lowercase : Tuple = {'mgp-str': 27} class __UpperCamelCase ( lowerCAmelCase_ ): A_ = VOCAB_FILES_NAMES A_ = PRETRAINED_VOCAB_FILES_MAP A_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES def __init__( self , __a , __a="[GO]" , __a="[GO]" , __a="[s]" , __a="[GO]" , **__a ): '''simple docstring''' super().__init__( unk_token=__a , bos_token=__a , eos_token=__a , pad_token=__a , **__a , ) with open(__a , encoding='utf-8' ) as vocab_handle: __a : Dict = json.load(__a ) __a : Union[str, Any] = {v: k for k, v in self.vocab.items()} @property def __UpperCAmelCase ( self ): '''simple docstring''' return len(self.vocab ) def __UpperCAmelCase ( self ): '''simple docstring''' return dict(self.vocab , **self.added_tokens_encoder ) def __UpperCAmelCase ( self , __a ): '''simple docstring''' __a : str = [] for s in text: char_tokens.extend(__a ) return char_tokens def __UpperCAmelCase ( self , __a ): '''simple docstring''' return self.vocab.get(__a , self.vocab.get(self.unk_token ) ) def __UpperCAmelCase ( self , __a ): '''simple docstring''' return self.decoder.get(__a ) def __UpperCAmelCase ( self , __a , __a = None ): '''simple docstring''' if not os.path.isdir(__a ): logger.error('Vocabulary path ({}) should be a directory'.format(__a ) ) return __a : Any = os.path.join( __a , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) with open(__a , 'w' , encoding='utf-8' ) as f: f.write(json.dumps(self.vocab , indent=2 , sort_keys=__a , ensure_ascii=__a ) + '\n' ) return (vocab_file,)
27
def SCREAMING_SNAKE_CASE_ ( snake_case__ ) -> List[str]: lowerCAmelCase = len(snake_case__ ) for i in range(length - 1 ): lowerCAmelCase = i for k in range(i + 1 , snake_case__ ): if collection[k] < collection[least]: lowerCAmelCase = k if least != i: lowerCAmelCase , lowerCAmelCase = (collection[i], collection[least]) return collection if __name__ == "__main__": lowercase__ : Optional[int] = input('''Enter numbers separated by a comma:\n''').strip() lowercase__ : str = [int(item) for item in user_input.split(''',''')] print(selection_sort(unsorted))
338
0
'''simple docstring''' import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, StableDiffusionAttendAndExcitePipeline, UNetaDConditionModel, ) from diffusers.utils import load_numpy, skip_mps, slow from diffusers.utils.testing_utils import require_torch_gpu from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin _lowerCamelCase : int = False @skip_mps class SCREAMING_SNAKE_CASE ( _a , _a , _a , unittest.TestCase ): """simple docstring""" _SCREAMING_SNAKE_CASE = StableDiffusionAttendAndExcitePipeline _SCREAMING_SNAKE_CASE = False _SCREAMING_SNAKE_CASE = TEXT_TO_IMAGE_PARAMS _SCREAMING_SNAKE_CASE = TEXT_TO_IMAGE_BATCH_PARAMS.union({"""token_indices"""} ) _SCREAMING_SNAKE_CASE = TEXT_TO_IMAGE_IMAGE_PARAMS _SCREAMING_SNAKE_CASE = TEXT_TO_IMAGE_IMAGE_PARAMS @classmethod def A ( cls : Union[str, Any] ): """simple docstring""" super().setUpClass() torch.use_deterministic_algorithms(UpperCamelCase__ ) @classmethod def A ( cls : Union[str, Any] ): """simple docstring""" super().tearDownClass() torch.use_deterministic_algorithms(UpperCamelCase__ ) def A ( self : Optional[int] ): """simple docstring""" torch.manual_seed(0 ) UpperCamelCase = UNetaDConditionModel( block_out_channels=(3_2, 6_4) , layers_per_block=1 , sample_size=3_2 , in_channels=4 , out_channels=4 , down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D') , up_block_types=('CrossAttnUpBlock2D', 'UpBlock2D') , cross_attention_dim=3_2 , attention_head_dim=(2, 4) , use_linear_projection=UpperCamelCase__ , ) UpperCamelCase = DDIMScheduler( beta_start=0.0_0_0_8_5 , beta_end=0.0_1_2 , beta_schedule='scaled_linear' , clip_sample=UpperCamelCase__ , set_alpha_to_one=UpperCamelCase__ , ) torch.manual_seed(0 ) UpperCamelCase = AutoencoderKL( block_out_channels=[3_2, 6_4] , in_channels=3 , out_channels=3 , down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D'] , up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D'] , latent_channels=4 , sample_size=1_2_8 , ) torch.manual_seed(0 ) UpperCamelCase = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=3_2 , intermediate_size=3_7 , layer_norm_eps=1E-0_5 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1_0_0_0 , hidden_act='gelu' , projection_dim=5_1_2 , ) UpperCamelCase = CLIPTextModel(UpperCamelCase__ ) UpperCamelCase = CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip' ) UpperCamelCase = { 'unet': unet, 'scheduler': scheduler, 'vae': vae, 'text_encoder': text_encoder, 'tokenizer': tokenizer, 'safety_checker': None, 'feature_extractor': None, } return components def A ( self : Dict , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : List[str]=0 ): """simple docstring""" if str(UpperCamelCase__ ).startswith('mps' ): UpperCamelCase = torch.manual_seed(UpperCamelCase__ ) else: UpperCamelCase = torch.Generator(device=UpperCamelCase__ ).manual_seed(UpperCamelCase__ ) UpperCamelCase = UpperCamelCase = { 'prompt': 'a cat and a frog', 'token_indices': [2, 5], 'generator': generator, 'num_inference_steps': 1, 'guidance_scale': 6.0, 'output_type': 'numpy', 'max_iter_to_alter': 2, 'thresholds': {0: 0.7}, } return inputs def A ( self : int ): """simple docstring""" UpperCamelCase = 'cpu' UpperCamelCase = self.get_dummy_components() UpperCamelCase = self.pipeline_class(**UpperCamelCase__ ) pipe.to(UpperCamelCase__ ) pipe.set_progress_bar_config(disable=UpperCamelCase__ ) UpperCamelCase = self.get_dummy_inputs(UpperCamelCase__ ) UpperCamelCase = pipe(**UpperCamelCase__ ).images UpperCamelCase = image[0, -3:, -3:, -1] self.assertEqual(image.shape , (1, 6_4, 6_4, 3) ) UpperCamelCase = np.array( [0.6_3_9_0_5_3_6_4, 0.6_2_8_9_7_3_0_7, 0.4_8_5_9_9_0_1_7, 0.5_1_3_3_6_2_4, 0.5_5_5_0_0_4_8, 0.4_5_7_6_9_5_1_6, 0.5_0_3_2_6_9_7_3, 0.5_0_2_3_1_3_9, 0.4_5_3_8_4_4_9_6] ) UpperCamelCase = np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(UpperCamelCase__ , 1E-3 ) def A ( self : Any ): """simple docstring""" super().test_cpu_offload_forward_pass(expected_max_diff=5E-4 ) def A ( self : int ): """simple docstring""" self._test_inference_batch_consistent(batch_sizes=[1, 2] ) def A ( self : Union[str, Any] ): """simple docstring""" self._test_inference_batch_single_identical(batch_size=2 , expected_max_diff=7E-4 ) def A ( self : Tuple ): """simple docstring""" super().test_dict_tuple_outputs_equivalent(expected_max_difference=3E-3 ) def A ( self : Optional[int] ): """simple docstring""" super().test_pt_np_pil_outputs_equivalent(expected_max_diff=5E-4 ) def A ( self : List[Any] ): """simple docstring""" super().test_save_load_local(expected_max_difference=5E-4 ) def A ( self : Optional[Any] ): """simple docstring""" super().test_save_load_optional_components(expected_max_difference=4E-4 ) @require_torch_gpu @slow class SCREAMING_SNAKE_CASE ( unittest.TestCase ): """simple docstring""" @classmethod def A ( cls : Union[str, Any] ): """simple docstring""" super().setUpClass() torch.use_deterministic_algorithms(UpperCamelCase__ ) @classmethod def A ( cls : Any ): """simple docstring""" super().tearDownClass() torch.use_deterministic_algorithms(UpperCamelCase__ ) def A ( self : int ): """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def A ( self : Tuple ): """simple docstring""" UpperCamelCase = torch.manual_seed(5_1 ) UpperCamelCase = StableDiffusionAttendAndExcitePipeline.from_pretrained( 'CompVis/stable-diffusion-v1-4' , safety_checker=UpperCamelCase__ , torch_dtype=torch.floataa ) pipe.to('cuda' ) UpperCamelCase = 'a painting of an elephant with glasses' UpperCamelCase = [5, 7] UpperCamelCase = pipe( prompt=UpperCamelCase__ , token_indices=UpperCamelCase__ , guidance_scale=7.5 , generator=UpperCamelCase__ , num_inference_steps=5 , max_iter_to_alter=5 , output_type='numpy' , ).images[0] UpperCamelCase = load_numpy( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/attend-and-excite/elephant_glasses.npy' ) assert np.abs((expected_image - image).max() ) < 5E-1
28
import unittest from transformers import EsmConfig, is_torch_available from transformers.testing_utils import TestCasePlus, require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers.models.esm.modeling_esmfold import EsmForProteinFolding class lowercase_ : """simple docstring""" def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=13 , __SCREAMING_SNAKE_CASE=7 , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=19 , __SCREAMING_SNAKE_CASE=32 , __SCREAMING_SNAKE_CASE=5 , __SCREAMING_SNAKE_CASE=4 , __SCREAMING_SNAKE_CASE=37 , __SCREAMING_SNAKE_CASE="gelu" , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=512 , __SCREAMING_SNAKE_CASE=16 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=0.0_2 , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=4 , __SCREAMING_SNAKE_CASE=None , ) ->Union[str, Any]: 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 SCREAMING_SNAKE_CASE_ ( self ) ->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 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, input_mask, sequence_labels, token_labels, choice_labels def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[int]: lowerCAmelCase = EsmConfig( vocab_size=33 , hidden_size=self.hidden_size , pad_token_id=1 , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , is_folding_model=__SCREAMING_SNAKE_CASE , esmfold_config={'''trunk''': {'''num_blocks''': 2}, '''fp16_esm''': False} , ) return config def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ->Tuple: lowerCAmelCase = EsmForProteinFolding(config=__SCREAMING_SNAKE_CASE ).float() model.to(__SCREAMING_SNAKE_CASE ) model.eval() lowerCAmelCase = model(__SCREAMING_SNAKE_CASE , attention_mask=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = model(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = model(__SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.positions.shape , (8, self.batch_size, self.seq_length, 14, 3) ) self.parent.assertEqual(result.angles.shape , (8, self.batch_size, self.seq_length, 7, 2) ) def SCREAMING_SNAKE_CASE_ ( self ) ->int: lowerCAmelCase = self.prepare_config_and_inputs() ( ( lowerCAmelCase ) , ( lowerCAmelCase ) , ( lowerCAmelCase ) , ( lowerCAmelCase ) , ( lowerCAmelCase ) , ( lowerCAmelCase ) , ) = config_and_inputs lowerCAmelCase = {'''input_ids''': input_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_torch class lowercase_ ( UpperCamelCase_ , UpperCamelCase_ , unittest.TestCase ): """simple docstring""" UpperCAmelCase_ : Optional[Any] = False UpperCAmelCase_ : Dict = (EsmForProteinFolding,) if is_torch_available() else () UpperCAmelCase_ : List[Any] = () UpperCAmelCase_ : Tuple = {} if is_torch_available() else {} UpperCAmelCase_ : List[str] = False def SCREAMING_SNAKE_CASE_ ( self ) ->Dict: lowerCAmelCase = EsmFoldModelTester(self ) lowerCAmelCase = ConfigTester(self , config_class=__SCREAMING_SNAKE_CASE , hidden_size=37 ) def SCREAMING_SNAKE_CASE_ ( self ) ->Any: self.config_tester.run_common_tests() def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[int]: lowerCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE ) @unittest.skip('''Does not support attention outputs''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->Tuple: pass @unittest.skip def SCREAMING_SNAKE_CASE_ ( self ) ->Any: pass @unittest.skip('''Esm does not support embedding resizing''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->List[str]: pass @unittest.skip('''Esm does not support embedding resizing''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->str: pass @unittest.skip('''ESMFold does not support passing input embeds!''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->List[Any]: pass @unittest.skip('''ESMFold does not support head pruning.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->str: pass @unittest.skip('''ESMFold does not support head pruning.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->Dict: pass @unittest.skip('''ESMFold does not support head pruning.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->List[str]: pass @unittest.skip('''ESMFold does not support head pruning.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->List[Any]: pass @unittest.skip('''ESMFold does not support head pruning.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[int]: pass @unittest.skip('''ESMFold does not output hidden states in the normal way.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->Tuple: pass @unittest.skip('''ESMfold does not output hidden states in the normal way.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->Dict: pass @unittest.skip('''ESMFold only has one output format.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->List[str]: pass @unittest.skip('''This test doesn\'t work for ESMFold and doesn\'t test core functionality''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->Dict: pass @unittest.skip('''ESMFold does not support input chunking.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->Union[str, Any]: pass @unittest.skip('''ESMFold doesn\'t respect you and it certainly doesn\'t respect your initialization arguments.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[Any]: pass @unittest.skip('''ESMFold doesn\'t support torchscript compilation.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->str: pass @unittest.skip('''ESMFold doesn\'t support torchscript compilation.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->Any: pass @unittest.skip('''ESMFold doesn\'t support torchscript compilation.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->str: pass @unittest.skip('''ESMFold doesn\'t support data parallel.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->str: pass @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[int]: pass @require_torch class lowercase_ ( UpperCamelCase_ ): """simple docstring""" @slow def SCREAMING_SNAKE_CASE_ ( self ) ->str: lowerCAmelCase = EsmForProteinFolding.from_pretrained('''facebook/esmfold_v1''' ).float() model.eval() lowerCAmelCase = torch.tensor([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]] ) lowerCAmelCase = model(__SCREAMING_SNAKE_CASE )['''positions'''] lowerCAmelCase = torch.tensor([2.5_8_2_8, 0.7_9_9_3, -1_0.9_3_3_4] , dtype=torch.floataa ) self.assertTrue(torch.allclose(position_outputs[0, 0, 0, 0] , __SCREAMING_SNAKE_CASE , atol=1e-4 ) )
338
0
from sklearn.metrics import fa_score, matthews_corrcoef import datasets from .record_evaluation import evaluate as evaluate_record __UpperCAmelCase = '\\n@article{wang2019superglue,\n title={SuperGLUE: A Stickier Benchmark for General-Purpose Language Understanding Systems},\n author={Wang, Alex and Pruksachatkun, Yada and Nangia, Nikita and Singh, Amanpreet and Michael, Julian and Hill, Felix and Levy, Omer and Bowman, Samuel R},\n journal={arXiv preprint arXiv:1905.00537},\n year={2019}\n}\n' __UpperCAmelCase = '\\nSuperGLUE (https://super.gluebenchmark.com/) is a new benchmark styled after\nGLUE with a new set of more difficult language understanding tasks, improved\nresources, and a new public leaderboard.\n' __UpperCAmelCase = '\nCompute SuperGLUE evaluation metric associated to each SuperGLUE dataset.\nArgs:\n predictions: list of predictions to score. Depending on the SuperGlUE subset:\n - for \'record\': list of question-answer dictionaries with the following keys:\n - \'idx\': index of the question as specified by the dataset\n - \'prediction_text\': the predicted answer text\n - for \'multirc\': list of question-answer dictionaries with the following keys:\n - \'idx\': index of the question-answer pair as specified by the dataset\n - \'prediction\': the predicted answer label\n - otherwise: list of predicted labels\n references: list of reference labels. Depending on the SuperGLUE subset:\n - for \'record\': list of question-answers dictionaries with the following keys:\n - \'idx\': index of the question as specified by the dataset\n - \'answers\': list of possible answers\n - otherwise: list of reference labels\nReturns: depending on the SuperGLUE subset:\n - for \'record\':\n - \'exact_match\': Exact match between answer and gold answer\n - \'f1\': F1 score\n - for \'multirc\':\n - \'exact_match\': Exact match between answer and gold answer\n - \'f1_m\': Per-question macro-F1 score\n - \'f1_a\': Average F1 score over all answers\n - for \'axb\':\n \'matthews_correlation\': Matthew Correlation\n - for \'cb\':\n - \'accuracy\': Accuracy\n - \'f1\': F1 score\n - for all others:\n - \'accuracy\': Accuracy\nExamples:\n\n >>> super_glue_metric = datasets.load_metric(\'super_glue\', \'copa\') # any of ["copa", "rte", "wic", "wsc", "wsc.fixed", "boolq", "axg"]\n >>> predictions = [0, 1]\n >>> references = [0, 1]\n >>> results = super_glue_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {\'accuracy\': 1.0}\n\n >>> super_glue_metric = datasets.load_metric(\'super_glue\', \'cb\')\n >>> predictions = [0, 1]\n >>> references = [0, 1]\n >>> results = super_glue_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {\'accuracy\': 1.0, \'f1\': 1.0}\n\n >>> super_glue_metric = datasets.load_metric(\'super_glue\', \'record\')\n >>> predictions = [{\'idx\': {\'passage\': 0, \'query\': 0}, \'prediction_text\': \'answer\'}]\n >>> references = [{\'idx\': {\'passage\': 0, \'query\': 0}, \'answers\': [\'answer\', \'another_answer\']}]\n >>> results = super_glue_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {\'exact_match\': 1.0, \'f1\': 1.0}\n\n >>> super_glue_metric = datasets.load_metric(\'super_glue\', \'multirc\')\n >>> predictions = [{\'idx\': {\'answer\': 0, \'paragraph\': 0, \'question\': 0}, \'prediction\': 0}, {\'idx\': {\'answer\': 1, \'paragraph\': 2, \'question\': 3}, \'prediction\': 1}]\n >>> references = [0, 1]\n >>> results = super_glue_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {\'exact_match\': 1.0, \'f1_m\': 1.0, \'f1_a\': 1.0}\n\n >>> super_glue_metric = datasets.load_metric(\'super_glue\', \'axb\')\n >>> references = [0, 1]\n >>> predictions = [0, 1]\n >>> results = super_glue_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {\'matthews_correlation\': 1.0}\n' def lowercase__ ( __snake_case : int , __snake_case : Dict ): '''simple docstring''' return float((preds == labels).mean() ) def lowercase__ ( __snake_case : Tuple , __snake_case : Union[str, Any] , __snake_case : Union[str, Any]="binary" ): '''simple docstring''' UpperCAmelCase_ : str = simple_accuracy(__snake_case , __snake_case ) UpperCAmelCase_ : Dict = float(fa_score(y_true=__snake_case , y_pred=__snake_case , average=__snake_case ) ) return { "accuracy": acc, "f1": fa, } def lowercase__ ( __snake_case : Optional[Any] , __snake_case : List[str] ): '''simple docstring''' UpperCAmelCase_ : Tuple = {} for id_pred, label in zip(__snake_case , __snake_case ): UpperCAmelCase_ : str = F"{id_pred['idx']['paragraph']}-{id_pred['idx']['question']}" UpperCAmelCase_ : Tuple = id_pred['prediction'] if question_id in question_map: question_map[question_id].append((pred, label) ) else: UpperCAmelCase_ : List[str] = [(pred, label)] UpperCAmelCase_ , UpperCAmelCase_ : List[str] = [], [] for question, preds_labels in question_map.items(): UpperCAmelCase_ , UpperCAmelCase_ : str = zip(*__snake_case ) UpperCAmelCase_ : List[Any] = fa_score(y_true=__snake_case , y_pred=__snake_case , average='macro' ) fas.append(__snake_case ) UpperCAmelCase_ : int = int(sum(pred == label for pred, label in preds_labels ) == len(__snake_case ) ) ems.append(__snake_case ) UpperCAmelCase_ : int = float(sum(__snake_case ) / len(__snake_case ) ) UpperCAmelCase_ : Optional[int] = sum(__snake_case ) / len(__snake_case ) UpperCAmelCase_ : List[str] = float(fa_score(y_true=__snake_case , y_pred=[id_pred['prediction'] for id_pred in ids_preds] ) ) return {"exact_match": em, "f1_m": fa_m, "f1_a": fa_a} @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class lowerCamelCase (datasets.Metric ): '''simple docstring''' def __UpperCAmelCase ( self ) -> Any: if self.config_name not in [ "boolq", "cb", "copa", "multirc", "record", "rte", "wic", "wsc", "wsc.fixed", "axb", "axg", ]: raise KeyError( 'You should supply a configuration name selected in ' '["boolq", "cb", "copa", "multirc", "record", "rte", "wic", "wsc", "wsc.fixed", "axb", "axg",]' ) return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features(self._get_feature_types() ) , codebase_urls=[] , reference_urls=[] , format='numpy' if not self.config_name == 'record' and not self.config_name == 'multirc' else None , ) def __UpperCAmelCase ( self ) -> List[Any]: if self.config_name == "record": return { "predictions": { "idx": { "passage": datasets.Value('int64' ), "query": datasets.Value('int64' ), }, "prediction_text": datasets.Value('string' ), }, "references": { "idx": { "passage": datasets.Value('int64' ), "query": datasets.Value('int64' ), }, "answers": datasets.Sequence(datasets.Value('string' ) ), }, } elif self.config_name == "multirc": return { "predictions": { "idx": { "answer": datasets.Value('int64' ), "paragraph": datasets.Value('int64' ), "question": datasets.Value('int64' ), }, "prediction": datasets.Value('int64' ), }, "references": datasets.Value('int64' ), } else: return { "predictions": datasets.Value('int64' ), "references": datasets.Value('int64' ), } def __UpperCAmelCase ( self , _UpperCamelCase , _UpperCamelCase ) -> Optional[int]: if self.config_name == "axb": return {"matthews_correlation": matthews_corrcoef(_UpperCamelCase , _UpperCamelCase )} elif self.config_name == "cb": return acc_and_fa(_UpperCamelCase , _UpperCamelCase , fa_avg='macro' ) elif self.config_name == "record": UpperCAmelCase_ : str = [ { 'qas': [ {'id': ref['idx']['query'], 'answers': [{'text': ans} for ans in ref['answers']]} for ref in references ] } ] UpperCAmelCase_ : Any = {pred['idx']['query']: pred['prediction_text'] for pred in predictions} return evaluate_record(_UpperCamelCase , _UpperCamelCase )[0] elif self.config_name == "multirc": return evaluate_multirc(_UpperCamelCase , _UpperCamelCase ) elif self.config_name in ["copa", "rte", "wic", "wsc", "wsc.fixed", "boolq", "axg"]: return {"accuracy": simple_accuracy(_UpperCamelCase , _UpperCamelCase )} else: raise KeyError( 'You should supply a configuration name selected in ' '["boolq", "cb", "copa", "multirc", "record", "rte", "wic", "wsc", "wsc.fixed", "axb", "axg",]' )
29
import warnings from typing import List import numpy as np from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding from ...utils import is_flax_available, is_tf_available, is_torch_available class lowercase_ ( UpperCamelCase_ ): """simple docstring""" UpperCAmelCase_ : List[str] = ["""image_processor""", """tokenizer"""] UpperCAmelCase_ : int = """OwlViTImageProcessor""" UpperCAmelCase_ : Any = ("""CLIPTokenizer""", """CLIPTokenizerFast""") def __init__( self , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE ) ->Any: lowerCAmelCase = None if "feature_extractor" in kwargs: warnings.warn( '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`''' ''' instead.''' , __SCREAMING_SNAKE_CASE , ) lowerCAmelCase = kwargs.pop('''feature_extractor''' ) lowerCAmelCase = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError('''You need to specify an `image_processor`.''' ) if tokenizer is None: raise ValueError('''You need to specify a `tokenizer`.''' ) super().__init__(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def __call__( self , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE="max_length" , __SCREAMING_SNAKE_CASE="np" , **__SCREAMING_SNAKE_CASE ) ->int: if text is None and query_images is None and images is None: raise ValueError( '''You have to specify at least one text or query image or image. All three cannot be none.''' ) if text is not None: if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) or (isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) and not isinstance(text[0] , __SCREAMING_SNAKE_CASE )): lowerCAmelCase = [self.tokenizer(__SCREAMING_SNAKE_CASE , padding=__SCREAMING_SNAKE_CASE , return_tensors=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE )] elif isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) and isinstance(text[0] , __SCREAMING_SNAKE_CASE ): lowerCAmelCase = [] # Maximum number of queries across batch lowerCAmelCase = max([len(__SCREAMING_SNAKE_CASE ) for t in text] ) # Pad all batch samples to max number of text queries for t in text: if len(__SCREAMING_SNAKE_CASE ) != max_num_queries: lowerCAmelCase = t + [''' '''] * (max_num_queries - len(__SCREAMING_SNAKE_CASE )) lowerCAmelCase = self.tokenizer(__SCREAMING_SNAKE_CASE , padding=__SCREAMING_SNAKE_CASE , return_tensors=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) encodings.append(__SCREAMING_SNAKE_CASE ) else: raise TypeError('''Input text should be a string, a list of strings or a nested list of strings''' ) if return_tensors == "np": lowerCAmelCase = np.concatenate([encoding['''input_ids'''] for encoding in encodings] , axis=0 ) lowerCAmelCase = np.concatenate([encoding['''attention_mask'''] for encoding in encodings] , axis=0 ) elif return_tensors == "jax" and is_flax_available(): import jax.numpy as jnp lowerCAmelCase = jnp.concatenate([encoding['''input_ids'''] for encoding in encodings] , axis=0 ) lowerCAmelCase = jnp.concatenate([encoding['''attention_mask'''] for encoding in encodings] , axis=0 ) elif return_tensors == "pt" and is_torch_available(): import torch lowerCAmelCase = torch.cat([encoding['''input_ids'''] for encoding in encodings] , dim=0 ) lowerCAmelCase = torch.cat([encoding['''attention_mask'''] for encoding in encodings] , dim=0 ) elif return_tensors == "tf" and is_tf_available(): import tensorflow as tf lowerCAmelCase = tf.stack([encoding['''input_ids'''] for encoding in encodings] , axis=0 ) lowerCAmelCase = tf.stack([encoding['''attention_mask'''] for encoding in encodings] , axis=0 ) else: raise ValueError('''Target return tensor type could not be returned''' ) lowerCAmelCase = BatchEncoding() lowerCAmelCase = input_ids lowerCAmelCase = attention_mask if query_images is not None: lowerCAmelCase = BatchEncoding() lowerCAmelCase = self.image_processor( __SCREAMING_SNAKE_CASE , return_tensors=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).pixel_values lowerCAmelCase = query_pixel_values if images is not None: lowerCAmelCase = self.image_processor(__SCREAMING_SNAKE_CASE , return_tensors=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) if text is not None and images is not None: lowerCAmelCase = image_features.pixel_values return encoding elif query_images is not None and images is not None: lowerCAmelCase = image_features.pixel_values return encoding elif text is not None or query_images is not None: return encoding else: return BatchEncoding(data=dict(**__SCREAMING_SNAKE_CASE ) , tensor_type=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) ->Optional[int]: return self.image_processor.post_process(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) ->Any: return self.image_processor.post_process_object_detection(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) ->Tuple: return self.image_processor.post_process_image_guided_detection(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) ->str: return self.tokenizer.batch_decode(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) ->List[Any]: return self.tokenizer.decode(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) @property def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[int]: warnings.warn( '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.''' , __SCREAMING_SNAKE_CASE , ) return self.image_processor_class @property def SCREAMING_SNAKE_CASE_ ( self ) ->int: warnings.warn( '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.''' , __SCREAMING_SNAKE_CASE , ) return self.image_processor
338
0
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available, is_vision_available, ) __a = { 'configuration_convnext': ['CONVNEXT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'ConvNextConfig', 'ConvNextOnnxConfig'] } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a = ['ConvNextFeatureExtractor'] __a = ['ConvNextImageProcessor'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a = [ 'CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LIST', 'ConvNextForImageClassification', 'ConvNextModel', 'ConvNextPreTrainedModel', 'ConvNextBackbone', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a = [ 'TFConvNextForImageClassification', 'TFConvNextModel', 'TFConvNextPreTrainedModel', ] if TYPE_CHECKING: from .configuration_convnext import CONVNEXT_PRETRAINED_CONFIG_ARCHIVE_MAP, ConvNextConfig, ConvNextOnnxConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_convnext import ConvNextFeatureExtractor from .image_processing_convnext import ConvNextImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_convnext import ( CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LIST, ConvNextBackbone, ConvNextForImageClassification, ConvNextModel, ConvNextPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_convnext import TFConvNextForImageClassification, TFConvNextModel, TFConvNextPreTrainedModel else: import sys __a = _LazyModule(__name__, globals()['__file__'], _import_structure)
30
import os import unicodedata from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging lowercase__ : List[Any] = logging.get_logger(__name__) lowercase__ : Optional[Any] = {'''vocab_file''': '''spiece.model'''} lowercase__ : Optional[int] = { '''vocab_file''': { '''albert-base-v1''': '''https://huggingface.co/albert-base-v1/resolve/main/spiece.model''', '''albert-large-v1''': '''https://huggingface.co/albert-large-v1/resolve/main/spiece.model''', '''albert-xlarge-v1''': '''https://huggingface.co/albert-xlarge-v1/resolve/main/spiece.model''', '''albert-xxlarge-v1''': '''https://huggingface.co/albert-xxlarge-v1/resolve/main/spiece.model''', '''albert-base-v2''': '''https://huggingface.co/albert-base-v2/resolve/main/spiece.model''', '''albert-large-v2''': '''https://huggingface.co/albert-large-v2/resolve/main/spiece.model''', '''albert-xlarge-v2''': '''https://huggingface.co/albert-xlarge-v2/resolve/main/spiece.model''', '''albert-xxlarge-v2''': '''https://huggingface.co/albert-xxlarge-v2/resolve/main/spiece.model''', } } lowercase__ : Any = { '''albert-base-v1''': 5_1_2, '''albert-large-v1''': 5_1_2, '''albert-xlarge-v1''': 5_1_2, '''albert-xxlarge-v1''': 5_1_2, '''albert-base-v2''': 5_1_2, '''albert-large-v2''': 5_1_2, '''albert-xlarge-v2''': 5_1_2, '''albert-xxlarge-v2''': 5_1_2, } lowercase__ : Tuple = '''▁''' class lowercase_ ( UpperCamelCase_ ): """simple docstring""" UpperCAmelCase_ : Dict = VOCAB_FILES_NAMES UpperCAmelCase_ : Tuple = PRETRAINED_VOCAB_FILES_MAP UpperCAmelCase_ : Optional[int] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE="[CLS]" , __SCREAMING_SNAKE_CASE="[SEP]" , __SCREAMING_SNAKE_CASE="<unk>" , __SCREAMING_SNAKE_CASE="[SEP]" , __SCREAMING_SNAKE_CASE="<pad>" , __SCREAMING_SNAKE_CASE="[CLS]" , __SCREAMING_SNAKE_CASE="[MASK]" , __SCREAMING_SNAKE_CASE = None , **__SCREAMING_SNAKE_CASE , ) ->None: # Mask token behave like a normal word, i.e. include the space before it and # is included in the raw text, there should be a match in a non-normalized sentence. lowerCAmelCase = ( AddedToken(__SCREAMING_SNAKE_CASE , lstrip=__SCREAMING_SNAKE_CASE , rstrip=__SCREAMING_SNAKE_CASE , normalized=__SCREAMING_SNAKE_CASE ) if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else mask_token ) lowerCAmelCase = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( do_lower_case=__SCREAMING_SNAKE_CASE , remove_space=__SCREAMING_SNAKE_CASE , keep_accents=__SCREAMING_SNAKE_CASE , bos_token=__SCREAMING_SNAKE_CASE , eos_token=__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 , sp_model_kwargs=self.sp_model_kwargs , **__SCREAMING_SNAKE_CASE , ) lowerCAmelCase = do_lower_case lowerCAmelCase = remove_space lowerCAmelCase = keep_accents lowerCAmelCase = vocab_file lowerCAmelCase = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(__SCREAMING_SNAKE_CASE ) @property def SCREAMING_SNAKE_CASE_ ( self ) ->Any: return len(self.sp_model ) def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[Any]: 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 , __SCREAMING_SNAKE_CASE ) ->Tuple: lowerCAmelCase = d # for backward compatibility if not hasattr(self , '''sp_model_kwargs''' ): lowerCAmelCase = {} lowerCAmelCase = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE ) ->Any: if self.remove_space: lowerCAmelCase = ''' '''.join(inputs.strip().split() ) else: lowerCAmelCase = inputs lowerCAmelCase = outputs.replace('''``''' , '''"''' ).replace('''\'\'''' , '''"''' ) if not self.keep_accents: lowerCAmelCase = unicodedata.normalize('''NFKD''' , __SCREAMING_SNAKE_CASE ) lowerCAmelCase = ''''''.join([c for c in outputs if not unicodedata.combining(__SCREAMING_SNAKE_CASE )] ) if self.do_lower_case: lowerCAmelCase = outputs.lower() return outputs def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE ) ->List[str]: lowerCAmelCase = self.preprocess_text(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = self.sp_model.encode(__SCREAMING_SNAKE_CASE , out_type=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = [] for piece in pieces: if len(__SCREAMING_SNAKE_CASE ) > 1 and piece[-1] == str(''',''' ) and piece[-2].isdigit(): lowerCAmelCase = self.sp_model.EncodeAsPieces(piece[:-1].replace(__SCREAMING_SNAKE_CASE , '''''' ) ) if piece[0] != SPIECE_UNDERLINE and cur_pieces[0][0] == SPIECE_UNDERLINE: if len(cur_pieces[0] ) == 1: lowerCAmelCase = cur_pieces[1:] else: lowerCAmelCase = cur_pieces[0][1:] cur_pieces.append(piece[-1] ) new_pieces.extend(__SCREAMING_SNAKE_CASE ) else: new_pieces.append(__SCREAMING_SNAKE_CASE ) return new_pieces def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE ) ->int: return self.sp_model.PieceToId(__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE ) ->int: return self.sp_model.IdToPiece(__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE ) ->Optional[int]: lowerCAmelCase = [] lowerCAmelCase = '''''' lowerCAmelCase = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: if not prev_is_special: out_string += " " out_string += self.sp_model.decode(__SCREAMING_SNAKE_CASE ) + token lowerCAmelCase = True lowerCAmelCase = [] else: current_sub_tokens.append(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = False out_string += self.sp_model.decode(__SCREAMING_SNAKE_CASE ) return out_string.strip() def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ) ->List[int]: lowerCAmelCase = [self.sep_token_id] lowerCAmelCase = [self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = False ) ->List[int]: 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 not None: return [1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1] return [1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1] def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ) ->List[int]: 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 ) * [0] + len(token_ids_a + sep ) * [1] def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ) ->Tuple[str]: 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 ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , __SCREAMING_SNAKE_CASE ) elif not os.path.isfile(self.vocab_file ): with open(__SCREAMING_SNAKE_CASE , '''wb''' ) as fi: lowerCAmelCase = self.sp_model.serialized_model_proto() fi.write(__SCREAMING_SNAKE_CASE ) return (out_vocab_file,)
338
0
'''simple docstring''' __SCREAMING_SNAKE_CASE : int = """Alexander Joslin""" import operator as op from .stack import Stack def UpperCamelCase_ ( _UpperCAmelCase : str ) -> int: """simple docstring""" _UpperCAmelCase : Tuple = {"*": op.mul, "/": op.truediv, "+": op.add, "-": op.sub} _UpperCAmelCase : Stack[int] = Stack() _UpperCAmelCase : Stack[str] = Stack() for i in equation: if i.isdigit(): # RULE 1 operand_stack.push(int(_UpperCAmelCase ) ) elif i in operators: # RULE 2 operator_stack.push(_UpperCAmelCase ) elif i == ")": # RULE 4 _UpperCAmelCase : str = operator_stack.peek() operator_stack.pop() _UpperCAmelCase : List[str] = operand_stack.peek() operand_stack.pop() _UpperCAmelCase : List[str] = operand_stack.peek() operand_stack.pop() _UpperCAmelCase : List[Any] = operators[opr](_UpperCAmelCase , _UpperCAmelCase ) operand_stack.push(_UpperCAmelCase ) # RULE 5 return operand_stack.peek() if __name__ == "__main__": __SCREAMING_SNAKE_CASE : List[str] = """(5 + ((4 * 2) * (2 + 3)))""" # answer = 45 print(F'{equation} = {dijkstras_two_stack_algorithm(equation)}')
31
import tempfile import torch from diffusers import ( DEISMultistepScheduler, DPMSolverMultistepScheduler, DPMSolverSinglestepScheduler, UniPCMultistepScheduler, ) from .test_schedulers import SchedulerCommonTest class lowercase_ ( UpperCamelCase_ ): """simple docstring""" UpperCAmelCase_ : List[Any] = (DEISMultistepScheduler,) UpperCAmelCase_ : int = (("""num_inference_steps""", 25),) def SCREAMING_SNAKE_CASE_ ( self , **__SCREAMING_SNAKE_CASE ) ->str: lowerCAmelCase = { '''num_train_timesteps''': 1000, '''beta_start''': 0.0_0_0_1, '''beta_end''': 0.0_2, '''beta_schedule''': '''linear''', '''solver_order''': 2, } config.update(**__SCREAMING_SNAKE_CASE ) return config def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE=0 , **__SCREAMING_SNAKE_CASE ) ->Tuple: lowerCAmelCase = dict(self.forward_default_kwargs ) lowerCAmelCase = kwargs.pop('''num_inference_steps''' , __SCREAMING_SNAKE_CASE ) lowerCAmelCase = self.dummy_sample lowerCAmelCase = 0.1 * sample lowerCAmelCase = [residual + 0.2, residual + 0.1_5, residual + 0.1_0] for scheduler_class in self.scheduler_classes: lowerCAmelCase = self.get_scheduler_config(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) # copy over dummy past residuals lowerCAmelCase = dummy_past_residuals[: scheduler.config.solver_order] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = scheduler_class.from_pretrained(__SCREAMING_SNAKE_CASE ) new_scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) # copy over dummy past residuals lowerCAmelCase = dummy_past_residuals[: new_scheduler.config.solver_order] lowerCAmelCase , lowerCAmelCase = sample, sample for t in range(__SCREAMING_SNAKE_CASE , time_step + scheduler.config.solver_order + 1 ): lowerCAmelCase = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).prev_sample lowerCAmelCase = new_scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1e-5, "Scheduler outputs are not identical" def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[int]: pass def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE=0 , **__SCREAMING_SNAKE_CASE ) ->List[Any]: lowerCAmelCase = dict(self.forward_default_kwargs ) lowerCAmelCase = kwargs.pop('''num_inference_steps''' , __SCREAMING_SNAKE_CASE ) lowerCAmelCase = self.dummy_sample lowerCAmelCase = 0.1 * sample lowerCAmelCase = [residual + 0.2, residual + 0.1_5, residual + 0.1_0] for scheduler_class in self.scheduler_classes: lowerCAmelCase = self.get_scheduler_config() lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) # copy over dummy past residuals (must be after setting timesteps) lowerCAmelCase = dummy_past_residuals[: scheduler.config.solver_order] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = scheduler_class.from_pretrained(__SCREAMING_SNAKE_CASE ) # copy over dummy past residuals new_scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) # copy over dummy past residual (must be after setting timesteps) lowerCAmelCase = dummy_past_residuals[: new_scheduler.config.solver_order] lowerCAmelCase = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).prev_sample lowerCAmelCase = new_scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1e-5, "Scheduler outputs are not identical" def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE ) ->List[Any]: if scheduler is None: lowerCAmelCase = self.scheduler_classes[0] lowerCAmelCase = self.get_scheduler_config(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = self.scheduler_classes[0] lowerCAmelCase = self.get_scheduler_config(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = 10 lowerCAmelCase = self.dummy_model() lowerCAmelCase = self.dummy_sample_deter scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) for i, t in enumerate(scheduler.timesteps ): lowerCAmelCase = model(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) lowerCAmelCase = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ).prev_sample return sample def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[int]: lowerCAmelCase = dict(self.forward_default_kwargs ) lowerCAmelCase = kwargs.pop('''num_inference_steps''' , __SCREAMING_SNAKE_CASE ) for scheduler_class in self.scheduler_classes: lowerCAmelCase = self.get_scheduler_config() lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = self.dummy_sample lowerCAmelCase = 0.1 * sample if num_inference_steps is not None and hasattr(__SCREAMING_SNAKE_CASE , '''set_timesteps''' ): scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) elif num_inference_steps is not None and not hasattr(__SCREAMING_SNAKE_CASE , '''set_timesteps''' ): lowerCAmelCase = num_inference_steps # copy over dummy past residuals (must be done after set_timesteps) lowerCAmelCase = [residual + 0.2, residual + 0.1_5, residual + 0.1_0] lowerCAmelCase = dummy_past_residuals[: scheduler.config.solver_order] lowerCAmelCase = scheduler.timesteps[5] lowerCAmelCase = scheduler.timesteps[6] lowerCAmelCase = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).prev_sample lowerCAmelCase = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).prev_sample self.assertEqual(output_a.shape , sample.shape ) self.assertEqual(output_a.shape , output_a.shape ) def SCREAMING_SNAKE_CASE_ ( self ) ->int: # make sure that iterating over schedulers with same config names gives same results # for defaults lowerCAmelCase = DEISMultistepScheduler(**self.get_scheduler_config() ) lowerCAmelCase = self.full_loop(scheduler=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = torch.mean(torch.abs(__SCREAMING_SNAKE_CASE ) ) assert abs(result_mean.item() - 0.2_3_9_1_6 ) < 1e-3 lowerCAmelCase = DPMSolverSinglestepScheduler.from_config(scheduler.config ) lowerCAmelCase = DPMSolverMultistepScheduler.from_config(scheduler.config ) lowerCAmelCase = UniPCMultistepScheduler.from_config(scheduler.config ) lowerCAmelCase = DEISMultistepScheduler.from_config(scheduler.config ) lowerCAmelCase = self.full_loop(scheduler=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = torch.mean(torch.abs(__SCREAMING_SNAKE_CASE ) ) assert abs(result_mean.item() - 0.2_3_9_1_6 ) < 1e-3 def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[int]: for timesteps in [25, 50, 100, 999, 1000]: self.check_over_configs(num_train_timesteps=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->int: self.check_over_configs(thresholding=__SCREAMING_SNAKE_CASE ) for order in [1, 2, 3]: for solver_type in ["logrho"]: for threshold in [0.5, 1.0, 2.0]: for prediction_type in ["epsilon", "sample"]: self.check_over_configs( thresholding=__SCREAMING_SNAKE_CASE , prediction_type=__SCREAMING_SNAKE_CASE , sample_max_value=__SCREAMING_SNAKE_CASE , algorithm_type='''deis''' , solver_order=__SCREAMING_SNAKE_CASE , solver_type=__SCREAMING_SNAKE_CASE , ) def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[Any]: for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->Union[str, Any]: for algorithm_type in ["deis"]: for solver_type in ["logrho"]: for order in [1, 2, 3]: for prediction_type in ["epsilon", "sample"]: self.check_over_configs( solver_order=__SCREAMING_SNAKE_CASE , solver_type=__SCREAMING_SNAKE_CASE , prediction_type=__SCREAMING_SNAKE_CASE , algorithm_type=__SCREAMING_SNAKE_CASE , ) lowerCAmelCase = self.full_loop( solver_order=__SCREAMING_SNAKE_CASE , solver_type=__SCREAMING_SNAKE_CASE , prediction_type=__SCREAMING_SNAKE_CASE , algorithm_type=__SCREAMING_SNAKE_CASE , ) assert not torch.isnan(__SCREAMING_SNAKE_CASE ).any(), "Samples have nan numbers" def SCREAMING_SNAKE_CASE_ ( self ) ->List[Any]: self.check_over_configs(lower_order_final=__SCREAMING_SNAKE_CASE ) self.check_over_configs(lower_order_final=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->List[Any]: for num_inference_steps in [1, 2, 3, 5, 10, 50, 100, 999, 1000]: self.check_over_forward(num_inference_steps=__SCREAMING_SNAKE_CASE , time_step=0 ) def SCREAMING_SNAKE_CASE_ ( self ) ->Dict: lowerCAmelCase = self.full_loop() lowerCAmelCase = torch.mean(torch.abs(__SCREAMING_SNAKE_CASE ) ) assert abs(result_mean.item() - 0.2_3_9_1_6 ) < 1e-3 def SCREAMING_SNAKE_CASE_ ( self ) ->List[str]: lowerCAmelCase = self.full_loop(prediction_type='''v_prediction''' ) lowerCAmelCase = torch.mean(torch.abs(__SCREAMING_SNAKE_CASE ) ) assert abs(result_mean.item() - 0.0_9_1 ) < 1e-3 def SCREAMING_SNAKE_CASE_ ( self ) ->Dict: lowerCAmelCase = self.scheduler_classes[0] lowerCAmelCase = self.get_scheduler_config(thresholding=__SCREAMING_SNAKE_CASE , dynamic_thresholding_ratio=0 ) lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = 10 lowerCAmelCase = self.dummy_model() lowerCAmelCase = self.dummy_sample_deter.half() scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) for i, t in enumerate(scheduler.timesteps ): lowerCAmelCase = model(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) lowerCAmelCase = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ).prev_sample assert sample.dtype == torch.floataa
338
0
def SCREAMING_SNAKE_CASE_ ( __A : list ) -> list: """simple docstring""" if len(__A ) <= 1: return lst a_ : List[Any] = 1 while i < len(__A ): if lst[i - 1] <= lst[i]: i += 1 else: a_ , a_ : Tuple = lst[i], lst[i - 1] i -= 1 if i == 0: a_ : Dict = 1 return lst if __name__ == "__main__": UpperCAmelCase_ : Any = input('Enter numbers separated by a comma:\n').strip() UpperCAmelCase_ : Any = [int(item) for item in user_input.split(',')] print(gnome_sort(unsorted))
32
import unittest import numpy as np import torch from diffusers import KarrasVePipeline, KarrasVeScheduler, UNetaDModel from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device enable_full_determinism() class lowercase_ ( unittest.TestCase ): """simple docstring""" @property def SCREAMING_SNAKE_CASE_ ( self ) ->List[str]: torch.manual_seed(0 ) lowerCAmelCase = UNetaDModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=3 , out_channels=3 , down_block_types=('''DownBlock2D''', '''AttnDownBlock2D''') , up_block_types=('''AttnUpBlock2D''', '''UpBlock2D''') , ) return model def SCREAMING_SNAKE_CASE_ ( self ) ->int: lowerCAmelCase = self.dummy_uncond_unet lowerCAmelCase = KarrasVeScheduler() lowerCAmelCase = KarrasVePipeline(unet=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = torch.manual_seed(0 ) lowerCAmelCase = pipe(num_inference_steps=2 , generator=__SCREAMING_SNAKE_CASE , output_type='''numpy''' ).images lowerCAmelCase = torch.manual_seed(0 ) lowerCAmelCase = pipe(num_inference_steps=2 , generator=__SCREAMING_SNAKE_CASE , output_type='''numpy''' , return_dict=__SCREAMING_SNAKE_CASE )[0] lowerCAmelCase = image[0, -3:, -3:, -1] lowerCAmelCase = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) lowerCAmelCase = np.array([0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 @slow @require_torch class lowercase_ ( unittest.TestCase ): """simple docstring""" def SCREAMING_SNAKE_CASE_ ( self ) ->List[Any]: lowerCAmelCase = '''google/ncsnpp-celebahq-256''' lowerCAmelCase = UNetaDModel.from_pretrained(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = KarrasVeScheduler() lowerCAmelCase = KarrasVePipeline(unet=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = torch.manual_seed(0 ) lowerCAmelCase = pipe(num_inference_steps=20 , generator=__SCREAMING_SNAKE_CASE , output_type='''numpy''' ).images lowerCAmelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 256, 256, 3) lowerCAmelCase = np.array([0.5_7_8, 0.5_8_1_1, 0.5_9_2_4, 0.5_8_0_9, 0.5_8_7, 0.5_8_8_6, 0.5_8_6_1, 0.5_8_0_2, 0.5_8_6] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
338
0
"""simple docstring""" def lowercase ( __snake_case : int , __snake_case : list[int] , __snake_case : int ): def count_of_possible_combinations(__snake_case : 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(__snake_case ) def lowercase ( __snake_case : int , __snake_case : list[int] , __snake_case : int ): def count_of_possible_combinations_with_dp_array( __snake_case : int , __snake_case : list[int] ) -> int: if target < 0: return 0 if target == 0: return 1 if dp_array[target] != -1: return dp_array[target] lowercase_ : Optional[int] = sum( count_of_possible_combinations_with_dp_array(target - item , __snake_case ) for item in array ) lowercase_ : Union[str, Any] = answer return answer lowercase_ : Dict = [-1] * (target + 1) return count_of_possible_combinations_with_dp_array(__snake_case , __snake_case ) def lowercase ( __snake_case : int , __snake_case : list[int] , __snake_case : int ): lowercase_ : List[Any] = [0] * (target + 1) lowercase_ : Optional[Any] = 1 for i in range(1 , target + 1 ): for j in range(__snake_case ): if i - array[j] >= 0: dp_array[i] += dp_array[i - array[j]] return dp_array[target] if __name__ == "__main__": import doctest doctest.testmod() __A : int = 3 __A : Union[str, Any] = 5 __A : List[str] = [1, 2, 5] print(combination_sum_iv(n, array, target))
33
from typing import Dict import numpy as np from ..utils import add_end_docstrings, is_tf_available, is_torch_available, logging from .base import PIPELINE_INIT_ARGS, GenericTensor, Pipeline, PipelineException if is_tf_available(): import tensorflow as tf from ..tf_utils import stable_softmax if is_torch_available(): import torch lowercase__ : Dict = logging.get_logger(__name__) @add_end_docstrings( UpperCamelCase_ , r""" top_k (`int`, defaults to 5): The number of predictions to return. targets (`str` or `List[str]`, *optional*): When passed, the model will limit the scores to the passed targets instead of looking up in the whole vocab. If the provided targets are not in the model vocab, they will be tokenized and the first resulting token will be used (with a warning, and that might be slower). """ , ) class lowercase_ ( UpperCamelCase_ ): """simple docstring""" def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE ) ->np.ndarray: if self.framework == "tf": lowerCAmelCase = tf.where(input_ids == self.tokenizer.mask_token_id ).numpy() elif self.framework == "pt": lowerCAmelCase = torch.nonzero(input_ids == self.tokenizer.mask_token_id , as_tuple=__SCREAMING_SNAKE_CASE ) else: raise ValueError('''Unsupported framework''' ) return masked_index def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE ) ->np.ndarray: lowerCAmelCase = self.get_masked_index(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = np.prod(masked_index.shape ) if numel < 1: raise PipelineException( '''fill-mask''' , self.model.base_model_prefix , F"No mask_token ({self.tokenizer.mask_token}) found on the input" , ) def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE ) ->str: if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): for model_input in model_inputs: self._ensure_exactly_one_mask_token(model_input['''input_ids'''][0] ) else: for input_ids in model_inputs["input_ids"]: self._ensure_exactly_one_mask_token(__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE ) ->Dict[str, GenericTensor]: if return_tensors is None: lowerCAmelCase = self.framework lowerCAmelCase = self.tokenizer(__SCREAMING_SNAKE_CASE , return_tensors=__SCREAMING_SNAKE_CASE ) self.ensure_exactly_one_mask_token(__SCREAMING_SNAKE_CASE ) return model_inputs def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE ) ->Tuple: lowerCAmelCase = self.model(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = model_inputs['''input_ids'''] return model_outputs def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=5 , __SCREAMING_SNAKE_CASE=None ) ->str: # Cap top_k if there are targets if target_ids is not None and target_ids.shape[0] < top_k: lowerCAmelCase = target_ids.shape[0] lowerCAmelCase = model_outputs['''input_ids'''][0] lowerCAmelCase = model_outputs['''logits'''] if self.framework == "tf": lowerCAmelCase = tf.where(input_ids == self.tokenizer.mask_token_id ).numpy()[:, 0] lowerCAmelCase = outputs.numpy() lowerCAmelCase = outputs[0, masked_index, :] lowerCAmelCase = stable_softmax(__SCREAMING_SNAKE_CASE , axis=-1 ) if target_ids is not None: lowerCAmelCase = tf.gather_nd(tf.squeeze(__SCREAMING_SNAKE_CASE , 0 ) , target_ids.reshape(-1 , 1 ) ) lowerCAmelCase = tf.expand_dims(__SCREAMING_SNAKE_CASE , 0 ) lowerCAmelCase = tf.math.top_k(__SCREAMING_SNAKE_CASE , k=__SCREAMING_SNAKE_CASE ) lowerCAmelCase , lowerCAmelCase = topk.values.numpy(), topk.indices.numpy() else: lowerCAmelCase = torch.nonzero(input_ids == self.tokenizer.mask_token_id , as_tuple=__SCREAMING_SNAKE_CASE ).squeeze(-1 ) # Fill mask pipeline supports only one ${mask_token} per sample lowerCAmelCase = outputs[0, masked_index, :] lowerCAmelCase = logits.softmax(dim=-1 ) if target_ids is not None: lowerCAmelCase = probs[..., target_ids] lowerCAmelCase , lowerCAmelCase = probs.topk(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = [] lowerCAmelCase = values.shape[0] == 1 for i, (_values, _predictions) in enumerate(zip(values.tolist() , predictions.tolist() ) ): lowerCAmelCase = [] for v, p in zip(_values , _predictions ): # Copy is important since we're going to modify this array in place lowerCAmelCase = input_ids.numpy().copy() if target_ids is not None: lowerCAmelCase = target_ids[p].tolist() lowerCAmelCase = p # Filter padding out: lowerCAmelCase = tokens[np.where(tokens != self.tokenizer.pad_token_id )] # Originally we skip special tokens to give readable output. # For multi masks though, the other [MASK] would be removed otherwise # making the output look odd, so we add them back lowerCAmelCase = self.tokenizer.decode(__SCREAMING_SNAKE_CASE , skip_special_tokens=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = {'''score''': v, '''token''': p, '''token_str''': self.tokenizer.decode([p] ), '''sequence''': sequence} row.append(__SCREAMING_SNAKE_CASE ) result.append(__SCREAMING_SNAKE_CASE ) if single_mask: return result[0] return result def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None ) ->Optional[Any]: if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): lowerCAmelCase = [targets] try: lowerCAmelCase = self.tokenizer.get_vocab() except Exception: lowerCAmelCase = {} lowerCAmelCase = [] for target in targets: lowerCAmelCase = vocab.get(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) if id_ is None: lowerCAmelCase = self.tokenizer( __SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , return_attention_mask=__SCREAMING_SNAKE_CASE , return_token_type_ids=__SCREAMING_SNAKE_CASE , max_length=1 , truncation=__SCREAMING_SNAKE_CASE , )['''input_ids'''] if len(__SCREAMING_SNAKE_CASE ) == 0: logger.warning( F"The specified target token `{target}` does not exist in the model vocabulary. " '''We cannot replace it with anything meaningful, ignoring it''' ) continue lowerCAmelCase = input_ids[0] # XXX: If users encounter this pass # it becomes pretty slow, so let's make sure # The warning enables them to fix the input to # get faster performance. logger.warning( F"The specified target token `{target}` does not exist in the model vocabulary. " F"Replacing with `{self.tokenizer.convert_ids_to_tokens(id_ )}`." ) target_ids.append(id_ ) lowerCAmelCase = list(set(__SCREAMING_SNAKE_CASE ) ) if len(__SCREAMING_SNAKE_CASE ) == 0: raise ValueError('''At least one target must be provided when passed.''' ) lowerCAmelCase = np.array(__SCREAMING_SNAKE_CASE ) return target_ids def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None ) ->Dict: lowerCAmelCase = {} if targets is not None: lowerCAmelCase = self.get_target_ids(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) lowerCAmelCase = target_ids if top_k is not None: lowerCAmelCase = top_k if self.tokenizer.mask_token_id is None: raise PipelineException( '''fill-mask''' , self.model.base_model_prefix , '''The tokenizer does not define a `mask_token`.''' ) return {}, {}, postprocess_params def __call__( self , __SCREAMING_SNAKE_CASE , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) ->List[Any]: lowerCAmelCase = super().__call__(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) and len(__SCREAMING_SNAKE_CASE ) == 1: return outputs[0] return outputs
338
0
'''simple docstring''' from __future__ import annotations def snake_case_ (_a : Dict , _a : str , _a : Optional[Any] , _a : List[str] ): # noqa: E741 while r - l > 1: UpperCAmelCase = (l + r) // 2 if v[m] >= key: UpperCAmelCase = m else: UpperCAmelCase = m # noqa: E741 return r def snake_case_ (_a : list[int] ): if len(_a ) == 0: return 0 UpperCAmelCase = [0] * len(_a ) UpperCAmelCase = 1 UpperCAmelCase = v[0] for i in range(1 , len(_a ) ): if v[i] < tail[0]: UpperCAmelCase = v[i] elif v[i] > tail[length - 1]: UpperCAmelCase = v[i] length += 1 else: UpperCAmelCase = v[i] return length if __name__ == "__main__": import doctest doctest.testmod()
34
from typing import TYPE_CHECKING from ...utils import _LazyModule lowercase__ : int = {'''tokenization_wav2vec2_phoneme''': ['''Wav2Vec2PhonemeCTCTokenizer''']} if TYPE_CHECKING: from .tokenization_wavaveca_phoneme import WavaVecaPhonemeCTCTokenizer else: import sys lowercase__ : Optional[int] = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
338
0
'''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 __a = logging.get_logger(__name__) __a = { "microsoft/beit-base-patch16-224-pt22k": ( "https://huggingface.co/microsoft/beit-base-patch16-224-pt22k/resolve/main/config.json" ), # See all BEiT models at https://huggingface.co/models?filter=beit } class UpperCAmelCase_ ( _a ): """simple docstring""" lowercase = "beit" def __init__( self : List[str] , snake_case_ : Union[str, Any]=8_192 , snake_case_ : int=768 , snake_case_ : Any=12 , snake_case_ : int=12 , snake_case_ : List[Any]=3_072 , snake_case_ : Optional[int]="gelu" , snake_case_ : str=0.0 , snake_case_ : Any=0.0 , snake_case_ : int=0.02 , snake_case_ : int=1E-1_2 , snake_case_ : Union[str, Any]=224 , snake_case_ : str=16 , snake_case_ : List[str]=3 , snake_case_ : List[str]=False , snake_case_ : List[Any]=False , snake_case_ : Union[str, Any]=False , snake_case_ : List[Any]=False , snake_case_ : List[str]=0.1 , snake_case_ : Optional[int]=0.1 , snake_case_ : Dict=True , snake_case_ : List[str]=[3, 5, 7, 11] , snake_case_ : Optional[Any]=[1, 2, 3, 6] , snake_case_ : str=True , snake_case_ : List[str]=0.4 , snake_case_ : List[Any]=256 , snake_case_ : str=1 , snake_case_ : Dict=False , snake_case_ : Optional[int]=255 , **snake_case_ : Optional[Any] , ): super().__init__(**snake_case_ ) snake_case__ : Any = vocab_size snake_case__ : Tuple = hidden_size snake_case__ : Optional[int] = num_hidden_layers snake_case__ : Optional[int] = num_attention_heads snake_case__ : Union[str, Any] = intermediate_size snake_case__ : List[Any] = hidden_act snake_case__ : int = hidden_dropout_prob snake_case__ : List[str] = attention_probs_dropout_prob snake_case__ : int = initializer_range snake_case__ : Dict = layer_norm_eps snake_case__ : List[Any] = image_size snake_case__ : List[Any] = patch_size snake_case__ : List[Any] = num_channels snake_case__ : Union[str, Any] = use_mask_token snake_case__ : List[str] = use_absolute_position_embeddings snake_case__ : List[Any] = use_relative_position_bias snake_case__ : List[str] = use_shared_relative_position_bias snake_case__ : Dict = layer_scale_init_value snake_case__ : Optional[int] = drop_path_rate snake_case__ : List[Any] = use_mean_pooling # decode head attributes (semantic segmentation) snake_case__ : Optional[int] = out_indices snake_case__ : str = pool_scales # auxiliary head attributes (semantic segmentation) snake_case__ : Dict = use_auxiliary_head snake_case__ : Any = auxiliary_loss_weight snake_case__ : int = auxiliary_channels snake_case__ : Optional[int] = auxiliary_num_convs snake_case__ : Optional[int] = auxiliary_concat_input snake_case__ : Tuple = semantic_loss_ignore_index class UpperCAmelCase_ ( _a ): """simple docstring""" lowercase = version.parse("1.11" ) @property def lowerCamelCase ( self : int ): return OrderedDict( [ ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}), ] ) @property def lowerCamelCase ( self : Dict ): return 1E-4
35
lowercase__ : Optional[int] = '''ABCDEFGHIJKLMNOPQRSTUVWXYZ''' def SCREAMING_SNAKE_CASE_ ( ) -> None: lowerCAmelCase = input('''Enter message: ''' ) lowerCAmelCase = input('''Enter key [alphanumeric]: ''' ) lowerCAmelCase = input('''Encrypt/Decrypt [e/d]: ''' ) if mode.lower().startswith('''e''' ): lowerCAmelCase = '''encrypt''' lowerCAmelCase = encrypt_message(snake_case__ , snake_case__ ) elif mode.lower().startswith('''d''' ): lowerCAmelCase = '''decrypt''' lowerCAmelCase = decrypt_message(snake_case__ , snake_case__ ) print(f"\n{mode.title()}ed message:" ) print(snake_case__ ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ ) -> str: return translate_message(snake_case__ , snake_case__ , '''encrypt''' ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ ) -> str: return translate_message(snake_case__ , snake_case__ , '''decrypt''' ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> str: lowerCAmelCase = [] lowerCAmelCase = 0 lowerCAmelCase = key.upper() for symbol in message: lowerCAmelCase = LETTERS.find(symbol.upper() ) if num != -1: if mode == "encrypt": num += LETTERS.find(key[key_index] ) elif mode == "decrypt": num -= LETTERS.find(key[key_index] ) num %= len(snake_case__ ) if symbol.isupper(): translated.append(LETTERS[num] ) elif symbol.islower(): translated.append(LETTERS[num].lower() ) key_index += 1 if key_index == len(snake_case__ ): lowerCAmelCase = 0 else: translated.append(snake_case__ ) return "".join(snake_case__ ) if __name__ == "__main__": main()
338
0
from __future__ import annotations def A ( _lowerCamelCase , _lowerCamelCase = None , _lowerCamelCase = None ): '''simple docstring''' if start is None: _lowerCAmelCase : Union[str, Any] = 0 if end is None: _lowerCAmelCase : int = len(_lowerCamelCase ) - 1 if start >= end: return _lowerCAmelCase : Union[str, Any] = (start + end) // 2 slowsort(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) slowsort(_lowerCamelCase , mid + 1 , _lowerCamelCase ) if sequence[end] < sequence[mid]: _lowerCAmelCase , _lowerCAmelCase : Union[str, Any] = sequence[mid], sequence[end] slowsort(_lowerCamelCase , _lowerCamelCase , end - 1 ) if __name__ == "__main__": from doctest import testmod testmod()
36
from collections import defaultdict from math import ceil, sqrt def SCREAMING_SNAKE_CASE_ ( snake_case__ = 1_0_0_0_0_0_0 , snake_case__ = 1_0 ) -> int: lowerCAmelCase = defaultdict(snake_case__ ) for outer_width in range(3 , (t_limit // 4) + 2 ): if outer_width * outer_width > t_limit: lowerCAmelCase = max( ceil(sqrt(outer_width * outer_width - t_limit ) ) , 1 ) else: lowerCAmelCase = 1 hole_width_lower_bound += (outer_width - hole_width_lower_bound) % 2 for hole_width in range(snake_case__ , outer_width - 1 , 2 ): count[outer_width * outer_width - hole_width * hole_width] += 1 return sum(1 for n in count.values() if 1 <= n <= 1_0 ) if __name__ == "__main__": print(f'{solution() = }')
338
0
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging _lowerCAmelCase = logging.get_logger(__name__) _lowerCAmelCase = { '''tiiuae/falcon-40b''': '''https://huggingface.co/tiiuae/falcon-40b/resolve/main/config.json''', '''tiiuae/falcon-7b''': '''https://huggingface.co/tiiuae/falcon-7b/resolve/main/config.json''', } class lowerCAmelCase_( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' __lowercase : Optional[Any] = '''falcon''' __lowercase : Optional[int] = ['''past_key_values'''] def __init__( self ,__UpperCAmelCase=6_5024 ,__UpperCAmelCase=4544 ,__UpperCAmelCase=32 ,__UpperCAmelCase=71 ,__UpperCAmelCase=1E-5 ,__UpperCAmelCase=0.0_2 ,__UpperCAmelCase=True ,__UpperCAmelCase=0.0 ,__UpperCAmelCase=0.0 ,__UpperCAmelCase=None ,__UpperCAmelCase=False ,__UpperCAmelCase=False ,__UpperCAmelCase=True ,__UpperCAmelCase=True ,__UpperCAmelCase=False ,__UpperCAmelCase=11 ,__UpperCAmelCase=11 ,**__UpperCAmelCase ,) -> Union[str, Any]: lowerCAmelCase__ : List[str] = vocab_size # Backward compatibility with n_embed kwarg lowerCAmelCase__ : List[str] = kwargs.pop("""n_embed""" ,__UpperCAmelCase ) lowerCAmelCase__ : List[Any] = hidden_size if n_embed is None else n_embed lowerCAmelCase__ : List[Any] = num_hidden_layers lowerCAmelCase__ : Optional[Any] = num_attention_heads lowerCAmelCase__ : str = layer_norm_epsilon lowerCAmelCase__ : int = initializer_range lowerCAmelCase__ : str = use_cache lowerCAmelCase__ : str = hidden_dropout lowerCAmelCase__ : Tuple = attention_dropout lowerCAmelCase__ : Tuple = bos_token_id lowerCAmelCase__ : Union[str, Any] = eos_token_id lowerCAmelCase__ : Any = num_attention_heads if num_kv_heads is None else num_kv_heads lowerCAmelCase__ : int = alibi lowerCAmelCase__ : Any = new_decoder_architecture lowerCAmelCase__ : Optional[int] = multi_query # Ignored when new_decoder_architecture is True lowerCAmelCase__ : Union[str, Any] = parallel_attn lowerCAmelCase__ : str = bias super().__init__(bos_token_id=__UpperCAmelCase ,eos_token_id=__UpperCAmelCase ,**__UpperCAmelCase ) @property def UpperCAmelCase_ ( self ) -> str: return self.hidden_size // self.num_attention_heads @property def UpperCAmelCase_ ( self ) -> Tuple: return not self.alibi
37
import pytest from datasets import Dataset, DatasetDict, Features, NamedSplit, Value from datasets.io.text import TextDatasetReader from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ ) -> Union[str, Any]: assert isinstance(snake_case__ , snake_case__ ) assert dataset.num_rows == 4 assert dataset.num_columns == 1 assert dataset.column_names == ["text"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize('''keep_in_memory''' , [False, True] ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> Union[str, Any]: lowerCAmelCase = tmp_path / '''cache''' lowerCAmelCase = {'''text''': '''string'''} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): lowerCAmelCase = TextDatasetReader(snake_case__ , cache_dir=snake_case__ , keep_in_memory=snake_case__ ).read() _check_text_dataset(snake_case__ , snake_case__ ) @pytest.mark.parametrize( '''features''' , [ None, {'''text''': '''string'''}, {'''text''': '''int32'''}, {'''text''': '''float32'''}, ] , ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> Optional[Any]: lowerCAmelCase = tmp_path / '''cache''' lowerCAmelCase = {'''text''': '''string'''} lowerCAmelCase = features.copy() if features else default_expected_features lowerCAmelCase = ( Features({feature: Value(snake_case__ ) for feature, dtype in features.items()} ) if features is not None else None ) lowerCAmelCase = TextDatasetReader(snake_case__ , features=snake_case__ , cache_dir=snake_case__ ).read() _check_text_dataset(snake_case__ , snake_case__ ) @pytest.mark.parametrize('''split''' , [None, NamedSplit('''train''' ), '''train''', '''test'''] ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> List[str]: lowerCAmelCase = tmp_path / '''cache''' lowerCAmelCase = {'''text''': '''string'''} lowerCAmelCase = TextDatasetReader(snake_case__ , cache_dir=snake_case__ , split=snake_case__ ).read() _check_text_dataset(snake_case__ , snake_case__ ) assert dataset.split == split if split else "train" @pytest.mark.parametrize('''path_type''' , [str, list] ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> Optional[int]: if issubclass(snake_case__ , snake_case__ ): lowerCAmelCase = text_path elif issubclass(snake_case__ , snake_case__ ): lowerCAmelCase = [text_path] lowerCAmelCase = tmp_path / '''cache''' lowerCAmelCase = {'''text''': '''string'''} lowerCAmelCase = TextDatasetReader(snake_case__ , cache_dir=snake_case__ ).read() _check_text_dataset(snake_case__ , snake_case__ ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__=("train",) ) -> Optional[Any]: assert isinstance(snake_case__ , snake_case__ ) for split in splits: lowerCAmelCase = dataset_dict[split] assert dataset.num_rows == 4 assert dataset.num_columns == 1 assert dataset.column_names == ["text"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize('''keep_in_memory''' , [False, True] ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> Optional[Any]: lowerCAmelCase = tmp_path / '''cache''' lowerCAmelCase = {'''text''': '''string'''} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): lowerCAmelCase = TextDatasetReader({'''train''': text_path} , cache_dir=snake_case__ , keep_in_memory=snake_case__ ).read() _check_text_datasetdict(snake_case__ , snake_case__ ) @pytest.mark.parametrize( '''features''' , [ None, {'''text''': '''string'''}, {'''text''': '''int32'''}, {'''text''': '''float32'''}, ] , ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> List[Any]: lowerCAmelCase = tmp_path / '''cache''' # CSV file loses col_1 string dtype information: default now is "int64" instead of "string" lowerCAmelCase = {'''text''': '''string'''} lowerCAmelCase = features.copy() if features else default_expected_features lowerCAmelCase = ( Features({feature: Value(snake_case__ ) for feature, dtype in features.items()} ) if features is not None else None ) lowerCAmelCase = TextDatasetReader({'''train''': text_path} , features=snake_case__ , cache_dir=snake_case__ ).read() _check_text_datasetdict(snake_case__ , snake_case__ ) @pytest.mark.parametrize('''split''' , [None, NamedSplit('''train''' ), '''train''', '''test'''] ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> Any: if split: lowerCAmelCase = {split: text_path} else: lowerCAmelCase = '''train''' lowerCAmelCase = {'''train''': text_path, '''test''': text_path} lowerCAmelCase = tmp_path / '''cache''' lowerCAmelCase = {'''text''': '''string'''} lowerCAmelCase = TextDatasetReader(snake_case__ , cache_dir=snake_case__ ).read() _check_text_datasetdict(snake_case__ , snake_case__ , splits=list(path.keys() ) ) assert all(dataset[split].split == split for split in path.keys() )
338
0
import unittest from transformers import AutoConfig, AutoTokenizer, BertConfig, TensorType, is_flax_available from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, require_flax, slow if is_flax_available(): import jax from transformers.models.auto.modeling_flax_auto import FlaxAutoModel from transformers.models.bert.modeling_flax_bert import FlaxBertModel from transformers.models.roberta.modeling_flax_roberta import FlaxRobertaModel @require_flax class _SCREAMING_SNAKE_CASE ( unittest.TestCase ): @slow def _A ( self : Any ): for model_name in ["bert-base-cased", "bert-large-uncased"]: with self.subTest(__lowerCamelCase ): UpperCamelCase :List[str] = AutoConfig.from_pretrained(__lowerCamelCase ) self.assertIsNotNone(__lowerCamelCase ) self.assertIsInstance(__lowerCamelCase , __lowerCamelCase ) UpperCamelCase :Optional[int] = FlaxAutoModel.from_pretrained(__lowerCamelCase ) self.assertIsNotNone(__lowerCamelCase ) self.assertIsInstance(__lowerCamelCase , __lowerCamelCase ) @slow def _A ( self : Optional[int] ): for model_name in ["roberta-base", "roberta-large"]: with self.subTest(__lowerCamelCase ): UpperCamelCase :Dict = AutoConfig.from_pretrained(__lowerCamelCase ) self.assertIsNotNone(__lowerCamelCase ) self.assertIsInstance(__lowerCamelCase , __lowerCamelCase ) UpperCamelCase :Union[str, Any] = FlaxAutoModel.from_pretrained(__lowerCamelCase ) self.assertIsNotNone(__lowerCamelCase ) self.assertIsInstance(__lowerCamelCase , __lowerCamelCase ) @slow def _A ( self : int ): for model_name in ["bert-base-cased", "bert-large-uncased"]: UpperCamelCase :str = AutoTokenizer.from_pretrained(__lowerCamelCase ) UpperCamelCase :Union[str, Any] = FlaxBertModel.from_pretrained(__lowerCamelCase ) UpperCamelCase :List[Any] = tokenizer("""Do you support jax jitted function?""" , return_tensors=TensorType.JAX ) @jax.jit def eval(**__lowerCamelCase : int ): return model(**__lowerCamelCase ) eval(**__lowerCamelCase ).block_until_ready() @slow def _A ( self : Union[str, Any] ): for model_name in ["roberta-base", "roberta-large"]: UpperCamelCase :Tuple = AutoTokenizer.from_pretrained(__lowerCamelCase ) UpperCamelCase :Any = FlaxRobertaModel.from_pretrained(__lowerCamelCase ) UpperCamelCase :List[str] = tokenizer("""Do you support jax jitted function?""" , return_tensors=TensorType.JAX ) @jax.jit def eval(**__lowerCamelCase : Any ): return model(**__lowerCamelCase ) eval(**__lowerCamelCase ).block_until_ready() def _A ( self : Any ): with self.assertRaisesRegex( __lowerCamelCase , """bert-base is not a local folder and is not a valid model identifier""" ): UpperCamelCase :Dict = FlaxAutoModel.from_pretrained("""bert-base""" ) def _A ( self : Union[str, Any] ): with self.assertRaisesRegex( __lowerCamelCase , R"""aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)""" ): UpperCamelCase :Optional[int] = FlaxAutoModel.from_pretrained(__lowerCamelCase , revision="""aaaaaa""" ) def _A ( self : Any ): with self.assertRaisesRegex( __lowerCamelCase , """hf-internal-testing/config-no-model does not appear to have a file named flax_model.msgpack""" , ): UpperCamelCase :int = FlaxAutoModel.from_pretrained("""hf-internal-testing/config-no-model""" ) def _A ( self : List[Any] ): with self.assertRaisesRegex(__lowerCamelCase , """Use `from_pt=True` to load this model""" ): UpperCamelCase :str = FlaxAutoModel.from_pretrained("""hf-internal-testing/tiny-bert-pt-only""" )
38
def SCREAMING_SNAKE_CASE_ ( snake_case__ ) -> str: if isinstance(snake_case__ , snake_case__ ): raise TypeError('''\'float\' object cannot be interpreted as an integer''' ) if isinstance(snake_case__ , snake_case__ ): raise TypeError('''\'str\' object cannot be interpreted as an integer''' ) if num == 0: return "0b0" lowerCAmelCase = False if num < 0: lowerCAmelCase = True lowerCAmelCase = -num lowerCAmelCase = [] while num > 0: binary.insert(0 , num % 2 ) num >>= 1 if negative: return "-0b" + "".join(str(snake_case__ ) for e in binary ) return "0b" + "".join(str(snake_case__ ) for e in binary ) if __name__ == "__main__": import doctest doctest.testmod()
338
0
from typing import Any, Dict, List, Union from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging, requires_backends from .base import PIPELINE_INIT_ARGS, ChunkPipeline if is_vision_available(): from PIL import Image from ..image_utils import load_image if is_torch_available(): import torch from transformers.modeling_outputs import BaseModelOutput from ..models.auto.modeling_auto import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING _a = logging.get_logger(__name__) @add_end_docstrings(snake_case__) class __lowerCamelCase ( snake_case__): """simple docstring""" def __init__( self , **UpperCAmelCase ): """simple docstring""" super().__init__(**UpperCAmelCase ) if self.framework == "tf": raise ValueError(F"""The {self.__class__} is only available in PyTorch.""" ) requires_backends(self , 'vision' ) self.check_model_type(UpperCAmelCase ) def __call__( self , UpperCAmelCase , UpperCAmelCase = None , **UpperCAmelCase , ): """simple docstring""" if "text_queries" in kwargs: _UpperCAmelCase = kwargs.pop('text_queries' ) if isinstance(UpperCAmelCase , (str, Image.Image) ): _UpperCAmelCase = {'image': image, 'candidate_labels': candidate_labels} else: _UpperCAmelCase = image _UpperCAmelCase = super().__call__(UpperCAmelCase , **UpperCAmelCase ) return results def UpperCamelCase ( self , **UpperCAmelCase ): """simple docstring""" _UpperCAmelCase = {} if "threshold" in kwargs: _UpperCAmelCase = kwargs['threshold'] if "top_k" in kwargs: _UpperCAmelCase = kwargs['top_k'] return {}, {}, postprocess_params def UpperCamelCase ( self , UpperCAmelCase ): """simple docstring""" _UpperCAmelCase = load_image(inputs['image'] ) _UpperCAmelCase = inputs['candidate_labels'] if isinstance(UpperCAmelCase , UpperCAmelCase ): _UpperCAmelCase = candidate_labels.split(',' ) _UpperCAmelCase = torch.tensor([[image.height, image.width]] , dtype=torch.intaa ) for i, candidate_label in enumerate(UpperCAmelCase ): _UpperCAmelCase = self.tokenizer(UpperCAmelCase , return_tensors=self.framework ) _UpperCAmelCase = self.image_processor(UpperCAmelCase , return_tensors=self.framework ) yield { "is_last": i == len(UpperCAmelCase ) - 1, "target_size": target_size, "candidate_label": candidate_label, **text_inputs, **image_features, } def UpperCamelCase ( self , UpperCAmelCase ): """simple docstring""" _UpperCAmelCase = model_inputs.pop('target_size' ) _UpperCAmelCase = model_inputs.pop('candidate_label' ) _UpperCAmelCase = model_inputs.pop('is_last' ) _UpperCAmelCase = self.model(**UpperCAmelCase ) _UpperCAmelCase = {'target_size': target_size, 'candidate_label': candidate_label, 'is_last': is_last, **outputs} return model_outputs def UpperCamelCase ( self , UpperCAmelCase , UpperCAmelCase=0.1 , UpperCAmelCase=None ): """simple docstring""" _UpperCAmelCase = [] for model_output in model_outputs: _UpperCAmelCase = model_output['candidate_label'] _UpperCAmelCase = BaseModelOutput(UpperCAmelCase ) _UpperCAmelCase = self.image_processor.post_process_object_detection( outputs=UpperCAmelCase , threshold=UpperCAmelCase , target_sizes=model_output['target_size'] )[0] for index in outputs["scores"].nonzero(): _UpperCAmelCase = outputs['scores'][index].item() _UpperCAmelCase = self._get_bounding_box(outputs['boxes'][index][0] ) _UpperCAmelCase = {'score': score, 'label': label, 'box': box} results.append(UpperCAmelCase ) _UpperCAmelCase = sorted(UpperCAmelCase , key=lambda UpperCAmelCase : x["score"] , reverse=UpperCAmelCase ) if top_k: _UpperCAmelCase = results[:top_k] return results def UpperCamelCase ( self , UpperCAmelCase ): """simple docstring""" if self.framework != "pt": raise ValueError('The ZeroShotObjectDetectionPipeline is only available in PyTorch.' ) _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase = box.int().tolist() _UpperCAmelCase = { 'xmin': xmin, 'ymin': ymin, 'xmax': xmax, 'ymax': ymax, } return bbox
39
class lowercase_ : """simple docstring""" def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ->Any: lowerCAmelCase = name lowerCAmelCase = value lowerCAmelCase = weight def __repr__( self ) ->str: return F"{self.__class__.__name__}({self.name}, {self.value}, {self.weight})" def SCREAMING_SNAKE_CASE_ ( self ) ->List[Any]: return self.value def SCREAMING_SNAKE_CASE_ ( self ) ->int: return self.name def SCREAMING_SNAKE_CASE_ ( self ) ->Union[str, Any]: return self.weight def SCREAMING_SNAKE_CASE_ ( self ) ->Tuple: return self.value / self.weight def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> int: lowerCAmelCase = [] for i in range(len(snake_case__ ) ): menu.append(Things(name[i] , value[i] , weight[i] ) ) return menu def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> Optional[int]: lowerCAmelCase = sorted(snake_case__ , key=snake_case__ , reverse=snake_case__ ) lowerCAmelCase = [] lowerCAmelCase , lowerCAmelCase = 0.0, 0.0 for i in range(len(snake_case__ ) ): if (total_cost + items_copy[i].get_weight()) <= max_cost: result.append(items_copy[i] ) total_cost += items_copy[i].get_weight() total_value += items_copy[i].get_value() return (result, total_value) def SCREAMING_SNAKE_CASE_ ( ) -> Optional[int]: pass if __name__ == "__main__": import doctest doctest.testmod()
338
0
"""simple docstring""" from .dependency_versions_table import deps from .utils.versions import require_version, require_version_core # define which module versions we always want to check at run time # (usually the ones defined in `install_requires` in setup.py) # # order specific notes: # - tqdm must be checked before tokenizers __lowercase = [ """python""", """tqdm""", """regex""", """requests""", """packaging""", """filelock""", """numpy""", """tokenizers""", """huggingface-hub""", """safetensors""", """accelerate""", """pyyaml""", ] for pkg in pkgs_to_check_at_runtime: if pkg in deps: if pkg == "tokenizers": # must be loaded here, or else tqdm check may fail from .utils import is_tokenizers_available if not is_tokenizers_available(): continue # not required, check version only if installed elif pkg == "accelerate": # must be loaded here, or else tqdm check may fail from .utils import is_accelerate_available # Maybe switch to is_torch_available in the future here so that Accelerate is hard dep of # Transformers with PyTorch if not is_accelerate_available(): continue # not required, check version only if installed require_version_core(deps[pkg]) else: raise ValueError(f'''can\'t find {pkg} in {deps.keys()}, check dependency_versions_table.py''') def lowercase ( A_ , A_=None )-> Optional[int]: '''simple docstring''' require_version(deps[pkg] , A_ )
40
import numpy as np import skfuzzy as fuzz if __name__ == "__main__": # Create universe of discourse in Python using linspace () lowercase__ : Dict = np.linspace(start=0, stop=7_5, num=7_5, endpoint=True, retstep=False) # Create two fuzzy sets by defining any membership function # (trapmf(), gbellmf(), gaussmf(), etc). lowercase__ : Optional[int] = [0, 2_5, 5_0] lowercase__ : Union[str, Any] = [2_5, 5_0, 7_5] lowercase__ : int = fuzz.membership.trimf(X, abca) lowercase__ : Tuple = fuzz.membership.trimf(X, abca) # Compute the different operations using inbuilt functions. lowercase__ : List[str] = np.ones(7_5) lowercase__ : Any = np.zeros((7_5,)) # 1. Union = max(µA(x), µB(x)) lowercase__ : Union[str, Any] = fuzz.fuzzy_or(X, young, X, middle_aged)[1] # 2. Intersection = min(µA(x), µB(x)) lowercase__ : int = fuzz.fuzzy_and(X, young, X, middle_aged)[1] # 3. Complement (A) = (1- min(µA(x)) lowercase__ : Union[str, Any] = fuzz.fuzzy_not(young) # 4. Difference (A/B) = min(µA(x),(1- µB(x))) lowercase__ : Optional[int] = fuzz.fuzzy_and(X, young, X, fuzz.fuzzy_not(middle_aged)[1])[1] # 5. Algebraic Sum = [µA(x) + µB(x) – (µA(x) * µB(x))] lowercase__ : Any = young + middle_aged - (young * middle_aged) # 6. Algebraic Product = (µA(x) * µB(x)) lowercase__ : str = young * middle_aged # 7. Bounded Sum = min[1,(µA(x), µB(x))] lowercase__ : Tuple = fuzz.fuzzy_and(X, one, X, young + middle_aged)[1] # 8. Bounded difference = min[0,(µA(x), µB(x))] lowercase__ : Tuple = fuzz.fuzzy_or(X, zero, X, young - middle_aged)[1] # max-min composition # max-product composition # Plot each set A, set B and each operation result using plot() and subplot(). from matplotlib import pyplot as plt plt.figure() plt.subplot(4, 3, 1) plt.plot(X, young) plt.title('''Young''') plt.grid(True) plt.subplot(4, 3, 2) plt.plot(X, middle_aged) plt.title('''Middle aged''') plt.grid(True) plt.subplot(4, 3, 3) plt.plot(X, union) plt.title('''union''') plt.grid(True) plt.subplot(4, 3, 4) plt.plot(X, intersection) plt.title('''intersection''') plt.grid(True) plt.subplot(4, 3, 5) plt.plot(X, complement_a) plt.title('''complement_a''') plt.grid(True) plt.subplot(4, 3, 6) plt.plot(X, difference) plt.title('''difference a/b''') plt.grid(True) plt.subplot(4, 3, 7) plt.plot(X, alg_sum) plt.title('''alg_sum''') plt.grid(True) plt.subplot(4, 3, 8) plt.plot(X, alg_product) plt.title('''alg_product''') plt.grid(True) plt.subplot(4, 3, 9) plt.plot(X, bdd_sum) plt.title('''bdd_sum''') plt.grid(True) plt.subplot(4, 3, 1_0) plt.plot(X, bdd_difference) plt.title('''bdd_difference''') plt.grid(True) plt.subplots_adjust(hspace=0.5) plt.show()
338
0
'''simple docstring''' from collections import defaultdict def SCREAMING_SNAKE_CASE_ (UpperCamelCase , UpperCamelCase ) -> bool: lowerCamelCase__ : List[str] = first_str.lower().strip() lowerCamelCase__ : Dict = second_str.lower().strip() # Remove whitespace lowerCamelCase__ : int = first_str.replace(""" """ , """""" ) lowerCamelCase__ : Optional[int] = second_str.replace(""" """ , """""" ) # Strings of different lengths are not anagrams if len(UpperCamelCase ) != len(UpperCamelCase ): return False # Default values for count should be 0 lowerCamelCase__ : defaultdict[str, int] = defaultdict(UpperCamelCase ) # For each character in input strings, # increment count in the corresponding for i in range(len(UpperCamelCase ) ): count[first_str[i]] += 1 count[second_str[i]] -= 1 return all(_count == 0 for _count in count.values() ) if __name__ == "__main__": from doctest import testmod testmod() _A : Optional[Any] =input('''Enter the first string ''').strip() _A : str =input('''Enter the second string ''').strip() _A : Dict =check_anagrams(input_a, input_b) print(F'{input_a} and {input_b} are {"" if status else "not "}anagrams.')
41
import torch from diffusers import DDPMScheduler from .test_schedulers import SchedulerCommonTest class lowercase_ ( UpperCamelCase_ ): """simple docstring""" UpperCAmelCase_ : str = (DDPMScheduler,) def SCREAMING_SNAKE_CASE_ ( self , **__SCREAMING_SNAKE_CASE ) ->Optional[Any]: lowerCAmelCase = { '''num_train_timesteps''': 1000, '''beta_start''': 0.0_0_0_1, '''beta_end''': 0.0_2, '''beta_schedule''': '''linear''', '''variance_type''': '''fixed_small''', '''clip_sample''': True, } config.update(**__SCREAMING_SNAKE_CASE ) return config def SCREAMING_SNAKE_CASE_ ( self ) ->Tuple: for timesteps in [1, 5, 100, 1000]: self.check_over_configs(num_train_timesteps=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->List[Any]: for beta_start, beta_end in zip([0.0_0_0_1, 0.0_0_1, 0.0_1, 0.1] , [0.0_0_2, 0.0_2, 0.2, 2] ): self.check_over_configs(beta_start=__SCREAMING_SNAKE_CASE , beta_end=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[Any]: for schedule in ["linear", "squaredcos_cap_v2"]: self.check_over_configs(beta_schedule=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->Dict: for variance in ["fixed_small", "fixed_large", "other"]: self.check_over_configs(variance_type=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->List[str]: for clip_sample in [True, False]: self.check_over_configs(clip_sample=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->Any: self.check_over_configs(thresholding=__SCREAMING_SNAKE_CASE ) for threshold in [0.5, 1.0, 2.0]: for prediction_type in ["epsilon", "sample", "v_prediction"]: self.check_over_configs( thresholding=__SCREAMING_SNAKE_CASE , prediction_type=__SCREAMING_SNAKE_CASE , sample_max_value=__SCREAMING_SNAKE_CASE , ) def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[int]: for prediction_type in ["epsilon", "sample", "v_prediction"]: self.check_over_configs(prediction_type=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[Any]: for t in [0, 500, 999]: self.check_over_forward(time_step=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->Tuple: lowerCAmelCase = self.scheduler_classes[0] lowerCAmelCase = self.get_scheduler_config() lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) assert torch.sum(torch.abs(scheduler._get_variance(0 ) - 0.0 ) ) < 1e-5 assert torch.sum(torch.abs(scheduler._get_variance(487 ) - 0.0_0_9_7_9 ) ) < 1e-5 assert torch.sum(torch.abs(scheduler._get_variance(999 ) - 0.0_2 ) ) < 1e-5 def SCREAMING_SNAKE_CASE_ ( self ) ->str: lowerCAmelCase = self.scheduler_classes[0] lowerCAmelCase = self.get_scheduler_config() lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = len(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = self.dummy_model() lowerCAmelCase = self.dummy_sample_deter lowerCAmelCase = torch.manual_seed(0 ) for t in reversed(range(__SCREAMING_SNAKE_CASE ) ): # 1. predict noise residual lowerCAmelCase = model(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # 2. predict previous mean of sample x_t-1 lowerCAmelCase = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , generator=__SCREAMING_SNAKE_CASE ).prev_sample # if t > 0: # noise = self.dummy_sample_deter # variance = scheduler.get_variance(t) ** (0.5) * noise # # sample = pred_prev_sample + variance lowerCAmelCase = pred_prev_sample lowerCAmelCase = torch.sum(torch.abs(__SCREAMING_SNAKE_CASE ) ) lowerCAmelCase = torch.mean(torch.abs(__SCREAMING_SNAKE_CASE ) ) assert abs(result_sum.item() - 2_5_8.9_6_0_6 ) < 1e-2 assert abs(result_mean.item() - 0.3_3_7_2 ) < 1e-3 def SCREAMING_SNAKE_CASE_ ( self ) ->str: lowerCAmelCase = self.scheduler_classes[0] lowerCAmelCase = self.get_scheduler_config(prediction_type='''v_prediction''' ) lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = len(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = self.dummy_model() lowerCAmelCase = self.dummy_sample_deter lowerCAmelCase = torch.manual_seed(0 ) for t in reversed(range(__SCREAMING_SNAKE_CASE ) ): # 1. predict noise residual lowerCAmelCase = model(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # 2. predict previous mean of sample x_t-1 lowerCAmelCase = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , generator=__SCREAMING_SNAKE_CASE ).prev_sample # if t > 0: # noise = self.dummy_sample_deter # variance = scheduler.get_variance(t) ** (0.5) * noise # # sample = pred_prev_sample + variance lowerCAmelCase = pred_prev_sample lowerCAmelCase = torch.sum(torch.abs(__SCREAMING_SNAKE_CASE ) ) lowerCAmelCase = torch.mean(torch.abs(__SCREAMING_SNAKE_CASE ) ) assert abs(result_sum.item() - 2_0_2.0_2_9_6 ) < 1e-2 assert abs(result_mean.item() - 0.2_6_3_1 ) < 1e-3 def SCREAMING_SNAKE_CASE_ ( self ) ->Any: lowerCAmelCase = self.scheduler_classes[0] lowerCAmelCase = self.get_scheduler_config() lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = [100, 87, 50, 1, 0] scheduler.set_timesteps(timesteps=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = scheduler.timesteps for i, timestep in enumerate(__SCREAMING_SNAKE_CASE ): if i == len(__SCREAMING_SNAKE_CASE ) - 1: lowerCAmelCase = -1 else: lowerCAmelCase = timesteps[i + 1] lowerCAmelCase = scheduler.previous_timestep(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = prev_t.item() self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[Any]: lowerCAmelCase = self.scheduler_classes[0] lowerCAmelCase = self.get_scheduler_config() lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = [100, 87, 50, 51, 0] with self.assertRaises(__SCREAMING_SNAKE_CASE , msg='''`custom_timesteps` must be in descending order.''' ): scheduler.set_timesteps(timesteps=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->str: lowerCAmelCase = self.scheduler_classes[0] lowerCAmelCase = self.get_scheduler_config() lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = [100, 87, 50, 1, 0] lowerCAmelCase = len(__SCREAMING_SNAKE_CASE ) with self.assertRaises(__SCREAMING_SNAKE_CASE , msg='''Can only pass one of `num_inference_steps` or `custom_timesteps`.''' ): scheduler.set_timesteps(num_inference_steps=__SCREAMING_SNAKE_CASE , timesteps=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->Any: lowerCAmelCase = self.scheduler_classes[0] lowerCAmelCase = self.get_scheduler_config() lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = [scheduler.config.num_train_timesteps] with self.assertRaises( __SCREAMING_SNAKE_CASE , msg='''`timesteps` must start before `self.config.train_timesteps`: {scheduler.config.num_train_timesteps}}''' , ): scheduler.set_timesteps(timesteps=__SCREAMING_SNAKE_CASE )
338
0
'''simple docstring''' from __future__ import annotations from typing import TypedDict class __UpperCAmelCase ( _lowerCamelCase ): __lowercase = 42 __lowercase = 42 def SCREAMING_SNAKE_CASE__ ( __A ) -> list[str]: if not isinstance(__A , __A ): raise TypeError('The parameter s type must be str.' ) return [s[i:] + s[:i] for i in range(len(__A ) )] def SCREAMING_SNAKE_CASE__ ( __A ) -> BWTTransformDict: if not isinstance(__A , __A ): raise TypeError('The parameter s type must be str.' ) if not s: raise ValueError('The parameter s must not be empty.' ) _snake_case = all_rotations(__A ) rotations.sort() # sort the list of rotations in alphabetically order # make a string composed of the last char of each rotation _snake_case = { "bwt_string": "".join([word[-1] for word in rotations] ), "idx_original_string": rotations.index(__A ), } return response def SCREAMING_SNAKE_CASE__ ( __A , __A ) -> str: if not isinstance(__A , __A ): raise TypeError('The parameter bwt_string type must be str.' ) if not bwt_string: raise ValueError('The parameter bwt_string must not be empty.' ) try: _snake_case = int(__A ) except ValueError: raise TypeError( 'The parameter idx_original_string type must be int or passive' ' of cast to int.' ) if idx_original_string < 0: raise ValueError('The parameter idx_original_string must not be lower than 0.' ) if idx_original_string >= len(__A ): raise ValueError( 'The parameter idx_original_string must be lower than' ' len(bwt_string).' ) _snake_case = [''] * len(__A ) for _ in range(len(__A ) ): for i in range(len(__A ) ): _snake_case = bwt_string[i] + ordered_rotations[i] ordered_rotations.sort() return ordered_rotations[idx_original_string] if __name__ == "__main__": lowercase : Optional[int] = "Provide a string that I will generate its BWT transform: " lowercase : List[str] = input(entry_msg).strip() lowercase : Optional[Any] = bwt_transform(s) print( F'''Burrows Wheeler transform for string \'{s}\' results ''' F'''in \'{result["bwt_string"]}\'''' ) lowercase : Optional[int] = reverse_bwt(result["bwt_string"], result["idx_original_string"]) print( F'''Reversing Burrows Wheeler transform for entry \'{result["bwt_string"]}\' ''' F'''we get original string \'{original_string}\'''' )
42
import json import os from typing import Optional import numpy as np from ...feature_extraction_utils import BatchFeature from ...processing_utils import ProcessorMixin from ...utils import logging from ...utils.hub import get_file_from_repo from ..auto import AutoTokenizer lowercase__ : str = logging.get_logger(__name__) class lowercase_ ( UpperCamelCase_ ): """simple docstring""" UpperCAmelCase_ : Any = """AutoTokenizer""" UpperCAmelCase_ : Optional[int] = ["""tokenizer"""] UpperCAmelCase_ : str = { """semantic_prompt""": 1, """coarse_prompt""": 2, """fine_prompt""": 2, } def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None ) ->Optional[Any]: super().__init__(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = speaker_embeddings @classmethod def SCREAMING_SNAKE_CASE_ ( cls , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE="speaker_embeddings_path.json" , **__SCREAMING_SNAKE_CASE ) ->Tuple: if speaker_embeddings_dict_path is not None: lowerCAmelCase = get_file_from_repo( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , subfolder=kwargs.pop('''subfolder''' , __SCREAMING_SNAKE_CASE ) , cache_dir=kwargs.pop('''cache_dir''' , __SCREAMING_SNAKE_CASE ) , force_download=kwargs.pop('''force_download''' , __SCREAMING_SNAKE_CASE ) , proxies=kwargs.pop('''proxies''' , __SCREAMING_SNAKE_CASE ) , resume_download=kwargs.pop('''resume_download''' , __SCREAMING_SNAKE_CASE ) , local_files_only=kwargs.pop('''local_files_only''' , __SCREAMING_SNAKE_CASE ) , use_auth_token=kwargs.pop('''use_auth_token''' , __SCREAMING_SNAKE_CASE ) , revision=kwargs.pop('''revision''' , __SCREAMING_SNAKE_CASE ) , ) if speaker_embeddings_path is None: logger.warning( F"`{os.path.join(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE )}` does not exists\n , no preloaded speaker embeddings will be used - Make sure to provide a correct path to the json\n dictionnary if wanted, otherwise set `speaker_embeddings_dict_path=None`." ) lowerCAmelCase = None else: with open(__SCREAMING_SNAKE_CASE ) as speaker_embeddings_json: lowerCAmelCase = json.load(__SCREAMING_SNAKE_CASE ) else: lowerCAmelCase = None lowerCAmelCase = AutoTokenizer.from_pretrained(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) return cls(tokenizer=__SCREAMING_SNAKE_CASE , speaker_embeddings=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE="speaker_embeddings_path.json" , __SCREAMING_SNAKE_CASE="speaker_embeddings" , __SCREAMING_SNAKE_CASE = False , **__SCREAMING_SNAKE_CASE , ) ->int: if self.speaker_embeddings is not None: os.makedirs(os.path.join(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , '''v2''' ) , exist_ok=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = {} lowerCAmelCase = save_directory for prompt_key in self.speaker_embeddings: if prompt_key != "repo_or_path": lowerCAmelCase = self._load_voice_preset(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = {} for key in self.speaker_embeddings[prompt_key]: np.save( os.path.join( embeddings_dict['''repo_or_path'''] , __SCREAMING_SNAKE_CASE , F"{prompt_key}_{key}" ) , voice_preset[key] , allow_pickle=__SCREAMING_SNAKE_CASE , ) lowerCAmelCase = os.path.join(__SCREAMING_SNAKE_CASE , F"{prompt_key}_{key}.npy" ) lowerCAmelCase = tmp_dict with open(os.path.join(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) , '''w''' ) as fp: json.dump(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) super().save_pretrained(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE = None , **__SCREAMING_SNAKE_CASE ) ->List[str]: lowerCAmelCase = self.speaker_embeddings[voice_preset] lowerCAmelCase = {} for key in ["semantic_prompt", "coarse_prompt", "fine_prompt"]: if key not in voice_preset_paths: raise ValueError( F"Voice preset unrecognized, missing {key} as a key in self.speaker_embeddings[{voice_preset}]." ) lowerCAmelCase = get_file_from_repo( self.speaker_embeddings.get('''repo_or_path''' , '''/''' ) , voice_preset_paths[key] , subfolder=kwargs.pop('''subfolder''' , __SCREAMING_SNAKE_CASE ) , cache_dir=kwargs.pop('''cache_dir''' , __SCREAMING_SNAKE_CASE ) , force_download=kwargs.pop('''force_download''' , __SCREAMING_SNAKE_CASE ) , proxies=kwargs.pop('''proxies''' , __SCREAMING_SNAKE_CASE ) , resume_download=kwargs.pop('''resume_download''' , __SCREAMING_SNAKE_CASE ) , local_files_only=kwargs.pop('''local_files_only''' , __SCREAMING_SNAKE_CASE ) , use_auth_token=kwargs.pop('''use_auth_token''' , __SCREAMING_SNAKE_CASE ) , revision=kwargs.pop('''revision''' , __SCREAMING_SNAKE_CASE ) , ) if path is None: raise ValueError( F"`{os.path.join(self.speaker_embeddings.get('repo_or_path' , '/' ) , voice_preset_paths[key] )}` does not exists\n , no preloaded voice preset will be used - Make sure to provide correct paths to the {voice_preset}\n embeddings." ) lowerCAmelCase = np.load(__SCREAMING_SNAKE_CASE ) return voice_preset_dict def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE = None ) ->Tuple: for key in ["semantic_prompt", "coarse_prompt", "fine_prompt"]: if key not in voice_preset: raise ValueError(F"Voice preset unrecognized, missing {key} as a key." ) if not isinstance(voice_preset[key] , np.ndarray ): raise ValueError(F"{key} voice preset must be a {str(self.preset_shape[key] )}D ndarray." ) if len(voice_preset[key].shape ) != self.preset_shape[key]: raise ValueError(F"{key} voice preset must be a {str(self.preset_shape[key] )}D ndarray." ) def __call__( self , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE="pt" , __SCREAMING_SNAKE_CASE=256 , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=False , **__SCREAMING_SNAKE_CASE , ) ->int: if voice_preset is not None and not isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): if ( isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) and self.speaker_embeddings is not None and voice_preset in self.speaker_embeddings ): lowerCAmelCase = self._load_voice_preset(__SCREAMING_SNAKE_CASE ) else: if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) and not voice_preset.endswith('''.npz''' ): lowerCAmelCase = voice_preset + '''.npz''' lowerCAmelCase = np.load(__SCREAMING_SNAKE_CASE ) if voice_preset is not None: self._validate_voice_preset_dict(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) lowerCAmelCase = BatchFeature(data=__SCREAMING_SNAKE_CASE , tensor_type=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = self.tokenizer( __SCREAMING_SNAKE_CASE , return_tensors=__SCREAMING_SNAKE_CASE , padding='''max_length''' , max_length=__SCREAMING_SNAKE_CASE , return_attention_mask=__SCREAMING_SNAKE_CASE , return_token_type_ids=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) if voice_preset is not None: lowerCAmelCase = voice_preset return encoded_text
338
0
import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import CLIPTokenizer, CLIPTokenizerFast from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES from transformers.testing_utils import require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available if is_vision_available(): from PIL import Image from transformers import CLIPSegProcessor, ViTImageProcessor @require_vision class lowerCamelCase_ ( unittest.TestCase ): '''simple docstring''' def UpperCamelCase__ ( self) -> List[str]: __UpperCamelCase :int = tempfile.mkdtemp() # fmt: off __UpperCamelCase :int = ['''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''lo''', '''l</w>''', '''w</w>''', '''r</w>''', '''t</w>''', '''low</w>''', '''er</w>''', '''lowest</w>''', '''newer</w>''', '''wider''', '''<unk>''', '''<|startoftext|>''', '''<|endoftext|>'''] # fmt: on __UpperCamelCase :Tuple = dict(zip(__lowercase , range(len(__lowercase)))) __UpperCamelCase :List[Any] = ['''#version: 0.2''', '''l o''', '''lo w</w>''', '''e r</w>''', ''''''] __UpperCamelCase :Optional[Any] = {'''unk_token''': '''<unk>'''} __UpperCamelCase :List[str] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file''']) __UpperCamelCase :List[Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''merges_file''']) with open(self.vocab_file , '''w''' , encoding='''utf-8''') as fp: fp.write(json.dumps(__lowercase) + '''\n''') with open(self.merges_file , '''w''' , encoding='''utf-8''') as fp: fp.write('''\n'''.join(__lowercase)) __UpperCamelCase :List[str] = { '''do_resize''': True, '''size''': 20, '''do_center_crop''': True, '''crop_size''': 18, '''do_normalize''': True, '''image_mean''': [0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73], '''image_std''': [0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11], } __UpperCamelCase :Any = os.path.join(self.tmpdirname , __lowercase) with open(self.image_processor_file , '''w''' , encoding='''utf-8''') as fp: json.dump(__lowercase , __lowercase) def UpperCamelCase__ ( self , **__lowercase) -> Any: return CLIPTokenizer.from_pretrained(self.tmpdirname , **__lowercase) def UpperCamelCase__ ( self , **__lowercase) -> Any: return CLIPTokenizerFast.from_pretrained(self.tmpdirname , **__lowercase) def UpperCamelCase__ ( self , **__lowercase) -> List[str]: return ViTImageProcessor.from_pretrained(self.tmpdirname , **__lowercase) def UpperCamelCase__ ( self) -> List[str]: shutil.rmtree(self.tmpdirname) def UpperCamelCase__ ( self) -> Tuple: __UpperCamelCase :Dict = [np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta)] __UpperCamelCase :int = [Image.fromarray(np.moveaxis(__lowercase , 0 , -1)) for x in image_inputs] return image_inputs def UpperCamelCase__ ( self) -> Tuple: __UpperCamelCase :Dict = self.get_tokenizer() __UpperCamelCase :str = self.get_rust_tokenizer() __UpperCamelCase :Optional[Any] = self.get_image_processor() __UpperCamelCase :List[Any] = CLIPSegProcessor(tokenizer=__lowercase , image_processor=__lowercase) processor_slow.save_pretrained(self.tmpdirname) __UpperCamelCase :Optional[int] = CLIPSegProcessor.from_pretrained(self.tmpdirname , use_fast=__lowercase) __UpperCamelCase :Union[str, Any] = CLIPSegProcessor(tokenizer=__lowercase , image_processor=__lowercase) processor_fast.save_pretrained(self.tmpdirname) __UpperCamelCase :Dict = CLIPSegProcessor.from_pretrained(self.tmpdirname) self.assertEqual(processor_slow.tokenizer.get_vocab() , tokenizer_slow.get_vocab()) self.assertEqual(processor_fast.tokenizer.get_vocab() , tokenizer_fast.get_vocab()) self.assertEqual(tokenizer_slow.get_vocab() , tokenizer_fast.get_vocab()) self.assertIsInstance(processor_slow.tokenizer , __lowercase) self.assertIsInstance(processor_fast.tokenizer , __lowercase) self.assertEqual(processor_slow.image_processor.to_json_string() , image_processor.to_json_string()) self.assertEqual(processor_fast.image_processor.to_json_string() , image_processor.to_json_string()) self.assertIsInstance(processor_slow.image_processor , __lowercase) self.assertIsInstance(processor_fast.image_processor , __lowercase) def UpperCamelCase__ ( self) -> Union[str, Any]: __UpperCamelCase :Dict = CLIPSegProcessor(tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor()) processor.save_pretrained(self.tmpdirname) __UpperCamelCase :int = self.get_tokenizer(bos_token='''(BOS)''' , eos_token='''(EOS)''') __UpperCamelCase :List[Any] = self.get_image_processor(do_normalize=__lowercase , padding_value=1.0) __UpperCamelCase :Optional[Any] = CLIPSegProcessor.from_pretrained( self.tmpdirname , bos_token='''(BOS)''' , eos_token='''(EOS)''' , do_normalize=__lowercase , padding_value=1.0) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab()) self.assertIsInstance(processor.tokenizer , __lowercase) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string()) self.assertIsInstance(processor.image_processor , __lowercase) def UpperCamelCase__ ( self) -> Optional[Any]: __UpperCamelCase :Optional[int] = self.get_image_processor() __UpperCamelCase :Optional[Any] = self.get_tokenizer() __UpperCamelCase :Dict = CLIPSegProcessor(tokenizer=__lowercase , image_processor=__lowercase) __UpperCamelCase :Dict = self.prepare_image_inputs() __UpperCamelCase :Union[str, Any] = image_processor(__lowercase , return_tensors='''np''') __UpperCamelCase :str = processor(images=__lowercase , return_tensors='''np''') for key in input_feat_extract.keys(): self.assertAlmostEqual(input_feat_extract[key].sum() , input_processor[key].sum() , delta=1E-2) def UpperCamelCase__ ( self) -> Tuple: __UpperCamelCase :Tuple = self.get_image_processor() __UpperCamelCase :Dict = self.get_tokenizer() __UpperCamelCase :str = CLIPSegProcessor(tokenizer=__lowercase , image_processor=__lowercase) __UpperCamelCase :Tuple = '''lower newer''' __UpperCamelCase :str = processor(text=__lowercase) __UpperCamelCase :Optional[Any] = tokenizer(__lowercase) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key]) def UpperCamelCase__ ( self) -> Any: __UpperCamelCase :str = self.get_image_processor() __UpperCamelCase :Tuple = self.get_tokenizer() __UpperCamelCase :Optional[Any] = CLIPSegProcessor(tokenizer=__lowercase , image_processor=__lowercase) __UpperCamelCase :Optional[int] = '''lower newer''' __UpperCamelCase :str = self.prepare_image_inputs() __UpperCamelCase :Any = processor(text=__lowercase , images=__lowercase) self.assertListEqual(list(inputs.keys()) , ['''input_ids''', '''attention_mask''', '''pixel_values''']) # test if it raises when no input is passed with pytest.raises(__lowercase): processor() def UpperCamelCase__ ( self) -> Any: __UpperCamelCase :str = self.get_image_processor() __UpperCamelCase :Tuple = self.get_tokenizer() __UpperCamelCase :List[Any] = CLIPSegProcessor(tokenizer=__lowercase , image_processor=__lowercase) __UpperCamelCase :Optional[Any] = self.prepare_image_inputs() __UpperCamelCase :Optional[Any] = self.prepare_image_inputs() __UpperCamelCase :List[Any] = processor(images=__lowercase , visual_prompt=__lowercase) self.assertListEqual(list(inputs.keys()) , ['''pixel_values''', '''conditional_pixel_values''']) # test if it raises when no input is passed with pytest.raises(__lowercase): processor() def UpperCamelCase__ ( self) -> Dict: __UpperCamelCase :Optional[Any] = self.get_image_processor() __UpperCamelCase :List[str] = self.get_tokenizer() __UpperCamelCase :Optional[Any] = CLIPSegProcessor(tokenizer=__lowercase , image_processor=__lowercase) __UpperCamelCase :int = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] __UpperCamelCase :Optional[Any] = processor.batch_decode(__lowercase) __UpperCamelCase :Any = tokenizer.batch_decode(__lowercase) self.assertListEqual(__lowercase , __lowercase)
43
import warnings from diffusers import StableDiffusionInpaintPipeline as StableDiffusionInpaintPipeline # noqa F401 warnings.warn( '''The `inpainting.py` script is outdated. Please use directly `from diffusers import''' ''' StableDiffusionInpaintPipeline` instead.''' )
338
0
"""simple docstring""" import unittest from transformers import BertGenerationTokenizer from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_torch, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin _a : str = '▁' _a : Optional[int] = get_tests_dir('fixtures/test_sentencepiece.model') @require_sentencepiece class __A ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): _UpperCamelCase : Union[str, Any] = BertGenerationTokenizer _UpperCamelCase : List[Any] = False _UpperCamelCase : Tuple = True def __A ( self ): super().setUp() _lowerCAmelCase : List[str] = BertGenerationTokenizer(a__ , keep_accents=a__ ) tokenizer.save_pretrained(self.tmpdirname ) def __A ( self ): _lowerCAmelCase : Dict = """<s>""" _lowerCAmelCase : Tuple = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(a__ ) , a__ ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(a__ ) , a__ ) def __A ( self ): _lowerCAmelCase : List[Any] = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , """<unk>""" ) self.assertEqual(vocab_keys[1] , """<s>""" ) self.assertEqual(vocab_keys[-1] , """<pad>""" ) self.assertEqual(len(a__ ) , 1002 ) def __A ( self ): self.assertEqual(self.get_tokenizer().vocab_size , 1000 ) def __A ( self ): _lowerCAmelCase : Union[str, Any] = BertGenerationTokenizer(a__ , keep_accents=a__ ) _lowerCAmelCase : str = tokenizer.tokenize("""This is a test""" ) self.assertListEqual(a__ , ["""▁This""", """▁is""", """▁a""", """▁t""", """est"""] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(a__ ) , [285, 46, 10, 170, 382] , ) _lowerCAmelCase : Union[str, Any] = tokenizer.tokenize("""I was born in 92000, and this is falsé.""" ) self.assertListEqual( a__ , [ 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 : Tuple = tokenizer.convert_tokens_to_ids(a__ ) self.assertListEqual( a__ , [8, 21, 84, 55, 24, 19, 7, 0, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 0, 4] , ) _lowerCAmelCase : List[Any] = tokenizer.convert_ids_to_tokens(a__ ) self.assertListEqual( a__ , [ SPIECE_UNDERLINE + """I""", SPIECE_UNDERLINE + """was""", SPIECE_UNDERLINE + """b""", """or""", """n""", SPIECE_UNDERLINE + """in""", SPIECE_UNDERLINE + """""", """<unk>""", """2""", """0""", """0""", """0""", """,""", SPIECE_UNDERLINE + """and""", SPIECE_UNDERLINE + """this""", SPIECE_UNDERLINE + """is""", SPIECE_UNDERLINE + """f""", """al""", """s""", """<unk>""", """.""", ] , ) @cached_property def __A ( self ): return BertGenerationTokenizer.from_pretrained("""google/bert_for_seq_generation_L-24_bbc_encoder""" ) @slow def __A ( self ): _lowerCAmelCase : Tuple = """Hello World!""" _lowerCAmelCase : Union[str, Any] = [18536, 2260, 101] self.assertListEqual(a__ , self.big_tokenizer.encode(a__ ) ) @slow def __A ( self ): _lowerCAmelCase : List[str] = ( """This is a very long text with a lot of weird characters, such as: . , ~ ? ( ) \" [ ] ! : - . Also we will""" """ add words that should not exsist and be tokenized to <unk>, such as saoneuhaoesuth""" ) _lowerCAmelCase : Dict = [ 871, 419, 358, 946, 991, 2521, 452, 358, 1357, 387, 7751, 3536, 112, 985, 456, 126, 865, 938, 5400, 5734, 458, 1368, 467, 786, 2462, 5246, 1159, 633, 865, 4519, 457, 582, 852, 2557, 427, 916, 508, 405, 34324, 497, 391, 408, 11342, 1244, 385, 100, 938, 985, 456, 574, 362, 12597, 3200, 3129, 1172, ] self.assertListEqual(a__ , self.big_tokenizer.encode(a__ ) ) @require_torch @slow def __A ( self ): import torch from transformers import BertGenerationConfig, BertGenerationEncoder # Build sequence _lowerCAmelCase : int = list(self.big_tokenizer.get_vocab().keys() )[:10] _lowerCAmelCase : List[str] = """ """.join(a__ ) _lowerCAmelCase : Union[str, Any] = self.big_tokenizer.encode_plus(a__ , return_tensors="""pt""" , return_token_type_ids=a__ ) _lowerCAmelCase : Union[str, Any] = self.big_tokenizer.batch_encode_plus( [sequence + """ """ + sequence] , return_tensors="""pt""" , return_token_type_ids=a__ ) _lowerCAmelCase : int = BertGenerationConfig() _lowerCAmelCase : Optional[Any] = BertGenerationEncoder(a__ ) assert model.get_input_embeddings().weight.shape[0] >= self.big_tokenizer.vocab_size with torch.no_grad(): model(**a__ ) model(**a__ ) @slow def __A ( self ): # fmt: off _lowerCAmelCase : Dict = {"""input_ids""": [[39286, 458, 36335, 2001, 456, 13073, 13266, 455, 113, 7746, 1741, 11157, 391, 13073, 13266, 455, 113, 3967, 35412, 113, 4936, 109, 3870, 2377, 113, 30084, 45720, 458, 134, 17496, 112, 503, 11672, 113, 118, 112, 5665, 13347, 38687, 112, 1496, 31389, 112, 3268, 47264, 134, 962, 112, 16377, 8035, 23130, 430, 12169, 15518, 28592, 458, 146, 41697, 109, 391, 12169, 15518, 16689, 458, 146, 41358, 109, 452, 726, 4034, 111, 763, 35412, 5082, 388, 1903, 111, 9051, 391, 2870, 48918, 1900, 1123, 550, 998, 112, 9586, 15985, 455, 391, 410, 22955, 37636, 114], [448, 17496, 419, 3663, 385, 763, 113, 27533, 2870, 3283, 13043, 1639, 24713, 523, 656, 24013, 18550, 2521, 517, 27014, 21244, 420, 1212, 1465, 391, 927, 4833, 388, 578, 11786, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [484, 2169, 7687, 21932, 18146, 726, 363, 17032, 3391, 114, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], """attention_mask""": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=a__ , model_name="""google/bert_for_seq_generation_L-24_bbc_encoder""" , revision="""c817d1fd1be2ffa69431227a1fe320544943d4db""" , )
44
import os import re import shutil import sys import tempfile import unittest import black lowercase__ : List[str] = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) sys.path.append(os.path.join(git_repo_path, '''utils''')) import check_copies # noqa: E402 # This is the reference code that will be used in the tests. # If BertLMPredictionHead is changed in modeling_bert.py, this code needs to be manually updated. lowercase__ : Dict = ''' def __init__(self, config): super().__init__() self.transform = BertPredictionHeadTransform(config) # The output weights are the same as the input embeddings, but there is # an output-only bias for each token. self.decoder = nn.Linear(config.hidden_size, config.vocab_size, bias=False) self.bias = nn.Parameter(torch.zeros(config.vocab_size)) # Need a link between the two variables so that the bias is correctly resized with `resize_token_embeddings` self.decoder.bias = self.bias def forward(self, hidden_states): hidden_states = self.transform(hidden_states) hidden_states = self.decoder(hidden_states) return hidden_states ''' class lowercase_ ( unittest.TestCase ): """simple docstring""" def SCREAMING_SNAKE_CASE_ ( self ) ->Dict: lowerCAmelCase = tempfile.mkdtemp() os.makedirs(os.path.join(self.transformer_dir , '''models/bert/''' ) ) lowerCAmelCase = self.transformer_dir shutil.copy( os.path.join(__SCREAMING_SNAKE_CASE , '''src/transformers/models/bert/modeling_bert.py''' ) , os.path.join(self.transformer_dir , '''models/bert/modeling_bert.py''' ) , ) def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[Any]: lowerCAmelCase = '''src/transformers''' shutil.rmtree(self.transformer_dir ) def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None ) ->Union[str, Any]: lowerCAmelCase = comment + F"\nclass {class_name}(nn.Module):\n" + class_code if overwrite_result is not None: lowerCAmelCase = comment + F"\nclass {class_name}(nn.Module):\n" + overwrite_result lowerCAmelCase = black.Mode(target_versions={black.TargetVersion.PYaa} , line_length=119 ) lowerCAmelCase = black.format_str(__SCREAMING_SNAKE_CASE , mode=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = os.path.join(self.transformer_dir , '''new_code.py''' ) with open(__SCREAMING_SNAKE_CASE , '''w''' , newline='''\n''' ) as f: f.write(__SCREAMING_SNAKE_CASE ) if overwrite_result is None: self.assertTrue(len(check_copies.is_copy_consistent(__SCREAMING_SNAKE_CASE ) ) == 0 ) else: check_copies.is_copy_consistent(f.name , overwrite=__SCREAMING_SNAKE_CASE ) with open(__SCREAMING_SNAKE_CASE , '''r''' ) as f: self.assertTrue(f.read() , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->int: lowerCAmelCase = check_copies.find_code_in_transformers('''models.bert.modeling_bert.BertLMPredictionHead''' ) self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->Tuple: # Base copy consistency self.check_copy_consistency( '''# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead''' , '''BertLMPredictionHead''' , REFERENCE_CODE + '''\n''' , ) # With no empty line at the end self.check_copy_consistency( '''# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead''' , '''BertLMPredictionHead''' , __SCREAMING_SNAKE_CASE , ) # Copy consistency with rename self.check_copy_consistency( '''# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->TestModel''' , '''TestModelLMPredictionHead''' , re.sub('''Bert''' , '''TestModel''' , __SCREAMING_SNAKE_CASE ) , ) # Copy consistency with a really long name lowerCAmelCase = '''TestModelWithAReallyLongNameBecauseSomePeopleLikeThatForSomeReason''' self.check_copy_consistency( F"# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->{long_class_name}" , F"{long_class_name}LMPredictionHead" , re.sub('''Bert''' , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) , ) # Copy consistency with overwrite self.check_copy_consistency( '''# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->TestModel''' , '''TestModelLMPredictionHead''' , __SCREAMING_SNAKE_CASE , overwrite_result=re.sub('''Bert''' , '''TestModel''' , __SCREAMING_SNAKE_CASE ) , ) def SCREAMING_SNAKE_CASE_ ( self ) ->Tuple: lowerCAmelCase = check_copies.LOCALIZED_READMES['''README_zh-hans.md'''] lowerCAmelCase = ( '''1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (from Google Research and the''' ''' Toyota Technological Institute at Chicago) released with the paper [ALBERT: A Lite BERT for''' ''' Self-supervised Learning of Language Representations](https://arxiv.org/abs/1909.11942), by Zhenzhong''' ''' Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut.\n1.''' ''' **[DistilBERT](https://huggingface.co/transformers/model_doc/distilbert.html)** (from HuggingFace),''' ''' released together with the paper [DistilBERT, a distilled version of BERT: smaller, faster, cheaper and''' ''' lighter](https://arxiv.org/abs/1910.01108) by Victor Sanh, Lysandre Debut and Thomas Wolf. The same''' ''' method has been applied to compress GPT2 into''' ''' [DistilGPT2](https://github.com/huggingface/transformers/tree/main/examples/distillation), RoBERTa into''' ''' [DistilRoBERTa](https://github.com/huggingface/transformers/tree/main/examples/distillation),''' ''' Multilingual BERT into''' ''' [DistilmBERT](https://github.com/huggingface/transformers/tree/main/examples/distillation) and a German''' ''' version of DistilBERT.\n1. **[ELECTRA](https://huggingface.co/transformers/model_doc/electra.html)**''' ''' (from Google Research/Stanford University) released with the paper [ELECTRA: Pre-training text encoders''' ''' as discriminators rather than generators](https://arxiv.org/abs/2003.10555) by Kevin Clark, Minh-Thang''' ''' Luong, Quoc V. Le, Christopher D. Manning.''' ) lowerCAmelCase = ( '''1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the''' ''' Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of''' ''' Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian''' ''' Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n''' ) lowerCAmelCase = ( '''1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the''' ''' Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of''' ''' Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian''' ''' Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n1.''' ''' **[DistilBERT](https://huggingface.co/transformers/model_doc/distilbert.html)** (来自 HuggingFace) 伴随论文''' ''' [DistilBERT, a distilled version of BERT: smaller, faster, cheaper and''' ''' lighter](https://arxiv.org/abs/1910.01108) 由 Victor Sanh, Lysandre Debut and Thomas Wolf 发布。 The same''' ''' method has been applied to compress GPT2 into''' ''' [DistilGPT2](https://github.com/huggingface/transformers/tree/main/examples/distillation), RoBERTa into''' ''' [DistilRoBERTa](https://github.com/huggingface/transformers/tree/main/examples/distillation),''' ''' Multilingual BERT into''' ''' [DistilmBERT](https://github.com/huggingface/transformers/tree/main/examples/distillation) and a German''' ''' version of DistilBERT.\n1. **[ELECTRA](https://huggingface.co/transformers/model_doc/electra.html)** (来自''' ''' Google Research/Stanford University) 伴随论文 [ELECTRA: Pre-training text encoders as discriminators rather''' ''' than generators](https://arxiv.org/abs/2003.10555) 由 Kevin Clark, Minh-Thang Luong, Quoc V. Le,''' ''' Christopher D. Manning 发布。\n''' ) lowerCAmelCase , lowerCAmelCase = check_copies.convert_to_localized_md( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , localized_readme['''format_model_list'''] ) self.assertFalse(__SCREAMING_SNAKE_CASE ) self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) lowerCAmelCase , lowerCAmelCase = check_copies.convert_to_localized_md( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , localized_readme['''format_model_list'''] ) # Check whether the number of models is equal to README.md after conversion. self.assertTrue(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = ( '''1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (from Google Research and the''' ''' Toyota Technological Institute at Chicago) released with the paper [ALBERT: A Lite BERT for''' ''' Self-supervised Learning of Language Representations](https://arxiv.org/abs/1909.11942), by Zhenzhong''' ''' Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut.''' ) lowerCAmelCase = ( '''1. **[ALBERT](https://huggingface.co/transformers/main/model_doc/albert.html)** (来自 Google Research and''' ''' the Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of''' ''' Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian''' ''' Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n''' ) lowerCAmelCase = ( '''1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the''' ''' Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of''' ''' Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian''' ''' Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n''' ) lowerCAmelCase , lowerCAmelCase = check_copies.convert_to_localized_md( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , localized_readme['''format_model_list'''] ) # Check if the model link is synchronized. self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE )
338
0
"""simple docstring""" def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : Union[str, Any] ) -> List[str]: __a = 0 __a = len(lowerCAmelCase__ ) - 1 while left <= right: # avoid divided by 0 during interpolation if sorted_collection[left] == sorted_collection[right]: if sorted_collection[left] == item: return left else: return None __a = left + ((item - sorted_collection[left]) * (right - left)) // ( sorted_collection[right] - sorted_collection[left] ) # out of range check if point < 0 or point >= len(lowerCAmelCase__ ): return None __a = sorted_collection[point] if current_item == item: return point else: if point < left: __a = left __a = point elif point > right: __a = right __a = point else: if item < current_item: __a = point - 1 else: __a = point + 1 return None def lowercase ( lowerCAmelCase__ : Any , lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : Union[str, Any] ) -> List[str]: # avoid divided by 0 during interpolation if sorted_collection[left] == sorted_collection[right]: if sorted_collection[left] == item: return left else: return None __a = left + ((item - sorted_collection[left]) * (right - left)) // ( sorted_collection[right] - sorted_collection[left] ) # out of range check if point < 0 or point >= len(lowerCAmelCase__ ): return None if sorted_collection[point] == item: return point elif point < left: return interpolation_search_by_recursion(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) elif point > right: return interpolation_search_by_recursion(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) else: if sorted_collection[point] > item: return interpolation_search_by_recursion( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , point - 1 ) else: return interpolation_search_by_recursion( lowerCAmelCase__ , lowerCAmelCase__ , point + 1 , lowerCAmelCase__ ) def lowercase ( lowerCAmelCase__ : int ) -> List[str]: if collection != sorted(lowerCAmelCase__ ): raise ValueError('''Collection must be ascending sorted''' ) return True if __name__ == "__main__": import sys lowercase_ = 0 if debug == 1: lowercase_ = [1_0, 3_0, 4_0, 4_5, 5_0, 6_6, 7_7, 9_3] try: __assert_sorted(collection) except ValueError: sys.exit("Sequence must be ascending sorted to apply interpolation search") lowercase_ = 6_7 lowercase_ = interpolation_search(collection, target) if result is not None: print(F'''{target} found at positions: {result}''') else: print("Not found")
45
import pytest from datasets.splits import SplitDict, SplitInfo from datasets.utils.py_utils import asdict @pytest.mark.parametrize( '''split_dict''' , [ SplitDict(), SplitDict({'''train''': SplitInfo(name='''train''' , num_bytes=1_3_3_7 , num_examples=4_2 , dataset_name='''my_dataset''' )} ), SplitDict({'''train''': SplitInfo(name='''train''' , num_bytes=1_3_3_7 , num_examples=4_2 )} ), SplitDict({'''train''': SplitInfo()} ), ] , ) def SCREAMING_SNAKE_CASE_ ( snake_case__ ) -> Union[str, Any]: lowerCAmelCase = split_dict._to_yaml_list() assert len(snake_case__ ) == len(snake_case__ ) lowerCAmelCase = SplitDict._from_yaml_list(snake_case__ ) for split_name, split_info in split_dict.items(): # dataset_name field is deprecated, and is therefore not part of the YAML dump lowerCAmelCase = None # the split name of split_dict takes over the name of the split info object lowerCAmelCase = split_name assert split_dict == reloaded @pytest.mark.parametrize( '''split_info''' , [SplitInfo(), SplitInfo(dataset_name=snake_case__ ), SplitInfo(dataset_name='''my_dataset''' )] ) def SCREAMING_SNAKE_CASE_ ( snake_case__ ) -> Optional[int]: # For backward compatibility, we need asdict(split_dict) to return split info dictrionaries with the "dataset_name" # field even if it's deprecated. This way old versionso of `datasets` can still reload dataset_infos.json files lowerCAmelCase = asdict(SplitDict({'''train''': split_info} ) ) assert "dataset_name" in split_dict_asdict["train"] assert split_dict_asdict["train"]["dataset_name"] == split_info.dataset_name
338
0
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) SCREAMING_SNAKE_CASE__ = { "configuration_electra": ["ELECTRA_PRETRAINED_CONFIG_ARCHIVE_MAP", "ElectraConfig", "ElectraOnnxConfig"], "tokenization_electra": ["ElectraTokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = ["ElectraTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ "ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST", "ElectraForCausalLM", "ElectraForMaskedLM", "ElectraForMultipleChoice", "ElectraForPreTraining", "ElectraForQuestionAnswering", "ElectraForSequenceClassification", "ElectraForTokenClassification", "ElectraModel", "ElectraPreTrainedModel", "load_tf_weights_in_electra", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ "TF_ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST", "TFElectraForMaskedLM", "TFElectraForMultipleChoice", "TFElectraForPreTraining", "TFElectraForQuestionAnswering", "TFElectraForSequenceClassification", "TFElectraForTokenClassification", "TFElectraModel", "TFElectraPreTrainedModel", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: SCREAMING_SNAKE_CASE__ = [ "FlaxElectraForCausalLM", "FlaxElectraForMaskedLM", "FlaxElectraForMultipleChoice", "FlaxElectraForPreTraining", "FlaxElectraForQuestionAnswering", "FlaxElectraForSequenceClassification", "FlaxElectraForTokenClassification", "FlaxElectraModel", "FlaxElectraPreTrainedModel", ] if TYPE_CHECKING: from .configuration_electra import ELECTRA_PRETRAINED_CONFIG_ARCHIVE_MAP, ElectraConfig, ElectraOnnxConfig from .tokenization_electra import ElectraTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_electra_fast import ElectraTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_electra import ( ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST, ElectraForCausalLM, ElectraForMaskedLM, ElectraForMultipleChoice, ElectraForPreTraining, ElectraForQuestionAnswering, ElectraForSequenceClassification, ElectraForTokenClassification, ElectraModel, ElectraPreTrainedModel, load_tf_weights_in_electra, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_electra import ( TF_ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST, TFElectraForMaskedLM, TFElectraForMultipleChoice, TFElectraForPreTraining, TFElectraForQuestionAnswering, TFElectraForSequenceClassification, TFElectraForTokenClassification, TFElectraModel, TFElectraPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_electra import ( FlaxElectraForCausalLM, FlaxElectraForMaskedLM, FlaxElectraForMultipleChoice, FlaxElectraForPreTraining, FlaxElectraForQuestionAnswering, FlaxElectraForSequenceClassification, FlaxElectraForTokenClassification, FlaxElectraModel, FlaxElectraPreTrainedModel, ) else: import sys SCREAMING_SNAKE_CASE__ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
46
import unittest import numpy as np def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ , snake_case__ = None , ) -> np.ndarray: lowerCAmelCase = np.shape(snake_case__ ) lowerCAmelCase = np.shape(snake_case__ ) lowerCAmelCase = np.shape(snake_case__ ) if shape_a[0] != shape_b[0]: lowerCAmelCase = ( '''Expected the same number of rows for A and B. ''' f"Instead found A of size {shape_a} and B of size {shape_b}" ) raise ValueError(snake_case__ ) if shape_b[1] != shape_c[1]: lowerCAmelCase = ( '''Expected the same number of columns for B and C. ''' f"Instead found B of size {shape_b} and C of size {shape_c}" ) raise ValueError(snake_case__ ) lowerCAmelCase = pseudo_inv if a_inv is None: try: lowerCAmelCase = np.linalg.inv(snake_case__ ) except np.linalg.LinAlgError: raise ValueError( '''Input matrix A is not invertible. Cannot compute Schur complement.''' ) return mat_c - mat_b.T @ a_inv @ mat_b class lowercase_ ( unittest.TestCase ): """simple docstring""" def SCREAMING_SNAKE_CASE_ ( self ) ->None: lowerCAmelCase = np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]] ) lowerCAmelCase = np.array([[0, 3], [3, 0], [2, 3]] ) lowerCAmelCase = np.array([[2, 1], [6, 3]] ) lowerCAmelCase = schur_complement(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) lowerCAmelCase = np.block([[a, b], [b.T, c]] ) lowerCAmelCase = np.linalg.det(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = np.linalg.det(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = np.linalg.det(__SCREAMING_SNAKE_CASE ) self.assertAlmostEqual(__SCREAMING_SNAKE_CASE , det_a * det_s ) def SCREAMING_SNAKE_CASE_ ( self ) ->None: lowerCAmelCase = np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]] ) lowerCAmelCase = np.array([[0, 3], [3, 0], [2, 3]] ) lowerCAmelCase = np.array([[2, 1], [6, 3]] ) with self.assertRaises(__SCREAMING_SNAKE_CASE ): schur_complement(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->None: lowerCAmelCase = np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]] ) lowerCAmelCase = np.array([[0, 3], [3, 0], [2, 3]] ) lowerCAmelCase = np.array([[2, 1, 3], [6, 3, 5]] ) with self.assertRaises(__SCREAMING_SNAKE_CASE ): schur_complement(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) if __name__ == "__main__": import doctest doctest.testmod() unittest.main()
338
0
'''simple docstring''' import argparse import json from pathlib import Path import requests import timm import torch from huggingface_hub import hf_hub_download from PIL import Image from timm.data import resolve_data_config from timm.data.transforms_factory import create_transform from transformers import ( BitConfig, ViTHybridConfig, ViTHybridForImageClassification, ViTHybridImageProcessor, ViTHybridModel, ) from transformers.image_utils import PILImageResampling from transformers.utils import logging logging.set_verbosity_info() lowerCamelCase : List[Any] = logging.get_logger(__name__) def _lowerCAmelCase ( _UpperCamelCase : str , _UpperCamelCase : List[str]=False ) -> Optional[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =[] # fmt: off # stem: rename_keys.append(('cls_token', 'vit.embeddings.cls_token') ) rename_keys.append(('pos_embed', 'vit.embeddings.position_embeddings') ) rename_keys.append(('patch_embed.proj.weight', 'vit.embeddings.patch_embeddings.projection.weight') ) rename_keys.append(('patch_embed.proj.bias', 'vit.embeddings.patch_embeddings.projection.bias') ) # backbone rename_keys.append(('patch_embed.backbone.stem.conv.weight', 'vit.embeddings.patch_embeddings.backbone.bit.embedder.convolution.weight') ) rename_keys.append(('patch_embed.backbone.stem.norm.weight', 'vit.embeddings.patch_embeddings.backbone.bit.embedder.norm.weight') ) rename_keys.append(('patch_embed.backbone.stem.norm.bias', 'vit.embeddings.patch_embeddings.backbone.bit.embedder.norm.bias') ) for stage_idx in range(len(config.backbone_config.depths ) ): for layer_idx in range(config.backbone_config.depths[stage_idx] ): rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.conv1.weight", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.conv1.weight") ) rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm1.weight", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm1.weight") ) rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm1.bias", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm1.bias") ) rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.conv2.weight", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.conv2.weight") ) rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm2.weight", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm2.weight") ) rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm2.bias", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm2.bias") ) rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.conv3.weight", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.conv3.weight") ) rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm3.weight", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm3.weight") ) rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm3.bias", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm3.bias") ) rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.0.downsample.conv.weight", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.0.downsample.conv.weight") ) rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.0.downsample.norm.weight", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.0.downsample.norm.weight") ) rename_keys.append((f"patch_embed.backbone.stages.{stage_idx}.blocks.0.downsample.norm.bias", f"vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.0.downsample.norm.bias") ) # transformer encoder for i in range(config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append((f"blocks.{i}.norm1.weight", f"vit.encoder.layer.{i}.layernorm_before.weight") ) rename_keys.append((f"blocks.{i}.norm1.bias", f"vit.encoder.layer.{i}.layernorm_before.bias") ) rename_keys.append((f"blocks.{i}.attn.proj.weight", f"vit.encoder.layer.{i}.attention.output.dense.weight") ) rename_keys.append((f"blocks.{i}.attn.proj.bias", f"vit.encoder.layer.{i}.attention.output.dense.bias") ) rename_keys.append((f"blocks.{i}.norm2.weight", f"vit.encoder.layer.{i}.layernorm_after.weight") ) rename_keys.append((f"blocks.{i}.norm2.bias", f"vit.encoder.layer.{i}.layernorm_after.bias") ) rename_keys.append((f"blocks.{i}.mlp.fc1.weight", f"vit.encoder.layer.{i}.intermediate.dense.weight") ) rename_keys.append((f"blocks.{i}.mlp.fc1.bias", f"vit.encoder.layer.{i}.intermediate.dense.bias") ) rename_keys.append((f"blocks.{i}.mlp.fc2.weight", f"vit.encoder.layer.{i}.output.dense.weight") ) rename_keys.append((f"blocks.{i}.mlp.fc2.bias", f"vit.encoder.layer.{i}.output.dense.bias") ) if base_model: # layernorm + pooler rename_keys.extend( [ ('norm.weight', 'layernorm.weight'), ('norm.bias', 'layernorm.bias'), ('pre_logits.fc.weight', 'pooler.dense.weight'), ('pre_logits.fc.bias', 'pooler.dense.bias'), ] ) # if just the base model, we should remove "vit" from all keys that start with "vit" _SCREAMING_SNAKE_CASE =[(pair[0], pair[1][4:]) if pair[1].startswith('vit' ) else pair for pair in rename_keys] else: # layernorm + classification head rename_keys.extend( [ ('norm.weight', 'vit.layernorm.weight'), ('norm.bias', 'vit.layernorm.bias'), ('head.weight', 'classifier.weight'), ('head.bias', 'classifier.bias'), ] ) # fmt: on return rename_keys def _lowerCAmelCase ( _UpperCamelCase : Tuple , _UpperCamelCase : Any , _UpperCamelCase : List[str]=False ) -> int: """simple docstring""" for i in range(config.num_hidden_layers ): if base_model: _SCREAMING_SNAKE_CASE ='' else: _SCREAMING_SNAKE_CASE ='vit.' # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) _SCREAMING_SNAKE_CASE =state_dict.pop(f"blocks.{i}.attn.qkv.weight" ) _SCREAMING_SNAKE_CASE =state_dict.pop(f"blocks.{i}.attn.qkv.bias" ) # next, add query, keys and values (in that order) to the state dict _SCREAMING_SNAKE_CASE =in_proj_weight[ : config.hidden_size, : ] _SCREAMING_SNAKE_CASE =in_proj_bias[: config.hidden_size] _SCREAMING_SNAKE_CASE =in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] _SCREAMING_SNAKE_CASE =in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] _SCREAMING_SNAKE_CASE =in_proj_weight[ -config.hidden_size :, : ] _SCREAMING_SNAKE_CASE =in_proj_bias[-config.hidden_size :] def _lowerCAmelCase ( _UpperCamelCase : Tuple ) -> List[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =['head.weight', 'head.bias'] for k in ignore_keys: state_dict.pop(_UpperCamelCase , _UpperCamelCase ) def _lowerCAmelCase ( _UpperCamelCase : Optional[Any] , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : str ) -> List[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =dct.pop(_UpperCamelCase ) _SCREAMING_SNAKE_CASE =val def _lowerCAmelCase ( ) -> Optional[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE ='http://images.cocodataset.org/val2017/000000039769.jpg' _SCREAMING_SNAKE_CASE =Image.open(requests.get(_UpperCamelCase , stream=_UpperCamelCase ).raw ) return im @torch.no_grad() def _lowerCAmelCase ( _UpperCamelCase : int , _UpperCamelCase : Dict , _UpperCamelCase : Dict=False ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE =BitConfig( global_padding='same' , layer_type='bottleneck' , depths=(3, 4, 9) , out_features=['stage3'] , embedding_dynamic_padding=_UpperCamelCase , ) _SCREAMING_SNAKE_CASE =ViTHybridConfig(backbone_config=_UpperCamelCase , image_size=3_84 , num_labels=10_00 ) _SCREAMING_SNAKE_CASE =False # load original model from timm _SCREAMING_SNAKE_CASE =timm.create_model(_UpperCamelCase , pretrained=_UpperCamelCase ) timm_model.eval() # load state_dict of original model, remove and rename some keys _SCREAMING_SNAKE_CASE =timm_model.state_dict() if base_model: remove_classification_head_(_UpperCamelCase ) _SCREAMING_SNAKE_CASE =create_rename_keys(_UpperCamelCase , _UpperCamelCase ) for src, dest in rename_keys: rename_key(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) read_in_q_k_v(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) _SCREAMING_SNAKE_CASE ='huggingface/label-files' _SCREAMING_SNAKE_CASE ='imagenet-1k-id2label.json' _SCREAMING_SNAKE_CASE =json.load(open(hf_hub_download(_UpperCamelCase , _UpperCamelCase , repo_type='dataset' ) , 'r' ) ) _SCREAMING_SNAKE_CASE ={int(_UpperCamelCase ): v for k, v in idalabel.items()} _SCREAMING_SNAKE_CASE =idalabel _SCREAMING_SNAKE_CASE ={v: k for k, v in idalabel.items()} # load HuggingFace model if vit_name[-5:] == "in21k": _SCREAMING_SNAKE_CASE =ViTHybridModel(_UpperCamelCase ).eval() else: _SCREAMING_SNAKE_CASE =ViTHybridForImageClassification(_UpperCamelCase ).eval() model.load_state_dict(_UpperCamelCase ) # create image processor _SCREAMING_SNAKE_CASE =create_transform(**resolve_data_config({} , model=_UpperCamelCase ) ) _SCREAMING_SNAKE_CASE =transform.transforms _SCREAMING_SNAKE_CASE ={ 'bilinear': PILImageResampling.BILINEAR, 'bicubic': PILImageResampling.BICUBIC, 'nearest': PILImageResampling.NEAREST, } _SCREAMING_SNAKE_CASE =ViTHybridImageProcessor( do_resize=_UpperCamelCase , size={'shortest_edge': timm_transforms[0].size} , resample=pillow_resamplings[timm_transforms[0].interpolation.value] , do_center_crop=_UpperCamelCase , crop_size={'height': timm_transforms[1].size[0], 'width': timm_transforms[1].size[1]} , do_normalize=_UpperCamelCase , image_mean=timm_transforms[-1].mean.tolist() , image_std=timm_transforms[-1].std.tolist() , ) _SCREAMING_SNAKE_CASE =prepare_img() _SCREAMING_SNAKE_CASE =transform(_UpperCamelCase ).unsqueeze(0 ) _SCREAMING_SNAKE_CASE =processor(_UpperCamelCase , return_tensors='pt' ).pixel_values # verify pixel values assert torch.allclose(_UpperCamelCase , _UpperCamelCase ) # verify logits with torch.no_grad(): _SCREAMING_SNAKE_CASE =model(_UpperCamelCase ) _SCREAMING_SNAKE_CASE =outputs.logits print('Predicted class:' , logits.argmax(-1 ).item() ) if base_model: _SCREAMING_SNAKE_CASE =timm_model.forward_features(_UpperCamelCase ) assert timm_pooled_output.shape == outputs.pooler_output.shape assert torch.allclose(_UpperCamelCase , outputs.pooler_output , atol=1E-3 ) else: _SCREAMING_SNAKE_CASE =timm_model(_UpperCamelCase ) assert timm_logits.shape == outputs.logits.shape assert torch.allclose(_UpperCamelCase , outputs.logits , atol=1E-3 ) print('Looks ok!' ) if pytorch_dump_folder_path is not None: Path(_UpperCamelCase ).mkdir(exist_ok=_UpperCamelCase ) print(f"Saving model {vit_name} to {pytorch_dump_folder_path}" ) model.save_pretrained(_UpperCamelCase ) print(f"Saving processor to {pytorch_dump_folder_path}" ) processor.save_pretrained(_UpperCamelCase ) if push_to_hub: print(f"Pushing model and processor to the hub {vit_name}" ) model.push_to_hub(f"ybelkada/{vit_name}" ) processor.push_to_hub(f"ybelkada/{vit_name}" ) if __name__ == "__main__": lowerCamelCase : Optional[int] = argparse.ArgumentParser() # Required parameters parser.add_argument( "--vit_name", default="vit_base_r50_s16_384", type=str, help="Name of the hybrid ViT timm model you'd like to convert.", ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model directory." ) parser.add_argument( "--push_to_hub", action="store_true", help="Whether to upload the model to the HuggingFace hub." ) lowerCamelCase : Tuple = parser.parse_args() convert_vit_checkpoint(args.vit_name, args.pytorch_dump_folder_path, args.push_to_hub)
47
import argparse import hashlib import os import urllib import warnings import torch from torch import nn from tqdm import tqdm from transformers import WhisperConfig, WhisperForConditionalGeneration lowercase__ : Any = { '''tiny.en''': '''https://openaipublic.azureedge.net/main/whisper/models/d3dd57d32accea0b295c96e26691aa14d8822fac7d9d27d5dc00b4ca2826dd03/tiny.en.pt''', '''tiny''': '''https://openaipublic.azureedge.net/main/whisper/models/65147644a518d12f04e32d6f3b26facc3f8dd46e5390956a9424a650c0ce22b9/tiny.pt''', '''base.en''': '''https://openaipublic.azureedge.net/main/whisper/models/25a8566e1d0c1e2231d1c762132cd20e0f96a85d16145c3a00adf5d1ac670ead/base.en.pt''', '''base''': '''https://openaipublic.azureedge.net/main/whisper/models/ed3a0b6b1c0edf879ad9b11b1af5a0e6ab5db9205f891f668f8b0e6c6326e34e/base.pt''', '''small.en''': '''https://openaipublic.azureedge.net/main/whisper/models/f953ad0fd29cacd07d5a9eda5624af0f6bcf2258be67c92b79389873d91e0872/small.en.pt''', '''small''': '''https://openaipublic.azureedge.net/main/whisper/models/9ecf779972d90ba49c06d968637d720dd632c55bbf19d441fb42bf17a411e794/small.pt''', '''medium.en''': '''https://openaipublic.azureedge.net/main/whisper/models/d7440d1dc186f76616474e0ff0b3b6b879abc9d1a4926b7adfa41db2d497ab4f/medium.en.pt''', '''medium''': '''https://openaipublic.azureedge.net/main/whisper/models/345ae4da62f9b3d59415adc60127b97c714f32e89e936602e85993674d08dcb1/medium.pt''', '''large''': '''https://openaipublic.azureedge.net/main/whisper/models/e4b87e7e0bf463eb8e6956e646f1e277e901512310def2c24bf0e11bd3c28e9a/large.pt''', '''large-v2''': '''https://openaipublic.azureedge.net/main/whisper/models/81f7c96c852ee8fc832187b0132e569d6c3065a3252ed18e56effd0b6a73e524/large-v2.pt''', } def SCREAMING_SNAKE_CASE_ ( snake_case__ ) -> str: lowerCAmelCase = ['''layers''', '''blocks'''] for k in ignore_keys: state_dict.pop(snake_case__ , snake_case__ ) lowercase__ : List[Any] = { '''blocks''': '''layers''', '''mlp.0''': '''fc1''', '''mlp.2''': '''fc2''', '''mlp_ln''': '''final_layer_norm''', '''.attn.query''': '''.self_attn.q_proj''', '''.attn.key''': '''.self_attn.k_proj''', '''.attn.value''': '''.self_attn.v_proj''', '''.attn_ln''': '''.self_attn_layer_norm''', '''.attn.out''': '''.self_attn.out_proj''', '''.cross_attn.query''': '''.encoder_attn.q_proj''', '''.cross_attn.key''': '''.encoder_attn.k_proj''', '''.cross_attn.value''': '''.encoder_attn.v_proj''', '''.cross_attn_ln''': '''.encoder_attn_layer_norm''', '''.cross_attn.out''': '''.encoder_attn.out_proj''', '''decoder.ln.''': '''decoder.layer_norm.''', '''encoder.ln.''': '''encoder.layer_norm.''', '''token_embedding''': '''embed_tokens''', '''encoder.positional_embedding''': '''encoder.embed_positions.weight''', '''decoder.positional_embedding''': '''decoder.embed_positions.weight''', '''ln_post''': '''layer_norm''', } def SCREAMING_SNAKE_CASE_ ( snake_case__ ) -> Union[str, Any]: lowerCAmelCase = list(s_dict.keys() ) for key in keys: lowerCAmelCase = key for k, v in WHISPER_MAPPING.items(): if k in key: lowerCAmelCase = new_key.replace(snake_case__ , snake_case__ ) print(f"{key} -> {new_key}" ) lowerCAmelCase = s_dict.pop(snake_case__ ) return s_dict def SCREAMING_SNAKE_CASE_ ( snake_case__ ) -> Union[str, Any]: lowerCAmelCase , lowerCAmelCase = emb.weight.shape lowerCAmelCase = nn.Linear(snake_case__ , snake_case__ , bias=snake_case__ ) lowerCAmelCase = emb.weight.data return lin_layer def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ ) -> bytes: os.makedirs(snake_case__ , exist_ok=snake_case__ ) lowerCAmelCase = os.path.basename(snake_case__ ) lowerCAmelCase = url.split('''/''' )[-2] lowerCAmelCase = os.path.join(snake_case__ , snake_case__ ) if os.path.exists(snake_case__ ) and not os.path.isfile(snake_case__ ): raise RuntimeError(f"{download_target} exists and is not a regular file" ) if os.path.isfile(snake_case__ ): lowerCAmelCase = open(snake_case__ , '''rb''' ).read() if hashlib.shaaaa(snake_case__ ).hexdigest() == expected_shaaaa: return model_bytes else: warnings.warn(f"{download_target} exists, but the SHA256 checksum does not match; re-downloading the file" ) with urllib.request.urlopen(snake_case__ ) as source, open(snake_case__ , '''wb''' ) as output: with tqdm( total=int(source.info().get('''Content-Length''' ) ) , ncols=8_0 , unit='''iB''' , unit_scale=snake_case__ , unit_divisor=1_0_2_4 ) as loop: while True: lowerCAmelCase = source.read(8_1_9_2 ) if not buffer: break output.write(snake_case__ ) loop.update(len(snake_case__ ) ) lowerCAmelCase = open(snake_case__ , '''rb''' ).read() if hashlib.shaaaa(snake_case__ ).hexdigest() != expected_shaaaa: raise RuntimeError( '''Model has been downloaded but the SHA256 checksum does not not match. Please retry loading the model.''' ) return model_bytes def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ ) -> str: if ".pt" not in checkpoint_path: lowerCAmelCase = _download(_MODELS[checkpoint_path] ) else: lowerCAmelCase = torch.load(snake_case__ , map_location='''cpu''' ) lowerCAmelCase = original_checkpoint['''dims'''] lowerCAmelCase = original_checkpoint['''model_state_dict'''] lowerCAmelCase = state_dict['''decoder.token_embedding.weight'''] remove_ignore_keys_(snake_case__ ) rename_keys(snake_case__ ) lowerCAmelCase = True lowerCAmelCase = state_dict['''decoder.layers.0.fc1.weight'''].shape[0] lowerCAmelCase = WhisperConfig( vocab_size=dimensions['''n_vocab'''] , encoder_ffn_dim=snake_case__ , decoder_ffn_dim=snake_case__ , num_mel_bins=dimensions['''n_mels'''] , d_model=dimensions['''n_audio_state'''] , max_target_positions=dimensions['''n_text_ctx'''] , encoder_layers=dimensions['''n_audio_layer'''] , encoder_attention_heads=dimensions['''n_audio_head'''] , decoder_layers=dimensions['''n_text_layer'''] , decoder_attention_heads=dimensions['''n_text_state'''] , max_source_positions=dimensions['''n_audio_ctx'''] , ) lowerCAmelCase = WhisperForConditionalGeneration(snake_case__ ) lowerCAmelCase , lowerCAmelCase = model.model.load_state_dict(snake_case__ , strict=snake_case__ ) if len(snake_case__ ) > 0 and not set(snake_case__ ) <= { "encoder.embed_positions.weights", "decoder.embed_positions.weights", }: raise ValueError( '''Only `encoder.embed_positions.weights` and `decoder.embed_positions.weights` are allowed to be missing,''' f" but all the following weights are missing {missing}" ) if tie_embeds: lowerCAmelCase = make_linear_from_emb(model.model.decoder.embed_tokens ) else: lowerCAmelCase = proj_out_weights model.save_pretrained(snake_case__ ) if __name__ == "__main__": lowercase__ : List[str] = argparse.ArgumentParser() # # Required parameters parser.add_argument('''--checkpoint_path''', type=str, help='''Patht to the downloaded checkpoints''') parser.add_argument('''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''') lowercase__ : int = parser.parse_args() convert_openai_whisper_to_tfms(args.checkpoint_path, args.pytorch_dump_folder_path)
338
0
def A ( _SCREAMING_SNAKE_CASE ) -> list: if n_term == "": return [] lowerCamelCase : list = [] for temp in range(int(_SCREAMING_SNAKE_CASE ) ): series.append(f'''1/{temp + 1}''' if series else "1" ) return series if __name__ == "__main__": SCREAMING_SNAKE_CASE__ : Dict = input('Enter the last number (nth term) of the Harmonic Series') print('Formula of Harmonic Series => 1+1/2+1/3 ..... 1/n') print(harmonic_series(nth_term))
48
from ...processing_utils import ProcessorMixin class lowercase_ ( UpperCamelCase_ ): """simple docstring""" UpperCAmelCase_ : List[Any] = ["""image_processor""", """feature_extractor"""] UpperCAmelCase_ : Optional[int] = """TvltImageProcessor""" UpperCAmelCase_ : Optional[int] = """TvltFeatureExtractor""" def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ->Optional[int]: super().__init__(image_processor=__SCREAMING_SNAKE_CASE , feature_extractor=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = image_processor lowerCAmelCase = feature_extractor def __call__( self , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=False , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) ->List[Any]: if images is None and audio is None: raise ValueError('''You need to specify either an `images` or `audio` input to process.''' ) lowerCAmelCase = None if images is not None: lowerCAmelCase = self.image_processor(__SCREAMING_SNAKE_CASE , mask_pixel=__SCREAMING_SNAKE_CASE , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) if images_mixed is not None: lowerCAmelCase = self.image_processor(__SCREAMING_SNAKE_CASE , is_mixed=__SCREAMING_SNAKE_CASE , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) if audio is not None: lowerCAmelCase = self.feature_extractor( __SCREAMING_SNAKE_CASE , *__SCREAMING_SNAKE_CASE , sampling_rate=__SCREAMING_SNAKE_CASE , mask_audio=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) lowerCAmelCase = {} if audio is not None: output_dict.update(__SCREAMING_SNAKE_CASE ) if images is not None: output_dict.update(__SCREAMING_SNAKE_CASE ) if images_mixed_dict is not None: output_dict.update(__SCREAMING_SNAKE_CASE ) return output_dict @property def SCREAMING_SNAKE_CASE_ ( self ) ->Any: lowerCAmelCase = self.image_processor.model_input_names lowerCAmelCase = self.feature_extractor.model_input_names return list(dict.fromkeys(image_processor_input_names + feature_extractor_input_names ) )
338
0
import os import unicodedata from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import SPIECE_UNDERLINE, logging __snake_case :Optional[Any] = logging.get_logger(__name__) __snake_case :Any = {'''vocab_file''': '''spiece.model'''} __snake_case :Optional[Any] = { '''vocab_file''': { '''TsinghuaAI/CPM-Generate''': '''https://huggingface.co/TsinghuaAI/CPM-Generate/resolve/main/spiece.model''', } } class _A ( __UpperCAmelCase ): def __init__( self : Optional[int] , __SCREAMING_SNAKE_CASE : List[Any] , __SCREAMING_SNAKE_CASE : int=False , __SCREAMING_SNAKE_CASE : Optional[Any]=True , __SCREAMING_SNAKE_CASE : Union[str, Any]=False , __SCREAMING_SNAKE_CASE : Dict="<s>" , __SCREAMING_SNAKE_CASE : int="</s>" , __SCREAMING_SNAKE_CASE : Union[str, Any]="<unk>" , __SCREAMING_SNAKE_CASE : Optional[Any]="<sep>" , __SCREAMING_SNAKE_CASE : str="<pad>" , __SCREAMING_SNAKE_CASE : Tuple="<cls>" , __SCREAMING_SNAKE_CASE : int="<mask>" , __SCREAMING_SNAKE_CASE : Dict=["<eop>", "<eod>"] , __SCREAMING_SNAKE_CASE : Optional[Dict[str, Any]] = None , **__SCREAMING_SNAKE_CASE : str , ): '''simple docstring''' __a = AddedToken(__SCREAMING_SNAKE_CASE , lstrip=__SCREAMING_SNAKE_CASE , rstrip=__SCREAMING_SNAKE_CASE) if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE) else mask_token __a = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( do_lower_case=__SCREAMING_SNAKE_CASE , remove_space=__SCREAMING_SNAKE_CASE , keep_accents=__SCREAMING_SNAKE_CASE , bos_token=__SCREAMING_SNAKE_CASE , eos_token=__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 , additional_special_tokens=__SCREAMING_SNAKE_CASE , sp_model_kwargs=self.sp_model_kwargs , **__SCREAMING_SNAKE_CASE , ) __a = 3 __a = do_lower_case __a = remove_space __a = keep_accents __a = vocab_file __a = spm.SentencePieceProcessor(**self.sp_model_kwargs) self.sp_model.Load(__SCREAMING_SNAKE_CASE) try: import jieba except ModuleNotFoundError as error: raise error.__class__( '''You need to install jieba to use CpmTokenizer or CpmTokenizerFast. ''' '''See https://pypi.org/project/jieba/ for installation.''') __a = jieba __a = str.maketrans(''' \n''' , '''\u2582\u2583''') @property # Copied from transformers.models.xlnet.tokenization_xlnet.XLNetTokenizer.vocab_size def _lowerCamelCase ( self : Optional[int]): '''simple docstring''' return len(self.sp_model) def _lowerCamelCase ( self : str): '''simple docstring''' __a = {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 : str): '''simple docstring''' __a = self.__dict__.copy() __a = None return state def __setstate__( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : Union[str, Any]): '''simple docstring''' __a = d # for backward compatibility if not hasattr(self , '''sp_model_kwargs'''): __a = {} __a = spm.SentencePieceProcessor(**self.sp_model_kwargs) self.sp_model.Load(self.vocab_file) def _lowerCamelCase ( self : Optional[Any] , __SCREAMING_SNAKE_CASE : List[Any]): '''simple docstring''' if self.remove_space: __a = ''' '''.join(inputs.strip().split()) else: __a = inputs __a = outputs.replace('''``''' , '''"''').replace('''\'\'''' , '''"''') if not self.keep_accents: __a = unicodedata.normalize('''NFKD''' , __SCREAMING_SNAKE_CASE) __a = ''''''.join([c for c in outputs if not unicodedata.combining(__SCREAMING_SNAKE_CASE)]) if self.do_lower_case: __a = outputs.lower() return outputs def _lowerCamelCase ( self : Dict , __SCREAMING_SNAKE_CASE : str): '''simple docstring''' __a = self.preprocess_text(__SCREAMING_SNAKE_CASE) __a = self.sp_model.encode(__SCREAMING_SNAKE_CASE , out_type=__SCREAMING_SNAKE_CASE) __a = [] for piece in pieces: if len(__SCREAMING_SNAKE_CASE) > 1 and piece[-1] == str(''',''') and piece[-2].isdigit(): __a = self.sp_model.EncodeAsPieces(piece[:-1].replace(__SCREAMING_SNAKE_CASE , '''''')) if piece[0] != SPIECE_UNDERLINE and cur_pieces[0][0] == SPIECE_UNDERLINE: if len(cur_pieces[0]) == 1: __a = cur_pieces[1:] else: __a = cur_pieces[0][1:] cur_pieces.append(piece[-1]) new_pieces.extend(__SCREAMING_SNAKE_CASE) else: new_pieces.append(__SCREAMING_SNAKE_CASE) return new_pieces def _lowerCamelCase ( self : Optional[Any] , __SCREAMING_SNAKE_CASE : Union[str, Any]): '''simple docstring''' return self.sp_model.PieceToId(__SCREAMING_SNAKE_CASE) def _lowerCamelCase ( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : Union[str, Any]): '''simple docstring''' return self.sp_model.IdToPiece(__SCREAMING_SNAKE_CASE) def _lowerCamelCase ( self : str , __SCREAMING_SNAKE_CASE : List[str]): '''simple docstring''' __a = ''''''.join(__SCREAMING_SNAKE_CASE).replace(__SCREAMING_SNAKE_CASE , ''' ''').strip() return out_string def _lowerCamelCase ( self : Dict , __SCREAMING_SNAKE_CASE : List[int] , __SCREAMING_SNAKE_CASE : Optional[List[int]] = None): '''simple docstring''' __a = [self.sep_token_id] __a = [self.cls_token_id] if token_ids_a is None: return token_ids_a + sep + cls return token_ids_a + sep + token_ids_a + sep + cls def _lowerCamelCase ( self : str , __SCREAMING_SNAKE_CASE : List[int] , __SCREAMING_SNAKE_CASE : Optional[List[int]] = None , __SCREAMING_SNAKE_CASE : bool = False): '''simple docstring''' 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 not None: return ([0] * len(__SCREAMING_SNAKE_CASE)) + [1] + ([0] * len(__SCREAMING_SNAKE_CASE)) + [1, 1] return ([0] * len(__SCREAMING_SNAKE_CASE)) + [1, 1] def _lowerCamelCase ( self : Optional[int] , __SCREAMING_SNAKE_CASE : List[int] , __SCREAMING_SNAKE_CASE : Optional[List[int]] = None): '''simple docstring''' __a = [self.sep_token_id] __a = [2] if token_ids_a is None: return len(token_ids_a + sep) * [0] + cls_segment_id return len(token_ids_a + sep) * [0] + len(token_ids_a + sep) * [1] + cls_segment_id def _lowerCamelCase ( self : Optional[Any] , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : Optional[str] = None): '''simple docstring''' if not os.path.isdir(__SCREAMING_SNAKE_CASE): logger.error(F'Vocabulary path ({save_directory}) should be a directory') return __a = 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) and os.path.isfile(self.vocab_file): copyfile(self.vocab_file , __SCREAMING_SNAKE_CASE) elif not os.path.isfile(self.vocab_file): with open(__SCREAMING_SNAKE_CASE , '''wb''') as fi: __a = self.sp_model.serialized_model_proto() fi.write(__SCREAMING_SNAKE_CASE) return (out_vocab_file,) def _lowerCamelCase ( self : Any , *__SCREAMING_SNAKE_CASE : int , **__SCREAMING_SNAKE_CASE : List[Any]): '''simple docstring''' __a = super()._decode(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE) __a = text.replace(''' ''' , '''''').replace('''\u2582''' , ''' ''').replace('''\u2583''' , '''\n''') return text
49
def SCREAMING_SNAKE_CASE_ ( snake_case__ ) -> List[str]: lowerCAmelCase = len(snake_case__ ) for i in range(length - 1 ): lowerCAmelCase = i for k in range(i + 1 , snake_case__ ): if collection[k] < collection[least]: lowerCAmelCase = k if least != i: lowerCAmelCase , lowerCAmelCase = (collection[i], collection[least]) return collection if __name__ == "__main__": lowercase__ : Optional[int] = input('''Enter numbers separated by a comma:\n''').strip() lowercase__ : str = [int(item) for item in user_input.split(''',''')] print(selection_sort(unsorted))
338
0
import collections import tempfile import unittest import numpy as np from transformers.testing_utils import ( is_pt_flax_cross_test, require_flax, require_torch, require_vision, slow, torch_device, ) from transformers.utils import is_flax_available, is_torch_available, is_vision_available from ...test_modeling_flax_common import floats_tensor, ids_tensor, random_attention_mask from ..bert.test_modeling_flax_bert import FlaxBertModelTester from ..clip.test_modeling_flax_clip import FlaxCLIPVisionModelTester from ..vit.test_modeling_flax_vit import FlaxViTModelTester if is_flax_available(): from transformers import ( FlaxBertModel, FlaxCLIPVisionModel, FlaxVisionTextDualEncoderModel, FlaxViTModel, VisionTextDualEncoderConfig, VisionTextDualEncoderProcessor, ) from transformers.modeling_flax_pytorch_utils import ( convert_pytorch_state_dict_to_flax, load_flax_weights_in_pytorch_model, ) if is_torch_available(): import torch from transformers import VisionTextDualEncoderModel if is_vision_available(): from PIL import Image def SCREAMING_SNAKE_CASE ( _UpperCAmelCase ) -> Any: if isinstance(_UpperCAmelCase , collections.abc.Iterable ): return x return (x, x) @require_flax class lowerCAmelCase : def A_ ( self : Any , UpperCAmelCase : Dict , UpperCAmelCase : Optional[Any] ) -> Union[str, Any]: pass def A_ ( self : Dict ) -> int: pass def A_ ( self : List[Any] ) -> List[str]: pass def A_ ( self : Union[str, Any] , UpperCAmelCase : np.ndarray , UpperCAmelCase : np.ndarray , UpperCAmelCase : float ) -> Union[str, Any]: lowerCamelCase__ : Optional[Any] = np.abs((a - b) ).max() self.assertLessEqual(UpperCAmelCase , UpperCAmelCase , F"""Difference between torch and flax is {diff} (>= {tol}).""" ) def A_ ( self : Any , UpperCAmelCase : Dict , UpperCAmelCase : int , UpperCAmelCase : Optional[int] , UpperCAmelCase : List[Any] , UpperCAmelCase : Any=None , **UpperCAmelCase : Dict ) -> Tuple: lowerCamelCase__ : str = VisionTextDualEncoderConfig.from_vision_text_configs(UpperCAmelCase , UpperCAmelCase ) lowerCamelCase__ : str = FlaxVisionTextDualEncoderModel(UpperCAmelCase ) lowerCamelCase__ : int = model(input_ids=UpperCAmelCase , pixel_values=UpperCAmelCase , attention_mask=UpperCAmelCase ) self.assertEqual(output['text_embeds'].shape , (input_ids.shape[0], config.projection_dim) ) self.assertEqual(output['image_embeds'].shape , (pixel_values.shape[0], config.projection_dim) ) def A_ ( self : Tuple , UpperCAmelCase : Any , UpperCAmelCase : List[Any] , UpperCAmelCase : Union[str, Any] , UpperCAmelCase : str , UpperCAmelCase : Dict=None , **UpperCAmelCase : Optional[int] ) -> Dict: lowerCamelCase__ , lowerCamelCase__ : Optional[Any] = self.get_vision_text_model(UpperCAmelCase , UpperCAmelCase ) lowerCamelCase__ : Union[str, Any] = {'vision_model': vision_model, 'text_model': text_model} lowerCamelCase__ : str = FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(**UpperCAmelCase ) lowerCamelCase__ : Any = model(input_ids=UpperCAmelCase , pixel_values=UpperCAmelCase , attention_mask=UpperCAmelCase ) self.assertEqual(output['text_embeds'].shape , (input_ids.shape[0], model.config.projection_dim) ) self.assertEqual(output['image_embeds'].shape , (pixel_values.shape[0], model.config.projection_dim) ) def A_ ( self : Tuple , UpperCAmelCase : List[Any] , UpperCAmelCase : List[Any] , UpperCAmelCase : Optional[int] , UpperCAmelCase : int , UpperCAmelCase : Optional[int]=None , **UpperCAmelCase : List[str] ) -> Dict: lowerCamelCase__ , lowerCamelCase__ : str = self.get_vision_text_model(UpperCAmelCase , UpperCAmelCase ) lowerCamelCase__ : str = {'vision_model': vision_model, 'text_model': text_model} lowerCamelCase__ : Union[str, Any] = FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(**UpperCAmelCase ) lowerCamelCase__ : Any = model(input_ids=UpperCAmelCase , pixel_values=UpperCAmelCase , attention_mask=UpperCAmelCase ) lowerCamelCase__ : Optional[int] = output[0] with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(UpperCAmelCase ) lowerCamelCase__ : Union[str, Any] = FlaxVisionTextDualEncoderModel.from_pretrained(UpperCAmelCase ) lowerCamelCase__ : int = model(input_ids=UpperCAmelCase , pixel_values=UpperCAmelCase , attention_mask=UpperCAmelCase ) lowerCamelCase__ : List[Any] = after_output[0] lowerCamelCase__ : Optional[Any] = np.amax(np.abs(out_a - out_a ) ) self.assertLessEqual(UpperCAmelCase , 1e-3 ) def A_ ( self : Optional[int] , UpperCAmelCase : Optional[int] , UpperCAmelCase : Dict , UpperCAmelCase : str , UpperCAmelCase : str , UpperCAmelCase : str=None , **UpperCAmelCase : List[Any] ) -> str: lowerCamelCase__ , lowerCamelCase__ : Tuple = self.get_vision_text_model(UpperCAmelCase , UpperCAmelCase ) lowerCamelCase__ : Tuple = {'vision_model': vision_model, 'text_model': text_model} lowerCamelCase__ : Any = FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(**UpperCAmelCase ) lowerCamelCase__ : int = model( input_ids=UpperCAmelCase , pixel_values=UpperCAmelCase , attention_mask=UpperCAmelCase , output_attentions=UpperCAmelCase ) lowerCamelCase__ : Optional[Any] = output.vision_model_output.attentions self.assertEqual(len(UpperCAmelCase ) , vision_config.num_hidden_layers ) # in ViT, the seq_len equals the number of patches + 1 (we add 1 for the [CLS] token) lowerCamelCase__ : str = to_atuple(vision_model.config.image_size ) lowerCamelCase__ : Optional[int] = to_atuple(vision_model.config.patch_size ) lowerCamelCase__ : Tuple = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0]) lowerCamelCase__ : Optional[Any] = num_patches + 1 self.assertEqual(vision_attentions[0].shape[-3:] , (vision_config.num_attention_heads, seq_len, seq_len) ) lowerCamelCase__ : List[str] = output.text_model_output.attentions self.assertEqual(len(UpperCAmelCase ) , text_config.num_hidden_layers ) self.assertEqual( text_attentions[0].shape[-3:] , (text_config.num_attention_heads, input_ids.shape[-1], input_ids.shape[-1]) , ) def A_ ( self : Any , UpperCAmelCase : int , UpperCAmelCase : Optional[Any] , UpperCAmelCase : List[Any] ) -> str: pt_model.to(UpperCAmelCase ) pt_model.eval() # prepare inputs lowerCamelCase__ : Optional[Any] = inputs_dict lowerCamelCase__ : Optional[int] = {k: torch.tensor(v.tolist() ) for k, v in flax_inputs.items()} with torch.no_grad(): lowerCamelCase__ : Optional[int] = pt_model(**UpperCAmelCase ).to_tuple() lowerCamelCase__ : Tuple = fx_model(**UpperCAmelCase ).to_tuple() self.assertEqual(len(UpperCAmelCase ) , len(UpperCAmelCase ) , 'Output lengths differ between Flax and PyTorch' ) for fx_output, pt_output in zip(fx_outputs[:4] , pt_outputs[:4] ): self.assert_almost_equals(UpperCAmelCase , pt_output.numpy() , 4e-2 ) # PT -> Flax with tempfile.TemporaryDirectory() as tmpdirname: pt_model.save_pretrained(UpperCAmelCase ) lowerCamelCase__ : Optional[Any] = FlaxVisionTextDualEncoderModel.from_pretrained(UpperCAmelCase , from_pt=UpperCAmelCase ) lowerCamelCase__ : Optional[Any] = fx_model_loaded(**UpperCAmelCase ).to_tuple() self.assertEqual(len(UpperCAmelCase ) , len(UpperCAmelCase ) , 'Output lengths differ between Flax and PyTorch' ) for fx_output_loaded, pt_output in zip(fx_outputs_loaded[:4] , pt_outputs[:4] ): self.assert_almost_equals(UpperCAmelCase , pt_output.numpy() , 4e-2 ) # Flax -> PT with tempfile.TemporaryDirectory() as tmpdirname: fx_model.save_pretrained(UpperCAmelCase ) lowerCamelCase__ : Optional[int] = VisionTextDualEncoderModel.from_pretrained(UpperCAmelCase , from_flax=UpperCAmelCase ) pt_model_loaded.to(UpperCAmelCase ) pt_model_loaded.eval() with torch.no_grad(): lowerCamelCase__ : Optional[int] = pt_model_loaded(**UpperCAmelCase ).to_tuple() self.assertEqual(len(UpperCAmelCase ) , len(UpperCAmelCase ) , 'Output lengths differ between Flax and PyTorch' ) for fx_output, pt_output_loaded in zip(fx_outputs[:4] , pt_outputs_loaded[:4] ): self.assert_almost_equals(UpperCAmelCase , pt_output_loaded.numpy() , 4e-2 ) def A_ ( self : str , UpperCAmelCase : Tuple , UpperCAmelCase : int , UpperCAmelCase : Optional[int] ) -> Dict: lowerCamelCase__ : List[Any] = VisionTextDualEncoderConfig.from_vision_text_configs(UpperCAmelCase , UpperCAmelCase ) lowerCamelCase__ : Dict = VisionTextDualEncoderModel(UpperCAmelCase ) lowerCamelCase__ : Tuple = FlaxVisionTextDualEncoderModel(UpperCAmelCase ) lowerCamelCase__ : Optional[int] = convert_pytorch_state_dict_to_flax(pt_model.state_dict() , UpperCAmelCase ) lowerCamelCase__ : Dict = fx_state self.check_pt_flax_equivalence(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) def A_ ( self : Tuple , UpperCAmelCase : int , UpperCAmelCase : Optional[Any] , UpperCAmelCase : Optional[int] ) -> str: lowerCamelCase__ : str = VisionTextDualEncoderConfig.from_vision_text_configs(UpperCAmelCase , UpperCAmelCase ) lowerCamelCase__ : Any = VisionTextDualEncoderModel(UpperCAmelCase ) lowerCamelCase__ : Dict = FlaxVisionTextDualEncoderModel(UpperCAmelCase ) lowerCamelCase__ : Dict = load_flax_weights_in_pytorch_model(UpperCAmelCase , fx_model.params ) self.check_pt_flax_equivalence(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) def A_ ( self : List[Any] ) -> int: lowerCamelCase__ : Optional[int] = self.prepare_config_and_inputs() self.check_model_from_pretrained_configs(**UpperCAmelCase ) def A_ ( self : Any ) -> Any: lowerCamelCase__ : List[str] = self.prepare_config_and_inputs() self.check_vision_text_dual_encoder_from_pretrained(**UpperCAmelCase ) def A_ ( self : Any ) -> Optional[int]: lowerCamelCase__ : Tuple = self.prepare_config_and_inputs() self.check_save_load(**UpperCAmelCase ) def A_ ( self : Dict ) -> str: lowerCamelCase__ : int = self.prepare_config_and_inputs() self.check_vision_text_output_attention(**UpperCAmelCase ) @is_pt_flax_cross_test def A_ ( self : int ) -> List[str]: lowerCamelCase__ : str = self.prepare_config_and_inputs() lowerCamelCase__ : Dict = config_inputs_dict.pop('vision_config' ) lowerCamelCase__ : List[Any] = config_inputs_dict.pop('text_config' ) lowerCamelCase__ : Optional[Any] = config_inputs_dict self.check_equivalence_pt_to_flax(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) self.check_equivalence_flax_to_pt(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) @slow def A_ ( self : Any ) -> Dict: lowerCamelCase__ , lowerCamelCase__ : Tuple = self.get_pretrained_model_and_inputs() lowerCamelCase__ : int = model_a(**UpperCAmelCase ) lowerCamelCase__ : List[Any] = outputs[0] with tempfile.TemporaryDirectory() as tmp_dirname: model_a.save_pretrained(UpperCAmelCase ) lowerCamelCase__ : Tuple = FlaxVisionTextDualEncoderModel.from_pretrained(UpperCAmelCase ) lowerCamelCase__ : Optional[Any] = model_a(**UpperCAmelCase ) lowerCamelCase__ : int = after_outputs[0] lowerCamelCase__ : Optional[Any] = np.amax(np.abs(out_a - out_a ) ) self.assertLessEqual(UpperCAmelCase , 1e-5 ) @require_flax class lowerCAmelCase ( __UpperCamelCase, unittest.TestCase ): def A_ ( self : Dict ) -> Tuple: lowerCamelCase__ : Optional[int] = FlaxVisionTextDualEncoderModel.from_vision_text_pretrained( 'hf-internal-testing/tiny-random-vit' , 'hf-internal-testing/tiny-bert' , vision_from_pt=UpperCAmelCase , text_from_pt=UpperCAmelCase , ) lowerCamelCase__ : List[str] = 13 lowerCamelCase__ : Optional[int] = floats_tensor( [ batch_size, model.config.vision_config.num_channels, model.config.vision_config.image_size, model.config.vision_config.image_size, ] ) lowerCamelCase__ : Union[str, Any] = ids_tensor([batch_size, 4] , model.config.text_config.vocab_size ) lowerCamelCase__ : Union[str, Any] = random_attention_mask([batch_size, 4] ) lowerCamelCase__ : Tuple = {'pixel_values': pixel_values, 'input_ids': input_ids, 'attention_mask': attention_mask} return model, inputs def A_ ( self : Union[str, Any] , UpperCAmelCase : Union[str, Any] , UpperCAmelCase : str ) -> Optional[int]: lowerCamelCase__ : str = FlaxViTModel(UpperCAmelCase ) lowerCamelCase__ : int = FlaxBertModel(UpperCAmelCase ) return vision_model, text_model def A_ ( self : Any ) -> Optional[int]: lowerCamelCase__ : Any = FlaxViTModelTester(self ) lowerCamelCase__ : Tuple = FlaxBertModelTester(self ) lowerCamelCase__ : Union[str, Any] = vit_model_tester.prepare_config_and_inputs() lowerCamelCase__ : Tuple = bert_model_tester.prepare_config_and_inputs() lowerCamelCase__ , lowerCamelCase__ : Dict = vision_config_and_inputs lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ : Optional[Any] = text_config_and_inputs # make sure that cross attention layers are added return { "text_config": text_config, "vision_config": vision_config, "pixel_values": pixel_values, "attention_mask": attention_mask, "input_ids": input_ids, "token_type_ids": token_type_ids, } @require_torch class lowerCAmelCase ( __UpperCamelCase, unittest.TestCase ): def A_ ( self : Tuple ) -> Union[str, Any]: lowerCamelCase__ : List[str] = FlaxVisionTextDualEncoderModel.from_vision_text_pretrained( 'hf-internal-testing/tiny-random-clip' , 'hf-internal-testing/tiny-bert' , vision_from_pt=UpperCAmelCase , text_from_pt=UpperCAmelCase , ) lowerCamelCase__ : List[Any] = 13 lowerCamelCase__ : int = floats_tensor( [ batch_size, model.config.vision_config.num_channels, model.config.vision_config.image_size, model.config.vision_config.image_size, ] ) lowerCamelCase__ : Optional[Any] = ids_tensor([batch_size, 4] , model.config.text_config.vocab_size ) lowerCamelCase__ : Optional[int] = random_attention_mask([batch_size, 4] ) lowerCamelCase__ : Optional[Any] = {'pixel_values': pixel_values, 'input_ids': input_ids, 'attention_mask': attention_mask} return model, inputs def A_ ( self : Any , UpperCAmelCase : Tuple , UpperCAmelCase : Tuple ) -> Any: lowerCamelCase__ : Any = FlaxCLIPVisionModel(UpperCAmelCase ) lowerCamelCase__ : Any = FlaxBertModel(UpperCAmelCase ) return vision_model, text_model def A_ ( self : List[Any] ) -> List[Any]: lowerCamelCase__ : Any = FlaxCLIPVisionModelTester(self ) lowerCamelCase__ : List[str] = FlaxBertModelTester(self ) lowerCamelCase__ : Optional[Any] = clip_model_tester.prepare_config_and_inputs() lowerCamelCase__ : int = bert_model_tester.prepare_config_and_inputs() lowerCamelCase__ , lowerCamelCase__ : Tuple = vision_config_and_inputs lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ : int = text_config_and_inputs # make sure that cross attention layers are added return { "text_config": text_config, "vision_config": vision_config, "pixel_values": pixel_values, "attention_mask": attention_mask, "input_ids": input_ids, "token_type_ids": token_type_ids, } @require_flax @require_vision class lowerCAmelCase ( unittest.TestCase ): @slow def A_ ( self : int ) -> str: lowerCamelCase__ : Tuple = FlaxVisionTextDualEncoderModel.from_pretrained('clip-italian/clip-italian' , logit_scale_init_value=1.0 ) lowerCamelCase__ : int = VisionTextDualEncoderProcessor.from_pretrained('clip-italian/clip-italian' ) lowerCamelCase__ : List[str] = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) lowerCamelCase__ : Optional[Any] = processor( text=['una foto di un gatto', 'una foto di un cane'] , images=UpperCAmelCase , padding=UpperCAmelCase , return_tensors='np' ) lowerCamelCase__ : int = model(**UpperCAmelCase ) # verify the logits self.assertEqual(outputs.logits_per_image.shape , (inputs.pixel_values.shape[0], inputs.input_ids.shape[0]) ) self.assertEqual( outputs.logits_per_text.shape , (inputs.input_ids.shape[0], inputs.pixel_values.shape[0]) , ) lowerCamelCase__ : Optional[int] = np.array([[1.2_2_8_4_7_2_7, 0.3_1_0_4_1_2_2]] ) self.assertTrue(np.allclose(outputs.logits_per_image , UpperCAmelCase , atol=1e-3 ) )
50
import unittest from transformers import EsmConfig, is_torch_available from transformers.testing_utils import TestCasePlus, require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers.models.esm.modeling_esmfold import EsmForProteinFolding class lowercase_ : """simple docstring""" def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=13 , __SCREAMING_SNAKE_CASE=7 , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE=19 , __SCREAMING_SNAKE_CASE=32 , __SCREAMING_SNAKE_CASE=5 , __SCREAMING_SNAKE_CASE=4 , __SCREAMING_SNAKE_CASE=37 , __SCREAMING_SNAKE_CASE="gelu" , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=0.1 , __SCREAMING_SNAKE_CASE=512 , __SCREAMING_SNAKE_CASE=16 , __SCREAMING_SNAKE_CASE=2 , __SCREAMING_SNAKE_CASE=0.0_2 , __SCREAMING_SNAKE_CASE=3 , __SCREAMING_SNAKE_CASE=4 , __SCREAMING_SNAKE_CASE=None , ) ->Union[str, Any]: 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 SCREAMING_SNAKE_CASE_ ( self ) ->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 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, input_mask, sequence_labels, token_labels, choice_labels def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[int]: lowerCAmelCase = EsmConfig( vocab_size=33 , hidden_size=self.hidden_size , pad_token_id=1 , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , is_folding_model=__SCREAMING_SNAKE_CASE , esmfold_config={'''trunk''': {'''num_blocks''': 2}, '''fp16_esm''': False} , ) return config def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ->Tuple: lowerCAmelCase = EsmForProteinFolding(config=__SCREAMING_SNAKE_CASE ).float() model.to(__SCREAMING_SNAKE_CASE ) model.eval() lowerCAmelCase = model(__SCREAMING_SNAKE_CASE , attention_mask=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = model(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = model(__SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.positions.shape , (8, self.batch_size, self.seq_length, 14, 3) ) self.parent.assertEqual(result.angles.shape , (8, self.batch_size, self.seq_length, 7, 2) ) def SCREAMING_SNAKE_CASE_ ( self ) ->int: lowerCAmelCase = self.prepare_config_and_inputs() ( ( lowerCAmelCase ) , ( lowerCAmelCase ) , ( lowerCAmelCase ) , ( lowerCAmelCase ) , ( lowerCAmelCase ) , ( lowerCAmelCase ) , ) = config_and_inputs lowerCAmelCase = {'''input_ids''': input_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_torch class lowercase_ ( UpperCamelCase_ , UpperCamelCase_ , unittest.TestCase ): """simple docstring""" UpperCAmelCase_ : Optional[Any] = False UpperCAmelCase_ : Dict = (EsmForProteinFolding,) if is_torch_available() else () UpperCAmelCase_ : List[Any] = () UpperCAmelCase_ : Tuple = {} if is_torch_available() else {} UpperCAmelCase_ : List[str] = False def SCREAMING_SNAKE_CASE_ ( self ) ->Dict: lowerCAmelCase = EsmFoldModelTester(self ) lowerCAmelCase = ConfigTester(self , config_class=__SCREAMING_SNAKE_CASE , hidden_size=37 ) def SCREAMING_SNAKE_CASE_ ( self ) ->Any: self.config_tester.run_common_tests() def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[int]: lowerCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE ) @unittest.skip('''Does not support attention outputs''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->Tuple: pass @unittest.skip def SCREAMING_SNAKE_CASE_ ( self ) ->Any: pass @unittest.skip('''Esm does not support embedding resizing''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->List[str]: pass @unittest.skip('''Esm does not support embedding resizing''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->str: pass @unittest.skip('''ESMFold does not support passing input embeds!''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->List[Any]: pass @unittest.skip('''ESMFold does not support head pruning.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->str: pass @unittest.skip('''ESMFold does not support head pruning.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->Dict: pass @unittest.skip('''ESMFold does not support head pruning.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->List[str]: pass @unittest.skip('''ESMFold does not support head pruning.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->List[Any]: pass @unittest.skip('''ESMFold does not support head pruning.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[int]: pass @unittest.skip('''ESMFold does not output hidden states in the normal way.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->Tuple: pass @unittest.skip('''ESMfold does not output hidden states in the normal way.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->Dict: pass @unittest.skip('''ESMFold only has one output format.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->List[str]: pass @unittest.skip('''This test doesn\'t work for ESMFold and doesn\'t test core functionality''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->Dict: pass @unittest.skip('''ESMFold does not support input chunking.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->Union[str, Any]: pass @unittest.skip('''ESMFold doesn\'t respect you and it certainly doesn\'t respect your initialization arguments.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[Any]: pass @unittest.skip('''ESMFold doesn\'t support torchscript compilation.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->str: pass @unittest.skip('''ESMFold doesn\'t support torchscript compilation.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->Any: pass @unittest.skip('''ESMFold doesn\'t support torchscript compilation.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->str: pass @unittest.skip('''ESMFold doesn\'t support data parallel.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->str: pass @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.''' ) def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[int]: pass @require_torch class lowercase_ ( UpperCamelCase_ ): """simple docstring""" @slow def SCREAMING_SNAKE_CASE_ ( self ) ->str: lowerCAmelCase = EsmForProteinFolding.from_pretrained('''facebook/esmfold_v1''' ).float() model.eval() lowerCAmelCase = torch.tensor([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]] ) lowerCAmelCase = model(__SCREAMING_SNAKE_CASE )['''positions'''] lowerCAmelCase = torch.tensor([2.5_8_2_8, 0.7_9_9_3, -1_0.9_3_3_4] , dtype=torch.floataa ) self.assertTrue(torch.allclose(position_outputs[0, 0, 0, 0] , __SCREAMING_SNAKE_CASE , atol=1e-4 ) )
338
0
from __future__ import annotations import time from math import sqrt # 1 for manhattan, 0 for euclidean snake_case_ : str = 0 snake_case_ : Union[str, Any] = [ [0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles [0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0], [1, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0], ] snake_case_ : str = [[-1, 0], [0, -1], [1, 0], [0, 1]] # up, left, down, right snake_case_ : List[Any] = tuple[int, int] class __snake_case : def __init__( self : Any , _snake_case : int , _snake_case : int , _snake_case : int , _snake_case : int , _snake_case : int , _snake_case : Node | None , ): """simple docstring""" UpperCAmelCase_ = pos_x UpperCAmelCase_ = pos_y UpperCAmelCase_ = (pos_y, pos_x) UpperCAmelCase_ = goal_x UpperCAmelCase_ = goal_y UpperCAmelCase_ = g_cost UpperCAmelCase_ = parent UpperCAmelCase_ = self.calculate_heuristic() UpperCAmelCase_ = self.g_cost + self.h_cost def lowerCamelCase ( self : List[str]): """simple docstring""" UpperCAmelCase_ = self.pos_x - self.goal_x UpperCAmelCase_ = self.pos_y - self.goal_y if HEURISTIC == 1: return abs(_snake_case) + abs(_snake_case) else: return sqrt(dy**2 + dx**2) def __lt__( self : Union[str, Any] , _snake_case : Node): """simple docstring""" return self.f_cost < other.f_cost class __snake_case : def __init__( self : str , _snake_case : TPosition , _snake_case : TPosition): """simple docstring""" UpperCAmelCase_ = Node(start[1] , start[0] , goal[1] , goal[0] , 0 , _snake_case) UpperCAmelCase_ = Node(goal[1] , goal[0] , goal[1] , goal[0] , 99999 , _snake_case) UpperCAmelCase_ = [self.start] UpperCAmelCase_ = [] UpperCAmelCase_ = False def lowerCamelCase ( self : Optional[int]): """simple docstring""" while self.open_nodes: # Open Nodes are sorted using __lt__ self.open_nodes.sort() UpperCAmelCase_ = self.open_nodes.pop(0) if current_node.pos == self.target.pos: return self.retrace_path(_snake_case) self.closed_nodes.append(_snake_case) UpperCAmelCase_ = self.get_successors(_snake_case) for child_node in successors: if child_node in self.closed_nodes: continue if child_node not in self.open_nodes: self.open_nodes.append(_snake_case) else: # retrieve the best current path UpperCAmelCase_ = self.open_nodes.pop(self.open_nodes.index(_snake_case)) if child_node.g_cost < better_node.g_cost: self.open_nodes.append(_snake_case) else: self.open_nodes.append(_snake_case) return [self.start.pos] def lowerCamelCase ( self : Tuple , _snake_case : Node): """simple docstring""" UpperCAmelCase_ = [] for action in delta: UpperCAmelCase_ = parent.pos_x + action[1] UpperCAmelCase_ = parent.pos_y + action[0] if not (0 <= pos_x <= len(grid[0]) - 1 and 0 <= pos_y <= len(_snake_case) - 1): continue if grid[pos_y][pos_x] != 0: continue successors.append( Node( _snake_case , _snake_case , self.target.pos_y , self.target.pos_x , parent.g_cost + 1 , _snake_case , )) return successors def lowerCamelCase ( self : Any , _snake_case : Node | None): """simple docstring""" UpperCAmelCase_ = node UpperCAmelCase_ = [] while current_node is not None: path.append((current_node.pos_y, current_node.pos_x)) UpperCAmelCase_ = current_node.parent path.reverse() return path class __snake_case : def __init__( self : Any , _snake_case : TPosition , _snake_case : TPosition): """simple docstring""" UpperCAmelCase_ = AStar(_snake_case , _snake_case) UpperCAmelCase_ = AStar(_snake_case , _snake_case) UpperCAmelCase_ = False def lowerCamelCase ( self : List[Any]): """simple docstring""" while self.fwd_astar.open_nodes or self.bwd_astar.open_nodes: self.fwd_astar.open_nodes.sort() self.bwd_astar.open_nodes.sort() UpperCAmelCase_ = self.fwd_astar.open_nodes.pop(0) UpperCAmelCase_ = self.bwd_astar.open_nodes.pop(0) if current_bwd_node.pos == current_fwd_node.pos: return self.retrace_bidirectional_path( _snake_case , _snake_case) self.fwd_astar.closed_nodes.append(_snake_case) self.bwd_astar.closed_nodes.append(_snake_case) UpperCAmelCase_ = current_bwd_node UpperCAmelCase_ = current_fwd_node UpperCAmelCase_ = { self.fwd_astar: self.fwd_astar.get_successors(_snake_case), self.bwd_astar: self.bwd_astar.get_successors(_snake_case), } for astar in [self.fwd_astar, self.bwd_astar]: for child_node in successors[astar]: if child_node in astar.closed_nodes: continue if child_node not in astar.open_nodes: astar.open_nodes.append(_snake_case) else: # retrieve the best current path UpperCAmelCase_ = astar.open_nodes.pop( astar.open_nodes.index(_snake_case)) if child_node.g_cost < better_node.g_cost: astar.open_nodes.append(_snake_case) else: astar.open_nodes.append(_snake_case) return [self.fwd_astar.start.pos] def lowerCamelCase ( self : int , _snake_case : Node , _snake_case : Node): """simple docstring""" UpperCAmelCase_ = self.fwd_astar.retrace_path(_snake_case) UpperCAmelCase_ = self.bwd_astar.retrace_path(_snake_case) bwd_path.pop() bwd_path.reverse() UpperCAmelCase_ = fwd_path + bwd_path return path if __name__ == "__main__": # all coordinates are given in format [y,x] snake_case_ : Any = (0, 0) snake_case_ : Union[str, Any] = (len(grid) - 1, len(grid[0]) - 1) for elem in grid: print(elem) snake_case_ : str = time.time() snake_case_ : List[str] = AStar(init, goal) snake_case_ : Optional[int] = a_star.search() snake_case_ : Optional[Any] = time.time() - start_time print(f"AStar execution time = {end_time:f} seconds") snake_case_ : int = time.time() snake_case_ : Dict = BidirectionalAStar(init, goal) snake_case_ : str = time.time() - bd_start_time print(f"BidirectionalAStar execution time = {bd_end_time:f} seconds")
51
import warnings from typing import List import numpy as np from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding from ...utils import is_flax_available, is_tf_available, is_torch_available class lowercase_ ( UpperCamelCase_ ): """simple docstring""" UpperCAmelCase_ : List[str] = ["""image_processor""", """tokenizer"""] UpperCAmelCase_ : int = """OwlViTImageProcessor""" UpperCAmelCase_ : Any = ("""CLIPTokenizer""", """CLIPTokenizerFast""") def __init__( self , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE ) ->Any: lowerCAmelCase = None if "feature_extractor" in kwargs: warnings.warn( '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`''' ''' instead.''' , __SCREAMING_SNAKE_CASE , ) lowerCAmelCase = kwargs.pop('''feature_extractor''' ) lowerCAmelCase = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError('''You need to specify an `image_processor`.''' ) if tokenizer is None: raise ValueError('''You need to specify a `tokenizer`.''' ) super().__init__(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def __call__( self , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE="max_length" , __SCREAMING_SNAKE_CASE="np" , **__SCREAMING_SNAKE_CASE ) ->int: if text is None and query_images is None and images is None: raise ValueError( '''You have to specify at least one text or query image or image. All three cannot be none.''' ) if text is not None: if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) or (isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) and not isinstance(text[0] , __SCREAMING_SNAKE_CASE )): lowerCAmelCase = [self.tokenizer(__SCREAMING_SNAKE_CASE , padding=__SCREAMING_SNAKE_CASE , return_tensors=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE )] elif isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) and isinstance(text[0] , __SCREAMING_SNAKE_CASE ): lowerCAmelCase = [] # Maximum number of queries across batch lowerCAmelCase = max([len(__SCREAMING_SNAKE_CASE ) for t in text] ) # Pad all batch samples to max number of text queries for t in text: if len(__SCREAMING_SNAKE_CASE ) != max_num_queries: lowerCAmelCase = t + [''' '''] * (max_num_queries - len(__SCREAMING_SNAKE_CASE )) lowerCAmelCase = self.tokenizer(__SCREAMING_SNAKE_CASE , padding=__SCREAMING_SNAKE_CASE , return_tensors=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) encodings.append(__SCREAMING_SNAKE_CASE ) else: raise TypeError('''Input text should be a string, a list of strings or a nested list of strings''' ) if return_tensors == "np": lowerCAmelCase = np.concatenate([encoding['''input_ids'''] for encoding in encodings] , axis=0 ) lowerCAmelCase = np.concatenate([encoding['''attention_mask'''] for encoding in encodings] , axis=0 ) elif return_tensors == "jax" and is_flax_available(): import jax.numpy as jnp lowerCAmelCase = jnp.concatenate([encoding['''input_ids'''] for encoding in encodings] , axis=0 ) lowerCAmelCase = jnp.concatenate([encoding['''attention_mask'''] for encoding in encodings] , axis=0 ) elif return_tensors == "pt" and is_torch_available(): import torch lowerCAmelCase = torch.cat([encoding['''input_ids'''] for encoding in encodings] , dim=0 ) lowerCAmelCase = torch.cat([encoding['''attention_mask'''] for encoding in encodings] , dim=0 ) elif return_tensors == "tf" and is_tf_available(): import tensorflow as tf lowerCAmelCase = tf.stack([encoding['''input_ids'''] for encoding in encodings] , axis=0 ) lowerCAmelCase = tf.stack([encoding['''attention_mask'''] for encoding in encodings] , axis=0 ) else: raise ValueError('''Target return tensor type could not be returned''' ) lowerCAmelCase = BatchEncoding() lowerCAmelCase = input_ids lowerCAmelCase = attention_mask if query_images is not None: lowerCAmelCase = BatchEncoding() lowerCAmelCase = self.image_processor( __SCREAMING_SNAKE_CASE , return_tensors=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).pixel_values lowerCAmelCase = query_pixel_values if images is not None: lowerCAmelCase = self.image_processor(__SCREAMING_SNAKE_CASE , return_tensors=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) if text is not None and images is not None: lowerCAmelCase = image_features.pixel_values return encoding elif query_images is not None and images is not None: lowerCAmelCase = image_features.pixel_values return encoding elif text is not None or query_images is not None: return encoding else: return BatchEncoding(data=dict(**__SCREAMING_SNAKE_CASE ) , tensor_type=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) ->Optional[int]: return self.image_processor.post_process(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) ->Any: return self.image_processor.post_process_object_detection(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) ->Tuple: return self.image_processor.post_process_image_guided_detection(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) ->str: return self.tokenizer.batch_decode(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) ->List[Any]: return self.tokenizer.decode(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) @property def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[int]: warnings.warn( '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.''' , __SCREAMING_SNAKE_CASE , ) return self.image_processor_class @property def SCREAMING_SNAKE_CASE_ ( self ) ->int: warnings.warn( '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.''' , __SCREAMING_SNAKE_CASE , ) return self.image_processor
338
0
from typing import Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature from ...image_transforms import get_image_size, pad, rescale, to_channel_dimension_format from ...image_utils import ChannelDimension, ImageInput, make_list_of_images, to_numpy_array, valid_images from ...utils import TensorType, logging __lowerCamelCase : str = logging.get_logger(__name__) class A__ ( __snake_case ): _UpperCAmelCase :Optional[Any] = ['pixel_values'] def __init__( self , A_ = True , A_ = 1 / 255 , A_ = True , A_ = 8 , **A_ , ): '''simple docstring''' super().__init__(**A_ ) UpperCamelCase : List[Any] = do_rescale UpperCamelCase : Any = rescale_factor UpperCamelCase : int = do_pad UpperCamelCase : Optional[int] = pad_size def __UpperCamelCase( self , A_ , A_ , A_ = None , **A_ ): '''simple docstring''' return rescale(A_ , scale=A_ , data_format=A_ , **A_ ) def __UpperCamelCase( self , A_ , A_ , A_ = None ): '''simple docstring''' UpperCamelCase , UpperCamelCase : List[str] = get_image_size(A_ ) UpperCamelCase : int = (old_height // size + 1) * size - old_height UpperCamelCase : Dict = (old_width // size + 1) * size - old_width return pad(A_ , ((0, pad_height), (0, pad_width)) , mode="symmetric" , data_format=A_ ) def __UpperCamelCase( self , A_ , A_ = None , A_ = None , A_ = None , A_ = None , A_ = None , A_ = ChannelDimension.FIRST , **A_ , ): '''simple docstring''' UpperCamelCase : List[str] = do_rescale if do_rescale is not None else self.do_rescale UpperCamelCase : str = rescale_factor if rescale_factor is not None else self.rescale_factor UpperCamelCase : Union[str, Any] = do_pad if do_pad is not None else self.do_pad UpperCamelCase : List[Any] = pad_size if pad_size is not None else self.pad_size UpperCamelCase : Tuple = make_list_of_images(A_ ) if not valid_images(A_ ): raise ValueError( "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, " "torch.Tensor, tf.Tensor or jax.ndarray." ) if do_rescale and rescale_factor is None: raise ValueError("Rescale factor must be specified if do_rescale is True." ) # All transformations expect numpy arrays. UpperCamelCase : Any = [to_numpy_array(A_ ) for image in images] if do_rescale: UpperCamelCase : int = [self.rescale(image=A_ , scale=A_ ) for image in images] if do_pad: UpperCamelCase : List[str] = [self.pad(A_ , size=A_ ) for image in images] UpperCamelCase : Dict = [to_channel_dimension_format(A_ , A_ ) for image in images] UpperCamelCase : Dict = {"pixel_values": images} return BatchFeature(data=A_ , tensor_type=A_ )
52
import os import unicodedata from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging lowercase__ : List[Any] = logging.get_logger(__name__) lowercase__ : Optional[Any] = {'''vocab_file''': '''spiece.model'''} lowercase__ : Optional[int] = { '''vocab_file''': { '''albert-base-v1''': '''https://huggingface.co/albert-base-v1/resolve/main/spiece.model''', '''albert-large-v1''': '''https://huggingface.co/albert-large-v1/resolve/main/spiece.model''', '''albert-xlarge-v1''': '''https://huggingface.co/albert-xlarge-v1/resolve/main/spiece.model''', '''albert-xxlarge-v1''': '''https://huggingface.co/albert-xxlarge-v1/resolve/main/spiece.model''', '''albert-base-v2''': '''https://huggingface.co/albert-base-v2/resolve/main/spiece.model''', '''albert-large-v2''': '''https://huggingface.co/albert-large-v2/resolve/main/spiece.model''', '''albert-xlarge-v2''': '''https://huggingface.co/albert-xlarge-v2/resolve/main/spiece.model''', '''albert-xxlarge-v2''': '''https://huggingface.co/albert-xxlarge-v2/resolve/main/spiece.model''', } } lowercase__ : Any = { '''albert-base-v1''': 5_1_2, '''albert-large-v1''': 5_1_2, '''albert-xlarge-v1''': 5_1_2, '''albert-xxlarge-v1''': 5_1_2, '''albert-base-v2''': 5_1_2, '''albert-large-v2''': 5_1_2, '''albert-xlarge-v2''': 5_1_2, '''albert-xxlarge-v2''': 5_1_2, } lowercase__ : Tuple = '''▁''' class lowercase_ ( UpperCamelCase_ ): """simple docstring""" UpperCAmelCase_ : Dict = VOCAB_FILES_NAMES UpperCAmelCase_ : Tuple = PRETRAINED_VOCAB_FILES_MAP UpperCAmelCase_ : Optional[int] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES def __init__( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=True , __SCREAMING_SNAKE_CASE=False , __SCREAMING_SNAKE_CASE="[CLS]" , __SCREAMING_SNAKE_CASE="[SEP]" , __SCREAMING_SNAKE_CASE="<unk>" , __SCREAMING_SNAKE_CASE="[SEP]" , __SCREAMING_SNAKE_CASE="<pad>" , __SCREAMING_SNAKE_CASE="[CLS]" , __SCREAMING_SNAKE_CASE="[MASK]" , __SCREAMING_SNAKE_CASE = None , **__SCREAMING_SNAKE_CASE , ) ->None: # Mask token behave like a normal word, i.e. include the space before it and # is included in the raw text, there should be a match in a non-normalized sentence. lowerCAmelCase = ( AddedToken(__SCREAMING_SNAKE_CASE , lstrip=__SCREAMING_SNAKE_CASE , rstrip=__SCREAMING_SNAKE_CASE , normalized=__SCREAMING_SNAKE_CASE ) if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else mask_token ) lowerCAmelCase = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( do_lower_case=__SCREAMING_SNAKE_CASE , remove_space=__SCREAMING_SNAKE_CASE , keep_accents=__SCREAMING_SNAKE_CASE , bos_token=__SCREAMING_SNAKE_CASE , eos_token=__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 , sp_model_kwargs=self.sp_model_kwargs , **__SCREAMING_SNAKE_CASE , ) lowerCAmelCase = do_lower_case lowerCAmelCase = remove_space lowerCAmelCase = keep_accents lowerCAmelCase = vocab_file lowerCAmelCase = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(__SCREAMING_SNAKE_CASE ) @property def SCREAMING_SNAKE_CASE_ ( self ) ->Any: return len(self.sp_model ) def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[Any]: 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 , __SCREAMING_SNAKE_CASE ) ->Tuple: lowerCAmelCase = d # for backward compatibility if not hasattr(self , '''sp_model_kwargs''' ): lowerCAmelCase = {} lowerCAmelCase = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE ) ->Any: if self.remove_space: lowerCAmelCase = ''' '''.join(inputs.strip().split() ) else: lowerCAmelCase = inputs lowerCAmelCase = outputs.replace('''``''' , '''"''' ).replace('''\'\'''' , '''"''' ) if not self.keep_accents: lowerCAmelCase = unicodedata.normalize('''NFKD''' , __SCREAMING_SNAKE_CASE ) lowerCAmelCase = ''''''.join([c for c in outputs if not unicodedata.combining(__SCREAMING_SNAKE_CASE )] ) if self.do_lower_case: lowerCAmelCase = outputs.lower() return outputs def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE ) ->List[str]: lowerCAmelCase = self.preprocess_text(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = self.sp_model.encode(__SCREAMING_SNAKE_CASE , out_type=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = [] for piece in pieces: if len(__SCREAMING_SNAKE_CASE ) > 1 and piece[-1] == str(''',''' ) and piece[-2].isdigit(): lowerCAmelCase = self.sp_model.EncodeAsPieces(piece[:-1].replace(__SCREAMING_SNAKE_CASE , '''''' ) ) if piece[0] != SPIECE_UNDERLINE and cur_pieces[0][0] == SPIECE_UNDERLINE: if len(cur_pieces[0] ) == 1: lowerCAmelCase = cur_pieces[1:] else: lowerCAmelCase = cur_pieces[0][1:] cur_pieces.append(piece[-1] ) new_pieces.extend(__SCREAMING_SNAKE_CASE ) else: new_pieces.append(__SCREAMING_SNAKE_CASE ) return new_pieces def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE ) ->int: return self.sp_model.PieceToId(__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE ) ->int: return self.sp_model.IdToPiece(__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE ) ->Optional[int]: lowerCAmelCase = [] lowerCAmelCase = '''''' lowerCAmelCase = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: if not prev_is_special: out_string += " " out_string += self.sp_model.decode(__SCREAMING_SNAKE_CASE ) + token lowerCAmelCase = True lowerCAmelCase = [] else: current_sub_tokens.append(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = False out_string += self.sp_model.decode(__SCREAMING_SNAKE_CASE ) return out_string.strip() def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ) ->List[int]: lowerCAmelCase = [self.sep_token_id] lowerCAmelCase = [self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None , __SCREAMING_SNAKE_CASE = False ) ->List[int]: 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 not None: return [1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1] return [1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1] def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ) ->List[int]: 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 ) * [0] + len(token_ids_a + sep ) * [1] def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = None ) ->Tuple[str]: 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 ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , __SCREAMING_SNAKE_CASE ) elif not os.path.isfile(self.vocab_file ): with open(__SCREAMING_SNAKE_CASE , '''wb''' ) as fi: lowerCAmelCase = self.sp_model.serialized_model_proto() fi.write(__SCREAMING_SNAKE_CASE ) return (out_vocab_file,)
338
0
'''simple docstring''' # Copyright 2023 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import TYPE_CHECKING from ..models.auto import AutoModelForVisionaSeq from ..utils import requires_backends from .base import PipelineTool if TYPE_CHECKING: from PIL import Image class snake_case ( __lowerCamelCase ): """simple docstring""" SCREAMING_SNAKE_CASE_ : Optional[int] ="Salesforce/blip-image-captioning-base" SCREAMING_SNAKE_CASE_ : Union[str, Any] =( "This is a tool that generates a description of an image. It takes an input named `image` which should be the " "image to caption, and returns a text that contains the description in English." ) SCREAMING_SNAKE_CASE_ : Tuple ="image_captioner" SCREAMING_SNAKE_CASE_ : List[str] =AutoModelForVisionaSeq SCREAMING_SNAKE_CASE_ : Union[str, Any] =["image"] SCREAMING_SNAKE_CASE_ : Union[str, Any] =["text"] def __init__( self : Union[str, Any] , *__A : Union[str, Any] , **__A : Union[str, Any] ): requires_backends(self , ['vision'] ) super().__init__(*__A , **__A ) def _lowerCamelCase ( self : List[Any] , __A : "Image" ): return self.pre_processor(images=__A , return_tensors='pt' ) def _lowerCamelCase ( self : Tuple , __A : List[Any] ): return self.model.generate(**__A ) def _lowerCamelCase ( self : Optional[int] , __A : int ): return self.pre_processor.batch_decode(__A , skip_special_tokens=__A )[0].strip()
53
import tempfile import torch from diffusers import ( DEISMultistepScheduler, DPMSolverMultistepScheduler, DPMSolverSinglestepScheduler, UniPCMultistepScheduler, ) from .test_schedulers import SchedulerCommonTest class lowercase_ ( UpperCamelCase_ ): """simple docstring""" UpperCAmelCase_ : List[Any] = (DEISMultistepScheduler,) UpperCAmelCase_ : int = (("""num_inference_steps""", 25),) def SCREAMING_SNAKE_CASE_ ( self , **__SCREAMING_SNAKE_CASE ) ->str: lowerCAmelCase = { '''num_train_timesteps''': 1000, '''beta_start''': 0.0_0_0_1, '''beta_end''': 0.0_2, '''beta_schedule''': '''linear''', '''solver_order''': 2, } config.update(**__SCREAMING_SNAKE_CASE ) return config def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE=0 , **__SCREAMING_SNAKE_CASE ) ->Tuple: lowerCAmelCase = dict(self.forward_default_kwargs ) lowerCAmelCase = kwargs.pop('''num_inference_steps''' , __SCREAMING_SNAKE_CASE ) lowerCAmelCase = self.dummy_sample lowerCAmelCase = 0.1 * sample lowerCAmelCase = [residual + 0.2, residual + 0.1_5, residual + 0.1_0] for scheduler_class in self.scheduler_classes: lowerCAmelCase = self.get_scheduler_config(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) # copy over dummy past residuals lowerCAmelCase = dummy_past_residuals[: scheduler.config.solver_order] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = scheduler_class.from_pretrained(__SCREAMING_SNAKE_CASE ) new_scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) # copy over dummy past residuals lowerCAmelCase = dummy_past_residuals[: new_scheduler.config.solver_order] lowerCAmelCase , lowerCAmelCase = sample, sample for t in range(__SCREAMING_SNAKE_CASE , time_step + scheduler.config.solver_order + 1 ): lowerCAmelCase = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).prev_sample lowerCAmelCase = new_scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1e-5, "Scheduler outputs are not identical" def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[int]: pass def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE=0 , **__SCREAMING_SNAKE_CASE ) ->List[Any]: lowerCAmelCase = dict(self.forward_default_kwargs ) lowerCAmelCase = kwargs.pop('''num_inference_steps''' , __SCREAMING_SNAKE_CASE ) lowerCAmelCase = self.dummy_sample lowerCAmelCase = 0.1 * sample lowerCAmelCase = [residual + 0.2, residual + 0.1_5, residual + 0.1_0] for scheduler_class in self.scheduler_classes: lowerCAmelCase = self.get_scheduler_config() lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) # copy over dummy past residuals (must be after setting timesteps) lowerCAmelCase = dummy_past_residuals[: scheduler.config.solver_order] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = scheduler_class.from_pretrained(__SCREAMING_SNAKE_CASE ) # copy over dummy past residuals new_scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) # copy over dummy past residual (must be after setting timesteps) lowerCAmelCase = dummy_past_residuals[: new_scheduler.config.solver_order] lowerCAmelCase = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).prev_sample lowerCAmelCase = new_scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1e-5, "Scheduler outputs are not identical" def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE ) ->List[Any]: if scheduler is None: lowerCAmelCase = self.scheduler_classes[0] lowerCAmelCase = self.get_scheduler_config(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = self.scheduler_classes[0] lowerCAmelCase = self.get_scheduler_config(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = 10 lowerCAmelCase = self.dummy_model() lowerCAmelCase = self.dummy_sample_deter scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) for i, t in enumerate(scheduler.timesteps ): lowerCAmelCase = model(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) lowerCAmelCase = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ).prev_sample return sample def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[int]: lowerCAmelCase = dict(self.forward_default_kwargs ) lowerCAmelCase = kwargs.pop('''num_inference_steps''' , __SCREAMING_SNAKE_CASE ) for scheduler_class in self.scheduler_classes: lowerCAmelCase = self.get_scheduler_config() lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = self.dummy_sample lowerCAmelCase = 0.1 * sample if num_inference_steps is not None and hasattr(__SCREAMING_SNAKE_CASE , '''set_timesteps''' ): scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) elif num_inference_steps is not None and not hasattr(__SCREAMING_SNAKE_CASE , '''set_timesteps''' ): lowerCAmelCase = num_inference_steps # copy over dummy past residuals (must be done after set_timesteps) lowerCAmelCase = [residual + 0.2, residual + 0.1_5, residual + 0.1_0] lowerCAmelCase = dummy_past_residuals[: scheduler.config.solver_order] lowerCAmelCase = scheduler.timesteps[5] lowerCAmelCase = scheduler.timesteps[6] lowerCAmelCase = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).prev_sample lowerCAmelCase = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ).prev_sample self.assertEqual(output_a.shape , sample.shape ) self.assertEqual(output_a.shape , output_a.shape ) def SCREAMING_SNAKE_CASE_ ( self ) ->int: # make sure that iterating over schedulers with same config names gives same results # for defaults lowerCAmelCase = DEISMultistepScheduler(**self.get_scheduler_config() ) lowerCAmelCase = self.full_loop(scheduler=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = torch.mean(torch.abs(__SCREAMING_SNAKE_CASE ) ) assert abs(result_mean.item() - 0.2_3_9_1_6 ) < 1e-3 lowerCAmelCase = DPMSolverSinglestepScheduler.from_config(scheduler.config ) lowerCAmelCase = DPMSolverMultistepScheduler.from_config(scheduler.config ) lowerCAmelCase = UniPCMultistepScheduler.from_config(scheduler.config ) lowerCAmelCase = DEISMultistepScheduler.from_config(scheduler.config ) lowerCAmelCase = self.full_loop(scheduler=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = torch.mean(torch.abs(__SCREAMING_SNAKE_CASE ) ) assert abs(result_mean.item() - 0.2_3_9_1_6 ) < 1e-3 def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[int]: for timesteps in [25, 50, 100, 999, 1000]: self.check_over_configs(num_train_timesteps=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->int: self.check_over_configs(thresholding=__SCREAMING_SNAKE_CASE ) for order in [1, 2, 3]: for solver_type in ["logrho"]: for threshold in [0.5, 1.0, 2.0]: for prediction_type in ["epsilon", "sample"]: self.check_over_configs( thresholding=__SCREAMING_SNAKE_CASE , prediction_type=__SCREAMING_SNAKE_CASE , sample_max_value=__SCREAMING_SNAKE_CASE , algorithm_type='''deis''' , solver_order=__SCREAMING_SNAKE_CASE , solver_type=__SCREAMING_SNAKE_CASE , ) def SCREAMING_SNAKE_CASE_ ( self ) ->Optional[Any]: for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->Union[str, Any]: for algorithm_type in ["deis"]: for solver_type in ["logrho"]: for order in [1, 2, 3]: for prediction_type in ["epsilon", "sample"]: self.check_over_configs( solver_order=__SCREAMING_SNAKE_CASE , solver_type=__SCREAMING_SNAKE_CASE , prediction_type=__SCREAMING_SNAKE_CASE , algorithm_type=__SCREAMING_SNAKE_CASE , ) lowerCAmelCase = self.full_loop( solver_order=__SCREAMING_SNAKE_CASE , solver_type=__SCREAMING_SNAKE_CASE , prediction_type=__SCREAMING_SNAKE_CASE , algorithm_type=__SCREAMING_SNAKE_CASE , ) assert not torch.isnan(__SCREAMING_SNAKE_CASE ).any(), "Samples have nan numbers" def SCREAMING_SNAKE_CASE_ ( self ) ->List[Any]: self.check_over_configs(lower_order_final=__SCREAMING_SNAKE_CASE ) self.check_over_configs(lower_order_final=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self ) ->List[Any]: for num_inference_steps in [1, 2, 3, 5, 10, 50, 100, 999, 1000]: self.check_over_forward(num_inference_steps=__SCREAMING_SNAKE_CASE , time_step=0 ) def SCREAMING_SNAKE_CASE_ ( self ) ->Dict: lowerCAmelCase = self.full_loop() lowerCAmelCase = torch.mean(torch.abs(__SCREAMING_SNAKE_CASE ) ) assert abs(result_mean.item() - 0.2_3_9_1_6 ) < 1e-3 def SCREAMING_SNAKE_CASE_ ( self ) ->List[str]: lowerCAmelCase = self.full_loop(prediction_type='''v_prediction''' ) lowerCAmelCase = torch.mean(torch.abs(__SCREAMING_SNAKE_CASE ) ) assert abs(result_mean.item() - 0.0_9_1 ) < 1e-3 def SCREAMING_SNAKE_CASE_ ( self ) ->Dict: lowerCAmelCase = self.scheduler_classes[0] lowerCAmelCase = self.get_scheduler_config(thresholding=__SCREAMING_SNAKE_CASE , dynamic_thresholding_ratio=0 ) lowerCAmelCase = scheduler_class(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = 10 lowerCAmelCase = self.dummy_model() lowerCAmelCase = self.dummy_sample_deter.half() scheduler.set_timesteps(__SCREAMING_SNAKE_CASE ) for i, t in enumerate(scheduler.timesteps ): lowerCAmelCase = model(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) lowerCAmelCase = scheduler.step(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ).prev_sample assert sample.dtype == torch.floataa
338
0
"""simple docstring""" import inspect import unittest from transformers import DPTConfig from transformers.file_utils import is_torch_available, is_vision_available from transformers.models.auto import get_values from transformers.testing_utils import require_torch, require_vision, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import MODEL_MAPPING, DPTForDepthEstimation, DPTForSemanticSegmentation, DPTModel from transformers.models.dpt.modeling_dpt import DPT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import DPTImageProcessor class UpperCamelCase_ : """simple docstring""" def __init__( self : Union[str, Any] , UpperCAmelCase__ : Optional[Any] , UpperCAmelCase__ : Union[str, Any]=2 , UpperCAmelCase__ : Optional[Any]=3_2 , UpperCAmelCase__ : Dict=1_6 , UpperCAmelCase__ : Tuple=3 , UpperCAmelCase__ : str=True , UpperCAmelCase__ : Union[str, Any]=True , UpperCAmelCase__ : Any=3_2 , UpperCAmelCase__ : Tuple=4 , UpperCAmelCase__ : List[str]=[0, 1, 2, 3] , UpperCAmelCase__ : Optional[Any]=4 , UpperCAmelCase__ : str=3_7 , UpperCAmelCase__ : Any="gelu" , UpperCAmelCase__ : str=0.1 , UpperCAmelCase__ : Dict=0.1 , UpperCAmelCase__ : List[Any]=0.02 , UpperCAmelCase__ : int=3 , UpperCAmelCase__ : List[Any]=[1, 3_8_4, 2_4, 2_4] , UpperCAmelCase__ : Dict=True , UpperCAmelCase__ : Optional[int]=None , ) -> List[Any]: __SCREAMING_SNAKE_CASE = parent __SCREAMING_SNAKE_CASE = batch_size __SCREAMING_SNAKE_CASE = image_size __SCREAMING_SNAKE_CASE = patch_size __SCREAMING_SNAKE_CASE = num_channels __SCREAMING_SNAKE_CASE = is_training __SCREAMING_SNAKE_CASE = use_labels __SCREAMING_SNAKE_CASE = hidden_size __SCREAMING_SNAKE_CASE = num_hidden_layers __SCREAMING_SNAKE_CASE = backbone_out_indices __SCREAMING_SNAKE_CASE = num_attention_heads __SCREAMING_SNAKE_CASE = intermediate_size __SCREAMING_SNAKE_CASE = hidden_act __SCREAMING_SNAKE_CASE = hidden_dropout_prob __SCREAMING_SNAKE_CASE = attention_probs_dropout_prob __SCREAMING_SNAKE_CASE = initializer_range __SCREAMING_SNAKE_CASE = num_labels __SCREAMING_SNAKE_CASE = backbone_featmap_shape __SCREAMING_SNAKE_CASE = scope __SCREAMING_SNAKE_CASE = is_hybrid # sequence length of DPT = num_patches + 1 (we add 1 for the [CLS] token) __SCREAMING_SNAKE_CASE = (image_size // patch_size) ** 2 __SCREAMING_SNAKE_CASE = num_patches + 1 def UpperCAmelCase_ ( self : Optional[Any] ) -> Dict: __SCREAMING_SNAKE_CASE = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) __SCREAMING_SNAKE_CASE = None if self.use_labels: __SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.image_size, self.image_size] , self.num_labels ) __SCREAMING_SNAKE_CASE = self.get_config() return config, pixel_values, labels def UpperCAmelCase_ ( self : str ) -> Any: __SCREAMING_SNAKE_CASE = { "global_padding": "same", "layer_type": "bottleneck", "depths": [3, 4, 9], "out_features": ["stage1", "stage2", "stage3"], "embedding_dynamic_padding": True, "hidden_sizes": [9_6, 1_9_2, 3_8_4, 7_6_8], "num_groups": 2, } return DPTConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , backbone_out_indices=self.backbone_out_indices , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=UpperCAmelCase__ , initializer_range=self.initializer_range , is_hybrid=self.is_hybrid , backbone_config=UpperCAmelCase__ , backbone_featmap_shape=self.backbone_featmap_shape , ) def UpperCAmelCase_ ( self : List[str] , UpperCAmelCase__ : Optional[int] , UpperCAmelCase__ : int , UpperCAmelCase__ : Tuple ) -> Dict: __SCREAMING_SNAKE_CASE = DPTModel(config=UpperCAmelCase__ ) model.to(UpperCAmelCase__ ) model.eval() __SCREAMING_SNAKE_CASE = model(UpperCAmelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def UpperCAmelCase_ ( self : Any , UpperCAmelCase__ : str , UpperCAmelCase__ : str , UpperCAmelCase__ : Dict ) -> Union[str, Any]: __SCREAMING_SNAKE_CASE = self.num_labels __SCREAMING_SNAKE_CASE = DPTForDepthEstimation(UpperCAmelCase__ ) model.to(UpperCAmelCase__ ) model.eval() __SCREAMING_SNAKE_CASE = model(UpperCAmelCase__ ) self.parent.assertEqual(result.predicted_depth.shape , (self.batch_size, self.image_size, self.image_size) ) def UpperCAmelCase_ ( self : int , UpperCAmelCase__ : Any , UpperCAmelCase__ : Optional[int] , UpperCAmelCase__ : Union[str, Any] ) -> List[Any]: __SCREAMING_SNAKE_CASE = self.num_labels __SCREAMING_SNAKE_CASE = DPTForSemanticSegmentation(UpperCAmelCase__ ) model.to(UpperCAmelCase__ ) model.eval() __SCREAMING_SNAKE_CASE = model(UpperCAmelCase__ , labels=UpperCAmelCase__ ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size, self.image_size) ) def UpperCAmelCase_ ( self : Tuple ) -> Tuple: __SCREAMING_SNAKE_CASE = self.prepare_config_and_inputs() __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = config_and_inputs __SCREAMING_SNAKE_CASE = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class UpperCamelCase_ ( UpperCamelCase , UpperCamelCase , unittest.TestCase): """simple docstring""" snake_case__ : Union[str, Any] = (DPTModel, DPTForDepthEstimation, DPTForSemanticSegmentation) if is_torch_available() else () snake_case__ : Any = ( { "depth-estimation": DPTForDepthEstimation, "feature-extraction": DPTModel, "image-segmentation": DPTForSemanticSegmentation, } if is_torch_available() else {} ) snake_case__ : Union[str, Any] = False snake_case__ : List[Any] = False snake_case__ : Optional[int] = False def UpperCAmelCase_ ( self : Union[str, Any] ) -> Dict: __SCREAMING_SNAKE_CASE = DPTModelTester(self ) __SCREAMING_SNAKE_CASE = ConfigTester(self , config_class=UpperCAmelCase__ , has_text_modality=UpperCAmelCase__ , hidden_size=3_7 ) def UpperCAmelCase_ ( self : Optional[int] ) -> List[Any]: self.config_tester.run_common_tests() @unittest.skip(reason="DPT does not use inputs_embeds" ) def UpperCAmelCase_ ( self : Dict ) -> Dict: pass def UpperCAmelCase_ ( self : Any ) -> Dict: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: __SCREAMING_SNAKE_CASE = model_class(UpperCAmelCase__ ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) __SCREAMING_SNAKE_CASE = model.get_output_embeddings() self.assertTrue(x is None or isinstance(UpperCAmelCase__ , nn.Linear ) ) def UpperCAmelCase_ ( self : Optional[Any] ) -> Union[str, Any]: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: __SCREAMING_SNAKE_CASE = model_class(UpperCAmelCase__ ) __SCREAMING_SNAKE_CASE = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic __SCREAMING_SNAKE_CASE = [*signature.parameters.keys()] __SCREAMING_SNAKE_CASE = ["pixel_values"] self.assertListEqual(arg_names[:1] , UpperCAmelCase__ ) def UpperCAmelCase_ ( self : Union[str, Any] ) -> Union[str, Any]: __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*UpperCAmelCase__ ) def UpperCAmelCase_ ( self : Tuple ) -> List[str]: __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_depth_estimation(*UpperCAmelCase__ ) def UpperCAmelCase_ ( self : int ) -> List[Any]: __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_semantic_segmentation(*UpperCAmelCase__ ) def UpperCAmelCase_ ( self : Optional[Any] ) -> Any: for model_class in self.all_model_classes: if model_class.__name__ == "DPTForDepthEstimation": continue __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() __SCREAMING_SNAKE_CASE = True if model_class in get_values(UpperCAmelCase__ ): continue __SCREAMING_SNAKE_CASE = model_class(UpperCAmelCase__ ) model.to(UpperCAmelCase__ ) model.train() __SCREAMING_SNAKE_CASE = self._prepare_for_class(UpperCAmelCase__ , UpperCAmelCase__ , return_labels=UpperCAmelCase__ ) __SCREAMING_SNAKE_CASE = model(**UpperCAmelCase__ ).loss loss.backward() def UpperCAmelCase_ ( self : Dict ) -> str: for model_class in self.all_model_classes: if model_class.__name__ == "DPTForDepthEstimation": continue __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() __SCREAMING_SNAKE_CASE = False __SCREAMING_SNAKE_CASE = True if model_class in get_values(UpperCAmelCase__ ) or not model_class.supports_gradient_checkpointing: continue __SCREAMING_SNAKE_CASE = model_class(UpperCAmelCase__ ) model.to(UpperCAmelCase__ ) model.gradient_checkpointing_enable() model.train() __SCREAMING_SNAKE_CASE = self._prepare_for_class(UpperCAmelCase__ , UpperCAmelCase__ , return_labels=UpperCAmelCase__ ) __SCREAMING_SNAKE_CASE = model(**UpperCAmelCase__ ).loss loss.backward() def UpperCAmelCase_ ( self : List[Any] ) -> Dict: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() __SCREAMING_SNAKE_CASE = _config_zero_init(UpperCAmelCase__ ) for model_class in self.all_model_classes: __SCREAMING_SNAKE_CASE = model_class(config=UpperCAmelCase__ ) # Skip the check for the backbone __SCREAMING_SNAKE_CASE = [] for name, module in model.named_modules(): if module.__class__.__name__ == "DPTViTHybridEmbeddings": __SCREAMING_SNAKE_CASE = [F"""{name}.{key}""" for key in module.state_dict().keys()] break for name, param in model.named_parameters(): if param.requires_grad: if name in backbone_params: continue self.assertIn( ((param.data.mean() * 1E9).round() / 1E9).item() , [0.0, 1.0] , msg=F"""Parameter {name} of model {model_class} seems not properly initialized""" , ) @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests." ) def UpperCAmelCase_ ( self : Tuple ) -> Dict: pass @slow def UpperCAmelCase_ ( self : Tuple ) -> str: for model_name in DPT_PRETRAINED_MODEL_ARCHIVE_LIST[1:]: __SCREAMING_SNAKE_CASE = DPTModel.from_pretrained(UpperCAmelCase__ ) self.assertIsNotNone(UpperCAmelCase__ ) def UpperCAmelCase_ ( self : List[Any] ) -> Dict: # We do this test only for DPTForDepthEstimation since it is the only model that uses readout_type __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() __SCREAMING_SNAKE_CASE = "add" with self.assertRaises(UpperCAmelCase__ ): __SCREAMING_SNAKE_CASE = DPTForDepthEstimation(UpperCAmelCase__ ) def UpperCAmelCase__ (): '''simple docstring''' __SCREAMING_SNAKE_CASE = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_torch @require_vision @slow class UpperCamelCase_ ( unittest.TestCase): """simple docstring""" def UpperCAmelCase_ ( self : Tuple ) -> str: __SCREAMING_SNAKE_CASE = DPTImageProcessor.from_pretrained("Intel/dpt-hybrid-midas" ) __SCREAMING_SNAKE_CASE = DPTForDepthEstimation.from_pretrained("Intel/dpt-hybrid-midas" ).to(UpperCAmelCase__ ) __SCREAMING_SNAKE_CASE = prepare_img() __SCREAMING_SNAKE_CASE = image_processor(images=UpperCAmelCase__ , return_tensors="pt" ).to(UpperCAmelCase__ ) # forward pass with torch.no_grad(): __SCREAMING_SNAKE_CASE = model(**UpperCAmelCase__ ) __SCREAMING_SNAKE_CASE = outputs.predicted_depth # verify the predicted depth __SCREAMING_SNAKE_CASE = torch.Size((1, 3_8_4, 3_8_4) ) self.assertEqual(predicted_depth.shape , UpperCAmelCase__ ) __SCREAMING_SNAKE_CASE = torch.tensor( [[[5.6_437, 5.6_146, 5.6_511], [5.4_371, 5.5_649, 5.5_958], [5.5_215, 5.5_184, 5.5_293]]] ).to(UpperCAmelCase__ ) self.assertTrue(torch.allclose(outputs.predicted_depth[:3, :3, :3] / 1_0_0 , UpperCAmelCase__ , atol=1E-4 ) )
54
import unittest import numpy as np import torch from diffusers import KarrasVePipeline, KarrasVeScheduler, UNetaDModel from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device enable_full_determinism() class lowercase_ ( unittest.TestCase ): """simple docstring""" @property def SCREAMING_SNAKE_CASE_ ( self ) ->List[str]: torch.manual_seed(0 ) lowerCAmelCase = UNetaDModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=3 , out_channels=3 , down_block_types=('''DownBlock2D''', '''AttnDownBlock2D''') , up_block_types=('''AttnUpBlock2D''', '''UpBlock2D''') , ) return model def SCREAMING_SNAKE_CASE_ ( self ) ->int: lowerCAmelCase = self.dummy_uncond_unet lowerCAmelCase = KarrasVeScheduler() lowerCAmelCase = KarrasVePipeline(unet=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = torch.manual_seed(0 ) lowerCAmelCase = pipe(num_inference_steps=2 , generator=__SCREAMING_SNAKE_CASE , output_type='''numpy''' ).images lowerCAmelCase = torch.manual_seed(0 ) lowerCAmelCase = pipe(num_inference_steps=2 , generator=__SCREAMING_SNAKE_CASE , output_type='''numpy''' , return_dict=__SCREAMING_SNAKE_CASE )[0] lowerCAmelCase = image[0, -3:, -3:, -1] lowerCAmelCase = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) lowerCAmelCase = np.array([0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 @slow @require_torch class lowercase_ ( unittest.TestCase ): """simple docstring""" def SCREAMING_SNAKE_CASE_ ( self ) ->List[Any]: lowerCAmelCase = '''google/ncsnpp-celebahq-256''' lowerCAmelCase = UNetaDModel.from_pretrained(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = KarrasVeScheduler() lowerCAmelCase = KarrasVePipeline(unet=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = torch.manual_seed(0 ) lowerCAmelCase = pipe(num_inference_steps=20 , generator=__SCREAMING_SNAKE_CASE , output_type='''numpy''' ).images lowerCAmelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 256, 256, 3) lowerCAmelCase = np.array([0.5_7_8, 0.5_8_1_1, 0.5_9_2_4, 0.5_8_0_9, 0.5_8_7, 0.5_8_8_6, 0.5_8_6_1, 0.5_8_0_2, 0.5_8_6] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
338
0
'''simple docstring''' import unittest import numpy as np from transformers import is_flax_available from transformers.testing_utils import require_flax from ..test_modeling_flax_common import ids_tensor if is_flax_available(): import jax import jax.numpy as jnp from transformers.generation import ( FlaxForcedBOSTokenLogitsProcessor, FlaxForcedEOSTokenLogitsProcessor, FlaxLogitsProcessorList, FlaxMinLengthLogitsProcessor, FlaxTemperatureLogitsWarper, FlaxTopKLogitsWarper, FlaxTopPLogitsWarper, ) @require_flax class snake_case ( unittest.TestCase ): """simple docstring""" def snake_case ( self , UpperCamelCase , UpperCamelCase ): """simple docstring""" lowerCamelCase_ = jnp.ones((batch_size, length) ) / length return scores def snake_case ( self ): """simple docstring""" lowerCamelCase_ = None lowerCamelCase_ = 20 lowerCamelCase_ = self._get_uniform_logits(batch_size=2 , length=UpperCamelCase ) # tweak scores to not be uniform anymore lowerCamelCase_ = scores.at[1, 5].set((1 / length) + 0.1 ) # peak, 1st batch lowerCamelCase_ = scores.at[1, 10].set((1 / length) - 0.4 ) # valley, 1st batch # compute softmax lowerCamelCase_ = jax.nn.softmax(UpperCamelCase , axis=-1 ) lowerCamelCase_ = FlaxTemperatureLogitsWarper(temperature=0.5 ) lowerCamelCase_ = FlaxTemperatureLogitsWarper(temperature=1.3 ) lowerCamelCase_ = jax.nn.softmax(temp_dist_warper_sharper(UpperCamelCase , scores.copy() , cur_len=UpperCamelCase ) , axis=-1 ) lowerCamelCase_ = jax.nn.softmax(temp_dist_warper_smoother(UpperCamelCase , scores.copy() , cur_len=UpperCamelCase ) , axis=-1 ) # uniform distribution stays uniform self.assertTrue(jnp.allclose(probs[0, :] , warped_prob_sharp[0, :] , atol=1e-3 ) ) self.assertTrue(jnp.allclose(probs[0, :] , warped_prob_smooth[0, :] , atol=1e-3 ) ) # sharp peaks get higher, valleys get lower self.assertLess(probs[1, :].max() , warped_prob_sharp[1, :].max() ) self.assertGreater(probs[1, :].min() , warped_prob_sharp[1, :].min() ) # smooth peaks get lower, valleys get higher self.assertGreater(probs[1, :].max() , warped_prob_smooth[1, :].max() ) self.assertLess(probs[1, :].min() , warped_prob_smooth[1, :].min() ) def snake_case ( self ): """simple docstring""" lowerCamelCase_ = None lowerCamelCase_ = 10 lowerCamelCase_ = 2 # create ramp distribution lowerCamelCase_ = np.broadcast_to(np.arange(UpperCamelCase )[None, :] , (batch_size, vocab_size) ).copy() lowerCamelCase_ = ramp_logits[1:, : vocab_size // 2] + vocab_size lowerCamelCase_ = FlaxTopKLogitsWarper(3 ) lowerCamelCase_ = top_k_warp(UpperCamelCase , UpperCamelCase , cur_len=UpperCamelCase ) # check that correct tokens are filtered self.assertListEqual(jnp.isinf(scores[0] ).tolist() , 7 * [True] + 3 * [False] ) self.assertListEqual(jnp.isinf(scores[1] ).tolist() , 2 * [True] + 3 * [False] + 5 * [True] ) # check special case lowerCamelCase_ = 5 lowerCamelCase_ = FlaxTopKLogitsWarper(top_k=1 , filter_value=0.0 , min_tokens_to_keep=3 ) lowerCamelCase_ = np.broadcast_to(np.arange(UpperCamelCase )[None, :] , (batch_size, length) ).copy() lowerCamelCase_ = top_k_warp_safety_check(UpperCamelCase , UpperCamelCase , cur_len=UpperCamelCase ) # min_tokens overwrites k: 3 tokens are kept => 2 tokens are nullified self.assertListEqual((scores == 0.0).sum(axis=-1 ).tolist() , [2, 2] ) def snake_case ( self ): """simple docstring""" lowerCamelCase_ = None lowerCamelCase_ = 10 lowerCamelCase_ = 2 # create distribution and take log (inverse to Softmax as taken in TopPLogitsWarper) lowerCamelCase_ = np.log(np.array([[0.3, 0.1, 0.1, 0.5], [0.15, 0.3, 0.3, 0.25]] ) ) lowerCamelCase_ = FlaxTopPLogitsWarper(0.8 ) lowerCamelCase_ = np.exp(top_p_warp(UpperCamelCase , UpperCamelCase , cur_len=UpperCamelCase ) ) # dist should be filtered to keep min num values so that sum is >= top_p # exp (-inf) => 0 lowerCamelCase_ = np.array([[0.3, 0.0, 0.0, 0.5], [0.0, 0.3, 0.3, 0.25]] ) self.assertTrue(np.allclose(UpperCamelCase , UpperCamelCase , atol=1e-3 ) ) # check edge cases with negative and extreme logits lowerCamelCase_ = np.broadcast_to(np.arange(UpperCamelCase )[None, :] , (batch_size, vocab_size) ).copy() - ( vocab_size // 2 ) # make ramp_logits more extreme lowerCamelCase_ = ramp_logits[1] * 100.0 # make sure at least 2 tokens are kept lowerCamelCase_ = FlaxTopPLogitsWarper(0.9 , min_tokens_to_keep=2 , filter_value=0.0 ) lowerCamelCase_ = top_p_warp(UpperCamelCase , UpperCamelCase , cur_len=UpperCamelCase ) # first batch should keep three tokens, second batch would keep only 1, but due to `min_tokens_to_keep=2` keeps 2. self.assertListEqual((filtered_dist != 0.0).sum(axis=-1 ).tolist() , [3, 2] ) def snake_case ( self ): """simple docstring""" lowerCamelCase_ = 20 lowerCamelCase_ = 4 lowerCamelCase_ = 0 lowerCamelCase_ = FlaxMinLengthLogitsProcessor(min_length=10 , eos_token_id=UpperCamelCase ) # check that min length is applied at length 5 lowerCamelCase_ = ids_tensor((batch_size, 20) , vocab_size=20 ) lowerCamelCase_ = 5 lowerCamelCase_ = self._get_uniform_logits(UpperCamelCase , UpperCamelCase ) lowerCamelCase_ = min_dist_processor(UpperCamelCase , UpperCamelCase , cur_len=UpperCamelCase ) self.assertListEqual(scores_before_min_length[:, eos_token_id].tolist() , 4 * [-float("inf" )] ) # check that min length is not applied anymore at length 15 lowerCamelCase_ = self._get_uniform_logits(UpperCamelCase , UpperCamelCase ) lowerCamelCase_ = 15 lowerCamelCase_ = min_dist_processor(UpperCamelCase , UpperCamelCase , cur_len=UpperCamelCase ) self.assertFalse(jnp.isinf(UpperCamelCase ).any() ) def snake_case ( self ): """simple docstring""" lowerCamelCase_ = 20 lowerCamelCase_ = 4 lowerCamelCase_ = 0 lowerCamelCase_ = FlaxForcedBOSTokenLogitsProcessor(bos_token_id=UpperCamelCase ) # check that all scores are -inf except the bos_token_id score lowerCamelCase_ = ids_tensor((batch_size, 1) , vocab_size=20 ) lowerCamelCase_ = 1 lowerCamelCase_ = self._get_uniform_logits(UpperCamelCase , UpperCamelCase ) lowerCamelCase_ = logits_processor(UpperCamelCase , UpperCamelCase , cur_len=UpperCamelCase ) self.assertTrue(jnp.isneginf(scores[:, bos_token_id + 1 :] ).all() ) self.assertListEqual(scores[:, bos_token_id].tolist() , 4 * [0] ) # score for bos_token_id shold be zero # check that bos_token_id is not forced if current length is greater than 1 lowerCamelCase_ = 3 lowerCamelCase_ = self._get_uniform_logits(UpperCamelCase , UpperCamelCase ) lowerCamelCase_ = logits_processor(UpperCamelCase , UpperCamelCase , cur_len=UpperCamelCase ) self.assertFalse(jnp.isinf(UpperCamelCase ).any() ) def snake_case ( self ): """simple docstring""" lowerCamelCase_ = 20 lowerCamelCase_ = 4 lowerCamelCase_ = 0 lowerCamelCase_ = 5 lowerCamelCase_ = FlaxForcedEOSTokenLogitsProcessor(max_length=UpperCamelCase , eos_token_id=UpperCamelCase ) # check that all scores are -inf except the eos_token_id when max_length is reached lowerCamelCase_ = ids_tensor((batch_size, 4) , vocab_size=20 ) lowerCamelCase_ = 4 lowerCamelCase_ = self._get_uniform_logits(UpperCamelCase , UpperCamelCase ) lowerCamelCase_ = logits_processor(UpperCamelCase , UpperCamelCase , cur_len=UpperCamelCase ) self.assertTrue(jnp.isneginf(scores[:, eos_token_id + 1 :] ).all() ) self.assertListEqual(scores[:, eos_token_id].tolist() , 4 * [0] ) # score for eos_token_id should be zero # check that eos_token_id is not forced if max_length is not reached lowerCamelCase_ = 3 lowerCamelCase_ = self._get_uniform_logits(UpperCamelCase , UpperCamelCase ) lowerCamelCase_ = logits_processor(UpperCamelCase , UpperCamelCase , cur_len=UpperCamelCase ) self.assertFalse(jnp.isinf(UpperCamelCase ).any() ) def snake_case ( self ): """simple docstring""" lowerCamelCase_ = 4 lowerCamelCase_ = 10 lowerCamelCase_ = 15 lowerCamelCase_ = 2 lowerCamelCase_ = 1 lowerCamelCase_ = 15 # dummy input_ids and scores lowerCamelCase_ = ids_tensor((batch_size, sequence_length) , UpperCamelCase ) lowerCamelCase_ = input_ids.copy() lowerCamelCase_ = self._get_uniform_logits(UpperCamelCase , UpperCamelCase ) lowerCamelCase_ = scores.copy() # instantiate all dist processors lowerCamelCase_ = FlaxTemperatureLogitsWarper(temperature=0.5 ) lowerCamelCase_ = FlaxTopKLogitsWarper(3 ) lowerCamelCase_ = FlaxTopPLogitsWarper(0.8 ) # instantiate all logits processors lowerCamelCase_ = FlaxMinLengthLogitsProcessor(min_length=10 , eos_token_id=UpperCamelCase ) lowerCamelCase_ = FlaxForcedBOSTokenLogitsProcessor(bos_token_id=UpperCamelCase ) lowerCamelCase_ = FlaxForcedEOSTokenLogitsProcessor(max_length=UpperCamelCase , eos_token_id=UpperCamelCase ) lowerCamelCase_ = 10 # no processor list lowerCamelCase_ = temp_dist_warp(UpperCamelCase , UpperCamelCase , cur_len=UpperCamelCase ) lowerCamelCase_ = top_k_warp(UpperCamelCase , UpperCamelCase , cur_len=UpperCamelCase ) lowerCamelCase_ = top_p_warp(UpperCamelCase , UpperCamelCase , cur_len=UpperCamelCase ) lowerCamelCase_ = min_dist_proc(UpperCamelCase , UpperCamelCase , cur_len=UpperCamelCase ) lowerCamelCase_ = bos_dist_proc(UpperCamelCase , UpperCamelCase , cur_len=UpperCamelCase ) lowerCamelCase_ = eos_dist_proc(UpperCamelCase , UpperCamelCase , cur_len=UpperCamelCase ) # with processor list lowerCamelCase_ = FlaxLogitsProcessorList( [temp_dist_warp, top_k_warp, top_p_warp, min_dist_proc, bos_dist_proc, eos_dist_proc] ) lowerCamelCase_ = processor(UpperCamelCase , UpperCamelCase , cur_len=UpperCamelCase ) # scores should be equal self.assertTrue(jnp.allclose(UpperCamelCase , UpperCamelCase , atol=1e-3 ) ) # input_ids should never be changed self.assertListEqual(input_ids.tolist() , input_ids_comp.tolist() ) def snake_case ( self ): """simple docstring""" lowerCamelCase_ = 4 lowerCamelCase_ = 10 lowerCamelCase_ = 15 lowerCamelCase_ = 2 lowerCamelCase_ = 1 lowerCamelCase_ = 15 # dummy input_ids and scores lowerCamelCase_ = ids_tensor((batch_size, sequence_length) , UpperCamelCase ) lowerCamelCase_ = input_ids.copy() lowerCamelCase_ = self._get_uniform_logits(UpperCamelCase , UpperCamelCase ) lowerCamelCase_ = scores.copy() # instantiate all dist processors lowerCamelCase_ = FlaxTemperatureLogitsWarper(temperature=0.5 ) lowerCamelCase_ = FlaxTopKLogitsWarper(3 ) lowerCamelCase_ = FlaxTopPLogitsWarper(0.8 ) # instantiate all logits processors lowerCamelCase_ = FlaxMinLengthLogitsProcessor(min_length=10 , eos_token_id=UpperCamelCase ) lowerCamelCase_ = FlaxForcedBOSTokenLogitsProcessor(bos_token_id=UpperCamelCase ) lowerCamelCase_ = FlaxForcedEOSTokenLogitsProcessor(max_length=UpperCamelCase , eos_token_id=UpperCamelCase ) lowerCamelCase_ = 10 # no processor list def run_no_processor_list(UpperCamelCase , UpperCamelCase , UpperCamelCase ): lowerCamelCase_ = temp_dist_warp(UpperCamelCase , UpperCamelCase , cur_len=UpperCamelCase ) lowerCamelCase_ = top_k_warp(UpperCamelCase , UpperCamelCase , cur_len=UpperCamelCase ) lowerCamelCase_ = top_p_warp(UpperCamelCase , UpperCamelCase , cur_len=UpperCamelCase ) lowerCamelCase_ = min_dist_proc(UpperCamelCase , UpperCamelCase , cur_len=UpperCamelCase ) lowerCamelCase_ = bos_dist_proc(UpperCamelCase , UpperCamelCase , cur_len=UpperCamelCase ) lowerCamelCase_ = eos_dist_proc(UpperCamelCase , UpperCamelCase , cur_len=UpperCamelCase ) return scores # with processor list def run_processor_list(UpperCamelCase , UpperCamelCase , UpperCamelCase ): lowerCamelCase_ = FlaxLogitsProcessorList( [temp_dist_warp, top_k_warp, top_p_warp, min_dist_proc, bos_dist_proc, eos_dist_proc] ) lowerCamelCase_ = processor(UpperCamelCase , UpperCamelCase , cur_len=UpperCamelCase ) return scores lowerCamelCase_ = jax.jit(UpperCamelCase ) lowerCamelCase_ = jax.jit(UpperCamelCase ) lowerCamelCase_ = jitted_run_no_processor_list(UpperCamelCase , UpperCamelCase , UpperCamelCase ) lowerCamelCase_ = jitted_run_processor_list(UpperCamelCase , UpperCamelCase , UpperCamelCase ) # scores should be equal self.assertTrue(jnp.allclose(UpperCamelCase , UpperCamelCase , atol=1e-3 ) ) # input_ids should never be changed self.assertListEqual(input_ids.tolist() , input_ids_comp.tolist() )
55
from typing import Dict import numpy as np from ..utils import add_end_docstrings, is_tf_available, is_torch_available, logging from .base import PIPELINE_INIT_ARGS, GenericTensor, Pipeline, PipelineException if is_tf_available(): import tensorflow as tf from ..tf_utils import stable_softmax if is_torch_available(): import torch lowercase__ : Dict = logging.get_logger(__name__) @add_end_docstrings( UpperCamelCase_ , r""" top_k (`int`, defaults to 5): The number of predictions to return. targets (`str` or `List[str]`, *optional*): When passed, the model will limit the scores to the passed targets instead of looking up in the whole vocab. If the provided targets are not in the model vocab, they will be tokenized and the first resulting token will be used (with a warning, and that might be slower). """ , ) class lowercase_ ( UpperCamelCase_ ): """simple docstring""" def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE ) ->np.ndarray: if self.framework == "tf": lowerCAmelCase = tf.where(input_ids == self.tokenizer.mask_token_id ).numpy() elif self.framework == "pt": lowerCAmelCase = torch.nonzero(input_ids == self.tokenizer.mask_token_id , as_tuple=__SCREAMING_SNAKE_CASE ) else: raise ValueError('''Unsupported framework''' ) return masked_index def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE ) ->np.ndarray: lowerCAmelCase = self.get_masked_index(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = np.prod(masked_index.shape ) if numel < 1: raise PipelineException( '''fill-mask''' , self.model.base_model_prefix , F"No mask_token ({self.tokenizer.mask_token}) found on the input" , ) def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE ) ->str: if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): for model_input in model_inputs: self._ensure_exactly_one_mask_token(model_input['''input_ids'''][0] ) else: for input_ids in model_inputs["input_ids"]: self._ensure_exactly_one_mask_token(__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None , **__SCREAMING_SNAKE_CASE ) ->Dict[str, GenericTensor]: if return_tensors is None: lowerCAmelCase = self.framework lowerCAmelCase = self.tokenizer(__SCREAMING_SNAKE_CASE , return_tensors=__SCREAMING_SNAKE_CASE ) self.ensure_exactly_one_mask_token(__SCREAMING_SNAKE_CASE ) return model_inputs def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE ) ->Tuple: lowerCAmelCase = self.model(**__SCREAMING_SNAKE_CASE ) lowerCAmelCase = model_inputs['''input_ids'''] return model_outputs def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=5 , __SCREAMING_SNAKE_CASE=None ) ->str: # Cap top_k if there are targets if target_ids is not None and target_ids.shape[0] < top_k: lowerCAmelCase = target_ids.shape[0] lowerCAmelCase = model_outputs['''input_ids'''][0] lowerCAmelCase = model_outputs['''logits'''] if self.framework == "tf": lowerCAmelCase = tf.where(input_ids == self.tokenizer.mask_token_id ).numpy()[:, 0] lowerCAmelCase = outputs.numpy() lowerCAmelCase = outputs[0, masked_index, :] lowerCAmelCase = stable_softmax(__SCREAMING_SNAKE_CASE , axis=-1 ) if target_ids is not None: lowerCAmelCase = tf.gather_nd(tf.squeeze(__SCREAMING_SNAKE_CASE , 0 ) , target_ids.reshape(-1 , 1 ) ) lowerCAmelCase = tf.expand_dims(__SCREAMING_SNAKE_CASE , 0 ) lowerCAmelCase = tf.math.top_k(__SCREAMING_SNAKE_CASE , k=__SCREAMING_SNAKE_CASE ) lowerCAmelCase , lowerCAmelCase = topk.values.numpy(), topk.indices.numpy() else: lowerCAmelCase = torch.nonzero(input_ids == self.tokenizer.mask_token_id , as_tuple=__SCREAMING_SNAKE_CASE ).squeeze(-1 ) # Fill mask pipeline supports only one ${mask_token} per sample lowerCAmelCase = outputs[0, masked_index, :] lowerCAmelCase = logits.softmax(dim=-1 ) if target_ids is not None: lowerCAmelCase = probs[..., target_ids] lowerCAmelCase , lowerCAmelCase = probs.topk(__SCREAMING_SNAKE_CASE ) lowerCAmelCase = [] lowerCAmelCase = values.shape[0] == 1 for i, (_values, _predictions) in enumerate(zip(values.tolist() , predictions.tolist() ) ): lowerCAmelCase = [] for v, p in zip(_values , _predictions ): # Copy is important since we're going to modify this array in place lowerCAmelCase = input_ids.numpy().copy() if target_ids is not None: lowerCAmelCase = target_ids[p].tolist() lowerCAmelCase = p # Filter padding out: lowerCAmelCase = tokens[np.where(tokens != self.tokenizer.pad_token_id )] # Originally we skip special tokens to give readable output. # For multi masks though, the other [MASK] would be removed otherwise # making the output look odd, so we add them back lowerCAmelCase = self.tokenizer.decode(__SCREAMING_SNAKE_CASE , skip_special_tokens=__SCREAMING_SNAKE_CASE ) lowerCAmelCase = {'''score''': v, '''token''': p, '''token_str''': self.tokenizer.decode([p] ), '''sequence''': sequence} row.append(__SCREAMING_SNAKE_CASE ) result.append(__SCREAMING_SNAKE_CASE ) if single_mask: return result[0] return result def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE=None ) ->Optional[Any]: if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): lowerCAmelCase = [targets] try: lowerCAmelCase = self.tokenizer.get_vocab() except Exception: lowerCAmelCase = {} lowerCAmelCase = [] for target in targets: lowerCAmelCase = vocab.get(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) if id_ is None: lowerCAmelCase = self.tokenizer( __SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , return_attention_mask=__SCREAMING_SNAKE_CASE , return_token_type_ids=__SCREAMING_SNAKE_CASE , max_length=1 , truncation=__SCREAMING_SNAKE_CASE , )['''input_ids'''] if len(__SCREAMING_SNAKE_CASE ) == 0: logger.warning( F"The specified target token `{target}` does not exist in the model vocabulary. " '''We cannot replace it with anything meaningful, ignoring it''' ) continue lowerCAmelCase = input_ids[0] # XXX: If users encounter this pass # it becomes pretty slow, so let's make sure # The warning enables them to fix the input to # get faster performance. logger.warning( F"The specified target token `{target}` does not exist in the model vocabulary. " F"Replacing with `{self.tokenizer.convert_ids_to_tokens(id_ )}`." ) target_ids.append(id_ ) lowerCAmelCase = list(set(__SCREAMING_SNAKE_CASE ) ) if len(__SCREAMING_SNAKE_CASE ) == 0: raise ValueError('''At least one target must be provided when passed.''' ) lowerCAmelCase = np.array(__SCREAMING_SNAKE_CASE ) return target_ids def SCREAMING_SNAKE_CASE_ ( self , __SCREAMING_SNAKE_CASE=None , __SCREAMING_SNAKE_CASE=None ) ->Dict: lowerCAmelCase = {} if targets is not None: lowerCAmelCase = self.get_target_ids(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) lowerCAmelCase = target_ids if top_k is not None: lowerCAmelCase = top_k if self.tokenizer.mask_token_id is None: raise PipelineException( '''fill-mask''' , self.model.base_model_prefix , '''The tokenizer does not define a `mask_token`.''' ) return {}, {}, postprocess_params def __call__( self , __SCREAMING_SNAKE_CASE , *__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) ->List[Any]: lowerCAmelCase = super().__call__(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) and len(__SCREAMING_SNAKE_CASE ) == 1: return outputs[0] return outputs
338
0
'''simple docstring''' import datetime import platform import subprocess from typing import Optional, Tuple, Union import numpy as np def __magic_name__ ( __UpperCAmelCase, __UpperCAmelCase ) -> np.array: '''simple docstring''' snake_case_ = F"{sampling_rate}" snake_case_ = '''1''' snake_case_ = '''f32le''' snake_case_ = [ '''ffmpeg''', '''-i''', '''pipe:0''', '''-ac''', ac, '''-ar''', ar, '''-f''', format_for_conversion, '''-hide_banner''', '''-loglevel''', '''quiet''', '''pipe:1''', ] try: with subprocess.Popen(__UpperCAmelCase, stdin=subprocess.PIPE, stdout=subprocess.PIPE ) as ffmpeg_process: snake_case_ = ffmpeg_process.communicate(__UpperCAmelCase ) except FileNotFoundError as error: raise ValueError('''ffmpeg was not found but is required to load audio files from filename''' ) from error snake_case_ = output_stream[0] snake_case_ = np.frombuffer(__UpperCAmelCase, np.floataa ) if audio.shape[0] == 0: raise ValueError('''Malformed soundfile''' ) return audio def __magic_name__ ( __UpperCAmelCase, __UpperCAmelCase, __UpperCAmelCase = "f32le", ) -> List[Any]: '''simple docstring''' snake_case_ = F"{sampling_rate}" snake_case_ = '''1''' if format_for_conversion == "s16le": snake_case_ = 2 elif format_for_conversion == "f32le": snake_case_ = 4 else: raise ValueError(F"Unhandled format `{format_for_conversion}`. Please use `s16le` or `f32le`" ) snake_case_ = platform.system() if system == "Linux": snake_case_ = '''alsa''' snake_case_ = '''default''' elif system == "Darwin": snake_case_ = '''avfoundation''' snake_case_ = ''':0''' elif system == "Windows": snake_case_ = '''dshow''' snake_case_ = '''default''' snake_case_ = [ '''ffmpeg''', '''-f''', format_, '''-i''', input_, '''-ac''', ac, '''-ar''', ar, '''-f''', format_for_conversion, '''-fflags''', '''nobuffer''', '''-hide_banner''', '''-loglevel''', '''quiet''', '''pipe:1''', ] snake_case_ = int(round(sampling_rate * chunk_length_s ) ) * size_of_sample snake_case_ = _ffmpeg_stream(__UpperCAmelCase, __UpperCAmelCase ) for item in iterator: yield item def __magic_name__ ( __UpperCAmelCase, __UpperCAmelCase, __UpperCAmelCase = None, __UpperCAmelCase = None, __UpperCAmelCase = "f32le", ) -> Dict: '''simple docstring''' if stream_chunk_s is not None: snake_case_ = stream_chunk_s else: snake_case_ = chunk_length_s snake_case_ = ffmpeg_microphone(__UpperCAmelCase, __UpperCAmelCase, format_for_conversion=__UpperCAmelCase ) if format_for_conversion == "s16le": snake_case_ = np.intaa snake_case_ = 2 elif format_for_conversion == "f32le": snake_case_ = np.floataa snake_case_ = 4 else: raise ValueError(F"Unhandled format `{format_for_conversion}`. Please use `s16le` or `f32le`" ) if stride_length_s is None: snake_case_ = chunk_length_s / 6 snake_case_ = int(round(sampling_rate * chunk_length_s ) ) * size_of_sample if isinstance(__UpperCAmelCase, (int, float) ): snake_case_ = [stride_length_s, stride_length_s] snake_case_ = int(round(sampling_rate * stride_length_s[0] ) ) * size_of_sample snake_case_ = int(round(sampling_rate * stride_length_s[1] ) ) * size_of_sample snake_case_ = datetime.datetime.now() snake_case_ = datetime.timedelta(seconds=__UpperCAmelCase ) for item in chunk_bytes_iter(__UpperCAmelCase, __UpperCAmelCase, stride=(stride_left, stride_right), stream=__UpperCAmelCase ): # Put everything back in numpy scale snake_case_ = np.frombuffer(item['''raw'''], dtype=__UpperCAmelCase ) snake_case_ = ( item['''stride'''][0] // size_of_sample, item['''stride'''][1] // size_of_sample, ) snake_case_ = sampling_rate audio_time += delta if datetime.datetime.now() > audio_time + 10 * delta: # We're late !! SKIP continue yield item def __magic_name__ ( __UpperCAmelCase, __UpperCAmelCase, __UpperCAmelCase, __UpperCAmelCase = False ) -> Any: '''simple docstring''' snake_case_ = b'''''' snake_case_ ,snake_case_ = stride if stride_left + stride_right >= chunk_len: raise ValueError( F"Stride needs to be strictly smaller than chunk_len: ({stride_left}, {stride_right}) vs {chunk_len}" ) snake_case_ = 0 for raw in iterator: acc += raw if stream and len(__UpperCAmelCase ) < chunk_len: snake_case_ = (_stride_left, 0) yield {"raw": acc[:chunk_len], "stride": stride, "partial": True} else: while len(__UpperCAmelCase ) >= chunk_len: # We are flushing the accumulator snake_case_ = (_stride_left, stride_right) snake_case_ = {'''raw''': acc[:chunk_len], '''stride''': stride} if stream: snake_case_ = False yield item snake_case_ = stride_left snake_case_ = acc[chunk_len - stride_left - stride_right :] # Last chunk if len(__UpperCAmelCase ) > stride_left: snake_case_ = {'''raw''': acc, '''stride''': (_stride_left, 0)} if stream: snake_case_ = False yield item def __magic_name__ ( __UpperCAmelCase, __UpperCAmelCase ) -> Optional[int]: '''simple docstring''' snake_case_ = 2**24 # 16Mo try: with subprocess.Popen(__UpperCAmelCase, stdout=subprocess.PIPE, bufsize=__UpperCAmelCase ) as ffmpeg_process: while True: snake_case_ = ffmpeg_process.stdout.read(__UpperCAmelCase ) if raw == b"": break yield raw except FileNotFoundError as error: raise ValueError('''ffmpeg was not found but is required to stream audio files from filename''' ) from error
56
from typing import TYPE_CHECKING from ...utils import _LazyModule lowercase__ : int = {'''tokenization_wav2vec2_phoneme''': ['''Wav2Vec2PhonemeCTCTokenizer''']} if TYPE_CHECKING: from .tokenization_wavaveca_phoneme import WavaVecaPhonemeCTCTokenizer else: import sys lowercase__ : Optional[int] = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
338
0
"""simple docstring""" import sacrebleu as scb from packaging import version from sacrebleu import TER import datasets A : Optional[Any] = "\\n@inproceedings{snover-etal-2006-study,\n title = \"A Study of Translation Edit Rate with Targeted Human Annotation\",\n author = \"Snover, Matthew and\n Dorr, Bonnie and\n Schwartz, Rich and\n Micciulla, Linnea and\n Makhoul, John\",\n booktitle = \"Proceedings of the 7th Conference of the Association for Machine Translation in the Americas: Technical Papers\",\n month = aug # \" 8-12\",\n year = \"2006\",\n address = \"Cambridge, Massachusetts, USA\",\n publisher = \"Association for Machine Translation in the Americas\",\n url = \"https://aclanthology.org/2006.amta-papers.25\",\n pages = \"223--231\",\n}\n@inproceedings{post-2018-call,\n title = \"A Call for Clarity in Reporting {BLEU} Scores\",\n author = \"Post, Matt\",\n booktitle = \"Proceedings of the Third Conference on Machine Translation: Research Papers\",\n month = oct,\n year = \"2018\",\n address = \"Belgium, Brussels\",\n publisher = \"Association for Computational Linguistics\",\n url = \"https://www.aclweb.org/anthology/W18-6319\",\n pages = \"186--191\",\n}\n" A : Optional[int] = "\\nTER (Translation Edit Rate, also called Translation Error Rate) is a metric to quantify the edit operations that a\nhypothesis requires to match a reference translation. We use the implementation that is already present in sacrebleu\n(https://github.com/mjpost/sacreBLEU#ter), which in turn is inspired by the TERCOM implementation, which can be found\nhere: https://github.com/jhclark/tercom.\n\nThe implementation here is slightly different from sacrebleu in terms of the required input format. The length of\nthe references and hypotheses lists need to be the same, so you may need to transpose your references compared to\nsacrebleu's required input format. See https://github.com/huggingface/datasets/issues/3154#issuecomment-950746534\n\nSee the README.md file at https://github.com/mjpost/sacreBLEU#ter for more information.\n" A : Any = "\nProduces TER scores alongside the number of edits and reference length.\n\nArgs:\n predictions (list of str): The system stream (a sequence of segments).\n references (list of list of str): A list of one or more reference streams (each a sequence of segments).\n normalized (boolean): If `True`, applies basic tokenization and normalization to sentences. Defaults to `False`.\n ignore_punct (boolean): If `True`, applies basic tokenization and normalization to sentences. Defaults to `False`.\n support_zh_ja_chars (boolean): If `True`, tokenization/normalization supports processing of Chinese characters,\n as well as Japanese Kanji, Hiragana, Katakana, and Phonetic Extensions of Katakana.\n Only applies if `normalized = True`. Defaults to `False`.\n case_sensitive (boolean): If `False`, makes all predictions and references lowercase to ignore differences in case. Defaults to `False`.\n\nReturns:\n 'score' (float): TER score (num_edits / sum_ref_lengths * 100)\n 'num_edits' (int): The cumulative number of edits\n 'ref_length' (float): The cumulative average reference length\n\nExamples:\n Example 1:\n >>> predictions = [\"does this sentence match??\",\n ... \"what about this sentence?\",\n ... \"What did the TER metric user say to the developer?\"]\n >>> references = [[\"does this sentence match\", \"does this sentence match!?!\"],\n ... [\"wHaT aBoUt ThIs SeNtEnCe?\", \"wHaT aBoUt ThIs SeNtEnCe?\"],\n ... [\"Your jokes are...\", \"...TERrible\"]]\n >>> ter = datasets.load_metric(\"ter\")\n >>> results = ter.compute(predictions=predictions,\n ... references=references,\n ... case_sensitive=True)\n >>> print(results)\n {'score': 150.0, 'num_edits': 15, 'ref_length': 10.0}\n\n Example 2:\n >>> predictions = [\"does this sentence match??\",\n ... \"what about this sentence?\"]\n >>> references = [[\"does this sentence match\", \"does this sentence match!?!\"],\n ... [\"wHaT aBoUt ThIs SeNtEnCe?\", \"wHaT aBoUt ThIs SeNtEnCe?\"]]\n >>> ter = datasets.load_metric(\"ter\")\n >>> results = ter.compute(predictions=predictions,\n ... references=references,\n ... case_sensitive=True)\n >>> print(results)\n {'score': 62.5, 'num_edits': 5, 'ref_length': 8.0}\n\n Example 3:\n >>> predictions = [\"does this sentence match??\",\n ... \"what about this sentence?\"]\n >>> references = [[\"does this sentence match\", \"does this sentence match!?!\"],\n ... [\"wHaT aBoUt ThIs SeNtEnCe?\", \"wHaT aBoUt ThIs SeNtEnCe?\"]]\n >>> ter = datasets.load_metric(\"ter\")\n >>> results = ter.compute(predictions=predictions,\n ... references=references,\n ... normalized=True,\n ... case_sensitive=True)\n >>> print(results)\n {'score': 57.14285714285714, 'num_edits': 6, 'ref_length': 10.5}\n\n Example 4:\n >>> predictions = [\"does this sentence match??\",\n ... \"what about this sentence?\"]\n >>> references = [[\"does this sentence match\", \"does this sentence match!?!\"],\n ... [\"wHaT aBoUt ThIs SeNtEnCe?\", \"wHaT aBoUt ThIs SeNtEnCe?\"]]\n >>> ter = datasets.load_metric(\"ter\")\n >>> results = ter.compute(predictions=predictions,\n ... references=references,\n ... ignore_punct=True,\n ... case_sensitive=False)\n >>> print(results)\n {'score': 0.0, 'num_edits': 0, 'ref_length': 8.0}\n\n Example 5:\n >>> predictions = [\"does this sentence match??\",\n ... \"what about this sentence?\",\n ... \"What did the TER metric user say to the developer?\"]\n >>> references = [[\"does this sentence match\", \"does this sentence match!?!\"],\n ... [\"wHaT aBoUt ThIs SeNtEnCe?\", \"wHaT aBoUt ThIs SeNtEnCe?\"],\n ... [\"Your jokes are...\", \"...TERrible\"]]\n >>> ter = datasets.load_metric(\"ter\")\n >>> results = ter.compute(predictions=predictions,\n ... references=references,\n ... ignore_punct=True,\n ... case_sensitive=False)\n >>> print(results)\n {'score': 100.0, 'num_edits': 10, 'ref_length': 10.0}\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION ,_KWARGS_DESCRIPTION ) class _UpperCamelCase ( datasets.Metric ): '''simple docstring''' def snake_case ( self ): if version.parse(scb.__version__ ) < version.parse("1.4.12" ): raise ImportWarning( "To use `sacrebleu`, the module `sacrebleu>=1.4.12` is required, and the current version of `sacrebleu` doesn't match this condition.\n" "You can install it with `pip install \"sacrebleu>=1.4.12\"`." ) return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , homepage="http://www.cs.umd.edu/~snover/tercom/" , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { "predictions": datasets.Value("string" , id="sequence" ), "references": datasets.Sequence(datasets.Value("string" , id="sequence" ) , id="references" ), } ) , codebase_urls=["https://github.com/mjpost/sacreBLEU#ter"] , reference_urls=[ "https://github.com/jhclark/tercom", ] , ) def snake_case ( self , __a , __a , __a = False , __a = False , __a = False , __a = False , ): __lowerCAmelCase = len(references[0] ) if any(len(__a ) != references_per_prediction for refs in references ): raise ValueError("Sacrebleu requires the same number of references for each prediction" ) __lowerCAmelCase = [[refs[i] for refs in references] for i in range(__a )] __lowerCAmelCase = TER( normalized=__a , no_punct=__a , asian_support=__a , case_sensitive=__a , ) __lowerCAmelCase = sb_ter.corpus_score(__a , __a ) return {"score": output.score, "num_edits": output.num_edits, "ref_length": output.ref_length}
57
lowercase__ : Optional[int] = '''ABCDEFGHIJKLMNOPQRSTUVWXYZ''' def SCREAMING_SNAKE_CASE_ ( ) -> None: lowerCAmelCase = input('''Enter message: ''' ) lowerCAmelCase = input('''Enter key [alphanumeric]: ''' ) lowerCAmelCase = input('''Encrypt/Decrypt [e/d]: ''' ) if mode.lower().startswith('''e''' ): lowerCAmelCase = '''encrypt''' lowerCAmelCase = encrypt_message(snake_case__ , snake_case__ ) elif mode.lower().startswith('''d''' ): lowerCAmelCase = '''decrypt''' lowerCAmelCase = decrypt_message(snake_case__ , snake_case__ ) print(f"\n{mode.title()}ed message:" ) print(snake_case__ ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ ) -> str: return translate_message(snake_case__ , snake_case__ , '''encrypt''' ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ ) -> str: return translate_message(snake_case__ , snake_case__ , '''decrypt''' ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> str: lowerCAmelCase = [] lowerCAmelCase = 0 lowerCAmelCase = key.upper() for symbol in message: lowerCAmelCase = LETTERS.find(symbol.upper() ) if num != -1: if mode == "encrypt": num += LETTERS.find(key[key_index] ) elif mode == "decrypt": num -= LETTERS.find(key[key_index] ) num %= len(snake_case__ ) if symbol.isupper(): translated.append(LETTERS[num] ) elif symbol.islower(): translated.append(LETTERS[num].lower() ) key_index += 1 if key_index == len(snake_case__ ): lowerCAmelCase = 0 else: translated.append(snake_case__ ) return "".join(snake_case__ ) if __name__ == "__main__": main()
338
0
'''simple docstring''' import unittest from transformers import is_torch_available from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device if is_torch_available(): from transformers import AutoModelForSeqaSeqLM, AutoTokenizer @require_torch @require_sentencepiece @require_tokenizers class a_ ( unittest.TestCase ): '''simple docstring''' @slow def snake_case_( self ) -> int: _SCREAMING_SNAKE_CASE = AutoModelForSeqaSeqLM.from_pretrained("""google/mt5-small""" , return_dict=A ).to(A ) _SCREAMING_SNAKE_CASE = AutoTokenizer.from_pretrained("""google/mt5-small""" ) _SCREAMING_SNAKE_CASE = tokenizer("""Hello there""" , return_tensors="""pt""" ).input_ids _SCREAMING_SNAKE_CASE = tokenizer("""Hi I am""" , return_tensors="""pt""" ).input_ids _SCREAMING_SNAKE_CASE = model(input_ids.to(A ) , labels=labels.to(A ) ).loss _SCREAMING_SNAKE_CASE = -(labels.shape[-1] * loss.item()) _SCREAMING_SNAKE_CASE = -84.9127 self.assertTrue(abs(mtf_score - EXPECTED_SCORE ) < 1e-4 )
58
from collections import defaultdict from math import ceil, sqrt def SCREAMING_SNAKE_CASE_ ( snake_case__ = 1_0_0_0_0_0_0 , snake_case__ = 1_0 ) -> int: lowerCAmelCase = defaultdict(snake_case__ ) for outer_width in range(3 , (t_limit // 4) + 2 ): if outer_width * outer_width > t_limit: lowerCAmelCase = max( ceil(sqrt(outer_width * outer_width - t_limit ) ) , 1 ) else: lowerCAmelCase = 1 hole_width_lower_bound += (outer_width - hole_width_lower_bound) % 2 for hole_width in range(snake_case__ , outer_width - 1 , 2 ): count[outer_width * outer_width - hole_width * hole_width] += 1 return sum(1 for n in count.values() if 1 <= n <= 1_0 ) if __name__ == "__main__": print(f'{solution() = }')
338
0
import torch from diffusers import StableDiffusionPipeline __lowerCamelCase = """path-to-your-trained-model""" __lowerCamelCase = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.floataa).to("""cuda""") __lowerCamelCase = """A photo of sks dog in a bucket""" __lowerCamelCase = pipe(prompt, num_inference_steps=50, guidance_scale=7.5).images[0] image.save("""dog-bucket.png""")
59
import pytest from datasets import Dataset, DatasetDict, Features, NamedSplit, Value from datasets.io.text import TextDatasetReader from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ ) -> Union[str, Any]: assert isinstance(snake_case__ , snake_case__ ) assert dataset.num_rows == 4 assert dataset.num_columns == 1 assert dataset.column_names == ["text"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize('''keep_in_memory''' , [False, True] ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> Union[str, Any]: lowerCAmelCase = tmp_path / '''cache''' lowerCAmelCase = {'''text''': '''string'''} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): lowerCAmelCase = TextDatasetReader(snake_case__ , cache_dir=snake_case__ , keep_in_memory=snake_case__ ).read() _check_text_dataset(snake_case__ , snake_case__ ) @pytest.mark.parametrize( '''features''' , [ None, {'''text''': '''string'''}, {'''text''': '''int32'''}, {'''text''': '''float32'''}, ] , ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> Optional[Any]: lowerCAmelCase = tmp_path / '''cache''' lowerCAmelCase = {'''text''': '''string'''} lowerCAmelCase = features.copy() if features else default_expected_features lowerCAmelCase = ( Features({feature: Value(snake_case__ ) for feature, dtype in features.items()} ) if features is not None else None ) lowerCAmelCase = TextDatasetReader(snake_case__ , features=snake_case__ , cache_dir=snake_case__ ).read() _check_text_dataset(snake_case__ , snake_case__ ) @pytest.mark.parametrize('''split''' , [None, NamedSplit('''train''' ), '''train''', '''test'''] ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> List[str]: lowerCAmelCase = tmp_path / '''cache''' lowerCAmelCase = {'''text''': '''string'''} lowerCAmelCase = TextDatasetReader(snake_case__ , cache_dir=snake_case__ , split=snake_case__ ).read() _check_text_dataset(snake_case__ , snake_case__ ) assert dataset.split == split if split else "train" @pytest.mark.parametrize('''path_type''' , [str, list] ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> Optional[int]: if issubclass(snake_case__ , snake_case__ ): lowerCAmelCase = text_path elif issubclass(snake_case__ , snake_case__ ): lowerCAmelCase = [text_path] lowerCAmelCase = tmp_path / '''cache''' lowerCAmelCase = {'''text''': '''string'''} lowerCAmelCase = TextDatasetReader(snake_case__ , cache_dir=snake_case__ ).read() _check_text_dataset(snake_case__ , snake_case__ ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__=("train",) ) -> Optional[Any]: assert isinstance(snake_case__ , snake_case__ ) for split in splits: lowerCAmelCase = dataset_dict[split] assert dataset.num_rows == 4 assert dataset.num_columns == 1 assert dataset.column_names == ["text"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize('''keep_in_memory''' , [False, True] ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> Optional[Any]: lowerCAmelCase = tmp_path / '''cache''' lowerCAmelCase = {'''text''': '''string'''} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): lowerCAmelCase = TextDatasetReader({'''train''': text_path} , cache_dir=snake_case__ , keep_in_memory=snake_case__ ).read() _check_text_datasetdict(snake_case__ , snake_case__ ) @pytest.mark.parametrize( '''features''' , [ None, {'''text''': '''string'''}, {'''text''': '''int32'''}, {'''text''': '''float32'''}, ] , ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> List[Any]: lowerCAmelCase = tmp_path / '''cache''' # CSV file loses col_1 string dtype information: default now is "int64" instead of "string" lowerCAmelCase = {'''text''': '''string'''} lowerCAmelCase = features.copy() if features else default_expected_features lowerCAmelCase = ( Features({feature: Value(snake_case__ ) for feature, dtype in features.items()} ) if features is not None else None ) lowerCAmelCase = TextDatasetReader({'''train''': text_path} , features=snake_case__ , cache_dir=snake_case__ ).read() _check_text_datasetdict(snake_case__ , snake_case__ ) @pytest.mark.parametrize('''split''' , [None, NamedSplit('''train''' ), '''train''', '''test'''] ) def SCREAMING_SNAKE_CASE_ ( snake_case__ , snake_case__ , snake_case__ ) -> Any: if split: lowerCAmelCase = {split: text_path} else: lowerCAmelCase = '''train''' lowerCAmelCase = {'''train''': text_path, '''test''': text_path} lowerCAmelCase = tmp_path / '''cache''' lowerCAmelCase = {'''text''': '''string'''} lowerCAmelCase = TextDatasetReader(snake_case__ , cache_dir=snake_case__ ).read() _check_text_datasetdict(snake_case__ , snake_case__ , splits=list(path.keys() ) ) assert all(dataset[split].split == split for split in path.keys() )
338
0
"""simple docstring""" import argparse import json import re from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( MobileNetVaConfig, MobileNetVaForImageClassification, MobileNetVaImageProcessor, load_tf_weights_in_mobilenet_va, ) from transformers.utils import logging logging.set_verbosity_info() snake_case__ : Any = logging.get_logger(__name__) def _snake_case ( _snake_case : str ): lowerCAmelCase : Optional[int] = MobileNetVaConfig(layer_norm_eps=0.001 ) if "_quant" in model_name: raise ValueError('''Quantized models are not supported.''' ) lowerCAmelCase : Union[str, Any] = re.match(r'''^mobilenet_v1_([^_]*)_([^_]*)$''' , _snake_case ) if matches: lowerCAmelCase : List[Any] = float(matches[1] ) lowerCAmelCase : Optional[Any] = int(matches[2] ) # The TensorFlow version of MobileNetV1 predicts 1001 classes instead of # the usual 1000. The first class (index 0) is "background". lowerCAmelCase : Tuple = 1001 lowerCAmelCase : Optional[int] = '''imagenet-1k-id2label.json''' lowerCAmelCase : List[Any] = '''huggingface/label-files''' lowerCAmelCase : str = json.load(open(hf_hub_download(_snake_case , _snake_case , repo_type='''dataset''' ) , '''r''' ) ) lowerCAmelCase : List[str] = {int(_snake_case ) + 1: v for k, v in idalabel.items()} lowerCAmelCase : Optional[int] = '''background''' lowerCAmelCase : Tuple = idalabel lowerCAmelCase : Union[str, Any] = {v: k for k, v in idalabel.items()} return config def _snake_case ( ): lowerCAmelCase : Any = '''http://images.cocodataset.org/val2017/000000039769.jpg''' lowerCAmelCase : List[str] = Image.open(requests.get(_snake_case , stream=_snake_case ).raw ) return im @torch.no_grad() def _snake_case ( _snake_case : List[str] , _snake_case : Optional[Any] , _snake_case : Any , _snake_case : Tuple=False ): lowerCAmelCase : Dict = get_mobilenet_va_config(_snake_case ) # Load 🤗 model lowerCAmelCase : Union[str, Any] = MobileNetVaForImageClassification(_snake_case ).eval() # Load weights from TensorFlow checkpoint load_tf_weights_in_mobilenet_va(_snake_case , _snake_case , _snake_case ) # Check outputs on an image, prepared by MobileNetV1ImageProcessor lowerCAmelCase : Dict = MobileNetVaImageProcessor( crop_size={'''width''': config.image_size, '''height''': config.image_size} , size={'''shortest_edge''': config.image_size + 32} , ) lowerCAmelCase : Dict = image_processor(images=prepare_img() , return_tensors='''pt''' ) lowerCAmelCase : List[Any] = model(**_snake_case ) lowerCAmelCase : Optional[Any] = outputs.logits assert logits.shape == (1, 1001) if model_name == "mobilenet_v1_1.0_224": lowerCAmelCase : str = torch.tensor([-4.1739, -1.1233, 3.1205] ) elif model_name == "mobilenet_v1_0.75_192": lowerCAmelCase : Any = torch.tensor([-3.9440, -2.3141, -0.3333] ) else: lowerCAmelCase : Union[str, Any] = None if expected_logits is not None: assert torch.allclose(logits[0, :3] , _snake_case , atol=1E-4 ) Path(_snake_case ).mkdir(exist_ok=_snake_case ) print(f'''Saving model {model_name} to {pytorch_dump_folder_path}''' ) model.save_pretrained(_snake_case ) print(f'''Saving image processor to {pytorch_dump_folder_path}''' ) image_processor.save_pretrained(_snake_case ) if push_to_hub: print('''Pushing to the hub...''' ) lowerCAmelCase : Optional[Any] = '''google/''' + model_name image_processor.push_to_hub(_snake_case ) model.push_to_hub(_snake_case ) if __name__ == "__main__": snake_case__ : Tuple = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default='''mobilenet_v1_1.0_224''', type=str, help='''Name of the MobileNetV1 model you\'d like to convert. Should in the form \'mobilenet_v1_<depth>_<size>\'.''', ) parser.add_argument( '''--checkpoint_path''', required=True, type=str, help='''Path to the original TensorFlow checkpoint (.ckpt file).''' ) parser.add_argument( '''--pytorch_dump_folder_path''', required=True, type=str, help='''Path to the output PyTorch model directory.''' ) parser.add_argument( '''--push_to_hub''', action='''store_true''', help='''Whether or not to push the converted model to the 🤗 hub.''' ) snake_case__ : Optional[Any] = parser.parse_args() convert_movilevit_checkpoint( args.model_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub )
60
def SCREAMING_SNAKE_CASE_ ( snake_case__ ) -> str: if isinstance(snake_case__ , snake_case__ ): raise TypeError('''\'float\' object cannot be interpreted as an integer''' ) if isinstance(snake_case__ , snake_case__ ): raise TypeError('''\'str\' object cannot be interpreted as an integer''' ) if num == 0: return "0b0" lowerCAmelCase = False if num < 0: lowerCAmelCase = True lowerCAmelCase = -num lowerCAmelCase = [] while num > 0: binary.insert(0 , num % 2 ) num >>= 1 if negative: return "-0b" + "".join(str(snake_case__ ) for e in binary ) return "0b" + "".join(str(snake_case__ ) for e in binary ) if __name__ == "__main__": import doctest doctest.testmod()
338
0