code
stringlengths
87
55.2k
code_codestyle
int64
0
349
style_context
stringlengths
135
49.1k
style_context_codestyle
int64
0
349
label
int64
0
1
from multiprocessing import Lock, Pipe, Process # lock used to ensure that two processes do not access a pipe at the same time UpperCAmelCase__ = Lock() def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , __snake_case ) -> str: """simple docstring""" global process_lock # we perform n swaps since after n swaps we know we are sorted # we *could* stop early if we are sorted already, but it takes as long to # find out we are sorted as it does to sort the list with this algorithm for i in range(0 , 10 ): if (i + position) % 2 == 0 and r_send is not None: # send your value to your right neighbor process_lock.acquire() r_send[1].send(__snake_case ) process_lock.release() # receive your right neighbor's value process_lock.acquire() _lowercase =rr_cv[0].recv() process_lock.release() # take the lower value since you are on the left _lowercase =min(__snake_case , __snake_case ) elif (i + position) % 2 != 0 and l_send is not None: # send your value to your left neighbor process_lock.acquire() l_send[1].send(__snake_case ) process_lock.release() # receive your left neighbor's value process_lock.acquire() _lowercase =lr_cv[0].recv() process_lock.release() # take the higher value since you are on the right _lowercase =max(__snake_case , __snake_case ) # after all swaps are performed, send the values back to main result_pipe[1].send(__snake_case ) def UpperCAmelCase_ ( __snake_case ) -> int: """simple docstring""" _lowercase =[] _lowercase =[] # initialize the list of pipes where the values will be retrieved for _ in arr: result_pipe.append(Pipe() ) # creates the processes # the first and last process only have one neighbor so they are made outside # of the loop _lowercase =Pipe() _lowercase =Pipe() process_array_.append( Process( target=__snake_case , args=(0, arr[0], None, temp_rs, None, temp_rr, result_pipe[0]) , ) ) _lowercase =temp_rs _lowercase =temp_rr for i in range(1 , len(__snake_case ) - 1 ): _lowercase =Pipe() _lowercase =Pipe() process_array_.append( Process( target=__snake_case , args=(i, arr[i], temp_ls, temp_rs, temp_lr, temp_rr, result_pipe[i]) , ) ) _lowercase =temp_rs _lowercase =temp_rr process_array_.append( Process( target=__snake_case , args=( len(__snake_case ) - 1, arr[len(__snake_case ) - 1], temp_ls, None, temp_lr, None, result_pipe[len(__snake_case ) - 1], ) , ) ) # start the processes for p in process_array_: p.start() # wait for the processes to end and write their values to the list for p in range(0 , len(__snake_case ) ): _lowercase =result_pipe[p][0].recv() process_array_[p].join() return arr def UpperCAmelCase_ ( ) -> str: """simple docstring""" _lowercase =list(range(10 , 0 , -1 ) ) print('''Initial List''' ) print(*__snake_case ) _lowercase =odd_even_transposition(__snake_case ) print('''Sorted List\n''' ) print(*__snake_case ) if __name__ == "__main__": main()
5
# Copyright 2023 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available UpperCAmelCase__ = { '''configuration_efficientnet''': [ '''EFFICIENTNET_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''EfficientNetConfig''', '''EfficientNetOnnxConfig''', ] } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = ['''EfficientNetImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ '''EFFICIENTNET_PRETRAINED_MODEL_ARCHIVE_LIST''', '''EfficientNetForImageClassification''', '''EfficientNetModel''', '''EfficientNetPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_efficientnet import ( EFFICIENTNET_PRETRAINED_CONFIG_ARCHIVE_MAP, EfficientNetConfig, EfficientNetOnnxConfig, ) try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_efficientnet import EfficientNetImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_efficientnet import ( EFFICIENTNET_PRETRAINED_MODEL_ARCHIVE_LIST, EfficientNetForImageClassification, EfficientNetModel, EfficientNetPreTrainedModel, ) else: import sys UpperCAmelCase__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure)
5
1
import argparse import glob import logging import os import time from argparse import Namespace import numpy as np import torch from lightning_base import BaseTransformer, add_generic_args, generic_train from torch.utils.data import DataLoader, TensorDataset from transformers import glue_compute_metrics as compute_metrics from transformers import glue_convert_examples_to_features as convert_examples_to_features from transformers import glue_output_modes, glue_tasks_num_labels from transformers import glue_processors as processors UpperCAmelCase__ = logging.getLogger(__name__) class lowerCamelCase__ ( lowerCAmelCase): SCREAMING_SNAKE_CASE__ = '''sequence-classification''' def __init__(self , UpperCAmelCase ) -> Union[str, Any]: if type(UpperCAmelCase ) == dict: _lowercase =Namespace(**UpperCAmelCase ) _lowercase =glue_output_modes[hparams.task] _lowercase =glue_tasks_num_labels[hparams.task] super().__init__(UpperCAmelCase , UpperCAmelCase , self.mode ) def __A (self , **UpperCAmelCase ) -> List[Any]: return self.model(**UpperCAmelCase ) def __A (self , UpperCAmelCase , UpperCAmelCase ) -> Optional[Any]: _lowercase ={'''input_ids''': batch[0], '''attention_mask''': batch[1], '''labels''': batch[3]} if self.config.model_type not in ["distilbert", "bart"]: _lowercase =batch[2] if self.config.model_type in ['''bert''', '''xlnet''', '''albert'''] else None _lowercase =self(**UpperCAmelCase ) _lowercase =outputs[0] _lowercase =self.trainer.lr_schedulers[0]['''scheduler'''] _lowercase ={'''loss''': loss, '''rate''': lr_scheduler.get_last_lr()[-1]} return {"loss": loss, "log": tensorboard_logs} def __A (self ) -> Dict: _lowercase =self.hparams _lowercase =processors[args.task]() _lowercase =processor.get_labels() for mode in ["train", "dev"]: _lowercase =self._feature_file(UpperCAmelCase ) if os.path.exists(UpperCAmelCase ) and not args.overwrite_cache: logger.info('''Loading features from cached file %s''' , UpperCAmelCase ) else: logger.info('''Creating features from dataset file at %s''' , args.data_dir ) _lowercase =( processor.get_dev_examples(args.data_dir ) if mode == '''dev''' else processor.get_train_examples(args.data_dir ) ) _lowercase =convert_examples_to_features( UpperCAmelCase , self.tokenizer , max_length=args.max_seq_length , label_list=self.labels , output_mode=args.glue_output_mode , ) logger.info('''Saving features into cached file %s''' , UpperCAmelCase ) torch.save(UpperCAmelCase , UpperCAmelCase ) def __A (self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = False ) -> DataLoader: _lowercase ='''dev''' if mode == '''test''' else mode _lowercase =self._feature_file(UpperCAmelCase ) logger.info('''Loading features from cached file %s''' , UpperCAmelCase ) _lowercase =torch.load(UpperCAmelCase ) _lowercase =torch.tensor([f.input_ids for f in features] , dtype=torch.long ) _lowercase =torch.tensor([f.attention_mask for f in features] , dtype=torch.long ) _lowercase =torch.tensor([f.token_type_ids for f in features] , dtype=torch.long ) if self.hparams.glue_output_mode == "classification": _lowercase =torch.tensor([f.label for f in features] , dtype=torch.long ) elif self.hparams.glue_output_mode == "regression": _lowercase =torch.tensor([f.label for f in features] , dtype=torch.float ) return DataLoader( TensorDataset(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) , batch_size=UpperCAmelCase , shuffle=UpperCAmelCase , ) def __A (self , UpperCAmelCase , UpperCAmelCase ) -> int: _lowercase ={'''input_ids''': batch[0], '''attention_mask''': batch[1], '''labels''': batch[3]} if self.config.model_type not in ["distilbert", "bart"]: _lowercase =batch[2] if self.config.model_type in ['''bert''', '''xlnet''', '''albert'''] else None _lowercase =self(**UpperCAmelCase ) _lowercase , _lowercase =outputs[:2] _lowercase =logits.detach().cpu().numpy() _lowercase =inputs['''labels'''].detach().cpu().numpy() return {"val_loss": tmp_eval_loss.detach().cpu(), "pred": preds, "target": out_label_ids} def __A (self , UpperCAmelCase ) -> tuple: _lowercase =torch.stack([x['''val_loss'''] for x in outputs] ).mean().detach().cpu().item() _lowercase =np.concatenate([x['''pred'''] for x in outputs] , axis=0 ) if self.hparams.glue_output_mode == "classification": _lowercase =np.argmax(UpperCAmelCase , axis=1 ) elif self.hparams.glue_output_mode == "regression": _lowercase =np.squeeze(UpperCAmelCase ) _lowercase =np.concatenate([x['''target'''] for x in outputs] , axis=0 ) _lowercase =[[] for _ in range(out_label_ids.shape[0] )] _lowercase =[[] for _ in range(out_label_ids.shape[0] )] _lowercase ={**{'''val_loss''': val_loss_mean}, **compute_metrics(self.hparams.task , UpperCAmelCase , UpperCAmelCase )} _lowercase =dict(results.items() ) _lowercase =results return ret, preds_list, out_label_list def __A (self , UpperCAmelCase ) -> dict: _lowercase , _lowercase , _lowercase =self._eval_end(UpperCAmelCase ) _lowercase =ret['''log'''] return {"val_loss": logs["val_loss"], "log": logs, "progress_bar": logs} def __A (self , UpperCAmelCase ) -> dict: _lowercase , _lowercase , _lowercase =self._eval_end(UpperCAmelCase ) _lowercase =ret['''log'''] # `val_loss` is the key returned by `self._eval_end()` but actually refers to `test_loss` return {"avg_test_loss": logs["val_loss"], "log": logs, "progress_bar": logs} @staticmethod def __A (UpperCAmelCase , UpperCAmelCase ) -> List[Any]: BaseTransformer.add_model_specific_args(UpperCAmelCase , UpperCAmelCase ) parser.add_argument( '''--max_seq_length''' , default=1_2_8 , type=UpperCAmelCase , help=( '''The maximum total input sequence length after tokenization. Sequences longer ''' '''than this will be truncated, sequences shorter will be padded.''' ) , ) parser.add_argument( '''--task''' , default='''''' , type=UpperCAmelCase , required=UpperCAmelCase , help='''The GLUE task to run''' , ) parser.add_argument( '''--gpus''' , default=0 , type=UpperCAmelCase , help='''The number of GPUs allocated for this, it is by default 0 meaning none''' , ) parser.add_argument( '''--overwrite_cache''' , action='''store_true''' , help='''Overwrite the cached training and evaluation sets''' ) return parser def UpperCAmelCase_ ( ) -> Dict: """simple docstring""" _lowercase =argparse.ArgumentParser() add_generic_args(__snake_case , os.getcwd() ) _lowercase =GLUETransformer.add_model_specific_args(__snake_case , os.getcwd() ) _lowercase =parser.parse_args() # If output_dir not provided, a folder will be generated in pwd if args.output_dir is None: _lowercase =os.path.join( '''./results''' , F"{args.task}_{time.strftime('%Y%m%d_%H%M%S' )}" , ) os.makedirs(args.output_dir ) _lowercase =GLUETransformer(__snake_case ) _lowercase =generic_train(__snake_case , __snake_case ) # Optionally, predict on dev set and write to output_dir if args.do_predict: _lowercase =sorted(glob.glob(os.path.join(args.output_dir , '''checkpoint-epoch=*.ckpt''' ) , recursive=__snake_case ) ) _lowercase =model.load_from_checkpoint(checkpoints[-1] ) return trainer.test(__snake_case ) if __name__ == "__main__": main()
5
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available UpperCAmelCase__ = { '''configuration_timesformer''': ['''TIMESFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''TimesformerConfig'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ '''TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TimesformerModel''', '''TimesformerForVideoClassification''', '''TimesformerPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_timesformer import TIMESFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TimesformerConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_timesformer import ( TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TimesformerForVideoClassification, TimesformerModel, TimesformerPreTrainedModel, ) else: import sys UpperCAmelCase__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
5
1
import logging import os from typing import List, TextIO, Union from conllu import parse_incr from utils_ner import InputExample, Split, TokenClassificationTask UpperCAmelCase__ = logging.getLogger(__name__) class lowerCamelCase__ ( lowerCAmelCase): def __init__(self , UpperCAmelCase=-1 ) -> Optional[int]: # in NER datasets, the last column is usually reserved for NER label _lowercase =label_idx def __A (self , UpperCAmelCase , UpperCAmelCase ) -> List[InputExample]: if isinstance(UpperCAmelCase , UpperCAmelCase ): _lowercase =mode.value _lowercase =os.path.join(UpperCAmelCase , f"{mode}.txt" ) _lowercase =1 _lowercase =[] with open(UpperCAmelCase , encoding='''utf-8''' ) as f: _lowercase =[] _lowercase =[] for line in f: if line.startswith('''-DOCSTART-''' ) or line == "" or line == "\n": if words: examples.append(InputExample(guid=f"{mode}-{guid_index}" , words=UpperCAmelCase , labels=UpperCAmelCase ) ) guid_index += 1 _lowercase =[] _lowercase =[] else: _lowercase =line.split(''' ''' ) words.append(splits[0] ) if len(UpperCAmelCase ) > 1: labels.append(splits[self.label_idx].replace('''\n''' , '''''' ) ) else: # Examples could have no label for mode = "test" labels.append('''O''' ) if words: examples.append(InputExample(guid=f"{mode}-{guid_index}" , words=UpperCAmelCase , labels=UpperCAmelCase ) ) return examples def __A (self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) -> List[str]: _lowercase =0 for line in test_input_reader: if line.startswith('''-DOCSTART-''' ) or line == "" or line == "\n": writer.write(UpperCAmelCase ) if not preds_list[example_id]: example_id += 1 elif preds_list[example_id]: _lowercase =line.split()[0] + ''' ''' + preds_list[example_id].pop(0 ) + '''\n''' writer.write(UpperCAmelCase ) else: logger.warning('''Maximum sequence length exceeded: No prediction for \'%s\'.''' , line.split()[0] ) def __A (self , UpperCAmelCase ) -> List[str]: if path: with open(UpperCAmelCase , '''r''' ) as f: _lowercase =f.read().splitlines() if "O" not in labels: _lowercase =['''O'''] + labels return labels else: return ["O", "B-MISC", "I-MISC", "B-PER", "I-PER", "B-ORG", "I-ORG", "B-LOC", "I-LOC"] class lowerCamelCase__ ( lowerCAmelCase): def __init__(self ) -> str: # in CONLL2003 dataset chunk column is second-to-last super().__init__(label_idx=-2 ) def __A (self , UpperCAmelCase ) -> List[str]: if path: with open(UpperCAmelCase , '''r''' ) as f: _lowercase =f.read().splitlines() if "O" not in labels: _lowercase =['''O'''] + labels return labels else: return [ "O", "B-ADVP", "B-INTJ", "B-LST", "B-PRT", "B-NP", "B-SBAR", "B-VP", "B-ADJP", "B-CONJP", "B-PP", "I-ADVP", "I-INTJ", "I-LST", "I-PRT", "I-NP", "I-SBAR", "I-VP", "I-ADJP", "I-CONJP", "I-PP", ] class lowerCamelCase__ ( lowerCAmelCase): def __A (self , UpperCAmelCase , UpperCAmelCase ) -> List[InputExample]: if isinstance(UpperCAmelCase , UpperCAmelCase ): _lowercase =mode.value _lowercase =os.path.join(UpperCAmelCase , f"{mode}.txt" ) _lowercase =1 _lowercase =[] with open(UpperCAmelCase , encoding='''utf-8''' ) as f: for sentence in parse_incr(UpperCAmelCase ): _lowercase =[] _lowercase =[] for token in sentence: words.append(token['''form'''] ) labels.append(token['''upos'''] ) assert len(UpperCAmelCase ) == len(UpperCAmelCase ) if words: examples.append(InputExample(guid=f"{mode}-{guid_index}" , words=UpperCAmelCase , labels=UpperCAmelCase ) ) guid_index += 1 return examples def __A (self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) -> Any: _lowercase =0 for sentence in parse_incr(UpperCAmelCase ): _lowercase =preds_list[example_id] _lowercase ='''''' for token in sentence: out += f"{token['form']} ({token['upos']}|{s_p.pop(0 )}) " out += "\n" writer.write(UpperCAmelCase ) example_id += 1 def __A (self , UpperCAmelCase ) -> List[str]: if path: with open(UpperCAmelCase , '''r''' ) as f: return f.read().splitlines() else: return [ "ADJ", "ADP", "ADV", "AUX", "CCONJ", "DET", "INTJ", "NOUN", "NUM", "PART", "PRON", "PROPN", "PUNCT", "SCONJ", "SYM", "VERB", "X", ]
5
def UpperCAmelCase_ ( __snake_case , __snake_case ) -> List[Any]: """simple docstring""" if b == 0: return 1 if (b % 2) == 0: return actual_power(__snake_case , int(b / 2 ) ) * actual_power(__snake_case , int(b / 2 ) ) else: return a * actual_power(__snake_case , int(b / 2 ) ) * actual_power(__snake_case , int(b / 2 ) ) def UpperCAmelCase_ ( __snake_case , __snake_case ) -> float: """simple docstring""" if b < 0: return 1 / actual_power(__snake_case , __snake_case ) return actual_power(__snake_case , __snake_case ) if __name__ == "__main__": print(power(-2, -3))
5
1
import comet # From: unbabel-comet import torch import datasets UpperCAmelCase__ = datasets.logging.get_logger(__name__) UpperCAmelCase__ = '''\ @inproceedings{rei-EtAl:2020:WMT, author = {Rei, Ricardo and Stewart, Craig and Farinha, Ana C and Lavie, Alon}, title = {Unbabel\'s Participation in the WMT20 Metrics Shared Task}, booktitle = {Proceedings of the Fifth Conference on Machine Translation}, month = {November}, year = {2020}, address = {Online}, publisher = {Association for Computational Linguistics}, pages = {909--918}, } @inproceedings{rei-etal-2020-comet, title = "{COMET}: A Neural Framework for {MT} Evaluation", author = "Rei, Ricardo and Stewart, Craig and Farinha, Ana C and Lavie, Alon", booktitle = "Proceedings of the 2020 Conference on Empirical Methods in Natural Language Processing (EMNLP)", month = nov, year = "2020", address = "Online", publisher = "Association for Computational Linguistics", url = "https://www.aclweb.org/anthology/2020.emnlp-main.213", pages = "2685--2702", } ''' UpperCAmelCase__ = '''\ Crosslingual Optimized Metric for Evaluation of Translation (COMET) is an open-source framework used to train Machine Translation metrics that achieve high levels of correlation with different types of human judgments (HTER, DA\'s or MQM). With the release of the framework the authors also released fully trained models that were used to compete in the WMT20 Metrics Shared Task achieving SOTA in that years competition. See the [README.md] file at https://unbabel.github.io/COMET/html/models.html for more information. ''' UpperCAmelCase__ = ''' COMET score. Args: `sources` (list of str): Source sentences `predictions` (list of str): candidate translations `references` (list of str): reference translations `cuda` (bool): If set to True, runs COMET using GPU `show_progress` (bool): Shows progress `model`: COMET model to be used. Will default to `wmt-large-da-estimator-1719` if None. Returns: `samples`: List of dictionaries with `src`, `mt`, `ref` and `score`. `scores`: List of scores. Examples: >>> comet_metric = datasets.load_metric(\'comet\') >>> # comet_metric = load_metric(\'comet\', \'wmt20-comet-da\') # you can also choose which model to use >>> source = ["Dem Feuer konnte Einhalt geboten werden", "Schulen und Kindergärten wurden eröffnet."] >>> hypothesis = ["The fire could be stopped", "Schools and kindergartens were open"] >>> reference = ["They were able to control the fire.", "Schools and kindergartens opened"] >>> results = comet_metric.compute(predictions=hypothesis, references=reference, sources=source) >>> print([round(v, 2) for v in results["scores"]]) [0.19, 0.92] ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION) class lowerCamelCase__ ( datasets.Metric): def __A (self ) -> Optional[int]: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , homepage='''https://unbabel.github.io/COMET/html/index.html''' , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''sources''': datasets.Value('''string''' , id='''sequence''' ), '''predictions''': datasets.Value('''string''' , id='''sequence''' ), '''references''': datasets.Value('''string''' , id='''sequence''' ), } ) , codebase_urls=['''https://github.com/Unbabel/COMET'''] , reference_urls=[ '''https://github.com/Unbabel/COMET''', '''https://www.aclweb.org/anthology/2020.emnlp-main.213/''', '''http://www.statmt.org/wmt20/pdf/2020.wmt-1.101.pdf6''', ] , ) def __A (self , UpperCAmelCase ) -> Dict: if self.config_name == "default": _lowercase =comet.load_from_checkpoint(comet.download_model('''wmt20-comet-da''' ) ) else: _lowercase =comet.load_from_checkpoint(comet.download_model(self.config_name ) ) def __A (self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase=None , UpperCAmelCase=False ) -> int: if gpus is None: _lowercase =1 if torch.cuda.is_available() else 0 _lowercase ={'''src''': sources, '''mt''': predictions, '''ref''': references} _lowercase =[dict(zip(UpperCAmelCase , UpperCAmelCase ) ) for t in zip(*data.values() )] _lowercase , _lowercase =self.scorer.predict(UpperCAmelCase , gpus=UpperCAmelCase , progress_bar=UpperCAmelCase ) return {"mean_score": mean_score, "scores": scores}
5
from typing import Optional from torch import nn from .transformer_ad import TransformeraDModel, TransformeraDModelOutput class lowerCamelCase__ ( nn.Module): def __init__(self , UpperCAmelCase = 1_6 , UpperCAmelCase = 8_8 , UpperCAmelCase = None , UpperCAmelCase = 1 , UpperCAmelCase = 0.0 , UpperCAmelCase = 3_2 , UpperCAmelCase = None , UpperCAmelCase = False , UpperCAmelCase = None , UpperCAmelCase = None , UpperCAmelCase = "geglu" , UpperCAmelCase = None , ) -> Any: super().__init__() _lowercase =nn.ModuleList( [ TransformeraDModel( num_attention_heads=UpperCAmelCase , attention_head_dim=UpperCAmelCase , in_channels=UpperCAmelCase , num_layers=UpperCAmelCase , dropout=UpperCAmelCase , norm_num_groups=UpperCAmelCase , cross_attention_dim=UpperCAmelCase , attention_bias=UpperCAmelCase , sample_size=UpperCAmelCase , num_vector_embeds=UpperCAmelCase , activation_fn=UpperCAmelCase , num_embeds_ada_norm=UpperCAmelCase , ) for _ in range(2 ) ] ) # Variables that can be set by a pipeline: # The ratio of transformer1 to transformer2's output states to be combined during inference _lowercase =0.5 # The shape of `encoder_hidden_states` is expected to be # `(batch_size, condition_lengths[0]+condition_lengths[1], num_features)` _lowercase =[7_7, 2_5_7] # Which transformer to use to encode which condition. # E.g. `(1, 0)` means that we'll use `transformers[1](conditions[0])` and `transformers[0](conditions[1])` _lowercase =[1, 0] def __A (self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase=None , UpperCAmelCase=None , UpperCAmelCase=None , UpperCAmelCase = True , ) -> str: _lowercase =hidden_states _lowercase =[] _lowercase =0 # attention_mask is not used yet for i in range(2 ): # for each of the two transformers, pass the corresponding condition tokens _lowercase =encoder_hidden_states[:, tokens_start : tokens_start + self.condition_lengths[i]] _lowercase =self.transformer_index_for_condition[i] _lowercase =self.transformers[transformer_index]( UpperCAmelCase , encoder_hidden_states=UpperCAmelCase , timestep=UpperCAmelCase , cross_attention_kwargs=UpperCAmelCase , return_dict=UpperCAmelCase , )[0] encoded_states.append(encoded_state - input_states ) tokens_start += self.condition_lengths[i] _lowercase =encoded_states[0] * self.mix_ratio + encoded_states[1] * (1 - self.mix_ratio) _lowercase =output_states + input_states if not return_dict: return (output_states,) return TransformeraDModelOutput(sample=UpperCAmelCase )
5
1
import math import random from typing import Any from .hill_climbing import SearchProblem def UpperCAmelCase_ ( __snake_case , __snake_case = True , __snake_case = math.inf , __snake_case = -math.inf , __snake_case = math.inf , __snake_case = -math.inf , __snake_case = False , __snake_case = 100 , __snake_case = 0.01 , __snake_case = 1 , ) -> Any: """simple docstring""" _lowercase =False _lowercase =search_prob _lowercase =start_temperate _lowercase =[] _lowercase =0 _lowercase =None while not search_end: _lowercase =current_state.score() if best_state is None or current_score > best_state.score(): _lowercase =current_state scores.append(__snake_case ) iterations += 1 _lowercase =None _lowercase =current_state.get_neighbors() while ( next_state is None and neighbors ): # till we do not find a neighbor that we can move to _lowercase =random.randint(0 , len(__snake_case ) - 1 ) # picking a random neighbor _lowercase =neighbors.pop(__snake_case ) _lowercase =picked_neighbor.score() - current_score if ( picked_neighbor.x > max_x or picked_neighbor.x < min_x or picked_neighbor.y > max_y or picked_neighbor.y < min_y ): continue # neighbor outside our bounds if not find_max: _lowercase =change * -1 # in case we are finding minimum if change > 0: # improves the solution _lowercase =picked_neighbor else: _lowercase =(math.e) ** ( change / current_temp ) # probability generation function if random.random() < probability: # random number within probability _lowercase =picked_neighbor _lowercase =current_temp - (current_temp * rate_of_decrease) if current_temp < threshold_temp or next_state is None: # temperature below threshold, or could not find a suitable neighbor _lowercase =True else: _lowercase =next_state if visualization: from matplotlib import pyplot as plt plt.plot(range(__snake_case ) , __snake_case ) plt.xlabel('''Iterations''' ) plt.ylabel('''Function values''' ) plt.show() return best_state if __name__ == "__main__": def UpperCAmelCase_ ( __snake_case , __snake_case ) -> Optional[Any]: """simple docstring""" return (x**2) + (y**2) # starting the problem with initial coordinates (12, 47) UpperCAmelCase__ = SearchProblem(x=12, y=47, step_size=1, function_to_optimize=test_fa) UpperCAmelCase__ = simulated_annealing( prob, find_max=False, max_x=100, min_x=5, max_y=50, min_y=-5, visualization=True ) print( '''The minimum score for f(x, y) = x^2 + y^2 with the domain 100 > x > 5 ''' f'''and 50 > y > - 5 found via hill climbing: {local_min.score()}''' ) # starting the problem with initial coordinates (12, 47) UpperCAmelCase__ = SearchProblem(x=12, y=47, step_size=1, function_to_optimize=test_fa) UpperCAmelCase__ = simulated_annealing( prob, find_max=True, max_x=100, min_x=5, max_y=50, min_y=-5, visualization=True ) print( '''The maximum score for f(x, y) = x^2 + y^2 with the domain 100 > x > 5 ''' f'''and 50 > y > - 5 found via hill climbing: {local_min.score()}''' ) def UpperCAmelCase_ ( __snake_case , __snake_case ) -> List[str]: """simple docstring""" return (3 * x**2) - (6 * y) UpperCAmelCase__ = SearchProblem(x=3, y=4, step_size=1, function_to_optimize=test_fa) UpperCAmelCase__ = simulated_annealing(prob, find_max=False, visualization=True) print( '''The minimum score for f(x, y) = 3*x^2 - 6*y found via hill climbing: ''' f'''{local_min.score()}''' ) UpperCAmelCase__ = SearchProblem(x=3, y=4, step_size=1, function_to_optimize=test_fa) UpperCAmelCase__ = simulated_annealing(prob, find_max=True, visualization=True) print( '''The maximum score for f(x, y) = 3*x^2 - 6*y found via hill climbing: ''' f'''{local_min.score()}''' )
5
import heapq as hq import math from collections.abc import Iterator class lowerCamelCase__ : def __init__(self , UpperCAmelCase ) -> Any: _lowercase =str(id_ ) _lowercase =None _lowercase =None _lowercase =[] _lowercase ={} # {vertex:distance} def __lt__(self , UpperCAmelCase ) -> List[str]: return self.key < other.key def __repr__(self ) -> str: return self.id def __A (self , UpperCAmelCase ) -> Dict: self.neighbors.append(UpperCAmelCase ) def __A (self , UpperCAmelCase , UpperCAmelCase ) -> Optional[Any]: _lowercase =weight def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case ) -> List[str]: """simple docstring""" graph[a - 1].add_neighbor(graph[b - 1] ) graph[b - 1].add_neighbor(graph[a - 1] ) # add the edges: graph[a - 1].add_edge(graph[b - 1] , __snake_case ) graph[b - 1].add_edge(graph[a - 1] , __snake_case ) def UpperCAmelCase_ ( __snake_case , __snake_case ) -> list: """simple docstring""" _lowercase =[] for u in graph: _lowercase =math.inf _lowercase =None _lowercase =0 _lowercase =graph[:] while q: _lowercase =min(__snake_case ) q.remove(__snake_case ) for v in u.neighbors: if (v in q) and (u.edges[v.id] < v.key): _lowercase =u _lowercase =u.edges[v.id] for i in range(1 , len(__snake_case ) ): a.append((int(graph[i].id ) + 1, int(graph[i].pi.id ) + 1) ) return a def UpperCAmelCase_ ( __snake_case , __snake_case ) -> Iterator[tuple]: """simple docstring""" for u in graph: _lowercase =math.inf _lowercase =None _lowercase =0 _lowercase =list(__snake_case ) hq.heapify(__snake_case ) while h: _lowercase =hq.heappop(__snake_case ) for v in u.neighbors: if (v in h) and (u.edges[v.id] < v.key): _lowercase =u _lowercase =u.edges[v.id] hq.heapify(__snake_case ) for i in range(1 , len(__snake_case ) ): yield (int(graph[i].id ) + 1, int(graph[i].pi.id ) + 1) def UpperCAmelCase_ ( ) -> None: """simple docstring""" if __name__ == "__main__": import doctest doctest.testmod()
5
1
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( MobileViTConfig, MobileViTForImageClassification, MobileViTForSemanticSegmentation, MobileViTImageProcessor, ) from transformers.utils import logging logging.set_verbosity_info() UpperCAmelCase__ = logging.get_logger(__name__) def UpperCAmelCase_ ( __snake_case ) -> Optional[Any]: """simple docstring""" _lowercase =MobileViTConfig() # size of the architecture if "mobilevit_s" in mobilevit_name: _lowercase =[144, 192, 240] _lowercase =[16, 32, 64, 96, 128, 160, 640] elif "mobilevit_xs" in mobilevit_name: _lowercase =[96, 120, 144] _lowercase =[16, 32, 48, 64, 80, 96, 384] elif "mobilevit_xxs" in mobilevit_name: _lowercase =[64, 80, 96] _lowercase =[16, 16, 24, 48, 64, 80, 320] _lowercase =0.05 _lowercase =2.0 if mobilevit_name.startswith('''deeplabv3_''' ): _lowercase =512 _lowercase =16 _lowercase =21 _lowercase ='''pascal-voc-id2label.json''' else: _lowercase =1000 _lowercase ='''imagenet-1k-id2label.json''' _lowercase ='''huggingface/label-files''' _lowercase =json.load(open(hf_hub_download(__snake_case , __snake_case , repo_type='''dataset''' ) , '''r''' ) ) _lowercase ={int(__snake_case ): v for k, v in idalabel.items()} _lowercase =idalabel _lowercase ={v: k for k, v in idalabel.items()} return config def UpperCAmelCase_ ( __snake_case , __snake_case=False ) -> Tuple: """simple docstring""" for i in range(1 , 6 ): if F"layer_{i}." in name: _lowercase =name.replace(F"layer_{i}." , F"encoder.layer.{i - 1}." ) if "conv_1." in name: _lowercase =name.replace('''conv_1.''' , '''conv_stem.''' ) if ".block." in name: _lowercase =name.replace('''.block.''' , '''.''' ) if "exp_1x1" in name: _lowercase =name.replace('''exp_1x1''' , '''expand_1x1''' ) if "red_1x1" in name: _lowercase =name.replace('''red_1x1''' , '''reduce_1x1''' ) if ".local_rep.conv_3x3." in name: _lowercase =name.replace('''.local_rep.conv_3x3.''' , '''.conv_kxk.''' ) if ".local_rep.conv_1x1." in name: _lowercase =name.replace('''.local_rep.conv_1x1.''' , '''.conv_1x1.''' ) if ".norm." in name: _lowercase =name.replace('''.norm.''' , '''.normalization.''' ) if ".conv." in name: _lowercase =name.replace('''.conv.''' , '''.convolution.''' ) if ".conv_proj." in name: _lowercase =name.replace('''.conv_proj.''' , '''.conv_projection.''' ) for i in range(0 , 2 ): for j in range(0 , 4 ): if F".{i}.{j}." in name: _lowercase =name.replace(F".{i}.{j}." , F".{i}.layer.{j}." ) for i in range(2 , 6 ): for j in range(0 , 4 ): if F".{i}.{j}." in name: _lowercase =name.replace(F".{i}.{j}." , F".{i}." ) if "expand_1x1" in name: _lowercase =name.replace('''expand_1x1''' , '''downsampling_layer.expand_1x1''' ) if "conv_3x3" in name: _lowercase =name.replace('''conv_3x3''' , '''downsampling_layer.conv_3x3''' ) if "reduce_1x1" in name: _lowercase =name.replace('''reduce_1x1''' , '''downsampling_layer.reduce_1x1''' ) for i in range(2 , 5 ): if F".global_rep.{i}.weight" in name: _lowercase =name.replace(F".global_rep.{i}.weight" , '''.layernorm.weight''' ) if F".global_rep.{i}.bias" in name: _lowercase =name.replace(F".global_rep.{i}.bias" , '''.layernorm.bias''' ) if ".global_rep." in name: _lowercase =name.replace('''.global_rep.''' , '''.transformer.''' ) if ".pre_norm_mha.0." in name: _lowercase =name.replace('''.pre_norm_mha.0.''' , '''.layernorm_before.''' ) if ".pre_norm_mha.1.out_proj." in name: _lowercase =name.replace('''.pre_norm_mha.1.out_proj.''' , '''.attention.output.dense.''' ) if ".pre_norm_ffn.0." in name: _lowercase =name.replace('''.pre_norm_ffn.0.''' , '''.layernorm_after.''' ) if ".pre_norm_ffn.1." in name: _lowercase =name.replace('''.pre_norm_ffn.1.''' , '''.intermediate.dense.''' ) if ".pre_norm_ffn.4." in name: _lowercase =name.replace('''.pre_norm_ffn.4.''' , '''.output.dense.''' ) if ".transformer." in name: _lowercase =name.replace('''.transformer.''' , '''.transformer.layer.''' ) if ".aspp_layer." in name: _lowercase =name.replace('''.aspp_layer.''' , '''.''' ) if ".aspp_pool." in name: _lowercase =name.replace('''.aspp_pool.''' , '''.''' ) if "seg_head." in name: _lowercase =name.replace('''seg_head.''' , '''segmentation_head.''' ) if "segmentation_head.classifier.classifier." in name: _lowercase =name.replace('''segmentation_head.classifier.classifier.''' , '''segmentation_head.classifier.''' ) if "classifier.fc." in name: _lowercase =name.replace('''classifier.fc.''' , '''classifier.''' ) elif (not base_model) and ("segmentation_head." not in name): _lowercase ='''mobilevit.''' + name return name def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case=False ) -> Optional[Any]: """simple docstring""" if base_model: _lowercase ='''''' else: _lowercase ='''mobilevit.''' for key in orig_state_dict.copy().keys(): _lowercase =orig_state_dict.pop(__snake_case ) if key[:8] == "encoder.": _lowercase =key[8:] if "qkv" in key: _lowercase =key.split('''.''' ) _lowercase =int(key_split[0][6:] ) - 1 _lowercase =int(key_split[3] ) _lowercase =model.get_submodule(F"{model_prefix}encoder.layer.{layer_num}" ) _lowercase =layer.transformer.layer[transformer_num].attention.attention.all_head_size _lowercase =( F"{model_prefix}encoder.layer.{layer_num}.transformer.layer.{transformer_num}.attention.attention." ) if "weight" in key: _lowercase =val[:dim, :] _lowercase =val[dim : dim * 2, :] _lowercase =val[-dim:, :] else: _lowercase =val[:dim] _lowercase =val[dim : dim * 2] _lowercase =val[-dim:] else: _lowercase =val return orig_state_dict def UpperCAmelCase_ ( ) -> Union[str, Any]: """simple docstring""" _lowercase ='''http://images.cocodataset.org/val2017/000000039769.jpg''' _lowercase =Image.open(requests.get(__snake_case , stream=__snake_case ).raw ) return im @torch.no_grad() def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case=False ) -> int: """simple docstring""" _lowercase =get_mobilevit_config(__snake_case ) # load original state_dict _lowercase =torch.load(__snake_case , map_location='''cpu''' ) # load 🤗 model if mobilevit_name.startswith('''deeplabv3_''' ): _lowercase =MobileViTForSemanticSegmentation(__snake_case ).eval() else: _lowercase =MobileViTForImageClassification(__snake_case ).eval() _lowercase =convert_state_dict(__snake_case , __snake_case ) model.load_state_dict(__snake_case ) # Check outputs on an image, prepared by MobileViTImageProcessor _lowercase =MobileViTImageProcessor(crop_size=config.image_size , size=config.image_size + 32 ) _lowercase =image_processor(images=prepare_img() , return_tensors='''pt''' ) _lowercase =model(**__snake_case ) _lowercase =outputs.logits if mobilevit_name.startswith('''deeplabv3_''' ): assert logits.shape == (1, 21, 32, 32) if mobilevit_name == "deeplabv3_mobilevit_s": _lowercase =torch.tensor( [ [[6.20_65, 6.12_92, 6.20_70], [6.10_79, 6.12_54, 6.17_47], [6.00_42, 6.10_71, 6.10_34]], [[-6.92_53, -6.86_53, -7.03_98], [-7.32_18, -7.39_83, -7.36_70], [-7.19_61, -7.24_82, -7.15_69]], [[-4.47_23, -4.43_48, -4.37_69], [-5.36_29, -5.46_32, -5.45_98], [-5.15_87, -5.34_02, -5.50_59]], ] ) elif mobilevit_name == "deeplabv3_mobilevit_xs": _lowercase =torch.tensor( [ [[5.44_49, 5.57_33, 5.63_14], [5.18_15, 5.39_30, 5.59_63], [5.16_56, 5.43_33, 5.48_53]], [[-9.44_23, -9.77_66, -9.67_14], [-9.15_81, -9.57_20, -9.55_19], [-9.10_06, -9.64_58, -9.57_03]], [[-7.77_21, -7.37_16, -7.15_83], [-8.45_99, -8.06_24, -7.79_44], [-8.41_72, -7.83_66, -7.50_25]], ] ) elif mobilevit_name == "deeplabv3_mobilevit_xxs": _lowercase =torch.tensor( [ [[6.98_11, 6.97_43, 7.31_23], [7.17_77, 7.19_31, 7.39_38], [7.56_33, 7.80_50, 7.89_01]], [[-10.55_36, -10.23_32, -10.29_24], [-10.23_36, -9.86_24, -9.59_64], [-10.88_40, -10.81_58, -10.66_59]], [[-3.49_38, -3.06_31, -2.86_20], [-3.42_05, -2.81_35, -2.68_75], [-3.41_79, -2.79_45, -2.87_50]], ] ) else: raise ValueError(F"Unknown mobilevit_name: {mobilevit_name}" ) assert torch.allclose(logits[0, :3, :3, :3] , __snake_case , atol=1e-4 ) else: assert logits.shape == (1, 1000) if mobilevit_name == "mobilevit_s": _lowercase =torch.tensor([-0.98_66, 0.23_92, -1.12_41] ) elif mobilevit_name == "mobilevit_xs": _lowercase =torch.tensor([-2.47_61, -0.93_99, -1.95_87] ) elif mobilevit_name == "mobilevit_xxs": _lowercase =torch.tensor([-1.93_64, -1.23_27, -0.46_53] ) else: raise ValueError(F"Unknown mobilevit_name: {mobilevit_name}" ) assert torch.allclose(logits[0, :3] , __snake_case , atol=1e-4 ) Path(__snake_case ).mkdir(exist_ok=__snake_case ) print(F"Saving model {mobilevit_name} to {pytorch_dump_folder_path}" ) model.save_pretrained(__snake_case ) print(F"Saving image processor to {pytorch_dump_folder_path}" ) image_processor.save_pretrained(__snake_case ) if push_to_hub: _lowercase ={ '''mobilevit_s''': '''mobilevit-small''', '''mobilevit_xs''': '''mobilevit-x-small''', '''mobilevit_xxs''': '''mobilevit-xx-small''', '''deeplabv3_mobilevit_s''': '''deeplabv3-mobilevit-small''', '''deeplabv3_mobilevit_xs''': '''deeplabv3-mobilevit-x-small''', '''deeplabv3_mobilevit_xxs''': '''deeplabv3-mobilevit-xx-small''', } print('''Pushing to the hub...''' ) _lowercase =model_mapping[mobilevit_name] image_processor.push_to_hub(__snake_case , organization='''apple''' ) model.push_to_hub(__snake_case , organization='''apple''' ) if __name__ == "__main__": UpperCAmelCase__ = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--mobilevit_name''', default='''mobilevit_s''', type=str, help=( '''Name of the MobileViT model you\'d like to convert. Should be one of \'mobilevit_s\', \'mobilevit_xs\',''' ''' \'mobilevit_xxs\', \'deeplabv3_mobilevit_s\', \'deeplabv3_mobilevit_xs\', \'deeplabv3_mobilevit_xxs\'.''' ), ) parser.add_argument( '''--checkpoint_path''', required=True, type=str, help='''Path to the original state dict (.pt 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.mobilevit_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub )
5
# flake8: noqa # Lint as: python3 UpperCAmelCase__ = [ '''VerificationMode''', '''Version''', '''disable_progress_bar''', '''enable_progress_bar''', '''is_progress_bar_enabled''', '''experimental''', ] from .info_utils import VerificationMode from .logging import disable_progress_bar, enable_progress_bar, is_progress_bar_enabled from .version import Version from .experimental import experimental
5
1
import json import os import unittest from transformers.models.ctrl.tokenization_ctrl import VOCAB_FILES_NAMES, CTRLTokenizer from ...test_tokenization_common import TokenizerTesterMixin class lowerCamelCase__ ( lowerCAmelCase , unittest.TestCase): SCREAMING_SNAKE_CASE__ = CTRLTokenizer SCREAMING_SNAKE_CASE__ = False SCREAMING_SNAKE_CASE__ = False def __A (self ) -> int: super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt _lowercase =['''adapt''', '''re@@''', '''a@@''', '''apt''', '''c@@''', '''t''', '''<unk>'''] _lowercase =dict(zip(UpperCAmelCase , range(len(UpperCAmelCase ) ) ) ) _lowercase =['''#version: 0.2''', '''a p''', '''ap t</w>''', '''r e''', '''a d''', '''ad apt</w>''', ''''''] _lowercase ={'''unk_token''': '''<unk>'''} _lowercase =os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) _lowercase =os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''merges_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(UpperCAmelCase ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(UpperCAmelCase ) ) def __A (self , **UpperCAmelCase ) -> str: kwargs.update(self.special_tokens_map ) return CTRLTokenizer.from_pretrained(self.tmpdirname , **UpperCAmelCase ) def __A (self , UpperCAmelCase ) -> Union[str, Any]: _lowercase ='''adapt react readapt apt''' _lowercase ='''adapt react readapt apt''' return input_text, output_text def __A (self ) -> Dict: _lowercase =CTRLTokenizer(self.vocab_file , self.merges_file , **self.special_tokens_map ) _lowercase ='''adapt react readapt apt''' _lowercase ='''adapt re@@ a@@ c@@ t re@@ adapt apt'''.split() _lowercase =tokenizer.tokenize(UpperCAmelCase ) self.assertListEqual(UpperCAmelCase , UpperCAmelCase ) _lowercase =tokens + [tokenizer.unk_token] _lowercase =[0, 1, 2, 4, 5, 1, 0, 3, 6] self.assertListEqual(tokenizer.convert_tokens_to_ids(UpperCAmelCase ) , UpperCAmelCase )
5
import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = { '''microsoft/wavlm-base''': '''https://huggingface.co/microsoft/wavlm-base/resolve/main/config.json''', # See all WavLM models at https://huggingface.co/models?filter=wavlm } class lowerCamelCase__ ( lowerCAmelCase): SCREAMING_SNAKE_CASE__ = '''wavlm''' def __init__(self , UpperCAmelCase=3_2 , UpperCAmelCase=7_6_8 , UpperCAmelCase=1_2 , UpperCAmelCase=1_2 , UpperCAmelCase=3_0_7_2 , UpperCAmelCase="gelu" , UpperCAmelCase=0.1 , UpperCAmelCase=0.1 , UpperCAmelCase=0.1 , UpperCAmelCase=0.0 , UpperCAmelCase=0.1 , UpperCAmelCase=0.1 , UpperCAmelCase=0.02 , UpperCAmelCase=1e-5 , UpperCAmelCase="group" , UpperCAmelCase="gelu" , UpperCAmelCase=(5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2) , UpperCAmelCase=(5, 2, 2, 2, 2, 2, 2) , UpperCAmelCase=(1_0, 3, 3, 3, 3, 2, 2) , UpperCAmelCase=False , UpperCAmelCase=1_2_8 , UpperCAmelCase=1_6 , UpperCAmelCase=3_2_0 , UpperCAmelCase=8_0_0 , UpperCAmelCase=False , UpperCAmelCase=True , UpperCAmelCase=0.05 , UpperCAmelCase=1_0 , UpperCAmelCase=2 , UpperCAmelCase=0.0 , UpperCAmelCase=1_0 , UpperCAmelCase=3_2_0 , UpperCAmelCase=2 , UpperCAmelCase=0.1 , UpperCAmelCase=1_0_0 , UpperCAmelCase=2_5_6 , UpperCAmelCase=2_5_6 , UpperCAmelCase=0.1 , UpperCAmelCase="mean" , UpperCAmelCase=False , UpperCAmelCase=False , UpperCAmelCase=2_5_6 , UpperCAmelCase=(5_1_2, 5_1_2, 5_1_2, 5_1_2, 1_5_0_0) , UpperCAmelCase=(5, 3, 3, 1, 1) , UpperCAmelCase=(1, 2, 3, 1, 1) , UpperCAmelCase=5_1_2 , UpperCAmelCase=8_0 , UpperCAmelCase=0 , UpperCAmelCase=1 , UpperCAmelCase=2 , UpperCAmelCase=False , UpperCAmelCase=3 , UpperCAmelCase=2 , UpperCAmelCase=3 , UpperCAmelCase=None , **UpperCAmelCase , ) -> Optional[Any]: super().__init__(**UpperCAmelCase , pad_token_id=UpperCAmelCase , bos_token_id=UpperCAmelCase , eos_token_id=UpperCAmelCase ) _lowercase =hidden_size _lowercase =feat_extract_norm _lowercase =feat_extract_activation _lowercase =list(UpperCAmelCase ) _lowercase =list(UpperCAmelCase ) _lowercase =list(UpperCAmelCase ) _lowercase =conv_bias _lowercase =num_buckets _lowercase =max_bucket_distance _lowercase =num_conv_pos_embeddings _lowercase =num_conv_pos_embedding_groups _lowercase =len(self.conv_dim ) _lowercase =num_hidden_layers _lowercase =intermediate_size _lowercase =hidden_act _lowercase =num_attention_heads _lowercase =hidden_dropout _lowercase =attention_dropout _lowercase =activation_dropout _lowercase =feat_proj_dropout _lowercase =final_dropout _lowercase =layerdrop _lowercase =layer_norm_eps _lowercase =initializer_range _lowercase =num_ctc_classes _lowercase =vocab_size _lowercase =do_stable_layer_norm _lowercase =use_weighted_layer_sum _lowercase =classifier_proj_size if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( '''Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` ==''' ''' `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) =''' f" {len(self.conv_dim )}`, `len(config.conv_stride) = {len(self.conv_stride )}`," f" `len(config.conv_kernel) = {len(self.conv_kernel )}`." ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 _lowercase =apply_spec_augment _lowercase =mask_time_prob _lowercase =mask_time_length _lowercase =mask_time_min_masks _lowercase =mask_feature_prob _lowercase =mask_feature_length # parameters for pretraining with codevector quantized representations _lowercase =num_codevectors_per_group _lowercase =num_codevector_groups _lowercase =contrastive_logits_temperature _lowercase =num_negatives _lowercase =codevector_dim _lowercase =proj_codevector_dim _lowercase =diversity_loss_weight # ctc loss _lowercase =ctc_loss_reduction _lowercase =ctc_zero_infinity # adapter _lowercase =add_adapter _lowercase =adapter_kernel_size _lowercase =adapter_stride _lowercase =num_adapter_layers _lowercase =output_hidden_size or hidden_size # SequenceClassification-specific parameter. Feel free to ignore for other classes. _lowercase =classifier_proj_size # XVector-specific parameters. Feel free to ignore for other classes. _lowercase =list(UpperCAmelCase ) _lowercase =list(UpperCAmelCase ) _lowercase =list(UpperCAmelCase ) _lowercase =xvector_output_dim @property def __A (self ) -> int: return functools.reduce(operator.mul , self.conv_stride , 1 )
5
1
from __future__ import annotations from collections import namedtuple def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case ) -> tuple: """simple docstring""" _lowercase =namedtuple('''result''' , '''name value''' ) if (voltage, current, power).count(0 ) != 1: raise ValueError('''Only one argument must be 0''' ) elif power < 0: raise ValueError( '''Power cannot be negative in any electrical/electronics system''' ) elif voltage == 0: return result('''voltage''' , power / current ) elif current == 0: return result('''current''' , power / voltage ) elif power == 0: return result('''power''' , float(round(abs(voltage * current ) , 2 ) ) ) else: raise ValueError('''Exactly one argument must be 0''' ) if __name__ == "__main__": import doctest doctest.testmod()
5
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): def __A (self ) -> List[Any]: # clean up the VRAM after each test super().tearDown() gc.collect() def __A (self ) -> Optional[Any]: _lowercase =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/sd2-inpaint/init_image.png''' ) _lowercase =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png''' ) _lowercase ='''xvjiarui/stable-diffusion-2-inpainting''' _lowercase , _lowercase =FlaxStableDiffusionInpaintPipeline.from_pretrained(UpperCAmelCase , safety_checker=UpperCAmelCase ) _lowercase ='''Face of a yellow cat, high resolution, sitting on a park bench''' _lowercase =jax.random.PRNGKey(0 ) _lowercase =5_0 _lowercase =jax.device_count() _lowercase =num_samples * [prompt] _lowercase =num_samples * [init_image] _lowercase =num_samples * [mask_image] _lowercase , _lowercase , _lowercase =pipeline.prepare_inputs(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) # shard inputs and rng _lowercase =replicate(UpperCAmelCase ) _lowercase =jax.random.split(UpperCAmelCase , jax.device_count() ) _lowercase =shard(UpperCAmelCase ) _lowercase =shard(UpperCAmelCase ) _lowercase =shard(UpperCAmelCase ) _lowercase =pipeline( UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , jit=UpperCAmelCase ) _lowercase =output.images.reshape(UpperCAmelCase , 5_1_2 , 5_1_2 , 3 ) _lowercase =images[0, 2_5_3:2_5_6, 2_5_3:2_5_6, -1] _lowercase =jnp.asarray(jax.device_get(image_slice.flatten() ) ) _lowercase =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
5
1
import requests from bsa import BeautifulSoup def UpperCAmelCase_ ( __snake_case = "AAPL" ) -> str: """simple docstring""" _lowercase =F"https://in.finance.yahoo.com/quote/{symbol}?s={symbol}" _lowercase =BeautifulSoup(requests.get(__snake_case ).text , '''html.parser''' ) _lowercase ='''My(6px) Pos(r) smartphone_Mt(6px)''' return soup.find('''div''' , class_=class_ ).find('''span''' ).text if __name__ == "__main__": for symbol in "AAPL AMZN IBM GOOG MSFT ORCL".split(): print(f'''Current {symbol:<4} stock price is {stock_price(symbol):>8}''')
5
import comet # From: unbabel-comet import torch import datasets UpperCAmelCase__ = datasets.logging.get_logger(__name__) UpperCAmelCase__ = '''\ @inproceedings{rei-EtAl:2020:WMT, author = {Rei, Ricardo and Stewart, Craig and Farinha, Ana C and Lavie, Alon}, title = {Unbabel\'s Participation in the WMT20 Metrics Shared Task}, booktitle = {Proceedings of the Fifth Conference on Machine Translation}, month = {November}, year = {2020}, address = {Online}, publisher = {Association for Computational Linguistics}, pages = {909--918}, } @inproceedings{rei-etal-2020-comet, title = "{COMET}: A Neural Framework for {MT} Evaluation", author = "Rei, Ricardo and Stewart, Craig and Farinha, Ana C and Lavie, Alon", booktitle = "Proceedings of the 2020 Conference on Empirical Methods in Natural Language Processing (EMNLP)", month = nov, year = "2020", address = "Online", publisher = "Association for Computational Linguistics", url = "https://www.aclweb.org/anthology/2020.emnlp-main.213", pages = "2685--2702", } ''' UpperCAmelCase__ = '''\ Crosslingual Optimized Metric for Evaluation of Translation (COMET) is an open-source framework used to train Machine Translation metrics that achieve high levels of correlation with different types of human judgments (HTER, DA\'s or MQM). With the release of the framework the authors also released fully trained models that were used to compete in the WMT20 Metrics Shared Task achieving SOTA in that years competition. See the [README.md] file at https://unbabel.github.io/COMET/html/models.html for more information. ''' UpperCAmelCase__ = ''' COMET score. Args: `sources` (list of str): Source sentences `predictions` (list of str): candidate translations `references` (list of str): reference translations `cuda` (bool): If set to True, runs COMET using GPU `show_progress` (bool): Shows progress `model`: COMET model to be used. Will default to `wmt-large-da-estimator-1719` if None. Returns: `samples`: List of dictionaries with `src`, `mt`, `ref` and `score`. `scores`: List of scores. Examples: >>> comet_metric = datasets.load_metric(\'comet\') >>> # comet_metric = load_metric(\'comet\', \'wmt20-comet-da\') # you can also choose which model to use >>> source = ["Dem Feuer konnte Einhalt geboten werden", "Schulen und Kindergärten wurden eröffnet."] >>> hypothesis = ["The fire could be stopped", "Schools and kindergartens were open"] >>> reference = ["They were able to control the fire.", "Schools and kindergartens opened"] >>> results = comet_metric.compute(predictions=hypothesis, references=reference, sources=source) >>> print([round(v, 2) for v in results["scores"]]) [0.19, 0.92] ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION) class lowerCamelCase__ ( datasets.Metric): def __A (self ) -> Optional[int]: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , homepage='''https://unbabel.github.io/COMET/html/index.html''' , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''sources''': datasets.Value('''string''' , id='''sequence''' ), '''predictions''': datasets.Value('''string''' , id='''sequence''' ), '''references''': datasets.Value('''string''' , id='''sequence''' ), } ) , codebase_urls=['''https://github.com/Unbabel/COMET'''] , reference_urls=[ '''https://github.com/Unbabel/COMET''', '''https://www.aclweb.org/anthology/2020.emnlp-main.213/''', '''http://www.statmt.org/wmt20/pdf/2020.wmt-1.101.pdf6''', ] , ) def __A (self , UpperCAmelCase ) -> Dict: if self.config_name == "default": _lowercase =comet.load_from_checkpoint(comet.download_model('''wmt20-comet-da''' ) ) else: _lowercase =comet.load_from_checkpoint(comet.download_model(self.config_name ) ) def __A (self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase=None , UpperCAmelCase=False ) -> int: if gpus is None: _lowercase =1 if torch.cuda.is_available() else 0 _lowercase ={'''src''': sources, '''mt''': predictions, '''ref''': references} _lowercase =[dict(zip(UpperCAmelCase , UpperCAmelCase ) ) for t in zip(*data.values() )] _lowercase , _lowercase =self.scorer.predict(UpperCAmelCase , gpus=UpperCAmelCase , progress_bar=UpperCAmelCase ) return {"mean_score": mean_score, "scores": scores}
5
1
import argparse import json import os import fairseq import torch from fairseq.data import Dictionary from transformers import ( WavaVecaConformerConfig, WavaVecaConformerForCTC, WavaVecaConformerForPreTraining, WavaVecaCTCTokenizer, WavaVecaFeatureExtractor, WavaVecaProcessor, logging, ) logging.set_verbosity_info() UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = { '''post_extract_proj''': '''feature_projection.projection''', '''encoder.pos_conv.0''': '''encoder.pos_conv_embed.conv''', '''self_attn.linear_k''': '''encoder.layers.*.self_attn.linear_k''', '''self_attn.linear_v''': '''encoder.layers.*.self_attn.linear_v''', '''self_attn.linear_q''': '''encoder.layers.*.self_attn.linear_q''', '''self_attn.pos_bias_u''': '''encoder.layers.*.self_attn.pos_bias_u''', '''self_attn.pos_bias_v''': '''encoder.layers.*.self_attn.pos_bias_v''', '''self_attn.linear_out''': '''encoder.layers.*.self_attn.linear_out''', '''self_attn.linear_pos''': '''encoder.layers.*.self_attn.linear_pos''', '''self_attn.rotary_emb''': '''encoder.embed_positions''', '''self_attn_layer_norm''': '''encoder.layers.*.self_attn_layer_norm''', '''conv_module.pointwise_conv1''': '''encoder.layers.*.conv_module.pointwise_conv1''', '''conv_module.pointwise_conv2''': '''encoder.layers.*.conv_module.pointwise_conv2''', '''conv_module.depthwise_conv''': '''encoder.layers.*.conv_module.depthwise_conv''', '''conv_module.batch_norm''': '''encoder.layers.*.conv_module.batch_norm''', '''conv_module.layer_norm''': '''encoder.layers.*.conv_module.layer_norm''', '''ffn1.w_1''': '''encoder.layers.*.ffn1.intermediate_dense''', '''ffn1.w_2''': '''encoder.layers.*.ffn1.output_dense''', '''ffn1.layer_norm''': '''encoder.layers.*.ffn1_layer_norm''', '''ffn2.w_1''': '''encoder.layers.*.ffn2.intermediate_dense''', '''ffn2.w_2''': '''encoder.layers.*.ffn2.output_dense''', '''ffn2.layer_norm''': '''encoder.layers.*.ffn2_layer_norm''', '''final_layer_norm''': '''encoder.layers.*.final_layer_norm''', '''encoder.layer_norm''': '''encoder.layer_norm''', '''w2v_model.layer_norm''': '''feature_projection.layer_norm''', '''quantizer.weight_proj''': '''quantizer.weight_proj''', '''quantizer.vars''': '''quantizer.codevectors''', '''project_q''': '''project_q''', '''final_proj''': '''project_hid''', '''w2v_encoder.proj''': '''lm_head''', '''mask_emb''': '''masked_spec_embed''', } UpperCAmelCase__ = [ '''lm_head''', '''quantizer.weight_proj''', '''quantizer.codevectors''', '''project_q''', '''project_hid''', ] def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case ) -> Dict: """simple docstring""" for attribute in key.split('''.''' ): _lowercase =getattr(__snake_case , __snake_case ) if weight_type is not None: _lowercase =getattr(__snake_case , __snake_case ).shape else: _lowercase =hf_pointer.shape if hf_shape != value.shape: raise ValueError( F"Shape of hf {key + '.' + weight_type if weight_type is not None else ''} is {hf_shape}, but should be" F" {value.shape} for {full_name}" ) if weight_type == "weight": _lowercase =value elif weight_type == "weight_g": _lowercase =value elif weight_type == "weight_v": _lowercase =value elif weight_type == "bias": _lowercase =value elif weight_type == "running_mean": _lowercase =value elif weight_type == "running_var": _lowercase =value elif weight_type == "num_batches_tracked": _lowercase =value elif weight_type == "inv_freq": _lowercase =value else: _lowercase =value logger.info(F"{key + '.' + weight_type if weight_type is not None else ''} was initialized from {full_name}." ) def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case ) -> List[str]: """simple docstring""" _lowercase =[] _lowercase =fairseq_model.state_dict() _lowercase =hf_model.wavaveca_conformer.feature_extractor for name, value in fairseq_dict.items(): _lowercase =False if "conv_layers" in name: load_conv_layer( __snake_case , __snake_case , __snake_case , __snake_case , hf_model.config.feat_extract_norm == '''group''' , ) _lowercase =True else: for key, mapped_key in MAPPING.items(): _lowercase ='''wav2vec2_conformer.''' + mapped_key if mapped_key not in TOP_LEVEL_KEYS else mapped_key if key in name or key.split('''w2v_model.''' )[-1] == name.split('''.''' )[0]: _lowercase =True if "*" in mapped_key: _lowercase =name.split(__snake_case )[0].split('''.''' )[-2] _lowercase =mapped_key.replace('''*''' , __snake_case ) if "pos_bias_u" in name: _lowercase =None elif "pos_bias_v" in name: _lowercase =None elif "weight_g" in name: _lowercase ='''weight_g''' elif "weight_v" in name: _lowercase ='''weight_v''' elif "bias" in name: _lowercase ='''bias''' elif "weight" in name: # TODO: don't match quantizer.weight_proj _lowercase ='''weight''' elif "running_mean" in name: _lowercase ='''running_mean''' elif "inv_freq" in name: _lowercase ='''inv_freq''' elif "running_var" in name: _lowercase ='''running_var''' elif "num_batches_tracked" in name: _lowercase ='''num_batches_tracked''' else: _lowercase =None set_recursively(__snake_case , __snake_case , __snake_case , __snake_case , __snake_case ) continue if not is_used: unused_weights.append(__snake_case ) logger.warning(F"Unused weights: {unused_weights}" ) def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case ) -> str: """simple docstring""" _lowercase =full_name.split('''conv_layers.''' )[-1] _lowercase =name.split('''.''' ) _lowercase =int(items[0] ) _lowercase =int(items[1] ) if type_id == 0: if "bias" in name: if value.shape != feature_extractor.conv_layers[layer_id].conv.bias.data.shape: raise ValueError( F"{full_name} has size {value.shape}, but" F" {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found." ) _lowercase =value logger.info(F"Feat extract conv layer {layer_id} was initialized from {full_name}." ) elif "weight" in name: if value.shape != feature_extractor.conv_layers[layer_id].conv.weight.data.shape: raise ValueError( F"{full_name} has size {value.shape}, but" F" {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found." ) _lowercase =value logger.info(F"Feat extract conv layer {layer_id} was initialized from {full_name}." ) elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm): if "bias" in name: if value.shape != feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape: raise ValueError( F"{full_name} has size {value.shape}, but" F" {feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape} was found." ) _lowercase =value logger.info(F"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}." ) elif "weight" in name: if value.shape != feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape: raise ValueError( F"{full_name} has size {value.shape}, but" F" {feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape} was found." ) _lowercase =value logger.info(F"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}." ) else: unused_weights.append(__snake_case ) @torch.no_grad() def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case=None , __snake_case=None , __snake_case=True ) -> str: """simple docstring""" if config_path is not None: _lowercase =WavaVecaConformerConfig.from_pretrained(__snake_case , hidden_act='''swish''' ) else: _lowercase =WavaVecaConformerConfig() if "rope" in checkpoint_path: _lowercase ='''rotary''' if is_finetuned: if dict_path: _lowercase =Dictionary.load(__snake_case ) # important change bos & pad token id since CTC symbol is <pad> and # not <s> as in fairseq _lowercase =target_dict.pad_index _lowercase =target_dict.bos_index _lowercase =target_dict.eos_index _lowercase =len(target_dict.symbols ) _lowercase =os.path.join(__snake_case , '''vocab.json''' ) if not os.path.isdir(__snake_case ): logger.error('''--pytorch_dump_folder_path ({}) should be a directory'''.format(__snake_case ) ) return os.makedirs(__snake_case , exist_ok=__snake_case ) _lowercase =target_dict.indices # fairseq has the <pad> and <s> switched _lowercase =0 _lowercase =1 with open(__snake_case , '''w''' , encoding='''utf-8''' ) as vocab_handle: json.dump(__snake_case , __snake_case ) _lowercase =WavaVecaCTCTokenizer( __snake_case , unk_token=target_dict.unk_word , pad_token=target_dict.pad_word , bos_token=target_dict.bos_word , eos_token=target_dict.eos_word , word_delimiter_token='''|''' , do_lower_case=__snake_case , ) _lowercase =True if config.feat_extract_norm == '''layer''' else False _lowercase =WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=16000 , padding_value=0 , do_normalize=__snake_case , return_attention_mask=__snake_case , ) _lowercase =WavaVecaProcessor(feature_extractor=__snake_case , tokenizer=__snake_case ) processor.save_pretrained(__snake_case ) _lowercase =WavaVecaConformerForCTC(__snake_case ) else: _lowercase =WavaVecaConformerForPreTraining(__snake_case ) if is_finetuned: _lowercase , _lowercase , _lowercase =fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] , arg_overrides={'''data''': '''/'''.join(dict_path.split('''/''' )[:-1] )} ) else: _lowercase =argparse.Namespace(task='''audio_pretraining''' ) _lowercase =fairseq.tasks.setup_task(__snake_case ) _lowercase , _lowercase , _lowercase =fairseq.checkpoint_utils.load_model_ensemble_and_task([checkpoint_path] , task=__snake_case ) _lowercase =model[0].eval() recursively_load_weights(__snake_case , __snake_case , not is_finetuned ) hf_wavavec.save_pretrained(__snake_case ) if __name__ == "__main__": UpperCAmelCase__ = argparse.ArgumentParser() parser.add_argument('''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''') parser.add_argument('''--checkpoint_path''', default=None, type=str, help='''Path to fairseq checkpoint''') parser.add_argument('''--dict_path''', default=None, type=str, help='''Path to dict of fine-tuned model''') parser.add_argument('''--config_path''', default=None, type=str, help='''Path to hf config.json of model to convert''') parser.add_argument( '''--not_finetuned''', action='''store_true''', help='''Whether the model to convert is a fine-tuned model or not''' ) UpperCAmelCase__ = parser.parse_args() convert_wavaveca_conformer_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.dict_path, not args.not_finetuned )
5
# DISCLAIMER: This file is strongly influenced by https://github.com/yang-song/score_sde_pytorch import math from typing import Union import torch from ..configuration_utils import ConfigMixin, register_to_config from ..utils import randn_tensor from .scheduling_utils import SchedulerMixin class lowerCamelCase__ ( lowerCAmelCase , lowerCAmelCase): SCREAMING_SNAKE_CASE__ = 1 @register_to_config def __init__(self , UpperCAmelCase=2_0_0_0 , UpperCAmelCase=0.1 , UpperCAmelCase=2_0 , UpperCAmelCase=1e-3 ) -> List[str]: _lowercase =None _lowercase =None _lowercase =None def __A (self , UpperCAmelCase , UpperCAmelCase = None ) -> str: _lowercase =torch.linspace(1 , self.config.sampling_eps , UpperCAmelCase , device=UpperCAmelCase ) def __A (self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase=None ) -> Optional[int]: if self.timesteps is None: raise ValueError( '''`self.timesteps` is not set, you need to run \'set_timesteps\' after creating the scheduler''' ) # TODO(Patrick) better comments + non-PyTorch # postprocess model score _lowercase =( -0.25 * t**2 * (self.config.beta_max - self.config.beta_min) - 0.5 * t * self.config.beta_min ) _lowercase =torch.sqrt(1.0 - torch.exp(2.0 * log_mean_coeff ) ) _lowercase =std.flatten() while len(std.shape ) < len(score.shape ): _lowercase =std.unsqueeze(-1 ) _lowercase =-score / std # compute _lowercase =-1.0 / len(self.timesteps ) _lowercase =self.config.beta_min + t * (self.config.beta_max - self.config.beta_min) _lowercase =beta_t.flatten() while len(beta_t.shape ) < len(x.shape ): _lowercase =beta_t.unsqueeze(-1 ) _lowercase =-0.5 * beta_t * x _lowercase =torch.sqrt(UpperCAmelCase ) _lowercase =drift - diffusion**2 * score _lowercase =x + drift * dt # add noise _lowercase =randn_tensor(x.shape , layout=x.layout , generator=UpperCAmelCase , device=x.device , dtype=x.dtype ) _lowercase =x_mean + diffusion * math.sqrt(-dt ) * noise return x, x_mean def __len__(self ) -> str: return self.config.num_train_timesteps
5
1
import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = '''▁''' UpperCAmelCase__ = {'''vocab_file''': '''spiece.model'''} UpperCAmelCase__ = { '''vocab_file''': { '''google/reformer-crime-and-punishment''': ( '''https://huggingface.co/google/reformer-crime-and-punishment/resolve/main/spiece.model''' ) } } UpperCAmelCase__ = { '''google/reformer-crime-and-punishment''': 52_4288, } class lowerCamelCase__ ( lowerCAmelCase): SCREAMING_SNAKE_CASE__ = VOCAB_FILES_NAMES SCREAMING_SNAKE_CASE__ = PRETRAINED_VOCAB_FILES_MAP SCREAMING_SNAKE_CASE__ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES SCREAMING_SNAKE_CASE__ = ['''input_ids''', '''attention_mask'''] def __init__(self , UpperCAmelCase , UpperCAmelCase="</s>" , UpperCAmelCase="<unk>" , UpperCAmelCase=[] , UpperCAmelCase = None , **UpperCAmelCase , ) -> None: _lowercase ={} if sp_model_kwargs is None else sp_model_kwargs super().__init__( eos_token=UpperCAmelCase , unk_token=UpperCAmelCase , additional_special_tokens=UpperCAmelCase , sp_model_kwargs=self.sp_model_kwargs , **UpperCAmelCase , ) _lowercase =vocab_file _lowercase =spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(UpperCAmelCase ) @property def __A (self ) -> Optional[int]: return self.sp_model.get_piece_size() def __A (self ) -> Dict[str, int]: _lowercase ={self.convert_ids_to_tokens(UpperCAmelCase ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__(self ) -> Union[str, Any]: _lowercase =self.__dict__.copy() _lowercase =None return state def __setstate__(self , UpperCAmelCase ) -> Optional[Any]: _lowercase =d # for backward compatibility if not hasattr(self , '''sp_model_kwargs''' ): _lowercase ={} _lowercase =spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def __A (self , UpperCAmelCase ) -> List[str]: return self.sp_model.encode(UpperCAmelCase , out_type=UpperCAmelCase ) def __A (self , UpperCAmelCase ) -> str: return self.sp_model.piece_to_id(UpperCAmelCase ) def __A (self , UpperCAmelCase ) -> Tuple: if index < self.sp_model.get_piece_size(): _lowercase =self.sp_model.IdToPiece(UpperCAmelCase ) return token def __A (self , UpperCAmelCase ) -> str: _lowercase =[] _lowercase ='''''' for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: out_string += self.sp_model.decode(UpperCAmelCase ) + token _lowercase =[] else: current_sub_tokens.append(UpperCAmelCase ) out_string += self.sp_model.decode(UpperCAmelCase ) return out_string.strip() def __A (self , UpperCAmelCase , UpperCAmelCase = None ) -> Tuple[str]: if not os.path.isdir(UpperCAmelCase ): logger.error(f"Vocabulary path ({save_directory}) should be a directory" ) return _lowercase =os.path.join( UpperCAmelCase , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(UpperCAmelCase ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , UpperCAmelCase ) elif not os.path.isfile(self.vocab_file ): with open(UpperCAmelCase , '''wb''' ) as fi: _lowercase =self.sp_model.serialized_model_proto() fi.write(UpperCAmelCase ) return (out_vocab_file,)
5
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( MobileViTConfig, MobileViTForImageClassification, MobileViTForSemanticSegmentation, MobileViTImageProcessor, ) from transformers.utils import logging logging.set_verbosity_info() UpperCAmelCase__ = logging.get_logger(__name__) def UpperCAmelCase_ ( __snake_case ) -> Optional[Any]: """simple docstring""" _lowercase =MobileViTConfig() # size of the architecture if "mobilevit_s" in mobilevit_name: _lowercase =[144, 192, 240] _lowercase =[16, 32, 64, 96, 128, 160, 640] elif "mobilevit_xs" in mobilevit_name: _lowercase =[96, 120, 144] _lowercase =[16, 32, 48, 64, 80, 96, 384] elif "mobilevit_xxs" in mobilevit_name: _lowercase =[64, 80, 96] _lowercase =[16, 16, 24, 48, 64, 80, 320] _lowercase =0.05 _lowercase =2.0 if mobilevit_name.startswith('''deeplabv3_''' ): _lowercase =512 _lowercase =16 _lowercase =21 _lowercase ='''pascal-voc-id2label.json''' else: _lowercase =1000 _lowercase ='''imagenet-1k-id2label.json''' _lowercase ='''huggingface/label-files''' _lowercase =json.load(open(hf_hub_download(__snake_case , __snake_case , repo_type='''dataset''' ) , '''r''' ) ) _lowercase ={int(__snake_case ): v for k, v in idalabel.items()} _lowercase =idalabel _lowercase ={v: k for k, v in idalabel.items()} return config def UpperCAmelCase_ ( __snake_case , __snake_case=False ) -> Tuple: """simple docstring""" for i in range(1 , 6 ): if F"layer_{i}." in name: _lowercase =name.replace(F"layer_{i}." , F"encoder.layer.{i - 1}." ) if "conv_1." in name: _lowercase =name.replace('''conv_1.''' , '''conv_stem.''' ) if ".block." in name: _lowercase =name.replace('''.block.''' , '''.''' ) if "exp_1x1" in name: _lowercase =name.replace('''exp_1x1''' , '''expand_1x1''' ) if "red_1x1" in name: _lowercase =name.replace('''red_1x1''' , '''reduce_1x1''' ) if ".local_rep.conv_3x3." in name: _lowercase =name.replace('''.local_rep.conv_3x3.''' , '''.conv_kxk.''' ) if ".local_rep.conv_1x1." in name: _lowercase =name.replace('''.local_rep.conv_1x1.''' , '''.conv_1x1.''' ) if ".norm." in name: _lowercase =name.replace('''.norm.''' , '''.normalization.''' ) if ".conv." in name: _lowercase =name.replace('''.conv.''' , '''.convolution.''' ) if ".conv_proj." in name: _lowercase =name.replace('''.conv_proj.''' , '''.conv_projection.''' ) for i in range(0 , 2 ): for j in range(0 , 4 ): if F".{i}.{j}." in name: _lowercase =name.replace(F".{i}.{j}." , F".{i}.layer.{j}." ) for i in range(2 , 6 ): for j in range(0 , 4 ): if F".{i}.{j}." in name: _lowercase =name.replace(F".{i}.{j}." , F".{i}." ) if "expand_1x1" in name: _lowercase =name.replace('''expand_1x1''' , '''downsampling_layer.expand_1x1''' ) if "conv_3x3" in name: _lowercase =name.replace('''conv_3x3''' , '''downsampling_layer.conv_3x3''' ) if "reduce_1x1" in name: _lowercase =name.replace('''reduce_1x1''' , '''downsampling_layer.reduce_1x1''' ) for i in range(2 , 5 ): if F".global_rep.{i}.weight" in name: _lowercase =name.replace(F".global_rep.{i}.weight" , '''.layernorm.weight''' ) if F".global_rep.{i}.bias" in name: _lowercase =name.replace(F".global_rep.{i}.bias" , '''.layernorm.bias''' ) if ".global_rep." in name: _lowercase =name.replace('''.global_rep.''' , '''.transformer.''' ) if ".pre_norm_mha.0." in name: _lowercase =name.replace('''.pre_norm_mha.0.''' , '''.layernorm_before.''' ) if ".pre_norm_mha.1.out_proj." in name: _lowercase =name.replace('''.pre_norm_mha.1.out_proj.''' , '''.attention.output.dense.''' ) if ".pre_norm_ffn.0." in name: _lowercase =name.replace('''.pre_norm_ffn.0.''' , '''.layernorm_after.''' ) if ".pre_norm_ffn.1." in name: _lowercase =name.replace('''.pre_norm_ffn.1.''' , '''.intermediate.dense.''' ) if ".pre_norm_ffn.4." in name: _lowercase =name.replace('''.pre_norm_ffn.4.''' , '''.output.dense.''' ) if ".transformer." in name: _lowercase =name.replace('''.transformer.''' , '''.transformer.layer.''' ) if ".aspp_layer." in name: _lowercase =name.replace('''.aspp_layer.''' , '''.''' ) if ".aspp_pool." in name: _lowercase =name.replace('''.aspp_pool.''' , '''.''' ) if "seg_head." in name: _lowercase =name.replace('''seg_head.''' , '''segmentation_head.''' ) if "segmentation_head.classifier.classifier." in name: _lowercase =name.replace('''segmentation_head.classifier.classifier.''' , '''segmentation_head.classifier.''' ) if "classifier.fc." in name: _lowercase =name.replace('''classifier.fc.''' , '''classifier.''' ) elif (not base_model) and ("segmentation_head." not in name): _lowercase ='''mobilevit.''' + name return name def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case=False ) -> Optional[Any]: """simple docstring""" if base_model: _lowercase ='''''' else: _lowercase ='''mobilevit.''' for key in orig_state_dict.copy().keys(): _lowercase =orig_state_dict.pop(__snake_case ) if key[:8] == "encoder.": _lowercase =key[8:] if "qkv" in key: _lowercase =key.split('''.''' ) _lowercase =int(key_split[0][6:] ) - 1 _lowercase =int(key_split[3] ) _lowercase =model.get_submodule(F"{model_prefix}encoder.layer.{layer_num}" ) _lowercase =layer.transformer.layer[transformer_num].attention.attention.all_head_size _lowercase =( F"{model_prefix}encoder.layer.{layer_num}.transformer.layer.{transformer_num}.attention.attention." ) if "weight" in key: _lowercase =val[:dim, :] _lowercase =val[dim : dim * 2, :] _lowercase =val[-dim:, :] else: _lowercase =val[:dim] _lowercase =val[dim : dim * 2] _lowercase =val[-dim:] else: _lowercase =val return orig_state_dict def UpperCAmelCase_ ( ) -> Union[str, Any]: """simple docstring""" _lowercase ='''http://images.cocodataset.org/val2017/000000039769.jpg''' _lowercase =Image.open(requests.get(__snake_case , stream=__snake_case ).raw ) return im @torch.no_grad() def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case=False ) -> int: """simple docstring""" _lowercase =get_mobilevit_config(__snake_case ) # load original state_dict _lowercase =torch.load(__snake_case , map_location='''cpu''' ) # load 🤗 model if mobilevit_name.startswith('''deeplabv3_''' ): _lowercase =MobileViTForSemanticSegmentation(__snake_case ).eval() else: _lowercase =MobileViTForImageClassification(__snake_case ).eval() _lowercase =convert_state_dict(__snake_case , __snake_case ) model.load_state_dict(__snake_case ) # Check outputs on an image, prepared by MobileViTImageProcessor _lowercase =MobileViTImageProcessor(crop_size=config.image_size , size=config.image_size + 32 ) _lowercase =image_processor(images=prepare_img() , return_tensors='''pt''' ) _lowercase =model(**__snake_case ) _lowercase =outputs.logits if mobilevit_name.startswith('''deeplabv3_''' ): assert logits.shape == (1, 21, 32, 32) if mobilevit_name == "deeplabv3_mobilevit_s": _lowercase =torch.tensor( [ [[6.20_65, 6.12_92, 6.20_70], [6.10_79, 6.12_54, 6.17_47], [6.00_42, 6.10_71, 6.10_34]], [[-6.92_53, -6.86_53, -7.03_98], [-7.32_18, -7.39_83, -7.36_70], [-7.19_61, -7.24_82, -7.15_69]], [[-4.47_23, -4.43_48, -4.37_69], [-5.36_29, -5.46_32, -5.45_98], [-5.15_87, -5.34_02, -5.50_59]], ] ) elif mobilevit_name == "deeplabv3_mobilevit_xs": _lowercase =torch.tensor( [ [[5.44_49, 5.57_33, 5.63_14], [5.18_15, 5.39_30, 5.59_63], [5.16_56, 5.43_33, 5.48_53]], [[-9.44_23, -9.77_66, -9.67_14], [-9.15_81, -9.57_20, -9.55_19], [-9.10_06, -9.64_58, -9.57_03]], [[-7.77_21, -7.37_16, -7.15_83], [-8.45_99, -8.06_24, -7.79_44], [-8.41_72, -7.83_66, -7.50_25]], ] ) elif mobilevit_name == "deeplabv3_mobilevit_xxs": _lowercase =torch.tensor( [ [[6.98_11, 6.97_43, 7.31_23], [7.17_77, 7.19_31, 7.39_38], [7.56_33, 7.80_50, 7.89_01]], [[-10.55_36, -10.23_32, -10.29_24], [-10.23_36, -9.86_24, -9.59_64], [-10.88_40, -10.81_58, -10.66_59]], [[-3.49_38, -3.06_31, -2.86_20], [-3.42_05, -2.81_35, -2.68_75], [-3.41_79, -2.79_45, -2.87_50]], ] ) else: raise ValueError(F"Unknown mobilevit_name: {mobilevit_name}" ) assert torch.allclose(logits[0, :3, :3, :3] , __snake_case , atol=1e-4 ) else: assert logits.shape == (1, 1000) if mobilevit_name == "mobilevit_s": _lowercase =torch.tensor([-0.98_66, 0.23_92, -1.12_41] ) elif mobilevit_name == "mobilevit_xs": _lowercase =torch.tensor([-2.47_61, -0.93_99, -1.95_87] ) elif mobilevit_name == "mobilevit_xxs": _lowercase =torch.tensor([-1.93_64, -1.23_27, -0.46_53] ) else: raise ValueError(F"Unknown mobilevit_name: {mobilevit_name}" ) assert torch.allclose(logits[0, :3] , __snake_case , atol=1e-4 ) Path(__snake_case ).mkdir(exist_ok=__snake_case ) print(F"Saving model {mobilevit_name} to {pytorch_dump_folder_path}" ) model.save_pretrained(__snake_case ) print(F"Saving image processor to {pytorch_dump_folder_path}" ) image_processor.save_pretrained(__snake_case ) if push_to_hub: _lowercase ={ '''mobilevit_s''': '''mobilevit-small''', '''mobilevit_xs''': '''mobilevit-x-small''', '''mobilevit_xxs''': '''mobilevit-xx-small''', '''deeplabv3_mobilevit_s''': '''deeplabv3-mobilevit-small''', '''deeplabv3_mobilevit_xs''': '''deeplabv3-mobilevit-x-small''', '''deeplabv3_mobilevit_xxs''': '''deeplabv3-mobilevit-xx-small''', } print('''Pushing to the hub...''' ) _lowercase =model_mapping[mobilevit_name] image_processor.push_to_hub(__snake_case , organization='''apple''' ) model.push_to_hub(__snake_case , organization='''apple''' ) if __name__ == "__main__": UpperCAmelCase__ = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--mobilevit_name''', default='''mobilevit_s''', type=str, help=( '''Name of the MobileViT model you\'d like to convert. Should be one of \'mobilevit_s\', \'mobilevit_xs\',''' ''' \'mobilevit_xxs\', \'deeplabv3_mobilevit_s\', \'deeplabv3_mobilevit_xs\', \'deeplabv3_mobilevit_xxs\'.''' ), ) parser.add_argument( '''--checkpoint_path''', required=True, type=str, help='''Path to the original state dict (.pt 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.mobilevit_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub )
5
1
from __future__ import annotations from typing import Any class lowerCamelCase__ ( lowerCAmelCase): pass class lowerCamelCase__ : def __init__(self , UpperCAmelCase ) -> None: _lowercase =data _lowercase =None def __iter__(self ) -> List[Any]: _lowercase =self _lowercase =[] while node: if node in visited: raise ContainsLoopError visited.append(UpperCAmelCase ) yield node.data _lowercase =node.next_node @property def __A (self ) -> bool: try: list(self ) return False except ContainsLoopError: return True if __name__ == "__main__": UpperCAmelCase__ = Node(1) UpperCAmelCase__ = Node(2) UpperCAmelCase__ = Node(3) UpperCAmelCase__ = Node(4) print(root_node.has_loop) # False UpperCAmelCase__ = root_node.next_node print(root_node.has_loop) # True UpperCAmelCase__ = Node(5) UpperCAmelCase__ = Node(6) UpperCAmelCase__ = Node(5) UpperCAmelCase__ = Node(6) print(root_node.has_loop) # False UpperCAmelCase__ = Node(1) print(root_node.has_loop) # False
5
import requests from bsa import BeautifulSoup def UpperCAmelCase_ ( __snake_case = "https://www.worldometers.info/coronavirus" ) -> dict: """simple docstring""" _lowercase =BeautifulSoup(requests.get(__snake_case ).text , '''html.parser''' ) _lowercase =soup.findAll('''h1''' ) _lowercase =soup.findAll('''div''' , {'''class''': '''maincounter-number'''} ) keys += soup.findAll('''span''' , {'''class''': '''panel-title'''} ) values += soup.findAll('''div''' , {'''class''': '''number-table-main'''} ) return {key.text.strip(): value.text.strip() for key, value in zip(__snake_case , __snake_case )} if __name__ == "__main__": print('''\033[1m''' + '''COVID-19 Status of the World''' + '''\033[0m\n''') for key, value in world_covidaa_stats().items(): print(f'''{key}\n{value}\n''')
5
1
def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , ) -> float: """simple docstring""" _lowercase =[redshift, radiation_density, matter_density, dark_energy] if any(p < 0 for p in parameters ): raise ValueError('''All input parameters must be positive''' ) if any(p > 1 for p in parameters[1:4] ): raise ValueError('''Relative densities cannot be greater than one''' ) else: _lowercase =1 - (matter_density + radiation_density + dark_energy) _lowercase =( radiation_density * (redshift + 1) ** 4 + matter_density * (redshift + 1) ** 3 + curvature * (redshift + 1) ** 2 + dark_energy ) _lowercase =hubble_constant * e_a ** (1 / 2) return hubble if __name__ == "__main__": import doctest # run doctest doctest.testmod() # demo LCDM approximation UpperCAmelCase__ = 0.3 print( hubble_parameter( hubble_constant=68.3, radiation_density=1e-4, matter_density=matter_density, dark_energy=1 - matter_density, redshift=0, ) )
5
from typing import TYPE_CHECKING from ..utils import _LazyModule UpperCAmelCase__ = { '''config''': [ '''EXTERNAL_DATA_FORMAT_SIZE_LIMIT''', '''OnnxConfig''', '''OnnxConfigWithPast''', '''OnnxSeq2SeqConfigWithPast''', '''PatchingSpec''', ], '''convert''': ['''export''', '''validate_model_outputs'''], '''features''': ['''FeaturesManager'''], '''utils''': ['''ParameterFormat''', '''compute_serialized_parameters_size'''], } if TYPE_CHECKING: from .config import ( EXTERNAL_DATA_FORMAT_SIZE_LIMIT, OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast, PatchingSpec, ) from .convert import export, validate_model_outputs from .features import FeaturesManager from .utils import ParameterFormat, compute_serialized_parameters_size else: import sys UpperCAmelCase__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
5
1
from __future__ import annotations import unittest from transformers import AutoTokenizer, MBartConfig, is_tf_available from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFAutoModelForSeqaSeqLM, TFMBartForConditionalGeneration, TFMBartModel @require_tf class lowerCamelCase__ : SCREAMING_SNAKE_CASE__ = MBartConfig SCREAMING_SNAKE_CASE__ = {} SCREAMING_SNAKE_CASE__ = '''gelu''' def __init__(self , UpperCAmelCase , UpperCAmelCase=1_3 , UpperCAmelCase=7 , UpperCAmelCase=True , UpperCAmelCase=False , UpperCAmelCase=9_9 , UpperCAmelCase=3_2 , UpperCAmelCase=2 , UpperCAmelCase=4 , UpperCAmelCase=3_7 , UpperCAmelCase=0.1 , UpperCAmelCase=0.1 , UpperCAmelCase=2_0 , UpperCAmelCase=2 , UpperCAmelCase=1 , UpperCAmelCase=0 , ) -> Optional[Any]: _lowercase =parent _lowercase =batch_size _lowercase =seq_length _lowercase =is_training _lowercase =use_labels _lowercase =vocab_size _lowercase =hidden_size _lowercase =num_hidden_layers _lowercase =num_attention_heads _lowercase =intermediate_size _lowercase =hidden_dropout_prob _lowercase =attention_probs_dropout_prob _lowercase =max_position_embeddings _lowercase =eos_token_id _lowercase =pad_token_id _lowercase =bos_token_id def __A (self ) -> Union[str, Any]: _lowercase =ids_tensor([self.batch_size, self.seq_length - 1] , self.vocab_size ) _lowercase =tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size ) , 1 ) _lowercase =tf.concat([input_ids, eos_tensor] , axis=1 ) _lowercase =ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _lowercase =self.config_cls( vocab_size=self.vocab_size , d_model=self.hidden_size , encoder_layers=self.num_hidden_layers , decoder_layers=self.num_hidden_layers , encoder_attention_heads=self.num_attention_heads , decoder_attention_heads=self.num_attention_heads , encoder_ffn_dim=self.intermediate_size , decoder_ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , eos_token_ids=[2] , bos_token_id=self.bos_token_id , pad_token_id=self.pad_token_id , decoder_start_token_id=self.pad_token_id , **self.config_updates , ) _lowercase =prepare_mbart_inputs_dict(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) return config, inputs_dict def __A (self , UpperCAmelCase , UpperCAmelCase ) -> Tuple: _lowercase =TFMBartModel(config=UpperCAmelCase ).get_decoder() _lowercase =inputs_dict['''input_ids'''] _lowercase =input_ids[:1, :] _lowercase =inputs_dict['''attention_mask'''][:1, :] _lowercase =inputs_dict['''head_mask'''] _lowercase =1 # first forward pass _lowercase =model(UpperCAmelCase , attention_mask=UpperCAmelCase , head_mask=UpperCAmelCase , use_cache=UpperCAmelCase ) _lowercase , _lowercase =outputs.to_tuple() _lowercase =past_key_values[1] def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case=None , __snake_case=None , __snake_case=None , __snake_case=None , __snake_case=None , ) -> Dict: """simple docstring""" if attention_mask is None: _lowercase =tf.cast(tf.math.not_equal(__snake_case , config.pad_token_id ) , tf.inta ) if decoder_attention_mask is None: _lowercase =tf.concat( [ tf.ones(decoder_input_ids[:, :1].shape , dtype=tf.inta ), tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:] , config.pad_token_id ) , tf.inta ), ] , axis=-1 , ) if head_mask is None: _lowercase =tf.ones((config.encoder_layers, config.encoder_attention_heads) ) if decoder_head_mask is None: _lowercase =tf.ones((config.decoder_layers, config.decoder_attention_heads) ) if cross_attn_head_mask is None: _lowercase =tf.ones((config.decoder_layers, config.decoder_attention_heads) ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": decoder_attention_mask, "head_mask": head_mask, "decoder_head_mask": decoder_head_mask, "cross_attn_head_mask": cross_attn_head_mask, } @require_tf class lowerCamelCase__ ( lowerCAmelCase , lowerCAmelCase , unittest.TestCase): SCREAMING_SNAKE_CASE__ = (TFMBartForConditionalGeneration, TFMBartModel) if is_tf_available() else () SCREAMING_SNAKE_CASE__ = (TFMBartForConditionalGeneration,) if is_tf_available() else () SCREAMING_SNAKE_CASE__ = ( { '''conversational''': TFMBartForConditionalGeneration, '''feature-extraction''': TFMBartModel, '''summarization''': TFMBartForConditionalGeneration, '''text2text-generation''': TFMBartForConditionalGeneration, '''translation''': TFMBartForConditionalGeneration, } if is_tf_available() else {} ) SCREAMING_SNAKE_CASE__ = True SCREAMING_SNAKE_CASE__ = False SCREAMING_SNAKE_CASE__ = False def __A (self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) -> List[str]: if pipeline_test_casse_name != "FeatureExtractionPipelineTests": # Exception encountered when calling layer '...' return True return False def __A (self ) -> Tuple: _lowercase =TFMBartModelTester(self ) _lowercase =ConfigTester(self , config_class=UpperCAmelCase ) def __A (self ) -> str: self.config_tester.run_common_tests() def __A (self ) -> List[Any]: _lowercase =self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_decoder_model_past_large_inputs(*UpperCAmelCase ) @require_sentencepiece @require_tokenizers @require_tf class lowerCamelCase__ ( unittest.TestCase): SCREAMING_SNAKE_CASE__ = [ ''' UN Chief Says There Is No Military Solution in Syria''', ] SCREAMING_SNAKE_CASE__ = [ '''Şeful ONU declară că nu există o soluţie militară în Siria''', ] SCREAMING_SNAKE_CASE__ = '''facebook/mbart-large-en-ro''' @cached_property def __A (self ) -> Optional[Any]: return AutoTokenizer.from_pretrained(self.model_name ) @cached_property def __A (self ) -> Optional[int]: _lowercase =TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name ) return model def __A (self , **UpperCAmelCase ) -> Dict: _lowercase =self.translate_src_text(**UpperCAmelCase ) self.assertListEqual(self.expected_text , UpperCAmelCase ) def __A (self , **UpperCAmelCase ) -> Tuple: _lowercase =self.tokenizer(self.src_text , **UpperCAmelCase , return_tensors='''tf''' ) _lowercase =self.model.generate( model_inputs.input_ids , attention_mask=model_inputs.attention_mask , num_beams=2 ) _lowercase =self.tokenizer.batch_decode(UpperCAmelCase , skip_special_tokens=UpperCAmelCase ) return generated_words @slow def __A (self ) -> Optional[int]: self._assert_generated_batch_equal_expected()
5
def UpperCAmelCase_ ( __snake_case ) -> str: """simple docstring""" _lowercase =0 # if input_string is "aba" than new_input_string become "a|b|a" _lowercase ='''''' _lowercase ='''''' # append each character + "|" in new_string for range(0, length-1) for i in input_string[: len(__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 _lowercase , _lowercase =0, 0 # length[i] shows the length of palindromic substring with center i _lowercase =[1 for i in range(len(__snake_case ) )] # for each character in new_string find corresponding palindromic string _lowercase =0 for j in range(len(__snake_case ) ): _lowercase =1 if j > r else min(length[l + r - j] // 2 , r - j + 1 ) while ( j - k >= 0 and j + k < len(__snake_case ) and new_input_string[k + j] == new_input_string[j - k] ): k += 1 _lowercase =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: _lowercase =j - k + 1 # noqa: E741 _lowercase =j + k - 1 # update max_length and start position if max_length < length[j]: _lowercase =length[j] _lowercase =j # create that string _lowercase =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()
5
1
import math import time from transformers import Trainer, is_torch_tpu_available from transformers.trainer_utils import PredictionOutput, speed_metrics if is_torch_tpu_available(check_device=False): import torch_xla.core.xla_model as xm import torch_xla.debug.metrics as met class lowerCamelCase__ ( lowerCAmelCase): def __init__(self , *UpperCAmelCase , UpperCAmelCase=None , UpperCAmelCase=None , **UpperCAmelCase ) -> List[Any]: super().__init__(*UpperCAmelCase , **UpperCAmelCase ) _lowercase =eval_examples _lowercase =post_process_function def __A (self , UpperCAmelCase=None , UpperCAmelCase=None , UpperCAmelCase=None , UpperCAmelCase = "eval" ) -> str: _lowercase =self.eval_dataset if eval_dataset is None else eval_dataset _lowercase =self.get_eval_dataloader(UpperCAmelCase ) _lowercase =self.eval_examples if eval_examples is None else eval_examples # Temporarily disable metric computation, we will do it in the loop here. _lowercase =self.compute_metrics _lowercase =None _lowercase =self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop _lowercase =time.time() try: _lowercase =eval_loop( UpperCAmelCase , description='''Evaluation''' , prediction_loss_only=True if compute_metrics is None else None , ignore_keys=UpperCAmelCase , metric_key_prefix=UpperCAmelCase , ) finally: _lowercase =compute_metrics _lowercase =self.args.eval_batch_size * self.args.world_size if f"{metric_key_prefix}_jit_compilation_time" in output.metrics: start_time += output.metrics[f"{metric_key_prefix}_jit_compilation_time"] output.metrics.update( speed_metrics( UpperCAmelCase , UpperCAmelCase , num_samples=output.num_samples , num_steps=math.ceil(output.num_samples / total_batch_size ) , ) ) if self.post_process_function is not None and self.compute_metrics is not None and self.args.should_save: # Only the main node write the results by default _lowercase =self.post_process_function(UpperCAmelCase , UpperCAmelCase , output.predictions ) _lowercase =self.compute_metrics(UpperCAmelCase ) # Prefix all keys with metric_key_prefix + '_' for key in list(metrics.keys() ): if not key.startswith(f"{metric_key_prefix}_" ): _lowercase =metrics.pop(UpperCAmelCase ) metrics.update(output.metrics ) else: _lowercase =output.metrics if self.args.should_log: # Only the main node log the results by default self.log(UpperCAmelCase ) if self.args.tpu_metrics_debug or self.args.debug: # tpu-comment: Logging debug metrics for PyTorch/XLA (compile, execute times, ops, etc.) xm.master_print(met.metrics_report() ) _lowercase =self.callback_handler.on_evaluate(self.args , self.state , self.control , UpperCAmelCase ) return metrics def __A (self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase=None , UpperCAmelCase = "test" ) -> Union[str, Any]: _lowercase =self.get_test_dataloader(UpperCAmelCase ) # Temporarily disable metric computation, we will do it in the loop here. _lowercase =self.compute_metrics _lowercase =None _lowercase =self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop _lowercase =time.time() try: _lowercase =eval_loop( UpperCAmelCase , description='''Prediction''' , prediction_loss_only=True if compute_metrics is None else None , ignore_keys=UpperCAmelCase , metric_key_prefix=UpperCAmelCase , ) finally: _lowercase =compute_metrics _lowercase =self.args.eval_batch_size * self.args.world_size if f"{metric_key_prefix}_jit_compilation_time" in output.metrics: start_time += output.metrics[f"{metric_key_prefix}_jit_compilation_time"] output.metrics.update( speed_metrics( UpperCAmelCase , UpperCAmelCase , num_samples=output.num_samples , num_steps=math.ceil(output.num_samples / total_batch_size ) , ) ) if self.post_process_function is None or self.compute_metrics is None: return output _lowercase =self.post_process_function(UpperCAmelCase , UpperCAmelCase , output.predictions , '''predict''' ) _lowercase =self.compute_metrics(UpperCAmelCase ) # Prefix all keys with metric_key_prefix + '_' for key in list(metrics.keys() ): if not key.startswith(f"{metric_key_prefix}_" ): _lowercase =metrics.pop(UpperCAmelCase ) metrics.update(output.metrics ) return PredictionOutput(predictions=predictions.predictions , label_ids=predictions.label_ids , metrics=UpperCAmelCase )
5
from math import isqrt def UpperCAmelCase_ ( __snake_case ) -> list[int]: """simple docstring""" _lowercase =[True] * max_number for i in range(2 , isqrt(max_number - 1 ) + 1 ): if is_prime[i]: for j in range(i**2 , __snake_case , __snake_case ): _lowercase =False return [i for i in range(2 , __snake_case ) if is_prime[i]] def UpperCAmelCase_ ( __snake_case = 10**8 ) -> int: """simple docstring""" _lowercase =calculate_prime_numbers(max_number // 2 ) _lowercase =0 _lowercase =0 _lowercase =len(__snake_case ) - 1 while left <= right: while prime_numbers[left] * prime_numbers[right] >= max_number: right -= 1 semiprimes_count += right - left + 1 left += 1 return semiprimes_count if __name__ == "__main__": print(f'''{solution() = }''')
5
1
from typing import TYPE_CHECKING from ...utils import _LazyModule UpperCAmelCase__ = {'''tokenization_byt5''': ['''ByT5Tokenizer''']} if TYPE_CHECKING: from .tokenization_byta import ByTaTokenizer else: import sys UpperCAmelCase__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
5
UpperCAmelCase__ = { '''A''': '''.-''', '''B''': '''-...''', '''C''': '''-.-.''', '''D''': '''-..''', '''E''': '''.''', '''F''': '''..-.''', '''G''': '''--.''', '''H''': '''....''', '''I''': '''..''', '''J''': '''.---''', '''K''': '''-.-''', '''L''': '''.-..''', '''M''': '''--''', '''N''': '''-.''', '''O''': '''---''', '''P''': '''.--.''', '''Q''': '''--.-''', '''R''': '''.-.''', '''S''': '''...''', '''T''': '''-''', '''U''': '''..-''', '''V''': '''...-''', '''W''': '''.--''', '''X''': '''-..-''', '''Y''': '''-.--''', '''Z''': '''--..''', '''1''': '''.----''', '''2''': '''..---''', '''3''': '''...--''', '''4''': '''....-''', '''5''': '''.....''', '''6''': '''-....''', '''7''': '''--...''', '''8''': '''---..''', '''9''': '''----.''', '''0''': '''-----''', '''&''': '''.-...''', '''@''': '''.--.-.''', ''':''': '''---...''', ''',''': '''--..--''', '''.''': '''.-.-.-''', '''\'''': '''.----.''', '''"''': '''.-..-.''', '''?''': '''..--..''', '''/''': '''-..-.''', '''=''': '''-...-''', '''+''': '''.-.-.''', '''-''': '''-....-''', '''(''': '''-.--.''', ''')''': '''-.--.-''', '''!''': '''-.-.--''', ''' ''': '''/''' } # Exclamation mark is not in ITU-R recommendation # fmt: on UpperCAmelCase__ = {value: key for key, value in MORSE_CODE_DICT.items()} def UpperCAmelCase_ ( __snake_case ) -> str: """simple docstring""" return " ".join(MORSE_CODE_DICT[char] for char in message.upper() ) def UpperCAmelCase_ ( __snake_case ) -> str: """simple docstring""" return "".join(REVERSE_DICT[char] for char in message.split() ) def UpperCAmelCase_ ( ) -> None: """simple docstring""" _lowercase ='''Morse code here!''' print(__snake_case ) _lowercase =encrypt(__snake_case ) print(__snake_case ) _lowercase =decrypt(__snake_case ) print(__snake_case ) if __name__ == "__main__": main()
5
1
def UpperCAmelCase_ ( __snake_case = 100 ) -> int: """simple docstring""" _lowercase =(n * (n + 1) // 2) ** 2 _lowercase =n * (n + 1) * (2 * n + 1) // 6 return sum_cubes - sum_squares if __name__ == "__main__": print(f'''{solution() = }''')
5
from typing import Any def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , ) -> list: """simple docstring""" _validation( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , ) # Creates data structures and fill initial step _lowercase ={} _lowercase ={} for state in states_space: _lowercase =observations_space[0] _lowercase =( initial_probabilities[state] * emission_probabilities[state][observation] ) _lowercase =None # Fills the data structure with the probabilities of # different transitions and pointers to previous states for o in range(1 , len(__snake_case ) ): _lowercase =observations_space[o] _lowercase =observations_space[o - 1] for state in states_space: # Calculates the argmax for probability function _lowercase ='''''' _lowercase =-1 for k_state in states_space: _lowercase =( probabilities[(k_state, prior_observation)] * transition_probabilities[k_state][state] * emission_probabilities[state][observation] ) if probability > max_probability: _lowercase =probability _lowercase =k_state # Update probabilities and pointers dicts _lowercase =( probabilities[(arg_max, prior_observation)] * transition_probabilities[arg_max][state] * emission_probabilities[state][observation] ) _lowercase =arg_max # The final observation _lowercase =observations_space[len(__snake_case ) - 1] # argmax for given final observation _lowercase ='''''' _lowercase =-1 for k_state in states_space: _lowercase =probabilities[(k_state, final_observation)] if probability > max_probability: _lowercase =probability _lowercase =k_state _lowercase =arg_max # Process pointers backwards _lowercase =last_state _lowercase =[] for o in range(len(__snake_case ) - 1 , -1 , -1 ): result.append(__snake_case ) _lowercase =pointers[previous, observations_space[o]] result.reverse() return result def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , ) -> None: """simple docstring""" _validate_not_empty( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , ) _validate_lists(__snake_case , __snake_case ) _validate_dicts( __snake_case , __snake_case , __snake_case ) def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , ) -> None: """simple docstring""" if not all( [ observations_space, states_space, initial_probabilities, transition_probabilities, emission_probabilities, ] ): raise ValueError('''There\'s an empty parameter''' ) def UpperCAmelCase_ ( __snake_case , __snake_case ) -> None: """simple docstring""" _validate_list(__snake_case , '''observations_space''' ) _validate_list(__snake_case , '''states_space''' ) def UpperCAmelCase_ ( __snake_case , __snake_case ) -> None: """simple docstring""" if not isinstance(_object , __snake_case ): _lowercase =F"{var_name} must be a list" raise ValueError(__snake_case ) else: for x in _object: if not isinstance(__snake_case , __snake_case ): _lowercase =F"{var_name} must be a list of strings" raise ValueError(__snake_case ) def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , ) -> None: """simple docstring""" _validate_dict(__snake_case , '''initial_probabilities''' , __snake_case ) _validate_nested_dict(__snake_case , '''transition_probabilities''' ) _validate_nested_dict(__snake_case , '''emission_probabilities''' ) def UpperCAmelCase_ ( __snake_case , __snake_case ) -> None: """simple docstring""" _validate_dict(_object , __snake_case , __snake_case ) for x in _object.values(): _validate_dict(__snake_case , __snake_case , __snake_case , __snake_case ) def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case = False ) -> None: """simple docstring""" if not isinstance(_object , __snake_case ): _lowercase =F"{var_name} must be a dict" raise ValueError(__snake_case ) if not all(isinstance(__snake_case , __snake_case ) for x in _object ): _lowercase =F"{var_name} all keys must be strings" raise ValueError(__snake_case ) if not all(isinstance(__snake_case , __snake_case ) for x in _object.values() ): _lowercase ='''nested dictionary ''' if nested else '''''' _lowercase =F"{var_name} {nested_text}all values must be {value_type.__name__}" raise ValueError(__snake_case ) if __name__ == "__main__": from doctest import testmod testmod()
5
1
from dataclasses import asdict, dataclass from typing import Optional from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCAmelCase__ = logging.get_logger(__name__) # TODO Update this UpperCAmelCase__ = { '''facebook/esm-1b''': '''https://huggingface.co/facebook/esm-1b/resolve/main/config.json''', # See all ESM models at https://huggingface.co/models?filter=esm } class lowerCamelCase__ ( lowerCAmelCase): SCREAMING_SNAKE_CASE__ = '''esm''' def __init__(self , UpperCAmelCase=None , UpperCAmelCase=None , UpperCAmelCase=None , UpperCAmelCase=7_6_8 , UpperCAmelCase=1_2 , UpperCAmelCase=1_2 , UpperCAmelCase=3_0_7_2 , UpperCAmelCase=0.1 , UpperCAmelCase=0.1 , UpperCAmelCase=1_0_2_6 , UpperCAmelCase=0.02 , UpperCAmelCase=1e-12 , UpperCAmelCase="absolute" , UpperCAmelCase=True , UpperCAmelCase=None , UpperCAmelCase=False , UpperCAmelCase=False , UpperCAmelCase=None , UpperCAmelCase=None , **UpperCAmelCase , ) -> Tuple: super().__init__(pad_token_id=UpperCAmelCase , mask_token_id=UpperCAmelCase , **UpperCAmelCase ) _lowercase =vocab_size _lowercase =hidden_size _lowercase =num_hidden_layers _lowercase =num_attention_heads _lowercase =intermediate_size _lowercase =hidden_dropout_prob _lowercase =attention_probs_dropout_prob _lowercase =max_position_embeddings _lowercase =initializer_range _lowercase =layer_norm_eps _lowercase =position_embedding_type _lowercase =use_cache _lowercase =emb_layer_norm_before _lowercase =token_dropout _lowercase =is_folding_model if is_folding_model: if esmfold_config is None: logger.info('''No esmfold_config supplied for folding model, using default values.''' ) _lowercase =EsmFoldConfig() elif isinstance(UpperCAmelCase , UpperCAmelCase ): _lowercase =EsmFoldConfig(**UpperCAmelCase ) _lowercase =esmfold_config if vocab_list is None: logger.warning('''No vocab_list supplied for folding model, assuming the ESM-2 vocabulary!''' ) _lowercase =get_default_vocab_list() else: _lowercase =vocab_list else: _lowercase =None _lowercase =None if self.esmfold_config is not None and getattr(self.esmfold_config , '''use_esm_attn_map''' , UpperCAmelCase ): raise ValueError('''The HuggingFace port of ESMFold does not support use_esm_attn_map at this time!''' ) def __A (self ) -> List[str]: _lowercase =super().to_dict() if isinstance(self.esmfold_config , UpperCAmelCase ): _lowercase =self.esmfold_config.to_dict() return output @dataclass class lowerCamelCase__ : SCREAMING_SNAKE_CASE__ = None SCREAMING_SNAKE_CASE__ = True SCREAMING_SNAKE_CASE__ = False SCREAMING_SNAKE_CASE__ = False SCREAMING_SNAKE_CASE__ = False SCREAMING_SNAKE_CASE__ = 0 SCREAMING_SNAKE_CASE__ = True SCREAMING_SNAKE_CASE__ = False SCREAMING_SNAKE_CASE__ = 128 SCREAMING_SNAKE_CASE__ = None def __A (self ) -> Union[str, Any]: if self.trunk is None: _lowercase =TrunkConfig() elif isinstance(self.trunk , UpperCAmelCase ): _lowercase =TrunkConfig(**self.trunk ) def __A (self ) -> Tuple: _lowercase =asdict(self ) _lowercase =self.trunk.to_dict() return output @dataclass class lowerCamelCase__ : SCREAMING_SNAKE_CASE__ = 48 SCREAMING_SNAKE_CASE__ = 1024 SCREAMING_SNAKE_CASE__ = 128 SCREAMING_SNAKE_CASE__ = 32 SCREAMING_SNAKE_CASE__ = 32 SCREAMING_SNAKE_CASE__ = 32 SCREAMING_SNAKE_CASE__ = 0 SCREAMING_SNAKE_CASE__ = 0 SCREAMING_SNAKE_CASE__ = False SCREAMING_SNAKE_CASE__ = 4 SCREAMING_SNAKE_CASE__ = 128 SCREAMING_SNAKE_CASE__ = None def __A (self ) -> List[str]: if self.structure_module is None: _lowercase =StructureModuleConfig() elif isinstance(self.structure_module , UpperCAmelCase ): _lowercase =StructureModuleConfig(**self.structure_module ) if self.max_recycles <= 0: raise ValueError(f"`max_recycles` should be positive, got {self.max_recycles}." ) if self.sequence_state_dim % self.sequence_state_dim != 0: raise ValueError( '''`sequence_state_dim` should be a round multiple of `sequence_state_dim`, got''' f" {self.sequence_state_dim} and {self.sequence_state_dim}." ) if self.pairwise_state_dim % self.pairwise_state_dim != 0: raise ValueError( '''`pairwise_state_dim` should be a round multiple of `pairwise_state_dim`, got''' f" {self.pairwise_state_dim} and {self.pairwise_state_dim}." ) _lowercase =self.sequence_state_dim // self.sequence_head_width _lowercase =self.pairwise_state_dim // self.pairwise_head_width if self.sequence_state_dim != sequence_num_heads * self.sequence_head_width: raise ValueError( '''`sequence_state_dim` should be equal to `sequence_num_heads * sequence_head_width, got''' f" {self.sequence_state_dim} != {sequence_num_heads} * {self.sequence_head_width}." ) if self.pairwise_state_dim != pairwise_num_heads * self.pairwise_head_width: raise ValueError( '''`pairwise_state_dim` should be equal to `pairwise_num_heads * pairwise_head_width, got''' f" {self.pairwise_state_dim} != {pairwise_num_heads} * {self.pairwise_head_width}." ) if self.pairwise_state_dim % 2 != 0: raise ValueError(f"`pairwise_state_dim` should be even, got {self.pairwise_state_dim}." ) if self.dropout >= 0.4: raise ValueError(f"`dropout` should not be greater than 0.4, got {self.dropout}." ) def __A (self ) -> Dict: _lowercase =asdict(self ) _lowercase =self.structure_module.to_dict() return output @dataclass class lowerCamelCase__ : SCREAMING_SNAKE_CASE__ = 384 SCREAMING_SNAKE_CASE__ = 128 SCREAMING_SNAKE_CASE__ = 16 SCREAMING_SNAKE_CASE__ = 128 SCREAMING_SNAKE_CASE__ = 12 SCREAMING_SNAKE_CASE__ = 4 SCREAMING_SNAKE_CASE__ = 8 SCREAMING_SNAKE_CASE__ = 0.1 SCREAMING_SNAKE_CASE__ = 8 SCREAMING_SNAKE_CASE__ = 1 SCREAMING_SNAKE_CASE__ = 2 SCREAMING_SNAKE_CASE__ = 7 SCREAMING_SNAKE_CASE__ = 10 SCREAMING_SNAKE_CASE__ = 1E-8 SCREAMING_SNAKE_CASE__ = 1E5 def __A (self ) -> List[Any]: return asdict(self ) def UpperCAmelCase_ ( ) -> Tuple: """simple docstring""" return ( "<cls>", "<pad>", "<eos>", "<unk>", "L", "A", "G", "V", "S", "E", "R", "T", "I", "D", "P", "K", "Q", "N", "F", "Y", "M", "H", "W", "C", "X", "B", "U", "Z", "O", ".", "-", "<null_1>", "<mask>", )
5
from dataclasses import asdict, dataclass from typing import Optional from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCAmelCase__ = logging.get_logger(__name__) # TODO Update this UpperCAmelCase__ = { '''facebook/esm-1b''': '''https://huggingface.co/facebook/esm-1b/resolve/main/config.json''', # See all ESM models at https://huggingface.co/models?filter=esm } class lowerCamelCase__ ( lowerCAmelCase): SCREAMING_SNAKE_CASE__ = '''esm''' def __init__(self , UpperCAmelCase=None , UpperCAmelCase=None , UpperCAmelCase=None , UpperCAmelCase=7_6_8 , UpperCAmelCase=1_2 , UpperCAmelCase=1_2 , UpperCAmelCase=3_0_7_2 , UpperCAmelCase=0.1 , UpperCAmelCase=0.1 , UpperCAmelCase=1_0_2_6 , UpperCAmelCase=0.02 , UpperCAmelCase=1e-12 , UpperCAmelCase="absolute" , UpperCAmelCase=True , UpperCAmelCase=None , UpperCAmelCase=False , UpperCAmelCase=False , UpperCAmelCase=None , UpperCAmelCase=None , **UpperCAmelCase , ) -> Tuple: super().__init__(pad_token_id=UpperCAmelCase , mask_token_id=UpperCAmelCase , **UpperCAmelCase ) _lowercase =vocab_size _lowercase =hidden_size _lowercase =num_hidden_layers _lowercase =num_attention_heads _lowercase =intermediate_size _lowercase =hidden_dropout_prob _lowercase =attention_probs_dropout_prob _lowercase =max_position_embeddings _lowercase =initializer_range _lowercase =layer_norm_eps _lowercase =position_embedding_type _lowercase =use_cache _lowercase =emb_layer_norm_before _lowercase =token_dropout _lowercase =is_folding_model if is_folding_model: if esmfold_config is None: logger.info('''No esmfold_config supplied for folding model, using default values.''' ) _lowercase =EsmFoldConfig() elif isinstance(UpperCAmelCase , UpperCAmelCase ): _lowercase =EsmFoldConfig(**UpperCAmelCase ) _lowercase =esmfold_config if vocab_list is None: logger.warning('''No vocab_list supplied for folding model, assuming the ESM-2 vocabulary!''' ) _lowercase =get_default_vocab_list() else: _lowercase =vocab_list else: _lowercase =None _lowercase =None if self.esmfold_config is not None and getattr(self.esmfold_config , '''use_esm_attn_map''' , UpperCAmelCase ): raise ValueError('''The HuggingFace port of ESMFold does not support use_esm_attn_map at this time!''' ) def __A (self ) -> List[str]: _lowercase =super().to_dict() if isinstance(self.esmfold_config , UpperCAmelCase ): _lowercase =self.esmfold_config.to_dict() return output @dataclass class lowerCamelCase__ : SCREAMING_SNAKE_CASE__ = None SCREAMING_SNAKE_CASE__ = True SCREAMING_SNAKE_CASE__ = False SCREAMING_SNAKE_CASE__ = False SCREAMING_SNAKE_CASE__ = False SCREAMING_SNAKE_CASE__ = 0 SCREAMING_SNAKE_CASE__ = True SCREAMING_SNAKE_CASE__ = False SCREAMING_SNAKE_CASE__ = 128 SCREAMING_SNAKE_CASE__ = None def __A (self ) -> Union[str, Any]: if self.trunk is None: _lowercase =TrunkConfig() elif isinstance(self.trunk , UpperCAmelCase ): _lowercase =TrunkConfig(**self.trunk ) def __A (self ) -> Tuple: _lowercase =asdict(self ) _lowercase =self.trunk.to_dict() return output @dataclass class lowerCamelCase__ : SCREAMING_SNAKE_CASE__ = 48 SCREAMING_SNAKE_CASE__ = 1024 SCREAMING_SNAKE_CASE__ = 128 SCREAMING_SNAKE_CASE__ = 32 SCREAMING_SNAKE_CASE__ = 32 SCREAMING_SNAKE_CASE__ = 32 SCREAMING_SNAKE_CASE__ = 0 SCREAMING_SNAKE_CASE__ = 0 SCREAMING_SNAKE_CASE__ = False SCREAMING_SNAKE_CASE__ = 4 SCREAMING_SNAKE_CASE__ = 128 SCREAMING_SNAKE_CASE__ = None def __A (self ) -> List[str]: if self.structure_module is None: _lowercase =StructureModuleConfig() elif isinstance(self.structure_module , UpperCAmelCase ): _lowercase =StructureModuleConfig(**self.structure_module ) if self.max_recycles <= 0: raise ValueError(f"`max_recycles` should be positive, got {self.max_recycles}." ) if self.sequence_state_dim % self.sequence_state_dim != 0: raise ValueError( '''`sequence_state_dim` should be a round multiple of `sequence_state_dim`, got''' f" {self.sequence_state_dim} and {self.sequence_state_dim}." ) if self.pairwise_state_dim % self.pairwise_state_dim != 0: raise ValueError( '''`pairwise_state_dim` should be a round multiple of `pairwise_state_dim`, got''' f" {self.pairwise_state_dim} and {self.pairwise_state_dim}." ) _lowercase =self.sequence_state_dim // self.sequence_head_width _lowercase =self.pairwise_state_dim // self.pairwise_head_width if self.sequence_state_dim != sequence_num_heads * self.sequence_head_width: raise ValueError( '''`sequence_state_dim` should be equal to `sequence_num_heads * sequence_head_width, got''' f" {self.sequence_state_dim} != {sequence_num_heads} * {self.sequence_head_width}." ) if self.pairwise_state_dim != pairwise_num_heads * self.pairwise_head_width: raise ValueError( '''`pairwise_state_dim` should be equal to `pairwise_num_heads * pairwise_head_width, got''' f" {self.pairwise_state_dim} != {pairwise_num_heads} * {self.pairwise_head_width}." ) if self.pairwise_state_dim % 2 != 0: raise ValueError(f"`pairwise_state_dim` should be even, got {self.pairwise_state_dim}." ) if self.dropout >= 0.4: raise ValueError(f"`dropout` should not be greater than 0.4, got {self.dropout}." ) def __A (self ) -> Dict: _lowercase =asdict(self ) _lowercase =self.structure_module.to_dict() return output @dataclass class lowerCamelCase__ : SCREAMING_SNAKE_CASE__ = 384 SCREAMING_SNAKE_CASE__ = 128 SCREAMING_SNAKE_CASE__ = 16 SCREAMING_SNAKE_CASE__ = 128 SCREAMING_SNAKE_CASE__ = 12 SCREAMING_SNAKE_CASE__ = 4 SCREAMING_SNAKE_CASE__ = 8 SCREAMING_SNAKE_CASE__ = 0.1 SCREAMING_SNAKE_CASE__ = 8 SCREAMING_SNAKE_CASE__ = 1 SCREAMING_SNAKE_CASE__ = 2 SCREAMING_SNAKE_CASE__ = 7 SCREAMING_SNAKE_CASE__ = 10 SCREAMING_SNAKE_CASE__ = 1E-8 SCREAMING_SNAKE_CASE__ = 1E5 def __A (self ) -> List[Any]: return asdict(self ) def UpperCAmelCase_ ( ) -> Tuple: """simple docstring""" return ( "<cls>", "<pad>", "<eos>", "<unk>", "L", "A", "G", "V", "S", "E", "R", "T", "I", "D", "P", "K", "Q", "N", "F", "Y", "M", "H", "W", "C", "X", "B", "U", "Z", "O", ".", "-", "<null_1>", "<mask>", )
5
1
import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = { '''microsoft/wavlm-base''': '''https://huggingface.co/microsoft/wavlm-base/resolve/main/config.json''', # See all WavLM models at https://huggingface.co/models?filter=wavlm } class lowerCamelCase__ ( lowerCAmelCase): SCREAMING_SNAKE_CASE__ = '''wavlm''' def __init__(self , UpperCAmelCase=3_2 , UpperCAmelCase=7_6_8 , UpperCAmelCase=1_2 , UpperCAmelCase=1_2 , UpperCAmelCase=3_0_7_2 , UpperCAmelCase="gelu" , UpperCAmelCase=0.1 , UpperCAmelCase=0.1 , UpperCAmelCase=0.1 , UpperCAmelCase=0.0 , UpperCAmelCase=0.1 , UpperCAmelCase=0.1 , UpperCAmelCase=0.02 , UpperCAmelCase=1e-5 , UpperCAmelCase="group" , UpperCAmelCase="gelu" , UpperCAmelCase=(5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2) , UpperCAmelCase=(5, 2, 2, 2, 2, 2, 2) , UpperCAmelCase=(1_0, 3, 3, 3, 3, 2, 2) , UpperCAmelCase=False , UpperCAmelCase=1_2_8 , UpperCAmelCase=1_6 , UpperCAmelCase=3_2_0 , UpperCAmelCase=8_0_0 , UpperCAmelCase=False , UpperCAmelCase=True , UpperCAmelCase=0.05 , UpperCAmelCase=1_0 , UpperCAmelCase=2 , UpperCAmelCase=0.0 , UpperCAmelCase=1_0 , UpperCAmelCase=3_2_0 , UpperCAmelCase=2 , UpperCAmelCase=0.1 , UpperCAmelCase=1_0_0 , UpperCAmelCase=2_5_6 , UpperCAmelCase=2_5_6 , UpperCAmelCase=0.1 , UpperCAmelCase="mean" , UpperCAmelCase=False , UpperCAmelCase=False , UpperCAmelCase=2_5_6 , UpperCAmelCase=(5_1_2, 5_1_2, 5_1_2, 5_1_2, 1_5_0_0) , UpperCAmelCase=(5, 3, 3, 1, 1) , UpperCAmelCase=(1, 2, 3, 1, 1) , UpperCAmelCase=5_1_2 , UpperCAmelCase=8_0 , UpperCAmelCase=0 , UpperCAmelCase=1 , UpperCAmelCase=2 , UpperCAmelCase=False , UpperCAmelCase=3 , UpperCAmelCase=2 , UpperCAmelCase=3 , UpperCAmelCase=None , **UpperCAmelCase , ) -> Optional[Any]: super().__init__(**UpperCAmelCase , pad_token_id=UpperCAmelCase , bos_token_id=UpperCAmelCase , eos_token_id=UpperCAmelCase ) _lowercase =hidden_size _lowercase =feat_extract_norm _lowercase =feat_extract_activation _lowercase =list(UpperCAmelCase ) _lowercase =list(UpperCAmelCase ) _lowercase =list(UpperCAmelCase ) _lowercase =conv_bias _lowercase =num_buckets _lowercase =max_bucket_distance _lowercase =num_conv_pos_embeddings _lowercase =num_conv_pos_embedding_groups _lowercase =len(self.conv_dim ) _lowercase =num_hidden_layers _lowercase =intermediate_size _lowercase =hidden_act _lowercase =num_attention_heads _lowercase =hidden_dropout _lowercase =attention_dropout _lowercase =activation_dropout _lowercase =feat_proj_dropout _lowercase =final_dropout _lowercase =layerdrop _lowercase =layer_norm_eps _lowercase =initializer_range _lowercase =num_ctc_classes _lowercase =vocab_size _lowercase =do_stable_layer_norm _lowercase =use_weighted_layer_sum _lowercase =classifier_proj_size if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( '''Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` ==''' ''' `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) =''' f" {len(self.conv_dim )}`, `len(config.conv_stride) = {len(self.conv_stride )}`," f" `len(config.conv_kernel) = {len(self.conv_kernel )}`." ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 _lowercase =apply_spec_augment _lowercase =mask_time_prob _lowercase =mask_time_length _lowercase =mask_time_min_masks _lowercase =mask_feature_prob _lowercase =mask_feature_length # parameters for pretraining with codevector quantized representations _lowercase =num_codevectors_per_group _lowercase =num_codevector_groups _lowercase =contrastive_logits_temperature _lowercase =num_negatives _lowercase =codevector_dim _lowercase =proj_codevector_dim _lowercase =diversity_loss_weight # ctc loss _lowercase =ctc_loss_reduction _lowercase =ctc_zero_infinity # adapter _lowercase =add_adapter _lowercase =adapter_kernel_size _lowercase =adapter_stride _lowercase =num_adapter_layers _lowercase =output_hidden_size or hidden_size # SequenceClassification-specific parameter. Feel free to ignore for other classes. _lowercase =classifier_proj_size # XVector-specific parameters. Feel free to ignore for other classes. _lowercase =list(UpperCAmelCase ) _lowercase =list(UpperCAmelCase ) _lowercase =list(UpperCAmelCase ) _lowercase =xvector_output_dim @property def __A (self ) -> int: return functools.reduce(operator.mul , self.conv_stride , 1 )
5
import json import multiprocessing import os import re from collections import defaultdict import torch from accelerate import Accelerator from accelerate.utils import set_seed from arguments import HumanEvalArguments from datasets import load_dataset, load_metric from torch.utils.data import IterableDataset from torch.utils.data.dataloader import DataLoader from tqdm import tqdm import transformers from transformers import AutoModelForCausalLM, AutoTokenizer, HfArgumentParser, StoppingCriteria, StoppingCriteriaList UpperCAmelCase__ = ['''\nclass''', '''\ndef''', '''\n#''', '''\n@''', '''\nprint''', '''\nif'''] class lowerCamelCase__ ( lowerCAmelCase): def __init__(self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase=None , UpperCAmelCase=1 ) -> Dict: _lowercase =tokenizer _lowercase =dataset _lowercase =len(UpperCAmelCase ) if n_tasks is None else n_tasks _lowercase =n_copies def __iter__(self ) -> Optional[Any]: _lowercase =[] for task in range(self.n_tasks ): # without strip, the model generate commented codes ... prompts.append(self.tokenizer.eos_token + self.dataset[task]['''prompt'''].strip() ) _lowercase =self.tokenizer(UpperCAmelCase , padding=UpperCAmelCase , return_tensors='''pt''' ) for task in range(self.n_tasks ): for _ in range(self.n_copies ): yield { "ids": outputs.input_ids[task], "task_id": task, "input_len": outputs.attention_mask[task].sum(), } class lowerCamelCase__ ( lowerCAmelCase): def __init__(self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) -> Optional[Any]: _lowercase =start_length _lowercase =eof_strings _lowercase =tokenizer def __call__(self , UpperCAmelCase , UpperCAmelCase , **UpperCAmelCase ) -> Dict: _lowercase =self.tokenizer.batch_decode(input_ids[:, self.start_length :] ) _lowercase =[] for decoded_generation in decoded_generations: done.append(any(stop_string in decoded_generation for stop_string in self.eof_strings ) ) return all(UpperCAmelCase ) def UpperCAmelCase_ ( __snake_case ) -> Optional[Any]: """simple docstring""" _lowercase =re.split('''(%s)''' % '''|'''.join(__snake_case ) , __snake_case ) # last string should be "" return "".join(string_list[:-2] ) def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , __snake_case=20 , **__snake_case ) -> Tuple: """simple docstring""" _lowercase =defaultdict(__snake_case ) # dict of list of generated tokens for step, batch in tqdm(enumerate(__snake_case ) ): with torch.no_grad(): _lowercase =batch['''ids'''].shape[-1] _lowercase =accelerator.unwrap_model(__snake_case ).generate( input_ids=batch['''ids'''][:, : batch['''input_len''']] , num_return_sequences=__snake_case , **__snake_case ) # each task is generated batch_size times _lowercase =batch['''task_id'''].repeat(__snake_case ) _lowercase =accelerator.pad_across_processes( __snake_case , dim=1 , pad_index=tokenizer.pad_token_id ) _lowercase , _lowercase =accelerator.gather((generated_tokens, generated_tasks) ) _lowercase =generated_tokens.cpu().numpy() _lowercase =generated_tasks.cpu().numpy() for task, generated_tokens in zip(__snake_case , __snake_case ): gen_token_dict[task].append(__snake_case ) _lowercase =[[] for _ in range(__snake_case )] for task, generated_tokens in gen_token_dict.items(): for s in generated_tokens: _lowercase =tokenizer.decode(__snake_case , skip_special_tokens=__snake_case , clean_up_tokenization_spaces=__snake_case ) code_gens[task].append(remove_last_block(__snake_case ) ) return code_gens def UpperCAmelCase_ ( ) -> str: """simple docstring""" _lowercase =HfArgumentParser(__snake_case ) _lowercase =parser.parse_args() transformers.logging.set_verbosity_error() # enables code execution in code_eval metric _lowercase =args.HF_ALLOW_CODE_EVAL # make sure tokenizer plays nice with multiprocessing _lowercase ='''false''' if args.num_workers is None: _lowercase =multiprocessing.cpu_count() # Use dataset load to feed to accelerate _lowercase =Accelerator() set_seed(args.seed , device_specific=__snake_case ) # Load model and tokenizer _lowercase =AutoTokenizer.from_pretrained(args.model_ckpt ) _lowercase =tokenizer.eos_token _lowercase =AutoModelForCausalLM.from_pretrained(args.model_ckpt ) # Generation settings _lowercase ={ '''do_sample''': args.do_sample, '''temperature''': args.temperature, '''max_new_tokens''': args.max_new_tokens, '''top_p''': args.top_p, '''top_k''': args.top_k, '''stopping_criteria''': StoppingCriteriaList([EndOfFunctionCriteria(0 , __snake_case , __snake_case )] ), } # Load evaluation dataset and metric _lowercase =load_dataset('''openai_humaneval''' ) _lowercase =load_metric('''code_eval''' ) _lowercase =args.num_tasks if args.num_tasks is not None else len(human_eval['''test'''] ) _lowercase =args.n_samples // args.batch_size _lowercase =TokenizedDataset(__snake_case , human_eval['''test'''] , n_copies=__snake_case , n_tasks=__snake_case ) # do not confuse args.batch_size, which is actually the num_return_sequences _lowercase =DataLoader(__snake_case , batch_size=1 ) # Run a quick test to see if code evaluation is enabled try: _lowercase =code_eval_metric.compute(references=[''''''] , predictions=[['''''']] ) except ValueError as exception: print( '''Code evaluation not enabled. Read the warning below carefully and then use `--HF_ALLOW_CODE_EVAL="1"`''' ''' flag to enable code evaluation.''' ) raise exception _lowercase , _lowercase =accelerator.prepare(__snake_case , __snake_case ) _lowercase =complete_code( __snake_case , __snake_case , __snake_case , __snake_case , n_tasks=__snake_case , batch_size=args.batch_size , **__snake_case , ) if accelerator.is_main_process: _lowercase =[] for task in tqdm(range(__snake_case ) ): _lowercase =human_eval['''test'''][task]['''test'''] _lowercase =F"check({human_eval['test'][task]['entry_point']})" references.append('''\n''' + test_func + '''\n''' + entry_point ) # Evaluate completions with "code_eval" metric _lowercase , _lowercase =code_eval_metric.compute( references=__snake_case , predictions=__snake_case , num_workers=args.num_workers ) print(F"Results: {pass_at_k}" ) # Save results to json file with open(args.output_file , '''w''' ) as fp: json.dump(__snake_case , __snake_case ) # For some reason the folliwng seems to be necessary sometimes for code_eval to work nice with multiprocessing # https://stackoverflow.com/questions/60804599/python-multiprocessing-keeps-spawning-the-whole-script if __name__ == "__main__": main()
5
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available UpperCAmelCase__ = { '''configuration_blip_2''': [ '''BLIP_2_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''Blip2Config''', '''Blip2QFormerConfig''', '''Blip2VisionConfig''', ], '''processing_blip_2''': ['''Blip2Processor'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ '''BLIP_2_PRETRAINED_MODEL_ARCHIVE_LIST''', '''Blip2Model''', '''Blip2QFormerModel''', '''Blip2PreTrainedModel''', '''Blip2ForConditionalGeneration''', '''Blip2VisionModel''', ] if TYPE_CHECKING: from .configuration_blip_a import ( BLIP_2_PRETRAINED_CONFIG_ARCHIVE_MAP, BlipaConfig, BlipaQFormerConfig, BlipaVisionConfig, ) from .processing_blip_a import BlipaProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_blip_a import ( BLIP_2_PRETRAINED_MODEL_ARCHIVE_LIST, BlipaForConditionalGeneration, BlipaModel, BlipaPreTrainedModel, BlipaQFormerModel, BlipaVisionModel, ) else: import sys UpperCAmelCase__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
5
UpperCAmelCase__ = 8.31_44_62 # Unit - J mol-1 K-1 def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case ) -> float: """simple docstring""" if moles < 0 or kelvin < 0 or volume < 0: raise ValueError('''Invalid inputs. Enter positive value.''' ) return moles * kelvin * UNIVERSAL_GAS_CONSTANT / volume def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case ) -> float: """simple docstring""" if moles < 0 or kelvin < 0 or pressure < 0: raise ValueError('''Invalid inputs. Enter positive value.''' ) return moles * kelvin * UNIVERSAL_GAS_CONSTANT / pressure if __name__ == "__main__": from doctest import testmod testmod()
5
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available UpperCAmelCase__ = { '''configuration_time_series_transformer''': [ '''TIME_SERIES_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''TimeSeriesTransformerConfig''', ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ '''TIME_SERIES_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TimeSeriesTransformerForPrediction''', '''TimeSeriesTransformerModel''', '''TimeSeriesTransformerPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_time_series_transformer import ( TIME_SERIES_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TimeSeriesTransformerConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_time_series_transformer import ( TIME_SERIES_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TimeSeriesTransformerForPrediction, TimeSeriesTransformerModel, TimeSeriesTransformerPreTrainedModel, ) else: import sys UpperCAmelCase__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
5
from __future__ import annotations from collections.abc import Callable UpperCAmelCase__ = list[list[float | int]] def UpperCAmelCase_ ( __snake_case , __snake_case ) -> Matrix: """simple docstring""" _lowercase =len(__snake_case ) _lowercase =[[0 for _ in range(size + 1 )] for _ in range(__snake_case )] _lowercase =42 _lowercase =42 _lowercase =42 _lowercase =42 _lowercase =42 _lowercase =42 for row in range(__snake_case ): for col in range(__snake_case ): _lowercase =matrix[row][col] _lowercase =vector[row][0] _lowercase =0 _lowercase =0 while row < size and col < size: # pivoting _lowercase =max((abs(augmented[rowa][col] ), rowa) for rowa in range(__snake_case , __snake_case ) )[ 1 ] if augmented[pivot_row][col] == 0: col += 1 continue else: _lowercase , _lowercase =augmented[pivot_row], augmented[row] for rowa in range(row + 1 , __snake_case ): _lowercase =augmented[rowa][col] / augmented[row][col] _lowercase =0 for cola in range(col + 1 , size + 1 ): augmented[rowa][cola] -= augmented[row][cola] * ratio row += 1 col += 1 # back substitution for col in range(1 , __snake_case ): for row in range(__snake_case ): _lowercase =augmented[row][col] / augmented[col][col] for cola in range(__snake_case , size + 1 ): augmented[row][cola] -= augmented[col][cola] * ratio # round to get rid of numbers like 2.000000000000004 return [ [round(augmented[row][size] / augmented[row][row] , 10 )] for row in range(__snake_case ) ] def UpperCAmelCase_ ( __snake_case ) -> Callable[[int], int]: """simple docstring""" _lowercase =len(__snake_case ) _lowercase =[[0 for _ in range(__snake_case )] for _ in range(__snake_case )] _lowercase =[[0] for _ in range(__snake_case )] _lowercase =42 _lowercase =42 _lowercase =42 _lowercase =42 for x_val, y_val in enumerate(__snake_case ): for col in range(__snake_case ): _lowercase =(x_val + 1) ** (size - col - 1) _lowercase =y_val _lowercase =solve(__snake_case , __snake_case ) def interpolated_func(__snake_case ) -> int: return sum( round(coeffs[x_val][0] ) * (var ** (size - x_val - 1)) for x_val in range(__snake_case ) ) return interpolated_func def UpperCAmelCase_ ( __snake_case ) -> int: """simple docstring""" return ( 1 - variable + variable**2 - variable**3 + variable**4 - variable**5 + variable**6 - variable**7 + variable**8 - variable**9 + variable**10 ) def UpperCAmelCase_ ( __snake_case = question_function , __snake_case = 10 ) -> int: """simple docstring""" _lowercase =[func(__snake_case ) for x_val in range(1 , order + 1 )] _lowercase =[ interpolate(data_points[:max_coeff] ) for max_coeff in range(1 , order + 1 ) ] _lowercase =0 _lowercase =42 _lowercase =42 for poly in polynomials: _lowercase =1 while func(__snake_case ) == poly(__snake_case ): x_val += 1 ret += poly(__snake_case ) return ret if __name__ == "__main__": print(f'''{solution() = }''')
5
1
import tempfile import unittest from transformers import TaConfig, is_torch_available from transformers.testing_utils import ( require_sentencepiece, require_tokenizers, require_torch, slow, torch_device, ) from ...generation.test_utils import GenerationTesterMixin from ...test_modeling_common import ModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import AutoTokenizer, UMTaForConditionalGeneration, UMTaForQuestionAnswering, UMTaModel class lowerCamelCase__ : def __init__(self , UpperCAmelCase , UpperCAmelCase=9_9 , UpperCAmelCase=1_3 , UpperCAmelCase=7 , UpperCAmelCase=9 , UpperCAmelCase=True , UpperCAmelCase=True , UpperCAmelCase=False , UpperCAmelCase=3_2 , UpperCAmelCase=5 , UpperCAmelCase=4 , UpperCAmelCase=3_7 , UpperCAmelCase=8 , UpperCAmelCase=0.1 , UpperCAmelCase=0.002 , UpperCAmelCase=1 , UpperCAmelCase=0 , UpperCAmelCase=0 , UpperCAmelCase=None , UpperCAmelCase=None , ) -> Union[str, Any]: _lowercase =parent _lowercase =batch_size _lowercase =encoder_seq_length _lowercase =decoder_seq_length # For common tests _lowercase =self.decoder_seq_length _lowercase =is_training _lowercase =use_attention_mask _lowercase =use_labels _lowercase =vocab_size _lowercase =hidden_size _lowercase =num_hidden_layers _lowercase =num_attention_heads _lowercase =d_ff _lowercase =relative_attention_num_buckets _lowercase =dropout_rate _lowercase =initializer_factor _lowercase =eos_token_id _lowercase =pad_token_id _lowercase =decoder_start_token_id _lowercase =None _lowercase =decoder_layers def __A (self ) -> List[str]: return TaConfig.from_pretrained('''google/umt5-base''' ) def __A (self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase=None , UpperCAmelCase=None , UpperCAmelCase=None , UpperCAmelCase=None , UpperCAmelCase=None , ) -> Optional[int]: if attention_mask is None: _lowercase =input_ids.ne(config.pad_token_id ) if decoder_attention_mask is None: _lowercase =decoder_input_ids.ne(config.pad_token_id ) if head_mask is None: _lowercase =torch.ones(config.num_hidden_layers , config.num_attention_heads , device=UpperCAmelCase ) if decoder_head_mask is None: _lowercase =torch.ones(config.num_decoder_layers , config.num_attention_heads , device=UpperCAmelCase ) if cross_attn_head_mask is None: _lowercase =torch.ones( config.num_decoder_layers , config.num_attention_heads , device=UpperCAmelCase ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": decoder_attention_mask, "head_mask": head_mask, "decoder_head_mask": decoder_head_mask, "cross_attn_head_mask": cross_attn_head_mask, } def __A (self ) -> str: _lowercase =ids_tensor([self.batch_size, self.encoder_seq_length] , self.vocab_size ) _lowercase =ids_tensor([self.batch_size, self.decoder_seq_length] , self.vocab_size ) # we need to clamp the input ids here to avoid having pad token in between # this is because for NllbMoe the position_ids are prepared such that # all pad tokens have pos id = 2 and rest are between 2..seq_length # and the seq_length here is seq_length - num_pad_tokens # but when using past, there is no way of knowing if the past input ids had # pad tokens in them, which results in incorrect seq_lenth and which in turn results in # position_ids being off by num_pad_tokens in past input _lowercase =input_ids.clamp(self.pad_token_id + 1 ) _lowercase =decoder_input_ids.clamp(self.pad_token_id + 1 ) _lowercase =self.get_config() _lowercase =config.num_attention_heads _lowercase =self.prepare_inputs_dict(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) return config, input_dict def __A (self ) -> Dict: _lowercase , _lowercase =self.prepare_config_and_inputs() return config, inputs_dict def __A (self ) -> Dict: return TaConfig( vocab_size=1_6_6 , d_model=self.hidden_size , d_ff=self.d_ff , d_kv=self.hidden_size // self.num_attention_heads , num_layers=self.num_hidden_layers , num_decoder_layers=self.decoder_layers , num_heads=self.num_attention_heads , relative_attention_num_buckets=self.relative_attention_num_buckets , dropout_rate=self.dropout_rate , initializer_factor=self.initializer_factor , eos_token_id=self.eos_token_id , bos_token_id=self.pad_token_id , pad_token_id=self.pad_token_id , decoder_start_token_id=self.decoder_start_token_id , ) def __A (self ) -> List[str]: return TaConfig( vocab_size=self.vocab_size , d_model=self.hidden_size , d_ff=self.d_ff , d_kv=self.hidden_size // self.num_attention_heads , num_layers=self.num_hidden_layers , num_decoder_layers=self.decoder_layers , num_heads=self.num_attention_heads , relative_attention_num_buckets=self.relative_attention_num_buckets , dropout_rate=self.dropout_rate , initializer_factor=self.initializer_factor , eos_token_id=self.eos_token_id , bos_token_id=self.pad_token_id , pad_token_id=self.pad_token_id , decoder_start_token_id=self.decoder_start_token_id , ) def __A (self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , ) -> Optional[Any]: _lowercase =UMTaModel(config=UpperCAmelCase ) model.to(UpperCAmelCase ) model.eval() _lowercase =model( input_ids=UpperCAmelCase , decoder_input_ids=UpperCAmelCase , attention_mask=UpperCAmelCase , decoder_attention_mask=UpperCAmelCase , ) _lowercase =model(input_ids=UpperCAmelCase , decoder_input_ids=UpperCAmelCase ) _lowercase =result.last_hidden_state _lowercase =result.past_key_values _lowercase =result.encoder_last_hidden_state self.parent.assertEqual(encoder_output.size() , (self.batch_size, self.encoder_seq_length, self.hidden_size) ) self.parent.assertEqual(decoder_output.size() , (self.batch_size, self.decoder_seq_length, self.hidden_size) ) # There should be `num_layers` key value embeddings stored in decoder_past self.parent.assertEqual(len(UpperCAmelCase ) , config.num_layers ) # There should be a self attn key, a self attn value, a cross attn key and a cross attn value stored in each decoder_past tuple self.parent.assertEqual(len(decoder_past[0] ) , 4 ) def __A (self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , ) -> Optional[int]: _lowercase =UMTaModel(config=UpperCAmelCase ).get_decoder().to(UpperCAmelCase ).eval() # first forward pass _lowercase =model(UpperCAmelCase , use_cache=UpperCAmelCase ) _lowercase =model(UpperCAmelCase ) _lowercase =model(UpperCAmelCase , use_cache=UpperCAmelCase ) self.parent.assertTrue(len(UpperCAmelCase ) == len(UpperCAmelCase ) ) self.parent.assertTrue(len(UpperCAmelCase ) == len(UpperCAmelCase ) + 1 ) _lowercase , _lowercase =outputs.to_tuple() # create hypothetical next token and extent to next_input_ids _lowercase =ids_tensor((self.batch_size, 1) , config.vocab_size ) # append to next input_ids and _lowercase =torch.cat([input_ids, next_tokens] , dim=-1 ) _lowercase =model(UpperCAmelCase )['''last_hidden_state'''] _lowercase =model(UpperCAmelCase , past_key_values=UpperCAmelCase )['''last_hidden_state'''] # select random slice _lowercase =ids_tensor((1,) , output_from_past.shape[-1] ).item() _lowercase =output_from_no_past[:, -1, random_slice_idx].detach() _lowercase =output_from_past[:, 0, random_slice_idx].detach() # test that outputs are equal for slice self.parent.assertTrue(torch.allclose(UpperCAmelCase , UpperCAmelCase , atol=1e-3 ) ) def __A (self , UpperCAmelCase , UpperCAmelCase , ) -> Optional[int]: _lowercase =UMTaModel(config=UpperCAmelCase ).to(UpperCAmelCase ).half().eval() _lowercase =model(**UpperCAmelCase )['''last_hidden_state'''] self.parent.assertFalse(torch.isnan(UpperCAmelCase ).any().item() ) @require_torch class lowerCamelCase__ ( lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , unittest.TestCase): SCREAMING_SNAKE_CASE__ = ( (UMTaModel, UMTaForConditionalGeneration, UMTaForQuestionAnswering) if is_torch_available() else () ) SCREAMING_SNAKE_CASE__ = (UMTaForConditionalGeneration,) if is_torch_available() else () SCREAMING_SNAKE_CASE__ = ( { '''conversational''': UMTaForConditionalGeneration, '''feature-extraction''': UMTaModel, '''summarization''': UMTaForConditionalGeneration, '''text2text-generation''': UMTaForConditionalGeneration, '''translation''': UMTaForConditionalGeneration, '''question-answering''': UMTaForQuestionAnswering, } if is_torch_available() else {} ) SCREAMING_SNAKE_CASE__ = True SCREAMING_SNAKE_CASE__ = False SCREAMING_SNAKE_CASE__ = False SCREAMING_SNAKE_CASE__ = True SCREAMING_SNAKE_CASE__ = True # The small UMT5 model needs higher percentages for CPU/MP tests SCREAMING_SNAKE_CASE__ = [0.8, 0.9] def __A (self ) -> Dict: _lowercase =UMTaModelTester(self ) @unittest.skip('''Test has a segmentation fault on torch 1.8.0''' ) def __A (self ) -> Optional[int]: _lowercase =self.model_tester.prepare_config_and_inputs() _lowercase =UMTaModel(config_and_inputs[0] ).to(UpperCAmelCase ) with tempfile.TemporaryDirectory() as tmpdirname: torch.onnx.export( UpperCAmelCase , (config_and_inputs[1], config_and_inputs[3], config_and_inputs[2]) , f"{tmpdirname}/t5_test.onnx" , export_params=UpperCAmelCase , opset_version=9 , input_names=['''input_ids''', '''decoder_input_ids'''] , ) @unittest.skipIf(torch_device == '''cpu''' , '''Cant do half precision''' ) def __A (self ) -> List[Any]: _lowercase =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model_fpaa_forward(*UpperCAmelCase ) def __A (self ) -> Dict: _lowercase =['''encoder_attentions''', '''decoder_attentions''', '''cross_attentions'''] _lowercase =self.model_tester.prepare_config_and_inputs() _lowercase =config_and_inputs[0] _lowercase =UMTaForConditionalGeneration(UpperCAmelCase ).eval() model.to(UpperCAmelCase ) _lowercase ={ '''head_mask''': torch.zeros(config.num_layers , config.num_heads , device=UpperCAmelCase ), '''decoder_head_mask''': torch.zeros(config.num_decoder_layers , config.num_heads , device=UpperCAmelCase ), '''cross_attn_head_mask''': torch.zeros(config.num_decoder_layers , config.num_heads , device=UpperCAmelCase ), } for attn_name, (name, mask) in zip(UpperCAmelCase , head_masking.items() ): _lowercase ={name: mask} # Explicitly pass decoder_head_mask as it is required from T5 model when head_mask specified if name == "head_mask": _lowercase =torch.ones( config.num_decoder_layers , config.num_heads , device=UpperCAmelCase ) _lowercase =model.generate( config_and_inputs[1]['''input_ids'''] , num_beams=1 , max_length=3 , output_attentions=UpperCAmelCase , return_dict_in_generate=UpperCAmelCase , **UpperCAmelCase , ) # We check the state of decoder_attentions and cross_attentions just from the last step _lowercase =out[attn_name] if attn_name == attention_names[0] else out[attn_name][-1] self.assertEqual(sum([w.sum().item() for w in attn_weights] ) , 0.0 ) @unittest.skip('''Does not work on the tiny model as we keep hitting edge cases.''' ) def __A (self ) -> Any: pass @require_torch @require_sentencepiece @require_tokenizers class lowerCamelCase__ ( unittest.TestCase): @slow @unittest.skip( '''Unless we stop stripping left and right by default for all special tokens, the expected ids obtained here will not match the original ones. Wait for https://github.com/huggingface/transformers/pull/23909 to be merged''' ) def __A (self ) -> Optional[int]: _lowercase =UMTaForConditionalGeneration.from_pretrained('''google/umt5-small''' , return_dict=UpperCAmelCase ).to(UpperCAmelCase ) _lowercase =AutoTokenizer.from_pretrained('''google/umt5-small''' , use_fast=UpperCAmelCase , legacy=UpperCAmelCase ) _lowercase =[ '''Bonjour monsieur <extra_id_0> bien <extra_id_1>.''', '''No se como puedo <extra_id_0>.''', '''This is the reason why we <extra_id_0> them.''', '''The <extra_id_0> walks in <extra_id_1>, seats''', '''A <extra_id_0> walks into a bar and orders a <extra_id_1> with <extra_id_2> pinch of <extra_id_3>.''', ] _lowercase =tokenizer(UpperCAmelCase , return_tensors='''pt''' , padding=UpperCAmelCase ).input_ids # fmt: off _lowercase =torch.tensor( [ [ 3_8_5_3_0, 2_1_0_7_0_3, 2_5_6_2_9_9, 1_4_1_0, 2_5_6_2_9_8, 2_7_4, 1, 0,0, 0, 0, 0, 0, 0, 0, 0,0, 0], [ 8_2_6, 3_2_1, 6_7_1, 2_5_9_2_2, 2_5_6_2_9_9, 2_7_4, 1, 0,0, 0, 0, 0, 0, 0, 0, 0,0, 0], [ 1_4_6_0, 3_3_9, 3_1_2, 1_9_0_1_4, 1_0_6_2_0, 7_5_8, 2_5_6_2_9_9, 2_3_5_5,2_7_4, 1, 0, 0, 0, 0, 0, 0,0, 0], [ 5_1_7, 2_5_6_2_9_9, 1_4_8_6_9, 2_8_1, 3_0_1, 2_5_6_2_9_8, 2_7_5, 1_1_9_9_8_3,1, 0, 0, 0, 0, 0, 0, 0,0, 0], [ 3_2_0, 2_5_6_2_9_9, 1_4_8_6_9, 2_8_1, 2_2_3_4, 2_8_9, 2_2_7_5, 3_3_3,6_1_3_9_1, 2_8_9, 2_5_6_2_9_8, 5_4_3, 2_5_6_2_9_7, 1_6_8_7_1_4, 3_2_9, 2_5_6_2_9_6,2_7_4, 1], ] ) # fmt: on torch.testing.assert_allclose(UpperCAmelCase , UpperCAmelCase ) _lowercase =model.generate(input_ids.to(UpperCAmelCase ) ) _lowercase =[ '''<pad><extra_id_0> et<extra_id_1> [eod] <extra_id_2><extra_id_55>.. [eod] 💐 💐 💐 💐 💐 💐 💐 💐 💐 💐 💐 <extra_id_56>ajšietosto<extra_id_56>lleux<extra_id_19><extra_id_6>ajšie</s>''', '''<pad><extra_id_0>.<extra_id_1>.,<0x0A>...spech <0x0A><extra_id_20> <extra_id_21></s><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad>''', '''<pad><extra_id_0> are not going to be a part of the world. We are not going to be a part of<extra_id_1> and<extra_id_2><0x0A><extra_id_48>.<extra_id_48></s><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad>''', '''<pad><extra_id_0> door<extra_id_1>, the door<extra_id_2> 피해[/</s><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad>''', '''<pad><extra_id_0>nyone who<extra_id_1> drink<extra_id_2> a<extra_id_3> alcohol<extra_id_4> A<extra_id_5> A. This<extra_id_6> I<extra_id_7><extra_id_52><extra_id_53></s><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad>''', ] _lowercase =tokenizer.batch_decode(UpperCAmelCase ) self.assertEqual(UpperCAmelCase , UpperCAmelCase )
5
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available UpperCAmelCase__ = { '''configuration_xlm''': ['''XLM_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''XLMConfig''', '''XLMOnnxConfig'''], '''tokenization_xlm''': ['''XLMTokenizer'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ '''XLM_PRETRAINED_MODEL_ARCHIVE_LIST''', '''XLMForMultipleChoice''', '''XLMForQuestionAnswering''', '''XLMForQuestionAnsweringSimple''', '''XLMForSequenceClassification''', '''XLMForTokenClassification''', '''XLMModel''', '''XLMPreTrainedModel''', '''XLMWithLMHeadModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ '''TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFXLMForMultipleChoice''', '''TFXLMForQuestionAnsweringSimple''', '''TFXLMForSequenceClassification''', '''TFXLMForTokenClassification''', '''TFXLMMainLayer''', '''TFXLMModel''', '''TFXLMPreTrainedModel''', '''TFXLMWithLMHeadModel''', ] if TYPE_CHECKING: from .configuration_xlm import XLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XLMConfig, XLMOnnxConfig from .tokenization_xlm import XLMTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xlm import ( XLM_PRETRAINED_MODEL_ARCHIVE_LIST, XLMForMultipleChoice, XLMForQuestionAnswering, XLMForQuestionAnsweringSimple, XLMForSequenceClassification, XLMForTokenClassification, XLMModel, XLMPreTrainedModel, XLMWithLMHeadModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_xlm import ( TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFXLMForMultipleChoice, TFXLMForQuestionAnsweringSimple, TFXLMForSequenceClassification, TFXLMForTokenClassification, TFXLMMainLayer, TFXLMModel, TFXLMPreTrainedModel, TFXLMWithLMHeadModel, ) else: import sys UpperCAmelCase__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
5
1
import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = '''▁''' UpperCAmelCase__ = {'''vocab_file''': '''sentencepiece.bpe.model'''} UpperCAmelCase__ = { '''vocab_file''': { '''xlm-roberta-base''': '''https://huggingface.co/xlm-roberta-base/resolve/main/sentencepiece.bpe.model''', '''xlm-roberta-large''': '''https://huggingface.co/xlm-roberta-large/resolve/main/sentencepiece.bpe.model''', '''xlm-roberta-large-finetuned-conll02-dutch''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll02-dutch/resolve/main/sentencepiece.bpe.model''' ), '''xlm-roberta-large-finetuned-conll02-spanish''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll02-spanish/resolve/main/sentencepiece.bpe.model''' ), '''xlm-roberta-large-finetuned-conll03-english''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll03-english/resolve/main/sentencepiece.bpe.model''' ), '''xlm-roberta-large-finetuned-conll03-german''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll03-german/resolve/main/sentencepiece.bpe.model''' ), } } UpperCAmelCase__ = { '''xlm-roberta-base''': 512, '''xlm-roberta-large''': 512, '''xlm-roberta-large-finetuned-conll02-dutch''': 512, '''xlm-roberta-large-finetuned-conll02-spanish''': 512, '''xlm-roberta-large-finetuned-conll03-english''': 512, '''xlm-roberta-large-finetuned-conll03-german''': 512, } class lowerCamelCase__ ( lowerCAmelCase): SCREAMING_SNAKE_CASE__ = VOCAB_FILES_NAMES SCREAMING_SNAKE_CASE__ = PRETRAINED_VOCAB_FILES_MAP SCREAMING_SNAKE_CASE__ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES SCREAMING_SNAKE_CASE__ = ['''input_ids''', '''attention_mask'''] def __init__(self , UpperCAmelCase , UpperCAmelCase="<s>" , UpperCAmelCase="</s>" , UpperCAmelCase="</s>" , UpperCAmelCase="<s>" , UpperCAmelCase="<unk>" , UpperCAmelCase="<pad>" , UpperCAmelCase="<mask>" , UpperCAmelCase = None , **UpperCAmelCase , ) -> None: # Mask token behave like a normal word, i.e. include the space before it _lowercase =AddedToken(UpperCAmelCase , lstrip=UpperCAmelCase , rstrip=UpperCAmelCase ) if isinstance(UpperCAmelCase , UpperCAmelCase ) else mask_token _lowercase ={} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=UpperCAmelCase , eos_token=UpperCAmelCase , unk_token=UpperCAmelCase , sep_token=UpperCAmelCase , cls_token=UpperCAmelCase , pad_token=UpperCAmelCase , mask_token=UpperCAmelCase , sp_model_kwargs=self.sp_model_kwargs , **UpperCAmelCase , ) _lowercase =spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(UpperCAmelCase ) ) _lowercase =vocab_file # Original fairseq vocab and spm vocab must be "aligned": # Vocab | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 # -------- | ------- | ------- | ------ | ------- | --- | --- | --- | ----- | ----- | ---- # fairseq | '<s>' | '<pad>' | '</s>' | '<unk>' | ',' | '.' | '▁' | 's' | '▁de' | '-' # spm | '<unk>' | '<s>' | '</s>' | ',' | '.' | '▁' | 's' | '▁de' | '-' | '▁a' # Mimic fairseq token-to-id alignment for the first 4 token _lowercase ={'''<s>''': 0, '''<pad>''': 1, '''</s>''': 2, '''<unk>''': 3} # The first "real" token "," has position 4 in the original fairseq vocab and position 3 in the spm vocab _lowercase =1 _lowercase =len(self.sp_model ) + self.fairseq_offset _lowercase ={v: k for k, v in self.fairseq_tokens_to_ids.items()} def __getstate__(self ) -> Optional[int]: _lowercase =self.__dict__.copy() _lowercase =None _lowercase =self.sp_model.serialized_model_proto() return state def __setstate__(self , UpperCAmelCase ) -> List[str]: _lowercase =d # for backward compatibility if not hasattr(self , '''sp_model_kwargs''' ): _lowercase ={} _lowercase =spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.LoadFromSerializedProto(self.sp_model_proto ) def __A (self , UpperCAmelCase , UpperCAmelCase = None ) -> List[int]: if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] _lowercase =[self.cls_token_id] _lowercase =[self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def __A (self , UpperCAmelCase , UpperCAmelCase = None , UpperCAmelCase = False ) -> List[int]: if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=UpperCAmelCase , token_ids_a=UpperCAmelCase , already_has_special_tokens=UpperCAmelCase ) if token_ids_a is None: return [1] + ([0] * len(UpperCAmelCase )) + [1] return [1] + ([0] * len(UpperCAmelCase )) + [1, 1] + ([0] * len(UpperCAmelCase )) + [1] def __A (self , UpperCAmelCase , UpperCAmelCase = None ) -> List[int]: _lowercase =[self.sep_token_id] _lowercase =[self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] @property def __A (self ) -> List[Any]: return len(self.sp_model ) + self.fairseq_offset + 1 # Add the <mask> token def __A (self ) -> Any: _lowercase ={self.convert_ids_to_tokens(UpperCAmelCase ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __A (self , UpperCAmelCase ) -> List[str]: return self.sp_model.encode(UpperCAmelCase , out_type=UpperCAmelCase ) def __A (self , UpperCAmelCase ) -> Union[str, Any]: if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] _lowercase =self.sp_model.PieceToId(UpperCAmelCase ) # Need to return unknown token if the SP model returned 0 return spm_id + self.fairseq_offset if spm_id else self.unk_token_id def __A (self , UpperCAmelCase ) -> str: if index in self.fairseq_ids_to_tokens: return self.fairseq_ids_to_tokens[index] return self.sp_model.IdToPiece(index - self.fairseq_offset ) def __A (self , UpperCAmelCase ) -> List[Any]: _lowercase =''''''.join(UpperCAmelCase ).replace(UpperCAmelCase , ''' ''' ).strip() return out_string def __A (self , UpperCAmelCase , UpperCAmelCase = None ) -> Tuple[str]: if not os.path.isdir(UpperCAmelCase ): logger.error(f"Vocabulary path ({save_directory}) should be a directory" ) return _lowercase =os.path.join( UpperCAmelCase , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(UpperCAmelCase ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , UpperCAmelCase ) elif not os.path.isfile(self.vocab_file ): with open(UpperCAmelCase , '''wb''' ) as fi: _lowercase =self.sp_model.serialized_model_proto() fi.write(UpperCAmelCase ) return (out_vocab_file,)
5
# Copyright 2023 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available UpperCAmelCase__ = { '''configuration_efficientnet''': [ '''EFFICIENTNET_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''EfficientNetConfig''', '''EfficientNetOnnxConfig''', ] } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = ['''EfficientNetImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ '''EFFICIENTNET_PRETRAINED_MODEL_ARCHIVE_LIST''', '''EfficientNetForImageClassification''', '''EfficientNetModel''', '''EfficientNetPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_efficientnet import ( EFFICIENTNET_PRETRAINED_CONFIG_ARCHIVE_MAP, EfficientNetConfig, EfficientNetOnnxConfig, ) try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_efficientnet import EfficientNetImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_efficientnet import ( EFFICIENTNET_PRETRAINED_MODEL_ARCHIVE_LIST, EfficientNetForImageClassification, EfficientNetModel, EfficientNetPreTrainedModel, ) else: import sys UpperCAmelCase__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure)
5
1
def UpperCAmelCase_ ( __snake_case , __snake_case ) -> Optional[int]: """simple docstring""" _lowercase ='''''' for i in table: res += inp[i - 1] return res def UpperCAmelCase_ ( __snake_case ) -> int: """simple docstring""" return data[1:] + data[0] def UpperCAmelCase_ ( __snake_case , __snake_case ) -> Dict: """simple docstring""" _lowercase ='''''' for i in range(len(__snake_case ) ): if a[i] == b[i]: res += "0" else: res += "1" return res def UpperCAmelCase_ ( __snake_case , __snake_case ) -> List[Any]: """simple docstring""" _lowercase =int('''0b''' + data[0] + data[-1] , 2 ) _lowercase =int('''0b''' + data[1:3] , 2 ) return bin(s[row][col] )[2:] def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case ) -> Dict: """simple docstring""" _lowercase =message[:4] _lowercase =message[4:] _lowercase =apply_table(__snake_case , __snake_case ) _lowercase =xor(__snake_case , __snake_case ) _lowercase =apply_sbox(__snake_case , temp[:4] ) # noqa: E741 _lowercase =apply_sbox(__snake_case , temp[4:] ) _lowercase ='''0''' * (2 - len(__snake_case )) + l # noqa: E741 _lowercase ='''0''' * (2 - len(__snake_case )) + r _lowercase =apply_table(l + r , __snake_case ) _lowercase =xor(__snake_case , __snake_case ) return temp + right if __name__ == "__main__": UpperCAmelCase__ = input('''Enter 10 bit key: ''') UpperCAmelCase__ = input('''Enter 8 bit message: ''') UpperCAmelCase__ = [6, 3, 7, 4, 8, 5, 10, 9] UpperCAmelCase__ = [3, 5, 2, 7, 4, 10, 1, 9, 8, 6] UpperCAmelCase__ = [2, 4, 3, 1] UpperCAmelCase__ = [2, 6, 3, 1, 4, 8, 5, 7] UpperCAmelCase__ = [4, 1, 3, 5, 7, 2, 8, 6] UpperCAmelCase__ = [4, 1, 2, 3, 2, 3, 4, 1] UpperCAmelCase__ = [[1, 0, 3, 2], [3, 2, 1, 0], [0, 2, 1, 3], [3, 1, 3, 2]] UpperCAmelCase__ = [[0, 1, 2, 3], [2, 0, 1, 3], [3, 0, 1, 0], [2, 1, 0, 3]] # key generation UpperCAmelCase__ = apply_table(key, paa_table) UpperCAmelCase__ = temp[:5] UpperCAmelCase__ = temp[5:] UpperCAmelCase__ = left_shift(left) UpperCAmelCase__ = left_shift(right) UpperCAmelCase__ = apply_table(left + right, pa_table) UpperCAmelCase__ = left_shift(left) UpperCAmelCase__ = left_shift(right) UpperCAmelCase__ = left_shift(left) UpperCAmelCase__ = left_shift(right) UpperCAmelCase__ = apply_table(left + right, pa_table) # encryption UpperCAmelCase__ = apply_table(message, IP) UpperCAmelCase__ = function(expansion, sa, sa, keya, temp) UpperCAmelCase__ = temp[4:] + temp[:4] UpperCAmelCase__ = function(expansion, sa, sa, keya, temp) UpperCAmelCase__ = apply_table(temp, IP_inv) print('''Cipher text is:''', CT) # decryption UpperCAmelCase__ = apply_table(CT, IP) UpperCAmelCase__ = function(expansion, sa, sa, keya, temp) UpperCAmelCase__ = temp[4:] + temp[:4] UpperCAmelCase__ = function(expansion, sa, sa, keya, temp) UpperCAmelCase__ = apply_table(temp, IP_inv) print('''Plain text after decypting is:''', PT)
5
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available UpperCAmelCase__ = { '''configuration_timesformer''': ['''TIMESFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''TimesformerConfig'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ '''TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TimesformerModel''', '''TimesformerForVideoClassification''', '''TimesformerPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_timesformer import TIMESFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TimesformerConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_timesformer import ( TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TimesformerForVideoClassification, TimesformerModel, TimesformerPreTrainedModel, ) else: import sys UpperCAmelCase__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
5
1
from __future__ import annotations import typing from collections import Counter def UpperCAmelCase_ ( __snake_case ) -> typing.Counter[int]: """simple docstring""" _lowercase =Counter() for base in range(1 , max_perimeter + 1 ): for perpendicular in range(__snake_case , max_perimeter + 1 ): _lowercase =(base * base + perpendicular * perpendicular) ** 0.5 if hypotenuse == int(__snake_case ): _lowercase =int(base + perpendicular + hypotenuse ) if perimeter > max_perimeter: continue triplets[perimeter] += 1 return triplets def UpperCAmelCase_ ( __snake_case = 1000 ) -> int: """simple docstring""" _lowercase =pythagorean_triple(__snake_case ) return triplets.most_common(1 )[0][0] if __name__ == "__main__": print(f'''Perimeter {solution()} has maximum solutions''')
5
def UpperCAmelCase_ ( __snake_case , __snake_case ) -> List[Any]: """simple docstring""" if b == 0: return 1 if (b % 2) == 0: return actual_power(__snake_case , int(b / 2 ) ) * actual_power(__snake_case , int(b / 2 ) ) else: return a * actual_power(__snake_case , int(b / 2 ) ) * actual_power(__snake_case , int(b / 2 ) ) def UpperCAmelCase_ ( __snake_case , __snake_case ) -> float: """simple docstring""" if b < 0: return 1 / actual_power(__snake_case , __snake_case ) return actual_power(__snake_case , __snake_case ) if __name__ == "__main__": print(power(-2, -3))
5
1
import numpy as np import pandas as pd from sklearn.preprocessing import MinMaxScaler from tensorflow.keras.layers import LSTM, Dense from tensorflow.keras.models import Sequential if __name__ == "__main__": UpperCAmelCase__ = pd.read_csv('''sample_data.csv''', header=None) UpperCAmelCase__ = df.shape[:1][0] # If you're using some other dataset input the target column UpperCAmelCase__ = df.iloc[:, 1:2] UpperCAmelCase__ = actual_data.values.reshape(len_data, 1) UpperCAmelCase__ = MinMaxScaler().fit_transform(actual_data) UpperCAmelCase__ = 10 UpperCAmelCase__ = 5 UpperCAmelCase__ = 20 UpperCAmelCase__ = len_data - periods * look_back UpperCAmelCase__ = actual_data[:division] UpperCAmelCase__ = actual_data[division - look_back :] UpperCAmelCase__ ,UpperCAmelCase__ = [], [] UpperCAmelCase__ ,UpperCAmelCase__ = [], [] for i in range(0, len(train_data) - forward_days - look_back + 1): train_x.append(train_data[i : i + look_back]) train_y.append(train_data[i + look_back : i + look_back + forward_days]) for i in range(0, len(test_data) - forward_days - look_back + 1): test_x.append(test_data[i : i + look_back]) test_y.append(test_data[i + look_back : i + look_back + forward_days]) UpperCAmelCase__ = np.array(train_x) UpperCAmelCase__ = np.array(test_x) UpperCAmelCase__ = np.array([list(i.ravel()) for i in train_y]) UpperCAmelCase__ = np.array([list(i.ravel()) for i in test_y]) UpperCAmelCase__ = Sequential() model.add(LSTM(128, input_shape=(look_back, 1), return_sequences=True)) model.add(LSTM(64, input_shape=(128, 1))) model.add(Dense(forward_days)) model.compile(loss='''mean_squared_error''', optimizer='''adam''') UpperCAmelCase__ = model.fit( x_train, y_train, epochs=150, verbose=1, shuffle=True, batch_size=4 ) UpperCAmelCase__ = model.predict(x_test)
5
from typing import Optional from torch import nn from .transformer_ad import TransformeraDModel, TransformeraDModelOutput class lowerCamelCase__ ( nn.Module): def __init__(self , UpperCAmelCase = 1_6 , UpperCAmelCase = 8_8 , UpperCAmelCase = None , UpperCAmelCase = 1 , UpperCAmelCase = 0.0 , UpperCAmelCase = 3_2 , UpperCAmelCase = None , UpperCAmelCase = False , UpperCAmelCase = None , UpperCAmelCase = None , UpperCAmelCase = "geglu" , UpperCAmelCase = None , ) -> Any: super().__init__() _lowercase =nn.ModuleList( [ TransformeraDModel( num_attention_heads=UpperCAmelCase , attention_head_dim=UpperCAmelCase , in_channels=UpperCAmelCase , num_layers=UpperCAmelCase , dropout=UpperCAmelCase , norm_num_groups=UpperCAmelCase , cross_attention_dim=UpperCAmelCase , attention_bias=UpperCAmelCase , sample_size=UpperCAmelCase , num_vector_embeds=UpperCAmelCase , activation_fn=UpperCAmelCase , num_embeds_ada_norm=UpperCAmelCase , ) for _ in range(2 ) ] ) # Variables that can be set by a pipeline: # The ratio of transformer1 to transformer2's output states to be combined during inference _lowercase =0.5 # The shape of `encoder_hidden_states` is expected to be # `(batch_size, condition_lengths[0]+condition_lengths[1], num_features)` _lowercase =[7_7, 2_5_7] # Which transformer to use to encode which condition. # E.g. `(1, 0)` means that we'll use `transformers[1](conditions[0])` and `transformers[0](conditions[1])` _lowercase =[1, 0] def __A (self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase=None , UpperCAmelCase=None , UpperCAmelCase=None , UpperCAmelCase = True , ) -> str: _lowercase =hidden_states _lowercase =[] _lowercase =0 # attention_mask is not used yet for i in range(2 ): # for each of the two transformers, pass the corresponding condition tokens _lowercase =encoder_hidden_states[:, tokens_start : tokens_start + self.condition_lengths[i]] _lowercase =self.transformer_index_for_condition[i] _lowercase =self.transformers[transformer_index]( UpperCAmelCase , encoder_hidden_states=UpperCAmelCase , timestep=UpperCAmelCase , cross_attention_kwargs=UpperCAmelCase , return_dict=UpperCAmelCase , )[0] encoded_states.append(encoded_state - input_states ) tokens_start += self.condition_lengths[i] _lowercase =encoded_states[0] * self.mix_ratio + encoded_states[1] * (1 - self.mix_ratio) _lowercase =output_states + input_states if not return_dict: return (output_states,) return TransformeraDModelOutput(sample=UpperCAmelCase )
5
1
from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = { '''facebook/convnextv2-tiny-1k-224''': '''https://huggingface.co/facebook/convnextv2-tiny-1k-224/resolve/main/config.json''', } class lowerCamelCase__ ( lowerCAmelCase , lowerCAmelCase): SCREAMING_SNAKE_CASE__ = '''convnextv2''' def __init__(self , UpperCAmelCase=3 , UpperCAmelCase=4 , UpperCAmelCase=4 , UpperCAmelCase=None , UpperCAmelCase=None , UpperCAmelCase="gelu" , UpperCAmelCase=0.02 , UpperCAmelCase=1e-12 , UpperCAmelCase=0.0 , UpperCAmelCase=2_2_4 , UpperCAmelCase=None , UpperCAmelCase=None , **UpperCAmelCase , ) -> Optional[Any]: super().__init__(**UpperCAmelCase ) _lowercase =num_channels _lowercase =patch_size _lowercase =num_stages _lowercase =[9_6, 1_9_2, 3_8_4, 7_6_8] if hidden_sizes is None else hidden_sizes _lowercase =[3, 3, 9, 3] if depths is None else depths _lowercase =hidden_act _lowercase =initializer_range _lowercase =layer_norm_eps _lowercase =drop_path_rate _lowercase =image_size _lowercase =['''stem'''] + [f"stage{idx}" for idx in range(1 , len(self.depths ) + 1 )] _lowercase , _lowercase =get_aligned_output_features_output_indices( out_features=UpperCAmelCase , out_indices=UpperCAmelCase , stage_names=self.stage_names )
5
import heapq as hq import math from collections.abc import Iterator class lowerCamelCase__ : def __init__(self , UpperCAmelCase ) -> Any: _lowercase =str(id_ ) _lowercase =None _lowercase =None _lowercase =[] _lowercase ={} # {vertex:distance} def __lt__(self , UpperCAmelCase ) -> List[str]: return self.key < other.key def __repr__(self ) -> str: return self.id def __A (self , UpperCAmelCase ) -> Dict: self.neighbors.append(UpperCAmelCase ) def __A (self , UpperCAmelCase , UpperCAmelCase ) -> Optional[Any]: _lowercase =weight def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case ) -> List[str]: """simple docstring""" graph[a - 1].add_neighbor(graph[b - 1] ) graph[b - 1].add_neighbor(graph[a - 1] ) # add the edges: graph[a - 1].add_edge(graph[b - 1] , __snake_case ) graph[b - 1].add_edge(graph[a - 1] , __snake_case ) def UpperCAmelCase_ ( __snake_case , __snake_case ) -> list: """simple docstring""" _lowercase =[] for u in graph: _lowercase =math.inf _lowercase =None _lowercase =0 _lowercase =graph[:] while q: _lowercase =min(__snake_case ) q.remove(__snake_case ) for v in u.neighbors: if (v in q) and (u.edges[v.id] < v.key): _lowercase =u _lowercase =u.edges[v.id] for i in range(1 , len(__snake_case ) ): a.append((int(graph[i].id ) + 1, int(graph[i].pi.id ) + 1) ) return a def UpperCAmelCase_ ( __snake_case , __snake_case ) -> Iterator[tuple]: """simple docstring""" for u in graph: _lowercase =math.inf _lowercase =None _lowercase =0 _lowercase =list(__snake_case ) hq.heapify(__snake_case ) while h: _lowercase =hq.heappop(__snake_case ) for v in u.neighbors: if (v in h) and (u.edges[v.id] < v.key): _lowercase =u _lowercase =u.edges[v.id] hq.heapify(__snake_case ) for i in range(1 , len(__snake_case ) ): yield (int(graph[i].id ) + 1, int(graph[i].pi.id ) + 1) def UpperCAmelCase_ ( ) -> None: """simple docstring""" if __name__ == "__main__": import doctest doctest.testmod()
5
1
def UpperCAmelCase_ ( __snake_case , __snake_case ) -> bool: """simple docstring""" _lowercase =len(__snake_case ) + 1 _lowercase =len(__snake_case ) + 1 # dp is a 2d matrix where dp[i][j] denotes whether prefix string of # length i of input_string matches with prefix string of length j of # given pattern. # "dp" stands for dynamic programming. _lowercase =[[0 for i in range(__snake_case )] for j in range(__snake_case )] # since string of zero length match pattern of zero length _lowercase =1 # since pattern of zero length will never match with string of non-zero length for i in range(1 , __snake_case ): _lowercase =0 # since string of zero length will match with pattern where there # is at least one * alternatively for j in range(1 , __snake_case ): _lowercase =dp[0][j - 2] if pattern[j - 1] == '''*''' else 0 # now using bottom-up approach to find for all remaining lengths for i in range(1 , __snake_case ): for j in range(1 , __snake_case ): if input_string[i - 1] == pattern[j - 1] or pattern[j - 1] == ".": _lowercase =dp[i - 1][j - 1] elif pattern[j - 1] == "*": if dp[i][j - 2] == 1: _lowercase =1 elif pattern[j - 2] in (input_string[i - 1], "."): _lowercase =dp[i - 1][j] else: _lowercase =0 else: _lowercase =0 return bool(dp[-1][-1] ) if __name__ == "__main__": import doctest doctest.testmod() # inputing the strings # input_string = input("input a string :") # pattern = input("input a pattern :") UpperCAmelCase__ = '''aab''' UpperCAmelCase__ = '''c*a*b''' # using function to check whether given string matches the given pattern if match_pattern(input_string, pattern): print(f'''{input_string} matches the given pattern {pattern}''') else: print(f'''{input_string} does not match with the given pattern {pattern}''')
5
# flake8: noqa # Lint as: python3 UpperCAmelCase__ = [ '''VerificationMode''', '''Version''', '''disable_progress_bar''', '''enable_progress_bar''', '''is_progress_bar_enabled''', '''experimental''', ] from .info_utils import VerificationMode from .logging import disable_progress_bar, enable_progress_bar, is_progress_bar_enabled from .version import Version from .experimental import experimental
5
1
import os from typing import List, Optional, Union from ...image_processing_utils import BatchFeature from ...image_utils import ImageInput from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy from ...utils import TensorType from ..auto import AutoTokenizer class lowerCamelCase__ ( lowerCAmelCase): SCREAMING_SNAKE_CASE__ = ['''image_processor''', '''tokenizer'''] SCREAMING_SNAKE_CASE__ = '''BlipImageProcessor''' SCREAMING_SNAKE_CASE__ = '''AutoTokenizer''' def __init__(self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) -> Dict: super().__init__(UpperCAmelCase , UpperCAmelCase ) # add QFormer tokenizer _lowercase =qformer_tokenizer def __call__(self , UpperCAmelCase = None , UpperCAmelCase = None , UpperCAmelCase = True , UpperCAmelCase = False , UpperCAmelCase = None , UpperCAmelCase = None , UpperCAmelCase = 0 , UpperCAmelCase = None , UpperCAmelCase = None , UpperCAmelCase = False , UpperCAmelCase = False , UpperCAmelCase = False , UpperCAmelCase = False , UpperCAmelCase = False , UpperCAmelCase = True , UpperCAmelCase = None , **UpperCAmelCase , ) -> BatchFeature: if images is None and text is None: raise ValueError('''You have to specify at least images or text.''' ) _lowercase =BatchFeature() if text is not None: _lowercase =self.tokenizer( text=UpperCAmelCase , add_special_tokens=UpperCAmelCase , padding=UpperCAmelCase , truncation=UpperCAmelCase , max_length=UpperCAmelCase , stride=UpperCAmelCase , pad_to_multiple_of=UpperCAmelCase , return_attention_mask=UpperCAmelCase , return_overflowing_tokens=UpperCAmelCase , return_special_tokens_mask=UpperCAmelCase , return_offsets_mapping=UpperCAmelCase , return_token_type_ids=UpperCAmelCase , return_length=UpperCAmelCase , verbose=UpperCAmelCase , return_tensors=UpperCAmelCase , **UpperCAmelCase , ) encoding.update(UpperCAmelCase ) _lowercase =self.qformer_tokenizer( text=UpperCAmelCase , add_special_tokens=UpperCAmelCase , padding=UpperCAmelCase , truncation=UpperCAmelCase , max_length=UpperCAmelCase , stride=UpperCAmelCase , pad_to_multiple_of=UpperCAmelCase , return_attention_mask=UpperCAmelCase , return_overflowing_tokens=UpperCAmelCase , return_special_tokens_mask=UpperCAmelCase , return_offsets_mapping=UpperCAmelCase , return_token_type_ids=UpperCAmelCase , return_length=UpperCAmelCase , verbose=UpperCAmelCase , return_tensors=UpperCAmelCase , **UpperCAmelCase , ) _lowercase =qformer_text_encoding.pop('''input_ids''' ) _lowercase =qformer_text_encoding.pop('''attention_mask''' ) if images is not None: _lowercase =self.image_processor(UpperCAmelCase , return_tensors=UpperCAmelCase ) encoding.update(UpperCAmelCase ) return encoding def __A (self , *UpperCAmelCase , **UpperCAmelCase ) -> Any: return self.tokenizer.batch_decode(*UpperCAmelCase , **UpperCAmelCase ) def __A (self , *UpperCAmelCase , **UpperCAmelCase ) -> int: return self.tokenizer.decode(*UpperCAmelCase , **UpperCAmelCase ) @property # Copied from transformers.models.blip.processing_blip.BlipProcessor.model_input_names def __A (self ) -> List[Any]: _lowercase =self.tokenizer.model_input_names _lowercase =self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) ) def __A (self , UpperCAmelCase , **UpperCAmelCase ) -> Optional[Any]: if os.path.isfile(UpperCAmelCase ): raise ValueError(f"Provided path ({save_directory}) should be a directory, not a file" ) os.makedirs(UpperCAmelCase , exist_ok=UpperCAmelCase ) _lowercase =os.path.join(UpperCAmelCase , '''qformer_tokenizer''' ) self.qformer_tokenizer.save_pretrained(UpperCAmelCase ) return super().save_pretrained(UpperCAmelCase , **UpperCAmelCase ) @classmethod def __A (cls , UpperCAmelCase , **UpperCAmelCase ) -> Dict: _lowercase =AutoTokenizer.from_pretrained(UpperCAmelCase , subfolder='''qformer_tokenizer''' ) _lowercase =cls._get_arguments_from_pretrained(UpperCAmelCase , **UpperCAmelCase ) args.append(UpperCAmelCase ) return cls(*UpperCAmelCase )
5
import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = { '''microsoft/wavlm-base''': '''https://huggingface.co/microsoft/wavlm-base/resolve/main/config.json''', # See all WavLM models at https://huggingface.co/models?filter=wavlm } class lowerCamelCase__ ( lowerCAmelCase): SCREAMING_SNAKE_CASE__ = '''wavlm''' def __init__(self , UpperCAmelCase=3_2 , UpperCAmelCase=7_6_8 , UpperCAmelCase=1_2 , UpperCAmelCase=1_2 , UpperCAmelCase=3_0_7_2 , UpperCAmelCase="gelu" , UpperCAmelCase=0.1 , UpperCAmelCase=0.1 , UpperCAmelCase=0.1 , UpperCAmelCase=0.0 , UpperCAmelCase=0.1 , UpperCAmelCase=0.1 , UpperCAmelCase=0.02 , UpperCAmelCase=1e-5 , UpperCAmelCase="group" , UpperCAmelCase="gelu" , UpperCAmelCase=(5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2) , UpperCAmelCase=(5, 2, 2, 2, 2, 2, 2) , UpperCAmelCase=(1_0, 3, 3, 3, 3, 2, 2) , UpperCAmelCase=False , UpperCAmelCase=1_2_8 , UpperCAmelCase=1_6 , UpperCAmelCase=3_2_0 , UpperCAmelCase=8_0_0 , UpperCAmelCase=False , UpperCAmelCase=True , UpperCAmelCase=0.05 , UpperCAmelCase=1_0 , UpperCAmelCase=2 , UpperCAmelCase=0.0 , UpperCAmelCase=1_0 , UpperCAmelCase=3_2_0 , UpperCAmelCase=2 , UpperCAmelCase=0.1 , UpperCAmelCase=1_0_0 , UpperCAmelCase=2_5_6 , UpperCAmelCase=2_5_6 , UpperCAmelCase=0.1 , UpperCAmelCase="mean" , UpperCAmelCase=False , UpperCAmelCase=False , UpperCAmelCase=2_5_6 , UpperCAmelCase=(5_1_2, 5_1_2, 5_1_2, 5_1_2, 1_5_0_0) , UpperCAmelCase=(5, 3, 3, 1, 1) , UpperCAmelCase=(1, 2, 3, 1, 1) , UpperCAmelCase=5_1_2 , UpperCAmelCase=8_0 , UpperCAmelCase=0 , UpperCAmelCase=1 , UpperCAmelCase=2 , UpperCAmelCase=False , UpperCAmelCase=3 , UpperCAmelCase=2 , UpperCAmelCase=3 , UpperCAmelCase=None , **UpperCAmelCase , ) -> Optional[Any]: super().__init__(**UpperCAmelCase , pad_token_id=UpperCAmelCase , bos_token_id=UpperCAmelCase , eos_token_id=UpperCAmelCase ) _lowercase =hidden_size _lowercase =feat_extract_norm _lowercase =feat_extract_activation _lowercase =list(UpperCAmelCase ) _lowercase =list(UpperCAmelCase ) _lowercase =list(UpperCAmelCase ) _lowercase =conv_bias _lowercase =num_buckets _lowercase =max_bucket_distance _lowercase =num_conv_pos_embeddings _lowercase =num_conv_pos_embedding_groups _lowercase =len(self.conv_dim ) _lowercase =num_hidden_layers _lowercase =intermediate_size _lowercase =hidden_act _lowercase =num_attention_heads _lowercase =hidden_dropout _lowercase =attention_dropout _lowercase =activation_dropout _lowercase =feat_proj_dropout _lowercase =final_dropout _lowercase =layerdrop _lowercase =layer_norm_eps _lowercase =initializer_range _lowercase =num_ctc_classes _lowercase =vocab_size _lowercase =do_stable_layer_norm _lowercase =use_weighted_layer_sum _lowercase =classifier_proj_size if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( '''Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` ==''' ''' `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) =''' f" {len(self.conv_dim )}`, `len(config.conv_stride) = {len(self.conv_stride )}`," f" `len(config.conv_kernel) = {len(self.conv_kernel )}`." ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 _lowercase =apply_spec_augment _lowercase =mask_time_prob _lowercase =mask_time_length _lowercase =mask_time_min_masks _lowercase =mask_feature_prob _lowercase =mask_feature_length # parameters for pretraining with codevector quantized representations _lowercase =num_codevectors_per_group _lowercase =num_codevector_groups _lowercase =contrastive_logits_temperature _lowercase =num_negatives _lowercase =codevector_dim _lowercase =proj_codevector_dim _lowercase =diversity_loss_weight # ctc loss _lowercase =ctc_loss_reduction _lowercase =ctc_zero_infinity # adapter _lowercase =add_adapter _lowercase =adapter_kernel_size _lowercase =adapter_stride _lowercase =num_adapter_layers _lowercase =output_hidden_size or hidden_size # SequenceClassification-specific parameter. Feel free to ignore for other classes. _lowercase =classifier_proj_size # XVector-specific parameters. Feel free to ignore for other classes. _lowercase =list(UpperCAmelCase ) _lowercase =list(UpperCAmelCase ) _lowercase =list(UpperCAmelCase ) _lowercase =xvector_output_dim @property def __A (self ) -> int: return functools.reduce(operator.mul , self.conv_stride , 1 )
5
1
import itertools import string from collections.abc import Generator, Iterable def UpperCAmelCase_ ( __snake_case , __snake_case ) -> Generator[tuple[str, ...], None, None]: """simple docstring""" _lowercase =iter(__snake_case ) while True: _lowercase =tuple(itertools.islice(__snake_case , __snake_case ) ) if not chunk: return yield chunk def UpperCAmelCase_ ( __snake_case ) -> str: """simple docstring""" _lowercase =''''''.join([c.upper() for c in dirty if c in string.ascii_letters] ) _lowercase ='''''' if len(__snake_case ) < 2: return dirty for i in range(len(__snake_case ) - 1 ): clean += dirty[i] if dirty[i] == dirty[i + 1]: clean += "X" clean += dirty[-1] if len(__snake_case ) & 1: clean += "X" return clean def UpperCAmelCase_ ( __snake_case ) -> list[str]: """simple docstring""" _lowercase ='''ABCDEFGHIKLMNOPQRSTUVWXYZ''' # we're using a list instead of a '2d' array because it makes the math # for setting up the table and doing the actual encoding/decoding simpler _lowercase =[] # copy key chars into the table if they are in `alphabet` ignoring duplicates for char in key.upper(): if char not in table and char in alphabet: table.append(__snake_case ) # fill the rest of the table in with the remaining alphabet chars for char in alphabet: if char not in table: table.append(__snake_case ) return table def UpperCAmelCase_ ( __snake_case , __snake_case ) -> str: """simple docstring""" _lowercase =generate_table(__snake_case ) _lowercase =prepare_input(__snake_case ) _lowercase ='''''' # https://en.wikipedia.org/wiki/Playfair_cipher#Description for chara, chara in chunker(__snake_case , 2 ): _lowercase , _lowercase =divmod(table.index(__snake_case ) , 5 ) _lowercase , _lowercase =divmod(table.index(__snake_case ) , 5 ) if rowa == rowa: ciphertext += table[rowa * 5 + (cola + 1) % 5] ciphertext += table[rowa * 5 + (cola + 1) % 5] elif cola == cola: ciphertext += table[((rowa + 1) % 5) * 5 + cola] ciphertext += table[((rowa + 1) % 5) * 5 + cola] else: # rectangle ciphertext += table[rowa * 5 + cola] ciphertext += table[rowa * 5 + cola] return ciphertext def UpperCAmelCase_ ( __snake_case , __snake_case ) -> str: """simple docstring""" _lowercase =generate_table(__snake_case ) _lowercase ='''''' # https://en.wikipedia.org/wiki/Playfair_cipher#Description for chara, chara in chunker(__snake_case , 2 ): _lowercase , _lowercase =divmod(table.index(__snake_case ) , 5 ) _lowercase , _lowercase =divmod(table.index(__snake_case ) , 5 ) if rowa == rowa: plaintext += table[rowa * 5 + (cola - 1) % 5] plaintext += table[rowa * 5 + (cola - 1) % 5] elif cola == cola: plaintext += table[((rowa - 1) % 5) * 5 + cola] plaintext += table[((rowa - 1) % 5) * 5 + cola] else: # rectangle plaintext += table[rowa * 5 + cola] plaintext += table[rowa * 5 + cola] return plaintext
5
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): def __A (self ) -> List[Any]: # clean up the VRAM after each test super().tearDown() gc.collect() def __A (self ) -> Optional[Any]: _lowercase =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/sd2-inpaint/init_image.png''' ) _lowercase =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png''' ) _lowercase ='''xvjiarui/stable-diffusion-2-inpainting''' _lowercase , _lowercase =FlaxStableDiffusionInpaintPipeline.from_pretrained(UpperCAmelCase , safety_checker=UpperCAmelCase ) _lowercase ='''Face of a yellow cat, high resolution, sitting on a park bench''' _lowercase =jax.random.PRNGKey(0 ) _lowercase =5_0 _lowercase =jax.device_count() _lowercase =num_samples * [prompt] _lowercase =num_samples * [init_image] _lowercase =num_samples * [mask_image] _lowercase , _lowercase , _lowercase =pipeline.prepare_inputs(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) # shard inputs and rng _lowercase =replicate(UpperCAmelCase ) _lowercase =jax.random.split(UpperCAmelCase , jax.device_count() ) _lowercase =shard(UpperCAmelCase ) _lowercase =shard(UpperCAmelCase ) _lowercase =shard(UpperCAmelCase ) _lowercase =pipeline( UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , jit=UpperCAmelCase ) _lowercase =output.images.reshape(UpperCAmelCase , 5_1_2 , 5_1_2 , 3 ) _lowercase =images[0, 2_5_3:2_5_6, 2_5_3:2_5_6, -1] _lowercase =jnp.asarray(jax.device_get(image_slice.flatten() ) ) _lowercase =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
5
1
import argparse import shutil import time from json import JSONDecodeError from logging import getLogger from pathlib import Path from typing import Dict, List import torch from torch.utils.data import DataLoader from tqdm import tqdm from transformers import AutoModelForSeqaSeqLM, AutoTokenizer from utils import ( SeqaSeqDataset, calculate_bleu, calculate_rouge, chunks, lmap, load_json, parse_numeric_n_bool_cl_kwargs, save_json, use_task_specific_params, write_txt_file, ) UpperCAmelCase__ = getLogger(__name__) def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case = 8 , __snake_case = 1024 , __snake_case="val" , __snake_case=None , __snake_case=False , __snake_case="summarization" , __snake_case=None , __snake_case=1 , __snake_case = None , __snake_case="" , **__snake_case , ) -> Dict: """simple docstring""" _lowercase =str(__snake_case ) assert local_rank is not None torch.distributed.init_process_group(backend='''nccl''' , rank=__snake_case ) _lowercase =Path(__snake_case ) _lowercase =save_dir.joinpath(F"rank_{local_rank}_output.json" ) torch.cuda.set_device(__snake_case ) _lowercase =AutoModelForSeqaSeqLM.from_pretrained(__snake_case ).cuda() if fpaa: _lowercase =model.half() # determine if we need to increase num_beams use_task_specific_params(__snake_case , __snake_case ) # update config with task specific params _lowercase =generate_kwargs.pop('''num_beams''' , model.config.num_beams ) # AttributeError risk? if num_return_sequences > num_beams: _lowercase =num_return_sequences _lowercase =AutoTokenizer.from_pretrained(__snake_case ) logger.info(F"Inferred tokenizer type: {tokenizer.__class__}" ) # if this is wrong, check config.model_type. if max_source_length is None: _lowercase =tokenizer.model_max_length if prefix is None: _lowercase =prefix or getattr(model.config , '''prefix''' , '''''' ) or '''''' _lowercase =SeqaSeqDataset( __snake_case , __snake_case , __snake_case , max_target_length=1024 , type_path=__snake_case , n_obs=__snake_case , prefix=__snake_case , **__snake_case , ) # I set shuffle=True for a more accurate progress bar. # If all the longest samples are first, the prog bar estimate is too high at the beginning. _lowercase =ds.make_sortish_sampler(__snake_case , distributed=__snake_case , add_extra_examples=__snake_case , shuffle=__snake_case ) _lowercase =DataLoader(__snake_case , sampler=__snake_case , batch_size=__snake_case , collate_fn=ds.collate_fn ) _lowercase =[] for batch in tqdm(__snake_case ): _lowercase =model.generate( input_ids=batch['''input_ids'''].to(model.device ) , attention_mask=batch['''attention_mask'''].to(model.device ) , num_return_sequences=__snake_case , num_beams=__snake_case , **__snake_case , ) _lowercase =tokenizer.batch_decode(__snake_case , skip_special_tokens=__snake_case , clean_up_tokenization_spaces=__snake_case ) _lowercase =batch['''ids'''] if num_return_sequences > 1: _lowercase =chunks(__snake_case , __snake_case ) # batch size chunks, each of size num_return_seq for i, pred in enumerate(__snake_case ): results.append({'''pred''': pred, '''id''': ids[i].item()} ) save_json(__snake_case , __snake_case ) return results, sampler.num_replicas def UpperCAmelCase_ ( ) -> Dict: """simple docstring""" _lowercase =argparse.ArgumentParser( epilog='''Unspecified args like --num_beams=2 --decoder_start_token_id=4 are passed to model.generate''' ) parser.add_argument('''--data_dir''' , type=__snake_case , help='''like cnn_dm/test.source''' ) parser.add_argument( '''--model_name''' , type=__snake_case , help='''like facebook/bart-large-cnn,t5-base, etc.''' , default='''sshleifer/distilbart-xsum-12-3''' , ) parser.add_argument('''--save_dir''' , type=__snake_case , help='''where to save''' , default='''tmp_gen''' ) parser.add_argument('''--max_source_length''' , type=__snake_case , default=__snake_case ) parser.add_argument( '''--type_path''' , type=__snake_case , default='''test''' , help='''which subset to evaluate typically train/val/test''' ) parser.add_argument('''--task''' , type=__snake_case , default='''summarization''' , help='''used for task_specific_params + metrics''' ) parser.add_argument('''--bs''' , type=__snake_case , default=8 , required=__snake_case , help='''batch size''' ) parser.add_argument( '''--local_rank''' , type=__snake_case , default=-1 , required=__snake_case , help='''should be passed by distributed.launch''' ) parser.add_argument( '''--n_obs''' , type=__snake_case , default=__snake_case , required=__snake_case , help='''How many observations. Defaults to all.''' ) parser.add_argument( '''--num_return_sequences''' , type=__snake_case , default=1 , required=__snake_case , help='''How many sequences to return''' ) parser.add_argument( '''--sync_timeout''' , type=__snake_case , default=600 , required=__snake_case , help='''How long should master process wait for other processes to finish.''' , ) parser.add_argument('''--src_lang''' , type=__snake_case , default=__snake_case , required=__snake_case ) parser.add_argument('''--tgt_lang''' , type=__snake_case , default=__snake_case , required=__snake_case ) parser.add_argument( '''--prefix''' , type=__snake_case , required=__snake_case , default=__snake_case , help='''will be added to the begininng of src examples''' ) parser.add_argument('''--fp16''' , action='''store_true''' ) parser.add_argument('''--debug''' , action='''store_true''' ) _lowercase =time.time() _lowercase , _lowercase =parser.parse_known_args() _lowercase =parse_numeric_n_bool_cl_kwargs(__snake_case ) if generate_kwargs and args.local_rank <= 0: print(F"parsed the following generate kwargs: {generate_kwargs}" ) _lowercase =Path(args.save_dir + '''_tmp''' ) Path(__snake_case ).mkdir(exist_ok=__snake_case ) # this handles locking. _lowercase =list(json_save_dir.glob('''rank_*.json''' ) ) if intermediate_files: raise ValueError(F"Found files at {json_save_dir} please move or remove them." ) # In theory, a node could finish and save before another node hits this. If this happens, we can address later. _lowercase ={} if args.src_lang is not None: _lowercase =args.src_lang if args.tgt_lang is not None: _lowercase =args.tgt_lang Path(args.save_dir ).mkdir(exist_ok=__snake_case ) _lowercase , _lowercase =eval_data_dir( args.data_dir , __snake_case , args.model_name , type_path=args.type_path , bs=args.bs , fpaa=args.fpaa , task=args.task , local_rank=args.local_rank , n_obs=args.n_obs , max_source_length=args.max_source_length , num_return_sequences=args.num_return_sequences , prefix=args.prefix , dataset_kwargs=__snake_case , **__snake_case , ) if args.local_rank <= 0: _lowercase =Path(args.save_dir ) save_dir.mkdir(exist_ok=__snake_case ) _lowercase =gather_results_from_each_node(__snake_case , __snake_case , args.sync_timeout ) _lowercase =combine_partial_results(__snake_case ) if args.num_return_sequences > 1: _lowercase =save_dir.joinpath('''pseudolabel_results.json''' ) print(F"Saving aggregated results at {save_path}, intermediate in {json_save_dir}/" ) save_json(__snake_case , __snake_case ) return _lowercase =Path(args.data_dir ).joinpath(args.type_path + '''.target''' ) with open(__snake_case ) as f: _lowercase =[x.rstrip() for x in f.readlines()][: len(__snake_case )] # Calculate metrics, save metrics, and save _generations.txt _lowercase ='''translation''' in args.task _lowercase =calculate_bleu if calc_bleu else calculate_rouge _lowercase ='''bleu''' if calc_bleu else '''rouge''' _lowercase =score_fn(__snake_case , __snake_case ) _lowercase =len(__snake_case ) _lowercase =time.time() - start_time _lowercase =round(runtime / metrics['''n_obs'''] , 4 ) _lowercase =num_replicas # TODO(@stas00): add whatever metadata to metrics _lowercase =save_dir.joinpath(F"{args.type_path}_{metric_name}.json" ) save_json(__snake_case , __snake_case , indent=__snake_case ) print(__snake_case ) write_txt_file(__snake_case , save_dir.joinpath(F"{args.type_path}_generations.txt" ) ) if args.debug: write_txt_file(__snake_case , save_dir.joinpath(F"{args.type_path}.target" ) ) else: shutil.rmtree(__snake_case ) def UpperCAmelCase_ ( __snake_case ) -> List: """simple docstring""" _lowercase =[] for partial_result in partial_results: records.extend(__snake_case ) _lowercase =sorted(__snake_case , key=lambda __snake_case : x["id"] ) _lowercase =[x['''pred'''] for x in records] return preds def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case ) -> List[Dict[str, List]]: """simple docstring""" _lowercase =time.time() logger.info('''waiting for all nodes to finish''' ) _lowercase =None while (time.time() - start_wait) < timeout: _lowercase =list(save_dir.glob('''rank_*.json''' ) ) if len(__snake_case ) < num_replicas: continue try: # make sure all json files are fully saved _lowercase =lmap(__snake_case , __snake_case ) return json_data except JSONDecodeError: continue else: raise TimeoutError('''Rank 0 gave up on waiting for other processes''' ) # Unreachable if __name__ == "__main__": # Usage for MT: run_generate()
5
import comet # From: unbabel-comet import torch import datasets UpperCAmelCase__ = datasets.logging.get_logger(__name__) UpperCAmelCase__ = '''\ @inproceedings{rei-EtAl:2020:WMT, author = {Rei, Ricardo and Stewart, Craig and Farinha, Ana C and Lavie, Alon}, title = {Unbabel\'s Participation in the WMT20 Metrics Shared Task}, booktitle = {Proceedings of the Fifth Conference on Machine Translation}, month = {November}, year = {2020}, address = {Online}, publisher = {Association for Computational Linguistics}, pages = {909--918}, } @inproceedings{rei-etal-2020-comet, title = "{COMET}: A Neural Framework for {MT} Evaluation", author = "Rei, Ricardo and Stewart, Craig and Farinha, Ana C and Lavie, Alon", booktitle = "Proceedings of the 2020 Conference on Empirical Methods in Natural Language Processing (EMNLP)", month = nov, year = "2020", address = "Online", publisher = "Association for Computational Linguistics", url = "https://www.aclweb.org/anthology/2020.emnlp-main.213", pages = "2685--2702", } ''' UpperCAmelCase__ = '''\ Crosslingual Optimized Metric for Evaluation of Translation (COMET) is an open-source framework used to train Machine Translation metrics that achieve high levels of correlation with different types of human judgments (HTER, DA\'s or MQM). With the release of the framework the authors also released fully trained models that were used to compete in the WMT20 Metrics Shared Task achieving SOTA in that years competition. See the [README.md] file at https://unbabel.github.io/COMET/html/models.html for more information. ''' UpperCAmelCase__ = ''' COMET score. Args: `sources` (list of str): Source sentences `predictions` (list of str): candidate translations `references` (list of str): reference translations `cuda` (bool): If set to True, runs COMET using GPU `show_progress` (bool): Shows progress `model`: COMET model to be used. Will default to `wmt-large-da-estimator-1719` if None. Returns: `samples`: List of dictionaries with `src`, `mt`, `ref` and `score`. `scores`: List of scores. Examples: >>> comet_metric = datasets.load_metric(\'comet\') >>> # comet_metric = load_metric(\'comet\', \'wmt20-comet-da\') # you can also choose which model to use >>> source = ["Dem Feuer konnte Einhalt geboten werden", "Schulen und Kindergärten wurden eröffnet."] >>> hypothesis = ["The fire could be stopped", "Schools and kindergartens were open"] >>> reference = ["They were able to control the fire.", "Schools and kindergartens opened"] >>> results = comet_metric.compute(predictions=hypothesis, references=reference, sources=source) >>> print([round(v, 2) for v in results["scores"]]) [0.19, 0.92] ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION) class lowerCamelCase__ ( datasets.Metric): def __A (self ) -> Optional[int]: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , homepage='''https://unbabel.github.io/COMET/html/index.html''' , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''sources''': datasets.Value('''string''' , id='''sequence''' ), '''predictions''': datasets.Value('''string''' , id='''sequence''' ), '''references''': datasets.Value('''string''' , id='''sequence''' ), } ) , codebase_urls=['''https://github.com/Unbabel/COMET'''] , reference_urls=[ '''https://github.com/Unbabel/COMET''', '''https://www.aclweb.org/anthology/2020.emnlp-main.213/''', '''http://www.statmt.org/wmt20/pdf/2020.wmt-1.101.pdf6''', ] , ) def __A (self , UpperCAmelCase ) -> Dict: if self.config_name == "default": _lowercase =comet.load_from_checkpoint(comet.download_model('''wmt20-comet-da''' ) ) else: _lowercase =comet.load_from_checkpoint(comet.download_model(self.config_name ) ) def __A (self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase=None , UpperCAmelCase=False ) -> int: if gpus is None: _lowercase =1 if torch.cuda.is_available() else 0 _lowercase ={'''src''': sources, '''mt''': predictions, '''ref''': references} _lowercase =[dict(zip(UpperCAmelCase , UpperCAmelCase ) ) for t in zip(*data.values() )] _lowercase , _lowercase =self.scorer.predict(UpperCAmelCase , gpus=UpperCAmelCase , progress_bar=UpperCAmelCase ) return {"mean_score": mean_score, "scores": scores}
5
1
from typing import List, Optional, Union import torch from ...models import UNetaDConditionModel, VQModel from ...pipelines import DiffusionPipeline from ...pipelines.pipeline_utils import ImagePipelineOutput from ...schedulers import DDPMScheduler from ...utils import ( is_accelerate_available, is_accelerate_version, logging, randn_tensor, replace_example_docstring, ) UpperCAmelCase__ = logging.get_logger(__name__) # pylint: disable=invalid-name UpperCAmelCase__ = ''' Examples: ```py >>> import torch >>> import numpy as np >>> from diffusers import KandinskyV22PriorPipeline, KandinskyV22ControlnetPipeline >>> from transformers import pipeline >>> from diffusers.utils import load_image >>> def make_hint(image, depth_estimator): ... image = depth_estimator(image)["depth"] ... image = np.array(image) ... image = image[:, :, None] ... image = np.concatenate([image, image, image], axis=2) ... detected_map = torch.from_numpy(image).float() / 255.0 ... hint = detected_map.permute(2, 0, 1) ... return hint >>> depth_estimator = pipeline("depth-estimation") >>> pipe_prior = KandinskyV22PriorPipeline.from_pretrained( ... "kandinsky-community/kandinsky-2-2-prior", torch_dtype=torch.float16 ... ) >>> pipe_prior = pipe_prior.to("cuda") >>> pipe = KandinskyV22ControlnetPipeline.from_pretrained( ... "kandinsky-community/kandinsky-2-2-controlnet-depth", torch_dtype=torch.float16 ... ) >>> pipe = pipe.to("cuda") >>> img = load_image( ... "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" ... "/kandinsky/cat.png" ... ).resize((768, 768)) >>> hint = make_hint(img, depth_estimator).unsqueeze(0).half().to("cuda") >>> prompt = "A robot, 4k photo" >>> negative_prior_prompt = "lowres, text, error, cropped, worst quality, low quality, jpeg artifacts, ugly, duplicate, morbid, mutilated, out of frame, extra fingers, mutated hands, poorly drawn hands, poorly drawn face, mutation, deformed, blurry, dehydrated, bad anatomy, bad proportions, extra limbs, cloned face, disfigured, gross proportions, malformed limbs, missing arms, missing legs, extra arms, extra legs, fused fingers, too many fingers, long neck, username, watermark, signature" >>> generator = torch.Generator(device="cuda").manual_seed(43) >>> image_emb, zero_image_emb = pipe_prior( ... prompt=prompt, negative_prompt=negative_prior_prompt, generator=generator ... ).to_tuple() >>> images = pipe( ... image_embeds=image_emb, ... negative_image_embeds=zero_image_emb, ... hint=hint, ... num_inference_steps=50, ... generator=generator, ... height=768, ... width=768, ... ).images >>> images[0].save("robot_cat.png") ``` ''' def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case=8 ) -> Union[str, Any]: """simple docstring""" _lowercase =height // scale_factor**2 if height % scale_factor**2 != 0: new_height += 1 _lowercase =width // scale_factor**2 if width % scale_factor**2 != 0: new_width += 1 return new_height * scale_factor, new_width * scale_factor class lowerCamelCase__ ( lowerCAmelCase): def __init__(self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , ) -> Tuple: super().__init__() self.register_modules( unet=UpperCAmelCase , scheduler=UpperCAmelCase , movq=UpperCAmelCase , ) _lowercase =2 ** (len(self.movq.config.block_out_channels ) - 1) def __A (self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) -> Optional[Any]: if latents is None: _lowercase =randn_tensor(UpperCAmelCase , generator=UpperCAmelCase , device=UpperCAmelCase , dtype=UpperCAmelCase ) else: if latents.shape != shape: raise ValueError(f"Unexpected latents shape, got {latents.shape}, expected {shape}" ) _lowercase =latents.to(UpperCAmelCase ) _lowercase =latents * scheduler.init_noise_sigma return latents def __A (self , UpperCAmelCase=0 ) -> Tuple: if is_accelerate_available(): from accelerate import cpu_offload else: raise ImportError('''Please install accelerate via `pip install accelerate`''' ) _lowercase =torch.device(f"cuda:{gpu_id}" ) _lowercase =[ self.unet, self.movq, ] for cpu_offloaded_model in models: if cpu_offloaded_model is not None: cpu_offload(UpperCAmelCase , UpperCAmelCase ) def __A (self , UpperCAmelCase=0 ) -> str: if is_accelerate_available() and is_accelerate_version('''>=''' , '''0.17.0.dev0''' ): from accelerate import cpu_offload_with_hook else: raise ImportError('''`enable_model_cpu_offload` requires `accelerate v0.17.0` or higher.''' ) _lowercase =torch.device(f"cuda:{gpu_id}" ) if self.device.type != "cpu": self.to('''cpu''' , silence_dtype_warnings=UpperCAmelCase ) torch.cuda.empty_cache() # otherwise we don't see the memory savings (but they probably exist) _lowercase =None for cpu_offloaded_model in [self.unet, self.movq]: _lowercase , _lowercase =cpu_offload_with_hook(UpperCAmelCase , UpperCAmelCase , prev_module_hook=UpperCAmelCase ) # We'll offload the last model manually. _lowercase =hook @property # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device def __A (self ) -> Union[str, Any]: if not hasattr(self.unet , '''_hf_hook''' ): return self.device for module in self.unet.modules(): if ( hasattr(UpperCAmelCase , '''_hf_hook''' ) and hasattr(module._hf_hook , '''execution_device''' ) and module._hf_hook.execution_device is not None ): return torch.device(module._hf_hook.execution_device ) return self.device @torch.no_grad() @replace_example_docstring(UpperCAmelCase ) def __call__(self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = 5_1_2 , UpperCAmelCase = 5_1_2 , UpperCAmelCase = 1_0_0 , UpperCAmelCase = 4.0 , UpperCAmelCase = 1 , UpperCAmelCase = None , UpperCAmelCase = None , UpperCAmelCase = "pil" , UpperCAmelCase = True , ) -> Any: _lowercase =self._execution_device _lowercase =guidance_scale > 1.0 if isinstance(UpperCAmelCase , UpperCAmelCase ): _lowercase =torch.cat(UpperCAmelCase , dim=0 ) if isinstance(UpperCAmelCase , UpperCAmelCase ): _lowercase =torch.cat(UpperCAmelCase , dim=0 ) if isinstance(UpperCAmelCase , UpperCAmelCase ): _lowercase =torch.cat(UpperCAmelCase , dim=0 ) _lowercase =image_embeds.shape[0] * num_images_per_prompt if do_classifier_free_guidance: _lowercase =image_embeds.repeat_interleave(UpperCAmelCase , dim=0 ) _lowercase =negative_image_embeds.repeat_interleave(UpperCAmelCase , dim=0 ) _lowercase =hint.repeat_interleave(UpperCAmelCase , dim=0 ) _lowercase =torch.cat([negative_image_embeds, image_embeds] , dim=0 ).to(dtype=self.unet.dtype , device=UpperCAmelCase ) _lowercase =torch.cat([hint, hint] , dim=0 ).to(dtype=self.unet.dtype , device=UpperCAmelCase ) self.scheduler.set_timesteps(UpperCAmelCase , device=UpperCAmelCase ) _lowercase =self.scheduler.timesteps _lowercase =self.movq.config.latent_channels _lowercase , _lowercase =downscale_height_and_width(UpperCAmelCase , UpperCAmelCase , self.movq_scale_factor ) # create initial latent _lowercase =self.prepare_latents( (batch_size, num_channels_latents, height, width) , image_embeds.dtype , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , self.scheduler , ) for i, t in enumerate(self.progress_bar(UpperCAmelCase ) ): # expand the latents if we are doing classifier free guidance _lowercase =torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents _lowercase ={'''image_embeds''': image_embeds, '''hint''': hint} _lowercase =self.unet( sample=UpperCAmelCase , timestep=UpperCAmelCase , encoder_hidden_states=UpperCAmelCase , added_cond_kwargs=UpperCAmelCase , return_dict=UpperCAmelCase , )[0] if do_classifier_free_guidance: _lowercase , _lowercase =noise_pred.split(latents.shape[1] , dim=1 ) _lowercase , _lowercase =noise_pred.chunk(2 ) _lowercase , _lowercase =variance_pred.chunk(2 ) _lowercase =noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) _lowercase =torch.cat([noise_pred, variance_pred_text] , dim=1 ) if not ( hasattr(self.scheduler.config , '''variance_type''' ) and self.scheduler.config.variance_type in ["learned", "learned_range"] ): _lowercase , _lowercase =noise_pred.split(latents.shape[1] , dim=1 ) # compute the previous noisy sample x_t -> x_t-1 _lowercase =self.scheduler.step( UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , generator=UpperCAmelCase , )[0] # post-processing _lowercase =self.movq.decode(UpperCAmelCase , force_not_quantize=UpperCAmelCase )['''sample'''] if output_type not in ["pt", "np", "pil"]: raise ValueError(f"Only the output types `pt`, `pil` and `np` are supported not output_type={output_type}" ) if output_type in ["np", "pil"]: _lowercase =image * 0.5 + 0.5 _lowercase =image.clamp(0 , 1 ) _lowercase =image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() if output_type == "pil": _lowercase =self.numpy_to_pil(UpperCAmelCase ) if not return_dict: return (image,) return ImagePipelineOutput(images=UpperCAmelCase )
5
# DISCLAIMER: This file is strongly influenced by https://github.com/yang-song/score_sde_pytorch import math from typing import Union import torch from ..configuration_utils import ConfigMixin, register_to_config from ..utils import randn_tensor from .scheduling_utils import SchedulerMixin class lowerCamelCase__ ( lowerCAmelCase , lowerCAmelCase): SCREAMING_SNAKE_CASE__ = 1 @register_to_config def __init__(self , UpperCAmelCase=2_0_0_0 , UpperCAmelCase=0.1 , UpperCAmelCase=2_0 , UpperCAmelCase=1e-3 ) -> List[str]: _lowercase =None _lowercase =None _lowercase =None def __A (self , UpperCAmelCase , UpperCAmelCase = None ) -> str: _lowercase =torch.linspace(1 , self.config.sampling_eps , UpperCAmelCase , device=UpperCAmelCase ) def __A (self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase=None ) -> Optional[int]: if self.timesteps is None: raise ValueError( '''`self.timesteps` is not set, you need to run \'set_timesteps\' after creating the scheduler''' ) # TODO(Patrick) better comments + non-PyTorch # postprocess model score _lowercase =( -0.25 * t**2 * (self.config.beta_max - self.config.beta_min) - 0.5 * t * self.config.beta_min ) _lowercase =torch.sqrt(1.0 - torch.exp(2.0 * log_mean_coeff ) ) _lowercase =std.flatten() while len(std.shape ) < len(score.shape ): _lowercase =std.unsqueeze(-1 ) _lowercase =-score / std # compute _lowercase =-1.0 / len(self.timesteps ) _lowercase =self.config.beta_min + t * (self.config.beta_max - self.config.beta_min) _lowercase =beta_t.flatten() while len(beta_t.shape ) < len(x.shape ): _lowercase =beta_t.unsqueeze(-1 ) _lowercase =-0.5 * beta_t * x _lowercase =torch.sqrt(UpperCAmelCase ) _lowercase =drift - diffusion**2 * score _lowercase =x + drift * dt # add noise _lowercase =randn_tensor(x.shape , layout=x.layout , generator=UpperCAmelCase , device=x.device , dtype=x.dtype ) _lowercase =x_mean + diffusion * math.sqrt(-dt ) * noise return x, x_mean def __len__(self ) -> str: return self.config.num_train_timesteps
5
1
from typing import Optional import torch import torch.utils.checkpoint from torch import Tensor, nn from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss from ...activations import ACTaFN from ...modeling_outputs import ( BackboneOutput, BaseModelOutputWithNoAttention, BaseModelOutputWithPoolingAndNoAttention, ImageClassifierOutputWithNoAttention, ) from ...modeling_utils import PreTrainedModel from ...utils import ( add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging, replace_return_docstrings, ) from ...utils.backbone_utils import BackboneMixin from .configuration_resnet import ResNetConfig UpperCAmelCase__ = logging.get_logger(__name__) # General docstring UpperCAmelCase__ = '''ResNetConfig''' # Base docstring UpperCAmelCase__ = '''microsoft/resnet-50''' UpperCAmelCase__ = [1, 2048, 7, 7] # Image classification docstring UpperCAmelCase__ = '''microsoft/resnet-50''' UpperCAmelCase__ = '''tiger cat''' UpperCAmelCase__ = [ '''microsoft/resnet-50''', # See all resnet models at https://huggingface.co/models?filter=resnet ] class lowerCamelCase__ ( nn.Module): def __init__(self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = 3 , UpperCAmelCase = 1 , UpperCAmelCase = "relu" ) -> Any: super().__init__() _lowercase =nn.Convad( UpperCAmelCase , UpperCAmelCase , kernel_size=UpperCAmelCase , stride=UpperCAmelCase , padding=kernel_size // 2 , bias=UpperCAmelCase ) _lowercase =nn.BatchNormad(UpperCAmelCase ) _lowercase =ACTaFN[activation] if activation is not None else nn.Identity() def __A (self , UpperCAmelCase ) -> Tensor: _lowercase =self.convolution(UpperCAmelCase ) _lowercase =self.normalization(UpperCAmelCase ) _lowercase =self.activation(UpperCAmelCase ) return hidden_state class lowerCamelCase__ ( nn.Module): def __init__(self , UpperCAmelCase ) -> str: super().__init__() _lowercase =ResNetConvLayer( config.num_channels , config.embedding_size , kernel_size=7 , stride=2 , activation=config.hidden_act ) _lowercase =nn.MaxPoolad(kernel_size=3 , stride=2 , padding=1 ) _lowercase =config.num_channels def __A (self , UpperCAmelCase ) -> Tensor: _lowercase =pixel_values.shape[1] if num_channels != self.num_channels: raise ValueError( '''Make sure that the channel dimension of the pixel values match with the one set in the configuration.''' ) _lowercase =self.embedder(UpperCAmelCase ) _lowercase =self.pooler(UpperCAmelCase ) return embedding class lowerCamelCase__ ( nn.Module): def __init__(self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = 2 ) -> Dict: super().__init__() _lowercase =nn.Convad(UpperCAmelCase , UpperCAmelCase , kernel_size=1 , stride=UpperCAmelCase , bias=UpperCAmelCase ) _lowercase =nn.BatchNormad(UpperCAmelCase ) def __A (self , UpperCAmelCase ) -> Tensor: _lowercase =self.convolution(UpperCAmelCase ) _lowercase =self.normalization(UpperCAmelCase ) return hidden_state class lowerCamelCase__ ( nn.Module): def __init__(self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = 1 , UpperCAmelCase = "relu" ) -> Optional[Any]: super().__init__() _lowercase =in_channels != out_channels or stride != 1 _lowercase =( ResNetShortCut(UpperCAmelCase , UpperCAmelCase , stride=UpperCAmelCase ) if should_apply_shortcut else nn.Identity() ) _lowercase =nn.Sequential( ResNetConvLayer(UpperCAmelCase , UpperCAmelCase , stride=UpperCAmelCase ) , ResNetConvLayer(UpperCAmelCase , UpperCAmelCase , activation=UpperCAmelCase ) , ) _lowercase =ACTaFN[activation] def __A (self , UpperCAmelCase ) -> List[str]: _lowercase =hidden_state _lowercase =self.layer(UpperCAmelCase ) _lowercase =self.shortcut(UpperCAmelCase ) hidden_state += residual _lowercase =self.activation(UpperCAmelCase ) return hidden_state class lowerCamelCase__ ( nn.Module): def __init__(self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = 1 , UpperCAmelCase = "relu" , UpperCAmelCase = 4 ) -> Any: super().__init__() _lowercase =in_channels != out_channels or stride != 1 _lowercase =out_channels // reduction _lowercase =( ResNetShortCut(UpperCAmelCase , UpperCAmelCase , stride=UpperCAmelCase ) if should_apply_shortcut else nn.Identity() ) _lowercase =nn.Sequential( ResNetConvLayer(UpperCAmelCase , UpperCAmelCase , kernel_size=1 ) , ResNetConvLayer(UpperCAmelCase , UpperCAmelCase , stride=UpperCAmelCase ) , ResNetConvLayer(UpperCAmelCase , UpperCAmelCase , kernel_size=1 , activation=UpperCAmelCase ) , ) _lowercase =ACTaFN[activation] def __A (self , UpperCAmelCase ) -> Dict: _lowercase =hidden_state _lowercase =self.layer(UpperCAmelCase ) _lowercase =self.shortcut(UpperCAmelCase ) hidden_state += residual _lowercase =self.activation(UpperCAmelCase ) return hidden_state class lowerCamelCase__ ( nn.Module): def __init__(self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = 2 , UpperCAmelCase = 2 , ) -> List[Any]: super().__init__() _lowercase =ResNetBottleNeckLayer if config.layer_type == '''bottleneck''' else ResNetBasicLayer _lowercase =nn.Sequential( # downsampling is done in the first layer with stride of 2 layer(UpperCAmelCase , UpperCAmelCase , stride=UpperCAmelCase , activation=config.hidden_act ) , *[layer(UpperCAmelCase , UpperCAmelCase , activation=config.hidden_act ) for _ in range(depth - 1 )] , ) def __A (self , UpperCAmelCase ) -> Tensor: _lowercase =input for layer in self.layers: _lowercase =layer(UpperCAmelCase ) return hidden_state class lowerCamelCase__ ( nn.Module): def __init__(self , UpperCAmelCase ) -> List[str]: super().__init__() _lowercase =nn.ModuleList([] ) # based on `downsample_in_first_stage` the first layer of the first stage may or may not downsample the input self.stages.append( ResNetStage( UpperCAmelCase , config.embedding_size , config.hidden_sizes[0] , stride=2 if config.downsample_in_first_stage else 1 , depth=config.depths[0] , ) ) _lowercase =zip(config.hidden_sizes , config.hidden_sizes[1:] ) for (in_channels, out_channels), depth in zip(UpperCAmelCase , config.depths[1:] ): self.stages.append(ResNetStage(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , depth=UpperCAmelCase ) ) def __A (self , UpperCAmelCase , UpperCAmelCase = False , UpperCAmelCase = True ) -> BaseModelOutputWithNoAttention: _lowercase =() if output_hidden_states else None for stage_module in self.stages: if output_hidden_states: _lowercase =hidden_states + (hidden_state,) _lowercase =stage_module(UpperCAmelCase ) if output_hidden_states: _lowercase =hidden_states + (hidden_state,) if not return_dict: return tuple(v for v in [hidden_state, hidden_states] if v is not None ) return BaseModelOutputWithNoAttention( last_hidden_state=UpperCAmelCase , hidden_states=UpperCAmelCase , ) class lowerCamelCase__ ( lowerCAmelCase): SCREAMING_SNAKE_CASE__ = ResNetConfig SCREAMING_SNAKE_CASE__ = '''resnet''' SCREAMING_SNAKE_CASE__ = '''pixel_values''' SCREAMING_SNAKE_CASE__ = True def __A (self , UpperCAmelCase ) -> Union[str, Any]: if isinstance(UpperCAmelCase , nn.Convad ): nn.init.kaiming_normal_(module.weight , mode='''fan_out''' , nonlinearity='''relu''' ) elif isinstance(UpperCAmelCase , (nn.BatchNormad, nn.GroupNorm) ): nn.init.constant_(module.weight , 1 ) nn.init.constant_(module.bias , 0 ) def __A (self , UpperCAmelCase , UpperCAmelCase=False ) -> Dict: if isinstance(UpperCAmelCase , UpperCAmelCase ): _lowercase =value UpperCAmelCase__ = R''' This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and behavior. Parameters: config ([`ResNetConfig`]): Model configuration class with all the parameters of the model. Initializing with a config file does not load the weights associated with the model, only the configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights. ''' UpperCAmelCase__ = R''' Args: pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`): Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See [`ConvNextImageProcessor.__call__`] for details. output_hidden_states (`bool`, *optional*): Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for more detail. return_dict (`bool`, *optional*): Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple. ''' @add_start_docstrings( '''The bare ResNet model outputting raw features without any specific head on top.''' , lowerCAmelCase , ) class lowerCamelCase__ ( lowerCAmelCase): def __init__(self , UpperCAmelCase ) -> str: super().__init__(UpperCAmelCase ) _lowercase =config _lowercase =ResNetEmbeddings(UpperCAmelCase ) _lowercase =ResNetEncoder(UpperCAmelCase ) _lowercase =nn.AdaptiveAvgPoolad((1, 1) ) # Initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(UpperCAmelCase ) @add_code_sample_docstrings( checkpoint=_CHECKPOINT_FOR_DOC , output_type=UpperCAmelCase , config_class=_CONFIG_FOR_DOC , modality='''vision''' , expected_output=_EXPECTED_OUTPUT_SHAPE , ) def __A (self , UpperCAmelCase , UpperCAmelCase = None , UpperCAmelCase = None ) -> BaseModelOutputWithPoolingAndNoAttention: _lowercase =( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) _lowercase =return_dict if return_dict is not None else self.config.use_return_dict _lowercase =self.embedder(UpperCAmelCase ) _lowercase =self.encoder( UpperCAmelCase , output_hidden_states=UpperCAmelCase , return_dict=UpperCAmelCase ) _lowercase =encoder_outputs[0] _lowercase =self.pooler(UpperCAmelCase ) if not return_dict: return (last_hidden_state, pooled_output) + encoder_outputs[1:] return BaseModelOutputWithPoolingAndNoAttention( last_hidden_state=UpperCAmelCase , pooler_output=UpperCAmelCase , hidden_states=encoder_outputs.hidden_states , ) @add_start_docstrings( ''' ResNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for ImageNet. ''' , lowerCAmelCase , ) class lowerCamelCase__ ( lowerCAmelCase): def __init__(self , UpperCAmelCase ) -> int: super().__init__(UpperCAmelCase ) _lowercase =config.num_labels _lowercase =ResNetModel(UpperCAmelCase ) # classification head _lowercase =nn.Sequential( nn.Flatten() , nn.Linear(config.hidden_sizes[-1] , config.num_labels ) if config.num_labels > 0 else nn.Identity() , ) # initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(UpperCAmelCase ) @add_code_sample_docstrings( checkpoint=_IMAGE_CLASS_CHECKPOINT , output_type=UpperCAmelCase , config_class=_CONFIG_FOR_DOC , expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT , ) def __A (self , UpperCAmelCase = None , UpperCAmelCase = None , UpperCAmelCase = None , UpperCAmelCase = None , ) -> ImageClassifierOutputWithNoAttention: _lowercase =return_dict if return_dict is not None else self.config.use_return_dict _lowercase =self.resnet(UpperCAmelCase , output_hidden_states=UpperCAmelCase , return_dict=UpperCAmelCase ) _lowercase =outputs.pooler_output if return_dict else outputs[1] _lowercase =self.classifier(UpperCAmelCase ) _lowercase =None if labels is not None: if self.config.problem_type is None: if self.num_labels == 1: _lowercase ='''regression''' elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int): _lowercase ='''single_label_classification''' else: _lowercase ='''multi_label_classification''' if self.config.problem_type == "regression": _lowercase =MSELoss() if self.num_labels == 1: _lowercase =loss_fct(logits.squeeze() , labels.squeeze() ) else: _lowercase =loss_fct(UpperCAmelCase , UpperCAmelCase ) elif self.config.problem_type == "single_label_classification": _lowercase =CrossEntropyLoss() _lowercase =loss_fct(logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) elif self.config.problem_type == "multi_label_classification": _lowercase =BCEWithLogitsLoss() _lowercase =loss_fct(UpperCAmelCase , UpperCAmelCase ) if not return_dict: _lowercase =(logits,) + outputs[2:] return (loss,) + output if loss is not None else output return ImageClassifierOutputWithNoAttention(loss=UpperCAmelCase , logits=UpperCAmelCase , hidden_states=outputs.hidden_states ) @add_start_docstrings( ''' ResNet backbone, to be used with frameworks like DETR and MaskFormer. ''' , lowerCAmelCase , ) class lowerCamelCase__ ( lowerCAmelCase , lowerCAmelCase): def __init__(self , UpperCAmelCase ) -> Tuple: super().__init__(UpperCAmelCase ) super()._init_backbone(UpperCAmelCase ) _lowercase =[config.embedding_size] + config.hidden_sizes _lowercase =ResNetEmbeddings(UpperCAmelCase ) _lowercase =ResNetEncoder(UpperCAmelCase ) # initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(UpperCAmelCase ) @replace_return_docstrings(output_type=UpperCAmelCase , config_class=_CONFIG_FOR_DOC ) def __A (self , UpperCAmelCase , UpperCAmelCase = None , UpperCAmelCase = None ) -> BackboneOutput: _lowercase =return_dict if return_dict is not None else self.config.use_return_dict _lowercase =( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) _lowercase =self.embedder(UpperCAmelCase ) _lowercase =self.encoder(UpperCAmelCase , output_hidden_states=UpperCAmelCase , return_dict=UpperCAmelCase ) _lowercase =outputs.hidden_states _lowercase =() for idx, stage in enumerate(self.stage_names ): if stage in self.out_features: feature_maps += (hidden_states[idx],) if not return_dict: _lowercase =(feature_maps,) if output_hidden_states: output += (outputs.hidden_states,) return output return BackboneOutput( feature_maps=UpperCAmelCase , hidden_states=outputs.hidden_states if output_hidden_states else None , attentions=UpperCAmelCase , )
5
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( MobileViTConfig, MobileViTForImageClassification, MobileViTForSemanticSegmentation, MobileViTImageProcessor, ) from transformers.utils import logging logging.set_verbosity_info() UpperCAmelCase__ = logging.get_logger(__name__) def UpperCAmelCase_ ( __snake_case ) -> Optional[Any]: """simple docstring""" _lowercase =MobileViTConfig() # size of the architecture if "mobilevit_s" in mobilevit_name: _lowercase =[144, 192, 240] _lowercase =[16, 32, 64, 96, 128, 160, 640] elif "mobilevit_xs" in mobilevit_name: _lowercase =[96, 120, 144] _lowercase =[16, 32, 48, 64, 80, 96, 384] elif "mobilevit_xxs" in mobilevit_name: _lowercase =[64, 80, 96] _lowercase =[16, 16, 24, 48, 64, 80, 320] _lowercase =0.05 _lowercase =2.0 if mobilevit_name.startswith('''deeplabv3_''' ): _lowercase =512 _lowercase =16 _lowercase =21 _lowercase ='''pascal-voc-id2label.json''' else: _lowercase =1000 _lowercase ='''imagenet-1k-id2label.json''' _lowercase ='''huggingface/label-files''' _lowercase =json.load(open(hf_hub_download(__snake_case , __snake_case , repo_type='''dataset''' ) , '''r''' ) ) _lowercase ={int(__snake_case ): v for k, v in idalabel.items()} _lowercase =idalabel _lowercase ={v: k for k, v in idalabel.items()} return config def UpperCAmelCase_ ( __snake_case , __snake_case=False ) -> Tuple: """simple docstring""" for i in range(1 , 6 ): if F"layer_{i}." in name: _lowercase =name.replace(F"layer_{i}." , F"encoder.layer.{i - 1}." ) if "conv_1." in name: _lowercase =name.replace('''conv_1.''' , '''conv_stem.''' ) if ".block." in name: _lowercase =name.replace('''.block.''' , '''.''' ) if "exp_1x1" in name: _lowercase =name.replace('''exp_1x1''' , '''expand_1x1''' ) if "red_1x1" in name: _lowercase =name.replace('''red_1x1''' , '''reduce_1x1''' ) if ".local_rep.conv_3x3." in name: _lowercase =name.replace('''.local_rep.conv_3x3.''' , '''.conv_kxk.''' ) if ".local_rep.conv_1x1." in name: _lowercase =name.replace('''.local_rep.conv_1x1.''' , '''.conv_1x1.''' ) if ".norm." in name: _lowercase =name.replace('''.norm.''' , '''.normalization.''' ) if ".conv." in name: _lowercase =name.replace('''.conv.''' , '''.convolution.''' ) if ".conv_proj." in name: _lowercase =name.replace('''.conv_proj.''' , '''.conv_projection.''' ) for i in range(0 , 2 ): for j in range(0 , 4 ): if F".{i}.{j}." in name: _lowercase =name.replace(F".{i}.{j}." , F".{i}.layer.{j}." ) for i in range(2 , 6 ): for j in range(0 , 4 ): if F".{i}.{j}." in name: _lowercase =name.replace(F".{i}.{j}." , F".{i}." ) if "expand_1x1" in name: _lowercase =name.replace('''expand_1x1''' , '''downsampling_layer.expand_1x1''' ) if "conv_3x3" in name: _lowercase =name.replace('''conv_3x3''' , '''downsampling_layer.conv_3x3''' ) if "reduce_1x1" in name: _lowercase =name.replace('''reduce_1x1''' , '''downsampling_layer.reduce_1x1''' ) for i in range(2 , 5 ): if F".global_rep.{i}.weight" in name: _lowercase =name.replace(F".global_rep.{i}.weight" , '''.layernorm.weight''' ) if F".global_rep.{i}.bias" in name: _lowercase =name.replace(F".global_rep.{i}.bias" , '''.layernorm.bias''' ) if ".global_rep." in name: _lowercase =name.replace('''.global_rep.''' , '''.transformer.''' ) if ".pre_norm_mha.0." in name: _lowercase =name.replace('''.pre_norm_mha.0.''' , '''.layernorm_before.''' ) if ".pre_norm_mha.1.out_proj." in name: _lowercase =name.replace('''.pre_norm_mha.1.out_proj.''' , '''.attention.output.dense.''' ) if ".pre_norm_ffn.0." in name: _lowercase =name.replace('''.pre_norm_ffn.0.''' , '''.layernorm_after.''' ) if ".pre_norm_ffn.1." in name: _lowercase =name.replace('''.pre_norm_ffn.1.''' , '''.intermediate.dense.''' ) if ".pre_norm_ffn.4." in name: _lowercase =name.replace('''.pre_norm_ffn.4.''' , '''.output.dense.''' ) if ".transformer." in name: _lowercase =name.replace('''.transformer.''' , '''.transformer.layer.''' ) if ".aspp_layer." in name: _lowercase =name.replace('''.aspp_layer.''' , '''.''' ) if ".aspp_pool." in name: _lowercase =name.replace('''.aspp_pool.''' , '''.''' ) if "seg_head." in name: _lowercase =name.replace('''seg_head.''' , '''segmentation_head.''' ) if "segmentation_head.classifier.classifier." in name: _lowercase =name.replace('''segmentation_head.classifier.classifier.''' , '''segmentation_head.classifier.''' ) if "classifier.fc." in name: _lowercase =name.replace('''classifier.fc.''' , '''classifier.''' ) elif (not base_model) and ("segmentation_head." not in name): _lowercase ='''mobilevit.''' + name return name def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case=False ) -> Optional[Any]: """simple docstring""" if base_model: _lowercase ='''''' else: _lowercase ='''mobilevit.''' for key in orig_state_dict.copy().keys(): _lowercase =orig_state_dict.pop(__snake_case ) if key[:8] == "encoder.": _lowercase =key[8:] if "qkv" in key: _lowercase =key.split('''.''' ) _lowercase =int(key_split[0][6:] ) - 1 _lowercase =int(key_split[3] ) _lowercase =model.get_submodule(F"{model_prefix}encoder.layer.{layer_num}" ) _lowercase =layer.transformer.layer[transformer_num].attention.attention.all_head_size _lowercase =( F"{model_prefix}encoder.layer.{layer_num}.transformer.layer.{transformer_num}.attention.attention." ) if "weight" in key: _lowercase =val[:dim, :] _lowercase =val[dim : dim * 2, :] _lowercase =val[-dim:, :] else: _lowercase =val[:dim] _lowercase =val[dim : dim * 2] _lowercase =val[-dim:] else: _lowercase =val return orig_state_dict def UpperCAmelCase_ ( ) -> Union[str, Any]: """simple docstring""" _lowercase ='''http://images.cocodataset.org/val2017/000000039769.jpg''' _lowercase =Image.open(requests.get(__snake_case , stream=__snake_case ).raw ) return im @torch.no_grad() def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case=False ) -> int: """simple docstring""" _lowercase =get_mobilevit_config(__snake_case ) # load original state_dict _lowercase =torch.load(__snake_case , map_location='''cpu''' ) # load 🤗 model if mobilevit_name.startswith('''deeplabv3_''' ): _lowercase =MobileViTForSemanticSegmentation(__snake_case ).eval() else: _lowercase =MobileViTForImageClassification(__snake_case ).eval() _lowercase =convert_state_dict(__snake_case , __snake_case ) model.load_state_dict(__snake_case ) # Check outputs on an image, prepared by MobileViTImageProcessor _lowercase =MobileViTImageProcessor(crop_size=config.image_size , size=config.image_size + 32 ) _lowercase =image_processor(images=prepare_img() , return_tensors='''pt''' ) _lowercase =model(**__snake_case ) _lowercase =outputs.logits if mobilevit_name.startswith('''deeplabv3_''' ): assert logits.shape == (1, 21, 32, 32) if mobilevit_name == "deeplabv3_mobilevit_s": _lowercase =torch.tensor( [ [[6.20_65, 6.12_92, 6.20_70], [6.10_79, 6.12_54, 6.17_47], [6.00_42, 6.10_71, 6.10_34]], [[-6.92_53, -6.86_53, -7.03_98], [-7.32_18, -7.39_83, -7.36_70], [-7.19_61, -7.24_82, -7.15_69]], [[-4.47_23, -4.43_48, -4.37_69], [-5.36_29, -5.46_32, -5.45_98], [-5.15_87, -5.34_02, -5.50_59]], ] ) elif mobilevit_name == "deeplabv3_mobilevit_xs": _lowercase =torch.tensor( [ [[5.44_49, 5.57_33, 5.63_14], [5.18_15, 5.39_30, 5.59_63], [5.16_56, 5.43_33, 5.48_53]], [[-9.44_23, -9.77_66, -9.67_14], [-9.15_81, -9.57_20, -9.55_19], [-9.10_06, -9.64_58, -9.57_03]], [[-7.77_21, -7.37_16, -7.15_83], [-8.45_99, -8.06_24, -7.79_44], [-8.41_72, -7.83_66, -7.50_25]], ] ) elif mobilevit_name == "deeplabv3_mobilevit_xxs": _lowercase =torch.tensor( [ [[6.98_11, 6.97_43, 7.31_23], [7.17_77, 7.19_31, 7.39_38], [7.56_33, 7.80_50, 7.89_01]], [[-10.55_36, -10.23_32, -10.29_24], [-10.23_36, -9.86_24, -9.59_64], [-10.88_40, -10.81_58, -10.66_59]], [[-3.49_38, -3.06_31, -2.86_20], [-3.42_05, -2.81_35, -2.68_75], [-3.41_79, -2.79_45, -2.87_50]], ] ) else: raise ValueError(F"Unknown mobilevit_name: {mobilevit_name}" ) assert torch.allclose(logits[0, :3, :3, :3] , __snake_case , atol=1e-4 ) else: assert logits.shape == (1, 1000) if mobilevit_name == "mobilevit_s": _lowercase =torch.tensor([-0.98_66, 0.23_92, -1.12_41] ) elif mobilevit_name == "mobilevit_xs": _lowercase =torch.tensor([-2.47_61, -0.93_99, -1.95_87] ) elif mobilevit_name == "mobilevit_xxs": _lowercase =torch.tensor([-1.93_64, -1.23_27, -0.46_53] ) else: raise ValueError(F"Unknown mobilevit_name: {mobilevit_name}" ) assert torch.allclose(logits[0, :3] , __snake_case , atol=1e-4 ) Path(__snake_case ).mkdir(exist_ok=__snake_case ) print(F"Saving model {mobilevit_name} to {pytorch_dump_folder_path}" ) model.save_pretrained(__snake_case ) print(F"Saving image processor to {pytorch_dump_folder_path}" ) image_processor.save_pretrained(__snake_case ) if push_to_hub: _lowercase ={ '''mobilevit_s''': '''mobilevit-small''', '''mobilevit_xs''': '''mobilevit-x-small''', '''mobilevit_xxs''': '''mobilevit-xx-small''', '''deeplabv3_mobilevit_s''': '''deeplabv3-mobilevit-small''', '''deeplabv3_mobilevit_xs''': '''deeplabv3-mobilevit-x-small''', '''deeplabv3_mobilevit_xxs''': '''deeplabv3-mobilevit-xx-small''', } print('''Pushing to the hub...''' ) _lowercase =model_mapping[mobilevit_name] image_processor.push_to_hub(__snake_case , organization='''apple''' ) model.push_to_hub(__snake_case , organization='''apple''' ) if __name__ == "__main__": UpperCAmelCase__ = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--mobilevit_name''', default='''mobilevit_s''', type=str, help=( '''Name of the MobileViT model you\'d like to convert. Should be one of \'mobilevit_s\', \'mobilevit_xs\',''' ''' \'mobilevit_xxs\', \'deeplabv3_mobilevit_s\', \'deeplabv3_mobilevit_xs\', \'deeplabv3_mobilevit_xxs\'.''' ), ) parser.add_argument( '''--checkpoint_path''', required=True, type=str, help='''Path to the original state dict (.pt 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.mobilevit_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub )
5
1
import functools import logging import os import sys import threading from logging import ( CRITICAL, # NOQA DEBUG, # NOQA ERROR, # NOQA FATAL, # NOQA INFO, # NOQA NOTSET, # NOQA WARN, # NOQA WARNING, # NOQA ) from typing import Optional import huggingface_hub.utils as hf_hub_utils from tqdm import auto as tqdm_lib UpperCAmelCase__ = threading.Lock() UpperCAmelCase__ = None UpperCAmelCase__ = { '''debug''': logging.DEBUG, '''info''': logging.INFO, '''warning''': logging.WARNING, '''error''': logging.ERROR, '''critical''': logging.CRITICAL, } UpperCAmelCase__ = logging.WARNING UpperCAmelCase__ = True def UpperCAmelCase_ ( ) -> Any: """simple docstring""" _lowercase =os.getenv('''TRANSFORMERS_VERBOSITY''' , __snake_case ) if env_level_str: if env_level_str in log_levels: return log_levels[env_level_str] else: logging.getLogger().warning( F"Unknown option TRANSFORMERS_VERBOSITY={env_level_str}, " F"has to be one of: { ', '.join(log_levels.keys() ) }" ) return _default_log_level def UpperCAmelCase_ ( ) -> str: """simple docstring""" return __name__.split('''.''' )[0] def UpperCAmelCase_ ( ) -> logging.Logger: """simple docstring""" return logging.getLogger(_get_library_name() ) def UpperCAmelCase_ ( ) -> None: """simple docstring""" global _default_handler with _lock: if _default_handler: # This library has already configured the library root logger. return _lowercase =logging.StreamHandler() # Set sys.stderr as stream. _lowercase =sys.stderr.flush # Apply our default configuration to the library root logger. _lowercase =_get_library_root_logger() library_root_logger.addHandler(_default_handler ) library_root_logger.setLevel(_get_default_logging_level() ) _lowercase =False def UpperCAmelCase_ ( ) -> None: """simple docstring""" global _default_handler with _lock: if not _default_handler: return _lowercase =_get_library_root_logger() library_root_logger.removeHandler(_default_handler ) library_root_logger.setLevel(logging.NOTSET ) _lowercase =None def UpperCAmelCase_ ( ) -> Optional[Any]: """simple docstring""" return log_levels def UpperCAmelCase_ ( __snake_case = None ) -> logging.Logger: """simple docstring""" if name is None: _lowercase =_get_library_name() _configure_library_root_logger() return logging.getLogger(__snake_case ) def UpperCAmelCase_ ( ) -> int: """simple docstring""" _configure_library_root_logger() return _get_library_root_logger().getEffectiveLevel() def UpperCAmelCase_ ( __snake_case ) -> None: """simple docstring""" _configure_library_root_logger() _get_library_root_logger().setLevel(__snake_case ) def UpperCAmelCase_ ( ) -> str: """simple docstring""" return set_verbosity(__snake_case ) def UpperCAmelCase_ ( ) -> List[str]: """simple docstring""" return set_verbosity(__snake_case ) def UpperCAmelCase_ ( ) -> List[str]: """simple docstring""" return set_verbosity(__snake_case ) def UpperCAmelCase_ ( ) -> List[Any]: """simple docstring""" return set_verbosity(__snake_case ) def UpperCAmelCase_ ( ) -> None: """simple docstring""" _configure_library_root_logger() assert _default_handler is not None _get_library_root_logger().removeHandler(_default_handler ) def UpperCAmelCase_ ( ) -> None: """simple docstring""" _configure_library_root_logger() assert _default_handler is not None _get_library_root_logger().addHandler(_default_handler ) def UpperCAmelCase_ ( __snake_case ) -> None: """simple docstring""" _configure_library_root_logger() assert handler is not None _get_library_root_logger().addHandler(__snake_case ) def UpperCAmelCase_ ( __snake_case ) -> None: """simple docstring""" _configure_library_root_logger() assert handler is not None and handler not in _get_library_root_logger().handlers _get_library_root_logger().removeHandler(__snake_case ) def UpperCAmelCase_ ( ) -> None: """simple docstring""" _configure_library_root_logger() _lowercase =False def UpperCAmelCase_ ( ) -> None: """simple docstring""" _configure_library_root_logger() _lowercase =True def UpperCAmelCase_ ( ) -> None: """simple docstring""" _lowercase =_get_library_root_logger().handlers for handler in handlers: _lowercase =logging.Formatter('''[%(levelname)s|%(filename)s:%(lineno)s] %(asctime)s >> %(message)s''' ) handler.setFormatter(__snake_case ) def UpperCAmelCase_ ( ) -> None: """simple docstring""" _lowercase =_get_library_root_logger().handlers for handler in handlers: handler.setFormatter(__snake_case ) def UpperCAmelCase_ ( self , *__snake_case , **__snake_case ) -> Union[str, Any]: """simple docstring""" _lowercase =os.getenv('''TRANSFORMERS_NO_ADVISORY_WARNINGS''' , __snake_case ) if no_advisory_warnings: return self.warning(*__snake_case , **__snake_case ) UpperCAmelCase__ = warning_advice @functools.lru_cache(__snake_case ) def UpperCAmelCase_ ( self , *__snake_case , **__snake_case ) -> Union[str, Any]: """simple docstring""" self.warning(*__snake_case , **__snake_case ) UpperCAmelCase__ = warning_once class lowerCamelCase__ : def __init__(self , *UpperCAmelCase , **UpperCAmelCase ) -> str: # pylint: disable=unused-argument _lowercase =args[0] if args else None def __iter__(self ) -> Any: return iter(self._iterator ) def __getattr__(self , UpperCAmelCase ) -> str: def empty_fn(*UpperCAmelCase , **UpperCAmelCase ): # pylint: disable=unused-argument return return empty_fn def __enter__(self ) -> List[Any]: return self def __exit__(self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) -> int: return class lowerCamelCase__ : def __call__(self , *UpperCAmelCase , **UpperCAmelCase ) -> Union[str, Any]: if _tqdm_active: return tqdm_lib.tqdm(*UpperCAmelCase , **UpperCAmelCase ) else: return EmptyTqdm(*UpperCAmelCase , **UpperCAmelCase ) def __A (self , *UpperCAmelCase , **UpperCAmelCase ) -> int: _lowercase =None if _tqdm_active: return tqdm_lib.tqdm.set_lock(*UpperCAmelCase , **UpperCAmelCase ) def __A (self ) -> Tuple: if _tqdm_active: return tqdm_lib.tqdm.get_lock() UpperCAmelCase__ = _tqdm_cls() def UpperCAmelCase_ ( ) -> bool: """simple docstring""" global _tqdm_active return bool(_tqdm_active ) def UpperCAmelCase_ ( ) -> Dict: """simple docstring""" global _tqdm_active _lowercase =True hf_hub_utils.enable_progress_bars() def UpperCAmelCase_ ( ) -> Optional[int]: """simple docstring""" global _tqdm_active _lowercase =False hf_hub_utils.disable_progress_bars()
5
import requests from bsa import BeautifulSoup def UpperCAmelCase_ ( __snake_case = "https://www.worldometers.info/coronavirus" ) -> dict: """simple docstring""" _lowercase =BeautifulSoup(requests.get(__snake_case ).text , '''html.parser''' ) _lowercase =soup.findAll('''h1''' ) _lowercase =soup.findAll('''div''' , {'''class''': '''maincounter-number'''} ) keys += soup.findAll('''span''' , {'''class''': '''panel-title'''} ) values += soup.findAll('''div''' , {'''class''': '''number-table-main'''} ) return {key.text.strip(): value.text.strip() for key, value in zip(__snake_case , __snake_case )} if __name__ == "__main__": print('''\033[1m''' + '''COVID-19 Status of the World''' + '''\033[0m\n''') for key, value in world_covidaa_stats().items(): print(f'''{key}\n{value}\n''')
5
1
import json import os import unittest from transformers.models.blenderbot_small.tokenization_blenderbot_small import ( VOCAB_FILES_NAMES, BlenderbotSmallTokenizer, ) from ...test_tokenization_common import TokenizerTesterMixin class lowerCamelCase__ ( lowerCAmelCase , unittest.TestCase): SCREAMING_SNAKE_CASE__ = BlenderbotSmallTokenizer SCREAMING_SNAKE_CASE__ = False def __A (self ) -> Any: super().setUp() _lowercase =['''__start__''', '''adapt''', '''act''', '''ap@@''', '''te''', '''__end__''', '''__unk__'''] _lowercase =dict(zip(UpperCAmelCase , range(len(UpperCAmelCase ) ) ) ) _lowercase =['''#version: 0.2''', '''a p''', '''t e</w>''', '''ap t</w>''', '''a d''', '''ad apt</w>''', '''a c''', '''ac t</w>''', ''''''] _lowercase ={'''unk_token''': '''__unk__''', '''bos_token''': '''__start__''', '''eos_token''': '''__end__'''} _lowercase =os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) _lowercase =os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''merges_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(UpperCAmelCase ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(UpperCAmelCase ) ) def __A (self , **UpperCAmelCase ) -> List[str]: kwargs.update(self.special_tokens_map ) return BlenderbotSmallTokenizer.from_pretrained(self.tmpdirname , **UpperCAmelCase ) def __A (self , UpperCAmelCase ) -> Optional[int]: _lowercase ='''adapt act apte''' _lowercase ='''adapt act apte''' return input_text, output_text def __A (self ) -> str: _lowercase =BlenderbotSmallTokenizer(self.vocab_file , self.merges_file , **self.special_tokens_map ) _lowercase ='''adapt act apte''' _lowercase =['''adapt''', '''act''', '''ap@@''', '''te'''] _lowercase =tokenizer.tokenize(UpperCAmelCase ) self.assertListEqual(UpperCAmelCase , UpperCAmelCase ) _lowercase =[tokenizer.bos_token] + tokens + [tokenizer.eos_token] _lowercase =[0, 1, 2, 3, 4, 5] self.assertListEqual(tokenizer.convert_tokens_to_ids(UpperCAmelCase ) , UpperCAmelCase ) def __A (self ) -> Dict: _lowercase =BlenderbotSmallTokenizer.from_pretrained('''facebook/blenderbot-90M''' ) assert tok('''sam''' ).input_ids == [1_3_8_4] _lowercase ='''I am a small frog.''' _lowercase =tok([src_text] , padding=UpperCAmelCase , truncation=UpperCAmelCase )['''input_ids'''] _lowercase =tok.batch_decode(UpperCAmelCase , skip_special_tokens=UpperCAmelCase , clean_up_tokenization_spaces=UpperCAmelCase )[0] assert src_text != decoded # I wish it did! assert decoded == "i am a small frog ." def __A (self ) -> Dict: _lowercase =BlenderbotSmallTokenizer.from_pretrained('''facebook/blenderbot-90M''' ) _lowercase ='''I am a small frog .''' _lowercase ='''.''' _lowercase =tok(UpperCAmelCase )['''input_ids'''] _lowercase =tok(UpperCAmelCase )['''input_ids'''] assert encoded[-1] == encoded_dot[0]
5
from typing import TYPE_CHECKING from ..utils import _LazyModule UpperCAmelCase__ = { '''config''': [ '''EXTERNAL_DATA_FORMAT_SIZE_LIMIT''', '''OnnxConfig''', '''OnnxConfigWithPast''', '''OnnxSeq2SeqConfigWithPast''', '''PatchingSpec''', ], '''convert''': ['''export''', '''validate_model_outputs'''], '''features''': ['''FeaturesManager'''], '''utils''': ['''ParameterFormat''', '''compute_serialized_parameters_size'''], } if TYPE_CHECKING: from .config import ( EXTERNAL_DATA_FORMAT_SIZE_LIMIT, OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast, PatchingSpec, ) from .convert import export, validate_model_outputs from .features import FeaturesManager from .utils import ParameterFormat, compute_serialized_parameters_size else: import sys UpperCAmelCase__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
5
1
import json import os import shutil import sys import tempfile import unittest import unittest.mock as mock from pathlib import Path from huggingface_hub import HfFolder, delete_repo from requests.exceptions import HTTPError from transformers import AutoConfig, BertConfig, GPTaConfig from transformers.configuration_utils import PretrainedConfig from transformers.testing_utils import TOKEN, USER, is_staging_test sys.path.append(str(Path(__file__).parent.parent / '''utils''')) from test_module.custom_configuration import CustomConfig # noqa E402 UpperCAmelCase__ = { '''return_dict''': False, '''output_hidden_states''': True, '''output_attentions''': True, '''torchscript''': True, '''torch_dtype''': '''float16''', '''use_bfloat16''': True, '''tf_legacy_loss''': True, '''pruned_heads''': {'''a''': 1}, '''tie_word_embeddings''': False, '''is_decoder''': True, '''cross_attention_hidden_size''': 128, '''add_cross_attention''': True, '''tie_encoder_decoder''': True, '''max_length''': 50, '''min_length''': 3, '''do_sample''': True, '''early_stopping''': True, '''num_beams''': 3, '''num_beam_groups''': 3, '''diversity_penalty''': 0.5, '''temperature''': 2.0, '''top_k''': 10, '''top_p''': 0.7, '''typical_p''': 0.2, '''repetition_penalty''': 0.8, '''length_penalty''': 0.8, '''no_repeat_ngram_size''': 5, '''encoder_no_repeat_ngram_size''': 5, '''bad_words_ids''': [1, 2, 3], '''num_return_sequences''': 3, '''chunk_size_feed_forward''': 5, '''output_scores''': True, '''return_dict_in_generate''': True, '''forced_bos_token_id''': 2, '''forced_eos_token_id''': 3, '''remove_invalid_values''': True, '''architectures''': ['''BertModel'''], '''finetuning_task''': '''translation''', '''id2label''': {0: '''label'''}, '''label2id''': {'''label''': '''0'''}, '''tokenizer_class''': '''BertTokenizerFast''', '''prefix''': '''prefix''', '''bos_token_id''': 6, '''pad_token_id''': 7, '''eos_token_id''': 8, '''sep_token_id''': 9, '''decoder_start_token_id''': 10, '''exponential_decay_length_penalty''': (5, 1.01), '''suppress_tokens''': [0, 1], '''begin_suppress_tokens''': 2, '''task_specific_params''': {'''translation''': '''some_params'''}, '''problem_type''': '''regression''', } @is_staging_test class lowerCamelCase__ ( unittest.TestCase): @classmethod def __A (cls ) -> List[str]: _lowercase =TOKEN HfFolder.save_token(UpperCAmelCase ) @classmethod def __A (cls ) -> Dict: try: delete_repo(token=cls._token , repo_id='''test-config''' ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id='''valid_org/test-config-org''' ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id='''test-dynamic-config''' ) except HTTPError: pass def __A (self ) -> Dict: _lowercase =BertConfig( vocab_size=9_9 , hidden_size=3_2 , num_hidden_layers=5 , num_attention_heads=4 , intermediate_size=3_7 ) config.push_to_hub('''test-config''' , use_auth_token=self._token ) _lowercase =BertConfig.from_pretrained(f"{USER}/test-config" ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(UpperCAmelCase , getattr(UpperCAmelCase , UpperCAmelCase ) ) # Reset repo delete_repo(token=self._token , repo_id='''test-config''' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained(UpperCAmelCase , repo_id='''test-config''' , push_to_hub=UpperCAmelCase , use_auth_token=self._token ) _lowercase =BertConfig.from_pretrained(f"{USER}/test-config" ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(UpperCAmelCase , getattr(UpperCAmelCase , UpperCAmelCase ) ) def __A (self ) -> Optional[Any]: _lowercase =BertConfig( vocab_size=9_9 , hidden_size=3_2 , num_hidden_layers=5 , num_attention_heads=4 , intermediate_size=3_7 ) config.push_to_hub('''valid_org/test-config-org''' , use_auth_token=self._token ) _lowercase =BertConfig.from_pretrained('''valid_org/test-config-org''' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(UpperCAmelCase , getattr(UpperCAmelCase , UpperCAmelCase ) ) # Reset repo delete_repo(token=self._token , repo_id='''valid_org/test-config-org''' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained( UpperCAmelCase , repo_id='''valid_org/test-config-org''' , push_to_hub=UpperCAmelCase , use_auth_token=self._token ) _lowercase =BertConfig.from_pretrained('''valid_org/test-config-org''' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(UpperCAmelCase , getattr(UpperCAmelCase , UpperCAmelCase ) ) def __A (self ) -> str: CustomConfig.register_for_auto_class() _lowercase =CustomConfig(attribute=4_2 ) config.push_to_hub('''test-dynamic-config''' , use_auth_token=self._token ) # This has added the proper auto_map field to the config self.assertDictEqual(config.auto_map , {'''AutoConfig''': '''custom_configuration.CustomConfig'''} ) _lowercase =AutoConfig.from_pretrained(f"{USER}/test-dynamic-config" , trust_remote_code=UpperCAmelCase ) # Can't make an isinstance check because the new_config is from the FakeConfig class of a dynamic module self.assertEqual(new_config.__class__.__name__ , '''CustomConfig''' ) self.assertEqual(new_config.attribute , 4_2 ) class lowerCamelCase__ ( unittest.TestCase): def __A (self ) -> Any: _lowercase =GPTaConfig() # attempt to modify each of int/float/bool/str config records and verify they were updated _lowercase =c.n_embd + 1 # int _lowercase =c.resid_pdrop + 1.0 # float _lowercase =not c.scale_attn_weights # bool _lowercase =c.summary_type + '''foo''' # str c.update_from_string( f"n_embd={n_embd},resid_pdrop={resid_pdrop},scale_attn_weights={scale_attn_weights},summary_type={summary_type}" ) self.assertEqual(UpperCAmelCase , c.n_embd , '''mismatch for key: n_embd''' ) self.assertEqual(UpperCAmelCase , c.resid_pdrop , '''mismatch for key: resid_pdrop''' ) self.assertEqual(UpperCAmelCase , c.scale_attn_weights , '''mismatch for key: scale_attn_weights''' ) self.assertEqual(UpperCAmelCase , c.summary_type , '''mismatch for key: summary_type''' ) def __A (self ) -> Union[str, Any]: _lowercase =PretrainedConfig() _lowercase =[key for key in base_config.__dict__ if key not in config_common_kwargs] # If this part of the test fails, you have arguments to addin config_common_kwargs above. self.assertListEqual( UpperCAmelCase , ['''is_encoder_decoder''', '''_name_or_path''', '''_commit_hash''', '''transformers_version'''] ) _lowercase =[key for key, value in config_common_kwargs.items() if value == getattr(UpperCAmelCase , UpperCAmelCase )] if len(UpperCAmelCase ) > 0: raise ValueError( '''The following keys are set with the default values in''' ''' `test_configuration_common.config_common_kwargs` pick another value for them:''' f" {', '.join(UpperCAmelCase )}." ) def __A (self ) -> Optional[int]: with self.assertRaises(UpperCAmelCase ): # config is in subfolder, the following should not work without specifying the subfolder _lowercase =BertConfig.from_pretrained('''hf-internal-testing/tiny-random-bert-subfolder''' ) _lowercase =BertConfig.from_pretrained('''hf-internal-testing/tiny-random-bert-subfolder''' , subfolder='''bert''' ) self.assertIsNotNone(UpperCAmelCase ) def __A (self ) -> List[str]: # A mock response for an HTTP head request to emulate server down _lowercase =mock.Mock() _lowercase =5_0_0 _lowercase ={} _lowercase =HTTPError _lowercase ={} # Download this model to make sure it's in the cache. _lowercase =BertConfig.from_pretrained('''hf-internal-testing/tiny-random-bert''' ) # Under the mock environment we get a 500 error when trying to reach the model. with mock.patch('''requests.Session.request''' , return_value=UpperCAmelCase ) as mock_head: _lowercase =BertConfig.from_pretrained('''hf-internal-testing/tiny-random-bert''' ) # This check we did call the fake head request mock_head.assert_called() def __A (self ) -> Union[str, Any]: # This test is for deprecated behavior and can be removed in v5 _lowercase =BertConfig.from_pretrained( '''https://huggingface.co/hf-internal-testing/tiny-random-bert/resolve/main/config.json''' ) def __A (self ) -> Any: _lowercase =AutoConfig.from_pretrained('''bert-base-cased''' ) _lowercase =['''config.4.0.0.json'''] with tempfile.TemporaryDirectory() as tmp_dir: configuration.save_pretrained(UpperCAmelCase ) _lowercase =2 json.dump(configuration.to_dict() , open(os.path.join(UpperCAmelCase , '''config.4.0.0.json''' ) , '''w''' ) ) # This should pick the new configuration file as the version of Transformers is > 4.0.0 _lowercase =AutoConfig.from_pretrained(UpperCAmelCase ) self.assertEqual(new_configuration.hidden_size , 2 ) # Will need to be adjusted if we reach v42 and this test is still here. # Should pick the old configuration file as the version of Transformers is < 4.42.0 _lowercase =['''config.42.0.0.json'''] _lowercase =7_6_8 configuration.save_pretrained(UpperCAmelCase ) shutil.move(os.path.join(UpperCAmelCase , '''config.4.0.0.json''' ) , os.path.join(UpperCAmelCase , '''config.42.0.0.json''' ) ) _lowercase =AutoConfig.from_pretrained(UpperCAmelCase ) self.assertEqual(new_configuration.hidden_size , 7_6_8 ) def __A (self ) -> List[Any]: # This repo has two configuration files, one for v4.0.0 and above with a different hidden size. _lowercase ='''hf-internal-testing/test-two-configs''' import transformers as new_transformers _lowercase ='''v4.0.0''' _lowercase , _lowercase =new_transformers.models.auto.AutoConfig.from_pretrained( UpperCAmelCase , return_unused_kwargs=UpperCAmelCase ) self.assertEqual(new_configuration.hidden_size , 2 ) # This checks `_configuration_file` ia not kept in the kwargs by mistake. self.assertDictEqual(UpperCAmelCase , {} ) # Testing an older version by monkey-patching the version in the module it's used. import transformers as old_transformers _lowercase ='''v3.0.0''' _lowercase =old_transformers.models.auto.AutoConfig.from_pretrained(UpperCAmelCase ) self.assertEqual(old_configuration.hidden_size , 7_6_8 )
5
def UpperCAmelCase_ ( __snake_case ) -> str: """simple docstring""" _lowercase =0 # if input_string is "aba" than new_input_string become "a|b|a" _lowercase ='''''' _lowercase ='''''' # append each character + "|" in new_string for range(0, length-1) for i in input_string[: len(__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 _lowercase , _lowercase =0, 0 # length[i] shows the length of palindromic substring with center i _lowercase =[1 for i in range(len(__snake_case ) )] # for each character in new_string find corresponding palindromic string _lowercase =0 for j in range(len(__snake_case ) ): _lowercase =1 if j > r else min(length[l + r - j] // 2 , r - j + 1 ) while ( j - k >= 0 and j + k < len(__snake_case ) and new_input_string[k + j] == new_input_string[j - k] ): k += 1 _lowercase =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: _lowercase =j - k + 1 # noqa: E741 _lowercase =j + k - 1 # update max_length and start position if max_length < length[j]: _lowercase =length[j] _lowercase =j # create that string _lowercase =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()
5
1
def UpperCAmelCase_ ( __snake_case , __snake_case ) -> tuple[float, float]: """simple docstring""" if not len(__snake_case ) == len(__snake_case ) == 3: raise ValueError('''Please enter a valid equation.''' ) if equationa[0] == equationa[1] == equationa[0] == equationa[1] == 0: raise ValueError('''Both a & b of two equations can\'t be zero.''' ) # Extract the coefficients _lowercase , _lowercase , _lowercase =equationa _lowercase , _lowercase , _lowercase =equationa # Calculate the determinants of the matrices _lowercase =aa * ba - aa * ba _lowercase =ca * ba - ca * ba _lowercase =aa * ca - aa * ca # Check if the system of linear equations has a solution (using Cramer's rule) if determinant == 0: if determinant_x == determinant_y == 0: raise ValueError('''Infinite solutions. (Consistent system)''' ) else: raise ValueError('''No solution. (Inconsistent system)''' ) else: if determinant_x == determinant_y == 0: # Trivial solution (Inconsistent system) return (0.0, 0.0) else: _lowercase =determinant_x / determinant _lowercase =determinant_y / determinant # Non-Trivial Solution (Consistent system) return (x, y)
5
from math import isqrt def UpperCAmelCase_ ( __snake_case ) -> list[int]: """simple docstring""" _lowercase =[True] * max_number for i in range(2 , isqrt(max_number - 1 ) + 1 ): if is_prime[i]: for j in range(i**2 , __snake_case , __snake_case ): _lowercase =False return [i for i in range(2 , __snake_case ) if is_prime[i]] def UpperCAmelCase_ ( __snake_case = 10**8 ) -> int: """simple docstring""" _lowercase =calculate_prime_numbers(max_number // 2 ) _lowercase =0 _lowercase =0 _lowercase =len(__snake_case ) - 1 while left <= right: while prime_numbers[left] * prime_numbers[right] >= max_number: right -= 1 semiprimes_count += right - left + 1 left += 1 return semiprimes_count if __name__ == "__main__": print(f'''{solution() = }''')
5
1
# NOTE: This file is deprecated and will be removed in a future version. # It only exists so that temporarely `from diffusers.pipelines import DiffusionPipeline` works from ...utils import deprecate from ..controlnet.pipeline_flax_controlnet import FlaxStableDiffusionControlNetPipeline # noqa: F401 deprecate( '''stable diffusion controlnet''', '''0.22.0''', '''Importing `FlaxStableDiffusionControlNetPipeline` from diffusers.pipelines.stable_diffusion.flax_pipeline_stable_diffusion_controlnet is deprecated. Please import `from diffusers import FlaxStableDiffusionControlNetPipeline` instead.''', standard_warn=False, stacklevel=3, )
5
UpperCAmelCase__ = { '''A''': '''.-''', '''B''': '''-...''', '''C''': '''-.-.''', '''D''': '''-..''', '''E''': '''.''', '''F''': '''..-.''', '''G''': '''--.''', '''H''': '''....''', '''I''': '''..''', '''J''': '''.---''', '''K''': '''-.-''', '''L''': '''.-..''', '''M''': '''--''', '''N''': '''-.''', '''O''': '''---''', '''P''': '''.--.''', '''Q''': '''--.-''', '''R''': '''.-.''', '''S''': '''...''', '''T''': '''-''', '''U''': '''..-''', '''V''': '''...-''', '''W''': '''.--''', '''X''': '''-..-''', '''Y''': '''-.--''', '''Z''': '''--..''', '''1''': '''.----''', '''2''': '''..---''', '''3''': '''...--''', '''4''': '''....-''', '''5''': '''.....''', '''6''': '''-....''', '''7''': '''--...''', '''8''': '''---..''', '''9''': '''----.''', '''0''': '''-----''', '''&''': '''.-...''', '''@''': '''.--.-.''', ''':''': '''---...''', ''',''': '''--..--''', '''.''': '''.-.-.-''', '''\'''': '''.----.''', '''"''': '''.-..-.''', '''?''': '''..--..''', '''/''': '''-..-.''', '''=''': '''-...-''', '''+''': '''.-.-.''', '''-''': '''-....-''', '''(''': '''-.--.''', ''')''': '''-.--.-''', '''!''': '''-.-.--''', ''' ''': '''/''' } # Exclamation mark is not in ITU-R recommendation # fmt: on UpperCAmelCase__ = {value: key for key, value in MORSE_CODE_DICT.items()} def UpperCAmelCase_ ( __snake_case ) -> str: """simple docstring""" return " ".join(MORSE_CODE_DICT[char] for char in message.upper() ) def UpperCAmelCase_ ( __snake_case ) -> str: """simple docstring""" return "".join(REVERSE_DICT[char] for char in message.split() ) def UpperCAmelCase_ ( ) -> None: """simple docstring""" _lowercase ='''Morse code here!''' print(__snake_case ) _lowercase =encrypt(__snake_case ) print(__snake_case ) _lowercase =decrypt(__snake_case ) print(__snake_case ) if __name__ == "__main__": main()
5
1
import argparse import os import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType from accelerate.local_sgd import LocalSGD ######################################################################## # This is a fully working simple example to use Accelerate # with LocalSGD, which is a method to synchronize model # parameters every K batches. It is different, but complementary # to gradient accumulation. # # This example trains a Bert base model on GLUE MRPC # in any of the following settings (with the same script): # - single CPU or single GPU # - multi GPUS (using PyTorch distributed mode) # - (multi) TPUs # - fp16 (mixed-precision) or fp32 (normal precision) # # To run it in each of these various modes, follow the instructions # in the readme for examples: # https://github.com/huggingface/accelerate/tree/main/examples # ######################################################################## UpperCAmelCase__ = 16 UpperCAmelCase__ = 32 def UpperCAmelCase_ ( __snake_case , __snake_case = 16 ) -> str: """simple docstring""" _lowercase =AutoTokenizer.from_pretrained('''bert-base-cased''' ) _lowercase =load_dataset('''glue''' , '''mrpc''' ) def tokenize_function(__snake_case ): # max_length=None => use the model max length (it's actually the default) _lowercase =tokenizer(examples['''sentence1'''] , examples['''sentence2'''] , truncation=__snake_case , max_length=__snake_case ) return outputs # Apply the method we just defined to all the examples in all the splits of the dataset # starting with the main process first: with accelerator.main_process_first(): _lowercase =datasets.map( __snake_case , batched=__snake_case , remove_columns=['''idx''', '''sentence1''', '''sentence2'''] , ) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library _lowercase =tokenized_datasets.rename_column('''label''' , '''labels''' ) def collate_fn(__snake_case ): # On TPU it's best to pad everything to the same length or training will be very slow. _lowercase =128 if accelerator.distributed_type == DistributedType.TPU else None # When using mixed precision we want round multiples of 8/16 if accelerator.mixed_precision == "fp8": _lowercase =16 elif accelerator.mixed_precision != "no": _lowercase =8 else: _lowercase =None return tokenizer.pad( __snake_case , padding='''longest''' , max_length=__snake_case , pad_to_multiple_of=__snake_case , return_tensors='''pt''' , ) # Instantiate dataloaders. _lowercase =DataLoader( tokenized_datasets['''train'''] , shuffle=__snake_case , collate_fn=__snake_case , batch_size=__snake_case ) _lowercase =DataLoader( tokenized_datasets['''validation'''] , shuffle=__snake_case , collate_fn=__snake_case , batch_size=__snake_case ) return train_dataloader, eval_dataloader # For testing only if os.environ.get('''TESTING_MOCKED_DATALOADERS''', None) == "1": from accelerate.test_utils.training import mocked_dataloaders UpperCAmelCase__ = mocked_dataloaders # noqa: F811 def UpperCAmelCase_ ( __snake_case , __snake_case ) -> List[Any]: """simple docstring""" if os.environ.get('''TESTING_MOCKED_DATALOADERS''' , __snake_case ) == "1": _lowercase =2 # New Code # _lowercase =int(args.gradient_accumulation_steps ) _lowercase =int(args.local_sgd_steps ) # Initialize accelerator _lowercase =Accelerator( cpu=args.cpu , mixed_precision=args.mixed_precision , gradient_accumulation_steps=__snake_case ) if accelerator.distributed_type not in [DistributedType.NO, DistributedType.MULTI_CPU, DistributedType.MULTI_GPU]: raise NotImplementedError('''LocalSGD is supported only for CPUs and GPUs (no DeepSpeed or MegatronLM)''' ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs _lowercase =config['''lr'''] _lowercase =int(config['''num_epochs'''] ) _lowercase =int(config['''seed'''] ) _lowercase =int(config['''batch_size'''] ) _lowercase =evaluate.load('''glue''' , '''mrpc''' ) set_seed(__snake_case ) _lowercase , _lowercase =get_dataloaders(__snake_case , __snake_case ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) _lowercase =AutoModelForSequenceClassification.from_pretrained('''bert-base-cased''' , return_dict=__snake_case ) # We could avoid this line since the accelerator is set with `device_placement=True` (default value). # Note that if you are placing tensors on devices manually, this line absolutely needs to be before the optimizer # creation otherwise training will not work on TPU (`accelerate` will kindly throw an error to make us aware of that). _lowercase =model.to(accelerator.device ) # Instantiate optimizer _lowercase =AdamW(params=model.parameters() , lr=__snake_case ) # Instantiate scheduler _lowercase =get_linear_schedule_with_warmup( optimizer=__snake_case , num_warmup_steps=100 , num_training_steps=(len(__snake_case ) * num_epochs) , ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. _lowercase , _lowercase , _lowercase , _lowercase , _lowercase =accelerator.prepare( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case ) # Now we train the model for epoch in range(__snake_case ): model.train() with LocalSGD( accelerator=__snake_case , model=__snake_case , local_sgd_steps=__snake_case , enabled=local_sgd_steps is not None ) as local_sgd: for step, batch in enumerate(__snake_case ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) # New code # # We use the new `accumulate` context manager to perform gradient accumulation # We also currently do not support TPUs nor advise it as bugs were found on the XLA side when running our tests. with accelerator.accumulate(__snake_case ): _lowercase =model(**__snake_case ) _lowercase =output.loss accelerator.backward(__snake_case ) optimizer.step() lr_scheduler.step() optimizer.zero_grad() # LocalSGD-specific line local_sgd.step() model.eval() for step, batch in enumerate(__snake_case ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): _lowercase =model(**__snake_case ) _lowercase =outputs.logits.argmax(dim=-1 ) _lowercase , _lowercase =accelerator.gather_for_metrics((predictions, batch['''labels''']) ) metric.add_batch( predictions=__snake_case , references=__snake_case , ) _lowercase =metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(F"epoch {epoch}:" , __snake_case ) def UpperCAmelCase_ ( ) -> Optional[int]: """simple docstring""" _lowercase =argparse.ArgumentParser(description='''Simple example of training script.''' ) parser.add_argument( '''--mixed_precision''' , type=__snake_case , default=__snake_case , choices=['''no''', '''fp16''', '''bf16''', '''fp8'''] , help='''Whether to use mixed precision. Choose''' '''between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10.''' '''and an Nvidia Ampere GPU.''' , ) # New Code # parser.add_argument( '''--gradient_accumulation_steps''' , type=__snake_case , default=1 , help='''The number of minibatches to be ran before gradients are accumulated.''' , ) parser.add_argument( '''--local_sgd_steps''' , type=__snake_case , default=8 , help='''Number of local SGD steps or None to disable local SGD''' ) parser.add_argument('''--cpu''' , action='''store_true''' , help='''If passed, will train on the CPU.''' ) _lowercase =parser.parse_args() _lowercase ={'''lr''': 2e-5, '''num_epochs''': 3, '''seed''': 42, '''batch_size''': 16} training_function(__snake_case , __snake_case ) if __name__ == "__main__": main()
5
from typing import Any def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , ) -> list: """simple docstring""" _validation( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , ) # Creates data structures and fill initial step _lowercase ={} _lowercase ={} for state in states_space: _lowercase =observations_space[0] _lowercase =( initial_probabilities[state] * emission_probabilities[state][observation] ) _lowercase =None # Fills the data structure with the probabilities of # different transitions and pointers to previous states for o in range(1 , len(__snake_case ) ): _lowercase =observations_space[o] _lowercase =observations_space[o - 1] for state in states_space: # Calculates the argmax for probability function _lowercase ='''''' _lowercase =-1 for k_state in states_space: _lowercase =( probabilities[(k_state, prior_observation)] * transition_probabilities[k_state][state] * emission_probabilities[state][observation] ) if probability > max_probability: _lowercase =probability _lowercase =k_state # Update probabilities and pointers dicts _lowercase =( probabilities[(arg_max, prior_observation)] * transition_probabilities[arg_max][state] * emission_probabilities[state][observation] ) _lowercase =arg_max # The final observation _lowercase =observations_space[len(__snake_case ) - 1] # argmax for given final observation _lowercase ='''''' _lowercase =-1 for k_state in states_space: _lowercase =probabilities[(k_state, final_observation)] if probability > max_probability: _lowercase =probability _lowercase =k_state _lowercase =arg_max # Process pointers backwards _lowercase =last_state _lowercase =[] for o in range(len(__snake_case ) - 1 , -1 , -1 ): result.append(__snake_case ) _lowercase =pointers[previous, observations_space[o]] result.reverse() return result def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , ) -> None: """simple docstring""" _validate_not_empty( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , ) _validate_lists(__snake_case , __snake_case ) _validate_dicts( __snake_case , __snake_case , __snake_case ) def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , ) -> None: """simple docstring""" if not all( [ observations_space, states_space, initial_probabilities, transition_probabilities, emission_probabilities, ] ): raise ValueError('''There\'s an empty parameter''' ) def UpperCAmelCase_ ( __snake_case , __snake_case ) -> None: """simple docstring""" _validate_list(__snake_case , '''observations_space''' ) _validate_list(__snake_case , '''states_space''' ) def UpperCAmelCase_ ( __snake_case , __snake_case ) -> None: """simple docstring""" if not isinstance(_object , __snake_case ): _lowercase =F"{var_name} must be a list" raise ValueError(__snake_case ) else: for x in _object: if not isinstance(__snake_case , __snake_case ): _lowercase =F"{var_name} must be a list of strings" raise ValueError(__snake_case ) def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , ) -> None: """simple docstring""" _validate_dict(__snake_case , '''initial_probabilities''' , __snake_case ) _validate_nested_dict(__snake_case , '''transition_probabilities''' ) _validate_nested_dict(__snake_case , '''emission_probabilities''' ) def UpperCAmelCase_ ( __snake_case , __snake_case ) -> None: """simple docstring""" _validate_dict(_object , __snake_case , __snake_case ) for x in _object.values(): _validate_dict(__snake_case , __snake_case , __snake_case , __snake_case ) def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case = False ) -> None: """simple docstring""" if not isinstance(_object , __snake_case ): _lowercase =F"{var_name} must be a dict" raise ValueError(__snake_case ) if not all(isinstance(__snake_case , __snake_case ) for x in _object ): _lowercase =F"{var_name} all keys must be strings" raise ValueError(__snake_case ) if not all(isinstance(__snake_case , __snake_case ) for x in _object.values() ): _lowercase ='''nested dictionary ''' if nested else '''''' _lowercase =F"{var_name} {nested_text}all values must be {value_type.__name__}" raise ValueError(__snake_case ) if __name__ == "__main__": from doctest import testmod testmod()
5
1
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 UpperCAmelCase__ = [ '''python''', '''tqdm''', '''regex''', '''requests''', '''packaging''', '''filelock''', '''numpy''', '''tokenizers''', '''huggingface-hub''', '''safetensors''', '''accelerate''', '''pyyaml''', ] for pkg in pkgs_to_check_at_runtime: if pkg in deps: if pkg == "tokenizers": # must be loaded here, or else tqdm check may fail from .utils import is_tokenizers_available if not is_tokenizers_available(): continue # not required, check version only if installed elif pkg == "accelerate": # must be loaded here, or else tqdm check may fail from .utils import is_accelerate_available # Maybe switch to is_torch_available in the future here so that Accelerate is hard dep of # Transformers with PyTorch if not is_accelerate_available(): continue # not required, check version only if installed require_version_core(deps[pkg]) else: raise ValueError(f'''can\'t find {pkg} in {deps.keys()}, check dependency_versions_table.py''') def UpperCAmelCase_ ( __snake_case , __snake_case=None ) -> Union[str, Any]: """simple docstring""" require_version(deps[pkg] , __snake_case )
5
from dataclasses import asdict, dataclass from typing import Optional from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCAmelCase__ = logging.get_logger(__name__) # TODO Update this UpperCAmelCase__ = { '''facebook/esm-1b''': '''https://huggingface.co/facebook/esm-1b/resolve/main/config.json''', # See all ESM models at https://huggingface.co/models?filter=esm } class lowerCamelCase__ ( lowerCAmelCase): SCREAMING_SNAKE_CASE__ = '''esm''' def __init__(self , UpperCAmelCase=None , UpperCAmelCase=None , UpperCAmelCase=None , UpperCAmelCase=7_6_8 , UpperCAmelCase=1_2 , UpperCAmelCase=1_2 , UpperCAmelCase=3_0_7_2 , UpperCAmelCase=0.1 , UpperCAmelCase=0.1 , UpperCAmelCase=1_0_2_6 , UpperCAmelCase=0.02 , UpperCAmelCase=1e-12 , UpperCAmelCase="absolute" , UpperCAmelCase=True , UpperCAmelCase=None , UpperCAmelCase=False , UpperCAmelCase=False , UpperCAmelCase=None , UpperCAmelCase=None , **UpperCAmelCase , ) -> Tuple: super().__init__(pad_token_id=UpperCAmelCase , mask_token_id=UpperCAmelCase , **UpperCAmelCase ) _lowercase =vocab_size _lowercase =hidden_size _lowercase =num_hidden_layers _lowercase =num_attention_heads _lowercase =intermediate_size _lowercase =hidden_dropout_prob _lowercase =attention_probs_dropout_prob _lowercase =max_position_embeddings _lowercase =initializer_range _lowercase =layer_norm_eps _lowercase =position_embedding_type _lowercase =use_cache _lowercase =emb_layer_norm_before _lowercase =token_dropout _lowercase =is_folding_model if is_folding_model: if esmfold_config is None: logger.info('''No esmfold_config supplied for folding model, using default values.''' ) _lowercase =EsmFoldConfig() elif isinstance(UpperCAmelCase , UpperCAmelCase ): _lowercase =EsmFoldConfig(**UpperCAmelCase ) _lowercase =esmfold_config if vocab_list is None: logger.warning('''No vocab_list supplied for folding model, assuming the ESM-2 vocabulary!''' ) _lowercase =get_default_vocab_list() else: _lowercase =vocab_list else: _lowercase =None _lowercase =None if self.esmfold_config is not None and getattr(self.esmfold_config , '''use_esm_attn_map''' , UpperCAmelCase ): raise ValueError('''The HuggingFace port of ESMFold does not support use_esm_attn_map at this time!''' ) def __A (self ) -> List[str]: _lowercase =super().to_dict() if isinstance(self.esmfold_config , UpperCAmelCase ): _lowercase =self.esmfold_config.to_dict() return output @dataclass class lowerCamelCase__ : SCREAMING_SNAKE_CASE__ = None SCREAMING_SNAKE_CASE__ = True SCREAMING_SNAKE_CASE__ = False SCREAMING_SNAKE_CASE__ = False SCREAMING_SNAKE_CASE__ = False SCREAMING_SNAKE_CASE__ = 0 SCREAMING_SNAKE_CASE__ = True SCREAMING_SNAKE_CASE__ = False SCREAMING_SNAKE_CASE__ = 128 SCREAMING_SNAKE_CASE__ = None def __A (self ) -> Union[str, Any]: if self.trunk is None: _lowercase =TrunkConfig() elif isinstance(self.trunk , UpperCAmelCase ): _lowercase =TrunkConfig(**self.trunk ) def __A (self ) -> Tuple: _lowercase =asdict(self ) _lowercase =self.trunk.to_dict() return output @dataclass class lowerCamelCase__ : SCREAMING_SNAKE_CASE__ = 48 SCREAMING_SNAKE_CASE__ = 1024 SCREAMING_SNAKE_CASE__ = 128 SCREAMING_SNAKE_CASE__ = 32 SCREAMING_SNAKE_CASE__ = 32 SCREAMING_SNAKE_CASE__ = 32 SCREAMING_SNAKE_CASE__ = 0 SCREAMING_SNAKE_CASE__ = 0 SCREAMING_SNAKE_CASE__ = False SCREAMING_SNAKE_CASE__ = 4 SCREAMING_SNAKE_CASE__ = 128 SCREAMING_SNAKE_CASE__ = None def __A (self ) -> List[str]: if self.structure_module is None: _lowercase =StructureModuleConfig() elif isinstance(self.structure_module , UpperCAmelCase ): _lowercase =StructureModuleConfig(**self.structure_module ) if self.max_recycles <= 0: raise ValueError(f"`max_recycles` should be positive, got {self.max_recycles}." ) if self.sequence_state_dim % self.sequence_state_dim != 0: raise ValueError( '''`sequence_state_dim` should be a round multiple of `sequence_state_dim`, got''' f" {self.sequence_state_dim} and {self.sequence_state_dim}." ) if self.pairwise_state_dim % self.pairwise_state_dim != 0: raise ValueError( '''`pairwise_state_dim` should be a round multiple of `pairwise_state_dim`, got''' f" {self.pairwise_state_dim} and {self.pairwise_state_dim}." ) _lowercase =self.sequence_state_dim // self.sequence_head_width _lowercase =self.pairwise_state_dim // self.pairwise_head_width if self.sequence_state_dim != sequence_num_heads * self.sequence_head_width: raise ValueError( '''`sequence_state_dim` should be equal to `sequence_num_heads * sequence_head_width, got''' f" {self.sequence_state_dim} != {sequence_num_heads} * {self.sequence_head_width}." ) if self.pairwise_state_dim != pairwise_num_heads * self.pairwise_head_width: raise ValueError( '''`pairwise_state_dim` should be equal to `pairwise_num_heads * pairwise_head_width, got''' f" {self.pairwise_state_dim} != {pairwise_num_heads} * {self.pairwise_head_width}." ) if self.pairwise_state_dim % 2 != 0: raise ValueError(f"`pairwise_state_dim` should be even, got {self.pairwise_state_dim}." ) if self.dropout >= 0.4: raise ValueError(f"`dropout` should not be greater than 0.4, got {self.dropout}." ) def __A (self ) -> Dict: _lowercase =asdict(self ) _lowercase =self.structure_module.to_dict() return output @dataclass class lowerCamelCase__ : SCREAMING_SNAKE_CASE__ = 384 SCREAMING_SNAKE_CASE__ = 128 SCREAMING_SNAKE_CASE__ = 16 SCREAMING_SNAKE_CASE__ = 128 SCREAMING_SNAKE_CASE__ = 12 SCREAMING_SNAKE_CASE__ = 4 SCREAMING_SNAKE_CASE__ = 8 SCREAMING_SNAKE_CASE__ = 0.1 SCREAMING_SNAKE_CASE__ = 8 SCREAMING_SNAKE_CASE__ = 1 SCREAMING_SNAKE_CASE__ = 2 SCREAMING_SNAKE_CASE__ = 7 SCREAMING_SNAKE_CASE__ = 10 SCREAMING_SNAKE_CASE__ = 1E-8 SCREAMING_SNAKE_CASE__ = 1E5 def __A (self ) -> List[Any]: return asdict(self ) def UpperCAmelCase_ ( ) -> Tuple: """simple docstring""" return ( "<cls>", "<pad>", "<eos>", "<unk>", "L", "A", "G", "V", "S", "E", "R", "T", "I", "D", "P", "K", "Q", "N", "F", "Y", "M", "H", "W", "C", "X", "B", "U", "Z", "O", ".", "-", "<null_1>", "<mask>", )
5
1
import json from typing import TYPE_CHECKING, List, Optional, Tuple from tokenizers import pre_tokenizers from ...tokenization_utils_base import BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = {'''tokenizer_file''': '''tokenizer.json'''} UpperCAmelCase__ = { '''tokenizer_file''': { '''bigscience/tokenizer''': '''https://huggingface.co/bigscience/tokenizer/blob/main/tokenizer.json''', '''bigscience/bloom-560m''': '''https://huggingface.co/bigscience/bloom-560m/blob/main/tokenizer.json''', '''bigscience/bloom-1b1''': '''https://huggingface.co/bigscience/bloom-1b1/blob/main/tokenizer.json''', '''bigscience/bloom-1b7''': '''https://huggingface.co/bigscience/bloom-1b7/blob/main/tokenizer.json''', '''bigscience/bloom-3b''': '''https://huggingface.co/bigscience/bloom-3b/blob/main/tokenizer.json''', '''bigscience/bloom-7b1''': '''https://huggingface.co/bigscience/bloom-7b1/blob/main/tokenizer.json''', '''bigscience/bloom''': '''https://huggingface.co/bigscience/bloom/blob/main/tokenizer.json''', }, } class lowerCamelCase__ ( lowerCAmelCase): SCREAMING_SNAKE_CASE__ = VOCAB_FILES_NAMES SCREAMING_SNAKE_CASE__ = PRETRAINED_VOCAB_FILES_MAP SCREAMING_SNAKE_CASE__ = ['''input_ids''', '''attention_mask'''] SCREAMING_SNAKE_CASE__ = None def __init__(self , UpperCAmelCase=None , UpperCAmelCase=None , UpperCAmelCase=None , UpperCAmelCase="<unk>" , UpperCAmelCase="<s>" , UpperCAmelCase="</s>" , UpperCAmelCase="<pad>" , UpperCAmelCase=False , UpperCAmelCase=False , **UpperCAmelCase , ) -> Optional[Any]: super().__init__( UpperCAmelCase , UpperCAmelCase , tokenizer_file=UpperCAmelCase , unk_token=UpperCAmelCase , bos_token=UpperCAmelCase , eos_token=UpperCAmelCase , pad_token=UpperCAmelCase , add_prefix_space=UpperCAmelCase , clean_up_tokenization_spaces=UpperCAmelCase , **UpperCAmelCase , ) _lowercase =json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get('''add_prefix_space''' , UpperCAmelCase ) != add_prefix_space: _lowercase =getattr(UpperCAmelCase , pre_tok_state.pop('''type''' ) ) _lowercase =add_prefix_space _lowercase =pre_tok_class(**UpperCAmelCase ) _lowercase =add_prefix_space def __A (self , *UpperCAmelCase , **UpperCAmelCase ) -> BatchEncoding: _lowercase =kwargs.get('''is_split_into_words''' , UpperCAmelCase ) if not (self.add_prefix_space or not is_split_into_words): raise Exception( f"You need to instantiate {self.__class__.__name__} with add_prefix_space=True to use it with" ''' pretokenized inputs.''' ) return super()._batch_encode_plus(*UpperCAmelCase , **UpperCAmelCase ) def __A (self , *UpperCAmelCase , **UpperCAmelCase ) -> BatchEncoding: _lowercase =kwargs.get('''is_split_into_words''' , UpperCAmelCase ) if not (self.add_prefix_space or not is_split_into_words): raise Exception( f"You need to instantiate {self.__class__.__name__} with add_prefix_space=True to use it with" ''' pretokenized inputs.''' ) return super()._encode_plus(*UpperCAmelCase , **UpperCAmelCase ) def __A (self , UpperCAmelCase , UpperCAmelCase = None ) -> Tuple[str]: _lowercase =self._tokenizer.model.save(UpperCAmelCase , name=UpperCAmelCase ) return tuple(UpperCAmelCase ) def __A (self , UpperCAmelCase ) -> List[int]: _lowercase =[] for is_user, text in conversation.iter_texts(): input_ids.extend(self.encode(UpperCAmelCase , add_special_tokens=UpperCAmelCase ) + [self.eos_token_id] ) if len(UpperCAmelCase ) > self.model_max_length: _lowercase =input_ids[-self.model_max_length :] return input_ids
5
import json import multiprocessing import os import re from collections import defaultdict import torch from accelerate import Accelerator from accelerate.utils import set_seed from arguments import HumanEvalArguments from datasets import load_dataset, load_metric from torch.utils.data import IterableDataset from torch.utils.data.dataloader import DataLoader from tqdm import tqdm import transformers from transformers import AutoModelForCausalLM, AutoTokenizer, HfArgumentParser, StoppingCriteria, StoppingCriteriaList UpperCAmelCase__ = ['''\nclass''', '''\ndef''', '''\n#''', '''\n@''', '''\nprint''', '''\nif'''] class lowerCamelCase__ ( lowerCAmelCase): def __init__(self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase=None , UpperCAmelCase=1 ) -> Dict: _lowercase =tokenizer _lowercase =dataset _lowercase =len(UpperCAmelCase ) if n_tasks is None else n_tasks _lowercase =n_copies def __iter__(self ) -> Optional[Any]: _lowercase =[] for task in range(self.n_tasks ): # without strip, the model generate commented codes ... prompts.append(self.tokenizer.eos_token + self.dataset[task]['''prompt'''].strip() ) _lowercase =self.tokenizer(UpperCAmelCase , padding=UpperCAmelCase , return_tensors='''pt''' ) for task in range(self.n_tasks ): for _ in range(self.n_copies ): yield { "ids": outputs.input_ids[task], "task_id": task, "input_len": outputs.attention_mask[task].sum(), } class lowerCamelCase__ ( lowerCAmelCase): def __init__(self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) -> Optional[Any]: _lowercase =start_length _lowercase =eof_strings _lowercase =tokenizer def __call__(self , UpperCAmelCase , UpperCAmelCase , **UpperCAmelCase ) -> Dict: _lowercase =self.tokenizer.batch_decode(input_ids[:, self.start_length :] ) _lowercase =[] for decoded_generation in decoded_generations: done.append(any(stop_string in decoded_generation for stop_string in self.eof_strings ) ) return all(UpperCAmelCase ) def UpperCAmelCase_ ( __snake_case ) -> Optional[Any]: """simple docstring""" _lowercase =re.split('''(%s)''' % '''|'''.join(__snake_case ) , __snake_case ) # last string should be "" return "".join(string_list[:-2] ) def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , __snake_case=20 , **__snake_case ) -> Tuple: """simple docstring""" _lowercase =defaultdict(__snake_case ) # dict of list of generated tokens for step, batch in tqdm(enumerate(__snake_case ) ): with torch.no_grad(): _lowercase =batch['''ids'''].shape[-1] _lowercase =accelerator.unwrap_model(__snake_case ).generate( input_ids=batch['''ids'''][:, : batch['''input_len''']] , num_return_sequences=__snake_case , **__snake_case ) # each task is generated batch_size times _lowercase =batch['''task_id'''].repeat(__snake_case ) _lowercase =accelerator.pad_across_processes( __snake_case , dim=1 , pad_index=tokenizer.pad_token_id ) _lowercase , _lowercase =accelerator.gather((generated_tokens, generated_tasks) ) _lowercase =generated_tokens.cpu().numpy() _lowercase =generated_tasks.cpu().numpy() for task, generated_tokens in zip(__snake_case , __snake_case ): gen_token_dict[task].append(__snake_case ) _lowercase =[[] for _ in range(__snake_case )] for task, generated_tokens in gen_token_dict.items(): for s in generated_tokens: _lowercase =tokenizer.decode(__snake_case , skip_special_tokens=__snake_case , clean_up_tokenization_spaces=__snake_case ) code_gens[task].append(remove_last_block(__snake_case ) ) return code_gens def UpperCAmelCase_ ( ) -> str: """simple docstring""" _lowercase =HfArgumentParser(__snake_case ) _lowercase =parser.parse_args() transformers.logging.set_verbosity_error() # enables code execution in code_eval metric _lowercase =args.HF_ALLOW_CODE_EVAL # make sure tokenizer plays nice with multiprocessing _lowercase ='''false''' if args.num_workers is None: _lowercase =multiprocessing.cpu_count() # Use dataset load to feed to accelerate _lowercase =Accelerator() set_seed(args.seed , device_specific=__snake_case ) # Load model and tokenizer _lowercase =AutoTokenizer.from_pretrained(args.model_ckpt ) _lowercase =tokenizer.eos_token _lowercase =AutoModelForCausalLM.from_pretrained(args.model_ckpt ) # Generation settings _lowercase ={ '''do_sample''': args.do_sample, '''temperature''': args.temperature, '''max_new_tokens''': args.max_new_tokens, '''top_p''': args.top_p, '''top_k''': args.top_k, '''stopping_criteria''': StoppingCriteriaList([EndOfFunctionCriteria(0 , __snake_case , __snake_case )] ), } # Load evaluation dataset and metric _lowercase =load_dataset('''openai_humaneval''' ) _lowercase =load_metric('''code_eval''' ) _lowercase =args.num_tasks if args.num_tasks is not None else len(human_eval['''test'''] ) _lowercase =args.n_samples // args.batch_size _lowercase =TokenizedDataset(__snake_case , human_eval['''test'''] , n_copies=__snake_case , n_tasks=__snake_case ) # do not confuse args.batch_size, which is actually the num_return_sequences _lowercase =DataLoader(__snake_case , batch_size=1 ) # Run a quick test to see if code evaluation is enabled try: _lowercase =code_eval_metric.compute(references=[''''''] , predictions=[['''''']] ) except ValueError as exception: print( '''Code evaluation not enabled. Read the warning below carefully and then use `--HF_ALLOW_CODE_EVAL="1"`''' ''' flag to enable code evaluation.''' ) raise exception _lowercase , _lowercase =accelerator.prepare(__snake_case , __snake_case ) _lowercase =complete_code( __snake_case , __snake_case , __snake_case , __snake_case , n_tasks=__snake_case , batch_size=args.batch_size , **__snake_case , ) if accelerator.is_main_process: _lowercase =[] for task in tqdm(range(__snake_case ) ): _lowercase =human_eval['''test'''][task]['''test'''] _lowercase =F"check({human_eval['test'][task]['entry_point']})" references.append('''\n''' + test_func + '''\n''' + entry_point ) # Evaluate completions with "code_eval" metric _lowercase , _lowercase =code_eval_metric.compute( references=__snake_case , predictions=__snake_case , num_workers=args.num_workers ) print(F"Results: {pass_at_k}" ) # Save results to json file with open(args.output_file , '''w''' ) as fp: json.dump(__snake_case , __snake_case ) # For some reason the folliwng seems to be necessary sometimes for code_eval to work nice with multiprocessing # https://stackoverflow.com/questions/60804599/python-multiprocessing-keeps-spawning-the-whole-script if __name__ == "__main__": main()
5
1
from math import factorial UpperCAmelCase__ = {str(digit): factorial(digit) for digit in range(10)} def UpperCAmelCase_ ( __snake_case ) -> int: """simple docstring""" if not isinstance(__snake_case , __snake_case ): raise TypeError('''Parameter number must be int''' ) if number < 0: raise ValueError('''Parameter number must be greater than or equal to 0''' ) # Converts number in string to iterate on its digits and adds its factorial. return sum(DIGIT_FACTORIAL[digit] for digit in str(__snake_case ) ) def UpperCAmelCase_ ( __snake_case = 60 , __snake_case = 1000000 ) -> int: """simple docstring""" if not isinstance(__snake_case , __snake_case ) or not isinstance(__snake_case , __snake_case ): raise TypeError('''Parameters chain_length and number_limit must be int''' ) if chain_length <= 0 or number_limit <= 0: raise ValueError( '''Parameters chain_length and number_limit must be greater than 0''' ) # the counter for the chains with the exact desired length _lowercase =0 # the cached sizes of the previous chains _lowercase ={} for start_chain_element in range(1 , __snake_case ): # The temporary set will contain the elements of the chain _lowercase =set() _lowercase =0 # Stop computing the chain when you find a cached size, a repeating item or the # length is greater then the desired one. _lowercase =start_chain_element while ( chain_element not in chain_sets_lengths and chain_element not in chain_set and chain_set_length <= chain_length ): chain_set.add(__snake_case ) chain_set_length += 1 _lowercase =digit_factorial_sum(__snake_case ) if chain_element in chain_sets_lengths: chain_set_length += chain_sets_lengths[chain_element] _lowercase =chain_set_length # If chain contains the exact amount of elements increase the counter if chain_set_length == chain_length: chains_counter += 1 return chains_counter if __name__ == "__main__": import doctest doctest.testmod() print(f'''{solution()}''')
5
UpperCAmelCase__ = 8.31_44_62 # Unit - J mol-1 K-1 def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case ) -> float: """simple docstring""" if moles < 0 or kelvin < 0 or volume < 0: raise ValueError('''Invalid inputs. Enter positive value.''' ) return moles * kelvin * UNIVERSAL_GAS_CONSTANT / volume def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case ) -> float: """simple docstring""" if moles < 0 or kelvin < 0 or pressure < 0: raise ValueError('''Invalid inputs. Enter positive value.''' ) return moles * kelvin * UNIVERSAL_GAS_CONSTANT / pressure if __name__ == "__main__": from doctest import testmod testmod()
5
1
from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = { '''google/vivit-b-16x2-kinetics400''': ( '''https://huggingface.co/google/vivit-b-16x2-kinetics400/resolve/main/config.json''' ), # See all Vivit models at https://huggingface.co/models?filter=vivit } class lowerCamelCase__ ( lowerCAmelCase): SCREAMING_SNAKE_CASE__ = '''vivit''' def __init__(self , UpperCAmelCase=2_2_4 , UpperCAmelCase=3_2 , UpperCAmelCase=[2, 1_6, 1_6] , UpperCAmelCase=3 , UpperCAmelCase=7_6_8 , UpperCAmelCase=1_2 , UpperCAmelCase=1_2 , UpperCAmelCase=3_0_7_2 , UpperCAmelCase="gelu_fast" , UpperCAmelCase=0.0 , UpperCAmelCase=0.0 , UpperCAmelCase=0.02 , UpperCAmelCase=1e-06 , UpperCAmelCase=True , **UpperCAmelCase , ) -> List[str]: _lowercase =hidden_size _lowercase =num_hidden_layers _lowercase =num_attention_heads _lowercase =intermediate_size _lowercase =hidden_act _lowercase =hidden_dropout_prob _lowercase =attention_probs_dropout_prob _lowercase =initializer_range _lowercase =layer_norm_eps _lowercase =image_size _lowercase =num_frames _lowercase =tubelet_size _lowercase =num_channels _lowercase =qkv_bias super().__init__(**UpperCAmelCase )
5
from __future__ import annotations from collections.abc import Callable UpperCAmelCase__ = list[list[float | int]] def UpperCAmelCase_ ( __snake_case , __snake_case ) -> Matrix: """simple docstring""" _lowercase =len(__snake_case ) _lowercase =[[0 for _ in range(size + 1 )] for _ in range(__snake_case )] _lowercase =42 _lowercase =42 _lowercase =42 _lowercase =42 _lowercase =42 _lowercase =42 for row in range(__snake_case ): for col in range(__snake_case ): _lowercase =matrix[row][col] _lowercase =vector[row][0] _lowercase =0 _lowercase =0 while row < size and col < size: # pivoting _lowercase =max((abs(augmented[rowa][col] ), rowa) for rowa in range(__snake_case , __snake_case ) )[ 1 ] if augmented[pivot_row][col] == 0: col += 1 continue else: _lowercase , _lowercase =augmented[pivot_row], augmented[row] for rowa in range(row + 1 , __snake_case ): _lowercase =augmented[rowa][col] / augmented[row][col] _lowercase =0 for cola in range(col + 1 , size + 1 ): augmented[rowa][cola] -= augmented[row][cola] * ratio row += 1 col += 1 # back substitution for col in range(1 , __snake_case ): for row in range(__snake_case ): _lowercase =augmented[row][col] / augmented[col][col] for cola in range(__snake_case , size + 1 ): augmented[row][cola] -= augmented[col][cola] * ratio # round to get rid of numbers like 2.000000000000004 return [ [round(augmented[row][size] / augmented[row][row] , 10 )] for row in range(__snake_case ) ] def UpperCAmelCase_ ( __snake_case ) -> Callable[[int], int]: """simple docstring""" _lowercase =len(__snake_case ) _lowercase =[[0 for _ in range(__snake_case )] for _ in range(__snake_case )] _lowercase =[[0] for _ in range(__snake_case )] _lowercase =42 _lowercase =42 _lowercase =42 _lowercase =42 for x_val, y_val in enumerate(__snake_case ): for col in range(__snake_case ): _lowercase =(x_val + 1) ** (size - col - 1) _lowercase =y_val _lowercase =solve(__snake_case , __snake_case ) def interpolated_func(__snake_case ) -> int: return sum( round(coeffs[x_val][0] ) * (var ** (size - x_val - 1)) for x_val in range(__snake_case ) ) return interpolated_func def UpperCAmelCase_ ( __snake_case ) -> int: """simple docstring""" return ( 1 - variable + variable**2 - variable**3 + variable**4 - variable**5 + variable**6 - variable**7 + variable**8 - variable**9 + variable**10 ) def UpperCAmelCase_ ( __snake_case = question_function , __snake_case = 10 ) -> int: """simple docstring""" _lowercase =[func(__snake_case ) for x_val in range(1 , order + 1 )] _lowercase =[ interpolate(data_points[:max_coeff] ) for max_coeff in range(1 , order + 1 ) ] _lowercase =0 _lowercase =42 _lowercase =42 for poly in polynomials: _lowercase =1 while func(__snake_case ) == poly(__snake_case ): x_val += 1 ret += poly(__snake_case ) return ret if __name__ == "__main__": print(f'''{solution() = }''')
5
1
import pickle import shutil import tempfile import unittest from transformers import SPIECE_UNDERLINE, XLMRobertaTokenizer, XLMRobertaTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin UpperCAmelCase__ = get_tests_dir('''fixtures/test_sentencepiece.model''') @require_sentencepiece @require_tokenizers class lowerCamelCase__ ( lowerCAmelCase , unittest.TestCase): SCREAMING_SNAKE_CASE__ = XLMRobertaTokenizer SCREAMING_SNAKE_CASE__ = XLMRobertaTokenizerFast SCREAMING_SNAKE_CASE__ = True SCREAMING_SNAKE_CASE__ = True def __A (self ) -> List[Any]: super().setUp() # We have a SentencePiece fixture for testing _lowercase =XLMRobertaTokenizer(UpperCAmelCase , keep_accents=UpperCAmelCase ) tokenizer.save_pretrained(self.tmpdirname ) def __A (self ) -> Optional[Any]: _lowercase ='''<pad>''' _lowercase =1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(UpperCAmelCase ) , UpperCAmelCase ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(UpperCAmelCase ) , UpperCAmelCase ) def __A (self ) -> Tuple: _lowercase =list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '''<s>''' ) self.assertEqual(vocab_keys[1] , '''<pad>''' ) self.assertEqual(vocab_keys[-1] , '''<mask>''' ) self.assertEqual(len(UpperCAmelCase ) , 1_0_0_2 ) def __A (self ) -> int: self.assertEqual(self.get_tokenizer().vocab_size , 1_0_0_2 ) def __A (self ) -> Dict: _lowercase =XLMRobertaTokenizer(UpperCAmelCase , keep_accents=UpperCAmelCase ) _lowercase =tokenizer.tokenize('''This is a test''' ) self.assertListEqual(UpperCAmelCase , ['''▁This''', '''▁is''', '''▁a''', '''▁t''', '''est'''] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(UpperCAmelCase ) , [value + tokenizer.fairseq_offset for value in [2_8_5, 4_6, 1_0, 1_7_0, 3_8_2]] , ) _lowercase =tokenizer.tokenize('''I was born in 92000, and this is falsé.''' ) self.assertListEqual( UpperCAmelCase , [ SPIECE_UNDERLINE + '''I''', SPIECE_UNDERLINE + '''was''', SPIECE_UNDERLINE + '''b''', '''or''', '''n''', SPIECE_UNDERLINE + '''in''', SPIECE_UNDERLINE + '''''', '''9''', '''2''', '''0''', '''0''', '''0''', ''',''', SPIECE_UNDERLINE + '''and''', SPIECE_UNDERLINE + '''this''', SPIECE_UNDERLINE + '''is''', SPIECE_UNDERLINE + '''f''', '''al''', '''s''', '''é''', '''.''', ] , ) _lowercase =tokenizer.convert_tokens_to_ids(UpperCAmelCase ) self.assertListEqual( UpperCAmelCase , [ value + tokenizer.fairseq_offset for value in [8, 2_1, 8_4, 5_5, 2_4, 1_9, 7, 2, 6_0_2, 3_4_7, 3_4_7, 3_4_7, 3, 1_2, 6_6, 4_6, 7_2, 8_0, 6, 2, 4] # ^ unk: 2 + 1 = 3 unk: 2 + 1 = 3 ^ ] , ) _lowercase =tokenizer.convert_ids_to_tokens(UpperCAmelCase ) self.assertListEqual( UpperCAmelCase , [ SPIECE_UNDERLINE + '''I''', SPIECE_UNDERLINE + '''was''', SPIECE_UNDERLINE + '''b''', '''or''', '''n''', SPIECE_UNDERLINE + '''in''', SPIECE_UNDERLINE + '''''', '''<unk>''', '''2''', '''0''', '''0''', '''0''', ''',''', SPIECE_UNDERLINE + '''and''', SPIECE_UNDERLINE + '''this''', SPIECE_UNDERLINE + '''is''', SPIECE_UNDERLINE + '''f''', '''al''', '''s''', '''<unk>''', '''.''', ] , ) def __A (self ) -> Optional[Any]: if not self.test_slow_tokenizer: # as we don't have a slow version, we can't compare the outputs between slow and fast versions return _lowercase =(self.rust_tokenizer_class, '''hf-internal-testing/tiny-xlm-roberta''', {}) for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f"{tokenizer.__class__.__name__} ({pretrained_name})" ): _lowercase =self.rust_tokenizer_class.from_pretrained(UpperCAmelCase , **UpperCAmelCase ) _lowercase =self.tokenizer_class.from_pretrained(UpperCAmelCase , **UpperCAmelCase ) _lowercase =tempfile.mkdtemp() _lowercase =tokenizer_r.save_pretrained(UpperCAmelCase ) _lowercase =tokenizer_p.save_pretrained(UpperCAmelCase ) # Checks it save with the same files + the tokenizer.json file for the fast one self.assertTrue(any('''tokenizer.json''' in f for f in tokenizer_r_files ) ) _lowercase =tuple(f for f in tokenizer_r_files if '''tokenizer.json''' not in f ) self.assertSequenceEqual(UpperCAmelCase , UpperCAmelCase ) # Checks everything loads correctly in the same way _lowercase =tokenizer_r.from_pretrained(UpperCAmelCase ) _lowercase =tokenizer_p.from_pretrained(UpperCAmelCase ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(UpperCAmelCase , UpperCAmelCase ) ) # self.assertEqual(getattr(tokenizer_rp, key), getattr(tokenizer_pp, key)) # self.assertEqual(getattr(tokenizer_rp, key + "_id"), getattr(tokenizer_pp, key + "_id")) shutil.rmtree(UpperCAmelCase ) # Save tokenizer rust, legacy_format=True _lowercase =tempfile.mkdtemp() _lowercase =tokenizer_r.save_pretrained(UpperCAmelCase , legacy_format=UpperCAmelCase ) _lowercase =tokenizer_p.save_pretrained(UpperCAmelCase ) # Checks it save with the same files self.assertSequenceEqual(UpperCAmelCase , UpperCAmelCase ) # Checks everything loads correctly in the same way _lowercase =tokenizer_r.from_pretrained(UpperCAmelCase ) _lowercase =tokenizer_p.from_pretrained(UpperCAmelCase ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(UpperCAmelCase , UpperCAmelCase ) ) shutil.rmtree(UpperCAmelCase ) # Save tokenizer rust, legacy_format=False _lowercase =tempfile.mkdtemp() _lowercase =tokenizer_r.save_pretrained(UpperCAmelCase , legacy_format=UpperCAmelCase ) _lowercase =tokenizer_p.save_pretrained(UpperCAmelCase ) # Checks it saved the tokenizer.json file self.assertTrue(any('''tokenizer.json''' in f for f in tokenizer_r_files ) ) # Checks everything loads correctly in the same way _lowercase =tokenizer_r.from_pretrained(UpperCAmelCase ) _lowercase =tokenizer_p.from_pretrained(UpperCAmelCase ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(UpperCAmelCase , UpperCAmelCase ) ) shutil.rmtree(UpperCAmelCase ) @cached_property def __A (self ) -> List[str]: return XLMRobertaTokenizer.from_pretrained('''xlm-roberta-base''' ) def __A (self ) -> Dict: with tempfile.NamedTemporaryFile() as f: shutil.copyfile(UpperCAmelCase , f.name ) _lowercase =XLMRobertaTokenizer(f.name , keep_accents=UpperCAmelCase ) _lowercase =pickle.dumps(UpperCAmelCase ) pickle.loads(UpperCAmelCase ) def __A (self ) -> str: if not self.test_rust_tokenizer: return _lowercase =self.get_tokenizer() _lowercase =self.get_rust_tokenizer() _lowercase ='''I was born in 92000, and this is falsé.''' _lowercase =tokenizer.tokenize(UpperCAmelCase ) _lowercase =rust_tokenizer.tokenize(UpperCAmelCase ) self.assertListEqual(UpperCAmelCase , UpperCAmelCase ) _lowercase =tokenizer.encode(UpperCAmelCase , add_special_tokens=UpperCAmelCase ) _lowercase =rust_tokenizer.encode(UpperCAmelCase , add_special_tokens=UpperCAmelCase ) self.assertListEqual(UpperCAmelCase , UpperCAmelCase ) _lowercase =self.get_rust_tokenizer() _lowercase =tokenizer.encode(UpperCAmelCase ) _lowercase =rust_tokenizer.encode(UpperCAmelCase ) self.assertListEqual(UpperCAmelCase , UpperCAmelCase ) @slow def __A (self ) -> List[Any]: _lowercase ='''Hello World!''' _lowercase =[0, 3_5_3_7_8, 6_6_6_1, 3_8, 2] # xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.base') # xlmr.large has same tokenizer # xlmr.eval() # xlmr.encode(symbols) self.assertListEqual(UpperCAmelCase , self.big_tokenizer.encode(UpperCAmelCase ) ) @slow def __A (self ) -> Optional[int]: _lowercase =( '''This is a very long text with a lot of weird characters, such as: . , ~ ? ( ) " [ ] ! : - . Also we will''' ''' add words that should not exsist and be tokenized to <unk>, such as saoneuhaoesuth''' ) _lowercase =[ 0, 3_2_9_3, 8_3, 1_0, 4_5_5_2, 4_9_8_9, 7_9_8_6, 6_7_8, 1_0, 5_9_1_5, 1_1_1, 1_7_9_4_5_9, 1_2_4_8_5_0, 4, 6_0_4_4, 2_3_7, 1_2, 6, 5, 6, 4, 6_7_8_0, 7_0_5, 1_5, 1_3_8_8, 4_4, 3_7_8, 1_0_1_1_4, 7_1_1, 1_5_2, 2_0, 6, 5, 2_2_3_7_6, 6_4_2, 1_2_2_1, 1_5_1_9_0, 3_4_1_5_3, 4_5_0, 5_6_0_8, 9_5_9, 1_1_1_9, 5_7_7_0_2, 1_3_6, 1_8_6, 4_7, 1_0_9_8, 2_9_3_6_7, 4_7, # 4426, # What fairseq tokenizes from "<unk>": "_<" # 3678, # What fairseq tokenizes from "<unk>": "unk" # 2740, # What fairseq tokenizes from "<unk>": ">" 3, # What we tokenize from "<unk>": "<unk>" 6, # Residue from the tokenization: an extra sentencepiece underline 4, 6_0_4_4, 2_3_7, 6_2_8_4, 5_0_9_0_1, 5_2_8, 3_1, 9_0, 3_4, 9_2_7, 2, ] # xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.base') # xlmr.large has same tokenizer # xlmr.eval() # xlmr.encode(symbols) self.assertListEqual(UpperCAmelCase , self.big_tokenizer.encode(UpperCAmelCase ) ) @slow def __A (self ) -> Union[str, Any]: # fmt: off _lowercase ={'''input_ids''': [[0, 1_1_0_6_2, 8_2_7_7_2, 7, 1_5, 8_2_7_7_2, 5_3_8, 5_1_5_2_9, 2_3_7, 1_7_1_9_8, 1_2_9_0, 2_0_6, 9, 2_1_5_1_7_5, 1_3_1_4, 1_3_6, 1_7_1_9_8, 1_2_9_0, 2_0_6, 9, 5_6_3_5_9, 4_2, 1_2_2_0_0_9, 9, 1_6_4_6_6, 1_6, 8_7_3_4_4, 4_5_3_7, 9, 4_7_1_7, 7_8_3_8_1, 6, 1_5_9_9_5_8, 7, 1_5, 2_4_4_8_0, 6_1_8, 4, 5_2_7, 2_2_6_9_3, 5_4_2_8, 4, 2_7_7_7, 2_4_4_8_0, 9_8_7_4, 4, 4_3_5_2_3, 5_9_4, 4, 8_0_3, 1_8_3_9_2, 3_3_1_8_9, 1_8, 4, 4_3_5_2_3, 2_4_4_4_7, 1_2_3_9_9, 1_0_0, 2_4_9_5_5, 8_3_6_5_8, 9_6_2_6, 1_4_4_0_5_7, 1_5, 8_3_9, 2_2_3_3_5, 1_6, 1_3_6, 2_4_9_5_5, 8_3_6_5_8, 8_3_4_7_9, 1_5, 3_9_1_0_2, 7_2_4, 1_6, 6_7_8, 6_4_5, 2_7_8_9, 1_3_2_8, 4_5_8_9, 4_2, 1_2_2_0_0_9, 1_1_5_7_7_4, 2_3, 8_0_5, 1_3_2_8, 4_6_8_7_6, 7, 1_3_6, 5_3_8_9_4, 1_9_4_0, 4_2_2_2_7, 4_1_1_5_9, 1_7_7_2_1, 8_2_3, 4_2_5, 4, 2_7_5_1_2, 9_8_7_2_2, 2_0_6, 1_3_6, 5_5_3_1, 4_9_7_0, 9_1_9, 1_7_3_3_6, 5, 2], [0, 2_0_0_8_0, 6_1_8, 8_3, 8_2_7_7_5, 4_7, 4_7_9, 9, 1_5_1_7, 7_3, 5_3_8_9_4, 3_3_3, 8_0_5_8_1, 1_1_0_1_1_7, 1_8_8_1_1, 5_2_5_6, 1_2_9_5, 5_1, 1_5_2_5_2_6, 2_9_7, 7_9_8_6, 3_9_0, 1_2_4_4_1_6, 5_3_8, 3_5_4_3_1, 2_1_4, 9_8, 1_5_0_4_4, 2_5_7_3_7, 1_3_6, 7_1_0_8, 4_3_7_0_1, 2_3, 7_5_6, 1_3_5_3_5_5, 7, 5, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 5_8_1, 6_3_7_7_3, 1_1_9_4_5_5, 6, 1_4_7_7_9_7, 8_8_2_0_3, 7, 6_4_5, 7_0, 2_1, 3_2_8_5, 1_0_2_6_9, 5, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], '''attention_mask''': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=UpperCAmelCase , model_name='''xlm-roberta-base''' , revision='''d9d8a8ea5eb94b1c6654ae9249df7793cd2933d3''' , )
5
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available UpperCAmelCase__ = { '''configuration_xlm''': ['''XLM_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''XLMConfig''', '''XLMOnnxConfig'''], '''tokenization_xlm''': ['''XLMTokenizer'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ '''XLM_PRETRAINED_MODEL_ARCHIVE_LIST''', '''XLMForMultipleChoice''', '''XLMForQuestionAnswering''', '''XLMForQuestionAnsweringSimple''', '''XLMForSequenceClassification''', '''XLMForTokenClassification''', '''XLMModel''', '''XLMPreTrainedModel''', '''XLMWithLMHeadModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ '''TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFXLMForMultipleChoice''', '''TFXLMForQuestionAnsweringSimple''', '''TFXLMForSequenceClassification''', '''TFXLMForTokenClassification''', '''TFXLMMainLayer''', '''TFXLMModel''', '''TFXLMPreTrainedModel''', '''TFXLMWithLMHeadModel''', ] if TYPE_CHECKING: from .configuration_xlm import XLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XLMConfig, XLMOnnxConfig from .tokenization_xlm import XLMTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xlm import ( XLM_PRETRAINED_MODEL_ARCHIVE_LIST, XLMForMultipleChoice, XLMForQuestionAnswering, XLMForQuestionAnsweringSimple, XLMForSequenceClassification, XLMForTokenClassification, XLMModel, XLMPreTrainedModel, XLMWithLMHeadModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_xlm import ( TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFXLMForMultipleChoice, TFXLMForQuestionAnsweringSimple, TFXLMForSequenceClassification, TFXLMForTokenClassification, TFXLMMainLayer, TFXLMModel, TFXLMPreTrainedModel, TFXLMWithLMHeadModel, ) else: import sys UpperCAmelCase__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
5
1
from dataclasses import dataclass from typing import Optional import numpy as np import torch import torch.nn as nn from ..utils import BaseOutput, is_torch_version, randn_tensor from .attention_processor import SpatialNorm from .unet_ad_blocks import UNetMidBlockaD, get_down_block, get_up_block @dataclass class lowerCamelCase__ ( lowerCAmelCase): SCREAMING_SNAKE_CASE__ = 42 class lowerCamelCase__ ( nn.Module): def __init__(self , UpperCAmelCase=3 , UpperCAmelCase=3 , UpperCAmelCase=("DownEncoderBlock2D",) , UpperCAmelCase=(6_4,) , UpperCAmelCase=2 , UpperCAmelCase=3_2 , UpperCAmelCase="silu" , UpperCAmelCase=True , ) -> Any: super().__init__() _lowercase =layers_per_block _lowercase =torch.nn.Convad( UpperCAmelCase , block_out_channels[0] , kernel_size=3 , stride=1 , padding=1 , ) _lowercase =None _lowercase =nn.ModuleList([] ) # down _lowercase =block_out_channels[0] for i, down_block_type in enumerate(UpperCAmelCase ): _lowercase =output_channel _lowercase =block_out_channels[i] _lowercase =i == len(UpperCAmelCase ) - 1 _lowercase =get_down_block( UpperCAmelCase , num_layers=self.layers_per_block , in_channels=UpperCAmelCase , out_channels=UpperCAmelCase , add_downsample=not is_final_block , resnet_eps=1e-6 , downsample_padding=0 , resnet_act_fn=UpperCAmelCase , resnet_groups=UpperCAmelCase , attention_head_dim=UpperCAmelCase , temb_channels=UpperCAmelCase , ) self.down_blocks.append(UpperCAmelCase ) # mid _lowercase =UNetMidBlockaD( in_channels=block_out_channels[-1] , resnet_eps=1e-6 , resnet_act_fn=UpperCAmelCase , output_scale_factor=1 , resnet_time_scale_shift='''default''' , attention_head_dim=block_out_channels[-1] , resnet_groups=UpperCAmelCase , temb_channels=UpperCAmelCase , ) # out _lowercase =nn.GroupNorm(num_channels=block_out_channels[-1] , num_groups=UpperCAmelCase , eps=1e-6 ) _lowercase =nn.SiLU() _lowercase =2 * out_channels if double_z else out_channels _lowercase =nn.Convad(block_out_channels[-1] , UpperCAmelCase , 3 , padding=1 ) _lowercase =False def __A (self , UpperCAmelCase ) -> Any: _lowercase =x _lowercase =self.conv_in(UpperCAmelCase ) if self.training and self.gradient_checkpointing: def create_custom_forward(UpperCAmelCase ): def custom_forward(*UpperCAmelCase ): return module(*UpperCAmelCase ) return custom_forward # down if is_torch_version('''>=''' , '''1.11.0''' ): for down_block in self.down_blocks: _lowercase =torch.utils.checkpoint.checkpoint( create_custom_forward(UpperCAmelCase ) , UpperCAmelCase , use_reentrant=UpperCAmelCase ) # middle _lowercase =torch.utils.checkpoint.checkpoint( create_custom_forward(self.mid_block ) , UpperCAmelCase , use_reentrant=UpperCAmelCase ) else: for down_block in self.down_blocks: _lowercase =torch.utils.checkpoint.checkpoint(create_custom_forward(UpperCAmelCase ) , UpperCAmelCase ) # middle _lowercase =torch.utils.checkpoint.checkpoint(create_custom_forward(self.mid_block ) , UpperCAmelCase ) else: # down for down_block in self.down_blocks: _lowercase =down_block(UpperCAmelCase ) # middle _lowercase =self.mid_block(UpperCAmelCase ) # post-process _lowercase =self.conv_norm_out(UpperCAmelCase ) _lowercase =self.conv_act(UpperCAmelCase ) _lowercase =self.conv_out(UpperCAmelCase ) return sample class lowerCamelCase__ ( nn.Module): def __init__(self , UpperCAmelCase=3 , UpperCAmelCase=3 , UpperCAmelCase=("UpDecoderBlock2D",) , UpperCAmelCase=(6_4,) , UpperCAmelCase=2 , UpperCAmelCase=3_2 , UpperCAmelCase="silu" , UpperCAmelCase="group" , ) -> int: super().__init__() _lowercase =layers_per_block _lowercase =nn.Convad( UpperCAmelCase , block_out_channels[-1] , kernel_size=3 , stride=1 , padding=1 , ) _lowercase =None _lowercase =nn.ModuleList([] ) _lowercase =in_channels if norm_type == '''spatial''' else None # mid _lowercase =UNetMidBlockaD( in_channels=block_out_channels[-1] , resnet_eps=1e-6 , resnet_act_fn=UpperCAmelCase , output_scale_factor=1 , resnet_time_scale_shift='''default''' if norm_type == '''group''' else norm_type , attention_head_dim=block_out_channels[-1] , resnet_groups=UpperCAmelCase , temb_channels=UpperCAmelCase , ) # up _lowercase =list(reversed(UpperCAmelCase ) ) _lowercase =reversed_block_out_channels[0] for i, up_block_type in enumerate(UpperCAmelCase ): _lowercase =output_channel _lowercase =reversed_block_out_channels[i] _lowercase =i == len(UpperCAmelCase ) - 1 _lowercase =get_up_block( UpperCAmelCase , num_layers=self.layers_per_block + 1 , in_channels=UpperCAmelCase , out_channels=UpperCAmelCase , prev_output_channel=UpperCAmelCase , add_upsample=not is_final_block , resnet_eps=1e-6 , resnet_act_fn=UpperCAmelCase , resnet_groups=UpperCAmelCase , attention_head_dim=UpperCAmelCase , temb_channels=UpperCAmelCase , resnet_time_scale_shift=UpperCAmelCase , ) self.up_blocks.append(UpperCAmelCase ) _lowercase =output_channel # out if norm_type == "spatial": _lowercase =SpatialNorm(block_out_channels[0] , UpperCAmelCase ) else: _lowercase =nn.GroupNorm(num_channels=block_out_channels[0] , num_groups=UpperCAmelCase , eps=1e-6 ) _lowercase =nn.SiLU() _lowercase =nn.Convad(block_out_channels[0] , UpperCAmelCase , 3 , padding=1 ) _lowercase =False def __A (self , UpperCAmelCase , UpperCAmelCase=None ) -> Any: _lowercase =z _lowercase =self.conv_in(UpperCAmelCase ) _lowercase =next(iter(self.up_blocks.parameters() ) ).dtype if self.training and self.gradient_checkpointing: def create_custom_forward(UpperCAmelCase ): def custom_forward(*UpperCAmelCase ): return module(*UpperCAmelCase ) return custom_forward if is_torch_version('''>=''' , '''1.11.0''' ): # middle _lowercase =torch.utils.checkpoint.checkpoint( create_custom_forward(self.mid_block ) , UpperCAmelCase , UpperCAmelCase , use_reentrant=UpperCAmelCase ) _lowercase =sample.to(UpperCAmelCase ) # up for up_block in self.up_blocks: _lowercase =torch.utils.checkpoint.checkpoint( create_custom_forward(UpperCAmelCase ) , UpperCAmelCase , UpperCAmelCase , use_reentrant=UpperCAmelCase ) else: # middle _lowercase =torch.utils.checkpoint.checkpoint( create_custom_forward(self.mid_block ) , UpperCAmelCase , UpperCAmelCase ) _lowercase =sample.to(UpperCAmelCase ) # up for up_block in self.up_blocks: _lowercase =torch.utils.checkpoint.checkpoint(create_custom_forward(UpperCAmelCase ) , UpperCAmelCase , UpperCAmelCase ) else: # middle _lowercase =self.mid_block(UpperCAmelCase , UpperCAmelCase ) _lowercase =sample.to(UpperCAmelCase ) # up for up_block in self.up_blocks: _lowercase =up_block(UpperCAmelCase , UpperCAmelCase ) # post-process if latent_embeds is None: _lowercase =self.conv_norm_out(UpperCAmelCase ) else: _lowercase =self.conv_norm_out(UpperCAmelCase , UpperCAmelCase ) _lowercase =self.conv_act(UpperCAmelCase ) _lowercase =self.conv_out(UpperCAmelCase ) return sample class lowerCamelCase__ ( nn.Module): def __init__(self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase=None , UpperCAmelCase="random" , UpperCAmelCase=False , UpperCAmelCase=True ) -> Tuple: super().__init__() _lowercase =n_e _lowercase =vq_embed_dim _lowercase =beta _lowercase =legacy _lowercase =nn.Embedding(self.n_e , self.vq_embed_dim ) self.embedding.weight.data.uniform_(-1.0 / self.n_e , 1.0 / self.n_e ) _lowercase =remap if self.remap is not None: self.register_buffer('''used''' , torch.tensor(np.load(self.remap ) ) ) _lowercase =self.used.shape[0] _lowercase =unknown_index # "random" or "extra" or integer if self.unknown_index == "extra": _lowercase =self.re_embed _lowercase =self.re_embed + 1 print( f"Remapping {self.n_e} indices to {self.re_embed} indices. " f"Using {self.unknown_index} for unknown indices." ) else: _lowercase =n_e _lowercase =sane_index_shape def __A (self , UpperCAmelCase ) -> int: _lowercase =inds.shape assert len(UpperCAmelCase ) > 1 _lowercase =inds.reshape(ishape[0] , -1 ) _lowercase =self.used.to(UpperCAmelCase ) _lowercase =(inds[:, :, None] == used[None, None, ...]).long() _lowercase =match.argmax(-1 ) _lowercase =match.sum(2 ) < 1 if self.unknown_index == "random": _lowercase =torch.randint(0 , self.re_embed , size=new[unknown].shape ).to(device=new.device ) else: _lowercase =self.unknown_index return new.reshape(UpperCAmelCase ) def __A (self , UpperCAmelCase ) -> int: _lowercase =inds.shape assert len(UpperCAmelCase ) > 1 _lowercase =inds.reshape(ishape[0] , -1 ) _lowercase =self.used.to(UpperCAmelCase ) if self.re_embed > self.used.shape[0]: # extra token _lowercase =0 # simply set to zero _lowercase =torch.gather(used[None, :][inds.shape[0] * [0], :] , 1 , UpperCAmelCase ) return back.reshape(UpperCAmelCase ) def __A (self , UpperCAmelCase ) -> str: # reshape z -> (batch, height, width, channel) and flatten _lowercase =z.permute(0 , 2 , 3 , 1 ).contiguous() _lowercase =z.view(-1 , self.vq_embed_dim ) # distances from z to embeddings e_j (z - e)^2 = z^2 + e^2 - 2 e * z _lowercase =torch.argmin(torch.cdist(UpperCAmelCase , self.embedding.weight ) , dim=1 ) _lowercase =self.embedding(UpperCAmelCase ).view(z.shape ) _lowercase =None _lowercase =None # compute loss for embedding if not self.legacy: _lowercase =self.beta * torch.mean((z_q.detach() - z) ** 2 ) + torch.mean((z_q - z.detach()) ** 2 ) else: _lowercase =torch.mean((z_q.detach() - z) ** 2 ) + self.beta * torch.mean((z_q - z.detach()) ** 2 ) # preserve gradients _lowercase =z + (z_q - z).detach() # reshape back to match original input shape _lowercase =z_q.permute(0 , 3 , 1 , 2 ).contiguous() if self.remap is not None: _lowercase =min_encoding_indices.reshape(z.shape[0] , -1 ) # add batch axis _lowercase =self.remap_to_used(UpperCAmelCase ) _lowercase =min_encoding_indices.reshape(-1 , 1 ) # flatten if self.sane_index_shape: _lowercase =min_encoding_indices.reshape(z_q.shape[0] , z_q.shape[2] , z_q.shape[3] ) return z_q, loss, (perplexity, min_encodings, min_encoding_indices) def __A (self , UpperCAmelCase , UpperCAmelCase ) -> int: # shape specifying (batch, height, width, channel) if self.remap is not None: _lowercase =indices.reshape(shape[0] , -1 ) # add batch axis _lowercase =self.unmap_to_all(UpperCAmelCase ) _lowercase =indices.reshape(-1 ) # flatten again # get quantized latent vectors _lowercase =self.embedding(UpperCAmelCase ) if shape is not None: _lowercase =z_q.view(UpperCAmelCase ) # reshape back to match original input shape _lowercase =z_q.permute(0 , 3 , 1 , 2 ).contiguous() return z_q class lowerCamelCase__ ( lowerCAmelCase): def __init__(self , UpperCAmelCase , UpperCAmelCase=False ) -> Tuple: _lowercase =parameters _lowercase , _lowercase =torch.chunk(UpperCAmelCase , 2 , dim=1 ) _lowercase =torch.clamp(self.logvar , -30.0 , 20.0 ) _lowercase =deterministic _lowercase =torch.exp(0.5 * self.logvar ) _lowercase =torch.exp(self.logvar ) if self.deterministic: _lowercase =_lowercase =torch.zeros_like( self.mean , device=self.parameters.device , dtype=self.parameters.dtype ) def __A (self , UpperCAmelCase = None ) -> torch.FloatTensor: # make sure sample is on the same device as the parameters and has same dtype _lowercase =randn_tensor( self.mean.shape , generator=UpperCAmelCase , device=self.parameters.device , dtype=self.parameters.dtype ) _lowercase =self.mean + self.std * sample return x def __A (self , UpperCAmelCase=None ) -> Dict: if self.deterministic: return torch.Tensor([0.0] ) else: if other is None: return 0.5 * torch.sum(torch.pow(self.mean , 2 ) + self.var - 1.0 - self.logvar , dim=[1, 2, 3] ) else: return 0.5 * torch.sum( torch.pow(self.mean - other.mean , 2 ) / other.var + self.var / other.var - 1.0 - self.logvar + other.logvar , dim=[1, 2, 3] , ) def __A (self , UpperCAmelCase , UpperCAmelCase=[1, 2, 3] ) -> Dict: if self.deterministic: return torch.Tensor([0.0] ) _lowercase =np.log(2.0 * np.pi ) return 0.5 * torch.sum(logtwopi + self.logvar + torch.pow(sample - self.mean , 2 ) / self.var , dim=UpperCAmelCase ) def __A (self ) -> Optional[Any]: return self.mean
5
# Copyright 2023 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available UpperCAmelCase__ = { '''configuration_efficientnet''': [ '''EFFICIENTNET_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''EfficientNetConfig''', '''EfficientNetOnnxConfig''', ] } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = ['''EfficientNetImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ '''EFFICIENTNET_PRETRAINED_MODEL_ARCHIVE_LIST''', '''EfficientNetForImageClassification''', '''EfficientNetModel''', '''EfficientNetPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_efficientnet import ( EFFICIENTNET_PRETRAINED_CONFIG_ARCHIVE_MAP, EfficientNetConfig, EfficientNetOnnxConfig, ) try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_efficientnet import EfficientNetImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_efficientnet import ( EFFICIENTNET_PRETRAINED_MODEL_ARCHIVE_LIST, EfficientNetForImageClassification, EfficientNetModel, EfficientNetPreTrainedModel, ) else: import sys UpperCAmelCase__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure)
5
1
import os # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/check_doctest_list.py UpperCAmelCase__ = '''.''' if __name__ == "__main__": UpperCAmelCase__ = os.path.join(REPO_PATH, '''utils/documentation_tests.txt''') UpperCAmelCase__ = [] UpperCAmelCase__ = [] with open(doctest_file_path) as fp: for line in fp: UpperCAmelCase__ = line.strip() UpperCAmelCase__ = os.path.join(REPO_PATH, line) if not (os.path.isfile(path) or os.path.isdir(path)): non_existent_paths.append(line) all_paths.append(path) if len(non_existent_paths) > 0: UpperCAmelCase__ = '''\n'''.join(non_existent_paths) raise ValueError(f'''`utils/documentation_tests.txt` contains non-existent paths:\n{non_existent_paths}''') if all_paths != sorted(all_paths): raise ValueError('''Files in `utils/documentation_tests.txt` are not in alphabetical order.''')
5
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available UpperCAmelCase__ = { '''configuration_timesformer''': ['''TIMESFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''TimesformerConfig'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ '''TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TimesformerModel''', '''TimesformerForVideoClassification''', '''TimesformerPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_timesformer import TIMESFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TimesformerConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_timesformer import ( TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TimesformerForVideoClassification, TimesformerModel, TimesformerPreTrainedModel, ) else: import sys UpperCAmelCase__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
5
1
import tempfile import unittest from pathlib import Path from shutil import copyfile from transformers import MaMaaaTokenizer, is_torch_available from transformers.testing_utils import ( get_tests_dir, nested_simplify, require_sentencepiece, require_tokenizers, require_torch, slow, ) from transformers.utils import is_sentencepiece_available if is_sentencepiece_available(): from transformers.models.mam_aaa.tokenization_mam_aaa import VOCAB_FILES_NAMES, save_json from ...test_tokenization_common import TokenizerTesterMixin if is_sentencepiece_available(): UpperCAmelCase__ = get_tests_dir('''fixtures/test_sentencepiece.model''') if is_torch_available(): from transformers.models.mam_aaa.modeling_mam_aaa import shift_tokens_right UpperCAmelCase__ = 12_8022 UpperCAmelCase__ = 12_8028 @require_sentencepiece class lowerCamelCase__ ( lowerCAmelCase , unittest.TestCase): SCREAMING_SNAKE_CASE__ = MaMaaaTokenizer SCREAMING_SNAKE_CASE__ = False SCREAMING_SNAKE_CASE__ = False SCREAMING_SNAKE_CASE__ = True def __A (self ) -> Optional[Any]: super().setUp() _lowercase =['''</s>''', '''<unk>''', '''▁This''', '''▁is''', '''▁a''', '''▁t''', '''est''', '''\u0120''', '''<pad>'''] _lowercase =dict(zip(UpperCAmelCase , range(len(UpperCAmelCase ) ) ) ) _lowercase =Path(self.tmpdirname ) save_json(UpperCAmelCase , save_dir / VOCAB_FILES_NAMES['''vocab_file'''] ) if not (save_dir / VOCAB_FILES_NAMES["spm_file"]).exists(): copyfile(UpperCAmelCase , save_dir / VOCAB_FILES_NAMES['''spm_file'''] ) _lowercase =MaMaaaTokenizer.from_pretrained(self.tmpdirname ) tokenizer.save_pretrained(self.tmpdirname ) def __A (self , **UpperCAmelCase ) -> Union[str, Any]: return MaMaaaTokenizer.from_pretrained(self.tmpdirname , **UpperCAmelCase ) def __A (self , UpperCAmelCase ) -> List[Any]: return ( "This is a test", "This is a test", ) def __A (self ) -> List[Any]: _lowercase ='''</s>''' _lowercase =0 self.assertEqual(self.get_tokenizer()._convert_token_to_id(UpperCAmelCase ) , UpperCAmelCase ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(UpperCAmelCase ) , UpperCAmelCase ) def __A (self ) -> List[Any]: _lowercase =self.get_tokenizer() _lowercase =list(tokenizer.get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '''</s>''' ) self.assertEqual(vocab_keys[1] , '''<unk>''' ) self.assertEqual(vocab_keys[-1] , '''<s>''' ) self.assertEqual(len(UpperCAmelCase ) , tokenizer.vocab_size + len(tokenizer.get_added_vocab() ) ) @unittest.skip('''Skip this test while all models are still to be uploaded.''' ) def __A (self ) -> Optional[int]: pass def __A (self ) -> Tuple: _lowercase =self.get_tokenizer() _lowercase =tokenizer.tokenize('''This is a test''' ) self.assertListEqual(UpperCAmelCase , ['''▁This''', '''▁is''', '''▁a''', '''▁t''', '''est'''] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(UpperCAmelCase ) , [2, 3, 4, 5, 6] , ) _lowercase =tokenizer.convert_ids_to_tokens([2, 3, 4, 5, 6] ) self.assertListEqual(UpperCAmelCase , ['''▁This''', '''▁is''', '''▁a''', '''▁t''', '''est'''] ) _lowercase =tokenizer.convert_tokens_to_string(UpperCAmelCase ) self.assertEqual(UpperCAmelCase , '''This is a test''' ) @slow def __A (self ) -> Any: # fmt: off _lowercase ={'''input_ids''': [[1_2_8_0_2_2, 1_1_0_1_0_8, 3_9_7, 1_1, 3_8_2_7_2, 2_2_4_7, 1_2_4_8_1_1, 2_8_5, 1_8_1_0_5, 1_5_8_6, 2_0_7, 7, 3_9_5_3_4, 4_4_2_8, 3_9_7, 1_0_1_9, 1_8_1_0_5, 1_5_8_6, 2_0_7, 7, 4_1_3_3_7, 1_6_7_8_6, 2_4_1, 7, 2_0_2_1_4, 1_7, 1_2_5_6_9_0, 1_0_3_9_8, 7, 4_4_3_7_8, 5_8_0_6_9, 6_8_3_4_2, 7_7_9_8, 7_3_4_3, 1_1, 2_9_9, 3_3_3_1_0, 4, 1_5_8, 3_7_3_5_0, 9_4_0_7_7, 4_5_6_9, 2_9_9, 3_3_3_1_0, 9_0, 4, 5_2_8_4_0, 2_9_0, 4, 3_1_2_7_0, 1_1_2, 2_9_9, 6_8_2, 4, 5_2_8_4_0, 3_9_9_5_3, 1_4_0_7_9, 1_9_3, 5_2_5_1_9, 9_0_8_9_4, 1_7_8_9_4, 1_2_0_6_9_7, 1_1, 4_0_4_4_5, 5_5_1, 1_7, 1_0_1_9, 5_2_5_1_9, 9_0_8_9_4, 1_7_7_5_6, 9_6_3, 1_1, 4_0_4_4_5, 4_8_0, 1_7, 9_7_9_2, 1_1_2_0, 5_1_7_3, 1_3_9_3, 6_2_4_0, 1_6_7_8_6, 2_4_1, 1_2_0_9_9_6, 2_8, 1_2_4_5, 1_3_9_3, 1_1_8_2_4_0, 1_1_1_2_3, 1_0_1_9, 9_3_6_1_2, 2_6_9_1, 1_0_6_1_8, 9_8_0_5_8, 1_2_0_4_0_9, 1_9_2_8, 2_7_9, 4, 4_0_6_8_3, 3_6_7, 1_7_8, 2_0_7, 1_0_1_9, 1_0_3, 1_0_3_1_2_1, 5_0_6, 6_5_2_9_6, 5, 2], [1_2_8_0_2_2, 2_1_2_1_7, 3_6_7, 1_1_7, 1_2_5_4_5_0, 1_2_8, 7_1_9, 7, 7_3_0_8, 4_0, 9_3_6_1_2, 1_2_6_6_9, 1_1_1_6, 1_6_7_0_4, 7_1, 1_7_7_8_5, 3_6_9_9, 1_5_5_9_2, 3_5, 1_4_4, 9_5_8_4, 2_4_1, 1_1_9_4_3, 7_1_3, 9_5_0, 7_9_9, 2_2_4_7, 8_8_4_2_7, 1_5_0, 1_4_9, 1_1_8_8_1_3, 1_2_0_7_0_6, 1_0_1_9, 1_0_6_9_0_6, 8_1_5_1_8, 2_8, 1_2_2_4, 2_2_7_9_9, 3_9_7, 5, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1_2_8_0_2_2, 1_6_5_8, 1_2_3_3_1_1, 5_1_5_5, 5_5_7_8, 4_7_2_2, 2_7_9, 1_4_9_4_7, 2_3_6_6, 1_1_2_0, 1_1_9_7, 1_4, 1_3_4_8, 9_2_3_2, 5, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], '''attention_mask''': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=UpperCAmelCase , model_name='''facebook/m2m100_418M''' , revision='''c168bae485c864188cf9aa0e4108b0b6934dc91e''' , ) @require_torch @require_sentencepiece @require_tokenizers class lowerCamelCase__ ( unittest.TestCase): SCREAMING_SNAKE_CASE__ = '''facebook/m2m100_418M''' SCREAMING_SNAKE_CASE__ = [ '''In my opinion, there are two levels of response from the French government.''', '''NSA Affair Emphasizes Complete Lack of Debate on Intelligence''', ] SCREAMING_SNAKE_CASE__ = [ '''Selon moi, il y a deux niveaux de réponse de la part du gouvernement français.''', '''L\'affaire NSA souligne l\'absence totale de débat sur le renseignement''', ] # fmt: off SCREAMING_SNAKE_CASE__ = [EN_CODE, 593, 1949, 115781, 4, 71586, 4234, 60633, 126233, 432, 123808, 15592, 1197, 117132, 120618, 5, 2] @classmethod def __A (cls ) -> Union[str, Any]: _lowercase =MaMaaaTokenizer.from_pretrained( cls.checkpoint_name , src_lang='''en''' , tgt_lang='''fr''' ) _lowercase =1 return cls def __A (self ) -> Any: self.assertEqual(self.tokenizer.get_lang_id('''ar''' ) , 1_2_8_0_0_6 ) self.assertEqual(self.tokenizer.get_lang_id('''en''' ) , 1_2_8_0_2_2 ) self.assertEqual(self.tokenizer.get_lang_id('''ro''' ) , 1_2_8_0_7_6 ) self.assertEqual(self.tokenizer.get_lang_id('''mr''' ) , 1_2_8_0_6_3 ) def __A (self ) -> List[Any]: _lowercase =self.tokenizer.get_vocab() self.assertEqual(len(UpperCAmelCase ) , self.tokenizer.vocab_size ) self.assertEqual(vocab['''<unk>'''] , 3 ) self.assertIn(self.tokenizer.get_lang_token('''en''' ) , UpperCAmelCase ) def __A (self ) -> Dict: _lowercase ='''en''' _lowercase =self.tokenizer.batch_encode_plus(self.src_text ).input_ids[0] self.assertListEqual(self.expected_src_tokens , UpperCAmelCase ) def __A (self ) -> List[Any]: self.assertIn(UpperCAmelCase , self.tokenizer.all_special_ids ) # fmt: off _lowercase =[FR_CODE, 5_3_6_4, 8_2, 8_6_4_2, 4, 2_9_4, 4_7, 8, 1_4_0_2_8, 1_3_6, 3_2_8_6, 9_7_0_6, 6, 9_0_7_9_7, 6, 1_4_4_0_1_2, 1_6_2, 8_8_1_2_8, 3_0_0_6_1, 5, 2] # fmt: on _lowercase =self.tokenizer.decode(UpperCAmelCase , skip_special_tokens=UpperCAmelCase ) _lowercase =self.tokenizer.decode(generated_ids[1:] , skip_special_tokens=UpperCAmelCase ) self.assertEqual(UpperCAmelCase , UpperCAmelCase ) self.assertNotIn(self.tokenizer.eos_token , UpperCAmelCase ) def __A (self ) -> Union[str, Any]: _lowercase =tempfile.mkdtemp() _lowercase =self.tokenizer.lang_token_to_id self.tokenizer.save_pretrained(UpperCAmelCase ) _lowercase =MaMaaaTokenizer.from_pretrained(UpperCAmelCase ) self.assertDictEqual(new_tok.lang_token_to_id , UpperCAmelCase ) @require_torch def __A (self ) -> Optional[Any]: _lowercase ='''en''' _lowercase ='''fr''' _lowercase =self.tokenizer(self.src_text , text_target=self.tgt_text , padding=UpperCAmelCase , return_tensors='''pt''' ) _lowercase =shift_tokens_right( batch['''labels'''] , self.tokenizer.pad_token_id , self.tokenizer.eos_token_id ) for k in batch: _lowercase =batch[k].tolist() # batch = {k: v.tolist() for k,v in batch.items()} # fairseq batch: https://gist.github.com/sshleifer/cba08bc2109361a74ac3760a7e30e4f4 # batch.decoder_inputs_ids[0][0] == assert batch.input_ids[1][0] == EN_CODE assert batch.input_ids[1][-1] == 2 assert batch.labels[1][0] == FR_CODE assert batch.labels[1][-1] == 2 assert batch.decoder_input_ids[1][:2] == [2, FR_CODE] @require_torch def __A (self ) -> str: _lowercase ='''mr''' self.assertListEqual(self.tokenizer.prefix_tokens , [self.tokenizer.get_lang_id('''mr''' )] ) self.assertListEqual(self.tokenizer.suffix_tokens , [self.tokenizer.eos_token_id] ) _lowercase ='''zh''' self.assertListEqual(self.tokenizer.prefix_tokens , [self.tokenizer.get_lang_id('''zh''' )] ) self.assertListEqual(self.tokenizer.suffix_tokens , [self.tokenizer.eos_token_id] ) @require_torch def __A (self ) -> Any: _lowercase ='''mr''' self.tokenizer._switch_to_target_mode() self.assertListEqual(self.tokenizer.prefix_tokens , [self.tokenizer.get_lang_id('''mr''' )] ) self.assertListEqual(self.tokenizer.suffix_tokens , [self.tokenizer.eos_token_id] ) self.tokenizer._switch_to_input_mode() self.assertListEqual(self.tokenizer.prefix_tokens , [self.tokenizer.get_lang_id(self.tokenizer.src_lang )] ) _lowercase ='''zh''' self.tokenizer._switch_to_target_mode() self.assertListEqual(self.tokenizer.prefix_tokens , [self.tokenizer.get_lang_id('''zh''' )] ) self.assertListEqual(self.tokenizer.suffix_tokens , [self.tokenizer.eos_token_id] ) self.tokenizer._switch_to_input_mode() self.assertListEqual(self.tokenizer.prefix_tokens , [self.tokenizer.get_lang_id(self.tokenizer.src_lang )] ) @require_torch def __A (self ) -> Optional[int]: _lowercase =self.tokenizer._build_translation_inputs('''A test''' , return_tensors='''pt''' , src_lang='''en''' , tgt_lang='''ar''' ) self.assertEqual( nested_simplify(UpperCAmelCase ) , { # en_XX, A, test, EOS '''input_ids''': [[1_2_8_0_2_2, 5_8, 4_1_8_3, 2]], '''attention_mask''': [[1, 1, 1, 1]], # ar_AR '''forced_bos_token_id''': 1_2_8_0_0_6, } , )
5
def UpperCAmelCase_ ( __snake_case , __snake_case ) -> List[Any]: """simple docstring""" if b == 0: return 1 if (b % 2) == 0: return actual_power(__snake_case , int(b / 2 ) ) * actual_power(__snake_case , int(b / 2 ) ) else: return a * actual_power(__snake_case , int(b / 2 ) ) * actual_power(__snake_case , int(b / 2 ) ) def UpperCAmelCase_ ( __snake_case , __snake_case ) -> float: """simple docstring""" if b < 0: return 1 / actual_power(__snake_case , __snake_case ) return actual_power(__snake_case , __snake_case ) if __name__ == "__main__": print(power(-2, -3))
5
1
from functools import lru_cache @lru_cache def UpperCAmelCase_ ( __snake_case ) -> int: """simple docstring""" if num < 0: raise ValueError('''Number should not be negative.''' ) return 1 if num in (0, 1) else num * factorial(num - 1 ) if __name__ == "__main__": import doctest doctest.testmod()
5
from typing import Optional from torch import nn from .transformer_ad import TransformeraDModel, TransformeraDModelOutput class lowerCamelCase__ ( nn.Module): def __init__(self , UpperCAmelCase = 1_6 , UpperCAmelCase = 8_8 , UpperCAmelCase = None , UpperCAmelCase = 1 , UpperCAmelCase = 0.0 , UpperCAmelCase = 3_2 , UpperCAmelCase = None , UpperCAmelCase = False , UpperCAmelCase = None , UpperCAmelCase = None , UpperCAmelCase = "geglu" , UpperCAmelCase = None , ) -> Any: super().__init__() _lowercase =nn.ModuleList( [ TransformeraDModel( num_attention_heads=UpperCAmelCase , attention_head_dim=UpperCAmelCase , in_channels=UpperCAmelCase , num_layers=UpperCAmelCase , dropout=UpperCAmelCase , norm_num_groups=UpperCAmelCase , cross_attention_dim=UpperCAmelCase , attention_bias=UpperCAmelCase , sample_size=UpperCAmelCase , num_vector_embeds=UpperCAmelCase , activation_fn=UpperCAmelCase , num_embeds_ada_norm=UpperCAmelCase , ) for _ in range(2 ) ] ) # Variables that can be set by a pipeline: # The ratio of transformer1 to transformer2's output states to be combined during inference _lowercase =0.5 # The shape of `encoder_hidden_states` is expected to be # `(batch_size, condition_lengths[0]+condition_lengths[1], num_features)` _lowercase =[7_7, 2_5_7] # Which transformer to use to encode which condition. # E.g. `(1, 0)` means that we'll use `transformers[1](conditions[0])` and `transformers[0](conditions[1])` _lowercase =[1, 0] def __A (self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase=None , UpperCAmelCase=None , UpperCAmelCase=None , UpperCAmelCase = True , ) -> str: _lowercase =hidden_states _lowercase =[] _lowercase =0 # attention_mask is not used yet for i in range(2 ): # for each of the two transformers, pass the corresponding condition tokens _lowercase =encoder_hidden_states[:, tokens_start : tokens_start + self.condition_lengths[i]] _lowercase =self.transformer_index_for_condition[i] _lowercase =self.transformers[transformer_index]( UpperCAmelCase , encoder_hidden_states=UpperCAmelCase , timestep=UpperCAmelCase , cross_attention_kwargs=UpperCAmelCase , return_dict=UpperCAmelCase , )[0] encoded_states.append(encoded_state - input_states ) tokens_start += self.condition_lengths[i] _lowercase =encoded_states[0] * self.mix_ratio + encoded_states[1] * (1 - self.mix_ratio) _lowercase =output_states + input_states if not return_dict: return (output_states,) return TransformeraDModelOutput(sample=UpperCAmelCase )
5
1
def UpperCAmelCase_ ( __snake_case ) -> Union[str, Any]: """simple docstring""" _lowercase =1 _lowercase =2 while i * i <= n: _lowercase =0 while n % i == 0: n //= i multiplicity += 1 n_divisors *= multiplicity + 1 i += 1 if n > 1: n_divisors *= 2 return n_divisors def UpperCAmelCase_ ( ) -> Tuple: """simple docstring""" _lowercase =1 _lowercase =1 while True: i += 1 t_num += i if count_divisors(__snake_case ) > 500: break return t_num if __name__ == "__main__": print(solution())
5
import heapq as hq import math from collections.abc import Iterator class lowerCamelCase__ : def __init__(self , UpperCAmelCase ) -> Any: _lowercase =str(id_ ) _lowercase =None _lowercase =None _lowercase =[] _lowercase ={} # {vertex:distance} def __lt__(self , UpperCAmelCase ) -> List[str]: return self.key < other.key def __repr__(self ) -> str: return self.id def __A (self , UpperCAmelCase ) -> Dict: self.neighbors.append(UpperCAmelCase ) def __A (self , UpperCAmelCase , UpperCAmelCase ) -> Optional[Any]: _lowercase =weight def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case ) -> List[str]: """simple docstring""" graph[a - 1].add_neighbor(graph[b - 1] ) graph[b - 1].add_neighbor(graph[a - 1] ) # add the edges: graph[a - 1].add_edge(graph[b - 1] , __snake_case ) graph[b - 1].add_edge(graph[a - 1] , __snake_case ) def UpperCAmelCase_ ( __snake_case , __snake_case ) -> list: """simple docstring""" _lowercase =[] for u in graph: _lowercase =math.inf _lowercase =None _lowercase =0 _lowercase =graph[:] while q: _lowercase =min(__snake_case ) q.remove(__snake_case ) for v in u.neighbors: if (v in q) and (u.edges[v.id] < v.key): _lowercase =u _lowercase =u.edges[v.id] for i in range(1 , len(__snake_case ) ): a.append((int(graph[i].id ) + 1, int(graph[i].pi.id ) + 1) ) return a def UpperCAmelCase_ ( __snake_case , __snake_case ) -> Iterator[tuple]: """simple docstring""" for u in graph: _lowercase =math.inf _lowercase =None _lowercase =0 _lowercase =list(__snake_case ) hq.heapify(__snake_case ) while h: _lowercase =hq.heappop(__snake_case ) for v in u.neighbors: if (v in h) and (u.edges[v.id] < v.key): _lowercase =u _lowercase =u.edges[v.id] hq.heapify(__snake_case ) for i in range(1 , len(__snake_case ) ): yield (int(graph[i].id ) + 1, int(graph[i].pi.id ) + 1) def UpperCAmelCase_ ( ) -> None: """simple docstring""" if __name__ == "__main__": import doctest doctest.testmod()
5
1
# this script reports modified .py files under the desired list of top-level sub-dirs passed as a list of arguments, e.g.: # python ./utils/get_modified_files.py utils src tests examples # # it uses git to find the forking point and which files were modified - i.e. files not under git won't be considered # since the output of this script is fed into Makefile commands it doesn't print a newline after the results import re import subprocess import sys UpperCAmelCase__ = subprocess.check_output('''git merge-base main HEAD'''.split()).decode('''utf-8''') UpperCAmelCase__ = subprocess.check_output(f'''git diff --name-only {fork_point_sha}'''.split()).decode('''utf-8''').split() UpperCAmelCase__ = '''|'''.join(sys.argv[1:]) UpperCAmelCase__ = re.compile(Rf'''^({joined_dirs}).*?\.py$''') UpperCAmelCase__ = [x for x in modified_files if regex.match(x)] print(''' '''.join(relevant_modified_files), end='''''')
5
# flake8: noqa # Lint as: python3 UpperCAmelCase__ = [ '''VerificationMode''', '''Version''', '''disable_progress_bar''', '''enable_progress_bar''', '''is_progress_bar_enabled''', '''experimental''', ] from .info_utils import VerificationMode from .logging import disable_progress_bar, enable_progress_bar, is_progress_bar_enabled from .version import Version from .experimental import experimental
5
1
class lowerCamelCase__ : def __init__(self , UpperCAmelCase ) -> None: _lowercase =set_counts _lowercase =max(UpperCAmelCase ) _lowercase =len(UpperCAmelCase ) _lowercase =[1] * num_sets _lowercase =list(range(UpperCAmelCase ) ) def __A (self , UpperCAmelCase , UpperCAmelCase ) -> bool: _lowercase =self.get_parent(UpperCAmelCase ) _lowercase =self.get_parent(UpperCAmelCase ) if src_parent == dst_parent: return False if self.ranks[dst_parent] >= self.ranks[src_parent]: self.set_counts[dst_parent] += self.set_counts[src_parent] _lowercase =0 _lowercase =dst_parent if self.ranks[dst_parent] == self.ranks[src_parent]: self.ranks[dst_parent] += 1 _lowercase =self.set_counts[dst_parent] else: self.set_counts[src_parent] += self.set_counts[dst_parent] _lowercase =0 _lowercase =src_parent _lowercase =self.set_counts[src_parent] _lowercase =max(self.max_set , UpperCAmelCase ) return True def __A (self , UpperCAmelCase ) -> int: if self.parents[disj_set] == disj_set: return disj_set _lowercase =self.get_parent(self.parents[disj_set] ) return self.parents[disj_set]
5
import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = { '''microsoft/wavlm-base''': '''https://huggingface.co/microsoft/wavlm-base/resolve/main/config.json''', # See all WavLM models at https://huggingface.co/models?filter=wavlm } class lowerCamelCase__ ( lowerCAmelCase): SCREAMING_SNAKE_CASE__ = '''wavlm''' def __init__(self , UpperCAmelCase=3_2 , UpperCAmelCase=7_6_8 , UpperCAmelCase=1_2 , UpperCAmelCase=1_2 , UpperCAmelCase=3_0_7_2 , UpperCAmelCase="gelu" , UpperCAmelCase=0.1 , UpperCAmelCase=0.1 , UpperCAmelCase=0.1 , UpperCAmelCase=0.0 , UpperCAmelCase=0.1 , UpperCAmelCase=0.1 , UpperCAmelCase=0.02 , UpperCAmelCase=1e-5 , UpperCAmelCase="group" , UpperCAmelCase="gelu" , UpperCAmelCase=(5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2) , UpperCAmelCase=(5, 2, 2, 2, 2, 2, 2) , UpperCAmelCase=(1_0, 3, 3, 3, 3, 2, 2) , UpperCAmelCase=False , UpperCAmelCase=1_2_8 , UpperCAmelCase=1_6 , UpperCAmelCase=3_2_0 , UpperCAmelCase=8_0_0 , UpperCAmelCase=False , UpperCAmelCase=True , UpperCAmelCase=0.05 , UpperCAmelCase=1_0 , UpperCAmelCase=2 , UpperCAmelCase=0.0 , UpperCAmelCase=1_0 , UpperCAmelCase=3_2_0 , UpperCAmelCase=2 , UpperCAmelCase=0.1 , UpperCAmelCase=1_0_0 , UpperCAmelCase=2_5_6 , UpperCAmelCase=2_5_6 , UpperCAmelCase=0.1 , UpperCAmelCase="mean" , UpperCAmelCase=False , UpperCAmelCase=False , UpperCAmelCase=2_5_6 , UpperCAmelCase=(5_1_2, 5_1_2, 5_1_2, 5_1_2, 1_5_0_0) , UpperCAmelCase=(5, 3, 3, 1, 1) , UpperCAmelCase=(1, 2, 3, 1, 1) , UpperCAmelCase=5_1_2 , UpperCAmelCase=8_0 , UpperCAmelCase=0 , UpperCAmelCase=1 , UpperCAmelCase=2 , UpperCAmelCase=False , UpperCAmelCase=3 , UpperCAmelCase=2 , UpperCAmelCase=3 , UpperCAmelCase=None , **UpperCAmelCase , ) -> Optional[Any]: super().__init__(**UpperCAmelCase , pad_token_id=UpperCAmelCase , bos_token_id=UpperCAmelCase , eos_token_id=UpperCAmelCase ) _lowercase =hidden_size _lowercase =feat_extract_norm _lowercase =feat_extract_activation _lowercase =list(UpperCAmelCase ) _lowercase =list(UpperCAmelCase ) _lowercase =list(UpperCAmelCase ) _lowercase =conv_bias _lowercase =num_buckets _lowercase =max_bucket_distance _lowercase =num_conv_pos_embeddings _lowercase =num_conv_pos_embedding_groups _lowercase =len(self.conv_dim ) _lowercase =num_hidden_layers _lowercase =intermediate_size _lowercase =hidden_act _lowercase =num_attention_heads _lowercase =hidden_dropout _lowercase =attention_dropout _lowercase =activation_dropout _lowercase =feat_proj_dropout _lowercase =final_dropout _lowercase =layerdrop _lowercase =layer_norm_eps _lowercase =initializer_range _lowercase =num_ctc_classes _lowercase =vocab_size _lowercase =do_stable_layer_norm _lowercase =use_weighted_layer_sum _lowercase =classifier_proj_size if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( '''Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` ==''' ''' `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) =''' f" {len(self.conv_dim )}`, `len(config.conv_stride) = {len(self.conv_stride )}`," f" `len(config.conv_kernel) = {len(self.conv_kernel )}`." ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 _lowercase =apply_spec_augment _lowercase =mask_time_prob _lowercase =mask_time_length _lowercase =mask_time_min_masks _lowercase =mask_feature_prob _lowercase =mask_feature_length # parameters for pretraining with codevector quantized representations _lowercase =num_codevectors_per_group _lowercase =num_codevector_groups _lowercase =contrastive_logits_temperature _lowercase =num_negatives _lowercase =codevector_dim _lowercase =proj_codevector_dim _lowercase =diversity_loss_weight # ctc loss _lowercase =ctc_loss_reduction _lowercase =ctc_zero_infinity # adapter _lowercase =add_adapter _lowercase =adapter_kernel_size _lowercase =adapter_stride _lowercase =num_adapter_layers _lowercase =output_hidden_size or hidden_size # SequenceClassification-specific parameter. Feel free to ignore for other classes. _lowercase =classifier_proj_size # XVector-specific parameters. Feel free to ignore for other classes. _lowercase =list(UpperCAmelCase ) _lowercase =list(UpperCAmelCase ) _lowercase =list(UpperCAmelCase ) _lowercase =xvector_output_dim @property def __A (self ) -> int: return functools.reduce(operator.mul , self.conv_stride , 1 )
5
1
import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_barthez import BarthezTokenizer else: UpperCAmelCase__ = None UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = {'''vocab_file''': '''sentencepiece.bpe.model''', '''tokenizer_file''': '''tokenizer.json'''} UpperCAmelCase__ = { '''vocab_file''': { '''moussaKam/mbarthez''': '''https://huggingface.co/moussaKam/mbarthez/resolve/main/sentencepiece.bpe.model''', '''moussaKam/barthez''': '''https://huggingface.co/moussaKam/barthez/resolve/main/sentencepiece.bpe.model''', '''moussaKam/barthez-orangesum-title''': ( '''https://huggingface.co/moussaKam/barthez-orangesum-title/resolve/main/sentencepiece.bpe.model''' ), }, '''tokenizer_file''': { '''moussaKam/mbarthez''': '''https://huggingface.co/moussaKam/mbarthez/resolve/main/tokenizer.json''', '''moussaKam/barthez''': '''https://huggingface.co/moussaKam/barthez/resolve/main/tokenizer.json''', '''moussaKam/barthez-orangesum-title''': ( '''https://huggingface.co/moussaKam/barthez-orangesum-title/resolve/main/tokenizer.json''' ), }, } UpperCAmelCase__ = { '''moussaKam/mbarthez''': 1024, '''moussaKam/barthez''': 1024, '''moussaKam/barthez-orangesum-title''': 1024, } UpperCAmelCase__ = '''▁''' class lowerCamelCase__ ( lowerCAmelCase): SCREAMING_SNAKE_CASE__ = VOCAB_FILES_NAMES SCREAMING_SNAKE_CASE__ = PRETRAINED_VOCAB_FILES_MAP SCREAMING_SNAKE_CASE__ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES SCREAMING_SNAKE_CASE__ = ['''input_ids''', '''attention_mask'''] SCREAMING_SNAKE_CASE__ = BarthezTokenizer def __init__(self , UpperCAmelCase=None , UpperCAmelCase=None , UpperCAmelCase="<s>" , UpperCAmelCase="</s>" , UpperCAmelCase="</s>" , UpperCAmelCase="<s>" , UpperCAmelCase="<unk>" , UpperCAmelCase="<pad>" , UpperCAmelCase="<mask>" , **UpperCAmelCase , ) -> List[str]: # Mask token behave like a normal word, i.e. include the space before it _lowercase =AddedToken(UpperCAmelCase , lstrip=UpperCAmelCase , rstrip=UpperCAmelCase ) if isinstance(UpperCAmelCase , UpperCAmelCase ) else mask_token super().__init__( UpperCAmelCase , tokenizer_file=UpperCAmelCase , bos_token=UpperCAmelCase , eos_token=UpperCAmelCase , unk_token=UpperCAmelCase , sep_token=UpperCAmelCase , cls_token=UpperCAmelCase , pad_token=UpperCAmelCase , mask_token=UpperCAmelCase , **UpperCAmelCase , ) _lowercase =vocab_file _lowercase =False if not self.vocab_file else True def __A (self , UpperCAmelCase , UpperCAmelCase = None ) -> List[int]: if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] _lowercase =[self.cls_token_id] _lowercase =[self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def __A (self , UpperCAmelCase , UpperCAmelCase = None ) -> List[int]: _lowercase =[self.sep_token_id] _lowercase =[self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def __A (self , UpperCAmelCase , UpperCAmelCase = None ) -> Tuple[str]: if not self.can_save_slow_tokenizer: raise ValueError( '''Your fast tokenizer does not have the necessary information to save the vocabulary for a slow ''' '''tokenizer.''' ) if not os.path.isdir(UpperCAmelCase ): logger.error(f"Vocabulary path ({save_directory}) should be a directory" ) return _lowercase =os.path.join( UpperCAmelCase , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(UpperCAmelCase ): copyfile(self.vocab_file , UpperCAmelCase ) return (out_vocab_file,)
5
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): def __A (self ) -> List[Any]: # clean up the VRAM after each test super().tearDown() gc.collect() def __A (self ) -> Optional[Any]: _lowercase =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/sd2-inpaint/init_image.png''' ) _lowercase =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png''' ) _lowercase ='''xvjiarui/stable-diffusion-2-inpainting''' _lowercase , _lowercase =FlaxStableDiffusionInpaintPipeline.from_pretrained(UpperCAmelCase , safety_checker=UpperCAmelCase ) _lowercase ='''Face of a yellow cat, high resolution, sitting on a park bench''' _lowercase =jax.random.PRNGKey(0 ) _lowercase =5_0 _lowercase =jax.device_count() _lowercase =num_samples * [prompt] _lowercase =num_samples * [init_image] _lowercase =num_samples * [mask_image] _lowercase , _lowercase , _lowercase =pipeline.prepare_inputs(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) # shard inputs and rng _lowercase =replicate(UpperCAmelCase ) _lowercase =jax.random.split(UpperCAmelCase , jax.device_count() ) _lowercase =shard(UpperCAmelCase ) _lowercase =shard(UpperCAmelCase ) _lowercase =shard(UpperCAmelCase ) _lowercase =pipeline( UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , jit=UpperCAmelCase ) _lowercase =output.images.reshape(UpperCAmelCase , 5_1_2 , 5_1_2 , 3 ) _lowercase =images[0, 2_5_3:2_5_6, 2_5_3:2_5_6, -1] _lowercase =jnp.asarray(jax.device_get(image_slice.flatten() ) ) _lowercase =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
5
1
import math def UpperCAmelCase_ ( __snake_case , __snake_case ) -> float: """simple docstring""" if initial_intensity < 0: raise ValueError('''The value of intensity cannot be negative''' ) # handling of negative values of initial intensity if angle < 0 or angle > 360: raise ValueError('''In Malus Law, the angle is in the range 0-360 degrees''' ) # handling of values out of allowed range return initial_intensity * (math.cos(math.radians(__snake_case ) ) ** 2) if __name__ == "__main__": import doctest doctest.testmod(name='''malus_law''')
5
import comet # From: unbabel-comet import torch import datasets UpperCAmelCase__ = datasets.logging.get_logger(__name__) UpperCAmelCase__ = '''\ @inproceedings{rei-EtAl:2020:WMT, author = {Rei, Ricardo and Stewart, Craig and Farinha, Ana C and Lavie, Alon}, title = {Unbabel\'s Participation in the WMT20 Metrics Shared Task}, booktitle = {Proceedings of the Fifth Conference on Machine Translation}, month = {November}, year = {2020}, address = {Online}, publisher = {Association for Computational Linguistics}, pages = {909--918}, } @inproceedings{rei-etal-2020-comet, title = "{COMET}: A Neural Framework for {MT} Evaluation", author = "Rei, Ricardo and Stewart, Craig and Farinha, Ana C and Lavie, Alon", booktitle = "Proceedings of the 2020 Conference on Empirical Methods in Natural Language Processing (EMNLP)", month = nov, year = "2020", address = "Online", publisher = "Association for Computational Linguistics", url = "https://www.aclweb.org/anthology/2020.emnlp-main.213", pages = "2685--2702", } ''' UpperCAmelCase__ = '''\ Crosslingual Optimized Metric for Evaluation of Translation (COMET) is an open-source framework used to train Machine Translation metrics that achieve high levels of correlation with different types of human judgments (HTER, DA\'s or MQM). With the release of the framework the authors also released fully trained models that were used to compete in the WMT20 Metrics Shared Task achieving SOTA in that years competition. See the [README.md] file at https://unbabel.github.io/COMET/html/models.html for more information. ''' UpperCAmelCase__ = ''' COMET score. Args: `sources` (list of str): Source sentences `predictions` (list of str): candidate translations `references` (list of str): reference translations `cuda` (bool): If set to True, runs COMET using GPU `show_progress` (bool): Shows progress `model`: COMET model to be used. Will default to `wmt-large-da-estimator-1719` if None. Returns: `samples`: List of dictionaries with `src`, `mt`, `ref` and `score`. `scores`: List of scores. Examples: >>> comet_metric = datasets.load_metric(\'comet\') >>> # comet_metric = load_metric(\'comet\', \'wmt20-comet-da\') # you can also choose which model to use >>> source = ["Dem Feuer konnte Einhalt geboten werden", "Schulen und Kindergärten wurden eröffnet."] >>> hypothesis = ["The fire could be stopped", "Schools and kindergartens were open"] >>> reference = ["They were able to control the fire.", "Schools and kindergartens opened"] >>> results = comet_metric.compute(predictions=hypothesis, references=reference, sources=source) >>> print([round(v, 2) for v in results["scores"]]) [0.19, 0.92] ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION) class lowerCamelCase__ ( datasets.Metric): def __A (self ) -> Optional[int]: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , homepage='''https://unbabel.github.io/COMET/html/index.html''' , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''sources''': datasets.Value('''string''' , id='''sequence''' ), '''predictions''': datasets.Value('''string''' , id='''sequence''' ), '''references''': datasets.Value('''string''' , id='''sequence''' ), } ) , codebase_urls=['''https://github.com/Unbabel/COMET'''] , reference_urls=[ '''https://github.com/Unbabel/COMET''', '''https://www.aclweb.org/anthology/2020.emnlp-main.213/''', '''http://www.statmt.org/wmt20/pdf/2020.wmt-1.101.pdf6''', ] , ) def __A (self , UpperCAmelCase ) -> Dict: if self.config_name == "default": _lowercase =comet.load_from_checkpoint(comet.download_model('''wmt20-comet-da''' ) ) else: _lowercase =comet.load_from_checkpoint(comet.download_model(self.config_name ) ) def __A (self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase=None , UpperCAmelCase=False ) -> int: if gpus is None: _lowercase =1 if torch.cuda.is_available() else 0 _lowercase ={'''src''': sources, '''mt''': predictions, '''ref''': references} _lowercase =[dict(zip(UpperCAmelCase , UpperCAmelCase ) ) for t in zip(*data.values() )] _lowercase , _lowercase =self.scorer.predict(UpperCAmelCase , gpus=UpperCAmelCase , progress_bar=UpperCAmelCase ) return {"mean_score": mean_score, "scores": scores}
5
1
from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = { '''google/vit-base-patch16-224''': '''https://huggingface.co/vit-base-patch16-224/resolve/main/config.json''', # See all ViT models at https://huggingface.co/models?filter=vit } class lowerCamelCase__ ( lowerCAmelCase): SCREAMING_SNAKE_CASE__ = '''vit''' def __init__(self , UpperCAmelCase=7_6_8 , UpperCAmelCase=1_2 , UpperCAmelCase=1_2 , UpperCAmelCase=3_0_7_2 , UpperCAmelCase="gelu" , UpperCAmelCase=0.0 , UpperCAmelCase=0.0 , UpperCAmelCase=0.02 , UpperCAmelCase=1e-12 , UpperCAmelCase=2_2_4 , UpperCAmelCase=1_6 , UpperCAmelCase=3 , UpperCAmelCase=True , UpperCAmelCase=1_6 , **UpperCAmelCase , ) -> List[str]: super().__init__(**UpperCAmelCase ) _lowercase =hidden_size _lowercase =num_hidden_layers _lowercase =num_attention_heads _lowercase =intermediate_size _lowercase =hidden_act _lowercase =hidden_dropout_prob _lowercase =attention_probs_dropout_prob _lowercase =initializer_range _lowercase =layer_norm_eps _lowercase =image_size _lowercase =patch_size _lowercase =num_channels _lowercase =qkv_bias _lowercase =encoder_stride class lowerCamelCase__ ( lowerCAmelCase): SCREAMING_SNAKE_CASE__ = version.parse('''1.11''') @property def __A (self ) -> Mapping[str, Mapping[int, str]]: return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ] ) @property def __A (self ) -> float: return 1e-4
5
# DISCLAIMER: This file is strongly influenced by https://github.com/yang-song/score_sde_pytorch import math from typing import Union import torch from ..configuration_utils import ConfigMixin, register_to_config from ..utils import randn_tensor from .scheduling_utils import SchedulerMixin class lowerCamelCase__ ( lowerCAmelCase , lowerCAmelCase): SCREAMING_SNAKE_CASE__ = 1 @register_to_config def __init__(self , UpperCAmelCase=2_0_0_0 , UpperCAmelCase=0.1 , UpperCAmelCase=2_0 , UpperCAmelCase=1e-3 ) -> List[str]: _lowercase =None _lowercase =None _lowercase =None def __A (self , UpperCAmelCase , UpperCAmelCase = None ) -> str: _lowercase =torch.linspace(1 , self.config.sampling_eps , UpperCAmelCase , device=UpperCAmelCase ) def __A (self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase=None ) -> Optional[int]: if self.timesteps is None: raise ValueError( '''`self.timesteps` is not set, you need to run \'set_timesteps\' after creating the scheduler''' ) # TODO(Patrick) better comments + non-PyTorch # postprocess model score _lowercase =( -0.25 * t**2 * (self.config.beta_max - self.config.beta_min) - 0.5 * t * self.config.beta_min ) _lowercase =torch.sqrt(1.0 - torch.exp(2.0 * log_mean_coeff ) ) _lowercase =std.flatten() while len(std.shape ) < len(score.shape ): _lowercase =std.unsqueeze(-1 ) _lowercase =-score / std # compute _lowercase =-1.0 / len(self.timesteps ) _lowercase =self.config.beta_min + t * (self.config.beta_max - self.config.beta_min) _lowercase =beta_t.flatten() while len(beta_t.shape ) < len(x.shape ): _lowercase =beta_t.unsqueeze(-1 ) _lowercase =-0.5 * beta_t * x _lowercase =torch.sqrt(UpperCAmelCase ) _lowercase =drift - diffusion**2 * score _lowercase =x + drift * dt # add noise _lowercase =randn_tensor(x.shape , layout=x.layout , generator=UpperCAmelCase , device=x.device , dtype=x.dtype ) _lowercase =x_mean + diffusion * math.sqrt(-dt ) * noise return x, x_mean def __len__(self ) -> str: return self.config.num_train_timesteps
5
1
from __future__ import annotations import numpy as np from numpy import floataa from numpy.typing import NDArray def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case , ) -> list[float]: """simple docstring""" _lowercase , _lowercase =coefficient_matrix.shape _lowercase , _lowercase =constant_matrix.shape if rowsa != colsa: _lowercase =F"Coefficient matrix dimensions must be nxn but received {rowsa}x{colsa}" raise ValueError(__snake_case ) if colsa != 1: _lowercase =F"Constant matrix must be nx1 but received {rowsa}x{colsa}" raise ValueError(__snake_case ) if rowsa != rowsa: _lowercase =( '''Coefficient and constant matrices dimensions must be nxn and nx1 but ''' F"received {rowsa}x{colsa} and {rowsa}x{colsa}" ) raise ValueError(__snake_case ) if len(__snake_case ) != rowsa: _lowercase =( '''Number of initial values must be equal to number of rows in coefficient ''' F"matrix but received {len(__snake_case )} and {rowsa}" ) raise ValueError(__snake_case ) if iterations <= 0: raise ValueError('''Iterations must be at least 1''' ) _lowercase =np.concatenate( (coefficient_matrix, constant_matrix) , axis=1 ) _lowercase , _lowercase =table.shape strictly_diagonally_dominant(__snake_case ) # Iterates the whole matrix for given number of times for _ in range(__snake_case ): _lowercase =[] for row in range(__snake_case ): _lowercase =0 for col in range(__snake_case ): if col == row: _lowercase =table[row][col] elif col == cols - 1: _lowercase =table[row][col] else: temp += (-1) * table[row][col] * init_val[col] _lowercase =(temp + val) / denom new_val.append(__snake_case ) _lowercase =new_val return [float(__snake_case ) for i in new_val] def UpperCAmelCase_ ( __snake_case ) -> bool: """simple docstring""" _lowercase , _lowercase =table.shape _lowercase =True for i in range(0 , __snake_case ): _lowercase =0 for j in range(0 , cols - 1 ): if i == j: continue else: total += table[i][j] if table[i][i] <= total: raise ValueError('''Coefficient matrix is not strictly diagonally dominant''' ) return is_diagonally_dominant # Test Cases if __name__ == "__main__": import doctest doctest.testmod()
5
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( MobileViTConfig, MobileViTForImageClassification, MobileViTForSemanticSegmentation, MobileViTImageProcessor, ) from transformers.utils import logging logging.set_verbosity_info() UpperCAmelCase__ = logging.get_logger(__name__) def UpperCAmelCase_ ( __snake_case ) -> Optional[Any]: """simple docstring""" _lowercase =MobileViTConfig() # size of the architecture if "mobilevit_s" in mobilevit_name: _lowercase =[144, 192, 240] _lowercase =[16, 32, 64, 96, 128, 160, 640] elif "mobilevit_xs" in mobilevit_name: _lowercase =[96, 120, 144] _lowercase =[16, 32, 48, 64, 80, 96, 384] elif "mobilevit_xxs" in mobilevit_name: _lowercase =[64, 80, 96] _lowercase =[16, 16, 24, 48, 64, 80, 320] _lowercase =0.05 _lowercase =2.0 if mobilevit_name.startswith('''deeplabv3_''' ): _lowercase =512 _lowercase =16 _lowercase =21 _lowercase ='''pascal-voc-id2label.json''' else: _lowercase =1000 _lowercase ='''imagenet-1k-id2label.json''' _lowercase ='''huggingface/label-files''' _lowercase =json.load(open(hf_hub_download(__snake_case , __snake_case , repo_type='''dataset''' ) , '''r''' ) ) _lowercase ={int(__snake_case ): v for k, v in idalabel.items()} _lowercase =idalabel _lowercase ={v: k for k, v in idalabel.items()} return config def UpperCAmelCase_ ( __snake_case , __snake_case=False ) -> Tuple: """simple docstring""" for i in range(1 , 6 ): if F"layer_{i}." in name: _lowercase =name.replace(F"layer_{i}." , F"encoder.layer.{i - 1}." ) if "conv_1." in name: _lowercase =name.replace('''conv_1.''' , '''conv_stem.''' ) if ".block." in name: _lowercase =name.replace('''.block.''' , '''.''' ) if "exp_1x1" in name: _lowercase =name.replace('''exp_1x1''' , '''expand_1x1''' ) if "red_1x1" in name: _lowercase =name.replace('''red_1x1''' , '''reduce_1x1''' ) if ".local_rep.conv_3x3." in name: _lowercase =name.replace('''.local_rep.conv_3x3.''' , '''.conv_kxk.''' ) if ".local_rep.conv_1x1." in name: _lowercase =name.replace('''.local_rep.conv_1x1.''' , '''.conv_1x1.''' ) if ".norm." in name: _lowercase =name.replace('''.norm.''' , '''.normalization.''' ) if ".conv." in name: _lowercase =name.replace('''.conv.''' , '''.convolution.''' ) if ".conv_proj." in name: _lowercase =name.replace('''.conv_proj.''' , '''.conv_projection.''' ) for i in range(0 , 2 ): for j in range(0 , 4 ): if F".{i}.{j}." in name: _lowercase =name.replace(F".{i}.{j}." , F".{i}.layer.{j}." ) for i in range(2 , 6 ): for j in range(0 , 4 ): if F".{i}.{j}." in name: _lowercase =name.replace(F".{i}.{j}." , F".{i}." ) if "expand_1x1" in name: _lowercase =name.replace('''expand_1x1''' , '''downsampling_layer.expand_1x1''' ) if "conv_3x3" in name: _lowercase =name.replace('''conv_3x3''' , '''downsampling_layer.conv_3x3''' ) if "reduce_1x1" in name: _lowercase =name.replace('''reduce_1x1''' , '''downsampling_layer.reduce_1x1''' ) for i in range(2 , 5 ): if F".global_rep.{i}.weight" in name: _lowercase =name.replace(F".global_rep.{i}.weight" , '''.layernorm.weight''' ) if F".global_rep.{i}.bias" in name: _lowercase =name.replace(F".global_rep.{i}.bias" , '''.layernorm.bias''' ) if ".global_rep." in name: _lowercase =name.replace('''.global_rep.''' , '''.transformer.''' ) if ".pre_norm_mha.0." in name: _lowercase =name.replace('''.pre_norm_mha.0.''' , '''.layernorm_before.''' ) if ".pre_norm_mha.1.out_proj." in name: _lowercase =name.replace('''.pre_norm_mha.1.out_proj.''' , '''.attention.output.dense.''' ) if ".pre_norm_ffn.0." in name: _lowercase =name.replace('''.pre_norm_ffn.0.''' , '''.layernorm_after.''' ) if ".pre_norm_ffn.1." in name: _lowercase =name.replace('''.pre_norm_ffn.1.''' , '''.intermediate.dense.''' ) if ".pre_norm_ffn.4." in name: _lowercase =name.replace('''.pre_norm_ffn.4.''' , '''.output.dense.''' ) if ".transformer." in name: _lowercase =name.replace('''.transformer.''' , '''.transformer.layer.''' ) if ".aspp_layer." in name: _lowercase =name.replace('''.aspp_layer.''' , '''.''' ) if ".aspp_pool." in name: _lowercase =name.replace('''.aspp_pool.''' , '''.''' ) if "seg_head." in name: _lowercase =name.replace('''seg_head.''' , '''segmentation_head.''' ) if "segmentation_head.classifier.classifier." in name: _lowercase =name.replace('''segmentation_head.classifier.classifier.''' , '''segmentation_head.classifier.''' ) if "classifier.fc." in name: _lowercase =name.replace('''classifier.fc.''' , '''classifier.''' ) elif (not base_model) and ("segmentation_head." not in name): _lowercase ='''mobilevit.''' + name return name def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case=False ) -> Optional[Any]: """simple docstring""" if base_model: _lowercase ='''''' else: _lowercase ='''mobilevit.''' for key in orig_state_dict.copy().keys(): _lowercase =orig_state_dict.pop(__snake_case ) if key[:8] == "encoder.": _lowercase =key[8:] if "qkv" in key: _lowercase =key.split('''.''' ) _lowercase =int(key_split[0][6:] ) - 1 _lowercase =int(key_split[3] ) _lowercase =model.get_submodule(F"{model_prefix}encoder.layer.{layer_num}" ) _lowercase =layer.transformer.layer[transformer_num].attention.attention.all_head_size _lowercase =( F"{model_prefix}encoder.layer.{layer_num}.transformer.layer.{transformer_num}.attention.attention." ) if "weight" in key: _lowercase =val[:dim, :] _lowercase =val[dim : dim * 2, :] _lowercase =val[-dim:, :] else: _lowercase =val[:dim] _lowercase =val[dim : dim * 2] _lowercase =val[-dim:] else: _lowercase =val return orig_state_dict def UpperCAmelCase_ ( ) -> Union[str, Any]: """simple docstring""" _lowercase ='''http://images.cocodataset.org/val2017/000000039769.jpg''' _lowercase =Image.open(requests.get(__snake_case , stream=__snake_case ).raw ) return im @torch.no_grad() def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case=False ) -> int: """simple docstring""" _lowercase =get_mobilevit_config(__snake_case ) # load original state_dict _lowercase =torch.load(__snake_case , map_location='''cpu''' ) # load 🤗 model if mobilevit_name.startswith('''deeplabv3_''' ): _lowercase =MobileViTForSemanticSegmentation(__snake_case ).eval() else: _lowercase =MobileViTForImageClassification(__snake_case ).eval() _lowercase =convert_state_dict(__snake_case , __snake_case ) model.load_state_dict(__snake_case ) # Check outputs on an image, prepared by MobileViTImageProcessor _lowercase =MobileViTImageProcessor(crop_size=config.image_size , size=config.image_size + 32 ) _lowercase =image_processor(images=prepare_img() , return_tensors='''pt''' ) _lowercase =model(**__snake_case ) _lowercase =outputs.logits if mobilevit_name.startswith('''deeplabv3_''' ): assert logits.shape == (1, 21, 32, 32) if mobilevit_name == "deeplabv3_mobilevit_s": _lowercase =torch.tensor( [ [[6.20_65, 6.12_92, 6.20_70], [6.10_79, 6.12_54, 6.17_47], [6.00_42, 6.10_71, 6.10_34]], [[-6.92_53, -6.86_53, -7.03_98], [-7.32_18, -7.39_83, -7.36_70], [-7.19_61, -7.24_82, -7.15_69]], [[-4.47_23, -4.43_48, -4.37_69], [-5.36_29, -5.46_32, -5.45_98], [-5.15_87, -5.34_02, -5.50_59]], ] ) elif mobilevit_name == "deeplabv3_mobilevit_xs": _lowercase =torch.tensor( [ [[5.44_49, 5.57_33, 5.63_14], [5.18_15, 5.39_30, 5.59_63], [5.16_56, 5.43_33, 5.48_53]], [[-9.44_23, -9.77_66, -9.67_14], [-9.15_81, -9.57_20, -9.55_19], [-9.10_06, -9.64_58, -9.57_03]], [[-7.77_21, -7.37_16, -7.15_83], [-8.45_99, -8.06_24, -7.79_44], [-8.41_72, -7.83_66, -7.50_25]], ] ) elif mobilevit_name == "deeplabv3_mobilevit_xxs": _lowercase =torch.tensor( [ [[6.98_11, 6.97_43, 7.31_23], [7.17_77, 7.19_31, 7.39_38], [7.56_33, 7.80_50, 7.89_01]], [[-10.55_36, -10.23_32, -10.29_24], [-10.23_36, -9.86_24, -9.59_64], [-10.88_40, -10.81_58, -10.66_59]], [[-3.49_38, -3.06_31, -2.86_20], [-3.42_05, -2.81_35, -2.68_75], [-3.41_79, -2.79_45, -2.87_50]], ] ) else: raise ValueError(F"Unknown mobilevit_name: {mobilevit_name}" ) assert torch.allclose(logits[0, :3, :3, :3] , __snake_case , atol=1e-4 ) else: assert logits.shape == (1, 1000) if mobilevit_name == "mobilevit_s": _lowercase =torch.tensor([-0.98_66, 0.23_92, -1.12_41] ) elif mobilevit_name == "mobilevit_xs": _lowercase =torch.tensor([-2.47_61, -0.93_99, -1.95_87] ) elif mobilevit_name == "mobilevit_xxs": _lowercase =torch.tensor([-1.93_64, -1.23_27, -0.46_53] ) else: raise ValueError(F"Unknown mobilevit_name: {mobilevit_name}" ) assert torch.allclose(logits[0, :3] , __snake_case , atol=1e-4 ) Path(__snake_case ).mkdir(exist_ok=__snake_case ) print(F"Saving model {mobilevit_name} to {pytorch_dump_folder_path}" ) model.save_pretrained(__snake_case ) print(F"Saving image processor to {pytorch_dump_folder_path}" ) image_processor.save_pretrained(__snake_case ) if push_to_hub: _lowercase ={ '''mobilevit_s''': '''mobilevit-small''', '''mobilevit_xs''': '''mobilevit-x-small''', '''mobilevit_xxs''': '''mobilevit-xx-small''', '''deeplabv3_mobilevit_s''': '''deeplabv3-mobilevit-small''', '''deeplabv3_mobilevit_xs''': '''deeplabv3-mobilevit-x-small''', '''deeplabv3_mobilevit_xxs''': '''deeplabv3-mobilevit-xx-small''', } print('''Pushing to the hub...''' ) _lowercase =model_mapping[mobilevit_name] image_processor.push_to_hub(__snake_case , organization='''apple''' ) model.push_to_hub(__snake_case , organization='''apple''' ) if __name__ == "__main__": UpperCAmelCase__ = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--mobilevit_name''', default='''mobilevit_s''', type=str, help=( '''Name of the MobileViT model you\'d like to convert. Should be one of \'mobilevit_s\', \'mobilevit_xs\',''' ''' \'mobilevit_xxs\', \'deeplabv3_mobilevit_s\', \'deeplabv3_mobilevit_xs\', \'deeplabv3_mobilevit_xxs\'.''' ), ) parser.add_argument( '''--checkpoint_path''', required=True, type=str, help='''Path to the original state dict (.pt 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.mobilevit_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub )
5
1
from __future__ import annotations import copy import inspect import unittest import numpy as np from transformers import is_tf_available, is_vision_available from transformers.models.auto import get_values from transformers.testing_utils import require_tf, slow from transformers.utils import cached_property 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 tensorflow as tf from transformers import ( TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST, TF_MODEL_FOR_MULTIPLE_CHOICE_MAPPING, TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING, TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, LayoutLMvaConfig, TFLayoutLMvaForQuestionAnswering, TFLayoutLMvaForSequenceClassification, TFLayoutLMvaForTokenClassification, TFLayoutLMvaModel, ) if is_vision_available(): from PIL import Image from transformers import LayoutLMvaImageProcessor class lowerCamelCase__ : def __init__(self , UpperCAmelCase , UpperCAmelCase=2 , UpperCAmelCase=3 , UpperCAmelCase=4 , UpperCAmelCase=2 , UpperCAmelCase=7 , UpperCAmelCase=True , UpperCAmelCase=True , UpperCAmelCase=True , UpperCAmelCase=True , UpperCAmelCase=9_9 , UpperCAmelCase=3_6 , UpperCAmelCase=2 , UpperCAmelCase=4 , UpperCAmelCase=3_7 , UpperCAmelCase="gelu" , UpperCAmelCase=0.1 , UpperCAmelCase=0.1 , UpperCAmelCase=5_1_2 , UpperCAmelCase=1_6 , UpperCAmelCase=2 , UpperCAmelCase=0.02 , UpperCAmelCase=6 , UpperCAmelCase=6 , UpperCAmelCase=3 , UpperCAmelCase=4 , UpperCAmelCase=None , UpperCAmelCase=1_0_0_0 , ) -> Dict: _lowercase =parent _lowercase =batch_size _lowercase =num_channels _lowercase =image_size _lowercase =patch_size _lowercase =is_training _lowercase =use_input_mask _lowercase =use_token_type_ids _lowercase =use_labels _lowercase =vocab_size _lowercase =hidden_size _lowercase =num_hidden_layers _lowercase =num_attention_heads _lowercase =intermediate_size _lowercase =hidden_act _lowercase =hidden_dropout_prob _lowercase =attention_probs_dropout_prob _lowercase =max_position_embeddings _lowercase =type_vocab_size _lowercase =type_sequence_label_size _lowercase =initializer_range _lowercase =coordinate_size _lowercase =shape_size _lowercase =num_labels _lowercase =num_choices _lowercase =scope _lowercase =range_bbox # LayoutLMv3's sequence length equals the number of text tokens + number of patches + 1 (we add 1 for the CLS token) _lowercase =text_seq_length _lowercase =(image_size // patch_size) ** 2 + 1 _lowercase =self.text_seq_length + self.image_seq_length def __A (self ) -> Optional[Any]: _lowercase =ids_tensor([self.batch_size, self.text_seq_length] , self.vocab_size ) _lowercase =ids_tensor([self.batch_size, self.text_seq_length, 4] , self.range_bbox ) _lowercase =bbox.numpy() # Ensure that bbox is legal for i in range(bbox.shape[0] ): for j in range(bbox.shape[1] ): if bbox[i, j, 3] < bbox[i, j, 1]: _lowercase =bbox[i, j, 3] _lowercase =bbox[i, j, 1] _lowercase =tmp_coordinate if bbox[i, j, 2] < bbox[i, j, 0]: _lowercase =bbox[i, j, 2] _lowercase =bbox[i, j, 0] _lowercase =tmp_coordinate _lowercase =tf.constant(UpperCAmelCase ) _lowercase =floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) _lowercase =None if self.use_input_mask: _lowercase =random_attention_mask([self.batch_size, self.text_seq_length] ) _lowercase =None if self.use_token_type_ids: _lowercase =ids_tensor([self.batch_size, self.text_seq_length] , self.type_vocab_size ) _lowercase =None _lowercase =None if self.use_labels: _lowercase =ids_tensor([self.batch_size] , self.type_sequence_label_size ) _lowercase =ids_tensor([self.batch_size, self.text_seq_length] , self.num_labels ) _lowercase =LayoutLMvaConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , coordinate_size=self.coordinate_size , shape_size=self.shape_size , input_size=self.image_size , patch_size=self.patch_size , ) return config, input_ids, bbox, pixel_values, token_type_ids, input_mask, sequence_labels, token_labels def __A (self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) -> Optional[int]: _lowercase =TFLayoutLMvaModel(config=UpperCAmelCase ) # text + image _lowercase =model(UpperCAmelCase , pixel_values=UpperCAmelCase , training=UpperCAmelCase ) _lowercase =model( UpperCAmelCase , bbox=UpperCAmelCase , pixel_values=UpperCAmelCase , attention_mask=UpperCAmelCase , token_type_ids=UpperCAmelCase , training=UpperCAmelCase , ) _lowercase =model(UpperCAmelCase , bbox=UpperCAmelCase , pixel_values=UpperCAmelCase , training=UpperCAmelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) # text only _lowercase =model(UpperCAmelCase , training=UpperCAmelCase ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.text_seq_length, self.hidden_size) ) # image only _lowercase =model({'''pixel_values''': pixel_values} , training=UpperCAmelCase ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.image_seq_length, self.hidden_size) ) def __A (self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) -> Union[str, Any]: _lowercase =self.num_labels _lowercase =TFLayoutLMvaForSequenceClassification(config=UpperCAmelCase ) _lowercase =model( UpperCAmelCase , bbox=UpperCAmelCase , pixel_values=UpperCAmelCase , attention_mask=UpperCAmelCase , token_type_ids=UpperCAmelCase , labels=UpperCAmelCase , training=UpperCAmelCase , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __A (self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) -> Optional[int]: _lowercase =self.num_labels _lowercase =TFLayoutLMvaForTokenClassification(config=UpperCAmelCase ) _lowercase =model( UpperCAmelCase , bbox=UpperCAmelCase , pixel_values=UpperCAmelCase , attention_mask=UpperCAmelCase , token_type_ids=UpperCAmelCase , labels=UpperCAmelCase , training=UpperCAmelCase , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.text_seq_length, self.num_labels) ) def __A (self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) -> Dict: _lowercase =2 _lowercase =TFLayoutLMvaForQuestionAnswering(config=UpperCAmelCase ) _lowercase =model( UpperCAmelCase , bbox=UpperCAmelCase , pixel_values=UpperCAmelCase , attention_mask=UpperCAmelCase , token_type_ids=UpperCAmelCase , start_positions=UpperCAmelCase , end_positions=UpperCAmelCase , training=UpperCAmelCase , ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def __A (self ) -> Union[str, Any]: _lowercase =self.prepare_config_and_inputs() ((_lowercase) , (_lowercase) , (_lowercase) , (_lowercase) , (_lowercase) , (_lowercase) , (_lowercase) , (_lowercase)) =config_and_inputs _lowercase ={ '''input_ids''': input_ids, '''bbox''': bbox, '''pixel_values''': pixel_values, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask, } return config, inputs_dict @require_tf class lowerCamelCase__ ( lowerCAmelCase , lowerCAmelCase , unittest.TestCase): SCREAMING_SNAKE_CASE__ = ( ( TFLayoutLMvaModel, TFLayoutLMvaForQuestionAnswering, TFLayoutLMvaForSequenceClassification, TFLayoutLMvaForTokenClassification, ) if is_tf_available() else () ) SCREAMING_SNAKE_CASE__ = ( {'''document-question-answering''': TFLayoutLMvaForQuestionAnswering, '''feature-extraction''': TFLayoutLMvaModel} if is_tf_available() else {} ) SCREAMING_SNAKE_CASE__ = False SCREAMING_SNAKE_CASE__ = False SCREAMING_SNAKE_CASE__ = False def __A (self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) -> str: return True def __A (self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase=False ) -> dict: _lowercase =copy.deepcopy(UpperCAmelCase ) if model_class in get_values(UpperCAmelCase ): _lowercase ={ k: tf.tile(tf.expand_dims(UpperCAmelCase , 1 ) , (1, self.model_tester.num_choices) + (1,) * (v.ndim - 1) ) if isinstance(UpperCAmelCase , tf.Tensor ) and v.ndim > 0 else v for k, v in inputs_dict.items() } if return_labels: if model_class in get_values(UpperCAmelCase ): _lowercase =tf.ones(self.model_tester.batch_size , dtype=tf.intaa ) elif model_class in get_values(UpperCAmelCase ): _lowercase =tf.zeros(self.model_tester.batch_size , dtype=tf.intaa ) _lowercase =tf.zeros(self.model_tester.batch_size , dtype=tf.intaa ) elif model_class in get_values(UpperCAmelCase ): _lowercase =tf.zeros(self.model_tester.batch_size , dtype=tf.intaa ) elif model_class in get_values(UpperCAmelCase ): _lowercase =tf.zeros( (self.model_tester.batch_size, self.model_tester.text_seq_length) , dtype=tf.intaa ) return inputs_dict def __A (self ) -> str: _lowercase =TFLayoutLMvaModelTester(self ) _lowercase =ConfigTester(self , config_class=UpperCAmelCase , hidden_size=3_7 ) def __A (self ) -> Optional[Any]: self.config_tester.run_common_tests() def __A (self ) -> Optional[int]: _lowercase , _lowercase =self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _lowercase =model_class(UpperCAmelCase ) if getattr(UpperCAmelCase , '''hf_compute_loss''' , UpperCAmelCase ): # The number of elements in the loss should be the same as the number of elements in the label _lowercase =self._prepare_for_class(inputs_dict.copy() , UpperCAmelCase , return_labels=UpperCAmelCase ) _lowercase =prepared_for_class[ sorted(prepared_for_class.keys() - inputs_dict.keys() , reverse=UpperCAmelCase )[0] ] _lowercase =added_label.shape.as_list()[:1] # Test that model correctly compute the loss with kwargs _lowercase =self._prepare_for_class(inputs_dict.copy() , UpperCAmelCase , return_labels=UpperCAmelCase ) _lowercase =prepared_for_class.pop('''input_ids''' ) _lowercase =model(UpperCAmelCase , **UpperCAmelCase )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) # Test that model correctly compute the loss when we mask some positions _lowercase =self._prepare_for_class(inputs_dict.copy() , UpperCAmelCase , return_labels=UpperCAmelCase ) _lowercase =prepared_for_class.pop('''input_ids''' ) if "labels" in prepared_for_class: _lowercase =prepared_for_class['''labels'''].numpy() if len(labels.shape ) > 1 and labels.shape[1] != 1: _lowercase =-1_0_0 _lowercase =tf.convert_to_tensor(UpperCAmelCase ) _lowercase =model(UpperCAmelCase , **UpperCAmelCase )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) self.assertTrue(not np.any(np.isnan(loss.numpy() ) ) ) # Test that model correctly compute the loss with a dict _lowercase =self._prepare_for_class(inputs_dict.copy() , UpperCAmelCase , return_labels=UpperCAmelCase ) _lowercase =model(UpperCAmelCase )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) # Test that model correctly compute the loss with a tuple _lowercase =self._prepare_for_class(inputs_dict.copy() , UpperCAmelCase , return_labels=UpperCAmelCase ) # Get keys that were added with the _prepare_for_class function _lowercase =prepared_for_class.keys() - inputs_dict.keys() _lowercase =inspect.signature(model.call ).parameters _lowercase =list(signature.keys() ) # Create a dictionary holding the location of the tensors in the tuple _lowercase ={0: '''input_ids'''} for label_key in label_keys: _lowercase =signature_names.index(UpperCAmelCase ) _lowercase =label_key _lowercase =sorted(tuple_index_mapping.items() ) # Initialize a list with their default values, update the values and convert to a tuple _lowercase =[] for name in signature_names: if name != "kwargs": list_input.append(signature[name].default ) for index, value in sorted_tuple_index_mapping: _lowercase =prepared_for_class[value] _lowercase =tuple(UpperCAmelCase ) # Send to model _lowercase =model(tuple_input[:-1] )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) def __A (self ) -> List[Any]: ( ( _lowercase ) , ( _lowercase ) , ( _lowercase ) , ( _lowercase ) , ( _lowercase ) , ( _lowercase ) , ( _lowercase ) , ( _lowercase ) , ) =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) def __A (self ) -> int: ( ( _lowercase ) , ( _lowercase ) , ( _lowercase ) , ( _lowercase ) , ( _lowercase ) , ( _lowercase ) , ( _lowercase ) , ( _lowercase ) , ) =self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: _lowercase =type self.model_tester.create_and_check_model(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) def __A (self ) -> Dict: ( ( _lowercase ) , ( _lowercase ) , ( _lowercase ) , ( _lowercase ) , ( _lowercase ) , ( _lowercase ) , ( _lowercase ) , ( _lowercase ) , ) =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification( UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) def __A (self ) -> Tuple: ( ( _lowercase ) , ( _lowercase ) , ( _lowercase ) , ( _lowercase ) , ( _lowercase ) , ( _lowercase ) , ( _lowercase ) , ( _lowercase ) , ) =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification( UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) def __A (self ) -> Any: ( ( _lowercase ) , ( _lowercase ) , ( _lowercase ) , ( _lowercase ) , ( _lowercase ) , ( _lowercase ) , ( _lowercase ) , ( _lowercase ) , ) =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering( UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) @slow def __A (self ) -> Optional[Any]: for model_name in TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _lowercase =TFLayoutLMvaModel.from_pretrained(UpperCAmelCase ) self.assertIsNotNone(UpperCAmelCase ) def UpperCAmelCase_ ( ) -> List[Any]: """simple docstring""" _lowercase =Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_tf class lowerCamelCase__ ( unittest.TestCase): @cached_property def __A (self ) -> Any: return LayoutLMvaImageProcessor(apply_ocr=UpperCAmelCase ) if is_vision_available() else None @slow def __A (self ) -> Any: _lowercase =TFLayoutLMvaModel.from_pretrained('''microsoft/layoutlmv3-base''' ) _lowercase =self.default_image_processor _lowercase =prepare_img() _lowercase =image_processor(images=UpperCAmelCase , return_tensors='''tf''' ).pixel_values _lowercase =tf.constant([[1, 2]] ) _lowercase =tf.expand_dims(tf.constant([[1, 2, 3, 4], [5, 6, 7, 8]] ) , axis=0 ) # forward pass _lowercase =model(input_ids=UpperCAmelCase , bbox=UpperCAmelCase , pixel_values=UpperCAmelCase , training=UpperCAmelCase ) # verify the logits _lowercase =(1, 1_9_9, 7_6_8) self.assertEqual(outputs.last_hidden_state.shape , UpperCAmelCase ) _lowercase =tf.constant( [[-0.0529, 0.3618, 0.1632], [-0.1587, -0.1667, -0.0400], [-0.1557, -0.1671, -0.0505]] ) self.assertTrue(np.allclose(outputs.last_hidden_state[0, :3, :3] , UpperCAmelCase , atol=1e-4 ) )
5
import requests from bsa import BeautifulSoup def UpperCAmelCase_ ( __snake_case = "https://www.worldometers.info/coronavirus" ) -> dict: """simple docstring""" _lowercase =BeautifulSoup(requests.get(__snake_case ).text , '''html.parser''' ) _lowercase =soup.findAll('''h1''' ) _lowercase =soup.findAll('''div''' , {'''class''': '''maincounter-number'''} ) keys += soup.findAll('''span''' , {'''class''': '''panel-title'''} ) values += soup.findAll('''div''' , {'''class''': '''number-table-main'''} ) return {key.text.strip(): value.text.strip() for key, value in zip(__snake_case , __snake_case )} if __name__ == "__main__": print('''\033[1m''' + '''COVID-19 Status of the World''' + '''\033[0m\n''') for key, value in world_covidaa_stats().items(): print(f'''{key}\n{value}\n''')
5
1
import tempfile import unittest from make_student import create_student_by_copying_alternating_layers from transformers import AutoConfig from transformers.file_utils import cached_property from transformers.testing_utils import require_torch UpperCAmelCase__ = '''sshleifer/bart-tiny-random''' UpperCAmelCase__ = '''patrickvonplaten/t5-tiny-random''' @require_torch class lowerCamelCase__ ( unittest.TestCase): @cached_property def __A (self ) -> Optional[int]: return AutoConfig.from_pretrained(UpperCAmelCase ) def __A (self ) -> Any: _lowercase , *_lowercase =create_student_by_copying_alternating_layers(UpperCAmelCase , tempfile.mkdtemp() , e=1 , d=1 ) self.assertEqual(student.config.num_hidden_layers , 1 ) def __A (self ) -> Optional[Any]: _lowercase , *_lowercase =create_student_by_copying_alternating_layers(UpperCAmelCase , tempfile.mkdtemp() , e=1 , d=UpperCAmelCase ) def __A (self ) -> Union[str, Any]: _lowercase , *_lowercase =create_student_by_copying_alternating_layers(UpperCAmelCase , tempfile.mkdtemp() , e=1 , d=UpperCAmelCase ) self.assertEqual(student.config.encoder_layers , 1 ) self.assertEqual(student.config.decoder_layers , self.teacher_config.encoder_layers ) def __A (self ) -> List[Any]: _lowercase , *_lowercase =create_student_by_copying_alternating_layers(UpperCAmelCase , tempfile.mkdtemp() , e=1 , d=1 ) self.assertEqual(student.config.encoder_layers , 1 ) self.assertEqual(student.config.decoder_layers , 1 ) def __A (self ) -> Dict: with self.assertRaises(UpperCAmelCase ): create_student_by_copying_alternating_layers(UpperCAmelCase , tempfile.mkdtemp() , e=UpperCAmelCase , d=UpperCAmelCase )
5
from typing import TYPE_CHECKING from ..utils import _LazyModule UpperCAmelCase__ = { '''config''': [ '''EXTERNAL_DATA_FORMAT_SIZE_LIMIT''', '''OnnxConfig''', '''OnnxConfigWithPast''', '''OnnxSeq2SeqConfigWithPast''', '''PatchingSpec''', ], '''convert''': ['''export''', '''validate_model_outputs'''], '''features''': ['''FeaturesManager'''], '''utils''': ['''ParameterFormat''', '''compute_serialized_parameters_size'''], } if TYPE_CHECKING: from .config import ( EXTERNAL_DATA_FORMAT_SIZE_LIMIT, OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast, PatchingSpec, ) from .convert import export, validate_model_outputs from .features import FeaturesManager from .utils import ParameterFormat, compute_serialized_parameters_size else: import sys UpperCAmelCase__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
5
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_torch_available, ) UpperCAmelCase__ = { '''configuration_mega''': ['''MEGA_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''MegaConfig''', '''MegaOnnxConfig'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ '''MEGA_PRETRAINED_MODEL_ARCHIVE_LIST''', '''MegaForCausalLM''', '''MegaForMaskedLM''', '''MegaForMultipleChoice''', '''MegaForQuestionAnswering''', '''MegaForSequenceClassification''', '''MegaForTokenClassification''', '''MegaModel''', '''MegaPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_mega import MEGA_PRETRAINED_CONFIG_ARCHIVE_MAP, MegaConfig, MegaOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mega import ( MEGA_PRETRAINED_MODEL_ARCHIVE_LIST, MegaForCausalLM, MegaForMaskedLM, MegaForMultipleChoice, MegaForQuestionAnswering, MegaForSequenceClassification, MegaForTokenClassification, MegaModel, MegaPreTrainedModel, ) else: import sys UpperCAmelCase__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
5
def UpperCAmelCase_ ( __snake_case ) -> str: """simple docstring""" _lowercase =0 # if input_string is "aba" than new_input_string become "a|b|a" _lowercase ='''''' _lowercase ='''''' # append each character + "|" in new_string for range(0, length-1) for i in input_string[: len(__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 _lowercase , _lowercase =0, 0 # length[i] shows the length of palindromic substring with center i _lowercase =[1 for i in range(len(__snake_case ) )] # for each character in new_string find corresponding palindromic string _lowercase =0 for j in range(len(__snake_case ) ): _lowercase =1 if j > r else min(length[l + r - j] // 2 , r - j + 1 ) while ( j - k >= 0 and j + k < len(__snake_case ) and new_input_string[k + j] == new_input_string[j - k] ): k += 1 _lowercase =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: _lowercase =j - k + 1 # noqa: E741 _lowercase =j + k - 1 # update max_length and start position if max_length < length[j]: _lowercase =length[j] _lowercase =j # create that string _lowercase =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()
5
1
def UpperCAmelCase_ ( __snake_case = 10**12 ) -> int: """simple docstring""" _lowercase =1 _lowercase =0 _lowercase =1 _lowercase =1 while numerator <= 2 * min_total - 1: prev_numerator += 2 * numerator numerator += 2 * prev_numerator prev_denominator += 2 * denominator denominator += 2 * prev_denominator return (denominator + 1) // 2 if __name__ == "__main__": print(f'''{solution() = }''')
5
from math import isqrt def UpperCAmelCase_ ( __snake_case ) -> list[int]: """simple docstring""" _lowercase =[True] * max_number for i in range(2 , isqrt(max_number - 1 ) + 1 ): if is_prime[i]: for j in range(i**2 , __snake_case , __snake_case ): _lowercase =False return [i for i in range(2 , __snake_case ) if is_prime[i]] def UpperCAmelCase_ ( __snake_case = 10**8 ) -> int: """simple docstring""" _lowercase =calculate_prime_numbers(max_number // 2 ) _lowercase =0 _lowercase =0 _lowercase =len(__snake_case ) - 1 while left <= right: while prime_numbers[left] * prime_numbers[right] >= max_number: right -= 1 semiprimes_count += right - left + 1 left += 1 return semiprimes_count if __name__ == "__main__": print(f'''{solution() = }''')
5
1
from __future__ import annotations import unittest from transformers import BlenderbotConfig, BlenderbotTokenizer, is_tf_available from transformers.testing_utils import require_tf, require_tokenizers, slow from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFAutoModelForSeqaSeqLM, TFBlenderbotForConditionalGeneration, TFBlenderbotModel @require_tf class lowerCamelCase__ : SCREAMING_SNAKE_CASE__ = BlenderbotConfig SCREAMING_SNAKE_CASE__ = {} SCREAMING_SNAKE_CASE__ = '''gelu''' def __init__(self , UpperCAmelCase , UpperCAmelCase=1_3 , UpperCAmelCase=7 , UpperCAmelCase=True , UpperCAmelCase=False , UpperCAmelCase=9_9 , UpperCAmelCase=3_2 , UpperCAmelCase=2 , UpperCAmelCase=4 , UpperCAmelCase=3_7 , UpperCAmelCase=0.1 , UpperCAmelCase=0.1 , UpperCAmelCase=2_0 , UpperCAmelCase=2 , UpperCAmelCase=1 , UpperCAmelCase=0 , ) -> Dict: _lowercase =parent _lowercase =batch_size _lowercase =seq_length _lowercase =is_training _lowercase =use_labels _lowercase =vocab_size _lowercase =hidden_size _lowercase =num_hidden_layers _lowercase =num_attention_heads _lowercase =intermediate_size _lowercase =hidden_dropout_prob _lowercase =attention_probs_dropout_prob _lowercase =max_position_embeddings _lowercase =eos_token_id _lowercase =pad_token_id _lowercase =bos_token_id def __A (self ) -> Optional[int]: _lowercase =ids_tensor([self.batch_size, self.seq_length - 1] , self.vocab_size ) _lowercase =tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size ) , 1 ) _lowercase =tf.concat([input_ids, eos_tensor] , axis=1 ) _lowercase =ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) _lowercase =self.config_cls( vocab_size=self.vocab_size , d_model=self.hidden_size , encoder_layers=self.num_hidden_layers , decoder_layers=self.num_hidden_layers , encoder_attention_heads=self.num_attention_heads , decoder_attention_heads=self.num_attention_heads , encoder_ffn_dim=self.intermediate_size , decoder_ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , eos_token_ids=[2] , bos_token_id=self.bos_token_id , pad_token_id=self.pad_token_id , decoder_start_token_id=self.pad_token_id , **self.config_updates , ) _lowercase =prepare_blenderbot_inputs_dict(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) return config, inputs_dict def __A (self , UpperCAmelCase , UpperCAmelCase ) -> Optional[int]: _lowercase =TFBlenderbotModel(config=UpperCAmelCase ).get_decoder() _lowercase =inputs_dict['''input_ids'''] _lowercase =input_ids[:1, :] _lowercase =inputs_dict['''attention_mask'''][:1, :] _lowercase =inputs_dict['''head_mask'''] _lowercase =1 # first forward pass _lowercase =model(UpperCAmelCase , attention_mask=UpperCAmelCase , head_mask=UpperCAmelCase , use_cache=UpperCAmelCase ) _lowercase , _lowercase =outputs.to_tuple() # create hypothetical next token and extent to next_input_ids _lowercase =ids_tensor((self.batch_size, 3) , config.vocab_size ) _lowercase =tf.cast(ids_tensor((self.batch_size, 3) , 2 ) , tf.inta ) # append to next input_ids and _lowercase =tf.concat([input_ids, next_tokens] , axis=-1 ) _lowercase =tf.concat([attention_mask, next_attn_mask] , axis=-1 ) _lowercase =model(UpperCAmelCase , attention_mask=UpperCAmelCase )[0] _lowercase =model(UpperCAmelCase , attention_mask=UpperCAmelCase , past_key_values=UpperCAmelCase )[0] self.parent.assertEqual(next_tokens.shape[1] , output_from_past.shape[1] ) # select random slice _lowercase =int(ids_tensor((1,) , output_from_past.shape[-1] ) ) _lowercase =output_from_no_past[:, -3:, random_slice_idx] _lowercase =output_from_past[:, :, random_slice_idx] # test that outputs are equal for slice tf.debugging.assert_near(UpperCAmelCase , UpperCAmelCase , rtol=1e-3 ) def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case=None , __snake_case=None , __snake_case=None , __snake_case=None , __snake_case=None , ) -> Optional[int]: """simple docstring""" if attention_mask is None: _lowercase =tf.cast(tf.math.not_equal(__snake_case , config.pad_token_id ) , tf.inta ) if decoder_attention_mask is None: _lowercase =tf.concat( [ tf.ones(decoder_input_ids[:, :1].shape , dtype=tf.inta ), tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:] , config.pad_token_id ) , tf.inta ), ] , axis=-1 , ) if head_mask is None: _lowercase =tf.ones((config.encoder_layers, config.encoder_attention_heads) ) if decoder_head_mask is None: _lowercase =tf.ones((config.decoder_layers, config.decoder_attention_heads) ) if cross_attn_head_mask is None: _lowercase =tf.ones((config.decoder_layers, config.decoder_attention_heads) ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": decoder_attention_mask, "head_mask": head_mask, "decoder_head_mask": decoder_head_mask, "cross_attn_head_mask": cross_attn_head_mask, } @require_tf class lowerCamelCase__ ( lowerCAmelCase , lowerCAmelCase , unittest.TestCase): SCREAMING_SNAKE_CASE__ = (TFBlenderbotForConditionalGeneration, TFBlenderbotModel) if is_tf_available() else () SCREAMING_SNAKE_CASE__ = (TFBlenderbotForConditionalGeneration,) if is_tf_available() else () SCREAMING_SNAKE_CASE__ = ( { '''conversational''': TFBlenderbotForConditionalGeneration, '''feature-extraction''': TFBlenderbotModel, '''summarization''': TFBlenderbotForConditionalGeneration, '''text2text-generation''': TFBlenderbotForConditionalGeneration, '''translation''': TFBlenderbotForConditionalGeneration, } if is_tf_available() else {} ) SCREAMING_SNAKE_CASE__ = True SCREAMING_SNAKE_CASE__ = False SCREAMING_SNAKE_CASE__ = False def __A (self ) -> Optional[Any]: _lowercase =TFBlenderbotModelTester(self ) _lowercase =ConfigTester(self , config_class=UpperCAmelCase ) def __A (self ) -> Optional[Any]: self.config_tester.run_common_tests() def __A (self ) -> Union[str, Any]: _lowercase =self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_decoder_model_past_large_inputs(*UpperCAmelCase ) @require_tokenizers @require_tf class lowerCamelCase__ ( unittest.TestCase): SCREAMING_SNAKE_CASE__ = ['''My friends are cool but they eat too many carbs.'''] SCREAMING_SNAKE_CASE__ = '''facebook/blenderbot-400M-distill''' @cached_property def __A (self ) -> Union[str, Any]: return BlenderbotTokenizer.from_pretrained(self.model_name ) @cached_property def __A (self ) -> List[Any]: _lowercase =TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name ) return model @slow def __A (self ) -> str: _lowercase =self.tokenizer(self.src_text , return_tensors='''tf''' ) _lowercase =self.model.generate( model_inputs.input_ids , ) _lowercase =self.tokenizer.batch_decode(generated_ids.numpy() , skip_special_tokens=UpperCAmelCase )[0] assert ( generated_words == " That's unfortunate. Are they trying to lose weight or are they just trying to be healthier?" )
5
UpperCAmelCase__ = { '''A''': '''.-''', '''B''': '''-...''', '''C''': '''-.-.''', '''D''': '''-..''', '''E''': '''.''', '''F''': '''..-.''', '''G''': '''--.''', '''H''': '''....''', '''I''': '''..''', '''J''': '''.---''', '''K''': '''-.-''', '''L''': '''.-..''', '''M''': '''--''', '''N''': '''-.''', '''O''': '''---''', '''P''': '''.--.''', '''Q''': '''--.-''', '''R''': '''.-.''', '''S''': '''...''', '''T''': '''-''', '''U''': '''..-''', '''V''': '''...-''', '''W''': '''.--''', '''X''': '''-..-''', '''Y''': '''-.--''', '''Z''': '''--..''', '''1''': '''.----''', '''2''': '''..---''', '''3''': '''...--''', '''4''': '''....-''', '''5''': '''.....''', '''6''': '''-....''', '''7''': '''--...''', '''8''': '''---..''', '''9''': '''----.''', '''0''': '''-----''', '''&''': '''.-...''', '''@''': '''.--.-.''', ''':''': '''---...''', ''',''': '''--..--''', '''.''': '''.-.-.-''', '''\'''': '''.----.''', '''"''': '''.-..-.''', '''?''': '''..--..''', '''/''': '''-..-.''', '''=''': '''-...-''', '''+''': '''.-.-.''', '''-''': '''-....-''', '''(''': '''-.--.''', ''')''': '''-.--.-''', '''!''': '''-.-.--''', ''' ''': '''/''' } # Exclamation mark is not in ITU-R recommendation # fmt: on UpperCAmelCase__ = {value: key for key, value in MORSE_CODE_DICT.items()} def UpperCAmelCase_ ( __snake_case ) -> str: """simple docstring""" return " ".join(MORSE_CODE_DICT[char] for char in message.upper() ) def UpperCAmelCase_ ( __snake_case ) -> str: """simple docstring""" return "".join(REVERSE_DICT[char] for char in message.split() ) def UpperCAmelCase_ ( ) -> None: """simple docstring""" _lowercase ='''Morse code here!''' print(__snake_case ) _lowercase =encrypt(__snake_case ) print(__snake_case ) _lowercase =decrypt(__snake_case ) print(__snake_case ) if __name__ == "__main__": main()
5
1
import os import unittest from transformers import LxmertTokenizer, LxmertTokenizerFast from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class lowerCamelCase__ ( lowerCAmelCase , unittest.TestCase): SCREAMING_SNAKE_CASE__ = LxmertTokenizer SCREAMING_SNAKE_CASE__ = LxmertTokenizerFast SCREAMING_SNAKE_CASE__ = True SCREAMING_SNAKE_CASE__ = True def __A (self ) -> Any: super().setUp() _lowercase =[ '''[UNK]''', '''[CLS]''', '''[SEP]''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing''', ''',''', '''low''', '''lowest''', ] _lowercase =os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as vocab_writer: vocab_writer.write(''''''.join([x + '''\n''' for x in vocab_tokens] ) ) def __A (self , UpperCAmelCase ) -> str: _lowercase ='''UNwant\u00E9d,running''' _lowercase ='''unwanted, running''' return input_text, output_text def __A (self ) -> Tuple: _lowercase =self.tokenizer_class(self.vocab_file ) _lowercase =tokenizer.tokenize('''UNwant\u00E9d,running''' ) self.assertListEqual(UpperCAmelCase , ['''un''', '''##want''', '''##ed''', ''',''', '''runn''', '''##ing'''] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(UpperCAmelCase ) , [7, 4, 5, 1_0, 8, 9] ) def __A (self ) -> int: if not self.test_rust_tokenizer: return _lowercase =self.get_tokenizer() _lowercase =self.get_rust_tokenizer() _lowercase ='''I was born in 92000, and this is falsé.''' _lowercase =tokenizer.tokenize(UpperCAmelCase ) _lowercase =rust_tokenizer.tokenize(UpperCAmelCase ) self.assertListEqual(UpperCAmelCase , UpperCAmelCase ) _lowercase =tokenizer.encode(UpperCAmelCase , add_special_tokens=UpperCAmelCase ) _lowercase =rust_tokenizer.encode(UpperCAmelCase , add_special_tokens=UpperCAmelCase ) self.assertListEqual(UpperCAmelCase , UpperCAmelCase ) _lowercase =self.get_rust_tokenizer() _lowercase =tokenizer.encode(UpperCAmelCase ) _lowercase =rust_tokenizer.encode(UpperCAmelCase ) self.assertListEqual(UpperCAmelCase , UpperCAmelCase )
5
from typing import Any def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , ) -> list: """simple docstring""" _validation( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , ) # Creates data structures and fill initial step _lowercase ={} _lowercase ={} for state in states_space: _lowercase =observations_space[0] _lowercase =( initial_probabilities[state] * emission_probabilities[state][observation] ) _lowercase =None # Fills the data structure with the probabilities of # different transitions and pointers to previous states for o in range(1 , len(__snake_case ) ): _lowercase =observations_space[o] _lowercase =observations_space[o - 1] for state in states_space: # Calculates the argmax for probability function _lowercase ='''''' _lowercase =-1 for k_state in states_space: _lowercase =( probabilities[(k_state, prior_observation)] * transition_probabilities[k_state][state] * emission_probabilities[state][observation] ) if probability > max_probability: _lowercase =probability _lowercase =k_state # Update probabilities and pointers dicts _lowercase =( probabilities[(arg_max, prior_observation)] * transition_probabilities[arg_max][state] * emission_probabilities[state][observation] ) _lowercase =arg_max # The final observation _lowercase =observations_space[len(__snake_case ) - 1] # argmax for given final observation _lowercase ='''''' _lowercase =-1 for k_state in states_space: _lowercase =probabilities[(k_state, final_observation)] if probability > max_probability: _lowercase =probability _lowercase =k_state _lowercase =arg_max # Process pointers backwards _lowercase =last_state _lowercase =[] for o in range(len(__snake_case ) - 1 , -1 , -1 ): result.append(__snake_case ) _lowercase =pointers[previous, observations_space[o]] result.reverse() return result def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , ) -> None: """simple docstring""" _validate_not_empty( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , ) _validate_lists(__snake_case , __snake_case ) _validate_dicts( __snake_case , __snake_case , __snake_case ) def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , ) -> None: """simple docstring""" if not all( [ observations_space, states_space, initial_probabilities, transition_probabilities, emission_probabilities, ] ): raise ValueError('''There\'s an empty parameter''' ) def UpperCAmelCase_ ( __snake_case , __snake_case ) -> None: """simple docstring""" _validate_list(__snake_case , '''observations_space''' ) _validate_list(__snake_case , '''states_space''' ) def UpperCAmelCase_ ( __snake_case , __snake_case ) -> None: """simple docstring""" if not isinstance(_object , __snake_case ): _lowercase =F"{var_name} must be a list" raise ValueError(__snake_case ) else: for x in _object: if not isinstance(__snake_case , __snake_case ): _lowercase =F"{var_name} must be a list of strings" raise ValueError(__snake_case ) def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , ) -> None: """simple docstring""" _validate_dict(__snake_case , '''initial_probabilities''' , __snake_case ) _validate_nested_dict(__snake_case , '''transition_probabilities''' ) _validate_nested_dict(__snake_case , '''emission_probabilities''' ) def UpperCAmelCase_ ( __snake_case , __snake_case ) -> None: """simple docstring""" _validate_dict(_object , __snake_case , __snake_case ) for x in _object.values(): _validate_dict(__snake_case , __snake_case , __snake_case , __snake_case ) def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case = False ) -> None: """simple docstring""" if not isinstance(_object , __snake_case ): _lowercase =F"{var_name} must be a dict" raise ValueError(__snake_case ) if not all(isinstance(__snake_case , __snake_case ) for x in _object ): _lowercase =F"{var_name} all keys must be strings" raise ValueError(__snake_case ) if not all(isinstance(__snake_case , __snake_case ) for x in _object.values() ): _lowercase ='''nested dictionary ''' if nested else '''''' _lowercase =F"{var_name} {nested_text}all values must be {value_type.__name__}" raise ValueError(__snake_case ) if __name__ == "__main__": from doctest import testmod testmod()
5
1
import argparse from collections import defaultdict import yaml UpperCAmelCase__ = '''docs/source/en/_toctree.yml''' def UpperCAmelCase_ ( __snake_case ) -> Dict: """simple docstring""" _lowercase =defaultdict(__snake_case ) for doc in model_doc: counts[doc["local"]] += 1 _lowercase =[key for key, value in counts.items() if value > 1] _lowercase =[] for duplicate_key in duplicates: _lowercase =list({doc['''title'''] for doc in model_doc if doc['''local'''] == duplicate_key} ) if len(__snake_case ) > 1: raise ValueError( F"{duplicate_key} is present several times in the documentation table of content at " '''`docs/source/en/_toctree.yml` with different *Title* values. Choose one of those and remove the ''' '''others.''' ) # Only add this once new_doc.append({'''local''': duplicate_key, '''title''': titles[0]} ) # Add none duplicate-keys new_doc.extend([doc for doc in model_doc if counts[doc['''local''']] == 1] ) # Sort return sorted(__snake_case , key=lambda __snake_case : s["title"].lower() ) def UpperCAmelCase_ ( __snake_case=False ) -> Optional[int]: """simple docstring""" with open(__snake_case , encoding='''utf-8''' ) as f: _lowercase =yaml.safe_load(f.read() ) # Get to the API doc _lowercase =0 while content[api_idx]["title"] != "API": api_idx += 1 _lowercase =content[api_idx]['''sections'''] # Then to the model doc _lowercase =0 while api_doc[model_idx]["title"] != "Models": model_idx += 1 _lowercase =api_doc[model_idx]['''sections'''] _lowercase =[(idx, section) for idx, section in enumerate(__snake_case ) if '''sections''' in section] _lowercase =False for idx, modality_doc in modalities_docs: _lowercase =modality_doc['''sections'''] _lowercase =clean_model_doc_toc(__snake_case ) if old_modality_doc != new_modality_doc: _lowercase =True if overwrite: _lowercase =new_modality_doc if diff: if overwrite: _lowercase =model_doc _lowercase =api_doc with open(__snake_case , '''w''' , encoding='''utf-8''' ) as f: f.write(yaml.dump(__snake_case , allow_unicode=__snake_case ) ) else: raise ValueError( '''The model doc part of the table of content is not properly sorted, run `make style` to fix this.''' ) if __name__ == "__main__": UpperCAmelCase__ = argparse.ArgumentParser() parser.add_argument('''--fix_and_overwrite''', action='''store_true''', help='''Whether to fix inconsistencies.''') UpperCAmelCase__ = parser.parse_args() check_model_doc(args.fix_and_overwrite)
5
from dataclasses import asdict, dataclass from typing import Optional from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCAmelCase__ = logging.get_logger(__name__) # TODO Update this UpperCAmelCase__ = { '''facebook/esm-1b''': '''https://huggingface.co/facebook/esm-1b/resolve/main/config.json''', # See all ESM models at https://huggingface.co/models?filter=esm } class lowerCamelCase__ ( lowerCAmelCase): SCREAMING_SNAKE_CASE__ = '''esm''' def __init__(self , UpperCAmelCase=None , UpperCAmelCase=None , UpperCAmelCase=None , UpperCAmelCase=7_6_8 , UpperCAmelCase=1_2 , UpperCAmelCase=1_2 , UpperCAmelCase=3_0_7_2 , UpperCAmelCase=0.1 , UpperCAmelCase=0.1 , UpperCAmelCase=1_0_2_6 , UpperCAmelCase=0.02 , UpperCAmelCase=1e-12 , UpperCAmelCase="absolute" , UpperCAmelCase=True , UpperCAmelCase=None , UpperCAmelCase=False , UpperCAmelCase=False , UpperCAmelCase=None , UpperCAmelCase=None , **UpperCAmelCase , ) -> Tuple: super().__init__(pad_token_id=UpperCAmelCase , mask_token_id=UpperCAmelCase , **UpperCAmelCase ) _lowercase =vocab_size _lowercase =hidden_size _lowercase =num_hidden_layers _lowercase =num_attention_heads _lowercase =intermediate_size _lowercase =hidden_dropout_prob _lowercase =attention_probs_dropout_prob _lowercase =max_position_embeddings _lowercase =initializer_range _lowercase =layer_norm_eps _lowercase =position_embedding_type _lowercase =use_cache _lowercase =emb_layer_norm_before _lowercase =token_dropout _lowercase =is_folding_model if is_folding_model: if esmfold_config is None: logger.info('''No esmfold_config supplied for folding model, using default values.''' ) _lowercase =EsmFoldConfig() elif isinstance(UpperCAmelCase , UpperCAmelCase ): _lowercase =EsmFoldConfig(**UpperCAmelCase ) _lowercase =esmfold_config if vocab_list is None: logger.warning('''No vocab_list supplied for folding model, assuming the ESM-2 vocabulary!''' ) _lowercase =get_default_vocab_list() else: _lowercase =vocab_list else: _lowercase =None _lowercase =None if self.esmfold_config is not None and getattr(self.esmfold_config , '''use_esm_attn_map''' , UpperCAmelCase ): raise ValueError('''The HuggingFace port of ESMFold does not support use_esm_attn_map at this time!''' ) def __A (self ) -> List[str]: _lowercase =super().to_dict() if isinstance(self.esmfold_config , UpperCAmelCase ): _lowercase =self.esmfold_config.to_dict() return output @dataclass class lowerCamelCase__ : SCREAMING_SNAKE_CASE__ = None SCREAMING_SNAKE_CASE__ = True SCREAMING_SNAKE_CASE__ = False SCREAMING_SNAKE_CASE__ = False SCREAMING_SNAKE_CASE__ = False SCREAMING_SNAKE_CASE__ = 0 SCREAMING_SNAKE_CASE__ = True SCREAMING_SNAKE_CASE__ = False SCREAMING_SNAKE_CASE__ = 128 SCREAMING_SNAKE_CASE__ = None def __A (self ) -> Union[str, Any]: if self.trunk is None: _lowercase =TrunkConfig() elif isinstance(self.trunk , UpperCAmelCase ): _lowercase =TrunkConfig(**self.trunk ) def __A (self ) -> Tuple: _lowercase =asdict(self ) _lowercase =self.trunk.to_dict() return output @dataclass class lowerCamelCase__ : SCREAMING_SNAKE_CASE__ = 48 SCREAMING_SNAKE_CASE__ = 1024 SCREAMING_SNAKE_CASE__ = 128 SCREAMING_SNAKE_CASE__ = 32 SCREAMING_SNAKE_CASE__ = 32 SCREAMING_SNAKE_CASE__ = 32 SCREAMING_SNAKE_CASE__ = 0 SCREAMING_SNAKE_CASE__ = 0 SCREAMING_SNAKE_CASE__ = False SCREAMING_SNAKE_CASE__ = 4 SCREAMING_SNAKE_CASE__ = 128 SCREAMING_SNAKE_CASE__ = None def __A (self ) -> List[str]: if self.structure_module is None: _lowercase =StructureModuleConfig() elif isinstance(self.structure_module , UpperCAmelCase ): _lowercase =StructureModuleConfig(**self.structure_module ) if self.max_recycles <= 0: raise ValueError(f"`max_recycles` should be positive, got {self.max_recycles}." ) if self.sequence_state_dim % self.sequence_state_dim != 0: raise ValueError( '''`sequence_state_dim` should be a round multiple of `sequence_state_dim`, got''' f" {self.sequence_state_dim} and {self.sequence_state_dim}." ) if self.pairwise_state_dim % self.pairwise_state_dim != 0: raise ValueError( '''`pairwise_state_dim` should be a round multiple of `pairwise_state_dim`, got''' f" {self.pairwise_state_dim} and {self.pairwise_state_dim}." ) _lowercase =self.sequence_state_dim // self.sequence_head_width _lowercase =self.pairwise_state_dim // self.pairwise_head_width if self.sequence_state_dim != sequence_num_heads * self.sequence_head_width: raise ValueError( '''`sequence_state_dim` should be equal to `sequence_num_heads * sequence_head_width, got''' f" {self.sequence_state_dim} != {sequence_num_heads} * {self.sequence_head_width}." ) if self.pairwise_state_dim != pairwise_num_heads * self.pairwise_head_width: raise ValueError( '''`pairwise_state_dim` should be equal to `pairwise_num_heads * pairwise_head_width, got''' f" {self.pairwise_state_dim} != {pairwise_num_heads} * {self.pairwise_head_width}." ) if self.pairwise_state_dim % 2 != 0: raise ValueError(f"`pairwise_state_dim` should be even, got {self.pairwise_state_dim}." ) if self.dropout >= 0.4: raise ValueError(f"`dropout` should not be greater than 0.4, got {self.dropout}." ) def __A (self ) -> Dict: _lowercase =asdict(self ) _lowercase =self.structure_module.to_dict() return output @dataclass class lowerCamelCase__ : SCREAMING_SNAKE_CASE__ = 384 SCREAMING_SNAKE_CASE__ = 128 SCREAMING_SNAKE_CASE__ = 16 SCREAMING_SNAKE_CASE__ = 128 SCREAMING_SNAKE_CASE__ = 12 SCREAMING_SNAKE_CASE__ = 4 SCREAMING_SNAKE_CASE__ = 8 SCREAMING_SNAKE_CASE__ = 0.1 SCREAMING_SNAKE_CASE__ = 8 SCREAMING_SNAKE_CASE__ = 1 SCREAMING_SNAKE_CASE__ = 2 SCREAMING_SNAKE_CASE__ = 7 SCREAMING_SNAKE_CASE__ = 10 SCREAMING_SNAKE_CASE__ = 1E-8 SCREAMING_SNAKE_CASE__ = 1E5 def __A (self ) -> List[Any]: return asdict(self ) def UpperCAmelCase_ ( ) -> Tuple: """simple docstring""" return ( "<cls>", "<pad>", "<eos>", "<unk>", "L", "A", "G", "V", "S", "E", "R", "T", "I", "D", "P", "K", "Q", "N", "F", "Y", "M", "H", "W", "C", "X", "B", "U", "Z", "O", ".", "-", "<null_1>", "<mask>", )
5
1
from manim import * class lowerCamelCase__ ( lowerCAmelCase): def __A (self ) -> List[str]: _lowercase =Rectangle(height=0.5 , width=0.5 ) _lowercase =Rectangle(height=0.46 , width=0.46 ).set_stroke(width=0 ) _lowercase =Rectangle(height=0.25 , width=0.25 ) _lowercase =[mem.copy() for i in range(6 )] _lowercase =[mem.copy() for i in range(6 )] _lowercase =VGroup(*UpperCAmelCase ).arrange(UpperCAmelCase , buff=0 ) _lowercase =VGroup(*UpperCAmelCase ).arrange(UpperCAmelCase , buff=0 ) _lowercase =VGroup(UpperCAmelCase , UpperCAmelCase ).arrange(UpperCAmelCase , buff=0 ) _lowercase =Text('''CPU''' , font_size=2_4 ) _lowercase =Group(UpperCAmelCase , UpperCAmelCase ).arrange(UpperCAmelCase , buff=0.5 , aligned_edge=UpperCAmelCase ) cpu.move_to([-2.5, -0.5, 0] ) self.add(UpperCAmelCase ) _lowercase =[mem.copy() for i in range(4 )] _lowercase =VGroup(*UpperCAmelCase ).arrange(UpperCAmelCase , buff=0 ) _lowercase =Text('''GPU''' , font_size=2_4 ) _lowercase =Group(UpperCAmelCase , UpperCAmelCase ).arrange(UpperCAmelCase , buff=0.5 , aligned_edge=UpperCAmelCase ) gpu.move_to([-1, -1, 0] ) self.add(UpperCAmelCase ) _lowercase =[mem.copy() for i in range(6 )] _lowercase =VGroup(*UpperCAmelCase ).arrange(UpperCAmelCase , buff=0 ) _lowercase =Text('''Model''' , font_size=2_4 ) _lowercase =Group(UpperCAmelCase , UpperCAmelCase ).arrange(UpperCAmelCase , buff=0.5 , aligned_edge=UpperCAmelCase ) model.move_to([3, -1.0, 0] ) self.add(UpperCAmelCase ) _lowercase =[] _lowercase =[] for i, rect in enumerate(UpperCAmelCase ): _lowercase =fill.copy().set_fill(UpperCAmelCase , opacity=0.8 ) target.move_to(UpperCAmelCase ) model_arr.append(UpperCAmelCase ) _lowercase =Rectangle(height=0.46 , width=0.46 ).set_stroke(width=0.0 ).set_fill(UpperCAmelCase , opacity=0.8 ) cpu_target.move_to(cpu_left_col_base[i] ) model_cpu_arr.append(UpperCAmelCase ) self.add(*UpperCAmelCase , *UpperCAmelCase ) _lowercase =[meta_mem.copy() for i in range(6 )] _lowercase =[meta_mem.copy() for i in range(6 )] _lowercase =VGroup(*UpperCAmelCase ).arrange(UpperCAmelCase , buff=0 ) _lowercase =VGroup(*UpperCAmelCase ).arrange(UpperCAmelCase , buff=0 ) _lowercase =VGroup(UpperCAmelCase , UpperCAmelCase ).arrange(UpperCAmelCase , buff=0 ) _lowercase =Text('''Disk''' , font_size=2_4 ) _lowercase =Group(UpperCAmelCase , UpperCAmelCase ).arrange(UpperCAmelCase , buff=0.5 , aligned_edge=UpperCAmelCase ) disk.move_to([-4, -1.25, 0] ) self.add(UpperCAmelCase , UpperCAmelCase ) _lowercase =Square(side_length=2.2 ) key.move_to([-5, 2, 0] ) _lowercase =MarkupText( f"<b>Key:</b>\n\n<span fgcolor='{YELLOW}'>●</span> Empty Model" , font_size=1_8 , ) key_text.move_to([-5, 2.4, 0] ) self.add(UpperCAmelCase , UpperCAmelCase ) _lowercase =MarkupText( f"<span fgcolor='{BLUE}'>●</span> Checkpoint" , font_size=1_8 , ) blue_text.next_to(UpperCAmelCase , DOWN * 2.4 , aligned_edge=key_text.get_left() ) self.add(UpperCAmelCase ) _lowercase =MarkupText( f"Now watch as an input is passed through the model\nand how the memory is utilized and handled." , font_size=2_4 , ) step_a.move_to([2, 2, 0] ) self.play(Write(UpperCAmelCase ) ) _lowercase =Square(0.3 ) input.set_fill(UpperCAmelCase , opacity=1.0 ) input.set_stroke(width=0.0 ) input.next_to(model_base[0] , UpperCAmelCase , buff=0.5 ) self.play(Write(UpperCAmelCase ) ) input.generate_target() input.target.next_to(model_arr[0] , direction=UpperCAmelCase , buff=0.02 ) self.play(MoveToTarget(UpperCAmelCase ) ) self.play(FadeOut(UpperCAmelCase ) ) _lowercase =Arrow(start=UpperCAmelCase , end=UpperCAmelCase , color=UpperCAmelCase , buff=0.5 ) a.next_to(model_arr[0].get_left() , UpperCAmelCase , buff=0.2 ) model_cpu_arr[0].generate_target() model_cpu_arr[0].target.move_to(gpu_rect[0] ) _lowercase =MarkupText( f"As the input reaches a layer, the hook triggers\nand weights are moved from the CPU\nto the GPU and back." , font_size=2_4 , ) step_a.move_to([2, 2, 0] ) self.play(Write(UpperCAmelCase , run_time=3 ) ) _lowercase ={'''run_time''': 1, '''fade_in''': True, '''fade_out''': True, '''buff''': 0.02} self.play( Write(UpperCAmelCase ) , Circumscribe(model_arr[0] , color=UpperCAmelCase , **UpperCAmelCase ) , Circumscribe(model_cpu_arr[0] , color=UpperCAmelCase , **UpperCAmelCase ) , Circumscribe(gpu_rect[0] , color=UpperCAmelCase , **UpperCAmelCase ) , ) self.play(MoveToTarget(model_cpu_arr[0] ) ) _lowercase =a.copy() for i in range(6 ): a_c.next_to(model_arr[i].get_right() + 0.02 , UpperCAmelCase , buff=0.2 ) input.generate_target() input.target.move_to(model_arr[i].get_right() + 0.02 ) _lowercase =AnimationGroup( FadeOut(UpperCAmelCase , run_time=0.5 ) , MoveToTarget(UpperCAmelCase , run_time=0.5 ) , FadeIn(UpperCAmelCase , run_time=0.5 ) , lag_ratio=0.2 ) self.play(UpperCAmelCase ) model_cpu_arr[i].generate_target() model_cpu_arr[i].target.move_to(cpu_left_col_base[i] ) if i < 5: model_cpu_arr[i + 1].generate_target() model_cpu_arr[i + 1].target.move_to(gpu_rect[0] ) if i >= 1: _lowercase =0.7 self.play( Circumscribe(model_arr[i] , **UpperCAmelCase ) , Circumscribe(cpu_left_col_base[i] , **UpperCAmelCase ) , Circumscribe(cpu_left_col_base[i + 1] , color=UpperCAmelCase , **UpperCAmelCase ) , Circumscribe(gpu_rect[0] , color=UpperCAmelCase , **UpperCAmelCase ) , Circumscribe(model_arr[i + 1] , color=UpperCAmelCase , **UpperCAmelCase ) , ) if i < 1: self.play( MoveToTarget(model_cpu_arr[i] ) , MoveToTarget(model_cpu_arr[i + 1] ) , ) else: self.play( MoveToTarget(model_cpu_arr[i] , run_time=0.7 ) , MoveToTarget(model_cpu_arr[i + 1] , run_time=0.7 ) , ) else: model_cpu_arr[i].generate_target() model_cpu_arr[i].target.move_to(cpu_left_col_base[-1] ) input.generate_target() input.target.next_to(model_arr[-1].get_right() , RIGHT + 0.02 , buff=0.2 ) self.play( Circumscribe(model_arr[-1] , color=UpperCAmelCase , **UpperCAmelCase ) , Circumscribe(cpu_left_col_base[-1] , color=UpperCAmelCase , **UpperCAmelCase ) , Circumscribe(gpu_rect[0] , color=UpperCAmelCase , **UpperCAmelCase ) , ) self.play(MoveToTarget(model_cpu_arr[i] ) ) _lowercase =a_c _lowercase =a_c.copy() input.generate_target() input.target.next_to(model_base[-1] , RIGHT + 0.02 , buff=0.5 ) self.play( FadeOut(UpperCAmelCase ) , FadeOut(UpperCAmelCase , run_time=0.5 ) , ) _lowercase =MarkupText(f"Inference on a model too large for GPU memory\nis successfully completed." , font_size=2_4 ) step_a.move_to([2, 2, 0] ) self.play(Write(UpperCAmelCase , run_time=3 ) , MoveToTarget(UpperCAmelCase ) ) self.wait()
5
import json import multiprocessing import os import re from collections import defaultdict import torch from accelerate import Accelerator from accelerate.utils import set_seed from arguments import HumanEvalArguments from datasets import load_dataset, load_metric from torch.utils.data import IterableDataset from torch.utils.data.dataloader import DataLoader from tqdm import tqdm import transformers from transformers import AutoModelForCausalLM, AutoTokenizer, HfArgumentParser, StoppingCriteria, StoppingCriteriaList UpperCAmelCase__ = ['''\nclass''', '''\ndef''', '''\n#''', '''\n@''', '''\nprint''', '''\nif'''] class lowerCamelCase__ ( lowerCAmelCase): def __init__(self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase=None , UpperCAmelCase=1 ) -> Dict: _lowercase =tokenizer _lowercase =dataset _lowercase =len(UpperCAmelCase ) if n_tasks is None else n_tasks _lowercase =n_copies def __iter__(self ) -> Optional[Any]: _lowercase =[] for task in range(self.n_tasks ): # without strip, the model generate commented codes ... prompts.append(self.tokenizer.eos_token + self.dataset[task]['''prompt'''].strip() ) _lowercase =self.tokenizer(UpperCAmelCase , padding=UpperCAmelCase , return_tensors='''pt''' ) for task in range(self.n_tasks ): for _ in range(self.n_copies ): yield { "ids": outputs.input_ids[task], "task_id": task, "input_len": outputs.attention_mask[task].sum(), } class lowerCamelCase__ ( lowerCAmelCase): def __init__(self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) -> Optional[Any]: _lowercase =start_length _lowercase =eof_strings _lowercase =tokenizer def __call__(self , UpperCAmelCase , UpperCAmelCase , **UpperCAmelCase ) -> Dict: _lowercase =self.tokenizer.batch_decode(input_ids[:, self.start_length :] ) _lowercase =[] for decoded_generation in decoded_generations: done.append(any(stop_string in decoded_generation for stop_string in self.eof_strings ) ) return all(UpperCAmelCase ) def UpperCAmelCase_ ( __snake_case ) -> Optional[Any]: """simple docstring""" _lowercase =re.split('''(%s)''' % '''|'''.join(__snake_case ) , __snake_case ) # last string should be "" return "".join(string_list[:-2] ) def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , __snake_case=20 , **__snake_case ) -> Tuple: """simple docstring""" _lowercase =defaultdict(__snake_case ) # dict of list of generated tokens for step, batch in tqdm(enumerate(__snake_case ) ): with torch.no_grad(): _lowercase =batch['''ids'''].shape[-1] _lowercase =accelerator.unwrap_model(__snake_case ).generate( input_ids=batch['''ids'''][:, : batch['''input_len''']] , num_return_sequences=__snake_case , **__snake_case ) # each task is generated batch_size times _lowercase =batch['''task_id'''].repeat(__snake_case ) _lowercase =accelerator.pad_across_processes( __snake_case , dim=1 , pad_index=tokenizer.pad_token_id ) _lowercase , _lowercase =accelerator.gather((generated_tokens, generated_tasks) ) _lowercase =generated_tokens.cpu().numpy() _lowercase =generated_tasks.cpu().numpy() for task, generated_tokens in zip(__snake_case , __snake_case ): gen_token_dict[task].append(__snake_case ) _lowercase =[[] for _ in range(__snake_case )] for task, generated_tokens in gen_token_dict.items(): for s in generated_tokens: _lowercase =tokenizer.decode(__snake_case , skip_special_tokens=__snake_case , clean_up_tokenization_spaces=__snake_case ) code_gens[task].append(remove_last_block(__snake_case ) ) return code_gens def UpperCAmelCase_ ( ) -> str: """simple docstring""" _lowercase =HfArgumentParser(__snake_case ) _lowercase =parser.parse_args() transformers.logging.set_verbosity_error() # enables code execution in code_eval metric _lowercase =args.HF_ALLOW_CODE_EVAL # make sure tokenizer plays nice with multiprocessing _lowercase ='''false''' if args.num_workers is None: _lowercase =multiprocessing.cpu_count() # Use dataset load to feed to accelerate _lowercase =Accelerator() set_seed(args.seed , device_specific=__snake_case ) # Load model and tokenizer _lowercase =AutoTokenizer.from_pretrained(args.model_ckpt ) _lowercase =tokenizer.eos_token _lowercase =AutoModelForCausalLM.from_pretrained(args.model_ckpt ) # Generation settings _lowercase ={ '''do_sample''': args.do_sample, '''temperature''': args.temperature, '''max_new_tokens''': args.max_new_tokens, '''top_p''': args.top_p, '''top_k''': args.top_k, '''stopping_criteria''': StoppingCriteriaList([EndOfFunctionCriteria(0 , __snake_case , __snake_case )] ), } # Load evaluation dataset and metric _lowercase =load_dataset('''openai_humaneval''' ) _lowercase =load_metric('''code_eval''' ) _lowercase =args.num_tasks if args.num_tasks is not None else len(human_eval['''test'''] ) _lowercase =args.n_samples // args.batch_size _lowercase =TokenizedDataset(__snake_case , human_eval['''test'''] , n_copies=__snake_case , n_tasks=__snake_case ) # do not confuse args.batch_size, which is actually the num_return_sequences _lowercase =DataLoader(__snake_case , batch_size=1 ) # Run a quick test to see if code evaluation is enabled try: _lowercase =code_eval_metric.compute(references=[''''''] , predictions=[['''''']] ) except ValueError as exception: print( '''Code evaluation not enabled. Read the warning below carefully and then use `--HF_ALLOW_CODE_EVAL="1"`''' ''' flag to enable code evaluation.''' ) raise exception _lowercase , _lowercase =accelerator.prepare(__snake_case , __snake_case ) _lowercase =complete_code( __snake_case , __snake_case , __snake_case , __snake_case , n_tasks=__snake_case , batch_size=args.batch_size , **__snake_case , ) if accelerator.is_main_process: _lowercase =[] for task in tqdm(range(__snake_case ) ): _lowercase =human_eval['''test'''][task]['''test'''] _lowercase =F"check({human_eval['test'][task]['entry_point']})" references.append('''\n''' + test_func + '''\n''' + entry_point ) # Evaluate completions with "code_eval" metric _lowercase , _lowercase =code_eval_metric.compute( references=__snake_case , predictions=__snake_case , num_workers=args.num_workers ) print(F"Results: {pass_at_k}" ) # Save results to json file with open(args.output_file , '''w''' ) as fp: json.dump(__snake_case , __snake_case ) # For some reason the folliwng seems to be necessary sometimes for code_eval to work nice with multiprocessing # https://stackoverflow.com/questions/60804599/python-multiprocessing-keeps-spawning-the-whole-script if __name__ == "__main__": main()
5
1
import itertools from dataclasses import dataclass from typing import List, Optional import pyarrow as pa import pyarrow.parquet as pq import datasets from datasets.table import table_cast UpperCAmelCase__ = datasets.utils.logging.get_logger(__name__) @dataclass class lowerCamelCase__ ( datasets.BuilderConfig): SCREAMING_SNAKE_CASE__ = 10000 SCREAMING_SNAKE_CASE__ = None SCREAMING_SNAKE_CASE__ = None class lowerCamelCase__ ( datasets.ArrowBasedBuilder): SCREAMING_SNAKE_CASE__ = ParquetConfig def __A (self ) -> Union[str, Any]: return datasets.DatasetInfo(features=self.config.features ) def __A (self , UpperCAmelCase ) -> List[str]: if not self.config.data_files: raise ValueError(f"At least one data file must be specified, but got data_files={self.config.data_files}" ) _lowercase =dl_manager.download_and_extract(self.config.data_files ) if isinstance(UpperCAmelCase , (str, list, tuple) ): _lowercase =data_files if isinstance(UpperCAmelCase , UpperCAmelCase ): _lowercase =[files] # Use `dl_manager.iter_files` to skip hidden files in an extracted archive _lowercase =[dl_manager.iter_files(UpperCAmelCase ) for file in files] return [datasets.SplitGenerator(name=datasets.Split.TRAIN , gen_kwargs={'''files''': files} )] _lowercase =[] for split_name, files in data_files.items(): if isinstance(UpperCAmelCase , UpperCAmelCase ): _lowercase =[files] # Use `dl_manager.iter_files` to skip hidden files in an extracted archive _lowercase =[dl_manager.iter_files(UpperCAmelCase ) for file in files] # Infer features is they are stoed in the arrow schema if self.info.features is None: for file in itertools.chain.from_iterable(UpperCAmelCase ): with open(UpperCAmelCase , '''rb''' ) as f: _lowercase =datasets.Features.from_arrow_schema(pq.read_schema(UpperCAmelCase ) ) break splits.append(datasets.SplitGenerator(name=UpperCAmelCase , gen_kwargs={'''files''': files} ) ) return splits def __A (self , UpperCAmelCase ) -> pa.Table: if self.info.features is not None: # more expensive cast to support nested features with keys in a different order # allows str <-> int/float or str to Audio for example _lowercase =table_cast(UpperCAmelCase , self.info.features.arrow_schema ) return pa_table def __A (self , UpperCAmelCase ) -> Tuple: _lowercase =self.info.features.arrow_schema if self.info.features is not None else None if self.info.features is not None and self.config.columns is not None: if sorted(field.name for field in schema ) != sorted(self.config.columns ): raise ValueError( f"Tried to load parquet data with columns '{self.config.columns}' with mismatching features '{self.info.features}'" ) for file_idx, file in enumerate(itertools.chain.from_iterable(UpperCAmelCase ) ): with open(UpperCAmelCase , '''rb''' ) as f: _lowercase =pq.ParquetFile(UpperCAmelCase ) try: for batch_idx, record_batch in enumerate( parquet_file.iter_batches(batch_size=self.config.batch_size , columns=self.config.columns ) ): _lowercase =pa.Table.from_batches([record_batch] ) # Uncomment for debugging (will print the Arrow table size and elements) # logger.warning(f"pa_table: {pa_table} num rows: {pa_table.num_rows}") # logger.warning('\n'.join(str(pa_table.slice(i, 1).to_pydict()) for i in range(pa_table.num_rows))) yield f"{file_idx}_{batch_idx}", self._cast_table(UpperCAmelCase ) except ValueError as e: logger.error(f"Failed to read file '{file}' with error {type(UpperCAmelCase )}: {e}" ) raise
5
UpperCAmelCase__ = 8.31_44_62 # Unit - J mol-1 K-1 def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case ) -> float: """simple docstring""" if moles < 0 or kelvin < 0 or volume < 0: raise ValueError('''Invalid inputs. Enter positive value.''' ) return moles * kelvin * UNIVERSAL_GAS_CONSTANT / volume def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case ) -> float: """simple docstring""" if moles < 0 or kelvin < 0 or pressure < 0: raise ValueError('''Invalid inputs. Enter positive value.''' ) return moles * kelvin * UNIVERSAL_GAS_CONSTANT / pressure if __name__ == "__main__": from doctest import testmod testmod()
5
1
from typing import Any, Dict, List, Optional, Tuple, Union import torch from torch import nn from torch.utils.data import DistributedSampler, RandomSampler from transformers import PreTrainedModel, Trainer, logging from transformers.integrations import is_fairscale_available from transformers.models.fsmt.configuration_fsmt import FSMTConfig from transformers.optimization import ( Adafactor, AdamW, get_constant_schedule, get_constant_schedule_with_warmup, get_cosine_schedule_with_warmup, get_cosine_with_hard_restarts_schedule_with_warmup, get_linear_schedule_with_warmup, get_polynomial_decay_schedule_with_warmup, ) from transformers.trainer_pt_utils import get_tpu_sampler from transformers.training_args import ParallelMode from transformers.utils import is_torch_tpu_available if is_fairscale_available(): from fairscale.optim import OSS UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = { '''linear''': get_linear_schedule_with_warmup, '''cosine''': get_cosine_schedule_with_warmup, '''cosine_w_restarts''': get_cosine_with_hard_restarts_schedule_with_warmup, '''polynomial''': get_polynomial_decay_schedule_with_warmup, '''constant''': get_constant_schedule, '''constant_w_warmup''': get_constant_schedule_with_warmup, } class lowerCamelCase__ ( lowerCAmelCase): def __init__(self , UpperCAmelCase=None , UpperCAmelCase=None , *UpperCAmelCase , **UpperCAmelCase ) -> Union[str, Any]: super().__init__(*UpperCAmelCase , **UpperCAmelCase ) if config is None: assert isinstance(self.model , UpperCAmelCase ), ( "If no `config` is passed the model to be trained has to be of type `PreTrainedModel`, but is" f" {self.model.__class__}" ) _lowercase =self.model.config else: _lowercase =config _lowercase =data_args _lowercase =self.config.tgt_vocab_size if isinstance(self.config , UpperCAmelCase ) else self.config.vocab_size if self.args.label_smoothing != 0 or (self.data_args is not None and self.data_args.ignore_pad_token_for_loss): assert self.config.pad_token_id is not None, ( "Make sure that `config.pad_token_id` is correcly defined when ignoring `pad_token` for loss" " calculation or doing label smoothing." ) if self.config.pad_token_id is None and self.config.eos_token_id is not None: logger.warning( f"The `config.pad_token_id` is `None`. Using `config.eos_token_id` = {self.config.eos_token_id} for" ''' padding..''' ) if self.args.label_smoothing == 0: _lowercase =torch.nn.CrossEntropyLoss(ignore_index=self.config.pad_token_id ) else: # dynamically import label_smoothed_nll_loss from utils import label_smoothed_nll_loss _lowercase =label_smoothed_nll_loss def __A (self , UpperCAmelCase ) -> int: if self.optimizer is None: _lowercase =['''bias''', '''LayerNorm.weight'''] _lowercase =[ { '''params''': [p for n, p in self.model.named_parameters() if not any(nd in n for nd in no_decay )], '''weight_decay''': self.args.weight_decay, }, { '''params''': [p for n, p in self.model.named_parameters() if any(nd in n for nd in no_decay )], '''weight_decay''': 0.0, }, ] _lowercase =Adafactor if self.args.adafactor else AdamW if self.args.adafactor: _lowercase =Adafactor _lowercase ={'''scale_parameter''': False, '''relative_step''': False} else: _lowercase =AdamW _lowercase ={ '''betas''': (self.args.adam_betaa, self.args.adam_betaa), '''eps''': self.args.adam_epsilon, } _lowercase =self.args.learning_rate if self.sharded_ddp: _lowercase =OSS( params=UpperCAmelCase , optim=UpperCAmelCase , **UpperCAmelCase , ) else: _lowercase =optimizer_cls(UpperCAmelCase , **UpperCAmelCase ) if self.lr_scheduler is None: _lowercase =self._get_lr_scheduler(UpperCAmelCase ) else: # ignoring --lr_scheduler logger.warning('''scheduler is passed to `Seq2SeqTrainer`, `--lr_scheduler` arg is ignored.''' ) def __A (self , UpperCAmelCase ) -> int: _lowercase =arg_to_scheduler[self.args.lr_scheduler] if self.args.lr_scheduler == "constant": _lowercase =schedule_func(self.optimizer ) elif self.args.lr_scheduler == "constant_w_warmup": _lowercase =schedule_func(self.optimizer , num_warmup_steps=self.args.warmup_steps ) else: _lowercase =schedule_func( self.optimizer , num_warmup_steps=self.args.warmup_steps , num_training_steps=UpperCAmelCase ) return scheduler def __A (self ) -> Optional[torch.utils.data.Sampler]: if isinstance(self.train_dataset , torch.utils.data.IterableDataset ): return None elif is_torch_tpu_available(): return get_tpu_sampler(self.train_dataset ) else: if self.args.sortish_sampler: self.train_dataset.make_sortish_sampler( self.args.per_device_train_batch_size , distributed=(self.args.parallel_mode == ParallelMode.DISTRIBUTED) , ) return ( RandomSampler(self.train_dataset ) if self.args.local_rank == -1 else DistributedSampler(self.train_dataset ) ) def __A (self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) -> List[str]: if self.args.label_smoothing == 0: if self.data_args is not None and self.data_args.ignore_pad_token_for_loss: # force training to ignore pad token _lowercase =model(**UpperCAmelCase , use_cache=UpperCAmelCase )[0] _lowercase =self.loss_fn(logits.view(-1 , logits.shape[-1] ) , labels.view(-1 ) ) else: # compute usual loss via models _lowercase , _lowercase =model(**UpperCAmelCase , labels=UpperCAmelCase , use_cache=UpperCAmelCase )[:2] else: # compute label smoothed loss _lowercase =model(**UpperCAmelCase , use_cache=UpperCAmelCase )[0] _lowercase =torch.nn.functional.log_softmax(UpperCAmelCase , dim=-1 ) _lowercase , _lowercase =self.loss_fn(UpperCAmelCase , UpperCAmelCase , self.args.label_smoothing , ignore_index=self.config.pad_token_id ) return loss, logits def __A (self , UpperCAmelCase , UpperCAmelCase ) -> str: _lowercase =inputs.pop('''labels''' ) _lowercase , _lowercase =self._compute_loss(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) return loss def __A (self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = None , ) -> Tuple[Optional[float], Optional[torch.Tensor], Optional[torch.Tensor]]: _lowercase =self._prepare_inputs(UpperCAmelCase ) _lowercase ={ '''max_length''': self.data_args.val_max_target_length if self.data_args is not None else self.config.max_length, '''num_beams''': self.data_args.eval_beams if self.data_args is not None else self.config.num_beams, } if self.args.predict_with_generate and not self.args.prediction_loss_only: _lowercase =self.model.generate( inputs['''input_ids'''] , attention_mask=inputs['''attention_mask'''] , **UpperCAmelCase , ) # in case the batch is shorter than max length, the output should be padded if generated_tokens.shape[-1] < gen_kwargs["max_length"]: _lowercase =self._pad_tensors_to_max_len(UpperCAmelCase , gen_kwargs['''max_length'''] ) _lowercase =inputs.pop('''labels''' ) with torch.no_grad(): # compute loss on predict data _lowercase , _lowercase =self._compute_loss(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) _lowercase =loss.mean().detach() if self.args.prediction_loss_only: return (loss, None, None) _lowercase =generated_tokens if self.args.predict_with_generate else logits if labels.shape[-1] < gen_kwargs["max_length"]: _lowercase =self._pad_tensors_to_max_len(UpperCAmelCase , gen_kwargs['''max_length'''] ) return (loss, logits, labels) def __A (self , UpperCAmelCase , UpperCAmelCase ) -> Optional[Any]: # If PAD token is not defined at least EOS token has to be defined _lowercase =self.config.pad_token_id if self.config.pad_token_id is not None else self.config.eos_token_id if pad_token_id is None: raise ValueError( '''Make sure that either `config.pad_token_id` or `config.eos_token_id` is defined if tensor has to be''' f" padded to `max_length`={max_length}" ) _lowercase =pad_token_id * torch.ones( (tensor.shape[0], max_length) , dtype=tensor.dtype , device=tensor.device ) _lowercase =tensor return padded_tensor
5
from __future__ import annotations from collections.abc import Callable UpperCAmelCase__ = list[list[float | int]] def UpperCAmelCase_ ( __snake_case , __snake_case ) -> Matrix: """simple docstring""" _lowercase =len(__snake_case ) _lowercase =[[0 for _ in range(size + 1 )] for _ in range(__snake_case )] _lowercase =42 _lowercase =42 _lowercase =42 _lowercase =42 _lowercase =42 _lowercase =42 for row in range(__snake_case ): for col in range(__snake_case ): _lowercase =matrix[row][col] _lowercase =vector[row][0] _lowercase =0 _lowercase =0 while row < size and col < size: # pivoting _lowercase =max((abs(augmented[rowa][col] ), rowa) for rowa in range(__snake_case , __snake_case ) )[ 1 ] if augmented[pivot_row][col] == 0: col += 1 continue else: _lowercase , _lowercase =augmented[pivot_row], augmented[row] for rowa in range(row + 1 , __snake_case ): _lowercase =augmented[rowa][col] / augmented[row][col] _lowercase =0 for cola in range(col + 1 , size + 1 ): augmented[rowa][cola] -= augmented[row][cola] * ratio row += 1 col += 1 # back substitution for col in range(1 , __snake_case ): for row in range(__snake_case ): _lowercase =augmented[row][col] / augmented[col][col] for cola in range(__snake_case , size + 1 ): augmented[row][cola] -= augmented[col][cola] * ratio # round to get rid of numbers like 2.000000000000004 return [ [round(augmented[row][size] / augmented[row][row] , 10 )] for row in range(__snake_case ) ] def UpperCAmelCase_ ( __snake_case ) -> Callable[[int], int]: """simple docstring""" _lowercase =len(__snake_case ) _lowercase =[[0 for _ in range(__snake_case )] for _ in range(__snake_case )] _lowercase =[[0] for _ in range(__snake_case )] _lowercase =42 _lowercase =42 _lowercase =42 _lowercase =42 for x_val, y_val in enumerate(__snake_case ): for col in range(__snake_case ): _lowercase =(x_val + 1) ** (size - col - 1) _lowercase =y_val _lowercase =solve(__snake_case , __snake_case ) def interpolated_func(__snake_case ) -> int: return sum( round(coeffs[x_val][0] ) * (var ** (size - x_val - 1)) for x_val in range(__snake_case ) ) return interpolated_func def UpperCAmelCase_ ( __snake_case ) -> int: """simple docstring""" return ( 1 - variable + variable**2 - variable**3 + variable**4 - variable**5 + variable**6 - variable**7 + variable**8 - variable**9 + variable**10 ) def UpperCAmelCase_ ( __snake_case = question_function , __snake_case = 10 ) -> int: """simple docstring""" _lowercase =[func(__snake_case ) for x_val in range(1 , order + 1 )] _lowercase =[ interpolate(data_points[:max_coeff] ) for max_coeff in range(1 , order + 1 ) ] _lowercase =0 _lowercase =42 _lowercase =42 for poly in polynomials: _lowercase =1 while func(__snake_case ) == poly(__snake_case ): x_val += 1 ret += poly(__snake_case ) return ret if __name__ == "__main__": print(f'''{solution() = }''')
5
1
import inspect import unittest import numpy as np from tests.test_modeling_common import floats_tensor from transformers import MaskaFormerConfig, 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 MaskaFormerForUniversalSegmentation, MaskaFormerModel if is_vision_available(): from transformers import MaskaFormerImageProcessor if is_vision_available(): from PIL import Image class lowerCamelCase__ : def __init__(self , UpperCAmelCase , UpperCAmelCase=2 , UpperCAmelCase=True , UpperCAmelCase=False , UpperCAmelCase=1_0 , UpperCAmelCase=3 , UpperCAmelCase=3_2 * 8 , UpperCAmelCase=3_2 * 8 , UpperCAmelCase=4 , UpperCAmelCase=6_4 , ) -> List[Any]: _lowercase =parent _lowercase =batch_size _lowercase =is_training _lowercase =use_auxiliary_loss _lowercase =num_queries _lowercase =num_channels _lowercase =min_size _lowercase =max_size _lowercase =num_labels _lowercase =hidden_dim _lowercase =hidden_dim def __A (self ) -> int: _lowercase =floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size] ).to( UpperCAmelCase ) _lowercase =torch.ones([self.batch_size, self.min_size, self.max_size] , device=UpperCAmelCase ) _lowercase =( torch.rand([self.batch_size, self.num_labels, self.min_size, self.max_size] , device=UpperCAmelCase ) > 0.5 ).float() _lowercase =(torch.rand((self.batch_size, self.num_labels) , device=UpperCAmelCase ) > 0.5).long() _lowercase =self.get_config() return config, pixel_values, pixel_mask, mask_labels, class_labels def __A (self ) -> int: _lowercase =MaskaFormerConfig( hidden_size=self.hidden_dim , ) _lowercase =self.num_queries _lowercase =self.num_labels _lowercase =[1, 1, 1, 1] _lowercase =self.num_channels _lowercase =6_4 _lowercase =1_2_8 _lowercase =self.hidden_dim _lowercase =self.hidden_dim _lowercase =self.hidden_dim return config def __A (self ) -> Tuple: _lowercase , _lowercase , _lowercase , _lowercase , _lowercase =self.prepare_config_and_inputs() _lowercase ={'''pixel_values''': pixel_values, '''pixel_mask''': pixel_mask} return config, inputs_dict def __A (self , UpperCAmelCase , UpperCAmelCase ) -> str: _lowercase =output.encoder_hidden_states _lowercase =output.pixel_decoder_hidden_states _lowercase =output.transformer_decoder_hidden_states self.parent.assertTrue(len(UpperCAmelCase ) , len(config.backbone_config.depths ) ) self.parent.assertTrue(len(UpperCAmelCase ) , len(config.backbone_config.depths ) ) self.parent.assertTrue(len(UpperCAmelCase ) , config.decoder_layers ) def __A (self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase=False ) -> Any: with torch.no_grad(): _lowercase =MaskaFormerModel(config=UpperCAmelCase ) model.to(UpperCAmelCase ) model.eval() _lowercase =model(pixel_values=UpperCAmelCase , pixel_mask=UpperCAmelCase ) _lowercase =model(UpperCAmelCase , output_hidden_states=UpperCAmelCase ) self.parent.assertEqual( output.transformer_decoder_last_hidden_state.shape , (self.batch_size, self.num_queries, self.hidden_dim) , ) # 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(UpperCAmelCase , UpperCAmelCase ) def __A (self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) -> Any: _lowercase =MaskaFormerForUniversalSegmentation(config=UpperCAmelCase ) model.to(UpperCAmelCase ) model.eval() def comm_check_on_output(UpperCAmelCase ): # 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(): _lowercase =model(pixel_values=UpperCAmelCase , pixel_mask=UpperCAmelCase ) _lowercase =model(UpperCAmelCase ) comm_check_on_output(UpperCAmelCase ) _lowercase =model( pixel_values=UpperCAmelCase , pixel_mask=UpperCAmelCase , mask_labels=UpperCAmelCase , class_labels=UpperCAmelCase ) comm_check_on_output(UpperCAmelCase ) self.parent.assertTrue(result.loss is not None ) self.parent.assertEqual(result.loss.shape , torch.Size([1] ) ) @require_torch class lowerCamelCase__ ( lowerCAmelCase , lowerCAmelCase , unittest.TestCase): SCREAMING_SNAKE_CASE__ = (MaskaFormerModel, MaskaFormerForUniversalSegmentation) if is_torch_available() else () SCREAMING_SNAKE_CASE__ = {'''feature-extraction''': MaskaFormerModel} if is_torch_available() else {} SCREAMING_SNAKE_CASE__ = False SCREAMING_SNAKE_CASE__ = False SCREAMING_SNAKE_CASE__ = False SCREAMING_SNAKE_CASE__ = False def __A (self ) -> Dict: _lowercase =MaskaFormerModelTester(self ) _lowercase =ConfigTester(self , config_class=UpperCAmelCase , has_text_modality=UpperCAmelCase ) def __A (self ) -> List[Any]: self.config_tester.run_common_tests() def __A (self ) -> Union[str, Any]: _lowercase , _lowercase =self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.create_and_check_maskaformer_model(UpperCAmelCase , **UpperCAmelCase , output_hidden_states=UpperCAmelCase ) def __A (self ) -> str: _lowercase =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_maskaformer_instance_segmentation_head_model(*UpperCAmelCase ) @unittest.skip(reason='''Mask2Former does not use inputs_embeds''' ) def __A (self ) -> Dict: pass @unittest.skip(reason='''Mask2Former does not have a get_input_embeddings method''' ) def __A (self ) -> int: pass @unittest.skip(reason='''Mask2Former is not a generative model''' ) def __A (self ) -> Tuple: pass @unittest.skip(reason='''Mask2Former does not use token embeddings''' ) def __A (self ) -> Any: pass @require_torch_multi_gpu @unittest.skip( reason='''Mask2Former has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`''' ) def __A (self ) -> List[Any]: pass @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.''' ) def __A (self ) -> List[str]: pass def __A (self ) -> List[str]: _lowercase , _lowercase =self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _lowercase =model_class(UpperCAmelCase ) _lowercase =inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _lowercase =[*signature.parameters.keys()] _lowercase =['''pixel_values'''] self.assertListEqual(arg_names[:1] , UpperCAmelCase ) @slow def __A (self ) -> Tuple: for model_name in ["facebook/mask2former-swin-small-coco-instance"]: _lowercase =MaskaFormerModel.from_pretrained(UpperCAmelCase ) self.assertIsNotNone(UpperCAmelCase ) def __A (self ) -> Optional[Any]: _lowercase =(self.model_tester.min_size,) * 2 _lowercase ={ '''pixel_values''': torch.randn((2, 3, *size) , device=UpperCAmelCase ), '''mask_labels''': torch.randn((2, 1_0, *size) , device=UpperCAmelCase ), '''class_labels''': torch.zeros(2 , 1_0 , device=UpperCAmelCase ).long(), } _lowercase =self.model_tester.get_config() _lowercase =MaskaFormerForUniversalSegmentation(UpperCAmelCase ).to(UpperCAmelCase ) _lowercase =model(**UpperCAmelCase ) self.assertTrue(outputs.loss is not None ) def __A (self ) -> Optional[Any]: _lowercase , _lowercase =self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.create_and_check_maskaformer_model(UpperCAmelCase , **UpperCAmelCase , output_hidden_states=UpperCAmelCase ) def __A (self ) -> Dict: _lowercase , _lowercase =self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _lowercase =model_class(UpperCAmelCase ).to(UpperCAmelCase ) _lowercase =model(**UpperCAmelCase , output_attentions=UpperCAmelCase ) self.assertTrue(outputs.attentions is not None ) def __A (self ) -> Any: if not self.model_tester.is_training: return _lowercase =self.all_model_classes[1] _lowercase , _lowercase , _lowercase , _lowercase , _lowercase =self.model_tester.prepare_config_and_inputs() _lowercase =model_class(UpperCAmelCase ) model.to(UpperCAmelCase ) model.train() _lowercase =model(UpperCAmelCase , mask_labels=UpperCAmelCase , class_labels=UpperCAmelCase ).loss loss.backward() def __A (self ) -> Dict: _lowercase =self.all_model_classes[1] _lowercase , _lowercase , _lowercase , _lowercase , _lowercase =self.model_tester.prepare_config_and_inputs() _lowercase =True _lowercase =True _lowercase =model_class(UpperCAmelCase ).to(UpperCAmelCase ) model.train() _lowercase =model(UpperCAmelCase , mask_labels=UpperCAmelCase , class_labels=UpperCAmelCase ) _lowercase =outputs.encoder_hidden_states[0] encoder_hidden_states.retain_grad() _lowercase =outputs.pixel_decoder_hidden_states[0] pixel_decoder_hidden_states.retain_grad() _lowercase =outputs.transformer_decoder_hidden_states[0] transformer_decoder_hidden_states.retain_grad() _lowercase =outputs.attentions[0] attentions.retain_grad() outputs.loss.backward(retain_graph=UpperCAmelCase ) self.assertIsNotNone(encoder_hidden_states.grad ) self.assertIsNotNone(pixel_decoder_hidden_states.grad ) self.assertIsNotNone(transformer_decoder_hidden_states.grad ) self.assertIsNotNone(attentions.grad ) UpperCAmelCase__ = 1e-4 def UpperCAmelCase_ ( ) -> Optional[Any]: """simple docstring""" _lowercase =Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_vision @slow class lowerCamelCase__ ( unittest.TestCase): @cached_property def __A (self ) -> List[str]: return "facebook/mask2former-swin-small-coco-instance" @cached_property def __A (self ) -> Tuple: return MaskaFormerImageProcessor.from_pretrained(self.model_checkpoints ) if is_vision_available() else None def __A (self ) -> str: _lowercase =MaskaFormerModel.from_pretrained(self.model_checkpoints ).to(UpperCAmelCase ) _lowercase =self.default_image_processor _lowercase =prepare_img() _lowercase =image_processor(UpperCAmelCase , return_tensors='''pt''' ).to(UpperCAmelCase ) _lowercase =inputs['''pixel_values'''].shape # check size is divisible by 32 self.assertTrue((inputs_shape[-1] % 3_2) == 0 and (inputs_shape[-2] % 3_2) == 0 ) # check size self.assertEqual(UpperCAmelCase , (1, 3, 3_8_4, 3_8_4) ) with torch.no_grad(): _lowercase =model(**UpperCAmelCase ) _lowercase =torch.tensor( [[-0.2790, -1.0717, -1.1668], [-0.5128, -0.3128, -0.4987], [-0.5832, 0.1971, -0.0197]] ).to(UpperCAmelCase ) self.assertTrue( torch.allclose( outputs.encoder_last_hidden_state[0, 0, :3, :3] , UpperCAmelCase , atol=UpperCAmelCase ) ) _lowercase =torch.tensor( [[0.8973, 1.1847, 1.1776], [1.1934, 1.5040, 1.5128], [1.1153, 1.4486, 1.4951]] ).to(UpperCAmelCase ) self.assertTrue( torch.allclose( outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3] , UpperCAmelCase , atol=UpperCAmelCase ) ) _lowercase =torch.tensor( [[2.1152, 1.7000, -0.8603], [1.5808, 1.8004, -0.9353], [1.6043, 1.7495, -0.5999]] ).to(UpperCAmelCase ) self.assertTrue( torch.allclose( outputs.transformer_decoder_last_hidden_state[0, :3, :3] , UpperCAmelCase , atol=UpperCAmelCase ) ) def __A (self ) -> Tuple: _lowercase =MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints ).to(UpperCAmelCase ).eval() _lowercase =self.default_image_processor _lowercase =prepare_img() _lowercase =image_processor(UpperCAmelCase , return_tensors='''pt''' ).to(UpperCAmelCase ) _lowercase =inputs['''pixel_values'''].shape # check size is divisible by 32 self.assertTrue((inputs_shape[-1] % 3_2) == 0 and (inputs_shape[-2] % 3_2) == 0 ) # check size self.assertEqual(UpperCAmelCase , (1, 3, 3_8_4, 3_8_4) ) with torch.no_grad(): _lowercase =model(**UpperCAmelCase ) # masks_queries_logits _lowercase =outputs.masks_queries_logits self.assertEqual( masks_queries_logits.shape , (1, model.config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4) ) _lowercase =[ [-8.7839, -9.0056, -8.8121], [-7.4104, -7.0313, -6.5401], [-6.6105, -6.3427, -6.4675], ] _lowercase =torch.tensor(UpperCAmelCase ).to(UpperCAmelCase ) self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3] , UpperCAmelCase , atol=UpperCAmelCase ) ) # class_queries_logits _lowercase =outputs.class_queries_logits self.assertEqual(class_queries_logits.shape , (1, model.config.num_queries, model.config.num_labels + 1) ) _lowercase =torch.tensor( [ [1.8324, -8.0835, -4.1922], [0.8450, -9.0050, -3.6053], [0.3045, -7.7293, -3.0275], ] ).to(UpperCAmelCase ) self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3] , UpperCAmelCase , atol=UpperCAmelCase ) ) def __A (self ) -> Optional[Any]: _lowercase =MaskaFormerForUniversalSegmentation.from_pretrained(self.model_checkpoints ).to(UpperCAmelCase ).eval() _lowercase =self.default_image_processor _lowercase =image_processor( [np.zeros((3, 8_0_0, 1_3_3_3) ), np.zeros((3, 8_0_0, 1_3_3_3) )] , segmentation_maps=[np.zeros((3_8_4, 3_8_4) ).astype(np.floataa ), np.zeros((3_8_4, 3_8_4) ).astype(np.floataa )] , return_tensors='''pt''' , ) _lowercase =inputs['''pixel_values'''].to(UpperCAmelCase ) _lowercase =[el.to(UpperCAmelCase ) for el in inputs['''mask_labels''']] _lowercase =[el.to(UpperCAmelCase ) for el in inputs['''class_labels''']] with torch.no_grad(): _lowercase =model(**UpperCAmelCase ) self.assertTrue(outputs.loss is not None )
5
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available UpperCAmelCase__ = { '''configuration_xlm''': ['''XLM_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''XLMConfig''', '''XLMOnnxConfig'''], '''tokenization_xlm''': ['''XLMTokenizer'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ '''XLM_PRETRAINED_MODEL_ARCHIVE_LIST''', '''XLMForMultipleChoice''', '''XLMForQuestionAnswering''', '''XLMForQuestionAnsweringSimple''', '''XLMForSequenceClassification''', '''XLMForTokenClassification''', '''XLMModel''', '''XLMPreTrainedModel''', '''XLMWithLMHeadModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ '''TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFXLMForMultipleChoice''', '''TFXLMForQuestionAnsweringSimple''', '''TFXLMForSequenceClassification''', '''TFXLMForTokenClassification''', '''TFXLMMainLayer''', '''TFXLMModel''', '''TFXLMPreTrainedModel''', '''TFXLMWithLMHeadModel''', ] if TYPE_CHECKING: from .configuration_xlm import XLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XLMConfig, XLMOnnxConfig from .tokenization_xlm import XLMTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xlm import ( XLM_PRETRAINED_MODEL_ARCHIVE_LIST, XLMForMultipleChoice, XLMForQuestionAnswering, XLMForQuestionAnsweringSimple, XLMForSequenceClassification, XLMForTokenClassification, XLMModel, XLMPreTrainedModel, XLMWithLMHeadModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_xlm import ( TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFXLMForMultipleChoice, TFXLMForQuestionAnsweringSimple, TFXLMForSequenceClassification, TFXLMForTokenClassification, TFXLMMainLayer, TFXLMModel, TFXLMPreTrainedModel, TFXLMWithLMHeadModel, ) else: import sys UpperCAmelCase__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
5
1
import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_base import BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import PaddingStrategy, logging from .tokenization_realm import RealmTokenizer UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = {'''vocab_file''': '''vocab.txt''', '''tokenizer_file''': '''tokenizer.json'''} UpperCAmelCase__ = { '''vocab_file''': { '''google/realm-cc-news-pretrained-embedder''': ( '''https://huggingface.co/google/realm-cc-news-pretrained-embedder/resolve/main/vocab.txt''' ), '''google/realm-cc-news-pretrained-encoder''': ( '''https://huggingface.co/google/realm-cc-news-pretrained-encoder/resolve/main/vocab.txt''' ), '''google/realm-cc-news-pretrained-scorer''': ( '''https://huggingface.co/google/realm-cc-news-pretrained-scorer/resolve/main/vocab.txt''' ), '''google/realm-cc-news-pretrained-openqa''': ( '''https://huggingface.co/google/realm-cc-news-pretrained-openqa/aresolve/main/vocab.txt''' ), '''google/realm-orqa-nq-openqa''': '''https://huggingface.co/google/realm-orqa-nq-openqa/resolve/main/vocab.txt''', '''google/realm-orqa-nq-reader''': '''https://huggingface.co/google/realm-orqa-nq-reader/resolve/main/vocab.txt''', '''google/realm-orqa-wq-openqa''': '''https://huggingface.co/google/realm-orqa-wq-openqa/resolve/main/vocab.txt''', '''google/realm-orqa-wq-reader''': '''https://huggingface.co/google/realm-orqa-wq-reader/resolve/main/vocab.txt''', }, '''tokenizer_file''': { '''google/realm-cc-news-pretrained-embedder''': ( '''https://huggingface.co/google/realm-cc-news-pretrained-embedder/resolve/main/tokenizer.jsont''' ), '''google/realm-cc-news-pretrained-encoder''': ( '''https://huggingface.co/google/realm-cc-news-pretrained-encoder/resolve/main/tokenizer.json''' ), '''google/realm-cc-news-pretrained-scorer''': ( '''https://huggingface.co/google/realm-cc-news-pretrained-scorer/resolve/main/tokenizer.json''' ), '''google/realm-cc-news-pretrained-openqa''': ( '''https://huggingface.co/google/realm-cc-news-pretrained-openqa/aresolve/main/tokenizer.json''' ), '''google/realm-orqa-nq-openqa''': ( '''https://huggingface.co/google/realm-orqa-nq-openqa/resolve/main/tokenizer.json''' ), '''google/realm-orqa-nq-reader''': ( '''https://huggingface.co/google/realm-orqa-nq-reader/resolve/main/tokenizer.json''' ), '''google/realm-orqa-wq-openqa''': ( '''https://huggingface.co/google/realm-orqa-wq-openqa/resolve/main/tokenizer.json''' ), '''google/realm-orqa-wq-reader''': ( '''https://huggingface.co/google/realm-orqa-wq-reader/resolve/main/tokenizer.json''' ), }, } UpperCAmelCase__ = { '''google/realm-cc-news-pretrained-embedder''': 512, '''google/realm-cc-news-pretrained-encoder''': 512, '''google/realm-cc-news-pretrained-scorer''': 512, '''google/realm-cc-news-pretrained-openqa''': 512, '''google/realm-orqa-nq-openqa''': 512, '''google/realm-orqa-nq-reader''': 512, '''google/realm-orqa-wq-openqa''': 512, '''google/realm-orqa-wq-reader''': 512, } UpperCAmelCase__ = { '''google/realm-cc-news-pretrained-embedder''': {'''do_lower_case''': True}, '''google/realm-cc-news-pretrained-encoder''': {'''do_lower_case''': True}, '''google/realm-cc-news-pretrained-scorer''': {'''do_lower_case''': True}, '''google/realm-cc-news-pretrained-openqa''': {'''do_lower_case''': True}, '''google/realm-orqa-nq-openqa''': {'''do_lower_case''': True}, '''google/realm-orqa-nq-reader''': {'''do_lower_case''': True}, '''google/realm-orqa-wq-openqa''': {'''do_lower_case''': True}, '''google/realm-orqa-wq-reader''': {'''do_lower_case''': True}, } class lowerCamelCase__ ( lowerCAmelCase): SCREAMING_SNAKE_CASE__ = VOCAB_FILES_NAMES SCREAMING_SNAKE_CASE__ = PRETRAINED_VOCAB_FILES_MAP SCREAMING_SNAKE_CASE__ = PRETRAINED_INIT_CONFIGURATION SCREAMING_SNAKE_CASE__ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES SCREAMING_SNAKE_CASE__ = RealmTokenizer def __init__(self , UpperCAmelCase=None , UpperCAmelCase=None , UpperCAmelCase=True , UpperCAmelCase="[UNK]" , UpperCAmelCase="[SEP]" , UpperCAmelCase="[PAD]" , UpperCAmelCase="[CLS]" , UpperCAmelCase="[MASK]" , UpperCAmelCase=True , UpperCAmelCase=None , **UpperCAmelCase , ) -> Optional[Any]: super().__init__( UpperCAmelCase , tokenizer_file=UpperCAmelCase , do_lower_case=UpperCAmelCase , unk_token=UpperCAmelCase , sep_token=UpperCAmelCase , pad_token=UpperCAmelCase , cls_token=UpperCAmelCase , mask_token=UpperCAmelCase , tokenize_chinese_chars=UpperCAmelCase , strip_accents=UpperCAmelCase , **UpperCAmelCase , ) _lowercase =json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get('''lowercase''' , UpperCAmelCase ) != do_lower_case or normalizer_state.get('''strip_accents''' , UpperCAmelCase ) != strip_accents or normalizer_state.get('''handle_chinese_chars''' , UpperCAmelCase ) != tokenize_chinese_chars ): _lowercase =getattr(UpperCAmelCase , normalizer_state.pop('''type''' ) ) _lowercase =do_lower_case _lowercase =strip_accents _lowercase =tokenize_chinese_chars _lowercase =normalizer_class(**UpperCAmelCase ) _lowercase =do_lower_case def __A (self , UpperCAmelCase , **UpperCAmelCase ) -> Any: _lowercase =PaddingStrategy.MAX_LENGTH _lowercase =text _lowercase =kwargs.pop('''text_pair''' , UpperCAmelCase ) _lowercase =kwargs.pop('''return_tensors''' , UpperCAmelCase ) _lowercase ={ '''input_ids''': [], '''attention_mask''': [], '''token_type_ids''': [], } for idx, candidate_text in enumerate(UpperCAmelCase ): if batch_text_pair is not None: _lowercase =batch_text_pair[idx] else: _lowercase =None _lowercase =super().__call__(UpperCAmelCase , UpperCAmelCase , return_tensors=UpperCAmelCase , **UpperCAmelCase ) _lowercase =encoded_candidates.get('''input_ids''' ) _lowercase =encoded_candidates.get('''attention_mask''' ) _lowercase =encoded_candidates.get('''token_type_ids''' ) if encoded_input_ids is not None: output_data["input_ids"].append(UpperCAmelCase ) if encoded_attention_mask is not None: output_data["attention_mask"].append(UpperCAmelCase ) if encoded_token_type_ids is not None: output_data["token_type_ids"].append(UpperCAmelCase ) _lowercase ={key: item for key, item in output_data.items() if len(UpperCAmelCase ) != 0} return BatchEncoding(UpperCAmelCase , tensor_type=UpperCAmelCase ) def __A (self , UpperCAmelCase , UpperCAmelCase=None ) -> Dict: _lowercase =[self.cls_token_id] + token_ids_a + [self.sep_token_id] if token_ids_a: output += token_ids_a + [self.sep_token_id] return output def __A (self , UpperCAmelCase , UpperCAmelCase = None ) -> List[int]: _lowercase =[self.sep_token_id] _lowercase =[self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def __A (self , UpperCAmelCase , UpperCAmelCase = None ) -> Tuple[str]: _lowercase =self._tokenizer.model.save(UpperCAmelCase , name=UpperCAmelCase ) return tuple(UpperCAmelCase )
5
# Copyright 2023 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available UpperCAmelCase__ = { '''configuration_efficientnet''': [ '''EFFICIENTNET_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''EfficientNetConfig''', '''EfficientNetOnnxConfig''', ] } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = ['''EfficientNetImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ '''EFFICIENTNET_PRETRAINED_MODEL_ARCHIVE_LIST''', '''EfficientNetForImageClassification''', '''EfficientNetModel''', '''EfficientNetPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_efficientnet import ( EFFICIENTNET_PRETRAINED_CONFIG_ARCHIVE_MAP, EfficientNetConfig, EfficientNetOnnxConfig, ) try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_efficientnet import EfficientNetImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_efficientnet import ( EFFICIENTNET_PRETRAINED_MODEL_ARCHIVE_LIST, EfficientNetForImageClassification, EfficientNetModel, EfficientNetPreTrainedModel, ) else: import sys UpperCAmelCase__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure)
5
1
from dataclasses import dataclass from typing import Optional, Tuple, Union import torch import torch.nn as nn from ..configuration_utils import ConfigMixin, register_to_config from ..utils import BaseOutput, apply_forward_hook from .modeling_utils import ModelMixin from .vae import Decoder, DecoderOutput, Encoder, VectorQuantizer @dataclass class lowerCamelCase__ ( lowerCAmelCase): SCREAMING_SNAKE_CASE__ = 42 class lowerCamelCase__ ( lowerCAmelCase , lowerCAmelCase): @register_to_config def __init__(self , UpperCAmelCase = 3 , UpperCAmelCase = 3 , UpperCAmelCase = ("DownEncoderBlock2D",) , UpperCAmelCase = ("UpDecoderBlock2D",) , UpperCAmelCase = (6_4,) , UpperCAmelCase = 1 , UpperCAmelCase = "silu" , UpperCAmelCase = 3 , UpperCAmelCase = 3_2 , UpperCAmelCase = 2_5_6 , UpperCAmelCase = 3_2 , UpperCAmelCase = None , UpperCAmelCase = 0.1_8215 , UpperCAmelCase = "group" , ) -> Any: super().__init__() # pass init params to Encoder _lowercase =Encoder( in_channels=UpperCAmelCase , out_channels=UpperCAmelCase , down_block_types=UpperCAmelCase , block_out_channels=UpperCAmelCase , layers_per_block=UpperCAmelCase , act_fn=UpperCAmelCase , norm_num_groups=UpperCAmelCase , double_z=UpperCAmelCase , ) _lowercase =vq_embed_dim if vq_embed_dim is not None else latent_channels _lowercase =nn.Convad(UpperCAmelCase , UpperCAmelCase , 1 ) _lowercase =VectorQuantizer(UpperCAmelCase , UpperCAmelCase , beta=0.25 , remap=UpperCAmelCase , sane_index_shape=UpperCAmelCase ) _lowercase =nn.Convad(UpperCAmelCase , UpperCAmelCase , 1 ) # pass init params to Decoder _lowercase =Decoder( in_channels=UpperCAmelCase , out_channels=UpperCAmelCase , up_block_types=UpperCAmelCase , block_out_channels=UpperCAmelCase , layers_per_block=UpperCAmelCase , act_fn=UpperCAmelCase , norm_num_groups=UpperCAmelCase , norm_type=UpperCAmelCase , ) @apply_forward_hook def __A (self , UpperCAmelCase , UpperCAmelCase = True ) -> VQEncoderOutput: _lowercase =self.encoder(UpperCAmelCase ) _lowercase =self.quant_conv(UpperCAmelCase ) if not return_dict: return (h,) return VQEncoderOutput(latents=UpperCAmelCase ) @apply_forward_hook def __A (self , UpperCAmelCase , UpperCAmelCase = False , UpperCAmelCase = True ) -> Union[DecoderOutput, torch.FloatTensor]: # also go through quantization layer if not force_not_quantize: _lowercase , _lowercase , _lowercase =self.quantize(UpperCAmelCase ) else: _lowercase =h _lowercase =self.post_quant_conv(UpperCAmelCase ) _lowercase =self.decoder(UpperCAmelCase , quant if self.config.norm_type == '''spatial''' else None ) if not return_dict: return (dec,) return DecoderOutput(sample=UpperCAmelCase ) def __A (self , UpperCAmelCase , UpperCAmelCase = True ) -> Union[DecoderOutput, torch.FloatTensor]: _lowercase =sample _lowercase =self.encode(UpperCAmelCase ).latents _lowercase =self.decode(UpperCAmelCase ).sample if not return_dict: return (dec,) return DecoderOutput(sample=UpperCAmelCase )
5
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available UpperCAmelCase__ = { '''configuration_timesformer''': ['''TIMESFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''TimesformerConfig'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ '''TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TimesformerModel''', '''TimesformerForVideoClassification''', '''TimesformerPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_timesformer import TIMESFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TimesformerConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_timesformer import ( TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TimesformerForVideoClassification, TimesformerModel, TimesformerPreTrainedModel, ) else: import sys UpperCAmelCase__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
5
1
import copy import re class lowerCamelCase__ : SCREAMING_SNAKE_CASE__ = '''hp''' SCREAMING_SNAKE_CASE__ = {} SCREAMING_SNAKE_CASE__ = None @classmethod def __A (cls , UpperCAmelCase , UpperCAmelCase ) -> Any: _lowercase =prefix _lowercase =defaults cls.build_naming_info() @staticmethod def __A (UpperCAmelCase , UpperCAmelCase ) -> List[str]: if len(UpperCAmelCase ) == 0: return "" _lowercase =None if any(char.isdigit() for char in word ): raise Exception(f"Parameters should not contain numbers: '{word}' contains a number" ) if word in info["short_word"]: return info["short_word"][word] for prefix_len in range(1 , len(UpperCAmelCase ) + 1 ): _lowercase =word[:prefix_len] if prefix in info["reverse_short_word"]: continue else: _lowercase =prefix break if short_word is None: # Paranoid fallback def int_to_alphabetic(UpperCAmelCase ): _lowercase ='''''' while integer != 0: _lowercase =chr(ord('''A''' ) + integer % 1_0 ) + s integer //= 1_0 return s _lowercase =0 while True: _lowercase =word + '''#''' + int_to_alphabetic(UpperCAmelCase ) if sword in info["reverse_short_word"]: continue else: _lowercase =sword break _lowercase =short_word _lowercase =word return short_word @staticmethod def __A (UpperCAmelCase , UpperCAmelCase ) -> Tuple: _lowercase =param_name.split('''_''' ) _lowercase =[TrialShortNamer.shortname_for_word(UpperCAmelCase , UpperCAmelCase ) for word in words] # We try to create a separatorless short name, but if there is a collision we have to fallback # to a separated short name _lowercase =['''''', '''_'''] for separator in separators: _lowercase =separator.join(UpperCAmelCase ) if shortname not in info["reverse_short_param"]: _lowercase =shortname _lowercase =param_name return shortname return param_name @staticmethod def __A (UpperCAmelCase , UpperCAmelCase ) -> Optional[Any]: _lowercase =TrialShortNamer.shortname_for_key(UpperCAmelCase , UpperCAmelCase ) _lowercase =short_name _lowercase =param_name @classmethod def __A (cls ) -> Optional[Any]: if cls.NAMING_INFO is not None: return _lowercase ={ '''short_word''': {}, '''reverse_short_word''': {}, '''short_param''': {}, '''reverse_short_param''': {}, } _lowercase =list(cls.DEFAULTS.keys() ) for k in field_keys: cls.add_new_param_name(UpperCAmelCase , UpperCAmelCase ) _lowercase =info @classmethod def __A (cls , UpperCAmelCase ) -> Any: cls.build_naming_info() assert cls.PREFIX is not None _lowercase =[copy.copy(cls.PREFIX )] for k, v in params.items(): if k not in cls.DEFAULTS: raise Exception(f"You should provide a default value for the param name {k} with value {v}" ) if v == cls.DEFAULTS[k]: # The default value is not added to the name continue _lowercase =cls.NAMING_INFO['''short_param'''][k] if isinstance(UpperCAmelCase , UpperCAmelCase ): _lowercase =1 if v else 0 _lowercase ='''''' if isinstance(UpperCAmelCase , (int, float) ) else '''-''' _lowercase =f"{key}{sep}{v}" name.append(UpperCAmelCase ) return "_".join(UpperCAmelCase ) @classmethod def __A (cls , UpperCAmelCase ) -> Optional[int]: _lowercase =repr[len(cls.PREFIX ) + 1 :] if repr == "": _lowercase =[] else: _lowercase =repr.split('''_''' ) _lowercase ={} for value in values: if "-" in value: _lowercase , _lowercase =value.split('''-''' ) else: _lowercase =re.sub('''[0-9.]''' , '''''' , UpperCAmelCase ) _lowercase =float(re.sub('''[^0-9.]''' , '''''' , UpperCAmelCase ) ) _lowercase =cls.NAMING_INFO['''reverse_short_param'''][p_k] _lowercase =p_v for k in cls.DEFAULTS: if k not in parameters: _lowercase =cls.DEFAULTS[k] return parameters
5
def UpperCAmelCase_ ( __snake_case , __snake_case ) -> List[Any]: """simple docstring""" if b == 0: return 1 if (b % 2) == 0: return actual_power(__snake_case , int(b / 2 ) ) * actual_power(__snake_case , int(b / 2 ) ) else: return a * actual_power(__snake_case , int(b / 2 ) ) * actual_power(__snake_case , int(b / 2 ) ) def UpperCAmelCase_ ( __snake_case , __snake_case ) -> float: """simple docstring""" if b < 0: return 1 / actual_power(__snake_case , __snake_case ) return actual_power(__snake_case , __snake_case ) if __name__ == "__main__": print(power(-2, -3))
5
1
from __future__ import annotations # This is the precision for this function which can be altered. # It is recommended for users to keep this number greater than or equal to 10. UpperCAmelCase__ = 10 def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case ) -> int: """simple docstring""" for i in range(__snake_case , __snake_case ): if array[i] == target: return i return -1 def UpperCAmelCase_ ( __snake_case , __snake_case ) -> int: """simple docstring""" _lowercase =0 _lowercase =len(__snake_case ) while left <= right: if right - left < precision: return lin_search(__snake_case , __snake_case , __snake_case , __snake_case ) _lowercase =(left + right) // 3 + 1 _lowercase =2 * (left + right) // 3 + 1 if array[one_third] == target: return one_third elif array[two_third] == target: return two_third elif target < array[one_third]: _lowercase =one_third - 1 elif array[two_third] < target: _lowercase =two_third + 1 else: _lowercase =one_third + 1 _lowercase =two_third - 1 else: return -1 def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case ) -> int: """simple docstring""" if left < right: if right - left < precision: return lin_search(__snake_case , __snake_case , __snake_case , __snake_case ) _lowercase =(left + right) // 3 + 1 _lowercase =2 * (left + right) // 3 + 1 if array[one_third] == target: return one_third elif array[two_third] == target: return two_third elif target < array[one_third]: return rec_ternary_search(__snake_case , one_third - 1 , __snake_case , __snake_case ) elif array[two_third] < target: return rec_ternary_search(two_third + 1 , __snake_case , __snake_case , __snake_case ) else: return rec_ternary_search(one_third + 1 , two_third - 1 , __snake_case , __snake_case ) else: return -1 if __name__ == "__main__": import doctest doctest.testmod() UpperCAmelCase__ = input('''Enter numbers separated by comma:\n''').strip() UpperCAmelCase__ = [int(item.strip()) for item in user_input.split(''',''')] assert collection == sorted(collection), f"List must be ordered.\n{collection}." UpperCAmelCase__ = int(input('''Enter the number to be found in the list:\n''').strip()) UpperCAmelCase__ = ite_ternary_search(collection, target) UpperCAmelCase__ = rec_ternary_search(0, len(collection) - 1, collection, target) if resulta != -1: print(f'''Iterative search: {target} found at positions: {resulta}''') print(f'''Recursive search: {target} found at positions: {resulta}''') else: print('''Not found''')
5
from typing import Optional from torch import nn from .transformer_ad import TransformeraDModel, TransformeraDModelOutput class lowerCamelCase__ ( nn.Module): def __init__(self , UpperCAmelCase = 1_6 , UpperCAmelCase = 8_8 , UpperCAmelCase = None , UpperCAmelCase = 1 , UpperCAmelCase = 0.0 , UpperCAmelCase = 3_2 , UpperCAmelCase = None , UpperCAmelCase = False , UpperCAmelCase = None , UpperCAmelCase = None , UpperCAmelCase = "geglu" , UpperCAmelCase = None , ) -> Any: super().__init__() _lowercase =nn.ModuleList( [ TransformeraDModel( num_attention_heads=UpperCAmelCase , attention_head_dim=UpperCAmelCase , in_channels=UpperCAmelCase , num_layers=UpperCAmelCase , dropout=UpperCAmelCase , norm_num_groups=UpperCAmelCase , cross_attention_dim=UpperCAmelCase , attention_bias=UpperCAmelCase , sample_size=UpperCAmelCase , num_vector_embeds=UpperCAmelCase , activation_fn=UpperCAmelCase , num_embeds_ada_norm=UpperCAmelCase , ) for _ in range(2 ) ] ) # Variables that can be set by a pipeline: # The ratio of transformer1 to transformer2's output states to be combined during inference _lowercase =0.5 # The shape of `encoder_hidden_states` is expected to be # `(batch_size, condition_lengths[0]+condition_lengths[1], num_features)` _lowercase =[7_7, 2_5_7] # Which transformer to use to encode which condition. # E.g. `(1, 0)` means that we'll use `transformers[1](conditions[0])` and `transformers[0](conditions[1])` _lowercase =[1, 0] def __A (self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase=None , UpperCAmelCase=None , UpperCAmelCase=None , UpperCAmelCase = True , ) -> str: _lowercase =hidden_states _lowercase =[] _lowercase =0 # attention_mask is not used yet for i in range(2 ): # for each of the two transformers, pass the corresponding condition tokens _lowercase =encoder_hidden_states[:, tokens_start : tokens_start + self.condition_lengths[i]] _lowercase =self.transformer_index_for_condition[i] _lowercase =self.transformers[transformer_index]( UpperCAmelCase , encoder_hidden_states=UpperCAmelCase , timestep=UpperCAmelCase , cross_attention_kwargs=UpperCAmelCase , return_dict=UpperCAmelCase , )[0] encoded_states.append(encoded_state - input_states ) tokens_start += self.condition_lengths[i] _lowercase =encoded_states[0] * self.mix_ratio + encoded_states[1] * (1 - self.mix_ratio) _lowercase =output_states + input_states if not return_dict: return (output_states,) return TransformeraDModelOutput(sample=UpperCAmelCase )
5
1
# A Bipartite Graph is a graph whose vertices can be divided into two independent sets, # U and V such that every edge (u, v) either connects a vertex from U to V or a vertex # from V to U. In other words, for every edge (u, v), either u belongs to U and v to V, # or u belongs to V and v to U. We can also say that there is no edge that connects # vertices of same set. def UpperCAmelCase_ ( __snake_case ) -> Any: """simple docstring""" _lowercase =[False] * len(__snake_case ) _lowercase =[-1] * len(__snake_case ) def dfs(__snake_case , __snake_case ): _lowercase =True _lowercase =c for u in graph[v]: if not visited[u]: dfs(__snake_case , 1 - c ) for i in range(len(__snake_case ) ): if not visited[i]: dfs(__snake_case , 0 ) for i in range(len(__snake_case ) ): for j in graph[i]: if color[i] == color[j]: return False return True # Adjacency list of graph UpperCAmelCase__ = {0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 4: []} print(check_bipartite_dfs(graph))
5
import heapq as hq import math from collections.abc import Iterator class lowerCamelCase__ : def __init__(self , UpperCAmelCase ) -> Any: _lowercase =str(id_ ) _lowercase =None _lowercase =None _lowercase =[] _lowercase ={} # {vertex:distance} def __lt__(self , UpperCAmelCase ) -> List[str]: return self.key < other.key def __repr__(self ) -> str: return self.id def __A (self , UpperCAmelCase ) -> Dict: self.neighbors.append(UpperCAmelCase ) def __A (self , UpperCAmelCase , UpperCAmelCase ) -> Optional[Any]: _lowercase =weight def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case ) -> List[str]: """simple docstring""" graph[a - 1].add_neighbor(graph[b - 1] ) graph[b - 1].add_neighbor(graph[a - 1] ) # add the edges: graph[a - 1].add_edge(graph[b - 1] , __snake_case ) graph[b - 1].add_edge(graph[a - 1] , __snake_case ) def UpperCAmelCase_ ( __snake_case , __snake_case ) -> list: """simple docstring""" _lowercase =[] for u in graph: _lowercase =math.inf _lowercase =None _lowercase =0 _lowercase =graph[:] while q: _lowercase =min(__snake_case ) q.remove(__snake_case ) for v in u.neighbors: if (v in q) and (u.edges[v.id] < v.key): _lowercase =u _lowercase =u.edges[v.id] for i in range(1 , len(__snake_case ) ): a.append((int(graph[i].id ) + 1, int(graph[i].pi.id ) + 1) ) return a def UpperCAmelCase_ ( __snake_case , __snake_case ) -> Iterator[tuple]: """simple docstring""" for u in graph: _lowercase =math.inf _lowercase =None _lowercase =0 _lowercase =list(__snake_case ) hq.heapify(__snake_case ) while h: _lowercase =hq.heappop(__snake_case ) for v in u.neighbors: if (v in h) and (u.edges[v.id] < v.key): _lowercase =u _lowercase =u.edges[v.id] hq.heapify(__snake_case ) for i in range(1 , len(__snake_case ) ): yield (int(graph[i].id ) + 1, int(graph[i].pi.id ) + 1) def UpperCAmelCase_ ( ) -> None: """simple docstring""" if __name__ == "__main__": import doctest doctest.testmod()
5
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) if is_sentencepiece_available(): from ..ta.tokenization_ta import TaTokenizer else: from ...utils.dummy_sentencepiece_objects import TaTokenizer UpperCAmelCase__ = TaTokenizer if is_tokenizers_available(): from ..ta.tokenization_ta_fast import TaTokenizerFast else: from ...utils.dummy_tokenizers_objects import TaTokenizerFast UpperCAmelCase__ = TaTokenizerFast UpperCAmelCase__ = {'''configuration_mt5''': ['''MT5Config''', '''MT5OnnxConfig''']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ '''MT5EncoderModel''', '''MT5ForConditionalGeneration''', '''MT5ForQuestionAnswering''', '''MT5Model''', '''MT5PreTrainedModel''', '''MT5Stack''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = ['''TFMT5EncoderModel''', '''TFMT5ForConditionalGeneration''', '''TFMT5Model'''] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = ['''FlaxMT5EncoderModel''', '''FlaxMT5ForConditionalGeneration''', '''FlaxMT5Model'''] if TYPE_CHECKING: from .configuration_mta import MTaConfig, MTaOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mta import ( MTaEncoderModel, MTaForConditionalGeneration, MTaForQuestionAnswering, MTaModel, MTaPreTrainedModel, MTaStack, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_mta import TFMTaEncoderModel, TFMTaForConditionalGeneration, TFMTaModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_mta import FlaxMTaEncoderModel, FlaxMTaForConditionalGeneration, FlaxMTaModel else: import sys UpperCAmelCase__ = _LazyModule( __name__, globals()['''__file__'''], _import_structure, extra_objects={'''MT5Tokenizer''': MTaTokenizer, '''MT5TokenizerFast''': MTaTokenizerFast}, module_spec=__spec__, )
5
# flake8: noqa # Lint as: python3 UpperCAmelCase__ = [ '''VerificationMode''', '''Version''', '''disable_progress_bar''', '''enable_progress_bar''', '''is_progress_bar_enabled''', '''experimental''', ] from .info_utils import VerificationMode from .logging import disable_progress_bar, enable_progress_bar, is_progress_bar_enabled from .version import Version from .experimental import experimental
5
1
import argparse import json import torch from diffusers import DDPMScheduler, LDMPipeline, UNetaDModel, VQModel def UpperCAmelCase_ ( __snake_case , __snake_case=1 ) -> Any: """simple docstring""" if n_shave_prefix_segments >= 0: return ".".join(path.split('''.''' )[n_shave_prefix_segments:] ) else: return ".".join(path.split('''.''' )[:n_shave_prefix_segments] ) def UpperCAmelCase_ ( __snake_case , __snake_case=0 ) -> str: """simple docstring""" _lowercase =[] for old_item in old_list: _lowercase =old_item.replace('''in_layers.0''' , '''norm1''' ) _lowercase =new_item.replace('''in_layers.2''' , '''conv1''' ) _lowercase =new_item.replace('''out_layers.0''' , '''norm2''' ) _lowercase =new_item.replace('''out_layers.3''' , '''conv2''' ) _lowercase =new_item.replace('''emb_layers.1''' , '''time_emb_proj''' ) _lowercase =new_item.replace('''skip_connection''' , '''conv_shortcut''' ) _lowercase =shave_segments(__snake_case , n_shave_prefix_segments=__snake_case ) mapping.append({'''old''': old_item, '''new''': new_item} ) return mapping def UpperCAmelCase_ ( __snake_case , __snake_case=0 ) -> Tuple: """simple docstring""" _lowercase =[] for old_item in old_list: _lowercase =old_item _lowercase =new_item.replace('''norm.weight''' , '''group_norm.weight''' ) _lowercase =new_item.replace('''norm.bias''' , '''group_norm.bias''' ) _lowercase =new_item.replace('''proj_out.weight''' , '''proj_attn.weight''' ) _lowercase =new_item.replace('''proj_out.bias''' , '''proj_attn.bias''' ) _lowercase =shave_segments(__snake_case , n_shave_prefix_segments=__snake_case ) mapping.append({'''old''': old_item, '''new''': new_item} ) return mapping def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case=None , __snake_case=None , __snake_case=None ) -> Any: """simple docstring""" assert isinstance(__snake_case , __snake_case ), "Paths should be a list of dicts containing 'old' and 'new' keys." # Splits the attention layers into three variables. if attention_paths_to_split is not None: for path, path_map in attention_paths_to_split.items(): _lowercase =old_checkpoint[path] _lowercase =old_tensor.shape[0] // 3 _lowercase =(-1, channels) if len(old_tensor.shape ) == 3 else (-1) _lowercase =old_tensor.shape[0] // config['''num_head_channels'''] // 3 _lowercase =old_tensor.reshape((num_heads, 3 * channels // num_heads) + old_tensor.shape[1:] ) _lowercase , _lowercase , _lowercase =old_tensor.split(channels // num_heads , dim=1 ) _lowercase =query.reshape(__snake_case ) _lowercase =key.reshape(__snake_case ) _lowercase =value.reshape(__snake_case ) for path in paths: _lowercase =path['''new'''] # These have already been assigned if attention_paths_to_split is not None and new_path in attention_paths_to_split: continue # Global renaming happens here _lowercase =new_path.replace('''middle_block.0''' , '''mid_block.resnets.0''' ) _lowercase =new_path.replace('''middle_block.1''' , '''mid_block.attentions.0''' ) _lowercase =new_path.replace('''middle_block.2''' , '''mid_block.resnets.1''' ) if additional_replacements is not None: for replacement in additional_replacements: _lowercase =new_path.replace(replacement['''old'''] , replacement['''new'''] ) # proj_attn.weight has to be converted from conv 1D to linear if "proj_attn.weight" in new_path: _lowercase =old_checkpoint[path['''old''']][:, :, 0] else: _lowercase =old_checkpoint[path['''old''']] def UpperCAmelCase_ ( __snake_case , __snake_case ) -> str: """simple docstring""" _lowercase ={} _lowercase =checkpoint['''time_embed.0.weight'''] _lowercase =checkpoint['''time_embed.0.bias'''] _lowercase =checkpoint['''time_embed.2.weight'''] _lowercase =checkpoint['''time_embed.2.bias'''] _lowercase =checkpoint['''input_blocks.0.0.weight'''] _lowercase =checkpoint['''input_blocks.0.0.bias'''] _lowercase =checkpoint['''out.0.weight'''] _lowercase =checkpoint['''out.0.bias'''] _lowercase =checkpoint['''out.2.weight'''] _lowercase =checkpoint['''out.2.bias'''] # Retrieves the keys for the input blocks only _lowercase =len({'''.'''.join(layer.split('''.''' )[:2] ) for layer in checkpoint if '''input_blocks''' in layer} ) _lowercase ={ layer_id: [key for key in checkpoint if F"input_blocks.{layer_id}" in key] for layer_id in range(__snake_case ) } # Retrieves the keys for the middle blocks only _lowercase =len({'''.'''.join(layer.split('''.''' )[:2] ) for layer in checkpoint if '''middle_block''' in layer} ) _lowercase ={ layer_id: [key for key in checkpoint if F"middle_block.{layer_id}" in key] for layer_id in range(__snake_case ) } # Retrieves the keys for the output blocks only _lowercase =len({'''.'''.join(layer.split('''.''' )[:2] ) for layer in checkpoint if '''output_blocks''' in layer} ) _lowercase ={ layer_id: [key for key in checkpoint if F"output_blocks.{layer_id}" in key] for layer_id in range(__snake_case ) } for i in range(1 , __snake_case ): _lowercase =(i - 1) // (config['''num_res_blocks'''] + 1) _lowercase =(i - 1) % (config['''num_res_blocks'''] + 1) _lowercase =[key for key in input_blocks[i] if F"input_blocks.{i}.0" in key] _lowercase =[key for key in input_blocks[i] if F"input_blocks.{i}.1" in key] if F"input_blocks.{i}.0.op.weight" in checkpoint: _lowercase =checkpoint[ F"input_blocks.{i}.0.op.weight" ] _lowercase =checkpoint[ F"input_blocks.{i}.0.op.bias" ] continue _lowercase =renew_resnet_paths(__snake_case ) _lowercase ={'''old''': F"input_blocks.{i}.0", '''new''': F"down_blocks.{block_id}.resnets.{layer_in_block_id}"} _lowercase ={'''old''': '''resnets.2.op''', '''new''': '''downsamplers.0.op'''} assign_to_checkpoint( __snake_case , __snake_case , __snake_case , additional_replacements=[meta_path, resnet_op] , config=__snake_case ) if len(__snake_case ): _lowercase =renew_attention_paths(__snake_case ) _lowercase ={ '''old''': F"input_blocks.{i}.1", '''new''': F"down_blocks.{block_id}.attentions.{layer_in_block_id}", } _lowercase ={ F"input_blocks.{i}.1.qkv.bias": { '''key''': F"down_blocks.{block_id}.attentions.{layer_in_block_id}.key.bias", '''query''': F"down_blocks.{block_id}.attentions.{layer_in_block_id}.query.bias", '''value''': F"down_blocks.{block_id}.attentions.{layer_in_block_id}.value.bias", }, F"input_blocks.{i}.1.qkv.weight": { '''key''': F"down_blocks.{block_id}.attentions.{layer_in_block_id}.key.weight", '''query''': F"down_blocks.{block_id}.attentions.{layer_in_block_id}.query.weight", '''value''': F"down_blocks.{block_id}.attentions.{layer_in_block_id}.value.weight", }, } assign_to_checkpoint( __snake_case , __snake_case , __snake_case , additional_replacements=[meta_path] , attention_paths_to_split=__snake_case , config=__snake_case , ) _lowercase =middle_blocks[0] _lowercase =middle_blocks[1] _lowercase =middle_blocks[2] _lowercase =renew_resnet_paths(__snake_case ) assign_to_checkpoint(__snake_case , __snake_case , __snake_case , config=__snake_case ) _lowercase =renew_resnet_paths(__snake_case ) assign_to_checkpoint(__snake_case , __snake_case , __snake_case , config=__snake_case ) _lowercase =renew_attention_paths(__snake_case ) _lowercase ={ '''middle_block.1.qkv.bias''': { '''key''': '''mid_block.attentions.0.key.bias''', '''query''': '''mid_block.attentions.0.query.bias''', '''value''': '''mid_block.attentions.0.value.bias''', }, '''middle_block.1.qkv.weight''': { '''key''': '''mid_block.attentions.0.key.weight''', '''query''': '''mid_block.attentions.0.query.weight''', '''value''': '''mid_block.attentions.0.value.weight''', }, } assign_to_checkpoint( __snake_case , __snake_case , __snake_case , attention_paths_to_split=__snake_case , config=__snake_case ) for i in range(__snake_case ): _lowercase =i // (config['''num_res_blocks'''] + 1) _lowercase =i % (config['''num_res_blocks'''] + 1) _lowercase =[shave_segments(__snake_case , 2 ) for name in output_blocks[i]] _lowercase ={} for layer in output_block_layers: _lowercase , _lowercase =layer.split('''.''' )[0], shave_segments(__snake_case , 1 ) if layer_id in output_block_list: output_block_list[layer_id].append(__snake_case ) else: _lowercase =[layer_name] if len(__snake_case ) > 1: _lowercase =[key for key in output_blocks[i] if F"output_blocks.{i}.0" in key] _lowercase =[key for key in output_blocks[i] if F"output_blocks.{i}.1" in key] _lowercase =renew_resnet_paths(__snake_case ) _lowercase =renew_resnet_paths(__snake_case ) _lowercase ={'''old''': F"output_blocks.{i}.0", '''new''': F"up_blocks.{block_id}.resnets.{layer_in_block_id}"} assign_to_checkpoint(__snake_case , __snake_case , __snake_case , additional_replacements=[meta_path] , config=__snake_case ) if ["conv.weight", "conv.bias"] in output_block_list.values(): _lowercase =list(output_block_list.values() ).index(['''conv.weight''', '''conv.bias'''] ) _lowercase =checkpoint[ F"output_blocks.{i}.{index}.conv.weight" ] _lowercase =checkpoint[ F"output_blocks.{i}.{index}.conv.bias" ] # Clear attentions as they have been attributed above. if len(__snake_case ) == 2: _lowercase =[] if len(__snake_case ): _lowercase =renew_attention_paths(__snake_case ) _lowercase ={ '''old''': F"output_blocks.{i}.1", '''new''': F"up_blocks.{block_id}.attentions.{layer_in_block_id}", } _lowercase ={ F"output_blocks.{i}.1.qkv.bias": { '''key''': F"up_blocks.{block_id}.attentions.{layer_in_block_id}.key.bias", '''query''': F"up_blocks.{block_id}.attentions.{layer_in_block_id}.query.bias", '''value''': F"up_blocks.{block_id}.attentions.{layer_in_block_id}.value.bias", }, F"output_blocks.{i}.1.qkv.weight": { '''key''': F"up_blocks.{block_id}.attentions.{layer_in_block_id}.key.weight", '''query''': F"up_blocks.{block_id}.attentions.{layer_in_block_id}.query.weight", '''value''': F"up_blocks.{block_id}.attentions.{layer_in_block_id}.value.weight", }, } assign_to_checkpoint( __snake_case , __snake_case , __snake_case , additional_replacements=[meta_path] , attention_paths_to_split=to_split if any('''qkv''' in key for key in attentions ) else None , config=__snake_case , ) else: _lowercase =renew_resnet_paths(__snake_case , n_shave_prefix_segments=1 ) for path in resnet_0_paths: _lowercase ='''.'''.join(['''output_blocks''', str(__snake_case ), path['''old''']] ) _lowercase ='''.'''.join(['''up_blocks''', str(__snake_case ), '''resnets''', str(__snake_case ), path['''new''']] ) _lowercase =checkpoint[old_path] return new_checkpoint if __name__ == "__main__": UpperCAmelCase__ = argparse.ArgumentParser() parser.add_argument( '''--checkpoint_path''', default=None, type=str, required=True, help='''Path to the checkpoint to convert.''' ) parser.add_argument( '''--config_file''', default=None, type=str, required=True, help='''The config json file corresponding to the architecture.''', ) parser.add_argument('''--dump_path''', default=None, type=str, required=True, help='''Path to the output model.''') UpperCAmelCase__ = parser.parse_args() UpperCAmelCase__ = torch.load(args.checkpoint_path) with open(args.config_file) as f: UpperCAmelCase__ = json.loads(f.read()) UpperCAmelCase__ = convert_ldm_checkpoint(checkpoint, config) if "ldm" in config: del config["ldm"] UpperCAmelCase__ = UNetaDModel(**config) model.load_state_dict(converted_checkpoint) try: UpperCAmelCase__ = DDPMScheduler.from_config('''/'''.join(args.checkpoint_path.split('''/''')[:-1])) UpperCAmelCase__ = VQModel.from_pretrained('''/'''.join(args.checkpoint_path.split('''/''')[:-1])) UpperCAmelCase__ = LDMPipeline(unet=model, scheduler=scheduler, vae=vqvae) pipe.save_pretrained(args.dump_path) except: # noqa: E722 model.save_pretrained(args.dump_path)
5
import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = { '''microsoft/wavlm-base''': '''https://huggingface.co/microsoft/wavlm-base/resolve/main/config.json''', # See all WavLM models at https://huggingface.co/models?filter=wavlm } class lowerCamelCase__ ( lowerCAmelCase): SCREAMING_SNAKE_CASE__ = '''wavlm''' def __init__(self , UpperCAmelCase=3_2 , UpperCAmelCase=7_6_8 , UpperCAmelCase=1_2 , UpperCAmelCase=1_2 , UpperCAmelCase=3_0_7_2 , UpperCAmelCase="gelu" , UpperCAmelCase=0.1 , UpperCAmelCase=0.1 , UpperCAmelCase=0.1 , UpperCAmelCase=0.0 , UpperCAmelCase=0.1 , UpperCAmelCase=0.1 , UpperCAmelCase=0.02 , UpperCAmelCase=1e-5 , UpperCAmelCase="group" , UpperCAmelCase="gelu" , UpperCAmelCase=(5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2) , UpperCAmelCase=(5, 2, 2, 2, 2, 2, 2) , UpperCAmelCase=(1_0, 3, 3, 3, 3, 2, 2) , UpperCAmelCase=False , UpperCAmelCase=1_2_8 , UpperCAmelCase=1_6 , UpperCAmelCase=3_2_0 , UpperCAmelCase=8_0_0 , UpperCAmelCase=False , UpperCAmelCase=True , UpperCAmelCase=0.05 , UpperCAmelCase=1_0 , UpperCAmelCase=2 , UpperCAmelCase=0.0 , UpperCAmelCase=1_0 , UpperCAmelCase=3_2_0 , UpperCAmelCase=2 , UpperCAmelCase=0.1 , UpperCAmelCase=1_0_0 , UpperCAmelCase=2_5_6 , UpperCAmelCase=2_5_6 , UpperCAmelCase=0.1 , UpperCAmelCase="mean" , UpperCAmelCase=False , UpperCAmelCase=False , UpperCAmelCase=2_5_6 , UpperCAmelCase=(5_1_2, 5_1_2, 5_1_2, 5_1_2, 1_5_0_0) , UpperCAmelCase=(5, 3, 3, 1, 1) , UpperCAmelCase=(1, 2, 3, 1, 1) , UpperCAmelCase=5_1_2 , UpperCAmelCase=8_0 , UpperCAmelCase=0 , UpperCAmelCase=1 , UpperCAmelCase=2 , UpperCAmelCase=False , UpperCAmelCase=3 , UpperCAmelCase=2 , UpperCAmelCase=3 , UpperCAmelCase=None , **UpperCAmelCase , ) -> Optional[Any]: super().__init__(**UpperCAmelCase , pad_token_id=UpperCAmelCase , bos_token_id=UpperCAmelCase , eos_token_id=UpperCAmelCase ) _lowercase =hidden_size _lowercase =feat_extract_norm _lowercase =feat_extract_activation _lowercase =list(UpperCAmelCase ) _lowercase =list(UpperCAmelCase ) _lowercase =list(UpperCAmelCase ) _lowercase =conv_bias _lowercase =num_buckets _lowercase =max_bucket_distance _lowercase =num_conv_pos_embeddings _lowercase =num_conv_pos_embedding_groups _lowercase =len(self.conv_dim ) _lowercase =num_hidden_layers _lowercase =intermediate_size _lowercase =hidden_act _lowercase =num_attention_heads _lowercase =hidden_dropout _lowercase =attention_dropout _lowercase =activation_dropout _lowercase =feat_proj_dropout _lowercase =final_dropout _lowercase =layerdrop _lowercase =layer_norm_eps _lowercase =initializer_range _lowercase =num_ctc_classes _lowercase =vocab_size _lowercase =do_stable_layer_norm _lowercase =use_weighted_layer_sum _lowercase =classifier_proj_size if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( '''Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` ==''' ''' `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) =''' f" {len(self.conv_dim )}`, `len(config.conv_stride) = {len(self.conv_stride )}`," f" `len(config.conv_kernel) = {len(self.conv_kernel )}`." ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 _lowercase =apply_spec_augment _lowercase =mask_time_prob _lowercase =mask_time_length _lowercase =mask_time_min_masks _lowercase =mask_feature_prob _lowercase =mask_feature_length # parameters for pretraining with codevector quantized representations _lowercase =num_codevectors_per_group _lowercase =num_codevector_groups _lowercase =contrastive_logits_temperature _lowercase =num_negatives _lowercase =codevector_dim _lowercase =proj_codevector_dim _lowercase =diversity_loss_weight # ctc loss _lowercase =ctc_loss_reduction _lowercase =ctc_zero_infinity # adapter _lowercase =add_adapter _lowercase =adapter_kernel_size _lowercase =adapter_stride _lowercase =num_adapter_layers _lowercase =output_hidden_size or hidden_size # SequenceClassification-specific parameter. Feel free to ignore for other classes. _lowercase =classifier_proj_size # XVector-specific parameters. Feel free to ignore for other classes. _lowercase =list(UpperCAmelCase ) _lowercase =list(UpperCAmelCase ) _lowercase =list(UpperCAmelCase ) _lowercase =xvector_output_dim @property def __A (self ) -> int: return functools.reduce(operator.mul , self.conv_stride , 1 )
5
1
import copy import tempfile import unittest from huggingface_hub import HfFolder, delete_repo from parameterized import parameterized from requests.exceptions import HTTPError from transformers import AutoConfig, GenerationConfig from transformers.testing_utils import TOKEN, USER, is_staging_test class lowerCamelCase__ ( unittest.TestCase): @parameterized.expand([(None,), ('''foo.json''',)] ) def __A (self , UpperCAmelCase ) -> Any: _lowercase =GenerationConfig( do_sample=UpperCAmelCase , temperature=0.7 , length_penalty=1.0 , bad_words_ids=[[1, 2, 3], [4, 5]] , ) with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained(UpperCAmelCase , config_name=UpperCAmelCase ) _lowercase =GenerationConfig.from_pretrained(UpperCAmelCase , config_name=UpperCAmelCase ) # Checks parameters that were specified self.assertEqual(loaded_config.do_sample , UpperCAmelCase ) self.assertEqual(loaded_config.temperature , 0.7 ) self.assertEqual(loaded_config.length_penalty , 1.0 ) self.assertEqual(loaded_config.bad_words_ids , [[1, 2, 3], [4, 5]] ) # Checks parameters that were not specified (defaults) self.assertEqual(loaded_config.top_k , 5_0 ) self.assertEqual(loaded_config.max_length , 2_0 ) self.assertEqual(loaded_config.max_time , UpperCAmelCase ) def __A (self ) -> Optional[Any]: _lowercase =AutoConfig.from_pretrained('''gpt2''' ) _lowercase =GenerationConfig.from_model_config(UpperCAmelCase ) _lowercase =GenerationConfig() # The generation config has loaded a few non-default parameters from the model config self.assertNotEqual(UpperCAmelCase , UpperCAmelCase ) # One of those parameters is eos_token_id -- check if it matches self.assertNotEqual(generation_config_from_model.eos_token_id , default_generation_config.eos_token_id ) self.assertEqual(generation_config_from_model.eos_token_id , model_config.eos_token_id ) def __A (self ) -> List[str]: _lowercase =GenerationConfig() _lowercase ={ '''max_new_tokens''': 1_0_2_4, '''foo''': '''bar''', } _lowercase =copy.deepcopy(UpperCAmelCase ) _lowercase =generation_config.update(**UpperCAmelCase ) # update_kwargs was not modified (no side effects) self.assertEqual(UpperCAmelCase , UpperCAmelCase ) # update_kwargs was used to update the config on valid attributes self.assertEqual(generation_config.max_new_tokens , 1_0_2_4 ) # `.update()` returns a dictionary of unused kwargs self.assertEqual(UpperCAmelCase , {'''foo''': '''bar'''} ) def __A (self ) -> Tuple: _lowercase =GenerationConfig() _lowercase ='''bar''' with tempfile.TemporaryDirectory('''test-generation-config''' ) as tmp_dir: generation_config.save_pretrained(UpperCAmelCase ) _lowercase =GenerationConfig.from_pretrained(UpperCAmelCase ) # update_kwargs was used to update the config on valid attributes self.assertEqual(new_config.foo , '''bar''' ) _lowercase =GenerationConfig.from_model_config(UpperCAmelCase ) assert not hasattr(UpperCAmelCase , '''foo''' ) # no new kwargs should be initialized if from config def __A (self ) -> List[str]: _lowercase =GenerationConfig() self.assertEqual(default_config.temperature , 1.0 ) self.assertEqual(default_config.do_sample , UpperCAmelCase ) self.assertEqual(default_config.num_beams , 1 ) _lowercase =GenerationConfig( do_sample=UpperCAmelCase , temperature=0.7 , length_penalty=1.0 , bad_words_ids=[[1, 2, 3], [4, 5]] , ) self.assertEqual(config.temperature , 0.7 ) self.assertEqual(config.do_sample , UpperCAmelCase ) self.assertEqual(config.num_beams , 1 ) with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained(UpperCAmelCase ) _lowercase =GenerationConfig.from_pretrained(UpperCAmelCase , temperature=1.0 ) self.assertEqual(loaded_config.temperature , 1.0 ) self.assertEqual(loaded_config.do_sample , UpperCAmelCase ) self.assertEqual(loaded_config.num_beams , 1 ) # default value @is_staging_test class lowerCamelCase__ ( unittest.TestCase): @classmethod def __A (cls ) -> str: _lowercase =TOKEN HfFolder.save_token(UpperCAmelCase ) @classmethod def __A (cls ) -> Tuple: try: delete_repo(token=cls._token , repo_id='''test-generation-config''' ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id='''valid_org/test-generation-config-org''' ) except HTTPError: pass def __A (self ) -> int: _lowercase =GenerationConfig( do_sample=UpperCAmelCase , temperature=0.7 , length_penalty=1.0 , ) config.push_to_hub('''test-generation-config''' , use_auth_token=self._token ) _lowercase =GenerationConfig.from_pretrained(f"{USER}/test-generation-config" ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(UpperCAmelCase , getattr(UpperCAmelCase , UpperCAmelCase ) ) # Reset repo delete_repo(token=self._token , repo_id='''test-generation-config''' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained( UpperCAmelCase , repo_id='''test-generation-config''' , push_to_hub=UpperCAmelCase , use_auth_token=self._token ) _lowercase =GenerationConfig.from_pretrained(f"{USER}/test-generation-config" ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(UpperCAmelCase , getattr(UpperCAmelCase , UpperCAmelCase ) ) def __A (self ) -> Optional[int]: _lowercase =GenerationConfig( do_sample=UpperCAmelCase , temperature=0.7 , length_penalty=1.0 , ) config.push_to_hub('''valid_org/test-generation-config-org''' , use_auth_token=self._token ) _lowercase =GenerationConfig.from_pretrained('''valid_org/test-generation-config-org''' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(UpperCAmelCase , getattr(UpperCAmelCase , UpperCAmelCase ) ) # Reset repo delete_repo(token=self._token , repo_id='''valid_org/test-generation-config-org''' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained( UpperCAmelCase , repo_id='''valid_org/test-generation-config-org''' , push_to_hub=UpperCAmelCase , use_auth_token=self._token ) _lowercase =GenerationConfig.from_pretrained('''valid_org/test-generation-config-org''' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(UpperCAmelCase , getattr(UpperCAmelCase , UpperCAmelCase ) )
5
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): def __A (self ) -> List[Any]: # clean up the VRAM after each test super().tearDown() gc.collect() def __A (self ) -> Optional[Any]: _lowercase =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/sd2-inpaint/init_image.png''' ) _lowercase =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png''' ) _lowercase ='''xvjiarui/stable-diffusion-2-inpainting''' _lowercase , _lowercase =FlaxStableDiffusionInpaintPipeline.from_pretrained(UpperCAmelCase , safety_checker=UpperCAmelCase ) _lowercase ='''Face of a yellow cat, high resolution, sitting on a park bench''' _lowercase =jax.random.PRNGKey(0 ) _lowercase =5_0 _lowercase =jax.device_count() _lowercase =num_samples * [prompt] _lowercase =num_samples * [init_image] _lowercase =num_samples * [mask_image] _lowercase , _lowercase , _lowercase =pipeline.prepare_inputs(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) # shard inputs and rng _lowercase =replicate(UpperCAmelCase ) _lowercase =jax.random.split(UpperCAmelCase , jax.device_count() ) _lowercase =shard(UpperCAmelCase ) _lowercase =shard(UpperCAmelCase ) _lowercase =shard(UpperCAmelCase ) _lowercase =pipeline( UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , jit=UpperCAmelCase ) _lowercase =output.images.reshape(UpperCAmelCase , 5_1_2 , 5_1_2 , 3 ) _lowercase =images[0, 2_5_3:2_5_6, 2_5_3:2_5_6, -1] _lowercase =jnp.asarray(jax.device_get(image_slice.flatten() ) ) _lowercase =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
5
1
from __future__ import annotations import unittest from transformers import XGLMConfig, XGLMTokenizer, 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 tensorflow as tf from transformers.models.xglm.modeling_tf_xglm import ( TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFXGLMForCausalLM, TFXGLMModel, ) @require_tf class lowerCamelCase__ : SCREAMING_SNAKE_CASE__ = XGLMConfig SCREAMING_SNAKE_CASE__ = {} SCREAMING_SNAKE_CASE__ = '''gelu''' def __init__(self , UpperCAmelCase , UpperCAmelCase=1_4 , UpperCAmelCase=7 , UpperCAmelCase=True , UpperCAmelCase=True , UpperCAmelCase=True , UpperCAmelCase=9_9 , UpperCAmelCase=3_2 , UpperCAmelCase=2 , UpperCAmelCase=4 , UpperCAmelCase=3_7 , UpperCAmelCase="gelu" , UpperCAmelCase=0.1 , UpperCAmelCase=0.1 , UpperCAmelCase=5_1_2 , UpperCAmelCase=0.02 , ) -> List[Any]: _lowercase =parent _lowercase =batch_size _lowercase =seq_length _lowercase =is_training _lowercase =use_input_mask _lowercase =use_labels _lowercase =vocab_size _lowercase =d_model _lowercase =num_hidden_layers _lowercase =num_attention_heads _lowercase =ffn_dim _lowercase =activation_function _lowercase =activation_dropout _lowercase =attention_dropout _lowercase =max_position_embeddings _lowercase =initializer_range _lowercase =None _lowercase =0 _lowercase =2 _lowercase =1 def __A (self ) -> List[str]: return XGLMConfig.from_pretrained('''facebook/xglm-564M''' ) def __A (self ) -> List[str]: _lowercase =tf.clip_by_value( ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) , clip_value_min=0 , clip_value_max=3 ) _lowercase =None if self.use_input_mask: _lowercase =random_attention_mask([self.batch_size, self.seq_length] ) _lowercase =self.get_config() _lowercase =floats_tensor([self.num_hidden_layers, self.num_attention_heads] , 2 ) return ( config, input_ids, input_mask, head_mask, ) def __A (self ) -> int: return XGLMConfig( vocab_size=self.vocab_size , d_model=self.hidden_size , num_layers=self.num_hidden_layers , attention_heads=self.num_attention_heads , ffn_dim=self.ffn_dim , activation_function=self.activation_function , activation_dropout=self.activation_dropout , attention_dropout=self.attention_dropout , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , use_cache=UpperCAmelCase , bos_token_id=self.bos_token_id , eos_token_id=self.eos_token_id , pad_token_id=self.pad_token_id , return_dict=UpperCAmelCase , ) def __A (self ) -> str: _lowercase =self.prepare_config_and_inputs() ( ( _lowercase ) , ( _lowercase ) , ( _lowercase ) , ( _lowercase ) , ) =config_and_inputs _lowercase ={ '''input_ids''': input_ids, '''head_mask''': head_mask, } return config, inputs_dict @require_tf class lowerCamelCase__ ( lowerCAmelCase , lowerCAmelCase , unittest.TestCase): SCREAMING_SNAKE_CASE__ = (TFXGLMModel, TFXGLMForCausalLM) if is_tf_available() else () SCREAMING_SNAKE_CASE__ = (TFXGLMForCausalLM,) if is_tf_available() else () SCREAMING_SNAKE_CASE__ = ( {'''feature-extraction''': TFXGLMModel, '''text-generation''': TFXGLMForCausalLM} if is_tf_available() else {} ) SCREAMING_SNAKE_CASE__ = False SCREAMING_SNAKE_CASE__ = False SCREAMING_SNAKE_CASE__ = False def __A (self ) -> Optional[Any]: _lowercase =TFXGLMModelTester(self ) _lowercase =ConfigTester(self , config_class=UpperCAmelCase , n_embd=3_7 ) def __A (self ) -> Tuple: self.config_tester.run_common_tests() @slow def __A (self ) -> str: for model_name in TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _lowercase =TFXGLMModel.from_pretrained(UpperCAmelCase ) self.assertIsNotNone(UpperCAmelCase ) @unittest.skip(reason='''Currently, model embeddings are going to undergo a major refactor.''' ) def __A (self ) -> Optional[Any]: super().test_resize_token_embeddings() @require_tf class lowerCamelCase__ ( unittest.TestCase): @slow def __A (self , UpperCAmelCase=True ) -> str: _lowercase =TFXGLMForCausalLM.from_pretrained('''facebook/xglm-564M''' ) _lowercase =tf.convert_to_tensor([[2, 2_6_8, 9_8_6_5]] , dtype=tf.intaa ) # The dog # </s> The dog is a very friendly dog. He is very affectionate and loves to play with other # fmt: off _lowercase =[2, 2_6_8, 9_8_6_5, 6_7, 1_1, 1_9_8_8, 5_7_2_5_2, 9_8_6_5, 5, 9_8_4, 6_7, 1_9_8_8, 2_1_3_8_3_8, 1_6_5_8, 5_3, 7_0_4_4_6, 3_3, 6_6_5_7, 2_7_8, 1_5_8_1] # fmt: on _lowercase =model.generate(UpperCAmelCase , do_sample=UpperCAmelCase , num_beams=1 ) if verify_outputs: self.assertListEqual(output_ids[0].numpy().tolist() , UpperCAmelCase ) @slow def __A (self ) -> int: _lowercase =XGLMTokenizer.from_pretrained('''facebook/xglm-564M''' ) _lowercase =TFXGLMForCausalLM.from_pretrained('''facebook/xglm-564M''' ) tf.random.set_seed(0 ) _lowercase =tokenizer('''Today is a nice day and''' , return_tensors='''tf''' ) _lowercase =tokenized.input_ids # forces the generation to happen on CPU, to avoid GPU-related quirks (and assure same output regardless of the available devices) with tf.device(''':/CPU:0''' ): _lowercase =model.generate(UpperCAmelCase , do_sample=UpperCAmelCase , seed=[7, 0] ) _lowercase =tokenizer.decode(output_ids[0] , skip_special_tokens=UpperCAmelCase ) _lowercase =( '''Today is a nice day and warm evening here over Southern Alberta!! Today when they closed schools due''' ) self.assertEqual(UpperCAmelCase , UpperCAmelCase ) @slow def __A (self ) -> str: _lowercase =TFXGLMForCausalLM.from_pretrained('''facebook/xglm-564M''' ) _lowercase =XGLMTokenizer.from_pretrained('''facebook/xglm-564M''' ) _lowercase ='''left''' # use different length sentences to test batching _lowercase =[ '''This is an extremelly long sentence that only exists to test the ability of the model to cope with ''' '''left-padding, such as in batched generation. The output for the sequence below should be the same ''' '''regardless of whether left padding is applied or not. When''', '''Hello, my dog is a little''', ] _lowercase =tokenizer(UpperCAmelCase , return_tensors='''tf''' , padding=UpperCAmelCase ) _lowercase =inputs['''input_ids'''] _lowercase =model.generate(input_ids=UpperCAmelCase , attention_mask=inputs['''attention_mask'''] , max_new_tokens=1_2 ) _lowercase =tokenizer(sentences[0] , return_tensors='''tf''' ).input_ids _lowercase =model.generate(input_ids=UpperCAmelCase , max_new_tokens=1_2 ) _lowercase =tokenizer(sentences[1] , return_tensors='''tf''' ).input_ids _lowercase =model.generate(input_ids=UpperCAmelCase , max_new_tokens=1_2 ) _lowercase =tokenizer.batch_decode(UpperCAmelCase , skip_special_tokens=UpperCAmelCase ) _lowercase =tokenizer.decode(output_non_padded[0] , skip_special_tokens=UpperCAmelCase ) _lowercase =tokenizer.decode(output_padded[0] , skip_special_tokens=UpperCAmelCase ) _lowercase =[ '''This is an extremelly long sentence that only exists to test the ability of the model to cope with ''' '''left-padding, such as in batched generation. The output for the sequence below should be the same ''' '''regardless of whether left padding is applied or not. When left padding is applied, the sequence will be ''' '''a single''', '''Hello, my dog is a little bit of a shy one, but he is very friendly''', ] self.assertListEqual(UpperCAmelCase , UpperCAmelCase ) self.assertListEqual(UpperCAmelCase , [non_padded_sentence, padded_sentence] )
5
import comet # From: unbabel-comet import torch import datasets UpperCAmelCase__ = datasets.logging.get_logger(__name__) UpperCAmelCase__ = '''\ @inproceedings{rei-EtAl:2020:WMT, author = {Rei, Ricardo and Stewart, Craig and Farinha, Ana C and Lavie, Alon}, title = {Unbabel\'s Participation in the WMT20 Metrics Shared Task}, booktitle = {Proceedings of the Fifth Conference on Machine Translation}, month = {November}, year = {2020}, address = {Online}, publisher = {Association for Computational Linguistics}, pages = {909--918}, } @inproceedings{rei-etal-2020-comet, title = "{COMET}: A Neural Framework for {MT} Evaluation", author = "Rei, Ricardo and Stewart, Craig and Farinha, Ana C and Lavie, Alon", booktitle = "Proceedings of the 2020 Conference on Empirical Methods in Natural Language Processing (EMNLP)", month = nov, year = "2020", address = "Online", publisher = "Association for Computational Linguistics", url = "https://www.aclweb.org/anthology/2020.emnlp-main.213", pages = "2685--2702", } ''' UpperCAmelCase__ = '''\ Crosslingual Optimized Metric for Evaluation of Translation (COMET) is an open-source framework used to train Machine Translation metrics that achieve high levels of correlation with different types of human judgments (HTER, DA\'s or MQM). With the release of the framework the authors also released fully trained models that were used to compete in the WMT20 Metrics Shared Task achieving SOTA in that years competition. See the [README.md] file at https://unbabel.github.io/COMET/html/models.html for more information. ''' UpperCAmelCase__ = ''' COMET score. Args: `sources` (list of str): Source sentences `predictions` (list of str): candidate translations `references` (list of str): reference translations `cuda` (bool): If set to True, runs COMET using GPU `show_progress` (bool): Shows progress `model`: COMET model to be used. Will default to `wmt-large-da-estimator-1719` if None. Returns: `samples`: List of dictionaries with `src`, `mt`, `ref` and `score`. `scores`: List of scores. Examples: >>> comet_metric = datasets.load_metric(\'comet\') >>> # comet_metric = load_metric(\'comet\', \'wmt20-comet-da\') # you can also choose which model to use >>> source = ["Dem Feuer konnte Einhalt geboten werden", "Schulen und Kindergärten wurden eröffnet."] >>> hypothesis = ["The fire could be stopped", "Schools and kindergartens were open"] >>> reference = ["They were able to control the fire.", "Schools and kindergartens opened"] >>> results = comet_metric.compute(predictions=hypothesis, references=reference, sources=source) >>> print([round(v, 2) for v in results["scores"]]) [0.19, 0.92] ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION) class lowerCamelCase__ ( datasets.Metric): def __A (self ) -> Optional[int]: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , homepage='''https://unbabel.github.io/COMET/html/index.html''' , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''sources''': datasets.Value('''string''' , id='''sequence''' ), '''predictions''': datasets.Value('''string''' , id='''sequence''' ), '''references''': datasets.Value('''string''' , id='''sequence''' ), } ) , codebase_urls=['''https://github.com/Unbabel/COMET'''] , reference_urls=[ '''https://github.com/Unbabel/COMET''', '''https://www.aclweb.org/anthology/2020.emnlp-main.213/''', '''http://www.statmt.org/wmt20/pdf/2020.wmt-1.101.pdf6''', ] , ) def __A (self , UpperCAmelCase ) -> Dict: if self.config_name == "default": _lowercase =comet.load_from_checkpoint(comet.download_model('''wmt20-comet-da''' ) ) else: _lowercase =comet.load_from_checkpoint(comet.download_model(self.config_name ) ) def __A (self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase=None , UpperCAmelCase=False ) -> int: if gpus is None: _lowercase =1 if torch.cuda.is_available() else 0 _lowercase ={'''src''': sources, '''mt''': predictions, '''ref''': references} _lowercase =[dict(zip(UpperCAmelCase , UpperCAmelCase ) ) for t in zip(*data.values() )] _lowercase , _lowercase =self.scorer.predict(UpperCAmelCase , gpus=UpperCAmelCase , progress_bar=UpperCAmelCase ) return {"mean_score": mean_score, "scores": scores}
5
1
import argparse import pickle import numpy as np import torch from torch import nn from transformers import ReformerConfig, ReformerModelWithLMHead from transformers.utils import logging logging.set_verbosity_info() def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case=None ) -> Tuple: """simple docstring""" assert torch_layer.weight.shape == weight.shape, F"{torch_layer} layer.weight does not match" _lowercase =nn.Parameter(__snake_case ) if bias is not None: assert torch_layer.bias.shape == bias.shape, F"{torch_layer} layer.bias does not match" _lowercase =nn.Parameter(__snake_case ) def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case ) -> Union[str, Any]: """simple docstring""" _lowercase =np.asarray(weights[0] ) _lowercase =np.asarray(weights[1] ) _lowercase =np.asarray(weights[2] ) set_param( torch_layer.self_attention.query_key , torch.tensor(__snake_case ).transpose(1 , 2 ).contiguous().view(-1 , __snake_case ) , ) set_param( torch_layer.self_attention.value , torch.tensor(__snake_case ).transpose(1 , 2 ).contiguous().view(-1 , __snake_case ) , ) set_param( torch_layer.output.dense , torch.tensor(__snake_case ).view(-1 , __snake_case ).contiguous().transpose(0 , 1 ) , ) def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case ) -> int: """simple docstring""" _lowercase =np.asarray(weights[0] ) _lowercase =np.asarray(weights[1] ) _lowercase =np.asarray(weights[2] ) _lowercase =np.asarray(weights[3] ) set_param( torch_layer.self_attention.query , torch.tensor(__snake_case ).transpose(1 , 2 ).contiguous().view(-1 , __snake_case ) , ) set_param( torch_layer.self_attention.key , torch.tensor(__snake_case ).transpose(1 , 2 ).contiguous().view(-1 , __snake_case ) , ) set_param( torch_layer.self_attention.value , torch.tensor(__snake_case ).transpose(1 , 2 ).contiguous().view(-1 , __snake_case ) , ) set_param( torch_layer.output.dense , torch.tensor(__snake_case ).view(-1 , __snake_case ).contiguous().transpose(0 , 1 ) , ) def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case ) -> Optional[Any]: """simple docstring""" _lowercase =weights[0][0][0] _lowercase =np.asarray(layer_norm_a[0] ) _lowercase =np.asarray(layer_norm_a[1] ) set_param( torch_block.attention.layer_norm , torch.tensor(__snake_case ) , torch.tensor(__snake_case ) , ) # lsh weights + output _lowercase =weights[0][1] if len(__snake_case ) < 4: set_layer_weights_in_torch_lsh(__snake_case , torch_block.attention , __snake_case ) else: set_layer_weights_in_torch_local(__snake_case , torch_block.attention , __snake_case ) # intermediate weighs _lowercase =weights[2][0][1][2] # Chunked Feed Forward if len(__snake_case ) == 4: _lowercase =intermediate_weights[2] # layernorm 2 _lowercase =np.asarray(intermediate_weights[0][0] ) _lowercase =np.asarray(intermediate_weights[0][1] ) set_param( torch_block.feed_forward.layer_norm , torch.tensor(__snake_case ) , torch.tensor(__snake_case ) , ) # intermediate dense _lowercase =np.asarray(intermediate_weights[1][0] ) _lowercase =np.asarray(intermediate_weights[1][1] ) set_param( torch_block.feed_forward.dense.dense , torch.tensor(__snake_case ).transpose(0 , 1 ).contiguous() , torch.tensor(__snake_case ) , ) # intermediate out _lowercase =np.asarray(intermediate_weights[4][0] ) _lowercase =np.asarray(intermediate_weights[4][1] ) set_param( torch_block.feed_forward.output.dense , torch.tensor(__snake_case ).transpose(0 , 1 ).contiguous() , torch.tensor(__snake_case ) , ) def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case ) -> List[Any]: """simple docstring""" _lowercase =torch_model.reformer # word embeds _lowercase =np.asarray(weights[1] ) set_param( torch_model_reformer.embeddings.word_embeddings , torch.tensor(__snake_case ) , ) if isinstance(weights[3] , __snake_case ): _lowercase =torch_model_reformer.embeddings.position_embeddings for emb_idx in range(len(position_embeddings.weights ) ): _lowercase =np.asarray(weights[3][emb_idx][0] ) assert ( position_embeddings.weights[emb_idx].shape == emb_weights.shape ), F"{position_embeddings[emb_idx]} emb does not match" _lowercase =nn.Parameter(torch.tensor(__snake_case ) ) _lowercase =weights[5] assert len(torch_model_reformer.encoder.layers ) * 4 == len( __snake_case ), "HF and trax model do not have the same number of layers" for layer_idx, layer in enumerate(torch_model_reformer.encoder.layers ): _lowercase =trax_layer_weights[4 * layer_idx : 4 * (layer_idx + 1)] set_block_weights_in_torch(__snake_case , __snake_case , __snake_case ) # output layer norm _lowercase =np.asarray(weights[7][0] ) _lowercase =np.asarray(weights[7][1] ) set_param( torch_model_reformer.encoder.layer_norm , torch.tensor(__snake_case ) , torch.tensor(__snake_case ) , ) # output embeddings _lowercase =np.asarray(weights[9][0] ) _lowercase =np.asarray(weights[9][1] ) set_param( torch_model.lm_head.decoder , torch.tensor(__snake_case ).transpose(0 , 1 ).contiguous() , torch.tensor(__snake_case ) , ) def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case ) -> Any: """simple docstring""" _lowercase =ReformerConfig.from_json_file(__snake_case ) print(F"Building PyTorch model from configuration: {config}" ) _lowercase =ReformerModelWithLMHead(__snake_case ) with open(__snake_case , '''rb''' ) as f: _lowercase =pickle.load(__snake_case )['''weights'''] set_model_weights_in_torch(__snake_case , __snake_case , config.hidden_size ) # Save pytorch-model print(F"Save PyTorch model to {pytorch_dump_path}" ) torch.save(model.state_dict() , __snake_case ) if __name__ == "__main__": UpperCAmelCase__ = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--trax_model_pkl_path''', default=None, type=str, required=True, help='''Path to the TensorFlow checkpoint path.''' ) parser.add_argument( '''--config_file''', default=None, type=str, required=True, help=( '''The config json file corresponding to the pre-trained Reformer model. \n''' '''This specifies the model architecture.''' ), ) parser.add_argument( '''--pytorch_dump_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.''' ) UpperCAmelCase__ = parser.parse_args() convert_trax_checkpoint_to_pytorch(args.trax_model_pkl_path, args.config_file, args.pytorch_dump_path)
5
# DISCLAIMER: This file is strongly influenced by https://github.com/yang-song/score_sde_pytorch import math from typing import Union import torch from ..configuration_utils import ConfigMixin, register_to_config from ..utils import randn_tensor from .scheduling_utils import SchedulerMixin class lowerCamelCase__ ( lowerCAmelCase , lowerCAmelCase): SCREAMING_SNAKE_CASE__ = 1 @register_to_config def __init__(self , UpperCAmelCase=2_0_0_0 , UpperCAmelCase=0.1 , UpperCAmelCase=2_0 , UpperCAmelCase=1e-3 ) -> List[str]: _lowercase =None _lowercase =None _lowercase =None def __A (self , UpperCAmelCase , UpperCAmelCase = None ) -> str: _lowercase =torch.linspace(1 , self.config.sampling_eps , UpperCAmelCase , device=UpperCAmelCase ) def __A (self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase=None ) -> Optional[int]: if self.timesteps is None: raise ValueError( '''`self.timesteps` is not set, you need to run \'set_timesteps\' after creating the scheduler''' ) # TODO(Patrick) better comments + non-PyTorch # postprocess model score _lowercase =( -0.25 * t**2 * (self.config.beta_max - self.config.beta_min) - 0.5 * t * self.config.beta_min ) _lowercase =torch.sqrt(1.0 - torch.exp(2.0 * log_mean_coeff ) ) _lowercase =std.flatten() while len(std.shape ) < len(score.shape ): _lowercase =std.unsqueeze(-1 ) _lowercase =-score / std # compute _lowercase =-1.0 / len(self.timesteps ) _lowercase =self.config.beta_min + t * (self.config.beta_max - self.config.beta_min) _lowercase =beta_t.flatten() while len(beta_t.shape ) < len(x.shape ): _lowercase =beta_t.unsqueeze(-1 ) _lowercase =-0.5 * beta_t * x _lowercase =torch.sqrt(UpperCAmelCase ) _lowercase =drift - diffusion**2 * score _lowercase =x + drift * dt # add noise _lowercase =randn_tensor(x.shape , layout=x.layout , generator=UpperCAmelCase , device=x.device , dtype=x.dtype ) _lowercase =x_mean + diffusion * math.sqrt(-dt ) * noise return x, x_mean def __len__(self ) -> str: return self.config.num_train_timesteps
5
1
from __future__ import annotations import math def UpperCAmelCase_ ( __snake_case ) -> bool: """simple docstring""" if 1 < number < 4: # 2 and 3 are primes return True elif number < 2 or number % 2 == 0 or number % 3 == 0: # Negatives, 0, 1, all even numbers, all multiples of 3 are not primes return False # All primes number are in format of 6k +/- 1 for i in range(5 , int(math.sqrt(__snake_case ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True UpperCAmelCase__ = [num for num in range(3, 10_0001, 2) if not is_prime(num)] def UpperCAmelCase_ ( __snake_case ) -> list[int]: """simple docstring""" if not isinstance(__snake_case , __snake_case ): raise ValueError('''n must be an integer''' ) if n <= 0: raise ValueError('''n must be >= 0''' ) _lowercase =[] for num in range(len(__snake_case ) ): _lowercase =0 while 2 * i * i <= odd_composites[num]: _lowercase =odd_composites[num] - 2 * i * i if is_prime(__snake_case ): break i += 1 else: list_nums.append(odd_composites[num] ) if len(__snake_case ) == n: return list_nums return [] def UpperCAmelCase_ ( ) -> int: """simple docstring""" return compute_nums(1 )[0] if __name__ == "__main__": print(f'''{solution() = }''')
5
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( MobileViTConfig, MobileViTForImageClassification, MobileViTForSemanticSegmentation, MobileViTImageProcessor, ) from transformers.utils import logging logging.set_verbosity_info() UpperCAmelCase__ = logging.get_logger(__name__) def UpperCAmelCase_ ( __snake_case ) -> Optional[Any]: """simple docstring""" _lowercase =MobileViTConfig() # size of the architecture if "mobilevit_s" in mobilevit_name: _lowercase =[144, 192, 240] _lowercase =[16, 32, 64, 96, 128, 160, 640] elif "mobilevit_xs" in mobilevit_name: _lowercase =[96, 120, 144] _lowercase =[16, 32, 48, 64, 80, 96, 384] elif "mobilevit_xxs" in mobilevit_name: _lowercase =[64, 80, 96] _lowercase =[16, 16, 24, 48, 64, 80, 320] _lowercase =0.05 _lowercase =2.0 if mobilevit_name.startswith('''deeplabv3_''' ): _lowercase =512 _lowercase =16 _lowercase =21 _lowercase ='''pascal-voc-id2label.json''' else: _lowercase =1000 _lowercase ='''imagenet-1k-id2label.json''' _lowercase ='''huggingface/label-files''' _lowercase =json.load(open(hf_hub_download(__snake_case , __snake_case , repo_type='''dataset''' ) , '''r''' ) ) _lowercase ={int(__snake_case ): v for k, v in idalabel.items()} _lowercase =idalabel _lowercase ={v: k for k, v in idalabel.items()} return config def UpperCAmelCase_ ( __snake_case , __snake_case=False ) -> Tuple: """simple docstring""" for i in range(1 , 6 ): if F"layer_{i}." in name: _lowercase =name.replace(F"layer_{i}." , F"encoder.layer.{i - 1}." ) if "conv_1." in name: _lowercase =name.replace('''conv_1.''' , '''conv_stem.''' ) if ".block." in name: _lowercase =name.replace('''.block.''' , '''.''' ) if "exp_1x1" in name: _lowercase =name.replace('''exp_1x1''' , '''expand_1x1''' ) if "red_1x1" in name: _lowercase =name.replace('''red_1x1''' , '''reduce_1x1''' ) if ".local_rep.conv_3x3." in name: _lowercase =name.replace('''.local_rep.conv_3x3.''' , '''.conv_kxk.''' ) if ".local_rep.conv_1x1." in name: _lowercase =name.replace('''.local_rep.conv_1x1.''' , '''.conv_1x1.''' ) if ".norm." in name: _lowercase =name.replace('''.norm.''' , '''.normalization.''' ) if ".conv." in name: _lowercase =name.replace('''.conv.''' , '''.convolution.''' ) if ".conv_proj." in name: _lowercase =name.replace('''.conv_proj.''' , '''.conv_projection.''' ) for i in range(0 , 2 ): for j in range(0 , 4 ): if F".{i}.{j}." in name: _lowercase =name.replace(F".{i}.{j}." , F".{i}.layer.{j}." ) for i in range(2 , 6 ): for j in range(0 , 4 ): if F".{i}.{j}." in name: _lowercase =name.replace(F".{i}.{j}." , F".{i}." ) if "expand_1x1" in name: _lowercase =name.replace('''expand_1x1''' , '''downsampling_layer.expand_1x1''' ) if "conv_3x3" in name: _lowercase =name.replace('''conv_3x3''' , '''downsampling_layer.conv_3x3''' ) if "reduce_1x1" in name: _lowercase =name.replace('''reduce_1x1''' , '''downsampling_layer.reduce_1x1''' ) for i in range(2 , 5 ): if F".global_rep.{i}.weight" in name: _lowercase =name.replace(F".global_rep.{i}.weight" , '''.layernorm.weight''' ) if F".global_rep.{i}.bias" in name: _lowercase =name.replace(F".global_rep.{i}.bias" , '''.layernorm.bias''' ) if ".global_rep." in name: _lowercase =name.replace('''.global_rep.''' , '''.transformer.''' ) if ".pre_norm_mha.0." in name: _lowercase =name.replace('''.pre_norm_mha.0.''' , '''.layernorm_before.''' ) if ".pre_norm_mha.1.out_proj." in name: _lowercase =name.replace('''.pre_norm_mha.1.out_proj.''' , '''.attention.output.dense.''' ) if ".pre_norm_ffn.0." in name: _lowercase =name.replace('''.pre_norm_ffn.0.''' , '''.layernorm_after.''' ) if ".pre_norm_ffn.1." in name: _lowercase =name.replace('''.pre_norm_ffn.1.''' , '''.intermediate.dense.''' ) if ".pre_norm_ffn.4." in name: _lowercase =name.replace('''.pre_norm_ffn.4.''' , '''.output.dense.''' ) if ".transformer." in name: _lowercase =name.replace('''.transformer.''' , '''.transformer.layer.''' ) if ".aspp_layer." in name: _lowercase =name.replace('''.aspp_layer.''' , '''.''' ) if ".aspp_pool." in name: _lowercase =name.replace('''.aspp_pool.''' , '''.''' ) if "seg_head." in name: _lowercase =name.replace('''seg_head.''' , '''segmentation_head.''' ) if "segmentation_head.classifier.classifier." in name: _lowercase =name.replace('''segmentation_head.classifier.classifier.''' , '''segmentation_head.classifier.''' ) if "classifier.fc." in name: _lowercase =name.replace('''classifier.fc.''' , '''classifier.''' ) elif (not base_model) and ("segmentation_head." not in name): _lowercase ='''mobilevit.''' + name return name def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case=False ) -> Optional[Any]: """simple docstring""" if base_model: _lowercase ='''''' else: _lowercase ='''mobilevit.''' for key in orig_state_dict.copy().keys(): _lowercase =orig_state_dict.pop(__snake_case ) if key[:8] == "encoder.": _lowercase =key[8:] if "qkv" in key: _lowercase =key.split('''.''' ) _lowercase =int(key_split[0][6:] ) - 1 _lowercase =int(key_split[3] ) _lowercase =model.get_submodule(F"{model_prefix}encoder.layer.{layer_num}" ) _lowercase =layer.transformer.layer[transformer_num].attention.attention.all_head_size _lowercase =( F"{model_prefix}encoder.layer.{layer_num}.transformer.layer.{transformer_num}.attention.attention." ) if "weight" in key: _lowercase =val[:dim, :] _lowercase =val[dim : dim * 2, :] _lowercase =val[-dim:, :] else: _lowercase =val[:dim] _lowercase =val[dim : dim * 2] _lowercase =val[-dim:] else: _lowercase =val return orig_state_dict def UpperCAmelCase_ ( ) -> Union[str, Any]: """simple docstring""" _lowercase ='''http://images.cocodataset.org/val2017/000000039769.jpg''' _lowercase =Image.open(requests.get(__snake_case , stream=__snake_case ).raw ) return im @torch.no_grad() def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case=False ) -> int: """simple docstring""" _lowercase =get_mobilevit_config(__snake_case ) # load original state_dict _lowercase =torch.load(__snake_case , map_location='''cpu''' ) # load 🤗 model if mobilevit_name.startswith('''deeplabv3_''' ): _lowercase =MobileViTForSemanticSegmentation(__snake_case ).eval() else: _lowercase =MobileViTForImageClassification(__snake_case ).eval() _lowercase =convert_state_dict(__snake_case , __snake_case ) model.load_state_dict(__snake_case ) # Check outputs on an image, prepared by MobileViTImageProcessor _lowercase =MobileViTImageProcessor(crop_size=config.image_size , size=config.image_size + 32 ) _lowercase =image_processor(images=prepare_img() , return_tensors='''pt''' ) _lowercase =model(**__snake_case ) _lowercase =outputs.logits if mobilevit_name.startswith('''deeplabv3_''' ): assert logits.shape == (1, 21, 32, 32) if mobilevit_name == "deeplabv3_mobilevit_s": _lowercase =torch.tensor( [ [[6.20_65, 6.12_92, 6.20_70], [6.10_79, 6.12_54, 6.17_47], [6.00_42, 6.10_71, 6.10_34]], [[-6.92_53, -6.86_53, -7.03_98], [-7.32_18, -7.39_83, -7.36_70], [-7.19_61, -7.24_82, -7.15_69]], [[-4.47_23, -4.43_48, -4.37_69], [-5.36_29, -5.46_32, -5.45_98], [-5.15_87, -5.34_02, -5.50_59]], ] ) elif mobilevit_name == "deeplabv3_mobilevit_xs": _lowercase =torch.tensor( [ [[5.44_49, 5.57_33, 5.63_14], [5.18_15, 5.39_30, 5.59_63], [5.16_56, 5.43_33, 5.48_53]], [[-9.44_23, -9.77_66, -9.67_14], [-9.15_81, -9.57_20, -9.55_19], [-9.10_06, -9.64_58, -9.57_03]], [[-7.77_21, -7.37_16, -7.15_83], [-8.45_99, -8.06_24, -7.79_44], [-8.41_72, -7.83_66, -7.50_25]], ] ) elif mobilevit_name == "deeplabv3_mobilevit_xxs": _lowercase =torch.tensor( [ [[6.98_11, 6.97_43, 7.31_23], [7.17_77, 7.19_31, 7.39_38], [7.56_33, 7.80_50, 7.89_01]], [[-10.55_36, -10.23_32, -10.29_24], [-10.23_36, -9.86_24, -9.59_64], [-10.88_40, -10.81_58, -10.66_59]], [[-3.49_38, -3.06_31, -2.86_20], [-3.42_05, -2.81_35, -2.68_75], [-3.41_79, -2.79_45, -2.87_50]], ] ) else: raise ValueError(F"Unknown mobilevit_name: {mobilevit_name}" ) assert torch.allclose(logits[0, :3, :3, :3] , __snake_case , atol=1e-4 ) else: assert logits.shape == (1, 1000) if mobilevit_name == "mobilevit_s": _lowercase =torch.tensor([-0.98_66, 0.23_92, -1.12_41] ) elif mobilevit_name == "mobilevit_xs": _lowercase =torch.tensor([-2.47_61, -0.93_99, -1.95_87] ) elif mobilevit_name == "mobilevit_xxs": _lowercase =torch.tensor([-1.93_64, -1.23_27, -0.46_53] ) else: raise ValueError(F"Unknown mobilevit_name: {mobilevit_name}" ) assert torch.allclose(logits[0, :3] , __snake_case , atol=1e-4 ) Path(__snake_case ).mkdir(exist_ok=__snake_case ) print(F"Saving model {mobilevit_name} to {pytorch_dump_folder_path}" ) model.save_pretrained(__snake_case ) print(F"Saving image processor to {pytorch_dump_folder_path}" ) image_processor.save_pretrained(__snake_case ) if push_to_hub: _lowercase ={ '''mobilevit_s''': '''mobilevit-small''', '''mobilevit_xs''': '''mobilevit-x-small''', '''mobilevit_xxs''': '''mobilevit-xx-small''', '''deeplabv3_mobilevit_s''': '''deeplabv3-mobilevit-small''', '''deeplabv3_mobilevit_xs''': '''deeplabv3-mobilevit-x-small''', '''deeplabv3_mobilevit_xxs''': '''deeplabv3-mobilevit-xx-small''', } print('''Pushing to the hub...''' ) _lowercase =model_mapping[mobilevit_name] image_processor.push_to_hub(__snake_case , organization='''apple''' ) model.push_to_hub(__snake_case , organization='''apple''' ) if __name__ == "__main__": UpperCAmelCase__ = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--mobilevit_name''', default='''mobilevit_s''', type=str, help=( '''Name of the MobileViT model you\'d like to convert. Should be one of \'mobilevit_s\', \'mobilevit_xs\',''' ''' \'mobilevit_xxs\', \'deeplabv3_mobilevit_s\', \'deeplabv3_mobilevit_xs\', \'deeplabv3_mobilevit_xxs\'.''' ), ) parser.add_argument( '''--checkpoint_path''', required=True, type=str, help='''Path to the original state dict (.pt 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.mobilevit_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub )
5
1
import math from collections import defaultdict from typing import List, Optional, Tuple, Union import numpy as np import torch from ..configuration_utils import ConfigMixin, register_to_config from .scheduling_utils import KarrasDiffusionSchedulers, SchedulerMixin, SchedulerOutput def UpperCAmelCase_ ( __snake_case , __snake_case=0.9_99 , __snake_case="cosine" , ) -> Union[str, Any]: """simple docstring""" if alpha_transform_type == "cosine": def alpha_bar_fn(__snake_case ): return math.cos((t + 0.0_08) / 1.0_08 * math.pi / 2 ) ** 2 elif alpha_transform_type == "exp": def alpha_bar_fn(__snake_case ): return math.exp(t * -12.0 ) else: raise ValueError(F"Unsupported alpha_tranform_type: {alpha_transform_type}" ) _lowercase =[] for i in range(__snake_case ): _lowercase =i / num_diffusion_timesteps _lowercase =(i + 1) / num_diffusion_timesteps betas.append(min(1 - alpha_bar_fn(__snake_case ) / alpha_bar_fn(__snake_case ) , __snake_case ) ) return torch.tensor(__snake_case , dtype=torch.floataa ) class lowerCamelCase__ ( lowerCAmelCase , lowerCAmelCase): SCREAMING_SNAKE_CASE__ = [e.name for e in KarrasDiffusionSchedulers] SCREAMING_SNAKE_CASE__ = 2 @register_to_config def __init__(self , UpperCAmelCase = 1_0_0_0 , UpperCAmelCase = 0.0_0085 , UpperCAmelCase = 0.012 , UpperCAmelCase = "linear" , UpperCAmelCase = None , UpperCAmelCase = "epsilon" , UpperCAmelCase = False , UpperCAmelCase = False , UpperCAmelCase = 1.0 , UpperCAmelCase = "linspace" , UpperCAmelCase = 0 , ) -> List[str]: if trained_betas is not None: _lowercase =torch.tensor(UpperCAmelCase , dtype=torch.floataa ) elif beta_schedule == "linear": _lowercase =torch.linspace(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , dtype=torch.floataa ) elif beta_schedule == "scaled_linear": # this schedule is very specific to the latent diffusion model. _lowercase =( torch.linspace(beta_start**0.5 , beta_end**0.5 , UpperCAmelCase , dtype=torch.floataa ) ** 2 ) elif beta_schedule == "squaredcos_cap_v2": # Glide cosine schedule _lowercase =betas_for_alpha_bar(UpperCAmelCase , alpha_transform_type='''cosine''' ) elif beta_schedule == "exp": _lowercase =betas_for_alpha_bar(UpperCAmelCase , alpha_transform_type='''exp''' ) else: raise NotImplementedError(f"{beta_schedule} does is not implemented for {self.__class__}" ) _lowercase =1.0 - self.betas _lowercase =torch.cumprod(self.alphas , dim=0 ) # set all values self.set_timesteps(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) _lowercase =use_karras_sigmas def __A (self , UpperCAmelCase , UpperCAmelCase=None ) -> Dict: if schedule_timesteps is None: _lowercase =self.timesteps _lowercase =(schedule_timesteps == timestep).nonzero() # The sigma index that is taken for the **very** first `step` # is always the second index (or the last index if there is only 1) # This way we can ensure we don't accidentally skip a sigma in # case we start in the middle of the denoising schedule (e.g. for image-to-image) if len(self._index_counter ) == 0: _lowercase =1 if len(UpperCAmelCase ) > 1 else 0 else: _lowercase =timestep.cpu().item() if torch.is_tensor(UpperCAmelCase ) else timestep _lowercase =self._index_counter[timestep_int] return indices[pos].item() @property def __A (self ) -> str: # standard deviation of the initial noise distribution if self.config.timestep_spacing in ["linspace", "trailing"]: return self.sigmas.max() return (self.sigmas.max() ** 2 + 1) ** 0.5 def __A (self , UpperCAmelCase , UpperCAmelCase , ) -> torch.FloatTensor: _lowercase =self.index_for_timestep(UpperCAmelCase ) _lowercase =self.sigmas[step_index] _lowercase =sample / ((sigma**2 + 1) ** 0.5) return sample def __A (self , UpperCAmelCase , UpperCAmelCase = None , UpperCAmelCase = None , ) -> int: _lowercase =num_inference_steps _lowercase =num_train_timesteps or self.config.num_train_timesteps # "linspace", "leading", "trailing" corresponds to annotation of Table 2. of https://arxiv.org/abs/2305.08891 if self.config.timestep_spacing == "linspace": _lowercase =np.linspace(0 , num_train_timesteps - 1 , UpperCAmelCase , dtype=UpperCAmelCase )[::-1].copy() elif self.config.timestep_spacing == "leading": _lowercase =num_train_timesteps // self.num_inference_steps # creates integer timesteps by multiplying by ratio # casting to int to avoid issues when num_inference_step is power of 3 _lowercase =(np.arange(0 , UpperCAmelCase ) * step_ratio).round()[::-1].copy().astype(UpperCAmelCase ) timesteps += self.config.steps_offset elif self.config.timestep_spacing == "trailing": _lowercase =num_train_timesteps / self.num_inference_steps # creates integer timesteps by multiplying by ratio # casting to int to avoid issues when num_inference_step is power of 3 _lowercase =(np.arange(UpperCAmelCase , 0 , -step_ratio )).round().copy().astype(UpperCAmelCase ) timesteps -= 1 else: raise ValueError( f"{self.config.timestep_spacing} is not supported. Please make sure to choose one of 'linspace', 'leading' or 'trailing'." ) _lowercase =np.array(((1 - self.alphas_cumprod) / self.alphas_cumprod) ** 0.5 ) _lowercase =np.log(UpperCAmelCase ) _lowercase =np.interp(UpperCAmelCase , np.arange(0 , len(UpperCAmelCase ) ) , UpperCAmelCase ) if self.config.use_karras_sigmas: _lowercase =self._convert_to_karras(in_sigmas=UpperCAmelCase , num_inference_steps=self.num_inference_steps ) _lowercase =np.array([self._sigma_to_t(UpperCAmelCase , UpperCAmelCase ) for sigma in sigmas] ) _lowercase =np.concatenate([sigmas, [0.0]] ).astype(np.floataa ) _lowercase =torch.from_numpy(UpperCAmelCase ).to(device=UpperCAmelCase ) _lowercase =torch.cat([sigmas[:1], sigmas[1:-1].repeat_interleave(2 ), sigmas[-1:]] ) _lowercase =torch.from_numpy(UpperCAmelCase ) _lowercase =torch.cat([timesteps[:1], timesteps[1:].repeat_interleave(2 )] ) if str(UpperCAmelCase ).startswith('''mps''' ): # mps does not support float64 _lowercase =timesteps.to(UpperCAmelCase , dtype=torch.floataa ) else: _lowercase =timesteps.to(device=UpperCAmelCase ) # empty dt and derivative _lowercase =None _lowercase =None # for exp beta schedules, such as the one for `pipeline_shap_e.py` # we need an index counter _lowercase =defaultdict(UpperCAmelCase ) def __A (self , UpperCAmelCase , UpperCAmelCase ) -> Optional[int]: # get log sigma _lowercase =np.log(UpperCAmelCase ) # get distribution _lowercase =log_sigma - log_sigmas[:, np.newaxis] # get sigmas range _lowercase =np.cumsum((dists >= 0) , axis=0 ).argmax(axis=0 ).clip(max=log_sigmas.shape[0] - 2 ) _lowercase =low_idx + 1 _lowercase =log_sigmas[low_idx] _lowercase =log_sigmas[high_idx] # interpolate sigmas _lowercase =(low - log_sigma) / (low - high) _lowercase =np.clip(UpperCAmelCase , 0 , 1 ) # transform interpolation to time range _lowercase =(1 - w) * low_idx + w * high_idx _lowercase =t.reshape(sigma.shape ) return t def __A (self , UpperCAmelCase , UpperCAmelCase ) -> torch.FloatTensor: _lowercase =in_sigmas[-1].item() _lowercase =in_sigmas[0].item() _lowercase =7.0 # 7.0 is the value used in the paper _lowercase =np.linspace(0 , 1 , UpperCAmelCase ) _lowercase =sigma_min ** (1 / rho) _lowercase =sigma_max ** (1 / rho) _lowercase =(max_inv_rho + ramp * (min_inv_rho - max_inv_rho)) ** rho return sigmas @property def __A (self ) -> List[Any]: return self.dt is None def __A (self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = True , ) -> Union[SchedulerOutput, Tuple]: _lowercase =self.index_for_timestep(UpperCAmelCase ) # advance index counter by 1 _lowercase =timestep.cpu().item() if torch.is_tensor(UpperCAmelCase ) else timestep self._index_counter[timestep_int] += 1 if self.state_in_first_order: _lowercase =self.sigmas[step_index] _lowercase =self.sigmas[step_index + 1] else: # 2nd order / Heun's method _lowercase =self.sigmas[step_index - 1] _lowercase =self.sigmas[step_index] # currently only gamma=0 is supported. This usually works best anyways. # We can support gamma in the future but then need to scale the timestep before # passing it to the model which requires a change in API _lowercase =0 _lowercase =sigma * (gamma + 1) # Note: sigma_hat == sigma for now # 1. compute predicted original sample (x_0) from sigma-scaled predicted noise if self.config.prediction_type == "epsilon": _lowercase =sigma_hat if self.state_in_first_order else sigma_next _lowercase =sample - sigma_input * model_output elif self.config.prediction_type == "v_prediction": _lowercase =sigma_hat if self.state_in_first_order else sigma_next _lowercase =model_output * (-sigma_input / (sigma_input**2 + 1) ** 0.5) + ( sample / (sigma_input**2 + 1) ) elif self.config.prediction_type == "sample": _lowercase =model_output else: raise ValueError( f"prediction_type given as {self.config.prediction_type} must be one of `epsilon`, or `v_prediction`" ) if self.config.clip_sample: _lowercase =pred_original_sample.clamp( -self.config.clip_sample_range , self.config.clip_sample_range ) if self.state_in_first_order: # 2. Convert to an ODE derivative for 1st order _lowercase =(sample - pred_original_sample) / sigma_hat # 3. delta timestep _lowercase =sigma_next - sigma_hat # store for 2nd order step _lowercase =derivative _lowercase =dt _lowercase =sample else: # 2. 2nd order / Heun's method _lowercase =(sample - pred_original_sample) / sigma_next _lowercase =(self.prev_derivative + derivative) / 2 # 3. take prev timestep & sample _lowercase =self.dt _lowercase =self.sample # free dt and derivative # Note, this puts the scheduler in "first order mode" _lowercase =None _lowercase =None _lowercase =None _lowercase =sample + derivative * dt if not return_dict: return (prev_sample,) return SchedulerOutput(prev_sample=UpperCAmelCase ) def __A (self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , ) -> torch.FloatTensor: # Make sure sigmas and timesteps have the same device and dtype as original_samples _lowercase =self.sigmas.to(device=original_samples.device , dtype=original_samples.dtype ) if original_samples.device.type == "mps" and torch.is_floating_point(UpperCAmelCase ): # mps does not support float64 _lowercase =self.timesteps.to(original_samples.device , dtype=torch.floataa ) _lowercase =timesteps.to(original_samples.device , dtype=torch.floataa ) else: _lowercase =self.timesteps.to(original_samples.device ) _lowercase =timesteps.to(original_samples.device ) _lowercase =[self.index_for_timestep(UpperCAmelCase , UpperCAmelCase ) for t in timesteps] _lowercase =sigmas[step_indices].flatten() while len(sigma.shape ) < len(original_samples.shape ): _lowercase =sigma.unsqueeze(-1 ) _lowercase =original_samples + noise * sigma return noisy_samples def __len__(self ) -> Tuple: return self.config.num_train_timesteps
5
import requests from bsa import BeautifulSoup def UpperCAmelCase_ ( __snake_case = "https://www.worldometers.info/coronavirus" ) -> dict: """simple docstring""" _lowercase =BeautifulSoup(requests.get(__snake_case ).text , '''html.parser''' ) _lowercase =soup.findAll('''h1''' ) _lowercase =soup.findAll('''div''' , {'''class''': '''maincounter-number'''} ) keys += soup.findAll('''span''' , {'''class''': '''panel-title'''} ) values += soup.findAll('''div''' , {'''class''': '''number-table-main'''} ) return {key.text.strip(): value.text.strip() for key, value in zip(__snake_case , __snake_case )} if __name__ == "__main__": print('''\033[1m''' + '''COVID-19 Status of the World''' + '''\033[0m\n''') for key, value in world_covidaa_stats().items(): print(f'''{key}\n{value}\n''')
5
1
from __future__ import annotations from bisect import bisect_left from functools import total_ordering from heapq import merge @total_ordering class lowerCamelCase__ ( lowerCAmelCase): def __lt__(self , UpperCAmelCase ) -> Union[str, Any]: return self[-1] < other[-1] def __eq__(self , UpperCAmelCase ) -> List[Any]: return self[-1] == other[-1] def UpperCAmelCase_ ( __snake_case ) -> list: """simple docstring""" _lowercase =[] # sort into stacks for element in collection: _lowercase =Stack([element] ) _lowercase =bisect_left(__snake_case , __snake_case ) if i != len(__snake_case ): stacks[i].append(__snake_case ) else: stacks.append(__snake_case ) # use a heap-based merge to merge stack efficiently _lowercase =merge(*(reversed(__snake_case ) for stack in stacks) ) return collection if __name__ == "__main__": UpperCAmelCase__ = input('''Enter numbers separated by a comma:\n''').strip() UpperCAmelCase__ = [int(item) for item in user_input.split(''',''')] print(patience_sort(unsorted))
5
from typing import TYPE_CHECKING from ..utils import _LazyModule UpperCAmelCase__ = { '''config''': [ '''EXTERNAL_DATA_FORMAT_SIZE_LIMIT''', '''OnnxConfig''', '''OnnxConfigWithPast''', '''OnnxSeq2SeqConfigWithPast''', '''PatchingSpec''', ], '''convert''': ['''export''', '''validate_model_outputs'''], '''features''': ['''FeaturesManager'''], '''utils''': ['''ParameterFormat''', '''compute_serialized_parameters_size'''], } if TYPE_CHECKING: from .config import ( EXTERNAL_DATA_FORMAT_SIZE_LIMIT, OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast, PatchingSpec, ) from .convert import export, validate_model_outputs from .features import FeaturesManager from .utils import ParameterFormat, compute_serialized_parameters_size else: import sys UpperCAmelCase__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
5
1
from typing import Callable, List, Optional, Tuple, Union import torch from transformers import CLIPTextModel, CLIPTokenizer from ...configuration_utils import ConfigMixin, register_to_config from ...models import ModelMixin, TransformeraDModel, VQModel from ...schedulers import VQDiffusionScheduler from ...utils import logging from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput UpperCAmelCase__ = logging.get_logger(__name__) # pylint: disable=invalid-name class lowerCamelCase__ ( lowerCAmelCase , lowerCAmelCase): @register_to_config def __init__(self , UpperCAmelCase , UpperCAmelCase = None , UpperCAmelCase = None ) -> str: super().__init__() _lowercase =learnable if self.learnable: assert hidden_size is not None, "learnable=True requires `hidden_size` to be set" assert length is not None, "learnable=True requires `length` to be set" _lowercase =torch.zeros(UpperCAmelCase , UpperCAmelCase ) else: _lowercase =None _lowercase =torch.nn.Parameter(UpperCAmelCase ) class lowerCamelCase__ ( lowerCAmelCase): SCREAMING_SNAKE_CASE__ = 42 SCREAMING_SNAKE_CASE__ = 42 SCREAMING_SNAKE_CASE__ = 42 SCREAMING_SNAKE_CASE__ = 42 SCREAMING_SNAKE_CASE__ = 42 SCREAMING_SNAKE_CASE__ = 42 def __init__(self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , ) -> int: super().__init__() self.register_modules( vqvae=UpperCAmelCase , transformer=UpperCAmelCase , text_encoder=UpperCAmelCase , tokenizer=UpperCAmelCase , scheduler=UpperCAmelCase , learned_classifier_free_sampling_embeddings=UpperCAmelCase , ) def __A (self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) -> Union[str, Any]: _lowercase =len(UpperCAmelCase ) if isinstance(UpperCAmelCase , UpperCAmelCase ) else 1 # get prompt text embeddings _lowercase =self.tokenizer( UpperCAmelCase , padding='''max_length''' , max_length=self.tokenizer.model_max_length , return_tensors='''pt''' , ) _lowercase =text_inputs.input_ids if text_input_ids.shape[-1] > self.tokenizer.model_max_length: _lowercase =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}" ) _lowercase =text_input_ids[:, : self.tokenizer.model_max_length] _lowercase =self.text_encoder(text_input_ids.to(self.device ) )[0] # NOTE: This additional step of normalizing the text embeddings is from VQ-Diffusion. # While CLIP does normalize the pooled output of the text transformer when combining # the image and text embeddings, CLIP does not directly normalize the last hidden state. # # CLIP normalizing the pooled output. # https://github.com/huggingface/transformers/blob/d92e22d1f28324f513f3080e5c47c071a3916721/src/transformers/models/clip/modeling_clip.py#L1052-L1053 _lowercase =prompt_embeds / prompt_embeds.norm(dim=-1 , keepdim=UpperCAmelCase ) # duplicate text embeddings for each generation per prompt _lowercase =prompt_embeds.repeat_interleave(UpperCAmelCase , dim=0 ) if do_classifier_free_guidance: if self.learned_classifier_free_sampling_embeddings.learnable: _lowercase =self.learned_classifier_free_sampling_embeddings.embeddings _lowercase =negative_prompt_embeds.unsqueeze(0 ).repeat(UpperCAmelCase , 1 , 1 ) else: _lowercase =[''''''] * batch_size _lowercase =text_input_ids.shape[-1] _lowercase =self.tokenizer( UpperCAmelCase , padding='''max_length''' , max_length=UpperCAmelCase , truncation=UpperCAmelCase , return_tensors='''pt''' , ) _lowercase =self.text_encoder(uncond_input.input_ids.to(self.device ) )[0] # See comment for normalizing text embeddings _lowercase =negative_prompt_embeds / negative_prompt_embeds.norm(dim=-1 , keepdim=UpperCAmelCase ) # duplicate unconditional embeddings for each generation per prompt, using mps friendly method _lowercase =negative_prompt_embeds.shape[1] _lowercase =negative_prompt_embeds.repeat(1 , UpperCAmelCase , 1 ) _lowercase =negative_prompt_embeds.view(batch_size * num_images_per_prompt , UpperCAmelCase , -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 _lowercase =torch.cat([negative_prompt_embeds, prompt_embeds] ) return prompt_embeds @torch.no_grad() def __call__(self , UpperCAmelCase , UpperCAmelCase = 1_0_0 , UpperCAmelCase = 5.0 , UpperCAmelCase = 1.0 , UpperCAmelCase = 1 , UpperCAmelCase = None , UpperCAmelCase = None , UpperCAmelCase = "pil" , UpperCAmelCase = True , UpperCAmelCase = None , UpperCAmelCase = 1 , ) -> Union[ImagePipelineOutput, Tuple]: if isinstance(UpperCAmelCase , UpperCAmelCase ): _lowercase =1 elif isinstance(UpperCAmelCase , UpperCAmelCase ): _lowercase =len(UpperCAmelCase ) else: raise ValueError(f"`prompt` has to be of type `str` or `list` but is {type(UpperCAmelCase )}" ) _lowercase =batch_size * num_images_per_prompt _lowercase =guidance_scale > 1.0 _lowercase =self._encode_prompt(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) if (callback_steps is None) or ( callback_steps is not None and (not isinstance(UpperCAmelCase , UpperCAmelCase ) or callback_steps <= 0) ): raise ValueError( f"`callback_steps` has to be a positive integer but is {callback_steps} of type" f" {type(UpperCAmelCase )}." ) # get the initial completely masked latents unless the user supplied it _lowercase =(batch_size, self.transformer.num_latent_pixels) if latents is None: _lowercase =self.transformer.num_vector_embeds - 1 _lowercase =torch.full(UpperCAmelCase , UpperCAmelCase ).to(self.device ) else: if latents.shape != latents_shape: raise ValueError(f"Unexpected latents shape, got {latents.shape}, expected {latents_shape}" ) if (latents < 0).any() or (latents >= self.transformer.num_vector_embeds).any(): raise ValueError( '''Unexpected latents value(s). All latents be valid embedding indices i.e. in the range 0,''' f" {self.transformer.num_vector_embeds - 1} (inclusive)." ) _lowercase =latents.to(self.device ) # set timesteps self.scheduler.set_timesteps(UpperCAmelCase , device=self.device ) _lowercase =self.scheduler.timesteps.to(self.device ) _lowercase =latents for i, t in enumerate(self.progress_bar(UpperCAmelCase ) ): # expand the sample if we are doing classifier free guidance _lowercase =torch.cat([sample] * 2 ) if do_classifier_free_guidance else sample # predict the un-noised image # model_output == `log_p_x_0` _lowercase =self.transformer(UpperCAmelCase , encoder_hidden_states=UpperCAmelCase , timestep=UpperCAmelCase ).sample if do_classifier_free_guidance: _lowercase , _lowercase =model_output.chunk(2 ) _lowercase =model_output_uncond + guidance_scale * (model_output_text - model_output_uncond) model_output -= torch.logsumexp(UpperCAmelCase , dim=1 , keepdim=UpperCAmelCase ) _lowercase =self.truncate(UpperCAmelCase , UpperCAmelCase ) # remove `log(0)`'s (`-inf`s) _lowercase =model_output.clamp(-7_0 ) # compute the previous noisy sample x_t -> x_t-1 _lowercase =self.scheduler.step(UpperCAmelCase , timestep=UpperCAmelCase , sample=UpperCAmelCase , generator=UpperCAmelCase ).prev_sample # call the callback, if provided if callback is not None and i % callback_steps == 0: callback(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) _lowercase =self.vqvae.config.vq_embed_dim _lowercase =(batch_size, self.transformer.height, self.transformer.width, embedding_channels) _lowercase =self.vqvae.quantize.get_codebook_entry(UpperCAmelCase , shape=UpperCAmelCase ) _lowercase =self.vqvae.decode(UpperCAmelCase , force_not_quantize=UpperCAmelCase ).sample _lowercase =(image / 2 + 0.5).clamp(0 , 1 ) _lowercase =image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": _lowercase =self.numpy_to_pil(UpperCAmelCase ) if not return_dict: return (image,) return ImagePipelineOutput(images=UpperCAmelCase ) def __A (self , UpperCAmelCase , UpperCAmelCase ) -> torch.FloatTensor: _lowercase , _lowercase =torch.sort(UpperCAmelCase , 1 , descending=UpperCAmelCase ) _lowercase =torch.exp(UpperCAmelCase ) _lowercase =sorted_p_x_0.cumsum(dim=1 ) < truncation_rate # Ensure that at least the largest probability is not zeroed out _lowercase =torch.full_like(keep_mask[:, 0:1, :] , UpperCAmelCase ) _lowercase =torch.cat((all_true, keep_mask) , dim=1 ) _lowercase =keep_mask[:, :-1, :] _lowercase =keep_mask.gather(1 , indices.argsort(1 ) ) _lowercase =log_p_x_0.clone() _lowercase =-torch.inf # -inf = log(0) return rv
5
def UpperCAmelCase_ ( __snake_case ) -> str: """simple docstring""" _lowercase =0 # if input_string is "aba" than new_input_string become "a|b|a" _lowercase ='''''' _lowercase ='''''' # append each character + "|" in new_string for range(0, length-1) for i in input_string[: len(__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 _lowercase , _lowercase =0, 0 # length[i] shows the length of palindromic substring with center i _lowercase =[1 for i in range(len(__snake_case ) )] # for each character in new_string find corresponding palindromic string _lowercase =0 for j in range(len(__snake_case ) ): _lowercase =1 if j > r else min(length[l + r - j] // 2 , r - j + 1 ) while ( j - k >= 0 and j + k < len(__snake_case ) and new_input_string[k + j] == new_input_string[j - k] ): k += 1 _lowercase =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: _lowercase =j - k + 1 # noqa: E741 _lowercase =j + k - 1 # update max_length and start position if max_length < length[j]: _lowercase =length[j] _lowercase =j # create that string _lowercase =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()
5
1
def UpperCAmelCase_ ( __snake_case , __snake_case ) -> List[Any]: """simple docstring""" if b == 0: return 1 if (b % 2) == 0: return actual_power(__snake_case , int(b / 2 ) ) * actual_power(__snake_case , int(b / 2 ) ) else: return a * actual_power(__snake_case , int(b / 2 ) ) * actual_power(__snake_case , int(b / 2 ) ) def UpperCAmelCase_ ( __snake_case , __snake_case ) -> float: """simple docstring""" if b < 0: return 1 / actual_power(__snake_case , __snake_case ) return actual_power(__snake_case , __snake_case ) if __name__ == "__main__": print(power(-2, -3))
5
from math import isqrt def UpperCAmelCase_ ( __snake_case ) -> list[int]: """simple docstring""" _lowercase =[True] * max_number for i in range(2 , isqrt(max_number - 1 ) + 1 ): if is_prime[i]: for j in range(i**2 , __snake_case , __snake_case ): _lowercase =False return [i for i in range(2 , __snake_case ) if is_prime[i]] def UpperCAmelCase_ ( __snake_case = 10**8 ) -> int: """simple docstring""" _lowercase =calculate_prime_numbers(max_number // 2 ) _lowercase =0 _lowercase =0 _lowercase =len(__snake_case ) - 1 while left <= right: while prime_numbers[left] * prime_numbers[right] >= max_number: right -= 1 semiprimes_count += right - left + 1 left += 1 return semiprimes_count if __name__ == "__main__": print(f'''{solution() = }''')
5
1
from timeit import timeit def UpperCAmelCase_ ( __snake_case ) -> int: """simple docstring""" if number < 0: raise ValueError('''the value of input must not be negative''' ) _lowercase =0 while number: number &= number - 1 result += 1 return result def UpperCAmelCase_ ( __snake_case ) -> int: """simple docstring""" if number < 0: raise ValueError('''the value of input must not be negative''' ) _lowercase =0 while number: if number % 2 == 1: result += 1 number >>= 1 return result def UpperCAmelCase_ ( ) -> None: """simple docstring""" def do_benchmark(__snake_case ) -> None: _lowercase ='''import __main__ as z''' print(F"Benchmark when {number = }:" ) print(F"{get_set_bits_count_using_modulo_operator(__snake_case ) = }" ) _lowercase =timeit('''z.get_set_bits_count_using_modulo_operator(25)''' , setup=__snake_case ) print(F"timeit() runs in {timing} seconds" ) print(F"{get_set_bits_count_using_brian_kernighans_algorithm(__snake_case ) = }" ) _lowercase =timeit( '''z.get_set_bits_count_using_brian_kernighans_algorithm(25)''' , setup=__snake_case , ) print(F"timeit() runs in {timing} seconds" ) for number in (25, 37, 58, 0): do_benchmark(__snake_case ) print() if __name__ == "__main__": import doctest doctest.testmod() benchmark()
5
UpperCAmelCase__ = { '''A''': '''.-''', '''B''': '''-...''', '''C''': '''-.-.''', '''D''': '''-..''', '''E''': '''.''', '''F''': '''..-.''', '''G''': '''--.''', '''H''': '''....''', '''I''': '''..''', '''J''': '''.---''', '''K''': '''-.-''', '''L''': '''.-..''', '''M''': '''--''', '''N''': '''-.''', '''O''': '''---''', '''P''': '''.--.''', '''Q''': '''--.-''', '''R''': '''.-.''', '''S''': '''...''', '''T''': '''-''', '''U''': '''..-''', '''V''': '''...-''', '''W''': '''.--''', '''X''': '''-..-''', '''Y''': '''-.--''', '''Z''': '''--..''', '''1''': '''.----''', '''2''': '''..---''', '''3''': '''...--''', '''4''': '''....-''', '''5''': '''.....''', '''6''': '''-....''', '''7''': '''--...''', '''8''': '''---..''', '''9''': '''----.''', '''0''': '''-----''', '''&''': '''.-...''', '''@''': '''.--.-.''', ''':''': '''---...''', ''',''': '''--..--''', '''.''': '''.-.-.-''', '''\'''': '''.----.''', '''"''': '''.-..-.''', '''?''': '''..--..''', '''/''': '''-..-.''', '''=''': '''-...-''', '''+''': '''.-.-.''', '''-''': '''-....-''', '''(''': '''-.--.''', ''')''': '''-.--.-''', '''!''': '''-.-.--''', ''' ''': '''/''' } # Exclamation mark is not in ITU-R recommendation # fmt: on UpperCAmelCase__ = {value: key for key, value in MORSE_CODE_DICT.items()} def UpperCAmelCase_ ( __snake_case ) -> str: """simple docstring""" return " ".join(MORSE_CODE_DICT[char] for char in message.upper() ) def UpperCAmelCase_ ( __snake_case ) -> str: """simple docstring""" return "".join(REVERSE_DICT[char] for char in message.split() ) def UpperCAmelCase_ ( ) -> None: """simple docstring""" _lowercase ='''Morse code here!''' print(__snake_case ) _lowercase =encrypt(__snake_case ) print(__snake_case ) _lowercase =decrypt(__snake_case ) print(__snake_case ) if __name__ == "__main__": main()
5
1
import math def UpperCAmelCase_ ( __snake_case ) -> bool: """simple docstring""" return math.sqrt(__snake_case ) * math.sqrt(__snake_case ) == num def UpperCAmelCase_ ( __snake_case ) -> bool: """simple docstring""" _lowercase =0 _lowercase =n while left <= right: _lowercase =(left + right) // 2 if mid**2 == n: return True elif mid**2 > n: _lowercase =mid - 1 else: _lowercase =mid + 1 return False if __name__ == "__main__": import doctest doctest.testmod()
5
from typing import Any def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , ) -> list: """simple docstring""" _validation( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , ) # Creates data structures and fill initial step _lowercase ={} _lowercase ={} for state in states_space: _lowercase =observations_space[0] _lowercase =( initial_probabilities[state] * emission_probabilities[state][observation] ) _lowercase =None # Fills the data structure with the probabilities of # different transitions and pointers to previous states for o in range(1 , len(__snake_case ) ): _lowercase =observations_space[o] _lowercase =observations_space[o - 1] for state in states_space: # Calculates the argmax for probability function _lowercase ='''''' _lowercase =-1 for k_state in states_space: _lowercase =( probabilities[(k_state, prior_observation)] * transition_probabilities[k_state][state] * emission_probabilities[state][observation] ) if probability > max_probability: _lowercase =probability _lowercase =k_state # Update probabilities and pointers dicts _lowercase =( probabilities[(arg_max, prior_observation)] * transition_probabilities[arg_max][state] * emission_probabilities[state][observation] ) _lowercase =arg_max # The final observation _lowercase =observations_space[len(__snake_case ) - 1] # argmax for given final observation _lowercase ='''''' _lowercase =-1 for k_state in states_space: _lowercase =probabilities[(k_state, final_observation)] if probability > max_probability: _lowercase =probability _lowercase =k_state _lowercase =arg_max # Process pointers backwards _lowercase =last_state _lowercase =[] for o in range(len(__snake_case ) - 1 , -1 , -1 ): result.append(__snake_case ) _lowercase =pointers[previous, observations_space[o]] result.reverse() return result def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , ) -> None: """simple docstring""" _validate_not_empty( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , ) _validate_lists(__snake_case , __snake_case ) _validate_dicts( __snake_case , __snake_case , __snake_case ) def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , ) -> None: """simple docstring""" if not all( [ observations_space, states_space, initial_probabilities, transition_probabilities, emission_probabilities, ] ): raise ValueError('''There\'s an empty parameter''' ) def UpperCAmelCase_ ( __snake_case , __snake_case ) -> None: """simple docstring""" _validate_list(__snake_case , '''observations_space''' ) _validate_list(__snake_case , '''states_space''' ) def UpperCAmelCase_ ( __snake_case , __snake_case ) -> None: """simple docstring""" if not isinstance(_object , __snake_case ): _lowercase =F"{var_name} must be a list" raise ValueError(__snake_case ) else: for x in _object: if not isinstance(__snake_case , __snake_case ): _lowercase =F"{var_name} must be a list of strings" raise ValueError(__snake_case ) def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , ) -> None: """simple docstring""" _validate_dict(__snake_case , '''initial_probabilities''' , __snake_case ) _validate_nested_dict(__snake_case , '''transition_probabilities''' ) _validate_nested_dict(__snake_case , '''emission_probabilities''' ) def UpperCAmelCase_ ( __snake_case , __snake_case ) -> None: """simple docstring""" _validate_dict(_object , __snake_case , __snake_case ) for x in _object.values(): _validate_dict(__snake_case , __snake_case , __snake_case , __snake_case ) def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case = False ) -> None: """simple docstring""" if not isinstance(_object , __snake_case ): _lowercase =F"{var_name} must be a dict" raise ValueError(__snake_case ) if not all(isinstance(__snake_case , __snake_case ) for x in _object ): _lowercase =F"{var_name} all keys must be strings" raise ValueError(__snake_case ) if not all(isinstance(__snake_case , __snake_case ) for x in _object.values() ): _lowercase ='''nested dictionary ''' if nested else '''''' _lowercase =F"{var_name} {nested_text}all values must be {value_type.__name__}" raise ValueError(__snake_case ) if __name__ == "__main__": from doctest import testmod testmod()
5
1
from math import isqrt def UpperCAmelCase_ ( __snake_case ) -> list[int]: """simple docstring""" _lowercase =[True] * max_number for i in range(2 , isqrt(max_number - 1 ) + 1 ): if is_prime[i]: for j in range(i**2 , __snake_case , __snake_case ): _lowercase =False return [i for i in range(2 , __snake_case ) if is_prime[i]] def UpperCAmelCase_ ( __snake_case = 10**8 ) -> int: """simple docstring""" _lowercase =calculate_prime_numbers(max_number // 2 ) _lowercase =0 _lowercase =0 _lowercase =len(__snake_case ) - 1 while left <= right: while prime_numbers[left] * prime_numbers[right] >= max_number: right -= 1 semiprimes_count += right - left + 1 left += 1 return semiprimes_count if __name__ == "__main__": print(f'''{solution() = }''')
5
from dataclasses import asdict, dataclass from typing import Optional from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCAmelCase__ = logging.get_logger(__name__) # TODO Update this UpperCAmelCase__ = { '''facebook/esm-1b''': '''https://huggingface.co/facebook/esm-1b/resolve/main/config.json''', # See all ESM models at https://huggingface.co/models?filter=esm } class lowerCamelCase__ ( lowerCAmelCase): SCREAMING_SNAKE_CASE__ = '''esm''' def __init__(self , UpperCAmelCase=None , UpperCAmelCase=None , UpperCAmelCase=None , UpperCAmelCase=7_6_8 , UpperCAmelCase=1_2 , UpperCAmelCase=1_2 , UpperCAmelCase=3_0_7_2 , UpperCAmelCase=0.1 , UpperCAmelCase=0.1 , UpperCAmelCase=1_0_2_6 , UpperCAmelCase=0.02 , UpperCAmelCase=1e-12 , UpperCAmelCase="absolute" , UpperCAmelCase=True , UpperCAmelCase=None , UpperCAmelCase=False , UpperCAmelCase=False , UpperCAmelCase=None , UpperCAmelCase=None , **UpperCAmelCase , ) -> Tuple: super().__init__(pad_token_id=UpperCAmelCase , mask_token_id=UpperCAmelCase , **UpperCAmelCase ) _lowercase =vocab_size _lowercase =hidden_size _lowercase =num_hidden_layers _lowercase =num_attention_heads _lowercase =intermediate_size _lowercase =hidden_dropout_prob _lowercase =attention_probs_dropout_prob _lowercase =max_position_embeddings _lowercase =initializer_range _lowercase =layer_norm_eps _lowercase =position_embedding_type _lowercase =use_cache _lowercase =emb_layer_norm_before _lowercase =token_dropout _lowercase =is_folding_model if is_folding_model: if esmfold_config is None: logger.info('''No esmfold_config supplied for folding model, using default values.''' ) _lowercase =EsmFoldConfig() elif isinstance(UpperCAmelCase , UpperCAmelCase ): _lowercase =EsmFoldConfig(**UpperCAmelCase ) _lowercase =esmfold_config if vocab_list is None: logger.warning('''No vocab_list supplied for folding model, assuming the ESM-2 vocabulary!''' ) _lowercase =get_default_vocab_list() else: _lowercase =vocab_list else: _lowercase =None _lowercase =None if self.esmfold_config is not None and getattr(self.esmfold_config , '''use_esm_attn_map''' , UpperCAmelCase ): raise ValueError('''The HuggingFace port of ESMFold does not support use_esm_attn_map at this time!''' ) def __A (self ) -> List[str]: _lowercase =super().to_dict() if isinstance(self.esmfold_config , UpperCAmelCase ): _lowercase =self.esmfold_config.to_dict() return output @dataclass class lowerCamelCase__ : SCREAMING_SNAKE_CASE__ = None SCREAMING_SNAKE_CASE__ = True SCREAMING_SNAKE_CASE__ = False SCREAMING_SNAKE_CASE__ = False SCREAMING_SNAKE_CASE__ = False SCREAMING_SNAKE_CASE__ = 0 SCREAMING_SNAKE_CASE__ = True SCREAMING_SNAKE_CASE__ = False SCREAMING_SNAKE_CASE__ = 128 SCREAMING_SNAKE_CASE__ = None def __A (self ) -> Union[str, Any]: if self.trunk is None: _lowercase =TrunkConfig() elif isinstance(self.trunk , UpperCAmelCase ): _lowercase =TrunkConfig(**self.trunk ) def __A (self ) -> Tuple: _lowercase =asdict(self ) _lowercase =self.trunk.to_dict() return output @dataclass class lowerCamelCase__ : SCREAMING_SNAKE_CASE__ = 48 SCREAMING_SNAKE_CASE__ = 1024 SCREAMING_SNAKE_CASE__ = 128 SCREAMING_SNAKE_CASE__ = 32 SCREAMING_SNAKE_CASE__ = 32 SCREAMING_SNAKE_CASE__ = 32 SCREAMING_SNAKE_CASE__ = 0 SCREAMING_SNAKE_CASE__ = 0 SCREAMING_SNAKE_CASE__ = False SCREAMING_SNAKE_CASE__ = 4 SCREAMING_SNAKE_CASE__ = 128 SCREAMING_SNAKE_CASE__ = None def __A (self ) -> List[str]: if self.structure_module is None: _lowercase =StructureModuleConfig() elif isinstance(self.structure_module , UpperCAmelCase ): _lowercase =StructureModuleConfig(**self.structure_module ) if self.max_recycles <= 0: raise ValueError(f"`max_recycles` should be positive, got {self.max_recycles}." ) if self.sequence_state_dim % self.sequence_state_dim != 0: raise ValueError( '''`sequence_state_dim` should be a round multiple of `sequence_state_dim`, got''' f" {self.sequence_state_dim} and {self.sequence_state_dim}." ) if self.pairwise_state_dim % self.pairwise_state_dim != 0: raise ValueError( '''`pairwise_state_dim` should be a round multiple of `pairwise_state_dim`, got''' f" {self.pairwise_state_dim} and {self.pairwise_state_dim}." ) _lowercase =self.sequence_state_dim // self.sequence_head_width _lowercase =self.pairwise_state_dim // self.pairwise_head_width if self.sequence_state_dim != sequence_num_heads * self.sequence_head_width: raise ValueError( '''`sequence_state_dim` should be equal to `sequence_num_heads * sequence_head_width, got''' f" {self.sequence_state_dim} != {sequence_num_heads} * {self.sequence_head_width}." ) if self.pairwise_state_dim != pairwise_num_heads * self.pairwise_head_width: raise ValueError( '''`pairwise_state_dim` should be equal to `pairwise_num_heads * pairwise_head_width, got''' f" {self.pairwise_state_dim} != {pairwise_num_heads} * {self.pairwise_head_width}." ) if self.pairwise_state_dim % 2 != 0: raise ValueError(f"`pairwise_state_dim` should be even, got {self.pairwise_state_dim}." ) if self.dropout >= 0.4: raise ValueError(f"`dropout` should not be greater than 0.4, got {self.dropout}." ) def __A (self ) -> Dict: _lowercase =asdict(self ) _lowercase =self.structure_module.to_dict() return output @dataclass class lowerCamelCase__ : SCREAMING_SNAKE_CASE__ = 384 SCREAMING_SNAKE_CASE__ = 128 SCREAMING_SNAKE_CASE__ = 16 SCREAMING_SNAKE_CASE__ = 128 SCREAMING_SNAKE_CASE__ = 12 SCREAMING_SNAKE_CASE__ = 4 SCREAMING_SNAKE_CASE__ = 8 SCREAMING_SNAKE_CASE__ = 0.1 SCREAMING_SNAKE_CASE__ = 8 SCREAMING_SNAKE_CASE__ = 1 SCREAMING_SNAKE_CASE__ = 2 SCREAMING_SNAKE_CASE__ = 7 SCREAMING_SNAKE_CASE__ = 10 SCREAMING_SNAKE_CASE__ = 1E-8 SCREAMING_SNAKE_CASE__ = 1E5 def __A (self ) -> List[Any]: return asdict(self ) def UpperCAmelCase_ ( ) -> Tuple: """simple docstring""" return ( "<cls>", "<pad>", "<eos>", "<unk>", "L", "A", "G", "V", "S", "E", "R", "T", "I", "D", "P", "K", "Q", "N", "F", "Y", "M", "H", "W", "C", "X", "B", "U", "Z", "O", ".", "-", "<null_1>", "<mask>", )
5
1
def UpperCAmelCase_ ( __snake_case , __snake_case ) -> str: """simple docstring""" _lowercase =[[] for _ in range(__snake_case )] _lowercase =key - 1 if key <= 0: raise ValueError('''Height of grid can\'t be 0 or negative''' ) if key == 1 or len(__snake_case ) <= key: return input_string for position, character in enumerate(__snake_case ): _lowercase =position % (lowest * 2) # puts it in bounds _lowercase =min(__snake_case , lowest * 2 - num ) # creates zigzag pattern temp_grid[num].append(__snake_case ) _lowercase =[''''''.join(__snake_case ) for row in temp_grid] _lowercase =''''''.join(__snake_case ) return output_string def UpperCAmelCase_ ( __snake_case , __snake_case ) -> str: """simple docstring""" _lowercase =[] _lowercase =key - 1 if key <= 0: raise ValueError('''Height of grid can\'t be 0 or negative''' ) if key == 1: return input_string _lowercase =[[] for _ in range(__snake_case )] # generates template for position in range(len(__snake_case ) ): _lowercase =position % (lowest * 2) # puts it in bounds _lowercase =min(__snake_case , lowest * 2 - num ) # creates zigzag pattern temp_grid[num].append('''*''' ) _lowercase =0 for row in temp_grid: # fills in the characters _lowercase =input_string[counter : counter + len(__snake_case )] grid.append(list(__snake_case ) ) counter += len(__snake_case ) _lowercase ='''''' # reads as zigzag for position in range(len(__snake_case ) ): _lowercase =position % (lowest * 2) # puts it in bounds _lowercase =min(__snake_case , lowest * 2 - num ) # creates zigzag pattern output_string += grid[num][0] grid[num].pop(0 ) return output_string def UpperCAmelCase_ ( __snake_case ) -> dict[int, str]: """simple docstring""" _lowercase ={} for key_guess in range(1 , len(__snake_case ) ): # tries every key _lowercase =decrypt(__snake_case , __snake_case ) return results if __name__ == "__main__": import doctest doctest.testmod()
5
import json import multiprocessing import os import re from collections import defaultdict import torch from accelerate import Accelerator from accelerate.utils import set_seed from arguments import HumanEvalArguments from datasets import load_dataset, load_metric from torch.utils.data import IterableDataset from torch.utils.data.dataloader import DataLoader from tqdm import tqdm import transformers from transformers import AutoModelForCausalLM, AutoTokenizer, HfArgumentParser, StoppingCriteria, StoppingCriteriaList UpperCAmelCase__ = ['''\nclass''', '''\ndef''', '''\n#''', '''\n@''', '''\nprint''', '''\nif'''] class lowerCamelCase__ ( lowerCAmelCase): def __init__(self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase=None , UpperCAmelCase=1 ) -> Dict: _lowercase =tokenizer _lowercase =dataset _lowercase =len(UpperCAmelCase ) if n_tasks is None else n_tasks _lowercase =n_copies def __iter__(self ) -> Optional[Any]: _lowercase =[] for task in range(self.n_tasks ): # without strip, the model generate commented codes ... prompts.append(self.tokenizer.eos_token + self.dataset[task]['''prompt'''].strip() ) _lowercase =self.tokenizer(UpperCAmelCase , padding=UpperCAmelCase , return_tensors='''pt''' ) for task in range(self.n_tasks ): for _ in range(self.n_copies ): yield { "ids": outputs.input_ids[task], "task_id": task, "input_len": outputs.attention_mask[task].sum(), } class lowerCamelCase__ ( lowerCAmelCase): def __init__(self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) -> Optional[Any]: _lowercase =start_length _lowercase =eof_strings _lowercase =tokenizer def __call__(self , UpperCAmelCase , UpperCAmelCase , **UpperCAmelCase ) -> Dict: _lowercase =self.tokenizer.batch_decode(input_ids[:, self.start_length :] ) _lowercase =[] for decoded_generation in decoded_generations: done.append(any(stop_string in decoded_generation for stop_string in self.eof_strings ) ) return all(UpperCAmelCase ) def UpperCAmelCase_ ( __snake_case ) -> Optional[Any]: """simple docstring""" _lowercase =re.split('''(%s)''' % '''|'''.join(__snake_case ) , __snake_case ) # last string should be "" return "".join(string_list[:-2] ) def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , __snake_case=20 , **__snake_case ) -> Tuple: """simple docstring""" _lowercase =defaultdict(__snake_case ) # dict of list of generated tokens for step, batch in tqdm(enumerate(__snake_case ) ): with torch.no_grad(): _lowercase =batch['''ids'''].shape[-1] _lowercase =accelerator.unwrap_model(__snake_case ).generate( input_ids=batch['''ids'''][:, : batch['''input_len''']] , num_return_sequences=__snake_case , **__snake_case ) # each task is generated batch_size times _lowercase =batch['''task_id'''].repeat(__snake_case ) _lowercase =accelerator.pad_across_processes( __snake_case , dim=1 , pad_index=tokenizer.pad_token_id ) _lowercase , _lowercase =accelerator.gather((generated_tokens, generated_tasks) ) _lowercase =generated_tokens.cpu().numpy() _lowercase =generated_tasks.cpu().numpy() for task, generated_tokens in zip(__snake_case , __snake_case ): gen_token_dict[task].append(__snake_case ) _lowercase =[[] for _ in range(__snake_case )] for task, generated_tokens in gen_token_dict.items(): for s in generated_tokens: _lowercase =tokenizer.decode(__snake_case , skip_special_tokens=__snake_case , clean_up_tokenization_spaces=__snake_case ) code_gens[task].append(remove_last_block(__snake_case ) ) return code_gens def UpperCAmelCase_ ( ) -> str: """simple docstring""" _lowercase =HfArgumentParser(__snake_case ) _lowercase =parser.parse_args() transformers.logging.set_verbosity_error() # enables code execution in code_eval metric _lowercase =args.HF_ALLOW_CODE_EVAL # make sure tokenizer plays nice with multiprocessing _lowercase ='''false''' if args.num_workers is None: _lowercase =multiprocessing.cpu_count() # Use dataset load to feed to accelerate _lowercase =Accelerator() set_seed(args.seed , device_specific=__snake_case ) # Load model and tokenizer _lowercase =AutoTokenizer.from_pretrained(args.model_ckpt ) _lowercase =tokenizer.eos_token _lowercase =AutoModelForCausalLM.from_pretrained(args.model_ckpt ) # Generation settings _lowercase ={ '''do_sample''': args.do_sample, '''temperature''': args.temperature, '''max_new_tokens''': args.max_new_tokens, '''top_p''': args.top_p, '''top_k''': args.top_k, '''stopping_criteria''': StoppingCriteriaList([EndOfFunctionCriteria(0 , __snake_case , __snake_case )] ), } # Load evaluation dataset and metric _lowercase =load_dataset('''openai_humaneval''' ) _lowercase =load_metric('''code_eval''' ) _lowercase =args.num_tasks if args.num_tasks is not None else len(human_eval['''test'''] ) _lowercase =args.n_samples // args.batch_size _lowercase =TokenizedDataset(__snake_case , human_eval['''test'''] , n_copies=__snake_case , n_tasks=__snake_case ) # do not confuse args.batch_size, which is actually the num_return_sequences _lowercase =DataLoader(__snake_case , batch_size=1 ) # Run a quick test to see if code evaluation is enabled try: _lowercase =code_eval_metric.compute(references=[''''''] , predictions=[['''''']] ) except ValueError as exception: print( '''Code evaluation not enabled. Read the warning below carefully and then use `--HF_ALLOW_CODE_EVAL="1"`''' ''' flag to enable code evaluation.''' ) raise exception _lowercase , _lowercase =accelerator.prepare(__snake_case , __snake_case ) _lowercase =complete_code( __snake_case , __snake_case , __snake_case , __snake_case , n_tasks=__snake_case , batch_size=args.batch_size , **__snake_case , ) if accelerator.is_main_process: _lowercase =[] for task in tqdm(range(__snake_case ) ): _lowercase =human_eval['''test'''][task]['''test'''] _lowercase =F"check({human_eval['test'][task]['entry_point']})" references.append('''\n''' + test_func + '''\n''' + entry_point ) # Evaluate completions with "code_eval" metric _lowercase , _lowercase =code_eval_metric.compute( references=__snake_case , predictions=__snake_case , num_workers=args.num_workers ) print(F"Results: {pass_at_k}" ) # Save results to json file with open(args.output_file , '''w''' ) as fp: json.dump(__snake_case , __snake_case ) # For some reason the folliwng seems to be necessary sometimes for code_eval to work nice with multiprocessing # https://stackoverflow.com/questions/60804599/python-multiprocessing-keeps-spawning-the-whole-script if __name__ == "__main__": main()
5
1
import argparse import datetime def UpperCAmelCase_ ( __snake_case ) -> str: """simple docstring""" _lowercase ={ '''0''': '''Sunday''', '''1''': '''Monday''', '''2''': '''Tuesday''', '''3''': '''Wednesday''', '''4''': '''Thursday''', '''5''': '''Friday''', '''6''': '''Saturday''', } _lowercase ={0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: 0} # Validate if not 0 < len(__snake_case ) < 11: raise ValueError('''Must be 10 characters long''' ) # Get month _lowercase =int(date_input[0] + date_input[1] ) # Validate if not 0 < m < 13: raise ValueError('''Month must be between 1 - 12''' ) _lowercase =date_input[2] # Validate if sep_a not in ["-", "/"]: raise ValueError('''Date separator must be \'-\' or \'/\'''' ) # Get day _lowercase =int(date_input[3] + date_input[4] ) # Validate if not 0 < d < 32: raise ValueError('''Date must be between 1 - 31''' ) # Get second separator _lowercase =date_input[5] # Validate if sep_a not in ["-", "/"]: raise ValueError('''Date separator must be \'-\' or \'/\'''' ) # Get year _lowercase =int(date_input[6] + date_input[7] + date_input[8] + date_input[9] ) # Arbitrary year range if not 45 < y < 8500: raise ValueError( '''Year out of range. There has to be some sort of limit...right?''' ) # Get datetime obj for validation _lowercase =datetime.date(int(__snake_case ) , int(__snake_case ) , int(__snake_case ) ) # Start math if m <= 2: _lowercase =y - 1 _lowercase =m + 12 # maths var _lowercase =int(str(__snake_case )[:2] ) _lowercase =int(str(__snake_case )[2:] ) _lowercase =int(2.6 * m - 5.39 ) _lowercase =int(c / 4 ) _lowercase =int(k / 4 ) _lowercase =int(d + k ) _lowercase =int(t + u + v + x ) _lowercase =int(z - (2 * c) ) _lowercase =round(w % 7 ) # End math # Validate math if f != convert_datetime_days[dt_ck.weekday()]: raise AssertionError('''The date was evaluated incorrectly. Contact developer.''' ) # Response _lowercase =F"Your date {date_input}, is a {days[str(__snake_case )]}!" return response if __name__ == "__main__": import doctest doctest.testmod() UpperCAmelCase__ = argparse.ArgumentParser( description=( '''Find out what day of the week nearly any date is or was. Enter ''' '''date as a string in the mm-dd-yyyy or mm/dd/yyyy format''' ) ) parser.add_argument( '''date_input''', type=str, help='''Date as a string (mm-dd-yyyy or mm/dd/yyyy)''' ) UpperCAmelCase__ = parser.parse_args() zeller(args.date_input)
5
UpperCAmelCase__ = 8.31_44_62 # Unit - J mol-1 K-1 def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case ) -> float: """simple docstring""" if moles < 0 or kelvin < 0 or volume < 0: raise ValueError('''Invalid inputs. Enter positive value.''' ) return moles * kelvin * UNIVERSAL_GAS_CONSTANT / volume def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case ) -> float: """simple docstring""" if moles < 0 or kelvin < 0 or pressure < 0: raise ValueError('''Invalid inputs. Enter positive value.''' ) return moles * kelvin * UNIVERSAL_GAS_CONSTANT / pressure if __name__ == "__main__": from doctest import testmod testmod()
5
1
from .imports import is_tqdm_available if is_tqdm_available(): from tqdm.auto import tqdm as _tqdm from ..state import PartialState def UpperCAmelCase_ ( __snake_case = True , *__snake_case , **__snake_case ) -> Tuple: """simple docstring""" if not is_tqdm_available(): raise ImportError('''Accelerate\'s `tqdm` module requires `tqdm` to be installed. Please run `pip install tqdm`.''' ) _lowercase =False if main_process_only: _lowercase =PartialState().local_process_index == 0 return _tqdm(*__snake_case , **__snake_case , disable=__snake_case )
5
from __future__ import annotations from collections.abc import Callable UpperCAmelCase__ = list[list[float | int]] def UpperCAmelCase_ ( __snake_case , __snake_case ) -> Matrix: """simple docstring""" _lowercase =len(__snake_case ) _lowercase =[[0 for _ in range(size + 1 )] for _ in range(__snake_case )] _lowercase =42 _lowercase =42 _lowercase =42 _lowercase =42 _lowercase =42 _lowercase =42 for row in range(__snake_case ): for col in range(__snake_case ): _lowercase =matrix[row][col] _lowercase =vector[row][0] _lowercase =0 _lowercase =0 while row < size and col < size: # pivoting _lowercase =max((abs(augmented[rowa][col] ), rowa) for rowa in range(__snake_case , __snake_case ) )[ 1 ] if augmented[pivot_row][col] == 0: col += 1 continue else: _lowercase , _lowercase =augmented[pivot_row], augmented[row] for rowa in range(row + 1 , __snake_case ): _lowercase =augmented[rowa][col] / augmented[row][col] _lowercase =0 for cola in range(col + 1 , size + 1 ): augmented[rowa][cola] -= augmented[row][cola] * ratio row += 1 col += 1 # back substitution for col in range(1 , __snake_case ): for row in range(__snake_case ): _lowercase =augmented[row][col] / augmented[col][col] for cola in range(__snake_case , size + 1 ): augmented[row][cola] -= augmented[col][cola] * ratio # round to get rid of numbers like 2.000000000000004 return [ [round(augmented[row][size] / augmented[row][row] , 10 )] for row in range(__snake_case ) ] def UpperCAmelCase_ ( __snake_case ) -> Callable[[int], int]: """simple docstring""" _lowercase =len(__snake_case ) _lowercase =[[0 for _ in range(__snake_case )] for _ in range(__snake_case )] _lowercase =[[0] for _ in range(__snake_case )] _lowercase =42 _lowercase =42 _lowercase =42 _lowercase =42 for x_val, y_val in enumerate(__snake_case ): for col in range(__snake_case ): _lowercase =(x_val + 1) ** (size - col - 1) _lowercase =y_val _lowercase =solve(__snake_case , __snake_case ) def interpolated_func(__snake_case ) -> int: return sum( round(coeffs[x_val][0] ) * (var ** (size - x_val - 1)) for x_val in range(__snake_case ) ) return interpolated_func def UpperCAmelCase_ ( __snake_case ) -> int: """simple docstring""" return ( 1 - variable + variable**2 - variable**3 + variable**4 - variable**5 + variable**6 - variable**7 + variable**8 - variable**9 + variable**10 ) def UpperCAmelCase_ ( __snake_case = question_function , __snake_case = 10 ) -> int: """simple docstring""" _lowercase =[func(__snake_case ) for x_val in range(1 , order + 1 )] _lowercase =[ interpolate(data_points[:max_coeff] ) for max_coeff in range(1 , order + 1 ) ] _lowercase =0 _lowercase =42 _lowercase =42 for poly in polynomials: _lowercase =1 while func(__snake_case ) == poly(__snake_case ): x_val += 1 ret += poly(__snake_case ) return ret if __name__ == "__main__": print(f'''{solution() = }''')
5
1
from __future__ import annotations UpperCAmelCase__ = [True] * 100_0001 UpperCAmelCase__ = 2 while i * i <= 100_0000: if seive[i]: for j in range(i * i, 100_0001, i): UpperCAmelCase__ = False i += 1 def UpperCAmelCase_ ( __snake_case ) -> bool: """simple docstring""" return seive[n] def UpperCAmelCase_ ( __snake_case ) -> bool: """simple docstring""" return any(digit in '''02468''' for digit in str(__snake_case ) ) def UpperCAmelCase_ ( __snake_case = 1000000 ) -> list[int]: """simple docstring""" _lowercase =[2] # result already includes the number 2. for num in range(3 , limit + 1 , 2 ): if is_prime(__snake_case ) and not contains_an_even_digit(__snake_case ): _lowercase =str(__snake_case ) _lowercase =[int(str_num[j:] + str_num[:j] ) for j in range(len(__snake_case ) )] if all(is_prime(__snake_case ) for i in list_nums ): result.append(__snake_case ) return result def UpperCAmelCase_ ( ) -> int: """simple docstring""" return len(find_circular_primes() ) if __name__ == "__main__": print(f'''{len(find_circular_primes()) = }''')
5
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available UpperCAmelCase__ = { '''configuration_xlm''': ['''XLM_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''XLMConfig''', '''XLMOnnxConfig'''], '''tokenization_xlm''': ['''XLMTokenizer'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ '''XLM_PRETRAINED_MODEL_ARCHIVE_LIST''', '''XLMForMultipleChoice''', '''XLMForQuestionAnswering''', '''XLMForQuestionAnsweringSimple''', '''XLMForSequenceClassification''', '''XLMForTokenClassification''', '''XLMModel''', '''XLMPreTrainedModel''', '''XLMWithLMHeadModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ '''TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFXLMForMultipleChoice''', '''TFXLMForQuestionAnsweringSimple''', '''TFXLMForSequenceClassification''', '''TFXLMForTokenClassification''', '''TFXLMMainLayer''', '''TFXLMModel''', '''TFXLMPreTrainedModel''', '''TFXLMWithLMHeadModel''', ] if TYPE_CHECKING: from .configuration_xlm import XLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XLMConfig, XLMOnnxConfig from .tokenization_xlm import XLMTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xlm import ( XLM_PRETRAINED_MODEL_ARCHIVE_LIST, XLMForMultipleChoice, XLMForQuestionAnswering, XLMForQuestionAnsweringSimple, XLMForSequenceClassification, XLMForTokenClassification, XLMModel, XLMPreTrainedModel, XLMWithLMHeadModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_xlm import ( TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFXLMForMultipleChoice, TFXLMForQuestionAnsweringSimple, TFXLMForSequenceClassification, TFXLMForTokenClassification, TFXLMMainLayer, TFXLMModel, TFXLMPreTrainedModel, TFXLMWithLMHeadModel, ) else: import sys UpperCAmelCase__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
5
1
from copy import deepcopy from typing import Optional, Union import numpy as np from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding from ...utils import TensorType, is_tf_available, is_torch_available if is_torch_available(): import torch if is_tf_available(): import tensorflow as tf class lowerCamelCase__ ( lowerCAmelCase): SCREAMING_SNAKE_CASE__ = ['''image_processor'''] SCREAMING_SNAKE_CASE__ = '''SamImageProcessor''' def __init__(self , UpperCAmelCase ) -> List[str]: super().__init__(UpperCAmelCase ) _lowercase =self.image_processor _lowercase =-1_0 _lowercase =self.image_processor.size['''longest_edge'''] def __call__(self , UpperCAmelCase=None , UpperCAmelCase=None , UpperCAmelCase=None , UpperCAmelCase=None , UpperCAmelCase = None , **UpperCAmelCase , ) -> BatchEncoding: _lowercase =self.image_processor( UpperCAmelCase , return_tensors=UpperCAmelCase , **UpperCAmelCase , ) # pop arguments that are not used in the foward but used nevertheless _lowercase =encoding_image_processor['''original_sizes'''] if hasattr(UpperCAmelCase , '''numpy''' ): # Checks if Torch or TF tensor _lowercase =original_sizes.numpy() _lowercase , _lowercase , _lowercase =self._check_and_preprocess_points( input_points=UpperCAmelCase , input_labels=UpperCAmelCase , input_boxes=UpperCAmelCase , ) _lowercase =self._normalize_and_convert( UpperCAmelCase , UpperCAmelCase , input_points=UpperCAmelCase , input_labels=UpperCAmelCase , input_boxes=UpperCAmelCase , return_tensors=UpperCAmelCase , ) return encoding_image_processor def __A (self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase=None , UpperCAmelCase=None , UpperCAmelCase=None , UpperCAmelCase="pt" , ) -> Tuple: if input_points is not None: if len(UpperCAmelCase ) != len(UpperCAmelCase ): _lowercase =[ self._normalize_coordinates(self.target_size , UpperCAmelCase , original_sizes[0] ) for point in input_points ] else: _lowercase =[ self._normalize_coordinates(self.target_size , UpperCAmelCase , UpperCAmelCase ) for point, original_size in zip(UpperCAmelCase , UpperCAmelCase ) ] # check that all arrays have the same shape if not all(point.shape == input_points[0].shape for point in input_points ): if input_labels is not None: _lowercase , _lowercase =self._pad_points_and_labels(UpperCAmelCase , UpperCAmelCase ) _lowercase =np.array(UpperCAmelCase ) if input_labels is not None: _lowercase =np.array(UpperCAmelCase ) if input_boxes is not None: if len(UpperCAmelCase ) != len(UpperCAmelCase ): _lowercase =[ self._normalize_coordinates(self.target_size , UpperCAmelCase , original_sizes[0] , is_bounding_box=UpperCAmelCase ) for box in input_boxes ] else: _lowercase =[ self._normalize_coordinates(self.target_size , UpperCAmelCase , UpperCAmelCase , is_bounding_box=UpperCAmelCase ) for box, original_size in zip(UpperCAmelCase , UpperCAmelCase ) ] _lowercase =np.array(UpperCAmelCase ) if input_boxes is not None: if return_tensors == "pt": _lowercase =torch.from_numpy(UpperCAmelCase ) # boxes batch size of 1 by default _lowercase =input_boxes.unsqueeze(1 ) if len(input_boxes.shape ) != 3 else input_boxes elif return_tensors == "tf": _lowercase =tf.convert_to_tensor(UpperCAmelCase ) # boxes batch size of 1 by default _lowercase =tf.expand_dims(UpperCAmelCase , 1 ) if len(input_boxes.shape ) != 3 else input_boxes encoding_image_processor.update({'''input_boxes''': input_boxes} ) if input_points is not None: if return_tensors == "pt": _lowercase =torch.from_numpy(UpperCAmelCase ) # point batch size of 1 by default _lowercase =input_points.unsqueeze(1 ) if len(input_points.shape ) != 4 else input_points elif return_tensors == "tf": _lowercase =tf.convert_to_tensor(UpperCAmelCase ) # point batch size of 1 by default _lowercase =tf.expand_dims(UpperCAmelCase , 1 ) if len(input_points.shape ) != 4 else input_points encoding_image_processor.update({'''input_points''': input_points} ) if input_labels is not None: if return_tensors == "pt": _lowercase =torch.from_numpy(UpperCAmelCase ) # point batch size of 1 by default _lowercase =input_labels.unsqueeze(1 ) if len(input_labels.shape ) != 3 else input_labels elif return_tensors == "tf": _lowercase =tf.convert_to_tensor(UpperCAmelCase ) # point batch size of 1 by default _lowercase =tf.expand_dims(UpperCAmelCase , 1 ) if len(input_labels.shape ) != 3 else input_labels encoding_image_processor.update({'''input_labels''': input_labels} ) return encoding_image_processor def __A (self , UpperCAmelCase , UpperCAmelCase ) -> Any: _lowercase =max([point.shape[0] for point in input_points] ) _lowercase =[] for i, point in enumerate(UpperCAmelCase ): if point.shape[0] != expected_nb_points: _lowercase =np.concatenate( [point, np.zeros((expected_nb_points - point.shape[0], 2) ) + self.point_pad_value] , axis=0 ) _lowercase =np.append(input_labels[i] , [self.point_pad_value] ) processed_input_points.append(UpperCAmelCase ) _lowercase =processed_input_points return input_points, input_labels def __A (self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase=False ) -> np.ndarray: _lowercase , _lowercase =original_size _lowercase , _lowercase =self.image_processor._get_preprocess_shape(UpperCAmelCase , longest_edge=UpperCAmelCase ) _lowercase =deepcopy(UpperCAmelCase ).astype(UpperCAmelCase ) if is_bounding_box: _lowercase =coords.reshape(-1 , 2 , 2 ) _lowercase =coords[..., 0] * (new_w / old_w) _lowercase =coords[..., 1] * (new_h / old_h) if is_bounding_box: _lowercase =coords.reshape(-1 , 4 ) return coords def __A (self , UpperCAmelCase=None , UpperCAmelCase=None , UpperCAmelCase=None , ) -> Dict: if input_points is not None: if hasattr(UpperCAmelCase , '''numpy''' ): # Checks for TF or Torch tensor _lowercase =input_points.numpy().tolist() if not isinstance(UpperCAmelCase , UpperCAmelCase ) or not isinstance(input_points[0] , UpperCAmelCase ): raise ValueError('''Input points must be a list of list of floating points.''' ) _lowercase =[np.array(UpperCAmelCase ) for input_point in input_points] else: _lowercase =None if input_labels is not None: if hasattr(UpperCAmelCase , '''numpy''' ): _lowercase =input_labels.numpy().tolist() if not isinstance(UpperCAmelCase , UpperCAmelCase ) or not isinstance(input_labels[0] , UpperCAmelCase ): raise ValueError('''Input labels must be a list of list integers.''' ) _lowercase =[np.array(UpperCAmelCase ) for label in input_labels] else: _lowercase =None if input_boxes is not None: if hasattr(UpperCAmelCase , '''numpy''' ): _lowercase =input_boxes.numpy().tolist() if ( not isinstance(UpperCAmelCase , UpperCAmelCase ) or not isinstance(input_boxes[0] , UpperCAmelCase ) or not isinstance(input_boxes[0][0] , UpperCAmelCase ) ): raise ValueError('''Input boxes must be a list of list of list of floating points.''' ) _lowercase =[np.array(UpperCAmelCase ).astype(np.floataa ) for box in input_boxes] else: _lowercase =None return input_points, input_labels, input_boxes @property def __A (self ) -> Tuple: _lowercase =self.image_processor.model_input_names return list(dict.fromkeys(UpperCAmelCase ) ) def __A (self , *UpperCAmelCase , **UpperCAmelCase ) -> Optional[int]: return self.image_processor.post_process_masks(*UpperCAmelCase , **UpperCAmelCase )
5
# Copyright 2023 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available UpperCAmelCase__ = { '''configuration_efficientnet''': [ '''EFFICIENTNET_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''EfficientNetConfig''', '''EfficientNetOnnxConfig''', ] } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = ['''EfficientNetImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ '''EFFICIENTNET_PRETRAINED_MODEL_ARCHIVE_LIST''', '''EfficientNetForImageClassification''', '''EfficientNetModel''', '''EfficientNetPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_efficientnet import ( EFFICIENTNET_PRETRAINED_CONFIG_ARCHIVE_MAP, EfficientNetConfig, EfficientNetOnnxConfig, ) try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_efficientnet import EfficientNetImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_efficientnet import ( EFFICIENTNET_PRETRAINED_MODEL_ARCHIVE_LIST, EfficientNetForImageClassification, EfficientNetModel, EfficientNetPreTrainedModel, ) else: import sys UpperCAmelCase__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure)
5
1
import io import itertools import json from dataclasses import dataclass from typing import Optional import pyarrow as pa import pyarrow.json as paj import datasets from datasets.table import table_cast from datasets.utils.file_utils import readline UpperCAmelCase__ = datasets.utils.logging.get_logger(__name__) @dataclass class lowerCamelCase__ ( datasets.BuilderConfig): SCREAMING_SNAKE_CASE__ = None SCREAMING_SNAKE_CASE__ = "utf-8" SCREAMING_SNAKE_CASE__ = None SCREAMING_SNAKE_CASE__ = None SCREAMING_SNAKE_CASE__ = True # deprecated SCREAMING_SNAKE_CASE__ = None # deprecated SCREAMING_SNAKE_CASE__ = 10 << 20 # 10MB SCREAMING_SNAKE_CASE__ = None class lowerCamelCase__ ( datasets.ArrowBasedBuilder): SCREAMING_SNAKE_CASE__ = JsonConfig def __A (self ) -> Dict: if self.config.block_size is not None: logger.warning('''The JSON loader parameter `block_size` is deprecated. Please use `chunksize` instead''' ) _lowercase =self.config.block_size if self.config.use_threads is not True: logger.warning( '''The JSON loader parameter `use_threads` is deprecated and doesn\'t have any effect anymore.''' ) if self.config.newlines_in_values is not None: raise ValueError('''The JSON loader parameter `newlines_in_values` is no longer supported''' ) return datasets.DatasetInfo(features=self.config.features ) def __A (self , UpperCAmelCase ) -> Any: if not self.config.data_files: raise ValueError(f"At least one data file must be specified, but got data_files={self.config.data_files}" ) _lowercase =dl_manager.download_and_extract(self.config.data_files ) if isinstance(UpperCAmelCase , (str, list, tuple) ): _lowercase =data_files if isinstance(UpperCAmelCase , UpperCAmelCase ): _lowercase =[files] _lowercase =[dl_manager.iter_files(UpperCAmelCase ) for file in files] return [datasets.SplitGenerator(name=datasets.Split.TRAIN , gen_kwargs={'''files''': files} )] _lowercase =[] for split_name, files in data_files.items(): if isinstance(UpperCAmelCase , UpperCAmelCase ): _lowercase =[files] _lowercase =[dl_manager.iter_files(UpperCAmelCase ) for file in files] splits.append(datasets.SplitGenerator(name=UpperCAmelCase , gen_kwargs={'''files''': files} ) ) return splits def __A (self , UpperCAmelCase ) -> pa.Table: if self.config.features is not None: # adding missing columns for column_name in set(self.config.features ) - set(pa_table.column_names ): _lowercase =self.config.features.arrow_schema.field(UpperCAmelCase ).type _lowercase =pa_table.append_column(UpperCAmelCase , pa.array([None] * len(UpperCAmelCase ) , type=UpperCAmelCase ) ) # more expensive cast to support nested structures with keys in a different order # allows str <-> int/float or str to Audio for example _lowercase =table_cast(UpperCAmelCase , self.config.features.arrow_schema ) return pa_table def __A (self , UpperCAmelCase ) -> int: for file_idx, file in enumerate(itertools.chain.from_iterable(UpperCAmelCase ) ): # If the file is one json object and if we need to look at the list of items in one specific field if self.config.field is not None: with open(UpperCAmelCase , encoding=self.config.encoding , errors=self.config.encoding_errors ) as f: _lowercase =json.load(UpperCAmelCase ) # We keep only the field we are interested in _lowercase =dataset[self.config.field] # We accept two format: a list of dicts or a dict of lists if isinstance(UpperCAmelCase , (list, tuple) ): _lowercase =set().union(*[row.keys() for row in dataset] ) _lowercase ={col: [row.get(UpperCAmelCase ) for row in dataset] for col in keys} else: _lowercase =dataset _lowercase =pa.Table.from_pydict(UpperCAmelCase ) yield file_idx, self._cast_table(UpperCAmelCase ) # If the file has one json object per line else: with open(UpperCAmelCase , '''rb''' ) as f: _lowercase =0 # Use block_size equal to the chunk size divided by 32 to leverage multithreading # Set a default minimum value of 16kB if the chunk size is really small _lowercase =max(self.config.chunksize // 3_2 , 1_6 << 1_0 ) _lowercase =( self.config.encoding_errors if self.config.encoding_errors is not None else '''strict''' ) while True: _lowercase =f.read(self.config.chunksize ) if not batch: break # Finish current line try: batch += f.readline() except (AttributeError, io.UnsupportedOperation): batch += readline(UpperCAmelCase ) # PyArrow only accepts utf-8 encoded bytes if self.config.encoding != "utf-8": _lowercase =batch.decode(self.config.encoding , errors=UpperCAmelCase ).encode('''utf-8''' ) try: while True: try: _lowercase =paj.read_json( io.BytesIO(UpperCAmelCase ) , read_options=paj.ReadOptions(block_size=UpperCAmelCase ) ) break except (pa.ArrowInvalid, pa.ArrowNotImplementedError) as e: if ( isinstance(UpperCAmelCase , pa.ArrowInvalid ) and "straddling" not in str(UpperCAmelCase ) or block_size > len(UpperCAmelCase ) ): raise else: # Increase the block size in case it was too small. # The block size will be reset for the next file. logger.debug( f"Batch of {len(UpperCAmelCase )} bytes couldn't be parsed with block_size={block_size}. Retrying with block_size={block_size * 2}." ) block_size *= 2 except pa.ArrowInvalid as e: try: with open( UpperCAmelCase , encoding=self.config.encoding , errors=self.config.encoding_errors ) as f: _lowercase =json.load(UpperCAmelCase ) except json.JSONDecodeError: logger.error(f"Failed to read file '{file}' with error {type(UpperCAmelCase )}: {e}" ) raise e # If possible, parse the file as a list of json objects and exit the loop if isinstance(UpperCAmelCase , UpperCAmelCase ): # list is the only sequence type supported in JSON try: _lowercase =set().union(*[row.keys() for row in dataset] ) _lowercase ={col: [row.get(UpperCAmelCase ) for row in dataset] for col in keys} _lowercase =pa.Table.from_pydict(UpperCAmelCase ) except (pa.ArrowInvalid, AttributeError) as e: logger.error(f"Failed to read file '{file}' with error {type(UpperCAmelCase )}: {e}" ) raise ValueError(f"Not able to read records in the JSON file at {file}." ) from None yield file_idx, self._cast_table(UpperCAmelCase ) break else: logger.error(f"Failed to read file '{file}' with error {type(UpperCAmelCase )}: {e}" ) raise ValueError( f"Not able to read records in the JSON file at {file}. " f"You should probably indicate the field of the JSON file containing your records. " f"This JSON file contain the following fields: {str(list(dataset.keys() ) )}. " f"Select the correct one and provide it as `field='XXX'` to the dataset loading method. " ) from None # Uncomment for debugging (will print the Arrow table size and elements) # logger.warning(f"pa_table: {pa_table} num rows: {pa_table.num_rows}") # logger.warning('\n'.join(str(pa_table.slice(i, 1).to_pydict()) for i in range(pa_table.num_rows))) yield (file_idx, batch_idx), self._cast_table(UpperCAmelCase ) batch_idx += 1
5
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available UpperCAmelCase__ = { '''configuration_timesformer''': ['''TIMESFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''TimesformerConfig'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase__ = [ '''TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TimesformerModel''', '''TimesformerForVideoClassification''', '''TimesformerPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_timesformer import TIMESFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TimesformerConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_timesformer import ( TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TimesformerForVideoClassification, TimesformerModel, TimesformerPreTrainedModel, ) else: import sys UpperCAmelCase__ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
5
1
import os import time import pytest from datasets.utils.filelock import FileLock, Timeout def UpperCAmelCase_ ( __snake_case ) -> List[Any]: """simple docstring""" _lowercase =FileLock(str(tmpdir / '''foo.lock''' ) ) _lowercase =FileLock(str(tmpdir / '''foo.lock''' ) ) _lowercase =0.01 with locka.acquire(): with pytest.raises(__snake_case ): _lowercase =time.time() locka.acquire(__snake_case ) assert time.time() - _start > timeout def UpperCAmelCase_ ( __snake_case ) -> Union[str, Any]: """simple docstring""" _lowercase ='''a''' * 1000 + '''.lock''' _lowercase =FileLock(str(tmpdir / filename ) ) assert locka._lock_file.endswith('''.lock''' ) assert not locka._lock_file.endswith(__snake_case ) assert len(os.path.basename(locka._lock_file ) ) <= 255 _lowercase =FileLock(tmpdir / filename ) with locka.acquire(): with pytest.raises(__snake_case ): locka.acquire(0 )
5
def UpperCAmelCase_ ( __snake_case , __snake_case ) -> List[Any]: """simple docstring""" if b == 0: return 1 if (b % 2) == 0: return actual_power(__snake_case , int(b / 2 ) ) * actual_power(__snake_case , int(b / 2 ) ) else: return a * actual_power(__snake_case , int(b / 2 ) ) * actual_power(__snake_case , int(b / 2 ) ) def UpperCAmelCase_ ( __snake_case , __snake_case ) -> float: """simple docstring""" if b < 0: return 1 / actual_power(__snake_case , __snake_case ) return actual_power(__snake_case , __snake_case ) if __name__ == "__main__": print(power(-2, -3))
5
1
from __future__ import annotations def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case ) -> None: """simple docstring""" if (direction == 1 and array[indexa] > array[indexa]) or ( direction == 0 and array[indexa] < array[indexa] ): _lowercase , _lowercase =array[indexa], array[indexa] def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case ) -> None: """simple docstring""" if length > 1: _lowercase =int(length / 2 ) for i in range(__snake_case , low + middle ): comp_and_swap(__snake_case , __snake_case , i + middle , __snake_case ) bitonic_merge(__snake_case , __snake_case , __snake_case , __snake_case ) bitonic_merge(__snake_case , low + middle , __snake_case , __snake_case ) def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case ) -> None: """simple docstring""" if length > 1: _lowercase =int(length / 2 ) bitonic_sort(__snake_case , __snake_case , __snake_case , 1 ) bitonic_sort(__snake_case , low + middle , __snake_case , 0 ) bitonic_merge(__snake_case , __snake_case , __snake_case , __snake_case ) if __name__ == "__main__": UpperCAmelCase__ = input('''Enter numbers separated by a comma:\n''').strip() UpperCAmelCase__ = [int(item.strip()) for item in user_input.split(''',''')] bitonic_sort(unsorted, 0, len(unsorted), 1) print('''\nSorted array in ascending order is: ''', end='''''') print(*unsorted, sep=''', ''') bitonic_merge(unsorted, 0, len(unsorted), 0) print('''Sorted array in descending order is: ''', end='''''') print(*unsorted, sep=''', ''')
5
from typing import Optional from torch import nn from .transformer_ad import TransformeraDModel, TransformeraDModelOutput class lowerCamelCase__ ( nn.Module): def __init__(self , UpperCAmelCase = 1_6 , UpperCAmelCase = 8_8 , UpperCAmelCase = None , UpperCAmelCase = 1 , UpperCAmelCase = 0.0 , UpperCAmelCase = 3_2 , UpperCAmelCase = None , UpperCAmelCase = False , UpperCAmelCase = None , UpperCAmelCase = None , UpperCAmelCase = "geglu" , UpperCAmelCase = None , ) -> Any: super().__init__() _lowercase =nn.ModuleList( [ TransformeraDModel( num_attention_heads=UpperCAmelCase , attention_head_dim=UpperCAmelCase , in_channels=UpperCAmelCase , num_layers=UpperCAmelCase , dropout=UpperCAmelCase , norm_num_groups=UpperCAmelCase , cross_attention_dim=UpperCAmelCase , attention_bias=UpperCAmelCase , sample_size=UpperCAmelCase , num_vector_embeds=UpperCAmelCase , activation_fn=UpperCAmelCase , num_embeds_ada_norm=UpperCAmelCase , ) for _ in range(2 ) ] ) # Variables that can be set by a pipeline: # The ratio of transformer1 to transformer2's output states to be combined during inference _lowercase =0.5 # The shape of `encoder_hidden_states` is expected to be # `(batch_size, condition_lengths[0]+condition_lengths[1], num_features)` _lowercase =[7_7, 2_5_7] # Which transformer to use to encode which condition. # E.g. `(1, 0)` means that we'll use `transformers[1](conditions[0])` and `transformers[0](conditions[1])` _lowercase =[1, 0] def __A (self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase=None , UpperCAmelCase=None , UpperCAmelCase=None , UpperCAmelCase = True , ) -> str: _lowercase =hidden_states _lowercase =[] _lowercase =0 # attention_mask is not used yet for i in range(2 ): # for each of the two transformers, pass the corresponding condition tokens _lowercase =encoder_hidden_states[:, tokens_start : tokens_start + self.condition_lengths[i]] _lowercase =self.transformer_index_for_condition[i] _lowercase =self.transformers[transformer_index]( UpperCAmelCase , encoder_hidden_states=UpperCAmelCase , timestep=UpperCAmelCase , cross_attention_kwargs=UpperCAmelCase , return_dict=UpperCAmelCase , )[0] encoded_states.append(encoded_state - input_states ) tokens_start += self.condition_lengths[i] _lowercase =encoded_states[0] * self.mix_ratio + encoded_states[1] * (1 - self.mix_ratio) _lowercase =output_states + input_states if not return_dict: return (output_states,) return TransformeraDModelOutput(sample=UpperCAmelCase )
5
1
# Lint as: python3 # pylint: enable=line-too-long # pylint: disable=g-import-not-at-top,g-bad-import-order,wrong-import-position UpperCAmelCase__ = '''2.13.1''' import platform import pyarrow from packaging import version if version.parse(platform.python_version()) < version.parse('''3.7'''): raise ImportWarning( '''To use `datasets`, Python>=3.7 is required, and the current version of Python doesn\'t match this condition.''' ) if version.parse(pyarrow.__version__).major < 8: raise ImportWarning( '''To use `datasets`, the module `pyarrow>=8.0.0` is required, and the current version of `pyarrow` doesn\'t match this condition.\n''' '''If you are running this in a Google Colab, you should probably just restart the runtime to use the right version of `pyarrow`.''' ) del platform del pyarrow del version from .arrow_dataset import Dataset from .arrow_reader import ReadInstruction from .builder import ArrowBasedBuilder, BeamBasedBuilder, BuilderConfig, DatasetBuilder, GeneratorBasedBuilder from .combine import concatenate_datasets, interleave_datasets from .dataset_dict import DatasetDict, IterableDatasetDict from .download import * from .features import * from .fingerprint import disable_caching, enable_caching, is_caching_enabled, set_caching_enabled from .info import DatasetInfo, MetricInfo from .inspect import ( get_dataset_config_info, get_dataset_config_names, get_dataset_infos, get_dataset_split_names, inspect_dataset, inspect_metric, list_datasets, list_metrics, ) from .iterable_dataset import IterableDataset from .load import load_dataset, load_dataset_builder, load_from_disk, load_metric from .metric import Metric from .splits import ( NamedSplit, NamedSplitAll, Split, SplitBase, SplitDict, SplitGenerator, SplitInfo, SubSplitInfo, percent, ) from .tasks import * from .utils import * from .utils import logging # deprecated modules from datasets import arrow_dataset as _arrow_dataset # isort:skip from datasets import utils as _utils # isort:skip from datasets.utils import download_manager as _deprecated_download_manager # isort:skip UpperCAmelCase__ = concatenate_datasets UpperCAmelCase__ = DownloadConfig UpperCAmelCase__ = DownloadManager UpperCAmelCase__ = DownloadMode UpperCAmelCase__ = DownloadConfig UpperCAmelCase__ = DownloadMode UpperCAmelCase__ = DownloadManager del _arrow_dataset, _utils, _deprecated_download_manager
5
import heapq as hq import math from collections.abc import Iterator class lowerCamelCase__ : def __init__(self , UpperCAmelCase ) -> Any: _lowercase =str(id_ ) _lowercase =None _lowercase =None _lowercase =[] _lowercase ={} # {vertex:distance} def __lt__(self , UpperCAmelCase ) -> List[str]: return self.key < other.key def __repr__(self ) -> str: return self.id def __A (self , UpperCAmelCase ) -> Dict: self.neighbors.append(UpperCAmelCase ) def __A (self , UpperCAmelCase , UpperCAmelCase ) -> Optional[Any]: _lowercase =weight def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case ) -> List[str]: """simple docstring""" graph[a - 1].add_neighbor(graph[b - 1] ) graph[b - 1].add_neighbor(graph[a - 1] ) # add the edges: graph[a - 1].add_edge(graph[b - 1] , __snake_case ) graph[b - 1].add_edge(graph[a - 1] , __snake_case ) def UpperCAmelCase_ ( __snake_case , __snake_case ) -> list: """simple docstring""" _lowercase =[] for u in graph: _lowercase =math.inf _lowercase =None _lowercase =0 _lowercase =graph[:] while q: _lowercase =min(__snake_case ) q.remove(__snake_case ) for v in u.neighbors: if (v in q) and (u.edges[v.id] < v.key): _lowercase =u _lowercase =u.edges[v.id] for i in range(1 , len(__snake_case ) ): a.append((int(graph[i].id ) + 1, int(graph[i].pi.id ) + 1) ) return a def UpperCAmelCase_ ( __snake_case , __snake_case ) -> Iterator[tuple]: """simple docstring""" for u in graph: _lowercase =math.inf _lowercase =None _lowercase =0 _lowercase =list(__snake_case ) hq.heapify(__snake_case ) while h: _lowercase =hq.heappop(__snake_case ) for v in u.neighbors: if (v in h) and (u.edges[v.id] < v.key): _lowercase =u _lowercase =u.edges[v.id] hq.heapify(__snake_case ) for i in range(1 , len(__snake_case ) ): yield (int(graph[i].id ) + 1, int(graph[i].pi.id ) + 1) def UpperCAmelCase_ ( ) -> None: """simple docstring""" if __name__ == "__main__": import doctest doctest.testmod()
5
1
UpperCAmelCase__ = [ '''DownloadConfig''', '''DownloadManager''', '''DownloadMode''', '''StreamingDownloadManager''', ] from .download_config import DownloadConfig from .download_manager import DownloadManager, DownloadMode from .streaming_download_manager import StreamingDownloadManager
5
# flake8: noqa # Lint as: python3 UpperCAmelCase__ = [ '''VerificationMode''', '''Version''', '''disable_progress_bar''', '''enable_progress_bar''', '''is_progress_bar_enabled''', '''experimental''', ] from .info_utils import VerificationMode from .logging import disable_progress_bar, enable_progress_bar, is_progress_bar_enabled from .version import Version from .experimental import experimental
5
1
UpperCAmelCase__ = 8.31_44_62 # Unit - J mol-1 K-1 def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case ) -> float: """simple docstring""" if moles < 0 or kelvin < 0 or volume < 0: raise ValueError('''Invalid inputs. Enter positive value.''' ) return moles * kelvin * UNIVERSAL_GAS_CONSTANT / volume def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case ) -> float: """simple docstring""" if moles < 0 or kelvin < 0 or pressure < 0: raise ValueError('''Invalid inputs. Enter positive value.''' ) return moles * kelvin * UNIVERSAL_GAS_CONSTANT / pressure if __name__ == "__main__": from doctest import testmod testmod()
5
import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = { '''microsoft/wavlm-base''': '''https://huggingface.co/microsoft/wavlm-base/resolve/main/config.json''', # See all WavLM models at https://huggingface.co/models?filter=wavlm } class lowerCamelCase__ ( lowerCAmelCase): SCREAMING_SNAKE_CASE__ = '''wavlm''' def __init__(self , UpperCAmelCase=3_2 , UpperCAmelCase=7_6_8 , UpperCAmelCase=1_2 , UpperCAmelCase=1_2 , UpperCAmelCase=3_0_7_2 , UpperCAmelCase="gelu" , UpperCAmelCase=0.1 , UpperCAmelCase=0.1 , UpperCAmelCase=0.1 , UpperCAmelCase=0.0 , UpperCAmelCase=0.1 , UpperCAmelCase=0.1 , UpperCAmelCase=0.02 , UpperCAmelCase=1e-5 , UpperCAmelCase="group" , UpperCAmelCase="gelu" , UpperCAmelCase=(5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2) , UpperCAmelCase=(5, 2, 2, 2, 2, 2, 2) , UpperCAmelCase=(1_0, 3, 3, 3, 3, 2, 2) , UpperCAmelCase=False , UpperCAmelCase=1_2_8 , UpperCAmelCase=1_6 , UpperCAmelCase=3_2_0 , UpperCAmelCase=8_0_0 , UpperCAmelCase=False , UpperCAmelCase=True , UpperCAmelCase=0.05 , UpperCAmelCase=1_0 , UpperCAmelCase=2 , UpperCAmelCase=0.0 , UpperCAmelCase=1_0 , UpperCAmelCase=3_2_0 , UpperCAmelCase=2 , UpperCAmelCase=0.1 , UpperCAmelCase=1_0_0 , UpperCAmelCase=2_5_6 , UpperCAmelCase=2_5_6 , UpperCAmelCase=0.1 , UpperCAmelCase="mean" , UpperCAmelCase=False , UpperCAmelCase=False , UpperCAmelCase=2_5_6 , UpperCAmelCase=(5_1_2, 5_1_2, 5_1_2, 5_1_2, 1_5_0_0) , UpperCAmelCase=(5, 3, 3, 1, 1) , UpperCAmelCase=(1, 2, 3, 1, 1) , UpperCAmelCase=5_1_2 , UpperCAmelCase=8_0 , UpperCAmelCase=0 , UpperCAmelCase=1 , UpperCAmelCase=2 , UpperCAmelCase=False , UpperCAmelCase=3 , UpperCAmelCase=2 , UpperCAmelCase=3 , UpperCAmelCase=None , **UpperCAmelCase , ) -> Optional[Any]: super().__init__(**UpperCAmelCase , pad_token_id=UpperCAmelCase , bos_token_id=UpperCAmelCase , eos_token_id=UpperCAmelCase ) _lowercase =hidden_size _lowercase =feat_extract_norm _lowercase =feat_extract_activation _lowercase =list(UpperCAmelCase ) _lowercase =list(UpperCAmelCase ) _lowercase =list(UpperCAmelCase ) _lowercase =conv_bias _lowercase =num_buckets _lowercase =max_bucket_distance _lowercase =num_conv_pos_embeddings _lowercase =num_conv_pos_embedding_groups _lowercase =len(self.conv_dim ) _lowercase =num_hidden_layers _lowercase =intermediate_size _lowercase =hidden_act _lowercase =num_attention_heads _lowercase =hidden_dropout _lowercase =attention_dropout _lowercase =activation_dropout _lowercase =feat_proj_dropout _lowercase =final_dropout _lowercase =layerdrop _lowercase =layer_norm_eps _lowercase =initializer_range _lowercase =num_ctc_classes _lowercase =vocab_size _lowercase =do_stable_layer_norm _lowercase =use_weighted_layer_sum _lowercase =classifier_proj_size if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( '''Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` ==''' ''' `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) =''' f" {len(self.conv_dim )}`, `len(config.conv_stride) = {len(self.conv_stride )}`," f" `len(config.conv_kernel) = {len(self.conv_kernel )}`." ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 _lowercase =apply_spec_augment _lowercase =mask_time_prob _lowercase =mask_time_length _lowercase =mask_time_min_masks _lowercase =mask_feature_prob _lowercase =mask_feature_length # parameters for pretraining with codevector quantized representations _lowercase =num_codevectors_per_group _lowercase =num_codevector_groups _lowercase =contrastive_logits_temperature _lowercase =num_negatives _lowercase =codevector_dim _lowercase =proj_codevector_dim _lowercase =diversity_loss_weight # ctc loss _lowercase =ctc_loss_reduction _lowercase =ctc_zero_infinity # adapter _lowercase =add_adapter _lowercase =adapter_kernel_size _lowercase =adapter_stride _lowercase =num_adapter_layers _lowercase =output_hidden_size or hidden_size # SequenceClassification-specific parameter. Feel free to ignore for other classes. _lowercase =classifier_proj_size # XVector-specific parameters. Feel free to ignore for other classes. _lowercase =list(UpperCAmelCase ) _lowercase =list(UpperCAmelCase ) _lowercase =list(UpperCAmelCase ) _lowercase =xvector_output_dim @property def __A (self ) -> int: return functools.reduce(operator.mul , self.conv_stride , 1 )
5
1
import logging import os import threading import time try: import warnings except ImportError: UpperCAmelCase__ = None try: import msvcrt except ImportError: UpperCAmelCase__ = None try: import fcntl except ImportError: UpperCAmelCase__ = None # Backward compatibility # ------------------------------------------------ try: TimeoutError except NameError: UpperCAmelCase__ = OSError # Data # ------------------------------------------------ UpperCAmelCase__ = [ '''Timeout''', '''BaseFileLock''', '''WindowsFileLock''', '''UnixFileLock''', '''SoftFileLock''', '''FileLock''', ] UpperCAmelCase__ = '''3.0.12''' UpperCAmelCase__ = None def UpperCAmelCase_ ( ) -> Optional[int]: """simple docstring""" global _logger _lowercase =_logger or logging.getLogger(__name__ ) return _logger class lowerCamelCase__ ( lowerCAmelCase): def __init__(self , UpperCAmelCase ) -> Optional[Any]: _lowercase =lock_file return None def __str__(self ) -> str: _lowercase =f"The file lock '{self.lock_file}' could not be acquired." return temp class lowerCamelCase__ : def __init__(self , UpperCAmelCase ) -> Dict: _lowercase =lock return None def __enter__(self ) -> Optional[Any]: return self.lock def __exit__(self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) -> List[Any]: self.lock.release() return None class lowerCamelCase__ : def __init__(self , UpperCAmelCase , UpperCAmelCase=-1 , UpperCAmelCase=None ) -> Dict: _lowercase =max_filename_length if max_filename_length is not None else 2_5_5 # Hash the filename if it's too long _lowercase =self.hash_filename_if_too_long(UpperCAmelCase , UpperCAmelCase ) # The path to the lock file. _lowercase =lock_file # The file descriptor for the *_lock_file* as it is returned by the # os.open() function. # This file lock is only NOT None, if the object currently holds the # lock. _lowercase =None # The default timeout value. _lowercase =timeout # We use this lock primarily for the lock counter. _lowercase =threading.Lock() # The lock counter is used for implementing the nested locking # mechanism. Whenever the lock is acquired, the counter is increased and # the lock is only released, when this value is 0 again. _lowercase =0 return None @property def __A (self ) -> int: return self._lock_file @property def __A (self ) -> Optional[Any]: return self._timeout @timeout.setter def __A (self , UpperCAmelCase ) -> Union[str, Any]: _lowercase =float(UpperCAmelCase ) return None def __A (self ) -> Any: raise NotImplementedError() def __A (self ) -> List[Any]: raise NotImplementedError() @property def __A (self ) -> Tuple: return self._lock_file_fd is not None def __A (self , UpperCAmelCase=None , UpperCAmelCase=0.05 ) -> Union[str, Any]: # Use the default timeout, if no timeout is provided. if timeout is None: _lowercase =self.timeout # Increment the number right at the beginning. # We can still undo it, if something fails. with self._thread_lock: self._lock_counter += 1 _lowercase =id(self ) _lowercase =self._lock_file _lowercase =time.time() try: while True: with self._thread_lock: if not self.is_locked: logger().debug(f"Attempting to acquire lock {lock_id} on {lock_filename}" ) self._acquire() if self.is_locked: logger().debug(f"Lock {lock_id} acquired on {lock_filename}" ) break elif timeout >= 0 and time.time() - start_time > timeout: logger().debug(f"Timeout on acquiring lock {lock_id} on {lock_filename}" ) raise Timeout(self._lock_file ) else: logger().debug( f"Lock {lock_id} not acquired on {lock_filename}, waiting {poll_intervall} seconds ..." ) time.sleep(UpperCAmelCase ) except: # noqa # Something did go wrong, so decrement the counter. with self._thread_lock: _lowercase =max(0 , self._lock_counter - 1 ) raise return _Acquire_ReturnProxy(lock=self ) def __A (self , UpperCAmelCase=False ) -> Union[str, Any]: with self._thread_lock: if self.is_locked: self._lock_counter -= 1 if self._lock_counter == 0 or force: _lowercase =id(self ) _lowercase =self._lock_file logger().debug(f"Attempting to release lock {lock_id} on {lock_filename}" ) self._release() _lowercase =0 logger().debug(f"Lock {lock_id} released on {lock_filename}" ) return None def __enter__(self ) -> List[Any]: self.acquire() return self def __exit__(self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) -> Dict: self.release() return None def __del__(self ) -> Optional[Any]: self.release(force=UpperCAmelCase ) return None def __A (self , UpperCAmelCase , UpperCAmelCase ) -> str: _lowercase =os.path.basename(UpperCAmelCase ) if len(UpperCAmelCase ) > max_length and max_length > 0: _lowercase =os.path.dirname(UpperCAmelCase ) _lowercase =str(hash(UpperCAmelCase ) ) _lowercase =filename[: max_length - len(UpperCAmelCase ) - 8] + '''...''' + hashed_filename + '''.lock''' return os.path.join(UpperCAmelCase , UpperCAmelCase ) else: return path class lowerCamelCase__ ( lowerCAmelCase): def __init__(self , UpperCAmelCase , UpperCAmelCase=-1 , UpperCAmelCase=None ) -> Tuple: from .file_utils import relative_to_absolute_path super().__init__(UpperCAmelCase , timeout=UpperCAmelCase , max_filename_length=UpperCAmelCase ) _lowercase ='''\\\\?\\''' + relative_to_absolute_path(self.lock_file ) def __A (self ) -> Any: _lowercase =os.O_RDWR | os.O_CREAT | os.O_TRUNC try: _lowercase =os.open(self._lock_file , UpperCAmelCase ) except OSError: pass else: try: msvcrt.locking(UpperCAmelCase , msvcrt.LK_NBLCK , 1 ) except OSError: os.close(UpperCAmelCase ) else: _lowercase =fd return None def __A (self ) -> Optional[int]: _lowercase =self._lock_file_fd _lowercase =None msvcrt.locking(UpperCAmelCase , msvcrt.LK_UNLCK , 1 ) os.close(UpperCAmelCase ) try: os.remove(self._lock_file ) # Probably another instance of the application # that acquired the file lock. except OSError: pass return None class lowerCamelCase__ ( lowerCAmelCase): def __init__(self , UpperCAmelCase , UpperCAmelCase=-1 , UpperCAmelCase=None ) -> Tuple: _lowercase =os.statvfs(os.path.dirname(UpperCAmelCase ) ).f_namemax super().__init__(UpperCAmelCase , timeout=UpperCAmelCase , max_filename_length=UpperCAmelCase ) def __A (self ) -> int: _lowercase =os.O_RDWR | os.O_CREAT | os.O_TRUNC _lowercase =os.open(self._lock_file , UpperCAmelCase ) try: fcntl.flock(UpperCAmelCase , fcntl.LOCK_EX | fcntl.LOCK_NB ) except OSError: os.close(UpperCAmelCase ) else: _lowercase =fd return None def __A (self ) -> Any: # Do not remove the lockfile: # # https://github.com/benediktschmitt/py-filelock/issues/31 # https://stackoverflow.com/questions/17708885/flock-removing-locked-file-without-race-condition _lowercase =self._lock_file_fd _lowercase =None fcntl.flock(UpperCAmelCase , fcntl.LOCK_UN ) os.close(UpperCAmelCase ) return None class lowerCamelCase__ ( lowerCAmelCase): def __A (self ) -> Dict: _lowercase =os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_TRUNC try: _lowercase =os.open(self._lock_file , UpperCAmelCase ) except OSError: pass else: _lowercase =fd return None def __A (self ) -> Any: os.close(self._lock_file_fd ) _lowercase =None try: os.remove(self._lock_file ) # The file is already deleted and that's what we want. except OSError: pass return None UpperCAmelCase__ = None if msvcrt: UpperCAmelCase__ = WindowsFileLock elif fcntl: UpperCAmelCase__ = UnixFileLock else: UpperCAmelCase__ = SoftFileLock if warnings is not None: warnings.warn('''only soft file lock is available''')
5
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): def __A (self ) -> List[Any]: # clean up the VRAM after each test super().tearDown() gc.collect() def __A (self ) -> Optional[Any]: _lowercase =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/sd2-inpaint/init_image.png''' ) _lowercase =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png''' ) _lowercase ='''xvjiarui/stable-diffusion-2-inpainting''' _lowercase , _lowercase =FlaxStableDiffusionInpaintPipeline.from_pretrained(UpperCAmelCase , safety_checker=UpperCAmelCase ) _lowercase ='''Face of a yellow cat, high resolution, sitting on a park bench''' _lowercase =jax.random.PRNGKey(0 ) _lowercase =5_0 _lowercase =jax.device_count() _lowercase =num_samples * [prompt] _lowercase =num_samples * [init_image] _lowercase =num_samples * [mask_image] _lowercase , _lowercase , _lowercase =pipeline.prepare_inputs(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) # shard inputs and rng _lowercase =replicate(UpperCAmelCase ) _lowercase =jax.random.split(UpperCAmelCase , jax.device_count() ) _lowercase =shard(UpperCAmelCase ) _lowercase =shard(UpperCAmelCase ) _lowercase =shard(UpperCAmelCase ) _lowercase =pipeline( UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , jit=UpperCAmelCase ) _lowercase =output.images.reshape(UpperCAmelCase , 5_1_2 , 5_1_2 , 3 ) _lowercase =images[0, 2_5_3:2_5_6, 2_5_3:2_5_6, -1] _lowercase =jnp.asarray(jax.device_get(image_slice.flatten() ) ) _lowercase =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
5
1
import math def UpperCAmelCase_ ( __snake_case ) -> int: """simple docstring""" if not isinstance(__snake_case , __snake_case ): _lowercase =F"Input value of [number={number}] must be an integer" raise TypeError(__snake_case ) if number < 1: _lowercase =F"Input value of [number={number}] must be > 0" raise ValueError(__snake_case ) elif number == 1: return 3 elif number == 2: return 5 else: _lowercase =int(math.log(number // 3 , 2 ) ) + 2 _lowercase =[3, 5] _lowercase =2 _lowercase =3 for block in range(1 , __snake_case ): for _ in range(__snake_case ): proth_list.append(2 ** (block + 1) + proth_list[proth_index - 1] ) proth_index += 1 increment *= 2 return proth_list[number - 1] if __name__ == "__main__": import doctest doctest.testmod() for number in range(11): UpperCAmelCase__ = 0 try: UpperCAmelCase__ = proth(number) except ValueError: print(f'''ValueError: there is no {number}th Proth number''') continue print(f'''The {number}th Proth number: {value}''')
5
import comet # From: unbabel-comet import torch import datasets UpperCAmelCase__ = datasets.logging.get_logger(__name__) UpperCAmelCase__ = '''\ @inproceedings{rei-EtAl:2020:WMT, author = {Rei, Ricardo and Stewart, Craig and Farinha, Ana C and Lavie, Alon}, title = {Unbabel\'s Participation in the WMT20 Metrics Shared Task}, booktitle = {Proceedings of the Fifth Conference on Machine Translation}, month = {November}, year = {2020}, address = {Online}, publisher = {Association for Computational Linguistics}, pages = {909--918}, } @inproceedings{rei-etal-2020-comet, title = "{COMET}: A Neural Framework for {MT} Evaluation", author = "Rei, Ricardo and Stewart, Craig and Farinha, Ana C and Lavie, Alon", booktitle = "Proceedings of the 2020 Conference on Empirical Methods in Natural Language Processing (EMNLP)", month = nov, year = "2020", address = "Online", publisher = "Association for Computational Linguistics", url = "https://www.aclweb.org/anthology/2020.emnlp-main.213", pages = "2685--2702", } ''' UpperCAmelCase__ = '''\ Crosslingual Optimized Metric for Evaluation of Translation (COMET) is an open-source framework used to train Machine Translation metrics that achieve high levels of correlation with different types of human judgments (HTER, DA\'s or MQM). With the release of the framework the authors also released fully trained models that were used to compete in the WMT20 Metrics Shared Task achieving SOTA in that years competition. See the [README.md] file at https://unbabel.github.io/COMET/html/models.html for more information. ''' UpperCAmelCase__ = ''' COMET score. Args: `sources` (list of str): Source sentences `predictions` (list of str): candidate translations `references` (list of str): reference translations `cuda` (bool): If set to True, runs COMET using GPU `show_progress` (bool): Shows progress `model`: COMET model to be used. Will default to `wmt-large-da-estimator-1719` if None. Returns: `samples`: List of dictionaries with `src`, `mt`, `ref` and `score`. `scores`: List of scores. Examples: >>> comet_metric = datasets.load_metric(\'comet\') >>> # comet_metric = load_metric(\'comet\', \'wmt20-comet-da\') # you can also choose which model to use >>> source = ["Dem Feuer konnte Einhalt geboten werden", "Schulen und Kindergärten wurden eröffnet."] >>> hypothesis = ["The fire could be stopped", "Schools and kindergartens were open"] >>> reference = ["They were able to control the fire.", "Schools and kindergartens opened"] >>> results = comet_metric.compute(predictions=hypothesis, references=reference, sources=source) >>> print([round(v, 2) for v in results["scores"]]) [0.19, 0.92] ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION) class lowerCamelCase__ ( datasets.Metric): def __A (self ) -> Optional[int]: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , homepage='''https://unbabel.github.io/COMET/html/index.html''' , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''sources''': datasets.Value('''string''' , id='''sequence''' ), '''predictions''': datasets.Value('''string''' , id='''sequence''' ), '''references''': datasets.Value('''string''' , id='''sequence''' ), } ) , codebase_urls=['''https://github.com/Unbabel/COMET'''] , reference_urls=[ '''https://github.com/Unbabel/COMET''', '''https://www.aclweb.org/anthology/2020.emnlp-main.213/''', '''http://www.statmt.org/wmt20/pdf/2020.wmt-1.101.pdf6''', ] , ) def __A (self , UpperCAmelCase ) -> Dict: if self.config_name == "default": _lowercase =comet.load_from_checkpoint(comet.download_model('''wmt20-comet-da''' ) ) else: _lowercase =comet.load_from_checkpoint(comet.download_model(self.config_name ) ) def __A (self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase=None , UpperCAmelCase=False ) -> int: if gpus is None: _lowercase =1 if torch.cuda.is_available() else 0 _lowercase ={'''src''': sources, '''mt''': predictions, '''ref''': references} _lowercase =[dict(zip(UpperCAmelCase , UpperCAmelCase ) ) for t in zip(*data.values() )] _lowercase , _lowercase =self.scorer.predict(UpperCAmelCase , gpus=UpperCAmelCase , progress_bar=UpperCAmelCase ) return {"mean_score": mean_score, "scores": scores}
5
1
import sys def UpperCAmelCase_ ( __snake_case ) -> str: """simple docstring""" _lowercase =len(__snake_case ) _lowercase =[[0 for x in range(__snake_case )] for x in range(__snake_case )] _lowercase =[[0 for x in range(__snake_case )] for x in range(__snake_case )] for chain_length in range(2 , __snake_case ): for a in range(1 , n - chain_length + 1 ): _lowercase =a + chain_length - 1 _lowercase =sys.maxsize for c in range(__snake_case , __snake_case ): _lowercase =( matrix[a][c] + matrix[c + 1][b] + array[a - 1] * array[c] * array[b] ) if cost < matrix[a][b]: _lowercase =cost _lowercase =c return matrix, sol def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case ) -> str: """simple docstring""" if i == j: print('''A''' + str(__snake_case ) , end=''' ''' ) else: print('''(''' , end=''' ''' ) print_optiomal_solution(__snake_case , __snake_case , optimal_solution[i][j] ) print_optiomal_solution(__snake_case , optimal_solution[i][j] + 1 , __snake_case ) print(''')''' , end=''' ''' ) def UpperCAmelCase_ ( ) -> Union[str, Any]: """simple docstring""" _lowercase =[30, 35, 15, 5, 10, 20, 25] _lowercase =len(__snake_case ) # Size of matrix created from above array will be # 30*35 35*15 15*5 5*10 10*20 20*25 _lowercase , _lowercase =matrix_chain_order(__snake_case ) print('''No. of Operation required: ''' + str(matrix[1][n - 1] ) ) print_optiomal_solution(__snake_case , 1 , n - 1 ) if __name__ == "__main__": main()
5
# DISCLAIMER: This file is strongly influenced by https://github.com/yang-song/score_sde_pytorch import math from typing import Union import torch from ..configuration_utils import ConfigMixin, register_to_config from ..utils import randn_tensor from .scheduling_utils import SchedulerMixin class lowerCamelCase__ ( lowerCAmelCase , lowerCAmelCase): SCREAMING_SNAKE_CASE__ = 1 @register_to_config def __init__(self , UpperCAmelCase=2_0_0_0 , UpperCAmelCase=0.1 , UpperCAmelCase=2_0 , UpperCAmelCase=1e-3 ) -> List[str]: _lowercase =None _lowercase =None _lowercase =None def __A (self , UpperCAmelCase , UpperCAmelCase = None ) -> str: _lowercase =torch.linspace(1 , self.config.sampling_eps , UpperCAmelCase , device=UpperCAmelCase ) def __A (self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase=None ) -> Optional[int]: if self.timesteps is None: raise ValueError( '''`self.timesteps` is not set, you need to run \'set_timesteps\' after creating the scheduler''' ) # TODO(Patrick) better comments + non-PyTorch # postprocess model score _lowercase =( -0.25 * t**2 * (self.config.beta_max - self.config.beta_min) - 0.5 * t * self.config.beta_min ) _lowercase =torch.sqrt(1.0 - torch.exp(2.0 * log_mean_coeff ) ) _lowercase =std.flatten() while len(std.shape ) < len(score.shape ): _lowercase =std.unsqueeze(-1 ) _lowercase =-score / std # compute _lowercase =-1.0 / len(self.timesteps ) _lowercase =self.config.beta_min + t * (self.config.beta_max - self.config.beta_min) _lowercase =beta_t.flatten() while len(beta_t.shape ) < len(x.shape ): _lowercase =beta_t.unsqueeze(-1 ) _lowercase =-0.5 * beta_t * x _lowercase =torch.sqrt(UpperCAmelCase ) _lowercase =drift - diffusion**2 * score _lowercase =x + drift * dt # add noise _lowercase =randn_tensor(x.shape , layout=x.layout , generator=UpperCAmelCase , device=x.device , dtype=x.dtype ) _lowercase =x_mean + diffusion * math.sqrt(-dt ) * noise return x, x_mean def __len__(self ) -> str: return self.config.num_train_timesteps
5
1
import warnings from ...utils import logging from .image_processing_flava import FlavaImageProcessor UpperCAmelCase__ = logging.get_logger(__name__) class lowerCamelCase__ ( lowerCAmelCase): def __init__(self , *UpperCAmelCase , **UpperCAmelCase ) -> None: warnings.warn( '''The class FlavaFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please''' ''' use FlavaImageProcessor instead.''' , UpperCAmelCase , ) super().__init__(*UpperCAmelCase , **UpperCAmelCase )
5
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( MobileViTConfig, MobileViTForImageClassification, MobileViTForSemanticSegmentation, MobileViTImageProcessor, ) from transformers.utils import logging logging.set_verbosity_info() UpperCAmelCase__ = logging.get_logger(__name__) def UpperCAmelCase_ ( __snake_case ) -> Optional[Any]: """simple docstring""" _lowercase =MobileViTConfig() # size of the architecture if "mobilevit_s" in mobilevit_name: _lowercase =[144, 192, 240] _lowercase =[16, 32, 64, 96, 128, 160, 640] elif "mobilevit_xs" in mobilevit_name: _lowercase =[96, 120, 144] _lowercase =[16, 32, 48, 64, 80, 96, 384] elif "mobilevit_xxs" in mobilevit_name: _lowercase =[64, 80, 96] _lowercase =[16, 16, 24, 48, 64, 80, 320] _lowercase =0.05 _lowercase =2.0 if mobilevit_name.startswith('''deeplabv3_''' ): _lowercase =512 _lowercase =16 _lowercase =21 _lowercase ='''pascal-voc-id2label.json''' else: _lowercase =1000 _lowercase ='''imagenet-1k-id2label.json''' _lowercase ='''huggingface/label-files''' _lowercase =json.load(open(hf_hub_download(__snake_case , __snake_case , repo_type='''dataset''' ) , '''r''' ) ) _lowercase ={int(__snake_case ): v for k, v in idalabel.items()} _lowercase =idalabel _lowercase ={v: k for k, v in idalabel.items()} return config def UpperCAmelCase_ ( __snake_case , __snake_case=False ) -> Tuple: """simple docstring""" for i in range(1 , 6 ): if F"layer_{i}." in name: _lowercase =name.replace(F"layer_{i}." , F"encoder.layer.{i - 1}." ) if "conv_1." in name: _lowercase =name.replace('''conv_1.''' , '''conv_stem.''' ) if ".block." in name: _lowercase =name.replace('''.block.''' , '''.''' ) if "exp_1x1" in name: _lowercase =name.replace('''exp_1x1''' , '''expand_1x1''' ) if "red_1x1" in name: _lowercase =name.replace('''red_1x1''' , '''reduce_1x1''' ) if ".local_rep.conv_3x3." in name: _lowercase =name.replace('''.local_rep.conv_3x3.''' , '''.conv_kxk.''' ) if ".local_rep.conv_1x1." in name: _lowercase =name.replace('''.local_rep.conv_1x1.''' , '''.conv_1x1.''' ) if ".norm." in name: _lowercase =name.replace('''.norm.''' , '''.normalization.''' ) if ".conv." in name: _lowercase =name.replace('''.conv.''' , '''.convolution.''' ) if ".conv_proj." in name: _lowercase =name.replace('''.conv_proj.''' , '''.conv_projection.''' ) for i in range(0 , 2 ): for j in range(0 , 4 ): if F".{i}.{j}." in name: _lowercase =name.replace(F".{i}.{j}." , F".{i}.layer.{j}." ) for i in range(2 , 6 ): for j in range(0 , 4 ): if F".{i}.{j}." in name: _lowercase =name.replace(F".{i}.{j}." , F".{i}." ) if "expand_1x1" in name: _lowercase =name.replace('''expand_1x1''' , '''downsampling_layer.expand_1x1''' ) if "conv_3x3" in name: _lowercase =name.replace('''conv_3x3''' , '''downsampling_layer.conv_3x3''' ) if "reduce_1x1" in name: _lowercase =name.replace('''reduce_1x1''' , '''downsampling_layer.reduce_1x1''' ) for i in range(2 , 5 ): if F".global_rep.{i}.weight" in name: _lowercase =name.replace(F".global_rep.{i}.weight" , '''.layernorm.weight''' ) if F".global_rep.{i}.bias" in name: _lowercase =name.replace(F".global_rep.{i}.bias" , '''.layernorm.bias''' ) if ".global_rep." in name: _lowercase =name.replace('''.global_rep.''' , '''.transformer.''' ) if ".pre_norm_mha.0." in name: _lowercase =name.replace('''.pre_norm_mha.0.''' , '''.layernorm_before.''' ) if ".pre_norm_mha.1.out_proj." in name: _lowercase =name.replace('''.pre_norm_mha.1.out_proj.''' , '''.attention.output.dense.''' ) if ".pre_norm_ffn.0." in name: _lowercase =name.replace('''.pre_norm_ffn.0.''' , '''.layernorm_after.''' ) if ".pre_norm_ffn.1." in name: _lowercase =name.replace('''.pre_norm_ffn.1.''' , '''.intermediate.dense.''' ) if ".pre_norm_ffn.4." in name: _lowercase =name.replace('''.pre_norm_ffn.4.''' , '''.output.dense.''' ) if ".transformer." in name: _lowercase =name.replace('''.transformer.''' , '''.transformer.layer.''' ) if ".aspp_layer." in name: _lowercase =name.replace('''.aspp_layer.''' , '''.''' ) if ".aspp_pool." in name: _lowercase =name.replace('''.aspp_pool.''' , '''.''' ) if "seg_head." in name: _lowercase =name.replace('''seg_head.''' , '''segmentation_head.''' ) if "segmentation_head.classifier.classifier." in name: _lowercase =name.replace('''segmentation_head.classifier.classifier.''' , '''segmentation_head.classifier.''' ) if "classifier.fc." in name: _lowercase =name.replace('''classifier.fc.''' , '''classifier.''' ) elif (not base_model) and ("segmentation_head." not in name): _lowercase ='''mobilevit.''' + name return name def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case=False ) -> Optional[Any]: """simple docstring""" if base_model: _lowercase ='''''' else: _lowercase ='''mobilevit.''' for key in orig_state_dict.copy().keys(): _lowercase =orig_state_dict.pop(__snake_case ) if key[:8] == "encoder.": _lowercase =key[8:] if "qkv" in key: _lowercase =key.split('''.''' ) _lowercase =int(key_split[0][6:] ) - 1 _lowercase =int(key_split[3] ) _lowercase =model.get_submodule(F"{model_prefix}encoder.layer.{layer_num}" ) _lowercase =layer.transformer.layer[transformer_num].attention.attention.all_head_size _lowercase =( F"{model_prefix}encoder.layer.{layer_num}.transformer.layer.{transformer_num}.attention.attention." ) if "weight" in key: _lowercase =val[:dim, :] _lowercase =val[dim : dim * 2, :] _lowercase =val[-dim:, :] else: _lowercase =val[:dim] _lowercase =val[dim : dim * 2] _lowercase =val[-dim:] else: _lowercase =val return orig_state_dict def UpperCAmelCase_ ( ) -> Union[str, Any]: """simple docstring""" _lowercase ='''http://images.cocodataset.org/val2017/000000039769.jpg''' _lowercase =Image.open(requests.get(__snake_case , stream=__snake_case ).raw ) return im @torch.no_grad() def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case=False ) -> int: """simple docstring""" _lowercase =get_mobilevit_config(__snake_case ) # load original state_dict _lowercase =torch.load(__snake_case , map_location='''cpu''' ) # load 🤗 model if mobilevit_name.startswith('''deeplabv3_''' ): _lowercase =MobileViTForSemanticSegmentation(__snake_case ).eval() else: _lowercase =MobileViTForImageClassification(__snake_case ).eval() _lowercase =convert_state_dict(__snake_case , __snake_case ) model.load_state_dict(__snake_case ) # Check outputs on an image, prepared by MobileViTImageProcessor _lowercase =MobileViTImageProcessor(crop_size=config.image_size , size=config.image_size + 32 ) _lowercase =image_processor(images=prepare_img() , return_tensors='''pt''' ) _lowercase =model(**__snake_case ) _lowercase =outputs.logits if mobilevit_name.startswith('''deeplabv3_''' ): assert logits.shape == (1, 21, 32, 32) if mobilevit_name == "deeplabv3_mobilevit_s": _lowercase =torch.tensor( [ [[6.20_65, 6.12_92, 6.20_70], [6.10_79, 6.12_54, 6.17_47], [6.00_42, 6.10_71, 6.10_34]], [[-6.92_53, -6.86_53, -7.03_98], [-7.32_18, -7.39_83, -7.36_70], [-7.19_61, -7.24_82, -7.15_69]], [[-4.47_23, -4.43_48, -4.37_69], [-5.36_29, -5.46_32, -5.45_98], [-5.15_87, -5.34_02, -5.50_59]], ] ) elif mobilevit_name == "deeplabv3_mobilevit_xs": _lowercase =torch.tensor( [ [[5.44_49, 5.57_33, 5.63_14], [5.18_15, 5.39_30, 5.59_63], [5.16_56, 5.43_33, 5.48_53]], [[-9.44_23, -9.77_66, -9.67_14], [-9.15_81, -9.57_20, -9.55_19], [-9.10_06, -9.64_58, -9.57_03]], [[-7.77_21, -7.37_16, -7.15_83], [-8.45_99, -8.06_24, -7.79_44], [-8.41_72, -7.83_66, -7.50_25]], ] ) elif mobilevit_name == "deeplabv3_mobilevit_xxs": _lowercase =torch.tensor( [ [[6.98_11, 6.97_43, 7.31_23], [7.17_77, 7.19_31, 7.39_38], [7.56_33, 7.80_50, 7.89_01]], [[-10.55_36, -10.23_32, -10.29_24], [-10.23_36, -9.86_24, -9.59_64], [-10.88_40, -10.81_58, -10.66_59]], [[-3.49_38, -3.06_31, -2.86_20], [-3.42_05, -2.81_35, -2.68_75], [-3.41_79, -2.79_45, -2.87_50]], ] ) else: raise ValueError(F"Unknown mobilevit_name: {mobilevit_name}" ) assert torch.allclose(logits[0, :3, :3, :3] , __snake_case , atol=1e-4 ) else: assert logits.shape == (1, 1000) if mobilevit_name == "mobilevit_s": _lowercase =torch.tensor([-0.98_66, 0.23_92, -1.12_41] ) elif mobilevit_name == "mobilevit_xs": _lowercase =torch.tensor([-2.47_61, -0.93_99, -1.95_87] ) elif mobilevit_name == "mobilevit_xxs": _lowercase =torch.tensor([-1.93_64, -1.23_27, -0.46_53] ) else: raise ValueError(F"Unknown mobilevit_name: {mobilevit_name}" ) assert torch.allclose(logits[0, :3] , __snake_case , atol=1e-4 ) Path(__snake_case ).mkdir(exist_ok=__snake_case ) print(F"Saving model {mobilevit_name} to {pytorch_dump_folder_path}" ) model.save_pretrained(__snake_case ) print(F"Saving image processor to {pytorch_dump_folder_path}" ) image_processor.save_pretrained(__snake_case ) if push_to_hub: _lowercase ={ '''mobilevit_s''': '''mobilevit-small''', '''mobilevit_xs''': '''mobilevit-x-small''', '''mobilevit_xxs''': '''mobilevit-xx-small''', '''deeplabv3_mobilevit_s''': '''deeplabv3-mobilevit-small''', '''deeplabv3_mobilevit_xs''': '''deeplabv3-mobilevit-x-small''', '''deeplabv3_mobilevit_xxs''': '''deeplabv3-mobilevit-xx-small''', } print('''Pushing to the hub...''' ) _lowercase =model_mapping[mobilevit_name] image_processor.push_to_hub(__snake_case , organization='''apple''' ) model.push_to_hub(__snake_case , organization='''apple''' ) if __name__ == "__main__": UpperCAmelCase__ = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--mobilevit_name''', default='''mobilevit_s''', type=str, help=( '''Name of the MobileViT model you\'d like to convert. Should be one of \'mobilevit_s\', \'mobilevit_xs\',''' ''' \'mobilevit_xxs\', \'deeplabv3_mobilevit_s\', \'deeplabv3_mobilevit_xs\', \'deeplabv3_mobilevit_xxs\'.''' ), ) parser.add_argument( '''--checkpoint_path''', required=True, type=str, help='''Path to the original state dict (.pt 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.mobilevit_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub )
5
1
import argparse import json import os import fairseq import torch from fairseq.data import Dictionary from transformers import ( UniSpeechConfig, UniSpeechForCTC, UniSpeechForPreTraining, WavaVecaFeatureExtractor, WavaVecaPhonemeCTCTokenizer, WavaVecaProcessor, logging, ) logging.set_verbosity_info() UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = { '''post_extract_proj''': '''feature_projection.projection''', '''encoder.pos_conv.0''': '''encoder.pos_conv_embed.conv''', '''self_attn.k_proj''': '''encoder.layers.*.attention.k_proj''', '''self_attn.v_proj''': '''encoder.layers.*.attention.v_proj''', '''self_attn.q_proj''': '''encoder.layers.*.attention.q_proj''', '''self_attn.out_proj''': '''encoder.layers.*.attention.out_proj''', '''self_attn_layer_norm''': '''encoder.layers.*.layer_norm''', '''fc1''': '''encoder.layers.*.feed_forward.intermediate_dense''', '''fc2''': '''encoder.layers.*.feed_forward.output_dense''', '''final_layer_norm''': '''encoder.layers.*.final_layer_norm''', '''encoder.layer_norm''': '''encoder.layer_norm''', '''w2v_model.layer_norm''': '''feature_projection.layer_norm''', '''quantizer.weight_proj''': '''quantizer.weight_proj''', '''quantizer.vars''': '''quantizer.codevectors''', '''project_q''': '''project_q''', '''final_proj''': '''project_hid''', '''w2v_encoder.proj''': '''ctc_proj''', '''mask_emb''': '''masked_spec_embed''', } UpperCAmelCase__ = [ '''ctc_proj''', '''quantizer.weight_proj''', '''quantizer.codevectors''', '''project_q''', '''project_hid''', ] def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case , __snake_case ) -> Dict: """simple docstring""" for attribute in key.split('''.''' ): if is_finetuned: if attribute in ["quantizer", "project_q", "project_hid"]: # those layers are only relevant for pretraining and should be dropped return if attribute == "ctc_proj": # we should rename `ctc_proj` to `lm_head` for fine-tuned phoneme models _lowercase ='''lm_head''' _lowercase =getattr(__snake_case , __snake_case ) if weight_type is not None: _lowercase =getattr(__snake_case , __snake_case ).shape else: _lowercase =hf_pointer.shape assert hf_shape == value.shape, ( F"Shape of hf {key + '.' + weight_type if weight_type is not None else ''} is {hf_shape}, but should be" F" {value.shape} for {full_name}" ) if weight_type == "weight": _lowercase =value elif weight_type == "weight_g": _lowercase =value elif weight_type == "weight_v": _lowercase =value elif weight_type == "bias": _lowercase =value else: _lowercase =value logger.info(F"{key + '.' + weight_type if weight_type is not None else ''} was initialized from {full_name}." ) def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case ) -> List[Any]: """simple docstring""" _lowercase =[] _lowercase =fairseq_model.state_dict() _lowercase =hf_model.unispeech.feature_extractor for name, value in fairseq_dict.items(): _lowercase =False if "conv_layers" in name: load_conv_layer( __snake_case , __snake_case , __snake_case , __snake_case , hf_model.config.feat_extract_norm == '''group''' , ) _lowercase =True else: for key, mapped_key in MAPPING.items(): _lowercase ='''unispeech.''' + mapped_key if mapped_key not in TOP_LEVEL_KEYS else mapped_key if key in name or key.split('''w2v_model.''' )[-1] == name.split('''.''' )[0]: _lowercase =True if "*" in mapped_key: _lowercase =name.split(__snake_case )[0].split('''.''' )[-2] _lowercase =mapped_key.replace('''*''' , __snake_case ) if "weight_g" in name: _lowercase ='''weight_g''' elif "weight_v" in name: _lowercase ='''weight_v''' elif "bias" in name: _lowercase ='''bias''' elif "weight" in name: # TODO: don't match quantizer.weight_proj _lowercase ='''weight''' else: _lowercase =None set_recursively(__snake_case , __snake_case , __snake_case , __snake_case , __snake_case , __snake_case ) continue if not is_used: unused_weights.append(__snake_case ) logger.warning(F"Unused weights: {unused_weights}" ) def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case , __snake_case ) -> Optional[int]: """simple docstring""" _lowercase =full_name.split('''conv_layers.''' )[-1] _lowercase =name.split('''.''' ) _lowercase =int(items[0] ) _lowercase =int(items[1] ) if type_id == 0: if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.bias.data.shape, ( F"{full_name} has size {value.shape}, but" F" {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found." ) _lowercase =value logger.info(F"Feat extract conv layer {layer_id} was initialized from {full_name}." ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.weight.data.shape, ( F"{full_name} has size {value.shape}, but" F" {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found." ) _lowercase =value logger.info(F"Feat extract conv layer {layer_id} was initialized from {full_name}." ) elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm): if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape, ( F"{full_name} has size {value.shape}, but {feature_extractor[layer_id].layer_norm.bias.data.shape} was" " found." ) _lowercase =value logger.info(F"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}." ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape, ( F"{full_name} has size {value.shape}, but" F" {feature_extractor[layer_id].layer_norm.weight.data.shape} was found." ) _lowercase =value logger.info(F"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}." ) else: unused_weights.append(__snake_case ) @torch.no_grad() def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case=None , __snake_case=None , __snake_case=True ) -> List[Any]: """simple docstring""" if config_path is not None: _lowercase =UniSpeechConfig.from_pretrained(__snake_case ) else: _lowercase =UniSpeechConfig() if is_finetuned: if dict_path: _lowercase =Dictionary.load_from_json(__snake_case ) # important change bos & pad token id since CTC symbol is <pad> and # not <s> as in fairseq _lowercase =target_dict.pad_index _lowercase =target_dict.bos_index _lowercase =target_dict.eos_index _lowercase =len(target_dict.symbols ) _lowercase =os.path.join(__snake_case , '''vocab.json''' ) if not os.path.isdir(__snake_case ): logger.error('''--pytorch_dump_folder_path ({}) should be a directory'''.format(__snake_case ) ) return os.makedirs(__snake_case , exist_ok=__snake_case ) _lowercase =target_dict.indices # fairseq has the <pad> and <s> switched _lowercase =42 _lowercase =43 with open(__snake_case , '''w''' , encoding='''utf-8''' ) as vocab_handle: json.dump(__snake_case , __snake_case ) _lowercase =WavaVecaPhonemeCTCTokenizer( __snake_case , unk_token=target_dict.unk_word , pad_token=target_dict.pad_word , bos_token=target_dict.bos_word , eos_token=target_dict.eos_word , word_delimiter_token='''|''' , do_lower_case=__snake_case , ) _lowercase =True if config.feat_extract_norm == '''layer''' else False _lowercase =WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=16000 , padding_value=0 , do_normalize=__snake_case , return_attention_mask=__snake_case , ) _lowercase =WavaVecaProcessor(feature_extractor=__snake_case , tokenizer=__snake_case ) processor.save_pretrained(__snake_case ) _lowercase =UniSpeechForCTC(__snake_case ) else: _lowercase =UniSpeechForPreTraining(__snake_case ) if is_finetuned: _lowercase , _lowercase , _lowercase =fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] , arg_overrides={'''data''': '''/'''.join(dict_path.split('''/''' )[:-1] ), '''w2v_path''': checkpoint_path} ) else: _lowercase , _lowercase , _lowercase =fairseq.checkpoint_utils.load_model_ensemble_and_task([checkpoint_path] ) _lowercase =model[0].eval() recursively_load_weights(__snake_case , __snake_case , __snake_case ) hf_unispeech.save_pretrained(__snake_case ) if __name__ == "__main__": UpperCAmelCase__ = argparse.ArgumentParser() parser.add_argument('''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''') parser.add_argument('''--checkpoint_path''', default=None, type=str, help='''Path to fairseq checkpoint''') parser.add_argument('''--dict_path''', default=None, type=str, help='''Path to dict of fine-tuned model''') parser.add_argument('''--config_path''', default=None, type=str, help='''Path to hf config.json of model to convert''') parser.add_argument( '''--not_finetuned''', action='''store_true''', help='''Whether the model to convert is a fine-tuned model or not''' ) UpperCAmelCase__ = parser.parse_args() convert_unispeech_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.dict_path, not args.not_finetuned )
5
import requests from bsa import BeautifulSoup def UpperCAmelCase_ ( __snake_case = "https://www.worldometers.info/coronavirus" ) -> dict: """simple docstring""" _lowercase =BeautifulSoup(requests.get(__snake_case ).text , '''html.parser''' ) _lowercase =soup.findAll('''h1''' ) _lowercase =soup.findAll('''div''' , {'''class''': '''maincounter-number'''} ) keys += soup.findAll('''span''' , {'''class''': '''panel-title'''} ) values += soup.findAll('''div''' , {'''class''': '''number-table-main'''} ) return {key.text.strip(): value.text.strip() for key, value in zip(__snake_case , __snake_case )} if __name__ == "__main__": print('''\033[1m''' + '''COVID-19 Status of the World''' + '''\033[0m\n''') for key, value in world_covidaa_stats().items(): print(f'''{key}\n{value}\n''')
5
1