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
import os import unittest from huggingface_hub.utils import are_progress_bars_disabled import transformers.models.bart.tokenization_bart from transformers import logging from transformers.testing_utils import CaptureLogger, mockenv, mockenv_context from transformers.utils.logging import disable_progress_bar, enable_progress_bar class UpperCamelCase_ ( unittest.TestCase ): '''simple docstring''' def _UpperCamelCase ( self ) -> Optional[Any]: snake_case_ = logging.get_logger() # the current default level is logging.WARNING snake_case_ = logging.get_verbosity() logging.set_verbosity_error() self.assertEqual(logger.getEffectiveLevel() , logging.get_verbosity() ) logging.set_verbosity_warning() self.assertEqual(logger.getEffectiveLevel() , logging.get_verbosity() ) logging.set_verbosity_info() self.assertEqual(logger.getEffectiveLevel() , logging.get_verbosity() ) logging.set_verbosity_debug() self.assertEqual(logger.getEffectiveLevel() , logging.get_verbosity() ) # restore to the original level logging.set_verbosity(__lowerCAmelCase ) def _UpperCamelCase ( self ) -> str: snake_case_ = logging.get_verbosity() snake_case_ = logging.get_logger('transformers.models.bart.tokenization_bart' ) snake_case_ = 'Testing 1, 2, 3' # should be able to log warnings (if default settings weren't overridden by `pytest --log-level-all`) if level_origin <= logging.WARNING: with CaptureLogger(__lowerCAmelCase ) as cl: logger.warning(__lowerCAmelCase ) self.assertEqual(cl.out , msg + '\n' ) # this is setting the level for all of `transformers.*` loggers logging.set_verbosity_error() # should not be able to log warnings with CaptureLogger(__lowerCAmelCase ) as cl: logger.warning(__lowerCAmelCase ) self.assertEqual(cl.out , '' ) # should be able to log warnings again logging.set_verbosity_warning() with CaptureLogger(__lowerCAmelCase ) as cl: logger.warning(__lowerCAmelCase ) self.assertEqual(cl.out , msg + '\n' ) # restore to the original level logging.set_verbosity(__lowerCAmelCase ) @mockenv(TRANSFORMERS_VERBOSITY='error' ) def _UpperCamelCase ( self ) -> List[Any]: transformers.utils.logging._reset_library_root_logger() # this action activates the env var snake_case_ = logging.get_logger('transformers.models.bart.tokenization_bart' ) snake_case_ = os.getenv('TRANSFORMERS_VERBOSITY' , __lowerCAmelCase ) snake_case_ = logging.log_levels[env_level_str] snake_case_ = logging.get_verbosity() self.assertEqual( __lowerCAmelCase , __lowerCAmelCase , F'''TRANSFORMERS_VERBOSITY={env_level_str}/{env_level}, but internal verbosity is {current_level}''' , ) # restore to the original level snake_case_ = '' transformers.utils.logging._reset_library_root_logger() @mockenv(TRANSFORMERS_VERBOSITY='super-error' ) def _UpperCamelCase ( self ) -> str: transformers.utils.logging._reset_library_root_logger() snake_case_ = logging.logging.getLogger() with CaptureLogger(__lowerCAmelCase ) as cl: # this action activates the env var logging.get_logger('transformers.models.bart.tokenization_bart' ) self.assertIn('Unknown option TRANSFORMERS_VERBOSITY=super-error' , cl.out ) # no need to restore as nothing was changed def _UpperCamelCase ( self ) -> int: transformers.utils.logging._reset_library_root_logger() snake_case_ = logging.get_logger('transformers.models.bart.tokenization_bart' ) snake_case_ = 'Testing 1, 2, 3' with mockenv_context(TRANSFORMERS_NO_ADVISORY_WARNINGS='1' ): # nothing should be logged as env var disables this method with CaptureLogger(__lowerCAmelCase ) as cl: logger.warning_advice(__lowerCAmelCase ) self.assertEqual(cl.out , '' ) with mockenv_context(TRANSFORMERS_NO_ADVISORY_WARNINGS='' ): # should log normally as TRANSFORMERS_NO_ADVISORY_WARNINGS is unset with CaptureLogger(__lowerCAmelCase ) as cl: logger.warning_advice(__lowerCAmelCase ) self.assertEqual(cl.out , msg + '\n' ) def __UpperCAmelCase ( ): disable_progress_bar() assert are_progress_bars_disabled() enable_progress_bar() assert not are_progress_bars_disabled()
178
from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...onnx.utils import compute_effective_axis_dimension from ...utils import logging if TYPE_CHECKING: from ...processing_utils import ProcessorMixin from ...utils import TensorType A : str = logging.get_logger(__name__) A : Union[str, Any] = { '''microsoft/layoutlmv3-base''': '''https://huggingface.co/microsoft/layoutlmv3-base/resolve/main/config.json''', } class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : Optional[Any] = '''layoutlmv3''' def __init__( self : Tuple , __lowerCAmelCase : Optional[int]=5_02_65 , __lowerCAmelCase : Tuple=7_68 , __lowerCAmelCase : Union[str, Any]=12 , __lowerCAmelCase : Any=12 , __lowerCAmelCase : List[Any]=30_72 , __lowerCAmelCase : Optional[int]="gelu" , __lowerCAmelCase : Dict=0.1 , __lowerCAmelCase : List[Any]=0.1 , __lowerCAmelCase : Dict=5_12 , __lowerCAmelCase : Any=2 , __lowerCAmelCase : Dict=0.0_2 , __lowerCAmelCase : List[str]=1e-5 , __lowerCAmelCase : List[str]=1 , __lowerCAmelCase : int=0 , __lowerCAmelCase : Dict=2 , __lowerCAmelCase : Tuple=10_24 , __lowerCAmelCase : List[str]=1_28 , __lowerCAmelCase : Optional[int]=1_28 , __lowerCAmelCase : Any=True , __lowerCAmelCase : Optional[Any]=32 , __lowerCAmelCase : Any=1_28 , __lowerCAmelCase : str=64 , __lowerCAmelCase : Optional[int]=2_56 , __lowerCAmelCase : int=True , __lowerCAmelCase : int=True , __lowerCAmelCase : List[str]=True , __lowerCAmelCase : int=2_24 , __lowerCAmelCase : Dict=3 , __lowerCAmelCase : List[Any]=16 , __lowerCAmelCase : Dict=None , **__lowerCAmelCase : Optional[Any] , ) -> Dict: """simple docstring""" super().__init__( vocab_size=__lowerCAmelCase , hidden_size=__lowerCAmelCase , num_hidden_layers=__lowerCAmelCase , num_attention_heads=__lowerCAmelCase , intermediate_size=__lowerCAmelCase , hidden_act=__lowerCAmelCase , hidden_dropout_prob=__lowerCAmelCase , attention_probs_dropout_prob=__lowerCAmelCase , max_position_embeddings=__lowerCAmelCase , type_vocab_size=__lowerCAmelCase , initializer_range=__lowerCAmelCase , layer_norm_eps=__lowerCAmelCase , pad_token_id=__lowerCAmelCase , bos_token_id=__lowerCAmelCase , eos_token_id=__lowerCAmelCase , **__lowerCAmelCase , ) A__ = max_ad_position_embeddings A__ = coordinate_size A__ = shape_size A__ = has_relative_attention_bias A__ = rel_pos_bins A__ = max_rel_pos A__ = has_spatial_attention_bias A__ = rel_ad_pos_bins A__ = max_rel_ad_pos A__ = text_embed A__ = visual_embed A__ = input_size A__ = num_channels A__ = patch_size A__ = classifier_dropout class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : List[str] = version.parse('''1.12''' ) @property def a_ ( self : int ) -> Mapping[str, Mapping[int, str]]: """simple docstring""" if self.task in ["question-answering", "sequence-classification"]: return OrderedDict( [ ("""input_ids""", {0: """batch""", 1: """sequence"""}), ("""attention_mask""", {0: """batch""", 1: """sequence"""}), ("""bbox""", {0: """batch""", 1: """sequence"""}), ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}), ] ) else: return OrderedDict( [ ("""input_ids""", {0: """batch""", 1: """sequence"""}), ("""bbox""", {0: """batch""", 1: """sequence"""}), ("""attention_mask""", {0: """batch""", 1: """sequence"""}), ("""pixel_values""", {0: """batch""", 1: """num_channels"""}), ] ) @property def a_ ( self : Optional[int] ) -> float: """simple docstring""" return 1e-5 @property def a_ ( self : Tuple ) -> int: """simple docstring""" return 12 def a_ ( self : str , __lowerCAmelCase : "ProcessorMixin" , __lowerCAmelCase : int = -1 , __lowerCAmelCase : int = -1 , __lowerCAmelCase : bool = False , __lowerCAmelCase : Optional["TensorType"] = None , __lowerCAmelCase : int = 3 , __lowerCAmelCase : int = 40 , __lowerCAmelCase : int = 40 , ) -> Mapping[str, Any]: """simple docstring""" setattr(processor.image_processor , """apply_ocr""" , __lowerCAmelCase ) # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX A__ = compute_effective_axis_dimension( __lowerCAmelCase , fixed_dimension=OnnxConfig.default_fixed_batch , num_token_to_add=0 ) # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX A__ = processor.tokenizer.num_special_tokens_to_add(__lowerCAmelCase ) A__ = compute_effective_axis_dimension( __lowerCAmelCase , fixed_dimension=OnnxConfig.default_fixed_sequence , num_token_to_add=__lowerCAmelCase ) # Generate dummy inputs according to compute batch and sequence A__ = [[""" """.join([processor.tokenizer.unk_token] ) * seq_length]] * batch_size # Generate dummy bounding boxes A__ = [[[48, 84, 73, 1_28]]] * batch_size # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX # batch_size = compute_effective_axis_dimension(batch_size, fixed_dimension=OnnxConfig.default_fixed_batch) A__ = self._generate_dummy_images(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) A__ = dict( processor( __lowerCAmelCase , text=__lowerCAmelCase , boxes=__lowerCAmelCase , return_tensors=__lowerCAmelCase , ) ) return inputs
274
0
from typing import Optional, Union import torch from torch import nn from ...configuration_utils import ConfigMixin, register_to_config from ...models.modeling_utils import ModelMixin class UpperCAmelCase__ ( A_ , A_ ): """simple docstring""" @register_to_config def __init__( self , A_ = 768 , ) -> Tuple: super().__init__() __UpperCamelCase =nn.Parameter(torch.zeros(1 , __lowerCAmelCase ) ) __UpperCamelCase =nn.Parameter(torch.ones(1 , __lowerCAmelCase ) ) def _a ( self , A_ = None , A_ = None , ) -> str: __UpperCamelCase =nn.Parameter(self.mean.to(__lowerCAmelCase ).to(__lowerCAmelCase ) ) __UpperCamelCase =nn.Parameter(self.std.to(__lowerCAmelCase ).to(__lowerCAmelCase ) ) return self def _a ( self , A_ ) -> Union[str, Any]: __UpperCamelCase =(embeds - self.mean) * 1.0 / self.std return embeds def _a ( self , A_ ) -> str: __UpperCamelCase =(embeds * self.std) + self.mean return embeds
62
import json import os from pathlib import Path from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple, Union import sentencepiece from ...tokenization_utils import BatchEncoding, PreTrainedTokenizer from ...utils import logging A : Optional[Any] = logging.get_logger(__name__) A : str = '''▁''' A : Any = { '''vocab_file''': '''vocab.json''', '''spm_file''': '''sentencepiece.bpe.model''', '''tokenizer_config_file''': '''tokenizer_config.json''', } A : List[Any] = { '''vocab_file''': { '''facebook/m2m100_418M''': '''https://huggingface.co/facebook/m2m100_418M/resolve/main/vocab.json''', '''facebook/m2m100_1.2B''': '''https://huggingface.co/facebook/m2m100_1.2B/resolve/main/vocab.json''', }, '''spm_file''': { '''facebook/m2m100_418M''': '''https://huggingface.co/facebook/m2m100_418M/resolve/main/sentencepiece.bpe.model''', '''facebook/m2m100_1.2B''': '''https://huggingface.co/facebook/m2m100_1.2B/resolve/main/sentencepiece.bpe.model''', }, '''tokenizer_config_file''': { '''facebook/m2m100_418M''': '''https://huggingface.co/facebook/m2m100_418M/resolve/main/tokenizer_config.json''', '''facebook/m2m100_1.2B''': '''https://huggingface.co/facebook/m2m100_1.2B/resolve/main/tokenizer_config.json''', }, } A : Tuple = { '''facebook/m2m100_418M''': 1_0_2_4, } # fmt: off A : Optional[int] = { '''m2m100''': ['''af''', '''am''', '''ar''', '''ast''', '''az''', '''ba''', '''be''', '''bg''', '''bn''', '''br''', '''bs''', '''ca''', '''ceb''', '''cs''', '''cy''', '''da''', '''de''', '''el''', '''en''', '''es''', '''et''', '''fa''', '''ff''', '''fi''', '''fr''', '''fy''', '''ga''', '''gd''', '''gl''', '''gu''', '''ha''', '''he''', '''hi''', '''hr''', '''ht''', '''hu''', '''hy''', '''id''', '''ig''', '''ilo''', '''is''', '''it''', '''ja''', '''jv''', '''ka''', '''kk''', '''km''', '''kn''', '''ko''', '''lb''', '''lg''', '''ln''', '''lo''', '''lt''', '''lv''', '''mg''', '''mk''', '''ml''', '''mn''', '''mr''', '''ms''', '''my''', '''ne''', '''nl''', '''no''', '''ns''', '''oc''', '''or''', '''pa''', '''pl''', '''ps''', '''pt''', '''ro''', '''ru''', '''sd''', '''si''', '''sk''', '''sl''', '''so''', '''sq''', '''sr''', '''ss''', '''su''', '''sv''', '''sw''', '''ta''', '''th''', '''tl''', '''tn''', '''tr''', '''uk''', '''ur''', '''uz''', '''vi''', '''wo''', '''xh''', '''yi''', '''yo''', '''zh''', '''zu'''], '''wmt21''': ['''en''', '''ha''', '''is''', '''ja''', '''cs''', '''ru''', '''zh''', '''de'''] } class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : Union[str, Any] = VOCAB_FILES_NAMES __lowerCamelCase : List[str] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase : Optional[int] = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase : Dict = ['''input_ids''', '''attention_mask'''] __lowerCamelCase : List[int] = [] __lowerCamelCase : List[int] = [] def __init__( self : List[Any] , __lowerCAmelCase : List[Any] , __lowerCAmelCase : Dict , __lowerCAmelCase : Union[str, Any]=None , __lowerCAmelCase : str=None , __lowerCAmelCase : List[Any]="<s>" , __lowerCAmelCase : List[Any]="</s>" , __lowerCAmelCase : Optional[int]="</s>" , __lowerCAmelCase : Optional[Any]="<pad>" , __lowerCAmelCase : Any="<unk>" , __lowerCAmelCase : Any="m2m100" , __lowerCAmelCase : Optional[Dict[str, Any]] = None , __lowerCAmelCase : Dict=8 , **__lowerCAmelCase : Tuple , ) -> None: """simple docstring""" A__ = {} if sp_model_kwargs is None else sp_model_kwargs A__ = language_codes A__ = FAIRSEQ_LANGUAGE_CODES[language_codes] A__ = {lang_code: f'__{lang_code}__' for lang_code in fairseq_language_code} A__ = kwargs.get("""additional_special_tokens""" , [] ) kwargs["additional_special_tokens"] += [ self.get_lang_token(__lowerCAmelCase ) for lang_code in fairseq_language_code if self.get_lang_token(__lowerCAmelCase ) not in kwargs["additional_special_tokens"] ] super().__init__( src_lang=__lowerCAmelCase , tgt_lang=__lowerCAmelCase , bos_token=__lowerCAmelCase , eos_token=__lowerCAmelCase , sep_token=__lowerCAmelCase , unk_token=__lowerCAmelCase , pad_token=__lowerCAmelCase , language_codes=__lowerCAmelCase , sp_model_kwargs=self.sp_model_kwargs , num_madeup_words=__lowerCAmelCase , **__lowerCAmelCase , ) A__ = vocab_file A__ = load_json(__lowerCAmelCase ) A__ = {v: k for k, v in self.encoder.items()} A__ = spm_file A__ = load_spm(__lowerCAmelCase , self.sp_model_kwargs ) A__ = len(self.encoder ) A__ = { self.get_lang_token(__lowerCAmelCase ): self.encoder_size + i for i, lang_code in enumerate(__lowerCAmelCase ) } A__ = {lang_code: self.encoder_size + i for i, lang_code in enumerate(__lowerCAmelCase )} A__ = {v: k for k, v in self.lang_token_to_id.items()} A__ = src_lang if src_lang is not None else """en""" A__ = tgt_lang A__ = self.get_lang_id(self._src_lang ) self.set_src_lang_special_tokens(self._src_lang ) A__ = num_madeup_words @property def a_ ( self : Optional[int] ) -> int: """simple docstring""" return len(self.encoder ) + len(self.lang_token_to_id ) @property def a_ ( self : Optional[Any] ) -> str: """simple docstring""" return self._src_lang @src_lang.setter def a_ ( self : List[Any] , __lowerCAmelCase : str ) -> None: """simple docstring""" A__ = new_src_lang self.set_src_lang_special_tokens(self._src_lang ) def a_ ( self : Optional[int] , __lowerCAmelCase : str ) -> List[str]: """simple docstring""" return self.sp_model.encode(__lowerCAmelCase , out_type=__lowerCAmelCase ) def a_ ( self : Optional[Any] , __lowerCAmelCase : Dict ) -> Optional[Any]: """simple docstring""" if token in self.lang_token_to_id: return self.lang_token_to_id[token] return self.encoder.get(__lowerCAmelCase , self.encoder[self.unk_token] ) def a_ ( self : Optional[int] , __lowerCAmelCase : int ) -> str: """simple docstring""" if index in self.id_to_lang_token: return self.id_to_lang_token[index] return self.decoder.get(__lowerCAmelCase , self.unk_token ) def a_ ( self : Optional[int] , __lowerCAmelCase : Dict ) -> str: """simple docstring""" A__ = [] A__ = """""" 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(__lowerCAmelCase ) + token A__ = [] else: current_sub_tokens.append(__lowerCAmelCase ) out_string += self.sp_model.decode(__lowerCAmelCase ) return out_string.strip() def a_ ( self : List[str] , __lowerCAmelCase : List[int] , __lowerCAmelCase : Optional[List[int]] = None , __lowerCAmelCase : bool = False ) -> List[int]: """simple docstring""" if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=__lowerCAmelCase , token_ids_a=__lowerCAmelCase , already_has_special_tokens=__lowerCAmelCase ) A__ = [1] * len(self.prefix_tokens ) A__ = [1] * len(self.suffix_tokens ) if token_ids_a is None: return prefix_ones + ([0] * len(__lowerCAmelCase )) + suffix_ones return prefix_ones + ([0] * len(__lowerCAmelCase )) + ([0] * len(__lowerCAmelCase )) + suffix_ones def a_ ( self : Tuple , __lowerCAmelCase : List[int] , __lowerCAmelCase : Optional[List[int]] = None ) -> List[int]: """simple docstring""" if token_ids_a is None: return self.prefix_tokens + token_ids_a + self.suffix_tokens # We don't expect to process pairs, but leave the pair logic for API consistency return self.prefix_tokens + token_ids_a + token_ids_a + self.suffix_tokens def a_ ( self : int ) -> Dict: """simple docstring""" A__ = {self.convert_ids_to_tokens(__lowerCAmelCase ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self : Union[str, Any] ) -> Dict: """simple docstring""" A__ = self.__dict__.copy() A__ = None return state def __setstate__( self : str , __lowerCAmelCase : Dict ) -> None: """simple docstring""" A__ = d # for backward compatibility if not hasattr(self , """sp_model_kwargs""" ): A__ = {} A__ = load_spm(self.spm_file , self.sp_model_kwargs ) def a_ ( self : List[str] , __lowerCAmelCase : str , __lowerCAmelCase : Optional[str] = None ) -> Tuple[str]: """simple docstring""" A__ = Path(__lowerCAmelCase ) if not save_dir.is_dir(): raise OSError(f'{save_directory} should be a directory' ) A__ = save_dir / ( (filename_prefix + """-""" if filename_prefix else """""") + self.vocab_files_names["""vocab_file"""] ) A__ = save_dir / ( (filename_prefix + """-""" if filename_prefix else """""") + self.vocab_files_names["""spm_file"""] ) save_json(self.encoder , __lowerCAmelCase ) if os.path.abspath(self.spm_file ) != os.path.abspath(__lowerCAmelCase ) and os.path.isfile(self.spm_file ): copyfile(self.spm_file , __lowerCAmelCase ) elif not os.path.isfile(self.spm_file ): with open(__lowerCAmelCase , """wb""" ) as fi: A__ = self.sp_model.serialized_model_proto() fi.write(__lowerCAmelCase ) return (str(__lowerCAmelCase ), str(__lowerCAmelCase )) def a_ ( self : str , __lowerCAmelCase : List[str] , __lowerCAmelCase : str = "en" , __lowerCAmelCase : Optional[List[str]] = None , __lowerCAmelCase : str = "ro" , **__lowerCAmelCase : List[Any] , ) -> BatchEncoding: """simple docstring""" A__ = src_lang A__ = tgt_lang self.set_src_lang_special_tokens(self.src_lang ) return super().prepare_seqaseq_batch(__lowerCAmelCase , __lowerCAmelCase , **__lowerCAmelCase ) def a_ ( self : Optional[int] , __lowerCAmelCase : Any , __lowerCAmelCase : Optional[str] , __lowerCAmelCase : Optional[str] , **__lowerCAmelCase : Tuple ) -> Tuple: """simple docstring""" if src_lang is None or tgt_lang is None: raise ValueError("""Translation requires a `src_lang` and a `tgt_lang` for this model""" ) A__ = src_lang A__ = self(__lowerCAmelCase , add_special_tokens=__lowerCAmelCase , **__lowerCAmelCase ) A__ = self.get_lang_id(__lowerCAmelCase ) A__ = tgt_lang_id return inputs def a_ ( self : Dict ) -> int: """simple docstring""" self.set_src_lang_special_tokens(self.src_lang ) def a_ ( self : Union[str, Any] ) -> Union[str, Any]: """simple docstring""" self.set_tgt_lang_special_tokens(self.tgt_lang ) def a_ ( self : str , __lowerCAmelCase : str ) -> None: """simple docstring""" A__ = self.get_lang_token(__lowerCAmelCase ) A__ = self.lang_token_to_id[lang_token] A__ = [self.cur_lang_id] A__ = [self.eos_token_id] def a_ ( self : Tuple , __lowerCAmelCase : str ) -> None: """simple docstring""" A__ = self.get_lang_token(__lowerCAmelCase ) A__ = self.lang_token_to_id[lang_token] A__ = [self.cur_lang_id] A__ = [self.eos_token_id] def a_ ( self : Union[str, Any] , __lowerCAmelCase : str ) -> str: """simple docstring""" return self.lang_code_to_token[lang] def a_ ( self : Union[str, Any] , __lowerCAmelCase : str ) -> int: """simple docstring""" A__ = self.get_lang_token(__lowerCAmelCase ) return self.lang_token_to_id[lang_token] def __lowerCamelCase ( __a :str , __a :Dict[str, Any] ) -> sentencepiece.SentencePieceProcessor: """simple docstring""" A__ = sentencepiece.SentencePieceProcessor(**__a ) spm.Load(str(__a ) ) return spm def __lowerCamelCase ( __a :str ) -> Union[Dict, List]: """simple docstring""" with open(__a , """r""" ) as f: return json.load(__a ) def __lowerCamelCase ( __a :List[Any] , __a :str ) -> None: """simple docstring""" with open(__a , """w""" ) as f: json.dump(__a , __a , indent=2 )
274
0
'''simple docstring''' import math import time from typing import Dict, List, Optional from torch.utils.data import Dataset from transformers import SeqaSeqTrainer, is_torch_tpu_available from transformers.trainer_utils import PredictionOutput, speed_metrics if is_torch_tpu_available(check_device=False): import torch_xla.core.xla_model as xm import torch_xla.debug.metrics as met class A ( SCREAMING_SNAKE_CASE__ ): '''simple docstring''' def __init__(self , *_UpperCAmelCase , _UpperCAmelCase=None , _UpperCAmelCase=None , **_UpperCAmelCase ) -> Union[str, Any]: super().__init__(*__lowerCAmelCase , **__lowerCAmelCase ) __UpperCamelCase : str = eval_examples __UpperCamelCase : List[Any] = post_process_function def a_ (self , _UpperCAmelCase = None , _UpperCAmelCase=None , _UpperCAmelCase = None , _UpperCAmelCase = "eval" , **_UpperCAmelCase , ) -> Dict[str, float]: __UpperCamelCase : Any = gen_kwargs.copy() __UpperCamelCase : List[str] = ( gen_kwargs["max_length"] if gen_kwargs.get("max_length" ) is not None else self.args.generation_max_length ) __UpperCamelCase : Union[str, Any] = ( gen_kwargs["num_beams"] if gen_kwargs.get("num_beams" ) is not None else self.args.generation_num_beams ) __UpperCamelCase : str = gen_kwargs __UpperCamelCase : str = self.eval_dataset if eval_dataset is None else eval_dataset __UpperCamelCase : Dict = self.get_eval_dataloader(__lowerCAmelCase ) __UpperCamelCase : int = self.eval_examples if eval_examples is None else eval_examples # Temporarily disable metric computation, we will do it in the loop here. __UpperCamelCase : Optional[Any] = self.compute_metrics __UpperCamelCase : Optional[Any] = None __UpperCamelCase : List[Any] = time.time() __UpperCamelCase : int = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop try: __UpperCamelCase : str = eval_loop( __lowerCAmelCase , description="Evaluation" , prediction_loss_only=True if compute_metrics is None else None , ignore_keys=__lowerCAmelCase , metric_key_prefix=__lowerCAmelCase , ) finally: __UpperCamelCase : List[Any] = compute_metrics __UpperCamelCase : List[Any] = 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( __lowerCAmelCase , __lowerCAmelCase , 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 __UpperCamelCase : Dict = self.post_process_function(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) __UpperCamelCase : Tuple = self.compute_metrics(__lowerCAmelCase ) # Prefix all keys with metric_key_prefix + '_' for key in list(metrics.keys() ): if not key.startswith(f"{metric_key_prefix}_" ): __UpperCamelCase : Any = metrics.pop(__lowerCAmelCase ) metrics.update(output.metrics ) else: __UpperCamelCase : List[str] = output.metrics if self.args.should_log: # Only the main node log the results by default self.log(__lowerCAmelCase ) 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() ) __UpperCamelCase : Dict = self.callback_handler.on_evaluate(self.args , self.state , self.control , __lowerCAmelCase ) return metrics def a_ (self , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase=None , _UpperCAmelCase = "test" , **_UpperCAmelCase ) -> Any: __UpperCamelCase : Optional[Any] = gen_kwargs.copy() __UpperCamelCase : Union[str, Any] = self.get_test_dataloader(__lowerCAmelCase ) # Temporarily disable metric computation, we will do it in the loop here. __UpperCamelCase : Tuple = self.compute_metrics __UpperCamelCase : Optional[Any] = None __UpperCamelCase : List[str] = time.time() __UpperCamelCase : int = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop try: __UpperCamelCase : Dict = eval_loop( __lowerCAmelCase , description="Prediction" , prediction_loss_only=True if compute_metrics is None else None , ignore_keys=__lowerCAmelCase , metric_key_prefix=__lowerCAmelCase , ) finally: __UpperCamelCase : Any = compute_metrics __UpperCamelCase : int = 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( __lowerCAmelCase , __lowerCAmelCase , 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 __UpperCamelCase : int = self.post_process_function(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , "predict" ) __UpperCamelCase : Union[str, Any] = self.compute_metrics(__lowerCAmelCase ) # Prefix all keys with metric_key_prefix + '_' for key in list(metrics.keys() ): if not key.startswith(f"{metric_key_prefix}_" ): __UpperCamelCase : Optional[Any] = metrics.pop(__lowerCAmelCase ) metrics.update(output.metrics ) return PredictionOutput(predictions=predictions.predictions , label_ids=predictions.label_ids , metrics=__lowerCAmelCase )
298
from __future__ import annotations from PIL import Image # Define glider example A : Any = [ [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [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], ] # Define blinker example A : Optional[Any] = [[0, 1, 0], [0, 1, 0], [0, 1, 0]] def __lowerCamelCase ( __a :list[list[int]] ) -> list[list[int]]: """simple docstring""" A__ = [] for i in range(len(__a ) ): A__ = [] for j in range(len(cells[i] ) ): # Get the number of live neighbours A__ = 0 if i > 0 and j > 0: neighbour_count += cells[i - 1][j - 1] if i > 0: neighbour_count += cells[i - 1][j] if i > 0 and j < len(cells[i] ) - 1: neighbour_count += cells[i - 1][j + 1] if j > 0: neighbour_count += cells[i][j - 1] if j < len(cells[i] ) - 1: neighbour_count += cells[i][j + 1] if i < len(__a ) - 1 and j > 0: neighbour_count += cells[i + 1][j - 1] if i < len(__a ) - 1: neighbour_count += cells[i + 1][j] if i < len(__a ) - 1 and j < len(cells[i] ) - 1: neighbour_count += cells[i + 1][j + 1] # Rules of the game of life (excerpt from Wikipedia): # 1. Any live cell with two or three live neighbours survives. # 2. Any dead cell with three live neighbours becomes a live cell. # 3. All other live cells die in the next generation. # Similarly, all other dead cells stay dead. A__ = cells[i][j] == 1 if ( (alive and 2 <= neighbour_count <= 3) or not alive and neighbour_count == 3 ): next_generation_row.append(1 ) else: next_generation_row.append(0 ) next_generation.append(__a ) return next_generation def __lowerCamelCase ( __a :list[list[int]] , __a :int ) -> list[Image.Image]: """simple docstring""" A__ = [] for _ in range(__a ): # Create output image A__ = Image.new("""RGB""" , (len(cells[0] ), len(__a )) ) A__ = img.load() # Save cells to image for x in range(len(__a ) ): for y in range(len(cells[0] ) ): A__ = 2_5_5 - cells[y][x] * 2_5_5 A__ = (colour, colour, colour) # Save image images.append(__a ) A__ = new_generation(__a ) return images if __name__ == "__main__": A : str = generate_images(GLIDER, 1_6) images[0].save('''out.gif''', save_all=True, append_images=images[1:])
274
0
"""simple docstring""" def lowerCAmelCase__ ( UpperCamelCase__ = 1_0_0_0_0_0_0 ): '''simple docstring''' _a : Tuple = limit + 1 _a : List[str] = [0] * limit for first_term in range(1 , __a ): for n in range(__a , __a , __a ): _a : Optional[int] = first_term + n / first_term if common_difference % 4: # d must be divisble by 4 continue else: common_difference /= 4 if ( first_term > common_difference and first_term < 4 * common_difference ): # since x,y,z are positive integers frequency[n] += 1 # so z>0 and a>d ,also 4d<a _a : Dict = sum(1 for x in frequency[1:limit] if x == 1_0 ) return count if __name__ == "__main__": print(F'''{solution() = }''')
294
from datetime import datetime import requests from bsa import BeautifulSoup if __name__ == "__main__": A : List[str] = input('''Enter image url: ''').strip() print(F'''Downloading image from {url} ...''') A : Any = BeautifulSoup(requests.get(url).content, '''html.parser''') # The image URL is in the content field of the first meta tag with property og:image A : List[Any] = soup.find('''meta''', {'''property''': '''og:image'''})['''content'''] A : Dict = requests.get(image_url).content A : Tuple = F'''{datetime.now():%Y-%m-%d_%H:%M:%S}.jpg''' with open(file_name, '''wb''') as fp: fp.write(image_data) print(F'''Done. Image saved to disk as {file_name}.''')
274
0
__UpperCAmelCase = '''Tobias Carryer''' from time import time class lowerCamelCase : '''simple docstring''' def __init__( self , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase=int(time() ) ) -> List[Any]: # noqa: B008 UpperCAmelCase_ : Tuple = multiplier UpperCAmelCase_ : List[str] = increment UpperCAmelCase_ : List[str] = modulo UpperCAmelCase_ : List[Any] = seed def __UpperCAmelCase ( self ) -> List[str]: UpperCAmelCase_ : List[Any] = (self.multiplier * self.seed + self.increment) % self.modulo return self.seed if __name__ == "__main__": # Show the LCG in action. __UpperCAmelCase = LinearCongruentialGenerator(1664525, 1013904223, 2 << 31) while True: print(lcg.next_number())
29
# This code is adapted from OpenAI's release # https://github.com/openai/human-eval/blob/master/human_eval/execution.py import contextlib import faulthandler import io import multiprocessing import os import platform import signal import tempfile def __lowerCamelCase ( __a :List[str] , __a :List[Any] , __a :Union[str, Any] , __a :List[Any] ) -> Dict: """simple docstring""" A__ = multiprocessing.Manager() A__ = manager.list() A__ = multiprocessing.Process(target=__a , args=(check_program, result, timeout) ) p.start() p.join(timeout=timeout + 1 ) if p.is_alive(): p.kill() if not result: result.append("""timed out""" ) return { "task_id": task_id, "passed": result[0] == "passed", "result": result[0], "completion_id": completion_id, } def __lowerCamelCase ( __a :Optional[Any] , __a :Any , __a :List[Any] ) -> Union[str, Any]: """simple docstring""" with create_tempdir(): # These system calls are needed when cleaning up tempdir. import os import shutil A__ = shutil.rmtree A__ = os.rmdir A__ = os.chdir # Disable functionalities that can make destructive changes to the test. reliability_guard() # Run program. try: A__ = {} with swallow_io(): with time_limit(__a ): exec(__a , __a ) result.append("""passed""" ) except TimeoutException: result.append("""timed out""" ) except BaseException as e: result.append(F'failed: {e}' ) # Needed for cleaning up. A__ = rmtree A__ = rmdir A__ = chdir @contextlib.contextmanager def __lowerCamelCase ( __a :List[str] ) -> Dict: """simple docstring""" def signal_handler(__a :List[Any] , __a :Optional[Any] ): raise TimeoutException("""Timed out!""" ) signal.setitimer(signal.ITIMER_REAL , __a ) signal.signal(signal.SIGALRM , __a ) try: yield finally: signal.setitimer(signal.ITIMER_REAL , 0 ) @contextlib.contextmanager def __lowerCamelCase ( ) -> Union[str, Any]: """simple docstring""" A__ = WriteOnlyStringIO() with contextlib.redirect_stdout(__a ): with contextlib.redirect_stderr(__a ): with redirect_stdin(__a ): yield @contextlib.contextmanager def __lowerCamelCase ( ) -> Dict: """simple docstring""" with tempfile.TemporaryDirectory() as dirname: with chdir(__a ): yield dirname class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' pass class A (io.StringIO ): '''simple docstring''' def a_ ( self : Any , *__lowerCAmelCase : List[str] , **__lowerCAmelCase : str ) -> Dict: """simple docstring""" raise OSError def a_ ( self : Optional[Any] , *__lowerCAmelCase : Any , **__lowerCAmelCase : Optional[int] ) -> str: """simple docstring""" raise OSError def a_ ( self : Optional[Any] , *__lowerCAmelCase : Any , **__lowerCAmelCase : Any ) -> int: """simple docstring""" raise OSError def a_ ( self : str , *__lowerCAmelCase : Any , **__lowerCAmelCase : Union[str, Any] ) -> int: """simple docstring""" return False class A (contextlib._RedirectStream ): # type: ignore '''simple docstring''' __lowerCamelCase : Union[str, Any] = '''stdin''' @contextlib.contextmanager def __lowerCamelCase ( __a :Union[str, Any] ) -> List[str]: """simple docstring""" if root == ".": yield return A__ = os.getcwd() os.chdir(__a ) try: yield except BaseException as exc: raise exc finally: os.chdir(__a ) def __lowerCamelCase ( __a :Union[str, Any]=None ) -> Dict: """simple docstring""" if maximum_memory_bytes is not None: import resource resource.setrlimit(resource.RLIMIT_AS , (maximum_memory_bytes, maximum_memory_bytes) ) resource.setrlimit(resource.RLIMIT_DATA , (maximum_memory_bytes, maximum_memory_bytes) ) if not platform.uname().system == "Darwin": resource.setrlimit(resource.RLIMIT_STACK , (maximum_memory_bytes, maximum_memory_bytes) ) faulthandler.disable() import builtins A__ = None A__ = None import os A__ = """1""" A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None import shutil A__ = None A__ = None A__ = None import subprocess A__ = None # type: ignore A__ = None import sys A__ = None A__ = None A__ = None A__ = None A__ = None
274
0
from ...configuration_utils import PretrainedConfig from ...utils import logging a_ = logging.get_logger(__name__) a_ = { '''bigcode/gpt_bigcode-santacoder''': '''https://huggingface.co/bigcode/gpt_bigcode-santacoder/resolve/main/config.json''', } class lowercase__ ( _UpperCAmelCase ): a_ ='''gpt_bigcode''' a_ =['''past_key_values'''] a_ ={ '''hidden_size''': '''n_embd''', '''max_position_embeddings''': '''n_positions''', '''num_attention_heads''': '''n_head''', '''num_hidden_layers''': '''n_layer''', } def __init__( self , __UpperCAmelCase=50257 , __UpperCAmelCase=1024 , __UpperCAmelCase=768 , __UpperCAmelCase=12 , __UpperCAmelCase=12 , __UpperCAmelCase=None , __UpperCAmelCase="gelu_pytorch_tanh" , __UpperCAmelCase=0.1 , __UpperCAmelCase=0.1 , __UpperCAmelCase=0.1 , __UpperCAmelCase=1E-5 , __UpperCAmelCase=0.02 , __UpperCAmelCase=True , __UpperCAmelCase=True , __UpperCAmelCase=50256 , __UpperCAmelCase=50256 , __UpperCAmelCase=True , __UpperCAmelCase=True , __UpperCAmelCase=True , **__UpperCAmelCase , )-> Any: '''simple docstring''' lowerCAmelCase__ = vocab_size lowerCAmelCase__ = n_positions lowerCAmelCase__ = n_embd lowerCAmelCase__ = n_layer lowerCAmelCase__ = n_head lowerCAmelCase__ = n_inner lowerCAmelCase__ = activation_function lowerCAmelCase__ = resid_pdrop lowerCAmelCase__ = embd_pdrop lowerCAmelCase__ = attn_pdrop lowerCAmelCase__ = layer_norm_epsilon lowerCAmelCase__ = initializer_range lowerCAmelCase__ = scale_attn_weights lowerCAmelCase__ = use_cache lowerCAmelCase__ = attention_softmax_in_fpaa lowerCAmelCase__ = scale_attention_softmax_in_fpaa lowerCAmelCase__ = multi_query lowerCAmelCase__ = bos_token_id lowerCAmelCase__ = eos_token_id super().__init__(bos_token_id=__lowerCAmelCase , eos_token_id=__lowerCAmelCase , **__lowerCAmelCase )
340
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_albert import AlbertTokenizer else: A : Tuple = None A : Optional[Any] = logging.get_logger(__name__) A : Tuple = {'''vocab_file''': '''spiece.model''', '''tokenizer_file''': '''tokenizer.json'''} A : List[str] = { '''vocab_file''': { '''albert-base-v1''': '''https://huggingface.co/albert-base-v1/resolve/main/spiece.model''', '''albert-large-v1''': '''https://huggingface.co/albert-large-v1/resolve/main/spiece.model''', '''albert-xlarge-v1''': '''https://huggingface.co/albert-xlarge-v1/resolve/main/spiece.model''', '''albert-xxlarge-v1''': '''https://huggingface.co/albert-xxlarge-v1/resolve/main/spiece.model''', '''albert-base-v2''': '''https://huggingface.co/albert-base-v2/resolve/main/spiece.model''', '''albert-large-v2''': '''https://huggingface.co/albert-large-v2/resolve/main/spiece.model''', '''albert-xlarge-v2''': '''https://huggingface.co/albert-xlarge-v2/resolve/main/spiece.model''', '''albert-xxlarge-v2''': '''https://huggingface.co/albert-xxlarge-v2/resolve/main/spiece.model''', }, '''tokenizer_file''': { '''albert-base-v1''': '''https://huggingface.co/albert-base-v1/resolve/main/tokenizer.json''', '''albert-large-v1''': '''https://huggingface.co/albert-large-v1/resolve/main/tokenizer.json''', '''albert-xlarge-v1''': '''https://huggingface.co/albert-xlarge-v1/resolve/main/tokenizer.json''', '''albert-xxlarge-v1''': '''https://huggingface.co/albert-xxlarge-v1/resolve/main/tokenizer.json''', '''albert-base-v2''': '''https://huggingface.co/albert-base-v2/resolve/main/tokenizer.json''', '''albert-large-v2''': '''https://huggingface.co/albert-large-v2/resolve/main/tokenizer.json''', '''albert-xlarge-v2''': '''https://huggingface.co/albert-xlarge-v2/resolve/main/tokenizer.json''', '''albert-xxlarge-v2''': '''https://huggingface.co/albert-xxlarge-v2/resolve/main/tokenizer.json''', }, } A : List[str] = { '''albert-base-v1''': 5_1_2, '''albert-large-v1''': 5_1_2, '''albert-xlarge-v1''': 5_1_2, '''albert-xxlarge-v1''': 5_1_2, '''albert-base-v2''': 5_1_2, '''albert-large-v2''': 5_1_2, '''albert-xlarge-v2''': 5_1_2, '''albert-xxlarge-v2''': 5_1_2, } A : Optional[int] = '''▁''' class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : str = VOCAB_FILES_NAMES __lowerCamelCase : List[Any] = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase : Dict = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase : List[str] = AlbertTokenizer def __init__( self : Tuple , __lowerCAmelCase : Tuple=None , __lowerCAmelCase : List[str]=None , __lowerCAmelCase : Optional[Any]=True , __lowerCAmelCase : List[Any]=True , __lowerCAmelCase : str=False , __lowerCAmelCase : Union[str, Any]="[CLS]" , __lowerCAmelCase : int="[SEP]" , __lowerCAmelCase : Dict="<unk>" , __lowerCAmelCase : Dict="[SEP]" , __lowerCAmelCase : Union[str, Any]="<pad>" , __lowerCAmelCase : str="[CLS]" , __lowerCAmelCase : int="[MASK]" , **__lowerCAmelCase : Optional[Any] , ) -> Optional[Any]: """simple docstring""" A__ = ( AddedToken(__lowerCAmelCase , lstrip=__lowerCAmelCase , rstrip=__lowerCAmelCase , normalized=__lowerCAmelCase ) if isinstance(__lowerCAmelCase , __lowerCAmelCase ) else mask_token ) super().__init__( __lowerCAmelCase , tokenizer_file=__lowerCAmelCase , do_lower_case=__lowerCAmelCase , remove_space=__lowerCAmelCase , keep_accents=__lowerCAmelCase , bos_token=__lowerCAmelCase , eos_token=__lowerCAmelCase , unk_token=__lowerCAmelCase , sep_token=__lowerCAmelCase , pad_token=__lowerCAmelCase , cls_token=__lowerCAmelCase , mask_token=__lowerCAmelCase , **__lowerCAmelCase , ) A__ = do_lower_case A__ = remove_space A__ = keep_accents A__ = vocab_file A__ = False if not self.vocab_file else True def a_ ( self : List[Any] , __lowerCAmelCase : List[int] , __lowerCAmelCase : Optional[List[int]] = None ) -> List[int]: """simple docstring""" A__ = [self.sep_token_id] A__ = [self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def a_ ( self : Union[str, Any] , __lowerCAmelCase : List[int] , __lowerCAmelCase : Optional[List[int]] = None ) -> List[int]: """simple docstring""" A__ = [self.sep_token_id] A__ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def a_ ( self : Tuple , __lowerCAmelCase : str , __lowerCAmelCase : Optional[str] = None ) -> Tuple[str]: """simple docstring""" 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(__lowerCAmelCase ): logger.error(f'Vocabulary path ({save_directory}) should be a directory' ) return A__ = os.path.join( __lowerCAmelCase , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(__lowerCAmelCase ): copyfile(self.vocab_file , __lowerCAmelCase ) return (out_vocab_file,)
274
0
import json import os from pathlib import Path from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple, Union import sentencepiece from ...tokenization_utils import BatchEncoding, PreTrainedTokenizer from ...utils import logging _A = logging.get_logger(__name__) _A = '''▁''' _A = { '''vocab_file''': '''vocab.json''', '''spm_file''': '''sentencepiece.bpe.model''', '''tokenizer_config_file''': '''tokenizer_config.json''', } _A = { '''vocab_file''': { '''facebook/m2m100_418M''': '''https://huggingface.co/facebook/m2m100_418M/resolve/main/vocab.json''', '''facebook/m2m100_1.2B''': '''https://huggingface.co/facebook/m2m100_1.2B/resolve/main/vocab.json''', }, '''spm_file''': { '''facebook/m2m100_418M''': '''https://huggingface.co/facebook/m2m100_418M/resolve/main/sentencepiece.bpe.model''', '''facebook/m2m100_1.2B''': '''https://huggingface.co/facebook/m2m100_1.2B/resolve/main/sentencepiece.bpe.model''', }, '''tokenizer_config_file''': { '''facebook/m2m100_418M''': '''https://huggingface.co/facebook/m2m100_418M/resolve/main/tokenizer_config.json''', '''facebook/m2m100_1.2B''': '''https://huggingface.co/facebook/m2m100_1.2B/resolve/main/tokenizer_config.json''', }, } _A = { '''facebook/m2m100_418M''': 1_024, } # fmt: off _A = { '''m2m100''': ['''af''', '''am''', '''ar''', '''ast''', '''az''', '''ba''', '''be''', '''bg''', '''bn''', '''br''', '''bs''', '''ca''', '''ceb''', '''cs''', '''cy''', '''da''', '''de''', '''el''', '''en''', '''es''', '''et''', '''fa''', '''ff''', '''fi''', '''fr''', '''fy''', '''ga''', '''gd''', '''gl''', '''gu''', '''ha''', '''he''', '''hi''', '''hr''', '''ht''', '''hu''', '''hy''', '''id''', '''ig''', '''ilo''', '''is''', '''it''', '''ja''', '''jv''', '''ka''', '''kk''', '''km''', '''kn''', '''ko''', '''lb''', '''lg''', '''ln''', '''lo''', '''lt''', '''lv''', '''mg''', '''mk''', '''ml''', '''mn''', '''mr''', '''ms''', '''my''', '''ne''', '''nl''', '''no''', '''ns''', '''oc''', '''or''', '''pa''', '''pl''', '''ps''', '''pt''', '''ro''', '''ru''', '''sd''', '''si''', '''sk''', '''sl''', '''so''', '''sq''', '''sr''', '''ss''', '''su''', '''sv''', '''sw''', '''ta''', '''th''', '''tl''', '''tn''', '''tr''', '''uk''', '''ur''', '''uz''', '''vi''', '''wo''', '''xh''', '''yi''', '''yo''', '''zh''', '''zu'''], '''wmt21''': ['''en''', '''ha''', '''is''', '''ja''', '''cs''', '''ru''', '''zh''', '''de'''] } class lowercase_ ( __SCREAMING_SNAKE_CASE ): A__ : Union[str, Any] = VOCAB_FILES_NAMES A__ : List[str] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES A__ : Optional[int] = PRETRAINED_VOCAB_FILES_MAP A__ : Dict = ['''input_ids''', '''attention_mask'''] A__ : List[int] = [] A__ : List[int] = [] def __init__( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase=None , __UpperCamelCase=None , __UpperCamelCase="<s>" , __UpperCamelCase="</s>" , __UpperCamelCase="</s>" , __UpperCamelCase="<pad>" , __UpperCamelCase="<unk>" , __UpperCamelCase="m2m100" , __UpperCamelCase = None , __UpperCamelCase=8 , **__UpperCamelCase , ): """simple docstring""" UpperCamelCase_ = {} if sp_model_kwargs is None else sp_model_kwargs UpperCamelCase_ = language_codes UpperCamelCase_ = FAIRSEQ_LANGUAGE_CODES[language_codes] UpperCamelCase_ = {lang_code: f'''__{lang_code}__''' for lang_code in fairseq_language_code} UpperCamelCase_ = kwargs.get("""additional_special_tokens""" , [] ) kwargs["additional_special_tokens"] += [ self.get_lang_token(__lowerCAmelCase ) for lang_code in fairseq_language_code if self.get_lang_token(__lowerCAmelCase ) not in kwargs["additional_special_tokens"] ] super().__init__( src_lang=__lowerCAmelCase , tgt_lang=__lowerCAmelCase , bos_token=__lowerCAmelCase , eos_token=__lowerCAmelCase , sep_token=__lowerCAmelCase , unk_token=__lowerCAmelCase , pad_token=__lowerCAmelCase , language_codes=__lowerCAmelCase , sp_model_kwargs=self.sp_model_kwargs , num_madeup_words=__lowerCAmelCase , **__lowerCAmelCase , ) UpperCamelCase_ = vocab_file UpperCamelCase_ = load_json(__lowerCAmelCase ) UpperCamelCase_ = {v: k for k, v in self.encoder.items()} UpperCamelCase_ = spm_file UpperCamelCase_ = load_spm(__lowerCAmelCase , self.sp_model_kwargs ) UpperCamelCase_ = len(self.encoder ) UpperCamelCase_ = { self.get_lang_token(__lowerCAmelCase ): self.encoder_size + i for i, lang_code in enumerate(__lowerCAmelCase ) } UpperCamelCase_ = {lang_code: self.encoder_size + i for i, lang_code in enumerate(__lowerCAmelCase )} UpperCamelCase_ = {v: k for k, v in self.lang_token_to_id.items()} UpperCamelCase_ = src_lang if src_lang is not None else """en""" UpperCamelCase_ = tgt_lang UpperCamelCase_ = self.get_lang_id(self._src_lang ) self.set_src_lang_special_tokens(self._src_lang ) UpperCamelCase_ = num_madeup_words @property def lowerCamelCase_ ( self ): """simple docstring""" return len(self.encoder ) + len(self.lang_token_to_id ) @property def lowerCamelCase_ ( self ): """simple docstring""" return self._src_lang @src_lang.setter def lowerCamelCase_ ( self , __UpperCamelCase ): """simple docstring""" UpperCamelCase_ = new_src_lang self.set_src_lang_special_tokens(self._src_lang ) def lowerCamelCase_ ( self , __UpperCamelCase ): """simple docstring""" return self.sp_model.encode(__lowerCAmelCase , out_type=__lowerCAmelCase ) def lowerCamelCase_ ( self , __UpperCamelCase ): """simple docstring""" if token in self.lang_token_to_id: return self.lang_token_to_id[token] return self.encoder.get(__lowerCAmelCase , self.encoder[self.unk_token] ) def lowerCamelCase_ ( self , __UpperCamelCase ): """simple docstring""" if index in self.id_to_lang_token: return self.id_to_lang_token[index] return self.decoder.get(__lowerCAmelCase , self.unk_token ) def lowerCamelCase_ ( self , __UpperCamelCase ): """simple docstring""" UpperCamelCase_ = [] UpperCamelCase_ = """""" 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(__lowerCAmelCase ) + token UpperCamelCase_ = [] else: current_sub_tokens.append(__lowerCAmelCase ) out_string += self.sp_model.decode(__lowerCAmelCase ) return out_string.strip() def lowerCamelCase_ ( self , __UpperCamelCase , __UpperCamelCase = None , __UpperCamelCase = False ): """simple docstring""" if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=__lowerCAmelCase , token_ids_a=__lowerCAmelCase , already_has_special_tokens=__lowerCAmelCase ) UpperCamelCase_ = [1] * len(self.prefix_tokens ) UpperCamelCase_ = [1] * len(self.suffix_tokens ) if token_ids_a is None: return prefix_ones + ([0] * len(__lowerCAmelCase )) + suffix_ones return prefix_ones + ([0] * len(__lowerCAmelCase )) + ([0] * len(__lowerCAmelCase )) + suffix_ones def lowerCamelCase_ ( self , __UpperCamelCase , __UpperCamelCase = None ): """simple docstring""" if token_ids_a is None: return self.prefix_tokens + token_ids_a + self.suffix_tokens # We don't expect to process pairs, but leave the pair logic for API consistency return self.prefix_tokens + token_ids_a + token_ids_a + self.suffix_tokens def lowerCamelCase_ ( self ): """simple docstring""" UpperCamelCase_ = {self.convert_ids_to_tokens(__lowerCAmelCase ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self ): """simple docstring""" UpperCamelCase_ = self.__dict__.copy() UpperCamelCase_ = None return state def __setstate__( self , __UpperCamelCase ): """simple docstring""" UpperCamelCase_ = d # for backward compatibility if not hasattr(self , """sp_model_kwargs""" ): UpperCamelCase_ = {} UpperCamelCase_ = load_spm(self.spm_file , self.sp_model_kwargs ) def lowerCamelCase_ ( self , __UpperCamelCase , __UpperCamelCase = None ): """simple docstring""" UpperCamelCase_ = Path(__lowerCAmelCase ) if not save_dir.is_dir(): raise OSError(f'''{save_directory} should be a directory''' ) UpperCamelCase_ = save_dir / ( (filename_prefix + """-""" if filename_prefix else """""") + self.vocab_files_names["""vocab_file"""] ) UpperCamelCase_ = save_dir / ( (filename_prefix + """-""" if filename_prefix else """""") + self.vocab_files_names["""spm_file"""] ) save_json(self.encoder , __lowerCAmelCase ) if os.path.abspath(self.spm_file ) != os.path.abspath(__lowerCAmelCase ) and os.path.isfile(self.spm_file ): copyfile(self.spm_file , __lowerCAmelCase ) elif not os.path.isfile(self.spm_file ): with open(__lowerCAmelCase , """wb""" ) as fi: UpperCamelCase_ = self.sp_model.serialized_model_proto() fi.write(__lowerCAmelCase ) return (str(__lowerCAmelCase ), str(__lowerCAmelCase )) def lowerCamelCase_ ( self , __UpperCamelCase , __UpperCamelCase = "en" , __UpperCamelCase = None , __UpperCamelCase = "ro" , **__UpperCamelCase , ): """simple docstring""" UpperCamelCase_ = src_lang UpperCamelCase_ = tgt_lang self.set_src_lang_special_tokens(self.src_lang ) return super().prepare_seqaseq_batch(__lowerCAmelCase , __lowerCAmelCase , **__lowerCAmelCase ) def lowerCamelCase_ ( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , **__UpperCamelCase ): """simple docstring""" if src_lang is None or tgt_lang is None: raise ValueError("""Translation requires a `src_lang` and a `tgt_lang` for this model""" ) UpperCamelCase_ = src_lang UpperCamelCase_ = self(__lowerCAmelCase , add_special_tokens=__lowerCAmelCase , **__lowerCAmelCase ) UpperCamelCase_ = self.get_lang_id(__lowerCAmelCase ) UpperCamelCase_ = tgt_lang_id return inputs def lowerCamelCase_ ( self ): """simple docstring""" self.set_src_lang_special_tokens(self.src_lang ) def lowerCamelCase_ ( self ): """simple docstring""" self.set_tgt_lang_special_tokens(self.tgt_lang ) def lowerCamelCase_ ( self , __UpperCamelCase ): """simple docstring""" UpperCamelCase_ = self.get_lang_token(__lowerCAmelCase ) UpperCamelCase_ = self.lang_token_to_id[lang_token] UpperCamelCase_ = [self.cur_lang_id] UpperCamelCase_ = [self.eos_token_id] def lowerCamelCase_ ( self , __UpperCamelCase ): """simple docstring""" UpperCamelCase_ = self.get_lang_token(__lowerCAmelCase ) UpperCamelCase_ = self.lang_token_to_id[lang_token] UpperCamelCase_ = [self.cur_lang_id] UpperCamelCase_ = [self.eos_token_id] def lowerCamelCase_ ( self , __UpperCamelCase ): """simple docstring""" return self.lang_code_to_token[lang] def lowerCamelCase_ ( self , __UpperCamelCase ): """simple docstring""" UpperCamelCase_ = self.get_lang_token(__lowerCAmelCase ) return self.lang_token_to_id[lang_token] def lowerCamelCase__ ( a__ : str , a__ : Dict[str, Any] ) -> sentencepiece.SentencePieceProcessor: UpperCamelCase_ = sentencepiece.SentencePieceProcessor(**__a ) spm.Load(str(__a ) ) return spm def lowerCamelCase__ ( a__ : str ) -> Union[Dict, List]: with open(__a , """r""" ) as f: return json.load(__a ) def lowerCamelCase__ ( a__ : List[Any] , a__ : str ) -> None: with open(__a , """w""" ) as f: json.dump(__a , __a , indent=2 )
122
import dataclasses import json import warnings from dataclasses import dataclass, field from time import time from typing import List from ..utils import logging A : Dict = logging.get_logger(__name__) def __lowerCamelCase ( __a :int=None , __a :Optional[Any]=None ) -> int: """simple docstring""" return field(default_factory=lambda: default , metadata=__a ) @dataclass class A : '''simple docstring''' __lowerCamelCase : List[str] = list_field( default=[] , metadata={ '''help''': ( '''Model checkpoints to be provided to the AutoModel classes. Leave blank to benchmark the base version''' ''' of all available models''' ) } , ) __lowerCamelCase : List[int] = list_field( default=[8] , metadata={'''help''': '''List of batch sizes for which memory and time performance will be evaluated'''} ) __lowerCamelCase : List[int] = list_field( default=[8, 32, 128, 512] , metadata={'''help''': '''List of sequence lengths for which memory and time performance will be evaluated'''} , ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Whether to benchmark inference of model. Inference can be disabled via --no-inference.'''} , ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Whether to run on available cuda devices. Cuda can be disabled via --no-cuda.'''} , ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Whether to run on available tpu devices. TPU can be disabled via --no-tpu.'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Use FP16 to accelerate inference.'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Benchmark training of model'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Verbose memory tracing'''} ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Whether to perform speed measurements. Speed measurements can be disabled via --no-speed.'''} , ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={ '''help''': '''Whether to perform memory measurements. Memory measurements can be disabled via --no-memory''' } , ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Trace memory line by line'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Save result to a CSV file'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Save all print statements in a log file'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Whether to print environment information'''} ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={ '''help''': ( '''Whether to use multiprocessing for memory and speed measurement. It is highly recommended to use''' ''' multiprocessing for accurate CPU and GPU memory measurements. This option should only be disabled''' ''' for debugging / testing and on TPU.''' ) } , ) __lowerCamelCase : str = field( default=F'''inference_time_{round(time() )}.csv''' , metadata={'''help''': '''CSV filename used if saving time results to csv.'''} , ) __lowerCamelCase : str = field( default=F'''inference_memory_{round(time() )}.csv''' , metadata={'''help''': '''CSV filename used if saving memory results to csv.'''} , ) __lowerCamelCase : str = field( default=F'''train_time_{round(time() )}.csv''' , metadata={'''help''': '''CSV filename used if saving time results to csv for training.'''} , ) __lowerCamelCase : str = field( default=F'''train_memory_{round(time() )}.csv''' , metadata={'''help''': '''CSV filename used if saving memory results to csv for training.'''} , ) __lowerCamelCase : str = field( default=F'''env_info_{round(time() )}.csv''' , metadata={'''help''': '''CSV filename used if saving environment information.'''} , ) __lowerCamelCase : str = field( default=F'''log_{round(time() )}.csv''' , metadata={'''help''': '''Log filename used if print statements are saved in log.'''} , ) __lowerCamelCase : int = field(default=3 , metadata={'''help''': '''Times an experiment will be run.'''} ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={ '''help''': ( '''Instead of loading the model as defined in `config.architectures` if exists, just load the pretrain''' ''' model weights.''' ) } , ) def a_ ( self : Dict ) -> Union[str, Any]: """simple docstring""" warnings.warn( f'The class {self.__class__} is deprecated. Hugging Face Benchmarking utils' """ are deprecated in general and it is advised to use external Benchmarking libraries """ """ to benchmark Transformer models.""" , __lowerCAmelCase , ) def a_ ( self : Union[str, Any] ) -> List[str]: """simple docstring""" return json.dumps(dataclasses.asdict(self ) , indent=2 ) @property def a_ ( self : Tuple ) -> List[str]: """simple docstring""" if len(self.models ) <= 0: raise ValueError( """Please make sure you provide at least one model name / model identifier, *e.g.* `--models""" """ bert-base-cased` or `args.models = ['bert-base-cased'].""" ) return self.models @property def a_ ( self : Union[str, Any] ) -> Optional[Any]: """simple docstring""" if not self.multi_process: return False elif self.is_tpu: logger.info("""Multiprocessing is currently not possible on TPU.""" ) return False else: return True
274
0
from __future__ import annotations UpperCAmelCase__ = list[tuple[int, int]] UpperCAmelCase__ = [ [0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles [0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0], [1, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0], ] UpperCAmelCase__ = ([-1, 0], [0, -1], [1, 0], [0, 1]) # up, left, down, right class lowerCamelCase__ : def __init__(self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , ) -> Dict: _lowercase =pos_x _lowercase =pos_y _lowercase =(pos_y, pos_x) _lowercase =goal_x _lowercase =goal_y _lowercase =g_cost _lowercase =parent _lowercase =self.calculate_heuristic() def __A (self ) -> float: _lowercase =abs(self.pos_x - self.goal_x ) _lowercase =abs(self.pos_y - self.goal_y ) return dx + dy def __lt__(self , UpperCAmelCase ) -> bool: return self.f_cost < other.f_cost class lowerCamelCase__ : def __init__(self , UpperCAmelCase , UpperCAmelCase ) -> List[Any]: _lowercase =Node(start[1] , start[0] , goal[1] , goal[0] , 0 , __lowerCAmelCase ) _lowercase =Node(goal[1] , goal[0] , goal[1] , goal[0] , 9_9_9_9_9 , __lowerCAmelCase ) _lowercase =[self.start] _lowercase =[] _lowercase =False def __A (self ) -> Path | None: while self.open_nodes: # Open Nodes are sorted using __lt__ self.open_nodes.sort() _lowercase =self.open_nodes.pop(0 ) if current_node.pos == self.target.pos: _lowercase =True return self.retrace_path(__lowerCAmelCase ) self.closed_nodes.append(__lowerCAmelCase ) _lowercase =self.get_successors(__lowerCAmelCase ) for child_node in successors: if child_node in self.closed_nodes: continue if child_node not in self.open_nodes: self.open_nodes.append(__lowerCAmelCase ) else: # retrieve the best current path _lowercase =self.open_nodes.pop(self.open_nodes.index(__lowerCAmelCase ) ) if child_node.g_cost < better_node.g_cost: self.open_nodes.append(__lowerCAmelCase ) else: self.open_nodes.append(__lowerCAmelCase ) if not self.reached: return [self.start.pos] return None def __A (self , UpperCAmelCase ) -> list[Node]: _lowercase =[] for action in delta: _lowercase =parent.pos_x + action[1] _lowercase =parent.pos_y + action[0] if not (0 <= pos_x <= len(grid[0] ) - 1 and 0 <= pos_y <= len(__lowerCAmelCase ) - 1): continue if grid[pos_y][pos_x] != 0: continue successors.append( Node( __lowerCAmelCase , __lowerCAmelCase , self.target.pos_y , self.target.pos_x , parent.g_cost + 1 , __lowerCAmelCase , ) ) return successors def __A (self , UpperCAmelCase ) -> Path: _lowercase =node _lowercase =[] while current_node is not None: path.append((current_node.pos_y, current_node.pos_x) ) _lowercase =current_node.parent path.reverse() return path if __name__ == "__main__": UpperCAmelCase__ = (0, 0) UpperCAmelCase__ = (len(grid) - 1, len(grid[0]) - 1) for elem in grid: print(elem) print('''------''') UpperCAmelCase__ = GreedyBestFirst(init, goal) UpperCAmelCase__ = greedy_bf.search() if path: for pos_x, pos_y in path: UpperCAmelCase__ = 2 for elem in grid: print(elem)
5
from math import ceil def __lowerCamelCase ( __a :int = 1_0_0_1 ) -> int: """simple docstring""" A__ = 1 for i in range(1 , int(ceil(n / 2.0 ) ) ): A__ = 2 * i + 1 A__ = 2 * i A__ = total + 4 * odd**2 - 6 * even return total if __name__ == "__main__": import sys if len(sys.argv) == 1: print(solution()) else: try: A : List[str] = int(sys.argv[1]) print(solution(n)) except ValueError: print('''Invalid entry - please enter a number''')
274
0
'''simple docstring''' from typing import List, Optional import numpy as np from ...processing_utils import ProcessorMixin from ...utils import to_numpy class lowerCAmelCase_ ( __magic_name__ ): __lowerCamelCase : Tuple = '''EncodecFeatureExtractor''' __lowerCamelCase : List[str] = ('''T5Tokenizer''', '''T5TokenizerFast''') def __init__( self , _lowerCAmelCase , _lowerCAmelCase ) -> List[str]: super().__init__(__lowerCAmelCase , __lowerCAmelCase ) _lowerCAmelCase = self.feature_extractor _lowerCAmelCase = False def _snake_case ( self , _lowerCAmelCase=None , _lowerCAmelCase=None , _lowerCAmelCase=True ) -> List[str]: return self.tokenizer.get_decoder_prompt_ids(task=__lowerCAmelCase , language=__lowerCAmelCase , no_timestamps=__lowerCAmelCase ) def __call__( self , *_lowerCAmelCase , **_lowerCAmelCase ) -> str: if self._in_target_context_manager: return self.current_processor(*__lowerCAmelCase , **__lowerCAmelCase ) _lowerCAmelCase = kwargs.pop("audio" , __lowerCAmelCase ) _lowerCAmelCase = kwargs.pop("sampling_rate" , __lowerCAmelCase ) _lowerCAmelCase = kwargs.pop("text" , __lowerCAmelCase ) if len(__lowerCAmelCase ) > 0: _lowerCAmelCase = args[0] _lowerCAmelCase = args[1:] if audio is None and text is None: raise ValueError("You need to specify either an `audio` or `text` input to process." ) if text is not None: _lowerCAmelCase = self.tokenizer(__lowerCAmelCase , **__lowerCAmelCase ) if audio is not None: _lowerCAmelCase = self.feature_extractor(__lowerCAmelCase , *__lowerCAmelCase , sampling_rate=__lowerCAmelCase , **__lowerCAmelCase ) if audio is None: return inputs elif text is None: return audio_inputs else: _lowerCAmelCase = audio_inputs["input_values"] if "padding_mask" in audio_inputs: _lowerCAmelCase = audio_inputs["padding_mask"] return inputs def _snake_case ( self , *_lowerCAmelCase , **_lowerCAmelCase ) -> Any: _lowerCAmelCase = kwargs.pop("audio" , __lowerCAmelCase ) _lowerCAmelCase = kwargs.pop("padding_mask" , __lowerCAmelCase ) if len(__lowerCAmelCase ) > 0: _lowerCAmelCase = args[0] _lowerCAmelCase = args[1:] if audio_values is not None: return self._decode_audio(__lowerCAmelCase , padding_mask=__lowerCAmelCase ) else: return self.tokenizer.batch_decode(*__lowerCAmelCase , **__lowerCAmelCase ) def _snake_case ( self , *_lowerCAmelCase , **_lowerCAmelCase ) -> Any: return self.tokenizer.decode(*__lowerCAmelCase , **__lowerCAmelCase ) def _snake_case ( self , _lowerCAmelCase , _lowerCAmelCase = None ) -> List[np.ndarray]: _lowerCAmelCase = to_numpy(__lowerCAmelCase ) _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase = audio_values.shape if padding_mask is None: return list(__lowerCAmelCase ) _lowerCAmelCase = to_numpy(__lowerCAmelCase ) # match the sequence length of the padding mask to the generated audio arrays by padding with the **non-padding** # token (so that the generated audio values are **not** treated as padded tokens) _lowerCAmelCase = seq_len - padding_mask.shape[-1] _lowerCAmelCase = 1 - self.feature_extractor.padding_value _lowerCAmelCase = np.pad(__lowerCAmelCase , ((0, 0), (0, difference)) , "constant" , constant_values=__lowerCAmelCase ) _lowerCAmelCase = audio_values.tolist() for i in range(__lowerCAmelCase ): _lowerCAmelCase = np.asarray(audio_values[i] )[ padding_mask[i][None, :] != self.feature_extractor.padding_value ] _lowerCAmelCase = sliced_audio.reshape(__lowerCAmelCase , -1 ) return audio_values
158
import argparse import csv import logging import os import random import numpy as np import torch from torch.utils.data import DataLoader, RandomSampler, SequentialSampler, TensorDataset from tqdm import tqdm, trange from transformers import ( CONFIG_NAME, WEIGHTS_NAME, AdamW, OpenAIGPTDoubleHeadsModel, OpenAIGPTTokenizer, get_linear_schedule_with_warmup, ) logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''', datefmt='''%m/%d/%Y %H:%M:%S''', level=logging.INFO ) A : Tuple = logging.getLogger(__name__) def __lowerCamelCase ( __a :Optional[int] , __a :List[str] ) -> Tuple: """simple docstring""" A__ = np.argmax(__a , axis=1 ) return np.sum(outputs == labels ) def __lowerCamelCase ( __a :Tuple ) -> Dict: """simple docstring""" with open(__a , encoding="""utf_8""" ) as f: A__ = csv.reader(__a ) A__ = [] next(__a ) # skip the first line for line in tqdm(__a ): output.append((""" """.join(line[1:5] ), line[5], line[6], int(line[-1] ) - 1) ) return output def __lowerCamelCase ( __a :Optional[int] , __a :List[Any] , __a :Dict , __a :Optional[Any] , __a :Optional[Any] , __a :int ) -> Union[str, Any]: """simple docstring""" A__ = [] for dataset in encoded_datasets: A__ = len(__a ) A__ = np.zeros((n_batch, 2, input_len) , dtype=np.intaa ) A__ = np.zeros((n_batch, 2) , dtype=np.intaa ) A__ = np.full((n_batch, 2, input_len) , fill_value=-1_0_0 , dtype=np.intaa ) A__ = np.zeros((n_batch,) , dtype=np.intaa ) for ( i, (story, conta, conta, mc_label), ) in enumerate(__a ): A__ = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] A__ = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] A__ = with_conta A__ = with_conta A__ = len(__a ) - 1 A__ = len(__a ) - 1 A__ = with_conta A__ = with_conta A__ = mc_label A__ = (input_ids, mc_token_ids, lm_labels, mc_labels) tensor_datasets.append(tuple(torch.tensor(__a ) for t in all_inputs ) ) return tensor_datasets def __lowerCamelCase ( ) -> Union[str, Any]: """simple docstring""" A__ = argparse.ArgumentParser() parser.add_argument("""--model_name""" , type=__a , default="""openai-gpt""" , help="""pretrained model name""" ) parser.add_argument("""--do_train""" , action="""store_true""" , help="""Whether to run training.""" ) parser.add_argument("""--do_eval""" , action="""store_true""" , help="""Whether to run eval on the dev set.""" ) parser.add_argument( """--output_dir""" , default=__a , type=__a , required=__a , help="""The output directory where the model predictions and checkpoints will be written.""" , ) parser.add_argument("""--train_dataset""" , type=__a , default="""""" ) parser.add_argument("""--eval_dataset""" , type=__a , default="""""" ) parser.add_argument("""--seed""" , type=__a , default=4_2 ) parser.add_argument("""--num_train_epochs""" , type=__a , default=3 ) parser.add_argument("""--train_batch_size""" , type=__a , default=8 ) parser.add_argument("""--eval_batch_size""" , type=__a , default=1_6 ) parser.add_argument("""--adam_epsilon""" , default=1E-8 , type=__a , help="""Epsilon for Adam optimizer.""" ) parser.add_argument("""--max_grad_norm""" , type=__a , default=1 ) parser.add_argument( """--max_steps""" , default=-1 , type=__a , help=( """If > 0: set total number of training steps to perform. Override num_train_epochs.""" ) , ) parser.add_argument( """--gradient_accumulation_steps""" , type=__a , default=1 , help="""Number of updates steps to accumulate before performing a backward/update pass.""" , ) parser.add_argument("""--learning_rate""" , type=__a , default=6.25E-5 ) parser.add_argument("""--warmup_steps""" , default=0 , type=__a , help="""Linear warmup over warmup_steps.""" ) parser.add_argument("""--lr_schedule""" , type=__a , default="""warmup_linear""" ) parser.add_argument("""--weight_decay""" , type=__a , default=0.01 ) parser.add_argument("""--lm_coef""" , type=__a , default=0.9 ) parser.add_argument("""--n_valid""" , type=__a , default=3_7_4 ) parser.add_argument("""--server_ip""" , type=__a , default="""""" , help="""Can be used for distant debugging.""" ) parser.add_argument("""--server_port""" , type=__a , default="""""" , help="""Can be used for distant debugging.""" ) A__ = parser.parse_args() print(__a ) if args.server_ip and args.server_port: # Distant debugging - see https://code.visualstudio.com/docs/python/debugging#_attach-to-a-local-script import ptvsd print("""Waiting for debugger attach""" ) ptvsd.enable_attach(address=(args.server_ip, args.server_port) , redirect_output=__a ) ptvsd.wait_for_attach() random.seed(args.seed ) np.random.seed(args.seed ) torch.manual_seed(args.seed ) torch.cuda.manual_seed_all(args.seed ) A__ = torch.device("""cuda""" if torch.cuda.is_available() else """cpu""" ) A__ = torch.cuda.device_count() logger.info("""device: {}, n_gpu {}""".format(__a , __a ) ) if not args.do_train and not args.do_eval: raise ValueError("""At least one of `do_train` or `do_eval` must be True.""" ) if not os.path.exists(args.output_dir ): os.makedirs(args.output_dir ) # Load tokenizer and model # This loading functions also add new tokens and embeddings called `special tokens` # These new embeddings will be fine-tuned on the RocStories dataset A__ = ["""_start_""", """_delimiter_""", """_classify_"""] A__ = OpenAIGPTTokenizer.from_pretrained(args.model_name ) tokenizer.add_tokens(__a ) A__ = tokenizer.convert_tokens_to_ids(__a ) A__ = OpenAIGPTDoubleHeadsModel.from_pretrained(args.model_name ) model.resize_token_embeddings(len(__a ) ) model.to(__a ) # Load and encode the datasets def tokenize_and_encode(__a :Tuple ): if isinstance(__a , __a ): return tokenizer.convert_tokens_to_ids(tokenizer.tokenize(__a ) ) elif isinstance(__a , __a ): return obj return [tokenize_and_encode(__a ) for o in obj] logger.info("""Encoding dataset...""" ) A__ = load_rocstories_dataset(args.train_dataset ) A__ = load_rocstories_dataset(args.eval_dataset ) A__ = (train_dataset, eval_dataset) A__ = tokenize_and_encode(__a ) # Compute the max input length for the Transformer A__ = model.config.n_positions // 2 - 2 A__ = max( len(story[:max_length] ) + max(len(conta[:max_length] ) , len(conta[:max_length] ) ) + 3 for dataset in encoded_datasets for story, conta, conta, _ in dataset ) A__ = min(__a , model.config.n_positions ) # Max size of input for the pre-trained model # Prepare inputs tensors and dataloaders A__ = pre_process_datasets(__a , __a , __a , *__a ) A__ , A__ = tensor_datasets[0], tensor_datasets[1] A__ = TensorDataset(*__a ) A__ = RandomSampler(__a ) A__ = DataLoader(__a , sampler=__a , batch_size=args.train_batch_size ) A__ = TensorDataset(*__a ) A__ = SequentialSampler(__a ) A__ = DataLoader(__a , sampler=__a , batch_size=args.eval_batch_size ) # Prepare optimizer if args.do_train: if args.max_steps > 0: A__ = args.max_steps A__ = args.max_steps // (len(__a ) // args.gradient_accumulation_steps) + 1 else: A__ = len(__a ) // args.gradient_accumulation_steps * args.num_train_epochs A__ = list(model.named_parameters() ) A__ = ["""bias""", """LayerNorm.bias""", """LayerNorm.weight"""] A__ = [ { """params""": [p for n, p in param_optimizer if not any(nd in n for nd in no_decay )], """weight_decay""": args.weight_decay, }, {"""params""": [p for n, p in param_optimizer if any(nd in n for nd in no_decay )], """weight_decay""": 0.0}, ] A__ = AdamW(__a , lr=args.learning_rate , eps=args.adam_epsilon ) A__ = get_linear_schedule_with_warmup( __a , num_warmup_steps=args.warmup_steps , num_training_steps=__a ) if args.do_train: A__ , A__ , A__ = 0, 0, None model.train() for _ in trange(int(args.num_train_epochs ) , desc="""Epoch""" ): A__ = 0 A__ = 0 A__ = tqdm(__a , desc="""Training""" ) for step, batch in enumerate(__a ): A__ = tuple(t.to(__a ) for t in batch ) A__ , A__ , A__ , A__ = batch A__ = model(__a , mc_token_ids=__a , lm_labels=__a , mc_labels=__a ) A__ = args.lm_coef * losses[0] + losses[1] loss.backward() optimizer.step() scheduler.step() optimizer.zero_grad() tr_loss += loss.item() A__ = ( loss.item() if exp_average_loss is None else 0.7 * exp_average_loss + 0.3 * loss.item() ) nb_tr_steps += 1 A__ = """Training loss: {:.2e} lr: {:.2e}""".format(__a , scheduler.get_lr()[0] ) # Save a trained model if args.do_train: # Save a trained model, configuration and tokenizer A__ = model.module if hasattr(__a , """module""" ) else model # Only save the model itself # If we save using the predefined names, we can load using `from_pretrained` A__ = os.path.join(args.output_dir , __a ) A__ = os.path.join(args.output_dir , __a ) torch.save(model_to_save.state_dict() , __a ) model_to_save.config.to_json_file(__a ) tokenizer.save_vocabulary(args.output_dir ) # Load a trained model and vocabulary that you have fine-tuned A__ = OpenAIGPTDoubleHeadsModel.from_pretrained(args.output_dir ) A__ = OpenAIGPTTokenizer.from_pretrained(args.output_dir ) model.to(__a ) if args.do_eval: model.eval() A__ , A__ = 0, 0 A__ , A__ = 0, 0 for batch in tqdm(__a , desc="""Evaluating""" ): A__ = tuple(t.to(__a ) for t in batch ) A__ , A__ , A__ , A__ = batch with torch.no_grad(): A__ , A__ , A__ , A__ = model( __a , mc_token_ids=__a , lm_labels=__a , mc_labels=__a ) A__ = mc_logits.detach().cpu().numpy() A__ = mc_labels.to("""cpu""" ).numpy() A__ = accuracy(__a , __a ) eval_loss += mc_loss.mean().item() eval_accuracy += tmp_eval_accuracy nb_eval_examples += input_ids.size(0 ) nb_eval_steps += 1 A__ = eval_loss / nb_eval_steps A__ = eval_accuracy / nb_eval_examples A__ = tr_loss / nb_tr_steps if args.do_train else None A__ = {"""eval_loss""": eval_loss, """eval_accuracy""": eval_accuracy, """train_loss""": train_loss} A__ = os.path.join(args.output_dir , """eval_results.txt""" ) with open(__a , """w""" ) as writer: logger.info("""***** Eval results *****""" ) for key in sorted(result.keys() ): logger.info(""" %s = %s""" , __a , str(result[key] ) ) writer.write("""%s = %s\n""" % (key, str(result[key] )) ) if __name__ == "__main__": main()
274
0
import pytest from datasets.parallel import ParallelBackendConfig, parallel_backend from datasets.utils.py_utils import map_nested from .utils import require_dill_gt_0_3_2, require_joblibspark, require_not_windows def lowerCAmelCase__(__snake_case ) -> str: # picklable for multiprocessing '''simple docstring''' return i + 1 @require_dill_gt_0_3_2 @require_joblibspark @require_not_windows def lowerCAmelCase__() -> Dict: '''simple docstring''' with parallel_backend('''spark''' ): assert ParallelBackendConfig.backend_name == "spark" lowerCamelCase__ = [1, 2, 3] with pytest.raises(__a ): with parallel_backend('''unsupported backend''' ): map_nested(__a ,__a ,num_proc=2 ) with pytest.raises(__a ): with parallel_backend('''unsupported backend''' ): map_nested(__a ,__a ,num_proc=-1 ) @require_dill_gt_0_3_2 @require_joblibspark @require_not_windows @pytest.mark.parametrize('''num_proc''' ,[2, -1] ) def lowerCAmelCase__(__snake_case ) -> Tuple: '''simple docstring''' lowerCamelCase__ = [1, 2] lowerCamelCase__ = {'''a''': 1, '''b''': 2} lowerCamelCase__ = {'''a''': [1, 2], '''b''': [3, 4]} lowerCamelCase__ = {'''a''': {'''1''': 1}, '''b''': 2} lowerCamelCase__ = {'''a''': 1, '''b''': 2, '''c''': 3, '''d''': 4} lowerCamelCase__ = [2, 3] lowerCamelCase__ = {'''a''': 2, '''b''': 3} lowerCamelCase__ = {'''a''': [2, 3], '''b''': [4, 5]} lowerCamelCase__ = {'''a''': {'''1''': 2}, '''b''': 3} lowerCamelCase__ = {'''a''': 2, '''b''': 3, '''c''': 4, '''d''': 5} with parallel_backend('''spark''' ): assert map_nested(__a ,__a ,num_proc=__a ) == expected_map_nested_sa assert map_nested(__a ,__a ,num_proc=__a ) == expected_map_nested_sa assert map_nested(__a ,__a ,num_proc=__a ) == expected_map_nested_sa assert map_nested(__a ,__a ,num_proc=__a ) == expected_map_nested_sa assert map_nested(__a ,__a ,num_proc=__a ) == expected_map_nested_sa
209
import argparse from collections import defaultdict import yaml A : str = '''docs/source/en/_toctree.yml''' def __lowerCamelCase ( __a :str ) -> List[Any]: """simple docstring""" A__ = defaultdict(__a ) A__ = [] A__ = [] for doc in doc_list: if "local" in doc: counts[doc["local"]] += 1 if doc["title"].lower() == "overview": overview_doc.append({"""local""": doc["""local"""], """title""": doc["""title"""]} ) else: new_doc_list.append(__a ) A__ = new_doc_list A__ = [key for key, value in counts.items() if value > 1] A__ = [] for duplicate_key in duplicates: A__ = list({doc["""title"""] for doc in doc_list if doc["""local"""] == duplicate_key} ) if len(__a ) > 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 doc_list if """local""" not in counts or counts[doc["""local"""]] == 1] ) A__ = sorted(__a , key=lambda __a : s["title"].lower() ) # "overview" gets special treatment and is always first if len(__a ) > 1: raise ValueError("""{doc_list} has two 'overview' docs which is not allowed.""" ) overview_doc.extend(__a ) # Sort return overview_doc def __lowerCamelCase ( __a :Any=False ) -> List[str]: """simple docstring""" with open(__a , encoding="""utf-8""" ) as f: A__ = yaml.safe_load(f.read() ) # Get to the API doc A__ = 0 while content[api_idx]["title"] != "API": api_idx += 1 A__ = content[api_idx]["""sections"""] # Then to the model doc A__ = 0 while api_doc[scheduler_idx]["title"] != "Schedulers": scheduler_idx += 1 A__ = api_doc[scheduler_idx]["""sections"""] A__ = clean_doc_toc(__a ) A__ = False if new_scheduler_doc != scheduler_doc: A__ = True if overwrite: A__ = new_scheduler_doc if diff: if overwrite: A__ = api_doc with open(__a , """w""" , encoding="""utf-8""" ) as f: f.write(yaml.dump(__a , allow_unicode=__a ) ) else: raise ValueError( """The model doc part of the table of content is not properly sorted, run `make style` to fix this.""" ) def __lowerCamelCase ( __a :Optional[int]=False ) -> Dict: """simple docstring""" with open(__a , encoding="""utf-8""" ) as f: A__ = yaml.safe_load(f.read() ) # Get to the API doc A__ = 0 while content[api_idx]["title"] != "API": api_idx += 1 A__ = content[api_idx]["""sections"""] # Then to the model doc A__ = 0 while api_doc[pipeline_idx]["title"] != "Pipelines": pipeline_idx += 1 A__ = False A__ = api_doc[pipeline_idx]["""sections"""] A__ = [] # sort sub pipeline docs for pipeline_doc in pipeline_docs: if "section" in pipeline_doc: A__ = pipeline_doc["""section"""] A__ = clean_doc_toc(__a ) if overwrite: A__ = new_sub_pipeline_doc new_pipeline_docs.append(__a ) # sort overall pipeline doc A__ = clean_doc_toc(__a ) if new_pipeline_docs != pipeline_docs: A__ = True if overwrite: A__ = new_pipeline_docs if diff: if overwrite: A__ = api_doc with open(__a , """w""" , encoding="""utf-8""" ) as f: f.write(yaml.dump(__a , allow_unicode=__a ) ) 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__": A : Tuple = argparse.ArgumentParser() parser.add_argument('''--fix_and_overwrite''', action='''store_true''', help='''Whether to fix inconsistencies.''') A : Optional[Any] = parser.parse_args() check_scheduler_doc(args.fix_and_overwrite) check_pipeline_doc(args.fix_and_overwrite)
274
0
import copy import re class _lowercase : """simple docstring""" A__ = '''hp''' A__ = {} A__ = None @classmethod def lowerCAmelCase ( cls : List[str] , __lowerCamelCase : Optional[Any] , __lowerCamelCase : Any ): '''simple docstring''' lowerCamelCase__ : Dict = prefix lowerCamelCase__ : Tuple = defaults cls.build_naming_info() @staticmethod def lowerCAmelCase ( __lowerCamelCase : List[Any] , __lowerCamelCase : int ): '''simple docstring''' if len(__lowerCAmelCase ) == 0: return "" lowerCamelCase__ : Any = 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(__lowerCAmelCase ) + 1 ): lowerCamelCase__ : str = word[:prefix_len] if prefix in info["reverse_short_word"]: continue else: lowerCamelCase__ : Dict = prefix break if short_word is None: # Paranoid fallback def int_to_alphabetic(__lowerCamelCase : List[str] ): lowerCamelCase__ : Union[str, Any] = "" while integer != 0: lowerCamelCase__ : Tuple = chr(ord("A" ) + integer % 10 ) + s integer //= 10 return s lowerCamelCase__ : List[Any] = 0 while True: lowerCamelCase__ : List[str] = word + "#" + int_to_alphabetic(__lowerCAmelCase ) if sword in info["reverse_short_word"]: continue else: lowerCamelCase__ : List[str] = sword break lowerCamelCase__ : Optional[Any] = short_word lowerCamelCase__ : Union[str, Any] = word return short_word @staticmethod def lowerCAmelCase ( __lowerCamelCase : int , __lowerCamelCase : Optional[Any] ): '''simple docstring''' lowerCamelCase__ : Optional[Any] = param_name.split("_" ) lowerCamelCase__ : List[Any] = [TrialShortNamer.shortname_for_word(__lowerCAmelCase , __lowerCAmelCase ) 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 lowerCamelCase__ : int = ["", "_"] for separator in separators: lowerCamelCase__ : Any = separator.join(__lowerCAmelCase ) if shortname not in info["reverse_short_param"]: lowerCamelCase__ : Tuple = shortname lowerCamelCase__ : Optional[Any] = param_name return shortname return param_name @staticmethod def lowerCAmelCase ( __lowerCamelCase : Any , __lowerCamelCase : List[Any] ): '''simple docstring''' lowerCamelCase__ : Any = TrialShortNamer.shortname_for_key(__lowerCAmelCase , __lowerCAmelCase ) lowerCamelCase__ : Tuple = short_name lowerCamelCase__ : Union[str, Any] = param_name @classmethod def lowerCAmelCase ( cls : Dict ): '''simple docstring''' if cls.NAMING_INFO is not None: return lowerCamelCase__ : List[str] = { "short_word": {}, "reverse_short_word": {}, "short_param": {}, "reverse_short_param": {}, } lowerCamelCase__ : Optional[Any] = list(cls.DEFAULTS.keys() ) for k in field_keys: cls.add_new_param_name(__lowerCAmelCase , __lowerCAmelCase ) lowerCamelCase__ : List[str] = info @classmethod def lowerCAmelCase ( cls : str , __lowerCamelCase : Optional[int] ): '''simple docstring''' cls.build_naming_info() assert cls.PREFIX is not None lowerCamelCase__ : Union[str, Any] = [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 lowerCamelCase__ : int = cls.NAMING_INFO["short_param"][k] if isinstance(__lowerCAmelCase , __lowerCAmelCase ): lowerCamelCase__ : List[Any] = 1 if v else 0 lowerCamelCase__ : Optional[int] = "" if isinstance(__lowerCAmelCase , (int, float) ) else "-" lowerCamelCase__ : Any = f"{key}{sep}{v}" name.append(__lowerCAmelCase ) return "_".join(__lowerCAmelCase ) @classmethod def lowerCAmelCase ( cls : List[str] , __lowerCamelCase : Optional[Any] ): '''simple docstring''' lowerCamelCase__ : Union[str, Any] = repr[len(cls.PREFIX ) + 1 :] if repr == "": lowerCamelCase__ : Optional[int] = [] else: lowerCamelCase__ : Optional[int] = repr.split("_" ) lowerCamelCase__ : Any = {} for value in values: if "-" in value: lowerCamelCase__ , lowerCamelCase__ : List[str] = value.split("-" ) else: lowerCamelCase__ : Optional[Any] = re.sub("[0-9.]" , "" , __lowerCAmelCase ) lowerCamelCase__ : int = float(re.sub("[^0-9.]" , "" , __lowerCAmelCase ) ) lowerCamelCase__ : int = cls.NAMING_INFO["reverse_short_param"][p_k] lowerCamelCase__ : Optional[int] = p_v for k in cls.DEFAULTS: if k not in parameters: lowerCamelCase__ : List[str] = cls.DEFAULTS[k] return parameters
184
def __lowerCamelCase ( __a :str ) -> list: """simple docstring""" A__ = [0] * len(__a ) for i in range(1 , len(__a ) ): # use last results for better performance - dynamic programming A__ = prefix_result[i - 1] while j > 0 and input_string[i] != input_string[j]: A__ = prefix_result[j - 1] if input_string[i] == input_string[j]: j += 1 A__ = j return prefix_result def __lowerCamelCase ( __a :str ) -> int: """simple docstring""" return max(prefix_function(__a ) ) if __name__ == "__main__": import doctest doctest.testmod()
274
0
from __future__ import annotations import unittest from transformers import DebertaVaConfig, is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TFDebertaVaForMaskedLM, TFDebertaVaForQuestionAnswering, TFDebertaVaForSequenceClassification, TFDebertaVaForTokenClassification, TFDebertaVaModel, ) class UpperCamelCase_ : '''simple docstring''' def __init__( self , a , a=13 , a=7 , a=True , a=True , a=True , a=True , a=99 , a=32 , a=2 , a=4 , a=37 , a="gelu" , a=0.1 , a=0.1 , a=5_12 , a=16 , a=2 , a=0.02 , a=False , a=True , a="None" , a=3 , a=4 , a=None , ) -> Optional[int]: snake_case_ = parent snake_case_ = batch_size snake_case_ = seq_length snake_case_ = is_training snake_case_ = use_input_mask snake_case_ = use_token_type_ids snake_case_ = use_labels snake_case_ = vocab_size snake_case_ = hidden_size snake_case_ = num_hidden_layers snake_case_ = num_attention_heads snake_case_ = intermediate_size snake_case_ = hidden_act snake_case_ = hidden_dropout_prob snake_case_ = attention_probs_dropout_prob snake_case_ = max_position_embeddings snake_case_ = type_vocab_size snake_case_ = type_sequence_label_size snake_case_ = initializer_range snake_case_ = num_labels snake_case_ = num_choices snake_case_ = relative_attention snake_case_ = position_biased_input snake_case_ = pos_att_type snake_case_ = scope def _UpperCamelCase ( self ) -> int: snake_case_ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) snake_case_ = None if self.use_input_mask: snake_case_ = random_attention_mask([self.batch_size, self.seq_length] ) snake_case_ = None if self.use_token_type_ids: snake_case_ = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) snake_case_ = None snake_case_ = None snake_case_ = None if self.use_labels: snake_case_ = ids_tensor([self.batch_size] , self.type_sequence_label_size ) snake_case_ = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) snake_case_ = DebertaVaConfig( 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 , relative_attention=self.relative_attention , position_biased_input=self.position_biased_input , initializer_range=self.initializer_range , return_dict=__lowerCAmelCase , ) return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def _UpperCamelCase ( self , a , a , a , a , a , a , a ) -> Tuple: snake_case_ = TFDebertaVaModel(config=__lowerCAmelCase ) snake_case_ = {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids} snake_case_ = [input_ids, input_mask] snake_case_ = model(__lowerCAmelCase ) snake_case_ = model(__lowerCAmelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _UpperCamelCase ( self , a , a , a , a , a , a , a ) -> Tuple: snake_case_ = TFDebertaVaForMaskedLM(config=__lowerCAmelCase ) snake_case_ = { 'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids, } snake_case_ = model(__lowerCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def _UpperCamelCase ( self , a , a , a , a , a , a , a ) -> Dict: snake_case_ = self.num_labels snake_case_ = TFDebertaVaForSequenceClassification(config=__lowerCAmelCase ) snake_case_ = { 'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids, } snake_case_ = model(__lowerCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def _UpperCamelCase ( self , a , a , a , a , a , a , a ) -> Union[str, Any]: snake_case_ = self.num_labels snake_case_ = TFDebertaVaForTokenClassification(config=__lowerCAmelCase ) snake_case_ = { 'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids, } snake_case_ = model(__lowerCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def _UpperCamelCase ( self , a , a , a , a , a , a , a ) -> int: snake_case_ = TFDebertaVaForQuestionAnswering(config=__lowerCAmelCase ) snake_case_ = { 'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids, } snake_case_ = model(__lowerCAmelCase ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def _UpperCamelCase ( self ) -> Union[str, Any]: snake_case_ = self.prepare_config_and_inputs() ( ( snake_case_ ) , ( snake_case_ ) , ( snake_case_ ) , ( snake_case_ ) , ( snake_case_ ) , ( snake_case_ ) , ( snake_case_ ) , ) = config_and_inputs snake_case_ = {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask} return config, inputs_dict @require_tf class UpperCamelCase_ ( snake_case_ , snake_case_ , unittest.TestCase ): '''simple docstring''' lowerCAmelCase = ( ( TFDebertaVaModel, TFDebertaVaForMaskedLM, TFDebertaVaForQuestionAnswering, TFDebertaVaForSequenceClassification, TFDebertaVaForTokenClassification, ) if is_tf_available() else () ) lowerCAmelCase = ( { '''feature-extraction''': TFDebertaVaModel, '''fill-mask''': TFDebertaVaForMaskedLM, '''question-answering''': TFDebertaVaForQuestionAnswering, '''text-classification''': TFDebertaVaForSequenceClassification, '''token-classification''': TFDebertaVaForTokenClassification, '''zero-shot''': TFDebertaVaForSequenceClassification, } if is_tf_available() else {} ) lowerCAmelCase = False lowerCAmelCase = False def _UpperCamelCase ( self ) -> int: snake_case_ = TFDebertaVaModelTester(self ) snake_case_ = ConfigTester(self , config_class=__lowerCAmelCase , hidden_size=37 ) def _UpperCamelCase ( self ) -> Dict: self.config_tester.run_common_tests() def _UpperCamelCase ( self ) -> Dict: snake_case_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__lowerCAmelCase ) def _UpperCamelCase ( self ) -> int: snake_case_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*__lowerCAmelCase ) def _UpperCamelCase ( self ) -> Optional[Any]: snake_case_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*__lowerCAmelCase ) def _UpperCamelCase ( self ) -> List[Any]: snake_case_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*__lowerCAmelCase ) def _UpperCamelCase ( self ) -> List[str]: snake_case_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*__lowerCAmelCase ) @slow def _UpperCamelCase ( self ) -> str: snake_case_ = TFDebertaVaModel.from_pretrained('kamalkraj/deberta-v2-xlarge' ) self.assertIsNotNone(__lowerCAmelCase ) @require_tf class UpperCamelCase_ ( unittest.TestCase ): '''simple docstring''' @unittest.skip(reason='Model not available yet' ) def _UpperCamelCase ( self ) -> Optional[int]: pass @slow def _UpperCamelCase ( self ) -> Optional[int]: snake_case_ = TFDebertaVaModel.from_pretrained('kamalkraj/deberta-v2-xlarge' ) snake_case_ = tf.constant([[0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69, 4_60_78, 15_88, 2]] ) snake_case_ = tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) snake_case_ = model(__lowerCAmelCase , attention_mask=__lowerCAmelCase )[0] snake_case_ = tf.constant( [[[0.2_356, 0.1_948, 0.0_369], [-0.1_063, 0.3_586, -0.5_152], [-0.6_399, -0.0_259, -0.2_525]]] ) tf.debugging.assert_near(output[:, 1:4, 1:4] , __lowerCAmelCase , atol=1E-4 )
178
def __lowerCamelCase ( __a :int = 1_0_0_0_0_0_0 ) -> int: """simple docstring""" A__ = limit + 1 A__ = [0] * limit for first_term in range(1 , __a ): for n in range(__a , __a , __a ): A__ = first_term + n / first_term if common_difference % 4: # d must be divisble by 4 continue else: common_difference /= 4 if ( first_term > common_difference and first_term < 4 * common_difference ): # since x,y,z are positive integers frequency[n] += 1 # so z>0 and a>d ,also 4d<a A__ = sum(1 for x in frequency[1:limit] if x == 1_0 ) return count if __name__ == "__main__": print(F'''{solution() = }''')
274
0
from __future__ import annotations from collections.abc import Iterable, Iterator from dataclasses import dataclass _A = (3, 9, -11, 0, 7, 5, 1, -1) _A = (4, 6, 2, 0, 8, 10, 3, -2) @dataclass class UpperCAmelCase__ : """simple docstring""" UpperCAmelCase__ : int UpperCAmelCase__ : Node | None class UpperCAmelCase__ : """simple docstring""" def __init__( self , A_ ) -> None: __UpperCamelCase =None for i in sorted(__lowerCAmelCase , reverse=__lowerCAmelCase ): __UpperCamelCase =Node(__lowerCAmelCase , self.head ) def __iter__( self ) -> Iterator[int]: __UpperCamelCase =self.head while node: yield node.data __UpperCamelCase =node.next_node def __len__( self ) -> int: return sum(1 for _ in self ) def __str__( self ) -> str: return " -> ".join([str(__lowerCAmelCase ) for node in self] ) def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : SortedLinkedList , SCREAMING_SNAKE_CASE__ : SortedLinkedList ): return SortedLinkedList(list(__a ) + list(__a ) ) if __name__ == "__main__": import doctest doctest.testmod() _A = SortedLinkedList print(merge_lists(SSL(test_data_odd), SSL(test_data_even)))
62
class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' pass class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' pass class A : '''simple docstring''' def __init__( self : List[Any] ) -> str: """simple docstring""" A__ = [ [], [], [], ] def a_ ( self : Dict , __lowerCAmelCase : int , __lowerCAmelCase : int ) -> None: """simple docstring""" try: if len(self.queues[priority] ) >= 1_00: raise OverflowError("""Maximum queue size is 100""" ) self.queues[priority].append(__lowerCAmelCase ) except IndexError: raise ValueError("""Valid priorities are 0, 1, and 2""" ) def a_ ( self : Optional[Any] ) -> int: """simple docstring""" for queue in self.queues: if queue: return queue.pop(0 ) raise UnderFlowError("""All queues are empty""" ) def __str__( self : Tuple ) -> str: """simple docstring""" return "\n".join(f'Priority {i}: {q}' for i, q in enumerate(self.queues ) ) class A : '''simple docstring''' def __init__( self : int ) -> str: """simple docstring""" A__ = [] def a_ ( self : int , __lowerCAmelCase : int ) -> None: """simple docstring""" if len(self.queue ) == 1_00: raise OverFlowError("""Maximum queue size is 100""" ) self.queue.append(__lowerCAmelCase ) def a_ ( self : List[str] ) -> int: """simple docstring""" if not self.queue: raise UnderFlowError("""The queue is empty""" ) else: A__ = min(self.queue ) self.queue.remove(__lowerCAmelCase ) return data def __str__( self : List[Any] ) -> str: """simple docstring""" return str(self.queue ) def __lowerCamelCase ( ) -> Optional[Any]: """simple docstring""" A__ = FixedPriorityQueue() fpq.enqueue(0 , 1_0 ) fpq.enqueue(1 , 7_0 ) fpq.enqueue(0 , 1_0_0 ) fpq.enqueue(2 , 1 ) fpq.enqueue(2 , 5 ) fpq.enqueue(1 , 7 ) fpq.enqueue(2 , 4 ) fpq.enqueue(1 , 6_4 ) fpq.enqueue(0 , 1_2_8 ) print(__a ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(__a ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) def __lowerCamelCase ( ) -> int: """simple docstring""" A__ = ElementPriorityQueue() epq.enqueue(1_0 ) epq.enqueue(7_0 ) epq.enqueue(1_0_0 ) epq.enqueue(1 ) epq.enqueue(5 ) epq.enqueue(7 ) epq.enqueue(4 ) epq.enqueue(6_4 ) epq.enqueue(1_2_8 ) print(__a ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(__a ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) if __name__ == "__main__": fixed_priority_queue() element_priority_queue()
274
0
'''simple docstring''' import argparse import json import logging import os import sys from unittest.mock import patch from transformers.testing_utils import TestCasePlus, get_gpu_count, slow _lowerCAmelCase = [ os.path.join(os.path.dirname(__file__), dirname) for dirname in [ '''text-classification''', '''language-modeling''', '''summarization''', '''token-classification''', '''question-answering''', ] ] sys.path.extend(SRC_DIRS) if SRC_DIRS is not None: import run_clm_flax import run_flax_glue import run_flax_ner import run_mlm_flax import run_qa import run_summarization_flax import run_ta_mlm_flax logging.basicConfig(level=logging.DEBUG) _lowerCAmelCase = logging.getLogger() def __lowerCAmelCase ( ): __UpperCamelCase : List[str] = argparse.ArgumentParser() parser.add_argument("-f" ) __UpperCamelCase : int = parser.parse_args() return args.f def __lowerCAmelCase ( snake_case__ , snake_case__="eval" ): __UpperCamelCase : Dict = os.path.join(__a , F"{split}_results.json" ) if os.path.exists(__a ): with open(__a , "r" ) as f: return json.load(__a ) raise ValueError(F"can\'t find {path}" ) _lowerCAmelCase = logging.StreamHandler(sys.stdout) logger.addHandler(stream_handler) class A ( SCREAMING_SNAKE_CASE__ ): '''simple docstring''' def a_ (self ) -> Union[str, Any]: __UpperCamelCase : Tuple = self.get_auto_remove_tmp_dir() __UpperCamelCase : Optional[Any] = f"\n run_glue.py\n --model_name_or_path distilbert-base-uncased\n --output_dir {tmp_dir}\n --train_file ./tests/fixtures/tests_samples/MRPC/train.csv\n --validation_file ./tests/fixtures/tests_samples/MRPC/dev.csv\n --per_device_train_batch_size=2\n --per_device_eval_batch_size=1\n --learning_rate=1e-4\n --eval_steps=2\n --warmup_steps=2\n --seed=42\n --max_seq_length=128\n ".split() with patch.object(__lowerCAmelCase , "argv" , __lowerCAmelCase ): run_flax_glue.main() __UpperCamelCase : List[str] = get_results(__lowerCAmelCase ) self.assertGreaterEqual(result["eval_accuracy"] , 0.75 ) @slow def a_ (self ) -> Union[str, Any]: __UpperCamelCase : Optional[int] = self.get_auto_remove_tmp_dir() __UpperCamelCase : int = f"\n run_clm_flax.py\n --model_name_or_path distilgpt2\n --train_file ./tests/fixtures/sample_text.txt\n --validation_file ./tests/fixtures/sample_text.txt\n --do_train\n --do_eval\n --block_size 128\n --per_device_train_batch_size 4\n --per_device_eval_batch_size 4\n --num_train_epochs 2\n --logging_steps 2 --eval_steps 2\n --output_dir {tmp_dir}\n --overwrite_output_dir\n ".split() with patch.object(__lowerCAmelCase , "argv" , __lowerCAmelCase ): run_clm_flax.main() __UpperCamelCase : Tuple = get_results(__lowerCAmelCase ) self.assertLess(result["eval_perplexity"] , 1_0_0 ) @slow def a_ (self ) -> Dict: __UpperCamelCase : Tuple = self.get_auto_remove_tmp_dir() __UpperCamelCase : Optional[int] = f"\n run_summarization.py\n --model_name_or_path t5-small\n --train_file tests/fixtures/tests_samples/xsum/sample.json\n --validation_file tests/fixtures/tests_samples/xsum/sample.json\n --test_file tests/fixtures/tests_samples/xsum/sample.json\n --output_dir {tmp_dir}\n --overwrite_output_dir\n --num_train_epochs=3\n --warmup_steps=8\n --do_train\n --do_eval\n --do_predict\n --learning_rate=2e-4\n --per_device_train_batch_size=2\n --per_device_eval_batch_size=1\n --predict_with_generate\n ".split() with patch.object(__lowerCAmelCase , "argv" , __lowerCAmelCase ): run_summarization_flax.main() __UpperCamelCase : str = get_results(__lowerCAmelCase , split="test" ) self.assertGreaterEqual(result["test_rouge1"] , 1_0 ) self.assertGreaterEqual(result["test_rouge2"] , 2 ) self.assertGreaterEqual(result["test_rougeL"] , 7 ) self.assertGreaterEqual(result["test_rougeLsum"] , 7 ) @slow def a_ (self ) -> Union[str, Any]: __UpperCamelCase : Optional[int] = self.get_auto_remove_tmp_dir() __UpperCamelCase : Any = f"\n run_mlm.py\n --model_name_or_path distilroberta-base\n --train_file ./tests/fixtures/sample_text.txt\n --validation_file ./tests/fixtures/sample_text.txt\n --output_dir {tmp_dir}\n --overwrite_output_dir\n --max_seq_length 128\n --per_device_train_batch_size 4\n --per_device_eval_batch_size 4\n --logging_steps 2 --eval_steps 2\n --do_train\n --do_eval\n --num_train_epochs=1\n ".split() with patch.object(__lowerCAmelCase , "argv" , __lowerCAmelCase ): run_mlm_flax.main() __UpperCamelCase : int = get_results(__lowerCAmelCase ) self.assertLess(result["eval_perplexity"] , 4_2 ) @slow def a_ (self ) -> Dict: __UpperCamelCase : Optional[Any] = self.get_auto_remove_tmp_dir() __UpperCamelCase : List[Any] = f"\n run_t5_mlm_flax.py\n --model_name_or_path t5-small\n --train_file ./tests/fixtures/sample_text.txt\n --validation_file ./tests/fixtures/sample_text.txt\n --do_train\n --do_eval\n --max_seq_length 128\n --per_device_train_batch_size 4\n --per_device_eval_batch_size 4\n --num_train_epochs 2\n --logging_steps 2 --eval_steps 2\n --output_dir {tmp_dir}\n --overwrite_output_dir\n ".split() with patch.object(__lowerCAmelCase , "argv" , __lowerCAmelCase ): run_ta_mlm_flax.main() __UpperCamelCase : List[Any] = get_results(__lowerCAmelCase ) self.assertGreaterEqual(result["eval_accuracy"] , 0.42 ) @slow def a_ (self ) -> Optional[int]: __UpperCamelCase : int = 7 if get_gpu_count() > 1 else 2 __UpperCamelCase : str = self.get_auto_remove_tmp_dir() __UpperCamelCase : Any = f"\n run_flax_ner.py\n --model_name_or_path bert-base-uncased\n --train_file tests/fixtures/tests_samples/conll/sample.json\n --validation_file tests/fixtures/tests_samples/conll/sample.json\n --output_dir {tmp_dir}\n --overwrite_output_dir\n --do_train\n --do_eval\n --warmup_steps=2\n --learning_rate=2e-4\n --logging_steps 2 --eval_steps 2\n --per_device_train_batch_size=2\n --per_device_eval_batch_size=2\n --num_train_epochs={epochs}\n --seed 7\n ".split() with patch.object(__lowerCAmelCase , "argv" , __lowerCAmelCase ): run_flax_ner.main() __UpperCamelCase : Union[str, Any] = get_results(__lowerCAmelCase ) self.assertGreaterEqual(result["eval_accuracy"] , 0.75 ) self.assertGreaterEqual(result["eval_f1"] , 0.3 ) @slow def a_ (self ) -> Tuple: __UpperCamelCase : Union[str, Any] = self.get_auto_remove_tmp_dir() __UpperCamelCase : Any = f"\n run_qa.py\n --model_name_or_path bert-base-uncased\n --version_2_with_negative\n --train_file tests/fixtures/tests_samples/SQUAD/sample.json\n --validation_file tests/fixtures/tests_samples/SQUAD/sample.json\n --output_dir {tmp_dir}\n --overwrite_output_dir\n --num_train_epochs=3\n --warmup_steps=2\n --do_train\n --do_eval\n --logging_steps 2 --eval_steps 2\n --learning_rate=2e-4\n --per_device_train_batch_size=2\n --per_device_eval_batch_size=1\n ".split() with patch.object(__lowerCAmelCase , "argv" , __lowerCAmelCase ): run_qa.main() __UpperCamelCase : str = get_results(__lowerCAmelCase ) self.assertGreaterEqual(result["eval_f1"] , 3_0 ) self.assertGreaterEqual(result["eval_exact"] , 3_0 )
298
import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import CLIPTokenizer, CLIPTokenizerFast from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES from transformers.testing_utils import require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available if is_vision_available(): from PIL import Image from transformers import CLIPImageProcessor, CLIPProcessor @require_vision class A (unittest.TestCase ): '''simple docstring''' def a_ ( self : Union[str, Any] ) -> Dict: """simple docstring""" A__ = tempfile.mkdtemp() # fmt: off A__ = ["""l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """lo""", """l</w>""", """w</w>""", """r</w>""", """t</w>""", """low</w>""", """er</w>""", """lowest</w>""", """newer</w>""", """wider""", """<unk>""", """<|startoftext|>""", """<|endoftext|>"""] # fmt: on A__ = dict(zip(__lowerCAmelCase , range(len(__lowerCAmelCase ) ) ) ) A__ = ["""#version: 0.2""", """l o""", """lo w</w>""", """e r</w>""", """"""] A__ = {"""unk_token""": """<unk>"""} A__ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""vocab_file"""] ) A__ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""merges_file"""] ) with open(self.vocab_file , """w""" , encoding="""utf-8""" ) as fp: fp.write(json.dumps(__lowerCAmelCase ) + """\n""" ) with open(self.merges_file , """w""" , encoding="""utf-8""" ) as fp: fp.write("""\n""".join(__lowerCAmelCase ) ) A__ = { """do_resize""": True, """size""": 20, """do_center_crop""": True, """crop_size""": 18, """do_normalize""": True, """image_mean""": [0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3], """image_std""": [0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1], } A__ = os.path.join(self.tmpdirname , __lowerCAmelCase ) with open(self.image_processor_file , """w""" , encoding="""utf-8""" ) as fp: json.dump(__lowerCAmelCase , __lowerCAmelCase ) def a_ ( self : Tuple , **__lowerCAmelCase : Dict ) -> str: """simple docstring""" return CLIPTokenizer.from_pretrained(self.tmpdirname , **__lowerCAmelCase ) def a_ ( self : Union[str, Any] , **__lowerCAmelCase : Dict ) -> List[str]: """simple docstring""" return CLIPTokenizerFast.from_pretrained(self.tmpdirname , **__lowerCAmelCase ) def a_ ( self : List[str] , **__lowerCAmelCase : Optional[Any] ) -> Dict: """simple docstring""" return CLIPImageProcessor.from_pretrained(self.tmpdirname , **__lowerCAmelCase ) def a_ ( self : str ) -> Dict: """simple docstring""" shutil.rmtree(self.tmpdirname ) def a_ ( self : str ) -> Any: """simple docstring""" A__ = [np.random.randint(2_55 , size=(3, 30, 4_00) , dtype=np.uinta )] A__ = [Image.fromarray(np.moveaxis(__lowerCAmelCase , 0 , -1 ) ) for x in image_inputs] return image_inputs def a_ ( self : Optional[int] ) -> Tuple: """simple docstring""" A__ = self.get_tokenizer() A__ = self.get_rust_tokenizer() A__ = self.get_image_processor() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) processor_slow.save_pretrained(self.tmpdirname ) A__ = CLIPProcessor.from_pretrained(self.tmpdirname , use_fast=__lowerCAmelCase ) A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) processor_fast.save_pretrained(self.tmpdirname ) A__ = CLIPProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor_slow.tokenizer.get_vocab() , tokenizer_slow.get_vocab() ) self.assertEqual(processor_fast.tokenizer.get_vocab() , tokenizer_fast.get_vocab() ) self.assertEqual(tokenizer_slow.get_vocab() , tokenizer_fast.get_vocab() ) self.assertIsInstance(processor_slow.tokenizer , __lowerCAmelCase ) self.assertIsInstance(processor_fast.tokenizer , __lowerCAmelCase ) self.assertEqual(processor_slow.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertEqual(processor_fast.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertIsInstance(processor_slow.image_processor , __lowerCAmelCase ) self.assertIsInstance(processor_fast.image_processor , __lowerCAmelCase ) def a_ ( self : Optional[Any] ) -> Union[str, Any]: """simple docstring""" A__ = CLIPProcessor(tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) A__ = self.get_tokenizer(bos_token="""(BOS)""" , eos_token="""(EOS)""" ) A__ = self.get_image_processor(do_normalize=__lowerCAmelCase , padding_value=1.0 ) A__ = CLIPProcessor.from_pretrained( self.tmpdirname , bos_token="""(BOS)""" , eos_token="""(EOS)""" , do_normalize=__lowerCAmelCase , padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , __lowerCAmelCase ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , __lowerCAmelCase ) def a_ ( self : List[Any] ) -> Dict: """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) A__ = self.prepare_image_inputs() A__ = image_processor(__lowerCAmelCase , return_tensors="""np""" ) A__ = processor(images=__lowerCAmelCase , return_tensors="""np""" ) for key in input_image_proc.keys(): self.assertAlmostEqual(input_image_proc[key].sum() , input_processor[key].sum() , delta=1e-2 ) def a_ ( self : Optional[Any] ) -> Any: """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) A__ = """lower newer""" A__ = processor(text=__lowerCAmelCase ) A__ = tokenizer(__lowerCAmelCase ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def a_ ( self : Union[str, Any] ) -> Dict: """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) A__ = """lower newer""" A__ = self.prepare_image_inputs() A__ = processor(text=__lowerCAmelCase , images=__lowerCAmelCase ) self.assertListEqual(list(inputs.keys() ) , ["""input_ids""", """attention_mask""", """pixel_values"""] ) # test if it raises when no input is passed with pytest.raises(__lowerCAmelCase ): processor() def a_ ( self : Tuple ) -> str: """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) A__ = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] A__ = processor.batch_decode(__lowerCAmelCase ) A__ = tokenizer.batch_decode(__lowerCAmelCase ) self.assertListEqual(__lowerCAmelCase , __lowerCAmelCase ) def a_ ( self : Optional[int] ) -> str: """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) A__ = """lower newer""" A__ = self.prepare_image_inputs() A__ = processor(text=__lowerCAmelCase , images=__lowerCAmelCase ) self.assertListEqual(list(inputs.keys() ) , processor.model_input_names )
274
0
"""simple docstring""" import warnings from ...utils import logging from .image_processing_segformer import SegformerImageProcessor _snake_case = logging.get_logger(__name__) class UpperCamelCase ( snake_case_ ): def __init__( self : Tuple , *UpperCAmelCase__ : int , **UpperCAmelCase__ : Optional[Any] ) -> None: warnings.warn( """The class SegformerFeatureExtractor is deprecated and will be removed in version 5 of Transformers.""" """ Please use SegformerImageProcessor instead.""" , __lowerCAmelCase , ) super().__init__(*__lowerCAmelCase , **__lowerCAmelCase )
294
from multiprocessing import Lock, Pipe, Process # lock used to ensure that two processes do not access a pipe at the same time A : Dict = Lock() def __lowerCamelCase ( __a :Dict , __a :List[str] , __a :Optional[int] , __a :Optional[int] , __a :Optional[Any] , __a :Optional[int] , __a :int ) -> Dict: """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 , 1_0 ): 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(__a ) process_lock.release() # receive your right neighbor's value process_lock.acquire() A__ = rr_cv[0].recv() process_lock.release() # take the lower value since you are on the left A__ = min(__a , __a ) 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(__a ) process_lock.release() # receive your left neighbor's value process_lock.acquire() A__ = lr_cv[0].recv() process_lock.release() # take the higher value since you are on the right A__ = max(__a , __a ) # after all swaps are performed, send the values back to main result_pipe[1].send(__a ) def __lowerCamelCase ( __a :List[str] ) -> int: """simple docstring""" A__ = [] A__ = [] # 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 A__ = Pipe() A__ = Pipe() process_array_.append( Process( target=__a , args=(0, arr[0], None, temp_rs, None, temp_rr, result_pipe[0]) , ) ) A__ = temp_rs A__ = temp_rr for i in range(1 , len(__a ) - 1 ): A__ = Pipe() A__ = Pipe() process_array_.append( Process( target=__a , args=(i, arr[i], temp_ls, temp_rs, temp_lr, temp_rr, result_pipe[i]) , ) ) A__ = temp_rs A__ = temp_rr process_array_.append( Process( target=__a , args=( len(__a ) - 1, arr[len(__a ) - 1], temp_ls, None, temp_lr, None, result_pipe[len(__a ) - 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(__a ) ): A__ = result_pipe[p][0].recv() process_array_[p].join() return arr def __lowerCamelCase ( ) -> str: """simple docstring""" A__ = list(range(1_0 , 0 , -1 ) ) print("""Initial List""" ) print(*__a ) A__ = odd_even_transposition(__a ) print("""Sorted List\n""" ) print(*__a ) if __name__ == "__main__": main()
274
0
import argparse import os import re import packaging.version __UpperCAmelCase = '''examples/''' __UpperCAmelCase = { '''examples''': (re.compile(R'^check_min_version\("[^"]+"\)\s*$', re.MULTILINE), '''check_min_version("VERSION")\n'''), '''init''': (re.compile(R'^__version__\s+=\s+"([^"]+)"\s*$', re.MULTILINE), '''__version__ = "VERSION"\n'''), '''setup''': (re.compile(R'^(\s*)version\s*=\s*"[^"]+",', re.MULTILINE), R'''\1version="VERSION",'''), '''doc''': (re.compile(R'^(\s*)release\s*=\s*"[^"]+"$', re.MULTILINE), '''release = "VERSION"\n'''), } __UpperCAmelCase = { '''init''': '''src/diffusers/__init__.py''', '''setup''': '''setup.py''', } __UpperCAmelCase = '''README.md''' def lowercase__ ( __snake_case : List[Any] , __snake_case : str , __snake_case : Tuple ): '''simple docstring''' with open(__a , 'r' , encoding='utf-8' , newline='\n' ) as f: UpperCAmelCase_ : List[str] = f.read() UpperCAmelCase_ , UpperCAmelCase_ : Union[str, Any] = REPLACE_PATTERNS[pattern] UpperCAmelCase_ : int = replace.replace('VERSION' , __a ) UpperCAmelCase_ : List[str] = re_pattern.sub(__a , __a ) with open(__a , 'w' , encoding='utf-8' , newline='\n' ) as f: f.write(__a ) def lowercase__ ( __snake_case : int ): '''simple docstring''' for folder, directories, fnames in os.walk(__a ): # Removing some of the folders with non-actively maintained examples from the walk if "research_projects" in directories: directories.remove('research_projects' ) if "legacy" in directories: directories.remove('legacy' ) for fname in fnames: if fname.endswith('.py' ): update_version_in_file(os.path.join(__a , __a ) , __a , pattern='examples' ) def lowercase__ ( __snake_case : int , __snake_case : Union[str, Any]=False ): '''simple docstring''' for pattern, fname in REPLACE_FILES.items(): update_version_in_file(__a , __a , __a ) if not patch: update_version_in_examples(__a ) def lowercase__ ( ): '''simple docstring''' UpperCAmelCase_ : List[Any] = '🤗 Transformers currently provides the following architectures' UpperCAmelCase_ : int = '1. Want to contribute a new model?' with open(__a , 'r' , encoding='utf-8' , newline='\n' ) as f: UpperCAmelCase_ : Optional[Any] = f.readlines() # Find the start of the list. UpperCAmelCase_ : Any = 0 while not lines[start_index].startswith(_start_prompt ): start_index += 1 start_index += 1 UpperCAmelCase_ : Optional[Any] = start_index # Update the lines in the model list. while not lines[index].startswith(_end_prompt ): if lines[index].startswith('1.' ): UpperCAmelCase_ : Union[str, Any] = lines[index].replace( 'https://huggingface.co/docs/diffusers/main/model_doc' , 'https://huggingface.co/docs/diffusers/model_doc' , ) index += 1 with open(__a , 'w' , encoding='utf-8' , newline='\n' ) as f: f.writelines(__a ) def lowercase__ ( ): '''simple docstring''' with open(REPLACE_FILES['init'] , 'r' ) as f: UpperCAmelCase_ : int = f.read() UpperCAmelCase_ : int = REPLACE_PATTERNS['init'][0].search(__a ).groups()[0] return packaging.version.parse(__a ) def lowercase__ ( __snake_case : Any=False ): '''simple docstring''' UpperCAmelCase_ : List[Any] = get_version() if patch and default_version.is_devrelease: raise ValueError('Can\'t create a patch version from the dev branch, checkout a released version!' ) if default_version.is_devrelease: UpperCAmelCase_ : Optional[Any] = default_version.base_version elif patch: UpperCAmelCase_ : Optional[int] = F"{default_version.major}.{default_version.minor}.{default_version.micro + 1}" else: UpperCAmelCase_ : List[str] = F"{default_version.major}.{default_version.minor + 1}.0" # Now let's ask nicely if that's the right one. UpperCAmelCase_ : str = input(F"Which version are you releasing? [{default_version}]" ) if len(__a ) == 0: UpperCAmelCase_ : Dict = default_version print(F"Updating version to {version}." ) global_version_update(__a , patch=__a ) def lowercase__ ( ): '''simple docstring''' UpperCAmelCase_ : Union[str, Any] = get_version() UpperCAmelCase_ : Any = F"{current_version.major}.{current_version.minor + 1}.0.dev0" UpperCAmelCase_ : Union[str, Any] = current_version.base_version # Check with the user we got that right. UpperCAmelCase_ : int = input(F"Which version are we developing now? [{dev_version}]" ) if len(__a ) == 0: UpperCAmelCase_ : Any = dev_version print(F"Updating version to {version}." ) global_version_update(__a ) # print("Cleaning main README, don't forget to run `make fix-copies`.") # clean_main_ref_in_model_list() if __name__ == "__main__": __UpperCAmelCase = argparse.ArgumentParser() parser.add_argument('--post_release', action='store_true', help='Whether this is pre or post release.') parser.add_argument('--patch', action='store_true', help='Whether or not this is a patch release.') __UpperCAmelCase = parser.parse_args() if not args.post_release: pre_release_work(patch=args.patch) elif args.patch: print('Nothing to do after a patch :-)') else: post_release_work()
29
import argparse from argparse import Namespace import torch from torch import nn from transformers import XGLMConfig, XGLMForCausalLM def __lowerCamelCase ( __a :Dict ) -> Any: """simple docstring""" A__ = [ """decoder.version""", """decoder.output_projection.weight""", """_float_tensor""", """decoder.embed_positions._float_tensor""", ] for k in ignore_keys: state_dict.pop(__a , __a ) def __lowerCamelCase ( __a :str ) -> Union[str, Any]: """simple docstring""" A__ , A__ = emb.weight.shape A__ = nn.Linear(__a , __a , bias=__a ) A__ = emb.weight.data return lin_layer def __lowerCamelCase ( __a :str ) -> List[str]: """simple docstring""" A__ = torch.load(__a , map_location="""cpu""" ) A__ = Namespace(**checkpoint["""cfg"""]["""model"""] ) A__ = checkpoint["""model"""] remove_ignore_keys_(__a ) A__ = state_dict["""decoder.embed_tokens.weight"""].shape[0] A__ = {key.replace("""decoder""" , """model""" ): val for key, val in state_dict.items()} A__ = XGLMConfig( vocab_size=__a , max_position_embeddings=args.max_target_positions , num_layers=args.decoder_layers , attention_heads=args.decoder_attention_heads , ffn_dim=args.decoder_ffn_embed_dim , d_model=args.decoder_embed_dim , layerdrop=args.decoder_layerdrop , dropout=args.dropout , attention_dropout=args.attention_dropout , activation_dropout=args.activation_dropout , activation_function="""gelu""" , scale_embedding=not args.no_scale_embedding , tie_word_embeddings=args.share_decoder_input_output_embed , ) A__ = XGLMForCausalLM(__a ) A__ = model.load_state_dict(__a , strict=__a ) print(__a ) A__ = make_linear_from_emb(model.model.embed_tokens ) return model if __name__ == "__main__": A : int = argparse.ArgumentParser() # Required parameters parser.add_argument('''fairseq_path''', type=str, help='''path to a model.pt on local filesystem.''') parser.add_argument('''pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''') A : str = parser.parse_args() A : str = convert_fairseq_xglm_checkpoint_from_disk(args.fairseq_path) model.save_pretrained(args.pytorch_dump_folder_path)
274
0
from ...configuration_utils import PretrainedConfig from ...utils import logging a_ = logging.get_logger(__name__) a_ = { '''tiiuae/falcon-40b''': '''https://huggingface.co/tiiuae/falcon-40b/resolve/main/config.json''', '''tiiuae/falcon-7b''': '''https://huggingface.co/tiiuae/falcon-7b/resolve/main/config.json''', } class lowercase__ ( _UpperCAmelCase ): a_ ='''falcon''' a_ =['''past_key_values'''] def __init__( self , __UpperCAmelCase=65024 , __UpperCAmelCase=4544 , __UpperCAmelCase=32 , __UpperCAmelCase=71 , __UpperCAmelCase=1E-5 , __UpperCAmelCase=0.02 , __UpperCAmelCase=True , __UpperCAmelCase=0.0 , __UpperCAmelCase=0.0 , __UpperCAmelCase=None , __UpperCAmelCase=False , __UpperCAmelCase=False , __UpperCAmelCase=True , __UpperCAmelCase=True , __UpperCAmelCase=False , __UpperCAmelCase=11 , __UpperCAmelCase=11 , **__UpperCAmelCase , )-> Union[str, Any]: '''simple docstring''' lowerCAmelCase__ = vocab_size # Backward compatibility with n_embed kwarg lowerCAmelCase__ = kwargs.pop("n_embed" , __lowerCAmelCase ) lowerCAmelCase__ = hidden_size if n_embed is None else n_embed lowerCAmelCase__ = num_hidden_layers lowerCAmelCase__ = num_attention_heads lowerCAmelCase__ = layer_norm_epsilon lowerCAmelCase__ = initializer_range lowerCAmelCase__ = use_cache lowerCAmelCase__ = hidden_dropout lowerCAmelCase__ = attention_dropout lowerCAmelCase__ = bos_token_id lowerCAmelCase__ = eos_token_id lowerCAmelCase__ = num_attention_heads if num_kv_heads is None else num_kv_heads lowerCAmelCase__ = alibi lowerCAmelCase__ = new_decoder_architecture lowerCAmelCase__ = multi_query # Ignored when new_decoder_architecture is True lowerCAmelCase__ = parallel_attn lowerCAmelCase__ = bias super().__init__(bos_token_id=__lowerCAmelCase , eos_token_id=__lowerCAmelCase , **__lowerCAmelCase ) @property def UpperCAmelCase ( self )-> int: '''simple docstring''' return self.hidden_size // self.num_attention_heads @property def UpperCAmelCase ( self )-> Optional[int]: '''simple docstring''' return not self.alibi
340
import unittest import numpy as np from transformers import AlbertConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax.numpy as jnp from transformers.models.albert.modeling_flax_albert import ( FlaxAlbertForMaskedLM, FlaxAlbertForMultipleChoice, FlaxAlbertForPreTraining, FlaxAlbertForQuestionAnswering, FlaxAlbertForSequenceClassification, FlaxAlbertForTokenClassification, FlaxAlbertModel, ) class A (unittest.TestCase ): '''simple docstring''' def __init__( self : List[str] , __lowerCAmelCase : int , __lowerCAmelCase : List[str]=13 , __lowerCAmelCase : int=7 , __lowerCAmelCase : Tuple=True , __lowerCAmelCase : int=True , __lowerCAmelCase : Dict=True , __lowerCAmelCase : Union[str, Any]=True , __lowerCAmelCase : List[Any]=99 , __lowerCAmelCase : Optional[Any]=32 , __lowerCAmelCase : Optional[Any]=5 , __lowerCAmelCase : Tuple=4 , __lowerCAmelCase : Any=37 , __lowerCAmelCase : str="gelu" , __lowerCAmelCase : Optional[Any]=0.1 , __lowerCAmelCase : Union[str, Any]=0.1 , __lowerCAmelCase : List[str]=5_12 , __lowerCAmelCase : int=16 , __lowerCAmelCase : Optional[int]=2 , __lowerCAmelCase : List[Any]=0.0_2 , __lowerCAmelCase : Tuple=4 , ) -> Dict: """simple docstring""" A__ = parent A__ = batch_size A__ = seq_length A__ = is_training A__ = use_attention_mask A__ = use_token_type_ids A__ = use_labels A__ = vocab_size A__ = hidden_size A__ = num_hidden_layers A__ = num_attention_heads A__ = intermediate_size A__ = hidden_act A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = max_position_embeddings A__ = type_vocab_size A__ = type_sequence_label_size A__ = initializer_range A__ = num_choices def a_ ( self : Any ) -> str: """simple docstring""" A__ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) A__ = None if self.use_attention_mask: A__ = random_attention_mask([self.batch_size, self.seq_length] ) A__ = None if self.use_token_type_ids: A__ = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) A__ = AlbertConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=__lowerCAmelCase , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def a_ ( self : Optional[int] ) -> Optional[int]: """simple docstring""" A__ = self.prepare_config_and_inputs() A__ , A__ , A__ , A__ = config_and_inputs A__ = {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": attention_mask} return config, inputs_dict @require_flax class A (SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : str = ( ( FlaxAlbertModel, FlaxAlbertForPreTraining, FlaxAlbertForMaskedLM, FlaxAlbertForMultipleChoice, FlaxAlbertForQuestionAnswering, FlaxAlbertForSequenceClassification, FlaxAlbertForTokenClassification, FlaxAlbertForQuestionAnswering, ) if is_flax_available() else () ) def a_ ( self : str ) -> Optional[int]: """simple docstring""" A__ = FlaxAlbertModelTester(self ) @slow def a_ ( self : int ) -> Tuple: """simple docstring""" for model_class_name in self.all_model_classes: A__ = model_class_name.from_pretrained("""albert-base-v2""" ) A__ = model(np.ones((1, 1) ) ) self.assertIsNotNone(__lowerCAmelCase ) @require_flax class A (unittest.TestCase ): '''simple docstring''' @slow def a_ ( self : Dict ) -> List[Any]: """simple docstring""" A__ = FlaxAlbertModel.from_pretrained("""albert-base-v2""" ) A__ = np.array([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]] ) A__ = np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) A__ = model(__lowerCAmelCase , attention_mask=__lowerCAmelCase )[0] A__ = (1, 11, 7_68) self.assertEqual(output.shape , __lowerCAmelCase ) A__ = np.array( [[[-0.6_5_1_3, 1.5_0_3_5, -0.2_7_6_6], [-0.6_5_1_5, 1.5_0_4_6, -0.2_7_8_0], [-0.6_5_1_2, 1.5_0_4_9, -0.2_7_8_4]]] ) self.assertTrue(jnp.allclose(output[:, 1:4, 1:4] , __lowerCAmelCase , atol=1e-4 ) )
274
0
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging _A = logging.get_logger(__name__) _A = { '''xlm-roberta-base''': '''https://huggingface.co/xlm-roberta-base/resolve/main/config.json''', '''xlm-roberta-large''': '''https://huggingface.co/xlm-roberta-large/resolve/main/config.json''', '''xlm-roberta-large-finetuned-conll02-dutch''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll02-dutch/resolve/main/config.json''' ), '''xlm-roberta-large-finetuned-conll02-spanish''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll02-spanish/resolve/main/config.json''' ), '''xlm-roberta-large-finetuned-conll03-english''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll03-english/resolve/main/config.json''' ), '''xlm-roberta-large-finetuned-conll03-german''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll03-german/resolve/main/config.json''' ), } class lowercase_ ( __SCREAMING_SNAKE_CASE ): A__ : Any = '''xlm-roberta''' def __init__( self , __UpperCamelCase=3_0_5_2_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=5_1_2 , __UpperCamelCase=2 , __UpperCamelCase=0.02 , __UpperCamelCase=1e-12 , __UpperCamelCase=1 , __UpperCamelCase=0 , __UpperCamelCase=2 , __UpperCamelCase="absolute" , __UpperCamelCase=True , __UpperCamelCase=None , **__UpperCamelCase , ): """simple docstring""" super().__init__(pad_token_id=__lowerCAmelCase , bos_token_id=__lowerCAmelCase , eos_token_id=__lowerCAmelCase , **__lowerCAmelCase ) UpperCamelCase_ = vocab_size UpperCamelCase_ = hidden_size UpperCamelCase_ = num_hidden_layers UpperCamelCase_ = num_attention_heads UpperCamelCase_ = hidden_act UpperCamelCase_ = intermediate_size UpperCamelCase_ = hidden_dropout_prob UpperCamelCase_ = attention_probs_dropout_prob UpperCamelCase_ = max_position_embeddings UpperCamelCase_ = type_vocab_size UpperCamelCase_ = initializer_range UpperCamelCase_ = layer_norm_eps UpperCamelCase_ = position_embedding_type UpperCamelCase_ = use_cache UpperCamelCase_ = classifier_dropout class lowercase_ ( __SCREAMING_SNAKE_CASE ): @property def lowerCamelCase_ ( self ): """simple docstring""" if self.task == "multiple-choice": UpperCamelCase_ = {0: """batch""", 1: """choice""", 2: """sequence"""} else: UpperCamelCase_ = {0: """batch""", 1: """sequence"""} return OrderedDict( [ ("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ] )
122
from sklearn.metrics import fa_score import datasets A : Any = ''' The F1 score is the harmonic mean of the precision and recall. It can be computed with the equation: F1 = 2 * (precision * recall) / (precision + recall) ''' A : List[Any] = ''' Args: predictions (`list` of `int`): Predicted labels. references (`list` of `int`): Ground truth labels. labels (`list` of `int`): The set of labels to include when `average` is not set to `\'binary\'`, and the order of the labels if `average` is `None`. Labels present in the data can be excluded, for example to calculate a multiclass average ignoring a majority negative class. Labels not present in the data will result in 0 components in a macro average. For multilabel targets, labels are column indices. By default, all labels in `predictions` and `references` are used in sorted order. Defaults to None. pos_label (`int`): The class to be considered the positive class, in the case where `average` is set to `binary`. Defaults to 1. average (`string`): This parameter is required for multiclass/multilabel targets. If set to `None`, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data. Defaults to `\'binary\'`. - \'binary\': Only report results for the class specified by `pos_label`. This is applicable only if the classes found in `predictions` and `references` are binary. - \'micro\': Calculate metrics globally by counting the total true positives, false negatives and false positives. - \'macro\': Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account. - \'weighted\': Calculate metrics for each label, and find their average weighted by support (the number of true instances for each label). This alters `\'macro\'` to account for label imbalance. This option can result in an F-score that is not between precision and recall. - \'samples\': Calculate metrics for each instance, and find their average (only meaningful for multilabel classification). sample_weight (`list` of `float`): Sample weights Defaults to None. Returns: f1 (`float` or `array` of `float`): F1 score or list of f1 scores, depending on the value passed to `average`. Minimum possible value is 0. Maximum possible value is 1. Higher f1 scores are better. Examples: Example 1-A simple binary example >>> f1_metric = datasets.load_metric("f1") >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0]) >>> print(results) {\'f1\': 0.5} Example 2-The same simple binary example as in Example 1, but with `pos_label` set to `0`. >>> f1_metric = datasets.load_metric("f1") >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0], pos_label=0) >>> print(round(results[\'f1\'], 2)) 0.67 Example 3-The same simple binary example as in Example 1, but with `sample_weight` included. >>> f1_metric = datasets.load_metric("f1") >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0], sample_weight=[0.9, 0.5, 3.9, 1.2, 0.3]) >>> print(round(results[\'f1\'], 2)) 0.35 Example 4-A multiclass example, with different values for the `average` input. >>> predictions = [0, 2, 1, 0, 0, 1] >>> references = [0, 1, 2, 0, 1, 2] >>> results = f1_metric.compute(predictions=predictions, references=references, average="macro") >>> print(round(results[\'f1\'], 2)) 0.27 >>> results = f1_metric.compute(predictions=predictions, references=references, average="micro") >>> print(round(results[\'f1\'], 2)) 0.33 >>> results = f1_metric.compute(predictions=predictions, references=references, average="weighted") >>> print(round(results[\'f1\'], 2)) 0.27 >>> results = f1_metric.compute(predictions=predictions, references=references, average=None) >>> print(results) {\'f1\': array([0.8, 0. , 0. ])} ''' A : List[Any] = ''' @article{scikit-learn, title={Scikit-learn: Machine Learning in {P}ython}, author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V. and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P. and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.}, journal={Journal of Machine Learning Research}, volume={12}, pages={2825--2830}, year={2011} } ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class A (datasets.Metric ): '''simple docstring''' def a_ ( self : Optional[int] ) -> List[Any]: """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": datasets.Sequence(datasets.Value("""int32""" ) ), """references""": datasets.Sequence(datasets.Value("""int32""" ) ), } if self.config_name == """multilabel""" else { """predictions""": datasets.Value("""int32""" ), """references""": datasets.Value("""int32""" ), } ) , reference_urls=["""https://scikit-learn.org/stable/modules/generated/sklearn.metrics.f1_score.html"""] , ) def a_ ( self : Any , __lowerCAmelCase : Tuple , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : Dict=None , __lowerCAmelCase : List[str]=1 , __lowerCAmelCase : Any="binary" , __lowerCAmelCase : Optional[int]=None ) -> List[Any]: """simple docstring""" A__ = fa_score( __lowerCAmelCase , __lowerCAmelCase , labels=__lowerCAmelCase , pos_label=__lowerCAmelCase , average=__lowerCAmelCase , sample_weight=__lowerCAmelCase ) return {"f1": float(__lowerCAmelCase ) if score.size == 1 else score}
274
0
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_albert import AlbertTokenizer else: UpperCAmelCase__ = None UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = {'''vocab_file''': '''spiece.model''', '''tokenizer_file''': '''tokenizer.json'''} UpperCAmelCase__ = { '''vocab_file''': { '''albert-base-v1''': '''https://huggingface.co/albert-base-v1/resolve/main/spiece.model''', '''albert-large-v1''': '''https://huggingface.co/albert-large-v1/resolve/main/spiece.model''', '''albert-xlarge-v1''': '''https://huggingface.co/albert-xlarge-v1/resolve/main/spiece.model''', '''albert-xxlarge-v1''': '''https://huggingface.co/albert-xxlarge-v1/resolve/main/spiece.model''', '''albert-base-v2''': '''https://huggingface.co/albert-base-v2/resolve/main/spiece.model''', '''albert-large-v2''': '''https://huggingface.co/albert-large-v2/resolve/main/spiece.model''', '''albert-xlarge-v2''': '''https://huggingface.co/albert-xlarge-v2/resolve/main/spiece.model''', '''albert-xxlarge-v2''': '''https://huggingface.co/albert-xxlarge-v2/resolve/main/spiece.model''', }, '''tokenizer_file''': { '''albert-base-v1''': '''https://huggingface.co/albert-base-v1/resolve/main/tokenizer.json''', '''albert-large-v1''': '''https://huggingface.co/albert-large-v1/resolve/main/tokenizer.json''', '''albert-xlarge-v1''': '''https://huggingface.co/albert-xlarge-v1/resolve/main/tokenizer.json''', '''albert-xxlarge-v1''': '''https://huggingface.co/albert-xxlarge-v1/resolve/main/tokenizer.json''', '''albert-base-v2''': '''https://huggingface.co/albert-base-v2/resolve/main/tokenizer.json''', '''albert-large-v2''': '''https://huggingface.co/albert-large-v2/resolve/main/tokenizer.json''', '''albert-xlarge-v2''': '''https://huggingface.co/albert-xlarge-v2/resolve/main/tokenizer.json''', '''albert-xxlarge-v2''': '''https://huggingface.co/albert-xxlarge-v2/resolve/main/tokenizer.json''', }, } UpperCAmelCase__ = { '''albert-base-v1''': 512, '''albert-large-v1''': 512, '''albert-xlarge-v1''': 512, '''albert-xxlarge-v1''': 512, '''albert-base-v2''': 512, '''albert-large-v2''': 512, '''albert-xlarge-v2''': 512, '''albert-xxlarge-v2''': 512, } 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__ = AlbertTokenizer def __init__(self , UpperCAmelCase=None , UpperCAmelCase=None , UpperCAmelCase=True , UpperCAmelCase=True , UpperCAmelCase=False , UpperCAmelCase="[CLS]" , UpperCAmelCase="[SEP]" , UpperCAmelCase="<unk>" , UpperCAmelCase="[SEP]" , UpperCAmelCase="<pad>" , UpperCAmelCase="[CLS]" , UpperCAmelCase="[MASK]" , **UpperCAmelCase , ) -> Optional[Any]: _lowercase =( AddedToken(__lowerCAmelCase , lstrip=__lowerCAmelCase , rstrip=__lowerCAmelCase , normalized=__lowerCAmelCase ) if isinstance(__lowerCAmelCase , __lowerCAmelCase ) else mask_token ) super().__init__( __lowerCAmelCase , tokenizer_file=__lowerCAmelCase , do_lower_case=__lowerCAmelCase , remove_space=__lowerCAmelCase , keep_accents=__lowerCAmelCase , bos_token=__lowerCAmelCase , eos_token=__lowerCAmelCase , unk_token=__lowerCAmelCase , sep_token=__lowerCAmelCase , pad_token=__lowerCAmelCase , cls_token=__lowerCAmelCase , mask_token=__lowerCAmelCase , **__lowerCAmelCase , ) _lowercase =do_lower_case _lowercase =remove_space _lowercase =keep_accents _lowercase =vocab_file _lowercase =False if not self.vocab_file else True 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 cls + token_ids_a + sep return cls + token_ids_a + 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 ) * [0] + len(token_ids_a + sep ) * [1] 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(__lowerCAmelCase ): logger.error(f"Vocabulary path ({save_directory}) should be a directory" ) return _lowercase =os.path.join( __lowerCAmelCase , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(__lowerCAmelCase ): copyfile(self.vocab_file , __lowerCAmelCase ) return (out_vocab_file,)
5
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging A : Union[str, Any] = logging.get_logger(__name__) A : int = { '''xlm-roberta-base''': '''https://huggingface.co/xlm-roberta-base/resolve/main/config.json''', '''xlm-roberta-large''': '''https://huggingface.co/xlm-roberta-large/resolve/main/config.json''', '''xlm-roberta-large-finetuned-conll02-dutch''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll02-dutch/resolve/main/config.json''' ), '''xlm-roberta-large-finetuned-conll02-spanish''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll02-spanish/resolve/main/config.json''' ), '''xlm-roberta-large-finetuned-conll03-english''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll03-english/resolve/main/config.json''' ), '''xlm-roberta-large-finetuned-conll03-german''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll03-german/resolve/main/config.json''' ), } class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : Any = '''xlm-roberta''' def __init__( self : Optional[Any] , __lowerCAmelCase : List[Any]=3_05_22 , __lowerCAmelCase : int=7_68 , __lowerCAmelCase : Tuple=12 , __lowerCAmelCase : str=12 , __lowerCAmelCase : Union[str, Any]=30_72 , __lowerCAmelCase : Union[str, Any]="gelu" , __lowerCAmelCase : Optional[int]=0.1 , __lowerCAmelCase : Dict=0.1 , __lowerCAmelCase : List[str]=5_12 , __lowerCAmelCase : Tuple=2 , __lowerCAmelCase : Dict=0.0_2 , __lowerCAmelCase : List[str]=1e-12 , __lowerCAmelCase : Union[str, Any]=1 , __lowerCAmelCase : str=0 , __lowerCAmelCase : Optional[int]=2 , __lowerCAmelCase : Tuple="absolute" , __lowerCAmelCase : Any=True , __lowerCAmelCase : Any=None , **__lowerCAmelCase : str , ) -> Optional[Any]: """simple docstring""" super().__init__(pad_token_id=__lowerCAmelCase , bos_token_id=__lowerCAmelCase , eos_token_id=__lowerCAmelCase , **__lowerCAmelCase ) A__ = vocab_size A__ = hidden_size A__ = num_hidden_layers A__ = num_attention_heads A__ = hidden_act A__ = intermediate_size A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = max_position_embeddings A__ = type_vocab_size A__ = initializer_range A__ = layer_norm_eps A__ = position_embedding_type A__ = use_cache A__ = classifier_dropout class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' @property def a_ ( self : int ) -> Mapping[str, Mapping[int, str]]: """simple docstring""" if self.task == "multiple-choice": A__ = {0: """batch""", 1: """choice""", 2: """sequence"""} else: A__ = {0: """batch""", 1: """sequence"""} return OrderedDict( [ ("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ] )
274
0
'''simple docstring''' import tempfile import torch from diffusers import ( DEISMultistepScheduler, DPMSolverMultistepScheduler, DPMSolverSinglestepScheduler, UniPCMultistepScheduler, ) from .test_schedulers import SchedulerCommonTest class lowerCAmelCase_ ( __magic_name__ ): __lowerCamelCase : Union[str, Any] = (DEISMultistepScheduler,) __lowerCamelCase : Union[str, Any] = (('''num_inference_steps''', 25),) def _snake_case ( self , **_lowerCAmelCase ) -> int: _lowerCAmelCase = { "num_train_timesteps": 1000, "beta_start": 0.0001, "beta_end": 0.02, "beta_schedule": "linear", "solver_order": 2, } config.update(**__lowerCAmelCase ) return config def _snake_case ( self , _lowerCAmelCase=0 , **_lowerCAmelCase ) -> int: _lowerCAmelCase = dict(self.forward_default_kwargs ) _lowerCAmelCase = kwargs.pop("num_inference_steps" , __lowerCAmelCase ) _lowerCAmelCase = self.dummy_sample _lowerCAmelCase = 0.1 * sample _lowerCAmelCase = [residual + 0.2, residual + 0.15, residual + 0.10] for scheduler_class in self.scheduler_classes: _lowerCAmelCase = self.get_scheduler_config(**__lowerCAmelCase ) _lowerCAmelCase = scheduler_class(**__lowerCAmelCase ) scheduler.set_timesteps(__lowerCAmelCase ) # copy over dummy past residuals _lowerCAmelCase = dummy_past_residuals[: scheduler.config.solver_order] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(__lowerCAmelCase ) _lowerCAmelCase = scheduler_class.from_pretrained(__lowerCAmelCase ) new_scheduler.set_timesteps(__lowerCAmelCase ) # copy over dummy past residuals _lowerCAmelCase = dummy_past_residuals[: new_scheduler.config.solver_order] _lowerCAmelCase , _lowerCAmelCase = sample, sample for t in range(__lowerCAmelCase , time_step + scheduler.config.solver_order + 1 ): _lowerCAmelCase = scheduler.step(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , **__lowerCAmelCase ).prev_sample _lowerCAmelCase = new_scheduler.step(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , **__lowerCAmelCase ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" def _snake_case ( self ) -> List[Any]: pass def _snake_case ( self , _lowerCAmelCase=0 , **_lowerCAmelCase ) -> Dict: _lowerCAmelCase = dict(self.forward_default_kwargs ) _lowerCAmelCase = kwargs.pop("num_inference_steps" , __lowerCAmelCase ) _lowerCAmelCase = self.dummy_sample _lowerCAmelCase = 0.1 * sample _lowerCAmelCase = [residual + 0.2, residual + 0.15, residual + 0.10] for scheduler_class in self.scheduler_classes: _lowerCAmelCase = self.get_scheduler_config() _lowerCAmelCase = scheduler_class(**__lowerCAmelCase ) scheduler.set_timesteps(__lowerCAmelCase ) # copy over dummy past residuals (must be after setting timesteps) _lowerCAmelCase = dummy_past_residuals[: scheduler.config.solver_order] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(__lowerCAmelCase ) _lowerCAmelCase = scheduler_class.from_pretrained(__lowerCAmelCase ) # copy over dummy past residuals new_scheduler.set_timesteps(__lowerCAmelCase ) # copy over dummy past residual (must be after setting timesteps) _lowerCAmelCase = dummy_past_residuals[: new_scheduler.config.solver_order] _lowerCAmelCase = scheduler.step(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , **__lowerCAmelCase ).prev_sample _lowerCAmelCase = new_scheduler.step(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , **__lowerCAmelCase ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" def _snake_case ( self , _lowerCAmelCase=None , **_lowerCAmelCase ) -> Any: if scheduler is None: _lowerCAmelCase = self.scheduler_classes[0] _lowerCAmelCase = self.get_scheduler_config(**__lowerCAmelCase ) _lowerCAmelCase = scheduler_class(**__lowerCAmelCase ) _lowerCAmelCase = self.scheduler_classes[0] _lowerCAmelCase = self.get_scheduler_config(**__lowerCAmelCase ) _lowerCAmelCase = scheduler_class(**__lowerCAmelCase ) _lowerCAmelCase = 10 _lowerCAmelCase = self.dummy_model() _lowerCAmelCase = self.dummy_sample_deter scheduler.set_timesteps(__lowerCAmelCase ) for i, t in enumerate(scheduler.timesteps ): _lowerCAmelCase = model(__lowerCAmelCase , __lowerCAmelCase ) _lowerCAmelCase = scheduler.step(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ).prev_sample return sample def _snake_case ( self ) -> Any: _lowerCAmelCase = dict(self.forward_default_kwargs ) _lowerCAmelCase = kwargs.pop("num_inference_steps" , __lowerCAmelCase ) for scheduler_class in self.scheduler_classes: _lowerCAmelCase = self.get_scheduler_config() _lowerCAmelCase = scheduler_class(**__lowerCAmelCase ) _lowerCAmelCase = self.dummy_sample _lowerCAmelCase = 0.1 * sample if num_inference_steps is not None and hasattr(__lowerCAmelCase , "set_timesteps" ): scheduler.set_timesteps(__lowerCAmelCase ) elif num_inference_steps is not None and not hasattr(__lowerCAmelCase , "set_timesteps" ): _lowerCAmelCase = num_inference_steps # copy over dummy past residuals (must be done after set_timesteps) _lowerCAmelCase = [residual + 0.2, residual + 0.15, residual + 0.10] _lowerCAmelCase = dummy_past_residuals[: scheduler.config.solver_order] _lowerCAmelCase = scheduler.timesteps[5] _lowerCAmelCase = scheduler.timesteps[6] _lowerCAmelCase = scheduler.step(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , **__lowerCAmelCase ).prev_sample _lowerCAmelCase = scheduler.step(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , **__lowerCAmelCase ).prev_sample self.assertEqual(output_a.shape , sample.shape ) self.assertEqual(output_a.shape , output_a.shape ) def _snake_case ( self ) -> Tuple: _lowerCAmelCase = DEISMultistepScheduler(**self.get_scheduler_config() ) _lowerCAmelCase = self.full_loop(scheduler=__lowerCAmelCase ) _lowerCAmelCase = torch.mean(torch.abs(__lowerCAmelCase ) ) assert abs(result_mean.item() - 0.23916 ) < 1E-3 _lowerCAmelCase = DPMSolverSinglestepScheduler.from_config(scheduler.config ) _lowerCAmelCase = DPMSolverMultistepScheduler.from_config(scheduler.config ) _lowerCAmelCase = UniPCMultistepScheduler.from_config(scheduler.config ) _lowerCAmelCase = DEISMultistepScheduler.from_config(scheduler.config ) _lowerCAmelCase = self.full_loop(scheduler=__lowerCAmelCase ) _lowerCAmelCase = torch.mean(torch.abs(__lowerCAmelCase ) ) assert abs(result_mean.item() - 0.23916 ) < 1E-3 def _snake_case ( self ) -> List[Any]: for timesteps in [25, 50, 100, 999, 1000]: self.check_over_configs(num_train_timesteps=__lowerCAmelCase ) def _snake_case ( self ) -> int: self.check_over_configs(thresholding=__lowerCAmelCase ) for order in [1, 2, 3]: for solver_type in ["logrho"]: for threshold in [0.5, 1.0, 2.0]: for prediction_type in ["epsilon", "sample"]: self.check_over_configs( thresholding=__lowerCAmelCase , prediction_type=__lowerCAmelCase , sample_max_value=__lowerCAmelCase , algorithm_type="deis" , solver_order=__lowerCAmelCase , solver_type=__lowerCAmelCase , ) def _snake_case ( self ) -> Optional[Any]: for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=__lowerCAmelCase ) def _snake_case ( self ) -> Any: for algorithm_type in ["deis"]: for solver_type in ["logrho"]: for order in [1, 2, 3]: for prediction_type in ["epsilon", "sample"]: self.check_over_configs( solver_order=__lowerCAmelCase , solver_type=__lowerCAmelCase , prediction_type=__lowerCAmelCase , algorithm_type=__lowerCAmelCase , ) _lowerCAmelCase = self.full_loop( solver_order=__lowerCAmelCase , solver_type=__lowerCAmelCase , prediction_type=__lowerCAmelCase , algorithm_type=__lowerCAmelCase , ) assert not torch.isnan(__lowerCAmelCase ).any(), "Samples have nan numbers" def _snake_case ( self ) -> Any: self.check_over_configs(lower_order_final=__lowerCAmelCase ) self.check_over_configs(lower_order_final=__lowerCAmelCase ) def _snake_case ( self ) -> Union[str, Any]: for num_inference_steps in [1, 2, 3, 5, 10, 50, 100, 999, 1000]: self.check_over_forward(num_inference_steps=__lowerCAmelCase , time_step=0 ) def _snake_case ( self ) -> Optional[Any]: _lowerCAmelCase = self.full_loop() _lowerCAmelCase = torch.mean(torch.abs(__lowerCAmelCase ) ) assert abs(result_mean.item() - 0.23916 ) < 1E-3 def _snake_case ( self ) -> Dict: _lowerCAmelCase = self.full_loop(prediction_type="v_prediction" ) _lowerCAmelCase = torch.mean(torch.abs(__lowerCAmelCase ) ) assert abs(result_mean.item() - 0.091 ) < 1E-3 def _snake_case ( self ) -> int: _lowerCAmelCase = self.scheduler_classes[0] _lowerCAmelCase = self.get_scheduler_config(thresholding=__lowerCAmelCase , dynamic_thresholding_ratio=0 ) _lowerCAmelCase = scheduler_class(**__lowerCAmelCase ) _lowerCAmelCase = 10 _lowerCAmelCase = self.dummy_model() _lowerCAmelCase = self.dummy_sample_deter.half() scheduler.set_timesteps(__lowerCAmelCase ) for i, t in enumerate(scheduler.timesteps ): _lowerCAmelCase = model(__lowerCAmelCase , __lowerCAmelCase ) _lowerCAmelCase = scheduler.step(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ).prev_sample assert sample.dtype == torch.floataa
158
from __future__ import annotations import time from math import sqrt # 1 for manhattan, 0 for euclidean A : str = 0 A : Any = [ [0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles [0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0], [1, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0], ] A : Dict = [[-1, 0], [0, -1], [1, 0], [0, 1]] # up, left, down, right A : Union[str, Any] = tuple[int, int] class A : '''simple docstring''' def __init__( self : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : Node | None , ) -> None: """simple docstring""" A__ = pos_x A__ = pos_y A__ = (pos_y, pos_x) A__ = goal_x A__ = goal_y A__ = g_cost A__ = parent A__ = self.calculate_heuristic() A__ = self.g_cost + self.h_cost def a_ ( self : Dict ) -> float: """simple docstring""" A__ = self.pos_x - self.goal_x A__ = self.pos_y - self.goal_y if HEURISTIC == 1: return abs(__lowerCAmelCase ) + abs(__lowerCAmelCase ) else: return sqrt(dy**2 + dx**2 ) def __lt__( self : int , __lowerCAmelCase : Node ) -> bool: """simple docstring""" return self.f_cost < other.f_cost class A : '''simple docstring''' def __init__( self : Union[str, Any] , __lowerCAmelCase : TPosition , __lowerCAmelCase : TPosition ) -> Tuple: """simple docstring""" A__ = Node(start[1] , start[0] , goal[1] , goal[0] , 0 , __lowerCAmelCase ) A__ = Node(goal[1] , goal[0] , goal[1] , goal[0] , 9_99_99 , __lowerCAmelCase ) A__ = [self.start] A__ = [] A__ = False def a_ ( self : List[str] ) -> list[TPosition]: """simple docstring""" while self.open_nodes: # Open Nodes are sorted using __lt__ self.open_nodes.sort() A__ = self.open_nodes.pop(0 ) if current_node.pos == self.target.pos: return self.retrace_path(__lowerCAmelCase ) self.closed_nodes.append(__lowerCAmelCase ) A__ = self.get_successors(__lowerCAmelCase ) for child_node in successors: if child_node in self.closed_nodes: continue if child_node not in self.open_nodes: self.open_nodes.append(__lowerCAmelCase ) else: # retrieve the best current path A__ = self.open_nodes.pop(self.open_nodes.index(__lowerCAmelCase ) ) if child_node.g_cost < better_node.g_cost: self.open_nodes.append(__lowerCAmelCase ) else: self.open_nodes.append(__lowerCAmelCase ) return [self.start.pos] def a_ ( self : Optional[Any] , __lowerCAmelCase : Node ) -> list[Node]: """simple docstring""" A__ = [] for action in delta: A__ = parent.pos_x + action[1] A__ = parent.pos_y + action[0] if not (0 <= pos_x <= len(grid[0] ) - 1 and 0 <= pos_y <= len(__lowerCAmelCase ) - 1): continue if grid[pos_y][pos_x] != 0: continue successors.append( Node( __lowerCAmelCase , __lowerCAmelCase , self.target.pos_y , self.target.pos_x , parent.g_cost + 1 , __lowerCAmelCase , ) ) return successors def a_ ( self : List[Any] , __lowerCAmelCase : Node | None ) -> list[TPosition]: """simple docstring""" A__ = node A__ = [] while current_node is not None: path.append((current_node.pos_y, current_node.pos_x) ) A__ = current_node.parent path.reverse() return path class A : '''simple docstring''' def __init__( self : Optional[Any] , __lowerCAmelCase : TPosition , __lowerCAmelCase : TPosition ) -> None: """simple docstring""" A__ = AStar(__lowerCAmelCase , __lowerCAmelCase ) A__ = AStar(__lowerCAmelCase , __lowerCAmelCase ) A__ = False def a_ ( self : int ) -> list[TPosition]: """simple docstring""" while self.fwd_astar.open_nodes or self.bwd_astar.open_nodes: self.fwd_astar.open_nodes.sort() self.bwd_astar.open_nodes.sort() A__ = self.fwd_astar.open_nodes.pop(0 ) A__ = self.bwd_astar.open_nodes.pop(0 ) if current_bwd_node.pos == current_fwd_node.pos: return self.retrace_bidirectional_path( __lowerCAmelCase , __lowerCAmelCase ) self.fwd_astar.closed_nodes.append(__lowerCAmelCase ) self.bwd_astar.closed_nodes.append(__lowerCAmelCase ) A__ = current_bwd_node A__ = current_fwd_node A__ = { self.fwd_astar: self.fwd_astar.get_successors(__lowerCAmelCase ), self.bwd_astar: self.bwd_astar.get_successors(__lowerCAmelCase ), } for astar in [self.fwd_astar, self.bwd_astar]: for child_node in successors[astar]: if child_node in astar.closed_nodes: continue if child_node not in astar.open_nodes: astar.open_nodes.append(__lowerCAmelCase ) else: # retrieve the best current path A__ = astar.open_nodes.pop( astar.open_nodes.index(__lowerCAmelCase ) ) if child_node.g_cost < better_node.g_cost: astar.open_nodes.append(__lowerCAmelCase ) else: astar.open_nodes.append(__lowerCAmelCase ) return [self.fwd_astar.start.pos] def a_ ( self : List[str] , __lowerCAmelCase : Node , __lowerCAmelCase : Node ) -> list[TPosition]: """simple docstring""" A__ = self.fwd_astar.retrace_path(__lowerCAmelCase ) A__ = self.bwd_astar.retrace_path(__lowerCAmelCase ) bwd_path.pop() bwd_path.reverse() A__ = fwd_path + bwd_path return path if __name__ == "__main__": # all coordinates are given in format [y,x] A : Optional[int] = (0, 0) A : int = (len(grid) - 1, len(grid[0]) - 1) for elem in grid: print(elem) A : Dict = time.time() A : Optional[Any] = AStar(init, goal) A : Optional[int] = a_star.search() A : Optional[int] = time.time() - start_time print(F'''AStar execution time = {end_time:f} seconds''') A : Dict = time.time() A : Tuple = BidirectionalAStar(init, goal) A : List[Any] = time.time() - bd_start_time print(F'''BidirectionalAStar execution time = {bd_end_time:f} seconds''')
274
0
import math def lowerCAmelCase__(__snake_case ) -> list[int]: '''simple docstring''' lowerCamelCase__ = [] lowerCamelCase__ = 2 lowerCamelCase__ = int(math.sqrt(__a ) ) # Size of every segment lowerCamelCase__ = [True] * (end + 1) lowerCamelCase__ = [] while start <= end: if temp[start] is True: in_prime.append(__a ) for i in range(start * start ,end + 1 ,__a ): lowerCamelCase__ = False start += 1 prime += in_prime lowerCamelCase__ = end + 1 lowerCamelCase__ = min(2 * end ,__a ) while low <= n: lowerCamelCase__ = [True] * (high - low + 1) for each in in_prime: lowerCamelCase__ = math.floor(low / each ) * each if t < low: t += each for j in range(__a ,high + 1 ,__a ): lowerCamelCase__ = False for j in range(len(__a ) ): if temp[j] is True: prime.append(j + low ) lowerCamelCase__ = high + 1 lowerCamelCase__ = min(high + end ,__a ) return prime print(sieve(10**6))
209
import unittest from transformers.utils.backbone_utils import ( BackboneMixin, get_aligned_output_features_output_indices, verify_out_features_out_indices, ) class A (unittest.TestCase ): '''simple docstring''' def a_ ( self : Any ) -> Union[str, Any]: """simple docstring""" A__ = ["""a""", """b""", """c"""] # Defaults to last layer if both are None A__ , A__ = get_aligned_output_features_output_indices(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , ["""c"""] ) self.assertEqual(__lowerCAmelCase , [2] ) # Out indices set to match out features A__ , A__ = get_aligned_output_features_output_indices(["""a""", """c"""] , __lowerCAmelCase , __lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , ["""a""", """c"""] ) self.assertEqual(__lowerCAmelCase , [0, 2] ) # Out features set to match out indices A__ , A__ = get_aligned_output_features_output_indices(__lowerCAmelCase , [0, 2] , __lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , ["""a""", """c"""] ) self.assertEqual(__lowerCAmelCase , [0, 2] ) # Out features selected from negative indices A__ , A__ = get_aligned_output_features_output_indices(__lowerCAmelCase , [-3, -1] , __lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , ["""a""", """c"""] ) self.assertEqual(__lowerCAmelCase , [-3, -1] ) def a_ ( self : Optional[Any] ) -> Optional[Any]: """simple docstring""" with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(["""a""", """b"""] , (0, 1) , __lowerCAmelCase ) # Out features must be a list with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(("""a""", """b""") , (0, 1) , ["""a""", """b"""] ) # Out features must be a subset of stage names with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(["""a""", """b"""] , (0, 1) , ["""a"""] ) # Out indices must be a list or tuple with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(__lowerCAmelCase , 0 , ["""a""", """b"""] ) # Out indices must be a subset of stage names with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(__lowerCAmelCase , (0, 1) , ["""a"""] ) # Out features and out indices must be the same length with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(["""a""", """b"""] , (0,) , ["""a""", """b""", """c"""] ) # Out features should match out indices with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(["""a""", """b"""] , (0, 2) , ["""a""", """b""", """c"""] ) # Out features and out indices should be in order with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(["""b""", """a"""] , (0, 1) , ["""a""", """b"""] ) # Check passes with valid inputs verify_out_features_out_indices(["""a""", """b""", """d"""] , (0, 1, -1) , ["""a""", """b""", """c""", """d"""] ) def a_ ( self : Union[str, Any] ) -> List[str]: """simple docstring""" A__ = BackboneMixin() A__ = ["""a""", """b""", """c"""] A__ = ["""a""", """c"""] A__ = [0, 2] # Check that the output features and indices are set correctly self.assertEqual(backbone.out_features , ["""a""", """c"""] ) self.assertEqual(backbone.out_indices , [0, 2] ) # Check out features and indices are updated correctly A__ = ["""a""", """b"""] self.assertEqual(backbone.out_features , ["""a""", """b"""] ) self.assertEqual(backbone.out_indices , [0, 1] ) A__ = [-3, -1] self.assertEqual(backbone.out_features , ["""a""", """c"""] ) self.assertEqual(backbone.out_indices , [-3, -1] )
274
0
import os import unicodedata from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import SPIECE_UNDERLINE, logging A : str = logging.get_logger(__name__) A : Union[str, Any] = {'''vocab_file''': '''spiece.model'''} A : Any = { '''vocab_file''': { '''xlnet-base-cased''': '''https://huggingface.co/xlnet-base-cased/resolve/main/spiece.model''', '''xlnet-large-cased''': '''https://huggingface.co/xlnet-large-cased/resolve/main/spiece.model''', } } A : List[str] = { '''xlnet-base-cased''': None, '''xlnet-large-cased''': None, } # Segments (not really needed) A : Dict = 0 A : Any = 1 A : str = 2 A : Optional[int] = 3 A : str = 4 class _lowercase ( lowercase__): """simple docstring""" A__ = VOCAB_FILES_NAMES A__ = PRETRAINED_VOCAB_FILES_MAP A__ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES A__ = '''left''' def __init__( self : Optional[Any] , __lowerCamelCase : Any , __lowerCamelCase : Optional[int]=False , __lowerCamelCase : int=True , __lowerCamelCase : List[str]=False , __lowerCamelCase : int="<s>" , __lowerCamelCase : Dict="</s>" , __lowerCamelCase : Optional[Any]="<unk>" , __lowerCamelCase : Union[str, Any]="<sep>" , __lowerCamelCase : Optional[Any]="<pad>" , __lowerCamelCase : Union[str, Any]="<cls>" , __lowerCamelCase : Optional[Any]="<mask>" , __lowerCamelCase : Dict=["<eop>", "<eod>"] , __lowerCamelCase : Optional[Dict[str, Any]] = None , **__lowerCamelCase : int , ): '''simple docstring''' lowerCamelCase__ : Any = AddedToken(__lowerCAmelCase , lstrip=__lowerCAmelCase , rstrip=__lowerCAmelCase ) if isinstance(__lowerCAmelCase , __lowerCAmelCase ) else mask_token lowerCamelCase__ : str = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( do_lower_case=__lowerCAmelCase , remove_space=__lowerCAmelCase , keep_accents=__lowerCAmelCase , bos_token=__lowerCAmelCase , eos_token=__lowerCAmelCase , unk_token=__lowerCAmelCase , sep_token=__lowerCAmelCase , pad_token=__lowerCAmelCase , cls_token=__lowerCAmelCase , mask_token=__lowerCAmelCase , additional_special_tokens=__lowerCAmelCase , sp_model_kwargs=self.sp_model_kwargs , **__lowerCAmelCase , ) lowerCamelCase__ : Optional[Any] = 3 lowerCamelCase__ : List[Any] = do_lower_case lowerCamelCase__ : Optional[int] = remove_space lowerCamelCase__ : Any = keep_accents lowerCamelCase__ : Tuple = vocab_file lowerCamelCase__ : Dict = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(__lowerCAmelCase ) @property def lowerCAmelCase ( self : List[Any] ): '''simple docstring''' return len(self.sp_model ) def lowerCAmelCase ( self : str ): '''simple docstring''' lowerCamelCase__ : Optional[Any] = {self.convert_ids_to_tokens(__lowerCAmelCase ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self : Optional[Any] ): '''simple docstring''' lowerCamelCase__ : str = self.__dict__.copy() lowerCamelCase__ : Union[str, Any] = None return state def __setstate__( self : str , __lowerCamelCase : Optional[Any] ): '''simple docstring''' lowerCamelCase__ : List[Any] = d # for backward compatibility if not hasattr(self , "sp_model_kwargs" ): lowerCamelCase__ : str = {} lowerCamelCase__ : str = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def lowerCAmelCase ( self : List[Any] , __lowerCamelCase : int ): '''simple docstring''' if self.remove_space: lowerCamelCase__ : str = " ".join(inputs.strip().split() ) else: lowerCamelCase__ : int = inputs lowerCamelCase__ : Dict = outputs.replace("``" , "\"" ).replace("''" , "\"" ) if not self.keep_accents: lowerCamelCase__ : Dict = unicodedata.normalize("NFKD" , __lowerCAmelCase ) lowerCamelCase__ : List[Any] = "".join([c for c in outputs if not unicodedata.combining(__lowerCAmelCase )] ) if self.do_lower_case: lowerCamelCase__ : str = outputs.lower() return outputs def lowerCAmelCase ( self : List[Any] , __lowerCamelCase : str ): '''simple docstring''' lowerCamelCase__ : Union[str, Any] = self.preprocess_text(__lowerCAmelCase ) lowerCamelCase__ : Union[str, Any] = self.sp_model.encode(__lowerCAmelCase , out_type=__lowerCAmelCase ) lowerCamelCase__ : Tuple = [] for piece in pieces: if len(__lowerCAmelCase ) > 1 and piece[-1] == str("," ) and piece[-2].isdigit(): lowerCamelCase__ : Any = self.sp_model.EncodeAsPieces(piece[:-1].replace(__lowerCAmelCase , "" ) ) if piece[0] != SPIECE_UNDERLINE and cur_pieces[0][0] == SPIECE_UNDERLINE: if len(cur_pieces[0] ) == 1: lowerCamelCase__ : Union[str, Any] = cur_pieces[1:] else: lowerCamelCase__ : Optional[Any] = cur_pieces[0][1:] cur_pieces.append(piece[-1] ) new_pieces.extend(__lowerCAmelCase ) else: new_pieces.append(__lowerCAmelCase ) return new_pieces def lowerCAmelCase ( self : Tuple , __lowerCamelCase : Dict ): '''simple docstring''' return self.sp_model.PieceToId(__lowerCAmelCase ) def lowerCAmelCase ( self : Union[str, Any] , __lowerCamelCase : List[Any] ): '''simple docstring''' return self.sp_model.IdToPiece(__lowerCAmelCase ) def lowerCAmelCase ( self : Optional[Any] , __lowerCamelCase : Dict ): '''simple docstring''' lowerCamelCase__ : int = "".join(__lowerCAmelCase ).replace(__lowerCAmelCase , " " ).strip() return out_string def lowerCAmelCase ( self : int , __lowerCamelCase : List[int] , __lowerCamelCase : bool = False , __lowerCamelCase : bool = None , __lowerCamelCase : bool = True , **__lowerCamelCase : int , ): '''simple docstring''' lowerCamelCase__ : List[str] = kwargs.pop("use_source_tokenizer" , __lowerCAmelCase ) lowerCamelCase__ : Any = self.convert_ids_to_tokens(__lowerCAmelCase , skip_special_tokens=__lowerCAmelCase ) # To avoid mixing byte-level and unicode for byte-level BPT # we need to build string separately for added tokens and byte-level tokens # cf. https://github.com/huggingface/transformers/issues/1133 lowerCamelCase__ : Tuple = [] lowerCamelCase__ : str = [] for token in filtered_tokens: if skip_special_tokens and token in self.all_special_ids: continue if token in self.added_tokens_encoder: if current_sub_text: sub_texts.append(self.convert_tokens_to_string(__lowerCAmelCase ) ) lowerCamelCase__ : List[str] = [] sub_texts.append(__lowerCAmelCase ) else: current_sub_text.append(__lowerCAmelCase ) if current_sub_text: sub_texts.append(self.convert_tokens_to_string(__lowerCAmelCase ) ) # Mimic the behavior of the Rust tokenizer: # By default, there are no spaces between special tokens lowerCamelCase__ : str = "".join(__lowerCAmelCase ) lowerCamelCase__ : Union[str, Any] = ( clean_up_tokenization_spaces if clean_up_tokenization_spaces is not None else self.clean_up_tokenization_spaces ) if clean_up_tokenization_spaces: lowerCamelCase__ : Tuple = self.clean_up_tokenization(__lowerCAmelCase ) return clean_text else: return text def lowerCAmelCase ( self : Optional[int] , __lowerCamelCase : List[int] , __lowerCamelCase : Optional[List[int]] = None ): '''simple docstring''' lowerCamelCase__ : Optional[Any] = [self.sep_token_id] lowerCamelCase__ : int = [self.cls_token_id] if token_ids_a is None: return token_ids_a + sep + cls return token_ids_a + sep + token_ids_a + sep + cls def lowerCAmelCase ( self : Tuple , __lowerCamelCase : List[int] , __lowerCamelCase : Optional[List[int]] = None , __lowerCamelCase : bool = False ): '''simple docstring''' if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=__lowerCAmelCase , token_ids_a=__lowerCAmelCase , already_has_special_tokens=__lowerCAmelCase ) if token_ids_a is not None: return ([0] * len(__lowerCAmelCase )) + [1] + ([0] * len(__lowerCAmelCase )) + [1, 1] return ([0] * len(__lowerCAmelCase )) + [1, 1] def lowerCAmelCase ( self : Dict , __lowerCamelCase : List[int] , __lowerCamelCase : Optional[List[int]] = None ): '''simple docstring''' lowerCamelCase__ : Dict = [self.sep_token_id] lowerCamelCase__ : List[Any] = [2] if token_ids_a is None: return len(token_ids_a + sep ) * [0] + cls_segment_id return len(token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] + cls_segment_id def lowerCAmelCase ( self : Dict , __lowerCamelCase : str , __lowerCamelCase : Optional[str] = None ): '''simple docstring''' if not os.path.isdir(__lowerCAmelCase ): logger.error(f"Vocabulary path ({save_directory}) should be a directory" ) return lowerCamelCase__ : Optional[int] = os.path.join( __lowerCAmelCase , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(__lowerCAmelCase ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , __lowerCAmelCase ) elif not os.path.isfile(self.vocab_file ): with open(__lowerCAmelCase , "wb" ) as fi: lowerCamelCase__ : Tuple = self.sp_model.serialized_model_proto() fi.write(__lowerCAmelCase ) return (out_vocab_file,)
184
from collections import deque class A : '''simple docstring''' def __init__( self : List[str] , __lowerCAmelCase : str , __lowerCAmelCase : int , __lowerCAmelCase : int ) -> None: """simple docstring""" A__ = process_name # process name A__ = arrival_time # arrival time of the process # completion time of finished process or last interrupted time A__ = arrival_time A__ = burst_time # remaining burst time A__ = 0 # total time of the process wait in ready queue A__ = 0 # time from arrival time to completion time class A : '''simple docstring''' def __init__( self : Tuple , __lowerCAmelCase : int , __lowerCAmelCase : list[int] , __lowerCAmelCase : deque[Process] , __lowerCAmelCase : int , ) -> None: """simple docstring""" A__ = number_of_queues # time slice of queues that round robin algorithm applied A__ = time_slices # unfinished process is in this ready_queue A__ = queue # current time A__ = current_time # finished process is in this sequence queue A__ = deque() def a_ ( self : Dict ) -> list[str]: """simple docstring""" A__ = [] for i in range(len(self.finish_queue ) ): sequence.append(self.finish_queue[i].process_name ) return sequence def a_ ( self : Tuple , __lowerCAmelCase : list[Process] ) -> list[int]: """simple docstring""" A__ = [] for i in range(len(__lowerCAmelCase ) ): waiting_times.append(queue[i].waiting_time ) return waiting_times def a_ ( self : Optional[Any] , __lowerCAmelCase : list[Process] ) -> list[int]: """simple docstring""" A__ = [] for i in range(len(__lowerCAmelCase ) ): turnaround_times.append(queue[i].turnaround_time ) return turnaround_times def a_ ( self : Dict , __lowerCAmelCase : list[Process] ) -> list[int]: """simple docstring""" A__ = [] for i in range(len(__lowerCAmelCase ) ): completion_times.append(queue[i].stop_time ) return completion_times def a_ ( self : int , __lowerCAmelCase : deque[Process] ) -> list[int]: """simple docstring""" return [q.burst_time for q in queue] def a_ ( self : Any , __lowerCAmelCase : Process ) -> int: """simple docstring""" process.waiting_time += self.current_time - process.stop_time return process.waiting_time def a_ ( self : Union[str, Any] , __lowerCAmelCase : deque[Process] ) -> deque[Process]: """simple docstring""" A__ = deque() # sequence deque of finished process while len(__lowerCAmelCase ) != 0: A__ = ready_queue.popleft() # current process # if process's arrival time is later than current time, update current time if self.current_time < cp.arrival_time: self.current_time += cp.arrival_time # update waiting time of current process self.update_waiting_time(__lowerCAmelCase ) # update current time self.current_time += cp.burst_time # finish the process and set the process's burst-time 0 A__ = 0 # set the process's turnaround time because it is finished A__ = self.current_time - cp.arrival_time # set the completion time A__ = self.current_time # add the process to queue that has finished queue finished.append(__lowerCAmelCase ) self.finish_queue.extend(__lowerCAmelCase ) # add finished process to finish queue # FCFS will finish all remaining processes return finished def a_ ( self : Optional[Any] , __lowerCAmelCase : deque[Process] , __lowerCAmelCase : int ) -> tuple[deque[Process], deque[Process]]: """simple docstring""" A__ = deque() # sequence deque of terminated process # just for 1 cycle and unfinished processes will go back to queue for _ in range(len(__lowerCAmelCase ) ): A__ = ready_queue.popleft() # current process # if process's arrival time is later than current time, update current time if self.current_time < cp.arrival_time: self.current_time += cp.arrival_time # update waiting time of unfinished processes self.update_waiting_time(__lowerCAmelCase ) # if the burst time of process is bigger than time-slice if cp.burst_time > time_slice: # use CPU for only time-slice self.current_time += time_slice # update remaining burst time cp.burst_time -= time_slice # update end point time A__ = self.current_time # locate the process behind the queue because it is not finished ready_queue.append(__lowerCAmelCase ) else: # use CPU for remaining burst time self.current_time += cp.burst_time # set burst time 0 because the process is finished A__ = 0 # set the finish time A__ = self.current_time # update the process' turnaround time because it is finished A__ = self.current_time - cp.arrival_time # add the process to queue that has finished queue finished.append(__lowerCAmelCase ) self.finish_queue.extend(__lowerCAmelCase ) # add finished process to finish queue # return finished processes queue and remaining processes queue return finished, ready_queue def a_ ( self : List[Any] ) -> deque[Process]: """simple docstring""" for i in range(self.number_of_queues - 1 ): A__ , A__ = self.round_robin( self.ready_queue , self.time_slices[i] ) # the last queue has first_come_first_served algorithm self.first_come_first_served(self.ready_queue ) return self.finish_queue if __name__ == "__main__": import doctest A : Union[str, Any] = Process('''P1''', 0, 5_3) A : Optional[Any] = Process('''P2''', 0, 1_7) A : Optional[int] = Process('''P3''', 0, 6_8) A : int = Process('''P4''', 0, 2_4) A : Any = 3 A : List[Any] = [1_7, 2_5] A : Optional[Any] = deque([Pa, Pa, Pa, Pa]) if len(time_slices) != number_of_queues - 1: raise SystemExit(0) doctest.testmod(extraglobs={'''queue''': deque([Pa, Pa, Pa, Pa])}) A : Optional[Any] = Process('''P1''', 0, 5_3) A : int = Process('''P2''', 0, 1_7) A : Optional[int] = Process('''P3''', 0, 6_8) A : Tuple = Process('''P4''', 0, 2_4) A : Union[str, Any] = 3 A : Optional[Any] = [1_7, 2_5] A : Tuple = deque([Pa, Pa, Pa, Pa]) A : Optional[int] = MLFQ(number_of_queues, time_slices, queue, 0) A : Dict = mlfq.multi_level_feedback_queue() # print total waiting times of processes(P1, P2, P3, P4) print( F'''waiting time:\ \t\t\t{MLFQ.calculate_waiting_time(mlfq, [Pa, Pa, Pa, Pa])}''' ) # print completion times of processes(P1, P2, P3, P4) print( F'''completion time:\ \t\t{MLFQ.calculate_completion_time(mlfq, [Pa, Pa, Pa, Pa])}''' ) # print total turnaround times of processes(P1, P2, P3, P4) print( F'''turnaround time:\ \t\t{MLFQ.calculate_turnaround_time(mlfq, [Pa, Pa, Pa, Pa])}''' ) # print sequence of finished processes print( F'''sequence of finished processes:\ {mlfq.calculate_sequence_of_finish_queue()}''' )
274
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available lowercase = { '''configuration_bloom''': ['''BLOOM_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''BloomConfig''', '''BloomOnnxConfig'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase = ['''BloomTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase = [ '''BLOOM_PRETRAINED_MODEL_ARCHIVE_LIST''', '''BloomForCausalLM''', '''BloomModel''', '''BloomPreTrainedModel''', '''BloomForSequenceClassification''', '''BloomForTokenClassification''', '''BloomForQuestionAnswering''', ] if TYPE_CHECKING: from .configuration_bloom import BLOOM_PRETRAINED_CONFIG_ARCHIVE_MAP, BloomConfig, BloomOnnxConfig try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_bloom_fast import BloomTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_bloom import ( BLOOM_PRETRAINED_MODEL_ARCHIVE_LIST, BloomForCausalLM, BloomForQuestionAnswering, BloomForSequenceClassification, BloomForTokenClassification, BloomModel, BloomPreTrainedModel, ) else: import sys lowercase = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
178
from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...onnx.utils import compute_effective_axis_dimension from ...utils import logging if TYPE_CHECKING: from ...processing_utils import ProcessorMixin from ...utils import TensorType A : str = logging.get_logger(__name__) A : Union[str, Any] = { '''microsoft/layoutlmv3-base''': '''https://huggingface.co/microsoft/layoutlmv3-base/resolve/main/config.json''', } class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : Optional[Any] = '''layoutlmv3''' def __init__( self : Tuple , __lowerCAmelCase : Optional[int]=5_02_65 , __lowerCAmelCase : Tuple=7_68 , __lowerCAmelCase : Union[str, Any]=12 , __lowerCAmelCase : Any=12 , __lowerCAmelCase : List[Any]=30_72 , __lowerCAmelCase : Optional[int]="gelu" , __lowerCAmelCase : Dict=0.1 , __lowerCAmelCase : List[Any]=0.1 , __lowerCAmelCase : Dict=5_12 , __lowerCAmelCase : Any=2 , __lowerCAmelCase : Dict=0.0_2 , __lowerCAmelCase : List[str]=1e-5 , __lowerCAmelCase : List[str]=1 , __lowerCAmelCase : int=0 , __lowerCAmelCase : Dict=2 , __lowerCAmelCase : Tuple=10_24 , __lowerCAmelCase : List[str]=1_28 , __lowerCAmelCase : Optional[int]=1_28 , __lowerCAmelCase : Any=True , __lowerCAmelCase : Optional[Any]=32 , __lowerCAmelCase : Any=1_28 , __lowerCAmelCase : str=64 , __lowerCAmelCase : Optional[int]=2_56 , __lowerCAmelCase : int=True , __lowerCAmelCase : int=True , __lowerCAmelCase : List[str]=True , __lowerCAmelCase : int=2_24 , __lowerCAmelCase : Dict=3 , __lowerCAmelCase : List[Any]=16 , __lowerCAmelCase : Dict=None , **__lowerCAmelCase : Optional[Any] , ) -> Dict: """simple docstring""" super().__init__( vocab_size=__lowerCAmelCase , hidden_size=__lowerCAmelCase , num_hidden_layers=__lowerCAmelCase , num_attention_heads=__lowerCAmelCase , intermediate_size=__lowerCAmelCase , hidden_act=__lowerCAmelCase , hidden_dropout_prob=__lowerCAmelCase , attention_probs_dropout_prob=__lowerCAmelCase , max_position_embeddings=__lowerCAmelCase , type_vocab_size=__lowerCAmelCase , initializer_range=__lowerCAmelCase , layer_norm_eps=__lowerCAmelCase , pad_token_id=__lowerCAmelCase , bos_token_id=__lowerCAmelCase , eos_token_id=__lowerCAmelCase , **__lowerCAmelCase , ) A__ = max_ad_position_embeddings A__ = coordinate_size A__ = shape_size A__ = has_relative_attention_bias A__ = rel_pos_bins A__ = max_rel_pos A__ = has_spatial_attention_bias A__ = rel_ad_pos_bins A__ = max_rel_ad_pos A__ = text_embed A__ = visual_embed A__ = input_size A__ = num_channels A__ = patch_size A__ = classifier_dropout class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : List[str] = version.parse('''1.12''' ) @property def a_ ( self : int ) -> Mapping[str, Mapping[int, str]]: """simple docstring""" if self.task in ["question-answering", "sequence-classification"]: return OrderedDict( [ ("""input_ids""", {0: """batch""", 1: """sequence"""}), ("""attention_mask""", {0: """batch""", 1: """sequence"""}), ("""bbox""", {0: """batch""", 1: """sequence"""}), ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}), ] ) else: return OrderedDict( [ ("""input_ids""", {0: """batch""", 1: """sequence"""}), ("""bbox""", {0: """batch""", 1: """sequence"""}), ("""attention_mask""", {0: """batch""", 1: """sequence"""}), ("""pixel_values""", {0: """batch""", 1: """num_channels"""}), ] ) @property def a_ ( self : Optional[int] ) -> float: """simple docstring""" return 1e-5 @property def a_ ( self : Tuple ) -> int: """simple docstring""" return 12 def a_ ( self : str , __lowerCAmelCase : "ProcessorMixin" , __lowerCAmelCase : int = -1 , __lowerCAmelCase : int = -1 , __lowerCAmelCase : bool = False , __lowerCAmelCase : Optional["TensorType"] = None , __lowerCAmelCase : int = 3 , __lowerCAmelCase : int = 40 , __lowerCAmelCase : int = 40 , ) -> Mapping[str, Any]: """simple docstring""" setattr(processor.image_processor , """apply_ocr""" , __lowerCAmelCase ) # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX A__ = compute_effective_axis_dimension( __lowerCAmelCase , fixed_dimension=OnnxConfig.default_fixed_batch , num_token_to_add=0 ) # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX A__ = processor.tokenizer.num_special_tokens_to_add(__lowerCAmelCase ) A__ = compute_effective_axis_dimension( __lowerCAmelCase , fixed_dimension=OnnxConfig.default_fixed_sequence , num_token_to_add=__lowerCAmelCase ) # Generate dummy inputs according to compute batch and sequence A__ = [[""" """.join([processor.tokenizer.unk_token] ) * seq_length]] * batch_size # Generate dummy bounding boxes A__ = [[[48, 84, 73, 1_28]]] * batch_size # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX # batch_size = compute_effective_axis_dimension(batch_size, fixed_dimension=OnnxConfig.default_fixed_batch) A__ = self._generate_dummy_images(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) A__ = dict( processor( __lowerCAmelCase , text=__lowerCAmelCase , boxes=__lowerCAmelCase , return_tensors=__lowerCAmelCase , ) ) return inputs
274
0
import argparse import torch from ...utils import logging from . import AlbertConfig, AlbertForPreTraining, load_tf_weights_in_albert logging.set_verbosity_info() def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : Tuple , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : Union[str, Any] ): __UpperCamelCase =AlbertConfig.from_json_file(__a ) print(F'Building PyTorch model from configuration: {config}' ) __UpperCamelCase =AlbertForPreTraining(__a ) # Load weights from tf checkpoint load_tf_weights_in_albert(__a , __a , __a ) # Save pytorch-model print(F'Save PyTorch model to {pytorch_dump_path}' ) torch.save(model.state_dict() , __a ) if __name__ == "__main__": _A = argparse.ArgumentParser() # Required parameters parser.add_argument( '--tf_checkpoint_path', default=None, type=str, required=True, help='Path to the TensorFlow checkpoint path.' ) parser.add_argument( '--albert_config_file', default=None, type=str, required=True, help=( 'The config json file corresponding to the pre-trained ALBERT 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.' ) _A = parser.parse_args() convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.albert_config_file, args.pytorch_dump_path)
62
import json import os from pathlib import Path from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple, Union import sentencepiece from ...tokenization_utils import BatchEncoding, PreTrainedTokenizer from ...utils import logging A : Optional[Any] = logging.get_logger(__name__) A : str = '''▁''' A : Any = { '''vocab_file''': '''vocab.json''', '''spm_file''': '''sentencepiece.bpe.model''', '''tokenizer_config_file''': '''tokenizer_config.json''', } A : List[Any] = { '''vocab_file''': { '''facebook/m2m100_418M''': '''https://huggingface.co/facebook/m2m100_418M/resolve/main/vocab.json''', '''facebook/m2m100_1.2B''': '''https://huggingface.co/facebook/m2m100_1.2B/resolve/main/vocab.json''', }, '''spm_file''': { '''facebook/m2m100_418M''': '''https://huggingface.co/facebook/m2m100_418M/resolve/main/sentencepiece.bpe.model''', '''facebook/m2m100_1.2B''': '''https://huggingface.co/facebook/m2m100_1.2B/resolve/main/sentencepiece.bpe.model''', }, '''tokenizer_config_file''': { '''facebook/m2m100_418M''': '''https://huggingface.co/facebook/m2m100_418M/resolve/main/tokenizer_config.json''', '''facebook/m2m100_1.2B''': '''https://huggingface.co/facebook/m2m100_1.2B/resolve/main/tokenizer_config.json''', }, } A : Tuple = { '''facebook/m2m100_418M''': 1_0_2_4, } # fmt: off A : Optional[int] = { '''m2m100''': ['''af''', '''am''', '''ar''', '''ast''', '''az''', '''ba''', '''be''', '''bg''', '''bn''', '''br''', '''bs''', '''ca''', '''ceb''', '''cs''', '''cy''', '''da''', '''de''', '''el''', '''en''', '''es''', '''et''', '''fa''', '''ff''', '''fi''', '''fr''', '''fy''', '''ga''', '''gd''', '''gl''', '''gu''', '''ha''', '''he''', '''hi''', '''hr''', '''ht''', '''hu''', '''hy''', '''id''', '''ig''', '''ilo''', '''is''', '''it''', '''ja''', '''jv''', '''ka''', '''kk''', '''km''', '''kn''', '''ko''', '''lb''', '''lg''', '''ln''', '''lo''', '''lt''', '''lv''', '''mg''', '''mk''', '''ml''', '''mn''', '''mr''', '''ms''', '''my''', '''ne''', '''nl''', '''no''', '''ns''', '''oc''', '''or''', '''pa''', '''pl''', '''ps''', '''pt''', '''ro''', '''ru''', '''sd''', '''si''', '''sk''', '''sl''', '''so''', '''sq''', '''sr''', '''ss''', '''su''', '''sv''', '''sw''', '''ta''', '''th''', '''tl''', '''tn''', '''tr''', '''uk''', '''ur''', '''uz''', '''vi''', '''wo''', '''xh''', '''yi''', '''yo''', '''zh''', '''zu'''], '''wmt21''': ['''en''', '''ha''', '''is''', '''ja''', '''cs''', '''ru''', '''zh''', '''de'''] } class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : Union[str, Any] = VOCAB_FILES_NAMES __lowerCamelCase : List[str] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase : Optional[int] = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase : Dict = ['''input_ids''', '''attention_mask'''] __lowerCamelCase : List[int] = [] __lowerCamelCase : List[int] = [] def __init__( self : List[Any] , __lowerCAmelCase : List[Any] , __lowerCAmelCase : Dict , __lowerCAmelCase : Union[str, Any]=None , __lowerCAmelCase : str=None , __lowerCAmelCase : List[Any]="<s>" , __lowerCAmelCase : List[Any]="</s>" , __lowerCAmelCase : Optional[int]="</s>" , __lowerCAmelCase : Optional[Any]="<pad>" , __lowerCAmelCase : Any="<unk>" , __lowerCAmelCase : Any="m2m100" , __lowerCAmelCase : Optional[Dict[str, Any]] = None , __lowerCAmelCase : Dict=8 , **__lowerCAmelCase : Tuple , ) -> None: """simple docstring""" A__ = {} if sp_model_kwargs is None else sp_model_kwargs A__ = language_codes A__ = FAIRSEQ_LANGUAGE_CODES[language_codes] A__ = {lang_code: f'__{lang_code}__' for lang_code in fairseq_language_code} A__ = kwargs.get("""additional_special_tokens""" , [] ) kwargs["additional_special_tokens"] += [ self.get_lang_token(__lowerCAmelCase ) for lang_code in fairseq_language_code if self.get_lang_token(__lowerCAmelCase ) not in kwargs["additional_special_tokens"] ] super().__init__( src_lang=__lowerCAmelCase , tgt_lang=__lowerCAmelCase , bos_token=__lowerCAmelCase , eos_token=__lowerCAmelCase , sep_token=__lowerCAmelCase , unk_token=__lowerCAmelCase , pad_token=__lowerCAmelCase , language_codes=__lowerCAmelCase , sp_model_kwargs=self.sp_model_kwargs , num_madeup_words=__lowerCAmelCase , **__lowerCAmelCase , ) A__ = vocab_file A__ = load_json(__lowerCAmelCase ) A__ = {v: k for k, v in self.encoder.items()} A__ = spm_file A__ = load_spm(__lowerCAmelCase , self.sp_model_kwargs ) A__ = len(self.encoder ) A__ = { self.get_lang_token(__lowerCAmelCase ): self.encoder_size + i for i, lang_code in enumerate(__lowerCAmelCase ) } A__ = {lang_code: self.encoder_size + i for i, lang_code in enumerate(__lowerCAmelCase )} A__ = {v: k for k, v in self.lang_token_to_id.items()} A__ = src_lang if src_lang is not None else """en""" A__ = tgt_lang A__ = self.get_lang_id(self._src_lang ) self.set_src_lang_special_tokens(self._src_lang ) A__ = num_madeup_words @property def a_ ( self : Optional[int] ) -> int: """simple docstring""" return len(self.encoder ) + len(self.lang_token_to_id ) @property def a_ ( self : Optional[Any] ) -> str: """simple docstring""" return self._src_lang @src_lang.setter def a_ ( self : List[Any] , __lowerCAmelCase : str ) -> None: """simple docstring""" A__ = new_src_lang self.set_src_lang_special_tokens(self._src_lang ) def a_ ( self : Optional[int] , __lowerCAmelCase : str ) -> List[str]: """simple docstring""" return self.sp_model.encode(__lowerCAmelCase , out_type=__lowerCAmelCase ) def a_ ( self : Optional[Any] , __lowerCAmelCase : Dict ) -> Optional[Any]: """simple docstring""" if token in self.lang_token_to_id: return self.lang_token_to_id[token] return self.encoder.get(__lowerCAmelCase , self.encoder[self.unk_token] ) def a_ ( self : Optional[int] , __lowerCAmelCase : int ) -> str: """simple docstring""" if index in self.id_to_lang_token: return self.id_to_lang_token[index] return self.decoder.get(__lowerCAmelCase , self.unk_token ) def a_ ( self : Optional[int] , __lowerCAmelCase : Dict ) -> str: """simple docstring""" A__ = [] A__ = """""" 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(__lowerCAmelCase ) + token A__ = [] else: current_sub_tokens.append(__lowerCAmelCase ) out_string += self.sp_model.decode(__lowerCAmelCase ) return out_string.strip() def a_ ( self : List[str] , __lowerCAmelCase : List[int] , __lowerCAmelCase : Optional[List[int]] = None , __lowerCAmelCase : bool = False ) -> List[int]: """simple docstring""" if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=__lowerCAmelCase , token_ids_a=__lowerCAmelCase , already_has_special_tokens=__lowerCAmelCase ) A__ = [1] * len(self.prefix_tokens ) A__ = [1] * len(self.suffix_tokens ) if token_ids_a is None: return prefix_ones + ([0] * len(__lowerCAmelCase )) + suffix_ones return prefix_ones + ([0] * len(__lowerCAmelCase )) + ([0] * len(__lowerCAmelCase )) + suffix_ones def a_ ( self : Tuple , __lowerCAmelCase : List[int] , __lowerCAmelCase : Optional[List[int]] = None ) -> List[int]: """simple docstring""" if token_ids_a is None: return self.prefix_tokens + token_ids_a + self.suffix_tokens # We don't expect to process pairs, but leave the pair logic for API consistency return self.prefix_tokens + token_ids_a + token_ids_a + self.suffix_tokens def a_ ( self : int ) -> Dict: """simple docstring""" A__ = {self.convert_ids_to_tokens(__lowerCAmelCase ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self : Union[str, Any] ) -> Dict: """simple docstring""" A__ = self.__dict__.copy() A__ = None return state def __setstate__( self : str , __lowerCAmelCase : Dict ) -> None: """simple docstring""" A__ = d # for backward compatibility if not hasattr(self , """sp_model_kwargs""" ): A__ = {} A__ = load_spm(self.spm_file , self.sp_model_kwargs ) def a_ ( self : List[str] , __lowerCAmelCase : str , __lowerCAmelCase : Optional[str] = None ) -> Tuple[str]: """simple docstring""" A__ = Path(__lowerCAmelCase ) if not save_dir.is_dir(): raise OSError(f'{save_directory} should be a directory' ) A__ = save_dir / ( (filename_prefix + """-""" if filename_prefix else """""") + self.vocab_files_names["""vocab_file"""] ) A__ = save_dir / ( (filename_prefix + """-""" if filename_prefix else """""") + self.vocab_files_names["""spm_file"""] ) save_json(self.encoder , __lowerCAmelCase ) if os.path.abspath(self.spm_file ) != os.path.abspath(__lowerCAmelCase ) and os.path.isfile(self.spm_file ): copyfile(self.spm_file , __lowerCAmelCase ) elif not os.path.isfile(self.spm_file ): with open(__lowerCAmelCase , """wb""" ) as fi: A__ = self.sp_model.serialized_model_proto() fi.write(__lowerCAmelCase ) return (str(__lowerCAmelCase ), str(__lowerCAmelCase )) def a_ ( self : str , __lowerCAmelCase : List[str] , __lowerCAmelCase : str = "en" , __lowerCAmelCase : Optional[List[str]] = None , __lowerCAmelCase : str = "ro" , **__lowerCAmelCase : List[Any] , ) -> BatchEncoding: """simple docstring""" A__ = src_lang A__ = tgt_lang self.set_src_lang_special_tokens(self.src_lang ) return super().prepare_seqaseq_batch(__lowerCAmelCase , __lowerCAmelCase , **__lowerCAmelCase ) def a_ ( self : Optional[int] , __lowerCAmelCase : Any , __lowerCAmelCase : Optional[str] , __lowerCAmelCase : Optional[str] , **__lowerCAmelCase : Tuple ) -> Tuple: """simple docstring""" if src_lang is None or tgt_lang is None: raise ValueError("""Translation requires a `src_lang` and a `tgt_lang` for this model""" ) A__ = src_lang A__ = self(__lowerCAmelCase , add_special_tokens=__lowerCAmelCase , **__lowerCAmelCase ) A__ = self.get_lang_id(__lowerCAmelCase ) A__ = tgt_lang_id return inputs def a_ ( self : Dict ) -> int: """simple docstring""" self.set_src_lang_special_tokens(self.src_lang ) def a_ ( self : Union[str, Any] ) -> Union[str, Any]: """simple docstring""" self.set_tgt_lang_special_tokens(self.tgt_lang ) def a_ ( self : str , __lowerCAmelCase : str ) -> None: """simple docstring""" A__ = self.get_lang_token(__lowerCAmelCase ) A__ = self.lang_token_to_id[lang_token] A__ = [self.cur_lang_id] A__ = [self.eos_token_id] def a_ ( self : Tuple , __lowerCAmelCase : str ) -> None: """simple docstring""" A__ = self.get_lang_token(__lowerCAmelCase ) A__ = self.lang_token_to_id[lang_token] A__ = [self.cur_lang_id] A__ = [self.eos_token_id] def a_ ( self : Union[str, Any] , __lowerCAmelCase : str ) -> str: """simple docstring""" return self.lang_code_to_token[lang] def a_ ( self : Union[str, Any] , __lowerCAmelCase : str ) -> int: """simple docstring""" A__ = self.get_lang_token(__lowerCAmelCase ) return self.lang_token_to_id[lang_token] def __lowerCamelCase ( __a :str , __a :Dict[str, Any] ) -> sentencepiece.SentencePieceProcessor: """simple docstring""" A__ = sentencepiece.SentencePieceProcessor(**__a ) spm.Load(str(__a ) ) return spm def __lowerCamelCase ( __a :str ) -> Union[Dict, List]: """simple docstring""" with open(__a , """r""" ) as f: return json.load(__a ) def __lowerCamelCase ( __a :List[Any] , __a :str ) -> None: """simple docstring""" with open(__a , """w""" ) as f: json.dump(__a , __a , indent=2 )
274
0
'''simple docstring''' def __lowerCAmelCase ( snake_case__ = 1_000 ): __UpperCamelCase , __UpperCamelCase : str = 1, 1 __UpperCamelCase : int = 2 while True: __UpperCamelCase : Tuple = 0 __UpperCamelCase : Dict = fa + fa __UpperCamelCase , __UpperCamelCase : Union[str, Any] = fa, f index += 1 for _ in str(__a ): i += 1 if i == n: break return index if __name__ == "__main__": print(solution(int(str(input()).strip())))
298
from __future__ import annotations from PIL import Image # Define glider example A : Any = [ [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [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], ] # Define blinker example A : Optional[Any] = [[0, 1, 0], [0, 1, 0], [0, 1, 0]] def __lowerCamelCase ( __a :list[list[int]] ) -> list[list[int]]: """simple docstring""" A__ = [] for i in range(len(__a ) ): A__ = [] for j in range(len(cells[i] ) ): # Get the number of live neighbours A__ = 0 if i > 0 and j > 0: neighbour_count += cells[i - 1][j - 1] if i > 0: neighbour_count += cells[i - 1][j] if i > 0 and j < len(cells[i] ) - 1: neighbour_count += cells[i - 1][j + 1] if j > 0: neighbour_count += cells[i][j - 1] if j < len(cells[i] ) - 1: neighbour_count += cells[i][j + 1] if i < len(__a ) - 1 and j > 0: neighbour_count += cells[i + 1][j - 1] if i < len(__a ) - 1: neighbour_count += cells[i + 1][j] if i < len(__a ) - 1 and j < len(cells[i] ) - 1: neighbour_count += cells[i + 1][j + 1] # Rules of the game of life (excerpt from Wikipedia): # 1. Any live cell with two or three live neighbours survives. # 2. Any dead cell with three live neighbours becomes a live cell. # 3. All other live cells die in the next generation. # Similarly, all other dead cells stay dead. A__ = cells[i][j] == 1 if ( (alive and 2 <= neighbour_count <= 3) or not alive and neighbour_count == 3 ): next_generation_row.append(1 ) else: next_generation_row.append(0 ) next_generation.append(__a ) return next_generation def __lowerCamelCase ( __a :list[list[int]] , __a :int ) -> list[Image.Image]: """simple docstring""" A__ = [] for _ in range(__a ): # Create output image A__ = Image.new("""RGB""" , (len(cells[0] ), len(__a )) ) A__ = img.load() # Save cells to image for x in range(len(__a ) ): for y in range(len(cells[0] ) ): A__ = 2_5_5 - cells[y][x] * 2_5_5 A__ = (colour, colour, colour) # Save image images.append(__a ) A__ = new_generation(__a ) return images if __name__ == "__main__": A : str = generate_images(GLIDER, 1_6) images[0].save('''out.gif''', save_all=True, append_images=images[1:])
274
0
"""simple docstring""" def lowerCAmelCase__ ( UpperCamelCase__ ): '''simple docstring''' _a : int = [0] * len(__a ) for i in range(1 , len(__a ) ): # use last results for better performance - dynamic programming _a : int = prefix_result[i - 1] while j > 0 and input_string[i] != input_string[j]: _a : Optional[Any] = prefix_result[j - 1] if input_string[i] == input_string[j]: j += 1 _a : Optional[Any] = j return prefix_result def lowerCAmelCase__ ( UpperCamelCase__ ): '''simple docstring''' return max(prefix_function(__a ) ) if __name__ == "__main__": import doctest doctest.testmod()
294
from datetime import datetime import requests from bsa import BeautifulSoup if __name__ == "__main__": A : List[str] = input('''Enter image url: ''').strip() print(F'''Downloading image from {url} ...''') A : Any = BeautifulSoup(requests.get(url).content, '''html.parser''') # The image URL is in the content field of the first meta tag with property og:image A : List[Any] = soup.find('''meta''', {'''property''': '''og:image'''})['''content'''] A : Dict = requests.get(image_url).content A : Tuple = F'''{datetime.now():%Y-%m-%d_%H:%M:%S}.jpg''' with open(file_name, '''wb''') as fp: fp.write(image_data) print(F'''Done. Image saved to disk as {file_name}.''')
274
0
import importlib.metadata import operator import re import sys from typing import Optional from packaging import version __UpperCAmelCase = { '''<''': operator.lt, '''<=''': operator.le, '''==''': operator.eq, '''!=''': operator.ne, '''>=''': operator.ge, '''>''': operator.gt, } def lowercase__ ( __snake_case : Union[str, Any] , __snake_case : Optional[Any] , __snake_case : Dict , __snake_case : str , __snake_case : List[Any] , __snake_case : Dict ): '''simple docstring''' if got_ver is None or want_ver is None: raise ValueError( F"Unable to compare versions for {requirement}: need={want_ver} found={got_ver}. This is unusual. Consider" F" reinstalling {pkg}." ) if not ops[op](version.parse(__a ) , version.parse(__a ) ): raise ImportError( F"{requirement} is required for a normal functioning of this module, but found {pkg}=={got_ver}.{hint}" ) def lowercase__ ( __snake_case : str , __snake_case : Optional[str] = None ): '''simple docstring''' UpperCAmelCase_ : Dict = F"\n{hint}" if hint is not None else '' # non-versioned check if re.match(R'^[\w_\-\d]+$' , __a ): UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : str = requirement, None, None else: UpperCAmelCase_ : Union[str, Any] = re.findall(R'^([^!=<>\s]+)([\s!=<>]{1,2}.+)' , __a ) if not match: raise ValueError( 'requirement needs to be in the pip package format, .e.g., package_a==1.23, or package_b>=1.23, but' F" got {requirement}" ) UpperCAmelCase_ , UpperCAmelCase_ : Optional[int] = match[0] UpperCAmelCase_ : str = want_full.split(',' ) # there could be multiple requirements UpperCAmelCase_ : int = {} for w in want_range: UpperCAmelCase_ : Optional[Any] = re.findall(R'^([\s!=<>]{1,2})(.+)' , __a ) if not match: raise ValueError( 'requirement needs to be in the pip package format, .e.g., package_a==1.23, or package_b>=1.23,' F" but got {requirement}" ) UpperCAmelCase_ , UpperCAmelCase_ : Dict = match[0] UpperCAmelCase_ : str = want_ver if op not in ops: raise ValueError(F"{requirement}: need one of {list(ops.keys() )}, but got {op}" ) # special case if pkg == "python": UpperCAmelCase_ : List[Any] = '.'.join([str(__a ) for x in sys.version_info[:3]] ) for op, want_ver in wanted.items(): _compare_versions(__a , __a , __a , __a , __a , __a ) return # check if any version is installed try: UpperCAmelCase_ : Any = importlib.metadata.version(__a ) except importlib.metadata.PackageNotFoundError: raise importlib.metadata.PackageNotFoundError( F"The \'{requirement}\' distribution was not found and is required by this application. {hint}" ) # check that the right version is installed if version number or a range was provided if want_ver is not None: for op, want_ver in wanted.items(): _compare_versions(__a , __a , __a , __a , __a , __a ) def lowercase__ ( __snake_case : Optional[Any] ): '''simple docstring''' UpperCAmelCase_ : Union[str, Any] = 'Try: pip install transformers -U or pip install -e \'.[dev]\' if you\'re working with git main' return require_version(__a , __a )
29
# This code is adapted from OpenAI's release # https://github.com/openai/human-eval/blob/master/human_eval/execution.py import contextlib import faulthandler import io import multiprocessing import os import platform import signal import tempfile def __lowerCamelCase ( __a :List[str] , __a :List[Any] , __a :Union[str, Any] , __a :List[Any] ) -> Dict: """simple docstring""" A__ = multiprocessing.Manager() A__ = manager.list() A__ = multiprocessing.Process(target=__a , args=(check_program, result, timeout) ) p.start() p.join(timeout=timeout + 1 ) if p.is_alive(): p.kill() if not result: result.append("""timed out""" ) return { "task_id": task_id, "passed": result[0] == "passed", "result": result[0], "completion_id": completion_id, } def __lowerCamelCase ( __a :Optional[Any] , __a :Any , __a :List[Any] ) -> Union[str, Any]: """simple docstring""" with create_tempdir(): # These system calls are needed when cleaning up tempdir. import os import shutil A__ = shutil.rmtree A__ = os.rmdir A__ = os.chdir # Disable functionalities that can make destructive changes to the test. reliability_guard() # Run program. try: A__ = {} with swallow_io(): with time_limit(__a ): exec(__a , __a ) result.append("""passed""" ) except TimeoutException: result.append("""timed out""" ) except BaseException as e: result.append(F'failed: {e}' ) # Needed for cleaning up. A__ = rmtree A__ = rmdir A__ = chdir @contextlib.contextmanager def __lowerCamelCase ( __a :List[str] ) -> Dict: """simple docstring""" def signal_handler(__a :List[Any] , __a :Optional[Any] ): raise TimeoutException("""Timed out!""" ) signal.setitimer(signal.ITIMER_REAL , __a ) signal.signal(signal.SIGALRM , __a ) try: yield finally: signal.setitimer(signal.ITIMER_REAL , 0 ) @contextlib.contextmanager def __lowerCamelCase ( ) -> Union[str, Any]: """simple docstring""" A__ = WriteOnlyStringIO() with contextlib.redirect_stdout(__a ): with contextlib.redirect_stderr(__a ): with redirect_stdin(__a ): yield @contextlib.contextmanager def __lowerCamelCase ( ) -> Dict: """simple docstring""" with tempfile.TemporaryDirectory() as dirname: with chdir(__a ): yield dirname class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' pass class A (io.StringIO ): '''simple docstring''' def a_ ( self : Any , *__lowerCAmelCase : List[str] , **__lowerCAmelCase : str ) -> Dict: """simple docstring""" raise OSError def a_ ( self : Optional[Any] , *__lowerCAmelCase : Any , **__lowerCAmelCase : Optional[int] ) -> str: """simple docstring""" raise OSError def a_ ( self : Optional[Any] , *__lowerCAmelCase : Any , **__lowerCAmelCase : Any ) -> int: """simple docstring""" raise OSError def a_ ( self : str , *__lowerCAmelCase : Any , **__lowerCAmelCase : Union[str, Any] ) -> int: """simple docstring""" return False class A (contextlib._RedirectStream ): # type: ignore '''simple docstring''' __lowerCamelCase : Union[str, Any] = '''stdin''' @contextlib.contextmanager def __lowerCamelCase ( __a :Union[str, Any] ) -> List[str]: """simple docstring""" if root == ".": yield return A__ = os.getcwd() os.chdir(__a ) try: yield except BaseException as exc: raise exc finally: os.chdir(__a ) def __lowerCamelCase ( __a :Union[str, Any]=None ) -> Dict: """simple docstring""" if maximum_memory_bytes is not None: import resource resource.setrlimit(resource.RLIMIT_AS , (maximum_memory_bytes, maximum_memory_bytes) ) resource.setrlimit(resource.RLIMIT_DATA , (maximum_memory_bytes, maximum_memory_bytes) ) if not platform.uname().system == "Darwin": resource.setrlimit(resource.RLIMIT_STACK , (maximum_memory_bytes, maximum_memory_bytes) ) faulthandler.disable() import builtins A__ = None A__ = None import os A__ = """1""" A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None import shutil A__ = None A__ = None A__ = None import subprocess A__ = None # type: ignore A__ = None import sys A__ = None A__ = None A__ = None A__ = None A__ = None
274
0
from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...onnx.utils import compute_effective_axis_dimension from ...utils import logging if TYPE_CHECKING: from ...processing_utils import ProcessorMixin from ...utils import TensorType a_ = logging.get_logger(__name__) a_ = { '''microsoft/layoutlmv3-base''': '''https://huggingface.co/microsoft/layoutlmv3-base/resolve/main/config.json''', } class lowercase__ ( _UpperCAmelCase ): a_ ='''layoutlmv3''' def __init__( self , __UpperCAmelCase=50265 , __UpperCAmelCase=768 , __UpperCAmelCase=12 , __UpperCAmelCase=12 , __UpperCAmelCase=3072 , __UpperCAmelCase="gelu" , __UpperCAmelCase=0.1 , __UpperCAmelCase=0.1 , __UpperCAmelCase=512 , __UpperCAmelCase=2 , __UpperCAmelCase=0.02 , __UpperCAmelCase=1E-5 , __UpperCAmelCase=1 , __UpperCAmelCase=0 , __UpperCAmelCase=2 , __UpperCAmelCase=1024 , __UpperCAmelCase=128 , __UpperCAmelCase=128 , __UpperCAmelCase=True , __UpperCAmelCase=32 , __UpperCAmelCase=128 , __UpperCAmelCase=64 , __UpperCAmelCase=256 , __UpperCAmelCase=True , __UpperCAmelCase=True , __UpperCAmelCase=True , __UpperCAmelCase=224 , __UpperCAmelCase=3 , __UpperCAmelCase=16 , __UpperCAmelCase=None , **__UpperCAmelCase , )-> Dict: '''simple docstring''' super().__init__( vocab_size=__lowerCAmelCase , hidden_size=__lowerCAmelCase , num_hidden_layers=__lowerCAmelCase , num_attention_heads=__lowerCAmelCase , intermediate_size=__lowerCAmelCase , hidden_act=__lowerCAmelCase , hidden_dropout_prob=__lowerCAmelCase , attention_probs_dropout_prob=__lowerCAmelCase , max_position_embeddings=__lowerCAmelCase , type_vocab_size=__lowerCAmelCase , initializer_range=__lowerCAmelCase , layer_norm_eps=__lowerCAmelCase , pad_token_id=__lowerCAmelCase , bos_token_id=__lowerCAmelCase , eos_token_id=__lowerCAmelCase , **__lowerCAmelCase , ) lowerCAmelCase__ = max_ad_position_embeddings lowerCAmelCase__ = coordinate_size lowerCAmelCase__ = shape_size lowerCAmelCase__ = has_relative_attention_bias lowerCAmelCase__ = rel_pos_bins lowerCAmelCase__ = max_rel_pos lowerCAmelCase__ = has_spatial_attention_bias lowerCAmelCase__ = rel_ad_pos_bins lowerCAmelCase__ = max_rel_ad_pos lowerCAmelCase__ = text_embed lowerCAmelCase__ = visual_embed lowerCAmelCase__ = input_size lowerCAmelCase__ = num_channels lowerCAmelCase__ = patch_size lowerCAmelCase__ = classifier_dropout class lowercase__ ( _UpperCAmelCase ): a_ =version.parse("""1.12""" ) @property def UpperCAmelCase ( self )-> Mapping[str, Mapping[int, str]]: '''simple docstring''' if self.task in ["question-answering", "sequence-classification"]: return OrderedDict( [ ("input_ids", {0: "batch", 1: "sequence"}), ("attention_mask", {0: "batch", 1: "sequence"}), ("bbox", {0: "batch", 1: "sequence"}), ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}), ] ) else: return OrderedDict( [ ("input_ids", {0: "batch", 1: "sequence"}), ("bbox", {0: "batch", 1: "sequence"}), ("attention_mask", {0: "batch", 1: "sequence"}), ("pixel_values", {0: "batch", 1: "num_channels"}), ] ) @property def UpperCAmelCase ( self )-> float: '''simple docstring''' return 1E-5 @property def UpperCAmelCase ( self )-> int: '''simple docstring''' return 12 def UpperCAmelCase ( self , __UpperCAmelCase , __UpperCAmelCase = -1 , __UpperCAmelCase = -1 , __UpperCAmelCase = False , __UpperCAmelCase = None , __UpperCAmelCase = 3 , __UpperCAmelCase = 40 , __UpperCAmelCase = 40 , )-> Mapping[str, Any]: '''simple docstring''' setattr(processor.image_processor , "apply_ocr" , __lowerCAmelCase ) # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX lowerCAmelCase__ = compute_effective_axis_dimension( __lowerCAmelCase , fixed_dimension=OnnxConfig.default_fixed_batch , num_token_to_add=0 ) # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX lowerCAmelCase__ = processor.tokenizer.num_special_tokens_to_add(__lowerCAmelCase ) lowerCAmelCase__ = compute_effective_axis_dimension( __lowerCAmelCase , fixed_dimension=OnnxConfig.default_fixed_sequence , num_token_to_add=__lowerCAmelCase ) # Generate dummy inputs according to compute batch and sequence lowerCAmelCase__ = [[" ".join([processor.tokenizer.unk_token] ) * seq_length]] * batch_size # Generate dummy bounding boxes lowerCAmelCase__ = [[[48, 84, 73, 128]]] * batch_size # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX # batch_size = compute_effective_axis_dimension(batch_size, fixed_dimension=OnnxConfig.default_fixed_batch) lowerCAmelCase__ = self._generate_dummy_images(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) lowerCAmelCase__ = dict( processor( __lowerCAmelCase , text=__lowerCAmelCase , boxes=__lowerCAmelCase , return_tensors=__lowerCAmelCase , ) ) return inputs
340
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_albert import AlbertTokenizer else: A : Tuple = None A : Optional[Any] = logging.get_logger(__name__) A : Tuple = {'''vocab_file''': '''spiece.model''', '''tokenizer_file''': '''tokenizer.json'''} A : List[str] = { '''vocab_file''': { '''albert-base-v1''': '''https://huggingface.co/albert-base-v1/resolve/main/spiece.model''', '''albert-large-v1''': '''https://huggingface.co/albert-large-v1/resolve/main/spiece.model''', '''albert-xlarge-v1''': '''https://huggingface.co/albert-xlarge-v1/resolve/main/spiece.model''', '''albert-xxlarge-v1''': '''https://huggingface.co/albert-xxlarge-v1/resolve/main/spiece.model''', '''albert-base-v2''': '''https://huggingface.co/albert-base-v2/resolve/main/spiece.model''', '''albert-large-v2''': '''https://huggingface.co/albert-large-v2/resolve/main/spiece.model''', '''albert-xlarge-v2''': '''https://huggingface.co/albert-xlarge-v2/resolve/main/spiece.model''', '''albert-xxlarge-v2''': '''https://huggingface.co/albert-xxlarge-v2/resolve/main/spiece.model''', }, '''tokenizer_file''': { '''albert-base-v1''': '''https://huggingface.co/albert-base-v1/resolve/main/tokenizer.json''', '''albert-large-v1''': '''https://huggingface.co/albert-large-v1/resolve/main/tokenizer.json''', '''albert-xlarge-v1''': '''https://huggingface.co/albert-xlarge-v1/resolve/main/tokenizer.json''', '''albert-xxlarge-v1''': '''https://huggingface.co/albert-xxlarge-v1/resolve/main/tokenizer.json''', '''albert-base-v2''': '''https://huggingface.co/albert-base-v2/resolve/main/tokenizer.json''', '''albert-large-v2''': '''https://huggingface.co/albert-large-v2/resolve/main/tokenizer.json''', '''albert-xlarge-v2''': '''https://huggingface.co/albert-xlarge-v2/resolve/main/tokenizer.json''', '''albert-xxlarge-v2''': '''https://huggingface.co/albert-xxlarge-v2/resolve/main/tokenizer.json''', }, } A : List[str] = { '''albert-base-v1''': 5_1_2, '''albert-large-v1''': 5_1_2, '''albert-xlarge-v1''': 5_1_2, '''albert-xxlarge-v1''': 5_1_2, '''albert-base-v2''': 5_1_2, '''albert-large-v2''': 5_1_2, '''albert-xlarge-v2''': 5_1_2, '''albert-xxlarge-v2''': 5_1_2, } A : Optional[int] = '''▁''' class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : str = VOCAB_FILES_NAMES __lowerCamelCase : List[Any] = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase : Dict = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase : List[str] = AlbertTokenizer def __init__( self : Tuple , __lowerCAmelCase : Tuple=None , __lowerCAmelCase : List[str]=None , __lowerCAmelCase : Optional[Any]=True , __lowerCAmelCase : List[Any]=True , __lowerCAmelCase : str=False , __lowerCAmelCase : Union[str, Any]="[CLS]" , __lowerCAmelCase : int="[SEP]" , __lowerCAmelCase : Dict="<unk>" , __lowerCAmelCase : Dict="[SEP]" , __lowerCAmelCase : Union[str, Any]="<pad>" , __lowerCAmelCase : str="[CLS]" , __lowerCAmelCase : int="[MASK]" , **__lowerCAmelCase : Optional[Any] , ) -> Optional[Any]: """simple docstring""" A__ = ( AddedToken(__lowerCAmelCase , lstrip=__lowerCAmelCase , rstrip=__lowerCAmelCase , normalized=__lowerCAmelCase ) if isinstance(__lowerCAmelCase , __lowerCAmelCase ) else mask_token ) super().__init__( __lowerCAmelCase , tokenizer_file=__lowerCAmelCase , do_lower_case=__lowerCAmelCase , remove_space=__lowerCAmelCase , keep_accents=__lowerCAmelCase , bos_token=__lowerCAmelCase , eos_token=__lowerCAmelCase , unk_token=__lowerCAmelCase , sep_token=__lowerCAmelCase , pad_token=__lowerCAmelCase , cls_token=__lowerCAmelCase , mask_token=__lowerCAmelCase , **__lowerCAmelCase , ) A__ = do_lower_case A__ = remove_space A__ = keep_accents A__ = vocab_file A__ = False if not self.vocab_file else True def a_ ( self : List[Any] , __lowerCAmelCase : List[int] , __lowerCAmelCase : Optional[List[int]] = None ) -> List[int]: """simple docstring""" A__ = [self.sep_token_id] A__ = [self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def a_ ( self : Union[str, Any] , __lowerCAmelCase : List[int] , __lowerCAmelCase : Optional[List[int]] = None ) -> List[int]: """simple docstring""" A__ = [self.sep_token_id] A__ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def a_ ( self : Tuple , __lowerCAmelCase : str , __lowerCAmelCase : Optional[str] = None ) -> Tuple[str]: """simple docstring""" 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(__lowerCAmelCase ): logger.error(f'Vocabulary path ({save_directory}) should be a directory' ) return A__ = os.path.join( __lowerCAmelCase , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(__lowerCAmelCase ): copyfile(self.vocab_file , __lowerCAmelCase ) return (out_vocab_file,)
274
0
from typing import List, Optional, Union import torch from transformers import ( XLMRobertaTokenizer, ) from ...models import UNetaDConditionModel, VQModel from ...pipelines import DiffusionPipeline from ...pipelines.pipeline_utils import ImagePipelineOutput from ...schedulers import DDIMScheduler, DDPMScheduler from ...utils import ( is_accelerate_available, is_accelerate_version, logging, randn_tensor, replace_example_docstring, ) from .text_encoder import MultilingualCLIP _A = logging.get_logger(__name__) # pylint: disable=invalid-name _A = ''' Examples: ```py >>> from diffusers import KandinskyPipeline, KandinskyPriorPipeline >>> import torch >>> pipe_prior = KandinskyPriorPipeline.from_pretrained("kandinsky-community/Kandinsky-2-1-prior") >>> pipe_prior.to("cuda") >>> prompt = "red cat, 4k photo" >>> out = pipe_prior(prompt) >>> image_emb = out.image_embeds >>> negative_image_emb = out.negative_image_embeds >>> pipe = KandinskyPipeline.from_pretrained("kandinsky-community/kandinsky-2-1") >>> pipe.to("cuda") >>> image = pipe( ... prompt, ... image_embeds=image_emb, ... negative_image_embeds=negative_image_emb, ... height=768, ... width=768, ... num_inference_steps=100, ... ).images >>> image[0].save("cat.png") ``` ''' def lowerCamelCase__ ( a__ : str , a__ : Any , a__ : List[str]=8 ) -> Dict: UpperCamelCase_ = h // scale_factor**2 if h % scale_factor**2 != 0: new_h += 1 UpperCamelCase_ = w // scale_factor**2 if w % scale_factor**2 != 0: new_w += 1 return new_h * scale_factor, new_w * scale_factor class lowercase_ ( __SCREAMING_SNAKE_CASE ): def __init__( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , ): """simple docstring""" super().__init__() self.register_modules( text_encoder=__lowerCAmelCase , tokenizer=__lowerCAmelCase , unet=__lowerCAmelCase , scheduler=__lowerCAmelCase , movq=__lowerCAmelCase , ) UpperCamelCase_ = 2 ** (len(self.movq.config.block_out_channels ) - 1) def lowerCamelCase_ ( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): """simple docstring""" if latents is None: UpperCamelCase_ = randn_tensor(__lowerCAmelCase , generator=__lowerCAmelCase , device=__lowerCAmelCase , dtype=__lowerCAmelCase ) else: if latents.shape != shape: raise ValueError(f'''Unexpected latents shape, got {latents.shape}, expected {shape}''' ) UpperCamelCase_ = latents.to(__lowerCAmelCase ) UpperCamelCase_ = latents * scheduler.init_noise_sigma return latents def lowerCamelCase_ ( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase=None , ): """simple docstring""" UpperCamelCase_ = len(__lowerCAmelCase ) if isinstance(__lowerCAmelCase , __lowerCAmelCase ) else 1 # get prompt text embeddings UpperCamelCase_ = self.tokenizer( __lowerCAmelCase , padding="""max_length""" , truncation=__lowerCAmelCase , max_length=7_7 , return_attention_mask=__lowerCAmelCase , add_special_tokens=__lowerCAmelCase , return_tensors="""pt""" , ) UpperCamelCase_ = text_inputs.input_ids UpperCamelCase_ = self.tokenizer(__lowerCAmelCase , padding="""longest""" , return_tensors="""pt""" ).input_ids if untruncated_ids.shape[-1] >= text_input_ids.shape[-1] and not torch.equal(__lowerCAmelCase , __lowerCAmelCase ): UpperCamelCase_ = self.tokenizer.batch_decode(untruncated_ids[:, self.tokenizer.model_max_length - 1 : -1] ) 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}''' ) UpperCamelCase_ = text_input_ids.to(__lowerCAmelCase ) UpperCamelCase_ = text_inputs.attention_mask.to(__lowerCAmelCase ) UpperCamelCase_ , UpperCamelCase_ = self.text_encoder( input_ids=__lowerCAmelCase , attention_mask=__lowerCAmelCase ) UpperCamelCase_ = prompt_embeds.repeat_interleave(__lowerCAmelCase , dim=0 ) UpperCamelCase_ = text_encoder_hidden_states.repeat_interleave(__lowerCAmelCase , dim=0 ) UpperCamelCase_ = text_mask.repeat_interleave(__lowerCAmelCase , dim=0 ) if do_classifier_free_guidance: UpperCamelCase_ = 4_2 if negative_prompt is None: UpperCamelCase_ = [""""""] * batch_size elif type(__lowerCAmelCase ) is not type(__lowerCAmelCase ): raise TypeError( f'''`negative_prompt` should be the same type to `prompt`, but got {type(__lowerCAmelCase )} !=''' f''' {type(__lowerCAmelCase )}.''' ) elif isinstance(__lowerCAmelCase , __lowerCAmelCase ): UpperCamelCase_ = [negative_prompt] elif batch_size != len(__lowerCAmelCase ): raise ValueError( f'''`negative_prompt`: {negative_prompt} has batch size {len(__lowerCAmelCase )}, but `prompt`:''' f''' {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches''' """ the batch size of `prompt`.""" ) else: UpperCamelCase_ = negative_prompt UpperCamelCase_ = self.tokenizer( __lowerCAmelCase , padding="""max_length""" , max_length=7_7 , truncation=__lowerCAmelCase , return_attention_mask=__lowerCAmelCase , add_special_tokens=__lowerCAmelCase , return_tensors="""pt""" , ) UpperCamelCase_ = uncond_input.input_ids.to(__lowerCAmelCase ) UpperCamelCase_ = uncond_input.attention_mask.to(__lowerCAmelCase ) UpperCamelCase_ , UpperCamelCase_ = self.text_encoder( input_ids=__lowerCAmelCase , attention_mask=__lowerCAmelCase ) # duplicate unconditional embeddings for each generation per prompt, using mps friendly method UpperCamelCase_ = negative_prompt_embeds.shape[1] UpperCamelCase_ = negative_prompt_embeds.repeat(1 , __lowerCAmelCase ) UpperCamelCase_ = negative_prompt_embeds.view(batch_size * num_images_per_prompt , __lowerCAmelCase ) UpperCamelCase_ = uncond_text_encoder_hidden_states.shape[1] UpperCamelCase_ = uncond_text_encoder_hidden_states.repeat(1 , __lowerCAmelCase , 1 ) UpperCamelCase_ = uncond_text_encoder_hidden_states.view( batch_size * num_images_per_prompt , __lowerCAmelCase , -1 ) UpperCamelCase_ = uncond_text_mask.repeat_interleave(__lowerCAmelCase , dim=0 ) # done duplicates # 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 UpperCamelCase_ = torch.cat([negative_prompt_embeds, prompt_embeds] ) UpperCamelCase_ = torch.cat([uncond_text_encoder_hidden_states, text_encoder_hidden_states] ) UpperCamelCase_ = torch.cat([uncond_text_mask, text_mask] ) return prompt_embeds, text_encoder_hidden_states, text_mask def lowerCamelCase_ ( self , __UpperCamelCase=0 ): """simple docstring""" if is_accelerate_available(): from accelerate import cpu_offload else: raise ImportError("""Please install accelerate via `pip install accelerate`""" ) UpperCamelCase_ = torch.device(f'''cuda:{gpu_id}''' ) UpperCamelCase_ = [ self.unet, self.text_encoder, self.movq, ] for cpu_offloaded_model in models: if cpu_offloaded_model is not None: cpu_offload(__lowerCAmelCase , __lowerCAmelCase ) def lowerCamelCase_ ( self , __UpperCamelCase=0 ): """simple docstring""" 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.""" ) UpperCamelCase_ = torch.device(f'''cuda:{gpu_id}''' ) if self.device.type != "cpu": self.to("""cpu""" , silence_dtype_warnings=__lowerCAmelCase ) torch.cuda.empty_cache() # otherwise we don't see the memory savings (but they probably exist) UpperCamelCase_ = None for cpu_offloaded_model in [self.text_encoder, self.unet, self.movq]: UpperCamelCase_ , UpperCamelCase_ = cpu_offload_with_hook(__lowerCAmelCase , __lowerCAmelCase , prev_module_hook=__lowerCAmelCase ) if self.safety_checker is not None: UpperCamelCase_ , UpperCamelCase_ = cpu_offload_with_hook(self.safety_checker , __lowerCAmelCase , prev_module_hook=__lowerCAmelCase ) # We'll offload the last model manually. UpperCamelCase_ = hook @property # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device def lowerCamelCase_ ( self ): """simple docstring""" if not hasattr(self.unet , """_hf_hook""" ): return self.device for module in self.unet.modules(): if ( hasattr(__lowerCAmelCase , """_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(__lowerCAmelCase ) def __call__( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase = None , __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 , ): """simple docstring""" if isinstance(__lowerCAmelCase , __lowerCAmelCase ): UpperCamelCase_ = 1 elif isinstance(__lowerCAmelCase , __lowerCAmelCase ): UpperCamelCase_ = len(__lowerCAmelCase ) else: raise ValueError(f'''`prompt` has to be of type `str` or `list` but is {type(__lowerCAmelCase )}''' ) UpperCamelCase_ = self._execution_device UpperCamelCase_ = batch_size * num_images_per_prompt UpperCamelCase_ = guidance_scale > 1.0 UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ = self._encode_prompt( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) if isinstance(__lowerCAmelCase , __lowerCAmelCase ): UpperCamelCase_ = torch.cat(__lowerCAmelCase , dim=0 ) if isinstance(__lowerCAmelCase , __lowerCAmelCase ): UpperCamelCase_ = torch.cat(__lowerCAmelCase , dim=0 ) if do_classifier_free_guidance: UpperCamelCase_ = image_embeds.repeat_interleave(__lowerCAmelCase , dim=0 ) UpperCamelCase_ = negative_image_embeds.repeat_interleave(__lowerCAmelCase , dim=0 ) UpperCamelCase_ = torch.cat([negative_image_embeds, image_embeds] , dim=0 ).to( dtype=prompt_embeds.dtype , device=__lowerCAmelCase ) self.scheduler.set_timesteps(__lowerCAmelCase , device=__lowerCAmelCase ) UpperCamelCase_ = self.scheduler.timesteps UpperCamelCase_ = self.unet.config.in_channels UpperCamelCase_ , UpperCamelCase_ = get_new_h_w(__lowerCAmelCase , __lowerCAmelCase , self.movq_scale_factor ) # create initial latent UpperCamelCase_ = self.prepare_latents( (batch_size, num_channels_latents, height, width) , text_encoder_hidden_states.dtype , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , self.scheduler , ) for i, t in enumerate(self.progress_bar(__lowerCAmelCase ) ): # expand the latents if we are doing classifier free guidance UpperCamelCase_ = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents UpperCamelCase_ = {"""text_embeds""": prompt_embeds, """image_embeds""": image_embeds} UpperCamelCase_ = self.unet( sample=__lowerCAmelCase , timestep=__lowerCAmelCase , encoder_hidden_states=__lowerCAmelCase , added_cond_kwargs=__lowerCAmelCase , return_dict=__lowerCAmelCase , )[0] if do_classifier_free_guidance: UpperCamelCase_ , UpperCamelCase_ = noise_pred.split(latents.shape[1] , dim=1 ) UpperCamelCase_ , UpperCamelCase_ = noise_pred.chunk(2 ) UpperCamelCase_ , UpperCamelCase_ = variance_pred.chunk(2 ) UpperCamelCase_ = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) UpperCamelCase_ = 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"] ): UpperCamelCase_ , UpperCamelCase_ = noise_pred.split(latents.shape[1] , dim=1 ) # compute the previous noisy sample x_t -> x_t-1 UpperCamelCase_ = self.scheduler.step( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , generator=__lowerCAmelCase , ).prev_sample # post-processing UpperCamelCase_ = self.movq.decode(__lowerCAmelCase , force_not_quantize=__lowerCAmelCase )["""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"]: UpperCamelCase_ = image * 0.5 + 0.5 UpperCamelCase_ = image.clamp(0 , 1 ) UpperCamelCase_ = image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() if output_type == "pil": UpperCamelCase_ = self.numpy_to_pil(__lowerCAmelCase ) if not return_dict: return (image,) return ImagePipelineOutput(images=__lowerCAmelCase )
122
import dataclasses import json import warnings from dataclasses import dataclass, field from time import time from typing import List from ..utils import logging A : Dict = logging.get_logger(__name__) def __lowerCamelCase ( __a :int=None , __a :Optional[Any]=None ) -> int: """simple docstring""" return field(default_factory=lambda: default , metadata=__a ) @dataclass class A : '''simple docstring''' __lowerCamelCase : List[str] = list_field( default=[] , metadata={ '''help''': ( '''Model checkpoints to be provided to the AutoModel classes. Leave blank to benchmark the base version''' ''' of all available models''' ) } , ) __lowerCamelCase : List[int] = list_field( default=[8] , metadata={'''help''': '''List of batch sizes for which memory and time performance will be evaluated'''} ) __lowerCamelCase : List[int] = list_field( default=[8, 32, 128, 512] , metadata={'''help''': '''List of sequence lengths for which memory and time performance will be evaluated'''} , ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Whether to benchmark inference of model. Inference can be disabled via --no-inference.'''} , ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Whether to run on available cuda devices. Cuda can be disabled via --no-cuda.'''} , ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Whether to run on available tpu devices. TPU can be disabled via --no-tpu.'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Use FP16 to accelerate inference.'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Benchmark training of model'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Verbose memory tracing'''} ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Whether to perform speed measurements. Speed measurements can be disabled via --no-speed.'''} , ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={ '''help''': '''Whether to perform memory measurements. Memory measurements can be disabled via --no-memory''' } , ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Trace memory line by line'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Save result to a CSV file'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Save all print statements in a log file'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Whether to print environment information'''} ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={ '''help''': ( '''Whether to use multiprocessing for memory and speed measurement. It is highly recommended to use''' ''' multiprocessing for accurate CPU and GPU memory measurements. This option should only be disabled''' ''' for debugging / testing and on TPU.''' ) } , ) __lowerCamelCase : str = field( default=F'''inference_time_{round(time() )}.csv''' , metadata={'''help''': '''CSV filename used if saving time results to csv.'''} , ) __lowerCamelCase : str = field( default=F'''inference_memory_{round(time() )}.csv''' , metadata={'''help''': '''CSV filename used if saving memory results to csv.'''} , ) __lowerCamelCase : str = field( default=F'''train_time_{round(time() )}.csv''' , metadata={'''help''': '''CSV filename used if saving time results to csv for training.'''} , ) __lowerCamelCase : str = field( default=F'''train_memory_{round(time() )}.csv''' , metadata={'''help''': '''CSV filename used if saving memory results to csv for training.'''} , ) __lowerCamelCase : str = field( default=F'''env_info_{round(time() )}.csv''' , metadata={'''help''': '''CSV filename used if saving environment information.'''} , ) __lowerCamelCase : str = field( default=F'''log_{round(time() )}.csv''' , metadata={'''help''': '''Log filename used if print statements are saved in log.'''} , ) __lowerCamelCase : int = field(default=3 , metadata={'''help''': '''Times an experiment will be run.'''} ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={ '''help''': ( '''Instead of loading the model as defined in `config.architectures` if exists, just load the pretrain''' ''' model weights.''' ) } , ) def a_ ( self : Dict ) -> Union[str, Any]: """simple docstring""" warnings.warn( f'The class {self.__class__} is deprecated. Hugging Face Benchmarking utils' """ are deprecated in general and it is advised to use external Benchmarking libraries """ """ to benchmark Transformer models.""" , __lowerCAmelCase , ) def a_ ( self : Union[str, Any] ) -> List[str]: """simple docstring""" return json.dumps(dataclasses.asdict(self ) , indent=2 ) @property def a_ ( self : Tuple ) -> List[str]: """simple docstring""" if len(self.models ) <= 0: raise ValueError( """Please make sure you provide at least one model name / model identifier, *e.g.* `--models""" """ bert-base-cased` or `args.models = ['bert-base-cased'].""" ) return self.models @property def a_ ( self : Union[str, Any] ) -> Optional[Any]: """simple docstring""" if not self.multi_process: return False elif self.is_tpu: logger.info("""Multiprocessing is currently not possible on TPU.""" ) return False else: return True
274
0
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 math import ceil def __lowerCamelCase ( __a :int = 1_0_0_1 ) -> int: """simple docstring""" A__ = 1 for i in range(1 , int(ceil(n / 2.0 ) ) ): A__ = 2 * i + 1 A__ = 2 * i A__ = total + 4 * odd**2 - 6 * even return total if __name__ == "__main__": import sys if len(sys.argv) == 1: print(solution()) else: try: A : List[str] = int(sys.argv[1]) print(solution(n)) except ValueError: print('''Invalid entry - please enter a number''')
274
0
'''simple docstring''' import importlib import math import os from dataclasses import dataclass from enum import Enum from typing import Any, Dict, Optional, Tuple, Union import flax import jax.numpy as jnp from ..utils import BaseOutput _SCREAMING_SNAKE_CASE = '''scheduler_config.json''' class lowerCAmelCase_ ( __magic_name__ ): __lowerCamelCase : Union[str, Any] = 1 __lowerCamelCase : Optional[Any] = 2 __lowerCamelCase : Dict = 3 __lowerCamelCase : Any = 4 __lowerCamelCase : Union[str, Any] = 5 @dataclass class lowerCAmelCase_ ( __magic_name__ ): __lowerCamelCase : jnp.ndarray class lowerCAmelCase_ : __lowerCamelCase : List[str] = SCHEDULER_CONFIG_NAME __lowerCamelCase : Optional[int] = ['''dtype'''] __lowerCamelCase : List[Any] = [] __lowerCamelCase : Dict = True @classmethod def _snake_case ( cls , _lowerCAmelCase = None , _lowerCAmelCase = None , _lowerCAmelCase=False , **_lowerCAmelCase , ) -> Optional[int]: _lowerCAmelCase , _lowerCAmelCase = cls.load_config( pretrained_model_name_or_path=__lowerCAmelCase , subfolder=__lowerCAmelCase , return_unused_kwargs=__lowerCAmelCase , **__lowerCAmelCase , ) _lowerCAmelCase , _lowerCAmelCase = cls.from_config(__lowerCAmelCase , return_unused_kwargs=__lowerCAmelCase , **__lowerCAmelCase ) if hasattr(__lowerCAmelCase , "create_state" ) and getattr(__lowerCAmelCase , "has_state" , __lowerCAmelCase ): _lowerCAmelCase = scheduler.create_state() if return_unused_kwargs: return scheduler, state, unused_kwargs return scheduler, state def _snake_case ( self , _lowerCAmelCase , _lowerCAmelCase = False , **_lowerCAmelCase ) -> Any: self.save_config(save_directory=__lowerCAmelCase , push_to_hub=__lowerCAmelCase , **__lowerCAmelCase ) @property def _snake_case ( self ) -> Optional[int]: return self._get_compatibles() @classmethod def _snake_case ( cls ) -> Any: _lowerCAmelCase = list(set([cls.__name__] + cls._compatibles ) ) _lowerCAmelCase = importlib.import_module(__name__.split("." )[0] ) _lowerCAmelCase = [ getattr(__lowerCAmelCase , __lowerCAmelCase ) for c in compatible_classes_str if hasattr(__lowerCAmelCase , __lowerCAmelCase ) ] return compatible_classes def __a(SCREAMING_SNAKE_CASE_ : jnp.ndarray , SCREAMING_SNAKE_CASE_ : Tuple[int] ): '''simple docstring''' assert len(__a ) >= x.ndim return jnp.broadcast_to(x.reshape(x.shape + (1,) * (len(__a ) - x.ndim) ) , __a ) def __a(SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : Optional[Any]=0.999 , SCREAMING_SNAKE_CASE_ : Any=jnp.floataa ): '''simple docstring''' def alpha_bar(SCREAMING_SNAKE_CASE_ : Optional[Any] ): return math.cos((time_step + 0.008) / 1.008 * math.pi / 2 ) ** 2 _lowerCAmelCase = [] for i in range(__a ): _lowerCAmelCase = i / num_diffusion_timesteps _lowerCAmelCase = (i + 1) / num_diffusion_timesteps betas.append(min(1 - alpha_bar(__a ) / alpha_bar(__a ) , __a ) ) return jnp.array(__a , dtype=__a ) @flax.struct.dataclass class lowerCAmelCase_ : __lowerCamelCase : jnp.ndarray __lowerCamelCase : jnp.ndarray __lowerCamelCase : jnp.ndarray @classmethod def _snake_case ( cls , _lowerCAmelCase ) -> Dict: _lowerCAmelCase = scheduler.config if config.trained_betas is not None: _lowerCAmelCase = jnp.asarray(config.trained_betas , dtype=scheduler.dtype ) elif config.beta_schedule == "linear": _lowerCAmelCase = jnp.linspace(config.beta_start , config.beta_end , config.num_train_timesteps , dtype=scheduler.dtype ) elif config.beta_schedule == "scaled_linear": # this schedule is very specific to the latent diffusion model. _lowerCAmelCase = ( jnp.linspace( config.beta_start**0.5 , config.beta_end**0.5 , config.num_train_timesteps , dtype=scheduler.dtype ) ** 2 ) elif config.beta_schedule == "squaredcos_cap_v2": # Glide cosine schedule _lowerCAmelCase = betas_for_alpha_bar(config.num_train_timesteps , dtype=scheduler.dtype ) else: raise NotImplementedError( f'''beta_schedule {config.beta_schedule} is not implemented for scheduler {scheduler.__class__.__name__}''' ) _lowerCAmelCase = 1.0 - betas _lowerCAmelCase = jnp.cumprod(__lowerCAmelCase , axis=0 ) return cls( alphas=__lowerCAmelCase , betas=__lowerCAmelCase , alphas_cumprod=__lowerCAmelCase , ) def __a(SCREAMING_SNAKE_CASE_ : CommonSchedulerState , SCREAMING_SNAKE_CASE_ : jnp.ndarray , SCREAMING_SNAKE_CASE_ : jnp.ndarray , SCREAMING_SNAKE_CASE_ : jnp.ndarray ): '''simple docstring''' _lowerCAmelCase = state.alphas_cumprod _lowerCAmelCase = alphas_cumprod[timesteps] ** 0.5 _lowerCAmelCase = sqrt_alpha_prod.flatten() _lowerCAmelCase = broadcast_to_shape_from_left(__a , original_samples.shape ) _lowerCAmelCase = (1 - alphas_cumprod[timesteps]) ** 0.5 _lowerCAmelCase = sqrt_one_minus_alpha_prod.flatten() _lowerCAmelCase = broadcast_to_shape_from_left(__a , original_samples.shape ) return sqrt_alpha_prod, sqrt_one_minus_alpha_prod def __a(SCREAMING_SNAKE_CASE_ : CommonSchedulerState , SCREAMING_SNAKE_CASE_ : jnp.ndarray , SCREAMING_SNAKE_CASE_ : jnp.ndarray , SCREAMING_SNAKE_CASE_ : jnp.ndarray ): '''simple docstring''' _lowerCAmelCase , _lowerCAmelCase = get_sqrt_alpha_prod(__a , __a , __a , __a ) _lowerCAmelCase = sqrt_alpha_prod * original_samples + sqrt_one_minus_alpha_prod * noise return noisy_samples def __a(SCREAMING_SNAKE_CASE_ : CommonSchedulerState , SCREAMING_SNAKE_CASE_ : jnp.ndarray , SCREAMING_SNAKE_CASE_ : jnp.ndarray , SCREAMING_SNAKE_CASE_ : jnp.ndarray ): '''simple docstring''' _lowerCAmelCase , _lowerCAmelCase = get_sqrt_alpha_prod(__a , __a , __a , __a ) _lowerCAmelCase = sqrt_alpha_prod * noise - sqrt_one_minus_alpha_prod * sample return velocity
158
import argparse import csv import logging import os import random import numpy as np import torch from torch.utils.data import DataLoader, RandomSampler, SequentialSampler, TensorDataset from tqdm import tqdm, trange from transformers import ( CONFIG_NAME, WEIGHTS_NAME, AdamW, OpenAIGPTDoubleHeadsModel, OpenAIGPTTokenizer, get_linear_schedule_with_warmup, ) logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''', datefmt='''%m/%d/%Y %H:%M:%S''', level=logging.INFO ) A : Tuple = logging.getLogger(__name__) def __lowerCamelCase ( __a :Optional[int] , __a :List[str] ) -> Tuple: """simple docstring""" A__ = np.argmax(__a , axis=1 ) return np.sum(outputs == labels ) def __lowerCamelCase ( __a :Tuple ) -> Dict: """simple docstring""" with open(__a , encoding="""utf_8""" ) as f: A__ = csv.reader(__a ) A__ = [] next(__a ) # skip the first line for line in tqdm(__a ): output.append((""" """.join(line[1:5] ), line[5], line[6], int(line[-1] ) - 1) ) return output def __lowerCamelCase ( __a :Optional[int] , __a :List[Any] , __a :Dict , __a :Optional[Any] , __a :Optional[Any] , __a :int ) -> Union[str, Any]: """simple docstring""" A__ = [] for dataset in encoded_datasets: A__ = len(__a ) A__ = np.zeros((n_batch, 2, input_len) , dtype=np.intaa ) A__ = np.zeros((n_batch, 2) , dtype=np.intaa ) A__ = np.full((n_batch, 2, input_len) , fill_value=-1_0_0 , dtype=np.intaa ) A__ = np.zeros((n_batch,) , dtype=np.intaa ) for ( i, (story, conta, conta, mc_label), ) in enumerate(__a ): A__ = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] A__ = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] A__ = with_conta A__ = with_conta A__ = len(__a ) - 1 A__ = len(__a ) - 1 A__ = with_conta A__ = with_conta A__ = mc_label A__ = (input_ids, mc_token_ids, lm_labels, mc_labels) tensor_datasets.append(tuple(torch.tensor(__a ) for t in all_inputs ) ) return tensor_datasets def __lowerCamelCase ( ) -> Union[str, Any]: """simple docstring""" A__ = argparse.ArgumentParser() parser.add_argument("""--model_name""" , type=__a , default="""openai-gpt""" , help="""pretrained model name""" ) parser.add_argument("""--do_train""" , action="""store_true""" , help="""Whether to run training.""" ) parser.add_argument("""--do_eval""" , action="""store_true""" , help="""Whether to run eval on the dev set.""" ) parser.add_argument( """--output_dir""" , default=__a , type=__a , required=__a , help="""The output directory where the model predictions and checkpoints will be written.""" , ) parser.add_argument("""--train_dataset""" , type=__a , default="""""" ) parser.add_argument("""--eval_dataset""" , type=__a , default="""""" ) parser.add_argument("""--seed""" , type=__a , default=4_2 ) parser.add_argument("""--num_train_epochs""" , type=__a , default=3 ) parser.add_argument("""--train_batch_size""" , type=__a , default=8 ) parser.add_argument("""--eval_batch_size""" , type=__a , default=1_6 ) parser.add_argument("""--adam_epsilon""" , default=1E-8 , type=__a , help="""Epsilon for Adam optimizer.""" ) parser.add_argument("""--max_grad_norm""" , type=__a , default=1 ) parser.add_argument( """--max_steps""" , default=-1 , type=__a , help=( """If > 0: set total number of training steps to perform. Override num_train_epochs.""" ) , ) parser.add_argument( """--gradient_accumulation_steps""" , type=__a , default=1 , help="""Number of updates steps to accumulate before performing a backward/update pass.""" , ) parser.add_argument("""--learning_rate""" , type=__a , default=6.25E-5 ) parser.add_argument("""--warmup_steps""" , default=0 , type=__a , help="""Linear warmup over warmup_steps.""" ) parser.add_argument("""--lr_schedule""" , type=__a , default="""warmup_linear""" ) parser.add_argument("""--weight_decay""" , type=__a , default=0.01 ) parser.add_argument("""--lm_coef""" , type=__a , default=0.9 ) parser.add_argument("""--n_valid""" , type=__a , default=3_7_4 ) parser.add_argument("""--server_ip""" , type=__a , default="""""" , help="""Can be used for distant debugging.""" ) parser.add_argument("""--server_port""" , type=__a , default="""""" , help="""Can be used for distant debugging.""" ) A__ = parser.parse_args() print(__a ) if args.server_ip and args.server_port: # Distant debugging - see https://code.visualstudio.com/docs/python/debugging#_attach-to-a-local-script import ptvsd print("""Waiting for debugger attach""" ) ptvsd.enable_attach(address=(args.server_ip, args.server_port) , redirect_output=__a ) ptvsd.wait_for_attach() random.seed(args.seed ) np.random.seed(args.seed ) torch.manual_seed(args.seed ) torch.cuda.manual_seed_all(args.seed ) A__ = torch.device("""cuda""" if torch.cuda.is_available() else """cpu""" ) A__ = torch.cuda.device_count() logger.info("""device: {}, n_gpu {}""".format(__a , __a ) ) if not args.do_train and not args.do_eval: raise ValueError("""At least one of `do_train` or `do_eval` must be True.""" ) if not os.path.exists(args.output_dir ): os.makedirs(args.output_dir ) # Load tokenizer and model # This loading functions also add new tokens and embeddings called `special tokens` # These new embeddings will be fine-tuned on the RocStories dataset A__ = ["""_start_""", """_delimiter_""", """_classify_"""] A__ = OpenAIGPTTokenizer.from_pretrained(args.model_name ) tokenizer.add_tokens(__a ) A__ = tokenizer.convert_tokens_to_ids(__a ) A__ = OpenAIGPTDoubleHeadsModel.from_pretrained(args.model_name ) model.resize_token_embeddings(len(__a ) ) model.to(__a ) # Load and encode the datasets def tokenize_and_encode(__a :Tuple ): if isinstance(__a , __a ): return tokenizer.convert_tokens_to_ids(tokenizer.tokenize(__a ) ) elif isinstance(__a , __a ): return obj return [tokenize_and_encode(__a ) for o in obj] logger.info("""Encoding dataset...""" ) A__ = load_rocstories_dataset(args.train_dataset ) A__ = load_rocstories_dataset(args.eval_dataset ) A__ = (train_dataset, eval_dataset) A__ = tokenize_and_encode(__a ) # Compute the max input length for the Transformer A__ = model.config.n_positions // 2 - 2 A__ = max( len(story[:max_length] ) + max(len(conta[:max_length] ) , len(conta[:max_length] ) ) + 3 for dataset in encoded_datasets for story, conta, conta, _ in dataset ) A__ = min(__a , model.config.n_positions ) # Max size of input for the pre-trained model # Prepare inputs tensors and dataloaders A__ = pre_process_datasets(__a , __a , __a , *__a ) A__ , A__ = tensor_datasets[0], tensor_datasets[1] A__ = TensorDataset(*__a ) A__ = RandomSampler(__a ) A__ = DataLoader(__a , sampler=__a , batch_size=args.train_batch_size ) A__ = TensorDataset(*__a ) A__ = SequentialSampler(__a ) A__ = DataLoader(__a , sampler=__a , batch_size=args.eval_batch_size ) # Prepare optimizer if args.do_train: if args.max_steps > 0: A__ = args.max_steps A__ = args.max_steps // (len(__a ) // args.gradient_accumulation_steps) + 1 else: A__ = len(__a ) // args.gradient_accumulation_steps * args.num_train_epochs A__ = list(model.named_parameters() ) A__ = ["""bias""", """LayerNorm.bias""", """LayerNorm.weight"""] A__ = [ { """params""": [p for n, p in param_optimizer if not any(nd in n for nd in no_decay )], """weight_decay""": args.weight_decay, }, {"""params""": [p for n, p in param_optimizer if any(nd in n for nd in no_decay )], """weight_decay""": 0.0}, ] A__ = AdamW(__a , lr=args.learning_rate , eps=args.adam_epsilon ) A__ = get_linear_schedule_with_warmup( __a , num_warmup_steps=args.warmup_steps , num_training_steps=__a ) if args.do_train: A__ , A__ , A__ = 0, 0, None model.train() for _ in trange(int(args.num_train_epochs ) , desc="""Epoch""" ): A__ = 0 A__ = 0 A__ = tqdm(__a , desc="""Training""" ) for step, batch in enumerate(__a ): A__ = tuple(t.to(__a ) for t in batch ) A__ , A__ , A__ , A__ = batch A__ = model(__a , mc_token_ids=__a , lm_labels=__a , mc_labels=__a ) A__ = args.lm_coef * losses[0] + losses[1] loss.backward() optimizer.step() scheduler.step() optimizer.zero_grad() tr_loss += loss.item() A__ = ( loss.item() if exp_average_loss is None else 0.7 * exp_average_loss + 0.3 * loss.item() ) nb_tr_steps += 1 A__ = """Training loss: {:.2e} lr: {:.2e}""".format(__a , scheduler.get_lr()[0] ) # Save a trained model if args.do_train: # Save a trained model, configuration and tokenizer A__ = model.module if hasattr(__a , """module""" ) else model # Only save the model itself # If we save using the predefined names, we can load using `from_pretrained` A__ = os.path.join(args.output_dir , __a ) A__ = os.path.join(args.output_dir , __a ) torch.save(model_to_save.state_dict() , __a ) model_to_save.config.to_json_file(__a ) tokenizer.save_vocabulary(args.output_dir ) # Load a trained model and vocabulary that you have fine-tuned A__ = OpenAIGPTDoubleHeadsModel.from_pretrained(args.output_dir ) A__ = OpenAIGPTTokenizer.from_pretrained(args.output_dir ) model.to(__a ) if args.do_eval: model.eval() A__ , A__ = 0, 0 A__ , A__ = 0, 0 for batch in tqdm(__a , desc="""Evaluating""" ): A__ = tuple(t.to(__a ) for t in batch ) A__ , A__ , A__ , A__ = batch with torch.no_grad(): A__ , A__ , A__ , A__ = model( __a , mc_token_ids=__a , lm_labels=__a , mc_labels=__a ) A__ = mc_logits.detach().cpu().numpy() A__ = mc_labels.to("""cpu""" ).numpy() A__ = accuracy(__a , __a ) eval_loss += mc_loss.mean().item() eval_accuracy += tmp_eval_accuracy nb_eval_examples += input_ids.size(0 ) nb_eval_steps += 1 A__ = eval_loss / nb_eval_steps A__ = eval_accuracy / nb_eval_examples A__ = tr_loss / nb_tr_steps if args.do_train else None A__ = {"""eval_loss""": eval_loss, """eval_accuracy""": eval_accuracy, """train_loss""": train_loss} A__ = os.path.join(args.output_dir , """eval_results.txt""" ) with open(__a , """w""" ) as writer: logger.info("""***** Eval results *****""" ) for key in sorted(result.keys() ): logger.info(""" %s = %s""" , __a , str(result[key] ) ) writer.write("""%s = %s\n""" % (key, str(result[key] )) ) if __name__ == "__main__": main()
274
0
from typing import List import jiwer import jiwer.transforms as tr from packaging import version import datasets from datasets.config import PY_VERSION if PY_VERSION < version.parse("3.8"): import importlib_metadata else: import importlib.metadata as importlib_metadata _a = '''''' if version.parse(importlib_metadata.version("jiwer")) < version.parse("2.3.0"): class __A ( tr.AbstractTransform ): '''simple docstring''' def __init__( self , __lowerCAmelCase = " " ): '''simple docstring''' lowerCamelCase__ = sentence_delimiter def __lowerCamelCase ( self , __lowerCAmelCase ): '''simple docstring''' return list(__lowerCAmelCase ) def __lowerCamelCase ( self , __lowerCAmelCase ): '''simple docstring''' lowerCamelCase__ = [] for sent_idx, sentence in enumerate(__lowerCAmelCase ): chars.extend(self.process_string(__lowerCAmelCase ) ) if self.sentence_delimiter is not None and self.sentence_delimiter != "" and sent_idx < len(__lowerCAmelCase ) - 1: chars.append(self.sentence_delimiter ) return chars _a = tr.Compose( [tr.RemoveMultipleSpaces(), tr.Strip(), SentencesToListOfCharacters(SENTENCE_DELIMITER)] ) else: _a = tr.Compose( [ tr.RemoveMultipleSpaces(), tr.Strip(), tr.ReduceToSingleSentence(SENTENCE_DELIMITER), tr.ReduceToListOfListOfChars(), ] ) _a = '''\ @inproceedings{inproceedings, author = {Morris, Andrew and Maier, Viktoria and Green, Phil}, year = {2004}, month = {01}, pages = {}, title = {From WER and RIL to MER and WIL: improved evaluation measures for connected speech recognition.} } ''' _a = '''\ Character error rate (CER) is a common metric of the performance of an automatic speech recognition system. CER is similar to Word Error Rate (WER), but operates on character instead of word. Please refer to docs of WER for further information. Character error rate can be computed as: CER = (S + D + I) / N = (S + D + I) / (S + D + C) where S is the number of substitutions, D is the number of deletions, I is the number of insertions, C is the number of correct characters, N is the number of characters in the reference (N=S+D+C). CER\'s output is not always a number between 0 and 1, in particular when there is a high number of insertions. This value is often associated to the percentage of characters that were incorrectly predicted. The lower the value, the better the performance of the ASR system with a CER of 0 being a perfect score. ''' _a = ''' Computes CER score of transcribed segments against references. Args: references: list of references for each speech input. predictions: list of transcribtions to score. concatenate_texts: Whether or not to concatenate sentences before evaluation, set to True for more accurate result. Returns: (float): the character error rate Examples: >>> predictions = ["this is the prediction", "there is an other sample"] >>> references = ["this is the reference", "there is another one"] >>> cer = datasets.load_metric("cer") >>> cer_score = cer.compute(predictions=predictions, references=references) >>> print(cer_score) 0.34146341463414637 ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __A ( datasets.Metric ): '''simple docstring''' def __lowerCamelCase ( self ): '''simple docstring''' return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Value('''string''' , id='''sequence''' ), '''references''': datasets.Value('''string''' , id='''sequence''' ), } ) , codebase_urls=['''https://github.com/jitsi/jiwer/'''] , reference_urls=[ '''https://en.wikipedia.org/wiki/Word_error_rate''', '''https://sites.google.com/site/textdigitisation/qualitymeasures/computingerrorrates''', ] , ) def __lowerCamelCase ( self , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase=False ): '''simple docstring''' if concatenate_texts: return jiwer.compute_measures( __lowerCAmelCase , __lowerCAmelCase , truth_transform=__lowerCAmelCase , hypothesis_transform=__lowerCAmelCase , )["wer"] lowerCamelCase__ = 0 lowerCamelCase__ = 0 for prediction, reference in zip(__lowerCAmelCase , __lowerCAmelCase ): lowerCamelCase__ = jiwer.compute_measures( __lowerCAmelCase , __lowerCAmelCase , truth_transform=__lowerCAmelCase , hypothesis_transform=__lowerCAmelCase , ) incorrect += measures["substitutions"] + measures["deletions"] + measures["insertions"] total += measures["substitutions"] + measures["deletions"] + measures["hits"] return incorrect / total
209
import argparse from collections import defaultdict import yaml A : str = '''docs/source/en/_toctree.yml''' def __lowerCamelCase ( __a :str ) -> List[Any]: """simple docstring""" A__ = defaultdict(__a ) A__ = [] A__ = [] for doc in doc_list: if "local" in doc: counts[doc["local"]] += 1 if doc["title"].lower() == "overview": overview_doc.append({"""local""": doc["""local"""], """title""": doc["""title"""]} ) else: new_doc_list.append(__a ) A__ = new_doc_list A__ = [key for key, value in counts.items() if value > 1] A__ = [] for duplicate_key in duplicates: A__ = list({doc["""title"""] for doc in doc_list if doc["""local"""] == duplicate_key} ) if len(__a ) > 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 doc_list if """local""" not in counts or counts[doc["""local"""]] == 1] ) A__ = sorted(__a , key=lambda __a : s["title"].lower() ) # "overview" gets special treatment and is always first if len(__a ) > 1: raise ValueError("""{doc_list} has two 'overview' docs which is not allowed.""" ) overview_doc.extend(__a ) # Sort return overview_doc def __lowerCamelCase ( __a :Any=False ) -> List[str]: """simple docstring""" with open(__a , encoding="""utf-8""" ) as f: A__ = yaml.safe_load(f.read() ) # Get to the API doc A__ = 0 while content[api_idx]["title"] != "API": api_idx += 1 A__ = content[api_idx]["""sections"""] # Then to the model doc A__ = 0 while api_doc[scheduler_idx]["title"] != "Schedulers": scheduler_idx += 1 A__ = api_doc[scheduler_idx]["""sections"""] A__ = clean_doc_toc(__a ) A__ = False if new_scheduler_doc != scheduler_doc: A__ = True if overwrite: A__ = new_scheduler_doc if diff: if overwrite: A__ = api_doc with open(__a , """w""" , encoding="""utf-8""" ) as f: f.write(yaml.dump(__a , allow_unicode=__a ) ) else: raise ValueError( """The model doc part of the table of content is not properly sorted, run `make style` to fix this.""" ) def __lowerCamelCase ( __a :Optional[int]=False ) -> Dict: """simple docstring""" with open(__a , encoding="""utf-8""" ) as f: A__ = yaml.safe_load(f.read() ) # Get to the API doc A__ = 0 while content[api_idx]["title"] != "API": api_idx += 1 A__ = content[api_idx]["""sections"""] # Then to the model doc A__ = 0 while api_doc[pipeline_idx]["title"] != "Pipelines": pipeline_idx += 1 A__ = False A__ = api_doc[pipeline_idx]["""sections"""] A__ = [] # sort sub pipeline docs for pipeline_doc in pipeline_docs: if "section" in pipeline_doc: A__ = pipeline_doc["""section"""] A__ = clean_doc_toc(__a ) if overwrite: A__ = new_sub_pipeline_doc new_pipeline_docs.append(__a ) # sort overall pipeline doc A__ = clean_doc_toc(__a ) if new_pipeline_docs != pipeline_docs: A__ = True if overwrite: A__ = new_pipeline_docs if diff: if overwrite: A__ = api_doc with open(__a , """w""" , encoding="""utf-8""" ) as f: f.write(yaml.dump(__a , allow_unicode=__a ) ) 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__": A : Tuple = argparse.ArgumentParser() parser.add_argument('''--fix_and_overwrite''', action='''store_true''', help='''Whether to fix inconsistencies.''') A : Optional[Any] = parser.parse_args() check_scheduler_doc(args.fix_and_overwrite) check_pipeline_doc(args.fix_and_overwrite)
274
0
A : Tuple = [0, 2, 4, 6, 8] A : Dict = [1, 3, 5, 7, 9] def lowercase_ ( _A : int , _A : int , _A : list[int] , _A : int ): """simple docstring""" if remaining_length == 0: if digits[0] == 0 or digits[-1] == 0: return 0 for i in range(length // 2 - 1 , -1 , -1 ): remainder += digits[i] + digits[length - i - 1] if remainder % 2 == 0: return 0 remainder //= 10 return 1 if remaining_length == 1: if remainder % 2 == 0: return 0 lowerCamelCase__ : Any = 0 for digit in range(10 ): lowerCamelCase__ : int = digit result += reversible_numbers( 0 , (remainder + 2 * digit) // 10 , __a , __a ) return result lowerCamelCase__ : List[str] = 0 for digita in range(10 ): lowerCamelCase__ : Optional[Any] = digita if (remainder + digita) % 2 == 0: lowerCamelCase__ : List[str] = ODD_DIGITS else: lowerCamelCase__ : Union[str, Any] = EVEN_DIGITS for digita in other_parity_digits: lowerCamelCase__ : int = digita result += reversible_numbers( remaining_length - 2 , (remainder + digita + digita) // 10 , __a , __a , ) return result def lowercase_ ( _A : int = 9 ): """simple docstring""" lowerCamelCase__ : int = 0 for length in range(1 , max_power + 1 ): result += reversible_numbers(__a , 0 , [0] * length , __a ) return result if __name__ == "__main__": print(f'{solution() = }')
184
def __lowerCamelCase ( __a :str ) -> list: """simple docstring""" A__ = [0] * len(__a ) for i in range(1 , len(__a ) ): # use last results for better performance - dynamic programming A__ = prefix_result[i - 1] while j > 0 and input_string[i] != input_string[j]: A__ = prefix_result[j - 1] if input_string[i] == input_string[j]: j += 1 A__ = j return prefix_result def __lowerCamelCase ( __a :str ) -> int: """simple docstring""" return max(prefix_function(__a ) ) if __name__ == "__main__": import doctest doctest.testmod()
274
0
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_torch_available, ) lowercase = { '''configuration_vision_encoder_decoder''': ['''VisionEncoderDecoderConfig''', '''VisionEncoderDecoderOnnxConfig'''] } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase = ['''VisionEncoderDecoderModel'''] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase = ['''TFVisionEncoderDecoderModel'''] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase = ['''FlaxVisionEncoderDecoderModel'''] if TYPE_CHECKING: from .configuration_vision_encoder_decoder import VisionEncoderDecoderConfig, VisionEncoderDecoderOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vision_encoder_decoder import VisionEncoderDecoderModel try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_vision_encoder_decoder import TFVisionEncoderDecoderModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_vision_encoder_decoder import FlaxVisionEncoderDecoderModel else: import sys lowercase = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
178
def __lowerCamelCase ( __a :int = 1_0_0_0_0_0_0 ) -> int: """simple docstring""" A__ = limit + 1 A__ = [0] * limit for first_term in range(1 , __a ): for n in range(__a , __a , __a ): A__ = first_term + n / first_term if common_difference % 4: # d must be divisble by 4 continue else: common_difference /= 4 if ( first_term > common_difference and first_term < 4 * common_difference ): # since x,y,z are positive integers frequency[n] += 1 # so z>0 and a>d ,also 4d<a A__ = sum(1 for x in frequency[1:limit] if x == 1_0 ) return count if __name__ == "__main__": print(F'''{solution() = }''')
274
0
def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : List[Any] ): stooge(__a , 0 , len(__a ) - 1 ) return arr def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : Dict , SCREAMING_SNAKE_CASE__ : Optional[int] , SCREAMING_SNAKE_CASE__ : List[Any] ): if i >= h: return # If first element is smaller than the last then swap them if arr[i] > arr[h]: __UpperCamelCase , __UpperCamelCase =arr[h], arr[i] # If there are more than 2 elements in the array if h - i + 1 > 2: __UpperCamelCase =(int)((h - i + 1) / 3 ) # Recursively sort first 2/3 elements stooge(__a , __a , (h - t) ) # Recursively sort last 2/3 elements stooge(__a , i + t , (__a) ) # Recursively sort first 2/3 elements stooge(__a , __a , (h - t) ) if __name__ == "__main__": _A = input('Enter numbers separated by a comma:\n').strip() _A = [int(item) for item in user_input.split(',')] print(stooge_sort(unsorted))
62
class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' pass class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' pass class A : '''simple docstring''' def __init__( self : List[Any] ) -> str: """simple docstring""" A__ = [ [], [], [], ] def a_ ( self : Dict , __lowerCAmelCase : int , __lowerCAmelCase : int ) -> None: """simple docstring""" try: if len(self.queues[priority] ) >= 1_00: raise OverflowError("""Maximum queue size is 100""" ) self.queues[priority].append(__lowerCAmelCase ) except IndexError: raise ValueError("""Valid priorities are 0, 1, and 2""" ) def a_ ( self : Optional[Any] ) -> int: """simple docstring""" for queue in self.queues: if queue: return queue.pop(0 ) raise UnderFlowError("""All queues are empty""" ) def __str__( self : Tuple ) -> str: """simple docstring""" return "\n".join(f'Priority {i}: {q}' for i, q in enumerate(self.queues ) ) class A : '''simple docstring''' def __init__( self : int ) -> str: """simple docstring""" A__ = [] def a_ ( self : int , __lowerCAmelCase : int ) -> None: """simple docstring""" if len(self.queue ) == 1_00: raise OverFlowError("""Maximum queue size is 100""" ) self.queue.append(__lowerCAmelCase ) def a_ ( self : List[str] ) -> int: """simple docstring""" if not self.queue: raise UnderFlowError("""The queue is empty""" ) else: A__ = min(self.queue ) self.queue.remove(__lowerCAmelCase ) return data def __str__( self : List[Any] ) -> str: """simple docstring""" return str(self.queue ) def __lowerCamelCase ( ) -> Optional[Any]: """simple docstring""" A__ = FixedPriorityQueue() fpq.enqueue(0 , 1_0 ) fpq.enqueue(1 , 7_0 ) fpq.enqueue(0 , 1_0_0 ) fpq.enqueue(2 , 1 ) fpq.enqueue(2 , 5 ) fpq.enqueue(1 , 7 ) fpq.enqueue(2 , 4 ) fpq.enqueue(1 , 6_4 ) fpq.enqueue(0 , 1_2_8 ) print(__a ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(__a ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) def __lowerCamelCase ( ) -> int: """simple docstring""" A__ = ElementPriorityQueue() epq.enqueue(1_0 ) epq.enqueue(7_0 ) epq.enqueue(1_0_0 ) epq.enqueue(1 ) epq.enqueue(5 ) epq.enqueue(7 ) epq.enqueue(4 ) epq.enqueue(6_4 ) epq.enqueue(1_2_8 ) print(__a ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(__a ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) if __name__ == "__main__": fixed_priority_queue() element_priority_queue()
274
0
'''simple docstring''' from __future__ import annotations def __lowerCAmelCase ( snake_case__ , snake_case__ , snake_case__ ): if len(__a ) == 0: raise ValueError("find_max() arg is an empty sequence" ) if ( left >= len(__a ) or left < -len(__a ) or right >= len(__a ) or right < -len(__a ) ): raise IndexError("list index out of range" ) if left == right: return nums[left] __UpperCamelCase : List[str] = (left + right) >> 1 # the middle __UpperCamelCase : Any = find_max(__a , __a , __a ) # find max in range[left, mid] __UpperCamelCase : Dict = find_max(__a , mid + 1 , __a ) # find max in range[mid + 1, right] return left_max if left_max >= right_max else right_max if __name__ == "__main__": import doctest doctest.testmod(verbose=True)
298
import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import CLIPTokenizer, CLIPTokenizerFast from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES from transformers.testing_utils import require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available if is_vision_available(): from PIL import Image from transformers import CLIPImageProcessor, CLIPProcessor @require_vision class A (unittest.TestCase ): '''simple docstring''' def a_ ( self : Union[str, Any] ) -> Dict: """simple docstring""" A__ = tempfile.mkdtemp() # fmt: off A__ = ["""l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """lo""", """l</w>""", """w</w>""", """r</w>""", """t</w>""", """low</w>""", """er</w>""", """lowest</w>""", """newer</w>""", """wider""", """<unk>""", """<|startoftext|>""", """<|endoftext|>"""] # fmt: on A__ = dict(zip(__lowerCAmelCase , range(len(__lowerCAmelCase ) ) ) ) A__ = ["""#version: 0.2""", """l o""", """lo w</w>""", """e r</w>""", """"""] A__ = {"""unk_token""": """<unk>"""} A__ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""vocab_file"""] ) A__ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""merges_file"""] ) with open(self.vocab_file , """w""" , encoding="""utf-8""" ) as fp: fp.write(json.dumps(__lowerCAmelCase ) + """\n""" ) with open(self.merges_file , """w""" , encoding="""utf-8""" ) as fp: fp.write("""\n""".join(__lowerCAmelCase ) ) A__ = { """do_resize""": True, """size""": 20, """do_center_crop""": True, """crop_size""": 18, """do_normalize""": True, """image_mean""": [0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3], """image_std""": [0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1], } A__ = os.path.join(self.tmpdirname , __lowerCAmelCase ) with open(self.image_processor_file , """w""" , encoding="""utf-8""" ) as fp: json.dump(__lowerCAmelCase , __lowerCAmelCase ) def a_ ( self : Tuple , **__lowerCAmelCase : Dict ) -> str: """simple docstring""" return CLIPTokenizer.from_pretrained(self.tmpdirname , **__lowerCAmelCase ) def a_ ( self : Union[str, Any] , **__lowerCAmelCase : Dict ) -> List[str]: """simple docstring""" return CLIPTokenizerFast.from_pretrained(self.tmpdirname , **__lowerCAmelCase ) def a_ ( self : List[str] , **__lowerCAmelCase : Optional[Any] ) -> Dict: """simple docstring""" return CLIPImageProcessor.from_pretrained(self.tmpdirname , **__lowerCAmelCase ) def a_ ( self : str ) -> Dict: """simple docstring""" shutil.rmtree(self.tmpdirname ) def a_ ( self : str ) -> Any: """simple docstring""" A__ = [np.random.randint(2_55 , size=(3, 30, 4_00) , dtype=np.uinta )] A__ = [Image.fromarray(np.moveaxis(__lowerCAmelCase , 0 , -1 ) ) for x in image_inputs] return image_inputs def a_ ( self : Optional[int] ) -> Tuple: """simple docstring""" A__ = self.get_tokenizer() A__ = self.get_rust_tokenizer() A__ = self.get_image_processor() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) processor_slow.save_pretrained(self.tmpdirname ) A__ = CLIPProcessor.from_pretrained(self.tmpdirname , use_fast=__lowerCAmelCase ) A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) processor_fast.save_pretrained(self.tmpdirname ) A__ = CLIPProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor_slow.tokenizer.get_vocab() , tokenizer_slow.get_vocab() ) self.assertEqual(processor_fast.tokenizer.get_vocab() , tokenizer_fast.get_vocab() ) self.assertEqual(tokenizer_slow.get_vocab() , tokenizer_fast.get_vocab() ) self.assertIsInstance(processor_slow.tokenizer , __lowerCAmelCase ) self.assertIsInstance(processor_fast.tokenizer , __lowerCAmelCase ) self.assertEqual(processor_slow.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertEqual(processor_fast.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertIsInstance(processor_slow.image_processor , __lowerCAmelCase ) self.assertIsInstance(processor_fast.image_processor , __lowerCAmelCase ) def a_ ( self : Optional[Any] ) -> Union[str, Any]: """simple docstring""" A__ = CLIPProcessor(tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) A__ = self.get_tokenizer(bos_token="""(BOS)""" , eos_token="""(EOS)""" ) A__ = self.get_image_processor(do_normalize=__lowerCAmelCase , padding_value=1.0 ) A__ = CLIPProcessor.from_pretrained( self.tmpdirname , bos_token="""(BOS)""" , eos_token="""(EOS)""" , do_normalize=__lowerCAmelCase , padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , __lowerCAmelCase ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , __lowerCAmelCase ) def a_ ( self : List[Any] ) -> Dict: """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) A__ = self.prepare_image_inputs() A__ = image_processor(__lowerCAmelCase , return_tensors="""np""" ) A__ = processor(images=__lowerCAmelCase , return_tensors="""np""" ) for key in input_image_proc.keys(): self.assertAlmostEqual(input_image_proc[key].sum() , input_processor[key].sum() , delta=1e-2 ) def a_ ( self : Optional[Any] ) -> Any: """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) A__ = """lower newer""" A__ = processor(text=__lowerCAmelCase ) A__ = tokenizer(__lowerCAmelCase ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def a_ ( self : Union[str, Any] ) -> Dict: """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) A__ = """lower newer""" A__ = self.prepare_image_inputs() A__ = processor(text=__lowerCAmelCase , images=__lowerCAmelCase ) self.assertListEqual(list(inputs.keys() ) , ["""input_ids""", """attention_mask""", """pixel_values"""] ) # test if it raises when no input is passed with pytest.raises(__lowerCAmelCase ): processor() def a_ ( self : Tuple ) -> str: """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) A__ = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] A__ = processor.batch_decode(__lowerCAmelCase ) A__ = tokenizer.batch_decode(__lowerCAmelCase ) self.assertListEqual(__lowerCAmelCase , __lowerCAmelCase ) def a_ ( self : Optional[int] ) -> str: """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) A__ = """lower newer""" A__ = self.prepare_image_inputs() A__ = processor(text=__lowerCAmelCase , images=__lowerCAmelCase ) self.assertListEqual(list(inputs.keys() ) , processor.model_input_names )
274
0
"""simple docstring""" import math def lowerCAmelCase__ ( ): '''simple docstring''' _a : Any = input("""Enter message: """ ) _a : Any = int(input(F"""Enter key [2-{len(__a ) - 1}]: """ ) ) _a : Dict = input("""Encryption/Decryption [e/d]: """ ) if mode.lower().startswith("""e""" ): _a : Optional[int] = encrypt_message(__a , __a ) elif mode.lower().startswith("""d""" ): _a : Optional[Any] = decrypt_message(__a , __a ) # Append pipe symbol (vertical bar) to identify spaces at the end. print(F"""Output:\n{text + '|'}""" ) def lowerCAmelCase__ ( UpperCamelCase__ , UpperCamelCase__ ): '''simple docstring''' _a : Optional[Any] = [""""""] * key for col in range(__a ): _a : List[str] = col while pointer < len(__a ): cipher_text[col] += message[pointer] pointer += key return "".join(__a ) def lowerCAmelCase__ ( UpperCamelCase__ , UpperCamelCase__ ): '''simple docstring''' _a : List[Any] = math.ceil(len(__a ) / key ) _a : Tuple = key _a : Dict = (num_cols * num_rows) - len(__a ) _a : Any = [""""""] * num_cols _a : Dict = 0 _a : str = 0 for symbol in message: plain_text[col] += symbol col += 1 if ( (col == num_cols) or (col == num_cols - 1) and (row >= num_rows - num_shaded_boxes) ): _a : Optional[int] = 0 row += 1 return "".join(__a ) if __name__ == "__main__": import doctest doctest.testmod() main()
294
from multiprocessing import Lock, Pipe, Process # lock used to ensure that two processes do not access a pipe at the same time A : Dict = Lock() def __lowerCamelCase ( __a :Dict , __a :List[str] , __a :Optional[int] , __a :Optional[int] , __a :Optional[Any] , __a :Optional[int] , __a :int ) -> Dict: """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 , 1_0 ): 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(__a ) process_lock.release() # receive your right neighbor's value process_lock.acquire() A__ = rr_cv[0].recv() process_lock.release() # take the lower value since you are on the left A__ = min(__a , __a ) 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(__a ) process_lock.release() # receive your left neighbor's value process_lock.acquire() A__ = lr_cv[0].recv() process_lock.release() # take the higher value since you are on the right A__ = max(__a , __a ) # after all swaps are performed, send the values back to main result_pipe[1].send(__a ) def __lowerCamelCase ( __a :List[str] ) -> int: """simple docstring""" A__ = [] A__ = [] # 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 A__ = Pipe() A__ = Pipe() process_array_.append( Process( target=__a , args=(0, arr[0], None, temp_rs, None, temp_rr, result_pipe[0]) , ) ) A__ = temp_rs A__ = temp_rr for i in range(1 , len(__a ) - 1 ): A__ = Pipe() A__ = Pipe() process_array_.append( Process( target=__a , args=(i, arr[i], temp_ls, temp_rs, temp_lr, temp_rr, result_pipe[i]) , ) ) A__ = temp_rs A__ = temp_rr process_array_.append( Process( target=__a , args=( len(__a ) - 1, arr[len(__a ) - 1], temp_ls, None, temp_lr, None, result_pipe[len(__a ) - 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(__a ) ): A__ = result_pipe[p][0].recv() process_array_[p].join() return arr def __lowerCamelCase ( ) -> str: """simple docstring""" A__ = list(range(1_0 , 0 , -1 ) ) print("""Initial List""" ) print(*__a ) A__ = odd_even_transposition(__a ) print("""Sorted List\n""" ) print(*__a ) if __name__ == "__main__": main()
274
0
from itertools import permutations def lowercase__ ( __snake_case : tuple ): '''simple docstring''' if num[3] % 2 != 0: return False if (num[2] + num[3] + num[4]) % 3 != 0: return False if num[5] % 5 != 0: return False UpperCAmelCase_ : Any = [7, 11, 13, 17] for i, test in enumerate(__a ): if (num[i + 4] * 100 + num[i + 5] * 10 + num[i + 6]) % test != 0: return False return True def lowercase__ ( __snake_case : int = 10 ): '''simple docstring''' return sum( int(''.join(map(__a , __a ) ) ) for num in permutations(range(__a ) ) if is_substring_divisible(__a ) ) if __name__ == "__main__": print(F'{solution() = }')
29
import argparse from argparse import Namespace import torch from torch import nn from transformers import XGLMConfig, XGLMForCausalLM def __lowerCamelCase ( __a :Dict ) -> Any: """simple docstring""" A__ = [ """decoder.version""", """decoder.output_projection.weight""", """_float_tensor""", """decoder.embed_positions._float_tensor""", ] for k in ignore_keys: state_dict.pop(__a , __a ) def __lowerCamelCase ( __a :str ) -> Union[str, Any]: """simple docstring""" A__ , A__ = emb.weight.shape A__ = nn.Linear(__a , __a , bias=__a ) A__ = emb.weight.data return lin_layer def __lowerCamelCase ( __a :str ) -> List[str]: """simple docstring""" A__ = torch.load(__a , map_location="""cpu""" ) A__ = Namespace(**checkpoint["""cfg"""]["""model"""] ) A__ = checkpoint["""model"""] remove_ignore_keys_(__a ) A__ = state_dict["""decoder.embed_tokens.weight"""].shape[0] A__ = {key.replace("""decoder""" , """model""" ): val for key, val in state_dict.items()} A__ = XGLMConfig( vocab_size=__a , max_position_embeddings=args.max_target_positions , num_layers=args.decoder_layers , attention_heads=args.decoder_attention_heads , ffn_dim=args.decoder_ffn_embed_dim , d_model=args.decoder_embed_dim , layerdrop=args.decoder_layerdrop , dropout=args.dropout , attention_dropout=args.attention_dropout , activation_dropout=args.activation_dropout , activation_function="""gelu""" , scale_embedding=not args.no_scale_embedding , tie_word_embeddings=args.share_decoder_input_output_embed , ) A__ = XGLMForCausalLM(__a ) A__ = model.load_state_dict(__a , strict=__a ) print(__a ) A__ = make_linear_from_emb(model.model.embed_tokens ) return model if __name__ == "__main__": A : int = argparse.ArgumentParser() # Required parameters parser.add_argument('''fairseq_path''', type=str, help='''path to a model.pt on local filesystem.''') parser.add_argument('''pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''') A : str = parser.parse_args() A : str = convert_fairseq_xglm_checkpoint_from_disk(args.fairseq_path) model.save_pretrained(args.pytorch_dump_folder_path)
274
0
import json import os import torch from diffusers import UNetaDModel os.makedirs('''hub/hopper-medium-v2/unet/hor32''', exist_ok=True) os.makedirs('''hub/hopper-medium-v2/unet/hor128''', exist_ok=True) os.makedirs('''hub/hopper-medium-v2/value_function''', exist_ok=True) def _a ( UpperCamelCase_ : Union[str, Any] ) -> Any: """simple docstring""" if hor == 128: lowerCAmelCase__ = ("DownResnetBlock1D", "DownResnetBlock1D", "DownResnetBlock1D") lowerCAmelCase__ = (32, 128, 256) lowerCAmelCase__ = ("UpResnetBlock1D", "UpResnetBlock1D") elif hor == 32: lowerCAmelCase__ = ("DownResnetBlock1D", "DownResnetBlock1D", "DownResnetBlock1D", "DownResnetBlock1D") lowerCAmelCase__ = (32, 64, 128, 256) lowerCAmelCase__ = ("UpResnetBlock1D", "UpResnetBlock1D", "UpResnetBlock1D") lowerCAmelCase__ = torch.load(F"/Users/bglickenhaus/Documents/diffuser/temporal_unet-hopper-mediumv2-hor{hor}.torch" ) lowerCAmelCase__ = model.state_dict() lowerCAmelCase__ = { "down_block_types": down_block_types, "block_out_channels": block_out_channels, "up_block_types": up_block_types, "layers_per_block": 1, "use_timestep_embedding": True, "out_block_type": "OutConv1DBlock", "norm_num_groups": 8, "downsample_each_block": False, "in_channels": 14, "out_channels": 14, "extra_in_channels": 0, "time_embedding_type": "positional", "flip_sin_to_cos": False, "freq_shift": 1, "sample_size": 65_536, "mid_block_type": "MidResTemporalBlock1D", "act_fn": "mish", } lowerCAmelCase__ = UNetaDModel(**__a ) print(F"length of state dict: {len(state_dict.keys() )}" ) print(F"length of value function dict: {len(hf_value_function.state_dict().keys() )}" ) lowerCAmelCase__ = dict(zip(model.state_dict().keys() , hf_value_function.state_dict().keys() ) ) for k, v in mapping.items(): lowerCAmelCase__ = state_dict.pop(__a ) hf_value_function.load_state_dict(__a ) torch.save(hf_value_function.state_dict() , F"hub/hopper-medium-v2/unet/hor{hor}/diffusion_pytorch_model.bin" ) with open(F"hub/hopper-medium-v2/unet/hor{hor}/config.json" , "w" ) as f: json.dump(__a , __a ) def _a ( ) -> List[str]: """simple docstring""" lowerCAmelCase__ = { "in_channels": 14, "down_block_types": ("DownResnetBlock1D", "DownResnetBlock1D", "DownResnetBlock1D", "DownResnetBlock1D"), "up_block_types": (), "out_block_type": "ValueFunction", "mid_block_type": "ValueFunctionMidBlock1D", "block_out_channels": (32, 64, 128, 256), "layers_per_block": 1, "downsample_each_block": True, "sample_size": 65_536, "out_channels": 14, "extra_in_channels": 0, "time_embedding_type": "positional", "use_timestep_embedding": True, "flip_sin_to_cos": False, "freq_shift": 1, "norm_num_groups": 8, "act_fn": "mish", } lowerCAmelCase__ = torch.load("/Users/bglickenhaus/Documents/diffuser/value_function-hopper-mediumv2-hor32.torch" ) lowerCAmelCase__ = model lowerCAmelCase__ = UNetaDModel(**__a ) print(F"length of state dict: {len(state_dict.keys() )}" ) print(F"length of value function dict: {len(hf_value_function.state_dict().keys() )}" ) lowerCAmelCase__ = dict(zip(state_dict.keys() , hf_value_function.state_dict().keys() ) ) for k, v in mapping.items(): lowerCAmelCase__ = state_dict.pop(__a ) hf_value_function.load_state_dict(__a ) torch.save(hf_value_function.state_dict() , "hub/hopper-medium-v2/value_function/diffusion_pytorch_model.bin" ) with open("hub/hopper-medium-v2/value_function/config.json" , "w" ) as f: json.dump(__a , __a ) if __name__ == "__main__": unet(32) # unet(128) value_function()
340
import unittest import numpy as np from transformers import AlbertConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax.numpy as jnp from transformers.models.albert.modeling_flax_albert import ( FlaxAlbertForMaskedLM, FlaxAlbertForMultipleChoice, FlaxAlbertForPreTraining, FlaxAlbertForQuestionAnswering, FlaxAlbertForSequenceClassification, FlaxAlbertForTokenClassification, FlaxAlbertModel, ) class A (unittest.TestCase ): '''simple docstring''' def __init__( self : List[str] , __lowerCAmelCase : int , __lowerCAmelCase : List[str]=13 , __lowerCAmelCase : int=7 , __lowerCAmelCase : Tuple=True , __lowerCAmelCase : int=True , __lowerCAmelCase : Dict=True , __lowerCAmelCase : Union[str, Any]=True , __lowerCAmelCase : List[Any]=99 , __lowerCAmelCase : Optional[Any]=32 , __lowerCAmelCase : Optional[Any]=5 , __lowerCAmelCase : Tuple=4 , __lowerCAmelCase : Any=37 , __lowerCAmelCase : str="gelu" , __lowerCAmelCase : Optional[Any]=0.1 , __lowerCAmelCase : Union[str, Any]=0.1 , __lowerCAmelCase : List[str]=5_12 , __lowerCAmelCase : int=16 , __lowerCAmelCase : Optional[int]=2 , __lowerCAmelCase : List[Any]=0.0_2 , __lowerCAmelCase : Tuple=4 , ) -> Dict: """simple docstring""" A__ = parent A__ = batch_size A__ = seq_length A__ = is_training A__ = use_attention_mask A__ = use_token_type_ids A__ = use_labels A__ = vocab_size A__ = hidden_size A__ = num_hidden_layers A__ = num_attention_heads A__ = intermediate_size A__ = hidden_act A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = max_position_embeddings A__ = type_vocab_size A__ = type_sequence_label_size A__ = initializer_range A__ = num_choices def a_ ( self : Any ) -> str: """simple docstring""" A__ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) A__ = None if self.use_attention_mask: A__ = random_attention_mask([self.batch_size, self.seq_length] ) A__ = None if self.use_token_type_ids: A__ = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) A__ = AlbertConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=__lowerCAmelCase , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def a_ ( self : Optional[int] ) -> Optional[int]: """simple docstring""" A__ = self.prepare_config_and_inputs() A__ , A__ , A__ , A__ = config_and_inputs A__ = {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": attention_mask} return config, inputs_dict @require_flax class A (SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : str = ( ( FlaxAlbertModel, FlaxAlbertForPreTraining, FlaxAlbertForMaskedLM, FlaxAlbertForMultipleChoice, FlaxAlbertForQuestionAnswering, FlaxAlbertForSequenceClassification, FlaxAlbertForTokenClassification, FlaxAlbertForQuestionAnswering, ) if is_flax_available() else () ) def a_ ( self : str ) -> Optional[int]: """simple docstring""" A__ = FlaxAlbertModelTester(self ) @slow def a_ ( self : int ) -> Tuple: """simple docstring""" for model_class_name in self.all_model_classes: A__ = model_class_name.from_pretrained("""albert-base-v2""" ) A__ = model(np.ones((1, 1) ) ) self.assertIsNotNone(__lowerCAmelCase ) @require_flax class A (unittest.TestCase ): '''simple docstring''' @slow def a_ ( self : Dict ) -> List[Any]: """simple docstring""" A__ = FlaxAlbertModel.from_pretrained("""albert-base-v2""" ) A__ = np.array([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]] ) A__ = np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) A__ = model(__lowerCAmelCase , attention_mask=__lowerCAmelCase )[0] A__ = (1, 11, 7_68) self.assertEqual(output.shape , __lowerCAmelCase ) A__ = np.array( [[[-0.6_5_1_3, 1.5_0_3_5, -0.2_7_6_6], [-0.6_5_1_5, 1.5_0_4_6, -0.2_7_8_0], [-0.6_5_1_2, 1.5_0_4_9, -0.2_7_8_4]]] ) self.assertTrue(jnp.allclose(output[:, 1:4, 1:4] , __lowerCAmelCase , atol=1e-4 ) )
274
0
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_fnet import FNetTokenizer else: _A = None _A = logging.get_logger(__name__) _A = {'''vocab_file''': '''spiece.model''', '''tokenizer_file''': '''tokenizer.json'''} _A = { '''vocab_file''': { '''google/fnet-base''': '''https://huggingface.co/google/fnet-base/resolve/main/spiece.model''', '''google/fnet-large''': '''https://huggingface.co/google/fnet-large/resolve/main/spiece.model''', }, '''tokenizer_file''': { '''google/fnet-base''': '''https://huggingface.co/google/fnet-base/resolve/main/tokenizer.json''', '''google/fnet-large''': '''https://huggingface.co/google/fnet-large/resolve/main/tokenizer.json''', }, } _A = { '''google/fnet-base''': 512, '''google/fnet-large''': 512, } _A = '''▁''' class lowercase_ ( __SCREAMING_SNAKE_CASE ): A__ : Optional[Any] = VOCAB_FILES_NAMES A__ : Union[str, Any] = PRETRAINED_VOCAB_FILES_MAP A__ : Union[str, Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES A__ : int = ['''input_ids''', '''token_type_ids'''] A__ : Optional[Any] = FNetTokenizer def __init__( self , __UpperCamelCase=None , __UpperCamelCase=None , __UpperCamelCase=False , __UpperCamelCase=True , __UpperCamelCase=True , __UpperCamelCase="<unk>" , __UpperCamelCase="[SEP]" , __UpperCamelCase="<pad>" , __UpperCamelCase="[CLS]" , __UpperCamelCase="[MASK]" , **__UpperCamelCase , ): """simple docstring""" UpperCamelCase_ = ( AddedToken(__lowerCAmelCase , lstrip=__lowerCAmelCase , rstrip=__lowerCAmelCase , normalized=__lowerCAmelCase ) if isinstance(__lowerCAmelCase , __lowerCAmelCase ) else mask_token ) super().__init__( __lowerCAmelCase , tokenizer_file=__lowerCAmelCase , do_lower_case=__lowerCAmelCase , remove_space=__lowerCAmelCase , keep_accents=__lowerCAmelCase , unk_token=__lowerCAmelCase , sep_token=__lowerCAmelCase , pad_token=__lowerCAmelCase , cls_token=__lowerCAmelCase , mask_token=__lowerCAmelCase , **__lowerCAmelCase , ) UpperCamelCase_ = do_lower_case UpperCamelCase_ = remove_space UpperCamelCase_ = keep_accents UpperCamelCase_ = vocab_file UpperCamelCase_ = False if not self.vocab_file else True def lowerCamelCase_ ( self , __UpperCamelCase , __UpperCamelCase = None ): """simple docstring""" UpperCamelCase_ = [self.sep_token_id] UpperCamelCase_ = [self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def lowerCamelCase_ ( self , __UpperCamelCase , __UpperCamelCase = None ): """simple docstring""" UpperCamelCase_ = [self.sep_token_id] UpperCamelCase_ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def lowerCamelCase_ ( self , __UpperCamelCase , __UpperCamelCase = None ): """simple docstring""" if not os.path.isdir(__lowerCAmelCase ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' ) return UpperCamelCase_ = os.path.join( __lowerCAmelCase , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(__lowerCAmelCase ): copyfile(self.vocab_file , __lowerCAmelCase ) return (out_vocab_file,)
122
from sklearn.metrics import fa_score import datasets A : Any = ''' The F1 score is the harmonic mean of the precision and recall. It can be computed with the equation: F1 = 2 * (precision * recall) / (precision + recall) ''' A : List[Any] = ''' Args: predictions (`list` of `int`): Predicted labels. references (`list` of `int`): Ground truth labels. labels (`list` of `int`): The set of labels to include when `average` is not set to `\'binary\'`, and the order of the labels if `average` is `None`. Labels present in the data can be excluded, for example to calculate a multiclass average ignoring a majority negative class. Labels not present in the data will result in 0 components in a macro average. For multilabel targets, labels are column indices. By default, all labels in `predictions` and `references` are used in sorted order. Defaults to None. pos_label (`int`): The class to be considered the positive class, in the case where `average` is set to `binary`. Defaults to 1. average (`string`): This parameter is required for multiclass/multilabel targets. If set to `None`, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data. Defaults to `\'binary\'`. - \'binary\': Only report results for the class specified by `pos_label`. This is applicable only if the classes found in `predictions` and `references` are binary. - \'micro\': Calculate metrics globally by counting the total true positives, false negatives and false positives. - \'macro\': Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account. - \'weighted\': Calculate metrics for each label, and find their average weighted by support (the number of true instances for each label). This alters `\'macro\'` to account for label imbalance. This option can result in an F-score that is not between precision and recall. - \'samples\': Calculate metrics for each instance, and find their average (only meaningful for multilabel classification). sample_weight (`list` of `float`): Sample weights Defaults to None. Returns: f1 (`float` or `array` of `float`): F1 score or list of f1 scores, depending on the value passed to `average`. Minimum possible value is 0. Maximum possible value is 1. Higher f1 scores are better. Examples: Example 1-A simple binary example >>> f1_metric = datasets.load_metric("f1") >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0]) >>> print(results) {\'f1\': 0.5} Example 2-The same simple binary example as in Example 1, but with `pos_label` set to `0`. >>> f1_metric = datasets.load_metric("f1") >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0], pos_label=0) >>> print(round(results[\'f1\'], 2)) 0.67 Example 3-The same simple binary example as in Example 1, but with `sample_weight` included. >>> f1_metric = datasets.load_metric("f1") >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0], sample_weight=[0.9, 0.5, 3.9, 1.2, 0.3]) >>> print(round(results[\'f1\'], 2)) 0.35 Example 4-A multiclass example, with different values for the `average` input. >>> predictions = [0, 2, 1, 0, 0, 1] >>> references = [0, 1, 2, 0, 1, 2] >>> results = f1_metric.compute(predictions=predictions, references=references, average="macro") >>> print(round(results[\'f1\'], 2)) 0.27 >>> results = f1_metric.compute(predictions=predictions, references=references, average="micro") >>> print(round(results[\'f1\'], 2)) 0.33 >>> results = f1_metric.compute(predictions=predictions, references=references, average="weighted") >>> print(round(results[\'f1\'], 2)) 0.27 >>> results = f1_metric.compute(predictions=predictions, references=references, average=None) >>> print(results) {\'f1\': array([0.8, 0. , 0. ])} ''' A : List[Any] = ''' @article{scikit-learn, title={Scikit-learn: Machine Learning in {P}ython}, author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V. and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P. and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.}, journal={Journal of Machine Learning Research}, volume={12}, pages={2825--2830}, year={2011} } ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class A (datasets.Metric ): '''simple docstring''' def a_ ( self : Optional[int] ) -> List[Any]: """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": datasets.Sequence(datasets.Value("""int32""" ) ), """references""": datasets.Sequence(datasets.Value("""int32""" ) ), } if self.config_name == """multilabel""" else { """predictions""": datasets.Value("""int32""" ), """references""": datasets.Value("""int32""" ), } ) , reference_urls=["""https://scikit-learn.org/stable/modules/generated/sklearn.metrics.f1_score.html"""] , ) def a_ ( self : Any , __lowerCAmelCase : Tuple , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : Dict=None , __lowerCAmelCase : List[str]=1 , __lowerCAmelCase : Any="binary" , __lowerCAmelCase : Optional[int]=None ) -> List[Any]: """simple docstring""" A__ = fa_score( __lowerCAmelCase , __lowerCAmelCase , labels=__lowerCAmelCase , pos_label=__lowerCAmelCase , average=__lowerCAmelCase , sample_weight=__lowerCAmelCase ) return {"f1": float(__lowerCAmelCase ) if score.size == 1 else score}
274
0
import importlib import json import os from collections import OrderedDict from typing import Dict, Optional, Union # Build the list of all image processors from ...configuration_utils import PretrainedConfig from ...dynamic_module_utils import get_class_from_dynamic_module, resolve_trust_remote_code from ...image_processing_utils import ImageProcessingMixin from ...utils import CONFIG_NAME, IMAGE_PROCESSOR_NAME, get_file_from_repo, logging from .auto_factory import _LazyAutoMapping from .configuration_auto import ( CONFIG_MAPPING_NAMES, AutoConfig, model_type_to_module_name, replace_list_option_in_docstrings, ) UpperCAmelCase__ = logging.get_logger(__name__) UpperCAmelCase__ = OrderedDict( [ ('''align''', '''EfficientNetImageProcessor'''), ('''beit''', '''BeitImageProcessor'''), ('''bit''', '''BitImageProcessor'''), ('''blip''', '''BlipImageProcessor'''), ('''blip-2''', '''BlipImageProcessor'''), ('''bridgetower''', '''BridgeTowerImageProcessor'''), ('''chinese_clip''', '''ChineseCLIPImageProcessor'''), ('''clip''', '''CLIPImageProcessor'''), ('''clipseg''', '''ViTImageProcessor'''), ('''conditional_detr''', '''ConditionalDetrImageProcessor'''), ('''convnext''', '''ConvNextImageProcessor'''), ('''convnextv2''', '''ConvNextImageProcessor'''), ('''cvt''', '''ConvNextImageProcessor'''), ('''data2vec-vision''', '''BeitImageProcessor'''), ('''deformable_detr''', '''DeformableDetrImageProcessor'''), ('''deit''', '''DeiTImageProcessor'''), ('''deta''', '''DetaImageProcessor'''), ('''detr''', '''DetrImageProcessor'''), ('''dinat''', '''ViTImageProcessor'''), ('''donut-swin''', '''DonutImageProcessor'''), ('''dpt''', '''DPTImageProcessor'''), ('''efficientformer''', '''EfficientFormerImageProcessor'''), ('''efficientnet''', '''EfficientNetImageProcessor'''), ('''flava''', '''FlavaImageProcessor'''), ('''focalnet''', '''BitImageProcessor'''), ('''git''', '''CLIPImageProcessor'''), ('''glpn''', '''GLPNImageProcessor'''), ('''groupvit''', '''CLIPImageProcessor'''), ('''imagegpt''', '''ImageGPTImageProcessor'''), ('''instructblip''', '''BlipImageProcessor'''), ('''layoutlmv2''', '''LayoutLMv2ImageProcessor'''), ('''layoutlmv3''', '''LayoutLMv3ImageProcessor'''), ('''levit''', '''LevitImageProcessor'''), ('''mask2former''', '''Mask2FormerImageProcessor'''), ('''maskformer''', '''MaskFormerImageProcessor'''), ('''mgp-str''', '''ViTImageProcessor'''), ('''mobilenet_v1''', '''MobileNetV1ImageProcessor'''), ('''mobilenet_v2''', '''MobileNetV2ImageProcessor'''), ('''mobilevit''', '''MobileViTImageProcessor'''), ('''mobilevit''', '''MobileViTImageProcessor'''), ('''mobilevitv2''', '''MobileViTImageProcessor'''), ('''nat''', '''ViTImageProcessor'''), ('''oneformer''', '''OneFormerImageProcessor'''), ('''owlvit''', '''OwlViTImageProcessor'''), ('''perceiver''', '''PerceiverImageProcessor'''), ('''pix2struct''', '''Pix2StructImageProcessor'''), ('''poolformer''', '''PoolFormerImageProcessor'''), ('''regnet''', '''ConvNextImageProcessor'''), ('''resnet''', '''ConvNextImageProcessor'''), ('''sam''', '''SamImageProcessor'''), ('''segformer''', '''SegformerImageProcessor'''), ('''swiftformer''', '''ViTImageProcessor'''), ('''swin''', '''ViTImageProcessor'''), ('''swin2sr''', '''Swin2SRImageProcessor'''), ('''swinv2''', '''ViTImageProcessor'''), ('''table-transformer''', '''DetrImageProcessor'''), ('''timesformer''', '''VideoMAEImageProcessor'''), ('''tvlt''', '''TvltImageProcessor'''), ('''upernet''', '''SegformerImageProcessor'''), ('''van''', '''ConvNextImageProcessor'''), ('''videomae''', '''VideoMAEImageProcessor'''), ('''vilt''', '''ViltImageProcessor'''), ('''vit''', '''ViTImageProcessor'''), ('''vit_hybrid''', '''ViTHybridImageProcessor'''), ('''vit_mae''', '''ViTImageProcessor'''), ('''vit_msn''', '''ViTImageProcessor'''), ('''xclip''', '''CLIPImageProcessor'''), ('''yolos''', '''YolosImageProcessor'''), ] ) UpperCAmelCase__ = _LazyAutoMapping(CONFIG_MAPPING_NAMES, IMAGE_PROCESSOR_MAPPING_NAMES) def UpperCAmelCase_ ( __snake_case ) -> int: """simple docstring""" for module_name, extractors in IMAGE_PROCESSOR_MAPPING_NAMES.items(): if class_name in extractors: _lowercase =model_type_to_module_name(__a ) _lowercase =importlib.import_module(F".{module_name}" , '''transformers.models''' ) try: return getattr(__a , __a ) except AttributeError: continue for _, extractor in IMAGE_PROCESSOR_MAPPING._extra_content.items(): if getattr(__a , '''__name__''' , __a ) == class_name: return extractor # We did not fine the class, but maybe it's because a dep is missing. In that case, the class will be in the main # init and we return the proper dummy to get an appropriate error message. _lowercase =importlib.import_module('''transformers''' ) if hasattr(__a , __a ): return getattr(__a , __a ) return None def UpperCAmelCase_ ( __snake_case , __snake_case = None , __snake_case = False , __snake_case = False , __snake_case = None , __snake_case = None , __snake_case = None , __snake_case = False , **__snake_case , ) -> List[str]: """simple docstring""" _lowercase =get_file_from_repo( __a , __a , cache_dir=__a , force_download=__a , resume_download=__a , proxies=__a , use_auth_token=__a , revision=__a , local_files_only=__a , ) if resolved_config_file is None: logger.info( '''Could not locate the image processor configuration file, will try to use the model config instead.''' ) return {} with open(__a , encoding='''utf-8''' ) as reader: return json.load(__a ) class lowerCamelCase__ : def __init__(self ) -> Dict: raise EnvironmentError( '''AutoImageProcessor is designed to be instantiated ''' '''using the `AutoImageProcessor.from_pretrained(pretrained_model_name_or_path)` method.''' ) @classmethod @replace_list_option_in_docstrings(__lowerCAmelCase ) def __A (cls , UpperCAmelCase , **UpperCAmelCase ) -> Any: _lowercase =kwargs.pop('''config''' , __lowerCAmelCase ) _lowercase =kwargs.pop('''trust_remote_code''' , __lowerCAmelCase ) _lowercase =True _lowercase , _lowercase =ImageProcessingMixin.get_image_processor_dict(__lowerCAmelCase , **__lowerCAmelCase ) _lowercase =config_dict.get('''image_processor_type''' , __lowerCAmelCase ) _lowercase =None if "AutoImageProcessor" in config_dict.get('''auto_map''' , {} ): _lowercase =config_dict['''auto_map''']['''AutoImageProcessor'''] # If we still don't have the image processor class, check if we're loading from a previous feature extractor config # and if so, infer the image processor class from there. if image_processor_class is None and image_processor_auto_map is None: _lowercase =config_dict.pop('''feature_extractor_type''' , __lowerCAmelCase ) if feature_extractor_class is not None: logger.warning( '''Could not find image processor class in the image processor config or the model config. Loading''' ''' based on pattern matching with the model\'s feature extractor configuration.''' ) _lowercase =feature_extractor_class.replace('''FeatureExtractor''' , '''ImageProcessor''' ) if "AutoFeatureExtractor" in config_dict.get('''auto_map''' , {} ): _lowercase =config_dict['''auto_map''']['''AutoFeatureExtractor'''] _lowercase =feature_extractor_auto_map.replace('''FeatureExtractor''' , '''ImageProcessor''' ) logger.warning( '''Could not find image processor auto map in the image processor config or the model config.''' ''' Loading based on pattern matching with the model\'s feature extractor configuration.''' ) # If we don't find the image processor class in the image processor config, let's try the model config. if image_processor_class is None and image_processor_auto_map is None: if not isinstance(__lowerCAmelCase , __lowerCAmelCase ): _lowercase =AutoConfig.from_pretrained(__lowerCAmelCase , **__lowerCAmelCase ) # It could be in `config.image_processor_type`` _lowercase =getattr(__lowerCAmelCase , '''image_processor_type''' , __lowerCAmelCase ) if hasattr(__lowerCAmelCase , '''auto_map''' ) and "AutoImageProcessor" in config.auto_map: _lowercase =config.auto_map['''AutoImageProcessor'''] if image_processor_class is not None: _lowercase =image_processor_class_from_name(__lowerCAmelCase ) _lowercase =image_processor_auto_map is not None _lowercase =image_processor_class is not None or type(__lowerCAmelCase ) in IMAGE_PROCESSOR_MAPPING _lowercase =resolve_trust_remote_code( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) if has_remote_code and trust_remote_code: _lowercase =get_class_from_dynamic_module( __lowerCAmelCase , __lowerCAmelCase , **__lowerCAmelCase ) _lowercase =kwargs.pop('''code_revision''' , __lowerCAmelCase ) if os.path.isdir(__lowerCAmelCase ): image_processor_class.register_for_auto_class() return image_processor_class.from_dict(__lowerCAmelCase , **__lowerCAmelCase ) elif image_processor_class is not None: return image_processor_class.from_dict(__lowerCAmelCase , **__lowerCAmelCase ) # Last try: we use the IMAGE_PROCESSOR_MAPPING. elif type(__lowerCAmelCase ) in IMAGE_PROCESSOR_MAPPING: _lowercase =IMAGE_PROCESSOR_MAPPING[type(__lowerCAmelCase )] return image_processor_class.from_dict(__lowerCAmelCase , **__lowerCAmelCase ) raise ValueError( f"Unrecognized image processor in {pretrained_model_name_or_path}. Should have a " f"`image_processor_type` key in its {IMAGE_PROCESSOR_NAME} of {CONFIG_NAME}, or one of the following " f"`model_type` keys in its {CONFIG_NAME}: {', '.join(c for c in IMAGE_PROCESSOR_MAPPING_NAMES.keys() )}" ) @staticmethod def __A (UpperCAmelCase , UpperCAmelCase ) -> Dict: IMAGE_PROCESSOR_MAPPING.register(__lowerCAmelCase , __lowerCAmelCase )
5
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging A : Union[str, Any] = logging.get_logger(__name__) A : int = { '''xlm-roberta-base''': '''https://huggingface.co/xlm-roberta-base/resolve/main/config.json''', '''xlm-roberta-large''': '''https://huggingface.co/xlm-roberta-large/resolve/main/config.json''', '''xlm-roberta-large-finetuned-conll02-dutch''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll02-dutch/resolve/main/config.json''' ), '''xlm-roberta-large-finetuned-conll02-spanish''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll02-spanish/resolve/main/config.json''' ), '''xlm-roberta-large-finetuned-conll03-english''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll03-english/resolve/main/config.json''' ), '''xlm-roberta-large-finetuned-conll03-german''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll03-german/resolve/main/config.json''' ), } class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : Any = '''xlm-roberta''' def __init__( self : Optional[Any] , __lowerCAmelCase : List[Any]=3_05_22 , __lowerCAmelCase : int=7_68 , __lowerCAmelCase : Tuple=12 , __lowerCAmelCase : str=12 , __lowerCAmelCase : Union[str, Any]=30_72 , __lowerCAmelCase : Union[str, Any]="gelu" , __lowerCAmelCase : Optional[int]=0.1 , __lowerCAmelCase : Dict=0.1 , __lowerCAmelCase : List[str]=5_12 , __lowerCAmelCase : Tuple=2 , __lowerCAmelCase : Dict=0.0_2 , __lowerCAmelCase : List[str]=1e-12 , __lowerCAmelCase : Union[str, Any]=1 , __lowerCAmelCase : str=0 , __lowerCAmelCase : Optional[int]=2 , __lowerCAmelCase : Tuple="absolute" , __lowerCAmelCase : Any=True , __lowerCAmelCase : Any=None , **__lowerCAmelCase : str , ) -> Optional[Any]: """simple docstring""" super().__init__(pad_token_id=__lowerCAmelCase , bos_token_id=__lowerCAmelCase , eos_token_id=__lowerCAmelCase , **__lowerCAmelCase ) A__ = vocab_size A__ = hidden_size A__ = num_hidden_layers A__ = num_attention_heads A__ = hidden_act A__ = intermediate_size A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = max_position_embeddings A__ = type_vocab_size A__ = initializer_range A__ = layer_norm_eps A__ = position_embedding_type A__ = use_cache A__ = classifier_dropout class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' @property def a_ ( self : int ) -> Mapping[str, Mapping[int, str]]: """simple docstring""" if self.task == "multiple-choice": A__ = {0: """batch""", 1: """choice""", 2: """sequence"""} else: A__ = {0: """batch""", 1: """sequence"""} return OrderedDict( [ ("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ] )
274
0
'''simple docstring''' def __a(SCREAMING_SNAKE_CASE_ : List[str] ): '''simple docstring''' _lowerCAmelCase = [] _lowerCAmelCase = [] _lowerCAmelCase = { "^": 3, "*": 2, "/": 2, "%": 2, "+": 1, "-": 1, } # Priority of each operator _lowerCAmelCase = len(__a ) if (len(__a ) > 7) else 7 # Print table header for output print( "Symbol".center(8 ) , "Stack".center(__a ) , "Postfix".center(__a ) , sep=" | " , ) print("-" * (print_width * 3 + 7) ) for x in infix: if x.isalpha() or x.isdigit(): post_fix.append(__a ) # if x is Alphabet / Digit, add it to Postfix elif x == "(": stack.append(__a ) # if x is "(" push to Stack elif x == ")": # if x is ")" pop stack until "(" is encountered while stack[-1] != "(": post_fix.append(stack.pop() ) # Pop stack & add the content to Postfix stack.pop() else: if len(__a ) == 0: stack.append(__a ) # If stack is empty, push x to stack else: # while priority of x is not > priority of element in the stack while len(__a ) > 0 and priority[x] <= priority[stack[-1]]: post_fix.append(stack.pop() ) # pop stack & add to Postfix stack.append(__a ) # push x to stack print( x.center(8 ) , ("".join(__a )).ljust(__a ) , ("".join(__a )).ljust(__a ) , sep=" | " , ) # Output in tabular format while len(__a ) > 0: # while stack is not empty post_fix.append(stack.pop() ) # pop stack & add to Postfix print( " ".center(8 ) , ("".join(__a )).ljust(__a ) , ("".join(__a )).ljust(__a ) , sep=" | " , ) # Output in tabular format return "".join(__a ) # return Postfix as str def __a(SCREAMING_SNAKE_CASE_ : Tuple ): '''simple docstring''' _lowerCAmelCase = list(infix[::-1] ) # reverse the infix equation for i in range(len(__a ) ): if infix[i] == "(": _lowerCAmelCase = ")" # change "(" to ")" elif infix[i] == ")": _lowerCAmelCase = "(" # change ")" to "(" return (infix_2_postfix("".join(__a ) ))[ ::-1 ] # call infix_2_postfix on Infix, return reverse of Postfix if __name__ == "__main__": _SCREAMING_SNAKE_CASE = input("\nEnter an Infix Equation = ") # Input an Infix equation _SCREAMING_SNAKE_CASE = ''''''.join(Infix.split()) # Remove spaces from the input print("\n\t", Infix, "(Infix) -> ", infix_2_prefix(Infix), "(Prefix)")
158
from __future__ import annotations import time from math import sqrt # 1 for manhattan, 0 for euclidean A : str = 0 A : Any = [ [0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles [0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0], [1, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0], ] A : Dict = [[-1, 0], [0, -1], [1, 0], [0, 1]] # up, left, down, right A : Union[str, Any] = tuple[int, int] class A : '''simple docstring''' def __init__( self : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : Node | None , ) -> None: """simple docstring""" A__ = pos_x A__ = pos_y A__ = (pos_y, pos_x) A__ = goal_x A__ = goal_y A__ = g_cost A__ = parent A__ = self.calculate_heuristic() A__ = self.g_cost + self.h_cost def a_ ( self : Dict ) -> float: """simple docstring""" A__ = self.pos_x - self.goal_x A__ = self.pos_y - self.goal_y if HEURISTIC == 1: return abs(__lowerCAmelCase ) + abs(__lowerCAmelCase ) else: return sqrt(dy**2 + dx**2 ) def __lt__( self : int , __lowerCAmelCase : Node ) -> bool: """simple docstring""" return self.f_cost < other.f_cost class A : '''simple docstring''' def __init__( self : Union[str, Any] , __lowerCAmelCase : TPosition , __lowerCAmelCase : TPosition ) -> Tuple: """simple docstring""" A__ = Node(start[1] , start[0] , goal[1] , goal[0] , 0 , __lowerCAmelCase ) A__ = Node(goal[1] , goal[0] , goal[1] , goal[0] , 9_99_99 , __lowerCAmelCase ) A__ = [self.start] A__ = [] A__ = False def a_ ( self : List[str] ) -> list[TPosition]: """simple docstring""" while self.open_nodes: # Open Nodes are sorted using __lt__ self.open_nodes.sort() A__ = self.open_nodes.pop(0 ) if current_node.pos == self.target.pos: return self.retrace_path(__lowerCAmelCase ) self.closed_nodes.append(__lowerCAmelCase ) A__ = self.get_successors(__lowerCAmelCase ) for child_node in successors: if child_node in self.closed_nodes: continue if child_node not in self.open_nodes: self.open_nodes.append(__lowerCAmelCase ) else: # retrieve the best current path A__ = self.open_nodes.pop(self.open_nodes.index(__lowerCAmelCase ) ) if child_node.g_cost < better_node.g_cost: self.open_nodes.append(__lowerCAmelCase ) else: self.open_nodes.append(__lowerCAmelCase ) return [self.start.pos] def a_ ( self : Optional[Any] , __lowerCAmelCase : Node ) -> list[Node]: """simple docstring""" A__ = [] for action in delta: A__ = parent.pos_x + action[1] A__ = parent.pos_y + action[0] if not (0 <= pos_x <= len(grid[0] ) - 1 and 0 <= pos_y <= len(__lowerCAmelCase ) - 1): continue if grid[pos_y][pos_x] != 0: continue successors.append( Node( __lowerCAmelCase , __lowerCAmelCase , self.target.pos_y , self.target.pos_x , parent.g_cost + 1 , __lowerCAmelCase , ) ) return successors def a_ ( self : List[Any] , __lowerCAmelCase : Node | None ) -> list[TPosition]: """simple docstring""" A__ = node A__ = [] while current_node is not None: path.append((current_node.pos_y, current_node.pos_x) ) A__ = current_node.parent path.reverse() return path class A : '''simple docstring''' def __init__( self : Optional[Any] , __lowerCAmelCase : TPosition , __lowerCAmelCase : TPosition ) -> None: """simple docstring""" A__ = AStar(__lowerCAmelCase , __lowerCAmelCase ) A__ = AStar(__lowerCAmelCase , __lowerCAmelCase ) A__ = False def a_ ( self : int ) -> list[TPosition]: """simple docstring""" while self.fwd_astar.open_nodes or self.bwd_astar.open_nodes: self.fwd_astar.open_nodes.sort() self.bwd_astar.open_nodes.sort() A__ = self.fwd_astar.open_nodes.pop(0 ) A__ = self.bwd_astar.open_nodes.pop(0 ) if current_bwd_node.pos == current_fwd_node.pos: return self.retrace_bidirectional_path( __lowerCAmelCase , __lowerCAmelCase ) self.fwd_astar.closed_nodes.append(__lowerCAmelCase ) self.bwd_astar.closed_nodes.append(__lowerCAmelCase ) A__ = current_bwd_node A__ = current_fwd_node A__ = { self.fwd_astar: self.fwd_astar.get_successors(__lowerCAmelCase ), self.bwd_astar: self.bwd_astar.get_successors(__lowerCAmelCase ), } for astar in [self.fwd_astar, self.bwd_astar]: for child_node in successors[astar]: if child_node in astar.closed_nodes: continue if child_node not in astar.open_nodes: astar.open_nodes.append(__lowerCAmelCase ) else: # retrieve the best current path A__ = astar.open_nodes.pop( astar.open_nodes.index(__lowerCAmelCase ) ) if child_node.g_cost < better_node.g_cost: astar.open_nodes.append(__lowerCAmelCase ) else: astar.open_nodes.append(__lowerCAmelCase ) return [self.fwd_astar.start.pos] def a_ ( self : List[str] , __lowerCAmelCase : Node , __lowerCAmelCase : Node ) -> list[TPosition]: """simple docstring""" A__ = self.fwd_astar.retrace_path(__lowerCAmelCase ) A__ = self.bwd_astar.retrace_path(__lowerCAmelCase ) bwd_path.pop() bwd_path.reverse() A__ = fwd_path + bwd_path return path if __name__ == "__main__": # all coordinates are given in format [y,x] A : Optional[int] = (0, 0) A : int = (len(grid) - 1, len(grid[0]) - 1) for elem in grid: print(elem) A : Dict = time.time() A : Optional[Any] = AStar(init, goal) A : Optional[int] = a_star.search() A : Optional[int] = time.time() - start_time print(F'''AStar execution time = {end_time:f} seconds''') A : Dict = time.time() A : Tuple = BidirectionalAStar(init, goal) A : List[Any] = time.time() - bd_start_time print(F'''BidirectionalAStar execution time = {bd_end_time:f} seconds''')
274
0
from __future__ import annotations from collections.abc import MutableSequence class __A : '''simple docstring''' def __init__( self , __lowerCAmelCase , __lowerCAmelCase ): '''simple docstring''' if len(__lowerCAmelCase ) != degree + 1: raise ValueError( '''The number of coefficients should be equal to the degree + 1.''' ) lowerCamelCase__ = list(__lowerCAmelCase ) lowerCamelCase__ = degree def __add__( self , __lowerCAmelCase ): '''simple docstring''' if self.degree > polynomial_a.degree: lowerCamelCase__ = self.coefficients[:] for i in range(polynomial_a.degree + 1 ): coefficients[i] += polynomial_a.coefficients[i] return Polynomial(self.degree , __lowerCAmelCase ) else: lowerCamelCase__ = polynomial_a.coefficients[:] for i in range(self.degree + 1 ): coefficients[i] += self.coefficients[i] return Polynomial(polynomial_a.degree , __lowerCAmelCase ) def __sub__( self , __lowerCAmelCase ): '''simple docstring''' return self + polynomial_a * Polynomial(0 , [-1] ) def __neg__( self ): '''simple docstring''' return Polynomial(self.degree , [-c for c in self.coefficients] ) def __mul__( self , __lowerCAmelCase ): '''simple docstring''' lowerCamelCase__ = [0] * (self.degree + polynomial_a.degree + 1) for i in range(self.degree + 1 ): for j in range(polynomial_a.degree + 1 ): coefficients[i + j] += ( self.coefficients[i] * polynomial_a.coefficients[j] ) return Polynomial(self.degree + polynomial_a.degree , __lowerCAmelCase ) def __lowerCamelCase ( self , __lowerCAmelCase ): '''simple docstring''' lowerCamelCase__ = 0 for i in range(self.degree + 1 ): result += self.coefficients[i] * (substitution**i) return result def __str__( self ): '''simple docstring''' lowerCamelCase__ = '''''' for i in range(self.degree , -1 , -1 ): if self.coefficients[i] == 0: continue elif self.coefficients[i] > 0: if polynomial: polynomial += " + " else: polynomial += " - " if i == 0: polynomial += str(abs(self.coefficients[i] ) ) elif i == 1: polynomial += str(abs(self.coefficients[i] ) ) + "x" else: polynomial += str(abs(self.coefficients[i] ) ) + "x^" + str(__lowerCAmelCase ) return polynomial def __repr__( self ): '''simple docstring''' return self.__str__() def __lowerCamelCase ( self ): '''simple docstring''' lowerCamelCase__ = [0] * self.degree for i in range(self.degree ): lowerCamelCase__ = self.coefficients[i + 1] * (i + 1) return Polynomial(self.degree - 1 , __lowerCAmelCase ) def __lowerCamelCase ( self , __lowerCAmelCase = 0 ): '''simple docstring''' lowerCamelCase__ = [0] * (self.degree + 2) lowerCamelCase__ = constant for i in range(self.degree + 1 ): lowerCamelCase__ = self.coefficients[i] / (i + 1) return Polynomial(self.degree + 1 , __lowerCAmelCase ) def __eq__( self , __lowerCAmelCase ): '''simple docstring''' if not isinstance(__lowerCAmelCase , __lowerCAmelCase ): return False if self.degree != polynomial_a.degree: return False for i in range(self.degree + 1 ): if self.coefficients[i] != polynomial_a.coefficients[i]: return False return True def __ne__( self , __lowerCAmelCase ): '''simple docstring''' return not self.__eq__(__lowerCAmelCase )
209
import unittest from transformers.utils.backbone_utils import ( BackboneMixin, get_aligned_output_features_output_indices, verify_out_features_out_indices, ) class A (unittest.TestCase ): '''simple docstring''' def a_ ( self : Any ) -> Union[str, Any]: """simple docstring""" A__ = ["""a""", """b""", """c"""] # Defaults to last layer if both are None A__ , A__ = get_aligned_output_features_output_indices(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , ["""c"""] ) self.assertEqual(__lowerCAmelCase , [2] ) # Out indices set to match out features A__ , A__ = get_aligned_output_features_output_indices(["""a""", """c"""] , __lowerCAmelCase , __lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , ["""a""", """c"""] ) self.assertEqual(__lowerCAmelCase , [0, 2] ) # Out features set to match out indices A__ , A__ = get_aligned_output_features_output_indices(__lowerCAmelCase , [0, 2] , __lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , ["""a""", """c"""] ) self.assertEqual(__lowerCAmelCase , [0, 2] ) # Out features selected from negative indices A__ , A__ = get_aligned_output_features_output_indices(__lowerCAmelCase , [-3, -1] , __lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , ["""a""", """c"""] ) self.assertEqual(__lowerCAmelCase , [-3, -1] ) def a_ ( self : Optional[Any] ) -> Optional[Any]: """simple docstring""" with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(["""a""", """b"""] , (0, 1) , __lowerCAmelCase ) # Out features must be a list with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(("""a""", """b""") , (0, 1) , ["""a""", """b"""] ) # Out features must be a subset of stage names with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(["""a""", """b"""] , (0, 1) , ["""a"""] ) # Out indices must be a list or tuple with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(__lowerCAmelCase , 0 , ["""a""", """b"""] ) # Out indices must be a subset of stage names with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(__lowerCAmelCase , (0, 1) , ["""a"""] ) # Out features and out indices must be the same length with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(["""a""", """b"""] , (0,) , ["""a""", """b""", """c"""] ) # Out features should match out indices with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(["""a""", """b"""] , (0, 2) , ["""a""", """b""", """c"""] ) # Out features and out indices should be in order with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(["""b""", """a"""] , (0, 1) , ["""a""", """b"""] ) # Check passes with valid inputs verify_out_features_out_indices(["""a""", """b""", """d"""] , (0, 1, -1) , ["""a""", """b""", """c""", """d"""] ) def a_ ( self : Union[str, Any] ) -> List[str]: """simple docstring""" A__ = BackboneMixin() A__ = ["""a""", """b""", """c"""] A__ = ["""a""", """c"""] A__ = [0, 2] # Check that the output features and indices are set correctly self.assertEqual(backbone.out_features , ["""a""", """c"""] ) self.assertEqual(backbone.out_indices , [0, 2] ) # Check out features and indices are updated correctly A__ = ["""a""", """b"""] self.assertEqual(backbone.out_features , ["""a""", """b"""] ) self.assertEqual(backbone.out_indices , [0, 1] ) A__ = [-3, -1] self.assertEqual(backbone.out_features , ["""a""", """c"""] ) self.assertEqual(backbone.out_indices , [-3, -1] )
274
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available A : str = { '''configuration_biogpt''': ['''BIOGPT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''BioGptConfig'''], '''tokenization_biogpt''': ['''BioGptTokenizer'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Optional[int] = [ '''BIOGPT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''BioGptForCausalLM''', '''BioGptForTokenClassification''', '''BioGptForSequenceClassification''', '''BioGptModel''', '''BioGptPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_biogpt import BIOGPT_PRETRAINED_CONFIG_ARCHIVE_MAP, BioGptConfig from .tokenization_biogpt import BioGptTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_biogpt import ( BIOGPT_PRETRAINED_MODEL_ARCHIVE_LIST, BioGptForCausalLM, BioGptForSequenceClassification, BioGptForTokenClassification, BioGptModel, BioGptPreTrainedModel, ) else: import sys A : int = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
184
from collections import deque class A : '''simple docstring''' def __init__( self : List[str] , __lowerCAmelCase : str , __lowerCAmelCase : int , __lowerCAmelCase : int ) -> None: """simple docstring""" A__ = process_name # process name A__ = arrival_time # arrival time of the process # completion time of finished process or last interrupted time A__ = arrival_time A__ = burst_time # remaining burst time A__ = 0 # total time of the process wait in ready queue A__ = 0 # time from arrival time to completion time class A : '''simple docstring''' def __init__( self : Tuple , __lowerCAmelCase : int , __lowerCAmelCase : list[int] , __lowerCAmelCase : deque[Process] , __lowerCAmelCase : int , ) -> None: """simple docstring""" A__ = number_of_queues # time slice of queues that round robin algorithm applied A__ = time_slices # unfinished process is in this ready_queue A__ = queue # current time A__ = current_time # finished process is in this sequence queue A__ = deque() def a_ ( self : Dict ) -> list[str]: """simple docstring""" A__ = [] for i in range(len(self.finish_queue ) ): sequence.append(self.finish_queue[i].process_name ) return sequence def a_ ( self : Tuple , __lowerCAmelCase : list[Process] ) -> list[int]: """simple docstring""" A__ = [] for i in range(len(__lowerCAmelCase ) ): waiting_times.append(queue[i].waiting_time ) return waiting_times def a_ ( self : Optional[Any] , __lowerCAmelCase : list[Process] ) -> list[int]: """simple docstring""" A__ = [] for i in range(len(__lowerCAmelCase ) ): turnaround_times.append(queue[i].turnaround_time ) return turnaround_times def a_ ( self : Dict , __lowerCAmelCase : list[Process] ) -> list[int]: """simple docstring""" A__ = [] for i in range(len(__lowerCAmelCase ) ): completion_times.append(queue[i].stop_time ) return completion_times def a_ ( self : int , __lowerCAmelCase : deque[Process] ) -> list[int]: """simple docstring""" return [q.burst_time for q in queue] def a_ ( self : Any , __lowerCAmelCase : Process ) -> int: """simple docstring""" process.waiting_time += self.current_time - process.stop_time return process.waiting_time def a_ ( self : Union[str, Any] , __lowerCAmelCase : deque[Process] ) -> deque[Process]: """simple docstring""" A__ = deque() # sequence deque of finished process while len(__lowerCAmelCase ) != 0: A__ = ready_queue.popleft() # current process # if process's arrival time is later than current time, update current time if self.current_time < cp.arrival_time: self.current_time += cp.arrival_time # update waiting time of current process self.update_waiting_time(__lowerCAmelCase ) # update current time self.current_time += cp.burst_time # finish the process and set the process's burst-time 0 A__ = 0 # set the process's turnaround time because it is finished A__ = self.current_time - cp.arrival_time # set the completion time A__ = self.current_time # add the process to queue that has finished queue finished.append(__lowerCAmelCase ) self.finish_queue.extend(__lowerCAmelCase ) # add finished process to finish queue # FCFS will finish all remaining processes return finished def a_ ( self : Optional[Any] , __lowerCAmelCase : deque[Process] , __lowerCAmelCase : int ) -> tuple[deque[Process], deque[Process]]: """simple docstring""" A__ = deque() # sequence deque of terminated process # just for 1 cycle and unfinished processes will go back to queue for _ in range(len(__lowerCAmelCase ) ): A__ = ready_queue.popleft() # current process # if process's arrival time is later than current time, update current time if self.current_time < cp.arrival_time: self.current_time += cp.arrival_time # update waiting time of unfinished processes self.update_waiting_time(__lowerCAmelCase ) # if the burst time of process is bigger than time-slice if cp.burst_time > time_slice: # use CPU for only time-slice self.current_time += time_slice # update remaining burst time cp.burst_time -= time_slice # update end point time A__ = self.current_time # locate the process behind the queue because it is not finished ready_queue.append(__lowerCAmelCase ) else: # use CPU for remaining burst time self.current_time += cp.burst_time # set burst time 0 because the process is finished A__ = 0 # set the finish time A__ = self.current_time # update the process' turnaround time because it is finished A__ = self.current_time - cp.arrival_time # add the process to queue that has finished queue finished.append(__lowerCAmelCase ) self.finish_queue.extend(__lowerCAmelCase ) # add finished process to finish queue # return finished processes queue and remaining processes queue return finished, ready_queue def a_ ( self : List[Any] ) -> deque[Process]: """simple docstring""" for i in range(self.number_of_queues - 1 ): A__ , A__ = self.round_robin( self.ready_queue , self.time_slices[i] ) # the last queue has first_come_first_served algorithm self.first_come_first_served(self.ready_queue ) return self.finish_queue if __name__ == "__main__": import doctest A : Union[str, Any] = Process('''P1''', 0, 5_3) A : Optional[Any] = Process('''P2''', 0, 1_7) A : Optional[int] = Process('''P3''', 0, 6_8) A : int = Process('''P4''', 0, 2_4) A : Any = 3 A : List[Any] = [1_7, 2_5] A : Optional[Any] = deque([Pa, Pa, Pa, Pa]) if len(time_slices) != number_of_queues - 1: raise SystemExit(0) doctest.testmod(extraglobs={'''queue''': deque([Pa, Pa, Pa, Pa])}) A : Optional[Any] = Process('''P1''', 0, 5_3) A : int = Process('''P2''', 0, 1_7) A : Optional[int] = Process('''P3''', 0, 6_8) A : Tuple = Process('''P4''', 0, 2_4) A : Union[str, Any] = 3 A : Optional[Any] = [1_7, 2_5] A : Tuple = deque([Pa, Pa, Pa, Pa]) A : Optional[int] = MLFQ(number_of_queues, time_slices, queue, 0) A : Dict = mlfq.multi_level_feedback_queue() # print total waiting times of processes(P1, P2, P3, P4) print( F'''waiting time:\ \t\t\t{MLFQ.calculate_waiting_time(mlfq, [Pa, Pa, Pa, Pa])}''' ) # print completion times of processes(P1, P2, P3, P4) print( F'''completion time:\ \t\t{MLFQ.calculate_completion_time(mlfq, [Pa, Pa, Pa, Pa])}''' ) # print total turnaround times of processes(P1, P2, P3, P4) print( F'''turnaround time:\ \t\t{MLFQ.calculate_turnaround_time(mlfq, [Pa, Pa, Pa, Pa])}''' ) # print sequence of finished processes print( F'''sequence of finished processes:\ {mlfq.calculate_sequence_of_finish_queue()}''' )
274
0
import torch from diffusers import EulerDiscreteScheduler from diffusers.utils import torch_device from .test_schedulers import SchedulerCommonTest class UpperCamelCase_ ( snake_case_ ): '''simple docstring''' lowerCAmelCase = (EulerDiscreteScheduler,) lowerCAmelCase = 1_0 def _UpperCamelCase ( self , **a ) -> Optional[Any]: snake_case_ = { 'num_train_timesteps': 11_00, 'beta_start': 0.0_001, 'beta_end': 0.02, 'beta_schedule': 'linear', } config.update(**__lowerCAmelCase ) return config def _UpperCamelCase ( self ) -> Optional[int]: for timesteps in [10, 50, 1_00, 10_00]: self.check_over_configs(num_train_timesteps=__lowerCAmelCase ) def _UpperCamelCase ( self ) -> List[str]: for beta_start, beta_end in zip([0.00_001, 0.0_001, 0.001] , [0.0_002, 0.002, 0.02] ): self.check_over_configs(beta_start=__lowerCAmelCase , beta_end=__lowerCAmelCase ) def _UpperCamelCase ( self ) -> Optional[Any]: for schedule in ["linear", "scaled_linear"]: self.check_over_configs(beta_schedule=__lowerCAmelCase ) def _UpperCamelCase ( self ) -> List[str]: for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=__lowerCAmelCase ) def _UpperCamelCase ( self ) -> List[Any]: snake_case_ = self.scheduler_classes[0] snake_case_ = self.get_scheduler_config() snake_case_ = scheduler_class(**__lowerCAmelCase ) scheduler.set_timesteps(self.num_inference_steps ) snake_case_ = torch.manual_seed(0 ) snake_case_ = self.dummy_model() snake_case_ = self.dummy_sample_deter * scheduler.init_noise_sigma snake_case_ = sample.to(__lowerCAmelCase ) for i, t in enumerate(scheduler.timesteps ): snake_case_ = scheduler.scale_model_input(__lowerCAmelCase , __lowerCAmelCase ) snake_case_ = model(__lowerCAmelCase , __lowerCAmelCase ) snake_case_ = scheduler.step(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , generator=__lowerCAmelCase ) snake_case_ = output.prev_sample snake_case_ = torch.sum(torch.abs(__lowerCAmelCase ) ) snake_case_ = torch.mean(torch.abs(__lowerCAmelCase ) ) assert abs(result_sum.item() - 10.08_07 ) < 1E-2 assert abs(result_mean.item() - 0.0_131 ) < 1E-3 def _UpperCamelCase ( self ) -> Tuple: snake_case_ = self.scheduler_classes[0] snake_case_ = self.get_scheduler_config(prediction_type='v_prediction' ) snake_case_ = scheduler_class(**__lowerCAmelCase ) scheduler.set_timesteps(self.num_inference_steps ) snake_case_ = torch.manual_seed(0 ) snake_case_ = self.dummy_model() snake_case_ = self.dummy_sample_deter * scheduler.init_noise_sigma snake_case_ = sample.to(__lowerCAmelCase ) for i, t in enumerate(scheduler.timesteps ): snake_case_ = scheduler.scale_model_input(__lowerCAmelCase , __lowerCAmelCase ) snake_case_ = model(__lowerCAmelCase , __lowerCAmelCase ) snake_case_ = scheduler.step(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , generator=__lowerCAmelCase ) snake_case_ = output.prev_sample snake_case_ = torch.sum(torch.abs(__lowerCAmelCase ) ) snake_case_ = torch.mean(torch.abs(__lowerCAmelCase ) ) assert abs(result_sum.item() - 0.0_002 ) < 1E-2 assert abs(result_mean.item() - 2.2_676E-06 ) < 1E-3 def _UpperCamelCase ( self ) -> List[str]: snake_case_ = self.scheduler_classes[0] snake_case_ = self.get_scheduler_config() snake_case_ = scheduler_class(**__lowerCAmelCase ) scheduler.set_timesteps(self.num_inference_steps , device=__lowerCAmelCase ) snake_case_ = torch.manual_seed(0 ) snake_case_ = self.dummy_model() snake_case_ = self.dummy_sample_deter * scheduler.init_noise_sigma.cpu() snake_case_ = sample.to(__lowerCAmelCase ) for t in scheduler.timesteps: snake_case_ = scheduler.scale_model_input(__lowerCAmelCase , __lowerCAmelCase ) snake_case_ = model(__lowerCAmelCase , __lowerCAmelCase ) snake_case_ = scheduler.step(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , generator=__lowerCAmelCase ) snake_case_ = output.prev_sample snake_case_ = torch.sum(torch.abs(__lowerCAmelCase ) ) snake_case_ = torch.mean(torch.abs(__lowerCAmelCase ) ) assert abs(result_sum.item() - 10.08_07 ) < 1E-2 assert abs(result_mean.item() - 0.0_131 ) < 1E-3 def _UpperCamelCase ( self ) -> List[Any]: snake_case_ = self.scheduler_classes[0] snake_case_ = self.get_scheduler_config() snake_case_ = scheduler_class(**__lowerCAmelCase , use_karras_sigmas=__lowerCAmelCase ) scheduler.set_timesteps(self.num_inference_steps , device=__lowerCAmelCase ) snake_case_ = torch.manual_seed(0 ) snake_case_ = self.dummy_model() snake_case_ = self.dummy_sample_deter * scheduler.init_noise_sigma.cpu() snake_case_ = sample.to(__lowerCAmelCase ) for t in scheduler.timesteps: snake_case_ = scheduler.scale_model_input(__lowerCAmelCase , __lowerCAmelCase ) snake_case_ = model(__lowerCAmelCase , __lowerCAmelCase ) snake_case_ = scheduler.step(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , generator=__lowerCAmelCase ) snake_case_ = output.prev_sample snake_case_ = torch.sum(torch.abs(__lowerCAmelCase ) ) snake_case_ = torch.mean(torch.abs(__lowerCAmelCase ) ) assert abs(result_sum.item() - 1_24.52_29_94_99_51_17_19 ) < 1E-2 assert abs(result_mean.item() - 0.16_213_932_633_399_963 ) < 1E-3
178
from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...onnx.utils import compute_effective_axis_dimension from ...utils import logging if TYPE_CHECKING: from ...processing_utils import ProcessorMixin from ...utils import TensorType A : str = logging.get_logger(__name__) A : Union[str, Any] = { '''microsoft/layoutlmv3-base''': '''https://huggingface.co/microsoft/layoutlmv3-base/resolve/main/config.json''', } class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : Optional[Any] = '''layoutlmv3''' def __init__( self : Tuple , __lowerCAmelCase : Optional[int]=5_02_65 , __lowerCAmelCase : Tuple=7_68 , __lowerCAmelCase : Union[str, Any]=12 , __lowerCAmelCase : Any=12 , __lowerCAmelCase : List[Any]=30_72 , __lowerCAmelCase : Optional[int]="gelu" , __lowerCAmelCase : Dict=0.1 , __lowerCAmelCase : List[Any]=0.1 , __lowerCAmelCase : Dict=5_12 , __lowerCAmelCase : Any=2 , __lowerCAmelCase : Dict=0.0_2 , __lowerCAmelCase : List[str]=1e-5 , __lowerCAmelCase : List[str]=1 , __lowerCAmelCase : int=0 , __lowerCAmelCase : Dict=2 , __lowerCAmelCase : Tuple=10_24 , __lowerCAmelCase : List[str]=1_28 , __lowerCAmelCase : Optional[int]=1_28 , __lowerCAmelCase : Any=True , __lowerCAmelCase : Optional[Any]=32 , __lowerCAmelCase : Any=1_28 , __lowerCAmelCase : str=64 , __lowerCAmelCase : Optional[int]=2_56 , __lowerCAmelCase : int=True , __lowerCAmelCase : int=True , __lowerCAmelCase : List[str]=True , __lowerCAmelCase : int=2_24 , __lowerCAmelCase : Dict=3 , __lowerCAmelCase : List[Any]=16 , __lowerCAmelCase : Dict=None , **__lowerCAmelCase : Optional[Any] , ) -> Dict: """simple docstring""" super().__init__( vocab_size=__lowerCAmelCase , hidden_size=__lowerCAmelCase , num_hidden_layers=__lowerCAmelCase , num_attention_heads=__lowerCAmelCase , intermediate_size=__lowerCAmelCase , hidden_act=__lowerCAmelCase , hidden_dropout_prob=__lowerCAmelCase , attention_probs_dropout_prob=__lowerCAmelCase , max_position_embeddings=__lowerCAmelCase , type_vocab_size=__lowerCAmelCase , initializer_range=__lowerCAmelCase , layer_norm_eps=__lowerCAmelCase , pad_token_id=__lowerCAmelCase , bos_token_id=__lowerCAmelCase , eos_token_id=__lowerCAmelCase , **__lowerCAmelCase , ) A__ = max_ad_position_embeddings A__ = coordinate_size A__ = shape_size A__ = has_relative_attention_bias A__ = rel_pos_bins A__ = max_rel_pos A__ = has_spatial_attention_bias A__ = rel_ad_pos_bins A__ = max_rel_ad_pos A__ = text_embed A__ = visual_embed A__ = input_size A__ = num_channels A__ = patch_size A__ = classifier_dropout class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : List[str] = version.parse('''1.12''' ) @property def a_ ( self : int ) -> Mapping[str, Mapping[int, str]]: """simple docstring""" if self.task in ["question-answering", "sequence-classification"]: return OrderedDict( [ ("""input_ids""", {0: """batch""", 1: """sequence"""}), ("""attention_mask""", {0: """batch""", 1: """sequence"""}), ("""bbox""", {0: """batch""", 1: """sequence"""}), ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}), ] ) else: return OrderedDict( [ ("""input_ids""", {0: """batch""", 1: """sequence"""}), ("""bbox""", {0: """batch""", 1: """sequence"""}), ("""attention_mask""", {0: """batch""", 1: """sequence"""}), ("""pixel_values""", {0: """batch""", 1: """num_channels"""}), ] ) @property def a_ ( self : Optional[int] ) -> float: """simple docstring""" return 1e-5 @property def a_ ( self : Tuple ) -> int: """simple docstring""" return 12 def a_ ( self : str , __lowerCAmelCase : "ProcessorMixin" , __lowerCAmelCase : int = -1 , __lowerCAmelCase : int = -1 , __lowerCAmelCase : bool = False , __lowerCAmelCase : Optional["TensorType"] = None , __lowerCAmelCase : int = 3 , __lowerCAmelCase : int = 40 , __lowerCAmelCase : int = 40 , ) -> Mapping[str, Any]: """simple docstring""" setattr(processor.image_processor , """apply_ocr""" , __lowerCAmelCase ) # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX A__ = compute_effective_axis_dimension( __lowerCAmelCase , fixed_dimension=OnnxConfig.default_fixed_batch , num_token_to_add=0 ) # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX A__ = processor.tokenizer.num_special_tokens_to_add(__lowerCAmelCase ) A__ = compute_effective_axis_dimension( __lowerCAmelCase , fixed_dimension=OnnxConfig.default_fixed_sequence , num_token_to_add=__lowerCAmelCase ) # Generate dummy inputs according to compute batch and sequence A__ = [[""" """.join([processor.tokenizer.unk_token] ) * seq_length]] * batch_size # Generate dummy bounding boxes A__ = [[[48, 84, 73, 1_28]]] * batch_size # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX # batch_size = compute_effective_axis_dimension(batch_size, fixed_dimension=OnnxConfig.default_fixed_batch) A__ = self._generate_dummy_images(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) A__ = dict( processor( __lowerCAmelCase , text=__lowerCAmelCase , boxes=__lowerCAmelCase , return_tensors=__lowerCAmelCase , ) ) return inputs
274
0
from typing import List, Optional, Tuple, Union import torch from torch import nn from torch.nn import CrossEntropyLoss from ... import AutoBackbone from ...modeling_outputs import SemanticSegmenterOutput from ...modeling_utils import PreTrainedModel from ...utils import add_start_docstrings, add_start_docstrings_to_model_forward, replace_return_docstrings from ...utils.backbone_utils import BackboneMixin from .configuration_upernet import UperNetConfig _A = [ '''openmmlab/upernet-convnext-tiny''', # See all UperNet models at https://huggingface.co/models?filter=upernet ] # General docstring _A = '''UperNetConfig''' class UpperCAmelCase__ ( nn.Module ): """simple docstring""" def __init__( self , A_ , A_ , A_ , A_ = 0 , A_ = False , A_ = 1 , ) -> None: super().__init__() __UpperCamelCase =nn.Convad( in_channels=__lowerCAmelCase , out_channels=__lowerCAmelCase , kernel_size=__lowerCAmelCase , padding=__lowerCAmelCase , bias=__lowerCAmelCase , dilation=__lowerCAmelCase , ) __UpperCamelCase =nn.BatchNormad(__lowerCAmelCase ) __UpperCamelCase =nn.ReLU() def _a ( self , A_ ) -> torch.Tensor: __UpperCamelCase =self.conv(__lowerCAmelCase ) __UpperCamelCase =self.batch_norm(__lowerCAmelCase ) __UpperCamelCase =self.activation(__lowerCAmelCase ) return output class UpperCAmelCase__ ( nn.Module ): """simple docstring""" def __init__( self , A_ , A_ , A_ ) -> None: super().__init__() __UpperCamelCase =[ nn.AdaptiveAvgPoolad(__lowerCAmelCase ), UperNetConvModule(__lowerCAmelCase , __lowerCAmelCase , kernel_size=1 ), ] for i, layer in enumerate(self.layers ): self.add_module(str(__lowerCAmelCase ) , __lowerCAmelCase ) def _a ( self , A_ ) -> torch.Tensor: __UpperCamelCase =input for layer in self.layers: __UpperCamelCase =layer(__lowerCAmelCase ) return hidden_state class UpperCAmelCase__ ( nn.Module ): """simple docstring""" def __init__( self , A_ , A_ , A_ , A_ ) -> None: super().__init__() __UpperCamelCase =pool_scales __UpperCamelCase =align_corners __UpperCamelCase =in_channels __UpperCamelCase =channels __UpperCamelCase =[] for i, pool_scale in enumerate(__lowerCAmelCase ): __UpperCamelCase =UperNetPyramidPoolingBlock(pool_scale=__lowerCAmelCase , in_channels=__lowerCAmelCase , channels=__lowerCAmelCase ) self.blocks.append(__lowerCAmelCase ) self.add_module(str(__lowerCAmelCase ) , __lowerCAmelCase ) def _a ( self , A_ ) -> List[torch.Tensor]: __UpperCamelCase =[] for ppm in self.blocks: __UpperCamelCase =ppm(__lowerCAmelCase ) __UpperCamelCase =nn.functional.interpolate( __lowerCAmelCase , size=x.size()[2:] , mode='bilinear' , align_corners=self.align_corners ) ppm_outs.append(__lowerCAmelCase ) return ppm_outs class UpperCAmelCase__ ( nn.Module ): """simple docstring""" def __init__( self , A_ , A_ ) -> List[str]: super().__init__() __UpperCamelCase =config __UpperCamelCase =config.pool_scales # e.g. (1, 2, 3, 6) __UpperCamelCase =in_channels __UpperCamelCase =config.hidden_size __UpperCamelCase =False __UpperCamelCase =nn.Convad(self.channels , config.num_labels , kernel_size=1 ) # PSP Module __UpperCamelCase =UperNetPyramidPoolingModule( self.pool_scales , self.in_channels[-1] , self.channels , align_corners=self.align_corners , ) __UpperCamelCase =UperNetConvModule( self.in_channels[-1] + len(self.pool_scales ) * self.channels , self.channels , kernel_size=3 , padding=1 , ) # FPN Module __UpperCamelCase =nn.ModuleList() __UpperCamelCase =nn.ModuleList() for in_channels in self.in_channels[:-1]: # skip the top layer __UpperCamelCase =UperNetConvModule(__lowerCAmelCase , self.channels , kernel_size=1 ) __UpperCamelCase =UperNetConvModule(self.channels , self.channels , kernel_size=3 , padding=1 ) self.lateral_convs.append(__lowerCAmelCase ) self.fpn_convs.append(__lowerCAmelCase ) __UpperCamelCase =UperNetConvModule( len(self.in_channels ) * self.channels , self.channels , kernel_size=3 , padding=1 , ) def _a ( self ) -> Union[str, Any]: self.apply(self._init_weights ) def _a ( self , A_ ) -> Dict: if isinstance(__lowerCAmelCase , nn.Convad ): module.weight.data.normal_(mean=0.0 , std=self.config.initializer_range ) if module.bias is not None: module.bias.data.zero_() def _a ( self , A_ ) -> Dict: __UpperCamelCase =inputs[-1] __UpperCamelCase =[x] psp_outs.extend(self.psp_modules(__lowerCAmelCase ) ) __UpperCamelCase =torch.cat(__lowerCAmelCase , dim=1 ) __UpperCamelCase =self.bottleneck(__lowerCAmelCase ) return output def _a ( self , A_ ) -> torch.Tensor: __UpperCamelCase =[lateral_conv(encoder_hidden_states[i] ) for i, lateral_conv in enumerate(self.lateral_convs )] laterals.append(self.psp_forward(__lowerCAmelCase ) ) # build top-down path __UpperCamelCase =len(__lowerCAmelCase ) for i in range(used_backbone_levels - 1 , 0 , -1 ): __UpperCamelCase =laterals[i - 1].shape[2:] __UpperCamelCase =laterals[i - 1] + nn.functional.interpolate( laterals[i] , size=__lowerCAmelCase , mode='bilinear' , align_corners=self.align_corners ) # build outputs __UpperCamelCase =[self.fpn_convs[i](laterals[i] ) for i in range(used_backbone_levels - 1 )] # append psp feature fpn_outs.append(laterals[-1] ) for i in range(used_backbone_levels - 1 , 0 , -1 ): __UpperCamelCase =nn.functional.interpolate( fpn_outs[i] , size=fpn_outs[0].shape[2:] , mode='bilinear' , align_corners=self.align_corners ) __UpperCamelCase =torch.cat(__lowerCAmelCase , dim=1 ) __UpperCamelCase =self.fpn_bottleneck(__lowerCAmelCase ) __UpperCamelCase =self.classifier(__lowerCAmelCase ) return output class UpperCAmelCase__ ( nn.Module ): """simple docstring""" def __init__( self , A_ , A_ = 2 , A_ = 3 , A_ = 1 ) -> None: super().__init__() __UpperCamelCase =config __UpperCamelCase =config.auxiliary_in_channels __UpperCamelCase =config.auxiliary_channels __UpperCamelCase =config.auxiliary_num_convs __UpperCamelCase =config.auxiliary_concat_input __UpperCamelCase =in_index __UpperCamelCase =(kernel_size // 2) * dilation __UpperCamelCase =[] convs.append( UperNetConvModule( self.in_channels , self.channels , kernel_size=__lowerCAmelCase , padding=__lowerCAmelCase , dilation=__lowerCAmelCase ) ) for i in range(self.num_convs - 1 ): convs.append( UperNetConvModule( self.channels , self.channels , kernel_size=__lowerCAmelCase , padding=__lowerCAmelCase , dilation=__lowerCAmelCase ) ) if self.num_convs == 0: __UpperCamelCase =nn.Identity() else: __UpperCamelCase =nn.Sequential(*__lowerCAmelCase ) if self.concat_input: __UpperCamelCase =UperNetConvModule( self.in_channels + self.channels , self.channels , kernel_size=__lowerCAmelCase , padding=kernel_size // 2 ) __UpperCamelCase =nn.Convad(self.channels , config.num_labels , kernel_size=1 ) def _a ( self ) -> Tuple: self.apply(self._init_weights ) def _a ( self , A_ ) -> Union[str, Any]: if isinstance(__lowerCAmelCase , nn.Convad ): module.weight.data.normal_(mean=0.0 , std=self.config.initializer_range ) if module.bias is not None: module.bias.data.zero_() def _a ( self , A_ ) -> torch.Tensor: __UpperCamelCase =encoder_hidden_states[self.in_index] __UpperCamelCase =self.convs(__lowerCAmelCase ) if self.concat_input: __UpperCamelCase =self.conv_cat(torch.cat([hidden_states, output] , dim=1 ) ) __UpperCamelCase =self.classifier(__lowerCAmelCase ) return output class UpperCAmelCase__ ( A_ ): """simple docstring""" UpperCAmelCase__ : str = UperNetConfig UpperCAmelCase__ : List[Any] = '''pixel_values''' UpperCAmelCase__ : Optional[Any] = True def _a ( self , A_ ) -> List[str]: if isinstance(__lowerCAmelCase , __lowerCAmelCase ): module.backbone.init_weights() module.decode_head.init_weights() module.auxiliary_head.init_weights() def _a ( self ) -> Tuple: self.backbone.init_weights() self.decode_head.init_weights() self.auxiliary_head.init_weights() def _a ( self , A_ , A_=False ) -> Any: if isinstance(__lowerCAmelCase , __lowerCAmelCase ): __UpperCamelCase =value _A = R''' Parameters: This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) sub-class. Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and behavior. config ([`UperNetConfig`]): 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. ''' _A = R''' Args: pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`): Pixel values. Padding will be ignored by default should you provide it. Pixel values can be obtained using [`AutoImageProcessor`]. See [`SegformerImageProcessor.__call__`] for details. output_attentions (`bool`, *optional*): Whether or not to return the attentions tensors of all attention layers in case the backbone has them. See `attentions` under returned tensors for more detail. output_hidden_states (`bool`, *optional*): Whether or not to return the hidden states of all layers of the backbone. 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( "UperNet framework leveraging any vision backbone e.g. for ADE20k, CityScapes." , A_ , ) class UpperCAmelCase__ ( A_ ): """simple docstring""" def __init__( self , A_ ) -> List[Any]: super().__init__(__lowerCAmelCase ) __UpperCamelCase =AutoBackbone.from_config(config.backbone_config ) # Semantic segmentation head(s) __UpperCamelCase =UperNetHead(__lowerCAmelCase , in_channels=self.backbone.channels ) __UpperCamelCase =UperNetFCNHead(__lowerCAmelCase ) if config.use_auxiliary_head else None # Initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(UPERNET_INPUTS_DOCSTRING.format('batch_size, sequence_length' ) ) @replace_return_docstrings(output_type=__lowerCAmelCase , config_class=_CONFIG_FOR_DOC ) def _a ( self , A_ = None , A_ = None , A_ = None , A_ = None , A_ = None , ) -> Union[tuple, SemanticSegmenterOutput]: __UpperCamelCase =return_dict if return_dict is not None else self.config.use_return_dict __UpperCamelCase =( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) __UpperCamelCase =output_attentions if output_attentions is not None else self.config.output_attentions __UpperCamelCase =self.backbone.forward_with_filtered_kwargs( __lowerCAmelCase , output_hidden_states=__lowerCAmelCase , output_attentions=__lowerCAmelCase ) __UpperCamelCase =outputs.feature_maps __UpperCamelCase =self.decode_head(__lowerCAmelCase ) __UpperCamelCase =nn.functional.interpolate(__lowerCAmelCase , size=pixel_values.shape[2:] , mode='bilinear' , align_corners=__lowerCAmelCase ) __UpperCamelCase =None if self.auxiliary_head is not None: __UpperCamelCase =self.auxiliary_head(__lowerCAmelCase ) __UpperCamelCase =nn.functional.interpolate( __lowerCAmelCase , size=pixel_values.shape[2:] , mode='bilinear' , align_corners=__lowerCAmelCase ) __UpperCamelCase =None if labels is not None: if self.config.num_labels == 1: raise ValueError('The number of labels should be greater than one' ) else: # compute weighted loss __UpperCamelCase =CrossEntropyLoss(ignore_index=self.config.loss_ignore_index ) __UpperCamelCase =loss_fct(__lowerCAmelCase , __lowerCAmelCase ) __UpperCamelCase =loss_fct(__lowerCAmelCase , __lowerCAmelCase ) __UpperCamelCase =main_loss + self.config.auxiliary_loss_weight * auxiliary_loss if not return_dict: if output_hidden_states: __UpperCamelCase =(logits,) + outputs[1:] else: __UpperCamelCase =(logits,) + outputs[2:] return ((loss,) + output) if loss is not None else output return SemanticSegmenterOutput( loss=__lowerCAmelCase , logits=__lowerCAmelCase , hidden_states=outputs.hidden_states , attentions=outputs.attentions , )
62
import json import os from pathlib import Path from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple, Union import sentencepiece from ...tokenization_utils import BatchEncoding, PreTrainedTokenizer from ...utils import logging A : Optional[Any] = logging.get_logger(__name__) A : str = '''▁''' A : Any = { '''vocab_file''': '''vocab.json''', '''spm_file''': '''sentencepiece.bpe.model''', '''tokenizer_config_file''': '''tokenizer_config.json''', } A : List[Any] = { '''vocab_file''': { '''facebook/m2m100_418M''': '''https://huggingface.co/facebook/m2m100_418M/resolve/main/vocab.json''', '''facebook/m2m100_1.2B''': '''https://huggingface.co/facebook/m2m100_1.2B/resolve/main/vocab.json''', }, '''spm_file''': { '''facebook/m2m100_418M''': '''https://huggingface.co/facebook/m2m100_418M/resolve/main/sentencepiece.bpe.model''', '''facebook/m2m100_1.2B''': '''https://huggingface.co/facebook/m2m100_1.2B/resolve/main/sentencepiece.bpe.model''', }, '''tokenizer_config_file''': { '''facebook/m2m100_418M''': '''https://huggingface.co/facebook/m2m100_418M/resolve/main/tokenizer_config.json''', '''facebook/m2m100_1.2B''': '''https://huggingface.co/facebook/m2m100_1.2B/resolve/main/tokenizer_config.json''', }, } A : Tuple = { '''facebook/m2m100_418M''': 1_0_2_4, } # fmt: off A : Optional[int] = { '''m2m100''': ['''af''', '''am''', '''ar''', '''ast''', '''az''', '''ba''', '''be''', '''bg''', '''bn''', '''br''', '''bs''', '''ca''', '''ceb''', '''cs''', '''cy''', '''da''', '''de''', '''el''', '''en''', '''es''', '''et''', '''fa''', '''ff''', '''fi''', '''fr''', '''fy''', '''ga''', '''gd''', '''gl''', '''gu''', '''ha''', '''he''', '''hi''', '''hr''', '''ht''', '''hu''', '''hy''', '''id''', '''ig''', '''ilo''', '''is''', '''it''', '''ja''', '''jv''', '''ka''', '''kk''', '''km''', '''kn''', '''ko''', '''lb''', '''lg''', '''ln''', '''lo''', '''lt''', '''lv''', '''mg''', '''mk''', '''ml''', '''mn''', '''mr''', '''ms''', '''my''', '''ne''', '''nl''', '''no''', '''ns''', '''oc''', '''or''', '''pa''', '''pl''', '''ps''', '''pt''', '''ro''', '''ru''', '''sd''', '''si''', '''sk''', '''sl''', '''so''', '''sq''', '''sr''', '''ss''', '''su''', '''sv''', '''sw''', '''ta''', '''th''', '''tl''', '''tn''', '''tr''', '''uk''', '''ur''', '''uz''', '''vi''', '''wo''', '''xh''', '''yi''', '''yo''', '''zh''', '''zu'''], '''wmt21''': ['''en''', '''ha''', '''is''', '''ja''', '''cs''', '''ru''', '''zh''', '''de'''] } class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : Union[str, Any] = VOCAB_FILES_NAMES __lowerCamelCase : List[str] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase : Optional[int] = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase : Dict = ['''input_ids''', '''attention_mask'''] __lowerCamelCase : List[int] = [] __lowerCamelCase : List[int] = [] def __init__( self : List[Any] , __lowerCAmelCase : List[Any] , __lowerCAmelCase : Dict , __lowerCAmelCase : Union[str, Any]=None , __lowerCAmelCase : str=None , __lowerCAmelCase : List[Any]="<s>" , __lowerCAmelCase : List[Any]="</s>" , __lowerCAmelCase : Optional[int]="</s>" , __lowerCAmelCase : Optional[Any]="<pad>" , __lowerCAmelCase : Any="<unk>" , __lowerCAmelCase : Any="m2m100" , __lowerCAmelCase : Optional[Dict[str, Any]] = None , __lowerCAmelCase : Dict=8 , **__lowerCAmelCase : Tuple , ) -> None: """simple docstring""" A__ = {} if sp_model_kwargs is None else sp_model_kwargs A__ = language_codes A__ = FAIRSEQ_LANGUAGE_CODES[language_codes] A__ = {lang_code: f'__{lang_code}__' for lang_code in fairseq_language_code} A__ = kwargs.get("""additional_special_tokens""" , [] ) kwargs["additional_special_tokens"] += [ self.get_lang_token(__lowerCAmelCase ) for lang_code in fairseq_language_code if self.get_lang_token(__lowerCAmelCase ) not in kwargs["additional_special_tokens"] ] super().__init__( src_lang=__lowerCAmelCase , tgt_lang=__lowerCAmelCase , bos_token=__lowerCAmelCase , eos_token=__lowerCAmelCase , sep_token=__lowerCAmelCase , unk_token=__lowerCAmelCase , pad_token=__lowerCAmelCase , language_codes=__lowerCAmelCase , sp_model_kwargs=self.sp_model_kwargs , num_madeup_words=__lowerCAmelCase , **__lowerCAmelCase , ) A__ = vocab_file A__ = load_json(__lowerCAmelCase ) A__ = {v: k for k, v in self.encoder.items()} A__ = spm_file A__ = load_spm(__lowerCAmelCase , self.sp_model_kwargs ) A__ = len(self.encoder ) A__ = { self.get_lang_token(__lowerCAmelCase ): self.encoder_size + i for i, lang_code in enumerate(__lowerCAmelCase ) } A__ = {lang_code: self.encoder_size + i for i, lang_code in enumerate(__lowerCAmelCase )} A__ = {v: k for k, v in self.lang_token_to_id.items()} A__ = src_lang if src_lang is not None else """en""" A__ = tgt_lang A__ = self.get_lang_id(self._src_lang ) self.set_src_lang_special_tokens(self._src_lang ) A__ = num_madeup_words @property def a_ ( self : Optional[int] ) -> int: """simple docstring""" return len(self.encoder ) + len(self.lang_token_to_id ) @property def a_ ( self : Optional[Any] ) -> str: """simple docstring""" return self._src_lang @src_lang.setter def a_ ( self : List[Any] , __lowerCAmelCase : str ) -> None: """simple docstring""" A__ = new_src_lang self.set_src_lang_special_tokens(self._src_lang ) def a_ ( self : Optional[int] , __lowerCAmelCase : str ) -> List[str]: """simple docstring""" return self.sp_model.encode(__lowerCAmelCase , out_type=__lowerCAmelCase ) def a_ ( self : Optional[Any] , __lowerCAmelCase : Dict ) -> Optional[Any]: """simple docstring""" if token in self.lang_token_to_id: return self.lang_token_to_id[token] return self.encoder.get(__lowerCAmelCase , self.encoder[self.unk_token] ) def a_ ( self : Optional[int] , __lowerCAmelCase : int ) -> str: """simple docstring""" if index in self.id_to_lang_token: return self.id_to_lang_token[index] return self.decoder.get(__lowerCAmelCase , self.unk_token ) def a_ ( self : Optional[int] , __lowerCAmelCase : Dict ) -> str: """simple docstring""" A__ = [] A__ = """""" 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(__lowerCAmelCase ) + token A__ = [] else: current_sub_tokens.append(__lowerCAmelCase ) out_string += self.sp_model.decode(__lowerCAmelCase ) return out_string.strip() def a_ ( self : List[str] , __lowerCAmelCase : List[int] , __lowerCAmelCase : Optional[List[int]] = None , __lowerCAmelCase : bool = False ) -> List[int]: """simple docstring""" if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=__lowerCAmelCase , token_ids_a=__lowerCAmelCase , already_has_special_tokens=__lowerCAmelCase ) A__ = [1] * len(self.prefix_tokens ) A__ = [1] * len(self.suffix_tokens ) if token_ids_a is None: return prefix_ones + ([0] * len(__lowerCAmelCase )) + suffix_ones return prefix_ones + ([0] * len(__lowerCAmelCase )) + ([0] * len(__lowerCAmelCase )) + suffix_ones def a_ ( self : Tuple , __lowerCAmelCase : List[int] , __lowerCAmelCase : Optional[List[int]] = None ) -> List[int]: """simple docstring""" if token_ids_a is None: return self.prefix_tokens + token_ids_a + self.suffix_tokens # We don't expect to process pairs, but leave the pair logic for API consistency return self.prefix_tokens + token_ids_a + token_ids_a + self.suffix_tokens def a_ ( self : int ) -> Dict: """simple docstring""" A__ = {self.convert_ids_to_tokens(__lowerCAmelCase ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self : Union[str, Any] ) -> Dict: """simple docstring""" A__ = self.__dict__.copy() A__ = None return state def __setstate__( self : str , __lowerCAmelCase : Dict ) -> None: """simple docstring""" A__ = d # for backward compatibility if not hasattr(self , """sp_model_kwargs""" ): A__ = {} A__ = load_spm(self.spm_file , self.sp_model_kwargs ) def a_ ( self : List[str] , __lowerCAmelCase : str , __lowerCAmelCase : Optional[str] = None ) -> Tuple[str]: """simple docstring""" A__ = Path(__lowerCAmelCase ) if not save_dir.is_dir(): raise OSError(f'{save_directory} should be a directory' ) A__ = save_dir / ( (filename_prefix + """-""" if filename_prefix else """""") + self.vocab_files_names["""vocab_file"""] ) A__ = save_dir / ( (filename_prefix + """-""" if filename_prefix else """""") + self.vocab_files_names["""spm_file"""] ) save_json(self.encoder , __lowerCAmelCase ) if os.path.abspath(self.spm_file ) != os.path.abspath(__lowerCAmelCase ) and os.path.isfile(self.spm_file ): copyfile(self.spm_file , __lowerCAmelCase ) elif not os.path.isfile(self.spm_file ): with open(__lowerCAmelCase , """wb""" ) as fi: A__ = self.sp_model.serialized_model_proto() fi.write(__lowerCAmelCase ) return (str(__lowerCAmelCase ), str(__lowerCAmelCase )) def a_ ( self : str , __lowerCAmelCase : List[str] , __lowerCAmelCase : str = "en" , __lowerCAmelCase : Optional[List[str]] = None , __lowerCAmelCase : str = "ro" , **__lowerCAmelCase : List[Any] , ) -> BatchEncoding: """simple docstring""" A__ = src_lang A__ = tgt_lang self.set_src_lang_special_tokens(self.src_lang ) return super().prepare_seqaseq_batch(__lowerCAmelCase , __lowerCAmelCase , **__lowerCAmelCase ) def a_ ( self : Optional[int] , __lowerCAmelCase : Any , __lowerCAmelCase : Optional[str] , __lowerCAmelCase : Optional[str] , **__lowerCAmelCase : Tuple ) -> Tuple: """simple docstring""" if src_lang is None or tgt_lang is None: raise ValueError("""Translation requires a `src_lang` and a `tgt_lang` for this model""" ) A__ = src_lang A__ = self(__lowerCAmelCase , add_special_tokens=__lowerCAmelCase , **__lowerCAmelCase ) A__ = self.get_lang_id(__lowerCAmelCase ) A__ = tgt_lang_id return inputs def a_ ( self : Dict ) -> int: """simple docstring""" self.set_src_lang_special_tokens(self.src_lang ) def a_ ( self : Union[str, Any] ) -> Union[str, Any]: """simple docstring""" self.set_tgt_lang_special_tokens(self.tgt_lang ) def a_ ( self : str , __lowerCAmelCase : str ) -> None: """simple docstring""" A__ = self.get_lang_token(__lowerCAmelCase ) A__ = self.lang_token_to_id[lang_token] A__ = [self.cur_lang_id] A__ = [self.eos_token_id] def a_ ( self : Tuple , __lowerCAmelCase : str ) -> None: """simple docstring""" A__ = self.get_lang_token(__lowerCAmelCase ) A__ = self.lang_token_to_id[lang_token] A__ = [self.cur_lang_id] A__ = [self.eos_token_id] def a_ ( self : Union[str, Any] , __lowerCAmelCase : str ) -> str: """simple docstring""" return self.lang_code_to_token[lang] def a_ ( self : Union[str, Any] , __lowerCAmelCase : str ) -> int: """simple docstring""" A__ = self.get_lang_token(__lowerCAmelCase ) return self.lang_token_to_id[lang_token] def __lowerCamelCase ( __a :str , __a :Dict[str, Any] ) -> sentencepiece.SentencePieceProcessor: """simple docstring""" A__ = sentencepiece.SentencePieceProcessor(**__a ) spm.Load(str(__a ) ) return spm def __lowerCamelCase ( __a :str ) -> Union[Dict, List]: """simple docstring""" with open(__a , """r""" ) as f: return json.load(__a ) def __lowerCamelCase ( __a :List[Any] , __a :str ) -> None: """simple docstring""" with open(__a , """w""" ) as f: json.dump(__a , __a , indent=2 )
274
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available _lowerCAmelCase = { '''configuration_clipseg''': [ '''CLIPSEG_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''CLIPSegConfig''', '''CLIPSegTextConfig''', '''CLIPSegVisionConfig''', ], '''processing_clipseg''': ['''CLIPSegProcessor'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCAmelCase = [ '''CLIPSEG_PRETRAINED_MODEL_ARCHIVE_LIST''', '''CLIPSegModel''', '''CLIPSegPreTrainedModel''', '''CLIPSegTextModel''', '''CLIPSegVisionModel''', '''CLIPSegForImageSegmentation''', ] if TYPE_CHECKING: from .configuration_clipseg import ( CLIPSEG_PRETRAINED_CONFIG_ARCHIVE_MAP, CLIPSegConfig, CLIPSegTextConfig, CLIPSegVisionConfig, ) from .processing_clipseg import CLIPSegProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_clipseg import ( CLIPSEG_PRETRAINED_MODEL_ARCHIVE_LIST, CLIPSegForImageSegmentation, CLIPSegModel, CLIPSegPreTrainedModel, CLIPSegTextModel, CLIPSegVisionModel, ) else: import sys _lowerCAmelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
298
from __future__ import annotations from PIL import Image # Define glider example A : Any = [ [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [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], ] # Define blinker example A : Optional[Any] = [[0, 1, 0], [0, 1, 0], [0, 1, 0]] def __lowerCamelCase ( __a :list[list[int]] ) -> list[list[int]]: """simple docstring""" A__ = [] for i in range(len(__a ) ): A__ = [] for j in range(len(cells[i] ) ): # Get the number of live neighbours A__ = 0 if i > 0 and j > 0: neighbour_count += cells[i - 1][j - 1] if i > 0: neighbour_count += cells[i - 1][j] if i > 0 and j < len(cells[i] ) - 1: neighbour_count += cells[i - 1][j + 1] if j > 0: neighbour_count += cells[i][j - 1] if j < len(cells[i] ) - 1: neighbour_count += cells[i][j + 1] if i < len(__a ) - 1 and j > 0: neighbour_count += cells[i + 1][j - 1] if i < len(__a ) - 1: neighbour_count += cells[i + 1][j] if i < len(__a ) - 1 and j < len(cells[i] ) - 1: neighbour_count += cells[i + 1][j + 1] # Rules of the game of life (excerpt from Wikipedia): # 1. Any live cell with two or three live neighbours survives. # 2. Any dead cell with three live neighbours becomes a live cell. # 3. All other live cells die in the next generation. # Similarly, all other dead cells stay dead. A__ = cells[i][j] == 1 if ( (alive and 2 <= neighbour_count <= 3) or not alive and neighbour_count == 3 ): next_generation_row.append(1 ) else: next_generation_row.append(0 ) next_generation.append(__a ) return next_generation def __lowerCamelCase ( __a :list[list[int]] , __a :int ) -> list[Image.Image]: """simple docstring""" A__ = [] for _ in range(__a ): # Create output image A__ = Image.new("""RGB""" , (len(cells[0] ), len(__a )) ) A__ = img.load() # Save cells to image for x in range(len(__a ) ): for y in range(len(cells[0] ) ): A__ = 2_5_5 - cells[y][x] * 2_5_5 A__ = (colour, colour, colour) # Save image images.append(__a ) A__ = new_generation(__a ) return images if __name__ == "__main__": A : str = generate_images(GLIDER, 1_6) images[0].save('''out.gif''', save_all=True, append_images=images[1:])
274
0
"""simple docstring""" import inspect import math import tempfile import unittest import numpy as np from transformers import ViTMAEConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTMAEForPreTraining, ViTMAEModel from transformers.models.vit.modeling_vit import VIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class UpperCamelCase : def __init__( self : Any , UpperCAmelCase__ : Optional[Any] , UpperCAmelCase__ : Optional[int]=13 , UpperCAmelCase__ : Optional[int]=30 , UpperCAmelCase__ : Optional[int]=2 , UpperCAmelCase__ : List[Any]=3 , UpperCAmelCase__ : Optional[Any]=True , UpperCAmelCase__ : List[str]=True , UpperCAmelCase__ : Tuple=32 , UpperCAmelCase__ : int=5 , UpperCAmelCase__ : Tuple=4 , UpperCAmelCase__ : Optional[int]=37 , UpperCAmelCase__ : List[Any]="gelu" , UpperCAmelCase__ : Any=0.1 , UpperCAmelCase__ : int=0.1 , UpperCAmelCase__ : Dict=10 , UpperCAmelCase__ : List[Any]=0.0_2 , UpperCAmelCase__ : Union[str, Any]=3 , UpperCAmelCase__ : Optional[Any]=0.6 , UpperCAmelCase__ : Optional[int]=None , ) -> str: _a : Dict = parent _a : str = batch_size _a : Tuple = image_size _a : Any = patch_size _a : Union[str, Any] = num_channels _a : List[str] = is_training _a : Union[str, Any] = use_labels _a : Optional[int] = hidden_size _a : Optional[int] = num_hidden_layers _a : List[Any] = num_attention_heads _a : Dict = intermediate_size _a : str = hidden_act _a : Any = hidden_dropout_prob _a : Dict = attention_probs_dropout_prob _a : Any = type_sequence_label_size _a : List[str] = initializer_range _a : List[Any] = mask_ratio _a : List[str] = scope # in ViTMAE, the expected sequence length = (num_patches + 1) * (1 - config.mask_ratio), rounded above # (we add 1 for the [CLS] token) _a : Optional[int] = (image_size // patch_size) ** 2 _a : Any = int(math.ceil((1 - mask_ratio) * (num_patches + 1) ) ) def _lowercase ( self : int ) -> Union[str, Any]: _a : Union[str, Any] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) _a : List[Any] = None if self.use_labels: _a : List[Any] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _a : List[Any] = self.get_config() return config, pixel_values, labels def _lowercase ( self : Union[str, Any] ) -> Tuple: return ViTMAEConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=__lowerCAmelCase , initializer_range=self.initializer_range , mask_ratio=self.mask_ratio , ) def _lowercase ( self : int , UpperCAmelCase__ : int , UpperCAmelCase__ : Any , UpperCAmelCase__ : List[Any] ) -> List[Any]: _a : int = ViTMAEModel(config=__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.eval() _a : Tuple = model(__lowerCAmelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _lowercase ( self : Union[str, Any] , UpperCAmelCase__ : str , UpperCAmelCase__ : Union[str, Any] , UpperCAmelCase__ : str ) -> Any: _a : Tuple = ViTMAEForPreTraining(__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.eval() _a : List[str] = model(__lowerCAmelCase ) _a : Optional[int] = (self.image_size // self.patch_size) ** 2 _a : Dict = self.patch_size**2 * self.num_channels self.parent.assertEqual(result.logits.shape , (self.batch_size, num_patches, expected_num_channels) ) # test greyscale images _a : Union[str, Any] = 1 _a : Dict = ViTMAEForPreTraining(__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.eval() _a : Tuple = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) _a : List[Any] = model(__lowerCAmelCase ) _a : Tuple = self.patch_size**2 self.parent.assertEqual(result.logits.shape , (self.batch_size, num_patches, expected_num_channels) ) def _lowercase ( self : Optional[int] ) -> List[str]: _a : Tuple = self.prepare_config_and_inputs() _a , _a , _a : str = config_and_inputs _a : Optional[Any] = {"""pixel_values""": pixel_values} return config, inputs_dict @require_torch class UpperCamelCase ( snake_case_ , snake_case_ , unittest.TestCase ): UpperCamelCase : int = (ViTMAEModel, ViTMAEForPreTraining) if is_torch_available() else () UpperCamelCase : Union[str, Any] = {'''feature-extraction''': ViTMAEModel} if is_torch_available() else {} UpperCamelCase : int = False UpperCamelCase : Optional[Any] = False UpperCamelCase : str = False UpperCamelCase : str = False def _lowercase ( self : Optional[int] ) -> Optional[Any]: _a : Optional[int] = ViTMAEModelTester(self ) _a : str = ConfigTester(self , config_class=__lowerCAmelCase , has_text_modality=__lowerCAmelCase , hidden_size=37 ) def _lowercase ( self : Any ) -> Any: self.config_tester.run_common_tests() @unittest.skip(reason="""ViTMAE does not use inputs_embeds""" ) def _lowercase ( self : Optional[Any] ) -> Any: pass def _lowercase ( self : int ) -> int: _a , _a : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _a : Optional[int] = model_class(__lowerCAmelCase ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) _a : List[str] = model.get_output_embeddings() self.assertTrue(x is None or isinstance(__lowerCAmelCase , nn.Linear ) ) def _lowercase ( self : str ) -> Any: _a , _a : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _a : Tuple = model_class(__lowerCAmelCase ) _a : Union[str, Any] = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _a : Any = [*signature.parameters.keys()] _a : Optional[Any] = ["""pixel_values"""] self.assertListEqual(arg_names[:1] , __lowerCAmelCase ) def _lowercase ( self : Dict ) -> Tuple: _a : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__lowerCAmelCase ) def _lowercase ( self : List[Any] ) -> Optional[Any]: _a : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_pretraining(*__lowerCAmelCase ) def _lowercase ( self : Optional[int] , UpperCAmelCase__ : str , UpperCAmelCase__ : Any , UpperCAmelCase__ : Optional[int] ) -> str: np.random.seed(2 ) _a : List[str] = int((pt_model.config.image_size // pt_model.config.patch_size) ** 2 ) _a : List[str] = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) _a : Dict = torch.from_numpy(__lowerCAmelCase ) # Add `noise` argument. # PT inputs will be prepared in `super().check_pt_tf_models()` with this added `noise` argument _a : List[Any] = pt_noise super().check_pt_tf_models(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) def _lowercase ( self : Optional[Any] ) -> int: _a , _a : Dict = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _a : List[str] = model_class(__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.eval() # make random mask reproducible torch.manual_seed(2 ) with torch.no_grad(): _a : int = model(**self._prepare_for_class(__lowerCAmelCase , __lowerCAmelCase ) ) _a : List[str] = outputs[0].cpu().numpy() _a : Tuple = 0 with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(__lowerCAmelCase ) _a : Dict = model_class.from_pretrained(__lowerCAmelCase ) model.to(__lowerCAmelCase ) # make random mask reproducible torch.manual_seed(2 ) with torch.no_grad(): _a : Dict = model(**self._prepare_for_class(__lowerCAmelCase , __lowerCAmelCase ) ) # Make sure we don't have nans _a : List[str] = after_outputs[0].cpu().numpy() _a : Optional[int] = 0 _a : List[Any] = np.amax(np.abs(out_a - out_a ) ) self.assertLessEqual(__lowerCAmelCase , 1E-5 ) @unittest.skip( reason="""ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load to get deterministic results.""" ) def _lowercase ( self : List[str] ) -> Optional[Any]: pass @unittest.skip( reason="""ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load to get deterministic results.""" ) def _lowercase ( self : str ) -> Dict: pass @unittest.skip( reason="""ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load to get deterministic results.""" ) def _lowercase ( self : int ) -> List[Any]: pass @unittest.skip(reason="""ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load""" ) def _lowercase ( self : Optional[int] ) -> Union[str, Any]: pass @unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests.""" ) def _lowercase ( self : int ) -> Union[str, Any]: pass @slow def _lowercase ( self : Dict ) -> List[Any]: for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _a : Optional[int] = ViTMAEModel.from_pretrained(__lowerCAmelCase ) self.assertIsNotNone(__lowerCAmelCase ) def lowerCAmelCase__ ( ): '''simple docstring''' _a : str = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) return image @require_torch @require_vision class UpperCamelCase ( unittest.TestCase ): @cached_property def _lowercase ( self : Optional[Any] ) -> int: return ViTImageProcessor.from_pretrained("""facebook/vit-mae-base""" ) if is_vision_available() else None @slow def _lowercase ( self : int ) -> Union[str, Any]: np.random.seed(2 ) _a : Dict = ViTMAEForPreTraining.from_pretrained("""facebook/vit-mae-base""" ).to(__lowerCAmelCase ) _a : Any = self.default_image_processor _a : Tuple = prepare_img() _a : Dict = image_processor(images=__lowerCAmelCase , return_tensors="""pt""" ).to(__lowerCAmelCase ) # prepare a noise vector that will be also used for testing the TF model # (this way we can ensure that the PT and TF models operate on the same inputs) _a : List[Any] = ViTMAEConfig() _a : Union[str, Any] = int((vit_mae_config.image_size // vit_mae_config.patch_size) ** 2 ) _a : Any = np.random.uniform(size=(1, num_patches) ) # forward pass with torch.no_grad(): _a : Optional[Any] = model(**__lowerCAmelCase , noise=torch.from_numpy(__lowerCAmelCase ).to(device=__lowerCAmelCase ) ) # verify the logits _a : int = torch.Size((1, 196, 768) ) self.assertEqual(outputs.logits.shape , __lowerCAmelCase ) _a : Optional[int] = torch.tensor( [[-0.0_5_4_8, -1.7_0_2_3, -0.9_3_2_5], [0.3_7_2_1, -0.5_6_7_0, -0.2_2_3_3], [0.8_2_3_5, -1.3_8_7_8, -0.3_5_2_4]] ) self.assertTrue(torch.allclose(outputs.logits[0, :3, :3] , expected_slice.to(__lowerCAmelCase ) , atol=1E-4 ) )
294
from datetime import datetime import requests from bsa import BeautifulSoup if __name__ == "__main__": A : List[str] = input('''Enter image url: ''').strip() print(F'''Downloading image from {url} ...''') A : Any = BeautifulSoup(requests.get(url).content, '''html.parser''') # The image URL is in the content field of the first meta tag with property og:image A : List[Any] = soup.find('''meta''', {'''property''': '''og:image'''})['''content'''] A : Dict = requests.get(image_url).content A : Tuple = F'''{datetime.now():%Y-%m-%d_%H:%M:%S}.jpg''' with open(file_name, '''wb''') as fp: fp.write(image_data) print(F'''Done. Image saved to disk as {file_name}.''')
274
0
import os from pickle import UnpicklingError from typing import Dict, Tuple import jax import jax.numpy as jnp import numpy as np from flax.serialization import from_bytes from flax.traverse_util import flatten_dict, unflatten_dict import transformers from .utils import logging __UpperCAmelCase = logging.get_logger(__name__) def lowercase__ ( __snake_case : int , __snake_case : List[str] , __snake_case : Optional[Any] , __snake_case : Tuple=False ): '''simple docstring''' try: import torch # noqa: F401 except ImportError: logger.error( 'Loading a PyTorch model in Flax, requires both PyTorch and Flax to be installed. Please see' ' https://pytorch.org/ and https://flax.readthedocs.io/en/latest/installation.html for installation' ' instructions.' ) raise if not is_sharded: UpperCAmelCase_ : List[str] = os.path.abspath(__a ) logger.info(F"Loading PyTorch weights from {pt_path}" ) UpperCAmelCase_ : Dict = torch.load(__a , map_location='cpu' ) logger.info(F"PyTorch checkpoint contains {sum(t.numel() for t in pt_state_dict.values() ):,} parameters." ) UpperCAmelCase_ : Dict = convert_pytorch_state_dict_to_flax(__a , __a ) else: # model is sharded and pytorch_checkpoint_path already contains the list of .pt shard files UpperCAmelCase_ : Optional[Any] = convert_pytorch_sharded_state_dict_to_flax(__a , __a ) return flax_state_dict def lowercase__ ( __snake_case : Tuple[str] , __snake_case : np.ndarray , __snake_case : Dict[str, jnp.ndarray] , __snake_case : str , ): '''simple docstring''' def is_key_or_prefix_key_in_dict(__snake_case : Tuple[str] ) -> bool: return len(set(__a ) & {key, (model_prefix,) + key} ) > 0 # layer norm UpperCAmelCase_ : Union[str, Any] = pt_tuple_key[:-1] + ('scale',) if pt_tuple_key[-1] in ["weight", "gamma"] and is_key_or_prefix_key_in_dict(__a ): return renamed_pt_tuple_key, pt_tensor # batch norm layer mean UpperCAmelCase_ : Tuple = pt_tuple_key[:-1] + ('mean',) if pt_tuple_key[-1] == "running_mean" and not is_key_or_prefix_key_in_dict(__a ): return renamed_pt_tuple_key, pt_tensor # batch norm layer var UpperCAmelCase_ : str = pt_tuple_key[:-1] + ('var',) if pt_tuple_key[-1] == "running_var" and not is_key_or_prefix_key_in_dict(__a ): return renamed_pt_tuple_key, pt_tensor # embedding UpperCAmelCase_ : List[Any] = pt_tuple_key[:-1] + ('embedding',) if pt_tuple_key[-1] == "weight" and is_key_or_prefix_key_in_dict(__a ): return renamed_pt_tuple_key, pt_tensor # conv layer UpperCAmelCase_ : int = pt_tuple_key[:-1] + ('kernel',) if pt_tuple_key[-1] == "weight" and pt_tensor.ndim == 4 and not is_key_or_prefix_key_in_dict(__a ): UpperCAmelCase_ : List[Any] = pt_tensor.transpose(2 , 3 , 1 , 0 ) return renamed_pt_tuple_key, pt_tensor # linear layer UpperCAmelCase_ : Dict = pt_tuple_key[:-1] + ('kernel',) if pt_tuple_key[-1] == "weight" and not is_key_or_prefix_key_in_dict(__a ): UpperCAmelCase_ : str = pt_tensor.T return renamed_pt_tuple_key, pt_tensor # old PyTorch layer norm weight UpperCAmelCase_ : List[str] = pt_tuple_key[:-1] + ('weight',) if pt_tuple_key[-1] == "gamma": return renamed_pt_tuple_key, pt_tensor # old PyTorch layer norm bias UpperCAmelCase_ : Optional[int] = pt_tuple_key[:-1] + ('bias',) if pt_tuple_key[-1] == "beta": return renamed_pt_tuple_key, pt_tensor # New `weight_norm` from https://github.com/huggingface/transformers/pull/24030 UpperCAmelCase_ : List[Any] = None if pt_tuple_key[-3::2] == ("parametrizations", "original0"): UpperCAmelCase_ : int = pt_tuple_key[-2] + '_g' elif pt_tuple_key[-3::2] == ("parametrizations", "original1"): UpperCAmelCase_ : Dict = pt_tuple_key[-2] + '_v' if name is not None: UpperCAmelCase_ : Optional[int] = pt_tuple_key[:-3] + (name,) return renamed_pt_tuple_key, pt_tensor return pt_tuple_key, pt_tensor def lowercase__ ( __snake_case : Dict , __snake_case : Optional[Any] ): '''simple docstring''' UpperCAmelCase_ : List[str] = {k: v.numpy() for k, v in pt_state_dict.items()} UpperCAmelCase_ : str = flax_model.base_model_prefix # use params dict if the model contains batch norm layers if "params" in flax_model.params: UpperCAmelCase_ : str = flax_model.params['params'] else: UpperCAmelCase_ : Optional[Any] = flax_model.params UpperCAmelCase_ : int = flatten_dict(__a ) # add batch_stats keys,values to dict if "batch_stats" in flax_model.params: UpperCAmelCase_ : Dict = flatten_dict(flax_model.params['batch_stats'] ) random_flax_state_dict.update(__a ) UpperCAmelCase_ : Tuple = {} UpperCAmelCase_ : int = (model_prefix not in flax_model_params) and ( model_prefix in {k.split('.' )[0] for k in pt_state_dict.keys()} ) UpperCAmelCase_ : Union[str, Any] = (model_prefix in flax_model_params) and ( model_prefix not in {k.split('.' )[0] for k in pt_state_dict.keys()} ) # Need to change some parameters name to match Flax names for pt_key, pt_tensor in pt_state_dict.items(): UpperCAmelCase_ : Tuple = tuple(pt_key.split('.' ) ) # remove base model prefix if necessary UpperCAmelCase_ : List[str] = pt_tuple_key[0] == model_prefix if load_model_with_head_into_base_model and has_base_model_prefix: UpperCAmelCase_ : int = pt_tuple_key[1:] # Correctly rename weight parameters UpperCAmelCase_ , UpperCAmelCase_ : Any = rename_key_and_reshape_tensor( __a , __a , __a , __a ) # add model prefix if necessary UpperCAmelCase_ : List[str] = (model_prefix,) + flax_key in random_flax_state_dict if load_base_model_into_model_with_head and require_base_model_prefix: UpperCAmelCase_ : List[str] = (model_prefix,) + flax_key if flax_key in random_flax_state_dict: if flax_tensor.shape != random_flax_state_dict[flax_key].shape: raise ValueError( F"PyTorch checkpoint seems to be incorrect. Weight {pt_key} was expected to be of shape " F"{random_flax_state_dict[flax_key].shape}, but is {flax_tensor.shape}." ) # add batch stats if the model contains batchnorm layers if "batch_stats" in flax_model.params: if "mean" in flax_key[-1] or "var" in flax_key[-1]: UpperCAmelCase_ : Optional[Any] = jnp.asarray(__a ) continue # remove num_batches_tracked key if "num_batches_tracked" in flax_key[-1]: flax_state_dict.pop(__a , __a ) continue # also add unexpected weight so that warning is thrown UpperCAmelCase_ : int = jnp.asarray(__a ) else: # also add unexpected weight so that warning is thrown UpperCAmelCase_ : Any = jnp.asarray(__a ) return unflatten_dict(__a ) def lowercase__ ( __snake_case : Optional[int] , __snake_case : int ): '''simple docstring''' import torch # Load the index UpperCAmelCase_ : int = {} for shard_file in shard_filenames: # load using msgpack utils UpperCAmelCase_ : Optional[Any] = torch.load(__a ) UpperCAmelCase_ : Optional[Any] = {k: v.numpy() for k, v in pt_state_dict.items()} UpperCAmelCase_ : Dict = flax_model.base_model_prefix # use params dict if the model contains batch norm layers and then add batch_stats keys,values to dict if "batch_stats" in flax_model.params: UpperCAmelCase_ : Optional[Any] = flax_model.params['params'] UpperCAmelCase_ : List[str] = flatten_dict(__a ) random_flax_state_dict.update(flatten_dict(flax_model.params['batch_stats'] ) ) else: UpperCAmelCase_ : Dict = flax_model.params UpperCAmelCase_ : Optional[Any] = flatten_dict(__a ) UpperCAmelCase_ : Optional[int] = (model_prefix not in flax_model_params) and ( model_prefix in {k.split('.' )[0] for k in pt_state_dict.keys()} ) UpperCAmelCase_ : int = (model_prefix in flax_model_params) and ( model_prefix not in {k.split('.' )[0] for k in pt_state_dict.keys()} ) # Need to change some parameters name to match Flax names for pt_key, pt_tensor in pt_state_dict.items(): UpperCAmelCase_ : Any = tuple(pt_key.split('.' ) ) # remove base model prefix if necessary UpperCAmelCase_ : Union[str, Any] = pt_tuple_key[0] == model_prefix if load_model_with_head_into_base_model and has_base_model_prefix: UpperCAmelCase_ : int = pt_tuple_key[1:] # Correctly rename weight parameters UpperCAmelCase_ , UpperCAmelCase_ : Dict = rename_key_and_reshape_tensor( __a , __a , __a , __a ) # add model prefix if necessary UpperCAmelCase_ : Any = (model_prefix,) + flax_key in random_flax_state_dict if load_base_model_into_model_with_head and require_base_model_prefix: UpperCAmelCase_ : int = (model_prefix,) + flax_key if flax_key in random_flax_state_dict: if flax_tensor.shape != random_flax_state_dict[flax_key].shape: raise ValueError( F"PyTorch checkpoint seems to be incorrect. Weight {pt_key} was expected to be of shape " F"{random_flax_state_dict[flax_key].shape}, but is {flax_tensor.shape}." ) # add batch stats if the model contains batchnorm layers if "batch_stats" in flax_model.params: if "mean" in flax_key[-1]: UpperCAmelCase_ : List[str] = jnp.asarray(__a ) continue if "var" in flax_key[-1]: UpperCAmelCase_ : Union[str, Any] = jnp.asarray(__a ) continue # remove num_batches_tracked key if "num_batches_tracked" in flax_key[-1]: flax_state_dict.pop(__a , __a ) continue # also add unexpected weight so that warning is thrown UpperCAmelCase_ : Optional[Any] = jnp.asarray(__a ) else: # also add unexpected weight so that warning is thrown UpperCAmelCase_ : Any = jnp.asarray(__a ) return unflatten_dict(__a ) def lowercase__ ( __snake_case : Union[str, Any] , __snake_case : Dict ): '''simple docstring''' UpperCAmelCase_ : Optional[int] = os.path.abspath(__a ) logger.info(F"Loading Flax weights from {flax_checkpoint_path}" ) # import correct flax class UpperCAmelCase_ : Optional[Any] = getattr(__a , 'Flax' + model.__class__.__name__ ) # load flax weight dict with open(__a , 'rb' ) as state_f: try: UpperCAmelCase_ : Any = from_bytes(__a , state_f.read() ) except UnpicklingError: raise EnvironmentError(F"Unable to convert {flax_checkpoint_path} to Flax deserializable object. " ) return load_flax_weights_in_pytorch_model(__a , __a ) def lowercase__ ( __snake_case : str , __snake_case : Tuple ): '''simple docstring''' try: import torch # noqa: F401 except ImportError: logger.error( 'Loading a Flax weights in PyTorch, requires both PyTorch and Flax to be installed. Please see' ' https://pytorch.org/ and https://flax.readthedocs.io/en/latest/installation.html for installation' ' instructions.' ) raise # check if we have bf16 weights UpperCAmelCase_ : List[str] = flatten_dict(jax.tree_util.tree_map(lambda __snake_case : x.dtype == jnp.bfloataa , __a ) ).values() if any(__a ): # convert all weights to fp32 if the are bf16 since torch.from_numpy can-not handle bf16 # and bf16 is not fully supported in PT yet. logger.warning( 'Found ``bfloat16`` weights in Flax model. Casting all ``bfloat16`` weights to ``float32`` ' 'before loading those in PyTorch model.' ) UpperCAmelCase_ : List[Any] = jax.tree_util.tree_map( lambda __snake_case : params.astype(np.floataa ) if params.dtype == jnp.bfloataa else params , __a ) UpperCAmelCase_ : Optional[int] = flatten_dict(__a ) UpperCAmelCase_ : int = pt_model.state_dict() UpperCAmelCase_ : Tuple = (pt_model.base_model_prefix in flax_state) and ( pt_model.base_model_prefix not in {k.split('.' )[0] for k in pt_model_dict.keys()} ) UpperCAmelCase_ : Union[str, Any] = (pt_model.base_model_prefix not in flax_state) and ( pt_model.base_model_prefix in {k.split('.' )[0] for k in pt_model_dict.keys()} ) # keep track of unexpected & missing keys UpperCAmelCase_ : Tuple = [] UpperCAmelCase_ : Dict = set(pt_model_dict.keys() ) for flax_key_tuple, flax_tensor in flax_state_dict.items(): UpperCAmelCase_ : List[str] = flax_key_tuple[0] == pt_model.base_model_prefix UpperCAmelCase_ : Union[str, Any] = '.'.join((pt_model.base_model_prefix,) + flax_key_tuple ) in pt_model_dict # adapt flax_key to prepare for loading from/to base model only if load_model_with_head_into_base_model and has_base_model_prefix: UpperCAmelCase_ : str = flax_key_tuple[1:] elif load_base_model_into_model_with_head and require_base_model_prefix: UpperCAmelCase_ : Optional[int] = (pt_model.base_model_prefix,) + flax_key_tuple # rename flax weights to PyTorch format if flax_key_tuple[-1] == "kernel" and flax_tensor.ndim == 4 and ".".join(__a ) not in pt_model_dict: # conv layer UpperCAmelCase_ : Tuple = flax_key_tuple[:-1] + ('weight',) UpperCAmelCase_ : int = jnp.transpose(__a , (3, 2, 0, 1) ) elif flax_key_tuple[-1] == "kernel" and ".".join(__a ) not in pt_model_dict: # linear layer UpperCAmelCase_ : Tuple = flax_key_tuple[:-1] + ('weight',) UpperCAmelCase_ : Union[str, Any] = flax_tensor.T elif flax_key_tuple[-1] in ["scale", "embedding"]: UpperCAmelCase_ : int = flax_key_tuple[:-1] + ('weight',) # adding batch stats from flax batch norm to pt elif "mean" in flax_key_tuple[-1]: UpperCAmelCase_ : int = flax_key_tuple[:-1] + ('running_mean',) elif "var" in flax_key_tuple[-1]: UpperCAmelCase_ : Dict = flax_key_tuple[:-1] + ('running_var',) if "batch_stats" in flax_state: UpperCAmelCase_ : Tuple = '.'.join(flax_key_tuple[1:] ) # Remove the params/batch_stats header else: UpperCAmelCase_ : List[str] = '.'.join(__a ) # We also need to look at `pt_model_dict` and see if there are keys requiring further transformation. UpperCAmelCase_ : Optional[Any] = {} # New `weight_norm` from https://github.com/huggingface/transformers/pull/24030 for key in pt_model_dict: UpperCAmelCase_ : Union[str, Any] = key.split('.' ) UpperCAmelCase_ : Tuple = None if key_components[-3::2] == ["parametrizations", "original0"]: UpperCAmelCase_ : List[Any] = key_components[-2] + '_g' elif key_components[-3::2] == ["parametrizations", "original1"]: UpperCAmelCase_ : Optional[Any] = key_components[-2] + '_v' if name is not None: UpperCAmelCase_ : List[str] = key_components[:-3] + [name] UpperCAmelCase_ : List[str] = '.'.join(__a ) UpperCAmelCase_ : Union[str, Any] = key if flax_key in special_pt_names: UpperCAmelCase_ : Union[str, Any] = special_pt_names[flax_key] if flax_key in pt_model_dict: if flax_tensor.shape != pt_model_dict[flax_key].shape: raise ValueError( F"Flax checkpoint seems to be incorrect. Weight {flax_key_tuple} was expected " F"to be of shape {pt_model_dict[flax_key].shape}, but is {flax_tensor.shape}." ) else: # add weight to pytorch dict UpperCAmelCase_ : Optional[int] = np.asarray(__a ) if not isinstance(__a , np.ndarray ) else flax_tensor UpperCAmelCase_ : Union[str, Any] = torch.from_numpy(__a ) # remove from missing keys missing_keys.remove(__a ) else: # weight is not expected by PyTorch model unexpected_keys.append(__a ) pt_model.load_state_dict(__a ) # re-transform missing_keys to list UpperCAmelCase_ : List[str] = list(__a ) if len(__a ) > 0: logger.warning( 'Some weights of the Flax model were not used when initializing the PyTorch model' F" {pt_model.__class__.__name__}: {unexpected_keys}\n- This IS expected if you are initializing" F" {pt_model.__class__.__name__} from a Flax model trained on another task or with another architecture" ' (e.g. initializing a BertForSequenceClassification model from a FlaxBertForPreTraining model).\n- This' F" IS NOT expected if you are initializing {pt_model.__class__.__name__} from a Flax model that you expect" ' to be exactly identical (e.g. initializing a BertForSequenceClassification model from a' ' FlaxBertForSequenceClassification model).' ) else: logger.warning(F"All Flax model weights were used when initializing {pt_model.__class__.__name__}.\n" ) if len(__a ) > 0: logger.warning( F"Some weights of {pt_model.__class__.__name__} were not initialized from the Flax model and are newly" F" initialized: {missing_keys}\nYou should probably TRAIN this model on a down-stream task to be able to" ' use it for predictions and inference.' ) else: logger.warning( F"All the weights of {pt_model.__class__.__name__} were initialized from the Flax model.\n" 'If your task is similar to the task the model of the checkpoint was trained on, ' F"you can already use {pt_model.__class__.__name__} for predictions without further training." ) return pt_model
29
# This code is adapted from OpenAI's release # https://github.com/openai/human-eval/blob/master/human_eval/execution.py import contextlib import faulthandler import io import multiprocessing import os import platform import signal import tempfile def __lowerCamelCase ( __a :List[str] , __a :List[Any] , __a :Union[str, Any] , __a :List[Any] ) -> Dict: """simple docstring""" A__ = multiprocessing.Manager() A__ = manager.list() A__ = multiprocessing.Process(target=__a , args=(check_program, result, timeout) ) p.start() p.join(timeout=timeout + 1 ) if p.is_alive(): p.kill() if not result: result.append("""timed out""" ) return { "task_id": task_id, "passed": result[0] == "passed", "result": result[0], "completion_id": completion_id, } def __lowerCamelCase ( __a :Optional[Any] , __a :Any , __a :List[Any] ) -> Union[str, Any]: """simple docstring""" with create_tempdir(): # These system calls are needed when cleaning up tempdir. import os import shutil A__ = shutil.rmtree A__ = os.rmdir A__ = os.chdir # Disable functionalities that can make destructive changes to the test. reliability_guard() # Run program. try: A__ = {} with swallow_io(): with time_limit(__a ): exec(__a , __a ) result.append("""passed""" ) except TimeoutException: result.append("""timed out""" ) except BaseException as e: result.append(F'failed: {e}' ) # Needed for cleaning up. A__ = rmtree A__ = rmdir A__ = chdir @contextlib.contextmanager def __lowerCamelCase ( __a :List[str] ) -> Dict: """simple docstring""" def signal_handler(__a :List[Any] , __a :Optional[Any] ): raise TimeoutException("""Timed out!""" ) signal.setitimer(signal.ITIMER_REAL , __a ) signal.signal(signal.SIGALRM , __a ) try: yield finally: signal.setitimer(signal.ITIMER_REAL , 0 ) @contextlib.contextmanager def __lowerCamelCase ( ) -> Union[str, Any]: """simple docstring""" A__ = WriteOnlyStringIO() with contextlib.redirect_stdout(__a ): with contextlib.redirect_stderr(__a ): with redirect_stdin(__a ): yield @contextlib.contextmanager def __lowerCamelCase ( ) -> Dict: """simple docstring""" with tempfile.TemporaryDirectory() as dirname: with chdir(__a ): yield dirname class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' pass class A (io.StringIO ): '''simple docstring''' def a_ ( self : Any , *__lowerCAmelCase : List[str] , **__lowerCAmelCase : str ) -> Dict: """simple docstring""" raise OSError def a_ ( self : Optional[Any] , *__lowerCAmelCase : Any , **__lowerCAmelCase : Optional[int] ) -> str: """simple docstring""" raise OSError def a_ ( self : Optional[Any] , *__lowerCAmelCase : Any , **__lowerCAmelCase : Any ) -> int: """simple docstring""" raise OSError def a_ ( self : str , *__lowerCAmelCase : Any , **__lowerCAmelCase : Union[str, Any] ) -> int: """simple docstring""" return False class A (contextlib._RedirectStream ): # type: ignore '''simple docstring''' __lowerCamelCase : Union[str, Any] = '''stdin''' @contextlib.contextmanager def __lowerCamelCase ( __a :Union[str, Any] ) -> List[str]: """simple docstring""" if root == ".": yield return A__ = os.getcwd() os.chdir(__a ) try: yield except BaseException as exc: raise exc finally: os.chdir(__a ) def __lowerCamelCase ( __a :Union[str, Any]=None ) -> Dict: """simple docstring""" if maximum_memory_bytes is not None: import resource resource.setrlimit(resource.RLIMIT_AS , (maximum_memory_bytes, maximum_memory_bytes) ) resource.setrlimit(resource.RLIMIT_DATA , (maximum_memory_bytes, maximum_memory_bytes) ) if not platform.uname().system == "Darwin": resource.setrlimit(resource.RLIMIT_STACK , (maximum_memory_bytes, maximum_memory_bytes) ) faulthandler.disable() import builtins A__ = None A__ = None import os A__ = """1""" A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None import shutil A__ = None A__ = None A__ = None import subprocess A__ = None # type: ignore A__ = None import sys A__ = None A__ = None A__ = None A__ = None A__ = None
274
0
from typing import List from ...configuration_utils import PretrainedConfig from ...utils import logging a_ = logging.get_logger(__name__) a_ = { '''snap-research/efficientformer-l1-300''': ( '''https://huggingface.co/snap-research/efficientformer-l1-300/resolve/main/config.json''' ), } class lowercase__ ( _UpperCAmelCase ): a_ ='''efficientformer''' def __init__( self , __UpperCAmelCase = [3, 2, 6, 4] , __UpperCAmelCase = [48, 96, 224, 448] , __UpperCAmelCase = [True, True, True, True] , __UpperCAmelCase = 448 , __UpperCAmelCase = 32 , __UpperCAmelCase = 4 , __UpperCAmelCase = 7 , __UpperCAmelCase = 5 , __UpperCAmelCase = 8 , __UpperCAmelCase = 4 , __UpperCAmelCase = 0.0 , __UpperCAmelCase = 16 , __UpperCAmelCase = 3 , __UpperCAmelCase = 3 , __UpperCAmelCase = 3 , __UpperCAmelCase = 2 , __UpperCAmelCase = 1 , __UpperCAmelCase = 0.0 , __UpperCAmelCase = 1 , __UpperCAmelCase = True , __UpperCAmelCase = True , __UpperCAmelCase = 1E-5 , __UpperCAmelCase = "gelu" , __UpperCAmelCase = 0.02 , __UpperCAmelCase = 1E-1_2 , __UpperCAmelCase = 224 , __UpperCAmelCase = 1E-0_5 , **__UpperCAmelCase , )-> None: '''simple docstring''' super().__init__(**__lowerCAmelCase ) lowerCAmelCase__ = hidden_act lowerCAmelCase__ = hidden_dropout_prob lowerCAmelCase__ = hidden_sizes lowerCAmelCase__ = num_hidden_layers lowerCAmelCase__ = num_attention_heads lowerCAmelCase__ = initializer_range lowerCAmelCase__ = layer_norm_eps lowerCAmelCase__ = patch_size lowerCAmelCase__ = num_channels lowerCAmelCase__ = depths lowerCAmelCase__ = mlp_expansion_ratio lowerCAmelCase__ = downsamples lowerCAmelCase__ = dim lowerCAmelCase__ = key_dim lowerCAmelCase__ = attention_ratio lowerCAmelCase__ = resolution lowerCAmelCase__ = pool_size lowerCAmelCase__ = downsample_patch_size lowerCAmelCase__ = downsample_stride lowerCAmelCase__ = downsample_pad lowerCAmelCase__ = drop_path_rate lowerCAmelCase__ = num_metaad_blocks lowerCAmelCase__ = distillation lowerCAmelCase__ = use_layer_scale lowerCAmelCase__ = layer_scale_init_value lowerCAmelCase__ = image_size lowerCAmelCase__ = batch_norm_eps
340
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_albert import AlbertTokenizer else: A : Tuple = None A : Optional[Any] = logging.get_logger(__name__) A : Tuple = {'''vocab_file''': '''spiece.model''', '''tokenizer_file''': '''tokenizer.json'''} A : List[str] = { '''vocab_file''': { '''albert-base-v1''': '''https://huggingface.co/albert-base-v1/resolve/main/spiece.model''', '''albert-large-v1''': '''https://huggingface.co/albert-large-v1/resolve/main/spiece.model''', '''albert-xlarge-v1''': '''https://huggingface.co/albert-xlarge-v1/resolve/main/spiece.model''', '''albert-xxlarge-v1''': '''https://huggingface.co/albert-xxlarge-v1/resolve/main/spiece.model''', '''albert-base-v2''': '''https://huggingface.co/albert-base-v2/resolve/main/spiece.model''', '''albert-large-v2''': '''https://huggingface.co/albert-large-v2/resolve/main/spiece.model''', '''albert-xlarge-v2''': '''https://huggingface.co/albert-xlarge-v2/resolve/main/spiece.model''', '''albert-xxlarge-v2''': '''https://huggingface.co/albert-xxlarge-v2/resolve/main/spiece.model''', }, '''tokenizer_file''': { '''albert-base-v1''': '''https://huggingface.co/albert-base-v1/resolve/main/tokenizer.json''', '''albert-large-v1''': '''https://huggingface.co/albert-large-v1/resolve/main/tokenizer.json''', '''albert-xlarge-v1''': '''https://huggingface.co/albert-xlarge-v1/resolve/main/tokenizer.json''', '''albert-xxlarge-v1''': '''https://huggingface.co/albert-xxlarge-v1/resolve/main/tokenizer.json''', '''albert-base-v2''': '''https://huggingface.co/albert-base-v2/resolve/main/tokenizer.json''', '''albert-large-v2''': '''https://huggingface.co/albert-large-v2/resolve/main/tokenizer.json''', '''albert-xlarge-v2''': '''https://huggingface.co/albert-xlarge-v2/resolve/main/tokenizer.json''', '''albert-xxlarge-v2''': '''https://huggingface.co/albert-xxlarge-v2/resolve/main/tokenizer.json''', }, } A : List[str] = { '''albert-base-v1''': 5_1_2, '''albert-large-v1''': 5_1_2, '''albert-xlarge-v1''': 5_1_2, '''albert-xxlarge-v1''': 5_1_2, '''albert-base-v2''': 5_1_2, '''albert-large-v2''': 5_1_2, '''albert-xlarge-v2''': 5_1_2, '''albert-xxlarge-v2''': 5_1_2, } A : Optional[int] = '''▁''' class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : str = VOCAB_FILES_NAMES __lowerCamelCase : List[Any] = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase : Dict = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase : List[str] = AlbertTokenizer def __init__( self : Tuple , __lowerCAmelCase : Tuple=None , __lowerCAmelCase : List[str]=None , __lowerCAmelCase : Optional[Any]=True , __lowerCAmelCase : List[Any]=True , __lowerCAmelCase : str=False , __lowerCAmelCase : Union[str, Any]="[CLS]" , __lowerCAmelCase : int="[SEP]" , __lowerCAmelCase : Dict="<unk>" , __lowerCAmelCase : Dict="[SEP]" , __lowerCAmelCase : Union[str, Any]="<pad>" , __lowerCAmelCase : str="[CLS]" , __lowerCAmelCase : int="[MASK]" , **__lowerCAmelCase : Optional[Any] , ) -> Optional[Any]: """simple docstring""" A__ = ( AddedToken(__lowerCAmelCase , lstrip=__lowerCAmelCase , rstrip=__lowerCAmelCase , normalized=__lowerCAmelCase ) if isinstance(__lowerCAmelCase , __lowerCAmelCase ) else mask_token ) super().__init__( __lowerCAmelCase , tokenizer_file=__lowerCAmelCase , do_lower_case=__lowerCAmelCase , remove_space=__lowerCAmelCase , keep_accents=__lowerCAmelCase , bos_token=__lowerCAmelCase , eos_token=__lowerCAmelCase , unk_token=__lowerCAmelCase , sep_token=__lowerCAmelCase , pad_token=__lowerCAmelCase , cls_token=__lowerCAmelCase , mask_token=__lowerCAmelCase , **__lowerCAmelCase , ) A__ = do_lower_case A__ = remove_space A__ = keep_accents A__ = vocab_file A__ = False if not self.vocab_file else True def a_ ( self : List[Any] , __lowerCAmelCase : List[int] , __lowerCAmelCase : Optional[List[int]] = None ) -> List[int]: """simple docstring""" A__ = [self.sep_token_id] A__ = [self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def a_ ( self : Union[str, Any] , __lowerCAmelCase : List[int] , __lowerCAmelCase : Optional[List[int]] = None ) -> List[int]: """simple docstring""" A__ = [self.sep_token_id] A__ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def a_ ( self : Tuple , __lowerCAmelCase : str , __lowerCAmelCase : Optional[str] = None ) -> Tuple[str]: """simple docstring""" 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(__lowerCAmelCase ): logger.error(f'Vocabulary path ({save_directory}) should be a directory' ) return A__ = os.path.join( __lowerCAmelCase , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(__lowerCAmelCase ): copyfile(self.vocab_file , __lowerCAmelCase ) return (out_vocab_file,)
274
0
import unittest import torch from diffusers import VQModel from diffusers.utils import floats_tensor, torch_device from diffusers.utils.testing_utils import enable_full_determinism from .test_modeling_common import ModelTesterMixin, UNetTesterMixin enable_full_determinism() class lowercase_ ( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , unittest.TestCase ): A__ : Optional[Any] = VQModel A__ : Union[str, Any] = '''sample''' @property def lowerCamelCase_ ( self , __UpperCamelCase=(3_2, 3_2) ): """simple docstring""" UpperCamelCase_ = 4 UpperCamelCase_ = 3 UpperCamelCase_ = floats_tensor((batch_size, num_channels) + sizes ).to(__lowerCAmelCase ) return {"sample": image} @property def lowerCamelCase_ ( self ): """simple docstring""" return (3, 3_2, 3_2) @property def lowerCamelCase_ ( self ): """simple docstring""" return (3, 3_2, 3_2) def lowerCamelCase_ ( self ): """simple docstring""" UpperCamelCase_ = { """block_out_channels""": [3_2, 6_4], """in_channels""": 3, """out_channels""": 3, """down_block_types""": ["""DownEncoderBlock2D""", """DownEncoderBlock2D"""], """up_block_types""": ["""UpDecoderBlock2D""", """UpDecoderBlock2D"""], """latent_channels""": 3, } UpperCamelCase_ = self.dummy_input return init_dict, inputs_dict def lowerCamelCase_ ( self ): """simple docstring""" pass def lowerCamelCase_ ( self ): """simple docstring""" pass def lowerCamelCase_ ( self ): """simple docstring""" UpperCamelCase_ , UpperCamelCase_ = VQModel.from_pretrained("""fusing/vqgan-dummy""" , output_loading_info=__lowerCAmelCase ) self.assertIsNotNone(__lowerCAmelCase ) self.assertEqual(len(loading_info["""missing_keys"""] ) , 0 ) model.to(__lowerCAmelCase ) UpperCamelCase_ = model(**self.dummy_input ) assert image is not None, "Make sure output is not None" def lowerCamelCase_ ( self ): """simple docstring""" UpperCamelCase_ = VQModel.from_pretrained("""fusing/vqgan-dummy""" ) model.to(__lowerCAmelCase ).eval() torch.manual_seed(0 ) if torch.cuda.is_available(): torch.cuda.manual_seed_all(0 ) UpperCamelCase_ = torch.randn(1 , model.config.in_channels , model.config.sample_size , model.config.sample_size ) UpperCamelCase_ = image.to(__lowerCAmelCase ) with torch.no_grad(): UpperCamelCase_ = model(__lowerCAmelCase ).sample UpperCamelCase_ = output[0, -1, -3:, -3:].flatten().cpu() # fmt: off UpperCamelCase_ = torch.tensor([-0.0_153, -0.4_044, -0.1_880, -0.5_161, -0.2_418, -0.4_072, -0.1_612, -0.0_633, -0.0_143] ) # fmt: on self.assertTrue(torch.allclose(__lowerCAmelCase , __lowerCAmelCase , atol=1e-3 ) )
122
import dataclasses import json import warnings from dataclasses import dataclass, field from time import time from typing import List from ..utils import logging A : Dict = logging.get_logger(__name__) def __lowerCamelCase ( __a :int=None , __a :Optional[Any]=None ) -> int: """simple docstring""" return field(default_factory=lambda: default , metadata=__a ) @dataclass class A : '''simple docstring''' __lowerCamelCase : List[str] = list_field( default=[] , metadata={ '''help''': ( '''Model checkpoints to be provided to the AutoModel classes. Leave blank to benchmark the base version''' ''' of all available models''' ) } , ) __lowerCamelCase : List[int] = list_field( default=[8] , metadata={'''help''': '''List of batch sizes for which memory and time performance will be evaluated'''} ) __lowerCamelCase : List[int] = list_field( default=[8, 32, 128, 512] , metadata={'''help''': '''List of sequence lengths for which memory and time performance will be evaluated'''} , ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Whether to benchmark inference of model. Inference can be disabled via --no-inference.'''} , ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Whether to run on available cuda devices. Cuda can be disabled via --no-cuda.'''} , ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Whether to run on available tpu devices. TPU can be disabled via --no-tpu.'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Use FP16 to accelerate inference.'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Benchmark training of model'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Verbose memory tracing'''} ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Whether to perform speed measurements. Speed measurements can be disabled via --no-speed.'''} , ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={ '''help''': '''Whether to perform memory measurements. Memory measurements can be disabled via --no-memory''' } , ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Trace memory line by line'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Save result to a CSV file'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Save all print statements in a log file'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Whether to print environment information'''} ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={ '''help''': ( '''Whether to use multiprocessing for memory and speed measurement. It is highly recommended to use''' ''' multiprocessing for accurate CPU and GPU memory measurements. This option should only be disabled''' ''' for debugging / testing and on TPU.''' ) } , ) __lowerCamelCase : str = field( default=F'''inference_time_{round(time() )}.csv''' , metadata={'''help''': '''CSV filename used if saving time results to csv.'''} , ) __lowerCamelCase : str = field( default=F'''inference_memory_{round(time() )}.csv''' , metadata={'''help''': '''CSV filename used if saving memory results to csv.'''} , ) __lowerCamelCase : str = field( default=F'''train_time_{round(time() )}.csv''' , metadata={'''help''': '''CSV filename used if saving time results to csv for training.'''} , ) __lowerCamelCase : str = field( default=F'''train_memory_{round(time() )}.csv''' , metadata={'''help''': '''CSV filename used if saving memory results to csv for training.'''} , ) __lowerCamelCase : str = field( default=F'''env_info_{round(time() )}.csv''' , metadata={'''help''': '''CSV filename used if saving environment information.'''} , ) __lowerCamelCase : str = field( default=F'''log_{round(time() )}.csv''' , metadata={'''help''': '''Log filename used if print statements are saved in log.'''} , ) __lowerCamelCase : int = field(default=3 , metadata={'''help''': '''Times an experiment will be run.'''} ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={ '''help''': ( '''Instead of loading the model as defined in `config.architectures` if exists, just load the pretrain''' ''' model weights.''' ) } , ) def a_ ( self : Dict ) -> Union[str, Any]: """simple docstring""" warnings.warn( f'The class {self.__class__} is deprecated. Hugging Face Benchmarking utils' """ are deprecated in general and it is advised to use external Benchmarking libraries """ """ to benchmark Transformer models.""" , __lowerCAmelCase , ) def a_ ( self : Union[str, Any] ) -> List[str]: """simple docstring""" return json.dumps(dataclasses.asdict(self ) , indent=2 ) @property def a_ ( self : Tuple ) -> List[str]: """simple docstring""" if len(self.models ) <= 0: raise ValueError( """Please make sure you provide at least one model name / model identifier, *e.g.* `--models""" """ bert-base-cased` or `args.models = ['bert-base-cased'].""" ) return self.models @property def a_ ( self : Union[str, Any] ) -> Optional[Any]: """simple docstring""" if not self.multi_process: return False elif self.is_tpu: logger.info("""Multiprocessing is currently not possible on TPU.""" ) return False else: return True
274
0
# # This a `torch.distributed` diagnostics script that checks that all GPUs in the cluster (one or # many nodes) can talk to each other via nccl and allocate gpu memory. # # To run first adjust the number of processes and nodes: # # python -m torch.distributed.run --nproc_per_node 2 --nnodes 1 torch-distributed-gpu-test.py # # You may need to add --master_addr $MASTER_ADDR --master_port $MASTER_PORT if using a custom addr:port # # You can also use the rdzv API: --rdzv_endpoint $MASTER_ADDR:$MASTER_PORT --rdzv_backend c10d # # use torch.distributed.launch instead of torch.distributed.run for torch < 1.9 # # If you get a hanging in `barrier` calls you have some network issues, you may try to debug this with: # # NCCL_DEBUG=INFO python -m torch.distributed.run --nproc_per_node 2 --nnodes 1 torch-distributed-gpu-test.py # # which should tell you what's going on behind the scenes. # # # This script can be run via `srun` in the SLURM environment as well. Here is a SLURM script that # runs on 2 nodes of 4 gpus per node: # # #SBATCH --job-name=test-nodes # name # #SBATCH --nodes=2 # nodes # #SBATCH --ntasks-per-node=1 # crucial - only 1 task per dist per node! # #SBATCH --cpus-per-task=10 # number of cores per tasks # #SBATCH --gres=gpu:4 # number of gpus # #SBATCH --time 0:05:00 # maximum execution time (HH:MM:SS) # #SBATCH --output=%x-%j.out # output file name # # GPUS_PER_NODE=4 # MASTER_ADDR=$(scontrol show hostnames $SLURM_JOB_NODELIST | head -n 1) # MASTER_PORT=6000 # # srun --jobid $SLURM_JOBID bash -c 'python -m torch.distributed.run \ # --nproc_per_node $GPUS_PER_NODE --nnodes $SLURM_NNODES --node_rank $SLURM_PROCID \ # --master_addr $MASTER_ADDR --master_port $MASTER_PORT \ # torch-distributed-gpu-test.py' # import fcntl import os import socket import torch import torch.distributed as dist def UpperCAmelCase_ ( *__snake_case ) -> Tuple: """simple docstring""" with open(__a , '''r''' ) as fh: fcntl.flock(__a , fcntl.LOCK_EX ) try: print(*__a ) finally: fcntl.flock(__a , fcntl.LOCK_UN ) UpperCAmelCase__ = int(os.environ['''LOCAL_RANK''']) torch.cuda.set_device(local_rank) UpperCAmelCase__ = torch.device('''cuda''', local_rank) UpperCAmelCase__ = socket.gethostname() UpperCAmelCase__ = f'''[{hostname}-{local_rank}]''' try: # test distributed dist.init_process_group('''nccl''') dist.all_reduce(torch.ones(1).to(device), op=dist.ReduceOp.SUM) dist.barrier() # test cuda is available and can allocate memory torch.cuda.is_available() torch.ones(1).cuda(local_rank) # global rank UpperCAmelCase__ = dist.get_rank() UpperCAmelCase__ = dist.get_world_size() printflock(f'''{gpu} is OK (global rank: {rank}/{world_size})''') dist.barrier() if rank == 0: printflock(f'''pt={torch.__version__}, cuda={torch.version.cuda}, nccl={torch.cuda.nccl.version()}''') except Exception: printflock(f'''{gpu} is broken''') raise
5
from math import ceil def __lowerCamelCase ( __a :int = 1_0_0_1 ) -> int: """simple docstring""" A__ = 1 for i in range(1 , int(ceil(n / 2.0 ) ) ): A__ = 2 * i + 1 A__ = 2 * i A__ = total + 4 * odd**2 - 6 * even return total if __name__ == "__main__": import sys if len(sys.argv) == 1: print(solution()) else: try: A : List[str] = int(sys.argv[1]) print(solution(n)) except ValueError: print('''Invalid entry - please enter a number''')
274
0
'''simple docstring''' import unittest from transformers import is_torch_available from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device if is_torch_available(): from transformers import AutoModelForSeqaSeqLM, AutoTokenizer @require_torch @require_sentencepiece @require_tokenizers class lowerCAmelCase_ ( unittest.TestCase ): @slow def _snake_case ( self ) -> Dict: _lowerCAmelCase = AutoModelForSeqaSeqLM.from_pretrained("google/mt5-small" , return_dict=__lowerCAmelCase ).to(__lowerCAmelCase ) _lowerCAmelCase = AutoTokenizer.from_pretrained("google/mt5-small" ) _lowerCAmelCase = tokenizer("Hello there" , return_tensors="pt" ).input_ids _lowerCAmelCase = tokenizer("Hi I am" , return_tensors="pt" ).input_ids _lowerCAmelCase = model(input_ids.to(__lowerCAmelCase ) , labels=labels.to(__lowerCAmelCase ) ).loss _lowerCAmelCase = -(labels.shape[-1] * loss.item()) _lowerCAmelCase = -84.9127 self.assertTrue(abs(mtf_score - EXPECTED_SCORE ) < 1E-4 )
158
import argparse import csv import logging import os import random import numpy as np import torch from torch.utils.data import DataLoader, RandomSampler, SequentialSampler, TensorDataset from tqdm import tqdm, trange from transformers import ( CONFIG_NAME, WEIGHTS_NAME, AdamW, OpenAIGPTDoubleHeadsModel, OpenAIGPTTokenizer, get_linear_schedule_with_warmup, ) logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''', datefmt='''%m/%d/%Y %H:%M:%S''', level=logging.INFO ) A : Tuple = logging.getLogger(__name__) def __lowerCamelCase ( __a :Optional[int] , __a :List[str] ) -> Tuple: """simple docstring""" A__ = np.argmax(__a , axis=1 ) return np.sum(outputs == labels ) def __lowerCamelCase ( __a :Tuple ) -> Dict: """simple docstring""" with open(__a , encoding="""utf_8""" ) as f: A__ = csv.reader(__a ) A__ = [] next(__a ) # skip the first line for line in tqdm(__a ): output.append((""" """.join(line[1:5] ), line[5], line[6], int(line[-1] ) - 1) ) return output def __lowerCamelCase ( __a :Optional[int] , __a :List[Any] , __a :Dict , __a :Optional[Any] , __a :Optional[Any] , __a :int ) -> Union[str, Any]: """simple docstring""" A__ = [] for dataset in encoded_datasets: A__ = len(__a ) A__ = np.zeros((n_batch, 2, input_len) , dtype=np.intaa ) A__ = np.zeros((n_batch, 2) , dtype=np.intaa ) A__ = np.full((n_batch, 2, input_len) , fill_value=-1_0_0 , dtype=np.intaa ) A__ = np.zeros((n_batch,) , dtype=np.intaa ) for ( i, (story, conta, conta, mc_label), ) in enumerate(__a ): A__ = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] A__ = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] A__ = with_conta A__ = with_conta A__ = len(__a ) - 1 A__ = len(__a ) - 1 A__ = with_conta A__ = with_conta A__ = mc_label A__ = (input_ids, mc_token_ids, lm_labels, mc_labels) tensor_datasets.append(tuple(torch.tensor(__a ) for t in all_inputs ) ) return tensor_datasets def __lowerCamelCase ( ) -> Union[str, Any]: """simple docstring""" A__ = argparse.ArgumentParser() parser.add_argument("""--model_name""" , type=__a , default="""openai-gpt""" , help="""pretrained model name""" ) parser.add_argument("""--do_train""" , action="""store_true""" , help="""Whether to run training.""" ) parser.add_argument("""--do_eval""" , action="""store_true""" , help="""Whether to run eval on the dev set.""" ) parser.add_argument( """--output_dir""" , default=__a , type=__a , required=__a , help="""The output directory where the model predictions and checkpoints will be written.""" , ) parser.add_argument("""--train_dataset""" , type=__a , default="""""" ) parser.add_argument("""--eval_dataset""" , type=__a , default="""""" ) parser.add_argument("""--seed""" , type=__a , default=4_2 ) parser.add_argument("""--num_train_epochs""" , type=__a , default=3 ) parser.add_argument("""--train_batch_size""" , type=__a , default=8 ) parser.add_argument("""--eval_batch_size""" , type=__a , default=1_6 ) parser.add_argument("""--adam_epsilon""" , default=1E-8 , type=__a , help="""Epsilon for Adam optimizer.""" ) parser.add_argument("""--max_grad_norm""" , type=__a , default=1 ) parser.add_argument( """--max_steps""" , default=-1 , type=__a , help=( """If > 0: set total number of training steps to perform. Override num_train_epochs.""" ) , ) parser.add_argument( """--gradient_accumulation_steps""" , type=__a , default=1 , help="""Number of updates steps to accumulate before performing a backward/update pass.""" , ) parser.add_argument("""--learning_rate""" , type=__a , default=6.25E-5 ) parser.add_argument("""--warmup_steps""" , default=0 , type=__a , help="""Linear warmup over warmup_steps.""" ) parser.add_argument("""--lr_schedule""" , type=__a , default="""warmup_linear""" ) parser.add_argument("""--weight_decay""" , type=__a , default=0.01 ) parser.add_argument("""--lm_coef""" , type=__a , default=0.9 ) parser.add_argument("""--n_valid""" , type=__a , default=3_7_4 ) parser.add_argument("""--server_ip""" , type=__a , default="""""" , help="""Can be used for distant debugging.""" ) parser.add_argument("""--server_port""" , type=__a , default="""""" , help="""Can be used for distant debugging.""" ) A__ = parser.parse_args() print(__a ) if args.server_ip and args.server_port: # Distant debugging - see https://code.visualstudio.com/docs/python/debugging#_attach-to-a-local-script import ptvsd print("""Waiting for debugger attach""" ) ptvsd.enable_attach(address=(args.server_ip, args.server_port) , redirect_output=__a ) ptvsd.wait_for_attach() random.seed(args.seed ) np.random.seed(args.seed ) torch.manual_seed(args.seed ) torch.cuda.manual_seed_all(args.seed ) A__ = torch.device("""cuda""" if torch.cuda.is_available() else """cpu""" ) A__ = torch.cuda.device_count() logger.info("""device: {}, n_gpu {}""".format(__a , __a ) ) if not args.do_train and not args.do_eval: raise ValueError("""At least one of `do_train` or `do_eval` must be True.""" ) if not os.path.exists(args.output_dir ): os.makedirs(args.output_dir ) # Load tokenizer and model # This loading functions also add new tokens and embeddings called `special tokens` # These new embeddings will be fine-tuned on the RocStories dataset A__ = ["""_start_""", """_delimiter_""", """_classify_"""] A__ = OpenAIGPTTokenizer.from_pretrained(args.model_name ) tokenizer.add_tokens(__a ) A__ = tokenizer.convert_tokens_to_ids(__a ) A__ = OpenAIGPTDoubleHeadsModel.from_pretrained(args.model_name ) model.resize_token_embeddings(len(__a ) ) model.to(__a ) # Load and encode the datasets def tokenize_and_encode(__a :Tuple ): if isinstance(__a , __a ): return tokenizer.convert_tokens_to_ids(tokenizer.tokenize(__a ) ) elif isinstance(__a , __a ): return obj return [tokenize_and_encode(__a ) for o in obj] logger.info("""Encoding dataset...""" ) A__ = load_rocstories_dataset(args.train_dataset ) A__ = load_rocstories_dataset(args.eval_dataset ) A__ = (train_dataset, eval_dataset) A__ = tokenize_and_encode(__a ) # Compute the max input length for the Transformer A__ = model.config.n_positions // 2 - 2 A__ = max( len(story[:max_length] ) + max(len(conta[:max_length] ) , len(conta[:max_length] ) ) + 3 for dataset in encoded_datasets for story, conta, conta, _ in dataset ) A__ = min(__a , model.config.n_positions ) # Max size of input for the pre-trained model # Prepare inputs tensors and dataloaders A__ = pre_process_datasets(__a , __a , __a , *__a ) A__ , A__ = tensor_datasets[0], tensor_datasets[1] A__ = TensorDataset(*__a ) A__ = RandomSampler(__a ) A__ = DataLoader(__a , sampler=__a , batch_size=args.train_batch_size ) A__ = TensorDataset(*__a ) A__ = SequentialSampler(__a ) A__ = DataLoader(__a , sampler=__a , batch_size=args.eval_batch_size ) # Prepare optimizer if args.do_train: if args.max_steps > 0: A__ = args.max_steps A__ = args.max_steps // (len(__a ) // args.gradient_accumulation_steps) + 1 else: A__ = len(__a ) // args.gradient_accumulation_steps * args.num_train_epochs A__ = list(model.named_parameters() ) A__ = ["""bias""", """LayerNorm.bias""", """LayerNorm.weight"""] A__ = [ { """params""": [p for n, p in param_optimizer if not any(nd in n for nd in no_decay )], """weight_decay""": args.weight_decay, }, {"""params""": [p for n, p in param_optimizer if any(nd in n for nd in no_decay )], """weight_decay""": 0.0}, ] A__ = AdamW(__a , lr=args.learning_rate , eps=args.adam_epsilon ) A__ = get_linear_schedule_with_warmup( __a , num_warmup_steps=args.warmup_steps , num_training_steps=__a ) if args.do_train: A__ , A__ , A__ = 0, 0, None model.train() for _ in trange(int(args.num_train_epochs ) , desc="""Epoch""" ): A__ = 0 A__ = 0 A__ = tqdm(__a , desc="""Training""" ) for step, batch in enumerate(__a ): A__ = tuple(t.to(__a ) for t in batch ) A__ , A__ , A__ , A__ = batch A__ = model(__a , mc_token_ids=__a , lm_labels=__a , mc_labels=__a ) A__ = args.lm_coef * losses[0] + losses[1] loss.backward() optimizer.step() scheduler.step() optimizer.zero_grad() tr_loss += loss.item() A__ = ( loss.item() if exp_average_loss is None else 0.7 * exp_average_loss + 0.3 * loss.item() ) nb_tr_steps += 1 A__ = """Training loss: {:.2e} lr: {:.2e}""".format(__a , scheduler.get_lr()[0] ) # Save a trained model if args.do_train: # Save a trained model, configuration and tokenizer A__ = model.module if hasattr(__a , """module""" ) else model # Only save the model itself # If we save using the predefined names, we can load using `from_pretrained` A__ = os.path.join(args.output_dir , __a ) A__ = os.path.join(args.output_dir , __a ) torch.save(model_to_save.state_dict() , __a ) model_to_save.config.to_json_file(__a ) tokenizer.save_vocabulary(args.output_dir ) # Load a trained model and vocabulary that you have fine-tuned A__ = OpenAIGPTDoubleHeadsModel.from_pretrained(args.output_dir ) A__ = OpenAIGPTTokenizer.from_pretrained(args.output_dir ) model.to(__a ) if args.do_eval: model.eval() A__ , A__ = 0, 0 A__ , A__ = 0, 0 for batch in tqdm(__a , desc="""Evaluating""" ): A__ = tuple(t.to(__a ) for t in batch ) A__ , A__ , A__ , A__ = batch with torch.no_grad(): A__ , A__ , A__ , A__ = model( __a , mc_token_ids=__a , lm_labels=__a , mc_labels=__a ) A__ = mc_logits.detach().cpu().numpy() A__ = mc_labels.to("""cpu""" ).numpy() A__ = accuracy(__a , __a ) eval_loss += mc_loss.mean().item() eval_accuracy += tmp_eval_accuracy nb_eval_examples += input_ids.size(0 ) nb_eval_steps += 1 A__ = eval_loss / nb_eval_steps A__ = eval_accuracy / nb_eval_examples A__ = tr_loss / nb_tr_steps if args.do_train else None A__ = {"""eval_loss""": eval_loss, """eval_accuracy""": eval_accuracy, """train_loss""": train_loss} A__ = os.path.join(args.output_dir , """eval_results.txt""" ) with open(__a , """w""" ) as writer: logger.info("""***** Eval results *****""" ) for key in sorted(result.keys() ): logger.info(""" %s = %s""" , __a , str(result[key] ) ) writer.write("""%s = %s\n""" % (key, str(result[key] )) ) if __name__ == "__main__": main()
274
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available _a = { '''configuration_table_transformer''': [ '''TABLE_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''TableTransformerConfig''', '''TableTransformerOnnxConfig''', ] } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _a = [ '''TABLE_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TableTransformerForObjectDetection''', '''TableTransformerModel''', '''TableTransformerPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_table_transformer import ( TABLE_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TableTransformerConfig, TableTransformerOnnxConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_table_transformer import ( TABLE_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TableTransformerForObjectDetection, TableTransformerModel, TableTransformerPreTrainedModel, ) else: import sys _a = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
209
import argparse from collections import defaultdict import yaml A : str = '''docs/source/en/_toctree.yml''' def __lowerCamelCase ( __a :str ) -> List[Any]: """simple docstring""" A__ = defaultdict(__a ) A__ = [] A__ = [] for doc in doc_list: if "local" in doc: counts[doc["local"]] += 1 if doc["title"].lower() == "overview": overview_doc.append({"""local""": doc["""local"""], """title""": doc["""title"""]} ) else: new_doc_list.append(__a ) A__ = new_doc_list A__ = [key for key, value in counts.items() if value > 1] A__ = [] for duplicate_key in duplicates: A__ = list({doc["""title"""] for doc in doc_list if doc["""local"""] == duplicate_key} ) if len(__a ) > 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 doc_list if """local""" not in counts or counts[doc["""local"""]] == 1] ) A__ = sorted(__a , key=lambda __a : s["title"].lower() ) # "overview" gets special treatment and is always first if len(__a ) > 1: raise ValueError("""{doc_list} has two 'overview' docs which is not allowed.""" ) overview_doc.extend(__a ) # Sort return overview_doc def __lowerCamelCase ( __a :Any=False ) -> List[str]: """simple docstring""" with open(__a , encoding="""utf-8""" ) as f: A__ = yaml.safe_load(f.read() ) # Get to the API doc A__ = 0 while content[api_idx]["title"] != "API": api_idx += 1 A__ = content[api_idx]["""sections"""] # Then to the model doc A__ = 0 while api_doc[scheduler_idx]["title"] != "Schedulers": scheduler_idx += 1 A__ = api_doc[scheduler_idx]["""sections"""] A__ = clean_doc_toc(__a ) A__ = False if new_scheduler_doc != scheduler_doc: A__ = True if overwrite: A__ = new_scheduler_doc if diff: if overwrite: A__ = api_doc with open(__a , """w""" , encoding="""utf-8""" ) as f: f.write(yaml.dump(__a , allow_unicode=__a ) ) else: raise ValueError( """The model doc part of the table of content is not properly sorted, run `make style` to fix this.""" ) def __lowerCamelCase ( __a :Optional[int]=False ) -> Dict: """simple docstring""" with open(__a , encoding="""utf-8""" ) as f: A__ = yaml.safe_load(f.read() ) # Get to the API doc A__ = 0 while content[api_idx]["title"] != "API": api_idx += 1 A__ = content[api_idx]["""sections"""] # Then to the model doc A__ = 0 while api_doc[pipeline_idx]["title"] != "Pipelines": pipeline_idx += 1 A__ = False A__ = api_doc[pipeline_idx]["""sections"""] A__ = [] # sort sub pipeline docs for pipeline_doc in pipeline_docs: if "section" in pipeline_doc: A__ = pipeline_doc["""section"""] A__ = clean_doc_toc(__a ) if overwrite: A__ = new_sub_pipeline_doc new_pipeline_docs.append(__a ) # sort overall pipeline doc A__ = clean_doc_toc(__a ) if new_pipeline_docs != pipeline_docs: A__ = True if overwrite: A__ = new_pipeline_docs if diff: if overwrite: A__ = api_doc with open(__a , """w""" , encoding="""utf-8""" ) as f: f.write(yaml.dump(__a , allow_unicode=__a ) ) 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__": A : Tuple = argparse.ArgumentParser() parser.add_argument('''--fix_and_overwrite''', action='''store_true''', help='''Whether to fix inconsistencies.''') A : Optional[Any] = parser.parse_args() check_scheduler_doc(args.fix_and_overwrite) check_pipeline_doc(args.fix_and_overwrite)
274
0
from math import factorial def lowercase_ ( _A : int , _A : int , _A : float ): """simple docstring""" if successes > trials: raise ValueError("successes must be lower or equal to trials" ) if trials < 0 or successes < 0: raise ValueError("the function is defined for non-negative integers" ) if not isinstance(__a , __a ) or not isinstance(__a , __a ): raise ValueError("the function is defined for non-negative integers" ) if not 0 < prob < 1: raise ValueError("prob has to be in range of 1 - 0" ) lowerCamelCase__ : int = (prob**successes) * ((1 - prob) ** (trials - successes)) # Calculate the binomial coefficient: n! / k!(n-k)! lowerCamelCase__ : Dict = float(factorial(__a ) ) coefficient /= factorial(__a ) * factorial(trials - successes ) return probability * coefficient if __name__ == "__main__": from doctest import testmod testmod() print("Probability of 2 successes out of 4 trails") print("with probability of 0.75 is:", end=" ") print(binomial_distribution(2, 4, 0.7_5))
184
def __lowerCamelCase ( __a :str ) -> list: """simple docstring""" A__ = [0] * len(__a ) for i in range(1 , len(__a ) ): # use last results for better performance - dynamic programming A__ = prefix_result[i - 1] while j > 0 and input_string[i] != input_string[j]: A__ = prefix_result[j - 1] if input_string[i] == input_string[j]: j += 1 A__ = j return prefix_result def __lowerCamelCase ( __a :str ) -> int: """simple docstring""" return max(prefix_function(__a ) ) if __name__ == "__main__": import doctest doctest.testmod()
274
0
from random import randint from tempfile import TemporaryFile import numpy as np def __UpperCAmelCase ( a_ , a_ , a_): snake_case_ = 0 if start < end: snake_case_ = randint(__a , __a) snake_case_ = a[end] snake_case_ = a[pivot] snake_case_ = temp snake_case_ , snake_case_ = _in_place_partition(__a , __a , __a) count += _in_place_quick_sort(__a , __a , p - 1) count += _in_place_quick_sort(__a , p + 1 , __a) return count def __UpperCAmelCase ( a_ , a_ , a_): snake_case_ = 0 snake_case_ = randint(__a , __a) snake_case_ = a[end] snake_case_ = a[pivot] snake_case_ = temp snake_case_ = start - 1 for index in range(__a , __a): count += 1 if a[index] < a[end]: # check if current val is less than pivot value snake_case_ = new_pivot_index + 1 snake_case_ = a[new_pivot_index] snake_case_ = a[index] snake_case_ = temp snake_case_ = a[new_pivot_index + 1] snake_case_ = a[end] snake_case_ = temp return new_pivot_index + 1, count lowercase = TemporaryFile() lowercase = 100 # 1000 elements are to be sorted lowercase = 0, 1 # mean and standard deviation lowercase = np.random.normal(mu, sigma, p) np.save(outfile, X) print("The array is") print(X) outfile.seek(0) # using the same array lowercase = np.load(outfile) lowercase = len(M) - 1 lowercase = _in_place_quick_sort(M, 0, r) print( "No of Comparisons for 100 elements selected from a standard normal distribution" "is :" ) print(z)
178
def __lowerCamelCase ( __a :int = 1_0_0_0_0_0_0 ) -> int: """simple docstring""" A__ = limit + 1 A__ = [0] * limit for first_term in range(1 , __a ): for n in range(__a , __a , __a ): A__ = first_term + n / first_term if common_difference % 4: # d must be divisble by 4 continue else: common_difference /= 4 if ( first_term > common_difference and first_term < 4 * common_difference ): # since x,y,z are positive integers frequency[n] += 1 # so z>0 and a>d ,also 4d<a A__ = sum(1 for x in frequency[1:limit] if x == 1_0 ) return count if __name__ == "__main__": print(F'''{solution() = }''')
274
0
def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : bool = False ): if n == 2: return True if not n % 2 or n < 2: return False if n > 5 and n % 10 not in (1, 3, 7, 9): # can quickly check last digit return False if n > 3_31_70_44_06_46_79_88_73_85_96_19_81 and not allow_probable: raise ValueError( 'Warning: upper bound of deterministic test is exceeded. ' 'Pass allow_probable=True to allow probabilistic test. ' 'A return value of True indicates a probable prime.' ) # array bounds provided by analysis __UpperCamelCase =[ 20_47, 1_37_36_53, 25_32_60_01, 32_15_03_17_51, 2_15_23_02_89_87_47, 3_47_47_49_66_03_83, 3_41_55_00_71_72_83_21, 1, 3_82_51_23_05_65_46_41_30_51, 1, 1, 31_86_65_85_78_34_03_11_51_16_74_61, 3_31_70_44_06_46_79_88_73_85_96_19_81, ] __UpperCamelCase =[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41] for idx, _p in enumerate(__a , 1 ): if n < _p: # then we have our last prime to check __UpperCamelCase =primes[:idx] break __UpperCamelCase , __UpperCamelCase =n - 1, 0 # break up n -1 into a power of 2 (s) and # remaining odd component # essentially, solve for d * 2 ** s == n - 1 while d % 2 == 0: d //= 2 s += 1 for prime in plist: __UpperCamelCase =False for r in range(__a ): __UpperCamelCase =pow(__a , d * 2**r , __a ) # see article for analysis explanation for m if (r == 0 and m == 1) or ((m + 1) % n == 0): __UpperCamelCase =True # this loop will not determine compositeness break if pr: continue # if pr is False, then the above loop never evaluated to true, # and the n MUST be composite return False return True def _UpperCAmelCase ( ): assert not miller_rabin(5_61 ) assert miller_rabin(5_63 ) # 2047 assert not miller_rabin(83_82_01 ) assert miller_rabin(83_82_07 ) # 1_373_653 assert not miller_rabin(17_31_60_01 ) assert miller_rabin(17_31_60_17 ) # 25_326_001 assert not miller_rabin(30_78_38_66_41 ) assert miller_rabin(30_78_38_66_53 ) # 3_215_031_751 assert not miller_rabin(1_71_30_45_57_48_01 ) assert miller_rabin(1_71_30_45_57_48_19 ) # 2_152_302_898_747 assert not miller_rabin(2_77_97_99_72_83_07 ) assert miller_rabin(2_77_97_99_72_83_27 ) # 3_474_749_660_383 assert not miller_rabin(1_13_85_00_23_90_94_41 ) assert miller_rabin(1_13_85_00_23_90_95_27 ) # 341_550_071_728_321 assert not miller_rabin(1_27_50_41_01_88_48_80_43_51 ) assert miller_rabin(1_27_50_41_01_88_48_80_43_91 ) # 3_825_123_056_546_413_051 assert not miller_rabin(7_96_66_46_44_58_50_77_87_79_18_67 ) assert miller_rabin(7_96_66_46_44_58_50_77_87_79_19_51 ) # 318_665_857_834_031_151_167_461 assert not miller_rabin(55_28_40_67_74_46_64_78_97_66_03_33 ) assert miller_rabin(55_28_40_67_74_46_64_78_97_66_03_59 ) # 3_317_044_064_679_887_385_961_981 # upper limit for probabilistic test if __name__ == "__main__": test_miller_rabin()
62
class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' pass class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' pass class A : '''simple docstring''' def __init__( self : List[Any] ) -> str: """simple docstring""" A__ = [ [], [], [], ] def a_ ( self : Dict , __lowerCAmelCase : int , __lowerCAmelCase : int ) -> None: """simple docstring""" try: if len(self.queues[priority] ) >= 1_00: raise OverflowError("""Maximum queue size is 100""" ) self.queues[priority].append(__lowerCAmelCase ) except IndexError: raise ValueError("""Valid priorities are 0, 1, and 2""" ) def a_ ( self : Optional[Any] ) -> int: """simple docstring""" for queue in self.queues: if queue: return queue.pop(0 ) raise UnderFlowError("""All queues are empty""" ) def __str__( self : Tuple ) -> str: """simple docstring""" return "\n".join(f'Priority {i}: {q}' for i, q in enumerate(self.queues ) ) class A : '''simple docstring''' def __init__( self : int ) -> str: """simple docstring""" A__ = [] def a_ ( self : int , __lowerCAmelCase : int ) -> None: """simple docstring""" if len(self.queue ) == 1_00: raise OverFlowError("""Maximum queue size is 100""" ) self.queue.append(__lowerCAmelCase ) def a_ ( self : List[str] ) -> int: """simple docstring""" if not self.queue: raise UnderFlowError("""The queue is empty""" ) else: A__ = min(self.queue ) self.queue.remove(__lowerCAmelCase ) return data def __str__( self : List[Any] ) -> str: """simple docstring""" return str(self.queue ) def __lowerCamelCase ( ) -> Optional[Any]: """simple docstring""" A__ = FixedPriorityQueue() fpq.enqueue(0 , 1_0 ) fpq.enqueue(1 , 7_0 ) fpq.enqueue(0 , 1_0_0 ) fpq.enqueue(2 , 1 ) fpq.enqueue(2 , 5 ) fpq.enqueue(1 , 7 ) fpq.enqueue(2 , 4 ) fpq.enqueue(1 , 6_4 ) fpq.enqueue(0 , 1_2_8 ) print(__a ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(__a ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) def __lowerCamelCase ( ) -> int: """simple docstring""" A__ = ElementPriorityQueue() epq.enqueue(1_0 ) epq.enqueue(7_0 ) epq.enqueue(1_0_0 ) epq.enqueue(1 ) epq.enqueue(5 ) epq.enqueue(7 ) epq.enqueue(4 ) epq.enqueue(6_4 ) epq.enqueue(1_2_8 ) print(__a ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(__a ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) if __name__ == "__main__": fixed_priority_queue() element_priority_queue()
274
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_torch_available, is_vision_available, ) _lowerCAmelCase = {'''configuration_beit''': ['''BEIT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''BeitConfig''', '''BeitOnnxConfig''']} try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCAmelCase = ['''BeitFeatureExtractor'''] _lowerCAmelCase = ['''BeitImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCAmelCase = [ '''BEIT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''BeitForImageClassification''', '''BeitForMaskedImageModeling''', '''BeitForSemanticSegmentation''', '''BeitModel''', '''BeitPreTrainedModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCAmelCase = [ '''FlaxBeitForImageClassification''', '''FlaxBeitForMaskedImageModeling''', '''FlaxBeitModel''', '''FlaxBeitPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_beit import BEIT_PRETRAINED_CONFIG_ARCHIVE_MAP, BeitConfig, BeitOnnxConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_beit import BeitFeatureExtractor from .image_processing_beit import BeitImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_beit import ( BEIT_PRETRAINED_MODEL_ARCHIVE_LIST, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation, BeitModel, BeitPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_beit import ( FlaxBeitForImageClassification, FlaxBeitForMaskedImageModeling, FlaxBeitModel, FlaxBeitPreTrainedModel, ) else: import sys _lowerCAmelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
298
import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import CLIPTokenizer, CLIPTokenizerFast from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES from transformers.testing_utils import require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available if is_vision_available(): from PIL import Image from transformers import CLIPImageProcessor, CLIPProcessor @require_vision class A (unittest.TestCase ): '''simple docstring''' def a_ ( self : Union[str, Any] ) -> Dict: """simple docstring""" A__ = tempfile.mkdtemp() # fmt: off A__ = ["""l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """lo""", """l</w>""", """w</w>""", """r</w>""", """t</w>""", """low</w>""", """er</w>""", """lowest</w>""", """newer</w>""", """wider""", """<unk>""", """<|startoftext|>""", """<|endoftext|>"""] # fmt: on A__ = dict(zip(__lowerCAmelCase , range(len(__lowerCAmelCase ) ) ) ) A__ = ["""#version: 0.2""", """l o""", """lo w</w>""", """e r</w>""", """"""] A__ = {"""unk_token""": """<unk>"""} A__ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""vocab_file"""] ) A__ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""merges_file"""] ) with open(self.vocab_file , """w""" , encoding="""utf-8""" ) as fp: fp.write(json.dumps(__lowerCAmelCase ) + """\n""" ) with open(self.merges_file , """w""" , encoding="""utf-8""" ) as fp: fp.write("""\n""".join(__lowerCAmelCase ) ) A__ = { """do_resize""": True, """size""": 20, """do_center_crop""": True, """crop_size""": 18, """do_normalize""": True, """image_mean""": [0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3], """image_std""": [0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1], } A__ = os.path.join(self.tmpdirname , __lowerCAmelCase ) with open(self.image_processor_file , """w""" , encoding="""utf-8""" ) as fp: json.dump(__lowerCAmelCase , __lowerCAmelCase ) def a_ ( self : Tuple , **__lowerCAmelCase : Dict ) -> str: """simple docstring""" return CLIPTokenizer.from_pretrained(self.tmpdirname , **__lowerCAmelCase ) def a_ ( self : Union[str, Any] , **__lowerCAmelCase : Dict ) -> List[str]: """simple docstring""" return CLIPTokenizerFast.from_pretrained(self.tmpdirname , **__lowerCAmelCase ) def a_ ( self : List[str] , **__lowerCAmelCase : Optional[Any] ) -> Dict: """simple docstring""" return CLIPImageProcessor.from_pretrained(self.tmpdirname , **__lowerCAmelCase ) def a_ ( self : str ) -> Dict: """simple docstring""" shutil.rmtree(self.tmpdirname ) def a_ ( self : str ) -> Any: """simple docstring""" A__ = [np.random.randint(2_55 , size=(3, 30, 4_00) , dtype=np.uinta )] A__ = [Image.fromarray(np.moveaxis(__lowerCAmelCase , 0 , -1 ) ) for x in image_inputs] return image_inputs def a_ ( self : Optional[int] ) -> Tuple: """simple docstring""" A__ = self.get_tokenizer() A__ = self.get_rust_tokenizer() A__ = self.get_image_processor() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) processor_slow.save_pretrained(self.tmpdirname ) A__ = CLIPProcessor.from_pretrained(self.tmpdirname , use_fast=__lowerCAmelCase ) A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) processor_fast.save_pretrained(self.tmpdirname ) A__ = CLIPProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor_slow.tokenizer.get_vocab() , tokenizer_slow.get_vocab() ) self.assertEqual(processor_fast.tokenizer.get_vocab() , tokenizer_fast.get_vocab() ) self.assertEqual(tokenizer_slow.get_vocab() , tokenizer_fast.get_vocab() ) self.assertIsInstance(processor_slow.tokenizer , __lowerCAmelCase ) self.assertIsInstance(processor_fast.tokenizer , __lowerCAmelCase ) self.assertEqual(processor_slow.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertEqual(processor_fast.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertIsInstance(processor_slow.image_processor , __lowerCAmelCase ) self.assertIsInstance(processor_fast.image_processor , __lowerCAmelCase ) def a_ ( self : Optional[Any] ) -> Union[str, Any]: """simple docstring""" A__ = CLIPProcessor(tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) A__ = self.get_tokenizer(bos_token="""(BOS)""" , eos_token="""(EOS)""" ) A__ = self.get_image_processor(do_normalize=__lowerCAmelCase , padding_value=1.0 ) A__ = CLIPProcessor.from_pretrained( self.tmpdirname , bos_token="""(BOS)""" , eos_token="""(EOS)""" , do_normalize=__lowerCAmelCase , padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , __lowerCAmelCase ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , __lowerCAmelCase ) def a_ ( self : List[Any] ) -> Dict: """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) A__ = self.prepare_image_inputs() A__ = image_processor(__lowerCAmelCase , return_tensors="""np""" ) A__ = processor(images=__lowerCAmelCase , return_tensors="""np""" ) for key in input_image_proc.keys(): self.assertAlmostEqual(input_image_proc[key].sum() , input_processor[key].sum() , delta=1e-2 ) def a_ ( self : Optional[Any] ) -> Any: """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) A__ = """lower newer""" A__ = processor(text=__lowerCAmelCase ) A__ = tokenizer(__lowerCAmelCase ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def a_ ( self : Union[str, Any] ) -> Dict: """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) A__ = """lower newer""" A__ = self.prepare_image_inputs() A__ = processor(text=__lowerCAmelCase , images=__lowerCAmelCase ) self.assertListEqual(list(inputs.keys() ) , ["""input_ids""", """attention_mask""", """pixel_values"""] ) # test if it raises when no input is passed with pytest.raises(__lowerCAmelCase ): processor() def a_ ( self : Tuple ) -> str: """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) A__ = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] A__ = processor.batch_decode(__lowerCAmelCase ) A__ = tokenizer.batch_decode(__lowerCAmelCase ) self.assertListEqual(__lowerCAmelCase , __lowerCAmelCase ) def a_ ( self : Optional[int] ) -> str: """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) A__ = """lower newer""" A__ = self.prepare_image_inputs() A__ = processor(text=__lowerCAmelCase , images=__lowerCAmelCase ) self.assertListEqual(list(inputs.keys() ) , processor.model_input_names )
274
0
"""simple docstring""" def lowerCAmelCase__ ( UpperCamelCase__ , UpperCamelCase__ = " " ): '''simple docstring''' _a : Optional[Any] = [] _a : Any = 0 for index, char in enumerate(__a ): if char == separator: split_words.append(string[last_index:index] ) _a : Union[str, Any] = index + 1 elif index + 1 == len(__a ): split_words.append(string[last_index : index + 1] ) return split_words if __name__ == "__main__": from doctest import testmod testmod()
294
from multiprocessing import Lock, Pipe, Process # lock used to ensure that two processes do not access a pipe at the same time A : Dict = Lock() def __lowerCamelCase ( __a :Dict , __a :List[str] , __a :Optional[int] , __a :Optional[int] , __a :Optional[Any] , __a :Optional[int] , __a :int ) -> Dict: """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 , 1_0 ): 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(__a ) process_lock.release() # receive your right neighbor's value process_lock.acquire() A__ = rr_cv[0].recv() process_lock.release() # take the lower value since you are on the left A__ = min(__a , __a ) 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(__a ) process_lock.release() # receive your left neighbor's value process_lock.acquire() A__ = lr_cv[0].recv() process_lock.release() # take the higher value since you are on the right A__ = max(__a , __a ) # after all swaps are performed, send the values back to main result_pipe[1].send(__a ) def __lowerCamelCase ( __a :List[str] ) -> int: """simple docstring""" A__ = [] A__ = [] # 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 A__ = Pipe() A__ = Pipe() process_array_.append( Process( target=__a , args=(0, arr[0], None, temp_rs, None, temp_rr, result_pipe[0]) , ) ) A__ = temp_rs A__ = temp_rr for i in range(1 , len(__a ) - 1 ): A__ = Pipe() A__ = Pipe() process_array_.append( Process( target=__a , args=(i, arr[i], temp_ls, temp_rs, temp_lr, temp_rr, result_pipe[i]) , ) ) A__ = temp_rs A__ = temp_rr process_array_.append( Process( target=__a , args=( len(__a ) - 1, arr[len(__a ) - 1], temp_ls, None, temp_lr, None, result_pipe[len(__a ) - 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(__a ) ): A__ = result_pipe[p][0].recv() process_array_[p].join() return arr def __lowerCamelCase ( ) -> str: """simple docstring""" A__ = list(range(1_0 , 0 , -1 ) ) print("""Initial List""" ) print(*__a ) A__ = odd_even_transposition(__a ) print("""Sorted List\n""" ) print(*__a ) if __name__ == "__main__": main()
274
0
import copy import os import tempfile from unittest import TestCase from unittest.mock import patch import numpy as np import pyarrow as pa import pyarrow.parquet as pq import pytest from datasets.arrow_writer import ArrowWriter, OptimizedTypedSequence, ParquetWriter, TypedSequence from datasets.features import ArrayaD, ClassLabel, Features, Image, Value from datasets.features.features import ArrayaDExtensionType, cast_to_python_objects from datasets.keyhash import DuplicatedKeysError, InvalidKeyError from .utils import require_pil class lowerCamelCase (_snake_case ): '''simple docstring''' def __UpperCAmelCase ( self ) -> Dict: UpperCAmelCase_ : Dict = pa.array(TypedSequence([1, 2, 3] ) ) self.assertEqual(arr.type , pa.intaa() ) def __UpperCAmelCase ( self ) -> Union[str, Any]: with self.assertRaises(__lowerCAmelCase ): UpperCAmelCase_ : List[Any] = pa.array(TypedSequence([1, 2, 3] ) , type=pa.intaa() ) def __UpperCAmelCase ( self ) -> Any: with self.assertRaises(__lowerCAmelCase ): UpperCAmelCase_ : Union[str, Any] = pa.array(TypedSequence([1, 2, 3] , try_type=Value('bool' ) , type=Value('int64' ) ) ) def __UpperCAmelCase ( self ) -> Optional[int]: UpperCAmelCase_ : Tuple = pa.array(TypedSequence([1, 2, 3] , type=Value('int32' ) ) ) self.assertEqual(arr.type , pa.intaa() ) def __UpperCAmelCase ( self ) -> str: with self.assertRaises((TypeError, pa.lib.ArrowInvalid) ): UpperCAmelCase_ : Dict = pa.array(TypedSequence(['foo', 'bar'] , type=Value('int64' ) ) ) def __UpperCAmelCase ( self ) -> Dict: UpperCAmelCase_ : Any = pa.array(TypedSequence([1, 2, 3] , try_type=Value('int32' ) ) ) self.assertEqual(arr.type , pa.intaa() ) def __UpperCAmelCase ( self ) -> Any: UpperCAmelCase_ : Union[str, Any] = pa.array(TypedSequence(['foo', 'bar'] , try_type=Value('int64' ) ) ) self.assertEqual(arr.type , pa.string() ) def __UpperCAmelCase ( self ) -> Dict: UpperCAmelCase_ : int = pa.array(TypedSequence([[[1, 2, 3]]] , type=ArrayaD((1, 3) , 'int64' ) ) ) self.assertEqual(arr.type , ArrayaDExtensionType((1, 3) , 'int64' ) ) def __UpperCAmelCase ( self ) -> str: with self.assertRaises((TypeError, pa.lib.ArrowInvalid) ): UpperCAmelCase_ : Dict = pa.array(TypedSequence(['foo', 'bar'] , type=ArrayaD((1, 3) , 'int64' ) ) ) def __UpperCAmelCase ( self ) -> str: UpperCAmelCase_ : Any = pa.array(TypedSequence([[[1, 2, 3]]] , try_type=ArrayaD((1, 3) , 'int64' ) ) ) self.assertEqual(arr.type , ArrayaDExtensionType((1, 3) , 'int64' ) ) def __UpperCAmelCase ( self ) -> List[Any]: UpperCAmelCase_ : Dict = pa.array(TypedSequence(['foo', 'bar'] , try_type=ArrayaD((1, 3) , 'int64' ) ) ) self.assertEqual(arr.type , pa.string() ) @require_pil def __UpperCAmelCase ( self ) -> str: import PIL.Image UpperCAmelCase_ : Tuple = PIL.Image.fromarray(np.arange(1_0 , dtype=np.uinta ).reshape(2 , 5 ) ) with patch( 'datasets.arrow_writer.cast_to_python_objects' , side_effect=__lowerCAmelCase ) as mock_cast_to_python_objects: UpperCAmelCase_ : Dict = pa.array(TypedSequence([{'path': None, 'bytes': b'image_bytes'}, pil_image] , type=Image() ) ) UpperCAmelCase_ , UpperCAmelCase_ : List[Any] = mock_cast_to_python_objects.call_args_list[-1] self.assertIn('optimize_list_casting' , __lowerCAmelCase ) self.assertFalse(kwargs['optimize_list_casting'] ) def lowercase__ ( __snake_case : Optional[Any] , __snake_case : int ): '''simple docstring''' UpperCAmelCase_ : Union[str, Any] = pa.BufferReader(__a ) if isinstance(__a , pa.Buffer ) else pa.memory_map(__a ) UpperCAmelCase_ : str = pa.ipc.open_stream(__a ) UpperCAmelCase_ : Union[str, Any] = f.read_all() assert len(pa_table.to_batches() ) == expected_num_chunks assert pa_table.to_pydict() == {"col_1": ["foo", "bar"], "col_2": [1, 2]} del pa_table @pytest.mark.parametrize('writer_batch_size' , [None, 1, 10] ) @pytest.mark.parametrize( 'fields' , [None, {'col_1': pa.string(), 'col_2': pa.intaa()}, {'col_1': pa.string(), 'col_2': pa.intaa()}] ) def lowercase__ ( __snake_case : Dict , __snake_case : Optional[Any] ): '''simple docstring''' UpperCAmelCase_ : str = pa.BufferOutputStream() UpperCAmelCase_ : Tuple = pa.schema(__a ) if fields else None with ArrowWriter(stream=__a , schema=__a , writer_batch_size=__a ) as writer: writer.write({'col_1': 'foo', 'col_2': 1} ) writer.write({'col_1': 'bar', 'col_2': 2} ) UpperCAmelCase_ , UpperCAmelCase_ : List[Any] = writer.finalize() assert num_examples == 2 assert num_bytes > 0 if not fields: UpperCAmelCase_ : str = {'col_1': pa.string(), 'col_2': pa.intaa()} assert writer._schema == pa.schema(__a , metadata=writer._schema.metadata ) _check_output(output.getvalue() , expected_num_chunks=num_examples if writer_batch_size == 1 else 1 ) def lowercase__ ( ): '''simple docstring''' UpperCAmelCase_ : List[str] = pa.BufferOutputStream() UpperCAmelCase_ : Optional[int] = Features({'labels': ClassLabel(names=['neg', 'pos'] )} ) with ArrowWriter(stream=__a , features=__a ) as writer: writer.write({'labels': 0} ) writer.write({'labels': 1} ) UpperCAmelCase_ , UpperCAmelCase_ : Tuple = writer.finalize() assert num_examples == 2 assert num_bytes > 0 assert writer._schema == features.arrow_schema assert writer._schema.metadata == features.arrow_schema.metadata UpperCAmelCase_ : Dict = pa.BufferReader(output.getvalue() ) UpperCAmelCase_ : int = pa.ipc.open_stream(__a ) UpperCAmelCase_ : Optional[int] = f.read_all() UpperCAmelCase_ : Optional[Any] = pa_table.schema assert pa_table.num_rows == 2 assert schema == features.arrow_schema assert schema.metadata == features.arrow_schema.metadata assert features == Features.from_arrow_schema(__a ) @pytest.mark.parametrize('writer_batch_size' , [None, 1, 10] ) def lowercase__ ( __snake_case : int ): '''simple docstring''' UpperCAmelCase_ : str = pa.BufferOutputStream() with ArrowWriter( stream=__a , writer_batch_size=__a , hash_salt='split_name' , check_duplicates=__a , ) as writer: with pytest.raises(__a ): writer.write({'col_1': 'foo', 'col_2': 1} , key=[1, 2] ) UpperCAmelCase_ , UpperCAmelCase_ : List[str] = writer.finalize() @pytest.mark.parametrize('writer_batch_size' , [None, 2, 10] ) def lowercase__ ( __snake_case : str ): '''simple docstring''' UpperCAmelCase_ : Dict = pa.BufferOutputStream() with ArrowWriter( stream=__a , writer_batch_size=__a , hash_salt='split_name' , check_duplicates=__a , ) as writer: with pytest.raises(__a ): writer.write({'col_1': 'foo', 'col_2': 1} , key=10 ) writer.write({'col_1': 'bar', 'col_2': 2} , key=10 ) UpperCAmelCase_ , UpperCAmelCase_ : Dict = writer.finalize() @pytest.mark.parametrize('writer_batch_size' , [None, 2, 10] ) def lowercase__ ( __snake_case : Union[str, Any] ): '''simple docstring''' UpperCAmelCase_ : str = pa.BufferOutputStream() with ArrowWriter( stream=__a , writer_batch_size=__a , hash_salt='split_name' , check_duplicates=__a , ) as writer: writer.write({'col_1': 'foo', 'col_2': 1} , key=1 ) writer.write({'col_1': 'bar', 'col_2': 2} , key=2 ) UpperCAmelCase_ , UpperCAmelCase_ : str = writer.finalize() assert num_examples == 2 assert num_bytes > 0 _check_output(output.getvalue() , expected_num_chunks=num_examples if writer_batch_size == 1 else 1 ) @pytest.mark.parametrize('writer_batch_size' , [None, 1, 10] ) @pytest.mark.parametrize( 'fields' , [None, {'col_1': pa.string(), 'col_2': pa.intaa()}, {'col_1': pa.string(), 'col_2': pa.intaa()}] ) def lowercase__ ( __snake_case : Any , __snake_case : Any ): '''simple docstring''' UpperCAmelCase_ : int = pa.BufferOutputStream() UpperCAmelCase_ : Any = pa.schema(__a ) if fields else None with ArrowWriter(stream=__a , schema=__a , writer_batch_size=__a ) as writer: writer.write_batch({'col_1': ['foo', 'bar'], 'col_2': [1, 2]} ) writer.write_batch({'col_1': [], 'col_2': []} ) UpperCAmelCase_ , UpperCAmelCase_ : Union[str, Any] = writer.finalize() assert num_examples == 2 assert num_bytes > 0 if not fields: UpperCAmelCase_ : List[str] = {'col_1': pa.string(), 'col_2': pa.intaa()} assert writer._schema == pa.schema(__a , metadata=writer._schema.metadata ) _check_output(output.getvalue() , expected_num_chunks=num_examples if writer_batch_size == 1 else 1 ) @pytest.mark.parametrize('writer_batch_size' , [None, 1, 10] ) @pytest.mark.parametrize( 'fields' , [None, {'col_1': pa.string(), 'col_2': pa.intaa()}, {'col_1': pa.string(), 'col_2': pa.intaa()}] ) def lowercase__ ( __snake_case : Tuple , __snake_case : Optional[int] ): '''simple docstring''' UpperCAmelCase_ : int = pa.BufferOutputStream() UpperCAmelCase_ : Any = pa.schema(__a ) if fields else None with ArrowWriter(stream=__a , schema=__a , writer_batch_size=__a ) as writer: writer.write_table(pa.Table.from_pydict({'col_1': ['foo', 'bar'], 'col_2': [1, 2]} ) ) UpperCAmelCase_ , UpperCAmelCase_ : List[str] = writer.finalize() assert num_examples == 2 assert num_bytes > 0 if not fields: UpperCAmelCase_ : Dict = {'col_1': pa.string(), 'col_2': pa.intaa()} assert writer._schema == pa.schema(__a , metadata=writer._schema.metadata ) _check_output(output.getvalue() , expected_num_chunks=num_examples if writer_batch_size == 1 else 1 ) @pytest.mark.parametrize('writer_batch_size' , [None, 1, 10] ) @pytest.mark.parametrize( 'fields' , [None, {'col_1': pa.string(), 'col_2': pa.intaa()}, {'col_1': pa.string(), 'col_2': pa.intaa()}] ) def lowercase__ ( __snake_case : List[str] , __snake_case : str ): '''simple docstring''' UpperCAmelCase_ : Any = pa.BufferOutputStream() UpperCAmelCase_ : Tuple = pa.schema(__a ) if fields else None with ArrowWriter(stream=__a , schema=__a , writer_batch_size=__a ) as writer: writer.write_row(pa.Table.from_pydict({'col_1': ['foo'], 'col_2': [1]} ) ) writer.write_row(pa.Table.from_pydict({'col_1': ['bar'], 'col_2': [2]} ) ) UpperCAmelCase_ , UpperCAmelCase_ : Any = writer.finalize() assert num_examples == 2 assert num_bytes > 0 if not fields: UpperCAmelCase_ : Tuple = {'col_1': pa.string(), 'col_2': pa.intaa()} assert writer._schema == pa.schema(__a , metadata=writer._schema.metadata ) _check_output(output.getvalue() , expected_num_chunks=num_examples if writer_batch_size == 1 else 1 ) def lowercase__ ( ): '''simple docstring''' with tempfile.TemporaryDirectory() as tmp_dir: UpperCAmelCase_ : Any = {'col_1': pa.string(), 'col_2': pa.intaa()} UpperCAmelCase_ : int = os.path.join(__a , 'test.arrow' ) with ArrowWriter(path=__a , schema=pa.schema(__a ) ) as writer: writer.write_batch({'col_1': ['foo', 'bar'], 'col_2': [1, 2]} ) UpperCAmelCase_ , UpperCAmelCase_ : Any = writer.finalize() assert num_examples == 2 assert num_bytes > 0 assert writer._schema == pa.schema(__a , metadata=writer._schema.metadata ) _check_output(__a , 1 ) def lowercase__ ( __snake_case : Tuple ): '''simple docstring''' if pa.types.is_list(__a ): return get_base_dtype(arr_type.value_type ) else: return arr_type def lowercase__ ( __snake_case : Any , __snake_case : Optional[int] ): '''simple docstring''' if isinstance(lst[0] , __a ): change_first_primitive_element_in_list(lst[0] , __a ) else: UpperCAmelCase_ : str = value @pytest.mark.parametrize('optimized_int_type, expected_dtype' , [(None, pa.intaa()), (Value('int32' ), pa.intaa())] ) @pytest.mark.parametrize('sequence' , [[1, 2, 3], [[1, 2, 3]], [[[1, 2, 3]]]] ) def lowercase__ ( __snake_case : str , __snake_case : Dict , __snake_case : Dict ): '''simple docstring''' UpperCAmelCase_ : List[str] = pa.array(TypedSequence(__a , optimized_int_type=__a ) ) assert get_base_dtype(arr.type ) == expected_dtype @pytest.mark.parametrize( 'col, expected_dtype' , [ ('attention_mask', pa.inta()), ('special_tokens_mask', pa.inta()), ('token_type_ids', pa.inta()), ('input_ids', pa.intaa()), ('other', pa.intaa()), ] , ) @pytest.mark.parametrize('sequence' , [[1, 2, 3], [[1, 2, 3]], [[[1, 2, 3]]]] ) def lowercase__ ( __snake_case : Tuple , __snake_case : List[Any] , __snake_case : Tuple ): '''simple docstring''' UpperCAmelCase_ : Optional[Any] = pa.array(OptimizedTypedSequence(__a , col=__a ) ) assert get_base_dtype(arr.type ) == expected_dtype # not in range if col != "other": # avoids errors due to in-place modifications UpperCAmelCase_ : Any = copy.deepcopy(__a ) UpperCAmelCase_ : List[str] = np.iinfo(expected_dtype.to_pandas_dtype() ).max + 1 change_first_primitive_element_in_list(__a , __a ) UpperCAmelCase_ : Optional[int] = pa.array(OptimizedTypedSequence(__a , col=__a ) ) assert get_base_dtype(arr.type ) == pa.intaa() @pytest.mark.parametrize('raise_exception' , [False, True] ) def lowercase__ ( __snake_case : Optional[Any] , __snake_case : int ): '''simple docstring''' UpperCAmelCase_ : List[str] = str(tmp_path / 'dataset-train.arrow' ) try: with ArrowWriter(path=__a ) as writer: if raise_exception: raise pa.lib.ArrowInvalid() else: writer.stream.close() except pa.lib.ArrowInvalid: pass finally: assert writer.stream.closed def lowercase__ ( __snake_case : Tuple ): '''simple docstring''' UpperCAmelCase_ : str = 'mock://dataset-train.arrow' with ArrowWriter(path=__a , storage_options=mockfs.storage_options ) as writer: assert isinstance(writer._fs , type(__a ) ) assert writer._fs.storage_options == mockfs.storage_options writer.write({'col_1': 'foo', 'col_2': 1} ) writer.write({'col_1': 'bar', 'col_2': 2} ) UpperCAmelCase_ , UpperCAmelCase_ : int = writer.finalize() assert num_examples == 2 assert num_bytes > 0 assert mockfs.exists(__a ) def lowercase__ ( ): '''simple docstring''' UpperCAmelCase_ : int = pa.BufferOutputStream() with ParquetWriter(stream=__a ) as writer: writer.write({'col_1': 'foo', 'col_2': 1} ) writer.write({'col_1': 'bar', 'col_2': 2} ) UpperCAmelCase_ , UpperCAmelCase_ : Dict = writer.finalize() assert num_examples == 2 assert num_bytes > 0 UpperCAmelCase_ : List[str] = pa.BufferReader(output.getvalue() ) UpperCAmelCase_ : Tuple = pq.read_table(__a ) assert pa_table.to_pydict() == {"col_1": ["foo", "bar"], "col_2": [1, 2]} @require_pil @pytest.mark.parametrize('embed_local_files' , [False, True] ) def lowercase__ ( __snake_case : Dict , __snake_case : Dict ): '''simple docstring''' import PIL.Image UpperCAmelCase_ : Optional[int] = str(tmp_path / 'test_image_rgb.jpg' ) PIL.Image.fromarray(np.zeros((5, 5) , dtype=np.uinta ) ).save(__a , format='png' ) UpperCAmelCase_ : int = pa.BufferOutputStream() with ParquetWriter( stream=__a , features=Features({'image': Image()} ) , embed_local_files=__a ) as writer: writer.write({'image': image_path} ) writer.finalize() UpperCAmelCase_ : int = pa.BufferReader(output.getvalue() ) UpperCAmelCase_ : Tuple = pq.read_table(__a ) UpperCAmelCase_ : Any = pa_table.to_pydict() if embed_local_files: assert isinstance(out['image'][0]['path'] , __a ) with open(__a , 'rb' ) as f: assert out["image"][0]["bytes"] == f.read() else: assert out["image"][0]["path"] == image_path assert out["image"][0]["bytes"] is None def lowercase__ ( ): '''simple docstring''' UpperCAmelCase_ : Any = pa.schema([pa.field('col_1' , pa.string() , nullable=__a )] ) UpperCAmelCase_ : Any = pa.BufferOutputStream() with ArrowWriter(stream=__a ) as writer: writer._build_writer(inferred_schema=__a ) assert writer._schema == pa.schema([pa.field('col_1' , pa.string() )] )
29
import argparse from argparse import Namespace import torch from torch import nn from transformers import XGLMConfig, XGLMForCausalLM def __lowerCamelCase ( __a :Dict ) -> Any: """simple docstring""" A__ = [ """decoder.version""", """decoder.output_projection.weight""", """_float_tensor""", """decoder.embed_positions._float_tensor""", ] for k in ignore_keys: state_dict.pop(__a , __a ) def __lowerCamelCase ( __a :str ) -> Union[str, Any]: """simple docstring""" A__ , A__ = emb.weight.shape A__ = nn.Linear(__a , __a , bias=__a ) A__ = emb.weight.data return lin_layer def __lowerCamelCase ( __a :str ) -> List[str]: """simple docstring""" A__ = torch.load(__a , map_location="""cpu""" ) A__ = Namespace(**checkpoint["""cfg"""]["""model"""] ) A__ = checkpoint["""model"""] remove_ignore_keys_(__a ) A__ = state_dict["""decoder.embed_tokens.weight"""].shape[0] A__ = {key.replace("""decoder""" , """model""" ): val for key, val in state_dict.items()} A__ = XGLMConfig( vocab_size=__a , max_position_embeddings=args.max_target_positions , num_layers=args.decoder_layers , attention_heads=args.decoder_attention_heads , ffn_dim=args.decoder_ffn_embed_dim , d_model=args.decoder_embed_dim , layerdrop=args.decoder_layerdrop , dropout=args.dropout , attention_dropout=args.attention_dropout , activation_dropout=args.activation_dropout , activation_function="""gelu""" , scale_embedding=not args.no_scale_embedding , tie_word_embeddings=args.share_decoder_input_output_embed , ) A__ = XGLMForCausalLM(__a ) A__ = model.load_state_dict(__a , strict=__a ) print(__a ) A__ = make_linear_from_emb(model.model.embed_tokens ) return model if __name__ == "__main__": A : int = argparse.ArgumentParser() # Required parameters parser.add_argument('''fairseq_path''', type=str, help='''path to a model.pt on local filesystem.''') parser.add_argument('''pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''') A : str = parser.parse_args() A : str = convert_fairseq_xglm_checkpoint_from_disk(args.fairseq_path) model.save_pretrained(args.pytorch_dump_folder_path)
274
0
# Copyright (c) 2021-, NVIDIA CORPORATION. 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. #################################################################################################### # # Note: If when running this conversion script you're getting an exception: # ModuleNotFoundError: No module named 'megatron.model.enums' # you need to tell python where to find the clone of Megatron-LM, e.g.: # # cd /tmp # git clone https://github.com/NVIDIA/Megatron-LM # PYTHONPATH=/tmp/Megatron-LM python src/transformers/models/megatron_gpt2/convert_megatron_gpt2_checkpoint.py ... # # if you already have it cloned elsewhere, simply adjust the path to the existing path # # If the training was done using a Megatron-LM fork, e.g., # https://github.com/microsoft/Megatron-DeepSpeed/ then chances are that you need to have that one # in your path, i.e., /path/to/Megatron-DeepSpeed/ # import argparse import os import re import zipfile import torch from transformers import AutoTokenizer, GPTaConfig def _a ( UpperCamelCase_ : str , UpperCamelCase_ : str , UpperCamelCase_ : Union[str, Any]=0 ) -> str: """simple docstring""" if name is None: lowerCAmelCase__ = None else: lowerCAmelCase__ = "." * max(0 , spaces - 2 ) + "# {:" + str(50 - spaces ) + "s}" lowerCAmelCase__ = fmt.format(__a ) # Print and recurse (if needed). if isinstance(__a , __a ): if msg is not None: print(__a ) for k in val.keys(): recursive_print(__a , val[k] , spaces + 2 ) elif isinstance(__a , torch.Tensor ): print(__a , ":" , val.size() ) else: print(__a , ":" , __a ) def _a ( UpperCamelCase_ : Optional[int] , UpperCamelCase_ : Tuple , UpperCamelCase_ : Optional[Any] , UpperCamelCase_ : Tuple , UpperCamelCase_ : Optional[int] ) -> List[Any]: """simple docstring""" lowerCAmelCase__ = param.size() if checkpoint_version == 1.0: # version 1.0 stores [num_heads * hidden_size * num_splits, :] lowerCAmelCase__ = (num_heads, hidden_size, num_splits) + input_shape[1:] lowerCAmelCase__ = param.view(*__a ) lowerCAmelCase__ = param.transpose(0 , 2 ) lowerCAmelCase__ = param.transpose(1 , 2 ).contiguous() elif checkpoint_version >= 2.0: # other versions store [num_heads * num_splits * hidden_size, :] lowerCAmelCase__ = (num_heads, num_splits, hidden_size) + input_shape[1:] lowerCAmelCase__ = param.view(*__a ) lowerCAmelCase__ = param.transpose(0 , 1 ).contiguous() lowerCAmelCase__ = param.view(*__a ) return param def _a ( UpperCamelCase_ : Tuple , UpperCamelCase_ : str , UpperCamelCase_ : Tuple ) -> Union[str, Any]: """simple docstring""" lowerCAmelCase__ = {} # old versions did not store training args lowerCAmelCase__ = input_state_dict.get("args" , __a ) if ds_args is not None: # do not make the user write a config file when the exact dimensions/sizes are already in the checkpoint # from pprint import pprint # pprint(vars(ds_args)) lowerCAmelCase__ = ds_args.padded_vocab_size lowerCAmelCase__ = ds_args.max_position_embeddings lowerCAmelCase__ = ds_args.hidden_size lowerCAmelCase__ = ds_args.num_layers lowerCAmelCase__ = ds_args.num_attention_heads lowerCAmelCase__ = ds_args.ffn_hidden_size # pprint(config) # The number of heads. lowerCAmelCase__ = config.n_head # The hidden_size per head. lowerCAmelCase__ = config.n_embd // config.n_head # Megatron-LM checkpoint version if "checkpoint_version" in input_state_dict.keys(): lowerCAmelCase__ = input_state_dict["checkpoint_version"] else: lowerCAmelCase__ = 0.0 # The model. lowerCAmelCase__ = input_state_dict["model"] # The language model. lowerCAmelCase__ = model["language_model"] # The embeddings. lowerCAmelCase__ = lm["embedding"] # The word embeddings. lowerCAmelCase__ = embeddings["word_embeddings"]["weight"] # Truncate the embedding table to vocab_size rows. lowerCAmelCase__ = word_embeddings[: config.vocab_size, :] lowerCAmelCase__ = word_embeddings # The position embeddings. lowerCAmelCase__ = embeddings["position_embeddings"]["weight"] # Read the causal mask dimension (seqlen). [max_sequence_length, hidden_size] lowerCAmelCase__ = pos_embeddings.size(0 ) if n_positions != config.n_positions: raise ValueError( F"pos_embeddings.max_sequence_length={n_positions} and config.n_positions={config.n_positions} don\'t match" ) # Store the position embeddings. lowerCAmelCase__ = pos_embeddings # The transformer. lowerCAmelCase__ = lm["transformer"] if "transformer" in lm.keys() else lm["encoder"] # The regex to extract layer names. lowerCAmelCase__ = re.compile(R"layers\.(\d+)\.([a-z0-9_.]+)\.([a-z]+)" ) # The simple map of names for "automated" rules. lowerCAmelCase__ = { "attention.dense": ".attn.c_proj.", "self_attention.dense": ".attn.c_proj.", "mlp.dense_h_to_4h": ".mlp.c_fc.", "mlp.dense_4h_to_h": ".mlp.c_proj.", } # Extract the layers. for key, val in transformer.items(): # Match the name. lowerCAmelCase__ = layer_re.match(__a ) # Stop if that's not a layer if m is None: break # The index of the layer. lowerCAmelCase__ = int(m.group(1 ) ) # The name of the operation. lowerCAmelCase__ = m.group(2 ) # Is it a weight or a bias? lowerCAmelCase__ = m.group(3 ) # The name of the layer. lowerCAmelCase__ = F"transformer.h.{layer_idx}" # For layernorm(s), simply store the layer norm. if op_name.endswith("layernorm" ): lowerCAmelCase__ = "ln_1" if op_name.startswith("input" ) else "ln_2" lowerCAmelCase__ = val # Transpose the QKV matrix. elif ( op_name == "attention.query_key_value" or op_name == "self_attention.query_key_value" ) and weight_or_bias == "weight": # Insert a tensor of 1x1xDxD bias. lowerCAmelCase__ = torch.tril(torch.ones((n_positions, n_positions) , dtype=torch.floataa ) ).view( 1 , 1 , __a , __a ) lowerCAmelCase__ = causal_mask # Insert a "dummy" tensor for masked_bias. lowerCAmelCase__ = torch.tensor(-1e4 , dtype=torch.floataa ) lowerCAmelCase__ = masked_bias lowerCAmelCase__ = fix_query_key_value_ordering(__a , __a , 3 , __a , __a ) # Megatron stores (3*D) x D but transformers-GPT2 expects D x 3*D. lowerCAmelCase__ = out_val.transpose(0 , 1 ).contiguous() # Store. lowerCAmelCase__ = out_val # Transpose the bias. elif ( op_name == "attention.query_key_value" or op_name == "self_attention.query_key_value" ) and weight_or_bias == "bias": lowerCAmelCase__ = fix_query_key_value_ordering(__a , __a , 3 , __a , __a ) # Store. No change of shape. lowerCAmelCase__ = out_val # Transpose the weights. elif weight_or_bias == "weight": lowerCAmelCase__ = megatron_to_transformers[op_name] lowerCAmelCase__ = val.transpose(0 , 1 ) # Copy the bias. elif weight_or_bias == "bias": lowerCAmelCase__ = megatron_to_transformers[op_name] lowerCAmelCase__ = val # DEBUG. assert config.n_layer == layer_idx + 1 # The final layernorm. lowerCAmelCase__ = transformer["final_layernorm.weight"] lowerCAmelCase__ = transformer["final_layernorm.bias"] # For LM head, transformers' wants the matrix to weight embeddings. lowerCAmelCase__ = word_embeddings # It should be done! return output_state_dict def _a ( ) -> Dict: """simple docstring""" lowerCAmelCase__ = argparse.ArgumentParser() parser.add_argument("--print-checkpoint-structure" , action="store_true" ) parser.add_argument( "path_to_checkpoint" , type=__a , help="Path to the checkpoint file (.zip archive or direct .pt file)" , ) parser.add_argument( "--config_file" , default="" , type=__a , help="An optional config json file describing the pre-trained model." , ) lowerCAmelCase__ = parser.parse_args() # Extract the basename. lowerCAmelCase__ = os.path.dirname(args.path_to_checkpoint ) # Load the model. # the .zip is very optional, let's keep it for backward compatibility print(F"Extracting PyTorch state dictionary from {args.path_to_checkpoint}" ) if args.path_to_checkpoint.endswith(".zip" ): with zipfile.ZipFile(args.path_to_checkpoint , "r" ) as checkpoint: with checkpoint.open("release/mp_rank_00/model_optim_rng.pt" ) as pytorch_dict: lowerCAmelCase__ = torch.load(__a , map_location="cpu" ) else: lowerCAmelCase__ = torch.load(args.path_to_checkpoint , map_location="cpu" ) lowerCAmelCase__ = input_state_dict.get("args" , __a ) # Read the config, or default to the model released by NVIDIA. if args.config_file == "": if ds_args is not None: if ds_args.bias_gelu_fusion: lowerCAmelCase__ = "gelu_fast" elif ds_args.openai_gelu: lowerCAmelCase__ = "gelu_new" else: lowerCAmelCase__ = "gelu" else: # in the very early days this used to be "gelu_new" lowerCAmelCase__ = "gelu_new" # Spell out all parameters in case the defaults change. lowerCAmelCase__ = GPTaConfig( vocab_size=50_257 , n_positions=1_024 , n_embd=1_024 , n_layer=24 , n_head=16 , n_inner=4_096 , activation_function=__a , resid_pdrop=0.1 , embd_pdrop=0.1 , attn_pdrop=0.1 , layer_norm_epsilon=1e-5 , initializer_range=0.02 , summary_type="cls_index" , summary_use_proj=__a , summary_activation=__a , summary_proj_to_labels=__a , summary_first_dropout=0.1 , scale_attn_weights=__a , use_cache=__a , bos_token_id=50_256 , eos_token_id=50_256 , ) else: lowerCAmelCase__ = GPTaConfig.from_json_file(args.config_file ) lowerCAmelCase__ = ["GPT2LMHeadModel"] # Convert. print("Converting" ) lowerCAmelCase__ = convert_megatron_checkpoint(__a , __a , __a ) # Print the structure of converted state dict. if args.print_checkpoint_structure: recursive_print(__a , __a ) # Add tokenizer class info to config # see https://github.com/huggingface/transformers/issues/13906) if ds_args is not None: lowerCAmelCase__ = ds_args.tokenizer_type if tokenizer_type == "GPT2BPETokenizer": lowerCAmelCase__ = "gpt2" elif tokenizer_type == "PretrainedFromHF": lowerCAmelCase__ = ds_args.tokenizer_name_or_path else: raise ValueError(F"Unrecognized tokenizer_type {tokenizer_type}" ) else: lowerCAmelCase__ = "gpt2" lowerCAmelCase__ = AutoTokenizer.from_pretrained(__a ) lowerCAmelCase__ = type(__a ).__name__ lowerCAmelCase__ = tokenizer_class # Store the config to file. print("Saving config" ) config.save_pretrained(__a ) # Save tokenizer based on args print(F"Adding {tokenizer_class} tokenizer files" ) tokenizer.save_pretrained(__a ) # Store the state_dict to file. lowerCAmelCase__ = os.path.join(__a , "pytorch_model.bin" ) print(F"Saving checkpoint to \"{output_checkpoint_file}\"" ) torch.save(__a , __a ) #################################################################################################### if __name__ == "__main__": main() ####################################################################################################
340
import unittest import numpy as np from transformers import AlbertConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax.numpy as jnp from transformers.models.albert.modeling_flax_albert import ( FlaxAlbertForMaskedLM, FlaxAlbertForMultipleChoice, FlaxAlbertForPreTraining, FlaxAlbertForQuestionAnswering, FlaxAlbertForSequenceClassification, FlaxAlbertForTokenClassification, FlaxAlbertModel, ) class A (unittest.TestCase ): '''simple docstring''' def __init__( self : List[str] , __lowerCAmelCase : int , __lowerCAmelCase : List[str]=13 , __lowerCAmelCase : int=7 , __lowerCAmelCase : Tuple=True , __lowerCAmelCase : int=True , __lowerCAmelCase : Dict=True , __lowerCAmelCase : Union[str, Any]=True , __lowerCAmelCase : List[Any]=99 , __lowerCAmelCase : Optional[Any]=32 , __lowerCAmelCase : Optional[Any]=5 , __lowerCAmelCase : Tuple=4 , __lowerCAmelCase : Any=37 , __lowerCAmelCase : str="gelu" , __lowerCAmelCase : Optional[Any]=0.1 , __lowerCAmelCase : Union[str, Any]=0.1 , __lowerCAmelCase : List[str]=5_12 , __lowerCAmelCase : int=16 , __lowerCAmelCase : Optional[int]=2 , __lowerCAmelCase : List[Any]=0.0_2 , __lowerCAmelCase : Tuple=4 , ) -> Dict: """simple docstring""" A__ = parent A__ = batch_size A__ = seq_length A__ = is_training A__ = use_attention_mask A__ = use_token_type_ids A__ = use_labels A__ = vocab_size A__ = hidden_size A__ = num_hidden_layers A__ = num_attention_heads A__ = intermediate_size A__ = hidden_act A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = max_position_embeddings A__ = type_vocab_size A__ = type_sequence_label_size A__ = initializer_range A__ = num_choices def a_ ( self : Any ) -> str: """simple docstring""" A__ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) A__ = None if self.use_attention_mask: A__ = random_attention_mask([self.batch_size, self.seq_length] ) A__ = None if self.use_token_type_ids: A__ = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) A__ = AlbertConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=__lowerCAmelCase , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def a_ ( self : Optional[int] ) -> Optional[int]: """simple docstring""" A__ = self.prepare_config_and_inputs() A__ , A__ , A__ , A__ = config_and_inputs A__ = {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": attention_mask} return config, inputs_dict @require_flax class A (SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : str = ( ( FlaxAlbertModel, FlaxAlbertForPreTraining, FlaxAlbertForMaskedLM, FlaxAlbertForMultipleChoice, FlaxAlbertForQuestionAnswering, FlaxAlbertForSequenceClassification, FlaxAlbertForTokenClassification, FlaxAlbertForQuestionAnswering, ) if is_flax_available() else () ) def a_ ( self : str ) -> Optional[int]: """simple docstring""" A__ = FlaxAlbertModelTester(self ) @slow def a_ ( self : int ) -> Tuple: """simple docstring""" for model_class_name in self.all_model_classes: A__ = model_class_name.from_pretrained("""albert-base-v2""" ) A__ = model(np.ones((1, 1) ) ) self.assertIsNotNone(__lowerCAmelCase ) @require_flax class A (unittest.TestCase ): '''simple docstring''' @slow def a_ ( self : Dict ) -> List[Any]: """simple docstring""" A__ = FlaxAlbertModel.from_pretrained("""albert-base-v2""" ) A__ = np.array([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]] ) A__ = np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) A__ = model(__lowerCAmelCase , attention_mask=__lowerCAmelCase )[0] A__ = (1, 11, 7_68) self.assertEqual(output.shape , __lowerCAmelCase ) A__ = np.array( [[[-0.6_5_1_3, 1.5_0_3_5, -0.2_7_6_6], [-0.6_5_1_5, 1.5_0_4_6, -0.2_7_8_0], [-0.6_5_1_2, 1.5_0_4_9, -0.2_7_8_4]]] ) self.assertTrue(jnp.allclose(output[:, 1:4, 1:4] , __lowerCAmelCase , atol=1e-4 ) )
274
0
import logging import math import os from dataclasses import dataclass, field from glob import glob from typing import Optional from torch.utils.data import ConcatDataset import transformers from transformers import ( CONFIG_MAPPING, MODEL_WITH_LM_HEAD_MAPPING, AutoConfig, AutoModelWithLMHead, AutoTokenizer, DataCollatorForLanguageModeling, DataCollatorForPermutationLanguageModeling, DataCollatorForWholeWordMask, HfArgumentParser, LineByLineTextDataset, LineByLineWithRefDataset, PreTrainedTokenizer, TextDataset, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import is_main_process _A = logging.getLogger(__name__) _A = list(MODEL_WITH_LM_HEAD_MAPPING.keys()) _A = tuple(conf.model_type for conf in MODEL_CONFIG_CLASSES) @dataclass class lowercase_ : A__ : Optional[str] = field( default=__SCREAMING_SNAKE_CASE , metadata={ """help""": ( """The model checkpoint for weights initialization. Leave None if you want to train a model from""" """ scratch.""" ) } , ) A__ : Optional[str] = field( default=__SCREAMING_SNAKE_CASE , metadata={"""help""": """If training from scratch, pass a model type from the list: """ + """, """.join(__SCREAMING_SNAKE_CASE )} , ) A__ : Optional[str] = field( default=__SCREAMING_SNAKE_CASE , metadata={"""help""": """Pretrained config name or path if not the same as model_name"""} ) A__ : Optional[str] = field( default=__SCREAMING_SNAKE_CASE , metadata={"""help""": """Pretrained tokenizer name or path if not the same as model_name"""} ) A__ : Optional[str] = field( default=__SCREAMING_SNAKE_CASE , metadata={"""help""": """Where do you want to store the pretrained models downloaded from huggingface.co"""} , ) @dataclass class lowercase_ : A__ : Optional[str] = field( default=__SCREAMING_SNAKE_CASE , metadata={"""help""": """The input training data file (a text file)."""} ) A__ : Optional[str] = field( default=__SCREAMING_SNAKE_CASE , metadata={ """help""": ( """The input training data files (multiple files in glob format). """ """Very often splitting large files to smaller files can prevent tokenizer going out of memory""" ) } , ) A__ : Optional[str] = field( default=__SCREAMING_SNAKE_CASE , metadata={"""help""": """An optional input evaluation data file to evaluate the perplexity on (a text file)."""} , ) A__ : Optional[str] = field( default=__SCREAMING_SNAKE_CASE , metadata={"""help""": """An optional input train ref data file for whole word mask in Chinese."""} , ) A__ : Optional[str] = field( default=__SCREAMING_SNAKE_CASE , metadata={"""help""": """An optional input eval ref data file for whole word mask in Chinese."""} , ) A__ : bool = field( default=__SCREAMING_SNAKE_CASE , metadata={"""help""": """Whether distinct lines of text in the dataset are to be handled as distinct sequences."""} , ) A__ : bool = field( default=__SCREAMING_SNAKE_CASE , metadata={"""help""": """Train with masked-language modeling loss instead of language modeling."""} ) A__ : bool = field(default=__SCREAMING_SNAKE_CASE , metadata={"""help""": """Whether ot not to use whole word mask."""} ) A__ : float = field( default=0.15 , metadata={"""help""": """Ratio of tokens to mask for masked language modeling loss"""} ) A__ : float = field( default=1 / 6 , metadata={ """help""": ( """Ratio of length of a span of masked tokens to surrounding context length for permutation language""" """ modeling.""" ) } , ) A__ : int = field( default=5 , metadata={"""help""": """Maximum length of a span of masked tokens for permutation language modeling."""} ) A__ : int = field( default=-1 , metadata={ """help""": ( """Optional input sequence length after tokenization.""" """The training dataset will be truncated in block of this size for training.""" """Default to the model max input length for single sentence inputs (take into account special tokens).""" ) } , ) A__ : bool = field( default=__SCREAMING_SNAKE_CASE , metadata={"""help""": """Overwrite the cached training and evaluation sets"""} ) def lowerCamelCase__ ( a__ : DataTrainingArguments , a__ : PreTrainedTokenizer , a__ : bool = False , a__ : Optional[str] = None , ) -> int: def _dataset(a__ : Any , a__ : Tuple=None ): if args.line_by_line: if ref_path is not None: if not args.whole_word_mask or not args.mlm: raise ValueError("""You need to set world whole masking and mlm to True for Chinese Whole Word Mask""" ) return LineByLineWithRefDataset( tokenizer=__a , file_path=__a , block_size=args.block_size , ref_path=__a , ) return LineByLineTextDataset(tokenizer=__a , file_path=__a , block_size=args.block_size ) else: return TextDataset( tokenizer=__a , file_path=__a , block_size=args.block_size , overwrite_cache=args.overwrite_cache , cache_dir=__a , ) if evaluate: return _dataset(args.eval_data_file , args.eval_ref_file ) elif args.train_data_files: return ConcatDataset([_dataset(__a ) for f in glob(args.train_data_files )] ) else: return _dataset(args.train_data_file , args.train_ref_file ) def lowerCamelCase__ ( ) -> str: UpperCamelCase_ = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ = parser.parse_args_into_dataclasses() if data_args.eval_data_file is None and training_args.do_eval: raise ValueError( """Cannot do evaluation without an evaluation data file. Either supply a file to --eval_data_file """ """or remove the --do_eval argument.""" ) if ( os.path.exists(training_args.output_dir ) and os.listdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir ): raise ValueError( f'''Output directory ({training_args.output_dir}) already exists and is not empty. Use''' """ --overwrite_output_dir to overcome.""" ) # Setup logging logging.basicConfig( format="""%(asctime)s - %(levelname)s - %(name)s - %(message)s""" , datefmt="""%m/%d/%Y %H:%M:%S""" , level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN , ) logger.warning( """Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s""" , training_args.local_rank , training_args.device , training_args.n_gpu , bool(training_args.local_rank != -1 ) , training_args.fpaa , ) # Set the verbosity to info of the Transformers logger (on main process only): if is_main_process(training_args.local_rank ): transformers.utils.logging.set_verbosity_info() transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() logger.info("""Training/evaluation parameters %s""" , __a ) # Set seed set_seed(training_args.seed ) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. if model_args.config_name: UpperCamelCase_ = AutoConfig.from_pretrained(model_args.config_name , cache_dir=model_args.cache_dir ) elif model_args.model_name_or_path: UpperCamelCase_ = AutoConfig.from_pretrained(model_args.model_name_or_path , cache_dir=model_args.cache_dir ) else: UpperCamelCase_ = CONFIG_MAPPING[model_args.model_type]() logger.warning("""You are instantiating a new config instance from scratch.""" ) if model_args.tokenizer_name: UpperCamelCase_ = AutoTokenizer.from_pretrained(model_args.tokenizer_name , cache_dir=model_args.cache_dir ) elif model_args.model_name_or_path: UpperCamelCase_ = AutoTokenizer.from_pretrained(model_args.model_name_or_path , cache_dir=model_args.cache_dir ) else: raise ValueError( """You are instantiating a new tokenizer from scratch. This is not supported, but you can do it from another""" """ script, save it,and load it from here, using --tokenizer_name""" ) if model_args.model_name_or_path: UpperCamelCase_ = AutoModelWithLMHead.from_pretrained( model_args.model_name_or_path , from_tf=bool(""".ckpt""" in model_args.model_name_or_path ) , config=__a , cache_dir=model_args.cache_dir , ) else: logger.info("""Training new model from scratch""" ) UpperCamelCase_ = AutoModelWithLMHead.from_config(__a ) model.resize_token_embeddings(len(__a ) ) if config.model_type in ["bert", "roberta", "distilbert", "camembert"] and not data_args.mlm: raise ValueError( """BERT and RoBERTa-like models do not have LM heads but masked LM heads. They must be run using the""" """--mlm flag (masked language modeling).""" ) if data_args.block_size <= 0: UpperCamelCase_ = tokenizer.max_len # Our input block size will be the max possible for the model else: UpperCamelCase_ = min(data_args.block_size , tokenizer.max_len ) # Get datasets UpperCamelCase_ = ( get_dataset(__a , tokenizer=__a , cache_dir=model_args.cache_dir ) if training_args.do_train else None ) UpperCamelCase_ = ( get_dataset(__a , tokenizer=__a , evaluate=__a , cache_dir=model_args.cache_dir ) if training_args.do_eval else None ) if config.model_type == "xlnet": UpperCamelCase_ = DataCollatorForPermutationLanguageModeling( tokenizer=__a , plm_probability=data_args.plm_probability , max_span_length=data_args.max_span_length , ) else: if data_args.mlm and data_args.whole_word_mask: UpperCamelCase_ = DataCollatorForWholeWordMask( tokenizer=__a , mlm_probability=data_args.mlm_probability ) else: UpperCamelCase_ = DataCollatorForLanguageModeling( tokenizer=__a , mlm=data_args.mlm , mlm_probability=data_args.mlm_probability ) # Initialize our Trainer UpperCamelCase_ = Trainer( model=__a , args=__a , data_collator=__a , train_dataset=__a , eval_dataset=__a , prediction_loss_only=__a , ) # Training if training_args.do_train: UpperCamelCase_ = ( model_args.model_name_or_path if model_args.model_name_or_path is not None and os.path.isdir(model_args.model_name_or_path ) else None ) trainer.train(model_path=__a ) trainer.save_model() # For convenience, we also re-save the tokenizer to the same directory, # so that you can share your model easily on huggingface.co/models =) if trainer.is_world_master(): tokenizer.save_pretrained(training_args.output_dir ) # Evaluation UpperCamelCase_ = {} if training_args.do_eval: logger.info("""*** Evaluate ***""" ) UpperCamelCase_ = trainer.evaluate() UpperCamelCase_ = math.exp(eval_output["""eval_loss"""] ) UpperCamelCase_ = {"""perplexity""": perplexity} UpperCamelCase_ = os.path.join(training_args.output_dir , """eval_results_lm.txt""" ) if trainer.is_world_master(): with open(__a , """w""" ) as writer: logger.info("""***** Eval results *****""" ) for key in sorted(result.keys() ): logger.info(""" %s = %s""" , __a , str(result[key] ) ) writer.write("""%s = %s\n""" % (key, str(result[key] )) ) results.update(__a ) return results def lowerCamelCase__ ( a__ : Optional[Any] ) -> Dict: main() if __name__ == "__main__": main()
122
from sklearn.metrics import fa_score import datasets A : Any = ''' The F1 score is the harmonic mean of the precision and recall. It can be computed with the equation: F1 = 2 * (precision * recall) / (precision + recall) ''' A : List[Any] = ''' Args: predictions (`list` of `int`): Predicted labels. references (`list` of `int`): Ground truth labels. labels (`list` of `int`): The set of labels to include when `average` is not set to `\'binary\'`, and the order of the labels if `average` is `None`. Labels present in the data can be excluded, for example to calculate a multiclass average ignoring a majority negative class. Labels not present in the data will result in 0 components in a macro average. For multilabel targets, labels are column indices. By default, all labels in `predictions` and `references` are used in sorted order. Defaults to None. pos_label (`int`): The class to be considered the positive class, in the case where `average` is set to `binary`. Defaults to 1. average (`string`): This parameter is required for multiclass/multilabel targets. If set to `None`, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data. Defaults to `\'binary\'`. - \'binary\': Only report results for the class specified by `pos_label`. This is applicable only if the classes found in `predictions` and `references` are binary. - \'micro\': Calculate metrics globally by counting the total true positives, false negatives and false positives. - \'macro\': Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account. - \'weighted\': Calculate metrics for each label, and find their average weighted by support (the number of true instances for each label). This alters `\'macro\'` to account for label imbalance. This option can result in an F-score that is not between precision and recall. - \'samples\': Calculate metrics for each instance, and find their average (only meaningful for multilabel classification). sample_weight (`list` of `float`): Sample weights Defaults to None. Returns: f1 (`float` or `array` of `float`): F1 score or list of f1 scores, depending on the value passed to `average`. Minimum possible value is 0. Maximum possible value is 1. Higher f1 scores are better. Examples: Example 1-A simple binary example >>> f1_metric = datasets.load_metric("f1") >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0]) >>> print(results) {\'f1\': 0.5} Example 2-The same simple binary example as in Example 1, but with `pos_label` set to `0`. >>> f1_metric = datasets.load_metric("f1") >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0], pos_label=0) >>> print(round(results[\'f1\'], 2)) 0.67 Example 3-The same simple binary example as in Example 1, but with `sample_weight` included. >>> f1_metric = datasets.load_metric("f1") >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0], sample_weight=[0.9, 0.5, 3.9, 1.2, 0.3]) >>> print(round(results[\'f1\'], 2)) 0.35 Example 4-A multiclass example, with different values for the `average` input. >>> predictions = [0, 2, 1, 0, 0, 1] >>> references = [0, 1, 2, 0, 1, 2] >>> results = f1_metric.compute(predictions=predictions, references=references, average="macro") >>> print(round(results[\'f1\'], 2)) 0.27 >>> results = f1_metric.compute(predictions=predictions, references=references, average="micro") >>> print(round(results[\'f1\'], 2)) 0.33 >>> results = f1_metric.compute(predictions=predictions, references=references, average="weighted") >>> print(round(results[\'f1\'], 2)) 0.27 >>> results = f1_metric.compute(predictions=predictions, references=references, average=None) >>> print(results) {\'f1\': array([0.8, 0. , 0. ])} ''' A : List[Any] = ''' @article{scikit-learn, title={Scikit-learn: Machine Learning in {P}ython}, author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V. and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P. and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.}, journal={Journal of Machine Learning Research}, volume={12}, pages={2825--2830}, year={2011} } ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class A (datasets.Metric ): '''simple docstring''' def a_ ( self : Optional[int] ) -> List[Any]: """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": datasets.Sequence(datasets.Value("""int32""" ) ), """references""": datasets.Sequence(datasets.Value("""int32""" ) ), } if self.config_name == """multilabel""" else { """predictions""": datasets.Value("""int32""" ), """references""": datasets.Value("""int32""" ), } ) , reference_urls=["""https://scikit-learn.org/stable/modules/generated/sklearn.metrics.f1_score.html"""] , ) def a_ ( self : Any , __lowerCAmelCase : Tuple , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : Dict=None , __lowerCAmelCase : List[str]=1 , __lowerCAmelCase : Any="binary" , __lowerCAmelCase : Optional[int]=None ) -> List[Any]: """simple docstring""" A__ = fa_score( __lowerCAmelCase , __lowerCAmelCase , labels=__lowerCAmelCase , pos_label=__lowerCAmelCase , average=__lowerCAmelCase , sample_weight=__lowerCAmelCase ) return {"f1": float(__lowerCAmelCase ) if score.size == 1 else score}
274
0
import warnings from ...utils import logging from .image_processing_yolos import YolosImageProcessor UpperCAmelCase__ = logging.get_logger(__name__) class lowerCamelCase__ ( lowerCAmelCase): def __init__(self , *UpperCAmelCase , **UpperCAmelCase ) -> None: warnings.warn( '''The class YolosFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please''' ''' use YolosImageProcessor instead.''' , __lowerCAmelCase , ) super().__init__(*__lowerCAmelCase , **__lowerCAmelCase )
5
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging A : Union[str, Any] = logging.get_logger(__name__) A : int = { '''xlm-roberta-base''': '''https://huggingface.co/xlm-roberta-base/resolve/main/config.json''', '''xlm-roberta-large''': '''https://huggingface.co/xlm-roberta-large/resolve/main/config.json''', '''xlm-roberta-large-finetuned-conll02-dutch''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll02-dutch/resolve/main/config.json''' ), '''xlm-roberta-large-finetuned-conll02-spanish''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll02-spanish/resolve/main/config.json''' ), '''xlm-roberta-large-finetuned-conll03-english''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll03-english/resolve/main/config.json''' ), '''xlm-roberta-large-finetuned-conll03-german''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll03-german/resolve/main/config.json''' ), } class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : Any = '''xlm-roberta''' def __init__( self : Optional[Any] , __lowerCAmelCase : List[Any]=3_05_22 , __lowerCAmelCase : int=7_68 , __lowerCAmelCase : Tuple=12 , __lowerCAmelCase : str=12 , __lowerCAmelCase : Union[str, Any]=30_72 , __lowerCAmelCase : Union[str, Any]="gelu" , __lowerCAmelCase : Optional[int]=0.1 , __lowerCAmelCase : Dict=0.1 , __lowerCAmelCase : List[str]=5_12 , __lowerCAmelCase : Tuple=2 , __lowerCAmelCase : Dict=0.0_2 , __lowerCAmelCase : List[str]=1e-12 , __lowerCAmelCase : Union[str, Any]=1 , __lowerCAmelCase : str=0 , __lowerCAmelCase : Optional[int]=2 , __lowerCAmelCase : Tuple="absolute" , __lowerCAmelCase : Any=True , __lowerCAmelCase : Any=None , **__lowerCAmelCase : str , ) -> Optional[Any]: """simple docstring""" super().__init__(pad_token_id=__lowerCAmelCase , bos_token_id=__lowerCAmelCase , eos_token_id=__lowerCAmelCase , **__lowerCAmelCase ) A__ = vocab_size A__ = hidden_size A__ = num_hidden_layers A__ = num_attention_heads A__ = hidden_act A__ = intermediate_size A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = max_position_embeddings A__ = type_vocab_size A__ = initializer_range A__ = layer_norm_eps A__ = position_embedding_type A__ = use_cache A__ = classifier_dropout class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' @property def a_ ( self : int ) -> Mapping[str, Mapping[int, str]]: """simple docstring""" if self.task == "multiple-choice": A__ = {0: """batch""", 1: """choice""", 2: """sequence"""} else: A__ = {0: """batch""", 1: """sequence"""} return OrderedDict( [ ("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ] )
274
0
'''simple docstring''' from __future__ import annotations import time from math import sqrt # 1 for manhattan, 0 for euclidean _SCREAMING_SNAKE_CASE = 0 _SCREAMING_SNAKE_CASE = [ [0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles [0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0], [1, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0], ] _SCREAMING_SNAKE_CASE = [[-1, 0], [0, -1], [1, 0], [0, 1]] # up, left, down, right _SCREAMING_SNAKE_CASE = tuple[int, int] class lowerCAmelCase_ : def __init__( self , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , ) -> None: _lowerCAmelCase = pos_x _lowerCAmelCase = pos_y _lowerCAmelCase = (pos_y, pos_x) _lowerCAmelCase = goal_x _lowerCAmelCase = goal_y _lowerCAmelCase = g_cost _lowerCAmelCase = parent _lowerCAmelCase = self.calculate_heuristic() _lowerCAmelCase = self.g_cost + self.h_cost def _snake_case ( self ) -> float: _lowerCAmelCase = self.pos_x - self.goal_x _lowerCAmelCase = self.pos_y - self.goal_y if HEURISTIC == 1: return abs(__lowerCAmelCase ) + abs(__lowerCAmelCase ) else: return sqrt(dy**2 + dx**2 ) def __lt__( self , _lowerCAmelCase ) -> bool: return self.f_cost < other.f_cost class lowerCAmelCase_ : def __init__( self , _lowerCAmelCase , _lowerCAmelCase ) -> Tuple: _lowerCAmelCase = Node(start[1] , start[0] , goal[1] , goal[0] , 0 , __lowerCAmelCase ) _lowerCAmelCase = Node(goal[1] , goal[0] , goal[1] , goal[0] , 99999 , __lowerCAmelCase ) _lowerCAmelCase = [self.start] _lowerCAmelCase = [] _lowerCAmelCase = False def _snake_case ( self ) -> list[TPosition]: while self.open_nodes: # Open Nodes are sorted using __lt__ self.open_nodes.sort() _lowerCAmelCase = self.open_nodes.pop(0 ) if current_node.pos == self.target.pos: return self.retrace_path(__lowerCAmelCase ) self.closed_nodes.append(__lowerCAmelCase ) _lowerCAmelCase = self.get_successors(__lowerCAmelCase ) for child_node in successors: if child_node in self.closed_nodes: continue if child_node not in self.open_nodes: self.open_nodes.append(__lowerCAmelCase ) else: # retrieve the best current path _lowerCAmelCase = self.open_nodes.pop(self.open_nodes.index(__lowerCAmelCase ) ) if child_node.g_cost < better_node.g_cost: self.open_nodes.append(__lowerCAmelCase ) else: self.open_nodes.append(__lowerCAmelCase ) return [self.start.pos] def _snake_case ( self , _lowerCAmelCase ) -> list[Node]: _lowerCAmelCase = [] for action in delta: _lowerCAmelCase = parent.pos_x + action[1] _lowerCAmelCase = parent.pos_y + action[0] if not (0 <= pos_x <= len(grid[0] ) - 1 and 0 <= pos_y <= len(__lowerCAmelCase ) - 1): continue if grid[pos_y][pos_x] != 0: continue successors.append( Node( __lowerCAmelCase , __lowerCAmelCase , self.target.pos_y , self.target.pos_x , parent.g_cost + 1 , __lowerCAmelCase , ) ) return successors def _snake_case ( self , _lowerCAmelCase ) -> list[TPosition]: _lowerCAmelCase = node _lowerCAmelCase = [] while current_node is not None: path.append((current_node.pos_y, current_node.pos_x) ) _lowerCAmelCase = current_node.parent path.reverse() return path class lowerCAmelCase_ : def __init__( self , _lowerCAmelCase , _lowerCAmelCase ) -> None: _lowerCAmelCase = AStar(__lowerCAmelCase , __lowerCAmelCase ) _lowerCAmelCase = AStar(__lowerCAmelCase , __lowerCAmelCase ) _lowerCAmelCase = False def _snake_case ( self ) -> list[TPosition]: while self.fwd_astar.open_nodes or self.bwd_astar.open_nodes: self.fwd_astar.open_nodes.sort() self.bwd_astar.open_nodes.sort() _lowerCAmelCase = self.fwd_astar.open_nodes.pop(0 ) _lowerCAmelCase = self.bwd_astar.open_nodes.pop(0 ) if current_bwd_node.pos == current_fwd_node.pos: return self.retrace_bidirectional_path( __lowerCAmelCase , __lowerCAmelCase ) self.fwd_astar.closed_nodes.append(__lowerCAmelCase ) self.bwd_astar.closed_nodes.append(__lowerCAmelCase ) _lowerCAmelCase = current_bwd_node _lowerCAmelCase = current_fwd_node _lowerCAmelCase = { self.fwd_astar: self.fwd_astar.get_successors(__lowerCAmelCase ), self.bwd_astar: self.bwd_astar.get_successors(__lowerCAmelCase ), } for astar in [self.fwd_astar, self.bwd_astar]: for child_node in successors[astar]: if child_node in astar.closed_nodes: continue if child_node not in astar.open_nodes: astar.open_nodes.append(__lowerCAmelCase ) else: # retrieve the best current path _lowerCAmelCase = astar.open_nodes.pop( astar.open_nodes.index(__lowerCAmelCase ) ) if child_node.g_cost < better_node.g_cost: astar.open_nodes.append(__lowerCAmelCase ) else: astar.open_nodes.append(__lowerCAmelCase ) return [self.fwd_astar.start.pos] def _snake_case ( self , _lowerCAmelCase , _lowerCAmelCase ) -> list[TPosition]: _lowerCAmelCase = self.fwd_astar.retrace_path(__lowerCAmelCase ) _lowerCAmelCase = self.bwd_astar.retrace_path(__lowerCAmelCase ) bwd_path.pop() bwd_path.reverse() _lowerCAmelCase = fwd_path + bwd_path return path if __name__ == "__main__": # all coordinates are given in format [y,x] _SCREAMING_SNAKE_CASE = (0, 0) _SCREAMING_SNAKE_CASE = (len(grid) - 1, len(grid[0]) - 1) for elem in grid: print(elem) _SCREAMING_SNAKE_CASE = time.time() _SCREAMING_SNAKE_CASE = AStar(init, goal) _SCREAMING_SNAKE_CASE = a_star.search() _SCREAMING_SNAKE_CASE = time.time() - start_time print(f'''AStar execution time = {end_time:f} seconds''') _SCREAMING_SNAKE_CASE = time.time() _SCREAMING_SNAKE_CASE = BidirectionalAStar(init, goal) _SCREAMING_SNAKE_CASE = time.time() - bd_start_time print(f'''BidirectionalAStar execution time = {bd_end_time:f} seconds''')
158
from __future__ import annotations import time from math import sqrt # 1 for manhattan, 0 for euclidean A : str = 0 A : Any = [ [0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles [0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0], [1, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0], ] A : Dict = [[-1, 0], [0, -1], [1, 0], [0, 1]] # up, left, down, right A : Union[str, Any] = tuple[int, int] class A : '''simple docstring''' def __init__( self : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : Node | None , ) -> None: """simple docstring""" A__ = pos_x A__ = pos_y A__ = (pos_y, pos_x) A__ = goal_x A__ = goal_y A__ = g_cost A__ = parent A__ = self.calculate_heuristic() A__ = self.g_cost + self.h_cost def a_ ( self : Dict ) -> float: """simple docstring""" A__ = self.pos_x - self.goal_x A__ = self.pos_y - self.goal_y if HEURISTIC == 1: return abs(__lowerCAmelCase ) + abs(__lowerCAmelCase ) else: return sqrt(dy**2 + dx**2 ) def __lt__( self : int , __lowerCAmelCase : Node ) -> bool: """simple docstring""" return self.f_cost < other.f_cost class A : '''simple docstring''' def __init__( self : Union[str, Any] , __lowerCAmelCase : TPosition , __lowerCAmelCase : TPosition ) -> Tuple: """simple docstring""" A__ = Node(start[1] , start[0] , goal[1] , goal[0] , 0 , __lowerCAmelCase ) A__ = Node(goal[1] , goal[0] , goal[1] , goal[0] , 9_99_99 , __lowerCAmelCase ) A__ = [self.start] A__ = [] A__ = False def a_ ( self : List[str] ) -> list[TPosition]: """simple docstring""" while self.open_nodes: # Open Nodes are sorted using __lt__ self.open_nodes.sort() A__ = self.open_nodes.pop(0 ) if current_node.pos == self.target.pos: return self.retrace_path(__lowerCAmelCase ) self.closed_nodes.append(__lowerCAmelCase ) A__ = self.get_successors(__lowerCAmelCase ) for child_node in successors: if child_node in self.closed_nodes: continue if child_node not in self.open_nodes: self.open_nodes.append(__lowerCAmelCase ) else: # retrieve the best current path A__ = self.open_nodes.pop(self.open_nodes.index(__lowerCAmelCase ) ) if child_node.g_cost < better_node.g_cost: self.open_nodes.append(__lowerCAmelCase ) else: self.open_nodes.append(__lowerCAmelCase ) return [self.start.pos] def a_ ( self : Optional[Any] , __lowerCAmelCase : Node ) -> list[Node]: """simple docstring""" A__ = [] for action in delta: A__ = parent.pos_x + action[1] A__ = parent.pos_y + action[0] if not (0 <= pos_x <= len(grid[0] ) - 1 and 0 <= pos_y <= len(__lowerCAmelCase ) - 1): continue if grid[pos_y][pos_x] != 0: continue successors.append( Node( __lowerCAmelCase , __lowerCAmelCase , self.target.pos_y , self.target.pos_x , parent.g_cost + 1 , __lowerCAmelCase , ) ) return successors def a_ ( self : List[Any] , __lowerCAmelCase : Node | None ) -> list[TPosition]: """simple docstring""" A__ = node A__ = [] while current_node is not None: path.append((current_node.pos_y, current_node.pos_x) ) A__ = current_node.parent path.reverse() return path class A : '''simple docstring''' def __init__( self : Optional[Any] , __lowerCAmelCase : TPosition , __lowerCAmelCase : TPosition ) -> None: """simple docstring""" A__ = AStar(__lowerCAmelCase , __lowerCAmelCase ) A__ = AStar(__lowerCAmelCase , __lowerCAmelCase ) A__ = False def a_ ( self : int ) -> list[TPosition]: """simple docstring""" while self.fwd_astar.open_nodes or self.bwd_astar.open_nodes: self.fwd_astar.open_nodes.sort() self.bwd_astar.open_nodes.sort() A__ = self.fwd_astar.open_nodes.pop(0 ) A__ = self.bwd_astar.open_nodes.pop(0 ) if current_bwd_node.pos == current_fwd_node.pos: return self.retrace_bidirectional_path( __lowerCAmelCase , __lowerCAmelCase ) self.fwd_astar.closed_nodes.append(__lowerCAmelCase ) self.bwd_astar.closed_nodes.append(__lowerCAmelCase ) A__ = current_bwd_node A__ = current_fwd_node A__ = { self.fwd_astar: self.fwd_astar.get_successors(__lowerCAmelCase ), self.bwd_astar: self.bwd_astar.get_successors(__lowerCAmelCase ), } for astar in [self.fwd_astar, self.bwd_astar]: for child_node in successors[astar]: if child_node in astar.closed_nodes: continue if child_node not in astar.open_nodes: astar.open_nodes.append(__lowerCAmelCase ) else: # retrieve the best current path A__ = astar.open_nodes.pop( astar.open_nodes.index(__lowerCAmelCase ) ) if child_node.g_cost < better_node.g_cost: astar.open_nodes.append(__lowerCAmelCase ) else: astar.open_nodes.append(__lowerCAmelCase ) return [self.fwd_astar.start.pos] def a_ ( self : List[str] , __lowerCAmelCase : Node , __lowerCAmelCase : Node ) -> list[TPosition]: """simple docstring""" A__ = self.fwd_astar.retrace_path(__lowerCAmelCase ) A__ = self.bwd_astar.retrace_path(__lowerCAmelCase ) bwd_path.pop() bwd_path.reverse() A__ = fwd_path + bwd_path return path if __name__ == "__main__": # all coordinates are given in format [y,x] A : Optional[int] = (0, 0) A : int = (len(grid) - 1, len(grid[0]) - 1) for elem in grid: print(elem) A : Dict = time.time() A : Optional[Any] = AStar(init, goal) A : Optional[int] = a_star.search() A : Optional[int] = time.time() - start_time print(F'''AStar execution time = {end_time:f} seconds''') A : Dict = time.time() A : Tuple = BidirectionalAStar(init, goal) A : List[Any] = time.time() - bd_start_time print(F'''BidirectionalAStar execution time = {bd_end_time:f} seconds''')
274
0
from typing import List, Optional, Tuple, Union import torch from ...models import UNetaDModel from ...schedulers import KarrasVeScheduler from ...utils import randn_tensor from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput class __A ( lowerCAmelCase ): '''simple docstring''' lowerCAmelCase_ = 42 lowerCAmelCase_ = 42 def __init__( self , __lowerCAmelCase , __lowerCAmelCase ): '''simple docstring''' super().__init__() self.register_modules(unet=__lowerCAmelCase , scheduler=__lowerCAmelCase ) @torch.no_grad() def __call__( self , __lowerCAmelCase = 1 , __lowerCAmelCase = 5_0 , __lowerCAmelCase = None , __lowerCAmelCase = "pil" , __lowerCAmelCase = True , **__lowerCAmelCase , ): '''simple docstring''' lowerCamelCase__ = self.unet.config.sample_size lowerCamelCase__ = (batch_size, 3, img_size, img_size) lowerCamelCase__ = self.unet # sample x_0 ~ N(0, sigma_0^2 * I) lowerCamelCase__ = randn_tensor(__lowerCAmelCase , generator=__lowerCAmelCase , device=self.device ) * self.scheduler.init_noise_sigma self.scheduler.set_timesteps(__lowerCAmelCase ) for t in self.progress_bar(self.scheduler.timesteps ): # here sigma_t == t_i from the paper lowerCamelCase__ = self.scheduler.schedule[t] lowerCamelCase__ = self.scheduler.schedule[t - 1] if t > 0 else 0 # 1. Select temporarily increased noise level sigma_hat # 2. Add new noise to move from sample_i to sample_hat lowerCamelCase__ , lowerCamelCase__ = self.scheduler.add_noise_to_input(__lowerCAmelCase , __lowerCAmelCase , generator=__lowerCAmelCase ) # 3. Predict the noise residual given the noise magnitude `sigma_hat` # The model inputs and output are adjusted by following eq. (213) in [1]. lowerCamelCase__ = (sigma_hat / 2) * model((sample_hat + 1) / 2 , sigma_hat / 2 ).sample # 4. Evaluate dx/dt at sigma_hat # 5. Take Euler step from sigma to sigma_prev lowerCamelCase__ = self.scheduler.step(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) if sigma_prev != 0: # 6. Apply 2nd order correction # The model inputs and output are adjusted by following eq. (213) in [1]. lowerCamelCase__ = (sigma_prev / 2) * model((step_output.prev_sample + 1) / 2 , sigma_prev / 2 ).sample lowerCamelCase__ = self.scheduler.step_correct( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , step_output.prev_sample , step_output['''derivative'''] , ) lowerCamelCase__ = step_output.prev_sample lowerCamelCase__ = (sample / 2 + 0.5).clamp(0 , 1 ) lowerCamelCase__ = sample.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": lowerCamelCase__ = self.numpy_to_pil(__lowerCAmelCase ) if not return_dict: return (image,) return ImagePipelineOutput(images=__lowerCAmelCase )
209
import unittest from transformers.utils.backbone_utils import ( BackboneMixin, get_aligned_output_features_output_indices, verify_out_features_out_indices, ) class A (unittest.TestCase ): '''simple docstring''' def a_ ( self : Any ) -> Union[str, Any]: """simple docstring""" A__ = ["""a""", """b""", """c"""] # Defaults to last layer if both are None A__ , A__ = get_aligned_output_features_output_indices(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , ["""c"""] ) self.assertEqual(__lowerCAmelCase , [2] ) # Out indices set to match out features A__ , A__ = get_aligned_output_features_output_indices(["""a""", """c"""] , __lowerCAmelCase , __lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , ["""a""", """c"""] ) self.assertEqual(__lowerCAmelCase , [0, 2] ) # Out features set to match out indices A__ , A__ = get_aligned_output_features_output_indices(__lowerCAmelCase , [0, 2] , __lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , ["""a""", """c"""] ) self.assertEqual(__lowerCAmelCase , [0, 2] ) # Out features selected from negative indices A__ , A__ = get_aligned_output_features_output_indices(__lowerCAmelCase , [-3, -1] , __lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , ["""a""", """c"""] ) self.assertEqual(__lowerCAmelCase , [-3, -1] ) def a_ ( self : Optional[Any] ) -> Optional[Any]: """simple docstring""" with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(["""a""", """b"""] , (0, 1) , __lowerCAmelCase ) # Out features must be a list with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(("""a""", """b""") , (0, 1) , ["""a""", """b"""] ) # Out features must be a subset of stage names with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(["""a""", """b"""] , (0, 1) , ["""a"""] ) # Out indices must be a list or tuple with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(__lowerCAmelCase , 0 , ["""a""", """b"""] ) # Out indices must be a subset of stage names with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(__lowerCAmelCase , (0, 1) , ["""a"""] ) # Out features and out indices must be the same length with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(["""a""", """b"""] , (0,) , ["""a""", """b""", """c"""] ) # Out features should match out indices with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(["""a""", """b"""] , (0, 2) , ["""a""", """b""", """c"""] ) # Out features and out indices should be in order with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(["""b""", """a"""] , (0, 1) , ["""a""", """b"""] ) # Check passes with valid inputs verify_out_features_out_indices(["""a""", """b""", """d"""] , (0, 1, -1) , ["""a""", """b""", """c""", """d"""] ) def a_ ( self : Union[str, Any] ) -> List[str]: """simple docstring""" A__ = BackboneMixin() A__ = ["""a""", """b""", """c"""] A__ = ["""a""", """c"""] A__ = [0, 2] # Check that the output features and indices are set correctly self.assertEqual(backbone.out_features , ["""a""", """c"""] ) self.assertEqual(backbone.out_indices , [0, 2] ) # Check out features and indices are updated correctly A__ = ["""a""", """b"""] self.assertEqual(backbone.out_features , ["""a""", """b"""] ) self.assertEqual(backbone.out_indices , [0, 1] ) A__ = [-3, -1] self.assertEqual(backbone.out_features , ["""a""", """c"""] ) self.assertEqual(backbone.out_indices , [-3, -1] )
274
0
from collections import deque from math import floor from random import random from time import time class _lowercase : """simple docstring""" def __init__( self : Tuple ): '''simple docstring''' lowerCamelCase__ : int = {} def lowerCAmelCase ( self : str , __lowerCamelCase : Tuple , __lowerCamelCase : Optional[Any] , __lowerCamelCase : Any=1 ): '''simple docstring''' if self.graph.get(__lowerCAmelCase ): if self.graph[u].count([w, v] ) == 0: self.graph[u].append([w, v] ) else: lowerCamelCase__ : Optional[int] = [[w, v]] if not self.graph.get(__lowerCAmelCase ): lowerCamelCase__ : List[Any] = [] def lowerCAmelCase ( self : Optional[Any] ): '''simple docstring''' return list(self.graph ) def lowerCAmelCase ( self : Dict , __lowerCamelCase : Union[str, Any] , __lowerCamelCase : Optional[Any] ): '''simple docstring''' if self.graph.get(__lowerCAmelCase ): for _ in self.graph[u]: if _[1] == v: self.graph[u].remove(__lowerCAmelCase ) def lowerCAmelCase ( self : List[Any] , __lowerCamelCase : int=-2 , __lowerCamelCase : Tuple=-1 ): '''simple docstring''' if s == d: return [] lowerCamelCase__ : str = [] lowerCamelCase__ : str = [] if s == -2: lowerCamelCase__ : Union[str, Any] = list(self.graph )[0] stack.append(__lowerCAmelCase ) visited.append(__lowerCAmelCase ) lowerCamelCase__ : List[Any] = s while True: # check if there is any non isolated nodes if len(self.graph[s] ) != 0: lowerCamelCase__ : Optional[Any] = s for node in self.graph[s]: if visited.count(node[1] ) < 1: if node[1] == d: visited.append(__lowerCAmelCase ) return visited else: stack.append(node[1] ) visited.append(node[1] ) lowerCamelCase__ : Dict = node[1] break # check if all the children are visited if s == ss: stack.pop() if len(__lowerCAmelCase ) != 0: lowerCamelCase__ : List[str] = stack[len(__lowerCAmelCase ) - 1] else: lowerCamelCase__ : Optional[Any] = ss # check if se have reached the starting point if len(__lowerCAmelCase ) == 0: return visited def lowerCAmelCase ( self : List[str] , __lowerCamelCase : int=-1 ): '''simple docstring''' if c == -1: lowerCamelCase__ : int = floor(random() * 10000 ) + 10 for i in range(__lowerCAmelCase ): # every vertex has max 100 edges for _ in range(floor(random() * 102 ) + 1 ): lowerCamelCase__ : List[str] = floor(random() * c ) + 1 if n != i: self.add_pair(__lowerCAmelCase , __lowerCAmelCase , 1 ) def lowerCAmelCase ( self : List[Any] , __lowerCamelCase : int=-2 ): '''simple docstring''' lowerCamelCase__ : str = deque() lowerCamelCase__ : Optional[int] = [] if s == -2: lowerCamelCase__ : str = list(self.graph )[0] d.append(__lowerCAmelCase ) visited.append(__lowerCAmelCase ) while d: lowerCamelCase__ : Union[str, Any] = d.popleft() if len(self.graph[s] ) != 0: for node in self.graph[s]: if visited.count(node[1] ) < 1: d.append(node[1] ) visited.append(node[1] ) return visited def lowerCAmelCase ( self : Union[str, Any] , __lowerCamelCase : Any ): '''simple docstring''' lowerCamelCase__ : Union[str, Any] = 0 for x in self.graph: for y in self.graph[x]: if y[1] == u: count += 1 return count def lowerCAmelCase ( self : List[str] , __lowerCamelCase : Tuple ): '''simple docstring''' return len(self.graph[u] ) def lowerCAmelCase ( self : List[Any] , __lowerCamelCase : str=-2 ): '''simple docstring''' lowerCamelCase__ : str = [] lowerCamelCase__ : Dict = [] if s == -2: lowerCamelCase__ : List[str] = list(self.graph )[0] stack.append(__lowerCAmelCase ) visited.append(__lowerCAmelCase ) lowerCamelCase__ : int = s lowerCamelCase__ : Optional[Any] = [] while True: # check if there is any non isolated nodes if len(self.graph[s] ) != 0: lowerCamelCase__ : List[str] = s for node in self.graph[s]: if visited.count(node[1] ) < 1: stack.append(node[1] ) visited.append(node[1] ) lowerCamelCase__ : Optional[int] = node[1] break # check if all the children are visited if s == ss: sorted_nodes.append(stack.pop() ) if len(__lowerCAmelCase ) != 0: lowerCamelCase__ : Dict = stack[len(__lowerCAmelCase ) - 1] else: lowerCamelCase__ : Union[str, Any] = ss # check if se have reached the starting point if len(__lowerCAmelCase ) == 0: return sorted_nodes def lowerCAmelCase ( self : Optional[Any] ): '''simple docstring''' lowerCamelCase__ : List[Any] = [] lowerCamelCase__ : Tuple = [] lowerCamelCase__ : str = list(self.graph )[0] stack.append(__lowerCAmelCase ) visited.append(__lowerCAmelCase ) lowerCamelCase__ : Dict = -2 lowerCamelCase__ : Union[str, Any] = [] lowerCamelCase__ : str = s lowerCamelCase__ : str = False lowerCamelCase__ : Optional[int] = set() while True: # check if there is any non isolated nodes if len(self.graph[s] ) != 0: lowerCamelCase__ : Optional[Any] = s for node in self.graph[s]: if ( visited.count(node[1] ) > 0 and node[1] != parent and indirect_parents.count(node[1] ) > 0 and not on_the_way_back ): lowerCamelCase__ : Dict = len(__lowerCAmelCase ) - 1 while len_stack >= 0: if stack[len_stack] == node[1]: anticipating_nodes.add(node[1] ) break else: anticipating_nodes.add(stack[len_stack] ) len_stack -= 1 if visited.count(node[1] ) < 1: stack.append(node[1] ) visited.append(node[1] ) lowerCamelCase__ : List[str] = node[1] break # check if all the children are visited if s == ss: stack.pop() lowerCamelCase__ : Optional[Any] = True if len(__lowerCAmelCase ) != 0: lowerCamelCase__ : List[str] = stack[len(__lowerCAmelCase ) - 1] else: lowerCamelCase__ : str = False indirect_parents.append(__lowerCAmelCase ) lowerCamelCase__ : Tuple = s lowerCamelCase__ : int = ss # check if se have reached the starting point if len(__lowerCAmelCase ) == 0: return list(__lowerCAmelCase ) def lowerCAmelCase ( self : Optional[int] ): '''simple docstring''' lowerCamelCase__ : Tuple = [] lowerCamelCase__ : List[str] = [] lowerCamelCase__ : int = list(self.graph )[0] stack.append(__lowerCAmelCase ) visited.append(__lowerCAmelCase ) lowerCamelCase__ : int = -2 lowerCamelCase__ : Optional[Any] = [] lowerCamelCase__ : Tuple = s lowerCamelCase__ : Any = False lowerCamelCase__ : Dict = set() while True: # check if there is any non isolated nodes if len(self.graph[s] ) != 0: lowerCamelCase__ : Tuple = s for node in self.graph[s]: if ( visited.count(node[1] ) > 0 and node[1] != parent and indirect_parents.count(node[1] ) > 0 and not on_the_way_back ): lowerCamelCase__ : Optional[Any] = len(__lowerCAmelCase ) - 1 while len_stack_minus_one >= 0: if stack[len_stack_minus_one] == node[1]: anticipating_nodes.add(node[1] ) break else: return True if visited.count(node[1] ) < 1: stack.append(node[1] ) visited.append(node[1] ) lowerCamelCase__ : Optional[Any] = node[1] break # check if all the children are visited if s == ss: stack.pop() lowerCamelCase__ : str = True if len(__lowerCAmelCase ) != 0: lowerCamelCase__ : Dict = stack[len(__lowerCAmelCase ) - 1] else: lowerCamelCase__ : Optional[Any] = False indirect_parents.append(__lowerCAmelCase ) lowerCamelCase__ : Any = s lowerCamelCase__ : Union[str, Any] = ss # check if se have reached the starting point if len(__lowerCAmelCase ) == 0: return False def lowerCAmelCase ( self : int , __lowerCamelCase : int=-2 , __lowerCamelCase : Optional[Any]=-1 ): '''simple docstring''' lowerCamelCase__ : List[str] = time() self.dfs(__lowerCAmelCase , __lowerCAmelCase ) lowerCamelCase__ : Dict = time() return end - begin def lowerCAmelCase ( self : List[Any] , __lowerCamelCase : Tuple=-2 ): '''simple docstring''' lowerCamelCase__ : Optional[Any] = time() self.bfs(__lowerCAmelCase ) lowerCamelCase__ : List[str] = time() return end - begin class _lowercase : """simple docstring""" def __init__( self : Optional[Any] ): '''simple docstring''' lowerCamelCase__ : Optional[Any] = {} def lowerCAmelCase ( self : int , __lowerCamelCase : Optional[int] , __lowerCamelCase : Any , __lowerCamelCase : Dict=1 ): '''simple docstring''' if self.graph.get(__lowerCAmelCase ): # if there already is a edge if self.graph[u].count([w, v] ) == 0: self.graph[u].append([w, v] ) else: # if u does not exist lowerCamelCase__ : Union[str, Any] = [[w, v]] # add the other way if self.graph.get(__lowerCAmelCase ): # if there already is a edge if self.graph[v].count([w, u] ) == 0: self.graph[v].append([w, u] ) else: # if u does not exist lowerCamelCase__ : Any = [[w, u]] def lowerCAmelCase ( self : Optional[Any] , __lowerCamelCase : Dict , __lowerCamelCase : List[str] ): '''simple docstring''' if self.graph.get(__lowerCAmelCase ): for _ in self.graph[u]: if _[1] == v: self.graph[u].remove(__lowerCAmelCase ) # the other way round if self.graph.get(__lowerCAmelCase ): for _ in self.graph[v]: if _[1] == u: self.graph[v].remove(__lowerCAmelCase ) def lowerCAmelCase ( self : Dict , __lowerCamelCase : List[str]=-2 , __lowerCamelCase : Optional[Any]=-1 ): '''simple docstring''' if s == d: return [] lowerCamelCase__ : int = [] lowerCamelCase__ : Optional[Any] = [] if s == -2: lowerCamelCase__ : Any = list(self.graph )[0] stack.append(__lowerCAmelCase ) visited.append(__lowerCAmelCase ) lowerCamelCase__ : int = s while True: # check if there is any non isolated nodes if len(self.graph[s] ) != 0: lowerCamelCase__ : Optional[Any] = s for node in self.graph[s]: if visited.count(node[1] ) < 1: if node[1] == d: visited.append(__lowerCAmelCase ) return visited else: stack.append(node[1] ) visited.append(node[1] ) lowerCamelCase__ : List[Any] = node[1] break # check if all the children are visited if s == ss: stack.pop() if len(__lowerCAmelCase ) != 0: lowerCamelCase__ : List[str] = stack[len(__lowerCAmelCase ) - 1] else: lowerCamelCase__ : Dict = ss # check if se have reached the starting point if len(__lowerCAmelCase ) == 0: return visited def lowerCAmelCase ( self : Optional[Any] , __lowerCamelCase : Any=-1 ): '''simple docstring''' if c == -1: lowerCamelCase__ : Union[str, Any] = floor(random() * 10000 ) + 10 for i in range(__lowerCAmelCase ): # every vertex has max 100 edges for _ in range(floor(random() * 102 ) + 1 ): lowerCamelCase__ : Optional[Any] = floor(random() * c ) + 1 if n != i: self.add_pair(__lowerCAmelCase , __lowerCAmelCase , 1 ) def lowerCAmelCase ( self : Any , __lowerCamelCase : Optional[Any]=-2 ): '''simple docstring''' lowerCamelCase__ : Optional[Any] = deque() lowerCamelCase__ : Optional[Any] = [] if s == -2: lowerCamelCase__ : List[Any] = list(self.graph )[0] d.append(__lowerCAmelCase ) visited.append(__lowerCAmelCase ) while d: lowerCamelCase__ : Tuple = d.popleft() if len(self.graph[s] ) != 0: for node in self.graph[s]: if visited.count(node[1] ) < 1: d.append(node[1] ) visited.append(node[1] ) return visited def lowerCAmelCase ( self : str , __lowerCamelCase : List[str] ): '''simple docstring''' return len(self.graph[u] ) def lowerCAmelCase ( self : int ): '''simple docstring''' lowerCamelCase__ : Optional[int] = [] lowerCamelCase__ : int = [] lowerCamelCase__ : int = list(self.graph )[0] stack.append(__lowerCAmelCase ) visited.append(__lowerCAmelCase ) lowerCamelCase__ : int = -2 lowerCamelCase__ : Optional[Any] = [] lowerCamelCase__ : int = s lowerCamelCase__ : Optional[Any] = False lowerCamelCase__ : Optional[int] = set() while True: # check if there is any non isolated nodes if len(self.graph[s] ) != 0: lowerCamelCase__ : List[str] = s for node in self.graph[s]: if ( visited.count(node[1] ) > 0 and node[1] != parent and indirect_parents.count(node[1] ) > 0 and not on_the_way_back ): lowerCamelCase__ : str = len(__lowerCAmelCase ) - 1 while len_stack >= 0: if stack[len_stack] == node[1]: anticipating_nodes.add(node[1] ) break else: anticipating_nodes.add(stack[len_stack] ) len_stack -= 1 if visited.count(node[1] ) < 1: stack.append(node[1] ) visited.append(node[1] ) lowerCamelCase__ : Union[str, Any] = node[1] break # check if all the children are visited if s == ss: stack.pop() lowerCamelCase__ : Union[str, Any] = True if len(__lowerCAmelCase ) != 0: lowerCamelCase__ : Dict = stack[len(__lowerCAmelCase ) - 1] else: lowerCamelCase__ : Dict = False indirect_parents.append(__lowerCAmelCase ) lowerCamelCase__ : int = s lowerCamelCase__ : str = ss # check if se have reached the starting point if len(__lowerCAmelCase ) == 0: return list(__lowerCAmelCase ) def lowerCAmelCase ( self : Dict ): '''simple docstring''' lowerCamelCase__ : Optional[Any] = [] lowerCamelCase__ : str = [] lowerCamelCase__ : Any = list(self.graph )[0] stack.append(__lowerCAmelCase ) visited.append(__lowerCAmelCase ) lowerCamelCase__ : List[Any] = -2 lowerCamelCase__ : Optional[Any] = [] lowerCamelCase__ : Union[str, Any] = s lowerCamelCase__ : Union[str, Any] = False lowerCamelCase__ : Dict = set() while True: # check if there is any non isolated nodes if len(self.graph[s] ) != 0: lowerCamelCase__ : Union[str, Any] = s for node in self.graph[s]: if ( visited.count(node[1] ) > 0 and node[1] != parent and indirect_parents.count(node[1] ) > 0 and not on_the_way_back ): lowerCamelCase__ : Union[str, Any] = len(__lowerCAmelCase ) - 1 while len_stack_minus_one >= 0: if stack[len_stack_minus_one] == node[1]: anticipating_nodes.add(node[1] ) break else: return True if visited.count(node[1] ) < 1: stack.append(node[1] ) visited.append(node[1] ) lowerCamelCase__ : int = node[1] break # check if all the children are visited if s == ss: stack.pop() lowerCamelCase__ : str = True if len(__lowerCAmelCase ) != 0: lowerCamelCase__ : int = stack[len(__lowerCAmelCase ) - 1] else: lowerCamelCase__ : str = False indirect_parents.append(__lowerCAmelCase ) lowerCamelCase__ : Dict = s lowerCamelCase__ : List[str] = ss # check if se have reached the starting point if len(__lowerCAmelCase ) == 0: return False def lowerCAmelCase ( self : List[Any] ): '''simple docstring''' return list(self.graph ) def lowerCAmelCase ( self : List[str] , __lowerCamelCase : List[str]=-2 , __lowerCamelCase : int=-1 ): '''simple docstring''' lowerCamelCase__ : Union[str, Any] = time() self.dfs(__lowerCAmelCase , __lowerCAmelCase ) lowerCamelCase__ : List[Any] = time() return end - begin def lowerCAmelCase ( self : Optional[int] , __lowerCamelCase : Tuple=-2 ): '''simple docstring''' lowerCamelCase__ : List[Any] = time() self.bfs(__lowerCAmelCase ) lowerCamelCase__ : Union[str, Any] = time() return end - begin
184
from collections import deque class A : '''simple docstring''' def __init__( self : List[str] , __lowerCAmelCase : str , __lowerCAmelCase : int , __lowerCAmelCase : int ) -> None: """simple docstring""" A__ = process_name # process name A__ = arrival_time # arrival time of the process # completion time of finished process or last interrupted time A__ = arrival_time A__ = burst_time # remaining burst time A__ = 0 # total time of the process wait in ready queue A__ = 0 # time from arrival time to completion time class A : '''simple docstring''' def __init__( self : Tuple , __lowerCAmelCase : int , __lowerCAmelCase : list[int] , __lowerCAmelCase : deque[Process] , __lowerCAmelCase : int , ) -> None: """simple docstring""" A__ = number_of_queues # time slice of queues that round robin algorithm applied A__ = time_slices # unfinished process is in this ready_queue A__ = queue # current time A__ = current_time # finished process is in this sequence queue A__ = deque() def a_ ( self : Dict ) -> list[str]: """simple docstring""" A__ = [] for i in range(len(self.finish_queue ) ): sequence.append(self.finish_queue[i].process_name ) return sequence def a_ ( self : Tuple , __lowerCAmelCase : list[Process] ) -> list[int]: """simple docstring""" A__ = [] for i in range(len(__lowerCAmelCase ) ): waiting_times.append(queue[i].waiting_time ) return waiting_times def a_ ( self : Optional[Any] , __lowerCAmelCase : list[Process] ) -> list[int]: """simple docstring""" A__ = [] for i in range(len(__lowerCAmelCase ) ): turnaround_times.append(queue[i].turnaround_time ) return turnaround_times def a_ ( self : Dict , __lowerCAmelCase : list[Process] ) -> list[int]: """simple docstring""" A__ = [] for i in range(len(__lowerCAmelCase ) ): completion_times.append(queue[i].stop_time ) return completion_times def a_ ( self : int , __lowerCAmelCase : deque[Process] ) -> list[int]: """simple docstring""" return [q.burst_time for q in queue] def a_ ( self : Any , __lowerCAmelCase : Process ) -> int: """simple docstring""" process.waiting_time += self.current_time - process.stop_time return process.waiting_time def a_ ( self : Union[str, Any] , __lowerCAmelCase : deque[Process] ) -> deque[Process]: """simple docstring""" A__ = deque() # sequence deque of finished process while len(__lowerCAmelCase ) != 0: A__ = ready_queue.popleft() # current process # if process's arrival time is later than current time, update current time if self.current_time < cp.arrival_time: self.current_time += cp.arrival_time # update waiting time of current process self.update_waiting_time(__lowerCAmelCase ) # update current time self.current_time += cp.burst_time # finish the process and set the process's burst-time 0 A__ = 0 # set the process's turnaround time because it is finished A__ = self.current_time - cp.arrival_time # set the completion time A__ = self.current_time # add the process to queue that has finished queue finished.append(__lowerCAmelCase ) self.finish_queue.extend(__lowerCAmelCase ) # add finished process to finish queue # FCFS will finish all remaining processes return finished def a_ ( self : Optional[Any] , __lowerCAmelCase : deque[Process] , __lowerCAmelCase : int ) -> tuple[deque[Process], deque[Process]]: """simple docstring""" A__ = deque() # sequence deque of terminated process # just for 1 cycle and unfinished processes will go back to queue for _ in range(len(__lowerCAmelCase ) ): A__ = ready_queue.popleft() # current process # if process's arrival time is later than current time, update current time if self.current_time < cp.arrival_time: self.current_time += cp.arrival_time # update waiting time of unfinished processes self.update_waiting_time(__lowerCAmelCase ) # if the burst time of process is bigger than time-slice if cp.burst_time > time_slice: # use CPU for only time-slice self.current_time += time_slice # update remaining burst time cp.burst_time -= time_slice # update end point time A__ = self.current_time # locate the process behind the queue because it is not finished ready_queue.append(__lowerCAmelCase ) else: # use CPU for remaining burst time self.current_time += cp.burst_time # set burst time 0 because the process is finished A__ = 0 # set the finish time A__ = self.current_time # update the process' turnaround time because it is finished A__ = self.current_time - cp.arrival_time # add the process to queue that has finished queue finished.append(__lowerCAmelCase ) self.finish_queue.extend(__lowerCAmelCase ) # add finished process to finish queue # return finished processes queue and remaining processes queue return finished, ready_queue def a_ ( self : List[Any] ) -> deque[Process]: """simple docstring""" for i in range(self.number_of_queues - 1 ): A__ , A__ = self.round_robin( self.ready_queue , self.time_slices[i] ) # the last queue has first_come_first_served algorithm self.first_come_first_served(self.ready_queue ) return self.finish_queue if __name__ == "__main__": import doctest A : Union[str, Any] = Process('''P1''', 0, 5_3) A : Optional[Any] = Process('''P2''', 0, 1_7) A : Optional[int] = Process('''P3''', 0, 6_8) A : int = Process('''P4''', 0, 2_4) A : Any = 3 A : List[Any] = [1_7, 2_5] A : Optional[Any] = deque([Pa, Pa, Pa, Pa]) if len(time_slices) != number_of_queues - 1: raise SystemExit(0) doctest.testmod(extraglobs={'''queue''': deque([Pa, Pa, Pa, Pa])}) A : Optional[Any] = Process('''P1''', 0, 5_3) A : int = Process('''P2''', 0, 1_7) A : Optional[int] = Process('''P3''', 0, 6_8) A : Tuple = Process('''P4''', 0, 2_4) A : Union[str, Any] = 3 A : Optional[Any] = [1_7, 2_5] A : Tuple = deque([Pa, Pa, Pa, Pa]) A : Optional[int] = MLFQ(number_of_queues, time_slices, queue, 0) A : Dict = mlfq.multi_level_feedback_queue() # print total waiting times of processes(P1, P2, P3, P4) print( F'''waiting time:\ \t\t\t{MLFQ.calculate_waiting_time(mlfq, [Pa, Pa, Pa, Pa])}''' ) # print completion times of processes(P1, P2, P3, P4) print( F'''completion time:\ \t\t{MLFQ.calculate_completion_time(mlfq, [Pa, Pa, Pa, Pa])}''' ) # print total turnaround times of processes(P1, P2, P3, P4) print( F'''turnaround time:\ \t\t{MLFQ.calculate_turnaround_time(mlfq, [Pa, Pa, Pa, Pa])}''' ) # print sequence of finished processes print( F'''sequence of finished processes:\ {mlfq.calculate_sequence_of_finish_queue()}''' )
274
0
import os import time import numpy as np import onnxruntime as ort lowercase = '''1''' lowercase = '''0''' lowercase = '''1''' lowercase = ort.SessionOptions() lowercase = ort.GraphOptimizationLevel.ORT_DISABLE_ALL print("Create inference session...") lowercase = ['''TensorrtExecutionProvider''', '''CUDAExecutionProvider'''] lowercase = ort.InferenceSession("model.onnx", sess_options=sess_opt, providers=execution_provider) lowercase = ort.RunOptions() lowercase = 128 lowercase = 1 lowercase = np.ones((batch, sequence), dtype=np.intaa) lowercase = np.ones((batch, sequence), dtype=np.intaa) lowercase = np.ones((batch, sequence), dtype=np.intaa) print("Warm up phase...") sess.run( None, { sess.get_inputs()[0].name: input_ids, sess.get_inputs()[1].name: attention_mask, sess.get_inputs()[2].name: token_type_ids, }, run_options=run_opt, ) print("Start inference...") lowercase = time.time() lowercase = 2000 lowercase = {} for iter in range(max_iters): lowercase = sess.run( None, { sess.get_inputs()[0].name: input_ids, sess.get_inputs()[1].name: attention_mask, sess.get_inputs()[2].name: token_type_ids, }, run_options=run_opt, ) print("Average Inference Time = {:.3f} ms".format((time.time() - start_time) * 1000 / max_iters))
178
from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...onnx.utils import compute_effective_axis_dimension from ...utils import logging if TYPE_CHECKING: from ...processing_utils import ProcessorMixin from ...utils import TensorType A : str = logging.get_logger(__name__) A : Union[str, Any] = { '''microsoft/layoutlmv3-base''': '''https://huggingface.co/microsoft/layoutlmv3-base/resolve/main/config.json''', } class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : Optional[Any] = '''layoutlmv3''' def __init__( self : Tuple , __lowerCAmelCase : Optional[int]=5_02_65 , __lowerCAmelCase : Tuple=7_68 , __lowerCAmelCase : Union[str, Any]=12 , __lowerCAmelCase : Any=12 , __lowerCAmelCase : List[Any]=30_72 , __lowerCAmelCase : Optional[int]="gelu" , __lowerCAmelCase : Dict=0.1 , __lowerCAmelCase : List[Any]=0.1 , __lowerCAmelCase : Dict=5_12 , __lowerCAmelCase : Any=2 , __lowerCAmelCase : Dict=0.0_2 , __lowerCAmelCase : List[str]=1e-5 , __lowerCAmelCase : List[str]=1 , __lowerCAmelCase : int=0 , __lowerCAmelCase : Dict=2 , __lowerCAmelCase : Tuple=10_24 , __lowerCAmelCase : List[str]=1_28 , __lowerCAmelCase : Optional[int]=1_28 , __lowerCAmelCase : Any=True , __lowerCAmelCase : Optional[Any]=32 , __lowerCAmelCase : Any=1_28 , __lowerCAmelCase : str=64 , __lowerCAmelCase : Optional[int]=2_56 , __lowerCAmelCase : int=True , __lowerCAmelCase : int=True , __lowerCAmelCase : List[str]=True , __lowerCAmelCase : int=2_24 , __lowerCAmelCase : Dict=3 , __lowerCAmelCase : List[Any]=16 , __lowerCAmelCase : Dict=None , **__lowerCAmelCase : Optional[Any] , ) -> Dict: """simple docstring""" super().__init__( vocab_size=__lowerCAmelCase , hidden_size=__lowerCAmelCase , num_hidden_layers=__lowerCAmelCase , num_attention_heads=__lowerCAmelCase , intermediate_size=__lowerCAmelCase , hidden_act=__lowerCAmelCase , hidden_dropout_prob=__lowerCAmelCase , attention_probs_dropout_prob=__lowerCAmelCase , max_position_embeddings=__lowerCAmelCase , type_vocab_size=__lowerCAmelCase , initializer_range=__lowerCAmelCase , layer_norm_eps=__lowerCAmelCase , pad_token_id=__lowerCAmelCase , bos_token_id=__lowerCAmelCase , eos_token_id=__lowerCAmelCase , **__lowerCAmelCase , ) A__ = max_ad_position_embeddings A__ = coordinate_size A__ = shape_size A__ = has_relative_attention_bias A__ = rel_pos_bins A__ = max_rel_pos A__ = has_spatial_attention_bias A__ = rel_ad_pos_bins A__ = max_rel_ad_pos A__ = text_embed A__ = visual_embed A__ = input_size A__ = num_channels A__ = patch_size A__ = classifier_dropout class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : List[str] = version.parse('''1.12''' ) @property def a_ ( self : int ) -> Mapping[str, Mapping[int, str]]: """simple docstring""" if self.task in ["question-answering", "sequence-classification"]: return OrderedDict( [ ("""input_ids""", {0: """batch""", 1: """sequence"""}), ("""attention_mask""", {0: """batch""", 1: """sequence"""}), ("""bbox""", {0: """batch""", 1: """sequence"""}), ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}), ] ) else: return OrderedDict( [ ("""input_ids""", {0: """batch""", 1: """sequence"""}), ("""bbox""", {0: """batch""", 1: """sequence"""}), ("""attention_mask""", {0: """batch""", 1: """sequence"""}), ("""pixel_values""", {0: """batch""", 1: """num_channels"""}), ] ) @property def a_ ( self : Optional[int] ) -> float: """simple docstring""" return 1e-5 @property def a_ ( self : Tuple ) -> int: """simple docstring""" return 12 def a_ ( self : str , __lowerCAmelCase : "ProcessorMixin" , __lowerCAmelCase : int = -1 , __lowerCAmelCase : int = -1 , __lowerCAmelCase : bool = False , __lowerCAmelCase : Optional["TensorType"] = None , __lowerCAmelCase : int = 3 , __lowerCAmelCase : int = 40 , __lowerCAmelCase : int = 40 , ) -> Mapping[str, Any]: """simple docstring""" setattr(processor.image_processor , """apply_ocr""" , __lowerCAmelCase ) # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX A__ = compute_effective_axis_dimension( __lowerCAmelCase , fixed_dimension=OnnxConfig.default_fixed_batch , num_token_to_add=0 ) # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX A__ = processor.tokenizer.num_special_tokens_to_add(__lowerCAmelCase ) A__ = compute_effective_axis_dimension( __lowerCAmelCase , fixed_dimension=OnnxConfig.default_fixed_sequence , num_token_to_add=__lowerCAmelCase ) # Generate dummy inputs according to compute batch and sequence A__ = [[""" """.join([processor.tokenizer.unk_token] ) * seq_length]] * batch_size # Generate dummy bounding boxes A__ = [[[48, 84, 73, 1_28]]] * batch_size # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX # batch_size = compute_effective_axis_dimension(batch_size, fixed_dimension=OnnxConfig.default_fixed_batch) A__ = self._generate_dummy_images(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) A__ = dict( processor( __lowerCAmelCase , text=__lowerCAmelCase , boxes=__lowerCAmelCase , return_tensors=__lowerCAmelCase , ) ) return inputs
274
0
import os import time from dataclasses import dataclass, field from enum import Enum from typing import Dict, List, Optional, Union import torch from filelock import FileLock from torch.utils.data import Dataset from ...models.auto.modeling_auto import MODEL_FOR_QUESTION_ANSWERING_MAPPING from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging from ..processors.squad import SquadFeatures, SquadVaProcessor, SquadVaProcessor, squad_convert_examples_to_features _A = logging.get_logger(__name__) _A = list(MODEL_FOR_QUESTION_ANSWERING_MAPPING.keys()) _A = tuple(conf.model_type for conf in MODEL_CONFIG_CLASSES) @dataclass class UpperCAmelCase__ : """simple docstring""" UpperCAmelCase__ : str = field( default=A_ , metadata={"help": "Model type selected in the list: " + ", ".join(A_ )} ) UpperCAmelCase__ : str = field( default=A_ , metadata={"help": "The input data dir. Should contain the .json files for the SQuAD task."} ) UpperCAmelCase__ : int = field( default=1_2_8 , metadata={ "help": ( "The maximum total input sequence length after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) } , ) UpperCAmelCase__ : int = field( default=1_2_8 , metadata={"help": "When splitting up a long document into chunks, how much stride to take between chunks."} , ) UpperCAmelCase__ : int = field( default=6_4 , metadata={ "help": ( "The maximum number of tokens for the question. Questions longer than this will " "be truncated to this length." ) } , ) UpperCAmelCase__ : int = field( default=3_0 , metadata={ "help": ( "The maximum length of an answer that can be generated. This is needed because the start " "and end predictions are not conditioned on one another." ) } , ) UpperCAmelCase__ : bool = field( default=A_ , metadata={"help": "Overwrite the cached training and evaluation sets"} ) UpperCAmelCase__ : bool = field( default=A_ , metadata={"help": "If true, the SQuAD examples contain some that do not have an answer."} ) UpperCAmelCase__ : float = field( default=0.0 , metadata={"help": "If null_score - best_non_null is greater than the threshold predict null."} ) UpperCAmelCase__ : int = field( default=2_0 , metadata={"help": "If null_score - best_non_null is greater than the threshold predict null."} ) UpperCAmelCase__ : int = field( default=0 , metadata={ "help": ( "language id of input for language-specific xlm models (see" " tokenization_xlm.PRETRAINED_INIT_CONFIGURATION)" ) } , ) UpperCAmelCase__ : int = field(default=1 , metadata={"help": "multiple threads for converting example to features"} ) class UpperCAmelCase__ ( A_ ): """simple docstring""" UpperCAmelCase__ : List[str] = '''train''' UpperCAmelCase__ : List[Any] = '''dev''' class UpperCAmelCase__ ( A_ ): """simple docstring""" UpperCAmelCase__ : SquadDataTrainingArguments UpperCAmelCase__ : List[SquadFeatures] UpperCAmelCase__ : Split UpperCAmelCase__ : bool def __init__( self , A_ , A_ , A_ = None , A_ = Split.train , A_ = False , A_ = None , A_ = "pt" , ) -> Union[str, Any]: __UpperCamelCase =args __UpperCamelCase =is_language_sensitive __UpperCamelCase =SquadVaProcessor() if args.version_2_with_negative else SquadVaProcessor() if isinstance(__lowerCAmelCase , __lowerCAmelCase ): try: __UpperCamelCase =Split[mode] except KeyError: raise KeyError('mode is not a valid split name' ) __UpperCamelCase =mode # Load data features from cache or dataset file __UpperCamelCase ='v2' if args.version_2_with_negative else 'v1' __UpperCamelCase =os.path.join( cache_dir if cache_dir is not None else args.data_dir , f'cached_{mode.value}_{tokenizer.__class__.__name__}_{args.max_seq_length}_{version_tag}' , ) # Make sure only the first process in distributed training processes the dataset, # and the others will use the cache. __UpperCamelCase =cached_features_file + '.lock' with FileLock(__lowerCAmelCase ): if os.path.exists(__lowerCAmelCase ) and not args.overwrite_cache: __UpperCamelCase =time.time() __UpperCamelCase =torch.load(__lowerCAmelCase ) # Legacy cache files have only features, while new cache files # will have dataset and examples also. __UpperCamelCase =self.old_features['features'] __UpperCamelCase =self.old_features.get('dataset' , __lowerCAmelCase ) __UpperCamelCase =self.old_features.get('examples' , __lowerCAmelCase ) logger.info( f'Loading features from cached file {cached_features_file} [took %.3f s]' , time.time() - start ) if self.dataset is None or self.examples is None: logger.warning( f'Deleting cached file {cached_features_file} will allow dataset and examples to be cached in' ' future run' ) else: if mode == Split.dev: __UpperCamelCase =self.processor.get_dev_examples(args.data_dir ) else: __UpperCamelCase =self.processor.get_train_examples(args.data_dir ) __UpperCamelCase , __UpperCamelCase =squad_convert_examples_to_features( examples=self.examples , tokenizer=__lowerCAmelCase , max_seq_length=args.max_seq_length , doc_stride=args.doc_stride , max_query_length=args.max_query_length , is_training=mode == Split.train , threads=args.threads , return_dataset=__lowerCAmelCase , ) __UpperCamelCase =time.time() torch.save( {'features': self.features, 'dataset': self.dataset, 'examples': self.examples} , __lowerCAmelCase , ) # ^ This seems to take a lot of time so I want to investigate why and how we can improve. logger.info( f'Saving features into cached file {cached_features_file} [took {time.time() - start:.3f} s]' ) def __len__( self ) -> Any: return len(self.features ) def __getitem__( self , A_ ) -> Dict[str, torch.Tensor]: __UpperCamelCase =self.features[i] __UpperCamelCase =torch.tensor(feature.input_ids , dtype=torch.long ) __UpperCamelCase =torch.tensor(feature.attention_mask , dtype=torch.long ) __UpperCamelCase =torch.tensor(feature.token_type_ids , dtype=torch.long ) __UpperCamelCase =torch.tensor(feature.cls_index , dtype=torch.long ) __UpperCamelCase =torch.tensor(feature.p_mask , dtype=torch.float ) __UpperCamelCase =torch.tensor(feature.is_impossible , dtype=torch.float ) __UpperCamelCase ={ 'input_ids': input_ids, 'attention_mask': attention_mask, 'token_type_ids': token_type_ids, } if self.args.model_type in ["xlm", "roberta", "distilbert", "camembert"]: del inputs["token_type_ids"] if self.args.model_type in ["xlnet", "xlm"]: inputs.update({'cls_index': cls_index, 'p_mask': p_mask} ) if self.args.version_2_with_negative: inputs.update({'is_impossible': is_impossible} ) if self.is_language_sensitive: inputs.update({'langs': (torch.ones(input_ids.shape , dtype=torch.intaa ) * self.args.lang_id)} ) if self.mode == Split.train: __UpperCamelCase =torch.tensor(feature.start_position , dtype=torch.long ) __UpperCamelCase =torch.tensor(feature.end_position , dtype=torch.long ) inputs.update({'start_positions': start_positions, 'end_positions': end_positions} ) return inputs
62
import json import os from pathlib import Path from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple, Union import sentencepiece from ...tokenization_utils import BatchEncoding, PreTrainedTokenizer from ...utils import logging A : Optional[Any] = logging.get_logger(__name__) A : str = '''▁''' A : Any = { '''vocab_file''': '''vocab.json''', '''spm_file''': '''sentencepiece.bpe.model''', '''tokenizer_config_file''': '''tokenizer_config.json''', } A : List[Any] = { '''vocab_file''': { '''facebook/m2m100_418M''': '''https://huggingface.co/facebook/m2m100_418M/resolve/main/vocab.json''', '''facebook/m2m100_1.2B''': '''https://huggingface.co/facebook/m2m100_1.2B/resolve/main/vocab.json''', }, '''spm_file''': { '''facebook/m2m100_418M''': '''https://huggingface.co/facebook/m2m100_418M/resolve/main/sentencepiece.bpe.model''', '''facebook/m2m100_1.2B''': '''https://huggingface.co/facebook/m2m100_1.2B/resolve/main/sentencepiece.bpe.model''', }, '''tokenizer_config_file''': { '''facebook/m2m100_418M''': '''https://huggingface.co/facebook/m2m100_418M/resolve/main/tokenizer_config.json''', '''facebook/m2m100_1.2B''': '''https://huggingface.co/facebook/m2m100_1.2B/resolve/main/tokenizer_config.json''', }, } A : Tuple = { '''facebook/m2m100_418M''': 1_0_2_4, } # fmt: off A : Optional[int] = { '''m2m100''': ['''af''', '''am''', '''ar''', '''ast''', '''az''', '''ba''', '''be''', '''bg''', '''bn''', '''br''', '''bs''', '''ca''', '''ceb''', '''cs''', '''cy''', '''da''', '''de''', '''el''', '''en''', '''es''', '''et''', '''fa''', '''ff''', '''fi''', '''fr''', '''fy''', '''ga''', '''gd''', '''gl''', '''gu''', '''ha''', '''he''', '''hi''', '''hr''', '''ht''', '''hu''', '''hy''', '''id''', '''ig''', '''ilo''', '''is''', '''it''', '''ja''', '''jv''', '''ka''', '''kk''', '''km''', '''kn''', '''ko''', '''lb''', '''lg''', '''ln''', '''lo''', '''lt''', '''lv''', '''mg''', '''mk''', '''ml''', '''mn''', '''mr''', '''ms''', '''my''', '''ne''', '''nl''', '''no''', '''ns''', '''oc''', '''or''', '''pa''', '''pl''', '''ps''', '''pt''', '''ro''', '''ru''', '''sd''', '''si''', '''sk''', '''sl''', '''so''', '''sq''', '''sr''', '''ss''', '''su''', '''sv''', '''sw''', '''ta''', '''th''', '''tl''', '''tn''', '''tr''', '''uk''', '''ur''', '''uz''', '''vi''', '''wo''', '''xh''', '''yi''', '''yo''', '''zh''', '''zu'''], '''wmt21''': ['''en''', '''ha''', '''is''', '''ja''', '''cs''', '''ru''', '''zh''', '''de'''] } class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : Union[str, Any] = VOCAB_FILES_NAMES __lowerCamelCase : List[str] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase : Optional[int] = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase : Dict = ['''input_ids''', '''attention_mask'''] __lowerCamelCase : List[int] = [] __lowerCamelCase : List[int] = [] def __init__( self : List[Any] , __lowerCAmelCase : List[Any] , __lowerCAmelCase : Dict , __lowerCAmelCase : Union[str, Any]=None , __lowerCAmelCase : str=None , __lowerCAmelCase : List[Any]="<s>" , __lowerCAmelCase : List[Any]="</s>" , __lowerCAmelCase : Optional[int]="</s>" , __lowerCAmelCase : Optional[Any]="<pad>" , __lowerCAmelCase : Any="<unk>" , __lowerCAmelCase : Any="m2m100" , __lowerCAmelCase : Optional[Dict[str, Any]] = None , __lowerCAmelCase : Dict=8 , **__lowerCAmelCase : Tuple , ) -> None: """simple docstring""" A__ = {} if sp_model_kwargs is None else sp_model_kwargs A__ = language_codes A__ = FAIRSEQ_LANGUAGE_CODES[language_codes] A__ = {lang_code: f'__{lang_code}__' for lang_code in fairseq_language_code} A__ = kwargs.get("""additional_special_tokens""" , [] ) kwargs["additional_special_tokens"] += [ self.get_lang_token(__lowerCAmelCase ) for lang_code in fairseq_language_code if self.get_lang_token(__lowerCAmelCase ) not in kwargs["additional_special_tokens"] ] super().__init__( src_lang=__lowerCAmelCase , tgt_lang=__lowerCAmelCase , bos_token=__lowerCAmelCase , eos_token=__lowerCAmelCase , sep_token=__lowerCAmelCase , unk_token=__lowerCAmelCase , pad_token=__lowerCAmelCase , language_codes=__lowerCAmelCase , sp_model_kwargs=self.sp_model_kwargs , num_madeup_words=__lowerCAmelCase , **__lowerCAmelCase , ) A__ = vocab_file A__ = load_json(__lowerCAmelCase ) A__ = {v: k for k, v in self.encoder.items()} A__ = spm_file A__ = load_spm(__lowerCAmelCase , self.sp_model_kwargs ) A__ = len(self.encoder ) A__ = { self.get_lang_token(__lowerCAmelCase ): self.encoder_size + i for i, lang_code in enumerate(__lowerCAmelCase ) } A__ = {lang_code: self.encoder_size + i for i, lang_code in enumerate(__lowerCAmelCase )} A__ = {v: k for k, v in self.lang_token_to_id.items()} A__ = src_lang if src_lang is not None else """en""" A__ = tgt_lang A__ = self.get_lang_id(self._src_lang ) self.set_src_lang_special_tokens(self._src_lang ) A__ = num_madeup_words @property def a_ ( self : Optional[int] ) -> int: """simple docstring""" return len(self.encoder ) + len(self.lang_token_to_id ) @property def a_ ( self : Optional[Any] ) -> str: """simple docstring""" return self._src_lang @src_lang.setter def a_ ( self : List[Any] , __lowerCAmelCase : str ) -> None: """simple docstring""" A__ = new_src_lang self.set_src_lang_special_tokens(self._src_lang ) def a_ ( self : Optional[int] , __lowerCAmelCase : str ) -> List[str]: """simple docstring""" return self.sp_model.encode(__lowerCAmelCase , out_type=__lowerCAmelCase ) def a_ ( self : Optional[Any] , __lowerCAmelCase : Dict ) -> Optional[Any]: """simple docstring""" if token in self.lang_token_to_id: return self.lang_token_to_id[token] return self.encoder.get(__lowerCAmelCase , self.encoder[self.unk_token] ) def a_ ( self : Optional[int] , __lowerCAmelCase : int ) -> str: """simple docstring""" if index in self.id_to_lang_token: return self.id_to_lang_token[index] return self.decoder.get(__lowerCAmelCase , self.unk_token ) def a_ ( self : Optional[int] , __lowerCAmelCase : Dict ) -> str: """simple docstring""" A__ = [] A__ = """""" 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(__lowerCAmelCase ) + token A__ = [] else: current_sub_tokens.append(__lowerCAmelCase ) out_string += self.sp_model.decode(__lowerCAmelCase ) return out_string.strip() def a_ ( self : List[str] , __lowerCAmelCase : List[int] , __lowerCAmelCase : Optional[List[int]] = None , __lowerCAmelCase : bool = False ) -> List[int]: """simple docstring""" if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=__lowerCAmelCase , token_ids_a=__lowerCAmelCase , already_has_special_tokens=__lowerCAmelCase ) A__ = [1] * len(self.prefix_tokens ) A__ = [1] * len(self.suffix_tokens ) if token_ids_a is None: return prefix_ones + ([0] * len(__lowerCAmelCase )) + suffix_ones return prefix_ones + ([0] * len(__lowerCAmelCase )) + ([0] * len(__lowerCAmelCase )) + suffix_ones def a_ ( self : Tuple , __lowerCAmelCase : List[int] , __lowerCAmelCase : Optional[List[int]] = None ) -> List[int]: """simple docstring""" if token_ids_a is None: return self.prefix_tokens + token_ids_a + self.suffix_tokens # We don't expect to process pairs, but leave the pair logic for API consistency return self.prefix_tokens + token_ids_a + token_ids_a + self.suffix_tokens def a_ ( self : int ) -> Dict: """simple docstring""" A__ = {self.convert_ids_to_tokens(__lowerCAmelCase ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self : Union[str, Any] ) -> Dict: """simple docstring""" A__ = self.__dict__.copy() A__ = None return state def __setstate__( self : str , __lowerCAmelCase : Dict ) -> None: """simple docstring""" A__ = d # for backward compatibility if not hasattr(self , """sp_model_kwargs""" ): A__ = {} A__ = load_spm(self.spm_file , self.sp_model_kwargs ) def a_ ( self : List[str] , __lowerCAmelCase : str , __lowerCAmelCase : Optional[str] = None ) -> Tuple[str]: """simple docstring""" A__ = Path(__lowerCAmelCase ) if not save_dir.is_dir(): raise OSError(f'{save_directory} should be a directory' ) A__ = save_dir / ( (filename_prefix + """-""" if filename_prefix else """""") + self.vocab_files_names["""vocab_file"""] ) A__ = save_dir / ( (filename_prefix + """-""" if filename_prefix else """""") + self.vocab_files_names["""spm_file"""] ) save_json(self.encoder , __lowerCAmelCase ) if os.path.abspath(self.spm_file ) != os.path.abspath(__lowerCAmelCase ) and os.path.isfile(self.spm_file ): copyfile(self.spm_file , __lowerCAmelCase ) elif not os.path.isfile(self.spm_file ): with open(__lowerCAmelCase , """wb""" ) as fi: A__ = self.sp_model.serialized_model_proto() fi.write(__lowerCAmelCase ) return (str(__lowerCAmelCase ), str(__lowerCAmelCase )) def a_ ( self : str , __lowerCAmelCase : List[str] , __lowerCAmelCase : str = "en" , __lowerCAmelCase : Optional[List[str]] = None , __lowerCAmelCase : str = "ro" , **__lowerCAmelCase : List[Any] , ) -> BatchEncoding: """simple docstring""" A__ = src_lang A__ = tgt_lang self.set_src_lang_special_tokens(self.src_lang ) return super().prepare_seqaseq_batch(__lowerCAmelCase , __lowerCAmelCase , **__lowerCAmelCase ) def a_ ( self : Optional[int] , __lowerCAmelCase : Any , __lowerCAmelCase : Optional[str] , __lowerCAmelCase : Optional[str] , **__lowerCAmelCase : Tuple ) -> Tuple: """simple docstring""" if src_lang is None or tgt_lang is None: raise ValueError("""Translation requires a `src_lang` and a `tgt_lang` for this model""" ) A__ = src_lang A__ = self(__lowerCAmelCase , add_special_tokens=__lowerCAmelCase , **__lowerCAmelCase ) A__ = self.get_lang_id(__lowerCAmelCase ) A__ = tgt_lang_id return inputs def a_ ( self : Dict ) -> int: """simple docstring""" self.set_src_lang_special_tokens(self.src_lang ) def a_ ( self : Union[str, Any] ) -> Union[str, Any]: """simple docstring""" self.set_tgt_lang_special_tokens(self.tgt_lang ) def a_ ( self : str , __lowerCAmelCase : str ) -> None: """simple docstring""" A__ = self.get_lang_token(__lowerCAmelCase ) A__ = self.lang_token_to_id[lang_token] A__ = [self.cur_lang_id] A__ = [self.eos_token_id] def a_ ( self : Tuple , __lowerCAmelCase : str ) -> None: """simple docstring""" A__ = self.get_lang_token(__lowerCAmelCase ) A__ = self.lang_token_to_id[lang_token] A__ = [self.cur_lang_id] A__ = [self.eos_token_id] def a_ ( self : Union[str, Any] , __lowerCAmelCase : str ) -> str: """simple docstring""" return self.lang_code_to_token[lang] def a_ ( self : Union[str, Any] , __lowerCAmelCase : str ) -> int: """simple docstring""" A__ = self.get_lang_token(__lowerCAmelCase ) return self.lang_token_to_id[lang_token] def __lowerCamelCase ( __a :str , __a :Dict[str, Any] ) -> sentencepiece.SentencePieceProcessor: """simple docstring""" A__ = sentencepiece.SentencePieceProcessor(**__a ) spm.Load(str(__a ) ) return spm def __lowerCamelCase ( __a :str ) -> Union[Dict, List]: """simple docstring""" with open(__a , """r""" ) as f: return json.load(__a ) def __lowerCamelCase ( __a :List[Any] , __a :str ) -> None: """simple docstring""" with open(__a , """w""" ) as f: json.dump(__a , __a , indent=2 )
274
0
'''simple docstring''' from random import randint, random def __lowerCAmelCase ( snake_case__ , snake_case__ , snake_case__ , snake_case__ = False , snake_case__ = False , snake_case__ = 5 , ): __UpperCamelCase : Optional[int] = [[-1] * number_of_cells] # Create a highway without any car __UpperCamelCase : Optional[Any] = 0 __UpperCamelCase : int = max(__a , 0 ) while i < number_of_cells: __UpperCamelCase : Optional[int] = ( randint(0 , __a ) if random_speed else initial_speed ) # Place the cars i += ( randint(1 , max_speed * 2 ) if random_frequency else frequency ) # Arbitrary number, may need tuning return highway def __lowerCAmelCase ( snake_case__ , snake_case__ ): __UpperCamelCase : int = 0 __UpperCamelCase : Tuple = highway_now[car_index + 1 :] for cell in range(len(__a ) ): # May need a better name for this if cells[cell] != -1: # If the cell is not empty then return distance # we have the distance we wanted distance += 1 # Here if the car is near the end of the highway return distance + get_distance(__a , -1 ) def __lowerCAmelCase ( snake_case__ , snake_case__ , snake_case__ ): __UpperCamelCase : Tuple = len(__a ) # Beforce calculations, the highway is empty __UpperCamelCase : List[Any] = [-1] * number_of_cells for car_index in range(__a ): if highway_now[car_index] != -1: # Add 1 to the current speed of the car and cap the speed __UpperCamelCase : Optional[int] = min(highway_now[car_index] + 1 , __a ) # Number of empty cell before the next car __UpperCamelCase : Tuple = get_distance(__a , __a ) - 1 # We can't have the car causing an accident __UpperCamelCase : List[str] = min(next_highway[car_index] , __a ) if random() < probability: # Randomly, a driver will slow down __UpperCamelCase : List[str] = max(next_highway[car_index] - 1 , 0 ) return next_highway def __lowerCAmelCase ( snake_case__ , snake_case__ , snake_case__ , snake_case__ ): __UpperCamelCase : Union[str, Any] = len(highway[0] ) for i in range(__a ): __UpperCamelCase : Optional[int] = update(highway[i] , __a , __a ) __UpperCamelCase : List[str] = [-1] * number_of_cells for car_index in range(__a ): __UpperCamelCase : Optional[Any] = next_speeds_calculated[car_index] if speed != -1: # Change the position based on the speed (with % to create the loop) __UpperCamelCase : int = (car_index + speed) % number_of_cells # Commit the change of position __UpperCamelCase : List[str] = speed highway.append(__a ) return highway if __name__ == "__main__": import doctest doctest.testmod()
298
from __future__ import annotations from PIL import Image # Define glider example A : Any = [ [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [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], ] # Define blinker example A : Optional[Any] = [[0, 1, 0], [0, 1, 0], [0, 1, 0]] def __lowerCamelCase ( __a :list[list[int]] ) -> list[list[int]]: """simple docstring""" A__ = [] for i in range(len(__a ) ): A__ = [] for j in range(len(cells[i] ) ): # Get the number of live neighbours A__ = 0 if i > 0 and j > 0: neighbour_count += cells[i - 1][j - 1] if i > 0: neighbour_count += cells[i - 1][j] if i > 0 and j < len(cells[i] ) - 1: neighbour_count += cells[i - 1][j + 1] if j > 0: neighbour_count += cells[i][j - 1] if j < len(cells[i] ) - 1: neighbour_count += cells[i][j + 1] if i < len(__a ) - 1 and j > 0: neighbour_count += cells[i + 1][j - 1] if i < len(__a ) - 1: neighbour_count += cells[i + 1][j] if i < len(__a ) - 1 and j < len(cells[i] ) - 1: neighbour_count += cells[i + 1][j + 1] # Rules of the game of life (excerpt from Wikipedia): # 1. Any live cell with two or three live neighbours survives. # 2. Any dead cell with three live neighbours becomes a live cell. # 3. All other live cells die in the next generation. # Similarly, all other dead cells stay dead. A__ = cells[i][j] == 1 if ( (alive and 2 <= neighbour_count <= 3) or not alive and neighbour_count == 3 ): next_generation_row.append(1 ) else: next_generation_row.append(0 ) next_generation.append(__a ) return next_generation def __lowerCamelCase ( __a :list[list[int]] , __a :int ) -> list[Image.Image]: """simple docstring""" A__ = [] for _ in range(__a ): # Create output image A__ = Image.new("""RGB""" , (len(cells[0] ), len(__a )) ) A__ = img.load() # Save cells to image for x in range(len(__a ) ): for y in range(len(cells[0] ) ): A__ = 2_5_5 - cells[y][x] * 2_5_5 A__ = (colour, colour, colour) # Save image images.append(__a ) A__ = new_generation(__a ) return images if __name__ == "__main__": A : str = generate_images(GLIDER, 1_6) images[0].save('''out.gif''', save_all=True, append_images=images[1:])
274
0
"""simple docstring""" import json import os import unittest from typing import Tuple from transformers import WavaVecaPhonemeCTCTokenizer from transformers.models.wavaveca.tokenization_wavaveca import VOCAB_FILES_NAMES from transformers.models.wavaveca_phoneme.tokenization_wavaveca_phoneme import WavaVecaPhonemeCTCTokenizerOutput from transformers.testing_utils import require_phonemizer from ...test_tokenization_common import TokenizerTesterMixin @require_phonemizer class UpperCamelCase ( snake_case_ , unittest.TestCase ): UpperCamelCase : str = WavaVecaPhonemeCTCTokenizer UpperCamelCase : Optional[Any] = False def _lowercase ( self : Tuple ) -> Any: super().setUp() _a : Any = ( """<s> <pad> </s> <unk> n s t ə l a i k d m ɛ ɾ e ɪ p o ɐ z ð f j v b ɹ ʁ ʊ iː r w ʌ u ɡ æ aɪ ʃ h ɔ ɑː """ """ŋ ɚ eɪ β uː y ɑ̃ oʊ ᵻ eː θ aʊ ts oː ɔ̃ ɣ ɜ ɑ dʒ əl x ɜː ç ʒ tʃ ɔː ɑːɹ ɛ̃ ʎ ɔːɹ ʋ aː ɕ œ ø oːɹ ɲ yː """ """ʔ iə i5 s. tɕ ?? nʲ ɛː œ̃ ɭ ɔø ʑ tʲ ɨ ɛɹ ts. rʲ ɪɹ ɭʲ i.5 ɔɪ q sʲ u5 ʊɹ iɜ a5 iɛ5 øː ʕ ja əɜ th ɑ5 """ """oɪ dʲ ə5 tɕh ts.h mʲ ɯ dʑ vʲ e̞ tʃʲ ei5 o5 onɡ5 ɑu5 iɑ5 ai5 aɪɚ kh ə1 ʐ i2 ʉ ħ t[ aɪə ʲ ju ə2 u2 oɜ """ """pː iɛɜ ou5 y5 uɜ tː uo5 d[ uoɜ tsh ɑɜ ɵ i̪5 uei5 ɟ aɜ ɑɨ i.ɜ eʊ o2 ɐ̃ ä pʲ kʲ n̩ ɒ ph ɑu2 uɨ əɪ ɫ ɬ """ """yɜ bʲ ɑ2 s̪ aiɜ χ ɐ̃ʊ̃ 1 ə4 yæɜ a2 ɨː t̪ iouɜ ũ onɡɜ aɨ iɛ2 ɔɨ ɑuɜ o̞ ei2 iou2 c kː y2 ɖ oe dˤ yɛɜ """ """əʊ S ɡʲ onɡ2 u\" eiɜ ʈ ɯᵝ iou5 dZ r̝̊ i.2 tS s^ ʝ yə5 iɑɜ uə5 pf ɨu iɑ2 ou2 ər2 fʲ ai2 r̝ uəɜ ɳ əɨ """ """ua5 uɪ ɽ bː yu5 uo2 yɛ5 l̩ ɻ ərɜ ʂ i̪2 ouɜ uaɜ a. a.ː yæ5 dː r̩ ee ɪu ər5 i̪ ɜ æi u: i.ː t^ o1 ɪ^ """ """ai ueiɜ æː ɛɪ eə i. ɴ ie ua2 ɑ1 o4 tʃː o: ɑ: u1 N i̪1 au yæ2 u. qː yəɜ y: kʰ tʃʰ iʊ sx õ uo tʰ """ """uai5 bʰ u.ː uə2 ʊə d^ s̪ː yiɜ dʰ r. oe: i1 ɟː yu2 nʲʲ i̪4 uei2 tsʲ ɸ ĩ ɑ4 t̪ː eɑ u4 e: tsː ʈʰ ɡʰ """ """ɯɯ dʒʲ ʂʲ X ɵː uaiɜ tɕʲ ã t^ː ẽː yɛ2 cː i.1 ɛʊ dˤdˤ dʒː i4 ɡː yi ɕʲ ɟʰ pʰ dʑʲ yuɜ ua1 ua4 æiː ɐɐ """ """ui iou1 ʊː a1 iou4 cʰ iɛ1 yə2 ɖʰ ẽ ʒʲ ää ər4 iːː ɪː iɑ1 ər1 œː øi ɪuː cʰcʰ əː1 iː1 ũ kʰː o̞o̞ xʲ """ """ou1 iɛ4 e̞e̞ y1 dzː dʲʲ dʰː ɯᵝɯᵝ lː uo1 i.4 i: yɛ5ʲ a4""" ).split(""" """ ) _a : Dict = dict(zip(__lowerCAmelCase , range(len(__lowerCAmelCase ) ) ) ) _a : Any = {"""pad_token""": """<pad>""", """unk_token""": """<unk>""", """bos_token""": """<s>""", """eos_token""": """</s>"""} _a : List[Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""vocab_file"""] ) with open(self.vocab_file , """w""" , encoding="""utf-8""" ) as fp: fp.write(json.dumps(__lowerCAmelCase ) + """\n""" ) def _lowercase ( self : Tuple , UpperCAmelCase__ : Optional[Any] , UpperCAmelCase__ : Tuple=False , UpperCAmelCase__ : str=20 , UpperCAmelCase__ : Optional[Any]=5 ) -> Tuple[str, list]: _a : int = [(i, tokenizer.decode([i] , clean_up_tokenization_spaces=__lowerCAmelCase )) for i in range(len(__lowerCAmelCase ) )] _a : Tuple = list(filter(lambda UpperCAmelCase__ : [t[0]] == tokenizer.encode(t[1] , do_phonemize=__lowerCAmelCase ) , __lowerCAmelCase ) ) if max_length is not None and len(__lowerCAmelCase ) > max_length: _a : str = toks[:max_length] if min_length is not None and len(__lowerCAmelCase ) < min_length and len(__lowerCAmelCase ) > 0: while len(__lowerCAmelCase ) < min_length: _a : List[str] = toks + toks # toks_str = [t[1] for t in toks] _a : Optional[int] = [t[0] for t in toks] # Ensure consistency _a : int = tokenizer.decode(__lowerCAmelCase , clean_up_tokenization_spaces=__lowerCAmelCase ) if " " not in output_txt and len(__lowerCAmelCase ) > 1: _a : str = ( tokenizer.decode([toks_ids[0]] , clean_up_tokenization_spaces=__lowerCAmelCase ) + """ """ + tokenizer.decode(toks_ids[1:] , clean_up_tokenization_spaces=__lowerCAmelCase ) ) if with_prefix_space: _a : Dict = """ """ + output_txt _a : List[Any] = tokenizer.encode(__lowerCAmelCase , add_special_tokens=__lowerCAmelCase ) return output_txt, output_ids def _lowercase ( self : Any , **UpperCAmelCase__ : str ) -> Tuple: kwargs.update(self.special_tokens_map ) return WavaVecaPhonemeCTCTokenizer.from_pretrained(self.tmpdirname , **__lowerCAmelCase ) def _lowercase ( self : Optional[Any] ) -> Optional[int]: _a : Optional[int] = self.tokenizer_class.from_pretrained("""facebook/wav2vec2-lv-60-espeak-cv-ft""" ) # check adding a single token tokenizer.add_tokens("""xxx""" ) _a : Tuple = tokenizer("""m xxx ɪ""" , do_phonemize=__lowerCAmelCase ).input_ids self.assertEqual(__lowerCAmelCase , [13, 392, 17] ) # xxx should be last token tokenizer.add_tokens(["""aaa""", """bbb""", """ccc"""] ) _a : List[Any] = tokenizer("""m aaa ɪ ccc""" , do_phonemize=__lowerCAmelCase ).input_ids self.assertEqual(__lowerCAmelCase , [13, 393, 17, 395] ) # aaa and ccc should be after xxx and 2 after aaa _a : Optional[int] = tokenizer("""maɪ c""" , do_phonemize=__lowerCAmelCase ).input_ids self.assertEqual(__lowerCAmelCase , [3, 200] ) # mai should be <unk> (=3) def _lowercase ( self : List[Any] ) -> Optional[int]: _a : Optional[int] = self.tokenizer_class.from_pretrained("""facebook/wav2vec2-lv-60-espeak-cv-ft""" ) _a : int = """Hello how are you""" _a : Union[str, Any] = tokenizer.phonemize(__lowerCAmelCase , phonemizer_lang="""en-us""" ) self.assertEqual(__lowerCAmelCase , """h ə l oʊ h aʊ ɑːɹ j uː""" ) def _lowercase ( self : Optional[Any] ) -> Dict: _a : str = self.tokenizer_class.from_pretrained("""facebook/wav2vec2-lv-60-espeak-cv-ft""" ) _a : Optional[Any] = """Hello how are you""" _a : Any = tokenizer.phonemize(__lowerCAmelCase , phonemizer_lang="""en-us""" ) self.assertEqual(tokenizer(__lowerCAmelCase ).input_ids , tokenizer(__lowerCAmelCase , do_phonemize=__lowerCAmelCase ).input_ids ) def _lowercase ( self : List[str] ) -> Optional[Any]: _a : Optional[Any] = self.tokenizer_class.from_pretrained("""facebook/wav2vec2-lv-60-espeak-cv-ft""" ) _a : int = """Hello how are you""" _a : List[Any] = tokenizer.phonemize(__lowerCAmelCase , phonemizer_lang="""en-us""" ) _a : Optional[int] = tokenizer.decode(tokenizer(__lowerCAmelCase ).input_ids ) self.assertEqual(__lowerCAmelCase , __lowerCAmelCase ) def _lowercase ( self : List[str] ) -> Optional[int]: _a : Union[str, Any] = self.tokenizer_class.from_pretrained("""facebook/wav2vec2-lv-60-espeak-cv-ft""" ) _a : List[Any] = [ [11, 5, 15, tokenizer.pad_token_id, 15, 8, 98], [24, 22, 5, 24, 22, 5, 77], ] _a : str = tokenizer.decode(sample_ids[0] ) _a : Union[str, Any] = tokenizer.batch_decode(__lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , batch_tokens[0] ) self.assertEqual(__lowerCAmelCase , ["""k s ɾ ɾ l ɭʲ""", """j ð s j ð s oːɹ"""] ) def _lowercase ( self : List[Any] ) -> str: _a : Tuple = self.tokenizer_class.from_pretrained( """facebook/wav2vec2-lv-60-espeak-cv-ft""" , word_delimiter_token="""|""" ) tokenizer.add_tokens("""|""" ) _a : List[str] = """Hello how are you""" _a : int = tokenizer.phonemize(__lowerCAmelCase , phonemizer_lang="""en-us""" ) self.assertEqual(__lowerCAmelCase , """h ə l oʊ | h aʊ | ɑːɹ | j uː |""" ) def _lowercase ( self : List[Any] ) -> Tuple: _a : Any = self.tokenizer_class.from_pretrained( """facebook/wav2vec2-lv-60-espeak-cv-ft""" , word_delimiter_token="""|""" ) tokenizer.add_tokens("""|""" ) _a : List[Any] = """Hello how are you""" _a : List[str] = tokenizer.phonemize(__lowerCAmelCase , phonemizer_lang="""en-us""" ) self.assertEqual(tokenizer(__lowerCAmelCase ).input_ids , tokenizer(__lowerCAmelCase , do_phonemize=__lowerCAmelCase ).input_ids ) def _lowercase ( self : List[Any] ) -> Any: _a : Optional[int] = self.tokenizer_class.from_pretrained( """facebook/wav2vec2-lv-60-espeak-cv-ft""" , word_delimiter_token="""|""" ) tokenizer.add_tokens("""|""" ) # fmt: off _a : Any = [ [11, 5, 15, tokenizer.pad_token_id, tokenizer.word_delimiter_token_id, 15, 8, tokenizer.word_delimiter_token_id, 98], [tokenizer.word_delimiter_token_id, 24, 22, tokenizer.word_delimiter_token_id, 5, 24, 22, 5, 77], ] # fmt: on # decode with word_del_token filter _a : Union[str, Any] = tokenizer.decode(sample_ids[0] ) _a : Any = tokenizer.batch_decode(__lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , batch_tokens[0] ) self.assertEqual(__lowerCAmelCase , ["""k s ɾ ɾ l ɭʲ""", """j ð s j ð s oːɹ"""] ) # decode with no word_del_token filter _a : Tuple = tokenizer.decode(sample_ids[0] , filter_word_delimiter_token=__lowerCAmelCase ) _a : Dict = tokenizer.batch_decode(__lowerCAmelCase , filter_word_delimiter_token=__lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , batch_tokens[0] ) self.assertEqual(__lowerCAmelCase , ["""k s ɾ | ɾ l | ɭʲ""", """| j ð | s j ð s oːɹ"""] ) def _lowercase ( self : Dict ) -> Tuple: _a : Optional[Any] = self.tokenizer_class.from_pretrained( """facebook/wav2vec2-lv-60-espeak-cv-ft""" , word_delimiter_token="""|""" ) tokenizer.add_tokens("""|""" ) _a : List[Any] = """Hello how are you""" _a : Union[str, Any] = tokenizer.phonemize(__lowerCAmelCase , phonemizer_lang="""en-us""" ) _a : Tuple = tokenizer.decode(tokenizer(__lowerCAmelCase ).input_ids , filter_word_delimiter_token=__lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , __lowerCAmelCase ) def _lowercase ( self : Dict ) -> Union[str, Any]: _a : Optional[Any] = self.tokenizer_class.from_pretrained( """facebook/wav2vec2-lv-60-espeak-cv-ft""" , word_delimiter_token="""|""" ) tokenizer.add_tokens("""|""" ) _a : Any = """Hello how are you""" _a : str = tokenizer.phonemize(__lowerCAmelCase , phonemizer_lang="""en-us""" ) _a : Optional[Any] = tokenizer.decode(tokenizer(__lowerCAmelCase ).input_ids , filter_word_delimiter_token=__lowerCAmelCase ) self.assertEqual(""" """.join([p.strip() for p in phonemes.split(""" |""" )] ).strip() , __lowerCAmelCase ) def _lowercase ( self : List[str] ) -> Any: _a : Tuple = self.tokenizer_class.from_pretrained( """facebook/wav2vec2-lv-60-espeak-cv-ft""" , word_delimiter_token=__lowerCAmelCase ) _a : Optional[int] = """Hello how are you""" _a : Union[str, Any] = tokenizer(__lowerCAmelCase , phonemizer_lang="""en-us""" ).input_ids _a : Union[str, Any] = tokenizer(__lowerCAmelCase , phonemizer_lang="""fr-fr""" ).input_ids self.assertNotEqual(__lowerCAmelCase , __lowerCAmelCase ) _a : Union[str, Any] = tokenizer.decode(__lowerCAmelCase ) _a : str = tokenizer.decode(__lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , """h ə l oʊ h aʊ ɑːɹ j uː""" ) self.assertEqual(__lowerCAmelCase , """ɛ l o h aʊ a ʁ j u""" ) def _lowercase ( self : List[Any] ) -> Optional[Any]: _a : List[str] = self.tokenizer_class.from_pretrained("""facebook/wav2vec2-lv-60-espeak-cv-ft""" ) _a : List[str] = """Hello how Are you""" _a : Dict = """hello how are you""" _a : Tuple = tokenizer(__lowerCAmelCase ).input_ids _a : Any = tokenizer(__lowerCAmelCase ).input_ids self.assertEqual(__lowerCAmelCase , __lowerCAmelCase ) def _lowercase ( self : Union[str, Any] ) -> Tuple: _a : List[Any] = self.tokenizer_class.from_pretrained("""facebook/wav2vec2-lv-60-espeak-cv-ft""" ) tokenizer.add_tokens(["""!""", """?"""] ) tokenizer.add_special_tokens({"""cls_token""": """$$$"""} ) # fmt: off _a : Tuple = [ [11, 5, 15, tokenizer.pad_token_id, 15, 8, 98, 392, 392, 393, 392, 392, 393, 394, 394], [24, 22, 5, 24, 22, 5, 77, tokenizer.pad_token_id, 394, 394], ] # fmt: on _a : Any = tokenizer.batch_decode(__lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , ["""k s ɾ ɾ l ɭʲ!?!? $$$""", """j ð s j ð s oːɹ $$$"""] ) @staticmethod def _lowercase ( UpperCAmelCase__ : Tuple , UpperCAmelCase__ : int ) -> List[Any]: _a : Union[str, Any] = [d[key] for d in offsets] return retrieved_list def _lowercase ( self : Dict ) -> int: _a : Optional[Any] = self.get_tokenizer(word_delimiter_token="""|""" ) tokenizer.add_tokens("""|""" ) # fmt: off # ksssɾɾ|ɾɾ<pad>ɾɾ|<pad>ɾlll|ɭʲ -> k s ɾ ɾ | ɾ l | ɭʲ" _a : Dict = [11, 5, 5, 5, 15, 15, tokenizer.pad_token_id, 15, 15, tokenizer.word_delimiter_token_id, tokenizer.pad_token_id, 15, 8, 8, 8, tokenizer.word_delimiter_token_id, 98] # fmt: on _a : int = tokenizer.decode(__lowerCAmelCase , output_char_offsets=__lowerCAmelCase , filter_word_delimiter_token=__lowerCAmelCase ) # check Wav2Vec2CTCTokenizerOutput keys for char self.assertEqual(len(outputs.keys() ) , 2 ) self.assertTrue("""text""" in outputs ) self.assertTrue("""char_offsets""" in outputs ) self.assertTrue(isinstance(__lowerCAmelCase , __lowerCAmelCase ) ) # check that order of chars is correct and identical for both outputs self.assertEqual(""" """.join(self.get_from_offsets(outputs["""char_offsets"""] , """char""" ) ) , outputs.text ) self.assertListEqual( self.get_from_offsets(outputs["""char_offsets"""] , """char""" ) , ["""k""", """s""", """ɾ""", """ɾ""", """|""", """ɾ""", """l""", """|""", """ɭʲ"""] ) # check that offsets are actually correct for char # 0-1 is 11, 1-4 is 5, 4-6 is first 15, 6-7 is <pad> (thus not shown), 7-9 is second 15, 9-10 is word_delimiter_token, # 10-11 is <pad> (thus not shown), 11-12 is third 15, 12-15 is 8, 15-16 is word_delimiter_token, 16-17 is 98 self.assertListEqual( self.get_from_offsets(outputs["""char_offsets"""] , """start_offset""" ) , [0, 1, 4, 7, 9, 11, 12, 15, 16] ) self.assertListEqual( self.get_from_offsets(outputs["""char_offsets"""] , """end_offset""" ) , [1, 4, 6, 9, 10, 12, 15, 16, 17] ) def _lowercase ( self : Dict ) -> Union[str, Any]: _a : Optional[int] = self.get_tokenizer(word_delimiter_token="""|""" ) def check_list_tuples_equal(UpperCAmelCase__ : Any , UpperCAmelCase__ : Tuple ): self.assertTrue(isinstance(__lowerCAmelCase , __lowerCAmelCase ) ) self.assertTrue(isinstance(outputs_list[0] , __lowerCAmelCase ) ) # transform list to ModelOutput _a : List[str] = WavaVecaPhonemeCTCTokenizerOutput( {k: [d[k] for d in outputs_list] for k in outputs_list[0]} ) self.assertListEqual(outputs_batch["""text"""] , outputs_batch_a["""text"""] ) def recursive_check(UpperCAmelCase__ : List[str] , UpperCAmelCase__ : int ): if isinstance(__lowerCAmelCase , __lowerCAmelCase ): [recursive_check(__lowerCAmelCase , __lowerCAmelCase ) for la, la in zip(__lowerCAmelCase , __lowerCAmelCase )] self.assertEqual(__lowerCAmelCase , __lowerCAmelCase ) if "char_offsets" in outputs_batch: recursive_check(outputs_batch["""char_offsets"""] , outputs_batch_a["""char_offsets"""] ) # fmt: off _a : Dict = [ [11, 5, 15, tokenizer.pad_token_id, 15, 4, 8, 98, 32, 32, 32, 32, 4, 33, tokenizer.word_delimiter_token_id, 32, 32, 33, 34, 34], [24, 22, 5, tokenizer.word_delimiter_token_id, tokenizer.word_delimiter_token_id, 24, 22, 22, 22, 4, 5, 77, tokenizer.pad_token_id, 22, 22, 4, 34, 34, 34, 34], ] # fmt: on # We assume that `decode` works as expected. All we will check now is # the output type is correct and the output is identical to `decode` # char _a : List[str] = tokenizer.batch_decode(__lowerCAmelCase , output_char_offsets=__lowerCAmelCase ) _a : Any = [tokenizer.decode(__lowerCAmelCase , output_char_offsets=__lowerCAmelCase ) for ids in sample_ids] check_list_tuples_equal(__lowerCAmelCase , __lowerCAmelCase ) @unittest.skip("""Wav2Vec2PhonemeTokenizer always lower cases letters to correctly map to phonemes""" ) def _lowercase ( self : Any ) -> List[str]: pass @unittest.skip("""Wav2Vec2PhonemeTokenizer always puts spaces between phonemes""" ) def _lowercase ( self : Optional[int] ) -> List[str]: pass @unittest.skip("""encodes to text to ids, but decodes ids to phonemes -> not possible to have internal consistency""" ) def _lowercase ( self : int ) -> str: pass @unittest.skip("""Wav2Vec2PhonemeModel has no max model length => no testing""" ) def _lowercase ( self : Any ) -> Union[str, Any]: pass def _lowercase ( self : str ) -> str: _a : Tuple = self.get_tokenizers(do_lower_case=__lowerCAmelCase ) for tokenizer in tokenizers: with self.subTest(f"""{tokenizer.__class__.__name__}""" ): _a : List[str] = tokenizer.vocab_size _a : List[str] = len(__lowerCAmelCase ) self.assertNotEqual(__lowerCAmelCase , 0 ) # We usually have added tokens from the start in tests because our vocab fixtures are # smaller than the original vocabs - let's not assert this # self.assertEqual(vocab_size, all_size) _a : List[str] = ["""aaaaa bbbbbb""", """cccccccccdddddddd"""] _a : Union[str, Any] = tokenizer.add_tokens(__lowerCAmelCase ) _a : int = tokenizer.vocab_size _a : List[str] = len(__lowerCAmelCase ) self.assertNotEqual(__lowerCAmelCase , 0 ) self.assertEqual(__lowerCAmelCase , __lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , len(__lowerCAmelCase ) ) self.assertEqual(__lowerCAmelCase , all_size + len(__lowerCAmelCase ) ) _a : Tuple = tokenizer.encode("""aaaaa bbbbbb low cccccccccdddddddd l""" , add_special_tokens=__lowerCAmelCase ) self.assertGreaterEqual(len(__lowerCAmelCase ) , 4 ) self.assertGreater(tokens[0] , tokenizer.vocab_size - 1 ) self.assertGreater(tokens[-3] , tokenizer.vocab_size - 1 ) _a : Dict = {"""eos_token""": """>>>>|||<||<<|<<""", """pad_token""": """<<<<<|||>|>>>>|>"""} _a : Dict = tokenizer.add_special_tokens(__lowerCAmelCase ) _a : Union[str, Any] = tokenizer.vocab_size _a : Dict = len(__lowerCAmelCase ) self.assertNotEqual(__lowerCAmelCase , 0 ) self.assertEqual(__lowerCAmelCase , __lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , len(__lowerCAmelCase ) ) self.assertEqual(__lowerCAmelCase , all_size_a + len(__lowerCAmelCase ) ) _a : Dict = tokenizer.encode( """>>>>|||<||<<|<< aaaaabbbbbb low cccccccccdddddddd <<<<<|||>|>>>>|> l""" , add_special_tokens=__lowerCAmelCase ) self.assertGreaterEqual(len(__lowerCAmelCase ) , 6 ) self.assertGreater(tokens[0] , tokenizer.vocab_size - 1 ) self.assertGreater(tokens[0] , tokens[1] ) self.assertGreater(tokens[-3] , tokenizer.vocab_size - 1 ) self.assertGreater(tokens[-3] , tokens[-4] ) self.assertEqual(tokens[0] , tokenizer.eos_token_id ) self.assertEqual(tokens[-3] , tokenizer.pad_token_id ) @unittest.skip("""The tokenizer shouldn't be used to encode input IDs (except for labels), only to decode.""" ) def _lowercase ( self : Any ) -> Union[str, Any]: pass @unittest.skip("""The tokenizer shouldn't be used to encode input IDs (except for labels), only to decode.""" ) def _lowercase ( self : Any ) -> int: pass def _lowercase ( self : List[Any] ) -> Union[str, Any]: _a : str = self.get_tokenizers(fast=__lowerCAmelCase , do_lower_case=__lowerCAmelCase ) for tokenizer in tokenizers: with self.subTest(f"""{tokenizer.__class__.__name__}""" ): _a : Optional[int] = ["""ð""", """ɪ""", """s""", """ɪ""", """z""", """ɐ""", """t""", """ɛ""", """k""", """s""", """t"""] _a : str = tokenizer.convert_tokens_to_string(__lowerCAmelCase ) self.assertIsInstance(output["""text"""] , __lowerCAmelCase )
294
from datetime import datetime import requests from bsa import BeautifulSoup if __name__ == "__main__": A : List[str] = input('''Enter image url: ''').strip() print(F'''Downloading image from {url} ...''') A : Any = BeautifulSoup(requests.get(url).content, '''html.parser''') # The image URL is in the content field of the first meta tag with property og:image A : List[Any] = soup.find('''meta''', {'''property''': '''og:image'''})['''content'''] A : Dict = requests.get(image_url).content A : Tuple = F'''{datetime.now():%Y-%m-%d_%H:%M:%S}.jpg''' with open(file_name, '''wb''') as fp: fp.write(image_data) print(F'''Done. Image saved to disk as {file_name}.''')
274
0
import unittest import torch from diffusers import DDIMScheduler, DDPMScheduler, UNetaDModel from diffusers.training_utils import set_seed from diffusers.utils.testing_utils import slow __UpperCAmelCase = False class lowerCamelCase (unittest.TestCase ): '''simple docstring''' def __UpperCAmelCase ( self , _UpperCamelCase=3_2 ) -> List[str]: set_seed(0 ) UpperCAmelCase_ : int = UNetaDModel(sample_size=__lowerCAmelCase , in_channels=3 , out_channels=3 ) UpperCAmelCase_ : Dict = torch.optim.SGD(model.parameters() , lr=0.00_01 ) return model, optimizer @slow def __UpperCAmelCase ( self ) -> List[str]: UpperCAmelCase_ : Optional[Any] = 'cpu' # ensure full determinism without setting the CUBLAS_WORKSPACE_CONFIG env variable UpperCAmelCase_ : List[str] = DDPMScheduler( num_train_timesteps=1_0_0_0 , beta_start=0.00_01 , beta_end=0.02 , beta_schedule='linear' , clip_sample=__lowerCAmelCase , ) UpperCAmelCase_ : Optional[Any] = DDIMScheduler( num_train_timesteps=1_0_0_0 , beta_start=0.00_01 , beta_end=0.02 , beta_schedule='linear' , clip_sample=__lowerCAmelCase , ) assert ddpm_scheduler.config.num_train_timesteps == ddim_scheduler.config.num_train_timesteps # shared batches for DDPM and DDIM set_seed(0 ) UpperCAmelCase_ : Tuple = [torch.randn((4, 3, 3_2, 3_2) ).clip(-1 , 1 ).to(__lowerCAmelCase ) for _ in range(4 )] UpperCAmelCase_ : Optional[int] = [torch.randn((4, 3, 3_2, 3_2) ).to(__lowerCAmelCase ) for _ in range(4 )] UpperCAmelCase_ : str = [torch.randint(0 , 1_0_0_0 , (4,) ).long().to(__lowerCAmelCase ) for _ in range(4 )] # train with a DDPM scheduler UpperCAmelCase_ , UpperCAmelCase_ : Optional[Any] = self.get_model_optimizer(resolution=3_2 ) model.train().to(__lowerCAmelCase ) for i in range(4 ): optimizer.zero_grad() UpperCAmelCase_ : Any = ddpm_scheduler.add_noise(clean_images[i] , noise[i] , timesteps[i] ) UpperCAmelCase_ : List[Any] = model(__lowerCAmelCase , timesteps[i] ).sample UpperCAmelCase_ : Union[str, Any] = torch.nn.functional.mse_loss(__lowerCAmelCase , noise[i] ) loss.backward() optimizer.step() del model, optimizer # recreate the model and optimizer, and retry with DDIM UpperCAmelCase_ , UpperCAmelCase_ : List[Any] = self.get_model_optimizer(resolution=3_2 ) model.train().to(__lowerCAmelCase ) for i in range(4 ): optimizer.zero_grad() UpperCAmelCase_ : int = ddim_scheduler.add_noise(clean_images[i] , noise[i] , timesteps[i] ) UpperCAmelCase_ : Optional[int] = model(__lowerCAmelCase , timesteps[i] ).sample UpperCAmelCase_ : Union[str, Any] = torch.nn.functional.mse_loss(__lowerCAmelCase , noise[i] ) loss.backward() optimizer.step() del model, optimizer self.assertTrue(torch.allclose(__lowerCAmelCase , __lowerCAmelCase , atol=1E-5 ) ) self.assertTrue(torch.allclose(__lowerCAmelCase , __lowerCAmelCase , atol=1E-5 ) )
29
# This code is adapted from OpenAI's release # https://github.com/openai/human-eval/blob/master/human_eval/execution.py import contextlib import faulthandler import io import multiprocessing import os import platform import signal import tempfile def __lowerCamelCase ( __a :List[str] , __a :List[Any] , __a :Union[str, Any] , __a :List[Any] ) -> Dict: """simple docstring""" A__ = multiprocessing.Manager() A__ = manager.list() A__ = multiprocessing.Process(target=__a , args=(check_program, result, timeout) ) p.start() p.join(timeout=timeout + 1 ) if p.is_alive(): p.kill() if not result: result.append("""timed out""" ) return { "task_id": task_id, "passed": result[0] == "passed", "result": result[0], "completion_id": completion_id, } def __lowerCamelCase ( __a :Optional[Any] , __a :Any , __a :List[Any] ) -> Union[str, Any]: """simple docstring""" with create_tempdir(): # These system calls are needed when cleaning up tempdir. import os import shutil A__ = shutil.rmtree A__ = os.rmdir A__ = os.chdir # Disable functionalities that can make destructive changes to the test. reliability_guard() # Run program. try: A__ = {} with swallow_io(): with time_limit(__a ): exec(__a , __a ) result.append("""passed""" ) except TimeoutException: result.append("""timed out""" ) except BaseException as e: result.append(F'failed: {e}' ) # Needed for cleaning up. A__ = rmtree A__ = rmdir A__ = chdir @contextlib.contextmanager def __lowerCamelCase ( __a :List[str] ) -> Dict: """simple docstring""" def signal_handler(__a :List[Any] , __a :Optional[Any] ): raise TimeoutException("""Timed out!""" ) signal.setitimer(signal.ITIMER_REAL , __a ) signal.signal(signal.SIGALRM , __a ) try: yield finally: signal.setitimer(signal.ITIMER_REAL , 0 ) @contextlib.contextmanager def __lowerCamelCase ( ) -> Union[str, Any]: """simple docstring""" A__ = WriteOnlyStringIO() with contextlib.redirect_stdout(__a ): with contextlib.redirect_stderr(__a ): with redirect_stdin(__a ): yield @contextlib.contextmanager def __lowerCamelCase ( ) -> Dict: """simple docstring""" with tempfile.TemporaryDirectory() as dirname: with chdir(__a ): yield dirname class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' pass class A (io.StringIO ): '''simple docstring''' def a_ ( self : Any , *__lowerCAmelCase : List[str] , **__lowerCAmelCase : str ) -> Dict: """simple docstring""" raise OSError def a_ ( self : Optional[Any] , *__lowerCAmelCase : Any , **__lowerCAmelCase : Optional[int] ) -> str: """simple docstring""" raise OSError def a_ ( self : Optional[Any] , *__lowerCAmelCase : Any , **__lowerCAmelCase : Any ) -> int: """simple docstring""" raise OSError def a_ ( self : str , *__lowerCAmelCase : Any , **__lowerCAmelCase : Union[str, Any] ) -> int: """simple docstring""" return False class A (contextlib._RedirectStream ): # type: ignore '''simple docstring''' __lowerCamelCase : Union[str, Any] = '''stdin''' @contextlib.contextmanager def __lowerCamelCase ( __a :Union[str, Any] ) -> List[str]: """simple docstring""" if root == ".": yield return A__ = os.getcwd() os.chdir(__a ) try: yield except BaseException as exc: raise exc finally: os.chdir(__a ) def __lowerCamelCase ( __a :Union[str, Any]=None ) -> Dict: """simple docstring""" if maximum_memory_bytes is not None: import resource resource.setrlimit(resource.RLIMIT_AS , (maximum_memory_bytes, maximum_memory_bytes) ) resource.setrlimit(resource.RLIMIT_DATA , (maximum_memory_bytes, maximum_memory_bytes) ) if not platform.uname().system == "Darwin": resource.setrlimit(resource.RLIMIT_STACK , (maximum_memory_bytes, maximum_memory_bytes) ) faulthandler.disable() import builtins A__ = None A__ = None import os A__ = """1""" A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None import shutil A__ = None A__ = None A__ = None import subprocess A__ = None # type: ignore A__ = None import sys A__ = None A__ = None A__ = None A__ = None A__ = None
274
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available a_ = { '''configuration_groupvit''': [ '''GROUPVIT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''GroupViTConfig''', '''GroupViTOnnxConfig''', '''GroupViTTextConfig''', '''GroupViTVisionConfig''', ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a_ = [ '''GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''GroupViTModel''', '''GroupViTPreTrainedModel''', '''GroupViTTextModel''', '''GroupViTVisionModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a_ = [ '''TF_GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFGroupViTModel''', '''TFGroupViTPreTrainedModel''', '''TFGroupViTTextModel''', '''TFGroupViTVisionModel''', ] if TYPE_CHECKING: from .configuration_groupvit import ( GROUPVIT_PRETRAINED_CONFIG_ARCHIVE_MAP, GroupViTConfig, GroupViTOnnxConfig, GroupViTTextConfig, GroupViTVisionConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_groupvit import ( GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST, GroupViTModel, GroupViTPreTrainedModel, GroupViTTextModel, GroupViTVisionModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_groupvit import ( TF_GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST, TFGroupViTModel, TFGroupViTPreTrainedModel, TFGroupViTTextModel, TFGroupViTVisionModel, ) else: import sys a_ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
340
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_albert import AlbertTokenizer else: A : Tuple = None A : Optional[Any] = logging.get_logger(__name__) A : Tuple = {'''vocab_file''': '''spiece.model''', '''tokenizer_file''': '''tokenizer.json'''} A : List[str] = { '''vocab_file''': { '''albert-base-v1''': '''https://huggingface.co/albert-base-v1/resolve/main/spiece.model''', '''albert-large-v1''': '''https://huggingface.co/albert-large-v1/resolve/main/spiece.model''', '''albert-xlarge-v1''': '''https://huggingface.co/albert-xlarge-v1/resolve/main/spiece.model''', '''albert-xxlarge-v1''': '''https://huggingface.co/albert-xxlarge-v1/resolve/main/spiece.model''', '''albert-base-v2''': '''https://huggingface.co/albert-base-v2/resolve/main/spiece.model''', '''albert-large-v2''': '''https://huggingface.co/albert-large-v2/resolve/main/spiece.model''', '''albert-xlarge-v2''': '''https://huggingface.co/albert-xlarge-v2/resolve/main/spiece.model''', '''albert-xxlarge-v2''': '''https://huggingface.co/albert-xxlarge-v2/resolve/main/spiece.model''', }, '''tokenizer_file''': { '''albert-base-v1''': '''https://huggingface.co/albert-base-v1/resolve/main/tokenizer.json''', '''albert-large-v1''': '''https://huggingface.co/albert-large-v1/resolve/main/tokenizer.json''', '''albert-xlarge-v1''': '''https://huggingface.co/albert-xlarge-v1/resolve/main/tokenizer.json''', '''albert-xxlarge-v1''': '''https://huggingface.co/albert-xxlarge-v1/resolve/main/tokenizer.json''', '''albert-base-v2''': '''https://huggingface.co/albert-base-v2/resolve/main/tokenizer.json''', '''albert-large-v2''': '''https://huggingface.co/albert-large-v2/resolve/main/tokenizer.json''', '''albert-xlarge-v2''': '''https://huggingface.co/albert-xlarge-v2/resolve/main/tokenizer.json''', '''albert-xxlarge-v2''': '''https://huggingface.co/albert-xxlarge-v2/resolve/main/tokenizer.json''', }, } A : List[str] = { '''albert-base-v1''': 5_1_2, '''albert-large-v1''': 5_1_2, '''albert-xlarge-v1''': 5_1_2, '''albert-xxlarge-v1''': 5_1_2, '''albert-base-v2''': 5_1_2, '''albert-large-v2''': 5_1_2, '''albert-xlarge-v2''': 5_1_2, '''albert-xxlarge-v2''': 5_1_2, } A : Optional[int] = '''▁''' class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : str = VOCAB_FILES_NAMES __lowerCamelCase : List[Any] = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase : Dict = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase : List[str] = AlbertTokenizer def __init__( self : Tuple , __lowerCAmelCase : Tuple=None , __lowerCAmelCase : List[str]=None , __lowerCAmelCase : Optional[Any]=True , __lowerCAmelCase : List[Any]=True , __lowerCAmelCase : str=False , __lowerCAmelCase : Union[str, Any]="[CLS]" , __lowerCAmelCase : int="[SEP]" , __lowerCAmelCase : Dict="<unk>" , __lowerCAmelCase : Dict="[SEP]" , __lowerCAmelCase : Union[str, Any]="<pad>" , __lowerCAmelCase : str="[CLS]" , __lowerCAmelCase : int="[MASK]" , **__lowerCAmelCase : Optional[Any] , ) -> Optional[Any]: """simple docstring""" A__ = ( AddedToken(__lowerCAmelCase , lstrip=__lowerCAmelCase , rstrip=__lowerCAmelCase , normalized=__lowerCAmelCase ) if isinstance(__lowerCAmelCase , __lowerCAmelCase ) else mask_token ) super().__init__( __lowerCAmelCase , tokenizer_file=__lowerCAmelCase , do_lower_case=__lowerCAmelCase , remove_space=__lowerCAmelCase , keep_accents=__lowerCAmelCase , bos_token=__lowerCAmelCase , eos_token=__lowerCAmelCase , unk_token=__lowerCAmelCase , sep_token=__lowerCAmelCase , pad_token=__lowerCAmelCase , cls_token=__lowerCAmelCase , mask_token=__lowerCAmelCase , **__lowerCAmelCase , ) A__ = do_lower_case A__ = remove_space A__ = keep_accents A__ = vocab_file A__ = False if not self.vocab_file else True def a_ ( self : List[Any] , __lowerCAmelCase : List[int] , __lowerCAmelCase : Optional[List[int]] = None ) -> List[int]: """simple docstring""" A__ = [self.sep_token_id] A__ = [self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def a_ ( self : Union[str, Any] , __lowerCAmelCase : List[int] , __lowerCAmelCase : Optional[List[int]] = None ) -> List[int]: """simple docstring""" A__ = [self.sep_token_id] A__ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def a_ ( self : Tuple , __lowerCAmelCase : str , __lowerCAmelCase : Optional[str] = None ) -> Tuple[str]: """simple docstring""" 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(__lowerCAmelCase ): logger.error(f'Vocabulary path ({save_directory}) should be a directory' ) return A__ = os.path.join( __lowerCAmelCase , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(__lowerCAmelCase ): copyfile(self.vocab_file , __lowerCAmelCase ) return (out_vocab_file,)
274
0
import darl # noqa import gym import tqdm from diffusers.experimental import ValueGuidedRLPipeline _A = { '''n_samples''': 64, '''horizon''': 32, '''num_inference_steps''': 20, '''n_guide_steps''': 2, # can set to 0 for faster sampling, does not use value network '''scale_grad_by_std''': True, '''scale''': 0.1, '''eta''': 0.0, '''t_grad_cutoff''': 2, '''device''': '''cpu''', } if __name__ == "__main__": _A = '''hopper-medium-v2''' _A = gym.make(env_name) _A = ValueGuidedRLPipeline.from_pretrained( '''bglick13/hopper-medium-v2-value-function-hor32''', env=env, ) env.seed(0) _A = env.reset() _A = 0 _A = 0 _A = 1_000 _A = [obs.copy()] try: for t in tqdm.tqdm(range(T)): # call the policy _A = pipeline(obs, planning_horizon=32) # execute action in environment _A = env.step(denorm_actions) _A = env.get_normalized_score(total_reward) # update return total_reward += reward total_score += score print( F'''Step: {t}, Reward: {reward}, Total Reward: {total_reward}, Score: {score}, Total Score:''' F''' {total_score}''' ) # save observations for rendering rollout.append(next_observation.copy()) _A = next_observation except KeyboardInterrupt: pass print(F'''Total reward: {total_reward}''')
122
import dataclasses import json import warnings from dataclasses import dataclass, field from time import time from typing import List from ..utils import logging A : Dict = logging.get_logger(__name__) def __lowerCamelCase ( __a :int=None , __a :Optional[Any]=None ) -> int: """simple docstring""" return field(default_factory=lambda: default , metadata=__a ) @dataclass class A : '''simple docstring''' __lowerCamelCase : List[str] = list_field( default=[] , metadata={ '''help''': ( '''Model checkpoints to be provided to the AutoModel classes. Leave blank to benchmark the base version''' ''' of all available models''' ) } , ) __lowerCamelCase : List[int] = list_field( default=[8] , metadata={'''help''': '''List of batch sizes for which memory and time performance will be evaluated'''} ) __lowerCamelCase : List[int] = list_field( default=[8, 32, 128, 512] , metadata={'''help''': '''List of sequence lengths for which memory and time performance will be evaluated'''} , ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Whether to benchmark inference of model. Inference can be disabled via --no-inference.'''} , ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Whether to run on available cuda devices. Cuda can be disabled via --no-cuda.'''} , ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Whether to run on available tpu devices. TPU can be disabled via --no-tpu.'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Use FP16 to accelerate inference.'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Benchmark training of model'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Verbose memory tracing'''} ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Whether to perform speed measurements. Speed measurements can be disabled via --no-speed.'''} , ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={ '''help''': '''Whether to perform memory measurements. Memory measurements can be disabled via --no-memory''' } , ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Trace memory line by line'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Save result to a CSV file'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Save all print statements in a log file'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Whether to print environment information'''} ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={ '''help''': ( '''Whether to use multiprocessing for memory and speed measurement. It is highly recommended to use''' ''' multiprocessing for accurate CPU and GPU memory measurements. This option should only be disabled''' ''' for debugging / testing and on TPU.''' ) } , ) __lowerCamelCase : str = field( default=F'''inference_time_{round(time() )}.csv''' , metadata={'''help''': '''CSV filename used if saving time results to csv.'''} , ) __lowerCamelCase : str = field( default=F'''inference_memory_{round(time() )}.csv''' , metadata={'''help''': '''CSV filename used if saving memory results to csv.'''} , ) __lowerCamelCase : str = field( default=F'''train_time_{round(time() )}.csv''' , metadata={'''help''': '''CSV filename used if saving time results to csv for training.'''} , ) __lowerCamelCase : str = field( default=F'''train_memory_{round(time() )}.csv''' , metadata={'''help''': '''CSV filename used if saving memory results to csv for training.'''} , ) __lowerCamelCase : str = field( default=F'''env_info_{round(time() )}.csv''' , metadata={'''help''': '''CSV filename used if saving environment information.'''} , ) __lowerCamelCase : str = field( default=F'''log_{round(time() )}.csv''' , metadata={'''help''': '''Log filename used if print statements are saved in log.'''} , ) __lowerCamelCase : int = field(default=3 , metadata={'''help''': '''Times an experiment will be run.'''} ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={ '''help''': ( '''Instead of loading the model as defined in `config.architectures` if exists, just load the pretrain''' ''' model weights.''' ) } , ) def a_ ( self : Dict ) -> Union[str, Any]: """simple docstring""" warnings.warn( f'The class {self.__class__} is deprecated. Hugging Face Benchmarking utils' """ are deprecated in general and it is advised to use external Benchmarking libraries """ """ to benchmark Transformer models.""" , __lowerCAmelCase , ) def a_ ( self : Union[str, Any] ) -> List[str]: """simple docstring""" return json.dumps(dataclasses.asdict(self ) , indent=2 ) @property def a_ ( self : Tuple ) -> List[str]: """simple docstring""" if len(self.models ) <= 0: raise ValueError( """Please make sure you provide at least one model name / model identifier, *e.g.* `--models""" """ bert-base-cased` or `args.models = ['bert-base-cased'].""" ) return self.models @property def a_ ( self : Union[str, Any] ) -> Optional[Any]: """simple docstring""" if not self.multi_process: return False elif self.is_tpu: logger.info("""Multiprocessing is currently not possible on TPU.""" ) return False else: return True
274
0
def UpperCAmelCase_ ( __snake_case , __snake_case ) -> int: """simple docstring""" return int(input_a == input_a == 0 ) def UpperCAmelCase_ ( ) -> None: """simple docstring""" print('''Truth Table of NOR Gate:''' ) print('''| Input 1 | Input 2 | Output |''' ) print(F"| 0 | 0 | {nor_gate(0 , 0 )} |" ) print(F"| 0 | 1 | {nor_gate(0 , 1 )} |" ) print(F"| 1 | 0 | {nor_gate(1 , 0 )} |" ) print(F"| 1 | 1 | {nor_gate(1 , 1 )} |" ) if __name__ == "__main__": import doctest doctest.testmod() main()
5
from math import ceil def __lowerCamelCase ( __a :int = 1_0_0_1 ) -> int: """simple docstring""" A__ = 1 for i in range(1 , int(ceil(n / 2.0 ) ) ): A__ = 2 * i + 1 A__ = 2 * i A__ = total + 4 * odd**2 - 6 * even return total if __name__ == "__main__": import sys if len(sys.argv) == 1: print(solution()) else: try: A : List[str] = int(sys.argv[1]) print(solution(n)) except ValueError: print('''Invalid entry - please enter a number''')
274
0
'''simple docstring''' import argparse import gc import json import os import shutil import warnings import torch from transformers import LlamaConfig, LlamaForCausalLM, LlamaTokenizer try: from transformers import LlamaTokenizerFast except ImportError as e: warnings.warn(e) warnings.warn( "The converted tokenizer will be the `slow` tokenizer. To use the fast, update your `tokenizers` library and re-run the tokenizer conversion" ) _SCREAMING_SNAKE_CASE = None _SCREAMING_SNAKE_CASE = { '''7B''': 1_10_08, '''13B''': 1_38_24, '''30B''': 1_79_20, '''65B''': 2_20_16, '''70B''': 2_86_72, } _SCREAMING_SNAKE_CASE = { '''7B''': 1, '''7Bf''': 1, '''13B''': 2, '''13Bf''': 2, '''30B''': 4, '''65B''': 8, '''70B''': 8, '''70Bf''': 8, } def __a(SCREAMING_SNAKE_CASE_ : List[Any] , SCREAMING_SNAKE_CASE_ : List[Any]=1 , SCREAMING_SNAKE_CASE_ : Union[str, Any]=256 ): '''simple docstring''' return multiple_of * ((int(ffn_dim_multiplier * int(8 * n / 3 ) ) + multiple_of - 1) // multiple_of) def __a(SCREAMING_SNAKE_CASE_ : Optional[Any] ): '''simple docstring''' with open(__a , "r" ) as f: return json.load(__a ) def __a(SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Any ): '''simple docstring''' with open(__a , "w" ) as f: json.dump(__a , __a ) def __a(SCREAMING_SNAKE_CASE_ : List[Any] , SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : List[str] , SCREAMING_SNAKE_CASE_ : int=True ): '''simple docstring''' os.makedirs(__a , exist_ok=__a ) _lowerCAmelCase = os.path.join(__a , "tmp" ) os.makedirs(__a , exist_ok=__a ) _lowerCAmelCase = read_json(os.path.join(__a , "params.json" ) ) _lowerCAmelCase = NUM_SHARDS[model_size] _lowerCAmelCase = params["n_layers"] _lowerCAmelCase = params["n_heads"] _lowerCAmelCase = n_heads // num_shards _lowerCAmelCase = params["dim"] _lowerCAmelCase = dim // n_heads _lowerCAmelCase = 1_0000.0 _lowerCAmelCase = 1.0 / (base ** (torch.arange(0 , __a , 2 ).float() / dims_per_head)) if "n_kv_heads" in params: _lowerCAmelCase = params["n_kv_heads"] # for GQA / MQA _lowerCAmelCase = n_heads_per_shard // num_key_value_heads _lowerCAmelCase = dim // num_key_value_heads else: # compatibility with other checkpoints _lowerCAmelCase = n_heads _lowerCAmelCase = n_heads_per_shard _lowerCAmelCase = dim # permute for sliced rotary def permute(SCREAMING_SNAKE_CASE_ : List[Any] , SCREAMING_SNAKE_CASE_ : Tuple=n_heads , SCREAMING_SNAKE_CASE_ : int=dim , SCREAMING_SNAKE_CASE_ : int=dim ): return w.view(__a , dima // n_heads // 2 , 2 , __a ).transpose(1 , 2 ).reshape(__a , __a ) print(F'''Fetching all parameters from the checkpoint at {input_base_path}.''' ) # Load weights if model_size == "7B": # Not sharded # (The sharded implementation would also work, but this is simpler.) _lowerCAmelCase = torch.load(os.path.join(__a , "consolidated.00.pth" ) , map_location="cpu" ) else: # Sharded _lowerCAmelCase = [ torch.load(os.path.join(__a , F'''consolidated.{i:02d}.pth''' ) , map_location="cpu" ) for i in range(__a ) ] _lowerCAmelCase = 0 _lowerCAmelCase = {"weight_map": {}} for layer_i in range(__a ): _lowerCAmelCase = F'''pytorch_model-{layer_i + 1}-of-{n_layers + 1}.bin''' if model_size == "7B": # Unsharded _lowerCAmelCase = { F'''model.layers.{layer_i}.self_attn.q_proj.weight''': permute( loaded[F'''layers.{layer_i}.attention.wq.weight'''] ), F'''model.layers.{layer_i}.self_attn.k_proj.weight''': permute( loaded[F'''layers.{layer_i}.attention.wk.weight'''] ), F'''model.layers.{layer_i}.self_attn.v_proj.weight''': loaded[F'''layers.{layer_i}.attention.wv.weight'''], F'''model.layers.{layer_i}.self_attn.o_proj.weight''': loaded[F'''layers.{layer_i}.attention.wo.weight'''], F'''model.layers.{layer_i}.mlp.gate_proj.weight''': loaded[F'''layers.{layer_i}.feed_forward.w1.weight'''], F'''model.layers.{layer_i}.mlp.down_proj.weight''': loaded[F'''layers.{layer_i}.feed_forward.w2.weight'''], F'''model.layers.{layer_i}.mlp.up_proj.weight''': loaded[F'''layers.{layer_i}.feed_forward.w3.weight'''], F'''model.layers.{layer_i}.input_layernorm.weight''': loaded[F'''layers.{layer_i}.attention_norm.weight'''], F'''model.layers.{layer_i}.post_attention_layernorm.weight''': loaded[F'''layers.{layer_i}.ffn_norm.weight'''], } else: # Sharded # Note that attention.w{q,k,v,o}, feed_fordward.w[1,2,3], attention_norm.weight and ffn_norm.weight share # the same storage object, saving attention_norm and ffn_norm will save other weights too, which is # redundant as other weights will be stitched from multiple shards. To avoid that, they are cloned. _lowerCAmelCase = { F'''model.layers.{layer_i}.input_layernorm.weight''': loaded[0][ F'''layers.{layer_i}.attention_norm.weight''' ].clone(), F'''model.layers.{layer_i}.post_attention_layernorm.weight''': loaded[0][ F'''layers.{layer_i}.ffn_norm.weight''' ].clone(), } _lowerCAmelCase = permute( torch.cat( [ loaded[i][F'''layers.{layer_i}.attention.wq.weight'''].view(__a , __a , __a ) for i in range(__a ) ] , dim=0 , ).reshape(__a , __a ) ) _lowerCAmelCase = permute( torch.cat( [ loaded[i][F'''layers.{layer_i}.attention.wk.weight'''].view( __a , __a , __a ) for i in range(__a ) ] , dim=0 , ).reshape(__a , __a ) , __a , __a , __a , ) _lowerCAmelCase = torch.cat( [ loaded[i][F'''layers.{layer_i}.attention.wv.weight'''].view( __a , __a , __a ) for i in range(__a ) ] , dim=0 , ).reshape(__a , __a ) _lowerCAmelCase = torch.cat( [loaded[i][F'''layers.{layer_i}.attention.wo.weight'''] for i in range(__a )] , dim=1 ) _lowerCAmelCase = torch.cat( [loaded[i][F'''layers.{layer_i}.feed_forward.w1.weight'''] for i in range(__a )] , dim=0 ) _lowerCAmelCase = torch.cat( [loaded[i][F'''layers.{layer_i}.feed_forward.w2.weight'''] for i in range(__a )] , dim=1 ) _lowerCAmelCase = torch.cat( [loaded[i][F'''layers.{layer_i}.feed_forward.w3.weight'''] for i in range(__a )] , dim=0 ) _lowerCAmelCase = inv_freq for k, v in state_dict.items(): _lowerCAmelCase = filename param_count += v.numel() torch.save(__a , os.path.join(__a , __a ) ) _lowerCAmelCase = F'''pytorch_model-{n_layers + 1}-of-{n_layers + 1}.bin''' if model_size == "7B": # Unsharded _lowerCAmelCase = { "model.embed_tokens.weight": loaded["tok_embeddings.weight"], "model.norm.weight": loaded["norm.weight"], "lm_head.weight": loaded["output.weight"], } else: _lowerCAmelCase = { "model.norm.weight": loaded[0]["norm.weight"], "model.embed_tokens.weight": torch.cat( [loaded[i]["tok_embeddings.weight"] for i in range(__a )] , dim=1 ), "lm_head.weight": torch.cat([loaded[i]["output.weight"] for i in range(__a )] , dim=0 ), } for k, v in state_dict.items(): _lowerCAmelCase = filename param_count += v.numel() torch.save(__a , os.path.join(__a , __a ) ) # Write configs _lowerCAmelCase = {"total_size": param_count * 2} write_json(__a , os.path.join(__a , "pytorch_model.bin.index.json" ) ) _lowerCAmelCase = params["ffn_dim_multiplier"] if "ffn_dim_multiplier" in params else 1 _lowerCAmelCase = params["multiple_of"] if "multiple_of" in params else 256 _lowerCAmelCase = LlamaConfig( hidden_size=__a , intermediate_size=compute_intermediate_size(__a , __a , __a ) , num_attention_heads=params["n_heads"] , num_hidden_layers=params["n_layers"] , rms_norm_eps=params["norm_eps"] , num_key_value_heads=__a , ) config.save_pretrained(__a ) # Make space so we can load the model properly now. del state_dict del loaded gc.collect() print("Loading the checkpoint in a Llama model." ) _lowerCAmelCase = LlamaForCausalLM.from_pretrained(__a , torch_dtype=torch.floataa , low_cpu_mem_usage=__a ) # Avoid saving this as part of the config. del model.config._name_or_path print("Saving in the Transformers format." ) model.save_pretrained(__a , safe_serialization=__a ) shutil.rmtree(__a ) def __a(SCREAMING_SNAKE_CASE_ : Optional[int] , SCREAMING_SNAKE_CASE_ : List[str] ): '''simple docstring''' _lowerCAmelCase = LlamaTokenizer if LlamaTokenizerFast is None else LlamaTokenizerFast print(F'''Saving a {tokenizer_class.__name__} to {tokenizer_path}.''' ) _lowerCAmelCase = tokenizer_class(__a ) tokenizer.save_pretrained(__a ) def __a(): '''simple docstring''' _lowerCAmelCase = argparse.ArgumentParser() parser.add_argument( "--input_dir" , help="Location of LLaMA weights, which contains tokenizer.model and model folders" , ) parser.add_argument( "--model_size" , choices=["7B", "7Bf", "13B", "13Bf", "30B", "65B", "70B", "70Bf", "tokenizer_only"] , ) parser.add_argument( "--output_dir" , help="Location to write HF model and tokenizer" , ) parser.add_argument("--safe_serialization" , type=__a , help="Whether or not to save using `safetensors`." ) _lowerCAmelCase = parser.parse_args() if args.model_size != "tokenizer_only": write_model( model_path=args.output_dir , input_base_path=os.path.join(args.input_dir , args.model_size ) , model_size=args.model_size , safe_serialization=args.safe_serialization , ) _lowerCAmelCase = os.path.join(args.input_dir , "tokenizer.model" ) write_tokenizer(args.output_dir , __a ) if __name__ == "__main__": main()
158
import argparse import csv import logging import os import random import numpy as np import torch from torch.utils.data import DataLoader, RandomSampler, SequentialSampler, TensorDataset from tqdm import tqdm, trange from transformers import ( CONFIG_NAME, WEIGHTS_NAME, AdamW, OpenAIGPTDoubleHeadsModel, OpenAIGPTTokenizer, get_linear_schedule_with_warmup, ) logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''', datefmt='''%m/%d/%Y %H:%M:%S''', level=logging.INFO ) A : Tuple = logging.getLogger(__name__) def __lowerCamelCase ( __a :Optional[int] , __a :List[str] ) -> Tuple: """simple docstring""" A__ = np.argmax(__a , axis=1 ) return np.sum(outputs == labels ) def __lowerCamelCase ( __a :Tuple ) -> Dict: """simple docstring""" with open(__a , encoding="""utf_8""" ) as f: A__ = csv.reader(__a ) A__ = [] next(__a ) # skip the first line for line in tqdm(__a ): output.append((""" """.join(line[1:5] ), line[5], line[6], int(line[-1] ) - 1) ) return output def __lowerCamelCase ( __a :Optional[int] , __a :List[Any] , __a :Dict , __a :Optional[Any] , __a :Optional[Any] , __a :int ) -> Union[str, Any]: """simple docstring""" A__ = [] for dataset in encoded_datasets: A__ = len(__a ) A__ = np.zeros((n_batch, 2, input_len) , dtype=np.intaa ) A__ = np.zeros((n_batch, 2) , dtype=np.intaa ) A__ = np.full((n_batch, 2, input_len) , fill_value=-1_0_0 , dtype=np.intaa ) A__ = np.zeros((n_batch,) , dtype=np.intaa ) for ( i, (story, conta, conta, mc_label), ) in enumerate(__a ): A__ = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] A__ = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] A__ = with_conta A__ = with_conta A__ = len(__a ) - 1 A__ = len(__a ) - 1 A__ = with_conta A__ = with_conta A__ = mc_label A__ = (input_ids, mc_token_ids, lm_labels, mc_labels) tensor_datasets.append(tuple(torch.tensor(__a ) for t in all_inputs ) ) return tensor_datasets def __lowerCamelCase ( ) -> Union[str, Any]: """simple docstring""" A__ = argparse.ArgumentParser() parser.add_argument("""--model_name""" , type=__a , default="""openai-gpt""" , help="""pretrained model name""" ) parser.add_argument("""--do_train""" , action="""store_true""" , help="""Whether to run training.""" ) parser.add_argument("""--do_eval""" , action="""store_true""" , help="""Whether to run eval on the dev set.""" ) parser.add_argument( """--output_dir""" , default=__a , type=__a , required=__a , help="""The output directory where the model predictions and checkpoints will be written.""" , ) parser.add_argument("""--train_dataset""" , type=__a , default="""""" ) parser.add_argument("""--eval_dataset""" , type=__a , default="""""" ) parser.add_argument("""--seed""" , type=__a , default=4_2 ) parser.add_argument("""--num_train_epochs""" , type=__a , default=3 ) parser.add_argument("""--train_batch_size""" , type=__a , default=8 ) parser.add_argument("""--eval_batch_size""" , type=__a , default=1_6 ) parser.add_argument("""--adam_epsilon""" , default=1E-8 , type=__a , help="""Epsilon for Adam optimizer.""" ) parser.add_argument("""--max_grad_norm""" , type=__a , default=1 ) parser.add_argument( """--max_steps""" , default=-1 , type=__a , help=( """If > 0: set total number of training steps to perform. Override num_train_epochs.""" ) , ) parser.add_argument( """--gradient_accumulation_steps""" , type=__a , default=1 , help="""Number of updates steps to accumulate before performing a backward/update pass.""" , ) parser.add_argument("""--learning_rate""" , type=__a , default=6.25E-5 ) parser.add_argument("""--warmup_steps""" , default=0 , type=__a , help="""Linear warmup over warmup_steps.""" ) parser.add_argument("""--lr_schedule""" , type=__a , default="""warmup_linear""" ) parser.add_argument("""--weight_decay""" , type=__a , default=0.01 ) parser.add_argument("""--lm_coef""" , type=__a , default=0.9 ) parser.add_argument("""--n_valid""" , type=__a , default=3_7_4 ) parser.add_argument("""--server_ip""" , type=__a , default="""""" , help="""Can be used for distant debugging.""" ) parser.add_argument("""--server_port""" , type=__a , default="""""" , help="""Can be used for distant debugging.""" ) A__ = parser.parse_args() print(__a ) if args.server_ip and args.server_port: # Distant debugging - see https://code.visualstudio.com/docs/python/debugging#_attach-to-a-local-script import ptvsd print("""Waiting for debugger attach""" ) ptvsd.enable_attach(address=(args.server_ip, args.server_port) , redirect_output=__a ) ptvsd.wait_for_attach() random.seed(args.seed ) np.random.seed(args.seed ) torch.manual_seed(args.seed ) torch.cuda.manual_seed_all(args.seed ) A__ = torch.device("""cuda""" if torch.cuda.is_available() else """cpu""" ) A__ = torch.cuda.device_count() logger.info("""device: {}, n_gpu {}""".format(__a , __a ) ) if not args.do_train and not args.do_eval: raise ValueError("""At least one of `do_train` or `do_eval` must be True.""" ) if not os.path.exists(args.output_dir ): os.makedirs(args.output_dir ) # Load tokenizer and model # This loading functions also add new tokens and embeddings called `special tokens` # These new embeddings will be fine-tuned on the RocStories dataset A__ = ["""_start_""", """_delimiter_""", """_classify_"""] A__ = OpenAIGPTTokenizer.from_pretrained(args.model_name ) tokenizer.add_tokens(__a ) A__ = tokenizer.convert_tokens_to_ids(__a ) A__ = OpenAIGPTDoubleHeadsModel.from_pretrained(args.model_name ) model.resize_token_embeddings(len(__a ) ) model.to(__a ) # Load and encode the datasets def tokenize_and_encode(__a :Tuple ): if isinstance(__a , __a ): return tokenizer.convert_tokens_to_ids(tokenizer.tokenize(__a ) ) elif isinstance(__a , __a ): return obj return [tokenize_and_encode(__a ) for o in obj] logger.info("""Encoding dataset...""" ) A__ = load_rocstories_dataset(args.train_dataset ) A__ = load_rocstories_dataset(args.eval_dataset ) A__ = (train_dataset, eval_dataset) A__ = tokenize_and_encode(__a ) # Compute the max input length for the Transformer A__ = model.config.n_positions // 2 - 2 A__ = max( len(story[:max_length] ) + max(len(conta[:max_length] ) , len(conta[:max_length] ) ) + 3 for dataset in encoded_datasets for story, conta, conta, _ in dataset ) A__ = min(__a , model.config.n_positions ) # Max size of input for the pre-trained model # Prepare inputs tensors and dataloaders A__ = pre_process_datasets(__a , __a , __a , *__a ) A__ , A__ = tensor_datasets[0], tensor_datasets[1] A__ = TensorDataset(*__a ) A__ = RandomSampler(__a ) A__ = DataLoader(__a , sampler=__a , batch_size=args.train_batch_size ) A__ = TensorDataset(*__a ) A__ = SequentialSampler(__a ) A__ = DataLoader(__a , sampler=__a , batch_size=args.eval_batch_size ) # Prepare optimizer if args.do_train: if args.max_steps > 0: A__ = args.max_steps A__ = args.max_steps // (len(__a ) // args.gradient_accumulation_steps) + 1 else: A__ = len(__a ) // args.gradient_accumulation_steps * args.num_train_epochs A__ = list(model.named_parameters() ) A__ = ["""bias""", """LayerNorm.bias""", """LayerNorm.weight"""] A__ = [ { """params""": [p for n, p in param_optimizer if not any(nd in n for nd in no_decay )], """weight_decay""": args.weight_decay, }, {"""params""": [p for n, p in param_optimizer if any(nd in n for nd in no_decay )], """weight_decay""": 0.0}, ] A__ = AdamW(__a , lr=args.learning_rate , eps=args.adam_epsilon ) A__ = get_linear_schedule_with_warmup( __a , num_warmup_steps=args.warmup_steps , num_training_steps=__a ) if args.do_train: A__ , A__ , A__ = 0, 0, None model.train() for _ in trange(int(args.num_train_epochs ) , desc="""Epoch""" ): A__ = 0 A__ = 0 A__ = tqdm(__a , desc="""Training""" ) for step, batch in enumerate(__a ): A__ = tuple(t.to(__a ) for t in batch ) A__ , A__ , A__ , A__ = batch A__ = model(__a , mc_token_ids=__a , lm_labels=__a , mc_labels=__a ) A__ = args.lm_coef * losses[0] + losses[1] loss.backward() optimizer.step() scheduler.step() optimizer.zero_grad() tr_loss += loss.item() A__ = ( loss.item() if exp_average_loss is None else 0.7 * exp_average_loss + 0.3 * loss.item() ) nb_tr_steps += 1 A__ = """Training loss: {:.2e} lr: {:.2e}""".format(__a , scheduler.get_lr()[0] ) # Save a trained model if args.do_train: # Save a trained model, configuration and tokenizer A__ = model.module if hasattr(__a , """module""" ) else model # Only save the model itself # If we save using the predefined names, we can load using `from_pretrained` A__ = os.path.join(args.output_dir , __a ) A__ = os.path.join(args.output_dir , __a ) torch.save(model_to_save.state_dict() , __a ) model_to_save.config.to_json_file(__a ) tokenizer.save_vocabulary(args.output_dir ) # Load a trained model and vocabulary that you have fine-tuned A__ = OpenAIGPTDoubleHeadsModel.from_pretrained(args.output_dir ) A__ = OpenAIGPTTokenizer.from_pretrained(args.output_dir ) model.to(__a ) if args.do_eval: model.eval() A__ , A__ = 0, 0 A__ , A__ = 0, 0 for batch in tqdm(__a , desc="""Evaluating""" ): A__ = tuple(t.to(__a ) for t in batch ) A__ , A__ , A__ , A__ = batch with torch.no_grad(): A__ , A__ , A__ , A__ = model( __a , mc_token_ids=__a , lm_labels=__a , mc_labels=__a ) A__ = mc_logits.detach().cpu().numpy() A__ = mc_labels.to("""cpu""" ).numpy() A__ = accuracy(__a , __a ) eval_loss += mc_loss.mean().item() eval_accuracy += tmp_eval_accuracy nb_eval_examples += input_ids.size(0 ) nb_eval_steps += 1 A__ = eval_loss / nb_eval_steps A__ = eval_accuracy / nb_eval_examples A__ = tr_loss / nb_tr_steps if args.do_train else None A__ = {"""eval_loss""": eval_loss, """eval_accuracy""": eval_accuracy, """train_loss""": train_loss} A__ = os.path.join(args.output_dir , """eval_results.txt""" ) with open(__a , """w""" ) as writer: logger.info("""***** Eval results *****""" ) for key in sorted(result.keys() ): logger.info(""" %s = %s""" , __a , str(result[key] ) ) writer.write("""%s = %s\n""" % (key, str(result[key] )) ) if __name__ == "__main__": main()
274
0
from __future__ import annotations import math from collections import Counter from string import ascii_lowercase def lowerCAmelCase__(__snake_case ) -> None: '''simple docstring''' lowerCamelCase__ , lowerCamelCase__ = analyze_text(__a ) lowerCamelCase__ = list(''' ''' + ascii_lowercase ) # what is our total sum of probabilities. lowerCamelCase__ = sum(single_char_strings.values() ) # one length string lowerCamelCase__ = 0 # for each alpha we go in our dict and if it is in it we calculate entropy for ch in my_alphas: if ch in single_char_strings: lowerCamelCase__ = single_char_strings[ch] lowerCamelCase__ = my_str / all_sum my_fir_sum += prob * math.loga(__a ) # entropy formula. # print entropy print(F'{round(-1 * my_fir_sum ):.1f}' ) # two len string lowerCamelCase__ = sum(two_char_strings.values() ) lowerCamelCase__ = 0 # for each alpha (two in size) calculate entropy. for cha in my_alphas: for cha in my_alphas: lowerCamelCase__ = cha + cha if sequence in two_char_strings: lowerCamelCase__ = two_char_strings[sequence] lowerCamelCase__ = int(__a ) / all_sum my_sec_sum += prob * math.loga(__a ) # print second entropy print(F'{round(-1 * my_sec_sum ):.1f}' ) # print the difference between them print(F'{round((-1 * my_sec_sum) - (-1 * my_fir_sum) ):.1f}' ) def lowerCAmelCase__(__snake_case ) -> tuple[dict, dict]: '''simple docstring''' lowerCamelCase__ = Counter() # type: ignore lowerCamelCase__ = Counter() # type: ignore single_char_strings[text[-1]] += 1 # first case when we have space at start. two_char_strings[" " + text[0]] += 1 for i in range(0 ,len(__a ) - 1 ): single_char_strings[text[i]] += 1 two_char_strings[text[i : i + 2]] += 1 return single_char_strings, two_char_strings def lowerCAmelCase__() -> List[str]: '''simple docstring''' import doctest doctest.testmod() # text = ( # "Had repulsive dashwoods suspicion sincerity but advantage now him. Remark " # "easily garret nor nay. Civil those mrs enjoy shy fat merry. You greatest " # "jointure saw horrible. He private he on be imagine suppose. Fertile " # "beloved evident through no service elderly is. Blind there if every no so " # "at. Own neglected you preferred way sincerity delivered his attempted. To " # "of message cottage windows do besides against uncivil. Delightful " # "unreserved impossible few estimating men favourable see entreaties. She " # "propriety immediate was improving. He or entrance humoured likewise " # "moderate. Much nor game son say feel. Fat make met can must form into " # "gate. Me we offending prevailed discovery. " # ) # calculate_prob(text) if __name__ == "__main__": main()
209
import argparse from collections import defaultdict import yaml A : str = '''docs/source/en/_toctree.yml''' def __lowerCamelCase ( __a :str ) -> List[Any]: """simple docstring""" A__ = defaultdict(__a ) A__ = [] A__ = [] for doc in doc_list: if "local" in doc: counts[doc["local"]] += 1 if doc["title"].lower() == "overview": overview_doc.append({"""local""": doc["""local"""], """title""": doc["""title"""]} ) else: new_doc_list.append(__a ) A__ = new_doc_list A__ = [key for key, value in counts.items() if value > 1] A__ = [] for duplicate_key in duplicates: A__ = list({doc["""title"""] for doc in doc_list if doc["""local"""] == duplicate_key} ) if len(__a ) > 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 doc_list if """local""" not in counts or counts[doc["""local"""]] == 1] ) A__ = sorted(__a , key=lambda __a : s["title"].lower() ) # "overview" gets special treatment and is always first if len(__a ) > 1: raise ValueError("""{doc_list} has two 'overview' docs which is not allowed.""" ) overview_doc.extend(__a ) # Sort return overview_doc def __lowerCamelCase ( __a :Any=False ) -> List[str]: """simple docstring""" with open(__a , encoding="""utf-8""" ) as f: A__ = yaml.safe_load(f.read() ) # Get to the API doc A__ = 0 while content[api_idx]["title"] != "API": api_idx += 1 A__ = content[api_idx]["""sections"""] # Then to the model doc A__ = 0 while api_doc[scheduler_idx]["title"] != "Schedulers": scheduler_idx += 1 A__ = api_doc[scheduler_idx]["""sections"""] A__ = clean_doc_toc(__a ) A__ = False if new_scheduler_doc != scheduler_doc: A__ = True if overwrite: A__ = new_scheduler_doc if diff: if overwrite: A__ = api_doc with open(__a , """w""" , encoding="""utf-8""" ) as f: f.write(yaml.dump(__a , allow_unicode=__a ) ) else: raise ValueError( """The model doc part of the table of content is not properly sorted, run `make style` to fix this.""" ) def __lowerCamelCase ( __a :Optional[int]=False ) -> Dict: """simple docstring""" with open(__a , encoding="""utf-8""" ) as f: A__ = yaml.safe_load(f.read() ) # Get to the API doc A__ = 0 while content[api_idx]["title"] != "API": api_idx += 1 A__ = content[api_idx]["""sections"""] # Then to the model doc A__ = 0 while api_doc[pipeline_idx]["title"] != "Pipelines": pipeline_idx += 1 A__ = False A__ = api_doc[pipeline_idx]["""sections"""] A__ = [] # sort sub pipeline docs for pipeline_doc in pipeline_docs: if "section" in pipeline_doc: A__ = pipeline_doc["""section"""] A__ = clean_doc_toc(__a ) if overwrite: A__ = new_sub_pipeline_doc new_pipeline_docs.append(__a ) # sort overall pipeline doc A__ = clean_doc_toc(__a ) if new_pipeline_docs != pipeline_docs: A__ = True if overwrite: A__ = new_pipeline_docs if diff: if overwrite: A__ = api_doc with open(__a , """w""" , encoding="""utf-8""" ) as f: f.write(yaml.dump(__a , allow_unicode=__a ) ) 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__": A : Tuple = argparse.ArgumentParser() parser.add_argument('''--fix_and_overwrite''', action='''store_true''', help='''Whether to fix inconsistencies.''') A : Optional[Any] = parser.parse_args() check_scheduler_doc(args.fix_and_overwrite) check_pipeline_doc(args.fix_and_overwrite)
274
0
import argparse import json from pathlib import Path import requests import timm import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import AutoImageProcessor, SwinvaConfig, SwinvaForImageClassification def lowercase_ ( _A : int ): """simple docstring""" lowerCamelCase__ : List[Any] = SwinvaConfig() lowerCamelCase__ : Optional[int] = swinva_name.split("_" ) lowerCamelCase__ : Union[str, Any] = name_split[1] if "to" in name_split[3]: lowerCamelCase__ : Dict = int(name_split[3][-3:] ) else: lowerCamelCase__ : Any = int(name_split[3] ) if "to" in name_split[2]: lowerCamelCase__ : str = int(name_split[2][-2:] ) else: lowerCamelCase__ : Any = int(name_split[2][6:] ) if model_size == "tiny": lowerCamelCase__ : Union[str, Any] = 96 lowerCamelCase__ : Union[str, Any] = (2, 2, 6, 2) lowerCamelCase__ : int = (3, 6, 12, 24) elif model_size == "small": lowerCamelCase__ : Optional[int] = 96 lowerCamelCase__ : int = (2, 2, 18, 2) lowerCamelCase__ : Optional[Any] = (3, 6, 12, 24) elif model_size == "base": lowerCamelCase__ : Tuple = 128 lowerCamelCase__ : Any = (2, 2, 18, 2) lowerCamelCase__ : List[Any] = (4, 8, 16, 32) else: lowerCamelCase__ : Tuple = 192 lowerCamelCase__ : Optional[Any] = (2, 2, 18, 2) lowerCamelCase__ : List[Any] = (6, 12, 24, 48) if "to" in swinva_name: lowerCamelCase__ : Dict = (12, 12, 12, 6) if ("22k" in swinva_name) and ("to" not in swinva_name): lowerCamelCase__ : Optional[int] = 21841 lowerCamelCase__ : List[str] = "huggingface/label-files" lowerCamelCase__ : int = "imagenet-22k-id2label.json" lowerCamelCase__ : Dict = json.load(open(hf_hub_download(__a , __a , repo_type="dataset" ) , "r" ) ) lowerCamelCase__ : Dict = {int(__a ): v for k, v in idalabel.items()} lowerCamelCase__ : Tuple = idalabel lowerCamelCase__ : int = {v: k for k, v in idalabel.items()} else: lowerCamelCase__ : int = 1000 lowerCamelCase__ : Any = "huggingface/label-files" lowerCamelCase__ : Optional[int] = "imagenet-1k-id2label.json" lowerCamelCase__ : str = json.load(open(hf_hub_download(__a , __a , repo_type="dataset" ) , "r" ) ) lowerCamelCase__ : Dict = {int(__a ): v for k, v in idalabel.items()} lowerCamelCase__ : Union[str, Any] = idalabel lowerCamelCase__ : Dict = {v: k for k, v in idalabel.items()} lowerCamelCase__ : Tuple = img_size lowerCamelCase__ : Tuple = num_classes lowerCamelCase__ : List[str] = embed_dim lowerCamelCase__ : int = depths lowerCamelCase__ : List[Any] = num_heads lowerCamelCase__ : Dict = window_size return config def lowercase_ ( _A : List[str] ): """simple docstring""" if "patch_embed.proj" in name: lowerCamelCase__ : str = name.replace("patch_embed.proj" , "embeddings.patch_embeddings.projection" ) if "patch_embed.norm" in name: lowerCamelCase__ : List[Any] = name.replace("patch_embed.norm" , "embeddings.norm" ) if "layers" in name: lowerCamelCase__ : List[Any] = "encoder." + name if "attn.proj" in name: lowerCamelCase__ : str = name.replace("attn.proj" , "attention.output.dense" ) if "attn" in name: lowerCamelCase__ : Tuple = name.replace("attn" , "attention.self" ) if "norm1" in name: lowerCamelCase__ : Tuple = name.replace("norm1" , "layernorm_before" ) if "norm2" in name: lowerCamelCase__ : str = name.replace("norm2" , "layernorm_after" ) if "mlp.fc1" in name: lowerCamelCase__ : List[str] = name.replace("mlp.fc1" , "intermediate.dense" ) if "mlp.fc2" in name: lowerCamelCase__ : Optional[int] = name.replace("mlp.fc2" , "output.dense" ) if "q_bias" in name: lowerCamelCase__ : Dict = name.replace("q_bias" , "query.bias" ) if "k_bias" in name: lowerCamelCase__ : Optional[Any] = name.replace("k_bias" , "key.bias" ) if "v_bias" in name: lowerCamelCase__ : Optional[int] = name.replace("v_bias" , "value.bias" ) if "cpb_mlp" in name: lowerCamelCase__ : Optional[int] = name.replace("cpb_mlp" , "continuous_position_bias_mlp" ) if name == "norm.weight": lowerCamelCase__ : List[Any] = "layernorm.weight" if name == "norm.bias": lowerCamelCase__ : Optional[Any] = "layernorm.bias" if "head" in name: lowerCamelCase__ : Union[str, Any] = name.replace("head" , "classifier" ) else: lowerCamelCase__ : List[str] = "swinv2." + name return name def lowercase_ ( _A : Tuple , _A : List[str] ): """simple docstring""" for key in orig_state_dict.copy().keys(): lowerCamelCase__ : List[str] = orig_state_dict.pop(__a ) if "mask" in key: continue elif "qkv" in key: lowerCamelCase__ : Optional[Any] = key.split("." ) lowerCamelCase__ : Union[str, Any] = int(key_split[1] ) lowerCamelCase__ : str = int(key_split[3] ) lowerCamelCase__ : Union[str, Any] = model.swinva.encoder.layers[layer_num].blocks[block_num].attention.self.all_head_size if "weight" in key: lowerCamelCase__ : List[Any] = val[:dim, :] lowerCamelCase__ : List[Any] = val[dim : dim * 2, :] lowerCamelCase__ : List[Any] = val[-dim:, :] else: lowerCamelCase__ : Optional[Any] = val[:dim] lowerCamelCase__ : str = val[ dim : dim * 2 ] lowerCamelCase__ : Dict = val[-dim:] else: lowerCamelCase__ : List[Any] = val return orig_state_dict def lowercase_ ( _A : str , _A : Optional[Any] ): """simple docstring""" lowerCamelCase__ : Optional[Any] = timm.create_model(__a , pretrained=__a ) timm_model.eval() lowerCamelCase__ : int = get_swinva_config(__a ) lowerCamelCase__ : Optional[Any] = SwinvaForImageClassification(__a ) model.eval() lowerCamelCase__ : List[Any] = convert_state_dict(timm_model.state_dict() , __a ) model.load_state_dict(__a ) lowerCamelCase__ : Optional[int] = "http://images.cocodataset.org/val2017/000000039769.jpg" lowerCamelCase__ : Optional[Any] = AutoImageProcessor.from_pretrained("microsoft/{}".format(swinva_name.replace("_" , "-" ) ) ) lowerCamelCase__ : Tuple = Image.open(requests.get(__a , stream=__a ).raw ) lowerCamelCase__ : Dict = image_processor(images=__a , return_tensors="pt" ) lowerCamelCase__ : Tuple = timm_model(inputs["pixel_values"] ) lowerCamelCase__ : Any = model(**__a ).logits assert torch.allclose(__a , __a , atol=1E-3 ) print(F"Saving model {swinva_name} to {pytorch_dump_folder_path}" ) model.save_pretrained(__a ) print(F"Saving image processor to {pytorch_dump_folder_path}" ) image_processor.save_pretrained(__a ) model.push_to_hub( repo_path_or_name=Path(__a , __a ) , organization="nandwalritik" , commit_message="Add model" , ) if __name__ == "__main__": A : Tuple = argparse.ArgumentParser() # Required parameters parser.add_argument( "--swinv2_name", default="swinv2_tiny_patch4_window8_256", type=str, help="Name of the Swinv2 timm model you\'d like to convert.", ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model directory." ) A : Optional[int] = parser.parse_args() convert_swinva_checkpoint(args.swinva_name, args.pytorch_dump_folder_path)
184
def __lowerCamelCase ( __a :str ) -> list: """simple docstring""" A__ = [0] * len(__a ) for i in range(1 , len(__a ) ): # use last results for better performance - dynamic programming A__ = prefix_result[i - 1] while j > 0 and input_string[i] != input_string[j]: A__ = prefix_result[j - 1] if input_string[i] == input_string[j]: j += 1 A__ = j return prefix_result def __lowerCamelCase ( __a :str ) -> int: """simple docstring""" return max(prefix_function(__a ) ) if __name__ == "__main__": import doctest doctest.testmod()
274
0
import json import logging import math import os import sys from dataclasses import dataclass, field from typing import Optional from datasets import Dataset, load_dataset import transformers from transformers import ( CONFIG_MAPPING, MODEL_FOR_MASKED_LM_MAPPING, AutoConfig, AutoModelForMaskedLM, AutoTokenizer, DataCollatorForWholeWordMask, HfArgumentParser, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import get_last_checkpoint, is_main_process lowercase = logging.getLogger(__name__) lowercase = list(MODEL_FOR_MASKED_LM_MAPPING.keys()) lowercase = tuple(conf.model_type for conf in MODEL_CONFIG_CLASSES) @dataclass class UpperCamelCase_ : '''simple docstring''' lowerCAmelCase = field( default=snake_case_ , metadata={ '''help''': ( '''The model checkpoint for weights initialization.Don\'t set if you want to train a model from scratch.''' ) } , ) lowerCAmelCase = field( default=snake_case_ , metadata={'''help''': '''If training from scratch, pass a model type from the list: ''' + ''', '''.join(snake_case_ )} , ) lowerCAmelCase = field( default=snake_case_ , metadata={ '''help''': ( '''Override some existing default config settings when a model is trained from scratch. Example: ''' '''n_embd=10,resid_pdrop=0.2,scale_attn_weights=false,summary_type=cls_index''' ) } , ) lowerCAmelCase = field( default=snake_case_ , metadata={'''help''': '''Pretrained config name or path if not the same as model_name'''} ) lowerCAmelCase = field( default=snake_case_ , metadata={'''help''': '''Pretrained tokenizer name or path if not the same as model_name'''} ) lowerCAmelCase = field( default=snake_case_ , metadata={'''help''': '''Where do you want to store the pretrained models downloaded from huggingface.co'''} , ) lowerCAmelCase = field( default=snake_case_ , metadata={'''help''': '''Whether to use one of the fast tokenizer (backed by the tokenizers library) or not.'''} , ) lowerCAmelCase = field( default='''main''' , metadata={'''help''': '''The specific model version to use (can be a branch name, tag name or commit id).'''} , ) lowerCAmelCase = field( default=snake_case_ , metadata={ '''help''': ( '''Will use the token generated when running `huggingface-cli login` (necessary to use this script ''' '''with private models).''' ) } , ) def _UpperCamelCase ( self ) -> Tuple: if self.config_overrides is not None and (self.config_name is not None or self.model_name_or_path is not None): raise ValueError( '--config_overrides can\'t be used in combination with --config_name or --model_name_or_path' ) @dataclass class UpperCamelCase_ : '''simple docstring''' lowerCAmelCase = field( default=snake_case_ , metadata={'''help''': '''The name of the dataset to use (via the datasets library).'''} ) lowerCAmelCase = field( default=snake_case_ , metadata={'''help''': '''The configuration name of the dataset to use (via the datasets library).'''} ) lowerCAmelCase = field(default=snake_case_ , metadata={'''help''': '''The input training data file (a text file).'''} ) lowerCAmelCase = field( default=snake_case_ , metadata={'''help''': '''An optional input evaluation data file to evaluate the perplexity on (a text file).'''} , ) lowerCAmelCase = field( default=snake_case_ , metadata={'''help''': '''An optional input train ref data file for whole word masking in Chinese.'''} , ) lowerCAmelCase = field( default=snake_case_ , metadata={'''help''': '''An optional input validation ref data file for whole word masking in Chinese.'''} , ) lowerCAmelCase = field( default=snake_case_ , metadata={'''help''': '''Overwrite the cached training and evaluation sets'''} ) lowerCAmelCase = field( default=5 , metadata={ '''help''': '''The percentage of the train set used as validation set in case there\'s no validation split''' } , ) lowerCAmelCase = field( default=snake_case_ , metadata={ '''help''': ( '''The maximum total input sequence length after tokenization. Sequences longer ''' '''than this will be truncated. Default to the max input length of the model.''' ) } , ) lowerCAmelCase = field( default=snake_case_ , metadata={'''help''': '''The number of processes to use for the preprocessing.'''} , ) lowerCAmelCase = field( default=0.15 , metadata={'''help''': '''Ratio of tokens to mask for masked language modeling loss'''} ) lowerCAmelCase = field( default=snake_case_ , metadata={ '''help''': ( '''Whether to pad all samples to `max_seq_length`. ''' '''If False, will pad the samples dynamically when batching to the maximum length in the batch.''' ) } , ) def _UpperCamelCase ( self ) -> Optional[int]: if self.train_file is not None: snake_case_ = self.train_file.split('.' )[-1] assert extension in ["csv", "json", "txt"], "`train_file` should be a csv, a json or a txt file." if self.validation_file is not None: snake_case_ = self.validation_file.split('.' )[-1] assert extension in ["csv", "json", "txt"], "`validation_file` should be a csv, a json or a txt file." def __UpperCAmelCase ( a_ , a_): with open(__a , 'r' , encoding='utf-8') as f: snake_case_ = [json.loads(__a) for line in f.read().splitlines() if (len(__a) > 0 and not line.isspace())] assert len(__a) == len(__a) snake_case_ = {c: dataset[c] for c in dataset.column_names} snake_case_ = refs return Dataset.from_dict(__a) def __UpperCAmelCase ( ): snake_case_ = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments)) if len(sys.argv) == 2 and sys.argv[1].endswith('.json'): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. snake_case_ , snake_case_ , snake_case_ = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1])) else: snake_case_ , snake_case_ , snake_case_ = parser.parse_args_into_dataclasses() # Detecting last checkpoint. snake_case_ = None if os.path.isdir(training_args.output_dir) and training_args.do_train and not training_args.overwrite_output_dir: snake_case_ = get_last_checkpoint(training_args.output_dir) if last_checkpoint is None and len(os.listdir(training_args.output_dir)) > 0: raise ValueError( f'''Output directory ({training_args.output_dir}) already exists and is not empty. ''' 'Use --overwrite_output_dir to overcome.') elif last_checkpoint is not None: logger.info( f'''Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change ''' 'the `--output_dir` or add `--overwrite_output_dir` to train from scratch.') # Setup logging logging.basicConfig( format='%(asctime)s - %(levelname)s - %(name)s - %(message)s' , datefmt='%m/%d/%Y %H:%M:%S' , handlers=[logging.StreamHandler(sys.stdout)] , ) logger.setLevel(logging.INFO if is_main_process(training_args.local_rank) else logging.WARN) # Log on each process the small summary: logger.warning( f'''Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}''' + f'''distributed training: {bool(training_args.local_rank != -1)}, 16-bits training: {training_args.fpaa}''') # Set the verbosity to info of the Transformers logger (on main process only): if is_main_process(training_args.local_rank): transformers.utils.logging.set_verbosity_info() transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() logger.info('Training/evaluation parameters %s' , __a) # Set seed before initializing model. set_seed(training_args.seed) # Get the datasets: you can either provide your own CSV/JSON/TXT training and evaluation files (see below) # or just provide the name of one of the public datasets available on the hub at https://huggingface.co/datasets/ # (the dataset will be downloaded automatically from the datasets Hub). # # For CSV/JSON files, this script will use the column called 'text' or the first column if no column called # 'text' is found. You can easily tweak this behavior (see below). # # In distributed training, the load_dataset function guarantee that only one local process can concurrently # download the dataset. if data_args.dataset_name is not None: # Downloading and loading a dataset from the hub. snake_case_ = load_dataset(data_args.dataset_name , data_args.dataset_config_name) if "validation" not in datasets.keys(): snake_case_ = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split=f'''train[:{data_args.validation_split_percentage}%]''' , ) snake_case_ = load_dataset( data_args.dataset_name , data_args.dataset_config_name , split=f'''train[{data_args.validation_split_percentage}%:]''' , ) else: snake_case_ = {} if data_args.train_file is not None: snake_case_ = data_args.train_file if data_args.validation_file is not None: snake_case_ = data_args.validation_file snake_case_ = data_args.train_file.split('.')[-1] if extension == "txt": snake_case_ = 'text' snake_case_ = load_dataset(__a , data_files=__a) # See more about loading any type of standard or custom dataset (from files, python dict, pandas DataFrame, etc) at # https://huggingface.co/docs/datasets/loading_datasets.html. # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. snake_case_ = { 'cache_dir': model_args.cache_dir, 'revision': model_args.model_revision, 'use_auth_token': True if model_args.use_auth_token else None, } if model_args.config_name: snake_case_ = AutoConfig.from_pretrained(model_args.config_name , **__a) elif model_args.model_name_or_path: snake_case_ = AutoConfig.from_pretrained(model_args.model_name_or_path , **__a) else: snake_case_ = CONFIG_MAPPING[model_args.model_type]() logger.warning('You are instantiating a new config instance from scratch.') if model_args.config_overrides is not None: logger.info(f'''Overriding config: {model_args.config_overrides}''') config.update_from_string(model_args.config_overrides) logger.info(f'''New config: {config}''') snake_case_ = { 'cache_dir': model_args.cache_dir, 'use_fast': model_args.use_fast_tokenizer, 'revision': model_args.model_revision, 'use_auth_token': True if model_args.use_auth_token else None, } if model_args.tokenizer_name: snake_case_ = AutoTokenizer.from_pretrained(model_args.tokenizer_name , **__a) elif model_args.model_name_or_path: snake_case_ = AutoTokenizer.from_pretrained(model_args.model_name_or_path , **__a) else: raise ValueError( 'You are instantiating a new tokenizer from scratch. This is not supported by this script.' 'You can do it from another script, save it, and load it from here, using --tokenizer_name.') if model_args.model_name_or_path: snake_case_ = AutoModelForMaskedLM.from_pretrained( model_args.model_name_or_path , from_tf=bool('.ckpt' in model_args.model_name_or_path) , config=__a , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) else: logger.info('Training new model from scratch') snake_case_ = AutoModelForMaskedLM.from_config(__a) model.resize_token_embeddings(len(__a)) # Preprocessing the datasets. # First we tokenize all the texts. if training_args.do_train: snake_case_ = datasets['train'].column_names else: snake_case_ = datasets['validation'].column_names snake_case_ = 'text' if 'text' in column_names else column_names[0] snake_case_ = 'max_length' if data_args.pad_to_max_length else False def tokenize_function(a_): # Remove empty lines snake_case_ = [line for line in examples['text'] if len(__a) > 0 and not line.isspace()] return tokenizer(examples['text'] , padding=__a , truncation=__a , max_length=data_args.max_seq_length) snake_case_ = datasets.map( __a , batched=__a , num_proc=data_args.preprocessing_num_workers , remove_columns=[text_column_name] , load_from_cache_file=not data_args.overwrite_cache , ) # Add the chinese references if provided if data_args.train_ref_file is not None: snake_case_ = add_chinese_references(tokenized_datasets['train'] , data_args.train_ref_file) if data_args.validation_ref_file is not None: snake_case_ = add_chinese_references( tokenized_datasets['validation'] , data_args.validation_ref_file) # If we have ref files, need to avoid it removed by trainer snake_case_ = data_args.train_ref_file or data_args.validation_ref_file if has_ref: snake_case_ = False # Data collator # This one will take care of randomly masking the tokens. snake_case_ = DataCollatorForWholeWordMask(tokenizer=__a , mlm_probability=data_args.mlm_probability) # Initialize our Trainer snake_case_ = Trainer( model=__a , args=__a , train_dataset=tokenized_datasets['train'] if training_args.do_train else None , eval_dataset=tokenized_datasets['validation'] if training_args.do_eval else None , tokenizer=__a , data_collator=__a , ) # Training if training_args.do_train: if last_checkpoint is not None: snake_case_ = last_checkpoint elif model_args.model_name_or_path is not None and os.path.isdir(model_args.model_name_or_path): snake_case_ = model_args.model_name_or_path else: snake_case_ = None snake_case_ = trainer.train(resume_from_checkpoint=__a) trainer.save_model() # Saves the tokenizer too for easy upload snake_case_ = os.path.join(training_args.output_dir , 'train_results.txt') if trainer.is_world_process_zero(): with open(__a , 'w') as writer: logger.info('***** Train results *****') for key, value in sorted(train_result.metrics.items()): logger.info(f''' {key} = {value}''') writer.write(f'''{key} = {value}\n''') # Need to save the state, since Trainer.save_model saves only the tokenizer with the model trainer.state.save_to_json(os.path.join(training_args.output_dir , 'trainer_state.json')) # Evaluation snake_case_ = {} if training_args.do_eval: logger.info('*** Evaluate ***') snake_case_ = trainer.evaluate() snake_case_ = math.exp(eval_output['eval_loss']) snake_case_ = perplexity snake_case_ = os.path.join(training_args.output_dir , 'eval_results_mlm_wwm.txt') if trainer.is_world_process_zero(): with open(__a , 'w') as writer: logger.info('***** Eval results *****') for key, value in sorted(results.items()): logger.info(f''' {key} = {value}''') writer.write(f'''{key} = {value}\n''') return results def __UpperCAmelCase ( a_): main() if __name__ == "__main__": main()
178
def __lowerCamelCase ( __a :int = 1_0_0_0_0_0_0 ) -> int: """simple docstring""" A__ = limit + 1 A__ = [0] * limit for first_term in range(1 , __a ): for n in range(__a , __a , __a ): A__ = first_term + n / first_term if common_difference % 4: # d must be divisble by 4 continue else: common_difference /= 4 if ( first_term > common_difference and first_term < 4 * common_difference ): # since x,y,z are positive integers frequency[n] += 1 # so z>0 and a>d ,also 4d<a A__ = sum(1 for x in frequency[1:limit] if x == 1_0 ) return count if __name__ == "__main__": print(F'''{solution() = }''')
274
0
import math_equivalence # From: git+https://github.com/hendrycks/math.git import datasets _A = '''\ @article{hendrycksmath2021, title={Measuring Mathematical Problem Solving With the MATH Dataset}, author={Dan Hendrycks and Collin Burns and Saurav Kadavath and Akul Arora and Steven Basart and Eric Tang and Dawn Song and Jacob Steinhardt}, journal={arXiv preprint arXiv:2103.03874}, year={2021} } ''' _A = '''\ This metric is used to assess performance on the Mathematics Aptitude Test of Heuristics (MATH) dataset. It first canonicalizes the inputs (e.g., converting "1/2" to "\\frac{1}{2}") and then computes accuracy. ''' _A = R''' Calculates accuracy after canonicalizing inputs. Args: predictions: list of predictions to score. Each prediction is a string that contains natural language and LaTex. references: list of reference for each prediction. Each reference is a string that contains natural language and LaTex. Returns: accuracy: accuracy after canonicalizing inputs (e.g., converting "1/2" to "\\frac{1}{2}") Examples: >>> metric = datasets.load_metric("competition_math") >>> results = metric.compute(references=["\\frac{1}{2}"], predictions=["1/2"]) >>> print(results) {\'accuracy\': 1.0} ''' @datasets.utils.file_utils.add_end_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class UpperCAmelCase__ ( datasets.Metric ): """simple docstring""" def _a ( self ) -> str: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { 'predictions': datasets.Value('string' ), 'references': datasets.Value('string' ), } ) , homepage='https://github.com/hendrycks/math' , codebase_urls=['https://github.com/hendrycks/math'] , ) def _a ( self , A_ , A_ ) -> str: __UpperCamelCase =0.0 for i, j in zip(__lowerCAmelCase , __lowerCAmelCase ): n_correct += 1.0 if math_equivalence.is_equiv(__lowerCAmelCase , __lowerCAmelCase ) else 0.0 __UpperCamelCase =n_correct / len(__lowerCAmelCase ) return { "accuracy": accuracy, }
62
class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' pass class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' pass class A : '''simple docstring''' def __init__( self : List[Any] ) -> str: """simple docstring""" A__ = [ [], [], [], ] def a_ ( self : Dict , __lowerCAmelCase : int , __lowerCAmelCase : int ) -> None: """simple docstring""" try: if len(self.queues[priority] ) >= 1_00: raise OverflowError("""Maximum queue size is 100""" ) self.queues[priority].append(__lowerCAmelCase ) except IndexError: raise ValueError("""Valid priorities are 0, 1, and 2""" ) def a_ ( self : Optional[Any] ) -> int: """simple docstring""" for queue in self.queues: if queue: return queue.pop(0 ) raise UnderFlowError("""All queues are empty""" ) def __str__( self : Tuple ) -> str: """simple docstring""" return "\n".join(f'Priority {i}: {q}' for i, q in enumerate(self.queues ) ) class A : '''simple docstring''' def __init__( self : int ) -> str: """simple docstring""" A__ = [] def a_ ( self : int , __lowerCAmelCase : int ) -> None: """simple docstring""" if len(self.queue ) == 1_00: raise OverFlowError("""Maximum queue size is 100""" ) self.queue.append(__lowerCAmelCase ) def a_ ( self : List[str] ) -> int: """simple docstring""" if not self.queue: raise UnderFlowError("""The queue is empty""" ) else: A__ = min(self.queue ) self.queue.remove(__lowerCAmelCase ) return data def __str__( self : List[Any] ) -> str: """simple docstring""" return str(self.queue ) def __lowerCamelCase ( ) -> Optional[Any]: """simple docstring""" A__ = FixedPriorityQueue() fpq.enqueue(0 , 1_0 ) fpq.enqueue(1 , 7_0 ) fpq.enqueue(0 , 1_0_0 ) fpq.enqueue(2 , 1 ) fpq.enqueue(2 , 5 ) fpq.enqueue(1 , 7 ) fpq.enqueue(2 , 4 ) fpq.enqueue(1 , 6_4 ) fpq.enqueue(0 , 1_2_8 ) print(__a ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(__a ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) def __lowerCamelCase ( ) -> int: """simple docstring""" A__ = ElementPriorityQueue() epq.enqueue(1_0 ) epq.enqueue(7_0 ) epq.enqueue(1_0_0 ) epq.enqueue(1 ) epq.enqueue(5 ) epq.enqueue(7 ) epq.enqueue(4 ) epq.enqueue(6_4 ) epq.enqueue(1_2_8 ) print(__a ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(__a ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) if __name__ == "__main__": fixed_priority_queue() element_priority_queue()
274
0
'''simple docstring''' def __lowerCAmelCase ( snake_case__ , snake_case__ ): __UpperCamelCase : Tuple = int(__a ) # Initialize Result __UpperCamelCase : Dict = [] # Traverse through all denomination for denomination in reversed(__a ): # Find denominations while int(__a ) >= int(__a ): total_value -= int(__a ) answer.append(__a ) # Append the "answers" array return answer # Driver Code if __name__ == "__main__": _lowerCAmelCase = [] _lowerCAmelCase = '''0''' if ( input('''Do you want to enter your denominations ? (yY/n): ''').strip().lower() == "y" ): _lowerCAmelCase = int(input('''Enter the number of denominations you want to add: ''').strip()) for i in range(0, n): denominations.append(int(input(f'Denomination {i}: ').strip())) _lowerCAmelCase = input('''Enter the change you want to make in Indian Currency: ''').strip() else: # All denominations of Indian Currency if user does not enter _lowerCAmelCase = [1, 2, 5, 10, 20, 50, 100, 500, 2000] _lowerCAmelCase = input('''Enter the change you want to make: ''').strip() if int(value) == 0 or int(value) < 0: print('''The total value cannot be zero or negative.''') else: print(f'Following is minimal change for {value}: ') _lowerCAmelCase = find_minimum_change(denominations, value) # Print result for i in range(len(answer)): print(answer[i], end=''' ''')
298
import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import CLIPTokenizer, CLIPTokenizerFast from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES from transformers.testing_utils import require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available if is_vision_available(): from PIL import Image from transformers import CLIPImageProcessor, CLIPProcessor @require_vision class A (unittest.TestCase ): '''simple docstring''' def a_ ( self : Union[str, Any] ) -> Dict: """simple docstring""" A__ = tempfile.mkdtemp() # fmt: off A__ = ["""l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """lo""", """l</w>""", """w</w>""", """r</w>""", """t</w>""", """low</w>""", """er</w>""", """lowest</w>""", """newer</w>""", """wider""", """<unk>""", """<|startoftext|>""", """<|endoftext|>"""] # fmt: on A__ = dict(zip(__lowerCAmelCase , range(len(__lowerCAmelCase ) ) ) ) A__ = ["""#version: 0.2""", """l o""", """lo w</w>""", """e r</w>""", """"""] A__ = {"""unk_token""": """<unk>"""} A__ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""vocab_file"""] ) A__ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""merges_file"""] ) with open(self.vocab_file , """w""" , encoding="""utf-8""" ) as fp: fp.write(json.dumps(__lowerCAmelCase ) + """\n""" ) with open(self.merges_file , """w""" , encoding="""utf-8""" ) as fp: fp.write("""\n""".join(__lowerCAmelCase ) ) A__ = { """do_resize""": True, """size""": 20, """do_center_crop""": True, """crop_size""": 18, """do_normalize""": True, """image_mean""": [0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3], """image_std""": [0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1], } A__ = os.path.join(self.tmpdirname , __lowerCAmelCase ) with open(self.image_processor_file , """w""" , encoding="""utf-8""" ) as fp: json.dump(__lowerCAmelCase , __lowerCAmelCase ) def a_ ( self : Tuple , **__lowerCAmelCase : Dict ) -> str: """simple docstring""" return CLIPTokenizer.from_pretrained(self.tmpdirname , **__lowerCAmelCase ) def a_ ( self : Union[str, Any] , **__lowerCAmelCase : Dict ) -> List[str]: """simple docstring""" return CLIPTokenizerFast.from_pretrained(self.tmpdirname , **__lowerCAmelCase ) def a_ ( self : List[str] , **__lowerCAmelCase : Optional[Any] ) -> Dict: """simple docstring""" return CLIPImageProcessor.from_pretrained(self.tmpdirname , **__lowerCAmelCase ) def a_ ( self : str ) -> Dict: """simple docstring""" shutil.rmtree(self.tmpdirname ) def a_ ( self : str ) -> Any: """simple docstring""" A__ = [np.random.randint(2_55 , size=(3, 30, 4_00) , dtype=np.uinta )] A__ = [Image.fromarray(np.moveaxis(__lowerCAmelCase , 0 , -1 ) ) for x in image_inputs] return image_inputs def a_ ( self : Optional[int] ) -> Tuple: """simple docstring""" A__ = self.get_tokenizer() A__ = self.get_rust_tokenizer() A__ = self.get_image_processor() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) processor_slow.save_pretrained(self.tmpdirname ) A__ = CLIPProcessor.from_pretrained(self.tmpdirname , use_fast=__lowerCAmelCase ) A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) processor_fast.save_pretrained(self.tmpdirname ) A__ = CLIPProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor_slow.tokenizer.get_vocab() , tokenizer_slow.get_vocab() ) self.assertEqual(processor_fast.tokenizer.get_vocab() , tokenizer_fast.get_vocab() ) self.assertEqual(tokenizer_slow.get_vocab() , tokenizer_fast.get_vocab() ) self.assertIsInstance(processor_slow.tokenizer , __lowerCAmelCase ) self.assertIsInstance(processor_fast.tokenizer , __lowerCAmelCase ) self.assertEqual(processor_slow.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertEqual(processor_fast.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertIsInstance(processor_slow.image_processor , __lowerCAmelCase ) self.assertIsInstance(processor_fast.image_processor , __lowerCAmelCase ) def a_ ( self : Optional[Any] ) -> Union[str, Any]: """simple docstring""" A__ = CLIPProcessor(tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) A__ = self.get_tokenizer(bos_token="""(BOS)""" , eos_token="""(EOS)""" ) A__ = self.get_image_processor(do_normalize=__lowerCAmelCase , padding_value=1.0 ) A__ = CLIPProcessor.from_pretrained( self.tmpdirname , bos_token="""(BOS)""" , eos_token="""(EOS)""" , do_normalize=__lowerCAmelCase , padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , __lowerCAmelCase ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , __lowerCAmelCase ) def a_ ( self : List[Any] ) -> Dict: """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) A__ = self.prepare_image_inputs() A__ = image_processor(__lowerCAmelCase , return_tensors="""np""" ) A__ = processor(images=__lowerCAmelCase , return_tensors="""np""" ) for key in input_image_proc.keys(): self.assertAlmostEqual(input_image_proc[key].sum() , input_processor[key].sum() , delta=1e-2 ) def a_ ( self : Optional[Any] ) -> Any: """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) A__ = """lower newer""" A__ = processor(text=__lowerCAmelCase ) A__ = tokenizer(__lowerCAmelCase ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def a_ ( self : Union[str, Any] ) -> Dict: """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) A__ = """lower newer""" A__ = self.prepare_image_inputs() A__ = processor(text=__lowerCAmelCase , images=__lowerCAmelCase ) self.assertListEqual(list(inputs.keys() ) , ["""input_ids""", """attention_mask""", """pixel_values"""] ) # test if it raises when no input is passed with pytest.raises(__lowerCAmelCase ): processor() def a_ ( self : Tuple ) -> str: """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) A__ = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] A__ = processor.batch_decode(__lowerCAmelCase ) A__ = tokenizer.batch_decode(__lowerCAmelCase ) self.assertListEqual(__lowerCAmelCase , __lowerCAmelCase ) def a_ ( self : Optional[int] ) -> str: """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) A__ = """lower newer""" A__ = self.prepare_image_inputs() A__ = processor(text=__lowerCAmelCase , images=__lowerCAmelCase ) self.assertListEqual(list(inputs.keys() ) , processor.model_input_names )
274
0
"""simple docstring""" import argparse import json import os import torch from transformers.file_utils import has_file from diffusers import UNetaDConditionModel, UNetaDModel _snake_case = False _snake_case = True _snake_case = False if __name__ == "__main__": _snake_case = argparse.ArgumentParser() parser.add_argument( '--repo_path', 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.') _snake_case = parser.parse_args() _snake_case = { '''image_size''': '''sample_size''', '''num_res_blocks''': '''layers_per_block''', '''block_channels''': '''block_out_channels''', '''down_blocks''': '''down_block_types''', '''up_blocks''': '''up_block_types''', '''downscale_freq_shift''': '''freq_shift''', '''resnet_num_groups''': '''norm_num_groups''', '''resnet_act_fn''': '''act_fn''', '''resnet_eps''': '''norm_eps''', '''num_head_channels''': '''attention_head_dim''', } _snake_case = { '''time_steps''': '''time_proj''', '''mid''': '''mid_block''', '''downsample_blocks''': '''down_blocks''', '''upsample_blocks''': '''up_blocks''', } _snake_case = '''''' if has_file(args.repo_path, 'config.json') else '''unet''' with open(os.path.join(args.repo_path, subfolder, 'config.json'), 'r', encoding='utf-8') as reader: _snake_case = reader.read() _snake_case = json.loads(text) if do_only_config: for key in config_parameters_to_change.keys(): config.pop(key, None) if has_file(args.repo_path, 'config.json'): _snake_case = UNetaDModel(**config) else: _snake_case = UNetaDConditionModel if '''ldm-text2im-large-256''' in args.repo_path else UNetaDModel _snake_case = class_name(**config) if do_only_config: model.save_config(os.path.join(args.repo_path, subfolder)) _snake_case = dict(model.config) if do_only_renaming: for key, value in config_parameters_to_change.items(): if key in config: _snake_case = config[key] del config[key] _snake_case = [k.replace('UNetRes', '') for k in config['''down_block_types''']] _snake_case = [k.replace('UNetRes', '') for k in config['''up_block_types''']] if do_only_weights: _snake_case = torch.load(os.path.join(args.repo_path, subfolder, 'diffusion_pytorch_model.bin')) _snake_case = {} for param_key, param_value in state_dict.items(): if param_key.endswith('.op.bias') or param_key.endswith('.op.weight'): continue _snake_case = False for key, new_key in key_parameters_to_change.items(): if not has_changed and param_key.split('.')[0] == key: _snake_case = param_value _snake_case = True if not has_changed: _snake_case = param_value model.load_state_dict(new_state_dict) model.save_pretrained(os.path.join(args.repo_path, subfolder))
294
from multiprocessing import Lock, Pipe, Process # lock used to ensure that two processes do not access a pipe at the same time A : Dict = Lock() def __lowerCamelCase ( __a :Dict , __a :List[str] , __a :Optional[int] , __a :Optional[int] , __a :Optional[Any] , __a :Optional[int] , __a :int ) -> Dict: """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 , 1_0 ): 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(__a ) process_lock.release() # receive your right neighbor's value process_lock.acquire() A__ = rr_cv[0].recv() process_lock.release() # take the lower value since you are on the left A__ = min(__a , __a ) 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(__a ) process_lock.release() # receive your left neighbor's value process_lock.acquire() A__ = lr_cv[0].recv() process_lock.release() # take the higher value since you are on the right A__ = max(__a , __a ) # after all swaps are performed, send the values back to main result_pipe[1].send(__a ) def __lowerCamelCase ( __a :List[str] ) -> int: """simple docstring""" A__ = [] A__ = [] # 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 A__ = Pipe() A__ = Pipe() process_array_.append( Process( target=__a , args=(0, arr[0], None, temp_rs, None, temp_rr, result_pipe[0]) , ) ) A__ = temp_rs A__ = temp_rr for i in range(1 , len(__a ) - 1 ): A__ = Pipe() A__ = Pipe() process_array_.append( Process( target=__a , args=(i, arr[i], temp_ls, temp_rs, temp_lr, temp_rr, result_pipe[i]) , ) ) A__ = temp_rs A__ = temp_rr process_array_.append( Process( target=__a , args=( len(__a ) - 1, arr[len(__a ) - 1], temp_ls, None, temp_lr, None, result_pipe[len(__a ) - 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(__a ) ): A__ = result_pipe[p][0].recv() process_array_[p].join() return arr def __lowerCamelCase ( ) -> str: """simple docstring""" A__ = list(range(1_0 , 0 , -1 ) ) print("""Initial List""" ) print(*__a ) A__ = odd_even_transposition(__a ) print("""Sorted List\n""" ) print(*__a ) if __name__ == "__main__": main()
274
0
import argparse import csv import logging import os import random import numpy as np import torch from torch.utils.data import DataLoader, RandomSampler, SequentialSampler, TensorDataset from tqdm import tqdm, trange from transformers import ( CONFIG_NAME, WEIGHTS_NAME, AdamW, OpenAIGPTDoubleHeadsModel, OpenAIGPTTokenizer, get_linear_schedule_with_warmup, ) logging.basicConfig( format='%(asctime)s - %(levelname)s - %(name)s - %(message)s', datefmt='%m/%d/%Y %H:%M:%S', level=logging.INFO ) __UpperCAmelCase = logging.getLogger(__name__) def lowercase__ ( __snake_case : Optional[int] , __snake_case : List[str] ): '''simple docstring''' UpperCAmelCase_ : int = np.argmax(__a , axis=1 ) return np.sum(outputs == labels ) def lowercase__ ( __snake_case : Tuple ): '''simple docstring''' with open(__a , encoding='utf_8' ) as f: UpperCAmelCase_ : Any = csv.reader(__a ) UpperCAmelCase_ : int = [] next(__a ) # skip the first line for line in tqdm(__a ): output.append((' '.join(line[1:5] ), line[5], line[6], int(line[-1] ) - 1) ) return output def lowercase__ ( __snake_case : Optional[int] , __snake_case : List[Any] , __snake_case : Dict , __snake_case : Optional[Any] , __snake_case : Optional[Any] , __snake_case : int ): '''simple docstring''' UpperCAmelCase_ : Tuple = [] for dataset in encoded_datasets: UpperCAmelCase_ : Any = len(__a ) UpperCAmelCase_ : Optional[int] = np.zeros((n_batch, 2, input_len) , dtype=np.intaa ) UpperCAmelCase_ : List[Any] = np.zeros((n_batch, 2) , dtype=np.intaa ) UpperCAmelCase_ : Optional[Any] = np.full((n_batch, 2, input_len) , fill_value=-100 , dtype=np.intaa ) UpperCAmelCase_ : Tuple = np.zeros((n_batch,) , dtype=np.intaa ) for ( i, (story, conta, conta, mc_label), ) in enumerate(__a ): UpperCAmelCase_ : Tuple = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] UpperCAmelCase_ : Any = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] UpperCAmelCase_ : List[Any] = with_conta UpperCAmelCase_ : str = with_conta UpperCAmelCase_ : List[Any] = len(__a ) - 1 UpperCAmelCase_ : Union[str, Any] = len(__a ) - 1 UpperCAmelCase_ : Tuple = with_conta UpperCAmelCase_ : List[Any] = with_conta UpperCAmelCase_ : List[str] = mc_label UpperCAmelCase_ : str = (input_ids, mc_token_ids, lm_labels, mc_labels) tensor_datasets.append(tuple(torch.tensor(__a ) for t in all_inputs ) ) return tensor_datasets def lowercase__ ( ): '''simple docstring''' UpperCAmelCase_ : List[Any] = argparse.ArgumentParser() parser.add_argument('--model_name' , type=__a , default='openai-gpt' , help='pretrained model name' ) parser.add_argument('--do_train' , action='store_true' , help='Whether to run training.' ) parser.add_argument('--do_eval' , action='store_true' , help='Whether to run eval on the dev set.' ) parser.add_argument( '--output_dir' , default=__a , type=__a , required=__a , help='The output directory where the model predictions and checkpoints will be written.' , ) parser.add_argument('--train_dataset' , type=__a , default='' ) parser.add_argument('--eval_dataset' , type=__a , default='' ) parser.add_argument('--seed' , type=__a , default=42 ) parser.add_argument('--num_train_epochs' , type=__a , default=3 ) parser.add_argument('--train_batch_size' , type=__a , default=8 ) parser.add_argument('--eval_batch_size' , type=__a , default=16 ) parser.add_argument('--adam_epsilon' , default=1E-8 , type=__a , help='Epsilon for Adam optimizer.' ) parser.add_argument('--max_grad_norm' , type=__a , default=1 ) parser.add_argument( '--max_steps' , default=-1 , type=__a , help=( 'If > 0: set total number of training steps to perform. Override num_train_epochs.' ) , ) parser.add_argument( '--gradient_accumulation_steps' , type=__a , default=1 , help='Number of updates steps to accumulate before performing a backward/update pass.' , ) parser.add_argument('--learning_rate' , type=__a , default=6.25E-5 ) parser.add_argument('--warmup_steps' , default=0 , type=__a , help='Linear warmup over warmup_steps.' ) parser.add_argument('--lr_schedule' , type=__a , default='warmup_linear' ) parser.add_argument('--weight_decay' , type=__a , default=0.01 ) parser.add_argument('--lm_coef' , type=__a , default=0.9 ) parser.add_argument('--n_valid' , type=__a , default=374 ) parser.add_argument('--server_ip' , type=__a , default='' , help='Can be used for distant debugging.' ) parser.add_argument('--server_port' , type=__a , default='' , help='Can be used for distant debugging.' ) UpperCAmelCase_ : int = parser.parse_args() print(__a ) if args.server_ip and args.server_port: # Distant debugging - see https://code.visualstudio.com/docs/python/debugging#_attach-to-a-local-script import ptvsd print('Waiting for debugger attach' ) ptvsd.enable_attach(address=(args.server_ip, args.server_port) , redirect_output=__a ) ptvsd.wait_for_attach() random.seed(args.seed ) np.random.seed(args.seed ) torch.manual_seed(args.seed ) torch.cuda.manual_seed_all(args.seed ) UpperCAmelCase_ : List[str] = torch.device('cuda' if torch.cuda.is_available() else 'cpu' ) UpperCAmelCase_ : List[str] = torch.cuda.device_count() logger.info('device: {}, n_gpu {}'.format(__a , __a ) ) if not args.do_train and not args.do_eval: raise ValueError('At least one of `do_train` or `do_eval` must be True.' ) if not os.path.exists(args.output_dir ): os.makedirs(args.output_dir ) # Load tokenizer and model # This loading functions also add new tokens and embeddings called `special tokens` # These new embeddings will be fine-tuned on the RocStories dataset UpperCAmelCase_ : Union[str, Any] = ['_start_', '_delimiter_', '_classify_'] UpperCAmelCase_ : int = OpenAIGPTTokenizer.from_pretrained(args.model_name ) tokenizer.add_tokens(__a ) UpperCAmelCase_ : Optional[int] = tokenizer.convert_tokens_to_ids(__a ) UpperCAmelCase_ : List[str] = OpenAIGPTDoubleHeadsModel.from_pretrained(args.model_name ) model.resize_token_embeddings(len(__a ) ) model.to(__a ) # Load and encode the datasets def tokenize_and_encode(__snake_case : Tuple ): if isinstance(__a , __a ): return tokenizer.convert_tokens_to_ids(tokenizer.tokenize(__a ) ) elif isinstance(__a , __a ): return obj return [tokenize_and_encode(__a ) for o in obj] logger.info('Encoding dataset...' ) UpperCAmelCase_ : Optional[int] = load_rocstories_dataset(args.train_dataset ) UpperCAmelCase_ : Tuple = load_rocstories_dataset(args.eval_dataset ) UpperCAmelCase_ : List[Any] = (train_dataset, eval_dataset) UpperCAmelCase_ : int = tokenize_and_encode(__a ) # Compute the max input length for the Transformer UpperCAmelCase_ : int = model.config.n_positions // 2 - 2 UpperCAmelCase_ : Dict = max( len(story[:max_length] ) + max(len(conta[:max_length] ) , len(conta[:max_length] ) ) + 3 for dataset in encoded_datasets for story, conta, conta, _ in dataset ) UpperCAmelCase_ : int = min(__a , model.config.n_positions ) # Max size of input for the pre-trained model # Prepare inputs tensors and dataloaders UpperCAmelCase_ : List[str] = pre_process_datasets(__a , __a , __a , *__a ) UpperCAmelCase_ , UpperCAmelCase_ : int = tensor_datasets[0], tensor_datasets[1] UpperCAmelCase_ : List[Any] = TensorDataset(*__a ) UpperCAmelCase_ : Any = RandomSampler(__a ) UpperCAmelCase_ : int = DataLoader(__a , sampler=__a , batch_size=args.train_batch_size ) UpperCAmelCase_ : Optional[Any] = TensorDataset(*__a ) UpperCAmelCase_ : Optional[Any] = SequentialSampler(__a ) UpperCAmelCase_ : int = DataLoader(__a , sampler=__a , batch_size=args.eval_batch_size ) # Prepare optimizer if args.do_train: if args.max_steps > 0: UpperCAmelCase_ : Optional[int] = args.max_steps UpperCAmelCase_ : Union[str, Any] = args.max_steps // (len(__a ) // args.gradient_accumulation_steps) + 1 else: UpperCAmelCase_ : List[str] = len(__a ) // args.gradient_accumulation_steps * args.num_train_epochs UpperCAmelCase_ : Any = list(model.named_parameters() ) UpperCAmelCase_ : str = ['bias', 'LayerNorm.bias', 'LayerNorm.weight'] UpperCAmelCase_ : Optional[Any] = [ { 'params': [p for n, p in param_optimizer if not any(nd in n for nd in no_decay )], 'weight_decay': args.weight_decay, }, {'params': [p for n, p in param_optimizer if any(nd in n for nd in no_decay )], 'weight_decay': 0.0}, ] UpperCAmelCase_ : str = AdamW(__a , lr=args.learning_rate , eps=args.adam_epsilon ) UpperCAmelCase_ : Optional[int] = get_linear_schedule_with_warmup( __a , num_warmup_steps=args.warmup_steps , num_training_steps=__a ) if args.do_train: UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : Optional[Any] = 0, 0, None model.train() for _ in trange(int(args.num_train_epochs ) , desc='Epoch' ): UpperCAmelCase_ : Any = 0 UpperCAmelCase_ : Optional[Any] = 0 UpperCAmelCase_ : Dict = tqdm(__a , desc='Training' ) for step, batch in enumerate(__a ): UpperCAmelCase_ : Any = tuple(t.to(__a ) for t in batch ) UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : Tuple = batch UpperCAmelCase_ : int = model(__a , mc_token_ids=__a , lm_labels=__a , mc_labels=__a ) UpperCAmelCase_ : Any = args.lm_coef * losses[0] + losses[1] loss.backward() optimizer.step() scheduler.step() optimizer.zero_grad() tr_loss += loss.item() UpperCAmelCase_ : Union[str, Any] = ( loss.item() if exp_average_loss is None else 0.7 * exp_average_loss + 0.3 * loss.item() ) nb_tr_steps += 1 UpperCAmelCase_ : int = 'Training loss: {:.2e} lr: {:.2e}'.format(__a , scheduler.get_lr()[0] ) # Save a trained model if args.do_train: # Save a trained model, configuration and tokenizer UpperCAmelCase_ : Optional[int] = model.module if hasattr(__a , 'module' ) else model # Only save the model itself # If we save using the predefined names, we can load using `from_pretrained` UpperCAmelCase_ : Dict = os.path.join(args.output_dir , __a ) UpperCAmelCase_ : int = os.path.join(args.output_dir , __a ) torch.save(model_to_save.state_dict() , __a ) model_to_save.config.to_json_file(__a ) tokenizer.save_vocabulary(args.output_dir ) # Load a trained model and vocabulary that you have fine-tuned UpperCAmelCase_ : int = OpenAIGPTDoubleHeadsModel.from_pretrained(args.output_dir ) UpperCAmelCase_ : Any = OpenAIGPTTokenizer.from_pretrained(args.output_dir ) model.to(__a ) if args.do_eval: model.eval() UpperCAmelCase_ , UpperCAmelCase_ : int = 0, 0 UpperCAmelCase_ , UpperCAmelCase_ : Any = 0, 0 for batch in tqdm(__a , desc='Evaluating' ): UpperCAmelCase_ : Tuple = tuple(t.to(__a ) for t in batch ) UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : Any = batch with torch.no_grad(): UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : int = model( __a , mc_token_ids=__a , lm_labels=__a , mc_labels=__a ) UpperCAmelCase_ : Dict = mc_logits.detach().cpu().numpy() UpperCAmelCase_ : Optional[Any] = mc_labels.to('cpu' ).numpy() UpperCAmelCase_ : Union[str, Any] = accuracy(__a , __a ) eval_loss += mc_loss.mean().item() eval_accuracy += tmp_eval_accuracy nb_eval_examples += input_ids.size(0 ) nb_eval_steps += 1 UpperCAmelCase_ : List[str] = eval_loss / nb_eval_steps UpperCAmelCase_ : List[Any] = eval_accuracy / nb_eval_examples UpperCAmelCase_ : Optional[Any] = tr_loss / nb_tr_steps if args.do_train else None UpperCAmelCase_ : Union[str, Any] = {'eval_loss': eval_loss, 'eval_accuracy': eval_accuracy, 'train_loss': train_loss} UpperCAmelCase_ : int = os.path.join(args.output_dir , 'eval_results.txt' ) with open(__a , 'w' ) as writer: logger.info('***** Eval results *****' ) for key in sorted(result.keys() ): logger.info(' %s = %s' , __a , str(result[key] ) ) writer.write('%s = %s\n' % (key, str(result[key] )) ) if __name__ == "__main__": main()
29
import argparse from argparse import Namespace import torch from torch import nn from transformers import XGLMConfig, XGLMForCausalLM def __lowerCamelCase ( __a :Dict ) -> Any: """simple docstring""" A__ = [ """decoder.version""", """decoder.output_projection.weight""", """_float_tensor""", """decoder.embed_positions._float_tensor""", ] for k in ignore_keys: state_dict.pop(__a , __a ) def __lowerCamelCase ( __a :str ) -> Union[str, Any]: """simple docstring""" A__ , A__ = emb.weight.shape A__ = nn.Linear(__a , __a , bias=__a ) A__ = emb.weight.data return lin_layer def __lowerCamelCase ( __a :str ) -> List[str]: """simple docstring""" A__ = torch.load(__a , map_location="""cpu""" ) A__ = Namespace(**checkpoint["""cfg"""]["""model"""] ) A__ = checkpoint["""model"""] remove_ignore_keys_(__a ) A__ = state_dict["""decoder.embed_tokens.weight"""].shape[0] A__ = {key.replace("""decoder""" , """model""" ): val for key, val in state_dict.items()} A__ = XGLMConfig( vocab_size=__a , max_position_embeddings=args.max_target_positions , num_layers=args.decoder_layers , attention_heads=args.decoder_attention_heads , ffn_dim=args.decoder_ffn_embed_dim , d_model=args.decoder_embed_dim , layerdrop=args.decoder_layerdrop , dropout=args.dropout , attention_dropout=args.attention_dropout , activation_dropout=args.activation_dropout , activation_function="""gelu""" , scale_embedding=not args.no_scale_embedding , tie_word_embeddings=args.share_decoder_input_output_embed , ) A__ = XGLMForCausalLM(__a ) A__ = model.load_state_dict(__a , strict=__a ) print(__a ) A__ = make_linear_from_emb(model.model.embed_tokens ) return model if __name__ == "__main__": A : int = argparse.ArgumentParser() # Required parameters parser.add_argument('''fairseq_path''', type=str, help='''path to a model.pt on local filesystem.''') parser.add_argument('''pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''') A : str = parser.parse_args() A : str = convert_fairseq_xglm_checkpoint_from_disk(args.fairseq_path) model.save_pretrained(args.pytorch_dump_folder_path)
274
0
def _a ( UpperCamelCase_ : int , UpperCamelCase_ : int ) -> float: """simple docstring""" return base * power(__a , (exponent - 1) ) if exponent else 1 if __name__ == "__main__": print('''Raise base to the power of exponent using recursion...''') a_ = int(input('''Enter the base: ''').strip()) a_ = int(input('''Enter the exponent: ''').strip()) a_ = power(base, abs(exponent)) if exponent < 0: # power() does not properly deal w/ negative exponents a_ = 1 / result print(F"{base} to the power of {exponent} is {result}")
340
import unittest import numpy as np from transformers import AlbertConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax.numpy as jnp from transformers.models.albert.modeling_flax_albert import ( FlaxAlbertForMaskedLM, FlaxAlbertForMultipleChoice, FlaxAlbertForPreTraining, FlaxAlbertForQuestionAnswering, FlaxAlbertForSequenceClassification, FlaxAlbertForTokenClassification, FlaxAlbertModel, ) class A (unittest.TestCase ): '''simple docstring''' def __init__( self : List[str] , __lowerCAmelCase : int , __lowerCAmelCase : List[str]=13 , __lowerCAmelCase : int=7 , __lowerCAmelCase : Tuple=True , __lowerCAmelCase : int=True , __lowerCAmelCase : Dict=True , __lowerCAmelCase : Union[str, Any]=True , __lowerCAmelCase : List[Any]=99 , __lowerCAmelCase : Optional[Any]=32 , __lowerCAmelCase : Optional[Any]=5 , __lowerCAmelCase : Tuple=4 , __lowerCAmelCase : Any=37 , __lowerCAmelCase : str="gelu" , __lowerCAmelCase : Optional[Any]=0.1 , __lowerCAmelCase : Union[str, Any]=0.1 , __lowerCAmelCase : List[str]=5_12 , __lowerCAmelCase : int=16 , __lowerCAmelCase : Optional[int]=2 , __lowerCAmelCase : List[Any]=0.0_2 , __lowerCAmelCase : Tuple=4 , ) -> Dict: """simple docstring""" A__ = parent A__ = batch_size A__ = seq_length A__ = is_training A__ = use_attention_mask A__ = use_token_type_ids A__ = use_labels A__ = vocab_size A__ = hidden_size A__ = num_hidden_layers A__ = num_attention_heads A__ = intermediate_size A__ = hidden_act A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = max_position_embeddings A__ = type_vocab_size A__ = type_sequence_label_size A__ = initializer_range A__ = num_choices def a_ ( self : Any ) -> str: """simple docstring""" A__ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) A__ = None if self.use_attention_mask: A__ = random_attention_mask([self.batch_size, self.seq_length] ) A__ = None if self.use_token_type_ids: A__ = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) A__ = AlbertConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=__lowerCAmelCase , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def a_ ( self : Optional[int] ) -> Optional[int]: """simple docstring""" A__ = self.prepare_config_and_inputs() A__ , A__ , A__ , A__ = config_and_inputs A__ = {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": attention_mask} return config, inputs_dict @require_flax class A (SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : str = ( ( FlaxAlbertModel, FlaxAlbertForPreTraining, FlaxAlbertForMaskedLM, FlaxAlbertForMultipleChoice, FlaxAlbertForQuestionAnswering, FlaxAlbertForSequenceClassification, FlaxAlbertForTokenClassification, FlaxAlbertForQuestionAnswering, ) if is_flax_available() else () ) def a_ ( self : str ) -> Optional[int]: """simple docstring""" A__ = FlaxAlbertModelTester(self ) @slow def a_ ( self : int ) -> Tuple: """simple docstring""" for model_class_name in self.all_model_classes: A__ = model_class_name.from_pretrained("""albert-base-v2""" ) A__ = model(np.ones((1, 1) ) ) self.assertIsNotNone(__lowerCAmelCase ) @require_flax class A (unittest.TestCase ): '''simple docstring''' @slow def a_ ( self : Dict ) -> List[Any]: """simple docstring""" A__ = FlaxAlbertModel.from_pretrained("""albert-base-v2""" ) A__ = np.array([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]] ) A__ = np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) A__ = model(__lowerCAmelCase , attention_mask=__lowerCAmelCase )[0] A__ = (1, 11, 7_68) self.assertEqual(output.shape , __lowerCAmelCase ) A__ = np.array( [[[-0.6_5_1_3, 1.5_0_3_5, -0.2_7_6_6], [-0.6_5_1_5, 1.5_0_4_6, -0.2_7_8_0], [-0.6_5_1_2, 1.5_0_4_9, -0.2_7_8_4]]] ) self.assertTrue(jnp.allclose(output[:, 1:4, 1:4] , __lowerCAmelCase , atol=1e-4 ) )
274
0
from collections import deque class lowercase_ : def __init__( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): """simple docstring""" UpperCamelCase_ = process_name # process name UpperCamelCase_ = arrival_time # arrival time of the process # completion time of finished process or last interrupted time UpperCamelCase_ = arrival_time UpperCamelCase_ = burst_time # remaining burst time UpperCamelCase_ = 0 # total time of the process wait in ready queue UpperCamelCase_ = 0 # time from arrival time to completion time class lowercase_ : def __init__( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , ): """simple docstring""" UpperCamelCase_ = number_of_queues # time slice of queues that round robin algorithm applied UpperCamelCase_ = time_slices # unfinished process is in this ready_queue UpperCamelCase_ = queue # current time UpperCamelCase_ = current_time # finished process is in this sequence queue UpperCamelCase_ = deque() def lowerCamelCase_ ( self ): """simple docstring""" UpperCamelCase_ = [] for i in range(len(self.finish_queue ) ): sequence.append(self.finish_queue[i].process_name ) return sequence def lowerCamelCase_ ( self , __UpperCamelCase ): """simple docstring""" UpperCamelCase_ = [] for i in range(len(__lowerCAmelCase ) ): waiting_times.append(queue[i].waiting_time ) return waiting_times def lowerCamelCase_ ( self , __UpperCamelCase ): """simple docstring""" UpperCamelCase_ = [] for i in range(len(__lowerCAmelCase ) ): turnaround_times.append(queue[i].turnaround_time ) return turnaround_times def lowerCamelCase_ ( self , __UpperCamelCase ): """simple docstring""" UpperCamelCase_ = [] for i in range(len(__lowerCAmelCase ) ): completion_times.append(queue[i].stop_time ) return completion_times def lowerCamelCase_ ( self , __UpperCamelCase ): """simple docstring""" return [q.burst_time for q in queue] def lowerCamelCase_ ( self , __UpperCamelCase ): """simple docstring""" process.waiting_time += self.current_time - process.stop_time return process.waiting_time def lowerCamelCase_ ( self , __UpperCamelCase ): """simple docstring""" UpperCamelCase_ = deque() # sequence deque of finished process while len(__lowerCAmelCase ) != 0: UpperCamelCase_ = ready_queue.popleft() # current process # if process's arrival time is later than current time, update current time if self.current_time < cp.arrival_time: self.current_time += cp.arrival_time # update waiting time of current process self.update_waiting_time(__lowerCAmelCase ) # update current time self.current_time += cp.burst_time # finish the process and set the process's burst-time 0 UpperCamelCase_ = 0 # set the process's turnaround time because it is finished UpperCamelCase_ = self.current_time - cp.arrival_time # set the completion time UpperCamelCase_ = self.current_time # add the process to queue that has finished queue finished.append(__lowerCAmelCase ) self.finish_queue.extend(__lowerCAmelCase ) # add finished process to finish queue # FCFS will finish all remaining processes return finished def lowerCamelCase_ ( self , __UpperCamelCase , __UpperCamelCase ): """simple docstring""" UpperCamelCase_ = deque() # sequence deque of terminated process # just for 1 cycle and unfinished processes will go back to queue for _ in range(len(__lowerCAmelCase ) ): UpperCamelCase_ = ready_queue.popleft() # current process # if process's arrival time is later than current time, update current time if self.current_time < cp.arrival_time: self.current_time += cp.arrival_time # update waiting time of unfinished processes self.update_waiting_time(__lowerCAmelCase ) # if the burst time of process is bigger than time-slice if cp.burst_time > time_slice: # use CPU for only time-slice self.current_time += time_slice # update remaining burst time cp.burst_time -= time_slice # update end point time UpperCamelCase_ = self.current_time # locate the process behind the queue because it is not finished ready_queue.append(__lowerCAmelCase ) else: # use CPU for remaining burst time self.current_time += cp.burst_time # set burst time 0 because the process is finished UpperCamelCase_ = 0 # set the finish time UpperCamelCase_ = self.current_time # update the process' turnaround time because it is finished UpperCamelCase_ = self.current_time - cp.arrival_time # add the process to queue that has finished queue finished.append(__lowerCAmelCase ) self.finish_queue.extend(__lowerCAmelCase ) # add finished process to finish queue # return finished processes queue and remaining processes queue return finished, ready_queue def lowerCamelCase_ ( self ): """simple docstring""" for i in range(self.number_of_queues - 1 ): UpperCamelCase_ , UpperCamelCase_ = self.round_robin( self.ready_queue , self.time_slices[i] ) # the last queue has first_come_first_served algorithm self.first_come_first_served(self.ready_queue ) return self.finish_queue if __name__ == "__main__": import doctest _A = Process('''P1''', 0, 53) _A = Process('''P2''', 0, 17) _A = Process('''P3''', 0, 68) _A = Process('''P4''', 0, 24) _A = 3 _A = [17, 25] _A = deque([Pa, Pa, Pa, Pa]) if len(time_slices) != number_of_queues - 1: raise SystemExit(0) doctest.testmod(extraglobs={'''queue''': deque([Pa, Pa, Pa, Pa])}) _A = Process('''P1''', 0, 53) _A = Process('''P2''', 0, 17) _A = Process('''P3''', 0, 68) _A = Process('''P4''', 0, 24) _A = 3 _A = [17, 25] _A = deque([Pa, Pa, Pa, Pa]) _A = MLFQ(number_of_queues, time_slices, queue, 0) _A = mlfq.multi_level_feedback_queue() # print total waiting times of processes(P1, P2, P3, P4) print( F'''waiting time:\ \t\t\t{MLFQ.calculate_waiting_time(mlfq, [Pa, Pa, Pa, Pa])}''' ) # print completion times of processes(P1, P2, P3, P4) print( F'''completion time:\ \t\t{MLFQ.calculate_completion_time(mlfq, [Pa, Pa, Pa, Pa])}''' ) # print total turnaround times of processes(P1, P2, P3, P4) print( F'''turnaround time:\ \t\t{MLFQ.calculate_turnaround_time(mlfq, [Pa, Pa, Pa, Pa])}''' ) # print sequence of finished processes print( F'''sequence of finished processes:\ {mlfq.calculate_sequence_of_finish_queue()}''' )
122
from sklearn.metrics import fa_score import datasets A : Any = ''' The F1 score is the harmonic mean of the precision and recall. It can be computed with the equation: F1 = 2 * (precision * recall) / (precision + recall) ''' A : List[Any] = ''' Args: predictions (`list` of `int`): Predicted labels. references (`list` of `int`): Ground truth labels. labels (`list` of `int`): The set of labels to include when `average` is not set to `\'binary\'`, and the order of the labels if `average` is `None`. Labels present in the data can be excluded, for example to calculate a multiclass average ignoring a majority negative class. Labels not present in the data will result in 0 components in a macro average. For multilabel targets, labels are column indices. By default, all labels in `predictions` and `references` are used in sorted order. Defaults to None. pos_label (`int`): The class to be considered the positive class, in the case where `average` is set to `binary`. Defaults to 1. average (`string`): This parameter is required for multiclass/multilabel targets. If set to `None`, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data. Defaults to `\'binary\'`. - \'binary\': Only report results for the class specified by `pos_label`. This is applicable only if the classes found in `predictions` and `references` are binary. - \'micro\': Calculate metrics globally by counting the total true positives, false negatives and false positives. - \'macro\': Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account. - \'weighted\': Calculate metrics for each label, and find their average weighted by support (the number of true instances for each label). This alters `\'macro\'` to account for label imbalance. This option can result in an F-score that is not between precision and recall. - \'samples\': Calculate metrics for each instance, and find their average (only meaningful for multilabel classification). sample_weight (`list` of `float`): Sample weights Defaults to None. Returns: f1 (`float` or `array` of `float`): F1 score or list of f1 scores, depending on the value passed to `average`. Minimum possible value is 0. Maximum possible value is 1. Higher f1 scores are better. Examples: Example 1-A simple binary example >>> f1_metric = datasets.load_metric("f1") >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0]) >>> print(results) {\'f1\': 0.5} Example 2-The same simple binary example as in Example 1, but with `pos_label` set to `0`. >>> f1_metric = datasets.load_metric("f1") >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0], pos_label=0) >>> print(round(results[\'f1\'], 2)) 0.67 Example 3-The same simple binary example as in Example 1, but with `sample_weight` included. >>> f1_metric = datasets.load_metric("f1") >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0], sample_weight=[0.9, 0.5, 3.9, 1.2, 0.3]) >>> print(round(results[\'f1\'], 2)) 0.35 Example 4-A multiclass example, with different values for the `average` input. >>> predictions = [0, 2, 1, 0, 0, 1] >>> references = [0, 1, 2, 0, 1, 2] >>> results = f1_metric.compute(predictions=predictions, references=references, average="macro") >>> print(round(results[\'f1\'], 2)) 0.27 >>> results = f1_metric.compute(predictions=predictions, references=references, average="micro") >>> print(round(results[\'f1\'], 2)) 0.33 >>> results = f1_metric.compute(predictions=predictions, references=references, average="weighted") >>> print(round(results[\'f1\'], 2)) 0.27 >>> results = f1_metric.compute(predictions=predictions, references=references, average=None) >>> print(results) {\'f1\': array([0.8, 0. , 0. ])} ''' A : List[Any] = ''' @article{scikit-learn, title={Scikit-learn: Machine Learning in {P}ython}, author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V. and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P. and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.}, journal={Journal of Machine Learning Research}, volume={12}, pages={2825--2830}, year={2011} } ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class A (datasets.Metric ): '''simple docstring''' def a_ ( self : Optional[int] ) -> List[Any]: """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": datasets.Sequence(datasets.Value("""int32""" ) ), """references""": datasets.Sequence(datasets.Value("""int32""" ) ), } if self.config_name == """multilabel""" else { """predictions""": datasets.Value("""int32""" ), """references""": datasets.Value("""int32""" ), } ) , reference_urls=["""https://scikit-learn.org/stable/modules/generated/sklearn.metrics.f1_score.html"""] , ) def a_ ( self : Any , __lowerCAmelCase : Tuple , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : Dict=None , __lowerCAmelCase : List[str]=1 , __lowerCAmelCase : Any="binary" , __lowerCAmelCase : Optional[int]=None ) -> List[Any]: """simple docstring""" A__ = fa_score( __lowerCAmelCase , __lowerCAmelCase , labels=__lowerCAmelCase , pos_label=__lowerCAmelCase , average=__lowerCAmelCase , sample_weight=__lowerCAmelCase ) return {"f1": float(__lowerCAmelCase ) if score.size == 1 else score}
274
0
def UpperCAmelCase_ ( __snake_case , __snake_case ) -> int: """simple docstring""" return x if y == 0 else greatest_common_divisor(__a , x % y ) def UpperCAmelCase_ ( __snake_case , __snake_case ) -> int: """simple docstring""" return (x * y) // greatest_common_divisor(__a , __a ) def UpperCAmelCase_ ( __snake_case = 20 ) -> int: """simple docstring""" _lowercase =1 for i in range(1 , n + 1 ): _lowercase =lcm(__a , __a ) return g if __name__ == "__main__": print(f'''{solution() = }''')
5
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging A : Union[str, Any] = logging.get_logger(__name__) A : int = { '''xlm-roberta-base''': '''https://huggingface.co/xlm-roberta-base/resolve/main/config.json''', '''xlm-roberta-large''': '''https://huggingface.co/xlm-roberta-large/resolve/main/config.json''', '''xlm-roberta-large-finetuned-conll02-dutch''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll02-dutch/resolve/main/config.json''' ), '''xlm-roberta-large-finetuned-conll02-spanish''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll02-spanish/resolve/main/config.json''' ), '''xlm-roberta-large-finetuned-conll03-english''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll03-english/resolve/main/config.json''' ), '''xlm-roberta-large-finetuned-conll03-german''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll03-german/resolve/main/config.json''' ), } class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : Any = '''xlm-roberta''' def __init__( self : Optional[Any] , __lowerCAmelCase : List[Any]=3_05_22 , __lowerCAmelCase : int=7_68 , __lowerCAmelCase : Tuple=12 , __lowerCAmelCase : str=12 , __lowerCAmelCase : Union[str, Any]=30_72 , __lowerCAmelCase : Union[str, Any]="gelu" , __lowerCAmelCase : Optional[int]=0.1 , __lowerCAmelCase : Dict=0.1 , __lowerCAmelCase : List[str]=5_12 , __lowerCAmelCase : Tuple=2 , __lowerCAmelCase : Dict=0.0_2 , __lowerCAmelCase : List[str]=1e-12 , __lowerCAmelCase : Union[str, Any]=1 , __lowerCAmelCase : str=0 , __lowerCAmelCase : Optional[int]=2 , __lowerCAmelCase : Tuple="absolute" , __lowerCAmelCase : Any=True , __lowerCAmelCase : Any=None , **__lowerCAmelCase : str , ) -> Optional[Any]: """simple docstring""" super().__init__(pad_token_id=__lowerCAmelCase , bos_token_id=__lowerCAmelCase , eos_token_id=__lowerCAmelCase , **__lowerCAmelCase ) A__ = vocab_size A__ = hidden_size A__ = num_hidden_layers A__ = num_attention_heads A__ = hidden_act A__ = intermediate_size A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = max_position_embeddings A__ = type_vocab_size A__ = initializer_range A__ = layer_norm_eps A__ = position_embedding_type A__ = use_cache A__ = classifier_dropout class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' @property def a_ ( self : int ) -> Mapping[str, Mapping[int, str]]: """simple docstring""" if self.task == "multiple-choice": A__ = {0: """batch""", 1: """choice""", 2: """sequence"""} else: A__ = {0: """batch""", 1: """sequence"""} return OrderedDict( [ ("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ] )
274
0
'''simple docstring''' from math import ceil def __a(SCREAMING_SNAKE_CASE_ : int = 1001 ): '''simple docstring''' _lowerCAmelCase = 1 for i in range(1 , int(ceil(n / 2.0 ) ) ): _lowerCAmelCase = 2 * i + 1 _lowerCAmelCase = 2 * i _lowerCAmelCase = total + 4 * odd**2 - 6 * even return total if __name__ == "__main__": import sys if len(sys.argv) == 1: print(solution()) else: try: _SCREAMING_SNAKE_CASE = int(sys.argv[1]) print(solution(n)) except ValueError: print("Invalid entry - please enter a number")
158
from __future__ import annotations import time from math import sqrt # 1 for manhattan, 0 for euclidean A : str = 0 A : Any = [ [0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles [0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0], [1, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0], ] A : Dict = [[-1, 0], [0, -1], [1, 0], [0, 1]] # up, left, down, right A : Union[str, Any] = tuple[int, int] class A : '''simple docstring''' def __init__( self : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : Node | None , ) -> None: """simple docstring""" A__ = pos_x A__ = pos_y A__ = (pos_y, pos_x) A__ = goal_x A__ = goal_y A__ = g_cost A__ = parent A__ = self.calculate_heuristic() A__ = self.g_cost + self.h_cost def a_ ( self : Dict ) -> float: """simple docstring""" A__ = self.pos_x - self.goal_x A__ = self.pos_y - self.goal_y if HEURISTIC == 1: return abs(__lowerCAmelCase ) + abs(__lowerCAmelCase ) else: return sqrt(dy**2 + dx**2 ) def __lt__( self : int , __lowerCAmelCase : Node ) -> bool: """simple docstring""" return self.f_cost < other.f_cost class A : '''simple docstring''' def __init__( self : Union[str, Any] , __lowerCAmelCase : TPosition , __lowerCAmelCase : TPosition ) -> Tuple: """simple docstring""" A__ = Node(start[1] , start[0] , goal[1] , goal[0] , 0 , __lowerCAmelCase ) A__ = Node(goal[1] , goal[0] , goal[1] , goal[0] , 9_99_99 , __lowerCAmelCase ) A__ = [self.start] A__ = [] A__ = False def a_ ( self : List[str] ) -> list[TPosition]: """simple docstring""" while self.open_nodes: # Open Nodes are sorted using __lt__ self.open_nodes.sort() A__ = self.open_nodes.pop(0 ) if current_node.pos == self.target.pos: return self.retrace_path(__lowerCAmelCase ) self.closed_nodes.append(__lowerCAmelCase ) A__ = self.get_successors(__lowerCAmelCase ) for child_node in successors: if child_node in self.closed_nodes: continue if child_node not in self.open_nodes: self.open_nodes.append(__lowerCAmelCase ) else: # retrieve the best current path A__ = self.open_nodes.pop(self.open_nodes.index(__lowerCAmelCase ) ) if child_node.g_cost < better_node.g_cost: self.open_nodes.append(__lowerCAmelCase ) else: self.open_nodes.append(__lowerCAmelCase ) return [self.start.pos] def a_ ( self : Optional[Any] , __lowerCAmelCase : Node ) -> list[Node]: """simple docstring""" A__ = [] for action in delta: A__ = parent.pos_x + action[1] A__ = parent.pos_y + action[0] if not (0 <= pos_x <= len(grid[0] ) - 1 and 0 <= pos_y <= len(__lowerCAmelCase ) - 1): continue if grid[pos_y][pos_x] != 0: continue successors.append( Node( __lowerCAmelCase , __lowerCAmelCase , self.target.pos_y , self.target.pos_x , parent.g_cost + 1 , __lowerCAmelCase , ) ) return successors def a_ ( self : List[Any] , __lowerCAmelCase : Node | None ) -> list[TPosition]: """simple docstring""" A__ = node A__ = [] while current_node is not None: path.append((current_node.pos_y, current_node.pos_x) ) A__ = current_node.parent path.reverse() return path class A : '''simple docstring''' def __init__( self : Optional[Any] , __lowerCAmelCase : TPosition , __lowerCAmelCase : TPosition ) -> None: """simple docstring""" A__ = AStar(__lowerCAmelCase , __lowerCAmelCase ) A__ = AStar(__lowerCAmelCase , __lowerCAmelCase ) A__ = False def a_ ( self : int ) -> list[TPosition]: """simple docstring""" while self.fwd_astar.open_nodes or self.bwd_astar.open_nodes: self.fwd_astar.open_nodes.sort() self.bwd_astar.open_nodes.sort() A__ = self.fwd_astar.open_nodes.pop(0 ) A__ = self.bwd_astar.open_nodes.pop(0 ) if current_bwd_node.pos == current_fwd_node.pos: return self.retrace_bidirectional_path( __lowerCAmelCase , __lowerCAmelCase ) self.fwd_astar.closed_nodes.append(__lowerCAmelCase ) self.bwd_astar.closed_nodes.append(__lowerCAmelCase ) A__ = current_bwd_node A__ = current_fwd_node A__ = { self.fwd_astar: self.fwd_astar.get_successors(__lowerCAmelCase ), self.bwd_astar: self.bwd_astar.get_successors(__lowerCAmelCase ), } for astar in [self.fwd_astar, self.bwd_astar]: for child_node in successors[astar]: if child_node in astar.closed_nodes: continue if child_node not in astar.open_nodes: astar.open_nodes.append(__lowerCAmelCase ) else: # retrieve the best current path A__ = astar.open_nodes.pop( astar.open_nodes.index(__lowerCAmelCase ) ) if child_node.g_cost < better_node.g_cost: astar.open_nodes.append(__lowerCAmelCase ) else: astar.open_nodes.append(__lowerCAmelCase ) return [self.fwd_astar.start.pos] def a_ ( self : List[str] , __lowerCAmelCase : Node , __lowerCAmelCase : Node ) -> list[TPosition]: """simple docstring""" A__ = self.fwd_astar.retrace_path(__lowerCAmelCase ) A__ = self.bwd_astar.retrace_path(__lowerCAmelCase ) bwd_path.pop() bwd_path.reverse() A__ = fwd_path + bwd_path return path if __name__ == "__main__": # all coordinates are given in format [y,x] A : Optional[int] = (0, 0) A : int = (len(grid) - 1, len(grid[0]) - 1) for elem in grid: print(elem) A : Dict = time.time() A : Optional[Any] = AStar(init, goal) A : Optional[int] = a_star.search() A : Optional[int] = time.time() - start_time print(F'''AStar execution time = {end_time:f} seconds''') A : Dict = time.time() A : Tuple = BidirectionalAStar(init, goal) A : List[Any] = time.time() - bd_start_time print(F'''BidirectionalAStar execution time = {bd_end_time:f} seconds''')
274
0
import unittest from parameterized import parameterized from transformers import OpenLlamaConfig, is_torch_available, set_seed from transformers.testing_utils import require_torch, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import OpenLlamaForCausalLM, OpenLlamaForSequenceClassification, OpenLlamaModel class __A : '''simple docstring''' def __init__( self , __lowerCAmelCase , __lowerCAmelCase=1_3 , __lowerCAmelCase=7 , __lowerCAmelCase=True , __lowerCAmelCase=True , __lowerCAmelCase=False , __lowerCAmelCase=True , __lowerCAmelCase=9_9 , __lowerCAmelCase=3_2 , __lowerCAmelCase=5 , __lowerCAmelCase=4 , __lowerCAmelCase=3_7 , __lowerCAmelCase="gelu" , __lowerCAmelCase=0.1 , __lowerCAmelCase=0.1 , __lowerCAmelCase=5_1_2 , __lowerCAmelCase=1_6 , __lowerCAmelCase=2 , __lowerCAmelCase=0.02 , __lowerCAmelCase=3 , __lowerCAmelCase=4 , __lowerCAmelCase=None , ): '''simple docstring''' lowerCamelCase__ = parent lowerCamelCase__ = batch_size lowerCamelCase__ = seq_length lowerCamelCase__ = is_training lowerCamelCase__ = use_input_mask lowerCamelCase__ = use_token_type_ids lowerCamelCase__ = use_labels lowerCamelCase__ = vocab_size lowerCamelCase__ = hidden_size lowerCamelCase__ = num_hidden_layers lowerCamelCase__ = num_attention_heads lowerCamelCase__ = intermediate_size lowerCamelCase__ = hidden_act lowerCamelCase__ = hidden_dropout_prob lowerCamelCase__ = attention_probs_dropout_prob lowerCamelCase__ = max_position_embeddings lowerCamelCase__ = type_vocab_size lowerCamelCase__ = type_sequence_label_size lowerCamelCase__ = initializer_range lowerCamelCase__ = num_labels lowerCamelCase__ = num_choices lowerCamelCase__ = scope def __lowerCamelCase ( self ): '''simple docstring''' lowerCamelCase__ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) lowerCamelCase__ = None if self.use_input_mask: lowerCamelCase__ = random_attention_mask([self.batch_size, self.seq_length] ) lowerCamelCase__ = None if self.use_token_type_ids: lowerCamelCase__ = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) lowerCamelCase__ = None lowerCamelCase__ = None lowerCamelCase__ = None if self.use_labels: lowerCamelCase__ = ids_tensor([self.batch_size] , self.type_sequence_label_size ) lowerCamelCase__ = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) lowerCamelCase__ = ids_tensor([self.batch_size] , self.num_choices ) lowerCamelCase__ = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def __lowerCamelCase ( self ): '''simple docstring''' return OpenLlamaConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=__lowerCAmelCase , initializer_range=self.initializer_range , use_stable_embedding=__lowerCAmelCase , ) def __lowerCamelCase ( self , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ): '''simple docstring''' lowerCamelCase__ = OpenLlamaModel(config=__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.eval() lowerCamelCase__ = model(__lowerCAmelCase , attention_mask=__lowerCAmelCase ) lowerCamelCase__ = model(__lowerCAmelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __lowerCamelCase ( self , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , ): '''simple docstring''' lowerCamelCase__ = True lowerCamelCase__ = OpenLlamaModel(__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.eval() lowerCamelCase__ = model( __lowerCAmelCase , attention_mask=__lowerCAmelCase , encoder_hidden_states=__lowerCAmelCase , encoder_attention_mask=__lowerCAmelCase , ) lowerCamelCase__ = model( __lowerCAmelCase , attention_mask=__lowerCAmelCase , encoder_hidden_states=__lowerCAmelCase , ) lowerCamelCase__ = model(__lowerCAmelCase , attention_mask=__lowerCAmelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __lowerCamelCase ( self , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , ): '''simple docstring''' lowerCamelCase__ = OpenLlamaForCausalLM(config=__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.eval() lowerCamelCase__ = model(__lowerCAmelCase , attention_mask=__lowerCAmelCase , labels=__lowerCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def __lowerCamelCase ( self , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , ): '''simple docstring''' lowerCamelCase__ = True lowerCamelCase__ = True lowerCamelCase__ = OpenLlamaForCausalLM(config=__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.eval() # first forward pass lowerCamelCase__ = model( __lowerCAmelCase , attention_mask=__lowerCAmelCase , encoder_hidden_states=__lowerCAmelCase , encoder_attention_mask=__lowerCAmelCase , use_cache=__lowerCAmelCase , ) lowerCamelCase__ = outputs.past_key_values # create hypothetical multiple next token and extent to next_input_ids lowerCamelCase__ = ids_tensor((self.batch_size, 3) , config.vocab_size ) lowerCamelCase__ = ids_tensor((self.batch_size, 3) , vocab_size=2 ) # append to next input_ids and lowerCamelCase__ = torch.cat([input_ids, next_tokens] , dim=-1 ) lowerCamelCase__ = torch.cat([input_mask, next_mask] , dim=-1 ) lowerCamelCase__ = model( __lowerCAmelCase , attention_mask=__lowerCAmelCase , encoder_hidden_states=__lowerCAmelCase , encoder_attention_mask=__lowerCAmelCase , output_hidden_states=__lowerCAmelCase , )['''hidden_states'''][0] lowerCamelCase__ = model( __lowerCAmelCase , attention_mask=__lowerCAmelCase , encoder_hidden_states=__lowerCAmelCase , encoder_attention_mask=__lowerCAmelCase , past_key_values=__lowerCAmelCase , output_hidden_states=__lowerCAmelCase , )['''hidden_states'''][0] # select random slice lowerCamelCase__ = ids_tensor((1,) , output_from_past.shape[-1] ).item() lowerCamelCase__ = output_from_no_past[:, -3:, random_slice_idx].detach() lowerCamelCase__ = output_from_past[:, :, random_slice_idx].detach() self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1] ) # test that outputs are equal for slice self.parent.assertTrue(torch.allclose(__lowerCAmelCase , __lowerCAmelCase , atol=1E-3 ) ) def __lowerCamelCase ( self ): '''simple docstring''' lowerCamelCase__ = self.prepare_config_and_inputs() ( ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ( lowerCamelCase__ ) , ) = config_and_inputs lowerCamelCase__ = {'''input_ids''': input_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_torch class __A ( lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , unittest.TestCase ): '''simple docstring''' lowerCAmelCase_ = ( (OpenLlamaModel, OpenLlamaForCausalLM, OpenLlamaForSequenceClassification) if is_torch_available() else () ) lowerCAmelCase_ = (OpenLlamaForCausalLM,) if is_torch_available() else () lowerCAmelCase_ = ( { '''feature-extraction''': OpenLlamaModel, '''text-classification''': OpenLlamaForSequenceClassification, '''text-generation''': OpenLlamaForCausalLM, '''zero-shot''': OpenLlamaForSequenceClassification, } if is_torch_available() else {} ) lowerCAmelCase_ = False lowerCAmelCase_ = False def __lowerCamelCase ( self ): '''simple docstring''' lowerCamelCase__ = OpenLlamaModelTester(self ) lowerCamelCase__ = ConfigTester(self , config_class=__lowerCAmelCase , hidden_size=3_7 ) def __lowerCamelCase ( self ): '''simple docstring''' self.config_tester.run_common_tests() def __lowerCamelCase ( self ): '''simple docstring''' lowerCamelCase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__lowerCAmelCase ) def __lowerCamelCase ( self ): '''simple docstring''' lowerCamelCase__ = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: lowerCamelCase__ = type self.model_tester.create_and_check_model(*__lowerCAmelCase ) def __lowerCamelCase ( self ): '''simple docstring''' lowerCamelCase__ , lowerCamelCase__ = self.model_tester.prepare_config_and_inputs_for_common() lowerCamelCase__ = 3 lowerCamelCase__ = input_dict['''input_ids'''] lowerCamelCase__ = input_ids.ne(1 ).to(__lowerCAmelCase ) lowerCamelCase__ = ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size ) lowerCamelCase__ = OpenLlamaForSequenceClassification(__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.eval() lowerCamelCase__ = model(__lowerCAmelCase , attention_mask=__lowerCAmelCase , labels=__lowerCAmelCase ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def __lowerCamelCase ( self ): '''simple docstring''' lowerCamelCase__ , lowerCamelCase__ = self.model_tester.prepare_config_and_inputs_for_common() lowerCamelCase__ = 3 lowerCamelCase__ = '''single_label_classification''' lowerCamelCase__ = input_dict['''input_ids'''] lowerCamelCase__ = input_ids.ne(1 ).to(__lowerCAmelCase ) lowerCamelCase__ = ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size ) lowerCamelCase__ = OpenLlamaForSequenceClassification(__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.eval() lowerCamelCase__ = model(__lowerCAmelCase , attention_mask=__lowerCAmelCase , labels=__lowerCAmelCase ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def __lowerCamelCase ( self ): '''simple docstring''' lowerCamelCase__ , lowerCamelCase__ = self.model_tester.prepare_config_and_inputs_for_common() lowerCamelCase__ = 3 lowerCamelCase__ = '''multi_label_classification''' lowerCamelCase__ = input_dict['''input_ids'''] lowerCamelCase__ = input_ids.ne(1 ).to(__lowerCAmelCase ) lowerCamelCase__ = ids_tensor( [self.model_tester.batch_size, config.num_labels] , self.model_tester.type_sequence_label_size ).to(torch.float ) lowerCamelCase__ = OpenLlamaForSequenceClassification(__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.eval() lowerCamelCase__ = model(__lowerCAmelCase , attention_mask=__lowerCAmelCase , labels=__lowerCAmelCase ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) @unittest.skip('''Open-Llama buffers include complex numbers, which breaks this test''' ) def __lowerCamelCase ( self ): '''simple docstring''' pass @parameterized.expand([('''linear''',), ('''dynamic''',)] ) def __lowerCamelCase ( self , __lowerCAmelCase ): '''simple docstring''' lowerCamelCase__ , lowerCamelCase__ = self.model_tester.prepare_config_and_inputs_for_common() lowerCamelCase__ = ids_tensor([1, 1_0] , config.vocab_size ) lowerCamelCase__ = ids_tensor([1, int(config.max_position_embeddings * 1.5 )] , config.vocab_size ) set_seed(4_2 ) # Fixed seed at init time so the two models get the same random weights lowerCamelCase__ = OpenLlamaModel(__lowerCAmelCase ) original_model.to(__lowerCAmelCase ) original_model.eval() lowerCamelCase__ = original_model(__lowerCAmelCase ).last_hidden_state lowerCamelCase__ = original_model(__lowerCAmelCase ).last_hidden_state set_seed(4_2 ) # Fixed seed at init time so the two models get the same random weights lowerCamelCase__ = {'''type''': scaling_type, '''factor''': 1_0.0} lowerCamelCase__ = OpenLlamaModel(__lowerCAmelCase ) scaled_model.to(__lowerCAmelCase ) scaled_model.eval() lowerCamelCase__ = scaled_model(__lowerCAmelCase ).last_hidden_state lowerCamelCase__ = scaled_model(__lowerCAmelCase ).last_hidden_state # Dynamic scaling does not change the RoPE embeddings until it receives an input longer than the original # maximum sequence length, so the outputs for the short input should match. if scaling_type == "dynamic": self.assertTrue(torch.allclose(__lowerCAmelCase , __lowerCAmelCase , atol=1E-5 ) ) else: self.assertFalse(torch.allclose(__lowerCAmelCase , __lowerCAmelCase , atol=1E-5 ) ) # The output should be different for long inputs self.assertFalse(torch.allclose(__lowerCAmelCase , __lowerCAmelCase , atol=1E-5 ) )
209
import unittest from transformers.utils.backbone_utils import ( BackboneMixin, get_aligned_output_features_output_indices, verify_out_features_out_indices, ) class A (unittest.TestCase ): '''simple docstring''' def a_ ( self : Any ) -> Union[str, Any]: """simple docstring""" A__ = ["""a""", """b""", """c"""] # Defaults to last layer if both are None A__ , A__ = get_aligned_output_features_output_indices(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , ["""c"""] ) self.assertEqual(__lowerCAmelCase , [2] ) # Out indices set to match out features A__ , A__ = get_aligned_output_features_output_indices(["""a""", """c"""] , __lowerCAmelCase , __lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , ["""a""", """c"""] ) self.assertEqual(__lowerCAmelCase , [0, 2] ) # Out features set to match out indices A__ , A__ = get_aligned_output_features_output_indices(__lowerCAmelCase , [0, 2] , __lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , ["""a""", """c"""] ) self.assertEqual(__lowerCAmelCase , [0, 2] ) # Out features selected from negative indices A__ , A__ = get_aligned_output_features_output_indices(__lowerCAmelCase , [-3, -1] , __lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , ["""a""", """c"""] ) self.assertEqual(__lowerCAmelCase , [-3, -1] ) def a_ ( self : Optional[Any] ) -> Optional[Any]: """simple docstring""" with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(["""a""", """b"""] , (0, 1) , __lowerCAmelCase ) # Out features must be a list with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(("""a""", """b""") , (0, 1) , ["""a""", """b"""] ) # Out features must be a subset of stage names with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(["""a""", """b"""] , (0, 1) , ["""a"""] ) # Out indices must be a list or tuple with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(__lowerCAmelCase , 0 , ["""a""", """b"""] ) # Out indices must be a subset of stage names with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(__lowerCAmelCase , (0, 1) , ["""a"""] ) # Out features and out indices must be the same length with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(["""a""", """b"""] , (0,) , ["""a""", """b""", """c"""] ) # Out features should match out indices with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(["""a""", """b"""] , (0, 2) , ["""a""", """b""", """c"""] ) # Out features and out indices should be in order with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(["""b""", """a"""] , (0, 1) , ["""a""", """b"""] ) # Check passes with valid inputs verify_out_features_out_indices(["""a""", """b""", """d"""] , (0, 1, -1) , ["""a""", """b""", """c""", """d"""] ) def a_ ( self : Union[str, Any] ) -> List[str]: """simple docstring""" A__ = BackboneMixin() A__ = ["""a""", """b""", """c"""] A__ = ["""a""", """c"""] A__ = [0, 2] # Check that the output features and indices are set correctly self.assertEqual(backbone.out_features , ["""a""", """c"""] ) self.assertEqual(backbone.out_indices , [0, 2] ) # Check out features and indices are updated correctly A__ = ["""a""", """b"""] self.assertEqual(backbone.out_features , ["""a""", """b"""] ) self.assertEqual(backbone.out_indices , [0, 1] ) A__ = [-3, -1] self.assertEqual(backbone.out_features , ["""a""", """c"""] ) self.assertEqual(backbone.out_indices , [-3, -1] )
274
0
import datasets import faiss import numpy as np import streamlit as st import torch from elasticsearch import Elasticsearch from elia_utils import ( embed_questions_for_retrieval, make_qa_sas_model, qa_sas_generate, query_es_index, query_qa_dense_index, ) import transformers from transformers import AutoModel, AutoModelForSeqaSeqLM, AutoTokenizer A : Optional[Any] = '''bart''' A : Optional[int] = True @st.cache(allow_output_mutation=__a ) def lowercase_ ( ): """simple docstring""" if LOAD_DENSE_INDEX: lowerCamelCase__ : List[str] = AutoTokenizer.from_pretrained("yjernite/retribert-base-uncased" ) lowerCamelCase__ : List[str] = AutoModel.from_pretrained("yjernite/retribert-base-uncased" ).to("cuda:0" ) lowerCamelCase__ : List[str] = qar_model.eval() else: lowerCamelCase__ , lowerCamelCase__ : str = (None, None) if MODEL_TYPE == "bart": lowerCamelCase__ : str = AutoTokenizer.from_pretrained("yjernite/bart_eli5" ) lowerCamelCase__ : str = AutoModelForSeqaSeqLM.from_pretrained("yjernite/bart_eli5" ).to("cuda:0" ) lowerCamelCase__ : Union[str, Any] = torch.load("seq2seq_models/eli5_bart_model_blm_2.pth" ) sas_model.load_state_dict(save_dict["model"] ) lowerCamelCase__ : Dict = sas_model.eval() else: lowerCamelCase__ , lowerCamelCase__ : Union[str, Any] = make_qa_sas_model( model_name="t5-small" , from_file="seq2seq_models/eli5_t5_model_1024_4.pth" , device="cuda:0" ) return (qar_tokenizer, qar_model, sas_tokenizer, sas_model) @st.cache(allow_output_mutation=__a ) def lowercase_ ( ): """simple docstring""" if LOAD_DENSE_INDEX: lowerCamelCase__ : Union[str, Any] = faiss.StandardGpuResources() lowerCamelCase__ : Optional[Any] = datasets.load_dataset(path="wiki_snippets" , name="wiki40b_en_100_0" )["train"] lowerCamelCase__ : Any = np.memmap( "wiki40b_passages_reps_32_l-8_h-768_b-512-512.dat" , dtype="float32" , mode="r" , shape=(wikiaab_passages.num_rows, 128) , ) lowerCamelCase__ : int = faiss.IndexFlatIP(128 ) lowerCamelCase__ : Any = faiss.index_cpu_to_gpu(__a , 1 , __a ) wikiaab_gpu_index_flat.add(__a ) # TODO fix for larger GPU else: lowerCamelCase__ , lowerCamelCase__ : List[Any] = (None, None) lowerCamelCase__ : Any = Elasticsearch([{"host": "localhost", "port": "9200"}] ) return (wikiaab_passages, wikiaab_gpu_index_flat, es_client) @st.cache(allow_output_mutation=__a ) def lowercase_ ( ): """simple docstring""" lowerCamelCase__ : int = datasets.load_dataset("eli5" , name="LFQA_reddit" ) lowerCamelCase__ : int = elia["train_eli5"] lowerCamelCase__ : Optional[int] = np.memmap( "eli5_questions_reps.dat" , dtype="float32" , mode="r" , shape=(elia_train.num_rows, 128) ) lowerCamelCase__ : List[Any] = faiss.IndexFlatIP(128 ) eli5_train_q_index.add(__a ) return (elia_train, eli5_train_q_index) A : str = load_indexes() A : str = load_models() A : Optional[Any] = load_train_data() def lowercase_ ( _A : int , _A : List[str]=10 ): """simple docstring""" lowerCamelCase__ : Optional[Any] = embed_questions_for_retrieval([question] , __a , __a ) lowerCamelCase__ , lowerCamelCase__ : Tuple = eli5_train_q_index.search(__a , __a ) lowerCamelCase__ : List[str] = [elia_train[int(__a )] for i in I[0]] return nn_examples def lowercase_ ( _A : Optional[Any] , _A : Any="wiki40b" , _A : int="dense" , _A : Any=10 ): """simple docstring""" if source == "none": lowerCamelCase__ , lowerCamelCase__ : List[Any] = (" <P> ".join(["" for _ in range(11 )] ).strip(), []) else: if method == "dense": lowerCamelCase__ , lowerCamelCase__ : str = query_qa_dense_index( __a , __a , __a , __a , __a , __a ) else: lowerCamelCase__ , lowerCamelCase__ : List[Any] = query_es_index( __a , __a , index_name="english_wiki40b_snippets_100w" , n_results=__a , ) lowerCamelCase__ : Optional[Any] = [ (res["article_title"], res["section_title"].strip(), res["score"], res["passage_text"]) for res in hit_lst ] lowerCamelCase__ : Optional[int] = "question: {} context: {}".format(__a , __a ) return question_doc, support_list @st.cache( hash_funcs={ torch.Tensor: (lambda _A : None), transformers.models.bart.tokenization_bart.BartTokenizer: (lambda _A : None), } ) def lowercase_ ( _A : List[str] , _A : List[Any] , _A : Optional[Any] , _A : Union[str, Any]=64 , _A : str=256 , _A : Union[str, Any]=False , _A : Union[str, Any]=2 , _A : int=0.95 , _A : Any=0.8 ): """simple docstring""" with torch.no_grad(): lowerCamelCase__ : Tuple = qa_sas_generate( __a , __a , __a , num_answers=1 , num_beams=__a , min_len=__a , max_len=__a , do_sample=__a , temp=__a , top_p=__a , top_k=__a , max_input_length=1024 , device="cuda:0" , )[0] return (answer, support_list) st.title("Long Form Question Answering with ELI5") # Start sidebar A : int = '''<img src=\'https://huggingface.co/front/assets/huggingface_logo.svg\'>''' A : Optional[int] = ''' <html> <head> <style> .img-container { padding-left: 90px; padding-right: 90px; padding-top: 50px; padding-bottom: 50px; background-color: #f0f3f9; } </style> </head> <body> <span class="img-container"> <!-- Inline parent element --> %s </span> </body> </html> ''' % ( header_html, ) st.sidebar.markdown( header_full, unsafe_allow_html=True, ) # Long Form QA with ELI5 and Wikipedia A : List[str] = ''' This demo presents a model trained to [provide long-form answers to open-domain questions](https://yjernite.github.io/lfqa.html). First, a document retriever fetches a set of relevant Wikipedia passages given the question from the [Wiki40b](https://research.google/pubs/pub49029/) dataset, a pre-processed fixed snapshot of Wikipedia. ''' st.sidebar.markdown(description, unsafe_allow_html=True) A : Optional[int] = [ '''Answer the question''', '''View the retrieved document only''', '''View the most similar ELI5 question and answer''', '''Show me everything, please!''', ] A : List[Any] = st.sidebar.checkbox("Demo options") if demo_options: A : Optional[Any] = st.sidebar.selectbox( "", action_list, index=3, ) A : Union[str, Any] = action_list.index(action_st) A : Union[str, Any] = st.sidebar.selectbox( "", ["Show full text of passages", "Show passage section titles"], index=0, ) A : Tuple = show_type == '''Show full text of passages''' else: A : str = 3 A : Tuple = True A : Union[str, Any] = st.sidebar.checkbox("Retrieval options") if retrieval_options: A : Tuple = ''' ### Information retriever options The **sparse** retriever uses ElasticSearch, while the **dense** retriever uses max-inner-product search between a question and passage embedding trained using the [ELI5](https://arxiv.org/abs/1907.09190) questions-answer pairs. The answer is then generated by sequence to sequence model which takes the question and retrieved document as input. ''' st.sidebar.markdown(retriever_info) A : str = st.sidebar.selectbox("Which Wikipedia format should the model use?", ["wiki40b", "none"]) A : int = st.sidebar.selectbox("Which Wikipedia indexer should the model use?", ["dense", "sparse", "mixed"]) else: A : Optional[Any] = '''wiki40b''' A : Tuple = '''dense''' A : List[str] = '''beam''' A : Optional[Any] = 2 A : Tuple = 64 A : List[str] = 256 A : str = None A : int = None A : List[Any] = st.sidebar.checkbox("Generation options") if generate_options: A : Any = ''' ### Answer generation options The sequence-to-sequence model was initialized with [BART](https://huggingface.co/facebook/bart-large) weights and fine-tuned on the ELI5 QA pairs and retrieved documents. You can use the model for greedy decoding with **beam** search, or **sample** from the decoder\'s output probabilities. ''' st.sidebar.markdown(generate_info) A : Any = st.sidebar.selectbox("Would you like to use beam search or sample an answer?", ["beam", "sampled"]) A : Any = st.sidebar.slider( "Minimum generation length", min_value=8, max_value=256, value=64, step=8, format=None, key=None ) A : Any = st.sidebar.slider( "Maximum generation length", min_value=64, max_value=512, value=256, step=16, format=None, key=None ) if sampled == "beam": A : List[Any] = st.sidebar.slider("Beam size", min_value=1, max_value=8, value=2, step=None, format=None, key=None) else: A : int = st.sidebar.slider( "Nucleus sampling p", min_value=0.1, max_value=1.0, value=0.9_5, step=0.0_1, format=None, key=None ) A : str = st.sidebar.slider( "Temperature", min_value=0.1, max_value=1.0, value=0.7, step=0.0_1, format=None, key=None ) A : Tuple = None # start main text A : Optional[Any] = [ '''<MY QUESTION>''', '''How do people make chocolate?''', '''Why do we get a fever when we are sick?''', '''How can different animals perceive different colors?''', '''What is natural language processing?''', '''What\'s the best way to treat a sunburn?''', '''What exactly are vitamins ?''', '''How does nuclear energy provide electricity?''', '''What\'s the difference between viruses and bacteria?''', '''Why are flutes classified as woodwinds when most of them are made out of metal ?''', '''Why do people like drinking coffee even though it tastes so bad?''', '''What happens when wine ages? How does it make the wine taste better?''', '''If an animal is an herbivore, where does it get the protein that it needs to survive if it only eats grass?''', '''How can we set a date to the beginning or end of an artistic period? Doesn\'t the change happen gradually?''', '''How does New Zealand have so many large bird predators?''', ] A : Tuple = st.selectbox( "What would you like to ask? ---- select <MY QUESTION> to enter a new query", questions_list, index=1, ) if question_s == "<MY QUESTION>": A : Any = st.text_input("Enter your question here:", "") else: A : List[Any] = question_s if st.button("Show me!"): if action in [0, 1, 3]: if index_type == "mixed": A : Dict = make_support(question, source=wiki_source, method="dense", n_results=10) A : int = make_support(question, source=wiki_source, method="sparse", n_results=10) A : Optional[Any] = [] for res_d, res_s in zip(support_list_dense, support_list_sparse): if tuple(res_d) not in support_list: support_list += [tuple(res_d)] if tuple(res_s) not in support_list: support_list += [tuple(res_s)] A : Optional[Any] = support_list[:10] A : List[Any] = '''<P> ''' + ''' <P> '''.join([res[-1] for res in support_list]) else: A : Tuple = make_support(question, source=wiki_source, method=index_type, n_results=10) if action in [0, 3]: A : int = answer_question( question_doc, sas_model, sas_tokenizer, min_len=min_len, max_len=int(max_len), sampling=(sampled == "sampled"), n_beams=n_beams, top_p=top_p, temp=temp, ) st.markdown("### The model generated answer is:") st.write(answer) if action in [0, 1, 3] and wiki_source != "none": st.markdown("--- \n ### The model is drawing information from the following Wikipedia passages:") for i, res in enumerate(support_list): A : Any = '''https://en.wikipedia.org/wiki/{}'''.format(res[0].replace(" ", "_")) A : Any = res[1].strip() if sec_titles == "": A : str = '''[{}]({})'''.format(res[0], wiki_url) else: A : Optional[Any] = sec_titles.split(" & ") A : str = ''' & '''.join( ["[{}]({}#{})".format(sec.strip(), wiki_url, sec.strip().replace(" ", "_")) for sec in sec_list] ) st.markdown( "{0:02d} - **Article**: {1:<18} <br> _Section_: {2}".format(i + 1, res[0], sections), unsafe_allow_html=True, ) if show_passages: st.write( "> <span style=\"font-family:arial; font-size:10pt;\">" + res[-1] + "</span>", unsafe_allow_html=True ) if action in [2, 3]: A : str = find_nearest_training(question) A : Union[str, Any] = nn_train_list[0] st.markdown( "--- \n ### The most similar question in the ELI5 training set was: \n\n {}".format(train_exple["title"]) ) A : Any = [ '''{}. {}'''.format(i + 1, " \n".join([line.strip() for line in ans.split("\n") if line.strip() != ""])) for i, (ans, sc) in enumerate(zip(train_exple["answers"]["text"], train_exple["answers"]["score"])) if i == 0 or sc > 2 ] st.markdown("##### Its answers were: \n\n {}".format("\n".join(answers_st))) A : Any = ''' --- **Disclaimer** *The intent of this app is to provide some (hopefully entertaining) insights into the behavior of a current LFQA system. Evaluating biases of such a model and ensuring factual generations are still very much open research problems. Therefore, until some significant progress is achieved, we caution against using the generated answers for practical purposes.* ''' st.sidebar.markdown(disclaimer, unsafe_allow_html=True)
184
from collections import deque class A : '''simple docstring''' def __init__( self : List[str] , __lowerCAmelCase : str , __lowerCAmelCase : int , __lowerCAmelCase : int ) -> None: """simple docstring""" A__ = process_name # process name A__ = arrival_time # arrival time of the process # completion time of finished process or last interrupted time A__ = arrival_time A__ = burst_time # remaining burst time A__ = 0 # total time of the process wait in ready queue A__ = 0 # time from arrival time to completion time class A : '''simple docstring''' def __init__( self : Tuple , __lowerCAmelCase : int , __lowerCAmelCase : list[int] , __lowerCAmelCase : deque[Process] , __lowerCAmelCase : int , ) -> None: """simple docstring""" A__ = number_of_queues # time slice of queues that round robin algorithm applied A__ = time_slices # unfinished process is in this ready_queue A__ = queue # current time A__ = current_time # finished process is in this sequence queue A__ = deque() def a_ ( self : Dict ) -> list[str]: """simple docstring""" A__ = [] for i in range(len(self.finish_queue ) ): sequence.append(self.finish_queue[i].process_name ) return sequence def a_ ( self : Tuple , __lowerCAmelCase : list[Process] ) -> list[int]: """simple docstring""" A__ = [] for i in range(len(__lowerCAmelCase ) ): waiting_times.append(queue[i].waiting_time ) return waiting_times def a_ ( self : Optional[Any] , __lowerCAmelCase : list[Process] ) -> list[int]: """simple docstring""" A__ = [] for i in range(len(__lowerCAmelCase ) ): turnaround_times.append(queue[i].turnaround_time ) return turnaround_times def a_ ( self : Dict , __lowerCAmelCase : list[Process] ) -> list[int]: """simple docstring""" A__ = [] for i in range(len(__lowerCAmelCase ) ): completion_times.append(queue[i].stop_time ) return completion_times def a_ ( self : int , __lowerCAmelCase : deque[Process] ) -> list[int]: """simple docstring""" return [q.burst_time for q in queue] def a_ ( self : Any , __lowerCAmelCase : Process ) -> int: """simple docstring""" process.waiting_time += self.current_time - process.stop_time return process.waiting_time def a_ ( self : Union[str, Any] , __lowerCAmelCase : deque[Process] ) -> deque[Process]: """simple docstring""" A__ = deque() # sequence deque of finished process while len(__lowerCAmelCase ) != 0: A__ = ready_queue.popleft() # current process # if process's arrival time is later than current time, update current time if self.current_time < cp.arrival_time: self.current_time += cp.arrival_time # update waiting time of current process self.update_waiting_time(__lowerCAmelCase ) # update current time self.current_time += cp.burst_time # finish the process and set the process's burst-time 0 A__ = 0 # set the process's turnaround time because it is finished A__ = self.current_time - cp.arrival_time # set the completion time A__ = self.current_time # add the process to queue that has finished queue finished.append(__lowerCAmelCase ) self.finish_queue.extend(__lowerCAmelCase ) # add finished process to finish queue # FCFS will finish all remaining processes return finished def a_ ( self : Optional[Any] , __lowerCAmelCase : deque[Process] , __lowerCAmelCase : int ) -> tuple[deque[Process], deque[Process]]: """simple docstring""" A__ = deque() # sequence deque of terminated process # just for 1 cycle and unfinished processes will go back to queue for _ in range(len(__lowerCAmelCase ) ): A__ = ready_queue.popleft() # current process # if process's arrival time is later than current time, update current time if self.current_time < cp.arrival_time: self.current_time += cp.arrival_time # update waiting time of unfinished processes self.update_waiting_time(__lowerCAmelCase ) # if the burst time of process is bigger than time-slice if cp.burst_time > time_slice: # use CPU for only time-slice self.current_time += time_slice # update remaining burst time cp.burst_time -= time_slice # update end point time A__ = self.current_time # locate the process behind the queue because it is not finished ready_queue.append(__lowerCAmelCase ) else: # use CPU for remaining burst time self.current_time += cp.burst_time # set burst time 0 because the process is finished A__ = 0 # set the finish time A__ = self.current_time # update the process' turnaround time because it is finished A__ = self.current_time - cp.arrival_time # add the process to queue that has finished queue finished.append(__lowerCAmelCase ) self.finish_queue.extend(__lowerCAmelCase ) # add finished process to finish queue # return finished processes queue and remaining processes queue return finished, ready_queue def a_ ( self : List[Any] ) -> deque[Process]: """simple docstring""" for i in range(self.number_of_queues - 1 ): A__ , A__ = self.round_robin( self.ready_queue , self.time_slices[i] ) # the last queue has first_come_first_served algorithm self.first_come_first_served(self.ready_queue ) return self.finish_queue if __name__ == "__main__": import doctest A : Union[str, Any] = Process('''P1''', 0, 5_3) A : Optional[Any] = Process('''P2''', 0, 1_7) A : Optional[int] = Process('''P3''', 0, 6_8) A : int = Process('''P4''', 0, 2_4) A : Any = 3 A : List[Any] = [1_7, 2_5] A : Optional[Any] = deque([Pa, Pa, Pa, Pa]) if len(time_slices) != number_of_queues - 1: raise SystemExit(0) doctest.testmod(extraglobs={'''queue''': deque([Pa, Pa, Pa, Pa])}) A : Optional[Any] = Process('''P1''', 0, 5_3) A : int = Process('''P2''', 0, 1_7) A : Optional[int] = Process('''P3''', 0, 6_8) A : Tuple = Process('''P4''', 0, 2_4) A : Union[str, Any] = 3 A : Optional[Any] = [1_7, 2_5] A : Tuple = deque([Pa, Pa, Pa, Pa]) A : Optional[int] = MLFQ(number_of_queues, time_slices, queue, 0) A : Dict = mlfq.multi_level_feedback_queue() # print total waiting times of processes(P1, P2, P3, P4) print( F'''waiting time:\ \t\t\t{MLFQ.calculate_waiting_time(mlfq, [Pa, Pa, Pa, Pa])}''' ) # print completion times of processes(P1, P2, P3, P4) print( F'''completion time:\ \t\t{MLFQ.calculate_completion_time(mlfq, [Pa, Pa, Pa, Pa])}''' ) # print total turnaround times of processes(P1, P2, P3, P4) print( F'''turnaround time:\ \t\t{MLFQ.calculate_turnaround_time(mlfq, [Pa, Pa, Pa, Pa])}''' ) # print sequence of finished processes print( F'''sequence of finished processes:\ {mlfq.calculate_sequence_of_finish_queue()}''' )
274
0
# Copyright 2021 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from pathlib import Path import torch from ...utils import is_npu_available, is_xpu_available from .config_args import ClusterConfig, default_json_config_file from .config_utils import SubcommandHelpFormatter lowercase = '''Create a default config file for Accelerate with only a few flags set.''' def __UpperCAmelCase ( a_="no" , a_ = default_json_config_file , a_ = False): snake_case_ = Path(__a) path.parent.mkdir(parents=__a , exist_ok=__a) if path.exists(): print( f'''Configuration already exists at {save_location}, will not override. Run `accelerate config` manually or pass a different `save_location`.''') return False snake_case_ = mixed_precision.lower() if mixed_precision not in ["no", "fp16", "bf16", "fp8"]: raise ValueError( f'''`mixed_precision` should be one of \'no\', \'fp16\', \'bf16\', or \'fp8\'. Received {mixed_precision}''') snake_case_ = { 'compute_environment': 'LOCAL_MACHINE', 'mixed_precision': mixed_precision, } if torch.cuda.is_available(): snake_case_ = torch.cuda.device_count() snake_case_ = num_gpus snake_case_ = False if num_gpus > 1: snake_case_ = 'MULTI_GPU' else: snake_case_ = 'NO' elif is_xpu_available() and use_xpu: snake_case_ = torch.xpu.device_count() snake_case_ = num_xpus snake_case_ = False if num_xpus > 1: snake_case_ = 'MULTI_XPU' else: snake_case_ = 'NO' elif is_npu_available(): snake_case_ = torch.npu.device_count() snake_case_ = num_npus snake_case_ = False if num_npus > 1: snake_case_ = 'MULTI_NPU' else: snake_case_ = 'NO' else: snake_case_ = 0 snake_case_ = True snake_case_ = 1 snake_case_ = 'NO' snake_case_ = ClusterConfig(**__a) config.to_json_file(__a) return path def __UpperCAmelCase ( a_ , a_): snake_case_ = parser.add_parser('default' , parents=__a , help=__a , formatter_class=__a) parser.add_argument( '--config_file' , default=__a , help=( 'The path to use to store the config file. Will default to a file named default_config.yaml in the cache ' 'location, which is the content of the environment `HF_HOME` suffixed with \'accelerate\', or if you don\'t have ' 'such an environment variable, your cache directory (\'~/.cache\' or the content of `XDG_CACHE_HOME`) suffixed ' 'with \'huggingface\'.' ) , dest='save_location' , ) parser.add_argument( '--mixed_precision' , choices=['no', 'fp16', 'bf16'] , type=__a , help='Whether or not to use mixed precision training. ' 'Choose between FP16 and BF16 (bfloat16) training. ' 'BF16 training is only supported on Nvidia Ampere GPUs and PyTorch 1.10 or later.' , default='no' , ) parser.set_defaults(func=__a) return parser def __UpperCAmelCase ( a_): snake_case_ = write_basic_config(args.mixed_precision , args.save_location) if config_file: print(f'''accelerate configuration saved at {config_file}''')
178
from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...onnx.utils import compute_effective_axis_dimension from ...utils import logging if TYPE_CHECKING: from ...processing_utils import ProcessorMixin from ...utils import TensorType A : str = logging.get_logger(__name__) A : Union[str, Any] = { '''microsoft/layoutlmv3-base''': '''https://huggingface.co/microsoft/layoutlmv3-base/resolve/main/config.json''', } class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : Optional[Any] = '''layoutlmv3''' def __init__( self : Tuple , __lowerCAmelCase : Optional[int]=5_02_65 , __lowerCAmelCase : Tuple=7_68 , __lowerCAmelCase : Union[str, Any]=12 , __lowerCAmelCase : Any=12 , __lowerCAmelCase : List[Any]=30_72 , __lowerCAmelCase : Optional[int]="gelu" , __lowerCAmelCase : Dict=0.1 , __lowerCAmelCase : List[Any]=0.1 , __lowerCAmelCase : Dict=5_12 , __lowerCAmelCase : Any=2 , __lowerCAmelCase : Dict=0.0_2 , __lowerCAmelCase : List[str]=1e-5 , __lowerCAmelCase : List[str]=1 , __lowerCAmelCase : int=0 , __lowerCAmelCase : Dict=2 , __lowerCAmelCase : Tuple=10_24 , __lowerCAmelCase : List[str]=1_28 , __lowerCAmelCase : Optional[int]=1_28 , __lowerCAmelCase : Any=True , __lowerCAmelCase : Optional[Any]=32 , __lowerCAmelCase : Any=1_28 , __lowerCAmelCase : str=64 , __lowerCAmelCase : Optional[int]=2_56 , __lowerCAmelCase : int=True , __lowerCAmelCase : int=True , __lowerCAmelCase : List[str]=True , __lowerCAmelCase : int=2_24 , __lowerCAmelCase : Dict=3 , __lowerCAmelCase : List[Any]=16 , __lowerCAmelCase : Dict=None , **__lowerCAmelCase : Optional[Any] , ) -> Dict: """simple docstring""" super().__init__( vocab_size=__lowerCAmelCase , hidden_size=__lowerCAmelCase , num_hidden_layers=__lowerCAmelCase , num_attention_heads=__lowerCAmelCase , intermediate_size=__lowerCAmelCase , hidden_act=__lowerCAmelCase , hidden_dropout_prob=__lowerCAmelCase , attention_probs_dropout_prob=__lowerCAmelCase , max_position_embeddings=__lowerCAmelCase , type_vocab_size=__lowerCAmelCase , initializer_range=__lowerCAmelCase , layer_norm_eps=__lowerCAmelCase , pad_token_id=__lowerCAmelCase , bos_token_id=__lowerCAmelCase , eos_token_id=__lowerCAmelCase , **__lowerCAmelCase , ) A__ = max_ad_position_embeddings A__ = coordinate_size A__ = shape_size A__ = has_relative_attention_bias A__ = rel_pos_bins A__ = max_rel_pos A__ = has_spatial_attention_bias A__ = rel_ad_pos_bins A__ = max_rel_ad_pos A__ = text_embed A__ = visual_embed A__ = input_size A__ = num_channels A__ = patch_size A__ = classifier_dropout class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : List[str] = version.parse('''1.12''' ) @property def a_ ( self : int ) -> Mapping[str, Mapping[int, str]]: """simple docstring""" if self.task in ["question-answering", "sequence-classification"]: return OrderedDict( [ ("""input_ids""", {0: """batch""", 1: """sequence"""}), ("""attention_mask""", {0: """batch""", 1: """sequence"""}), ("""bbox""", {0: """batch""", 1: """sequence"""}), ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}), ] ) else: return OrderedDict( [ ("""input_ids""", {0: """batch""", 1: """sequence"""}), ("""bbox""", {0: """batch""", 1: """sequence"""}), ("""attention_mask""", {0: """batch""", 1: """sequence"""}), ("""pixel_values""", {0: """batch""", 1: """num_channels"""}), ] ) @property def a_ ( self : Optional[int] ) -> float: """simple docstring""" return 1e-5 @property def a_ ( self : Tuple ) -> int: """simple docstring""" return 12 def a_ ( self : str , __lowerCAmelCase : "ProcessorMixin" , __lowerCAmelCase : int = -1 , __lowerCAmelCase : int = -1 , __lowerCAmelCase : bool = False , __lowerCAmelCase : Optional["TensorType"] = None , __lowerCAmelCase : int = 3 , __lowerCAmelCase : int = 40 , __lowerCAmelCase : int = 40 , ) -> Mapping[str, Any]: """simple docstring""" setattr(processor.image_processor , """apply_ocr""" , __lowerCAmelCase ) # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX A__ = compute_effective_axis_dimension( __lowerCAmelCase , fixed_dimension=OnnxConfig.default_fixed_batch , num_token_to_add=0 ) # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX A__ = processor.tokenizer.num_special_tokens_to_add(__lowerCAmelCase ) A__ = compute_effective_axis_dimension( __lowerCAmelCase , fixed_dimension=OnnxConfig.default_fixed_sequence , num_token_to_add=__lowerCAmelCase ) # Generate dummy inputs according to compute batch and sequence A__ = [[""" """.join([processor.tokenizer.unk_token] ) * seq_length]] * batch_size # Generate dummy bounding boxes A__ = [[[48, 84, 73, 1_28]]] * batch_size # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX # batch_size = compute_effective_axis_dimension(batch_size, fixed_dimension=OnnxConfig.default_fixed_batch) A__ = self._generate_dummy_images(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) A__ = dict( processor( __lowerCAmelCase , text=__lowerCAmelCase , boxes=__lowerCAmelCase , return_tensors=__lowerCAmelCase , ) ) return inputs
274
0
_A = {str(digit): digit**5 for digit in range(10)} def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : int ): return sum(DIGITS_FIFTH_POWER[digit] for digit in str(__a ) ) def _UpperCAmelCase ( ): return sum( number for number in range(10_00 , 1_00_00_00 ) if number == digits_fifth_powers_sum(__a ) ) if __name__ == "__main__": print(solution())
62
import json import os from pathlib import Path from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple, Union import sentencepiece from ...tokenization_utils import BatchEncoding, PreTrainedTokenizer from ...utils import logging A : Optional[Any] = logging.get_logger(__name__) A : str = '''▁''' A : Any = { '''vocab_file''': '''vocab.json''', '''spm_file''': '''sentencepiece.bpe.model''', '''tokenizer_config_file''': '''tokenizer_config.json''', } A : List[Any] = { '''vocab_file''': { '''facebook/m2m100_418M''': '''https://huggingface.co/facebook/m2m100_418M/resolve/main/vocab.json''', '''facebook/m2m100_1.2B''': '''https://huggingface.co/facebook/m2m100_1.2B/resolve/main/vocab.json''', }, '''spm_file''': { '''facebook/m2m100_418M''': '''https://huggingface.co/facebook/m2m100_418M/resolve/main/sentencepiece.bpe.model''', '''facebook/m2m100_1.2B''': '''https://huggingface.co/facebook/m2m100_1.2B/resolve/main/sentencepiece.bpe.model''', }, '''tokenizer_config_file''': { '''facebook/m2m100_418M''': '''https://huggingface.co/facebook/m2m100_418M/resolve/main/tokenizer_config.json''', '''facebook/m2m100_1.2B''': '''https://huggingface.co/facebook/m2m100_1.2B/resolve/main/tokenizer_config.json''', }, } A : Tuple = { '''facebook/m2m100_418M''': 1_0_2_4, } # fmt: off A : Optional[int] = { '''m2m100''': ['''af''', '''am''', '''ar''', '''ast''', '''az''', '''ba''', '''be''', '''bg''', '''bn''', '''br''', '''bs''', '''ca''', '''ceb''', '''cs''', '''cy''', '''da''', '''de''', '''el''', '''en''', '''es''', '''et''', '''fa''', '''ff''', '''fi''', '''fr''', '''fy''', '''ga''', '''gd''', '''gl''', '''gu''', '''ha''', '''he''', '''hi''', '''hr''', '''ht''', '''hu''', '''hy''', '''id''', '''ig''', '''ilo''', '''is''', '''it''', '''ja''', '''jv''', '''ka''', '''kk''', '''km''', '''kn''', '''ko''', '''lb''', '''lg''', '''ln''', '''lo''', '''lt''', '''lv''', '''mg''', '''mk''', '''ml''', '''mn''', '''mr''', '''ms''', '''my''', '''ne''', '''nl''', '''no''', '''ns''', '''oc''', '''or''', '''pa''', '''pl''', '''ps''', '''pt''', '''ro''', '''ru''', '''sd''', '''si''', '''sk''', '''sl''', '''so''', '''sq''', '''sr''', '''ss''', '''su''', '''sv''', '''sw''', '''ta''', '''th''', '''tl''', '''tn''', '''tr''', '''uk''', '''ur''', '''uz''', '''vi''', '''wo''', '''xh''', '''yi''', '''yo''', '''zh''', '''zu'''], '''wmt21''': ['''en''', '''ha''', '''is''', '''ja''', '''cs''', '''ru''', '''zh''', '''de'''] } class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : Union[str, Any] = VOCAB_FILES_NAMES __lowerCamelCase : List[str] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase : Optional[int] = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase : Dict = ['''input_ids''', '''attention_mask'''] __lowerCamelCase : List[int] = [] __lowerCamelCase : List[int] = [] def __init__( self : List[Any] , __lowerCAmelCase : List[Any] , __lowerCAmelCase : Dict , __lowerCAmelCase : Union[str, Any]=None , __lowerCAmelCase : str=None , __lowerCAmelCase : List[Any]="<s>" , __lowerCAmelCase : List[Any]="</s>" , __lowerCAmelCase : Optional[int]="</s>" , __lowerCAmelCase : Optional[Any]="<pad>" , __lowerCAmelCase : Any="<unk>" , __lowerCAmelCase : Any="m2m100" , __lowerCAmelCase : Optional[Dict[str, Any]] = None , __lowerCAmelCase : Dict=8 , **__lowerCAmelCase : Tuple , ) -> None: """simple docstring""" A__ = {} if sp_model_kwargs is None else sp_model_kwargs A__ = language_codes A__ = FAIRSEQ_LANGUAGE_CODES[language_codes] A__ = {lang_code: f'__{lang_code}__' for lang_code in fairseq_language_code} A__ = kwargs.get("""additional_special_tokens""" , [] ) kwargs["additional_special_tokens"] += [ self.get_lang_token(__lowerCAmelCase ) for lang_code in fairseq_language_code if self.get_lang_token(__lowerCAmelCase ) not in kwargs["additional_special_tokens"] ] super().__init__( src_lang=__lowerCAmelCase , tgt_lang=__lowerCAmelCase , bos_token=__lowerCAmelCase , eos_token=__lowerCAmelCase , sep_token=__lowerCAmelCase , unk_token=__lowerCAmelCase , pad_token=__lowerCAmelCase , language_codes=__lowerCAmelCase , sp_model_kwargs=self.sp_model_kwargs , num_madeup_words=__lowerCAmelCase , **__lowerCAmelCase , ) A__ = vocab_file A__ = load_json(__lowerCAmelCase ) A__ = {v: k for k, v in self.encoder.items()} A__ = spm_file A__ = load_spm(__lowerCAmelCase , self.sp_model_kwargs ) A__ = len(self.encoder ) A__ = { self.get_lang_token(__lowerCAmelCase ): self.encoder_size + i for i, lang_code in enumerate(__lowerCAmelCase ) } A__ = {lang_code: self.encoder_size + i for i, lang_code in enumerate(__lowerCAmelCase )} A__ = {v: k for k, v in self.lang_token_to_id.items()} A__ = src_lang if src_lang is not None else """en""" A__ = tgt_lang A__ = self.get_lang_id(self._src_lang ) self.set_src_lang_special_tokens(self._src_lang ) A__ = num_madeup_words @property def a_ ( self : Optional[int] ) -> int: """simple docstring""" return len(self.encoder ) + len(self.lang_token_to_id ) @property def a_ ( self : Optional[Any] ) -> str: """simple docstring""" return self._src_lang @src_lang.setter def a_ ( self : List[Any] , __lowerCAmelCase : str ) -> None: """simple docstring""" A__ = new_src_lang self.set_src_lang_special_tokens(self._src_lang ) def a_ ( self : Optional[int] , __lowerCAmelCase : str ) -> List[str]: """simple docstring""" return self.sp_model.encode(__lowerCAmelCase , out_type=__lowerCAmelCase ) def a_ ( self : Optional[Any] , __lowerCAmelCase : Dict ) -> Optional[Any]: """simple docstring""" if token in self.lang_token_to_id: return self.lang_token_to_id[token] return self.encoder.get(__lowerCAmelCase , self.encoder[self.unk_token] ) def a_ ( self : Optional[int] , __lowerCAmelCase : int ) -> str: """simple docstring""" if index in self.id_to_lang_token: return self.id_to_lang_token[index] return self.decoder.get(__lowerCAmelCase , self.unk_token ) def a_ ( self : Optional[int] , __lowerCAmelCase : Dict ) -> str: """simple docstring""" A__ = [] A__ = """""" 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(__lowerCAmelCase ) + token A__ = [] else: current_sub_tokens.append(__lowerCAmelCase ) out_string += self.sp_model.decode(__lowerCAmelCase ) return out_string.strip() def a_ ( self : List[str] , __lowerCAmelCase : List[int] , __lowerCAmelCase : Optional[List[int]] = None , __lowerCAmelCase : bool = False ) -> List[int]: """simple docstring""" if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=__lowerCAmelCase , token_ids_a=__lowerCAmelCase , already_has_special_tokens=__lowerCAmelCase ) A__ = [1] * len(self.prefix_tokens ) A__ = [1] * len(self.suffix_tokens ) if token_ids_a is None: return prefix_ones + ([0] * len(__lowerCAmelCase )) + suffix_ones return prefix_ones + ([0] * len(__lowerCAmelCase )) + ([0] * len(__lowerCAmelCase )) + suffix_ones def a_ ( self : Tuple , __lowerCAmelCase : List[int] , __lowerCAmelCase : Optional[List[int]] = None ) -> List[int]: """simple docstring""" if token_ids_a is None: return self.prefix_tokens + token_ids_a + self.suffix_tokens # We don't expect to process pairs, but leave the pair logic for API consistency return self.prefix_tokens + token_ids_a + token_ids_a + self.suffix_tokens def a_ ( self : int ) -> Dict: """simple docstring""" A__ = {self.convert_ids_to_tokens(__lowerCAmelCase ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self : Union[str, Any] ) -> Dict: """simple docstring""" A__ = self.__dict__.copy() A__ = None return state def __setstate__( self : str , __lowerCAmelCase : Dict ) -> None: """simple docstring""" A__ = d # for backward compatibility if not hasattr(self , """sp_model_kwargs""" ): A__ = {} A__ = load_spm(self.spm_file , self.sp_model_kwargs ) def a_ ( self : List[str] , __lowerCAmelCase : str , __lowerCAmelCase : Optional[str] = None ) -> Tuple[str]: """simple docstring""" A__ = Path(__lowerCAmelCase ) if not save_dir.is_dir(): raise OSError(f'{save_directory} should be a directory' ) A__ = save_dir / ( (filename_prefix + """-""" if filename_prefix else """""") + self.vocab_files_names["""vocab_file"""] ) A__ = save_dir / ( (filename_prefix + """-""" if filename_prefix else """""") + self.vocab_files_names["""spm_file"""] ) save_json(self.encoder , __lowerCAmelCase ) if os.path.abspath(self.spm_file ) != os.path.abspath(__lowerCAmelCase ) and os.path.isfile(self.spm_file ): copyfile(self.spm_file , __lowerCAmelCase ) elif not os.path.isfile(self.spm_file ): with open(__lowerCAmelCase , """wb""" ) as fi: A__ = self.sp_model.serialized_model_proto() fi.write(__lowerCAmelCase ) return (str(__lowerCAmelCase ), str(__lowerCAmelCase )) def a_ ( self : str , __lowerCAmelCase : List[str] , __lowerCAmelCase : str = "en" , __lowerCAmelCase : Optional[List[str]] = None , __lowerCAmelCase : str = "ro" , **__lowerCAmelCase : List[Any] , ) -> BatchEncoding: """simple docstring""" A__ = src_lang A__ = tgt_lang self.set_src_lang_special_tokens(self.src_lang ) return super().prepare_seqaseq_batch(__lowerCAmelCase , __lowerCAmelCase , **__lowerCAmelCase ) def a_ ( self : Optional[int] , __lowerCAmelCase : Any , __lowerCAmelCase : Optional[str] , __lowerCAmelCase : Optional[str] , **__lowerCAmelCase : Tuple ) -> Tuple: """simple docstring""" if src_lang is None or tgt_lang is None: raise ValueError("""Translation requires a `src_lang` and a `tgt_lang` for this model""" ) A__ = src_lang A__ = self(__lowerCAmelCase , add_special_tokens=__lowerCAmelCase , **__lowerCAmelCase ) A__ = self.get_lang_id(__lowerCAmelCase ) A__ = tgt_lang_id return inputs def a_ ( self : Dict ) -> int: """simple docstring""" self.set_src_lang_special_tokens(self.src_lang ) def a_ ( self : Union[str, Any] ) -> Union[str, Any]: """simple docstring""" self.set_tgt_lang_special_tokens(self.tgt_lang ) def a_ ( self : str , __lowerCAmelCase : str ) -> None: """simple docstring""" A__ = self.get_lang_token(__lowerCAmelCase ) A__ = self.lang_token_to_id[lang_token] A__ = [self.cur_lang_id] A__ = [self.eos_token_id] def a_ ( self : Tuple , __lowerCAmelCase : str ) -> None: """simple docstring""" A__ = self.get_lang_token(__lowerCAmelCase ) A__ = self.lang_token_to_id[lang_token] A__ = [self.cur_lang_id] A__ = [self.eos_token_id] def a_ ( self : Union[str, Any] , __lowerCAmelCase : str ) -> str: """simple docstring""" return self.lang_code_to_token[lang] def a_ ( self : Union[str, Any] , __lowerCAmelCase : str ) -> int: """simple docstring""" A__ = self.get_lang_token(__lowerCAmelCase ) return self.lang_token_to_id[lang_token] def __lowerCamelCase ( __a :str , __a :Dict[str, Any] ) -> sentencepiece.SentencePieceProcessor: """simple docstring""" A__ = sentencepiece.SentencePieceProcessor(**__a ) spm.Load(str(__a ) ) return spm def __lowerCamelCase ( __a :str ) -> Union[Dict, List]: """simple docstring""" with open(__a , """r""" ) as f: return json.load(__a ) def __lowerCamelCase ( __a :List[Any] , __a :str ) -> None: """simple docstring""" with open(__a , """w""" ) as f: json.dump(__a , __a , indent=2 )
274
0
'''simple docstring''' _lowerCAmelCase = 0 # The first color of the flag. _lowerCAmelCase = 1 # The second color of the flag. _lowerCAmelCase = 2 # The third color of the flag. _lowerCAmelCase = (red, white, blue) def __lowerCAmelCase ( snake_case__ ): if not sequence: return [] if len(__a ) == 1: return list(__a ) __UpperCamelCase : Any = 0 __UpperCamelCase : List[str] = len(__a ) - 1 __UpperCamelCase : Optional[Any] = 0 while mid <= high: if sequence[mid] == colors[0]: __UpperCamelCase , __UpperCamelCase : Dict = sequence[mid], sequence[low] low += 1 mid += 1 elif sequence[mid] == colors[1]: mid += 1 elif sequence[mid] == colors[2]: __UpperCamelCase , __UpperCamelCase : Tuple = sequence[high], sequence[mid] high -= 1 else: __UpperCamelCase : Any = F"The elements inside the sequence must contains only {colors} values" raise ValueError(__a ) return sequence if __name__ == "__main__": import doctest doctest.testmod() _lowerCAmelCase = input('''Enter numbers separated by commas:\n''').strip() _lowerCAmelCase = [int(item.strip()) for item in user_input.split(''',''')] print(f'{dutch_national_flag_sort(unsorted)}')
298
from __future__ import annotations from PIL import Image # Define glider example A : Any = [ [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [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], ] # Define blinker example A : Optional[Any] = [[0, 1, 0], [0, 1, 0], [0, 1, 0]] def __lowerCamelCase ( __a :list[list[int]] ) -> list[list[int]]: """simple docstring""" A__ = [] for i in range(len(__a ) ): A__ = [] for j in range(len(cells[i] ) ): # Get the number of live neighbours A__ = 0 if i > 0 and j > 0: neighbour_count += cells[i - 1][j - 1] if i > 0: neighbour_count += cells[i - 1][j] if i > 0 and j < len(cells[i] ) - 1: neighbour_count += cells[i - 1][j + 1] if j > 0: neighbour_count += cells[i][j - 1] if j < len(cells[i] ) - 1: neighbour_count += cells[i][j + 1] if i < len(__a ) - 1 and j > 0: neighbour_count += cells[i + 1][j - 1] if i < len(__a ) - 1: neighbour_count += cells[i + 1][j] if i < len(__a ) - 1 and j < len(cells[i] ) - 1: neighbour_count += cells[i + 1][j + 1] # Rules of the game of life (excerpt from Wikipedia): # 1. Any live cell with two or three live neighbours survives. # 2. Any dead cell with three live neighbours becomes a live cell. # 3. All other live cells die in the next generation. # Similarly, all other dead cells stay dead. A__ = cells[i][j] == 1 if ( (alive and 2 <= neighbour_count <= 3) or not alive and neighbour_count == 3 ): next_generation_row.append(1 ) else: next_generation_row.append(0 ) next_generation.append(__a ) return next_generation def __lowerCamelCase ( __a :list[list[int]] , __a :int ) -> list[Image.Image]: """simple docstring""" A__ = [] for _ in range(__a ): # Create output image A__ = Image.new("""RGB""" , (len(cells[0] ), len(__a )) ) A__ = img.load() # Save cells to image for x in range(len(__a ) ): for y in range(len(cells[0] ) ): A__ = 2_5_5 - cells[y][x] * 2_5_5 A__ = (colour, colour, colour) # Save image images.append(__a ) A__ = new_generation(__a ) return images if __name__ == "__main__": A : str = generate_images(GLIDER, 1_6) images[0].save('''out.gif''', save_all=True, append_images=images[1:])
274
0
"""simple docstring""" import os from glob import glob import imageio import torch import torchvision import wandb from img_processing import custom_to_pil, loop_post_process, preprocess, preprocess_vqgan from loaders import load_vqgan from PIL import Image from torch import nn from transformers import CLIPModel, CLIPTokenizerFast from utils import get_device, get_timestamp, show_pil class UpperCamelCase : def __init__( self : Dict , UpperCAmelCase__ : str = "cpu" , UpperCAmelCase__ : str = "openai/clip-vit-large-patch14" ) -> None: _a : List[Any] = device _a : str = CLIPTokenizerFast.from_pretrained(__lowerCAmelCase ) _a : List[str] = [0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3] _a : str = [0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1] _a : Union[str, Any] = torchvision.transforms.Normalize(self.image_mean , self.image_std ) _a : Union[str, Any] = torchvision.transforms.Resize(224 ) _a : int = torchvision.transforms.CenterCrop(224 ) def _lowercase ( self : Dict , UpperCAmelCase__ : Optional[int] ) -> Optional[int]: _a : Union[str, Any] = self.resize(__lowerCAmelCase ) _a : Tuple = self.center_crop(__lowerCAmelCase ) _a : Union[str, Any] = self.normalize(__lowerCAmelCase ) return images def __call__( self : Dict , UpperCAmelCase__ : Tuple=None , UpperCAmelCase__ : Any=None , **UpperCAmelCase__ : Dict ) -> Optional[int]: _a : Optional[int] = self.tokenizer(text=__lowerCAmelCase , **__lowerCAmelCase ) _a : Dict = self.preprocess_img(__lowerCAmelCase ) _a : Optional[Any] = {key: value.to(self.device ) for (key, value) in encoding.items()} return encoding class UpperCamelCase ( nn.Module ): def __init__( self : Dict , UpperCAmelCase__ : Any=10 , UpperCAmelCase__ : Union[str, Any]=0.0_1 , UpperCAmelCase__ : Tuple=None , UpperCAmelCase__ : int=None , UpperCAmelCase__ : List[str]=None , UpperCAmelCase__ : List[str]=None , UpperCAmelCase__ : List[str]=None , UpperCAmelCase__ : str=None , UpperCAmelCase__ : str=False , UpperCAmelCase__ : Optional[int]=True , UpperCAmelCase__ : List[Any]="image" , UpperCAmelCase__ : List[Any]=True , UpperCAmelCase__ : List[Any]=False , UpperCAmelCase__ : int=False , UpperCAmelCase__ : int=False , ) -> None: super().__init__() _a : Dict = None _a : Optional[int] = device if device else get_device() if vqgan: _a : List[Any] = vqgan else: _a : Tuple = load_vqgan(self.device , conf_path=__lowerCAmelCase , ckpt_path=__lowerCAmelCase ) self.vqgan.eval() if clip: _a : Tuple = clip else: _a : Union[str, Any] = CLIPModel.from_pretrained("""openai/clip-vit-base-patch32""" ) self.clip.to(self.device ) _a : Any = ProcessorGradientFlow(device=self.device ) _a : Dict = iterations _a : Tuple = lr _a : List[str] = log _a : Any = make_grid _a : int = return_val _a : str = quantize _a : int = self.vqgan.decoder.z_shape def _lowercase ( self : List[Any] , UpperCAmelCase__ : int=None , UpperCAmelCase__ : Optional[Any]=None , UpperCAmelCase__ : List[Any]=5 , UpperCAmelCase__ : Optional[int]=True ) -> Optional[int]: _a : str = [] if output_path is None: _a : Union[str, Any] = """./animation.gif""" if input_path is None: _a : str = self.save_path _a : Optional[int] = sorted(glob(input_path + """/*""" ) ) if not len(__lowerCAmelCase ): raise ValueError( """No images found in save path, aborting (did you pass save_intermediate=True to the generate""" """ function?)""" ) if len(__lowerCAmelCase ) == 1: print("""Only one image found in save path, (did you pass save_intermediate=True to the generate function?)""" ) _a : Union[str, Any] = total_duration / len(__lowerCAmelCase ) _a : List[Any] = [frame_duration] * len(__lowerCAmelCase ) if extend_frames: _a : Tuple = 1.5 _a : List[Any] = 3 for file_name in paths: if file_name.endswith(""".png""" ): images.append(imageio.imread(__lowerCAmelCase ) ) imageio.mimsave(__lowerCAmelCase , __lowerCAmelCase , duration=__lowerCAmelCase ) print(f"""gif saved to {output_path}""" ) def _lowercase ( self : int , UpperCAmelCase__ : Optional[Any]=None , UpperCAmelCase__ : Any=None ) -> List[Any]: if not (path or img): raise ValueError("""Input either path or tensor""" ) if img is not None: raise NotImplementedError _a : Optional[Any] = preprocess(Image.open(__lowerCAmelCase ) , target_image_size=256 ).to(self.device ) _a : str = preprocess_vqgan(__lowerCAmelCase ) _a , *_a : List[Any] = self.vqgan.encode(__lowerCAmelCase ) return z def _lowercase ( self : Optional[Any] , UpperCAmelCase__ : Any ) -> Tuple: _a : Tuple = self.latent.detach().requires_grad_() _a : Union[str, Any] = base_latent + transform_vector if self.quantize: _a , *_a : Dict = self.vqgan.quantize(__lowerCAmelCase ) else: _a : str = trans_latent return self.vqgan.decode(__lowerCAmelCase ) def _lowercase ( self : Optional[int] , UpperCAmelCase__ : Dict , UpperCAmelCase__ : Dict , UpperCAmelCase__ : Optional[Any]=None ) -> Optional[Any]: _a : List[Any] = self.clip_preprocessor(text=__lowerCAmelCase , images=__lowerCAmelCase , return_tensors="""pt""" , padding=__lowerCAmelCase ) _a : Optional[Any] = self.clip(**__lowerCAmelCase ) _a : Optional[int] = clip_outputs.logits_per_image if weights is not None: _a : Union[str, Any] = similarity_logits * weights return similarity_logits.sum() def _lowercase ( self : str , UpperCAmelCase__ : Any , UpperCAmelCase__ : str , UpperCAmelCase__ : Optional[Any] ) -> Union[str, Any]: _a : Tuple = self._get_clip_similarity(pos_prompts["""prompts"""] , __lowerCAmelCase , weights=(1 / pos_prompts["""weights"""]) ) if neg_prompts: _a : Any = self._get_clip_similarity(neg_prompts["""prompts"""] , __lowerCAmelCase , weights=neg_prompts["""weights"""] ) else: _a : Optional[int] = torch.tensor([1] , device=self.device ) _a : List[Any] = -torch.log(__lowerCAmelCase ) + torch.log(__lowerCAmelCase ) return loss def _lowercase ( self : Union[str, Any] , UpperCAmelCase__ : List[Any] , UpperCAmelCase__ : Any , UpperCAmelCase__ : Tuple ) -> str: _a : Tuple = torch.randn_like(self.latent , requires_grad=__lowerCAmelCase , device=self.device ) _a : Any = torch.optim.Adam([vector] , lr=self.lr ) for i in range(self.iterations ): optim.zero_grad() _a : int = self._add_vector(__lowerCAmelCase ) _a : str = loop_post_process(__lowerCAmelCase ) _a : str = self._get_CLIP_loss(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) print("""CLIP loss""" , __lowerCAmelCase ) if self.log: wandb.log({"""CLIP Loss""": clip_loss} ) clip_loss.backward(retain_graph=__lowerCAmelCase ) optim.step() if self.return_val == "image": yield custom_to_pil(transformed_img[0] ) else: yield vector def _lowercase ( self : List[str] , UpperCAmelCase__ : str , UpperCAmelCase__ : Optional[int] , UpperCAmelCase__ : str ) -> Dict: wandb.init(reinit=__lowerCAmelCase , project="""face-editor""" ) wandb.config.update({"""Positive Prompts""": positive_prompts} ) wandb.config.update({"""Negative Prompts""": negative_prompts} ) wandb.config.update({"""lr""": self.lr, """iterations""": self.iterations} ) if image_path: _a : Tuple = Image.open(__lowerCAmelCase ) _a : Any = image.resize((256, 256) ) wandb.log("""Original Image""" , wandb.Image(__lowerCAmelCase ) ) def _lowercase ( self : List[Any] , UpperCAmelCase__ : Tuple ) -> Optional[int]: if not prompts: return [] _a : Optional[Any] = [] _a : List[str] = [] if isinstance(__lowerCAmelCase , __lowerCAmelCase ): _a : List[Any] = [prompt.strip() for prompt in prompts.split("""|""" )] for prompt in prompts: if isinstance(__lowerCAmelCase , (tuple, list) ): _a : Optional[int] = prompt[0] _a : Optional[int] = float(prompt[1] ) elif ":" in prompt: _a , _a : Optional[Any] = prompt.split(""":""" ) _a : Any = float(__lowerCAmelCase ) else: _a : Any = prompt _a : Dict = 1.0 processed_prompts.append(__lowerCAmelCase ) weights.append(__lowerCAmelCase ) return { "prompts": processed_prompts, "weights": torch.tensor(__lowerCAmelCase , device=self.device ), } def _lowercase ( self : Dict , UpperCAmelCase__ : Dict , UpperCAmelCase__ : Any=None , UpperCAmelCase__ : List[Any]=None , UpperCAmelCase__ : List[str]=True , UpperCAmelCase__ : Dict=False , UpperCAmelCase__ : Optional[Any]=True , UpperCAmelCase__ : Optional[Any]=True , UpperCAmelCase__ : Dict=None , ) -> Dict: if image_path: _a : List[str] = self._get_latent(__lowerCAmelCase ) else: _a : List[Any] = torch.randn(self.latent_dim , device=self.device ) if self.log: self._init_logging(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) assert pos_prompts, "You must provide at least one positive prompt." _a : List[Any] = self.process_prompts(__lowerCAmelCase ) _a : Union[str, Any] = self.process_prompts(__lowerCAmelCase ) if save_final and save_path is None: _a : Any = os.path.join("""./outputs/""" , """_""".join(pos_prompts["""prompts"""] ) ) if not os.path.exists(__lowerCAmelCase ): os.makedirs(__lowerCAmelCase ) else: _a : Dict = save_path + """_""" + get_timestamp() os.makedirs(__lowerCAmelCase ) _a : int = save_path _a : List[Any] = self.vqgan.decode(self.latent )[0] if show_intermediate: print("""Original Image""" ) show_pil(custom_to_pil(__lowerCAmelCase ) ) _a : Tuple = loop_post_process(__lowerCAmelCase ) for iter, transformed_img in enumerate(self._optimize_CLIP(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) ): if show_intermediate: show_pil(__lowerCAmelCase ) if save_intermediate: transformed_img.save(os.path.join(self.save_path , f"""iter_{iter:03d}.png""" ) ) if self.log: wandb.log({"""Image""": wandb.Image(__lowerCAmelCase )} ) if show_final: show_pil(__lowerCAmelCase ) if save_final: transformed_img.save(os.path.join(self.save_path , f"""iter_{iter:03d}_final.png""" ) )
294
from datetime import datetime import requests from bsa import BeautifulSoup if __name__ == "__main__": A : List[str] = input('''Enter image url: ''').strip() print(F'''Downloading image from {url} ...''') A : Any = BeautifulSoup(requests.get(url).content, '''html.parser''') # The image URL is in the content field of the first meta tag with property og:image A : List[Any] = soup.find('''meta''', {'''property''': '''og:image'''})['''content'''] A : Dict = requests.get(image_url).content A : Tuple = F'''{datetime.now():%Y-%m-%d_%H:%M:%S}.jpg''' with open(file_name, '''wb''') as fp: fp.write(image_data) print(F'''Done. Image saved to disk as {file_name}.''')
274
0
from manim import * class lowerCamelCase (_snake_case ): '''simple docstring''' def __UpperCAmelCase ( self ) -> Any: UpperCAmelCase_ : Tuple = Rectangle(height=0.5 , width=0.5 ) UpperCAmelCase_ : Optional[Any] = Rectangle(height=0.46 , width=0.46 ).set_stroke(width=0 ) UpperCAmelCase_ : Optional[Any] = Rectangle(height=0.25 , width=0.25 ) UpperCAmelCase_ : Dict = [mem.copy() for i in range(6 )] UpperCAmelCase_ : List[str] = [mem.copy() for i in range(6 )] UpperCAmelCase_ : List[Any] = VGroup(*__lowerCAmelCase ).arrange(__lowerCAmelCase , buff=0 ) UpperCAmelCase_ : List[Any] = VGroup(*__lowerCAmelCase ).arrange(__lowerCAmelCase , buff=0 ) UpperCAmelCase_ : int = VGroup(__lowerCAmelCase , __lowerCAmelCase ).arrange(__lowerCAmelCase , buff=0 ) UpperCAmelCase_ : Optional[Any] = Text('CPU' , font_size=2_4 ) UpperCAmelCase_ : Union[str, Any] = Group(__lowerCAmelCase , __lowerCAmelCase ).arrange(__lowerCAmelCase , buff=0.5 , aligned_edge=__lowerCAmelCase ) cpu.move_to([-2.5, -0.5, 0] ) self.add(__lowerCAmelCase ) UpperCAmelCase_ : str = [mem.copy() for i in range(4 )] UpperCAmelCase_ : List[str] = VGroup(*__lowerCAmelCase ).arrange(__lowerCAmelCase , buff=0 ) UpperCAmelCase_ : Any = Text('GPU' , font_size=2_4 ) UpperCAmelCase_ : int = Group(__lowerCAmelCase , __lowerCAmelCase ).arrange(__lowerCAmelCase , buff=0.5 , aligned_edge=__lowerCAmelCase ) gpu.move_to([-1, -1, 0] ) self.add(__lowerCAmelCase ) UpperCAmelCase_ : List[Any] = [mem.copy() for i in range(6 )] UpperCAmelCase_ : int = VGroup(*__lowerCAmelCase ).arrange(__lowerCAmelCase , buff=0 ) UpperCAmelCase_ : Any = Text('Model' , font_size=2_4 ) UpperCAmelCase_ : Optional[int] = Group(__lowerCAmelCase , __lowerCAmelCase ).arrange(__lowerCAmelCase , buff=0.5 , aligned_edge=__lowerCAmelCase ) model.move_to([3, -1.0, 0] ) self.add(__lowerCAmelCase ) UpperCAmelCase_ : str = [] UpperCAmelCase_ : Any = [] for i, rect in enumerate(__lowerCAmelCase ): UpperCAmelCase_ : Union[str, Any] = fill.copy().set_fill(__lowerCAmelCase , opacity=0.8 ) target.move_to(__lowerCAmelCase ) model_arr.append(__lowerCAmelCase ) UpperCAmelCase_ : Tuple = Rectangle(height=0.46 , width=0.46 ).set_stroke(width=0.0 ).set_fill(__lowerCAmelCase , opacity=0.8 ) cpu_target.move_to(cpu_left_col_base[i] ) model_cpu_arr.append(__lowerCAmelCase ) self.add(*__lowerCAmelCase , *__lowerCAmelCase ) UpperCAmelCase_ : str = [meta_mem.copy() for i in range(6 )] UpperCAmelCase_ : Tuple = [meta_mem.copy() for i in range(6 )] UpperCAmelCase_ : Dict = VGroup(*__lowerCAmelCase ).arrange(__lowerCAmelCase , buff=0 ) UpperCAmelCase_ : int = VGroup(*__lowerCAmelCase ).arrange(__lowerCAmelCase , buff=0 ) UpperCAmelCase_ : Any = VGroup(__lowerCAmelCase , __lowerCAmelCase ).arrange(__lowerCAmelCase , buff=0 ) UpperCAmelCase_ : str = Text('Disk' , font_size=2_4 ) UpperCAmelCase_ : List[str] = Group(__lowerCAmelCase , __lowerCAmelCase ).arrange(__lowerCAmelCase , buff=0.5 , aligned_edge=__lowerCAmelCase ) disk.move_to([-4, -1.25, 0] ) self.add(__lowerCAmelCase , __lowerCAmelCase ) UpperCAmelCase_ : List[Any] = Square(side_length=2.2 ) key.move_to([-5, 2, 0] ) UpperCAmelCase_ : List[Any] = 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(__lowerCAmelCase , __lowerCAmelCase ) UpperCAmelCase_ : List[str] = MarkupText( f"<span fgcolor=\'{BLUE}\'>●</span> Checkpoint" , font_size=1_8 , ) blue_text.next_to(__lowerCAmelCase , DOWN * 2.4 , aligned_edge=key_text.get_left() ) self.add(__lowerCAmelCase ) UpperCAmelCase_ : Any = 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(__lowerCAmelCase ) ) UpperCAmelCase_ : Tuple = Square(0.3 ) input.set_fill(__lowerCAmelCase , opacity=1.0 ) input.set_stroke(width=0.0 ) input.next_to(model_base[0] , __lowerCAmelCase , buff=0.5 ) self.play(Write(__lowerCAmelCase ) ) input.generate_target() input.target.next_to(model_arr[0] , direction=__lowerCAmelCase , buff=0.02 ) self.play(MoveToTarget(__lowerCAmelCase ) ) self.play(FadeOut(__lowerCAmelCase ) ) UpperCAmelCase_ : List[str] = Arrow(start=__lowerCAmelCase , end=__lowerCAmelCase , color=__lowerCAmelCase , buff=0.5 ) a.next_to(model_arr[0].get_left() , __lowerCAmelCase , buff=0.2 ) model_cpu_arr[0].generate_target() model_cpu_arr[0].target.move_to(gpu_rect[0] ) UpperCAmelCase_ : Any = 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(__lowerCAmelCase , run_time=3 ) ) UpperCAmelCase_ : Union[str, Any] = {'run_time': 1, 'fade_in': True, 'fade_out': True, 'buff': 0.02} self.play( Write(__lowerCAmelCase ) , Circumscribe(model_arr[0] , color=__lowerCAmelCase , **__lowerCAmelCase ) , Circumscribe(model_cpu_arr[0] , color=__lowerCAmelCase , **__lowerCAmelCase ) , Circumscribe(gpu_rect[0] , color=__lowerCAmelCase , **__lowerCAmelCase ) , ) self.play(MoveToTarget(model_cpu_arr[0] ) ) UpperCAmelCase_ : str = a.copy() for i in range(6 ): a_c.next_to(model_arr[i].get_right() + 0.02 , __lowerCAmelCase , buff=0.2 ) input.generate_target() input.target.move_to(model_arr[i].get_right() + 0.02 ) UpperCAmelCase_ : Dict = AnimationGroup( FadeOut(__lowerCAmelCase , run_time=0.5 ) , MoveToTarget(__lowerCAmelCase , run_time=0.5 ) , FadeIn(__lowerCAmelCase , run_time=0.5 ) , lag_ratio=0.2 ) self.play(__lowerCAmelCase ) 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: UpperCAmelCase_ : Union[str, Any] = 0.7 self.play( Circumscribe(model_arr[i] , **__lowerCAmelCase ) , Circumscribe(cpu_left_col_base[i] , **__lowerCAmelCase ) , Circumscribe(cpu_left_col_base[i + 1] , color=__lowerCAmelCase , **__lowerCAmelCase ) , Circumscribe(gpu_rect[0] , color=__lowerCAmelCase , **__lowerCAmelCase ) , Circumscribe(model_arr[i + 1] , color=__lowerCAmelCase , **__lowerCAmelCase ) , ) 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=__lowerCAmelCase , **__lowerCAmelCase ) , Circumscribe(cpu_left_col_base[-1] , color=__lowerCAmelCase , **__lowerCAmelCase ) , Circumscribe(gpu_rect[0] , color=__lowerCAmelCase , **__lowerCAmelCase ) , ) self.play(MoveToTarget(model_cpu_arr[i] ) ) UpperCAmelCase_ : Dict = a_c UpperCAmelCase_ : Union[str, Any] = a_c.copy() input.generate_target() input.target.next_to(model_base[-1] , RIGHT + 0.02 , buff=0.5 ) self.play( FadeOut(__lowerCAmelCase ) , FadeOut(__lowerCAmelCase , run_time=0.5 ) , ) UpperCAmelCase_ : int = 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(__lowerCAmelCase , run_time=3 ) , MoveToTarget(__lowerCAmelCase ) ) self.wait()
29
# This code is adapted from OpenAI's release # https://github.com/openai/human-eval/blob/master/human_eval/execution.py import contextlib import faulthandler import io import multiprocessing import os import platform import signal import tempfile def __lowerCamelCase ( __a :List[str] , __a :List[Any] , __a :Union[str, Any] , __a :List[Any] ) -> Dict: """simple docstring""" A__ = multiprocessing.Manager() A__ = manager.list() A__ = multiprocessing.Process(target=__a , args=(check_program, result, timeout) ) p.start() p.join(timeout=timeout + 1 ) if p.is_alive(): p.kill() if not result: result.append("""timed out""" ) return { "task_id": task_id, "passed": result[0] == "passed", "result": result[0], "completion_id": completion_id, } def __lowerCamelCase ( __a :Optional[Any] , __a :Any , __a :List[Any] ) -> Union[str, Any]: """simple docstring""" with create_tempdir(): # These system calls are needed when cleaning up tempdir. import os import shutil A__ = shutil.rmtree A__ = os.rmdir A__ = os.chdir # Disable functionalities that can make destructive changes to the test. reliability_guard() # Run program. try: A__ = {} with swallow_io(): with time_limit(__a ): exec(__a , __a ) result.append("""passed""" ) except TimeoutException: result.append("""timed out""" ) except BaseException as e: result.append(F'failed: {e}' ) # Needed for cleaning up. A__ = rmtree A__ = rmdir A__ = chdir @contextlib.contextmanager def __lowerCamelCase ( __a :List[str] ) -> Dict: """simple docstring""" def signal_handler(__a :List[Any] , __a :Optional[Any] ): raise TimeoutException("""Timed out!""" ) signal.setitimer(signal.ITIMER_REAL , __a ) signal.signal(signal.SIGALRM , __a ) try: yield finally: signal.setitimer(signal.ITIMER_REAL , 0 ) @contextlib.contextmanager def __lowerCamelCase ( ) -> Union[str, Any]: """simple docstring""" A__ = WriteOnlyStringIO() with contextlib.redirect_stdout(__a ): with contextlib.redirect_stderr(__a ): with redirect_stdin(__a ): yield @contextlib.contextmanager def __lowerCamelCase ( ) -> Dict: """simple docstring""" with tempfile.TemporaryDirectory() as dirname: with chdir(__a ): yield dirname class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' pass class A (io.StringIO ): '''simple docstring''' def a_ ( self : Any , *__lowerCAmelCase : List[str] , **__lowerCAmelCase : str ) -> Dict: """simple docstring""" raise OSError def a_ ( self : Optional[Any] , *__lowerCAmelCase : Any , **__lowerCAmelCase : Optional[int] ) -> str: """simple docstring""" raise OSError def a_ ( self : Optional[Any] , *__lowerCAmelCase : Any , **__lowerCAmelCase : Any ) -> int: """simple docstring""" raise OSError def a_ ( self : str , *__lowerCAmelCase : Any , **__lowerCAmelCase : Union[str, Any] ) -> int: """simple docstring""" return False class A (contextlib._RedirectStream ): # type: ignore '''simple docstring''' __lowerCamelCase : Union[str, Any] = '''stdin''' @contextlib.contextmanager def __lowerCamelCase ( __a :Union[str, Any] ) -> List[str]: """simple docstring""" if root == ".": yield return A__ = os.getcwd() os.chdir(__a ) try: yield except BaseException as exc: raise exc finally: os.chdir(__a ) def __lowerCamelCase ( __a :Union[str, Any]=None ) -> Dict: """simple docstring""" if maximum_memory_bytes is not None: import resource resource.setrlimit(resource.RLIMIT_AS , (maximum_memory_bytes, maximum_memory_bytes) ) resource.setrlimit(resource.RLIMIT_DATA , (maximum_memory_bytes, maximum_memory_bytes) ) if not platform.uname().system == "Darwin": resource.setrlimit(resource.RLIMIT_STACK , (maximum_memory_bytes, maximum_memory_bytes) ) faulthandler.disable() import builtins A__ = None A__ = None import os A__ = """1""" A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None import shutil A__ = None A__ = None A__ = None import subprocess A__ = None # type: ignore A__ = None import sys A__ = None A__ = None A__ = None A__ = None A__ = None
274
0
import argparse import os import torch from transformers import ( XLNetConfig, XLNetForQuestionAnswering, XLNetForSequenceClassification, XLNetLMHeadModel, load_tf_weights_in_xlnet, ) from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging a_ = { '''cola''': 2, '''mnli''': 3, '''mrpc''': 2, '''sst-2''': 2, '''sts-b''': 1, '''qqp''': 2, '''qnli''': 2, '''rte''': 2, '''wnli''': 2, } logging.set_verbosity_info() def _a ( UpperCamelCase_ : Union[str, Any] , UpperCamelCase_ : Tuple , UpperCamelCase_ : str , UpperCamelCase_ : Any=None ) -> Dict: """simple docstring""" lowerCAmelCase__ = XLNetConfig.from_json_file(__a ) lowerCAmelCase__ = finetuning_task.lower() if finetuning_task is not None else "" if finetuning_task in GLUE_TASKS_NUM_LABELS: print(F"Building PyTorch XLNetForSequenceClassification model from configuration: {config}" ) lowerCAmelCase__ = finetuning_task lowerCAmelCase__ = GLUE_TASKS_NUM_LABELS[finetuning_task] lowerCAmelCase__ = XLNetForSequenceClassification(__a ) elif "squad" in finetuning_task: lowerCAmelCase__ = finetuning_task lowerCAmelCase__ = XLNetForQuestionAnswering(__a ) else: lowerCAmelCase__ = XLNetLMHeadModel(__a ) # Load weights from tf checkpoint load_tf_weights_in_xlnet(__a , __a , __a ) # Save pytorch-model lowerCAmelCase__ = os.path.join(__a , __a ) lowerCAmelCase__ = os.path.join(__a , __a ) print(F"Save PyTorch model to {os.path.abspath(__a )}" ) torch.save(model.state_dict() , __a ) print(F"Save configuration file to {os.path.abspath(__a )}" ) with open(__a , "w" , encoding="utf-8" ) as f: f.write(config.to_json_string() ) if __name__ == "__main__": a_ = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--tf_checkpoint_path''', default=None, type=str, required=True, help='''Path to the TensorFlow checkpoint path.''' ) parser.add_argument( '''--xlnet_config_file''', default=None, type=str, required=True, help=( '''The config json file corresponding to the pre-trained XLNet model. \n''' '''This specifies the model architecture.''' ), ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, required=True, help='''Path to the folder to store the PyTorch model or dataset/vocab.''', ) parser.add_argument( '''--finetuning_task''', default=None, type=str, help='''Name of a task on which the XLNet TensorFlow model was fine-tuned''', ) a_ = parser.parse_args() print(args) convert_xlnet_checkpoint_to_pytorch( args.tf_checkpoint_path, args.xlnet_config_file, args.pytorch_dump_folder_path, args.finetuning_task )
340
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_albert import AlbertTokenizer else: A : Tuple = None A : Optional[Any] = logging.get_logger(__name__) A : Tuple = {'''vocab_file''': '''spiece.model''', '''tokenizer_file''': '''tokenizer.json'''} A : List[str] = { '''vocab_file''': { '''albert-base-v1''': '''https://huggingface.co/albert-base-v1/resolve/main/spiece.model''', '''albert-large-v1''': '''https://huggingface.co/albert-large-v1/resolve/main/spiece.model''', '''albert-xlarge-v1''': '''https://huggingface.co/albert-xlarge-v1/resolve/main/spiece.model''', '''albert-xxlarge-v1''': '''https://huggingface.co/albert-xxlarge-v1/resolve/main/spiece.model''', '''albert-base-v2''': '''https://huggingface.co/albert-base-v2/resolve/main/spiece.model''', '''albert-large-v2''': '''https://huggingface.co/albert-large-v2/resolve/main/spiece.model''', '''albert-xlarge-v2''': '''https://huggingface.co/albert-xlarge-v2/resolve/main/spiece.model''', '''albert-xxlarge-v2''': '''https://huggingface.co/albert-xxlarge-v2/resolve/main/spiece.model''', }, '''tokenizer_file''': { '''albert-base-v1''': '''https://huggingface.co/albert-base-v1/resolve/main/tokenizer.json''', '''albert-large-v1''': '''https://huggingface.co/albert-large-v1/resolve/main/tokenizer.json''', '''albert-xlarge-v1''': '''https://huggingface.co/albert-xlarge-v1/resolve/main/tokenizer.json''', '''albert-xxlarge-v1''': '''https://huggingface.co/albert-xxlarge-v1/resolve/main/tokenizer.json''', '''albert-base-v2''': '''https://huggingface.co/albert-base-v2/resolve/main/tokenizer.json''', '''albert-large-v2''': '''https://huggingface.co/albert-large-v2/resolve/main/tokenizer.json''', '''albert-xlarge-v2''': '''https://huggingface.co/albert-xlarge-v2/resolve/main/tokenizer.json''', '''albert-xxlarge-v2''': '''https://huggingface.co/albert-xxlarge-v2/resolve/main/tokenizer.json''', }, } A : List[str] = { '''albert-base-v1''': 5_1_2, '''albert-large-v1''': 5_1_2, '''albert-xlarge-v1''': 5_1_2, '''albert-xxlarge-v1''': 5_1_2, '''albert-base-v2''': 5_1_2, '''albert-large-v2''': 5_1_2, '''albert-xlarge-v2''': 5_1_2, '''albert-xxlarge-v2''': 5_1_2, } A : Optional[int] = '''▁''' class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : str = VOCAB_FILES_NAMES __lowerCamelCase : List[Any] = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase : Dict = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase : List[str] = AlbertTokenizer def __init__( self : Tuple , __lowerCAmelCase : Tuple=None , __lowerCAmelCase : List[str]=None , __lowerCAmelCase : Optional[Any]=True , __lowerCAmelCase : List[Any]=True , __lowerCAmelCase : str=False , __lowerCAmelCase : Union[str, Any]="[CLS]" , __lowerCAmelCase : int="[SEP]" , __lowerCAmelCase : Dict="<unk>" , __lowerCAmelCase : Dict="[SEP]" , __lowerCAmelCase : Union[str, Any]="<pad>" , __lowerCAmelCase : str="[CLS]" , __lowerCAmelCase : int="[MASK]" , **__lowerCAmelCase : Optional[Any] , ) -> Optional[Any]: """simple docstring""" A__ = ( AddedToken(__lowerCAmelCase , lstrip=__lowerCAmelCase , rstrip=__lowerCAmelCase , normalized=__lowerCAmelCase ) if isinstance(__lowerCAmelCase , __lowerCAmelCase ) else mask_token ) super().__init__( __lowerCAmelCase , tokenizer_file=__lowerCAmelCase , do_lower_case=__lowerCAmelCase , remove_space=__lowerCAmelCase , keep_accents=__lowerCAmelCase , bos_token=__lowerCAmelCase , eos_token=__lowerCAmelCase , unk_token=__lowerCAmelCase , sep_token=__lowerCAmelCase , pad_token=__lowerCAmelCase , cls_token=__lowerCAmelCase , mask_token=__lowerCAmelCase , **__lowerCAmelCase , ) A__ = do_lower_case A__ = remove_space A__ = keep_accents A__ = vocab_file A__ = False if not self.vocab_file else True def a_ ( self : List[Any] , __lowerCAmelCase : List[int] , __lowerCAmelCase : Optional[List[int]] = None ) -> List[int]: """simple docstring""" A__ = [self.sep_token_id] A__ = [self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def a_ ( self : Union[str, Any] , __lowerCAmelCase : List[int] , __lowerCAmelCase : Optional[List[int]] = None ) -> List[int]: """simple docstring""" A__ = [self.sep_token_id] A__ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def a_ ( self : Tuple , __lowerCAmelCase : str , __lowerCAmelCase : Optional[str] = None ) -> Tuple[str]: """simple docstring""" 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(__lowerCAmelCase ): logger.error(f'Vocabulary path ({save_directory}) should be a directory' ) return A__ = os.path.join( __lowerCAmelCase , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(__lowerCAmelCase ): copyfile(self.vocab_file , __lowerCAmelCase ) return (out_vocab_file,)
274
0
import inspect from typing import Callable, List, Optional, Union import torch from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTokenizer from diffusers import DiffusionPipeline from diffusers.models import AutoencoderKL, UNetaDConditionModel from diffusers.pipelines.stable_diffusion import StableDiffusionPipelineOutput from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker from diffusers.schedulers import DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler from diffusers.utils import logging _A = logging.get_logger(__name__) # pylint: disable=invalid-name class lowercase_ ( __SCREAMING_SNAKE_CASE ): def __init__( self , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , ): """simple docstring""" super().__init__() self.register_modules( vae=__lowerCAmelCase , text_encoder=__lowerCAmelCase , tokenizer=__lowerCAmelCase , unet=__lowerCAmelCase , scheduler=__lowerCAmelCase , safety_checker=__lowerCAmelCase , feature_extractor=__lowerCAmelCase , ) def lowerCamelCase_ ( self , __UpperCamelCase = "auto" ): """simple docstring""" if slice_size == "auto": # half the attention head size is usually a good trade-off between # speed and memory UpperCamelCase_ = self.unet.config.attention_head_dim // 2 self.unet.set_attention_slice(__lowerCAmelCase ) def lowerCamelCase_ ( self ): """simple docstring""" self.enable_attention_slicing(__lowerCAmelCase ) @torch.no_grad() def __call__( self , __UpperCamelCase , __UpperCamelCase = 5_1_2 , __UpperCamelCase = 5_1_2 , __UpperCamelCase = 5_0 , __UpperCamelCase = 7.5 , __UpperCamelCase = None , __UpperCamelCase = 1 , __UpperCamelCase = 0.0 , __UpperCamelCase = None , __UpperCamelCase = None , __UpperCamelCase = "pil" , __UpperCamelCase = True , __UpperCamelCase = None , __UpperCamelCase = 1 , __UpperCamelCase = None , **__UpperCamelCase , ): """simple docstring""" if isinstance(__lowerCAmelCase , __lowerCAmelCase ): UpperCamelCase_ = 1 elif isinstance(__lowerCAmelCase , __lowerCAmelCase ): UpperCamelCase_ = len(__lowerCAmelCase ) else: raise ValueError(f'''`prompt` has to be of type `str` or `list` but is {type(__lowerCAmelCase )}''' ) if height % 8 != 0 or width % 8 != 0: raise ValueError(f'''`height` and `width` have to be divisible by 8 but are {height} and {width}.''' ) if (callback_steps is None) or ( callback_steps is not None and (not isinstance(__lowerCAmelCase , __lowerCAmelCase ) or callback_steps <= 0) ): raise ValueError( f'''`callback_steps` has to be a positive integer but is {callback_steps} of type''' f''' {type(__lowerCAmelCase )}.''' ) # get prompt text embeddings UpperCamelCase_ = self.tokenizer( __lowerCAmelCase , padding="""max_length""" , max_length=self.tokenizer.model_max_length , return_tensors="""pt""" , ) UpperCamelCase_ = text_inputs.input_ids if text_input_ids.shape[-1] > self.tokenizer.model_max_length: UpperCamelCase_ = 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}''' ) UpperCamelCase_ = text_input_ids[:, : self.tokenizer.model_max_length] if text_embeddings is None: UpperCamelCase_ = self.text_encoder(text_input_ids.to(self.device ) )[0] # duplicate text embeddings for each generation per prompt, using mps friendly method UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ = text_embeddings.shape UpperCamelCase_ = text_embeddings.repeat(1 , __lowerCAmelCase , 1 ) UpperCamelCase_ = text_embeddings.view(bs_embed * num_images_per_prompt , __lowerCAmelCase , -1 ) # here `guidance_scale` is defined analog to the guidance weight `w` of equation (2) # of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1` # corresponds to doing no classifier free guidance. UpperCamelCase_ = guidance_scale > 1.0 # get unconditional embeddings for classifier free guidance if do_classifier_free_guidance: UpperCamelCase_ = 4_2 if negative_prompt is None: UpperCamelCase_ = [""""""] elif type(__lowerCAmelCase ) is not type(__lowerCAmelCase ): raise TypeError( f'''`negative_prompt` should be the same type to `prompt`, but got {type(__lowerCAmelCase )} !=''' f''' {type(__lowerCAmelCase )}.''' ) elif isinstance(__lowerCAmelCase , __lowerCAmelCase ): UpperCamelCase_ = [negative_prompt] elif batch_size != len(__lowerCAmelCase ): raise ValueError( f'''`negative_prompt`: {negative_prompt} has batch size {len(__lowerCAmelCase )}, but `prompt`:''' f''' {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches''' """ the batch size of `prompt`.""" ) else: UpperCamelCase_ = negative_prompt UpperCamelCase_ = text_input_ids.shape[-1] UpperCamelCase_ = self.tokenizer( __lowerCAmelCase , padding="""max_length""" , max_length=__lowerCAmelCase , truncation=__lowerCAmelCase , return_tensors="""pt""" , ) UpperCamelCase_ = self.text_encoder(uncond_input.input_ids.to(self.device ) )[0] # duplicate unconditional embeddings for each generation per prompt, using mps friendly method UpperCamelCase_ = uncond_embeddings.shape[1] UpperCamelCase_ = uncond_embeddings.repeat(__lowerCAmelCase , __lowerCAmelCase , 1 ) UpperCamelCase_ = uncond_embeddings.view(batch_size * num_images_per_prompt , __lowerCAmelCase , -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 UpperCamelCase_ = torch.cat([uncond_embeddings, text_embeddings] ) # get the initial random noise unless the user supplied it # Unlike in other pipelines, latents need to be generated in the target device # for 1-to-1 results reproducibility with the CompVis implementation. # However this currently doesn't work in `mps`. UpperCamelCase_ = (batch_size * num_images_per_prompt, self.unet.config.in_channels, height // 8, width // 8) UpperCamelCase_ = (batch_size * num_images_per_prompt, self.unet.config.in_channels, 6_4, 6_4) UpperCamelCase_ = text_embeddings.dtype if latents is None: if self.device.type == "mps": # randn does not exist on mps UpperCamelCase_ = torch.randn( __lowerCAmelCase , generator=__lowerCAmelCase , device="""cpu""" , dtype=__lowerCAmelCase ).to(self.device ) UpperCamelCase_ = torch.randn(__lowerCAmelCase , generator=__lowerCAmelCase , device="""cpu""" , dtype=__lowerCAmelCase ).to( self.device ) else: UpperCamelCase_ = torch.randn( __lowerCAmelCase , generator=__lowerCAmelCase , device=self.device , dtype=__lowerCAmelCase ) UpperCamelCase_ = torch.randn(__lowerCAmelCase , generator=__lowerCAmelCase , device=self.device , dtype=__lowerCAmelCase ) else: if latents_reference.shape != latents_shape: raise ValueError(f'''Unexpected latents shape, got {latents.shape}, expected {latents_shape}''' ) UpperCamelCase_ = latents_reference.to(self.device ) UpperCamelCase_ = latents.to(self.device ) # This is the key part of the pipeline where we # try to ensure that the generated images w/ the same seed # but different sizes actually result in similar images UpperCamelCase_ = (latents_shape[3] - latents_shape_reference[3]) // 2 UpperCamelCase_ = (latents_shape[2] - latents_shape_reference[2]) // 2 UpperCamelCase_ = latents_shape_reference[3] if dx >= 0 else latents_shape_reference[3] + 2 * dx UpperCamelCase_ = latents_shape_reference[2] if dy >= 0 else latents_shape_reference[2] + 2 * dy UpperCamelCase_ = 0 if dx < 0 else dx UpperCamelCase_ = 0 if dy < 0 else dy UpperCamelCase_ = max(-dx , 0 ) UpperCamelCase_ = max(-dy , 0 ) # import pdb # pdb.set_trace() UpperCamelCase_ = latents_reference[:, :, dy : dy + h, dx : dx + w] # set timesteps self.scheduler.set_timesteps(__lowerCAmelCase ) # Some schedulers like PNDM have timesteps as arrays # It's more optimized to move all timesteps to correct device beforehand UpperCamelCase_ = self.scheduler.timesteps.to(self.device ) # scale the initial noise by the standard deviation required by the scheduler UpperCamelCase_ = latents * self.scheduler.init_noise_sigma # prepare extra kwargs for the scheduler step, since not all schedulers have the same signature # eta (η) is only used with the DDIMScheduler, it will be ignored for other schedulers. # eta corresponds to η in DDIM paper: https://arxiv.org/abs/2010.02502 # and should be between [0, 1] UpperCamelCase_ = """eta""" in set(inspect.signature(self.scheduler.step ).parameters.keys() ) UpperCamelCase_ = {} if accepts_eta: UpperCamelCase_ = eta for i, t in enumerate(self.progress_bar(__lowerCAmelCase ) ): # expand the latents if we are doing classifier free guidance UpperCamelCase_ = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents UpperCamelCase_ = self.scheduler.scale_model_input(__lowerCAmelCase , __lowerCAmelCase ) # predict the noise residual UpperCamelCase_ = self.unet(__lowerCAmelCase , __lowerCAmelCase , encoder_hidden_states=__lowerCAmelCase ).sample # perform guidance if do_classifier_free_guidance: UpperCamelCase_ , UpperCamelCase_ = noise_pred.chunk(2 ) UpperCamelCase_ = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) # compute the previous noisy sample x_t -> x_t-1 UpperCamelCase_ = self.scheduler.step(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , **__lowerCAmelCase ).prev_sample # call the callback, if provided if callback is not None and i % callback_steps == 0: callback(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) UpperCamelCase_ = 1 / 0.18_215 * latents UpperCamelCase_ = self.vae.decode(__lowerCAmelCase ).sample UpperCamelCase_ = (image / 2 + 0.5).clamp(0 , 1 ) # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16 UpperCamelCase_ = image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() if self.safety_checker is not None: UpperCamelCase_ = self.feature_extractor(self.numpy_to_pil(__lowerCAmelCase ) , return_tensors="""pt""" ).to( self.device ) UpperCamelCase_ , UpperCamelCase_ = self.safety_checker( images=__lowerCAmelCase , clip_input=safety_checker_input.pixel_values.to(text_embeddings.dtype ) ) else: UpperCamelCase_ = None if output_type == "pil": UpperCamelCase_ = self.numpy_to_pil(__lowerCAmelCase ) if not return_dict: return (image, has_nsfw_concept) return StableDiffusionPipelineOutput(images=__lowerCAmelCase , nsfw_content_detected=__lowerCAmelCase )
122
import dataclasses import json import warnings from dataclasses import dataclass, field from time import time from typing import List from ..utils import logging A : Dict = logging.get_logger(__name__) def __lowerCamelCase ( __a :int=None , __a :Optional[Any]=None ) -> int: """simple docstring""" return field(default_factory=lambda: default , metadata=__a ) @dataclass class A : '''simple docstring''' __lowerCamelCase : List[str] = list_field( default=[] , metadata={ '''help''': ( '''Model checkpoints to be provided to the AutoModel classes. Leave blank to benchmark the base version''' ''' of all available models''' ) } , ) __lowerCamelCase : List[int] = list_field( default=[8] , metadata={'''help''': '''List of batch sizes for which memory and time performance will be evaluated'''} ) __lowerCamelCase : List[int] = list_field( default=[8, 32, 128, 512] , metadata={'''help''': '''List of sequence lengths for which memory and time performance will be evaluated'''} , ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Whether to benchmark inference of model. Inference can be disabled via --no-inference.'''} , ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Whether to run on available cuda devices. Cuda can be disabled via --no-cuda.'''} , ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Whether to run on available tpu devices. TPU can be disabled via --no-tpu.'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Use FP16 to accelerate inference.'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Benchmark training of model'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Verbose memory tracing'''} ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Whether to perform speed measurements. Speed measurements can be disabled via --no-speed.'''} , ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={ '''help''': '''Whether to perform memory measurements. Memory measurements can be disabled via --no-memory''' } , ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Trace memory line by line'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Save result to a CSV file'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Save all print statements in a log file'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Whether to print environment information'''} ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={ '''help''': ( '''Whether to use multiprocessing for memory and speed measurement. It is highly recommended to use''' ''' multiprocessing for accurate CPU and GPU memory measurements. This option should only be disabled''' ''' for debugging / testing and on TPU.''' ) } , ) __lowerCamelCase : str = field( default=F'''inference_time_{round(time() )}.csv''' , metadata={'''help''': '''CSV filename used if saving time results to csv.'''} , ) __lowerCamelCase : str = field( default=F'''inference_memory_{round(time() )}.csv''' , metadata={'''help''': '''CSV filename used if saving memory results to csv.'''} , ) __lowerCamelCase : str = field( default=F'''train_time_{round(time() )}.csv''' , metadata={'''help''': '''CSV filename used if saving time results to csv for training.'''} , ) __lowerCamelCase : str = field( default=F'''train_memory_{round(time() )}.csv''' , metadata={'''help''': '''CSV filename used if saving memory results to csv for training.'''} , ) __lowerCamelCase : str = field( default=F'''env_info_{round(time() )}.csv''' , metadata={'''help''': '''CSV filename used if saving environment information.'''} , ) __lowerCamelCase : str = field( default=F'''log_{round(time() )}.csv''' , metadata={'''help''': '''Log filename used if print statements are saved in log.'''} , ) __lowerCamelCase : int = field(default=3 , metadata={'''help''': '''Times an experiment will be run.'''} ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={ '''help''': ( '''Instead of loading the model as defined in `config.architectures` if exists, just load the pretrain''' ''' model weights.''' ) } , ) def a_ ( self : Dict ) -> Union[str, Any]: """simple docstring""" warnings.warn( f'The class {self.__class__} is deprecated. Hugging Face Benchmarking utils' """ are deprecated in general and it is advised to use external Benchmarking libraries """ """ to benchmark Transformer models.""" , __lowerCAmelCase , ) def a_ ( self : Union[str, Any] ) -> List[str]: """simple docstring""" return json.dumps(dataclasses.asdict(self ) , indent=2 ) @property def a_ ( self : Tuple ) -> List[str]: """simple docstring""" if len(self.models ) <= 0: raise ValueError( """Please make sure you provide at least one model name / model identifier, *e.g.* `--models""" """ bert-base-cased` or `args.models = ['bert-base-cased'].""" ) return self.models @property def a_ ( self : Union[str, Any] ) -> Optional[Any]: """simple docstring""" if not self.multi_process: return False elif self.is_tpu: logger.info("""Multiprocessing is currently not possible on TPU.""" ) return False else: return True
274
0
# This code is adapted from OpenAI's release # https://github.com/openai/human-eval/blob/master/human_eval/execution.py import contextlib import faulthandler import io import multiprocessing import os import platform import signal import tempfile def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case , __snake_case ) -> Dict: """simple docstring""" _lowercase =multiprocessing.Manager() _lowercase =manager.list() _lowercase =multiprocessing.Process(target=__a , args=(check_program, result, timeout) ) p.start() p.join(timeout=timeout + 1 ) if p.is_alive(): p.kill() if not result: result.append('''timed out''' ) return { "task_id": task_id, "passed": result[0] == "passed", "result": result[0], "completion_id": completion_id, } def UpperCAmelCase_ ( __snake_case , __snake_case , __snake_case ) -> Union[str, Any]: """simple docstring""" with create_tempdir(): # These system calls are needed when cleaning up tempdir. import os import shutil _lowercase =shutil.rmtree _lowercase =os.rmdir _lowercase =os.chdir # Disable functionalities that can make destructive changes to the test. reliability_guard() # Run program. try: _lowercase ={} with swallow_io(): with time_limit(__a ): exec(__a , __a ) result.append('''passed''' ) except TimeoutException: result.append('''timed out''' ) except BaseException as e: result.append(F"failed: {e}" ) # Needed for cleaning up. _lowercase =rmtree _lowercase =rmdir _lowercase =chdir @contextlib.contextmanager def UpperCAmelCase_ ( __snake_case ) -> Dict: """simple docstring""" def signal_handler(__snake_case , __snake_case ): raise TimeoutException('''Timed out!''' ) signal.setitimer(signal.ITIMER_REAL , __a ) signal.signal(signal.SIGALRM , __a ) try: yield finally: signal.setitimer(signal.ITIMER_REAL , 0 ) @contextlib.contextmanager def UpperCAmelCase_ ( ) -> Union[str, Any]: """simple docstring""" _lowercase =WriteOnlyStringIO() with contextlib.redirect_stdout(__a ): with contextlib.redirect_stderr(__a ): with redirect_stdin(__a ): yield @contextlib.contextmanager def UpperCAmelCase_ ( ) -> Dict: """simple docstring""" with tempfile.TemporaryDirectory() as dirname: with chdir(__a ): yield dirname class lowerCamelCase__ ( lowerCAmelCase): pass class lowerCamelCase__ ( io.StringIO): def __A (self , *UpperCAmelCase , **UpperCAmelCase ) -> Dict: raise OSError def __A (self , *UpperCAmelCase , **UpperCAmelCase ) -> str: raise OSError def __A (self , *UpperCAmelCase , **UpperCAmelCase ) -> int: raise OSError def __A (self , *UpperCAmelCase , **UpperCAmelCase ) -> int: return False class lowerCamelCase__ ( contextlib._RedirectStream): # type: ignore SCREAMING_SNAKE_CASE__ = '''stdin''' @contextlib.contextmanager def UpperCAmelCase_ ( __snake_case ) -> List[str]: """simple docstring""" if root == ".": yield return _lowercase =os.getcwd() os.chdir(__a ) try: yield except BaseException as exc: raise exc finally: os.chdir(__a ) def UpperCAmelCase_ ( __snake_case=None ) -> Dict: """simple docstring""" if maximum_memory_bytes is not None: import resource resource.setrlimit(resource.RLIMIT_AS , (maximum_memory_bytes, maximum_memory_bytes) ) resource.setrlimit(resource.RLIMIT_DATA , (maximum_memory_bytes, maximum_memory_bytes) ) if not platform.uname().system == "Darwin": resource.setrlimit(resource.RLIMIT_STACK , (maximum_memory_bytes, maximum_memory_bytes) ) faulthandler.disable() import builtins _lowercase =None _lowercase =None import os _lowercase ='''1''' _lowercase =None _lowercase =None _lowercase =None _lowercase =None _lowercase =None _lowercase =None _lowercase =None _lowercase =None _lowercase =None _lowercase =None _lowercase =None _lowercase =None _lowercase =None _lowercase =None _lowercase =None _lowercase =None _lowercase =None _lowercase =None _lowercase =None _lowercase =None _lowercase =None _lowercase =None _lowercase =None _lowercase =None _lowercase =None _lowercase =None _lowercase =None import shutil _lowercase =None _lowercase =None _lowercase =None import subprocess _lowercase =None # type: ignore _lowercase =None import sys _lowercase =None _lowercase =None _lowercase =None _lowercase =None _lowercase =None
5
from math import ceil def __lowerCamelCase ( __a :int = 1_0_0_1 ) -> int: """simple docstring""" A__ = 1 for i in range(1 , int(ceil(n / 2.0 ) ) ): A__ = 2 * i + 1 A__ = 2 * i A__ = total + 4 * odd**2 - 6 * even return total if __name__ == "__main__": import sys if len(sys.argv) == 1: print(solution()) else: try: A : List[str] = int(sys.argv[1]) print(solution(n)) except ValueError: print('''Invalid entry - please enter a number''')
274
0
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging _SCREAMING_SNAKE_CASE = logging.get_logger(__name__) _SCREAMING_SNAKE_CASE = { '''google/fnet-base''': '''https://huggingface.co/google/fnet-base/resolve/main/config.json''', '''google/fnet-large''': '''https://huggingface.co/google/fnet-large/resolve/main/config.json''' # See all FNet models at https://huggingface.co/models?filter=fnet } class lowerCAmelCase_ ( __magic_name__ ): __lowerCamelCase : List[Any] = '''fnet''' def __init__( self , _lowerCAmelCase=32000 , _lowerCAmelCase=768 , _lowerCAmelCase=12 , _lowerCAmelCase=3072 , _lowerCAmelCase="gelu_new" , _lowerCAmelCase=0.1 , _lowerCAmelCase=512 , _lowerCAmelCase=4 , _lowerCAmelCase=0.02 , _lowerCAmelCase=1E-12 , _lowerCAmelCase=False , _lowerCAmelCase=512 , _lowerCAmelCase=3 , _lowerCAmelCase=1 , _lowerCAmelCase=2 , **_lowerCAmelCase , ) -> Optional[Any]: super().__init__(pad_token_id=__lowerCAmelCase , bos_token_id=__lowerCAmelCase , eos_token_id=__lowerCAmelCase , **__lowerCAmelCase ) _lowerCAmelCase = vocab_size _lowerCAmelCase = max_position_embeddings _lowerCAmelCase = hidden_size _lowerCAmelCase = num_hidden_layers _lowerCAmelCase = intermediate_size _lowerCAmelCase = hidden_act _lowerCAmelCase = hidden_dropout_prob _lowerCAmelCase = initializer_range _lowerCAmelCase = type_vocab_size _lowerCAmelCase = layer_norm_eps _lowerCAmelCase = use_tpu_fourier_optimizations _lowerCAmelCase = tpu_short_seq_length
158
import argparse import csv import logging import os import random import numpy as np import torch from torch.utils.data import DataLoader, RandomSampler, SequentialSampler, TensorDataset from tqdm import tqdm, trange from transformers import ( CONFIG_NAME, WEIGHTS_NAME, AdamW, OpenAIGPTDoubleHeadsModel, OpenAIGPTTokenizer, get_linear_schedule_with_warmup, ) logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''', datefmt='''%m/%d/%Y %H:%M:%S''', level=logging.INFO ) A : Tuple = logging.getLogger(__name__) def __lowerCamelCase ( __a :Optional[int] , __a :List[str] ) -> Tuple: """simple docstring""" A__ = np.argmax(__a , axis=1 ) return np.sum(outputs == labels ) def __lowerCamelCase ( __a :Tuple ) -> Dict: """simple docstring""" with open(__a , encoding="""utf_8""" ) as f: A__ = csv.reader(__a ) A__ = [] next(__a ) # skip the first line for line in tqdm(__a ): output.append((""" """.join(line[1:5] ), line[5], line[6], int(line[-1] ) - 1) ) return output def __lowerCamelCase ( __a :Optional[int] , __a :List[Any] , __a :Dict , __a :Optional[Any] , __a :Optional[Any] , __a :int ) -> Union[str, Any]: """simple docstring""" A__ = [] for dataset in encoded_datasets: A__ = len(__a ) A__ = np.zeros((n_batch, 2, input_len) , dtype=np.intaa ) A__ = np.zeros((n_batch, 2) , dtype=np.intaa ) A__ = np.full((n_batch, 2, input_len) , fill_value=-1_0_0 , dtype=np.intaa ) A__ = np.zeros((n_batch,) , dtype=np.intaa ) for ( i, (story, conta, conta, mc_label), ) in enumerate(__a ): A__ = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] A__ = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] A__ = with_conta A__ = with_conta A__ = len(__a ) - 1 A__ = len(__a ) - 1 A__ = with_conta A__ = with_conta A__ = mc_label A__ = (input_ids, mc_token_ids, lm_labels, mc_labels) tensor_datasets.append(tuple(torch.tensor(__a ) for t in all_inputs ) ) return tensor_datasets def __lowerCamelCase ( ) -> Union[str, Any]: """simple docstring""" A__ = argparse.ArgumentParser() parser.add_argument("""--model_name""" , type=__a , default="""openai-gpt""" , help="""pretrained model name""" ) parser.add_argument("""--do_train""" , action="""store_true""" , help="""Whether to run training.""" ) parser.add_argument("""--do_eval""" , action="""store_true""" , help="""Whether to run eval on the dev set.""" ) parser.add_argument( """--output_dir""" , default=__a , type=__a , required=__a , help="""The output directory where the model predictions and checkpoints will be written.""" , ) parser.add_argument("""--train_dataset""" , type=__a , default="""""" ) parser.add_argument("""--eval_dataset""" , type=__a , default="""""" ) parser.add_argument("""--seed""" , type=__a , default=4_2 ) parser.add_argument("""--num_train_epochs""" , type=__a , default=3 ) parser.add_argument("""--train_batch_size""" , type=__a , default=8 ) parser.add_argument("""--eval_batch_size""" , type=__a , default=1_6 ) parser.add_argument("""--adam_epsilon""" , default=1E-8 , type=__a , help="""Epsilon for Adam optimizer.""" ) parser.add_argument("""--max_grad_norm""" , type=__a , default=1 ) parser.add_argument( """--max_steps""" , default=-1 , type=__a , help=( """If > 0: set total number of training steps to perform. Override num_train_epochs.""" ) , ) parser.add_argument( """--gradient_accumulation_steps""" , type=__a , default=1 , help="""Number of updates steps to accumulate before performing a backward/update pass.""" , ) parser.add_argument("""--learning_rate""" , type=__a , default=6.25E-5 ) parser.add_argument("""--warmup_steps""" , default=0 , type=__a , help="""Linear warmup over warmup_steps.""" ) parser.add_argument("""--lr_schedule""" , type=__a , default="""warmup_linear""" ) parser.add_argument("""--weight_decay""" , type=__a , default=0.01 ) parser.add_argument("""--lm_coef""" , type=__a , default=0.9 ) parser.add_argument("""--n_valid""" , type=__a , default=3_7_4 ) parser.add_argument("""--server_ip""" , type=__a , default="""""" , help="""Can be used for distant debugging.""" ) parser.add_argument("""--server_port""" , type=__a , default="""""" , help="""Can be used for distant debugging.""" ) A__ = parser.parse_args() print(__a ) if args.server_ip and args.server_port: # Distant debugging - see https://code.visualstudio.com/docs/python/debugging#_attach-to-a-local-script import ptvsd print("""Waiting for debugger attach""" ) ptvsd.enable_attach(address=(args.server_ip, args.server_port) , redirect_output=__a ) ptvsd.wait_for_attach() random.seed(args.seed ) np.random.seed(args.seed ) torch.manual_seed(args.seed ) torch.cuda.manual_seed_all(args.seed ) A__ = torch.device("""cuda""" if torch.cuda.is_available() else """cpu""" ) A__ = torch.cuda.device_count() logger.info("""device: {}, n_gpu {}""".format(__a , __a ) ) if not args.do_train and not args.do_eval: raise ValueError("""At least one of `do_train` or `do_eval` must be True.""" ) if not os.path.exists(args.output_dir ): os.makedirs(args.output_dir ) # Load tokenizer and model # This loading functions also add new tokens and embeddings called `special tokens` # These new embeddings will be fine-tuned on the RocStories dataset A__ = ["""_start_""", """_delimiter_""", """_classify_"""] A__ = OpenAIGPTTokenizer.from_pretrained(args.model_name ) tokenizer.add_tokens(__a ) A__ = tokenizer.convert_tokens_to_ids(__a ) A__ = OpenAIGPTDoubleHeadsModel.from_pretrained(args.model_name ) model.resize_token_embeddings(len(__a ) ) model.to(__a ) # Load and encode the datasets def tokenize_and_encode(__a :Tuple ): if isinstance(__a , __a ): return tokenizer.convert_tokens_to_ids(tokenizer.tokenize(__a ) ) elif isinstance(__a , __a ): return obj return [tokenize_and_encode(__a ) for o in obj] logger.info("""Encoding dataset...""" ) A__ = load_rocstories_dataset(args.train_dataset ) A__ = load_rocstories_dataset(args.eval_dataset ) A__ = (train_dataset, eval_dataset) A__ = tokenize_and_encode(__a ) # Compute the max input length for the Transformer A__ = model.config.n_positions // 2 - 2 A__ = max( len(story[:max_length] ) + max(len(conta[:max_length] ) , len(conta[:max_length] ) ) + 3 for dataset in encoded_datasets for story, conta, conta, _ in dataset ) A__ = min(__a , model.config.n_positions ) # Max size of input for the pre-trained model # Prepare inputs tensors and dataloaders A__ = pre_process_datasets(__a , __a , __a , *__a ) A__ , A__ = tensor_datasets[0], tensor_datasets[1] A__ = TensorDataset(*__a ) A__ = RandomSampler(__a ) A__ = DataLoader(__a , sampler=__a , batch_size=args.train_batch_size ) A__ = TensorDataset(*__a ) A__ = SequentialSampler(__a ) A__ = DataLoader(__a , sampler=__a , batch_size=args.eval_batch_size ) # Prepare optimizer if args.do_train: if args.max_steps > 0: A__ = args.max_steps A__ = args.max_steps // (len(__a ) // args.gradient_accumulation_steps) + 1 else: A__ = len(__a ) // args.gradient_accumulation_steps * args.num_train_epochs A__ = list(model.named_parameters() ) A__ = ["""bias""", """LayerNorm.bias""", """LayerNorm.weight"""] A__ = [ { """params""": [p for n, p in param_optimizer if not any(nd in n for nd in no_decay )], """weight_decay""": args.weight_decay, }, {"""params""": [p for n, p in param_optimizer if any(nd in n for nd in no_decay )], """weight_decay""": 0.0}, ] A__ = AdamW(__a , lr=args.learning_rate , eps=args.adam_epsilon ) A__ = get_linear_schedule_with_warmup( __a , num_warmup_steps=args.warmup_steps , num_training_steps=__a ) if args.do_train: A__ , A__ , A__ = 0, 0, None model.train() for _ in trange(int(args.num_train_epochs ) , desc="""Epoch""" ): A__ = 0 A__ = 0 A__ = tqdm(__a , desc="""Training""" ) for step, batch in enumerate(__a ): A__ = tuple(t.to(__a ) for t in batch ) A__ , A__ , A__ , A__ = batch A__ = model(__a , mc_token_ids=__a , lm_labels=__a , mc_labels=__a ) A__ = args.lm_coef * losses[0] + losses[1] loss.backward() optimizer.step() scheduler.step() optimizer.zero_grad() tr_loss += loss.item() A__ = ( loss.item() if exp_average_loss is None else 0.7 * exp_average_loss + 0.3 * loss.item() ) nb_tr_steps += 1 A__ = """Training loss: {:.2e} lr: {:.2e}""".format(__a , scheduler.get_lr()[0] ) # Save a trained model if args.do_train: # Save a trained model, configuration and tokenizer A__ = model.module if hasattr(__a , """module""" ) else model # Only save the model itself # If we save using the predefined names, we can load using `from_pretrained` A__ = os.path.join(args.output_dir , __a ) A__ = os.path.join(args.output_dir , __a ) torch.save(model_to_save.state_dict() , __a ) model_to_save.config.to_json_file(__a ) tokenizer.save_vocabulary(args.output_dir ) # Load a trained model and vocabulary that you have fine-tuned A__ = OpenAIGPTDoubleHeadsModel.from_pretrained(args.output_dir ) A__ = OpenAIGPTTokenizer.from_pretrained(args.output_dir ) model.to(__a ) if args.do_eval: model.eval() A__ , A__ = 0, 0 A__ , A__ = 0, 0 for batch in tqdm(__a , desc="""Evaluating""" ): A__ = tuple(t.to(__a ) for t in batch ) A__ , A__ , A__ , A__ = batch with torch.no_grad(): A__ , A__ , A__ , A__ = model( __a , mc_token_ids=__a , lm_labels=__a , mc_labels=__a ) A__ = mc_logits.detach().cpu().numpy() A__ = mc_labels.to("""cpu""" ).numpy() A__ = accuracy(__a , __a ) eval_loss += mc_loss.mean().item() eval_accuracy += tmp_eval_accuracy nb_eval_examples += input_ids.size(0 ) nb_eval_steps += 1 A__ = eval_loss / nb_eval_steps A__ = eval_accuracy / nb_eval_examples A__ = tr_loss / nb_tr_steps if args.do_train else None A__ = {"""eval_loss""": eval_loss, """eval_accuracy""": eval_accuracy, """train_loss""": train_loss} A__ = os.path.join(args.output_dir , """eval_results.txt""" ) with open(__a , """w""" ) as writer: logger.info("""***** Eval results *****""" ) for key in sorted(result.keys() ): logger.info(""" %s = %s""" , __a , str(result[key] ) ) writer.write("""%s = %s\n""" % (key, str(result[key] )) ) if __name__ == "__main__": main()
274
0
import unittest from knapsack import knapsack as k class __A ( unittest.TestCase ): '''simple docstring''' def __lowerCamelCase ( self ): '''simple docstring''' lowerCamelCase__ = 0 lowerCamelCase__ = [0] lowerCamelCase__ = [0] lowerCamelCase__ = len(__lowerCAmelCase ) self.assertEqual(k.knapsack(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) , 0 ) lowerCamelCase__ = [6_0] lowerCamelCase__ = [1_0] lowerCamelCase__ = len(__lowerCAmelCase ) self.assertEqual(k.knapsack(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) , 0 ) def __lowerCamelCase ( self ): '''simple docstring''' lowerCamelCase__ = 3 lowerCamelCase__ = [1, 2, 3] lowerCamelCase__ = [3, 2, 1] lowerCamelCase__ = len(__lowerCAmelCase ) self.assertEqual(k.knapsack(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) , 5 ) def __lowerCamelCase ( self ): '''simple docstring''' lowerCamelCase__ = 5_0 lowerCamelCase__ = [6_0, 1_0_0, 1_2_0] lowerCamelCase__ = [1_0, 2_0, 3_0] lowerCamelCase__ = len(__lowerCAmelCase ) self.assertEqual(k.knapsack(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) , 2_2_0 ) if __name__ == "__main__": unittest.main()
209
import argparse from collections import defaultdict import yaml A : str = '''docs/source/en/_toctree.yml''' def __lowerCamelCase ( __a :str ) -> List[Any]: """simple docstring""" A__ = defaultdict(__a ) A__ = [] A__ = [] for doc in doc_list: if "local" in doc: counts[doc["local"]] += 1 if doc["title"].lower() == "overview": overview_doc.append({"""local""": doc["""local"""], """title""": doc["""title"""]} ) else: new_doc_list.append(__a ) A__ = new_doc_list A__ = [key for key, value in counts.items() if value > 1] A__ = [] for duplicate_key in duplicates: A__ = list({doc["""title"""] for doc in doc_list if doc["""local"""] == duplicate_key} ) if len(__a ) > 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 doc_list if """local""" not in counts or counts[doc["""local"""]] == 1] ) A__ = sorted(__a , key=lambda __a : s["title"].lower() ) # "overview" gets special treatment and is always first if len(__a ) > 1: raise ValueError("""{doc_list} has two 'overview' docs which is not allowed.""" ) overview_doc.extend(__a ) # Sort return overview_doc def __lowerCamelCase ( __a :Any=False ) -> List[str]: """simple docstring""" with open(__a , encoding="""utf-8""" ) as f: A__ = yaml.safe_load(f.read() ) # Get to the API doc A__ = 0 while content[api_idx]["title"] != "API": api_idx += 1 A__ = content[api_idx]["""sections"""] # Then to the model doc A__ = 0 while api_doc[scheduler_idx]["title"] != "Schedulers": scheduler_idx += 1 A__ = api_doc[scheduler_idx]["""sections"""] A__ = clean_doc_toc(__a ) A__ = False if new_scheduler_doc != scheduler_doc: A__ = True if overwrite: A__ = new_scheduler_doc if diff: if overwrite: A__ = api_doc with open(__a , """w""" , encoding="""utf-8""" ) as f: f.write(yaml.dump(__a , allow_unicode=__a ) ) else: raise ValueError( """The model doc part of the table of content is not properly sorted, run `make style` to fix this.""" ) def __lowerCamelCase ( __a :Optional[int]=False ) -> Dict: """simple docstring""" with open(__a , encoding="""utf-8""" ) as f: A__ = yaml.safe_load(f.read() ) # Get to the API doc A__ = 0 while content[api_idx]["title"] != "API": api_idx += 1 A__ = content[api_idx]["""sections"""] # Then to the model doc A__ = 0 while api_doc[pipeline_idx]["title"] != "Pipelines": pipeline_idx += 1 A__ = False A__ = api_doc[pipeline_idx]["""sections"""] A__ = [] # sort sub pipeline docs for pipeline_doc in pipeline_docs: if "section" in pipeline_doc: A__ = pipeline_doc["""section"""] A__ = clean_doc_toc(__a ) if overwrite: A__ = new_sub_pipeline_doc new_pipeline_docs.append(__a ) # sort overall pipeline doc A__ = clean_doc_toc(__a ) if new_pipeline_docs != pipeline_docs: A__ = True if overwrite: A__ = new_pipeline_docs if diff: if overwrite: A__ = api_doc with open(__a , """w""" , encoding="""utf-8""" ) as f: f.write(yaml.dump(__a , allow_unicode=__a ) ) 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__": A : Tuple = argparse.ArgumentParser() parser.add_argument('''--fix_and_overwrite''', action='''store_true''', help='''Whether to fix inconsistencies.''') A : Optional[Any] = parser.parse_args() check_scheduler_doc(args.fix_and_overwrite) check_pipeline_doc(args.fix_and_overwrite)
274
0
from typing import Dict import numpy as np import torch from . import residue_constants as rc from .tensor_utils import tensor_tree_map, tree_map def lowercase_ ( _A : Dict[str, torch.Tensor] ): """simple docstring""" lowerCamelCase__ : Any = [] lowerCamelCase__ : List[Any] = [] lowerCamelCase__ : Optional[Any] = [] for rt in rc.restypes: lowerCamelCase__ : List[str] = rc.restype_name_to_atomaa_names[rc.restype_atoa[rt]] restype_atomaa_to_atomaa_list.append([(rc.atom_order[name] if name else 0) for name in atom_names] ) lowerCamelCase__ : int = {name: i for i, name in enumerate(__a )} restype_atomaa_to_atomaa_list.append( [(atom_name_to_idxaa[name] if name in atom_name_to_idxaa else 0) for name in rc.atom_types] ) restype_atomaa_mask_list.append([(1.0 if name else 0.0) for name in atom_names] ) # Add dummy mapping for restype 'UNK' restype_atomaa_to_atomaa_list.append([0] * 14 ) restype_atomaa_to_atomaa_list.append([0] * 37 ) restype_atomaa_mask_list.append([0.0] * 14 ) lowerCamelCase__ : Union[str, Any] = torch.tensor( __a , dtype=torch.intaa , device=protein["aatype"].device , ) lowerCamelCase__ : int = torch.tensor( __a , dtype=torch.intaa , device=protein["aatype"].device , ) lowerCamelCase__ : Any = torch.tensor( __a , dtype=torch.floataa , device=protein["aatype"].device , ) lowerCamelCase__ : int = protein["aatype"].to(torch.long ) # create the mapping for (residx, atom14) --> atom37, i.e. an array # with shape (num_res, 14) containing the atom37 indices for this protein lowerCamelCase__ : List[str] = restype_atomaa_to_atomaa[protein_aatype] lowerCamelCase__ : Optional[int] = restype_atomaa_mask[protein_aatype] lowerCamelCase__ : Optional[int] = residx_atomaa_mask lowerCamelCase__ : int = residx_atomaa_to_atomaa.long() # create the gather indices for mapping back lowerCamelCase__ : Any = restype_atomaa_to_atomaa[protein_aatype] lowerCamelCase__ : Optional[int] = residx_atomaa_to_atomaa.long() # create the corresponding mask lowerCamelCase__ : Optional[int] = torch.zeros([21, 37] , dtype=torch.floataa , device=protein["aatype"].device ) for restype, restype_letter in enumerate(rc.restypes ): lowerCamelCase__ : Optional[int] = rc.restype_atoa[restype_letter] lowerCamelCase__ : Dict = rc.residue_atoms[restype_name] for atom_name in atom_names: lowerCamelCase__ : Optional[Any] = rc.atom_order[atom_name] lowerCamelCase__ : Tuple = 1 lowerCamelCase__ : Dict = restype_atomaa_mask[protein_aatype] lowerCamelCase__ : Any = residx_atomaa_mask return protein def lowercase_ ( _A : Dict[str, torch.Tensor] ): """simple docstring""" lowerCamelCase__ : Any = tree_map(lambda _A : torch.tensor(__a , device=batch["aatype"].device ) , __a , np.ndarray ) lowerCamelCase__ : Dict = tensor_tree_map(lambda _A : np.array(__a ) , make_atomaa_masks(__a ) ) return out
184
def __lowerCamelCase ( __a :str ) -> list: """simple docstring""" A__ = [0] * len(__a ) for i in range(1 , len(__a ) ): # use last results for better performance - dynamic programming A__ = prefix_result[i - 1] while j > 0 and input_string[i] != input_string[j]: A__ = prefix_result[j - 1] if input_string[i] == input_string[j]: j += 1 A__ = j return prefix_result def __lowerCamelCase ( __a :str ) -> int: """simple docstring""" return max(prefix_function(__a ) ) if __name__ == "__main__": import doctest doctest.testmod()
274
0
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 from ..auto import CONFIG_MAPPING _UpperCamelCase = logging.get_logger(__name__) _UpperCamelCase = { "microsoft/table-transformer-detection": ( "https://huggingface.co/microsoft/table-transformer-detection/resolve/main/config.json" ), } class __lowercase (_UpperCAmelCase ): _UpperCamelCase = """table-transformer""" _UpperCamelCase = ["""past_key_values"""] _UpperCamelCase = { """hidden_size""": """d_model""", """num_attention_heads""": """encoder_attention_heads""", } def __init__( self , A_=True , A_=None , A_=3 , A_=100 , A_=6 , A_=2048 , A_=8 , A_=6 , A_=2048 , A_=8 , A_=0.0 , A_=0.0 , A_=True , A_="relu" , A_=256 , A_=0.1 , A_=0.0 , A_=0.0 , A_=0.02 , A_=1.0 , A_=False , A_="sine" , A_="resnet50" , A_=True , A_=False , A_=1 , A_=5 , A_=2 , A_=1 , A_=1 , A_=5 , A_=2 , A_=0.1 , **A_ , ) ->Any: '''simple docstring''' if backbone_config is not None and use_timm_backbone: raise ValueError('''You can\'t specify both `backbone_config` and `use_timm_backbone`.''' ) if not use_timm_backbone: if backbone_config is None: logger.info('''`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone.''' ) __lowerCAmelCase : Optional[Any] = CONFIG_MAPPING['''resnet'''](out_features=['''stage4'''] ) elif isinstance(A_ , A_ ): __lowerCAmelCase : int = backbone_config.get('''model_type''' ) __lowerCAmelCase : List[str] = CONFIG_MAPPING[backbone_model_type] __lowerCAmelCase : Any = config_class.from_dict(A_ ) # set timm attributes to None __lowerCAmelCase, __lowerCAmelCase, __lowerCAmelCase : List[str] = None, None, None __lowerCAmelCase : Tuple = use_timm_backbone __lowerCAmelCase : Optional[Any] = backbone_config __lowerCAmelCase : List[str] = num_channels __lowerCAmelCase : Tuple = num_queries __lowerCAmelCase : int = d_model __lowerCAmelCase : List[Any] = encoder_ffn_dim __lowerCAmelCase : Optional[int] = encoder_layers __lowerCAmelCase : List[str] = encoder_attention_heads __lowerCAmelCase : str = decoder_ffn_dim __lowerCAmelCase : Union[str, Any] = decoder_layers __lowerCAmelCase : Any = decoder_attention_heads __lowerCAmelCase : Optional[int] = dropout __lowerCAmelCase : Any = attention_dropout __lowerCAmelCase : Tuple = activation_dropout __lowerCAmelCase : Optional[Any] = activation_function __lowerCAmelCase : List[str] = init_std __lowerCAmelCase : Tuple = init_xavier_std __lowerCAmelCase : Any = encoder_layerdrop __lowerCAmelCase : List[Any] = decoder_layerdrop __lowerCAmelCase : Optional[Any] = encoder_layers __lowerCAmelCase : Optional[Any] = auxiliary_loss __lowerCAmelCase : Optional[Any] = position_embedding_type __lowerCAmelCase : Tuple = backbone __lowerCAmelCase : Any = use_pretrained_backbone __lowerCAmelCase : int = dilation # Hungarian matcher __lowerCAmelCase : Dict = class_cost __lowerCAmelCase : List[str] = bbox_cost __lowerCAmelCase : int = giou_cost # Loss coefficients __lowerCAmelCase : Optional[Any] = mask_loss_coefficient __lowerCAmelCase : Tuple = dice_loss_coefficient __lowerCAmelCase : int = bbox_loss_coefficient __lowerCAmelCase : List[Any] = giou_loss_coefficient __lowerCAmelCase : int = eos_coefficient super().__init__(is_encoder_decoder=A_ , **A_ ) @property def UpperCamelCase__ ( self ) ->int: '''simple docstring''' return self.encoder_attention_heads @property def UpperCamelCase__ ( self ) ->int: '''simple docstring''' return self.d_model class __lowercase (_UpperCAmelCase ): _UpperCamelCase = version.parse("""1.11""" ) @property def UpperCamelCase__ ( self ) ->Mapping[str, Mapping[int, str]]: '''simple docstring''' return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ('''pixel_mask''', {0: '''batch'''}), ] ) @property def UpperCamelCase__ ( self ) ->float: '''simple docstring''' return 1e-5 @property def UpperCamelCase__ ( self ) ->int: '''simple docstring''' return 12
275
import unittest import numpy as np import torch from diffusers import PNDMPipeline, PNDMScheduler, UNetaDModel from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device enable_full_determinism() class __lowercase (unittest.TestCase ): @property def UpperCamelCase__ ( self ) ->Tuple: '''simple docstring''' torch.manual_seed(0 ) __lowerCAmelCase : List[Any] = UNetaDModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=3 , out_channels=3 , down_block_types=('''DownBlock2D''', '''AttnDownBlock2D''') , up_block_types=('''AttnUpBlock2D''', '''UpBlock2D''') , ) return model def UpperCamelCase__ ( self ) ->int: '''simple docstring''' __lowerCAmelCase : List[str] = self.dummy_uncond_unet __lowerCAmelCase : Any = PNDMScheduler() __lowerCAmelCase : Dict = PNDMPipeline(unet=A_ , scheduler=A_ ) pndm.to(A_ ) pndm.set_progress_bar_config(disable=A_ ) __lowerCAmelCase : Optional[Any] = torch.manual_seed(0 ) __lowerCAmelCase : Any = pndm(generator=A_ , num_inference_steps=20 , output_type='''numpy''' ).images __lowerCAmelCase : Optional[Any] = torch.manual_seed(0 ) __lowerCAmelCase : List[Any] = pndm(generator=A_ , num_inference_steps=20 , output_type='''numpy''' , return_dict=A_ )[0] __lowerCAmelCase : Tuple = image[0, -3:, -3:, -1] __lowerCAmelCase : Optional[Any] = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) __lowerCAmelCase : int = np.array([1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 @slow @require_torch class __lowercase (unittest.TestCase ): def UpperCamelCase__ ( self ) ->Optional[Any]: '''simple docstring''' __lowerCAmelCase : Optional[int] = '''google/ddpm-cifar10-32''' __lowerCAmelCase : Union[str, Any] = UNetaDModel.from_pretrained(A_ ) __lowerCAmelCase : int = PNDMScheduler() __lowerCAmelCase : Any = PNDMPipeline(unet=A_ , scheduler=A_ ) pndm.to(A_ ) pndm.set_progress_bar_config(disable=A_ ) __lowerCAmelCase : Tuple = torch.manual_seed(0 ) __lowerCAmelCase : Any = pndm(generator=A_ , output_type='''numpy''' ).images __lowerCAmelCase : Optional[int] = image[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) __lowerCAmelCase : List[Any] = np.array([0.1_564, 0.14_645, 0.1_406, 0.14_715, 0.12_425, 0.14_045, 0.13_115, 0.12_175, 0.125] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
275
1