code
stringlengths
82
54.1k
code_codestyle
int64
0
699
style_context
stringlengths
111
35.6k
style_context_codestyle
int64
0
699
label
int64
0
1
import shutil import tempfile import unittest import numpy as np import pytest from transformers.testing_utils import require_vision from transformers.utils import is_vision_available if is_vision_available(): from PIL import Image from transformers import ( AutoProcessor, BertTokenizerFast, BlipImageProcessor, GPTaTokenizer, InstructBlipProcessor, PreTrainedTokenizerFast, ) @require_vision class lowercase__ ( unittest.TestCase ): def _UpperCAmelCase ( self : Optional[int] ): """simple docstring""" UpperCAmelCase__ = tempfile.mkdtemp() UpperCAmelCase__ = BlipImageProcessor() UpperCAmelCase__ = GPTaTokenizer.from_pretrained("hf-internal-testing/tiny-random-GPT2Model" ) UpperCAmelCase__ = BertTokenizerFast.from_pretrained("hf-internal-testing/tiny-random-bert" ) UpperCAmelCase__ = InstructBlipProcessor(_lowercase , _lowercase , _lowercase ) processor.save_pretrained(self.tmpdirname ) def _UpperCAmelCase ( self : List[Any] , **_lowercase : Tuple ): """simple docstring""" return AutoProcessor.from_pretrained(self.tmpdirname , **_lowercase ).tokenizer def _UpperCAmelCase ( self : Optional[int] , **_lowercase : Optional[Any] ): """simple docstring""" return AutoProcessor.from_pretrained(self.tmpdirname , **_lowercase ).image_processor def _UpperCAmelCase ( self : int , **_lowercase : Dict ): """simple docstring""" return AutoProcessor.from_pretrained(self.tmpdirname , **_lowercase ).qformer_tokenizer def _UpperCAmelCase ( self : Any ): """simple docstring""" shutil.rmtree(self.tmpdirname ) def _UpperCAmelCase ( self : List[str] ): """simple docstring""" UpperCAmelCase__ = [np.random.randint(2_55 , size=(3, 30, 4_00) , dtype=np.uinta )] UpperCAmelCase__ = [Image.fromarray(np.moveaxis(_lowercase , 0 , -1 ) ) for x in image_inputs] return image_inputs def _UpperCAmelCase ( self : int ): """simple docstring""" UpperCAmelCase__ = InstructBlipProcessor( tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() , qformer_tokenizer=self.get_qformer_tokenizer() , ) processor.save_pretrained(self.tmpdirname ) UpperCAmelCase__ = self.get_tokenizer(bos_token="(BOS)" , eos_token="(EOS)" ) UpperCAmelCase__ = self.get_image_processor(do_normalize=_lowercase , padding_value=1.0 ) UpperCAmelCase__ = InstructBlipProcessor.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 ) self.assertIsInstance(processor.qformer_tokenizer , _lowercase ) def _UpperCAmelCase ( self : Union[str, Any] ): """simple docstring""" UpperCAmelCase__ = self.get_image_processor() UpperCAmelCase__ = self.get_tokenizer() UpperCAmelCase__ = self.get_qformer_tokenizer() UpperCAmelCase__ = InstructBlipProcessor( tokenizer=_lowercase , image_processor=_lowercase , qformer_tokenizer=_lowercase ) UpperCAmelCase__ = self.prepare_image_inputs() UpperCAmelCase__ = image_processor(_lowercase , return_tensors="np" ) UpperCAmelCase__ = 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 : Optional[int] ): """simple docstring""" UpperCAmelCase__ = self.get_image_processor() UpperCAmelCase__ = self.get_tokenizer() UpperCAmelCase__ = self.get_qformer_tokenizer() UpperCAmelCase__ = InstructBlipProcessor( tokenizer=_lowercase , image_processor=_lowercase , qformer_tokenizer=_lowercase ) UpperCAmelCase__ = """lower newer""" UpperCAmelCase__ = processor(text=_lowercase ) UpperCAmelCase__ = tokenizer(_lowercase , return_token_type_ids=_lowercase ) UpperCAmelCase__ = qformer_tokenizer(_lowercase , return_token_type_ids=_lowercase ) for key in encoded_tokens.keys(): self.assertListEqual(encoded_tokens[key] , encoded_processor[key] ) for key in encoded_tokens_qformer.keys(): self.assertListEqual(encoded_tokens_qformer[key] , encoded_processor["qformer_" + key] ) def _UpperCAmelCase ( self : List[str] ): """simple docstring""" UpperCAmelCase__ = self.get_image_processor() UpperCAmelCase__ = self.get_tokenizer() UpperCAmelCase__ = self.get_qformer_tokenizer() UpperCAmelCase__ = InstructBlipProcessor( tokenizer=_lowercase , image_processor=_lowercase , qformer_tokenizer=_lowercase ) UpperCAmelCase__ = """lower newer""" UpperCAmelCase__ = self.prepare_image_inputs() UpperCAmelCase__ = processor(text=_lowercase , images=_lowercase ) self.assertListEqual( list(inputs.keys() ) , ["input_ids", "attention_mask", "qformer_input_ids", "qformer_attention_mask", "pixel_values"] , ) # test if it raises when no input is passed with pytest.raises(_lowercase ): processor() def _UpperCAmelCase ( self : Any ): """simple docstring""" UpperCAmelCase__ = self.get_image_processor() UpperCAmelCase__ = self.get_tokenizer() UpperCAmelCase__ = self.get_qformer_tokenizer() UpperCAmelCase__ = InstructBlipProcessor( tokenizer=_lowercase , image_processor=_lowercase , qformer_tokenizer=_lowercase ) UpperCAmelCase__ = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] UpperCAmelCase__ = processor.batch_decode(_lowercase ) UpperCAmelCase__ = tokenizer.batch_decode(_lowercase ) self.assertListEqual(_lowercase , _lowercase ) def _UpperCAmelCase ( self : Union[str, Any] ): """simple docstring""" UpperCAmelCase__ = self.get_image_processor() UpperCAmelCase__ = self.get_tokenizer() UpperCAmelCase__ = self.get_qformer_tokenizer() UpperCAmelCase__ = InstructBlipProcessor( tokenizer=_lowercase , image_processor=_lowercase , qformer_tokenizer=_lowercase ) UpperCAmelCase__ = """lower newer""" UpperCAmelCase__ = self.prepare_image_inputs() UpperCAmelCase__ = processor(text=_lowercase , images=_lowercase ) self.assertListEqual( list(inputs.keys() ) , ["input_ids", "attention_mask", "qformer_input_ids", "qformer_attention_mask", "pixel_values"] , )
475
'''simple docstring''' import os from huggingface_hub.constants import HUGGINGFACE_HUB_CACHE, hf_cache_home __UpperCamelCase = HUGGINGFACE_HUB_CACHE __UpperCamelCase = "config.json" __UpperCamelCase = "diffusion_pytorch_model.bin" __UpperCamelCase = "diffusion_flax_model.msgpack" __UpperCamelCase = "model.onnx" __UpperCamelCase = "diffusion_pytorch_model.safetensors" __UpperCamelCase = "weights.pb" __UpperCamelCase = "https://huggingface.co" __UpperCamelCase = default_cache_path __UpperCamelCase = "diffusers_modules" __UpperCamelCase = os.getenv("HF_MODULES_CACHE", os.path.join(hf_cache_home, "modules")) __UpperCamelCase = ["fp16", "non-ema"] __UpperCamelCase = ".self_attn"
26
0
def _lowerCAmelCase ( _lowerCAmelCase ) -> bool: '''simple docstring''' __snake_case = (1 + 24 * n) ** 0.5 return ((1 + root) / 6) % 1 == 0 def _lowerCAmelCase ( _lowerCAmelCase = 5000 ) -> int: '''simple docstring''' __snake_case = [(i * (3 * i - 1)) // 2 for i in range(1 , _lowerCamelCase )] for i, pentagonal_i in enumerate(_lowerCamelCase ): for j in range(_lowerCamelCase , len(_lowerCamelCase ) ): __snake_case = pentagonal_nums[j] __snake_case = pentagonal_i + pentagonal_j __snake_case = pentagonal_j - pentagonal_i if is_pentagonal(_lowerCamelCase ) and is_pentagonal(_lowerCamelCase ): return b return -1 if __name__ == "__main__": print(f'''{solution() = }''')
371
'''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() __UpperCamelCase = logging.get_logger(__name__) def _a ( _lowerCamelCase ) -> Any: """simple docstring""" __snake_case : Union[str, Any] = MobileNetVaConfig(layer_norm_eps=0.0_01 ) if "_quant" in model_name: raise ValueError("""Quantized models are not supported.""" ) __snake_case : List[Any] = re.match(R"""^mobilenet_v1_([^_]*)_([^_]*)$""" , _lowerCamelCase ) if matches: __snake_case : Optional[Any] = float(matches[1] ) __snake_case : Union[str, Any] = int(matches[2] ) # The TensorFlow version of MobileNetV1 predicts 1001 classes instead of # the usual 1000. The first class (index 0) is "background". __snake_case : Tuple = 1001 __snake_case : Any = """imagenet-1k-id2label.json""" __snake_case : Optional[Any] = """huggingface/label-files""" __snake_case : List[Any] = json.load(open(hf_hub_download(_lowerCamelCase , _lowerCamelCase , repo_type="""dataset""" ) , """r""" ) ) __snake_case : Dict = {int(_lowerCamelCase ) + 1: v for k, v in idalabel.items()} __snake_case : List[str] = """background""" __snake_case : List[str] = idalabel __snake_case : List[Any] = {v: k for k, v in idalabel.items()} return config def _a ( ) -> Optional[Any]: """simple docstring""" __snake_case : Optional[int] = """http://images.cocodataset.org/val2017/000000039769.jpg""" __snake_case : List[Any] = Image.open(requests.get(_lowerCamelCase , stream=_lowerCamelCase ).raw ) return im @torch.no_grad() def _a ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase=False ) -> Optional[Any]: """simple docstring""" __snake_case : Optional[int] = get_mobilenet_va_config(_lowerCamelCase ) # Load 🤗 model __snake_case : Optional[Any] = MobileNetVaForImageClassification(_lowerCamelCase ).eval() # Load weights from TensorFlow checkpoint load_tf_weights_in_mobilenet_va(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) # Check outputs on an image, prepared by MobileNetV1ImageProcessor __snake_case : Optional[int] = MobileNetVaImageProcessor( crop_size={"""width""": config.image_size, """height""": config.image_size} , size={"""shortest_edge""": config.image_size + 32} , ) __snake_case : Tuple = image_processor(images=prepare_img() , return_tensors="""pt""" ) __snake_case : Optional[Any] = model(**_lowerCamelCase ) __snake_case : List[Any] = outputs.logits assert logits.shape == (1, 1001) if model_name == "mobilenet_v1_1.0_224": __snake_case : str = torch.tensor([-4.17_39, -1.12_33, 3.12_05] ) elif model_name == "mobilenet_v1_0.75_192": __snake_case : Tuple = torch.tensor([-3.94_40, -2.31_41, -0.33_33] ) else: __snake_case : List[Any] = None if expected_logits is not None: assert torch.allclose(logits[0, :3] , _lowerCamelCase , atol=1E-4 ) Path(_lowerCamelCase ).mkdir(exist_ok=_lowerCamelCase ) print(F'''Saving model {model_name} to {pytorch_dump_folder_path}''' ) model.save_pretrained(_lowerCamelCase ) print(F'''Saving image processor to {pytorch_dump_folder_path}''' ) image_processor.save_pretrained(_lowerCamelCase ) if push_to_hub: print("""Pushing to the hub...""" ) __snake_case : Optional[Any] = """google/""" + model_name image_processor.push_to_hub(_lowerCamelCase ) model.push_to_hub(_lowerCamelCase ) if __name__ == "__main__": __UpperCamelCase = 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." ) __UpperCamelCase = parser.parse_args() convert_movilevit_checkpoint( args.model_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub )
26
0
import json import os from datetime import date from pathlib import Path from tabulate import DataRow, TableFormat, tabulate lowercase_: Any = TableFormat( lineabove=None, linebelowheader=None, linebetweenrows=None, linebelow=None, headerrow=DataRow('', '|', '|'), datarow=DataRow('', '|', '|'), padding=1, with_header_hide=None, ) lowercase_: Union[str, Any] = [] lowercase_: Tuple = [] lowercase_: Dict = {'type': 'section', 'text': {'type': 'plain_text', 'text': 'No failed tests! 🤗', 'emoji': True}} lowercase_: List[Any] = [ { 'type': 'header', 'text': { 'type': 'plain_text', 'text': F"""🤗 Accelerate nightly {os.environ.get("TEST_TYPE", "")} test results""", 'emoji': True, }, } ] lowercase_: Optional[int] = 0 for log in Path().glob('*.log'): lowercase_: List[Any] = 0 with open(log, 'r') as f: for line in f: lowercase_: Any = json.loads(line) if line.get('nodeid', '') != "": lowercase_: int = line['nodeid'] if line.get('duration', None) is not None: lowercase_: Tuple = F"""{line["duration"]:.4f}""" if line.get('outcome', '') == "failed": section_num_failed += 1 failed.append([test, duration, log.name.split('_')[0]]) total_num_failed += 1 group_info.append([str(log), section_num_failed, failed]) lowercase_: List[str] = [] log.unlink() lowercase_: List[str] = '' lowercase_: Any = [] if total_num_failed > 0: for name, num_failed, failed_tests in group_info: if num_failed > 0: if num_failed == 1: message += F"*{name[1:]}: {num_failed} failed test*\n" else: message += F"*{name[1:]}: {num_failed} failed tests*\n" lowercase_: List[Any] = [] lowercase_: Dict = {} for test in failed_tests: lowercase_: Dict = test[0].split('::') lowercase_: Tuple = data[0].split('/')[-1] if data[0] not in filesafailed: lowercase_: str = [data[1:]] else: filesafailed[data[0]] += [data[1:]] failed_table.append(data) lowercase_: Optional[int] = [test[0] for test in failed_table] lowercase_: Optional[int] = list(set(files)) # Count number of instances in failed_tests lowercase_: List[str] = [] for file in individual_files: table.append([file, len(filesafailed[file])]) lowercase_: Union[str, Any] = tabulate( table, headers=['Test Location', 'Num Failed'], tablefmt=hf_table_format, stralign='right', ) message += F"\n```\n{failed_table}\n```" all_filesafailed.append(filesafailed) if len(message) > 30_00: lowercase_: Tuple = 'Too many failed tests, please see the full report in the Action results.' lowercase_: List[Any] = len(err) + 10 lowercase_: Tuple = message[: 30_00 - offset] + F"""\n...\n```\n{err}""" print(F"""### {message}""") else: lowercase_: Optional[Any] = 'No failed tests! 🤗' print(F"""## {message}""") payload.append(no_error_payload) if os.environ.get('TEST_TYPE', '') != "": from slack_sdk import WebClient lowercase_: str = WebClient(token=os.environ['SLACK_API_TOKEN']) if message != "No failed tests! 🤗": lowercase_: Tuple = { 'type': 'section', 'text': { 'type': 'mrkdwn', 'text': message, }, } payload.append(md_report) lowercase_: int = { 'type': 'section', 'text': { 'type': 'mrkdwn', 'text': '*For more details:*', }, 'accessory': { 'type': 'button', 'text': { 'type': 'plain_text', 'text': 'Check Action results', 'emoji': True, }, 'url': F"""https://github.com/{os.environ["GITHUB_REPOSITORY"]}/actions/runs/{os.environ["GITHUB_RUN_ID"]}""", }, } payload.append(action_button) lowercase_: Optional[Any] = { 'type': 'context', 'elements': [ { 'type': 'plain_text', 'text': F"""Nightly {os.environ.get("TEST_TYPE")} test results for {date.today()}""", } ], } payload.append(date_report) lowercase_: int = client.chat_postMessage(channel='#accelerate-ci-daily', text=message, blocks=payload) lowercase_: List[Any] = response.data['ts'] for failed_file in all_filesafailed: for test_location, test_failures in failed_file.items(): # Keep only the first instance of the test name lowercase_: str = '' for i, row in enumerate(test_failures): if row[0] != test_class: lowercase_: List[Any] = row[0] else: lowercase_: int = '' lowercase_: Optional[Any] = { 'type': 'section', 'text': { 'type': 'mrkdwn', 'text': F"""Test location: {test_location}\n```\n{tabulate(test_failures, headers=["Class", "Test"], tablefmt=hf_table_format, stralign="right")}\n```""", }, } client.chat_postMessage( channel='#accelerate-ci-daily', thread_ts=ts, blocks=[payload], )
648
'''simple docstring''' from sklearn.metrics import recall_score import datasets __UpperCamelCase = "\nRecall is the fraction of the positive examples that were correctly labeled by the model as positive. It can be computed with the equation:\nRecall = TP / (TP + FN)\nWhere TP is the true positives and FN is the false negatives.\n" __UpperCamelCase = "\nArgs:\n- **predictions** (`list` of `int`): The predicted labels.\n- **references** (`list` of `int`): The ground truth labels.\n- **labels** (`list` of `int`): The set of labels to include when `average` is not set to `binary`, and their order when average is `None`. Labels present in the data can be excluded in this input, for example to calculate a multiclass average ignoring a majority negative class, while labels not present in the data will result in 0 components in a macro average. For multilabel targets, labels are column indices. By default, all labels in y_true and y_pred are used in sorted order. Defaults to None.\n- **pos_label** (`int`): The class label to use as the 'positive class' when calculating the recall. Defaults to `1`.\n- **average** (`string`): This parameter is required for multiclass/multilabel targets. If None, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data. Defaults to `'binary'`.\n - `'binary'`: Only report results for the class specified by `pos_label`. This is applicable only if the target labels and predictions are binary.\n - `'micro'`: Calculate metrics globally by counting the total true positives, false negatives, and false positives.\n - `'macro'`: Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account.\n - `'weighted'`: Calculate metrics for each label, and find their average weighted by support (the number of true instances for each label). This alters `'macro'` to account for label imbalance. Note that it can result in an F-score that is not between precision and recall.\n - `'samples'`: Calculate metrics for each instance, and find their average (only meaningful for multilabel classification).\n- **sample_weight** (`list` of `float`): Sample weights Defaults to `None`.\n- **zero_division** (): Sets the value to return when there is a zero division. Defaults to .\n - `'warn'`: If there is a zero division, the return value is `0`, but warnings are also raised.\n - `0`: If there is a zero division, the return value is `0`.\n - `1`: If there is a zero division, the return value is `1`.\n\nReturns:\n- **recall** (`float`, or `array` of `float`): Either the general recall score, or the recall scores for individual classes, depending on the values input to `labels` and `average`. Minimum possible value is 0. Maximum possible value is 1. A higher recall means that more of the positive examples have been labeled correctly. Therefore, a higher recall is generally considered better.\n\nExamples:\n\n Example 1-A simple example with some errors\n >>> recall_metric = datasets.load_metric('recall')\n >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1])\n >>> print(results)\n {'recall': 0.6666666666666666}\n\n Example 2-The same example as Example 1, but with `pos_label=0` instead of the default `pos_label=1`.\n >>> recall_metric = datasets.load_metric('recall')\n >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1], pos_label=0)\n >>> print(results)\n {'recall': 0.5}\n\n Example 3-The same example as Example 1, but with `sample_weight` included.\n >>> recall_metric = datasets.load_metric('recall')\n >>> sample_weight = [0.9, 0.2, 0.9, 0.3, 0.8]\n >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1], sample_weight=sample_weight)\n >>> print(results)\n {'recall': 0.55}\n\n Example 4-A multiclass example, using different averages.\n >>> recall_metric = datasets.load_metric('recall')\n >>> predictions = [0, 2, 1, 0, 0, 1]\n >>> references = [0, 1, 2, 0, 1, 2]\n >>> results = recall_metric.compute(predictions=predictions, references=references, average='macro')\n >>> print(results)\n {'recall': 0.3333333333333333}\n >>> results = recall_metric.compute(predictions=predictions, references=references, average='micro')\n >>> print(results)\n {'recall': 0.3333333333333333}\n >>> results = recall_metric.compute(predictions=predictions, references=references, average='weighted')\n >>> print(results)\n {'recall': 0.3333333333333333}\n >>> results = recall_metric.compute(predictions=predictions, references=references, average=None)\n >>> print(results)\n {'recall': array([1., 0., 0.])}\n" __UpperCamelCase = "\n@article{scikit-learn, title={Scikit-learn: Machine Learning in {P}ython}, author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V. and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P. and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.}, journal={Journal of Machine Learning Research}, volume={12}, pages={2825--2830}, year={2011}\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class _A ( datasets.Metric ): def lowercase__ ( self : Optional[int] ) -> Any: """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": datasets.Sequence(datasets.Value("""int32""" ) ), """references""": datasets.Sequence(datasets.Value("""int32""" ) ), } if self.config_name == """multilabel""" else { """predictions""": datasets.Value("""int32""" ), """references""": datasets.Value("""int32""" ), } ) , reference_urls=["""https://scikit-learn.org/stable/modules/generated/sklearn.metrics.recall_score.html"""] , ) def lowercase__ ( self : Tuple , __magic_name__ : int , __magic_name__ : Union[str, Any] , __magic_name__ : Any=None , __magic_name__ : Optional[Any]=1 , __magic_name__ : List[str]="binary" , __magic_name__ : Tuple=None , __magic_name__ : Dict="warn" , ) -> Any: """simple docstring""" __snake_case : Tuple = recall_score( __magic_name__ , __magic_name__ , labels=__magic_name__ , pos_label=__magic_name__ , average=__magic_name__ , sample_weight=__magic_name__ , zero_division=__magic_name__ , ) return {"recall": float(__magic_name__ ) if score.size == 1 else score}
26
0
"""simple docstring""" from random import shuffle import tensorflow as tf from numpy import array def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE__, SCREAMING_SNAKE_CASE__ ) -> str: a_ : str = int(_lowerCamelCase ) assert noofclusters < len(_lowerCamelCase ) # Find out the dimensionality a_ : Tuple = len(vectors[0] ) # Will help select random centroids from among the available vectors a_ : Dict = list(range(len(_lowerCamelCase ) ) ) shuffle(_lowerCamelCase ) # GRAPH OF COMPUTATION # We initialize a new graph and set it as the default during each run # of this algorithm. This ensures that as this function is called # multiple times, the default graph doesn't keep getting crowded with # unused ops and Variables from previous function calls. a_ : Dict = tf.Graph() with graph.as_default(): # SESSION OF COMPUTATION a_ : List[Any] = tf.Session() ##CONSTRUCTING THE ELEMENTS OF COMPUTATION ##First lets ensure we have a Variable vector for each centroid, ##initialized to one of the vectors from the available data points a_ : List[Any] = [ tf.Variable(vectors[vector_indices[i]] ) for i in range(_lowerCamelCase ) ] ##These nodes will assign the centroid Variables the appropriate ##values a_ : Dict = tf.placeholder("float64", [dim] ) a_ : Optional[int] = [] for centroid in centroids: cent_assigns.append(tf.assign(_lowerCamelCase, _lowerCamelCase ) ) ##Variables for cluster assignments of individual vectors(initialized ##to 0 at first) a_ : Dict = [tf.Variable(0 ) for i in range(len(_lowerCamelCase ) )] ##These nodes will assign an assignment Variable the appropriate ##value a_ : Optional[Any] = tf.placeholder("int32" ) a_ : Any = [] for assignment in assignments: cluster_assigns.append(tf.assign(_lowerCamelCase, _lowerCamelCase ) ) ##Now lets construct the node that will compute the mean # The placeholder for the input a_ : Union[str, Any] = tf.placeholder("float", [None, dim] ) # The Node/op takes the input and computes a mean along the 0th # dimension, i.e. the list of input vectors a_ : Optional[int] = tf.reduce_mean(_lowerCamelCase, 0 ) ##Node for computing Euclidean distances # Placeholders for input a_ : int = tf.placeholder("float", [dim] ) a_ : Optional[int] = tf.placeholder("float", [dim] ) a_ : int = tf.sqrt(tf.reduce_sum(tf.pow(tf.sub(_lowerCamelCase, _lowerCamelCase ), 2 ) ) ) ##This node will figure out which cluster to assign a vector to, ##based on Euclidean distances of the vector from the centroids. # Placeholder for input a_ : Optional[Any] = tf.placeholder("float", [noofclusters] ) a_ : Optional[int] = tf.argmin(_lowerCamelCase, 0 ) ##INITIALIZING STATE VARIABLES ##This will help initialization of all Variables defined with respect ##to the graph. The Variable-initializer should be defined after ##all the Variables have been constructed, so that each of them ##will be included in the initialization. a_ : str = tf.initialize_all_variables() # Initialize all variables sess.run(_lowerCamelCase ) ##CLUSTERING ITERATIONS # Now perform the Expectation-Maximization steps of K-Means clustering # iterations. To keep things simple, we will only do a set number of # iterations, instead of using a Stopping Criterion. a_ : int = 100 for _ in range(_lowerCamelCase ): ##EXPECTATION STEP ##Based on the centroid locations till last iteration, compute ##the _expected_ centroid assignments. # Iterate over each vector for vector_n in range(len(_lowerCamelCase ) ): a_ : Optional[int] = vectors[vector_n] # Compute Euclidean distance between this vector and each # centroid. Remember that this list cannot be named #'centroid_distances', since that is the input to the # cluster assignment node. a_ : Union[str, Any] = [ sess.run(_lowerCamelCase, feed_dict={va: vect, va: sess.run(_lowerCamelCase )} ) for centroid in centroids ] # Now use the cluster assignment node, with the distances # as the input a_ : Dict = sess.run( _lowerCamelCase, feed_dict={centroid_distances: distances} ) # Now assign the value to the appropriate state variable sess.run( cluster_assigns[vector_n], feed_dict={assignment_value: assignment} ) ##MAXIMIZATION STEP # Based on the expected state computed from the Expectation Step, # compute the locations of the centroids so as to maximize the # overall objective of minimizing within-cluster Sum-of-Squares for cluster_n in range(_lowerCamelCase ): # Collect all the vectors assigned to this cluster a_ : str = [ vectors[i] for i in range(len(_lowerCamelCase ) ) if sess.run(assignments[i] ) == cluster_n ] # Compute new centroid location a_ : str = sess.run( _lowerCamelCase, feed_dict={mean_input: array(_lowerCamelCase )} ) # Assign value to appropriate variable sess.run( cent_assigns[cluster_n], feed_dict={centroid_value: new_location} ) # Return centroids and assignments a_ : Tuple = sess.run(_lowerCamelCase ) a_ : str = sess.run(_lowerCamelCase ) return centroids, assignments
237
'''simple docstring''' from sklearn.metrics import matthews_corrcoef import datasets __UpperCamelCase = "\nCompute the Matthews correlation coefficient (MCC)\n\nThe Matthews correlation coefficient is used in machine learning as a\nmeasure of the quality of binary and multiclass classifications. It takes\ninto account true and false positives and negatives and is generally\nregarded as a balanced measure which can be used even if the classes are of\nvery different sizes. The MCC is in essence a correlation coefficient value\nbetween -1 and +1. A coefficient of +1 represents a perfect prediction, 0\nan average random prediction and -1 an inverse prediction. The statistic\nis also known as the phi coefficient. [source: Wikipedia]\n" __UpperCamelCase = "\nArgs:\n predictions (list of int): Predicted labels, as returned by a model.\n references (list of int): Ground truth labels.\n sample_weight (list of int, float, or bool): Sample weights. Defaults to `None`.\nReturns:\n matthews_correlation (dict containing float): Matthews correlation.\nExamples:\n Example 1, a basic example with only predictions and references as inputs:\n >>> matthews_metric = datasets.load_metric(\"matthews_correlation\")\n >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],\n ... predictions=[1, 2, 2, 0, 3, 3])\n >>> print(round(results['matthews_correlation'], 2))\n 0.54\n\n Example 2, the same example as above, but also including sample weights:\n >>> matthews_metric = datasets.load_metric(\"matthews_correlation\")\n >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],\n ... predictions=[1, 2, 2, 0, 3, 3],\n ... sample_weight=[0.5, 3, 1, 1, 1, 2])\n >>> print(round(results['matthews_correlation'], 2))\n 0.1\n\n Example 3, the same example as above, but with sample weights that cause a negative correlation:\n >>> matthews_metric = datasets.load_metric(\"matthews_correlation\")\n >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],\n ... predictions=[1, 2, 2, 0, 3, 3],\n ... sample_weight=[0.5, 1, 0, 0, 0, 1])\n >>> print(round(results['matthews_correlation'], 2))\n -0.25\n" __UpperCamelCase = "\\n@article{scikit-learn,\n title={Scikit-learn: Machine Learning in {P}ython},\n author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V.\n and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P.\n and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and\n Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.},\n journal={Journal of Machine Learning Research},\n volume={12},\n pages={2825--2830},\n year={2011}\n}\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class _A ( datasets.Metric ): def lowercase__ ( self : Tuple ) -> Dict: """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": datasets.Value("""int32""" ), """references""": datasets.Value("""int32""" ), } ) , reference_urls=[ """https://scikit-learn.org/stable/modules/generated/sklearn.metrics.matthews_corrcoef.html""" ] , ) def lowercase__ ( self : List[Any] , __magic_name__ : Tuple , __magic_name__ : List[Any] , __magic_name__ : Union[str, Any]=None ) -> Optional[int]: """simple docstring""" return { "matthews_correlation": float(matthews_corrcoef(__magic_name__ , __magic_name__ , sample_weight=__magic_name__ ) ), }
26
0
"""simple docstring""" class a__ : def __init__( self : str) -> List[Any]: """simple docstring""" _lowerCAmelCase:Optional[Any] = 0 _lowerCAmelCase:List[Any] = 0 _lowerCAmelCase:List[Any] = {} def __UpperCamelCase ( self : List[str] ,a__ : List[Any]) -> Dict: """simple docstring""" if vertex not in self.adjacency: _lowerCAmelCase:List[str] = {} self.num_vertices += 1 def __UpperCamelCase ( self : List[Any] ,a__ : Optional[Any] ,a__ : Optional[Any] ,a__ : int) -> int: """simple docstring""" self.add_vertex(a__) self.add_vertex(a__) if head == tail: return _lowerCAmelCase:Tuple = weight _lowerCAmelCase:Optional[Any] = weight def __UpperCamelCase ( self : Optional[int]) -> Optional[Any]: """simple docstring""" _lowerCAmelCase:Any = self.get_edges() for edge in edges: _lowerCAmelCase:Union[str, Any] = edge edges.remove((tail, head, weight)) for i in range(len(a__)): _lowerCAmelCase:Optional[int] = list(edges[i]) edges.sort(key=lambda a__: e[2]) for i in range(len(a__) - 1): if edges[i][2] >= edges[i + 1][2]: _lowerCAmelCase:Optional[Any] = edges[i][2] + 1 for edge in edges: _lowerCAmelCase:Union[str, Any] = edge _lowerCAmelCase:List[Any] = weight _lowerCAmelCase:Dict = weight def __str__( self : Optional[Any]) -> Optional[int]: """simple docstring""" _lowerCAmelCase:int = """""" for tail in self.adjacency: for head in self.adjacency[tail]: _lowerCAmelCase:Union[str, Any] = self.adjacency[head][tail] string += F'{head} -> {tail} == {weight}\n' return string.rstrip('''\n''') def __UpperCamelCase ( self : int) -> List[str]: """simple docstring""" _lowerCAmelCase:int = [] for tail in self.adjacency: for head in self.adjacency[tail]: output.append((tail, head, self.adjacency[head][tail])) return output def __UpperCamelCase ( self : Union[str, Any]) -> str: """simple docstring""" return self.adjacency.keys() @staticmethod def __UpperCamelCase ( a__ : int=None ,a__ : List[str]=None) -> Tuple: """simple docstring""" _lowerCAmelCase:Dict = Graph() if vertices is None: _lowerCAmelCase:Tuple = [] if edges is None: _lowerCAmelCase:Optional[Any] = [] for vertex in vertices: g.add_vertex(a__) for edge in edges: g.add_edge(*a__) return g class a__ : def __init__( self : int) -> Tuple: """simple docstring""" _lowerCAmelCase:Optional[int] = {} _lowerCAmelCase:Union[str, Any] = {} def __len__( self : Tuple) -> Optional[int]: """simple docstring""" return len(self.parent) def __UpperCamelCase ( self : str ,a__ : int) -> Optional[Any]: """simple docstring""" if item in self.parent: return self.find(a__) _lowerCAmelCase:Any = item _lowerCAmelCase:str = 0 return item def __UpperCamelCase ( self : List[Any] ,a__ : str) -> List[Any]: """simple docstring""" if item not in self.parent: return self.make_set(a__) if item != self.parent[item]: _lowerCAmelCase:int = self.find(self.parent[item]) return self.parent[item] def __UpperCamelCase ( self : List[str] ,a__ : Dict ,a__ : Optional[int]) -> List[str]: """simple docstring""" _lowerCAmelCase:List[str] = self.find(a__) _lowerCAmelCase:List[str] = self.find(a__) if roota == roota: return roota if self.rank[roota] > self.rank[roota]: _lowerCAmelCase:int = roota return roota if self.rank[roota] < self.rank[roota]: _lowerCAmelCase:Union[str, Any] = roota return roota if self.rank[roota] == self.rank[roota]: self.rank[roota] += 1 _lowerCAmelCase:Union[str, Any] = roota return roota return None @staticmethod def __UpperCamelCase ( a__ : int) -> Union[str, Any]: """simple docstring""" _lowerCAmelCase:Any = graph.num_vertices _lowerCAmelCase:Dict = Graph.UnionFind() _lowerCAmelCase:Dict = [] while num_components > 1: _lowerCAmelCase:Optional[int] = {} for vertex in graph.get_vertices(): _lowerCAmelCase:Union[str, Any] = -1 _lowerCAmelCase:Optional[Any] = graph.get_edges() for edge in edges: _lowerCAmelCase:Union[str, Any] = edge edges.remove((tail, head, weight)) for edge in edges: _lowerCAmelCase:Optional[Any] = edge _lowerCAmelCase:List[Any] = union_find.find(a__) _lowerCAmelCase:Union[str, Any] = union_find.find(a__) if seta != seta: if cheap_edge[seta] == -1 or cheap_edge[seta][2] > weight: _lowerCAmelCase:str = [head, tail, weight] if cheap_edge[seta] == -1 or cheap_edge[seta][2] > weight: _lowerCAmelCase:Optional[int] = [head, tail, weight] for vertex in cheap_edge: if cheap_edge[vertex] != -1: _lowerCAmelCase:Any = cheap_edge[vertex] if union_find.find(a__) != union_find.find(a__): union_find.union(a__ ,a__) mst_edges.append(cheap_edge[vertex]) _lowerCAmelCase:int = num_components - 1 _lowerCAmelCase:Optional[int] = Graph.build(edges=a__) return mst
227
'''simple docstring''' import json import os from pathlib import Path import pytest from datasets.download.download_config import DownloadConfig from datasets.download.download_manager import DownloadManager from datasets.utils.file_utils import hash_url_to_filename __UpperCamelCase = "http://www.mocksite.com/file1.txt" __UpperCamelCase = "\"text\": [\"foo\", \"foo\"]" __UpperCamelCase = "6d8ce9aa78a471c7477201efbeabd3bb01ac2e7d100a6dc024ba1608361f90a8" class _A : lowercase__: str = 200 lowercase__: List[str] = {'''Content-Length''': '''100'''} lowercase__: Union[str, Any] = {} def lowercase__ ( self : Any , **__magic_name__ : List[Any] ) -> Dict: """simple docstring""" return [bytes(__magic_name__ , """utf-8""" )] def _a ( *_lowerCamelCase , **_lowerCamelCase ) -> List[str]: """simple docstring""" return MockResponse() @pytest.mark.parametrize("""urls_type""" , [str, list, dict] ) def _a ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) -> List[str]: """simple docstring""" import requests monkeypatch.setattr(_lowerCamelCase , """request""" , _lowerCamelCase ) __snake_case : Union[str, Any] = URL if issubclass(_lowerCamelCase , _lowerCamelCase ): __snake_case : str = url elif issubclass(_lowerCamelCase , _lowerCamelCase ): __snake_case : Dict = [url] elif issubclass(_lowerCamelCase , _lowerCamelCase ): __snake_case : Union[str, Any] = {"""train""": url} __snake_case : Dict = """dummy""" __snake_case : List[str] = """downloads""" __snake_case : List[Any] = tmp_path __snake_case : List[Any] = DownloadConfig( cache_dir=os.path.join(_lowerCamelCase , _lowerCamelCase ) , use_etag=_lowerCamelCase , ) __snake_case : List[str] = DownloadManager(dataset_name=_lowerCamelCase , download_config=_lowerCamelCase ) __snake_case : int = dl_manager.download(_lowerCamelCase ) __snake_case : Tuple = urls for downloaded_paths in [downloaded_paths]: if isinstance(_lowerCamelCase , _lowerCamelCase ): __snake_case : Any = [downloaded_paths] __snake_case : List[Any] = [urls] elif isinstance(_lowerCamelCase , _lowerCamelCase ): assert "train" in downloaded_paths.keys() __snake_case : Tuple = downloaded_paths.values() __snake_case : Optional[int] = urls.values() assert downloaded_paths for downloaded_path, input_url in zip(_lowerCamelCase , _lowerCamelCase ): assert downloaded_path == dl_manager.downloaded_paths[input_url] __snake_case : List[str] = Path(_lowerCamelCase ) __snake_case : Any = downloaded_path.parts assert parts[-1] == HASH assert parts[-2] == cache_subdir assert downloaded_path.exists() __snake_case : Union[str, Any] = downloaded_path.read_text() assert content == CONTENT __snake_case : List[str] = downloaded_path.with_suffix(""".json""" ) assert metadata_downloaded_path.exists() __snake_case : Union[str, Any] = json.loads(metadata_downloaded_path.read_text() ) assert metadata_content == {"url": URL, "etag": None} @pytest.mark.parametrize("""paths_type""" , [str, list, dict] ) def _a ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) -> List[Any]: """simple docstring""" __snake_case : Any = str(_lowerCamelCase ) if issubclass(_lowerCamelCase , _lowerCamelCase ): __snake_case : Optional[int] = filename elif issubclass(_lowerCamelCase , _lowerCamelCase ): __snake_case : Tuple = [filename] elif issubclass(_lowerCamelCase , _lowerCamelCase ): __snake_case : Dict = {"""train""": filename} __snake_case : Optional[Any] = """dummy""" __snake_case : List[Any] = xz_file.parent __snake_case : int = """extracted""" __snake_case : Dict = DownloadConfig( cache_dir=_lowerCamelCase , use_etag=_lowerCamelCase , ) __snake_case : List[str] = DownloadManager(dataset_name=_lowerCamelCase , download_config=_lowerCamelCase ) __snake_case : Optional[Any] = dl_manager.extract(_lowerCamelCase ) __snake_case : Union[str, Any] = paths for extracted_paths in [extracted_paths]: if isinstance(_lowerCamelCase , _lowerCamelCase ): __snake_case : Dict = [extracted_paths] __snake_case : int = [paths] elif isinstance(_lowerCamelCase , _lowerCamelCase ): assert "train" in extracted_paths.keys() __snake_case : int = extracted_paths.values() __snake_case : int = paths.values() assert extracted_paths for extracted_path, input_path in zip(_lowerCamelCase , _lowerCamelCase ): assert extracted_path == dl_manager.extracted_paths[input_path] __snake_case : Any = Path(_lowerCamelCase ) __snake_case : str = extracted_path.parts assert parts[-1] == hash_url_to_filename(_lowerCamelCase , etag=_lowerCamelCase ) assert parts[-2] == extracted_subdir assert extracted_path.exists() __snake_case : Optional[int] = extracted_path.read_text() __snake_case : str = text_file.read_text() assert extracted_file_content == expected_file_content def _a ( _lowerCamelCase , _lowerCamelCase ) -> Optional[int]: """simple docstring""" assert path.endswith(""".jsonl""" ) for num_items, line in enumerate(_lowerCamelCase , start=1 ): __snake_case : Tuple = json.loads(line.decode("""utf-8""" ) ) assert item.keys() == {"col_1", "col_2", "col_3"} assert num_items == 4 @pytest.mark.parametrize("""archive_jsonl""" , ["""tar_jsonl_path""", """zip_jsonl_path"""] ) def _a ( _lowerCamelCase , _lowerCamelCase ) -> Optional[int]: """simple docstring""" __snake_case : Any = request.getfixturevalue(_lowerCamelCase ) __snake_case : str = DownloadManager() for num_jsonl, (path, file) in enumerate(dl_manager.iter_archive(_lowerCamelCase ) , start=1 ): _test_jsonl(_lowerCamelCase , _lowerCamelCase ) assert num_jsonl == 2 @pytest.mark.parametrize("""archive_nested_jsonl""" , ["""tar_nested_jsonl_path""", """zip_nested_jsonl_path"""] ) def _a ( _lowerCamelCase , _lowerCamelCase ) -> List[str]: """simple docstring""" __snake_case : int = request.getfixturevalue(_lowerCamelCase ) __snake_case : List[str] = DownloadManager() for num_tar, (path, file) in enumerate(dl_manager.iter_archive(_lowerCamelCase ) , start=1 ): for num_jsonl, (subpath, subfile) in enumerate(dl_manager.iter_archive(_lowerCamelCase ) , start=1 ): _test_jsonl(_lowerCamelCase , _lowerCamelCase ) assert num_tar == 1 assert num_jsonl == 2 def _a ( _lowerCamelCase ) -> Any: """simple docstring""" __snake_case : List[str] = DownloadManager() for num_file, file in enumerate(dl_manager.iter_files(_lowerCamelCase ) , start=1 ): assert os.path.basename(_lowerCamelCase ) == ("test.txt" if num_file == 1 else "train.txt") assert num_file == 2
26
0
def _lowerCamelCase ( __lowerCamelCase ) -> Any: '''simple docstring''' UpperCAmelCase__ : List[Any] = [0] * len(_lowerCamelCase ) UpperCAmelCase__ : List[Any] = [] UpperCAmelCase__ : List[Any] = [1] * len(_lowerCamelCase ) for values in graph.values(): for i in values: indegree[i] += 1 for i in range(len(_lowerCamelCase ) ): if indegree[i] == 0: queue.append(_lowerCamelCase ) while queue: UpperCAmelCase__ : Dict = queue.pop(0 ) for x in graph[vertex]: indegree[x] -= 1 if long_dist[vertex] + 1 > long_dist[x]: UpperCAmelCase__ : int = long_dist[vertex] + 1 if indegree[x] == 0: queue.append(_lowerCamelCase ) print(max(_lowerCamelCase ) ) # Adjacency list of Graph SCREAMING_SNAKE_CASE__ : Any = {0: [2, 3, 4], 1: [2, 7], 2: [5], 3: [5, 7], 4: [7], 5: [6], 6: [7], 7: []} longest_distance(graph)
79
'''simple docstring''' def _a ( _lowerCamelCase = 100 ) -> int: """simple docstring""" __snake_case : Any = n * (n + 1) * (2 * n + 1) / 6 __snake_case : List[Any] = (n * (n + 1) / 2) ** 2 return int(square_of_sum - sum_of_squares ) if __name__ == "__main__": print(f"""{solution() = }""")
26
0
import argparse from collections import OrderedDict from pathlib import Path import requests import torch from PIL import Image from transformers import GLPNConfig, GLPNForDepthEstimation, GLPNImageProcessor from transformers.utils import logging logging.set_verbosity_info() snake_case_ : Optional[int] = logging.get_logger(__name__) def __a ( __UpperCAmelCase : Tuple ) -> Tuple: """simple docstring""" lowerCamelCase_ : Union[str, Any] = OrderedDict() for key, value in state_dict.items(): if key.startswith("module.encoder" ): lowerCamelCase_ : Optional[int] = key.replace("module.encoder" , "glpn.encoder" ) if key.startswith("module.decoder" ): lowerCamelCase_ : str = key.replace("module.decoder" , "decoder.stages" ) if "patch_embed" in key: # replace for example patch_embed1 by patch_embeddings.0 lowerCamelCase_ : List[Any] = key[key.find("patch_embed" ) + len("patch_embed" )] lowerCamelCase_ : List[Any] = key.replace(f"patch_embed{idx}" , f"patch_embeddings.{int(_lowerCamelCase )-1}" ) if "norm" in key: lowerCamelCase_ : Union[str, Any] = key.replace("norm" , "layer_norm" ) if "glpn.encoder.layer_norm" in key: # replace for example layer_norm1 by layer_norm.0 lowerCamelCase_ : int = key[key.find("glpn.encoder.layer_norm" ) + len("glpn.encoder.layer_norm" )] lowerCamelCase_ : List[Any] = key.replace(f"layer_norm{idx}" , f"layer_norm.{int(_lowerCamelCase )-1}" ) if "layer_norm1" in key: lowerCamelCase_ : Optional[Any] = key.replace("layer_norm1" , "layer_norm_1" ) if "layer_norm2" in key: lowerCamelCase_ : Any = key.replace("layer_norm2" , "layer_norm_2" ) if "block" in key: # replace for example block1 by block.0 lowerCamelCase_ : Optional[int] = key[key.find("block" ) + len("block" )] lowerCamelCase_ : Optional[Any] = key.replace(f"block{idx}" , f"block.{int(_lowerCamelCase )-1}" ) if "attn.q" in key: lowerCamelCase_ : List[str] = key.replace("attn.q" , "attention.self.query" ) if "attn.proj" in key: lowerCamelCase_ : Union[str, Any] = key.replace("attn.proj" , "attention.output.dense" ) if "attn" in key: lowerCamelCase_ : List[Any] = key.replace("attn" , "attention.self" ) if "fc1" in key: lowerCamelCase_ : str = key.replace("fc1" , "dense1" ) if "fc2" in key: lowerCamelCase_ : Union[str, Any] = key.replace("fc2" , "dense2" ) if "linear_pred" in key: lowerCamelCase_ : List[str] = key.replace("linear_pred" , "classifier" ) if "linear_fuse" in key: lowerCamelCase_ : Any = key.replace("linear_fuse.conv" , "linear_fuse" ) lowerCamelCase_ : Any = key.replace("linear_fuse.bn" , "batch_norm" ) if "linear_c" in key: # replace for example linear_c4 by linear_c.3 lowerCamelCase_ : str = key[key.find("linear_c" ) + len("linear_c" )] lowerCamelCase_ : List[Any] = key.replace(f"linear_c{idx}" , f"linear_c.{int(_lowerCamelCase )-1}" ) if "bot_conv" in key: lowerCamelCase_ : Dict = key.replace("bot_conv" , "0.convolution" ) if "skip_conv1" in key: lowerCamelCase_ : List[str] = key.replace("skip_conv1" , "1.convolution" ) if "skip_conv2" in key: lowerCamelCase_ : Optional[int] = key.replace("skip_conv2" , "2.convolution" ) if "fusion1" in key: lowerCamelCase_ : Union[str, Any] = key.replace("fusion1" , "1.fusion" ) if "fusion2" in key: lowerCamelCase_ : List[Any] = key.replace("fusion2" , "2.fusion" ) if "fusion3" in key: lowerCamelCase_ : List[Any] = key.replace("fusion3" , "3.fusion" ) if "fusion" in key and "conv" in key: lowerCamelCase_ : Dict = key.replace("conv" , "convolutional_layer" ) if key.startswith("module.last_layer_depth" ): lowerCamelCase_ : Union[str, Any] = key.replace("module.last_layer_depth" , "head.head" ) lowerCamelCase_ : int = value return new_state_dict def __a ( __UpperCAmelCase : int , __UpperCAmelCase : List[str] ) -> Any: """simple docstring""" for i in range(config.num_encoder_blocks ): for j in range(config.depths[i] ): # read in weights + bias of keys and values (which is a single matrix in the original implementation) lowerCamelCase_ : Optional[int] = state_dict.pop(f"glpn.encoder.block.{i}.{j}.attention.self.kv.weight" ) lowerCamelCase_ : int = state_dict.pop(f"glpn.encoder.block.{i}.{j}.attention.self.kv.bias" ) # next, add keys and values (in that order) to the state dict lowerCamelCase_ : str = kv_weight[ : config.hidden_sizes[i], : ] lowerCamelCase_ : Dict = kv_bias[: config.hidden_sizes[i]] lowerCamelCase_ : List[Any] = kv_weight[ config.hidden_sizes[i] :, : ] lowerCamelCase_ : List[str] = kv_bias[config.hidden_sizes[i] :] def __a ( ) -> Union[str, Any]: """simple docstring""" lowerCamelCase_ : Optional[int] = """http://images.cocodataset.org/val2017/000000039769.jpg""" lowerCamelCase_ : str = Image.open(requests.get(_lowerCamelCase , stream=_lowerCamelCase ).raw ) return image @torch.no_grad() def __a ( __UpperCAmelCase : Optional[int] , __UpperCAmelCase : str , __UpperCAmelCase : Optional[int]=False , __UpperCAmelCase : Tuple=None ) -> List[Any]: """simple docstring""" lowerCamelCase_ : Optional[Any] = GLPNConfig(hidden_sizes=[64, 128, 320, 512] , decoder_hidden_size=64 , depths=[3, 8, 27, 3] ) # load image processor (only resize + rescale) lowerCamelCase_ : List[Any] = GLPNImageProcessor() # prepare image lowerCamelCase_ : str = prepare_img() lowerCamelCase_ : str = image_processor(images=_lowerCamelCase , return_tensors="pt" ).pixel_values logger.info("Converting model..." ) # load original state dict lowerCamelCase_ : Union[str, Any] = torch.load(_lowerCamelCase , map_location=torch.device("cpu" ) ) # rename keys lowerCamelCase_ : Any = rename_keys(_lowerCamelCase ) # key and value matrices need special treatment read_in_k_v(_lowerCamelCase , _lowerCamelCase ) # create HuggingFace model and load state dict lowerCamelCase_ : Any = GLPNForDepthEstimation(_lowerCamelCase ) model.load_state_dict(_lowerCamelCase ) model.eval() # forward pass lowerCamelCase_ : Optional[Any] = model(_lowerCamelCase ) lowerCamelCase_ : Any = outputs.predicted_depth # verify output if model_name is not None: if "nyu" in model_name: lowerCamelCase_ : Any = torch.tensor( [[4.4_1_4_7, 4.0_8_7_3, 4.0_6_7_3], [3.7_8_9_0, 3.2_8_8_1, 3.1_5_2_5], [3.7_6_7_4, 3.5_4_2_3, 3.4_9_1_3]] ) elif "kitti" in model_name: lowerCamelCase_ : Tuple = torch.tensor( [[3.4_2_9_1, 2.7_8_6_5, 2.5_1_5_1], [3.2_8_4_1, 2.7_0_2_1, 2.3_5_0_2], [3.1_1_4_7, 2.4_6_2_5, 2.2_4_8_1]] ) else: raise ValueError(f"Unknown model name: {model_name}" ) lowerCamelCase_ : List[Any] = torch.Size([1, 480, 640] ) assert predicted_depth.shape == expected_shape assert torch.allclose(predicted_depth[0, :3, :3] , _lowerCamelCase , atol=1e-4 ) print("Looks ok!" ) # finally, push to hub if required if push_to_hub: logger.info("Pushing model and image processor to the hub..." ) model.push_to_hub( repo_path_or_name=Path(_lowerCamelCase , _lowerCamelCase ) , organization="nielsr" , commit_message="Add model" , use_temp_dir=_lowerCamelCase , ) image_processor.push_to_hub( repo_path_or_name=Path(_lowerCamelCase , _lowerCamelCase ) , organization="nielsr" , commit_message="Add image processor" , use_temp_dir=_lowerCamelCase , ) if __name__ == "__main__": snake_case_ : Tuple = argparse.ArgumentParser() parser.add_argument( "--checkpoint_path", default=None, type=str, help="Path to the original PyTorch checkpoint (.pth file).", ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, help="Path to the folder to output PyTorch model." ) parser.add_argument( "--push_to_hub", action="store_true", help="Whether to upload the model to the HuggingFace hub." ) parser.add_argument( "--model_name", default="glpn-kitti", type=str, help="Name of the model in case you're pushing to the hub.", ) snake_case_ : Optional[Any] = parser.parse_args() convert_glpn_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub, args.model_name)
488
'''simple docstring''' from __future__ import annotations from typing import Any class _A : def __init__( self : str , __magic_name__ : int , __magic_name__ : int , __magic_name__ : float = 0 ) -> None: """simple docstring""" __snake_case , __snake_case : Optional[Any] = row, column __snake_case : Dict = [[default_value for c in range(__magic_name__ )] for r in range(__magic_name__ )] def __str__( self : List[Any] ) -> str: """simple docstring""" __snake_case : Dict = f'''Matrix consist of {self.row} rows and {self.column} columns\n''' # Make string identifier __snake_case : Optional[int] = 0 for row_vector in self.array: for obj in row_vector: __snake_case : Optional[int] = max(__magic_name__ , len(str(__magic_name__ ) ) ) __snake_case : str = f'''%{max_element_length}s''' # Make string and return def single_line(__magic_name__ : list[float] ) -> str: nonlocal string_format_identifier __snake_case : Union[str, Any] = """[""" line += ", ".join(string_format_identifier % (obj,) for obj in row_vector ) line += "]" return line s += "\n".join(single_line(__magic_name__ ) for row_vector in self.array ) return s def __repr__( self : Optional[int] ) -> str: """simple docstring""" return str(self ) def lowercase__ ( self : Dict , __magic_name__ : tuple[int, int] ) -> bool: """simple docstring""" if not (isinstance(__magic_name__ , (list, tuple) ) and len(__magic_name__ ) == 2): return False elif not (0 <= loc[0] < self.row and 0 <= loc[1] < self.column): return False else: return True def __getitem__( self : int , __magic_name__ : tuple[int, int] ) -> Any: """simple docstring""" assert self.validate_indicies(__magic_name__ ) return self.array[loc[0]][loc[1]] def __setitem__( self : List[str] , __magic_name__ : tuple[int, int] , __magic_name__ : float ) -> None: """simple docstring""" assert self.validate_indicies(__magic_name__ ) __snake_case : Optional[int] = value def __add__( self : Any , __magic_name__ : Matrix ) -> Matrix: """simple docstring""" assert isinstance(__magic_name__ , __magic_name__ ) assert self.row == another.row and self.column == another.column # Add __snake_case : Union[str, Any] = Matrix(self.row , self.column ) for r in range(self.row ): for c in range(self.column ): __snake_case : List[Any] = self[r, c] + another[r, c] return result def __neg__( self : Tuple ) -> Matrix: """simple docstring""" __snake_case : Tuple = Matrix(self.row , self.column ) for r in range(self.row ): for c in range(self.column ): __snake_case : List[Any] = -self[r, c] return result def __sub__( self : Optional[int] , __magic_name__ : Matrix ) -> Matrix: """simple docstring""" return self + (-another) def __mul__( self : List[Any] , __magic_name__ : int | float | Matrix ) -> Matrix: """simple docstring""" if isinstance(__magic_name__ , (int, float) ): # Scalar multiplication __snake_case : Optional[int] = Matrix(self.row , self.column ) for r in range(self.row ): for c in range(self.column ): __snake_case : Tuple = self[r, c] * another return result elif isinstance(__magic_name__ , __magic_name__ ): # Matrix multiplication assert self.column == another.row __snake_case : Dict = Matrix(self.row , another.column ) for r in range(self.row ): for c in range(another.column ): for i in range(self.column ): result[r, c] += self[r, i] * another[i, c] return result else: __snake_case : Optional[int] = f'''Unsupported type given for another ({type(__magic_name__ )})''' raise TypeError(__magic_name__ ) def lowercase__ ( self : str ) -> Matrix: """simple docstring""" __snake_case : Any = Matrix(self.column , self.row ) for r in range(self.row ): for c in range(self.column ): __snake_case : str = self[r, c] return result def lowercase__ ( self : Union[str, Any] , __magic_name__ : Matrix , __magic_name__ : Matrix ) -> Any: """simple docstring""" assert isinstance(__magic_name__ , __magic_name__ ) and isinstance(__magic_name__ , __magic_name__ ) assert self.row == self.column == u.row == v.row # u, v should be column vector assert u.column == v.column == 1 # u, v should be column vector # Calculate __snake_case : List[str] = v.transpose() __snake_case : Tuple = (v_t * self * u)[0, 0] + 1 if numerator_factor == 0: return None # It's not invertable return self - ((self * u) * (v_t * self) * (1.0 / numerator_factor)) # Testing if __name__ == "__main__": def _a ( ) -> None: """simple docstring""" __snake_case : Tuple = Matrix(3 , 3 , 0 ) for i in range(3 ): __snake_case : Any = 1 print(F'''a^(-1) is {ainv}''' ) # u, v __snake_case : Dict = Matrix(3 , 1 , 0 ) __snake_case , __snake_case , __snake_case : Union[str, Any] = 1, 2, -3 __snake_case : str = Matrix(3 , 1 , 0 ) __snake_case , __snake_case , __snake_case : Tuple = 4, -2, 5 print(F'''u is {u}''' ) print(F'''v is {v}''' ) print(F'''uv^T is {u * v.transpose()}''' ) # Sherman Morrison print(F'''(a + uv^T)^(-1) is {ainv.sherman_morrison(_lowerCamelCase , _lowerCamelCase )}''' ) def _a ( ) -> None: """simple docstring""" import doctest doctest.testmod() testa()
26
0
def __SCREAMING_SNAKE_CASE ( a__ : List[str] = 100 ) -> int: __A : Any = n * (n + 1) * (2 * n + 1) / 6 __A : List[Any] = (n * (n + 1) / 2) ** 2 return int(square_of_sum - sum_of_squares ) if __name__ == "__main__": print(f"""{solution() = }""")
17
'''simple docstring''' import argparse import json import os import torch from torch import nn from transformers import NllbMoeConfig, NllbMoeModel from transformers.modeling_utils import dtype_byte_size from transformers.utils import WEIGHTS_INDEX_NAME, WEIGHTS_NAME def _a ( _lowerCamelCase ) -> List[Any]: """simple docstring""" __snake_case : Union[str, Any] = [ """encoder.version""", """decoder.version""", """model.encoder.version""", """model.decoder.version""", """decoder.output_projection.weight""", """_float_tensor""", """encoder.embed_positions._float_tensor""", """decoder.embed_positions._float_tensor""", ] for k in ignore_keys: state_dict.pop(_lowerCamelCase , _lowerCamelCase ) def _a ( _lowerCamelCase ) -> List[str]: """simple docstring""" __snake_case , __snake_case : Dict = emb.weight.shape __snake_case : Optional[int] = nn.Linear(_lowerCamelCase , _lowerCamelCase , bias=_lowerCamelCase ) __snake_case : Union[str, Any] = emb.weight.data return lin_layer def _a ( _lowerCamelCase , _lowerCamelCase=None ) -> Union[str, Any]: """simple docstring""" __snake_case : Any = {} for old_key in state_dict.keys(): __snake_case : Union[str, Any] = old_key if "moe_layer.experts." in key: if expert_idx is not None: __snake_case : Tuple = key.replace("""moe_layer.experts.0""" , F'''ffn.experts.expert_{expert_idx}''' ) else: __snake_case : Optional[int] = key.replace("""moe_layer.experts.""" , """ffn.experts.expert_""" ) if "gate" in key: __snake_case : Dict = key.replace(""".moe_layer.gate.wg""" , """.ffn.router.classifier""" ) if "fc2" and "experts" not in key: __snake_case : Union[str, Any] = key.replace(""".fc2.""" , """.ffn.fc2.""" ) if "fc1" and "experts" not in key: __snake_case : Optional[int] = key.replace(""".fc1.""" , """.ffn.fc1.""" ) if ".encoder_attn." in key: __snake_case : Tuple = key.replace(""".encoder_attn.""" , """.cross_attention.""" ) if "encoder_attn_layer_norm" in key: __snake_case : Union[str, Any] = key.replace("""encoder_attn_layer_norm""" , """cross_attention_layer_norm""" ) if "final_layer_norm" in key: __snake_case : str = key.replace("""final_layer_norm""" , """ff_layer_norm""" ) __snake_case : str = state_dict[old_key] return new_dict def _a ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase = WEIGHTS_NAME ) -> Dict: """simple docstring""" __snake_case : Optional[int] = [] __snake_case : Dict = 0 os.makedirs(_lowerCamelCase , exist_ok=_lowerCamelCase ) for expert in range(_lowerCamelCase ): __snake_case : Tuple = switch_checkpoint_path + F'''-rank-{expert}.pt''' if os.path.isfile(_lowerCamelCase ): __snake_case : Dict = torch.load(_lowerCamelCase )["""model"""] remove_ignore_keys_(_lowerCamelCase ) __snake_case : Optional[Any] = rename_fairseq_keys(_lowerCamelCase , _lowerCamelCase ) __snake_case : List[Any] = os.path.join( _lowerCamelCase , weights_name.replace(""".bin""" , F'''-{len(_lowerCamelCase )+1:05d}-of-???.bin''' ) ) torch.save(_lowerCamelCase , _lowerCamelCase ) sharded_state_dicts.append(expert_state.keys() ) total_size += sum([value.numel() for key, value in expert_state.items()] ) * dtype_byte_size( expert_state[list(_lowerCamelCase )[0]].dtype ) # Add the last block __snake_case : Optional[Any] = os.path.join(_lowerCamelCase , weights_name.replace(""".bin""" , F'''-{len(_lowerCamelCase )+1:05d}-of-???.bin''' ) ) __snake_case : str = torch.load(switch_checkpoint_path + """-shared.pt""" )["""model"""] remove_ignore_keys_(_lowerCamelCase ) __snake_case : Optional[Any] = rename_fairseq_keys(_lowerCamelCase , _lowerCamelCase ) __snake_case : List[str] = shared_weights["""decoder.embed_tokens.weight"""] sharded_state_dicts.append(shared_weights.keys() ) # If we only have the shared weights (dummy model/experts saved on the same file) if len(_lowerCamelCase ) == 1: __snake_case : Optional[Any] = os.path.join(_lowerCamelCase , _lowerCamelCase ) torch.save(_lowerCamelCase , _lowerCamelCase ) return {weights_name: sharded_state_dicts[0]}, None else: torch.save(_lowerCamelCase , _lowerCamelCase ) # Otherwise, let's build the index __snake_case : Tuple = {} for idx, shard in enumerate(_lowerCamelCase ): __snake_case : Any = weights_name.replace(""".bin""" , F'''-{idx+1:05d}-of-{len(_lowerCamelCase ):05d}.bin''' ) __snake_case : int = os.path.join(_lowerCamelCase , weights_name.replace(""".bin""" , F'''-{idx+1:05d}-of-???.bin''' ) ) os.rename(_lowerCamelCase , os.path.join(_lowerCamelCase , _lowerCamelCase ) ) for key in shard: __snake_case : str = shard_file # Add the metadata __snake_case : Optional[Any] = {"""total_size""": total_size} __snake_case : int = {"""metadata""": metadata, """weight_map""": weight_map} with open(os.path.join(_lowerCamelCase , _lowerCamelCase ) , """w""" , encoding="""utf-8""" ) as f: __snake_case : Union[str, Any] = json.dumps(_lowerCamelCase , indent=2 , sort_keys=_lowerCamelCase ) + """\n""" f.write(_lowerCamelCase ) return metadata, index if __name__ == "__main__": __UpperCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( "--nllb_moe_checkpoint_path", default="/home/arthur_huggingface_co/fairseq/weights/checkpoints/model_moe_54b/checkpoint_2_300000", type=str, required=False, help="Path to a directory containing a folder per layer. Follows the original Google format.", ) parser.add_argument("--dtype", default="float32", type=str, required=False, help="dtype of the saved model") parser.add_argument( "--pytorch_dump_folder_path", default="/home/arthur_huggingface_co/fairseq/weights/checkpoints/hf-converted-moe-54b", type=str, required=False, help="Path to the output pytorch model.", ) __UpperCamelCase = parser.parse_args() __UpperCamelCase , __UpperCamelCase = shard_on_the_fly( args.nllb_moe_checkpoint_path, args.pytorch_dump_folder_path, 128, args.dtype, ) __UpperCamelCase = NllbMoeConfig.from_pretrained( "facebook/nllb-200-3.3B", encoder_sparse_step=4, decoder_sparse_step=4, num_experts=128 ) config.save_pretrained(args.pytorch_dump_folder_path) __UpperCamelCase = NllbMoeModel.from_pretrained(args.pytorch_dump_folder_path) print("Done") model.save_pretrained(args.pytorch_dump_folder_path)
26
0
"""simple docstring""" def __magic_name__ ( __snake_case : str , __snake_case : Optional[int] ) -> List[Any]: lowercase : Optional[Any] = [0 for i in range(r + 1 )] # nc0 = 1 lowercase : Optional[Any] = 1 for i in range(1 , n + 1 ): # to compute current row from previous row. lowercase : Optional[Any] = min(_lowerCamelCase , _lowerCamelCase ) while j > 0: c[j] += c[j - 1] j -= 1 return c[r] print(binomial_coefficient(n=10, r=5))
361
'''simple docstring''' import cva import numpy as np class _A : def __init__( self : Any , __magic_name__ : float , __magic_name__ : int ) -> Optional[int]: """simple docstring""" if k in (0.04, 0.06): __snake_case : List[str] = k __snake_case : int = window_size else: raise ValueError("""invalid k value""" ) def __str__( self : Union[str, Any] ) -> str: """simple docstring""" return str(self.k ) def lowercase__ ( self : Dict , __magic_name__ : str ) -> tuple[cva.Mat, list[list[int]]]: """simple docstring""" __snake_case : Dict = cva.imread(__magic_name__ , 0 ) __snake_case , __snake_case : List[str] = img.shape __snake_case : list[list[int]] = [] __snake_case : str = img.copy() __snake_case : Tuple = cva.cvtColor(__magic_name__ , cva.COLOR_GRAY2RGB ) __snake_case , __snake_case : List[Any] = np.gradient(__magic_name__ ) __snake_case : Optional[Any] = dx**2 __snake_case : Tuple = dy**2 __snake_case : List[Any] = dx * dy __snake_case : List[Any] = 0.04 __snake_case : Tuple = self.window_size // 2 for y in range(__magic_name__ , h - offset ): for x in range(__magic_name__ , w - offset ): __snake_case : Dict = ixx[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() __snake_case : Optional[int] = iyy[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() __snake_case : str = ixy[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() __snake_case : List[str] = (wxx * wyy) - (wxy**2) __snake_case : Dict = wxx + wyy __snake_case : List[str] = det - k * (trace**2) # Can change the value if r > 0.5: corner_list.append([x, y, r] ) color_img.itemset((y, x, 0) , 0 ) color_img.itemset((y, x, 1) , 0 ) color_img.itemset((y, x, 2) , 2_55 ) return color_img, corner_list if __name__ == "__main__": __UpperCamelCase = HarrisCorner(0.04, 3) __UpperCamelCase , __UpperCamelCase = edge_detect.detect("path_to_image") cva.imwrite("detect.png", color_img)
26
0
from __future__ import annotations def _SCREAMING_SNAKE_CASE ( lowercase : int , lowercase : Optional[Any] = None , lowercase : Union[str, Any] = None ): '''simple docstring''' if start is None: lowerCamelCase_ = 0 if end is None: lowerCamelCase_ = len(_lowerCamelCase ) - 1 if start >= end: return lowerCamelCase_ = (start + end) // 2 slowsort(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) slowsort(_lowerCamelCase , mid + 1 , _lowerCamelCase ) if sequence[end] < sequence[mid]: lowerCamelCase_ = sequence[mid], sequence[end] slowsort(_lowerCamelCase , _lowerCamelCase , end - 1 ) if __name__ == "__main__": from doctest import testmod testmod()
70
'''simple docstring''' import warnings from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class _A ( __lowercase ): lowercase__: Any = ['''image_processor''', '''tokenizer'''] lowercase__: Any = '''CLIPImageProcessor''' lowercase__: Optional[Any] = ('''CLIPTokenizer''', '''CLIPTokenizerFast''') def __init__( self : int , __magic_name__ : Dict=None , __magic_name__ : Dict=None , **__magic_name__ : Union[str, Any] ) -> Any: """simple docstring""" __snake_case : Optional[Any] = None if "feature_extractor" in kwargs: warnings.warn( """The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`""" """ instead.""" , __magic_name__ , ) __snake_case : List[Any] = kwargs.pop("""feature_extractor""" ) __snake_case : List[str] = 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__(__magic_name__ , __magic_name__ ) def __call__( self : int , __magic_name__ : List[str]=None , __magic_name__ : Tuple=None , __magic_name__ : Any=None , **__magic_name__ : Union[str, Any] ) -> Optional[int]: """simple docstring""" 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: __snake_case : int = self.tokenizer(__magic_name__ , return_tensors=__magic_name__ , **__magic_name__ ) if images is not None: __snake_case : str = self.image_processor(__magic_name__ , return_tensors=__magic_name__ , **__magic_name__ ) if text is not None and images is not None: __snake_case : Union[str, Any] = image_features.pixel_values return encoding elif text is not None: return encoding else: return BatchEncoding(data=dict(**__magic_name__ ) , tensor_type=__magic_name__ ) def lowercase__ ( self : Optional[int] , *__magic_name__ : List[Any] , **__magic_name__ : Any ) -> Optional[Any]: """simple docstring""" return self.tokenizer.batch_decode(*__magic_name__ , **__magic_name__ ) def lowercase__ ( self : List[str] , *__magic_name__ : Tuple , **__magic_name__ : List[Any] ) -> int: """simple docstring""" return self.tokenizer.decode(*__magic_name__ , **__magic_name__ ) @property def lowercase__ ( self : List[Any] ) -> Optional[Any]: """simple docstring""" __snake_case : Dict = self.tokenizer.model_input_names __snake_case : str = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) ) @property def lowercase__ ( self : int ) -> List[str]: """simple docstring""" warnings.warn( """`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.""" , __magic_name__ , ) return self.image_processor_class @property def lowercase__ ( self : Union[str, Any] ) -> Dict: """simple docstring""" warnings.warn( """`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.""" , __magic_name__ , ) return self.image_processor
26
0
"""simple docstring""" from .constants import ( MODEL_NAME, OPTIMIZER_NAME, RNG_STATE_NAME, SAFE_WEIGHTS_INDEX_NAME, SAFE_WEIGHTS_NAME, SCALER_NAME, SCHEDULER_NAME, TORCH_LAUNCH_PARAMS, WEIGHTS_INDEX_NAME, WEIGHTS_NAME, ) from .dataclasses import ( BnbQuantizationConfig, ComputeEnvironment, CustomDtype, DeepSpeedPlugin, DistributedDataParallelKwargs, DistributedType, DynamoBackend, FPaRecipeKwargs, FullyShardedDataParallelPlugin, GradientAccumulationPlugin, GradScalerKwargs, InitProcessGroupKwargs, KwargsHandler, LoggerType, MegatronLMPlugin, PrecisionType, ProjectConfiguration, RNGType, SageMakerDistributedType, TensorInformation, TorchDynamoPlugin, ) from .environment import get_int_from_env, parse_choice_from_env, parse_flag_from_env from .imports import ( get_ccl_version, is_abit_bnb_available, is_abit_bnb_available, is_aim_available, is_bfaa_available, is_bnb_available, is_botoa_available, is_ccl_available, is_comet_ml_available, is_datasets_available, is_deepspeed_available, is_fpa_available, is_ipex_available, is_megatron_lm_available, is_mlflow_available, is_mps_available, is_npu_available, is_rich_available, is_safetensors_available, is_sagemaker_available, is_tensorboard_available, is_tpu_available, is_transformers_available, is_wandb_available, is_xpu_available, ) from .modeling import ( check_device_map, check_tied_parameters_in_config, check_tied_parameters_on_same_device, compute_module_sizes, convert_file_size_to_int, dtype_byte_size, find_tied_parameters, get_balanced_memory, get_max_layer_size, get_max_memory, get_mixed_precision_context_manager, id_tensor_storage, infer_auto_device_map, load_checkpoint_in_model, load_offloaded_weights, load_state_dict, named_module_tensors, retie_parameters, set_module_tensor_to_device, shard_checkpoint, ) from .offload import ( OffloadedWeightsLoader, PrefixedDataset, extract_submodules_state_dict, load_offloaded_weight, offload_state_dict, offload_weight, save_offload_index, ) from .operations import ( broadcast, broadcast_object_list, concatenate, convert_outputs_to_fpaa, convert_to_fpaa, find_batch_size, find_device, gather, gather_object, get_data_structure, honor_type, initialize_tensors, is_namedtuple, is_tensor_information, is_torch_tensor, listify, pad_across_processes, recursively_apply, reduce, send_to_device, slice_tensors, ) from .versions import compare_versions, is_torch_version if is_deepspeed_available(): from .deepspeed import ( DeepSpeedEngineWrapper, DeepSpeedOptimizerWrapper, DeepSpeedSchedulerWrapper, DummyOptim, DummyScheduler, HfDeepSpeedConfig, ) from .bnb import has_abit_bnb_layers, load_and_quantize_model from .fsdp_utils import load_fsdp_model, load_fsdp_optimizer, save_fsdp_model, save_fsdp_optimizer from .launch import ( PrepareForLaunch, _filter_args, prepare_deepspeed_cmd_env, prepare_multi_gpu_env, prepare_sagemager_args_inputs, prepare_simple_launcher_cmd_env, prepare_tpu, ) from .megatron_lm import ( AbstractTrainStep, BertTrainStep, GPTTrainStep, MegatronEngine, MegatronLMDummyDataLoader, MegatronLMDummyScheduler, MegatronLMOptimizerWrapper, MegatronLMSchedulerWrapper, TaTrainStep, avg_losses_across_data_parallel_group, gather_across_data_parallel_groups, ) from .megatron_lm import initialize as megatron_lm_initialize from .megatron_lm import prepare_data_loader as megatron_lm_prepare_data_loader from .megatron_lm import prepare_model as megatron_lm_prepare_model from .megatron_lm import prepare_optimizer as megatron_lm_prepare_optimizer from .megatron_lm import prepare_scheduler as megatron_lm_prepare_scheduler from .memory import find_executable_batch_size, release_memory from .other import ( extract_model_from_parallel, get_pretty_name, is_port_in_use, merge_dicts, patch_environment, save, wait_for_everyone, write_basic_config, ) from .random import set_seed, synchronize_rng_state, synchronize_rng_states from .torch_xla import install_xla from .tqdm import tqdm from .transformer_engine import convert_model, has_transformer_engine_layers
512
'''simple docstring''' import datasets import faiss import numpy as np import streamlit as st import torch from elasticsearch import Elasticsearch from elia_utils import ( embed_questions_for_retrieval, make_qa_sas_model, qa_sas_generate, query_es_index, query_qa_dense_index, ) import transformers from transformers import AutoModel, AutoModelForSeqaSeqLM, AutoTokenizer __UpperCamelCase = "bart" __UpperCamelCase = True @st.cache(allow_output_mutation=_lowerCamelCase ) def _a ( ) -> Union[str, Any]: """simple docstring""" if LOAD_DENSE_INDEX: __snake_case : int = AutoTokenizer.from_pretrained("""yjernite/retribert-base-uncased""" ) __snake_case : Tuple = AutoModel.from_pretrained("""yjernite/retribert-base-uncased""" ).to("""cuda:0""" ) __snake_case : List[Any] = qar_model.eval() else: __snake_case , __snake_case : Optional[Any] = (None, None) if MODEL_TYPE == "bart": __snake_case : List[str] = AutoTokenizer.from_pretrained("""yjernite/bart_eli5""" ) __snake_case : Any = AutoModelForSeqaSeqLM.from_pretrained("""yjernite/bart_eli5""" ).to("""cuda:0""" ) __snake_case : int = torch.load("""seq2seq_models/eli5_bart_model_blm_2.pth""" ) sas_model.load_state_dict(save_dict["""model"""] ) __snake_case : int = sas_model.eval() else: __snake_case , __snake_case : Dict = make_qa_sas_model( model_name="""t5-small""" , from_file="""seq2seq_models/eli5_t5_model_1024_4.pth""" , device="""cuda:0""" ) return (qar_tokenizer, qar_model, sas_tokenizer, sas_model) @st.cache(allow_output_mutation=_lowerCamelCase ) def _a ( ) -> Tuple: """simple docstring""" if LOAD_DENSE_INDEX: __snake_case : Tuple = faiss.StandardGpuResources() __snake_case : Optional[Any] = datasets.load_dataset(path="""wiki_snippets""" , name="""wiki40b_en_100_0""" )["""train"""] __snake_case : str = np.memmap( """wiki40b_passages_reps_32_l-8_h-768_b-512-512.dat""" , dtype="""float32""" , mode="""r""" , shape=(wikiaab_passages.num_rows, 128) , ) __snake_case : Optional[int] = faiss.IndexFlatIP(128 ) __snake_case : Any = faiss.index_cpu_to_gpu(_lowerCamelCase , 1 , _lowerCamelCase ) wikiaab_gpu_index_flat.add(_lowerCamelCase ) # TODO fix for larger GPU else: __snake_case , __snake_case : Tuple = (None, None) __snake_case : List[str] = Elasticsearch([{"""host""": """localhost""", """port""": """9200"""}] ) return (wikiaab_passages, wikiaab_gpu_index_flat, es_client) @st.cache(allow_output_mutation=_lowerCamelCase ) def _a ( ) -> List[Any]: """simple docstring""" __snake_case : Tuple = datasets.load_dataset("""eli5""" , name="""LFQA_reddit""" ) __snake_case : Dict = elia["""train_eli5"""] __snake_case : int = np.memmap( """eli5_questions_reps.dat""" , dtype="""float32""" , mode="""r""" , shape=(elia_train.num_rows, 128) ) __snake_case : Dict = faiss.IndexFlatIP(128 ) eli5_train_q_index.add(_lowerCamelCase ) return (elia_train, eli5_train_q_index) __UpperCamelCase , __UpperCamelCase , __UpperCamelCase = load_indexes() __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase = load_models() __UpperCamelCase , __UpperCamelCase = load_train_data() def _a ( _lowerCamelCase , _lowerCamelCase=10 ) -> int: """simple docstring""" __snake_case : Optional[int] = embed_questions_for_retrieval([question] , _lowerCamelCase , _lowerCamelCase ) __snake_case , __snake_case : Tuple = eli5_train_q_index.search(_lowerCamelCase , _lowerCamelCase ) __snake_case : Tuple = [elia_train[int(_lowerCamelCase )] for i in I[0]] return nn_examples def _a ( _lowerCamelCase , _lowerCamelCase="wiki40b" , _lowerCamelCase="dense" , _lowerCamelCase=10 ) -> Optional[Any]: """simple docstring""" if source == "none": __snake_case , __snake_case : Dict = (""" <P> """.join(["""""" for _ in range(11 )] ).strip(), []) else: if method == "dense": __snake_case , __snake_case : Dict = query_qa_dense_index( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) else: __snake_case , __snake_case : str = query_es_index( _lowerCamelCase , _lowerCamelCase , index_name="""english_wiki40b_snippets_100w""" , n_results=_lowerCamelCase , ) __snake_case : Optional[int] = [ (res["""article_title"""], res["""section_title"""].strip(), res["""score"""], res["""passage_text"""]) for res in hit_lst ] __snake_case : Optional[Any] = """question: {} context: {}""".format(_lowerCamelCase , _lowerCamelCase ) return question_doc, support_list @st.cache( hash_funcs={ torch.Tensor: (lambda _lowerCamelCase : None), transformers.models.bart.tokenization_bart.BartTokenizer: (lambda _lowerCamelCase : None), } ) def _a ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase=64 , _lowerCamelCase=256 , _lowerCamelCase=False , _lowerCamelCase=2 , _lowerCamelCase=0.95 , _lowerCamelCase=0.8 ) -> List[str]: """simple docstring""" with torch.no_grad(): __snake_case : Union[str, Any] = qa_sas_generate( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , num_answers=1 , num_beams=_lowerCamelCase , min_len=_lowerCamelCase , max_len=_lowerCamelCase , do_sample=_lowerCamelCase , temp=_lowerCamelCase , top_p=_lowerCamelCase , top_k=_lowerCamelCase , max_input_length=1024 , device="""cuda:0""" , )[0] return (answer, support_list) st.title("Long Form Question Answering with ELI5") # Start sidebar __UpperCamelCase = "<img src='https://huggingface.co/front/assets/huggingface_logo.svg'>" __UpperCamelCase = "\n<html>\n <head>\n <style>\n .img-container {\n padding-left: 90px;\n padding-right: 90px;\n padding-top: 50px;\n padding-bottom: 50px;\n background-color: #f0f3f9;\n }\n </style>\n </head>\n <body>\n <span class=\"img-container\"> <!-- Inline parent element -->\n %s\n </span>\n </body>\n</html>\n" % ( header_html, ) st.sidebar.markdown( header_full, unsafe_allow_html=True, ) # Long Form QA with ELI5 and Wikipedia __UpperCamelCase = "\nThis demo presents a model trained to [provide long-form answers to open-domain questions](https://yjernite.github.io/lfqa.html).\nFirst, a document retriever fetches a set of relevant Wikipedia passages given the question from the [Wiki40b](https://research.google/pubs/pub49029/) dataset,\na pre-processed fixed snapshot of Wikipedia.\n" st.sidebar.markdown(description, unsafe_allow_html=True) __UpperCamelCase = [ "Answer the question", "View the retrieved document only", "View the most similar ELI5 question and answer", "Show me everything, please!", ] __UpperCamelCase = st.sidebar.checkbox("Demo options") if demo_options: __UpperCamelCase = st.sidebar.selectbox( "", action_list, index=3, ) __UpperCamelCase = action_list.index(action_st) __UpperCamelCase = st.sidebar.selectbox( "", ["Show full text of passages", "Show passage section titles"], index=0, ) __UpperCamelCase = show_type == "Show full text of passages" else: __UpperCamelCase = 3 __UpperCamelCase = True __UpperCamelCase = st.sidebar.checkbox("Retrieval options") if retrieval_options: __UpperCamelCase = "\n ### Information retriever options\n\n The **sparse** retriever uses ElasticSearch, while the **dense** retriever uses max-inner-product search between a question and passage embedding\n trained using the [ELI5](https://arxiv.org/abs/1907.09190) questions-answer pairs.\n The answer is then generated by sequence to sequence model which takes the question and retrieved document as input.\n " st.sidebar.markdown(retriever_info) __UpperCamelCase = st.sidebar.selectbox("Which Wikipedia format should the model use?", ["wiki40b", "none"]) __UpperCamelCase = st.sidebar.selectbox("Which Wikipedia indexer should the model use?", ["dense", "sparse", "mixed"]) else: __UpperCamelCase = "wiki40b" __UpperCamelCase = "dense" __UpperCamelCase = "beam" __UpperCamelCase = 2 __UpperCamelCase = 64 __UpperCamelCase = 256 __UpperCamelCase = None __UpperCamelCase = None __UpperCamelCase = st.sidebar.checkbox("Generation options") if generate_options: __UpperCamelCase = "\n ### Answer generation options\n\n The sequence-to-sequence model was initialized with [BART](https://huggingface.co/facebook/bart-large)\n weights and fine-tuned on the ELI5 QA pairs and retrieved documents. You can use the model for greedy decoding with\n **beam** search, or **sample** from the decoder's output probabilities.\n " st.sidebar.markdown(generate_info) __UpperCamelCase = st.sidebar.selectbox("Would you like to use beam search or sample an answer?", ["beam", "sampled"]) __UpperCamelCase = st.sidebar.slider( "Minimum generation length", min_value=8, max_value=256, value=64, step=8, format=None, key=None ) __UpperCamelCase = st.sidebar.slider( "Maximum generation length", min_value=64, max_value=512, value=256, step=16, format=None, key=None ) if sampled == "beam": __UpperCamelCase = st.sidebar.slider("Beam size", min_value=1, max_value=8, value=2, step=None, format=None, key=None) else: __UpperCamelCase = st.sidebar.slider( "Nucleus sampling p", min_value=0.1, max_value=1.0, value=0.95, step=0.01, format=None, key=None ) __UpperCamelCase = st.sidebar.slider( "Temperature", min_value=0.1, max_value=1.0, value=0.7, step=0.01, format=None, key=None ) __UpperCamelCase = None # start main text __UpperCamelCase = [ "<MY QUESTION>", "How do people make chocolate?", "Why do we get a fever when we are sick?", "How can different animals perceive different colors?", "What is natural language processing?", "What's the best way to treat a sunburn?", "What exactly are vitamins ?", "How does nuclear energy provide electricity?", "What's the difference between viruses and bacteria?", "Why are flutes classified as woodwinds when most of them are made out of metal ?", "Why do people like drinking coffee even though it tastes so bad?", "What happens when wine ages? How does it make the wine taste better?", "If an animal is an herbivore, where does it get the protein that it needs to survive if it only eats grass?", "How can we set a date to the beginning or end of an artistic period? Doesn't the change happen gradually?", "How does New Zealand have so many large bird predators?", ] __UpperCamelCase = st.selectbox( "What would you like to ask? ---- select <MY QUESTION> to enter a new query", questions_list, index=1, ) if question_s == "<MY QUESTION>": __UpperCamelCase = st.text_input("Enter your question here:", "") else: __UpperCamelCase = question_s if st.button("Show me!"): if action in [0, 1, 3]: if index_type == "mixed": __UpperCamelCase , __UpperCamelCase = make_support(question, source=wiki_source, method="dense", n_results=10) __UpperCamelCase , __UpperCamelCase = make_support(question, source=wiki_source, method="sparse", n_results=10) __UpperCamelCase = [] for res_d, res_s in zip(support_list_dense, support_list_sparse): if tuple(res_d) not in support_list: support_list += [tuple(res_d)] if tuple(res_s) not in support_list: support_list += [tuple(res_s)] __UpperCamelCase = support_list[:10] __UpperCamelCase = "<P> " + " <P> ".join([res[-1] for res in support_list]) else: __UpperCamelCase , __UpperCamelCase = make_support(question, source=wiki_source, method=index_type, n_results=10) if action in [0, 3]: __UpperCamelCase , __UpperCamelCase = answer_question( question_doc, sas_model, sas_tokenizer, min_len=min_len, max_len=int(max_len), sampling=(sampled == "sampled"), n_beams=n_beams, top_p=top_p, temp=temp, ) st.markdown("### The model generated answer is:") st.write(answer) if action in [0, 1, 3] and wiki_source != "none": st.markdown("--- \n ### The model is drawing information from the following Wikipedia passages:") for i, res in enumerate(support_list): __UpperCamelCase = "https://en.wikipedia.org/wiki/{}".format(res[0].replace(" ", "_")) __UpperCamelCase = res[1].strip() if sec_titles == "": __UpperCamelCase = "[{}]({})".format(res[0], wiki_url) else: __UpperCamelCase = sec_titles.split(" & ") __UpperCamelCase = " & ".join( ["[{}]({}#{})".format(sec.strip(), wiki_url, sec.strip().replace(" ", "_")) for sec in sec_list] ) st.markdown( "{0:02d} - **Article**: {1:<18} <br> _Section_: {2}".format(i + 1, res[0], sections), unsafe_allow_html=True, ) if show_passages: st.write( "> <span style=\"font-family:arial; font-size:10pt;\">" + res[-1] + "</span>", unsafe_allow_html=True ) if action in [2, 3]: __UpperCamelCase = find_nearest_training(question) __UpperCamelCase = nn_train_list[0] st.markdown( "--- \n ### The most similar question in the ELI5 training set was: \n\n {}".format(train_exple["title"]) ) __UpperCamelCase = [ "{}. {}".format(i + 1, " \n".join([line.strip() for line in ans.split("\n") if line.strip() != ""])) for i, (ans, sc) in enumerate(zip(train_exple["answers"]["text"], train_exple["answers"]["score"])) if i == 0 or sc > 2 ] st.markdown("##### Its answers were: \n\n {}".format("\n".join(answers_st))) __UpperCamelCase = "\n---\n\n**Disclaimer**\n\n*The intent of this app is to provide some (hopefully entertaining) insights into the behavior of a current LFQA system.\nEvaluating biases of such a model and ensuring factual generations are still very much open research problems.\nTherefore, until some significant progress is achieved, we caution against using the generated answers for practical purposes.*\n" st.sidebar.markdown(disclaimer, unsafe_allow_html=True)
26
0
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_tokenizers_available, is_torch_available, ) A = { "configuration_longformer": [ "LONGFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", "LongformerConfig", "LongformerOnnxConfig", ], "tokenization_longformer": ["LongformerTokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A = ["LongformerTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A = [ "LONGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "LongformerForMaskedLM", "LongformerForMultipleChoice", "LongformerForQuestionAnswering", "LongformerForSequenceClassification", "LongformerForTokenClassification", "LongformerModel", "LongformerPreTrainedModel", "LongformerSelfAttention", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A = [ "TF_LONGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "TFLongformerForMaskedLM", "TFLongformerForMultipleChoice", "TFLongformerForQuestionAnswering", "TFLongformerForSequenceClassification", "TFLongformerForTokenClassification", "TFLongformerModel", "TFLongformerPreTrainedModel", "TFLongformerSelfAttention", ] if TYPE_CHECKING: from .configuration_longformer import ( LONGFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, LongformerConfig, LongformerOnnxConfig, ) from .tokenization_longformer import LongformerTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_longformer_fast import LongformerTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_longformer import ( LONGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, LongformerForMaskedLM, LongformerForMultipleChoice, LongformerForQuestionAnswering, LongformerForSequenceClassification, LongformerForTokenClassification, LongformerModel, LongformerPreTrainedModel, LongformerSelfAttention, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_longformer import ( TF_LONGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TFLongformerForMaskedLM, TFLongformerForMultipleChoice, TFLongformerForQuestionAnswering, TFLongformerForSequenceClassification, TFLongformerForTokenClassification, TFLongformerModel, TFLongformerPreTrainedModel, TFLongformerSelfAttention, ) else: import sys A = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
475
'''simple docstring''' import warnings from ...utils import logging from .image_processing_owlvit import OwlViTImageProcessor __UpperCamelCase = logging.get_logger(__name__) class _A ( __lowercase ): def __init__( self : int , *__magic_name__ : Optional[Any] , **__magic_name__ : Any ) -> None: """simple docstring""" warnings.warn( """The class OwlViTFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please""" """ use OwlViTImageProcessor instead.""" , __magic_name__ , ) super().__init__(*__magic_name__ , **__magic_name__ )
26
0
def _lowerCAmelCase ( _lowerCAmelCase , _lowerCAmelCase ) -> str: '''simple docstring''' if a < 0 or b < 0: raise ValueError("the value of both inputs must be positive" ) __snake_case = str(bin(_lowerCamelCase ) )[2:] # remove the leading "0b" __snake_case = str(bin(_lowerCamelCase ) )[2:] # remove the leading "0b" __snake_case = max(len(_lowerCamelCase ) , len(_lowerCamelCase ) ) return "0b" + "".join( str(int(char_a != char_b ) ) for char_a, char_b in zip(a_binary.zfill(_lowerCamelCase ) , b_binary.zfill(_lowerCamelCase ) ) ) if __name__ == "__main__": import doctest doctest.testmod()
371
'''simple docstring''' import argparse import torch from transformers import BlenderbotConfig, BlenderbotForConditionalGeneration from transformers.utils import logging logging.set_verbosity_info() __UpperCamelCase = logging.get_logger(__name__) __UpperCamelCase = [ ["attention", "attn"], ["encoder_attention", "encoder_attn"], ["q_lin", "q_proj"], ["k_lin", "k_proj"], ["v_lin", "v_proj"], ["out_lin", "out_proj"], ["norm_embeddings", "layernorm_embedding"], ["position_embeddings", "embed_positions"], ["embeddings", "embed_tokens"], ["ffn.lin", "fc"], ] def _a ( _lowerCamelCase ) -> Union[str, Any]: """simple docstring""" if k == "embeddings.weight": return "shared.weight" for parlai_name, hf_name in PATTERNS: __snake_case : List[str] = k.replace(_lowerCamelCase , _lowerCamelCase ) if k.startswith("""encoder""" ): __snake_case : Optional[int] = k.replace(""".attn""" , """.self_attn""" ) __snake_case : Tuple = k.replace("""norm1""" , """self_attn_layer_norm""" ) __snake_case : List[str] = k.replace("""norm2""" , """final_layer_norm""" ) elif k.startswith("""decoder""" ): __snake_case : List[Any] = k.replace("""norm1""" , """self_attn_layer_norm""" ) __snake_case : str = k.replace("""norm2""" , """encoder_attn_layer_norm""" ) __snake_case : Optional[int] = k.replace("""norm3""" , """final_layer_norm""" ) return k def _a ( _lowerCamelCase ) -> Any: """simple docstring""" __snake_case : Optional[int] = [ """model.encoder.layernorm_embedding.weight""", """model.encoder.layernorm_embedding.bias""", """model.decoder.layernorm_embedding.weight""", """model.decoder.layernorm_embedding.bias""", ] for k in keys: __snake_case : Optional[Any] = sd.pop(_lowerCamelCase ) __snake_case : List[str] = k.replace("""layernorm_embedding""" , """layer_norm""" ) assert new_k not in sd __snake_case : Union[str, Any] = v __UpperCamelCase = ["START"] @torch.no_grad() def _a ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) -> Dict: """simple docstring""" __snake_case : Optional[int] = torch.load(_lowerCamelCase , map_location="""cpu""" ) __snake_case : Dict = model["""model"""] __snake_case : Optional[int] = BlenderbotConfig.from_json_file(_lowerCamelCase ) __snake_case : Union[str, Any] = BlenderbotForConditionalGeneration(_lowerCamelCase ) __snake_case : List[Any] = m.model.state_dict().keys() __snake_case : int = [] __snake_case : Union[str, Any] = {} for k, v in sd.items(): if k in IGNORE_KEYS: continue __snake_case : Optional[int] = rename_state_dict_key(_lowerCamelCase ) if new_k not in valid_keys: failures.append([k, new_k] ) else: __snake_case : str = v if cfg.normalize_before: # Blenderbot-3B checkpoints. Rename layernorm_embedding -> layer_norm rename_layernorm_keys(_lowerCamelCase ) m.model.load_state_dict(_lowerCamelCase , strict=_lowerCamelCase ) m.half() m.save_pretrained(_lowerCamelCase ) if __name__ == "__main__": __UpperCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument("--src_path", type=str, help="like blenderbot-model.bin") parser.add_argument("--save_dir", default="hf_blenderbot", type=str, help="Where to save converted model.") parser.add_argument( "--hf_config_json", default="blenderbot-3b-config.json", type=str, help="Path to config to use" ) __UpperCamelCase = parser.parse_args() convert_parlai_checkpoint(args.src_path, args.save_dir, args.hf_config_json)
26
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 lowercase_: List[Any] = Mapping[str, np.ndarray] lowercase_: int = Mapping[str, Any] # Is a nested dict. lowercase_: int = 0.0_1 @dataclasses.dataclass(frozen=__lowercase ) class lowercase__ : """simple docstring""" __UpperCamelCase : np.ndarray # [num_res, num_atom_type, 3] # Amino-acid type for each residue represented as an integer between 0 and # 20, where 20 is 'X'. __UpperCamelCase : np.ndarray # [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. __UpperCamelCase : np.ndarray # [num_res, num_atom_type] # Residue index as used in PDB. It is not necessarily continuous or 0-indexed. __UpperCamelCase : np.ndarray # [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. __UpperCamelCase : np.ndarray # [num_res, num_atom_type] # Chain indices for multi-chain predictions __UpperCamelCase : Optional[np.ndarray] = None # Optional remark about the protein. Included as a comment in output PDB # files __UpperCamelCase : Optional[str] = None # Templates used to generate this protein (prediction-only) __UpperCamelCase : Optional[Sequence[str]] = None # Chain corresponding to each parent __UpperCamelCase : Optional[Sequence[int]] = None def _lowercase ( UpperCAmelCase_): """simple docstring""" snake_case__ : List[str] = R"""(\[[A-Z]+\]\n)""" snake_case__ : List[str] = [tag.strip() for tag in re.split(_lowerCamelCase , _lowerCamelCase) if len(_lowerCamelCase) > 0] snake_case__ : Iterator[Tuple[str, List[str]]] = zip(tags[0::2] , [l.split("""\n""") for l in tags[1::2]]) snake_case__ : List[str] = ["N", "CA", "C"] snake_case__ : Union[str, Any] = None snake_case__ : Optional[Any] = None snake_case__ : str = None for g in groups: if "[PRIMARY]" == g[0]: snake_case__ : Any = g[1][0].strip() for i in range(len(_lowerCamelCase)): if seq[i] not in residue_constants.restypes: snake_case__ : Union[str, Any] = """X""" # FIXME: strings are immutable snake_case__ : Optional[Any] = np.array( [residue_constants.restype_order.get(_lowerCamelCase , residue_constants.restype_num) for res_symbol in seq]) elif "[TERTIARY]" == g[0]: snake_case__ : List[List[float]] = [] for axis in range(3): tertiary.append(list(map(_lowerCamelCase , g[1][axis].split()))) snake_case__ : Optional[Any] = np.array(_lowerCamelCase) snake_case__ : Any = np.zeros((len(tertiary[0]) // 3, residue_constants.atom_type_num, 3)).astype(np.floataa) for i, atom in enumerate(_lowerCamelCase): snake_case__ : Union[str, Any] = np.transpose(tertiary_np[:, i::3]) atom_positions *= PICO_TO_ANGSTROM elif "[MASK]" == g[0]: snake_case__ : Union[str, Any] = np.array(list(map({"""-""": 0, """+""": 1}.get , g[1][0].strip()))) snake_case__ : List[str] = np.zeros( ( len(_lowerCamelCase), residue_constants.atom_type_num, )).astype(np.floataa) for i, atom in enumerate(_lowerCamelCase): snake_case__ : Optional[Any] = 1 atom_mask *= mask[..., None] assert aatype is not None return Protein( atom_positions=_lowerCamelCase , atom_mask=_lowerCamelCase , aatype=_lowerCamelCase , residue_index=np.arange(len(_lowerCamelCase)) , b_factors=_lowerCamelCase , ) def _lowercase ( UpperCAmelCase_ , UpperCAmelCase_ = 0): """simple docstring""" snake_case__ : List[str] = [] snake_case__ : Tuple = prot.remark if remark is not None: pdb_headers.append(F'REMARK {remark}') snake_case__ : Optional[int] = prot.parents snake_case__ : Optional[int] = prot.parents_chain_index if parents is not None and parents_chain_index is not None: snake_case__ : Any = [p for i, p in zip(_lowerCamelCase , _lowerCamelCase) if i == chain_id] if parents is None or len(_lowerCamelCase) == 0: snake_case__ : str = ["""N/A"""] pdb_headers.append(F'PARENT {" ".join(_lowerCamelCase)}') return pdb_headers def _lowercase ( UpperCAmelCase_ , UpperCAmelCase_): """simple docstring""" snake_case__ : List[str] = [] snake_case__ : int = pdb_str.split("""\n""") snake_case__ : Union[str, Any] = prot.remark if remark is not None: out_pdb_lines.append(F'REMARK {remark}') snake_case__ : List[List[str]] if prot.parents is not None and len(prot.parents) > 0: snake_case__ : Union[str, Any] = [] if prot.parents_chain_index is not None: snake_case__ : Dict[str, List[str]] = {} for p, i in zip(prot.parents , prot.parents_chain_index): parent_dict.setdefault(str(_lowerCamelCase) , []) parent_dict[str(_lowerCamelCase)].append(_lowerCamelCase) snake_case__ : Optional[int] = max([int(_lowerCamelCase) for chain_idx in parent_dict]) for i in range(max_idx + 1): snake_case__ : Tuple = parent_dict.get(str(_lowerCamelCase) , ["""N/A"""]) parents_per_chain.append(_lowerCamelCase) else: parents_per_chain.append(list(prot.parents)) else: snake_case__ : Dict = [["""N/A"""]] def make_parent_line(UpperCAmelCase_) -> str: return F'PARENT {" ".join(_lowerCamelCase)}' out_pdb_lines.append(make_parent_line(parents_per_chain[0])) snake_case__ : Any = 0 for i, l in enumerate(_lowerCamelCase): if "PARENT" not in l and "REMARK" not in l: out_pdb_lines.append(_lowerCamelCase) if "TER" in l and "END" not in lines[i + 1]: chain_counter += 1 if not chain_counter >= len(_lowerCamelCase): snake_case__ : int = parents_per_chain[chain_counter] else: snake_case__ : int = ["""N/A"""] out_pdb_lines.append(make_parent_line(_lowerCamelCase)) return "\n".join(_lowerCamelCase) def _lowercase ( UpperCAmelCase_): """simple docstring""" snake_case__ : Optional[int] = residue_constants.restypes + ["""X"""] def res_atoa(UpperCAmelCase_) -> str: return residue_constants.restype_atoa.get(restypes[r] , """UNK""") snake_case__ : Union[str, Any] = residue_constants.atom_types snake_case__ : List[str] = [] snake_case__ : int = prot.atom_mask snake_case__ : Optional[int] = prot.aatype snake_case__ : List[str] = prot.atom_positions snake_case__ : str = prot.residue_index.astype(np.intaa) snake_case__ : Optional[int] = prot.b_factors snake_case__ : str = prot.chain_index if np.any(aatype > residue_constants.restype_num): raise ValueError("""Invalid aatypes.""") snake_case__ : Optional[int] = get_pdb_headers(_lowerCamelCase) if len(_lowerCamelCase) > 0: pdb_lines.extend(_lowerCamelCase) snake_case__ : Dict = aatype.shape[0] snake_case__ : Optional[int] = 1 snake_case__ : List[str] = 0 snake_case__ : Optional[Any] = string.ascii_uppercase snake_case__ : Tuple = None # Add all atom sites. for i in range(_lowerCamelCase): snake_case__ : Optional[Any] = res_atoa(aatype[i]) for atom_name, pos, mask, b_factor in zip(_lowerCamelCase , atom_positions[i] , atom_mask[i] , b_factors[i]): if mask < 0.5: continue snake_case__ : int = """ATOM""" snake_case__ : int = atom_name if len(_lowerCamelCase) == 4 else F' {atom_name}' snake_case__ : List[str] = """""" snake_case__ : Optional[Any] = """""" snake_case__ : Any = 1.00 snake_case__ : Optional[Any] = atom_name[0] # Protein supports only C, N, O, S, this works. snake_case__ : List[Any] = """""" snake_case__ : Tuple = """A""" if chain_index is not None: snake_case__ : Optional[int] = chain_tags[chain_index[i]] # PDB is a columnar format, every space matters here! snake_case__ : Optional[int] = ( 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(_lowerCamelCase) atom_index += 1 snake_case__ : List[str] = i == n - 1 if chain_index is not None: if i != n - 1 and chain_index[i + 1] != prev_chain_index: snake_case__ : Optional[Any] = True snake_case__ : Union[str, Any] = chain_index[i + 1] if should_terminate: # Close the chain. snake_case__ : Optional[Any] = """TER""" snake_case__ : List[str] = ( F'{chain_end:<6}{atom_index:>5} {res_atoa(aatype[i]):>3} {chain_tag:>1}{residue_index[i]:>4}' ) pdb_lines.append(_lowerCamelCase) 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(_lowerCamelCase , _lowerCamelCase)) pdb_lines.append("""END""") pdb_lines.append("""""") return "\n".join(_lowerCamelCase) def _lowercase ( UpperCAmelCase_): """simple docstring""" return residue_constants.STANDARD_ATOM_MASK[prot.aatype] def _lowercase ( UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ = None , UpperCAmelCase_ = None , UpperCAmelCase_ = None , UpperCAmelCase_ = None , UpperCAmelCase_ = None , ): """simple docstring""" 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=_lowerCamelCase , remark=_lowerCamelCase , parents=_lowerCamelCase , parents_chain_index=_lowerCamelCase , )
648
'''simple docstring''' import argparse import os import re import packaging.version __UpperCamelCase = "examples/" __UpperCamelCase = { "examples": (re.compile(R"^check_min_version\(\"[^\"]+\"\)\s*$", re.MULTILINE), "check_min_version(\"VERSION\")\n"), "init": (re.compile(R"^__version__\s+=\s+\"([^\"]+)\"\s*$", re.MULTILINE), "__version__ = \"VERSION\"\n"), "setup": (re.compile(R"^(\s*)version\s*=\s*\"[^\"]+\",", re.MULTILINE), R"\1version=\"VERSION\","), "doc": (re.compile(R"^(\s*)release\s*=\s*\"[^\"]+\"$", re.MULTILINE), "release = \"VERSION\"\n"), } __UpperCamelCase = { "init": "src/transformers/__init__.py", "setup": "setup.py", } __UpperCamelCase = "README.md" def _a ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) -> Tuple: """simple docstring""" with open(_lowerCamelCase , """r""" , encoding="""utf-8""" , newline="""\n""" ) as f: __snake_case : Union[str, Any] = f.read() __snake_case , __snake_case : List[Any] = REPLACE_PATTERNS[pattern] __snake_case : Optional[Any] = replace.replace("""VERSION""" , _lowerCamelCase ) __snake_case : Optional[Any] = re_pattern.sub(_lowerCamelCase , _lowerCamelCase ) with open(_lowerCamelCase , """w""" , encoding="""utf-8""" , newline="""\n""" ) as f: f.write(_lowerCamelCase ) def _a ( _lowerCamelCase ) -> Union[str, Any]: """simple docstring""" for folder, directories, fnames in os.walk(_lowerCamelCase ): # Removing some of the folders with non-actively maintained examples from the walk if "research_projects" in directories: directories.remove("""research_projects""" ) if "legacy" in directories: directories.remove("""legacy""" ) for fname in fnames: if fname.endswith(""".py""" ): update_version_in_file(os.path.join(_lowerCamelCase , _lowerCamelCase ) , _lowerCamelCase , pattern="""examples""" ) def _a ( _lowerCamelCase , _lowerCamelCase=False ) -> str: """simple docstring""" for pattern, fname in REPLACE_FILES.items(): update_version_in_file(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) if not patch: update_version_in_examples(_lowerCamelCase ) def _a ( ) -> Optional[int]: """simple docstring""" __snake_case : str = """🤗 Transformers currently provides the following architectures""" __snake_case : List[Any] = """1. Want to contribute a new model?""" with open(_lowerCamelCase , """r""" , encoding="""utf-8""" , newline="""\n""" ) as f: __snake_case : List[str] = f.readlines() # Find the start of the list. __snake_case : Optional[Any] = 0 while not lines[start_index].startswith(_start_prompt ): start_index += 1 start_index += 1 __snake_case : int = start_index # Update the lines in the model list. while not lines[index].startswith(_end_prompt ): if lines[index].startswith("""1.""" ): __snake_case : Optional[Any] = lines[index].replace( """https://huggingface.co/docs/transformers/main/model_doc""" , """https://huggingface.co/docs/transformers/model_doc""" , ) index += 1 with open(_lowerCamelCase , """w""" , encoding="""utf-8""" , newline="""\n""" ) as f: f.writelines(_lowerCamelCase ) def _a ( ) -> Union[str, Any]: """simple docstring""" with open(REPLACE_FILES["""init"""] , """r""" ) as f: __snake_case : List[Any] = f.read() __snake_case : str = REPLACE_PATTERNS["""init"""][0].search(_lowerCamelCase ).groups()[0] return packaging.version.parse(_lowerCamelCase ) def _a ( _lowerCamelCase=False ) -> int: """simple docstring""" __snake_case : List[Any] = get_version() if patch and default_version.is_devrelease: raise ValueError("""Can't create a patch version from the dev branch, checkout a released version!""" ) if default_version.is_devrelease: __snake_case : str = default_version.base_version elif patch: __snake_case : Optional[int] = F'''{default_version.major}.{default_version.minor}.{default_version.micro + 1}''' else: __snake_case : Dict = F'''{default_version.major}.{default_version.minor + 1}.0''' # Now let's ask nicely if that's the right one. __snake_case : Dict = input(F'''Which version are you releasing? [{default_version}]''' ) if len(_lowerCamelCase ) == 0: __snake_case : Any = default_version print(F'''Updating version to {version}.''' ) global_version_update(_lowerCamelCase , patch=_lowerCamelCase ) if not patch: print("""Cleaning main README, don't forget to run `make fix-copies`.""" ) clean_main_ref_in_model_list() def _a ( ) -> Tuple: """simple docstring""" __snake_case : Optional[Any] = get_version() __snake_case : Tuple = F'''{current_version.major}.{current_version.minor + 1}.0.dev0''' __snake_case : Union[str, Any] = current_version.base_version # Check with the user we got that right. __snake_case : int = input(F'''Which version are we developing now? [{dev_version}]''' ) if len(_lowerCamelCase ) == 0: __snake_case : Optional[int] = dev_version print(F'''Updating version to {version}.''' ) global_version_update(_lowerCamelCase ) print("""Cleaning main README, don't forget to run `make fix-copies`.""" ) clean_main_ref_in_model_list() if __name__ == "__main__": __UpperCamelCase = argparse.ArgumentParser() parser.add_argument("--post_release", action="store_true", help="Whether this is pre or post release.") parser.add_argument("--patch", action="store_true", help="Whether or not this is a patch release.") __UpperCamelCase = parser.parse_args() if not args.post_release: pre_release_work(patch=args.patch) elif args.patch: print("Nothing to do after a patch :-)") else: post_release_work()
26
0
"""simple docstring""" import json import os import unittest from transformers.models.biogpt.tokenization_biogpt import VOCAB_FILES_NAMES, BioGptTokenizer from transformers.testing_utils import slow from ...test_tokenization_common import TokenizerTesterMixin class snake_case_ ( __lowercase ,unittest.TestCase ): __lowerCAmelCase = BioGptTokenizer __lowerCAmelCase = False def snake_case_ ( self ): super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt a_ : List[Any] = [ """l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """w</w>""", """r</w>""", """t</w>""", """lo""", """low""", """er</w>""", """low</w>""", """lowest</w>""", """newer</w>""", """wider</w>""", """<unk>""", ] a_ : Optional[int] = dict(zip(a_ , range(len(a_ ) ) ) ) a_ : int = ["""l o 123""", """lo w 1456""", """e r</w> 1789""", """"""] a_ : Any = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["vocab_file"] ) a_ : int = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["merges_file"] ) with open(self.vocab_file , "w" ) as fp: fp.write(json.dumps(a_ ) ) with open(self.merges_file , "w" ) as fp: fp.write("\n".join(a_ ) ) def snake_case_ ( self , a_ ): a_ : Any = """lower newer""" a_ : Optional[int] = """lower newer""" return input_text, output_text def snake_case_ ( self ): a_ : List[str] = BioGptTokenizer(self.vocab_file , self.merges_file ) a_ : Any = """lower""" a_ : Tuple = ["""low""", """er</w>"""] a_ : Optional[int] = tokenizer.tokenize(a_ ) self.assertListEqual(a_ , a_ ) a_ : Tuple = tokens + ["""<unk>"""] a_ : Tuple = [1_4, 1_5, 2_0] self.assertListEqual(tokenizer.convert_tokens_to_ids(a_ ) , a_ ) @slow def snake_case_ ( self ): a_ : List[str] = BioGptTokenizer.from_pretrained("microsoft/biogpt" ) a_ : Tuple = tokenizer.encode("sequence builders" , add_special_tokens=a_ ) a_ : str = tokenizer.encode("multi-sequence build" , add_special_tokens=a_ ) a_ : int = tokenizer.build_inputs_with_special_tokens(a_ ) a_ : Tuple = tokenizer.build_inputs_with_special_tokens(a_ , a_ ) self.assertTrue(encoded_sentence == [2] + text ) self.assertTrue(encoded_pair == [2] + text + [2] + text_a )
237
'''simple docstring''' from unittest import TestCase from datasets import Sequence, Value from datasets.arrow_dataset import Dataset class _A ( __lowercase ): def lowercase__ ( self : Any ) -> str: """simple docstring""" return [ {"col_1": 3, "col_2": "a"}, {"col_1": 2, "col_2": "b"}, {"col_1": 1, "col_2": "c"}, {"col_1": 0, "col_2": "d"}, ] def lowercase__ ( self : str ) -> int: """simple docstring""" __snake_case : Union[str, Any] = {"""col_1""": [3, 2, 1, 0], """col_2""": ["""a""", """b""", """c""", """d"""]} return Dataset.from_dict(__magic_name__ ) def lowercase__ ( self : str ) -> List[Any]: """simple docstring""" __snake_case : Any = self._create_example_records() __snake_case : str = Dataset.from_list(__magic_name__ ) self.assertListEqual(dset.column_names , ["""col_1""", """col_2"""] ) for i, r in enumerate(__magic_name__ ): self.assertDictEqual(__magic_name__ , example_records[i] ) def lowercase__ ( self : Optional[Any] ) -> List[Any]: """simple docstring""" __snake_case : List[Any] = self._create_example_records() __snake_case : Dict = Dataset.from_list(__magic_name__ ) __snake_case : List[Any] = Dataset.from_dict({k: [r[k] for r in example_records] for k in example_records[0]} ) self.assertEqual(dset.info , dset_from_dict.info ) def lowercase__ ( self : str ) -> List[Any]: # checks what happens with missing columns """simple docstring""" __snake_case : Union[str, Any] = [{"""col_1""": 1}, {"""col_2""": """x"""}] __snake_case : Optional[int] = Dataset.from_list(__magic_name__ ) self.assertDictEqual(dset[0] , {"""col_1""": 1} ) self.assertDictEqual(dset[1] , {"""col_1""": None} ) # NB: first record is used for columns def lowercase__ ( self : List[str] ) -> Optional[Any]: # checks if the type can be inferred from the second record """simple docstring""" __snake_case : List[Any] = [{"""col_1""": []}, {"""col_1""": [1, 2]}] __snake_case : int = Dataset.from_list(__magic_name__ ) self.assertEqual(dset.info.features["""col_1"""] , Sequence(Value("""int64""" ) ) ) def lowercase__ ( self : int ) -> Union[str, Any]: """simple docstring""" __snake_case : Tuple = Dataset.from_list([] ) self.assertEqual(len(__magic_name__ ) , 0 ) self.assertListEqual(dset.column_names , [] )
26
0
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available UpperCamelCase__ = { '''configuration_table_transformer''': [ '''TABLE_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''TableTransformerConfig''', '''TableTransformerOnnxConfig''', ] } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase__ = [ '''TABLE_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TableTransformerForObjectDetection''', '''TableTransformerModel''', '''TableTransformerPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_table_transformer import ( TABLE_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TableTransformerConfig, TableTransformerOnnxConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_table_transformer import ( TABLE_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TableTransformerForObjectDetection, TableTransformerModel, TableTransformerPreTrainedModel, ) else: import sys UpperCamelCase__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
227
'''simple docstring''' import inspect import unittest import torch import torch.nn as nn from accelerate.hooks import ( AlignDevicesHook, ModelHook, SequentialHook, add_hook_to_module, attach_align_device_hook, remove_hook_from_module, remove_hook_from_submodules, ) from accelerate.test_utils import require_multi_gpu class _A ( nn.Module ): def __init__( self : List[str] ) -> Optional[Any]: """simple docstring""" super().__init__() __snake_case : List[Any] = nn.Linear(3 , 4 ) __snake_case : str = nn.BatchNormad(4 ) __snake_case : Optional[Any] = nn.Linear(4 , 5 ) def lowercase__ ( self : str , __magic_name__ : Dict ) -> List[str]: """simple docstring""" return self.lineara(self.batchnorm(self.lineara(__magic_name__ ) ) ) class _A ( __lowercase ): def lowercase__ ( self : List[str] , __magic_name__ : Tuple , *__magic_name__ : Dict , **__magic_name__ : Optional[Any] ) -> Tuple: """simple docstring""" return (args[0] + 1,) + args[1:], kwargs class _A ( __lowercase ): def lowercase__ ( self : str , __magic_name__ : Union[str, Any] , __magic_name__ : Tuple ) -> Union[str, Any]: """simple docstring""" return output + 1 class _A ( unittest.TestCase ): def lowercase__ ( self : Dict ) -> Any: """simple docstring""" __snake_case : int = ModelForTest() __snake_case : Tuple = ModelHook() add_hook_to_module(__magic_name__ , __magic_name__ ) self.assertEqual(test_model._hf_hook , __magic_name__ ) self.assertTrue(hasattr(__magic_name__ , """_old_forward""" ) ) # Check adding the hook did not change the name or the signature self.assertEqual(test_model.forward.__name__ , """forward""" ) self.assertListEqual(list(inspect.signature(test_model.forward ).parameters ) , ["""x"""] ) remove_hook_from_module(__magic_name__ ) self.assertFalse(hasattr(__magic_name__ , """_hf_hook""" ) ) self.assertFalse(hasattr(__magic_name__ , """_old_forward""" ) ) def lowercase__ ( self : Tuple ) -> List[str]: """simple docstring""" __snake_case : List[Any] = ModelForTest() __snake_case : Optional[int] = ModelHook() add_hook_to_module(__magic_name__ , __magic_name__ ) add_hook_to_module(__magic_name__ , __magic_name__ , append=__magic_name__ ) self.assertEqual(isinstance(test_model._hf_hook , __magic_name__ ) , __magic_name__ ) self.assertEqual(len(test_model._hf_hook.hooks ) , 2 ) self.assertTrue(hasattr(__magic_name__ , """_old_forward""" ) ) # Check adding the hook did not change the name or the signature self.assertEqual(test_model.forward.__name__ , """forward""" ) self.assertListEqual(list(inspect.signature(test_model.forward ).parameters ) , ["""x"""] ) remove_hook_from_module(__magic_name__ ) self.assertFalse(hasattr(__magic_name__ , """_hf_hook""" ) ) self.assertFalse(hasattr(__magic_name__ , """_old_forward""" ) ) def lowercase__ ( self : str ) -> Union[str, Any]: """simple docstring""" __snake_case : List[Any] = ModelForTest() __snake_case : Any = torch.randn(2 , 3 ) __snake_case : str = test_model(x + 1 ) __snake_case : int = test_model(x + 2 ) __snake_case : Union[str, Any] = PreForwardHook() add_hook_to_module(__magic_name__ , __magic_name__ ) __snake_case : int = test_model(__magic_name__ ) self.assertTrue(torch.allclose(__magic_name__ , __magic_name__ , atol=1E-5 ) ) # Attaching a hook to a model when it already has one replaces, does not chain __snake_case : Optional[int] = PreForwardHook() add_hook_to_module(__magic_name__ , __magic_name__ ) __snake_case : List[Any] = test_model(__magic_name__ ) self.assertTrue(torch.allclose(__magic_name__ , __magic_name__ , atol=1E-5 ) ) # You need to use the sequential hook to chain two or more hooks __snake_case : Optional[int] = SequentialHook(PreForwardHook() , PreForwardHook() ) add_hook_to_module(__magic_name__ , __magic_name__ ) __snake_case : List[str] = test_model(__magic_name__ ) assert torch.allclose(__magic_name__ , __magic_name__ , atol=1E-5 ) def lowercase__ ( self : Union[str, Any] ) -> List[str]: """simple docstring""" __snake_case : Union[str, Any] = ModelForTest() __snake_case : str = torch.randn(2 , 3 ) __snake_case : Any = test_model(__magic_name__ ) __snake_case : Any = PostForwardHook() add_hook_to_module(__magic_name__ , __magic_name__ ) __snake_case : Any = test_model(__magic_name__ ) self.assertTrue(torch.allclose(__magic_name__ , output + 1 , atol=1E-5 ) ) # Attaching a hook to a model when it already has one replaces, does not chain __snake_case : Any = PostForwardHook() add_hook_to_module(__magic_name__ , __magic_name__ ) __snake_case : Dict = test_model(__magic_name__ ) self.assertTrue(torch.allclose(__magic_name__ , output + 1 , atol=1E-5 ) ) # You need to use the sequential hook to chain two or more hooks __snake_case : str = SequentialHook(PostForwardHook() , PostForwardHook() ) add_hook_to_module(__magic_name__ , __magic_name__ ) __snake_case : int = test_model(__magic_name__ ) assert torch.allclose(__magic_name__ , output + 2 , atol=1E-5 ) def lowercase__ ( self : str ) -> int: """simple docstring""" __snake_case : Union[str, Any] = ModelForTest() __snake_case : int = torch.randn(2 , 3 ) __snake_case : Any = test_model(__magic_name__ ) __snake_case : Dict = PostForwardHook() add_hook_to_module(__magic_name__ , __magic_name__ ) __snake_case : List[Any] = test_model(__magic_name__ ) self.assertTrue(torch.allclose(__magic_name__ , output + 1 ) ) self.assertTrue(outputa.requires_grad ) __snake_case : Dict = True __snake_case : int = test_model(__magic_name__ ) self.assertFalse(outputa.requires_grad ) @require_multi_gpu def lowercase__ ( self : Tuple ) -> List[Any]: """simple docstring""" __snake_case : Tuple = ModelForTest() # Everything is on CPU self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.batchnorm.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) # This will move each submodule on different devices add_hook_to_module(model.lineara , AlignDevicesHook(execution_device=0 ) ) add_hook_to_module(model.batchnorm , AlignDevicesHook(execution_device=0 ) ) add_hook_to_module(model.lineara , AlignDevicesHook(execution_device=1 ) ) self.assertEqual(model.lineara.weight.device , torch.device(0 ) ) self.assertEqual(model.batchnorm.weight.device , torch.device(0 ) ) self.assertEqual(model.batchnorm.running_mean.device , torch.device(0 ) ) self.assertEqual(model.lineara.weight.device , torch.device(1 ) ) # We can still make a forward pass. The input does not need to be on any particular device __snake_case : Tuple = torch.randn(2 , 3 ) __snake_case : Union[str, Any] = model(__magic_name__ ) self.assertEqual(output.device , torch.device(1 ) ) # We can add a general hook to put back output on same device as input. add_hook_to_module(__magic_name__ , AlignDevicesHook(io_same_device=__magic_name__ ) ) __snake_case : Tuple = torch.randn(2 , 3 ).to(0 ) __snake_case : Any = model(__magic_name__ ) self.assertEqual(output.device , torch.device(0 ) ) def lowercase__ ( self : Union[str, Any] ) -> str: """simple docstring""" __snake_case : int = ModelForTest() # Everything is on CPU self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.batchnorm.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) # This will move each submodule on different devices __snake_case : List[str] = {"""execution_device""": 0 if torch.cuda.is_available() else """cpu""", """offload""": True} add_hook_to_module(model.lineara , AlignDevicesHook(**__magic_name__ ) ) add_hook_to_module(model.batchnorm , AlignDevicesHook(**__magic_name__ ) ) add_hook_to_module(model.lineara , AlignDevicesHook(**__magic_name__ ) ) # Parameters have been offloaded, so on the meta device self.assertEqual(model.lineara.weight.device , torch.device("""meta""" ) ) self.assertEqual(model.batchnorm.weight.device , torch.device("""meta""" ) ) self.assertEqual(model.lineara.weight.device , torch.device("""meta""" ) ) # Buffers are not included in the offload by default, so are on the execution device __snake_case : Any = torch.device(hook_kwargs["""execution_device"""] ) self.assertEqual(model.batchnorm.running_mean.device , __magic_name__ ) __snake_case : Dict = torch.randn(2 , 3 ) __snake_case : Any = model(__magic_name__ ) self.assertEqual(output.device , __magic_name__ ) # Removing hooks loads back the weights in the model. remove_hook_from_module(model.lineara ) remove_hook_from_module(model.batchnorm ) remove_hook_from_module(model.lineara ) self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.batchnorm.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) # Now test with buffers included in the offload __snake_case : int = { """execution_device""": 0 if torch.cuda.is_available() else """cpu""", """offload""": True, """offload_buffers""": True, } add_hook_to_module(model.lineara , AlignDevicesHook(**__magic_name__ ) ) add_hook_to_module(model.batchnorm , AlignDevicesHook(**__magic_name__ ) ) add_hook_to_module(model.lineara , AlignDevicesHook(**__magic_name__ ) ) # Parameters have been offloaded, so on the meta device, buffers included self.assertEqual(model.lineara.weight.device , torch.device("""meta""" ) ) self.assertEqual(model.batchnorm.weight.device , torch.device("""meta""" ) ) self.assertEqual(model.lineara.weight.device , torch.device("""meta""" ) ) self.assertEqual(model.batchnorm.running_mean.device , torch.device("""meta""" ) ) __snake_case : str = torch.randn(2 , 3 ) __snake_case : str = model(__magic_name__ ) self.assertEqual(output.device , __magic_name__ ) # Removing hooks loads back the weights in the model. remove_hook_from_module(model.lineara ) remove_hook_from_module(model.batchnorm ) remove_hook_from_module(model.lineara ) self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.batchnorm.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) def lowercase__ ( self : Dict ) -> str: """simple docstring""" __snake_case : Tuple = ModelForTest() # Everything is on CPU self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.batchnorm.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) # This will move each submodule on different devices __snake_case : Union[str, Any] = 0 if torch.cuda.is_available() else """cpu""" attach_align_device_hook(__magic_name__ , execution_device=__magic_name__ , offload=__magic_name__ ) # Parameters have been offloaded, so on the meta device self.assertEqual(model.lineara.weight.device , torch.device("""meta""" ) ) self.assertEqual(model.batchnorm.weight.device , torch.device("""meta""" ) ) self.assertEqual(model.lineara.weight.device , torch.device("""meta""" ) ) # Buffers are not included in the offload by default, so are on the execution device __snake_case : Union[str, Any] = torch.device(__magic_name__ ) self.assertEqual(model.batchnorm.running_mean.device , __magic_name__ ) __snake_case : Optional[int] = torch.randn(2 , 3 ) __snake_case : Dict = model(__magic_name__ ) self.assertEqual(output.device , __magic_name__ ) # Removing hooks loads back the weights in the model. remove_hook_from_submodules(__magic_name__ ) self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.batchnorm.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) # Now test with buffers included in the offload attach_align_device_hook(__magic_name__ , execution_device=__magic_name__ , offload=__magic_name__ , offload_buffers=__magic_name__ ) # Parameters have been offloaded, so on the meta device, buffers included self.assertEqual(model.lineara.weight.device , torch.device("""meta""" ) ) self.assertEqual(model.batchnorm.weight.device , torch.device("""meta""" ) ) self.assertEqual(model.lineara.weight.device , torch.device("""meta""" ) ) self.assertEqual(model.batchnorm.running_mean.device , torch.device("""meta""" ) ) __snake_case : Dict = torch.randn(2 , 3 ) __snake_case : Optional[int] = model(__magic_name__ ) self.assertEqual(output.device , __magic_name__ ) # Removing hooks loads back the weights in the model. remove_hook_from_submodules(__magic_name__ ) self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.batchnorm.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) def lowercase__ ( self : Any ) -> Union[str, Any]: """simple docstring""" __snake_case : Any = ModelForTest() # Everything is on CPU self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.batchnorm.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) # This will move each submodule on different devices __snake_case : str = 0 if torch.cuda.is_available() else """cpu""" attach_align_device_hook( __magic_name__ , execution_device=__magic_name__ , offload=__magic_name__ , weights_map=model.state_dict() ) # Parameters have been offloaded, so on the meta device self.assertEqual(model.lineara.weight.device , torch.device("""meta""" ) ) self.assertEqual(model.batchnorm.weight.device , torch.device("""meta""" ) ) self.assertEqual(model.lineara.weight.device , torch.device("""meta""" ) ) # Buffers are not included in the offload by default, so are on the execution device __snake_case : List[str] = torch.device(__magic_name__ ) self.assertEqual(model.batchnorm.running_mean.device , __magic_name__ ) __snake_case : Tuple = torch.randn(2 , 3 ) __snake_case : Optional[Any] = model(__magic_name__ ) self.assertEqual(output.device , __magic_name__ ) # Removing hooks loads back the weights in the model. remove_hook_from_submodules(__magic_name__ ) self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.batchnorm.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) # Now test with buffers included in the offload attach_align_device_hook( __magic_name__ , execution_device=__magic_name__ , offload=__magic_name__ , weights_map=model.state_dict() , offload_buffers=__magic_name__ , ) # Parameters have been offloaded, so on the meta device, buffers included self.assertEqual(model.lineara.weight.device , torch.device("""meta""" ) ) self.assertEqual(model.batchnorm.weight.device , torch.device("""meta""" ) ) self.assertEqual(model.lineara.weight.device , torch.device("""meta""" ) ) self.assertEqual(model.batchnorm.running_mean.device , torch.device("""meta""" ) ) __snake_case : List[str] = torch.randn(2 , 3 ) __snake_case : Dict = model(__magic_name__ ) self.assertEqual(output.device , __magic_name__ ) # Removing hooks loads back the weights in the model. remove_hook_from_submodules(__magic_name__ ) self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.batchnorm.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) )
26
0
import argparse import json from dataclasses import dataclass, field from functools import partial from pathlib import Path from typing import Callable, Dict, List, Tuple import timm import torch import torch.nn as nn from classy_vision.models.regnet import RegNet, RegNetParams, RegNetYaagf, RegNetYaagf, RegNetYaaagf from huggingface_hub import cached_download, hf_hub_url from torch import Tensor from vissl.models.model_helpers import get_trunk_forward_outputs from transformers import AutoImageProcessor, RegNetConfig, RegNetForImageClassification, RegNetModel from transformers.utils import logging logging.set_verbosity_info() SCREAMING_SNAKE_CASE__ : int = logging.get_logger() @dataclass class UpperCAmelCase_ : __lowerCamelCase = 42 __lowerCamelCase = field(default_factory=__lowercase ) __lowerCamelCase = field(default_factory=__lowercase ) def __UpperCAmelCase ( self , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ): UpperCAmelCase__ : Tuple = len(list(m.modules() ) ) == 1 or isinstance(_lowerCAmelCase , nn.Convad ) or isinstance(_lowerCAmelCase , nn.BatchNormad ) if has_not_submodules: self.traced.append(_lowerCAmelCase ) def __call__( self , _lowerCAmelCase ): for m in self.module.modules(): self.handles.append(m.register_forward_hook(self._forward_hook ) ) self.module(_lowerCAmelCase ) [x.remove() for x in self.handles] return self @property def __UpperCAmelCase ( self ): return list(filter(lambda _lowerCAmelCase : len(list(x.state_dict().keys() ) ) > 0 , self.traced ) ) @dataclass class UpperCAmelCase_ : __lowerCamelCase = 42 __lowerCamelCase = 42 __lowerCamelCase = 1 __lowerCamelCase = field(default_factory=__lowercase ) __lowerCamelCase = field(default_factory=__lowercase ) __lowerCamelCase = True def __call__( self , _lowerCAmelCase ): UpperCAmelCase__ : Any = Tracker(self.dest )(_lowerCAmelCase ).parametrized UpperCAmelCase__ : Dict = Tracker(self.src )(_lowerCAmelCase ).parametrized UpperCAmelCase__ : List[str] = list(filter(lambda _lowerCAmelCase : type(_lowerCAmelCase ) not in self.src_skip , _lowerCAmelCase ) ) UpperCAmelCase__ : List[str] = list(filter(lambda _lowerCAmelCase : type(_lowerCAmelCase ) not in self.dest_skip , _lowerCAmelCase ) ) if len(_lowerCAmelCase ) != len(_lowerCAmelCase ) and self.raise_if_mismatch: raise Exception( f"Numbers of operations are different. Source module has {len(_lowerCAmelCase )} operations while" f" destination module has {len(_lowerCAmelCase )}." ) for dest_m, src_m in zip(_lowerCAmelCase , _lowerCAmelCase ): dest_m.load_state_dict(src_m.state_dict() ) if self.verbose == 1: print(f"Transfered from={src_m} to={dest_m}" ) class UpperCAmelCase_ ( nn.Module ): def __init__( self , _lowerCAmelCase ): super().__init__() UpperCAmelCase__ : List[Tuple[str, nn.Module]] = [] # - get the stem feature_blocks.append(("""conv1""", model.stem) ) # - get all the feature blocks for k, v in model.trunk_output.named_children(): assert k.startswith("""block""" ), f"Unexpected layer name {k}" UpperCAmelCase__ : Optional[int] = len(_lowerCAmelCase ) + 1 feature_blocks.append((f"res{block_index}", v) ) UpperCAmelCase__ : str = nn.ModuleDict(_lowerCAmelCase ) def __UpperCAmelCase ( self , _lowerCAmelCase ): return get_trunk_forward_outputs( _lowerCAmelCase , out_feat_keys=_lowerCAmelCase , feature_blocks=self._feature_blocks , ) class UpperCAmelCase_ ( __lowercase ): def __UpperCAmelCase ( self , _lowerCAmelCase ): UpperCAmelCase__ : List[Any] = x.split("""-""" ) return x_split[0] + x_split[1] + "_" + "".join(x_split[2:] ) def __getitem__( self , _lowerCAmelCase ): if x not in self: UpperCAmelCase__ : Tuple = self.convert_name_to_timm(_lowerCAmelCase ) UpperCAmelCase__ : List[str] = partial(lambda: (timm.create_model(_lowerCAmelCase , pretrained=_lowerCAmelCase ).eval(), None) ) else: UpperCAmelCase__ : Tuple = super().__getitem__(_lowerCAmelCase ) return val class UpperCAmelCase_ ( __lowercase ): def __getitem__( self , _lowerCAmelCase ): if "seer" in x and "in1k" not in x: UpperCAmelCase__ : Any = RegNetModel else: UpperCAmelCase__ : Tuple = RegNetForImageClassification return val def _lowerCamelCase ( __lowerCamelCase , __lowerCamelCase , __lowerCamelCase ) -> Optional[int]: '''simple docstring''' for from_key, to_key in keys: UpperCAmelCase__ : int = from_state_dict[from_key].clone() print(F"Copied key={from_key} to={to_key}" ) return to_state_dict def _lowerCamelCase ( __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase = True , ) -> Union[str, Any]: '''simple docstring''' print(F"Converting {name}..." ) with torch.no_grad(): UpperCAmelCase__ : str = from_model_func() UpperCAmelCase__ : Optional[Any] = our_model_func(_lowerCamelCase ).eval() UpperCAmelCase__ : Dict = ModuleTransfer(src=_lowerCamelCase , dest=_lowerCamelCase , raise_if_mismatch=_lowerCamelCase ) UpperCAmelCase__ : Union[str, Any] = torch.randn((1, 3, 224, 224) ) module_transfer(_lowerCamelCase ) if from_state_dict is not None: UpperCAmelCase__ : Optional[Any] = [] # for seer - in1k finetuned we have to manually copy the head if "seer" in name and "in1k" in name: UpperCAmelCase__ : Any = [("""0.clf.0.weight""", """classifier.1.weight"""), ("""0.clf.0.bias""", """classifier.1.bias""")] UpperCAmelCase__ : List[str] = manually_copy_vissl_head(_lowerCamelCase , our_model.state_dict() , _lowerCamelCase ) our_model.load_state_dict(_lowerCamelCase ) UpperCAmelCase__ : str = our_model(_lowerCamelCase , output_hidden_states=_lowerCamelCase ) UpperCAmelCase__ : Any = ( our_outputs.logits if isinstance(_lowerCamelCase , _lowerCamelCase ) else our_outputs.last_hidden_state ) UpperCAmelCase__ : List[str] = from_model(_lowerCamelCase ) UpperCAmelCase__ : List[Any] = from_output[-1] if type(_lowerCamelCase ) is list else from_output # now since I don't want to use any config files, vissl seer model doesn't actually have an head, so let's just check the last hidden state if "seer" in name and "in1k" in name: UpperCAmelCase__ : Union[str, Any] = our_outputs.hidden_states[-1] assert torch.allclose(_lowerCamelCase , _lowerCamelCase ), "The model logits don't match the original one." if push_to_hub: our_model.push_to_hub( repo_path_or_name=save_directory / name , commit_message="""Add model""" , use_temp_dir=_lowerCamelCase , ) UpperCAmelCase__ : Optional[int] = 224 if """seer""" not in name else 384 # we can use the convnext one UpperCAmelCase__ : List[Any] = AutoImageProcessor.from_pretrained("""facebook/convnext-base-224-22k-1k""" , size=_lowerCamelCase ) image_processor.push_to_hub( repo_path_or_name=save_directory / name , commit_message="""Add image processor""" , use_temp_dir=_lowerCamelCase , ) print(F"Pushed {name}" ) def _lowerCamelCase ( __lowerCamelCase , __lowerCamelCase = None , __lowerCamelCase = True ) -> List[str]: '''simple docstring''' UpperCAmelCase__ : int = """imagenet-1k-id2label.json""" UpperCAmelCase__ : int = 1000 UpperCAmelCase__ : Any = (1, num_labels) UpperCAmelCase__ : Union[str, Any] = """huggingface/label-files""" UpperCAmelCase__ : List[str] = num_labels UpperCAmelCase__ : int = json.load(open(cached_download(hf_hub_url(_lowerCamelCase , _lowerCamelCase , repo_type="""dataset""" ) ) , """r""" ) ) UpperCAmelCase__ : Optional[int] = {int(_lowerCamelCase ): v for k, v in idalabel.items()} UpperCAmelCase__ : Optional[Any] = idalabel UpperCAmelCase__ : Any = {v: k for k, v in idalabel.items()} UpperCAmelCase__ : int = partial(_lowerCamelCase , num_labels=_lowerCamelCase , idalabel=_lowerCamelCase , labelaid=_lowerCamelCase ) UpperCAmelCase__ : Optional[Any] = { """regnet-x-002""": ImageNetPreTrainedConfig( depths=[1, 1, 4, 7] , hidden_sizes=[24, 56, 152, 368] , groups_width=8 , layer_type="""x""" ), """regnet-x-004""": ImageNetPreTrainedConfig( depths=[1, 2, 7, 12] , hidden_sizes=[32, 64, 160, 384] , groups_width=16 , layer_type="""x""" ), """regnet-x-006""": ImageNetPreTrainedConfig( depths=[1, 3, 5, 7] , hidden_sizes=[48, 96, 240, 528] , groups_width=24 , layer_type="""x""" ), """regnet-x-008""": ImageNetPreTrainedConfig( depths=[1, 3, 7, 5] , hidden_sizes=[64, 128, 288, 672] , groups_width=16 , layer_type="""x""" ), """regnet-x-016""": ImageNetPreTrainedConfig( depths=[2, 4, 10, 2] , hidden_sizes=[72, 168, 408, 912] , groups_width=24 , layer_type="""x""" ), """regnet-x-032""": ImageNetPreTrainedConfig( depths=[2, 6, 15, 2] , hidden_sizes=[96, 192, 432, 1008] , groups_width=48 , layer_type="""x""" ), """regnet-x-040""": ImageNetPreTrainedConfig( depths=[2, 5, 14, 2] , hidden_sizes=[80, 240, 560, 1360] , groups_width=40 , layer_type="""x""" ), """regnet-x-064""": ImageNetPreTrainedConfig( depths=[2, 4, 10, 1] , hidden_sizes=[168, 392, 784, 1624] , groups_width=56 , layer_type="""x""" ), """regnet-x-080""": ImageNetPreTrainedConfig( depths=[2, 5, 15, 1] , hidden_sizes=[80, 240, 720, 1920] , groups_width=120 , layer_type="""x""" ), """regnet-x-120""": ImageNetPreTrainedConfig( depths=[2, 5, 11, 1] , hidden_sizes=[224, 448, 896, 2240] , groups_width=112 , layer_type="""x""" ), """regnet-x-160""": ImageNetPreTrainedConfig( depths=[2, 6, 13, 1] , hidden_sizes=[256, 512, 896, 2048] , groups_width=128 , layer_type="""x""" ), """regnet-x-320""": ImageNetPreTrainedConfig( depths=[2, 7, 13, 1] , hidden_sizes=[336, 672, 1344, 2520] , groups_width=168 , layer_type="""x""" ), # y variant """regnet-y-002""": ImageNetPreTrainedConfig(depths=[1, 1, 4, 7] , hidden_sizes=[24, 56, 152, 368] , groups_width=8 ), """regnet-y-004""": ImageNetPreTrainedConfig( depths=[1, 3, 6, 6] , hidden_sizes=[48, 104, 208, 440] , groups_width=8 ), """regnet-y-006""": ImageNetPreTrainedConfig( depths=[1, 3, 7, 4] , hidden_sizes=[48, 112, 256, 608] , groups_width=16 ), """regnet-y-008""": ImageNetPreTrainedConfig( depths=[1, 3, 8, 2] , hidden_sizes=[64, 128, 320, 768] , groups_width=16 ), """regnet-y-016""": ImageNetPreTrainedConfig( depths=[2, 6, 17, 2] , hidden_sizes=[48, 120, 336, 888] , groups_width=24 ), """regnet-y-032""": ImageNetPreTrainedConfig( depths=[2, 5, 13, 1] , hidden_sizes=[72, 216, 576, 1512] , groups_width=24 ), """regnet-y-040""": ImageNetPreTrainedConfig( depths=[2, 6, 12, 2] , hidden_sizes=[128, 192, 512, 1088] , groups_width=64 ), """regnet-y-064""": ImageNetPreTrainedConfig( depths=[2, 7, 14, 2] , hidden_sizes=[144, 288, 576, 1296] , groups_width=72 ), """regnet-y-080""": ImageNetPreTrainedConfig( depths=[2, 4, 10, 1] , hidden_sizes=[168, 448, 896, 2016] , groups_width=56 ), """regnet-y-120""": ImageNetPreTrainedConfig( depths=[2, 5, 11, 1] , hidden_sizes=[224, 448, 896, 2240] , groups_width=112 ), """regnet-y-160""": ImageNetPreTrainedConfig( depths=[2, 4, 11, 1] , hidden_sizes=[224, 448, 1232, 3024] , groups_width=112 ), """regnet-y-320""": ImageNetPreTrainedConfig( depths=[2, 5, 12, 1] , hidden_sizes=[232, 696, 1392, 3712] , groups_width=232 ), # models created by SEER -> https://arxiv.org/abs/2202.08360 """regnet-y-320-seer""": RegNetConfig(depths=[2, 5, 12, 1] , hidden_sizes=[232, 696, 1392, 3712] , groups_width=232 ), """regnet-y-640-seer""": RegNetConfig(depths=[2, 5, 12, 1] , hidden_sizes=[328, 984, 1968, 4920] , groups_width=328 ), """regnet-y-1280-seer""": RegNetConfig( depths=[2, 7, 17, 1] , hidden_sizes=[528, 1056, 2904, 7392] , groups_width=264 ), """regnet-y-2560-seer""": RegNetConfig( depths=[3, 7, 16, 1] , hidden_sizes=[640, 1696, 2544, 5088] , groups_width=640 ), """regnet-y-10b-seer""": ImageNetPreTrainedConfig( depths=[2, 7, 17, 1] , hidden_sizes=[2020, 4040, 1_1110, 2_8280] , groups_width=1010 ), # finetuned on imagenet """regnet-y-320-seer-in1k""": ImageNetPreTrainedConfig( depths=[2, 5, 12, 1] , hidden_sizes=[232, 696, 1392, 3712] , groups_width=232 ), """regnet-y-640-seer-in1k""": ImageNetPreTrainedConfig( depths=[2, 5, 12, 1] , hidden_sizes=[328, 984, 1968, 4920] , groups_width=328 ), """regnet-y-1280-seer-in1k""": ImageNetPreTrainedConfig( depths=[2, 7, 17, 1] , hidden_sizes=[528, 1056, 2904, 7392] , groups_width=264 ), """regnet-y-2560-seer-in1k""": ImageNetPreTrainedConfig( depths=[3, 7, 16, 1] , hidden_sizes=[640, 1696, 2544, 5088] , groups_width=640 ), """regnet-y-10b-seer-in1k""": ImageNetPreTrainedConfig( depths=[2, 7, 17, 1] , hidden_sizes=[2020, 4040, 1_1110, 2_8280] , groups_width=1010 ), } UpperCAmelCase__ : List[str] = NameToOurModelFuncMap() UpperCAmelCase__ : List[str] = NameToFromModelFuncMap() # add seer weights logic def load_using_classy_vision(__lowerCamelCase , __lowerCamelCase ) -> Tuple[nn.Module, Dict]: UpperCAmelCase__ : List[str] = torch.hub.load_state_dict_from_url(_lowerCamelCase , model_dir=str(_lowerCamelCase ) , map_location="""cpu""" ) UpperCAmelCase__ : Optional[Any] = model_func() # check if we have a head, if yes add it UpperCAmelCase__ : str = files["""classy_state_dict"""]["""base_model"""]["""model"""] UpperCAmelCase__ : Any = model_state_dict["""trunk"""] model.load_state_dict(_lowerCamelCase ) return model.eval(), model_state_dict["heads"] # pretrained UpperCAmelCase__ : List[str] = partial( _lowerCamelCase , """https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_regnet32d/seer_regnet32gf_model_iteration244000.torch""" , lambda: FakeRegNetVisslWrapper(RegNetYaagf() ) , ) UpperCAmelCase__ : List[str] = partial( _lowerCamelCase , """https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_regnet64/seer_regnet64gf_model_final_checkpoint_phase0.torch""" , lambda: FakeRegNetVisslWrapper(RegNetYaagf() ) , ) UpperCAmelCase__ : Tuple = partial( _lowerCamelCase , """https://dl.fbaipublicfiles.com/vissl/model_zoo/swav_ig1b_regnet128Gf_cnstant_bs32_node16_sinkhorn10_proto16k_syncBN64_warmup8k/model_final_checkpoint_phase0.torch""" , lambda: FakeRegNetVisslWrapper(RegNetYaaagf() ) , ) UpperCAmelCase__ : str = partial( _lowerCamelCase , """https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_regnet10B/model_iteration124500_conso.torch""" , lambda: FakeRegNetVisslWrapper( RegNet(RegNetParams(depth=27 , group_width=1010 , w_a=1744 , w_a=620.83 , w_m=2.52 ) ) ) , ) # IN1K finetuned UpperCAmelCase__ : Dict = partial( _lowerCamelCase , """https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_finetuned/seer_regnet32_finetuned_in1k_model_final_checkpoint_phase78.torch""" , lambda: FakeRegNetVisslWrapper(RegNetYaagf() ) , ) UpperCAmelCase__ : List[Any] = partial( _lowerCamelCase , """https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_finetuned/seer_regnet64_finetuned_in1k_model_final_checkpoint_phase78.torch""" , lambda: FakeRegNetVisslWrapper(RegNetYaagf() ) , ) UpperCAmelCase__ : Union[str, Any] = partial( _lowerCamelCase , """https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_finetuned/seer_regnet128_finetuned_in1k_model_final_checkpoint_phase78.torch""" , lambda: FakeRegNetVisslWrapper(RegNetYaaagf() ) , ) UpperCAmelCase__ : str = partial( _lowerCamelCase , """https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_finetuned/seer_10b_finetuned_in1k_model_phase28_conso.torch""" , lambda: FakeRegNetVisslWrapper( RegNet(RegNetParams(depth=27 , group_width=1010 , w_a=1744 , w_a=620.83 , w_m=2.52 ) ) ) , ) if model_name: convert_weight_and_push( _lowerCamelCase , names_to_from_model_map[model_name] , names_to_ours_model_map[model_name] , names_to_config[model_name] , _lowerCamelCase , _lowerCamelCase , ) else: for model_name, config in names_to_config.items(): convert_weight_and_push( _lowerCamelCase , names_to_from_model_map[model_name] , names_to_ours_model_map[model_name] , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , ) return config, expected_shape if __name__ == "__main__": SCREAMING_SNAKE_CASE__ : Tuple = argparse.ArgumentParser() # Required parameters parser.add_argument( """--model_name""", default=None, type=str, help=( """The name of the model you wish to convert, it must be one of the supported regnet* architecture,""" """ currently: regnetx-*, regnety-*. If `None`, all of them will the converted.""" ), ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=Path, required=True, help="""Path to the output PyTorch model directory.""", ) parser.add_argument( """--push_to_hub""", default=True, type=bool, required=False, help="""If True, push model and image processor to the hub.""", ) SCREAMING_SNAKE_CASE__ : str = parser.parse_args() SCREAMING_SNAKE_CASE__ : str = args.pytorch_dump_folder_path pytorch_dump_folder_path.mkdir(exist_ok=True, parents=True) convert_weights_and_push(pytorch_dump_folder_path, args.model_name, args.push_to_hub)
79
'''simple docstring''' from __future__ import annotations __UpperCamelCase = [ [-1, 0], # left [0, -1], # down [1, 0], # right [0, 1], # up ] def _a ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , ) -> tuple[list[list[int]], list[list[int]]]: """simple docstring""" __snake_case : List[str] = [ [0 for col in range(len(grid[0] ) )] for row in range(len(_lowerCamelCase ) ) ] # the reference grid __snake_case : Tuple = 1 __snake_case : List[str] = [ [0 for col in range(len(grid[0] ) )] for row in range(len(_lowerCamelCase ) ) ] # the action grid __snake_case : List[str] = init[0] __snake_case : str = init[1] __snake_case : int = 0 __snake_case : int = g + heuristic[x][y] # cost from starting cell to destination cell __snake_case : List[str] = [[f, g, x, y]] __snake_case : Any = False # flag that is set when search is complete __snake_case : int = False # flag set if we can't find expand while not found and not resign: if len(_lowerCamelCase ) == 0: raise ValueError("""Algorithm is unable to find solution""" ) else: # to choose the least costliest action so as to move closer to the goal cell.sort() cell.reverse() __snake_case : Tuple = cell.pop() __snake_case : Optional[int] = next_cell[2] __snake_case : List[Any] = next_cell[3] __snake_case : int = next_cell[1] if x == goal[0] and y == goal[1]: __snake_case : Optional[Any] = True else: for i in range(len(_lowerCamelCase ) ): # to try out different valid actions __snake_case : Union[str, Any] = x + DIRECTIONS[i][0] __snake_case : str = y + DIRECTIONS[i][1] if xa >= 0 and xa < len(_lowerCamelCase ) and ya >= 0 and ya < len(grid[0] ): if closed[xa][ya] == 0 and grid[xa][ya] == 0: __snake_case : str = g + cost __snake_case : Tuple = ga + heuristic[xa][ya] cell.append([fa, ga, xa, ya] ) __snake_case : List[str] = 1 __snake_case : Optional[int] = i __snake_case : List[str] = [] __snake_case : Optional[int] = goal[0] __snake_case : List[Any] = goal[1] invpath.append([x, y] ) # we get the reverse path from here while x != init[0] or y != init[1]: __snake_case : Dict = x - DIRECTIONS[action[x][y]][0] __snake_case : int = y - DIRECTIONS[action[x][y]][1] __snake_case : Optional[int] = xa __snake_case : int = ya invpath.append([x, y] ) __snake_case : Optional[int] = [] for i in range(len(_lowerCamelCase ) ): path.append(invpath[len(_lowerCamelCase ) - 1 - i] ) return path, action if __name__ == "__main__": __UpperCamelCase = [ [0, 1, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles [0, 1, 0, 0, 0, 0], [0, 1, 0, 0, 1, 0], [0, 0, 0, 0, 1, 0], ] __UpperCamelCase = [0, 0] # all coordinates are given in format [y,x] __UpperCamelCase = [len(grid) - 1, len(grid[0]) - 1] __UpperCamelCase = 1 # the cost map which pushes the path closer to the goal __UpperCamelCase = [[0 for row in range(len(grid[0]))] for col in range(len(grid))] for i in range(len(grid)): for j in range(len(grid[0])): __UpperCamelCase = abs(i - goal[0]) + abs(j - goal[1]) if grid[i][j] == 1: # added extra penalty in the heuristic map __UpperCamelCase = 99 __UpperCamelCase , __UpperCamelCase = search(grid, init, goal, cost, heuristic) print("ACTION MAP") for i in range(len(action)): print(action[i]) for i in range(len(path)): print(path[i])
26
0
import unittest import numpy as np from transformers import RoFormerConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax.numpy as jnp from transformers.models.roformer.modeling_flax_roformer import ( FlaxRoFormerForMaskedLM, FlaxRoFormerForMultipleChoice, FlaxRoFormerForQuestionAnswering, FlaxRoFormerForSequenceClassification, FlaxRoFormerForTokenClassification, FlaxRoFormerModel, ) class snake_case_ ( unittest.TestCase ): '''simple docstring''' def __init__( self : Optional[Any] , __magic_name__ : List[Any] , __magic_name__ : int=13 , __magic_name__ : List[str]=7 , __magic_name__ : int=True , __magic_name__ : List[Any]=True , __magic_name__ : Optional[Any]=True , __magic_name__ : str=True , __magic_name__ : Union[str, Any]=99 , __magic_name__ : Dict=32 , __magic_name__ : Union[str, Any]=5 , __magic_name__ : str=4 , __magic_name__ : Optional[Any]=37 , __magic_name__ : List[str]="gelu" , __magic_name__ : Dict=0.1 , __magic_name__ : Optional[int]=0.1 , __magic_name__ : int=512 , __magic_name__ : str=16 , __magic_name__ : Any=2 , __magic_name__ : str=0.02 , __magic_name__ : Any=4 , ) -> Optional[int]: lowerCamelCase_ : Optional[Any] = parent lowerCamelCase_ : List[str] = batch_size lowerCamelCase_ : List[str] = seq_length lowerCamelCase_ : Optional[Any] = is_training lowerCamelCase_ : Optional[Any] = use_attention_mask lowerCamelCase_ : int = use_token_type_ids lowerCamelCase_ : Tuple = use_labels lowerCamelCase_ : Optional[int] = vocab_size lowerCamelCase_ : str = hidden_size lowerCamelCase_ : Union[str, Any] = num_hidden_layers lowerCamelCase_ : str = num_attention_heads lowerCamelCase_ : Union[str, Any] = intermediate_size lowerCamelCase_ : Any = hidden_act lowerCamelCase_ : List[str] = hidden_dropout_prob lowerCamelCase_ : Optional[Any] = attention_probs_dropout_prob lowerCamelCase_ : List[Any] = max_position_embeddings lowerCamelCase_ : List[Any] = type_vocab_size lowerCamelCase_ : Optional[int] = type_sequence_label_size lowerCamelCase_ : Tuple = initializer_range lowerCamelCase_ : int = num_choices def __SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> Tuple: lowerCamelCase_ : Dict = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) lowerCamelCase_ : int = None if self.use_attention_mask: lowerCamelCase_ : Dict = random_attention_mask([self.batch_size, self.seq_length] ) lowerCamelCase_ : Optional[int] = None if self.use_token_type_ids: lowerCamelCase_ : str = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) lowerCamelCase_ : 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 , is_decoder=__magic_name__ , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def __SCREAMING_SNAKE_CASE ( self : Dict ) -> List[Any]: lowerCamelCase_ : str = self.prepare_config_and_inputs() lowerCamelCase_ : Optional[int] = config_and_inputs lowerCamelCase_ : Any = {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": attention_mask} return config, inputs_dict @require_flax class snake_case_ ( __lowercase , unittest.TestCase ): '''simple docstring''' lowerCamelCase = True lowerCamelCase = ( ( FlaxRoFormerModel, FlaxRoFormerForMaskedLM, FlaxRoFormerForSequenceClassification, FlaxRoFormerForTokenClassification, FlaxRoFormerForMultipleChoice, FlaxRoFormerForQuestionAnswering, ) if is_flax_available() else () ) def __SCREAMING_SNAKE_CASE ( self : List[str] ) -> Union[str, Any]: lowerCamelCase_ : List[Any] = FlaxRoFormerModelTester(self ) @slow def __SCREAMING_SNAKE_CASE ( self : str ) -> int: for model_class_name in self.all_model_classes: lowerCamelCase_ : Optional[Any] = model_class_name.from_pretrained("junnyu/roformer_chinese_small" , from_pt=__magic_name__ ) lowerCamelCase_ : Dict = model(np.ones((1, 1) ) ) self.assertIsNotNone(__magic_name__ ) @require_flax class snake_case_ ( unittest.TestCase ): '''simple docstring''' @slow def __SCREAMING_SNAKE_CASE ( self : str ) -> Union[str, Any]: lowerCamelCase_ : str = FlaxRoFormerForMaskedLM.from_pretrained("junnyu/roformer_chinese_base" ) lowerCamelCase_ : Optional[Any] = jnp.array([[0, 1, 2, 3, 4, 5]] ) lowerCamelCase_ : int = model(__magic_name__ )[0] lowerCamelCase_ : Union[str, Any] = 5_0000 lowerCamelCase_ : Dict = (1, 6, vocab_size) self.assertEqual(output.shape , __magic_name__ ) lowerCamelCase_ : Optional[int] = jnp.array( [[[-0.1205, -1.0265, 0.2922], [-1.5134, 0.1974, 0.1519], [-5.0135, -3.9003, -0.8404]]] ) self.assertTrue(jnp.allclose(output[:, :3, :3] , __magic_name__ , atol=1e-4 ) )
488
'''simple docstring''' def _a ( _lowerCamelCase ) -> int: """simple docstring""" if not isinstance(_lowerCamelCase , _lowerCamelCase ): raise TypeError("""only integers accepted as input""" ) else: __snake_case : List[Any] = str(abs(_lowerCamelCase ) ) __snake_case : Union[str, Any] = [list(_lowerCamelCase ) for char in range(len(_lowerCamelCase ) )] for index in range(len(_lowerCamelCase ) ): num_transpositions[index].pop(_lowerCamelCase ) return max( int("""""".join(list(_lowerCamelCase ) ) ) for transposition in num_transpositions ) if __name__ == "__main__": __import__("doctest").testmod()
26
0
from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCAmelCase_ : str = logging.get_logger(__name__) UpperCAmelCase_ : Union[str, Any] = { '''MIT/ast-finetuned-audioset-10-10-0.4593''': ( '''https://huggingface.co/MIT/ast-finetuned-audioset-10-10-0.4593/resolve/main/config.json''' ), } class lowerCamelCase_ ( __lowercase ): _lowercase : Any = '''audio-spectrogram-transformer''' def __init__( self : List[Any] , __A : Optional[Any]=768 , __A : Optional[Any]=12 , __A : int=12 , __A : Union[str, Any]=3072 , __A : List[str]="gelu" , __A : Any=0.0 , __A : Dict=0.0 , __A : Tuple=0.0_2 , __A : Union[str, Any]=1e-1_2 , __A : str=16 , __A : Optional[Any]=True , __A : List[Any]=10 , __A : Any=10 , __A : Tuple=1024 , __A : Optional[int]=128 , **__A : int , ): super().__init__(**__A ) __A : Any = hidden_size __A : Union[str, Any] = num_hidden_layers __A : Dict = num_attention_heads __A : Optional[int] = intermediate_size __A : Optional[Any] = hidden_act __A : Optional[Any] = hidden_dropout_prob __A : Union[str, Any] = attention_probs_dropout_prob __A : Any = initializer_range __A : Tuple = layer_norm_eps __A : Dict = patch_size __A : Any = qkv_bias __A : List[Any] = frequency_stride __A : int = time_stride __A : Tuple = max_length __A : int = num_mel_bins
17
'''simple docstring''' from __future__ import annotations import math def _a ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) -> int: """simple docstring""" if depth < 0: raise ValueError("""Depth cannot be less than 0""" ) if not scores: raise ValueError("""Scores cannot be empty""" ) if depth == height: return scores[node_index] return ( max( minimax(depth + 1 , node_index * 2 , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) , minimax(depth + 1 , node_index * 2 + 1 , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) , ) if is_max else min( minimax(depth + 1 , node_index * 2 , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) , minimax(depth + 1 , node_index * 2 + 1 , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) , ) ) def _a ( ) -> None: """simple docstring""" __snake_case : Union[str, Any] = [90, 23, 6, 33, 21, 65, 123, 3_4423] __snake_case : Optional[int] = math.log(len(_lowerCamelCase ) , 2 ) print(F'''Optimal value : {minimax(0 , 0 , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase )}''' ) if __name__ == "__main__": import doctest doctest.testmod() main()
26
0
"""simple docstring""" from typing import Dict, List from nltk.translate import gleu_score import datasets from datasets import MetricInfo _A : List[Any] = """\\n@misc{wu2016googles,\n title={Google's Neural Machine Translation System: Bridging the Gap between Human and Machine Translation},\n author={Yonghui Wu and Mike Schuster and Zhifeng Chen and Quoc V. Le and Mohammad Norouzi and Wolfgang Macherey\n and Maxim Krikun and Yuan Cao and Qin Gao and Klaus Macherey and Jeff Klingner and Apurva Shah and Melvin\n Johnson and Xiaobing Liu and Łukasz Kaiser and Stephan Gouws and Yoshikiyo Kato and Taku Kudo and Hideto\n Kazawa and Keith Stevens and George Kurian and Nishant Patil and Wei Wang and Cliff Young and\n Jason Smith and Jason Riesa and Alex Rudnick and Oriol Vinyals and Greg Corrado and Macduff Hughes\n and Jeffrey Dean},\n year={2016},\n eprint={1609.08144},\n archivePrefix={arXiv},\n primaryClass={cs.CL}\n}\n""" _A : List[str] = """\\nThe BLEU score has some undesirable properties when used for single\nsentences, as it was designed to be a corpus measure. We therefore\nuse a slightly different score for our RL experiments which we call\nthe 'GLEU score'. For the GLEU score, we record all sub-sequences of\n1, 2, 3 or 4 tokens in output and target sequence (n-grams). We then\ncompute a recall, which is the ratio of the number of matching n-grams\nto the number of total n-grams in the target (ground truth) sequence,\nand a precision, which is the ratio of the number of matching n-grams\nto the number of total n-grams in the generated output sequence. Then\nGLEU score is simply the minimum of recall and precision. This GLEU\nscore's range is always between 0 (no matches) and 1 (all match) and\nit is symmetrical when switching output and target. According to\nour experiments, GLEU score correlates quite well with the BLEU\nmetric on a corpus level but does not have its drawbacks for our per\nsentence reward objective.\n""" _A : List[str] = """\\nComputes corpus-level Google BLEU (GLEU) score of translated segments against one or more references.\nInstead of averaging the sentence level GLEU scores (i.e. macro-average precision), Wu et al. (2016) sum up the matching\ntokens and the max of hypothesis and reference tokens for each sentence, then compute using the aggregate values.\n\nArgs:\n predictions (list of str): list of translations to score.\n Each translation should be tokenized into a list of tokens.\n references (list of list of str): list of lists of references for each translation.\n Each reference should be tokenized into a list of tokens.\n min_len (int): The minimum order of n-gram this function should extract. Defaults to 1.\n max_len (int): The maximum order of n-gram this function should extract. Defaults to 4.\n\nReturns:\n 'google_bleu': google_bleu score\n\nExamples:\n Example 1:\n >>> hyp1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'which',\n ... 'ensures', 'that', 'the', 'rubber', 'duck', 'always',\n ... 'disobeys', 'the', 'commands', 'of', 'the', 'cat']\n >>> ref1a = ['It', 'is', 'the', 'guiding', 'principle', 'which',\n ... 'guarantees', 'the', 'rubber', 'duck', 'forces', 'never',\n ... 'being', 'under', 'the', 'command', 'of', 'the', 'cat']\n\n >>> hyp2 = ['he', 'read', 'the', 'book', 'because', 'he', 'was',\n ... 'interested', 'in', 'world', 'history']\n >>> ref2a = ['he', 'was', 'interested', 'in', 'world', 'history',\n ... 'because', 'he', 'read', 'the', 'book']\n\n >>> list_of_references = [[ref1a], [ref2a]]\n >>> hypotheses = [hyp1, hyp2]\n >>> google_bleu = datasets.load_metric(\"google_bleu\")\n >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references)\n >>> print(round(results[\"google_bleu\"], 2))\n 0.44\n\n Example 2:\n >>> hyp1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'which',\n ... 'ensures', 'that', 'the', 'rubber', 'duck', 'always',\n ... 'disobeys', 'the', 'commands', 'of', 'the', 'cat']\n >>> ref1a = ['It', 'is', 'the', 'guiding', 'principle', 'which',\n ... 'guarantees', 'the', 'rubber', 'duck', 'forces', 'never',\n ... 'being', 'under', 'the', 'command', 'of', 'the', 'cat']\n >>> ref1b = ['It', 'is', 'a', 'guide', 'to', 'action', 'that',\n ... 'ensures', 'that', 'the', 'rubber', 'duck', 'will', 'never',\n ... 'heed', 'the', 'cat', 'commands']\n >>> ref1c = ['It', 'is', 'the', 'practical', 'guide', 'for', 'the',\n ... 'rubber', 'duck', 'army', 'never', 'to', 'heed', 'the', 'directions',\n ... 'of', 'the', 'cat']\n\n >>> hyp2 = ['he', 'read', 'the', 'book', 'because', 'he', 'was',\n ... 'interested', 'in', 'world', 'history']\n >>> ref2a = ['he', 'was', 'interested', 'in', 'world', 'history',\n ... 'because', 'he', 'read', 'the', 'book']\n\n >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]]\n >>> hypotheses = [hyp1, hyp2]\n >>> google_bleu = datasets.load_metric(\"google_bleu\")\n >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references)\n >>> print(round(results[\"google_bleu\"], 2))\n 0.61\n\n Example 3:\n >>> hyp1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'which',\n ... 'ensures', 'that', 'the', 'rubber', 'duck', 'always',\n ... 'disobeys', 'the', 'commands', 'of', 'the', 'cat']\n >>> ref1a = ['It', 'is', 'the', 'guiding', 'principle', 'which',\n ... 'guarantees', 'the', 'rubber', 'duck', 'forces', 'never',\n ... 'being', 'under', 'the', 'command', 'of', 'the', 'cat']\n >>> ref1b = ['It', 'is', 'a', 'guide', 'to', 'action', 'that',\n ... 'ensures', 'that', 'the', 'rubber', 'duck', 'will', 'never',\n ... 'heed', 'the', 'cat', 'commands']\n >>> ref1c = ['It', 'is', 'the', 'practical', 'guide', 'for', 'the',\n ... 'rubber', 'duck', 'army', 'never', 'to', 'heed', 'the', 'directions',\n ... 'of', 'the', 'cat']\n\n >>> hyp2 = ['he', 'read', 'the', 'book', 'because', 'he', 'was',\n ... 'interested', 'in', 'world', 'history']\n >>> ref2a = ['he', 'was', 'interested', 'in', 'world', 'history',\n ... 'because', 'he', 'read', 'the', 'book']\n\n >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]]\n >>> hypotheses = [hyp1, hyp2]\n >>> google_bleu = datasets.load_metric(\"google_bleu\")\n >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references, min_len=2)\n >>> print(round(results[\"google_bleu\"], 2))\n 0.53\n\n Example 4:\n >>> hyp1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'which',\n ... 'ensures', 'that', 'the', 'rubber', 'duck', 'always',\n ... 'disobeys', 'the', 'commands', 'of', 'the', 'cat']\n >>> ref1a = ['It', 'is', 'the', 'guiding', 'principle', 'which',\n ... 'guarantees', 'the', 'rubber', 'duck', 'forces', 'never',\n ... 'being', 'under', 'the', 'command', 'of', 'the', 'cat']\n >>> ref1b = ['It', 'is', 'a', 'guide', 'to', 'action', 'that',\n ... 'ensures', 'that', 'the', 'rubber', 'duck', 'will', 'never',\n ... 'heed', 'the', 'cat', 'commands']\n >>> ref1c = ['It', 'is', 'the', 'practical', 'guide', 'for', 'the',\n ... 'rubber', 'duck', 'army', 'never', 'to', 'heed', 'the', 'directions',\n ... 'of', 'the', 'cat']\n\n >>> hyp2 = ['he', 'read', 'the', 'book', 'because', 'he', 'was',\n ... 'interested', 'in', 'world', 'history']\n >>> ref2a = ['he', 'was', 'interested', 'in', 'world', 'history',\n ... 'because', 'he', 'read', 'the', 'book']\n\n >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]]\n >>> hypotheses = [hyp1, hyp2]\n >>> google_bleu = datasets.load_metric(\"google_bleu\")\n >>> results = google_bleu.compute(predictions=hypotheses,references=list_of_references, min_len=2, max_len=6)\n >>> print(round(results[\"google_bleu\"], 2))\n 0.4\n""" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION ) class a__ ( datasets.Metric ): def __magic_name__ ( self ): return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { "predictions": datasets.Sequence(datasets.Value("string" , id="token" ) , id="sequence" ), "references": datasets.Sequence( datasets.Sequence(datasets.Value("string" , id="token" ) , id="sequence" ) , id="references" ), } ) , ) def __magic_name__ ( self , _a , _a , _a = 1 , _a = 4 , ): return { "google_bleu": gleu_score.corpus_gleu( list_of_references=_a , hypotheses=_a , min_len=_a , max_len=_a ) }
361
'''simple docstring''' from __future__ import annotations def _a ( _lowerCamelCase , _lowerCamelCase = None , _lowerCamelCase = None ) -> None: """simple docstring""" if start is None: __snake_case : Optional[Any] = 0 if end is None: __snake_case : Optional[Any] = len(_lowerCamelCase ) - 1 if start >= end: return __snake_case : Tuple = (start + end) // 2 slowsort(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) slowsort(_lowerCamelCase , mid + 1 , _lowerCamelCase ) if sequence[end] < sequence[mid]: __snake_case , __snake_case : str = sequence[mid], sequence[end] slowsort(_lowerCamelCase , _lowerCamelCase , end - 1 ) if __name__ == "__main__": from doctest import testmod testmod()
26
0
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) lowerCamelCase : int = {"configuration_xlnet": ["XLNET_PRETRAINED_CONFIG_ARCHIVE_MAP", "XLNetConfig"]} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase : Optional[Any] = ["XLNetTokenizer"] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase : Optional[int] = ["XLNetTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase : Optional[Any] = [ "XLNET_PRETRAINED_MODEL_ARCHIVE_LIST", "XLNetForMultipleChoice", "XLNetForQuestionAnswering", "XLNetForQuestionAnsweringSimple", "XLNetForSequenceClassification", "XLNetForTokenClassification", "XLNetLMHeadModel", "XLNetModel", "XLNetPreTrainedModel", "load_tf_weights_in_xlnet", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase : Optional[Any] = [ "TF_XLNET_PRETRAINED_MODEL_ARCHIVE_LIST", "TFXLNetForMultipleChoice", "TFXLNetForQuestionAnsweringSimple", "TFXLNetForSequenceClassification", "TFXLNetForTokenClassification", "TFXLNetLMHeadModel", "TFXLNetMainLayer", "TFXLNetModel", "TFXLNetPreTrainedModel", ] if TYPE_CHECKING: from .configuration_xlnet import XLNET_PRETRAINED_CONFIG_ARCHIVE_MAP, XLNetConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xlnet import XLNetTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xlnet_fast import XLNetTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xlnet import ( XLNET_PRETRAINED_MODEL_ARCHIVE_LIST, XLNetForMultipleChoice, XLNetForQuestionAnswering, XLNetForQuestionAnsweringSimple, XLNetForSequenceClassification, XLNetForTokenClassification, XLNetLMHeadModel, XLNetModel, XLNetPreTrainedModel, load_tf_weights_in_xlnet, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_xlnet import ( TF_XLNET_PRETRAINED_MODEL_ARCHIVE_LIST, TFXLNetForMultipleChoice, TFXLNetForQuestionAnsweringSimple, TFXLNetForSequenceClassification, TFXLNetForTokenClassification, TFXLNetLMHeadModel, TFXLNetMainLayer, TFXLNetModel, TFXLNetPreTrainedModel, ) else: import sys lowerCamelCase : List[Any] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
70
'''simple docstring''' import doctest import logging import os import unittest from pathlib import Path from typing import List, Union import transformers from transformers.testing_utils import require_tf, require_torch, slow __UpperCamelCase = logging.getLogger() @unittest.skip('''Temporarily disable the doc tests.''' ) @require_torch @require_tf @slow class _A ( unittest.TestCase ): def lowercase__ ( self : Optional[int] , __magic_name__ : Path , __magic_name__ : Union[str, None] = None , __magic_name__ : Union[List[str], None] = None , __magic_name__ : Union[str, List[str], None] = None , __magic_name__ : bool = True , ) -> Optional[int]: """simple docstring""" __snake_case : Union[str, Any] = [file for file in os.listdir(__magic_name__ ) if os.path.isfile(os.path.join(__magic_name__ , __magic_name__ ) )] if identifier is not None: __snake_case : List[Any] = [file for file in files if identifier in file] if n_identifier is not None: if isinstance(__magic_name__ , __magic_name__ ): for n_ in n_identifier: __snake_case : Optional[int] = [file for file in files if n_ not in file] else: __snake_case : Tuple = [file for file in files if n_identifier not in file] __snake_case : Dict = ignore_files or [] ignore_files.append("""__init__.py""" ) __snake_case : List[str] = [file for file in files if file not in ignore_files] for file in files: # Open all files print("""Testing""" , __magic_name__ ) if only_modules: __snake_case : List[Any] = file.split(""".""" )[0] try: __snake_case : List[Any] = getattr(__magic_name__ , __magic_name__ ) __snake_case : Union[str, Any] = doctest.DocTestSuite(__magic_name__ ) __snake_case : Dict = unittest.TextTestRunner().run(__magic_name__ ) self.assertIs(len(result.failures ) , 0 ) except AttributeError: logger.info(f'''{module_identifier} is not a module.''' ) else: __snake_case : Tuple = doctest.testfile(str("""..""" / directory / file ) , optionflags=doctest.ELLIPSIS ) self.assertIs(result.failed , 0 ) def lowercase__ ( self : Union[str, Any] ) -> Any: """simple docstring""" __snake_case : List[Any] = Path("""src/transformers""" ) __snake_case : List[Any] = """modeling""" __snake_case : Union[str, Any] = [ """modeling_ctrl.py""", """modeling_tf_ctrl.py""", ] self.analyze_directory(__magic_name__ , identifier=__magic_name__ , ignore_files=__magic_name__ ) def lowercase__ ( self : Union[str, Any] ) -> int: """simple docstring""" __snake_case : Union[str, Any] = Path("""src/transformers""" ) __snake_case : Any = """tokenization""" self.analyze_directory(__magic_name__ , identifier=__magic_name__ ) def lowercase__ ( self : Union[str, Any] ) -> int: """simple docstring""" __snake_case : List[Any] = Path("""src/transformers""" ) __snake_case : List[str] = """configuration""" self.analyze_directory(__magic_name__ , identifier=__magic_name__ ) def lowercase__ ( self : Dict ) -> Dict: """simple docstring""" __snake_case : Tuple = Path("""src/transformers""" ) __snake_case : int = ["""configuration""", """modeling""", """tokenization"""] self.analyze_directory(__magic_name__ , n_identifier=__magic_name__ ) def lowercase__ ( self : Optional[int] ) -> Union[str, Any]: """simple docstring""" __snake_case : int = Path("""docs/source""" ) __snake_case : Optional[int] = ["""favicon.ico"""] self.analyze_directory(__magic_name__ , ignore_files=__magic_name__ , only_modules=__magic_name__ )
26
0
"""simple docstring""" from ...utils import is_torch_available, is_transformers_available if is_transformers_available() and is_torch_available(): from .pipeline_vq_diffusion import LearnedClassifierFreeSamplingEmbeddings, VQDiffusionPipeline
512
'''simple docstring''' import inspect from typing import Callable, List, Optional, Union import torch from transformers import ( CLIPImageProcessor, CLIPTextModel, CLIPTokenizer, WhisperForConditionalGeneration, WhisperProcessor, ) from diffusers import ( AutoencoderKL, DDIMScheduler, DiffusionPipeline, LMSDiscreteScheduler, PNDMScheduler, UNetaDConditionModel, ) from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion import StableDiffusionPipelineOutput from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker from diffusers.utils import logging __UpperCamelCase = logging.get_logger(__name__) # pylint: disable=invalid-name class _A ( __lowercase ): def __init__( self : str , __magic_name__ : WhisperForConditionalGeneration , __magic_name__ : WhisperProcessor , __magic_name__ : AutoencoderKL , __magic_name__ : CLIPTextModel , __magic_name__ : CLIPTokenizer , __magic_name__ : UNetaDConditionModel , __magic_name__ : Union[DDIMScheduler, PNDMScheduler, LMSDiscreteScheduler] , __magic_name__ : StableDiffusionSafetyChecker , __magic_name__ : CLIPImageProcessor , ) -> Union[str, Any]: """simple docstring""" super().__init__() if safety_checker is None: logger.warning( f'''You have disabled the safety checker for {self.__class__} by passing `safety_checker=None`. Ensure''' """ that you abide to the conditions of the Stable Diffusion license and do not expose unfiltered""" """ results in services or applications open to the public. Both the diffusers team and Hugging Face""" """ strongly recommend to keep the safety filter enabled in all public facing circumstances, disabling""" """ it only for use-cases that involve analyzing network behavior or auditing its results. For more""" """ information, please have a look at https://github.com/huggingface/diffusers/pull/254 .""" ) self.register_modules( speech_model=__magic_name__ , speech_processor=__magic_name__ , vae=__magic_name__ , text_encoder=__magic_name__ , tokenizer=__magic_name__ , unet=__magic_name__ , scheduler=__magic_name__ , feature_extractor=__magic_name__ , ) def lowercase__ ( self : Optional[Any] , __magic_name__ : Optional[Union[str, int]] = "auto" ) -> Union[str, Any]: """simple docstring""" if slice_size == "auto": __snake_case : str = self.unet.config.attention_head_dim // 2 self.unet.set_attention_slice(__magic_name__ ) def lowercase__ ( self : str ) -> Any: """simple docstring""" self.enable_attention_slicing(__magic_name__ ) @torch.no_grad() def __call__( self : Optional[int] , __magic_name__ : str , __magic_name__ : Dict=1_60_00 , __magic_name__ : int = 5_12 , __magic_name__ : int = 5_12 , __magic_name__ : int = 50 , __magic_name__ : float = 7.5 , __magic_name__ : Optional[Union[str, List[str]]] = None , __magic_name__ : Optional[int] = 1 , __magic_name__ : float = 0.0 , __magic_name__ : Optional[torch.Generator] = None , __magic_name__ : Optional[torch.FloatTensor] = None , __magic_name__ : Optional[str] = "pil" , __magic_name__ : bool = True , __magic_name__ : Optional[Callable[[int, int, torch.FloatTensor], None]] = None , __magic_name__ : int = 1 , **__magic_name__ : List[str] , ) -> int: """simple docstring""" __snake_case : List[Any] = self.speech_processor.feature_extractor( __magic_name__ , return_tensors="""pt""" , sampling_rate=__magic_name__ ).input_features.to(self.device ) __snake_case : List[str] = self.speech_model.generate(__magic_name__ , max_length=48_00_00 ) __snake_case : List[Any] = self.speech_processor.tokenizer.batch_decode(__magic_name__ , skip_special_tokens=__magic_name__ , normalize=__magic_name__ )[ 0 ] if isinstance(__magic_name__ , __magic_name__ ): __snake_case : Tuple = 1 elif isinstance(__magic_name__ , __magic_name__ ): __snake_case : Optional[int] = len(__magic_name__ ) else: raise ValueError(f'''`prompt` has to be of type `str` or `list` but is {type(__magic_name__ )}''' ) if height % 8 != 0 or width % 8 != 0: raise ValueError(f'''`height` and `width` have to be divisible by 8 but are {height} and {width}.''' ) if (callback_steps is None) or ( callback_steps is not None and (not isinstance(__magic_name__ , __magic_name__ ) or callback_steps <= 0) ): raise ValueError( f'''`callback_steps` has to be a positive integer but is {callback_steps} of type''' f''' {type(__magic_name__ )}.''' ) # get prompt text embeddings __snake_case : Dict = self.tokenizer( __magic_name__ , padding="""max_length""" , max_length=self.tokenizer.model_max_length , return_tensors="""pt""" , ) __snake_case : Optional[Any] = text_inputs.input_ids if text_input_ids.shape[-1] > self.tokenizer.model_max_length: __snake_case : Tuple = self.tokenizer.batch_decode(text_input_ids[:, self.tokenizer.model_max_length :] ) logger.warning( """The following part of your input was truncated because CLIP can only handle sequences up to""" f''' {self.tokenizer.model_max_length} tokens: {removed_text}''' ) __snake_case : Any = text_input_ids[:, : self.tokenizer.model_max_length] __snake_case : int = self.text_encoder(text_input_ids.to(self.device ) )[0] # duplicate text embeddings for each generation per prompt, using mps friendly method __snake_case , __snake_case , __snake_case : Any = text_embeddings.shape __snake_case : List[Any] = text_embeddings.repeat(1 , __magic_name__ , 1 ) __snake_case : Dict = text_embeddings.view(bs_embed * num_images_per_prompt , __magic_name__ , -1 ) # here `guidance_scale` is defined analog to the guidance weight `w` of equation (2) # of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1` # corresponds to doing no classifier free guidance. __snake_case : Optional[int] = guidance_scale > 1.0 # get unconditional embeddings for classifier free guidance if do_classifier_free_guidance: __snake_case : List[str] if negative_prompt is None: __snake_case : Optional[Any] = [""""""] * batch_size elif type(__magic_name__ ) is not type(__magic_name__ ): raise TypeError( f'''`negative_prompt` should be the same type to `prompt`, but got {type(__magic_name__ )} !=''' f''' {type(__magic_name__ )}.''' ) elif isinstance(__magic_name__ , __magic_name__ ): __snake_case : Dict = [negative_prompt] elif batch_size != len(__magic_name__ ): raise ValueError( f'''`negative_prompt`: {negative_prompt} has batch size {len(__magic_name__ )}, but `prompt`:''' f''' {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches''' """ the batch size of `prompt`.""" ) else: __snake_case : int = negative_prompt __snake_case : List[str] = text_input_ids.shape[-1] __snake_case : Any = self.tokenizer( __magic_name__ , padding="""max_length""" , max_length=__magic_name__ , truncation=__magic_name__ , return_tensors="""pt""" , ) __snake_case : Dict = self.text_encoder(uncond_input.input_ids.to(self.device ) )[0] # duplicate unconditional embeddings for each generation per prompt, using mps friendly method __snake_case : Optional[int] = uncond_embeddings.shape[1] __snake_case : Union[str, Any] = uncond_embeddings.repeat(1 , __magic_name__ , 1 ) __snake_case : Tuple = uncond_embeddings.view(batch_size * num_images_per_prompt , __magic_name__ , -1 ) # For classifier free guidance, we need to do two forward passes. # Here we concatenate the unconditional and text embeddings into a single batch # to avoid doing two forward passes __snake_case : Dict = torch.cat([uncond_embeddings, text_embeddings] ) # get the initial random noise unless the user supplied it # Unlike in other pipelines, latents need to be generated in the target device # for 1-to-1 results reproducibility with the CompVis implementation. # However this currently doesn't work in `mps`. __snake_case : List[Any] = (batch_size * num_images_per_prompt, self.unet.config.in_channels, height // 8, width // 8) __snake_case : List[Any] = text_embeddings.dtype if latents is None: if self.device.type == "mps": # randn does not exist on mps __snake_case : Optional[int] = torch.randn(__magic_name__ , generator=__magic_name__ , device="""cpu""" , dtype=__magic_name__ ).to( self.device ) else: __snake_case : int = torch.randn(__magic_name__ , generator=__magic_name__ , device=self.device , dtype=__magic_name__ ) else: if latents.shape != latents_shape: raise ValueError(f'''Unexpected latents shape, got {latents.shape}, expected {latents_shape}''' ) __snake_case : List[str] = latents.to(self.device ) # set timesteps self.scheduler.set_timesteps(__magic_name__ ) # Some schedulers like PNDM have timesteps as arrays # It's more optimized to move all timesteps to correct device beforehand __snake_case : Optional[int] = self.scheduler.timesteps.to(self.device ) # scale the initial noise by the standard deviation required by the scheduler __snake_case : str = latents * self.scheduler.init_noise_sigma # prepare extra kwargs for the scheduler step, since not all schedulers have the same signature # eta (η) is only used with the DDIMScheduler, it will be ignored for other schedulers. # eta corresponds to η in DDIM paper: https://arxiv.org/abs/2010.02502 # and should be between [0, 1] __snake_case : Tuple = """eta""" in set(inspect.signature(self.scheduler.step ).parameters.keys() ) __snake_case : List[str] = {} if accepts_eta: __snake_case : str = eta for i, t in enumerate(self.progress_bar(__magic_name__ ) ): # expand the latents if we are doing classifier free guidance __snake_case : Union[str, Any] = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents __snake_case : Dict = self.scheduler.scale_model_input(__magic_name__ , __magic_name__ ) # predict the noise residual __snake_case : Tuple = self.unet(__magic_name__ , __magic_name__ , encoder_hidden_states=__magic_name__ ).sample # perform guidance if do_classifier_free_guidance: __snake_case , __snake_case : str = noise_pred.chunk(2 ) __snake_case : Any = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) # compute the previous noisy sample x_t -> x_t-1 __snake_case : Optional[Any] = self.scheduler.step(__magic_name__ , __magic_name__ , __magic_name__ , **__magic_name__ ).prev_sample # call the callback, if provided if callback is not None and i % callback_steps == 0: callback(__magic_name__ , __magic_name__ , __magic_name__ ) __snake_case : int = 1 / 0.18215 * latents __snake_case : Optional[Any] = self.vae.decode(__magic_name__ ).sample __snake_case : Any = (image / 2 + 0.5).clamp(0 , 1 ) # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16 __snake_case : Any = image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() if output_type == "pil": __snake_case : Tuple = self.numpy_to_pil(__magic_name__ ) if not return_dict: return image return StableDiffusionPipelineOutput(images=__magic_name__ , nsfw_content_detected=__magic_name__ )
26
0
def __UpperCAmelCase ( __A ) -> str: '''simple docstring''' return " ".join(input_str.split()[::-1] ) if __name__ == "__main__": import doctest doctest.testmod()
475
'''simple docstring''' import os from huggingface_hub.constants import HUGGINGFACE_HUB_CACHE, hf_cache_home __UpperCamelCase = HUGGINGFACE_HUB_CACHE __UpperCamelCase = "config.json" __UpperCamelCase = "diffusion_pytorch_model.bin" __UpperCamelCase = "diffusion_flax_model.msgpack" __UpperCamelCase = "model.onnx" __UpperCamelCase = "diffusion_pytorch_model.safetensors" __UpperCamelCase = "weights.pb" __UpperCamelCase = "https://huggingface.co" __UpperCamelCase = default_cache_path __UpperCamelCase = "diffusers_modules" __UpperCamelCase = os.getenv("HF_MODULES_CACHE", os.path.join(hf_cache_home, "modules")) __UpperCamelCase = ["fp16", "non-ema"] __UpperCamelCase = ".self_attn"
26
0
import warnings from ...utils import logging from .image_processing_beit import BeitImageProcessor A : List[str] = logging.get_logger(__name__) class UpperCamelCase( __lowercase ): def __init__( self : str , *SCREAMING_SNAKE_CASE : Tuple , **SCREAMING_SNAKE_CASE : Tuple ) -> None: '''simple docstring''' warnings.warn( "The class BeitFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please" " use BeitImageProcessor instead." , SCREAMING_SNAKE_CASE , ) super().__init__(*SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE )
371
'''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() __UpperCamelCase = logging.get_logger(__name__) def _a ( _lowerCamelCase ) -> Any: """simple docstring""" __snake_case : Union[str, Any] = MobileNetVaConfig(layer_norm_eps=0.0_01 ) if "_quant" in model_name: raise ValueError("""Quantized models are not supported.""" ) __snake_case : List[Any] = re.match(R"""^mobilenet_v1_([^_]*)_([^_]*)$""" , _lowerCamelCase ) if matches: __snake_case : Optional[Any] = float(matches[1] ) __snake_case : Union[str, Any] = int(matches[2] ) # The TensorFlow version of MobileNetV1 predicts 1001 classes instead of # the usual 1000. The first class (index 0) is "background". __snake_case : Tuple = 1001 __snake_case : Any = """imagenet-1k-id2label.json""" __snake_case : Optional[Any] = """huggingface/label-files""" __snake_case : List[Any] = json.load(open(hf_hub_download(_lowerCamelCase , _lowerCamelCase , repo_type="""dataset""" ) , """r""" ) ) __snake_case : Dict = {int(_lowerCamelCase ) + 1: v for k, v in idalabel.items()} __snake_case : List[str] = """background""" __snake_case : List[str] = idalabel __snake_case : List[Any] = {v: k for k, v in idalabel.items()} return config def _a ( ) -> Optional[Any]: """simple docstring""" __snake_case : Optional[int] = """http://images.cocodataset.org/val2017/000000039769.jpg""" __snake_case : List[Any] = Image.open(requests.get(_lowerCamelCase , stream=_lowerCamelCase ).raw ) return im @torch.no_grad() def _a ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase=False ) -> Optional[Any]: """simple docstring""" __snake_case : Optional[int] = get_mobilenet_va_config(_lowerCamelCase ) # Load 🤗 model __snake_case : Optional[Any] = MobileNetVaForImageClassification(_lowerCamelCase ).eval() # Load weights from TensorFlow checkpoint load_tf_weights_in_mobilenet_va(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) # Check outputs on an image, prepared by MobileNetV1ImageProcessor __snake_case : Optional[int] = MobileNetVaImageProcessor( crop_size={"""width""": config.image_size, """height""": config.image_size} , size={"""shortest_edge""": config.image_size + 32} , ) __snake_case : Tuple = image_processor(images=prepare_img() , return_tensors="""pt""" ) __snake_case : Optional[Any] = model(**_lowerCamelCase ) __snake_case : List[Any] = outputs.logits assert logits.shape == (1, 1001) if model_name == "mobilenet_v1_1.0_224": __snake_case : str = torch.tensor([-4.17_39, -1.12_33, 3.12_05] ) elif model_name == "mobilenet_v1_0.75_192": __snake_case : Tuple = torch.tensor([-3.94_40, -2.31_41, -0.33_33] ) else: __snake_case : List[Any] = None if expected_logits is not None: assert torch.allclose(logits[0, :3] , _lowerCamelCase , atol=1E-4 ) Path(_lowerCamelCase ).mkdir(exist_ok=_lowerCamelCase ) print(F'''Saving model {model_name} to {pytorch_dump_folder_path}''' ) model.save_pretrained(_lowerCamelCase ) print(F'''Saving image processor to {pytorch_dump_folder_path}''' ) image_processor.save_pretrained(_lowerCamelCase ) if push_to_hub: print("""Pushing to the hub...""" ) __snake_case : Optional[Any] = """google/""" + model_name image_processor.push_to_hub(_lowerCamelCase ) model.push_to_hub(_lowerCamelCase ) if __name__ == "__main__": __UpperCamelCase = 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." ) __UpperCamelCase = parser.parse_args() convert_movilevit_checkpoint( args.model_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub )
26
0
lowercase_: Tuple = 0 # The first color of the flag. lowercase_: List[Any] = 1 # The second color of the flag. lowercase_: List[str] = 2 # The third color of the flag. lowercase_: int = (red, white, blue) def _lowercase ( UpperCAmelCase_): """simple docstring""" if not sequence: return [] if len(_lowerCamelCase) == 1: return list(_lowerCamelCase) snake_case__ : Any = 0 snake_case__ : Union[str, Any] = len(_lowerCamelCase) - 1 snake_case__ : List[Any] = 0 while mid <= high: if sequence[mid] == colors[0]: snake_case__ : List[Any] = sequence[mid], sequence[low] low += 1 mid += 1 elif sequence[mid] == colors[1]: mid += 1 elif sequence[mid] == colors[2]: snake_case__ : Optional[Any] = sequence[high], sequence[mid] high -= 1 else: snake_case__ : Tuple = F'The elements inside the sequence must contains only {colors} values' raise ValueError(_lowerCamelCase) return sequence if __name__ == "__main__": import doctest doctest.testmod() lowercase_: str = input('Enter numbers separated by commas:\n').strip() lowercase_: Union[str, Any] = [int(item.strip()) for item in user_input.split(',')] print(F"""{dutch_national_flag_sort(unsorted)}""")
648
'''simple docstring''' from sklearn.metrics import recall_score import datasets __UpperCamelCase = "\nRecall is the fraction of the positive examples that were correctly labeled by the model as positive. It can be computed with the equation:\nRecall = TP / (TP + FN)\nWhere TP is the true positives and FN is the false negatives.\n" __UpperCamelCase = "\nArgs:\n- **predictions** (`list` of `int`): The predicted labels.\n- **references** (`list` of `int`): The ground truth labels.\n- **labels** (`list` of `int`): The set of labels to include when `average` is not set to `binary`, and their order when average is `None`. Labels present in the data can be excluded in this input, for example to calculate a multiclass average ignoring a majority negative class, while labels not present in the data will result in 0 components in a macro average. For multilabel targets, labels are column indices. By default, all labels in y_true and y_pred are used in sorted order. Defaults to None.\n- **pos_label** (`int`): The class label to use as the 'positive class' when calculating the recall. Defaults to `1`.\n- **average** (`string`): This parameter is required for multiclass/multilabel targets. If None, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data. Defaults to `'binary'`.\n - `'binary'`: Only report results for the class specified by `pos_label`. This is applicable only if the target labels and predictions are binary.\n - `'micro'`: Calculate metrics globally by counting the total true positives, false negatives, and false positives.\n - `'macro'`: Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account.\n - `'weighted'`: Calculate metrics for each label, and find their average weighted by support (the number of true instances for each label). This alters `'macro'` to account for label imbalance. Note that it can result in an F-score that is not between precision and recall.\n - `'samples'`: Calculate metrics for each instance, and find their average (only meaningful for multilabel classification).\n- **sample_weight** (`list` of `float`): Sample weights Defaults to `None`.\n- **zero_division** (): Sets the value to return when there is a zero division. Defaults to .\n - `'warn'`: If there is a zero division, the return value is `0`, but warnings are also raised.\n - `0`: If there is a zero division, the return value is `0`.\n - `1`: If there is a zero division, the return value is `1`.\n\nReturns:\n- **recall** (`float`, or `array` of `float`): Either the general recall score, or the recall scores for individual classes, depending on the values input to `labels` and `average`. Minimum possible value is 0. Maximum possible value is 1. A higher recall means that more of the positive examples have been labeled correctly. Therefore, a higher recall is generally considered better.\n\nExamples:\n\n Example 1-A simple example with some errors\n >>> recall_metric = datasets.load_metric('recall')\n >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1])\n >>> print(results)\n {'recall': 0.6666666666666666}\n\n Example 2-The same example as Example 1, but with `pos_label=0` instead of the default `pos_label=1`.\n >>> recall_metric = datasets.load_metric('recall')\n >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1], pos_label=0)\n >>> print(results)\n {'recall': 0.5}\n\n Example 3-The same example as Example 1, but with `sample_weight` included.\n >>> recall_metric = datasets.load_metric('recall')\n >>> sample_weight = [0.9, 0.2, 0.9, 0.3, 0.8]\n >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1], sample_weight=sample_weight)\n >>> print(results)\n {'recall': 0.55}\n\n Example 4-A multiclass example, using different averages.\n >>> recall_metric = datasets.load_metric('recall')\n >>> predictions = [0, 2, 1, 0, 0, 1]\n >>> references = [0, 1, 2, 0, 1, 2]\n >>> results = recall_metric.compute(predictions=predictions, references=references, average='macro')\n >>> print(results)\n {'recall': 0.3333333333333333}\n >>> results = recall_metric.compute(predictions=predictions, references=references, average='micro')\n >>> print(results)\n {'recall': 0.3333333333333333}\n >>> results = recall_metric.compute(predictions=predictions, references=references, average='weighted')\n >>> print(results)\n {'recall': 0.3333333333333333}\n >>> results = recall_metric.compute(predictions=predictions, references=references, average=None)\n >>> print(results)\n {'recall': array([1., 0., 0.])}\n" __UpperCamelCase = "\n@article{scikit-learn, title={Scikit-learn: Machine Learning in {P}ython}, author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V. and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P. and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.}, journal={Journal of Machine Learning Research}, volume={12}, pages={2825--2830}, year={2011}\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class _A ( datasets.Metric ): def lowercase__ ( self : Optional[int] ) -> Any: """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": datasets.Sequence(datasets.Value("""int32""" ) ), """references""": datasets.Sequence(datasets.Value("""int32""" ) ), } if self.config_name == """multilabel""" else { """predictions""": datasets.Value("""int32""" ), """references""": datasets.Value("""int32""" ), } ) , reference_urls=["""https://scikit-learn.org/stable/modules/generated/sklearn.metrics.recall_score.html"""] , ) def lowercase__ ( self : Tuple , __magic_name__ : int , __magic_name__ : Union[str, Any] , __magic_name__ : Any=None , __magic_name__ : Optional[Any]=1 , __magic_name__ : List[str]="binary" , __magic_name__ : Tuple=None , __magic_name__ : Dict="warn" , ) -> Any: """simple docstring""" __snake_case : Tuple = recall_score( __magic_name__ , __magic_name__ , labels=__magic_name__ , pos_label=__magic_name__ , average=__magic_name__ , sample_weight=__magic_name__ , zero_division=__magic_name__ , ) return {"recall": float(__magic_name__ ) if score.size == 1 else score}
26
0
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging SCREAMING_SNAKE_CASE_ = logging.get_logger(__name__) SCREAMING_SNAKE_CASE_ = { """microsoft/trocr-base-handwritten""": ( """https://huggingface.co/microsoft/trocr-base-handwritten/resolve/main/config.json""" ), # See all TrOCR models at https://huggingface.co/models?filter=trocr } class snake_case_ ( __lowercase ): __lowerCAmelCase = '''trocr''' __lowerCAmelCase = ['''past_key_values'''] __lowerCAmelCase = { '''num_attention_heads''': '''decoder_attention_heads''', '''hidden_size''': '''d_model''', '''num_hidden_layers''': '''decoder_layers''', } def __init__( self , a_=5_0_2_6_5 , a_=1_0_2_4 , a_=1_2 , a_=1_6 , a_=4_0_9_6 , a_="gelu" , a_=5_1_2 , a_=0.1 , a_=0.0 , a_=0.0 , a_=2 , a_=0.02 , a_=0.0 , a_=True , a_=False , a_=True , a_=True , a_=1 , a_=0 , a_=2 , **a_ , ): a_ : Optional[Any] = vocab_size a_ : Union[str, Any] = d_model a_ : List[Any] = decoder_layers a_ : Optional[Any] = decoder_attention_heads a_ : Optional[int] = decoder_ffn_dim a_ : Optional[int] = activation_function a_ : Dict = max_position_embeddings a_ : Optional[int] = dropout a_ : str = attention_dropout a_ : List[str] = activation_dropout a_ : str = init_std a_ : List[str] = decoder_layerdrop a_ : Any = use_cache a_ : int = scale_embedding a_ : List[Any] = use_learned_position_embeddings a_ : int = layernorm_embedding super().__init__( pad_token_id=a_ , bos_token_id=a_ , eos_token_id=a_ , decoder_start_token_id=a_ , **a_ , )
237
'''simple docstring''' from sklearn.metrics import matthews_corrcoef import datasets __UpperCamelCase = "\nCompute the Matthews correlation coefficient (MCC)\n\nThe Matthews correlation coefficient is used in machine learning as a\nmeasure of the quality of binary and multiclass classifications. It takes\ninto account true and false positives and negatives and is generally\nregarded as a balanced measure which can be used even if the classes are of\nvery different sizes. The MCC is in essence a correlation coefficient value\nbetween -1 and +1. A coefficient of +1 represents a perfect prediction, 0\nan average random prediction and -1 an inverse prediction. The statistic\nis also known as the phi coefficient. [source: Wikipedia]\n" __UpperCamelCase = "\nArgs:\n predictions (list of int): Predicted labels, as returned by a model.\n references (list of int): Ground truth labels.\n sample_weight (list of int, float, or bool): Sample weights. Defaults to `None`.\nReturns:\n matthews_correlation (dict containing float): Matthews correlation.\nExamples:\n Example 1, a basic example with only predictions and references as inputs:\n >>> matthews_metric = datasets.load_metric(\"matthews_correlation\")\n >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],\n ... predictions=[1, 2, 2, 0, 3, 3])\n >>> print(round(results['matthews_correlation'], 2))\n 0.54\n\n Example 2, the same example as above, but also including sample weights:\n >>> matthews_metric = datasets.load_metric(\"matthews_correlation\")\n >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],\n ... predictions=[1, 2, 2, 0, 3, 3],\n ... sample_weight=[0.5, 3, 1, 1, 1, 2])\n >>> print(round(results['matthews_correlation'], 2))\n 0.1\n\n Example 3, the same example as above, but with sample weights that cause a negative correlation:\n >>> matthews_metric = datasets.load_metric(\"matthews_correlation\")\n >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],\n ... predictions=[1, 2, 2, 0, 3, 3],\n ... sample_weight=[0.5, 1, 0, 0, 0, 1])\n >>> print(round(results['matthews_correlation'], 2))\n -0.25\n" __UpperCamelCase = "\\n@article{scikit-learn,\n title={Scikit-learn: Machine Learning in {P}ython},\n author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V.\n and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P.\n and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and\n Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.},\n journal={Journal of Machine Learning Research},\n volume={12},\n pages={2825--2830},\n year={2011}\n}\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class _A ( datasets.Metric ): def lowercase__ ( self : Tuple ) -> Dict: """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": datasets.Value("""int32""" ), """references""": datasets.Value("""int32""" ), } ) , reference_urls=[ """https://scikit-learn.org/stable/modules/generated/sklearn.metrics.matthews_corrcoef.html""" ] , ) def lowercase__ ( self : List[Any] , __magic_name__ : Tuple , __magic_name__ : List[Any] , __magic_name__ : Union[str, Any]=None ) -> Optional[int]: """simple docstring""" return { "matthews_correlation": float(matthews_corrcoef(__magic_name__ , __magic_name__ , sample_weight=__magic_name__ ) ), }
26
0
"""simple docstring""" UpperCamelCase__ = '''Alexander Joslin''' import operator as op from .stack import Stack def UpperCAmelCase ( snake_case : List[str] ): _lowerCAmelCase:Dict = {"""*""": op.mul, """/""": op.truediv, """+""": op.add, """-""": op.sub} _lowerCAmelCase:Stack[int] = Stack() _lowerCAmelCase:Stack[str] = Stack() for i in equation: if i.isdigit(): # RULE 1 operand_stack.push(int(_lowerCamelCase ) ) elif i in operators: # RULE 2 operator_stack.push(_lowerCamelCase ) elif i == ")": # RULE 4 _lowerCAmelCase:Dict = operator_stack.peek() operator_stack.pop() _lowerCAmelCase:Dict = operand_stack.peek() operand_stack.pop() _lowerCAmelCase:Tuple = operand_stack.peek() operand_stack.pop() _lowerCAmelCase:Optional[Any] = operators[opr](_lowerCamelCase , _lowerCamelCase ) operand_stack.push(_lowerCamelCase ) # RULE 5 return operand_stack.peek() if __name__ == "__main__": UpperCamelCase__ = '''(5 + ((4 * 2) * (2 + 3)))''' # answer = 45 print(F"{equation} = {dijkstras_two_stack_algorithm(equation)}")
227
'''simple docstring''' import json import os from pathlib import Path import pytest from datasets.download.download_config import DownloadConfig from datasets.download.download_manager import DownloadManager from datasets.utils.file_utils import hash_url_to_filename __UpperCamelCase = "http://www.mocksite.com/file1.txt" __UpperCamelCase = "\"text\": [\"foo\", \"foo\"]" __UpperCamelCase = "6d8ce9aa78a471c7477201efbeabd3bb01ac2e7d100a6dc024ba1608361f90a8" class _A : lowercase__: str = 200 lowercase__: List[str] = {'''Content-Length''': '''100'''} lowercase__: Union[str, Any] = {} def lowercase__ ( self : Any , **__magic_name__ : List[Any] ) -> Dict: """simple docstring""" return [bytes(__magic_name__ , """utf-8""" )] def _a ( *_lowerCamelCase , **_lowerCamelCase ) -> List[str]: """simple docstring""" return MockResponse() @pytest.mark.parametrize("""urls_type""" , [str, list, dict] ) def _a ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) -> List[str]: """simple docstring""" import requests monkeypatch.setattr(_lowerCamelCase , """request""" , _lowerCamelCase ) __snake_case : Union[str, Any] = URL if issubclass(_lowerCamelCase , _lowerCamelCase ): __snake_case : str = url elif issubclass(_lowerCamelCase , _lowerCamelCase ): __snake_case : Dict = [url] elif issubclass(_lowerCamelCase , _lowerCamelCase ): __snake_case : Union[str, Any] = {"""train""": url} __snake_case : Dict = """dummy""" __snake_case : List[str] = """downloads""" __snake_case : List[Any] = tmp_path __snake_case : List[Any] = DownloadConfig( cache_dir=os.path.join(_lowerCamelCase , _lowerCamelCase ) , use_etag=_lowerCamelCase , ) __snake_case : List[str] = DownloadManager(dataset_name=_lowerCamelCase , download_config=_lowerCamelCase ) __snake_case : int = dl_manager.download(_lowerCamelCase ) __snake_case : Tuple = urls for downloaded_paths in [downloaded_paths]: if isinstance(_lowerCamelCase , _lowerCamelCase ): __snake_case : Any = [downloaded_paths] __snake_case : List[Any] = [urls] elif isinstance(_lowerCamelCase , _lowerCamelCase ): assert "train" in downloaded_paths.keys() __snake_case : Tuple = downloaded_paths.values() __snake_case : Optional[int] = urls.values() assert downloaded_paths for downloaded_path, input_url in zip(_lowerCamelCase , _lowerCamelCase ): assert downloaded_path == dl_manager.downloaded_paths[input_url] __snake_case : List[str] = Path(_lowerCamelCase ) __snake_case : Any = downloaded_path.parts assert parts[-1] == HASH assert parts[-2] == cache_subdir assert downloaded_path.exists() __snake_case : Union[str, Any] = downloaded_path.read_text() assert content == CONTENT __snake_case : List[str] = downloaded_path.with_suffix(""".json""" ) assert metadata_downloaded_path.exists() __snake_case : Union[str, Any] = json.loads(metadata_downloaded_path.read_text() ) assert metadata_content == {"url": URL, "etag": None} @pytest.mark.parametrize("""paths_type""" , [str, list, dict] ) def _a ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) -> List[Any]: """simple docstring""" __snake_case : Any = str(_lowerCamelCase ) if issubclass(_lowerCamelCase , _lowerCamelCase ): __snake_case : Optional[int] = filename elif issubclass(_lowerCamelCase , _lowerCamelCase ): __snake_case : Tuple = [filename] elif issubclass(_lowerCamelCase , _lowerCamelCase ): __snake_case : Dict = {"""train""": filename} __snake_case : Optional[Any] = """dummy""" __snake_case : List[Any] = xz_file.parent __snake_case : int = """extracted""" __snake_case : Dict = DownloadConfig( cache_dir=_lowerCamelCase , use_etag=_lowerCamelCase , ) __snake_case : List[str] = DownloadManager(dataset_name=_lowerCamelCase , download_config=_lowerCamelCase ) __snake_case : Optional[Any] = dl_manager.extract(_lowerCamelCase ) __snake_case : Union[str, Any] = paths for extracted_paths in [extracted_paths]: if isinstance(_lowerCamelCase , _lowerCamelCase ): __snake_case : Dict = [extracted_paths] __snake_case : int = [paths] elif isinstance(_lowerCamelCase , _lowerCamelCase ): assert "train" in extracted_paths.keys() __snake_case : int = extracted_paths.values() __snake_case : int = paths.values() assert extracted_paths for extracted_path, input_path in zip(_lowerCamelCase , _lowerCamelCase ): assert extracted_path == dl_manager.extracted_paths[input_path] __snake_case : Any = Path(_lowerCamelCase ) __snake_case : str = extracted_path.parts assert parts[-1] == hash_url_to_filename(_lowerCamelCase , etag=_lowerCamelCase ) assert parts[-2] == extracted_subdir assert extracted_path.exists() __snake_case : Optional[int] = extracted_path.read_text() __snake_case : str = text_file.read_text() assert extracted_file_content == expected_file_content def _a ( _lowerCamelCase , _lowerCamelCase ) -> Optional[int]: """simple docstring""" assert path.endswith(""".jsonl""" ) for num_items, line in enumerate(_lowerCamelCase , start=1 ): __snake_case : Tuple = json.loads(line.decode("""utf-8""" ) ) assert item.keys() == {"col_1", "col_2", "col_3"} assert num_items == 4 @pytest.mark.parametrize("""archive_jsonl""" , ["""tar_jsonl_path""", """zip_jsonl_path"""] ) def _a ( _lowerCamelCase , _lowerCamelCase ) -> Optional[int]: """simple docstring""" __snake_case : Any = request.getfixturevalue(_lowerCamelCase ) __snake_case : str = DownloadManager() for num_jsonl, (path, file) in enumerate(dl_manager.iter_archive(_lowerCamelCase ) , start=1 ): _test_jsonl(_lowerCamelCase , _lowerCamelCase ) assert num_jsonl == 2 @pytest.mark.parametrize("""archive_nested_jsonl""" , ["""tar_nested_jsonl_path""", """zip_nested_jsonl_path"""] ) def _a ( _lowerCamelCase , _lowerCamelCase ) -> List[str]: """simple docstring""" __snake_case : int = request.getfixturevalue(_lowerCamelCase ) __snake_case : List[str] = DownloadManager() for num_tar, (path, file) in enumerate(dl_manager.iter_archive(_lowerCamelCase ) , start=1 ): for num_jsonl, (subpath, subfile) in enumerate(dl_manager.iter_archive(_lowerCamelCase ) , start=1 ): _test_jsonl(_lowerCamelCase , _lowerCamelCase ) assert num_tar == 1 assert num_jsonl == 2 def _a ( _lowerCamelCase ) -> Any: """simple docstring""" __snake_case : List[str] = DownloadManager() for num_file, file in enumerate(dl_manager.iter_files(_lowerCamelCase ) , start=1 ): assert os.path.basename(_lowerCamelCase ) == ("test.txt" if num_file == 1 else "train.txt") assert num_file == 2
26
0
from __future__ import annotations import math import random from typing import Any class UpperCAmelCase_ : def __init__( self ): UpperCAmelCase__ : list[Any] = [] UpperCAmelCase__ : int = 0 UpperCAmelCase__ : int = 0 def __UpperCAmelCase ( self ): return self.head == self.tail def __UpperCAmelCase ( self , _lowerCAmelCase ): self.data.append(_lowerCAmelCase ) UpperCAmelCase__ : Dict = self.tail + 1 def __UpperCAmelCase ( self ): UpperCAmelCase__ : Optional[int] = self.data[self.head] UpperCAmelCase__ : Optional[Any] = self.head + 1 return ret def __UpperCAmelCase ( self ): return self.tail - self.head def __UpperCAmelCase ( self ): print(self.data ) print("""**************""" ) print(self.data[self.head : self.tail] ) class UpperCAmelCase_ : def __init__( self , _lowerCAmelCase ): UpperCAmelCase__ : List[Any] = data UpperCAmelCase__ : MyNode | None = None UpperCAmelCase__ : MyNode | None = None UpperCAmelCase__ : int = 1 def __UpperCAmelCase ( self ): return self.data def __UpperCAmelCase ( self ): return self.left def __UpperCAmelCase ( self ): return self.right def __UpperCAmelCase ( self ): return self.height def __UpperCAmelCase ( self , _lowerCAmelCase ): UpperCAmelCase__ : Any = data def __UpperCAmelCase ( self , _lowerCAmelCase ): UpperCAmelCase__ : List[str] = node def __UpperCAmelCase ( self , _lowerCAmelCase ): UpperCAmelCase__ : List[Any] = node def __UpperCAmelCase ( self , _lowerCAmelCase ): UpperCAmelCase__ : Tuple = height def _lowerCamelCase ( __lowerCamelCase ) -> int: '''simple docstring''' if node is None: return 0 return node.get_height() def _lowerCamelCase ( __lowerCamelCase , __lowerCamelCase ) -> int: '''simple docstring''' if a > b: return a return b def _lowerCamelCase ( __lowerCamelCase ) -> MyNode: '''simple docstring''' print("""left rotation node:""" , node.get_data() ) UpperCAmelCase__ : Optional[Any] = node.get_left() assert ret is not None node.set_left(ret.get_right() ) ret.set_right(_lowerCamelCase ) UpperCAmelCase__ : str = my_max(get_height(node.get_right() ) , get_height(node.get_left() ) ) + 1 node.set_height(_lowerCamelCase ) UpperCAmelCase__ : List[Any] = my_max(get_height(ret.get_right() ) , get_height(ret.get_left() ) ) + 1 ret.set_height(_lowerCamelCase ) return ret def _lowerCamelCase ( __lowerCamelCase ) -> MyNode: '''simple docstring''' print("""right rotation node:""" , node.get_data() ) UpperCAmelCase__ : Optional[Any] = node.get_right() assert ret is not None node.set_right(ret.get_left() ) ret.set_left(_lowerCamelCase ) UpperCAmelCase__ : Tuple = my_max(get_height(node.get_right() ) , get_height(node.get_left() ) ) + 1 node.set_height(_lowerCamelCase ) UpperCAmelCase__ : Any = my_max(get_height(ret.get_right() ) , get_height(ret.get_left() ) ) + 1 ret.set_height(_lowerCamelCase ) return ret def _lowerCamelCase ( __lowerCamelCase ) -> MyNode: '''simple docstring''' UpperCAmelCase__ : str = node.get_left() assert left_child is not None node.set_left(left_rotation(_lowerCamelCase ) ) return right_rotation(_lowerCamelCase ) def _lowerCamelCase ( __lowerCamelCase ) -> MyNode: '''simple docstring''' UpperCAmelCase__ : Optional[int] = node.get_right() assert right_child is not None node.set_right(right_rotation(_lowerCamelCase ) ) return left_rotation(_lowerCamelCase ) def _lowerCamelCase ( __lowerCamelCase , __lowerCamelCase ) -> MyNode | None: '''simple docstring''' if node is None: return MyNode(_lowerCamelCase ) if data < node.get_data(): node.set_left(insert_node(node.get_left() , _lowerCamelCase ) ) if ( get_height(node.get_left() ) - get_height(node.get_right() ) == 2 ): # an unbalance detected UpperCAmelCase__ : Union[str, Any] = node.get_left() assert left_child is not None if ( data < left_child.get_data() ): # new node is the left child of the left child UpperCAmelCase__ : Any = right_rotation(_lowerCamelCase ) else: UpperCAmelCase__ : Tuple = lr_rotation(_lowerCamelCase ) else: node.set_right(insert_node(node.get_right() , _lowerCamelCase ) ) if get_height(node.get_right() ) - get_height(node.get_left() ) == 2: UpperCAmelCase__ : Optional[int] = node.get_right() assert right_child is not None if data < right_child.get_data(): UpperCAmelCase__ : Optional[int] = rl_rotation(_lowerCamelCase ) else: UpperCAmelCase__ : Tuple = left_rotation(_lowerCamelCase ) UpperCAmelCase__ : Optional[Any] = my_max(get_height(node.get_right() ) , get_height(node.get_left() ) ) + 1 node.set_height(_lowerCamelCase ) return node def _lowerCamelCase ( __lowerCamelCase ) -> Any: '''simple docstring''' while True: UpperCAmelCase__ : Dict = root.get_right() if right_child is None: break UpperCAmelCase__ : List[Any] = right_child return root.get_data() def _lowerCamelCase ( __lowerCamelCase ) -> Any: '''simple docstring''' while True: UpperCAmelCase__ : Optional[Any] = root.get_left() if left_child is None: break UpperCAmelCase__ : int = left_child return root.get_data() def _lowerCamelCase ( __lowerCamelCase , __lowerCamelCase ) -> MyNode | None: '''simple docstring''' UpperCAmelCase__ : int = root.get_left() UpperCAmelCase__ : int = root.get_right() if root.get_data() == data: if left_child is not None and right_child is not None: UpperCAmelCase__ : Tuple = get_left_most(_lowerCamelCase ) root.set_data(_lowerCamelCase ) root.set_right(del_node(_lowerCamelCase , _lowerCamelCase ) ) elif left_child is not None: UpperCAmelCase__ : Optional[int] = left_child elif right_child is not None: UpperCAmelCase__ : str = right_child else: return None elif root.get_data() > data: if left_child is None: print("""No such data""" ) return root else: root.set_left(del_node(_lowerCamelCase , _lowerCamelCase ) ) else: # root.get_data() < data if right_child is None: return root else: root.set_right(del_node(_lowerCamelCase , _lowerCamelCase ) ) if get_height(_lowerCamelCase ) - get_height(_lowerCamelCase ) == 2: assert right_child is not None if get_height(right_child.get_right() ) > get_height(right_child.get_left() ): UpperCAmelCase__ : Optional[Any] = left_rotation(_lowerCamelCase ) else: UpperCAmelCase__ : str = rl_rotation(_lowerCamelCase ) elif get_height(_lowerCamelCase ) - get_height(_lowerCamelCase ) == -2: assert left_child is not None if get_height(left_child.get_left() ) > get_height(left_child.get_right() ): UpperCAmelCase__ : List[Any] = right_rotation(_lowerCamelCase ) else: UpperCAmelCase__ : List[str] = lr_rotation(_lowerCamelCase ) UpperCAmelCase__ : List[Any] = my_max(get_height(root.get_right() ) , get_height(root.get_left() ) ) + 1 root.set_height(_lowerCamelCase ) return root class UpperCAmelCase_ : def __init__( self ): UpperCAmelCase__ : MyNode | None = None def __UpperCAmelCase ( self ): return get_height(self.root ) def __UpperCAmelCase ( self , _lowerCAmelCase ): print("""insert:""" + str(_lowerCAmelCase ) ) UpperCAmelCase__ : Optional[Any] = insert_node(self.root , _lowerCAmelCase ) def __UpperCAmelCase ( self , _lowerCAmelCase ): print("""delete:""" + str(_lowerCAmelCase ) ) if self.root is None: print("""Tree is empty!""" ) return UpperCAmelCase__ : str = del_node(self.root , _lowerCAmelCase ) def __str__( self , ): # a level traversale, gives a more intuitive look on the tree UpperCAmelCase__ : List[Any] = """""" UpperCAmelCase__ : int = MyQueue() q.push(self.root ) UpperCAmelCase__ : int = self.get_height() if layer == 0: return output UpperCAmelCase__ : Any = 0 while not q.is_empty(): UpperCAmelCase__ : Optional[int] = q.pop() UpperCAmelCase__ : Optional[int] = """ """ * int(math.pow(2 , layer - 1 ) ) output += space if node is None: output += "*" q.push(_lowerCAmelCase ) q.push(_lowerCAmelCase ) else: output += str(node.get_data() ) q.push(node.get_left() ) q.push(node.get_right() ) output += space UpperCAmelCase__ : Tuple = cnt + 1 for i in range(100 ): if cnt == math.pow(2 , _lowerCAmelCase ) - 1: UpperCAmelCase__ : Tuple = layer - 1 if layer == 0: output += "\n*************************************" return output output += "\n" break output += "\n*************************************" return output def _lowerCamelCase ( ) -> None: '''simple docstring''' import doctest doctest.testmod() if __name__ == "__main__": _test() SCREAMING_SNAKE_CASE__ : Optional[int] = AVLtree() SCREAMING_SNAKE_CASE__ : Optional[int] = list(range(10)) random.shuffle(lst) for i in lst: t.insert(i) print(str(t)) random.shuffle(lst) for i in lst: t.del_node(i) print(str(t))
79
'''simple docstring''' def _a ( _lowerCamelCase = 100 ) -> int: """simple docstring""" __snake_case : Any = n * (n + 1) * (2 * n + 1) / 6 __snake_case : List[Any] = (n * (n + 1) / 2) ** 2 return int(square_of_sum - sum_of_squares ) if __name__ == "__main__": print(f"""{solution() = }""")
26
0
import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModelWithProjection, CLIPTokenizer from diffusers import HeunDiscreteScheduler, PriorTransformer, ShapEPipeline from diffusers.pipelines.shap_e import ShapERenderer from diffusers.utils import load_numpy, slow from diffusers.utils.testing_utils import require_torch_gpu, torch_device from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference class snake_case_ ( __lowercase , unittest.TestCase ): '''simple docstring''' lowerCamelCase = ShapEPipeline lowerCamelCase = ['''prompt'''] lowerCamelCase = ['''prompt'''] lowerCamelCase = [ '''num_images_per_prompt''', '''num_inference_steps''', '''generator''', '''latents''', '''guidance_scale''', '''frame_size''', '''output_type''', '''return_dict''', ] lowerCamelCase = False @property def __SCREAMING_SNAKE_CASE ( self : str ) -> int: return 32 @property def __SCREAMING_SNAKE_CASE ( self : List[Any] ) -> List[Any]: return 32 @property def __SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Optional[Any]: return self.time_input_dim * 4 @property def __SCREAMING_SNAKE_CASE ( self : Tuple ) -> Union[str, Any]: return 8 @property def __SCREAMING_SNAKE_CASE ( self : List[str] ) -> Dict: lowerCamelCase_ : Tuple = CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip" ) return tokenizer @property def __SCREAMING_SNAKE_CASE ( self : Any ) -> Any: torch.manual_seed(0 ) lowerCamelCase_ : int = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=self.text_embedder_hidden_size , projection_dim=self.text_embedder_hidden_size , intermediate_size=37 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , ) return CLIPTextModelWithProjection(__magic_name__ ) @property def __SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> Optional[int]: torch.manual_seed(0 ) lowerCamelCase_ : Tuple = { """num_attention_heads""": 2, """attention_head_dim""": 16, """embedding_dim""": self.time_input_dim, """num_embeddings""": 32, """embedding_proj_dim""": self.text_embedder_hidden_size, """time_embed_dim""": self.time_embed_dim, """num_layers""": 1, """clip_embed_dim""": self.time_input_dim * 2, """additional_embeddings""": 0, """time_embed_act_fn""": """gelu""", """norm_in_type""": """layer""", """encoder_hid_proj_type""": None, """added_emb_type""": None, } lowerCamelCase_ : Union[str, Any] = PriorTransformer(**__magic_name__ ) return model @property def __SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> str: torch.manual_seed(0 ) lowerCamelCase_ : Dict = { """param_shapes""": ( (self.renderer_dim, 93), (self.renderer_dim, 8), (self.renderer_dim, 8), (self.renderer_dim, 8), ), """d_latent""": self.time_input_dim, """d_hidden""": self.renderer_dim, """n_output""": 12, """background""": ( 0.1, 0.1, 0.1, ), } lowerCamelCase_ : Dict = ShapERenderer(**__magic_name__ ) return model def __SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> Any: lowerCamelCase_ : List[str] = self.dummy_prior lowerCamelCase_ : Any = self.dummy_text_encoder lowerCamelCase_ : Tuple = self.dummy_tokenizer lowerCamelCase_ : int = self.dummy_renderer lowerCamelCase_ : Tuple = HeunDiscreteScheduler( beta_schedule="exp" , num_train_timesteps=1024 , prediction_type="sample" , use_karras_sigmas=__magic_name__ , clip_sample=__magic_name__ , clip_sample_range=1.0 , ) lowerCamelCase_ : int = { """prior""": prior, """text_encoder""": text_encoder, """tokenizer""": tokenizer, """renderer""": renderer, """scheduler""": scheduler, } return components def __SCREAMING_SNAKE_CASE ( self : Optional[Any] , __magic_name__ : List[Any] , __magic_name__ : Any=0 ) -> Union[str, Any]: if str(__magic_name__ ).startswith("mps" ): lowerCamelCase_ : List[Any] = torch.manual_seed(__magic_name__ ) else: lowerCamelCase_ : Optional[int] = torch.Generator(device=__magic_name__ ).manual_seed(__magic_name__ ) lowerCamelCase_ : str = { """prompt""": """horse""", """generator""": generator, """num_inference_steps""": 1, """frame_size""": 32, """output_type""": """np""", } return inputs def __SCREAMING_SNAKE_CASE ( self : List[Any] ) -> Optional[Any]: lowerCamelCase_ : Tuple = """cpu""" lowerCamelCase_ : Dict = self.get_dummy_components() lowerCamelCase_ : Dict = self.pipeline_class(**__magic_name__ ) lowerCamelCase_ : List[Any] = pipe.to(__magic_name__ ) pipe.set_progress_bar_config(disable=__magic_name__ ) lowerCamelCase_ : Optional[Any] = pipe(**self.get_dummy_inputs(__magic_name__ ) ) lowerCamelCase_ : str = output.images[0] lowerCamelCase_ : int = image[0, -3:, -3:, -1] assert image.shape == (20, 32, 32, 3) lowerCamelCase_ : int = np.array( [ 0.0003_9216, 0.0003_9216, 0.0003_9216, 0.0003_9216, 0.0003_9216, 0.0003_9216, 0.0003_9216, 0.0003_9216, 0.0003_9216, ] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def __SCREAMING_SNAKE_CASE ( self : Any ) -> str: self._test_inference_batch_consistent(batch_sizes=[1, 2] ) def __SCREAMING_SNAKE_CASE ( self : int ) -> List[str]: lowerCamelCase_ : Dict = torch_device == """cpu""" lowerCamelCase_ : int = True self._test_inference_batch_single_identical( batch_size=2 , test_max_difference=__magic_name__ , relax_max_difference=__magic_name__ , ) def __SCREAMING_SNAKE_CASE ( self : Tuple ) -> Tuple: lowerCamelCase_ : int = self.get_dummy_components() lowerCamelCase_ : List[str] = self.pipeline_class(**__magic_name__ ) lowerCamelCase_ : List[Any] = pipe.to(__magic_name__ ) pipe.set_progress_bar_config(disable=__magic_name__ ) lowerCamelCase_ : str = 1 lowerCamelCase_ : Optional[int] = 2 lowerCamelCase_ : int = self.get_dummy_inputs(__magic_name__ ) for key in inputs.keys(): if key in self.batch_params: lowerCamelCase_ : List[str] = batch_size * [inputs[key]] lowerCamelCase_ : Optional[Any] = pipe(**__magic_name__ , num_images_per_prompt=__magic_name__ )[0] assert images.shape[0] == batch_size * num_images_per_prompt @slow @require_torch_gpu class snake_case_ ( unittest.TestCase ): '''simple docstring''' def __SCREAMING_SNAKE_CASE ( self : List[Any] ) -> Optional[int]: super().tearDown() gc.collect() torch.cuda.empty_cache() def __SCREAMING_SNAKE_CASE ( self : List[str] ) -> List[str]: lowerCamelCase_ : List[Any] = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/shap_e/test_shap_e_np_out.npy" ) lowerCamelCase_ : List[Any] = ShapEPipeline.from_pretrained("openai/shap-e" ) lowerCamelCase_ : Optional[int] = pipe.to(__magic_name__ ) pipe.set_progress_bar_config(disable=__magic_name__ ) lowerCamelCase_ : Optional[int] = torch.Generator(device=__magic_name__ ).manual_seed(0 ) lowerCamelCase_ : Dict = pipe( "a shark" , generator=__magic_name__ , guidance_scale=15.0 , num_inference_steps=64 , frame_size=64 , output_type="np" , ).images[0] assert images.shape == (20, 64, 64, 3) assert_mean_pixel_difference(__magic_name__ , __magic_name__ )
488
'''simple docstring''' from __future__ import annotations from typing import Any class _A : def __init__( self : str , __magic_name__ : int , __magic_name__ : int , __magic_name__ : float = 0 ) -> None: """simple docstring""" __snake_case , __snake_case : Optional[Any] = row, column __snake_case : Dict = [[default_value for c in range(__magic_name__ )] for r in range(__magic_name__ )] def __str__( self : List[Any] ) -> str: """simple docstring""" __snake_case : Dict = f'''Matrix consist of {self.row} rows and {self.column} columns\n''' # Make string identifier __snake_case : Optional[int] = 0 for row_vector in self.array: for obj in row_vector: __snake_case : Optional[int] = max(__magic_name__ , len(str(__magic_name__ ) ) ) __snake_case : str = f'''%{max_element_length}s''' # Make string and return def single_line(__magic_name__ : list[float] ) -> str: nonlocal string_format_identifier __snake_case : Union[str, Any] = """[""" line += ", ".join(string_format_identifier % (obj,) for obj in row_vector ) line += "]" return line s += "\n".join(single_line(__magic_name__ ) for row_vector in self.array ) return s def __repr__( self : Optional[int] ) -> str: """simple docstring""" return str(self ) def lowercase__ ( self : Dict , __magic_name__ : tuple[int, int] ) -> bool: """simple docstring""" if not (isinstance(__magic_name__ , (list, tuple) ) and len(__magic_name__ ) == 2): return False elif not (0 <= loc[0] < self.row and 0 <= loc[1] < self.column): return False else: return True def __getitem__( self : int , __magic_name__ : tuple[int, int] ) -> Any: """simple docstring""" assert self.validate_indicies(__magic_name__ ) return self.array[loc[0]][loc[1]] def __setitem__( self : List[str] , __magic_name__ : tuple[int, int] , __magic_name__ : float ) -> None: """simple docstring""" assert self.validate_indicies(__magic_name__ ) __snake_case : Optional[int] = value def __add__( self : Any , __magic_name__ : Matrix ) -> Matrix: """simple docstring""" assert isinstance(__magic_name__ , __magic_name__ ) assert self.row == another.row and self.column == another.column # Add __snake_case : Union[str, Any] = Matrix(self.row , self.column ) for r in range(self.row ): for c in range(self.column ): __snake_case : List[Any] = self[r, c] + another[r, c] return result def __neg__( self : Tuple ) -> Matrix: """simple docstring""" __snake_case : Tuple = Matrix(self.row , self.column ) for r in range(self.row ): for c in range(self.column ): __snake_case : List[Any] = -self[r, c] return result def __sub__( self : Optional[int] , __magic_name__ : Matrix ) -> Matrix: """simple docstring""" return self + (-another) def __mul__( self : List[Any] , __magic_name__ : int | float | Matrix ) -> Matrix: """simple docstring""" if isinstance(__magic_name__ , (int, float) ): # Scalar multiplication __snake_case : Optional[int] = Matrix(self.row , self.column ) for r in range(self.row ): for c in range(self.column ): __snake_case : Tuple = self[r, c] * another return result elif isinstance(__magic_name__ , __magic_name__ ): # Matrix multiplication assert self.column == another.row __snake_case : Dict = Matrix(self.row , another.column ) for r in range(self.row ): for c in range(another.column ): for i in range(self.column ): result[r, c] += self[r, i] * another[i, c] return result else: __snake_case : Optional[int] = f'''Unsupported type given for another ({type(__magic_name__ )})''' raise TypeError(__magic_name__ ) def lowercase__ ( self : str ) -> Matrix: """simple docstring""" __snake_case : Any = Matrix(self.column , self.row ) for r in range(self.row ): for c in range(self.column ): __snake_case : str = self[r, c] return result def lowercase__ ( self : Union[str, Any] , __magic_name__ : Matrix , __magic_name__ : Matrix ) -> Any: """simple docstring""" assert isinstance(__magic_name__ , __magic_name__ ) and isinstance(__magic_name__ , __magic_name__ ) assert self.row == self.column == u.row == v.row # u, v should be column vector assert u.column == v.column == 1 # u, v should be column vector # Calculate __snake_case : List[str] = v.transpose() __snake_case : Tuple = (v_t * self * u)[0, 0] + 1 if numerator_factor == 0: return None # It's not invertable return self - ((self * u) * (v_t * self) * (1.0 / numerator_factor)) # Testing if __name__ == "__main__": def _a ( ) -> None: """simple docstring""" __snake_case : Tuple = Matrix(3 , 3 , 0 ) for i in range(3 ): __snake_case : Any = 1 print(F'''a^(-1) is {ainv}''' ) # u, v __snake_case : Dict = Matrix(3 , 1 , 0 ) __snake_case , __snake_case , __snake_case : Union[str, Any] = 1, 2, -3 __snake_case : str = Matrix(3 , 1 , 0 ) __snake_case , __snake_case , __snake_case : Tuple = 4, -2, 5 print(F'''u is {u}''' ) print(F'''v is {v}''' ) print(F'''uv^T is {u * v.transpose()}''' ) # Sherman Morrison print(F'''(a + uv^T)^(-1) is {ainv.sherman_morrison(_lowerCamelCase , _lowerCamelCase )}''' ) def _a ( ) -> None: """simple docstring""" import doctest doctest.testmod() testa()
26
0
from __future__ import annotations def __SCREAMING_SNAKE_CASE ( a__ : Optional[int] ,a__ : Any ,a__ : int ) -> int | float: if len(_lowerCamelCase ) == 0: raise ValueError("""find_max() arg is an empty sequence""" ) if ( left >= len(_lowerCamelCase ) or left < -len(_lowerCamelCase ) or right >= len(_lowerCamelCase ) or right < -len(_lowerCamelCase ) ): raise IndexError("""list index out of range""" ) if left == right: return nums[left] __A : Optional[int] = (left + right) >> 1 # the middle __A : str = find_max(_lowerCamelCase ,_lowerCamelCase ,_lowerCamelCase ) # find max in range[left, mid] __A : Union[str, Any] = find_max(_lowerCamelCase ,mid + 1 ,_lowerCamelCase ) # find max in range[mid + 1, right] return left_max if left_max >= right_max else right_max if __name__ == "__main__": import doctest doctest.testmod(verbose=True)
17
'''simple docstring''' import argparse import json import os import torch from torch import nn from transformers import NllbMoeConfig, NllbMoeModel from transformers.modeling_utils import dtype_byte_size from transformers.utils import WEIGHTS_INDEX_NAME, WEIGHTS_NAME def _a ( _lowerCamelCase ) -> List[Any]: """simple docstring""" __snake_case : Union[str, Any] = [ """encoder.version""", """decoder.version""", """model.encoder.version""", """model.decoder.version""", """decoder.output_projection.weight""", """_float_tensor""", """encoder.embed_positions._float_tensor""", """decoder.embed_positions._float_tensor""", ] for k in ignore_keys: state_dict.pop(_lowerCamelCase , _lowerCamelCase ) def _a ( _lowerCamelCase ) -> List[str]: """simple docstring""" __snake_case , __snake_case : Dict = emb.weight.shape __snake_case : Optional[int] = nn.Linear(_lowerCamelCase , _lowerCamelCase , bias=_lowerCamelCase ) __snake_case : Union[str, Any] = emb.weight.data return lin_layer def _a ( _lowerCamelCase , _lowerCamelCase=None ) -> Union[str, Any]: """simple docstring""" __snake_case : Any = {} for old_key in state_dict.keys(): __snake_case : Union[str, Any] = old_key if "moe_layer.experts." in key: if expert_idx is not None: __snake_case : Tuple = key.replace("""moe_layer.experts.0""" , F'''ffn.experts.expert_{expert_idx}''' ) else: __snake_case : Optional[int] = key.replace("""moe_layer.experts.""" , """ffn.experts.expert_""" ) if "gate" in key: __snake_case : Dict = key.replace(""".moe_layer.gate.wg""" , """.ffn.router.classifier""" ) if "fc2" and "experts" not in key: __snake_case : Union[str, Any] = key.replace(""".fc2.""" , """.ffn.fc2.""" ) if "fc1" and "experts" not in key: __snake_case : Optional[int] = key.replace(""".fc1.""" , """.ffn.fc1.""" ) if ".encoder_attn." in key: __snake_case : Tuple = key.replace(""".encoder_attn.""" , """.cross_attention.""" ) if "encoder_attn_layer_norm" in key: __snake_case : Union[str, Any] = key.replace("""encoder_attn_layer_norm""" , """cross_attention_layer_norm""" ) if "final_layer_norm" in key: __snake_case : str = key.replace("""final_layer_norm""" , """ff_layer_norm""" ) __snake_case : str = state_dict[old_key] return new_dict def _a ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase = WEIGHTS_NAME ) -> Dict: """simple docstring""" __snake_case : Optional[int] = [] __snake_case : Dict = 0 os.makedirs(_lowerCamelCase , exist_ok=_lowerCamelCase ) for expert in range(_lowerCamelCase ): __snake_case : Tuple = switch_checkpoint_path + F'''-rank-{expert}.pt''' if os.path.isfile(_lowerCamelCase ): __snake_case : Dict = torch.load(_lowerCamelCase )["""model"""] remove_ignore_keys_(_lowerCamelCase ) __snake_case : Optional[Any] = rename_fairseq_keys(_lowerCamelCase , _lowerCamelCase ) __snake_case : List[Any] = os.path.join( _lowerCamelCase , weights_name.replace(""".bin""" , F'''-{len(_lowerCamelCase )+1:05d}-of-???.bin''' ) ) torch.save(_lowerCamelCase , _lowerCamelCase ) sharded_state_dicts.append(expert_state.keys() ) total_size += sum([value.numel() for key, value in expert_state.items()] ) * dtype_byte_size( expert_state[list(_lowerCamelCase )[0]].dtype ) # Add the last block __snake_case : Optional[Any] = os.path.join(_lowerCamelCase , weights_name.replace(""".bin""" , F'''-{len(_lowerCamelCase )+1:05d}-of-???.bin''' ) ) __snake_case : str = torch.load(switch_checkpoint_path + """-shared.pt""" )["""model"""] remove_ignore_keys_(_lowerCamelCase ) __snake_case : Optional[Any] = rename_fairseq_keys(_lowerCamelCase , _lowerCamelCase ) __snake_case : List[str] = shared_weights["""decoder.embed_tokens.weight"""] sharded_state_dicts.append(shared_weights.keys() ) # If we only have the shared weights (dummy model/experts saved on the same file) if len(_lowerCamelCase ) == 1: __snake_case : Optional[Any] = os.path.join(_lowerCamelCase , _lowerCamelCase ) torch.save(_lowerCamelCase , _lowerCamelCase ) return {weights_name: sharded_state_dicts[0]}, None else: torch.save(_lowerCamelCase , _lowerCamelCase ) # Otherwise, let's build the index __snake_case : Tuple = {} for idx, shard in enumerate(_lowerCamelCase ): __snake_case : Any = weights_name.replace(""".bin""" , F'''-{idx+1:05d}-of-{len(_lowerCamelCase ):05d}.bin''' ) __snake_case : int = os.path.join(_lowerCamelCase , weights_name.replace(""".bin""" , F'''-{idx+1:05d}-of-???.bin''' ) ) os.rename(_lowerCamelCase , os.path.join(_lowerCamelCase , _lowerCamelCase ) ) for key in shard: __snake_case : str = shard_file # Add the metadata __snake_case : Optional[Any] = {"""total_size""": total_size} __snake_case : int = {"""metadata""": metadata, """weight_map""": weight_map} with open(os.path.join(_lowerCamelCase , _lowerCamelCase ) , """w""" , encoding="""utf-8""" ) as f: __snake_case : Union[str, Any] = json.dumps(_lowerCamelCase , indent=2 , sort_keys=_lowerCamelCase ) + """\n""" f.write(_lowerCamelCase ) return metadata, index if __name__ == "__main__": __UpperCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( "--nllb_moe_checkpoint_path", default="/home/arthur_huggingface_co/fairseq/weights/checkpoints/model_moe_54b/checkpoint_2_300000", type=str, required=False, help="Path to a directory containing a folder per layer. Follows the original Google format.", ) parser.add_argument("--dtype", default="float32", type=str, required=False, help="dtype of the saved model") parser.add_argument( "--pytorch_dump_folder_path", default="/home/arthur_huggingface_co/fairseq/weights/checkpoints/hf-converted-moe-54b", type=str, required=False, help="Path to the output pytorch model.", ) __UpperCamelCase = parser.parse_args() __UpperCamelCase , __UpperCamelCase = shard_on_the_fly( args.nllb_moe_checkpoint_path, args.pytorch_dump_folder_path, 128, args.dtype, ) __UpperCamelCase = NllbMoeConfig.from_pretrained( "facebook/nllb-200-3.3B", encoder_sparse_step=4, decoder_sparse_step=4, num_experts=128 ) config.save_pretrained(args.pytorch_dump_folder_path) __UpperCamelCase = NllbMoeModel.from_pretrained(args.pytorch_dump_folder_path) print("Done") model.save_pretrained(args.pytorch_dump_folder_path)
26
0
"""simple docstring""" import random import timeit from functools import wraps from typing import Callable, Optional from ..configuration_utils import PretrainedConfig from ..models.auto.modeling_tf_auto import TF_MODEL_MAPPING, TF_MODEL_WITH_LM_HEAD_MAPPING from ..utils import is_pyanvml_available, is_tf_available, logging from .benchmark_utils import ( Benchmark, Memory, MemorySummary, measure_peak_memory_cpu, start_memory_tracing, stop_memory_tracing, ) if is_tf_available(): import tensorflow as tf from tensorflow.python.framework.errors_impl import ResourceExhaustedError from .benchmark_args_tf import TensorFlowBenchmarkArguments if is_pyanvml_available(): import pyanvml.pyanvml as nvml _A : str = logging.get_logger(__name__) def __magic_name__ ( __snake_case : Optional[int] , __snake_case : Tuple ) -> Union[str, Any]: def run_func(__snake_case : Any ): @wraps(_lowerCamelCase ) def run_in_eager_mode(*__snake_case : Tuple , **__snake_case : int ): return func(*_lowerCamelCase , **_lowerCamelCase ) @wraps(_lowerCamelCase ) @tf.function(experimental_compile=_lowerCamelCase ) def run_in_graph_mode(*__snake_case : Dict , **__snake_case : str ): return func(*_lowerCamelCase , **_lowerCamelCase ) if do_eager_mode is True: if use_xla is not False: raise ValueError( "Cannot run model in XLA, if `args.eager_mode` is set to `True`. Please set `args.eager_mode=False`." ) return run_in_eager_mode else: return run_in_graph_mode return run_func def __magic_name__ ( __snake_case : List[Any] , __snake_case : List[str] , __snake_case : List[Any] ) -> ["tf.Tensor"]: lowercase : Dict = random.Random() lowercase : str = [rng.randint(0 , vocab_size - 1 ) for i in range(batch_size * sequence_length )] return tf.constant(_lowerCamelCase , shape=(batch_size, sequence_length) , dtype=tf.intaa ) class a__ ( __lowercase ): __lowerCAmelCase = 42 __lowerCAmelCase = 42 __lowerCAmelCase = "TensorFlow" @property def __magic_name__ ( self ): return tf.__version__ def __magic_name__ ( self , _a , _a , _a ): lowercase : Tuple = self.args.strategy if strategy is None: raise ValueError("A device strategy has to be initialized before using TensorFlow." ) lowercase : Any = self._prepare_inference_func(_a , _a , _a ) return self._measure_speed(_inference ) def __magic_name__ ( self , _a , _a , _a ): lowercase : Tuple = self.args.strategy if strategy is None: raise ValueError("A device strategy has to be initialized before using TensorFlow." ) lowercase : List[Any] = self._prepare_train_func(_a , _a , _a ) return self._measure_speed(_train ) def __magic_name__ ( self , _a , _a , _a ): if self.args.is_gpu: tf.config.experimental.set_memory_growth(self.args.gpu_list[self.args.device_idx] , _a ) lowercase : List[Any] = self.args.strategy if strategy is None: raise ValueError("A device strategy has to be initialized before using TensorFlow." ) lowercase : Optional[Any] = self._prepare_inference_func(_a , _a , _a ) return self._measure_memory(_inference ) def __magic_name__ ( self , _a , _a , _a ): if self.args.is_gpu: tf.config.experimental.set_memory_growth(self.args.gpu_list[self.args.device_idx] , _a ) lowercase : Tuple = self.args.strategy if strategy is None: raise ValueError("A device strategy has to be initialized before using TensorFlow." ) lowercase : List[str] = self._prepare_train_func(_a , _a , _a ) return self._measure_memory(_train ) def __magic_name__ ( self , _a , _a , _a ): lowercase : List[Any] = self.config_dict[model_name] if self.args.fpaa: raise NotImplementedError("Mixed precision is currently not supported." ) lowercase : str = ( hasattr(_a , "architectures" ) and isinstance(config.architectures , _a ) and len(config.architectures ) > 0 ) if not self.args.only_pretrain_model and has_model_class_in_config: try: lowercase : List[Any] = """TF""" + config.architectures[0] # prepend 'TF' for tensorflow model lowercase : Any = __import__("transformers" , fromlist=[model_class] ) lowercase : Union[str, Any] = getattr(_a , _a ) lowercase : Dict = model_cls(_a ) except ImportError: raise ImportError( f"""{model_class} does not exist. If you just want to test the pretrained model, you might want to""" " set `--only_pretrain_model` or `args.only_pretrain_model=True`." ) else: lowercase : int = TF_MODEL_MAPPING[config.__class__](_a ) # encoder-decoder has vocab size saved differently lowercase : Optional[int] = config.vocab_size if hasattr(_a , "vocab_size" ) else config.encoder.vocab_size lowercase : str = random_input_ids(_a , _a , _a ) @run_with_tf_optimizations(self.args.eager_mode , self.args.use_xla ) def encoder_decoder_forward(): return model(_a , decoder_input_ids=_a , training=_a ) @run_with_tf_optimizations(self.args.eager_mode , self.args.use_xla ) def encoder_forward(): return model(_a , training=_a ) lowercase : Any = encoder_decoder_forward if config.is_encoder_decoder else encoder_forward return _inference def __magic_name__ ( self , _a , _a , _a ): lowercase : Tuple = self.config_dict[model_name] if self.args.eager_mode is not False: raise ValueError("Training cannot be done in eager mode. Please make sure that `args.eager_mode = False`." ) if self.args.fpaa: raise NotImplementedError("Mixed precision is currently not supported." ) lowercase : Optional[int] = ( hasattr(_a , "architectures" ) and isinstance(config.architectures , _a ) and len(config.architectures ) > 0 ) if not self.args.only_pretrain_model and has_model_class_in_config: try: lowercase : Optional[int] = """TF""" + config.architectures[0] # prepend 'TF' for tensorflow model lowercase : Dict = __import__("transformers" , fromlist=[model_class] ) lowercase : List[Any] = getattr(_a , _a ) lowercase : Tuple = model_cls(_a ) except ImportError: raise ImportError( f"""{model_class} does not exist. If you just want to test the pretrained model, you might want to""" " set `--only_pretrain_model` or `args.only_pretrain_model=True`." ) else: lowercase : Union[str, Any] = TF_MODEL_WITH_LM_HEAD_MAPPING[config.__class__](_a ) # encoder-decoder has vocab size saved differently lowercase : Optional[Any] = config.vocab_size if hasattr(_a , "vocab_size" ) else config.encoder.vocab_size lowercase : List[str] = random_input_ids(_a , _a , _a ) @run_with_tf_optimizations(self.args.eager_mode , self.args.use_xla ) def encoder_decoder_train(): lowercase : Tuple = model(_a , decoder_input_ids=_a , labels=_a , training=_a )[0] lowercase : Dict = tf.gradients(_a , model.trainable_variables ) return gradients @run_with_tf_optimizations(self.args.eager_mode , self.args.use_xla ) def encoder_train(): lowercase : Optional[Any] = model(_a , labels=_a , training=_a )[0] lowercase : Optional[int] = tf.gradients(_a , model.trainable_variables ) return gradients lowercase : str = encoder_decoder_train if config.is_encoder_decoder else encoder_train return _train def __magic_name__ ( self , _a ): with self.args.strategy.scope(): try: if self.args.is_tpu or self.args.use_xla: # run additional 10 times to stabilize compilation for tpu logger.info("Do inference on TPU. Running model 5 times to stabilize compilation" ) timeit.repeat(_a , repeat=1 , number=5 ) # as written in https://docs.python.org/2/library/timeit.html#timeit.Timer.repeat, min should be taken rather than the average lowercase : Optional[int] = timeit.repeat( _a , repeat=self.args.repeat , number=10 , ) return min(_a ) / 1_0.0 except ResourceExhaustedError as e: self.print_fn(f"""Doesn\'t fit on GPU. {e}""" ) def __magic_name__ ( self , _a ): logger.info( "Note that TensorFlow allocates more memory than " "it might need to speed up computation. " "The memory reported here corresponds to the memory " "reported by `nvidia-smi`, which can vary depending " "on total available memory on the GPU that is used." ) with self.args.strategy.scope(): try: if self.args.trace_memory_line_by_line: if not self.args.eager_mode: raise ValueError( "`args.eager_mode` is set to `False`. Make sure to run model in eager mode to measure memory" " consumption line by line." ) lowercase : Dict = start_memory_tracing("transformers" ) if self.args.is_tpu: # tpu raise NotImplementedError( "Memory Benchmarking is currently not implemented for TPU. Please disable memory benchmarking" " with `args.memory=False`" ) elif self.args.is_gpu: # gpu if not is_pyanvml_available(): logger.warning( "py3nvml not installed, we won't log GPU memory usage. " "Install py3nvml (pip install py3nvml) to log information about GPU." ) lowercase : int = """N/A""" else: logger.info( "Measuring total GPU usage on GPU device. Make sure to not have additional processes" " running on the same GPU." ) # init nvml nvml.nvmlInit() func() lowercase : Any = nvml.nvmlDeviceGetHandleByIndex(self.args.device_idx ) lowercase : Union[str, Any] = nvml.nvmlDeviceGetMemoryInfo(_a ) lowercase : Union[str, Any] = meminfo.used lowercase : List[Any] = Memory(_a ) # shutdown nvml nvml.nvmlShutdown() else: # cpu if self.args.trace_memory_line_by_line: logger.info( "When enabling line by line tracing, the max peak memory for CPU is inaccurate in" " TensorFlow." ) lowercase : int = None else: lowercase : Dict = measure_peak_memory_cpu(_a ) lowercase : Union[str, Any] = Memory(_a ) if isinstance(_a , _a ) else memory_bytes if self.args.trace_memory_line_by_line: lowercase : Tuple = stop_memory_tracing(_a ) if memory is None: lowercase : Any = summary.total else: lowercase : Any = None return memory, summary except ResourceExhaustedError as e: self.print_fn(f"""Doesn\'t fit on GPU. {e}""" ) return "N/A", None
361
'''simple docstring''' import cva import numpy as np class _A : def __init__( self : Any , __magic_name__ : float , __magic_name__ : int ) -> Optional[int]: """simple docstring""" if k in (0.04, 0.06): __snake_case : List[str] = k __snake_case : int = window_size else: raise ValueError("""invalid k value""" ) def __str__( self : Union[str, Any] ) -> str: """simple docstring""" return str(self.k ) def lowercase__ ( self : Dict , __magic_name__ : str ) -> tuple[cva.Mat, list[list[int]]]: """simple docstring""" __snake_case : Dict = cva.imread(__magic_name__ , 0 ) __snake_case , __snake_case : List[str] = img.shape __snake_case : list[list[int]] = [] __snake_case : str = img.copy() __snake_case : Tuple = cva.cvtColor(__magic_name__ , cva.COLOR_GRAY2RGB ) __snake_case , __snake_case : List[Any] = np.gradient(__magic_name__ ) __snake_case : Optional[Any] = dx**2 __snake_case : Tuple = dy**2 __snake_case : List[Any] = dx * dy __snake_case : List[Any] = 0.04 __snake_case : Tuple = self.window_size // 2 for y in range(__magic_name__ , h - offset ): for x in range(__magic_name__ , w - offset ): __snake_case : Dict = ixx[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() __snake_case : Optional[int] = iyy[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() __snake_case : str = ixy[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() __snake_case : List[str] = (wxx * wyy) - (wxy**2) __snake_case : Dict = wxx + wyy __snake_case : List[str] = det - k * (trace**2) # Can change the value if r > 0.5: corner_list.append([x, y, r] ) color_img.itemset((y, x, 0) , 0 ) color_img.itemset((y, x, 1) , 0 ) color_img.itemset((y, x, 2) , 2_55 ) return color_img, corner_list if __name__ == "__main__": __UpperCamelCase = HarrisCorner(0.04, 3) __UpperCamelCase , __UpperCamelCase = edge_detect.detect("path_to_image") cva.imwrite("detect.png", color_img)
26
0
import io import json import fsspec import pytest from datasets import Dataset, DatasetDict, Features, NamedSplit, Value from datasets.io.json import JsonDatasetReader, JsonDatasetWriter from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases def _SCREAMING_SNAKE_CASE ( lowercase : Tuple , lowercase : Dict ): '''simple docstring''' assert isinstance(_lowerCamelCase , _lowerCamelCase ) assert dataset.num_rows == 4 assert dataset.num_columns == 3 assert dataset.column_names == ["col_1", "col_2", "col_3"] 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 ( lowercase : Any , lowercase : List[Any] , lowercase : str ): '''simple docstring''' lowerCamelCase_ = tmp_path / """cache""" lowerCamelCase_ = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): lowerCamelCase_ = JsonDatasetReader(_lowerCamelCase , cache_dir=_lowerCamelCase , keep_in_memory=_lowerCamelCase ).read() _check_json_dataset(_lowerCamelCase , _lowerCamelCase ) @pytest.mark.parametrize( 'features' , [ None, {'col_1': 'string', 'col_2': 'int64', 'col_3': 'float64'}, {'col_1': 'string', 'col_2': 'string', 'col_3': 'string'}, {'col_1': 'int32', 'col_2': 'int32', 'col_3': 'int32'}, {'col_1': 'float32', 'col_2': 'float32', 'col_3': 'float32'}, ] , ) def _SCREAMING_SNAKE_CASE ( lowercase : Dict , lowercase : Any , lowercase : int ): '''simple docstring''' lowerCamelCase_ = tmp_path / """cache""" lowerCamelCase_ = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} lowerCamelCase_ = features.copy() if features else default_expected_features lowerCamelCase_ = ( Features({feature: Value(_lowerCamelCase ) for feature, dtype in features.items()} ) if features is not None else None ) lowerCamelCase_ = JsonDatasetReader(_lowerCamelCase , features=_lowerCamelCase , cache_dir=_lowerCamelCase ).read() _check_json_dataset(_lowerCamelCase , _lowerCamelCase ) @pytest.mark.parametrize( 'features' , [ None, {'col_3': 'float64', 'col_1': 'string', 'col_2': 'int64'}, ] , ) def _SCREAMING_SNAKE_CASE ( lowercase : Any , lowercase : Optional[int] , lowercase : Optional[int] ): '''simple docstring''' lowerCamelCase_ = tmp_path / """cache""" lowerCamelCase_ = {"""col_3""": """float64""", """col_1""": """string""", """col_2""": """int64"""} lowerCamelCase_ = features.copy() if features else default_expected_features lowerCamelCase_ = ( Features({feature: Value(_lowerCamelCase ) for feature, dtype in features.items()} ) if features is not None else None ) lowerCamelCase_ = JsonDatasetReader(_lowerCamelCase , features=_lowerCamelCase , cache_dir=_lowerCamelCase ).read() assert isinstance(_lowerCamelCase , _lowerCamelCase ) assert dataset.num_rows == 2 assert dataset.num_columns == 3 assert dataset.column_names == ["col_3", "col_1", "col_2"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype def _SCREAMING_SNAKE_CASE ( lowercase : Dict , lowercase : List[str] ): '''simple docstring''' lowerCamelCase_ = {"""col_2""": """int64""", """col_3""": """float64""", """col_1""": """string"""} lowerCamelCase_ = features.copy() lowerCamelCase_ = ( Features({feature: Value(_lowerCamelCase ) for feature, dtype in features.items()} ) if features is not None else None ) lowerCamelCase_ = tmp_path / """cache""" lowerCamelCase_ = JsonDatasetReader(_lowerCamelCase , features=_lowerCamelCase , cache_dir=_lowerCamelCase ).read() assert isinstance(_lowerCamelCase , _lowerCamelCase ) assert dataset.num_rows == 2 assert dataset.num_columns == 3 assert dataset.column_names == ["col_2", "col_3", "col_1"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @pytest.mark.parametrize('split' , [None, NamedSplit('train' ), 'train', 'test'] ) def _SCREAMING_SNAKE_CASE ( lowercase : List[Any] , lowercase : Dict , lowercase : List[Any] ): '''simple docstring''' lowerCamelCase_ = tmp_path / """cache""" lowerCamelCase_ = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} lowerCamelCase_ = JsonDatasetReader(_lowerCamelCase , cache_dir=_lowerCamelCase , split=_lowerCamelCase ).read() _check_json_dataset(_lowerCamelCase , _lowerCamelCase ) assert dataset.split == split if split else "train" @pytest.mark.parametrize('path_type' , [str, list] ) def _SCREAMING_SNAKE_CASE ( lowercase : List[Any] , lowercase : List[str] , lowercase : List[str] ): '''simple docstring''' if issubclass(_lowerCamelCase , _lowerCamelCase ): lowerCamelCase_ = jsonl_path elif issubclass(_lowerCamelCase , _lowerCamelCase ): lowerCamelCase_ = [jsonl_path] lowerCamelCase_ = tmp_path / """cache""" lowerCamelCase_ = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} lowerCamelCase_ = JsonDatasetReader(_lowerCamelCase , cache_dir=_lowerCamelCase ).read() _check_json_dataset(_lowerCamelCase , _lowerCamelCase ) def _SCREAMING_SNAKE_CASE ( lowercase : Optional[Any] , lowercase : List[str] , lowercase : Any=("train",) ): '''simple docstring''' assert isinstance(_lowerCamelCase , _lowerCamelCase ) for split in splits: lowerCamelCase_ = dataset_dict[split] assert dataset.num_rows == 4 assert dataset.num_columns == 3 assert dataset.column_names == ["col_1", "col_2", "col_3"] 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 ( lowercase : Optional[Any] , lowercase : int , lowercase : Tuple ): '''simple docstring''' lowerCamelCase_ = tmp_path / """cache""" lowerCamelCase_ = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): lowerCamelCase_ = JsonDatasetReader({'train': jsonl_path} , cache_dir=_lowerCamelCase , keep_in_memory=_lowerCamelCase ).read() _check_json_datasetdict(_lowerCamelCase , _lowerCamelCase ) @pytest.mark.parametrize( 'features' , [ None, {'col_1': 'string', 'col_2': 'int64', 'col_3': 'float64'}, {'col_1': 'string', 'col_2': 'string', 'col_3': 'string'}, {'col_1': 'int32', 'col_2': 'int32', 'col_3': 'int32'}, {'col_1': 'float32', 'col_2': 'float32', 'col_3': 'float32'}, ] , ) def _SCREAMING_SNAKE_CASE ( lowercase : int , lowercase : List[str] , lowercase : List[str] ): '''simple docstring''' lowerCamelCase_ = tmp_path / """cache""" lowerCamelCase_ = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} lowerCamelCase_ = features.copy() if features else default_expected_features lowerCamelCase_ = ( Features({feature: Value(_lowerCamelCase ) for feature, dtype in features.items()} ) if features is not None else None ) lowerCamelCase_ = JsonDatasetReader({'train': jsonl_path} , features=_lowerCamelCase , cache_dir=_lowerCamelCase ).read() _check_json_datasetdict(_lowerCamelCase , _lowerCamelCase ) @pytest.mark.parametrize('split' , [None, NamedSplit('train' ), 'train', 'test'] ) def _SCREAMING_SNAKE_CASE ( lowercase : int , lowercase : Union[str, Any] , lowercase : Optional[Any] ): '''simple docstring''' if split: lowerCamelCase_ = {split: jsonl_path} else: lowerCamelCase_ = """train""" lowerCamelCase_ = {"""train""": jsonl_path, """test""": jsonl_path} lowerCamelCase_ = tmp_path / """cache""" lowerCamelCase_ = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} lowerCamelCase_ = JsonDatasetReader(_lowerCamelCase , cache_dir=_lowerCamelCase ).read() _check_json_datasetdict(_lowerCamelCase , _lowerCamelCase , splits=list(path.keys() ) ) assert all(dataset[split].split == split for split in path.keys() ) def _SCREAMING_SNAKE_CASE ( lowercase : Union[str, Any] ): '''simple docstring''' return json.load(_lowerCamelCase ) def _SCREAMING_SNAKE_CASE ( lowercase : Optional[int] ): '''simple docstring''' return [json.loads(_lowerCamelCase ) for line in buffer] class A: '''simple docstring''' @pytest.mark.parametrize('lines, load_json_function' , [(True, load_json_lines), (False, load_json)] ) def a__ ( self : Optional[Any] , A_ : List[Any] , A_ : List[str] , A_ : Optional[int] ) -> Tuple: """simple docstring""" with io.BytesIO() as buffer: JsonDatasetWriter(A_ , A_ , lines=A_ ).write() buffer.seek(0 ) lowerCamelCase_ = load_json_function(A_ ) assert isinstance(A_ , A_ ) assert isinstance(exported_content[0] , A_ ) assert len(A_ ) == 10 @pytest.mark.parametrize( 'orient, container, keys, len_at' , [ ('records', list, {'tokens', 'labels', 'answers', 'id'}, None), ('split', dict, {'columns', 'data'}, 'data'), ('index', dict, set('0123456789' ), None), ('columns', dict, {'tokens', 'labels', 'answers', 'id'}, 'tokens'), ('values', list, None, None), ('table', dict, {'schema', 'data'}, 'data'), ] , ) def a__ ( self : Optional[Any] , A_ : List[Any] , A_ : Dict , A_ : Union[str, Any] , A_ : Any , A_ : Tuple ) -> Tuple: """simple docstring""" with io.BytesIO() as buffer: JsonDatasetWriter(A_ , A_ , lines=A_ , orient=A_ ).write() buffer.seek(0 ) lowerCamelCase_ = load_json(A_ ) assert isinstance(A_ , A_ ) if keys: if container is dict: assert exported_content.keys() == keys else: assert exported_content[0].keys() == keys else: assert not hasattr(A_ , 'keys' ) and not hasattr(exported_content[0] , 'keys' ) if len_at: assert len(exported_content[len_at] ) == 10 else: assert len(A_ ) == 10 @pytest.mark.parametrize('lines, load_json_function' , [(True, load_json_lines), (False, load_json)] ) def a__ ( self : Union[str, Any] , A_ : List[Any] , A_ : Tuple , A_ : Dict ) -> Tuple: """simple docstring""" with io.BytesIO() as buffer: JsonDatasetWriter(A_ , A_ , lines=A_ , num_proc=2 ).write() buffer.seek(0 ) lowerCamelCase_ = load_json_function(A_ ) assert isinstance(A_ , A_ ) assert isinstance(exported_content[0] , A_ ) assert len(A_ ) == 10 @pytest.mark.parametrize( 'orient, container, keys, len_at' , [ ('records', list, {'tokens', 'labels', 'answers', 'id'}, None), ('split', dict, {'columns', 'data'}, 'data'), ('index', dict, set('0123456789' ), None), ('columns', dict, {'tokens', 'labels', 'answers', 'id'}, 'tokens'), ('values', list, None, None), ('table', dict, {'schema', 'data'}, 'data'), ] , ) def a__ ( self : Optional[int] , A_ : List[Any] , A_ : List[Any] , A_ : str , A_ : Optional[int] , A_ : Optional[int] ) -> Optional[int]: """simple docstring""" with io.BytesIO() as buffer: JsonDatasetWriter(A_ , A_ , lines=A_ , orient=A_ , num_proc=2 ).write() buffer.seek(0 ) lowerCamelCase_ = load_json(A_ ) assert isinstance(A_ , A_ ) if keys: if container is dict: assert exported_content.keys() == keys else: assert exported_content[0].keys() == keys else: assert not hasattr(A_ , 'keys' ) and not hasattr(exported_content[0] , 'keys' ) if len_at: assert len(exported_content[len_at] ) == 10 else: assert len(A_ ) == 10 def a__ ( self : List[Any] , A_ : List[str] ) -> str: """simple docstring""" with pytest.raises(A_ ): with io.BytesIO() as buffer: JsonDatasetWriter(A_ , A_ , num_proc=0 ) @pytest.mark.parametrize('compression, extension' , [('gzip', 'gz'), ('bz2', 'bz2'), ('xz', 'xz')] ) def a__ ( self : Union[str, Any] , A_ : Optional[Any] , A_ : Optional[int] , A_ : Optional[Any] , A_ : Optional[Any] , A_ : Union[str, Any] ) -> Tuple: """simple docstring""" lowerCamelCase_ = tmp_path_factory.mktemp('data' ) / f"""test.json.{extension}""" lowerCamelCase_ = str(shared_datadir / f"""test_file.json.{extension}""" ) JsonDatasetWriter(A_ , A_ , compression=A_ ).write() with fsspec.open(A_ , 'rb' , compression='infer' ) as f: lowerCamelCase_ = f.read() with fsspec.open(A_ , 'rb' , compression='infer' ) as f: lowerCamelCase_ = f.read() assert exported_content == original_content
70
'''simple docstring''' import warnings from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class _A ( __lowercase ): lowercase__: Any = ['''image_processor''', '''tokenizer'''] lowercase__: Any = '''CLIPImageProcessor''' lowercase__: Optional[Any] = ('''CLIPTokenizer''', '''CLIPTokenizerFast''') def __init__( self : int , __magic_name__ : Dict=None , __magic_name__ : Dict=None , **__magic_name__ : Union[str, Any] ) -> Any: """simple docstring""" __snake_case : Optional[Any] = None if "feature_extractor" in kwargs: warnings.warn( """The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`""" """ instead.""" , __magic_name__ , ) __snake_case : List[Any] = kwargs.pop("""feature_extractor""" ) __snake_case : List[str] = 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__(__magic_name__ , __magic_name__ ) def __call__( self : int , __magic_name__ : List[str]=None , __magic_name__ : Tuple=None , __magic_name__ : Any=None , **__magic_name__ : Union[str, Any] ) -> Optional[int]: """simple docstring""" 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: __snake_case : int = self.tokenizer(__magic_name__ , return_tensors=__magic_name__ , **__magic_name__ ) if images is not None: __snake_case : str = self.image_processor(__magic_name__ , return_tensors=__magic_name__ , **__magic_name__ ) if text is not None and images is not None: __snake_case : Union[str, Any] = image_features.pixel_values return encoding elif text is not None: return encoding else: return BatchEncoding(data=dict(**__magic_name__ ) , tensor_type=__magic_name__ ) def lowercase__ ( self : Optional[int] , *__magic_name__ : List[Any] , **__magic_name__ : Any ) -> Optional[Any]: """simple docstring""" return self.tokenizer.batch_decode(*__magic_name__ , **__magic_name__ ) def lowercase__ ( self : List[str] , *__magic_name__ : Tuple , **__magic_name__ : List[Any] ) -> int: """simple docstring""" return self.tokenizer.decode(*__magic_name__ , **__magic_name__ ) @property def lowercase__ ( self : List[Any] ) -> Optional[Any]: """simple docstring""" __snake_case : Dict = self.tokenizer.model_input_names __snake_case : str = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) ) @property def lowercase__ ( self : int ) -> List[str]: """simple docstring""" warnings.warn( """`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.""" , __magic_name__ , ) return self.image_processor_class @property def lowercase__ ( self : Union[str, Any] ) -> Dict: """simple docstring""" warnings.warn( """`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.""" , __magic_name__ , ) return self.image_processor
26
0
"""simple docstring""" def _A (__a ) -> bool: """simple docstring""" if not isinstance(_lowerCamelCase , _lowerCamelCase ): SCREAMING_SNAKE_CASE_ : Dict = f'Input value of [number={number}] must be an integer' raise TypeError(_lowerCamelCase ) if number < 0: return False SCREAMING_SNAKE_CASE_ : List[str] = number * number while number > 0: if number % 10 != number_square % 10: return False number //= 10 number_square //= 10 return True if __name__ == "__main__": import doctest doctest.testmod()
512
'''simple docstring''' import datasets import faiss import numpy as np import streamlit as st import torch from elasticsearch import Elasticsearch from elia_utils import ( embed_questions_for_retrieval, make_qa_sas_model, qa_sas_generate, query_es_index, query_qa_dense_index, ) import transformers from transformers import AutoModel, AutoModelForSeqaSeqLM, AutoTokenizer __UpperCamelCase = "bart" __UpperCamelCase = True @st.cache(allow_output_mutation=_lowerCamelCase ) def _a ( ) -> Union[str, Any]: """simple docstring""" if LOAD_DENSE_INDEX: __snake_case : int = AutoTokenizer.from_pretrained("""yjernite/retribert-base-uncased""" ) __snake_case : Tuple = AutoModel.from_pretrained("""yjernite/retribert-base-uncased""" ).to("""cuda:0""" ) __snake_case : List[Any] = qar_model.eval() else: __snake_case , __snake_case : Optional[Any] = (None, None) if MODEL_TYPE == "bart": __snake_case : List[str] = AutoTokenizer.from_pretrained("""yjernite/bart_eli5""" ) __snake_case : Any = AutoModelForSeqaSeqLM.from_pretrained("""yjernite/bart_eli5""" ).to("""cuda:0""" ) __snake_case : int = torch.load("""seq2seq_models/eli5_bart_model_blm_2.pth""" ) sas_model.load_state_dict(save_dict["""model"""] ) __snake_case : int = sas_model.eval() else: __snake_case , __snake_case : Dict = make_qa_sas_model( model_name="""t5-small""" , from_file="""seq2seq_models/eli5_t5_model_1024_4.pth""" , device="""cuda:0""" ) return (qar_tokenizer, qar_model, sas_tokenizer, sas_model) @st.cache(allow_output_mutation=_lowerCamelCase ) def _a ( ) -> Tuple: """simple docstring""" if LOAD_DENSE_INDEX: __snake_case : Tuple = faiss.StandardGpuResources() __snake_case : Optional[Any] = datasets.load_dataset(path="""wiki_snippets""" , name="""wiki40b_en_100_0""" )["""train"""] __snake_case : str = np.memmap( """wiki40b_passages_reps_32_l-8_h-768_b-512-512.dat""" , dtype="""float32""" , mode="""r""" , shape=(wikiaab_passages.num_rows, 128) , ) __snake_case : Optional[int] = faiss.IndexFlatIP(128 ) __snake_case : Any = faiss.index_cpu_to_gpu(_lowerCamelCase , 1 , _lowerCamelCase ) wikiaab_gpu_index_flat.add(_lowerCamelCase ) # TODO fix for larger GPU else: __snake_case , __snake_case : Tuple = (None, None) __snake_case : List[str] = Elasticsearch([{"""host""": """localhost""", """port""": """9200"""}] ) return (wikiaab_passages, wikiaab_gpu_index_flat, es_client) @st.cache(allow_output_mutation=_lowerCamelCase ) def _a ( ) -> List[Any]: """simple docstring""" __snake_case : Tuple = datasets.load_dataset("""eli5""" , name="""LFQA_reddit""" ) __snake_case : Dict = elia["""train_eli5"""] __snake_case : int = np.memmap( """eli5_questions_reps.dat""" , dtype="""float32""" , mode="""r""" , shape=(elia_train.num_rows, 128) ) __snake_case : Dict = faiss.IndexFlatIP(128 ) eli5_train_q_index.add(_lowerCamelCase ) return (elia_train, eli5_train_q_index) __UpperCamelCase , __UpperCamelCase , __UpperCamelCase = load_indexes() __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase = load_models() __UpperCamelCase , __UpperCamelCase = load_train_data() def _a ( _lowerCamelCase , _lowerCamelCase=10 ) -> int: """simple docstring""" __snake_case : Optional[int] = embed_questions_for_retrieval([question] , _lowerCamelCase , _lowerCamelCase ) __snake_case , __snake_case : Tuple = eli5_train_q_index.search(_lowerCamelCase , _lowerCamelCase ) __snake_case : Tuple = [elia_train[int(_lowerCamelCase )] for i in I[0]] return nn_examples def _a ( _lowerCamelCase , _lowerCamelCase="wiki40b" , _lowerCamelCase="dense" , _lowerCamelCase=10 ) -> Optional[Any]: """simple docstring""" if source == "none": __snake_case , __snake_case : Dict = (""" <P> """.join(["""""" for _ in range(11 )] ).strip(), []) else: if method == "dense": __snake_case , __snake_case : Dict = query_qa_dense_index( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) else: __snake_case , __snake_case : str = query_es_index( _lowerCamelCase , _lowerCamelCase , index_name="""english_wiki40b_snippets_100w""" , n_results=_lowerCamelCase , ) __snake_case : Optional[int] = [ (res["""article_title"""], res["""section_title"""].strip(), res["""score"""], res["""passage_text"""]) for res in hit_lst ] __snake_case : Optional[Any] = """question: {} context: {}""".format(_lowerCamelCase , _lowerCamelCase ) return question_doc, support_list @st.cache( hash_funcs={ torch.Tensor: (lambda _lowerCamelCase : None), transformers.models.bart.tokenization_bart.BartTokenizer: (lambda _lowerCamelCase : None), } ) def _a ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase=64 , _lowerCamelCase=256 , _lowerCamelCase=False , _lowerCamelCase=2 , _lowerCamelCase=0.95 , _lowerCamelCase=0.8 ) -> List[str]: """simple docstring""" with torch.no_grad(): __snake_case : Union[str, Any] = qa_sas_generate( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , num_answers=1 , num_beams=_lowerCamelCase , min_len=_lowerCamelCase , max_len=_lowerCamelCase , do_sample=_lowerCamelCase , temp=_lowerCamelCase , top_p=_lowerCamelCase , top_k=_lowerCamelCase , max_input_length=1024 , device="""cuda:0""" , )[0] return (answer, support_list) st.title("Long Form Question Answering with ELI5") # Start sidebar __UpperCamelCase = "<img src='https://huggingface.co/front/assets/huggingface_logo.svg'>" __UpperCamelCase = "\n<html>\n <head>\n <style>\n .img-container {\n padding-left: 90px;\n padding-right: 90px;\n padding-top: 50px;\n padding-bottom: 50px;\n background-color: #f0f3f9;\n }\n </style>\n </head>\n <body>\n <span class=\"img-container\"> <!-- Inline parent element -->\n %s\n </span>\n </body>\n</html>\n" % ( header_html, ) st.sidebar.markdown( header_full, unsafe_allow_html=True, ) # Long Form QA with ELI5 and Wikipedia __UpperCamelCase = "\nThis demo presents a model trained to [provide long-form answers to open-domain questions](https://yjernite.github.io/lfqa.html).\nFirst, a document retriever fetches a set of relevant Wikipedia passages given the question from the [Wiki40b](https://research.google/pubs/pub49029/) dataset,\na pre-processed fixed snapshot of Wikipedia.\n" st.sidebar.markdown(description, unsafe_allow_html=True) __UpperCamelCase = [ "Answer the question", "View the retrieved document only", "View the most similar ELI5 question and answer", "Show me everything, please!", ] __UpperCamelCase = st.sidebar.checkbox("Demo options") if demo_options: __UpperCamelCase = st.sidebar.selectbox( "", action_list, index=3, ) __UpperCamelCase = action_list.index(action_st) __UpperCamelCase = st.sidebar.selectbox( "", ["Show full text of passages", "Show passage section titles"], index=0, ) __UpperCamelCase = show_type == "Show full text of passages" else: __UpperCamelCase = 3 __UpperCamelCase = True __UpperCamelCase = st.sidebar.checkbox("Retrieval options") if retrieval_options: __UpperCamelCase = "\n ### Information retriever options\n\n The **sparse** retriever uses ElasticSearch, while the **dense** retriever uses max-inner-product search between a question and passage embedding\n trained using the [ELI5](https://arxiv.org/abs/1907.09190) questions-answer pairs.\n The answer is then generated by sequence to sequence model which takes the question and retrieved document as input.\n " st.sidebar.markdown(retriever_info) __UpperCamelCase = st.sidebar.selectbox("Which Wikipedia format should the model use?", ["wiki40b", "none"]) __UpperCamelCase = st.sidebar.selectbox("Which Wikipedia indexer should the model use?", ["dense", "sparse", "mixed"]) else: __UpperCamelCase = "wiki40b" __UpperCamelCase = "dense" __UpperCamelCase = "beam" __UpperCamelCase = 2 __UpperCamelCase = 64 __UpperCamelCase = 256 __UpperCamelCase = None __UpperCamelCase = None __UpperCamelCase = st.sidebar.checkbox("Generation options") if generate_options: __UpperCamelCase = "\n ### Answer generation options\n\n The sequence-to-sequence model was initialized with [BART](https://huggingface.co/facebook/bart-large)\n weights and fine-tuned on the ELI5 QA pairs and retrieved documents. You can use the model for greedy decoding with\n **beam** search, or **sample** from the decoder's output probabilities.\n " st.sidebar.markdown(generate_info) __UpperCamelCase = st.sidebar.selectbox("Would you like to use beam search or sample an answer?", ["beam", "sampled"]) __UpperCamelCase = st.sidebar.slider( "Minimum generation length", min_value=8, max_value=256, value=64, step=8, format=None, key=None ) __UpperCamelCase = st.sidebar.slider( "Maximum generation length", min_value=64, max_value=512, value=256, step=16, format=None, key=None ) if sampled == "beam": __UpperCamelCase = st.sidebar.slider("Beam size", min_value=1, max_value=8, value=2, step=None, format=None, key=None) else: __UpperCamelCase = st.sidebar.slider( "Nucleus sampling p", min_value=0.1, max_value=1.0, value=0.95, step=0.01, format=None, key=None ) __UpperCamelCase = st.sidebar.slider( "Temperature", min_value=0.1, max_value=1.0, value=0.7, step=0.01, format=None, key=None ) __UpperCamelCase = None # start main text __UpperCamelCase = [ "<MY QUESTION>", "How do people make chocolate?", "Why do we get a fever when we are sick?", "How can different animals perceive different colors?", "What is natural language processing?", "What's the best way to treat a sunburn?", "What exactly are vitamins ?", "How does nuclear energy provide electricity?", "What's the difference between viruses and bacteria?", "Why are flutes classified as woodwinds when most of them are made out of metal ?", "Why do people like drinking coffee even though it tastes so bad?", "What happens when wine ages? How does it make the wine taste better?", "If an animal is an herbivore, where does it get the protein that it needs to survive if it only eats grass?", "How can we set a date to the beginning or end of an artistic period? Doesn't the change happen gradually?", "How does New Zealand have so many large bird predators?", ] __UpperCamelCase = st.selectbox( "What would you like to ask? ---- select <MY QUESTION> to enter a new query", questions_list, index=1, ) if question_s == "<MY QUESTION>": __UpperCamelCase = st.text_input("Enter your question here:", "") else: __UpperCamelCase = question_s if st.button("Show me!"): if action in [0, 1, 3]: if index_type == "mixed": __UpperCamelCase , __UpperCamelCase = make_support(question, source=wiki_source, method="dense", n_results=10) __UpperCamelCase , __UpperCamelCase = make_support(question, source=wiki_source, method="sparse", n_results=10) __UpperCamelCase = [] for res_d, res_s in zip(support_list_dense, support_list_sparse): if tuple(res_d) not in support_list: support_list += [tuple(res_d)] if tuple(res_s) not in support_list: support_list += [tuple(res_s)] __UpperCamelCase = support_list[:10] __UpperCamelCase = "<P> " + " <P> ".join([res[-1] for res in support_list]) else: __UpperCamelCase , __UpperCamelCase = make_support(question, source=wiki_source, method=index_type, n_results=10) if action in [0, 3]: __UpperCamelCase , __UpperCamelCase = answer_question( question_doc, sas_model, sas_tokenizer, min_len=min_len, max_len=int(max_len), sampling=(sampled == "sampled"), n_beams=n_beams, top_p=top_p, temp=temp, ) st.markdown("### The model generated answer is:") st.write(answer) if action in [0, 1, 3] and wiki_source != "none": st.markdown("--- \n ### The model is drawing information from the following Wikipedia passages:") for i, res in enumerate(support_list): __UpperCamelCase = "https://en.wikipedia.org/wiki/{}".format(res[0].replace(" ", "_")) __UpperCamelCase = res[1].strip() if sec_titles == "": __UpperCamelCase = "[{}]({})".format(res[0], wiki_url) else: __UpperCamelCase = sec_titles.split(" & ") __UpperCamelCase = " & ".join( ["[{}]({}#{})".format(sec.strip(), wiki_url, sec.strip().replace(" ", "_")) for sec in sec_list] ) st.markdown( "{0:02d} - **Article**: {1:<18} <br> _Section_: {2}".format(i + 1, res[0], sections), unsafe_allow_html=True, ) if show_passages: st.write( "> <span style=\"font-family:arial; font-size:10pt;\">" + res[-1] + "</span>", unsafe_allow_html=True ) if action in [2, 3]: __UpperCamelCase = find_nearest_training(question) __UpperCamelCase = nn_train_list[0] st.markdown( "--- \n ### The most similar question in the ELI5 training set was: \n\n {}".format(train_exple["title"]) ) __UpperCamelCase = [ "{}. {}".format(i + 1, " \n".join([line.strip() for line in ans.split("\n") if line.strip() != ""])) for i, (ans, sc) in enumerate(zip(train_exple["answers"]["text"], train_exple["answers"]["score"])) if i == 0 or sc > 2 ] st.markdown("##### Its answers were: \n\n {}".format("\n".join(answers_st))) __UpperCamelCase = "\n---\n\n**Disclaimer**\n\n*The intent of this app is to provide some (hopefully entertaining) insights into the behavior of a current LFQA system.\nEvaluating biases of such a model and ensuring factual generations are still very much open research problems.\nTherefore, until some significant progress is achieved, we caution against using the generated answers for practical purposes.*\n" st.sidebar.markdown(disclaimer, unsafe_allow_html=True)
26
0
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_tokenizers_available, is_torch_available, ) A = { "configuration_deberta": ["DEBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP", "DebertaConfig", "DebertaOnnxConfig"], "tokenization_deberta": ["DebertaTokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A = ["DebertaTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A = [ "DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST", "DebertaForMaskedLM", "DebertaForQuestionAnswering", "DebertaForSequenceClassification", "DebertaForTokenClassification", "DebertaModel", "DebertaPreTrainedModel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A = [ "TF_DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST", "TFDebertaForMaskedLM", "TFDebertaForQuestionAnswering", "TFDebertaForSequenceClassification", "TFDebertaForTokenClassification", "TFDebertaModel", "TFDebertaPreTrainedModel", ] if TYPE_CHECKING: from .configuration_deberta import DEBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, DebertaConfig, DebertaOnnxConfig from .tokenization_deberta import DebertaTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_deberta_fast import DebertaTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_deberta import ( DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, DebertaForMaskedLM, DebertaForQuestionAnswering, DebertaForSequenceClassification, DebertaForTokenClassification, DebertaModel, DebertaPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_deberta import ( TF_DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, TFDebertaForMaskedLM, TFDebertaForQuestionAnswering, TFDebertaForSequenceClassification, TFDebertaForTokenClassification, TFDebertaModel, TFDebertaPreTrainedModel, ) else: import sys A = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
475
'''simple docstring''' import warnings from ...utils import logging from .image_processing_owlvit import OwlViTImageProcessor __UpperCamelCase = logging.get_logger(__name__) class _A ( __lowercase ): def __init__( self : int , *__magic_name__ : Optional[Any] , **__magic_name__ : Any ) -> None: """simple docstring""" warnings.warn( """The class OwlViTFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please""" """ use OwlViTImageProcessor instead.""" , __magic_name__ , ) super().__init__(*__magic_name__ , **__magic_name__ )
26
0
from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging if TYPE_CHECKING: from ... import FeatureExtractionMixin, TensorType A : Dict = logging.get_logger(__name__) A : str = { 'openai/imagegpt-small': '', 'openai/imagegpt-medium': '', 'openai/imagegpt-large': '', } class UpperCamelCase( __lowercase ): snake_case_ : Dict = '''imagegpt''' snake_case_ : Optional[int] = ['''past_key_values'''] snake_case_ : Union[str, Any] = { '''hidden_size''': '''n_embd''', '''max_position_embeddings''': '''n_positions''', '''num_attention_heads''': '''n_head''', '''num_hidden_layers''': '''n_layer''', } def __init__( self : str , SCREAMING_SNAKE_CASE : str=5_1_2 + 1 , SCREAMING_SNAKE_CASE : Union[str, Any]=3_2 * 3_2 , SCREAMING_SNAKE_CASE : List[Any]=5_1_2 , SCREAMING_SNAKE_CASE : Any=2_4 , SCREAMING_SNAKE_CASE : List[str]=8 , SCREAMING_SNAKE_CASE : int=None , SCREAMING_SNAKE_CASE : Tuple="quick_gelu" , SCREAMING_SNAKE_CASE : Optional[Any]=0.1 , SCREAMING_SNAKE_CASE : List[str]=0.1 , SCREAMING_SNAKE_CASE : Optional[Any]=0.1 , SCREAMING_SNAKE_CASE : Union[str, Any]=1e-5 , SCREAMING_SNAKE_CASE : Optional[Any]=0.02 , SCREAMING_SNAKE_CASE : Optional[Any]=True , SCREAMING_SNAKE_CASE : Dict=True , SCREAMING_SNAKE_CASE : Any=False , SCREAMING_SNAKE_CASE : Optional[Any]=False , SCREAMING_SNAKE_CASE : Dict=False , **SCREAMING_SNAKE_CASE : Dict , ) -> Optional[Any]: '''simple docstring''' __snake_case = vocab_size __snake_case = n_positions __snake_case = n_embd __snake_case = n_layer __snake_case = n_head __snake_case = n_inner __snake_case = activation_function __snake_case = resid_pdrop __snake_case = embd_pdrop __snake_case = attn_pdrop __snake_case = layer_norm_epsilon __snake_case = initializer_range __snake_case = scale_attn_weights __snake_case = use_cache __snake_case = scale_attn_by_inverse_layer_idx __snake_case = reorder_and_upcast_attn __snake_case = tie_word_embeddings super().__init__(tie_word_embeddings=SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) class UpperCamelCase( __lowercase ): @property def SCREAMING_SNAKE_CASE_ ( self : List[str] ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' return OrderedDict( [ ("input_ids", {0: "batch", 1: "sequence"}), ] ) def SCREAMING_SNAKE_CASE_ ( self : Any , SCREAMING_SNAKE_CASE : "FeatureExtractionMixin" , SCREAMING_SNAKE_CASE : int = 1 , SCREAMING_SNAKE_CASE : int = -1 , SCREAMING_SNAKE_CASE : bool = False , SCREAMING_SNAKE_CASE : Optional["TensorType"] = None , SCREAMING_SNAKE_CASE : int = 3 , SCREAMING_SNAKE_CASE : int = 3_2 , SCREAMING_SNAKE_CASE : int = 3_2 , ) -> Mapping[str, Any]: '''simple docstring''' __snake_case = self._generate_dummy_images(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) __snake_case = dict(preprocessor(images=SCREAMING_SNAKE_CASE , return_tensors=SCREAMING_SNAKE_CASE ) ) return inputs
371
'''simple docstring''' import argparse import torch from transformers import BlenderbotConfig, BlenderbotForConditionalGeneration from transformers.utils import logging logging.set_verbosity_info() __UpperCamelCase = logging.get_logger(__name__) __UpperCamelCase = [ ["attention", "attn"], ["encoder_attention", "encoder_attn"], ["q_lin", "q_proj"], ["k_lin", "k_proj"], ["v_lin", "v_proj"], ["out_lin", "out_proj"], ["norm_embeddings", "layernorm_embedding"], ["position_embeddings", "embed_positions"], ["embeddings", "embed_tokens"], ["ffn.lin", "fc"], ] def _a ( _lowerCamelCase ) -> Union[str, Any]: """simple docstring""" if k == "embeddings.weight": return "shared.weight" for parlai_name, hf_name in PATTERNS: __snake_case : List[str] = k.replace(_lowerCamelCase , _lowerCamelCase ) if k.startswith("""encoder""" ): __snake_case : Optional[int] = k.replace(""".attn""" , """.self_attn""" ) __snake_case : Tuple = k.replace("""norm1""" , """self_attn_layer_norm""" ) __snake_case : List[str] = k.replace("""norm2""" , """final_layer_norm""" ) elif k.startswith("""decoder""" ): __snake_case : List[Any] = k.replace("""norm1""" , """self_attn_layer_norm""" ) __snake_case : str = k.replace("""norm2""" , """encoder_attn_layer_norm""" ) __snake_case : Optional[int] = k.replace("""norm3""" , """final_layer_norm""" ) return k def _a ( _lowerCamelCase ) -> Any: """simple docstring""" __snake_case : Optional[int] = [ """model.encoder.layernorm_embedding.weight""", """model.encoder.layernorm_embedding.bias""", """model.decoder.layernorm_embedding.weight""", """model.decoder.layernorm_embedding.bias""", ] for k in keys: __snake_case : Optional[Any] = sd.pop(_lowerCamelCase ) __snake_case : List[str] = k.replace("""layernorm_embedding""" , """layer_norm""" ) assert new_k not in sd __snake_case : Union[str, Any] = v __UpperCamelCase = ["START"] @torch.no_grad() def _a ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) -> Dict: """simple docstring""" __snake_case : Optional[int] = torch.load(_lowerCamelCase , map_location="""cpu""" ) __snake_case : Dict = model["""model"""] __snake_case : Optional[int] = BlenderbotConfig.from_json_file(_lowerCamelCase ) __snake_case : Union[str, Any] = BlenderbotForConditionalGeneration(_lowerCamelCase ) __snake_case : List[Any] = m.model.state_dict().keys() __snake_case : int = [] __snake_case : Union[str, Any] = {} for k, v in sd.items(): if k in IGNORE_KEYS: continue __snake_case : Optional[int] = rename_state_dict_key(_lowerCamelCase ) if new_k not in valid_keys: failures.append([k, new_k] ) else: __snake_case : str = v if cfg.normalize_before: # Blenderbot-3B checkpoints. Rename layernorm_embedding -> layer_norm rename_layernorm_keys(_lowerCamelCase ) m.model.load_state_dict(_lowerCamelCase , strict=_lowerCamelCase ) m.half() m.save_pretrained(_lowerCamelCase ) if __name__ == "__main__": __UpperCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument("--src_path", type=str, help="like blenderbot-model.bin") parser.add_argument("--save_dir", default="hf_blenderbot", type=str, help="Where to save converted model.") parser.add_argument( "--hf_config_json", default="blenderbot-3b-config.json", type=str, help="Path to config to use" ) __UpperCamelCase = parser.parse_args() convert_parlai_checkpoint(args.src_path, args.save_dir, args.hf_config_json)
26
0
from dataclasses import dataclass from typing import List, Optional, Union import numpy as np import PIL from ...utils import BaseOutput, OptionalDependencyNotAvailable, is_torch_available, is_transformers_available from .timesteps import ( fastaa_timesteps, smartaa_timesteps, smartaa_timesteps, smartaaa_timesteps, smartaaa_timesteps, superaa_timesteps, superaa_timesteps, superaaa_timesteps, ) @dataclass class lowercase__ (__lowercase ): """simple docstring""" __UpperCamelCase : Union[List[PIL.Image.Image], np.ndarray] __UpperCamelCase : Optional[List[bool]] __UpperCamelCase : Optional[List[bool]] try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import * # noqa F403 else: from .pipeline_if import IFPipeline from .pipeline_if_imgaimg import IFImgaImgPipeline from .pipeline_if_imgaimg_superresolution import IFImgaImgSuperResolutionPipeline from .pipeline_if_inpainting import IFInpaintingPipeline from .pipeline_if_inpainting_superresolution import IFInpaintingSuperResolutionPipeline from .pipeline_if_superresolution import IFSuperResolutionPipeline from .safety_checker import IFSafetyChecker from .watermark import IFWatermarker
648
'''simple docstring''' import argparse import os import re import packaging.version __UpperCamelCase = "examples/" __UpperCamelCase = { "examples": (re.compile(R"^check_min_version\(\"[^\"]+\"\)\s*$", re.MULTILINE), "check_min_version(\"VERSION\")\n"), "init": (re.compile(R"^__version__\s+=\s+\"([^\"]+)\"\s*$", re.MULTILINE), "__version__ = \"VERSION\"\n"), "setup": (re.compile(R"^(\s*)version\s*=\s*\"[^\"]+\",", re.MULTILINE), R"\1version=\"VERSION\","), "doc": (re.compile(R"^(\s*)release\s*=\s*\"[^\"]+\"$", re.MULTILINE), "release = \"VERSION\"\n"), } __UpperCamelCase = { "init": "src/transformers/__init__.py", "setup": "setup.py", } __UpperCamelCase = "README.md" def _a ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) -> Tuple: """simple docstring""" with open(_lowerCamelCase , """r""" , encoding="""utf-8""" , newline="""\n""" ) as f: __snake_case : Union[str, Any] = f.read() __snake_case , __snake_case : List[Any] = REPLACE_PATTERNS[pattern] __snake_case : Optional[Any] = replace.replace("""VERSION""" , _lowerCamelCase ) __snake_case : Optional[Any] = re_pattern.sub(_lowerCamelCase , _lowerCamelCase ) with open(_lowerCamelCase , """w""" , encoding="""utf-8""" , newline="""\n""" ) as f: f.write(_lowerCamelCase ) def _a ( _lowerCamelCase ) -> Union[str, Any]: """simple docstring""" for folder, directories, fnames in os.walk(_lowerCamelCase ): # Removing some of the folders with non-actively maintained examples from the walk if "research_projects" in directories: directories.remove("""research_projects""" ) if "legacy" in directories: directories.remove("""legacy""" ) for fname in fnames: if fname.endswith(""".py""" ): update_version_in_file(os.path.join(_lowerCamelCase , _lowerCamelCase ) , _lowerCamelCase , pattern="""examples""" ) def _a ( _lowerCamelCase , _lowerCamelCase=False ) -> str: """simple docstring""" for pattern, fname in REPLACE_FILES.items(): update_version_in_file(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) if not patch: update_version_in_examples(_lowerCamelCase ) def _a ( ) -> Optional[int]: """simple docstring""" __snake_case : str = """🤗 Transformers currently provides the following architectures""" __snake_case : List[Any] = """1. Want to contribute a new model?""" with open(_lowerCamelCase , """r""" , encoding="""utf-8""" , newline="""\n""" ) as f: __snake_case : List[str] = f.readlines() # Find the start of the list. __snake_case : Optional[Any] = 0 while not lines[start_index].startswith(_start_prompt ): start_index += 1 start_index += 1 __snake_case : int = start_index # Update the lines in the model list. while not lines[index].startswith(_end_prompt ): if lines[index].startswith("""1.""" ): __snake_case : Optional[Any] = lines[index].replace( """https://huggingface.co/docs/transformers/main/model_doc""" , """https://huggingface.co/docs/transformers/model_doc""" , ) index += 1 with open(_lowerCamelCase , """w""" , encoding="""utf-8""" , newline="""\n""" ) as f: f.writelines(_lowerCamelCase ) def _a ( ) -> Union[str, Any]: """simple docstring""" with open(REPLACE_FILES["""init"""] , """r""" ) as f: __snake_case : List[Any] = f.read() __snake_case : str = REPLACE_PATTERNS["""init"""][0].search(_lowerCamelCase ).groups()[0] return packaging.version.parse(_lowerCamelCase ) def _a ( _lowerCamelCase=False ) -> int: """simple docstring""" __snake_case : List[Any] = get_version() if patch and default_version.is_devrelease: raise ValueError("""Can't create a patch version from the dev branch, checkout a released version!""" ) if default_version.is_devrelease: __snake_case : str = default_version.base_version elif patch: __snake_case : Optional[int] = F'''{default_version.major}.{default_version.minor}.{default_version.micro + 1}''' else: __snake_case : Dict = F'''{default_version.major}.{default_version.minor + 1}.0''' # Now let's ask nicely if that's the right one. __snake_case : Dict = input(F'''Which version are you releasing? [{default_version}]''' ) if len(_lowerCamelCase ) == 0: __snake_case : Any = default_version print(F'''Updating version to {version}.''' ) global_version_update(_lowerCamelCase , patch=_lowerCamelCase ) if not patch: print("""Cleaning main README, don't forget to run `make fix-copies`.""" ) clean_main_ref_in_model_list() def _a ( ) -> Tuple: """simple docstring""" __snake_case : Optional[Any] = get_version() __snake_case : Tuple = F'''{current_version.major}.{current_version.minor + 1}.0.dev0''' __snake_case : Union[str, Any] = current_version.base_version # Check with the user we got that right. __snake_case : int = input(F'''Which version are we developing now? [{dev_version}]''' ) if len(_lowerCamelCase ) == 0: __snake_case : Optional[int] = dev_version print(F'''Updating version to {version}.''' ) global_version_update(_lowerCamelCase ) print("""Cleaning main README, don't forget to run `make fix-copies`.""" ) clean_main_ref_in_model_list() if __name__ == "__main__": __UpperCamelCase = argparse.ArgumentParser() parser.add_argument("--post_release", action="store_true", help="Whether this is pre or post release.") parser.add_argument("--patch", action="store_true", help="Whether or not this is a patch release.") __UpperCamelCase = parser.parse_args() if not args.post_release: pre_release_work(patch=args.patch) elif args.patch: print("Nothing to do after a patch :-)") else: post_release_work()
26
0
"""simple docstring""" import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, XLMRobertaTokenizer from diffusers import AltDiffusionPipeline, AutoencoderKL, DDIMScheduler, PNDMScheduler, UNetaDConditionModel from diffusers.pipelines.alt_diffusion.modeling_roberta_series import ( RobertaSeriesConfig, RobertaSeriesModelWithTransformation, ) from diffusers.utils import slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class snake_case_ ( __lowercase ,__lowercase ,__lowercase ,unittest.TestCase ): __lowerCAmelCase = AltDiffusionPipeline __lowerCAmelCase = TEXT_TO_IMAGE_PARAMS __lowerCAmelCase = TEXT_TO_IMAGE_BATCH_PARAMS __lowerCAmelCase = TEXT_TO_IMAGE_IMAGE_PARAMS __lowerCAmelCase = TEXT_TO_IMAGE_IMAGE_PARAMS def snake_case_ ( self ): torch.manual_seed(0 ) a_ : Dict = UNetaDConditionModel( block_out_channels=(3_2, 6_4) , layers_per_block=2 , 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 , ) a_ : Any = DDIMScheduler( beta_start=0.00_085 , beta_end=0.012 , beta_schedule="scaled_linear" , clip_sample=a_ , set_alpha_to_one=a_ , ) torch.manual_seed(0 ) a_ : Any = 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 , ) # TODO: address the non-deterministic text encoder (fails for save-load tests) # torch.manual_seed(0) # text_encoder_config = RobertaSeriesConfig( # hidden_size=32, # project_dim=32, # intermediate_size=37, # layer_norm_eps=1e-05, # num_attention_heads=4, # num_hidden_layers=5, # vocab_size=5002, # ) # text_encoder = RobertaSeriesModelWithTransformation(text_encoder_config) torch.manual_seed(0 ) a_ : int = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=3_2 , projection_dim=3_2 , intermediate_size=3_7 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=5_0_0_2 , ) a_ : Dict = CLIPTextModel(a_ ) a_ : Optional[int] = XLMRobertaTokenizer.from_pretrained("hf-internal-testing/tiny-xlm-roberta" ) a_ : str = 7_7 a_ : List[Any] = { """unet""": unet, """scheduler""": scheduler, """vae""": vae, """text_encoder""": text_encoder, """tokenizer""": tokenizer, """safety_checker""": None, """feature_extractor""": None, } return components def snake_case_ ( self , a_ , a_=0 ): if str(a_ ).startswith("mps" ): a_ : int = torch.manual_seed(a_ ) else: a_ : Optional[Any] = torch.Generator(device=a_ ).manual_seed(a_ ) a_ : List[Any] = { """prompt""": """A painting of a squirrel eating a burger""", """generator""": generator, """num_inference_steps""": 2, """guidance_scale""": 6.0, """output_type""": """numpy""", } return inputs def snake_case_ ( self ): super().test_attention_slicing_forward_pass(expected_max_diff=3e-3 ) def snake_case_ ( self ): super().test_inference_batch_single_identical(expected_max_diff=3e-3 ) def snake_case_ ( self ): a_ : Dict = """cpu""" # ensure determinism for the device-dependent torch.Generator a_ : Optional[Any] = self.get_dummy_components() torch.manual_seed(0 ) a_ : str = RobertaSeriesConfig( hidden_size=3_2 , project_dim=3_2 , intermediate_size=3_7 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , vocab_size=5_0_0_2 , ) # TODO: remove after fixing the non-deterministic text encoder a_ : Optional[int] = RobertaSeriesModelWithTransformation(a_ ) a_ : Optional[Any] = text_encoder a_ : Any = AltDiffusionPipeline(**a_ ) a_ : Any = alt_pipe.to(a_ ) alt_pipe.set_progress_bar_config(disable=a_ ) a_ : int = self.get_dummy_inputs(a_ ) a_ : Union[str, Any] = """A photo of an astronaut""" a_ : Any = alt_pipe(**a_ ) a_ : List[Any] = output.images a_ : Optional[Any] = image[0, -3:, -3:, -1] assert image.shape == (1, 6_4, 6_4, 3) a_ : int = np.array( [0.5_748_162, 0.60_447_145, 0.48_821_217, 0.50_100_636, 0.5_431_185, 0.45_763_683, 0.49_657_696, 0.48_132_733, 0.47_573_093] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def snake_case_ ( self ): a_ : Any = """cpu""" # ensure determinism for the device-dependent torch.Generator a_ : Any = self.get_dummy_components() a_ : List[Any] = PNDMScheduler(skip_prk_steps=a_ ) torch.manual_seed(0 ) a_ : int = RobertaSeriesConfig( hidden_size=3_2 , project_dim=3_2 , intermediate_size=3_7 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , vocab_size=5_0_0_2 , ) # TODO: remove after fixing the non-deterministic text encoder a_ : Dict = RobertaSeriesModelWithTransformation(a_ ) a_ : Tuple = text_encoder a_ : Optional[Any] = AltDiffusionPipeline(**a_ ) a_ : Optional[int] = alt_pipe.to(a_ ) alt_pipe.set_progress_bar_config(disable=a_ ) a_ : Dict = self.get_dummy_inputs(a_ ) a_ : List[Any] = alt_pipe(**a_ ) a_ : Dict = output.images a_ : Any = image[0, -3:, -3:, -1] assert image.shape == (1, 6_4, 6_4, 3) a_ : str = np.array( [0.51_605_093, 0.5_707_241, 0.47_365_507, 0.50_578_886, 0.5_633_877, 0.4_642_503, 0.5_182_081, 0.48_763_484, 0.49_084_237] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 @slow @require_torch_gpu class snake_case_ ( unittest.TestCase ): def snake_case_ ( self ): super().tearDown() gc.collect() torch.cuda.empty_cache() def snake_case_ ( self ): a_ : List[str] = AltDiffusionPipeline.from_pretrained("BAAI/AltDiffusion" , safety_checker=a_ ) a_ : List[str] = alt_pipe.to(a_ ) alt_pipe.set_progress_bar_config(disable=a_ ) a_ : int = """A painting of a squirrel eating a burger""" a_ : Tuple = torch.manual_seed(0 ) a_ : Dict = alt_pipe([prompt] , generator=a_ , guidance_scale=6.0 , num_inference_steps=2_0 , output_type="np" ) a_ : Optional[Any] = output.images a_ : Optional[Any] = image[0, -3:, -3:, -1] assert image.shape == (1, 5_1_2, 5_1_2, 3) a_ : str = np.array([0.1_010, 0.0_800, 0.0_794, 0.0_885, 0.0_843, 0.0_762, 0.0_769, 0.0_729, 0.0_586] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def snake_case_ ( self ): a_ : Union[str, Any] = DDIMScheduler.from_pretrained("BAAI/AltDiffusion" , subfolder="scheduler" ) a_ : Dict = AltDiffusionPipeline.from_pretrained("BAAI/AltDiffusion" , scheduler=a_ , safety_checker=a_ ) a_ : List[str] = alt_pipe.to(a_ ) alt_pipe.set_progress_bar_config(disable=a_ ) a_ : Dict = """A painting of a squirrel eating a burger""" a_ : Dict = torch.manual_seed(0 ) a_ : Any = alt_pipe([prompt] , generator=a_ , num_inference_steps=2 , output_type="numpy" ) a_ : List[Any] = output.images a_ : Tuple = image[0, -3:, -3:, -1] assert image.shape == (1, 5_1_2, 5_1_2, 3) a_ : Union[str, Any] = np.array([0.4_019, 0.4_052, 0.3_810, 0.4_119, 0.3_916, 0.3_982, 0.4_651, 0.4_195, 0.5_323] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
237
'''simple docstring''' from unittest import TestCase from datasets import Sequence, Value from datasets.arrow_dataset import Dataset class _A ( __lowercase ): def lowercase__ ( self : Any ) -> str: """simple docstring""" return [ {"col_1": 3, "col_2": "a"}, {"col_1": 2, "col_2": "b"}, {"col_1": 1, "col_2": "c"}, {"col_1": 0, "col_2": "d"}, ] def lowercase__ ( self : str ) -> int: """simple docstring""" __snake_case : Union[str, Any] = {"""col_1""": [3, 2, 1, 0], """col_2""": ["""a""", """b""", """c""", """d"""]} return Dataset.from_dict(__magic_name__ ) def lowercase__ ( self : str ) -> List[Any]: """simple docstring""" __snake_case : Any = self._create_example_records() __snake_case : str = Dataset.from_list(__magic_name__ ) self.assertListEqual(dset.column_names , ["""col_1""", """col_2"""] ) for i, r in enumerate(__magic_name__ ): self.assertDictEqual(__magic_name__ , example_records[i] ) def lowercase__ ( self : Optional[Any] ) -> List[Any]: """simple docstring""" __snake_case : List[Any] = self._create_example_records() __snake_case : Dict = Dataset.from_list(__magic_name__ ) __snake_case : List[Any] = Dataset.from_dict({k: [r[k] for r in example_records] for k in example_records[0]} ) self.assertEqual(dset.info , dset_from_dict.info ) def lowercase__ ( self : str ) -> List[Any]: # checks what happens with missing columns """simple docstring""" __snake_case : Union[str, Any] = [{"""col_1""": 1}, {"""col_2""": """x"""}] __snake_case : Optional[int] = Dataset.from_list(__magic_name__ ) self.assertDictEqual(dset[0] , {"""col_1""": 1} ) self.assertDictEqual(dset[1] , {"""col_1""": None} ) # NB: first record is used for columns def lowercase__ ( self : List[str] ) -> Optional[Any]: # checks if the type can be inferred from the second record """simple docstring""" __snake_case : List[Any] = [{"""col_1""": []}, {"""col_1""": [1, 2]}] __snake_case : int = Dataset.from_list(__magic_name__ ) self.assertEqual(dset.info.features["""col_1"""] , Sequence(Value("""int64""" ) ) ) def lowercase__ ( self : int ) -> Union[str, Any]: """simple docstring""" __snake_case : Tuple = Dataset.from_list([] ) self.assertEqual(len(__magic_name__ ) , 0 ) self.assertListEqual(dset.column_names , [] )
26
0
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCamelCase__ = logging.get_logger(__name__) UpperCamelCase__ = { '''facebook/xglm-564M''': '''https://huggingface.co/facebook/xglm-564M/resolve/main/config.json''', # See all XGLM models at https://huggingface.co/models?filter=xglm } class a__ ( __lowercase ): snake_case__ = '''xglm''' snake_case__ = ['''past_key_values'''] snake_case__ = { '''num_attention_heads''': '''attention_heads''', '''hidden_size''': '''d_model''', '''num_hidden_layers''': '''num_layers''', } def __init__( self : Tuple ,a__ : str=25_6008 ,a__ : int=2048 ,a__ : List[Any]=1024 ,a__ : Union[str, Any]=4096 ,a__ : Optional[Any]=24 ,a__ : Union[str, Any]=16 ,a__ : Dict="gelu" ,a__ : Union[str, Any]=0.1 ,a__ : Any=0.1 ,a__ : Optional[int]=0.0 ,a__ : Dict=0.0 ,a__ : List[Any]=0.02 ,a__ : Any=True ,a__ : List[str]=True ,a__ : Union[str, Any]=2 ,a__ : Any=1 ,a__ : Tuple=0 ,a__ : Any=2 ,**a__ : int ,) -> int: """simple docstring""" _lowerCAmelCase:Tuple = vocab_size _lowerCAmelCase:Any = max_position_embeddings _lowerCAmelCase:Optional[Any] = d_model _lowerCAmelCase:Tuple = ffn_dim _lowerCAmelCase:Optional[int] = num_layers _lowerCAmelCase:Union[str, Any] = attention_heads _lowerCAmelCase:Union[str, Any] = activation_function _lowerCAmelCase:Optional[int] = dropout _lowerCAmelCase:Optional[int] = attention_dropout _lowerCAmelCase:Any = activation_dropout _lowerCAmelCase:Dict = layerdrop _lowerCAmelCase:List[Any] = init_std _lowerCAmelCase:List[Any] = scale_embedding # scale factor will be sqrt(d_model) if True _lowerCAmelCase:Any = use_cache super().__init__( pad_token_id=a__ ,bos_token_id=a__ ,eos_token_id=a__ ,decoder_start_token_id=a__ ,**a__ ,)
227
'''simple docstring''' import inspect import unittest import torch import torch.nn as nn from accelerate.hooks import ( AlignDevicesHook, ModelHook, SequentialHook, add_hook_to_module, attach_align_device_hook, remove_hook_from_module, remove_hook_from_submodules, ) from accelerate.test_utils import require_multi_gpu class _A ( nn.Module ): def __init__( self : List[str] ) -> Optional[Any]: """simple docstring""" super().__init__() __snake_case : List[Any] = nn.Linear(3 , 4 ) __snake_case : str = nn.BatchNormad(4 ) __snake_case : Optional[Any] = nn.Linear(4 , 5 ) def lowercase__ ( self : str , __magic_name__ : Dict ) -> List[str]: """simple docstring""" return self.lineara(self.batchnorm(self.lineara(__magic_name__ ) ) ) class _A ( __lowercase ): def lowercase__ ( self : List[str] , __magic_name__ : Tuple , *__magic_name__ : Dict , **__magic_name__ : Optional[Any] ) -> Tuple: """simple docstring""" return (args[0] + 1,) + args[1:], kwargs class _A ( __lowercase ): def lowercase__ ( self : str , __magic_name__ : Union[str, Any] , __magic_name__ : Tuple ) -> Union[str, Any]: """simple docstring""" return output + 1 class _A ( unittest.TestCase ): def lowercase__ ( self : Dict ) -> Any: """simple docstring""" __snake_case : int = ModelForTest() __snake_case : Tuple = ModelHook() add_hook_to_module(__magic_name__ , __magic_name__ ) self.assertEqual(test_model._hf_hook , __magic_name__ ) self.assertTrue(hasattr(__magic_name__ , """_old_forward""" ) ) # Check adding the hook did not change the name or the signature self.assertEqual(test_model.forward.__name__ , """forward""" ) self.assertListEqual(list(inspect.signature(test_model.forward ).parameters ) , ["""x"""] ) remove_hook_from_module(__magic_name__ ) self.assertFalse(hasattr(__magic_name__ , """_hf_hook""" ) ) self.assertFalse(hasattr(__magic_name__ , """_old_forward""" ) ) def lowercase__ ( self : Tuple ) -> List[str]: """simple docstring""" __snake_case : List[Any] = ModelForTest() __snake_case : Optional[int] = ModelHook() add_hook_to_module(__magic_name__ , __magic_name__ ) add_hook_to_module(__magic_name__ , __magic_name__ , append=__magic_name__ ) self.assertEqual(isinstance(test_model._hf_hook , __magic_name__ ) , __magic_name__ ) self.assertEqual(len(test_model._hf_hook.hooks ) , 2 ) self.assertTrue(hasattr(__magic_name__ , """_old_forward""" ) ) # Check adding the hook did not change the name or the signature self.assertEqual(test_model.forward.__name__ , """forward""" ) self.assertListEqual(list(inspect.signature(test_model.forward ).parameters ) , ["""x"""] ) remove_hook_from_module(__magic_name__ ) self.assertFalse(hasattr(__magic_name__ , """_hf_hook""" ) ) self.assertFalse(hasattr(__magic_name__ , """_old_forward""" ) ) def lowercase__ ( self : str ) -> Union[str, Any]: """simple docstring""" __snake_case : List[Any] = ModelForTest() __snake_case : Any = torch.randn(2 , 3 ) __snake_case : str = test_model(x + 1 ) __snake_case : int = test_model(x + 2 ) __snake_case : Union[str, Any] = PreForwardHook() add_hook_to_module(__magic_name__ , __magic_name__ ) __snake_case : int = test_model(__magic_name__ ) self.assertTrue(torch.allclose(__magic_name__ , __magic_name__ , atol=1E-5 ) ) # Attaching a hook to a model when it already has one replaces, does not chain __snake_case : Optional[int] = PreForwardHook() add_hook_to_module(__magic_name__ , __magic_name__ ) __snake_case : List[Any] = test_model(__magic_name__ ) self.assertTrue(torch.allclose(__magic_name__ , __magic_name__ , atol=1E-5 ) ) # You need to use the sequential hook to chain two or more hooks __snake_case : Optional[int] = SequentialHook(PreForwardHook() , PreForwardHook() ) add_hook_to_module(__magic_name__ , __magic_name__ ) __snake_case : List[str] = test_model(__magic_name__ ) assert torch.allclose(__magic_name__ , __magic_name__ , atol=1E-5 ) def lowercase__ ( self : Union[str, Any] ) -> List[str]: """simple docstring""" __snake_case : Union[str, Any] = ModelForTest() __snake_case : str = torch.randn(2 , 3 ) __snake_case : Any = test_model(__magic_name__ ) __snake_case : Any = PostForwardHook() add_hook_to_module(__magic_name__ , __magic_name__ ) __snake_case : Any = test_model(__magic_name__ ) self.assertTrue(torch.allclose(__magic_name__ , output + 1 , atol=1E-5 ) ) # Attaching a hook to a model when it already has one replaces, does not chain __snake_case : Any = PostForwardHook() add_hook_to_module(__magic_name__ , __magic_name__ ) __snake_case : Dict = test_model(__magic_name__ ) self.assertTrue(torch.allclose(__magic_name__ , output + 1 , atol=1E-5 ) ) # You need to use the sequential hook to chain two or more hooks __snake_case : str = SequentialHook(PostForwardHook() , PostForwardHook() ) add_hook_to_module(__magic_name__ , __magic_name__ ) __snake_case : int = test_model(__magic_name__ ) assert torch.allclose(__magic_name__ , output + 2 , atol=1E-5 ) def lowercase__ ( self : str ) -> int: """simple docstring""" __snake_case : Union[str, Any] = ModelForTest() __snake_case : int = torch.randn(2 , 3 ) __snake_case : Any = test_model(__magic_name__ ) __snake_case : Dict = PostForwardHook() add_hook_to_module(__magic_name__ , __magic_name__ ) __snake_case : List[Any] = test_model(__magic_name__ ) self.assertTrue(torch.allclose(__magic_name__ , output + 1 ) ) self.assertTrue(outputa.requires_grad ) __snake_case : Dict = True __snake_case : int = test_model(__magic_name__ ) self.assertFalse(outputa.requires_grad ) @require_multi_gpu def lowercase__ ( self : Tuple ) -> List[Any]: """simple docstring""" __snake_case : Tuple = ModelForTest() # Everything is on CPU self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.batchnorm.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) # This will move each submodule on different devices add_hook_to_module(model.lineara , AlignDevicesHook(execution_device=0 ) ) add_hook_to_module(model.batchnorm , AlignDevicesHook(execution_device=0 ) ) add_hook_to_module(model.lineara , AlignDevicesHook(execution_device=1 ) ) self.assertEqual(model.lineara.weight.device , torch.device(0 ) ) self.assertEqual(model.batchnorm.weight.device , torch.device(0 ) ) self.assertEqual(model.batchnorm.running_mean.device , torch.device(0 ) ) self.assertEqual(model.lineara.weight.device , torch.device(1 ) ) # We can still make a forward pass. The input does not need to be on any particular device __snake_case : Tuple = torch.randn(2 , 3 ) __snake_case : Union[str, Any] = model(__magic_name__ ) self.assertEqual(output.device , torch.device(1 ) ) # We can add a general hook to put back output on same device as input. add_hook_to_module(__magic_name__ , AlignDevicesHook(io_same_device=__magic_name__ ) ) __snake_case : Tuple = torch.randn(2 , 3 ).to(0 ) __snake_case : Any = model(__magic_name__ ) self.assertEqual(output.device , torch.device(0 ) ) def lowercase__ ( self : Union[str, Any] ) -> str: """simple docstring""" __snake_case : int = ModelForTest() # Everything is on CPU self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.batchnorm.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) # This will move each submodule on different devices __snake_case : List[str] = {"""execution_device""": 0 if torch.cuda.is_available() else """cpu""", """offload""": True} add_hook_to_module(model.lineara , AlignDevicesHook(**__magic_name__ ) ) add_hook_to_module(model.batchnorm , AlignDevicesHook(**__magic_name__ ) ) add_hook_to_module(model.lineara , AlignDevicesHook(**__magic_name__ ) ) # Parameters have been offloaded, so on the meta device self.assertEqual(model.lineara.weight.device , torch.device("""meta""" ) ) self.assertEqual(model.batchnorm.weight.device , torch.device("""meta""" ) ) self.assertEqual(model.lineara.weight.device , torch.device("""meta""" ) ) # Buffers are not included in the offload by default, so are on the execution device __snake_case : Any = torch.device(hook_kwargs["""execution_device"""] ) self.assertEqual(model.batchnorm.running_mean.device , __magic_name__ ) __snake_case : Dict = torch.randn(2 , 3 ) __snake_case : Any = model(__magic_name__ ) self.assertEqual(output.device , __magic_name__ ) # Removing hooks loads back the weights in the model. remove_hook_from_module(model.lineara ) remove_hook_from_module(model.batchnorm ) remove_hook_from_module(model.lineara ) self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.batchnorm.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) # Now test with buffers included in the offload __snake_case : int = { """execution_device""": 0 if torch.cuda.is_available() else """cpu""", """offload""": True, """offload_buffers""": True, } add_hook_to_module(model.lineara , AlignDevicesHook(**__magic_name__ ) ) add_hook_to_module(model.batchnorm , AlignDevicesHook(**__magic_name__ ) ) add_hook_to_module(model.lineara , AlignDevicesHook(**__magic_name__ ) ) # Parameters have been offloaded, so on the meta device, buffers included self.assertEqual(model.lineara.weight.device , torch.device("""meta""" ) ) self.assertEqual(model.batchnorm.weight.device , torch.device("""meta""" ) ) self.assertEqual(model.lineara.weight.device , torch.device("""meta""" ) ) self.assertEqual(model.batchnorm.running_mean.device , torch.device("""meta""" ) ) __snake_case : str = torch.randn(2 , 3 ) __snake_case : str = model(__magic_name__ ) self.assertEqual(output.device , __magic_name__ ) # Removing hooks loads back the weights in the model. remove_hook_from_module(model.lineara ) remove_hook_from_module(model.batchnorm ) remove_hook_from_module(model.lineara ) self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.batchnorm.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) def lowercase__ ( self : Dict ) -> str: """simple docstring""" __snake_case : Tuple = ModelForTest() # Everything is on CPU self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.batchnorm.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) # This will move each submodule on different devices __snake_case : Union[str, Any] = 0 if torch.cuda.is_available() else """cpu""" attach_align_device_hook(__magic_name__ , execution_device=__magic_name__ , offload=__magic_name__ ) # Parameters have been offloaded, so on the meta device self.assertEqual(model.lineara.weight.device , torch.device("""meta""" ) ) self.assertEqual(model.batchnorm.weight.device , torch.device("""meta""" ) ) self.assertEqual(model.lineara.weight.device , torch.device("""meta""" ) ) # Buffers are not included in the offload by default, so are on the execution device __snake_case : Union[str, Any] = torch.device(__magic_name__ ) self.assertEqual(model.batchnorm.running_mean.device , __magic_name__ ) __snake_case : Optional[int] = torch.randn(2 , 3 ) __snake_case : Dict = model(__magic_name__ ) self.assertEqual(output.device , __magic_name__ ) # Removing hooks loads back the weights in the model. remove_hook_from_submodules(__magic_name__ ) self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.batchnorm.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) # Now test with buffers included in the offload attach_align_device_hook(__magic_name__ , execution_device=__magic_name__ , offload=__magic_name__ , offload_buffers=__magic_name__ ) # Parameters have been offloaded, so on the meta device, buffers included self.assertEqual(model.lineara.weight.device , torch.device("""meta""" ) ) self.assertEqual(model.batchnorm.weight.device , torch.device("""meta""" ) ) self.assertEqual(model.lineara.weight.device , torch.device("""meta""" ) ) self.assertEqual(model.batchnorm.running_mean.device , torch.device("""meta""" ) ) __snake_case : Dict = torch.randn(2 , 3 ) __snake_case : Optional[int] = model(__magic_name__ ) self.assertEqual(output.device , __magic_name__ ) # Removing hooks loads back the weights in the model. remove_hook_from_submodules(__magic_name__ ) self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.batchnorm.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) def lowercase__ ( self : Any ) -> Union[str, Any]: """simple docstring""" __snake_case : Any = ModelForTest() # Everything is on CPU self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.batchnorm.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) # This will move each submodule on different devices __snake_case : str = 0 if torch.cuda.is_available() else """cpu""" attach_align_device_hook( __magic_name__ , execution_device=__magic_name__ , offload=__magic_name__ , weights_map=model.state_dict() ) # Parameters have been offloaded, so on the meta device self.assertEqual(model.lineara.weight.device , torch.device("""meta""" ) ) self.assertEqual(model.batchnorm.weight.device , torch.device("""meta""" ) ) self.assertEqual(model.lineara.weight.device , torch.device("""meta""" ) ) # Buffers are not included in the offload by default, so are on the execution device __snake_case : List[str] = torch.device(__magic_name__ ) self.assertEqual(model.batchnorm.running_mean.device , __magic_name__ ) __snake_case : Tuple = torch.randn(2 , 3 ) __snake_case : Optional[Any] = model(__magic_name__ ) self.assertEqual(output.device , __magic_name__ ) # Removing hooks loads back the weights in the model. remove_hook_from_submodules(__magic_name__ ) self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.batchnorm.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) # Now test with buffers included in the offload attach_align_device_hook( __magic_name__ , execution_device=__magic_name__ , offload=__magic_name__ , weights_map=model.state_dict() , offload_buffers=__magic_name__ , ) # Parameters have been offloaded, so on the meta device, buffers included self.assertEqual(model.lineara.weight.device , torch.device("""meta""" ) ) self.assertEqual(model.batchnorm.weight.device , torch.device("""meta""" ) ) self.assertEqual(model.lineara.weight.device , torch.device("""meta""" ) ) self.assertEqual(model.batchnorm.running_mean.device , torch.device("""meta""" ) ) __snake_case : List[str] = torch.randn(2 , 3 ) __snake_case : Dict = model(__magic_name__ ) self.assertEqual(output.device , __magic_name__ ) # Removing hooks loads back the weights in the model. remove_hook_from_submodules(__magic_name__ ) self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.batchnorm.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) )
26
0
import math import sys def _lowerCamelCase ( __lowerCamelCase ) -> str: '''simple docstring''' UpperCAmelCase__ : List[str] = """""" try: with open(_lowerCamelCase , """rb""" ) as binary_file: UpperCAmelCase__ : Optional[Any] = binary_file.read() for dat in data: UpperCAmelCase__ : Union[str, Any] = F"{dat:08b}" result += curr_byte return result except OSError: print("""File not accessible""" ) sys.exit() def _lowerCamelCase ( __lowerCamelCase ) -> str: '''simple docstring''' UpperCAmelCase__ : int = {"""0""": """0""", """1""": """1"""} UpperCAmelCase__ : List[str] = """""", """""" UpperCAmelCase__ : Dict = len(_lowerCamelCase ) for i in range(len(_lowerCamelCase ) ): curr_string += data_bits[i] if curr_string not in lexicon: continue UpperCAmelCase__ : str = lexicon[curr_string] result += last_match_id UpperCAmelCase__ : List[str] = last_match_id + """0""" if math.loga(_lowerCamelCase ).is_integer(): UpperCAmelCase__ : Optional[int] = {} for curr_key in list(_lowerCamelCase ): UpperCAmelCase__ : Optional[Any] = lexicon.pop(_lowerCamelCase ) UpperCAmelCase__ : Optional[int] = new_lex UpperCAmelCase__ : List[Any] = last_match_id + """1""" index += 1 UpperCAmelCase__ : List[Any] = """""" return result def _lowerCamelCase ( __lowerCamelCase , __lowerCamelCase ) -> None: '''simple docstring''' UpperCAmelCase__ : Optional[Any] = 8 try: with open(_lowerCamelCase , """wb""" ) as opened_file: UpperCAmelCase__ : Any = [ to_write[i : i + byte_length] for i in range(0 , len(_lowerCamelCase ) , _lowerCamelCase ) ] if len(result_byte_array[-1] ) % byte_length == 0: result_byte_array.append("""10000000""" ) else: result_byte_array[-1] += "1" + "0" * ( byte_length - len(result_byte_array[-1] ) - 1 ) for elem in result_byte_array[:-1]: opened_file.write(int(_lowerCamelCase , 2 ).to_bytes(1 , byteorder="""big""" ) ) except OSError: print("""File not accessible""" ) sys.exit() def _lowerCamelCase ( __lowerCamelCase ) -> str: '''simple docstring''' UpperCAmelCase__ : List[Any] = 0 for letter in data_bits: if letter == "1": break counter += 1 UpperCAmelCase__ : Optional[int] = data_bits[counter:] UpperCAmelCase__ : int = data_bits[counter + 1 :] return data_bits def _lowerCamelCase ( __lowerCamelCase , __lowerCamelCase ) -> None: '''simple docstring''' UpperCAmelCase__ : Union[str, Any] = read_file_binary(_lowerCamelCase ) UpperCAmelCase__ : Optional[int] = remove_prefix(_lowerCamelCase ) UpperCAmelCase__ : int = decompress_data(_lowerCamelCase ) write_file_binary(_lowerCamelCase , _lowerCamelCase ) if __name__ == "__main__": compress(sys.argv[1], sys.argv[2])
79
'''simple docstring''' from __future__ import annotations __UpperCamelCase = [ [-1, 0], # left [0, -1], # down [1, 0], # right [0, 1], # up ] def _a ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , ) -> tuple[list[list[int]], list[list[int]]]: """simple docstring""" __snake_case : List[str] = [ [0 for col in range(len(grid[0] ) )] for row in range(len(_lowerCamelCase ) ) ] # the reference grid __snake_case : Tuple = 1 __snake_case : List[str] = [ [0 for col in range(len(grid[0] ) )] for row in range(len(_lowerCamelCase ) ) ] # the action grid __snake_case : List[str] = init[0] __snake_case : str = init[1] __snake_case : int = 0 __snake_case : int = g + heuristic[x][y] # cost from starting cell to destination cell __snake_case : List[str] = [[f, g, x, y]] __snake_case : Any = False # flag that is set when search is complete __snake_case : int = False # flag set if we can't find expand while not found and not resign: if len(_lowerCamelCase ) == 0: raise ValueError("""Algorithm is unable to find solution""" ) else: # to choose the least costliest action so as to move closer to the goal cell.sort() cell.reverse() __snake_case : Tuple = cell.pop() __snake_case : Optional[int] = next_cell[2] __snake_case : List[Any] = next_cell[3] __snake_case : int = next_cell[1] if x == goal[0] and y == goal[1]: __snake_case : Optional[Any] = True else: for i in range(len(_lowerCamelCase ) ): # to try out different valid actions __snake_case : Union[str, Any] = x + DIRECTIONS[i][0] __snake_case : str = y + DIRECTIONS[i][1] if xa >= 0 and xa < len(_lowerCamelCase ) and ya >= 0 and ya < len(grid[0] ): if closed[xa][ya] == 0 and grid[xa][ya] == 0: __snake_case : str = g + cost __snake_case : Tuple = ga + heuristic[xa][ya] cell.append([fa, ga, xa, ya] ) __snake_case : List[str] = 1 __snake_case : Optional[int] = i __snake_case : List[str] = [] __snake_case : Optional[int] = goal[0] __snake_case : List[Any] = goal[1] invpath.append([x, y] ) # we get the reverse path from here while x != init[0] or y != init[1]: __snake_case : Dict = x - DIRECTIONS[action[x][y]][0] __snake_case : int = y - DIRECTIONS[action[x][y]][1] __snake_case : Optional[int] = xa __snake_case : int = ya invpath.append([x, y] ) __snake_case : Optional[int] = [] for i in range(len(_lowerCamelCase ) ): path.append(invpath[len(_lowerCamelCase ) - 1 - i] ) return path, action if __name__ == "__main__": __UpperCamelCase = [ [0, 1, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles [0, 1, 0, 0, 0, 0], [0, 1, 0, 0, 1, 0], [0, 0, 0, 0, 1, 0], ] __UpperCamelCase = [0, 0] # all coordinates are given in format [y,x] __UpperCamelCase = [len(grid) - 1, len(grid[0]) - 1] __UpperCamelCase = 1 # the cost map which pushes the path closer to the goal __UpperCamelCase = [[0 for row in range(len(grid[0]))] for col in range(len(grid))] for i in range(len(grid)): for j in range(len(grid[0])): __UpperCamelCase = abs(i - goal[0]) + abs(j - goal[1]) if grid[i][j] == 1: # added extra penalty in the heuristic map __UpperCamelCase = 99 __UpperCamelCase , __UpperCamelCase = search(grid, init, goal, cost, heuristic) print("ACTION MAP") for i in range(len(action)): print(action[i]) for i in range(len(path)): print(path[i])
26
0
def __a ( __UpperCAmelCase : List[str] , __UpperCAmelCase : Union[str, Any] , __UpperCAmelCase : str ) -> float: """simple docstring""" return round(float(moles / volume ) * nfactor ) def __a ( __UpperCAmelCase : Tuple , __UpperCAmelCase : List[str] , __UpperCAmelCase : List[str] ) -> float: """simple docstring""" return round(float((moles * 0.0_8_2_1 * temperature) / (volume) ) ) def __a ( __UpperCAmelCase : Dict , __UpperCAmelCase : str , __UpperCAmelCase : List[Any] ) -> float: """simple docstring""" return round(float((moles * 0.0_8_2_1 * temperature) / (pressure) ) ) def __a ( __UpperCAmelCase : Optional[int] , __UpperCAmelCase : Tuple , __UpperCAmelCase : Optional[Any] ) -> float: """simple docstring""" return round(float((pressure * volume) / (0.0_8_2_1 * moles) ) ) if __name__ == "__main__": import doctest doctest.testmod()
488
'''simple docstring''' def _a ( _lowerCamelCase ) -> int: """simple docstring""" if not isinstance(_lowerCamelCase , _lowerCamelCase ): raise TypeError("""only integers accepted as input""" ) else: __snake_case : List[Any] = str(abs(_lowerCamelCase ) ) __snake_case : Union[str, Any] = [list(_lowerCamelCase ) for char in range(len(_lowerCamelCase ) )] for index in range(len(_lowerCamelCase ) ): num_transpositions[index].pop(_lowerCamelCase ) return max( int("""""".join(list(_lowerCamelCase ) ) ) for transposition in num_transpositions ) if __name__ == "__main__": __import__("doctest").testmod()
26
0
from collections.abc import Iterable from typing import Any class lowerCamelCase_ : def __init__( self : Optional[int] , __A : int | None = None ): __A : str = value __A : Node | None = None # Added in order to delete a node easier __A : Node | None = None __A : Node | None = None def __repr__( self : Any ): from pprint import pformat if self.left is None and self.right is None: return str(self.value ) return pformat({F"""{self.value}""": (self.left, self.right)} , indent=1 ) class lowerCamelCase_ : def __init__( self : int , __A : Node | None = None ): __A : Tuple = root def __str__( self : List[str] ): return str(self.root ) def lowerCAmelCase_ ( self : Any , __A : Node , __A : Node | None ): if new_children is not None: # reset its kids __A : Dict = node.parent if node.parent is not None: # reset its parent if self.is_right(__A ): # If it is the right children __A : Any = new_children else: __A : Optional[Any] = new_children else: __A : Dict = new_children def lowerCAmelCase_ ( self : Optional[Any] , __A : Node ): if node.parent and node.parent.right: return node == node.parent.right return False def lowerCAmelCase_ ( self : Optional[int] ): return self.root is None def lowerCAmelCase_ ( self : Dict , __A : Optional[int] ): __A : Any = Node(__A ) # create a new Node if self.empty(): # if Tree is empty __A : List[Any] = new_node # set its root else: # Tree is not empty __A : Union[str, Any] = self.root # from root if parent_node is None: return while True: # While we don't get to a leaf if value < parent_node.value: # We go left if parent_node.left is None: __A : Optional[int] = new_node # We insert the new node in a leaf break else: __A : Optional[int] = parent_node.left else: if parent_node.right is None: __A : Union[str, Any] = new_node break else: __A : str = parent_node.right __A : List[Any] = parent_node def lowerCAmelCase_ ( self : Dict , *__A : Union[str, Any] ): for value in values: self.__insert(__A ) def lowerCAmelCase_ ( self : int , __A : Tuple ): if self.empty(): raise IndexError("""Warning: Tree is empty! please use another.""" ) else: __A : str = self.root # use lazy evaluation here to avoid NoneType Attribute error while node is not None and node.value is not value: __A : Dict = node.left if value < node.value else node.right return node def lowerCAmelCase_ ( self : Union[str, Any] , __A : Node | None = None ): if node is None: if self.root is None: return None __A : List[str] = self.root if not self.empty(): while node.right is not None: __A : Optional[Any] = node.right return node def lowerCAmelCase_ ( self : Optional[int] , __A : Node | None = None ): if node is None: __A : List[str] = self.root if self.root is None: return None if not self.empty(): __A : Dict = self.root while node.left is not None: __A : Optional[Any] = node.left return node def lowerCAmelCase_ ( self : str , __A : int ): __A : Dict = self.search(__A ) # Look for the node with that label if node is not None: if node.left is None and node.right is None: # If it has no children self.__reassign_nodes(__A , __A ) elif node.left is None: # Has only right children self.__reassign_nodes(__A , node.right ) elif node.right is None: # Has only left children self.__reassign_nodes(__A , node.left ) else: __A : Tuple = self.get_max( node.left ) # Gets the max value of the left branch self.remove(tmp_node.value ) # type: ignore __A : Any = ( tmp_node.value # type: ignore ) # Assigns the value to the node to delete and keep tree structure def lowerCAmelCase_ ( self : List[Any] , __A : Node | None ): if node is not None: yield node # Preorder Traversal yield from self.preorder_traverse(node.left ) yield from self.preorder_traverse(node.right ) def lowerCAmelCase_ ( self : int , __A : Optional[int]=None ): if traversal_function is None: return self.preorder_traverse(self.root ) else: return traversal_function(self.root ) def lowerCAmelCase_ ( self : str , __A : list , __A : Node | None ): if node: self.inorder(__A , node.left ) arr.append(node.value ) self.inorder(__A , node.right ) def lowerCAmelCase_ ( self : int , __A : int , __A : Node ): __A : list[int] = [] self.inorder(__A , __A ) # append all values to list using inorder traversal return arr[k - 1] def __SCREAMING_SNAKE_CASE ( a__ : List[Any] ) -> list[Node]: __A : int = [] if curr_node is not None: __A : List[Any] = postorder(curr_node.left ) + postorder(curr_node.right ) + [curr_node] return node_list def __SCREAMING_SNAKE_CASE ( ) -> None: __A : Optional[Any] = (8, 3, 6, 1, 10, 14, 13, 4, 7) __A : str = BinarySearchTree() for i in testlist: t.insert(_lowerCamelCase ) # Prints all the elements of the list in order traversal print(_lowerCamelCase ) if t.search(6 ) is not None: print("""The value 6 exists""" ) else: print("""The value 6 doesn't exist""" ) if t.search(-1 ) is not None: print("""The value -1 exists""" ) else: print("""The value -1 doesn't exist""" ) if not t.empty(): print("""Max Value: """ ,t.get_max().value ) # type: ignore print("""Min Value: """ ,t.get_min().value ) # type: ignore for i in testlist: t.remove(_lowerCamelCase ) print(_lowerCamelCase ) if __name__ == "__main__": import doctest doctest.testmod(verbose=True)
17
'''simple docstring''' from __future__ import annotations import math def _a ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) -> int: """simple docstring""" if depth < 0: raise ValueError("""Depth cannot be less than 0""" ) if not scores: raise ValueError("""Scores cannot be empty""" ) if depth == height: return scores[node_index] return ( max( minimax(depth + 1 , node_index * 2 , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) , minimax(depth + 1 , node_index * 2 + 1 , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) , ) if is_max else min( minimax(depth + 1 , node_index * 2 , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) , minimax(depth + 1 , node_index * 2 + 1 , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) , ) ) def _a ( ) -> None: """simple docstring""" __snake_case : Union[str, Any] = [90, 23, 6, 33, 21, 65, 123, 3_4423] __snake_case : Optional[int] = math.log(len(_lowerCamelCase ) , 2 ) print(F'''Optimal value : {minimax(0 , 0 , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase )}''' ) if __name__ == "__main__": import doctest doctest.testmod() main()
26
0
"""simple docstring""" from __future__ import annotations from fractions import Fraction def __magic_name__ ( __snake_case : List[Any] , __snake_case : Tuple ) -> bool: return ( num != den and num % 10 == den // 10 and (num // 10) / (den % 10) == num / den ) def __magic_name__ ( __snake_case : Optional[int] ) -> list[str]: lowercase : Union[str, Any] = [] lowercase : Dict = 11 lowercase : List[Any] = int("1" + "0" * digit_len ) for num in range(_lowerCamelCase , _lowerCamelCase ): while den <= 99: if (num != den) and (num % 10 == den // 10) and (den % 10 != 0): if is_digit_cancelling(_lowerCamelCase , _lowerCamelCase ): solutions.append(f"""{num}/{den}""" ) den += 1 num += 1 lowercase : str = 10 return solutions def __magic_name__ ( __snake_case : str = 2 ) -> int: lowercase : List[Any] = 1.0 for fraction in fraction_list(_lowerCamelCase ): lowercase : List[Any] = Fraction(_lowerCamelCase ) result *= frac.denominator / frac.numerator return int(_lowerCamelCase ) if __name__ == "__main__": print(solution())
361
'''simple docstring''' from __future__ import annotations def _a ( _lowerCamelCase , _lowerCamelCase = None , _lowerCamelCase = None ) -> None: """simple docstring""" if start is None: __snake_case : Optional[Any] = 0 if end is None: __snake_case : Optional[Any] = len(_lowerCamelCase ) - 1 if start >= end: return __snake_case : Tuple = (start + end) // 2 slowsort(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) slowsort(_lowerCamelCase , mid + 1 , _lowerCamelCase ) if sequence[end] < sequence[mid]: __snake_case , __snake_case : str = sequence[mid], sequence[end] slowsort(_lowerCamelCase , _lowerCamelCase , end - 1 ) if __name__ == "__main__": from doctest import testmod testmod()
26
0
import copy import inspect import unittest from transformers import AutoBackbone from transformers.configuration_utils import PretrainedConfig from transformers.testing_utils import require_timm, require_torch, torch_device from transformers.utils.import_utils import is_torch_available from ...test_backbone_common import BackboneTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor if is_torch_available(): import torch from transformers import TimmBackbone, TimmBackboneConfig from ...test_pipeline_mixin import PipelineTesterMixin class A: '''simple docstring''' def __init__( self : Tuple , A_ : Optional[int] , A_ : Optional[Any]=None , A_ : Dict=None , A_ : List[str]=None , A_ : int="resnet50" , A_ : Dict=3 , A_ : Union[str, Any]=32 , A_ : Union[str, Any]=3 , A_ : Dict=True , A_ : List[str]=True , ) -> Tuple: """simple docstring""" lowerCamelCase_ = parent lowerCamelCase_ = out_indices if out_indices is not None else [4] lowerCamelCase_ = stage_names lowerCamelCase_ = out_features lowerCamelCase_ = backbone lowerCamelCase_ = batch_size lowerCamelCase_ = image_size lowerCamelCase_ = num_channels lowerCamelCase_ = use_pretrained_backbone lowerCamelCase_ = is_training def a__ ( self : Tuple ) -> str: """simple docstring""" lowerCamelCase_ = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) lowerCamelCase_ = self.get_config() return config, pixel_values def a__ ( self : Union[str, Any] ) -> Any: """simple docstring""" return TimmBackboneConfig( image_size=self.image_size , num_channels=self.num_channels , out_features=self.out_features , out_indices=self.out_indices , stage_names=self.stage_names , use_pretrained_backbone=self.use_pretrained_backbone , backbone=self.backbone , ) def a__ ( self : Dict , A_ : List[Any] , A_ : List[Any] ) -> Optional[Any]: """simple docstring""" lowerCamelCase_ = TimmBackbone(config=A_ ) model.to(A_ ) model.eval() with torch.no_grad(): lowerCamelCase_ = model(A_ ) self.parent.assertEqual( result.feature_map[-1].shape , (self.batch_size, model.channels[-1], 14, 14) , ) def a__ ( self : str ) -> Optional[int]: """simple docstring""" lowerCamelCase_ = self.prepare_config_and_inputs() lowerCamelCase_ = config_and_inputs lowerCamelCase_ = {"""pixel_values""": pixel_values} return config, inputs_dict @require_torch @require_timm class A( __lowercase , __lowercase , __lowercase , unittest.TestCase ): '''simple docstring''' UpperCamelCase = (TimmBackbone,) if is_torch_available() else () UpperCamelCase = {'''feature-extraction''': TimmBackbone} if is_torch_available() else {} UpperCamelCase = False UpperCamelCase = False UpperCamelCase = False UpperCamelCase = False def a__ ( self : Dict ) -> Any: """simple docstring""" lowerCamelCase_ = TimmBackboneModelTester(self ) lowerCamelCase_ = ConfigTester(self , config_class=A_ , has_text_modality=A_ ) def a__ ( self : str ) -> str: """simple docstring""" self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() def a__ ( self : Optional[int] ) -> Dict: """simple docstring""" lowerCamelCase_ = """resnet18""" lowerCamelCase_ = """microsoft/resnet-18""" lowerCamelCase_ = AutoBackbone.from_pretrained(A_ , use_timm_backbone=A_ ) lowerCamelCase_ = AutoBackbone.from_pretrained(A_ ) self.assertEqual(len(timm_model.out_features ) , len(transformers_model.out_features ) ) self.assertEqual(len(timm_model.stage_names ) , len(transformers_model.stage_names ) ) self.assertEqual(timm_model.channels , transformers_model.channels ) # Out indices are set to the last layer by default. For timm models, we don't know # the number of layers in advance, so we set it to (-1,), whereas for transformers # models, we set it to [len(stage_names) - 1] (kept for backward compatibility). self.assertEqual(timm_model.out_indices , (-1,) ) self.assertEqual(transformers_model.out_indices , [len(timm_model.stage_names ) - 1] ) lowerCamelCase_ = AutoBackbone.from_pretrained(A_ , use_timm_backbone=A_ , out_indices=[1, 2, 3] ) lowerCamelCase_ = AutoBackbone.from_pretrained(A_ , out_indices=[1, 2, 3] ) self.assertEqual(timm_model.out_indices , transformers_model.out_indices ) self.assertEqual(len(timm_model.out_features ) , len(transformers_model.out_features ) ) self.assertEqual(timm_model.channels , transformers_model.channels ) @unittest.skip('TimmBackbone doesn\'t support feed forward chunking' ) def a__ ( self : Dict ) -> Dict: """simple docstring""" pass @unittest.skip('TimmBackbone doesn\'t have num_hidden_layers attribute' ) def a__ ( self : str ) -> List[str]: """simple docstring""" pass @unittest.skip('TimmBackbone initialization is managed on the timm side' ) def a__ ( self : Any ) -> Optional[int]: """simple docstring""" pass @unittest.skip('TimmBackbone models doesn\'t have inputs_embeds' ) def a__ ( self : List[Any] ) -> Any: """simple docstring""" pass @unittest.skip('TimmBackbone models doesn\'t have inputs_embeds' ) def a__ ( self : Dict ) -> int: """simple docstring""" pass @unittest.skip('TimmBackbone model cannot be created without specifying a backbone checkpoint' ) def a__ ( self : Dict ) -> Optional[Any]: """simple docstring""" pass @unittest.skip('Only checkpoints on timm can be loaded into TimmBackbone' ) def a__ ( self : Tuple ) -> List[str]: """simple docstring""" pass @unittest.skip('model weights aren\'t tied in TimmBackbone.' ) def a__ ( self : str ) -> Any: """simple docstring""" pass @unittest.skip('model weights aren\'t tied in TimmBackbone.' ) def a__ ( self : Dict ) -> int: """simple docstring""" pass @unittest.skip('Only checkpoints on timm can be loaded into TimmBackbone' ) def a__ ( self : Dict ) -> List[Any]: """simple docstring""" pass @unittest.skip('Only checkpoints on timm can be loaded into TimmBackbone' ) def a__ ( self : Any ) -> List[str]: """simple docstring""" pass @unittest.skip('TimmBackbone doesn\'t have hidden size info in its configuration.' ) def a__ ( self : Optional[Any] ) -> Tuple: """simple docstring""" pass @unittest.skip('TimmBackbone doesn\'t support output_attentions.' ) def a__ ( self : List[str] ) -> List[Any]: """simple docstring""" pass @unittest.skip('Safetensors is not supported by timm.' ) def a__ ( self : Optional[Any] ) -> Tuple: """simple docstring""" pass @unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.' ) def a__ ( self : int ) -> Dict: """simple docstring""" pass def a__ ( self : Optional[Any] ) -> Dict: """simple docstring""" lowerCamelCase_ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCamelCase_ = model_class(A_ ) lowerCamelCase_ = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic lowerCamelCase_ = [*signature.parameters.keys()] lowerCamelCase_ = ["""pixel_values"""] self.assertListEqual(arg_names[:1] , A_ ) def a__ ( self : Optional[int] ) -> Union[str, Any]: """simple docstring""" lowerCamelCase_ = self.model_tester.prepare_config_and_inputs_for_common() lowerCamelCase_ = True lowerCamelCase_ = self.has_attentions # no need to test all models as different heads yield the same functionality lowerCamelCase_ = self.all_model_classes[0] lowerCamelCase_ = model_class(A_ ) model.to(A_ ) lowerCamelCase_ = self._prepare_for_class(A_ , A_ ) lowerCamelCase_ = model(**A_ ) lowerCamelCase_ = outputs[0][-1] # Encoder-/Decoder-only models lowerCamelCase_ = outputs.hidden_states[0] hidden_states.retain_grad() if self.has_attentions: lowerCamelCase_ = outputs.attentions[0] attentions.retain_grad() output.flatten()[0].backward(retain_graph=A_ ) self.assertIsNotNone(hidden_states.grad ) if self.has_attentions: self.assertIsNotNone(attentions.grad ) def a__ ( self : List[str] ) -> Optional[int]: """simple docstring""" lowerCamelCase_ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCamelCase_ = model_class(A_ ) model.to(A_ ) model.eval() lowerCamelCase_ = model(**A_ ) self.assertEqual(len(result.feature_maps ) , len(config.out_indices ) ) self.assertEqual(len(model.channels ) , len(config.out_indices ) ) # Check output of last stage is taken if out_features=None, out_indices=None lowerCamelCase_ = copy.deepcopy(A_ ) lowerCamelCase_ = None lowerCamelCase_ = model_class(A_ ) model.to(A_ ) model.eval() lowerCamelCase_ = model(**A_ ) self.assertEqual(len(result.feature_maps ) , 1 ) self.assertEqual(len(model.channels ) , 1 ) # Check backbone can be initialized with fresh weights lowerCamelCase_ = copy.deepcopy(A_ ) lowerCamelCase_ = False lowerCamelCase_ = model_class(A_ ) model.to(A_ ) model.eval() lowerCamelCase_ = model(**A_ )
70
'''simple docstring''' import doctest import logging import os import unittest from pathlib import Path from typing import List, Union import transformers from transformers.testing_utils import require_tf, require_torch, slow __UpperCamelCase = logging.getLogger() @unittest.skip('''Temporarily disable the doc tests.''' ) @require_torch @require_tf @slow class _A ( unittest.TestCase ): def lowercase__ ( self : Optional[int] , __magic_name__ : Path , __magic_name__ : Union[str, None] = None , __magic_name__ : Union[List[str], None] = None , __magic_name__ : Union[str, List[str], None] = None , __magic_name__ : bool = True , ) -> Optional[int]: """simple docstring""" __snake_case : Union[str, Any] = [file for file in os.listdir(__magic_name__ ) if os.path.isfile(os.path.join(__magic_name__ , __magic_name__ ) )] if identifier is not None: __snake_case : List[Any] = [file for file in files if identifier in file] if n_identifier is not None: if isinstance(__magic_name__ , __magic_name__ ): for n_ in n_identifier: __snake_case : Optional[int] = [file for file in files if n_ not in file] else: __snake_case : Tuple = [file for file in files if n_identifier not in file] __snake_case : Dict = ignore_files or [] ignore_files.append("""__init__.py""" ) __snake_case : List[str] = [file for file in files if file not in ignore_files] for file in files: # Open all files print("""Testing""" , __magic_name__ ) if only_modules: __snake_case : List[Any] = file.split(""".""" )[0] try: __snake_case : List[Any] = getattr(__magic_name__ , __magic_name__ ) __snake_case : Union[str, Any] = doctest.DocTestSuite(__magic_name__ ) __snake_case : Dict = unittest.TextTestRunner().run(__magic_name__ ) self.assertIs(len(result.failures ) , 0 ) except AttributeError: logger.info(f'''{module_identifier} is not a module.''' ) else: __snake_case : Tuple = doctest.testfile(str("""..""" / directory / file ) , optionflags=doctest.ELLIPSIS ) self.assertIs(result.failed , 0 ) def lowercase__ ( self : Union[str, Any] ) -> Any: """simple docstring""" __snake_case : List[Any] = Path("""src/transformers""" ) __snake_case : List[Any] = """modeling""" __snake_case : Union[str, Any] = [ """modeling_ctrl.py""", """modeling_tf_ctrl.py""", ] self.analyze_directory(__magic_name__ , identifier=__magic_name__ , ignore_files=__magic_name__ ) def lowercase__ ( self : Union[str, Any] ) -> int: """simple docstring""" __snake_case : Union[str, Any] = Path("""src/transformers""" ) __snake_case : Any = """tokenization""" self.analyze_directory(__magic_name__ , identifier=__magic_name__ ) def lowercase__ ( self : Union[str, Any] ) -> int: """simple docstring""" __snake_case : List[Any] = Path("""src/transformers""" ) __snake_case : List[str] = """configuration""" self.analyze_directory(__magic_name__ , identifier=__magic_name__ ) def lowercase__ ( self : Dict ) -> Dict: """simple docstring""" __snake_case : Tuple = Path("""src/transformers""" ) __snake_case : int = ["""configuration""", """modeling""", """tokenization"""] self.analyze_directory(__magic_name__ , n_identifier=__magic_name__ ) def lowercase__ ( self : Optional[int] ) -> Union[str, Any]: """simple docstring""" __snake_case : int = Path("""docs/source""" ) __snake_case : Optional[int] = ["""favicon.ico"""] self.analyze_directory(__magic_name__ , ignore_files=__magic_name__ , only_modules=__magic_name__ )
26
0
"""simple docstring""" from typing import List from .keymap import KEYMAP, get_character def _A (__a ) -> int: """simple docstring""" def decorator(__a ): SCREAMING_SNAKE_CASE_ : str = getattr(_lowerCamelCase , '''handle_key''' , [] ) handle += [key] setattr(_lowerCamelCase , '''handle_key''' , _lowerCamelCase ) return func return decorator def _A (*__a ) -> str: """simple docstring""" def decorator(__a ): SCREAMING_SNAKE_CASE_ : List[Any] = getattr(_lowerCamelCase , '''handle_key''' , [] ) handle += keys setattr(_lowerCamelCase , '''handle_key''' , _lowerCamelCase ) return func return decorator class lowerCAmelCase__ ( __lowercase ): '''simple docstring''' def __new__( cls : List[Any] , lowercase_ : Optional[Any] , lowercase_ : Optional[Any] , lowercase_ : List[Any]): '''simple docstring''' SCREAMING_SNAKE_CASE_ : str = super().__new__(cls , lowercase_ , lowercase_ , lowercase_) if not hasattr(lowercase_ , '''key_handler'''): setattr(lowercase_ , '''key_handler''' , {}) setattr(lowercase_ , '''handle_input''' , KeyHandler.handle_input) for value in attrs.values(): SCREAMING_SNAKE_CASE_ : Optional[int] = getattr(lowercase_ , '''handle_key''' , []) for key in handled_keys: SCREAMING_SNAKE_CASE_ : int = value return new_cls @staticmethod def _SCREAMING_SNAKE_CASE ( cls : Any): '''simple docstring''' SCREAMING_SNAKE_CASE_ : Optional[Any] = get_character() if char != KEYMAP["undefined"]: SCREAMING_SNAKE_CASE_ : Tuple = ord(lowercase_) SCREAMING_SNAKE_CASE_ : List[Any] = cls.key_handler.get(lowercase_) if handler: SCREAMING_SNAKE_CASE_ : Any = char return handler(cls) else: return None def _A (cls ) -> str: """simple docstring""" return KeyHandler(cls.__name__ , cls.__bases__ , cls.__dict__.copy() )
512
'''simple docstring''' import inspect from typing import Callable, List, Optional, Union import torch from transformers import ( CLIPImageProcessor, CLIPTextModel, CLIPTokenizer, WhisperForConditionalGeneration, WhisperProcessor, ) from diffusers import ( AutoencoderKL, DDIMScheduler, DiffusionPipeline, LMSDiscreteScheduler, PNDMScheduler, UNetaDConditionModel, ) from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion import StableDiffusionPipelineOutput from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker from diffusers.utils import logging __UpperCamelCase = logging.get_logger(__name__) # pylint: disable=invalid-name class _A ( __lowercase ): def __init__( self : str , __magic_name__ : WhisperForConditionalGeneration , __magic_name__ : WhisperProcessor , __magic_name__ : AutoencoderKL , __magic_name__ : CLIPTextModel , __magic_name__ : CLIPTokenizer , __magic_name__ : UNetaDConditionModel , __magic_name__ : Union[DDIMScheduler, PNDMScheduler, LMSDiscreteScheduler] , __magic_name__ : StableDiffusionSafetyChecker , __magic_name__ : CLIPImageProcessor , ) -> Union[str, Any]: """simple docstring""" super().__init__() if safety_checker is None: logger.warning( f'''You have disabled the safety checker for {self.__class__} by passing `safety_checker=None`. Ensure''' """ that you abide to the conditions of the Stable Diffusion license and do not expose unfiltered""" """ results in services or applications open to the public. Both the diffusers team and Hugging Face""" """ strongly recommend to keep the safety filter enabled in all public facing circumstances, disabling""" """ it only for use-cases that involve analyzing network behavior or auditing its results. For more""" """ information, please have a look at https://github.com/huggingface/diffusers/pull/254 .""" ) self.register_modules( speech_model=__magic_name__ , speech_processor=__magic_name__ , vae=__magic_name__ , text_encoder=__magic_name__ , tokenizer=__magic_name__ , unet=__magic_name__ , scheduler=__magic_name__ , feature_extractor=__magic_name__ , ) def lowercase__ ( self : Optional[Any] , __magic_name__ : Optional[Union[str, int]] = "auto" ) -> Union[str, Any]: """simple docstring""" if slice_size == "auto": __snake_case : str = self.unet.config.attention_head_dim // 2 self.unet.set_attention_slice(__magic_name__ ) def lowercase__ ( self : str ) -> Any: """simple docstring""" self.enable_attention_slicing(__magic_name__ ) @torch.no_grad() def __call__( self : Optional[int] , __magic_name__ : str , __magic_name__ : Dict=1_60_00 , __magic_name__ : int = 5_12 , __magic_name__ : int = 5_12 , __magic_name__ : int = 50 , __magic_name__ : float = 7.5 , __magic_name__ : Optional[Union[str, List[str]]] = None , __magic_name__ : Optional[int] = 1 , __magic_name__ : float = 0.0 , __magic_name__ : Optional[torch.Generator] = None , __magic_name__ : Optional[torch.FloatTensor] = None , __magic_name__ : Optional[str] = "pil" , __magic_name__ : bool = True , __magic_name__ : Optional[Callable[[int, int, torch.FloatTensor], None]] = None , __magic_name__ : int = 1 , **__magic_name__ : List[str] , ) -> int: """simple docstring""" __snake_case : List[Any] = self.speech_processor.feature_extractor( __magic_name__ , return_tensors="""pt""" , sampling_rate=__magic_name__ ).input_features.to(self.device ) __snake_case : List[str] = self.speech_model.generate(__magic_name__ , max_length=48_00_00 ) __snake_case : List[Any] = self.speech_processor.tokenizer.batch_decode(__magic_name__ , skip_special_tokens=__magic_name__ , normalize=__magic_name__ )[ 0 ] if isinstance(__magic_name__ , __magic_name__ ): __snake_case : Tuple = 1 elif isinstance(__magic_name__ , __magic_name__ ): __snake_case : Optional[int] = len(__magic_name__ ) else: raise ValueError(f'''`prompt` has to be of type `str` or `list` but is {type(__magic_name__ )}''' ) if height % 8 != 0 or width % 8 != 0: raise ValueError(f'''`height` and `width` have to be divisible by 8 but are {height} and {width}.''' ) if (callback_steps is None) or ( callback_steps is not None and (not isinstance(__magic_name__ , __magic_name__ ) or callback_steps <= 0) ): raise ValueError( f'''`callback_steps` has to be a positive integer but is {callback_steps} of type''' f''' {type(__magic_name__ )}.''' ) # get prompt text embeddings __snake_case : Dict = self.tokenizer( __magic_name__ , padding="""max_length""" , max_length=self.tokenizer.model_max_length , return_tensors="""pt""" , ) __snake_case : Optional[Any] = text_inputs.input_ids if text_input_ids.shape[-1] > self.tokenizer.model_max_length: __snake_case : Tuple = self.tokenizer.batch_decode(text_input_ids[:, self.tokenizer.model_max_length :] ) logger.warning( """The following part of your input was truncated because CLIP can only handle sequences up to""" f''' {self.tokenizer.model_max_length} tokens: {removed_text}''' ) __snake_case : Any = text_input_ids[:, : self.tokenizer.model_max_length] __snake_case : int = self.text_encoder(text_input_ids.to(self.device ) )[0] # duplicate text embeddings for each generation per prompt, using mps friendly method __snake_case , __snake_case , __snake_case : Any = text_embeddings.shape __snake_case : List[Any] = text_embeddings.repeat(1 , __magic_name__ , 1 ) __snake_case : Dict = text_embeddings.view(bs_embed * num_images_per_prompt , __magic_name__ , -1 ) # here `guidance_scale` is defined analog to the guidance weight `w` of equation (2) # of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1` # corresponds to doing no classifier free guidance. __snake_case : Optional[int] = guidance_scale > 1.0 # get unconditional embeddings for classifier free guidance if do_classifier_free_guidance: __snake_case : List[str] if negative_prompt is None: __snake_case : Optional[Any] = [""""""] * batch_size elif type(__magic_name__ ) is not type(__magic_name__ ): raise TypeError( f'''`negative_prompt` should be the same type to `prompt`, but got {type(__magic_name__ )} !=''' f''' {type(__magic_name__ )}.''' ) elif isinstance(__magic_name__ , __magic_name__ ): __snake_case : Dict = [negative_prompt] elif batch_size != len(__magic_name__ ): raise ValueError( f'''`negative_prompt`: {negative_prompt} has batch size {len(__magic_name__ )}, but `prompt`:''' f''' {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches''' """ the batch size of `prompt`.""" ) else: __snake_case : int = negative_prompt __snake_case : List[str] = text_input_ids.shape[-1] __snake_case : Any = self.tokenizer( __magic_name__ , padding="""max_length""" , max_length=__magic_name__ , truncation=__magic_name__ , return_tensors="""pt""" , ) __snake_case : Dict = self.text_encoder(uncond_input.input_ids.to(self.device ) )[0] # duplicate unconditional embeddings for each generation per prompt, using mps friendly method __snake_case : Optional[int] = uncond_embeddings.shape[1] __snake_case : Union[str, Any] = uncond_embeddings.repeat(1 , __magic_name__ , 1 ) __snake_case : Tuple = uncond_embeddings.view(batch_size * num_images_per_prompt , __magic_name__ , -1 ) # For classifier free guidance, we need to do two forward passes. # Here we concatenate the unconditional and text embeddings into a single batch # to avoid doing two forward passes __snake_case : Dict = torch.cat([uncond_embeddings, text_embeddings] ) # get the initial random noise unless the user supplied it # Unlike in other pipelines, latents need to be generated in the target device # for 1-to-1 results reproducibility with the CompVis implementation. # However this currently doesn't work in `mps`. __snake_case : List[Any] = (batch_size * num_images_per_prompt, self.unet.config.in_channels, height // 8, width // 8) __snake_case : List[Any] = text_embeddings.dtype if latents is None: if self.device.type == "mps": # randn does not exist on mps __snake_case : Optional[int] = torch.randn(__magic_name__ , generator=__magic_name__ , device="""cpu""" , dtype=__magic_name__ ).to( self.device ) else: __snake_case : int = torch.randn(__magic_name__ , generator=__magic_name__ , device=self.device , dtype=__magic_name__ ) else: if latents.shape != latents_shape: raise ValueError(f'''Unexpected latents shape, got {latents.shape}, expected {latents_shape}''' ) __snake_case : List[str] = latents.to(self.device ) # set timesteps self.scheduler.set_timesteps(__magic_name__ ) # Some schedulers like PNDM have timesteps as arrays # It's more optimized to move all timesteps to correct device beforehand __snake_case : Optional[int] = self.scheduler.timesteps.to(self.device ) # scale the initial noise by the standard deviation required by the scheduler __snake_case : str = latents * self.scheduler.init_noise_sigma # prepare extra kwargs for the scheduler step, since not all schedulers have the same signature # eta (η) is only used with the DDIMScheduler, it will be ignored for other schedulers. # eta corresponds to η in DDIM paper: https://arxiv.org/abs/2010.02502 # and should be between [0, 1] __snake_case : Tuple = """eta""" in set(inspect.signature(self.scheduler.step ).parameters.keys() ) __snake_case : List[str] = {} if accepts_eta: __snake_case : str = eta for i, t in enumerate(self.progress_bar(__magic_name__ ) ): # expand the latents if we are doing classifier free guidance __snake_case : Union[str, Any] = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents __snake_case : Dict = self.scheduler.scale_model_input(__magic_name__ , __magic_name__ ) # predict the noise residual __snake_case : Tuple = self.unet(__magic_name__ , __magic_name__ , encoder_hidden_states=__magic_name__ ).sample # perform guidance if do_classifier_free_guidance: __snake_case , __snake_case : str = noise_pred.chunk(2 ) __snake_case : Any = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) # compute the previous noisy sample x_t -> x_t-1 __snake_case : Optional[Any] = self.scheduler.step(__magic_name__ , __magic_name__ , __magic_name__ , **__magic_name__ ).prev_sample # call the callback, if provided if callback is not None and i % callback_steps == 0: callback(__magic_name__ , __magic_name__ , __magic_name__ ) __snake_case : int = 1 / 0.18215 * latents __snake_case : Optional[Any] = self.vae.decode(__magic_name__ ).sample __snake_case : Any = (image / 2 + 0.5).clamp(0 , 1 ) # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16 __snake_case : Any = image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() if output_type == "pil": __snake_case : Tuple = self.numpy_to_pil(__magic_name__ ) if not return_dict: return image return StableDiffusionPipelineOutput(images=__magic_name__ , nsfw_content_detected=__magic_name__ )
26
0
import unittest from accelerate import debug_launcher from accelerate.test_utils import require_cpu, test_ops, test_script @require_cpu class lowercase__ ( unittest.TestCase ): def _UpperCAmelCase ( self : Tuple ): """simple docstring""" debug_launcher(test_script.main ) def _UpperCAmelCase ( self : Optional[int] ): """simple docstring""" debug_launcher(test_ops.main )
475
'''simple docstring''' import os from huggingface_hub.constants import HUGGINGFACE_HUB_CACHE, hf_cache_home __UpperCamelCase = HUGGINGFACE_HUB_CACHE __UpperCamelCase = "config.json" __UpperCamelCase = "diffusion_pytorch_model.bin" __UpperCamelCase = "diffusion_flax_model.msgpack" __UpperCamelCase = "model.onnx" __UpperCamelCase = "diffusion_pytorch_model.safetensors" __UpperCamelCase = "weights.pb" __UpperCamelCase = "https://huggingface.co" __UpperCamelCase = default_cache_path __UpperCamelCase = "diffusers_modules" __UpperCamelCase = os.getenv("HF_MODULES_CACHE", os.path.join(hf_cache_home, "modules")) __UpperCamelCase = ["fp16", "non-ema"] __UpperCamelCase = ".self_attn"
26
0
from __future__ import annotations import os import tempfile import unittest import numpy as np from huggingface_hub import hf_hub_download from transformers import is_tensorflow_text_available, is_tf_available from transformers.testing_utils import require_tensorflow_text, require_tf, slow from ..test_modeling_tf_common import floats_tensor from .test_framework_agnostic import GenerationIntegrationTestsMixin if is_tf_available(): import tensorflow as tf from transformers import ( AutoTokenizer, TFAutoModelForCausalLM, TFAutoModelForSeqaSeqLM, TFAutoModelForSpeechSeqaSeq, TFAutoModelForVisionaSeq, TFBartForConditionalGeneration, TFLogitsProcessorList, TFMinLengthLogitsProcessor, tf_top_k_top_p_filtering, ) if is_tensorflow_text_available(): import tensorflow_text as text @require_tf class UpperCamelCase( unittest.TestCase ): def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] ) -> List[str]: '''simple docstring''' __snake_case = tf.convert_to_tensor( [ [ 8.2220991, # 3rd highest value; idx. 0 -0.5620044, 5.23229752, 4.0386393, -6.8798378, -0.54785802, -3.2012153, 2.92777176, 1.88171953, 7.35341276, # 5th highest value; idx. 9 8.43207833, # 2nd highest value; idx. 10 -9.85711836, -5.96209236, -1.13039161, -7.1115294, -0.8369633, -5.3186408, 7.06427407, 0.81369344, -0.82023817, -5.9179796, 0.58813443, -6.99778438, 4.71551189, -0.18771637, 7.44020759, # 4th highest value; idx. 25 9.38450987, # 1st highest value; idx. 26 2.12662941, -9.32562038, 2.35652522, ], # cummulative prob of 5 highest values <= 0.6 [ 0.58425518, 4.53139238, -5.57510464, -6.28030699, -7.19529503, -4.02122551, 1.39337037, -6.06707057, 1.59480517, -9.643119, 0.03907799, 0.67231762, -8.88206726, 6.27115922, # 4th highest value; idx. 13 2.28520723, 4.82767506, 4.30421368, 8.8275313, # 2nd highest value; idx. 17 5.44029958, # 5th highest value; idx. 18 -4.4735794, 7.38579536, # 3rd highest value; idx. 20 -2.91051663, 2.61946077, -2.5674762, -9.48959302, -4.02922645, -1.35416918, 9.67702323, # 1st highest value; idx. 27 -5.89478553, 1.85370467, ], # cummulative prob of 5 highest values <= 0.6 ] , dtype=tf.floataa , ) __snake_case = tf.convert_to_tensor( [[0, 0], [0, 9], [0, 1_0], [0, 2_5], [0, 2_6], [1, 1_3], [1, 1_7], [1, 1_8], [1, 2_0], [1, 2_7]] , dtype=tf.intaa , ) # expected non filtered idx as noted above __snake_case = tf.convert_to_tensor( [8.222099, 7.3534126, 8.432078, 7.4402075, 9.38451, 6.271159, 8.827531, 5.4402995, 7.3857956, 9.677023] , dtype=tf.floataa , ) # expected non filtered values as noted above __snake_case = tf_top_k_top_p_filtering(SCREAMING_SNAKE_CASE , top_k=1_0 , top_p=0.6 , min_tokens_to_keep=4 ) __snake_case = output[output != -float("inf" )] __snake_case = tf.cast( tf.where(tf.not_equal(SCREAMING_SNAKE_CASE , tf.constant(-float("inf" ) , dtype=tf.floataa ) ) ) , dtype=tf.intaa , ) tf.debugging.assert_near(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , rtol=1e-1_2 ) tf.debugging.assert_equal(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) @require_tf class UpperCamelCase( unittest.TestCase , __lowercase ): # setting framework_dependent_parameters needs to be gated, just like its contents' imports if is_tf_available(): snake_case_ : Optional[Any] = { '''AutoModelForCausalLM''': TFAutoModelForCausalLM, '''AutoModelForSpeechSeq2Seq''': TFAutoModelForSpeechSeqaSeq, '''AutoModelForSeq2SeqLM''': TFAutoModelForSeqaSeqLM, '''AutoModelForVision2Seq''': TFAutoModelForVisionaSeq, '''LogitsProcessorList''': TFLogitsProcessorList, '''MinLengthLogitsProcessor''': TFMinLengthLogitsProcessor, '''create_tensor_fn''': tf.convert_to_tensor, '''floats_tensor''': floats_tensor, '''return_tensors''': '''tf''', } @slow def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] ) -> List[Any]: '''simple docstring''' __snake_case = TFAutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-random-gpt2" ) __snake_case = 2 __snake_case = 2 class UpperCamelCase( tf.Module ): def __init__( self : str , SCREAMING_SNAKE_CASE : List[str] ) -> Tuple: '''simple docstring''' super(SCREAMING_SNAKE_CASE , self ).__init__() __snake_case = model @tf.function( input_signature=( tf.TensorSpec((None, input_length) , tf.intaa , name="input_ids" ), tf.TensorSpec((None, input_length) , tf.intaa , name="attention_mask" ), ) , jit_compile=SCREAMING_SNAKE_CASE , ) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , SCREAMING_SNAKE_CASE : Optional[Any] , SCREAMING_SNAKE_CASE : Optional[int] ) -> Any: '''simple docstring''' __snake_case = self.model.generate( input_ids=SCREAMING_SNAKE_CASE , attention_mask=SCREAMING_SNAKE_CASE , max_new_tokens=SCREAMING_SNAKE_CASE , return_dict_in_generate=SCREAMING_SNAKE_CASE , ) return {"sequences": outputs["sequences"]} __snake_case = [[2, 0], [1_0_2, 1_0_3]] __snake_case = [[1, 0], [1, 1]] __snake_case = DummyModel(model=SCREAMING_SNAKE_CASE ) with tempfile.TemporaryDirectory() as tmp_dir: tf.saved_model.save(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , signatures={"serving_default": dummy_model.serving} ) __snake_case = tf.saved_model.load(SCREAMING_SNAKE_CASE ).signatures["""serving_default"""] for batch_size in range(1 , len(SCREAMING_SNAKE_CASE ) + 1 ): __snake_case = { """input_ids""": tf.constant(dummy_input_ids[:batch_size] ), """attention_mask""": tf.constant(dummy_attention_masks[:batch_size] ), } __snake_case = serving_func(**SCREAMING_SNAKE_CASE )["""sequences"""] __snake_case = test_model.generate(**SCREAMING_SNAKE_CASE , max_new_tokens=SCREAMING_SNAKE_CASE ) tf.debugging.assert_equal(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) @slow def SCREAMING_SNAKE_CASE_ ( self : Optional[int] ) -> Optional[int]: '''simple docstring''' __snake_case = TFAutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-random-gpt2" ) __snake_case = 1 __snake_case = 2 class UpperCamelCase( tf.Module ): def __init__( self : str , SCREAMING_SNAKE_CASE : List[str] ) -> List[Any]: '''simple docstring''' super(SCREAMING_SNAKE_CASE , self ).__init__() __snake_case = model @tf.function( input_signature=( tf.TensorSpec((batch_size, None) , tf.intaa , name="input_ids" ), tf.TensorSpec((batch_size, None) , tf.intaa , name="attention_mask" ), ) , jit_compile=SCREAMING_SNAKE_CASE , ) def SCREAMING_SNAKE_CASE_ ( self : List[Any] , SCREAMING_SNAKE_CASE : str , SCREAMING_SNAKE_CASE : int ) -> str: '''simple docstring''' __snake_case = self.model.generate( input_ids=SCREAMING_SNAKE_CASE , attention_mask=SCREAMING_SNAKE_CASE , max_new_tokens=SCREAMING_SNAKE_CASE , return_dict_in_generate=SCREAMING_SNAKE_CASE , ) return {"sequences": outputs["sequences"]} __snake_case = [[2], [1_0_2, 1_0_3]] __snake_case = [[1], [1, 1]] __snake_case = DummyModel(model=SCREAMING_SNAKE_CASE ) with tempfile.TemporaryDirectory() as tmp_dir: tf.saved_model.save(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , signatures={"serving_default": dummy_model.serving} ) __snake_case = tf.saved_model.load(SCREAMING_SNAKE_CASE ).signatures["""serving_default"""] for input_row in range(len(SCREAMING_SNAKE_CASE ) ): __snake_case = { """input_ids""": tf.constant([dummy_input_ids[input_row]] ), """attention_mask""": tf.constant([dummy_attention_masks[input_row]] ), } __snake_case = serving_func(**SCREAMING_SNAKE_CASE )["""sequences"""] __snake_case = test_model.generate(**SCREAMING_SNAKE_CASE , max_new_tokens=SCREAMING_SNAKE_CASE ) tf.debugging.assert_equal(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) @slow @require_tensorflow_text def SCREAMING_SNAKE_CASE_ ( self : List[str] ) -> Dict: '''simple docstring''' with tempfile.TemporaryDirectory() as tmp_dir: # file needed to load the TF tokenizer hf_hub_download(repo_id="google/flan-t5-small" , filename="spiece.model" , local_dir=SCREAMING_SNAKE_CASE ) class UpperCamelCase( tf.keras.layers.Layer ): def __init__( self : Optional[int] ) -> int: '''simple docstring''' super().__init__() __snake_case = text.SentencepieceTokenizer( model=tf.io.gfile.GFile(os.path.join(SCREAMING_SNAKE_CASE , "spiece.model" ) , "rb" ).read() ) __snake_case = TFAutoModelForSeqaSeqLM.from_pretrained("hf-internal-testing/tiny-random-t5" ) def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , SCREAMING_SNAKE_CASE : Tuple , *SCREAMING_SNAKE_CASE : List[Any] , **SCREAMING_SNAKE_CASE : Any ) -> Optional[int]: '''simple docstring''' __snake_case = self.tokenizer.tokenize(SCREAMING_SNAKE_CASE ) __snake_case = text.pad_model_inputs( SCREAMING_SNAKE_CASE , max_seq_length=6_4 , pad_value=self.model.config.pad_token_id ) __snake_case = self.model.generate(input_ids=SCREAMING_SNAKE_CASE , attention_mask=SCREAMING_SNAKE_CASE ) return self.tokenizer.detokenize(SCREAMING_SNAKE_CASE ) __snake_case = CompleteSentenceTransformer() __snake_case = tf.keras.layers.Input(shape=(1,) , dtype=tf.string , name="inputs" ) __snake_case = complete_model(SCREAMING_SNAKE_CASE ) __snake_case = tf.keras.Model(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) keras_model.save(SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE_ ( self : str ) -> Dict: '''simple docstring''' __snake_case = { """do_sample""": True, """num_beams""": 1, """top_p""": 0.7, """top_k""": 1_0, """temperature""": 0.7, } __snake_case = 1_4 __snake_case = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-gpt2" ) __snake_case = """Hello, my dog is cute and""" __snake_case = tokenizer(SCREAMING_SNAKE_CASE , return_tensors="tf" ) __snake_case = TFAutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-random-gpt2" ) __snake_case = 6_3_8 # forces the generation to happen on CPU, to avoid GPU-related quirks with tf.device(":/CPU:0" ): tf.random.set_seed(0 ) __snake_case = model.generate(**SCREAMING_SNAKE_CASE , eos_token_id=SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) self.assertTrue(expectation == len(generated_tokens[0] ) ) __snake_case = [6_3_8, 1_9_8] with tf.device(":/CPU:0" ): tf.random.set_seed(0 ) __snake_case = model.generate(**SCREAMING_SNAKE_CASE , eos_token_id=SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) self.assertTrue(expectation == len(generated_tokens[0] ) ) def SCREAMING_SNAKE_CASE_ ( self : Any ) -> List[str]: '''simple docstring''' __snake_case = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-bart" ) __snake_case = """Hugging Face is a technology company based in New York and Paris.""" __snake_case = bart_tokenizer(SCREAMING_SNAKE_CASE , return_tensors="tf" ).input_ids __snake_case = TFBartForConditionalGeneration.from_pretrained("hf-internal-testing/tiny-random-bart" ) __snake_case = bart_model.generate(SCREAMING_SNAKE_CASE ).numpy() class UpperCamelCase( __lowercase ): def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] , SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : Dict=None , **SCREAMING_SNAKE_CASE : Dict ) -> str: '''simple docstring''' return super().call(SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) __snake_case = FakeBart.from_pretrained("hf-internal-testing/tiny-random-bart" ) __snake_case = bart_model.generate(SCREAMING_SNAKE_CASE , foo="bar" ).numpy() self.assertTrue(np.array_equal(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) ) class UpperCamelCase( bart_model.model.encoder.__class__ ): def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] , SCREAMING_SNAKE_CASE : Tuple , **SCREAMING_SNAKE_CASE : Any ) -> int: '''simple docstring''' return super().call(SCREAMING_SNAKE_CASE , **SCREAMING_SNAKE_CASE ) __snake_case = FakeEncoder(bart_model.config , bart_model.model.shared ) __snake_case = fake_encoder # Normal generation still works (the output will be different because the encoder weights are different) __snake_case = bart_model.generate(SCREAMING_SNAKE_CASE ).numpy() with self.assertRaises(SCREAMING_SNAKE_CASE ): # FakeEncoder.call() accepts **kwargs -> no filtering -> value error due to unexpected input "foo" bart_model.generate(SCREAMING_SNAKE_CASE , foo="bar" )
371
'''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() __UpperCamelCase = logging.get_logger(__name__) def _a ( _lowerCamelCase ) -> Any: """simple docstring""" __snake_case : Union[str, Any] = MobileNetVaConfig(layer_norm_eps=0.0_01 ) if "_quant" in model_name: raise ValueError("""Quantized models are not supported.""" ) __snake_case : List[Any] = re.match(R"""^mobilenet_v1_([^_]*)_([^_]*)$""" , _lowerCamelCase ) if matches: __snake_case : Optional[Any] = float(matches[1] ) __snake_case : Union[str, Any] = int(matches[2] ) # The TensorFlow version of MobileNetV1 predicts 1001 classes instead of # the usual 1000. The first class (index 0) is "background". __snake_case : Tuple = 1001 __snake_case : Any = """imagenet-1k-id2label.json""" __snake_case : Optional[Any] = """huggingface/label-files""" __snake_case : List[Any] = json.load(open(hf_hub_download(_lowerCamelCase , _lowerCamelCase , repo_type="""dataset""" ) , """r""" ) ) __snake_case : Dict = {int(_lowerCamelCase ) + 1: v for k, v in idalabel.items()} __snake_case : List[str] = """background""" __snake_case : List[str] = idalabel __snake_case : List[Any] = {v: k for k, v in idalabel.items()} return config def _a ( ) -> Optional[Any]: """simple docstring""" __snake_case : Optional[int] = """http://images.cocodataset.org/val2017/000000039769.jpg""" __snake_case : List[Any] = Image.open(requests.get(_lowerCamelCase , stream=_lowerCamelCase ).raw ) return im @torch.no_grad() def _a ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase=False ) -> Optional[Any]: """simple docstring""" __snake_case : Optional[int] = get_mobilenet_va_config(_lowerCamelCase ) # Load 🤗 model __snake_case : Optional[Any] = MobileNetVaForImageClassification(_lowerCamelCase ).eval() # Load weights from TensorFlow checkpoint load_tf_weights_in_mobilenet_va(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) # Check outputs on an image, prepared by MobileNetV1ImageProcessor __snake_case : Optional[int] = MobileNetVaImageProcessor( crop_size={"""width""": config.image_size, """height""": config.image_size} , size={"""shortest_edge""": config.image_size + 32} , ) __snake_case : Tuple = image_processor(images=prepare_img() , return_tensors="""pt""" ) __snake_case : Optional[Any] = model(**_lowerCamelCase ) __snake_case : List[Any] = outputs.logits assert logits.shape == (1, 1001) if model_name == "mobilenet_v1_1.0_224": __snake_case : str = torch.tensor([-4.17_39, -1.12_33, 3.12_05] ) elif model_name == "mobilenet_v1_0.75_192": __snake_case : Tuple = torch.tensor([-3.94_40, -2.31_41, -0.33_33] ) else: __snake_case : List[Any] = None if expected_logits is not None: assert torch.allclose(logits[0, :3] , _lowerCamelCase , atol=1E-4 ) Path(_lowerCamelCase ).mkdir(exist_ok=_lowerCamelCase ) print(F'''Saving model {model_name} to {pytorch_dump_folder_path}''' ) model.save_pretrained(_lowerCamelCase ) print(F'''Saving image processor to {pytorch_dump_folder_path}''' ) image_processor.save_pretrained(_lowerCamelCase ) if push_to_hub: print("""Pushing to the hub...""" ) __snake_case : Optional[Any] = """google/""" + model_name image_processor.push_to_hub(_lowerCamelCase ) model.push_to_hub(_lowerCamelCase ) if __name__ == "__main__": __UpperCamelCase = 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." ) __UpperCamelCase = parser.parse_args() convert_movilevit_checkpoint( args.model_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub )
26
0
from __future__ import annotations def _lowercase ( UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_): # noqa: E741 """simple docstring""" while r - l > 1: snake_case__ : str = (l + r) // 2 if v[m] >= key: snake_case__ : Any = m else: snake_case__ : Union[str, Any] = m # noqa: E741 return r def _lowercase ( UpperCAmelCase_): """simple docstring""" if len(_lowerCamelCase) == 0: return 0 snake_case__ : Optional[Any] = [0] * len(_lowerCamelCase) snake_case__ : Dict = 1 snake_case__ : Any = v[0] for i in range(1 , len(_lowerCamelCase)): if v[i] < tail[0]: snake_case__ : List[str] = v[i] elif v[i] > tail[length - 1]: snake_case__ : Any = v[i] length += 1 else: snake_case__ : Optional[Any] = v[i] return length if __name__ == "__main__": import doctest doctest.testmod()
648
'''simple docstring''' from sklearn.metrics import recall_score import datasets __UpperCamelCase = "\nRecall is the fraction of the positive examples that were correctly labeled by the model as positive. It can be computed with the equation:\nRecall = TP / (TP + FN)\nWhere TP is the true positives and FN is the false negatives.\n" __UpperCamelCase = "\nArgs:\n- **predictions** (`list` of `int`): The predicted labels.\n- **references** (`list` of `int`): The ground truth labels.\n- **labels** (`list` of `int`): The set of labels to include when `average` is not set to `binary`, and their order when average is `None`. Labels present in the data can be excluded in this input, for example to calculate a multiclass average ignoring a majority negative class, while labels not present in the data will result in 0 components in a macro average. For multilabel targets, labels are column indices. By default, all labels in y_true and y_pred are used in sorted order. Defaults to None.\n- **pos_label** (`int`): The class label to use as the 'positive class' when calculating the recall. Defaults to `1`.\n- **average** (`string`): This parameter is required for multiclass/multilabel targets. If None, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data. Defaults to `'binary'`.\n - `'binary'`: Only report results for the class specified by `pos_label`. This is applicable only if the target labels and predictions are binary.\n - `'micro'`: Calculate metrics globally by counting the total true positives, false negatives, and false positives.\n - `'macro'`: Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account.\n - `'weighted'`: Calculate metrics for each label, and find their average weighted by support (the number of true instances for each label). This alters `'macro'` to account for label imbalance. Note that it can result in an F-score that is not between precision and recall.\n - `'samples'`: Calculate metrics for each instance, and find their average (only meaningful for multilabel classification).\n- **sample_weight** (`list` of `float`): Sample weights Defaults to `None`.\n- **zero_division** (): Sets the value to return when there is a zero division. Defaults to .\n - `'warn'`: If there is a zero division, the return value is `0`, but warnings are also raised.\n - `0`: If there is a zero division, the return value is `0`.\n - `1`: If there is a zero division, the return value is `1`.\n\nReturns:\n- **recall** (`float`, or `array` of `float`): Either the general recall score, or the recall scores for individual classes, depending on the values input to `labels` and `average`. Minimum possible value is 0. Maximum possible value is 1. A higher recall means that more of the positive examples have been labeled correctly. Therefore, a higher recall is generally considered better.\n\nExamples:\n\n Example 1-A simple example with some errors\n >>> recall_metric = datasets.load_metric('recall')\n >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1])\n >>> print(results)\n {'recall': 0.6666666666666666}\n\n Example 2-The same example as Example 1, but with `pos_label=0` instead of the default `pos_label=1`.\n >>> recall_metric = datasets.load_metric('recall')\n >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1], pos_label=0)\n >>> print(results)\n {'recall': 0.5}\n\n Example 3-The same example as Example 1, but with `sample_weight` included.\n >>> recall_metric = datasets.load_metric('recall')\n >>> sample_weight = [0.9, 0.2, 0.9, 0.3, 0.8]\n >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1], sample_weight=sample_weight)\n >>> print(results)\n {'recall': 0.55}\n\n Example 4-A multiclass example, using different averages.\n >>> recall_metric = datasets.load_metric('recall')\n >>> predictions = [0, 2, 1, 0, 0, 1]\n >>> references = [0, 1, 2, 0, 1, 2]\n >>> results = recall_metric.compute(predictions=predictions, references=references, average='macro')\n >>> print(results)\n {'recall': 0.3333333333333333}\n >>> results = recall_metric.compute(predictions=predictions, references=references, average='micro')\n >>> print(results)\n {'recall': 0.3333333333333333}\n >>> results = recall_metric.compute(predictions=predictions, references=references, average='weighted')\n >>> print(results)\n {'recall': 0.3333333333333333}\n >>> results = recall_metric.compute(predictions=predictions, references=references, average=None)\n >>> print(results)\n {'recall': array([1., 0., 0.])}\n" __UpperCamelCase = "\n@article{scikit-learn, title={Scikit-learn: Machine Learning in {P}ython}, author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V. and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P. and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.}, journal={Journal of Machine Learning Research}, volume={12}, pages={2825--2830}, year={2011}\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class _A ( datasets.Metric ): def lowercase__ ( self : Optional[int] ) -> Any: """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": datasets.Sequence(datasets.Value("""int32""" ) ), """references""": datasets.Sequence(datasets.Value("""int32""" ) ), } if self.config_name == """multilabel""" else { """predictions""": datasets.Value("""int32""" ), """references""": datasets.Value("""int32""" ), } ) , reference_urls=["""https://scikit-learn.org/stable/modules/generated/sklearn.metrics.recall_score.html"""] , ) def lowercase__ ( self : Tuple , __magic_name__ : int , __magic_name__ : Union[str, Any] , __magic_name__ : Any=None , __magic_name__ : Optional[Any]=1 , __magic_name__ : List[str]="binary" , __magic_name__ : Tuple=None , __magic_name__ : Dict="warn" , ) -> Any: """simple docstring""" __snake_case : Tuple = recall_score( __magic_name__ , __magic_name__ , labels=__magic_name__ , pos_label=__magic_name__ , average=__magic_name__ , sample_weight=__magic_name__ , zero_division=__magic_name__ , ) return {"recall": float(__magic_name__ ) if score.size == 1 else score}
26
0
"""simple docstring""" def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE__, SCREAMING_SNAKE_CASE__ ) -> list[int]: a_ : Tuple = int(_lowerCamelCase ) # Initialize Result a_ : Union[str, Any] = [] # Traverse through all denomination for denomination in reversed(_lowerCamelCase ): # Find denominations while int(_lowerCamelCase ) >= int(_lowerCamelCase ): total_value -= int(_lowerCamelCase ) answer.append(_lowerCamelCase ) # Append the "answers" array return answer # Driver Code if __name__ == "__main__": SCREAMING_SNAKE_CASE_ = [] SCREAMING_SNAKE_CASE_ = """0""" if ( input("""Do you want to enter your denominations ? (yY/n): """).strip().lower() == "y" ): SCREAMING_SNAKE_CASE_ = 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())) SCREAMING_SNAKE_CASE_ = input("""Enter the change you want to make in Indian Currency: """).strip() else: # All denominations of Indian Currency if user does not enter SCREAMING_SNAKE_CASE_ = [1, 2, 5, 10, 20, 50, 1_00, 5_00, 20_00] SCREAMING_SNAKE_CASE_ = 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}: """) SCREAMING_SNAKE_CASE_ = find_minimum_change(denominations, value) # Print result for i in range(len(answer)): print(answer[i], end=""" """)
237
'''simple docstring''' from sklearn.metrics import matthews_corrcoef import datasets __UpperCamelCase = "\nCompute the Matthews correlation coefficient (MCC)\n\nThe Matthews correlation coefficient is used in machine learning as a\nmeasure of the quality of binary and multiclass classifications. It takes\ninto account true and false positives and negatives and is generally\nregarded as a balanced measure which can be used even if the classes are of\nvery different sizes. The MCC is in essence a correlation coefficient value\nbetween -1 and +1. A coefficient of +1 represents a perfect prediction, 0\nan average random prediction and -1 an inverse prediction. The statistic\nis also known as the phi coefficient. [source: Wikipedia]\n" __UpperCamelCase = "\nArgs:\n predictions (list of int): Predicted labels, as returned by a model.\n references (list of int): Ground truth labels.\n sample_weight (list of int, float, or bool): Sample weights. Defaults to `None`.\nReturns:\n matthews_correlation (dict containing float): Matthews correlation.\nExamples:\n Example 1, a basic example with only predictions and references as inputs:\n >>> matthews_metric = datasets.load_metric(\"matthews_correlation\")\n >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],\n ... predictions=[1, 2, 2, 0, 3, 3])\n >>> print(round(results['matthews_correlation'], 2))\n 0.54\n\n Example 2, the same example as above, but also including sample weights:\n >>> matthews_metric = datasets.load_metric(\"matthews_correlation\")\n >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],\n ... predictions=[1, 2, 2, 0, 3, 3],\n ... sample_weight=[0.5, 3, 1, 1, 1, 2])\n >>> print(round(results['matthews_correlation'], 2))\n 0.1\n\n Example 3, the same example as above, but with sample weights that cause a negative correlation:\n >>> matthews_metric = datasets.load_metric(\"matthews_correlation\")\n >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],\n ... predictions=[1, 2, 2, 0, 3, 3],\n ... sample_weight=[0.5, 1, 0, 0, 0, 1])\n >>> print(round(results['matthews_correlation'], 2))\n -0.25\n" __UpperCamelCase = "\\n@article{scikit-learn,\n title={Scikit-learn: Machine Learning in {P}ython},\n author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V.\n and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P.\n and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and\n Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.},\n journal={Journal of Machine Learning Research},\n volume={12},\n pages={2825--2830},\n year={2011}\n}\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class _A ( datasets.Metric ): def lowercase__ ( self : Tuple ) -> Dict: """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": datasets.Value("""int32""" ), """references""": datasets.Value("""int32""" ), } ) , reference_urls=[ """https://scikit-learn.org/stable/modules/generated/sklearn.metrics.matthews_corrcoef.html""" ] , ) def lowercase__ ( self : List[Any] , __magic_name__ : Tuple , __magic_name__ : List[Any] , __magic_name__ : Union[str, Any]=None ) -> Optional[int]: """simple docstring""" return { "matthews_correlation": float(matthews_corrcoef(__magic_name__ , __magic_name__ , sample_weight=__magic_name__ ) ), }
26
0
"""simple docstring""" import sys from collections import defaultdict class a__ : def __init__( self : List[Any]) -> Optional[int]: """simple docstring""" _lowerCAmelCase:Optional[Any] = [] def __UpperCamelCase ( self : int ,a__ : List[Any]) -> Dict: """simple docstring""" return self.node_position[vertex] def __UpperCamelCase ( self : Optional[int] ,a__ : List[str] ,a__ : Optional[Any]) -> List[Any]: """simple docstring""" _lowerCAmelCase:List[str] = pos def __UpperCamelCase ( self : int ,a__ : int ,a__ : int ,a__ : Any ,a__ : str) -> Optional[Any]: """simple docstring""" if start > size // 2 - 1: return else: if 2 * start + 2 >= size: _lowerCAmelCase:int = 2 * start + 1 else: if heap[2 * start + 1] < heap[2 * start + 2]: _lowerCAmelCase:int = 2 * start + 1 else: _lowerCAmelCase:Any = 2 * start + 2 if heap[smallest_child] < heap[start]: _lowerCAmelCase:Tuple = heap[smallest_child], positions[smallest_child] _lowerCAmelCase:Dict = ( heap[start], positions[start], ) _lowerCAmelCase:Union[str, Any] = temp, tempa _lowerCAmelCase:Tuple = self.get_position(positions[smallest_child]) self.set_position( positions[smallest_child] ,self.get_position(positions[start])) self.set_position(positions[start] ,a__) self.top_to_bottom(a__ ,a__ ,a__ ,a__) def __UpperCamelCase ( self : str ,a__ : str ,a__ : List[str] ,a__ : Any ,a__ : Union[str, Any]) -> int: """simple docstring""" _lowerCAmelCase:List[Any] = position[index] while index != 0: _lowerCAmelCase:List[str] = int((index - 2) / 2) if index % 2 == 0 else int((index - 1) / 2) if val < heap[parent]: _lowerCAmelCase:Optional[Any] = heap[parent] _lowerCAmelCase:Dict = position[parent] self.set_position(position[parent] ,a__) else: _lowerCAmelCase:int = val _lowerCAmelCase:int = temp self.set_position(a__ ,a__) break _lowerCAmelCase:List[str] = parent else: _lowerCAmelCase:Dict = val _lowerCAmelCase:Optional[int] = temp self.set_position(a__ ,0) def __UpperCamelCase ( self : Optional[int] ,a__ : Dict ,a__ : Tuple) -> List[str]: """simple docstring""" _lowerCAmelCase:Any = len(a__) // 2 - 1 for i in range(a__ ,-1 ,-1): self.top_to_bottom(a__ ,a__ ,len(a__) ,a__) def __UpperCamelCase ( self : List[Any] ,a__ : List[Any] ,a__ : List[Any]) -> List[str]: """simple docstring""" _lowerCAmelCase:Optional[Any] = positions[0] _lowerCAmelCase:Optional[int] = sys.maxsize self.top_to_bottom(a__ ,0 ,len(a__) ,a__) return temp def UpperCAmelCase ( snake_case : int ): _lowerCAmelCase:List[Any] = Heap() _lowerCAmelCase:List[Any] = [0] * len(_lowerCamelCase ) _lowerCAmelCase:Dict = [-1] * len(_lowerCamelCase ) # Neighboring Tree Vertex of selected vertex # Minimum Distance of explored vertex with neighboring vertex of partial tree # formed in graph _lowerCAmelCase:List[Any] = [] # Heap of Distance of vertices from their neighboring vertex _lowerCAmelCase:str = [] for vertex in range(len(_lowerCamelCase ) ): distance_tv.append(sys.maxsize ) positions.append(_lowerCamelCase ) heap.node_position.append(_lowerCamelCase ) _lowerCAmelCase:Optional[int] = [] _lowerCAmelCase:List[str] = 1 _lowerCAmelCase:Any = sys.maxsize for neighbor, distance in adjacency_list[0]: _lowerCAmelCase:List[str] = 0 _lowerCAmelCase:List[Any] = distance heap.heapify(_lowerCamelCase , _lowerCamelCase ) for _ in range(1 , len(_lowerCamelCase ) ): _lowerCAmelCase:Tuple = heap.delete_minimum(_lowerCamelCase , _lowerCamelCase ) if visited[vertex] == 0: tree_edges.append((nbr_tv[vertex], vertex) ) _lowerCAmelCase:Any = 1 for neighbor, distance in adjacency_list[vertex]: if ( visited[neighbor] == 0 and distance < distance_tv[heap.get_position(_lowerCamelCase )] ): _lowerCAmelCase:Tuple = distance heap.bottom_to_top( _lowerCamelCase , heap.get_position(_lowerCamelCase ) , _lowerCamelCase , _lowerCamelCase ) _lowerCAmelCase:int = vertex return tree_edges if __name__ == "__main__": # pragma: no cover # < --------- Prims Algorithm --------- > UpperCamelCase__ = int(input('''Enter number of edges: ''').strip()) UpperCamelCase__ = defaultdict(list) for _ in range(edges_number): UpperCamelCase__ = [int(x) for x in input().strip().split()] adjacency_list[edge[0]].append([edge[1], edge[2]]) adjacency_list[edge[1]].append([edge[0], edge[2]]) print(prisms_algorithm(adjacency_list))
227
'''simple docstring''' import json import os from pathlib import Path import pytest from datasets.download.download_config import DownloadConfig from datasets.download.download_manager import DownloadManager from datasets.utils.file_utils import hash_url_to_filename __UpperCamelCase = "http://www.mocksite.com/file1.txt" __UpperCamelCase = "\"text\": [\"foo\", \"foo\"]" __UpperCamelCase = "6d8ce9aa78a471c7477201efbeabd3bb01ac2e7d100a6dc024ba1608361f90a8" class _A : lowercase__: str = 200 lowercase__: List[str] = {'''Content-Length''': '''100'''} lowercase__: Union[str, Any] = {} def lowercase__ ( self : Any , **__magic_name__ : List[Any] ) -> Dict: """simple docstring""" return [bytes(__magic_name__ , """utf-8""" )] def _a ( *_lowerCamelCase , **_lowerCamelCase ) -> List[str]: """simple docstring""" return MockResponse() @pytest.mark.parametrize("""urls_type""" , [str, list, dict] ) def _a ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) -> List[str]: """simple docstring""" import requests monkeypatch.setattr(_lowerCamelCase , """request""" , _lowerCamelCase ) __snake_case : Union[str, Any] = URL if issubclass(_lowerCamelCase , _lowerCamelCase ): __snake_case : str = url elif issubclass(_lowerCamelCase , _lowerCamelCase ): __snake_case : Dict = [url] elif issubclass(_lowerCamelCase , _lowerCamelCase ): __snake_case : Union[str, Any] = {"""train""": url} __snake_case : Dict = """dummy""" __snake_case : List[str] = """downloads""" __snake_case : List[Any] = tmp_path __snake_case : List[Any] = DownloadConfig( cache_dir=os.path.join(_lowerCamelCase , _lowerCamelCase ) , use_etag=_lowerCamelCase , ) __snake_case : List[str] = DownloadManager(dataset_name=_lowerCamelCase , download_config=_lowerCamelCase ) __snake_case : int = dl_manager.download(_lowerCamelCase ) __snake_case : Tuple = urls for downloaded_paths in [downloaded_paths]: if isinstance(_lowerCamelCase , _lowerCamelCase ): __snake_case : Any = [downloaded_paths] __snake_case : List[Any] = [urls] elif isinstance(_lowerCamelCase , _lowerCamelCase ): assert "train" in downloaded_paths.keys() __snake_case : Tuple = downloaded_paths.values() __snake_case : Optional[int] = urls.values() assert downloaded_paths for downloaded_path, input_url in zip(_lowerCamelCase , _lowerCamelCase ): assert downloaded_path == dl_manager.downloaded_paths[input_url] __snake_case : List[str] = Path(_lowerCamelCase ) __snake_case : Any = downloaded_path.parts assert parts[-1] == HASH assert parts[-2] == cache_subdir assert downloaded_path.exists() __snake_case : Union[str, Any] = downloaded_path.read_text() assert content == CONTENT __snake_case : List[str] = downloaded_path.with_suffix(""".json""" ) assert metadata_downloaded_path.exists() __snake_case : Union[str, Any] = json.loads(metadata_downloaded_path.read_text() ) assert metadata_content == {"url": URL, "etag": None} @pytest.mark.parametrize("""paths_type""" , [str, list, dict] ) def _a ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) -> List[Any]: """simple docstring""" __snake_case : Any = str(_lowerCamelCase ) if issubclass(_lowerCamelCase , _lowerCamelCase ): __snake_case : Optional[int] = filename elif issubclass(_lowerCamelCase , _lowerCamelCase ): __snake_case : Tuple = [filename] elif issubclass(_lowerCamelCase , _lowerCamelCase ): __snake_case : Dict = {"""train""": filename} __snake_case : Optional[Any] = """dummy""" __snake_case : List[Any] = xz_file.parent __snake_case : int = """extracted""" __snake_case : Dict = DownloadConfig( cache_dir=_lowerCamelCase , use_etag=_lowerCamelCase , ) __snake_case : List[str] = DownloadManager(dataset_name=_lowerCamelCase , download_config=_lowerCamelCase ) __snake_case : Optional[Any] = dl_manager.extract(_lowerCamelCase ) __snake_case : Union[str, Any] = paths for extracted_paths in [extracted_paths]: if isinstance(_lowerCamelCase , _lowerCamelCase ): __snake_case : Dict = [extracted_paths] __snake_case : int = [paths] elif isinstance(_lowerCamelCase , _lowerCamelCase ): assert "train" in extracted_paths.keys() __snake_case : int = extracted_paths.values() __snake_case : int = paths.values() assert extracted_paths for extracted_path, input_path in zip(_lowerCamelCase , _lowerCamelCase ): assert extracted_path == dl_manager.extracted_paths[input_path] __snake_case : Any = Path(_lowerCamelCase ) __snake_case : str = extracted_path.parts assert parts[-1] == hash_url_to_filename(_lowerCamelCase , etag=_lowerCamelCase ) assert parts[-2] == extracted_subdir assert extracted_path.exists() __snake_case : Optional[int] = extracted_path.read_text() __snake_case : str = text_file.read_text() assert extracted_file_content == expected_file_content def _a ( _lowerCamelCase , _lowerCamelCase ) -> Optional[int]: """simple docstring""" assert path.endswith(""".jsonl""" ) for num_items, line in enumerate(_lowerCamelCase , start=1 ): __snake_case : Tuple = json.loads(line.decode("""utf-8""" ) ) assert item.keys() == {"col_1", "col_2", "col_3"} assert num_items == 4 @pytest.mark.parametrize("""archive_jsonl""" , ["""tar_jsonl_path""", """zip_jsonl_path"""] ) def _a ( _lowerCamelCase , _lowerCamelCase ) -> Optional[int]: """simple docstring""" __snake_case : Any = request.getfixturevalue(_lowerCamelCase ) __snake_case : str = DownloadManager() for num_jsonl, (path, file) in enumerate(dl_manager.iter_archive(_lowerCamelCase ) , start=1 ): _test_jsonl(_lowerCamelCase , _lowerCamelCase ) assert num_jsonl == 2 @pytest.mark.parametrize("""archive_nested_jsonl""" , ["""tar_nested_jsonl_path""", """zip_nested_jsonl_path"""] ) def _a ( _lowerCamelCase , _lowerCamelCase ) -> List[str]: """simple docstring""" __snake_case : int = request.getfixturevalue(_lowerCamelCase ) __snake_case : List[str] = DownloadManager() for num_tar, (path, file) in enumerate(dl_manager.iter_archive(_lowerCamelCase ) , start=1 ): for num_jsonl, (subpath, subfile) in enumerate(dl_manager.iter_archive(_lowerCamelCase ) , start=1 ): _test_jsonl(_lowerCamelCase , _lowerCamelCase ) assert num_tar == 1 assert num_jsonl == 2 def _a ( _lowerCamelCase ) -> Any: """simple docstring""" __snake_case : List[str] = DownloadManager() for num_file, file in enumerate(dl_manager.iter_files(_lowerCamelCase ) , start=1 ): assert os.path.basename(_lowerCamelCase ) == ("test.txt" if num_file == 1 else "train.txt") assert num_file == 2
26
0
import math from dataclasses import dataclass from typing import Optional, Tuple, Union import numpy as np import torch from ..configuration_utils import ConfigMixin, register_to_config from ..utils import BaseOutput, randn_tensor from .scheduling_utils import SchedulerMixin @dataclass # Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->UnCLIP class UpperCAmelCase_ ( __lowercase ): __lowerCamelCase = 42 __lowerCamelCase = None def _lowerCamelCase ( __lowerCamelCase , __lowerCamelCase=0.999 , __lowerCamelCase="cosine" , ) -> int: '''simple docstring''' if alpha_transform_type == "cosine": def alpha_bar_fn(__lowerCamelCase ): return math.cos((t + 0.008) / 1.008 * math.pi / 2 ) ** 2 elif alpha_transform_type == "exp": def alpha_bar_fn(__lowerCamelCase ): return math.exp(t * -12.0 ) else: raise ValueError(F"Unsupported alpha_tranform_type: {alpha_transform_type}" ) UpperCAmelCase__ : Dict = [] for i in range(_lowerCamelCase ): UpperCAmelCase__ : List[Any] = i / num_diffusion_timesteps UpperCAmelCase__ : str = (i + 1) / num_diffusion_timesteps betas.append(min(1 - alpha_bar_fn(_lowerCamelCase ) / alpha_bar_fn(_lowerCamelCase ) , _lowerCamelCase ) ) return torch.tensor(_lowerCamelCase , dtype=torch.floataa ) class UpperCAmelCase_ ( __lowercase , __lowercase ): @register_to_config def __init__( self , _lowerCAmelCase = 1000 , _lowerCAmelCase = "fixed_small_log" , _lowerCAmelCase = True , _lowerCAmelCase = 1.0 , _lowerCAmelCase = "epsilon" , _lowerCAmelCase = "squaredcos_cap_v2" , ): if beta_schedule != "squaredcos_cap_v2": raise ValueError("""UnCLIPScheduler only supports `beta_schedule`: 'squaredcos_cap_v2'""" ) UpperCAmelCase__ : Optional[int] = betas_for_alpha_bar(_lowerCAmelCase ) UpperCAmelCase__ : List[str] = 1.0 - self.betas UpperCAmelCase__ : Optional[Any] = torch.cumprod(self.alphas , dim=0 ) UpperCAmelCase__ : Any = torch.tensor(1.0 ) # standard deviation of the initial noise distribution UpperCAmelCase__ : int = 1.0 # setable values UpperCAmelCase__ : Union[str, Any] = None UpperCAmelCase__ : Any = torch.from_numpy(np.arange(0 , _lowerCAmelCase )[::-1].copy() ) UpperCAmelCase__ : Optional[Any] = variance_type def __UpperCAmelCase ( self , _lowerCAmelCase , _lowerCAmelCase = None ): return sample def __UpperCAmelCase ( self , _lowerCAmelCase , _lowerCAmelCase = None ): UpperCAmelCase__ : Optional[Any] = num_inference_steps UpperCAmelCase__ : Optional[int] = (self.config.num_train_timesteps - 1) / (self.num_inference_steps - 1) UpperCAmelCase__ : int = (np.arange(0 , _lowerCAmelCase ) * step_ratio).round()[::-1].copy().astype(np.intaa ) UpperCAmelCase__ : str = torch.from_numpy(_lowerCAmelCase ).to(_lowerCAmelCase ) def __UpperCAmelCase ( self , _lowerCAmelCase , _lowerCAmelCase=None , _lowerCAmelCase=None , _lowerCAmelCase=None ): if prev_timestep is None: UpperCAmelCase__ : Union[str, Any] = t - 1 UpperCAmelCase__ : Optional[int] = self.alphas_cumprod[t] UpperCAmelCase__ : Optional[Any] = self.alphas_cumprod[prev_timestep] if prev_timestep >= 0 else self.one UpperCAmelCase__ : Union[str, Any] = 1 - alpha_prod_t UpperCAmelCase__ : int = 1 - alpha_prod_t_prev if prev_timestep == t - 1: UpperCAmelCase__ : Union[str, Any] = self.betas[t] else: UpperCAmelCase__ : Tuple = 1 - alpha_prod_t / alpha_prod_t_prev # For t > 0, compute predicted variance βt (see formula (6) and (7) from https://arxiv.org/pdf/2006.11239.pdf) # and sample from it to get previous sample # x_{t-1} ~ N(pred_prev_sample, variance) == add variance to pred_sample UpperCAmelCase__ : Union[str, Any] = beta_prod_t_prev / beta_prod_t * beta if variance_type is None: UpperCAmelCase__ : Optional[int] = self.config.variance_type # hacks - were probably added for training stability if variance_type == "fixed_small_log": UpperCAmelCase__ : int = torch.log(torch.clamp(_lowerCAmelCase , min=1e-20 ) ) UpperCAmelCase__ : List[Any] = torch.exp(0.5 * variance ) elif variance_type == "learned_range": # NOTE difference with DDPM scheduler UpperCAmelCase__ : Tuple = variance.log() UpperCAmelCase__ : str = beta.log() UpperCAmelCase__ : int = (predicted_variance + 1) / 2 UpperCAmelCase__ : Tuple = frac * max_log + (1 - frac) * min_log return variance def __UpperCAmelCase ( self , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase = None , _lowerCAmelCase=None , _lowerCAmelCase = True , ): UpperCAmelCase__ : Any = timestep if model_output.shape[1] == sample.shape[1] * 2 and self.variance_type == "learned_range": UpperCAmelCase__ : Optional[Any] = torch.split(_lowerCAmelCase , sample.shape[1] , dim=1 ) else: UpperCAmelCase__ : Tuple = None # 1. compute alphas, betas if prev_timestep is None: UpperCAmelCase__ : Tuple = t - 1 UpperCAmelCase__ : Union[str, Any] = self.alphas_cumprod[t] UpperCAmelCase__ : List[Any] = self.alphas_cumprod[prev_timestep] if prev_timestep >= 0 else self.one UpperCAmelCase__ : Optional[int] = 1 - alpha_prod_t UpperCAmelCase__ : Tuple = 1 - alpha_prod_t_prev if prev_timestep == t - 1: UpperCAmelCase__ : List[Any] = self.betas[t] UpperCAmelCase__ : List[Any] = self.alphas[t] else: UpperCAmelCase__ : int = 1 - alpha_prod_t / alpha_prod_t_prev UpperCAmelCase__ : Optional[Any] = 1 - beta # 2. compute predicted original sample from predicted noise also called # "predicted x_0" of formula (15) from https://arxiv.org/pdf/2006.11239.pdf if self.config.prediction_type == "epsilon": UpperCAmelCase__ : List[str] = (sample - beta_prod_t ** 0.5 * model_output) / alpha_prod_t ** 0.5 elif self.config.prediction_type == "sample": UpperCAmelCase__ : Optional[Any] = model_output else: raise ValueError( f"prediction_type given as {self.config.prediction_type} must be one of `epsilon` or `sample`" """ for the UnCLIPScheduler.""" ) # 3. Clip "predicted x_0" if self.config.clip_sample: UpperCAmelCase__ : Tuple = torch.clamp( _lowerCAmelCase , -self.config.clip_sample_range , self.config.clip_sample_range ) # 4. Compute coefficients for pred_original_sample x_0 and current sample x_t # See formula (7) from https://arxiv.org/pdf/2006.11239.pdf UpperCAmelCase__ : Union[str, Any] = (alpha_prod_t_prev ** 0.5 * beta) / beta_prod_t UpperCAmelCase__ : List[Any] = alpha ** 0.5 * beta_prod_t_prev / beta_prod_t # 5. Compute predicted previous sample µ_t # See formula (7) from https://arxiv.org/pdf/2006.11239.pdf UpperCAmelCase__ : List[Any] = pred_original_sample_coeff * pred_original_sample + current_sample_coeff * sample # 6. Add noise UpperCAmelCase__ : List[str] = 0 if t > 0: UpperCAmelCase__ : Dict = randn_tensor( model_output.shape , dtype=model_output.dtype , generator=_lowerCAmelCase , device=model_output.device ) UpperCAmelCase__ : Union[str, Any] = self._get_variance( _lowerCAmelCase , predicted_variance=_lowerCAmelCase , prev_timestep=_lowerCAmelCase , ) if self.variance_type == "fixed_small_log": UpperCAmelCase__ : Tuple = variance elif self.variance_type == "learned_range": UpperCAmelCase__ : int = (0.5 * variance).exp() else: raise ValueError( f"variance_type given as {self.variance_type} must be one of `fixed_small_log` or `learned_range`" """ for the UnCLIPScheduler.""" ) UpperCAmelCase__ : List[Any] = variance * variance_noise UpperCAmelCase__ : int = pred_prev_sample + variance if not return_dict: return (pred_prev_sample,) return UnCLIPSchedulerOutput(prev_sample=_lowerCAmelCase , pred_original_sample=_lowerCAmelCase ) def __UpperCAmelCase ( self , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , ): UpperCAmelCase__ : str = self.alphas_cumprod.to(device=original_samples.device , dtype=original_samples.dtype ) UpperCAmelCase__ : Tuple = timesteps.to(original_samples.device ) UpperCAmelCase__ : List[str] = alphas_cumprod[timesteps] ** 0.5 UpperCAmelCase__ : Any = sqrt_alpha_prod.flatten() while len(sqrt_alpha_prod.shape ) < len(original_samples.shape ): UpperCAmelCase__ : Union[str, Any] = sqrt_alpha_prod.unsqueeze(-1 ) UpperCAmelCase__ : List[str] = (1 - alphas_cumprod[timesteps]) ** 0.5 UpperCAmelCase__ : str = sqrt_one_minus_alpha_prod.flatten() while len(sqrt_one_minus_alpha_prod.shape ) < len(original_samples.shape ): UpperCAmelCase__ : Optional[Any] = sqrt_one_minus_alpha_prod.unsqueeze(-1 ) UpperCAmelCase__ : List[str] = sqrt_alpha_prod * original_samples + sqrt_one_minus_alpha_prod * noise return noisy_samples
79
'''simple docstring''' def _a ( _lowerCamelCase = 100 ) -> int: """simple docstring""" __snake_case : Any = n * (n + 1) * (2 * n + 1) / 6 __snake_case : List[Any] = (n * (n + 1) / 2) ** 2 return int(square_of_sum - sum_of_squares ) if __name__ == "__main__": print(f"""{solution() = }""")
26
0
from __future__ import annotations def __a ( __UpperCAmelCase : Tuple ) -> bool: """simple docstring""" lowerCamelCase_ : Union[str, Any] = len(_lowerCamelCase ) # We need to create solution object to save path. lowerCamelCase_ : Optional[Any] = [[0 for _ in range(_lowerCamelCase )] for _ in range(_lowerCamelCase )] lowerCamelCase_ : int = run_maze(_lowerCamelCase , 0 , 0 , _lowerCamelCase ) if solved: print("\n".join(str(_lowerCamelCase ) for row in solutions ) ) else: print("No solution exists!" ) return solved def __a ( __UpperCAmelCase : Optional[Any] , __UpperCAmelCase : List[Any] , __UpperCAmelCase : Union[str, Any] , __UpperCAmelCase : Any ) -> bool: """simple docstring""" lowerCamelCase_ : str = len(_lowerCamelCase ) # Final check point. if i == j == (size - 1): lowerCamelCase_ : Tuple = 1 return True lowerCamelCase_ : Union[str, Any] = (not i < 0) and (not j < 0) # Check lower bounds lowerCamelCase_ : int = (i < size) and (j < size) # Check upper bounds if lower_flag and upper_flag: # check for already visited and block points. lowerCamelCase_ : Union[str, Any] = (not solutions[i][j]) and (not maze[i][j]) if block_flag: # check visited lowerCamelCase_ : Union[str, Any] = 1 # check for directions if ( run_maze(_lowerCamelCase , i + 1 , _lowerCamelCase , _lowerCamelCase ) or run_maze(_lowerCamelCase , _lowerCamelCase , j + 1 , _lowerCamelCase ) or run_maze(_lowerCamelCase , i - 1 , _lowerCamelCase , _lowerCamelCase ) or run_maze(_lowerCamelCase , _lowerCamelCase , j - 1 , _lowerCamelCase ) ): return True lowerCamelCase_ : Dict = 0 return False return False if __name__ == "__main__": import doctest doctest.testmod()
488
'''simple docstring''' from __future__ import annotations from typing import Any class _A : def __init__( self : str , __magic_name__ : int , __magic_name__ : int , __magic_name__ : float = 0 ) -> None: """simple docstring""" __snake_case , __snake_case : Optional[Any] = row, column __snake_case : Dict = [[default_value for c in range(__magic_name__ )] for r in range(__magic_name__ )] def __str__( self : List[Any] ) -> str: """simple docstring""" __snake_case : Dict = f'''Matrix consist of {self.row} rows and {self.column} columns\n''' # Make string identifier __snake_case : Optional[int] = 0 for row_vector in self.array: for obj in row_vector: __snake_case : Optional[int] = max(__magic_name__ , len(str(__magic_name__ ) ) ) __snake_case : str = f'''%{max_element_length}s''' # Make string and return def single_line(__magic_name__ : list[float] ) -> str: nonlocal string_format_identifier __snake_case : Union[str, Any] = """[""" line += ", ".join(string_format_identifier % (obj,) for obj in row_vector ) line += "]" return line s += "\n".join(single_line(__magic_name__ ) for row_vector in self.array ) return s def __repr__( self : Optional[int] ) -> str: """simple docstring""" return str(self ) def lowercase__ ( self : Dict , __magic_name__ : tuple[int, int] ) -> bool: """simple docstring""" if not (isinstance(__magic_name__ , (list, tuple) ) and len(__magic_name__ ) == 2): return False elif not (0 <= loc[0] < self.row and 0 <= loc[1] < self.column): return False else: return True def __getitem__( self : int , __magic_name__ : tuple[int, int] ) -> Any: """simple docstring""" assert self.validate_indicies(__magic_name__ ) return self.array[loc[0]][loc[1]] def __setitem__( self : List[str] , __magic_name__ : tuple[int, int] , __magic_name__ : float ) -> None: """simple docstring""" assert self.validate_indicies(__magic_name__ ) __snake_case : Optional[int] = value def __add__( self : Any , __magic_name__ : Matrix ) -> Matrix: """simple docstring""" assert isinstance(__magic_name__ , __magic_name__ ) assert self.row == another.row and self.column == another.column # Add __snake_case : Union[str, Any] = Matrix(self.row , self.column ) for r in range(self.row ): for c in range(self.column ): __snake_case : List[Any] = self[r, c] + another[r, c] return result def __neg__( self : Tuple ) -> Matrix: """simple docstring""" __snake_case : Tuple = Matrix(self.row , self.column ) for r in range(self.row ): for c in range(self.column ): __snake_case : List[Any] = -self[r, c] return result def __sub__( self : Optional[int] , __magic_name__ : Matrix ) -> Matrix: """simple docstring""" return self + (-another) def __mul__( self : List[Any] , __magic_name__ : int | float | Matrix ) -> Matrix: """simple docstring""" if isinstance(__magic_name__ , (int, float) ): # Scalar multiplication __snake_case : Optional[int] = Matrix(self.row , self.column ) for r in range(self.row ): for c in range(self.column ): __snake_case : Tuple = self[r, c] * another return result elif isinstance(__magic_name__ , __magic_name__ ): # Matrix multiplication assert self.column == another.row __snake_case : Dict = Matrix(self.row , another.column ) for r in range(self.row ): for c in range(another.column ): for i in range(self.column ): result[r, c] += self[r, i] * another[i, c] return result else: __snake_case : Optional[int] = f'''Unsupported type given for another ({type(__magic_name__ )})''' raise TypeError(__magic_name__ ) def lowercase__ ( self : str ) -> Matrix: """simple docstring""" __snake_case : Any = Matrix(self.column , self.row ) for r in range(self.row ): for c in range(self.column ): __snake_case : str = self[r, c] return result def lowercase__ ( self : Union[str, Any] , __magic_name__ : Matrix , __magic_name__ : Matrix ) -> Any: """simple docstring""" assert isinstance(__magic_name__ , __magic_name__ ) and isinstance(__magic_name__ , __magic_name__ ) assert self.row == self.column == u.row == v.row # u, v should be column vector assert u.column == v.column == 1 # u, v should be column vector # Calculate __snake_case : List[str] = v.transpose() __snake_case : Tuple = (v_t * self * u)[0, 0] + 1 if numerator_factor == 0: return None # It's not invertable return self - ((self * u) * (v_t * self) * (1.0 / numerator_factor)) # Testing if __name__ == "__main__": def _a ( ) -> None: """simple docstring""" __snake_case : Tuple = Matrix(3 , 3 , 0 ) for i in range(3 ): __snake_case : Any = 1 print(F'''a^(-1) is {ainv}''' ) # u, v __snake_case : Dict = Matrix(3 , 1 , 0 ) __snake_case , __snake_case , __snake_case : Union[str, Any] = 1, 2, -3 __snake_case : str = Matrix(3 , 1 , 0 ) __snake_case , __snake_case , __snake_case : Tuple = 4, -2, 5 print(F'''u is {u}''' ) print(F'''v is {v}''' ) print(F'''uv^T is {u * v.transpose()}''' ) # Sherman Morrison print(F'''(a + uv^T)^(-1) is {ainv.sherman_morrison(_lowerCamelCase , _lowerCamelCase )}''' ) def _a ( ) -> None: """simple docstring""" import doctest doctest.testmod() testa()
26
0
import os import tempfile import unittest import uuid from pathlib import Path from transformers.testing_utils import get_tests_dir, require_soundfile, require_torch, require_vision from transformers.tools.agent_types import AgentAudio, AgentImage, AgentText from transformers.utils import is_soundfile_availble, is_torch_available, is_vision_available if is_torch_available(): import torch if is_soundfile_availble(): import soundfile as sf if is_vision_available(): from PIL import Image def __SCREAMING_SNAKE_CASE ( a__ : Dict="" ) -> str: __A : Optional[Any] = tempfile.mkdtemp() return os.path.join(_lowerCamelCase ,str(uuid.uuida() ) + suffix ) @require_soundfile @require_torch class lowerCamelCase_ ( unittest.TestCase ): def lowerCAmelCase_ ( self : Optional[Any] ): __A : Optional[int] = torch.rand(12 , dtype=torch.floataa ) - 0.5 __A : Tuple = AgentAudio(__A ) __A : List[str] = str(agent_type.to_string() ) # Ensure that the tensor and the agent_type's tensor are the same self.assertTrue(torch.allclose(__A , agent_type.to_raw() , atol=1e-4 ) ) del agent_type # Ensure the path remains even after the object deletion self.assertTrue(os.path.exists(__A ) ) # Ensure that the file contains the same value as the original tensor __A : Optional[Any] = sf.read(__A ) self.assertTrue(torch.allclose(__A , torch.tensor(__A ) , atol=1e-4 ) ) def lowerCAmelCase_ ( self : Tuple ): __A : Optional[int] = torch.rand(12 , dtype=torch.floataa ) - 0.5 __A : Tuple = get_new_path(suffix=""".wav""" ) sf.write(__A , __A , 1_6000 ) __A : int = AgentAudio(__A ) self.assertTrue(torch.allclose(__A , agent_type.to_raw() , atol=1e-4 ) ) self.assertEqual(agent_type.to_string() , __A ) @require_vision @require_torch class lowerCamelCase_ ( unittest.TestCase ): def lowerCAmelCase_ ( self : Tuple ): __A : int = torch.randint(0 , 256 , (64, 64, 3) ) __A : List[str] = AgentImage(__A ) __A : List[str] = str(agent_type.to_string() ) # Ensure that the tensor and the agent_type's tensor are the same self.assertTrue(torch.allclose(__A , agent_type._tensor , atol=1e-4 ) ) self.assertIsInstance(agent_type.to_raw() , Image.Image ) # Ensure the path remains even after the object deletion del agent_type self.assertTrue(os.path.exists(__A ) ) def lowerCAmelCase_ ( self : Union[str, Any] ): __A : Optional[Any] = Path(get_tests_dir("""fixtures/tests_samples/COCO""" ) ) / """000000039769.png""" __A : Tuple = Image.open(__A ) __A : Optional[int] = AgentImage(__A ) self.assertTrue(path.samefile(agent_type.to_string() ) ) self.assertTrue(image == agent_type.to_raw() ) # Ensure the path remains even after the object deletion del agent_type self.assertTrue(os.path.exists(__A ) ) def lowerCAmelCase_ ( self : Optional[int] ): __A : Optional[Any] = Path(get_tests_dir("""fixtures/tests_samples/COCO""" ) ) / """000000039769.png""" __A : List[str] = Image.open(__A ) __A : Union[str, Any] = AgentImage(__A ) self.assertFalse(path.samefile(agent_type.to_string() ) ) self.assertTrue(image == agent_type.to_raw() ) # Ensure the path remains even after the object deletion del agent_type self.assertTrue(os.path.exists(__A ) ) class lowerCamelCase_ ( unittest.TestCase ): def lowerCAmelCase_ ( self : Optional[Any] ): __A : List[str] = """Hey!""" __A : Optional[Any] = AgentText(__A ) self.assertEqual(__A , agent_type.to_string() ) self.assertEqual(__A , agent_type.to_raw() ) self.assertEqual(__A , __A )
17
'''simple docstring''' import argparse import json import os import torch from torch import nn from transformers import NllbMoeConfig, NllbMoeModel from transformers.modeling_utils import dtype_byte_size from transformers.utils import WEIGHTS_INDEX_NAME, WEIGHTS_NAME def _a ( _lowerCamelCase ) -> List[Any]: """simple docstring""" __snake_case : Union[str, Any] = [ """encoder.version""", """decoder.version""", """model.encoder.version""", """model.decoder.version""", """decoder.output_projection.weight""", """_float_tensor""", """encoder.embed_positions._float_tensor""", """decoder.embed_positions._float_tensor""", ] for k in ignore_keys: state_dict.pop(_lowerCamelCase , _lowerCamelCase ) def _a ( _lowerCamelCase ) -> List[str]: """simple docstring""" __snake_case , __snake_case : Dict = emb.weight.shape __snake_case : Optional[int] = nn.Linear(_lowerCamelCase , _lowerCamelCase , bias=_lowerCamelCase ) __snake_case : Union[str, Any] = emb.weight.data return lin_layer def _a ( _lowerCamelCase , _lowerCamelCase=None ) -> Union[str, Any]: """simple docstring""" __snake_case : Any = {} for old_key in state_dict.keys(): __snake_case : Union[str, Any] = old_key if "moe_layer.experts." in key: if expert_idx is not None: __snake_case : Tuple = key.replace("""moe_layer.experts.0""" , F'''ffn.experts.expert_{expert_idx}''' ) else: __snake_case : Optional[int] = key.replace("""moe_layer.experts.""" , """ffn.experts.expert_""" ) if "gate" in key: __snake_case : Dict = key.replace(""".moe_layer.gate.wg""" , """.ffn.router.classifier""" ) if "fc2" and "experts" not in key: __snake_case : Union[str, Any] = key.replace(""".fc2.""" , """.ffn.fc2.""" ) if "fc1" and "experts" not in key: __snake_case : Optional[int] = key.replace(""".fc1.""" , """.ffn.fc1.""" ) if ".encoder_attn." in key: __snake_case : Tuple = key.replace(""".encoder_attn.""" , """.cross_attention.""" ) if "encoder_attn_layer_norm" in key: __snake_case : Union[str, Any] = key.replace("""encoder_attn_layer_norm""" , """cross_attention_layer_norm""" ) if "final_layer_norm" in key: __snake_case : str = key.replace("""final_layer_norm""" , """ff_layer_norm""" ) __snake_case : str = state_dict[old_key] return new_dict def _a ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase = WEIGHTS_NAME ) -> Dict: """simple docstring""" __snake_case : Optional[int] = [] __snake_case : Dict = 0 os.makedirs(_lowerCamelCase , exist_ok=_lowerCamelCase ) for expert in range(_lowerCamelCase ): __snake_case : Tuple = switch_checkpoint_path + F'''-rank-{expert}.pt''' if os.path.isfile(_lowerCamelCase ): __snake_case : Dict = torch.load(_lowerCamelCase )["""model"""] remove_ignore_keys_(_lowerCamelCase ) __snake_case : Optional[Any] = rename_fairseq_keys(_lowerCamelCase , _lowerCamelCase ) __snake_case : List[Any] = os.path.join( _lowerCamelCase , weights_name.replace(""".bin""" , F'''-{len(_lowerCamelCase )+1:05d}-of-???.bin''' ) ) torch.save(_lowerCamelCase , _lowerCamelCase ) sharded_state_dicts.append(expert_state.keys() ) total_size += sum([value.numel() for key, value in expert_state.items()] ) * dtype_byte_size( expert_state[list(_lowerCamelCase )[0]].dtype ) # Add the last block __snake_case : Optional[Any] = os.path.join(_lowerCamelCase , weights_name.replace(""".bin""" , F'''-{len(_lowerCamelCase )+1:05d}-of-???.bin''' ) ) __snake_case : str = torch.load(switch_checkpoint_path + """-shared.pt""" )["""model"""] remove_ignore_keys_(_lowerCamelCase ) __snake_case : Optional[Any] = rename_fairseq_keys(_lowerCamelCase , _lowerCamelCase ) __snake_case : List[str] = shared_weights["""decoder.embed_tokens.weight"""] sharded_state_dicts.append(shared_weights.keys() ) # If we only have the shared weights (dummy model/experts saved on the same file) if len(_lowerCamelCase ) == 1: __snake_case : Optional[Any] = os.path.join(_lowerCamelCase , _lowerCamelCase ) torch.save(_lowerCamelCase , _lowerCamelCase ) return {weights_name: sharded_state_dicts[0]}, None else: torch.save(_lowerCamelCase , _lowerCamelCase ) # Otherwise, let's build the index __snake_case : Tuple = {} for idx, shard in enumerate(_lowerCamelCase ): __snake_case : Any = weights_name.replace(""".bin""" , F'''-{idx+1:05d}-of-{len(_lowerCamelCase ):05d}.bin''' ) __snake_case : int = os.path.join(_lowerCamelCase , weights_name.replace(""".bin""" , F'''-{idx+1:05d}-of-???.bin''' ) ) os.rename(_lowerCamelCase , os.path.join(_lowerCamelCase , _lowerCamelCase ) ) for key in shard: __snake_case : str = shard_file # Add the metadata __snake_case : Optional[Any] = {"""total_size""": total_size} __snake_case : int = {"""metadata""": metadata, """weight_map""": weight_map} with open(os.path.join(_lowerCamelCase , _lowerCamelCase ) , """w""" , encoding="""utf-8""" ) as f: __snake_case : Union[str, Any] = json.dumps(_lowerCamelCase , indent=2 , sort_keys=_lowerCamelCase ) + """\n""" f.write(_lowerCamelCase ) return metadata, index if __name__ == "__main__": __UpperCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( "--nllb_moe_checkpoint_path", default="/home/arthur_huggingface_co/fairseq/weights/checkpoints/model_moe_54b/checkpoint_2_300000", type=str, required=False, help="Path to a directory containing a folder per layer. Follows the original Google format.", ) parser.add_argument("--dtype", default="float32", type=str, required=False, help="dtype of the saved model") parser.add_argument( "--pytorch_dump_folder_path", default="/home/arthur_huggingface_co/fairseq/weights/checkpoints/hf-converted-moe-54b", type=str, required=False, help="Path to the output pytorch model.", ) __UpperCamelCase = parser.parse_args() __UpperCamelCase , __UpperCamelCase = shard_on_the_fly( args.nllb_moe_checkpoint_path, args.pytorch_dump_folder_path, 128, args.dtype, ) __UpperCamelCase = NllbMoeConfig.from_pretrained( "facebook/nllb-200-3.3B", encoder_sparse_step=4, decoder_sparse_step=4, num_experts=128 ) config.save_pretrained(args.pytorch_dump_folder_path) __UpperCamelCase = NllbMoeModel.from_pretrained(args.pytorch_dump_folder_path) print("Done") model.save_pretrained(args.pytorch_dump_folder_path)
26
0
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available _A : List[Any] = { """configuration_data2vec_audio""": ["""DATA2VEC_AUDIO_PRETRAINED_CONFIG_ARCHIVE_MAP""", """Data2VecAudioConfig"""], """configuration_data2vec_text""": [ """DATA2VEC_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """Data2VecTextConfig""", """Data2VecTextOnnxConfig""", ], """configuration_data2vec_vision""": [ """DATA2VEC_VISION_PRETRAINED_CONFIG_ARCHIVE_MAP""", """Data2VecVisionConfig""", """Data2VecVisionOnnxConfig""", ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _A : List[Any] = [ """DATA2VEC_AUDIO_PRETRAINED_MODEL_ARCHIVE_LIST""", """Data2VecAudioForAudioFrameClassification""", """Data2VecAudioForCTC""", """Data2VecAudioForSequenceClassification""", """Data2VecAudioForXVector""", """Data2VecAudioModel""", """Data2VecAudioPreTrainedModel""", ] _A : str = [ """DATA2VEC_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST""", """Data2VecTextForCausalLM""", """Data2VecTextForMaskedLM""", """Data2VecTextForMultipleChoice""", """Data2VecTextForQuestionAnswering""", """Data2VecTextForSequenceClassification""", """Data2VecTextForTokenClassification""", """Data2VecTextModel""", """Data2VecTextPreTrainedModel""", ] _A : Dict = [ """DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST""", """Data2VecVisionForImageClassification""", """Data2VecVisionForMaskedImageModeling""", """Data2VecVisionForSemanticSegmentation""", """Data2VecVisionModel""", """Data2VecVisionPreTrainedModel""", ] if is_tf_available(): _A : Optional[int] = [ """TFData2VecVisionForImageClassification""", """TFData2VecVisionForSemanticSegmentation""", """TFData2VecVisionModel""", """TFData2VecVisionPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_dataavec_audio import DATA2VEC_AUDIO_PRETRAINED_CONFIG_ARCHIVE_MAP, DataaVecAudioConfig from .configuration_dataavec_text import ( DATA2VEC_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP, DataaVecTextConfig, DataaVecTextOnnxConfig, ) from .configuration_dataavec_vision import ( DATA2VEC_VISION_PRETRAINED_CONFIG_ARCHIVE_MAP, DataaVecVisionConfig, DataaVecVisionOnnxConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_dataavec_audio import ( DATA2VEC_AUDIO_PRETRAINED_MODEL_ARCHIVE_LIST, DataaVecAudioForAudioFrameClassification, DataaVecAudioForCTC, DataaVecAudioForSequenceClassification, DataaVecAudioForXVector, DataaVecAudioModel, DataaVecAudioPreTrainedModel, ) from .modeling_dataavec_text import ( DATA2VEC_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST, DataaVecTextForCausalLM, DataaVecTextForMaskedLM, DataaVecTextForMultipleChoice, DataaVecTextForQuestionAnswering, DataaVecTextForSequenceClassification, DataaVecTextForTokenClassification, DataaVecTextModel, DataaVecTextPreTrainedModel, ) from .modeling_dataavec_vision import ( DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST, DataaVecVisionForImageClassification, DataaVecVisionForMaskedImageModeling, DataaVecVisionForSemanticSegmentation, DataaVecVisionModel, DataaVecVisionPreTrainedModel, ) if is_tf_available(): from .modeling_tf_dataavec_vision import ( TFDataaVecVisionForImageClassification, TFDataaVecVisionForSemanticSegmentation, TFDataaVecVisionModel, TFDataaVecVisionPreTrainedModel, ) else: import sys _A : Tuple = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
361
'''simple docstring''' import cva import numpy as np class _A : def __init__( self : Any , __magic_name__ : float , __magic_name__ : int ) -> Optional[int]: """simple docstring""" if k in (0.04, 0.06): __snake_case : List[str] = k __snake_case : int = window_size else: raise ValueError("""invalid k value""" ) def __str__( self : Union[str, Any] ) -> str: """simple docstring""" return str(self.k ) def lowercase__ ( self : Dict , __magic_name__ : str ) -> tuple[cva.Mat, list[list[int]]]: """simple docstring""" __snake_case : Dict = cva.imread(__magic_name__ , 0 ) __snake_case , __snake_case : List[str] = img.shape __snake_case : list[list[int]] = [] __snake_case : str = img.copy() __snake_case : Tuple = cva.cvtColor(__magic_name__ , cva.COLOR_GRAY2RGB ) __snake_case , __snake_case : List[Any] = np.gradient(__magic_name__ ) __snake_case : Optional[Any] = dx**2 __snake_case : Tuple = dy**2 __snake_case : List[Any] = dx * dy __snake_case : List[Any] = 0.04 __snake_case : Tuple = self.window_size // 2 for y in range(__magic_name__ , h - offset ): for x in range(__magic_name__ , w - offset ): __snake_case : Dict = ixx[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() __snake_case : Optional[int] = iyy[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() __snake_case : str = ixy[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() __snake_case : List[str] = (wxx * wyy) - (wxy**2) __snake_case : Dict = wxx + wyy __snake_case : List[str] = det - k * (trace**2) # Can change the value if r > 0.5: corner_list.append([x, y, r] ) color_img.itemset((y, x, 0) , 0 ) color_img.itemset((y, x, 1) , 0 ) color_img.itemset((y, x, 2) , 2_55 ) return color_img, corner_list if __name__ == "__main__": __UpperCamelCase = HarrisCorner(0.04, 3) __UpperCamelCase , __UpperCamelCase = edge_detect.detect("path_to_image") cva.imwrite("detect.png", color_img)
26
0
import logging import torch from torch import nn from torch.nn import CrossEntropyLoss, MSELoss from transformers.file_utils import add_start_docstrings, add_start_docstrings_to_model_forward from transformers.models.bert.modeling_bert import ( BERT_INPUTS_DOCSTRING, BERT_START_DOCSTRING, BertEncoder, BertModel, BertPreTrainedModel, ) lowerCamelCase : Union[str, Any] = logging.getLogger(__name__) class A( __lowercase ): '''simple docstring''' def a__ ( self : int , A_ : Dict , A_ : Any , A_ : Union[str, Any]=None , A_ : Optional[Any]=None ) -> str: """simple docstring""" lowerCamelCase_ = self.layer[current_layer](A_ , A_ , head_mask[current_layer] ) lowerCamelCase_ = layer_outputs[0] return hidden_states @add_start_docstrings( '''The bare Bert Model transformer with PABEE outputting raw hidden-states without any specific head on top.''' , __lowercase , ) class A( __lowercase ): '''simple docstring''' def __init__( self : Optional[Any] , A_ : str ) -> Dict: """simple docstring""" super().__init__(A_ ) lowerCamelCase_ = BertEncoderWithPabee(A_ ) self.init_weights() lowerCamelCase_ = 0 lowerCamelCase_ = 0 lowerCamelCase_ = 0 lowerCamelCase_ = 0 def a__ ( self : Union[str, Any] , A_ : Tuple ) -> Dict: """simple docstring""" lowerCamelCase_ = threshold def a__ ( self : List[str] , A_ : List[Any] ) -> Tuple: """simple docstring""" lowerCamelCase_ = patience def a__ ( self : Optional[Any] ) -> Tuple: """simple docstring""" lowerCamelCase_ = 0 lowerCamelCase_ = 0 def a__ ( self : Union[str, Any] ) -> int: """simple docstring""" lowerCamelCase_ = self.inference_layers_num / self.inference_instances_num lowerCamelCase_ = ( f"""*** Patience = {self.patience} Avg. Inference Layers = {avg_inf_layers:.2f} Speed Up =""" f""" {1 - avg_inf_layers / self.config.num_hidden_layers:.2f} ***""" ) print(A_ ) @add_start_docstrings_to_model_forward(A_ ) def a__ ( self : List[Any] , A_ : List[str]=None , A_ : List[Any]=None , A_ : Optional[int]=None , A_ : Optional[Any]=None , A_ : int=None , A_ : Optional[int]=None , A_ : Optional[Any]=None , A_ : Optional[Any]=None , A_ : List[str]=None , A_ : Optional[Any]=None , A_ : Optional[int]=False , ) -> str: """simple docstring""" if input_ids is not None and inputs_embeds is not None: raise ValueError('You cannot specify both input_ids and inputs_embeds at the same time' ) elif input_ids is not None: lowerCamelCase_ = input_ids.size() elif inputs_embeds is not None: lowerCamelCase_ = inputs_embeds.size()[:-1] else: raise ValueError('You have to specify either input_ids or inputs_embeds' ) lowerCamelCase_ = input_ids.device if input_ids is not None else inputs_embeds.device if attention_mask is None: lowerCamelCase_ = torch.ones(A_ , device=A_ ) if token_type_ids is None: lowerCamelCase_ = torch.zeros(A_ , dtype=torch.long , device=A_ ) # We can provide a self-attention mask of dimensions [batch_size, from_seq_length, to_seq_length] # ourselves in which case we just need to make it broadcastable to all heads. lowerCamelCase_ = self.get_extended_attention_mask(A_ , A_ , A_ ) # If a 2D ou 3D attention mask is provided for the cross-attention # we need to make broadcastable to [batch_size, num_heads, seq_length, seq_length] if self.config.is_decoder and encoder_hidden_states is not None: lowerCamelCase_ = encoder_hidden_states.size() lowerCamelCase_ = (encoder_batch_size, encoder_sequence_length) if encoder_attention_mask is None: lowerCamelCase_ = torch.ones(A_ , device=A_ ) lowerCamelCase_ = self.invert_attention_mask(A_ ) else: lowerCamelCase_ = None # Prepare head mask if needed # 1.0 in head_mask indicate we keep the head # attention_probs has shape bsz x n_heads x N x N # input head_mask has shape [num_heads] or [num_hidden_layers x num_heads] # and head_mask is converted to shape [num_hidden_layers x batch x num_heads x seq_length x seq_length] lowerCamelCase_ = self.get_head_mask(A_ , self.config.num_hidden_layers ) lowerCamelCase_ = self.embeddings( input_ids=A_ , position_ids=A_ , token_type_ids=A_ , inputs_embeds=A_ ) lowerCamelCase_ = embedding_output if self.training: lowerCamelCase_ = [] for i in range(self.config.num_hidden_layers ): lowerCamelCase_ = self.encoder.adaptive_forward( A_ , current_layer=A_ , attention_mask=A_ , head_mask=A_ ) lowerCamelCase_ = self.pooler(A_ ) lowerCamelCase_ = output_layers[i](output_dropout(A_ ) ) res.append(A_ ) elif self.patience == 0: # Use all layers for inference lowerCamelCase_ = self.encoder( A_ , attention_mask=A_ , head_mask=A_ , encoder_hidden_states=A_ , encoder_attention_mask=A_ , ) lowerCamelCase_ = self.pooler(encoder_outputs[0] ) lowerCamelCase_ = [output_layers[self.config.num_hidden_layers - 1](A_ )] else: lowerCamelCase_ = 0 lowerCamelCase_ = None lowerCamelCase_ = 0 for i in range(self.config.num_hidden_layers ): calculated_layer_num += 1 lowerCamelCase_ = self.encoder.adaptive_forward( A_ , current_layer=A_ , attention_mask=A_ , head_mask=A_ ) lowerCamelCase_ = self.pooler(A_ ) lowerCamelCase_ = output_layers[i](A_ ) if regression: lowerCamelCase_ = logits.detach() if patient_result is not None: lowerCamelCase_ = patient_result.detach() if (patient_result is not None) and torch.abs(patient_result - labels ) < self.regression_threshold: patient_counter += 1 else: lowerCamelCase_ = 0 else: lowerCamelCase_ = logits.detach().argmax(dim=1 ) if patient_result is not None: lowerCamelCase_ = patient_result.detach().argmax(dim=1 ) if (patient_result is not None) and torch.all(labels.eq(A_ ) ): patient_counter += 1 else: lowerCamelCase_ = 0 lowerCamelCase_ = logits if patient_counter == self.patience: break lowerCamelCase_ = [patient_result] self.inference_layers_num += calculated_layer_num self.inference_instances_num += 1 return res @add_start_docstrings( '''Bert Model transformer with PABEE and a sequence classification/regression head on top (a linear layer on top of the pooled output) e.g. for GLUE tasks. ''' , __lowercase , ) class A( __lowercase ): '''simple docstring''' def __init__( self : Union[str, Any] , A_ : str ) -> Any: """simple docstring""" super().__init__(A_ ) lowerCamelCase_ = config.num_labels lowerCamelCase_ = BertModelWithPabee(A_ ) lowerCamelCase_ = nn.Dropout(config.hidden_dropout_prob ) lowerCamelCase_ = nn.ModuleList( [nn.Linear(config.hidden_size , self.config.num_labels ) for _ in range(config.num_hidden_layers )] ) self.init_weights() @add_start_docstrings_to_model_forward(A_ ) def a__ ( self : List[str] , A_ : Dict=None , A_ : str=None , A_ : Union[str, Any]=None , A_ : str=None , A_ : Dict=None , A_ : str=None , A_ : Tuple=None , ) -> Optional[Any]: """simple docstring""" lowerCamelCase_ = self.bert( input_ids=A_ , attention_mask=A_ , token_type_ids=A_ , position_ids=A_ , head_mask=A_ , inputs_embeds=A_ , output_dropout=self.dropout , output_layers=self.classifiers , regression=self.num_labels == 1 , ) lowerCamelCase_ = (logits[-1],) if labels is not None: lowerCamelCase_ = None lowerCamelCase_ = 0 for ix, logits_item in enumerate(A_ ): if self.num_labels == 1: # We are doing regression lowerCamelCase_ = MSELoss() lowerCamelCase_ = loss_fct(logits_item.view(-1 ) , labels.view(-1 ) ) else: lowerCamelCase_ = CrossEntropyLoss() lowerCamelCase_ = loss_fct(logits_item.view(-1 , self.num_labels ) , labels.view(-1 ) ) if total_loss is None: lowerCamelCase_ = loss else: total_loss += loss * (ix + 1) total_weights += ix + 1 lowerCamelCase_ = (total_loss / total_weights,) + outputs return outputs
70
'''simple docstring''' import warnings from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class _A ( __lowercase ): lowercase__: Any = ['''image_processor''', '''tokenizer'''] lowercase__: Any = '''CLIPImageProcessor''' lowercase__: Optional[Any] = ('''CLIPTokenizer''', '''CLIPTokenizerFast''') def __init__( self : int , __magic_name__ : Dict=None , __magic_name__ : Dict=None , **__magic_name__ : Union[str, Any] ) -> Any: """simple docstring""" __snake_case : Optional[Any] = None if "feature_extractor" in kwargs: warnings.warn( """The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`""" """ instead.""" , __magic_name__ , ) __snake_case : List[Any] = kwargs.pop("""feature_extractor""" ) __snake_case : List[str] = 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__(__magic_name__ , __magic_name__ ) def __call__( self : int , __magic_name__ : List[str]=None , __magic_name__ : Tuple=None , __magic_name__ : Any=None , **__magic_name__ : Union[str, Any] ) -> Optional[int]: """simple docstring""" 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: __snake_case : int = self.tokenizer(__magic_name__ , return_tensors=__magic_name__ , **__magic_name__ ) if images is not None: __snake_case : str = self.image_processor(__magic_name__ , return_tensors=__magic_name__ , **__magic_name__ ) if text is not None and images is not None: __snake_case : Union[str, Any] = image_features.pixel_values return encoding elif text is not None: return encoding else: return BatchEncoding(data=dict(**__magic_name__ ) , tensor_type=__magic_name__ ) def lowercase__ ( self : Optional[int] , *__magic_name__ : List[Any] , **__magic_name__ : Any ) -> Optional[Any]: """simple docstring""" return self.tokenizer.batch_decode(*__magic_name__ , **__magic_name__ ) def lowercase__ ( self : List[str] , *__magic_name__ : Tuple , **__magic_name__ : List[Any] ) -> int: """simple docstring""" return self.tokenizer.decode(*__magic_name__ , **__magic_name__ ) @property def lowercase__ ( self : List[Any] ) -> Optional[Any]: """simple docstring""" __snake_case : Dict = self.tokenizer.model_input_names __snake_case : str = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) ) @property def lowercase__ ( self : int ) -> List[str]: """simple docstring""" warnings.warn( """`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.""" , __magic_name__ , ) return self.image_processor_class @property def lowercase__ ( self : Union[str, Any] ) -> Dict: """simple docstring""" warnings.warn( """`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.""" , __magic_name__ , ) return self.image_processor
26
0
"""simple docstring""" from __future__ import annotations import math def _A (__a , __a ) -> float: """simple docstring""" SCREAMING_SNAKE_CASE_ : Any = u for i in range(1 , _lowerCamelCase ): SCREAMING_SNAKE_CASE_ : Optional[Any] = temp * (u - i) return temp def _A () -> None: """simple docstring""" SCREAMING_SNAKE_CASE_ : str = int(input('''enter the numbers of values: ''' ) ) SCREAMING_SNAKE_CASE_ : list[list[float]] = [] for _ in range(_lowerCamelCase ): y.append([] ) for i in range(_lowerCamelCase ): for j in range(_lowerCamelCase ): y[i].append(_lowerCamelCase ) SCREAMING_SNAKE_CASE_ : Any = 0 print('''enter the values of parameters in a list: ''' ) SCREAMING_SNAKE_CASE_ : Dict = list(map(_lowerCamelCase , input().split() ) ) print('''enter the values of corresponding parameters: ''' ) for i in range(_lowerCamelCase ): SCREAMING_SNAKE_CASE_ : Optional[int] = float(input() ) SCREAMING_SNAKE_CASE_ : List[str] = int(input('''enter the value to interpolate: ''' ) ) SCREAMING_SNAKE_CASE_ : int = (value - x[0]) / (x[1] - x[0]) # for calculating forward difference table for i in range(1 , _lowerCamelCase ): for j in range(n - i ): SCREAMING_SNAKE_CASE_ : List[Any] = y[j + 1][i - 1] - y[j][i - 1] SCREAMING_SNAKE_CASE_ : Union[str, Any] = y[0][0] for i in range(1 , _lowerCamelCase ): summ += (ucal(_lowerCamelCase , _lowerCamelCase ) * y[0][i]) / math.factorial(_lowerCamelCase ) print(f'the value at {value} is {summ}' ) if __name__ == "__main__": main()
512
'''simple docstring''' import datasets import faiss import numpy as np import streamlit as st import torch from elasticsearch import Elasticsearch from elia_utils import ( embed_questions_for_retrieval, make_qa_sas_model, qa_sas_generate, query_es_index, query_qa_dense_index, ) import transformers from transformers import AutoModel, AutoModelForSeqaSeqLM, AutoTokenizer __UpperCamelCase = "bart" __UpperCamelCase = True @st.cache(allow_output_mutation=_lowerCamelCase ) def _a ( ) -> Union[str, Any]: """simple docstring""" if LOAD_DENSE_INDEX: __snake_case : int = AutoTokenizer.from_pretrained("""yjernite/retribert-base-uncased""" ) __snake_case : Tuple = AutoModel.from_pretrained("""yjernite/retribert-base-uncased""" ).to("""cuda:0""" ) __snake_case : List[Any] = qar_model.eval() else: __snake_case , __snake_case : Optional[Any] = (None, None) if MODEL_TYPE == "bart": __snake_case : List[str] = AutoTokenizer.from_pretrained("""yjernite/bart_eli5""" ) __snake_case : Any = AutoModelForSeqaSeqLM.from_pretrained("""yjernite/bart_eli5""" ).to("""cuda:0""" ) __snake_case : int = torch.load("""seq2seq_models/eli5_bart_model_blm_2.pth""" ) sas_model.load_state_dict(save_dict["""model"""] ) __snake_case : int = sas_model.eval() else: __snake_case , __snake_case : Dict = make_qa_sas_model( model_name="""t5-small""" , from_file="""seq2seq_models/eli5_t5_model_1024_4.pth""" , device="""cuda:0""" ) return (qar_tokenizer, qar_model, sas_tokenizer, sas_model) @st.cache(allow_output_mutation=_lowerCamelCase ) def _a ( ) -> Tuple: """simple docstring""" if LOAD_DENSE_INDEX: __snake_case : Tuple = faiss.StandardGpuResources() __snake_case : Optional[Any] = datasets.load_dataset(path="""wiki_snippets""" , name="""wiki40b_en_100_0""" )["""train"""] __snake_case : str = np.memmap( """wiki40b_passages_reps_32_l-8_h-768_b-512-512.dat""" , dtype="""float32""" , mode="""r""" , shape=(wikiaab_passages.num_rows, 128) , ) __snake_case : Optional[int] = faiss.IndexFlatIP(128 ) __snake_case : Any = faiss.index_cpu_to_gpu(_lowerCamelCase , 1 , _lowerCamelCase ) wikiaab_gpu_index_flat.add(_lowerCamelCase ) # TODO fix for larger GPU else: __snake_case , __snake_case : Tuple = (None, None) __snake_case : List[str] = Elasticsearch([{"""host""": """localhost""", """port""": """9200"""}] ) return (wikiaab_passages, wikiaab_gpu_index_flat, es_client) @st.cache(allow_output_mutation=_lowerCamelCase ) def _a ( ) -> List[Any]: """simple docstring""" __snake_case : Tuple = datasets.load_dataset("""eli5""" , name="""LFQA_reddit""" ) __snake_case : Dict = elia["""train_eli5"""] __snake_case : int = np.memmap( """eli5_questions_reps.dat""" , dtype="""float32""" , mode="""r""" , shape=(elia_train.num_rows, 128) ) __snake_case : Dict = faiss.IndexFlatIP(128 ) eli5_train_q_index.add(_lowerCamelCase ) return (elia_train, eli5_train_q_index) __UpperCamelCase , __UpperCamelCase , __UpperCamelCase = load_indexes() __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase = load_models() __UpperCamelCase , __UpperCamelCase = load_train_data() def _a ( _lowerCamelCase , _lowerCamelCase=10 ) -> int: """simple docstring""" __snake_case : Optional[int] = embed_questions_for_retrieval([question] , _lowerCamelCase , _lowerCamelCase ) __snake_case , __snake_case : Tuple = eli5_train_q_index.search(_lowerCamelCase , _lowerCamelCase ) __snake_case : Tuple = [elia_train[int(_lowerCamelCase )] for i in I[0]] return nn_examples def _a ( _lowerCamelCase , _lowerCamelCase="wiki40b" , _lowerCamelCase="dense" , _lowerCamelCase=10 ) -> Optional[Any]: """simple docstring""" if source == "none": __snake_case , __snake_case : Dict = (""" <P> """.join(["""""" for _ in range(11 )] ).strip(), []) else: if method == "dense": __snake_case , __snake_case : Dict = query_qa_dense_index( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) else: __snake_case , __snake_case : str = query_es_index( _lowerCamelCase , _lowerCamelCase , index_name="""english_wiki40b_snippets_100w""" , n_results=_lowerCamelCase , ) __snake_case : Optional[int] = [ (res["""article_title"""], res["""section_title"""].strip(), res["""score"""], res["""passage_text"""]) for res in hit_lst ] __snake_case : Optional[Any] = """question: {} context: {}""".format(_lowerCamelCase , _lowerCamelCase ) return question_doc, support_list @st.cache( hash_funcs={ torch.Tensor: (lambda _lowerCamelCase : None), transformers.models.bart.tokenization_bart.BartTokenizer: (lambda _lowerCamelCase : None), } ) def _a ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase=64 , _lowerCamelCase=256 , _lowerCamelCase=False , _lowerCamelCase=2 , _lowerCamelCase=0.95 , _lowerCamelCase=0.8 ) -> List[str]: """simple docstring""" with torch.no_grad(): __snake_case : Union[str, Any] = qa_sas_generate( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , num_answers=1 , num_beams=_lowerCamelCase , min_len=_lowerCamelCase , max_len=_lowerCamelCase , do_sample=_lowerCamelCase , temp=_lowerCamelCase , top_p=_lowerCamelCase , top_k=_lowerCamelCase , max_input_length=1024 , device="""cuda:0""" , )[0] return (answer, support_list) st.title("Long Form Question Answering with ELI5") # Start sidebar __UpperCamelCase = "<img src='https://huggingface.co/front/assets/huggingface_logo.svg'>" __UpperCamelCase = "\n<html>\n <head>\n <style>\n .img-container {\n padding-left: 90px;\n padding-right: 90px;\n padding-top: 50px;\n padding-bottom: 50px;\n background-color: #f0f3f9;\n }\n </style>\n </head>\n <body>\n <span class=\"img-container\"> <!-- Inline parent element -->\n %s\n </span>\n </body>\n</html>\n" % ( header_html, ) st.sidebar.markdown( header_full, unsafe_allow_html=True, ) # Long Form QA with ELI5 and Wikipedia __UpperCamelCase = "\nThis demo presents a model trained to [provide long-form answers to open-domain questions](https://yjernite.github.io/lfqa.html).\nFirst, a document retriever fetches a set of relevant Wikipedia passages given the question from the [Wiki40b](https://research.google/pubs/pub49029/) dataset,\na pre-processed fixed snapshot of Wikipedia.\n" st.sidebar.markdown(description, unsafe_allow_html=True) __UpperCamelCase = [ "Answer the question", "View the retrieved document only", "View the most similar ELI5 question and answer", "Show me everything, please!", ] __UpperCamelCase = st.sidebar.checkbox("Demo options") if demo_options: __UpperCamelCase = st.sidebar.selectbox( "", action_list, index=3, ) __UpperCamelCase = action_list.index(action_st) __UpperCamelCase = st.sidebar.selectbox( "", ["Show full text of passages", "Show passage section titles"], index=0, ) __UpperCamelCase = show_type == "Show full text of passages" else: __UpperCamelCase = 3 __UpperCamelCase = True __UpperCamelCase = st.sidebar.checkbox("Retrieval options") if retrieval_options: __UpperCamelCase = "\n ### Information retriever options\n\n The **sparse** retriever uses ElasticSearch, while the **dense** retriever uses max-inner-product search between a question and passage embedding\n trained using the [ELI5](https://arxiv.org/abs/1907.09190) questions-answer pairs.\n The answer is then generated by sequence to sequence model which takes the question and retrieved document as input.\n " st.sidebar.markdown(retriever_info) __UpperCamelCase = st.sidebar.selectbox("Which Wikipedia format should the model use?", ["wiki40b", "none"]) __UpperCamelCase = st.sidebar.selectbox("Which Wikipedia indexer should the model use?", ["dense", "sparse", "mixed"]) else: __UpperCamelCase = "wiki40b" __UpperCamelCase = "dense" __UpperCamelCase = "beam" __UpperCamelCase = 2 __UpperCamelCase = 64 __UpperCamelCase = 256 __UpperCamelCase = None __UpperCamelCase = None __UpperCamelCase = st.sidebar.checkbox("Generation options") if generate_options: __UpperCamelCase = "\n ### Answer generation options\n\n The sequence-to-sequence model was initialized with [BART](https://huggingface.co/facebook/bart-large)\n weights and fine-tuned on the ELI5 QA pairs and retrieved documents. You can use the model for greedy decoding with\n **beam** search, or **sample** from the decoder's output probabilities.\n " st.sidebar.markdown(generate_info) __UpperCamelCase = st.sidebar.selectbox("Would you like to use beam search or sample an answer?", ["beam", "sampled"]) __UpperCamelCase = st.sidebar.slider( "Minimum generation length", min_value=8, max_value=256, value=64, step=8, format=None, key=None ) __UpperCamelCase = st.sidebar.slider( "Maximum generation length", min_value=64, max_value=512, value=256, step=16, format=None, key=None ) if sampled == "beam": __UpperCamelCase = st.sidebar.slider("Beam size", min_value=1, max_value=8, value=2, step=None, format=None, key=None) else: __UpperCamelCase = st.sidebar.slider( "Nucleus sampling p", min_value=0.1, max_value=1.0, value=0.95, step=0.01, format=None, key=None ) __UpperCamelCase = st.sidebar.slider( "Temperature", min_value=0.1, max_value=1.0, value=0.7, step=0.01, format=None, key=None ) __UpperCamelCase = None # start main text __UpperCamelCase = [ "<MY QUESTION>", "How do people make chocolate?", "Why do we get a fever when we are sick?", "How can different animals perceive different colors?", "What is natural language processing?", "What's the best way to treat a sunburn?", "What exactly are vitamins ?", "How does nuclear energy provide electricity?", "What's the difference between viruses and bacteria?", "Why are flutes classified as woodwinds when most of them are made out of metal ?", "Why do people like drinking coffee even though it tastes so bad?", "What happens when wine ages? How does it make the wine taste better?", "If an animal is an herbivore, where does it get the protein that it needs to survive if it only eats grass?", "How can we set a date to the beginning or end of an artistic period? Doesn't the change happen gradually?", "How does New Zealand have so many large bird predators?", ] __UpperCamelCase = st.selectbox( "What would you like to ask? ---- select <MY QUESTION> to enter a new query", questions_list, index=1, ) if question_s == "<MY QUESTION>": __UpperCamelCase = st.text_input("Enter your question here:", "") else: __UpperCamelCase = question_s if st.button("Show me!"): if action in [0, 1, 3]: if index_type == "mixed": __UpperCamelCase , __UpperCamelCase = make_support(question, source=wiki_source, method="dense", n_results=10) __UpperCamelCase , __UpperCamelCase = make_support(question, source=wiki_source, method="sparse", n_results=10) __UpperCamelCase = [] for res_d, res_s in zip(support_list_dense, support_list_sparse): if tuple(res_d) not in support_list: support_list += [tuple(res_d)] if tuple(res_s) not in support_list: support_list += [tuple(res_s)] __UpperCamelCase = support_list[:10] __UpperCamelCase = "<P> " + " <P> ".join([res[-1] for res in support_list]) else: __UpperCamelCase , __UpperCamelCase = make_support(question, source=wiki_source, method=index_type, n_results=10) if action in [0, 3]: __UpperCamelCase , __UpperCamelCase = answer_question( question_doc, sas_model, sas_tokenizer, min_len=min_len, max_len=int(max_len), sampling=(sampled == "sampled"), n_beams=n_beams, top_p=top_p, temp=temp, ) st.markdown("### The model generated answer is:") st.write(answer) if action in [0, 1, 3] and wiki_source != "none": st.markdown("--- \n ### The model is drawing information from the following Wikipedia passages:") for i, res in enumerate(support_list): __UpperCamelCase = "https://en.wikipedia.org/wiki/{}".format(res[0].replace(" ", "_")) __UpperCamelCase = res[1].strip() if sec_titles == "": __UpperCamelCase = "[{}]({})".format(res[0], wiki_url) else: __UpperCamelCase = sec_titles.split(" & ") __UpperCamelCase = " & ".join( ["[{}]({}#{})".format(sec.strip(), wiki_url, sec.strip().replace(" ", "_")) for sec in sec_list] ) st.markdown( "{0:02d} - **Article**: {1:<18} <br> _Section_: {2}".format(i + 1, res[0], sections), unsafe_allow_html=True, ) if show_passages: st.write( "> <span style=\"font-family:arial; font-size:10pt;\">" + res[-1] + "</span>", unsafe_allow_html=True ) if action in [2, 3]: __UpperCamelCase = find_nearest_training(question) __UpperCamelCase = nn_train_list[0] st.markdown( "--- \n ### The most similar question in the ELI5 training set was: \n\n {}".format(train_exple["title"]) ) __UpperCamelCase = [ "{}. {}".format(i + 1, " \n".join([line.strip() for line in ans.split("\n") if line.strip() != ""])) for i, (ans, sc) in enumerate(zip(train_exple["answers"]["text"], train_exple["answers"]["score"])) if i == 0 or sc > 2 ] st.markdown("##### Its answers were: \n\n {}".format("\n".join(answers_st))) __UpperCamelCase = "\n---\n\n**Disclaimer**\n\n*The intent of this app is to provide some (hopefully entertaining) insights into the behavior of a current LFQA system.\nEvaluating biases of such a model and ensuring factual generations are still very much open research problems.\nTherefore, until some significant progress is achieved, we caution against using the generated answers for practical purposes.*\n" st.sidebar.markdown(disclaimer, unsafe_allow_html=True)
26
0
from typing import List, Optional, Tuple, Union import torch from ...utils import logging, randn_tensor from ..pipeline_utils import AudioPipelineOutput, DiffusionPipeline A = logging.get_logger(__name__) # pylint: disable=invalid-name class lowercase__ ( __lowercase ): def __init__( self : Optional[Any] , _lowercase : Optional[Any] , _lowercase : int ): """simple docstring""" super().__init__() self.register_modules(unet=_lowercase , scheduler=_lowercase ) @torch.no_grad() def __call__( self : Any , _lowercase : int = 1 , _lowercase : int = 1_00 , _lowercase : Optional[Union[torch.Generator, List[torch.Generator]]] = None , _lowercase : Optional[float] = None , _lowercase : bool = True , ): """simple docstring""" if audio_length_in_s is None: UpperCAmelCase__ = self.unet.config.sample_size / self.unet.config.sample_rate UpperCAmelCase__ = audio_length_in_s * self.unet.config.sample_rate UpperCAmelCase__ = 2 ** len(self.unet.up_blocks ) if sample_size < 3 * down_scale_factor: raise ValueError( F"""{audio_length_in_s} is too small. Make sure it\'s bigger or equal to""" F""" {3 * down_scale_factor / self.unet.config.sample_rate}.""" ) UpperCAmelCase__ = int(_lowercase ) if sample_size % down_scale_factor != 0: UpperCAmelCase__ = ( (audio_length_in_s * self.unet.config.sample_rate) // down_scale_factor + 1 ) * down_scale_factor logger.info( F"""{audio_length_in_s} is increased to {sample_size / self.unet.config.sample_rate} so that it can be handled""" F""" by the model. It will be cut to {original_sample_size / self.unet.config.sample_rate} after the denoising""" " process." ) UpperCAmelCase__ = int(_lowercase ) UpperCAmelCase__ = next(iter(self.unet.parameters() ) ).dtype UpperCAmelCase__ = (batch_size, self.unet.config.in_channels, sample_size) if isinstance(_lowercase , _lowercase ) and len(_lowercase ) != batch_size: raise ValueError( F"""You have passed a list of generators of length {len(_lowercase )}, but requested an effective batch""" F""" size of {batch_size}. Make sure the batch size matches the length of the generators.""" ) UpperCAmelCase__ = randn_tensor(_lowercase , generator=_lowercase , device=self.device , dtype=_lowercase ) # set step values self.scheduler.set_timesteps(_lowercase , device=audio.device ) UpperCAmelCase__ = self.scheduler.timesteps.to(_lowercase ) for t in self.progress_bar(self.scheduler.timesteps ): # 1. predict noise model_output UpperCAmelCase__ = self.unet(_lowercase , _lowercase ).sample # 2. compute previous image: x_t -> t_t-1 UpperCAmelCase__ = self.scheduler.step(_lowercase , _lowercase , _lowercase ).prev_sample UpperCAmelCase__ = audio.clamp(-1 , 1 ).float().cpu().numpy() UpperCAmelCase__ = audio[:, :, :original_sample_size] if not return_dict: return (audio,) return AudioPipelineOutput(audios=_lowercase )
475
'''simple docstring''' import warnings from ...utils import logging from .image_processing_owlvit import OwlViTImageProcessor __UpperCamelCase = logging.get_logger(__name__) class _A ( __lowercase ): def __init__( self : int , *__magic_name__ : Optional[Any] , **__magic_name__ : Any ) -> None: """simple docstring""" warnings.warn( """The class OwlViTFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please""" """ use OwlViTImageProcessor instead.""" , __magic_name__ , ) super().__init__(*__magic_name__ , **__magic_name__ )
26
0
from sklearn.metrics import mean_squared_error import datasets A : str = '\\n@article{scikit-learn,\n title={Scikit-learn: Machine Learning in {P}ython},\n author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V.\n and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P.\n and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and\n Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.},\n journal={Journal of Machine Learning Research},\n volume={12},\n pages={2825--2830},\n year={2011}\n}\n' A : Union[str, Any] = '\\nMean Squared Error(MSE) is the average of the square of difference between the predicted\nand actual values.\n' A : Optional[int] = '\nArgs:\n predictions: array-like of shape (n_samples,) or (n_samples, n_outputs)\n Estimated target values.\n references: array-like of shape (n_samples,) or (n_samples, n_outputs)\n Ground truth (correct) target values.\n sample_weight: array-like of shape (n_samples,), default=None\n Sample weights.\n multioutput: {\"raw_values\", \"uniform_average\"} or array-like of shape (n_outputs,), default=\"uniform_average\"\n Defines aggregating of multiple output values. Array-like value defines weights used to average errors.\n\n \"raw_values\" : Returns a full set of errors in case of multioutput input.\n\n \"uniform_average\" : Errors of all outputs are averaged with uniform weight.\n\n squared : bool, default=True\n If True returns MSE value, if False returns RMSE (Root Mean Squared Error) value.\n\nReturns:\n mse : mean squared error.\nExamples:\n\n >>> mse_metric = datasets.load_metric(\"mse\")\n >>> predictions = [2.5, 0.0, 2, 8]\n >>> references = [3, -0.5, 2, 7]\n >>> results = mse_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {\'mse\': 0.375}\n >>> rmse_result = mse_metric.compute(predictions=predictions, references=references, squared=False)\n >>> print(rmse_result)\n {\'mse\': 0.6123724356957945}\n\n If you\'re using multi-dimensional lists, then set the config as follows :\n\n >>> mse_metric = datasets.load_metric(\"mse\", \"multilist\")\n >>> predictions = [[0.5, 1], [-1, 1], [7, -6]]\n >>> references = [[0, 2], [-1, 2], [8, -5]]\n >>> results = mse_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {\'mse\': 0.7083333333333334}\n >>> results = mse_metric.compute(predictions=predictions, references=references, multioutput=\'raw_values\')\n >>> print(results) # doctest: +NORMALIZE_WHITESPACE\n {\'mse\': array([0.41666667, 1. ])}\n' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class UpperCamelCase( datasets.Metric ): def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features(self._get_feature_types() ) , reference_urls=[ "https://scikit-learn.org/stable/modules/generated/sklearn.metrics.mean_squared_error.html" ] , ) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] ) -> Tuple: '''simple docstring''' if self.config_name == "multilist": return { "predictions": datasets.Sequence(datasets.Value("float" ) ), "references": datasets.Sequence(datasets.Value("float" ) ), } else: return { "predictions": datasets.Value("float" ), "references": datasets.Value("float" ), } def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , SCREAMING_SNAKE_CASE : Union[str, Any] , SCREAMING_SNAKE_CASE : Any , SCREAMING_SNAKE_CASE : int=None , SCREAMING_SNAKE_CASE : Tuple="uniform_average" , SCREAMING_SNAKE_CASE : Tuple=True ) -> int: '''simple docstring''' __snake_case = mean_squared_error( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , sample_weight=SCREAMING_SNAKE_CASE , multioutput=SCREAMING_SNAKE_CASE , squared=SCREAMING_SNAKE_CASE ) return {"mse": mse}
371
'''simple docstring''' import argparse import torch from transformers import BlenderbotConfig, BlenderbotForConditionalGeneration from transformers.utils import logging logging.set_verbosity_info() __UpperCamelCase = logging.get_logger(__name__) __UpperCamelCase = [ ["attention", "attn"], ["encoder_attention", "encoder_attn"], ["q_lin", "q_proj"], ["k_lin", "k_proj"], ["v_lin", "v_proj"], ["out_lin", "out_proj"], ["norm_embeddings", "layernorm_embedding"], ["position_embeddings", "embed_positions"], ["embeddings", "embed_tokens"], ["ffn.lin", "fc"], ] def _a ( _lowerCamelCase ) -> Union[str, Any]: """simple docstring""" if k == "embeddings.weight": return "shared.weight" for parlai_name, hf_name in PATTERNS: __snake_case : List[str] = k.replace(_lowerCamelCase , _lowerCamelCase ) if k.startswith("""encoder""" ): __snake_case : Optional[int] = k.replace(""".attn""" , """.self_attn""" ) __snake_case : Tuple = k.replace("""norm1""" , """self_attn_layer_norm""" ) __snake_case : List[str] = k.replace("""norm2""" , """final_layer_norm""" ) elif k.startswith("""decoder""" ): __snake_case : List[Any] = k.replace("""norm1""" , """self_attn_layer_norm""" ) __snake_case : str = k.replace("""norm2""" , """encoder_attn_layer_norm""" ) __snake_case : Optional[int] = k.replace("""norm3""" , """final_layer_norm""" ) return k def _a ( _lowerCamelCase ) -> Any: """simple docstring""" __snake_case : Optional[int] = [ """model.encoder.layernorm_embedding.weight""", """model.encoder.layernorm_embedding.bias""", """model.decoder.layernorm_embedding.weight""", """model.decoder.layernorm_embedding.bias""", ] for k in keys: __snake_case : Optional[Any] = sd.pop(_lowerCamelCase ) __snake_case : List[str] = k.replace("""layernorm_embedding""" , """layer_norm""" ) assert new_k not in sd __snake_case : Union[str, Any] = v __UpperCamelCase = ["START"] @torch.no_grad() def _a ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) -> Dict: """simple docstring""" __snake_case : Optional[int] = torch.load(_lowerCamelCase , map_location="""cpu""" ) __snake_case : Dict = model["""model"""] __snake_case : Optional[int] = BlenderbotConfig.from_json_file(_lowerCamelCase ) __snake_case : Union[str, Any] = BlenderbotForConditionalGeneration(_lowerCamelCase ) __snake_case : List[Any] = m.model.state_dict().keys() __snake_case : int = [] __snake_case : Union[str, Any] = {} for k, v in sd.items(): if k in IGNORE_KEYS: continue __snake_case : Optional[int] = rename_state_dict_key(_lowerCamelCase ) if new_k not in valid_keys: failures.append([k, new_k] ) else: __snake_case : str = v if cfg.normalize_before: # Blenderbot-3B checkpoints. Rename layernorm_embedding -> layer_norm rename_layernorm_keys(_lowerCamelCase ) m.model.load_state_dict(_lowerCamelCase , strict=_lowerCamelCase ) m.half() m.save_pretrained(_lowerCamelCase ) if __name__ == "__main__": __UpperCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument("--src_path", type=str, help="like blenderbot-model.bin") parser.add_argument("--save_dir", default="hf_blenderbot", type=str, help="Where to save converted model.") parser.add_argument( "--hf_config_json", default="blenderbot-3b-config.json", type=str, help="Path to config to use" ) __UpperCamelCase = parser.parse_args() convert_parlai_checkpoint(args.src_path, args.save_dir, args.hf_config_json)
26
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available lowercase_: Any = { 'configuration_rag': ['RagConfig'], 'retrieval_rag': ['RagRetriever'], 'tokenization_rag': ['RagTokenizer'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_: str = [ 'RagModel', 'RagPreTrainedModel', 'RagSequenceForGeneration', 'RagTokenForGeneration', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_: List[Any] = [ 'TFRagModel', 'TFRagPreTrainedModel', 'TFRagSequenceForGeneration', 'TFRagTokenForGeneration', ] if TYPE_CHECKING: from .configuration_rag import RagConfig from .retrieval_rag import RagRetriever from .tokenization_rag import RagTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_rag import RagModel, RagPreTrainedModel, RagSequenceForGeneration, RagTokenForGeneration try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_rag import ( TFRagModel, TFRagPreTrainedModel, TFRagSequenceForGeneration, TFRagTokenForGeneration, ) else: import sys lowercase_: Tuple = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
648
'''simple docstring''' import argparse import os import re import packaging.version __UpperCamelCase = "examples/" __UpperCamelCase = { "examples": (re.compile(R"^check_min_version\(\"[^\"]+\"\)\s*$", re.MULTILINE), "check_min_version(\"VERSION\")\n"), "init": (re.compile(R"^__version__\s+=\s+\"([^\"]+)\"\s*$", re.MULTILINE), "__version__ = \"VERSION\"\n"), "setup": (re.compile(R"^(\s*)version\s*=\s*\"[^\"]+\",", re.MULTILINE), R"\1version=\"VERSION\","), "doc": (re.compile(R"^(\s*)release\s*=\s*\"[^\"]+\"$", re.MULTILINE), "release = \"VERSION\"\n"), } __UpperCamelCase = { "init": "src/transformers/__init__.py", "setup": "setup.py", } __UpperCamelCase = "README.md" def _a ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) -> Tuple: """simple docstring""" with open(_lowerCamelCase , """r""" , encoding="""utf-8""" , newline="""\n""" ) as f: __snake_case : Union[str, Any] = f.read() __snake_case , __snake_case : List[Any] = REPLACE_PATTERNS[pattern] __snake_case : Optional[Any] = replace.replace("""VERSION""" , _lowerCamelCase ) __snake_case : Optional[Any] = re_pattern.sub(_lowerCamelCase , _lowerCamelCase ) with open(_lowerCamelCase , """w""" , encoding="""utf-8""" , newline="""\n""" ) as f: f.write(_lowerCamelCase ) def _a ( _lowerCamelCase ) -> Union[str, Any]: """simple docstring""" for folder, directories, fnames in os.walk(_lowerCamelCase ): # Removing some of the folders with non-actively maintained examples from the walk if "research_projects" in directories: directories.remove("""research_projects""" ) if "legacy" in directories: directories.remove("""legacy""" ) for fname in fnames: if fname.endswith(""".py""" ): update_version_in_file(os.path.join(_lowerCamelCase , _lowerCamelCase ) , _lowerCamelCase , pattern="""examples""" ) def _a ( _lowerCamelCase , _lowerCamelCase=False ) -> str: """simple docstring""" for pattern, fname in REPLACE_FILES.items(): update_version_in_file(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) if not patch: update_version_in_examples(_lowerCamelCase ) def _a ( ) -> Optional[int]: """simple docstring""" __snake_case : str = """🤗 Transformers currently provides the following architectures""" __snake_case : List[Any] = """1. Want to contribute a new model?""" with open(_lowerCamelCase , """r""" , encoding="""utf-8""" , newline="""\n""" ) as f: __snake_case : List[str] = f.readlines() # Find the start of the list. __snake_case : Optional[Any] = 0 while not lines[start_index].startswith(_start_prompt ): start_index += 1 start_index += 1 __snake_case : int = start_index # Update the lines in the model list. while not lines[index].startswith(_end_prompt ): if lines[index].startswith("""1.""" ): __snake_case : Optional[Any] = lines[index].replace( """https://huggingface.co/docs/transformers/main/model_doc""" , """https://huggingface.co/docs/transformers/model_doc""" , ) index += 1 with open(_lowerCamelCase , """w""" , encoding="""utf-8""" , newline="""\n""" ) as f: f.writelines(_lowerCamelCase ) def _a ( ) -> Union[str, Any]: """simple docstring""" with open(REPLACE_FILES["""init"""] , """r""" ) as f: __snake_case : List[Any] = f.read() __snake_case : str = REPLACE_PATTERNS["""init"""][0].search(_lowerCamelCase ).groups()[0] return packaging.version.parse(_lowerCamelCase ) def _a ( _lowerCamelCase=False ) -> int: """simple docstring""" __snake_case : List[Any] = get_version() if patch and default_version.is_devrelease: raise ValueError("""Can't create a patch version from the dev branch, checkout a released version!""" ) if default_version.is_devrelease: __snake_case : str = default_version.base_version elif patch: __snake_case : Optional[int] = F'''{default_version.major}.{default_version.minor}.{default_version.micro + 1}''' else: __snake_case : Dict = F'''{default_version.major}.{default_version.minor + 1}.0''' # Now let's ask nicely if that's the right one. __snake_case : Dict = input(F'''Which version are you releasing? [{default_version}]''' ) if len(_lowerCamelCase ) == 0: __snake_case : Any = default_version print(F'''Updating version to {version}.''' ) global_version_update(_lowerCamelCase , patch=_lowerCamelCase ) if not patch: print("""Cleaning main README, don't forget to run `make fix-copies`.""" ) clean_main_ref_in_model_list() def _a ( ) -> Tuple: """simple docstring""" __snake_case : Optional[Any] = get_version() __snake_case : Tuple = F'''{current_version.major}.{current_version.minor + 1}.0.dev0''' __snake_case : Union[str, Any] = current_version.base_version # Check with the user we got that right. __snake_case : int = input(F'''Which version are we developing now? [{dev_version}]''' ) if len(_lowerCamelCase ) == 0: __snake_case : Optional[int] = dev_version print(F'''Updating version to {version}.''' ) global_version_update(_lowerCamelCase ) print("""Cleaning main README, don't forget to run `make fix-copies`.""" ) clean_main_ref_in_model_list() if __name__ == "__main__": __UpperCamelCase = argparse.ArgumentParser() parser.add_argument("--post_release", action="store_true", help="Whether this is pre or post release.") parser.add_argument("--patch", action="store_true", help="Whether or not this is a patch release.") __UpperCamelCase = parser.parse_args() if not args.post_release: pre_release_work(patch=args.patch) elif args.patch: print("Nothing to do after a patch :-)") else: post_release_work()
26
0
"""simple docstring""" import functools from typing import Any def lowerCAmelCase_ ( SCREAMING_SNAKE_CASE__, SCREAMING_SNAKE_CASE__ ) -> bool: if not isinstance(_lowerCamelCase, _lowerCamelCase ) or len(_lowerCamelCase ) == 0: raise ValueError("the string should be not empty string" ) if not isinstance(_lowerCamelCase, _lowerCamelCase ) or not all( isinstance(_lowerCamelCase, _lowerCamelCase ) and len(_lowerCamelCase ) > 0 for item in words ): raise ValueError("the words should be a list of non-empty strings" ) # Build trie a_ : dict[str, Any] = {} a_ : str = """WORD_KEEPER""" for word in words: a_ : str = trie for c in word: if c not in trie_node: a_ : List[Any] = {} a_ : Optional[int] = trie_node[c] a_ : List[str] = True a_ : List[str] = len(_lowerCamelCase ) # Dynamic programming method @functools.cache def is_breakable(SCREAMING_SNAKE_CASE__ ) -> bool: if index == len_string: return True a_ : Optional[int] = trie for i in range(_lowerCamelCase, _lowerCamelCase ): a_ : Dict = trie_node.get(string[i], _lowerCamelCase ) if trie_node is None: return False if trie_node.get(_lowerCamelCase, _lowerCamelCase ) and is_breakable(i + 1 ): return True return False return is_breakable(0 ) if __name__ == "__main__": import doctest doctest.testmod()
237
'''simple docstring''' from unittest import TestCase from datasets import Sequence, Value from datasets.arrow_dataset import Dataset class _A ( __lowercase ): def lowercase__ ( self : Any ) -> str: """simple docstring""" return [ {"col_1": 3, "col_2": "a"}, {"col_1": 2, "col_2": "b"}, {"col_1": 1, "col_2": "c"}, {"col_1": 0, "col_2": "d"}, ] def lowercase__ ( self : str ) -> int: """simple docstring""" __snake_case : Union[str, Any] = {"""col_1""": [3, 2, 1, 0], """col_2""": ["""a""", """b""", """c""", """d"""]} return Dataset.from_dict(__magic_name__ ) def lowercase__ ( self : str ) -> List[Any]: """simple docstring""" __snake_case : Any = self._create_example_records() __snake_case : str = Dataset.from_list(__magic_name__ ) self.assertListEqual(dset.column_names , ["""col_1""", """col_2"""] ) for i, r in enumerate(__magic_name__ ): self.assertDictEqual(__magic_name__ , example_records[i] ) def lowercase__ ( self : Optional[Any] ) -> List[Any]: """simple docstring""" __snake_case : List[Any] = self._create_example_records() __snake_case : Dict = Dataset.from_list(__magic_name__ ) __snake_case : List[Any] = Dataset.from_dict({k: [r[k] for r in example_records] for k in example_records[0]} ) self.assertEqual(dset.info , dset_from_dict.info ) def lowercase__ ( self : str ) -> List[Any]: # checks what happens with missing columns """simple docstring""" __snake_case : Union[str, Any] = [{"""col_1""": 1}, {"""col_2""": """x"""}] __snake_case : Optional[int] = Dataset.from_list(__magic_name__ ) self.assertDictEqual(dset[0] , {"""col_1""": 1} ) self.assertDictEqual(dset[1] , {"""col_1""": None} ) # NB: first record is used for columns def lowercase__ ( self : List[str] ) -> Optional[Any]: # checks if the type can be inferred from the second record """simple docstring""" __snake_case : List[Any] = [{"""col_1""": []}, {"""col_1""": [1, 2]}] __snake_case : int = Dataset.from_list(__magic_name__ ) self.assertEqual(dset.info.features["""col_1"""] , Sequence(Value("""int64""" ) ) ) def lowercase__ ( self : int ) -> Union[str, Any]: """simple docstring""" __snake_case : Tuple = Dataset.from_list([] ) self.assertEqual(len(__magic_name__ ) , 0 ) self.assertListEqual(dset.column_names , [] )
26
0
"""simple docstring""" import unittest import numpy as np from diffusers import LMSDiscreteScheduler, OnnxStableDiffusionInpaintPipeline from diffusers.utils.testing_utils import ( is_onnx_available, load_image, nightly, require_onnxruntime, require_torch_gpu, ) from ..test_pipelines_onnx_common import OnnxPipelineTesterMixin if is_onnx_available(): import onnxruntime as ort class a__ ( __lowercase , unittest.TestCase ): # FIXME: add fast tests pass @nightly @require_onnxruntime @require_torch_gpu class a__ ( unittest.TestCase ): @property def __UpperCamelCase ( self : str) -> Optional[Any]: """simple docstring""" return ( "CUDAExecutionProvider", { "gpu_mem_limit": "15000000000", # 15GB "arena_extend_strategy": "kSameAsRequested", }, ) @property def __UpperCamelCase ( self : int) -> Optional[Any]: """simple docstring""" _lowerCAmelCase:str = ort.SessionOptions() _lowerCAmelCase:Optional[Any] = False return options def __UpperCamelCase ( self : Any) -> Tuple: """simple docstring""" _lowerCAmelCase:Tuple = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo.png''') _lowerCAmelCase:List[Any] = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo_mask.png''') _lowerCAmelCase:List[str] = OnnxStableDiffusionInpaintPipeline.from_pretrained( '''runwayml/stable-diffusion-inpainting''' ,revision='''onnx''' ,safety_checker=a__ ,feature_extractor=a__ ,provider=self.gpu_provider ,sess_options=self.gpu_options ,) pipe.set_progress_bar_config(disable=a__) _lowerCAmelCase:str = """A red cat sitting on a park bench""" _lowerCAmelCase:List[str] = np.random.RandomState(0) _lowerCAmelCase:Optional[int] = pipe( prompt=a__ ,image=a__ ,mask_image=a__ ,guidance_scale=7.5 ,num_inference_steps=10 ,generator=a__ ,output_type='''np''' ,) _lowerCAmelCase:Tuple = output.images _lowerCAmelCase:Dict = images[0, 255:258, 255:258, -1] assert images.shape == (1, 512, 512, 3) _lowerCAmelCase:str = np.array([0.2514, 0.3007, 0.3517, 0.1790, 0.2382, 0.3167, 0.1944, 0.2273, 0.2464]) assert np.abs(image_slice.flatten() - expected_slice).max() < 1E-3 def __UpperCamelCase ( self : Union[str, Any]) -> str: """simple docstring""" _lowerCAmelCase:int = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo.png''') _lowerCAmelCase:Tuple = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/in_paint/overture-creations-5sI6fQgYIuo_mask.png''') _lowerCAmelCase:List[str] = LMSDiscreteScheduler.from_pretrained( '''runwayml/stable-diffusion-inpainting''' ,subfolder='''scheduler''' ,revision='''onnx''') _lowerCAmelCase:List[Any] = OnnxStableDiffusionInpaintPipeline.from_pretrained( '''runwayml/stable-diffusion-inpainting''' ,revision='''onnx''' ,scheduler=a__ ,safety_checker=a__ ,feature_extractor=a__ ,provider=self.gpu_provider ,sess_options=self.gpu_options ,) pipe.set_progress_bar_config(disable=a__) _lowerCAmelCase:Union[str, Any] = """A red cat sitting on a park bench""" _lowerCAmelCase:Union[str, Any] = np.random.RandomState(0) _lowerCAmelCase:Optional[Any] = pipe( prompt=a__ ,image=a__ ,mask_image=a__ ,guidance_scale=7.5 ,num_inference_steps=20 ,generator=a__ ,output_type='''np''' ,) _lowerCAmelCase:Optional[int] = output.images _lowerCAmelCase:Dict = images[0, 255:258, 255:258, -1] assert images.shape == (1, 512, 512, 3) _lowerCAmelCase:Dict = np.array([0.0086, 0.0077, 0.0083, 0.0093, 0.0107, 0.0139, 0.0094, 0.0097, 0.0125]) assert np.abs(image_slice.flatten() - expected_slice).max() < 1E-3
227
'''simple docstring''' import inspect import unittest import torch import torch.nn as nn from accelerate.hooks import ( AlignDevicesHook, ModelHook, SequentialHook, add_hook_to_module, attach_align_device_hook, remove_hook_from_module, remove_hook_from_submodules, ) from accelerate.test_utils import require_multi_gpu class _A ( nn.Module ): def __init__( self : List[str] ) -> Optional[Any]: """simple docstring""" super().__init__() __snake_case : List[Any] = nn.Linear(3 , 4 ) __snake_case : str = nn.BatchNormad(4 ) __snake_case : Optional[Any] = nn.Linear(4 , 5 ) def lowercase__ ( self : str , __magic_name__ : Dict ) -> List[str]: """simple docstring""" return self.lineara(self.batchnorm(self.lineara(__magic_name__ ) ) ) class _A ( __lowercase ): def lowercase__ ( self : List[str] , __magic_name__ : Tuple , *__magic_name__ : Dict , **__magic_name__ : Optional[Any] ) -> Tuple: """simple docstring""" return (args[0] + 1,) + args[1:], kwargs class _A ( __lowercase ): def lowercase__ ( self : str , __magic_name__ : Union[str, Any] , __magic_name__ : Tuple ) -> Union[str, Any]: """simple docstring""" return output + 1 class _A ( unittest.TestCase ): def lowercase__ ( self : Dict ) -> Any: """simple docstring""" __snake_case : int = ModelForTest() __snake_case : Tuple = ModelHook() add_hook_to_module(__magic_name__ , __magic_name__ ) self.assertEqual(test_model._hf_hook , __magic_name__ ) self.assertTrue(hasattr(__magic_name__ , """_old_forward""" ) ) # Check adding the hook did not change the name or the signature self.assertEqual(test_model.forward.__name__ , """forward""" ) self.assertListEqual(list(inspect.signature(test_model.forward ).parameters ) , ["""x"""] ) remove_hook_from_module(__magic_name__ ) self.assertFalse(hasattr(__magic_name__ , """_hf_hook""" ) ) self.assertFalse(hasattr(__magic_name__ , """_old_forward""" ) ) def lowercase__ ( self : Tuple ) -> List[str]: """simple docstring""" __snake_case : List[Any] = ModelForTest() __snake_case : Optional[int] = ModelHook() add_hook_to_module(__magic_name__ , __magic_name__ ) add_hook_to_module(__magic_name__ , __magic_name__ , append=__magic_name__ ) self.assertEqual(isinstance(test_model._hf_hook , __magic_name__ ) , __magic_name__ ) self.assertEqual(len(test_model._hf_hook.hooks ) , 2 ) self.assertTrue(hasattr(__magic_name__ , """_old_forward""" ) ) # Check adding the hook did not change the name or the signature self.assertEqual(test_model.forward.__name__ , """forward""" ) self.assertListEqual(list(inspect.signature(test_model.forward ).parameters ) , ["""x"""] ) remove_hook_from_module(__magic_name__ ) self.assertFalse(hasattr(__magic_name__ , """_hf_hook""" ) ) self.assertFalse(hasattr(__magic_name__ , """_old_forward""" ) ) def lowercase__ ( self : str ) -> Union[str, Any]: """simple docstring""" __snake_case : List[Any] = ModelForTest() __snake_case : Any = torch.randn(2 , 3 ) __snake_case : str = test_model(x + 1 ) __snake_case : int = test_model(x + 2 ) __snake_case : Union[str, Any] = PreForwardHook() add_hook_to_module(__magic_name__ , __magic_name__ ) __snake_case : int = test_model(__magic_name__ ) self.assertTrue(torch.allclose(__magic_name__ , __magic_name__ , atol=1E-5 ) ) # Attaching a hook to a model when it already has one replaces, does not chain __snake_case : Optional[int] = PreForwardHook() add_hook_to_module(__magic_name__ , __magic_name__ ) __snake_case : List[Any] = test_model(__magic_name__ ) self.assertTrue(torch.allclose(__magic_name__ , __magic_name__ , atol=1E-5 ) ) # You need to use the sequential hook to chain two or more hooks __snake_case : Optional[int] = SequentialHook(PreForwardHook() , PreForwardHook() ) add_hook_to_module(__magic_name__ , __magic_name__ ) __snake_case : List[str] = test_model(__magic_name__ ) assert torch.allclose(__magic_name__ , __magic_name__ , atol=1E-5 ) def lowercase__ ( self : Union[str, Any] ) -> List[str]: """simple docstring""" __snake_case : Union[str, Any] = ModelForTest() __snake_case : str = torch.randn(2 , 3 ) __snake_case : Any = test_model(__magic_name__ ) __snake_case : Any = PostForwardHook() add_hook_to_module(__magic_name__ , __magic_name__ ) __snake_case : Any = test_model(__magic_name__ ) self.assertTrue(torch.allclose(__magic_name__ , output + 1 , atol=1E-5 ) ) # Attaching a hook to a model when it already has one replaces, does not chain __snake_case : Any = PostForwardHook() add_hook_to_module(__magic_name__ , __magic_name__ ) __snake_case : Dict = test_model(__magic_name__ ) self.assertTrue(torch.allclose(__magic_name__ , output + 1 , atol=1E-5 ) ) # You need to use the sequential hook to chain two or more hooks __snake_case : str = SequentialHook(PostForwardHook() , PostForwardHook() ) add_hook_to_module(__magic_name__ , __magic_name__ ) __snake_case : int = test_model(__magic_name__ ) assert torch.allclose(__magic_name__ , output + 2 , atol=1E-5 ) def lowercase__ ( self : str ) -> int: """simple docstring""" __snake_case : Union[str, Any] = ModelForTest() __snake_case : int = torch.randn(2 , 3 ) __snake_case : Any = test_model(__magic_name__ ) __snake_case : Dict = PostForwardHook() add_hook_to_module(__magic_name__ , __magic_name__ ) __snake_case : List[Any] = test_model(__magic_name__ ) self.assertTrue(torch.allclose(__magic_name__ , output + 1 ) ) self.assertTrue(outputa.requires_grad ) __snake_case : Dict = True __snake_case : int = test_model(__magic_name__ ) self.assertFalse(outputa.requires_grad ) @require_multi_gpu def lowercase__ ( self : Tuple ) -> List[Any]: """simple docstring""" __snake_case : Tuple = ModelForTest() # Everything is on CPU self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.batchnorm.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) # This will move each submodule on different devices add_hook_to_module(model.lineara , AlignDevicesHook(execution_device=0 ) ) add_hook_to_module(model.batchnorm , AlignDevicesHook(execution_device=0 ) ) add_hook_to_module(model.lineara , AlignDevicesHook(execution_device=1 ) ) self.assertEqual(model.lineara.weight.device , torch.device(0 ) ) self.assertEqual(model.batchnorm.weight.device , torch.device(0 ) ) self.assertEqual(model.batchnorm.running_mean.device , torch.device(0 ) ) self.assertEqual(model.lineara.weight.device , torch.device(1 ) ) # We can still make a forward pass. The input does not need to be on any particular device __snake_case : Tuple = torch.randn(2 , 3 ) __snake_case : Union[str, Any] = model(__magic_name__ ) self.assertEqual(output.device , torch.device(1 ) ) # We can add a general hook to put back output on same device as input. add_hook_to_module(__magic_name__ , AlignDevicesHook(io_same_device=__magic_name__ ) ) __snake_case : Tuple = torch.randn(2 , 3 ).to(0 ) __snake_case : Any = model(__magic_name__ ) self.assertEqual(output.device , torch.device(0 ) ) def lowercase__ ( self : Union[str, Any] ) -> str: """simple docstring""" __snake_case : int = ModelForTest() # Everything is on CPU self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.batchnorm.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) # This will move each submodule on different devices __snake_case : List[str] = {"""execution_device""": 0 if torch.cuda.is_available() else """cpu""", """offload""": True} add_hook_to_module(model.lineara , AlignDevicesHook(**__magic_name__ ) ) add_hook_to_module(model.batchnorm , AlignDevicesHook(**__magic_name__ ) ) add_hook_to_module(model.lineara , AlignDevicesHook(**__magic_name__ ) ) # Parameters have been offloaded, so on the meta device self.assertEqual(model.lineara.weight.device , torch.device("""meta""" ) ) self.assertEqual(model.batchnorm.weight.device , torch.device("""meta""" ) ) self.assertEqual(model.lineara.weight.device , torch.device("""meta""" ) ) # Buffers are not included in the offload by default, so are on the execution device __snake_case : Any = torch.device(hook_kwargs["""execution_device"""] ) self.assertEqual(model.batchnorm.running_mean.device , __magic_name__ ) __snake_case : Dict = torch.randn(2 , 3 ) __snake_case : Any = model(__magic_name__ ) self.assertEqual(output.device , __magic_name__ ) # Removing hooks loads back the weights in the model. remove_hook_from_module(model.lineara ) remove_hook_from_module(model.batchnorm ) remove_hook_from_module(model.lineara ) self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.batchnorm.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) # Now test with buffers included in the offload __snake_case : int = { """execution_device""": 0 if torch.cuda.is_available() else """cpu""", """offload""": True, """offload_buffers""": True, } add_hook_to_module(model.lineara , AlignDevicesHook(**__magic_name__ ) ) add_hook_to_module(model.batchnorm , AlignDevicesHook(**__magic_name__ ) ) add_hook_to_module(model.lineara , AlignDevicesHook(**__magic_name__ ) ) # Parameters have been offloaded, so on the meta device, buffers included self.assertEqual(model.lineara.weight.device , torch.device("""meta""" ) ) self.assertEqual(model.batchnorm.weight.device , torch.device("""meta""" ) ) self.assertEqual(model.lineara.weight.device , torch.device("""meta""" ) ) self.assertEqual(model.batchnorm.running_mean.device , torch.device("""meta""" ) ) __snake_case : str = torch.randn(2 , 3 ) __snake_case : str = model(__magic_name__ ) self.assertEqual(output.device , __magic_name__ ) # Removing hooks loads back the weights in the model. remove_hook_from_module(model.lineara ) remove_hook_from_module(model.batchnorm ) remove_hook_from_module(model.lineara ) self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.batchnorm.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) def lowercase__ ( self : Dict ) -> str: """simple docstring""" __snake_case : Tuple = ModelForTest() # Everything is on CPU self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.batchnorm.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) # This will move each submodule on different devices __snake_case : Union[str, Any] = 0 if torch.cuda.is_available() else """cpu""" attach_align_device_hook(__magic_name__ , execution_device=__magic_name__ , offload=__magic_name__ ) # Parameters have been offloaded, so on the meta device self.assertEqual(model.lineara.weight.device , torch.device("""meta""" ) ) self.assertEqual(model.batchnorm.weight.device , torch.device("""meta""" ) ) self.assertEqual(model.lineara.weight.device , torch.device("""meta""" ) ) # Buffers are not included in the offload by default, so are on the execution device __snake_case : Union[str, Any] = torch.device(__magic_name__ ) self.assertEqual(model.batchnorm.running_mean.device , __magic_name__ ) __snake_case : Optional[int] = torch.randn(2 , 3 ) __snake_case : Dict = model(__magic_name__ ) self.assertEqual(output.device , __magic_name__ ) # Removing hooks loads back the weights in the model. remove_hook_from_submodules(__magic_name__ ) self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.batchnorm.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) # Now test with buffers included in the offload attach_align_device_hook(__magic_name__ , execution_device=__magic_name__ , offload=__magic_name__ , offload_buffers=__magic_name__ ) # Parameters have been offloaded, so on the meta device, buffers included self.assertEqual(model.lineara.weight.device , torch.device("""meta""" ) ) self.assertEqual(model.batchnorm.weight.device , torch.device("""meta""" ) ) self.assertEqual(model.lineara.weight.device , torch.device("""meta""" ) ) self.assertEqual(model.batchnorm.running_mean.device , torch.device("""meta""" ) ) __snake_case : Dict = torch.randn(2 , 3 ) __snake_case : Optional[int] = model(__magic_name__ ) self.assertEqual(output.device , __magic_name__ ) # Removing hooks loads back the weights in the model. remove_hook_from_submodules(__magic_name__ ) self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.batchnorm.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) def lowercase__ ( self : Any ) -> Union[str, Any]: """simple docstring""" __snake_case : Any = ModelForTest() # Everything is on CPU self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.batchnorm.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) # This will move each submodule on different devices __snake_case : str = 0 if torch.cuda.is_available() else """cpu""" attach_align_device_hook( __magic_name__ , execution_device=__magic_name__ , offload=__magic_name__ , weights_map=model.state_dict() ) # Parameters have been offloaded, so on the meta device self.assertEqual(model.lineara.weight.device , torch.device("""meta""" ) ) self.assertEqual(model.batchnorm.weight.device , torch.device("""meta""" ) ) self.assertEqual(model.lineara.weight.device , torch.device("""meta""" ) ) # Buffers are not included in the offload by default, so are on the execution device __snake_case : List[str] = torch.device(__magic_name__ ) self.assertEqual(model.batchnorm.running_mean.device , __magic_name__ ) __snake_case : Tuple = torch.randn(2 , 3 ) __snake_case : Optional[Any] = model(__magic_name__ ) self.assertEqual(output.device , __magic_name__ ) # Removing hooks loads back the weights in the model. remove_hook_from_submodules(__magic_name__ ) self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.batchnorm.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) # Now test with buffers included in the offload attach_align_device_hook( __magic_name__ , execution_device=__magic_name__ , offload=__magic_name__ , weights_map=model.state_dict() , offload_buffers=__magic_name__ , ) # Parameters have been offloaded, so on the meta device, buffers included self.assertEqual(model.lineara.weight.device , torch.device("""meta""" ) ) self.assertEqual(model.batchnorm.weight.device , torch.device("""meta""" ) ) self.assertEqual(model.lineara.weight.device , torch.device("""meta""" ) ) self.assertEqual(model.batchnorm.running_mean.device , torch.device("""meta""" ) ) __snake_case : List[str] = torch.randn(2 , 3 ) __snake_case : Dict = model(__magic_name__ ) self.assertEqual(output.device , __magic_name__ ) # Removing hooks loads back the weights in the model. remove_hook_from_submodules(__magic_name__ ) self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.batchnorm.weight.device , torch.device("""cpu""" ) ) self.assertEqual(model.lineara.weight.device , torch.device("""cpu""" ) )
26
0
from torch import nn def _lowerCamelCase ( __lowerCamelCase ) -> Tuple: '''simple docstring''' if act_fn in ["swish", "silu"]: return nn.SiLU() elif act_fn == "mish": return nn.Mish() elif act_fn == "gelu": return nn.GELU() else: raise ValueError(F"Unsupported activation function: {act_fn}" )
79
'''simple docstring''' from __future__ import annotations __UpperCamelCase = [ [-1, 0], # left [0, -1], # down [1, 0], # right [0, 1], # up ] def _a ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , ) -> tuple[list[list[int]], list[list[int]]]: """simple docstring""" __snake_case : List[str] = [ [0 for col in range(len(grid[0] ) )] for row in range(len(_lowerCamelCase ) ) ] # the reference grid __snake_case : Tuple = 1 __snake_case : List[str] = [ [0 for col in range(len(grid[0] ) )] for row in range(len(_lowerCamelCase ) ) ] # the action grid __snake_case : List[str] = init[0] __snake_case : str = init[1] __snake_case : int = 0 __snake_case : int = g + heuristic[x][y] # cost from starting cell to destination cell __snake_case : List[str] = [[f, g, x, y]] __snake_case : Any = False # flag that is set when search is complete __snake_case : int = False # flag set if we can't find expand while not found and not resign: if len(_lowerCamelCase ) == 0: raise ValueError("""Algorithm is unable to find solution""" ) else: # to choose the least costliest action so as to move closer to the goal cell.sort() cell.reverse() __snake_case : Tuple = cell.pop() __snake_case : Optional[int] = next_cell[2] __snake_case : List[Any] = next_cell[3] __snake_case : int = next_cell[1] if x == goal[0] and y == goal[1]: __snake_case : Optional[Any] = True else: for i in range(len(_lowerCamelCase ) ): # to try out different valid actions __snake_case : Union[str, Any] = x + DIRECTIONS[i][0] __snake_case : str = y + DIRECTIONS[i][1] if xa >= 0 and xa < len(_lowerCamelCase ) and ya >= 0 and ya < len(grid[0] ): if closed[xa][ya] == 0 and grid[xa][ya] == 0: __snake_case : str = g + cost __snake_case : Tuple = ga + heuristic[xa][ya] cell.append([fa, ga, xa, ya] ) __snake_case : List[str] = 1 __snake_case : Optional[int] = i __snake_case : List[str] = [] __snake_case : Optional[int] = goal[0] __snake_case : List[Any] = goal[1] invpath.append([x, y] ) # we get the reverse path from here while x != init[0] or y != init[1]: __snake_case : Dict = x - DIRECTIONS[action[x][y]][0] __snake_case : int = y - DIRECTIONS[action[x][y]][1] __snake_case : Optional[int] = xa __snake_case : int = ya invpath.append([x, y] ) __snake_case : Optional[int] = [] for i in range(len(_lowerCamelCase ) ): path.append(invpath[len(_lowerCamelCase ) - 1 - i] ) return path, action if __name__ == "__main__": __UpperCamelCase = [ [0, 1, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles [0, 1, 0, 0, 0, 0], [0, 1, 0, 0, 1, 0], [0, 0, 0, 0, 1, 0], ] __UpperCamelCase = [0, 0] # all coordinates are given in format [y,x] __UpperCamelCase = [len(grid) - 1, len(grid[0]) - 1] __UpperCamelCase = 1 # the cost map which pushes the path closer to the goal __UpperCamelCase = [[0 for row in range(len(grid[0]))] for col in range(len(grid))] for i in range(len(grid)): for j in range(len(grid[0])): __UpperCamelCase = abs(i - goal[0]) + abs(j - goal[1]) if grid[i][j] == 1: # added extra penalty in the heuristic map __UpperCamelCase = 99 __UpperCamelCase , __UpperCamelCase = search(grid, init, goal, cost, heuristic) print("ACTION MAP") for i in range(len(action)): print(action[i]) for i in range(len(path)): print(path[i])
26
0
import copy from ...configuration_utils import PretrainedConfig from ...utils import logging snake_case_ : Optional[Any] = logging.get_logger(__name__) class snake_case_ ( __lowercase ): '''simple docstring''' lowerCamelCase = '''encoder-decoder''' lowerCamelCase = True def __init__( self : str , **__magic_name__ : int ) -> str: super().__init__(**__magic_name__ ) assert ( "encoder" in kwargs and "decoder" in kwargs ), "Config has to be initialized with encoder and decoder config" lowerCamelCase_ : Any = kwargs.pop("encoder" ) lowerCamelCase_ : Union[str, Any] = encoder_config.pop("model_type" ) lowerCamelCase_ : Optional[int] = kwargs.pop("decoder" ) lowerCamelCase_ : Dict = decoder_config.pop("model_type" ) from ..auto.configuration_auto import AutoConfig lowerCamelCase_ : Optional[Any] = AutoConfig.for_model(__magic_name__ , **__magic_name__ ) lowerCamelCase_ : Optional[Any] = AutoConfig.for_model(__magic_name__ , **__magic_name__ ) lowerCamelCase_ : List[str] = True @classmethod def __SCREAMING_SNAKE_CASE ( cls : Optional[Any] , __magic_name__ : PretrainedConfig , __magic_name__ : PretrainedConfig , **__magic_name__ : Dict ) -> PretrainedConfig: logger.info("Set `config.is_decoder=True` and `config.add_cross_attention=True` for decoder_config" ) lowerCamelCase_ : List[Any] = True lowerCamelCase_ : Any = True return cls(encoder=encoder_config.to_dict() , decoder=decoder_config.to_dict() , **__magic_name__ ) def __SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> str: lowerCamelCase_ : Union[str, Any] = copy.deepcopy(self.__dict__ ) lowerCamelCase_ : int = self.encoder.to_dict() lowerCamelCase_ : List[str] = self.decoder.to_dict() lowerCamelCase_ : Tuple = self.__class__.model_type return output
488
'''simple docstring''' def _a ( _lowerCamelCase ) -> int: """simple docstring""" if not isinstance(_lowerCamelCase , _lowerCamelCase ): raise TypeError("""only integers accepted as input""" ) else: __snake_case : List[Any] = str(abs(_lowerCamelCase ) ) __snake_case : Union[str, Any] = [list(_lowerCamelCase ) for char in range(len(_lowerCamelCase ) )] for index in range(len(_lowerCamelCase ) ): num_transpositions[index].pop(_lowerCamelCase ) return max( int("""""".join(list(_lowerCamelCase ) ) ) for transposition in num_transpositions ) if __name__ == "__main__": __import__("doctest").testmod()
26
0
from __future__ import annotations from typing import Any class lowerCamelCase_ : def __init__( self : str , __A : int , __A : int , __A : float = 0 ): __A : Optional[Any] = row, column __A : Dict = [[default_value for c in range(__A )] for r in range(__A )] def __str__( self : List[Any] ): __A : Dict = F"""Matrix consist of {self.row} rows and {self.column} columns\n""" # Make string identifier __A : Optional[int] = 0 for row_vector in self.array: for obj in row_vector: __A : Optional[int] = max(__A , len(str(__A ) ) ) __A : str = F"""%{max_element_length}s""" # Make string and return def single_line(__A : list[float] ) -> str: nonlocal string_format_identifier __A : Union[str, Any] = """[""" line += ", ".join(string_format_identifier % (obj,) for obj in row_vector ) line += "]" return line s += "\n".join(single_line(__A ) for row_vector in self.array ) return s def __repr__( self : Optional[int] ): return str(self ) def lowerCAmelCase_ ( self : Dict , __A : tuple[int, int] ): if not (isinstance(__A , (list, tuple) ) and len(__A ) == 2): return False elif not (0 <= loc[0] < self.row and 0 <= loc[1] < self.column): return False else: return True def __getitem__( self : int , __A : tuple[int, int] ): assert self.validate_indicies(__A ) return self.array[loc[0]][loc[1]] def __setitem__( self : List[str] , __A : tuple[int, int] , __A : float ): assert self.validate_indicies(__A ) __A : Optional[int] = value def __add__( self : Any , __A : Matrix ): assert isinstance(__A , __A ) assert self.row == another.row and self.column == another.column # Add __A : Union[str, Any] = Matrix(self.row , self.column ) for r in range(self.row ): for c in range(self.column ): __A : List[Any] = self[r, c] + another[r, c] return result def __neg__( self : Tuple ): __A : Tuple = Matrix(self.row , self.column ) for r in range(self.row ): for c in range(self.column ): __A : List[Any] = -self[r, c] return result def __sub__( self : Optional[int] , __A : Matrix ): return self + (-another) def __mul__( self : List[Any] , __A : int | float | Matrix ): if isinstance(__A , (int, float) ): # Scalar multiplication __A : Optional[int] = Matrix(self.row , self.column ) for r in range(self.row ): for c in range(self.column ): __A : Tuple = self[r, c] * another return result elif isinstance(__A , __A ): # Matrix multiplication assert self.column == another.row __A : Dict = Matrix(self.row , another.column ) for r in range(self.row ): for c in range(another.column ): for i in range(self.column ): result[r, c] += self[r, i] * another[i, c] return result else: __A : Optional[int] = F"""Unsupported type given for another ({type(__A )})""" raise TypeError(__A ) def lowerCAmelCase_ ( self : str ): __A : Any = Matrix(self.column , self.row ) for r in range(self.row ): for c in range(self.column ): __A : str = self[r, c] return result def lowerCAmelCase_ ( self : Union[str, Any] , __A : Matrix , __A : Matrix ): assert isinstance(__A , __A ) and isinstance(__A , __A ) assert self.row == self.column == u.row == v.row # u, v should be column vector assert u.column == v.column == 1 # u, v should be column vector # Calculate __A : List[str] = v.transpose() __A : Tuple = (v_t * self * u)[0, 0] + 1 if numerator_factor == 0: return None # It's not invertable return self - ((self * u) * (v_t * self) * (1.0 / numerator_factor)) # Testing if __name__ == "__main__": def __SCREAMING_SNAKE_CASE ( ) -> None: __A : Tuple = Matrix(3 ,3 ,0 ) for i in range(3 ): __A : Any = 1 print(f"""a^(-1) is {ainv}""" ) # u, v __A : Dict = Matrix(3 ,1 ,0 ) __A : Union[str, Any] = 1, 2, -3 __A : str = Matrix(3 ,1 ,0 ) __A : Tuple = 4, -2, 5 print(f"""u is {u}""" ) print(f"""v is {v}""" ) print(f"""uv^T is {u * v.transpose()}""" ) # Sherman Morrison print(f"""(a + uv^T)^(-1) is {ainv.sherman_morrison(_lowerCamelCase ,_lowerCamelCase )}""" ) def __SCREAMING_SNAKE_CASE ( ) -> None: import doctest doctest.testmod() testa()
17
'''simple docstring''' from __future__ import annotations import math def _a ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) -> int: """simple docstring""" if depth < 0: raise ValueError("""Depth cannot be less than 0""" ) if not scores: raise ValueError("""Scores cannot be empty""" ) if depth == height: return scores[node_index] return ( max( minimax(depth + 1 , node_index * 2 , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) , minimax(depth + 1 , node_index * 2 + 1 , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) , ) if is_max else min( minimax(depth + 1 , node_index * 2 , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) , minimax(depth + 1 , node_index * 2 + 1 , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) , ) ) def _a ( ) -> None: """simple docstring""" __snake_case : Union[str, Any] = [90, 23, 6, 33, 21, 65, 123, 3_4423] __snake_case : Optional[int] = math.log(len(_lowerCamelCase ) , 2 ) print(F'''Optimal value : {minimax(0 , 0 , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase )}''' ) if __name__ == "__main__": import doctest doctest.testmod() main()
26
0
"""simple docstring""" from __future__ import annotations import math import random from collections.abc import Collection from typing import overload class a__ : def __init__( self , _a = None ): if components is None: lowercase : Tuple = [] lowercase : List[str] = list(_a ) def __len__( self ): return len(self.__components ) def __str__( self ): return "(" + ",".join(map(_a , self.__components ) ) + ")" def __add__( self , _a ): lowercase : List[str] = len(self ) if size == len(_a ): lowercase : Any = [self.__components[i] + other.component(_a ) for i in range(_a )] return Vector(_a ) else: raise Exception("must have the same size" ) def __sub__( self , _a ): lowercase : Union[str, Any] = len(self ) if size == len(_a ): lowercase : Dict = [self.__components[i] - other.component(_a ) for i in range(_a )] return Vector(_a ) else: # error case raise Exception("must have the same size" ) @overload def __mul__( self , _a ): ... @overload def __mul__( self , _a ): ... def __mul__( self , _a ): if isinstance(_a , (float, int) ): lowercase : Optional[int] = [c * other for c in self.__components] return Vector(_a ) elif isinstance(_a , _a ) and len(self ) == len(_a ): lowercase : Tuple = len(self ) lowercase : int = [self.__components[i] * other.component(_a ) for i in range(_a )] return sum(_a ) else: # error case raise Exception("invalid operand!" ) def __magic_name__ ( self ): return Vector(self.__components ) def __magic_name__ ( self , _a ): if isinstance(_a , _a ) and -len(self.__components ) <= i < len(self.__components ): return self.__components[i] else: raise Exception("index out of range" ) def __magic_name__ ( self , _a , _a ): assert -len(self.__components ) <= pos < len(self.__components ) lowercase : str = value def __magic_name__ ( self ): if len(self.__components ) == 0: raise Exception("Vector is empty" ) lowercase : str = [c**2 for c in self.__components] return math.sqrt(sum(_a ) ) def __magic_name__ ( self , _a , _a = False ): lowercase : Tuple = self * other lowercase : Optional[Any] = self.euclidean_length() * other.euclidean_length() if deg: return math.degrees(math.acos(num / den ) ) else: return math.acos(num / den ) def __magic_name__ ( __snake_case : List[Any] ) -> Vector: assert isinstance(_lowerCamelCase , _lowerCamelCase ) return Vector([0] * dimension ) def __magic_name__ ( __snake_case : str , __snake_case : Any ) -> Vector: assert isinstance(_lowerCamelCase , _lowerCamelCase ) and (isinstance(_lowerCamelCase , _lowerCamelCase )) lowercase : List[Any] = [0] * dimension lowercase : Union[str, Any] = 1 return Vector(_lowerCamelCase ) def __magic_name__ ( __snake_case : Optional[int] , __snake_case : Tuple , __snake_case : Optional[int] ) -> Vector: assert ( isinstance(_lowerCamelCase , _lowerCamelCase ) and isinstance(_lowerCamelCase , _lowerCamelCase ) and (isinstance(_lowerCamelCase , (int, float) )) ) return x * scalar + y def __magic_name__ ( __snake_case : Optional[int] , __snake_case : Optional[int] , __snake_case : Union[str, Any] ) -> Vector: random.seed(_lowerCamelCase ) lowercase : List[Any] = [random.randint(_lowerCamelCase , _lowerCamelCase ) for _ in range(_lowerCamelCase )] return Vector(_lowerCamelCase ) class a__ : def __init__( self , _a , _a , _a ): lowercase : Tuple = matrix lowercase : List[str] = w lowercase : Union[str, Any] = h def __str__( self ): lowercase : Optional[int] = """""" for i in range(self.__height ): ans += "|" for j in range(self.__width ): if j < self.__width - 1: ans += str(self.__matrix[i][j] ) + "," else: ans += str(self.__matrix[i][j] ) + "|\n" return ans def __add__( self , _a ): if self.__width == other.width() and self.__height == other.height(): lowercase : Tuple = [] for i in range(self.__height ): lowercase : Optional[Any] = [ self.__matrix[i][j] + other.component(_a , _a ) for j in range(self.__width ) ] matrix.append(_a ) return Matrix(_a , self.__width , self.__height ) else: raise Exception("matrix must have the same dimension!" ) def __sub__( self , _a ): if self.__width == other.width() and self.__height == other.height(): lowercase : Optional[Any] = [] for i in range(self.__height ): lowercase : Union[str, Any] = [ self.__matrix[i][j] - other.component(_a , _a ) for j in range(self.__width ) ] matrix.append(_a ) return Matrix(_a , self.__width , self.__height ) else: raise Exception("matrices must have the same dimension!" ) @overload def __mul__( self , _a ): ... @overload def __mul__( self , _a ): ... def __mul__( self , _a ): if isinstance(_a , _a ): # matrix-vector if len(_a ) == self.__width: lowercase : Optional[int] = zero_vector(self.__height ) for i in range(self.__height ): lowercase : List[Any] = [ self.__matrix[i][j] * other.component(_a ) for j in range(self.__width ) ] ans.change_component(_a , sum(_a ) ) return ans else: raise Exception( "vector must have the same size as the " "number of columns of the matrix!" ) elif isinstance(_a , (int, float) ): # matrix-scalar lowercase : Tuple = [ [self.__matrix[i][j] * other for j in range(self.__width )] for i in range(self.__height ) ] return Matrix(_a , self.__width , self.__height ) return None def __magic_name__ ( self ): return self.__height def __magic_name__ ( self ): return self.__width def __magic_name__ ( self , _a , _a ): if 0 <= x < self.__height and 0 <= y < self.__width: return self.__matrix[x][y] else: raise Exception("change_component: indices out of bounds" ) def __magic_name__ ( self , _a , _a , _a ): if 0 <= x < self.__height and 0 <= y < self.__width: lowercase : Optional[Any] = value else: raise Exception("change_component: indices out of bounds" ) def __magic_name__ ( self , _a , _a ): if self.__height != self.__width: raise Exception("Matrix is not square" ) lowercase : Optional[int] = self.__matrix[:x] + self.__matrix[x + 1 :] for i in range(len(_a ) ): lowercase : List[str] = minor[i][:y] + minor[i][y + 1 :] return Matrix(_a , self.__width - 1 , self.__height - 1 ).determinant() def __magic_name__ ( self , _a , _a ): if self.__height != self.__width: raise Exception("Matrix is not square" ) if 0 <= x < self.__height and 0 <= y < self.__width: return (-1) ** (x + y) * self.minor(_a , _a ) else: raise Exception("Indices out of bounds" ) def __magic_name__ ( self ): if self.__height != self.__width: raise Exception("Matrix is not square" ) if self.__height < 1: raise Exception("Matrix has no element" ) elif self.__height == 1: return self.__matrix[0][0] elif self.__height == 2: return ( self.__matrix[0][0] * self.__matrix[1][1] - self.__matrix[0][1] * self.__matrix[1][0] ) else: lowercase : Tuple = [ self.__matrix[0][y] * self.cofactor(0 , _a ) for y in range(self.__width ) ] return sum(_a ) def __magic_name__ ( __snake_case : Optional[Any] ) -> Matrix: lowercase : list[list[float]] = [[0] * n for _ in range(_lowerCamelCase )] return Matrix(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) def __magic_name__ ( __snake_case : List[str] , __snake_case : Dict , __snake_case : Optional[int] , __snake_case : Optional[int] ) -> Matrix: random.seed(_lowerCamelCase ) lowercase : list[list[float]] = [ [random.randint(_lowerCamelCase , _lowerCamelCase ) for _ in range(_lowerCamelCase )] for _ in range(_lowerCamelCase ) ] return Matrix(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase )
361
'''simple docstring''' from __future__ import annotations def _a ( _lowerCamelCase , _lowerCamelCase = None , _lowerCamelCase = None ) -> None: """simple docstring""" if start is None: __snake_case : Optional[Any] = 0 if end is None: __snake_case : Optional[Any] = len(_lowerCamelCase ) - 1 if start >= end: return __snake_case : Tuple = (start + end) // 2 slowsort(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) slowsort(_lowerCamelCase , mid + 1 , _lowerCamelCase ) if sequence[end] < sequence[mid]: __snake_case , __snake_case : str = sequence[mid], sequence[end] slowsort(_lowerCamelCase , _lowerCamelCase , end - 1 ) if __name__ == "__main__": from doctest import testmod testmod()
26
0
from ... import PretrainedConfig lowerCamelCase : List[str] = { "sijunhe/nezha-cn-base": "https://huggingface.co/sijunhe/nezha-cn-base/resolve/main/config.json", } class A( __lowercase ): '''simple docstring''' UpperCamelCase = NEZHA_PRETRAINED_CONFIG_ARCHIVE_MAP UpperCamelCase = '''nezha''' def __init__( self : List[Any] , A_ : str=21128 , A_ : Tuple=768 , A_ : int=12 , A_ : List[str]=12 , A_ : List[str]=3072 , A_ : Tuple="gelu" , A_ : List[Any]=0.1 , A_ : Dict=0.1 , A_ : int=512 , A_ : List[str]=64 , A_ : Optional[Any]=2 , A_ : List[str]=0.02 , A_ : Dict=1E-12 , A_ : Any=0.1 , A_ : str=0 , A_ : List[Any]=2 , A_ : Tuple=3 , A_ : Union[str, Any]=True , **A_ : List[str] , ) -> List[str]: """simple docstring""" super().__init__(pad_token_id=A_ , bos_token_id=A_ , eos_token_id=A_ , **A_ ) lowerCamelCase_ = vocab_size lowerCamelCase_ = hidden_size lowerCamelCase_ = num_hidden_layers lowerCamelCase_ = num_attention_heads lowerCamelCase_ = hidden_act lowerCamelCase_ = intermediate_size lowerCamelCase_ = hidden_dropout_prob lowerCamelCase_ = attention_probs_dropout_prob lowerCamelCase_ = max_position_embeddings lowerCamelCase_ = max_relative_position lowerCamelCase_ = type_vocab_size lowerCamelCase_ = initializer_range lowerCamelCase_ = layer_norm_eps lowerCamelCase_ = classifier_dropout lowerCamelCase_ = use_cache
70
'''simple docstring''' import doctest import logging import os import unittest from pathlib import Path from typing import List, Union import transformers from transformers.testing_utils import require_tf, require_torch, slow __UpperCamelCase = logging.getLogger() @unittest.skip('''Temporarily disable the doc tests.''' ) @require_torch @require_tf @slow class _A ( unittest.TestCase ): def lowercase__ ( self : Optional[int] , __magic_name__ : Path , __magic_name__ : Union[str, None] = None , __magic_name__ : Union[List[str], None] = None , __magic_name__ : Union[str, List[str], None] = None , __magic_name__ : bool = True , ) -> Optional[int]: """simple docstring""" __snake_case : Union[str, Any] = [file for file in os.listdir(__magic_name__ ) if os.path.isfile(os.path.join(__magic_name__ , __magic_name__ ) )] if identifier is not None: __snake_case : List[Any] = [file for file in files if identifier in file] if n_identifier is not None: if isinstance(__magic_name__ , __magic_name__ ): for n_ in n_identifier: __snake_case : Optional[int] = [file for file in files if n_ not in file] else: __snake_case : Tuple = [file for file in files if n_identifier not in file] __snake_case : Dict = ignore_files or [] ignore_files.append("""__init__.py""" ) __snake_case : List[str] = [file for file in files if file not in ignore_files] for file in files: # Open all files print("""Testing""" , __magic_name__ ) if only_modules: __snake_case : List[Any] = file.split(""".""" )[0] try: __snake_case : List[Any] = getattr(__magic_name__ , __magic_name__ ) __snake_case : Union[str, Any] = doctest.DocTestSuite(__magic_name__ ) __snake_case : Dict = unittest.TextTestRunner().run(__magic_name__ ) self.assertIs(len(result.failures ) , 0 ) except AttributeError: logger.info(f'''{module_identifier} is not a module.''' ) else: __snake_case : Tuple = doctest.testfile(str("""..""" / directory / file ) , optionflags=doctest.ELLIPSIS ) self.assertIs(result.failed , 0 ) def lowercase__ ( self : Union[str, Any] ) -> Any: """simple docstring""" __snake_case : List[Any] = Path("""src/transformers""" ) __snake_case : List[Any] = """modeling""" __snake_case : Union[str, Any] = [ """modeling_ctrl.py""", """modeling_tf_ctrl.py""", ] self.analyze_directory(__magic_name__ , identifier=__magic_name__ , ignore_files=__magic_name__ ) def lowercase__ ( self : Union[str, Any] ) -> int: """simple docstring""" __snake_case : Union[str, Any] = Path("""src/transformers""" ) __snake_case : Any = """tokenization""" self.analyze_directory(__magic_name__ , identifier=__magic_name__ ) def lowercase__ ( self : Union[str, Any] ) -> int: """simple docstring""" __snake_case : List[Any] = Path("""src/transformers""" ) __snake_case : List[str] = """configuration""" self.analyze_directory(__magic_name__ , identifier=__magic_name__ ) def lowercase__ ( self : Dict ) -> Dict: """simple docstring""" __snake_case : Tuple = Path("""src/transformers""" ) __snake_case : int = ["""configuration""", """modeling""", """tokenization"""] self.analyze_directory(__magic_name__ , n_identifier=__magic_name__ ) def lowercase__ ( self : Optional[int] ) -> Union[str, Any]: """simple docstring""" __snake_case : int = Path("""docs/source""" ) __snake_case : Optional[int] = ["""favicon.ico"""] self.analyze_directory(__magic_name__ , ignore_files=__magic_name__ , only_modules=__magic_name__ )
26
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() UpperCAmelCase_ : Union[str, Any] = logging.get_logger(__name__) def _A (__a ) -> Any: """simple docstring""" SCREAMING_SNAKE_CASE_ : Union[str, Any] = MobileNetVaConfig(layer_norm_eps=0.0_01 ) if "_quant" in model_name: raise ValueError('''Quantized models are not supported.''' ) SCREAMING_SNAKE_CASE_ : List[Any] = re.match(R'''^mobilenet_v1_([^_]*)_([^_]*)$''' , _lowerCamelCase ) if matches: SCREAMING_SNAKE_CASE_ : Optional[Any] = float(matches[1] ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = int(matches[2] ) # The TensorFlow version of MobileNetV1 predicts 1001 classes instead of # the usual 1000. The first class (index 0) is "background". SCREAMING_SNAKE_CASE_ : Tuple = 10_01 SCREAMING_SNAKE_CASE_ : Any = """imagenet-1k-id2label.json""" SCREAMING_SNAKE_CASE_ : Optional[Any] = """huggingface/label-files""" SCREAMING_SNAKE_CASE_ : List[Any] = json.load(open(hf_hub_download(_lowerCamelCase , _lowerCamelCase , repo_type='''dataset''' ) , '''r''' ) ) SCREAMING_SNAKE_CASE_ : Dict = {int(_lowerCamelCase ) + 1: v for k, v in idalabel.items()} SCREAMING_SNAKE_CASE_ : List[str] = """background""" SCREAMING_SNAKE_CASE_ : List[str] = idalabel SCREAMING_SNAKE_CASE_ : List[Any] = {v: k for k, v in idalabel.items()} return config def _A () -> Optional[Any]: """simple docstring""" SCREAMING_SNAKE_CASE_ : Optional[int] = """http://images.cocodataset.org/val2017/000000039769.jpg""" SCREAMING_SNAKE_CASE_ : List[Any] = Image.open(requests.get(_lowerCamelCase , stream=_lowerCamelCase ).raw ) return im @torch.no_grad() def _A (__a , __a , __a , __a=False ) -> Optional[Any]: """simple docstring""" SCREAMING_SNAKE_CASE_ : Optional[int] = get_mobilenet_va_config(_lowerCamelCase ) # Load 🤗 model SCREAMING_SNAKE_CASE_ : Optional[Any] = MobileNetVaForImageClassification(_lowerCamelCase ).eval() # Load weights from TensorFlow checkpoint load_tf_weights_in_mobilenet_va(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) # Check outputs on an image, prepared by MobileNetV1ImageProcessor SCREAMING_SNAKE_CASE_ : Optional[int] = MobileNetVaImageProcessor( crop_size={'''width''': config.image_size, '''height''': config.image_size} , size={'''shortest_edge''': config.image_size + 32} , ) SCREAMING_SNAKE_CASE_ : Tuple = image_processor(images=prepare_img() , return_tensors='''pt''' ) SCREAMING_SNAKE_CASE_ : Optional[Any] = model(**_lowerCamelCase ) SCREAMING_SNAKE_CASE_ : List[Any] = outputs.logits assert logits.shape == (1, 10_01) if model_name == "mobilenet_v1_1.0_224": SCREAMING_SNAKE_CASE_ : str = torch.tensor([-4.17_39, -1.12_33, 3.12_05] ) elif model_name == "mobilenet_v1_0.75_192": SCREAMING_SNAKE_CASE_ : Tuple = torch.tensor([-3.94_40, -2.31_41, -0.33_33] ) else: SCREAMING_SNAKE_CASE_ : List[Any] = None if expected_logits is not None: assert torch.allclose(logits[0, :3] , _lowerCamelCase , atol=1e-4 ) Path(_lowerCamelCase ).mkdir(exist_ok=_lowerCamelCase ) print(f'Saving model {model_name} to {pytorch_dump_folder_path}' ) model.save_pretrained(_lowerCamelCase ) print(f'Saving image processor to {pytorch_dump_folder_path}' ) image_processor.save_pretrained(_lowerCamelCase ) if push_to_hub: print('''Pushing to the hub...''' ) SCREAMING_SNAKE_CASE_ : Optional[Any] = """google/""" + model_name image_processor.push_to_hub(_lowerCamelCase ) model.push_to_hub(_lowerCamelCase ) if __name__ == "__main__": UpperCAmelCase_ : int = 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.""" ) UpperCAmelCase_ : int = parser.parse_args() convert_movilevit_checkpoint( args.model_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub )
512
'''simple docstring''' import inspect from typing import Callable, List, Optional, Union import torch from transformers import ( CLIPImageProcessor, CLIPTextModel, CLIPTokenizer, WhisperForConditionalGeneration, WhisperProcessor, ) from diffusers import ( AutoencoderKL, DDIMScheduler, DiffusionPipeline, LMSDiscreteScheduler, PNDMScheduler, UNetaDConditionModel, ) from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion import StableDiffusionPipelineOutput from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker from diffusers.utils import logging __UpperCamelCase = logging.get_logger(__name__) # pylint: disable=invalid-name class _A ( __lowercase ): def __init__( self : str , __magic_name__ : WhisperForConditionalGeneration , __magic_name__ : WhisperProcessor , __magic_name__ : AutoencoderKL , __magic_name__ : CLIPTextModel , __magic_name__ : CLIPTokenizer , __magic_name__ : UNetaDConditionModel , __magic_name__ : Union[DDIMScheduler, PNDMScheduler, LMSDiscreteScheduler] , __magic_name__ : StableDiffusionSafetyChecker , __magic_name__ : CLIPImageProcessor , ) -> Union[str, Any]: """simple docstring""" super().__init__() if safety_checker is None: logger.warning( f'''You have disabled the safety checker for {self.__class__} by passing `safety_checker=None`. Ensure''' """ that you abide to the conditions of the Stable Diffusion license and do not expose unfiltered""" """ results in services or applications open to the public. Both the diffusers team and Hugging Face""" """ strongly recommend to keep the safety filter enabled in all public facing circumstances, disabling""" """ it only for use-cases that involve analyzing network behavior or auditing its results. For more""" """ information, please have a look at https://github.com/huggingface/diffusers/pull/254 .""" ) self.register_modules( speech_model=__magic_name__ , speech_processor=__magic_name__ , vae=__magic_name__ , text_encoder=__magic_name__ , tokenizer=__magic_name__ , unet=__magic_name__ , scheduler=__magic_name__ , feature_extractor=__magic_name__ , ) def lowercase__ ( self : Optional[Any] , __magic_name__ : Optional[Union[str, int]] = "auto" ) -> Union[str, Any]: """simple docstring""" if slice_size == "auto": __snake_case : str = self.unet.config.attention_head_dim // 2 self.unet.set_attention_slice(__magic_name__ ) def lowercase__ ( self : str ) -> Any: """simple docstring""" self.enable_attention_slicing(__magic_name__ ) @torch.no_grad() def __call__( self : Optional[int] , __magic_name__ : str , __magic_name__ : Dict=1_60_00 , __magic_name__ : int = 5_12 , __magic_name__ : int = 5_12 , __magic_name__ : int = 50 , __magic_name__ : float = 7.5 , __magic_name__ : Optional[Union[str, List[str]]] = None , __magic_name__ : Optional[int] = 1 , __magic_name__ : float = 0.0 , __magic_name__ : Optional[torch.Generator] = None , __magic_name__ : Optional[torch.FloatTensor] = None , __magic_name__ : Optional[str] = "pil" , __magic_name__ : bool = True , __magic_name__ : Optional[Callable[[int, int, torch.FloatTensor], None]] = None , __magic_name__ : int = 1 , **__magic_name__ : List[str] , ) -> int: """simple docstring""" __snake_case : List[Any] = self.speech_processor.feature_extractor( __magic_name__ , return_tensors="""pt""" , sampling_rate=__magic_name__ ).input_features.to(self.device ) __snake_case : List[str] = self.speech_model.generate(__magic_name__ , max_length=48_00_00 ) __snake_case : List[Any] = self.speech_processor.tokenizer.batch_decode(__magic_name__ , skip_special_tokens=__magic_name__ , normalize=__magic_name__ )[ 0 ] if isinstance(__magic_name__ , __magic_name__ ): __snake_case : Tuple = 1 elif isinstance(__magic_name__ , __magic_name__ ): __snake_case : Optional[int] = len(__magic_name__ ) else: raise ValueError(f'''`prompt` has to be of type `str` or `list` but is {type(__magic_name__ )}''' ) if height % 8 != 0 or width % 8 != 0: raise ValueError(f'''`height` and `width` have to be divisible by 8 but are {height} and {width}.''' ) if (callback_steps is None) or ( callback_steps is not None and (not isinstance(__magic_name__ , __magic_name__ ) or callback_steps <= 0) ): raise ValueError( f'''`callback_steps` has to be a positive integer but is {callback_steps} of type''' f''' {type(__magic_name__ )}.''' ) # get prompt text embeddings __snake_case : Dict = self.tokenizer( __magic_name__ , padding="""max_length""" , max_length=self.tokenizer.model_max_length , return_tensors="""pt""" , ) __snake_case : Optional[Any] = text_inputs.input_ids if text_input_ids.shape[-1] > self.tokenizer.model_max_length: __snake_case : Tuple = self.tokenizer.batch_decode(text_input_ids[:, self.tokenizer.model_max_length :] ) logger.warning( """The following part of your input was truncated because CLIP can only handle sequences up to""" f''' {self.tokenizer.model_max_length} tokens: {removed_text}''' ) __snake_case : Any = text_input_ids[:, : self.tokenizer.model_max_length] __snake_case : int = self.text_encoder(text_input_ids.to(self.device ) )[0] # duplicate text embeddings for each generation per prompt, using mps friendly method __snake_case , __snake_case , __snake_case : Any = text_embeddings.shape __snake_case : List[Any] = text_embeddings.repeat(1 , __magic_name__ , 1 ) __snake_case : Dict = text_embeddings.view(bs_embed * num_images_per_prompt , __magic_name__ , -1 ) # here `guidance_scale` is defined analog to the guidance weight `w` of equation (2) # of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1` # corresponds to doing no classifier free guidance. __snake_case : Optional[int] = guidance_scale > 1.0 # get unconditional embeddings for classifier free guidance if do_classifier_free_guidance: __snake_case : List[str] if negative_prompt is None: __snake_case : Optional[Any] = [""""""] * batch_size elif type(__magic_name__ ) is not type(__magic_name__ ): raise TypeError( f'''`negative_prompt` should be the same type to `prompt`, but got {type(__magic_name__ )} !=''' f''' {type(__magic_name__ )}.''' ) elif isinstance(__magic_name__ , __magic_name__ ): __snake_case : Dict = [negative_prompt] elif batch_size != len(__magic_name__ ): raise ValueError( f'''`negative_prompt`: {negative_prompt} has batch size {len(__magic_name__ )}, but `prompt`:''' f''' {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches''' """ the batch size of `prompt`.""" ) else: __snake_case : int = negative_prompt __snake_case : List[str] = text_input_ids.shape[-1] __snake_case : Any = self.tokenizer( __magic_name__ , padding="""max_length""" , max_length=__magic_name__ , truncation=__magic_name__ , return_tensors="""pt""" , ) __snake_case : Dict = self.text_encoder(uncond_input.input_ids.to(self.device ) )[0] # duplicate unconditional embeddings for each generation per prompt, using mps friendly method __snake_case : Optional[int] = uncond_embeddings.shape[1] __snake_case : Union[str, Any] = uncond_embeddings.repeat(1 , __magic_name__ , 1 ) __snake_case : Tuple = uncond_embeddings.view(batch_size * num_images_per_prompt , __magic_name__ , -1 ) # For classifier free guidance, we need to do two forward passes. # Here we concatenate the unconditional and text embeddings into a single batch # to avoid doing two forward passes __snake_case : Dict = torch.cat([uncond_embeddings, text_embeddings] ) # get the initial random noise unless the user supplied it # Unlike in other pipelines, latents need to be generated in the target device # for 1-to-1 results reproducibility with the CompVis implementation. # However this currently doesn't work in `mps`. __snake_case : List[Any] = (batch_size * num_images_per_prompt, self.unet.config.in_channels, height // 8, width // 8) __snake_case : List[Any] = text_embeddings.dtype if latents is None: if self.device.type == "mps": # randn does not exist on mps __snake_case : Optional[int] = torch.randn(__magic_name__ , generator=__magic_name__ , device="""cpu""" , dtype=__magic_name__ ).to( self.device ) else: __snake_case : int = torch.randn(__magic_name__ , generator=__magic_name__ , device=self.device , dtype=__magic_name__ ) else: if latents.shape != latents_shape: raise ValueError(f'''Unexpected latents shape, got {latents.shape}, expected {latents_shape}''' ) __snake_case : List[str] = latents.to(self.device ) # set timesteps self.scheduler.set_timesteps(__magic_name__ ) # Some schedulers like PNDM have timesteps as arrays # It's more optimized to move all timesteps to correct device beforehand __snake_case : Optional[int] = self.scheduler.timesteps.to(self.device ) # scale the initial noise by the standard deviation required by the scheduler __snake_case : str = latents * self.scheduler.init_noise_sigma # prepare extra kwargs for the scheduler step, since not all schedulers have the same signature # eta (η) is only used with the DDIMScheduler, it will be ignored for other schedulers. # eta corresponds to η in DDIM paper: https://arxiv.org/abs/2010.02502 # and should be between [0, 1] __snake_case : Tuple = """eta""" in set(inspect.signature(self.scheduler.step ).parameters.keys() ) __snake_case : List[str] = {} if accepts_eta: __snake_case : str = eta for i, t in enumerate(self.progress_bar(__magic_name__ ) ): # expand the latents if we are doing classifier free guidance __snake_case : Union[str, Any] = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents __snake_case : Dict = self.scheduler.scale_model_input(__magic_name__ , __magic_name__ ) # predict the noise residual __snake_case : Tuple = self.unet(__magic_name__ , __magic_name__ , encoder_hidden_states=__magic_name__ ).sample # perform guidance if do_classifier_free_guidance: __snake_case , __snake_case : str = noise_pred.chunk(2 ) __snake_case : Any = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) # compute the previous noisy sample x_t -> x_t-1 __snake_case : Optional[Any] = self.scheduler.step(__magic_name__ , __magic_name__ , __magic_name__ , **__magic_name__ ).prev_sample # call the callback, if provided if callback is not None and i % callback_steps == 0: callback(__magic_name__ , __magic_name__ , __magic_name__ ) __snake_case : int = 1 / 0.18215 * latents __snake_case : Optional[Any] = self.vae.decode(__magic_name__ ).sample __snake_case : Any = (image / 2 + 0.5).clamp(0 , 1 ) # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16 __snake_case : Any = image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() if output_type == "pil": __snake_case : Tuple = self.numpy_to_pil(__magic_name__ ) if not return_dict: return image return StableDiffusionPipelineOutput(images=__magic_name__ , nsfw_content_detected=__magic_name__ )
26
0
from unittest import TestCase from datasets import Sequence, Value from datasets.arrow_dataset import Dataset class lowercase__ ( __lowercase ): def _UpperCAmelCase ( self : Any ): """simple docstring""" return [ {"col_1": 3, "col_2": "a"}, {"col_1": 2, "col_2": "b"}, {"col_1": 1, "col_2": "c"}, {"col_1": 0, "col_2": "d"}, ] def _UpperCAmelCase ( self : str ): """simple docstring""" UpperCAmelCase__ = {"""col_1""": [3, 2, 1, 0], """col_2""": ["""a""", """b""", """c""", """d"""]} return Dataset.from_dict(_lowercase ) def _UpperCAmelCase ( self : str ): """simple docstring""" UpperCAmelCase__ = self._create_example_records() UpperCAmelCase__ = Dataset.from_list(_lowercase ) self.assertListEqual(dset.column_names , ["col_1", "col_2"] ) for i, r in enumerate(_lowercase ): self.assertDictEqual(_lowercase , example_records[i] ) def _UpperCAmelCase ( self : Optional[Any] ): """simple docstring""" UpperCAmelCase__ = self._create_example_records() UpperCAmelCase__ = Dataset.from_list(_lowercase ) UpperCAmelCase__ = Dataset.from_dict({k: [r[k] for r in example_records] for k in example_records[0]} ) self.assertEqual(dset.info , dset_from_dict.info ) def _UpperCAmelCase ( self : str ): # checks what happens with missing columns """simple docstring""" UpperCAmelCase__ = [{"""col_1""": 1}, {"""col_2""": """x"""}] UpperCAmelCase__ = Dataset.from_list(_lowercase ) self.assertDictEqual(dset[0] , {"col_1": 1} ) self.assertDictEqual(dset[1] , {"col_1": None} ) # NB: first record is used for columns def _UpperCAmelCase ( self : List[str] ): # checks if the type can be inferred from the second record """simple docstring""" UpperCAmelCase__ = [{"""col_1""": []}, {"""col_1""": [1, 2]}] UpperCAmelCase__ = Dataset.from_list(_lowercase ) self.assertEqual(dset.info.features["col_1"] , Sequence(Value("int64" ) ) ) def _UpperCAmelCase ( self : int ): """simple docstring""" UpperCAmelCase__ = Dataset.from_list([] ) self.assertEqual(len(_lowercase ) , 0 ) self.assertListEqual(dset.column_names , [] )
475
'''simple docstring''' import os from huggingface_hub.constants import HUGGINGFACE_HUB_CACHE, hf_cache_home __UpperCamelCase = HUGGINGFACE_HUB_CACHE __UpperCamelCase = "config.json" __UpperCamelCase = "diffusion_pytorch_model.bin" __UpperCamelCase = "diffusion_flax_model.msgpack" __UpperCamelCase = "model.onnx" __UpperCamelCase = "diffusion_pytorch_model.safetensors" __UpperCamelCase = "weights.pb" __UpperCamelCase = "https://huggingface.co" __UpperCamelCase = default_cache_path __UpperCamelCase = "diffusers_modules" __UpperCamelCase = os.getenv("HF_MODULES_CACHE", os.path.join(hf_cache_home, "modules")) __UpperCamelCase = ["fp16", "non-ema"] __UpperCamelCase = ".self_attn"
26
0
import glob import os import random from string import ascii_lowercase, digits import cva A : Tuple = '' A : int = '' A : Optional[Any] = '' A : Tuple = 1 # (0 is vertical, 1 is horizontal) def _lowerCAmelCase ( ) -> None: '''simple docstring''' __snake_case = get_dataset(_lowerCamelCase , _lowerCamelCase ) print("Processing..." ) __snake_case = update_image_and_anno(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) for index, image in enumerate(_lowerCamelCase ): # Get random string code: '7b7ad245cdff75241935e4dd860f3bad' __snake_case = random_chars(32 ) __snake_case = paths[index].split(os.sep )[-1].rsplit("." , 1 )[0] __snake_case = F'''{OUTPUT_DIR}/{file_name}_FLIP_{letter_code}''' cva.imwrite(F'''/{file_root}.jpg''' , _lowerCamelCase , [cva.IMWRITE_JPEG_QUALITY, 85] ) print(F'''Success {index+1}/{len(_lowerCamelCase )} with {file_name}''' ) __snake_case = [] for anno in new_annos[index]: __snake_case = F'''{anno[0]} {anno[1]} {anno[2]} {anno[3]} {anno[4]}''' annos_list.append(_lowerCamelCase ) with open(F'''/{file_root}.txt''' , "w" ) as outfile: outfile.write("\n".join(line for line in annos_list ) ) def _lowerCAmelCase ( _lowerCAmelCase , _lowerCAmelCase ) -> tuple[list, list]: '''simple docstring''' __snake_case = [] __snake_case = [] for label_file in glob.glob(os.path.join(_lowerCamelCase , "*.txt" ) ): __snake_case = label_file.split(os.sep )[-1].rsplit("." , 1 )[0] with open(_lowerCamelCase ) as in_file: __snake_case = in_file.readlines() __snake_case = os.path.join(_lowerCamelCase , F'''{label_name}.jpg''' ) __snake_case = [] for obj_list in obj_lists: __snake_case = obj_list.rstrip("\n" ).split(" " ) boxes.append( [ int(obj[0] ), float(obj[1] ), float(obj[2] ), float(obj[3] ), float(obj[4] ), ] ) if not boxes: continue img_paths.append(_lowerCamelCase ) labels.append(_lowerCamelCase ) return img_paths, labels def _lowerCAmelCase ( _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase = 1 ) -> tuple[list, list, list]: '''simple docstring''' __snake_case = [] __snake_case = [] __snake_case = [] for idx in range(len(_lowerCamelCase ) ): __snake_case = [] __snake_case = img_list[idx] path_list.append(_lowerCamelCase ) __snake_case = anno_list[idx] __snake_case = cva.imread(_lowerCamelCase ) if flip_type == 1: __snake_case = cva.flip(_lowerCamelCase , _lowerCamelCase ) for bbox in img_annos: __snake_case = 1 - bbox[1] new_annos.append([bbox[0], x_center_new, bbox[2], bbox[3], bbox[4]] ) elif flip_type == 0: __snake_case = cva.flip(_lowerCamelCase , _lowerCamelCase ) for bbox in img_annos: __snake_case = 1 - bbox[2] new_annos.append([bbox[0], bbox[1], y_center_new, bbox[3], bbox[4]] ) new_annos_lists.append(_lowerCamelCase ) new_imgs_list.append(_lowerCamelCase ) return new_imgs_list, new_annos_lists, path_list def _lowerCAmelCase ( _lowerCAmelCase = 32 ) -> str: '''simple docstring''' assert number_char > 1, "The number of character should greater than 1" __snake_case = ascii_lowercase + digits return "".join(random.choice(_lowerCamelCase ) for _ in range(_lowerCamelCase ) ) if __name__ == "__main__": main() print('DONE ✅')
371
'''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() __UpperCamelCase = logging.get_logger(__name__) def _a ( _lowerCamelCase ) -> Any: """simple docstring""" __snake_case : Union[str, Any] = MobileNetVaConfig(layer_norm_eps=0.0_01 ) if "_quant" in model_name: raise ValueError("""Quantized models are not supported.""" ) __snake_case : List[Any] = re.match(R"""^mobilenet_v1_([^_]*)_([^_]*)$""" , _lowerCamelCase ) if matches: __snake_case : Optional[Any] = float(matches[1] ) __snake_case : Union[str, Any] = int(matches[2] ) # The TensorFlow version of MobileNetV1 predicts 1001 classes instead of # the usual 1000. The first class (index 0) is "background". __snake_case : Tuple = 1001 __snake_case : Any = """imagenet-1k-id2label.json""" __snake_case : Optional[Any] = """huggingface/label-files""" __snake_case : List[Any] = json.load(open(hf_hub_download(_lowerCamelCase , _lowerCamelCase , repo_type="""dataset""" ) , """r""" ) ) __snake_case : Dict = {int(_lowerCamelCase ) + 1: v for k, v in idalabel.items()} __snake_case : List[str] = """background""" __snake_case : List[str] = idalabel __snake_case : List[Any] = {v: k for k, v in idalabel.items()} return config def _a ( ) -> Optional[Any]: """simple docstring""" __snake_case : Optional[int] = """http://images.cocodataset.org/val2017/000000039769.jpg""" __snake_case : List[Any] = Image.open(requests.get(_lowerCamelCase , stream=_lowerCamelCase ).raw ) return im @torch.no_grad() def _a ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase=False ) -> Optional[Any]: """simple docstring""" __snake_case : Optional[int] = get_mobilenet_va_config(_lowerCamelCase ) # Load 🤗 model __snake_case : Optional[Any] = MobileNetVaForImageClassification(_lowerCamelCase ).eval() # Load weights from TensorFlow checkpoint load_tf_weights_in_mobilenet_va(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) # Check outputs on an image, prepared by MobileNetV1ImageProcessor __snake_case : Optional[int] = MobileNetVaImageProcessor( crop_size={"""width""": config.image_size, """height""": config.image_size} , size={"""shortest_edge""": config.image_size + 32} , ) __snake_case : Tuple = image_processor(images=prepare_img() , return_tensors="""pt""" ) __snake_case : Optional[Any] = model(**_lowerCamelCase ) __snake_case : List[Any] = outputs.logits assert logits.shape == (1, 1001) if model_name == "mobilenet_v1_1.0_224": __snake_case : str = torch.tensor([-4.17_39, -1.12_33, 3.12_05] ) elif model_name == "mobilenet_v1_0.75_192": __snake_case : Tuple = torch.tensor([-3.94_40, -2.31_41, -0.33_33] ) else: __snake_case : List[Any] = None if expected_logits is not None: assert torch.allclose(logits[0, :3] , _lowerCamelCase , atol=1E-4 ) Path(_lowerCamelCase ).mkdir(exist_ok=_lowerCamelCase ) print(F'''Saving model {model_name} to {pytorch_dump_folder_path}''' ) model.save_pretrained(_lowerCamelCase ) print(F'''Saving image processor to {pytorch_dump_folder_path}''' ) image_processor.save_pretrained(_lowerCamelCase ) if push_to_hub: print("""Pushing to the hub...""" ) __snake_case : Optional[Any] = """google/""" + model_name image_processor.push_to_hub(_lowerCamelCase ) model.push_to_hub(_lowerCamelCase ) if __name__ == "__main__": __UpperCamelCase = 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." ) __UpperCamelCase = parser.parse_args() convert_movilevit_checkpoint( args.model_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub )
26
0
import operator as op def _lowercase ( UpperCAmelCase_): """simple docstring""" snake_case__ : List[Any] = [] snake_case__ : Optional[int] = lambda UpperCAmelCase_ , UpperCAmelCase_: int(x / y) # noqa: E731 integer division operation snake_case__ : List[Any] = { """^""": op.pow, """*""": op.mul, """/""": div, """+""": op.add, """-""": op.sub, } # operators & their respective operation # print table header print("""Symbol""".center(8) , """Action""".center(12) , """Stack""" , sep=""" | """) print("""-""" * (30 + len(_lowerCamelCase))) for x in post_fix: if x.isdigit(): # if x in digit stack.append(_lowerCamelCase) # append x to stack # output in tabular format print(x.rjust(8) , ("""push(""" + x + """)""").ljust(12) , """,""".join(_lowerCamelCase) , sep=""" | """) else: snake_case__ : List[str] = stack.pop() # pop stack # output in tabular format print("""""".rjust(8) , ("""pop(""" + b + """)""").ljust(12) , """,""".join(_lowerCamelCase) , sep=""" | """) snake_case__ : Dict = stack.pop() # pop stack # output in tabular format print("""""".rjust(8) , ("""pop(""" + a + """)""").ljust(12) , """,""".join(_lowerCamelCase) , sep=""" | """) stack.append( str(opr[x](int(_lowerCamelCase) , int(_lowerCamelCase)))) # evaluate the 2 values popped from stack & push result to stack # output in tabular format print( x.rjust(8) , ("""push(""" + a + x + b + """)""").ljust(12) , """,""".join(_lowerCamelCase) , sep=""" | """ , ) return int(stack[0]) if __name__ == "__main__": lowercase_: str = input('\n\nEnter a Postfix Equation (space separated) = ').split(' ') print('\n\tResult = ', solve(Postfix))
648
'''simple docstring''' from sklearn.metrics import recall_score import datasets __UpperCamelCase = "\nRecall is the fraction of the positive examples that were correctly labeled by the model as positive. It can be computed with the equation:\nRecall = TP / (TP + FN)\nWhere TP is the true positives and FN is the false negatives.\n" __UpperCamelCase = "\nArgs:\n- **predictions** (`list` of `int`): The predicted labels.\n- **references** (`list` of `int`): The ground truth labels.\n- **labels** (`list` of `int`): The set of labels to include when `average` is not set to `binary`, and their order when average is `None`. Labels present in the data can be excluded in this input, for example to calculate a multiclass average ignoring a majority negative class, while labels not present in the data will result in 0 components in a macro average. For multilabel targets, labels are column indices. By default, all labels in y_true and y_pred are used in sorted order. Defaults to None.\n- **pos_label** (`int`): The class label to use as the 'positive class' when calculating the recall. Defaults to `1`.\n- **average** (`string`): This parameter is required for multiclass/multilabel targets. If None, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data. Defaults to `'binary'`.\n - `'binary'`: Only report results for the class specified by `pos_label`. This is applicable only if the target labels and predictions are binary.\n - `'micro'`: Calculate metrics globally by counting the total true positives, false negatives, and false positives.\n - `'macro'`: Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account.\n - `'weighted'`: Calculate metrics for each label, and find their average weighted by support (the number of true instances for each label). This alters `'macro'` to account for label imbalance. Note that it can result in an F-score that is not between precision and recall.\n - `'samples'`: Calculate metrics for each instance, and find their average (only meaningful for multilabel classification).\n- **sample_weight** (`list` of `float`): Sample weights Defaults to `None`.\n- **zero_division** (): Sets the value to return when there is a zero division. Defaults to .\n - `'warn'`: If there is a zero division, the return value is `0`, but warnings are also raised.\n - `0`: If there is a zero division, the return value is `0`.\n - `1`: If there is a zero division, the return value is `1`.\n\nReturns:\n- **recall** (`float`, or `array` of `float`): Either the general recall score, or the recall scores for individual classes, depending on the values input to `labels` and `average`. Minimum possible value is 0. Maximum possible value is 1. A higher recall means that more of the positive examples have been labeled correctly. Therefore, a higher recall is generally considered better.\n\nExamples:\n\n Example 1-A simple example with some errors\n >>> recall_metric = datasets.load_metric('recall')\n >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1])\n >>> print(results)\n {'recall': 0.6666666666666666}\n\n Example 2-The same example as Example 1, but with `pos_label=0` instead of the default `pos_label=1`.\n >>> recall_metric = datasets.load_metric('recall')\n >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1], pos_label=0)\n >>> print(results)\n {'recall': 0.5}\n\n Example 3-The same example as Example 1, but with `sample_weight` included.\n >>> recall_metric = datasets.load_metric('recall')\n >>> sample_weight = [0.9, 0.2, 0.9, 0.3, 0.8]\n >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1], sample_weight=sample_weight)\n >>> print(results)\n {'recall': 0.55}\n\n Example 4-A multiclass example, using different averages.\n >>> recall_metric = datasets.load_metric('recall')\n >>> predictions = [0, 2, 1, 0, 0, 1]\n >>> references = [0, 1, 2, 0, 1, 2]\n >>> results = recall_metric.compute(predictions=predictions, references=references, average='macro')\n >>> print(results)\n {'recall': 0.3333333333333333}\n >>> results = recall_metric.compute(predictions=predictions, references=references, average='micro')\n >>> print(results)\n {'recall': 0.3333333333333333}\n >>> results = recall_metric.compute(predictions=predictions, references=references, average='weighted')\n >>> print(results)\n {'recall': 0.3333333333333333}\n >>> results = recall_metric.compute(predictions=predictions, references=references, average=None)\n >>> print(results)\n {'recall': array([1., 0., 0.])}\n" __UpperCamelCase = "\n@article{scikit-learn, title={Scikit-learn: Machine Learning in {P}ython}, author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V. and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P. and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.}, journal={Journal of Machine Learning Research}, volume={12}, pages={2825--2830}, year={2011}\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class _A ( datasets.Metric ): def lowercase__ ( self : Optional[int] ) -> Any: """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": datasets.Sequence(datasets.Value("""int32""" ) ), """references""": datasets.Sequence(datasets.Value("""int32""" ) ), } if self.config_name == """multilabel""" else { """predictions""": datasets.Value("""int32""" ), """references""": datasets.Value("""int32""" ), } ) , reference_urls=["""https://scikit-learn.org/stable/modules/generated/sklearn.metrics.recall_score.html"""] , ) def lowercase__ ( self : Tuple , __magic_name__ : int , __magic_name__ : Union[str, Any] , __magic_name__ : Any=None , __magic_name__ : Optional[Any]=1 , __magic_name__ : List[str]="binary" , __magic_name__ : Tuple=None , __magic_name__ : Dict="warn" , ) -> Any: """simple docstring""" __snake_case : Tuple = recall_score( __magic_name__ , __magic_name__ , labels=__magic_name__ , pos_label=__magic_name__ , average=__magic_name__ , sample_weight=__magic_name__ , zero_division=__magic_name__ , ) return {"recall": float(__magic_name__ ) if score.size == 1 else score}
26
0
"""simple docstring""" import warnings from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class snake_case_ ( __lowercase ): __lowerCAmelCase = ['''image_processor''', '''tokenizer'''] __lowerCAmelCase = '''CLIPImageProcessor''' __lowerCAmelCase = ('''CLIPTokenizer''', '''CLIPTokenizerFast''') def __init__( self , a_=None , a_=None , **a_ ): a_ : Optional[Any] = None if "feature_extractor" in kwargs: warnings.warn( "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`" " instead." , a_ , ) a_ : List[Any] = kwargs.pop("feature_extractor" ) a_ : List[str] = 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__(a_ , a_ ) def __call__( self , a_=None , a_=None , a_=None , **a_ ): 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: a_ : int = self.tokenizer(a_ , return_tensors=a_ , **a_ ) if images is not None: a_ : str = self.image_processor(a_ , return_tensors=a_ , **a_ ) if text is not None and images is not None: a_ : Union[str, Any] = image_features.pixel_values return encoding elif text is not None: return encoding else: return BatchEncoding(data=dict(**a_ ) , tensor_type=a_ ) def snake_case_ ( self , *a_ , **a_ ): return self.tokenizer.batch_decode(*a_ , **a_ ) def snake_case_ ( self , *a_ , **a_ ): return self.tokenizer.decode(*a_ , **a_ ) @property def snake_case_ ( self ): a_ : Dict = self.tokenizer.model_input_names a_ : str = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) ) @property def snake_case_ ( self ): warnings.warn( "`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead." , a_ , ) return self.image_processor_class @property def snake_case_ ( self ): warnings.warn( "`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead." , a_ , ) return self.image_processor
237
'''simple docstring''' from sklearn.metrics import matthews_corrcoef import datasets __UpperCamelCase = "\nCompute the Matthews correlation coefficient (MCC)\n\nThe Matthews correlation coefficient is used in machine learning as a\nmeasure of the quality of binary and multiclass classifications. It takes\ninto account true and false positives and negatives and is generally\nregarded as a balanced measure which can be used even if the classes are of\nvery different sizes. The MCC is in essence a correlation coefficient value\nbetween -1 and +1. A coefficient of +1 represents a perfect prediction, 0\nan average random prediction and -1 an inverse prediction. The statistic\nis also known as the phi coefficient. [source: Wikipedia]\n" __UpperCamelCase = "\nArgs:\n predictions (list of int): Predicted labels, as returned by a model.\n references (list of int): Ground truth labels.\n sample_weight (list of int, float, or bool): Sample weights. Defaults to `None`.\nReturns:\n matthews_correlation (dict containing float): Matthews correlation.\nExamples:\n Example 1, a basic example with only predictions and references as inputs:\n >>> matthews_metric = datasets.load_metric(\"matthews_correlation\")\n >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],\n ... predictions=[1, 2, 2, 0, 3, 3])\n >>> print(round(results['matthews_correlation'], 2))\n 0.54\n\n Example 2, the same example as above, but also including sample weights:\n >>> matthews_metric = datasets.load_metric(\"matthews_correlation\")\n >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],\n ... predictions=[1, 2, 2, 0, 3, 3],\n ... sample_weight=[0.5, 3, 1, 1, 1, 2])\n >>> print(round(results['matthews_correlation'], 2))\n 0.1\n\n Example 3, the same example as above, but with sample weights that cause a negative correlation:\n >>> matthews_metric = datasets.load_metric(\"matthews_correlation\")\n >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],\n ... predictions=[1, 2, 2, 0, 3, 3],\n ... sample_weight=[0.5, 1, 0, 0, 0, 1])\n >>> print(round(results['matthews_correlation'], 2))\n -0.25\n" __UpperCamelCase = "\\n@article{scikit-learn,\n title={Scikit-learn: Machine Learning in {P}ython},\n author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V.\n and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P.\n and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and\n Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.},\n journal={Journal of Machine Learning Research},\n volume={12},\n pages={2825--2830},\n year={2011}\n}\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class _A ( datasets.Metric ): def lowercase__ ( self : Tuple ) -> Dict: """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": datasets.Value("""int32""" ), """references""": datasets.Value("""int32""" ), } ) , reference_urls=[ """https://scikit-learn.org/stable/modules/generated/sklearn.metrics.matthews_corrcoef.html""" ] , ) def lowercase__ ( self : List[Any] , __magic_name__ : Tuple , __magic_name__ : List[Any] , __magic_name__ : Union[str, Any]=None ) -> Optional[int]: """simple docstring""" return { "matthews_correlation": float(matthews_corrcoef(__magic_name__ , __magic_name__ , sample_weight=__magic_name__ ) ), }
26
0
"""simple docstring""" import multiprocessing import os from typing import BinaryIO, Optional, Union import fsspec from .. import Dataset, Features, NamedSplit, config from ..formatting import query_table from ..packaged_modules.json.json import Json from ..utils import logging from ..utils.typing import NestedDataStructureLike, PathLike from .abc import AbstractDatasetReader class a__ ( __lowercase ): def __init__( self : Union[str, Any] ,a__ : NestedDataStructureLike[PathLike] ,a__ : Optional[NamedSplit] = None ,a__ : Optional[Features] = None ,a__ : str = None ,a__ : bool = False ,a__ : bool = False ,a__ : Optional[str] = None ,a__ : Optional[int] = None ,**a__ : int ,) -> List[str]: """simple docstring""" super().__init__( a__ ,split=a__ ,features=a__ ,cache_dir=a__ ,keep_in_memory=a__ ,streaming=a__ ,num_proc=a__ ,**a__ ,) _lowerCAmelCase:Union[str, Any] = field _lowerCAmelCase:Dict = path_or_paths if isinstance(a__ ,a__) else {self.split: path_or_paths} _lowerCAmelCase:Any = Json( cache_dir=a__ ,data_files=a__ ,features=a__ ,field=a__ ,**a__ ,) def __UpperCamelCase ( self : str) -> Optional[int]: """simple docstring""" if self.streaming: _lowerCAmelCase:int = self.builder.as_streaming_dataset(split=self.split) # Build regular (map-style) dataset else: _lowerCAmelCase:List[str] = None _lowerCAmelCase:str = None _lowerCAmelCase:List[Any] = None _lowerCAmelCase:int = None self.builder.download_and_prepare( download_config=a__ ,download_mode=a__ ,verification_mode=a__ ,base_path=a__ ,num_proc=self.num_proc ,) _lowerCAmelCase:Tuple = self.builder.as_dataset( split=self.split ,verification_mode=a__ ,in_memory=self.keep_in_memory) return dataset class a__ : def __init__( self : Any ,a__ : Dataset ,a__ : Union[PathLike, BinaryIO] ,a__ : Optional[int] = None ,a__ : Optional[int] = None ,**a__ : List[Any] ,) -> List[str]: """simple docstring""" if num_proc is not None and num_proc <= 0: raise ValueError(F'num_proc {num_proc} must be an integer > 0.') _lowerCAmelCase:List[str] = dataset _lowerCAmelCase:List[str] = path_or_buf _lowerCAmelCase:Optional[int] = batch_size if batch_size else config.DEFAULT_MAX_BATCH_SIZE _lowerCAmelCase:List[str] = num_proc _lowerCAmelCase:int = """utf-8""" _lowerCAmelCase:Tuple = to_json_kwargs def __UpperCamelCase ( self : Tuple) -> int: """simple docstring""" _lowerCAmelCase:Optional[int] = self.to_json_kwargs.pop('''path_or_buf''' ,a__) _lowerCAmelCase:Optional[Any] = self.to_json_kwargs.pop('''orient''' ,'''records''') _lowerCAmelCase:List[Any] = self.to_json_kwargs.pop('''lines''' ,True if orient == '''records''' else False) _lowerCAmelCase:Optional[Any] = self.to_json_kwargs.pop('''index''' ,False if orient in ['''split''', '''table'''] else True) _lowerCAmelCase:Dict = self.to_json_kwargs.pop('''compression''' ,a__) if compression not in [None, "infer", "gzip", "bz2", "xz"]: raise NotImplementedError(F'`datasets` currently does not support {compression} compression') if isinstance(self.path_or_buf ,(str, bytes, os.PathLike)): with fsspec.open(self.path_or_buf ,'''wb''' ,compression=a__) as buffer: _lowerCAmelCase:Any = self._write(file_obj=a__ ,orient=a__ ,lines=a__ ,index=a__ ,**self.to_json_kwargs) else: if compression: raise NotImplementedError( F'The compression parameter is not supported when writing to a buffer, but compression={compression}' ''' was passed. Please provide a local path instead.''') _lowerCAmelCase:Dict = self._write( file_obj=self.path_or_buf ,orient=a__ ,lines=a__ ,index=a__ ,**self.to_json_kwargs) return written def __UpperCamelCase ( self : Optional[Any] ,a__ : Tuple) -> Optional[int]: """simple docstring""" _lowerCAmelCase:Union[str, Any] = args _lowerCAmelCase:List[Any] = query_table( table=self.dataset.data ,key=slice(a__ ,offset + self.batch_size) ,indices=self.dataset._indices ,) _lowerCAmelCase:str = batch.to_pandas().to_json( path_or_buf=a__ ,orient=a__ ,lines=a__ ,index=a__ ,**a__) if not json_str.endswith('''\n'''): json_str += "\n" return json_str.encode(self.encoding) def __UpperCamelCase ( self : Tuple ,a__ : BinaryIO ,a__ : List[str] ,a__ : Union[str, Any] ,a__ : Optional[int] ,**a__ : int ,) -> int: """simple docstring""" _lowerCAmelCase:Tuple = 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 json from Arrow format''' ,): _lowerCAmelCase:Tuple = self._batch_json((offset, orient, lines, index, to_json_kwargs)) written += file_obj.write(a__) else: _lowerCAmelCase:Any = len(self.dataset), self.batch_size with multiprocessing.Pool(self.num_proc) as pool: for json_str in logging.tqdm( pool.imap( self._batch_json ,[(offset, orient, lines, index, to_json_kwargs) for offset in range(0 ,a__ ,a__)] ,) ,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 json from Arrow format''' ,): written += file_obj.write(a__) return written
227
'''simple docstring''' import json import os from pathlib import Path import pytest from datasets.download.download_config import DownloadConfig from datasets.download.download_manager import DownloadManager from datasets.utils.file_utils import hash_url_to_filename __UpperCamelCase = "http://www.mocksite.com/file1.txt" __UpperCamelCase = "\"text\": [\"foo\", \"foo\"]" __UpperCamelCase = "6d8ce9aa78a471c7477201efbeabd3bb01ac2e7d100a6dc024ba1608361f90a8" class _A : lowercase__: str = 200 lowercase__: List[str] = {'''Content-Length''': '''100'''} lowercase__: Union[str, Any] = {} def lowercase__ ( self : Any , **__magic_name__ : List[Any] ) -> Dict: """simple docstring""" return [bytes(__magic_name__ , """utf-8""" )] def _a ( *_lowerCamelCase , **_lowerCamelCase ) -> List[str]: """simple docstring""" return MockResponse() @pytest.mark.parametrize("""urls_type""" , [str, list, dict] ) def _a ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) -> List[str]: """simple docstring""" import requests monkeypatch.setattr(_lowerCamelCase , """request""" , _lowerCamelCase ) __snake_case : Union[str, Any] = URL if issubclass(_lowerCamelCase , _lowerCamelCase ): __snake_case : str = url elif issubclass(_lowerCamelCase , _lowerCamelCase ): __snake_case : Dict = [url] elif issubclass(_lowerCamelCase , _lowerCamelCase ): __snake_case : Union[str, Any] = {"""train""": url} __snake_case : Dict = """dummy""" __snake_case : List[str] = """downloads""" __snake_case : List[Any] = tmp_path __snake_case : List[Any] = DownloadConfig( cache_dir=os.path.join(_lowerCamelCase , _lowerCamelCase ) , use_etag=_lowerCamelCase , ) __snake_case : List[str] = DownloadManager(dataset_name=_lowerCamelCase , download_config=_lowerCamelCase ) __snake_case : int = dl_manager.download(_lowerCamelCase ) __snake_case : Tuple = urls for downloaded_paths in [downloaded_paths]: if isinstance(_lowerCamelCase , _lowerCamelCase ): __snake_case : Any = [downloaded_paths] __snake_case : List[Any] = [urls] elif isinstance(_lowerCamelCase , _lowerCamelCase ): assert "train" in downloaded_paths.keys() __snake_case : Tuple = downloaded_paths.values() __snake_case : Optional[int] = urls.values() assert downloaded_paths for downloaded_path, input_url in zip(_lowerCamelCase , _lowerCamelCase ): assert downloaded_path == dl_manager.downloaded_paths[input_url] __snake_case : List[str] = Path(_lowerCamelCase ) __snake_case : Any = downloaded_path.parts assert parts[-1] == HASH assert parts[-2] == cache_subdir assert downloaded_path.exists() __snake_case : Union[str, Any] = downloaded_path.read_text() assert content == CONTENT __snake_case : List[str] = downloaded_path.with_suffix(""".json""" ) assert metadata_downloaded_path.exists() __snake_case : Union[str, Any] = json.loads(metadata_downloaded_path.read_text() ) assert metadata_content == {"url": URL, "etag": None} @pytest.mark.parametrize("""paths_type""" , [str, list, dict] ) def _a ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) -> List[Any]: """simple docstring""" __snake_case : Any = str(_lowerCamelCase ) if issubclass(_lowerCamelCase , _lowerCamelCase ): __snake_case : Optional[int] = filename elif issubclass(_lowerCamelCase , _lowerCamelCase ): __snake_case : Tuple = [filename] elif issubclass(_lowerCamelCase , _lowerCamelCase ): __snake_case : Dict = {"""train""": filename} __snake_case : Optional[Any] = """dummy""" __snake_case : List[Any] = xz_file.parent __snake_case : int = """extracted""" __snake_case : Dict = DownloadConfig( cache_dir=_lowerCamelCase , use_etag=_lowerCamelCase , ) __snake_case : List[str] = DownloadManager(dataset_name=_lowerCamelCase , download_config=_lowerCamelCase ) __snake_case : Optional[Any] = dl_manager.extract(_lowerCamelCase ) __snake_case : Union[str, Any] = paths for extracted_paths in [extracted_paths]: if isinstance(_lowerCamelCase , _lowerCamelCase ): __snake_case : Dict = [extracted_paths] __snake_case : int = [paths] elif isinstance(_lowerCamelCase , _lowerCamelCase ): assert "train" in extracted_paths.keys() __snake_case : int = extracted_paths.values() __snake_case : int = paths.values() assert extracted_paths for extracted_path, input_path in zip(_lowerCamelCase , _lowerCamelCase ): assert extracted_path == dl_manager.extracted_paths[input_path] __snake_case : Any = Path(_lowerCamelCase ) __snake_case : str = extracted_path.parts assert parts[-1] == hash_url_to_filename(_lowerCamelCase , etag=_lowerCamelCase ) assert parts[-2] == extracted_subdir assert extracted_path.exists() __snake_case : Optional[int] = extracted_path.read_text() __snake_case : str = text_file.read_text() assert extracted_file_content == expected_file_content def _a ( _lowerCamelCase , _lowerCamelCase ) -> Optional[int]: """simple docstring""" assert path.endswith(""".jsonl""" ) for num_items, line in enumerate(_lowerCamelCase , start=1 ): __snake_case : Tuple = json.loads(line.decode("""utf-8""" ) ) assert item.keys() == {"col_1", "col_2", "col_3"} assert num_items == 4 @pytest.mark.parametrize("""archive_jsonl""" , ["""tar_jsonl_path""", """zip_jsonl_path"""] ) def _a ( _lowerCamelCase , _lowerCamelCase ) -> Optional[int]: """simple docstring""" __snake_case : Any = request.getfixturevalue(_lowerCamelCase ) __snake_case : str = DownloadManager() for num_jsonl, (path, file) in enumerate(dl_manager.iter_archive(_lowerCamelCase ) , start=1 ): _test_jsonl(_lowerCamelCase , _lowerCamelCase ) assert num_jsonl == 2 @pytest.mark.parametrize("""archive_nested_jsonl""" , ["""tar_nested_jsonl_path""", """zip_nested_jsonl_path"""] ) def _a ( _lowerCamelCase , _lowerCamelCase ) -> List[str]: """simple docstring""" __snake_case : int = request.getfixturevalue(_lowerCamelCase ) __snake_case : List[str] = DownloadManager() for num_tar, (path, file) in enumerate(dl_manager.iter_archive(_lowerCamelCase ) , start=1 ): for num_jsonl, (subpath, subfile) in enumerate(dl_manager.iter_archive(_lowerCamelCase ) , start=1 ): _test_jsonl(_lowerCamelCase , _lowerCamelCase ) assert num_tar == 1 assert num_jsonl == 2 def _a ( _lowerCamelCase ) -> Any: """simple docstring""" __snake_case : List[str] = DownloadManager() for num_file, file in enumerate(dl_manager.iter_files(_lowerCamelCase ) , start=1 ): assert os.path.basename(_lowerCamelCase ) == ("test.txt" if num_file == 1 else "train.txt") assert num_file == 2
26
0
import inspect import unittest from huggingface_hub import hf_hub_download from transformers import ConvNextConfig, UperNetConfig from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device from transformers.utils import is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import UperNetForSemanticSegmentation from transformers.models.upernet.modeling_upernet import UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class UpperCAmelCase_ : def __init__( self , _lowerCAmelCase , _lowerCAmelCase=13 , _lowerCAmelCase=32 , _lowerCAmelCase=3 , _lowerCAmelCase=4 , _lowerCAmelCase=[10, 20, 30, 40] , _lowerCAmelCase=[2, 2, 3, 2] , _lowerCAmelCase=True , _lowerCAmelCase=True , _lowerCAmelCase=37 , _lowerCAmelCase="gelu" , _lowerCAmelCase=10 , _lowerCAmelCase=0.0_2 , _lowerCAmelCase=["stage2", "stage3", "stage4"] , _lowerCAmelCase=3 , _lowerCAmelCase=None , ): UpperCAmelCase__ : str = parent UpperCAmelCase__ : List[Any] = batch_size UpperCAmelCase__ : Any = image_size UpperCAmelCase__ : int = num_channels UpperCAmelCase__ : List[Any] = num_stages UpperCAmelCase__ : Union[str, Any] = hidden_sizes UpperCAmelCase__ : Dict = depths UpperCAmelCase__ : Optional[int] = is_training UpperCAmelCase__ : Any = use_labels UpperCAmelCase__ : Union[str, Any] = intermediate_size UpperCAmelCase__ : str = hidden_act UpperCAmelCase__ : Dict = type_sequence_label_size UpperCAmelCase__ : Any = initializer_range UpperCAmelCase__ : str = out_features UpperCAmelCase__ : str = num_labels UpperCAmelCase__ : Dict = scope UpperCAmelCase__ : Optional[int] = num_stages def __UpperCAmelCase ( self ): UpperCAmelCase__ : List[str] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) UpperCAmelCase__ : Optional[int] = None if self.use_labels: UpperCAmelCase__ : str = ids_tensor([self.batch_size] , self.type_sequence_label_size ) UpperCAmelCase__ : Optional[int] = self.get_config() return config, pixel_values, labels def __UpperCAmelCase ( self ): return ConvNextConfig( num_channels=self.num_channels , num_stages=self.num_stages , hidden_sizes=self.hidden_sizes , depths=self.depths , is_training=self.is_training , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , out_features=self.out_features , ) def __UpperCAmelCase ( self ): return UperNetConfig( backbone_config=self.get_backbone_config() , hidden_size=512 , pool_scales=[1, 2, 3, 6] , use_auxiliary_head=_lowerCAmelCase , auxiliary_loss_weight=0.4 , auxiliary_in_channels=40 , auxiliary_channels=256 , auxiliary_num_convs=1 , auxiliary_concat_input=_lowerCAmelCase , loss_ignore_index=255 , num_labels=self.num_labels , ) def __UpperCAmelCase ( self , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ): UpperCAmelCase__ : Optional[Any] = UperNetForSemanticSegmentation(config=_lowerCAmelCase ) model.to(_lowerCAmelCase ) model.eval() UpperCAmelCase__ : int = model(_lowerCAmelCase ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size, self.image_size) ) def __UpperCAmelCase ( self ): UpperCAmelCase__ : Tuple = self.prepare_config_and_inputs() ( UpperCAmelCase__ ) : List[str] = config_and_inputs UpperCAmelCase__ : int = {"""pixel_values""": pixel_values} return config, inputs_dict @require_torch class UpperCAmelCase_ ( __lowercase , __lowercase , unittest.TestCase ): __lowerCamelCase = (UperNetForSemanticSegmentation,) if is_torch_available() else () __lowerCamelCase = {'''image-segmentation''': UperNetForSemanticSegmentation} if is_torch_available() else {} __lowerCamelCase = False __lowerCamelCase = False __lowerCamelCase = False __lowerCamelCase = False __lowerCamelCase = False __lowerCamelCase = False def __UpperCAmelCase ( self ): UpperCAmelCase__ : Optional[Any] = UperNetModelTester(self ) UpperCAmelCase__ : Union[str, Any] = ConfigTester(self , config_class=_lowerCAmelCase , has_text_modality=_lowerCAmelCase , hidden_size=37 ) def __UpperCAmelCase ( self ): self.create_and_test_config_common_properties() self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() def __UpperCAmelCase ( self ): return def __UpperCAmelCase ( self ): UpperCAmelCase__ : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCAmelCase__ : Dict = model_class(_lowerCAmelCase ) UpperCAmelCase__ : Dict = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic UpperCAmelCase__ : Any = [*signature.parameters.keys()] UpperCAmelCase__ : str = ["""pixel_values"""] self.assertListEqual(arg_names[:1] , _lowerCAmelCase ) def __UpperCAmelCase ( self ): UpperCAmelCase__ : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_semantic_segmentation(*_lowerCAmelCase ) @unittest.skip(reason="""UperNet does not use inputs_embeds""" ) def __UpperCAmelCase ( self ): pass @unittest.skip(reason="""UperNet does not support input and output embeddings""" ) def __UpperCAmelCase ( self ): pass @unittest.skip(reason="""UperNet does not have a base model""" ) def __UpperCAmelCase ( self ): pass @unittest.skip(reason="""UperNet does not have a base model""" ) def __UpperCAmelCase ( self ): pass @require_torch_multi_gpu @unittest.skip(reason="""UperNet has some layers using `add_module` which doesn't work well with `nn.DataParallel`""" ) def __UpperCAmelCase ( self ): pass @unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests.""" ) def __UpperCAmelCase ( self ): pass def __UpperCAmelCase ( self ): def check_hidden_states_output(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ): UpperCAmelCase__ : Dict = model_class(_lowerCAmelCase ) model.to(_lowerCAmelCase ) model.eval() with torch.no_grad(): UpperCAmelCase__ : Union[str, Any] = model(**self._prepare_for_class(_lowerCAmelCase , _lowerCAmelCase ) ) UpperCAmelCase__ : int = outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states UpperCAmelCase__ : Optional[Any] = self.model_tester.num_stages self.assertEqual(len(_lowerCAmelCase ) , expected_num_stages + 1 ) # ConvNext's feature maps are of shape (batch_size, num_channels, height, width) self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [self.model_tester.image_size // 4, self.model_tester.image_size // 4] , ) UpperCAmelCase__ : str = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCAmelCase__ : List[str] = True check_hidden_states_output(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] UpperCAmelCase__ : Tuple = True check_hidden_states_output(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) def __UpperCAmelCase ( self ): UpperCAmelCase__ : Dict = self.model_tester.prepare_config_and_inputs_for_common() UpperCAmelCase__ : Any = _config_zero_init(_lowerCAmelCase ) UpperCAmelCase__ : Union[str, Any] = _config_zero_init(configs_no_init.backbone_config ) for model_class in self.all_model_classes: UpperCAmelCase__ : Optional[int] = model_class(config=_lowerCAmelCase ) for name, param in model.named_parameters(): if param.requires_grad: self.assertIn( ((param.data.mean() * 1e9).round() / 1e9).item() , [0.0, 1.0] , msg=f"Parameter {name} of model {model_class} seems not properly initialized" , ) @unittest.skip(reason="""UperNet does not have tied weights""" ) def __UpperCAmelCase ( self ): pass @slow def __UpperCAmelCase ( self ): for model_name in UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCAmelCase__ : List[str] = UperNetForSemanticSegmentation.from_pretrained(_lowerCAmelCase ) self.assertIsNotNone(_lowerCAmelCase ) def _lowerCamelCase ( ) -> Any: '''simple docstring''' UpperCAmelCase__ : Dict = hf_hub_download( repo_id="""hf-internal-testing/fixtures_ade20k""" , repo_type="""dataset""" , filename="""ADE_val_00000001.jpg""" ) UpperCAmelCase__ : List[str] = Image.open(_lowerCamelCase ).convert("""RGB""" ) return image @require_torch @require_vision @slow class UpperCAmelCase_ ( unittest.TestCase ): def __UpperCAmelCase ( self ): UpperCAmelCase__ : Union[str, Any] = AutoImageProcessor.from_pretrained("""openmmlab/upernet-swin-tiny""" ) UpperCAmelCase__ : int = UperNetForSemanticSegmentation.from_pretrained("""openmmlab/upernet-swin-tiny""" ).to(_lowerCAmelCase ) UpperCAmelCase__ : Tuple = prepare_img() UpperCAmelCase__ : int = processor(images=_lowerCAmelCase , return_tensors="""pt""" ).to(_lowerCAmelCase ) with torch.no_grad(): UpperCAmelCase__ : Union[str, Any] = model(**_lowerCAmelCase ) UpperCAmelCase__ : str = torch.Size((1, model.config.num_labels, 512, 512) ) self.assertEqual(outputs.logits.shape , _lowerCAmelCase ) UpperCAmelCase__ : List[str] = torch.tensor( [[-7.5_9_5_8, -7.5_9_5_8, -7.4_3_0_2], [-7.5_9_5_8, -7.5_9_5_8, -7.4_3_0_2], [-7.4_7_9_7, -7.4_7_9_7, -7.3_0_6_8]] ).to(_lowerCAmelCase ) self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3] , _lowerCAmelCase , atol=1e-4 ) ) def __UpperCAmelCase ( self ): UpperCAmelCase__ : str = AutoImageProcessor.from_pretrained("""openmmlab/upernet-convnext-tiny""" ) UpperCAmelCase__ : Any = UperNetForSemanticSegmentation.from_pretrained("""openmmlab/upernet-convnext-tiny""" ).to(_lowerCAmelCase ) UpperCAmelCase__ : Union[str, Any] = prepare_img() UpperCAmelCase__ : Optional[int] = processor(images=_lowerCAmelCase , return_tensors="""pt""" ).to(_lowerCAmelCase ) with torch.no_grad(): UpperCAmelCase__ : int = model(**_lowerCAmelCase ) UpperCAmelCase__ : Optional[Any] = torch.Size((1, model.config.num_labels, 512, 512) ) self.assertEqual(outputs.logits.shape , _lowerCAmelCase ) UpperCAmelCase__ : str = torch.tensor( [[-8.8_1_1_0, -8.8_1_1_0, -8.6_5_2_1], [-8.8_1_1_0, -8.8_1_1_0, -8.6_5_2_1], [-8.7_7_4_6, -8.7_7_4_6, -8.6_1_3_0]] ).to(_lowerCAmelCase ) self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3] , _lowerCAmelCase , atol=1e-4 ) )
79
'''simple docstring''' def _a ( _lowerCamelCase = 100 ) -> int: """simple docstring""" __snake_case : Any = n * (n + 1) * (2 * n + 1) / 6 __snake_case : List[Any] = (n * (n + 1) / 2) ** 2 return int(square_of_sum - sum_of_squares ) if __name__ == "__main__": print(f"""{solution() = }""")
26
0
from .integrations import ( is_optuna_available, is_ray_available, is_sigopt_available, is_wandb_available, run_hp_search_optuna, run_hp_search_ray, run_hp_search_sigopt, run_hp_search_wandb, ) from .trainer_utils import ( HPSearchBackend, default_hp_space_optuna, default_hp_space_ray, default_hp_space_sigopt, default_hp_space_wandb, ) from .utils import logging snake_case_ : Union[str, Any] = logging.get_logger(__name__) class snake_case_ : '''simple docstring''' lowerCamelCase = 42 lowerCamelCase = None @staticmethod def __SCREAMING_SNAKE_CASE ( ) -> Dict: raise NotImplementedError def __SCREAMING_SNAKE_CASE ( self : str , __magic_name__ : int , __magic_name__ : int , __magic_name__ : str , **__magic_name__ : Union[str, Any] ) -> Tuple: raise NotImplementedError def __SCREAMING_SNAKE_CASE ( self : int , __magic_name__ : Optional[Any] ) -> Any: raise NotImplementedError def __SCREAMING_SNAKE_CASE ( self : str ) -> List[str]: if not self.is_available(): raise RuntimeError( F"You picked the {self.name} backend, but it is not installed. Run {self.pip_install()}." ) @classmethod def __SCREAMING_SNAKE_CASE ( cls : Optional[Any] ) -> int: return F"`pip install {cls.pip_package or cls.name}`" class snake_case_ ( __lowercase ): '''simple docstring''' lowerCamelCase = '''optuna''' @staticmethod def __SCREAMING_SNAKE_CASE ( ) -> Union[str, Any]: return is_optuna_available() def __SCREAMING_SNAKE_CASE ( self : Optional[Any] , __magic_name__ : int , __magic_name__ : int , __magic_name__ : str , **__magic_name__ : List[Any] ) -> Optional[Any]: return run_hp_search_optuna(__magic_name__ , __magic_name__ , __magic_name__ , **__magic_name__ ) def __SCREAMING_SNAKE_CASE ( self : Dict , __magic_name__ : Tuple ) -> Optional[int]: return default_hp_space_optuna(__magic_name__ ) class snake_case_ ( __lowercase ): '''simple docstring''' lowerCamelCase = '''ray''' lowerCamelCase = '''\'ray[tune]\'''' @staticmethod def __SCREAMING_SNAKE_CASE ( ) -> str: return is_ray_available() def __SCREAMING_SNAKE_CASE ( self : Any , __magic_name__ : Optional[Any] , __magic_name__ : int , __magic_name__ : str , **__magic_name__ : List[str] ) -> Union[str, Any]: return run_hp_search_ray(__magic_name__ , __magic_name__ , __magic_name__ , **__magic_name__ ) def __SCREAMING_SNAKE_CASE ( self : Dict , __magic_name__ : Dict ) -> Optional[int]: return default_hp_space_ray(__magic_name__ ) class snake_case_ ( __lowercase ): '''simple docstring''' lowerCamelCase = '''sigopt''' @staticmethod def __SCREAMING_SNAKE_CASE ( ) -> List[Any]: return is_sigopt_available() def __SCREAMING_SNAKE_CASE ( self : str , __magic_name__ : str , __magic_name__ : int , __magic_name__ : str , **__magic_name__ : Union[str, Any] ) -> Optional[Any]: return run_hp_search_sigopt(__magic_name__ , __magic_name__ , __magic_name__ , **__magic_name__ ) def __SCREAMING_SNAKE_CASE ( self : List[str] , __magic_name__ : Dict ) -> int: return default_hp_space_sigopt(__magic_name__ ) class snake_case_ ( __lowercase ): '''simple docstring''' lowerCamelCase = '''wandb''' @staticmethod def __SCREAMING_SNAKE_CASE ( ) -> List[str]: return is_wandb_available() def __SCREAMING_SNAKE_CASE ( self : Dict , __magic_name__ : List[str] , __magic_name__ : int , __magic_name__ : str , **__magic_name__ : Any ) -> int: return run_hp_search_wandb(__magic_name__ , __magic_name__ , __magic_name__ , **__magic_name__ ) def __SCREAMING_SNAKE_CASE ( self : Dict , __magic_name__ : Tuple ) -> Optional[int]: return default_hp_space_wandb(__magic_name__ ) snake_case_ : Tuple = { HPSearchBackend(backend.name): backend for backend in [OptunaBackend, RayTuneBackend, SigOptBackend, WandbBackend] } def __a ( ) -> str: """simple docstring""" lowerCamelCase_ : List[Any] = [backend for backend in ALL_HYPERPARAMETER_SEARCH_BACKENDS.values() if backend.is_available()] if len(_lowerCamelCase ) > 0: lowerCamelCase_ : Union[str, Any] = available_backends[0].name if len(_lowerCamelCase ) > 1: logger.info( f"{len(_lowerCamelCase )} hyperparameter search backends available. Using {name} as the default." ) return name raise RuntimeError( "No hyperparameter search backend available.\n" + "\n".join( f" - To install {backend.name} run {backend.pip_install()}" for backend in ALL_HYPERPARAMETER_SEARCH_BACKENDS.values() ) )
488
'''simple docstring''' from __future__ import annotations from typing import Any class _A : def __init__( self : str , __magic_name__ : int , __magic_name__ : int , __magic_name__ : float = 0 ) -> None: """simple docstring""" __snake_case , __snake_case : Optional[Any] = row, column __snake_case : Dict = [[default_value for c in range(__magic_name__ )] for r in range(__magic_name__ )] def __str__( self : List[Any] ) -> str: """simple docstring""" __snake_case : Dict = f'''Matrix consist of {self.row} rows and {self.column} columns\n''' # Make string identifier __snake_case : Optional[int] = 0 for row_vector in self.array: for obj in row_vector: __snake_case : Optional[int] = max(__magic_name__ , len(str(__magic_name__ ) ) ) __snake_case : str = f'''%{max_element_length}s''' # Make string and return def single_line(__magic_name__ : list[float] ) -> str: nonlocal string_format_identifier __snake_case : Union[str, Any] = """[""" line += ", ".join(string_format_identifier % (obj,) for obj in row_vector ) line += "]" return line s += "\n".join(single_line(__magic_name__ ) for row_vector in self.array ) return s def __repr__( self : Optional[int] ) -> str: """simple docstring""" return str(self ) def lowercase__ ( self : Dict , __magic_name__ : tuple[int, int] ) -> bool: """simple docstring""" if not (isinstance(__magic_name__ , (list, tuple) ) and len(__magic_name__ ) == 2): return False elif not (0 <= loc[0] < self.row and 0 <= loc[1] < self.column): return False else: return True def __getitem__( self : int , __magic_name__ : tuple[int, int] ) -> Any: """simple docstring""" assert self.validate_indicies(__magic_name__ ) return self.array[loc[0]][loc[1]] def __setitem__( self : List[str] , __magic_name__ : tuple[int, int] , __magic_name__ : float ) -> None: """simple docstring""" assert self.validate_indicies(__magic_name__ ) __snake_case : Optional[int] = value def __add__( self : Any , __magic_name__ : Matrix ) -> Matrix: """simple docstring""" assert isinstance(__magic_name__ , __magic_name__ ) assert self.row == another.row and self.column == another.column # Add __snake_case : Union[str, Any] = Matrix(self.row , self.column ) for r in range(self.row ): for c in range(self.column ): __snake_case : List[Any] = self[r, c] + another[r, c] return result def __neg__( self : Tuple ) -> Matrix: """simple docstring""" __snake_case : Tuple = Matrix(self.row , self.column ) for r in range(self.row ): for c in range(self.column ): __snake_case : List[Any] = -self[r, c] return result def __sub__( self : Optional[int] , __magic_name__ : Matrix ) -> Matrix: """simple docstring""" return self + (-another) def __mul__( self : List[Any] , __magic_name__ : int | float | Matrix ) -> Matrix: """simple docstring""" if isinstance(__magic_name__ , (int, float) ): # Scalar multiplication __snake_case : Optional[int] = Matrix(self.row , self.column ) for r in range(self.row ): for c in range(self.column ): __snake_case : Tuple = self[r, c] * another return result elif isinstance(__magic_name__ , __magic_name__ ): # Matrix multiplication assert self.column == another.row __snake_case : Dict = Matrix(self.row , another.column ) for r in range(self.row ): for c in range(another.column ): for i in range(self.column ): result[r, c] += self[r, i] * another[i, c] return result else: __snake_case : Optional[int] = f'''Unsupported type given for another ({type(__magic_name__ )})''' raise TypeError(__magic_name__ ) def lowercase__ ( self : str ) -> Matrix: """simple docstring""" __snake_case : Any = Matrix(self.column , self.row ) for r in range(self.row ): for c in range(self.column ): __snake_case : str = self[r, c] return result def lowercase__ ( self : Union[str, Any] , __magic_name__ : Matrix , __magic_name__ : Matrix ) -> Any: """simple docstring""" assert isinstance(__magic_name__ , __magic_name__ ) and isinstance(__magic_name__ , __magic_name__ ) assert self.row == self.column == u.row == v.row # u, v should be column vector assert u.column == v.column == 1 # u, v should be column vector # Calculate __snake_case : List[str] = v.transpose() __snake_case : Tuple = (v_t * self * u)[0, 0] + 1 if numerator_factor == 0: return None # It's not invertable return self - ((self * u) * (v_t * self) * (1.0 / numerator_factor)) # Testing if __name__ == "__main__": def _a ( ) -> None: """simple docstring""" __snake_case : Tuple = Matrix(3 , 3 , 0 ) for i in range(3 ): __snake_case : Any = 1 print(F'''a^(-1) is {ainv}''' ) # u, v __snake_case : Dict = Matrix(3 , 1 , 0 ) __snake_case , __snake_case , __snake_case : Union[str, Any] = 1, 2, -3 __snake_case : str = Matrix(3 , 1 , 0 ) __snake_case , __snake_case , __snake_case : Tuple = 4, -2, 5 print(F'''u is {u}''' ) print(F'''v is {v}''' ) print(F'''uv^T is {u * v.transpose()}''' ) # Sherman Morrison print(F'''(a + uv^T)^(-1) is {ainv.sherman_morrison(_lowerCamelCase , _lowerCamelCase )}''' ) def _a ( ) -> None: """simple docstring""" import doctest doctest.testmod() testa()
26
0
def __SCREAMING_SNAKE_CASE ( a__ : Union[str, Any] ) -> int: return 1 if digit in (0, 1) else (digit * factorial(digit - 1 )) def __SCREAMING_SNAKE_CASE ( a__ : int ) -> bool: __A : str = 0 __A : Any = number while duplicate > 0: __A : Optional[int] = divmod(_lowerCamelCase ,10 ) fact_sum += factorial(_lowerCamelCase ) return fact_sum == number if __name__ == "__main__": print('''Program to check whether a number is a Krisnamurthy Number or not.''') UpperCAmelCase_ : Optional[int] = int(input('''Enter number: ''').strip()) print( f"""{number} is {"" if krishnamurthy(number) else "not "}a Krishnamurthy Number.""" )
17
'''simple docstring''' import argparse import json import os import torch from torch import nn from transformers import NllbMoeConfig, NllbMoeModel from transformers.modeling_utils import dtype_byte_size from transformers.utils import WEIGHTS_INDEX_NAME, WEIGHTS_NAME def _a ( _lowerCamelCase ) -> List[Any]: """simple docstring""" __snake_case : Union[str, Any] = [ """encoder.version""", """decoder.version""", """model.encoder.version""", """model.decoder.version""", """decoder.output_projection.weight""", """_float_tensor""", """encoder.embed_positions._float_tensor""", """decoder.embed_positions._float_tensor""", ] for k in ignore_keys: state_dict.pop(_lowerCamelCase , _lowerCamelCase ) def _a ( _lowerCamelCase ) -> List[str]: """simple docstring""" __snake_case , __snake_case : Dict = emb.weight.shape __snake_case : Optional[int] = nn.Linear(_lowerCamelCase , _lowerCamelCase , bias=_lowerCamelCase ) __snake_case : Union[str, Any] = emb.weight.data return lin_layer def _a ( _lowerCamelCase , _lowerCamelCase=None ) -> Union[str, Any]: """simple docstring""" __snake_case : Any = {} for old_key in state_dict.keys(): __snake_case : Union[str, Any] = old_key if "moe_layer.experts." in key: if expert_idx is not None: __snake_case : Tuple = key.replace("""moe_layer.experts.0""" , F'''ffn.experts.expert_{expert_idx}''' ) else: __snake_case : Optional[int] = key.replace("""moe_layer.experts.""" , """ffn.experts.expert_""" ) if "gate" in key: __snake_case : Dict = key.replace(""".moe_layer.gate.wg""" , """.ffn.router.classifier""" ) if "fc2" and "experts" not in key: __snake_case : Union[str, Any] = key.replace(""".fc2.""" , """.ffn.fc2.""" ) if "fc1" and "experts" not in key: __snake_case : Optional[int] = key.replace(""".fc1.""" , """.ffn.fc1.""" ) if ".encoder_attn." in key: __snake_case : Tuple = key.replace(""".encoder_attn.""" , """.cross_attention.""" ) if "encoder_attn_layer_norm" in key: __snake_case : Union[str, Any] = key.replace("""encoder_attn_layer_norm""" , """cross_attention_layer_norm""" ) if "final_layer_norm" in key: __snake_case : str = key.replace("""final_layer_norm""" , """ff_layer_norm""" ) __snake_case : str = state_dict[old_key] return new_dict def _a ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase = WEIGHTS_NAME ) -> Dict: """simple docstring""" __snake_case : Optional[int] = [] __snake_case : Dict = 0 os.makedirs(_lowerCamelCase , exist_ok=_lowerCamelCase ) for expert in range(_lowerCamelCase ): __snake_case : Tuple = switch_checkpoint_path + F'''-rank-{expert}.pt''' if os.path.isfile(_lowerCamelCase ): __snake_case : Dict = torch.load(_lowerCamelCase )["""model"""] remove_ignore_keys_(_lowerCamelCase ) __snake_case : Optional[Any] = rename_fairseq_keys(_lowerCamelCase , _lowerCamelCase ) __snake_case : List[Any] = os.path.join( _lowerCamelCase , weights_name.replace(""".bin""" , F'''-{len(_lowerCamelCase )+1:05d}-of-???.bin''' ) ) torch.save(_lowerCamelCase , _lowerCamelCase ) sharded_state_dicts.append(expert_state.keys() ) total_size += sum([value.numel() for key, value in expert_state.items()] ) * dtype_byte_size( expert_state[list(_lowerCamelCase )[0]].dtype ) # Add the last block __snake_case : Optional[Any] = os.path.join(_lowerCamelCase , weights_name.replace(""".bin""" , F'''-{len(_lowerCamelCase )+1:05d}-of-???.bin''' ) ) __snake_case : str = torch.load(switch_checkpoint_path + """-shared.pt""" )["""model"""] remove_ignore_keys_(_lowerCamelCase ) __snake_case : Optional[Any] = rename_fairseq_keys(_lowerCamelCase , _lowerCamelCase ) __snake_case : List[str] = shared_weights["""decoder.embed_tokens.weight"""] sharded_state_dicts.append(shared_weights.keys() ) # If we only have the shared weights (dummy model/experts saved on the same file) if len(_lowerCamelCase ) == 1: __snake_case : Optional[Any] = os.path.join(_lowerCamelCase , _lowerCamelCase ) torch.save(_lowerCamelCase , _lowerCamelCase ) return {weights_name: sharded_state_dicts[0]}, None else: torch.save(_lowerCamelCase , _lowerCamelCase ) # Otherwise, let's build the index __snake_case : Tuple = {} for idx, shard in enumerate(_lowerCamelCase ): __snake_case : Any = weights_name.replace(""".bin""" , F'''-{idx+1:05d}-of-{len(_lowerCamelCase ):05d}.bin''' ) __snake_case : int = os.path.join(_lowerCamelCase , weights_name.replace(""".bin""" , F'''-{idx+1:05d}-of-???.bin''' ) ) os.rename(_lowerCamelCase , os.path.join(_lowerCamelCase , _lowerCamelCase ) ) for key in shard: __snake_case : str = shard_file # Add the metadata __snake_case : Optional[Any] = {"""total_size""": total_size} __snake_case : int = {"""metadata""": metadata, """weight_map""": weight_map} with open(os.path.join(_lowerCamelCase , _lowerCamelCase ) , """w""" , encoding="""utf-8""" ) as f: __snake_case : Union[str, Any] = json.dumps(_lowerCamelCase , indent=2 , sort_keys=_lowerCamelCase ) + """\n""" f.write(_lowerCamelCase ) return metadata, index if __name__ == "__main__": __UpperCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( "--nllb_moe_checkpoint_path", default="/home/arthur_huggingface_co/fairseq/weights/checkpoints/model_moe_54b/checkpoint_2_300000", type=str, required=False, help="Path to a directory containing a folder per layer. Follows the original Google format.", ) parser.add_argument("--dtype", default="float32", type=str, required=False, help="dtype of the saved model") parser.add_argument( "--pytorch_dump_folder_path", default="/home/arthur_huggingface_co/fairseq/weights/checkpoints/hf-converted-moe-54b", type=str, required=False, help="Path to the output pytorch model.", ) __UpperCamelCase = parser.parse_args() __UpperCamelCase , __UpperCamelCase = shard_on_the_fly( args.nllb_moe_checkpoint_path, args.pytorch_dump_folder_path, 128, args.dtype, ) __UpperCamelCase = NllbMoeConfig.from_pretrained( "facebook/nllb-200-3.3B", encoder_sparse_step=4, decoder_sparse_step=4, num_experts=128 ) config.save_pretrained(args.pytorch_dump_folder_path) __UpperCamelCase = NllbMoeModel.from_pretrained(args.pytorch_dump_folder_path) print("Done") model.save_pretrained(args.pytorch_dump_folder_path)
26
0
"""simple docstring""" import numpy as np import torch from torch.utils.data import Dataset, IterableDataset from ..utils.generic import ModelOutput class a__ ( __lowercase ): def __init__( self , _a , _a , _a ): lowercase : Any = dataset lowercase : str = process lowercase : Union[str, Any] = params def __len__( self ): return len(self.dataset ) def __getitem__( self , _a ): lowercase : Union[str, Any] = self.dataset[i] lowercase : Dict = self.process(_a , **self.params ) return processed class a__ ( __lowercase ): def __init__( self , _a , _a , _a , _a=None ): lowercase : Tuple = loader lowercase : List[Any] = infer lowercase : Tuple = params if loader_batch_size == 1: # Let's spare some time by deactivating altogether lowercase : Any = None lowercase : Tuple = loader_batch_size # Internal bookkeeping lowercase : Union[str, Any] = None lowercase : Optional[Any] = None def __len__( self ): return len(self.loader ) def __iter__( self ): lowercase : Dict = iter(self.loader ) return self def __magic_name__ ( self ): if isinstance(self._loader_batch_data , torch.Tensor ): # Batch data is simple tensor, just fetch the slice lowercase : List[Any] = self._loader_batch_data[self._loader_batch_index] else: # Batch data is assumed to be BaseModelOutput (or dict) lowercase : Optional[Any] = {} for k, element in self._loader_batch_data.items(): if isinstance(_a , _a ): # Convert ModelOutput to tuple first lowercase : int = element.to_tuple() if isinstance(element[0] , torch.Tensor ): lowercase : List[str] = tuple(el[self._loader_batch_index].unsqueeze(0 ) for el in element ) elif isinstance(element[0] , np.ndarray ): lowercase : Optional[Any] = tuple(np.expand_dims(el[self._loader_batch_index] , 0 ) for el in element ) continue if k in {"hidden_states", "past_key_values", "attentions"} and isinstance(_a , _a ): # Those are stored as lists of tensors so need specific unbatching. if isinstance(element[0] , torch.Tensor ): lowercase : Any = tuple(el[self._loader_batch_index].unsqueeze(0 ) for el in element ) elif isinstance(element[0] , np.ndarray ): lowercase : Optional[Any] = tuple(np.expand_dims(el[self._loader_batch_index] , 0 ) for el in element ) continue if element is None: # This can happen for optional data that get passed around lowercase : Tuple = None elif isinstance(element[self._loader_batch_index] , torch.Tensor ): # Take correct batch data, but make it looked like batch_size=1 # For compatibility with other methods within transformers lowercase : List[str] = element[self._loader_batch_index].unsqueeze(0 ) elif isinstance(element[self._loader_batch_index] , np.ndarray ): # Take correct batch data, but make it looked like batch_size=1 # For compatibility with other methods within transformers lowercase : Union[str, Any] = np.expand_dims(element[self._loader_batch_index] , 0 ) else: # This is typically a list, so no need to `unsqueeze`. lowercase : int = element[self._loader_batch_index] # Recreate the element by reusing the original class to make it look # batch_size=1 lowercase : Union[str, Any] = self._loader_batch_data.__class__(_a ) self._loader_batch_index += 1 return result def __magic_name__ ( self ): if self._loader_batch_index is not None and self._loader_batch_index < self.loader_batch_size: # We are currently unrolling a batch so we just need to return # the current item within a batch return self.loader_batch_item() # We're out of items within a batch lowercase : List[Any] = next(self.iterator ) lowercase : Union[str, Any] = self.infer(_a , **self.params ) # We now have a batch of "inferred things". if self.loader_batch_size is not None: # Try to infer the size of the batch if isinstance(_a , torch.Tensor ): lowercase : List[Any] = processed else: lowercase : Optional[Any] = list(processed.keys() )[0] lowercase : Dict = processed[key] if isinstance(_a , _a ): lowercase : List[str] = len(_a ) else: lowercase : int = first_tensor.shape[0] if 0 < observed_batch_size < self.loader_batch_size: # could be last batch so we can't unroll as many # elements. lowercase : Optional[int] = observed_batch_size # Setting internal index to unwrap the batch lowercase : Union[str, Any] = processed lowercase : Any = 0 return self.loader_batch_item() else: # We're not unrolling batches return processed class a__ ( __lowercase ): def __init__( self , _a , _a , _a , _a=None ): super().__init__(_a , _a , _a ) def __iter__( self ): lowercase : Dict = iter(self.loader ) lowercase : List[str] = None return self def __magic_name__ ( self ): if self.subiterator is None: lowercase : Any = self.infer(next(self.iterator ) , **self.params ) try: # Try to return next item lowercase : List[Any] = next(self.subiterator ) except StopIteration: # When a preprocess iterator ends, we can start lookig at the next item # ChunkIterator will keep feeding until ALL elements of iterator # all have created their subiterator and have been iterating against. # # Another way to look at it, is we're basically flattening lists of lists # into a single list, but with generators lowercase : List[Any] = self.infer(next(self.iterator ) , **self.params ) lowercase : List[str] = next(self.subiterator ) return processed class a__ ( __lowercase ): def __iter__( self ): lowercase : Tuple = iter(self.loader ) return self def __magic_name__ ( self ): lowercase : Dict = False lowercase : Union[str, Any] = [] if self._loader_batch_index is not None and self._loader_batch_index < self.loader_batch_size: while self._loader_batch_index < self.loader_batch_size: lowercase : Union[str, Any] = self.loader_batch_item() lowercase : List[str] = item.pop("is_last" ) accumulator.append(_a ) if is_last: return accumulator while not is_last: lowercase : Union[str, Any] = self.infer(next(self.iterator ) , **self.params ) if self.loader_batch_size is not None: if isinstance(_a , torch.Tensor ): lowercase : str = processed else: lowercase : Optional[Any] = list(processed.keys() )[0] lowercase : List[str] = processed[key] if isinstance(_a , _a ): lowercase : Tuple = len(_a ) else: lowercase : Dict = first_tensor.shape[0] if 0 < observed_batch_size < self.loader_batch_size: # could be last batch so we can't unroll as many # elements. lowercase : List[str] = observed_batch_size lowercase : List[Any] = processed lowercase : str = 0 while self._loader_batch_index < self.loader_batch_size: lowercase : List[Any] = self.loader_batch_item() lowercase : List[Any] = item.pop("is_last" ) accumulator.append(_a ) if is_last: return accumulator else: lowercase : Tuple = processed lowercase : List[Any] = item.pop("is_last" ) accumulator.append(_a ) return accumulator class a__ ( __lowercase ): def __init__( self , _a , _a ): lowercase : List[Any] = dataset lowercase : Optional[int] = key def __len__( self ): return len(self.dataset ) def __getitem__( self , _a ): return self.dataset[i][self.key] class a__ ( __lowercase ): def __init__( self , _a , _a , _a ): lowercase : List[str] = dataset lowercase : Any = keya lowercase : int = keya def __len__( self ): return len(self.dataset ) def __getitem__( self , _a ): return {"text": self.dataset[i][self.keya], "text_pair": self.dataset[i][self.keya]}
361
'''simple docstring''' import cva import numpy as np class _A : def __init__( self : Any , __magic_name__ : float , __magic_name__ : int ) -> Optional[int]: """simple docstring""" if k in (0.04, 0.06): __snake_case : List[str] = k __snake_case : int = window_size else: raise ValueError("""invalid k value""" ) def __str__( self : Union[str, Any] ) -> str: """simple docstring""" return str(self.k ) def lowercase__ ( self : Dict , __magic_name__ : str ) -> tuple[cva.Mat, list[list[int]]]: """simple docstring""" __snake_case : Dict = cva.imread(__magic_name__ , 0 ) __snake_case , __snake_case : List[str] = img.shape __snake_case : list[list[int]] = [] __snake_case : str = img.copy() __snake_case : Tuple = cva.cvtColor(__magic_name__ , cva.COLOR_GRAY2RGB ) __snake_case , __snake_case : List[Any] = np.gradient(__magic_name__ ) __snake_case : Optional[Any] = dx**2 __snake_case : Tuple = dy**2 __snake_case : List[Any] = dx * dy __snake_case : List[Any] = 0.04 __snake_case : Tuple = self.window_size // 2 for y in range(__magic_name__ , h - offset ): for x in range(__magic_name__ , w - offset ): __snake_case : Dict = ixx[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() __snake_case : Optional[int] = iyy[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() __snake_case : str = ixy[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() __snake_case : List[str] = (wxx * wyy) - (wxy**2) __snake_case : Dict = wxx + wyy __snake_case : List[str] = det - k * (trace**2) # Can change the value if r > 0.5: corner_list.append([x, y, r] ) color_img.itemset((y, x, 0) , 0 ) color_img.itemset((y, x, 1) , 0 ) color_img.itemset((y, x, 2) , 2_55 ) return color_img, corner_list if __name__ == "__main__": __UpperCamelCase = HarrisCorner(0.04, 3) __UpperCamelCase , __UpperCamelCase = edge_detect.detect("path_to_image") cva.imwrite("detect.png", color_img)
26
0
class A: '''simple docstring''' def __init__( self : List[str] ) -> None: """simple docstring""" lowerCamelCase_ = {} # Mapping from char to TrieNode lowerCamelCase_ = False def a__ ( self : Union[str, Any] , A_ : list[str] ) -> None: """simple docstring""" for word in words: self.insert(A_ ) def a__ ( self : Any , A_ : str ) -> None: """simple docstring""" lowerCamelCase_ = self for char in word: if char not in curr.nodes: lowerCamelCase_ = TrieNode() lowerCamelCase_ = curr.nodes[char] lowerCamelCase_ = True def a__ ( self : Dict , A_ : str ) -> bool: """simple docstring""" lowerCamelCase_ = self for char in word: if char not in curr.nodes: return False lowerCamelCase_ = curr.nodes[char] return curr.is_leaf def a__ ( self : List[str] , A_ : str ) -> None: """simple docstring""" def _delete(A_ : TrieNode , A_ : str , A_ : int ) -> bool: if index == len(A_ ): # If word does not exist if not curr.is_leaf: return False lowerCamelCase_ = False return len(curr.nodes ) == 0 lowerCamelCase_ = word[index] lowerCamelCase_ = curr.nodes.get(A_ ) # If char not in current trie node if not char_node: return False # Flag to check if node can be deleted lowerCamelCase_ = _delete(A_ , A_ , index + 1 ) if delete_curr: del curr.nodes[char] return len(curr.nodes ) == 0 return delete_curr _delete(self , A_ , 0 ) def _SCREAMING_SNAKE_CASE ( lowercase : Union[str, Any] , lowercase : str ): '''simple docstring''' if node.is_leaf: print(_lowerCamelCase , end=' ' ) for key, value in node.nodes.items(): print_words(_lowerCamelCase , word + key ) def _SCREAMING_SNAKE_CASE ( ): '''simple docstring''' lowerCamelCase_ = """banana bananas bandana band apple all beast""".split() lowerCamelCase_ = TrieNode() root.insert_many(_lowerCamelCase ) # print_words(root, "") assert all(root.find(_lowerCamelCase ) for word in words ) assert root.find('banana' ) assert not root.find('bandanas' ) assert not root.find('apps' ) assert root.find('apple' ) assert root.find('all' ) root.delete('all' ) assert not root.find('all' ) root.delete('banana' ) assert not root.find('banana' ) assert root.find('bananas' ) return True def _SCREAMING_SNAKE_CASE ( lowercase : int , lowercase : str ): '''simple docstring''' print(str(_lowerCamelCase ) , 'works!' if passes else 'doesn\'t work :(' ) def _SCREAMING_SNAKE_CASE ( ): '''simple docstring''' assert test_trie() def _SCREAMING_SNAKE_CASE ( ): '''simple docstring''' print_results('Testing trie functionality' , test_trie() ) if __name__ == "__main__": main()
70
'''simple docstring''' import warnings from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class _A ( __lowercase ): lowercase__: Any = ['''image_processor''', '''tokenizer'''] lowercase__: Any = '''CLIPImageProcessor''' lowercase__: Optional[Any] = ('''CLIPTokenizer''', '''CLIPTokenizerFast''') def __init__( self : int , __magic_name__ : Dict=None , __magic_name__ : Dict=None , **__magic_name__ : Union[str, Any] ) -> Any: """simple docstring""" __snake_case : Optional[Any] = None if "feature_extractor" in kwargs: warnings.warn( """The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`""" """ instead.""" , __magic_name__ , ) __snake_case : List[Any] = kwargs.pop("""feature_extractor""" ) __snake_case : List[str] = 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__(__magic_name__ , __magic_name__ ) def __call__( self : int , __magic_name__ : List[str]=None , __magic_name__ : Tuple=None , __magic_name__ : Any=None , **__magic_name__ : Union[str, Any] ) -> Optional[int]: """simple docstring""" 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: __snake_case : int = self.tokenizer(__magic_name__ , return_tensors=__magic_name__ , **__magic_name__ ) if images is not None: __snake_case : str = self.image_processor(__magic_name__ , return_tensors=__magic_name__ , **__magic_name__ ) if text is not None and images is not None: __snake_case : Union[str, Any] = image_features.pixel_values return encoding elif text is not None: return encoding else: return BatchEncoding(data=dict(**__magic_name__ ) , tensor_type=__magic_name__ ) def lowercase__ ( self : Optional[int] , *__magic_name__ : List[Any] , **__magic_name__ : Any ) -> Optional[Any]: """simple docstring""" return self.tokenizer.batch_decode(*__magic_name__ , **__magic_name__ ) def lowercase__ ( self : List[str] , *__magic_name__ : Tuple , **__magic_name__ : List[Any] ) -> int: """simple docstring""" return self.tokenizer.decode(*__magic_name__ , **__magic_name__ ) @property def lowercase__ ( self : List[Any] ) -> Optional[Any]: """simple docstring""" __snake_case : Dict = self.tokenizer.model_input_names __snake_case : str = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) ) @property def lowercase__ ( self : int ) -> List[str]: """simple docstring""" warnings.warn( """`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.""" , __magic_name__ , ) return self.image_processor_class @property def lowercase__ ( self : Union[str, Any] ) -> Dict: """simple docstring""" warnings.warn( """`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.""" , __magic_name__ , ) return self.image_processor
26
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 lowerCAmelCase__ ( unittest.TestCase ): '''simple docstring''' def _SCREAMING_SNAKE_CASE ( self : str , lowercase_ : int , lowercase_ : int): '''simple docstring''' SCREAMING_SNAKE_CASE_ : Dict = jnp.ones((batch_size, length)) / length return scores def _SCREAMING_SNAKE_CASE ( self : int): '''simple docstring''' SCREAMING_SNAKE_CASE_ : List[str] = None SCREAMING_SNAKE_CASE_ : Tuple = 20 SCREAMING_SNAKE_CASE_ : Dict = self._get_uniform_logits(batch_size=2 , length=lowercase_) # tweak scores to not be uniform anymore SCREAMING_SNAKE_CASE_ : int = scores.at[1, 5].set((1 / length) + 0.1) # peak, 1st batch SCREAMING_SNAKE_CASE_ : Optional[int] = scores.at[1, 10].set((1 / length) - 0.4) # valley, 1st batch # compute softmax SCREAMING_SNAKE_CASE_ : str = jax.nn.softmax(lowercase_ , axis=-1) SCREAMING_SNAKE_CASE_ : Optional[Any] = FlaxTemperatureLogitsWarper(temperature=0.5) SCREAMING_SNAKE_CASE_ : Tuple = FlaxTemperatureLogitsWarper(temperature=1.3) SCREAMING_SNAKE_CASE_ : Optional[Any] = jax.nn.softmax(temp_dist_warper_sharper(lowercase_ , scores.copy() , cur_len=lowercase_) , axis=-1) SCREAMING_SNAKE_CASE_ : Optional[Any] = jax.nn.softmax(temp_dist_warper_smoother(lowercase_ , scores.copy() , cur_len=lowercase_) , 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 _SCREAMING_SNAKE_CASE ( self : int): '''simple docstring''' SCREAMING_SNAKE_CASE_ : int = None SCREAMING_SNAKE_CASE_ : List[str] = 10 SCREAMING_SNAKE_CASE_ : List[Any] = 2 # create ramp distribution SCREAMING_SNAKE_CASE_ : Optional[Any] = np.broadcast_to(np.arange(lowercase_)[None, :] , (batch_size, vocab_size)).copy() SCREAMING_SNAKE_CASE_ : Dict = ramp_logits[1:, : vocab_size // 2] + vocab_size SCREAMING_SNAKE_CASE_ : List[str] = FlaxTopKLogitsWarper(3) SCREAMING_SNAKE_CASE_ : Dict = top_k_warp(lowercase_ , lowercase_ , cur_len=lowercase_) # 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 SCREAMING_SNAKE_CASE_ : Any = 5 SCREAMING_SNAKE_CASE_ : Dict = FlaxTopKLogitsWarper(top_k=1 , filter_value=0.0 , min_tokens_to_keep=3) SCREAMING_SNAKE_CASE_ : Any = np.broadcast_to(np.arange(lowercase_)[None, :] , (batch_size, length)).copy() SCREAMING_SNAKE_CASE_ : List[str] = top_k_warp_safety_check(lowercase_ , lowercase_ , cur_len=lowercase_) # min_tokens overwrites k: 3 tokens are kept => 2 tokens are nullified self.assertListEqual((scores == 0.0).sum(axis=-1).tolist() , [2, 2]) def _SCREAMING_SNAKE_CASE ( self : List[str]): '''simple docstring''' SCREAMING_SNAKE_CASE_ : List[Any] = None SCREAMING_SNAKE_CASE_ : Any = 10 SCREAMING_SNAKE_CASE_ : List[str] = 2 # create distribution and take log (inverse to Softmax as taken in TopPLogitsWarper) SCREAMING_SNAKE_CASE_ : Any = np.log(np.array([[0.3, 0.1, 0.1, 0.5], [0.15, 0.3, 0.3, 0.25]])) SCREAMING_SNAKE_CASE_ : Any = FlaxTopPLogitsWarper(0.8) SCREAMING_SNAKE_CASE_ : Tuple = np.exp(top_p_warp(lowercase_ , lowercase_ , cur_len=lowercase_)) # dist should be filtered to keep min num values so that sum is >= top_p # exp (-inf) => 0 SCREAMING_SNAKE_CASE_ : Any = np.array([[0.3, 0.0, 0.0, 0.5], [0.0, 0.3, 0.3, 0.25]]) self.assertTrue(np.allclose(lowercase_ , lowercase_ , atol=1e-3)) # check edge cases with negative and extreme logits SCREAMING_SNAKE_CASE_ : Optional[Any] = np.broadcast_to(np.arange(lowercase_)[None, :] , (batch_size, vocab_size)).copy() - ( vocab_size // 2 ) # make ramp_logits more extreme SCREAMING_SNAKE_CASE_ : Optional[int] = ramp_logits[1] * 1_00.0 # make sure at least 2 tokens are kept SCREAMING_SNAKE_CASE_ : Union[str, Any] = FlaxTopPLogitsWarper(0.9 , min_tokens_to_keep=2 , filter_value=0.0) SCREAMING_SNAKE_CASE_ : Union[str, Any] = top_p_warp(lowercase_ , lowercase_ , cur_len=lowercase_) # 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 _SCREAMING_SNAKE_CASE ( self : Optional[int]): '''simple docstring''' SCREAMING_SNAKE_CASE_ : Any = 20 SCREAMING_SNAKE_CASE_ : Tuple = 4 SCREAMING_SNAKE_CASE_ : List[Any] = 0 SCREAMING_SNAKE_CASE_ : Tuple = FlaxMinLengthLogitsProcessor(min_length=10 , eos_token_id=lowercase_) # check that min length is applied at length 5 SCREAMING_SNAKE_CASE_ : Union[str, Any] = ids_tensor((batch_size, 20) , vocab_size=20) SCREAMING_SNAKE_CASE_ : int = 5 SCREAMING_SNAKE_CASE_ : Dict = self._get_uniform_logits(lowercase_ , lowercase_) SCREAMING_SNAKE_CASE_ : Any = min_dist_processor(lowercase_ , lowercase_ , cur_len=lowercase_) self.assertListEqual(scores_before_min_length[:, eos_token_id].tolist() , 4 * [-float('''inf''')]) # check that min length is not applied anymore at length 15 SCREAMING_SNAKE_CASE_ : str = self._get_uniform_logits(lowercase_ , lowercase_) SCREAMING_SNAKE_CASE_ : List[Any] = 15 SCREAMING_SNAKE_CASE_ : str = min_dist_processor(lowercase_ , lowercase_ , cur_len=lowercase_) self.assertFalse(jnp.isinf(lowercase_).any()) def _SCREAMING_SNAKE_CASE ( self : Dict): '''simple docstring''' SCREAMING_SNAKE_CASE_ : Dict = 20 SCREAMING_SNAKE_CASE_ : List[str] = 4 SCREAMING_SNAKE_CASE_ : Tuple = 0 SCREAMING_SNAKE_CASE_ : Optional[int] = FlaxForcedBOSTokenLogitsProcessor(bos_token_id=lowercase_) # check that all scores are -inf except the bos_token_id score SCREAMING_SNAKE_CASE_ : List[Any] = ids_tensor((batch_size, 1) , vocab_size=20) SCREAMING_SNAKE_CASE_ : Optional[int] = 1 SCREAMING_SNAKE_CASE_ : Tuple = self._get_uniform_logits(lowercase_ , lowercase_) SCREAMING_SNAKE_CASE_ : List[str] = logits_processor(lowercase_ , lowercase_ , cur_len=lowercase_) 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 SCREAMING_SNAKE_CASE_ : Any = 3 SCREAMING_SNAKE_CASE_ : Union[str, Any] = self._get_uniform_logits(lowercase_ , lowercase_) SCREAMING_SNAKE_CASE_ : Any = logits_processor(lowercase_ , lowercase_ , cur_len=lowercase_) self.assertFalse(jnp.isinf(lowercase_).any()) def _SCREAMING_SNAKE_CASE ( self : Any): '''simple docstring''' SCREAMING_SNAKE_CASE_ : List[Any] = 20 SCREAMING_SNAKE_CASE_ : Union[str, Any] = 4 SCREAMING_SNAKE_CASE_ : Dict = 0 SCREAMING_SNAKE_CASE_ : Optional[int] = 5 SCREAMING_SNAKE_CASE_ : List[Any] = FlaxForcedEOSTokenLogitsProcessor(max_length=lowercase_ , eos_token_id=lowercase_) # check that all scores are -inf except the eos_token_id when max_length is reached SCREAMING_SNAKE_CASE_ : Optional[int] = ids_tensor((batch_size, 4) , vocab_size=20) SCREAMING_SNAKE_CASE_ : int = 4 SCREAMING_SNAKE_CASE_ : Any = self._get_uniform_logits(lowercase_ , lowercase_) SCREAMING_SNAKE_CASE_ : Dict = logits_processor(lowercase_ , lowercase_ , cur_len=lowercase_) 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 SCREAMING_SNAKE_CASE_ : Optional[Any] = 3 SCREAMING_SNAKE_CASE_ : Optional[Any] = self._get_uniform_logits(lowercase_ , lowercase_) SCREAMING_SNAKE_CASE_ : Any = logits_processor(lowercase_ , lowercase_ , cur_len=lowercase_) self.assertFalse(jnp.isinf(lowercase_).any()) def _SCREAMING_SNAKE_CASE ( self : Optional[Any]): '''simple docstring''' SCREAMING_SNAKE_CASE_ : Optional[int] = 4 SCREAMING_SNAKE_CASE_ : int = 10 SCREAMING_SNAKE_CASE_ : List[Any] = 15 SCREAMING_SNAKE_CASE_ : Union[str, Any] = 2 SCREAMING_SNAKE_CASE_ : List[str] = 1 SCREAMING_SNAKE_CASE_ : Optional[Any] = 15 # dummy input_ids and scores SCREAMING_SNAKE_CASE_ : Dict = ids_tensor((batch_size, sequence_length) , lowercase_) SCREAMING_SNAKE_CASE_ : Any = input_ids.copy() SCREAMING_SNAKE_CASE_ : List[Any] = self._get_uniform_logits(lowercase_ , lowercase_) SCREAMING_SNAKE_CASE_ : str = scores.copy() # instantiate all dist processors SCREAMING_SNAKE_CASE_ : Tuple = FlaxTemperatureLogitsWarper(temperature=0.5) SCREAMING_SNAKE_CASE_ : Union[str, Any] = FlaxTopKLogitsWarper(3) SCREAMING_SNAKE_CASE_ : List[str] = FlaxTopPLogitsWarper(0.8) # instantiate all logits processors SCREAMING_SNAKE_CASE_ : Union[str, Any] = FlaxMinLengthLogitsProcessor(min_length=10 , eos_token_id=lowercase_) SCREAMING_SNAKE_CASE_ : Union[str, Any] = FlaxForcedBOSTokenLogitsProcessor(bos_token_id=lowercase_) SCREAMING_SNAKE_CASE_ : str = FlaxForcedEOSTokenLogitsProcessor(max_length=lowercase_ , eos_token_id=lowercase_) SCREAMING_SNAKE_CASE_ : Tuple = 10 # no processor list SCREAMING_SNAKE_CASE_ : Dict = temp_dist_warp(lowercase_ , lowercase_ , cur_len=lowercase_) SCREAMING_SNAKE_CASE_ : Any = top_k_warp(lowercase_ , lowercase_ , cur_len=lowercase_) SCREAMING_SNAKE_CASE_ : str = top_p_warp(lowercase_ , lowercase_ , cur_len=lowercase_) SCREAMING_SNAKE_CASE_ : Optional[int] = min_dist_proc(lowercase_ , lowercase_ , cur_len=lowercase_) SCREAMING_SNAKE_CASE_ : Optional[Any] = bos_dist_proc(lowercase_ , lowercase_ , cur_len=lowercase_) SCREAMING_SNAKE_CASE_ : Optional[Any] = eos_dist_proc(lowercase_ , lowercase_ , cur_len=lowercase_) # with processor list SCREAMING_SNAKE_CASE_ : List[Any] = FlaxLogitsProcessorList( [temp_dist_warp, top_k_warp, top_p_warp, min_dist_proc, bos_dist_proc, eos_dist_proc]) SCREAMING_SNAKE_CASE_ : int = processor(lowercase_ , lowercase_ , cur_len=lowercase_) # scores should be equal self.assertTrue(jnp.allclose(lowercase_ , lowercase_ , atol=1e-3)) # input_ids should never be changed self.assertListEqual(input_ids.tolist() , input_ids_comp.tolist()) def _SCREAMING_SNAKE_CASE ( self : Any): '''simple docstring''' SCREAMING_SNAKE_CASE_ : Union[str, Any] = 4 SCREAMING_SNAKE_CASE_ : Optional[int] = 10 SCREAMING_SNAKE_CASE_ : Optional[Any] = 15 SCREAMING_SNAKE_CASE_ : Dict = 2 SCREAMING_SNAKE_CASE_ : Tuple = 1 SCREAMING_SNAKE_CASE_ : Optional[int] = 15 # dummy input_ids and scores SCREAMING_SNAKE_CASE_ : List[Any] = ids_tensor((batch_size, sequence_length) , lowercase_) SCREAMING_SNAKE_CASE_ : str = input_ids.copy() SCREAMING_SNAKE_CASE_ : str = self._get_uniform_logits(lowercase_ , lowercase_) SCREAMING_SNAKE_CASE_ : int = scores.copy() # instantiate all dist processors SCREAMING_SNAKE_CASE_ : Optional[int] = FlaxTemperatureLogitsWarper(temperature=0.5) SCREAMING_SNAKE_CASE_ : Optional[Any] = FlaxTopKLogitsWarper(3) SCREAMING_SNAKE_CASE_ : str = FlaxTopPLogitsWarper(0.8) # instantiate all logits processors SCREAMING_SNAKE_CASE_ : Optional[Any] = FlaxMinLengthLogitsProcessor(min_length=10 , eos_token_id=lowercase_) SCREAMING_SNAKE_CASE_ : Union[str, Any] = FlaxForcedBOSTokenLogitsProcessor(bos_token_id=lowercase_) SCREAMING_SNAKE_CASE_ : Optional[Any] = FlaxForcedEOSTokenLogitsProcessor(max_length=lowercase_ , eos_token_id=lowercase_) SCREAMING_SNAKE_CASE_ : Dict = 10 # no processor list def run_no_processor_list(lowercase_ : str , lowercase_ : List[str] , lowercase_ : int): SCREAMING_SNAKE_CASE_ : Union[str, Any] = temp_dist_warp(lowercase_ , lowercase_ , cur_len=lowercase_) SCREAMING_SNAKE_CASE_ : Tuple = top_k_warp(lowercase_ , lowercase_ , cur_len=lowercase_) SCREAMING_SNAKE_CASE_ : List[Any] = top_p_warp(lowercase_ , lowercase_ , cur_len=lowercase_) SCREAMING_SNAKE_CASE_ : Union[str, Any] = min_dist_proc(lowercase_ , lowercase_ , cur_len=lowercase_) SCREAMING_SNAKE_CASE_ : List[Any] = bos_dist_proc(lowercase_ , lowercase_ , cur_len=lowercase_) SCREAMING_SNAKE_CASE_ : List[str] = eos_dist_proc(lowercase_ , lowercase_ , cur_len=lowercase_) return scores # with processor list def run_processor_list(lowercase_ : int , lowercase_ : Any , lowercase_ : Dict): SCREAMING_SNAKE_CASE_ : Optional[int] = FlaxLogitsProcessorList( [temp_dist_warp, top_k_warp, top_p_warp, min_dist_proc, bos_dist_proc, eos_dist_proc]) SCREAMING_SNAKE_CASE_ : Optional[Any] = processor(lowercase_ , lowercase_ , cur_len=lowercase_) return scores SCREAMING_SNAKE_CASE_ : int = jax.jit(lowercase_) SCREAMING_SNAKE_CASE_ : List[Any] = jax.jit(lowercase_) SCREAMING_SNAKE_CASE_ : List[str] = jitted_run_no_processor_list(lowercase_ , lowercase_ , lowercase_) SCREAMING_SNAKE_CASE_ : Tuple = jitted_run_processor_list(lowercase_ , lowercase_ , lowercase_) # scores should be equal self.assertTrue(jnp.allclose(lowercase_ , lowercase_ , atol=1e-3)) # input_ids should never be changed self.assertListEqual(input_ids.tolist() , input_ids_comp.tolist())
512
'''simple docstring''' import datasets import faiss import numpy as np import streamlit as st import torch from elasticsearch import Elasticsearch from elia_utils import ( embed_questions_for_retrieval, make_qa_sas_model, qa_sas_generate, query_es_index, query_qa_dense_index, ) import transformers from transformers import AutoModel, AutoModelForSeqaSeqLM, AutoTokenizer __UpperCamelCase = "bart" __UpperCamelCase = True @st.cache(allow_output_mutation=_lowerCamelCase ) def _a ( ) -> Union[str, Any]: """simple docstring""" if LOAD_DENSE_INDEX: __snake_case : int = AutoTokenizer.from_pretrained("""yjernite/retribert-base-uncased""" ) __snake_case : Tuple = AutoModel.from_pretrained("""yjernite/retribert-base-uncased""" ).to("""cuda:0""" ) __snake_case : List[Any] = qar_model.eval() else: __snake_case , __snake_case : Optional[Any] = (None, None) if MODEL_TYPE == "bart": __snake_case : List[str] = AutoTokenizer.from_pretrained("""yjernite/bart_eli5""" ) __snake_case : Any = AutoModelForSeqaSeqLM.from_pretrained("""yjernite/bart_eli5""" ).to("""cuda:0""" ) __snake_case : int = torch.load("""seq2seq_models/eli5_bart_model_blm_2.pth""" ) sas_model.load_state_dict(save_dict["""model"""] ) __snake_case : int = sas_model.eval() else: __snake_case , __snake_case : Dict = make_qa_sas_model( model_name="""t5-small""" , from_file="""seq2seq_models/eli5_t5_model_1024_4.pth""" , device="""cuda:0""" ) return (qar_tokenizer, qar_model, sas_tokenizer, sas_model) @st.cache(allow_output_mutation=_lowerCamelCase ) def _a ( ) -> Tuple: """simple docstring""" if LOAD_DENSE_INDEX: __snake_case : Tuple = faiss.StandardGpuResources() __snake_case : Optional[Any] = datasets.load_dataset(path="""wiki_snippets""" , name="""wiki40b_en_100_0""" )["""train"""] __snake_case : str = np.memmap( """wiki40b_passages_reps_32_l-8_h-768_b-512-512.dat""" , dtype="""float32""" , mode="""r""" , shape=(wikiaab_passages.num_rows, 128) , ) __snake_case : Optional[int] = faiss.IndexFlatIP(128 ) __snake_case : Any = faiss.index_cpu_to_gpu(_lowerCamelCase , 1 , _lowerCamelCase ) wikiaab_gpu_index_flat.add(_lowerCamelCase ) # TODO fix for larger GPU else: __snake_case , __snake_case : Tuple = (None, None) __snake_case : List[str] = Elasticsearch([{"""host""": """localhost""", """port""": """9200"""}] ) return (wikiaab_passages, wikiaab_gpu_index_flat, es_client) @st.cache(allow_output_mutation=_lowerCamelCase ) def _a ( ) -> List[Any]: """simple docstring""" __snake_case : Tuple = datasets.load_dataset("""eli5""" , name="""LFQA_reddit""" ) __snake_case : Dict = elia["""train_eli5"""] __snake_case : int = np.memmap( """eli5_questions_reps.dat""" , dtype="""float32""" , mode="""r""" , shape=(elia_train.num_rows, 128) ) __snake_case : Dict = faiss.IndexFlatIP(128 ) eli5_train_q_index.add(_lowerCamelCase ) return (elia_train, eli5_train_q_index) __UpperCamelCase , __UpperCamelCase , __UpperCamelCase = load_indexes() __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase = load_models() __UpperCamelCase , __UpperCamelCase = load_train_data() def _a ( _lowerCamelCase , _lowerCamelCase=10 ) -> int: """simple docstring""" __snake_case : Optional[int] = embed_questions_for_retrieval([question] , _lowerCamelCase , _lowerCamelCase ) __snake_case , __snake_case : Tuple = eli5_train_q_index.search(_lowerCamelCase , _lowerCamelCase ) __snake_case : Tuple = [elia_train[int(_lowerCamelCase )] for i in I[0]] return nn_examples def _a ( _lowerCamelCase , _lowerCamelCase="wiki40b" , _lowerCamelCase="dense" , _lowerCamelCase=10 ) -> Optional[Any]: """simple docstring""" if source == "none": __snake_case , __snake_case : Dict = (""" <P> """.join(["""""" for _ in range(11 )] ).strip(), []) else: if method == "dense": __snake_case , __snake_case : Dict = query_qa_dense_index( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) else: __snake_case , __snake_case : str = query_es_index( _lowerCamelCase , _lowerCamelCase , index_name="""english_wiki40b_snippets_100w""" , n_results=_lowerCamelCase , ) __snake_case : Optional[int] = [ (res["""article_title"""], res["""section_title"""].strip(), res["""score"""], res["""passage_text"""]) for res in hit_lst ] __snake_case : Optional[Any] = """question: {} context: {}""".format(_lowerCamelCase , _lowerCamelCase ) return question_doc, support_list @st.cache( hash_funcs={ torch.Tensor: (lambda _lowerCamelCase : None), transformers.models.bart.tokenization_bart.BartTokenizer: (lambda _lowerCamelCase : None), } ) def _a ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase=64 , _lowerCamelCase=256 , _lowerCamelCase=False , _lowerCamelCase=2 , _lowerCamelCase=0.95 , _lowerCamelCase=0.8 ) -> List[str]: """simple docstring""" with torch.no_grad(): __snake_case : Union[str, Any] = qa_sas_generate( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , num_answers=1 , num_beams=_lowerCamelCase , min_len=_lowerCamelCase , max_len=_lowerCamelCase , do_sample=_lowerCamelCase , temp=_lowerCamelCase , top_p=_lowerCamelCase , top_k=_lowerCamelCase , max_input_length=1024 , device="""cuda:0""" , )[0] return (answer, support_list) st.title("Long Form Question Answering with ELI5") # Start sidebar __UpperCamelCase = "<img src='https://huggingface.co/front/assets/huggingface_logo.svg'>" __UpperCamelCase = "\n<html>\n <head>\n <style>\n .img-container {\n padding-left: 90px;\n padding-right: 90px;\n padding-top: 50px;\n padding-bottom: 50px;\n background-color: #f0f3f9;\n }\n </style>\n </head>\n <body>\n <span class=\"img-container\"> <!-- Inline parent element -->\n %s\n </span>\n </body>\n</html>\n" % ( header_html, ) st.sidebar.markdown( header_full, unsafe_allow_html=True, ) # Long Form QA with ELI5 and Wikipedia __UpperCamelCase = "\nThis demo presents a model trained to [provide long-form answers to open-domain questions](https://yjernite.github.io/lfqa.html).\nFirst, a document retriever fetches a set of relevant Wikipedia passages given the question from the [Wiki40b](https://research.google/pubs/pub49029/) dataset,\na pre-processed fixed snapshot of Wikipedia.\n" st.sidebar.markdown(description, unsafe_allow_html=True) __UpperCamelCase = [ "Answer the question", "View the retrieved document only", "View the most similar ELI5 question and answer", "Show me everything, please!", ] __UpperCamelCase = st.sidebar.checkbox("Demo options") if demo_options: __UpperCamelCase = st.sidebar.selectbox( "", action_list, index=3, ) __UpperCamelCase = action_list.index(action_st) __UpperCamelCase = st.sidebar.selectbox( "", ["Show full text of passages", "Show passage section titles"], index=0, ) __UpperCamelCase = show_type == "Show full text of passages" else: __UpperCamelCase = 3 __UpperCamelCase = True __UpperCamelCase = st.sidebar.checkbox("Retrieval options") if retrieval_options: __UpperCamelCase = "\n ### Information retriever options\n\n The **sparse** retriever uses ElasticSearch, while the **dense** retriever uses max-inner-product search between a question and passage embedding\n trained using the [ELI5](https://arxiv.org/abs/1907.09190) questions-answer pairs.\n The answer is then generated by sequence to sequence model which takes the question and retrieved document as input.\n " st.sidebar.markdown(retriever_info) __UpperCamelCase = st.sidebar.selectbox("Which Wikipedia format should the model use?", ["wiki40b", "none"]) __UpperCamelCase = st.sidebar.selectbox("Which Wikipedia indexer should the model use?", ["dense", "sparse", "mixed"]) else: __UpperCamelCase = "wiki40b" __UpperCamelCase = "dense" __UpperCamelCase = "beam" __UpperCamelCase = 2 __UpperCamelCase = 64 __UpperCamelCase = 256 __UpperCamelCase = None __UpperCamelCase = None __UpperCamelCase = st.sidebar.checkbox("Generation options") if generate_options: __UpperCamelCase = "\n ### Answer generation options\n\n The sequence-to-sequence model was initialized with [BART](https://huggingface.co/facebook/bart-large)\n weights and fine-tuned on the ELI5 QA pairs and retrieved documents. You can use the model for greedy decoding with\n **beam** search, or **sample** from the decoder's output probabilities.\n " st.sidebar.markdown(generate_info) __UpperCamelCase = st.sidebar.selectbox("Would you like to use beam search or sample an answer?", ["beam", "sampled"]) __UpperCamelCase = st.sidebar.slider( "Minimum generation length", min_value=8, max_value=256, value=64, step=8, format=None, key=None ) __UpperCamelCase = st.sidebar.slider( "Maximum generation length", min_value=64, max_value=512, value=256, step=16, format=None, key=None ) if sampled == "beam": __UpperCamelCase = st.sidebar.slider("Beam size", min_value=1, max_value=8, value=2, step=None, format=None, key=None) else: __UpperCamelCase = st.sidebar.slider( "Nucleus sampling p", min_value=0.1, max_value=1.0, value=0.95, step=0.01, format=None, key=None ) __UpperCamelCase = st.sidebar.slider( "Temperature", min_value=0.1, max_value=1.0, value=0.7, step=0.01, format=None, key=None ) __UpperCamelCase = None # start main text __UpperCamelCase = [ "<MY QUESTION>", "How do people make chocolate?", "Why do we get a fever when we are sick?", "How can different animals perceive different colors?", "What is natural language processing?", "What's the best way to treat a sunburn?", "What exactly are vitamins ?", "How does nuclear energy provide electricity?", "What's the difference between viruses and bacteria?", "Why are flutes classified as woodwinds when most of them are made out of metal ?", "Why do people like drinking coffee even though it tastes so bad?", "What happens when wine ages? How does it make the wine taste better?", "If an animal is an herbivore, where does it get the protein that it needs to survive if it only eats grass?", "How can we set a date to the beginning or end of an artistic period? Doesn't the change happen gradually?", "How does New Zealand have so many large bird predators?", ] __UpperCamelCase = st.selectbox( "What would you like to ask? ---- select <MY QUESTION> to enter a new query", questions_list, index=1, ) if question_s == "<MY QUESTION>": __UpperCamelCase = st.text_input("Enter your question here:", "") else: __UpperCamelCase = question_s if st.button("Show me!"): if action in [0, 1, 3]: if index_type == "mixed": __UpperCamelCase , __UpperCamelCase = make_support(question, source=wiki_source, method="dense", n_results=10) __UpperCamelCase , __UpperCamelCase = make_support(question, source=wiki_source, method="sparse", n_results=10) __UpperCamelCase = [] for res_d, res_s in zip(support_list_dense, support_list_sparse): if tuple(res_d) not in support_list: support_list += [tuple(res_d)] if tuple(res_s) not in support_list: support_list += [tuple(res_s)] __UpperCamelCase = support_list[:10] __UpperCamelCase = "<P> " + " <P> ".join([res[-1] for res in support_list]) else: __UpperCamelCase , __UpperCamelCase = make_support(question, source=wiki_source, method=index_type, n_results=10) if action in [0, 3]: __UpperCamelCase , __UpperCamelCase = answer_question( question_doc, sas_model, sas_tokenizer, min_len=min_len, max_len=int(max_len), sampling=(sampled == "sampled"), n_beams=n_beams, top_p=top_p, temp=temp, ) st.markdown("### The model generated answer is:") st.write(answer) if action in [0, 1, 3] and wiki_source != "none": st.markdown("--- \n ### The model is drawing information from the following Wikipedia passages:") for i, res in enumerate(support_list): __UpperCamelCase = "https://en.wikipedia.org/wiki/{}".format(res[0].replace(" ", "_")) __UpperCamelCase = res[1].strip() if sec_titles == "": __UpperCamelCase = "[{}]({})".format(res[0], wiki_url) else: __UpperCamelCase = sec_titles.split(" & ") __UpperCamelCase = " & ".join( ["[{}]({}#{})".format(sec.strip(), wiki_url, sec.strip().replace(" ", "_")) for sec in sec_list] ) st.markdown( "{0:02d} - **Article**: {1:<18} <br> _Section_: {2}".format(i + 1, res[0], sections), unsafe_allow_html=True, ) if show_passages: st.write( "> <span style=\"font-family:arial; font-size:10pt;\">" + res[-1] + "</span>", unsafe_allow_html=True ) if action in [2, 3]: __UpperCamelCase = find_nearest_training(question) __UpperCamelCase = nn_train_list[0] st.markdown( "--- \n ### The most similar question in the ELI5 training set was: \n\n {}".format(train_exple["title"]) ) __UpperCamelCase = [ "{}. {}".format(i + 1, " \n".join([line.strip() for line in ans.split("\n") if line.strip() != ""])) for i, (ans, sc) in enumerate(zip(train_exple["answers"]["text"], train_exple["answers"]["score"])) if i == 0 or sc > 2 ] st.markdown("##### Its answers were: \n\n {}".format("\n".join(answers_st))) __UpperCamelCase = "\n---\n\n**Disclaimer**\n\n*The intent of this app is to provide some (hopefully entertaining) insights into the behavior of a current LFQA system.\nEvaluating biases of such a model and ensuring factual generations are still very much open research problems.\nTherefore, until some significant progress is achieved, we caution against using the generated answers for practical purposes.*\n" st.sidebar.markdown(disclaimer, unsafe_allow_html=True)
26
0
import inspect import unittest import numpy as np from tests.test_modeling_common import floats_tensor from transformers import DetrConfig, MaskFormerConfig, SwinConfig, is_torch_available, is_vision_available from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import MaskFormerForInstanceSegmentation, MaskFormerModel if is_vision_available(): from transformers import MaskFormerImageProcessor if is_vision_available(): from PIL import Image class lowercase__ : def __init__( self : Any , _lowercase : Dict , _lowercase : Optional[Any]=2 , _lowercase : List[Any]=True , _lowercase : Optional[Any]=False , _lowercase : Optional[int]=10 , _lowercase : Optional[int]=3 , _lowercase : Optional[int]=32 * 4 , _lowercase : int=32 * 6 , _lowercase : List[Any]=4 , _lowercase : List[Any]=32 , ): """simple docstring""" UpperCAmelCase__ = parent UpperCAmelCase__ = batch_size UpperCAmelCase__ = is_training UpperCAmelCase__ = use_auxiliary_loss UpperCAmelCase__ = num_queries UpperCAmelCase__ = num_channels UpperCAmelCase__ = min_size UpperCAmelCase__ = max_size UpperCAmelCase__ = num_labels UpperCAmelCase__ = mask_feature_size def _UpperCAmelCase ( self : str ): """simple docstring""" UpperCAmelCase__ = floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size] ).to( _lowercase ) UpperCAmelCase__ = torch.ones([self.batch_size, self.min_size, self.max_size] , device=_lowercase ) UpperCAmelCase__ = ( torch.rand([self.batch_size, self.num_labels, self.min_size, self.max_size] , device=_lowercase ) > 0.5 ).float() UpperCAmelCase__ = (torch.rand((self.batch_size, self.num_labels) , device=_lowercase ) > 0.5).long() UpperCAmelCase__ = self.get_config() return config, pixel_values, pixel_mask, mask_labels, class_labels def _UpperCAmelCase ( self : Optional[Any] ): """simple docstring""" return MaskFormerConfig.from_backbone_and_decoder_configs( backbone_config=SwinConfig( depths=[1, 1, 1, 1] , ) , decoder_config=DetrConfig( decoder_ffn_dim=1_28 , num_queries=self.num_queries , decoder_attention_heads=2 , d_model=self.mask_feature_size , ) , mask_feature_size=self.mask_feature_size , fpn_feature_size=self.mask_feature_size , num_channels=self.num_channels , num_labels=self.num_labels , ) def _UpperCAmelCase ( self : Optional[Any] ): """simple docstring""" UpperCAmelCase__ = self.prepare_config_and_inputs() UpperCAmelCase__ = {"""pixel_values""": pixel_values, """pixel_mask""": pixel_mask} return config, inputs_dict def _UpperCAmelCase ( self : str , _lowercase : int , _lowercase : int ): """simple docstring""" UpperCAmelCase__ = output.encoder_hidden_states UpperCAmelCase__ = output.pixel_decoder_hidden_states UpperCAmelCase__ = output.transformer_decoder_hidden_states self.parent.assertTrue(len(_lowercase ) , len(config.backbone_config.depths ) ) self.parent.assertTrue(len(_lowercase ) , len(config.backbone_config.depths ) ) self.parent.assertTrue(len(_lowercase ) , config.decoder_config.decoder_layers ) def _UpperCAmelCase ( self : List[str] , _lowercase : Union[str, Any] , _lowercase : int , _lowercase : int , _lowercase : Tuple=False ): """simple docstring""" with torch.no_grad(): UpperCAmelCase__ = MaskFormerModel(config=_lowercase ) model.to(_lowercase ) model.eval() UpperCAmelCase__ = model(pixel_values=_lowercase , pixel_mask=_lowercase ) UpperCAmelCase__ = model(_lowercase , output_hidden_states=_lowercase ) # the correct shape of output.transformer_decoder_hidden_states ensure the correcteness of the # encoder and pixel decoder self.parent.assertEqual( output.transformer_decoder_last_hidden_state.shape , (self.batch_size, self.num_queries, self.mask_feature_size) , ) # let's ensure the other two hidden state exists self.parent.assertTrue(output.pixel_decoder_last_hidden_state is not None ) self.parent.assertTrue(output.encoder_last_hidden_state is not None ) if output_hidden_states: self.check_output_hidden_state(_lowercase , _lowercase ) def _UpperCAmelCase ( self : Union[str, Any] , _lowercase : Dict , _lowercase : Optional[int] , _lowercase : List[Any] , _lowercase : Optional[int] , _lowercase : Union[str, Any] ): """simple docstring""" UpperCAmelCase__ = MaskFormerForInstanceSegmentation(config=_lowercase ) model.to(_lowercase ) model.eval() def comm_check_on_output(_lowercase : Dict ): # let's still check that all the required stuff is there self.parent.assertTrue(result.transformer_decoder_last_hidden_state is not None ) self.parent.assertTrue(result.pixel_decoder_last_hidden_state is not None ) self.parent.assertTrue(result.encoder_last_hidden_state is not None ) # okay, now we need to check the logits shape # due to the encoder compression, masks have a //4 spatial size self.parent.assertEqual( result.masks_queries_logits.shape , (self.batch_size, self.num_queries, self.min_size // 4, self.max_size // 4) , ) # + 1 for null class self.parent.assertEqual( result.class_queries_logits.shape , (self.batch_size, self.num_queries, self.num_labels + 1) ) with torch.no_grad(): UpperCAmelCase__ = model(pixel_values=_lowercase , pixel_mask=_lowercase ) UpperCAmelCase__ = model(_lowercase ) comm_check_on_output(_lowercase ) UpperCAmelCase__ = model( pixel_values=_lowercase , pixel_mask=_lowercase , mask_labels=_lowercase , class_labels=_lowercase ) comm_check_on_output(_lowercase ) self.parent.assertTrue(result.loss is not None ) self.parent.assertEqual(result.loss.shape , torch.Size([1] ) ) @require_torch class lowercase__ ( __lowercase , __lowercase , unittest.TestCase ): A__= (MaskFormerModel, MaskFormerForInstanceSegmentation) if is_torch_available() else () A__= ( {'''feature-extraction''': MaskFormerModel, '''image-segmentation''': MaskFormerForInstanceSegmentation} if is_torch_available() else {} ) A__= False A__= False A__= False A__= False def _UpperCAmelCase ( self : Optional[Any] ): """simple docstring""" UpperCAmelCase__ = MaskFormerModelTester(self ) UpperCAmelCase__ = ConfigTester(self , config_class=_lowercase , has_text_modality=_lowercase ) def _UpperCAmelCase ( self : List[Any] ): """simple docstring""" self.config_tester.run_common_tests() def _UpperCAmelCase ( self : Tuple ): """simple docstring""" UpperCAmelCase__ = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.create_and_check_maskformer_model(_lowercase , **_lowercase , output_hidden_states=_lowercase ) def _UpperCAmelCase ( self : Any ): """simple docstring""" UpperCAmelCase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_maskformer_instance_segmentation_head_model(*_lowercase ) @unittest.skip(reason="MaskFormer does not use inputs_embeds" ) def _UpperCAmelCase ( self : Optional[int] ): """simple docstring""" pass @unittest.skip(reason="MaskFormer does not have a get_input_embeddings method" ) def _UpperCAmelCase ( self : Any ): """simple docstring""" pass @unittest.skip(reason="MaskFormer is not a generative model" ) def _UpperCAmelCase ( self : Optional[Any] ): """simple docstring""" pass @unittest.skip(reason="MaskFormer does not use token embeddings" ) def _UpperCAmelCase ( self : List[str] ): """simple docstring""" pass @require_torch_multi_gpu @unittest.skip( reason="MaskFormer has some layers using `add_module` which doesn't work well with `nn.DataParallel`" ) def _UpperCAmelCase ( self : Dict ): """simple docstring""" pass @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests." ) def _UpperCAmelCase ( self : Any ): """simple docstring""" pass def _UpperCAmelCase ( self : List[str] ): """simple docstring""" UpperCAmelCase__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCAmelCase__ = model_class(_lowercase ) UpperCAmelCase__ = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic UpperCAmelCase__ = [*signature.parameters.keys()] UpperCAmelCase__ = ["""pixel_values"""] self.assertListEqual(arg_names[:1] , _lowercase ) @slow def _UpperCAmelCase ( self : List[Any] ): """simple docstring""" for model_name in ["facebook/maskformer-swin-small-coco"]: UpperCAmelCase__ = MaskFormerModel.from_pretrained(_lowercase ) self.assertIsNotNone(_lowercase ) def _UpperCAmelCase ( self : int ): """simple docstring""" UpperCAmelCase__ = (self.model_tester.min_size,) * 2 UpperCAmelCase__ = { """pixel_values""": torch.randn((2, 3, *size) , device=_lowercase ), """mask_labels""": torch.randn((2, 10, *size) , device=_lowercase ), """class_labels""": torch.zeros(2 , 10 , device=_lowercase ).long(), } UpperCAmelCase__ = MaskFormerForInstanceSegmentation(MaskFormerConfig() ).to(_lowercase ) UpperCAmelCase__ = model(**_lowercase ) self.assertTrue(outputs.loss is not None ) def _UpperCAmelCase ( self : List[str] ): """simple docstring""" UpperCAmelCase__ = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.create_and_check_maskformer_model(_lowercase , **_lowercase , output_hidden_states=_lowercase ) def _UpperCAmelCase ( self : Optional[int] ): """simple docstring""" UpperCAmelCase__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCAmelCase__ = model_class(_lowercase ).to(_lowercase ) UpperCAmelCase__ = model(**_lowercase , output_attentions=_lowercase ) self.assertTrue(outputs.attentions is not None ) def _UpperCAmelCase ( self : List[Any] ): """simple docstring""" if not self.model_tester.is_training: return # only MaskFormerForInstanceSegmentation has the loss UpperCAmelCase__ = self.all_model_classes[1] UpperCAmelCase__ = self.model_tester.prepare_config_and_inputs() UpperCAmelCase__ = model_class(_lowercase ) model.to(_lowercase ) model.train() UpperCAmelCase__ = model(_lowercase , mask_labels=_lowercase , class_labels=_lowercase ).loss loss.backward() def _UpperCAmelCase ( self : Tuple ): """simple docstring""" UpperCAmelCase__ = self.all_model_classes[1] UpperCAmelCase__ = self.model_tester.prepare_config_and_inputs() UpperCAmelCase__ = True UpperCAmelCase__ = True UpperCAmelCase__ = model_class(_lowercase ) model.to(_lowercase ) model.train() UpperCAmelCase__ = model(_lowercase , mask_labels=_lowercase , class_labels=_lowercase ) UpperCAmelCase__ = outputs.encoder_hidden_states[0] encoder_hidden_states.retain_grad() UpperCAmelCase__ = outputs.pixel_decoder_hidden_states[0] pixel_decoder_hidden_states.retain_grad() # we requires_grad=True in inputs_embeds (line 2152), the original implementation don't UpperCAmelCase__ = outputs.transformer_decoder_hidden_states[0] transformer_decoder_hidden_states.retain_grad() UpperCAmelCase__ = outputs.attentions[0] attentions.retain_grad() outputs.loss.backward(retain_graph=_lowercase ) self.assertIsNotNone(encoder_hidden_states.grad ) self.assertIsNotNone(pixel_decoder_hidden_states.grad ) self.assertIsNotNone(transformer_decoder_hidden_states.grad ) self.assertIsNotNone(attentions.grad ) A = 1e-4 def __UpperCAmelCase ( ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase__ = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_vision @slow class lowercase__ ( unittest.TestCase ): @cached_property def _UpperCAmelCase ( self : int ): """simple docstring""" return ( MaskFormerImageProcessor.from_pretrained("facebook/maskformer-swin-small-coco" ) if is_vision_available() else None ) def _UpperCAmelCase ( self : Tuple ): """simple docstring""" UpperCAmelCase__ = MaskFormerModel.from_pretrained("facebook/maskformer-swin-small-coco" ).to(_lowercase ) UpperCAmelCase__ = self.default_image_processor UpperCAmelCase__ = prepare_img() UpperCAmelCase__ = image_processor(_lowercase , return_tensors="pt" ).to(_lowercase ) UpperCAmelCase__ = inputs["""pixel_values"""].shape # check size is divisible by 32 self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0 ) # check size self.assertEqual(_lowercase , (1, 3, 8_00, 10_88) ) with torch.no_grad(): UpperCAmelCase__ = model(**_lowercase ) UpperCAmelCase__ = torch.tensor( [[-0.0_4_8_2, 0.9_2_2_8, 0.4_9_5_1], [-0.2_5_4_7, 0.8_0_1_7, 0.8_5_2_7], [-0.0_0_6_9, 0.3_3_8_5, -0.0_0_8_9]] ).to(_lowercase ) self.assertTrue( torch.allclose( outputs.encoder_last_hidden_state[0, 0, :3, :3] , _lowercase , atol=_lowercase ) ) UpperCAmelCase__ = torch.tensor( [[-0.8_4_2_2, -0.8_4_3_4, -0.9_7_1_8], [-1.0_1_4_4, -0.5_5_6_5, -0.4_1_9_5], [-1.0_0_3_8, -0.4_4_8_4, -0.1_9_6_1]] ).to(_lowercase ) self.assertTrue( torch.allclose( outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3] , _lowercase , atol=_lowercase ) ) UpperCAmelCase__ = torch.tensor( [[0.2_8_5_2, -0.0_1_5_9, 0.9_7_3_5], [0.6_2_5_4, 0.1_8_5_8, 0.8_5_2_9], [-0.0_6_8_0, -0.4_1_1_6, 1.8_4_1_3]] ).to(_lowercase ) self.assertTrue( torch.allclose( outputs.transformer_decoder_last_hidden_state[0, :3, :3] , _lowercase , atol=_lowercase ) ) def _UpperCAmelCase ( self : List[Any] ): """simple docstring""" UpperCAmelCase__ = ( MaskFormerForInstanceSegmentation.from_pretrained("facebook/maskformer-swin-small-coco" ) .to(_lowercase ) .eval() ) UpperCAmelCase__ = self.default_image_processor UpperCAmelCase__ = prepare_img() UpperCAmelCase__ = image_processor(_lowercase , return_tensors="pt" ).to(_lowercase ) UpperCAmelCase__ = inputs["""pixel_values"""].shape # check size is divisible by 32 self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0 ) # check size self.assertEqual(_lowercase , (1, 3, 8_00, 10_88) ) with torch.no_grad(): UpperCAmelCase__ = model(**_lowercase ) # masks_queries_logits UpperCAmelCase__ = outputs.masks_queries_logits self.assertEqual( masks_queries_logits.shape , (1, model.config.decoder_config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4) , ) UpperCAmelCase__ = [ [-1.3_7_3_7_1_2_4, -1.7_7_2_4_9_3_7, -1.9_3_6_4_2_3_3], [-1.5_9_7_7_2_8_1, -1.9_8_6_7_9_3_9, -2.1_5_2_3_6_9_5], [-1.5_7_9_5_3_9_8, -1.9_2_6_9_8_3_2, -2.0_9_3_9_4_2], ] UpperCAmelCase__ = torch.tensor(_lowercase ).to(_lowercase ) self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3] , _lowercase , atol=_lowercase ) ) # class_queries_logits UpperCAmelCase__ = outputs.class_queries_logits self.assertEqual( class_queries_logits.shape , (1, model.config.decoder_config.num_queries, model.config.num_labels + 1) ) UpperCAmelCase__ = torch.tensor( [ [1.6512E00, -5.2572E00, -3.3519E00], [3.6169E-02, -5.9025E00, -2.9313E00], [1.0766E-04, -7.7630E00, -5.1263E00], ] ).to(_lowercase ) self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3] , _lowercase , atol=_lowercase ) ) def _UpperCAmelCase ( self : Any ): """simple docstring""" UpperCAmelCase__ = ( MaskFormerForInstanceSegmentation.from_pretrained("facebook/maskformer-resnet101-coco-stuff" ) .to(_lowercase ) .eval() ) UpperCAmelCase__ = self.default_image_processor UpperCAmelCase__ = prepare_img() UpperCAmelCase__ = image_processor(_lowercase , return_tensors="pt" ).to(_lowercase ) UpperCAmelCase__ = inputs["""pixel_values"""].shape # check size is divisible by 32 self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0 ) # check size self.assertEqual(_lowercase , (1, 3, 8_00, 10_88) ) with torch.no_grad(): UpperCAmelCase__ = model(**_lowercase ) # masks_queries_logits UpperCAmelCase__ = outputs.masks_queries_logits self.assertEqual( masks_queries_logits.shape , (1, model.config.decoder_config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4) , ) UpperCAmelCase__ = [[-0.9_0_4_6, -2.6_3_6_6, -4.6_0_6_2], [-3.4_1_7_9, -5.7_8_9_0, -8.8_0_5_7], [-4.9_1_7_9, -7.6_5_6_0, -1_0.7_7_1_1]] UpperCAmelCase__ = torch.tensor(_lowercase ).to(_lowercase ) self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3] , _lowercase , atol=_lowercase ) ) # class_queries_logits UpperCAmelCase__ = outputs.class_queries_logits self.assertEqual( class_queries_logits.shape , (1, model.config.decoder_config.num_queries, model.config.num_labels + 1) ) UpperCAmelCase__ = torch.tensor( [[4.7_1_8_8, -3.2_5_8_5, -2.8_8_5_7], [6.6_8_7_1, -2.9_1_8_1, -1.2_4_8_7], [7.2_4_4_9, -2.2_7_6_4, -2.1_8_7_4]] ).to(_lowercase ) self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3] , _lowercase , atol=_lowercase ) ) def _UpperCAmelCase ( self : Optional[Any] ): """simple docstring""" UpperCAmelCase__ = ( MaskFormerForInstanceSegmentation.from_pretrained("facebook/maskformer-swin-small-coco" ) .to(_lowercase ) .eval() ) UpperCAmelCase__ = self.default_image_processor UpperCAmelCase__ = image_processor( [np.zeros((3, 8_00, 13_33) ), np.zeros((3, 8_00, 13_33) )] , segmentation_maps=[np.zeros((3_84, 3_84) ).astype(np.floataa ), np.zeros((3_84, 3_84) ).astype(np.floataa )] , return_tensors="pt" , ) UpperCAmelCase__ = inputs["""pixel_values"""].to(_lowercase ) UpperCAmelCase__ = [el.to(_lowercase ) for el in inputs["""mask_labels"""]] UpperCAmelCase__ = [el.to(_lowercase ) for el in inputs["""class_labels"""]] with torch.no_grad(): UpperCAmelCase__ = model(**_lowercase ) self.assertTrue(outputs.loss is not None )
475
'''simple docstring''' import warnings from ...utils import logging from .image_processing_owlvit import OwlViTImageProcessor __UpperCamelCase = logging.get_logger(__name__) class _A ( __lowercase ): def __init__( self : int , *__magic_name__ : Optional[Any] , **__magic_name__ : Any ) -> None: """simple docstring""" warnings.warn( """The class OwlViTFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please""" """ use OwlViTImageProcessor instead.""" , __magic_name__ , ) super().__init__(*__magic_name__ , **__magic_name__ )
26
0
import doctest import sys import warnings from os.path import abspath, dirname, join import _pytest from transformers.testing_utils import HfDoctestModule, HfDocTestParser # allow having multiple repository checkouts and not needing to remember to rerun # 'pip install -e .[dev]' when switching between checkouts and running tests. A : Dict = abspath(join(dirname(__file__), 'src')) sys.path.insert(1, git_repo_path) # silence FutureWarning warnings in tests since often we can't act on them until # they become normal warnings - i.e. the tests still need to test the current functionality warnings.simplefilter(action='ignore', category=FutureWarning) def _lowerCAmelCase ( _lowerCAmelCase ) -> str: '''simple docstring''' config.addinivalue_line( "markers" , "is_pt_tf_cross_test: mark test to run only when PT and TF interactions are tested" ) config.addinivalue_line( "markers" , "is_pt_flax_cross_test: mark test to run only when PT and FLAX interactions are tested" ) config.addinivalue_line("markers" , "is_pipeline_test: mark test to run only when pipelines are tested" ) config.addinivalue_line("markers" , "is_staging_test: mark test to run only in the staging environment" ) config.addinivalue_line("markers" , "accelerate_tests: mark test that require accelerate" ) config.addinivalue_line("markers" , "tool_tests: mark the tool tests that are run on their specific schedule" ) def _lowerCAmelCase ( _lowerCAmelCase ) -> List[str]: '''simple docstring''' from transformers.testing_utils import pytest_addoption_shared pytest_addoption_shared(_lowerCamelCase ) def _lowerCAmelCase ( _lowerCAmelCase ) -> int: '''simple docstring''' from transformers.testing_utils import pytest_terminal_summary_main __snake_case = terminalreporter.config.getoption("--make-reports" ) if make_reports: pytest_terminal_summary_main(_lowerCamelCase , id=_lowerCamelCase ) def _lowerCAmelCase ( _lowerCAmelCase , _lowerCAmelCase ) -> List[str]: '''simple docstring''' if exitstatus == 5: __snake_case = 0 # Doctest custom flag to ignore output. A : Any = doctest.register_optionflag('IGNORE_RESULT') A : int = doctest.OutputChecker class UpperCamelCase( __lowercase ): def SCREAMING_SNAKE_CASE_ ( self : List[str] , SCREAMING_SNAKE_CASE : Optional[Any] , SCREAMING_SNAKE_CASE : Dict , SCREAMING_SNAKE_CASE : List[str] ) -> Optional[int]: '''simple docstring''' if IGNORE_RESULT & optionflags: return True return OutputChecker.check_output(self , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) A : Optional[int] = CustomOutputChecker A : Dict = HfDoctestModule A : List[str] = HfDocTestParser
371
'''simple docstring''' import argparse import torch from transformers import BlenderbotConfig, BlenderbotForConditionalGeneration from transformers.utils import logging logging.set_verbosity_info() __UpperCamelCase = logging.get_logger(__name__) __UpperCamelCase = [ ["attention", "attn"], ["encoder_attention", "encoder_attn"], ["q_lin", "q_proj"], ["k_lin", "k_proj"], ["v_lin", "v_proj"], ["out_lin", "out_proj"], ["norm_embeddings", "layernorm_embedding"], ["position_embeddings", "embed_positions"], ["embeddings", "embed_tokens"], ["ffn.lin", "fc"], ] def _a ( _lowerCamelCase ) -> Union[str, Any]: """simple docstring""" if k == "embeddings.weight": return "shared.weight" for parlai_name, hf_name in PATTERNS: __snake_case : List[str] = k.replace(_lowerCamelCase , _lowerCamelCase ) if k.startswith("""encoder""" ): __snake_case : Optional[int] = k.replace(""".attn""" , """.self_attn""" ) __snake_case : Tuple = k.replace("""norm1""" , """self_attn_layer_norm""" ) __snake_case : List[str] = k.replace("""norm2""" , """final_layer_norm""" ) elif k.startswith("""decoder""" ): __snake_case : List[Any] = k.replace("""norm1""" , """self_attn_layer_norm""" ) __snake_case : str = k.replace("""norm2""" , """encoder_attn_layer_norm""" ) __snake_case : Optional[int] = k.replace("""norm3""" , """final_layer_norm""" ) return k def _a ( _lowerCamelCase ) -> Any: """simple docstring""" __snake_case : Optional[int] = [ """model.encoder.layernorm_embedding.weight""", """model.encoder.layernorm_embedding.bias""", """model.decoder.layernorm_embedding.weight""", """model.decoder.layernorm_embedding.bias""", ] for k in keys: __snake_case : Optional[Any] = sd.pop(_lowerCamelCase ) __snake_case : List[str] = k.replace("""layernorm_embedding""" , """layer_norm""" ) assert new_k not in sd __snake_case : Union[str, Any] = v __UpperCamelCase = ["START"] @torch.no_grad() def _a ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) -> Dict: """simple docstring""" __snake_case : Optional[int] = torch.load(_lowerCamelCase , map_location="""cpu""" ) __snake_case : Dict = model["""model"""] __snake_case : Optional[int] = BlenderbotConfig.from_json_file(_lowerCamelCase ) __snake_case : Union[str, Any] = BlenderbotForConditionalGeneration(_lowerCamelCase ) __snake_case : List[Any] = m.model.state_dict().keys() __snake_case : int = [] __snake_case : Union[str, Any] = {} for k, v in sd.items(): if k in IGNORE_KEYS: continue __snake_case : Optional[int] = rename_state_dict_key(_lowerCamelCase ) if new_k not in valid_keys: failures.append([k, new_k] ) else: __snake_case : str = v if cfg.normalize_before: # Blenderbot-3B checkpoints. Rename layernorm_embedding -> layer_norm rename_layernorm_keys(_lowerCamelCase ) m.model.load_state_dict(_lowerCamelCase , strict=_lowerCamelCase ) m.half() m.save_pretrained(_lowerCamelCase ) if __name__ == "__main__": __UpperCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument("--src_path", type=str, help="like blenderbot-model.bin") parser.add_argument("--save_dir", default="hf_blenderbot", type=str, help="Where to save converted model.") parser.add_argument( "--hf_config_json", default="blenderbot-3b-config.json", type=str, help="Path to config to use" ) __UpperCamelCase = parser.parse_args() convert_parlai_checkpoint(args.src_path, args.save_dir, args.hf_config_json)
26
0
import argparse import os.path as osp import re import torch from safetensors.torch import load_file, save_file # =================# # UNet Conversion # # =================# lowercase_: List[str] = [ # (stable-diffusion, HF Diffusers) ('time_embed.0.weight', 'time_embedding.linear_1.weight'), ('time_embed.0.bias', 'time_embedding.linear_1.bias'), ('time_embed.2.weight', 'time_embedding.linear_2.weight'), ('time_embed.2.bias', 'time_embedding.linear_2.bias'), ('input_blocks.0.0.weight', 'conv_in.weight'), ('input_blocks.0.0.bias', 'conv_in.bias'), ('out.0.weight', 'conv_norm_out.weight'), ('out.0.bias', 'conv_norm_out.bias'), ('out.2.weight', 'conv_out.weight'), ('out.2.bias', 'conv_out.bias'), ] lowercase_: int = [ # (stable-diffusion, HF Diffusers) ('in_layers.0', 'norm1'), ('in_layers.2', 'conv1'), ('out_layers.0', 'norm2'), ('out_layers.3', 'conv2'), ('emb_layers.1', 'time_emb_proj'), ('skip_connection', 'conv_shortcut'), ] lowercase_: Union[str, Any] = [] # hardcoded number of downblocks and resnets/attentions... # would need smarter logic for other networks. for i in range(4): # loop over downblocks/upblocks for j in range(2): # loop over resnets/attentions for downblocks lowercase_: Dict = F"""down_blocks.{i}.resnets.{j}.""" lowercase_: Optional[Any] = F"""input_blocks.{3*i + j + 1}.0.""" unet_conversion_map_layer.append((sd_down_res_prefix, hf_down_res_prefix)) if i < 3: # no attention layers in down_blocks.3 lowercase_: str = F"""down_blocks.{i}.attentions.{j}.""" lowercase_: Any = F"""input_blocks.{3*i + j + 1}.1.""" unet_conversion_map_layer.append((sd_down_atn_prefix, hf_down_atn_prefix)) for j in range(3): # loop over resnets/attentions for upblocks lowercase_: str = F"""up_blocks.{i}.resnets.{j}.""" lowercase_: Dict = F"""output_blocks.{3*i + j}.0.""" unet_conversion_map_layer.append((sd_up_res_prefix, hf_up_res_prefix)) if i > 0: # no attention layers in up_blocks.0 lowercase_: List[Any] = F"""up_blocks.{i}.attentions.{j}.""" lowercase_: Optional[Any] = F"""output_blocks.{3*i + j}.1.""" unet_conversion_map_layer.append((sd_up_atn_prefix, hf_up_atn_prefix)) if i < 3: # no downsample in down_blocks.3 lowercase_: List[str] = F"""down_blocks.{i}.downsamplers.0.conv.""" lowercase_: List[str] = F"""input_blocks.{3*(i+1)}.0.op.""" unet_conversion_map_layer.append((sd_downsample_prefix, hf_downsample_prefix)) # no upsample in up_blocks.3 lowercase_: int = F"""up_blocks.{i}.upsamplers.0.""" lowercase_: int = F"""output_blocks.{3*i + 2}.{1 if i == 0 else 2}.""" unet_conversion_map_layer.append((sd_upsample_prefix, hf_upsample_prefix)) lowercase_: str = 'mid_block.attentions.0.' lowercase_: Optional[int] = 'middle_block.1.' unet_conversion_map_layer.append((sd_mid_atn_prefix, hf_mid_atn_prefix)) for j in range(2): lowercase_: Tuple = F"""mid_block.resnets.{j}.""" lowercase_: List[str] = F"""middle_block.{2*j}.""" unet_conversion_map_layer.append((sd_mid_res_prefix, hf_mid_res_prefix)) def _lowercase ( UpperCAmelCase_): """simple docstring""" snake_case__ : str = {k: k for k in unet_state_dict.keys()} for sd_name, hf_name in unet_conversion_map: snake_case__ : Dict = sd_name for k, v in mapping.items(): if "resnets" in k: for sd_part, hf_part in unet_conversion_map_resnet: snake_case__ : Optional[int] = v.replace(_lowerCamelCase , _lowerCamelCase) snake_case__ : Optional[Any] = v for k, v in mapping.items(): for sd_part, hf_part in unet_conversion_map_layer: snake_case__ : Tuple = v.replace(_lowerCamelCase , _lowerCamelCase) snake_case__ : int = v snake_case__ : str = {v: unet_state_dict[k] for k, v in mapping.items()} return new_state_dict # ================# # VAE Conversion # # ================# lowercase_: List[str] = [ # (stable-diffusion, HF Diffusers) ('nin_shortcut', 'conv_shortcut'), ('norm_out', 'conv_norm_out'), ('mid.attn_1.', 'mid_block.attentions.0.'), ] for i in range(4): # down_blocks have two resnets for j in range(2): lowercase_: int = F"""encoder.down_blocks.{i}.resnets.{j}.""" lowercase_: List[str] = F"""encoder.down.{i}.block.{j}.""" vae_conversion_map.append((sd_down_prefix, hf_down_prefix)) if i < 3: lowercase_: Any = F"""down_blocks.{i}.downsamplers.0.""" lowercase_: Tuple = F"""down.{i}.downsample.""" vae_conversion_map.append((sd_downsample_prefix, hf_downsample_prefix)) lowercase_: List[str] = F"""up_blocks.{i}.upsamplers.0.""" lowercase_: List[Any] = F"""up.{3-i}.upsample.""" vae_conversion_map.append((sd_upsample_prefix, hf_upsample_prefix)) # up_blocks have three resnets # also, up blocks in hf are numbered in reverse from sd for j in range(3): lowercase_: Union[str, Any] = F"""decoder.up_blocks.{i}.resnets.{j}.""" lowercase_: Optional[int] = F"""decoder.up.{3-i}.block.{j}.""" vae_conversion_map.append((sd_up_prefix, hf_up_prefix)) # this part accounts for mid blocks in both the encoder and the decoder for i in range(2): lowercase_: Union[str, Any] = F"""mid_block.resnets.{i}.""" lowercase_: List[str] = F"""mid.block_{i+1}.""" vae_conversion_map.append((sd_mid_res_prefix, hf_mid_res_prefix)) lowercase_: List[str] = [ # (stable-diffusion, HF Diffusers) ('norm.', 'group_norm.'), ('q.', 'query.'), ('k.', 'key.'), ('v.', 'value.'), ('proj_out.', 'proj_attn.'), ] def _lowercase ( UpperCAmelCase_): """simple docstring""" return w.reshape(*w.shape , 1 , 1) def _lowercase ( UpperCAmelCase_): """simple docstring""" snake_case__ : Tuple = {k: k for k in vae_state_dict.keys()} for k, v in mapping.items(): for sd_part, hf_part in vae_conversion_map: snake_case__ : Optional[int] = v.replace(_lowerCamelCase , _lowerCamelCase) snake_case__ : Dict = v for k, v in mapping.items(): if "attentions" in k: for sd_part, hf_part in vae_conversion_map_attn: snake_case__ : Optional[int] = v.replace(_lowerCamelCase , _lowerCamelCase) snake_case__ : List[str] = v snake_case__ : List[Any] = {v: vae_state_dict[k] for k, v in mapping.items()} snake_case__ : Optional[Any] = ["""q""", """k""", """v""", """proj_out"""] for k, v in new_state_dict.items(): for weight_name in weights_to_convert: if F'mid.attn_1.{weight_name}.weight' in k: print(F'Reshaping {k} for SD format') snake_case__ : Any = reshape_weight_for_sd(_lowerCamelCase) return new_state_dict # =========================# # Text Encoder Conversion # # =========================# lowercase_: str = [ # (stable-diffusion, HF Diffusers) ('resblocks.', 'text_model.encoder.layers.'), ('ln_1', 'layer_norm1'), ('ln_2', 'layer_norm2'), ('.c_fc.', '.fc1.'), ('.c_proj.', '.fc2.'), ('.attn', '.self_attn'), ('ln_final.', 'transformer.text_model.final_layer_norm.'), ('token_embedding.weight', 'transformer.text_model.embeddings.token_embedding.weight'), ('positional_embedding', 'transformer.text_model.embeddings.position_embedding.weight'), ] lowercase_: Optional[int] = {re.escape(x[1]): x[0] for x in textenc_conversion_lst} lowercase_: int = re.compile('|'.join(protected.keys())) # Ordering is from https://github.com/pytorch/pytorch/blob/master/test/cpp/api/modules.cpp lowercase_: Union[str, Any] = {'q': 0, 'k': 1, 'v': 2} def _lowercase ( UpperCAmelCase_): """simple docstring""" snake_case__ : Any = {} snake_case__ : List[str] = {} snake_case__ : Union[str, Any] = {} for k, v in text_enc_dict.items(): if ( k.endswith(""".self_attn.q_proj.weight""") or k.endswith(""".self_attn.k_proj.weight""") or k.endswith(""".self_attn.v_proj.weight""") ): snake_case__ : Tuple = k[: -len(""".q_proj.weight""")] snake_case__ : str = k[-len("""q_proj.weight""")] if k_pre not in capture_qkv_weight: snake_case__ : Optional[Any] = [None, None, None] snake_case__ : Optional[int] = v continue if ( k.endswith(""".self_attn.q_proj.bias""") or k.endswith(""".self_attn.k_proj.bias""") or k.endswith(""".self_attn.v_proj.bias""") ): snake_case__ : Tuple = k[: -len(""".q_proj.bias""")] snake_case__ : Optional[int] = k[-len("""q_proj.bias""")] if k_pre not in capture_qkv_bias: snake_case__ : int = [None, None, None] snake_case__ : Union[str, Any] = v continue snake_case__ : str = textenc_pattern.sub(lambda UpperCAmelCase_: protected[re.escape(m.group(0))] , _lowerCamelCase) snake_case__ : Tuple = v for k_pre, tensors in capture_qkv_weight.items(): if None in tensors: raise Exception("""CORRUPTED MODEL: one of the q-k-v values for the text encoder was missing""") snake_case__ : Tuple = textenc_pattern.sub(lambda UpperCAmelCase_: protected[re.escape(m.group(0))] , _lowerCamelCase) snake_case__ : int = torch.cat(_lowerCamelCase) for k_pre, tensors in capture_qkv_bias.items(): if None in tensors: raise Exception("""CORRUPTED MODEL: one of the q-k-v values for the text encoder was missing""") snake_case__ : Any = textenc_pattern.sub(lambda UpperCAmelCase_: protected[re.escape(m.group(0))] , _lowerCamelCase) snake_case__ : Optional[int] = torch.cat(_lowerCamelCase) return new_state_dict def _lowercase ( UpperCAmelCase_): """simple docstring""" return text_enc_dict if __name__ == "__main__": lowercase_: List[Any] = argparse.ArgumentParser() parser.add_argument('--model_path', default=None, type=str, required=True, help='Path to the model to convert.') parser.add_argument('--checkpoint_path', default=None, type=str, required=True, help='Path to the output model.') parser.add_argument('--half', action='store_true', help='Save weights in half precision.') parser.add_argument( '--use_safetensors', action='store_true', help='Save weights use safetensors, default is ckpt.' ) lowercase_: Tuple = parser.parse_args() assert args.model_path is not None, "Must provide a model path!" assert args.checkpoint_path is not None, "Must provide a checkpoint path!" # Path for safetensors lowercase_: Union[str, Any] = osp.join(args.model_path, 'unet', 'diffusion_pytorch_model.safetensors') lowercase_: Optional[int] = osp.join(args.model_path, 'vae', 'diffusion_pytorch_model.safetensors') lowercase_: Optional[int] = osp.join(args.model_path, 'text_encoder', 'model.safetensors') # Load models from safetensors if it exists, if it doesn't pytorch if osp.exists(unet_path): lowercase_: Union[str, Any] = load_file(unet_path, device='cpu') else: lowercase_: Dict = osp.join(args.model_path, 'unet', 'diffusion_pytorch_model.bin') lowercase_: List[str] = torch.load(unet_path, map_location='cpu') if osp.exists(vae_path): lowercase_: Dict = load_file(vae_path, device='cpu') else: lowercase_: int = osp.join(args.model_path, 'vae', 'diffusion_pytorch_model.bin') lowercase_: Optional[Any] = torch.load(vae_path, map_location='cpu') if osp.exists(text_enc_path): lowercase_: str = load_file(text_enc_path, device='cpu') else: lowercase_: Dict = osp.join(args.model_path, 'text_encoder', 'pytorch_model.bin') lowercase_: Optional[Any] = torch.load(text_enc_path, map_location='cpu') # Convert the UNet model lowercase_: List[str] = convert_unet_state_dict(unet_state_dict) lowercase_: str = {'model.diffusion_model.' + k: v for k, v in unet_state_dict.items()} # Convert the VAE model lowercase_: Union[str, Any] = convert_vae_state_dict(vae_state_dict) lowercase_: List[str] = {'first_stage_model.' + k: v for k, v in vae_state_dict.items()} # Easiest way to identify v2.0 model seems to be that the text encoder (OpenCLIP) is deeper lowercase_: Optional[int] = 'text_model.encoder.layers.22.layer_norm2.bias' in text_enc_dict if is_vaa_model: # Need to add the tag 'transformer' in advance so we can knock it out from the final layer-norm lowercase_: List[Any] = {'transformer.' + k: v for k, v in text_enc_dict.items()} lowercase_: Tuple = convert_text_enc_state_dict_vaa(text_enc_dict) lowercase_: Union[str, Any] = {'cond_stage_model.model.' + k: v for k, v in text_enc_dict.items()} else: lowercase_: Optional[Any] = convert_text_enc_state_dict(text_enc_dict) lowercase_: Optional[int] = {'cond_stage_model.transformer.' + k: v for k, v in text_enc_dict.items()} # Put together new checkpoint lowercase_: Any = {**unet_state_dict, **vae_state_dict, **text_enc_dict} if args.half: lowercase_: Tuple = {k: v.half() for k, v in state_dict.items()} if args.use_safetensors: save_file(state_dict, args.checkpoint_path) else: lowercase_: Optional[int] = {'state_dict': state_dict} torch.save(state_dict, args.checkpoint_path)
648
'''simple docstring''' import argparse import os import re import packaging.version __UpperCamelCase = "examples/" __UpperCamelCase = { "examples": (re.compile(R"^check_min_version\(\"[^\"]+\"\)\s*$", re.MULTILINE), "check_min_version(\"VERSION\")\n"), "init": (re.compile(R"^__version__\s+=\s+\"([^\"]+)\"\s*$", re.MULTILINE), "__version__ = \"VERSION\"\n"), "setup": (re.compile(R"^(\s*)version\s*=\s*\"[^\"]+\",", re.MULTILINE), R"\1version=\"VERSION\","), "doc": (re.compile(R"^(\s*)release\s*=\s*\"[^\"]+\"$", re.MULTILINE), "release = \"VERSION\"\n"), } __UpperCamelCase = { "init": "src/transformers/__init__.py", "setup": "setup.py", } __UpperCamelCase = "README.md" def _a ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) -> Tuple: """simple docstring""" with open(_lowerCamelCase , """r""" , encoding="""utf-8""" , newline="""\n""" ) as f: __snake_case : Union[str, Any] = f.read() __snake_case , __snake_case : List[Any] = REPLACE_PATTERNS[pattern] __snake_case : Optional[Any] = replace.replace("""VERSION""" , _lowerCamelCase ) __snake_case : Optional[Any] = re_pattern.sub(_lowerCamelCase , _lowerCamelCase ) with open(_lowerCamelCase , """w""" , encoding="""utf-8""" , newline="""\n""" ) as f: f.write(_lowerCamelCase ) def _a ( _lowerCamelCase ) -> Union[str, Any]: """simple docstring""" for folder, directories, fnames in os.walk(_lowerCamelCase ): # Removing some of the folders with non-actively maintained examples from the walk if "research_projects" in directories: directories.remove("""research_projects""" ) if "legacy" in directories: directories.remove("""legacy""" ) for fname in fnames: if fname.endswith(""".py""" ): update_version_in_file(os.path.join(_lowerCamelCase , _lowerCamelCase ) , _lowerCamelCase , pattern="""examples""" ) def _a ( _lowerCamelCase , _lowerCamelCase=False ) -> str: """simple docstring""" for pattern, fname in REPLACE_FILES.items(): update_version_in_file(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) if not patch: update_version_in_examples(_lowerCamelCase ) def _a ( ) -> Optional[int]: """simple docstring""" __snake_case : str = """🤗 Transformers currently provides the following architectures""" __snake_case : List[Any] = """1. Want to contribute a new model?""" with open(_lowerCamelCase , """r""" , encoding="""utf-8""" , newline="""\n""" ) as f: __snake_case : List[str] = f.readlines() # Find the start of the list. __snake_case : Optional[Any] = 0 while not lines[start_index].startswith(_start_prompt ): start_index += 1 start_index += 1 __snake_case : int = start_index # Update the lines in the model list. while not lines[index].startswith(_end_prompt ): if lines[index].startswith("""1.""" ): __snake_case : Optional[Any] = lines[index].replace( """https://huggingface.co/docs/transformers/main/model_doc""" , """https://huggingface.co/docs/transformers/model_doc""" , ) index += 1 with open(_lowerCamelCase , """w""" , encoding="""utf-8""" , newline="""\n""" ) as f: f.writelines(_lowerCamelCase ) def _a ( ) -> Union[str, Any]: """simple docstring""" with open(REPLACE_FILES["""init"""] , """r""" ) as f: __snake_case : List[Any] = f.read() __snake_case : str = REPLACE_PATTERNS["""init"""][0].search(_lowerCamelCase ).groups()[0] return packaging.version.parse(_lowerCamelCase ) def _a ( _lowerCamelCase=False ) -> int: """simple docstring""" __snake_case : List[Any] = get_version() if patch and default_version.is_devrelease: raise ValueError("""Can't create a patch version from the dev branch, checkout a released version!""" ) if default_version.is_devrelease: __snake_case : str = default_version.base_version elif patch: __snake_case : Optional[int] = F'''{default_version.major}.{default_version.minor}.{default_version.micro + 1}''' else: __snake_case : Dict = F'''{default_version.major}.{default_version.minor + 1}.0''' # Now let's ask nicely if that's the right one. __snake_case : Dict = input(F'''Which version are you releasing? [{default_version}]''' ) if len(_lowerCamelCase ) == 0: __snake_case : Any = default_version print(F'''Updating version to {version}.''' ) global_version_update(_lowerCamelCase , patch=_lowerCamelCase ) if not patch: print("""Cleaning main README, don't forget to run `make fix-copies`.""" ) clean_main_ref_in_model_list() def _a ( ) -> Tuple: """simple docstring""" __snake_case : Optional[Any] = get_version() __snake_case : Tuple = F'''{current_version.major}.{current_version.minor + 1}.0.dev0''' __snake_case : Union[str, Any] = current_version.base_version # Check with the user we got that right. __snake_case : int = input(F'''Which version are we developing now? [{dev_version}]''' ) if len(_lowerCamelCase ) == 0: __snake_case : Optional[int] = dev_version print(F'''Updating version to {version}.''' ) global_version_update(_lowerCamelCase ) print("""Cleaning main README, don't forget to run `make fix-copies`.""" ) clean_main_ref_in_model_list() if __name__ == "__main__": __UpperCamelCase = argparse.ArgumentParser() parser.add_argument("--post_release", action="store_true", help="Whether this is pre or post release.") parser.add_argument("--patch", action="store_true", help="Whether or not this is a patch release.") __UpperCamelCase = parser.parse_args() if not args.post_release: pre_release_work(patch=args.patch) elif args.patch: print("Nothing to do after a patch :-)") else: post_release_work()
26
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __A : Optional[Any] = {"configuration_wavlm": ["WAVLM_PRETRAINED_CONFIG_ARCHIVE_MAP", "WavLMConfig"]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : int = [ "WAVLM_PRETRAINED_MODEL_ARCHIVE_LIST", "WavLMForAudioFrameClassification", "WavLMForCTC", "WavLMForSequenceClassification", "WavLMForXVector", "WavLMModel", "WavLMPreTrainedModel", ] if TYPE_CHECKING: from .configuration_wavlm import WAVLM_PRETRAINED_CONFIG_ARCHIVE_MAP, WavLMConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_wavlm import ( WAVLM_PRETRAINED_MODEL_ARCHIVE_LIST, WavLMForAudioFrameClassification, WavLMForCTC, WavLMForSequenceClassification, WavLMForXVector, WavLMModel, WavLMPreTrainedModel, ) else: import sys __A : Tuple = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
27
def __lowerCAmelCase( _SCREAMING_SNAKE_CASE ) -> bool: """simple docstring""" if not isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): raise ValueError('check_bouncy() accepts only integer arguments' ) _A = str(_SCREAMING_SNAKE_CASE ) _A = ''.join(sorted(_SCREAMING_SNAKE_CASE ) ) return sorted_str_n != str_n and sorted_str_n[::-1] != str_n def __lowerCAmelCase( _SCREAMING_SNAKE_CASE = 99 ) -> int: """simple docstring""" if not 0 < percent < 100: raise ValueError('solution() only accepts values from 0 to 100' ) _A = 0 _A = 1 while True: if check_bouncy(_SCREAMING_SNAKE_CASE ): bouncy_num += 1 if (bouncy_num / num) * 100 >= percent: return num num += 1 if __name__ == "__main__": from doctest import testmod testmod() print(f"{solution(99)}")
27
1
import warnings from contextlib import contextmanager from ...processing_utils import ProcessorMixin from .feature_extraction_wavaveca import WavaVecaFeatureExtractor from .tokenization_wavaveca import WavaVecaCTCTokenizer class lowerCamelCase( __snake_case ): '''simple docstring''' __magic_name__ = 'Wav2Vec2FeatureExtractor' __magic_name__ = 'AutoTokenizer' def __init__( self , snake_case_ , snake_case_ ): super().__init__(snake_case_ , snake_case_ ) _A = self.feature_extractor _A = False @classmethod def lowerCAmelCase__ ( cls , snake_case_ , **snake_case_ ): try: return super().from_pretrained(snake_case_ , **snake_case_ ) except OSError: warnings.warn( F"Loading a tokenizer inside {cls.__name__} from a config that does not" ' include a `tokenizer_class` attribute is deprecated and will be ' 'removed in v5. Please add `\'tokenizer_class\': \'Wav2Vec2CTCTokenizer\'`' ' attribute to either your `config.json` or `tokenizer_config.json` ' 'file to suppress this warning: ' , snake_case_ , ) _A = WavaVecaFeatureExtractor.from_pretrained(snake_case_ , **snake_case_ ) _A = WavaVecaCTCTokenizer.from_pretrained(snake_case_ , **snake_case_ ) return cls(feature_extractor=snake_case_ , tokenizer=snake_case_ ) def __call__( self , *snake_case_ , **snake_case_ ): # For backward compatibility if self._in_target_context_manager: return self.current_processor(*snake_case_ , **snake_case_ ) if "raw_speech" in kwargs: warnings.warn('Using `raw_speech` as a keyword argument is deprecated. Use `audio` instead.' ) _A = kwargs.pop('raw_speech' ) else: _A = kwargs.pop('audio' , snake_case_ ) _A = kwargs.pop('sampling_rate' , snake_case_ ) _A = kwargs.pop('text' , snake_case_ ) if len(snake_case_ ) > 0: _A = args[0] _A = args[1:] if audio is None and text is None: raise ValueError('You need to specify either an `audio` or `text` input to process.' ) if audio is not None: _A = self.feature_extractor(snake_case_ , *snake_case_ , sampling_rate=snake_case_ , **snake_case_ ) if text is not None: _A = self.tokenizer(snake_case_ , **snake_case_ ) if text is None: return inputs elif audio is None: return encodings else: _A = encodings['input_ids'] return inputs def lowerCAmelCase__ ( self , *snake_case_ , **snake_case_ ): # For backward compatibility if self._in_target_context_manager: return self.current_processor.pad(*snake_case_ , **snake_case_ ) _A = kwargs.pop('input_features' , snake_case_ ) _A = kwargs.pop('labels' , snake_case_ ) if len(snake_case_ ) > 0: _A = args[0] _A = args[1:] if input_features is not None: _A = self.feature_extractor.pad(snake_case_ , *snake_case_ , **snake_case_ ) if labels is not None: _A = self.tokenizer.pad(snake_case_ , **snake_case_ ) if labels is None: return input_features elif input_features is None: return labels else: _A = labels['input_ids'] return input_features def lowerCAmelCase__ ( self , *snake_case_ , **snake_case_ ): return self.tokenizer.batch_decode(*snake_case_ , **snake_case_ ) def lowerCAmelCase__ ( self , *snake_case_ , **snake_case_ ): return self.tokenizer.decode(*snake_case_ , **snake_case_ ) @contextmanager def lowerCAmelCase__ ( self ): warnings.warn( '`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your ' 'labels by using the argument `text` of the regular `__call__` method (either in the same call as ' 'your audio inputs, or in a separate call.' ) _A = True _A = self.tokenizer yield _A = self.feature_extractor _A = False
27
def __lowerCAmelCase( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> float: """simple docstring""" return price * (1 + tax_rate) if __name__ == "__main__": print(f"{price_plus_tax(100, 0.2_5) = }") print(f"{price_plus_tax(1_2_5.5_0, 0.0_5) = }")
27
1
def __lowerCAmelCase( _SCREAMING_SNAKE_CASE ) -> int: """simple docstring""" if n == 1 or not isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): return 0 elif n == 2: return 1 else: _A = [0, 1] for i in range(2 , n + 1 ): sequence.append(sequence[i - 1] + sequence[i - 2] ) return sequence[n] def __lowerCAmelCase( _SCREAMING_SNAKE_CASE ) -> int: """simple docstring""" _A = 0 _A = 2 while digits < n: index += 1 _A = len(str(fibonacci(_SCREAMING_SNAKE_CASE ) ) ) return index def __lowerCAmelCase( _SCREAMING_SNAKE_CASE = 1_000 ) -> int: """simple docstring""" return fibonacci_digits_index(_SCREAMING_SNAKE_CASE ) if __name__ == "__main__": print(solution(int(str(input()).strip())))
27
from collections.abc import Callable def __lowerCAmelCase( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> float: """simple docstring""" _A = a _A = b if function(_SCREAMING_SNAKE_CASE ) == 0: # one of the a or b is a root for the function return a elif function(_SCREAMING_SNAKE_CASE ) == 0: return b elif ( function(_SCREAMING_SNAKE_CASE ) * function(_SCREAMING_SNAKE_CASE ) > 0 ): # if none of these are root and they are both positive or negative, # then this algorithm can't find the root raise ValueError('could not find root in given interval.' ) else: _A = start + (end - start) / 2.0 while abs(start - mid ) > 10**-7: # until precisely equals to 10^-7 if function(_SCREAMING_SNAKE_CASE ) == 0: return mid elif function(_SCREAMING_SNAKE_CASE ) * function(_SCREAMING_SNAKE_CASE ) < 0: _A = mid else: _A = mid _A = start + (end - start) / 2.0 return mid def __lowerCAmelCase( _SCREAMING_SNAKE_CASE ) -> float: """simple docstring""" return x**3 - 2 * x - 5 if __name__ == "__main__": print(bisection(f, 1, 1_000)) import doctest doctest.testmod()
27
1
def __lowerCAmelCase( _SCREAMING_SNAKE_CASE ) -> str: """simple docstring""" _A = 0 # if input_string is "aba" than new_input_string become "a|b|a" _A = '' _A = '' # append each character + "|" in new_string for range(0, length-1) for i in input_string[: len(_SCREAMING_SNAKE_CASE ) - 1]: new_input_string += i + "|" # append last character new_input_string += input_string[-1] # we will store the starting and ending of previous furthest ending palindromic # substring _A, _A = 0, 0 # length[i] shows the length of palindromic substring with center i _A = [1 for i in range(len(_SCREAMING_SNAKE_CASE ) )] # for each character in new_string find corresponding palindromic string _A = 0 for j in range(len(_SCREAMING_SNAKE_CASE ) ): _A = 1 if j > r else min(length[l + r - j] // 2 , r - j + 1 ) while ( j - k >= 0 and j + k < len(_SCREAMING_SNAKE_CASE ) and new_input_string[k + j] == new_input_string[j - k] ): k += 1 _A = 2 * k - 1 # does this string is ending after the previously explored end (that is r) ? # if yes the update the new r to the last index of this if j + k - 1 > r: _A = j - k + 1 # noqa: E741 _A = j + k - 1 # update max_length and start position if max_length < length[j]: _A = length[j] _A = j # create that string _A = new_input_string[start - max_length // 2 : start + max_length // 2 + 1] for i in s: if i != "|": output_string += i return output_string if __name__ == "__main__": import doctest doctest.testmod()
27
import unittest from transformers import AutoTokenizer, NystromformerConfig, is_torch_available from transformers.testing_utils import require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( NystromformerForMaskedLM, NystromformerForMultipleChoice, NystromformerForQuestionAnswering, NystromformerForSequenceClassification, NystromformerForTokenClassification, NystromformerModel, ) from transformers.models.nystromformer.modeling_nystromformer import NYSTROMFORMER_PRETRAINED_MODEL_ARCHIVE_LIST class lowerCamelCase: '''simple docstring''' def __init__( self , snake_case_ , snake_case_=13 , snake_case_=7 , snake_case_=True , snake_case_=True , snake_case_=True , snake_case_=True , snake_case_=99 , snake_case_=32 , snake_case_=5 , snake_case_=4 , snake_case_=37 , snake_case_="gelu" , snake_case_=0.1 , snake_case_=0.1 , snake_case_=512 , snake_case_=16 , snake_case_=2 , snake_case_=0.02 , snake_case_=3 , snake_case_=4 , snake_case_=None , ): _A = parent _A = batch_size _A = seq_length _A = is_training _A = use_input_mask _A = use_token_type_ids _A = use_labels _A = vocab_size _A = hidden_size _A = num_hidden_layers _A = num_attention_heads _A = intermediate_size _A = hidden_act _A = hidden_dropout_prob _A = attention_probs_dropout_prob _A = max_position_embeddings _A = type_vocab_size _A = type_sequence_label_size _A = initializer_range _A = num_labels _A = num_choices _A = scope def lowerCAmelCase__ ( self ): _A = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _A = None if self.use_input_mask: _A = random_attention_mask([self.batch_size, self.seq_length] ) _A = None if self.use_token_type_ids: _A = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) _A = None _A = None _A = None if self.use_labels: _A = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _A = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) _A = ids_tensor([self.batch_size] , self.num_choices ) _A = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def lowerCAmelCase__ ( self ): return NystromformerConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=snake_case_ , initializer_range=self.initializer_range , ) def lowerCAmelCase__ ( self , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ ): _A = NystromformerModel(config=snake_case_ ) model.to(snake_case_ ) model.eval() _A = model(snake_case_ , attention_mask=snake_case_ , token_type_ids=snake_case_ ) _A = model(snake_case_ , token_type_ids=snake_case_ ) _A = model(snake_case_ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def lowerCAmelCase__ ( self , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ ): _A = NystromformerForMaskedLM(config=snake_case_ ) model.to(snake_case_ ) model.eval() _A = model(snake_case_ , attention_mask=snake_case_ , token_type_ids=snake_case_ , labels=snake_case_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def lowerCAmelCase__ ( self , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ ): _A = NystromformerForQuestionAnswering(config=snake_case_ ) model.to(snake_case_ ) model.eval() _A = model( snake_case_ , attention_mask=snake_case_ , token_type_ids=snake_case_ , start_positions=snake_case_ , end_positions=snake_case_ , ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def lowerCAmelCase__ ( self , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ ): _A = self.num_labels _A = NystromformerForSequenceClassification(snake_case_ ) model.to(snake_case_ ) model.eval() _A = model(snake_case_ , attention_mask=snake_case_ , token_type_ids=snake_case_ , labels=snake_case_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def lowerCAmelCase__ ( self , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ ): _A = self.num_labels _A = NystromformerForTokenClassification(config=snake_case_ ) model.to(snake_case_ ) model.eval() _A = model(snake_case_ , attention_mask=snake_case_ , token_type_ids=snake_case_ , labels=snake_case_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def lowerCAmelCase__ ( self , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ ): _A = self.num_choices _A = NystromformerForMultipleChoice(config=snake_case_ ) model.to(snake_case_ ) model.eval() _A = input_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() _A = token_type_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() _A = input_mask.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() _A = model( snake_case_ , attention_mask=snake_case_ , token_type_ids=snake_case_ , labels=snake_case_ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def lowerCAmelCase__ ( self ): _A = self.prepare_config_and_inputs() ( ( _A ), ( _A ), ( _A ), ( _A ), ( _A ), ( _A ), ( _A ), ) = config_and_inputs _A = {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask} return config, inputs_dict @require_torch class lowerCamelCase( __snake_case , __snake_case , unittest.TestCase ): '''simple docstring''' __magic_name__ = ( ( NystromformerModel, NystromformerForMaskedLM, NystromformerForMultipleChoice, NystromformerForQuestionAnswering, NystromformerForSequenceClassification, NystromformerForTokenClassification, ) if is_torch_available() else () ) __magic_name__ = ( { 'feature-extraction': NystromformerModel, 'fill-mask': NystromformerForMaskedLM, 'question-answering': NystromformerForQuestionAnswering, 'text-classification': NystromformerForSequenceClassification, 'token-classification': NystromformerForTokenClassification, 'zero-shot': NystromformerForSequenceClassification, } if is_torch_available() else {} ) __magic_name__ = False __magic_name__ = False def lowerCAmelCase__ ( self ): _A = NystromformerModelTester(self ) _A = ConfigTester(self , config_class=snake_case_ , hidden_size=37 ) def lowerCAmelCase__ ( self ): self.config_tester.run_common_tests() def lowerCAmelCase__ ( self ): _A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*snake_case_ ) def lowerCAmelCase__ ( self ): _A = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: _A = type self.model_tester.create_and_check_model(*snake_case_ ) def lowerCAmelCase__ ( self ): _A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*snake_case_ ) def lowerCAmelCase__ ( self ): _A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_multiple_choice(*snake_case_ ) def lowerCAmelCase__ ( self ): _A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*snake_case_ ) def lowerCAmelCase__ ( self ): _A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*snake_case_ ) def lowerCAmelCase__ ( self ): _A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*snake_case_ ) @slow def lowerCAmelCase__ ( self ): for model_name in NYSTROMFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _A = NystromformerModel.from_pretrained(snake_case_ ) self.assertIsNotNone(snake_case_ ) @require_torch class lowerCamelCase( unittest.TestCase ): '''simple docstring''' @slow def lowerCAmelCase__ ( self ): _A = NystromformerModel.from_pretrained('uw-madison/nystromformer-512' ) _A = torch.tensor([[0, 1, 2, 3, 4, 5]] ) with torch.no_grad(): _A = model(snake_case_ )[0] _A = torch.Size((1, 6, 768) ) self.assertEqual(output.shape , snake_case_ ) _A = torch.tensor( [[[-0.4532, -0.0936, 0.5137], [-0.2676, 0.0628, 0.6186], [-0.3629, -0.1726, 0.4716]]] ) self.assertTrue(torch.allclose(output[:, :3, :3] , snake_case_ , atol=1E-4 ) ) @slow def lowerCAmelCase__ ( self ): _A = 'the [MASK] of Belgium is Brussels' _A = AutoTokenizer.from_pretrained('uw-madison/nystromformer-512' ) _A = NystromformerForMaskedLM.from_pretrained('uw-madison/nystromformer-512' ) _A = tokenizer(snake_case_ , return_tensors='pt' ) with torch.no_grad(): _A = model(encoding.input_ids ).logits _A = token_logits[:, 2, :].argmax(-1 )[0] self.assertEqual(tokenizer.decode(snake_case_ ) , 'capital' )
27
1
def __lowerCAmelCase( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Dict: """simple docstring""" _A = [1] for i in range(2 , _SCREAMING_SNAKE_CASE ): factorials.append(factorials[-1] * i ) assert 0 <= k < factorials[-1] * n, "k out of bounds" _A = [] _A = list(range(_SCREAMING_SNAKE_CASE ) ) # Find permutation while factorials: _A = factorials.pop() _A, _A = divmod(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) permutation.append(elements[number] ) elements.remove(elements[number] ) permutation.append(elements[0] ) return permutation if __name__ == "__main__": import doctest doctest.testmod()
27
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) __A : Dict = { "configuration_blenderbot": [ "BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP", "BlenderbotConfig", "BlenderbotOnnxConfig", ], "tokenization_blenderbot": ["BlenderbotTokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : str = ["BlenderbotTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : str = [ "BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST", "BlenderbotForCausalLM", "BlenderbotForConditionalGeneration", "BlenderbotModel", "BlenderbotPreTrainedModel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Union[str, Any] = [ "TFBlenderbotForConditionalGeneration", "TFBlenderbotModel", "TFBlenderbotPreTrainedModel", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Optional[Any] = [ "FlaxBlenderbotForConditionalGeneration", "FlaxBlenderbotModel", "FlaxBlenderbotPreTrainedModel", ] if TYPE_CHECKING: from .configuration_blenderbot import ( BLENDERBOT_PRETRAINED_CONFIG_ARCHIVE_MAP, BlenderbotConfig, BlenderbotOnnxConfig, ) from .tokenization_blenderbot import BlenderbotTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_blenderbot_fast import BlenderbotTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_blenderbot import ( BLENDERBOT_PRETRAINED_MODEL_ARCHIVE_LIST, BlenderbotForCausalLM, BlenderbotForConditionalGeneration, BlenderbotModel, BlenderbotPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_blenderbot import ( TFBlenderbotForConditionalGeneration, TFBlenderbotModel, TFBlenderbotPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_blenderbot import ( FlaxBlenderbotForConditionalGeneration, FlaxBlenderbotModel, FlaxBlenderbotPreTrainedModel, ) else: import sys __A : str = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
27
1
import gc import unittest from diffusers import FlaxStableDiffusionInpaintPipeline from diffusers.utils import is_flax_available, load_image, slow from diffusers.utils.testing_utils import require_flax if is_flax_available(): import jax import jax.numpy as jnp from flax.jax_utils import replicate from flax.training.common_utils import shard @slow @require_flax class lowerCamelCase( unittest.TestCase ): '''simple docstring''' def lowerCAmelCase__ ( self ): # clean up the VRAM after each test super().tearDown() gc.collect() def lowerCAmelCase__ ( self ): _A = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/sd2-inpaint/init_image.png' ) _A = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png' ) _A = 'xvjiarui/stable-diffusion-2-inpainting' _A, _A = FlaxStableDiffusionInpaintPipeline.from_pretrained(snake_case_ , safety_checker=snake_case_ ) _A = 'Face of a yellow cat, high resolution, sitting on a park bench' _A = jax.random.PRNGKey(0 ) _A = 50 _A = jax.device_count() _A = num_samples * [prompt] _A = num_samples * [init_image] _A = num_samples * [mask_image] _A, _A, _A = pipeline.prepare_inputs(snake_case_ , snake_case_ , snake_case_ ) # shard inputs and rng _A = replicate(snake_case_ ) _A = jax.random.split(snake_case_ , jax.device_count() ) _A = shard(snake_case_ ) _A = shard(snake_case_ ) _A = shard(snake_case_ ) _A = pipeline( snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ , jit=snake_case_ ) _A = output.images.reshape(snake_case_ , 512 , 512 , 3 ) _A = images[0, 253:256, 253:256, -1] _A = jnp.asarray(jax.device_get(image_slice.flatten() ) ) _A = jnp.array( [0.361_1307, 0.3764_9736, 0.375_7408, 0.3821_3953, 0.3929_5167, 0.384_1631, 0.4155_4978, 0.413_7475, 0.421_7084] ) print(F"output_slice: {output_slice}" ) assert jnp.abs(output_slice - expected_slice ).max() < 1E-2
27
import sys 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 __A : List[Any] = "python tqdm regex requests packaging filelock numpy tokenizers".split() if sys.version_info < (3, 7): pkgs_to_check_at_runtime.append("dataclasses") if sys.version_info < (3, 8): pkgs_to_check_at_runtime.append("importlib_metadata") 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 require_version_core(deps[pkg]) else: raise ValueError(f"can't find {pkg} in {deps.keys()}, check dependency_versions_table.py") def __lowerCAmelCase( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=None ) -> Union[str, Any]: """simple docstring""" require_version(deps[pkg] , _SCREAMING_SNAKE_CASE )
27
1
from google.protobuf import descriptor as _descriptor from google.protobuf import descriptor_pool as _descriptor_pool from google.protobuf import symbol_database as _symbol_database from google.protobuf.internal import builder as _builder # @@protoc_insertion_point(imports) __A : List[Any] = _symbol_database.Default() __A : Union[str, Any] = _descriptor_pool.Default().AddSerializedFile( B"\n\x19sentencepiece_model.proto\x12\rsentencepiece\"\x80\x0c\n\x0bTrainerSpec\x12\r\n\x05input\x18\x01 \x03(\t\x12\x14\n\x0cinput_format\x18\x07 \x01(\t\x12\x14\n\x0cmodel_prefix\x18\x02 \x01(\t\x12\x41\n\nmodel_type\x18\x03 \x01(\x0e\x32$.sentencepiece.TrainerSpec.ModelType:\x07UNIGRAM\x12\x18\n\nvocab_size\x18\x04 \x01(\x05:\x04\x38\x30\x30\x30\x12\x17\n\x0f\x61\x63\x63\x65pt_language\x18\x05 \x03(\t\x12 \n\x15self_test_sample_size\x18\x06 \x01(\x05:\x01\x30\x12*\n\x1b\x65nable_differential_privacy\x18\x32 \x01(\x08:\x05\x66\x61lse\x12+\n differential_privacy_noise_level\x18\x33 \x01(\x02:\x01\x30\x12\x32\n\'differential_privacy_clipping_threshold\x18\x34 \x01(\x04:\x01\x30\x12\"\n\x12\x63haracter_coverage\x18\n \x01(\x02:\x06\x30.9995\x12\x1e\n\x13input_sentence_size\x18\x0b \x01(\x04:\x01\x30\x12$\n\x16shuffle_input_sentence\x18\x13 \x01(\x08:\x04true\x12 \n\x14mining_sentence_size\x18\x0c \x01(\x05\x42\x02\x18\x01\x12\"\n\x16training_sentence_size\x18\r \x01(\x05\x42\x02\x18\x01\x12(\n\x17seed_sentencepiece_size\x18\x0e \x01(\x05:\x07\x31\x30\x30\x30\x30\x30\x30\x12\x1e\n\x10shrinking_factor\x18\x0f \x01(\x02:\x04\x30.75\x12!\n\x13max_sentence_length\x18\x12 \x01(\x05:\x04\x34\x31\x39\x32\x12\x17\n\x0bnum_threads\x18\x10 \x01(\x05:\x02\x31\x36\x12\x1d\n\x12num_sub_iterations\x18\x11 \x01(\x05:\x01\x32\x12$\n\x18max_sentencepiece_length\x18\x14 \x01(\x05:\x02\x31\x36\x12%\n\x17split_by_unicode_script\x18\x15 \x01(\x08:\x04true\x12\x1d\n\x0fsplit_by_number\x18\x17 \x01(\x08:\x04true\x12!\n\x13split_by_whitespace\x18\x16 \x01(\x08:\x04true\x12)\n\x1atreat_whitespace_as_suffix\x18\x18 \x01(\x08:\x05\x66\x61lse\x12+\n\x1c\x61llow_whitespace_only_pieces\x18\x1a \x01(\x08:\x05\x66\x61lse\x12\x1b\n\x0csplit_digits\x18\x19 \x01(\x08:\x05\x66\x61lse\x12#\n\x19pretokenization_delimiter\x18\x35 \x01(\t:\x00\x12\x17\n\x0f\x63ontrol_symbols\x18\x1e \x03(\t\x12\x1c\n\x14user_defined_symbols\x18\x1f \x03(\t\x12\x16\n\x0erequired_chars\x18$ \x01(\t\x12\x1c\n\rbyte_fallback\x18# \x01(\x08:\x05\x66\x61lse\x12+\n\x1dvocabulary_output_piece_score\x18 \x01(\x08:\x04true\x12\x1e\n\x10hard_vocab_limit\x18! \x01(\x08:\x04true\x12\x1c\n\ruse_all_vocab\x18\" \x01(\x08:\x05\x66\x61lse\x12\x11\n\x06unk_id\x18( \x01(\x05:\x01\x30\x12\x11\n\x06\x62os_id\x18) \x01(\x05:\x01\x31\x12\x11\n\x06\x65os_id\x18* \x01(\x05:\x01\x32\x12\x12\n\x06pad_id\x18+ \x01(\x05:\x02-1\x12\x18\n\tunk_piece\x18- \x01(\t:\x05<unk>\x12\x16\n\tbos_piece\x18. \x01(\t:\x03<s>\x12\x17\n\teos_piece\x18/ \x01(\t:\x04</s>\x12\x18\n\tpad_piece\x18\x30 \x01(\t:\x05<pad>\x12\x1a\n\x0bunk_surface\x18, \x01(\t:\x05 \xe2\x81\x87 \x12+\n\x1ctrain_extremely_large_corpus\x18\x31 \x01(\x08:\x05\x66\x61lse\"5\n\tModelType\x12\x0b\n\x07UNIGRAM\x10\x01\x12\x07\n\x03\x42PE\x10\x02\x12\x08\n\x04WORD\x10\x03\x12\x08\n\x04\x43HAR\x10\x04*\t\x08\xc8\x01\x10\x80\x80\x80\x80\x02\"\xd1\x01\n\x0eNormalizerSpec\x12\x0c\n\x04name\x18\x01 \x01(\t\x12\x1c\n\x14precompiled_charsmap\x18\x02 \x01(\x0c\x12\x1e\n\x10\x61\x64\x64_dummy_prefix\x18\x03 \x01(\x08:\x04true\x12&\n\x18remove_extra_whitespaces\x18\x04 \x01(\x08:\x04true\x12 \n\x12\x65scape_whitespaces\x18\x05 \x01(\x08:\x04true\x12\x1e\n\x16normalization_rule_tsv\x18\x06 \x01(\t*\t\x08\xc8\x01\x10\x80\x80\x80\x80\x02\"y\n\x0cSelfTestData\x12\x33\n\x07samples\x18\x01 \x03(\x0b\x32\".sentencepiece.SelfTestData.Sample\x1a)\n\x06Sample\x12\r\n\x05input\x18\x01 \x01(\t\x12\x10\n\x08\x65xpected\x18\x02 \x01(\t*\t\x08\xc8\x01\x10\x80\x80\x80\x80\x02\"\xfe\x03\n\nModelProto\x12\x37\n\x06pieces\x18\x01 \x03(\x0b\x32\'.sentencepiece.ModelProto.SentencePiece\x12\x30\n\x0ctrainer_spec\x18\x02 \x01(\x0b\x32\x1a.sentencepiece.TrainerSpec\x12\x36\n\x0fnormalizer_spec\x18\x03 \x01(\x0b\x32\x1d.sentencepiece.NormalizerSpec\x12\x33\n\x0eself_test_data\x18\x04 \x01(\x0b\x32\x1b.sentencepiece.SelfTestData\x12\x38\n\x11\x64\x65normalizer_spec\x18\x05 \x01(\x0b\x32\x1d.sentencepiece.NormalizerSpec\x1a\xd2\x01\n\rSentencePiece\x12\r\n\x05piece\x18\x01 \x01(\t\x12\r\n\x05score\x18\x02 \x01(\x02\x12\x42\n\x04type\x18\x03 \x01(\x0e\x32,.sentencepiece.ModelProto.SentencePiece.Type:\x06NORMAL\"T\n\x04Type\x12\n\n\x06NORMAL\x10\x01\x12\x0b\n\x07UNKNOWN\x10\x02\x12\x0b\n\x07\x43ONTROL\x10\x03\x12\x10\n\x0cUSER_DEFINED\x10\x04\x12\x08\n\x04\x42YTE\x10\x06\x12\n\n\x06UNUSED\x10\x05*\t\x08\xc8\x01\x10\x80\x80\x80\x80\x02*\t\x08\xc8\x01\x10\x80\x80\x80\x80\x02\x42\x02H\x03" ) __A : List[Any] = globals() _builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, _globals) _builder.BuildTopDescriptorsAndMessages(DESCRIPTOR, "sentencepiece_model_pb2", _globals) if _descriptor._USE_C_DESCRIPTORS is False: __A : int = None __A : List[str] = B"H\003" # (generated by protobuf compiler, but `_TRAINERSPEC` is not defined) # _TRAINERSPEC.fields_by_name["mining_sentence_size"]._options = None # _TRAINERSPEC.fields_by_name["mining_sentence_size"]._serialized_options = b"\030\001" # _TRAINERSPEC.fields_by_name["training_sentence_size"]._options = None # _TRAINERSPEC.fields_by_name["training_sentence_size"]._serialized_options = b"\030\001" __A : int = 45 __A : int = 1_581 __A : List[Any] = 1_517 __A : Optional[Any] = 1_570 __A : List[str] = 1_584 __A : Dict = 1_793 __A : str = 1_795 __A : Tuple = 1_916 __A : str = 1_864 __A : Union[str, Any] = 1_905 __A : int = 1_919 __A : List[Any] = 2_429 __A : Any = 2_208 __A : Tuple = 2_418 __A : Union[str, Any] = 2_323 __A : Union[str, Any] = 2_407 # @@protoc_insertion_point(module_scope)
27
def __lowerCAmelCase( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> int: """simple docstring""" return int((input_a, input_a).count(0 ) != 0 ) def __lowerCAmelCase( ) -> None: """simple docstring""" assert nand_gate(0 , 0 ) == 1 assert nand_gate(0 , 1 ) == 1 assert nand_gate(1 , 0 ) == 1 assert nand_gate(1 , 1 ) == 0 if __name__ == "__main__": print(nand_gate(0, 0)) print(nand_gate(0, 1)) print(nand_gate(1, 0)) print(nand_gate(1, 1))
27
1
from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional, Union from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging if TYPE_CHECKING: from ... import FeatureExtractionMixin, PreTrainedTokenizerBase, TensorType __A : Optional[int] = logging.get_logger(__name__) __A : int = { "microsoft/deberta-v2-xlarge": "https://huggingface.co/microsoft/deberta-v2-xlarge/resolve/main/config.json", "microsoft/deberta-v2-xxlarge": "https://huggingface.co/microsoft/deberta-v2-xxlarge/resolve/main/config.json", "microsoft/deberta-v2-xlarge-mnli": ( "https://huggingface.co/microsoft/deberta-v2-xlarge-mnli/resolve/main/config.json" ), "microsoft/deberta-v2-xxlarge-mnli": ( "https://huggingface.co/microsoft/deberta-v2-xxlarge-mnli/resolve/main/config.json" ), } class lowerCamelCase( __snake_case ): '''simple docstring''' __magic_name__ = 'deberta-v2' def __init__( self , snake_case_=12_8100 , snake_case_=1536 , snake_case_=24 , snake_case_=24 , snake_case_=6144 , snake_case_="gelu" , snake_case_=0.1 , snake_case_=0.1 , snake_case_=512 , snake_case_=0 , snake_case_=0.02 , snake_case_=1E-7 , snake_case_=False , snake_case_=-1 , snake_case_=0 , snake_case_=True , snake_case_=None , snake_case_=0 , snake_case_="gelu" , **snake_case_ , ): super().__init__(**snake_case_ ) _A = hidden_size _A = num_hidden_layers _A = num_attention_heads _A = intermediate_size _A = hidden_act _A = hidden_dropout_prob _A = attention_probs_dropout_prob _A = max_position_embeddings _A = type_vocab_size _A = initializer_range _A = relative_attention _A = max_relative_positions _A = pad_token_id _A = position_biased_input # Backwards compatibility if type(snake_case_ ) == str: _A = [x.strip() for x in pos_att_type.lower().split('|' )] _A = pos_att_type _A = vocab_size _A = layer_norm_eps _A = kwargs.get('pooler_hidden_size' , snake_case_ ) _A = pooler_dropout _A = pooler_hidden_act class lowerCamelCase( __snake_case ): '''simple docstring''' @property def lowerCAmelCase__ ( self ): if self.task == "multiple-choice": _A = {0: 'batch', 1: 'choice', 2: 'sequence'} else: _A = {0: 'batch', 1: 'sequence'} if self._config.type_vocab_size > 0: return OrderedDict( [('input_ids', dynamic_axis), ('attention_mask', dynamic_axis), ('token_type_ids', dynamic_axis)] ) else: return OrderedDict([('input_ids', dynamic_axis), ('attention_mask', dynamic_axis)] ) @property def lowerCAmelCase__ ( self ): return 12 def lowerCAmelCase__ ( self , snake_case_ , snake_case_ = -1 , snake_case_ = -1 , snake_case_ = -1 , snake_case_ = False , snake_case_ = None , snake_case_ = 3 , snake_case_ = 40 , snake_case_ = 40 , snake_case_ = None , ): _A = super().generate_dummy_inputs(preprocessor=snake_case_ , framework=snake_case_ ) if self._config.type_vocab_size == 0 and "token_type_ids" in dummy_inputs: del dummy_inputs["token_type_ids"] return dummy_inputs
27
from __future__ import annotations import unittest from transformers import EsmConfig, is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import numpy import tensorflow as tf from transformers.models.esm.modeling_tf_esm import ( TF_ESM_PRETRAINED_MODEL_ARCHIVE_LIST, TFEsmForMaskedLM, TFEsmForSequenceClassification, TFEsmForTokenClassification, TFEsmModel, ) class lowerCamelCase: '''simple docstring''' def __init__( self , snake_case_ , ): _A = parent _A = 13 _A = 7 _A = True _A = True _A = True _A = 99 _A = 32 _A = 2 _A = 4 _A = 37 _A = 'gelu' _A = 0.1 _A = 0.1 _A = 512 _A = 16 _A = 2 _A = 0.02 _A = 3 _A = 4 _A = None def lowerCAmelCase__ ( self ): _A = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _A = None if self.use_input_mask: _A = random_attention_mask([self.batch_size, self.seq_length] ) _A = None _A = None _A = None if self.use_labels: _A = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _A = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) _A = ids_tensor([self.batch_size] , self.num_choices ) _A = EsmConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , pad_token_id=1 , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , ) return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels def lowerCAmelCase__ ( self ): ( ( _A ), ( _A ), ( _A ), ( _A ), ( _A ), ( _A ), ) = self.prepare_config_and_inputs() _A = True _A = floats_tensor([self.batch_size, self.seq_length, self.hidden_size] ) _A = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2 ) return ( config, input_ids, input_mask, sequence_labels, token_labels, choice_labels, encoder_hidden_states, encoder_attention_mask, ) def lowerCAmelCase__ ( self , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ ): _A = TFEsmModel(config=snake_case_ ) _A = {'input_ids': input_ids, 'attention_mask': input_mask} _A = model(snake_case_ ) _A = [input_ids, input_mask] _A = model(snake_case_ ) _A = model(snake_case_ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def lowerCAmelCase__ ( self , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ , ): _A = True _A = TFEsmModel(config=snake_case_ ) _A = { 'input_ids': input_ids, 'attention_mask': input_mask, 'encoder_hidden_states': encoder_hidden_states, 'encoder_attention_mask': encoder_attention_mask, } _A = model(snake_case_ ) _A = [input_ids, input_mask] _A = model(snake_case_ , encoder_hidden_states=snake_case_ ) # Also check the case where encoder outputs are not passed _A = model(snake_case_ , attention_mask=snake_case_ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def lowerCAmelCase__ ( self , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ ): _A = TFEsmForMaskedLM(config=snake_case_ ) _A = model([input_ids, input_mask] ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def lowerCAmelCase__ ( self , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ ): _A = self.num_labels _A = TFEsmForTokenClassification(config=snake_case_ ) _A = {'input_ids': input_ids, 'attention_mask': input_mask} _A = model(snake_case_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def lowerCAmelCase__ ( self ): _A = self.prepare_config_and_inputs() ( ( _A ), ( _A ), ( _A ), ( _A ), ( _A ), ( _A ), ) = config_and_inputs _A = {'input_ids': input_ids, 'attention_mask': input_mask} return config, inputs_dict @require_tf class lowerCamelCase( __snake_case , __snake_case , unittest.TestCase ): '''simple docstring''' __magic_name__ = ( ( TFEsmModel, TFEsmForMaskedLM, TFEsmForSequenceClassification, TFEsmForTokenClassification, ) if is_tf_available() else () ) __magic_name__ = ( { 'feature-extraction': TFEsmModel, 'fill-mask': TFEsmForMaskedLM, 'text-classification': TFEsmForSequenceClassification, 'token-classification': TFEsmForTokenClassification, 'zero-shot': TFEsmForSequenceClassification, } if is_tf_available() else {} ) __magic_name__ = False __magic_name__ = False def lowerCAmelCase__ ( self ): _A = TFEsmModelTester(self ) _A = ConfigTester(self , config_class=snake_case_ , hidden_size=37 ) def lowerCAmelCase__ ( self ): self.config_tester.run_common_tests() def lowerCAmelCase__ ( self ): _A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*snake_case_ ) def lowerCAmelCase__ ( self ): _A = self.model_tester.prepare_config_and_inputs_for_decoder() self.model_tester.create_and_check_model_as_decoder(*snake_case_ ) def lowerCAmelCase__ ( self ): _A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*snake_case_ ) def lowerCAmelCase__ ( self ): _A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*snake_case_ ) @slow def lowerCAmelCase__ ( self ): for model_name in TF_ESM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _A = TFEsmModel.from_pretrained(snake_case_ ) self.assertIsNotNone(snake_case_ ) @unittest.skip('Protein models do not support embedding resizing.' ) def lowerCAmelCase__ ( self ): pass @unittest.skip('Protein models do not support embedding resizing.' ) def lowerCAmelCase__ ( self ): pass def lowerCAmelCase__ ( self ): _A, _A = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _A = model_class(snake_case_ ) assert isinstance(model.get_input_embeddings() , tf.keras.layers.Layer ) if model_class is TFEsmForMaskedLM: # Output embedding test differs from the main test because they're a matrix, not a layer _A = model.get_bias() assert isinstance(snake_case_ , snake_case_ ) for k, v in name.items(): assert isinstance(snake_case_ , tf.Variable ) else: _A = model.get_output_embeddings() assert x is None _A = model.get_bias() assert name is None @require_tf class lowerCamelCase( unittest.TestCase ): '''simple docstring''' @slow def lowerCAmelCase__ ( self ): _A = TFEsmForMaskedLM.from_pretrained('facebook/esm2_t6_8M_UR50D' ) _A = tf.constant([[0, 1, 2, 3, 4, 5]] ) _A = model(snake_case_ )[0] _A = [1, 6, 33] self.assertEqual(list(output.numpy().shape ) , snake_case_ ) # compare the actual values for a slice. _A = tf.constant( [ [ [8.92_1518, -10.58_9814, -6.467_1307], [-6.396_7156, -13.91_1377, -1.121_1915], [-7.78_1247, -13.95_1557, -3.74_0592], ] ] ) self.assertTrue(numpy.allclose(output[:, :3, :3].numpy() , expected_slice.numpy() , atol=1E-2 ) ) @slow def lowerCAmelCase__ ( self ): _A = TFEsmModel.from_pretrained('facebook/esm2_t6_8M_UR50D' ) _A = tf.constant([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]] ) _A = model(snake_case_ )[0] # compare the actual values for a slice. _A = tf.constant( [ [ [0.1444_3092, 0.5412_5327, 0.324_7739], [0.3034_0484, 0.0052_6676, 0.3107_7722], [0.3227_8043, -0.2498_7096, 0.341_4628], ] ] ) self.assertTrue(numpy.allclose(output[:, :3, :3].numpy() , expected_slice.numpy() , atol=1E-4 ) )
27
1
import copy from typing import TYPE_CHECKING, Any, Mapping, Optional, OrderedDict from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging from ..auto.configuration_auto import AutoConfig if TYPE_CHECKING: from ... import PreTrainedTokenizerBase, TensorType __A : List[str] = logging.get_logger(__name__) class lowerCamelCase( __snake_case ): '''simple docstring''' __magic_name__ = 'vision-encoder-decoder' __magic_name__ = True def __init__( self , **snake_case_ ): super().__init__(**snake_case_ ) if "encoder" not in kwargs or "decoder" not in kwargs: raise ValueError( F"A configuraton of type {self.model_type} cannot be instantiated because " F"not both `encoder` and `decoder` sub-configurations are passed, but only {kwargs}" ) _A = kwargs.pop('encoder' ) _A = encoder_config.pop('model_type' ) _A = kwargs.pop('decoder' ) _A = decoder_config.pop('model_type' ) _A = AutoConfig.for_model(snake_case_ , **snake_case_ ) _A = AutoConfig.for_model(snake_case_ , **snake_case_ ) _A = True @classmethod def lowerCAmelCase__ ( cls , snake_case_ , snake_case_ , **snake_case_ ): logger.info('Setting `config.is_decoder=True` and `config.add_cross_attention=True` for decoder_config' ) _A = True _A = True return cls(encoder=encoder_config.to_dict() , decoder=decoder_config.to_dict() , **snake_case_ ) def lowerCAmelCase__ ( self ): _A = copy.deepcopy(self.__dict__ ) _A = self.encoder.to_dict() _A = self.decoder.to_dict() _A = self.__class__.model_type return output class lowerCamelCase( __snake_case ): '''simple docstring''' __magic_name__ = version.parse('1.11' ) @property def lowerCAmelCase__ ( self ): return OrderedDict( [ ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}), ] ) @property def lowerCAmelCase__ ( self ): return 1E-4 @property def lowerCAmelCase__ ( self ): return OrderedDict({'last_hidden_state': {0: 'batch', 1: 'encoder_sequence'}} ) class lowerCamelCase( __snake_case ): '''simple docstring''' @property def lowerCAmelCase__ ( self ): _A = OrderedDict() _A = {0: 'batch', 1: 'past_decoder_sequence + sequence'} _A = {0: 'batch', 1: 'past_decoder_sequence + sequence'} _A = {0: 'batch', 1: 'encoder_sequence'} return common_inputs def lowerCAmelCase__ ( self , snake_case_ , snake_case_ = -1 , snake_case_ = -1 , snake_case_ = False , snake_case_ = None , ): import torch _A = OrderedDict() _A = super().generate_dummy_inputs( snake_case_ , batch_size=snake_case_ , seq_length=snake_case_ , is_pair=snake_case_ , framework=snake_case_ ) _A, _A = dummy_input['input_ids'].shape _A = (batch, encoder_sequence, self._config.encoder_hidden_size) _A = dummy_input.pop('input_ids' ) _A = dummy_input.pop('attention_mask' ) _A = torch.zeros(snake_case_ ) return common_inputs class lowerCamelCase( __snake_case ): '''simple docstring''' @property def lowerCAmelCase__ ( self ): pass def lowerCAmelCase__ ( self , snake_case_ ): return VisionEncoderDecoderEncoderOnnxConfig(snake_case_ ) def lowerCAmelCase__ ( self , snake_case_ , snake_case_ , snake_case_ = "default" ): _A = encoder_config.hidden_size return VisionEncoderDecoderDecoderOnnxConfig(snake_case_ , snake_case_ )
27
import logging from pathlib import Path import numpy as np import pytorch_lightning as pl import torch from pytorch_lightning.callbacks import EarlyStopping, ModelCheckpoint from pytorch_lightning.utilities import rank_zero_only from utils_rag import save_json def __lowerCAmelCase( _SCREAMING_SNAKE_CASE ) -> Optional[int]: """simple docstring""" _A = filter(lambda _SCREAMING_SNAKE_CASE : p.requires_grad , model.parameters() ) _A = sum([np.prod(p.size() ) for p in model_parameters] ) return params __A : Union[str, Any] = logging.getLogger(__name__) def __lowerCAmelCase( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> int: """simple docstring""" if metric == "rouge2": _A = '{val_avg_rouge2:.4f}-{step_count}' elif metric == "bleu": _A = '{val_avg_bleu:.4f}-{step_count}' elif metric == "em": _A = '{val_avg_em:.4f}-{step_count}' elif metric == "loss": _A = '{val_avg_loss:.4f}-{step_count}' else: raise NotImplementedError( F"seq2seq callbacks only support rouge2 and bleu, got {metric}, You can make your own by adding to this" ' function.' ) _A = ModelCheckpoint( dirpath=_SCREAMING_SNAKE_CASE , filename=_SCREAMING_SNAKE_CASE , monitor=F"val_{metric}" , mode='max' , save_top_k=1 , every_n_epochs=1 , ) return checkpoint_callback def __lowerCAmelCase( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Tuple: """simple docstring""" return EarlyStopping( monitor=F"val_{metric}" , mode='min' if 'loss' in metric else 'max' , patience=_SCREAMING_SNAKE_CASE , verbose=_SCREAMING_SNAKE_CASE , ) class lowerCamelCase( pl.Callback ): '''simple docstring''' def lowerCAmelCase__ ( self , snake_case_ , snake_case_ ): _A = {F"lr_group_{i}": param['lr'] for i, param in enumerate(pl_module.trainer.optimizers[0].param_groups )} pl_module.logger.log_metrics(snake_case_ ) @rank_zero_only def lowerCAmelCase__ ( self , snake_case_ , snake_case_ , snake_case_ , snake_case_=True ): logger.info(F"***** {type_path} results at step {trainer.global_step:05d} *****" ) _A = trainer.callback_metrics trainer.logger.log_metrics({k: v for k, v in metrics.items() if k not in ['log', 'progress_bar', 'preds']} ) # Log results _A = Path(pl_module.hparams.output_dir ) if type_path == "test": _A = od / 'test_results.txt' _A = od / 'test_generations.txt' else: # this never gets hit. I prefer not to save intermediate generations, and results are in metrics.json # If people want this it will be easy enough to add back. _A = od / F"{type_path}_results/{trainer.global_step:05d}.txt" _A = od / F"{type_path}_generations/{trainer.global_step:05d}.txt" results_file.parent.mkdir(exist_ok=snake_case_ ) generations_file.parent.mkdir(exist_ok=snake_case_ ) with open(snake_case_ , 'a+' ) as writer: for key in sorted(snake_case_ ): if key in ["log", "progress_bar", "preds"]: continue _A = metrics[key] if isinstance(snake_case_ , torch.Tensor ): _A = val.item() _A = F"{key}: {val:.6f}\n" writer.write(snake_case_ ) if not save_generations: return if "preds" in metrics: _A = '\n'.join(metrics['preds'] ) generations_file.open('w+' ).write(snake_case_ ) @rank_zero_only def lowerCAmelCase__ ( self , snake_case_ , snake_case_ ): try: _A = pl_module.model.model.num_parameters() except AttributeError: _A = pl_module.model.num_parameters() _A = count_trainable_parameters(snake_case_ ) # mp stands for million parameters trainer.logger.log_metrics({'n_params': npars, 'mp': npars / 1E6, 'grad_mp': n_trainable_pars / 1E6} ) @rank_zero_only def lowerCAmelCase__ ( self , snake_case_ , snake_case_ ): save_json(pl_module.metrics , pl_module.metrics_save_path ) return self._write_logs(snake_case_ , snake_case_ , 'test' ) @rank_zero_only def lowerCAmelCase__ ( self , snake_case_ , snake_case_ ): save_json(pl_module.metrics , pl_module.metrics_save_path ) # Uncommenting this will save val generations # return self._write_logs(trainer, pl_module, "valid")
27
1
def __lowerCAmelCase( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = 0 , _SCREAMING_SNAKE_CASE = 0 ) -> int: """simple docstring""" _A = right or len(_SCREAMING_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(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , left + 1 , right - 1 ) if __name__ == "__main__": import doctest doctest.testmod()
27
def __lowerCAmelCase( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> int: """simple docstring""" print('\nThe shortest path matrix using Floyd Warshall algorithm\n' ) for i in range(_SCREAMING_SNAKE_CASE ): for j in range(_SCREAMING_SNAKE_CASE ): if dist[i][j] != float('inf' ): print(int(dist[i][j] ) , end='\t' ) else: print('INF' , end='\t' ) print() def __lowerCAmelCase( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> str: """simple docstring""" _A = [[float('inf' ) for _ in range(_SCREAMING_SNAKE_CASE )] for _ in range(_SCREAMING_SNAKE_CASE )] for i in range(_SCREAMING_SNAKE_CASE ): for j in range(_SCREAMING_SNAKE_CASE ): _A = graph[i][j] # check vertex k against all other vertices (i, j) for k in range(_SCREAMING_SNAKE_CASE ): # looping through rows of graph array for i in range(_SCREAMING_SNAKE_CASE ): # looping through columns of graph array for j in range(_SCREAMING_SNAKE_CASE ): if ( dist[i][k] != float('inf' ) and dist[k][j] != float('inf' ) and dist[i][k] + dist[k][j] < dist[i][j] ): _A = dist[i][k] + dist[k][j] _print_dist(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) return dist, v if __name__ == "__main__": __A : Dict = int(input("Enter number of vertices: ")) __A : Union[str, Any] = int(input("Enter number of edges: ")) __A : List[str] = [[float("inf") for i in range(v)] for j in range(v)] for i in range(v): __A : List[Any] = 0.0 # src and dst are indices that must be within the array size graph[e][v] # failure to follow this will result in an error for i in range(e): print("\nEdge ", i + 1) __A : Union[str, Any] = int(input("Enter source:")) __A : List[str] = int(input("Enter destination:")) __A : Union[str, Any] = float(input("Enter weight:")) __A : Any = weight floyd_warshall(graph, v) # Example Input # Enter number of vertices: 3 # Enter number of edges: 2 # # generated graph from vertex and edge inputs # [[inf, inf, inf], [inf, inf, inf], [inf, inf, inf]] # [[0.0, inf, inf], [inf, 0.0, inf], [inf, inf, 0.0]] # specify source, destination and weight for edge #1 # Edge 1 # Enter source:1 # Enter destination:2 # Enter weight:2 # specify source, destination and weight for edge #2 # Edge 2 # Enter source:2 # Enter destination:1 # Enter weight:1 # # Expected Output from the vertice, edge and src, dst, weight inputs!! # 0 INF INF # INF 0 2 # INF 1 0
27
1
import itertools import os import random import tempfile import unittest import numpy as np from datasets import load_dataset from transformers import is_speech_available from transformers.testing_utils import check_json_file_has_correct_format, require_torch, require_torchaudio from transformers.utils.import_utils import is_torch_available from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin if is_speech_available(): from transformers import WhisperFeatureExtractor if is_torch_available(): import torch __A : Union[str, Any] = random.Random() def __lowerCAmelCase( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=1.0 , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE=None ) -> Optional[Any]: """simple docstring""" if rng is None: _A = global_rng _A = [] for batch_idx in range(shape[0] ): values.append([] ) for _ in range(shape[1] ): values[-1].append(rng.random() * scale ) return values @require_torch @require_torchaudio class lowerCamelCase( unittest.TestCase ): '''simple docstring''' def __init__( self , snake_case_ , snake_case_=7 , snake_case_=400 , snake_case_=2000 , snake_case_=10 , snake_case_=160 , snake_case_=8 , snake_case_=0.0 , snake_case_=4000 , snake_case_=False , snake_case_=True , ): _A = parent _A = batch_size _A = min_seq_length _A = max_seq_length _A = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) _A = padding_value _A = sampling_rate _A = return_attention_mask _A = do_normalize _A = feature_size _A = chunk_length _A = hop_length def lowerCAmelCase__ ( self ): return { "feature_size": self.feature_size, "hop_length": self.hop_length, "chunk_length": self.chunk_length, "padding_value": self.padding_value, "sampling_rate": self.sampling_rate, "return_attention_mask": self.return_attention_mask, "do_normalize": self.do_normalize, } def lowerCAmelCase__ ( self , snake_case_=False , snake_case_=False ): def _flatten(snake_case_ ): return list(itertools.chain(*snake_case_ ) ) if equal_length: _A = [floats_list((self.max_seq_length, self.feature_size) ) for _ in range(self.batch_size )] else: # make sure that inputs increase in size _A = [ floats_list((x, self.feature_size) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: _A = [np.asarray(snake_case_ ) for x in speech_inputs] return speech_inputs @require_torch @require_torchaudio class lowerCamelCase( __snake_case , unittest.TestCase ): '''simple docstring''' __magic_name__ = WhisperFeatureExtractor if is_speech_available() else None def lowerCAmelCase__ ( self ): _A = WhisperFeatureExtractionTester(self ) def lowerCAmelCase__ ( self ): _A = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: _A = feat_extract_first.save_pretrained(snake_case_ )[0] check_json_file_has_correct_format(snake_case_ ) _A = self.feature_extraction_class.from_pretrained(snake_case_ ) _A = feat_extract_first.to_dict() _A = feat_extract_second.to_dict() _A = feat_extract_first.mel_filters _A = feat_extract_second.mel_filters self.assertTrue(np.allclose(snake_case_ , snake_case_ ) ) self.assertEqual(snake_case_ , snake_case_ ) def lowerCAmelCase__ ( self ): _A = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: _A = os.path.join(snake_case_ , 'feat_extract.json' ) feat_extract_first.to_json_file(snake_case_ ) _A = self.feature_extraction_class.from_json_file(snake_case_ ) _A = feat_extract_first.to_dict() _A = feat_extract_second.to_dict() _A = feat_extract_first.mel_filters _A = feat_extract_second.mel_filters self.assertTrue(np.allclose(snake_case_ , snake_case_ ) ) self.assertEqual(snake_case_ , snake_case_ ) def lowerCAmelCase__ ( self ): # Tests that all call wrap to encode_plus and batch_encode_plus _A = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 _A = [floats_list((1, x) )[0] for x in range(800 , 1400 , 200 )] _A = [np.asarray(snake_case_ ) for speech_input in speech_inputs] # Test feature size _A = feature_extractor(snake_case_ , padding='max_length' , return_tensors='np' ).input_features self.assertTrue(input_features.ndim == 3 ) self.assertTrue(input_features.shape[-1] == feature_extractor.nb_max_frames ) self.assertTrue(input_features.shape[-2] == feature_extractor.feature_size ) # Test not batched input _A = feature_extractor(speech_inputs[0] , return_tensors='np' ).input_features _A = feature_extractor(np_speech_inputs[0] , return_tensors='np' ).input_features self.assertTrue(np.allclose(snake_case_ , snake_case_ , atol=1E-3 ) ) # Test batched _A = feature_extractor(snake_case_ , return_tensors='np' ).input_features _A = feature_extractor(snake_case_ , return_tensors='np' ).input_features for enc_seq_a, enc_seq_a in zip(snake_case_ , snake_case_ ): self.assertTrue(np.allclose(snake_case_ , snake_case_ , atol=1E-3 ) ) # Test 2-D numpy arrays are batched. _A = [floats_list((1, x) )[0] for x in (800, 800, 800)] _A = np.asarray(snake_case_ ) _A = feature_extractor(snake_case_ , return_tensors='np' ).input_features _A = feature_extractor(snake_case_ , return_tensors='np' ).input_features for enc_seq_a, enc_seq_a in zip(snake_case_ , snake_case_ ): self.assertTrue(np.allclose(snake_case_ , snake_case_ , atol=1E-3 ) ) # Test truncation required _A = [floats_list((1, x) )[0] for x in range(200 , (feature_extractor.n_samples + 500) , 200 )] _A = [np.asarray(snake_case_ ) for speech_input in speech_inputs] _A = [x[: feature_extractor.n_samples] for x in speech_inputs] _A = [np.asarray(snake_case_ ) for speech_input in speech_inputs_truncated] _A = feature_extractor(snake_case_ , return_tensors='np' ).input_features _A = feature_extractor(snake_case_ , return_tensors='np' ).input_features for enc_seq_a, enc_seq_a in zip(snake_case_ , snake_case_ ): self.assertTrue(np.allclose(snake_case_ , snake_case_ , atol=1E-3 ) ) def lowerCAmelCase__ ( self ): import torch _A = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) _A = np.random.rand(100 , 32 ).astype(np.floataa ) _A = np_speech_inputs.tolist() for inputs in [py_speech_inputs, np_speech_inputs]: _A = feature_extractor.pad([{'input_features': inputs}] , return_tensors='np' ) self.assertTrue(np_processed.input_features.dtype == np.floataa ) _A = feature_extractor.pad([{'input_features': inputs}] , return_tensors='pt' ) self.assertTrue(pt_processed.input_features.dtype == torch.floataa ) def lowerCAmelCase__ ( self , snake_case_ ): _A = load_dataset('hf-internal-testing/librispeech_asr_dummy' , 'clean' , split='validation' ) # automatic decoding with librispeech _A = ds.sort('id' ).select(range(snake_case_ ) )[:num_samples]['audio'] return [x["array"] for x in speech_samples] def lowerCAmelCase__ ( self ): # fmt: off _A = torch.tensor( [ 0.1193, -0.0946, -0.1098, -0.0196, 0.0225, -0.0690, -0.1736, 0.0951, 0.0971, -0.0817, -0.0702, 0.0162, 0.0260, 0.0017, -0.0192, -0.1678, 0.0709, -0.1867, -0.0655, -0.0274, -0.0234, -0.1884, -0.0516, -0.0554, -0.0274, -0.1425, -0.1423, 0.0837, 0.0377, -0.0854 ] ) # fmt: on _A = self._load_datasamples(1 ) _A = WhisperFeatureExtractor() _A = feature_extractor(snake_case_ , return_tensors='pt' ).input_features self.assertEqual(input_features.shape , (1, 80, 3000) ) self.assertTrue(torch.allclose(input_features[0, 0, :30] , snake_case_ , atol=1E-4 ) ) def lowerCAmelCase__ ( self ): _A = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) _A = self._load_datasamples(1 )[0] _A = ((audio - audio.min()) / (audio.max() - audio.min())) * 6_5535 # Rescale to [0, 65535] to show issue _A = feat_extract.zero_mean_unit_var_norm([audio] , attention_mask=snake_case_ )[0] self.assertTrue(np.all(np.mean(snake_case_ ) < 1E-3 ) ) self.assertTrue(np.all(np.abs(np.var(snake_case_ ) - 1 ) < 1E-3 ) )
27
# Copyright 2022 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import argparse import os import subprocess from packaging.version import Version, parse from accelerate.commands.config.config_args import default_config_file, load_config_from_file __A : Optional[int] = "Run commands across TPU VMs for initial setup before running `accelerate launch`." def __lowerCAmelCase( _SCREAMING_SNAKE_CASE=None ) -> str: """simple docstring""" if subparsers is not None: _A = subparsers.add_parser('tpu-config' , description=_description ) else: _A = argparse.ArgumentParser('Accelerate tpu-config command' , description=_description ) # Core arguments _A = parser.add_argument_group( 'Config Arguments' , 'Arguments that can be configured through `accelerate config`.' ) config_args.add_argument( '--config_file' , type=_SCREAMING_SNAKE_CASE , default=_SCREAMING_SNAKE_CASE , help='Path to the config file to use for accelerate.' , ) config_args.add_argument( '--tpu_name' , default=_SCREAMING_SNAKE_CASE , help='The name of the TPU to use. If not specified, will use the TPU specified in the config file.' , ) config_args.add_argument( '--tpu_zone' , default=_SCREAMING_SNAKE_CASE , help='The zone of the TPU to use. If not specified, will use the zone specified in the config file.' , ) _A = parser.add_argument_group('TPU Arguments' , 'Arguments for options ran inside the TPU.' ) pod_args.add_argument( '--use_alpha' , action='store_true' , help='Whether to use `gcloud alpha` when running the TPU training script instead of `gcloud`.' , ) pod_args.add_argument( '--command_file' , default=_SCREAMING_SNAKE_CASE , help='The path to the file containing the commands to run on the pod on startup.' , ) pod_args.add_argument( '--command' , action='append' , nargs='+' , help='A command to run on the pod. Can be passed multiple times.' , ) pod_args.add_argument( '--install_accelerate' , action='store_true' , help='Whether to install accelerate on the pod. Defaults to False.' , ) pod_args.add_argument( '--accelerate_version' , default='latest' , help='The version of accelerate to install on the pod. If not specified, will use the latest pypi version. Specify \'dev\' to install from GitHub.' , ) pod_args.add_argument( '--debug' , action='store_true' , help='If set, will print the command that would be run instead of running it.' ) if subparsers is not None: parser.set_defaults(func=_SCREAMING_SNAKE_CASE ) return parser def __lowerCAmelCase( _SCREAMING_SNAKE_CASE ) -> int: """simple docstring""" _A = None # Get the default from the config file if it exists. if args.config_file is not None or os.path.isfile(_SCREAMING_SNAKE_CASE ): _A = load_config_from_file(args.config_file ) if not args.command_file and defaults.command_file is not None and not args.command: _A = defaults.command_file if not args.command and defaults.commands is not None: _A = defaults.commands if not args.tpu_name: _A = defaults.tpu_name if not args.tpu_zone: _A = defaults.tpu_zone if args.accelerate_version == "dev": _A = 'git+https://github.com/huggingface/accelerate.git' elif args.accelerate_version == "latest": _A = 'accelerate -U' elif isinstance(parse(args.accelerate_version ) , _SCREAMING_SNAKE_CASE ): _A = F"accelerate=={args.accelerate_version}" if not args.command_file and not args.command: raise ValueError('You must specify either a command file or a command to run on the pod.' ) if args.command_file: with open(args.command_file , 'r' ) as f: _A = [f.read().splitlines()] # To turn list of lists into list of strings if isinstance(args.command[0] , _SCREAMING_SNAKE_CASE ): _A = [line for cmd in args.command for line in cmd] # Default to the shared folder and install accelerate _A = ['cd /usr/share'] if args.install_accelerate: new_cmd += [F"pip install {args.accelerate_version}"] new_cmd += args.command _A = '; '.join(_SCREAMING_SNAKE_CASE ) # Then send it to gcloud # Eventually try to use google-api-core to do this instead of subprocess _A = ['gcloud'] if args.use_alpha: cmd += ["alpha"] cmd += [ "compute", "tpus", "tpu-vm", "ssh", args.tpu_name, "--zone", args.tpu_zone, "--command", args.command, "--worker", "all", ] if args.debug: print(F"Running {' '.join(_SCREAMING_SNAKE_CASE )}" ) return subprocess.run(_SCREAMING_SNAKE_CASE ) print('Successfully setup pod.' ) def __lowerCAmelCase( ) -> Tuple: """simple docstring""" _A = tpu_command_parser() _A = parser.parse_args() tpu_command_launcher(_SCREAMING_SNAKE_CASE )
27
1
import os def __lowerCAmelCase( ) -> Union[str, Any]: """simple docstring""" with open(os.path.dirname(_SCREAMING_SNAKE_CASE ) + '/grid.txt' ) as f: _A = [] # noqa: E741 for _ in range(20 ): l.append([int(_SCREAMING_SNAKE_CASE ) for x in f.readline().split()] ) _A = 0 # right for i in range(20 ): for j in range(17 ): _A = l[i][j] * l[i][j + 1] * l[i][j + 2] * l[i][j + 3] if temp > maximum: _A = temp # down for i in range(17 ): for j in range(20 ): _A = l[i][j] * l[i + 1][j] * l[i + 2][j] * l[i + 3][j] if temp > maximum: _A = temp # diagonal 1 for i in range(17 ): for j in range(17 ): _A = l[i][j] * l[i + 1][j + 1] * l[i + 2][j + 2] * l[i + 3][j + 3] if temp > maximum: _A = temp # diagonal 2 for i in range(17 ): for j in range(3 , 20 ): _A = l[i][j] * l[i + 1][j - 1] * l[i + 2][j - 2] * l[i + 3][j - 3] if temp > maximum: _A = temp return maximum if __name__ == "__main__": print(solution())
27
from ... import PretrainedConfig __A : Optional[Any] = { "sijunhe/nezha-cn-base": "https://huggingface.co/sijunhe/nezha-cn-base/resolve/main/config.json", } class lowerCamelCase( __snake_case ): '''simple docstring''' __magic_name__ = NEZHA_PRETRAINED_CONFIG_ARCHIVE_MAP __magic_name__ = 'nezha' def __init__( self , snake_case_=2_1128 , snake_case_=768 , snake_case_=12 , snake_case_=12 , snake_case_=3072 , snake_case_="gelu" , snake_case_=0.1 , snake_case_=0.1 , snake_case_=512 , snake_case_=64 , snake_case_=2 , snake_case_=0.02 , snake_case_=1E-12 , snake_case_=0.1 , snake_case_=0 , snake_case_=2 , snake_case_=3 , snake_case_=True , **snake_case_ , ): super().__init__(pad_token_id=snake_case_ , bos_token_id=snake_case_ , eos_token_id=snake_case_ , **snake_case_ ) _A = vocab_size _A = hidden_size _A = num_hidden_layers _A = num_attention_heads _A = hidden_act _A = intermediate_size _A = hidden_dropout_prob _A = attention_probs_dropout_prob _A = max_position_embeddings _A = max_relative_position _A = type_vocab_size _A = initializer_range _A = layer_norm_eps _A = classifier_dropout _A = use_cache
27
1
import sys __A : Union[str, Any] = ( "73167176531330624919225119674426574742355349194934" "96983520312774506326239578318016984801869478851843" "85861560789112949495459501737958331952853208805511" "12540698747158523863050715693290963295227443043557" "66896648950445244523161731856403098711121722383113" "62229893423380308135336276614282806444486645238749" "30358907296290491560440772390713810515859307960866" "70172427121883998797908792274921901699720888093776" "65727333001053367881220235421809751254540594752243" "52584907711670556013604839586446706324415722155397" "53697817977846174064955149290862569321978468622482" "83972241375657056057490261407972968652414535100474" "82166370484403199890008895243450658541227588666881" "16427171479924442928230863465674813919123162824586" "17866458359124566529476545682848912883142607690042" "24219022671055626321111109370544217506941658960408" "07198403850962455444362981230987879927244284909188" "84580156166097919133875499200524063689912560717606" "05886116467109405077541002256983155200055935729725" "71636269561882670428252483600823257530420752963450" ) def __lowerCAmelCase( _SCREAMING_SNAKE_CASE ) -> int: """simple docstring""" _A = 1 for digit in s: product *= int(_SCREAMING_SNAKE_CASE ) return product def __lowerCAmelCase( _SCREAMING_SNAKE_CASE = N ) -> int: """simple docstring""" _A = -sys.maxsize - 1 _A = n[:13] _A = 13 while cur_index < len(_SCREAMING_SNAKE_CASE ) - 13: if int(n[cur_index] ) >= int(substr[0] ): _A = substr[1:] + n[cur_index] cur_index += 1 else: _A = max(_SCREAMING_SNAKE_CASE , str_eval(_SCREAMING_SNAKE_CASE ) ) _A = n[cur_index : cur_index + 13] cur_index += 13 return largest_product if __name__ == "__main__": print(f"{solution() = }")
27
from collections import defaultdict from math import ceil, sqrt def __lowerCAmelCase( _SCREAMING_SNAKE_CASE = 1_000_000 , _SCREAMING_SNAKE_CASE = 10 ) -> int: """simple docstring""" _A = defaultdict(_SCREAMING_SNAKE_CASE ) for outer_width in range(3 , (t_limit // 4) + 2 ): if outer_width * outer_width > t_limit: _A = max( ceil(sqrt(outer_width * outer_width - t_limit ) ) , 1 ) else: _A = 1 hole_width_lower_bound += (outer_width - hole_width_lower_bound) % 2 for hole_width in range(_SCREAMING_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 <= 10 ) if __name__ == "__main__": print(f"{solution() = }")
27
1
import datasets from .evaluate import evaluate __A : int = "\\n@inproceedings{Rajpurkar2016SQuAD10,\n title={SQuAD: 100, 000+ Questions for Machine Comprehension of Text},\n author={Pranav Rajpurkar and Jian Zhang and Konstantin Lopyrev and Percy Liang},\n booktitle={EMNLP},\n year={2016}\n}\n" __A : int = "\nThis metric wrap the official scoring script for version 1 of the Stanford Question Answering Dataset (SQuAD).\n\nStanford Question Answering Dataset (SQuAD) is a reading comprehension dataset, consisting of questions posed by\ncrowdworkers on a set of Wikipedia articles, where the answer to every question is a segment of text, or span,\nfrom the corresponding reading passage, or the question might be unanswerable.\n" __A : int = "\nComputes SQuAD scores (F1 and EM).\nArgs:\n predictions: List of question-answers dictionaries with the following key-values:\n - 'id': id of the question-answer pair as given in the references (see below)\n - 'prediction_text': the text of the answer\n references: List of question-answers dictionaries with the following key-values:\n - 'id': id of the question-answer pair (see above),\n - 'answers': a Dict in the SQuAD dataset format\n {\n 'text': list of possible texts for the answer, as a list of strings\n 'answer_start': list of start positions for the answer, as a list of ints\n }\n Note that answer_start values are not taken into account to compute the metric.\nReturns:\n 'exact_match': Exact match (the normalized answer exactly match the gold answer)\n 'f1': The F-score of predicted tokens versus the gold answer\nExamples:\n\n >>> predictions = [{'prediction_text': '1976', 'id': '56e10a3be3433e1400422b22'}]\n >>> references = [{'answers': {'answer_start': [97], 'text': ['1976']}, 'id': '56e10a3be3433e1400422b22'}]\n >>> squad_metric = datasets.load_metric(\"squad\")\n >>> results = squad_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {'exact_match': 100.0, 'f1': 100.0}\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class lowerCamelCase( datasets.Metric ): '''simple docstring''' def lowerCAmelCase__ ( self ): return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { 'predictions': {'id': datasets.Value('string' ), 'prediction_text': datasets.Value('string' )}, 'references': { 'id': datasets.Value('string' ), 'answers': datasets.features.Sequence( { 'text': datasets.Value('string' ), 'answer_start': datasets.Value('int32' ), } ), }, } ) , codebase_urls=['https://rajpurkar.github.io/SQuAD-explorer/'] , reference_urls=['https://rajpurkar.github.io/SQuAD-explorer/'] , ) def lowerCAmelCase__ ( self , snake_case_ , snake_case_ ): _A = {prediction['id']: prediction['prediction_text'] for prediction in predictions} _A = [ { 'paragraphs': [ { 'qas': [ { 'answers': [{'text': answer_text} for answer_text in ref['answers']['text']], 'id': ref['id'], } for ref in references ] } ] } ] _A = evaluate(dataset=snake_case_ , predictions=snake_case_ ) return score
27
from math import pi, sqrt, tan def __lowerCAmelCase( _SCREAMING_SNAKE_CASE ) -> float: """simple docstring""" if side_length < 0: raise ValueError('surface_area_cube() only accepts non-negative values' ) return 6 * side_length**2 def __lowerCAmelCase( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> float: """simple docstring""" if length < 0 or breadth < 0 or height < 0: raise ValueError('surface_area_cuboid() only accepts non-negative values' ) return 2 * ((length * breadth) + (breadth * height) + (length * height)) def __lowerCAmelCase( _SCREAMING_SNAKE_CASE ) -> float: """simple docstring""" if radius < 0: raise ValueError('surface_area_sphere() only accepts non-negative values' ) return 4 * pi * radius**2 def __lowerCAmelCase( _SCREAMING_SNAKE_CASE ) -> float: """simple docstring""" if radius < 0: raise ValueError('surface_area_hemisphere() only accepts non-negative values' ) return 3 * pi * radius**2 def __lowerCAmelCase( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> float: """simple docstring""" if radius < 0 or height < 0: raise ValueError('surface_area_cone() only accepts non-negative values' ) return pi * radius * (radius + (height**2 + radius**2) ** 0.5) def __lowerCAmelCase( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> float: """simple docstring""" if radius_a < 0 or radius_a < 0 or height < 0: raise ValueError( 'surface_area_conical_frustum() only accepts non-negative values' ) _A = (height**2 + (radius_a - radius_a) ** 2) ** 0.5 return pi * ((slant_height * (radius_a + radius_a)) + radius_a**2 + radius_a**2) def __lowerCAmelCase( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> float: """simple docstring""" if radius < 0 or height < 0: raise ValueError('surface_area_cylinder() only accepts non-negative values' ) return 2 * pi * radius * (height + radius) def __lowerCAmelCase( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> float: """simple docstring""" if torus_radius < 0 or tube_radius < 0: raise ValueError('surface_area_torus() only accepts non-negative values' ) if torus_radius < tube_radius: raise ValueError( 'surface_area_torus() does not support spindle or self intersecting tori' ) return 4 * pow(_SCREAMING_SNAKE_CASE , 2 ) * torus_radius * tube_radius def __lowerCAmelCase( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> float: """simple docstring""" if length < 0 or width < 0: raise ValueError('area_rectangle() only accepts non-negative values' ) return length * width def __lowerCAmelCase( _SCREAMING_SNAKE_CASE ) -> float: """simple docstring""" if side_length < 0: raise ValueError('area_square() only accepts non-negative values' ) return side_length**2 def __lowerCAmelCase( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> float: """simple docstring""" if base < 0 or height < 0: raise ValueError('area_triangle() only accepts non-negative values' ) return (base * height) / 2 def __lowerCAmelCase( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> float: """simple docstring""" if sidea < 0 or sidea < 0 or sidea < 0: raise ValueError('area_triangle_three_sides() only accepts non-negative values' ) elif sidea + sidea < sidea or sidea + sidea < sidea or sidea + sidea < sidea: raise ValueError('Given three sides do not form a triangle' ) _A = (sidea + sidea + sidea) / 2 _A = sqrt( semi_perimeter * (semi_perimeter - sidea) * (semi_perimeter - sidea) * (semi_perimeter - sidea) ) return area def __lowerCAmelCase( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> float: """simple docstring""" if base < 0 or height < 0: raise ValueError('area_parallelogram() only accepts non-negative values' ) return base * height def __lowerCAmelCase( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> float: """simple docstring""" if basea < 0 or basea < 0 or height < 0: raise ValueError('area_trapezium() only accepts non-negative values' ) return 1 / 2 * (basea + basea) * height def __lowerCAmelCase( _SCREAMING_SNAKE_CASE ) -> float: """simple docstring""" if radius < 0: raise ValueError('area_circle() only accepts non-negative values' ) return pi * radius**2 def __lowerCAmelCase( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> float: """simple docstring""" if radius_x < 0 or radius_y < 0: raise ValueError('area_ellipse() only accepts non-negative values' ) return pi * radius_x * radius_y def __lowerCAmelCase( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> float: """simple docstring""" if diagonal_a < 0 or diagonal_a < 0: raise ValueError('area_rhombus() only accepts non-negative values' ) return 1 / 2 * diagonal_a * diagonal_a def __lowerCAmelCase( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> float: """simple docstring""" if not isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) or sides < 3: raise ValueError( 'area_reg_polygon() only accepts integers greater than or \ equal to three as number of sides' ) elif length < 0: raise ValueError( 'area_reg_polygon() only accepts non-negative values as \ length of a side' ) return (sides * length**2) / (4 * tan(pi / sides )) return (sides * length**2) / (4 * tan(pi / sides )) if __name__ == "__main__": import doctest doctest.testmod(verbose=True) # verbose so we can see methods missing tests print("[DEMO] Areas of various geometric shapes: \n") print(f"Rectangle: {area_rectangle(10, 20) = }") print(f"Square: {area_square(10) = }") print(f"Triangle: {area_triangle(10, 10) = }") print(f"Triangle: {area_triangle_three_sides(5, 12, 13) = }") print(f"Parallelogram: {area_parallelogram(10, 20) = }") print(f"Rhombus: {area_rhombus(10, 20) = }") print(f"Trapezium: {area_trapezium(10, 20, 30) = }") print(f"Circle: {area_circle(20) = }") print(f"Ellipse: {area_ellipse(10, 20) = }") print("\nSurface Areas of various geometric shapes: \n") print(f"Cube: {surface_area_cube(20) = }") print(f"Cuboid: {surface_area_cuboid(10, 20, 30) = }") print(f"Sphere: {surface_area_sphere(20) = }") print(f"Hemisphere: {surface_area_hemisphere(20) = }") print(f"Cone: {surface_area_cone(10, 20) = }") print(f"Conical Frustum: {surface_area_conical_frustum(10, 20, 30) = }") print(f"Cylinder: {surface_area_cylinder(10, 20) = }") print(f"Torus: {surface_area_torus(20, 10) = }") print(f"Equilateral Triangle: {area_reg_polygon(3, 10) = }") print(f"Square: {area_reg_polygon(4, 10) = }") print(f"Reqular Pentagon: {area_reg_polygon(5, 10) = }")
27
1
import qiskit def __lowerCAmelCase( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> qiskit.result.counts.Counts: """simple docstring""" _A = qiskit.Aer.get_backend('aer_simulator' ) # Create a Quantum Circuit acting on the q register _A = qiskit.QuantumCircuit(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # Apply X (NOT) Gate to Qubits 0 & 1 circuit.x(0 ) circuit.x(1 ) # Map the quantum measurement to the classical bits circuit.measure([0, 1] , [0, 1] ) # Execute the circuit on the qasm simulator _A = qiskit.execute(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , shots=1_000 ) # Return the histogram data of the results of the experiment. return job.result().get_counts(_SCREAMING_SNAKE_CASE ) if __name__ == "__main__": __A : Optional[int] = single_qubit_measure(2, 2) print(f"Total count for various states are: {counts}")
27
import numpy as np def __lowerCAmelCase( _SCREAMING_SNAKE_CASE ) -> np.array: """simple docstring""" return (2 / (1 + np.exp(-2 * vector ))) - 1 if __name__ == "__main__": import doctest doctest.testmod()
27
1
# Copyright 2022 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import argparse import os import subprocess from packaging.version import Version, parse from accelerate.commands.config.config_args import default_config_file, load_config_from_file __A : Optional[int] = "Run commands across TPU VMs for initial setup before running `accelerate launch`." def __lowerCAmelCase( _SCREAMING_SNAKE_CASE=None ) -> str: """simple docstring""" if subparsers is not None: _A = subparsers.add_parser('tpu-config' , description=_description ) else: _A = argparse.ArgumentParser('Accelerate tpu-config command' , description=_description ) # Core arguments _A = parser.add_argument_group( 'Config Arguments' , 'Arguments that can be configured through `accelerate config`.' ) config_args.add_argument( '--config_file' , type=_SCREAMING_SNAKE_CASE , default=_SCREAMING_SNAKE_CASE , help='Path to the config file to use for accelerate.' , ) config_args.add_argument( '--tpu_name' , default=_SCREAMING_SNAKE_CASE , help='The name of the TPU to use. If not specified, will use the TPU specified in the config file.' , ) config_args.add_argument( '--tpu_zone' , default=_SCREAMING_SNAKE_CASE , help='The zone of the TPU to use. If not specified, will use the zone specified in the config file.' , ) _A = parser.add_argument_group('TPU Arguments' , 'Arguments for options ran inside the TPU.' ) pod_args.add_argument( '--use_alpha' , action='store_true' , help='Whether to use `gcloud alpha` when running the TPU training script instead of `gcloud`.' , ) pod_args.add_argument( '--command_file' , default=_SCREAMING_SNAKE_CASE , help='The path to the file containing the commands to run on the pod on startup.' , ) pod_args.add_argument( '--command' , action='append' , nargs='+' , help='A command to run on the pod. Can be passed multiple times.' , ) pod_args.add_argument( '--install_accelerate' , action='store_true' , help='Whether to install accelerate on the pod. Defaults to False.' , ) pod_args.add_argument( '--accelerate_version' , default='latest' , help='The version of accelerate to install on the pod. If not specified, will use the latest pypi version. Specify \'dev\' to install from GitHub.' , ) pod_args.add_argument( '--debug' , action='store_true' , help='If set, will print the command that would be run instead of running it.' ) if subparsers is not None: parser.set_defaults(func=_SCREAMING_SNAKE_CASE ) return parser def __lowerCAmelCase( _SCREAMING_SNAKE_CASE ) -> int: """simple docstring""" _A = None # Get the default from the config file if it exists. if args.config_file is not None or os.path.isfile(_SCREAMING_SNAKE_CASE ): _A = load_config_from_file(args.config_file ) if not args.command_file and defaults.command_file is not None and not args.command: _A = defaults.command_file if not args.command and defaults.commands is not None: _A = defaults.commands if not args.tpu_name: _A = defaults.tpu_name if not args.tpu_zone: _A = defaults.tpu_zone if args.accelerate_version == "dev": _A = 'git+https://github.com/huggingface/accelerate.git' elif args.accelerate_version == "latest": _A = 'accelerate -U' elif isinstance(parse(args.accelerate_version ) , _SCREAMING_SNAKE_CASE ): _A = F"accelerate=={args.accelerate_version}" if not args.command_file and not args.command: raise ValueError('You must specify either a command file or a command to run on the pod.' ) if args.command_file: with open(args.command_file , 'r' ) as f: _A = [f.read().splitlines()] # To turn list of lists into list of strings if isinstance(args.command[0] , _SCREAMING_SNAKE_CASE ): _A = [line for cmd in args.command for line in cmd] # Default to the shared folder and install accelerate _A = ['cd /usr/share'] if args.install_accelerate: new_cmd += [F"pip install {args.accelerate_version}"] new_cmd += args.command _A = '; '.join(_SCREAMING_SNAKE_CASE ) # Then send it to gcloud # Eventually try to use google-api-core to do this instead of subprocess _A = ['gcloud'] if args.use_alpha: cmd += ["alpha"] cmd += [ "compute", "tpus", "tpu-vm", "ssh", args.tpu_name, "--zone", args.tpu_zone, "--command", args.command, "--worker", "all", ] if args.debug: print(F"Running {' '.join(_SCREAMING_SNAKE_CASE )}" ) return subprocess.run(_SCREAMING_SNAKE_CASE ) print('Successfully setup pod.' ) def __lowerCAmelCase( ) -> Tuple: """simple docstring""" _A = tpu_command_parser() _A = parser.parse_args() tpu_command_launcher(_SCREAMING_SNAKE_CASE )
27
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available __A : Optional[Any] = {} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Optional[int] = ["MLukeTokenizer"] if TYPE_CHECKING: try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_mluke import MLukeTokenizer else: import sys __A : List[str] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
27
1
import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, XLMRobertaTokenizer from diffusers import AltDiffusionPipeline, AutoencoderKL, DDIMScheduler, PNDMScheduler, UNetaDConditionModel from diffusers.pipelines.alt_diffusion.modeling_roberta_series import ( RobertaSeriesConfig, RobertaSeriesModelWithTransformation, ) from diffusers.utils import slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class lowerCamelCase( __snake_case , __snake_case , __snake_case , unittest.TestCase ): '''simple docstring''' __magic_name__ = AltDiffusionPipeline __magic_name__ = TEXT_TO_IMAGE_PARAMS __magic_name__ = TEXT_TO_IMAGE_BATCH_PARAMS __magic_name__ = TEXT_TO_IMAGE_IMAGE_PARAMS __magic_name__ = TEXT_TO_IMAGE_IMAGE_PARAMS def lowerCAmelCase__ ( self ): torch.manual_seed(0 ) _A = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D') , up_block_types=('CrossAttnUpBlock2D', 'UpBlock2D') , cross_attention_dim=32 , ) _A = DDIMScheduler( beta_start=0.0_0085 , beta_end=0.012 , beta_schedule='scaled_linear' , clip_sample=snake_case_ , set_alpha_to_one=snake_case_ , ) torch.manual_seed(0 ) _A = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D'] , up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D'] , latent_channels=4 , ) # TODO: address the non-deterministic text encoder (fails for save-load tests) # torch.manual_seed(0) # text_encoder_config = RobertaSeriesConfig( # hidden_size=32, # project_dim=32, # intermediate_size=37, # layer_norm_eps=1e-05, # num_attention_heads=4, # num_hidden_layers=5, # vocab_size=5002, # ) # text_encoder = RobertaSeriesModelWithTransformation(text_encoder_config) torch.manual_seed(0 ) _A = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , projection_dim=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=5002 , ) _A = CLIPTextModel(snake_case_ ) _A = XLMRobertaTokenizer.from_pretrained('hf-internal-testing/tiny-xlm-roberta' ) _A = 77 _A = { 'unet': unet, 'scheduler': scheduler, 'vae': vae, 'text_encoder': text_encoder, 'tokenizer': tokenizer, 'safety_checker': None, 'feature_extractor': None, } return components def lowerCAmelCase__ ( self , snake_case_ , snake_case_=0 ): if str(snake_case_ ).startswith('mps' ): _A = torch.manual_seed(snake_case_ ) else: _A = torch.Generator(device=snake_case_ ).manual_seed(snake_case_ ) _A = { 'prompt': 'A painting of a squirrel eating a burger', 'generator': generator, 'num_inference_steps': 2, 'guidance_scale': 6.0, 'output_type': 'numpy', } return inputs def lowerCAmelCase__ ( self ): super().test_attention_slicing_forward_pass(expected_max_diff=3E-3 ) def lowerCAmelCase__ ( self ): super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) def lowerCAmelCase__ ( self ): _A = 'cpu' # ensure determinism for the device-dependent torch.Generator _A = self.get_dummy_components() torch.manual_seed(0 ) _A = RobertaSeriesConfig( hidden_size=32 , project_dim=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , vocab_size=5002 , ) # TODO: remove after fixing the non-deterministic text encoder _A = RobertaSeriesModelWithTransformation(snake_case_ ) _A = text_encoder _A = AltDiffusionPipeline(**snake_case_ ) _A = alt_pipe.to(snake_case_ ) alt_pipe.set_progress_bar_config(disable=snake_case_ ) _A = self.get_dummy_inputs(snake_case_ ) _A = 'A photo of an astronaut' _A = alt_pipe(**snake_case_ ) _A = output.images _A = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) _A = np.array( [0.574_8162, 0.6044_7145, 0.4882_1217, 0.5010_0636, 0.543_1185, 0.4576_3683, 0.4965_7696, 0.4813_2733, 0.4757_3093] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def lowerCAmelCase__ ( self ): _A = 'cpu' # ensure determinism for the device-dependent torch.Generator _A = self.get_dummy_components() _A = PNDMScheduler(skip_prk_steps=snake_case_ ) torch.manual_seed(0 ) _A = RobertaSeriesConfig( hidden_size=32 , project_dim=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , vocab_size=5002 , ) # TODO: remove after fixing the non-deterministic text encoder _A = RobertaSeriesModelWithTransformation(snake_case_ ) _A = text_encoder _A = AltDiffusionPipeline(**snake_case_ ) _A = alt_pipe.to(snake_case_ ) alt_pipe.set_progress_bar_config(disable=snake_case_ ) _A = self.get_dummy_inputs(snake_case_ ) _A = alt_pipe(**snake_case_ ) _A = output.images _A = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) _A = np.array( [0.5160_5093, 0.570_7241, 0.4736_5507, 0.5057_8886, 0.563_3877, 0.464_2503, 0.518_2081, 0.4876_3484, 0.4908_4237] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 @slow @require_torch_gpu class lowerCamelCase( unittest.TestCase ): '''simple docstring''' def lowerCAmelCase__ ( self ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def lowerCAmelCase__ ( self ): # make sure here that pndm scheduler skips prk _A = AltDiffusionPipeline.from_pretrained('BAAI/AltDiffusion' , safety_checker=snake_case_ ) _A = alt_pipe.to(snake_case_ ) alt_pipe.set_progress_bar_config(disable=snake_case_ ) _A = 'A painting of a squirrel eating a burger' _A = torch.manual_seed(0 ) _A = alt_pipe([prompt] , generator=snake_case_ , guidance_scale=6.0 , num_inference_steps=20 , output_type='np' ) _A = output.images _A = image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) _A = np.array([0.1010, 0.0800, 0.0794, 0.0885, 0.0843, 0.0762, 0.0769, 0.0729, 0.0586] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def lowerCAmelCase__ ( self ): _A = DDIMScheduler.from_pretrained('BAAI/AltDiffusion' , subfolder='scheduler' ) _A = AltDiffusionPipeline.from_pretrained('BAAI/AltDiffusion' , scheduler=snake_case_ , safety_checker=snake_case_ ) _A = alt_pipe.to(snake_case_ ) alt_pipe.set_progress_bar_config(disable=snake_case_ ) _A = 'A painting of a squirrel eating a burger' _A = torch.manual_seed(0 ) _A = alt_pipe([prompt] , generator=snake_case_ , num_inference_steps=2 , output_type='numpy' ) _A = output.images _A = image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) _A = np.array([0.4019, 0.4052, 0.3810, 0.4119, 0.3916, 0.3982, 0.4651, 0.4195, 0.5323] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2
27
import json import os from pathlib import Path import pytest from datasets.download.download_config import DownloadConfig from datasets.download.download_manager import DownloadManager from datasets.utils.file_utils import hash_url_to_filename __A : List[Any] = "http://www.mocksite.com/file1.txt" __A : List[Any] = "\"text\": [\"foo\", \"foo\"]" __A : Dict = "6d8ce9aa78a471c7477201efbeabd3bb01ac2e7d100a6dc024ba1608361f90a8" class lowerCamelCase: '''simple docstring''' __magic_name__ = 200 __magic_name__ = {'Content-Length': '100'} __magic_name__ = {} def lowerCAmelCase__ ( self , **snake_case_ ): return [bytes(snake_case_ , 'utf-8' )] def __lowerCAmelCase( *_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) -> Optional[Any]: """simple docstring""" return MockResponse() @pytest.mark.parametrize('urls_type' , [str, list, dict] ) def __lowerCAmelCase( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> str: """simple docstring""" import requests monkeypatch.setattr(_SCREAMING_SNAKE_CASE , 'request' , _SCREAMING_SNAKE_CASE ) _A = URL if issubclass(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): _A = url elif issubclass(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): _A = [url] elif issubclass(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): _A = {'train': url} _A = 'dummy' _A = 'downloads' _A = tmp_path _A = DownloadConfig( cache_dir=os.path.join(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) , use_etag=_SCREAMING_SNAKE_CASE , ) _A = DownloadManager(dataset_name=_SCREAMING_SNAKE_CASE , download_config=_SCREAMING_SNAKE_CASE ) _A = dl_manager.download(_SCREAMING_SNAKE_CASE ) _A = urls for downloaded_paths in [downloaded_paths]: if isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): _A = [downloaded_paths] _A = [urls] elif isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): assert "train" in downloaded_paths.keys() _A = downloaded_paths.values() _A = urls.values() assert downloaded_paths for downloaded_path, input_url in zip(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): assert downloaded_path == dl_manager.downloaded_paths[input_url] _A = Path(_SCREAMING_SNAKE_CASE ) _A = downloaded_path.parts assert parts[-1] == HASH assert parts[-2] == cache_subdir assert downloaded_path.exists() _A = downloaded_path.read_text() assert content == CONTENT _A = downloaded_path.with_suffix('.json' ) assert metadata_downloaded_path.exists() _A = json.loads(metadata_downloaded_path.read_text() ) assert metadata_content == {"url": URL, "etag": None} @pytest.mark.parametrize('paths_type' , [str, list, dict] ) def __lowerCAmelCase( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Optional[Any]: """simple docstring""" _A = str(_SCREAMING_SNAKE_CASE ) if issubclass(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): _A = filename elif issubclass(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): _A = [filename] elif issubclass(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): _A = {'train': filename} _A = 'dummy' _A = xz_file.parent _A = 'extracted' _A = DownloadConfig( cache_dir=_SCREAMING_SNAKE_CASE , use_etag=_SCREAMING_SNAKE_CASE , ) _A = DownloadManager(dataset_name=_SCREAMING_SNAKE_CASE , download_config=_SCREAMING_SNAKE_CASE ) _A = dl_manager.extract(_SCREAMING_SNAKE_CASE ) _A = paths for extracted_paths in [extracted_paths]: if isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): _A = [extracted_paths] _A = [paths] elif isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): assert "train" in extracted_paths.keys() _A = extracted_paths.values() _A = paths.values() assert extracted_paths for extracted_path, input_path in zip(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): assert extracted_path == dl_manager.extracted_paths[input_path] _A = Path(_SCREAMING_SNAKE_CASE ) _A = extracted_path.parts assert parts[-1] == hash_url_to_filename(_SCREAMING_SNAKE_CASE , etag=_SCREAMING_SNAKE_CASE ) assert parts[-2] == extracted_subdir assert extracted_path.exists() _A = extracted_path.read_text() _A = text_file.read_text() assert extracted_file_content == expected_file_content def __lowerCAmelCase( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Dict: """simple docstring""" assert path.endswith('.jsonl' ) for num_items, line in enumerate(_SCREAMING_SNAKE_CASE , start=1 ): _A = json.loads(line.decode('utf-8' ) ) assert item.keys() == {"col_1", "col_2", "col_3"} assert num_items == 4 @pytest.mark.parametrize('archive_jsonl' , ['tar_jsonl_path', 'zip_jsonl_path'] ) def __lowerCAmelCase( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Optional[int]: """simple docstring""" _A = request.getfixturevalue(_SCREAMING_SNAKE_CASE ) _A = DownloadManager() for num_jsonl, (path, file) in enumerate(dl_manager.iter_archive(_SCREAMING_SNAKE_CASE ) , start=1 ): _test_jsonl(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) assert num_jsonl == 2 @pytest.mark.parametrize('archive_nested_jsonl' , ['tar_nested_jsonl_path', 'zip_nested_jsonl_path'] ) def __lowerCAmelCase( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Any: """simple docstring""" _A = request.getfixturevalue(_SCREAMING_SNAKE_CASE ) _A = DownloadManager() for num_tar, (path, file) in enumerate(dl_manager.iter_archive(_SCREAMING_SNAKE_CASE ) , start=1 ): for num_jsonl, (subpath, subfile) in enumerate(dl_manager.iter_archive(_SCREAMING_SNAKE_CASE ) , start=1 ): _test_jsonl(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) assert num_tar == 1 assert num_jsonl == 2 def __lowerCAmelCase( _SCREAMING_SNAKE_CASE ) -> int: """simple docstring""" _A = DownloadManager() for num_file, file in enumerate(dl_manager.iter_files(_SCREAMING_SNAKE_CASE ) , start=1 ): assert os.path.basename(_SCREAMING_SNAKE_CASE ) == ("test.txt" if num_file == 1 else "train.txt") assert num_file == 2
27
1
import logging from pathlib import Path import numpy as np import pytorch_lightning as pl import torch from pytorch_lightning.callbacks import EarlyStopping, ModelCheckpoint from pytorch_lightning.utilities import rank_zero_only from utils_rag import save_json def __lowerCAmelCase( _SCREAMING_SNAKE_CASE ) -> Optional[int]: """simple docstring""" _A = filter(lambda _SCREAMING_SNAKE_CASE : p.requires_grad , model.parameters() ) _A = sum([np.prod(p.size() ) for p in model_parameters] ) return params __A : Union[str, Any] = logging.getLogger(__name__) def __lowerCAmelCase( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> int: """simple docstring""" if metric == "rouge2": _A = '{val_avg_rouge2:.4f}-{step_count}' elif metric == "bleu": _A = '{val_avg_bleu:.4f}-{step_count}' elif metric == "em": _A = '{val_avg_em:.4f}-{step_count}' elif metric == "loss": _A = '{val_avg_loss:.4f}-{step_count}' else: raise NotImplementedError( F"seq2seq callbacks only support rouge2 and bleu, got {metric}, You can make your own by adding to this" ' function.' ) _A = ModelCheckpoint( dirpath=_SCREAMING_SNAKE_CASE , filename=_SCREAMING_SNAKE_CASE , monitor=F"val_{metric}" , mode='max' , save_top_k=1 , every_n_epochs=1 , ) return checkpoint_callback def __lowerCAmelCase( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Tuple: """simple docstring""" return EarlyStopping( monitor=F"val_{metric}" , mode='min' if 'loss' in metric else 'max' , patience=_SCREAMING_SNAKE_CASE , verbose=_SCREAMING_SNAKE_CASE , ) class lowerCamelCase( pl.Callback ): '''simple docstring''' def lowerCAmelCase__ ( self , snake_case_ , snake_case_ ): _A = {F"lr_group_{i}": param['lr'] for i, param in enumerate(pl_module.trainer.optimizers[0].param_groups )} pl_module.logger.log_metrics(snake_case_ ) @rank_zero_only def lowerCAmelCase__ ( self , snake_case_ , snake_case_ , snake_case_ , snake_case_=True ): logger.info(F"***** {type_path} results at step {trainer.global_step:05d} *****" ) _A = trainer.callback_metrics trainer.logger.log_metrics({k: v for k, v in metrics.items() if k not in ['log', 'progress_bar', 'preds']} ) # Log results _A = Path(pl_module.hparams.output_dir ) if type_path == "test": _A = od / 'test_results.txt' _A = od / 'test_generations.txt' else: # this never gets hit. I prefer not to save intermediate generations, and results are in metrics.json # If people want this it will be easy enough to add back. _A = od / F"{type_path}_results/{trainer.global_step:05d}.txt" _A = od / F"{type_path}_generations/{trainer.global_step:05d}.txt" results_file.parent.mkdir(exist_ok=snake_case_ ) generations_file.parent.mkdir(exist_ok=snake_case_ ) with open(snake_case_ , 'a+' ) as writer: for key in sorted(snake_case_ ): if key in ["log", "progress_bar", "preds"]: continue _A = metrics[key] if isinstance(snake_case_ , torch.Tensor ): _A = val.item() _A = F"{key}: {val:.6f}\n" writer.write(snake_case_ ) if not save_generations: return if "preds" in metrics: _A = '\n'.join(metrics['preds'] ) generations_file.open('w+' ).write(snake_case_ ) @rank_zero_only def lowerCAmelCase__ ( self , snake_case_ , snake_case_ ): try: _A = pl_module.model.model.num_parameters() except AttributeError: _A = pl_module.model.num_parameters() _A = count_trainable_parameters(snake_case_ ) # mp stands for million parameters trainer.logger.log_metrics({'n_params': npars, 'mp': npars / 1E6, 'grad_mp': n_trainable_pars / 1E6} ) @rank_zero_only def lowerCAmelCase__ ( self , snake_case_ , snake_case_ ): save_json(pl_module.metrics , pl_module.metrics_save_path ) return self._write_logs(snake_case_ , snake_case_ , 'test' ) @rank_zero_only def lowerCAmelCase__ ( self , snake_case_ , snake_case_ ): save_json(pl_module.metrics , pl_module.metrics_save_path ) # Uncommenting this will save val generations # return self._write_logs(trainer, pl_module, "valid")
27
from __future__ import annotations from fractions import Fraction from math import gcd, sqrt def __lowerCAmelCase( _SCREAMING_SNAKE_CASE ) -> bool: """simple docstring""" _A = int(number**0.5 ) return number == sq * sq def __lowerCAmelCase( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> tuple[int, int]: """simple docstring""" _A = x_num * y_den * z_den + y_num * x_den * z_den + z_num * x_den * y_den _A = x_den * y_den * z_den _A = gcd(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) top //= hcf bottom //= hcf return top, bottom def __lowerCAmelCase( _SCREAMING_SNAKE_CASE = 35 ) -> int: """simple docstring""" _A = set() _A = 42 _A = Fraction(0 ) _A = 42 for x_num in range(1 , order + 1 ): for x_den in range(x_num + 1 , order + 1 ): for y_num in range(1 , order + 1 ): for y_den in range(y_num + 1 , order + 1 ): # n=1 _A = x_num * y_den + x_den * y_num _A = x_den * y_den _A = gcd(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) z_num //= hcf z_den //= hcf if 0 < z_num < z_den <= order: _A = add_three( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) unique_s.add(_SCREAMING_SNAKE_CASE ) # n=2 _A = ( x_num * x_num * y_den * y_den + x_den * x_den * y_num * y_num ) _A = x_den * x_den * y_den * y_den if is_sq(_SCREAMING_SNAKE_CASE ) and is_sq(_SCREAMING_SNAKE_CASE ): _A = int(sqrt(_SCREAMING_SNAKE_CASE ) ) _A = int(sqrt(_SCREAMING_SNAKE_CASE ) ) _A = gcd(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) z_num //= hcf z_den //= hcf if 0 < z_num < z_den <= order: _A = add_three( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) unique_s.add(_SCREAMING_SNAKE_CASE ) # n=-1 _A = x_num * y_num _A = x_den * y_num + x_num * y_den _A = gcd(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) z_num //= hcf z_den //= hcf if 0 < z_num < z_den <= order: _A = add_three( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) unique_s.add(_SCREAMING_SNAKE_CASE ) # n=2 _A = x_num * x_num * y_num * y_num _A = ( x_den * x_den * y_num * y_num + x_num * x_num * y_den * y_den ) if is_sq(_SCREAMING_SNAKE_CASE ) and is_sq(_SCREAMING_SNAKE_CASE ): _A = int(sqrt(_SCREAMING_SNAKE_CASE ) ) _A = int(sqrt(_SCREAMING_SNAKE_CASE ) ) _A = gcd(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) z_num //= hcf z_den //= hcf if 0 < z_num < z_den <= order: _A = add_three( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) unique_s.add(_SCREAMING_SNAKE_CASE ) for num, den in unique_s: total += Fraction(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) return total.denominator + total.numerator if __name__ == "__main__": print(f"{solution() = }")
27
1
import argparse import json from typing import List from ltp import LTP from transformers.models.bert.tokenization_bert import BertTokenizer def __lowerCAmelCase( _SCREAMING_SNAKE_CASE ) -> List[Any]: """simple docstring""" if ( (cp >= 0X4E_00 and cp <= 0X9F_FF) or (cp >= 0X34_00 and cp <= 0X4D_BF) # or (cp >= 0X2_00_00 and cp <= 0X2_A6_DF) # or (cp >= 0X2_A7_00 and cp <= 0X2_B7_3F) # or (cp >= 0X2_B7_40 and cp <= 0X2_B8_1F) # or (cp >= 0X2_B8_20 and cp <= 0X2_CE_AF) # or (cp >= 0XF9_00 and cp <= 0XFA_FF) or (cp >= 0X2_F8_00 and cp <= 0X2_FA_1F) # ): # return True return False def __lowerCAmelCase( _SCREAMING_SNAKE_CASE ) -> int: """simple docstring""" for char in word: _A = ord(_SCREAMING_SNAKE_CASE ) if not _is_chinese_char(_SCREAMING_SNAKE_CASE ): return 0 return 1 def __lowerCAmelCase( _SCREAMING_SNAKE_CASE ) -> str: """simple docstring""" _A = set() for token in tokens: _A = len(_SCREAMING_SNAKE_CASE ) > 1 and is_chinese(_SCREAMING_SNAKE_CASE ) if chinese_word: word_set.add(_SCREAMING_SNAKE_CASE ) _A = list(_SCREAMING_SNAKE_CASE ) return word_list def __lowerCAmelCase( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Any: """simple docstring""" if not chinese_word_set: return bert_tokens _A = max([len(_SCREAMING_SNAKE_CASE ) for w in chinese_word_set] ) _A = bert_tokens _A, _A = 0, len(_SCREAMING_SNAKE_CASE ) while start < end: _A = True if is_chinese(bert_word[start] ): _A = min(end - start , _SCREAMING_SNAKE_CASE ) for i in range(_SCREAMING_SNAKE_CASE , 1 , -1 ): _A = ''.join(bert_word[start : start + i] ) if whole_word in chinese_word_set: for j in range(start + 1 , start + i ): _A = '##' + bert_word[j] _A = start + i _A = False break if single_word: start += 1 return bert_word def __lowerCAmelCase( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Optional[int]: """simple docstring""" _A = [] for i in range(0 , len(_SCREAMING_SNAKE_CASE ) , 100 ): _A = ltp_tokenizer.pipeline(lines[i : i + 100] , tasks=['cws'] ).cws _A = [get_chinese_word(_SCREAMING_SNAKE_CASE ) for r in res] ltp_res.extend(_SCREAMING_SNAKE_CASE ) assert len(_SCREAMING_SNAKE_CASE ) == len(_SCREAMING_SNAKE_CASE ) _A = [] for i in range(0 , len(_SCREAMING_SNAKE_CASE ) , 100 ): _A = bert_tokenizer(lines[i : i + 100] , add_special_tokens=_SCREAMING_SNAKE_CASE , truncation=_SCREAMING_SNAKE_CASE , max_length=512 ) bert_res.extend(res['input_ids'] ) assert len(_SCREAMING_SNAKE_CASE ) == len(_SCREAMING_SNAKE_CASE ) _A = [] for input_ids, chinese_word in zip(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): _A = [] for id in input_ids: _A = bert_tokenizer._convert_id_to_token(_SCREAMING_SNAKE_CASE ) input_tokens.append(_SCREAMING_SNAKE_CASE ) _A = add_sub_symbol(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) _A = [] # We only save pos of chinese subwords start with ##, which mean is part of a whole word. for i, token in enumerate(_SCREAMING_SNAKE_CASE ): if token[:2] == "##": _A = token[2:] # save chinese tokens' pos if len(_SCREAMING_SNAKE_CASE ) == 1 and _is_chinese_char(ord(_SCREAMING_SNAKE_CASE ) ): ref_id.append(_SCREAMING_SNAKE_CASE ) ref_ids.append(_SCREAMING_SNAKE_CASE ) assert len(_SCREAMING_SNAKE_CASE ) == len(_SCREAMING_SNAKE_CASE ) return ref_ids def __lowerCAmelCase( _SCREAMING_SNAKE_CASE ) -> Union[str, Any]: """simple docstring""" with open(args.file_name , 'r' , encoding='utf-8' ) as f: _A = f.readlines() _A = [line.strip() for line in data if len(_SCREAMING_SNAKE_CASE ) > 0 and not line.isspace()] # avoid delimiter like '\u2029' _A = LTP(args.ltp ) # faster in GPU device _A = BertTokenizer.from_pretrained(args.bert ) _A = prepare_ref(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) with open(args.save_path , 'w' , encoding='utf-8' ) as f: _A = [json.dumps(_SCREAMING_SNAKE_CASE ) + '\n' for ref in ref_ids] f.writelines(_SCREAMING_SNAKE_CASE ) if __name__ == "__main__": __A : List[str] = argparse.ArgumentParser(description="prepare_chinese_ref") parser.add_argument( "--file_name", required=False, type=str, default="./resources/chinese-demo.txt", help="file need process, same as training data in lm", ) parser.add_argument( "--ltp", required=False, type=str, default="./resources/ltp", help="resources for LTP tokenizer, usually a path", ) parser.add_argument( "--bert", required=False, type=str, default="./resources/robert", help="resources for Bert tokenizer", ) parser.add_argument( "--save_path", required=False, type=str, default="./resources/ref.txt", help="path to save res", ) __A : List[Any] = parser.parse_args() main(args)
27
from __future__ import annotations import math def __lowerCAmelCase( _SCREAMING_SNAKE_CASE ) -> list[int]: """simple docstring""" if num <= 0: _A = F"{num}: Invalid input, please enter a positive integer." raise ValueError(_SCREAMING_SNAKE_CASE ) _A = [True] * (num + 1) _A = [] _A = 2 _A = int(math.sqrt(_SCREAMING_SNAKE_CASE ) ) while start <= end: # If start is a prime if sieve[start] is True: prime.append(_SCREAMING_SNAKE_CASE ) # Set multiples of start be False for i in range(start * start , num + 1 , _SCREAMING_SNAKE_CASE ): if sieve[i] is True: _A = False start += 1 for j in range(end + 1 , num + 1 ): if sieve[j] is True: prime.append(_SCREAMING_SNAKE_CASE ) return prime if __name__ == "__main__": print(prime_sieve(int(input("Enter a positive integer: ").strip())))
27
1
import inspect import unittest from transformers import ViTConfig from transformers.testing_utils import ( require_accelerate, require_torch, require_torch_gpu, require_vision, slow, torch_device, ) from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTForImageClassification, ViTForMaskedImageModeling, ViTModel from transformers.models.vit.modeling_vit import VIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class lowerCamelCase: '''simple docstring''' def __init__( self , snake_case_ , snake_case_=13 , snake_case_=30 , snake_case_=2 , snake_case_=3 , snake_case_=True , snake_case_=True , snake_case_=32 , snake_case_=5 , snake_case_=4 , snake_case_=37 , snake_case_="gelu" , snake_case_=0.1 , snake_case_=0.1 , snake_case_=10 , snake_case_=0.02 , snake_case_=None , snake_case_=2 , ): _A = parent _A = batch_size _A = image_size _A = patch_size _A = num_channels _A = is_training _A = use_labels _A = hidden_size _A = num_hidden_layers _A = num_attention_heads _A = intermediate_size _A = hidden_act _A = hidden_dropout_prob _A = attention_probs_dropout_prob _A = type_sequence_label_size _A = initializer_range _A = scope _A = encoder_stride # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) _A = (image_size // patch_size) ** 2 _A = num_patches + 1 def lowerCAmelCase__ ( self ): _A = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) _A = None if self.use_labels: _A = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _A = self.get_config() return config, pixel_values, labels def lowerCAmelCase__ ( self ): return ViTConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=snake_case_ , initializer_range=self.initializer_range , encoder_stride=self.encoder_stride , ) def lowerCAmelCase__ ( self , snake_case_ , snake_case_ , snake_case_ ): _A = ViTModel(config=snake_case_ ) model.to(snake_case_ ) model.eval() _A = model(snake_case_ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def lowerCAmelCase__ ( self , snake_case_ , snake_case_ , snake_case_ ): _A = ViTForMaskedImageModeling(config=snake_case_ ) model.to(snake_case_ ) model.eval() _A = model(snake_case_ ) self.parent.assertEqual( result.reconstruction.shape , (self.batch_size, self.num_channels, self.image_size, self.image_size) ) # test greyscale images _A = 1 _A = ViTForMaskedImageModeling(snake_case_ ) model.to(snake_case_ ) model.eval() _A = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) _A = model(snake_case_ ) self.parent.assertEqual(result.reconstruction.shape , (self.batch_size, 1, self.image_size, self.image_size) ) def lowerCAmelCase__ ( self , snake_case_ , snake_case_ , snake_case_ ): _A = self.type_sequence_label_size _A = ViTForImageClassification(snake_case_ ) model.to(snake_case_ ) model.eval() _A = model(snake_case_ , labels=snake_case_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images _A = 1 _A = ViTForImageClassification(snake_case_ ) model.to(snake_case_ ) model.eval() _A = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) _A = model(snake_case_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def lowerCAmelCase__ ( self ): _A = self.prepare_config_and_inputs() ( ( _A ), ( _A ), ( _A ), ) = config_and_inputs _A = {'pixel_values': pixel_values} return config, inputs_dict @require_torch class lowerCamelCase( __snake_case , __snake_case , unittest.TestCase ): '''simple docstring''' __magic_name__ = ( ( ViTModel, ViTForImageClassification, ViTForMaskedImageModeling, ) if is_torch_available() else () ) __magic_name__ = ( {'feature-extraction': ViTModel, 'image-classification': ViTForImageClassification} if is_torch_available() else {} ) __magic_name__ = True __magic_name__ = False __magic_name__ = False __magic_name__ = False def lowerCAmelCase__ ( self ): _A = ViTModelTester(self ) _A = ConfigTester(self , config_class=snake_case_ , has_text_modality=snake_case_ , hidden_size=37 ) def lowerCAmelCase__ ( self ): self.config_tester.run_common_tests() @unittest.skip(reason='ViT does not use inputs_embeds' ) def lowerCAmelCase__ ( self ): pass def lowerCAmelCase__ ( self ): _A, _A = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _A = model_class(snake_case_ ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) _A = model.get_output_embeddings() self.assertTrue(x is None or isinstance(snake_case_ , nn.Linear ) ) def lowerCAmelCase__ ( self ): _A, _A = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _A = model_class(snake_case_ ) _A = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _A = [*signature.parameters.keys()] _A = ['pixel_values'] self.assertListEqual(arg_names[:1] , snake_case_ ) def lowerCAmelCase__ ( self ): _A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*snake_case_ ) def lowerCAmelCase__ ( self ): _A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_image_modeling(*snake_case_ ) def lowerCAmelCase__ ( self ): _A = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*snake_case_ ) @slow def lowerCAmelCase__ ( self ): for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _A = ViTModel.from_pretrained(snake_case_ ) self.assertIsNotNone(snake_case_ ) def __lowerCAmelCase( ) -> Optional[Any]: """simple docstring""" _A = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_torch @require_vision class lowerCamelCase( unittest.TestCase ): '''simple docstring''' @cached_property def lowerCAmelCase__ ( self ): return ViTImageProcessor.from_pretrained('google/vit-base-patch16-224' ) if is_vision_available() else None @slow def lowerCAmelCase__ ( self ): _A = ViTForImageClassification.from_pretrained('google/vit-base-patch16-224' ).to(snake_case_ ) _A = self.default_image_processor _A = prepare_img() _A = image_processor(images=snake_case_ , return_tensors='pt' ).to(snake_case_ ) # forward pass with torch.no_grad(): _A = model(**snake_case_ ) # verify the logits _A = torch.Size((1, 1000) ) self.assertEqual(outputs.logits.shape , snake_case_ ) _A = torch.tensor([-0.2744, 0.8215, -0.0836] ).to(snake_case_ ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , snake_case_ , atol=1E-4 ) ) @slow def lowerCAmelCase__ ( self ): # ViT models have an `interpolate_pos_encoding` argument in their forward method, # allowing to interpolate the pre-trained position embeddings in order to use # the model on higher resolutions. The DINO model by Facebook AI leverages this # to visualize self-attention on higher resolution images. _A = ViTModel.from_pretrained('facebook/dino-vits8' ).to(snake_case_ ) _A = ViTImageProcessor.from_pretrained('facebook/dino-vits8' , size=480 ) _A = prepare_img() _A = image_processor(images=snake_case_ , return_tensors='pt' ) _A = inputs.pixel_values.to(snake_case_ ) # forward pass with torch.no_grad(): _A = model(snake_case_ , interpolate_pos_encoding=snake_case_ ) # verify the logits _A = torch.Size((1, 3601, 384) ) self.assertEqual(outputs.last_hidden_state.shape , snake_case_ ) _A = torch.tensor( [[4.2340, 4.3906, -6.6692], [4.5463, 1.8928, -6.7257], [4.4429, 0.8496, -5.8585]] ).to(snake_case_ ) self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3] , snake_case_ , atol=1E-4 ) ) @slow @require_accelerate @require_torch_gpu def lowerCAmelCase__ ( self ): _A = ViTModel.from_pretrained('facebook/dino-vits8' , torch_dtype=torch.floataa , device_map='auto' ) _A = self.default_image_processor _A = prepare_img() _A = image_processor(images=snake_case_ , return_tensors='pt' ) _A = inputs.pixel_values.to(snake_case_ ) # forward pass to make sure inference works in fp16 with torch.no_grad(): _A = model(snake_case_ )
27
__A : Dict = "Alexander Joslin" import operator as op from .stack import Stack def __lowerCAmelCase( _SCREAMING_SNAKE_CASE ) -> int: """simple docstring""" _A = {'*': op.mul, '/': op.truediv, '+': op.add, '-': op.sub} _A = Stack() _A = Stack() for i in equation: if i.isdigit(): # RULE 1 operand_stack.push(int(_SCREAMING_SNAKE_CASE ) ) elif i in operators: # RULE 2 operator_stack.push(_SCREAMING_SNAKE_CASE ) elif i == ")": # RULE 4 _A = operator_stack.peek() operator_stack.pop() _A = operand_stack.peek() operand_stack.pop() _A = operand_stack.peek() operand_stack.pop() _A = operators[opr](_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) operand_stack.push(_SCREAMING_SNAKE_CASE ) # RULE 5 return operand_stack.peek() if __name__ == "__main__": __A : Any = "(5 + ((4 * 2) * (2 + 3)))" # answer = 45 print(f"{equation} = {dijkstras_two_stack_algorithm(equation)}")
27
1